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