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 ((error = devfs_get_cdevpriv((void **)&filp)) != 0) 749 return (error); 750 filp->f_flags = file->f_flag; 751 devfs_clear_cdevpriv(); 752 753 return (0); 754 } 755 756 #define LINUX_IOCTL_MIN_PTR 0x10000UL 757 #define LINUX_IOCTL_MAX_PTR (LINUX_IOCTL_MIN_PTR + IOCPARM_MAX) 758 759 static inline int 760 linux_remap_address(void **uaddr, size_t len) 761 { 762 uintptr_t uaddr_val = (uintptr_t)(*uaddr); 763 764 if (unlikely(uaddr_val >= LINUX_IOCTL_MIN_PTR && 765 uaddr_val < LINUX_IOCTL_MAX_PTR)) { 766 struct task_struct *pts = current; 767 if (pts == NULL) { 768 *uaddr = NULL; 769 return (1); 770 } 771 772 /* compute data offset */ 773 uaddr_val -= LINUX_IOCTL_MIN_PTR; 774 775 /* check that length is within bounds */ 776 if ((len > IOCPARM_MAX) || 777 (uaddr_val + len) > pts->bsd_ioctl_len) { 778 *uaddr = NULL; 779 return (1); 780 } 781 782 /* re-add kernel buffer address */ 783 uaddr_val += (uintptr_t)pts->bsd_ioctl_data; 784 785 /* update address location */ 786 *uaddr = (void *)uaddr_val; 787 return (1); 788 } 789 return (0); 790 } 791 792 int 793 linux_copyin(const void *uaddr, void *kaddr, size_t len) 794 { 795 if (linux_remap_address(__DECONST(void **, &uaddr), len)) { 796 if (uaddr == NULL) 797 return (-EFAULT); 798 memcpy(kaddr, uaddr, len); 799 return (0); 800 } 801 return (-copyin(uaddr, kaddr, len)); 802 } 803 804 int 805 linux_copyout(const void *kaddr, void *uaddr, size_t len) 806 { 807 if (linux_remap_address(&uaddr, len)) { 808 if (uaddr == NULL) 809 return (-EFAULT); 810 memcpy(uaddr, kaddr, len); 811 return (0); 812 } 813 return (-copyout(kaddr, uaddr, len)); 814 } 815 816 size_t 817 linux_clear_user(void *_uaddr, size_t _len) 818 { 819 uint8_t *uaddr = _uaddr; 820 size_t len = _len; 821 822 /* make sure uaddr is aligned before going into the fast loop */ 823 while (((uintptr_t)uaddr & 7) != 0 && len > 7) { 824 if (subyte(uaddr, 0)) 825 return (_len); 826 uaddr++; 827 len--; 828 } 829 830 /* zero 8 bytes at a time */ 831 while (len > 7) { 832 #ifdef __LP64__ 833 if (suword64(uaddr, 0)) 834 return (_len); 835 #else 836 if (suword32(uaddr, 0)) 837 return (_len); 838 if (suword32(uaddr + 4, 0)) 839 return (_len); 840 #endif 841 uaddr += 8; 842 len -= 8; 843 } 844 845 /* zero fill end, if any */ 846 while (len > 0) { 847 if (subyte(uaddr, 0)) 848 return (_len); 849 uaddr++; 850 len--; 851 } 852 return (0); 853 } 854 855 int 856 linux_access_ok(int rw, const void *uaddr, size_t len) 857 { 858 uintptr_t saddr; 859 uintptr_t eaddr; 860 861 /* get start and end address */ 862 saddr = (uintptr_t)uaddr; 863 eaddr = (uintptr_t)uaddr + len; 864 865 /* verify addresses are valid for userspace */ 866 return ((saddr == eaddr) || 867 (eaddr > saddr && eaddr <= VM_MAXUSER_ADDRESS)); 868 } 869 870 static int 871 linux_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, 872 struct thread *td) 873 { 874 struct linux_file *filp; 875 struct file *file; 876 unsigned size; 877 int error; 878 879 file = td->td_fpop; 880 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0) 881 return (error); 882 filp->f_flags = file->f_flag; 883 884 /* the LinuxKPI supports blocking and non-blocking I/O */ 885 if (cmd == FIONBIO || cmd == FIOASYNC) 886 return (0); 887 888 linux_set_current(td); 889 size = IOCPARM_LEN(cmd); 890 /* refer to logic in sys_ioctl() */ 891 if (size > 0) { 892 /* 893 * Setup hint for linux_copyin() and linux_copyout(). 894 * 895 * Background: Linux code expects a user-space address 896 * while FreeBSD supplies a kernel-space address. 897 */ 898 current->bsd_ioctl_data = data; 899 current->bsd_ioctl_len = size; 900 data = (void *)LINUX_IOCTL_MIN_PTR; 901 } else { 902 /* fetch user-space pointer */ 903 data = *(void **)data; 904 } 905 #if defined(__amd64__) 906 if (td->td_proc->p_elf_machine == EM_386) { 907 /* try the compat IOCTL handler first */ 908 if (filp->f_op->compat_ioctl != NULL) 909 error = -filp->f_op->compat_ioctl(filp, cmd, (u_long)data); 910 else 911 error = ENOTTY; 912 913 /* fallback to the regular IOCTL handler, if any */ 914 if (error == ENOTTY && filp->f_op->unlocked_ioctl != NULL) 915 error = -filp->f_op->unlocked_ioctl(filp, cmd, (u_long)data); 916 } else 917 #endif 918 if (filp->f_op->unlocked_ioctl != NULL) 919 error = -filp->f_op->unlocked_ioctl(filp, cmd, (u_long)data); 920 else 921 error = ENOTTY; 922 if (size > 0) { 923 current->bsd_ioctl_data = NULL; 924 current->bsd_ioctl_len = 0; 925 } 926 927 if (error == EWOULDBLOCK) { 928 /* update kqfilter status, if any */ 929 linux_dev_kqfilter_poll(filp, 930 LINUX_KQ_FLAG_HAS_READ | LINUX_KQ_FLAG_HAS_WRITE); 931 } else if (error == ERESTARTSYS) 932 error = ERESTART; 933 return (error); 934 } 935 936 static int 937 linux_dev_read(struct cdev *dev, struct uio *uio, int ioflag) 938 { 939 struct linux_file *filp; 940 struct thread *td; 941 struct file *file; 942 ssize_t bytes; 943 int error; 944 945 td = curthread; 946 file = td->td_fpop; 947 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0) 948 return (error); 949 filp->f_flags = file->f_flag; 950 /* XXX no support for I/O vectors currently */ 951 if (uio->uio_iovcnt != 1) 952 return (EOPNOTSUPP); 953 linux_set_current(td); 954 if (filp->f_op->read) { 955 bytes = filp->f_op->read(filp, uio->uio_iov->iov_base, 956 uio->uio_iov->iov_len, &uio->uio_offset); 957 if (bytes >= 0) { 958 uio->uio_iov->iov_base = 959 ((uint8_t *)uio->uio_iov->iov_base) + bytes; 960 uio->uio_iov->iov_len -= bytes; 961 uio->uio_resid -= bytes; 962 } else { 963 error = -bytes; 964 if (error == ERESTARTSYS) 965 error = ERESTART; 966 } 967 } else 968 error = ENXIO; 969 970 /* update kqfilter status, if any */ 971 linux_dev_kqfilter_poll(filp, LINUX_KQ_FLAG_HAS_READ); 972 973 return (error); 974 } 975 976 static int 977 linux_dev_write(struct cdev *dev, struct uio *uio, int ioflag) 978 { 979 struct linux_file *filp; 980 struct thread *td; 981 struct file *file; 982 ssize_t bytes; 983 int error; 984 985 td = curthread; 986 file = td->td_fpop; 987 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0) 988 return (error); 989 filp->f_flags = file->f_flag; 990 /* XXX no support for I/O vectors currently */ 991 if (uio->uio_iovcnt != 1) 992 return (EOPNOTSUPP); 993 linux_set_current(td); 994 if (filp->f_op->write) { 995 bytes = filp->f_op->write(filp, uio->uio_iov->iov_base, 996 uio->uio_iov->iov_len, &uio->uio_offset); 997 if (bytes >= 0) { 998 uio->uio_iov->iov_base = 999 ((uint8_t *)uio->uio_iov->iov_base) + bytes; 1000 uio->uio_iov->iov_len -= bytes; 1001 uio->uio_resid -= bytes; 1002 } else { 1003 error = -bytes; 1004 if (error == ERESTARTSYS) 1005 error = ERESTART; 1006 } 1007 } else 1008 error = ENXIO; 1009 1010 /* update kqfilter status, if any */ 1011 linux_dev_kqfilter_poll(filp, LINUX_KQ_FLAG_HAS_WRITE); 1012 1013 return (error); 1014 } 1015 1016 #define LINUX_POLL_TABLE_NORMAL ((poll_table *)1) 1017 1018 static int 1019 linux_dev_poll(struct cdev *dev, int events, struct thread *td) 1020 { 1021 struct linux_file *filp; 1022 struct file *file; 1023 int revents; 1024 1025 if (devfs_get_cdevpriv((void **)&filp) != 0) 1026 goto error; 1027 1028 file = td->td_fpop; 1029 filp->f_flags = file->f_flag; 1030 linux_set_current(td); 1031 if (filp->f_op->poll != NULL) 1032 revents = filp->f_op->poll(filp, LINUX_POLL_TABLE_NORMAL) & events; 1033 else 1034 revents = 0; 1035 1036 return (revents); 1037 error: 1038 return (events & (POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM)); 1039 } 1040 1041 /* 1042 * This function atomically updates the poll wakeup state and returns 1043 * the previous state at the time of update. 1044 */ 1045 static uint8_t 1046 linux_poll_wakeup_state(atomic_t *v, const uint8_t *pstate) 1047 { 1048 int c, old; 1049 1050 c = v->counter; 1051 1052 while ((old = atomic_cmpxchg(v, c, pstate[c])) != c) 1053 c = old; 1054 1055 return (c); 1056 } 1057 1058 1059 static int 1060 linux_poll_wakeup_callback(wait_queue_t *wq, unsigned int wq_state, int flags, void *key) 1061 { 1062 static const uint8_t state[LINUX_FWQ_STATE_MAX] = { 1063 [LINUX_FWQ_STATE_INIT] = LINUX_FWQ_STATE_INIT, /* NOP */ 1064 [LINUX_FWQ_STATE_NOT_READY] = LINUX_FWQ_STATE_NOT_READY, /* NOP */ 1065 [LINUX_FWQ_STATE_QUEUED] = LINUX_FWQ_STATE_READY, 1066 [LINUX_FWQ_STATE_READY] = LINUX_FWQ_STATE_READY, /* NOP */ 1067 }; 1068 struct linux_file *filp = container_of(wq, struct linux_file, f_wait_queue.wq); 1069 1070 switch (linux_poll_wakeup_state(&filp->f_wait_queue.state, state)) { 1071 case LINUX_FWQ_STATE_QUEUED: 1072 linux_poll_wakeup(filp); 1073 return (1); 1074 default: 1075 return (0); 1076 } 1077 } 1078 1079 void 1080 linux_poll_wait(struct linux_file *filp, wait_queue_head_t *wqh, poll_table *p) 1081 { 1082 static const uint8_t state[LINUX_FWQ_STATE_MAX] = { 1083 [LINUX_FWQ_STATE_INIT] = LINUX_FWQ_STATE_NOT_READY, 1084 [LINUX_FWQ_STATE_NOT_READY] = LINUX_FWQ_STATE_NOT_READY, /* NOP */ 1085 [LINUX_FWQ_STATE_QUEUED] = LINUX_FWQ_STATE_QUEUED, /* NOP */ 1086 [LINUX_FWQ_STATE_READY] = LINUX_FWQ_STATE_QUEUED, 1087 }; 1088 1089 /* check if we are called inside the select system call */ 1090 if (p == LINUX_POLL_TABLE_NORMAL) 1091 selrecord(curthread, &filp->f_selinfo); 1092 1093 switch (linux_poll_wakeup_state(&filp->f_wait_queue.state, state)) { 1094 case LINUX_FWQ_STATE_INIT: 1095 /* NOTE: file handles can only belong to one wait-queue */ 1096 filp->f_wait_queue.wqh = wqh; 1097 filp->f_wait_queue.wq.func = &linux_poll_wakeup_callback; 1098 add_wait_queue(wqh, &filp->f_wait_queue.wq); 1099 atomic_set(&filp->f_wait_queue.state, LINUX_FWQ_STATE_QUEUED); 1100 break; 1101 default: 1102 break; 1103 } 1104 } 1105 1106 static void 1107 linux_poll_wait_dequeue(struct linux_file *filp) 1108 { 1109 static const uint8_t state[LINUX_FWQ_STATE_MAX] = { 1110 [LINUX_FWQ_STATE_INIT] = LINUX_FWQ_STATE_INIT, /* NOP */ 1111 [LINUX_FWQ_STATE_NOT_READY] = LINUX_FWQ_STATE_INIT, 1112 [LINUX_FWQ_STATE_QUEUED] = LINUX_FWQ_STATE_INIT, 1113 [LINUX_FWQ_STATE_READY] = LINUX_FWQ_STATE_INIT, 1114 }; 1115 1116 seldrain(&filp->f_selinfo); 1117 1118 switch (linux_poll_wakeup_state(&filp->f_wait_queue.state, state)) { 1119 case LINUX_FWQ_STATE_NOT_READY: 1120 case LINUX_FWQ_STATE_QUEUED: 1121 case LINUX_FWQ_STATE_READY: 1122 remove_wait_queue(filp->f_wait_queue.wqh, &filp->f_wait_queue.wq); 1123 break; 1124 default: 1125 break; 1126 } 1127 } 1128 1129 void 1130 linux_poll_wakeup(struct linux_file *filp) 1131 { 1132 /* this function should be NULL-safe */ 1133 if (filp == NULL) 1134 return; 1135 1136 selwakeup(&filp->f_selinfo); 1137 1138 spin_lock(&filp->f_kqlock); 1139 filp->f_kqflags |= LINUX_KQ_FLAG_NEED_READ | 1140 LINUX_KQ_FLAG_NEED_WRITE; 1141 1142 /* make sure the "knote" gets woken up */ 1143 KNOTE_LOCKED(&filp->f_selinfo.si_note, 1); 1144 spin_unlock(&filp->f_kqlock); 1145 } 1146 1147 static void 1148 linux_dev_kqfilter_detach(struct knote *kn) 1149 { 1150 struct linux_file *filp = kn->kn_hook; 1151 1152 spin_lock(&filp->f_kqlock); 1153 knlist_remove(&filp->f_selinfo.si_note, kn, 1); 1154 spin_unlock(&filp->f_kqlock); 1155 } 1156 1157 static int 1158 linux_dev_kqfilter_read_event(struct knote *kn, long hint) 1159 { 1160 struct linux_file *filp = kn->kn_hook; 1161 1162 mtx_assert(&filp->f_kqlock.m, MA_OWNED); 1163 1164 return ((filp->f_kqflags & LINUX_KQ_FLAG_NEED_READ) ? 1 : 0); 1165 } 1166 1167 static int 1168 linux_dev_kqfilter_write_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_WRITE) ? 1 : 0); 1175 } 1176 1177 static struct filterops linux_dev_kqfiltops_read = { 1178 .f_isfd = 1, 1179 .f_detach = linux_dev_kqfilter_detach, 1180 .f_event = linux_dev_kqfilter_read_event, 1181 }; 1182 1183 static struct filterops linux_dev_kqfiltops_write = { 1184 .f_isfd = 1, 1185 .f_detach = linux_dev_kqfilter_detach, 1186 .f_event = linux_dev_kqfilter_write_event, 1187 }; 1188 1189 static void 1190 linux_dev_kqfilter_poll(struct linux_file *filp, int kqflags) 1191 { 1192 int temp; 1193 1194 if (filp->f_kqflags & kqflags) { 1195 /* get the latest polling state */ 1196 temp = filp->f_op->poll(filp, NULL); 1197 1198 spin_lock(&filp->f_kqlock); 1199 /* clear kqflags */ 1200 filp->f_kqflags &= ~(LINUX_KQ_FLAG_NEED_READ | 1201 LINUX_KQ_FLAG_NEED_WRITE); 1202 /* update kqflags */ 1203 if (temp & (POLLIN | POLLOUT)) { 1204 if (temp & POLLIN) 1205 filp->f_kqflags |= LINUX_KQ_FLAG_NEED_READ; 1206 if (temp & POLLOUT) 1207 filp->f_kqflags |= LINUX_KQ_FLAG_NEED_WRITE; 1208 1209 /* make sure the "knote" gets woken up */ 1210 KNOTE_LOCKED(&filp->f_selinfo.si_note, 0); 1211 } 1212 spin_unlock(&filp->f_kqlock); 1213 } 1214 } 1215 1216 static int 1217 linux_dev_kqfilter(struct cdev *dev, struct knote *kn) 1218 { 1219 struct linux_file *filp; 1220 struct file *file; 1221 struct thread *td; 1222 int error; 1223 1224 td = curthread; 1225 file = td->td_fpop; 1226 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0) 1227 return (error); 1228 filp->f_flags = file->f_flag; 1229 if (filp->f_op->poll == NULL) 1230 return (EINVAL); 1231 1232 spin_lock(&filp->f_kqlock); 1233 switch (kn->kn_filter) { 1234 case EVFILT_READ: 1235 filp->f_kqflags |= LINUX_KQ_FLAG_HAS_READ; 1236 kn->kn_fop = &linux_dev_kqfiltops_read; 1237 kn->kn_hook = filp; 1238 knlist_add(&filp->f_selinfo.si_note, kn, 1); 1239 break; 1240 case EVFILT_WRITE: 1241 filp->f_kqflags |= LINUX_KQ_FLAG_HAS_WRITE; 1242 kn->kn_fop = &linux_dev_kqfiltops_write; 1243 kn->kn_hook = filp; 1244 knlist_add(&filp->f_selinfo.si_note, kn, 1); 1245 break; 1246 default: 1247 error = EINVAL; 1248 break; 1249 } 1250 spin_unlock(&filp->f_kqlock); 1251 1252 if (error == 0) { 1253 linux_set_current(td); 1254 1255 /* update kqfilter status, if any */ 1256 linux_dev_kqfilter_poll(filp, 1257 LINUX_KQ_FLAG_HAS_READ | LINUX_KQ_FLAG_HAS_WRITE); 1258 } 1259 return (error); 1260 } 1261 1262 static int 1263 linux_dev_mmap_single(struct cdev *dev, vm_ooffset_t *offset, 1264 vm_size_t size, struct vm_object **object, int nprot) 1265 { 1266 struct vm_area_struct *vmap; 1267 struct mm_struct *mm; 1268 struct linux_file *filp; 1269 struct thread *td; 1270 struct file *file; 1271 vm_memattr_t attr; 1272 int error; 1273 1274 td = curthread; 1275 file = td->td_fpop; 1276 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0) 1277 return (error); 1278 filp->f_flags = file->f_flag; 1279 1280 if (filp->f_op->mmap == NULL) 1281 return (ENODEV); 1282 1283 linux_set_current(td); 1284 1285 /* 1286 * The same VM object might be shared by multiple processes 1287 * and the mm_struct is usually freed when a process exits. 1288 * 1289 * The atomic reference below makes sure the mm_struct is 1290 * available as long as the vmap is in the linux_vma_head. 1291 */ 1292 mm = current->mm; 1293 if (atomic_inc_not_zero(&mm->mm_users) == 0) 1294 return (EINVAL); 1295 1296 vmap = kzalloc(sizeof(*vmap), GFP_KERNEL); 1297 vmap->vm_start = 0; 1298 vmap->vm_end = size; 1299 vmap->vm_pgoff = *offset / PAGE_SIZE; 1300 vmap->vm_pfn = 0; 1301 vmap->vm_flags = vmap->vm_page_prot = (nprot & VM_PROT_ALL); 1302 vmap->vm_ops = NULL; 1303 vmap->vm_file = get_file(filp); 1304 vmap->vm_mm = mm; 1305 1306 if (unlikely(down_write_killable(&vmap->vm_mm->mmap_sem))) { 1307 error = EINTR; 1308 } else { 1309 error = -filp->f_op->mmap(filp, vmap); 1310 up_write(&vmap->vm_mm->mmap_sem); 1311 } 1312 1313 if (error != 0) { 1314 linux_cdev_handle_free(vmap); 1315 return (error); 1316 } 1317 1318 attr = pgprot2cachemode(vmap->vm_page_prot); 1319 1320 if (vmap->vm_ops != NULL) { 1321 void *vm_private_data; 1322 1323 if (vmap->vm_ops->open == NULL || 1324 vmap->vm_ops->close == NULL || 1325 vmap->vm_private_data == NULL) { 1326 linux_cdev_handle_free(vmap); 1327 return (EINVAL); 1328 } 1329 1330 vm_private_data = vmap->vm_private_data; 1331 1332 vmap = linux_cdev_handle_insert(vm_private_data, vmap); 1333 1334 if (vmap->vm_ops->fault == NULL) { 1335 *object = cdev_pager_allocate(vm_private_data, OBJT_DEVICE, 1336 &linux_cdev_pager_ops[1], size, nprot, *offset, 1337 curthread->td_ucred); 1338 } else { 1339 *object = cdev_pager_allocate(vm_private_data, OBJT_MGTDEVICE, 1340 &linux_cdev_pager_ops[0], size, nprot, *offset, 1341 curthread->td_ucred); 1342 } 1343 1344 if (*object == NULL) { 1345 linux_cdev_handle_remove(vmap); 1346 linux_cdev_handle_free(vmap); 1347 return (EINVAL); 1348 } 1349 } else { 1350 struct sglist *sg; 1351 1352 sg = sglist_alloc(1, M_WAITOK); 1353 sglist_append_phys(sg, 1354 (vm_paddr_t)vmap->vm_pfn << PAGE_SHIFT, vmap->vm_len); 1355 1356 *object = vm_pager_allocate(OBJT_SG, sg, vmap->vm_len, 1357 nprot, 0, curthread->td_ucred); 1358 1359 linux_cdev_handle_free(vmap); 1360 1361 if (*object == NULL) { 1362 sglist_free(sg); 1363 return (EINVAL); 1364 } 1365 } 1366 1367 if (attr != VM_MEMATTR_DEFAULT) { 1368 VM_OBJECT_WLOCK(*object); 1369 vm_object_set_memattr(*object, attr); 1370 VM_OBJECT_WUNLOCK(*object); 1371 } 1372 *offset = 0; 1373 return (0); 1374 } 1375 1376 struct cdevsw linuxcdevsw = { 1377 .d_version = D_VERSION, 1378 .d_flags = D_TRACKCLOSE, 1379 .d_open = linux_dev_open, 1380 .d_close = linux_dev_close, 1381 .d_read = linux_dev_read, 1382 .d_write = linux_dev_write, 1383 .d_ioctl = linux_dev_ioctl, 1384 .d_mmap_single = linux_dev_mmap_single, 1385 .d_poll = linux_dev_poll, 1386 .d_kqfilter = linux_dev_kqfilter, 1387 .d_name = "lkpidev", 1388 }; 1389 1390 static int 1391 linux_file_read(struct file *file, struct uio *uio, struct ucred *active_cred, 1392 int flags, struct thread *td) 1393 { 1394 struct linux_file *filp; 1395 ssize_t bytes; 1396 int error; 1397 1398 error = 0; 1399 filp = (struct linux_file *)file->f_data; 1400 filp->f_flags = file->f_flag; 1401 /* XXX no support for I/O vectors currently */ 1402 if (uio->uio_iovcnt != 1) 1403 return (EOPNOTSUPP); 1404 linux_set_current(td); 1405 if (filp->f_op->read) { 1406 bytes = filp->f_op->read(filp, uio->uio_iov->iov_base, 1407 uio->uio_iov->iov_len, &uio->uio_offset); 1408 if (bytes >= 0) { 1409 uio->uio_iov->iov_base = 1410 ((uint8_t *)uio->uio_iov->iov_base) + bytes; 1411 uio->uio_iov->iov_len -= bytes; 1412 uio->uio_resid -= bytes; 1413 } else 1414 error = -bytes; 1415 } else 1416 error = ENXIO; 1417 1418 return (error); 1419 } 1420 1421 static int 1422 linux_file_poll(struct file *file, int events, struct ucred *active_cred, 1423 struct thread *td) 1424 { 1425 struct linux_file *filp; 1426 int revents; 1427 1428 filp = (struct linux_file *)file->f_data; 1429 filp->f_flags = file->f_flag; 1430 linux_set_current(td); 1431 if (filp->f_op->poll != NULL) 1432 revents = filp->f_op->poll(filp, LINUX_POLL_TABLE_NORMAL) & events; 1433 else 1434 revents = 0; 1435 1436 return (revents); 1437 } 1438 1439 static int 1440 linux_file_close(struct file *file, struct thread *td) 1441 { 1442 struct linux_file *filp; 1443 int error; 1444 1445 filp = (struct linux_file *)file->f_data; 1446 filp->f_flags = file->f_flag; 1447 linux_set_current(td); 1448 linux_poll_wait_dequeue(filp); 1449 error = -filp->f_op->release(NULL, filp); 1450 funsetown(&filp->f_sigio); 1451 kfree(filp); 1452 1453 return (error); 1454 } 1455 1456 static int 1457 linux_file_ioctl(struct file *fp, u_long cmd, void *data, struct ucred *cred, 1458 struct thread *td) 1459 { 1460 struct linux_file *filp; 1461 int error; 1462 1463 filp = (struct linux_file *)fp->f_data; 1464 filp->f_flags = fp->f_flag; 1465 error = 0; 1466 1467 linux_set_current(td); 1468 switch (cmd) { 1469 case FIONBIO: 1470 break; 1471 case FIOASYNC: 1472 if (filp->f_op->fasync == NULL) 1473 break; 1474 error = filp->f_op->fasync(0, filp, fp->f_flag & FASYNC); 1475 break; 1476 case FIOSETOWN: 1477 error = fsetown(*(int *)data, &filp->f_sigio); 1478 if (error == 0) 1479 error = filp->f_op->fasync(0, filp, 1480 fp->f_flag & FASYNC); 1481 break; 1482 case FIOGETOWN: 1483 *(int *)data = fgetown(&filp->f_sigio); 1484 break; 1485 default: 1486 error = ENOTTY; 1487 break; 1488 } 1489 return (error); 1490 } 1491 1492 static int 1493 linux_file_stat(struct file *fp, struct stat *sb, struct ucred *active_cred, 1494 struct thread *td) 1495 { 1496 1497 return (EOPNOTSUPP); 1498 } 1499 1500 static int 1501 linux_file_fill_kinfo(struct file *fp, struct kinfo_file *kif, 1502 struct filedesc *fdp) 1503 { 1504 1505 return (0); 1506 } 1507 1508 unsigned int 1509 linux_iminor(struct inode *inode) 1510 { 1511 struct linux_cdev *ldev; 1512 1513 if (inode == NULL || inode->v_rdev == NULL || 1514 inode->v_rdev->si_devsw != &linuxcdevsw) 1515 return (-1U); 1516 ldev = inode->v_rdev->si_drv1; 1517 if (ldev == NULL) 1518 return (-1U); 1519 1520 return (minor(ldev->dev)); 1521 } 1522 1523 struct fileops linuxfileops = { 1524 .fo_read = linux_file_read, 1525 .fo_write = invfo_rdwr, 1526 .fo_truncate = invfo_truncate, 1527 .fo_kqfilter = invfo_kqfilter, 1528 .fo_stat = linux_file_stat, 1529 .fo_fill_kinfo = linux_file_fill_kinfo, 1530 .fo_poll = linux_file_poll, 1531 .fo_close = linux_file_close, 1532 .fo_ioctl = linux_file_ioctl, 1533 .fo_chmod = invfo_chmod, 1534 .fo_chown = invfo_chown, 1535 .fo_sendfile = invfo_sendfile, 1536 }; 1537 1538 /* 1539 * Hash of vmmap addresses. This is infrequently accessed and does not 1540 * need to be particularly large. This is done because we must store the 1541 * caller's idea of the map size to properly unmap. 1542 */ 1543 struct vmmap { 1544 LIST_ENTRY(vmmap) vm_next; 1545 void *vm_addr; 1546 unsigned long vm_size; 1547 }; 1548 1549 struct vmmaphd { 1550 struct vmmap *lh_first; 1551 }; 1552 #define VMMAP_HASH_SIZE 64 1553 #define VMMAP_HASH_MASK (VMMAP_HASH_SIZE - 1) 1554 #define VM_HASH(addr) ((uintptr_t)(addr) >> PAGE_SHIFT) & VMMAP_HASH_MASK 1555 static struct vmmaphd vmmaphead[VMMAP_HASH_SIZE]; 1556 static struct mtx vmmaplock; 1557 1558 static void 1559 vmmap_add(void *addr, unsigned long size) 1560 { 1561 struct vmmap *vmmap; 1562 1563 vmmap = kmalloc(sizeof(*vmmap), GFP_KERNEL); 1564 mtx_lock(&vmmaplock); 1565 vmmap->vm_size = size; 1566 vmmap->vm_addr = addr; 1567 LIST_INSERT_HEAD(&vmmaphead[VM_HASH(addr)], vmmap, vm_next); 1568 mtx_unlock(&vmmaplock); 1569 } 1570 1571 static struct vmmap * 1572 vmmap_remove(void *addr) 1573 { 1574 struct vmmap *vmmap; 1575 1576 mtx_lock(&vmmaplock); 1577 LIST_FOREACH(vmmap, &vmmaphead[VM_HASH(addr)], vm_next) 1578 if (vmmap->vm_addr == addr) 1579 break; 1580 if (vmmap) 1581 LIST_REMOVE(vmmap, vm_next); 1582 mtx_unlock(&vmmaplock); 1583 1584 return (vmmap); 1585 } 1586 1587 #if defined(__i386__) || defined(__amd64__) || defined(__powerpc__) 1588 void * 1589 _ioremap_attr(vm_paddr_t phys_addr, unsigned long size, int attr) 1590 { 1591 void *addr; 1592 1593 addr = pmap_mapdev_attr(phys_addr, size, attr); 1594 if (addr == NULL) 1595 return (NULL); 1596 vmmap_add(addr, size); 1597 1598 return (addr); 1599 } 1600 #endif 1601 1602 void 1603 iounmap(void *addr) 1604 { 1605 struct vmmap *vmmap; 1606 1607 vmmap = vmmap_remove(addr); 1608 if (vmmap == NULL) 1609 return; 1610 #if defined(__i386__) || defined(__amd64__) || defined(__powerpc__) 1611 pmap_unmapdev((vm_offset_t)addr, vmmap->vm_size); 1612 #endif 1613 kfree(vmmap); 1614 } 1615 1616 1617 void * 1618 vmap(struct page **pages, unsigned int count, unsigned long flags, int prot) 1619 { 1620 vm_offset_t off; 1621 size_t size; 1622 1623 size = count * PAGE_SIZE; 1624 off = kva_alloc(size); 1625 if (off == 0) 1626 return (NULL); 1627 vmmap_add((void *)off, size); 1628 pmap_qenter(off, pages, count); 1629 1630 return ((void *)off); 1631 } 1632 1633 void 1634 vunmap(void *addr) 1635 { 1636 struct vmmap *vmmap; 1637 1638 vmmap = vmmap_remove(addr); 1639 if (vmmap == NULL) 1640 return; 1641 pmap_qremove((vm_offset_t)addr, vmmap->vm_size / PAGE_SIZE); 1642 kva_free((vm_offset_t)addr, vmmap->vm_size); 1643 kfree(vmmap); 1644 } 1645 1646 char * 1647 kvasprintf(gfp_t gfp, const char *fmt, va_list ap) 1648 { 1649 unsigned int len; 1650 char *p; 1651 va_list aq; 1652 1653 va_copy(aq, ap); 1654 len = vsnprintf(NULL, 0, fmt, aq); 1655 va_end(aq); 1656 1657 p = kmalloc(len + 1, gfp); 1658 if (p != NULL) 1659 vsnprintf(p, len + 1, fmt, ap); 1660 1661 return (p); 1662 } 1663 1664 char * 1665 kasprintf(gfp_t gfp, const char *fmt, ...) 1666 { 1667 va_list ap; 1668 char *p; 1669 1670 va_start(ap, fmt); 1671 p = kvasprintf(gfp, fmt, ap); 1672 va_end(ap); 1673 1674 return (p); 1675 } 1676 1677 static void 1678 linux_timer_callback_wrapper(void *context) 1679 { 1680 struct timer_list *timer; 1681 1682 linux_set_current(curthread); 1683 1684 timer = context; 1685 timer->function(timer->data); 1686 } 1687 1688 void 1689 mod_timer(struct timer_list *timer, int expires) 1690 { 1691 1692 timer->expires = expires; 1693 callout_reset(&timer->timer_callout, 1694 linux_timer_jiffies_until(expires), 1695 &linux_timer_callback_wrapper, timer); 1696 } 1697 1698 void 1699 add_timer(struct timer_list *timer) 1700 { 1701 1702 callout_reset(&timer->timer_callout, 1703 linux_timer_jiffies_until(timer->expires), 1704 &linux_timer_callback_wrapper, timer); 1705 } 1706 1707 void 1708 add_timer_on(struct timer_list *timer, int cpu) 1709 { 1710 1711 callout_reset_on(&timer->timer_callout, 1712 linux_timer_jiffies_until(timer->expires), 1713 &linux_timer_callback_wrapper, timer, cpu); 1714 } 1715 1716 static void 1717 linux_timer_init(void *arg) 1718 { 1719 1720 /* 1721 * Compute an internal HZ value which can divide 2**32 to 1722 * avoid timer rounding problems when the tick value wraps 1723 * around 2**32: 1724 */ 1725 linux_timer_hz_mask = 1; 1726 while (linux_timer_hz_mask < (unsigned long)hz) 1727 linux_timer_hz_mask *= 2; 1728 linux_timer_hz_mask--; 1729 } 1730 SYSINIT(linux_timer, SI_SUB_DRIVERS, SI_ORDER_FIRST, linux_timer_init, NULL); 1731 1732 void 1733 linux_complete_common(struct completion *c, int all) 1734 { 1735 int wakeup_swapper; 1736 1737 sleepq_lock(c); 1738 c->done++; 1739 if (all) 1740 wakeup_swapper = sleepq_broadcast(c, SLEEPQ_SLEEP, 0, 0); 1741 else 1742 wakeup_swapper = sleepq_signal(c, SLEEPQ_SLEEP, 0, 0); 1743 sleepq_release(c); 1744 if (wakeup_swapper) 1745 kick_proc0(); 1746 } 1747 1748 /* 1749 * Indefinite wait for done != 0 with or without signals. 1750 */ 1751 int 1752 linux_wait_for_common(struct completion *c, int flags) 1753 { 1754 int error; 1755 1756 if (SCHEDULER_STOPPED()) 1757 return (0); 1758 1759 DROP_GIANT(); 1760 1761 if (flags != 0) 1762 flags = SLEEPQ_INTERRUPTIBLE | SLEEPQ_SLEEP; 1763 else 1764 flags = SLEEPQ_SLEEP; 1765 error = 0; 1766 for (;;) { 1767 sleepq_lock(c); 1768 if (c->done) 1769 break; 1770 sleepq_add(c, NULL, "completion", flags, 0); 1771 if (flags & SLEEPQ_INTERRUPTIBLE) { 1772 if (sleepq_wait_sig(c, 0) != 0) { 1773 error = -ERESTARTSYS; 1774 goto intr; 1775 } 1776 } else 1777 sleepq_wait(c, 0); 1778 } 1779 c->done--; 1780 sleepq_release(c); 1781 1782 intr: 1783 PICKUP_GIANT(); 1784 1785 return (error); 1786 } 1787 1788 /* 1789 * Time limited wait for done != 0 with or without signals. 1790 */ 1791 int 1792 linux_wait_for_timeout_common(struct completion *c, int timeout, int flags) 1793 { 1794 int end = jiffies + timeout; 1795 int error; 1796 int ret; 1797 1798 if (SCHEDULER_STOPPED()) 1799 return (0); 1800 1801 DROP_GIANT(); 1802 1803 if (flags != 0) 1804 flags = SLEEPQ_INTERRUPTIBLE | SLEEPQ_SLEEP; 1805 else 1806 flags = SLEEPQ_SLEEP; 1807 1808 error = 0; 1809 ret = 0; 1810 for (;;) { 1811 sleepq_lock(c); 1812 if (c->done) 1813 break; 1814 sleepq_add(c, NULL, "completion", flags, 0); 1815 sleepq_set_timeout(c, linux_timer_jiffies_until(end)); 1816 if (flags & SLEEPQ_INTERRUPTIBLE) 1817 ret = sleepq_timedwait_sig(c, 0); 1818 else 1819 ret = sleepq_timedwait(c, 0); 1820 if (ret != 0) { 1821 /* check for timeout or signal */ 1822 if (ret == EWOULDBLOCK) 1823 error = 0; 1824 else 1825 error = -ERESTARTSYS; 1826 goto intr; 1827 } 1828 } 1829 c->done--; 1830 sleepq_release(c); 1831 1832 intr: 1833 PICKUP_GIANT(); 1834 1835 /* return how many jiffies are left */ 1836 return (ret != 0 ? error : linux_timer_jiffies_until(end)); 1837 } 1838 1839 int 1840 linux_try_wait_for_completion(struct completion *c) 1841 { 1842 int isdone; 1843 1844 isdone = 1; 1845 sleepq_lock(c); 1846 if (c->done) 1847 c->done--; 1848 else 1849 isdone = 0; 1850 sleepq_release(c); 1851 return (isdone); 1852 } 1853 1854 int 1855 linux_completion_done(struct completion *c) 1856 { 1857 int isdone; 1858 1859 isdone = 1; 1860 sleepq_lock(c); 1861 if (c->done == 0) 1862 isdone = 0; 1863 sleepq_release(c); 1864 return (isdone); 1865 } 1866 1867 static void 1868 linux_cdev_release(struct kobject *kobj) 1869 { 1870 struct linux_cdev *cdev; 1871 struct kobject *parent; 1872 1873 cdev = container_of(kobj, struct linux_cdev, kobj); 1874 parent = kobj->parent; 1875 if (cdev->cdev) 1876 destroy_dev(cdev->cdev); 1877 kfree(cdev); 1878 kobject_put(parent); 1879 } 1880 1881 static void 1882 linux_cdev_static_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 kobject_put(parent); 1892 } 1893 1894 const struct kobj_type linux_cdev_ktype = { 1895 .release = linux_cdev_release, 1896 }; 1897 1898 const struct kobj_type linux_cdev_static_ktype = { 1899 .release = linux_cdev_static_release, 1900 }; 1901 1902 static void 1903 linux_handle_ifnet_link_event(void *arg, struct ifnet *ifp, int linkstate) 1904 { 1905 struct notifier_block *nb; 1906 1907 nb = arg; 1908 if (linkstate == LINK_STATE_UP) 1909 nb->notifier_call(nb, NETDEV_UP, ifp); 1910 else 1911 nb->notifier_call(nb, NETDEV_DOWN, ifp); 1912 } 1913 1914 static void 1915 linux_handle_ifnet_arrival_event(void *arg, struct ifnet *ifp) 1916 { 1917 struct notifier_block *nb; 1918 1919 nb = arg; 1920 nb->notifier_call(nb, NETDEV_REGISTER, ifp); 1921 } 1922 1923 static void 1924 linux_handle_ifnet_departure_event(void *arg, struct ifnet *ifp) 1925 { 1926 struct notifier_block *nb; 1927 1928 nb = arg; 1929 nb->notifier_call(nb, NETDEV_UNREGISTER, ifp); 1930 } 1931 1932 static void 1933 linux_handle_iflladdr_event(void *arg, struct ifnet *ifp) 1934 { 1935 struct notifier_block *nb; 1936 1937 nb = arg; 1938 nb->notifier_call(nb, NETDEV_CHANGEADDR, ifp); 1939 } 1940 1941 static void 1942 linux_handle_ifaddr_event(void *arg, struct ifnet *ifp) 1943 { 1944 struct notifier_block *nb; 1945 1946 nb = arg; 1947 nb->notifier_call(nb, NETDEV_CHANGEIFADDR, ifp); 1948 } 1949 1950 int 1951 register_netdevice_notifier(struct notifier_block *nb) 1952 { 1953 1954 nb->tags[NETDEV_UP] = EVENTHANDLER_REGISTER( 1955 ifnet_link_event, linux_handle_ifnet_link_event, nb, 0); 1956 nb->tags[NETDEV_REGISTER] = EVENTHANDLER_REGISTER( 1957 ifnet_arrival_event, linux_handle_ifnet_arrival_event, nb, 0); 1958 nb->tags[NETDEV_UNREGISTER] = EVENTHANDLER_REGISTER( 1959 ifnet_departure_event, linux_handle_ifnet_departure_event, nb, 0); 1960 nb->tags[NETDEV_CHANGEADDR] = EVENTHANDLER_REGISTER( 1961 iflladdr_event, linux_handle_iflladdr_event, nb, 0); 1962 1963 return (0); 1964 } 1965 1966 int 1967 register_inetaddr_notifier(struct notifier_block *nb) 1968 { 1969 1970 nb->tags[NETDEV_CHANGEIFADDR] = EVENTHANDLER_REGISTER( 1971 ifaddr_event, linux_handle_ifaddr_event, nb, 0); 1972 return (0); 1973 } 1974 1975 int 1976 unregister_netdevice_notifier(struct notifier_block *nb) 1977 { 1978 1979 EVENTHANDLER_DEREGISTER(ifnet_link_event, 1980 nb->tags[NETDEV_UP]); 1981 EVENTHANDLER_DEREGISTER(ifnet_arrival_event, 1982 nb->tags[NETDEV_REGISTER]); 1983 EVENTHANDLER_DEREGISTER(ifnet_departure_event, 1984 nb->tags[NETDEV_UNREGISTER]); 1985 EVENTHANDLER_DEREGISTER(iflladdr_event, 1986 nb->tags[NETDEV_CHANGEADDR]); 1987 1988 return (0); 1989 } 1990 1991 int 1992 unregister_inetaddr_notifier(struct notifier_block *nb) 1993 { 1994 1995 EVENTHANDLER_DEREGISTER(ifaddr_event, 1996 nb->tags[NETDEV_CHANGEIFADDR]); 1997 1998 return (0); 1999 } 2000 2001 struct list_sort_thunk { 2002 int (*cmp)(void *, struct list_head *, struct list_head *); 2003 void *priv; 2004 }; 2005 2006 static inline int 2007 linux_le_cmp(void *priv, const void *d1, const void *d2) 2008 { 2009 struct list_head *le1, *le2; 2010 struct list_sort_thunk *thunk; 2011 2012 thunk = priv; 2013 le1 = *(__DECONST(struct list_head **, d1)); 2014 le2 = *(__DECONST(struct list_head **, d2)); 2015 return ((thunk->cmp)(thunk->priv, le1, le2)); 2016 } 2017 2018 void 2019 list_sort(void *priv, struct list_head *head, int (*cmp)(void *priv, 2020 struct list_head *a, struct list_head *b)) 2021 { 2022 struct list_sort_thunk thunk; 2023 struct list_head **ar, *le; 2024 size_t count, i; 2025 2026 count = 0; 2027 list_for_each(le, head) 2028 count++; 2029 ar = malloc(sizeof(struct list_head *) * count, M_KMALLOC, M_WAITOK); 2030 i = 0; 2031 list_for_each(le, head) 2032 ar[i++] = le; 2033 thunk.cmp = cmp; 2034 thunk.priv = priv; 2035 qsort_r(ar, count, sizeof(struct list_head *), &thunk, linux_le_cmp); 2036 INIT_LIST_HEAD(head); 2037 for (i = 0; i < count; i++) 2038 list_add_tail(ar[i], head); 2039 free(ar, M_KMALLOC); 2040 } 2041 2042 void 2043 linux_irq_handler(void *ent) 2044 { 2045 struct irq_ent *irqe; 2046 2047 linux_set_current(curthread); 2048 2049 irqe = ent; 2050 irqe->handler(irqe->irq, irqe->arg); 2051 } 2052 2053 #if defined(__i386__) || defined(__amd64__) 2054 int 2055 linux_wbinvd_on_all_cpus(void) 2056 { 2057 2058 pmap_invalidate_cache(); 2059 return (0); 2060 } 2061 #endif 2062 2063 int 2064 linux_on_each_cpu(void callback(void *), void *data) 2065 { 2066 2067 smp_rendezvous(smp_no_rendezvous_barrier, callback, 2068 smp_no_rendezvous_barrier, data); 2069 return (0); 2070 } 2071 2072 int 2073 linux_in_atomic(void) 2074 { 2075 2076 return ((curthread->td_pflags & TDP_NOFAULTING) != 0); 2077 } 2078 2079 struct linux_cdev * 2080 linux_find_cdev(const char *name, unsigned major, unsigned minor) 2081 { 2082 dev_t dev = MKDEV(major, minor); 2083 struct cdev *cdev; 2084 2085 dev_lock(); 2086 LIST_FOREACH(cdev, &linuxcdevsw.d_devs, si_list) { 2087 struct linux_cdev *ldev = cdev->si_drv1; 2088 if (ldev->dev == dev && 2089 strcmp(kobject_name(&ldev->kobj), name) == 0) { 2090 break; 2091 } 2092 } 2093 dev_unlock(); 2094 2095 return (cdev != NULL ? cdev->si_drv1 : NULL); 2096 } 2097 2098 int 2099 __register_chrdev(unsigned int major, unsigned int baseminor, 2100 unsigned int count, const char *name, 2101 const struct file_operations *fops) 2102 { 2103 struct linux_cdev *cdev; 2104 int ret = 0; 2105 int i; 2106 2107 for (i = baseminor; i < baseminor + count; i++) { 2108 cdev = cdev_alloc(); 2109 cdev_init(cdev, fops); 2110 kobject_set_name(&cdev->kobj, name); 2111 2112 ret = cdev_add(cdev, makedev(major, i), 1); 2113 if (ret != 0) 2114 break; 2115 } 2116 return (ret); 2117 } 2118 2119 int 2120 __register_chrdev_p(unsigned int major, unsigned int baseminor, 2121 unsigned int count, const char *name, 2122 const struct file_operations *fops, uid_t uid, 2123 gid_t gid, int mode) 2124 { 2125 struct linux_cdev *cdev; 2126 int ret = 0; 2127 int i; 2128 2129 for (i = baseminor; i < baseminor + count; i++) { 2130 cdev = cdev_alloc(); 2131 cdev_init(cdev, fops); 2132 kobject_set_name(&cdev->kobj, name); 2133 2134 ret = cdev_add_ext(cdev, makedev(major, i), uid, gid, mode); 2135 if (ret != 0) 2136 break; 2137 } 2138 return (ret); 2139 } 2140 2141 void 2142 __unregister_chrdev(unsigned int major, unsigned int baseminor, 2143 unsigned int count, const char *name) 2144 { 2145 struct linux_cdev *cdevp; 2146 int i; 2147 2148 for (i = baseminor; i < baseminor + count; i++) { 2149 cdevp = linux_find_cdev(name, major, i); 2150 if (cdevp != NULL) 2151 cdev_del(cdevp); 2152 } 2153 } 2154 2155 #if defined(__i386__) || defined(__amd64__) 2156 bool linux_cpu_has_clflush; 2157 #endif 2158 2159 static void 2160 linux_compat_init(void *arg) 2161 { 2162 struct sysctl_oid *rootoid; 2163 int i; 2164 2165 #if defined(__i386__) || defined(__amd64__) 2166 linux_cpu_has_clflush = (cpu_feature & CPUID_CLFSH); 2167 #endif 2168 rw_init(&linux_vma_lock, "lkpi-vma-lock"); 2169 2170 rootoid = SYSCTL_ADD_ROOT_NODE(NULL, 2171 OID_AUTO, "sys", CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, "sys"); 2172 kobject_init(&linux_class_root, &linux_class_ktype); 2173 kobject_set_name(&linux_class_root, "class"); 2174 linux_class_root.oidp = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(rootoid), 2175 OID_AUTO, "class", CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, "class"); 2176 kobject_init(&linux_root_device.kobj, &linux_dev_ktype); 2177 kobject_set_name(&linux_root_device.kobj, "device"); 2178 linux_root_device.kobj.oidp = SYSCTL_ADD_NODE(NULL, 2179 SYSCTL_CHILDREN(rootoid), OID_AUTO, "device", CTLFLAG_RD, NULL, 2180 "device"); 2181 linux_root_device.bsddev = root_bus; 2182 linux_class_misc.name = "misc"; 2183 class_register(&linux_class_misc); 2184 INIT_LIST_HEAD(&pci_drivers); 2185 INIT_LIST_HEAD(&pci_devices); 2186 spin_lock_init(&pci_lock); 2187 mtx_init(&vmmaplock, "IO Map lock", NULL, MTX_DEF); 2188 for (i = 0; i < VMMAP_HASH_SIZE; i++) 2189 LIST_INIT(&vmmaphead[i]); 2190 } 2191 SYSINIT(linux_compat, SI_SUB_DRIVERS, SI_ORDER_SECOND, linux_compat_init, NULL); 2192 2193 static void 2194 linux_compat_uninit(void *arg) 2195 { 2196 linux_kobject_kfree_name(&linux_class_root); 2197 linux_kobject_kfree_name(&linux_root_device.kobj); 2198 linux_kobject_kfree_name(&linux_class_misc.kobj); 2199 2200 mtx_destroy(&vmmaplock); 2201 spin_lock_destroy(&pci_lock); 2202 rw_destroy(&linux_vma_lock); 2203 } 2204 SYSUNINIT(linux_compat, SI_SUB_DRIVERS, SI_ORDER_SECOND, linux_compat_uninit, NULL); 2205 2206 /* 2207 * NOTE: Linux frequently uses "unsigned long" for pointer to integer 2208 * conversion and vice versa, where in FreeBSD "uintptr_t" would be 2209 * used. Assert these types have the same size, else some parts of the 2210 * LinuxKPI may not work like expected: 2211 */ 2212 CTASSERT(sizeof(unsigned long) == sizeof(uintptr_t)); 2213