]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/bsd_stubs.c
xnu-1504.9.26.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 */
b0d623f7 39#include <sys/kernel.h>
0b4e3aa0 40#include <sys/user.h>
0c530ab8
A
41#include <sys/sysent.h>
42#include <sys/sysproto.h>
1c79356b 43
2d21ac55
A
44/* XXX these should be in a common header somwhere, but aren't */
45extern int chrtoblk_set(int, int);
b0d623f7 46extern vm_offset_t kmem_mb_alloc(vm_map_t, int, int);
2d21ac55
A
47
48/* XXX most of these just exist to export; there's no good header for them*/
49void pcb_synch(void);
2d21ac55
A
50void tbeproc(void *);
51
52
1c79356b
A
53/* Just to satisfy pstat command */
54int dmmin, dmmax, dmtext;
55
91447636 56vm_offset_t
b0d623f7 57kmem_mb_alloc(vm_map_t mbmap, int size, int physContig)
1c79356b 58{
b0d623f7
A
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);
1c79356b 65 else
b0d623f7
A
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;
1c79356b
A
73}
74
91447636
A
75/*
76 * XXX this function only exists to be exported and do nothing.
77 */
78void
79pcb_synch(void)
80{
81}
1c79356b
A
82
83struct proc *
84current_proc(void)
85{
86 /* Never returns a NULL */
0b4e3aa0
A
87 struct uthread * ut;
88 struct proc *p;
2d21ac55 89 thread_t thread = current_thread();
0b4e3aa0 90
2d21ac55 91 ut = (struct uthread *)get_bsdthread_info(thread);
91447636 92 if (ut && (ut->uu_flag & UT_VFORK) && ut->uu_proc) {
0b4e3aa0 93 p = ut->uu_proc;
2d21ac55 94 if ((p->p_lflag & P_LINVFORK) == 0)
0b4e3aa0 95 panic("returning child proc not under vfork");
2d21ac55 96 if (p->p_vforkact != (void *)thread)
0b4e3aa0
A
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
1c79356b 103 if (p == NULL)
0b4e3aa0
A
104 return (kernproc);
105
1c79356b
A
106 return (p);
107}
108
109/* Device switch add delete routines */
110
1c79356b
A
111struct bdevsw nobdev = NO_BDEVICE;
112struct 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 */
119int
120bdevsw_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 }
55e303ae
A
131 } else {
132 /* NB: Not used below unless index is in range */
133 devsw = &bdevsw[index];
1c79356b
A
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 */
150int
151bdevsw_add(int index, struct bdevsw * bsw)
152{
153 struct bdevsw *devsw;
154
155 if (index == -1) {
55e303ae 156 devsw = &bdevsw[1]; /* Start at slot 1 - this is a hack to fix the index=1 hack */
9bccf70c
A
157 /* yes, start at 1 to avoid collision with volfs (Radar 2842228) */
158 for(index=1; index < nblkdev; index++, devsw++) {
1c79356b
A
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 */
179int
180bdevsw_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 */
200int
201cdevsw_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
2d21ac55
A
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.
1c79356b
A
237 */
238int
239cdevsw_add(int index, struct cdevsw * csw)
240{
241 struct cdevsw *devsw;
242
2d21ac55
A
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++) {
1c79356b
A
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 */
270int
271cdevsw_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
9bccf70c
A
286static int
287cdev_set_bdev(int cdev, int bdev)
288{
9bccf70c
A
289 return (chrtoblk_set(cdev, bdev));
290}
291
292int
293cdevsw_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
2d21ac55
A
306#include <pexpert/pexpert.h> /* for PE_parse_boot_arg */
307
2d21ac55 308void
1c79356b
A
309tbeproc(void *procp)
310{
311 struct proc *p = procp;
312
313 if (p)
b0d623f7 314 OSBitOrAtomic(P_TBE, &p->p_flag);
1c79356b
A
315 return;
316}
317
b0d623f7
A
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 */
324int
325bsd_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