]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/bsd_stubs.c
xnu-1504.9.17.tar.gz
[apple/xnu.git] / bsd / kern / bsd_stubs.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_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. 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.
14 *
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
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>
37 #include <sys/proc_internal.h>
38 #include <sys/buf.h> /* for SET */
39 #include <sys/kernel.h>
40 #include <sys/user.h>
41 #include <sys/sysent.h>
42 #include <sys/sysproto.h>
43
44 /* XXX these should be in a common header somwhere, but aren't */
45 extern int chrtoblk_set(int, int);
46 extern vm_offset_t kmem_mb_alloc(vm_map_t, int, int);
47
48 /* XXX most of these just exist to export; there's no good header for them*/
49 void pcb_synch(void);
50 void tbeproc(void *);
51
52
53 /* Just to satisfy pstat command */
54 int dmmin, dmmax, dmtext;
55
56 vm_offset_t
57 kmem_mb_alloc(vm_map_t mbmap, int size, int physContig)
58 {
59 vm_offset_t addr = 0;
60 kern_return_t kr = KERN_SUCCESS;
61
62 if(!physContig)
63 kr = kernel_memory_allocate(mbmap, &addr, size,
64 0, KMA_NOPAGEWAIT|KMA_KOBJECT|KMA_LOMEM);
65 else
66 kr = kmem_alloc_contig(mbmap, &addr, size, PAGE_MASK,
67 0xfffff, 0, KMA_NOPAGEWAIT | KMA_KOBJECT | KMA_LOMEM);
68
69 if( kr != KERN_SUCCESS)
70 addr = 0;
71
72 return addr;
73 }
74
75 /*
76 * XXX this function only exists to be exported and do nothing.
77 */
78 void
79 pcb_synch(void)
80 {
81 }
82
83 struct proc *
84 current_proc(void)
85 {
86 /* Never returns a NULL */
87 struct uthread * ut;
88 struct proc *p;
89 thread_t thread = current_thread();
90
91 ut = (struct uthread *)get_bsdthread_info(thread);
92 if (ut && (ut->uu_flag & UT_VFORK) && ut->uu_proc) {
93 p = ut->uu_proc;
94 if ((p->p_lflag & P_LINVFORK) == 0)
95 panic("returning child proc not under vfork");
96 if (p->p_vforkact != (void *)thread)
97 panic("returning child proc which is not cur_act");
98 return(p);
99 }
100
101 p = (struct proc *)get_bsdtask_info(current_task());
102
103 if (p == NULL)
104 return (kernproc);
105
106 return (p);
107 }
108
109 /* Device switch add delete routines */
110
111 struct bdevsw nobdev = NO_BDEVICE;
112 struct cdevsw nocdev = NO_CDEVICE;
113 /*
114 * if index is -1, return a free slot if avaliable
115 * else see whether the index is free
116 * return the major number that is free else -1
117 *
118 */
119 int
120 bdevsw_isfree(int index)
121 {
122 struct bdevsw *devsw;
123 if (index == -1) {
124 devsw = bdevsw;
125 for(index=0; index < nblkdev; index++, devsw++) {
126 if(memcmp((char *)devsw,
127 (char *)&nobdev,
128 sizeof(struct bdevsw)) == 0)
129 break;
130 }
131 } else {
132 /* NB: Not used below unless index is in range */
133 devsw = &bdevsw[index];
134 }
135
136 if ((index < 0) || (index >= nblkdev) ||
137 (memcmp((char *)devsw,
138 (char *)&nobdev,
139 sizeof(struct bdevsw)) != 0)) {
140 return(-1);
141 }
142 return(index);
143 }
144
145 /*
146 * if index is -1, find a free slot to add
147 * else see whether the slot is free
148 * return the major number that is used else -1
149 */
150 int
151 bdevsw_add(int index, struct bdevsw * bsw)
152 {
153 struct bdevsw *devsw;
154
155 if (index == -1) {
156 devsw = &bdevsw[1]; /* Start at slot 1 - this is a hack to fix the index=1 hack */
157 /* yes, start at 1 to avoid collision with volfs (Radar 2842228) */
158 for(index=1; index < nblkdev; index++, devsw++) {
159 if(memcmp((char *)devsw,
160 (char *)&nobdev,
161 sizeof(struct bdevsw)) == 0)
162 break;
163 }
164 }
165 devsw = &bdevsw[index];
166 if ((index < 0) || (index >= nblkdev) ||
167 (memcmp((char *)devsw,
168 (char *)&nobdev,
169 sizeof(struct bdevsw)) != 0)) {
170 return(-1);
171 }
172 bdevsw[index] = *bsw;
173 return(index);
174 }
175 /*
176 * if the slot has the same bsw, then remove
177 * else -1
178 */
179 int
180 bdevsw_remove(int index, struct bdevsw * bsw)
181 {
182 struct bdevsw *devsw;
183
184 devsw = &bdevsw[index];
185 if ((index < 0) || (index >= nblkdev) ||
186 (memcmp((char *)devsw,
187 (char *)bsw,
188 sizeof(struct bdevsw)) != 0)) {
189 return(-1);
190 }
191 bdevsw[index] = nobdev;
192 return(index);
193 }
194
195 /*
196 * if index is -1, return a free slot if avaliable
197 * else see whether the index is free
198 * return the major number that is free else -1
199 */
200 int
201 cdevsw_isfree(int index)
202 {
203 struct cdevsw *devsw;
204
205 if (index == -1) {
206 devsw = cdevsw;
207 for(index=0; index < nchrdev; index++, devsw++) {
208 if(memcmp((char *)devsw,
209 (char *)&nocdev,
210 sizeof(struct cdevsw)) == 0)
211 break;
212 }
213 }
214 devsw = &cdevsw[index];
215 if ((index < 0) || (index >= nchrdev) ||
216 (memcmp((char *)devsw,
217 (char *)&nocdev,
218 sizeof(struct cdevsw)) != 0)) {
219 return(-1);
220 }
221 return(index);
222 }
223
224 /*
225 * if index is -1, find a free slot to add
226 * else see whether the slot is free
227 * return the major number that is used else -1
228 *
229 * NOTE: In practice, -1 is unusable, since there are kernel internal
230 * devices that call this function with absolute index values,
231 * which will stomp on free-slot based assignments that happen
232 * before them. Therefore, if index is negative, we start
233 * looking for a free slot at the absolute value of index,
234 * instead of starting at 0 (lets out slot 1, but that's one
235 * of the problem slots down low - the vndevice). -12 is
236 * currently a safe starting point.
237 */
238 int
239 cdevsw_add(int index, struct cdevsw * csw)
240 {
241 struct cdevsw *devsw;
242
243 if (index < 0) {
244 if (index == -1)
245 index = 0; /* historical behaviour; XXX broken */
246 else
247 index = -index; /* start at least this far up in the table */
248 devsw = &cdevsw[index];
249 for(; index < nchrdev; index++, devsw++) {
250 if(memcmp((char *)devsw,
251 (char *)&nocdev,
252 sizeof(struct cdevsw)) == 0)
253 break;
254 }
255 }
256 devsw = &cdevsw[index];
257 if ((index < 0) || (index >= nchrdev) ||
258 (memcmp((char *)devsw,
259 (char *)&nocdev,
260 sizeof(struct cdevsw)) != 0)) {
261 return(-1);
262 }
263 cdevsw[index] = *csw;
264 return(index);
265 }
266 /*
267 * if the index has the same bsw, then remove
268 * else -1
269 */
270 int
271 cdevsw_remove(int index, struct cdevsw * csw)
272 {
273 struct cdevsw *devsw;
274
275 devsw = &cdevsw[index];
276 if ((index < 0) || (index >= nchrdev) ||
277 (memcmp((char *)devsw,
278 (char *)csw,
279 sizeof(struct cdevsw)) != 0)) {
280 return(-1);
281 }
282 cdevsw[index] = nocdev;
283 return(index);
284 }
285
286 static int
287 cdev_set_bdev(int cdev, int bdev)
288 {
289 return (chrtoblk_set(cdev, bdev));
290 }
291
292 int
293 cdevsw_add_with_bdev(int index, struct cdevsw * csw, int bdev)
294 {
295 index = cdevsw_add(index, csw);
296 if (index < 0) {
297 return (index);
298 }
299 if (cdev_set_bdev(index, bdev) < 0) {
300 cdevsw_remove(index, csw);
301 return (-1);
302 }
303 return (index);
304 }
305
306 #include <pexpert/pexpert.h> /* for PE_parse_boot_arg */
307
308 void
309 tbeproc(void *procp)
310 {
311 struct proc *p = procp;
312
313 if (p)
314 OSBitOrAtomic(P_TBE, &p->p_flag);
315 return;
316 }
317
318 /*
319 * Copy the "hostname" variable into a caller-provided buffer
320 * Returns: 0 for success, ENAMETOOLONG for insufficient buffer space.
321 * On success, "len" will be set to the number of characters preceding
322 * the NULL character in the hostname.
323 */
324 int
325 bsd_hostname(char *buf, int bufsize, int *len)
326 {
327 /*
328 * "hostname" is null-terminated, and "hostnamelen" is equivalent to strlen(hostname).
329 */
330 if (hostnamelen < bufsize) {
331 strlcpy(buf, hostname, bufsize);
332 *len = hostnamelen;
333 return 0;
334 } else {
335 return ENAMETOOLONG;
336 }
337 }
338