]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
5d5c5d0d A |
2 | * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. |
3 | * | |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
3e170ce0 | 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. | |
3e170ce0 | 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. | |
3e170ce0 | 17 | * |
2d21ac55 A |
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. | |
3e170ce0 | 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> | |
6d2010ae | 34 | #include <sys/stat.h> |
1c79356b A |
35 | #include <vm/vm_map.h> |
36 | #include <sys/systm.h> | |
6d2010ae | 37 | #include <kern/assert.h> |
1c79356b | 38 | #include <sys/conf.h> |
91447636 | 39 | #include <sys/proc_internal.h> |
3e170ce0 | 40 | #include <sys/buf.h> /* for SET */ |
b0d623f7 | 41 | #include <sys/kernel.h> |
0b4e3aa0 | 42 | #include <sys/user.h> |
0c530ab8 A |
43 | #include <sys/sysent.h> |
44 | #include <sys/sysproto.h> | |
1c79356b | 45 | |
2d21ac55 A |
46 | /* XXX these should be in a common header somwhere, but aren't */ |
47 | extern int chrtoblk_set(int, int); | |
b0d623f7 | 48 | extern vm_offset_t kmem_mb_alloc(vm_map_t, int, int); |
2d21ac55 A |
49 | |
50 | /* XXX most of these just exist to export; there's no good header for them*/ | |
3e170ce0 | 51 | void pcb_synch(void); |
2d21ac55 | 52 | |
3e170ce0 | 53 | TAILQ_HEAD(, devsw_lock) devsw_locks; |
6d2010ae | 54 | lck_mtx_t devsw_lock_list_mtx; |
3e170ce0 | 55 | lck_grp_t * devsw_lock_grp; |
2d21ac55 | 56 | |
1c79356b | 57 | /* Just to satisfy pstat command */ |
3e170ce0 | 58 | int dmmin, dmmax, dmtext; |
1c79356b | 59 | |
91447636 | 60 | vm_offset_t |
3e170ce0 | 61 | kmem_mb_alloc(vm_map_t mbmap, int size, int physContig) |
1c79356b | 62 | { |
3e170ce0 | 63 | vm_offset_t addr = 0; |
b0d623f7 A |
64 | kern_return_t kr = KERN_SUCCESS; |
65 | ||
3e170ce0 A |
66 | if (!physContig) |
67 | kr = kernel_memory_allocate(mbmap, &addr, size, 0, KMA_NOPAGEWAIT | KMA_KOBJECT | KMA_LOMEM, VM_KERN_MEMORY_MBUF); | |
1c79356b | 68 | else |
3e170ce0 | 69 | kr = kmem_alloc_contig(mbmap, &addr, size, PAGE_MASK, 0xfffff, 0, KMA_NOPAGEWAIT | KMA_KOBJECT | KMA_LOMEM, VM_KERN_MEMORY_MBUF); |
b0d623f7 | 70 | |
3e170ce0 | 71 | if (kr != KERN_SUCCESS) |
b0d623f7 A |
72 | addr = 0; |
73 | ||
74 | return addr; | |
1c79356b A |
75 | } |
76 | ||
91447636 A |
77 | /* |
78 | * XXX this function only exists to be exported and do nothing. | |
79 | */ | |
80 | void | |
81 | pcb_synch(void) | |
82 | { | |
83 | } | |
1c79356b A |
84 | |
85 | struct proc * | |
86 | current_proc(void) | |
87 | { | |
88 | /* Never returns a NULL */ | |
0b4e3aa0 | 89 | struct uthread * ut; |
3e170ce0 | 90 | struct proc * p; |
2d21ac55 | 91 | thread_t thread = current_thread(); |
0b4e3aa0 | 92 | |
3e170ce0 A |
93 | ut = (struct uthread *)get_bsdthread_info(thread); |
94 | if (ut && (ut->uu_flag & UT_VFORK) && ut->uu_proc) { | |
0b4e3aa0 | 95 | p = ut->uu_proc; |
3e170ce0 | 96 | if ((p->p_lflag & P_LINVFORK) == 0) |
0b4e3aa0 | 97 | panic("returning child proc not under vfork"); |
3e170ce0 | 98 | if (p->p_vforkact != (void *)thread) |
0b4e3aa0 | 99 | panic("returning child proc which is not cur_act"); |
3e170ce0 | 100 | return (p); |
0b4e3aa0 A |
101 | } |
102 | ||
103 | p = (struct proc *)get_bsdtask_info(current_task()); | |
104 | ||
1c79356b | 105 | if (p == NULL) |
0b4e3aa0 A |
106 | return (kernproc); |
107 | ||
1c79356b A |
108 | return (p); |
109 | } | |
110 | ||
111 | /* Device switch add delete routines */ | |
112 | ||
1c79356b A |
113 | struct bdevsw nobdev = NO_BDEVICE; |
114 | struct cdevsw nocdev = NO_CDEVICE; | |
3e170ce0 | 115 | /* |
1c79356b A |
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 | |
119 | * | |
316670eb A |
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 | |
1c79356b A |
123 | */ |
124 | int | |
125 | bdevsw_isfree(int index) | |
126 | { | |
3e170ce0 | 127 | struct bdevsw * devsw; |
316670eb A |
128 | |
129 | if (index < 0) { | |
3e170ce0 A |
130 | if (index == -1) |
131 | index = 1; /* start at 1 to avoid collision with volfs (Radar 2842228) */ | |
132 | else | |
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) | |
137 | break; | |
138 | } | |
1c79356b | 139 | } |
3e170ce0 A |
140 | |
141 | if (index < 0 || index >= nblkdev) | |
142 | return (-1); | |
143 | ||
316670eb | 144 | devsw = &bdevsw[index]; |
3e170ce0 A |
145 | if ((memcmp((char *)devsw, (char *)&nobdev, sizeof(struct bdevsw)) != 0)) { |
146 | return (-1); | |
1c79356b | 147 | } |
3e170ce0 | 148 | return (index); |
1c79356b A |
149 | } |
150 | ||
3e170ce0 | 151 | /* |
1c79356b A |
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 | |
316670eb A |
155 | * |
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 | |
1c79356b A |
159 | */ |
160 | int | |
3e170ce0 | 161 | bdevsw_add(int index, struct bdevsw * bsw) |
1c79356b | 162 | { |
316670eb A |
163 | index = bdevsw_isfree(index); |
164 | if (index < 0) { | |
3e170ce0 | 165 | return (-1); |
1c79356b A |
166 | } |
167 | bdevsw[index] = *bsw; | |
3e170ce0 | 168 | return (index); |
1c79356b | 169 | } |
316670eb | 170 | /* |
1c79356b A |
171 | * if the slot has the same bsw, then remove |
172 | * else -1 | |
173 | */ | |
174 | int | |
3e170ce0 | 175 | bdevsw_remove(int index, struct bdevsw * bsw) |
1c79356b | 176 | { |
3e170ce0 A |
177 | struct bdevsw * devsw; |
178 | ||
179 | if (index < 0 || index >= nblkdev) | |
180 | return (-1); | |
1c79356b A |
181 | |
182 | devsw = &bdevsw[index]; | |
3e170ce0 A |
183 | if ((memcmp((char *)devsw, (char *)bsw, sizeof(struct bdevsw)) != 0)) { |
184 | return (-1); | |
1c79356b A |
185 | } |
186 | bdevsw[index] = nobdev; | |
3e170ce0 | 187 | return (index); |
1c79356b A |
188 | } |
189 | ||
3e170ce0 | 190 | /* |
1c79356b A |
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 | |
316670eb A |
194 | * |
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 | |
1c79356b A |
198 | */ |
199 | int | |
200 | cdevsw_isfree(int index) | |
201 | { | |
3e170ce0 | 202 | struct cdevsw * devsw; |
1c79356b | 203 | |
316670eb | 204 | if (index < 0) { |
3e170ce0 A |
205 | if (index == -1) |
206 | index = 0; | |
207 | else | |
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) | |
212 | break; | |
213 | } | |
1c79356b | 214 | } |
3e170ce0 A |
215 | |
216 | if (index < 0 || index >= nchrdev) | |
217 | return (-1); | |
218 | ||
1c79356b | 219 | devsw = &cdevsw[index]; |
3e170ce0 A |
220 | if ((memcmp((char *)devsw, (char *)&nocdev, sizeof(struct cdevsw)) != 0)) { |
221 | return (-1); | |
1c79356b | 222 | } |
3e170ce0 | 223 | return (index); |
1c79356b A |
224 | } |
225 | ||
3e170ce0 | 226 | /* |
1c79356b A |
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 | |
2d21ac55 | 230 | * |
316670eb A |
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 | |
234 | * | |
2d21ac55 A |
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 | |
316670eb | 238 | * before them. -24 is currently a safe starting point. |
1c79356b A |
239 | */ |
240 | int | |
3e170ce0 | 241 | cdevsw_add(int index, struct cdevsw * csw) |
1c79356b | 242 | { |
316670eb | 243 | index = cdevsw_isfree(index); |
2d21ac55 | 244 | if (index < 0) { |
3e170ce0 | 245 | return (-1); |
1c79356b A |
246 | } |
247 | cdevsw[index] = *csw; | |
3e170ce0 | 248 | return (index); |
1c79356b A |
249 | } |
250 | /* | |
316670eb | 251 | * if the slot has the same csw, then remove |
1c79356b A |
252 | * else -1 |
253 | */ | |
254 | int | |
3e170ce0 | 255 | cdevsw_remove(int index, struct cdevsw * csw) |
1c79356b | 256 | { |
3e170ce0 A |
257 | struct cdevsw * devsw; |
258 | ||
259 | if (index < 0 || index >= nchrdev) | |
260 | return (-1); | |
1c79356b A |
261 | |
262 | devsw = &cdevsw[index]; | |
3e170ce0 A |
263 | if ((memcmp((char *)devsw, (char *)csw, sizeof(struct cdevsw)) != 0)) { |
264 | return (-1); | |
1c79356b A |
265 | } |
266 | cdevsw[index] = nocdev; | |
6d2010ae | 267 | cdevsw_flags[index] = 0; |
3e170ce0 | 268 | return (index); |
1c79356b A |
269 | } |
270 | ||
9bccf70c A |
271 | static int |
272 | cdev_set_bdev(int cdev, int bdev) | |
273 | { | |
9bccf70c A |
274 | return (chrtoblk_set(cdev, bdev)); |
275 | } | |
276 | ||
3e170ce0 | 277 | int |
9bccf70c A |
278 | cdevsw_add_with_bdev(int index, struct cdevsw * csw, int bdev) |
279 | { | |
280 | index = cdevsw_add(index, csw); | |
281 | if (index < 0) { | |
282 | return (index); | |
283 | } | |
284 | if (cdev_set_bdev(index, bdev) < 0) { | |
285 | cdevsw_remove(index, csw); | |
286 | return (-1); | |
287 | } | |
288 | return (index); | |
289 | } | |
290 | ||
6d2010ae | 291 | int |
3e170ce0 | 292 | cdevsw_setkqueueok(int index, struct cdevsw * csw, int use_offset) |
6d2010ae | 293 | { |
3e170ce0 | 294 | struct cdevsw * devsw; |
6d2010ae A |
295 | uint64_t flags = CDEVSW_SELECT_KQUEUE; |
296 | ||
3e170ce0 A |
297 | if (index < 0 || index >= nchrdev) |
298 | return (-1); | |
299 | ||
6d2010ae | 300 | devsw = &cdevsw[index]; |
3e170ce0 A |
301 | if ((memcmp((char *)devsw, (char *)csw, sizeof(struct cdevsw)) != 0)) { |
302 | return (-1); | |
6d2010ae A |
303 | } |
304 | ||
305 | if (use_offset) { | |
306 | flags |= CDEVSW_USE_OFFSET; | |
307 | } | |
308 | ||
309 | cdevsw_flags[index] = flags; | |
310 | return 0; | |
311 | } | |
312 | ||
3e170ce0 | 313 | #include <pexpert/pexpert.h> /* for PE_parse_boot_arg */ |
2d21ac55 | 314 | |
b0d623f7 A |
315 | /* |
316 | * Copy the "hostname" variable into a caller-provided buffer | |
317 | * Returns: 0 for success, ENAMETOOLONG for insufficient buffer space. | |
3e170ce0 | 318 | * On success, "len" will be set to the number of characters preceding |
b0d623f7 A |
319 | * the NULL character in the hostname. |
320 | */ | |
321 | int | |
3e170ce0 | 322 | bsd_hostname(char * buf, int bufsize, int * len) |
b0d623f7 A |
323 | { |
324 | /* | |
3e170ce0 | 325 | * "hostname" is null-terminated, and "hostnamelen" is equivalent to strlen(hostname). |
b0d623f7 A |
326 | */ |
327 | if (hostnamelen < bufsize) { | |
328 | strlcpy(buf, hostname, bufsize); | |
329 | *len = hostnamelen; | |
330 | return 0; | |
331 | } else { | |
332 | return ENAMETOOLONG; | |
3e170ce0 | 333 | } |
b0d623f7 A |
334 | } |
335 | ||
6d2010ae A |
336 | void |
337 | devsw_lock(dev_t dev, int mode) | |
338 | { | |
339 | devsw_lock_t newlock, tmplock; | |
340 | int res; | |
341 | ||
3e170ce0 | 342 | assert(0 <= major(dev) && major(dev) < nchrdev); |
6d2010ae A |
343 | assert(mode == S_IFCHR || mode == S_IFBLK); |
344 | ||
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; | |
3e170ce0 | 349 | |
6d2010ae A |
350 | lck_mtx_lock_spin(&devsw_lock_list_mtx); |
351 | retry: | |
3e170ce0 A |
352 | TAILQ_FOREACH(tmplock, &devsw_locks, dl_list) |
353 | { | |
6d2010ae | 354 | if (tmplock->dl_dev == dev && tmplock->dl_mode == mode) { |
3e170ce0 | 355 | res = msleep(tmplock, &devsw_lock_list_mtx, PVFS, "devsw_lock", NULL); |
6d2010ae A |
356 | assert(res == 0); |
357 | goto retry; | |
358 | } | |
359 | } | |
360 | ||
361 | TAILQ_INSERT_TAIL(&devsw_locks, newlock, dl_list); | |
362 | lck_mtx_unlock(&devsw_lock_list_mtx); | |
6d2010ae A |
363 | } |
364 | void | |
365 | devsw_unlock(dev_t dev, int mode) | |
366 | { | |
367 | devsw_lock_t tmplock; | |
368 | ||
3e170ce0 | 369 | assert(0 <= major(dev) && major(dev) < nchrdev); |
6d2010ae A |
370 | |
371 | lck_mtx_lock_spin(&devsw_lock_list_mtx); | |
372 | ||
3e170ce0 A |
373 | TAILQ_FOREACH(tmplock, &devsw_locks, dl_list) |
374 | { | |
375 | if (tmplock->dl_dev == dev && tmplock->dl_mode == mode) { | |
6d2010ae A |
376 | break; |
377 | } | |
378 | } | |
379 | ||
380 | if (tmplock == NULL) { | |
381 | panic("Trying to unlock, and couldn't find lock."); | |
382 | } | |
383 | ||
384 | if (tmplock->dl_thread != current_thread()) { | |
385 | panic("Trying to unlock, but I don't hold the lock."); | |
386 | } | |
387 | ||
388 | wakeup(tmplock); | |
389 | TAILQ_REMOVE(&devsw_locks, tmplock, dl_list); | |
3e170ce0 | 390 | |
6d2010ae | 391 | lck_mtx_unlock(&devsw_lock_list_mtx); |
3e170ce0 | 392 | |
6d2010ae A |
393 | FREE(tmplock, M_TEMP); |
394 | } | |
395 | ||
396 | void | |
397 | devsw_init() | |
398 | { | |
399 | devsw_lock_grp = lck_grp_alloc_init("devsw", NULL); | |
400 | assert(devsw_lock_grp != NULL); | |
401 | ||
402 | lck_mtx_init(&devsw_lock_list_mtx, devsw_lock_grp, NULL); | |
403 | TAILQ_INIT(&devsw_locks); | |
404 | } |