2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
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>
35 #include <vm/vm_map.h>
36 #include <sys/systm.h>
37 #include <kern/assert.h>
39 #include <sys/proc_internal.h>
40 #include <sys/buf.h> /* for SET */
41 #include <sys/kernel.h>
43 #include <sys/sysent.h>
44 #include <sys/sysproto.h>
46 /* XXX these should be in a common header somwhere, but aren't */
47 extern int chrtoblk_set(int, int);
48 extern vm_offset_t
kmem_mb_alloc(vm_map_t
, int, int);
50 /* XXX most of these just exist to export; there's no good header for them*/
53 TAILQ_HEAD(, devsw_lock
) devsw_locks
;
54 lck_mtx_t devsw_lock_list_mtx
;
55 lck_grp_t
* devsw_lock_grp
;
57 /* Just to satisfy pstat command */
58 int dmmin
, dmmax
, dmtext
;
61 kmem_mb_alloc(vm_map_t mbmap
, int size
, int physContig
)
64 kern_return_t kr
= KERN_SUCCESS
;
67 kr
= kernel_memory_allocate(mbmap
, &addr
, size
, 0, KMA_NOPAGEWAIT
| KMA_KOBJECT
| KMA_LOMEM
, VM_KERN_MEMORY_MBUF
);
69 kr
= kmem_alloc_contig(mbmap
, &addr
, size
, PAGE_MASK
, 0xfffff, 0, KMA_NOPAGEWAIT
| KMA_KOBJECT
| KMA_LOMEM
, VM_KERN_MEMORY_MBUF
);
71 if (kr
!= KERN_SUCCESS
)
78 * XXX this function only exists to be exported and do nothing.
88 /* Never returns a NULL */
91 thread_t thread
= current_thread();
93 ut
= (struct uthread
*)get_bsdthread_info(thread
);
94 if (ut
&& (ut
->uu_flag
& UT_VFORK
) && ut
->uu_proc
) {
96 if ((p
->p_lflag
& P_LINVFORK
) == 0)
97 panic("returning child proc not under vfork");
98 if (p
->p_vforkact
!= (void *)thread
)
99 panic("returning child proc which is not cur_act");
103 p
= (struct proc
*)get_bsdtask_info(current_task());
111 /* Device switch add delete routines */
113 struct bdevsw nobdev
= NO_BDEVICE
;
114 struct cdevsw nocdev
= NO_CDEVICE
;
116 * if index is -1, return a free slot if avaliable
117 * else see whether the index is free
118 * return the major number that is free else -1
120 * if index is negative, we start
121 * looking for a free slot at the absolute value of index,
122 * instead of starting at 0
125 bdevsw_isfree(int index
)
127 struct bdevsw
* devsw
;
131 index
= 1; /* start at 1 to avoid collision with volfs (Radar 2842228) */
133 index
= -index
; /* start at least this far up in the table */
134 devsw
= &bdevsw
[index
];
135 for (; index
< nblkdev
; index
++, devsw
++) {
136 if (memcmp((char *)devsw
, (char *)&nobdev
, sizeof(struct bdevsw
)) == 0)
141 if (index
< 0 || index
>= nblkdev
)
144 devsw
= &bdevsw
[index
];
145 if ((memcmp((char *)devsw
, (char *)&nobdev
, sizeof(struct bdevsw
)) != 0)) {
152 * if index is -1, find a free slot to add
153 * else see whether the slot is free
154 * return the major number that is used else -1
156 * if index is negative, we start
157 * looking for a free slot at the absolute value of index,
158 * instead of starting at 0
161 bdevsw_add(int index
, struct bdevsw
* bsw
)
163 lck_mtx_lock_spin(&devsw_lock_list_mtx
);
164 index
= bdevsw_isfree(index
);
168 bdevsw
[index
] = *bsw
;
170 lck_mtx_unlock(&devsw_lock_list_mtx
);
174 * if the slot has the same bsw, then remove
178 bdevsw_remove(int index
, struct bdevsw
* bsw
)
180 struct bdevsw
* devsw
;
182 if (index
< 0 || index
>= nblkdev
)
185 devsw
= &bdevsw
[index
];
186 lck_mtx_lock_spin(&devsw_lock_list_mtx
);
187 if ((memcmp((char *)devsw
, (char *)bsw
, sizeof(struct bdevsw
)) != 0)) {
190 bdevsw
[index
] = nobdev
;
192 lck_mtx_unlock(&devsw_lock_list_mtx
);
197 * if index is -1, return a free slot if avaliable
198 * else see whether the index is free
199 * return the major number that is free else -1
201 * if index is negative, we start
202 * looking for a free slot at the absolute value of index,
203 * instead of starting at 0
206 cdevsw_isfree(int index
)
208 struct cdevsw
* devsw
;
214 index
= -index
; /* start at least this far up in the table */
215 devsw
= &cdevsw
[index
];
216 for (; index
< nchrdev
; index
++, devsw
++) {
217 if (memcmp((char *)devsw
, (char *)&nocdev
, sizeof(struct cdevsw
)) == 0)
222 if (index
< 0 || index
>= nchrdev
)
225 devsw
= &cdevsw
[index
];
226 if ((memcmp((char *)devsw
, (char *)&nocdev
, sizeof(struct cdevsw
)) != 0)) {
233 * if index is -1, find a free slot to add
234 * else see whether the slot is free
235 * return the major number that is used else -1
237 * if index is negative, we start
238 * looking for a free slot at the absolute value of index,
239 * instead of starting at 0
241 * NOTE: In practice, -1 is unusable, since there are kernel internal
242 * devices that call this function with absolute index values,
243 * which will stomp on free-slot based assignments that happen
244 * before them. -24 is currently a safe starting point.
247 cdevsw_add(int index
, struct cdevsw
* csw
)
249 lck_mtx_lock_spin(&devsw_lock_list_mtx
);
250 index
= cdevsw_isfree(index
);
254 cdevsw
[index
] = *csw
;
256 lck_mtx_unlock(&devsw_lock_list_mtx
);
260 * if the slot has the same csw, then remove
264 cdevsw_remove(int index
, struct cdevsw
* csw
)
266 struct cdevsw
* devsw
;
268 if (index
< 0 || index
>= nchrdev
)
271 devsw
= &cdevsw
[index
];
272 lck_mtx_lock_spin(&devsw_lock_list_mtx
);
273 if ((memcmp((char *)devsw
, (char *)csw
, sizeof(struct cdevsw
)) != 0)) {
276 cdevsw
[index
] = nocdev
;
277 cdevsw_flags
[index
] = 0;
279 lck_mtx_unlock(&devsw_lock_list_mtx
);
284 cdev_set_bdev(int cdev
, int bdev
)
286 return (chrtoblk_set(cdev
, bdev
));
290 cdevsw_add_with_bdev(int index
, struct cdevsw
* csw
, int bdev
)
292 index
= cdevsw_add(index
, csw
);
296 if (cdev_set_bdev(index
, bdev
) < 0) {
297 cdevsw_remove(index
, csw
);
304 cdevsw_setkqueueok(int maj
, struct cdevsw
* csw
, int extra_flags
)
306 struct cdevsw
* devsw
;
307 uint64_t flags
= CDEVSW_SELECT_KQUEUE
;
309 if (maj
< 0 || maj
>= nchrdev
) {
313 devsw
= &cdevsw
[maj
];
314 if ((memcmp((char *)devsw
, (char *)csw
, sizeof(struct cdevsw
)) != 0)) {
318 flags
|= extra_flags
;
320 cdevsw_flags
[maj
] = flags
;
324 #include <pexpert/pexpert.h> /* for PE_parse_boot_arg */
327 * Copy the "hostname" variable into a caller-provided buffer
328 * Returns: 0 for success, ENAMETOOLONG for insufficient buffer space.
329 * On success, "len" will be set to the number of characters preceding
330 * the NULL character in the hostname.
333 bsd_hostname(char * buf
, int bufsize
, int * len
)
336 * "hostname" is null-terminated, and "hostnamelen" is equivalent to strlen(hostname).
338 if (hostnamelen
< bufsize
) {
339 strlcpy(buf
, hostname
, bufsize
);
348 devsw_lock(dev_t dev
, int mode
)
350 devsw_lock_t newlock
, tmplock
;
353 assert(0 <= major(dev
) && major(dev
) < nchrdev
);
354 assert(mode
== S_IFCHR
|| mode
== S_IFBLK
);
356 MALLOC(newlock
, devsw_lock_t
, sizeof(struct devsw_lock
), M_TEMP
, M_WAITOK
| M_ZERO
);
357 newlock
->dl_dev
= dev
;
358 newlock
->dl_thread
= current_thread();
359 newlock
->dl_mode
= mode
;
361 lck_mtx_lock_spin(&devsw_lock_list_mtx
);
363 TAILQ_FOREACH(tmplock
, &devsw_locks
, dl_list
)
365 if (tmplock
->dl_dev
== dev
&& tmplock
->dl_mode
== mode
) {
366 res
= msleep(tmplock
, &devsw_lock_list_mtx
, PVFS
, "devsw_lock", NULL
);
372 TAILQ_INSERT_TAIL(&devsw_locks
, newlock
, dl_list
);
373 lck_mtx_unlock(&devsw_lock_list_mtx
);
376 devsw_unlock(dev_t dev
, int mode
)
378 devsw_lock_t tmplock
;
380 assert(0 <= major(dev
) && major(dev
) < nchrdev
);
382 lck_mtx_lock_spin(&devsw_lock_list_mtx
);
384 TAILQ_FOREACH(tmplock
, &devsw_locks
, dl_list
)
386 if (tmplock
->dl_dev
== dev
&& tmplock
->dl_mode
== mode
) {
391 if (tmplock
== NULL
) {
392 panic("Trying to unlock, and couldn't find lock.");
395 if (tmplock
->dl_thread
!= current_thread()) {
396 panic("Trying to unlock, but I don't hold the lock.");
400 TAILQ_REMOVE(&devsw_locks
, tmplock
, dl_list
);
402 lck_mtx_unlock(&devsw_lock_list_mtx
);
404 FREE(tmplock
, M_TEMP
);
410 devsw_lock_grp
= lck_grp_alloc_init("devsw", NULL
);
411 assert(devsw_lock_grp
!= NULL
);
413 lck_mtx_init(&devsw_lock_list_mtx
, devsw_lock_grp
, NULL
);
414 TAILQ_INIT(&devsw_locks
);