2 * Copyright (c) 2010 Isilon Systems, Inc.
3 * Copyright (c) 2010 iX Systems, Inc.
4 * Copyright (c) 2010 Panasas, Inc.
5 * Copyright (c) 2013-2018 Mellanox Technologies, Ltd.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice unmodified, this list of conditions, and the following
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include "opt_stack.h"
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/malloc.h>
38 #include <sys/kernel.h>
39 #include <sys/sysctl.h>
41 #include <sys/sglist.h>
42 #include <sys/sleepqueue.h>
44 #include <sys/mutex.h>
46 #include <sys/fcntl.h>
48 #include <sys/filio.h>
49 #include <sys/rwlock.h>
51 #include <sys/stack.h>
56 #include <vm/vm_object.h>
57 #include <vm/vm_page.h>
58 #include <vm/vm_pager.h>
60 #include <machine/stdarg.h>
62 #if defined(__i386__) || defined(__amd64__)
63 #include <machine/md_var.h>
66 #include <linux/kobject.h>
67 #include <linux/device.h>
68 #include <linux/slab.h>
69 #include <linux/module.h>
70 #include <linux/moduleparam.h>
71 #include <linux/cdev.h>
72 #include <linux/file.h>
73 #include <linux/sysfs.h>
76 #include <linux/vmalloc.h>
77 #include <linux/netdevice.h>
78 #include <linux/timer.h>
79 #include <linux/interrupt.h>
80 #include <linux/uaccess.h>
81 #include <linux/list.h>
82 #include <linux/kthread.h>
83 #include <linux/kernel.h>
84 #include <linux/compat.h>
85 #include <linux/poll.h>
86 #include <linux/smp.h>
88 #if defined(__i386__) || defined(__amd64__)
92 SYSCTL_NODE(_compat, OID_AUTO, linuxkpi, CTLFLAG_RW, 0, "LinuxKPI parameters");
94 MALLOC_DEFINE(M_KMALLOC, "linux", "Linux kmalloc compat");
96 #include <linux/rbtree.h>
97 /* Undo Linux compat changes. */
101 #define RB_ROOT(head) (head)->rbh_root
103 static struct vm_area_struct *linux_cdev_handle_find(void *handle);
105 struct kobject linux_class_root;
106 struct device linux_root_device;
107 struct class linux_class_misc;
108 struct list_head pci_drivers;
109 struct list_head pci_devices;
112 unsigned long linux_timer_hz_mask;
115 panic_cmp(struct rb_node *one, struct rb_node *two)
120 RB_GENERATE(linux_root, rb_node, __entry, panic_cmp);
123 kobject_set_name_vargs(struct kobject *kobj, const char *fmt, va_list args)
133 if (old && fmt == NULL)
136 /* compute length of string */
137 va_copy(tmp_va, args);
138 len = vsnprintf(&dummy, 0, fmt, tmp_va);
141 /* account for zero termination */
144 /* check for error */
148 /* allocate memory for string */
149 name = kzalloc(len, GFP_KERNEL);
152 vsnprintf(name, len, fmt, args);
155 /* free old string */
158 /* filter new string */
159 for (; *name != '\0'; name++)
166 kobject_set_name(struct kobject *kobj, const char *fmt, ...)
172 error = kobject_set_name_vargs(kobj, fmt, args);
179 kobject_add_complete(struct kobject *kobj, struct kobject *parent)
181 const struct kobj_type *t;
184 kobj->parent = parent;
185 error = sysfs_create_dir(kobj);
186 if (error == 0 && kobj->ktype && kobj->ktype->default_attrs) {
187 struct attribute **attr;
190 for (attr = t->default_attrs; *attr != NULL; attr++) {
191 error = sysfs_create_file(kobj, *attr);
196 sysfs_remove_dir(kobj);
203 kobject_add(struct kobject *kobj, struct kobject *parent, const char *fmt, ...)
209 error = kobject_set_name_vargs(kobj, fmt, args);
214 return kobject_add_complete(kobj, parent);
218 linux_kobject_release(struct kref *kref)
220 struct kobject *kobj;
223 kobj = container_of(kref, struct kobject, kref);
224 sysfs_remove_dir(kobj);
226 if (kobj->ktype && kobj->ktype->release)
227 kobj->ktype->release(kobj);
232 linux_kobject_kfree(struct kobject *kobj)
238 linux_kobject_kfree_name(struct kobject *kobj)
245 const struct kobj_type linux_kfree_type = {
246 .release = linux_kobject_kfree
250 linux_device_release(struct device *dev)
252 pr_debug("linux_device_release: %s\n", dev_name(dev));
257 linux_class_show(struct kobject *kobj, struct attribute *attr, char *buf)
259 struct class_attribute *dattr;
262 dattr = container_of(attr, struct class_attribute, attr);
265 error = dattr->show(container_of(kobj, struct class, kobj),
271 linux_class_store(struct kobject *kobj, struct attribute *attr, const char *buf,
274 struct class_attribute *dattr;
277 dattr = container_of(attr, struct class_attribute, attr);
280 error = dattr->store(container_of(kobj, struct class, kobj),
286 linux_class_release(struct kobject *kobj)
290 class = container_of(kobj, struct class, kobj);
291 if (class->class_release)
292 class->class_release(class);
295 static const struct sysfs_ops linux_class_sysfs = {
296 .show = linux_class_show,
297 .store = linux_class_store,
300 const struct kobj_type linux_class_ktype = {
301 .release = linux_class_release,
302 .sysfs_ops = &linux_class_sysfs
306 linux_dev_release(struct kobject *kobj)
310 dev = container_of(kobj, struct device, kobj);
311 /* This is the precedence defined by linux. */
314 else if (dev->class && dev->class->dev_release)
315 dev->class->dev_release(dev);
319 linux_dev_show(struct kobject *kobj, struct attribute *attr, char *buf)
321 struct device_attribute *dattr;
324 dattr = container_of(attr, struct device_attribute, attr);
327 error = dattr->show(container_of(kobj, struct device, kobj),
333 linux_dev_store(struct kobject *kobj, struct attribute *attr, const char *buf,
336 struct device_attribute *dattr;
339 dattr = container_of(attr, struct device_attribute, attr);
342 error = dattr->store(container_of(kobj, struct device, kobj),
347 static const struct sysfs_ops linux_dev_sysfs = {
348 .show = linux_dev_show,
349 .store = linux_dev_store,
352 const struct kobj_type linux_dev_ktype = {
353 .release = linux_dev_release,
354 .sysfs_ops = &linux_dev_sysfs
358 device_create(struct class *class, struct device *parent, dev_t devt,
359 void *drvdata, const char *fmt, ...)
364 dev = kzalloc(sizeof(*dev), M_WAITOK);
365 dev->parent = parent;
368 dev->driver_data = drvdata;
369 dev->release = linux_device_release;
371 kobject_set_name_vargs(&dev->kobj, fmt, args);
373 device_register(dev);
379 kobject_init_and_add(struct kobject *kobj, const struct kobj_type *ktype,
380 struct kobject *parent, const char *fmt, ...)
385 kobject_init(kobj, ktype);
387 kobj->parent = parent;
391 error = kobject_set_name_vargs(kobj, fmt, args);
395 return kobject_add_complete(kobj, parent);
399 linux_kq_lock(void *arg)
406 linux_kq_unlock(void *arg)
414 linux_kq_lock_owned(void *arg)
419 mtx_assert(&s->m, MA_OWNED);
424 linux_kq_lock_unowned(void *arg)
429 mtx_assert(&s->m, MA_NOTOWNED);
434 linux_file_kqfilter_poll(struct linux_file *, int);
437 linux_file_alloc(void)
439 struct linux_file *filp;
441 filp = kzalloc(sizeof(*filp), GFP_KERNEL);
443 /* set initial refcount */
446 /* setup fields needed by kqueue support */
447 spin_lock_init(&filp->f_kqlock);
448 knlist_init(&filp->f_selinfo.si_note, &filp->f_kqlock,
449 linux_kq_lock, linux_kq_unlock,
450 linux_kq_lock_owned, linux_kq_lock_unowned);
456 linux_file_free(struct linux_file *filp)
458 if (filp->_file == NULL) {
459 if (filp->f_shmem != NULL)
460 vm_object_deallocate(filp->f_shmem);
464 * The close method of the character device or file
465 * will free the linux_file structure:
467 _fdrop(filp->_file, curthread);
472 linux_cdev_pager_fault(vm_object_t vm_obj, vm_ooffset_t offset, int prot,
475 struct vm_area_struct *vmap;
477 vmap = linux_cdev_handle_find(vm_obj->handle);
480 MPASS(vmap->vm_private_data == vm_obj->handle);
482 if (likely(vmap->vm_ops != NULL && offset < vmap->vm_len)) {
483 vm_paddr_t paddr = IDX_TO_OFF(vmap->vm_pfn) + offset;
486 if (((*mres)->flags & PG_FICTITIOUS) != 0) {
488 * If the passed in result page is a fake
489 * page, update it with the new physical
493 vm_page_updatefake(page, paddr, vm_obj->memattr);
496 * Replace the passed in "mres" page with our
497 * own fake page and free up the all of the
500 VM_OBJECT_WUNLOCK(vm_obj);
501 page = vm_page_getfake(paddr, vm_obj->memattr);
502 VM_OBJECT_WLOCK(vm_obj);
504 vm_page_replace_checked(page, vm_obj,
505 (*mres)->pindex, *mres);
509 vm_page_unlock(*mres);
512 page->valid = VM_PAGE_BITS_ALL;
513 return (VM_PAGER_OK);
515 return (VM_PAGER_FAIL);
519 linux_cdev_pager_populate(vm_object_t vm_obj, vm_pindex_t pidx, int fault_type,
520 vm_prot_t max_prot, vm_pindex_t *first, vm_pindex_t *last)
522 struct vm_area_struct *vmap;
525 linux_set_current(curthread);
527 /* get VM area structure */
528 vmap = linux_cdev_handle_find(vm_obj->handle);
530 MPASS(vmap->vm_private_data == vm_obj->handle);
532 VM_OBJECT_WUNLOCK(vm_obj);
534 down_write(&vmap->vm_mm->mmap_sem);
535 if (unlikely(vmap->vm_ops == NULL)) {
536 err = VM_FAULT_SIGBUS;
540 /* fill out VM fault structure */
541 vmf.virtual_address = (void *)(uintptr_t)IDX_TO_OFF(pidx);
542 vmf.flags = (fault_type & VM_PROT_WRITE) ? FAULT_FLAG_WRITE : 0;
547 vmap->vm_pfn_count = 0;
548 vmap->vm_pfn_pcount = &vmap->vm_pfn_count;
549 vmap->vm_obj = vm_obj;
551 err = vmap->vm_ops->fault(vmap, &vmf);
553 while (vmap->vm_pfn_count == 0 && err == VM_FAULT_NOPAGE) {
554 kern_yield(PRI_USER);
555 err = vmap->vm_ops->fault(vmap, &vmf);
559 /* translate return code */
562 err = VM_PAGER_AGAIN;
564 case VM_FAULT_SIGBUS:
567 case VM_FAULT_NOPAGE:
569 * By contract the fault handler will return having
570 * busied all the pages itself. If pidx is already
571 * found in the object, it will simply xbusy the first
572 * page and return with vm_pfn_count set to 1.
574 *first = vmap->vm_pfn_first;
575 *last = *first + vmap->vm_pfn_count - 1;
579 err = VM_PAGER_ERROR;
582 up_write(&vmap->vm_mm->mmap_sem);
583 VM_OBJECT_WLOCK(vm_obj);
587 static struct rwlock linux_vma_lock;
588 static TAILQ_HEAD(, vm_area_struct) linux_vma_head =
589 TAILQ_HEAD_INITIALIZER(linux_vma_head);
592 linux_cdev_handle_free(struct vm_area_struct *vmap)
594 /* Drop reference on vm_file */
595 if (vmap->vm_file != NULL)
598 /* Drop reference on mm_struct */
605 linux_cdev_handle_remove(struct vm_area_struct *vmap)
607 rw_wlock(&linux_vma_lock);
608 TAILQ_REMOVE(&linux_vma_head, vmap, vm_entry);
609 rw_wunlock(&linux_vma_lock);
612 static struct vm_area_struct *
613 linux_cdev_handle_find(void *handle)
615 struct vm_area_struct *vmap;
617 rw_rlock(&linux_vma_lock);
618 TAILQ_FOREACH(vmap, &linux_vma_head, vm_entry) {
619 if (vmap->vm_private_data == handle)
622 rw_runlock(&linux_vma_lock);
627 linux_cdev_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
628 vm_ooffset_t foff, struct ucred *cred, u_short *color)
631 MPASS(linux_cdev_handle_find(handle) != NULL);
637 linux_cdev_pager_dtor(void *handle)
639 const struct vm_operations_struct *vm_ops;
640 struct vm_area_struct *vmap;
642 vmap = linux_cdev_handle_find(handle);
646 * Remove handle before calling close operation to prevent
647 * other threads from reusing the handle pointer.
649 linux_cdev_handle_remove(vmap);
651 down_write(&vmap->vm_mm->mmap_sem);
652 vm_ops = vmap->vm_ops;
653 if (likely(vm_ops != NULL))
655 up_write(&vmap->vm_mm->mmap_sem);
657 linux_cdev_handle_free(vmap);
660 static struct cdev_pager_ops linux_cdev_pager_ops[2] = {
663 .cdev_pg_populate = linux_cdev_pager_populate,
664 .cdev_pg_ctor = linux_cdev_pager_ctor,
665 .cdev_pg_dtor = linux_cdev_pager_dtor
669 .cdev_pg_fault = linux_cdev_pager_fault,
670 .cdev_pg_ctor = linux_cdev_pager_ctor,
671 .cdev_pg_dtor = linux_cdev_pager_dtor
675 #define OPW(fp,td,code) ({ \
676 struct file *__fpop; \
677 __typeof(code) __retval; \
679 __fpop = (td)->td_fpop; \
680 (td)->td_fpop = (fp); \
682 (td)->td_fpop = __fpop; \
687 linux_dev_fdopen(struct cdev *dev, int fflags, struct thread *td, struct file *file)
689 struct linux_cdev *ldev;
690 struct linux_file *filp;
695 filp = linux_file_alloc();
696 filp->f_dentry = &filp->f_dentry_store;
697 filp->f_op = ldev->ops;
698 filp->f_mode = file->f_flag;
699 filp->f_flags = file->f_flag;
700 filp->f_vnode = file->f_vnode;
704 linux_set_current(td);
706 /* get a reference on the Linux character device */
707 if (atomic_long_add_unless(&ldev->refs, 1, -1L) == 0) {
712 if (filp->f_op->open) {
713 error = -filp->f_op->open(file->f_vnode, filp);
715 atomic_long_dec(&ldev->refs);
721 /* hold on to the vnode - used for fstat() */
722 vhold(filp->f_vnode);
724 /* release the file from devfs */
725 finit(file, filp->f_mode, DTYPE_DEV, filp, &linuxfileops);
729 #define LINUX_IOCTL_MIN_PTR 0x10000UL
730 #define LINUX_IOCTL_MAX_PTR (LINUX_IOCTL_MIN_PTR + IOCPARM_MAX)
733 linux_remap_address(void **uaddr, size_t len)
735 uintptr_t uaddr_val = (uintptr_t)(*uaddr);
737 if (unlikely(uaddr_val >= LINUX_IOCTL_MIN_PTR &&
738 uaddr_val < LINUX_IOCTL_MAX_PTR)) {
739 struct task_struct *pts = current;
745 /* compute data offset */
746 uaddr_val -= LINUX_IOCTL_MIN_PTR;
748 /* check that length is within bounds */
749 if ((len > IOCPARM_MAX) ||
750 (uaddr_val + len) > pts->bsd_ioctl_len) {
755 /* re-add kernel buffer address */
756 uaddr_val += (uintptr_t)pts->bsd_ioctl_data;
758 /* update address location */
759 *uaddr = (void *)uaddr_val;
766 linux_copyin(const void *uaddr, void *kaddr, size_t len)
768 if (linux_remap_address(__DECONST(void **, &uaddr), len)) {
771 memcpy(kaddr, uaddr, len);
774 return (-copyin(uaddr, kaddr, len));
778 linux_copyout(const void *kaddr, void *uaddr, size_t len)
780 if (linux_remap_address(&uaddr, len)) {
783 memcpy(uaddr, kaddr, len);
786 return (-copyout(kaddr, uaddr, len));
790 linux_clear_user(void *_uaddr, size_t _len)
792 uint8_t *uaddr = _uaddr;
795 /* make sure uaddr is aligned before going into the fast loop */
796 while (((uintptr_t)uaddr & 7) != 0 && len > 7) {
797 if (subyte(uaddr, 0))
803 /* zero 8 bytes at a time */
806 if (suword64(uaddr, 0))
809 if (suword32(uaddr, 0))
811 if (suword32(uaddr + 4, 0))
818 /* zero fill end, if any */
820 if (subyte(uaddr, 0))
829 linux_access_ok(int rw, const void *uaddr, size_t len)
834 /* get start and end address */
835 saddr = (uintptr_t)uaddr;
836 eaddr = (uintptr_t)uaddr + len;
838 /* verify addresses are valid for userspace */
839 return ((saddr == eaddr) ||
840 (eaddr > saddr && eaddr <= VM_MAXUSER_ADDRESS));
844 * This function should return either EINTR or ERESTART depending on
845 * the signal type sent to this thread:
848 linux_get_error(struct task_struct *task, int error)
850 /* check for signal type interrupt code */
851 if (error == EINTR || error == ERESTARTSYS || error == ERESTART) {
852 error = -linux_schedule_get_interrupt_value(task);
860 linux_file_ioctl_sub(struct file *fp, struct linux_file *filp,
861 u_long cmd, caddr_t data, struct thread *td)
863 struct task_struct *task = current;
867 size = IOCPARM_LEN(cmd);
868 /* refer to logic in sys_ioctl() */
871 * Setup hint for linux_copyin() and linux_copyout().
873 * Background: Linux code expects a user-space address
874 * while FreeBSD supplies a kernel-space address.
876 task->bsd_ioctl_data = data;
877 task->bsd_ioctl_len = size;
878 data = (void *)LINUX_IOCTL_MIN_PTR;
880 /* fetch user-space pointer */
881 data = *(void **)data;
883 #if defined(__amd64__)
884 if (td->td_proc->p_elf_machine == EM_386) {
885 /* try the compat IOCTL handler first */
886 if (filp->f_op->compat_ioctl != NULL)
887 error = -OPW(fp, td, filp->f_op->compat_ioctl(filp, cmd, (u_long)data));
891 /* fallback to the regular IOCTL handler, if any */
892 if (error == ENOTTY && filp->f_op->unlocked_ioctl != NULL)
893 error = -OPW(fp, td, filp->f_op->unlocked_ioctl(filp, cmd, (u_long)data));
896 if (filp->f_op->unlocked_ioctl != NULL)
897 error = -OPW(fp, td, filp->f_op->unlocked_ioctl(filp, cmd, (u_long)data));
901 task->bsd_ioctl_data = NULL;
902 task->bsd_ioctl_len = 0;
905 if (error == EWOULDBLOCK) {
906 /* update kqfilter status, if any */
907 linux_file_kqfilter_poll(filp,
908 LINUX_KQ_FLAG_HAS_READ | LINUX_KQ_FLAG_HAS_WRITE);
910 error = linux_get_error(task, error);
915 #define LINUX_POLL_TABLE_NORMAL ((poll_table *)1)
918 * This function atomically updates the poll wakeup state and returns
919 * the previous state at the time of update.
922 linux_poll_wakeup_state(atomic_t *v, const uint8_t *pstate)
928 while ((old = atomic_cmpxchg(v, c, pstate[c])) != c)
936 linux_poll_wakeup_callback(wait_queue_t *wq, unsigned int wq_state, int flags, void *key)
938 static const uint8_t state[LINUX_FWQ_STATE_MAX] = {
939 [LINUX_FWQ_STATE_INIT] = LINUX_FWQ_STATE_INIT, /* NOP */
940 [LINUX_FWQ_STATE_NOT_READY] = LINUX_FWQ_STATE_NOT_READY, /* NOP */
941 [LINUX_FWQ_STATE_QUEUED] = LINUX_FWQ_STATE_READY,
942 [LINUX_FWQ_STATE_READY] = LINUX_FWQ_STATE_READY, /* NOP */
944 struct linux_file *filp = container_of(wq, struct linux_file, f_wait_queue.wq);
946 switch (linux_poll_wakeup_state(&filp->f_wait_queue.state, state)) {
947 case LINUX_FWQ_STATE_QUEUED:
948 linux_poll_wakeup(filp);
956 linux_poll_wait(struct linux_file *filp, wait_queue_head_t *wqh, poll_table *p)
958 static const uint8_t state[LINUX_FWQ_STATE_MAX] = {
959 [LINUX_FWQ_STATE_INIT] = LINUX_FWQ_STATE_NOT_READY,
960 [LINUX_FWQ_STATE_NOT_READY] = LINUX_FWQ_STATE_NOT_READY, /* NOP */
961 [LINUX_FWQ_STATE_QUEUED] = LINUX_FWQ_STATE_QUEUED, /* NOP */
962 [LINUX_FWQ_STATE_READY] = LINUX_FWQ_STATE_QUEUED,
965 /* check if we are called inside the select system call */
966 if (p == LINUX_POLL_TABLE_NORMAL)
967 selrecord(curthread, &filp->f_selinfo);
969 switch (linux_poll_wakeup_state(&filp->f_wait_queue.state, state)) {
970 case LINUX_FWQ_STATE_INIT:
971 /* NOTE: file handles can only belong to one wait-queue */
972 filp->f_wait_queue.wqh = wqh;
973 filp->f_wait_queue.wq.func = &linux_poll_wakeup_callback;
974 add_wait_queue(wqh, &filp->f_wait_queue.wq);
975 atomic_set(&filp->f_wait_queue.state, LINUX_FWQ_STATE_QUEUED);
983 linux_poll_wait_dequeue(struct linux_file *filp)
985 static const uint8_t state[LINUX_FWQ_STATE_MAX] = {
986 [LINUX_FWQ_STATE_INIT] = LINUX_FWQ_STATE_INIT, /* NOP */
987 [LINUX_FWQ_STATE_NOT_READY] = LINUX_FWQ_STATE_INIT,
988 [LINUX_FWQ_STATE_QUEUED] = LINUX_FWQ_STATE_INIT,
989 [LINUX_FWQ_STATE_READY] = LINUX_FWQ_STATE_INIT,
992 seldrain(&filp->f_selinfo);
994 switch (linux_poll_wakeup_state(&filp->f_wait_queue.state, state)) {
995 case LINUX_FWQ_STATE_NOT_READY:
996 case LINUX_FWQ_STATE_QUEUED:
997 case LINUX_FWQ_STATE_READY:
998 remove_wait_queue(filp->f_wait_queue.wqh, &filp->f_wait_queue.wq);
1006 linux_poll_wakeup(struct linux_file *filp)
1008 /* this function should be NULL-safe */
1012 selwakeup(&filp->f_selinfo);
1014 spin_lock(&filp->f_kqlock);
1015 filp->f_kqflags |= LINUX_KQ_FLAG_NEED_READ |
1016 LINUX_KQ_FLAG_NEED_WRITE;
1018 /* make sure the "knote" gets woken up */
1019 KNOTE_LOCKED(&filp->f_selinfo.si_note, 1);
1020 spin_unlock(&filp->f_kqlock);
1024 linux_file_kqfilter_detach(struct knote *kn)
1026 struct linux_file *filp = kn->kn_hook;
1028 spin_lock(&filp->f_kqlock);
1029 knlist_remove(&filp->f_selinfo.si_note, kn, 1);
1030 spin_unlock(&filp->f_kqlock);
1034 linux_file_kqfilter_read_event(struct knote *kn, long hint)
1036 struct linux_file *filp = kn->kn_hook;
1038 mtx_assert(&filp->f_kqlock.m, MA_OWNED);
1040 return ((filp->f_kqflags & LINUX_KQ_FLAG_NEED_READ) ? 1 : 0);
1044 linux_file_kqfilter_write_event(struct knote *kn, long hint)
1046 struct linux_file *filp = kn->kn_hook;
1048 mtx_assert(&filp->f_kqlock.m, MA_OWNED);
1050 return ((filp->f_kqflags & LINUX_KQ_FLAG_NEED_WRITE) ? 1 : 0);
1053 static struct filterops linux_dev_kqfiltops_read = {
1055 .f_detach = linux_file_kqfilter_detach,
1056 .f_event = linux_file_kqfilter_read_event,
1059 static struct filterops linux_dev_kqfiltops_write = {
1061 .f_detach = linux_file_kqfilter_detach,
1062 .f_event = linux_file_kqfilter_write_event,
1066 linux_file_kqfilter_poll(struct linux_file *filp, int kqflags)
1070 if (filp->f_kqflags & kqflags) {
1071 struct thread *td = curthread;
1073 /* get the latest polling state */
1074 temp = OPW(filp->_file, td, filp->f_op->poll(filp, NULL));
1076 spin_lock(&filp->f_kqlock);
1078 filp->f_kqflags &= ~(LINUX_KQ_FLAG_NEED_READ |
1079 LINUX_KQ_FLAG_NEED_WRITE);
1080 /* update kqflags */
1081 if (temp & (POLLIN | POLLOUT)) {
1083 filp->f_kqflags |= LINUX_KQ_FLAG_NEED_READ;
1085 filp->f_kqflags |= LINUX_KQ_FLAG_NEED_WRITE;
1087 /* make sure the "knote" gets woken up */
1088 KNOTE_LOCKED(&filp->f_selinfo.si_note, 0);
1090 spin_unlock(&filp->f_kqlock);
1095 linux_file_kqfilter(struct file *file, struct knote *kn)
1097 struct linux_file *filp;
1102 filp = (struct linux_file *)file->f_data;
1103 filp->f_flags = file->f_flag;
1104 if (filp->f_op->poll == NULL)
1107 spin_lock(&filp->f_kqlock);
1108 switch (kn->kn_filter) {
1110 filp->f_kqflags |= LINUX_KQ_FLAG_HAS_READ;
1111 kn->kn_fop = &linux_dev_kqfiltops_read;
1113 knlist_add(&filp->f_selinfo.si_note, kn, 1);
1117 filp->f_kqflags |= LINUX_KQ_FLAG_HAS_WRITE;
1118 kn->kn_fop = &linux_dev_kqfiltops_write;
1120 knlist_add(&filp->f_selinfo.si_note, kn, 1);
1127 spin_unlock(&filp->f_kqlock);
1130 linux_set_current(td);
1132 /* update kqfilter status, if any */
1133 linux_file_kqfilter_poll(filp,
1134 LINUX_KQ_FLAG_HAS_READ | LINUX_KQ_FLAG_HAS_WRITE);
1140 linux_file_mmap_single(struct file *fp, vm_ooffset_t *offset,
1141 vm_size_t size, struct vm_object **object, int nprot,
1144 struct task_struct *task;
1145 struct vm_area_struct *vmap;
1146 struct mm_struct *mm;
1147 struct linux_file *filp;
1151 filp = (struct linux_file *)fp->f_data;
1152 filp->f_flags = fp->f_flag;
1154 if (filp->f_op->mmap == NULL)
1155 return (EOPNOTSUPP);
1157 linux_set_current(td);
1160 * The same VM object might be shared by multiple processes
1161 * and the mm_struct is usually freed when a process exits.
1163 * The atomic reference below makes sure the mm_struct is
1164 * available as long as the vmap is in the linux_vma_head.
1168 if (atomic_inc_not_zero(&mm->mm_users) == 0)
1171 vmap = kzalloc(sizeof(*vmap), GFP_KERNEL);
1173 vmap->vm_end = size;
1174 vmap->vm_pgoff = *offset / PAGE_SIZE;
1176 vmap->vm_flags = vmap->vm_page_prot = (nprot & VM_PROT_ALL);
1177 vmap->vm_ops = NULL;
1178 vmap->vm_file = get_file(filp);
1181 if (unlikely(down_write_killable(&vmap->vm_mm->mmap_sem))) {
1182 error = linux_get_error(task, EINTR);
1184 error = -OPW(fp, td, filp->f_op->mmap(filp, vmap));
1185 error = linux_get_error(task, error);
1186 up_write(&vmap->vm_mm->mmap_sem);
1190 linux_cdev_handle_free(vmap);
1194 attr = pgprot2cachemode(vmap->vm_page_prot);
1196 if (vmap->vm_ops != NULL) {
1197 struct vm_area_struct *ptr;
1198 void *vm_private_data;
1201 if (vmap->vm_ops->open == NULL ||
1202 vmap->vm_ops->close == NULL ||
1203 vmap->vm_private_data == NULL) {
1204 /* free allocated VM area struct */
1205 linux_cdev_handle_free(vmap);
1209 vm_private_data = vmap->vm_private_data;
1211 rw_wlock(&linux_vma_lock);
1212 TAILQ_FOREACH(ptr, &linux_vma_head, vm_entry) {
1213 if (ptr->vm_private_data == vm_private_data)
1216 /* check if there is an existing VM area struct */
1218 /* check if the VM area structure is invalid */
1219 if (ptr->vm_ops == NULL ||
1220 ptr->vm_ops->open == NULL ||
1221 ptr->vm_ops->close == NULL) {
1226 vm_no_fault = (ptr->vm_ops->fault == NULL);
1229 /* insert VM area structure into list */
1230 TAILQ_INSERT_TAIL(&linux_vma_head, vmap, vm_entry);
1232 vm_no_fault = (vmap->vm_ops->fault == NULL);
1234 rw_wunlock(&linux_vma_lock);
1237 /* free allocated VM area struct */
1238 linux_cdev_handle_free(vmap);
1239 /* check for stale VM area struct */
1240 if (error != EEXIST)
1244 /* check if there is no fault handler */
1246 *object = cdev_pager_allocate(vm_private_data, OBJT_DEVICE,
1247 &linux_cdev_pager_ops[1], size, nprot, *offset,
1250 *object = cdev_pager_allocate(vm_private_data, OBJT_MGTDEVICE,
1251 &linux_cdev_pager_ops[0], size, nprot, *offset,
1255 /* check if allocating the VM object failed */
1256 if (*object == NULL) {
1258 /* remove VM area struct from list */
1259 linux_cdev_handle_remove(vmap);
1260 /* free allocated VM area struct */
1261 linux_cdev_handle_free(vmap);
1268 sg = sglist_alloc(1, M_WAITOK);
1269 sglist_append_phys(sg,
1270 (vm_paddr_t)vmap->vm_pfn << PAGE_SHIFT, vmap->vm_len);
1272 *object = vm_pager_allocate(OBJT_SG, sg, vmap->vm_len,
1273 nprot, 0, td->td_ucred);
1275 linux_cdev_handle_free(vmap);
1277 if (*object == NULL) {
1283 if (attr != VM_MEMATTR_DEFAULT) {
1284 VM_OBJECT_WLOCK(*object);
1285 vm_object_set_memattr(*object, attr);
1286 VM_OBJECT_WUNLOCK(*object);
1292 struct cdevsw linuxcdevsw = {
1293 .d_version = D_VERSION,
1294 .d_fdopen = linux_dev_fdopen,
1295 .d_name = "lkpidev",
1299 linux_file_read(struct file *file, struct uio *uio, struct ucred *active_cred,
1300 int flags, struct thread *td)
1302 struct linux_file *filp;
1307 filp = (struct linux_file *)file->f_data;
1308 filp->f_flags = file->f_flag;
1309 /* XXX no support for I/O vectors currently */
1310 if (uio->uio_iovcnt != 1)
1311 return (EOPNOTSUPP);
1312 if (uio->uio_resid > DEVFS_IOSIZE_MAX)
1314 linux_set_current(td);
1315 if (filp->f_op->read) {
1316 bytes = OPW(file, td, filp->f_op->read(filp, uio->uio_iov->iov_base,
1317 uio->uio_iov->iov_len, &uio->uio_offset));
1319 uio->uio_iov->iov_base =
1320 ((uint8_t *)uio->uio_iov->iov_base) + bytes;
1321 uio->uio_iov->iov_len -= bytes;
1322 uio->uio_resid -= bytes;
1324 error = linux_get_error(current, -bytes);
1329 /* update kqfilter status, if any */
1330 linux_file_kqfilter_poll(filp, LINUX_KQ_FLAG_HAS_READ);
1336 linux_file_write(struct file *file, struct uio *uio, struct ucred *active_cred,
1337 int flags, struct thread *td)
1339 struct linux_file *filp;
1344 filp = (struct linux_file *)file->f_data;
1345 filp->f_flags = file->f_flag;
1346 /* XXX no support for I/O vectors currently */
1347 if (uio->uio_iovcnt != 1)
1348 return (EOPNOTSUPP);
1349 if (uio->uio_resid > DEVFS_IOSIZE_MAX)
1351 linux_set_current(td);
1352 if (filp->f_op->write) {
1353 bytes = OPW(file, td, filp->f_op->write(filp, uio->uio_iov->iov_base,
1354 uio->uio_iov->iov_len, &uio->uio_offset));
1356 uio->uio_iov->iov_base =
1357 ((uint8_t *)uio->uio_iov->iov_base) + bytes;
1358 uio->uio_iov->iov_len -= bytes;
1359 uio->uio_resid -= bytes;
1361 error = linux_get_error(current, -bytes);
1366 /* update kqfilter status, if any */
1367 linux_file_kqfilter_poll(filp, LINUX_KQ_FLAG_HAS_WRITE);
1373 linux_file_poll(struct file *file, int events, struct ucred *active_cred,
1376 struct linux_file *filp;
1379 filp = (struct linux_file *)file->f_data;
1380 filp->f_flags = file->f_flag;
1381 linux_set_current(td);
1382 if (filp->f_op->poll != NULL)
1383 revents = OPW(file, td, filp->f_op->poll(filp, LINUX_POLL_TABLE_NORMAL)) & events;
1391 linux_file_close(struct file *file, struct thread *td)
1393 struct linux_file *filp;
1396 filp = (struct linux_file *)file->f_data;
1398 KASSERT(file_count(filp) == 0, ("File refcount(%d) is not zero", file_count(filp)));
1400 filp->f_flags = file->f_flag;
1401 linux_set_current(td);
1402 linux_poll_wait_dequeue(filp);
1403 error = -OPW(file, td, filp->f_op->release(filp->f_vnode, filp));
1404 funsetown(&filp->f_sigio);
1405 if (filp->f_vnode != NULL)
1406 vdrop(filp->f_vnode);
1407 if (filp->f_cdev != NULL) {
1408 /* put a reference on the Linux character device */
1409 atomic_long_dec(&filp->f_cdev->refs);
1417 linux_file_ioctl(struct file *fp, u_long cmd, void *data, struct ucred *cred,
1420 struct linux_file *filp;
1423 filp = (struct linux_file *)fp->f_data;
1424 filp->f_flags = fp->f_flag;
1427 linux_set_current(td);
1432 if (filp->f_op->fasync == NULL)
1434 error = -OPW(fp, td, filp->f_op->fasync(0, filp, fp->f_flag & FASYNC));
1437 error = fsetown(*(int *)data, &filp->f_sigio);
1439 if (filp->f_op->fasync == NULL)
1441 error = -OPW(fp, td, filp->f_op->fasync(0, filp,
1442 fp->f_flag & FASYNC));
1446 *(int *)data = fgetown(&filp->f_sigio);
1449 error = linux_file_ioctl_sub(fp, filp, cmd, data, td);
1456 linux_file_mmap_sub(struct thread *td, vm_size_t objsize, vm_prot_t prot,
1457 vm_prot_t *maxprotp, int *flagsp, struct file *fp,
1458 vm_ooffset_t *foff, vm_object_t *objp)
1461 * Character devices do not provide private mappings
1464 if ((*maxprotp & VM_PROT_WRITE) == 0 &&
1465 (prot & VM_PROT_WRITE) != 0)
1467 if ((*flagsp & (MAP_PRIVATE | MAP_COPY)) != 0)
1470 return (linux_file_mmap_single(fp, foff, objsize, objp, (int)prot, td));
1474 linux_file_mmap(struct file *fp, vm_map_t map, vm_offset_t *addr, vm_size_t size,
1475 vm_prot_t prot, vm_prot_t cap_maxprot, int flags, vm_ooffset_t foff,
1478 struct linux_file *filp;
1485 filp = (struct linux_file *)fp->f_data;
1489 return (EOPNOTSUPP);
1492 * Ensure that file and memory protections are
1496 if (mp != NULL && (mp->mnt_flag & MNT_NOEXEC) != 0) {
1497 maxprot = VM_PROT_NONE;
1498 if ((prot & VM_PROT_EXECUTE) != 0)
1501 maxprot = VM_PROT_EXECUTE;
1502 if ((fp->f_flag & FREAD) != 0)
1503 maxprot |= VM_PROT_READ;
1504 else if ((prot & VM_PROT_READ) != 0)
1508 * If we are sharing potential changes via MAP_SHARED and we
1509 * are trying to get write permission although we opened it
1510 * without asking for it, bail out.
1512 * Note that most character devices always share mappings.
1514 * Rely on linux_file_mmap_sub() to fail invalid MAP_PRIVATE
1515 * requests rather than doing it here.
1517 if ((flags & MAP_SHARED) != 0) {
1518 if ((fp->f_flag & FWRITE) != 0)
1519 maxprot |= VM_PROT_WRITE;
1520 else if ((prot & VM_PROT_WRITE) != 0)
1523 maxprot &= cap_maxprot;
1525 error = linux_file_mmap_sub(td, size, prot, &maxprot, &flags, fp, &foff,
1530 error = vm_mmap_object(map, addr, size, prot, maxprot, flags, object,
1533 vm_object_deallocate(object);
1538 linux_file_stat(struct file *fp, struct stat *sb, struct ucred *active_cred,
1541 struct linux_file *filp;
1545 filp = (struct linux_file *)fp->f_data;
1546 if (filp->f_vnode == NULL)
1547 return (EOPNOTSUPP);
1551 vn_lock(vp, LK_SHARED | LK_RETRY);
1552 error = vn_stat(vp, sb, td->td_ucred, NOCRED, td);
1559 linux_file_fill_kinfo(struct file *fp, struct kinfo_file *kif,
1560 struct filedesc *fdp)
1562 struct linux_file *filp;
1570 kif->kf_type = KF_TYPE_DEV;
1573 FILEDESC_SUNLOCK(fdp);
1574 error = vn_fill_kinfo_vnode(vp, kif);
1576 kif->kf_type = KF_TYPE_VNODE;
1577 FILEDESC_SLOCK(fdp);
1583 linux_iminor(struct inode *inode)
1585 struct linux_cdev *ldev;
1587 if (inode == NULL || inode->v_rdev == NULL ||
1588 inode->v_rdev->si_devsw != &linuxcdevsw)
1590 ldev = inode->v_rdev->si_drv1;
1594 return (minor(ldev->dev));
1597 struct fileops linuxfileops = {
1598 .fo_read = linux_file_read,
1599 .fo_write = linux_file_write,
1600 .fo_truncate = invfo_truncate,
1601 .fo_kqfilter = linux_file_kqfilter,
1602 .fo_stat = linux_file_stat,
1603 .fo_fill_kinfo = linux_file_fill_kinfo,
1604 .fo_poll = linux_file_poll,
1605 .fo_close = linux_file_close,
1606 .fo_ioctl = linux_file_ioctl,
1607 .fo_mmap = linux_file_mmap,
1608 .fo_chmod = invfo_chmod,
1609 .fo_chown = invfo_chown,
1610 .fo_sendfile = invfo_sendfile,
1611 .fo_flags = DFLAG_PASSABLE,
1615 * Hash of vmmap addresses. This is infrequently accessed and does not
1616 * need to be particularly large. This is done because we must store the
1617 * caller's idea of the map size to properly unmap.
1620 LIST_ENTRY(vmmap) vm_next;
1622 unsigned long vm_size;
1626 struct vmmap *lh_first;
1628 #define VMMAP_HASH_SIZE 64
1629 #define VMMAP_HASH_MASK (VMMAP_HASH_SIZE - 1)
1630 #define VM_HASH(addr) ((uintptr_t)(addr) >> PAGE_SHIFT) & VMMAP_HASH_MASK
1631 static struct vmmaphd vmmaphead[VMMAP_HASH_SIZE];
1632 static struct mtx vmmaplock;
1635 vmmap_add(void *addr, unsigned long size)
1637 struct vmmap *vmmap;
1639 vmmap = kmalloc(sizeof(*vmmap), GFP_KERNEL);
1640 mtx_lock(&vmmaplock);
1641 vmmap->vm_size = size;
1642 vmmap->vm_addr = addr;
1643 LIST_INSERT_HEAD(&vmmaphead[VM_HASH(addr)], vmmap, vm_next);
1644 mtx_unlock(&vmmaplock);
1647 static struct vmmap *
1648 vmmap_remove(void *addr)
1650 struct vmmap *vmmap;
1652 mtx_lock(&vmmaplock);
1653 LIST_FOREACH(vmmap, &vmmaphead[VM_HASH(addr)], vm_next)
1654 if (vmmap->vm_addr == addr)
1657 LIST_REMOVE(vmmap, vm_next);
1658 mtx_unlock(&vmmaplock);
1663 #if defined(__i386__) || defined(__amd64__) || defined(__powerpc__)
1665 _ioremap_attr(vm_paddr_t phys_addr, unsigned long size, int attr)
1669 addr = pmap_mapdev_attr(phys_addr, size, attr);
1672 vmmap_add(addr, size);
1681 struct vmmap *vmmap;
1683 vmmap = vmmap_remove(addr);
1686 #if defined(__i386__) || defined(__amd64__) || defined(__powerpc__)
1687 pmap_unmapdev((vm_offset_t)addr, vmmap->vm_size);
1694 vmap(struct page **pages, unsigned int count, unsigned long flags, int prot)
1699 size = count * PAGE_SIZE;
1700 off = kva_alloc(size);
1703 vmmap_add((void *)off, size);
1704 pmap_qenter(off, pages, count);
1706 return ((void *)off);
1712 struct vmmap *vmmap;
1714 vmmap = vmmap_remove(addr);
1717 pmap_qremove((vm_offset_t)addr, vmmap->vm_size / PAGE_SIZE);
1718 kva_free((vm_offset_t)addr, vmmap->vm_size);
1723 kvasprintf(gfp_t gfp, const char *fmt, va_list ap)
1730 len = vsnprintf(NULL, 0, fmt, aq);
1733 p = kmalloc(len + 1, gfp);
1735 vsnprintf(p, len + 1, fmt, ap);
1741 kasprintf(gfp_t gfp, const char *fmt, ...)
1747 p = kvasprintf(gfp, fmt, ap);
1754 linux_timer_callback_wrapper(void *context)
1756 struct timer_list *timer;
1758 linux_set_current(curthread);
1761 timer->function(timer->data);
1765 mod_timer(struct timer_list *timer, int expires)
1768 timer->expires = expires;
1769 callout_reset(&timer->callout,
1770 linux_timer_jiffies_until(expires),
1771 &linux_timer_callback_wrapper, timer);
1775 add_timer(struct timer_list *timer)
1778 callout_reset(&timer->callout,
1779 linux_timer_jiffies_until(timer->expires),
1780 &linux_timer_callback_wrapper, timer);
1784 add_timer_on(struct timer_list *timer, int cpu)
1787 callout_reset_on(&timer->callout,
1788 linux_timer_jiffies_until(timer->expires),
1789 &linux_timer_callback_wrapper, timer, cpu);
1793 linux_timer_init(void *arg)
1797 * Compute an internal HZ value which can divide 2**32 to
1798 * avoid timer rounding problems when the tick value wraps
1801 linux_timer_hz_mask = 1;
1802 while (linux_timer_hz_mask < (unsigned long)hz)
1803 linux_timer_hz_mask *= 2;
1804 linux_timer_hz_mask--;
1806 SYSINIT(linux_timer, SI_SUB_DRIVERS, SI_ORDER_FIRST, linux_timer_init, NULL);
1809 linux_complete_common(struct completion *c, int all)
1816 wakeup_swapper = sleepq_broadcast(c, SLEEPQ_SLEEP, 0, 0);
1818 if (c->done != UINT_MAX)
1820 wakeup_swapper = sleepq_signal(c, SLEEPQ_SLEEP, 0, 0);
1828 * Indefinite wait for done != 0 with or without signals.
1831 linux_wait_for_common(struct completion *c, int flags)
1833 struct task_struct *task;
1836 if (SCHEDULER_STOPPED())
1842 flags = SLEEPQ_INTERRUPTIBLE | SLEEPQ_SLEEP;
1844 flags = SLEEPQ_SLEEP;
1850 sleepq_add(c, NULL, "completion", flags, 0);
1851 if (flags & SLEEPQ_INTERRUPTIBLE) {
1853 error = -sleepq_wait_sig(c, 0);
1856 linux_schedule_save_interrupt_value(task, error);
1857 error = -ERESTARTSYS;
1866 if (c->done != UINT_MAX)
1875 * Time limited wait for done != 0 with or without signals.
1878 linux_wait_for_timeout_common(struct completion *c, int timeout, int flags)
1880 struct task_struct *task;
1881 int end = jiffies + timeout;
1884 if (SCHEDULER_STOPPED())
1890 flags = SLEEPQ_INTERRUPTIBLE | SLEEPQ_SLEEP;
1892 flags = SLEEPQ_SLEEP;
1898 sleepq_add(c, NULL, "completion", flags, 0);
1899 sleepq_set_timeout(c, linux_timer_jiffies_until(end));
1902 if (flags & SLEEPQ_INTERRUPTIBLE)
1903 error = -sleepq_timedwait_sig(c, 0);
1905 error = -sleepq_timedwait(c, 0);
1909 /* check for timeout */
1910 if (error == -EWOULDBLOCK) {
1911 error = 0; /* timeout */
1913 /* signal happened */
1914 linux_schedule_save_interrupt_value(task, error);
1915 error = -ERESTARTSYS;
1920 if (c->done != UINT_MAX)
1924 /* return how many jiffies are left */
1925 error = linux_timer_jiffies_until(end);
1931 linux_try_wait_for_completion(struct completion *c)
1936 isdone = (c->done != 0);
1937 if (c->done != 0 && c->done != UINT_MAX)
1944 linux_completion_done(struct completion *c)
1949 isdone = (c->done != 0);
1955 linux_cdev_release(struct kobject *kobj)
1957 struct linux_cdev *cdev;
1958 struct kobject *parent;
1960 cdev = container_of(kobj, struct linux_cdev, kobj);
1961 parent = kobj->parent;
1962 linux_destroy_dev(cdev);
1964 kobject_put(parent);
1968 linux_cdev_static_release(struct kobject *kobj)
1970 struct linux_cdev *cdev;
1971 struct kobject *parent;
1973 cdev = container_of(kobj, struct linux_cdev, kobj);
1974 parent = kobj->parent;
1975 linux_destroy_dev(cdev);
1976 kobject_put(parent);
1980 linux_destroy_dev(struct linux_cdev *cdev)
1983 if (cdev->cdev == NULL)
1986 atomic_long_dec(&cdev->refs);
1988 /* wait for all open files to be closed */
1989 while (atomic_long_read(&cdev->refs) != -1L)
1990 pause("ldevdrn", hz);
1992 destroy_dev(cdev->cdev);
1996 const struct kobj_type linux_cdev_ktype = {
1997 .release = linux_cdev_release,
2000 const struct kobj_type linux_cdev_static_ktype = {
2001 .release = linux_cdev_static_release,
2005 linux_handle_ifnet_link_event(void *arg, struct ifnet *ifp, int linkstate)
2007 struct notifier_block *nb;
2010 if (linkstate == LINK_STATE_UP)
2011 nb->notifier_call(nb, NETDEV_UP, ifp);
2013 nb->notifier_call(nb, NETDEV_DOWN, ifp);
2017 linux_handle_ifnet_arrival_event(void *arg, struct ifnet *ifp)
2019 struct notifier_block *nb;
2022 nb->notifier_call(nb, NETDEV_REGISTER, ifp);
2026 linux_handle_ifnet_departure_event(void *arg, struct ifnet *ifp)
2028 struct notifier_block *nb;
2031 nb->notifier_call(nb, NETDEV_UNREGISTER, ifp);
2035 linux_handle_iflladdr_event(void *arg, struct ifnet *ifp)
2037 struct notifier_block *nb;
2040 nb->notifier_call(nb, NETDEV_CHANGEADDR, ifp);
2044 linux_handle_ifaddr_event(void *arg, struct ifnet *ifp)
2046 struct notifier_block *nb;
2049 nb->notifier_call(nb, NETDEV_CHANGEIFADDR, ifp);
2053 register_netdevice_notifier(struct notifier_block *nb)
2056 nb->tags[NETDEV_UP] = EVENTHANDLER_REGISTER(
2057 ifnet_link_event, linux_handle_ifnet_link_event, nb, 0);
2058 nb->tags[NETDEV_REGISTER] = EVENTHANDLER_REGISTER(
2059 ifnet_arrival_event, linux_handle_ifnet_arrival_event, nb, 0);
2060 nb->tags[NETDEV_UNREGISTER] = EVENTHANDLER_REGISTER(
2061 ifnet_departure_event, linux_handle_ifnet_departure_event, nb, 0);
2062 nb->tags[NETDEV_CHANGEADDR] = EVENTHANDLER_REGISTER(
2063 iflladdr_event, linux_handle_iflladdr_event, nb, 0);
2069 register_inetaddr_notifier(struct notifier_block *nb)
2072 nb->tags[NETDEV_CHANGEIFADDR] = EVENTHANDLER_REGISTER(
2073 ifaddr_event, linux_handle_ifaddr_event, nb, 0);
2078 unregister_netdevice_notifier(struct notifier_block *nb)
2081 EVENTHANDLER_DEREGISTER(ifnet_link_event,
2082 nb->tags[NETDEV_UP]);
2083 EVENTHANDLER_DEREGISTER(ifnet_arrival_event,
2084 nb->tags[NETDEV_REGISTER]);
2085 EVENTHANDLER_DEREGISTER(ifnet_departure_event,
2086 nb->tags[NETDEV_UNREGISTER]);
2087 EVENTHANDLER_DEREGISTER(iflladdr_event,
2088 nb->tags[NETDEV_CHANGEADDR]);
2094 unregister_inetaddr_notifier(struct notifier_block *nb)
2097 EVENTHANDLER_DEREGISTER(ifaddr_event,
2098 nb->tags[NETDEV_CHANGEIFADDR]);
2103 struct list_sort_thunk {
2104 int (*cmp)(void *, struct list_head *, struct list_head *);
2109 linux_le_cmp(void *priv, const void *d1, const void *d2)
2111 struct list_head *le1, *le2;
2112 struct list_sort_thunk *thunk;
2115 le1 = *(__DECONST(struct list_head **, d1));
2116 le2 = *(__DECONST(struct list_head **, d2));
2117 return ((thunk->cmp)(thunk->priv, le1, le2));
2121 list_sort(void *priv, struct list_head *head, int (*cmp)(void *priv,
2122 struct list_head *a, struct list_head *b))
2124 struct list_sort_thunk thunk;
2125 struct list_head **ar, *le;
2129 list_for_each(le, head)
2131 ar = malloc(sizeof(struct list_head *) * count, M_KMALLOC, M_WAITOK);
2133 list_for_each(le, head)
2137 qsort_r(ar, count, sizeof(struct list_head *), &thunk, linux_le_cmp);
2138 INIT_LIST_HEAD(head);
2139 for (i = 0; i < count; i++)
2140 list_add_tail(ar[i], head);
2141 free(ar, M_KMALLOC);
2145 linux_irq_handler(void *ent)
2147 struct irq_ent *irqe;
2149 linux_set_current(curthread);
2152 irqe->handler(irqe->irq, irqe->arg);
2155 #if defined(__i386__) || defined(__amd64__)
2157 linux_wbinvd_on_all_cpus(void)
2160 pmap_invalidate_cache();
2166 linux_on_each_cpu(void callback(void *), void *data)
2169 smp_rendezvous(smp_no_rendezvous_barrier, callback,
2170 smp_no_rendezvous_barrier, data);
2175 linux_in_atomic(void)
2178 return ((curthread->td_pflags & TDP_NOFAULTING) != 0);
2182 linux_find_cdev(const char *name, unsigned major, unsigned minor)
2184 dev_t dev = MKDEV(major, minor);
2188 LIST_FOREACH(cdev, &linuxcdevsw.d_devs, si_list) {
2189 struct linux_cdev *ldev = cdev->si_drv1;
2190 if (ldev->dev == dev &&
2191 strcmp(kobject_name(&ldev->kobj), name) == 0) {
2197 return (cdev != NULL ? cdev->si_drv1 : NULL);
2201 __register_chrdev(unsigned int major, unsigned int baseminor,
2202 unsigned int count, const char *name,
2203 const struct file_operations *fops)
2205 struct linux_cdev *cdev;
2209 for (i = baseminor; i < baseminor + count; i++) {
2210 cdev = cdev_alloc();
2211 cdev_init(cdev, fops);
2212 kobject_set_name(&cdev->kobj, name);
2214 ret = cdev_add(cdev, makedev(major, i), 1);
2222 __register_chrdev_p(unsigned int major, unsigned int baseminor,
2223 unsigned int count, const char *name,
2224 const struct file_operations *fops, uid_t uid,
2225 gid_t gid, int mode)
2227 struct linux_cdev *cdev;
2231 for (i = baseminor; i < baseminor + count; i++) {
2232 cdev = cdev_alloc();
2233 cdev_init(cdev, fops);
2234 kobject_set_name(&cdev->kobj, name);
2236 ret = cdev_add_ext(cdev, makedev(major, i), uid, gid, mode);
2244 __unregister_chrdev(unsigned int major, unsigned int baseminor,
2245 unsigned int count, const char *name)
2247 struct linux_cdev *cdevp;
2250 for (i = baseminor; i < baseminor + count; i++) {
2251 cdevp = linux_find_cdev(name, major, i);
2258 linux_dump_stack(void)
2269 #if defined(__i386__) || defined(__amd64__)
2270 bool linux_cpu_has_clflush;
2274 linux_compat_init(void *arg)
2276 struct sysctl_oid *rootoid;
2279 #if defined(__i386__) || defined(__amd64__)
2280 linux_cpu_has_clflush = (cpu_feature & CPUID_CLFSH);
2282 rw_init(&linux_vma_lock, "lkpi-vma-lock");
2284 rootoid = SYSCTL_ADD_ROOT_NODE(NULL,
2285 OID_AUTO, "sys", CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, "sys");
2286 kobject_init(&linux_class_root, &linux_class_ktype);
2287 kobject_set_name(&linux_class_root, "class");
2288 linux_class_root.oidp = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(rootoid),
2289 OID_AUTO, "class", CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, "class");
2290 kobject_init(&linux_root_device.kobj, &linux_dev_ktype);
2291 kobject_set_name(&linux_root_device.kobj, "device");
2292 linux_root_device.kobj.oidp = SYSCTL_ADD_NODE(NULL,
2293 SYSCTL_CHILDREN(rootoid), OID_AUTO, "device", CTLFLAG_RD, NULL,
2295 linux_root_device.bsddev = root_bus;
2296 linux_class_misc.name = "misc";
2297 class_register(&linux_class_misc);
2298 INIT_LIST_HEAD(&pci_drivers);
2299 INIT_LIST_HEAD(&pci_devices);
2300 spin_lock_init(&pci_lock);
2301 mtx_init(&vmmaplock, "IO Map lock", NULL, MTX_DEF);
2302 for (i = 0; i < VMMAP_HASH_SIZE; i++)
2303 LIST_INIT(&vmmaphead[i]);
2305 SYSINIT(linux_compat, SI_SUB_DRIVERS, SI_ORDER_SECOND, linux_compat_init, NULL);
2308 linux_compat_uninit(void *arg)
2310 linux_kobject_kfree_name(&linux_class_root);
2311 linux_kobject_kfree_name(&linux_root_device.kobj);
2312 linux_kobject_kfree_name(&linux_class_misc.kobj);
2314 mtx_destroy(&vmmaplock);
2315 spin_lock_destroy(&pci_lock);
2316 rw_destroy(&linux_vma_lock);
2318 SYSUNINIT(linux_compat, SI_SUB_DRIVERS, SI_ORDER_SECOND, linux_compat_uninit, NULL);
2321 * NOTE: Linux frequently uses "unsigned long" for pointer to integer
2322 * conversion and vice versa, where in FreeBSD "uintptr_t" would be
2323 * used. Assert these types have the same size, else some parts of the
2324 * LinuxKPI may not work like expected:
2326 CTASSERT(sizeof(unsigned long) == sizeof(uintptr_t));