]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/bsd_stubs.c
xnu-792.10.96.tar.gz
[apple/xnu.git] / bsd / kern / bsd_stubs.c
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
37839358
A
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
1c79356b 11 *
37839358
A
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
1c79356b
A
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
37839358
A
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
1c79356b
A
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22#include <sys/time.h>
23#include <kern/task.h>
24#include <kern/thread.h>
25#include <mach/mach_types.h>
26#include <mach/vm_prot.h>
27#include <vm/vm_kern.h>
28#include <vm/vm_map.h>
29#include <sys/systm.h>
30#include <sys/conf.h>
91447636 31#include <sys/proc_internal.h>
1c79356b 32#include <sys/buf.h> /* for SET */
0b4e3aa0 33#include <sys/user.h>
c0fea474
A
34#include <sys/sysent.h>
35#include <sys/sysproto.h>
1c79356b
A
36
37/* Just to satisfy pstat command */
38int dmmin, dmmax, dmtext;
39
91447636 40vm_offset_t
1c79356b
A
41kmem_mb_alloc(vm_map_t mbmap, int size)
42{
43 vm_offset_t addr;
44 if (kernel_memory_allocate(mbmap, &addr, size,
45 0,
c0fea474 46 KMA_NOPAGEWAIT|KMA_KOBJECT|KMA_LOMEM) == KERN_SUCCESS)
55e303ae 47 return(addr);
1c79356b
A
48 else
49 return(0);
50
51}
52
91447636
A
53/*
54 * XXX this function only exists to be exported and do nothing.
55 */
56void
57pcb_synch(void)
58{
59}
1c79356b
A
60
61struct proc *
62current_proc(void)
63{
64 /* Never returns a NULL */
0b4e3aa0
A
65 struct uthread * ut;
66 struct proc *p;
91447636 67 thread_t thr_act = current_thread();
0b4e3aa0
A
68
69 ut = (struct uthread *)get_bsdthread_info(thr_act);
91447636 70 if (ut && (ut->uu_flag & UT_VFORK) && ut->uu_proc) {
0b4e3aa0
A
71 p = ut->uu_proc;
72 if ((p->p_flag & P_INVFORK) == 0)
73 panic("returning child proc not under vfork");
74 if (p->p_vforkact != (void *)thr_act)
75 panic("returning child proc which is not cur_act");
76 return(p);
77 }
78
79 p = (struct proc *)get_bsdtask_info(current_task());
80
1c79356b 81 if (p == NULL)
0b4e3aa0
A
82 return (kernproc);
83
1c79356b
A
84 return (p);
85}
86
87/* Device switch add delete routines */
88
89extern int nblkdev, nchrdev;
90
91struct bdevsw nobdev = NO_BDEVICE;
92struct cdevsw nocdev = NO_CDEVICE;
93/*
94 * if index is -1, return a free slot if avaliable
95 * else see whether the index is free
96 * return the major number that is free else -1
97 *
98 */
99int
100bdevsw_isfree(int index)
101{
102 struct bdevsw *devsw;
103 if (index == -1) {
104 devsw = bdevsw;
105 for(index=0; index < nblkdev; index++, devsw++) {
106 if(memcmp((char *)devsw,
107 (char *)&nobdev,
108 sizeof(struct bdevsw)) == 0)
109 break;
110 }
55e303ae
A
111 } else {
112 /* NB: Not used below unless index is in range */
113 devsw = &bdevsw[index];
1c79356b
A
114 }
115
116 if ((index < 0) || (index >= nblkdev) ||
117 (memcmp((char *)devsw,
118 (char *)&nobdev,
119 sizeof(struct bdevsw)) != 0)) {
120 return(-1);
121 }
122 return(index);
123}
124
125/*
126 * if index is -1, find a free slot to add
127 * else see whether the slot is free
128 * return the major number that is used else -1
129 */
130int
131bdevsw_add(int index, struct bdevsw * bsw)
132{
133 struct bdevsw *devsw;
134
135 if (index == -1) {
55e303ae 136 devsw = &bdevsw[1]; /* Start at slot 1 - this is a hack to fix the index=1 hack */
9bccf70c
A
137 /* yes, start at 1 to avoid collision with volfs (Radar 2842228) */
138 for(index=1; index < nblkdev; index++, devsw++) {
1c79356b
A
139 if(memcmp((char *)devsw,
140 (char *)&nobdev,
141 sizeof(struct bdevsw)) == 0)
142 break;
143 }
144 }
145 devsw = &bdevsw[index];
146 if ((index < 0) || (index >= nblkdev) ||
147 (memcmp((char *)devsw,
148 (char *)&nobdev,
149 sizeof(struct bdevsw)) != 0)) {
150 return(-1);
151 }
152 bdevsw[index] = *bsw;
153 return(index);
154}
155/*
156 * if the slot has the same bsw, then remove
157 * else -1
158 */
159int
160bdevsw_remove(int index, struct bdevsw * bsw)
161{
162 struct bdevsw *devsw;
163
164 devsw = &bdevsw[index];
165 if ((index < 0) || (index >= nblkdev) ||
166 (memcmp((char *)devsw,
167 (char *)bsw,
168 sizeof(struct bdevsw)) != 0)) {
169 return(-1);
170 }
171 bdevsw[index] = nobdev;
172 return(index);
173}
174
175/*
176 * if index is -1, return a free slot if avaliable
177 * else see whether the index is free
178 * return the major number that is free else -1
179 */
180int
181cdevsw_isfree(int index)
182{
183 struct cdevsw *devsw;
184
185 if (index == -1) {
186 devsw = cdevsw;
187 for(index=0; index < nchrdev; index++, devsw++) {
188 if(memcmp((char *)devsw,
189 (char *)&nocdev,
190 sizeof(struct cdevsw)) == 0)
191 break;
192 }
193 }
194 devsw = &cdevsw[index];
195 if ((index < 0) || (index >= nchrdev) ||
196 (memcmp((char *)devsw,
197 (char *)&nocdev,
198 sizeof(struct cdevsw)) != 0)) {
199 return(-1);
200 }
201 return(index);
202}
203
204/*
205 * if index is -1, find a free slot to add
206 * else see whether the slot is free
207 * return the major number that is used else -1
208 */
209int
210cdevsw_add(int index, struct cdevsw * csw)
211{
212 struct cdevsw *devsw;
213
214 if (index == -1) {
215 devsw = cdevsw;
216 for(index=0; index < nchrdev; index++, devsw++) {
217 if(memcmp((char *)devsw,
218 (char *)&nocdev,
219 sizeof(struct cdevsw)) == 0)
220 break;
221 }
222 }
223 devsw = &cdevsw[index];
224 if ((index < 0) || (index >= nchrdev) ||
225 (memcmp((char *)devsw,
226 (char *)&nocdev,
227 sizeof(struct cdevsw)) != 0)) {
228 return(-1);
229 }
230 cdevsw[index] = *csw;
231 return(index);
232}
233/*
234 * if the index has the same bsw, then remove
235 * else -1
236 */
237int
238cdevsw_remove(int index, struct cdevsw * csw)
239{
240 struct cdevsw *devsw;
241
242 devsw = &cdevsw[index];
243 if ((index < 0) || (index >= nchrdev) ||
244 (memcmp((char *)devsw,
245 (char *)csw,
246 sizeof(struct cdevsw)) != 0)) {
247 return(-1);
248 }
249 cdevsw[index] = nocdev;
250 return(index);
251}
252
9bccf70c
A
253static int
254cdev_set_bdev(int cdev, int bdev)
255{
256 extern int chrtoblk_add(int cdev, int bdev);
257
258 return (chrtoblk_set(cdev, bdev));
259}
260
261int
262cdevsw_add_with_bdev(int index, struct cdevsw * csw, int bdev)
263{
264 index = cdevsw_add(index, csw);
265 if (index < 0) {
266 return (index);
267 }
268 if (cdev_set_bdev(index, bdev) < 0) {
269 cdevsw_remove(index, csw);
270 return (-1);
271 }
272 return (index);
273}
274
1c79356b
A
275issingleuser(void)
276{
277 char namep[16];
278
279
280 if (PE_parse_boot_arg("-s", namep)) {
281 return(1);
282 } else {
283 return(0);
284 }
285}
286
287void *
288tbeproc(void *procp)
289{
290 struct proc *p = procp;
291
292 if (p)
293 SET(p->p_flag, P_TBE);
294 return;
295}
296
c0fea474
A
297
298/*
299 * WARNING - this is a temporary workaround for binary compatibility issues
300 * with anti-piracy software that relies on patching ptrace (3928003).
301 * This KPI will be removed in the system release after Tiger.
302 */
303uintptr_t temp_patch_ptrace(uintptr_t new_ptrace)
304{
305 struct sysent * callp;
306 sy_call_t * old_ptrace;
307#ifndef __ppc__
308 boolean_t funnel_state;
309#endif
310
311 if (new_ptrace == 0)
312 return(0);
313
314#ifdef __ppc__
315 enter_funnel_section(kernel_flock);
316#else
317 funnel_state = thread_funnel_set(kernel_flock, TRUE);
318#endif
319 callp = &sysent[26];
320 old_ptrace = callp->sy_call;
321
322 /* only allow one patcher of ptrace */
323 if (old_ptrace == (sy_call_t *) ptrace) {
324 callp->sy_call = (sy_call_t *) new_ptrace;
325 }
326 else {
327 old_ptrace = NULL;
328 }
329#ifdef __ppc__
330 exit_funnel_section( );
331#else
332 (void)thread_funnel_set(kernel_flock, funnel_state);
333#endif
334
335 return((uintptr_t)old_ptrace);
336}
337
338void temp_unpatch_ptrace(void)
339{
340 struct sysent * callp;
341#ifndef __ppc__
342 boolean_t funnel_state;
343#endif
344
345#ifdef __ppc__
346 enter_funnel_section(kernel_flock);
347#else
348 funnel_state = thread_funnel_set(kernel_flock, TRUE);
349#endif
350 callp = &sysent[26];
351 callp->sy_call = (sy_call_t *) ptrace;
352#ifdef __ppc__
353 exit_funnel_section( );
354#else
355 (void)thread_funnel_set(kernel_flock, funnel_state);
356#endif
357
358 return;
359}