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