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/eventhandler.h>
48 #include <sys/fcntl.h>
50 #include <sys/filio.h>
51 #include <sys/rwlock.h>
53 #include <sys/stack.h>
58 #include <vm/vm_object.h>
59 #include <vm/vm_page.h>
60 #include <vm/vm_pager.h>
62 #include <machine/stdarg.h>
64 #if defined(__i386__) || defined(__amd64__)
65 #include <machine/md_var.h>
68 #include <linux/kobject.h>
69 #include <linux/device.h>
70 #include <linux/slab.h>
71 #include <linux/module.h>
72 #include <linux/moduleparam.h>
73 #include <linux/cdev.h>
74 #include <linux/file.h>
75 #include <linux/sysfs.h>
78 #include <linux/vmalloc.h>
79 #include <linux/netdevice.h>
80 #include <linux/timer.h>
81 #include <linux/interrupt.h>
82 #include <linux/uaccess.h>
83 #include <linux/list.h>
84 #include <linux/kthread.h>
85 #include <linux/kernel.h>
86 #include <linux/compat.h>
87 #include <linux/poll.h>
88 #include <linux/smp.h>
90 #if defined(__i386__) || defined(__amd64__)
94 SYSCTL_NODE(_compat, OID_AUTO, linuxkpi, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
95 "LinuxKPI parameters");
98 SYSCTL_INT(_compat_linuxkpi, OID_AUTO, debug, CTLFLAG_RWTUN,
99 &linuxkpi_debug, 0, "Set to enable pr_debug() prints. Clear to disable.");
101 MALLOC_DEFINE(M_KMALLOC, "linux", "Linux kmalloc compat");
103 #include <linux/rbtree.h>
104 /* Undo Linux compat changes. */
108 #define RB_ROOT(head) (head)->rbh_root
110 static void linux_cdev_deref(struct linux_cdev *ldev);
111 static struct vm_area_struct *linux_cdev_handle_find(void *handle);
113 struct kobject linux_class_root;
114 struct device linux_root_device;
115 struct class linux_class_misc;
116 struct list_head pci_drivers;
117 struct list_head pci_devices;
120 unsigned long linux_timer_hz_mask;
123 panic_cmp(struct rb_node *one, struct rb_node *two)
128 RB_GENERATE(linux_root, rb_node, __entry, panic_cmp);
131 kobject_set_name_vargs(struct kobject *kobj, const char *fmt, va_list args)
141 if (old && fmt == NULL)
144 /* compute length of string */
145 va_copy(tmp_va, args);
146 len = vsnprintf(&dummy, 0, fmt, tmp_va);
149 /* account for zero termination */
152 /* check for error */
156 /* allocate memory for string */
157 name = kzalloc(len, GFP_KERNEL);
160 vsnprintf(name, len, fmt, args);
163 /* free old string */
166 /* filter new string */
167 for (; *name != '\0'; name++)
174 kobject_set_name(struct kobject *kobj, const char *fmt, ...)
180 error = kobject_set_name_vargs(kobj, fmt, args);
187 kobject_add_complete(struct kobject *kobj, struct kobject *parent)
189 const struct kobj_type *t;
192 kobj->parent = parent;
193 error = sysfs_create_dir(kobj);
194 if (error == 0 && kobj->ktype && kobj->ktype->default_attrs) {
195 struct attribute **attr;
198 for (attr = t->default_attrs; *attr != NULL; attr++) {
199 error = sysfs_create_file(kobj, *attr);
204 sysfs_remove_dir(kobj);
211 kobject_add(struct kobject *kobj, struct kobject *parent, const char *fmt, ...)
217 error = kobject_set_name_vargs(kobj, fmt, args);
222 return kobject_add_complete(kobj, parent);
226 linux_kobject_release(struct kref *kref)
228 struct kobject *kobj;
231 kobj = container_of(kref, struct kobject, kref);
232 sysfs_remove_dir(kobj);
234 if (kobj->ktype && kobj->ktype->release)
235 kobj->ktype->release(kobj);
240 linux_kobject_kfree(struct kobject *kobj)
246 linux_kobject_kfree_name(struct kobject *kobj)
253 const struct kobj_type linux_kfree_type = {
254 .release = linux_kobject_kfree
258 linux_device_release(struct device *dev)
260 pr_debug("linux_device_release: %s\n", dev_name(dev));
265 linux_class_show(struct kobject *kobj, struct attribute *attr, char *buf)
267 struct class_attribute *dattr;
270 dattr = container_of(attr, struct class_attribute, attr);
273 error = dattr->show(container_of(kobj, struct class, kobj),
279 linux_class_store(struct kobject *kobj, struct attribute *attr, const char *buf,
282 struct class_attribute *dattr;
285 dattr = container_of(attr, struct class_attribute, attr);
288 error = dattr->store(container_of(kobj, struct class, kobj),
294 linux_class_release(struct kobject *kobj)
298 class = container_of(kobj, struct class, kobj);
299 if (class->class_release)
300 class->class_release(class);
303 static const struct sysfs_ops linux_class_sysfs = {
304 .show = linux_class_show,
305 .store = linux_class_store,
308 const struct kobj_type linux_class_ktype = {
309 .release = linux_class_release,
310 .sysfs_ops = &linux_class_sysfs
314 linux_dev_release(struct kobject *kobj)
318 dev = container_of(kobj, struct device, kobj);
319 /* This is the precedence defined by linux. */
322 else if (dev->class && dev->class->dev_release)
323 dev->class->dev_release(dev);
327 linux_dev_show(struct kobject *kobj, struct attribute *attr, char *buf)
329 struct device_attribute *dattr;
332 dattr = container_of(attr, struct device_attribute, attr);
335 error = dattr->show(container_of(kobj, struct device, kobj),
341 linux_dev_store(struct kobject *kobj, struct attribute *attr, const char *buf,
344 struct device_attribute *dattr;
347 dattr = container_of(attr, struct device_attribute, attr);
350 error = dattr->store(container_of(kobj, struct device, kobj),
355 static const struct sysfs_ops linux_dev_sysfs = {
356 .show = linux_dev_show,
357 .store = linux_dev_store,
360 const struct kobj_type linux_dev_ktype = {
361 .release = linux_dev_release,
362 .sysfs_ops = &linux_dev_sysfs
366 device_create(struct class *class, struct device *parent, dev_t devt,
367 void *drvdata, const char *fmt, ...)
372 dev = kzalloc(sizeof(*dev), M_WAITOK);
373 dev->parent = parent;
376 dev->driver_data = drvdata;
377 dev->release = linux_device_release;
379 kobject_set_name_vargs(&dev->kobj, fmt, args);
381 device_register(dev);
387 kobject_init_and_add(struct kobject *kobj, const struct kobj_type *ktype,
388 struct kobject *parent, const char *fmt, ...)
393 kobject_init(kobj, ktype);
395 kobj->parent = parent;
399 error = kobject_set_name_vargs(kobj, fmt, args);
403 return kobject_add_complete(kobj, parent);
407 linux_kq_lock(void *arg)
414 linux_kq_unlock(void *arg)
422 linux_kq_lock_owned(void *arg)
427 mtx_assert(&s->m, MA_OWNED);
432 linux_kq_lock_unowned(void *arg)
437 mtx_assert(&s->m, MA_NOTOWNED);
442 linux_file_kqfilter_poll(struct linux_file *, int);
445 linux_file_alloc(void)
447 struct linux_file *filp;
449 filp = kzalloc(sizeof(*filp), GFP_KERNEL);
451 /* set initial refcount */
454 /* setup fields needed by kqueue support */
455 spin_lock_init(&filp->f_kqlock);
456 knlist_init(&filp->f_selinfo.si_note, &filp->f_kqlock,
457 linux_kq_lock, linux_kq_unlock,
458 linux_kq_lock_owned, linux_kq_lock_unowned);
464 linux_file_free(struct linux_file *filp)
466 if (filp->_file == NULL) {
467 if (filp->f_shmem != NULL)
468 vm_object_deallocate(filp->f_shmem);
472 * The close method of the character device or file
473 * will free the linux_file structure:
475 _fdrop(filp->_file, curthread);
480 linux_cdev_pager_fault(vm_object_t vm_obj, vm_ooffset_t offset, int prot,
483 struct vm_area_struct *vmap;
485 vmap = linux_cdev_handle_find(vm_obj->handle);
488 MPASS(vmap->vm_private_data == vm_obj->handle);
490 if (likely(vmap->vm_ops != NULL && offset < vmap->vm_len)) {
491 vm_paddr_t paddr = IDX_TO_OFF(vmap->vm_pfn) + offset;
494 if (((*mres)->flags & PG_FICTITIOUS) != 0) {
496 * If the passed in result page is a fake
497 * page, update it with the new physical
501 vm_page_updatefake(page, paddr, vm_obj->memattr);
504 * Replace the passed in "mres" page with our
505 * own fake page and free up the all of the
508 VM_OBJECT_WUNLOCK(vm_obj);
509 page = vm_page_getfake(paddr, vm_obj->memattr);
510 VM_OBJECT_WLOCK(vm_obj);
512 vm_page_replace(page, vm_obj, (*mres)->pindex, *mres);
516 return (VM_PAGER_OK);
518 return (VM_PAGER_FAIL);
522 linux_cdev_pager_populate(vm_object_t vm_obj, vm_pindex_t pidx, int fault_type,
523 vm_prot_t max_prot, vm_pindex_t *first, vm_pindex_t *last)
525 struct vm_area_struct *vmap;
528 /* get VM area structure */
529 vmap = linux_cdev_handle_find(vm_obj->handle);
531 MPASS(vmap->vm_private_data == vm_obj->handle);
533 VM_OBJECT_WUNLOCK(vm_obj);
535 linux_set_current(curthread);
537 down_write(&vmap->vm_mm->mmap_sem);
538 if (unlikely(vmap->vm_ops == NULL)) {
539 err = VM_FAULT_SIGBUS;
543 /* fill out VM fault structure */
544 vmf.virtual_address = (void *)(uintptr_t)IDX_TO_OFF(pidx);
545 vmf.flags = (fault_type & VM_PROT_WRITE) ? FAULT_FLAG_WRITE : 0;
550 vmap->vm_pfn_count = 0;
551 vmap->vm_pfn_pcount = &vmap->vm_pfn_count;
552 vmap->vm_obj = vm_obj;
554 err = vmap->vm_ops->fault(vmap, &vmf);
556 while (vmap->vm_pfn_count == 0 && err == VM_FAULT_NOPAGE) {
557 kern_yield(PRI_USER);
558 err = vmap->vm_ops->fault(vmap, &vmf);
562 /* translate return code */
565 err = VM_PAGER_AGAIN;
567 case VM_FAULT_SIGBUS:
570 case VM_FAULT_NOPAGE:
572 * By contract the fault handler will return having
573 * busied all the pages itself. If pidx is already
574 * found in the object, it will simply xbusy the first
575 * page and return with vm_pfn_count set to 1.
577 *first = vmap->vm_pfn_first;
578 *last = *first + vmap->vm_pfn_count - 1;
582 err = VM_PAGER_ERROR;
585 up_write(&vmap->vm_mm->mmap_sem);
586 VM_OBJECT_WLOCK(vm_obj);
590 static struct rwlock linux_vma_lock;
591 static TAILQ_HEAD(, vm_area_struct) linux_vma_head =
592 TAILQ_HEAD_INITIALIZER(linux_vma_head);
595 linux_cdev_handle_free(struct vm_area_struct *vmap)
597 /* Drop reference on vm_file */
598 if (vmap->vm_file != NULL)
601 /* Drop reference on mm_struct */
608 linux_cdev_handle_remove(struct vm_area_struct *vmap)
610 rw_wlock(&linux_vma_lock);
611 TAILQ_REMOVE(&linux_vma_head, vmap, vm_entry);
612 rw_wunlock(&linux_vma_lock);
615 static struct vm_area_struct *
616 linux_cdev_handle_find(void *handle)
618 struct vm_area_struct *vmap;
620 rw_rlock(&linux_vma_lock);
621 TAILQ_FOREACH(vmap, &linux_vma_head, vm_entry) {
622 if (vmap->vm_private_data == handle)
625 rw_runlock(&linux_vma_lock);
630 linux_cdev_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
631 vm_ooffset_t foff, struct ucred *cred, u_short *color)
634 MPASS(linux_cdev_handle_find(handle) != NULL);
640 linux_cdev_pager_dtor(void *handle)
642 const struct vm_operations_struct *vm_ops;
643 struct vm_area_struct *vmap;
645 vmap = linux_cdev_handle_find(handle);
649 * Remove handle before calling close operation to prevent
650 * other threads from reusing the handle pointer.
652 linux_cdev_handle_remove(vmap);
654 down_write(&vmap->vm_mm->mmap_sem);
655 vm_ops = vmap->vm_ops;
656 if (likely(vm_ops != NULL))
658 up_write(&vmap->vm_mm->mmap_sem);
660 linux_cdev_handle_free(vmap);
663 static struct cdev_pager_ops linux_cdev_pager_ops[2] = {
666 .cdev_pg_populate = linux_cdev_pager_populate,
667 .cdev_pg_ctor = linux_cdev_pager_ctor,
668 .cdev_pg_dtor = linux_cdev_pager_dtor
672 .cdev_pg_fault = linux_cdev_pager_fault,
673 .cdev_pg_ctor = linux_cdev_pager_ctor,
674 .cdev_pg_dtor = linux_cdev_pager_dtor
679 zap_vma_ptes(struct vm_area_struct *vma, unsigned long address,
686 if (obj == NULL || (obj->flags & OBJ_UNMANAGED) != 0)
688 VM_OBJECT_RLOCK(obj);
689 for (m = vm_page_find_least(obj, OFF_TO_IDX(address));
690 m != NULL && m->pindex < OFF_TO_IDX(address + size);
691 m = TAILQ_NEXT(m, listq))
693 VM_OBJECT_RUNLOCK(obj);
697 static struct file_operations dummy_ldev_ops = {
701 static struct linux_cdev dummy_ldev = {
702 .ops = &dummy_ldev_ops,
705 #define LDEV_SI_DTR 0x0001
706 #define LDEV_SI_REF 0x0002
709 linux_get_fop(struct linux_file *filp, const struct file_operations **fop,
710 struct linux_cdev **dev)
712 struct linux_cdev *ldev;
718 for (siref = ldev->siref;;) {
719 if ((siref & LDEV_SI_DTR) != 0) {
723 MPASS((ldev->siref & LDEV_SI_DTR) == 0);
724 } else if (atomic_fcmpset_int(&ldev->siref, &siref,
725 siref + LDEV_SI_REF)) {
734 linux_drop_fop(struct linux_cdev *ldev)
739 MPASS((ldev->siref & ~LDEV_SI_DTR) != 0);
740 atomic_subtract_int(&ldev->siref, LDEV_SI_REF);
743 #define OPW(fp,td,code) ({ \
744 struct file *__fpop; \
745 __typeof(code) __retval; \
747 __fpop = (td)->td_fpop; \
748 (td)->td_fpop = (fp); \
750 (td)->td_fpop = __fpop; \
755 linux_dev_fdopen(struct cdev *dev, int fflags, struct thread *td,
758 struct linux_cdev *ldev;
759 struct linux_file *filp;
760 const struct file_operations *fop;
765 filp = linux_file_alloc();
766 filp->f_dentry = &filp->f_dentry_store;
767 filp->f_op = ldev->ops;
768 filp->f_mode = file->f_flag;
769 filp->f_flags = file->f_flag;
770 filp->f_vnode = file->f_vnode;
772 refcount_acquire(&ldev->refs);
775 linux_set_current(td);
776 linux_get_fop(filp, &fop, &ldev);
778 if (fop->open != NULL) {
779 error = -fop->open(file->f_vnode, filp);
781 linux_drop_fop(ldev);
782 linux_cdev_deref(filp->f_cdev);
788 /* hold on to the vnode - used for fstat() */
789 vhold(filp->f_vnode);
791 /* release the file from devfs */
792 finit(file, filp->f_mode, DTYPE_DEV, filp, &linuxfileops);
793 linux_drop_fop(ldev);
797 #define LINUX_IOCTL_MIN_PTR 0x10000UL
798 #define LINUX_IOCTL_MAX_PTR (LINUX_IOCTL_MIN_PTR + IOCPARM_MAX)
801 linux_remap_address(void **uaddr, size_t len)
803 uintptr_t uaddr_val = (uintptr_t)(*uaddr);
805 if (unlikely(uaddr_val >= LINUX_IOCTL_MIN_PTR &&
806 uaddr_val < LINUX_IOCTL_MAX_PTR)) {
807 struct task_struct *pts = current;
813 /* compute data offset */
814 uaddr_val -= LINUX_IOCTL_MIN_PTR;
816 /* check that length is within bounds */
817 if ((len > IOCPARM_MAX) ||
818 (uaddr_val + len) > pts->bsd_ioctl_len) {
823 /* re-add kernel buffer address */
824 uaddr_val += (uintptr_t)pts->bsd_ioctl_data;
826 /* update address location */
827 *uaddr = (void *)uaddr_val;
834 linux_copyin(const void *uaddr, void *kaddr, size_t len)
836 if (linux_remap_address(__DECONST(void **, &uaddr), len)) {
839 memcpy(kaddr, uaddr, len);
842 return (-copyin(uaddr, kaddr, len));
846 linux_copyout(const void *kaddr, void *uaddr, size_t len)
848 if (linux_remap_address(&uaddr, len)) {
851 memcpy(uaddr, kaddr, len);
854 return (-copyout(kaddr, uaddr, len));
858 linux_clear_user(void *_uaddr, size_t _len)
860 uint8_t *uaddr = _uaddr;
863 /* make sure uaddr is aligned before going into the fast loop */
864 while (((uintptr_t)uaddr & 7) != 0 && len > 7) {
865 if (subyte(uaddr, 0))
871 /* zero 8 bytes at a time */
874 if (suword64(uaddr, 0))
877 if (suword32(uaddr, 0))
879 if (suword32(uaddr + 4, 0))
886 /* zero fill end, if any */
888 if (subyte(uaddr, 0))
897 linux_access_ok(const void *uaddr, size_t len)
902 /* get start and end address */
903 saddr = (uintptr_t)uaddr;
904 eaddr = (uintptr_t)uaddr + len;
906 /* verify addresses are valid for userspace */
907 return ((saddr == eaddr) ||
908 (eaddr > saddr && eaddr <= VM_MAXUSER_ADDRESS));
912 * This function should return either EINTR or ERESTART depending on
913 * the signal type sent to this thread:
916 linux_get_error(struct task_struct *task, int error)
918 /* check for signal type interrupt code */
919 if (error == EINTR || error == ERESTARTSYS || error == ERESTART) {
920 error = -linux_schedule_get_interrupt_value(task);
928 linux_file_ioctl_sub(struct file *fp, struct linux_file *filp,
929 const struct file_operations *fop, u_long cmd, caddr_t data,
932 struct task_struct *task = current;
936 size = IOCPARM_LEN(cmd);
937 /* refer to logic in sys_ioctl() */
940 * Setup hint for linux_copyin() and linux_copyout().
942 * Background: Linux code expects a user-space address
943 * while FreeBSD supplies a kernel-space address.
945 task->bsd_ioctl_data = data;
946 task->bsd_ioctl_len = size;
947 data = (void *)LINUX_IOCTL_MIN_PTR;
949 /* fetch user-space pointer */
950 data = *(void **)data;
952 #if defined(__amd64__)
953 if (td->td_proc->p_elf_machine == EM_386) {
954 /* try the compat IOCTL handler first */
955 if (fop->compat_ioctl != NULL) {
956 error = -OPW(fp, td, fop->compat_ioctl(filp,
962 /* fallback to the regular IOCTL handler, if any */
963 if (error == ENOTTY && fop->unlocked_ioctl != NULL) {
964 error = -OPW(fp, td, fop->unlocked_ioctl(filp,
970 if (fop->unlocked_ioctl != NULL) {
971 error = -OPW(fp, td, fop->unlocked_ioctl(filp,
978 task->bsd_ioctl_data = NULL;
979 task->bsd_ioctl_len = 0;
982 if (error == EWOULDBLOCK) {
983 /* update kqfilter status, if any */
984 linux_file_kqfilter_poll(filp,
985 LINUX_KQ_FLAG_HAS_READ | LINUX_KQ_FLAG_HAS_WRITE);
987 error = linux_get_error(task, error);
992 #define LINUX_POLL_TABLE_NORMAL ((poll_table *)1)
995 * This function atomically updates the poll wakeup state and returns
996 * the previous state at the time of update.
999 linux_poll_wakeup_state(atomic_t *v, const uint8_t *pstate)
1005 while ((old = atomic_cmpxchg(v, c, pstate[c])) != c)
1013 linux_poll_wakeup_callback(wait_queue_t *wq, unsigned int wq_state, int flags, void *key)
1015 static const uint8_t state[LINUX_FWQ_STATE_MAX] = {
1016 [LINUX_FWQ_STATE_INIT] = LINUX_FWQ_STATE_INIT, /* NOP */
1017 [LINUX_FWQ_STATE_NOT_READY] = LINUX_FWQ_STATE_NOT_READY, /* NOP */
1018 [LINUX_FWQ_STATE_QUEUED] = LINUX_FWQ_STATE_READY,
1019 [LINUX_FWQ_STATE_READY] = LINUX_FWQ_STATE_READY, /* NOP */
1021 struct linux_file *filp = container_of(wq, struct linux_file, f_wait_queue.wq);
1023 switch (linux_poll_wakeup_state(&filp->f_wait_queue.state, state)) {
1024 case LINUX_FWQ_STATE_QUEUED:
1025 linux_poll_wakeup(filp);
1033 linux_poll_wait(struct linux_file *filp, wait_queue_head_t *wqh, poll_table *p)
1035 static const uint8_t state[LINUX_FWQ_STATE_MAX] = {
1036 [LINUX_FWQ_STATE_INIT] = LINUX_FWQ_STATE_NOT_READY,
1037 [LINUX_FWQ_STATE_NOT_READY] = LINUX_FWQ_STATE_NOT_READY, /* NOP */
1038 [LINUX_FWQ_STATE_QUEUED] = LINUX_FWQ_STATE_QUEUED, /* NOP */
1039 [LINUX_FWQ_STATE_READY] = LINUX_FWQ_STATE_QUEUED,
1042 /* check if we are called inside the select system call */
1043 if (p == LINUX_POLL_TABLE_NORMAL)
1044 selrecord(curthread, &filp->f_selinfo);
1046 switch (linux_poll_wakeup_state(&filp->f_wait_queue.state, state)) {
1047 case LINUX_FWQ_STATE_INIT:
1048 /* NOTE: file handles can only belong to one wait-queue */
1049 filp->f_wait_queue.wqh = wqh;
1050 filp->f_wait_queue.wq.func = &linux_poll_wakeup_callback;
1051 add_wait_queue(wqh, &filp->f_wait_queue.wq);
1052 atomic_set(&filp->f_wait_queue.state, LINUX_FWQ_STATE_QUEUED);
1060 linux_poll_wait_dequeue(struct linux_file *filp)
1062 static const uint8_t state[LINUX_FWQ_STATE_MAX] = {
1063 [LINUX_FWQ_STATE_INIT] = LINUX_FWQ_STATE_INIT, /* NOP */
1064 [LINUX_FWQ_STATE_NOT_READY] = LINUX_FWQ_STATE_INIT,
1065 [LINUX_FWQ_STATE_QUEUED] = LINUX_FWQ_STATE_INIT,
1066 [LINUX_FWQ_STATE_READY] = LINUX_FWQ_STATE_INIT,
1069 seldrain(&filp->f_selinfo);
1071 switch (linux_poll_wakeup_state(&filp->f_wait_queue.state, state)) {
1072 case LINUX_FWQ_STATE_NOT_READY:
1073 case LINUX_FWQ_STATE_QUEUED:
1074 case LINUX_FWQ_STATE_READY:
1075 remove_wait_queue(filp->f_wait_queue.wqh, &filp->f_wait_queue.wq);
1083 linux_poll_wakeup(struct linux_file *filp)
1085 /* this function should be NULL-safe */
1089 selwakeup(&filp->f_selinfo);
1091 spin_lock(&filp->f_kqlock);
1092 filp->f_kqflags |= LINUX_KQ_FLAG_NEED_READ |
1093 LINUX_KQ_FLAG_NEED_WRITE;
1095 /* make sure the "knote" gets woken up */
1096 KNOTE_LOCKED(&filp->f_selinfo.si_note, 1);
1097 spin_unlock(&filp->f_kqlock);
1101 linux_file_kqfilter_detach(struct knote *kn)
1103 struct linux_file *filp = kn->kn_hook;
1105 spin_lock(&filp->f_kqlock);
1106 knlist_remove(&filp->f_selinfo.si_note, kn, 1);
1107 spin_unlock(&filp->f_kqlock);
1111 linux_file_kqfilter_read_event(struct knote *kn, long hint)
1113 struct linux_file *filp = kn->kn_hook;
1115 mtx_assert(&filp->f_kqlock.m, MA_OWNED);
1117 return ((filp->f_kqflags & LINUX_KQ_FLAG_NEED_READ) ? 1 : 0);
1121 linux_file_kqfilter_write_event(struct knote *kn, long hint)
1123 struct linux_file *filp = kn->kn_hook;
1125 mtx_assert(&filp->f_kqlock.m, MA_OWNED);
1127 return ((filp->f_kqflags & LINUX_KQ_FLAG_NEED_WRITE) ? 1 : 0);
1130 static struct filterops linux_dev_kqfiltops_read = {
1132 .f_detach = linux_file_kqfilter_detach,
1133 .f_event = linux_file_kqfilter_read_event,
1136 static struct filterops linux_dev_kqfiltops_write = {
1138 .f_detach = linux_file_kqfilter_detach,
1139 .f_event = linux_file_kqfilter_write_event,
1143 linux_file_kqfilter_poll(struct linux_file *filp, int kqflags)
1146 const struct file_operations *fop;
1147 struct linux_cdev *ldev;
1150 if ((filp->f_kqflags & kqflags) == 0)
1155 linux_get_fop(filp, &fop, &ldev);
1156 /* get the latest polling state */
1157 temp = OPW(filp->_file, td, fop->poll(filp, NULL));
1158 linux_drop_fop(ldev);
1160 spin_lock(&filp->f_kqlock);
1162 filp->f_kqflags &= ~(LINUX_KQ_FLAG_NEED_READ |
1163 LINUX_KQ_FLAG_NEED_WRITE);
1164 /* update kqflags */
1165 if ((temp & (POLLIN | POLLOUT)) != 0) {
1166 if ((temp & POLLIN) != 0)
1167 filp->f_kqflags |= LINUX_KQ_FLAG_NEED_READ;
1168 if ((temp & POLLOUT) != 0)
1169 filp->f_kqflags |= LINUX_KQ_FLAG_NEED_WRITE;
1171 /* make sure the "knote" gets woken up */
1172 KNOTE_LOCKED(&filp->f_selinfo.si_note, 0);
1174 spin_unlock(&filp->f_kqlock);
1178 linux_file_kqfilter(struct file *file, struct knote *kn)
1180 struct linux_file *filp;
1185 filp = (struct linux_file *)file->f_data;
1186 filp->f_flags = file->f_flag;
1187 if (filp->f_op->poll == NULL)
1190 spin_lock(&filp->f_kqlock);
1191 switch (kn->kn_filter) {
1193 filp->f_kqflags |= LINUX_KQ_FLAG_HAS_READ;
1194 kn->kn_fop = &linux_dev_kqfiltops_read;
1196 knlist_add(&filp->f_selinfo.si_note, kn, 1);
1200 filp->f_kqflags |= LINUX_KQ_FLAG_HAS_WRITE;
1201 kn->kn_fop = &linux_dev_kqfiltops_write;
1203 knlist_add(&filp->f_selinfo.si_note, kn, 1);
1210 spin_unlock(&filp->f_kqlock);
1213 linux_set_current(td);
1215 /* update kqfilter status, if any */
1216 linux_file_kqfilter_poll(filp,
1217 LINUX_KQ_FLAG_HAS_READ | LINUX_KQ_FLAG_HAS_WRITE);
1223 linux_file_mmap_single(struct file *fp, const struct file_operations *fop,
1224 vm_ooffset_t *offset, vm_size_t size, struct vm_object **object,
1225 int nprot, struct thread *td)
1227 struct task_struct *task;
1228 struct vm_area_struct *vmap;
1229 struct mm_struct *mm;
1230 struct linux_file *filp;
1234 filp = (struct linux_file *)fp->f_data;
1235 filp->f_flags = fp->f_flag;
1237 if (fop->mmap == NULL)
1238 return (EOPNOTSUPP);
1240 linux_set_current(td);
1243 * The same VM object might be shared by multiple processes
1244 * and the mm_struct is usually freed when a process exits.
1246 * The atomic reference below makes sure the mm_struct is
1247 * available as long as the vmap is in the linux_vma_head.
1251 if (atomic_inc_not_zero(&mm->mm_users) == 0)
1254 vmap = kzalloc(sizeof(*vmap), GFP_KERNEL);
1256 vmap->vm_end = size;
1257 vmap->vm_pgoff = *offset / PAGE_SIZE;
1259 vmap->vm_flags = vmap->vm_page_prot = (nprot & VM_PROT_ALL);
1260 vmap->vm_ops = NULL;
1261 vmap->vm_file = get_file(filp);
1264 if (unlikely(down_write_killable(&vmap->vm_mm->mmap_sem))) {
1265 error = linux_get_error(task, EINTR);
1267 error = -OPW(fp, td, fop->mmap(filp, vmap));
1268 error = linux_get_error(task, error);
1269 up_write(&vmap->vm_mm->mmap_sem);
1273 linux_cdev_handle_free(vmap);
1277 attr = pgprot2cachemode(vmap->vm_page_prot);
1279 if (vmap->vm_ops != NULL) {
1280 struct vm_area_struct *ptr;
1281 void *vm_private_data;
1284 if (vmap->vm_ops->open == NULL ||
1285 vmap->vm_ops->close == NULL ||
1286 vmap->vm_private_data == NULL) {
1287 /* free allocated VM area struct */
1288 linux_cdev_handle_free(vmap);
1292 vm_private_data = vmap->vm_private_data;
1294 rw_wlock(&linux_vma_lock);
1295 TAILQ_FOREACH(ptr, &linux_vma_head, vm_entry) {
1296 if (ptr->vm_private_data == vm_private_data)
1299 /* check if there is an existing VM area struct */
1301 /* check if the VM area structure is invalid */
1302 if (ptr->vm_ops == NULL ||
1303 ptr->vm_ops->open == NULL ||
1304 ptr->vm_ops->close == NULL) {
1309 vm_no_fault = (ptr->vm_ops->fault == NULL);
1312 /* insert VM area structure into list */
1313 TAILQ_INSERT_TAIL(&linux_vma_head, vmap, vm_entry);
1315 vm_no_fault = (vmap->vm_ops->fault == NULL);
1317 rw_wunlock(&linux_vma_lock);
1320 /* free allocated VM area struct */
1321 linux_cdev_handle_free(vmap);
1322 /* check for stale VM area struct */
1323 if (error != EEXIST)
1327 /* check if there is no fault handler */
1329 *object = cdev_pager_allocate(vm_private_data, OBJT_DEVICE,
1330 &linux_cdev_pager_ops[1], size, nprot, *offset,
1333 *object = cdev_pager_allocate(vm_private_data, OBJT_MGTDEVICE,
1334 &linux_cdev_pager_ops[0], size, nprot, *offset,
1338 /* check if allocating the VM object failed */
1339 if (*object == NULL) {
1341 /* remove VM area struct from list */
1342 linux_cdev_handle_remove(vmap);
1343 /* free allocated VM area struct */
1344 linux_cdev_handle_free(vmap);
1351 sg = sglist_alloc(1, M_WAITOK);
1352 sglist_append_phys(sg,
1353 (vm_paddr_t)vmap->vm_pfn << PAGE_SHIFT, vmap->vm_len);
1355 *object = vm_pager_allocate(OBJT_SG, sg, vmap->vm_len,
1356 nprot, 0, td->td_ucred);
1358 linux_cdev_handle_free(vmap);
1360 if (*object == NULL) {
1366 if (attr != VM_MEMATTR_DEFAULT) {
1367 VM_OBJECT_WLOCK(*object);
1368 vm_object_set_memattr(*object, attr);
1369 VM_OBJECT_WUNLOCK(*object);
1375 struct cdevsw linuxcdevsw = {
1376 .d_version = D_VERSION,
1377 .d_fdopen = linux_dev_fdopen,
1378 .d_name = "lkpidev",
1382 linux_file_read(struct file *file, struct uio *uio, struct ucred *active_cred,
1383 int flags, struct thread *td)
1385 struct linux_file *filp;
1386 const struct file_operations *fop;
1387 struct linux_cdev *ldev;
1392 filp = (struct linux_file *)file->f_data;
1393 filp->f_flags = file->f_flag;
1394 /* XXX no support for I/O vectors currently */
1395 if (uio->uio_iovcnt != 1)
1396 return (EOPNOTSUPP);
1397 if (uio->uio_resid > DEVFS_IOSIZE_MAX)
1399 linux_set_current(td);
1400 linux_get_fop(filp, &fop, &ldev);
1401 if (fop->read != NULL) {
1402 bytes = OPW(file, td, fop->read(filp,
1403 uio->uio_iov->iov_base,
1404 uio->uio_iov->iov_len, &uio->uio_offset));
1406 uio->uio_iov->iov_base =
1407 ((uint8_t *)uio->uio_iov->iov_base) + bytes;
1408 uio->uio_iov->iov_len -= bytes;
1409 uio->uio_resid -= bytes;
1411 error = linux_get_error(current, -bytes);
1416 /* update kqfilter status, if any */
1417 linux_file_kqfilter_poll(filp, LINUX_KQ_FLAG_HAS_READ);
1418 linux_drop_fop(ldev);
1424 linux_file_write(struct file *file, struct uio *uio, struct ucred *active_cred,
1425 int flags, struct thread *td)
1427 struct linux_file *filp;
1428 const struct file_operations *fop;
1429 struct linux_cdev *ldev;
1433 filp = (struct linux_file *)file->f_data;
1434 filp->f_flags = file->f_flag;
1435 /* XXX no support for I/O vectors currently */
1436 if (uio->uio_iovcnt != 1)
1437 return (EOPNOTSUPP);
1438 if (uio->uio_resid > DEVFS_IOSIZE_MAX)
1440 linux_set_current(td);
1441 linux_get_fop(filp, &fop, &ldev);
1442 if (fop->write != NULL) {
1443 bytes = OPW(file, td, fop->write(filp,
1444 uio->uio_iov->iov_base,
1445 uio->uio_iov->iov_len, &uio->uio_offset));
1447 uio->uio_iov->iov_base =
1448 ((uint8_t *)uio->uio_iov->iov_base) + bytes;
1449 uio->uio_iov->iov_len -= bytes;
1450 uio->uio_resid -= bytes;
1453 error = linux_get_error(current, -bytes);
1458 /* update kqfilter status, if any */
1459 linux_file_kqfilter_poll(filp, LINUX_KQ_FLAG_HAS_WRITE);
1461 linux_drop_fop(ldev);
1467 linux_file_poll(struct file *file, int events, struct ucred *active_cred,
1470 struct linux_file *filp;
1471 const struct file_operations *fop;
1472 struct linux_cdev *ldev;
1475 filp = (struct linux_file *)file->f_data;
1476 filp->f_flags = file->f_flag;
1477 linux_set_current(td);
1478 linux_get_fop(filp, &fop, &ldev);
1479 if (fop->poll != NULL) {
1480 revents = OPW(file, td, fop->poll(filp,
1481 LINUX_POLL_TABLE_NORMAL)) & events;
1485 linux_drop_fop(ldev);
1490 linux_file_close(struct file *file, struct thread *td)
1492 struct linux_file *filp;
1493 int (*release)(struct inode *, struct linux_file *);
1494 const struct file_operations *fop;
1495 struct linux_cdev *ldev;
1498 filp = (struct linux_file *)file->f_data;
1500 KASSERT(file_count(filp) == 0,
1501 ("File refcount(%d) is not zero", file_count(filp)));
1507 filp->f_flags = file->f_flag;
1508 linux_set_current(td);
1509 linux_poll_wait_dequeue(filp);
1510 linux_get_fop(filp, &fop, &ldev);
1512 * Always use the real release function, if any, to avoid
1513 * leaking device resources:
1515 release = filp->f_op->release;
1516 if (release != NULL)
1517 error = -OPW(file, td, release(filp->f_vnode, filp));
1518 funsetown(&filp->f_sigio);
1519 if (filp->f_vnode != NULL)
1520 vdrop(filp->f_vnode);
1521 linux_drop_fop(ldev);
1522 if (filp->f_cdev != NULL)
1523 linux_cdev_deref(filp->f_cdev);
1530 linux_file_ioctl(struct file *fp, u_long cmd, void *data, struct ucred *cred,
1533 struct linux_file *filp;
1534 const struct file_operations *fop;
1535 struct linux_cdev *ldev;
1536 struct fiodgname_arg *fgn;
1541 filp = (struct linux_file *)fp->f_data;
1542 filp->f_flags = fp->f_flag;
1543 linux_get_fop(filp, &fop, &ldev);
1545 linux_set_current(td);
1550 if (fop->fasync == NULL)
1552 error = -OPW(fp, td, fop->fasync(0, filp, fp->f_flag & FASYNC));
1555 error = fsetown(*(int *)data, &filp->f_sigio);
1557 if (fop->fasync == NULL)
1559 error = -OPW(fp, td, fop->fasync(0, filp,
1560 fp->f_flag & FASYNC));
1564 *(int *)data = fgetown(&filp->f_sigio);
1567 #ifdef COMPAT_FREEBSD32
1570 if (filp->f_cdev == NULL || filp->f_cdev->cdev == NULL) {
1575 p = devtoname(filp->f_cdev->cdev);
1581 error = copyout(p, fiodgname_buf_get_ptr(fgn, cmd), i);
1584 error = linux_file_ioctl_sub(fp, filp, fop, cmd, data, td);
1587 linux_drop_fop(ldev);
1592 linux_file_mmap_sub(struct thread *td, vm_size_t objsize, vm_prot_t prot,
1593 vm_prot_t *maxprotp, int *flagsp, struct file *fp,
1594 vm_ooffset_t *foff, const struct file_operations *fop, vm_object_t *objp)
1597 * Character devices do not provide private mappings
1600 if ((*maxprotp & VM_PROT_WRITE) == 0 &&
1601 (prot & VM_PROT_WRITE) != 0)
1603 if ((*flagsp & (MAP_PRIVATE | MAP_COPY)) != 0)
1606 return (linux_file_mmap_single(fp, fop, foff, objsize, objp,
1611 linux_file_mmap(struct file *fp, vm_map_t map, vm_offset_t *addr, vm_size_t size,
1612 vm_prot_t prot, vm_prot_t cap_maxprot, int flags, vm_ooffset_t foff,
1615 struct linux_file *filp;
1616 const struct file_operations *fop;
1617 struct linux_cdev *ldev;
1624 filp = (struct linux_file *)fp->f_data;
1628 return (EOPNOTSUPP);
1631 * Ensure that file and memory protections are
1635 if (mp != NULL && (mp->mnt_flag & MNT_NOEXEC) != 0) {
1636 maxprot = VM_PROT_NONE;
1637 if ((prot & VM_PROT_EXECUTE) != 0)
1640 maxprot = VM_PROT_EXECUTE;
1641 if ((fp->f_flag & FREAD) != 0)
1642 maxprot |= VM_PROT_READ;
1643 else if ((prot & VM_PROT_READ) != 0)
1647 * If we are sharing potential changes via MAP_SHARED and we
1648 * are trying to get write permission although we opened it
1649 * without asking for it, bail out.
1651 * Note that most character devices always share mappings.
1653 * Rely on linux_file_mmap_sub() to fail invalid MAP_PRIVATE
1654 * requests rather than doing it here.
1656 if ((flags & MAP_SHARED) != 0) {
1657 if ((fp->f_flag & FWRITE) != 0)
1658 maxprot |= VM_PROT_WRITE;
1659 else if ((prot & VM_PROT_WRITE) != 0)
1662 maxprot &= cap_maxprot;
1664 linux_get_fop(filp, &fop, &ldev);
1665 error = linux_file_mmap_sub(td, size, prot, &maxprot, &flags, fp,
1666 &foff, fop, &object);
1670 error = vm_mmap_object(map, addr, size, prot, maxprot, flags, object,
1673 vm_object_deallocate(object);
1675 linux_drop_fop(ldev);
1680 linux_file_stat(struct file *fp, struct stat *sb, struct ucred *active_cred,
1683 struct linux_file *filp;
1687 filp = (struct linux_file *)fp->f_data;
1688 if (filp->f_vnode == NULL)
1689 return (EOPNOTSUPP);
1693 vn_lock(vp, LK_SHARED | LK_RETRY);
1694 error = vn_stat(vp, sb, td->td_ucred, NOCRED, td);
1701 linux_file_fill_kinfo(struct file *fp, struct kinfo_file *kif,
1702 struct filedesc *fdp)
1704 struct linux_file *filp;
1712 kif->kf_type = KF_TYPE_DEV;
1715 FILEDESC_SUNLOCK(fdp);
1716 error = vn_fill_kinfo_vnode(vp, kif);
1718 kif->kf_type = KF_TYPE_VNODE;
1719 FILEDESC_SLOCK(fdp);
1725 linux_iminor(struct inode *inode)
1727 struct linux_cdev *ldev;
1729 if (inode == NULL || inode->v_rdev == NULL ||
1730 inode->v_rdev->si_devsw != &linuxcdevsw)
1732 ldev = inode->v_rdev->si_drv1;
1736 return (minor(ldev->dev));
1739 struct fileops linuxfileops = {
1740 .fo_read = linux_file_read,
1741 .fo_write = linux_file_write,
1742 .fo_truncate = invfo_truncate,
1743 .fo_kqfilter = linux_file_kqfilter,
1744 .fo_stat = linux_file_stat,
1745 .fo_fill_kinfo = linux_file_fill_kinfo,
1746 .fo_poll = linux_file_poll,
1747 .fo_close = linux_file_close,
1748 .fo_ioctl = linux_file_ioctl,
1749 .fo_mmap = linux_file_mmap,
1750 .fo_chmod = invfo_chmod,
1751 .fo_chown = invfo_chown,
1752 .fo_sendfile = invfo_sendfile,
1753 .fo_flags = DFLAG_PASSABLE,
1757 * Hash of vmmap addresses. This is infrequently accessed and does not
1758 * need to be particularly large. This is done because we must store the
1759 * caller's idea of the map size to properly unmap.
1762 LIST_ENTRY(vmmap) vm_next;
1764 unsigned long vm_size;
1768 struct vmmap *lh_first;
1770 #define VMMAP_HASH_SIZE 64
1771 #define VMMAP_HASH_MASK (VMMAP_HASH_SIZE - 1)
1772 #define VM_HASH(addr) ((uintptr_t)(addr) >> PAGE_SHIFT) & VMMAP_HASH_MASK
1773 static struct vmmaphd vmmaphead[VMMAP_HASH_SIZE];
1774 static struct mtx vmmaplock;
1777 vmmap_add(void *addr, unsigned long size)
1779 struct vmmap *vmmap;
1781 vmmap = kmalloc(sizeof(*vmmap), GFP_KERNEL);
1782 mtx_lock(&vmmaplock);
1783 vmmap->vm_size = size;
1784 vmmap->vm_addr = addr;
1785 LIST_INSERT_HEAD(&vmmaphead[VM_HASH(addr)], vmmap, vm_next);
1786 mtx_unlock(&vmmaplock);
1789 static struct vmmap *
1790 vmmap_remove(void *addr)
1792 struct vmmap *vmmap;
1794 mtx_lock(&vmmaplock);
1795 LIST_FOREACH(vmmap, &vmmaphead[VM_HASH(addr)], vm_next)
1796 if (vmmap->vm_addr == addr)
1799 LIST_REMOVE(vmmap, vm_next);
1800 mtx_unlock(&vmmaplock);
1805 #if defined(__i386__) || defined(__amd64__) || defined(__powerpc__) || defined(__aarch64__)
1807 _ioremap_attr(vm_paddr_t phys_addr, unsigned long size, int attr)
1811 addr = pmap_mapdev_attr(phys_addr, size, attr);
1814 vmmap_add(addr, size);
1823 struct vmmap *vmmap;
1825 vmmap = vmmap_remove(addr);
1828 #if defined(__i386__) || defined(__amd64__) || defined(__powerpc__) || defined(__aarch64__)
1829 pmap_unmapdev((vm_offset_t)addr, vmmap->vm_size);
1836 vmap(struct page **pages, unsigned int count, unsigned long flags, int prot)
1841 size = count * PAGE_SIZE;
1842 off = kva_alloc(size);
1845 vmmap_add((void *)off, size);
1846 pmap_qenter(off, pages, count);
1848 return ((void *)off);
1854 struct vmmap *vmmap;
1856 vmmap = vmmap_remove(addr);
1859 pmap_qremove((vm_offset_t)addr, vmmap->vm_size / PAGE_SIZE);
1860 kva_free((vm_offset_t)addr, vmmap->vm_size);
1865 kvasprintf(gfp_t gfp, const char *fmt, va_list ap)
1872 len = vsnprintf(NULL, 0, fmt, aq);
1875 p = kmalloc(len + 1, gfp);
1877 vsnprintf(p, len + 1, fmt, ap);
1883 kasprintf(gfp_t gfp, const char *fmt, ...)
1889 p = kvasprintf(gfp, fmt, ap);
1896 linux_timer_callback_wrapper(void *context)
1898 struct timer_list *timer;
1900 linux_set_current(curthread);
1903 timer->function(timer->data);
1907 mod_timer(struct timer_list *timer, int expires)
1911 timer->expires = expires;
1912 ret = callout_reset(&timer->callout,
1913 linux_timer_jiffies_until(expires),
1914 &linux_timer_callback_wrapper, timer);
1916 MPASS(ret == 0 || ret == 1);
1922 add_timer(struct timer_list *timer)
1925 callout_reset(&timer->callout,
1926 linux_timer_jiffies_until(timer->expires),
1927 &linux_timer_callback_wrapper, timer);
1931 add_timer_on(struct timer_list *timer, int cpu)
1934 callout_reset_on(&timer->callout,
1935 linux_timer_jiffies_until(timer->expires),
1936 &linux_timer_callback_wrapper, timer, cpu);
1940 del_timer(struct timer_list *timer)
1943 if (callout_stop(&(timer)->callout) == -1)
1949 del_timer_sync(struct timer_list *timer)
1952 if (callout_drain(&(timer)->callout) == -1)
1957 /* greatest common divisor, Euclid equation */
1959 lkpi_gcd_64(uint64_t a, uint64_t b)
1973 uint64_t lkpi_nsec2hz_rem;
1974 uint64_t lkpi_nsec2hz_div = 1000000000ULL;
1975 uint64_t lkpi_nsec2hz_max;
1977 uint64_t lkpi_usec2hz_rem;
1978 uint64_t lkpi_usec2hz_div = 1000000ULL;
1979 uint64_t lkpi_usec2hz_max;
1981 uint64_t lkpi_msec2hz_rem;
1982 uint64_t lkpi_msec2hz_div = 1000ULL;
1983 uint64_t lkpi_msec2hz_max;
1986 linux_timer_init(void *arg)
1991 * Compute an internal HZ value which can divide 2**32 to
1992 * avoid timer rounding problems when the tick value wraps
1995 linux_timer_hz_mask = 1;
1996 while (linux_timer_hz_mask < (unsigned long)hz)
1997 linux_timer_hz_mask *= 2;
1998 linux_timer_hz_mask--;
2000 /* compute some internal constants */
2002 lkpi_nsec2hz_rem = hz;
2003 lkpi_usec2hz_rem = hz;
2004 lkpi_msec2hz_rem = hz;
2006 gcd = lkpi_gcd_64(lkpi_nsec2hz_rem, lkpi_nsec2hz_div);
2007 lkpi_nsec2hz_rem /= gcd;
2008 lkpi_nsec2hz_div /= gcd;
2009 lkpi_nsec2hz_max = -1ULL / lkpi_nsec2hz_rem;
2011 gcd = lkpi_gcd_64(lkpi_usec2hz_rem, lkpi_usec2hz_div);
2012 lkpi_usec2hz_rem /= gcd;
2013 lkpi_usec2hz_div /= gcd;
2014 lkpi_usec2hz_max = -1ULL / lkpi_usec2hz_rem;
2016 gcd = lkpi_gcd_64(lkpi_msec2hz_rem, lkpi_msec2hz_div);
2017 lkpi_msec2hz_rem /= gcd;
2018 lkpi_msec2hz_div /= gcd;
2019 lkpi_msec2hz_max = -1ULL / lkpi_msec2hz_rem;
2021 SYSINIT(linux_timer, SI_SUB_DRIVERS, SI_ORDER_FIRST, linux_timer_init, NULL);
2024 linux_complete_common(struct completion *c, int all)
2031 wakeup_swapper = sleepq_broadcast(c, SLEEPQ_SLEEP, 0, 0);
2033 if (c->done != UINT_MAX)
2035 wakeup_swapper = sleepq_signal(c, SLEEPQ_SLEEP, 0, 0);
2043 * Indefinite wait for done != 0 with or without signals.
2046 linux_wait_for_common(struct completion *c, int flags)
2048 struct task_struct *task;
2051 if (SCHEDULER_STOPPED())
2057 flags = SLEEPQ_INTERRUPTIBLE | SLEEPQ_SLEEP;
2059 flags = SLEEPQ_SLEEP;
2065 sleepq_add(c, NULL, "completion", flags, 0);
2066 if (flags & SLEEPQ_INTERRUPTIBLE) {
2068 error = -sleepq_wait_sig(c, 0);
2071 linux_schedule_save_interrupt_value(task, error);
2072 error = -ERESTARTSYS;
2081 if (c->done != UINT_MAX)
2090 * Time limited wait for done != 0 with or without signals.
2093 linux_wait_for_timeout_common(struct completion *c, int timeout, int flags)
2095 struct task_struct *task;
2096 int end = jiffies + timeout;
2099 if (SCHEDULER_STOPPED())
2105 flags = SLEEPQ_INTERRUPTIBLE | SLEEPQ_SLEEP;
2107 flags = SLEEPQ_SLEEP;
2113 sleepq_add(c, NULL, "completion", flags, 0);
2114 sleepq_set_timeout(c, linux_timer_jiffies_until(end));
2117 if (flags & SLEEPQ_INTERRUPTIBLE)
2118 error = -sleepq_timedwait_sig(c, 0);
2120 error = -sleepq_timedwait(c, 0);
2124 /* check for timeout */
2125 if (error == -EWOULDBLOCK) {
2126 error = 0; /* timeout */
2128 /* signal happened */
2129 linux_schedule_save_interrupt_value(task, error);
2130 error = -ERESTARTSYS;
2135 if (c->done != UINT_MAX)
2139 /* return how many jiffies are left */
2140 error = linux_timer_jiffies_until(end);
2146 linux_try_wait_for_completion(struct completion *c)
2151 isdone = (c->done != 0);
2152 if (c->done != 0 && c->done != UINT_MAX)
2159 linux_completion_done(struct completion *c)
2164 isdone = (c->done != 0);
2170 linux_cdev_deref(struct linux_cdev *ldev)
2173 if (refcount_release(&ldev->refs))
2178 linux_cdev_release(struct kobject *kobj)
2180 struct linux_cdev *cdev;
2181 struct kobject *parent;
2183 cdev = container_of(kobj, struct linux_cdev, kobj);
2184 parent = kobj->parent;
2185 linux_destroy_dev(cdev);
2186 linux_cdev_deref(cdev);
2187 kobject_put(parent);
2191 linux_cdev_static_release(struct kobject *kobj)
2193 struct linux_cdev *cdev;
2194 struct kobject *parent;
2196 cdev = container_of(kobj, struct linux_cdev, kobj);
2197 parent = kobj->parent;
2198 linux_destroy_dev(cdev);
2199 kobject_put(parent);
2203 linux_destroy_dev(struct linux_cdev *ldev)
2206 if (ldev->cdev == NULL)
2209 MPASS((ldev->siref & LDEV_SI_DTR) == 0);
2210 atomic_set_int(&ldev->siref, LDEV_SI_DTR);
2211 while ((atomic_load_int(&ldev->siref) & ~LDEV_SI_DTR) != 0)
2212 pause("ldevdtr", hz / 4);
2214 destroy_dev(ldev->cdev);
2218 const struct kobj_type linux_cdev_ktype = {
2219 .release = linux_cdev_release,
2222 const struct kobj_type linux_cdev_static_ktype = {
2223 .release = linux_cdev_static_release,
2227 linux_handle_ifnet_link_event(void *arg, struct ifnet *ifp, int linkstate)
2229 struct notifier_block *nb;
2232 if (linkstate == LINK_STATE_UP)
2233 nb->notifier_call(nb, NETDEV_UP, ifp);
2235 nb->notifier_call(nb, NETDEV_DOWN, ifp);
2239 linux_handle_ifnet_arrival_event(void *arg, struct ifnet *ifp)
2241 struct notifier_block *nb;
2244 nb->notifier_call(nb, NETDEV_REGISTER, ifp);
2248 linux_handle_ifnet_departure_event(void *arg, struct ifnet *ifp)
2250 struct notifier_block *nb;
2253 nb->notifier_call(nb, NETDEV_UNREGISTER, ifp);
2257 linux_handle_iflladdr_event(void *arg, struct ifnet *ifp)
2259 struct notifier_block *nb;
2262 nb->notifier_call(nb, NETDEV_CHANGEADDR, ifp);
2266 linux_handle_ifaddr_event(void *arg, struct ifnet *ifp)
2268 struct notifier_block *nb;
2271 nb->notifier_call(nb, NETDEV_CHANGEIFADDR, ifp);
2275 register_netdevice_notifier(struct notifier_block *nb)
2278 nb->tags[NETDEV_UP] = EVENTHANDLER_REGISTER(
2279 ifnet_link_event, linux_handle_ifnet_link_event, nb, 0);
2280 nb->tags[NETDEV_REGISTER] = EVENTHANDLER_REGISTER(
2281 ifnet_arrival_event, linux_handle_ifnet_arrival_event, nb, 0);
2282 nb->tags[NETDEV_UNREGISTER] = EVENTHANDLER_REGISTER(
2283 ifnet_departure_event, linux_handle_ifnet_departure_event, nb, 0);
2284 nb->tags[NETDEV_CHANGEADDR] = EVENTHANDLER_REGISTER(
2285 iflladdr_event, linux_handle_iflladdr_event, nb, 0);
2291 register_inetaddr_notifier(struct notifier_block *nb)
2294 nb->tags[NETDEV_CHANGEIFADDR] = EVENTHANDLER_REGISTER(
2295 ifaddr_event, linux_handle_ifaddr_event, nb, 0);
2300 unregister_netdevice_notifier(struct notifier_block *nb)
2303 EVENTHANDLER_DEREGISTER(ifnet_link_event,
2304 nb->tags[NETDEV_UP]);
2305 EVENTHANDLER_DEREGISTER(ifnet_arrival_event,
2306 nb->tags[NETDEV_REGISTER]);
2307 EVENTHANDLER_DEREGISTER(ifnet_departure_event,
2308 nb->tags[NETDEV_UNREGISTER]);
2309 EVENTHANDLER_DEREGISTER(iflladdr_event,
2310 nb->tags[NETDEV_CHANGEADDR]);
2316 unregister_inetaddr_notifier(struct notifier_block *nb)
2319 EVENTHANDLER_DEREGISTER(ifaddr_event,
2320 nb->tags[NETDEV_CHANGEIFADDR]);
2325 struct list_sort_thunk {
2326 int (*cmp)(void *, struct list_head *, struct list_head *);
2331 linux_le_cmp(void *priv, const void *d1, const void *d2)
2333 struct list_head *le1, *le2;
2334 struct list_sort_thunk *thunk;
2337 le1 = *(__DECONST(struct list_head **, d1));
2338 le2 = *(__DECONST(struct list_head **, d2));
2339 return ((thunk->cmp)(thunk->priv, le1, le2));
2343 list_sort(void *priv, struct list_head *head, int (*cmp)(void *priv,
2344 struct list_head *a, struct list_head *b))
2346 struct list_sort_thunk thunk;
2347 struct list_head **ar, *le;
2351 list_for_each(le, head)
2353 ar = malloc(sizeof(struct list_head *) * count, M_KMALLOC, M_WAITOK);
2355 list_for_each(le, head)
2359 qsort_r(ar, count, sizeof(struct list_head *), &thunk, linux_le_cmp);
2360 INIT_LIST_HEAD(head);
2361 for (i = 0; i < count; i++)
2362 list_add_tail(ar[i], head);
2363 free(ar, M_KMALLOC);
2367 linux_irq_handler(void *ent)
2369 struct irq_ent *irqe;
2371 linux_set_current(curthread);
2374 irqe->handler(irqe->irq, irqe->arg);
2377 #if defined(__i386__) || defined(__amd64__)
2379 linux_wbinvd_on_all_cpus(void)
2382 pmap_invalidate_cache();
2388 linux_on_each_cpu(void callback(void *), void *data)
2391 smp_rendezvous(smp_no_rendezvous_barrier, callback,
2392 smp_no_rendezvous_barrier, data);
2397 linux_in_atomic(void)
2400 return ((curthread->td_pflags & TDP_NOFAULTING) != 0);
2404 linux_find_cdev(const char *name, unsigned major, unsigned minor)
2406 dev_t dev = MKDEV(major, minor);
2410 LIST_FOREACH(cdev, &linuxcdevsw.d_devs, si_list) {
2411 struct linux_cdev *ldev = cdev->si_drv1;
2412 if (ldev->dev == dev &&
2413 strcmp(kobject_name(&ldev->kobj), name) == 0) {
2419 return (cdev != NULL ? cdev->si_drv1 : NULL);
2423 __register_chrdev(unsigned int major, unsigned int baseminor,
2424 unsigned int count, const char *name,
2425 const struct file_operations *fops)
2427 struct linux_cdev *cdev;
2431 for (i = baseminor; i < baseminor + count; i++) {
2432 cdev = cdev_alloc();
2434 kobject_set_name(&cdev->kobj, name);
2436 ret = cdev_add(cdev, makedev(major, i), 1);
2444 __register_chrdev_p(unsigned int major, unsigned int baseminor,
2445 unsigned int count, const char *name,
2446 const struct file_operations *fops, uid_t uid,
2447 gid_t gid, int mode)
2449 struct linux_cdev *cdev;
2453 for (i = baseminor; i < baseminor + count; i++) {
2454 cdev = cdev_alloc();
2456 kobject_set_name(&cdev->kobj, name);
2458 ret = cdev_add_ext(cdev, makedev(major, i), uid, gid, mode);
2466 __unregister_chrdev(unsigned int major, unsigned int baseminor,
2467 unsigned int count, const char *name)
2469 struct linux_cdev *cdevp;
2472 for (i = baseminor; i < baseminor + count; i++) {
2473 cdevp = linux_find_cdev(name, major, i);
2480 linux_dump_stack(void)
2491 #if defined(__i386__) || defined(__amd64__)
2492 bool linux_cpu_has_clflush;
2496 linux_compat_init(void *arg)
2498 struct sysctl_oid *rootoid;
2501 #if defined(__i386__) || defined(__amd64__)
2502 linux_cpu_has_clflush = (cpu_feature & CPUID_CLFSH);
2504 rw_init(&linux_vma_lock, "lkpi-vma-lock");
2506 rootoid = SYSCTL_ADD_ROOT_NODE(NULL,
2507 OID_AUTO, "sys", CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, "sys");
2508 kobject_init(&linux_class_root, &linux_class_ktype);
2509 kobject_set_name(&linux_class_root, "class");
2510 linux_class_root.oidp = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(rootoid),
2511 OID_AUTO, "class", CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, "class");
2512 kobject_init(&linux_root_device.kobj, &linux_dev_ktype);
2513 kobject_set_name(&linux_root_device.kobj, "device");
2514 linux_root_device.kobj.oidp = SYSCTL_ADD_NODE(NULL,
2515 SYSCTL_CHILDREN(rootoid), OID_AUTO, "device",
2516 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "device");
2517 linux_root_device.bsddev = root_bus;
2518 linux_class_misc.name = "misc";
2519 class_register(&linux_class_misc);
2520 INIT_LIST_HEAD(&pci_drivers);
2521 INIT_LIST_HEAD(&pci_devices);
2522 spin_lock_init(&pci_lock);
2523 mtx_init(&vmmaplock, "IO Map lock", NULL, MTX_DEF);
2524 for (i = 0; i < VMMAP_HASH_SIZE; i++)
2525 LIST_INIT(&vmmaphead[i]);
2527 SYSINIT(linux_compat, SI_SUB_DRIVERS, SI_ORDER_SECOND, linux_compat_init, NULL);
2530 linux_compat_uninit(void *arg)
2532 linux_kobject_kfree_name(&linux_class_root);
2533 linux_kobject_kfree_name(&linux_root_device.kobj);
2534 linux_kobject_kfree_name(&linux_class_misc.kobj);
2536 mtx_destroy(&vmmaplock);
2537 spin_lock_destroy(&pci_lock);
2538 rw_destroy(&linux_vma_lock);
2540 SYSUNINIT(linux_compat, SI_SUB_DRIVERS, SI_ORDER_SECOND, linux_compat_uninit, NULL);
2543 * NOTE: Linux frequently uses "unsigned long" for pointer to integer
2544 * conversion and vice versa, where in FreeBSD "uintptr_t" would be
2545 * used. Assert these types have the same size, else some parts of the
2546 * LinuxKPI may not work like expected:
2548 CTASSERT(sizeof(unsigned long) == sizeof(uintptr_t));