2 * Copyright (c) 2010 Isilon Systems, Inc.
3 * Copyright (c) 2010 iX Systems, Inc.
4 * Copyright (c) 2010 Panasas, Inc.
5 * Copyright (c) 2013-2017 Mellanox Technologies, Ltd.
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 <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/malloc.h>
36 #include <sys/kernel.h>
37 #include <sys/sysctl.h>
39 #include <sys/sglist.h>
40 #include <sys/sleepqueue.h>
42 #include <sys/mutex.h>
44 #include <sys/fcntl.h>
46 #include <sys/filio.h>
47 #include <sys/rwlock.h>
51 #include <vm/vm_object.h>
52 #include <vm/vm_page.h>
53 #include <vm/vm_pager.h>
55 #include <machine/stdarg.h>
57 #if defined(__i386__) || defined(__amd64__)
58 #include <machine/md_var.h>
61 #include <linux/kobject.h>
62 #include <linux/device.h>
63 #include <linux/slab.h>
64 #include <linux/module.h>
65 #include <linux/moduleparam.h>
66 #include <linux/cdev.h>
67 #include <linux/file.h>
68 #include <linux/sysfs.h>
71 #include <linux/vmalloc.h>
72 #include <linux/netdevice.h>
73 #include <linux/timer.h>
74 #include <linux/interrupt.h>
75 #include <linux/uaccess.h>
76 #include <linux/list.h>
77 #include <linux/kthread.h>
78 #include <linux/kernel.h>
79 #include <linux/compat.h>
80 #include <linux/poll.h>
81 #include <linux/smp.h>
83 #if defined(__i386__) || defined(__amd64__)
87 SYSCTL_NODE(_compat, OID_AUTO, linuxkpi, CTLFLAG_RW, 0, "LinuxKPI parameters");
89 MALLOC_DEFINE(M_KMALLOC, "linux", "Linux kmalloc compat");
91 #include <linux/rbtree.h>
92 /* Undo Linux compat changes. */
96 #define RB_ROOT(head) (head)->rbh_root
98 static struct vm_area_struct *linux_cdev_handle_find(void *handle);
100 struct kobject linux_class_root;
101 struct device linux_root_device;
102 struct class linux_class_misc;
103 struct list_head pci_drivers;
104 struct list_head pci_devices;
107 unsigned long linux_timer_hz_mask;
110 panic_cmp(struct rb_node *one, struct rb_node *two)
115 RB_GENERATE(linux_root, rb_node, __entry, panic_cmp);
118 kobject_set_name_vargs(struct kobject *kobj, const char *fmt, va_list args)
128 if (old && fmt == NULL)
131 /* compute length of string */
132 va_copy(tmp_va, args);
133 len = vsnprintf(&dummy, 0, fmt, tmp_va);
136 /* account for zero termination */
139 /* check for error */
143 /* allocate memory for string */
144 name = kzalloc(len, GFP_KERNEL);
147 vsnprintf(name, len, fmt, args);
150 /* free old string */
153 /* filter new string */
154 for (; *name != '\0'; name++)
161 kobject_set_name(struct kobject *kobj, const char *fmt, ...)
167 error = kobject_set_name_vargs(kobj, fmt, args);
174 kobject_add_complete(struct kobject *kobj, struct kobject *parent)
176 const struct kobj_type *t;
179 kobj->parent = parent;
180 error = sysfs_create_dir(kobj);
181 if (error == 0 && kobj->ktype && kobj->ktype->default_attrs) {
182 struct attribute **attr;
185 for (attr = t->default_attrs; *attr != NULL; attr++) {
186 error = sysfs_create_file(kobj, *attr);
191 sysfs_remove_dir(kobj);
198 kobject_add(struct kobject *kobj, struct kobject *parent, const char *fmt, ...)
204 error = kobject_set_name_vargs(kobj, fmt, args);
209 return kobject_add_complete(kobj, parent);
213 linux_kobject_release(struct kref *kref)
215 struct kobject *kobj;
218 kobj = container_of(kref, struct kobject, kref);
219 sysfs_remove_dir(kobj);
221 if (kobj->ktype && kobj->ktype->release)
222 kobj->ktype->release(kobj);
227 linux_kobject_kfree(struct kobject *kobj)
233 linux_kobject_kfree_name(struct kobject *kobj)
240 const struct kobj_type linux_kfree_type = {
241 .release = linux_kobject_kfree
245 linux_device_release(struct device *dev)
247 pr_debug("linux_device_release: %s\n", dev_name(dev));
252 linux_class_show(struct kobject *kobj, struct attribute *attr, char *buf)
254 struct class_attribute *dattr;
257 dattr = container_of(attr, struct class_attribute, attr);
260 error = dattr->show(container_of(kobj, struct class, kobj),
266 linux_class_store(struct kobject *kobj, struct attribute *attr, const char *buf,
269 struct class_attribute *dattr;
272 dattr = container_of(attr, struct class_attribute, attr);
275 error = dattr->store(container_of(kobj, struct class, kobj),
281 linux_class_release(struct kobject *kobj)
285 class = container_of(kobj, struct class, kobj);
286 if (class->class_release)
287 class->class_release(class);
290 static const struct sysfs_ops linux_class_sysfs = {
291 .show = linux_class_show,
292 .store = linux_class_store,
295 const struct kobj_type linux_class_ktype = {
296 .release = linux_class_release,
297 .sysfs_ops = &linux_class_sysfs
301 linux_dev_release(struct kobject *kobj)
305 dev = container_of(kobj, struct device, kobj);
306 /* This is the precedence defined by linux. */
309 else if (dev->class && dev->class->dev_release)
310 dev->class->dev_release(dev);
314 linux_dev_show(struct kobject *kobj, struct attribute *attr, char *buf)
316 struct device_attribute *dattr;
319 dattr = container_of(attr, struct device_attribute, attr);
322 error = dattr->show(container_of(kobj, struct device, kobj),
328 linux_dev_store(struct kobject *kobj, struct attribute *attr, const char *buf,
331 struct device_attribute *dattr;
334 dattr = container_of(attr, struct device_attribute, attr);
337 error = dattr->store(container_of(kobj, struct device, kobj),
342 static const struct sysfs_ops linux_dev_sysfs = {
343 .show = linux_dev_show,
344 .store = linux_dev_store,
347 const struct kobj_type linux_dev_ktype = {
348 .release = linux_dev_release,
349 .sysfs_ops = &linux_dev_sysfs
353 device_create(struct class *class, struct device *parent, dev_t devt,
354 void *drvdata, const char *fmt, ...)
359 dev = kzalloc(sizeof(*dev), M_WAITOK);
360 dev->parent = parent;
363 dev->driver_data = drvdata;
364 dev->release = linux_device_release;
366 kobject_set_name_vargs(&dev->kobj, fmt, args);
368 device_register(dev);
374 kobject_init_and_add(struct kobject *kobj, const struct kobj_type *ktype,
375 struct kobject *parent, const char *fmt, ...)
380 kobject_init(kobj, ktype);
382 kobj->parent = parent;
386 error = kobject_set_name_vargs(kobj, fmt, args);
390 return kobject_add_complete(kobj, parent);
394 linux_file_dtor(void *cdp)
396 struct linux_file *filp;
398 linux_set_current(curthread);
400 filp->f_op->release(filp->f_vnode, filp);
401 vdrop(filp->f_vnode);
406 linux_kq_lock(void *arg)
413 linux_kq_unlock(void *arg)
421 linux_kq_lock_owned(void *arg)
426 mtx_assert(&s->m, MA_OWNED);
431 linux_kq_lock_unowned(void *arg)
436 mtx_assert(&s->m, MA_NOTOWNED);
441 linux_dev_kqfilter_poll(struct linux_file *, int);
444 linux_file_alloc(void)
446 struct linux_file *filp;
448 filp = kzalloc(sizeof(*filp), GFP_KERNEL);
450 /* set initial refcount */
453 /* setup fields needed by kqueue support */
454 spin_lock_init(&filp->f_kqlock);
455 knlist_init(&filp->f_selinfo.si_note, &filp->f_kqlock,
456 linux_kq_lock, linux_kq_unlock,
457 linux_kq_lock_owned, linux_kq_lock_unowned);
463 linux_file_free(struct linux_file *filp)
465 if (filp->_file == NULL) {
466 if (filp->f_shmem != NULL)
467 vm_object_deallocate(filp->f_shmem);
471 * The close method of the character device or file
472 * will free the linux_file structure:
474 _fdrop(filp->_file, curthread);
479 linux_cdev_pager_fault(vm_object_t vm_obj, vm_ooffset_t offset, int prot,
482 struct vm_area_struct *vmap;
484 vmap = linux_cdev_handle_find(vm_obj->handle);
487 MPASS(vmap->vm_private_data == vm_obj->handle);
489 if (likely(vmap->vm_ops != NULL && offset < vmap->vm_len)) {
490 vm_paddr_t paddr = IDX_TO_OFF(vmap->vm_pfn) + offset;
493 if (((*mres)->flags & PG_FICTITIOUS) != 0) {
495 * If the passed in result page is a fake
496 * page, update it with the new physical
500 vm_page_updatefake(page, paddr, vm_obj->memattr);
503 * Replace the passed in "mres" page with our
504 * own fake page and free up the all of the
507 VM_OBJECT_WUNLOCK(vm_obj);
508 page = vm_page_getfake(paddr, vm_obj->memattr);
509 VM_OBJECT_WLOCK(vm_obj);
511 vm_page_replace_checked(page, vm_obj,
512 (*mres)->pindex, *mres);
516 vm_page_unlock(*mres);
519 page->valid = VM_PAGE_BITS_ALL;
520 return (VM_PAGER_OK);
522 return (VM_PAGER_FAIL);
526 linux_cdev_pager_populate(vm_object_t vm_obj, vm_pindex_t pidx, int fault_type,
527 vm_prot_t max_prot, vm_pindex_t *first, vm_pindex_t *last)
529 struct vm_area_struct *vmap;
532 linux_set_current(curthread);
534 /* get VM area structure */
535 vmap = linux_cdev_handle_find(vm_obj->handle);
537 MPASS(vmap->vm_private_data == vm_obj->handle);
539 VM_OBJECT_WUNLOCK(vm_obj);
541 down_write(&vmap->vm_mm->mmap_sem);
542 if (unlikely(vmap->vm_ops == NULL)) {
543 err = VM_FAULT_SIGBUS;
547 /* fill out VM fault structure */
548 vmf.virtual_address = (void *)((uintptr_t)pidx << PAGE_SHIFT);
549 vmf.flags = (fault_type & VM_PROT_WRITE) ? FAULT_FLAG_WRITE : 0;
553 vmap->vm_pfn_count = 0;
554 vmap->vm_pfn_pcount = &vmap->vm_pfn_count;
555 vmap->vm_obj = vm_obj;
557 err = vmap->vm_ops->fault(vmap, &vmf);
559 while (vmap->vm_pfn_count == 0 && err == VM_FAULT_NOPAGE) {
560 kern_yield(PRI_USER);
561 err = vmap->vm_ops->fault(vmap, &vmf);
565 /* translate return code */
568 err = VM_PAGER_AGAIN;
570 case VM_FAULT_SIGBUS:
573 case VM_FAULT_NOPAGE:
575 * By contract the fault handler will return having
576 * busied all the pages itself. If pidx is already
577 * found in the object, it will simply xbusy the first
578 * page and return with vm_pfn_count set to 1.
580 *first = vmap->vm_pfn_first;
581 *last = *first + vmap->vm_pfn_count - 1;
585 err = VM_PAGER_ERROR;
588 up_write(&vmap->vm_mm->mmap_sem);
589 VM_OBJECT_WLOCK(vm_obj);
593 static struct rwlock linux_vma_lock;
594 static TAILQ_HEAD(, vm_area_struct) linux_vma_head =
595 TAILQ_HEAD_INITIALIZER(linux_vma_head);
598 linux_cdev_handle_free(struct vm_area_struct *vmap)
600 /* Drop reference on vm_file */
601 if (vmap->vm_file != NULL)
604 /* Drop reference on mm_struct */
610 static struct vm_area_struct *
611 linux_cdev_handle_insert(void *handle, struct vm_area_struct *vmap)
613 struct vm_area_struct *ptr;
615 rw_wlock(&linux_vma_lock);
616 TAILQ_FOREACH(ptr, &linux_vma_head, vm_entry) {
617 if (ptr->vm_private_data == handle) {
618 rw_wunlock(&linux_vma_lock);
619 linux_cdev_handle_free(vmap);
623 TAILQ_INSERT_TAIL(&linux_vma_head, vmap, vm_entry);
624 rw_wunlock(&linux_vma_lock);
629 linux_cdev_handle_remove(struct vm_area_struct *vmap)
631 rw_wlock(&linux_vma_lock);
632 TAILQ_REMOVE(&linux_vma_head, vmap, vm_entry);
633 rw_wunlock(&linux_vma_lock);
636 static struct vm_area_struct *
637 linux_cdev_handle_find(void *handle)
639 struct vm_area_struct *vmap;
641 rw_rlock(&linux_vma_lock);
642 TAILQ_FOREACH(vmap, &linux_vma_head, vm_entry) {
643 if (vmap->vm_private_data == handle)
646 rw_runlock(&linux_vma_lock);
651 linux_cdev_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
652 vm_ooffset_t foff, struct ucred *cred, u_short *color)
655 MPASS(linux_cdev_handle_find(handle) != NULL);
661 linux_cdev_pager_dtor(void *handle)
663 const struct vm_operations_struct *vm_ops;
664 struct vm_area_struct *vmap;
666 vmap = linux_cdev_handle_find(handle);
670 * Remove handle before calling close operation to prevent
671 * other threads from reusing the handle pointer.
673 linux_cdev_handle_remove(vmap);
675 down_write(&vmap->vm_mm->mmap_sem);
676 vm_ops = vmap->vm_ops;
677 if (likely(vm_ops != NULL))
679 up_write(&vmap->vm_mm->mmap_sem);
681 linux_cdev_handle_free(vmap);
684 static struct cdev_pager_ops linux_cdev_pager_ops[2] = {
687 .cdev_pg_populate = linux_cdev_pager_populate,
688 .cdev_pg_ctor = linux_cdev_pager_ctor,
689 .cdev_pg_dtor = linux_cdev_pager_dtor
693 .cdev_pg_fault = linux_cdev_pager_fault,
694 .cdev_pg_ctor = linux_cdev_pager_ctor,
695 .cdev_pg_dtor = linux_cdev_pager_dtor
700 linux_dev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
702 struct linux_cdev *ldev;
703 struct linux_file *filp;
712 filp = linux_file_alloc();
713 filp->f_dentry = &filp->f_dentry_store;
714 filp->f_op = ldev->ops;
715 filp->f_flags = file->f_flag;
716 vhold(file->f_vnode);
717 filp->f_vnode = file->f_vnode;
720 linux_set_current(td);
722 if (filp->f_op->open) {
723 error = -filp->f_op->open(file->f_vnode, filp);
725 vdrop(filp->f_vnode);
730 error = devfs_set_cdevpriv(filp, linux_file_dtor);
732 filp->f_op->release(file->f_vnode, filp);
733 vdrop(filp->f_vnode);
741 linux_dev_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
743 struct linux_file *filp;
748 if (dev->si_drv1 == NULL)
750 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
752 filp->f_flags = file->f_flag;
753 devfs_clear_cdevpriv();
758 #define LINUX_IOCTL_MIN_PTR 0x10000UL
759 #define LINUX_IOCTL_MAX_PTR (LINUX_IOCTL_MIN_PTR + IOCPARM_MAX)
762 linux_remap_address(void **uaddr, size_t len)
764 uintptr_t uaddr_val = (uintptr_t)(*uaddr);
766 if (unlikely(uaddr_val >= LINUX_IOCTL_MIN_PTR &&
767 uaddr_val < LINUX_IOCTL_MAX_PTR)) {
768 struct task_struct *pts = current;
774 /* compute data offset */
775 uaddr_val -= LINUX_IOCTL_MIN_PTR;
777 /* check that length is within bounds */
778 if ((len > IOCPARM_MAX) ||
779 (uaddr_val + len) > pts->bsd_ioctl_len) {
784 /* re-add kernel buffer address */
785 uaddr_val += (uintptr_t)pts->bsd_ioctl_data;
787 /* update address location */
788 *uaddr = (void *)uaddr_val;
795 linux_copyin(const void *uaddr, void *kaddr, size_t len)
797 if (linux_remap_address(__DECONST(void **, &uaddr), len)) {
800 memcpy(kaddr, uaddr, len);
803 return (-copyin(uaddr, kaddr, len));
807 linux_copyout(const void *kaddr, void *uaddr, size_t len)
809 if (linux_remap_address(&uaddr, len)) {
812 memcpy(uaddr, kaddr, len);
815 return (-copyout(kaddr, uaddr, len));
819 linux_clear_user(void *_uaddr, size_t _len)
821 uint8_t *uaddr = _uaddr;
824 /* make sure uaddr is aligned before going into the fast loop */
825 while (((uintptr_t)uaddr & 7) != 0 && len > 7) {
826 if (subyte(uaddr, 0))
832 /* zero 8 bytes at a time */
835 if (suword64(uaddr, 0))
838 if (suword32(uaddr, 0))
840 if (suword32(uaddr + 4, 0))
847 /* zero fill end, if any */
849 if (subyte(uaddr, 0))
858 linux_access_ok(int rw, const void *uaddr, size_t len)
863 /* get start and end address */
864 saddr = (uintptr_t)uaddr;
865 eaddr = (uintptr_t)uaddr + len;
867 /* verify addresses are valid for userspace */
868 return ((saddr == eaddr) ||
869 (eaddr > saddr && eaddr <= VM_MAXUSER_ADDRESS));
873 linux_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
876 struct linux_file *filp;
882 if (dev->si_drv1 == NULL)
884 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
886 filp->f_flags = file->f_flag;
888 /* the LinuxKPI supports blocking and non-blocking I/O */
889 if (cmd == FIONBIO || cmd == FIOASYNC)
892 linux_set_current(td);
893 size = IOCPARM_LEN(cmd);
894 /* refer to logic in sys_ioctl() */
897 * Setup hint for linux_copyin() and linux_copyout().
899 * Background: Linux code expects a user-space address
900 * while FreeBSD supplies a kernel-space address.
902 current->bsd_ioctl_data = data;
903 current->bsd_ioctl_len = size;
904 data = (void *)LINUX_IOCTL_MIN_PTR;
906 /* fetch user-space pointer */
907 data = *(void **)data;
909 if (filp->f_op->unlocked_ioctl)
910 error = -filp->f_op->unlocked_ioctl(filp, cmd, (u_long)data);
914 current->bsd_ioctl_data = NULL;
915 current->bsd_ioctl_len = 0;
918 if (error == EWOULDBLOCK) {
919 /* update kqfilter status, if any */
920 linux_dev_kqfilter_poll(filp,
921 LINUX_KQ_FLAG_HAS_READ | LINUX_KQ_FLAG_HAS_WRITE);
922 } else if (error == ERESTARTSYS)
928 linux_dev_read(struct cdev *dev, struct uio *uio, int ioflag)
930 struct linux_file *filp;
938 if (dev->si_drv1 == NULL)
940 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
942 filp->f_flags = file->f_flag;
943 /* XXX no support for I/O vectors currently */
944 if (uio->uio_iovcnt != 1)
946 linux_set_current(td);
947 if (filp->f_op->read) {
948 bytes = filp->f_op->read(filp, uio->uio_iov->iov_base,
949 uio->uio_iov->iov_len, &uio->uio_offset);
951 uio->uio_iov->iov_base =
952 ((uint8_t *)uio->uio_iov->iov_base) + bytes;
953 uio->uio_iov->iov_len -= bytes;
954 uio->uio_resid -= bytes;
957 if (error == ERESTARTSYS)
963 /* update kqfilter status, if any */
964 linux_dev_kqfilter_poll(filp, LINUX_KQ_FLAG_HAS_READ);
970 linux_dev_write(struct cdev *dev, struct uio *uio, int ioflag)
972 struct linux_file *filp;
980 if (dev->si_drv1 == NULL)
982 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
984 filp->f_flags = file->f_flag;
985 /* XXX no support for I/O vectors currently */
986 if (uio->uio_iovcnt != 1)
988 linux_set_current(td);
989 if (filp->f_op->write) {
990 bytes = filp->f_op->write(filp, uio->uio_iov->iov_base,
991 uio->uio_iov->iov_len, &uio->uio_offset);
993 uio->uio_iov->iov_base =
994 ((uint8_t *)uio->uio_iov->iov_base) + bytes;
995 uio->uio_iov->iov_len -= bytes;
996 uio->uio_resid -= bytes;
999 if (error == ERESTARTSYS)
1005 /* update kqfilter status, if any */
1006 linux_dev_kqfilter_poll(filp, LINUX_KQ_FLAG_HAS_WRITE);
1012 linux_dev_poll(struct cdev *dev, int events, struct thread *td)
1014 struct linux_file *filp;
1018 if (dev->si_drv1 == NULL)
1020 if (devfs_get_cdevpriv((void **)&filp) != 0)
1024 filp->f_flags = file->f_flag;
1025 linux_set_current(td);
1026 if (filp->f_op->poll != NULL)
1027 revents = filp->f_op->poll(filp, NULL) & events;
1033 return (events & (POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM));
1037 * This function atomically updates the poll wakeup state and returns
1038 * the previous state at the time of update.
1041 linux_poll_wakeup_state(atomic_t *v, const uint8_t *pstate)
1047 while ((old = atomic_cmpxchg(v, c, pstate[c])) != c)
1055 linux_poll_wakeup_callback(wait_queue_t *wq, unsigned int wq_state, int flags, void *key)
1057 static const uint8_t state[LINUX_FWQ_STATE_MAX] = {
1058 [LINUX_FWQ_STATE_INIT] = LINUX_FWQ_STATE_INIT, /* NOP */
1059 [LINUX_FWQ_STATE_NOT_READY] = LINUX_FWQ_STATE_NOT_READY, /* NOP */
1060 [LINUX_FWQ_STATE_QUEUED] = LINUX_FWQ_STATE_READY,
1061 [LINUX_FWQ_STATE_READY] = LINUX_FWQ_STATE_READY, /* NOP */
1063 struct linux_file *filp = container_of(wq, struct linux_file, f_wait_queue.wq);
1065 switch (linux_poll_wakeup_state(&filp->f_wait_queue.state, state)) {
1066 case LINUX_FWQ_STATE_QUEUED:
1067 linux_poll_wakeup(filp);
1075 linux_poll_wait(struct linux_file *filp, wait_queue_head_t *wqh, poll_table *p)
1077 static const uint8_t state[LINUX_FWQ_STATE_MAX] = {
1078 [LINUX_FWQ_STATE_INIT] = LINUX_FWQ_STATE_NOT_READY,
1079 [LINUX_FWQ_STATE_NOT_READY] = LINUX_FWQ_STATE_NOT_READY, /* NOP */
1080 [LINUX_FWQ_STATE_QUEUED] = LINUX_FWQ_STATE_QUEUED, /* NOP */
1081 [LINUX_FWQ_STATE_READY] = LINUX_FWQ_STATE_QUEUED,
1084 selrecord(curthread, &filp->f_selinfo);
1086 switch (linux_poll_wakeup_state(&filp->f_wait_queue.state, state)) {
1087 case LINUX_FWQ_STATE_INIT:
1088 /* NOTE: file handles can only belong to one wait-queue */
1089 filp->f_wait_queue.wqh = wqh;
1090 filp->f_wait_queue.wq.func = &linux_poll_wakeup_callback;
1091 add_wait_queue(wqh, &filp->f_wait_queue.wq);
1092 atomic_set(&filp->f_wait_queue.state, LINUX_FWQ_STATE_QUEUED);
1100 linux_poll_wait_dequeue(struct linux_file *filp)
1102 static const uint8_t state[LINUX_FWQ_STATE_MAX] = {
1103 [LINUX_FWQ_STATE_INIT] = LINUX_FWQ_STATE_INIT, /* NOP */
1104 [LINUX_FWQ_STATE_NOT_READY] = LINUX_FWQ_STATE_INIT,
1105 [LINUX_FWQ_STATE_QUEUED] = LINUX_FWQ_STATE_INIT,
1106 [LINUX_FWQ_STATE_READY] = LINUX_FWQ_STATE_INIT,
1109 seldrain(&filp->f_selinfo);
1111 switch (linux_poll_wakeup_state(&filp->f_wait_queue.state, state)) {
1112 case LINUX_FWQ_STATE_NOT_READY:
1113 case LINUX_FWQ_STATE_QUEUED:
1114 case LINUX_FWQ_STATE_READY:
1115 remove_wait_queue(filp->f_wait_queue.wqh, &filp->f_wait_queue.wq);
1123 linux_poll_wakeup(struct linux_file *filp)
1125 /* this function should be NULL-safe */
1129 selwakeup(&filp->f_selinfo);
1131 spin_lock(&filp->f_kqlock);
1132 filp->f_kqflags |= LINUX_KQ_FLAG_NEED_READ |
1133 LINUX_KQ_FLAG_NEED_WRITE;
1135 /* make sure the "knote" gets woken up */
1136 KNOTE_LOCKED(&filp->f_selinfo.si_note, 1);
1137 spin_unlock(&filp->f_kqlock);
1141 linux_dev_kqfilter_detach(struct knote *kn)
1143 struct linux_file *filp = kn->kn_hook;
1145 spin_lock(&filp->f_kqlock);
1146 knlist_remove(&filp->f_selinfo.si_note, kn, 1);
1147 spin_unlock(&filp->f_kqlock);
1151 linux_dev_kqfilter_read_event(struct knote *kn, long hint)
1153 struct linux_file *filp = kn->kn_hook;
1155 mtx_assert(&filp->f_kqlock.m, MA_OWNED);
1157 return ((filp->f_kqflags & LINUX_KQ_FLAG_NEED_READ) ? 1 : 0);
1161 linux_dev_kqfilter_write_event(struct knote *kn, long hint)
1163 struct linux_file *filp = kn->kn_hook;
1165 mtx_assert(&filp->f_kqlock.m, MA_OWNED);
1167 return ((filp->f_kqflags & LINUX_KQ_FLAG_NEED_WRITE) ? 1 : 0);
1170 static struct filterops linux_dev_kqfiltops_read = {
1172 .f_detach = linux_dev_kqfilter_detach,
1173 .f_event = linux_dev_kqfilter_read_event,
1176 static struct filterops linux_dev_kqfiltops_write = {
1178 .f_detach = linux_dev_kqfilter_detach,
1179 .f_event = linux_dev_kqfilter_write_event,
1183 linux_dev_kqfilter_poll(struct linux_file *filp, int kqflags)
1187 if (filp->f_kqflags & kqflags) {
1188 /* get the latest polling state */
1189 temp = filp->f_op->poll(filp, NULL);
1191 spin_lock(&filp->f_kqlock);
1193 filp->f_kqflags &= ~(LINUX_KQ_FLAG_NEED_READ |
1194 LINUX_KQ_FLAG_NEED_WRITE);
1195 /* update kqflags */
1196 if (temp & (POLLIN | POLLOUT)) {
1198 filp->f_kqflags |= LINUX_KQ_FLAG_NEED_READ;
1200 filp->f_kqflags |= LINUX_KQ_FLAG_NEED_WRITE;
1202 /* make sure the "knote" gets woken up */
1203 KNOTE_LOCKED(&filp->f_selinfo.si_note, 0);
1205 spin_unlock(&filp->f_kqlock);
1210 linux_dev_kqfilter(struct cdev *dev, struct knote *kn)
1212 struct linux_file *filp;
1219 if (dev->si_drv1 == NULL)
1221 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
1223 filp->f_flags = file->f_flag;
1224 if (filp->f_op->poll == NULL)
1227 spin_lock(&filp->f_kqlock);
1228 switch (kn->kn_filter) {
1230 filp->f_kqflags |= LINUX_KQ_FLAG_HAS_READ;
1231 kn->kn_fop = &linux_dev_kqfiltops_read;
1233 knlist_add(&filp->f_selinfo.si_note, kn, 1);
1236 filp->f_kqflags |= LINUX_KQ_FLAG_HAS_WRITE;
1237 kn->kn_fop = &linux_dev_kqfiltops_write;
1239 knlist_add(&filp->f_selinfo.si_note, kn, 1);
1245 spin_unlock(&filp->f_kqlock);
1248 linux_set_current(td);
1250 /* update kqfilter status, if any */
1251 linux_dev_kqfilter_poll(filp,
1252 LINUX_KQ_FLAG_HAS_READ | LINUX_KQ_FLAG_HAS_WRITE);
1258 linux_dev_mmap_single(struct cdev *dev, vm_ooffset_t *offset,
1259 vm_size_t size, struct vm_object **object, int nprot)
1261 struct vm_area_struct *vmap;
1262 struct mm_struct *mm;
1263 struct linux_file *filp;
1271 if (dev->si_drv1 == NULL)
1273 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
1275 filp->f_flags = file->f_flag;
1277 if (filp->f_op->mmap == NULL)
1280 linux_set_current(td);
1283 * The same VM object might be shared by multiple processes
1284 * and the mm_struct is usually freed when a process exits.
1286 * The atomic reference below makes sure the mm_struct is
1287 * available as long as the vmap is in the linux_vma_head.
1290 if (atomic_inc_not_zero(&mm->mm_users) == 0)
1293 vmap = kzalloc(sizeof(*vmap), GFP_KERNEL);
1295 vmap->vm_end = size;
1296 vmap->vm_pgoff = *offset / PAGE_SIZE;
1298 vmap->vm_flags = vmap->vm_page_prot = (nprot & VM_PROT_ALL);
1299 vmap->vm_ops = NULL;
1300 vmap->vm_file = get_file(filp);
1303 if (unlikely(down_write_killable(&vmap->vm_mm->mmap_sem))) {
1306 error = -filp->f_op->mmap(filp, vmap);
1307 up_write(&vmap->vm_mm->mmap_sem);
1311 linux_cdev_handle_free(vmap);
1315 attr = pgprot2cachemode(vmap->vm_page_prot);
1317 if (vmap->vm_ops != NULL) {
1318 void *vm_private_data;
1320 if (vmap->vm_ops->open == NULL ||
1321 vmap->vm_ops->close == NULL ||
1322 vmap->vm_private_data == NULL) {
1323 linux_cdev_handle_free(vmap);
1327 vm_private_data = vmap->vm_private_data;
1329 vmap = linux_cdev_handle_insert(vm_private_data, vmap);
1331 if (vmap->vm_ops->fault == NULL) {
1332 *object = cdev_pager_allocate(vm_private_data, OBJT_DEVICE,
1333 &linux_cdev_pager_ops[1], size, nprot, *offset,
1334 curthread->td_ucred);
1336 *object = cdev_pager_allocate(vm_private_data, OBJT_MGTDEVICE,
1337 &linux_cdev_pager_ops[0], size, nprot, *offset,
1338 curthread->td_ucred);
1341 if (*object == NULL) {
1342 linux_cdev_handle_remove(vmap);
1343 linux_cdev_handle_free(vmap);
1349 sg = sglist_alloc(1, M_WAITOK);
1350 sglist_append_phys(sg,
1351 (vm_paddr_t)vmap->vm_pfn << PAGE_SHIFT, vmap->vm_len);
1353 *object = vm_pager_allocate(OBJT_SG, sg, vmap->vm_len,
1354 nprot, 0, curthread->td_ucred);
1356 linux_cdev_handle_free(vmap);
1358 if (*object == NULL) {
1364 if (attr != VM_MEMATTR_DEFAULT) {
1365 VM_OBJECT_WLOCK(*object);
1366 vm_object_set_memattr(*object, attr);
1367 VM_OBJECT_WUNLOCK(*object);
1373 struct cdevsw linuxcdevsw = {
1374 .d_version = D_VERSION,
1375 .d_flags = D_TRACKCLOSE,
1376 .d_open = linux_dev_open,
1377 .d_close = linux_dev_close,
1378 .d_read = linux_dev_read,
1379 .d_write = linux_dev_write,
1380 .d_ioctl = linux_dev_ioctl,
1381 .d_mmap_single = linux_dev_mmap_single,
1382 .d_poll = linux_dev_poll,
1383 .d_kqfilter = linux_dev_kqfilter,
1384 .d_name = "lkpidev",
1388 linux_file_read(struct file *file, struct uio *uio, struct ucred *active_cred,
1389 int flags, struct thread *td)
1391 struct linux_file *filp;
1396 filp = (struct linux_file *)file->f_data;
1397 filp->f_flags = file->f_flag;
1398 /* XXX no support for I/O vectors currently */
1399 if (uio->uio_iovcnt != 1)
1400 return (EOPNOTSUPP);
1401 linux_set_current(td);
1402 if (filp->f_op->read) {
1403 bytes = filp->f_op->read(filp, 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;
1419 linux_file_poll(struct file *file, int events, struct ucred *active_cred,
1422 struct linux_file *filp;
1425 filp = (struct linux_file *)file->f_data;
1426 filp->f_flags = file->f_flag;
1427 linux_set_current(td);
1428 if (filp->f_op->poll != NULL) {
1429 selrecord(td, &filp->f_selinfo);
1430 revents = filp->f_op->poll(filp, NULL) & events;
1438 linux_file_close(struct file *file, struct thread *td)
1440 struct linux_file *filp;
1443 filp = (struct linux_file *)file->f_data;
1444 filp->f_flags = file->f_flag;
1445 linux_set_current(td);
1446 linux_poll_wait_dequeue(filp);
1447 error = -filp->f_op->release(NULL, filp);
1448 funsetown(&filp->f_sigio);
1455 linux_file_ioctl(struct file *fp, u_long cmd, void *data, struct ucred *cred,
1458 struct linux_file *filp;
1461 filp = (struct linux_file *)fp->f_data;
1462 filp->f_flags = fp->f_flag;
1465 linux_set_current(td);
1470 if (filp->f_op->fasync == NULL)
1472 error = filp->f_op->fasync(0, filp, fp->f_flag & FASYNC);
1475 error = fsetown(*(int *)data, &filp->f_sigio);
1477 error = filp->f_op->fasync(0, filp,
1478 fp->f_flag & FASYNC);
1481 *(int *)data = fgetown(&filp->f_sigio);
1491 linux_file_stat(struct file *fp, struct stat *sb, struct ucred *active_cred,
1495 return (EOPNOTSUPP);
1499 linux_file_fill_kinfo(struct file *fp, struct kinfo_file *kif,
1500 struct filedesc *fdp)
1507 linux_iminor(struct inode *inode)
1509 struct linux_cdev *ldev;
1511 if (inode == NULL || inode->v_rdev == NULL ||
1512 inode->v_rdev->si_devsw != &linuxcdevsw)
1514 ldev = inode->v_rdev->si_drv1;
1518 return (minor(ldev->dev));
1521 struct fileops linuxfileops = {
1522 .fo_read = linux_file_read,
1523 .fo_write = invfo_rdwr,
1524 .fo_truncate = invfo_truncate,
1525 .fo_kqfilter = invfo_kqfilter,
1526 .fo_stat = linux_file_stat,
1527 .fo_fill_kinfo = linux_file_fill_kinfo,
1528 .fo_poll = linux_file_poll,
1529 .fo_close = linux_file_close,
1530 .fo_ioctl = linux_file_ioctl,
1531 .fo_chmod = invfo_chmod,
1532 .fo_chown = invfo_chown,
1533 .fo_sendfile = invfo_sendfile,
1537 * Hash of vmmap addresses. This is infrequently accessed and does not
1538 * need to be particularly large. This is done because we must store the
1539 * caller's idea of the map size to properly unmap.
1542 LIST_ENTRY(vmmap) vm_next;
1544 unsigned long vm_size;
1548 struct vmmap *lh_first;
1550 #define VMMAP_HASH_SIZE 64
1551 #define VMMAP_HASH_MASK (VMMAP_HASH_SIZE - 1)
1552 #define VM_HASH(addr) ((uintptr_t)(addr) >> PAGE_SHIFT) & VMMAP_HASH_MASK
1553 static struct vmmaphd vmmaphead[VMMAP_HASH_SIZE];
1554 static struct mtx vmmaplock;
1557 vmmap_add(void *addr, unsigned long size)
1559 struct vmmap *vmmap;
1561 vmmap = kmalloc(sizeof(*vmmap), GFP_KERNEL);
1562 mtx_lock(&vmmaplock);
1563 vmmap->vm_size = size;
1564 vmmap->vm_addr = addr;
1565 LIST_INSERT_HEAD(&vmmaphead[VM_HASH(addr)], vmmap, vm_next);
1566 mtx_unlock(&vmmaplock);
1569 static struct vmmap *
1570 vmmap_remove(void *addr)
1572 struct vmmap *vmmap;
1574 mtx_lock(&vmmaplock);
1575 LIST_FOREACH(vmmap, &vmmaphead[VM_HASH(addr)], vm_next)
1576 if (vmmap->vm_addr == addr)
1579 LIST_REMOVE(vmmap, vm_next);
1580 mtx_unlock(&vmmaplock);
1585 #if defined(__i386__) || defined(__amd64__) || defined(__powerpc__)
1587 _ioremap_attr(vm_paddr_t phys_addr, unsigned long size, int attr)
1591 addr = pmap_mapdev_attr(phys_addr, size, attr);
1594 vmmap_add(addr, size);
1603 struct vmmap *vmmap;
1605 vmmap = vmmap_remove(addr);
1608 #if defined(__i386__) || defined(__amd64__) || defined(__powerpc__)
1609 pmap_unmapdev((vm_offset_t)addr, vmmap->vm_size);
1616 vmap(struct page **pages, unsigned int count, unsigned long flags, int prot)
1621 size = count * PAGE_SIZE;
1622 off = kva_alloc(size);
1625 vmmap_add((void *)off, size);
1626 pmap_qenter(off, pages, count);
1628 return ((void *)off);
1634 struct vmmap *vmmap;
1636 vmmap = vmmap_remove(addr);
1639 pmap_qremove((vm_offset_t)addr, vmmap->vm_size / PAGE_SIZE);
1640 kva_free((vm_offset_t)addr, vmmap->vm_size);
1645 kvasprintf(gfp_t gfp, const char *fmt, va_list ap)
1652 len = vsnprintf(NULL, 0, fmt, aq);
1655 p = kmalloc(len + 1, gfp);
1657 vsnprintf(p, len + 1, fmt, ap);
1663 kasprintf(gfp_t gfp, const char *fmt, ...)
1669 p = kvasprintf(gfp, fmt, ap);
1676 linux_timer_callback_wrapper(void *context)
1678 struct timer_list *timer;
1680 linux_set_current(curthread);
1683 timer->function(timer->data);
1687 mod_timer(struct timer_list *timer, int expires)
1690 timer->expires = expires;
1691 callout_reset(&timer->timer_callout,
1692 linux_timer_jiffies_until(expires),
1693 &linux_timer_callback_wrapper, timer);
1697 add_timer(struct timer_list *timer)
1700 callout_reset(&timer->timer_callout,
1701 linux_timer_jiffies_until(timer->expires),
1702 &linux_timer_callback_wrapper, timer);
1706 add_timer_on(struct timer_list *timer, int cpu)
1709 callout_reset_on(&timer->timer_callout,
1710 linux_timer_jiffies_until(timer->expires),
1711 &linux_timer_callback_wrapper, timer, cpu);
1715 linux_timer_init(void *arg)
1719 * Compute an internal HZ value which can divide 2**32 to
1720 * avoid timer rounding problems when the tick value wraps
1723 linux_timer_hz_mask = 1;
1724 while (linux_timer_hz_mask < (unsigned long)hz)
1725 linux_timer_hz_mask *= 2;
1726 linux_timer_hz_mask--;
1728 SYSINIT(linux_timer, SI_SUB_DRIVERS, SI_ORDER_FIRST, linux_timer_init, NULL);
1731 linux_complete_common(struct completion *c, int all)
1738 wakeup_swapper = sleepq_broadcast(c, SLEEPQ_SLEEP, 0, 0);
1740 wakeup_swapper = sleepq_signal(c, SLEEPQ_SLEEP, 0, 0);
1747 * Indefinite wait for done != 0 with or without signals.
1750 linux_wait_for_common(struct completion *c, int flags)
1754 if (SCHEDULER_STOPPED())
1760 flags = SLEEPQ_INTERRUPTIBLE | SLEEPQ_SLEEP;
1762 flags = SLEEPQ_SLEEP;
1768 sleepq_add(c, NULL, "completion", flags, 0);
1769 if (flags & SLEEPQ_INTERRUPTIBLE) {
1770 if (sleepq_wait_sig(c, 0) != 0) {
1771 error = -ERESTARTSYS;
1787 * Time limited wait for done != 0 with or without signals.
1790 linux_wait_for_timeout_common(struct completion *c, int timeout, int flags)
1792 int end = jiffies + timeout;
1796 if (SCHEDULER_STOPPED())
1802 flags = SLEEPQ_INTERRUPTIBLE | SLEEPQ_SLEEP;
1804 flags = SLEEPQ_SLEEP;
1812 sleepq_add(c, NULL, "completion", flags, 0);
1813 sleepq_set_timeout(c, linux_timer_jiffies_until(end));
1814 if (flags & SLEEPQ_INTERRUPTIBLE)
1815 ret = sleepq_timedwait_sig(c, 0);
1817 ret = sleepq_timedwait(c, 0);
1819 /* check for timeout or signal */
1820 if (ret == EWOULDBLOCK)
1823 error = -ERESTARTSYS;
1833 /* return how many jiffies are left */
1834 return (ret != 0 ? error : linux_timer_jiffies_until(end));
1838 linux_try_wait_for_completion(struct completion *c)
1853 linux_completion_done(struct completion *c)
1866 linux_cdev_release(struct kobject *kobj)
1868 struct linux_cdev *cdev;
1869 struct kobject *parent;
1871 cdev = container_of(kobj, struct linux_cdev, kobj);
1872 parent = kobj->parent;
1874 destroy_dev(cdev->cdev);
1876 kobject_put(parent);
1880 linux_cdev_static_release(struct kobject *kobj)
1882 struct linux_cdev *cdev;
1883 struct kobject *parent;
1885 cdev = container_of(kobj, struct linux_cdev, kobj);
1886 parent = kobj->parent;
1888 destroy_dev(cdev->cdev);
1889 kobject_put(parent);
1892 const struct kobj_type linux_cdev_ktype = {
1893 .release = linux_cdev_release,
1896 const struct kobj_type linux_cdev_static_ktype = {
1897 .release = linux_cdev_static_release,
1901 linux_handle_ifnet_link_event(void *arg, struct ifnet *ifp, int linkstate)
1903 struct notifier_block *nb;
1906 if (linkstate == LINK_STATE_UP)
1907 nb->notifier_call(nb, NETDEV_UP, ifp);
1909 nb->notifier_call(nb, NETDEV_DOWN, ifp);
1913 linux_handle_ifnet_arrival_event(void *arg, struct ifnet *ifp)
1915 struct notifier_block *nb;
1918 nb->notifier_call(nb, NETDEV_REGISTER, ifp);
1922 linux_handle_ifnet_departure_event(void *arg, struct ifnet *ifp)
1924 struct notifier_block *nb;
1927 nb->notifier_call(nb, NETDEV_UNREGISTER, ifp);
1931 linux_handle_iflladdr_event(void *arg, struct ifnet *ifp)
1933 struct notifier_block *nb;
1936 nb->notifier_call(nb, NETDEV_CHANGEADDR, ifp);
1940 linux_handle_ifaddr_event(void *arg, struct ifnet *ifp)
1942 struct notifier_block *nb;
1945 nb->notifier_call(nb, NETDEV_CHANGEIFADDR, ifp);
1949 register_netdevice_notifier(struct notifier_block *nb)
1952 nb->tags[NETDEV_UP] = EVENTHANDLER_REGISTER(
1953 ifnet_link_event, linux_handle_ifnet_link_event, nb, 0);
1954 nb->tags[NETDEV_REGISTER] = EVENTHANDLER_REGISTER(
1955 ifnet_arrival_event, linux_handle_ifnet_arrival_event, nb, 0);
1956 nb->tags[NETDEV_UNREGISTER] = EVENTHANDLER_REGISTER(
1957 ifnet_departure_event, linux_handle_ifnet_departure_event, nb, 0);
1958 nb->tags[NETDEV_CHANGEADDR] = EVENTHANDLER_REGISTER(
1959 iflladdr_event, linux_handle_iflladdr_event, nb, 0);
1965 register_inetaddr_notifier(struct notifier_block *nb)
1968 nb->tags[NETDEV_CHANGEIFADDR] = EVENTHANDLER_REGISTER(
1969 ifaddr_event, linux_handle_ifaddr_event, nb, 0);
1974 unregister_netdevice_notifier(struct notifier_block *nb)
1977 EVENTHANDLER_DEREGISTER(ifnet_link_event,
1978 nb->tags[NETDEV_UP]);
1979 EVENTHANDLER_DEREGISTER(ifnet_arrival_event,
1980 nb->tags[NETDEV_REGISTER]);
1981 EVENTHANDLER_DEREGISTER(ifnet_departure_event,
1982 nb->tags[NETDEV_UNREGISTER]);
1983 EVENTHANDLER_DEREGISTER(iflladdr_event,
1984 nb->tags[NETDEV_CHANGEADDR]);
1990 unregister_inetaddr_notifier(struct notifier_block *nb)
1993 EVENTHANDLER_DEREGISTER(ifaddr_event,
1994 nb->tags[NETDEV_CHANGEIFADDR]);
1999 struct list_sort_thunk {
2000 int (*cmp)(void *, struct list_head *, struct list_head *);
2005 linux_le_cmp(void *priv, const void *d1, const void *d2)
2007 struct list_head *le1, *le2;
2008 struct list_sort_thunk *thunk;
2011 le1 = *(__DECONST(struct list_head **, d1));
2012 le2 = *(__DECONST(struct list_head **, d2));
2013 return ((thunk->cmp)(thunk->priv, le1, le2));
2017 list_sort(void *priv, struct list_head *head, int (*cmp)(void *priv,
2018 struct list_head *a, struct list_head *b))
2020 struct list_sort_thunk thunk;
2021 struct list_head **ar, *le;
2025 list_for_each(le, head)
2027 ar = malloc(sizeof(struct list_head *) * count, M_KMALLOC, M_WAITOK);
2029 list_for_each(le, head)
2033 qsort_r(ar, count, sizeof(struct list_head *), &thunk, linux_le_cmp);
2034 INIT_LIST_HEAD(head);
2035 for (i = 0; i < count; i++)
2036 list_add_tail(ar[i], head);
2037 free(ar, M_KMALLOC);
2041 linux_irq_handler(void *ent)
2043 struct irq_ent *irqe;
2045 linux_set_current(curthread);
2048 irqe->handler(irqe->irq, irqe->arg);
2051 #if defined(__i386__) || defined(__amd64__)
2053 linux_wbinvd_on_all_cpus(void)
2056 pmap_invalidate_cache();
2062 linux_on_each_cpu(void callback(void *), void *data)
2065 smp_rendezvous(smp_no_rendezvous_barrier, callback,
2066 smp_no_rendezvous_barrier, data);
2071 linux_in_atomic(void)
2074 return ((curthread->td_pflags & TDP_NOFAULTING) != 0);
2078 linux_find_cdev(const char *name, unsigned major, unsigned minor)
2080 dev_t dev = MKDEV(major, minor);
2084 LIST_FOREACH(cdev, &linuxcdevsw.d_devs, si_list) {
2085 struct linux_cdev *ldev = cdev->si_drv1;
2086 if (ldev->dev == dev &&
2087 strcmp(kobject_name(&ldev->kobj), name) == 0) {
2093 return (cdev != NULL ? cdev->si_drv1 : NULL);
2097 __register_chrdev(unsigned int major, unsigned int baseminor,
2098 unsigned int count, const char *name,
2099 const struct file_operations *fops)
2101 struct linux_cdev *cdev;
2105 for (i = baseminor; i < baseminor + count; i++) {
2106 cdev = cdev_alloc();
2107 cdev_init(cdev, fops);
2108 kobject_set_name(&cdev->kobj, name);
2110 ret = cdev_add(cdev, makedev(major, i), 1);
2118 __register_chrdev_p(unsigned int major, unsigned int baseminor,
2119 unsigned int count, const char *name,
2120 const struct file_operations *fops, uid_t uid,
2121 gid_t gid, int mode)
2123 struct linux_cdev *cdev;
2127 for (i = baseminor; i < baseminor + count; i++) {
2128 cdev = cdev_alloc();
2129 cdev_init(cdev, fops);
2130 kobject_set_name(&cdev->kobj, name);
2132 ret = cdev_add_ext(cdev, makedev(major, i), uid, gid, mode);
2140 __unregister_chrdev(unsigned int major, unsigned int baseminor,
2141 unsigned int count, const char *name)
2143 struct linux_cdev *cdevp;
2146 for (i = baseminor; i < baseminor + count; i++) {
2147 cdevp = linux_find_cdev(name, major, i);
2153 #if defined(__i386__) || defined(__amd64__)
2154 bool linux_cpu_has_clflush;
2158 linux_compat_init(void *arg)
2160 struct sysctl_oid *rootoid;
2163 #if defined(__i386__) || defined(__amd64__)
2164 linux_cpu_has_clflush = (cpu_feature & CPUID_CLFSH);
2166 rw_init(&linux_vma_lock, "lkpi-vma-lock");
2168 rootoid = SYSCTL_ADD_ROOT_NODE(NULL,
2169 OID_AUTO, "sys", CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, "sys");
2170 kobject_init(&linux_class_root, &linux_class_ktype);
2171 kobject_set_name(&linux_class_root, "class");
2172 linux_class_root.oidp = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(rootoid),
2173 OID_AUTO, "class", CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, "class");
2174 kobject_init(&linux_root_device.kobj, &linux_dev_ktype);
2175 kobject_set_name(&linux_root_device.kobj, "device");
2176 linux_root_device.kobj.oidp = SYSCTL_ADD_NODE(NULL,
2177 SYSCTL_CHILDREN(rootoid), OID_AUTO, "device", CTLFLAG_RD, NULL,
2179 linux_root_device.bsddev = root_bus;
2180 linux_class_misc.name = "misc";
2181 class_register(&linux_class_misc);
2182 INIT_LIST_HEAD(&pci_drivers);
2183 INIT_LIST_HEAD(&pci_devices);
2184 spin_lock_init(&pci_lock);
2185 mtx_init(&vmmaplock, "IO Map lock", NULL, MTX_DEF);
2186 for (i = 0; i < VMMAP_HASH_SIZE; i++)
2187 LIST_INIT(&vmmaphead[i]);
2189 SYSINIT(linux_compat, SI_SUB_DRIVERS, SI_ORDER_SECOND, linux_compat_init, NULL);
2192 linux_compat_uninit(void *arg)
2194 linux_kobject_kfree_name(&linux_class_root);
2195 linux_kobject_kfree_name(&linux_root_device.kobj);
2196 linux_kobject_kfree_name(&linux_class_misc.kobj);
2198 mtx_destroy(&vmmaplock);
2199 spin_lock_destroy(&pci_lock);
2200 rw_destroy(&linux_vma_lock);
2202 SYSUNINIT(linux_compat, SI_SUB_DRIVERS, SI_ORDER_SECOND, linux_compat_uninit, NULL);
2205 * NOTE: Linux frequently uses "unsigned long" for pointer to integer
2206 * conversion and vice versa, where in FreeBSD "uintptr_t" would be
2207 * used. Assert these types have the same size, else some parts of the
2208 * LinuxKPI may not work like expected:
2210 CTASSERT(sizeof(unsigned long) == sizeof(uintptr_t));