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