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