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>
89 #include <linux/wait_bit.h>
91 #if defined(__i386__) || defined(__amd64__)
95 SYSCTL_NODE(_compat, OID_AUTO, linuxkpi, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
96 "LinuxKPI parameters");
99 SYSCTL_INT(_compat_linuxkpi, OID_AUTO, debug, CTLFLAG_RWTUN,
100 &linuxkpi_debug, 0, "Set to enable pr_debug() prints. Clear to disable.");
102 MALLOC_DEFINE(M_KMALLOC, "linux", "Linux kmalloc compat");
104 #include <linux/rbtree.h>
105 /* Undo Linux compat changes. */
109 #define RB_ROOT(head) (head)->rbh_root
111 static void linux_cdev_deref(struct linux_cdev *ldev);
112 static struct vm_area_struct *linux_cdev_handle_find(void *handle);
114 struct kobject linux_class_root;
115 struct device linux_root_device;
116 struct class linux_class_misc;
117 struct list_head pci_drivers;
118 struct list_head pci_devices;
121 unsigned long linux_timer_hz_mask;
123 wait_queue_head_t linux_bit_waitq;
124 wait_queue_head_t linux_var_waitq;
127 panic_cmp(struct rb_node *one, struct rb_node *two)
132 RB_GENERATE(linux_root, rb_node, __entry, panic_cmp);
135 kobject_set_name_vargs(struct kobject *kobj, const char *fmt, va_list args)
145 if (old && fmt == NULL)
148 /* compute length of string */
149 va_copy(tmp_va, args);
150 len = vsnprintf(&dummy, 0, fmt, tmp_va);
153 /* account for zero termination */
156 /* check for error */
160 /* allocate memory for string */
161 name = kzalloc(len, GFP_KERNEL);
164 vsnprintf(name, len, fmt, args);
167 /* free old string */
170 /* filter new string */
171 for (; *name != '\0'; name++)
178 kobject_set_name(struct kobject *kobj, const char *fmt, ...)
184 error = kobject_set_name_vargs(kobj, fmt, args);
191 kobject_add_complete(struct kobject *kobj, struct kobject *parent)
193 const struct kobj_type *t;
196 kobj->parent = parent;
197 error = sysfs_create_dir(kobj);
198 if (error == 0 && kobj->ktype && kobj->ktype->default_attrs) {
199 struct attribute **attr;
202 for (attr = t->default_attrs; *attr != NULL; attr++) {
203 error = sysfs_create_file(kobj, *attr);
208 sysfs_remove_dir(kobj);
214 kobject_add(struct kobject *kobj, struct kobject *parent, const char *fmt, ...)
220 error = kobject_set_name_vargs(kobj, fmt, args);
225 return kobject_add_complete(kobj, parent);
229 linux_kobject_release(struct kref *kref)
231 struct kobject *kobj;
234 kobj = container_of(kref, struct kobject, kref);
235 sysfs_remove_dir(kobj);
237 if (kobj->ktype && kobj->ktype->release)
238 kobj->ktype->release(kobj);
243 linux_kobject_kfree(struct kobject *kobj)
249 linux_kobject_kfree_name(struct kobject *kobj)
256 const struct kobj_type linux_kfree_type = {
257 .release = linux_kobject_kfree
261 linux_device_release(struct device *dev)
263 pr_debug("linux_device_release: %s\n", dev_name(dev));
268 linux_class_show(struct kobject *kobj, struct attribute *attr, char *buf)
270 struct class_attribute *dattr;
273 dattr = container_of(attr, struct class_attribute, attr);
276 error = dattr->show(container_of(kobj, struct class, kobj),
282 linux_class_store(struct kobject *kobj, struct attribute *attr, const char *buf,
285 struct class_attribute *dattr;
288 dattr = container_of(attr, struct class_attribute, attr);
291 error = dattr->store(container_of(kobj, struct class, kobj),
297 linux_class_release(struct kobject *kobj)
301 class = container_of(kobj, struct class, kobj);
302 if (class->class_release)
303 class->class_release(class);
306 static const struct sysfs_ops linux_class_sysfs = {
307 .show = linux_class_show,
308 .store = linux_class_store,
311 const struct kobj_type linux_class_ktype = {
312 .release = linux_class_release,
313 .sysfs_ops = &linux_class_sysfs
317 linux_dev_release(struct kobject *kobj)
321 dev = container_of(kobj, struct device, kobj);
322 /* This is the precedence defined by linux. */
325 else if (dev->class && dev->class->dev_release)
326 dev->class->dev_release(dev);
330 linux_dev_show(struct kobject *kobj, struct attribute *attr, char *buf)
332 struct device_attribute *dattr;
335 dattr = container_of(attr, struct device_attribute, attr);
338 error = dattr->show(container_of(kobj, struct device, kobj),
344 linux_dev_store(struct kobject *kobj, struct attribute *attr, const char *buf,
347 struct device_attribute *dattr;
350 dattr = container_of(attr, struct device_attribute, attr);
353 error = dattr->store(container_of(kobj, struct device, kobj),
358 static const struct sysfs_ops linux_dev_sysfs = {
359 .show = linux_dev_show,
360 .store = linux_dev_store,
363 const struct kobj_type linux_dev_ktype = {
364 .release = linux_dev_release,
365 .sysfs_ops = &linux_dev_sysfs
369 device_create(struct class *class, struct device *parent, dev_t devt,
370 void *drvdata, const char *fmt, ...)
375 dev = kzalloc(sizeof(*dev), M_WAITOK);
376 dev->parent = parent;
379 dev->driver_data = drvdata;
380 dev->release = linux_device_release;
382 kobject_set_name_vargs(&dev->kobj, fmt, args);
384 device_register(dev);
390 kobject_init_and_add(struct kobject *kobj, const struct kobj_type *ktype,
391 struct kobject *parent, const char *fmt, ...)
396 kobject_init(kobj, ktype);
398 kobj->parent = parent;
402 error = kobject_set_name_vargs(kobj, fmt, args);
406 return kobject_add_complete(kobj, parent);
410 linux_kq_lock(void *arg)
417 linux_kq_unlock(void *arg)
425 linux_kq_assert_lock(void *arg, int what)
430 if (what == LA_LOCKED)
431 mtx_assert(&s->m, MA_OWNED);
433 mtx_assert(&s->m, MA_NOTOWNED);
438 linux_file_kqfilter_poll(struct linux_file *, int);
441 linux_file_alloc(void)
443 struct linux_file *filp;
445 filp = kzalloc(sizeof(*filp), GFP_KERNEL);
447 /* set initial refcount */
450 /* setup fields needed by kqueue support */
451 spin_lock_init(&filp->f_kqlock);
452 knlist_init(&filp->f_selinfo.si_note, &filp->f_kqlock,
453 linux_kq_lock, linux_kq_unlock, linux_kq_assert_lock);
459 linux_file_free(struct linux_file *filp)
461 if (filp->_file == NULL) {
462 if (filp->f_shmem != NULL)
463 vm_object_deallocate(filp->f_shmem);
467 * The close method of the character device or file
468 * will free the linux_file structure:
470 _fdrop(filp->_file, curthread);
475 linux_cdev_pager_fault(vm_object_t vm_obj, vm_ooffset_t offset, int prot,
478 struct vm_area_struct *vmap;
480 vmap = linux_cdev_handle_find(vm_obj->handle);
483 MPASS(vmap->vm_private_data == vm_obj->handle);
485 if (likely(vmap->vm_ops != NULL && offset < vmap->vm_len)) {
486 vm_paddr_t paddr = IDX_TO_OFF(vmap->vm_pfn) + offset;
489 if (((*mres)->flags & PG_FICTITIOUS) != 0) {
491 * If the passed in result page is a fake
492 * page, update it with the new physical
496 vm_page_updatefake(page, paddr, vm_obj->memattr);
499 * Replace the passed in "mres" page with our
500 * own fake page and free up the all of the
503 VM_OBJECT_WUNLOCK(vm_obj);
504 page = vm_page_getfake(paddr, vm_obj->memattr);
505 VM_OBJECT_WLOCK(vm_obj);
507 vm_page_replace(page, vm_obj, (*mres)->pindex, *mres);
511 return (VM_PAGER_OK);
513 return (VM_PAGER_FAIL);
517 linux_cdev_pager_populate(vm_object_t vm_obj, vm_pindex_t pidx, int fault_type,
518 vm_prot_t max_prot, vm_pindex_t *first, vm_pindex_t *last)
520 struct vm_area_struct *vmap;
523 /* get VM area structure */
524 vmap = linux_cdev_handle_find(vm_obj->handle);
526 MPASS(vmap->vm_private_data == vm_obj->handle);
528 VM_OBJECT_WUNLOCK(vm_obj);
530 linux_set_current(curthread);
532 down_write(&vmap->vm_mm->mmap_sem);
533 if (unlikely(vmap->vm_ops == NULL)) {
534 err = VM_FAULT_SIGBUS;
538 /* fill out VM fault structure */
539 vmf.virtual_address = (void *)(uintptr_t)IDX_TO_OFF(pidx);
540 vmf.flags = (fault_type & VM_PROT_WRITE) ? FAULT_FLAG_WRITE : 0;
545 vmap->vm_pfn_count = 0;
546 vmap->vm_pfn_pcount = &vmap->vm_pfn_count;
547 vmap->vm_obj = vm_obj;
549 err = vmap->vm_ops->fault(vmap, &vmf);
551 while (vmap->vm_pfn_count == 0 && err == VM_FAULT_NOPAGE) {
552 kern_yield(PRI_USER);
553 err = vmap->vm_ops->fault(vmap, &vmf);
557 /* translate return code */
560 err = VM_PAGER_AGAIN;
562 case VM_FAULT_SIGBUS:
565 case VM_FAULT_NOPAGE:
567 * By contract the fault handler will return having
568 * busied all the pages itself. If pidx is already
569 * found in the object, it will simply xbusy the first
570 * page and return with vm_pfn_count set to 1.
572 *first = vmap->vm_pfn_first;
573 *last = *first + vmap->vm_pfn_count - 1;
577 err = VM_PAGER_ERROR;
580 up_write(&vmap->vm_mm->mmap_sem);
581 VM_OBJECT_WLOCK(vm_obj);
585 static struct rwlock linux_vma_lock;
586 static TAILQ_HEAD(, vm_area_struct) linux_vma_head =
587 TAILQ_HEAD_INITIALIZER(linux_vma_head);
590 linux_cdev_handle_free(struct vm_area_struct *vmap)
592 /* Drop reference on vm_file */
593 if (vmap->vm_file != NULL)
596 /* Drop reference on mm_struct */
603 linux_cdev_handle_remove(struct vm_area_struct *vmap)
605 rw_wlock(&linux_vma_lock);
606 TAILQ_REMOVE(&linux_vma_head, vmap, vm_entry);
607 rw_wunlock(&linux_vma_lock);
610 static struct vm_area_struct *
611 linux_cdev_handle_find(void *handle)
613 struct vm_area_struct *vmap;
615 rw_rlock(&linux_vma_lock);
616 TAILQ_FOREACH(vmap, &linux_vma_head, vm_entry) {
617 if (vmap->vm_private_data == handle)
620 rw_runlock(&linux_vma_lock);
625 linux_cdev_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
626 vm_ooffset_t foff, struct ucred *cred, u_short *color)
629 MPASS(linux_cdev_handle_find(handle) != NULL);
635 linux_cdev_pager_dtor(void *handle)
637 const struct vm_operations_struct *vm_ops;
638 struct vm_area_struct *vmap;
640 vmap = linux_cdev_handle_find(handle);
644 * Remove handle before calling close operation to prevent
645 * other threads from reusing the handle pointer.
647 linux_cdev_handle_remove(vmap);
649 down_write(&vmap->vm_mm->mmap_sem);
650 vm_ops = vmap->vm_ops;
651 if (likely(vm_ops != NULL))
653 up_write(&vmap->vm_mm->mmap_sem);
655 linux_cdev_handle_free(vmap);
658 static struct cdev_pager_ops linux_cdev_pager_ops[2] = {
661 .cdev_pg_populate = linux_cdev_pager_populate,
662 .cdev_pg_ctor = linux_cdev_pager_ctor,
663 .cdev_pg_dtor = linux_cdev_pager_dtor
667 .cdev_pg_fault = linux_cdev_pager_fault,
668 .cdev_pg_ctor = linux_cdev_pager_ctor,
669 .cdev_pg_dtor = linux_cdev_pager_dtor
674 zap_vma_ptes(struct vm_area_struct *vma, unsigned long address,
681 if (obj == NULL || (obj->flags & OBJ_UNMANAGED) != 0)
683 VM_OBJECT_RLOCK(obj);
684 for (m = vm_page_find_least(obj, OFF_TO_IDX(address));
685 m != NULL && m->pindex < OFF_TO_IDX(address + size);
686 m = TAILQ_NEXT(m, listq))
688 VM_OBJECT_RUNLOCK(obj);
692 static struct file_operations dummy_ldev_ops = {
696 static struct linux_cdev dummy_ldev = {
697 .ops = &dummy_ldev_ops,
700 #define LDEV_SI_DTR 0x0001
701 #define LDEV_SI_REF 0x0002
704 linux_get_fop(struct linux_file *filp, const struct file_operations **fop,
705 struct linux_cdev **dev)
707 struct linux_cdev *ldev;
713 for (siref = ldev->siref;;) {
714 if ((siref & LDEV_SI_DTR) != 0) {
718 MPASS((ldev->siref & LDEV_SI_DTR) == 0);
719 } else if (atomic_fcmpset_int(&ldev->siref, &siref,
720 siref + LDEV_SI_REF)) {
729 linux_drop_fop(struct linux_cdev *ldev)
734 MPASS((ldev->siref & ~LDEV_SI_DTR) != 0);
735 atomic_subtract_int(&ldev->siref, LDEV_SI_REF);
738 #define OPW(fp,td,code) ({ \
739 struct file *__fpop; \
740 __typeof(code) __retval; \
742 __fpop = (td)->td_fpop; \
743 (td)->td_fpop = (fp); \
745 (td)->td_fpop = __fpop; \
750 linux_dev_fdopen(struct cdev *dev, int fflags, struct thread *td,
753 struct linux_cdev *ldev;
754 struct linux_file *filp;
755 const struct file_operations *fop;
760 filp = linux_file_alloc();
761 filp->f_dentry = &filp->f_dentry_store;
762 filp->f_op = ldev->ops;
763 filp->f_mode = file->f_flag;
764 filp->f_flags = file->f_flag;
765 filp->f_vnode = file->f_vnode;
767 refcount_acquire(&ldev->refs);
770 linux_set_current(td);
771 linux_get_fop(filp, &fop, &ldev);
773 if (fop->open != NULL) {
774 error = -fop->open(file->f_vnode, filp);
776 linux_drop_fop(ldev);
777 linux_cdev_deref(filp->f_cdev);
783 /* hold on to the vnode - used for fstat() */
784 vhold(filp->f_vnode);
786 /* release the file from devfs */
787 finit(file, filp->f_mode, DTYPE_DEV, filp, &linuxfileops);
788 linux_drop_fop(ldev);
792 #define LINUX_IOCTL_MIN_PTR 0x10000UL
793 #define LINUX_IOCTL_MAX_PTR (LINUX_IOCTL_MIN_PTR + IOCPARM_MAX)
796 linux_remap_address(void **uaddr, size_t len)
798 uintptr_t uaddr_val = (uintptr_t)(*uaddr);
800 if (unlikely(uaddr_val >= LINUX_IOCTL_MIN_PTR &&
801 uaddr_val < LINUX_IOCTL_MAX_PTR)) {
802 struct task_struct *pts = current;
808 /* compute data offset */
809 uaddr_val -= LINUX_IOCTL_MIN_PTR;
811 /* check that length is within bounds */
812 if ((len > IOCPARM_MAX) ||
813 (uaddr_val + len) > pts->bsd_ioctl_len) {
818 /* re-add kernel buffer address */
819 uaddr_val += (uintptr_t)pts->bsd_ioctl_data;
821 /* update address location */
822 *uaddr = (void *)uaddr_val;
829 linux_copyin(const void *uaddr, void *kaddr, size_t len)
831 if (linux_remap_address(__DECONST(void **, &uaddr), len)) {
834 memcpy(kaddr, uaddr, len);
837 return (-copyin(uaddr, kaddr, len));
841 linux_copyout(const void *kaddr, void *uaddr, size_t len)
843 if (linux_remap_address(&uaddr, len)) {
846 memcpy(uaddr, kaddr, len);
849 return (-copyout(kaddr, uaddr, len));
853 linux_clear_user(void *_uaddr, size_t _len)
855 uint8_t *uaddr = _uaddr;
858 /* make sure uaddr is aligned before going into the fast loop */
859 while (((uintptr_t)uaddr & 7) != 0 && len > 7) {
860 if (subyte(uaddr, 0))
866 /* zero 8 bytes at a time */
869 if (suword64(uaddr, 0))
872 if (suword32(uaddr, 0))
874 if (suword32(uaddr + 4, 0))
881 /* zero fill end, if any */
883 if (subyte(uaddr, 0))
892 linux_access_ok(const void *uaddr, size_t len)
897 /* get start and end address */
898 saddr = (uintptr_t)uaddr;
899 eaddr = (uintptr_t)uaddr + len;
901 /* verify addresses are valid for userspace */
902 return ((saddr == eaddr) ||
903 (eaddr > saddr && eaddr <= VM_MAXUSER_ADDRESS));
907 * This function should return either EINTR or ERESTART depending on
908 * the signal type sent to this thread:
911 linux_get_error(struct task_struct *task, int error)
913 /* check for signal type interrupt code */
914 if (error == EINTR || error == ERESTARTSYS || error == ERESTART) {
915 error = -linux_schedule_get_interrupt_value(task);
923 linux_file_ioctl_sub(struct file *fp, struct linux_file *filp,
924 const struct file_operations *fop, u_long cmd, caddr_t data,
927 struct task_struct *task = current;
931 size = IOCPARM_LEN(cmd);
932 /* refer to logic in sys_ioctl() */
935 * Setup hint for linux_copyin() and linux_copyout().
937 * Background: Linux code expects a user-space address
938 * while FreeBSD supplies a kernel-space address.
940 task->bsd_ioctl_data = data;
941 task->bsd_ioctl_len = size;
942 data = (void *)LINUX_IOCTL_MIN_PTR;
944 /* fetch user-space pointer */
945 data = *(void **)data;
947 #if defined(__amd64__)
948 if (td->td_proc->p_elf_machine == EM_386) {
949 /* try the compat IOCTL handler first */
950 if (fop->compat_ioctl != NULL) {
951 error = -OPW(fp, td, fop->compat_ioctl(filp,
957 /* fallback to the regular IOCTL handler, if any */
958 if (error == ENOTTY && fop->unlocked_ioctl != NULL) {
959 error = -OPW(fp, td, fop->unlocked_ioctl(filp,
965 if (fop->unlocked_ioctl != NULL) {
966 error = -OPW(fp, td, fop->unlocked_ioctl(filp,
973 task->bsd_ioctl_data = NULL;
974 task->bsd_ioctl_len = 0;
977 if (error == EWOULDBLOCK) {
978 /* update kqfilter status, if any */
979 linux_file_kqfilter_poll(filp,
980 LINUX_KQ_FLAG_HAS_READ | LINUX_KQ_FLAG_HAS_WRITE);
982 error = linux_get_error(task, error);
987 #define LINUX_POLL_TABLE_NORMAL ((poll_table *)1)
990 * This function atomically updates the poll wakeup state and returns
991 * the previous state at the time of update.
994 linux_poll_wakeup_state(atomic_t *v, const uint8_t *pstate)
1000 while ((old = atomic_cmpxchg(v, c, pstate[c])) != c)
1007 linux_poll_wakeup_callback(wait_queue_t *wq, unsigned int wq_state, int flags, void *key)
1009 static const uint8_t state[LINUX_FWQ_STATE_MAX] = {
1010 [LINUX_FWQ_STATE_INIT] = LINUX_FWQ_STATE_INIT, /* NOP */
1011 [LINUX_FWQ_STATE_NOT_READY] = LINUX_FWQ_STATE_NOT_READY, /* NOP */
1012 [LINUX_FWQ_STATE_QUEUED] = LINUX_FWQ_STATE_READY,
1013 [LINUX_FWQ_STATE_READY] = LINUX_FWQ_STATE_READY, /* NOP */
1015 struct linux_file *filp = container_of(wq, struct linux_file, f_wait_queue.wq);
1017 switch (linux_poll_wakeup_state(&filp->f_wait_queue.state, state)) {
1018 case LINUX_FWQ_STATE_QUEUED:
1019 linux_poll_wakeup(filp);
1027 linux_poll_wait(struct linux_file *filp, wait_queue_head_t *wqh, poll_table *p)
1029 static const uint8_t state[LINUX_FWQ_STATE_MAX] = {
1030 [LINUX_FWQ_STATE_INIT] = LINUX_FWQ_STATE_NOT_READY,
1031 [LINUX_FWQ_STATE_NOT_READY] = LINUX_FWQ_STATE_NOT_READY, /* NOP */
1032 [LINUX_FWQ_STATE_QUEUED] = LINUX_FWQ_STATE_QUEUED, /* NOP */
1033 [LINUX_FWQ_STATE_READY] = LINUX_FWQ_STATE_QUEUED,
1036 /* check if we are called inside the select system call */
1037 if (p == LINUX_POLL_TABLE_NORMAL)
1038 selrecord(curthread, &filp->f_selinfo);
1040 switch (linux_poll_wakeup_state(&filp->f_wait_queue.state, state)) {
1041 case LINUX_FWQ_STATE_INIT:
1042 /* NOTE: file handles can only belong to one wait-queue */
1043 filp->f_wait_queue.wqh = wqh;
1044 filp->f_wait_queue.wq.func = &linux_poll_wakeup_callback;
1045 add_wait_queue(wqh, &filp->f_wait_queue.wq);
1046 atomic_set(&filp->f_wait_queue.state, LINUX_FWQ_STATE_QUEUED);
1054 linux_poll_wait_dequeue(struct linux_file *filp)
1056 static const uint8_t state[LINUX_FWQ_STATE_MAX] = {
1057 [LINUX_FWQ_STATE_INIT] = LINUX_FWQ_STATE_INIT, /* NOP */
1058 [LINUX_FWQ_STATE_NOT_READY] = LINUX_FWQ_STATE_INIT,
1059 [LINUX_FWQ_STATE_QUEUED] = LINUX_FWQ_STATE_INIT,
1060 [LINUX_FWQ_STATE_READY] = LINUX_FWQ_STATE_INIT,
1063 seldrain(&filp->f_selinfo);
1065 switch (linux_poll_wakeup_state(&filp->f_wait_queue.state, state)) {
1066 case LINUX_FWQ_STATE_NOT_READY:
1067 case LINUX_FWQ_STATE_QUEUED:
1068 case LINUX_FWQ_STATE_READY:
1069 remove_wait_queue(filp->f_wait_queue.wqh, &filp->f_wait_queue.wq);
1077 linux_poll_wakeup(struct linux_file *filp)
1079 /* this function should be NULL-safe */
1083 selwakeup(&filp->f_selinfo);
1085 spin_lock(&filp->f_kqlock);
1086 filp->f_kqflags |= LINUX_KQ_FLAG_NEED_READ |
1087 LINUX_KQ_FLAG_NEED_WRITE;
1089 /* make sure the "knote" gets woken up */
1090 KNOTE_LOCKED(&filp->f_selinfo.si_note, 1);
1091 spin_unlock(&filp->f_kqlock);
1095 linux_file_kqfilter_detach(struct knote *kn)
1097 struct linux_file *filp = kn->kn_hook;
1099 spin_lock(&filp->f_kqlock);
1100 knlist_remove(&filp->f_selinfo.si_note, kn, 1);
1101 spin_unlock(&filp->f_kqlock);
1105 linux_file_kqfilter_read_event(struct knote *kn, long hint)
1107 struct linux_file *filp = kn->kn_hook;
1109 mtx_assert(&filp->f_kqlock.m, MA_OWNED);
1111 return ((filp->f_kqflags & LINUX_KQ_FLAG_NEED_READ) ? 1 : 0);
1115 linux_file_kqfilter_write_event(struct knote *kn, long hint)
1117 struct linux_file *filp = kn->kn_hook;
1119 mtx_assert(&filp->f_kqlock.m, MA_OWNED);
1121 return ((filp->f_kqflags & LINUX_KQ_FLAG_NEED_WRITE) ? 1 : 0);
1124 static struct filterops linux_dev_kqfiltops_read = {
1126 .f_detach = linux_file_kqfilter_detach,
1127 .f_event = linux_file_kqfilter_read_event,
1130 static struct filterops linux_dev_kqfiltops_write = {
1132 .f_detach = linux_file_kqfilter_detach,
1133 .f_event = linux_file_kqfilter_write_event,
1137 linux_file_kqfilter_poll(struct linux_file *filp, int kqflags)
1140 const struct file_operations *fop;
1141 struct linux_cdev *ldev;
1144 if ((filp->f_kqflags & kqflags) == 0)
1149 linux_get_fop(filp, &fop, &ldev);
1150 /* get the latest polling state */
1151 temp = OPW(filp->_file, td, fop->poll(filp, NULL));
1152 linux_drop_fop(ldev);
1154 spin_lock(&filp->f_kqlock);
1156 filp->f_kqflags &= ~(LINUX_KQ_FLAG_NEED_READ |
1157 LINUX_KQ_FLAG_NEED_WRITE);
1158 /* update kqflags */
1159 if ((temp & (POLLIN | POLLOUT)) != 0) {
1160 if ((temp & POLLIN) != 0)
1161 filp->f_kqflags |= LINUX_KQ_FLAG_NEED_READ;
1162 if ((temp & POLLOUT) != 0)
1163 filp->f_kqflags |= LINUX_KQ_FLAG_NEED_WRITE;
1165 /* make sure the "knote" gets woken up */
1166 KNOTE_LOCKED(&filp->f_selinfo.si_note, 0);
1168 spin_unlock(&filp->f_kqlock);
1172 linux_file_kqfilter(struct file *file, struct knote *kn)
1174 struct linux_file *filp;
1179 filp = (struct linux_file *)file->f_data;
1180 filp->f_flags = file->f_flag;
1181 if (filp->f_op->poll == NULL)
1184 spin_lock(&filp->f_kqlock);
1185 switch (kn->kn_filter) {
1187 filp->f_kqflags |= LINUX_KQ_FLAG_HAS_READ;
1188 kn->kn_fop = &linux_dev_kqfiltops_read;
1190 knlist_add(&filp->f_selinfo.si_note, kn, 1);
1194 filp->f_kqflags |= LINUX_KQ_FLAG_HAS_WRITE;
1195 kn->kn_fop = &linux_dev_kqfiltops_write;
1197 knlist_add(&filp->f_selinfo.si_note, kn, 1);
1204 spin_unlock(&filp->f_kqlock);
1207 linux_set_current(td);
1209 /* update kqfilter status, if any */
1210 linux_file_kqfilter_poll(filp,
1211 LINUX_KQ_FLAG_HAS_READ | LINUX_KQ_FLAG_HAS_WRITE);
1217 linux_file_mmap_single(struct file *fp, const struct file_operations *fop,
1218 vm_ooffset_t *offset, vm_size_t size, struct vm_object **object,
1219 int nprot, struct thread *td)
1221 struct task_struct *task;
1222 struct vm_area_struct *vmap;
1223 struct mm_struct *mm;
1224 struct linux_file *filp;
1228 filp = (struct linux_file *)fp->f_data;
1229 filp->f_flags = fp->f_flag;
1231 if (fop->mmap == NULL)
1232 return (EOPNOTSUPP);
1234 linux_set_current(td);
1237 * The same VM object might be shared by multiple processes
1238 * and the mm_struct is usually freed when a process exits.
1240 * The atomic reference below makes sure the mm_struct is
1241 * available as long as the vmap is in the linux_vma_head.
1245 if (atomic_inc_not_zero(&mm->mm_users) == 0)
1248 vmap = kzalloc(sizeof(*vmap), GFP_KERNEL);
1250 vmap->vm_end = size;
1251 vmap->vm_pgoff = *offset / PAGE_SIZE;
1253 vmap->vm_flags = vmap->vm_page_prot = (nprot & VM_PROT_ALL);
1254 vmap->vm_ops = NULL;
1255 vmap->vm_file = get_file(filp);
1258 if (unlikely(down_write_killable(&vmap->vm_mm->mmap_sem))) {
1259 error = linux_get_error(task, EINTR);
1261 error = -OPW(fp, td, fop->mmap(filp, vmap));
1262 error = linux_get_error(task, error);
1263 up_write(&vmap->vm_mm->mmap_sem);
1267 linux_cdev_handle_free(vmap);
1271 attr = pgprot2cachemode(vmap->vm_page_prot);
1273 if (vmap->vm_ops != NULL) {
1274 struct vm_area_struct *ptr;
1275 void *vm_private_data;
1278 if (vmap->vm_ops->open == NULL ||
1279 vmap->vm_ops->close == NULL ||
1280 vmap->vm_private_data == NULL) {
1281 /* free allocated VM area struct */
1282 linux_cdev_handle_free(vmap);
1286 vm_private_data = vmap->vm_private_data;
1288 rw_wlock(&linux_vma_lock);
1289 TAILQ_FOREACH(ptr, &linux_vma_head, vm_entry) {
1290 if (ptr->vm_private_data == vm_private_data)
1293 /* check if there is an existing VM area struct */
1295 /* check if the VM area structure is invalid */
1296 if (ptr->vm_ops == NULL ||
1297 ptr->vm_ops->open == NULL ||
1298 ptr->vm_ops->close == NULL) {
1303 vm_no_fault = (ptr->vm_ops->fault == NULL);
1306 /* insert VM area structure into list */
1307 TAILQ_INSERT_TAIL(&linux_vma_head, vmap, vm_entry);
1309 vm_no_fault = (vmap->vm_ops->fault == NULL);
1311 rw_wunlock(&linux_vma_lock);
1314 /* free allocated VM area struct */
1315 linux_cdev_handle_free(vmap);
1316 /* check for stale VM area struct */
1317 if (error != EEXIST)
1321 /* check if there is no fault handler */
1323 *object = cdev_pager_allocate(vm_private_data, OBJT_DEVICE,
1324 &linux_cdev_pager_ops[1], size, nprot, *offset,
1327 *object = cdev_pager_allocate(vm_private_data, OBJT_MGTDEVICE,
1328 &linux_cdev_pager_ops[0], size, nprot, *offset,
1332 /* check if allocating the VM object failed */
1333 if (*object == NULL) {
1335 /* remove VM area struct from list */
1336 linux_cdev_handle_remove(vmap);
1337 /* free allocated VM area struct */
1338 linux_cdev_handle_free(vmap);
1345 sg = sglist_alloc(1, M_WAITOK);
1346 sglist_append_phys(sg,
1347 (vm_paddr_t)vmap->vm_pfn << PAGE_SHIFT, vmap->vm_len);
1349 *object = vm_pager_allocate(OBJT_SG, sg, vmap->vm_len,
1350 nprot, 0, td->td_ucred);
1352 linux_cdev_handle_free(vmap);
1354 if (*object == NULL) {
1360 if (attr != VM_MEMATTR_DEFAULT) {
1361 VM_OBJECT_WLOCK(*object);
1362 vm_object_set_memattr(*object, attr);
1363 VM_OBJECT_WUNLOCK(*object);
1369 struct cdevsw linuxcdevsw = {
1370 .d_version = D_VERSION,
1371 .d_fdopen = linux_dev_fdopen,
1372 .d_name = "lkpidev",
1376 linux_file_read(struct file *file, struct uio *uio, struct ucred *active_cred,
1377 int flags, struct thread *td)
1379 struct linux_file *filp;
1380 const struct file_operations *fop;
1381 struct linux_cdev *ldev;
1386 filp = (struct linux_file *)file->f_data;
1387 filp->f_flags = file->f_flag;
1388 /* XXX no support for I/O vectors currently */
1389 if (uio->uio_iovcnt != 1)
1390 return (EOPNOTSUPP);
1391 if (uio->uio_resid > DEVFS_IOSIZE_MAX)
1393 linux_set_current(td);
1394 linux_get_fop(filp, &fop, &ldev);
1395 if (fop->read != NULL) {
1396 bytes = OPW(file, td, fop->read(filp,
1397 uio->uio_iov->iov_base,
1398 uio->uio_iov->iov_len, &uio->uio_offset));
1400 uio->uio_iov->iov_base =
1401 ((uint8_t *)uio->uio_iov->iov_base) + bytes;
1402 uio->uio_iov->iov_len -= bytes;
1403 uio->uio_resid -= bytes;
1405 error = linux_get_error(current, -bytes);
1410 /* update kqfilter status, if any */
1411 linux_file_kqfilter_poll(filp, LINUX_KQ_FLAG_HAS_READ);
1412 linux_drop_fop(ldev);
1418 linux_file_write(struct file *file, struct uio *uio, struct ucred *active_cred,
1419 int flags, struct thread *td)
1421 struct linux_file *filp;
1422 const struct file_operations *fop;
1423 struct linux_cdev *ldev;
1427 filp = (struct linux_file *)file->f_data;
1428 filp->f_flags = file->f_flag;
1429 /* XXX no support for I/O vectors currently */
1430 if (uio->uio_iovcnt != 1)
1431 return (EOPNOTSUPP);
1432 if (uio->uio_resid > DEVFS_IOSIZE_MAX)
1434 linux_set_current(td);
1435 linux_get_fop(filp, &fop, &ldev);
1436 if (fop->write != NULL) {
1437 bytes = OPW(file, td, fop->write(filp,
1438 uio->uio_iov->iov_base,
1439 uio->uio_iov->iov_len, &uio->uio_offset));
1441 uio->uio_iov->iov_base =
1442 ((uint8_t *)uio->uio_iov->iov_base) + bytes;
1443 uio->uio_iov->iov_len -= bytes;
1444 uio->uio_resid -= bytes;
1447 error = linux_get_error(current, -bytes);
1452 /* update kqfilter status, if any */
1453 linux_file_kqfilter_poll(filp, LINUX_KQ_FLAG_HAS_WRITE);
1455 linux_drop_fop(ldev);
1461 linux_file_poll(struct file *file, int events, struct ucred *active_cred,
1464 struct linux_file *filp;
1465 const struct file_operations *fop;
1466 struct linux_cdev *ldev;
1469 filp = (struct linux_file *)file->f_data;
1470 filp->f_flags = file->f_flag;
1471 linux_set_current(td);
1472 linux_get_fop(filp, &fop, &ldev);
1473 if (fop->poll != NULL) {
1474 revents = OPW(file, td, fop->poll(filp,
1475 LINUX_POLL_TABLE_NORMAL)) & events;
1479 linux_drop_fop(ldev);
1484 linux_file_close(struct file *file, struct thread *td)
1486 struct linux_file *filp;
1487 int (*release)(struct inode *, struct linux_file *);
1488 const struct file_operations *fop;
1489 struct linux_cdev *ldev;
1492 filp = (struct linux_file *)file->f_data;
1494 KASSERT(file_count(filp) == 0,
1495 ("File refcount(%d) is not zero", file_count(filp)));
1501 filp->f_flags = file->f_flag;
1502 linux_set_current(td);
1503 linux_poll_wait_dequeue(filp);
1504 linux_get_fop(filp, &fop, &ldev);
1506 * Always use the real release function, if any, to avoid
1507 * leaking device resources:
1509 release = filp->f_op->release;
1510 if (release != NULL)
1511 error = -OPW(file, td, 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;
1530 struct fiodgname_arg *fgn;
1535 filp = (struct linux_file *)fp->f_data;
1536 filp->f_flags = fp->f_flag;
1537 linux_get_fop(filp, &fop, &ldev);
1539 linux_set_current(td);
1544 if (fop->fasync == NULL)
1546 error = -OPW(fp, td, fop->fasync(0, filp, fp->f_flag & FASYNC));
1549 error = fsetown(*(int *)data, &filp->f_sigio);
1551 if (fop->fasync == NULL)
1553 error = -OPW(fp, td, fop->fasync(0, filp,
1554 fp->f_flag & FASYNC));
1558 *(int *)data = fgetown(&filp->f_sigio);
1561 #ifdef COMPAT_FREEBSD32
1564 if (filp->f_cdev == NULL || filp->f_cdev->cdev == NULL) {
1569 p = devtoname(filp->f_cdev->cdev);
1575 error = copyout(p, fiodgname_buf_get_ptr(fgn, cmd), i);
1578 error = linux_file_ioctl_sub(fp, filp, fop, cmd, data, td);
1581 linux_drop_fop(ldev);
1586 linux_file_mmap_sub(struct thread *td, vm_size_t objsize, vm_prot_t prot,
1587 vm_prot_t *maxprotp, int *flagsp, struct file *fp,
1588 vm_ooffset_t *foff, const struct file_operations *fop, vm_object_t *objp)
1591 * Character devices do not provide private mappings
1594 if ((*maxprotp & VM_PROT_WRITE) == 0 &&
1595 (prot & VM_PROT_WRITE) != 0)
1597 if ((*flagsp & (MAP_PRIVATE | MAP_COPY)) != 0)
1600 return (linux_file_mmap_single(fp, fop, foff, objsize, objp,
1605 linux_file_mmap(struct file *fp, vm_map_t map, vm_offset_t *addr, vm_size_t size,
1606 vm_prot_t prot, vm_prot_t cap_maxprot, int flags, vm_ooffset_t foff,
1609 struct linux_file *filp;
1610 const struct file_operations *fop;
1611 struct linux_cdev *ldev;
1618 filp = (struct linux_file *)fp->f_data;
1622 return (EOPNOTSUPP);
1625 * Ensure that file and memory protections are
1629 if (mp != NULL && (mp->mnt_flag & MNT_NOEXEC) != 0) {
1630 maxprot = VM_PROT_NONE;
1631 if ((prot & VM_PROT_EXECUTE) != 0)
1634 maxprot = VM_PROT_EXECUTE;
1635 if ((fp->f_flag & FREAD) != 0)
1636 maxprot |= VM_PROT_READ;
1637 else if ((prot & VM_PROT_READ) != 0)
1641 * If we are sharing potential changes via MAP_SHARED and we
1642 * are trying to get write permission although we opened it
1643 * without asking for it, bail out.
1645 * Note that most character devices always share mappings.
1647 * Rely on linux_file_mmap_sub() to fail invalid MAP_PRIVATE
1648 * requests rather than doing it here.
1650 if ((flags & MAP_SHARED) != 0) {
1651 if ((fp->f_flag & FWRITE) != 0)
1652 maxprot |= VM_PROT_WRITE;
1653 else if ((prot & VM_PROT_WRITE) != 0)
1656 maxprot &= cap_maxprot;
1658 linux_get_fop(filp, &fop, &ldev);
1659 error = linux_file_mmap_sub(td, size, prot, &maxprot, &flags, fp,
1660 &foff, fop, &object);
1664 error = vm_mmap_object(map, addr, size, prot, maxprot, flags, object,
1667 vm_object_deallocate(object);
1669 linux_drop_fop(ldev);
1674 linux_file_stat(struct file *fp, struct stat *sb, struct ucred *active_cred,
1677 struct linux_file *filp;
1681 filp = (struct linux_file *)fp->f_data;
1682 if (filp->f_vnode == NULL)
1683 return (EOPNOTSUPP);
1687 vn_lock(vp, LK_SHARED | LK_RETRY);
1688 error = VOP_STAT(vp, sb, td->td_ucred, NOCRED, td);
1695 linux_file_fill_kinfo(struct file *fp, struct kinfo_file *kif,
1696 struct filedesc *fdp)
1698 struct linux_file *filp;
1706 kif->kf_type = KF_TYPE_DEV;
1709 FILEDESC_SUNLOCK(fdp);
1710 error = vn_fill_kinfo_vnode(vp, kif);
1712 kif->kf_type = KF_TYPE_VNODE;
1713 FILEDESC_SLOCK(fdp);
1719 linux_iminor(struct inode *inode)
1721 struct linux_cdev *ldev;
1723 if (inode == NULL || inode->v_rdev == NULL ||
1724 inode->v_rdev->si_devsw != &linuxcdevsw)
1726 ldev = inode->v_rdev->si_drv1;
1730 return (minor(ldev->dev));
1733 struct fileops linuxfileops = {
1734 .fo_read = linux_file_read,
1735 .fo_write = linux_file_write,
1736 .fo_truncate = invfo_truncate,
1737 .fo_kqfilter = linux_file_kqfilter,
1738 .fo_stat = linux_file_stat,
1739 .fo_fill_kinfo = linux_file_fill_kinfo,
1740 .fo_poll = linux_file_poll,
1741 .fo_close = linux_file_close,
1742 .fo_ioctl = linux_file_ioctl,
1743 .fo_mmap = linux_file_mmap,
1744 .fo_chmod = invfo_chmod,
1745 .fo_chown = invfo_chown,
1746 .fo_sendfile = invfo_sendfile,
1747 .fo_flags = DFLAG_PASSABLE,
1751 * Hash of vmmap addresses. This is infrequently accessed and does not
1752 * need to be particularly large. This is done because we must store the
1753 * caller's idea of the map size to properly unmap.
1756 LIST_ENTRY(vmmap) vm_next;
1758 unsigned long vm_size;
1762 struct vmmap *lh_first;
1764 #define VMMAP_HASH_SIZE 64
1765 #define VMMAP_HASH_MASK (VMMAP_HASH_SIZE - 1)
1766 #define VM_HASH(addr) ((uintptr_t)(addr) >> PAGE_SHIFT) & VMMAP_HASH_MASK
1767 static struct vmmaphd vmmaphead[VMMAP_HASH_SIZE];
1768 static struct mtx vmmaplock;
1771 vmmap_add(void *addr, unsigned long size)
1773 struct vmmap *vmmap;
1775 vmmap = kmalloc(sizeof(*vmmap), GFP_KERNEL);
1776 mtx_lock(&vmmaplock);
1777 vmmap->vm_size = size;
1778 vmmap->vm_addr = addr;
1779 LIST_INSERT_HEAD(&vmmaphead[VM_HASH(addr)], vmmap, vm_next);
1780 mtx_unlock(&vmmaplock);
1783 static struct vmmap *
1784 vmmap_remove(void *addr)
1786 struct vmmap *vmmap;
1788 mtx_lock(&vmmaplock);
1789 LIST_FOREACH(vmmap, &vmmaphead[VM_HASH(addr)], vm_next)
1790 if (vmmap->vm_addr == addr)
1793 LIST_REMOVE(vmmap, vm_next);
1794 mtx_unlock(&vmmaplock);
1799 #if defined(__i386__) || defined(__amd64__) || defined(__powerpc__) || defined(__aarch64__)
1801 _ioremap_attr(vm_paddr_t phys_addr, unsigned long size, int attr)
1805 addr = pmap_mapdev_attr(phys_addr, size, attr);
1808 vmmap_add(addr, size);
1817 struct vmmap *vmmap;
1819 vmmap = vmmap_remove(addr);
1822 #if defined(__i386__) || defined(__amd64__) || defined(__powerpc__) || defined(__aarch64__)
1823 pmap_unmapdev((vm_offset_t)addr, vmmap->vm_size);
1829 vmap(struct page **pages, unsigned int count, unsigned long flags, int prot)
1834 size = count * PAGE_SIZE;
1835 off = kva_alloc(size);
1838 vmmap_add((void *)off, size);
1839 pmap_qenter(off, pages, count);
1841 return ((void *)off);
1847 struct vmmap *vmmap;
1849 vmmap = vmmap_remove(addr);
1852 pmap_qremove((vm_offset_t)addr, vmmap->vm_size / PAGE_SIZE);
1853 kva_free((vm_offset_t)addr, vmmap->vm_size);
1858 kvasprintf(gfp_t gfp, const char *fmt, va_list ap)
1865 len = vsnprintf(NULL, 0, fmt, aq);
1868 p = kmalloc(len + 1, gfp);
1870 vsnprintf(p, len + 1, fmt, ap);
1876 kasprintf(gfp_t gfp, const char *fmt, ...)
1882 p = kvasprintf(gfp, fmt, ap);
1889 linux_timer_callback_wrapper(void *context)
1891 struct timer_list *timer;
1893 linux_set_current(curthread);
1896 timer->function(timer->data);
1900 mod_timer(struct timer_list *timer, int expires)
1904 timer->expires = expires;
1905 ret = callout_reset(&timer->callout,
1906 linux_timer_jiffies_until(expires),
1907 &linux_timer_callback_wrapper, timer);
1909 MPASS(ret == 0 || ret == 1);
1915 add_timer(struct timer_list *timer)
1918 callout_reset(&timer->callout,
1919 linux_timer_jiffies_until(timer->expires),
1920 &linux_timer_callback_wrapper, timer);
1924 add_timer_on(struct timer_list *timer, int cpu)
1927 callout_reset_on(&timer->callout,
1928 linux_timer_jiffies_until(timer->expires),
1929 &linux_timer_callback_wrapper, timer, cpu);
1933 del_timer(struct timer_list *timer)
1936 if (callout_stop(&(timer)->callout) == -1)
1942 del_timer_sync(struct timer_list *timer)
1945 if (callout_drain(&(timer)->callout) == -1)
1950 /* greatest common divisor, Euclid equation */
1952 lkpi_gcd_64(uint64_t a, uint64_t b)
1966 uint64_t lkpi_nsec2hz_rem;
1967 uint64_t lkpi_nsec2hz_div = 1000000000ULL;
1968 uint64_t lkpi_nsec2hz_max;
1970 uint64_t lkpi_usec2hz_rem;
1971 uint64_t lkpi_usec2hz_div = 1000000ULL;
1972 uint64_t lkpi_usec2hz_max;
1974 uint64_t lkpi_msec2hz_rem;
1975 uint64_t lkpi_msec2hz_div = 1000ULL;
1976 uint64_t lkpi_msec2hz_max;
1979 linux_timer_init(void *arg)
1984 * Compute an internal HZ value which can divide 2**32 to
1985 * avoid timer rounding problems when the tick value wraps
1988 linux_timer_hz_mask = 1;
1989 while (linux_timer_hz_mask < (unsigned long)hz)
1990 linux_timer_hz_mask *= 2;
1991 linux_timer_hz_mask--;
1993 /* compute some internal constants */
1995 lkpi_nsec2hz_rem = hz;
1996 lkpi_usec2hz_rem = hz;
1997 lkpi_msec2hz_rem = hz;
1999 gcd = lkpi_gcd_64(lkpi_nsec2hz_rem, lkpi_nsec2hz_div);
2000 lkpi_nsec2hz_rem /= gcd;
2001 lkpi_nsec2hz_div /= gcd;
2002 lkpi_nsec2hz_max = -1ULL / lkpi_nsec2hz_rem;
2004 gcd = lkpi_gcd_64(lkpi_usec2hz_rem, lkpi_usec2hz_div);
2005 lkpi_usec2hz_rem /= gcd;
2006 lkpi_usec2hz_div /= gcd;
2007 lkpi_usec2hz_max = -1ULL / lkpi_usec2hz_rem;
2009 gcd = lkpi_gcd_64(lkpi_msec2hz_rem, lkpi_msec2hz_div);
2010 lkpi_msec2hz_rem /= gcd;
2011 lkpi_msec2hz_div /= gcd;
2012 lkpi_msec2hz_max = -1ULL / lkpi_msec2hz_rem;
2014 SYSINIT(linux_timer, SI_SUB_DRIVERS, SI_ORDER_FIRST, linux_timer_init, NULL);
2017 linux_complete_common(struct completion *c, int all)
2024 wakeup_swapper = sleepq_broadcast(c, SLEEPQ_SLEEP, 0, 0);
2026 if (c->done != UINT_MAX)
2028 wakeup_swapper = sleepq_signal(c, SLEEPQ_SLEEP, 0, 0);
2036 * Indefinite wait for done != 0 with or without signals.
2039 linux_wait_for_common(struct completion *c, int flags)
2041 struct task_struct *task;
2044 if (SCHEDULER_STOPPED())
2050 flags = SLEEPQ_INTERRUPTIBLE | SLEEPQ_SLEEP;
2052 flags = SLEEPQ_SLEEP;
2058 sleepq_add(c, NULL, "completion", flags, 0);
2059 if (flags & SLEEPQ_INTERRUPTIBLE) {
2061 error = -sleepq_wait_sig(c, 0);
2064 linux_schedule_save_interrupt_value(task, error);
2065 error = -ERESTARTSYS;
2074 if (c->done != UINT_MAX)
2083 * Time limited wait for done != 0 with or without signals.
2086 linux_wait_for_timeout_common(struct completion *c, int timeout, int flags)
2088 struct task_struct *task;
2089 int end = jiffies + timeout;
2092 if (SCHEDULER_STOPPED())
2098 flags = SLEEPQ_INTERRUPTIBLE | SLEEPQ_SLEEP;
2100 flags = SLEEPQ_SLEEP;
2106 sleepq_add(c, NULL, "completion", flags, 0);
2107 sleepq_set_timeout(c, linux_timer_jiffies_until(end));
2110 if (flags & SLEEPQ_INTERRUPTIBLE)
2111 error = -sleepq_timedwait_sig(c, 0);
2113 error = -sleepq_timedwait(c, 0);
2117 /* check for timeout */
2118 if (error == -EWOULDBLOCK) {
2119 error = 0; /* timeout */
2121 /* signal happened */
2122 linux_schedule_save_interrupt_value(task, error);
2123 error = -ERESTARTSYS;
2128 if (c->done != UINT_MAX)
2132 /* return how many jiffies are left */
2133 error = linux_timer_jiffies_until(end);
2139 linux_try_wait_for_completion(struct completion *c)
2144 isdone = (c->done != 0);
2145 if (c->done != 0 && c->done != UINT_MAX)
2152 linux_completion_done(struct completion *c)
2157 isdone = (c->done != 0);
2163 linux_cdev_deref(struct linux_cdev *ldev)
2166 if (refcount_release(&ldev->refs))
2171 linux_cdev_release(struct kobject *kobj)
2173 struct linux_cdev *cdev;
2174 struct kobject *parent;
2176 cdev = container_of(kobj, struct linux_cdev, kobj);
2177 parent = kobj->parent;
2178 linux_destroy_dev(cdev);
2179 linux_cdev_deref(cdev);
2180 kobject_put(parent);
2184 linux_cdev_static_release(struct kobject *kobj)
2186 struct linux_cdev *cdev;
2187 struct kobject *parent;
2189 cdev = container_of(kobj, struct linux_cdev, kobj);
2190 parent = kobj->parent;
2191 linux_destroy_dev(cdev);
2192 kobject_put(parent);
2196 linux_destroy_dev(struct linux_cdev *ldev)
2199 if (ldev->cdev == NULL)
2202 MPASS((ldev->siref & LDEV_SI_DTR) == 0);
2203 atomic_set_int(&ldev->siref, LDEV_SI_DTR);
2204 while ((atomic_load_int(&ldev->siref) & ~LDEV_SI_DTR) != 0)
2205 pause("ldevdtr", hz / 4);
2207 destroy_dev(ldev->cdev);
2211 const struct kobj_type linux_cdev_ktype = {
2212 .release = linux_cdev_release,
2215 const struct kobj_type linux_cdev_static_ktype = {
2216 .release = linux_cdev_static_release,
2220 linux_handle_ifnet_link_event(void *arg, struct ifnet *ifp, int linkstate)
2222 struct notifier_block *nb;
2225 if (linkstate == LINK_STATE_UP)
2226 nb->notifier_call(nb, NETDEV_UP, ifp);
2228 nb->notifier_call(nb, NETDEV_DOWN, ifp);
2232 linux_handle_ifnet_arrival_event(void *arg, struct ifnet *ifp)
2234 struct notifier_block *nb;
2237 nb->notifier_call(nb, NETDEV_REGISTER, ifp);
2241 linux_handle_ifnet_departure_event(void *arg, struct ifnet *ifp)
2243 struct notifier_block *nb;
2246 nb->notifier_call(nb, NETDEV_UNREGISTER, ifp);
2250 linux_handle_iflladdr_event(void *arg, struct ifnet *ifp)
2252 struct notifier_block *nb;
2255 nb->notifier_call(nb, NETDEV_CHANGEADDR, ifp);
2259 linux_handle_ifaddr_event(void *arg, struct ifnet *ifp)
2261 struct notifier_block *nb;
2264 nb->notifier_call(nb, NETDEV_CHANGEIFADDR, ifp);
2268 register_netdevice_notifier(struct notifier_block *nb)
2271 nb->tags[NETDEV_UP] = EVENTHANDLER_REGISTER(
2272 ifnet_link_event, linux_handle_ifnet_link_event, nb, 0);
2273 nb->tags[NETDEV_REGISTER] = EVENTHANDLER_REGISTER(
2274 ifnet_arrival_event, linux_handle_ifnet_arrival_event, nb, 0);
2275 nb->tags[NETDEV_UNREGISTER] = EVENTHANDLER_REGISTER(
2276 ifnet_departure_event, linux_handle_ifnet_departure_event, nb, 0);
2277 nb->tags[NETDEV_CHANGEADDR] = EVENTHANDLER_REGISTER(
2278 iflladdr_event, linux_handle_iflladdr_event, nb, 0);
2284 register_inetaddr_notifier(struct notifier_block *nb)
2287 nb->tags[NETDEV_CHANGEIFADDR] = EVENTHANDLER_REGISTER(
2288 ifaddr_event, linux_handle_ifaddr_event, nb, 0);
2293 unregister_netdevice_notifier(struct notifier_block *nb)
2296 EVENTHANDLER_DEREGISTER(ifnet_link_event,
2297 nb->tags[NETDEV_UP]);
2298 EVENTHANDLER_DEREGISTER(ifnet_arrival_event,
2299 nb->tags[NETDEV_REGISTER]);
2300 EVENTHANDLER_DEREGISTER(ifnet_departure_event,
2301 nb->tags[NETDEV_UNREGISTER]);
2302 EVENTHANDLER_DEREGISTER(iflladdr_event,
2303 nb->tags[NETDEV_CHANGEADDR]);
2309 unregister_inetaddr_notifier(struct notifier_block *nb)
2312 EVENTHANDLER_DEREGISTER(ifaddr_event,
2313 nb->tags[NETDEV_CHANGEIFADDR]);
2318 struct list_sort_thunk {
2319 int (*cmp)(void *, struct list_head *, struct list_head *);
2324 linux_le_cmp(void *priv, const void *d1, const void *d2)
2326 struct list_head *le1, *le2;
2327 struct list_sort_thunk *thunk;
2330 le1 = *(__DECONST(struct list_head **, d1));
2331 le2 = *(__DECONST(struct list_head **, d2));
2332 return ((thunk->cmp)(thunk->priv, le1, le2));
2336 list_sort(void *priv, struct list_head *head, int (*cmp)(void *priv,
2337 struct list_head *a, struct list_head *b))
2339 struct list_sort_thunk thunk;
2340 struct list_head **ar, *le;
2344 list_for_each(le, head)
2346 ar = malloc(sizeof(struct list_head *) * count, M_KMALLOC, M_WAITOK);
2348 list_for_each(le, head)
2352 qsort_r(ar, count, sizeof(struct list_head *), &thunk, linux_le_cmp);
2353 INIT_LIST_HEAD(head);
2354 for (i = 0; i < count; i++)
2355 list_add_tail(ar[i], head);
2356 free(ar, M_KMALLOC);
2360 linux_irq_handler(void *ent)
2362 struct irq_ent *irqe;
2364 linux_set_current(curthread);
2367 irqe->handler(irqe->irq, irqe->arg);
2370 #if defined(__i386__) || defined(__amd64__)
2372 linux_wbinvd_on_all_cpus(void)
2375 pmap_invalidate_cache();
2381 linux_on_each_cpu(void callback(void *), void *data)
2384 smp_rendezvous(smp_no_rendezvous_barrier, callback,
2385 smp_no_rendezvous_barrier, data);
2390 linux_in_atomic(void)
2393 return ((curthread->td_pflags & TDP_NOFAULTING) != 0);
2397 linux_find_cdev(const char *name, unsigned major, unsigned minor)
2399 dev_t dev = MKDEV(major, minor);
2403 LIST_FOREACH(cdev, &linuxcdevsw.d_devs, si_list) {
2404 struct linux_cdev *ldev = cdev->si_drv1;
2405 if (ldev->dev == dev &&
2406 strcmp(kobject_name(&ldev->kobj), name) == 0) {
2412 return (cdev != NULL ? cdev->si_drv1 : NULL);
2416 __register_chrdev(unsigned int major, unsigned int baseminor,
2417 unsigned int count, const char *name,
2418 const struct file_operations *fops)
2420 struct linux_cdev *cdev;
2424 for (i = baseminor; i < baseminor + count; i++) {
2425 cdev = cdev_alloc();
2427 kobject_set_name(&cdev->kobj, name);
2429 ret = cdev_add(cdev, makedev(major, i), 1);
2437 __register_chrdev_p(unsigned int major, unsigned int baseminor,
2438 unsigned int count, const char *name,
2439 const struct file_operations *fops, uid_t uid,
2440 gid_t gid, int mode)
2442 struct linux_cdev *cdev;
2446 for (i = baseminor; i < baseminor + count; i++) {
2447 cdev = cdev_alloc();
2449 kobject_set_name(&cdev->kobj, name);
2451 ret = cdev_add_ext(cdev, makedev(major, i), uid, gid, mode);
2459 __unregister_chrdev(unsigned int major, unsigned int baseminor,
2460 unsigned int count, const char *name)
2462 struct linux_cdev *cdevp;
2465 for (i = baseminor; i < baseminor + count; i++) {
2466 cdevp = linux_find_cdev(name, major, i);
2473 linux_dump_stack(void)
2484 #if defined(__i386__) || defined(__amd64__)
2485 bool linux_cpu_has_clflush;
2489 linux_compat_init(void *arg)
2491 struct sysctl_oid *rootoid;
2494 #if defined(__i386__) || defined(__amd64__)
2495 linux_cpu_has_clflush = (cpu_feature & CPUID_CLFSH);
2497 rw_init(&linux_vma_lock, "lkpi-vma-lock");
2499 rootoid = SYSCTL_ADD_ROOT_NODE(NULL,
2500 OID_AUTO, "sys", CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, "sys");
2501 kobject_init(&linux_class_root, &linux_class_ktype);
2502 kobject_set_name(&linux_class_root, "class");
2503 linux_class_root.oidp = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(rootoid),
2504 OID_AUTO, "class", CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, "class");
2505 kobject_init(&linux_root_device.kobj, &linux_dev_ktype);
2506 kobject_set_name(&linux_root_device.kobj, "device");
2507 linux_root_device.kobj.oidp = SYSCTL_ADD_NODE(NULL,
2508 SYSCTL_CHILDREN(rootoid), OID_AUTO, "device",
2509 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "device");
2510 linux_root_device.bsddev = root_bus;
2511 linux_class_misc.name = "misc";
2512 class_register(&linux_class_misc);
2513 INIT_LIST_HEAD(&pci_drivers);
2514 INIT_LIST_HEAD(&pci_devices);
2515 spin_lock_init(&pci_lock);
2516 mtx_init(&vmmaplock, "IO Map lock", NULL, MTX_DEF);
2517 for (i = 0; i < VMMAP_HASH_SIZE; i++)
2518 LIST_INIT(&vmmaphead[i]);
2519 init_waitqueue_head(&linux_bit_waitq);
2520 init_waitqueue_head(&linux_var_waitq);
2522 SYSINIT(linux_compat, SI_SUB_DRIVERS, SI_ORDER_SECOND, linux_compat_init, NULL);
2525 linux_compat_uninit(void *arg)
2527 linux_kobject_kfree_name(&linux_class_root);
2528 linux_kobject_kfree_name(&linux_root_device.kobj);
2529 linux_kobject_kfree_name(&linux_class_misc.kobj);
2531 mtx_destroy(&vmmaplock);
2532 spin_lock_destroy(&pci_lock);
2533 rw_destroy(&linux_vma_lock);
2535 SYSUNINIT(linux_compat, SI_SUB_DRIVERS, SI_ORDER_SECOND, linux_compat_uninit, NULL);
2538 * NOTE: Linux frequently uses "unsigned long" for pointer to integer
2539 * conversion and vice versa, where in FreeBSD "uintptr_t" would be
2540 * used. Assert these types have the same size, else some parts of the
2541 * LinuxKPI may not work like expected:
2543 CTASSERT(sizeof(unsigned long) == sizeof(uintptr_t));