1 /*- 2 * Copyright (c) 2010 Isilon Systems, Inc. 3 * Copyright (c) 2010 iX Systems, Inc. 4 * Copyright (c) 2010 Panasas, Inc. 5 * Copyright (c) 2013-2017 Mellanox Technologies, Ltd. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice unmodified, this list of conditions, and the following 13 * disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 #include <sys/param.h> 34 #include <sys/systm.h> 35 #include <sys/malloc.h> 36 #include <sys/kernel.h> 37 #include <sys/sysctl.h> 38 #include <sys/proc.h> 39 #include <sys/sglist.h> 40 #include <sys/sleepqueue.h> 41 #include <sys/lock.h> 42 #include <sys/mutex.h> 43 #include <sys/bus.h> 44 #include <sys/fcntl.h> 45 #include <sys/file.h> 46 #include <sys/filio.h> 47 #include <sys/rwlock.h> 48 49 #include <vm/vm.h> 50 #include <vm/pmap.h> 51 #include <vm/vm_object.h> 52 #include <vm/vm_page.h> 53 #include <vm/vm_pager.h> 54 55 #include <machine/stdarg.h> 56 57 #if defined(__i386__) || defined(__amd64__) 58 #include <machine/md_var.h> 59 #endif 60 61 #include <linux/kobject.h> 62 #include <linux/device.h> 63 #include <linux/slab.h> 64 #include <linux/module.h> 65 #include <linux/moduleparam.h> 66 #include <linux/cdev.h> 67 #include <linux/file.h> 68 #include <linux/sysfs.h> 69 #include <linux/mm.h> 70 #include <linux/io.h> 71 #include <linux/vmalloc.h> 72 #include <linux/netdevice.h> 73 #include <linux/timer.h> 74 #include <linux/interrupt.h> 75 #include <linux/uaccess.h> 76 #include <linux/list.h> 77 #include <linux/kthread.h> 78 #include <linux/kernel.h> 79 #include <linux/compat.h> 80 #include <linux/poll.h> 81 #include <linux/smp.h> 82 83 #if defined(__i386__) || defined(__amd64__) 84 #include <asm/smp.h> 85 #endif 86 87 SYSCTL_NODE(_compat, OID_AUTO, linuxkpi, CTLFLAG_RW, 0, "LinuxKPI parameters"); 88 89 MALLOC_DEFINE(M_KMALLOC, "linux", "Linux kmalloc compat"); 90 91 #include <linux/rbtree.h> 92 /* Undo Linux compat changes. */ 93 #undef RB_ROOT 94 #undef file 95 #undef cdev 96 #define RB_ROOT(head) (head)->rbh_root 97 98 static struct vm_area_struct *linux_cdev_handle_find(void *handle); 99 100 struct kobject linux_class_root; 101 struct device linux_root_device; 102 struct class linux_class_misc; 103 struct list_head pci_drivers; 104 struct list_head pci_devices; 105 spinlock_t pci_lock; 106 107 unsigned long linux_timer_hz_mask; 108 109 int 110 panic_cmp(struct rb_node *one, struct rb_node *two) 111 { 112 panic("no cmp"); 113 } 114 115 RB_GENERATE(linux_root, rb_node, __entry, panic_cmp); 116 117 int 118 kobject_set_name_vargs(struct kobject *kobj, const char *fmt, va_list args) 119 { 120 va_list tmp_va; 121 int len; 122 char *old; 123 char *name; 124 char dummy; 125 126 old = kobj->name; 127 128 if (old && fmt == NULL) 129 return (0); 130 131 /* compute length of string */ 132 va_copy(tmp_va, args); 133 len = vsnprintf(&dummy, 0, fmt, tmp_va); 134 va_end(tmp_va); 135 136 /* account for zero termination */ 137 len++; 138 139 /* check for error */ 140 if (len < 1) 141 return (-EINVAL); 142 143 /* allocate memory for string */ 144 name = kzalloc(len, GFP_KERNEL); 145 if (name == NULL) 146 return (-ENOMEM); 147 vsnprintf(name, len, fmt, args); 148 kobj->name = name; 149 150 /* free old string */ 151 kfree(old); 152 153 /* filter new string */ 154 for (; *name != '\0'; name++) 155 if (*name == '/') 156 *name = '!'; 157 return (0); 158 } 159 160 int 161 kobject_set_name(struct kobject *kobj, const char *fmt, ...) 162 { 163 va_list args; 164 int error; 165 166 va_start(args, fmt); 167 error = kobject_set_name_vargs(kobj, fmt, args); 168 va_end(args); 169 170 return (error); 171 } 172 173 static int 174 kobject_add_complete(struct kobject *kobj, struct kobject *parent) 175 { 176 const struct kobj_type *t; 177 int error; 178 179 kobj->parent = parent; 180 error = sysfs_create_dir(kobj); 181 if (error == 0 && kobj->ktype && kobj->ktype->default_attrs) { 182 struct attribute **attr; 183 t = kobj->ktype; 184 185 for (attr = t->default_attrs; *attr != NULL; attr++) { 186 error = sysfs_create_file(kobj, *attr); 187 if (error) 188 break; 189 } 190 if (error) 191 sysfs_remove_dir(kobj); 192 193 } 194 return (error); 195 } 196 197 int 198 kobject_add(struct kobject *kobj, struct kobject *parent, const char *fmt, ...) 199 { 200 va_list args; 201 int error; 202 203 va_start(args, fmt); 204 error = kobject_set_name_vargs(kobj, fmt, args); 205 va_end(args); 206 if (error) 207 return (error); 208 209 return kobject_add_complete(kobj, parent); 210 } 211 212 void 213 linux_kobject_release(struct kref *kref) 214 { 215 struct kobject *kobj; 216 char *name; 217 218 kobj = container_of(kref, struct kobject, kref); 219 sysfs_remove_dir(kobj); 220 name = kobj->name; 221 if (kobj->ktype && kobj->ktype->release) 222 kobj->ktype->release(kobj); 223 kfree(name); 224 } 225 226 static void 227 linux_kobject_kfree(struct kobject *kobj) 228 { 229 kfree(kobj); 230 } 231 232 static void 233 linux_kobject_kfree_name(struct kobject *kobj) 234 { 235 if (kobj) { 236 kfree(kobj->name); 237 } 238 } 239 240 const struct kobj_type linux_kfree_type = { 241 .release = linux_kobject_kfree 242 }; 243 244 static void 245 linux_device_release(struct device *dev) 246 { 247 pr_debug("linux_device_release: %s\n", dev_name(dev)); 248 kfree(dev); 249 } 250 251 static ssize_t 252 linux_class_show(struct kobject *kobj, struct attribute *attr, char *buf) 253 { 254 struct class_attribute *dattr; 255 ssize_t error; 256 257 dattr = container_of(attr, struct class_attribute, attr); 258 error = -EIO; 259 if (dattr->show) 260 error = dattr->show(container_of(kobj, struct class, kobj), 261 dattr, buf); 262 return (error); 263 } 264 265 static ssize_t 266 linux_class_store(struct kobject *kobj, struct attribute *attr, const char *buf, 267 size_t count) 268 { 269 struct class_attribute *dattr; 270 ssize_t error; 271 272 dattr = container_of(attr, struct class_attribute, attr); 273 error = -EIO; 274 if (dattr->store) 275 error = dattr->store(container_of(kobj, struct class, kobj), 276 dattr, buf, count); 277 return (error); 278 } 279 280 static void 281 linux_class_release(struct kobject *kobj) 282 { 283 struct class *class; 284 285 class = container_of(kobj, struct class, kobj); 286 if (class->class_release) 287 class->class_release(class); 288 } 289 290 static const struct sysfs_ops linux_class_sysfs = { 291 .show = linux_class_show, 292 .store = linux_class_store, 293 }; 294 295 const struct kobj_type linux_class_ktype = { 296 .release = linux_class_release, 297 .sysfs_ops = &linux_class_sysfs 298 }; 299 300 static void 301 linux_dev_release(struct kobject *kobj) 302 { 303 struct device *dev; 304 305 dev = container_of(kobj, struct device, kobj); 306 /* This is the precedence defined by linux. */ 307 if (dev->release) 308 dev->release(dev); 309 else if (dev->class && dev->class->dev_release) 310 dev->class->dev_release(dev); 311 } 312 313 static ssize_t 314 linux_dev_show(struct kobject *kobj, struct attribute *attr, char *buf) 315 { 316 struct device_attribute *dattr; 317 ssize_t error; 318 319 dattr = container_of(attr, struct device_attribute, attr); 320 error = -EIO; 321 if (dattr->show) 322 error = dattr->show(container_of(kobj, struct device, kobj), 323 dattr, buf); 324 return (error); 325 } 326 327 static ssize_t 328 linux_dev_store(struct kobject *kobj, struct attribute *attr, const char *buf, 329 size_t count) 330 { 331 struct device_attribute *dattr; 332 ssize_t error; 333 334 dattr = container_of(attr, struct device_attribute, attr); 335 error = -EIO; 336 if (dattr->store) 337 error = dattr->store(container_of(kobj, struct device, kobj), 338 dattr, buf, count); 339 return (error); 340 } 341 342 static const struct sysfs_ops linux_dev_sysfs = { 343 .show = linux_dev_show, 344 .store = linux_dev_store, 345 }; 346 347 const struct kobj_type linux_dev_ktype = { 348 .release = linux_dev_release, 349 .sysfs_ops = &linux_dev_sysfs 350 }; 351 352 struct device * 353 device_create(struct class *class, struct device *parent, dev_t devt, 354 void *drvdata, const char *fmt, ...) 355 { 356 struct device *dev; 357 va_list args; 358 359 dev = kzalloc(sizeof(*dev), M_WAITOK); 360 dev->parent = parent; 361 dev->class = class; 362 dev->devt = devt; 363 dev->driver_data = drvdata; 364 dev->release = linux_device_release; 365 va_start(args, fmt); 366 kobject_set_name_vargs(&dev->kobj, fmt, args); 367 va_end(args); 368 device_register(dev); 369 370 return (dev); 371 } 372 373 int 374 kobject_init_and_add(struct kobject *kobj, const struct kobj_type *ktype, 375 struct kobject *parent, const char *fmt, ...) 376 { 377 va_list args; 378 int error; 379 380 kobject_init(kobj, ktype); 381 kobj->ktype = ktype; 382 kobj->parent = parent; 383 kobj->name = NULL; 384 385 va_start(args, fmt); 386 error = kobject_set_name_vargs(kobj, fmt, args); 387 va_end(args); 388 if (error) 389 return (error); 390 return kobject_add_complete(kobj, parent); 391 } 392 393 static void 394 linux_file_dtor(void *cdp) 395 { 396 struct linux_file *filp; 397 398 linux_set_current(curthread); 399 filp = cdp; 400 filp->f_op->release(filp->f_vnode, filp); 401 vdrop(filp->f_vnode); 402 kfree(filp); 403 } 404 405 static void 406 linux_kq_lock(void *arg) 407 { 408 spinlock_t *s = arg; 409 410 spin_lock(s); 411 } 412 static void 413 linux_kq_unlock(void *arg) 414 { 415 spinlock_t *s = arg; 416 417 spin_unlock(s); 418 } 419 420 static void 421 linux_kq_lock_owned(void *arg) 422 { 423 #ifdef INVARIANTS 424 spinlock_t *s = arg; 425 426 mtx_assert(&s->m, MA_OWNED); 427 #endif 428 } 429 430 static void 431 linux_kq_lock_unowned(void *arg) 432 { 433 #ifdef INVARIANTS 434 spinlock_t *s = arg; 435 436 mtx_assert(&s->m, MA_NOTOWNED); 437 #endif 438 } 439 440 static void 441 linux_dev_kqfilter_poll(struct linux_file *, int); 442 443 struct linux_file * 444 linux_file_alloc(void) 445 { 446 struct linux_file *filp; 447 448 filp = kzalloc(sizeof(*filp), GFP_KERNEL); 449 450 /* set initial refcount */ 451 filp->f_count = 1; 452 453 /* setup fields needed by kqueue support */ 454 spin_lock_init(&filp->f_kqlock); 455 knlist_init(&filp->f_selinfo.si_note, &filp->f_kqlock, 456 linux_kq_lock, linux_kq_unlock, 457 linux_kq_lock_owned, linux_kq_lock_unowned); 458 459 return (filp); 460 } 461 462 void 463 linux_file_free(struct linux_file *filp) 464 { 465 if (filp->_file == NULL) { 466 if (filp->f_shmem != NULL) 467 vm_object_deallocate(filp->f_shmem); 468 kfree(filp); 469 } else { 470 /* 471 * The close method of the character device or file 472 * will free the linux_file structure: 473 */ 474 _fdrop(filp->_file, curthread); 475 } 476 } 477 478 static int 479 linux_cdev_pager_fault(vm_object_t vm_obj, vm_ooffset_t offset, int prot, 480 vm_page_t *mres) 481 { 482 struct vm_area_struct *vmap; 483 484 vmap = linux_cdev_handle_find(vm_obj->handle); 485 486 MPASS(vmap != NULL); 487 MPASS(vmap->vm_private_data == vm_obj->handle); 488 489 if (likely(vmap->vm_ops != NULL && offset < vmap->vm_len)) { 490 vm_paddr_t paddr = IDX_TO_OFF(vmap->vm_pfn) + offset; 491 vm_page_t page; 492 493 if (((*mres)->flags & PG_FICTITIOUS) != 0) { 494 /* 495 * If the passed in result page is a fake 496 * page, update it with the new physical 497 * address. 498 */ 499 page = *mres; 500 vm_page_updatefake(page, paddr, vm_obj->memattr); 501 } else { 502 /* 503 * Replace the passed in "mres" page with our 504 * own fake page and free up the all of the 505 * original pages. 506 */ 507 VM_OBJECT_WUNLOCK(vm_obj); 508 page = vm_page_getfake(paddr, vm_obj->memattr); 509 VM_OBJECT_WLOCK(vm_obj); 510 511 vm_page_replace_checked(page, vm_obj, 512 (*mres)->pindex, *mres); 513 514 vm_page_lock(*mres); 515 vm_page_free(*mres); 516 vm_page_unlock(*mres); 517 *mres = page; 518 } 519 page->valid = VM_PAGE_BITS_ALL; 520 return (VM_PAGER_OK); 521 } 522 return (VM_PAGER_FAIL); 523 } 524 525 static int 526 linux_cdev_pager_populate(vm_object_t vm_obj, vm_pindex_t pidx, int fault_type, 527 vm_prot_t max_prot, vm_pindex_t *first, vm_pindex_t *last) 528 { 529 struct vm_area_struct *vmap; 530 int err; 531 532 linux_set_current(curthread); 533 534 /* get VM area structure */ 535 vmap = linux_cdev_handle_find(vm_obj->handle); 536 MPASS(vmap != NULL); 537 MPASS(vmap->vm_private_data == vm_obj->handle); 538 539 VM_OBJECT_WUNLOCK(vm_obj); 540 541 down_write(&vmap->vm_mm->mmap_sem); 542 if (unlikely(vmap->vm_ops == NULL)) { 543 err = VM_FAULT_SIGBUS; 544 } else { 545 struct vm_fault vmf; 546 547 /* fill out VM fault structure */ 548 vmf.virtual_address = (void *)((uintptr_t)pidx << PAGE_SHIFT); 549 vmf.flags = (fault_type & VM_PROT_WRITE) ? FAULT_FLAG_WRITE : 0; 550 vmf.pgoff = 0; 551 vmf.page = NULL; 552 553 vmap->vm_pfn_count = 0; 554 vmap->vm_pfn_pcount = &vmap->vm_pfn_count; 555 vmap->vm_obj = vm_obj; 556 557 err = vmap->vm_ops->fault(vmap, &vmf); 558 559 while (vmap->vm_pfn_count == 0 && err == VM_FAULT_NOPAGE) { 560 kern_yield(PRI_USER); 561 err = vmap->vm_ops->fault(vmap, &vmf); 562 } 563 } 564 565 /* translate return code */ 566 switch (err) { 567 case VM_FAULT_OOM: 568 err = VM_PAGER_AGAIN; 569 break; 570 case VM_FAULT_SIGBUS: 571 err = VM_PAGER_BAD; 572 break; 573 case VM_FAULT_NOPAGE: 574 /* 575 * By contract the fault handler will return having 576 * busied all the pages itself. If pidx is already 577 * found in the object, it will simply xbusy the first 578 * page and return with vm_pfn_count set to 1. 579 */ 580 *first = vmap->vm_pfn_first; 581 *last = *first + vmap->vm_pfn_count - 1; 582 err = VM_PAGER_OK; 583 break; 584 default: 585 err = VM_PAGER_ERROR; 586 break; 587 } 588 up_write(&vmap->vm_mm->mmap_sem); 589 VM_OBJECT_WLOCK(vm_obj); 590 return (err); 591 } 592 593 static struct rwlock linux_vma_lock; 594 static TAILQ_HEAD(, vm_area_struct) linux_vma_head = 595 TAILQ_HEAD_INITIALIZER(linux_vma_head); 596 597 static void 598 linux_cdev_handle_free(struct vm_area_struct *vmap) 599 { 600 /* Drop reference on vm_file */ 601 if (vmap->vm_file != NULL) 602 fput(vmap->vm_file); 603 604 /* Drop reference on mm_struct */ 605 mmput(vmap->vm_mm); 606 607 kfree(vmap); 608 } 609 610 static struct vm_area_struct * 611 linux_cdev_handle_insert(void *handle, struct vm_area_struct *vmap) 612 { 613 struct vm_area_struct *ptr; 614 615 rw_wlock(&linux_vma_lock); 616 TAILQ_FOREACH(ptr, &linux_vma_head, vm_entry) { 617 if (ptr->vm_private_data == handle) { 618 rw_wunlock(&linux_vma_lock); 619 linux_cdev_handle_free(vmap); 620 return (NULL); 621 } 622 } 623 TAILQ_INSERT_TAIL(&linux_vma_head, vmap, vm_entry); 624 rw_wunlock(&linux_vma_lock); 625 return (vmap); 626 } 627 628 static void 629 linux_cdev_handle_remove(struct vm_area_struct *vmap) 630 { 631 rw_wlock(&linux_vma_lock); 632 TAILQ_REMOVE(&linux_vma_head, vmap, vm_entry); 633 rw_wunlock(&linux_vma_lock); 634 } 635 636 static struct vm_area_struct * 637 linux_cdev_handle_find(void *handle) 638 { 639 struct vm_area_struct *vmap; 640 641 rw_rlock(&linux_vma_lock); 642 TAILQ_FOREACH(vmap, &linux_vma_head, vm_entry) { 643 if (vmap->vm_private_data == handle) 644 break; 645 } 646 rw_runlock(&linux_vma_lock); 647 return (vmap); 648 } 649 650 static int 651 linux_cdev_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot, 652 vm_ooffset_t foff, struct ucred *cred, u_short *color) 653 { 654 655 MPASS(linux_cdev_handle_find(handle) != NULL); 656 *color = 0; 657 return (0); 658 } 659 660 static void 661 linux_cdev_pager_dtor(void *handle) 662 { 663 const struct vm_operations_struct *vm_ops; 664 struct vm_area_struct *vmap; 665 666 vmap = linux_cdev_handle_find(handle); 667 MPASS(vmap != NULL); 668 669 /* 670 * Remove handle before calling close operation to prevent 671 * other threads from reusing the handle pointer. 672 */ 673 linux_cdev_handle_remove(vmap); 674 675 down_write(&vmap->vm_mm->mmap_sem); 676 vm_ops = vmap->vm_ops; 677 if (likely(vm_ops != NULL)) 678 vm_ops->close(vmap); 679 up_write(&vmap->vm_mm->mmap_sem); 680 681 linux_cdev_handle_free(vmap); 682 } 683 684 static struct cdev_pager_ops linux_cdev_pager_ops[2] = { 685 { 686 /* OBJT_MGTDEVICE */ 687 .cdev_pg_populate = linux_cdev_pager_populate, 688 .cdev_pg_ctor = linux_cdev_pager_ctor, 689 .cdev_pg_dtor = linux_cdev_pager_dtor 690 }, 691 { 692 /* OBJT_DEVICE */ 693 .cdev_pg_fault = linux_cdev_pager_fault, 694 .cdev_pg_ctor = linux_cdev_pager_ctor, 695 .cdev_pg_dtor = linux_cdev_pager_dtor 696 }, 697 }; 698 699 static int 700 linux_dev_open(struct cdev *dev, int oflags, int devtype, struct thread *td) 701 { 702 struct linux_cdev *ldev; 703 struct linux_file *filp; 704 struct file *file; 705 int error; 706 707 file = td->td_fpop; 708 ldev = dev->si_drv1; 709 if (ldev == NULL) 710 return (ENODEV); 711 712 filp = linux_file_alloc(); 713 filp->f_dentry = &filp->f_dentry_store; 714 filp->f_op = ldev->ops; 715 filp->f_flags = file->f_flag; 716 vhold(file->f_vnode); 717 filp->f_vnode = file->f_vnode; 718 filp->_file = file; 719 720 linux_set_current(td); 721 722 if (filp->f_op->open) { 723 error = -filp->f_op->open(file->f_vnode, filp); 724 if (error) { 725 vdrop(filp->f_vnode); 726 kfree(filp); 727 goto done; 728 } 729 } 730 error = devfs_set_cdevpriv(filp, linux_file_dtor); 731 if (error) { 732 filp->f_op->release(file->f_vnode, filp); 733 vdrop(filp->f_vnode); 734 kfree(filp); 735 } 736 done: 737 return (error); 738 } 739 740 static int 741 linux_dev_close(struct cdev *dev, int fflag, int devtype, struct thread *td) 742 { 743 struct linux_file *filp; 744 struct file *file; 745 int error; 746 747 file = td->td_fpop; 748 if (dev->si_drv1 == NULL) 749 return (0); 750 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0) 751 return (error); 752 filp->f_flags = file->f_flag; 753 devfs_clear_cdevpriv(); 754 755 return (0); 756 } 757 758 #define LINUX_IOCTL_MIN_PTR 0x10000UL 759 #define LINUX_IOCTL_MAX_PTR (LINUX_IOCTL_MIN_PTR + IOCPARM_MAX) 760 761 static inline int 762 linux_remap_address(void **uaddr, size_t len) 763 { 764 uintptr_t uaddr_val = (uintptr_t)(*uaddr); 765 766 if (unlikely(uaddr_val >= LINUX_IOCTL_MIN_PTR && 767 uaddr_val < LINUX_IOCTL_MAX_PTR)) { 768 struct task_struct *pts = current; 769 if (pts == NULL) { 770 *uaddr = NULL; 771 return (1); 772 } 773 774 /* compute data offset */ 775 uaddr_val -= LINUX_IOCTL_MIN_PTR; 776 777 /* check that length is within bounds */ 778 if ((len > IOCPARM_MAX) || 779 (uaddr_val + len) > pts->bsd_ioctl_len) { 780 *uaddr = NULL; 781 return (1); 782 } 783 784 /* re-add kernel buffer address */ 785 uaddr_val += (uintptr_t)pts->bsd_ioctl_data; 786 787 /* update address location */ 788 *uaddr = (void *)uaddr_val; 789 return (1); 790 } 791 return (0); 792 } 793 794 int 795 linux_copyin(const void *uaddr, void *kaddr, size_t len) 796 { 797 if (linux_remap_address(__DECONST(void **, &uaddr), len)) { 798 if (uaddr == NULL) 799 return (-EFAULT); 800 memcpy(kaddr, uaddr, len); 801 return (0); 802 } 803 return (-copyin(uaddr, kaddr, len)); 804 } 805 806 int 807 linux_copyout(const void *kaddr, void *uaddr, size_t len) 808 { 809 if (linux_remap_address(&uaddr, len)) { 810 if (uaddr == NULL) 811 return (-EFAULT); 812 memcpy(uaddr, kaddr, len); 813 return (0); 814 } 815 return (-copyout(kaddr, uaddr, len)); 816 } 817 818 size_t 819 linux_clear_user(void *_uaddr, size_t _len) 820 { 821 uint8_t *uaddr = _uaddr; 822 size_t len = _len; 823 824 /* make sure uaddr is aligned before going into the fast loop */ 825 while (((uintptr_t)uaddr & 7) != 0 && len > 7) { 826 if (subyte(uaddr, 0)) 827 return (_len); 828 uaddr++; 829 len--; 830 } 831 832 /* zero 8 bytes at a time */ 833 while (len > 7) { 834 #ifdef __LP64__ 835 if (suword64(uaddr, 0)) 836 return (_len); 837 #else 838 if (suword32(uaddr, 0)) 839 return (_len); 840 if (suword32(uaddr + 4, 0)) 841 return (_len); 842 #endif 843 uaddr += 8; 844 len -= 8; 845 } 846 847 /* zero fill end, if any */ 848 while (len > 0) { 849 if (subyte(uaddr, 0)) 850 return (_len); 851 uaddr++; 852 len--; 853 } 854 return (0); 855 } 856 857 int 858 linux_access_ok(int rw, const void *uaddr, size_t len) 859 { 860 uintptr_t saddr; 861 uintptr_t eaddr; 862 863 /* get start and end address */ 864 saddr = (uintptr_t)uaddr; 865 eaddr = (uintptr_t)uaddr + len; 866 867 /* verify addresses are valid for userspace */ 868 return ((saddr == eaddr) || 869 (eaddr > saddr && eaddr <= VM_MAXUSER_ADDRESS)); 870 } 871 872 static int 873 linux_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, 874 struct thread *td) 875 { 876 struct linux_file *filp; 877 struct file *file; 878 unsigned size; 879 int error; 880 881 file = td->td_fpop; 882 if (dev->si_drv1 == NULL) 883 return (ENXIO); 884 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0) 885 return (error); 886 filp->f_flags = file->f_flag; 887 888 /* the LinuxKPI supports blocking and non-blocking I/O */ 889 if (cmd == FIONBIO || cmd == FIOASYNC) 890 return (0); 891 892 linux_set_current(td); 893 size = IOCPARM_LEN(cmd); 894 /* refer to logic in sys_ioctl() */ 895 if (size > 0) { 896 /* 897 * Setup hint for linux_copyin() and linux_copyout(). 898 * 899 * Background: Linux code expects a user-space address 900 * while FreeBSD supplies a kernel-space address. 901 */ 902 current->bsd_ioctl_data = data; 903 current->bsd_ioctl_len = size; 904 data = (void *)LINUX_IOCTL_MIN_PTR; 905 } else { 906 /* fetch user-space pointer */ 907 data = *(void **)data; 908 } 909 #if defined(__amd64__) 910 if (td->td_proc->p_elf_machine == EM_386) { 911 /* try the compat IOCTL handler first */ 912 if (filp->f_op->compat_ioctl != NULL) 913 error = -filp->f_op->compat_ioctl(filp, cmd, (u_long)data); 914 else 915 error = ENOTTY; 916 917 /* fallback to the regular IOCTL handler, if any */ 918 if (error == ENOTTY && filp->f_op->unlocked_ioctl != NULL) 919 error = -filp->f_op->unlocked_ioctl(filp, cmd, (u_long)data); 920 } else 921 #endif 922 if (filp->f_op->unlocked_ioctl != NULL) 923 error = -filp->f_op->unlocked_ioctl(filp, cmd, (u_long)data); 924 else 925 error = ENOTTY; 926 if (size > 0) { 927 current->bsd_ioctl_data = NULL; 928 current->bsd_ioctl_len = 0; 929 } 930 931 if (error == EWOULDBLOCK) { 932 /* update kqfilter status, if any */ 933 linux_dev_kqfilter_poll(filp, 934 LINUX_KQ_FLAG_HAS_READ | LINUX_KQ_FLAG_HAS_WRITE); 935 } else if (error == ERESTARTSYS) 936 error = ERESTART; 937 return (error); 938 } 939 940 static int 941 linux_dev_read(struct cdev *dev, struct uio *uio, int ioflag) 942 { 943 struct linux_file *filp; 944 struct thread *td; 945 struct file *file; 946 ssize_t bytes; 947 int error; 948 949 td = curthread; 950 file = td->td_fpop; 951 if (dev->si_drv1 == NULL) 952 return (ENXIO); 953 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0) 954 return (error); 955 filp->f_flags = file->f_flag; 956 /* XXX no support for I/O vectors currently */ 957 if (uio->uio_iovcnt != 1) 958 return (EOPNOTSUPP); 959 linux_set_current(td); 960 if (filp->f_op->read) { 961 bytes = filp->f_op->read(filp, uio->uio_iov->iov_base, 962 uio->uio_iov->iov_len, &uio->uio_offset); 963 if (bytes >= 0) { 964 uio->uio_iov->iov_base = 965 ((uint8_t *)uio->uio_iov->iov_base) + bytes; 966 uio->uio_iov->iov_len -= bytes; 967 uio->uio_resid -= bytes; 968 } else { 969 error = -bytes; 970 if (error == ERESTARTSYS) 971 error = ERESTART; 972 } 973 } else 974 error = ENXIO; 975 976 /* update kqfilter status, if any */ 977 linux_dev_kqfilter_poll(filp, LINUX_KQ_FLAG_HAS_READ); 978 979 return (error); 980 } 981 982 static int 983 linux_dev_write(struct cdev *dev, struct uio *uio, int ioflag) 984 { 985 struct linux_file *filp; 986 struct thread *td; 987 struct file *file; 988 ssize_t bytes; 989 int error; 990 991 td = curthread; 992 file = td->td_fpop; 993 if (dev->si_drv1 == NULL) 994 return (ENXIO); 995 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0) 996 return (error); 997 filp->f_flags = file->f_flag; 998 /* XXX no support for I/O vectors currently */ 999 if (uio->uio_iovcnt != 1) 1000 return (EOPNOTSUPP); 1001 linux_set_current(td); 1002 if (filp->f_op->write) { 1003 bytes = filp->f_op->write(filp, uio->uio_iov->iov_base, 1004 uio->uio_iov->iov_len, &uio->uio_offset); 1005 if (bytes >= 0) { 1006 uio->uio_iov->iov_base = 1007 ((uint8_t *)uio->uio_iov->iov_base) + bytes; 1008 uio->uio_iov->iov_len -= bytes; 1009 uio->uio_resid -= bytes; 1010 } else { 1011 error = -bytes; 1012 if (error == ERESTARTSYS) 1013 error = ERESTART; 1014 } 1015 } else 1016 error = ENXIO; 1017 1018 /* update kqfilter status, if any */ 1019 linux_dev_kqfilter_poll(filp, LINUX_KQ_FLAG_HAS_WRITE); 1020 1021 return (error); 1022 } 1023 1024 #define LINUX_POLL_TABLE_NORMAL ((poll_table *)1) 1025 1026 static int 1027 linux_dev_poll(struct cdev *dev, int events, struct thread *td) 1028 { 1029 struct linux_file *filp; 1030 struct file *file; 1031 int revents; 1032 1033 if (dev->si_drv1 == NULL) 1034 goto error; 1035 if (devfs_get_cdevpriv((void **)&filp) != 0) 1036 goto error; 1037 1038 file = td->td_fpop; 1039 filp->f_flags = file->f_flag; 1040 linux_set_current(td); 1041 if (filp->f_op->poll != NULL) 1042 revents = filp->f_op->poll(filp, LINUX_POLL_TABLE_NORMAL) & events; 1043 else 1044 revents = 0; 1045 1046 return (revents); 1047 error: 1048 return (events & (POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM)); 1049 } 1050 1051 /* 1052 * This function atomically updates the poll wakeup state and returns 1053 * the previous state at the time of update. 1054 */ 1055 static uint8_t 1056 linux_poll_wakeup_state(atomic_t *v, const uint8_t *pstate) 1057 { 1058 int c, old; 1059 1060 c = v->counter; 1061 1062 while ((old = atomic_cmpxchg(v, c, pstate[c])) != c) 1063 c = old; 1064 1065 return (c); 1066 } 1067 1068 1069 static int 1070 linux_poll_wakeup_callback(wait_queue_t *wq, unsigned int wq_state, int flags, void *key) 1071 { 1072 static const uint8_t state[LINUX_FWQ_STATE_MAX] = { 1073 [LINUX_FWQ_STATE_INIT] = LINUX_FWQ_STATE_INIT, /* NOP */ 1074 [LINUX_FWQ_STATE_NOT_READY] = LINUX_FWQ_STATE_NOT_READY, /* NOP */ 1075 [LINUX_FWQ_STATE_QUEUED] = LINUX_FWQ_STATE_READY, 1076 [LINUX_FWQ_STATE_READY] = LINUX_FWQ_STATE_READY, /* NOP */ 1077 }; 1078 struct linux_file *filp = container_of(wq, struct linux_file, f_wait_queue.wq); 1079 1080 switch (linux_poll_wakeup_state(&filp->f_wait_queue.state, state)) { 1081 case LINUX_FWQ_STATE_QUEUED: 1082 linux_poll_wakeup(filp); 1083 return (1); 1084 default: 1085 return (0); 1086 } 1087 } 1088 1089 void 1090 linux_poll_wait(struct linux_file *filp, wait_queue_head_t *wqh, poll_table *p) 1091 { 1092 static const uint8_t state[LINUX_FWQ_STATE_MAX] = { 1093 [LINUX_FWQ_STATE_INIT] = LINUX_FWQ_STATE_NOT_READY, 1094 [LINUX_FWQ_STATE_NOT_READY] = LINUX_FWQ_STATE_NOT_READY, /* NOP */ 1095 [LINUX_FWQ_STATE_QUEUED] = LINUX_FWQ_STATE_QUEUED, /* NOP */ 1096 [LINUX_FWQ_STATE_READY] = LINUX_FWQ_STATE_QUEUED, 1097 }; 1098 1099 /* check if we are called inside the select system call */ 1100 if (p == LINUX_POLL_TABLE_NORMAL) 1101 selrecord(curthread, &filp->f_selinfo); 1102 1103 switch (linux_poll_wakeup_state(&filp->f_wait_queue.state, state)) { 1104 case LINUX_FWQ_STATE_INIT: 1105 /* NOTE: file handles can only belong to one wait-queue */ 1106 filp->f_wait_queue.wqh = wqh; 1107 filp->f_wait_queue.wq.func = &linux_poll_wakeup_callback; 1108 add_wait_queue(wqh, &filp->f_wait_queue.wq); 1109 atomic_set(&filp->f_wait_queue.state, LINUX_FWQ_STATE_QUEUED); 1110 break; 1111 default: 1112 break; 1113 } 1114 } 1115 1116 static void 1117 linux_poll_wait_dequeue(struct linux_file *filp) 1118 { 1119 static const uint8_t state[LINUX_FWQ_STATE_MAX] = { 1120 [LINUX_FWQ_STATE_INIT] = LINUX_FWQ_STATE_INIT, /* NOP */ 1121 [LINUX_FWQ_STATE_NOT_READY] = LINUX_FWQ_STATE_INIT, 1122 [LINUX_FWQ_STATE_QUEUED] = LINUX_FWQ_STATE_INIT, 1123 [LINUX_FWQ_STATE_READY] = LINUX_FWQ_STATE_INIT, 1124 }; 1125 1126 seldrain(&filp->f_selinfo); 1127 1128 switch (linux_poll_wakeup_state(&filp->f_wait_queue.state, state)) { 1129 case LINUX_FWQ_STATE_NOT_READY: 1130 case LINUX_FWQ_STATE_QUEUED: 1131 case LINUX_FWQ_STATE_READY: 1132 remove_wait_queue(filp->f_wait_queue.wqh, &filp->f_wait_queue.wq); 1133 break; 1134 default: 1135 break; 1136 } 1137 } 1138 1139 void 1140 linux_poll_wakeup(struct linux_file *filp) 1141 { 1142 /* this function should be NULL-safe */ 1143 if (filp == NULL) 1144 return; 1145 1146 selwakeup(&filp->f_selinfo); 1147 1148 spin_lock(&filp->f_kqlock); 1149 filp->f_kqflags |= LINUX_KQ_FLAG_NEED_READ | 1150 LINUX_KQ_FLAG_NEED_WRITE; 1151 1152 /* make sure the "knote" gets woken up */ 1153 KNOTE_LOCKED(&filp->f_selinfo.si_note, 1); 1154 spin_unlock(&filp->f_kqlock); 1155 } 1156 1157 static void 1158 linux_dev_kqfilter_detach(struct knote *kn) 1159 { 1160 struct linux_file *filp = kn->kn_hook; 1161 1162 spin_lock(&filp->f_kqlock); 1163 knlist_remove(&filp->f_selinfo.si_note, kn, 1); 1164 spin_unlock(&filp->f_kqlock); 1165 } 1166 1167 static int 1168 linux_dev_kqfilter_read_event(struct knote *kn, long hint) 1169 { 1170 struct linux_file *filp = kn->kn_hook; 1171 1172 mtx_assert(&filp->f_kqlock.m, MA_OWNED); 1173 1174 return ((filp->f_kqflags & LINUX_KQ_FLAG_NEED_READ) ? 1 : 0); 1175 } 1176 1177 static int 1178 linux_dev_kqfilter_write_event(struct knote *kn, long hint) 1179 { 1180 struct linux_file *filp = kn->kn_hook; 1181 1182 mtx_assert(&filp->f_kqlock.m, MA_OWNED); 1183 1184 return ((filp->f_kqflags & LINUX_KQ_FLAG_NEED_WRITE) ? 1 : 0); 1185 } 1186 1187 static struct filterops linux_dev_kqfiltops_read = { 1188 .f_isfd = 1, 1189 .f_detach = linux_dev_kqfilter_detach, 1190 .f_event = linux_dev_kqfilter_read_event, 1191 }; 1192 1193 static struct filterops linux_dev_kqfiltops_write = { 1194 .f_isfd = 1, 1195 .f_detach = linux_dev_kqfilter_detach, 1196 .f_event = linux_dev_kqfilter_write_event, 1197 }; 1198 1199 static void 1200 linux_dev_kqfilter_poll(struct linux_file *filp, int kqflags) 1201 { 1202 int temp; 1203 1204 if (filp->f_kqflags & kqflags) { 1205 /* get the latest polling state */ 1206 temp = filp->f_op->poll(filp, NULL); 1207 1208 spin_lock(&filp->f_kqlock); 1209 /* clear kqflags */ 1210 filp->f_kqflags &= ~(LINUX_KQ_FLAG_NEED_READ | 1211 LINUX_KQ_FLAG_NEED_WRITE); 1212 /* update kqflags */ 1213 if (temp & (POLLIN | POLLOUT)) { 1214 if (temp & POLLIN) 1215 filp->f_kqflags |= LINUX_KQ_FLAG_NEED_READ; 1216 if (temp & POLLOUT) 1217 filp->f_kqflags |= LINUX_KQ_FLAG_NEED_WRITE; 1218 1219 /* make sure the "knote" gets woken up */ 1220 KNOTE_LOCKED(&filp->f_selinfo.si_note, 0); 1221 } 1222 spin_unlock(&filp->f_kqlock); 1223 } 1224 } 1225 1226 static int 1227 linux_dev_kqfilter(struct cdev *dev, struct knote *kn) 1228 { 1229 struct linux_file *filp; 1230 struct file *file; 1231 struct thread *td; 1232 int error; 1233 1234 td = curthread; 1235 file = td->td_fpop; 1236 if (dev->si_drv1 == NULL) 1237 return (ENXIO); 1238 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0) 1239 return (error); 1240 filp->f_flags = file->f_flag; 1241 if (filp->f_op->poll == NULL) 1242 return (EINVAL); 1243 1244 spin_lock(&filp->f_kqlock); 1245 switch (kn->kn_filter) { 1246 case EVFILT_READ: 1247 filp->f_kqflags |= LINUX_KQ_FLAG_HAS_READ; 1248 kn->kn_fop = &linux_dev_kqfiltops_read; 1249 kn->kn_hook = filp; 1250 knlist_add(&filp->f_selinfo.si_note, kn, 1); 1251 break; 1252 case EVFILT_WRITE: 1253 filp->f_kqflags |= LINUX_KQ_FLAG_HAS_WRITE; 1254 kn->kn_fop = &linux_dev_kqfiltops_write; 1255 kn->kn_hook = filp; 1256 knlist_add(&filp->f_selinfo.si_note, kn, 1); 1257 break; 1258 default: 1259 error = EINVAL; 1260 break; 1261 } 1262 spin_unlock(&filp->f_kqlock); 1263 1264 if (error == 0) { 1265 linux_set_current(td); 1266 1267 /* update kqfilter status, if any */ 1268 linux_dev_kqfilter_poll(filp, 1269 LINUX_KQ_FLAG_HAS_READ | LINUX_KQ_FLAG_HAS_WRITE); 1270 } 1271 return (error); 1272 } 1273 1274 static int 1275 linux_dev_mmap_single(struct cdev *dev, vm_ooffset_t *offset, 1276 vm_size_t size, struct vm_object **object, int nprot) 1277 { 1278 struct vm_area_struct *vmap; 1279 struct mm_struct *mm; 1280 struct linux_file *filp; 1281 struct thread *td; 1282 struct file *file; 1283 vm_memattr_t attr; 1284 int error; 1285 1286 td = curthread; 1287 file = td->td_fpop; 1288 if (dev->si_drv1 == NULL) 1289 return (ENODEV); 1290 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0) 1291 return (error); 1292 filp->f_flags = file->f_flag; 1293 1294 if (filp->f_op->mmap == NULL) 1295 return (ENODEV); 1296 1297 linux_set_current(td); 1298 1299 /* 1300 * The same VM object might be shared by multiple processes 1301 * and the mm_struct is usually freed when a process exits. 1302 * 1303 * The atomic reference below makes sure the mm_struct is 1304 * available as long as the vmap is in the linux_vma_head. 1305 */ 1306 mm = current->mm; 1307 if (atomic_inc_not_zero(&mm->mm_users) == 0) 1308 return (EINVAL); 1309 1310 vmap = kzalloc(sizeof(*vmap), GFP_KERNEL); 1311 vmap->vm_start = 0; 1312 vmap->vm_end = size; 1313 vmap->vm_pgoff = *offset / PAGE_SIZE; 1314 vmap->vm_pfn = 0; 1315 vmap->vm_flags = vmap->vm_page_prot = (nprot & VM_PROT_ALL); 1316 vmap->vm_ops = NULL; 1317 vmap->vm_file = get_file(filp); 1318 vmap->vm_mm = mm; 1319 1320 if (unlikely(down_write_killable(&vmap->vm_mm->mmap_sem))) { 1321 error = EINTR; 1322 } else { 1323 error = -filp->f_op->mmap(filp, vmap); 1324 up_write(&vmap->vm_mm->mmap_sem); 1325 } 1326 1327 if (error != 0) { 1328 linux_cdev_handle_free(vmap); 1329 return (error); 1330 } 1331 1332 attr = pgprot2cachemode(vmap->vm_page_prot); 1333 1334 if (vmap->vm_ops != NULL) { 1335 void *vm_private_data; 1336 1337 if (vmap->vm_ops->open == NULL || 1338 vmap->vm_ops->close == NULL || 1339 vmap->vm_private_data == NULL) { 1340 linux_cdev_handle_free(vmap); 1341 return (EINVAL); 1342 } 1343 1344 vm_private_data = vmap->vm_private_data; 1345 1346 vmap = linux_cdev_handle_insert(vm_private_data, vmap); 1347 1348 if (vmap->vm_ops->fault == NULL) { 1349 *object = cdev_pager_allocate(vm_private_data, OBJT_DEVICE, 1350 &linux_cdev_pager_ops[1], size, nprot, *offset, 1351 curthread->td_ucred); 1352 } else { 1353 *object = cdev_pager_allocate(vm_private_data, OBJT_MGTDEVICE, 1354 &linux_cdev_pager_ops[0], size, nprot, *offset, 1355 curthread->td_ucred); 1356 } 1357 1358 if (*object == NULL) { 1359 linux_cdev_handle_remove(vmap); 1360 linux_cdev_handle_free(vmap); 1361 return (EINVAL); 1362 } 1363 } else { 1364 struct sglist *sg; 1365 1366 sg = sglist_alloc(1, M_WAITOK); 1367 sglist_append_phys(sg, 1368 (vm_paddr_t)vmap->vm_pfn << PAGE_SHIFT, vmap->vm_len); 1369 1370 *object = vm_pager_allocate(OBJT_SG, sg, vmap->vm_len, 1371 nprot, 0, curthread->td_ucred); 1372 1373 linux_cdev_handle_free(vmap); 1374 1375 if (*object == NULL) { 1376 sglist_free(sg); 1377 return (EINVAL); 1378 } 1379 } 1380 1381 if (attr != VM_MEMATTR_DEFAULT) { 1382 VM_OBJECT_WLOCK(*object); 1383 vm_object_set_memattr(*object, attr); 1384 VM_OBJECT_WUNLOCK(*object); 1385 } 1386 *offset = 0; 1387 return (0); 1388 } 1389 1390 struct cdevsw linuxcdevsw = { 1391 .d_version = D_VERSION, 1392 .d_flags = D_TRACKCLOSE, 1393 .d_open = linux_dev_open, 1394 .d_close = linux_dev_close, 1395 .d_read = linux_dev_read, 1396 .d_write = linux_dev_write, 1397 .d_ioctl = linux_dev_ioctl, 1398 .d_mmap_single = linux_dev_mmap_single, 1399 .d_poll = linux_dev_poll, 1400 .d_kqfilter = linux_dev_kqfilter, 1401 .d_name = "lkpidev", 1402 }; 1403 1404 static int 1405 linux_file_read(struct file *file, struct uio *uio, struct ucred *active_cred, 1406 int flags, struct thread *td) 1407 { 1408 struct linux_file *filp; 1409 ssize_t bytes; 1410 int error; 1411 1412 error = 0; 1413 filp = (struct linux_file *)file->f_data; 1414 filp->f_flags = file->f_flag; 1415 /* XXX no support for I/O vectors currently */ 1416 if (uio->uio_iovcnt != 1) 1417 return (EOPNOTSUPP); 1418 linux_set_current(td); 1419 if (filp->f_op->read) { 1420 bytes = filp->f_op->read(filp, uio->uio_iov->iov_base, 1421 uio->uio_iov->iov_len, &uio->uio_offset); 1422 if (bytes >= 0) { 1423 uio->uio_iov->iov_base = 1424 ((uint8_t *)uio->uio_iov->iov_base) + bytes; 1425 uio->uio_iov->iov_len -= bytes; 1426 uio->uio_resid -= bytes; 1427 } else 1428 error = -bytes; 1429 } else 1430 error = ENXIO; 1431 1432 return (error); 1433 } 1434 1435 static int 1436 linux_file_poll(struct file *file, int events, struct ucred *active_cred, 1437 struct thread *td) 1438 { 1439 struct linux_file *filp; 1440 int revents; 1441 1442 filp = (struct linux_file *)file->f_data; 1443 filp->f_flags = file->f_flag; 1444 linux_set_current(td); 1445 if (filp->f_op->poll != NULL) 1446 revents = filp->f_op->poll(filp, LINUX_POLL_TABLE_NORMAL) & events; 1447 else 1448 revents = 0; 1449 1450 return (revents); 1451 } 1452 1453 static int 1454 linux_file_close(struct file *file, struct thread *td) 1455 { 1456 struct linux_file *filp; 1457 int error; 1458 1459 filp = (struct linux_file *)file->f_data; 1460 filp->f_flags = file->f_flag; 1461 linux_set_current(td); 1462 linux_poll_wait_dequeue(filp); 1463 error = -filp->f_op->release(NULL, filp); 1464 funsetown(&filp->f_sigio); 1465 kfree(filp); 1466 1467 return (error); 1468 } 1469 1470 static int 1471 linux_file_ioctl(struct file *fp, u_long cmd, void *data, struct ucred *cred, 1472 struct thread *td) 1473 { 1474 struct linux_file *filp; 1475 int error; 1476 1477 filp = (struct linux_file *)fp->f_data; 1478 filp->f_flags = fp->f_flag; 1479 error = 0; 1480 1481 linux_set_current(td); 1482 switch (cmd) { 1483 case FIONBIO: 1484 break; 1485 case FIOASYNC: 1486 if (filp->f_op->fasync == NULL) 1487 break; 1488 error = filp->f_op->fasync(0, filp, fp->f_flag & FASYNC); 1489 break; 1490 case FIOSETOWN: 1491 error = fsetown(*(int *)data, &filp->f_sigio); 1492 if (error == 0) 1493 error = filp->f_op->fasync(0, filp, 1494 fp->f_flag & FASYNC); 1495 break; 1496 case FIOGETOWN: 1497 *(int *)data = fgetown(&filp->f_sigio); 1498 break; 1499 default: 1500 error = ENOTTY; 1501 break; 1502 } 1503 return (error); 1504 } 1505 1506 static int 1507 linux_file_stat(struct file *fp, struct stat *sb, struct ucred *active_cred, 1508 struct thread *td) 1509 { 1510 1511 return (EOPNOTSUPP); 1512 } 1513 1514 static int 1515 linux_file_fill_kinfo(struct file *fp, struct kinfo_file *kif, 1516 struct filedesc *fdp) 1517 { 1518 1519 return (0); 1520 } 1521 1522 unsigned int 1523 linux_iminor(struct inode *inode) 1524 { 1525 struct linux_cdev *ldev; 1526 1527 if (inode == NULL || inode->v_rdev == NULL || 1528 inode->v_rdev->si_devsw != &linuxcdevsw) 1529 return (-1U); 1530 ldev = inode->v_rdev->si_drv1; 1531 if (ldev == NULL) 1532 return (-1U); 1533 1534 return (minor(ldev->dev)); 1535 } 1536 1537 struct fileops linuxfileops = { 1538 .fo_read = linux_file_read, 1539 .fo_write = invfo_rdwr, 1540 .fo_truncate = invfo_truncate, 1541 .fo_kqfilter = invfo_kqfilter, 1542 .fo_stat = linux_file_stat, 1543 .fo_fill_kinfo = linux_file_fill_kinfo, 1544 .fo_poll = linux_file_poll, 1545 .fo_close = linux_file_close, 1546 .fo_ioctl = linux_file_ioctl, 1547 .fo_chmod = invfo_chmod, 1548 .fo_chown = invfo_chown, 1549 .fo_sendfile = invfo_sendfile, 1550 }; 1551 1552 /* 1553 * Hash of vmmap addresses. This is infrequently accessed and does not 1554 * need to be particularly large. This is done because we must store the 1555 * caller's idea of the map size to properly unmap. 1556 */ 1557 struct vmmap { 1558 LIST_ENTRY(vmmap) vm_next; 1559 void *vm_addr; 1560 unsigned long vm_size; 1561 }; 1562 1563 struct vmmaphd { 1564 struct vmmap *lh_first; 1565 }; 1566 #define VMMAP_HASH_SIZE 64 1567 #define VMMAP_HASH_MASK (VMMAP_HASH_SIZE - 1) 1568 #define VM_HASH(addr) ((uintptr_t)(addr) >> PAGE_SHIFT) & VMMAP_HASH_MASK 1569 static struct vmmaphd vmmaphead[VMMAP_HASH_SIZE]; 1570 static struct mtx vmmaplock; 1571 1572 static void 1573 vmmap_add(void *addr, unsigned long size) 1574 { 1575 struct vmmap *vmmap; 1576 1577 vmmap = kmalloc(sizeof(*vmmap), GFP_KERNEL); 1578 mtx_lock(&vmmaplock); 1579 vmmap->vm_size = size; 1580 vmmap->vm_addr = addr; 1581 LIST_INSERT_HEAD(&vmmaphead[VM_HASH(addr)], vmmap, vm_next); 1582 mtx_unlock(&vmmaplock); 1583 } 1584 1585 static struct vmmap * 1586 vmmap_remove(void *addr) 1587 { 1588 struct vmmap *vmmap; 1589 1590 mtx_lock(&vmmaplock); 1591 LIST_FOREACH(vmmap, &vmmaphead[VM_HASH(addr)], vm_next) 1592 if (vmmap->vm_addr == addr) 1593 break; 1594 if (vmmap) 1595 LIST_REMOVE(vmmap, vm_next); 1596 mtx_unlock(&vmmaplock); 1597 1598 return (vmmap); 1599 } 1600 1601 #if defined(__i386__) || defined(__amd64__) || defined(__powerpc__) 1602 void * 1603 _ioremap_attr(vm_paddr_t phys_addr, unsigned long size, int attr) 1604 { 1605 void *addr; 1606 1607 addr = pmap_mapdev_attr(phys_addr, size, attr); 1608 if (addr == NULL) 1609 return (NULL); 1610 vmmap_add(addr, size); 1611 1612 return (addr); 1613 } 1614 #endif 1615 1616 void 1617 iounmap(void *addr) 1618 { 1619 struct vmmap *vmmap; 1620 1621 vmmap = vmmap_remove(addr); 1622 if (vmmap == NULL) 1623 return; 1624 #if defined(__i386__) || defined(__amd64__) || defined(__powerpc__) 1625 pmap_unmapdev((vm_offset_t)addr, vmmap->vm_size); 1626 #endif 1627 kfree(vmmap); 1628 } 1629 1630 1631 void * 1632 vmap(struct page **pages, unsigned int count, unsigned long flags, int prot) 1633 { 1634 vm_offset_t off; 1635 size_t size; 1636 1637 size = count * PAGE_SIZE; 1638 off = kva_alloc(size); 1639 if (off == 0) 1640 return (NULL); 1641 vmmap_add((void *)off, size); 1642 pmap_qenter(off, pages, count); 1643 1644 return ((void *)off); 1645 } 1646 1647 void 1648 vunmap(void *addr) 1649 { 1650 struct vmmap *vmmap; 1651 1652 vmmap = vmmap_remove(addr); 1653 if (vmmap == NULL) 1654 return; 1655 pmap_qremove((vm_offset_t)addr, vmmap->vm_size / PAGE_SIZE); 1656 kva_free((vm_offset_t)addr, vmmap->vm_size); 1657 kfree(vmmap); 1658 } 1659 1660 char * 1661 kvasprintf(gfp_t gfp, const char *fmt, va_list ap) 1662 { 1663 unsigned int len; 1664 char *p; 1665 va_list aq; 1666 1667 va_copy(aq, ap); 1668 len = vsnprintf(NULL, 0, fmt, aq); 1669 va_end(aq); 1670 1671 p = kmalloc(len + 1, gfp); 1672 if (p != NULL) 1673 vsnprintf(p, len + 1, fmt, ap); 1674 1675 return (p); 1676 } 1677 1678 char * 1679 kasprintf(gfp_t gfp, const char *fmt, ...) 1680 { 1681 va_list ap; 1682 char *p; 1683 1684 va_start(ap, fmt); 1685 p = kvasprintf(gfp, fmt, ap); 1686 va_end(ap); 1687 1688 return (p); 1689 } 1690 1691 static void 1692 linux_timer_callback_wrapper(void *context) 1693 { 1694 struct timer_list *timer; 1695 1696 linux_set_current(curthread); 1697 1698 timer = context; 1699 timer->function(timer->data); 1700 } 1701 1702 void 1703 mod_timer(struct timer_list *timer, int expires) 1704 { 1705 1706 timer->expires = expires; 1707 callout_reset(&timer->timer_callout, 1708 linux_timer_jiffies_until(expires), 1709 &linux_timer_callback_wrapper, timer); 1710 } 1711 1712 void 1713 add_timer(struct timer_list *timer) 1714 { 1715 1716 callout_reset(&timer->timer_callout, 1717 linux_timer_jiffies_until(timer->expires), 1718 &linux_timer_callback_wrapper, timer); 1719 } 1720 1721 void 1722 add_timer_on(struct timer_list *timer, int cpu) 1723 { 1724 1725 callout_reset_on(&timer->timer_callout, 1726 linux_timer_jiffies_until(timer->expires), 1727 &linux_timer_callback_wrapper, timer, cpu); 1728 } 1729 1730 static void 1731 linux_timer_init(void *arg) 1732 { 1733 1734 /* 1735 * Compute an internal HZ value which can divide 2**32 to 1736 * avoid timer rounding problems when the tick value wraps 1737 * around 2**32: 1738 */ 1739 linux_timer_hz_mask = 1; 1740 while (linux_timer_hz_mask < (unsigned long)hz) 1741 linux_timer_hz_mask *= 2; 1742 linux_timer_hz_mask--; 1743 } 1744 SYSINIT(linux_timer, SI_SUB_DRIVERS, SI_ORDER_FIRST, linux_timer_init, NULL); 1745 1746 void 1747 linux_complete_common(struct completion *c, int all) 1748 { 1749 int wakeup_swapper; 1750 1751 sleepq_lock(c); 1752 c->done++; 1753 if (all) 1754 wakeup_swapper = sleepq_broadcast(c, SLEEPQ_SLEEP, 0, 0); 1755 else 1756 wakeup_swapper = sleepq_signal(c, SLEEPQ_SLEEP, 0, 0); 1757 sleepq_release(c); 1758 if (wakeup_swapper) 1759 kick_proc0(); 1760 } 1761 1762 /* 1763 * Indefinite wait for done != 0 with or without signals. 1764 */ 1765 int 1766 linux_wait_for_common(struct completion *c, int flags) 1767 { 1768 int error; 1769 1770 if (SCHEDULER_STOPPED()) 1771 return (0); 1772 1773 DROP_GIANT(); 1774 1775 if (flags != 0) 1776 flags = SLEEPQ_INTERRUPTIBLE | SLEEPQ_SLEEP; 1777 else 1778 flags = SLEEPQ_SLEEP; 1779 error = 0; 1780 for (;;) { 1781 sleepq_lock(c); 1782 if (c->done) 1783 break; 1784 sleepq_add(c, NULL, "completion", flags, 0); 1785 if (flags & SLEEPQ_INTERRUPTIBLE) { 1786 if (sleepq_wait_sig(c, 0) != 0) { 1787 error = -ERESTARTSYS; 1788 goto intr; 1789 } 1790 } else 1791 sleepq_wait(c, 0); 1792 } 1793 c->done--; 1794 sleepq_release(c); 1795 1796 intr: 1797 PICKUP_GIANT(); 1798 1799 return (error); 1800 } 1801 1802 /* 1803 * Time limited wait for done != 0 with or without signals. 1804 */ 1805 int 1806 linux_wait_for_timeout_common(struct completion *c, int timeout, int flags) 1807 { 1808 int end = jiffies + timeout; 1809 int error; 1810 int ret; 1811 1812 if (SCHEDULER_STOPPED()) 1813 return (0); 1814 1815 DROP_GIANT(); 1816 1817 if (flags != 0) 1818 flags = SLEEPQ_INTERRUPTIBLE | SLEEPQ_SLEEP; 1819 else 1820 flags = SLEEPQ_SLEEP; 1821 1822 error = 0; 1823 ret = 0; 1824 for (;;) { 1825 sleepq_lock(c); 1826 if (c->done) 1827 break; 1828 sleepq_add(c, NULL, "completion", flags, 0); 1829 sleepq_set_timeout(c, linux_timer_jiffies_until(end)); 1830 if (flags & SLEEPQ_INTERRUPTIBLE) 1831 ret = sleepq_timedwait_sig(c, 0); 1832 else 1833 ret = sleepq_timedwait(c, 0); 1834 if (ret != 0) { 1835 /* check for timeout or signal */ 1836 if (ret == EWOULDBLOCK) 1837 error = 0; 1838 else 1839 error = -ERESTARTSYS; 1840 goto intr; 1841 } 1842 } 1843 c->done--; 1844 sleepq_release(c); 1845 1846 intr: 1847 PICKUP_GIANT(); 1848 1849 /* return how many jiffies are left */ 1850 return (ret != 0 ? error : linux_timer_jiffies_until(end)); 1851 } 1852 1853 int 1854 linux_try_wait_for_completion(struct completion *c) 1855 { 1856 int isdone; 1857 1858 isdone = 1; 1859 sleepq_lock(c); 1860 if (c->done) 1861 c->done--; 1862 else 1863 isdone = 0; 1864 sleepq_release(c); 1865 return (isdone); 1866 } 1867 1868 int 1869 linux_completion_done(struct completion *c) 1870 { 1871 int isdone; 1872 1873 isdone = 1; 1874 sleepq_lock(c); 1875 if (c->done == 0) 1876 isdone = 0; 1877 sleepq_release(c); 1878 return (isdone); 1879 } 1880 1881 static void 1882 linux_cdev_release(struct kobject *kobj) 1883 { 1884 struct linux_cdev *cdev; 1885 struct kobject *parent; 1886 1887 cdev = container_of(kobj, struct linux_cdev, kobj); 1888 parent = kobj->parent; 1889 if (cdev->cdev) 1890 destroy_dev(cdev->cdev); 1891 kfree(cdev); 1892 kobject_put(parent); 1893 } 1894 1895 static void 1896 linux_cdev_static_release(struct kobject *kobj) 1897 { 1898 struct linux_cdev *cdev; 1899 struct kobject *parent; 1900 1901 cdev = container_of(kobj, struct linux_cdev, kobj); 1902 parent = kobj->parent; 1903 if (cdev->cdev) 1904 destroy_dev(cdev->cdev); 1905 kobject_put(parent); 1906 } 1907 1908 const struct kobj_type linux_cdev_ktype = { 1909 .release = linux_cdev_release, 1910 }; 1911 1912 const struct kobj_type linux_cdev_static_ktype = { 1913 .release = linux_cdev_static_release, 1914 }; 1915 1916 static void 1917 linux_handle_ifnet_link_event(void *arg, struct ifnet *ifp, int linkstate) 1918 { 1919 struct notifier_block *nb; 1920 1921 nb = arg; 1922 if (linkstate == LINK_STATE_UP) 1923 nb->notifier_call(nb, NETDEV_UP, ifp); 1924 else 1925 nb->notifier_call(nb, NETDEV_DOWN, ifp); 1926 } 1927 1928 static void 1929 linux_handle_ifnet_arrival_event(void *arg, struct ifnet *ifp) 1930 { 1931 struct notifier_block *nb; 1932 1933 nb = arg; 1934 nb->notifier_call(nb, NETDEV_REGISTER, ifp); 1935 } 1936 1937 static void 1938 linux_handle_ifnet_departure_event(void *arg, struct ifnet *ifp) 1939 { 1940 struct notifier_block *nb; 1941 1942 nb = arg; 1943 nb->notifier_call(nb, NETDEV_UNREGISTER, ifp); 1944 } 1945 1946 static void 1947 linux_handle_iflladdr_event(void *arg, struct ifnet *ifp) 1948 { 1949 struct notifier_block *nb; 1950 1951 nb = arg; 1952 nb->notifier_call(nb, NETDEV_CHANGEADDR, ifp); 1953 } 1954 1955 static void 1956 linux_handle_ifaddr_event(void *arg, struct ifnet *ifp) 1957 { 1958 struct notifier_block *nb; 1959 1960 nb = arg; 1961 nb->notifier_call(nb, NETDEV_CHANGEIFADDR, ifp); 1962 } 1963 1964 int 1965 register_netdevice_notifier(struct notifier_block *nb) 1966 { 1967 1968 nb->tags[NETDEV_UP] = EVENTHANDLER_REGISTER( 1969 ifnet_link_event, linux_handle_ifnet_link_event, nb, 0); 1970 nb->tags[NETDEV_REGISTER] = EVENTHANDLER_REGISTER( 1971 ifnet_arrival_event, linux_handle_ifnet_arrival_event, nb, 0); 1972 nb->tags[NETDEV_UNREGISTER] = EVENTHANDLER_REGISTER( 1973 ifnet_departure_event, linux_handle_ifnet_departure_event, nb, 0); 1974 nb->tags[NETDEV_CHANGEADDR] = EVENTHANDLER_REGISTER( 1975 iflladdr_event, linux_handle_iflladdr_event, nb, 0); 1976 1977 return (0); 1978 } 1979 1980 int 1981 register_inetaddr_notifier(struct notifier_block *nb) 1982 { 1983 1984 nb->tags[NETDEV_CHANGEIFADDR] = EVENTHANDLER_REGISTER( 1985 ifaddr_event, linux_handle_ifaddr_event, nb, 0); 1986 return (0); 1987 } 1988 1989 int 1990 unregister_netdevice_notifier(struct notifier_block *nb) 1991 { 1992 1993 EVENTHANDLER_DEREGISTER(ifnet_link_event, 1994 nb->tags[NETDEV_UP]); 1995 EVENTHANDLER_DEREGISTER(ifnet_arrival_event, 1996 nb->tags[NETDEV_REGISTER]); 1997 EVENTHANDLER_DEREGISTER(ifnet_departure_event, 1998 nb->tags[NETDEV_UNREGISTER]); 1999 EVENTHANDLER_DEREGISTER(iflladdr_event, 2000 nb->tags[NETDEV_CHANGEADDR]); 2001 2002 return (0); 2003 } 2004 2005 int 2006 unregister_inetaddr_notifier(struct notifier_block *nb) 2007 { 2008 2009 EVENTHANDLER_DEREGISTER(ifaddr_event, 2010 nb->tags[NETDEV_CHANGEIFADDR]); 2011 2012 return (0); 2013 } 2014 2015 struct list_sort_thunk { 2016 int (*cmp)(void *, struct list_head *, struct list_head *); 2017 void *priv; 2018 }; 2019 2020 static inline int 2021 linux_le_cmp(void *priv, const void *d1, const void *d2) 2022 { 2023 struct list_head *le1, *le2; 2024 struct list_sort_thunk *thunk; 2025 2026 thunk = priv; 2027 le1 = *(__DECONST(struct list_head **, d1)); 2028 le2 = *(__DECONST(struct list_head **, d2)); 2029 return ((thunk->cmp)(thunk->priv, le1, le2)); 2030 } 2031 2032 void 2033 list_sort(void *priv, struct list_head *head, int (*cmp)(void *priv, 2034 struct list_head *a, struct list_head *b)) 2035 { 2036 struct list_sort_thunk thunk; 2037 struct list_head **ar, *le; 2038 size_t count, i; 2039 2040 count = 0; 2041 list_for_each(le, head) 2042 count++; 2043 ar = malloc(sizeof(struct list_head *) * count, M_KMALLOC, M_WAITOK); 2044 i = 0; 2045 list_for_each(le, head) 2046 ar[i++] = le; 2047 thunk.cmp = cmp; 2048 thunk.priv = priv; 2049 qsort_r(ar, count, sizeof(struct list_head *), &thunk, linux_le_cmp); 2050 INIT_LIST_HEAD(head); 2051 for (i = 0; i < count; i++) 2052 list_add_tail(ar[i], head); 2053 free(ar, M_KMALLOC); 2054 } 2055 2056 void 2057 linux_irq_handler(void *ent) 2058 { 2059 struct irq_ent *irqe; 2060 2061 linux_set_current(curthread); 2062 2063 irqe = ent; 2064 irqe->handler(irqe->irq, irqe->arg); 2065 } 2066 2067 #if defined(__i386__) || defined(__amd64__) 2068 int 2069 linux_wbinvd_on_all_cpus(void) 2070 { 2071 2072 pmap_invalidate_cache(); 2073 return (0); 2074 } 2075 #endif 2076 2077 int 2078 linux_on_each_cpu(void callback(void *), void *data) 2079 { 2080 2081 smp_rendezvous(smp_no_rendezvous_barrier, callback, 2082 smp_no_rendezvous_barrier, data); 2083 return (0); 2084 } 2085 2086 int 2087 linux_in_atomic(void) 2088 { 2089 2090 return ((curthread->td_pflags & TDP_NOFAULTING) != 0); 2091 } 2092 2093 struct linux_cdev * 2094 linux_find_cdev(const char *name, unsigned major, unsigned minor) 2095 { 2096 dev_t dev = MKDEV(major, minor); 2097 struct cdev *cdev; 2098 2099 dev_lock(); 2100 LIST_FOREACH(cdev, &linuxcdevsw.d_devs, si_list) { 2101 struct linux_cdev *ldev = cdev->si_drv1; 2102 if (ldev->dev == dev && 2103 strcmp(kobject_name(&ldev->kobj), name) == 0) { 2104 break; 2105 } 2106 } 2107 dev_unlock(); 2108 2109 return (cdev != NULL ? cdev->si_drv1 : NULL); 2110 } 2111 2112 int 2113 __register_chrdev(unsigned int major, unsigned int baseminor, 2114 unsigned int count, const char *name, 2115 const struct file_operations *fops) 2116 { 2117 struct linux_cdev *cdev; 2118 int ret = 0; 2119 int i; 2120 2121 for (i = baseminor; i < baseminor + count; i++) { 2122 cdev = cdev_alloc(); 2123 cdev_init(cdev, fops); 2124 kobject_set_name(&cdev->kobj, name); 2125 2126 ret = cdev_add(cdev, makedev(major, i), 1); 2127 if (ret != 0) 2128 break; 2129 } 2130 return (ret); 2131 } 2132 2133 int 2134 __register_chrdev_p(unsigned int major, unsigned int baseminor, 2135 unsigned int count, const char *name, 2136 const struct file_operations *fops, uid_t uid, 2137 gid_t gid, int mode) 2138 { 2139 struct linux_cdev *cdev; 2140 int ret = 0; 2141 int i; 2142 2143 for (i = baseminor; i < baseminor + count; i++) { 2144 cdev = cdev_alloc(); 2145 cdev_init(cdev, fops); 2146 kobject_set_name(&cdev->kobj, name); 2147 2148 ret = cdev_add_ext(cdev, makedev(major, i), uid, gid, mode); 2149 if (ret != 0) 2150 break; 2151 } 2152 return (ret); 2153 } 2154 2155 void 2156 __unregister_chrdev(unsigned int major, unsigned int baseminor, 2157 unsigned int count, const char *name) 2158 { 2159 struct linux_cdev *cdevp; 2160 int i; 2161 2162 for (i = baseminor; i < baseminor + count; i++) { 2163 cdevp = linux_find_cdev(name, major, i); 2164 if (cdevp != NULL) 2165 cdev_del(cdevp); 2166 } 2167 } 2168 2169 #if defined(__i386__) || defined(__amd64__) 2170 bool linux_cpu_has_clflush; 2171 #endif 2172 2173 static void 2174 linux_compat_init(void *arg) 2175 { 2176 struct sysctl_oid *rootoid; 2177 int i; 2178 2179 #if defined(__i386__) || defined(__amd64__) 2180 linux_cpu_has_clflush = (cpu_feature & CPUID_CLFSH); 2181 #endif 2182 rw_init(&linux_vma_lock, "lkpi-vma-lock"); 2183 2184 rootoid = SYSCTL_ADD_ROOT_NODE(NULL, 2185 OID_AUTO, "sys", CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, "sys"); 2186 kobject_init(&linux_class_root, &linux_class_ktype); 2187 kobject_set_name(&linux_class_root, "class"); 2188 linux_class_root.oidp = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(rootoid), 2189 OID_AUTO, "class", CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, "class"); 2190 kobject_init(&linux_root_device.kobj, &linux_dev_ktype); 2191 kobject_set_name(&linux_root_device.kobj, "device"); 2192 linux_root_device.kobj.oidp = SYSCTL_ADD_NODE(NULL, 2193 SYSCTL_CHILDREN(rootoid), OID_AUTO, "device", CTLFLAG_RD, NULL, 2194 "device"); 2195 linux_root_device.bsddev = root_bus; 2196 linux_class_misc.name = "misc"; 2197 class_register(&linux_class_misc); 2198 INIT_LIST_HEAD(&pci_drivers); 2199 INIT_LIST_HEAD(&pci_devices); 2200 spin_lock_init(&pci_lock); 2201 mtx_init(&vmmaplock, "IO Map lock", NULL, MTX_DEF); 2202 for (i = 0; i < VMMAP_HASH_SIZE; i++) 2203 LIST_INIT(&vmmaphead[i]); 2204 } 2205 SYSINIT(linux_compat, SI_SUB_DRIVERS, SI_ORDER_SECOND, linux_compat_init, NULL); 2206 2207 static void 2208 linux_compat_uninit(void *arg) 2209 { 2210 linux_kobject_kfree_name(&linux_class_root); 2211 linux_kobject_kfree_name(&linux_root_device.kobj); 2212 linux_kobject_kfree_name(&linux_class_misc.kobj); 2213 2214 mtx_destroy(&vmmaplock); 2215 spin_lock_destroy(&pci_lock); 2216 rw_destroy(&linux_vma_lock); 2217 } 2218 SYSUNINIT(linux_compat, SI_SUB_DRIVERS, SI_ORDER_SECOND, linux_compat_uninit, NULL); 2219 2220 /* 2221 * NOTE: Linux frequently uses "unsigned long" for pointer to integer 2222 * conversion and vice versa, where in FreeBSD "uintptr_t" would be 2223 * used. Assert these types have the same size, else some parts of the 2224 * LinuxKPI may not work like expected: 2225 */ 2226 CTASSERT(sizeof(unsigned long) == sizeof(uintptr_t)); 2227