]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/bsd_stubs.c
xnu-1228.5.18.tar.gz
[apple/xnu.git] / bsd / kern / bsd_stubs.c
CommitLineData
1c79356b 1/*
5d5c5d0d
A
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
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 License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28#include <sys/time.h>
29#include <kern/task.h>
30#include <kern/thread.h>
31#include <mach/mach_types.h>
32#include <mach/vm_prot.h>
33#include <vm/vm_kern.h>
34#include <vm/vm_map.h>
35#include <sys/systm.h>
36#include <sys/conf.h>
91447636 37#include <sys/proc_internal.h>
1c79356b 38#include <sys/buf.h> /* for SET */
0b4e3aa0 39#include <sys/user.h>
0c530ab8
A
40#include <sys/sysent.h>
41#include <sys/sysproto.h>
1c79356b 42
2d21ac55
A
43/* XXX these should be in a common header somwhere, but aren't */
44extern int chrtoblk_set(int, int);
45extern vm_offset_t kmem_mb_alloc(vm_map_t, int);
46
47/* XXX most of these just exist to export; there's no good header for them*/
48void pcb_synch(void);
49int issingleuser(void);
50void tbeproc(void *);
51
52
1c79356b
A
53/* Just to satisfy pstat command */
54int dmmin, dmmax, dmtext;
55
91447636 56vm_offset_t
1c79356b
A
57kmem_mb_alloc(vm_map_t mbmap, int size)
58{
59 vm_offset_t addr;
60 if (kernel_memory_allocate(mbmap, &addr, size,
61 0,
0c530ab8 62 KMA_NOPAGEWAIT|KMA_KOBJECT|KMA_LOMEM) == KERN_SUCCESS)
55e303ae 63 return(addr);
1c79356b
A
64 else
65 return(0);
66
67}
68
91447636
A
69/*
70 * XXX this function only exists to be exported and do nothing.
71 */
72void
73pcb_synch(void)
74{
75}
1c79356b
A
76
77struct proc *
78current_proc(void)
79{
80 /* Never returns a NULL */
0b4e3aa0
A
81 struct uthread * ut;
82 struct proc *p;
2d21ac55 83 thread_t thread = current_thread();
0b4e3aa0 84
2d21ac55 85 ut = (struct uthread *)get_bsdthread_info(thread);
91447636 86 if (ut && (ut->uu_flag & UT_VFORK) && ut->uu_proc) {
0b4e3aa0 87 p = ut->uu_proc;
2d21ac55 88 if ((p->p_lflag & P_LINVFORK) == 0)
0b4e3aa0 89 panic("returning child proc not under vfork");
2d21ac55 90 if (p->p_vforkact != (void *)thread)
0b4e3aa0
A
91 panic("returning child proc which is not cur_act");
92 return(p);
93 }
94
95 p = (struct proc *)get_bsdtask_info(current_task());
96
1c79356b 97 if (p == NULL)
0b4e3aa0
A
98 return (kernproc);
99
1c79356b
A
100 return (p);
101}
102
103/* Device switch add delete routines */
104
1c79356b
A
105struct bdevsw nobdev = NO_BDEVICE;
106struct cdevsw nocdev = NO_CDEVICE;
107/*
108 * if index is -1, return a free slot if avaliable
109 * else see whether the index is free
110 * return the major number that is free else -1
111 *
112 */
113int
114bdevsw_isfree(int index)
115{
116 struct bdevsw *devsw;
117 if (index == -1) {
118 devsw = bdevsw;
119 for(index=0; index < nblkdev; index++, devsw++) {
120 if(memcmp((char *)devsw,
121 (char *)&nobdev,
122 sizeof(struct bdevsw)) == 0)
123 break;
124 }
55e303ae
A
125 } else {
126 /* NB: Not used below unless index is in range */
127 devsw = &bdevsw[index];
1c79356b
A
128 }
129
130 if ((index < 0) || (index >= nblkdev) ||
131 (memcmp((char *)devsw,
132 (char *)&nobdev,
133 sizeof(struct bdevsw)) != 0)) {
134 return(-1);
135 }
136 return(index);
137}
138
139/*
140 * if index is -1, find a free slot to add
141 * else see whether the slot is free
142 * return the major number that is used else -1
143 */
144int
145bdevsw_add(int index, struct bdevsw * bsw)
146{
147 struct bdevsw *devsw;
148
149 if (index == -1) {
55e303ae 150 devsw = &bdevsw[1]; /* Start at slot 1 - this is a hack to fix the index=1 hack */
9bccf70c
A
151 /* yes, start at 1 to avoid collision with volfs (Radar 2842228) */
152 for(index=1; index < nblkdev; index++, devsw++) {
1c79356b
A
153 if(memcmp((char *)devsw,
154 (char *)&nobdev,
155 sizeof(struct bdevsw)) == 0)
156 break;
157 }
158 }
159 devsw = &bdevsw[index];
160 if ((index < 0) || (index >= nblkdev) ||
161 (memcmp((char *)devsw,
162 (char *)&nobdev,
163 sizeof(struct bdevsw)) != 0)) {
164 return(-1);
165 }
166 bdevsw[index] = *bsw;
167 return(index);
168}
169/*
170 * if the slot has the same bsw, then remove
171 * else -1
172 */
173int
174bdevsw_remove(int index, struct bdevsw * bsw)
175{
176 struct bdevsw *devsw;
177
178 devsw = &bdevsw[index];
179 if ((index < 0) || (index >= nblkdev) ||
180 (memcmp((char *)devsw,
181 (char *)bsw,
182 sizeof(struct bdevsw)) != 0)) {
183 return(-1);
184 }
185 bdevsw[index] = nobdev;
186 return(index);
187}
188
189/*
190 * if index is -1, return a free slot if avaliable
191 * else see whether the index is free
192 * return the major number that is free else -1
193 */
194int
195cdevsw_isfree(int index)
196{
197 struct cdevsw *devsw;
198
199 if (index == -1) {
200 devsw = cdevsw;
201 for(index=0; index < nchrdev; index++, devsw++) {
202 if(memcmp((char *)devsw,
203 (char *)&nocdev,
204 sizeof(struct cdevsw)) == 0)
205 break;
206 }
207 }
208 devsw = &cdevsw[index];
209 if ((index < 0) || (index >= nchrdev) ||
210 (memcmp((char *)devsw,
211 (char *)&nocdev,
212 sizeof(struct cdevsw)) != 0)) {
213 return(-1);
214 }
215 return(index);
216}
217
218/*
219 * if index is -1, find a free slot to add
220 * else see whether the slot is free
221 * return the major number that is used else -1
2d21ac55
A
222 *
223 * NOTE: In practice, -1 is unusable, since there are kernel internal
224 * devices that call this function with absolute index values,
225 * which will stomp on free-slot based assignments that happen
226 * before them. Therefore, if index is negative, we start
227 * looking for a free slot at the absolute value of index,
228 * instead of starting at 0 (lets out slot 1, but that's one
229 * of the problem slots down low - the vndevice). -12 is
230 * currently a safe starting point.
1c79356b
A
231 */
232int
233cdevsw_add(int index, struct cdevsw * csw)
234{
235 struct cdevsw *devsw;
236
2d21ac55
A
237 if (index < 0) {
238 if (index == -1)
239 index = 0; /* historical behaviour; XXX broken */
240 else
241 index = -index; /* start at least this far up in the table */
242 devsw = &cdevsw[index];
243 for(; index < nchrdev; index++, devsw++) {
1c79356b
A
244 if(memcmp((char *)devsw,
245 (char *)&nocdev,
246 sizeof(struct cdevsw)) == 0)
247 break;
248 }
249 }
250 devsw = &cdevsw[index];
251 if ((index < 0) || (index >= nchrdev) ||
252 (memcmp((char *)devsw,
253 (char *)&nocdev,
254 sizeof(struct cdevsw)) != 0)) {
255 return(-1);
256 }
257 cdevsw[index] = *csw;
258 return(index);
259}
260/*
261 * if the index has the same bsw, then remove
262 * else -1
263 */
264int
265cdevsw_remove(int index, struct cdevsw * csw)
266{
267 struct cdevsw *devsw;
268
269 devsw = &cdevsw[index];
270 if ((index < 0) || (index >= nchrdev) ||
271 (memcmp((char *)devsw,
272 (char *)csw,
273 sizeof(struct cdevsw)) != 0)) {
274 return(-1);
275 }
276 cdevsw[index] = nocdev;
277 return(index);
278}
279
9bccf70c
A
280static int
281cdev_set_bdev(int cdev, int bdev)
282{
9bccf70c
A
283 return (chrtoblk_set(cdev, bdev));
284}
285
286int
287cdevsw_add_with_bdev(int index, struct cdevsw * csw, int bdev)
288{
289 index = cdevsw_add(index, csw);
290 if (index < 0) {
291 return (index);
292 }
293 if (cdev_set_bdev(index, bdev) < 0) {
294 cdevsw_remove(index, csw);
295 return (-1);
296 }
297 return (index);
298}
299
2d21ac55
A
300#include <pexpert/pexpert.h> /* for PE_parse_boot_arg */
301
302/*
303 * Notes: This function is used solely by UFS, apparently in an effort
304 * to work around an issue with single user mounts.
305 *
306 * It's not technically correct to reference PE_parse_boot_arg()
307 * from this file.
308 */
309int
1c79356b
A
310issingleuser(void)
311{
312 char namep[16];
313
1c79356b
A
314 if (PE_parse_boot_arg("-s", namep)) {
315 return(1);
316 } else {
317 return(0);
318 }
319}
320
2d21ac55 321void
1c79356b
A
322tbeproc(void *procp)
323{
324 struct proc *p = procp;
325
326 if (p)
2d21ac55 327 OSBitOrAtomic(P_TBE, (UInt32 *)&p->p_flag);
1c79356b
A
328 return;
329}
330