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>
43 #include <sys/refcount.h>
45 #include <sys/mutex.h>
47 #include <sys/fcntl.h>
49 #include <sys/filio.h>
50 #include <sys/rwlock.h>
52 #include <sys/stack.h>
57 #include <vm/vm_object.h>
58 #include <vm/vm_page.h>
59 #include <vm/vm_pager.h>
61 #include <machine/stdarg.h>
63 #if defined(__i386__) || defined(__amd64__)
64 #include <machine/md_var.h>
67 #include <linux/kobject.h>
68 #include <linux/device.h>
69 #include <linux/slab.h>
70 #include <linux/module.h>
71 #include <linux/moduleparam.h>
72 #include <linux/cdev.h>
73 #include <linux/file.h>
74 #include <linux/sysfs.h>
77 #include <linux/vmalloc.h>
78 #include <linux/netdevice.h>
79 #include <linux/timer.h>
80 #include <linux/interrupt.h>
81 #include <linux/uaccess.h>
82 #include <linux/list.h>
83 #include <linux/kthread.h>
84 #include <linux/kernel.h>
85 #include <linux/compat.h>
86 #include <linux/poll.h>
87 #include <linux/smp.h>
89 #if defined(__i386__) || defined(__amd64__)
93 SYSCTL_NODE(_compat, OID_AUTO, linuxkpi, CTLFLAG_RW, 0, "LinuxKPI parameters");
96 SYSCTL_INT(_compat_linuxkpi, OID_AUTO, debug, CTLFLAG_RWTUN,
97 &linuxkpi_debug, 0, "Set to enable pr_debug() prints. Clear to disable.");
99 MALLOC_DEFINE(M_KMALLOC, "linux", "Linux kmalloc compat");
101 #include <linux/rbtree.h>
102 /* Undo Linux compat changes. */
106 #define RB_ROOT(head) (head)->rbh_root
108 static void linux_cdev_deref(struct linux_cdev *ldev);
109 static struct vm_area_struct *linux_cdev_handle_find(void *handle);
111 struct kobject linux_class_root;
112 struct device linux_root_device;
113 struct class linux_class_misc;
114 struct list_head pci_drivers;
115 struct list_head pci_devices;
118 unsigned long linux_timer_hz_mask;
121 panic_cmp(struct rb_node *one, struct rb_node *two)
126 RB_GENERATE(linux_root, rb_node, __entry, panic_cmp);
129 kobject_set_name_vargs(struct kobject *kobj, const char *fmt, va_list args)
139 if (old && fmt == NULL)
142 /* compute length of string */
143 va_copy(tmp_va, args);
144 len = vsnprintf(&dummy, 0, fmt, tmp_va);
147 /* account for zero termination */
150 /* check for error */
154 /* allocate memory for string */
155 name = kzalloc(len, GFP_KERNEL);
158 vsnprintf(name, len, fmt, args);
161 /* free old string */
164 /* filter new string */
165 for (; *name != '\0'; name++)
172 kobject_set_name(struct kobject *kobj, const char *fmt, ...)
178 error = kobject_set_name_vargs(kobj, fmt, args);
185 kobject_add_complete(struct kobject *kobj, struct kobject *parent)
187 const struct kobj_type *t;
190 kobj->parent = parent;
191 error = sysfs_create_dir(kobj);
192 if (error == 0 && kobj->ktype && kobj->ktype->default_attrs) {
193 struct attribute **attr;
196 for (attr = t->default_attrs; *attr != NULL; attr++) {
197 error = sysfs_create_file(kobj, *attr);
202 sysfs_remove_dir(kobj);
209 kobject_add(struct kobject *kobj, struct kobject *parent, const char *fmt, ...)
215 error = kobject_set_name_vargs(kobj, fmt, args);
220 return kobject_add_complete(kobj, parent);
224 linux_kobject_release(struct kref *kref)
226 struct kobject *kobj;
229 kobj = container_of(kref, struct kobject, kref);
230 sysfs_remove_dir(kobj);
232 if (kobj->ktype && kobj->ktype->release)
233 kobj->ktype->release(kobj);
238 linux_kobject_kfree(struct kobject *kobj)
244 linux_kobject_kfree_name(struct kobject *kobj)
251 const struct kobj_type linux_kfree_type = {
252 .release = linux_kobject_kfree
256 linux_device_release(struct device *dev)
258 pr_debug("linux_device_release: %s\n", dev_name(dev));
263 linux_class_show(struct kobject *kobj, struct attribute *attr, char *buf)
265 struct class_attribute *dattr;
268 dattr = container_of(attr, struct class_attribute, attr);
271 error = dattr->show(container_of(kobj, struct class, kobj),
277 linux_class_store(struct kobject *kobj, struct attribute *attr, const char *buf,
280 struct class_attribute *dattr;
283 dattr = container_of(attr, struct class_attribute, attr);
286 error = dattr->store(container_of(kobj, struct class, kobj),
292 linux_class_release(struct kobject *kobj)
296 class = container_of(kobj, struct class, kobj);
297 if (class->class_release)
298 class->class_release(class);
301 static const struct sysfs_ops linux_class_sysfs = {
302 .show = linux_class_show,
303 .store = linux_class_store,
306 const struct kobj_type linux_class_ktype = {
307 .release = linux_class_release,
308 .sysfs_ops = &linux_class_sysfs
312 linux_dev_release(struct kobject *kobj)
316 dev = container_of(kobj, struct device, kobj);
317 /* This is the precedence defined by linux. */
320 else if (dev->class && dev->class->dev_release)
321 dev->class->dev_release(dev);
325 linux_dev_show(struct kobject *kobj, struct attribute *attr, char *buf)
327 struct device_attribute *dattr;
330 dattr = container_of(attr, struct device_attribute, attr);
333 error = dattr->show(container_of(kobj, struct device, kobj),
339 linux_dev_store(struct kobject *kobj, struct attribute *attr, const char *buf,
342 struct device_attribute *dattr;
345 dattr = container_of(attr, struct device_attribute, attr);
348 error = dattr->store(container_of(kobj, struct device, kobj),
353 static const struct sysfs_ops linux_dev_sysfs = {
354 .show = linux_dev_show,
355 .store = linux_dev_store,
358 const struct kobj_type linux_dev_ktype = {
359 .release = linux_dev_release,
360 .sysfs_ops = &linux_dev_sysfs
364 device_create(struct class *class, struct device *parent, dev_t devt,
365 void *drvdata, const char *fmt, ...)
370 dev = kzalloc(sizeof(*dev), M_WAITOK);
371 dev->parent = parent;
374 dev->driver_data = drvdata;
375 dev->release = linux_device_release;
377 kobject_set_name_vargs(&dev->kobj, fmt, args);
379 device_register(dev);
385 kobject_init_and_add(struct kobject *kobj, const struct kobj_type *ktype,
386 struct kobject *parent, const char *fmt, ...)
391 kobject_init(kobj, ktype);
393 kobj->parent = parent;
397 error = kobject_set_name_vargs(kobj, fmt, args);
401 return kobject_add_complete(kobj, parent);
405 linux_kq_lock(void *arg)
412 linux_kq_unlock(void *arg)
420 linux_kq_lock_owned(void *arg)
425 mtx_assert(&s->m, MA_OWNED);
430 linux_kq_lock_unowned(void *arg)
435 mtx_assert(&s->m, MA_NOTOWNED);
440 linux_file_kqfilter_poll(struct linux_file *, int);
443 linux_file_alloc(void)
445 struct linux_file *filp;
447 filp = kzalloc(sizeof(*filp), GFP_KERNEL);
449 /* set initial refcount */
452 /* setup fields needed by kqueue support */
453 spin_lock_init(&filp->f_kqlock);
454 knlist_init(&filp->f_selinfo.si_note, &filp->f_kqlock,
455 linux_kq_lock, linux_kq_unlock,
456 linux_kq_lock_owned, linux_kq_lock_unowned);
462 linux_file_free(struct linux_file *filp)
464 if (filp->_file == NULL) {
465 if (filp->f_shmem != NULL)
466 vm_object_deallocate(filp->f_shmem);
470 * The close method of the character device or file
471 * will free the linux_file structure:
473 _fdrop(filp->_file, curthread);
478 linux_cdev_pager_fault(vm_object_t vm_obj, vm_ooffset_t offset, int prot,
481 struct vm_area_struct *vmap;
483 vmap = linux_cdev_handle_find(vm_obj->handle);
486 MPASS(vmap->vm_private_data == vm_obj->handle);
488 if (likely(vmap->vm_ops != NULL && offset < vmap->vm_len)) {
489 vm_paddr_t paddr = IDX_TO_OFF(vmap->vm_pfn) + offset;
492 if (((*mres)->flags & PG_FICTITIOUS) != 0) {
494 * If the passed in result page is a fake
495 * page, update it with the new physical
499 vm_page_updatefake(page, paddr, vm_obj->memattr);
502 * Replace the passed in "mres" page with our
503 * own fake page and free up the all of the
506 VM_OBJECT_WUNLOCK(vm_obj);
507 page = vm_page_getfake(paddr, vm_obj->memattr);
508 VM_OBJECT_WLOCK(vm_obj);
510 vm_page_replace_checked(page, vm_obj,
511 (*mres)->pindex, *mres);
515 vm_page_unlock(*mres);
518 page->valid = VM_PAGE_BITS_ALL;
519 return (VM_PAGER_OK);
521 return (VM_PAGER_FAIL);
525 linux_cdev_pager_populate(vm_object_t vm_obj, vm_pindex_t pidx, int fault_type,
526 vm_prot_t max_prot, vm_pindex_t *first, vm_pindex_t *last)
528 struct vm_area_struct *vmap;
531 linux_set_current(curthread);
533 /* get VM area structure */
534 vmap = linux_cdev_handle_find(vm_obj->handle);
536 MPASS(vmap->vm_private_data == vm_obj->handle);
538 VM_OBJECT_WUNLOCK(vm_obj);
540 down_write(&vmap->vm_mm->mmap_sem);
541 if (unlikely(vmap->vm_ops == NULL)) {
542 err = VM_FAULT_SIGBUS;
546 /* fill out VM fault structure */
547 vmf.virtual_address = (void *)(uintptr_t)IDX_TO_OFF(pidx);
548 vmf.flags = (fault_type & VM_PROT_WRITE) ? FAULT_FLAG_WRITE : 0;
553 vmap->vm_pfn_count = 0;
554 vmap->vm_pfn_pcount = &vmap->vm_pfn_count;
555 vmap->vm_obj = vm_obj;
557 err = vmap->vm_ops->fault(vmap, &vmf);
559 while (vmap->vm_pfn_count == 0 && err == VM_FAULT_NOPAGE) {
560 kern_yield(PRI_USER);
561 err = vmap->vm_ops->fault(vmap, &vmf);
565 /* translate return code */
568 err = VM_PAGER_AGAIN;
570 case VM_FAULT_SIGBUS:
573 case VM_FAULT_NOPAGE:
575 * By contract the fault handler will return having
576 * busied all the pages itself. If pidx is already
577 * found in the object, it will simply xbusy the first
578 * page and return with vm_pfn_count set to 1.
580 *first = vmap->vm_pfn_first;
581 *last = *first + vmap->vm_pfn_count - 1;
585 err = VM_PAGER_ERROR;
588 up_write(&vmap->vm_mm->mmap_sem);
589 VM_OBJECT_WLOCK(vm_obj);
593 static struct rwlock linux_vma_lock;
594 static TAILQ_HEAD(, vm_area_struct) linux_vma_head =
595 TAILQ_HEAD_INITIALIZER(linux_vma_head);
598 linux_cdev_handle_free(struct vm_area_struct *vmap)
600 /* Drop reference on vm_file */
601 if (vmap->vm_file != NULL)
604 /* Drop reference on mm_struct */
611 linux_cdev_handle_remove(struct vm_area_struct *vmap)
613 rw_wlock(&linux_vma_lock);
614 TAILQ_REMOVE(&linux_vma_head, vmap, vm_entry);
615 rw_wunlock(&linux_vma_lock);
618 static struct vm_area_struct *
619 linux_cdev_handle_find(void *handle)
621 struct vm_area_struct *vmap;
623 rw_rlock(&linux_vma_lock);
624 TAILQ_FOREACH(vmap, &linux_vma_head, vm_entry) {
625 if (vmap->vm_private_data == handle)
628 rw_runlock(&linux_vma_lock);
633 linux_cdev_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
634 vm_ooffset_t foff, struct ucred *cred, u_short *color)
637 MPASS(linux_cdev_handle_find(handle) != NULL);
643 linux_cdev_pager_dtor(void *handle)
645 const struct vm_operations_struct *vm_ops;
646 struct vm_area_struct *vmap;
648 vmap = linux_cdev_handle_find(handle);
652 * Remove handle before calling close operation to prevent
653 * other threads from reusing the handle pointer.
655 linux_cdev_handle_remove(vmap);
657 down_write(&vmap->vm_mm->mmap_sem);
658 vm_ops = vmap->vm_ops;
659 if (likely(vm_ops != NULL))
661 up_write(&vmap->vm_mm->mmap_sem);
663 linux_cdev_handle_free(vmap);
666 static struct cdev_pager_ops linux_cdev_pager_ops[2] = {
669 .cdev_pg_populate = linux_cdev_pager_populate,
670 .cdev_pg_ctor = linux_cdev_pager_ctor,
671 .cdev_pg_dtor = linux_cdev_pager_dtor
675 .cdev_pg_fault = linux_cdev_pager_fault,
676 .cdev_pg_ctor = linux_cdev_pager_ctor,
677 .cdev_pg_dtor = linux_cdev_pager_dtor
682 zap_vma_ptes(struct vm_area_struct *vma, unsigned long address,
689 if (obj == NULL || (obj->flags & OBJ_UNMANAGED) != 0)
691 VM_OBJECT_RLOCK(obj);
692 for (m = vm_page_find_least(obj, OFF_TO_IDX(address));
693 m != NULL && m->pindex < OFF_TO_IDX(address + size);
694 m = TAILQ_NEXT(m, listq))
696 VM_OBJECT_RUNLOCK(obj);
700 static struct file_operations dummy_ldev_ops = {
704 static struct linux_cdev dummy_ldev = {
705 .ops = &dummy_ldev_ops,
708 #define LDEV_SI_DTR 0x0001
709 #define LDEV_SI_REF 0x0002
712 linux_get_fop(struct linux_file *filp, const struct file_operations **fop,
713 struct linux_cdev **dev)
715 struct linux_cdev *ldev;
721 for (siref = ldev->siref;;) {
722 if ((siref & LDEV_SI_DTR) != 0) {
726 MPASS((ldev->siref & LDEV_SI_DTR) == 0);
727 } else if (atomic_fcmpset_int(&ldev->siref, &siref,
728 siref + LDEV_SI_REF)) {
737 linux_drop_fop(struct linux_cdev *ldev)
742 MPASS((ldev->siref & ~LDEV_SI_DTR) != 0);
743 atomic_subtract_int(&ldev->siref, LDEV_SI_REF);
746 #define OPW(fp,td,code) ({ \
747 struct file *__fpop; \
748 __typeof(code) __retval; \
750 __fpop = (td)->td_fpop; \
751 (td)->td_fpop = (fp); \
753 (td)->td_fpop = __fpop; \
758 linux_dev_fdopen(struct cdev *dev, int fflags, struct thread *td,
761 struct linux_cdev *ldev;
762 struct linux_file *filp;
763 const struct file_operations *fop;
768 filp = linux_file_alloc();
769 filp->f_dentry = &filp->f_dentry_store;
770 filp->f_op = ldev->ops;
771 filp->f_mode = file->f_flag;
772 filp->f_flags = file->f_flag;
773 filp->f_vnode = file->f_vnode;
775 refcount_acquire(&ldev->refs);
778 linux_set_current(td);
779 linux_get_fop(filp, &fop, &ldev);
781 if (fop->open != NULL) {
782 error = -fop->open(file->f_vnode, filp);
784 linux_drop_fop(ldev);
785 linux_cdev_deref(filp->f_cdev);
791 /* hold on to the vnode - used for fstat() */
792 vhold(filp->f_vnode);
794 /* release the file from devfs */
795 finit(file, filp->f_mode, DTYPE_DEV, filp, &linuxfileops);
796 linux_drop_fop(ldev);
800 #define LINUX_IOCTL_MIN_PTR 0x10000UL
801 #define LINUX_IOCTL_MAX_PTR (LINUX_IOCTL_MIN_PTR + IOCPARM_MAX)
804 linux_remap_address(void **uaddr, size_t len)
806 uintptr_t uaddr_val = (uintptr_t)(*uaddr);
808 if (unlikely(uaddr_val >= LINUX_IOCTL_MIN_PTR &&
809 uaddr_val < LINUX_IOCTL_MAX_PTR)) {
810 struct task_struct *pts = current;
816 /* compute data offset */
817 uaddr_val -= LINUX_IOCTL_MIN_PTR;
819 /* check that length is within bounds */
820 if ((len > IOCPARM_MAX) ||
821 (uaddr_val + len) > pts->bsd_ioctl_len) {
826 /* re-add kernel buffer address */
827 uaddr_val += (uintptr_t)pts->bsd_ioctl_data;
829 /* update address location */
830 *uaddr = (void *)uaddr_val;
837 linux_copyin(const void *uaddr, void *kaddr, size_t len)
839 if (linux_remap_address(__DECONST(void **, &uaddr), len)) {
842 memcpy(kaddr, uaddr, len);
845 return (-copyin(uaddr, kaddr, len));
849 linux_copyout(const void *kaddr, void *uaddr, size_t len)
851 if (linux_remap_address(&uaddr, len)) {
854 memcpy(uaddr, kaddr, len);
857 return (-copyout(kaddr, uaddr, len));
861 linux_clear_user(void *_uaddr, size_t _len)
863 uint8_t *uaddr = _uaddr;
866 /* make sure uaddr is aligned before going into the fast loop */
867 while (((uintptr_t)uaddr & 7) != 0 && len > 7) {
868 if (subyte(uaddr, 0))
874 /* zero 8 bytes at a time */
877 if (suword64(uaddr, 0))
880 if (suword32(uaddr, 0))
882 if (suword32(uaddr + 4, 0))
889 /* zero fill end, if any */
891 if (subyte(uaddr, 0))
900 linux_access_ok(int rw, const void *uaddr, size_t len)
905 /* get start and end address */
906 saddr = (uintptr_t)uaddr;
907 eaddr = (uintptr_t)uaddr + len;
909 /* verify addresses are valid for userspace */
910 return ((saddr == eaddr) ||
911 (eaddr > saddr && eaddr <= VM_MAXUSER_ADDRESS));
915 * This function should return either EINTR or ERESTART depending on
916 * the signal type sent to this thread:
919 linux_get_error(struct task_struct *task, int error)
921 /* check for signal type interrupt code */
922 if (error == EINTR || error == ERESTARTSYS || error == ERESTART) {
923 error = -linux_schedule_get_interrupt_value(task);
931 linux_file_ioctl_sub(struct file *fp, struct linux_file *filp,
932 const struct file_operations *fop, u_long cmd, caddr_t data,
935 struct task_struct *task = current;
939 size = IOCPARM_LEN(cmd);
940 /* refer to logic in sys_ioctl() */
943 * Setup hint for linux_copyin() and linux_copyout().
945 * Background: Linux code expects a user-space address
946 * while FreeBSD supplies a kernel-space address.
948 task->bsd_ioctl_data = data;
949 task->bsd_ioctl_len = size;
950 data = (void *)LINUX_IOCTL_MIN_PTR;
952 /* fetch user-space pointer */
953 data = *(void **)data;
955 #if defined(__amd64__)
956 if (td->td_proc->p_elf_machine == EM_386) {
957 /* try the compat IOCTL handler first */
958 if (fop->compat_ioctl != NULL) {
959 error = -OPW(fp, td, fop->compat_ioctl(filp,
965 /* fallback to the regular IOCTL handler, if any */
966 if (error == ENOTTY && fop->unlocked_ioctl != NULL) {
967 error = -OPW(fp, td, fop->unlocked_ioctl(filp,
973 if (fop->unlocked_ioctl != NULL) {
974 error = -OPW(fp, td, fop->unlocked_ioctl(filp,
981 task->bsd_ioctl_data = NULL;
982 task->bsd_ioctl_len = 0;
985 if (error == EWOULDBLOCK) {
986 /* update kqfilter status, if any */
987 linux_file_kqfilter_poll(filp,
988 LINUX_KQ_FLAG_HAS_READ | LINUX_KQ_FLAG_HAS_WRITE);
990 error = linux_get_error(task, error);
995 #define LINUX_POLL_TABLE_NORMAL ((poll_table *)1)
998 * This function atomically updates the poll wakeup state and returns
999 * the previous state at the time of update.
1002 linux_poll_wakeup_state(atomic_t *v, const uint8_t *pstate)
1008 while ((old = atomic_cmpxchg(v, c, pstate[c])) != c)
1016 linux_poll_wakeup_callback(wait_queue_t *wq, unsigned int wq_state, int flags, void *key)
1018 static const uint8_t state[LINUX_FWQ_STATE_MAX] = {
1019 [LINUX_FWQ_STATE_INIT] = LINUX_FWQ_STATE_INIT, /* NOP */
1020 [LINUX_FWQ_STATE_NOT_READY] = LINUX_FWQ_STATE_NOT_READY, /* NOP */
1021 [LINUX_FWQ_STATE_QUEUED] = LINUX_FWQ_STATE_READY,
1022 [LINUX_FWQ_STATE_READY] = LINUX_FWQ_STATE_READY, /* NOP */
1024 struct linux_file *filp = container_of(wq, struct linux_file, f_wait_queue.wq);
1026 switch (linux_poll_wakeup_state(&filp->f_wait_queue.state, state)) {
1027 case LINUX_FWQ_STATE_QUEUED:
1028 linux_poll_wakeup(filp);
1036 linux_poll_wait(struct linux_file *filp, wait_queue_head_t *wqh, poll_table *p)
1038 static const uint8_t state[LINUX_FWQ_STATE_MAX] = {
1039 [LINUX_FWQ_STATE_INIT] = LINUX_FWQ_STATE_NOT_READY,
1040 [LINUX_FWQ_STATE_NOT_READY] = LINUX_FWQ_STATE_NOT_READY, /* NOP */
1041 [LINUX_FWQ_STATE_QUEUED] = LINUX_FWQ_STATE_QUEUED, /* NOP */
1042 [LINUX_FWQ_STATE_READY] = LINUX_FWQ_STATE_QUEUED,
1045 /* check if we are called inside the select system call */
1046 if (p == LINUX_POLL_TABLE_NORMAL)
1047 selrecord(curthread, &filp->f_selinfo);
1049 switch (linux_poll_wakeup_state(&filp->f_wait_queue.state, state)) {
1050 case LINUX_FWQ_STATE_INIT:
1051 /* NOTE: file handles can only belong to one wait-queue */
1052 filp->f_wait_queue.wqh = wqh;
1053 filp->f_wait_queue.wq.func = &linux_poll_wakeup_callback;
1054 add_wait_queue(wqh, &filp->f_wait_queue.wq);
1055 atomic_set(&filp->f_wait_queue.state, LINUX_FWQ_STATE_QUEUED);
1063 linux_poll_wait_dequeue(struct linux_file *filp)
1065 static const uint8_t state[LINUX_FWQ_STATE_MAX] = {
1066 [LINUX_FWQ_STATE_INIT] = LINUX_FWQ_STATE_INIT, /* NOP */
1067 [LINUX_FWQ_STATE_NOT_READY] = LINUX_FWQ_STATE_INIT,
1068 [LINUX_FWQ_STATE_QUEUED] = LINUX_FWQ_STATE_INIT,
1069 [LINUX_FWQ_STATE_READY] = LINUX_FWQ_STATE_INIT,
1072 seldrain(&filp->f_selinfo);
1074 switch (linux_poll_wakeup_state(&filp->f_wait_queue.state, state)) {
1075 case LINUX_FWQ_STATE_NOT_READY:
1076 case LINUX_FWQ_STATE_QUEUED:
1077 case LINUX_FWQ_STATE_READY:
1078 remove_wait_queue(filp->f_wait_queue.wqh, &filp->f_wait_queue.wq);
1086 linux_poll_wakeup(struct linux_file *filp)
1088 /* this function should be NULL-safe */
1092 selwakeup(&filp->f_selinfo);
1094 spin_lock(&filp->f_kqlock);
1095 filp->f_kqflags |= LINUX_KQ_FLAG_NEED_READ |
1096 LINUX_KQ_FLAG_NEED_WRITE;
1098 /* make sure the "knote" gets woken up */
1099 KNOTE_LOCKED(&filp->f_selinfo.si_note, 1);
1100 spin_unlock(&filp->f_kqlock);
1104 linux_file_kqfilter_detach(struct knote *kn)
1106 struct linux_file *filp = kn->kn_hook;
1108 spin_lock(&filp->f_kqlock);
1109 knlist_remove(&filp->f_selinfo.si_note, kn, 1);
1110 spin_unlock(&filp->f_kqlock);
1114 linux_file_kqfilter_read_event(struct knote *kn, long hint)
1116 struct linux_file *filp = kn->kn_hook;
1118 mtx_assert(&filp->f_kqlock.m, MA_OWNED);
1120 return ((filp->f_kqflags & LINUX_KQ_FLAG_NEED_READ) ? 1 : 0);
1124 linux_file_kqfilter_write_event(struct knote *kn, long hint)
1126 struct linux_file *filp = kn->kn_hook;
1128 mtx_assert(&filp->f_kqlock.m, MA_OWNED);
1130 return ((filp->f_kqflags & LINUX_KQ_FLAG_NEED_WRITE) ? 1 : 0);
1133 static struct filterops linux_dev_kqfiltops_read = {
1135 .f_detach = linux_file_kqfilter_detach,
1136 .f_event = linux_file_kqfilter_read_event,
1139 static struct filterops linux_dev_kqfiltops_write = {
1141 .f_detach = linux_file_kqfilter_detach,
1142 .f_event = linux_file_kqfilter_write_event,
1146 linux_file_kqfilter_poll(struct linux_file *filp, int kqflags)
1149 const struct file_operations *fop;
1150 struct linux_cdev *ldev;
1153 if ((filp->f_kqflags & kqflags) == 0)
1158 linux_get_fop(filp, &fop, &ldev);
1159 /* get the latest polling state */
1160 temp = OPW(filp->_file, td, fop->poll(filp, NULL));
1161 linux_drop_fop(ldev);
1163 spin_lock(&filp->f_kqlock);
1165 filp->f_kqflags &= ~(LINUX_KQ_FLAG_NEED_READ |
1166 LINUX_KQ_FLAG_NEED_WRITE);
1167 /* update kqflags */
1168 if ((temp & (POLLIN | POLLOUT)) != 0) {
1169 if ((temp & POLLIN) != 0)
1170 filp->f_kqflags |= LINUX_KQ_FLAG_NEED_READ;
1171 if ((temp & POLLOUT) != 0)
1172 filp->f_kqflags |= LINUX_KQ_FLAG_NEED_WRITE;
1174 /* make sure the "knote" gets woken up */
1175 KNOTE_LOCKED(&filp->f_selinfo.si_note, 0);
1177 spin_unlock(&filp->f_kqlock);
1181 linux_file_kqfilter(struct file *file, struct knote *kn)
1183 struct linux_file *filp;
1188 filp = (struct linux_file *)file->f_data;
1189 filp->f_flags = file->f_flag;
1190 if (filp->f_op->poll == NULL)
1193 spin_lock(&filp->f_kqlock);
1194 switch (kn->kn_filter) {
1196 filp->f_kqflags |= LINUX_KQ_FLAG_HAS_READ;
1197 kn->kn_fop = &linux_dev_kqfiltops_read;
1199 knlist_add(&filp->f_selinfo.si_note, kn, 1);
1203 filp->f_kqflags |= LINUX_KQ_FLAG_HAS_WRITE;
1204 kn->kn_fop = &linux_dev_kqfiltops_write;
1206 knlist_add(&filp->f_selinfo.si_note, kn, 1);
1213 spin_unlock(&filp->f_kqlock);
1216 linux_set_current(td);
1218 /* update kqfilter status, if any */
1219 linux_file_kqfilter_poll(filp,
1220 LINUX_KQ_FLAG_HAS_READ | LINUX_KQ_FLAG_HAS_WRITE);
1226 linux_file_mmap_single(struct file *fp, const struct file_operations *fop,
1227 vm_ooffset_t *offset, vm_size_t size, struct vm_object **object,
1228 int nprot, struct thread *td)
1230 struct task_struct *task;
1231 struct vm_area_struct *vmap;
1232 struct mm_struct *mm;
1233 struct linux_file *filp;
1237 filp = (struct linux_file *)fp->f_data;
1238 filp->f_flags = fp->f_flag;
1240 if (fop->mmap == NULL)
1241 return (EOPNOTSUPP);
1243 linux_set_current(td);
1246 * The same VM object might be shared by multiple processes
1247 * and the mm_struct is usually freed when a process exits.
1249 * The atomic reference below makes sure the mm_struct is
1250 * available as long as the vmap is in the linux_vma_head.
1254 if (atomic_inc_not_zero(&mm->mm_users) == 0)
1257 vmap = kzalloc(sizeof(*vmap), GFP_KERNEL);
1259 vmap->vm_end = size;
1260 vmap->vm_pgoff = *offset / PAGE_SIZE;
1262 vmap->vm_flags = vmap->vm_page_prot = (nprot & VM_PROT_ALL);
1263 vmap->vm_ops = NULL;
1264 vmap->vm_file = get_file(filp);
1267 if (unlikely(down_write_killable(&vmap->vm_mm->mmap_sem))) {
1268 error = linux_get_error(task, EINTR);
1270 error = -OPW(fp, td, fop->mmap(filp, vmap));
1271 error = linux_get_error(task, error);
1272 up_write(&vmap->vm_mm->mmap_sem);
1276 linux_cdev_handle_free(vmap);
1280 attr = pgprot2cachemode(vmap->vm_page_prot);
1282 if (vmap->vm_ops != NULL) {
1283 struct vm_area_struct *ptr;
1284 void *vm_private_data;
1287 if (vmap->vm_ops->open == NULL ||
1288 vmap->vm_ops->close == NULL ||
1289 vmap->vm_private_data == NULL) {
1290 /* free allocated VM area struct */
1291 linux_cdev_handle_free(vmap);
1295 vm_private_data = vmap->vm_private_data;
1297 rw_wlock(&linux_vma_lock);
1298 TAILQ_FOREACH(ptr, &linux_vma_head, vm_entry) {
1299 if (ptr->vm_private_data == vm_private_data)
1302 /* check if there is an existing VM area struct */
1304 /* check if the VM area structure is invalid */
1305 if (ptr->vm_ops == NULL ||
1306 ptr->vm_ops->open == NULL ||
1307 ptr->vm_ops->close == NULL) {
1312 vm_no_fault = (ptr->vm_ops->fault == NULL);
1315 /* insert VM area structure into list */
1316 TAILQ_INSERT_TAIL(&linux_vma_head, vmap, vm_entry);
1318 vm_no_fault = (vmap->vm_ops->fault == NULL);
1320 rw_wunlock(&linux_vma_lock);
1323 /* free allocated VM area struct */
1324 linux_cdev_handle_free(vmap);
1325 /* check for stale VM area struct */
1326 if (error != EEXIST)
1330 /* check if there is no fault handler */
1332 *object = cdev_pager_allocate(vm_private_data, OBJT_DEVICE,
1333 &linux_cdev_pager_ops[1], size, nprot, *offset,
1336 *object = cdev_pager_allocate(vm_private_data, OBJT_MGTDEVICE,
1337 &linux_cdev_pager_ops[0], size, nprot, *offset,
1341 /* check if allocating the VM object failed */
1342 if (*object == NULL) {
1344 /* remove VM area struct from list */
1345 linux_cdev_handle_remove(vmap);
1346 /* free allocated VM area struct */
1347 linux_cdev_handle_free(vmap);
1354 sg = sglist_alloc(1, M_WAITOK);
1355 sglist_append_phys(sg,
1356 (vm_paddr_t)vmap->vm_pfn << PAGE_SHIFT, vmap->vm_len);
1358 *object = vm_pager_allocate(OBJT_SG, sg, vmap->vm_len,
1359 nprot, 0, td->td_ucred);
1361 linux_cdev_handle_free(vmap);
1363 if (*object == NULL) {
1369 if (attr != VM_MEMATTR_DEFAULT) {
1370 VM_OBJECT_WLOCK(*object);
1371 vm_object_set_memattr(*object, attr);
1372 VM_OBJECT_WUNLOCK(*object);
1378 struct cdevsw linuxcdevsw = {
1379 .d_version = D_VERSION,
1380 .d_fdopen = linux_dev_fdopen,
1381 .d_name = "lkpidev",
1385 linux_file_read(struct file *file, struct uio *uio, struct ucred *active_cred,
1386 int flags, struct thread *td)
1388 struct linux_file *filp;
1389 const struct file_operations *fop;
1390 struct linux_cdev *ldev;
1395 filp = (struct linux_file *)file->f_data;
1396 filp->f_flags = file->f_flag;
1397 /* XXX no support for I/O vectors currently */
1398 if (uio->uio_iovcnt != 1)
1399 return (EOPNOTSUPP);
1400 if (uio->uio_resid > DEVFS_IOSIZE_MAX)
1402 linux_set_current(td);
1403 linux_get_fop(filp, &fop, &ldev);
1404 if (fop->read != NULL) {
1405 bytes = OPW(file, td, fop->read(filp,
1406 uio->uio_iov->iov_base,
1407 uio->uio_iov->iov_len, &uio->uio_offset));
1409 uio->uio_iov->iov_base =
1410 ((uint8_t *)uio->uio_iov->iov_base) + bytes;
1411 uio->uio_iov->iov_len -= bytes;
1412 uio->uio_resid -= bytes;
1414 error = linux_get_error(current, -bytes);
1419 /* update kqfilter status, if any */
1420 linux_file_kqfilter_poll(filp, LINUX_KQ_FLAG_HAS_READ);
1421 linux_drop_fop(ldev);
1427 linux_file_write(struct file *file, struct uio *uio, struct ucred *active_cred,
1428 int flags, struct thread *td)
1430 struct linux_file *filp;
1431 const struct file_operations *fop;
1432 struct linux_cdev *ldev;
1436 filp = (struct linux_file *)file->f_data;
1437 filp->f_flags = file->f_flag;
1438 /* XXX no support for I/O vectors currently */
1439 if (uio->uio_iovcnt != 1)
1440 return (EOPNOTSUPP);
1441 if (uio->uio_resid > DEVFS_IOSIZE_MAX)
1443 linux_set_current(td);
1444 linux_get_fop(filp, &fop, &ldev);
1445 if (fop->write != NULL) {
1446 bytes = OPW(file, td, fop->write(filp,
1447 uio->uio_iov->iov_base,
1448 uio->uio_iov->iov_len, &uio->uio_offset));
1450 uio->uio_iov->iov_base =
1451 ((uint8_t *)uio->uio_iov->iov_base) + bytes;
1452 uio->uio_iov->iov_len -= bytes;
1453 uio->uio_resid -= bytes;
1456 error = linux_get_error(current, -bytes);
1461 /* update kqfilter status, if any */
1462 linux_file_kqfilter_poll(filp, LINUX_KQ_FLAG_HAS_WRITE);
1464 linux_drop_fop(ldev);
1470 linux_file_poll(struct file *file, int events, struct ucred *active_cred,
1473 struct linux_file *filp;
1474 const struct file_operations *fop;
1475 struct linux_cdev *ldev;
1478 filp = (struct linux_file *)file->f_data;
1479 filp->f_flags = file->f_flag;
1480 linux_set_current(td);
1481 linux_get_fop(filp, &fop, &ldev);
1482 if (fop->poll != NULL) {
1483 revents = OPW(file, td, fop->poll(filp,
1484 LINUX_POLL_TABLE_NORMAL)) & events;
1488 linux_drop_fop(ldev);
1493 linux_file_close(struct file *file, struct thread *td)
1495 struct linux_file *filp;
1496 const struct file_operations *fop;
1497 struct linux_cdev *ldev;
1500 filp = (struct linux_file *)file->f_data;
1502 KASSERT(file_count(filp) == 0,
1503 ("File refcount(%d) is not zero", file_count(filp)));
1506 filp->f_flags = file->f_flag;
1507 linux_set_current(td);
1508 linux_poll_wait_dequeue(filp);
1509 linux_get_fop(filp, &fop, &ldev);
1510 if (fop->release != NULL)
1511 error = -OPW(file, td, fop->release(filp->f_vnode, filp));
1512 funsetown(&filp->f_sigio);
1513 if (filp->f_vnode != NULL)
1514 vdrop(filp->f_vnode);
1515 linux_drop_fop(ldev);
1516 if (filp->f_cdev != NULL)
1517 linux_cdev_deref(filp->f_cdev);
1524 linux_file_ioctl(struct file *fp, u_long cmd, void *data, struct ucred *cred,
1527 struct linux_file *filp;
1528 const struct file_operations *fop;
1529 struct linux_cdev *ldev;
1533 filp = (struct linux_file *)fp->f_data;
1534 filp->f_flags = fp->f_flag;
1535 linux_get_fop(filp, &fop, &ldev);
1537 linux_set_current(td);
1542 if (fop->fasync == NULL)
1544 error = -OPW(fp, td, fop->fasync(0, filp, fp->f_flag & FASYNC));
1547 error = fsetown(*(int *)data, &filp->f_sigio);
1549 if (fop->fasync == NULL)
1551 error = -OPW(fp, td, fop->fasync(0, filp,
1552 fp->f_flag & FASYNC));
1556 *(int *)data = fgetown(&filp->f_sigio);
1559 error = linux_file_ioctl_sub(fp, filp, fop, cmd, data, td);
1562 linux_drop_fop(ldev);
1567 linux_file_mmap_sub(struct thread *td, vm_size_t objsize, vm_prot_t prot,
1568 vm_prot_t *maxprotp, int *flagsp, struct file *fp,
1569 vm_ooffset_t *foff, const struct file_operations *fop, vm_object_t *objp)
1572 * Character devices do not provide private mappings
1575 if ((*maxprotp & VM_PROT_WRITE) == 0 &&
1576 (prot & VM_PROT_WRITE) != 0)
1578 if ((*flagsp & (MAP_PRIVATE | MAP_COPY)) != 0)
1581 return (linux_file_mmap_single(fp, fop, foff, objsize, objp,
1586 linux_file_mmap(struct file *fp, vm_map_t map, vm_offset_t *addr, vm_size_t size,
1587 vm_prot_t prot, vm_prot_t cap_maxprot, int flags, vm_ooffset_t foff,
1590 struct linux_file *filp;
1591 const struct file_operations *fop;
1592 struct linux_cdev *ldev;
1599 filp = (struct linux_file *)fp->f_data;
1603 return (EOPNOTSUPP);
1606 * Ensure that file and memory protections are
1610 if (mp != NULL && (mp->mnt_flag & MNT_NOEXEC) != 0) {
1611 maxprot = VM_PROT_NONE;
1612 if ((prot & VM_PROT_EXECUTE) != 0)
1615 maxprot = VM_PROT_EXECUTE;
1616 if ((fp->f_flag & FREAD) != 0)
1617 maxprot |= VM_PROT_READ;
1618 else if ((prot & VM_PROT_READ) != 0)
1622 * If we are sharing potential changes via MAP_SHARED and we
1623 * are trying to get write permission although we opened it
1624 * without asking for it, bail out.
1626 * Note that most character devices always share mappings.
1628 * Rely on linux_file_mmap_sub() to fail invalid MAP_PRIVATE
1629 * requests rather than doing it here.
1631 if ((flags & MAP_SHARED) != 0) {
1632 if ((fp->f_flag & FWRITE) != 0)
1633 maxprot |= VM_PROT_WRITE;
1634 else if ((prot & VM_PROT_WRITE) != 0)
1637 maxprot &= cap_maxprot;
1639 linux_get_fop(filp, &fop, &ldev);
1640 error = linux_file_mmap_sub(td, size, prot, &maxprot, &flags, fp,
1641 &foff, fop, &object);
1645 error = vm_mmap_object(map, addr, size, prot, maxprot, flags, object,
1648 vm_object_deallocate(object);
1650 linux_drop_fop(ldev);
1655 linux_file_stat(struct file *fp, struct stat *sb, struct ucred *active_cred,
1658 struct linux_file *filp;
1662 filp = (struct linux_file *)fp->f_data;
1663 if (filp->f_vnode == NULL)
1664 return (EOPNOTSUPP);
1668 vn_lock(vp, LK_SHARED | LK_RETRY);
1669 error = vn_stat(vp, sb, td->td_ucred, NOCRED, td);
1676 linux_file_fill_kinfo(struct file *fp, struct kinfo_file *kif,
1677 struct filedesc *fdp)
1679 struct linux_file *filp;
1687 kif->kf_type = KF_TYPE_DEV;
1690 FILEDESC_SUNLOCK(fdp);
1691 error = vn_fill_kinfo_vnode(vp, kif);
1693 kif->kf_type = KF_TYPE_VNODE;
1694 FILEDESC_SLOCK(fdp);
1700 linux_iminor(struct inode *inode)
1702 struct linux_cdev *ldev;
1704 if (inode == NULL || inode->v_rdev == NULL ||
1705 inode->v_rdev->si_devsw != &linuxcdevsw)
1707 ldev = inode->v_rdev->si_drv1;
1711 return (minor(ldev->dev));
1714 struct fileops linuxfileops = {
1715 .fo_read = linux_file_read,
1716 .fo_write = linux_file_write,
1717 .fo_truncate = invfo_truncate,
1718 .fo_kqfilter = linux_file_kqfilter,
1719 .fo_stat = linux_file_stat,
1720 .fo_fill_kinfo = linux_file_fill_kinfo,
1721 .fo_poll = linux_file_poll,
1722 .fo_close = linux_file_close,
1723 .fo_ioctl = linux_file_ioctl,
1724 .fo_mmap = linux_file_mmap,
1725 .fo_chmod = invfo_chmod,
1726 .fo_chown = invfo_chown,
1727 .fo_sendfile = invfo_sendfile,
1728 .fo_flags = DFLAG_PASSABLE,
1732 * Hash of vmmap addresses. This is infrequently accessed and does not
1733 * need to be particularly large. This is done because we must store the
1734 * caller's idea of the map size to properly unmap.
1737 LIST_ENTRY(vmmap) vm_next;
1739 unsigned long vm_size;
1743 struct vmmap *lh_first;
1745 #define VMMAP_HASH_SIZE 64
1746 #define VMMAP_HASH_MASK (VMMAP_HASH_SIZE - 1)
1747 #define VM_HASH(addr) ((uintptr_t)(addr) >> PAGE_SHIFT) & VMMAP_HASH_MASK
1748 static struct vmmaphd vmmaphead[VMMAP_HASH_SIZE];
1749 static struct mtx vmmaplock;
1752 vmmap_add(void *addr, unsigned long size)
1754 struct vmmap *vmmap;
1756 vmmap = kmalloc(sizeof(*vmmap), GFP_KERNEL);
1757 mtx_lock(&vmmaplock);
1758 vmmap->vm_size = size;
1759 vmmap->vm_addr = addr;
1760 LIST_INSERT_HEAD(&vmmaphead[VM_HASH(addr)], vmmap, vm_next);
1761 mtx_unlock(&vmmaplock);
1764 static struct vmmap *
1765 vmmap_remove(void *addr)
1767 struct vmmap *vmmap;
1769 mtx_lock(&vmmaplock);
1770 LIST_FOREACH(vmmap, &vmmaphead[VM_HASH(addr)], vm_next)
1771 if (vmmap->vm_addr == addr)
1774 LIST_REMOVE(vmmap, vm_next);
1775 mtx_unlock(&vmmaplock);
1780 #if defined(__i386__) || defined(__amd64__) || defined(__powerpc__)
1782 _ioremap_attr(vm_paddr_t phys_addr, unsigned long size, int attr)
1786 addr = pmap_mapdev_attr(phys_addr, size, attr);
1789 vmmap_add(addr, size);
1798 struct vmmap *vmmap;
1800 vmmap = vmmap_remove(addr);
1803 #if defined(__i386__) || defined(__amd64__) || defined(__powerpc__)
1804 pmap_unmapdev((vm_offset_t)addr, vmmap->vm_size);
1811 vmap(struct page **pages, unsigned int count, unsigned long flags, int prot)
1816 size = count * PAGE_SIZE;
1817 off = kva_alloc(size);
1820 vmmap_add((void *)off, size);
1821 pmap_qenter(off, pages, count);
1823 return ((void *)off);
1829 struct vmmap *vmmap;
1831 vmmap = vmmap_remove(addr);
1834 pmap_qremove((vm_offset_t)addr, vmmap->vm_size / PAGE_SIZE);
1835 kva_free((vm_offset_t)addr, vmmap->vm_size);
1840 kvasprintf(gfp_t gfp, const char *fmt, va_list ap)
1847 len = vsnprintf(NULL, 0, fmt, aq);
1850 p = kmalloc(len + 1, gfp);
1852 vsnprintf(p, len + 1, fmt, ap);
1858 kasprintf(gfp_t gfp, const char *fmt, ...)
1864 p = kvasprintf(gfp, fmt, ap);
1871 linux_timer_callback_wrapper(void *context)
1873 struct timer_list *timer;
1875 linux_set_current(curthread);
1878 timer->function(timer->data);
1882 mod_timer(struct timer_list *timer, int expires)
1885 timer->expires = expires;
1886 callout_reset(&timer->callout,
1887 linux_timer_jiffies_until(expires),
1888 &linux_timer_callback_wrapper, timer);
1892 add_timer(struct timer_list *timer)
1895 callout_reset(&timer->callout,
1896 linux_timer_jiffies_until(timer->expires),
1897 &linux_timer_callback_wrapper, timer);
1901 add_timer_on(struct timer_list *timer, int cpu)
1904 callout_reset_on(&timer->callout,
1905 linux_timer_jiffies_until(timer->expires),
1906 &linux_timer_callback_wrapper, timer, cpu);
1910 linux_timer_init(void *arg)
1914 * Compute an internal HZ value which can divide 2**32 to
1915 * avoid timer rounding problems when the tick value wraps
1918 linux_timer_hz_mask = 1;
1919 while (linux_timer_hz_mask < (unsigned long)hz)
1920 linux_timer_hz_mask *= 2;
1921 linux_timer_hz_mask--;
1923 SYSINIT(linux_timer, SI_SUB_DRIVERS, SI_ORDER_FIRST, linux_timer_init, NULL);
1926 linux_complete_common(struct completion *c, int all)
1933 wakeup_swapper = sleepq_broadcast(c, SLEEPQ_SLEEP, 0, 0);
1935 if (c->done != UINT_MAX)
1937 wakeup_swapper = sleepq_signal(c, SLEEPQ_SLEEP, 0, 0);
1945 * Indefinite wait for done != 0 with or without signals.
1948 linux_wait_for_common(struct completion *c, int flags)
1950 struct task_struct *task;
1953 if (SCHEDULER_STOPPED())
1959 flags = SLEEPQ_INTERRUPTIBLE | SLEEPQ_SLEEP;
1961 flags = SLEEPQ_SLEEP;
1967 sleepq_add(c, NULL, "completion", flags, 0);
1968 if (flags & SLEEPQ_INTERRUPTIBLE) {
1970 error = -sleepq_wait_sig(c, 0);
1973 linux_schedule_save_interrupt_value(task, error);
1974 error = -ERESTARTSYS;
1983 if (c->done != UINT_MAX)
1992 * Time limited wait for done != 0 with or without signals.
1995 linux_wait_for_timeout_common(struct completion *c, int timeout, int flags)
1997 struct task_struct *task;
1998 int end = jiffies + timeout;
2001 if (SCHEDULER_STOPPED())
2007 flags = SLEEPQ_INTERRUPTIBLE | SLEEPQ_SLEEP;
2009 flags = SLEEPQ_SLEEP;
2015 sleepq_add(c, NULL, "completion", flags, 0);
2016 sleepq_set_timeout(c, linux_timer_jiffies_until(end));
2019 if (flags & SLEEPQ_INTERRUPTIBLE)
2020 error = -sleepq_timedwait_sig(c, 0);
2022 error = -sleepq_timedwait(c, 0);
2026 /* check for timeout */
2027 if (error == -EWOULDBLOCK) {
2028 error = 0; /* timeout */
2030 /* signal happened */
2031 linux_schedule_save_interrupt_value(task, error);
2032 error = -ERESTARTSYS;
2037 if (c->done != UINT_MAX)
2041 /* return how many jiffies are left */
2042 error = linux_timer_jiffies_until(end);
2048 linux_try_wait_for_completion(struct completion *c)
2053 isdone = (c->done != 0);
2054 if (c->done != 0 && c->done != UINT_MAX)
2061 linux_completion_done(struct completion *c)
2066 isdone = (c->done != 0);
2072 linux_cdev_deref(struct linux_cdev *ldev)
2075 if (refcount_release(&ldev->refs))
2080 linux_cdev_release(struct kobject *kobj)
2082 struct linux_cdev *cdev;
2083 struct kobject *parent;
2085 cdev = container_of(kobj, struct linux_cdev, kobj);
2086 parent = kobj->parent;
2087 linux_destroy_dev(cdev);
2088 linux_cdev_deref(cdev);
2089 kobject_put(parent);
2093 linux_cdev_static_release(struct kobject *kobj)
2095 struct linux_cdev *cdev;
2096 struct kobject *parent;
2098 cdev = container_of(kobj, struct linux_cdev, kobj);
2099 parent = kobj->parent;
2100 linux_destroy_dev(cdev);
2101 kobject_put(parent);
2105 linux_destroy_dev(struct linux_cdev *ldev)
2108 if (ldev->cdev == NULL)
2111 MPASS((ldev->siref & LDEV_SI_DTR) == 0);
2112 atomic_set_int(&ldev->siref, LDEV_SI_DTR);
2113 while ((atomic_load_int(&ldev->siref) & ~LDEV_SI_DTR) != 0)
2114 pause("ldevdtr", hz / 4);
2116 destroy_dev(ldev->cdev);
2120 const struct kobj_type linux_cdev_ktype = {
2121 .release = linux_cdev_release,
2124 const struct kobj_type linux_cdev_static_ktype = {
2125 .release = linux_cdev_static_release,
2129 linux_handle_ifnet_link_event(void *arg, struct ifnet *ifp, int linkstate)
2131 struct notifier_block *nb;
2134 if (linkstate == LINK_STATE_UP)
2135 nb->notifier_call(nb, NETDEV_UP, ifp);
2137 nb->notifier_call(nb, NETDEV_DOWN, ifp);
2141 linux_handle_ifnet_arrival_event(void *arg, struct ifnet *ifp)
2143 struct notifier_block *nb;
2146 nb->notifier_call(nb, NETDEV_REGISTER, ifp);
2150 linux_handle_ifnet_departure_event(void *arg, struct ifnet *ifp)
2152 struct notifier_block *nb;
2155 nb->notifier_call(nb, NETDEV_UNREGISTER, ifp);
2159 linux_handle_iflladdr_event(void *arg, struct ifnet *ifp)
2161 struct notifier_block *nb;
2164 nb->notifier_call(nb, NETDEV_CHANGEADDR, ifp);
2168 linux_handle_ifaddr_event(void *arg, struct ifnet *ifp)
2170 struct notifier_block *nb;
2173 nb->notifier_call(nb, NETDEV_CHANGEIFADDR, ifp);
2177 register_netdevice_notifier(struct notifier_block *nb)
2180 nb->tags[NETDEV_UP] = EVENTHANDLER_REGISTER(
2181 ifnet_link_event, linux_handle_ifnet_link_event, nb, 0);
2182 nb->tags[NETDEV_REGISTER] = EVENTHANDLER_REGISTER(
2183 ifnet_arrival_event, linux_handle_ifnet_arrival_event, nb, 0);
2184 nb->tags[NETDEV_UNREGISTER] = EVENTHANDLER_REGISTER(
2185 ifnet_departure_event, linux_handle_ifnet_departure_event, nb, 0);
2186 nb->tags[NETDEV_CHANGEADDR] = EVENTHANDLER_REGISTER(
2187 iflladdr_event, linux_handle_iflladdr_event, nb, 0);
2193 register_inetaddr_notifier(struct notifier_block *nb)
2196 nb->tags[NETDEV_CHANGEIFADDR] = EVENTHANDLER_REGISTER(
2197 ifaddr_event, linux_handle_ifaddr_event, nb, 0);
2202 unregister_netdevice_notifier(struct notifier_block *nb)
2205 EVENTHANDLER_DEREGISTER(ifnet_link_event,
2206 nb->tags[NETDEV_UP]);
2207 EVENTHANDLER_DEREGISTER(ifnet_arrival_event,
2208 nb->tags[NETDEV_REGISTER]);
2209 EVENTHANDLER_DEREGISTER(ifnet_departure_event,
2210 nb->tags[NETDEV_UNREGISTER]);
2211 EVENTHANDLER_DEREGISTER(iflladdr_event,
2212 nb->tags[NETDEV_CHANGEADDR]);
2218 unregister_inetaddr_notifier(struct notifier_block *nb)
2221 EVENTHANDLER_DEREGISTER(ifaddr_event,
2222 nb->tags[NETDEV_CHANGEIFADDR]);
2227 struct list_sort_thunk {
2228 int (*cmp)(void *, struct list_head *, struct list_head *);
2233 linux_le_cmp(void *priv, const void *d1, const void *d2)
2235 struct list_head *le1, *le2;
2236 struct list_sort_thunk *thunk;
2239 le1 = *(__DECONST(struct list_head **, d1));
2240 le2 = *(__DECONST(struct list_head **, d2));
2241 return ((thunk->cmp)(thunk->priv, le1, le2));
2245 list_sort(void *priv, struct list_head *head, int (*cmp)(void *priv,
2246 struct list_head *a, struct list_head *b))
2248 struct list_sort_thunk thunk;
2249 struct list_head **ar, *le;
2253 list_for_each(le, head)
2255 ar = malloc(sizeof(struct list_head *) * count, M_KMALLOC, M_WAITOK);
2257 list_for_each(le, head)
2261 qsort_r(ar, count, sizeof(struct list_head *), &thunk, linux_le_cmp);
2262 INIT_LIST_HEAD(head);
2263 for (i = 0; i < count; i++)
2264 list_add_tail(ar[i], head);
2265 free(ar, M_KMALLOC);
2269 linux_irq_handler(void *ent)
2271 struct irq_ent *irqe;
2273 linux_set_current(curthread);
2276 irqe->handler(irqe->irq, irqe->arg);
2279 #if defined(__i386__) || defined(__amd64__)
2281 linux_wbinvd_on_all_cpus(void)
2284 pmap_invalidate_cache();
2290 linux_on_each_cpu(void callback(void *), void *data)
2293 smp_rendezvous(smp_no_rendezvous_barrier, callback,
2294 smp_no_rendezvous_barrier, data);
2299 linux_in_atomic(void)
2302 return ((curthread->td_pflags & TDP_NOFAULTING) != 0);
2306 linux_find_cdev(const char *name, unsigned major, unsigned minor)
2308 dev_t dev = MKDEV(major, minor);
2312 LIST_FOREACH(cdev, &linuxcdevsw.d_devs, si_list) {
2313 struct linux_cdev *ldev = cdev->si_drv1;
2314 if (ldev->dev == dev &&
2315 strcmp(kobject_name(&ldev->kobj), name) == 0) {
2321 return (cdev != NULL ? cdev->si_drv1 : NULL);
2325 __register_chrdev(unsigned int major, unsigned int baseminor,
2326 unsigned int count, const char *name,
2327 const struct file_operations *fops)
2329 struct linux_cdev *cdev;
2333 for (i = baseminor; i < baseminor + count; i++) {
2334 cdev = cdev_alloc();
2336 kobject_set_name(&cdev->kobj, name);
2338 ret = cdev_add(cdev, makedev(major, i), 1);
2346 __register_chrdev_p(unsigned int major, unsigned int baseminor,
2347 unsigned int count, const char *name,
2348 const struct file_operations *fops, uid_t uid,
2349 gid_t gid, int mode)
2351 struct linux_cdev *cdev;
2355 for (i = baseminor; i < baseminor + count; i++) {
2356 cdev = cdev_alloc();
2358 kobject_set_name(&cdev->kobj, name);
2360 ret = cdev_add_ext(cdev, makedev(major, i), uid, gid, mode);
2368 __unregister_chrdev(unsigned int major, unsigned int baseminor,
2369 unsigned int count, const char *name)
2371 struct linux_cdev *cdevp;
2374 for (i = baseminor; i < baseminor + count; i++) {
2375 cdevp = linux_find_cdev(name, major, i);
2382 linux_dump_stack(void)
2393 #if defined(__i386__) || defined(__amd64__)
2394 bool linux_cpu_has_clflush;
2398 linux_compat_init(void *arg)
2400 struct sysctl_oid *rootoid;
2403 #if defined(__i386__) || defined(__amd64__)
2404 linux_cpu_has_clflush = (cpu_feature & CPUID_CLFSH);
2406 rw_init(&linux_vma_lock, "lkpi-vma-lock");
2408 rootoid = SYSCTL_ADD_ROOT_NODE(NULL,
2409 OID_AUTO, "sys", CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, "sys");
2410 kobject_init(&linux_class_root, &linux_class_ktype);
2411 kobject_set_name(&linux_class_root, "class");
2412 linux_class_root.oidp = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(rootoid),
2413 OID_AUTO, "class", CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, "class");
2414 kobject_init(&linux_root_device.kobj, &linux_dev_ktype);
2415 kobject_set_name(&linux_root_device.kobj, "device");
2416 linux_root_device.kobj.oidp = SYSCTL_ADD_NODE(NULL,
2417 SYSCTL_CHILDREN(rootoid), OID_AUTO, "device", CTLFLAG_RD, NULL,
2419 linux_root_device.bsddev = root_bus;
2420 linux_class_misc.name = "misc";
2421 class_register(&linux_class_misc);
2422 INIT_LIST_HEAD(&pci_drivers);
2423 INIT_LIST_HEAD(&pci_devices);
2424 spin_lock_init(&pci_lock);
2425 mtx_init(&vmmaplock, "IO Map lock", NULL, MTX_DEF);
2426 for (i = 0; i < VMMAP_HASH_SIZE; i++)
2427 LIST_INIT(&vmmaphead[i]);
2429 SYSINIT(linux_compat, SI_SUB_DRIVERS, SI_ORDER_SECOND, linux_compat_init, NULL);
2432 linux_compat_uninit(void *arg)
2434 linux_kobject_kfree_name(&linux_class_root);
2435 linux_kobject_kfree_name(&linux_root_device.kobj);
2436 linux_kobject_kfree_name(&linux_class_misc.kobj);
2438 mtx_destroy(&vmmaplock);
2439 spin_lock_destroy(&pci_lock);
2440 rw_destroy(&linux_vma_lock);
2442 SYSUNINIT(linux_compat, SI_SUB_DRIVERS, SI_ORDER_SECOND, linux_compat_uninit, NULL);
2445 * NOTE: Linux frequently uses "unsigned long" for pointer to integer
2446 * conversion and vice versa, where in FreeBSD "uintptr_t" would be
2447 * used. Assert these types have the same size, else some parts of the
2448 * LinuxKPI may not work like expected:
2450 CTASSERT(sizeof(unsigned long) == sizeof(uintptr_t));