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