2 * Copyright (c) 2010 Isilon Systems, Inc.
3 * Copyright (c) 2010 iX Systems, Inc.
4 * Copyright (c) 2010 Panasas, Inc.
5 * Copyright (c) 2013-2016 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>
52 #include <machine/stdarg.h>
54 #if defined(__i386__) || defined(__amd64__)
55 #include <machine/md_var.h>
58 #include <linux/kobject.h>
59 #include <linux/device.h>
60 #include <linux/slab.h>
61 #include <linux/module.h>
62 #include <linux/cdev.h>
63 #include <linux/file.h>
64 #include <linux/sysfs.h>
67 #include <linux/vmalloc.h>
68 #include <linux/netdevice.h>
69 #include <linux/timer.h>
70 #include <linux/workqueue.h>
71 #include <linux/rcupdate.h>
72 #include <linux/interrupt.h>
73 #include <linux/uaccess.h>
74 #include <linux/kernel.h>
75 #include <linux/list.h>
76 #include <linux/compat.h>
78 #include <vm/vm_pager.h>
80 MALLOC_DEFINE(M_KMALLOC, "linux", "Linux kmalloc compat");
82 #include <linux/rbtree.h>
83 /* Undo Linux compat changes. */
87 #define RB_ROOT(head) (head)->rbh_root
89 struct kobject linux_class_root;
90 struct device linux_root_device;
91 struct class linux_class_misc;
92 struct list_head pci_drivers;
93 struct list_head pci_devices;
96 struct sx linux_global_rcu_lock;
98 unsigned long linux_timer_hz_mask;
101 panic_cmp(struct rb_node *one, struct rb_node *two)
106 RB_GENERATE(linux_root, rb_node, __entry, panic_cmp);
109 kobject_set_name_vargs(struct kobject *kobj, const char *fmt, va_list args)
119 if (old && fmt == NULL)
122 /* compute length of string */
123 va_copy(tmp_va, args);
124 len = vsnprintf(&dummy, 0, fmt, tmp_va);
127 /* account for zero termination */
130 /* check for error */
134 /* allocate memory for string */
135 name = kzalloc(len, GFP_KERNEL);
138 vsnprintf(name, len, fmt, args);
141 /* free old string */
144 /* filter new string */
145 for (; *name != '\0'; name++)
152 kobject_set_name(struct kobject *kobj, const char *fmt, ...)
158 error = kobject_set_name_vargs(kobj, fmt, args);
165 kobject_add_complete(struct kobject *kobj, struct kobject *parent)
167 const struct kobj_type *t;
170 kobj->parent = parent;
171 error = sysfs_create_dir(kobj);
172 if (error == 0 && kobj->ktype && kobj->ktype->default_attrs) {
173 struct attribute **attr;
176 for (attr = t->default_attrs; *attr != NULL; attr++) {
177 error = sysfs_create_file(kobj, *attr);
182 sysfs_remove_dir(kobj);
189 kobject_add(struct kobject *kobj, struct kobject *parent, const char *fmt, ...)
195 error = kobject_set_name_vargs(kobj, fmt, args);
200 return kobject_add_complete(kobj, parent);
204 linux_kobject_release(struct kref *kref)
206 struct kobject *kobj;
209 kobj = container_of(kref, struct kobject, kref);
210 sysfs_remove_dir(kobj);
212 if (kobj->ktype && kobj->ktype->release)
213 kobj->ktype->release(kobj);
218 linux_kobject_kfree(struct kobject *kobj)
224 linux_kobject_kfree_name(struct kobject *kobj)
231 const struct kobj_type linux_kfree_type = {
232 .release = linux_kobject_kfree
236 linux_device_release(struct device *dev)
238 pr_debug("linux_device_release: %s\n", dev_name(dev));
243 linux_class_show(struct kobject *kobj, struct attribute *attr, char *buf)
245 struct class_attribute *dattr;
248 dattr = container_of(attr, struct class_attribute, attr);
251 error = dattr->show(container_of(kobj, struct class, kobj),
257 linux_class_store(struct kobject *kobj, struct attribute *attr, const char *buf,
260 struct class_attribute *dattr;
263 dattr = container_of(attr, struct class_attribute, attr);
266 error = dattr->store(container_of(kobj, struct class, kobj),
272 linux_class_release(struct kobject *kobj)
276 class = container_of(kobj, struct class, kobj);
277 if (class->class_release)
278 class->class_release(class);
281 static const struct sysfs_ops linux_class_sysfs = {
282 .show = linux_class_show,
283 .store = linux_class_store,
286 const struct kobj_type linux_class_ktype = {
287 .release = linux_class_release,
288 .sysfs_ops = &linux_class_sysfs
292 linux_dev_release(struct kobject *kobj)
296 dev = container_of(kobj, struct device, kobj);
297 /* This is the precedence defined by linux. */
300 else if (dev->class && dev->class->dev_release)
301 dev->class->dev_release(dev);
305 linux_dev_show(struct kobject *kobj, struct attribute *attr, char *buf)
307 struct device_attribute *dattr;
310 dattr = container_of(attr, struct device_attribute, attr);
313 error = dattr->show(container_of(kobj, struct device, kobj),
319 linux_dev_store(struct kobject *kobj, struct attribute *attr, const char *buf,
322 struct device_attribute *dattr;
325 dattr = container_of(attr, struct device_attribute, attr);
328 error = dattr->store(container_of(kobj, struct device, kobj),
333 static const struct sysfs_ops linux_dev_sysfs = {
334 .show = linux_dev_show,
335 .store = linux_dev_store,
338 const struct kobj_type linux_dev_ktype = {
339 .release = linux_dev_release,
340 .sysfs_ops = &linux_dev_sysfs
344 device_create(struct class *class, struct device *parent, dev_t devt,
345 void *drvdata, const char *fmt, ...)
350 dev = kzalloc(sizeof(*dev), M_WAITOK);
351 dev->parent = parent;
354 dev->driver_data = drvdata;
355 dev->release = linux_device_release;
357 kobject_set_name_vargs(&dev->kobj, fmt, args);
359 device_register(dev);
365 kobject_init_and_add(struct kobject *kobj, const struct kobj_type *ktype,
366 struct kobject *parent, const char *fmt, ...)
371 kobject_init(kobj, ktype);
373 kobj->parent = parent;
377 error = kobject_set_name_vargs(kobj, fmt, args);
381 return kobject_add_complete(kobj, parent);
385 linux_set_current(struct thread *td, struct task_struct *t)
387 memset(t, 0, sizeof(*t));
388 task_struct_fill(td, t);
389 task_struct_set(td, t);
393 linux_clear_current(struct thread *td)
395 task_struct_set(td, NULL);
399 linux_file_dtor(void *cdp)
401 struct linux_file *filp;
402 struct task_struct t;
407 linux_set_current(td, &t);
408 filp->f_op->release(filp->f_vnode, filp);
409 linux_clear_current(td);
410 vdrop(filp->f_vnode);
415 linux_dev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
417 struct linux_cdev *ldev;
418 struct linux_file *filp;
419 struct task_struct t;
427 filp = kzalloc(sizeof(*filp), GFP_KERNEL);
428 filp->f_dentry = &filp->f_dentry_store;
429 filp->f_op = ldev->ops;
430 filp->f_flags = file->f_flag;
431 vhold(file->f_vnode);
432 filp->f_vnode = file->f_vnode;
433 linux_set_current(td, &t);
434 if (filp->f_op->open) {
435 error = -filp->f_op->open(file->f_vnode, filp);
441 error = devfs_set_cdevpriv(filp, linux_file_dtor);
443 filp->f_op->release(file->f_vnode, filp);
447 linux_clear_current(td);
452 linux_dev_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
454 struct linux_cdev *ldev;
455 struct linux_file *filp;
463 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
465 filp->f_flags = file->f_flag;
466 devfs_clear_cdevpriv();
472 #define LINUX_IOCTL_MIN_PTR 0x10000UL
473 #define LINUX_IOCTL_MAX_PTR (LINUX_IOCTL_MIN_PTR + IOCPARM_MAX)
476 linux_remap_address(void **uaddr, size_t len)
478 uintptr_t uaddr_val = (uintptr_t)(*uaddr);
480 if (unlikely(uaddr_val >= LINUX_IOCTL_MIN_PTR &&
481 uaddr_val < LINUX_IOCTL_MAX_PTR)) {
482 struct task_struct *pts = current;
488 /* compute data offset */
489 uaddr_val -= LINUX_IOCTL_MIN_PTR;
491 /* check that length is within bounds */
492 if ((len > IOCPARM_MAX) ||
493 (uaddr_val + len) > pts->bsd_ioctl_len) {
498 /* re-add kernel buffer address */
499 uaddr_val += (uintptr_t)pts->bsd_ioctl_data;
501 /* update address location */
502 *uaddr = (void *)uaddr_val;
509 linux_copyin(const void *uaddr, void *kaddr, size_t len)
511 if (linux_remap_address(__DECONST(void **, &uaddr), len)) {
514 memcpy(kaddr, uaddr, len);
517 return (-copyin(uaddr, kaddr, len));
521 linux_copyout(const void *kaddr, void *uaddr, size_t len)
523 if (linux_remap_address(&uaddr, len)) {
526 memcpy(uaddr, kaddr, len);
529 return (-copyout(kaddr, uaddr, len));
533 linux_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
536 struct linux_cdev *ldev;
537 struct linux_file *filp;
538 struct task_struct t;
547 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
549 filp->f_flags = file->f_flag;
550 linux_set_current(td, &t);
551 size = IOCPARM_LEN(cmd);
552 /* refer to logic in sys_ioctl() */
555 * Setup hint for linux_copyin() and linux_copyout().
557 * Background: Linux code expects a user-space address
558 * while FreeBSD supplies a kernel-space address.
560 t.bsd_ioctl_data = data;
561 t.bsd_ioctl_len = size;
562 data = (void *)LINUX_IOCTL_MIN_PTR;
564 /* fetch user-space pointer */
565 data = *(void **)data;
567 if (filp->f_op->unlocked_ioctl)
568 error = -filp->f_op->unlocked_ioctl(filp, cmd, (u_long)data);
571 linux_clear_current(td);
577 linux_dev_read(struct cdev *dev, struct uio *uio, int ioflag)
579 struct linux_cdev *ldev;
580 struct linux_file *filp;
581 struct task_struct t;
592 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
594 filp->f_flags = file->f_flag;
595 /* XXX no support for I/O vectors currently */
596 if (uio->uio_iovcnt != 1)
598 linux_set_current(td, &t);
599 if (filp->f_op->read) {
600 bytes = filp->f_op->read(filp, uio->uio_iov->iov_base,
601 uio->uio_iov->iov_len, &uio->uio_offset);
603 uio->uio_iov->iov_base =
604 ((uint8_t *)uio->uio_iov->iov_base) + bytes;
605 uio->uio_iov->iov_len -= bytes;
606 uio->uio_resid -= bytes;
611 linux_clear_current(td);
617 linux_dev_write(struct cdev *dev, struct uio *uio, int ioflag)
619 struct linux_cdev *ldev;
620 struct linux_file *filp;
621 struct task_struct t;
632 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
634 filp->f_flags = file->f_flag;
635 /* XXX no support for I/O vectors currently */
636 if (uio->uio_iovcnt != 1)
638 linux_set_current(td, &t);
639 if (filp->f_op->write) {
640 bytes = filp->f_op->write(filp, uio->uio_iov->iov_base,
641 uio->uio_iov->iov_len, &uio->uio_offset);
643 uio->uio_iov->iov_base =
644 ((uint8_t *)uio->uio_iov->iov_base) + bytes;
645 uio->uio_iov->iov_len -= bytes;
646 uio->uio_resid -= bytes;
651 linux_clear_current(td);
657 linux_dev_poll(struct cdev *dev, int events, struct thread *td)
659 struct linux_cdev *ldev;
660 struct linux_file *filp;
661 struct task_struct t;
670 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
672 filp->f_flags = file->f_flag;
673 linux_set_current(td, &t);
674 if (filp->f_op->poll)
675 revents = filp->f_op->poll(filp, NULL) & events;
678 linux_clear_current(td);
684 linux_dev_mmap_single(struct cdev *dev, vm_ooffset_t *offset,
685 vm_size_t size, struct vm_object **object, int nprot)
687 struct linux_cdev *ldev;
688 struct linux_file *filp;
690 struct task_struct t;
692 struct vm_area_struct vma;
700 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
702 filp->f_flags = file->f_flag;
703 linux_set_current(td, &t);
706 vma.vm_pgoff = *offset / PAGE_SIZE;
708 vma.vm_page_prot = VM_MEMATTR_DEFAULT;
709 if (filp->f_op->mmap) {
710 error = -filp->f_op->mmap(filp, &vma);
714 sg = sglist_alloc(1, M_WAITOK);
715 sglist_append_phys(sg,
716 (vm_paddr_t)vma.vm_pfn << PAGE_SHIFT, vma.vm_len);
717 *object = vm_pager_allocate(OBJT_SG, sg, vma.vm_len,
718 nprot, 0, td->td_ucred);
719 if (*object == NULL) {
725 if (vma.vm_page_prot != VM_MEMATTR_DEFAULT) {
726 VM_OBJECT_WLOCK(*object);
727 vm_object_set_memattr(*object,
729 VM_OBJECT_WUNLOCK(*object);
735 linux_clear_current(td);
739 struct cdevsw linuxcdevsw = {
740 .d_version = D_VERSION,
741 .d_flags = D_TRACKCLOSE,
742 .d_open = linux_dev_open,
743 .d_close = linux_dev_close,
744 .d_read = linux_dev_read,
745 .d_write = linux_dev_write,
746 .d_ioctl = linux_dev_ioctl,
747 .d_mmap_single = linux_dev_mmap_single,
748 .d_poll = linux_dev_poll,
752 linux_file_read(struct file *file, struct uio *uio, struct ucred *active_cred,
753 int flags, struct thread *td)
755 struct linux_file *filp;
756 struct task_struct t;
761 filp = (struct linux_file *)file->f_data;
762 filp->f_flags = file->f_flag;
763 /* XXX no support for I/O vectors currently */
764 if (uio->uio_iovcnt != 1)
766 linux_set_current(td, &t);
767 if (filp->f_op->read) {
768 bytes = filp->f_op->read(filp, uio->uio_iov->iov_base,
769 uio->uio_iov->iov_len, &uio->uio_offset);
771 uio->uio_iov->iov_base =
772 ((uint8_t *)uio->uio_iov->iov_base) + bytes;
773 uio->uio_iov->iov_len -= bytes;
774 uio->uio_resid -= bytes;
779 linux_clear_current(td);
785 linux_file_poll(struct file *file, int events, struct ucred *active_cred,
788 struct linux_file *filp;
789 struct task_struct t;
792 filp = (struct linux_file *)file->f_data;
793 filp->f_flags = file->f_flag;
794 linux_set_current(td, &t);
795 if (filp->f_op->poll)
796 revents = filp->f_op->poll(filp, NULL) & events;
799 linux_clear_current(td);
805 linux_file_close(struct file *file, struct thread *td)
807 struct linux_file *filp;
808 struct task_struct t;
811 filp = (struct linux_file *)file->f_data;
812 filp->f_flags = file->f_flag;
813 linux_set_current(td, &t);
814 error = -filp->f_op->release(NULL, filp);
815 linux_clear_current(td);
816 funsetown(&filp->f_sigio);
823 linux_file_ioctl(struct file *fp, u_long cmd, void *data, struct ucred *cred,
826 struct linux_file *filp;
827 struct task_struct t;
830 filp = (struct linux_file *)fp->f_data;
831 filp->f_flags = fp->f_flag;
834 linux_set_current(td, &t);
839 if (filp->f_op->fasync == NULL)
841 error = filp->f_op->fasync(0, filp, fp->f_flag & FASYNC);
844 error = fsetown(*(int *)data, &filp->f_sigio);
846 error = filp->f_op->fasync(0, filp,
847 fp->f_flag & FASYNC);
850 *(int *)data = fgetown(&filp->f_sigio);
856 linux_clear_current(td);
861 linux_file_stat(struct file *fp, struct stat *sb, struct ucred *active_cred,
869 linux_file_fill_kinfo(struct file *fp, struct kinfo_file *kif,
870 struct filedesc *fdp)
876 struct fileops linuxfileops = {
877 .fo_read = linux_file_read,
878 .fo_write = invfo_rdwr,
879 .fo_truncate = invfo_truncate,
880 .fo_kqfilter = invfo_kqfilter,
881 .fo_stat = linux_file_stat,
882 .fo_fill_kinfo = linux_file_fill_kinfo,
883 .fo_poll = linux_file_poll,
884 .fo_close = linux_file_close,
885 .fo_ioctl = linux_file_ioctl,
886 .fo_chmod = invfo_chmod,
887 .fo_chown = invfo_chown,
888 .fo_sendfile = invfo_sendfile,
892 * Hash of vmmap addresses. This is infrequently accessed and does not
893 * need to be particularly large. This is done because we must store the
894 * caller's idea of the map size to properly unmap.
897 LIST_ENTRY(vmmap) vm_next;
899 unsigned long vm_size;
903 struct vmmap *lh_first;
905 #define VMMAP_HASH_SIZE 64
906 #define VMMAP_HASH_MASK (VMMAP_HASH_SIZE - 1)
907 #define VM_HASH(addr) ((uintptr_t)(addr) >> PAGE_SHIFT) & VMMAP_HASH_MASK
908 static struct vmmaphd vmmaphead[VMMAP_HASH_SIZE];
909 static struct mtx vmmaplock;
912 vmmap_add(void *addr, unsigned long size)
916 vmmap = kmalloc(sizeof(*vmmap), GFP_KERNEL);
917 mtx_lock(&vmmaplock);
918 vmmap->vm_size = size;
919 vmmap->vm_addr = addr;
920 LIST_INSERT_HEAD(&vmmaphead[VM_HASH(addr)], vmmap, vm_next);
921 mtx_unlock(&vmmaplock);
924 static struct vmmap *
925 vmmap_remove(void *addr)
929 mtx_lock(&vmmaplock);
930 LIST_FOREACH(vmmap, &vmmaphead[VM_HASH(addr)], vm_next)
931 if (vmmap->vm_addr == addr)
934 LIST_REMOVE(vmmap, vm_next);
935 mtx_unlock(&vmmaplock);
940 #if defined(__i386__) || defined(__amd64__)
942 _ioremap_attr(vm_paddr_t phys_addr, unsigned long size, int attr)
946 addr = pmap_mapdev_attr(phys_addr, size, attr);
949 vmmap_add(addr, size);
960 vmmap = vmmap_remove(addr);
963 #if defined(__i386__) || defined(__amd64__)
964 pmap_unmapdev((vm_offset_t)addr, vmmap->vm_size);
971 vmap(struct page **pages, unsigned int count, unsigned long flags, int prot)
976 size = count * PAGE_SIZE;
977 off = kva_alloc(size);
980 vmmap_add((void *)off, size);
981 pmap_qenter(off, pages, count);
983 return ((void *)off);
991 vmmap = vmmap_remove(addr);
994 pmap_qremove((vm_offset_t)addr, vmmap->vm_size / PAGE_SIZE);
995 kva_free((vm_offset_t)addr, vmmap->vm_size);
1000 kvasprintf(gfp_t gfp, const char *fmt, va_list ap)
1007 len = vsnprintf(NULL, 0, fmt, aq);
1010 p = kmalloc(len + 1, gfp);
1012 vsnprintf(p, len + 1, fmt, ap);
1018 kasprintf(gfp_t gfp, const char *fmt, ...)
1024 p = kvasprintf(gfp, fmt, ap);
1031 linux_timer_callback_wrapper(void *context)
1033 struct timer_list *timer;
1036 timer->function(timer->data);
1040 mod_timer(struct timer_list *timer, unsigned long expires)
1043 timer->expires = expires;
1044 callout_reset(&timer->timer_callout,
1045 linux_timer_jiffies_until(expires),
1046 &linux_timer_callback_wrapper, timer);
1050 add_timer(struct timer_list *timer)
1053 callout_reset(&timer->timer_callout,
1054 linux_timer_jiffies_until(timer->expires),
1055 &linux_timer_callback_wrapper, timer);
1059 linux_timer_init(void *arg)
1063 * Compute an internal HZ value which can divide 2**32 to
1064 * avoid timer rounding problems when the tick value wraps
1067 linux_timer_hz_mask = 1;
1068 while (linux_timer_hz_mask < (unsigned long)hz)
1069 linux_timer_hz_mask *= 2;
1070 linux_timer_hz_mask--;
1072 SYSINIT(linux_timer, SI_SUB_DRIVERS, SI_ORDER_FIRST, linux_timer_init, NULL);
1075 linux_complete_common(struct completion *c, int all)
1082 wakeup_swapper = sleepq_broadcast(c, SLEEPQ_SLEEP, 0, 0);
1084 wakeup_swapper = sleepq_signal(c, SLEEPQ_SLEEP, 0, 0);
1091 * Indefinite wait for done != 0 with or without signals.
1094 linux_wait_for_common(struct completion *c, int flags)
1098 flags = SLEEPQ_INTERRUPTIBLE | SLEEPQ_SLEEP;
1100 flags = SLEEPQ_SLEEP;
1105 sleepq_add(c, NULL, "completion", flags, 0);
1106 if (flags & SLEEPQ_INTERRUPTIBLE) {
1107 if (sleepq_wait_sig(c, 0) != 0)
1108 return (-ERESTARTSYS);
1119 * Time limited wait for done != 0 with or without signals.
1122 linux_wait_for_timeout_common(struct completion *c, long timeout, int flags)
1124 long end = jiffies + timeout;
1127 flags = SLEEPQ_INTERRUPTIBLE | SLEEPQ_SLEEP;
1129 flags = SLEEPQ_SLEEP;
1136 sleepq_add(c, NULL, "completion", flags, 0);
1137 sleepq_set_timeout(c, linux_timer_jiffies_until(end));
1138 if (flags & SLEEPQ_INTERRUPTIBLE)
1139 ret = sleepq_timedwait_sig(c, 0);
1141 ret = sleepq_timedwait(c, 0);
1143 /* check for timeout or signal */
1144 if (ret == EWOULDBLOCK)
1147 return (-ERESTARTSYS);
1153 /* return how many jiffies are left */
1154 return (linux_timer_jiffies_until(end));
1158 linux_try_wait_for_completion(struct completion *c)
1173 linux_completion_done(struct completion *c)
1186 linux_delayed_work_fn(void *arg)
1188 struct delayed_work *work;
1191 taskqueue_enqueue(work->work.taskqueue, &work->work.work_task);
1195 linux_work_fn(void *context, int pending)
1197 struct work_struct *work;
1204 linux_flush_fn(void *context, int pending)
1208 struct workqueue_struct *
1209 linux_create_workqueue_common(const char *name, int cpus)
1211 struct workqueue_struct *wq;
1213 wq = kmalloc(sizeof(*wq), M_WAITOK);
1214 wq->taskqueue = taskqueue_create(name, M_WAITOK,
1215 taskqueue_thread_enqueue, &wq->taskqueue);
1216 atomic_set(&wq->draining, 0);
1217 taskqueue_start_threads(&wq->taskqueue, cpus, PWAIT, "%s", name);
1223 destroy_workqueue(struct workqueue_struct *wq)
1225 taskqueue_free(wq->taskqueue);
1230 linux_cdev_release(struct kobject *kobj)
1232 struct linux_cdev *cdev;
1233 struct kobject *parent;
1235 cdev = container_of(kobj, struct linux_cdev, kobj);
1236 parent = kobj->parent;
1238 destroy_dev(cdev->cdev);
1240 kobject_put(parent);
1244 linux_cdev_static_release(struct kobject *kobj)
1246 struct linux_cdev *cdev;
1247 struct kobject *parent;
1249 cdev = container_of(kobj, struct linux_cdev, kobj);
1250 parent = kobj->parent;
1252 destroy_dev(cdev->cdev);
1253 kobject_put(parent);
1256 const struct kobj_type linux_cdev_ktype = {
1257 .release = linux_cdev_release,
1260 const struct kobj_type linux_cdev_static_ktype = {
1261 .release = linux_cdev_static_release,
1265 linux_handle_ifnet_link_event(void *arg, struct ifnet *ifp, int linkstate)
1267 struct notifier_block *nb;
1270 if (linkstate == LINK_STATE_UP)
1271 nb->notifier_call(nb, NETDEV_UP, ifp);
1273 nb->notifier_call(nb, NETDEV_DOWN, ifp);
1277 linux_handle_ifnet_arrival_event(void *arg, struct ifnet *ifp)
1279 struct notifier_block *nb;
1282 nb->notifier_call(nb, NETDEV_REGISTER, ifp);
1286 linux_handle_ifnet_departure_event(void *arg, struct ifnet *ifp)
1288 struct notifier_block *nb;
1291 nb->notifier_call(nb, NETDEV_UNREGISTER, ifp);
1295 linux_handle_iflladdr_event(void *arg, struct ifnet *ifp)
1297 struct notifier_block *nb;
1300 nb->notifier_call(nb, NETDEV_CHANGEADDR, ifp);
1304 linux_handle_ifaddr_event(void *arg, struct ifnet *ifp)
1306 struct notifier_block *nb;
1309 nb->notifier_call(nb, NETDEV_CHANGEIFADDR, ifp);
1313 register_netdevice_notifier(struct notifier_block *nb)
1316 nb->tags[NETDEV_UP] = EVENTHANDLER_REGISTER(
1317 ifnet_link_event, linux_handle_ifnet_link_event, nb, 0);
1318 nb->tags[NETDEV_REGISTER] = EVENTHANDLER_REGISTER(
1319 ifnet_arrival_event, linux_handle_ifnet_arrival_event, nb, 0);
1320 nb->tags[NETDEV_UNREGISTER] = EVENTHANDLER_REGISTER(
1321 ifnet_departure_event, linux_handle_ifnet_departure_event, nb, 0);
1322 nb->tags[NETDEV_CHANGEADDR] = EVENTHANDLER_REGISTER(
1323 iflladdr_event, linux_handle_iflladdr_event, nb, 0);
1329 register_inetaddr_notifier(struct notifier_block *nb)
1332 nb->tags[NETDEV_CHANGEIFADDR] = EVENTHANDLER_REGISTER(
1333 ifaddr_event, linux_handle_ifaddr_event, nb, 0);
1338 unregister_netdevice_notifier(struct notifier_block *nb)
1341 EVENTHANDLER_DEREGISTER(ifnet_link_event,
1342 nb->tags[NETDEV_UP]);
1343 EVENTHANDLER_DEREGISTER(ifnet_arrival_event,
1344 nb->tags[NETDEV_REGISTER]);
1345 EVENTHANDLER_DEREGISTER(ifnet_departure_event,
1346 nb->tags[NETDEV_UNREGISTER]);
1347 EVENTHANDLER_DEREGISTER(iflladdr_event,
1348 nb->tags[NETDEV_CHANGEADDR]);
1354 unregister_inetaddr_notifier(struct notifier_block *nb)
1357 EVENTHANDLER_DEREGISTER(ifaddr_event,
1358 nb->tags[NETDEV_CHANGEIFADDR]);
1363 struct list_sort_thunk {
1364 int (*cmp)(void *, struct list_head *, struct list_head *);
1369 linux_le_cmp(void *priv, const void *d1, const void *d2)
1371 struct list_head *le1, *le2;
1372 struct list_sort_thunk *thunk;
1375 le1 = *(__DECONST(struct list_head **, d1));
1376 le2 = *(__DECONST(struct list_head **, d2));
1377 return ((thunk->cmp)(thunk->priv, le1, le2));
1381 list_sort(void *priv, struct list_head *head, int (*cmp)(void *priv,
1382 struct list_head *a, struct list_head *b))
1384 struct list_sort_thunk thunk;
1385 struct list_head **ar, *le;
1389 list_for_each(le, head)
1391 ar = malloc(sizeof(struct list_head *) * count, M_KMALLOC, M_WAITOK);
1393 list_for_each(le, head)
1397 qsort_r(ar, count, sizeof(struct list_head *), &thunk, linux_le_cmp);
1398 INIT_LIST_HEAD(head);
1399 for (i = 0; i < count; i++)
1400 list_add_tail(ar[i], head);
1401 free(ar, M_KMALLOC);
1405 linux_irq_handler(void *ent)
1407 struct irq_ent *irqe;
1410 irqe->handler(irqe->irq, irqe->arg);
1413 #if defined(__i386__) || defined(__amd64__)
1414 bool linux_cpu_has_clflush;
1418 linux_compat_init(void *arg)
1420 struct sysctl_oid *rootoid;
1423 #if defined(__i386__) || defined(__amd64__)
1424 linux_cpu_has_clflush = (cpu_feature & CPUID_CLFSH);
1426 sx_init(&linux_global_rcu_lock, "LinuxGlobalRCU");
1428 rootoid = SYSCTL_ADD_ROOT_NODE(NULL,
1429 OID_AUTO, "sys", CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, "sys");
1430 kobject_init(&linux_class_root, &linux_class_ktype);
1431 kobject_set_name(&linux_class_root, "class");
1432 linux_class_root.oidp = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(rootoid),
1433 OID_AUTO, "class", CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, "class");
1434 kobject_init(&linux_root_device.kobj, &linux_dev_ktype);
1435 kobject_set_name(&linux_root_device.kobj, "device");
1436 linux_root_device.kobj.oidp = SYSCTL_ADD_NODE(NULL,
1437 SYSCTL_CHILDREN(rootoid), OID_AUTO, "device", CTLFLAG_RD, NULL,
1439 linux_root_device.bsddev = root_bus;
1440 linux_class_misc.name = "misc";
1441 class_register(&linux_class_misc);
1442 INIT_LIST_HEAD(&pci_drivers);
1443 INIT_LIST_HEAD(&pci_devices);
1444 spin_lock_init(&pci_lock);
1445 mtx_init(&vmmaplock, "IO Map lock", NULL, MTX_DEF);
1446 for (i = 0; i < VMMAP_HASH_SIZE; i++)
1447 LIST_INIT(&vmmaphead[i]);
1449 SYSINIT(linux_compat, SI_SUB_DRIVERS, SI_ORDER_SECOND, linux_compat_init, NULL);
1452 linux_compat_uninit(void *arg)
1454 linux_kobject_kfree_name(&linux_class_root);
1455 linux_kobject_kfree_name(&linux_root_device.kobj);
1456 linux_kobject_kfree_name(&linux_class_misc.kobj);
1459 sx_destroy(&linux_global_rcu_lock);
1461 SYSUNINIT(linux_compat, SI_SUB_DRIVERS, SI_ORDER_SECOND, linux_compat_uninit, NULL);
1464 * NOTE: Linux frequently uses "unsigned long" for pointer to integer
1465 * conversion and vice versa, where in FreeBSD "uintptr_t" would be
1466 * used. Assert these types have the same size, else some parts of the
1467 * LinuxKPI may not work like expected:
1469 CTASSERT(sizeof(unsigned long) == sizeof(uintptr_t));