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.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice unmodified, this list of conditions, and the following
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.
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.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include "opt_stack.h"
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>
41 #include <sys/sglist.h>
42 #include <sys/sleepqueue.h>
44 #include <sys/mutex.h>
46 #include <sys/fcntl.h>
48 #include <sys/filio.h>
49 #include <sys/rwlock.h>
51 #include <sys/stack.h>
56 #include <vm/vm_object.h>
57 #include <vm/vm_page.h>
58 #include <vm/vm_pager.h>
60 #include <machine/stdarg.h>
62 #if defined(__i386__) || defined(__amd64__)
63 #include <machine/md_var.h>
66 #include <linux/kobject.h>
67 #include <linux/device.h>
68 #include <linux/slab.h>
69 #include <linux/module.h>
70 #include <linux/moduleparam.h>
71 #include <linux/cdev.h>
72 #include <linux/file.h>
73 #include <linux/sysfs.h>
76 #include <linux/vmalloc.h>
77 #include <linux/netdevice.h>
78 #include <linux/timer.h>
79 #include <linux/interrupt.h>
80 #include <linux/uaccess.h>
81 #include <linux/list.h>
82 #include <linux/kthread.h>
83 #include <linux/kernel.h>
84 #include <linux/compat.h>
85 #include <linux/poll.h>
86 #include <linux/smp.h>
88 #if defined(__i386__) || defined(__amd64__)
92 SYSCTL_NODE(_compat, OID_AUTO, linuxkpi, CTLFLAG_RW, 0, "LinuxKPI parameters");
94 MALLOC_DEFINE(M_KMALLOC, "linux", "Linux kmalloc compat");
96 #include <linux/rbtree.h>
97 /* Undo Linux compat changes. */
101 #define RB_ROOT(head) (head)->rbh_root
103 static struct vm_area_struct *linux_cdev_handle_find(void *handle);
105 struct kobject linux_class_root;
106 struct device linux_root_device;
107 struct class linux_class_misc;
108 struct list_head pci_drivers;
109 struct list_head pci_devices;
112 unsigned long linux_timer_hz_mask;
115 panic_cmp(struct rb_node *one, struct rb_node *two)
120 RB_GENERATE(linux_root, rb_node, __entry, panic_cmp);
123 kobject_set_name_vargs(struct kobject *kobj, const char *fmt, va_list args)
133 if (old && fmt == NULL)
136 /* compute length of string */
137 va_copy(tmp_va, args);
138 len = vsnprintf(&dummy, 0, fmt, tmp_va);
141 /* account for zero termination */
144 /* check for error */
148 /* allocate memory for string */
149 name = kzalloc(len, GFP_KERNEL);
152 vsnprintf(name, len, fmt, args);
155 /* free old string */
158 /* filter new string */
159 for (; *name != '\0'; name++)
166 kobject_set_name(struct kobject *kobj, const char *fmt, ...)
172 error = kobject_set_name_vargs(kobj, fmt, args);
179 kobject_add_complete(struct kobject *kobj, struct kobject *parent)
181 const struct kobj_type *t;
184 kobj->parent = parent;
185 error = sysfs_create_dir(kobj);
186 if (error == 0 && kobj->ktype && kobj->ktype->default_attrs) {
187 struct attribute **attr;
190 for (attr = t->default_attrs; *attr != NULL; attr++) {
191 error = sysfs_create_file(kobj, *attr);
196 sysfs_remove_dir(kobj);
203 kobject_add(struct kobject *kobj, struct kobject *parent, const char *fmt, ...)
209 error = kobject_set_name_vargs(kobj, fmt, args);
214 return kobject_add_complete(kobj, parent);
218 linux_kobject_release(struct kref *kref)
220 struct kobject *kobj;
223 kobj = container_of(kref, struct kobject, kref);
224 sysfs_remove_dir(kobj);
226 if (kobj->ktype && kobj->ktype->release)
227 kobj->ktype->release(kobj);
232 linux_kobject_kfree(struct kobject *kobj)
238 linux_kobject_kfree_name(struct kobject *kobj)
245 const struct kobj_type linux_kfree_type = {
246 .release = linux_kobject_kfree
250 linux_device_release(struct device *dev)
252 pr_debug("linux_device_release: %s\n", dev_name(dev));
257 linux_class_show(struct kobject *kobj, struct attribute *attr, char *buf)
259 struct class_attribute *dattr;
262 dattr = container_of(attr, struct class_attribute, attr);
265 error = dattr->show(container_of(kobj, struct class, kobj),
271 linux_class_store(struct kobject *kobj, struct attribute *attr, const char *buf,
274 struct class_attribute *dattr;
277 dattr = container_of(attr, struct class_attribute, attr);
280 error = dattr->store(container_of(kobj, struct class, kobj),
286 linux_class_release(struct kobject *kobj)
290 class = container_of(kobj, struct class, kobj);
291 if (class->class_release)
292 class->class_release(class);
295 static const struct sysfs_ops linux_class_sysfs = {
296 .show = linux_class_show,
297 .store = linux_class_store,
300 const struct kobj_type linux_class_ktype = {
301 .release = linux_class_release,
302 .sysfs_ops = &linux_class_sysfs
306 linux_dev_release(struct kobject *kobj)
310 dev = container_of(kobj, struct device, kobj);
311 /* This is the precedence defined by linux. */
314 else if (dev->class && dev->class->dev_release)
315 dev->class->dev_release(dev);
319 linux_dev_show(struct kobject *kobj, struct attribute *attr, char *buf)
321 struct device_attribute *dattr;
324 dattr = container_of(attr, struct device_attribute, attr);
327 error = dattr->show(container_of(kobj, struct device, kobj),
333 linux_dev_store(struct kobject *kobj, struct attribute *attr, const char *buf,
336 struct device_attribute *dattr;
339 dattr = container_of(attr, struct device_attribute, attr);
342 error = dattr->store(container_of(kobj, struct device, kobj),
347 static const struct sysfs_ops linux_dev_sysfs = {
348 .show = linux_dev_show,
349 .store = linux_dev_store,
352 const struct kobj_type linux_dev_ktype = {
353 .release = linux_dev_release,
354 .sysfs_ops = &linux_dev_sysfs
358 device_create(struct class *class, struct device *parent, dev_t devt,
359 void *drvdata, const char *fmt, ...)
364 dev = kzalloc(sizeof(*dev), M_WAITOK);
365 dev->parent = parent;
368 dev->driver_data = drvdata;
369 dev->release = linux_device_release;
371 kobject_set_name_vargs(&dev->kobj, fmt, args);
373 device_register(dev);
379 kobject_init_and_add(struct kobject *kobj, const struct kobj_type *ktype,
380 struct kobject *parent, const char *fmt, ...)
385 kobject_init(kobj, ktype);
387 kobj->parent = parent;
391 error = kobject_set_name_vargs(kobj, fmt, args);
395 return kobject_add_complete(kobj, parent);
399 linux_kq_lock(void *arg)
406 linux_kq_unlock(void *arg)
414 linux_kq_lock_owned(void *arg)
419 mtx_assert(&s->m, MA_OWNED);
424 linux_kq_lock_unowned(void *arg)
429 mtx_assert(&s->m, MA_NOTOWNED);
434 linux_file_kqfilter_poll(struct linux_file *, int);
437 linux_file_alloc(void)
439 struct linux_file *filp;
441 filp = kzalloc(sizeof(*filp), GFP_KERNEL);
443 /* set initial refcount */
446 /* setup fields needed by kqueue support */
447 spin_lock_init(&filp->f_kqlock);
448 knlist_init(&filp->f_selinfo.si_note, &filp->f_kqlock,
449 linux_kq_lock, linux_kq_unlock,
450 linux_kq_lock_owned, linux_kq_lock_unowned);
456 linux_file_free(struct linux_file *filp)
458 if (filp->_file == NULL) {
459 if (filp->f_shmem != NULL)
460 vm_object_deallocate(filp->f_shmem);
464 * The close method of the character device or file
465 * will free the linux_file structure:
467 _fdrop(filp->_file, curthread);
472 linux_cdev_pager_fault(vm_object_t vm_obj, vm_ooffset_t offset, int prot,
475 struct vm_area_struct *vmap;
477 vmap = linux_cdev_handle_find(vm_obj->handle);
480 MPASS(vmap->vm_private_data == vm_obj->handle);
482 if (likely(vmap->vm_ops != NULL && offset < vmap->vm_len)) {
483 vm_paddr_t paddr = IDX_TO_OFF(vmap->vm_pfn) + offset;
486 if (((*mres)->flags & PG_FICTITIOUS) != 0) {
488 * If the passed in result page is a fake
489 * page, update it with the new physical
493 vm_page_updatefake(page, paddr, vm_obj->memattr);
496 * Replace the passed in "mres" page with our
497 * own fake page and free up the all of the
500 VM_OBJECT_WUNLOCK(vm_obj);
501 page = vm_page_getfake(paddr, vm_obj->memattr);
502 VM_OBJECT_WLOCK(vm_obj);
504 vm_page_replace_checked(page, vm_obj,
505 (*mres)->pindex, *mres);
509 vm_page_unlock(*mres);
512 page->valid = VM_PAGE_BITS_ALL;
513 return (VM_PAGER_OK);
515 return (VM_PAGER_FAIL);
519 linux_cdev_pager_populate(vm_object_t vm_obj, vm_pindex_t pidx, int fault_type,
520 vm_prot_t max_prot, vm_pindex_t *first, vm_pindex_t *last)
522 struct vm_area_struct *vmap;
525 linux_set_current(curthread);
527 /* get VM area structure */
528 vmap = linux_cdev_handle_find(vm_obj->handle);
530 MPASS(vmap->vm_private_data == vm_obj->handle);
532 VM_OBJECT_WUNLOCK(vm_obj);
534 down_write(&vmap->vm_mm->mmap_sem);
535 if (unlikely(vmap->vm_ops == NULL)) {
536 err = VM_FAULT_SIGBUS;
540 /* fill out VM fault structure */
541 vmf.virtual_address = (void *)(uintptr_t)IDX_TO_OFF(pidx);
542 vmf.flags = (fault_type & VM_PROT_WRITE) ? FAULT_FLAG_WRITE : 0;
547 vmap->vm_pfn_count = 0;
548 vmap->vm_pfn_pcount = &vmap->vm_pfn_count;
549 vmap->vm_obj = vm_obj;
551 err = vmap->vm_ops->fault(vmap, &vmf);
553 while (vmap->vm_pfn_count == 0 && err == VM_FAULT_NOPAGE) {
554 kern_yield(PRI_USER);
555 err = vmap->vm_ops->fault(vmap, &vmf);
559 /* translate return code */
562 err = VM_PAGER_AGAIN;
564 case VM_FAULT_SIGBUS:
567 case VM_FAULT_NOPAGE:
569 * By contract the fault handler will return having
570 * busied all the pages itself. If pidx is already
571 * found in the object, it will simply xbusy the first
572 * page and return with vm_pfn_count set to 1.
574 *first = vmap->vm_pfn_first;
575 *last = *first + vmap->vm_pfn_count - 1;
579 err = VM_PAGER_ERROR;
582 up_write(&vmap->vm_mm->mmap_sem);
583 VM_OBJECT_WLOCK(vm_obj);
587 static struct rwlock linux_vma_lock;
588 static TAILQ_HEAD(, vm_area_struct) linux_vma_head =
589 TAILQ_HEAD_INITIALIZER(linux_vma_head);
592 linux_cdev_handle_free(struct vm_area_struct *vmap)
594 /* Drop reference on vm_file */
595 if (vmap->vm_file != NULL)
598 /* Drop reference on mm_struct */
605 linux_cdev_handle_remove(struct vm_area_struct *vmap)
607 rw_wlock(&linux_vma_lock);
608 TAILQ_REMOVE(&linux_vma_head, vmap, vm_entry);
609 rw_wunlock(&linux_vma_lock);
612 static struct vm_area_struct *
613 linux_cdev_handle_find(void *handle)
615 struct vm_area_struct *vmap;
617 rw_rlock(&linux_vma_lock);
618 TAILQ_FOREACH(vmap, &linux_vma_head, vm_entry) {
619 if (vmap->vm_private_data == handle)
622 rw_runlock(&linux_vma_lock);
627 linux_cdev_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
628 vm_ooffset_t foff, struct ucred *cred, u_short *color)
631 MPASS(linux_cdev_handle_find(handle) != NULL);
637 linux_cdev_pager_dtor(void *handle)
639 const struct vm_operations_struct *vm_ops;
640 struct vm_area_struct *vmap;
642 vmap = linux_cdev_handle_find(handle);
646 * Remove handle before calling close operation to prevent
647 * other threads from reusing the handle pointer.
649 linux_cdev_handle_remove(vmap);
651 down_write(&vmap->vm_mm->mmap_sem);
652 vm_ops = vmap->vm_ops;
653 if (likely(vm_ops != NULL))
655 up_write(&vmap->vm_mm->mmap_sem);
657 linux_cdev_handle_free(vmap);
660 static struct cdev_pager_ops linux_cdev_pager_ops[2] = {
663 .cdev_pg_populate = linux_cdev_pager_populate,
664 .cdev_pg_ctor = linux_cdev_pager_ctor,
665 .cdev_pg_dtor = linux_cdev_pager_dtor
669 .cdev_pg_fault = linux_cdev_pager_fault,
670 .cdev_pg_ctor = linux_cdev_pager_ctor,
671 .cdev_pg_dtor = linux_cdev_pager_dtor
676 zap_vma_ptes(struct vm_area_struct *vma, unsigned long address,
683 if (obj == NULL || (obj->flags & OBJ_UNMANAGED) != 0)
685 VM_OBJECT_RLOCK(obj);
686 for (m = vm_page_find_least(obj, OFF_TO_IDX(address));
687 m != NULL && m->pindex < OFF_TO_IDX(address + size);
688 m = TAILQ_NEXT(m, listq))
690 VM_OBJECT_RUNLOCK(obj);
694 #define OPW(fp,td,code) ({ \
695 struct file *__fpop; \
696 __typeof(code) __retval; \
698 __fpop = (td)->td_fpop; \
699 (td)->td_fpop = (fp); \
701 (td)->td_fpop = __fpop; \
706 linux_dev_fdopen(struct cdev *dev, int fflags, struct thread *td, struct file *file)
708 struct linux_cdev *ldev;
709 struct linux_file *filp;
714 filp = linux_file_alloc();
715 filp->f_dentry = &filp->f_dentry_store;
716 filp->f_op = ldev->ops;
717 filp->f_mode = file->f_flag;
718 filp->f_flags = file->f_flag;
719 filp->f_vnode = file->f_vnode;
723 linux_set_current(td);
725 /* get a reference on the Linux character device */
726 if (atomic_long_add_unless(&ldev->refs, 1, -1L) == 0) {
731 if (filp->f_op->open) {
732 error = -filp->f_op->open(file->f_vnode, filp);
734 atomic_long_dec(&ldev->refs);
740 /* hold on to the vnode - used for fstat() */
741 vhold(filp->f_vnode);
743 /* release the file from devfs */
744 finit(file, filp->f_mode, DTYPE_DEV, filp, &linuxfileops);
748 #define LINUX_IOCTL_MIN_PTR 0x10000UL
749 #define LINUX_IOCTL_MAX_PTR (LINUX_IOCTL_MIN_PTR + IOCPARM_MAX)
752 linux_remap_address(void **uaddr, size_t len)
754 uintptr_t uaddr_val = (uintptr_t)(*uaddr);
756 if (unlikely(uaddr_val >= LINUX_IOCTL_MIN_PTR &&
757 uaddr_val < LINUX_IOCTL_MAX_PTR)) {
758 struct task_struct *pts = current;
764 /* compute data offset */
765 uaddr_val -= LINUX_IOCTL_MIN_PTR;
767 /* check that length is within bounds */
768 if ((len > IOCPARM_MAX) ||
769 (uaddr_val + len) > pts->bsd_ioctl_len) {
774 /* re-add kernel buffer address */
775 uaddr_val += (uintptr_t)pts->bsd_ioctl_data;
777 /* update address location */
778 *uaddr = (void *)uaddr_val;
785 linux_copyin(const void *uaddr, void *kaddr, size_t len)
787 if (linux_remap_address(__DECONST(void **, &uaddr), len)) {
790 memcpy(kaddr, uaddr, len);
793 return (-copyin(uaddr, kaddr, len));
797 linux_copyout(const void *kaddr, void *uaddr, size_t len)
799 if (linux_remap_address(&uaddr, len)) {
802 memcpy(uaddr, kaddr, len);
805 return (-copyout(kaddr, uaddr, len));
809 linux_clear_user(void *_uaddr, size_t _len)
811 uint8_t *uaddr = _uaddr;
814 /* make sure uaddr is aligned before going into the fast loop */
815 while (((uintptr_t)uaddr & 7) != 0 && len > 7) {
816 if (subyte(uaddr, 0))
822 /* zero 8 bytes at a time */
825 if (suword64(uaddr, 0))
828 if (suword32(uaddr, 0))
830 if (suword32(uaddr + 4, 0))
837 /* zero fill end, if any */
839 if (subyte(uaddr, 0))
848 linux_access_ok(int rw, const void *uaddr, size_t len)
853 /* get start and end address */
854 saddr = (uintptr_t)uaddr;
855 eaddr = (uintptr_t)uaddr + len;
857 /* verify addresses are valid for userspace */
858 return ((saddr == eaddr) ||
859 (eaddr > saddr && eaddr <= VM_MAXUSER_ADDRESS));
863 * This function should return either EINTR or ERESTART depending on
864 * the signal type sent to this thread:
867 linux_get_error(struct task_struct *task, int error)
869 /* check for signal type interrupt code */
870 if (error == EINTR || error == ERESTARTSYS || error == ERESTART) {
871 error = -linux_schedule_get_interrupt_value(task);
879 linux_file_ioctl_sub(struct file *fp, struct linux_file *filp,
880 u_long cmd, caddr_t data, struct thread *td)
882 struct task_struct *task = current;
886 size = IOCPARM_LEN(cmd);
887 /* refer to logic in sys_ioctl() */
890 * Setup hint for linux_copyin() and linux_copyout().
892 * Background: Linux code expects a user-space address
893 * while FreeBSD supplies a kernel-space address.
895 task->bsd_ioctl_data = data;
896 task->bsd_ioctl_len = size;
897 data = (void *)LINUX_IOCTL_MIN_PTR;
899 /* fetch user-space pointer */
900 data = *(void **)data;
902 #if defined(__amd64__)
903 if (td->td_proc->p_elf_machine == EM_386) {
904 /* try the compat IOCTL handler first */
905 if (filp->f_op->compat_ioctl != NULL)
906 error = -OPW(fp, td, filp->f_op->compat_ioctl(filp, cmd, (u_long)data));
910 /* fallback to the regular IOCTL handler, if any */
911 if (error == ENOTTY && filp->f_op->unlocked_ioctl != NULL)
912 error = -OPW(fp, td, filp->f_op->unlocked_ioctl(filp, cmd, (u_long)data));
915 if (filp->f_op->unlocked_ioctl != NULL)
916 error = -OPW(fp, td, filp->f_op->unlocked_ioctl(filp, cmd, (u_long)data));
920 task->bsd_ioctl_data = NULL;
921 task->bsd_ioctl_len = 0;
924 if (error == EWOULDBLOCK) {
925 /* update kqfilter status, if any */
926 linux_file_kqfilter_poll(filp,
927 LINUX_KQ_FLAG_HAS_READ | LINUX_KQ_FLAG_HAS_WRITE);
929 error = linux_get_error(task, error);
934 #define LINUX_POLL_TABLE_NORMAL ((poll_table *)1)
937 * This function atomically updates the poll wakeup state and returns
938 * the previous state at the time of update.
941 linux_poll_wakeup_state(atomic_t *v, const uint8_t *pstate)
947 while ((old = atomic_cmpxchg(v, c, pstate[c])) != c)
955 linux_poll_wakeup_callback(wait_queue_t *wq, unsigned int wq_state, int flags, void *key)
957 static const uint8_t state[LINUX_FWQ_STATE_MAX] = {
958 [LINUX_FWQ_STATE_INIT] = LINUX_FWQ_STATE_INIT, /* NOP */
959 [LINUX_FWQ_STATE_NOT_READY] = LINUX_FWQ_STATE_NOT_READY, /* NOP */
960 [LINUX_FWQ_STATE_QUEUED] = LINUX_FWQ_STATE_READY,
961 [LINUX_FWQ_STATE_READY] = LINUX_FWQ_STATE_READY, /* NOP */
963 struct linux_file *filp = container_of(wq, struct linux_file, f_wait_queue.wq);
965 switch (linux_poll_wakeup_state(&filp->f_wait_queue.state, state)) {
966 case LINUX_FWQ_STATE_QUEUED:
967 linux_poll_wakeup(filp);
975 linux_poll_wait(struct linux_file *filp, wait_queue_head_t *wqh, poll_table *p)
977 static const uint8_t state[LINUX_FWQ_STATE_MAX] = {
978 [LINUX_FWQ_STATE_INIT] = LINUX_FWQ_STATE_NOT_READY,
979 [LINUX_FWQ_STATE_NOT_READY] = LINUX_FWQ_STATE_NOT_READY, /* NOP */
980 [LINUX_FWQ_STATE_QUEUED] = LINUX_FWQ_STATE_QUEUED, /* NOP */
981 [LINUX_FWQ_STATE_READY] = LINUX_FWQ_STATE_QUEUED,
984 /* check if we are called inside the select system call */
985 if (p == LINUX_POLL_TABLE_NORMAL)
986 selrecord(curthread, &filp->f_selinfo);
988 switch (linux_poll_wakeup_state(&filp->f_wait_queue.state, state)) {
989 case LINUX_FWQ_STATE_INIT:
990 /* NOTE: file handles can only belong to one wait-queue */
991 filp->f_wait_queue.wqh = wqh;
992 filp->f_wait_queue.wq.func = &linux_poll_wakeup_callback;
993 add_wait_queue(wqh, &filp->f_wait_queue.wq);
994 atomic_set(&filp->f_wait_queue.state, LINUX_FWQ_STATE_QUEUED);
1002 linux_poll_wait_dequeue(struct linux_file *filp)
1004 static const uint8_t state[LINUX_FWQ_STATE_MAX] = {
1005 [LINUX_FWQ_STATE_INIT] = LINUX_FWQ_STATE_INIT, /* NOP */
1006 [LINUX_FWQ_STATE_NOT_READY] = LINUX_FWQ_STATE_INIT,
1007 [LINUX_FWQ_STATE_QUEUED] = LINUX_FWQ_STATE_INIT,
1008 [LINUX_FWQ_STATE_READY] = LINUX_FWQ_STATE_INIT,
1011 seldrain(&filp->f_selinfo);
1013 switch (linux_poll_wakeup_state(&filp->f_wait_queue.state, state)) {
1014 case LINUX_FWQ_STATE_NOT_READY:
1015 case LINUX_FWQ_STATE_QUEUED:
1016 case LINUX_FWQ_STATE_READY:
1017 remove_wait_queue(filp->f_wait_queue.wqh, &filp->f_wait_queue.wq);
1025 linux_poll_wakeup(struct linux_file *filp)
1027 /* this function should be NULL-safe */
1031 selwakeup(&filp->f_selinfo);
1033 spin_lock(&filp->f_kqlock);
1034 filp->f_kqflags |= LINUX_KQ_FLAG_NEED_READ |
1035 LINUX_KQ_FLAG_NEED_WRITE;
1037 /* make sure the "knote" gets woken up */
1038 KNOTE_LOCKED(&filp->f_selinfo.si_note, 1);
1039 spin_unlock(&filp->f_kqlock);
1043 linux_file_kqfilter_detach(struct knote *kn)
1045 struct linux_file *filp = kn->kn_hook;
1047 spin_lock(&filp->f_kqlock);
1048 knlist_remove(&filp->f_selinfo.si_note, kn, 1);
1049 spin_unlock(&filp->f_kqlock);
1053 linux_file_kqfilter_read_event(struct knote *kn, long hint)
1055 struct linux_file *filp = kn->kn_hook;
1057 mtx_assert(&filp->f_kqlock.m, MA_OWNED);
1059 return ((filp->f_kqflags & LINUX_KQ_FLAG_NEED_READ) ? 1 : 0);
1063 linux_file_kqfilter_write_event(struct knote *kn, long hint)
1065 struct linux_file *filp = kn->kn_hook;
1067 mtx_assert(&filp->f_kqlock.m, MA_OWNED);
1069 return ((filp->f_kqflags & LINUX_KQ_FLAG_NEED_WRITE) ? 1 : 0);
1072 static struct filterops linux_dev_kqfiltops_read = {
1074 .f_detach = linux_file_kqfilter_detach,
1075 .f_event = linux_file_kqfilter_read_event,
1078 static struct filterops linux_dev_kqfiltops_write = {
1080 .f_detach = linux_file_kqfilter_detach,
1081 .f_event = linux_file_kqfilter_write_event,
1085 linux_file_kqfilter_poll(struct linux_file *filp, int kqflags)
1089 if (filp->f_kqflags & kqflags) {
1090 struct thread *td = curthread;
1092 /* get the latest polling state */
1093 temp = OPW(filp->_file, td, filp->f_op->poll(filp, NULL));
1095 spin_lock(&filp->f_kqlock);
1097 filp->f_kqflags &= ~(LINUX_KQ_FLAG_NEED_READ |
1098 LINUX_KQ_FLAG_NEED_WRITE);
1099 /* update kqflags */
1100 if (temp & (POLLIN | POLLOUT)) {
1102 filp->f_kqflags |= LINUX_KQ_FLAG_NEED_READ;
1104 filp->f_kqflags |= LINUX_KQ_FLAG_NEED_WRITE;
1106 /* make sure the "knote" gets woken up */
1107 KNOTE_LOCKED(&filp->f_selinfo.si_note, 0);
1109 spin_unlock(&filp->f_kqlock);
1114 linux_file_kqfilter(struct file *file, struct knote *kn)
1116 struct linux_file *filp;
1121 filp = (struct linux_file *)file->f_data;
1122 filp->f_flags = file->f_flag;
1123 if (filp->f_op->poll == NULL)
1126 spin_lock(&filp->f_kqlock);
1127 switch (kn->kn_filter) {
1129 filp->f_kqflags |= LINUX_KQ_FLAG_HAS_READ;
1130 kn->kn_fop = &linux_dev_kqfiltops_read;
1132 knlist_add(&filp->f_selinfo.si_note, kn, 1);
1136 filp->f_kqflags |= LINUX_KQ_FLAG_HAS_WRITE;
1137 kn->kn_fop = &linux_dev_kqfiltops_write;
1139 knlist_add(&filp->f_selinfo.si_note, kn, 1);
1146 spin_unlock(&filp->f_kqlock);
1149 linux_set_current(td);
1151 /* update kqfilter status, if any */
1152 linux_file_kqfilter_poll(filp,
1153 LINUX_KQ_FLAG_HAS_READ | LINUX_KQ_FLAG_HAS_WRITE);
1159 linux_file_mmap_single(struct file *fp, vm_ooffset_t *offset,
1160 vm_size_t size, struct vm_object **object, int nprot,
1163 struct task_struct *task;
1164 struct vm_area_struct *vmap;
1165 struct mm_struct *mm;
1166 struct linux_file *filp;
1170 filp = (struct linux_file *)fp->f_data;
1171 filp->f_flags = fp->f_flag;
1173 if (filp->f_op->mmap == NULL)
1174 return (EOPNOTSUPP);
1176 linux_set_current(td);
1179 * The same VM object might be shared by multiple processes
1180 * and the mm_struct is usually freed when a process exits.
1182 * The atomic reference below makes sure the mm_struct is
1183 * available as long as the vmap is in the linux_vma_head.
1187 if (atomic_inc_not_zero(&mm->mm_users) == 0)
1190 vmap = kzalloc(sizeof(*vmap), GFP_KERNEL);
1192 vmap->vm_end = size;
1193 vmap->vm_pgoff = *offset / PAGE_SIZE;
1195 vmap->vm_flags = vmap->vm_page_prot = (nprot & VM_PROT_ALL);
1196 vmap->vm_ops = NULL;
1197 vmap->vm_file = get_file(filp);
1200 if (unlikely(down_write_killable(&vmap->vm_mm->mmap_sem))) {
1201 error = linux_get_error(task, EINTR);
1203 error = -OPW(fp, td, filp->f_op->mmap(filp, vmap));
1204 error = linux_get_error(task, error);
1205 up_write(&vmap->vm_mm->mmap_sem);
1209 linux_cdev_handle_free(vmap);
1213 attr = pgprot2cachemode(vmap->vm_page_prot);
1215 if (vmap->vm_ops != NULL) {
1216 struct vm_area_struct *ptr;
1217 void *vm_private_data;
1220 if (vmap->vm_ops->open == NULL ||
1221 vmap->vm_ops->close == NULL ||
1222 vmap->vm_private_data == NULL) {
1223 /* free allocated VM area struct */
1224 linux_cdev_handle_free(vmap);
1228 vm_private_data = vmap->vm_private_data;
1230 rw_wlock(&linux_vma_lock);
1231 TAILQ_FOREACH(ptr, &linux_vma_head, vm_entry) {
1232 if (ptr->vm_private_data == vm_private_data)
1235 /* check if there is an existing VM area struct */
1237 /* check if the VM area structure is invalid */
1238 if (ptr->vm_ops == NULL ||
1239 ptr->vm_ops->open == NULL ||
1240 ptr->vm_ops->close == NULL) {
1245 vm_no_fault = (ptr->vm_ops->fault == NULL);
1248 /* insert VM area structure into list */
1249 TAILQ_INSERT_TAIL(&linux_vma_head, vmap, vm_entry);
1251 vm_no_fault = (vmap->vm_ops->fault == NULL);
1253 rw_wunlock(&linux_vma_lock);
1256 /* free allocated VM area struct */
1257 linux_cdev_handle_free(vmap);
1258 /* check for stale VM area struct */
1259 if (error != EEXIST)
1263 /* check if there is no fault handler */
1265 *object = cdev_pager_allocate(vm_private_data, OBJT_DEVICE,
1266 &linux_cdev_pager_ops[1], size, nprot, *offset,
1269 *object = cdev_pager_allocate(vm_private_data, OBJT_MGTDEVICE,
1270 &linux_cdev_pager_ops[0], size, nprot, *offset,
1274 /* check if allocating the VM object failed */
1275 if (*object == NULL) {
1277 /* remove VM area struct from list */
1278 linux_cdev_handle_remove(vmap);
1279 /* free allocated VM area struct */
1280 linux_cdev_handle_free(vmap);
1287 sg = sglist_alloc(1, M_WAITOK);
1288 sglist_append_phys(sg,
1289 (vm_paddr_t)vmap->vm_pfn << PAGE_SHIFT, vmap->vm_len);
1291 *object = vm_pager_allocate(OBJT_SG, sg, vmap->vm_len,
1292 nprot, 0, td->td_ucred);
1294 linux_cdev_handle_free(vmap);
1296 if (*object == NULL) {
1302 if (attr != VM_MEMATTR_DEFAULT) {
1303 VM_OBJECT_WLOCK(*object);
1304 vm_object_set_memattr(*object, attr);
1305 VM_OBJECT_WUNLOCK(*object);
1311 struct cdevsw linuxcdevsw = {
1312 .d_version = D_VERSION,
1313 .d_fdopen = linux_dev_fdopen,
1314 .d_name = "lkpidev",
1318 linux_file_read(struct file *file, struct uio *uio, struct ucred *active_cred,
1319 int flags, struct thread *td)
1321 struct linux_file *filp;
1326 filp = (struct linux_file *)file->f_data;
1327 filp->f_flags = file->f_flag;
1328 /* XXX no support for I/O vectors currently */
1329 if (uio->uio_iovcnt != 1)
1330 return (EOPNOTSUPP);
1331 if (uio->uio_resid > DEVFS_IOSIZE_MAX)
1333 linux_set_current(td);
1334 if (filp->f_op->read) {
1335 bytes = OPW(file, td, filp->f_op->read(filp, uio->uio_iov->iov_base,
1336 uio->uio_iov->iov_len, &uio->uio_offset));
1338 uio->uio_iov->iov_base =
1339 ((uint8_t *)uio->uio_iov->iov_base) + bytes;
1340 uio->uio_iov->iov_len -= bytes;
1341 uio->uio_resid -= bytes;
1343 error = linux_get_error(current, -bytes);
1348 /* update kqfilter status, if any */
1349 linux_file_kqfilter_poll(filp, LINUX_KQ_FLAG_HAS_READ);
1355 linux_file_write(struct file *file, struct uio *uio, struct ucred *active_cred,
1356 int flags, struct thread *td)
1358 struct linux_file *filp;
1363 filp = (struct linux_file *)file->f_data;
1364 filp->f_flags = file->f_flag;
1365 /* XXX no support for I/O vectors currently */
1366 if (uio->uio_iovcnt != 1)
1367 return (EOPNOTSUPP);
1368 if (uio->uio_resid > DEVFS_IOSIZE_MAX)
1370 linux_set_current(td);
1371 if (filp->f_op->write) {
1372 bytes = OPW(file, td, filp->f_op->write(filp, uio->uio_iov->iov_base,
1373 uio->uio_iov->iov_len, &uio->uio_offset));
1375 uio->uio_iov->iov_base =
1376 ((uint8_t *)uio->uio_iov->iov_base) + bytes;
1377 uio->uio_iov->iov_len -= bytes;
1378 uio->uio_resid -= bytes;
1380 error = linux_get_error(current, -bytes);
1385 /* update kqfilter status, if any */
1386 linux_file_kqfilter_poll(filp, LINUX_KQ_FLAG_HAS_WRITE);
1392 linux_file_poll(struct file *file, int events, struct ucred *active_cred,
1395 struct linux_file *filp;
1398 filp = (struct linux_file *)file->f_data;
1399 filp->f_flags = file->f_flag;
1400 linux_set_current(td);
1401 if (filp->f_op->poll != NULL)
1402 revents = OPW(file, td, filp->f_op->poll(filp, LINUX_POLL_TABLE_NORMAL)) & events;
1410 linux_file_close(struct file *file, struct thread *td)
1412 struct linux_file *filp;
1415 filp = (struct linux_file *)file->f_data;
1417 KASSERT(file_count(filp) == 0, ("File refcount(%d) is not zero", file_count(filp)));
1419 filp->f_flags = file->f_flag;
1420 linux_set_current(td);
1421 linux_poll_wait_dequeue(filp);
1422 error = -OPW(file, td, filp->f_op->release(filp->f_vnode, filp));
1423 funsetown(&filp->f_sigio);
1424 if (filp->f_vnode != NULL)
1425 vdrop(filp->f_vnode);
1426 if (filp->f_cdev != NULL) {
1427 /* put a reference on the Linux character device */
1428 atomic_long_dec(&filp->f_cdev->refs);
1436 linux_file_ioctl(struct file *fp, u_long cmd, void *data, struct ucred *cred,
1439 struct linux_file *filp;
1442 filp = (struct linux_file *)fp->f_data;
1443 filp->f_flags = fp->f_flag;
1446 linux_set_current(td);
1451 if (filp->f_op->fasync == NULL)
1453 error = -OPW(fp, td, filp->f_op->fasync(0, filp, fp->f_flag & FASYNC));
1456 error = fsetown(*(int *)data, &filp->f_sigio);
1458 if (filp->f_op->fasync == NULL)
1460 error = -OPW(fp, td, filp->f_op->fasync(0, filp,
1461 fp->f_flag & FASYNC));
1465 *(int *)data = fgetown(&filp->f_sigio);
1468 error = linux_file_ioctl_sub(fp, filp, cmd, data, td);
1475 linux_file_mmap_sub(struct thread *td, vm_size_t objsize, vm_prot_t prot,
1476 vm_prot_t *maxprotp, int *flagsp, struct file *fp,
1477 vm_ooffset_t *foff, vm_object_t *objp)
1480 * Character devices do not provide private mappings
1483 if ((*maxprotp & VM_PROT_WRITE) == 0 &&
1484 (prot & VM_PROT_WRITE) != 0)
1486 if ((*flagsp & (MAP_PRIVATE | MAP_COPY)) != 0)
1489 return (linux_file_mmap_single(fp, foff, objsize, objp, (int)prot, td));
1493 linux_file_mmap(struct file *fp, vm_map_t map, vm_offset_t *addr, vm_size_t size,
1494 vm_prot_t prot, vm_prot_t cap_maxprot, int flags, vm_ooffset_t foff,
1497 struct linux_file *filp;
1504 filp = (struct linux_file *)fp->f_data;
1508 return (EOPNOTSUPP);
1511 * Ensure that file and memory protections are
1515 if (mp != NULL && (mp->mnt_flag & MNT_NOEXEC) != 0) {
1516 maxprot = VM_PROT_NONE;
1517 if ((prot & VM_PROT_EXECUTE) != 0)
1520 maxprot = VM_PROT_EXECUTE;
1521 if ((fp->f_flag & FREAD) != 0)
1522 maxprot |= VM_PROT_READ;
1523 else if ((prot & VM_PROT_READ) != 0)
1527 * If we are sharing potential changes via MAP_SHARED and we
1528 * are trying to get write permission although we opened it
1529 * without asking for it, bail out.
1531 * Note that most character devices always share mappings.
1533 * Rely on linux_file_mmap_sub() to fail invalid MAP_PRIVATE
1534 * requests rather than doing it here.
1536 if ((flags & MAP_SHARED) != 0) {
1537 if ((fp->f_flag & FWRITE) != 0)
1538 maxprot |= VM_PROT_WRITE;
1539 else if ((prot & VM_PROT_WRITE) != 0)
1542 maxprot &= cap_maxprot;
1544 error = linux_file_mmap_sub(td, size, prot, &maxprot, &flags, fp, &foff,
1549 error = vm_mmap_object(map, addr, size, prot, maxprot, flags, object,
1552 vm_object_deallocate(object);
1557 linux_file_stat(struct file *fp, struct stat *sb, struct ucred *active_cred,
1560 struct linux_file *filp;
1564 filp = (struct linux_file *)fp->f_data;
1565 if (filp->f_vnode == NULL)
1566 return (EOPNOTSUPP);
1570 vn_lock(vp, LK_SHARED | LK_RETRY);
1571 error = vn_stat(vp, sb, td->td_ucred, NOCRED, td);
1578 linux_file_fill_kinfo(struct file *fp, struct kinfo_file *kif,
1579 struct filedesc *fdp)
1581 struct linux_file *filp;
1589 kif->kf_type = KF_TYPE_DEV;
1592 FILEDESC_SUNLOCK(fdp);
1593 error = vn_fill_kinfo_vnode(vp, kif);
1595 kif->kf_type = KF_TYPE_VNODE;
1596 FILEDESC_SLOCK(fdp);
1602 linux_iminor(struct inode *inode)
1604 struct linux_cdev *ldev;
1606 if (inode == NULL || inode->v_rdev == NULL ||
1607 inode->v_rdev->si_devsw != &linuxcdevsw)
1609 ldev = inode->v_rdev->si_drv1;
1613 return (minor(ldev->dev));
1616 struct fileops linuxfileops = {
1617 .fo_read = linux_file_read,
1618 .fo_write = linux_file_write,
1619 .fo_truncate = invfo_truncate,
1620 .fo_kqfilter = linux_file_kqfilter,
1621 .fo_stat = linux_file_stat,
1622 .fo_fill_kinfo = linux_file_fill_kinfo,
1623 .fo_poll = linux_file_poll,
1624 .fo_close = linux_file_close,
1625 .fo_ioctl = linux_file_ioctl,
1626 .fo_mmap = linux_file_mmap,
1627 .fo_chmod = invfo_chmod,
1628 .fo_chown = invfo_chown,
1629 .fo_sendfile = invfo_sendfile,
1630 .fo_flags = DFLAG_PASSABLE,
1634 * Hash of vmmap addresses. This is infrequently accessed and does not
1635 * need to be particularly large. This is done because we must store the
1636 * caller's idea of the map size to properly unmap.
1639 LIST_ENTRY(vmmap) vm_next;
1641 unsigned long vm_size;
1645 struct vmmap *lh_first;
1647 #define VMMAP_HASH_SIZE 64
1648 #define VMMAP_HASH_MASK (VMMAP_HASH_SIZE - 1)
1649 #define VM_HASH(addr) ((uintptr_t)(addr) >> PAGE_SHIFT) & VMMAP_HASH_MASK
1650 static struct vmmaphd vmmaphead[VMMAP_HASH_SIZE];
1651 static struct mtx vmmaplock;
1654 vmmap_add(void *addr, unsigned long size)
1656 struct vmmap *vmmap;
1658 vmmap = kmalloc(sizeof(*vmmap), GFP_KERNEL);
1659 mtx_lock(&vmmaplock);
1660 vmmap->vm_size = size;
1661 vmmap->vm_addr = addr;
1662 LIST_INSERT_HEAD(&vmmaphead[VM_HASH(addr)], vmmap, vm_next);
1663 mtx_unlock(&vmmaplock);
1666 static struct vmmap *
1667 vmmap_remove(void *addr)
1669 struct vmmap *vmmap;
1671 mtx_lock(&vmmaplock);
1672 LIST_FOREACH(vmmap, &vmmaphead[VM_HASH(addr)], vm_next)
1673 if (vmmap->vm_addr == addr)
1676 LIST_REMOVE(vmmap, vm_next);
1677 mtx_unlock(&vmmaplock);
1682 #if defined(__i386__) || defined(__amd64__) || defined(__powerpc__)
1684 _ioremap_attr(vm_paddr_t phys_addr, unsigned long size, int attr)
1688 addr = pmap_mapdev_attr(phys_addr, size, attr);
1691 vmmap_add(addr, size);
1700 struct vmmap *vmmap;
1702 vmmap = vmmap_remove(addr);
1705 #if defined(__i386__) || defined(__amd64__) || defined(__powerpc__)
1706 pmap_unmapdev((vm_offset_t)addr, vmmap->vm_size);
1713 vmap(struct page **pages, unsigned int count, unsigned long flags, int prot)
1718 size = count * PAGE_SIZE;
1719 off = kva_alloc(size);
1722 vmmap_add((void *)off, size);
1723 pmap_qenter(off, pages, count);
1725 return ((void *)off);
1731 struct vmmap *vmmap;
1733 vmmap = vmmap_remove(addr);
1736 pmap_qremove((vm_offset_t)addr, vmmap->vm_size / PAGE_SIZE);
1737 kva_free((vm_offset_t)addr, vmmap->vm_size);
1742 kvasprintf(gfp_t gfp, const char *fmt, va_list ap)
1749 len = vsnprintf(NULL, 0, fmt, aq);
1752 p = kmalloc(len + 1, gfp);
1754 vsnprintf(p, len + 1, fmt, ap);
1760 kasprintf(gfp_t gfp, const char *fmt, ...)
1766 p = kvasprintf(gfp, fmt, ap);
1773 linux_timer_callback_wrapper(void *context)
1775 struct timer_list *timer;
1777 linux_set_current(curthread);
1780 timer->function(timer->data);
1784 mod_timer(struct timer_list *timer, int expires)
1787 timer->expires = expires;
1788 callout_reset(&timer->callout,
1789 linux_timer_jiffies_until(expires),
1790 &linux_timer_callback_wrapper, timer);
1794 add_timer(struct timer_list *timer)
1797 callout_reset(&timer->callout,
1798 linux_timer_jiffies_until(timer->expires),
1799 &linux_timer_callback_wrapper, timer);
1803 add_timer_on(struct timer_list *timer, int cpu)
1806 callout_reset_on(&timer->callout,
1807 linux_timer_jiffies_until(timer->expires),
1808 &linux_timer_callback_wrapper, timer, cpu);
1812 linux_timer_init(void *arg)
1816 * Compute an internal HZ value which can divide 2**32 to
1817 * avoid timer rounding problems when the tick value wraps
1820 linux_timer_hz_mask = 1;
1821 while (linux_timer_hz_mask < (unsigned long)hz)
1822 linux_timer_hz_mask *= 2;
1823 linux_timer_hz_mask--;
1825 SYSINIT(linux_timer, SI_SUB_DRIVERS, SI_ORDER_FIRST, linux_timer_init, NULL);
1828 linux_complete_common(struct completion *c, int all)
1835 wakeup_swapper = sleepq_broadcast(c, SLEEPQ_SLEEP, 0, 0);
1837 if (c->done != UINT_MAX)
1839 wakeup_swapper = sleepq_signal(c, SLEEPQ_SLEEP, 0, 0);
1847 * Indefinite wait for done != 0 with or without signals.
1850 linux_wait_for_common(struct completion *c, int flags)
1852 struct task_struct *task;
1855 if (SCHEDULER_STOPPED())
1861 flags = SLEEPQ_INTERRUPTIBLE | SLEEPQ_SLEEP;
1863 flags = SLEEPQ_SLEEP;
1869 sleepq_add(c, NULL, "completion", flags, 0);
1870 if (flags & SLEEPQ_INTERRUPTIBLE) {
1872 error = -sleepq_wait_sig(c, 0);
1875 linux_schedule_save_interrupt_value(task, error);
1876 error = -ERESTARTSYS;
1885 if (c->done != UINT_MAX)
1894 * Time limited wait for done != 0 with or without signals.
1897 linux_wait_for_timeout_common(struct completion *c, int timeout, int flags)
1899 struct task_struct *task;
1900 int end = jiffies + timeout;
1903 if (SCHEDULER_STOPPED())
1909 flags = SLEEPQ_INTERRUPTIBLE | SLEEPQ_SLEEP;
1911 flags = SLEEPQ_SLEEP;
1917 sleepq_add(c, NULL, "completion", flags, 0);
1918 sleepq_set_timeout(c, linux_timer_jiffies_until(end));
1921 if (flags & SLEEPQ_INTERRUPTIBLE)
1922 error = -sleepq_timedwait_sig(c, 0);
1924 error = -sleepq_timedwait(c, 0);
1928 /* check for timeout */
1929 if (error == -EWOULDBLOCK) {
1930 error = 0; /* timeout */
1932 /* signal happened */
1933 linux_schedule_save_interrupt_value(task, error);
1934 error = -ERESTARTSYS;
1939 if (c->done != UINT_MAX)
1943 /* return how many jiffies are left */
1944 error = linux_timer_jiffies_until(end);
1950 linux_try_wait_for_completion(struct completion *c)
1955 isdone = (c->done != 0);
1956 if (c->done != 0 && c->done != UINT_MAX)
1963 linux_completion_done(struct completion *c)
1968 isdone = (c->done != 0);
1974 linux_cdev_release(struct kobject *kobj)
1976 struct linux_cdev *cdev;
1977 struct kobject *parent;
1979 cdev = container_of(kobj, struct linux_cdev, kobj);
1980 parent = kobj->parent;
1981 linux_destroy_dev(cdev);
1983 kobject_put(parent);
1987 linux_cdev_static_release(struct kobject *kobj)
1989 struct linux_cdev *cdev;
1990 struct kobject *parent;
1992 cdev = container_of(kobj, struct linux_cdev, kobj);
1993 parent = kobj->parent;
1994 linux_destroy_dev(cdev);
1995 kobject_put(parent);
1999 linux_destroy_dev(struct linux_cdev *cdev)
2002 if (cdev->cdev == NULL)
2005 atomic_long_dec(&cdev->refs);
2007 /* wait for all open files to be closed */
2008 while (atomic_long_read(&cdev->refs) != -1L)
2009 pause("ldevdrn", hz);
2011 destroy_dev(cdev->cdev);
2015 const struct kobj_type linux_cdev_ktype = {
2016 .release = linux_cdev_release,
2019 const struct kobj_type linux_cdev_static_ktype = {
2020 .release = linux_cdev_static_release,
2024 linux_handle_ifnet_link_event(void *arg, struct ifnet *ifp, int linkstate)
2026 struct notifier_block *nb;
2029 if (linkstate == LINK_STATE_UP)
2030 nb->notifier_call(nb, NETDEV_UP, ifp);
2032 nb->notifier_call(nb, NETDEV_DOWN, ifp);
2036 linux_handle_ifnet_arrival_event(void *arg, struct ifnet *ifp)
2038 struct notifier_block *nb;
2041 nb->notifier_call(nb, NETDEV_REGISTER, ifp);
2045 linux_handle_ifnet_departure_event(void *arg, struct ifnet *ifp)
2047 struct notifier_block *nb;
2050 nb->notifier_call(nb, NETDEV_UNREGISTER, ifp);
2054 linux_handle_iflladdr_event(void *arg, struct ifnet *ifp)
2056 struct notifier_block *nb;
2059 nb->notifier_call(nb, NETDEV_CHANGEADDR, ifp);
2063 linux_handle_ifaddr_event(void *arg, struct ifnet *ifp)
2065 struct notifier_block *nb;
2068 nb->notifier_call(nb, NETDEV_CHANGEIFADDR, ifp);
2072 register_netdevice_notifier(struct notifier_block *nb)
2075 nb->tags[NETDEV_UP] = EVENTHANDLER_REGISTER(
2076 ifnet_link_event, linux_handle_ifnet_link_event, nb, 0);
2077 nb->tags[NETDEV_REGISTER] = EVENTHANDLER_REGISTER(
2078 ifnet_arrival_event, linux_handle_ifnet_arrival_event, nb, 0);
2079 nb->tags[NETDEV_UNREGISTER] = EVENTHANDLER_REGISTER(
2080 ifnet_departure_event, linux_handle_ifnet_departure_event, nb, 0);
2081 nb->tags[NETDEV_CHANGEADDR] = EVENTHANDLER_REGISTER(
2082 iflladdr_event, linux_handle_iflladdr_event, nb, 0);
2088 register_inetaddr_notifier(struct notifier_block *nb)
2091 nb->tags[NETDEV_CHANGEIFADDR] = EVENTHANDLER_REGISTER(
2092 ifaddr_event, linux_handle_ifaddr_event, nb, 0);
2097 unregister_netdevice_notifier(struct notifier_block *nb)
2100 EVENTHANDLER_DEREGISTER(ifnet_link_event,
2101 nb->tags[NETDEV_UP]);
2102 EVENTHANDLER_DEREGISTER(ifnet_arrival_event,
2103 nb->tags[NETDEV_REGISTER]);
2104 EVENTHANDLER_DEREGISTER(ifnet_departure_event,
2105 nb->tags[NETDEV_UNREGISTER]);
2106 EVENTHANDLER_DEREGISTER(iflladdr_event,
2107 nb->tags[NETDEV_CHANGEADDR]);
2113 unregister_inetaddr_notifier(struct notifier_block *nb)
2116 EVENTHANDLER_DEREGISTER(ifaddr_event,
2117 nb->tags[NETDEV_CHANGEIFADDR]);
2122 struct list_sort_thunk {
2123 int (*cmp)(void *, struct list_head *, struct list_head *);
2128 linux_le_cmp(void *priv, const void *d1, const void *d2)
2130 struct list_head *le1, *le2;
2131 struct list_sort_thunk *thunk;
2134 le1 = *(__DECONST(struct list_head **, d1));
2135 le2 = *(__DECONST(struct list_head **, d2));
2136 return ((thunk->cmp)(thunk->priv, le1, le2));
2140 list_sort(void *priv, struct list_head *head, int (*cmp)(void *priv,
2141 struct list_head *a, struct list_head *b))
2143 struct list_sort_thunk thunk;
2144 struct list_head **ar, *le;
2148 list_for_each(le, head)
2150 ar = malloc(sizeof(struct list_head *) * count, M_KMALLOC, M_WAITOK);
2152 list_for_each(le, head)
2156 qsort_r(ar, count, sizeof(struct list_head *), &thunk, linux_le_cmp);
2157 INIT_LIST_HEAD(head);
2158 for (i = 0; i < count; i++)
2159 list_add_tail(ar[i], head);
2160 free(ar, M_KMALLOC);
2164 linux_irq_handler(void *ent)
2166 struct irq_ent *irqe;
2168 linux_set_current(curthread);
2171 irqe->handler(irqe->irq, irqe->arg);
2174 #if defined(__i386__) || defined(__amd64__)
2176 linux_wbinvd_on_all_cpus(void)
2179 pmap_invalidate_cache();
2185 linux_on_each_cpu(void callback(void *), void *data)
2188 smp_rendezvous(smp_no_rendezvous_barrier, callback,
2189 smp_no_rendezvous_barrier, data);
2194 linux_in_atomic(void)
2197 return ((curthread->td_pflags & TDP_NOFAULTING) != 0);
2201 linux_find_cdev(const char *name, unsigned major, unsigned minor)
2203 dev_t dev = MKDEV(major, minor);
2207 LIST_FOREACH(cdev, &linuxcdevsw.d_devs, si_list) {
2208 struct linux_cdev *ldev = cdev->si_drv1;
2209 if (ldev->dev == dev &&
2210 strcmp(kobject_name(&ldev->kobj), name) == 0) {
2216 return (cdev != NULL ? cdev->si_drv1 : NULL);
2220 __register_chrdev(unsigned int major, unsigned int baseminor,
2221 unsigned int count, const char *name,
2222 const struct file_operations *fops)
2224 struct linux_cdev *cdev;
2228 for (i = baseminor; i < baseminor + count; i++) {
2229 cdev = cdev_alloc();
2230 cdev_init(cdev, fops);
2231 kobject_set_name(&cdev->kobj, name);
2233 ret = cdev_add(cdev, makedev(major, i), 1);
2241 __register_chrdev_p(unsigned int major, unsigned int baseminor,
2242 unsigned int count, const char *name,
2243 const struct file_operations *fops, uid_t uid,
2244 gid_t gid, int mode)
2246 struct linux_cdev *cdev;
2250 for (i = baseminor; i < baseminor + count; i++) {
2251 cdev = cdev_alloc();
2252 cdev_init(cdev, fops);
2253 kobject_set_name(&cdev->kobj, name);
2255 ret = cdev_add_ext(cdev, makedev(major, i), uid, gid, mode);
2263 __unregister_chrdev(unsigned int major, unsigned int baseminor,
2264 unsigned int count, const char *name)
2266 struct linux_cdev *cdevp;
2269 for (i = baseminor; i < baseminor + count; i++) {
2270 cdevp = linux_find_cdev(name, major, i);
2277 linux_dump_stack(void)
2288 #if defined(__i386__) || defined(__amd64__)
2289 bool linux_cpu_has_clflush;
2293 linux_compat_init(void *arg)
2295 struct sysctl_oid *rootoid;
2298 #if defined(__i386__) || defined(__amd64__)
2299 linux_cpu_has_clflush = (cpu_feature & CPUID_CLFSH);
2301 rw_init(&linux_vma_lock, "lkpi-vma-lock");
2303 rootoid = SYSCTL_ADD_ROOT_NODE(NULL,
2304 OID_AUTO, "sys", CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, "sys");
2305 kobject_init(&linux_class_root, &linux_class_ktype);
2306 kobject_set_name(&linux_class_root, "class");
2307 linux_class_root.oidp = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(rootoid),
2308 OID_AUTO, "class", CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, "class");
2309 kobject_init(&linux_root_device.kobj, &linux_dev_ktype);
2310 kobject_set_name(&linux_root_device.kobj, "device");
2311 linux_root_device.kobj.oidp = SYSCTL_ADD_NODE(NULL,
2312 SYSCTL_CHILDREN(rootoid), OID_AUTO, "device", CTLFLAG_RD, NULL,
2314 linux_root_device.bsddev = root_bus;
2315 linux_class_misc.name = "misc";
2316 class_register(&linux_class_misc);
2317 INIT_LIST_HEAD(&pci_drivers);
2318 INIT_LIST_HEAD(&pci_devices);
2319 spin_lock_init(&pci_lock);
2320 mtx_init(&vmmaplock, "IO Map lock", NULL, MTX_DEF);
2321 for (i = 0; i < VMMAP_HASH_SIZE; i++)
2322 LIST_INIT(&vmmaphead[i]);
2324 SYSINIT(linux_compat, SI_SUB_DRIVERS, SI_ORDER_SECOND, linux_compat_init, NULL);
2327 linux_compat_uninit(void *arg)
2329 linux_kobject_kfree_name(&linux_class_root);
2330 linux_kobject_kfree_name(&linux_root_device.kobj);
2331 linux_kobject_kfree_name(&linux_class_misc.kobj);
2333 mtx_destroy(&vmmaplock);
2334 spin_lock_destroy(&pci_lock);
2335 rw_destroy(&linux_vma_lock);
2337 SYSUNINIT(linux_compat, SI_SUB_DRIVERS, SI_ORDER_SECOND, linux_compat_uninit, NULL);
2340 * NOTE: Linux frequently uses "unsigned long" for pointer to integer
2341 * conversion and vice versa, where in FreeBSD "uintptr_t" would be
2342 * used. Assert these types have the same size, else some parts of the
2343 * LinuxKPI may not work like expected:
2345 CTASSERT(sizeof(unsigned long) == sizeof(uintptr_t));