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/moduleparam.h>
63 #include <linux/cdev.h>
64 #include <linux/file.h>
65 #include <linux/sysfs.h>
68 #include <linux/vmalloc.h>
69 #include <linux/netdevice.h>
70 #include <linux/timer.h>
71 #include <linux/workqueue.h>
72 #include <linux/rcupdate.h>
73 #include <linux/interrupt.h>
74 #include <linux/uaccess.h>
75 #include <linux/kernel.h>
76 #include <linux/list.h>
77 #include <linux/compat.h>
79 #include <vm/vm_pager.h>
81 SYSCTL_NODE(_compat, OID_AUTO, linuxkpi, CTLFLAG_RW, 0, "LinuxKPI parameters");
83 MALLOC_DEFINE(M_KMALLOC, "linux", "Linux kmalloc compat");
85 #include <linux/rbtree.h>
86 /* Undo Linux compat changes. */
90 #define RB_ROOT(head) (head)->rbh_root
92 struct kobject linux_class_root;
93 struct device linux_root_device;
94 struct class linux_class_misc;
95 struct list_head pci_drivers;
96 struct list_head pci_devices;
99 struct sx linux_global_rcu_lock;
101 unsigned long linux_timer_hz_mask;
104 panic_cmp(struct rb_node *one, struct rb_node *two)
109 RB_GENERATE(linux_root, rb_node, __entry, panic_cmp);
112 kobject_set_name_vargs(struct kobject *kobj, const char *fmt, va_list args)
122 if (old && fmt == NULL)
125 /* compute length of string */
126 va_copy(tmp_va, args);
127 len = vsnprintf(&dummy, 0, fmt, tmp_va);
130 /* account for zero termination */
133 /* check for error */
137 /* allocate memory for string */
138 name = kzalloc(len, GFP_KERNEL);
141 vsnprintf(name, len, fmt, args);
144 /* free old string */
147 /* filter new string */
148 for (; *name != '\0'; name++)
155 kobject_set_name(struct kobject *kobj, const char *fmt, ...)
161 error = kobject_set_name_vargs(kobj, fmt, args);
168 kobject_add_complete(struct kobject *kobj, struct kobject *parent)
170 const struct kobj_type *t;
173 kobj->parent = parent;
174 error = sysfs_create_dir(kobj);
175 if (error == 0 && kobj->ktype && kobj->ktype->default_attrs) {
176 struct attribute **attr;
179 for (attr = t->default_attrs; *attr != NULL; attr++) {
180 error = sysfs_create_file(kobj, *attr);
185 sysfs_remove_dir(kobj);
192 kobject_add(struct kobject *kobj, struct kobject *parent, const char *fmt, ...)
198 error = kobject_set_name_vargs(kobj, fmt, args);
203 return kobject_add_complete(kobj, parent);
207 linux_kobject_release(struct kref *kref)
209 struct kobject *kobj;
212 kobj = container_of(kref, struct kobject, kref);
213 sysfs_remove_dir(kobj);
215 if (kobj->ktype && kobj->ktype->release)
216 kobj->ktype->release(kobj);
221 linux_kobject_kfree(struct kobject *kobj)
227 linux_kobject_kfree_name(struct kobject *kobj)
234 const struct kobj_type linux_kfree_type = {
235 .release = linux_kobject_kfree
239 linux_device_release(struct device *dev)
241 pr_debug("linux_device_release: %s\n", dev_name(dev));
246 linux_class_show(struct kobject *kobj, struct attribute *attr, char *buf)
248 struct class_attribute *dattr;
251 dattr = container_of(attr, struct class_attribute, attr);
254 error = dattr->show(container_of(kobj, struct class, kobj),
260 linux_class_store(struct kobject *kobj, struct attribute *attr, const char *buf,
263 struct class_attribute *dattr;
266 dattr = container_of(attr, struct class_attribute, attr);
269 error = dattr->store(container_of(kobj, struct class, kobj),
275 linux_class_release(struct kobject *kobj)
279 class = container_of(kobj, struct class, kobj);
280 if (class->class_release)
281 class->class_release(class);
284 static const struct sysfs_ops linux_class_sysfs = {
285 .show = linux_class_show,
286 .store = linux_class_store,
289 const struct kobj_type linux_class_ktype = {
290 .release = linux_class_release,
291 .sysfs_ops = &linux_class_sysfs
295 linux_dev_release(struct kobject *kobj)
299 dev = container_of(kobj, struct device, kobj);
300 /* This is the precedence defined by linux. */
303 else if (dev->class && dev->class->dev_release)
304 dev->class->dev_release(dev);
308 linux_dev_show(struct kobject *kobj, struct attribute *attr, char *buf)
310 struct device_attribute *dattr;
313 dattr = container_of(attr, struct device_attribute, attr);
316 error = dattr->show(container_of(kobj, struct device, kobj),
322 linux_dev_store(struct kobject *kobj, struct attribute *attr, const char *buf,
325 struct device_attribute *dattr;
328 dattr = container_of(attr, struct device_attribute, attr);
331 error = dattr->store(container_of(kobj, struct device, kobj),
336 static const struct sysfs_ops linux_dev_sysfs = {
337 .show = linux_dev_show,
338 .store = linux_dev_store,
341 const struct kobj_type linux_dev_ktype = {
342 .release = linux_dev_release,
343 .sysfs_ops = &linux_dev_sysfs
347 device_create(struct class *class, struct device *parent, dev_t devt,
348 void *drvdata, const char *fmt, ...)
353 dev = kzalloc(sizeof(*dev), M_WAITOK);
354 dev->parent = parent;
357 dev->driver_data = drvdata;
358 dev->release = linux_device_release;
360 kobject_set_name_vargs(&dev->kobj, fmt, args);
362 device_register(dev);
368 kobject_init_and_add(struct kobject *kobj, const struct kobj_type *ktype,
369 struct kobject *parent, const char *fmt, ...)
374 kobject_init(kobj, ktype);
376 kobj->parent = parent;
380 error = kobject_set_name_vargs(kobj, fmt, args);
384 return kobject_add_complete(kobj, parent);
388 linux_set_current(struct thread *td, struct task_struct *t)
390 memset(t, 0, sizeof(*t));
391 task_struct_fill(td, t);
392 task_struct_set(td, t);
396 linux_clear_current(struct thread *td)
398 task_struct_set(td, NULL);
402 linux_file_dtor(void *cdp)
404 struct linux_file *filp;
405 struct task_struct t;
410 linux_set_current(td, &t);
411 filp->f_op->release(filp->f_vnode, filp);
412 linux_clear_current(td);
413 vdrop(filp->f_vnode);
418 linux_dev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
420 struct linux_cdev *ldev;
421 struct linux_file *filp;
422 struct task_struct t;
430 filp = kzalloc(sizeof(*filp), GFP_KERNEL);
431 filp->f_dentry = &filp->f_dentry_store;
432 filp->f_op = ldev->ops;
433 filp->f_flags = file->f_flag;
434 vhold(file->f_vnode);
435 filp->f_vnode = file->f_vnode;
436 linux_set_current(td, &t);
437 if (filp->f_op->open) {
438 error = -filp->f_op->open(file->f_vnode, filp);
444 error = devfs_set_cdevpriv(filp, linux_file_dtor);
446 filp->f_op->release(file->f_vnode, filp);
450 linux_clear_current(td);
455 linux_dev_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
457 struct linux_cdev *ldev;
458 struct linux_file *filp;
466 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
468 filp->f_flags = file->f_flag;
469 devfs_clear_cdevpriv();
475 #define LINUX_IOCTL_MIN_PTR 0x10000UL
476 #define LINUX_IOCTL_MAX_PTR (LINUX_IOCTL_MIN_PTR + IOCPARM_MAX)
479 linux_remap_address(void **uaddr, size_t len)
481 uintptr_t uaddr_val = (uintptr_t)(*uaddr);
483 if (unlikely(uaddr_val >= LINUX_IOCTL_MIN_PTR &&
484 uaddr_val < LINUX_IOCTL_MAX_PTR)) {
485 struct task_struct *pts = current;
491 /* compute data offset */
492 uaddr_val -= LINUX_IOCTL_MIN_PTR;
494 /* check that length is within bounds */
495 if ((len > IOCPARM_MAX) ||
496 (uaddr_val + len) > pts->bsd_ioctl_len) {
501 /* re-add kernel buffer address */
502 uaddr_val += (uintptr_t)pts->bsd_ioctl_data;
504 /* update address location */
505 *uaddr = (void *)uaddr_val;
512 linux_copyin(const void *uaddr, void *kaddr, size_t len)
514 if (linux_remap_address(__DECONST(void **, &uaddr), len)) {
517 memcpy(kaddr, uaddr, len);
520 return (-copyin(uaddr, kaddr, len));
524 linux_copyout(const void *kaddr, void *uaddr, size_t len)
526 if (linux_remap_address(&uaddr, len)) {
529 memcpy(uaddr, kaddr, len);
532 return (-copyout(kaddr, uaddr, len));
536 linux_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
539 struct linux_cdev *ldev;
540 struct linux_file *filp;
541 struct task_struct t;
550 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
552 filp->f_flags = file->f_flag;
553 linux_set_current(td, &t);
554 size = IOCPARM_LEN(cmd);
555 /* refer to logic in sys_ioctl() */
558 * Setup hint for linux_copyin() and linux_copyout().
560 * Background: Linux code expects a user-space address
561 * while FreeBSD supplies a kernel-space address.
563 t.bsd_ioctl_data = data;
564 t.bsd_ioctl_len = size;
565 data = (void *)LINUX_IOCTL_MIN_PTR;
567 /* fetch user-space pointer */
568 data = *(void **)data;
570 if (filp->f_op->unlocked_ioctl)
571 error = -filp->f_op->unlocked_ioctl(filp, cmd, (u_long)data);
574 linux_clear_current(td);
580 linux_dev_read(struct cdev *dev, struct uio *uio, int ioflag)
582 struct linux_cdev *ldev;
583 struct linux_file *filp;
584 struct task_struct t;
595 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
597 filp->f_flags = file->f_flag;
598 /* XXX no support for I/O vectors currently */
599 if (uio->uio_iovcnt != 1)
601 linux_set_current(td, &t);
602 if (filp->f_op->read) {
603 bytes = filp->f_op->read(filp, uio->uio_iov->iov_base,
604 uio->uio_iov->iov_len, &uio->uio_offset);
606 uio->uio_iov->iov_base =
607 ((uint8_t *)uio->uio_iov->iov_base) + bytes;
608 uio->uio_iov->iov_len -= bytes;
609 uio->uio_resid -= bytes;
614 linux_clear_current(td);
620 linux_dev_write(struct cdev *dev, struct uio *uio, int ioflag)
622 struct linux_cdev *ldev;
623 struct linux_file *filp;
624 struct task_struct t;
635 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
637 filp->f_flags = file->f_flag;
638 /* XXX no support for I/O vectors currently */
639 if (uio->uio_iovcnt != 1)
641 linux_set_current(td, &t);
642 if (filp->f_op->write) {
643 bytes = filp->f_op->write(filp, uio->uio_iov->iov_base,
644 uio->uio_iov->iov_len, &uio->uio_offset);
646 uio->uio_iov->iov_base =
647 ((uint8_t *)uio->uio_iov->iov_base) + bytes;
648 uio->uio_iov->iov_len -= bytes;
649 uio->uio_resid -= bytes;
654 linux_clear_current(td);
660 linux_dev_poll(struct cdev *dev, int events, struct thread *td)
662 struct linux_cdev *ldev;
663 struct linux_file *filp;
664 struct task_struct t;
673 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
675 filp->f_flags = file->f_flag;
676 linux_set_current(td, &t);
677 if (filp->f_op->poll)
678 revents = filp->f_op->poll(filp, NULL) & events;
681 linux_clear_current(td);
687 linux_dev_mmap_single(struct cdev *dev, vm_ooffset_t *offset,
688 vm_size_t size, struct vm_object **object, int nprot)
690 struct linux_cdev *ldev;
691 struct linux_file *filp;
693 struct task_struct t;
695 struct vm_area_struct vma;
703 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
705 filp->f_flags = file->f_flag;
706 linux_set_current(td, &t);
709 vma.vm_pgoff = *offset / PAGE_SIZE;
711 vma.vm_page_prot = VM_MEMATTR_DEFAULT;
712 if (filp->f_op->mmap) {
713 error = -filp->f_op->mmap(filp, &vma);
717 sg = sglist_alloc(1, M_WAITOK);
718 sglist_append_phys(sg,
719 (vm_paddr_t)vma.vm_pfn << PAGE_SHIFT, vma.vm_len);
720 *object = vm_pager_allocate(OBJT_SG, sg, vma.vm_len,
721 nprot, 0, td->td_ucred);
722 if (*object == NULL) {
728 if (vma.vm_page_prot != VM_MEMATTR_DEFAULT) {
729 VM_OBJECT_WLOCK(*object);
730 vm_object_set_memattr(*object,
732 VM_OBJECT_WUNLOCK(*object);
738 linux_clear_current(td);
742 struct cdevsw linuxcdevsw = {
743 .d_version = D_VERSION,
744 .d_flags = D_TRACKCLOSE,
745 .d_open = linux_dev_open,
746 .d_close = linux_dev_close,
747 .d_read = linux_dev_read,
748 .d_write = linux_dev_write,
749 .d_ioctl = linux_dev_ioctl,
750 .d_mmap_single = linux_dev_mmap_single,
751 .d_poll = linux_dev_poll,
755 linux_file_read(struct file *file, struct uio *uio, struct ucred *active_cred,
756 int flags, struct thread *td)
758 struct linux_file *filp;
759 struct task_struct t;
764 filp = (struct linux_file *)file->f_data;
765 filp->f_flags = file->f_flag;
766 /* XXX no support for I/O vectors currently */
767 if (uio->uio_iovcnt != 1)
769 linux_set_current(td, &t);
770 if (filp->f_op->read) {
771 bytes = filp->f_op->read(filp, uio->uio_iov->iov_base,
772 uio->uio_iov->iov_len, &uio->uio_offset);
774 uio->uio_iov->iov_base =
775 ((uint8_t *)uio->uio_iov->iov_base) + bytes;
776 uio->uio_iov->iov_len -= bytes;
777 uio->uio_resid -= bytes;
782 linux_clear_current(td);
788 linux_file_poll(struct file *file, int events, struct ucred *active_cred,
791 struct linux_file *filp;
792 struct task_struct t;
795 filp = (struct linux_file *)file->f_data;
796 filp->f_flags = file->f_flag;
797 linux_set_current(td, &t);
798 if (filp->f_op->poll)
799 revents = filp->f_op->poll(filp, NULL) & events;
802 linux_clear_current(td);
808 linux_file_close(struct file *file, struct thread *td)
810 struct linux_file *filp;
811 struct task_struct t;
814 filp = (struct linux_file *)file->f_data;
815 filp->f_flags = file->f_flag;
816 linux_set_current(td, &t);
817 error = -filp->f_op->release(NULL, filp);
818 linux_clear_current(td);
819 funsetown(&filp->f_sigio);
826 linux_file_ioctl(struct file *fp, u_long cmd, void *data, struct ucred *cred,
829 struct linux_file *filp;
830 struct task_struct t;
833 filp = (struct linux_file *)fp->f_data;
834 filp->f_flags = fp->f_flag;
837 linux_set_current(td, &t);
842 if (filp->f_op->fasync == NULL)
844 error = filp->f_op->fasync(0, filp, fp->f_flag & FASYNC);
847 error = fsetown(*(int *)data, &filp->f_sigio);
849 error = filp->f_op->fasync(0, filp,
850 fp->f_flag & FASYNC);
853 *(int *)data = fgetown(&filp->f_sigio);
859 linux_clear_current(td);
864 linux_file_stat(struct file *fp, struct stat *sb, struct ucred *active_cred,
872 linux_file_fill_kinfo(struct file *fp, struct kinfo_file *kif,
873 struct filedesc *fdp)
879 struct fileops linuxfileops = {
880 .fo_read = linux_file_read,
881 .fo_write = invfo_rdwr,
882 .fo_truncate = invfo_truncate,
883 .fo_kqfilter = invfo_kqfilter,
884 .fo_stat = linux_file_stat,
885 .fo_fill_kinfo = linux_file_fill_kinfo,
886 .fo_poll = linux_file_poll,
887 .fo_close = linux_file_close,
888 .fo_ioctl = linux_file_ioctl,
889 .fo_chmod = invfo_chmod,
890 .fo_chown = invfo_chown,
891 .fo_sendfile = invfo_sendfile,
895 * Hash of vmmap addresses. This is infrequently accessed and does not
896 * need to be particularly large. This is done because we must store the
897 * caller's idea of the map size to properly unmap.
900 LIST_ENTRY(vmmap) vm_next;
902 unsigned long vm_size;
906 struct vmmap *lh_first;
908 #define VMMAP_HASH_SIZE 64
909 #define VMMAP_HASH_MASK (VMMAP_HASH_SIZE - 1)
910 #define VM_HASH(addr) ((uintptr_t)(addr) >> PAGE_SHIFT) & VMMAP_HASH_MASK
911 static struct vmmaphd vmmaphead[VMMAP_HASH_SIZE];
912 static struct mtx vmmaplock;
915 vmmap_add(void *addr, unsigned long size)
919 vmmap = kmalloc(sizeof(*vmmap), GFP_KERNEL);
920 mtx_lock(&vmmaplock);
921 vmmap->vm_size = size;
922 vmmap->vm_addr = addr;
923 LIST_INSERT_HEAD(&vmmaphead[VM_HASH(addr)], vmmap, vm_next);
924 mtx_unlock(&vmmaplock);
927 static struct vmmap *
928 vmmap_remove(void *addr)
932 mtx_lock(&vmmaplock);
933 LIST_FOREACH(vmmap, &vmmaphead[VM_HASH(addr)], vm_next)
934 if (vmmap->vm_addr == addr)
937 LIST_REMOVE(vmmap, vm_next);
938 mtx_unlock(&vmmaplock);
943 #if defined(__i386__) || defined(__amd64__)
945 _ioremap_attr(vm_paddr_t phys_addr, unsigned long size, int attr)
949 addr = pmap_mapdev_attr(phys_addr, size, attr);
952 vmmap_add(addr, size);
963 vmmap = vmmap_remove(addr);
966 #if defined(__i386__) || defined(__amd64__)
967 pmap_unmapdev((vm_offset_t)addr, vmmap->vm_size);
974 vmap(struct page **pages, unsigned int count, unsigned long flags, int prot)
979 size = count * PAGE_SIZE;
980 off = kva_alloc(size);
983 vmmap_add((void *)off, size);
984 pmap_qenter(off, pages, count);
986 return ((void *)off);
994 vmmap = vmmap_remove(addr);
997 pmap_qremove((vm_offset_t)addr, vmmap->vm_size / PAGE_SIZE);
998 kva_free((vm_offset_t)addr, vmmap->vm_size);
1003 kvasprintf(gfp_t gfp, const char *fmt, va_list ap)
1010 len = vsnprintf(NULL, 0, fmt, aq);
1013 p = kmalloc(len + 1, gfp);
1015 vsnprintf(p, len + 1, fmt, ap);
1021 kasprintf(gfp_t gfp, const char *fmt, ...)
1027 p = kvasprintf(gfp, fmt, ap);
1034 linux_timer_callback_wrapper(void *context)
1036 struct timer_list *timer;
1039 timer->function(timer->data);
1043 mod_timer(struct timer_list *timer, unsigned long expires)
1046 timer->expires = expires;
1047 callout_reset(&timer->timer_callout,
1048 linux_timer_jiffies_until(expires),
1049 &linux_timer_callback_wrapper, timer);
1053 add_timer(struct timer_list *timer)
1056 callout_reset(&timer->timer_callout,
1057 linux_timer_jiffies_until(timer->expires),
1058 &linux_timer_callback_wrapper, timer);
1062 linux_timer_init(void *arg)
1066 * Compute an internal HZ value which can divide 2**32 to
1067 * avoid timer rounding problems when the tick value wraps
1070 linux_timer_hz_mask = 1;
1071 while (linux_timer_hz_mask < (unsigned long)hz)
1072 linux_timer_hz_mask *= 2;
1073 linux_timer_hz_mask--;
1075 SYSINIT(linux_timer, SI_SUB_DRIVERS, SI_ORDER_FIRST, linux_timer_init, NULL);
1078 linux_complete_common(struct completion *c, int all)
1085 wakeup_swapper = sleepq_broadcast(c, SLEEPQ_SLEEP, 0, 0);
1087 wakeup_swapper = sleepq_signal(c, SLEEPQ_SLEEP, 0, 0);
1094 * Indefinite wait for done != 0 with or without signals.
1097 linux_wait_for_common(struct completion *c, int flags)
1099 if (unlikely(SCHEDULER_STOPPED()))
1103 flags = SLEEPQ_INTERRUPTIBLE | SLEEPQ_SLEEP;
1105 flags = SLEEPQ_SLEEP;
1110 sleepq_add(c, NULL, "completion", flags, 0);
1111 if (flags & SLEEPQ_INTERRUPTIBLE) {
1112 if (sleepq_wait_sig(c, 0) != 0)
1113 return (-ERESTARTSYS);
1124 * Time limited wait for done != 0 with or without signals.
1127 linux_wait_for_timeout_common(struct completion *c, long timeout, int flags)
1129 long end = jiffies + timeout;
1131 if (unlikely(SCHEDULER_STOPPED()))
1135 flags = SLEEPQ_INTERRUPTIBLE | SLEEPQ_SLEEP;
1137 flags = SLEEPQ_SLEEP;
1144 sleepq_add(c, NULL, "completion", flags, 0);
1145 sleepq_set_timeout(c, linux_timer_jiffies_until(end));
1146 if (flags & SLEEPQ_INTERRUPTIBLE)
1147 ret = sleepq_timedwait_sig(c, 0);
1149 ret = sleepq_timedwait(c, 0);
1151 /* check for timeout or signal */
1152 if (ret == EWOULDBLOCK)
1155 return (-ERESTARTSYS);
1161 /* return how many jiffies are left */
1162 return (linux_timer_jiffies_until(end));
1166 linux_try_wait_for_completion(struct completion *c)
1181 linux_completion_done(struct completion *c)
1194 linux_delayed_work_fn(void *arg)
1196 struct delayed_work *work;
1199 taskqueue_enqueue(work->work.taskqueue, &work->work.work_task);
1203 linux_work_fn(void *context, int pending)
1205 struct work_struct *work;
1212 linux_flush_fn(void *context, int pending)
1216 struct workqueue_struct *
1217 linux_create_workqueue_common(const char *name, int cpus)
1219 struct workqueue_struct *wq;
1221 wq = kmalloc(sizeof(*wq), M_WAITOK);
1222 wq->taskqueue = taskqueue_create(name, M_WAITOK,
1223 taskqueue_thread_enqueue, &wq->taskqueue);
1224 atomic_set(&wq->draining, 0);
1225 taskqueue_start_threads(&wq->taskqueue, cpus, PWAIT, "%s", name);
1231 destroy_workqueue(struct workqueue_struct *wq)
1233 taskqueue_free(wq->taskqueue);
1238 linux_cdev_release(struct kobject *kobj)
1240 struct linux_cdev *cdev;
1241 struct kobject *parent;
1243 cdev = container_of(kobj, struct linux_cdev, kobj);
1244 parent = kobj->parent;
1246 destroy_dev(cdev->cdev);
1248 kobject_put(parent);
1252 linux_cdev_static_release(struct kobject *kobj)
1254 struct linux_cdev *cdev;
1255 struct kobject *parent;
1257 cdev = container_of(kobj, struct linux_cdev, kobj);
1258 parent = kobj->parent;
1260 destroy_dev(cdev->cdev);
1261 kobject_put(parent);
1264 const struct kobj_type linux_cdev_ktype = {
1265 .release = linux_cdev_release,
1268 const struct kobj_type linux_cdev_static_ktype = {
1269 .release = linux_cdev_static_release,
1273 linux_handle_ifnet_link_event(void *arg, struct ifnet *ifp, int linkstate)
1275 struct notifier_block *nb;
1278 if (linkstate == LINK_STATE_UP)
1279 nb->notifier_call(nb, NETDEV_UP, ifp);
1281 nb->notifier_call(nb, NETDEV_DOWN, ifp);
1285 linux_handle_ifnet_arrival_event(void *arg, struct ifnet *ifp)
1287 struct notifier_block *nb;
1290 nb->notifier_call(nb, NETDEV_REGISTER, ifp);
1294 linux_handle_ifnet_departure_event(void *arg, struct ifnet *ifp)
1296 struct notifier_block *nb;
1299 nb->notifier_call(nb, NETDEV_UNREGISTER, ifp);
1303 linux_handle_iflladdr_event(void *arg, struct ifnet *ifp)
1305 struct notifier_block *nb;
1308 nb->notifier_call(nb, NETDEV_CHANGEADDR, ifp);
1312 linux_handle_ifaddr_event(void *arg, struct ifnet *ifp)
1314 struct notifier_block *nb;
1317 nb->notifier_call(nb, NETDEV_CHANGEIFADDR, ifp);
1321 register_netdevice_notifier(struct notifier_block *nb)
1324 nb->tags[NETDEV_UP] = EVENTHANDLER_REGISTER(
1325 ifnet_link_event, linux_handle_ifnet_link_event, nb, 0);
1326 nb->tags[NETDEV_REGISTER] = EVENTHANDLER_REGISTER(
1327 ifnet_arrival_event, linux_handle_ifnet_arrival_event, nb, 0);
1328 nb->tags[NETDEV_UNREGISTER] = EVENTHANDLER_REGISTER(
1329 ifnet_departure_event, linux_handle_ifnet_departure_event, nb, 0);
1330 nb->tags[NETDEV_CHANGEADDR] = EVENTHANDLER_REGISTER(
1331 iflladdr_event, linux_handle_iflladdr_event, nb, 0);
1337 register_inetaddr_notifier(struct notifier_block *nb)
1340 nb->tags[NETDEV_CHANGEIFADDR] = EVENTHANDLER_REGISTER(
1341 ifaddr_event, linux_handle_ifaddr_event, nb, 0);
1346 unregister_netdevice_notifier(struct notifier_block *nb)
1349 EVENTHANDLER_DEREGISTER(ifnet_link_event,
1350 nb->tags[NETDEV_UP]);
1351 EVENTHANDLER_DEREGISTER(ifnet_arrival_event,
1352 nb->tags[NETDEV_REGISTER]);
1353 EVENTHANDLER_DEREGISTER(ifnet_departure_event,
1354 nb->tags[NETDEV_UNREGISTER]);
1355 EVENTHANDLER_DEREGISTER(iflladdr_event,
1356 nb->tags[NETDEV_CHANGEADDR]);
1362 unregister_inetaddr_notifier(struct notifier_block *nb)
1365 EVENTHANDLER_DEREGISTER(ifaddr_event,
1366 nb->tags[NETDEV_CHANGEIFADDR]);
1371 struct list_sort_thunk {
1372 int (*cmp)(void *, struct list_head *, struct list_head *);
1377 linux_le_cmp(void *priv, const void *d1, const void *d2)
1379 struct list_head *le1, *le2;
1380 struct list_sort_thunk *thunk;
1383 le1 = *(__DECONST(struct list_head **, d1));
1384 le2 = *(__DECONST(struct list_head **, d2));
1385 return ((thunk->cmp)(thunk->priv, le1, le2));
1389 list_sort(void *priv, struct list_head *head, int (*cmp)(void *priv,
1390 struct list_head *a, struct list_head *b))
1392 struct list_sort_thunk thunk;
1393 struct list_head **ar, *le;
1397 list_for_each(le, head)
1399 ar = malloc(sizeof(struct list_head *) * count, M_KMALLOC, M_WAITOK);
1401 list_for_each(le, head)
1405 qsort_r(ar, count, sizeof(struct list_head *), &thunk, linux_le_cmp);
1406 INIT_LIST_HEAD(head);
1407 for (i = 0; i < count; i++)
1408 list_add_tail(ar[i], head);
1409 free(ar, M_KMALLOC);
1413 linux_irq_handler(void *ent)
1415 struct irq_ent *irqe;
1418 irqe->handler(irqe->irq, irqe->arg);
1421 #if defined(__i386__) || defined(__amd64__)
1422 bool linux_cpu_has_clflush;
1426 linux_compat_init(void *arg)
1428 struct sysctl_oid *rootoid;
1431 #if defined(__i386__) || defined(__amd64__)
1432 linux_cpu_has_clflush = (cpu_feature & CPUID_CLFSH);
1434 sx_init(&linux_global_rcu_lock, "LinuxGlobalRCU");
1436 rootoid = SYSCTL_ADD_ROOT_NODE(NULL,
1437 OID_AUTO, "sys", CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, "sys");
1438 kobject_init(&linux_class_root, &linux_class_ktype);
1439 kobject_set_name(&linux_class_root, "class");
1440 linux_class_root.oidp = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(rootoid),
1441 OID_AUTO, "class", CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, "class");
1442 kobject_init(&linux_root_device.kobj, &linux_dev_ktype);
1443 kobject_set_name(&linux_root_device.kobj, "device");
1444 linux_root_device.kobj.oidp = SYSCTL_ADD_NODE(NULL,
1445 SYSCTL_CHILDREN(rootoid), OID_AUTO, "device", CTLFLAG_RD, NULL,
1447 linux_root_device.bsddev = root_bus;
1448 linux_class_misc.name = "misc";
1449 class_register(&linux_class_misc);
1450 INIT_LIST_HEAD(&pci_drivers);
1451 INIT_LIST_HEAD(&pci_devices);
1452 spin_lock_init(&pci_lock);
1453 mtx_init(&vmmaplock, "IO Map lock", NULL, MTX_DEF);
1454 for (i = 0; i < VMMAP_HASH_SIZE; i++)
1455 LIST_INIT(&vmmaphead[i]);
1457 SYSINIT(linux_compat, SI_SUB_DRIVERS, SI_ORDER_SECOND, linux_compat_init, NULL);
1460 linux_compat_uninit(void *arg)
1462 linux_kobject_kfree_name(&linux_class_root);
1463 linux_kobject_kfree_name(&linux_root_device.kobj);
1464 linux_kobject_kfree_name(&linux_class_misc.kobj);
1467 sx_destroy(&linux_global_rcu_lock);
1469 SYSUNINIT(linux_compat, SI_SUB_DRIVERS, SI_ORDER_SECOND, linux_compat_uninit, NULL);
1472 * NOTE: Linux frequently uses "unsigned long" for pointer to integer
1473 * conversion and vice versa, where in FreeBSD "uintptr_t" would be
1474 * used. Assert these types have the same size, else some parts of the
1475 * LinuxKPI may not work like expected:
1477 CTASSERT(sizeof(unsigned long) == sizeof(uintptr_t));