]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/bsd_stubs.c
xnu-792.6.56.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_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 #include <sys/time.h>
24 #include <kern/task.h>
25 #include <kern/thread.h>
26 #include <mach/mach_types.h>
27 #include <mach/vm_prot.h>
28 #include <vm/vm_kern.h>
29 #include <vm/vm_map.h>
30 #include <sys/systm.h>
31 #include <sys/conf.h>
32 #include <sys/proc_internal.h>
33 #include <sys/buf.h> /* for SET */
34 #include <sys/user.h>
35
36 /* Just to satisfy pstat command */
37 int dmmin, dmmax, dmtext;
38
39 vm_offset_t
40 kmem_mb_alloc(vm_map_t mbmap, int size)
41 {
42 vm_offset_t addr;
43 if (kernel_memory_allocate(mbmap, &addr, size,
44 0,
45 KMA_NOPAGEWAIT|KMA_KOBJECT) == KERN_SUCCESS)
46 return(addr);
47 else
48 return(0);
49
50 }
51
52 /*
53 * XXX this function only exists to be exported and do nothing.
54 */
55 void
56 pcb_synch(void)
57 {
58 }
59
60 struct proc *
61 current_proc(void)
62 {
63 /* Never returns a NULL */
64 struct uthread * ut;
65 struct proc *p;
66 thread_t thr_act = current_thread();
67
68 ut = (struct uthread *)get_bsdthread_info(thr_act);
69 if (ut && (ut->uu_flag & UT_VFORK) && ut->uu_proc) {
70 p = ut->uu_proc;
71 if ((p->p_flag & P_INVFORK) == 0)
72 panic("returning child proc not under vfork");
73 if (p->p_vforkact != (void *)thr_act)
74 panic("returning child proc which is not cur_act");
75 return(p);
76 }
77
78 p = (struct proc *)get_bsdtask_info(current_task());
79
80 if (p == NULL)
81 return (kernproc);
82
83 return (p);
84 }
85
86 /* Device switch add delete routines */
87
88 extern int nblkdev, nchrdev;
89
90 struct bdevsw nobdev = NO_BDEVICE;
91 struct cdevsw nocdev = NO_CDEVICE;
92 /*
93 * if index is -1, return a free slot if avaliable
94 * else see whether the index is free
95 * return the major number that is free else -1
96 *
97 */
98 int
99 bdevsw_isfree(int index)
100 {
101 struct bdevsw *devsw;
102 if (index == -1) {
103 devsw = bdevsw;
104 for(index=0; index < nblkdev; index++, devsw++) {
105 if(memcmp((char *)devsw,
106 (char *)&nobdev,
107 sizeof(struct bdevsw)) == 0)
108 break;
109 }
110 } else {
111 /* NB: Not used below unless index is in range */
112 devsw = &bdevsw[index];
113 }
114
115 if ((index < 0) || (index >= nblkdev) ||
116 (memcmp((char *)devsw,
117 (char *)&nobdev,
118 sizeof(struct bdevsw)) != 0)) {
119 return(-1);
120 }
121 return(index);
122 }
123
124 /*
125 * if index is -1, find a free slot to add
126 * else see whether the slot is free
127 * return the major number that is used else -1
128 */
129 int
130 bdevsw_add(int index, struct bdevsw * bsw)
131 {
132 struct bdevsw *devsw;
133
134 if (index == -1) {
135 devsw = &bdevsw[1]; /* Start at slot 1 - this is a hack to fix the index=1 hack */
136 /* yes, start at 1 to avoid collision with volfs (Radar 2842228) */
137 for(index=1; index < nblkdev; index++, devsw++) {
138 if(memcmp((char *)devsw,
139 (char *)&nobdev,
140 sizeof(struct bdevsw)) == 0)
141 break;
142 }
143 }
144 devsw = &bdevsw[index];
145 if ((index < 0) || (index >= nblkdev) ||
146 (memcmp((char *)devsw,
147 (char *)&nobdev,
148 sizeof(struct bdevsw)) != 0)) {
149 return(-1);
150 }
151 bdevsw[index] = *bsw;
152 return(index);
153 }
154 /*
155 * if the slot has the same bsw, then remove
156 * else -1
157 */
158 int
159 bdevsw_remove(int index, struct bdevsw * bsw)
160 {
161 struct bdevsw *devsw;
162
163 devsw = &bdevsw[index];
164 if ((index < 0) || (index >= nblkdev) ||
165 (memcmp((char *)devsw,
166 (char *)bsw,
167 sizeof(struct bdevsw)) != 0)) {
168 return(-1);
169 }
170 bdevsw[index] = nobdev;
171 return(index);
172 }
173
174 /*
175 * if index is -1, return a free slot if avaliable
176 * else see whether the index is free
177 * return the major number that is free else -1
178 */
179 int
180 cdevsw_isfree(int index)
181 {
182 struct cdevsw *devsw;
183
184 if (index == -1) {
185 devsw = cdevsw;
186 for(index=0; index < nchrdev; index++, devsw++) {
187 if(memcmp((char *)devsw,
188 (char *)&nocdev,
189 sizeof(struct cdevsw)) == 0)
190 break;
191 }
192 }
193 devsw = &cdevsw[index];
194 if ((index < 0) || (index >= nchrdev) ||
195 (memcmp((char *)devsw,
196 (char *)&nocdev,
197 sizeof(struct cdevsw)) != 0)) {
198 return(-1);
199 }
200 return(index);
201 }
202
203 /*
204 * if index is -1, find a free slot to add
205 * else see whether the slot is free
206 * return the major number that is used else -1
207 */
208 int
209 cdevsw_add(int index, struct cdevsw * csw)
210 {
211 struct cdevsw *devsw;
212
213 if (index == -1) {
214 devsw = cdevsw;
215 for(index=0; index < nchrdev; index++, devsw++) {
216 if(memcmp((char *)devsw,
217 (char *)&nocdev,
218 sizeof(struct cdevsw)) == 0)
219 break;
220 }
221 }
222 devsw = &cdevsw[index];
223 if ((index < 0) || (index >= nchrdev) ||
224 (memcmp((char *)devsw,
225 (char *)&nocdev,
226 sizeof(struct cdevsw)) != 0)) {
227 return(-1);
228 }
229 cdevsw[index] = *csw;
230 return(index);
231 }
232 /*
233 * if the index has the same bsw, then remove
234 * else -1
235 */
236 int
237 cdevsw_remove(int index, struct cdevsw * csw)
238 {
239 struct cdevsw *devsw;
240
241 devsw = &cdevsw[index];
242 if ((index < 0) || (index >= nchrdev) ||
243 (memcmp((char *)devsw,
244 (char *)csw,
245 sizeof(struct cdevsw)) != 0)) {
246 return(-1);
247 }
248 cdevsw[index] = nocdev;
249 return(index);
250 }
251
252 static int
253 cdev_set_bdev(int cdev, int bdev)
254 {
255 extern int chrtoblk_add(int cdev, int bdev);
256
257 return (chrtoblk_set(cdev, bdev));
258 }
259
260 int
261 cdevsw_add_with_bdev(int index, struct cdevsw * csw, int bdev)
262 {
263 index = cdevsw_add(index, csw);
264 if (index < 0) {
265 return (index);
266 }
267 if (cdev_set_bdev(index, bdev) < 0) {
268 cdevsw_remove(index, csw);
269 return (-1);
270 }
271 return (index);
272 }
273
274 issingleuser(void)
275 {
276 char namep[16];
277
278
279 if (PE_parse_boot_arg("-s", namep)) {
280 return(1);
281 } else {
282 return(0);
283 }
284 }
285
286 void *
287 tbeproc(void *procp)
288 {
289 struct proc *p = procp;
290
291 if (p)
292 SET(p->p_flag, P_TBE);
293 return;
294 }
295