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-2021 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 #include "opt_global.h" 32 #include "opt_stack.h" 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/malloc.h> 37 #include <sys/kernel.h> 38 #include <sys/sysctl.h> 39 #include <sys/proc.h> 40 #include <sys/sglist.h> 41 #include <sys/sleepqueue.h> 42 #include <sys/refcount.h> 43 #include <sys/lock.h> 44 #include <sys/mutex.h> 45 #include <sys/bus.h> 46 #include <sys/eventhandler.h> 47 #include <sys/fcntl.h> 48 #include <sys/file.h> 49 #include <sys/filio.h> 50 #include <sys/rwlock.h> 51 #include <sys/mman.h> 52 #include <sys/stack.h> 53 #include <sys/sysent.h> 54 #include <sys/time.h> 55 #include <sys/user.h> 56 57 #include <vm/vm.h> 58 #include <vm/pmap.h> 59 #include <vm/vm_object.h> 60 #include <vm/vm_page.h> 61 #include <vm/vm_pager.h> 62 63 #include <machine/stdarg.h> 64 65 #if defined(__i386__) || defined(__amd64__) 66 #include <machine/cputypes.h> 67 #include <machine/md_var.h> 68 #endif 69 70 #include <linux/kobject.h> 71 #include <linux/cpu.h> 72 #include <linux/device.h> 73 #include <linux/slab.h> 74 #include <linux/module.h> 75 #include <linux/moduleparam.h> 76 #include <linux/cdev.h> 77 #include <linux/file.h> 78 #include <linux/sysfs.h> 79 #include <linux/mm.h> 80 #include <linux/io.h> 81 #include <linux/vmalloc.h> 82 #include <linux/netdevice.h> 83 #include <linux/timer.h> 84 #include <linux/interrupt.h> 85 #include <linux/uaccess.h> 86 #include <linux/utsname.h> 87 #include <linux/list.h> 88 #include <linux/kthread.h> 89 #include <linux/kernel.h> 90 #include <linux/compat.h> 91 #include <linux/io-mapping.h> 92 #include <linux/poll.h> 93 #include <linux/smp.h> 94 #include <linux/wait_bit.h> 95 #include <linux/rcupdate.h> 96 #include <linux/interval_tree.h> 97 #include <linux/interval_tree_generic.h> 98 99 #if defined(__i386__) || defined(__amd64__) 100 #include <asm/smp.h> 101 #include <asm/processor.h> 102 #endif 103 104 #include <xen/xen.h> 105 #ifdef XENHVM 106 #undef xen_pv_domain 107 #undef xen_initial_domain 108 /* xen/xen-os.h redefines __must_check */ 109 #undef __must_check 110 #include <xen/xen-os.h> 111 #endif 112 113 SYSCTL_NODE(_compat, OID_AUTO, linuxkpi, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 114 "LinuxKPI parameters"); 115 116 int linuxkpi_debug; 117 SYSCTL_INT(_compat_linuxkpi, OID_AUTO, debug, CTLFLAG_RWTUN, 118 &linuxkpi_debug, 0, "Set to enable pr_debug() prints. Clear to disable."); 119 120 int linuxkpi_warn_dump_stack = 0; 121 SYSCTL_INT(_compat_linuxkpi, OID_AUTO, warn_dump_stack, CTLFLAG_RWTUN, 122 &linuxkpi_warn_dump_stack, 0, 123 "Set to enable stack traces from WARN_ON(). Clear to disable."); 124 125 static struct timeval lkpi_net_lastlog; 126 static int lkpi_net_curpps; 127 static int lkpi_net_maxpps = 99; 128 SYSCTL_INT(_compat_linuxkpi, OID_AUTO, net_ratelimit, CTLFLAG_RWTUN, 129 &lkpi_net_maxpps, 0, "Limit number of LinuxKPI net messages per second."); 130 131 MALLOC_DEFINE(M_KMALLOC, "lkpikmalloc", "Linux kmalloc compat"); 132 133 #include <linux/rbtree.h> 134 /* Undo Linux compat changes. */ 135 #undef RB_ROOT 136 #undef file 137 #undef cdev 138 #define RB_ROOT(head) (head)->rbh_root 139 140 static void linux_destroy_dev(struct linux_cdev *); 141 static void linux_cdev_deref(struct linux_cdev *ldev); 142 static struct vm_area_struct *linux_cdev_handle_find(void *handle); 143 144 cpumask_t cpu_online_mask; 145 static cpumask_t **static_single_cpu_mask; 146 static cpumask_t *static_single_cpu_mask_lcs; 147 struct kobject linux_class_root; 148 struct device linux_root_device; 149 struct class linux_class_misc; 150 struct list_head pci_drivers; 151 struct list_head pci_devices; 152 spinlock_t pci_lock; 153 struct uts_namespace init_uts_ns; 154 155 unsigned long linux_timer_hz_mask; 156 157 wait_queue_head_t linux_bit_waitq; 158 wait_queue_head_t linux_var_waitq; 159 160 int 161 panic_cmp(struct rb_node *one, struct rb_node *two) 162 { 163 panic("no cmp"); 164 } 165 166 RB_GENERATE(linux_root, rb_node, __entry, panic_cmp); 167 168 #define START(node) ((node)->start) 169 #define LAST(node) ((node)->last) 170 171 INTERVAL_TREE_DEFINE(struct interval_tree_node, rb, unsigned long,, START, 172 LAST,, lkpi_interval_tree) 173 174 static void 175 linux_device_release(struct device *dev) 176 { 177 pr_debug("linux_device_release: %s\n", dev_name(dev)); 178 kfree(dev); 179 } 180 181 static ssize_t 182 linux_class_show(struct kobject *kobj, struct attribute *attr, char *buf) 183 { 184 struct class_attribute *dattr; 185 ssize_t error; 186 187 dattr = container_of(attr, struct class_attribute, attr); 188 error = -EIO; 189 if (dattr->show) 190 error = dattr->show(container_of(kobj, struct class, kobj), 191 dattr, buf); 192 return (error); 193 } 194 195 static ssize_t 196 linux_class_store(struct kobject *kobj, struct attribute *attr, const char *buf, 197 size_t count) 198 { 199 struct class_attribute *dattr; 200 ssize_t error; 201 202 dattr = container_of(attr, struct class_attribute, attr); 203 error = -EIO; 204 if (dattr->store) 205 error = dattr->store(container_of(kobj, struct class, kobj), 206 dattr, buf, count); 207 return (error); 208 } 209 210 static void 211 linux_class_release(struct kobject *kobj) 212 { 213 struct class *class; 214 215 class = container_of(kobj, struct class, kobj); 216 if (class->class_release) 217 class->class_release(class); 218 } 219 220 static const struct sysfs_ops linux_class_sysfs = { 221 .show = linux_class_show, 222 .store = linux_class_store, 223 }; 224 225 const struct kobj_type linux_class_ktype = { 226 .release = linux_class_release, 227 .sysfs_ops = &linux_class_sysfs 228 }; 229 230 static void 231 linux_dev_release(struct kobject *kobj) 232 { 233 struct device *dev; 234 235 dev = container_of(kobj, struct device, kobj); 236 /* This is the precedence defined by linux. */ 237 if (dev->release) 238 dev->release(dev); 239 else if (dev->class && dev->class->dev_release) 240 dev->class->dev_release(dev); 241 } 242 243 static ssize_t 244 linux_dev_show(struct kobject *kobj, struct attribute *attr, char *buf) 245 { 246 struct device_attribute *dattr; 247 ssize_t error; 248 249 dattr = container_of(attr, struct device_attribute, attr); 250 error = -EIO; 251 if (dattr->show) 252 error = dattr->show(container_of(kobj, struct device, kobj), 253 dattr, buf); 254 return (error); 255 } 256 257 static ssize_t 258 linux_dev_store(struct kobject *kobj, struct attribute *attr, const char *buf, 259 size_t count) 260 { 261 struct device_attribute *dattr; 262 ssize_t error; 263 264 dattr = container_of(attr, struct device_attribute, attr); 265 error = -EIO; 266 if (dattr->store) 267 error = dattr->store(container_of(kobj, struct device, kobj), 268 dattr, buf, count); 269 return (error); 270 } 271 272 static const struct sysfs_ops linux_dev_sysfs = { 273 .show = linux_dev_show, 274 .store = linux_dev_store, 275 }; 276 277 const struct kobj_type linux_dev_ktype = { 278 .release = linux_dev_release, 279 .sysfs_ops = &linux_dev_sysfs 280 }; 281 282 struct device * 283 device_create(struct class *class, struct device *parent, dev_t devt, 284 void *drvdata, const char *fmt, ...) 285 { 286 struct device *dev; 287 va_list args; 288 289 dev = kzalloc(sizeof(*dev), M_WAITOK); 290 dev->parent = parent; 291 dev->class = class; 292 dev->devt = devt; 293 dev->driver_data = drvdata; 294 dev->release = linux_device_release; 295 va_start(args, fmt); 296 kobject_set_name_vargs(&dev->kobj, fmt, args); 297 va_end(args); 298 device_register(dev); 299 300 return (dev); 301 } 302 303 struct device * 304 device_create_groups_vargs(struct class *class, struct device *parent, 305 dev_t devt, void *drvdata, const struct attribute_group **groups, 306 const char *fmt, va_list args) 307 { 308 struct device *dev = NULL; 309 int retval = -ENODEV; 310 311 if (class == NULL || IS_ERR(class)) 312 goto error; 313 314 dev = kzalloc(sizeof(*dev), GFP_KERNEL); 315 if (!dev) { 316 retval = -ENOMEM; 317 goto error; 318 } 319 320 dev->devt = devt; 321 dev->class = class; 322 dev->parent = parent; 323 dev->groups = groups; 324 dev->release = device_create_release; 325 /* device_initialize() needs the class and parent to be set */ 326 device_initialize(dev); 327 dev_set_drvdata(dev, drvdata); 328 329 retval = kobject_set_name_vargs(&dev->kobj, fmt, args); 330 if (retval) 331 goto error; 332 333 retval = device_add(dev); 334 if (retval) 335 goto error; 336 337 return dev; 338 339 error: 340 put_device(dev); 341 return ERR_PTR(retval); 342 } 343 344 struct class * 345 class_create(struct module *owner, const char *name) 346 { 347 struct class *class; 348 int error; 349 350 class = kzalloc(sizeof(*class), M_WAITOK); 351 class->owner = owner; 352 class->name = name; 353 class->class_release = linux_class_kfree; 354 error = class_register(class); 355 if (error) { 356 kfree(class); 357 return (NULL); 358 } 359 360 return (class); 361 } 362 363 static void 364 linux_kq_lock(void *arg) 365 { 366 spinlock_t *s = arg; 367 368 spin_lock(s); 369 } 370 static void 371 linux_kq_unlock(void *arg) 372 { 373 spinlock_t *s = arg; 374 375 spin_unlock(s); 376 } 377 378 static void 379 linux_kq_assert_lock(void *arg, int what) 380 { 381 #ifdef INVARIANTS 382 spinlock_t *s = arg; 383 384 if (what == LA_LOCKED) 385 mtx_assert(&s->m, MA_OWNED); 386 else 387 mtx_assert(&s->m, MA_NOTOWNED); 388 #endif 389 } 390 391 static void 392 linux_file_kqfilter_poll(struct linux_file *, int); 393 394 struct linux_file * 395 linux_file_alloc(void) 396 { 397 struct linux_file *filp; 398 399 filp = kzalloc(sizeof(*filp), GFP_KERNEL); 400 401 /* set initial refcount */ 402 filp->f_count = 1; 403 404 /* setup fields needed by kqueue support */ 405 spin_lock_init(&filp->f_kqlock); 406 knlist_init(&filp->f_selinfo.si_note, &filp->f_kqlock, 407 linux_kq_lock, linux_kq_unlock, linux_kq_assert_lock); 408 409 return (filp); 410 } 411 412 void 413 linux_file_free(struct linux_file *filp) 414 { 415 if (filp->_file == NULL) { 416 if (filp->f_op != NULL && filp->f_op->release != NULL) 417 filp->f_op->release(filp->f_vnode, filp); 418 if (filp->f_shmem != NULL) 419 vm_object_deallocate(filp->f_shmem); 420 kfree_rcu(filp, rcu); 421 } else { 422 /* 423 * The close method of the character device or file 424 * will free the linux_file structure: 425 */ 426 _fdrop(filp->_file, curthread); 427 } 428 } 429 430 struct linux_cdev * 431 cdev_alloc(void) 432 { 433 struct linux_cdev *cdev; 434 435 cdev = kzalloc(sizeof(struct linux_cdev), M_WAITOK); 436 kobject_init(&cdev->kobj, &linux_cdev_ktype); 437 cdev->refs = 1; 438 return (cdev); 439 } 440 441 static int 442 linux_cdev_pager_fault(vm_object_t vm_obj, vm_ooffset_t offset, int prot, 443 vm_page_t *mres) 444 { 445 struct vm_area_struct *vmap; 446 447 vmap = linux_cdev_handle_find(vm_obj->handle); 448 449 MPASS(vmap != NULL); 450 MPASS(vmap->vm_private_data == vm_obj->handle); 451 452 if (likely(vmap->vm_ops != NULL && offset < vmap->vm_len)) { 453 vm_paddr_t paddr = IDX_TO_OFF(vmap->vm_pfn) + offset; 454 vm_page_t page; 455 456 if (((*mres)->flags & PG_FICTITIOUS) != 0) { 457 /* 458 * If the passed in result page is a fake 459 * page, update it with the new physical 460 * address. 461 */ 462 page = *mres; 463 vm_page_updatefake(page, paddr, vm_obj->memattr); 464 } else { 465 /* 466 * Replace the passed in "mres" page with our 467 * own fake page and free up the all of the 468 * original pages. 469 */ 470 VM_OBJECT_WUNLOCK(vm_obj); 471 page = vm_page_getfake(paddr, vm_obj->memattr); 472 VM_OBJECT_WLOCK(vm_obj); 473 474 vm_page_replace(page, vm_obj, (*mres)->pindex, *mres); 475 *mres = page; 476 } 477 vm_page_valid(page); 478 return (VM_PAGER_OK); 479 } 480 return (VM_PAGER_FAIL); 481 } 482 483 static int 484 linux_cdev_pager_populate(vm_object_t vm_obj, vm_pindex_t pidx, int fault_type, 485 vm_prot_t max_prot, vm_pindex_t *first, vm_pindex_t *last) 486 { 487 struct vm_area_struct *vmap; 488 int err; 489 490 /* get VM area structure */ 491 vmap = linux_cdev_handle_find(vm_obj->handle); 492 MPASS(vmap != NULL); 493 MPASS(vmap->vm_private_data == vm_obj->handle); 494 495 VM_OBJECT_WUNLOCK(vm_obj); 496 497 linux_set_current(curthread); 498 499 down_write(&vmap->vm_mm->mmap_sem); 500 if (unlikely(vmap->vm_ops == NULL)) { 501 err = VM_FAULT_SIGBUS; 502 } else { 503 struct vm_fault vmf; 504 505 /* fill out VM fault structure */ 506 vmf.virtual_address = (void *)(uintptr_t)IDX_TO_OFF(pidx); 507 vmf.flags = (fault_type & VM_PROT_WRITE) ? FAULT_FLAG_WRITE : 0; 508 vmf.pgoff = 0; 509 vmf.page = NULL; 510 vmf.vma = vmap; 511 512 vmap->vm_pfn_count = 0; 513 vmap->vm_pfn_pcount = &vmap->vm_pfn_count; 514 vmap->vm_obj = vm_obj; 515 516 err = vmap->vm_ops->fault(&vmf); 517 518 while (vmap->vm_pfn_count == 0 && err == VM_FAULT_NOPAGE) { 519 kern_yield(PRI_USER); 520 err = vmap->vm_ops->fault(&vmf); 521 } 522 } 523 524 /* translate return code */ 525 switch (err) { 526 case VM_FAULT_OOM: 527 err = VM_PAGER_AGAIN; 528 break; 529 case VM_FAULT_SIGBUS: 530 err = VM_PAGER_BAD; 531 break; 532 case VM_FAULT_NOPAGE: 533 /* 534 * By contract the fault handler will return having 535 * busied all the pages itself. If pidx is already 536 * found in the object, it will simply xbusy the first 537 * page and return with vm_pfn_count set to 1. 538 */ 539 *first = vmap->vm_pfn_first; 540 *last = *first + vmap->vm_pfn_count - 1; 541 err = VM_PAGER_OK; 542 break; 543 default: 544 err = VM_PAGER_ERROR; 545 break; 546 } 547 up_write(&vmap->vm_mm->mmap_sem); 548 VM_OBJECT_WLOCK(vm_obj); 549 return (err); 550 } 551 552 static struct rwlock linux_vma_lock; 553 static TAILQ_HEAD(, vm_area_struct) linux_vma_head = 554 TAILQ_HEAD_INITIALIZER(linux_vma_head); 555 556 static void 557 linux_cdev_handle_free(struct vm_area_struct *vmap) 558 { 559 /* Drop reference on vm_file */ 560 if (vmap->vm_file != NULL) 561 fput(vmap->vm_file); 562 563 /* Drop reference on mm_struct */ 564 mmput(vmap->vm_mm); 565 566 kfree(vmap); 567 } 568 569 static void 570 linux_cdev_handle_remove(struct vm_area_struct *vmap) 571 { 572 rw_wlock(&linux_vma_lock); 573 TAILQ_REMOVE(&linux_vma_head, vmap, vm_entry); 574 rw_wunlock(&linux_vma_lock); 575 } 576 577 static struct vm_area_struct * 578 linux_cdev_handle_find(void *handle) 579 { 580 struct vm_area_struct *vmap; 581 582 rw_rlock(&linux_vma_lock); 583 TAILQ_FOREACH(vmap, &linux_vma_head, vm_entry) { 584 if (vmap->vm_private_data == handle) 585 break; 586 } 587 rw_runlock(&linux_vma_lock); 588 return (vmap); 589 } 590 591 static int 592 linux_cdev_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot, 593 vm_ooffset_t foff, struct ucred *cred, u_short *color) 594 { 595 596 MPASS(linux_cdev_handle_find(handle) != NULL); 597 *color = 0; 598 return (0); 599 } 600 601 static void 602 linux_cdev_pager_dtor(void *handle) 603 { 604 const struct vm_operations_struct *vm_ops; 605 struct vm_area_struct *vmap; 606 607 vmap = linux_cdev_handle_find(handle); 608 MPASS(vmap != NULL); 609 610 /* 611 * Remove handle before calling close operation to prevent 612 * other threads from reusing the handle pointer. 613 */ 614 linux_cdev_handle_remove(vmap); 615 616 down_write(&vmap->vm_mm->mmap_sem); 617 vm_ops = vmap->vm_ops; 618 if (likely(vm_ops != NULL)) 619 vm_ops->close(vmap); 620 up_write(&vmap->vm_mm->mmap_sem); 621 622 linux_cdev_handle_free(vmap); 623 } 624 625 static struct cdev_pager_ops linux_cdev_pager_ops[2] = { 626 { 627 /* OBJT_MGTDEVICE */ 628 .cdev_pg_populate = linux_cdev_pager_populate, 629 .cdev_pg_ctor = linux_cdev_pager_ctor, 630 .cdev_pg_dtor = linux_cdev_pager_dtor 631 }, 632 { 633 /* OBJT_DEVICE */ 634 .cdev_pg_fault = linux_cdev_pager_fault, 635 .cdev_pg_ctor = linux_cdev_pager_ctor, 636 .cdev_pg_dtor = linux_cdev_pager_dtor 637 }, 638 }; 639 640 int 641 zap_vma_ptes(struct vm_area_struct *vma, unsigned long address, 642 unsigned long size) 643 { 644 vm_object_t obj; 645 vm_page_t m; 646 647 obj = vma->vm_obj; 648 if (obj == NULL || (obj->flags & OBJ_UNMANAGED) != 0) 649 return (-ENOTSUP); 650 VM_OBJECT_RLOCK(obj); 651 for (m = vm_page_find_least(obj, OFF_TO_IDX(address)); 652 m != NULL && m->pindex < OFF_TO_IDX(address + size); 653 m = TAILQ_NEXT(m, listq)) 654 pmap_remove_all(m); 655 VM_OBJECT_RUNLOCK(obj); 656 return (0); 657 } 658 659 void 660 vma_set_file(struct vm_area_struct *vma, struct linux_file *file) 661 { 662 struct linux_file *tmp; 663 664 /* Changing an anonymous vma with this is illegal */ 665 get_file(file); 666 tmp = vma->vm_file; 667 vma->vm_file = file; 668 fput(tmp); 669 } 670 671 static struct file_operations dummy_ldev_ops = { 672 /* XXXKIB */ 673 }; 674 675 static struct linux_cdev dummy_ldev = { 676 .ops = &dummy_ldev_ops, 677 }; 678 679 #define LDEV_SI_DTR 0x0001 680 #define LDEV_SI_REF 0x0002 681 682 static void 683 linux_get_fop(struct linux_file *filp, const struct file_operations **fop, 684 struct linux_cdev **dev) 685 { 686 struct linux_cdev *ldev; 687 u_int siref; 688 689 ldev = filp->f_cdev; 690 *fop = filp->f_op; 691 if (ldev != NULL) { 692 if (ldev->kobj.ktype == &linux_cdev_static_ktype) { 693 refcount_acquire(&ldev->refs); 694 } else { 695 for (siref = ldev->siref;;) { 696 if ((siref & LDEV_SI_DTR) != 0) { 697 ldev = &dummy_ldev; 698 *fop = ldev->ops; 699 siref = ldev->siref; 700 MPASS((ldev->siref & LDEV_SI_DTR) == 0); 701 } else if (atomic_fcmpset_int(&ldev->siref, 702 &siref, siref + LDEV_SI_REF)) { 703 break; 704 } 705 } 706 } 707 } 708 *dev = ldev; 709 } 710 711 static void 712 linux_drop_fop(struct linux_cdev *ldev) 713 { 714 715 if (ldev == NULL) 716 return; 717 if (ldev->kobj.ktype == &linux_cdev_static_ktype) { 718 linux_cdev_deref(ldev); 719 } else { 720 MPASS(ldev->kobj.ktype == &linux_cdev_ktype); 721 MPASS((ldev->siref & ~LDEV_SI_DTR) != 0); 722 atomic_subtract_int(&ldev->siref, LDEV_SI_REF); 723 } 724 } 725 726 #define OPW(fp,td,code) ({ \ 727 struct file *__fpop; \ 728 __typeof(code) __retval; \ 729 \ 730 __fpop = (td)->td_fpop; \ 731 (td)->td_fpop = (fp); \ 732 __retval = (code); \ 733 (td)->td_fpop = __fpop; \ 734 __retval; \ 735 }) 736 737 static int 738 linux_dev_fdopen(struct cdev *dev, int fflags, struct thread *td, 739 struct file *file) 740 { 741 struct linux_cdev *ldev; 742 struct linux_file *filp; 743 const struct file_operations *fop; 744 int error; 745 746 ldev = dev->si_drv1; 747 748 filp = linux_file_alloc(); 749 filp->f_dentry = &filp->f_dentry_store; 750 filp->f_op = ldev->ops; 751 filp->f_mode = file->f_flag; 752 filp->f_flags = file->f_flag; 753 filp->f_vnode = file->f_vnode; 754 filp->_file = file; 755 refcount_acquire(&ldev->refs); 756 filp->f_cdev = ldev; 757 758 linux_set_current(td); 759 linux_get_fop(filp, &fop, &ldev); 760 761 if (fop->open != NULL) { 762 error = -fop->open(file->f_vnode, filp); 763 if (error != 0) { 764 linux_drop_fop(ldev); 765 linux_cdev_deref(filp->f_cdev); 766 kfree(filp); 767 return (error); 768 } 769 } 770 771 /* hold on to the vnode - used for fstat() */ 772 vhold(filp->f_vnode); 773 774 /* release the file from devfs */ 775 finit(file, filp->f_mode, DTYPE_DEV, filp, &linuxfileops); 776 linux_drop_fop(ldev); 777 return (ENXIO); 778 } 779 780 #define LINUX_IOCTL_MIN_PTR 0x10000UL 781 #define LINUX_IOCTL_MAX_PTR (LINUX_IOCTL_MIN_PTR + IOCPARM_MAX) 782 783 static inline int 784 linux_remap_address(void **uaddr, size_t len) 785 { 786 uintptr_t uaddr_val = (uintptr_t)(*uaddr); 787 788 if (unlikely(uaddr_val >= LINUX_IOCTL_MIN_PTR && 789 uaddr_val < LINUX_IOCTL_MAX_PTR)) { 790 struct task_struct *pts = current; 791 if (pts == NULL) { 792 *uaddr = NULL; 793 return (1); 794 } 795 796 /* compute data offset */ 797 uaddr_val -= LINUX_IOCTL_MIN_PTR; 798 799 /* check that length is within bounds */ 800 if ((len > IOCPARM_MAX) || 801 (uaddr_val + len) > pts->bsd_ioctl_len) { 802 *uaddr = NULL; 803 return (1); 804 } 805 806 /* re-add kernel buffer address */ 807 uaddr_val += (uintptr_t)pts->bsd_ioctl_data; 808 809 /* update address location */ 810 *uaddr = (void *)uaddr_val; 811 return (1); 812 } 813 return (0); 814 } 815 816 int 817 linux_copyin(const void *uaddr, void *kaddr, size_t len) 818 { 819 if (linux_remap_address(__DECONST(void **, &uaddr), len)) { 820 if (uaddr == NULL) 821 return (-EFAULT); 822 memcpy(kaddr, uaddr, len); 823 return (0); 824 } 825 return (-copyin(uaddr, kaddr, len)); 826 } 827 828 int 829 linux_copyout(const void *kaddr, void *uaddr, size_t len) 830 { 831 if (linux_remap_address(&uaddr, len)) { 832 if (uaddr == NULL) 833 return (-EFAULT); 834 memcpy(uaddr, kaddr, len); 835 return (0); 836 } 837 return (-copyout(kaddr, uaddr, len)); 838 } 839 840 size_t 841 linux_clear_user(void *_uaddr, size_t _len) 842 { 843 uint8_t *uaddr = _uaddr; 844 size_t len = _len; 845 846 /* make sure uaddr is aligned before going into the fast loop */ 847 while (((uintptr_t)uaddr & 7) != 0 && len > 7) { 848 if (subyte(uaddr, 0)) 849 return (_len); 850 uaddr++; 851 len--; 852 } 853 854 /* zero 8 bytes at a time */ 855 while (len > 7) { 856 #ifdef __LP64__ 857 if (suword64(uaddr, 0)) 858 return (_len); 859 #else 860 if (suword32(uaddr, 0)) 861 return (_len); 862 if (suword32(uaddr + 4, 0)) 863 return (_len); 864 #endif 865 uaddr += 8; 866 len -= 8; 867 } 868 869 /* zero fill end, if any */ 870 while (len > 0) { 871 if (subyte(uaddr, 0)) 872 return (_len); 873 uaddr++; 874 len--; 875 } 876 return (0); 877 } 878 879 int 880 linux_access_ok(const void *uaddr, size_t len) 881 { 882 uintptr_t saddr; 883 uintptr_t eaddr; 884 885 /* get start and end address */ 886 saddr = (uintptr_t)uaddr; 887 eaddr = (uintptr_t)uaddr + len; 888 889 /* verify addresses are valid for userspace */ 890 return ((saddr == eaddr) || 891 (eaddr > saddr && eaddr <= VM_MAXUSER_ADDRESS)); 892 } 893 894 /* 895 * This function should return either EINTR or ERESTART depending on 896 * the signal type sent to this thread: 897 */ 898 static int 899 linux_get_error(struct task_struct *task, int error) 900 { 901 /* check for signal type interrupt code */ 902 if (error == EINTR || error == ERESTARTSYS || error == ERESTART) { 903 error = -linux_schedule_get_interrupt_value(task); 904 if (error == 0) 905 error = EINTR; 906 } 907 return (error); 908 } 909 910 static int 911 linux_file_ioctl_sub(struct file *fp, struct linux_file *filp, 912 const struct file_operations *fop, u_long cmd, caddr_t data, 913 struct thread *td) 914 { 915 struct task_struct *task = current; 916 unsigned size; 917 int error; 918 919 size = IOCPARM_LEN(cmd); 920 /* refer to logic in sys_ioctl() */ 921 if (size > 0) { 922 /* 923 * Setup hint for linux_copyin() and linux_copyout(). 924 * 925 * Background: Linux code expects a user-space address 926 * while FreeBSD supplies a kernel-space address. 927 */ 928 task->bsd_ioctl_data = data; 929 task->bsd_ioctl_len = size; 930 data = (void *)LINUX_IOCTL_MIN_PTR; 931 } else { 932 /* fetch user-space pointer */ 933 data = *(void **)data; 934 } 935 #ifdef COMPAT_FREEBSD32 936 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) { 937 /* try the compat IOCTL handler first */ 938 if (fop->compat_ioctl != NULL) { 939 error = -OPW(fp, td, fop->compat_ioctl(filp, 940 cmd, (u_long)data)); 941 } else { 942 error = ENOTTY; 943 } 944 945 /* fallback to the regular IOCTL handler, if any */ 946 if (error == ENOTTY && fop->unlocked_ioctl != NULL) { 947 error = -OPW(fp, td, fop->unlocked_ioctl(filp, 948 cmd, (u_long)data)); 949 } 950 } else 951 #endif 952 { 953 if (fop->unlocked_ioctl != NULL) { 954 error = -OPW(fp, td, fop->unlocked_ioctl(filp, 955 cmd, (u_long)data)); 956 } else { 957 error = ENOTTY; 958 } 959 } 960 if (size > 0) { 961 task->bsd_ioctl_data = NULL; 962 task->bsd_ioctl_len = 0; 963 } 964 965 if (error == EWOULDBLOCK) { 966 /* update kqfilter status, if any */ 967 linux_file_kqfilter_poll(filp, 968 LINUX_KQ_FLAG_HAS_READ | LINUX_KQ_FLAG_HAS_WRITE); 969 } else { 970 error = linux_get_error(task, error); 971 } 972 return (error); 973 } 974 975 #define LINUX_POLL_TABLE_NORMAL ((poll_table *)1) 976 977 /* 978 * This function atomically updates the poll wakeup state and returns 979 * the previous state at the time of update. 980 */ 981 static uint8_t 982 linux_poll_wakeup_state(atomic_t *v, const uint8_t *pstate) 983 { 984 int c, old; 985 986 c = v->counter; 987 988 while ((old = atomic_cmpxchg(v, c, pstate[c])) != c) 989 c = old; 990 991 return (c); 992 } 993 994 static int 995 linux_poll_wakeup_callback(wait_queue_t *wq, unsigned int wq_state, int flags, void *key) 996 { 997 static const uint8_t state[LINUX_FWQ_STATE_MAX] = { 998 [LINUX_FWQ_STATE_INIT] = LINUX_FWQ_STATE_INIT, /* NOP */ 999 [LINUX_FWQ_STATE_NOT_READY] = LINUX_FWQ_STATE_NOT_READY, /* NOP */ 1000 [LINUX_FWQ_STATE_QUEUED] = LINUX_FWQ_STATE_READY, 1001 [LINUX_FWQ_STATE_READY] = LINUX_FWQ_STATE_READY, /* NOP */ 1002 }; 1003 struct linux_file *filp = container_of(wq, struct linux_file, f_wait_queue.wq); 1004 1005 switch (linux_poll_wakeup_state(&filp->f_wait_queue.state, state)) { 1006 case LINUX_FWQ_STATE_QUEUED: 1007 linux_poll_wakeup(filp); 1008 return (1); 1009 default: 1010 return (0); 1011 } 1012 } 1013 1014 void 1015 linux_poll_wait(struct linux_file *filp, wait_queue_head_t *wqh, poll_table *p) 1016 { 1017 static const uint8_t state[LINUX_FWQ_STATE_MAX] = { 1018 [LINUX_FWQ_STATE_INIT] = LINUX_FWQ_STATE_NOT_READY, 1019 [LINUX_FWQ_STATE_NOT_READY] = LINUX_FWQ_STATE_NOT_READY, /* NOP */ 1020 [LINUX_FWQ_STATE_QUEUED] = LINUX_FWQ_STATE_QUEUED, /* NOP */ 1021 [LINUX_FWQ_STATE_READY] = LINUX_FWQ_STATE_QUEUED, 1022 }; 1023 1024 /* check if we are called inside the select system call */ 1025 if (p == LINUX_POLL_TABLE_NORMAL) 1026 selrecord(curthread, &filp->f_selinfo); 1027 1028 switch (linux_poll_wakeup_state(&filp->f_wait_queue.state, state)) { 1029 case LINUX_FWQ_STATE_INIT: 1030 /* NOTE: file handles can only belong to one wait-queue */ 1031 filp->f_wait_queue.wqh = wqh; 1032 filp->f_wait_queue.wq.func = &linux_poll_wakeup_callback; 1033 add_wait_queue(wqh, &filp->f_wait_queue.wq); 1034 atomic_set(&filp->f_wait_queue.state, LINUX_FWQ_STATE_QUEUED); 1035 break; 1036 default: 1037 break; 1038 } 1039 } 1040 1041 static void 1042 linux_poll_wait_dequeue(struct linux_file *filp) 1043 { 1044 static const uint8_t state[LINUX_FWQ_STATE_MAX] = { 1045 [LINUX_FWQ_STATE_INIT] = LINUX_FWQ_STATE_INIT, /* NOP */ 1046 [LINUX_FWQ_STATE_NOT_READY] = LINUX_FWQ_STATE_INIT, 1047 [LINUX_FWQ_STATE_QUEUED] = LINUX_FWQ_STATE_INIT, 1048 [LINUX_FWQ_STATE_READY] = LINUX_FWQ_STATE_INIT, 1049 }; 1050 1051 seldrain(&filp->f_selinfo); 1052 1053 switch (linux_poll_wakeup_state(&filp->f_wait_queue.state, state)) { 1054 case LINUX_FWQ_STATE_NOT_READY: 1055 case LINUX_FWQ_STATE_QUEUED: 1056 case LINUX_FWQ_STATE_READY: 1057 remove_wait_queue(filp->f_wait_queue.wqh, &filp->f_wait_queue.wq); 1058 break; 1059 default: 1060 break; 1061 } 1062 } 1063 1064 void 1065 linux_poll_wakeup(struct linux_file *filp) 1066 { 1067 /* this function should be NULL-safe */ 1068 if (filp == NULL) 1069 return; 1070 1071 selwakeup(&filp->f_selinfo); 1072 1073 spin_lock(&filp->f_kqlock); 1074 filp->f_kqflags |= LINUX_KQ_FLAG_NEED_READ | 1075 LINUX_KQ_FLAG_NEED_WRITE; 1076 1077 /* make sure the "knote" gets woken up */ 1078 KNOTE_LOCKED(&filp->f_selinfo.si_note, 1); 1079 spin_unlock(&filp->f_kqlock); 1080 } 1081 1082 static void 1083 linux_file_kqfilter_detach(struct knote *kn) 1084 { 1085 struct linux_file *filp = kn->kn_hook; 1086 1087 spin_lock(&filp->f_kqlock); 1088 knlist_remove(&filp->f_selinfo.si_note, kn, 1); 1089 spin_unlock(&filp->f_kqlock); 1090 } 1091 1092 static int 1093 linux_file_kqfilter_read_event(struct knote *kn, long hint) 1094 { 1095 struct linux_file *filp = kn->kn_hook; 1096 1097 mtx_assert(&filp->f_kqlock.m, MA_OWNED); 1098 1099 return ((filp->f_kqflags & LINUX_KQ_FLAG_NEED_READ) ? 1 : 0); 1100 } 1101 1102 static int 1103 linux_file_kqfilter_write_event(struct knote *kn, long hint) 1104 { 1105 struct linux_file *filp = kn->kn_hook; 1106 1107 mtx_assert(&filp->f_kqlock.m, MA_OWNED); 1108 1109 return ((filp->f_kqflags & LINUX_KQ_FLAG_NEED_WRITE) ? 1 : 0); 1110 } 1111 1112 static struct filterops linux_dev_kqfiltops_read = { 1113 .f_isfd = 1, 1114 .f_detach = linux_file_kqfilter_detach, 1115 .f_event = linux_file_kqfilter_read_event, 1116 }; 1117 1118 static struct filterops linux_dev_kqfiltops_write = { 1119 .f_isfd = 1, 1120 .f_detach = linux_file_kqfilter_detach, 1121 .f_event = linux_file_kqfilter_write_event, 1122 }; 1123 1124 static void 1125 linux_file_kqfilter_poll(struct linux_file *filp, int kqflags) 1126 { 1127 struct thread *td; 1128 const struct file_operations *fop; 1129 struct linux_cdev *ldev; 1130 int temp; 1131 1132 if ((filp->f_kqflags & kqflags) == 0) 1133 return; 1134 1135 td = curthread; 1136 1137 linux_get_fop(filp, &fop, &ldev); 1138 /* get the latest polling state */ 1139 temp = OPW(filp->_file, td, fop->poll(filp, NULL)); 1140 linux_drop_fop(ldev); 1141 1142 spin_lock(&filp->f_kqlock); 1143 /* clear kqflags */ 1144 filp->f_kqflags &= ~(LINUX_KQ_FLAG_NEED_READ | 1145 LINUX_KQ_FLAG_NEED_WRITE); 1146 /* update kqflags */ 1147 if ((temp & (POLLIN | POLLOUT)) != 0) { 1148 if ((temp & POLLIN) != 0) 1149 filp->f_kqflags |= LINUX_KQ_FLAG_NEED_READ; 1150 if ((temp & POLLOUT) != 0) 1151 filp->f_kqflags |= LINUX_KQ_FLAG_NEED_WRITE; 1152 1153 /* make sure the "knote" gets woken up */ 1154 KNOTE_LOCKED(&filp->f_selinfo.si_note, 0); 1155 } 1156 spin_unlock(&filp->f_kqlock); 1157 } 1158 1159 static int 1160 linux_file_kqfilter(struct file *file, struct knote *kn) 1161 { 1162 struct linux_file *filp; 1163 struct thread *td; 1164 int error; 1165 1166 td = curthread; 1167 filp = (struct linux_file *)file->f_data; 1168 filp->f_flags = file->f_flag; 1169 if (filp->f_op->poll == NULL) 1170 return (EINVAL); 1171 1172 spin_lock(&filp->f_kqlock); 1173 switch (kn->kn_filter) { 1174 case EVFILT_READ: 1175 filp->f_kqflags |= LINUX_KQ_FLAG_HAS_READ; 1176 kn->kn_fop = &linux_dev_kqfiltops_read; 1177 kn->kn_hook = filp; 1178 knlist_add(&filp->f_selinfo.si_note, kn, 1); 1179 error = 0; 1180 break; 1181 case EVFILT_WRITE: 1182 filp->f_kqflags |= LINUX_KQ_FLAG_HAS_WRITE; 1183 kn->kn_fop = &linux_dev_kqfiltops_write; 1184 kn->kn_hook = filp; 1185 knlist_add(&filp->f_selinfo.si_note, kn, 1); 1186 error = 0; 1187 break; 1188 default: 1189 error = EINVAL; 1190 break; 1191 } 1192 spin_unlock(&filp->f_kqlock); 1193 1194 if (error == 0) { 1195 linux_set_current(td); 1196 1197 /* update kqfilter status, if any */ 1198 linux_file_kqfilter_poll(filp, 1199 LINUX_KQ_FLAG_HAS_READ | LINUX_KQ_FLAG_HAS_WRITE); 1200 } 1201 return (error); 1202 } 1203 1204 static int 1205 linux_file_mmap_single(struct file *fp, const struct file_operations *fop, 1206 vm_ooffset_t *offset, vm_size_t size, struct vm_object **object, 1207 int nprot, bool is_shared, struct thread *td) 1208 { 1209 struct task_struct *task; 1210 struct vm_area_struct *vmap; 1211 struct mm_struct *mm; 1212 struct linux_file *filp; 1213 vm_memattr_t attr; 1214 int error; 1215 1216 filp = (struct linux_file *)fp->f_data; 1217 filp->f_flags = fp->f_flag; 1218 1219 if (fop->mmap == NULL) 1220 return (EOPNOTSUPP); 1221 1222 linux_set_current(td); 1223 1224 /* 1225 * The same VM object might be shared by multiple processes 1226 * and the mm_struct is usually freed when a process exits. 1227 * 1228 * The atomic reference below makes sure the mm_struct is 1229 * available as long as the vmap is in the linux_vma_head. 1230 */ 1231 task = current; 1232 mm = task->mm; 1233 if (atomic_inc_not_zero(&mm->mm_users) == 0) 1234 return (EINVAL); 1235 1236 vmap = kzalloc(sizeof(*vmap), GFP_KERNEL); 1237 vmap->vm_start = 0; 1238 vmap->vm_end = size; 1239 vmap->vm_pgoff = *offset / PAGE_SIZE; 1240 vmap->vm_pfn = 0; 1241 vmap->vm_flags = vmap->vm_page_prot = (nprot & VM_PROT_ALL); 1242 if (is_shared) 1243 vmap->vm_flags |= VM_SHARED; 1244 vmap->vm_ops = NULL; 1245 vmap->vm_file = get_file(filp); 1246 vmap->vm_mm = mm; 1247 1248 if (unlikely(down_write_killable(&vmap->vm_mm->mmap_sem))) { 1249 error = linux_get_error(task, EINTR); 1250 } else { 1251 error = -OPW(fp, td, fop->mmap(filp, vmap)); 1252 error = linux_get_error(task, error); 1253 up_write(&vmap->vm_mm->mmap_sem); 1254 } 1255 1256 if (error != 0) { 1257 linux_cdev_handle_free(vmap); 1258 return (error); 1259 } 1260 1261 attr = pgprot2cachemode(vmap->vm_page_prot); 1262 1263 if (vmap->vm_ops != NULL) { 1264 struct vm_area_struct *ptr; 1265 void *vm_private_data; 1266 bool vm_no_fault; 1267 1268 if (vmap->vm_ops->open == NULL || 1269 vmap->vm_ops->close == NULL || 1270 vmap->vm_private_data == NULL) { 1271 /* free allocated VM area struct */ 1272 linux_cdev_handle_free(vmap); 1273 return (EINVAL); 1274 } 1275 1276 vm_private_data = vmap->vm_private_data; 1277 1278 rw_wlock(&linux_vma_lock); 1279 TAILQ_FOREACH(ptr, &linux_vma_head, vm_entry) { 1280 if (ptr->vm_private_data == vm_private_data) 1281 break; 1282 } 1283 /* check if there is an existing VM area struct */ 1284 if (ptr != NULL) { 1285 /* check if the VM area structure is invalid */ 1286 if (ptr->vm_ops == NULL || 1287 ptr->vm_ops->open == NULL || 1288 ptr->vm_ops->close == NULL) { 1289 error = ESTALE; 1290 vm_no_fault = 1; 1291 } else { 1292 error = EEXIST; 1293 vm_no_fault = (ptr->vm_ops->fault == NULL); 1294 } 1295 } else { 1296 /* insert VM area structure into list */ 1297 TAILQ_INSERT_TAIL(&linux_vma_head, vmap, vm_entry); 1298 error = 0; 1299 vm_no_fault = (vmap->vm_ops->fault == NULL); 1300 } 1301 rw_wunlock(&linux_vma_lock); 1302 1303 if (error != 0) { 1304 /* free allocated VM area struct */ 1305 linux_cdev_handle_free(vmap); 1306 /* check for stale VM area struct */ 1307 if (error != EEXIST) 1308 return (error); 1309 } 1310 1311 /* check if there is no fault handler */ 1312 if (vm_no_fault) { 1313 *object = cdev_pager_allocate(vm_private_data, OBJT_DEVICE, 1314 &linux_cdev_pager_ops[1], size, nprot, *offset, 1315 td->td_ucred); 1316 } else { 1317 *object = cdev_pager_allocate(vm_private_data, OBJT_MGTDEVICE, 1318 &linux_cdev_pager_ops[0], size, nprot, *offset, 1319 td->td_ucred); 1320 } 1321 1322 /* check if allocating the VM object failed */ 1323 if (*object == NULL) { 1324 if (error == 0) { 1325 /* remove VM area struct from list */ 1326 linux_cdev_handle_remove(vmap); 1327 /* free allocated VM area struct */ 1328 linux_cdev_handle_free(vmap); 1329 } 1330 return (EINVAL); 1331 } 1332 } else { 1333 struct sglist *sg; 1334 1335 sg = sglist_alloc(1, M_WAITOK); 1336 sglist_append_phys(sg, 1337 (vm_paddr_t)vmap->vm_pfn << PAGE_SHIFT, vmap->vm_len); 1338 1339 *object = vm_pager_allocate(OBJT_SG, sg, vmap->vm_len, 1340 nprot, 0, td->td_ucred); 1341 1342 linux_cdev_handle_free(vmap); 1343 1344 if (*object == NULL) { 1345 sglist_free(sg); 1346 return (EINVAL); 1347 } 1348 } 1349 1350 if (attr != VM_MEMATTR_DEFAULT) { 1351 VM_OBJECT_WLOCK(*object); 1352 vm_object_set_memattr(*object, attr); 1353 VM_OBJECT_WUNLOCK(*object); 1354 } 1355 *offset = 0; 1356 return (0); 1357 } 1358 1359 struct cdevsw linuxcdevsw = { 1360 .d_version = D_VERSION, 1361 .d_fdopen = linux_dev_fdopen, 1362 .d_name = "lkpidev", 1363 }; 1364 1365 static int 1366 linux_file_read(struct file *file, struct uio *uio, struct ucred *active_cred, 1367 int flags, struct thread *td) 1368 { 1369 struct linux_file *filp; 1370 const struct file_operations *fop; 1371 struct linux_cdev *ldev; 1372 ssize_t bytes; 1373 int error; 1374 1375 error = 0; 1376 filp = (struct linux_file *)file->f_data; 1377 filp->f_flags = file->f_flag; 1378 /* XXX no support for I/O vectors currently */ 1379 if (uio->uio_iovcnt != 1) 1380 return (EOPNOTSUPP); 1381 if (uio->uio_resid > DEVFS_IOSIZE_MAX) 1382 return (EINVAL); 1383 linux_set_current(td); 1384 linux_get_fop(filp, &fop, &ldev); 1385 if (fop->read != NULL) { 1386 bytes = OPW(file, td, fop->read(filp, 1387 uio->uio_iov->iov_base, 1388 uio->uio_iov->iov_len, &uio->uio_offset)); 1389 if (bytes >= 0) { 1390 uio->uio_iov->iov_base = 1391 ((uint8_t *)uio->uio_iov->iov_base) + bytes; 1392 uio->uio_iov->iov_len -= bytes; 1393 uio->uio_resid -= bytes; 1394 } else { 1395 error = linux_get_error(current, -bytes); 1396 } 1397 } else 1398 error = ENXIO; 1399 1400 /* update kqfilter status, if any */ 1401 linux_file_kqfilter_poll(filp, LINUX_KQ_FLAG_HAS_READ); 1402 linux_drop_fop(ldev); 1403 1404 return (error); 1405 } 1406 1407 static int 1408 linux_file_write(struct file *file, struct uio *uio, struct ucred *active_cred, 1409 int flags, struct thread *td) 1410 { 1411 struct linux_file *filp; 1412 const struct file_operations *fop; 1413 struct linux_cdev *ldev; 1414 ssize_t bytes; 1415 int error; 1416 1417 filp = (struct linux_file *)file->f_data; 1418 filp->f_flags = file->f_flag; 1419 /* XXX no support for I/O vectors currently */ 1420 if (uio->uio_iovcnt != 1) 1421 return (EOPNOTSUPP); 1422 if (uio->uio_resid > DEVFS_IOSIZE_MAX) 1423 return (EINVAL); 1424 linux_set_current(td); 1425 linux_get_fop(filp, &fop, &ldev); 1426 if (fop->write != NULL) { 1427 bytes = OPW(file, td, fop->write(filp, 1428 uio->uio_iov->iov_base, 1429 uio->uio_iov->iov_len, &uio->uio_offset)); 1430 if (bytes >= 0) { 1431 uio->uio_iov->iov_base = 1432 ((uint8_t *)uio->uio_iov->iov_base) + bytes; 1433 uio->uio_iov->iov_len -= bytes; 1434 uio->uio_resid -= bytes; 1435 error = 0; 1436 } else { 1437 error = linux_get_error(current, -bytes); 1438 } 1439 } else 1440 error = ENXIO; 1441 1442 /* update kqfilter status, if any */ 1443 linux_file_kqfilter_poll(filp, LINUX_KQ_FLAG_HAS_WRITE); 1444 1445 linux_drop_fop(ldev); 1446 1447 return (error); 1448 } 1449 1450 static int 1451 linux_file_poll(struct file *file, int events, struct ucred *active_cred, 1452 struct thread *td) 1453 { 1454 struct linux_file *filp; 1455 const struct file_operations *fop; 1456 struct linux_cdev *ldev; 1457 int revents; 1458 1459 filp = (struct linux_file *)file->f_data; 1460 filp->f_flags = file->f_flag; 1461 linux_set_current(td); 1462 linux_get_fop(filp, &fop, &ldev); 1463 if (fop->poll != NULL) { 1464 revents = OPW(file, td, fop->poll(filp, 1465 LINUX_POLL_TABLE_NORMAL)) & events; 1466 } else { 1467 revents = 0; 1468 } 1469 linux_drop_fop(ldev); 1470 return (revents); 1471 } 1472 1473 static int 1474 linux_file_close(struct file *file, struct thread *td) 1475 { 1476 struct linux_file *filp; 1477 int (*release)(struct inode *, struct linux_file *); 1478 const struct file_operations *fop; 1479 struct linux_cdev *ldev; 1480 int error; 1481 1482 filp = (struct linux_file *)file->f_data; 1483 1484 KASSERT(file_count(filp) == 0, 1485 ("File refcount(%d) is not zero", file_count(filp))); 1486 1487 if (td == NULL) 1488 td = curthread; 1489 1490 error = 0; 1491 filp->f_flags = file->f_flag; 1492 linux_set_current(td); 1493 linux_poll_wait_dequeue(filp); 1494 linux_get_fop(filp, &fop, &ldev); 1495 /* 1496 * Always use the real release function, if any, to avoid 1497 * leaking device resources: 1498 */ 1499 release = filp->f_op->release; 1500 if (release != NULL) 1501 error = -OPW(file, td, release(filp->f_vnode, filp)); 1502 funsetown(&filp->f_sigio); 1503 if (filp->f_vnode != NULL) 1504 vdrop(filp->f_vnode); 1505 linux_drop_fop(ldev); 1506 ldev = filp->f_cdev; 1507 if (ldev != NULL) 1508 linux_cdev_deref(ldev); 1509 linux_synchronize_rcu(RCU_TYPE_REGULAR); 1510 kfree(filp); 1511 1512 return (error); 1513 } 1514 1515 static int 1516 linux_file_ioctl(struct file *fp, u_long cmd, void *data, struct ucred *cred, 1517 struct thread *td) 1518 { 1519 struct linux_file *filp; 1520 const struct file_operations *fop; 1521 struct linux_cdev *ldev; 1522 struct fiodgname_arg *fgn; 1523 const char *p; 1524 int error, i; 1525 1526 error = 0; 1527 filp = (struct linux_file *)fp->f_data; 1528 filp->f_flags = fp->f_flag; 1529 linux_get_fop(filp, &fop, &ldev); 1530 1531 linux_set_current(td); 1532 switch (cmd) { 1533 case FIONBIO: 1534 break; 1535 case FIOASYNC: 1536 if (fop->fasync == NULL) 1537 break; 1538 error = -OPW(fp, td, fop->fasync(0, filp, fp->f_flag & FASYNC)); 1539 break; 1540 case FIOSETOWN: 1541 error = fsetown(*(int *)data, &filp->f_sigio); 1542 if (error == 0) { 1543 if (fop->fasync == NULL) 1544 break; 1545 error = -OPW(fp, td, fop->fasync(0, filp, 1546 fp->f_flag & FASYNC)); 1547 } 1548 break; 1549 case FIOGETOWN: 1550 *(int *)data = fgetown(&filp->f_sigio); 1551 break; 1552 case FIODGNAME: 1553 #ifdef COMPAT_FREEBSD32 1554 case FIODGNAME_32: 1555 #endif 1556 if (filp->f_cdev == NULL || filp->f_cdev->cdev == NULL) { 1557 error = ENXIO; 1558 break; 1559 } 1560 fgn = data; 1561 p = devtoname(filp->f_cdev->cdev); 1562 i = strlen(p) + 1; 1563 if (i > fgn->len) { 1564 error = EINVAL; 1565 break; 1566 } 1567 error = copyout(p, fiodgname_buf_get_ptr(fgn, cmd), i); 1568 break; 1569 default: 1570 error = linux_file_ioctl_sub(fp, filp, fop, cmd, data, td); 1571 break; 1572 } 1573 linux_drop_fop(ldev); 1574 return (error); 1575 } 1576 1577 static int 1578 linux_file_mmap_sub(struct thread *td, vm_size_t objsize, vm_prot_t prot, 1579 vm_prot_t maxprot, int flags, struct file *fp, 1580 vm_ooffset_t *foff, const struct file_operations *fop, vm_object_t *objp) 1581 { 1582 /* 1583 * Character devices do not provide private mappings 1584 * of any kind: 1585 */ 1586 if ((maxprot & VM_PROT_WRITE) == 0 && 1587 (prot & VM_PROT_WRITE) != 0) 1588 return (EACCES); 1589 if ((flags & (MAP_PRIVATE | MAP_COPY)) != 0) 1590 return (EINVAL); 1591 1592 return (linux_file_mmap_single(fp, fop, foff, objsize, objp, 1593 (int)prot, (flags & MAP_SHARED) ? true : false, td)); 1594 } 1595 1596 static int 1597 linux_file_mmap(struct file *fp, vm_map_t map, vm_offset_t *addr, vm_size_t size, 1598 vm_prot_t prot, vm_prot_t cap_maxprot, int flags, vm_ooffset_t foff, 1599 struct thread *td) 1600 { 1601 struct linux_file *filp; 1602 const struct file_operations *fop; 1603 struct linux_cdev *ldev; 1604 struct mount *mp; 1605 struct vnode *vp; 1606 vm_object_t object; 1607 vm_prot_t maxprot; 1608 int error; 1609 1610 filp = (struct linux_file *)fp->f_data; 1611 1612 vp = filp->f_vnode; 1613 if (vp == NULL) 1614 return (EOPNOTSUPP); 1615 1616 /* 1617 * Ensure that file and memory protections are 1618 * compatible. 1619 */ 1620 mp = vp->v_mount; 1621 if (mp != NULL && (mp->mnt_flag & MNT_NOEXEC) != 0) { 1622 maxprot = VM_PROT_NONE; 1623 if ((prot & VM_PROT_EXECUTE) != 0) 1624 return (EACCES); 1625 } else 1626 maxprot = VM_PROT_EXECUTE; 1627 if ((fp->f_flag & FREAD) != 0) 1628 maxprot |= VM_PROT_READ; 1629 else if ((prot & VM_PROT_READ) != 0) 1630 return (EACCES); 1631 1632 /* 1633 * If we are sharing potential changes via MAP_SHARED and we 1634 * are trying to get write permission although we opened it 1635 * without asking for it, bail out. 1636 * 1637 * Note that most character devices always share mappings. 1638 * 1639 * Rely on linux_file_mmap_sub() to fail invalid MAP_PRIVATE 1640 * requests rather than doing it here. 1641 */ 1642 if ((flags & MAP_SHARED) != 0) { 1643 if ((fp->f_flag & FWRITE) != 0) 1644 maxprot |= VM_PROT_WRITE; 1645 else if ((prot & VM_PROT_WRITE) != 0) 1646 return (EACCES); 1647 } 1648 maxprot &= cap_maxprot; 1649 1650 linux_get_fop(filp, &fop, &ldev); 1651 error = linux_file_mmap_sub(td, size, prot, maxprot, flags, fp, 1652 &foff, fop, &object); 1653 if (error != 0) 1654 goto out; 1655 1656 error = vm_mmap_object(map, addr, size, prot, maxprot, flags, object, 1657 foff, FALSE, td); 1658 if (error != 0) 1659 vm_object_deallocate(object); 1660 out: 1661 linux_drop_fop(ldev); 1662 return (error); 1663 } 1664 1665 static int 1666 linux_file_stat(struct file *fp, struct stat *sb, struct ucred *active_cred) 1667 { 1668 struct linux_file *filp; 1669 struct vnode *vp; 1670 int error; 1671 1672 filp = (struct linux_file *)fp->f_data; 1673 if (filp->f_vnode == NULL) 1674 return (EOPNOTSUPP); 1675 1676 vp = filp->f_vnode; 1677 1678 vn_lock(vp, LK_SHARED | LK_RETRY); 1679 error = VOP_STAT(vp, sb, curthread->td_ucred, NOCRED); 1680 VOP_UNLOCK(vp); 1681 1682 return (error); 1683 } 1684 1685 static int 1686 linux_file_fill_kinfo(struct file *fp, struct kinfo_file *kif, 1687 struct filedesc *fdp) 1688 { 1689 struct linux_file *filp; 1690 struct vnode *vp; 1691 int error; 1692 1693 filp = fp->f_data; 1694 vp = filp->f_vnode; 1695 if (vp == NULL) { 1696 error = 0; 1697 kif->kf_type = KF_TYPE_DEV; 1698 } else { 1699 vref(vp); 1700 FILEDESC_SUNLOCK(fdp); 1701 error = vn_fill_kinfo_vnode(vp, kif); 1702 vrele(vp); 1703 kif->kf_type = KF_TYPE_VNODE; 1704 FILEDESC_SLOCK(fdp); 1705 } 1706 return (error); 1707 } 1708 1709 unsigned int 1710 linux_iminor(struct inode *inode) 1711 { 1712 struct linux_cdev *ldev; 1713 1714 if (inode == NULL || inode->v_rdev == NULL || 1715 inode->v_rdev->si_devsw != &linuxcdevsw) 1716 return (-1U); 1717 ldev = inode->v_rdev->si_drv1; 1718 if (ldev == NULL) 1719 return (-1U); 1720 1721 return (minor(ldev->dev)); 1722 } 1723 1724 static int 1725 linux_file_kcmp(struct file *fp1, struct file *fp2, struct thread *td) 1726 { 1727 struct linux_file *filp1, *filp2; 1728 1729 if (fp2->f_type != DTYPE_DEV) 1730 return (3); 1731 1732 filp1 = fp1->f_data; 1733 filp2 = fp2->f_data; 1734 return (kcmp_cmp((uintptr_t)filp1->f_cdev, (uintptr_t)filp2->f_cdev)); 1735 } 1736 1737 struct fileops linuxfileops = { 1738 .fo_read = linux_file_read, 1739 .fo_write = linux_file_write, 1740 .fo_truncate = invfo_truncate, 1741 .fo_kqfilter = linux_file_kqfilter, 1742 .fo_stat = linux_file_stat, 1743 .fo_fill_kinfo = linux_file_fill_kinfo, 1744 .fo_poll = linux_file_poll, 1745 .fo_close = linux_file_close, 1746 .fo_ioctl = linux_file_ioctl, 1747 .fo_mmap = linux_file_mmap, 1748 .fo_chmod = invfo_chmod, 1749 .fo_chown = invfo_chown, 1750 .fo_sendfile = invfo_sendfile, 1751 .fo_cmp = linux_file_kcmp, 1752 .fo_flags = DFLAG_PASSABLE, 1753 }; 1754 1755 /* 1756 * Hash of vmmap addresses. This is infrequently accessed and does not 1757 * need to be particularly large. This is done because we must store the 1758 * caller's idea of the map size to properly unmap. 1759 */ 1760 struct vmmap { 1761 LIST_ENTRY(vmmap) vm_next; 1762 void *vm_addr; 1763 unsigned long vm_size; 1764 }; 1765 1766 struct vmmaphd { 1767 struct vmmap *lh_first; 1768 }; 1769 #define VMMAP_HASH_SIZE 64 1770 #define VMMAP_HASH_MASK (VMMAP_HASH_SIZE - 1) 1771 #define VM_HASH(addr) ((uintptr_t)(addr) >> PAGE_SHIFT) & VMMAP_HASH_MASK 1772 static struct vmmaphd vmmaphead[VMMAP_HASH_SIZE]; 1773 static struct mtx vmmaplock; 1774 1775 static void 1776 vmmap_add(void *addr, unsigned long size) 1777 { 1778 struct vmmap *vmmap; 1779 1780 vmmap = kmalloc(sizeof(*vmmap), GFP_KERNEL); 1781 mtx_lock(&vmmaplock); 1782 vmmap->vm_size = size; 1783 vmmap->vm_addr = addr; 1784 LIST_INSERT_HEAD(&vmmaphead[VM_HASH(addr)], vmmap, vm_next); 1785 mtx_unlock(&vmmaplock); 1786 } 1787 1788 static struct vmmap * 1789 vmmap_remove(void *addr) 1790 { 1791 struct vmmap *vmmap; 1792 1793 mtx_lock(&vmmaplock); 1794 LIST_FOREACH(vmmap, &vmmaphead[VM_HASH(addr)], vm_next) 1795 if (vmmap->vm_addr == addr) 1796 break; 1797 if (vmmap) 1798 LIST_REMOVE(vmmap, vm_next); 1799 mtx_unlock(&vmmaplock); 1800 1801 return (vmmap); 1802 } 1803 1804 #if defined(__i386__) || defined(__amd64__) || defined(__powerpc__) || defined(__aarch64__) || defined(__riscv) 1805 void * 1806 _ioremap_attr(vm_paddr_t phys_addr, unsigned long size, int attr) 1807 { 1808 void *addr; 1809 1810 addr = pmap_mapdev_attr(phys_addr, size, attr); 1811 if (addr == NULL) 1812 return (NULL); 1813 vmmap_add(addr, size); 1814 1815 return (addr); 1816 } 1817 #endif 1818 1819 void 1820 iounmap(void *addr) 1821 { 1822 struct vmmap *vmmap; 1823 1824 vmmap = vmmap_remove(addr); 1825 if (vmmap == NULL) 1826 return; 1827 #if defined(__i386__) || defined(__amd64__) || defined(__powerpc__) || defined(__aarch64__) || defined(__riscv) 1828 pmap_unmapdev(addr, vmmap->vm_size); 1829 #endif 1830 kfree(vmmap); 1831 } 1832 1833 void * 1834 vmap(struct page **pages, unsigned int count, unsigned long flags, int prot) 1835 { 1836 vm_offset_t off; 1837 size_t size; 1838 1839 size = count * PAGE_SIZE; 1840 off = kva_alloc(size); 1841 if (off == 0) 1842 return (NULL); 1843 vmmap_add((void *)off, size); 1844 pmap_qenter(off, pages, count); 1845 1846 return ((void *)off); 1847 } 1848 1849 void 1850 vunmap(void *addr) 1851 { 1852 struct vmmap *vmmap; 1853 1854 vmmap = vmmap_remove(addr); 1855 if (vmmap == NULL) 1856 return; 1857 pmap_qremove((vm_offset_t)addr, vmmap->vm_size / PAGE_SIZE); 1858 kva_free((vm_offset_t)addr, vmmap->vm_size); 1859 kfree(vmmap); 1860 } 1861 1862 static char * 1863 devm_kvasprintf(struct device *dev, gfp_t gfp, const char *fmt, va_list ap) 1864 { 1865 unsigned int len; 1866 char *p; 1867 va_list aq; 1868 1869 va_copy(aq, ap); 1870 len = vsnprintf(NULL, 0, fmt, aq); 1871 va_end(aq); 1872 1873 if (dev != NULL) 1874 p = devm_kmalloc(dev, len + 1, gfp); 1875 else 1876 p = kmalloc(len + 1, gfp); 1877 if (p != NULL) 1878 vsnprintf(p, len + 1, fmt, ap); 1879 1880 return (p); 1881 } 1882 1883 char * 1884 kvasprintf(gfp_t gfp, const char *fmt, va_list ap) 1885 { 1886 1887 return (devm_kvasprintf(NULL, gfp, fmt, ap)); 1888 } 1889 1890 char * 1891 lkpi_devm_kasprintf(struct device *dev, gfp_t gfp, const char *fmt, ...) 1892 { 1893 va_list ap; 1894 char *p; 1895 1896 va_start(ap, fmt); 1897 p = devm_kvasprintf(dev, gfp, fmt, ap); 1898 va_end(ap); 1899 1900 return (p); 1901 } 1902 1903 char * 1904 kasprintf(gfp_t gfp, const char *fmt, ...) 1905 { 1906 va_list ap; 1907 char *p; 1908 1909 va_start(ap, fmt); 1910 p = kvasprintf(gfp, fmt, ap); 1911 va_end(ap); 1912 1913 return (p); 1914 } 1915 1916 static void 1917 linux_timer_callback_wrapper(void *context) 1918 { 1919 struct timer_list *timer; 1920 1921 timer = context; 1922 1923 if (linux_set_current_flags(curthread, M_NOWAIT)) { 1924 /* try again later */ 1925 callout_reset(&timer->callout, 1, 1926 &linux_timer_callback_wrapper, timer); 1927 return; 1928 } 1929 1930 timer->function(timer->data); 1931 } 1932 1933 int 1934 mod_timer(struct timer_list *timer, int expires) 1935 { 1936 int ret; 1937 1938 timer->expires = expires; 1939 ret = callout_reset(&timer->callout, 1940 linux_timer_jiffies_until(expires), 1941 &linux_timer_callback_wrapper, timer); 1942 1943 MPASS(ret == 0 || ret == 1); 1944 1945 return (ret == 1); 1946 } 1947 1948 void 1949 add_timer(struct timer_list *timer) 1950 { 1951 1952 callout_reset(&timer->callout, 1953 linux_timer_jiffies_until(timer->expires), 1954 &linux_timer_callback_wrapper, timer); 1955 } 1956 1957 void 1958 add_timer_on(struct timer_list *timer, int cpu) 1959 { 1960 1961 callout_reset_on(&timer->callout, 1962 linux_timer_jiffies_until(timer->expires), 1963 &linux_timer_callback_wrapper, timer, cpu); 1964 } 1965 1966 int 1967 del_timer(struct timer_list *timer) 1968 { 1969 1970 if (callout_stop(&(timer)->callout) == -1) 1971 return (0); 1972 return (1); 1973 } 1974 1975 int 1976 del_timer_sync(struct timer_list *timer) 1977 { 1978 1979 if (callout_drain(&(timer)->callout) == -1) 1980 return (0); 1981 return (1); 1982 } 1983 1984 int 1985 timer_delete_sync(struct timer_list *timer) 1986 { 1987 1988 return (del_timer_sync(timer)); 1989 } 1990 1991 int 1992 timer_shutdown_sync(struct timer_list *timer) 1993 { 1994 1995 return (del_timer_sync(timer)); 1996 } 1997 1998 /* greatest common divisor, Euclid equation */ 1999 static uint64_t 2000 lkpi_gcd_64(uint64_t a, uint64_t b) 2001 { 2002 uint64_t an; 2003 uint64_t bn; 2004 2005 while (b != 0) { 2006 an = b; 2007 bn = a % b; 2008 a = an; 2009 b = bn; 2010 } 2011 return (a); 2012 } 2013 2014 uint64_t lkpi_nsec2hz_rem; 2015 uint64_t lkpi_nsec2hz_div = 1000000000ULL; 2016 uint64_t lkpi_nsec2hz_max; 2017 2018 uint64_t lkpi_usec2hz_rem; 2019 uint64_t lkpi_usec2hz_div = 1000000ULL; 2020 uint64_t lkpi_usec2hz_max; 2021 2022 uint64_t lkpi_msec2hz_rem; 2023 uint64_t lkpi_msec2hz_div = 1000ULL; 2024 uint64_t lkpi_msec2hz_max; 2025 2026 static void 2027 linux_timer_init(void *arg) 2028 { 2029 uint64_t gcd; 2030 2031 /* 2032 * Compute an internal HZ value which can divide 2**32 to 2033 * avoid timer rounding problems when the tick value wraps 2034 * around 2**32: 2035 */ 2036 linux_timer_hz_mask = 1; 2037 while (linux_timer_hz_mask < (unsigned long)hz) 2038 linux_timer_hz_mask *= 2; 2039 linux_timer_hz_mask--; 2040 2041 /* compute some internal constants */ 2042 2043 lkpi_nsec2hz_rem = hz; 2044 lkpi_usec2hz_rem = hz; 2045 lkpi_msec2hz_rem = hz; 2046 2047 gcd = lkpi_gcd_64(lkpi_nsec2hz_rem, lkpi_nsec2hz_div); 2048 lkpi_nsec2hz_rem /= gcd; 2049 lkpi_nsec2hz_div /= gcd; 2050 lkpi_nsec2hz_max = -1ULL / lkpi_nsec2hz_rem; 2051 2052 gcd = lkpi_gcd_64(lkpi_usec2hz_rem, lkpi_usec2hz_div); 2053 lkpi_usec2hz_rem /= gcd; 2054 lkpi_usec2hz_div /= gcd; 2055 lkpi_usec2hz_max = -1ULL / lkpi_usec2hz_rem; 2056 2057 gcd = lkpi_gcd_64(lkpi_msec2hz_rem, lkpi_msec2hz_div); 2058 lkpi_msec2hz_rem /= gcd; 2059 lkpi_msec2hz_div /= gcd; 2060 lkpi_msec2hz_max = -1ULL / lkpi_msec2hz_rem; 2061 } 2062 SYSINIT(linux_timer, SI_SUB_DRIVERS, SI_ORDER_FIRST, linux_timer_init, NULL); 2063 2064 void 2065 linux_complete_common(struct completion *c, int all) 2066 { 2067 int wakeup_swapper; 2068 2069 sleepq_lock(c); 2070 if (all) { 2071 c->done = UINT_MAX; 2072 wakeup_swapper = sleepq_broadcast(c, SLEEPQ_SLEEP, 0, 0); 2073 } else { 2074 if (c->done != UINT_MAX) 2075 c->done++; 2076 wakeup_swapper = sleepq_signal(c, SLEEPQ_SLEEP, 0, 0); 2077 } 2078 sleepq_release(c); 2079 if (wakeup_swapper) 2080 kick_proc0(); 2081 } 2082 2083 /* 2084 * Indefinite wait for done != 0 with or without signals. 2085 */ 2086 int 2087 linux_wait_for_common(struct completion *c, int flags) 2088 { 2089 struct task_struct *task; 2090 int error; 2091 2092 if (SCHEDULER_STOPPED()) 2093 return (0); 2094 2095 task = current; 2096 2097 if (flags != 0) 2098 flags = SLEEPQ_INTERRUPTIBLE | SLEEPQ_SLEEP; 2099 else 2100 flags = SLEEPQ_SLEEP; 2101 error = 0; 2102 for (;;) { 2103 sleepq_lock(c); 2104 if (c->done) 2105 break; 2106 sleepq_add(c, NULL, "completion", flags, 0); 2107 if (flags & SLEEPQ_INTERRUPTIBLE) { 2108 DROP_GIANT(); 2109 error = -sleepq_wait_sig(c, 0); 2110 PICKUP_GIANT(); 2111 if (error != 0) { 2112 linux_schedule_save_interrupt_value(task, error); 2113 error = -ERESTARTSYS; 2114 goto intr; 2115 } 2116 } else { 2117 DROP_GIANT(); 2118 sleepq_wait(c, 0); 2119 PICKUP_GIANT(); 2120 } 2121 } 2122 if (c->done != UINT_MAX) 2123 c->done--; 2124 sleepq_release(c); 2125 2126 intr: 2127 return (error); 2128 } 2129 2130 /* 2131 * Time limited wait for done != 0 with or without signals. 2132 */ 2133 int 2134 linux_wait_for_timeout_common(struct completion *c, int timeout, int flags) 2135 { 2136 struct task_struct *task; 2137 int end = jiffies + timeout; 2138 int error; 2139 2140 if (SCHEDULER_STOPPED()) 2141 return (0); 2142 2143 task = current; 2144 2145 if (flags != 0) 2146 flags = SLEEPQ_INTERRUPTIBLE | SLEEPQ_SLEEP; 2147 else 2148 flags = SLEEPQ_SLEEP; 2149 2150 for (;;) { 2151 sleepq_lock(c); 2152 if (c->done) 2153 break; 2154 sleepq_add(c, NULL, "completion", flags, 0); 2155 sleepq_set_timeout(c, linux_timer_jiffies_until(end)); 2156 2157 DROP_GIANT(); 2158 if (flags & SLEEPQ_INTERRUPTIBLE) 2159 error = -sleepq_timedwait_sig(c, 0); 2160 else 2161 error = -sleepq_timedwait(c, 0); 2162 PICKUP_GIANT(); 2163 2164 if (error != 0) { 2165 /* check for timeout */ 2166 if (error == -EWOULDBLOCK) { 2167 error = 0; /* timeout */ 2168 } else { 2169 /* signal happened */ 2170 linux_schedule_save_interrupt_value(task, error); 2171 error = -ERESTARTSYS; 2172 } 2173 goto done; 2174 } 2175 } 2176 if (c->done != UINT_MAX) 2177 c->done--; 2178 sleepq_release(c); 2179 2180 /* return how many jiffies are left */ 2181 error = linux_timer_jiffies_until(end); 2182 done: 2183 return (error); 2184 } 2185 2186 int 2187 linux_try_wait_for_completion(struct completion *c) 2188 { 2189 int isdone; 2190 2191 sleepq_lock(c); 2192 isdone = (c->done != 0); 2193 if (c->done != 0 && c->done != UINT_MAX) 2194 c->done--; 2195 sleepq_release(c); 2196 return (isdone); 2197 } 2198 2199 int 2200 linux_completion_done(struct completion *c) 2201 { 2202 int isdone; 2203 2204 sleepq_lock(c); 2205 isdone = (c->done != 0); 2206 sleepq_release(c); 2207 return (isdone); 2208 } 2209 2210 static void 2211 linux_cdev_deref(struct linux_cdev *ldev) 2212 { 2213 if (refcount_release(&ldev->refs) && 2214 ldev->kobj.ktype == &linux_cdev_ktype) 2215 kfree(ldev); 2216 } 2217 2218 static void 2219 linux_cdev_release(struct kobject *kobj) 2220 { 2221 struct linux_cdev *cdev; 2222 struct kobject *parent; 2223 2224 cdev = container_of(kobj, struct linux_cdev, kobj); 2225 parent = kobj->parent; 2226 linux_destroy_dev(cdev); 2227 linux_cdev_deref(cdev); 2228 kobject_put(parent); 2229 } 2230 2231 static void 2232 linux_cdev_static_release(struct kobject *kobj) 2233 { 2234 struct cdev *cdev; 2235 struct linux_cdev *ldev; 2236 2237 ldev = container_of(kobj, struct linux_cdev, kobj); 2238 cdev = ldev->cdev; 2239 if (cdev != NULL) { 2240 destroy_dev(cdev); 2241 ldev->cdev = NULL; 2242 } 2243 kobject_put(kobj->parent); 2244 } 2245 2246 int 2247 linux_cdev_device_add(struct linux_cdev *ldev, struct device *dev) 2248 { 2249 int ret; 2250 2251 if (dev->devt != 0) { 2252 /* Set parent kernel object. */ 2253 ldev->kobj.parent = &dev->kobj; 2254 2255 /* 2256 * Unlike Linux we require the kobject of the 2257 * character device structure to have a valid name 2258 * before calling this function: 2259 */ 2260 if (ldev->kobj.name == NULL) 2261 return (-EINVAL); 2262 2263 ret = cdev_add(ldev, dev->devt, 1); 2264 if (ret) 2265 return (ret); 2266 } 2267 ret = device_add(dev); 2268 if (ret != 0 && dev->devt != 0) 2269 cdev_del(ldev); 2270 return (ret); 2271 } 2272 2273 void 2274 linux_cdev_device_del(struct linux_cdev *ldev, struct device *dev) 2275 { 2276 device_del(dev); 2277 2278 if (dev->devt != 0) 2279 cdev_del(ldev); 2280 } 2281 2282 static void 2283 linux_destroy_dev(struct linux_cdev *ldev) 2284 { 2285 2286 if (ldev->cdev == NULL) 2287 return; 2288 2289 MPASS((ldev->siref & LDEV_SI_DTR) == 0); 2290 MPASS(ldev->kobj.ktype == &linux_cdev_ktype); 2291 2292 atomic_set_int(&ldev->siref, LDEV_SI_DTR); 2293 while ((atomic_load_int(&ldev->siref) & ~LDEV_SI_DTR) != 0) 2294 pause("ldevdtr", hz / 4); 2295 2296 destroy_dev(ldev->cdev); 2297 ldev->cdev = NULL; 2298 } 2299 2300 const struct kobj_type linux_cdev_ktype = { 2301 .release = linux_cdev_release, 2302 }; 2303 2304 const struct kobj_type linux_cdev_static_ktype = { 2305 .release = linux_cdev_static_release, 2306 }; 2307 2308 static void 2309 linux_handle_ifnet_link_event(void *arg, struct ifnet *ifp, int linkstate) 2310 { 2311 struct notifier_block *nb; 2312 struct netdev_notifier_info ni; 2313 2314 nb = arg; 2315 ni.ifp = ifp; 2316 ni.dev = (struct net_device *)ifp; 2317 if (linkstate == LINK_STATE_UP) 2318 nb->notifier_call(nb, NETDEV_UP, &ni); 2319 else 2320 nb->notifier_call(nb, NETDEV_DOWN, &ni); 2321 } 2322 2323 static void 2324 linux_handle_ifnet_arrival_event(void *arg, struct ifnet *ifp) 2325 { 2326 struct notifier_block *nb; 2327 struct netdev_notifier_info ni; 2328 2329 nb = arg; 2330 ni.ifp = ifp; 2331 ni.dev = (struct net_device *)ifp; 2332 nb->notifier_call(nb, NETDEV_REGISTER, &ni); 2333 } 2334 2335 static void 2336 linux_handle_ifnet_departure_event(void *arg, struct ifnet *ifp) 2337 { 2338 struct notifier_block *nb; 2339 struct netdev_notifier_info ni; 2340 2341 nb = arg; 2342 ni.ifp = ifp; 2343 ni.dev = (struct net_device *)ifp; 2344 nb->notifier_call(nb, NETDEV_UNREGISTER, &ni); 2345 } 2346 2347 static void 2348 linux_handle_iflladdr_event(void *arg, struct ifnet *ifp) 2349 { 2350 struct notifier_block *nb; 2351 struct netdev_notifier_info ni; 2352 2353 nb = arg; 2354 ni.ifp = ifp; 2355 ni.dev = (struct net_device *)ifp; 2356 nb->notifier_call(nb, NETDEV_CHANGEADDR, &ni); 2357 } 2358 2359 static void 2360 linux_handle_ifaddr_event(void *arg, struct ifnet *ifp) 2361 { 2362 struct notifier_block *nb; 2363 struct netdev_notifier_info ni; 2364 2365 nb = arg; 2366 ni.ifp = ifp; 2367 ni.dev = (struct net_device *)ifp; 2368 nb->notifier_call(nb, NETDEV_CHANGEIFADDR, &ni); 2369 } 2370 2371 int 2372 register_netdevice_notifier(struct notifier_block *nb) 2373 { 2374 2375 nb->tags[NETDEV_UP] = EVENTHANDLER_REGISTER( 2376 ifnet_link_event, linux_handle_ifnet_link_event, nb, 0); 2377 nb->tags[NETDEV_REGISTER] = EVENTHANDLER_REGISTER( 2378 ifnet_arrival_event, linux_handle_ifnet_arrival_event, nb, 0); 2379 nb->tags[NETDEV_UNREGISTER] = EVENTHANDLER_REGISTER( 2380 ifnet_departure_event, linux_handle_ifnet_departure_event, nb, 0); 2381 nb->tags[NETDEV_CHANGEADDR] = EVENTHANDLER_REGISTER( 2382 iflladdr_event, linux_handle_iflladdr_event, nb, 0); 2383 2384 return (0); 2385 } 2386 2387 int 2388 register_inetaddr_notifier(struct notifier_block *nb) 2389 { 2390 2391 nb->tags[NETDEV_CHANGEIFADDR] = EVENTHANDLER_REGISTER( 2392 ifaddr_event, linux_handle_ifaddr_event, nb, 0); 2393 return (0); 2394 } 2395 2396 int 2397 unregister_netdevice_notifier(struct notifier_block *nb) 2398 { 2399 2400 EVENTHANDLER_DEREGISTER(ifnet_link_event, 2401 nb->tags[NETDEV_UP]); 2402 EVENTHANDLER_DEREGISTER(ifnet_arrival_event, 2403 nb->tags[NETDEV_REGISTER]); 2404 EVENTHANDLER_DEREGISTER(ifnet_departure_event, 2405 nb->tags[NETDEV_UNREGISTER]); 2406 EVENTHANDLER_DEREGISTER(iflladdr_event, 2407 nb->tags[NETDEV_CHANGEADDR]); 2408 2409 return (0); 2410 } 2411 2412 int 2413 unregister_inetaddr_notifier(struct notifier_block *nb) 2414 { 2415 2416 EVENTHANDLER_DEREGISTER(ifaddr_event, 2417 nb->tags[NETDEV_CHANGEIFADDR]); 2418 2419 return (0); 2420 } 2421 2422 struct list_sort_thunk { 2423 int (*cmp)(void *, struct list_head *, struct list_head *); 2424 void *priv; 2425 }; 2426 2427 static inline int 2428 linux_le_cmp(const void *d1, const void *d2, void *priv) 2429 { 2430 struct list_head *le1, *le2; 2431 struct list_sort_thunk *thunk; 2432 2433 thunk = priv; 2434 le1 = *(__DECONST(struct list_head **, d1)); 2435 le2 = *(__DECONST(struct list_head **, d2)); 2436 return ((thunk->cmp)(thunk->priv, le1, le2)); 2437 } 2438 2439 void 2440 list_sort(void *priv, struct list_head *head, int (*cmp)(void *priv, 2441 struct list_head *a, struct list_head *b)) 2442 { 2443 struct list_sort_thunk thunk; 2444 struct list_head **ar, *le; 2445 size_t count, i; 2446 2447 count = 0; 2448 list_for_each(le, head) 2449 count++; 2450 ar = malloc(sizeof(struct list_head *) * count, M_KMALLOC, M_WAITOK); 2451 i = 0; 2452 list_for_each(le, head) 2453 ar[i++] = le; 2454 thunk.cmp = cmp; 2455 thunk.priv = priv; 2456 qsort_r(ar, count, sizeof(struct list_head *), linux_le_cmp, &thunk); 2457 INIT_LIST_HEAD(head); 2458 for (i = 0; i < count; i++) 2459 list_add_tail(ar[i], head); 2460 free(ar, M_KMALLOC); 2461 } 2462 2463 #if defined(__i386__) || defined(__amd64__) 2464 int 2465 linux_wbinvd_on_all_cpus(void) 2466 { 2467 2468 pmap_invalidate_cache(); 2469 return (0); 2470 } 2471 #endif 2472 2473 int 2474 linux_on_each_cpu(void callback(void *), void *data) 2475 { 2476 2477 smp_rendezvous(smp_no_rendezvous_barrier, callback, 2478 smp_no_rendezvous_barrier, data); 2479 return (0); 2480 } 2481 2482 int 2483 linux_in_atomic(void) 2484 { 2485 2486 return ((curthread->td_pflags & TDP_NOFAULTING) != 0); 2487 } 2488 2489 struct linux_cdev * 2490 linux_find_cdev(const char *name, unsigned major, unsigned minor) 2491 { 2492 dev_t dev = MKDEV(major, minor); 2493 struct cdev *cdev; 2494 2495 dev_lock(); 2496 LIST_FOREACH(cdev, &linuxcdevsw.d_devs, si_list) { 2497 struct linux_cdev *ldev = cdev->si_drv1; 2498 if (ldev->dev == dev && 2499 strcmp(kobject_name(&ldev->kobj), name) == 0) { 2500 break; 2501 } 2502 } 2503 dev_unlock(); 2504 2505 return (cdev != NULL ? cdev->si_drv1 : NULL); 2506 } 2507 2508 int 2509 __register_chrdev(unsigned int major, unsigned int baseminor, 2510 unsigned int count, const char *name, 2511 const struct file_operations *fops) 2512 { 2513 struct linux_cdev *cdev; 2514 int ret = 0; 2515 int i; 2516 2517 for (i = baseminor; i < baseminor + count; i++) { 2518 cdev = cdev_alloc(); 2519 cdev->ops = fops; 2520 kobject_set_name(&cdev->kobj, name); 2521 2522 ret = cdev_add(cdev, makedev(major, i), 1); 2523 if (ret != 0) 2524 break; 2525 } 2526 return (ret); 2527 } 2528 2529 int 2530 __register_chrdev_p(unsigned int major, unsigned int baseminor, 2531 unsigned int count, const char *name, 2532 const struct file_operations *fops, uid_t uid, 2533 gid_t gid, int mode) 2534 { 2535 struct linux_cdev *cdev; 2536 int ret = 0; 2537 int i; 2538 2539 for (i = baseminor; i < baseminor + count; i++) { 2540 cdev = cdev_alloc(); 2541 cdev->ops = fops; 2542 kobject_set_name(&cdev->kobj, name); 2543 2544 ret = cdev_add_ext(cdev, makedev(major, i), uid, gid, mode); 2545 if (ret != 0) 2546 break; 2547 } 2548 return (ret); 2549 } 2550 2551 void 2552 __unregister_chrdev(unsigned int major, unsigned int baseminor, 2553 unsigned int count, const char *name) 2554 { 2555 struct linux_cdev *cdevp; 2556 int i; 2557 2558 for (i = baseminor; i < baseminor + count; i++) { 2559 cdevp = linux_find_cdev(name, major, i); 2560 if (cdevp != NULL) 2561 cdev_del(cdevp); 2562 } 2563 } 2564 2565 void 2566 linux_dump_stack(void) 2567 { 2568 #ifdef STACK 2569 struct stack st; 2570 2571 stack_save(&st); 2572 stack_print(&st); 2573 #endif 2574 } 2575 2576 int 2577 linuxkpi_net_ratelimit(void) 2578 { 2579 2580 return (ppsratecheck(&lkpi_net_lastlog, &lkpi_net_curpps, 2581 lkpi_net_maxpps)); 2582 } 2583 2584 struct io_mapping * 2585 io_mapping_create_wc(resource_size_t base, unsigned long size) 2586 { 2587 struct io_mapping *mapping; 2588 2589 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL); 2590 if (mapping == NULL) 2591 return (NULL); 2592 return (io_mapping_init_wc(mapping, base, size)); 2593 } 2594 2595 #if defined(__i386__) || defined(__amd64__) 2596 bool linux_cpu_has_clflush; 2597 struct cpuinfo_x86 boot_cpu_data; 2598 struct cpuinfo_x86 *__cpu_data; 2599 #endif 2600 2601 cpumask_t * 2602 lkpi_get_static_single_cpu_mask(int cpuid) 2603 { 2604 2605 KASSERT((cpuid >= 0 && cpuid <= mp_maxid), ("%s: invalid cpuid %d\n", 2606 __func__, cpuid)); 2607 KASSERT(!CPU_ABSENT(cpuid), ("%s: cpu with cpuid %d is absent\n", 2608 __func__, cpuid)); 2609 2610 return (static_single_cpu_mask[cpuid]); 2611 } 2612 2613 bool 2614 lkpi_xen_initial_domain(void) 2615 { 2616 #ifdef XENHVM 2617 return (xen_initial_domain()); 2618 #else 2619 return (false); 2620 #endif 2621 } 2622 2623 bool 2624 lkpi_xen_pv_domain(void) 2625 { 2626 #ifdef XENHVM 2627 return (xen_pv_domain()); 2628 #else 2629 return (false); 2630 #endif 2631 } 2632 2633 static void 2634 linux_compat_init(void *arg) 2635 { 2636 struct sysctl_oid *rootoid; 2637 int i; 2638 2639 #if defined(__i386__) || defined(__amd64__) 2640 static const uint32_t x86_vendors[X86_VENDOR_NUM] = { 2641 [X86_VENDOR_INTEL] = CPU_VENDOR_INTEL, 2642 [X86_VENDOR_CYRIX] = CPU_VENDOR_CYRIX, 2643 [X86_VENDOR_AMD] = CPU_VENDOR_AMD, 2644 [X86_VENDOR_UMC] = CPU_VENDOR_UMC, 2645 [X86_VENDOR_CENTAUR] = CPU_VENDOR_CENTAUR, 2646 [X86_VENDOR_TRANSMETA] = CPU_VENDOR_TRANSMETA, 2647 [X86_VENDOR_NSC] = CPU_VENDOR_NSC, 2648 [X86_VENDOR_HYGON] = CPU_VENDOR_HYGON, 2649 }; 2650 uint8_t x86_vendor = X86_VENDOR_UNKNOWN; 2651 2652 for (i = 0; i < X86_VENDOR_NUM; i++) { 2653 if (cpu_vendor_id != 0 && cpu_vendor_id == x86_vendors[i]) { 2654 x86_vendor = i; 2655 break; 2656 } 2657 } 2658 linux_cpu_has_clflush = (cpu_feature & CPUID_CLFSH); 2659 boot_cpu_data.x86_clflush_size = cpu_clflush_line_size; 2660 boot_cpu_data.x86_max_cores = mp_ncpus; 2661 boot_cpu_data.x86 = CPUID_TO_FAMILY(cpu_id); 2662 boot_cpu_data.x86_model = CPUID_TO_MODEL(cpu_id); 2663 boot_cpu_data.x86_vendor = x86_vendor; 2664 2665 __cpu_data = mallocarray(mp_maxid + 1, 2666 sizeof(*__cpu_data), M_KMALLOC, M_WAITOK | M_ZERO); 2667 CPU_FOREACH(i) { 2668 __cpu_data[i].x86_clflush_size = cpu_clflush_line_size; 2669 __cpu_data[i].x86_max_cores = mp_ncpus; 2670 __cpu_data[i].x86 = CPUID_TO_FAMILY(cpu_id); 2671 __cpu_data[i].x86_model = CPUID_TO_MODEL(cpu_id); 2672 __cpu_data[i].x86_vendor = x86_vendor; 2673 } 2674 #endif 2675 rw_init(&linux_vma_lock, "lkpi-vma-lock"); 2676 2677 rootoid = SYSCTL_ADD_ROOT_NODE(NULL, 2678 OID_AUTO, "sys", CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, "sys"); 2679 kobject_init(&linux_class_root, &linux_class_ktype); 2680 kobject_set_name(&linux_class_root, "class"); 2681 linux_class_root.oidp = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(rootoid), 2682 OID_AUTO, "class", CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, "class"); 2683 kobject_init(&linux_root_device.kobj, &linux_dev_ktype); 2684 kobject_set_name(&linux_root_device.kobj, "device"); 2685 linux_root_device.kobj.oidp = SYSCTL_ADD_NODE(NULL, 2686 SYSCTL_CHILDREN(rootoid), OID_AUTO, "device", 2687 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "device"); 2688 linux_root_device.bsddev = root_bus; 2689 linux_class_misc.name = "misc"; 2690 class_register(&linux_class_misc); 2691 INIT_LIST_HEAD(&pci_drivers); 2692 INIT_LIST_HEAD(&pci_devices); 2693 spin_lock_init(&pci_lock); 2694 mtx_init(&vmmaplock, "IO Map lock", NULL, MTX_DEF); 2695 for (i = 0; i < VMMAP_HASH_SIZE; i++) 2696 LIST_INIT(&vmmaphead[i]); 2697 init_waitqueue_head(&linux_bit_waitq); 2698 init_waitqueue_head(&linux_var_waitq); 2699 2700 CPU_COPY(&all_cpus, &cpu_online_mask); 2701 /* 2702 * Generate a single-CPU cpumask_t for each CPU (possibly) in the system. 2703 * CPUs are indexed from 0..(mp_maxid). The entry for cpuid 0 will only 2704 * have itself in the cpumask, cupid 1 only itself on entry 1, and so on. 2705 * This is used by cpumask_of() (and possibly others in the future) for, 2706 * e.g., drivers to pass hints to irq_set_affinity_hint(). 2707 */ 2708 static_single_cpu_mask = mallocarray(mp_maxid + 1, 2709 sizeof(static_single_cpu_mask), M_KMALLOC, M_WAITOK | M_ZERO); 2710 2711 /* 2712 * When the number of CPUs reach a threshold, we start to save memory 2713 * given the sets are static by overlapping those having their single 2714 * bit set at same position in a bitset word. Asymptotically, this 2715 * regular scheme is in O(n²) whereas the overlapping one is in O(n) 2716 * only with n being the maximum number of CPUs, so the gain will become 2717 * huge quite quickly. The threshold for 64-bit architectures is 128 2718 * CPUs. 2719 */ 2720 if (mp_ncpus < (2 * _BITSET_BITS)) { 2721 cpumask_t *sscm_ptr; 2722 2723 /* 2724 * This represents 'mp_ncpus * __bitset_words(CPU_SETSIZE) * 2725 * (_BITSET_BITS / 8)' bytes (for comparison with the 2726 * overlapping scheme). 2727 */ 2728 static_single_cpu_mask_lcs = mallocarray(mp_ncpus, 2729 sizeof(*static_single_cpu_mask_lcs), 2730 M_KMALLOC, M_WAITOK | M_ZERO); 2731 2732 sscm_ptr = static_single_cpu_mask_lcs; 2733 CPU_FOREACH(i) { 2734 static_single_cpu_mask[i] = sscm_ptr++; 2735 CPU_SET(i, static_single_cpu_mask[i]); 2736 } 2737 } else { 2738 /* Pointer to a bitset word. */ 2739 __typeof(((cpuset_t *)NULL)->__bits[0]) *bwp; 2740 2741 /* 2742 * Allocate memory for (static) spans of 'cpumask_t' ('cpuset_t' 2743 * really) with a single bit set that can be reused for all 2744 * single CPU masks by making them start at different offsets. 2745 * We need '__bitset_words(CPU_SETSIZE) - 1' bitset words before 2746 * the word having its single bit set, and the same amount 2747 * after. 2748 */ 2749 static_single_cpu_mask_lcs = mallocarray(_BITSET_BITS, 2750 (2 * __bitset_words(CPU_SETSIZE) - 1) * (_BITSET_BITS / 8), 2751 M_KMALLOC, M_WAITOK | M_ZERO); 2752 2753 /* 2754 * We rely below on cpuset_t and the bitset generic 2755 * implementation assigning words in the '__bits' array in the 2756 * same order of bits (i.e., little-endian ordering, not to be 2757 * confused with machine endianness, which concerns bits in 2758 * words and other integers). This is an imperfect test, but it 2759 * will detect a change to big-endian ordering. 2760 */ 2761 _Static_assert( 2762 __bitset_word(_BITSET_BITS + 1, _BITSET_BITS) == 1, 2763 "Assumes a bitset implementation that is little-endian " 2764 "on its words"); 2765 2766 /* Initialize the single bit of each static span. */ 2767 bwp = (__typeof(bwp))static_single_cpu_mask_lcs + 2768 (__bitset_words(CPU_SETSIZE) - 1); 2769 for (i = 0; i < _BITSET_BITS; i++) { 2770 CPU_SET(i, (cpuset_t *)bwp); 2771 bwp += (2 * __bitset_words(CPU_SETSIZE) - 1); 2772 } 2773 2774 /* 2775 * Finally set all CPU masks to the proper word in their 2776 * relevant span. 2777 */ 2778 CPU_FOREACH(i) { 2779 bwp = (__typeof(bwp))static_single_cpu_mask_lcs; 2780 /* Find the non-zero word of the relevant span. */ 2781 bwp += (2 * __bitset_words(CPU_SETSIZE) - 1) * 2782 (i % _BITSET_BITS) + 2783 __bitset_words(CPU_SETSIZE) - 1; 2784 /* Shift to find the CPU mask start. */ 2785 bwp -= (i / _BITSET_BITS); 2786 static_single_cpu_mask[i] = (cpuset_t *)bwp; 2787 } 2788 } 2789 2790 strlcpy(init_uts_ns.name.release, osrelease, sizeof(init_uts_ns.name.release)); 2791 } 2792 SYSINIT(linux_compat, SI_SUB_DRIVERS, SI_ORDER_SECOND, linux_compat_init, NULL); 2793 2794 static void 2795 linux_compat_uninit(void *arg) 2796 { 2797 linux_kobject_kfree_name(&linux_class_root); 2798 linux_kobject_kfree_name(&linux_root_device.kobj); 2799 linux_kobject_kfree_name(&linux_class_misc.kobj); 2800 2801 free(static_single_cpu_mask_lcs, M_KMALLOC); 2802 free(static_single_cpu_mask, M_KMALLOC); 2803 #if defined(__i386__) || defined(__amd64__) 2804 free(__cpu_data, M_KMALLOC); 2805 #endif 2806 2807 mtx_destroy(&vmmaplock); 2808 spin_lock_destroy(&pci_lock); 2809 rw_destroy(&linux_vma_lock); 2810 } 2811 SYSUNINIT(linux_compat, SI_SUB_DRIVERS, SI_ORDER_SECOND, linux_compat_uninit, NULL); 2812 2813 /* 2814 * NOTE: Linux frequently uses "unsigned long" for pointer to integer 2815 * conversion and vice versa, where in FreeBSD "uintptr_t" would be 2816 * used. Assert these types have the same size, else some parts of the 2817 * LinuxKPI may not work like expected: 2818 */ 2819 CTASSERT(sizeof(unsigned long) == sizeof(uintptr_t)); 2820