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*/
54 TAILQ_HEAD(,devsw_lock
) devsw_locks
;
55 lck_mtx_t devsw_lock_list_mtx
;
56 lck_grp_t
*devsw_lock_grp
;
58 /* Just to satisfy pstat command */
59 int dmmin
, dmmax
, dmtext
;
62 kmem_mb_alloc(vm_map_t mbmap
, int size
, int physContig
)
65 kern_return_t kr
= KERN_SUCCESS
;
68 kr
= kernel_memory_allocate(mbmap
, &addr
, size
,
69 0, KMA_NOPAGEWAIT
|KMA_KOBJECT
|KMA_LOMEM
);
71 kr
= kmem_alloc_contig(mbmap
, &addr
, size
, PAGE_MASK
,
72 0xfffff, 0, KMA_NOPAGEWAIT
| KMA_KOBJECT
| KMA_LOMEM
);
74 if( kr
!= KERN_SUCCESS
)
81 * XXX this function only exists to be exported and do nothing.
91 /* Never returns a NULL */
94 thread_t thread
= current_thread();
96 ut
= (struct uthread
*)get_bsdthread_info(thread
);
97 if (ut
&& (ut
->uu_flag
& UT_VFORK
) && ut
->uu_proc
) {
99 if ((p
->p_lflag
& P_LINVFORK
) == 0)
100 panic("returning child proc not under vfork");
101 if (p
->p_vforkact
!= (void *)thread
)
102 panic("returning child proc which is not cur_act");
106 p
= (struct proc
*)get_bsdtask_info(current_task());
114 /* Device switch add delete routines */
116 struct bdevsw nobdev
= NO_BDEVICE
;
117 struct cdevsw nocdev
= NO_CDEVICE
;
119 * if index is -1, return a free slot if avaliable
120 * else see whether the index is free
121 * return the major number that is free else -1
123 * if index is negative, we start
124 * looking for a free slot at the absolute value of index,
125 * instead of starting at 0
128 bdevsw_isfree(int index
)
130 struct bdevsw
*devsw
;
134 index
= 1; /* start at 1 to avoid collision with volfs (Radar 2842228) */
136 index
= -index
; /* start at least this far up in the table */
137 devsw
= &bdevsw
[index
];
138 for(; index
< nblkdev
; index
++, devsw
++) {
139 if(memcmp((char *)devsw
,
141 sizeof(struct bdevsw
)) == 0)
145 devsw
= &bdevsw
[index
];
146 if ((index
< 0) || (index
>= nblkdev
) ||
147 (memcmp((char *)devsw
,
149 sizeof(struct bdevsw
)) != 0)) {
156 * if index is -1, find a free slot to add
157 * else see whether the slot is free
158 * return the major number that is used else -1
160 * if index is negative, we start
161 * looking for a free slot at the absolute value of index,
162 * instead of starting at 0
165 bdevsw_add(int index
, struct bdevsw
* bsw
)
167 index
= bdevsw_isfree(index
);
171 bdevsw
[index
] = *bsw
;
175 * if the slot has the same bsw, then remove
179 bdevsw_remove(int index
, struct bdevsw
* bsw
)
181 struct bdevsw
*devsw
;
183 devsw
= &bdevsw
[index
];
184 if ((index
< 0) || (index
>= nblkdev
) ||
185 (memcmp((char *)devsw
,
187 sizeof(struct bdevsw
)) != 0)) {
190 bdevsw
[index
] = nobdev
;
195 * if index is -1, return a free slot if avaliable
196 * else see whether the index is free
197 * return the major number that is free else -1
199 * if index is negative, we start
200 * looking for a free slot at the absolute value of index,
201 * instead of starting at 0
204 cdevsw_isfree(int index
)
206 struct cdevsw
*devsw
;
212 index
= -index
; /* start at least this far up in the table */
213 devsw
= &cdevsw
[index
];
214 for(; index
< nchrdev
; index
++, devsw
++) {
215 if(memcmp((char *)devsw
,
217 sizeof(struct cdevsw
)) == 0)
221 devsw
= &cdevsw
[index
];
222 if ((index
< 0) || (index
>= nchrdev
) ||
223 (memcmp((char *)devsw
,
225 sizeof(struct cdevsw
)) != 0)) {
232 * if index is -1, find a free slot to add
233 * else see whether the slot is free
234 * return the major number that is used else -1
236 * if index is negative, we start
237 * looking for a free slot at the absolute value of index,
238 * instead of starting at 0
240 * NOTE: In practice, -1 is unusable, since there are kernel internal
241 * devices that call this function with absolute index values,
242 * which will stomp on free-slot based assignments that happen
243 * before them. -24 is currently a safe starting point.
246 cdevsw_add(int index
, struct cdevsw
* csw
)
248 index
= cdevsw_isfree(index
);
252 cdevsw
[index
] = *csw
;
256 * if the slot has the same csw, then remove
260 cdevsw_remove(int index
, struct cdevsw
* csw
)
262 struct cdevsw
*devsw
;
264 devsw
= &cdevsw
[index
];
265 if ((index
< 0) || (index
>= nchrdev
) ||
266 (memcmp((char *)devsw
,
268 sizeof(struct cdevsw
)) != 0)) {
271 cdevsw
[index
] = nocdev
;
272 cdevsw_flags
[index
] = 0;
277 cdev_set_bdev(int cdev
, int bdev
)
279 return (chrtoblk_set(cdev
, bdev
));
283 cdevsw_add_with_bdev(int index
, struct cdevsw
* csw
, int bdev
)
285 index
= cdevsw_add(index
, csw
);
289 if (cdev_set_bdev(index
, bdev
) < 0) {
290 cdevsw_remove(index
, csw
);
297 cdevsw_setkqueueok(int index
, struct cdevsw
*csw
, int use_offset
)
299 struct cdevsw
*devsw
;
300 uint64_t flags
= CDEVSW_SELECT_KQUEUE
;
302 devsw
= &cdevsw
[index
];
303 if ((index
< 0) || (index
>= nchrdev
) ||
304 (memcmp((char *)devsw
,
306 sizeof(struct cdevsw
)) != 0)) {
311 flags
|= CDEVSW_USE_OFFSET
;
314 cdevsw_flags
[index
] = flags
;
318 #include <pexpert/pexpert.h> /* for PE_parse_boot_arg */
323 struct proc
*p
= procp
;
326 OSBitOrAtomic(P_TBE
, &p
->p_flag
);
331 * Copy the "hostname" variable into a caller-provided buffer
332 * Returns: 0 for success, ENAMETOOLONG for insufficient buffer space.
333 * On success, "len" will be set to the number of characters preceding
334 * the NULL character in the hostname.
337 bsd_hostname(char *buf
, int bufsize
, int *len
)
340 * "hostname" is null-terminated, and "hostnamelen" is equivalent to strlen(hostname).
342 if (hostnamelen
< bufsize
) {
343 strlcpy(buf
, hostname
, bufsize
);
352 devsw_lock(dev_t dev
, int mode
)
354 devsw_lock_t newlock
, tmplock
;
357 assert(0 <= major(dev
) && major(dev
) < nchrdev
);
358 assert(mode
== S_IFCHR
|| mode
== S_IFBLK
);
360 MALLOC(newlock
, devsw_lock_t
, sizeof(struct devsw_lock
), M_TEMP
, M_WAITOK
| M_ZERO
);
361 newlock
->dl_dev
= dev
;
362 newlock
->dl_thread
= current_thread();
363 newlock
->dl_mode
= mode
;
365 lck_mtx_lock_spin(&devsw_lock_list_mtx
);
367 TAILQ_FOREACH(tmplock
, &devsw_locks
, dl_list
) {
368 if (tmplock
->dl_dev
== dev
&& tmplock
->dl_mode
== mode
) {
369 res
= msleep(tmplock
, &devsw_lock_list_mtx
, PVFS
, "devsw_lock", NULL
);
375 TAILQ_INSERT_TAIL(&devsw_locks
, newlock
, dl_list
);
376 lck_mtx_unlock(&devsw_lock_list_mtx
);
380 devsw_unlock(dev_t dev
, int mode
)
382 devsw_lock_t tmplock
;
384 assert(0 <= major(dev
) && major(dev
) < nchrdev
);
386 lck_mtx_lock_spin(&devsw_lock_list_mtx
);
388 TAILQ_FOREACH(tmplock
, &devsw_locks
, dl_list
) {
389 if (tmplock
->dl_dev
== dev
&& tmplock
->dl_mode
== mode
) {
394 if (tmplock
== NULL
) {
395 panic("Trying to unlock, and couldn't find lock.");
398 if (tmplock
->dl_thread
!= current_thread()) {
399 panic("Trying to unlock, but I don't hold the lock.");
403 TAILQ_REMOVE(&devsw_locks
, tmplock
, dl_list
);
405 lck_mtx_unlock(&devsw_lock_list_mtx
);
407 FREE(tmplock
, M_TEMP
);
413 devsw_lock_grp
= lck_grp_alloc_init("devsw", NULL
);
414 assert(devsw_lock_grp
!= NULL
);
416 lck_mtx_init(&devsw_lock_list_mtx
, devsw_lock_grp
, NULL
);
417 TAILQ_INIT(&devsw_locks
);