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 index
= bdevsw_isfree(index
);
167 bdevsw
[index
] = *bsw
;
171 * if the slot has the same bsw, then remove
175 bdevsw_remove(int index
, struct bdevsw
* bsw
)
177 struct bdevsw
* devsw
;
179 if (index
< 0 || index
>= nblkdev
)
182 devsw
= &bdevsw
[index
];
183 if ((memcmp((char *)devsw
, (char *)bsw
, sizeof(struct bdevsw
)) != 0)) {
186 bdevsw
[index
] = nobdev
;
191 * if index is -1, return a free slot if avaliable
192 * else see whether the index is free
193 * return the major number that is free else -1
195 * if index is negative, we start
196 * looking for a free slot at the absolute value of index,
197 * instead of starting at 0
200 cdevsw_isfree(int index
)
202 struct cdevsw
* devsw
;
208 index
= -index
; /* start at least this far up in the table */
209 devsw
= &cdevsw
[index
];
210 for (; index
< nchrdev
; index
++, devsw
++) {
211 if (memcmp((char *)devsw
, (char *)&nocdev
, sizeof(struct cdevsw
)) == 0)
216 if (index
< 0 || index
>= nchrdev
)
219 devsw
= &cdevsw
[index
];
220 if ((memcmp((char *)devsw
, (char *)&nocdev
, sizeof(struct cdevsw
)) != 0)) {
227 * if index is -1, find a free slot to add
228 * else see whether the slot is free
229 * return the major number that is used else -1
231 * if index is negative, we start
232 * looking for a free slot at the absolute value of index,
233 * instead of starting at 0
235 * NOTE: In practice, -1 is unusable, since there are kernel internal
236 * devices that call this function with absolute index values,
237 * which will stomp on free-slot based assignments that happen
238 * before them. -24 is currently a safe starting point.
241 cdevsw_add(int index
, struct cdevsw
* csw
)
243 index
= cdevsw_isfree(index
);
247 cdevsw
[index
] = *csw
;
251 * if the slot has the same csw, then remove
255 cdevsw_remove(int index
, struct cdevsw
* csw
)
257 struct cdevsw
* devsw
;
259 if (index
< 0 || index
>= nchrdev
)
262 devsw
= &cdevsw
[index
];
263 if ((memcmp((char *)devsw
, (char *)csw
, sizeof(struct cdevsw
)) != 0)) {
266 cdevsw
[index
] = nocdev
;
267 cdevsw_flags
[index
] = 0;
272 cdev_set_bdev(int cdev
, int bdev
)
274 return (chrtoblk_set(cdev
, bdev
));
278 cdevsw_add_with_bdev(int index
, struct cdevsw
* csw
, int bdev
)
280 index
= cdevsw_add(index
, csw
);
284 if (cdev_set_bdev(index
, bdev
) < 0) {
285 cdevsw_remove(index
, csw
);
292 cdevsw_setkqueueok(int index
, struct cdevsw
* csw
, int use_offset
)
294 struct cdevsw
* devsw
;
295 uint64_t flags
= CDEVSW_SELECT_KQUEUE
;
297 if (index
< 0 || index
>= nchrdev
)
300 devsw
= &cdevsw
[index
];
301 if ((memcmp((char *)devsw
, (char *)csw
, sizeof(struct cdevsw
)) != 0)) {
306 flags
|= CDEVSW_USE_OFFSET
;
309 cdevsw_flags
[index
] = flags
;
313 #include <pexpert/pexpert.h> /* for PE_parse_boot_arg */
316 * Copy the "hostname" variable into a caller-provided buffer
317 * Returns: 0 for success, ENAMETOOLONG for insufficient buffer space.
318 * On success, "len" will be set to the number of characters preceding
319 * the NULL character in the hostname.
322 bsd_hostname(char * buf
, int bufsize
, int * len
)
325 * "hostname" is null-terminated, and "hostnamelen" is equivalent to strlen(hostname).
327 if (hostnamelen
< bufsize
) {
328 strlcpy(buf
, hostname
, bufsize
);
337 devsw_lock(dev_t dev
, int mode
)
339 devsw_lock_t newlock
, tmplock
;
342 assert(0 <= major(dev
) && major(dev
) < nchrdev
);
343 assert(mode
== S_IFCHR
|| mode
== S_IFBLK
);
345 MALLOC(newlock
, devsw_lock_t
, sizeof(struct devsw_lock
), M_TEMP
, M_WAITOK
| M_ZERO
);
346 newlock
->dl_dev
= dev
;
347 newlock
->dl_thread
= current_thread();
348 newlock
->dl_mode
= mode
;
350 lck_mtx_lock_spin(&devsw_lock_list_mtx
);
352 TAILQ_FOREACH(tmplock
, &devsw_locks
, dl_list
)
354 if (tmplock
->dl_dev
== dev
&& tmplock
->dl_mode
== mode
) {
355 res
= msleep(tmplock
, &devsw_lock_list_mtx
, PVFS
, "devsw_lock", NULL
);
361 TAILQ_INSERT_TAIL(&devsw_locks
, newlock
, dl_list
);
362 lck_mtx_unlock(&devsw_lock_list_mtx
);
365 devsw_unlock(dev_t dev
, int mode
)
367 devsw_lock_t tmplock
;
369 assert(0 <= major(dev
) && major(dev
) < nchrdev
);
371 lck_mtx_lock_spin(&devsw_lock_list_mtx
);
373 TAILQ_FOREACH(tmplock
, &devsw_locks
, dl_list
)
375 if (tmplock
->dl_dev
== dev
&& tmplock
->dl_mode
== mode
) {
380 if (tmplock
== NULL
) {
381 panic("Trying to unlock, and couldn't find lock.");
384 if (tmplock
->dl_thread
!= current_thread()) {
385 panic("Trying to unlock, but I don't hold the lock.");
389 TAILQ_REMOVE(&devsw_locks
, tmplock
, dl_list
);
391 lck_mtx_unlock(&devsw_lock_list_mtx
);
393 FREE(tmplock
, M_TEMP
);
399 devsw_lock_grp
= lck_grp_alloc_init("devsw", NULL
);
400 assert(devsw_lock_grp
!= NULL
);
402 lck_mtx_init(&devsw_lock_list_mtx
, devsw_lock_grp
, NULL
);
403 TAILQ_INIT(&devsw_locks
);