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);
49 /* XXX most of these just exist to export; there's no good header for them*/
52 typedef struct devsw_lock
{
53 TAILQ_ENTRY(devsw_lock
) dl_list
;
60 static LCK_GRP_DECLARE(devsw_lock_grp
, "devsw");
61 static LCK_MTX_DECLARE(devsw_lock_list_mtx
, &devsw_lock_grp
);
62 static TAILQ_HEAD(, devsw_lock
) devsw_locks
= TAILQ_HEAD_INITIALIZER(devsw_locks
);
64 /* Just to satisfy pstat command */
65 int dmmin
, dmmax
, dmtext
;
68 * XXX this function only exists to be exported and do nothing.
78 /* Never returns a NULL */
81 thread_t thread
= current_thread();
83 ut
= (struct uthread
*)get_bsdthread_info(thread
);
84 if (ut
&& (ut
->uu_flag
& UT_VFORK
) && ut
->uu_proc
) {
86 if ((p
->p_lflag
& P_LINVFORK
) == 0) {
87 panic("returning child proc not under vfork");
89 if (p
->p_vforkact
!= (void *)thread
) {
90 panic("returning child proc which is not cur_act");
95 p
= (struct proc
*)get_bsdtask_info(current_task());
104 /* Device switch add delete routines */
106 const struct bdevsw nobdev
= NO_BDEVICE
;
107 const struct cdevsw nocdev
= NO_CDEVICE
;
109 * if index is -1, return a free slot if avaliable
110 * else see whether the index is free
111 * return the major number that is free else -1
113 * if index is negative, we start
114 * looking for a free slot at the absolute value of index,
115 * instead of starting at 0
118 bdevsw_isfree(int index
)
120 struct bdevsw
* devsw
;
124 index
= 1; /* start at 1 to avoid collision with volfs (Radar 2842228) */
126 index
= -index
; /* start at least this far up in the table */
128 devsw
= &bdevsw
[index
];
129 for (; index
< nblkdev
; index
++, devsw
++) {
130 if (memcmp((const char *)devsw
, (const char *)&nobdev
, sizeof(struct bdevsw
)) == 0) {
136 if (index
< 0 || index
>= nblkdev
) {
140 devsw
= &bdevsw
[index
];
141 if ((memcmp((const char *)devsw
, (const char *)&nobdev
, sizeof(struct bdevsw
)) != 0)) {
148 * if index is -1, find a free slot to add
149 * else see whether the slot is free
150 * return the major number that is used else -1
152 * if index is negative, we start
153 * looking for a free slot at the absolute value of index,
154 * instead of starting at 0
157 bdevsw_add(int index
, const struct bdevsw
* bsw
)
159 lck_mtx_lock_spin(&devsw_lock_list_mtx
);
160 index
= bdevsw_isfree(index
);
164 bdevsw
[index
] = *bsw
;
166 lck_mtx_unlock(&devsw_lock_list_mtx
);
170 * if the slot has the same bsw, then remove
174 bdevsw_remove(int index
, const struct bdevsw
* bsw
)
176 struct bdevsw
* devsw
;
178 if (index
< 0 || index
>= nblkdev
) {
182 devsw
= &bdevsw
[index
];
183 lck_mtx_lock_spin(&devsw_lock_list_mtx
);
184 if ((memcmp((const char *)devsw
, (const char *)bsw
, sizeof(struct bdevsw
)) != 0)) {
187 bdevsw
[index
] = nobdev
;
189 lck_mtx_unlock(&devsw_lock_list_mtx
);
194 * if index is -1, return a free slot if avaliable
195 * else see whether the index is free
196 * return the major number that is free else -1
198 * if index is negative, we start
199 * looking for a free slot at the absolute value of index,
200 * instead of starting at 0
203 cdevsw_isfree(int index
)
205 struct cdevsw
* devsw
;
211 index
= -index
; /* start at least this far up in the table */
213 devsw
= &cdevsw
[index
];
214 for (; index
< nchrdev
; index
++, devsw
++) {
215 if (memcmp((const char *)devsw
, (const char *)&nocdev
, sizeof(struct cdevsw
)) == 0) {
221 if (index
< 0 || index
>= nchrdev
) {
225 devsw
= &cdevsw
[index
];
226 if ((memcmp((const char *)devsw
, (const 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
, const 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
, const struct cdevsw
* csw
)
266 struct cdevsw
* devsw
;
268 if (index
< 0 || index
>= nchrdev
) {
272 devsw
= &cdevsw
[index
];
273 lck_mtx_lock_spin(&devsw_lock_list_mtx
);
274 if ((memcmp((const char *)devsw
, (const char *)csw
, sizeof(struct cdevsw
)) != 0)) {
277 cdevsw
[index
] = nocdev
;
278 cdevsw_flags
[index
] = 0;
280 lck_mtx_unlock(&devsw_lock_list_mtx
);
285 cdev_set_bdev(int cdev
, int bdev
)
287 return chrtoblk_set(cdev
, bdev
);
291 cdevsw_add_with_bdev(int index
, const struct cdevsw
* csw
, int bdev
)
293 index
= cdevsw_add(index
, csw
);
297 if (cdev_set_bdev(index
, bdev
) < 0) {
298 cdevsw_remove(index
, csw
);
305 cdevsw_setkqueueok(int maj
, const struct cdevsw
* csw
, int extra_flags
)
307 struct cdevsw
* devsw
;
308 uint64_t flags
= CDEVSW_SELECT_KQUEUE
;
310 if (maj
< 0 || maj
>= nchrdev
) {
314 devsw
= &cdevsw
[maj
];
315 if ((memcmp((const char *)devsw
, (const char *)csw
, sizeof(struct cdevsw
)) != 0)) {
319 flags
|= extra_flags
;
321 cdevsw_flags
[maj
] = flags
;
325 #include <pexpert/pexpert.h> /* for PE_parse_boot_arg */
328 * Copy the "hostname" variable into a caller-provided buffer
329 * Returns: 0 for success, ENAMETOOLONG for insufficient buffer space.
330 * On success, "len" will be set to the number of characters preceding
331 * the NULL character in the hostname.
334 bsd_hostname(char *buf
, size_t bufsize
, size_t *len
)
339 * "hostname" is null-terminated
341 lck_mtx_lock(&hostname_lock
);
342 hnlen
= strlen(hostname
);
343 if (hnlen
< bufsize
) {
344 strlcpy(buf
, hostname
, bufsize
);
350 lck_mtx_unlock(&hostname_lock
);
355 devsw_lock_find_locked(dev_t dev
, int mode
)
359 TAILQ_FOREACH(lock
, &devsw_locks
, dl_list
) {
360 if (lock
->dl_dev
== dev
&& lock
->dl_mode
== mode
) {
369 devsw_lock(dev_t dev
, int mode
)
371 devsw_lock_t newlock
, curlock
;
373 assert(0 <= major(dev
) && major(dev
) < nchrdev
);
374 assert(mode
== S_IFCHR
|| mode
== S_IFBLK
);
376 newlock
= kalloc_flags(sizeof(struct devsw_lock
), Z_WAITOK
| Z_ZERO
);
377 newlock
->dl_dev
= dev
;
378 newlock
->dl_thread
= current_thread();
379 newlock
->dl_mode
= mode
;
381 lck_mtx_lock_spin(&devsw_lock_list_mtx
);
383 curlock
= devsw_lock_find_locked(dev
, mode
);
384 if (curlock
== NULL
) {
385 TAILQ_INSERT_TAIL(&devsw_locks
, newlock
, dl_list
);
387 curlock
->dl_waiters
++;
388 lck_mtx_sleep_with_inheritor(&devsw_lock_list_mtx
,
389 LCK_SLEEP_SPIN
, curlock
, curlock
->dl_thread
,
390 THREAD_UNINT
| THREAD_WAIT_NOREPORT
,
391 TIMEOUT_WAIT_FOREVER
);
392 assert(curlock
->dl_thread
== current_thread());
393 curlock
->dl_waiters
--;
396 lck_mtx_unlock(&devsw_lock_list_mtx
);
398 if (curlock
!= NULL
) {
399 kfree(newlock
, sizeof(struct devsw_lock
));
404 devsw_unlock(dev_t dev
, int mode
)
407 thread_t inheritor_thread
= NULL
;
409 assert(0 <= major(dev
) && major(dev
) < nchrdev
);
411 lck_mtx_lock_spin(&devsw_lock_list_mtx
);
413 lock
= devsw_lock_find_locked(dev
, mode
);
415 if (lock
== NULL
|| lock
->dl_thread
!= current_thread()) {
416 panic("current thread doesn't own the lock (%p)", lock
);
419 if (lock
->dl_waiters
) {
420 wakeup_one_with_inheritor(lock
, THREAD_AWAKENED
,
421 LCK_WAKE_DEFAULT
, &lock
->dl_thread
);
422 inheritor_thread
= lock
->dl_thread
;
425 TAILQ_REMOVE(&devsw_locks
, lock
, dl_list
);
428 lck_mtx_unlock(&devsw_lock_list_mtx
);
430 if (inheritor_thread
) {
431 thread_deallocate(inheritor_thread
);
433 kfree(lock
, sizeof(struct devsw_lock
));