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