xref: /freebsd/sys/compat/linuxkpi/common/src/linux_compat.c (revision 488e8a7faca51a71987fbf00cd36cfcd19269db7)
18d59ecb2SHans Petter Selasky /*-
28d59ecb2SHans Petter Selasky  * Copyright (c) 2010 Isilon Systems, Inc.
38d59ecb2SHans Petter Selasky  * Copyright (c) 2010 iX Systems, Inc.
48d59ecb2SHans Petter Selasky  * Copyright (c) 2010 Panasas, Inc.
5b8f113caSHans Petter Selasky  * Copyright (c) 2013-2021 Mellanox Technologies, Ltd.
68d59ecb2SHans Petter Selasky  * All rights reserved.
78d59ecb2SHans Petter Selasky  *
88d59ecb2SHans Petter Selasky  * Redistribution and use in source and binary forms, with or without
98d59ecb2SHans Petter Selasky  * modification, are permitted provided that the following conditions
108d59ecb2SHans Petter Selasky  * are met:
118d59ecb2SHans Petter Selasky  * 1. Redistributions of source code must retain the above copyright
128d59ecb2SHans Petter Selasky  *    notice unmodified, this list of conditions, and the following
138d59ecb2SHans Petter Selasky  *    disclaimer.
148d59ecb2SHans Petter Selasky  * 2. Redistributions in binary form must reproduce the above copyright
158d59ecb2SHans Petter Selasky  *    notice, this list of conditions and the following disclaimer in the
168d59ecb2SHans Petter Selasky  *    documentation and/or other materials provided with the distribution.
178d59ecb2SHans Petter Selasky  *
188d59ecb2SHans Petter Selasky  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
198d59ecb2SHans Petter Selasky  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
208d59ecb2SHans Petter Selasky  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
218d59ecb2SHans Petter Selasky  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
228d59ecb2SHans Petter Selasky  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
238d59ecb2SHans Petter Selasky  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
248d59ecb2SHans Petter Selasky  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
258d59ecb2SHans Petter Selasky  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
268d59ecb2SHans Petter Selasky  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
278d59ecb2SHans Petter Selasky  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
288d59ecb2SHans Petter Selasky  */
298d59ecb2SHans Petter Selasky 
308d59ecb2SHans Petter Selasky #include <sys/cdefs.h>
31e35079dbSHans Petter Selasky #include "opt_stack.h"
32e35079dbSHans Petter Selasky 
338d59ecb2SHans Petter Selasky #include <sys/param.h>
348d59ecb2SHans Petter Selasky #include <sys/systm.h>
358d59ecb2SHans Petter Selasky #include <sys/malloc.h>
368d59ecb2SHans Petter Selasky #include <sys/kernel.h>
378d59ecb2SHans Petter Selasky #include <sys/sysctl.h>
388d59ecb2SHans Petter Selasky #include <sys/proc.h>
398d59ecb2SHans Petter Selasky #include <sys/sglist.h>
408d59ecb2SHans Petter Selasky #include <sys/sleepqueue.h>
41f823a36eSKonstantin Belousov #include <sys/refcount.h>
428d59ecb2SHans Petter Selasky #include <sys/lock.h>
438d59ecb2SHans Petter Selasky #include <sys/mutex.h>
448d59ecb2SHans Petter Selasky #include <sys/bus.h>
45e12be321SConrad Meyer #include <sys/eventhandler.h>
468d59ecb2SHans Petter Selasky #include <sys/fcntl.h>
478d59ecb2SHans Petter Selasky #include <sys/file.h>
488d59ecb2SHans Petter Selasky #include <sys/filio.h>
498d59ecb2SHans Petter Selasky #include <sys/rwlock.h>
50e23ae408SHans Petter Selasky #include <sys/mman.h>
51e35079dbSHans Petter Selasky #include <sys/stack.h>
52945accf5SDmitry Chagin #include <sys/sysent.h>
53bc042266SBjoern A. Zeeb #include <sys/time.h>
54f1863400SKonstantin Belousov #include <sys/user.h>
558d59ecb2SHans Petter Selasky 
568d59ecb2SHans Petter Selasky #include <vm/vm.h>
578d59ecb2SHans Petter Selasky #include <vm/pmap.h>
58c12488bbSMark Johnston #include <vm/vm_object.h>
59c12488bbSMark Johnston #include <vm/vm_page.h>
60c12488bbSMark Johnston #include <vm/vm_pager.h>
618d59ecb2SHans Petter Selasky 
628d59ecb2SHans Petter Selasky #include <machine/stdarg.h>
638d59ecb2SHans Petter Selasky 
6403219fbaSHans Petter Selasky #if defined(__i386__) || defined(__amd64__)
6503219fbaSHans Petter Selasky #include <machine/md_var.h>
6603219fbaSHans Petter Selasky #endif
6703219fbaSHans Petter Selasky 
688d59ecb2SHans Petter Selasky #include <linux/kobject.h>
695fce8027SBjoern A. Zeeb #include <linux/cpu.h>
708d59ecb2SHans Petter Selasky #include <linux/device.h>
718d59ecb2SHans Petter Selasky #include <linux/slab.h>
728d59ecb2SHans Petter Selasky #include <linux/module.h>
731d9b99e5SHans Petter Selasky #include <linux/moduleparam.h>
748d59ecb2SHans Petter Selasky #include <linux/cdev.h>
758d59ecb2SHans Petter Selasky #include <linux/file.h>
768d59ecb2SHans Petter Selasky #include <linux/sysfs.h>
778d59ecb2SHans Petter Selasky #include <linux/mm.h>
788d59ecb2SHans Petter Selasky #include <linux/io.h>
798d59ecb2SHans Petter Selasky #include <linux/vmalloc.h>
808d59ecb2SHans Petter Selasky #include <linux/netdevice.h>
818d59ecb2SHans Petter Selasky #include <linux/timer.h>
820c510167SHans Petter Selasky #include <linux/interrupt.h>
833a8bec33SHans Petter Selasky #include <linux/uaccess.h>
84ad513b4dSBjoern A. Zeeb #include <linux/utsname.h>
85aad02fb4SHans Petter Selasky #include <linux/list.h>
86dff36e69SHans Petter Selasky #include <linux/kthread.h>
87dff36e69SHans Petter Selasky #include <linux/kernel.h>
883ce12630SHans Petter Selasky #include <linux/compat.h>
89132b00f9SWarner Losh #include <linux/io-mapping.h>
906e3e6544SHans Petter Selasky #include <linux/poll.h>
91c12488bbSMark Johnston #include <linux/smp.h>
920e123c13SEmmanuel Vadot #include <linux/wait_bit.h>
93a81b36c6SVladimir Kondratyev #include <linux/rcupdate.h>
94dbc920bdSVladimir Kondratyev #include <linux/interval_tree.h>
95dbc920bdSVladimir Kondratyev #include <linux/interval_tree_generic.h>
968d59ecb2SHans Petter Selasky 
97c12488bbSMark Johnston #if defined(__i386__) || defined(__amd64__)
98c12488bbSMark Johnston #include <asm/smp.h>
99b2c86006SEmmanuel Vadot #include <asm/processor.h>
100c12488bbSMark Johnston #endif
1018d59ecb2SHans Petter Selasky 
10239a3542bSPawel Biernacki SYSCTL_NODE(_compat, OID_AUTO, linuxkpi, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
10339a3542bSPawel Biernacki     "LinuxKPI parameters");
1041d9b99e5SHans Petter Selasky 
1054580f5eaSHans Petter Selasky int linuxkpi_debug;
1064580f5eaSHans Petter Selasky SYSCTL_INT(_compat_linuxkpi, OID_AUTO, debug, CTLFLAG_RWTUN,
1074580f5eaSHans Petter Selasky     &linuxkpi_debug, 0, "Set to enable pr_debug() prints. Clear to disable.");
1084580f5eaSHans Petter Selasky 
1092390a144SHans Petter Selasky int linuxkpi_warn_dump_stack = 0;
1102390a144SHans Petter Selasky SYSCTL_INT(_compat_linuxkpi, OID_AUTO, warn_dump_stack, CTLFLAG_RWTUN,
1112390a144SHans Petter Selasky     &linuxkpi_warn_dump_stack, 0,
1122390a144SHans Petter Selasky     "Set to enable stack traces from WARN_ON(). Clear to disable.");
1132390a144SHans Petter Selasky 
114bc042266SBjoern A. Zeeb static struct timeval lkpi_net_lastlog;
115bc042266SBjoern A. Zeeb static int lkpi_net_curpps;
116bc042266SBjoern A. Zeeb static int lkpi_net_maxpps = 99;
117bc042266SBjoern A. Zeeb SYSCTL_INT(_compat_linuxkpi, OID_AUTO, net_ratelimit, CTLFLAG_RWTUN,
118bc042266SBjoern A. Zeeb     &lkpi_net_maxpps, 0, "Limit number of LinuxKPI net messages per second.");
119bc042266SBjoern A. Zeeb 
1205ca9d417SDmitry Chagin MALLOC_DEFINE(M_KMALLOC, "lkpikmalloc", "Linux kmalloc compat");
1218d59ecb2SHans Petter Selasky 
1228d59ecb2SHans Petter Selasky #include <linux/rbtree.h>
1238d59ecb2SHans Petter Selasky /* Undo Linux compat changes. */
1248d59ecb2SHans Petter Selasky #undef RB_ROOT
1258d59ecb2SHans Petter Selasky #undef file
1268d59ecb2SHans Petter Selasky #undef cdev
1278d59ecb2SHans Petter Selasky #define	RB_ROOT(head)	(head)->rbh_root
1288d59ecb2SHans Petter Selasky 
12967807f50SHans Petter Selasky static void linux_destroy_dev(struct linux_cdev *);
130f823a36eSKonstantin Belousov static void linux_cdev_deref(struct linux_cdev *ldev);
1311ea4c857SHans Petter Selasky static struct vm_area_struct *linux_cdev_handle_find(void *handle);
1321ea4c857SHans Petter Selasky 
1335fce8027SBjoern A. Zeeb cpumask_t cpu_online_mask;
134*488e8a7fSBjoern A. Zeeb static cpumask_t **static_single_cpu_mask;
135*488e8a7fSBjoern A. Zeeb static cpumask_t *static_single_cpu_mask_lcs;
13606204f8eSHans Petter Selasky struct kobject linux_class_root;
13706204f8eSHans Petter Selasky struct device linux_root_device;
13806204f8eSHans Petter Selasky struct class linux_class_misc;
1398d59ecb2SHans Petter Selasky struct list_head pci_drivers;
1408d59ecb2SHans Petter Selasky struct list_head pci_devices;
1418d59ecb2SHans Petter Selasky spinlock_t pci_lock;
142ad513b4dSBjoern A. Zeeb struct uts_namespace init_uts_ns;
1438d59ecb2SHans Petter Selasky 
1448d59ecb2SHans Petter Selasky unsigned long linux_timer_hz_mask;
1458d59ecb2SHans Petter Selasky 
1460e123c13SEmmanuel Vadot wait_queue_head_t linux_bit_waitq;
1470e123c13SEmmanuel Vadot wait_queue_head_t linux_var_waitq;
1480e123c13SEmmanuel Vadot 
1498d59ecb2SHans Petter Selasky int
1508d59ecb2SHans Petter Selasky panic_cmp(struct rb_node *one, struct rb_node *two)
1518d59ecb2SHans Petter Selasky {
1528d59ecb2SHans Petter Selasky 	panic("no cmp");
1538d59ecb2SHans Petter Selasky }
1548d59ecb2SHans Petter Selasky 
1558d59ecb2SHans Petter Selasky RB_GENERATE(linux_root, rb_node, __entry, panic_cmp);
1568d59ecb2SHans Petter Selasky 
157dbc920bdSVladimir Kondratyev #define	START(node)	((node)->start)
158dbc920bdSVladimir Kondratyev #define	LAST(node)	((node)->last)
159dbc920bdSVladimir Kondratyev 
160dbc920bdSVladimir Kondratyev INTERVAL_TREE_DEFINE(struct interval_tree_node, rb, unsigned long,, START,
161dbc920bdSVladimir Kondratyev     LAST,, lkpi_interval_tree)
162dbc920bdSVladimir Kondratyev 
1638d59ecb2SHans Petter Selasky static void
16406204f8eSHans Petter Selasky linux_device_release(struct device *dev)
1658d59ecb2SHans Petter Selasky {
16606204f8eSHans Petter Selasky 	pr_debug("linux_device_release: %s\n", dev_name(dev));
1678d59ecb2SHans Petter Selasky 	kfree(dev);
1688d59ecb2SHans Petter Selasky }
1698d59ecb2SHans Petter Selasky 
17006204f8eSHans Petter Selasky static ssize_t
17106204f8eSHans Petter Selasky linux_class_show(struct kobject *kobj, struct attribute *attr, char *buf)
17206204f8eSHans Petter Selasky {
17306204f8eSHans Petter Selasky 	struct class_attribute *dattr;
17406204f8eSHans Petter Selasky 	ssize_t error;
17506204f8eSHans Petter Selasky 
17606204f8eSHans Petter Selasky 	dattr = container_of(attr, struct class_attribute, attr);
17706204f8eSHans Petter Selasky 	error = -EIO;
17806204f8eSHans Petter Selasky 	if (dattr->show)
17906204f8eSHans Petter Selasky 		error = dattr->show(container_of(kobj, struct class, kobj),
18006204f8eSHans Petter Selasky 		    dattr, buf);
18106204f8eSHans Petter Selasky 	return (error);
18206204f8eSHans Petter Selasky }
18306204f8eSHans Petter Selasky 
18406204f8eSHans Petter Selasky static ssize_t
18506204f8eSHans Petter Selasky linux_class_store(struct kobject *kobj, struct attribute *attr, const char *buf,
18606204f8eSHans Petter Selasky     size_t count)
18706204f8eSHans Petter Selasky {
18806204f8eSHans Petter Selasky 	struct class_attribute *dattr;
18906204f8eSHans Petter Selasky 	ssize_t error;
19006204f8eSHans Petter Selasky 
19106204f8eSHans Petter Selasky 	dattr = container_of(attr, struct class_attribute, attr);
19206204f8eSHans Petter Selasky 	error = -EIO;
19306204f8eSHans Petter Selasky 	if (dattr->store)
19406204f8eSHans Petter Selasky 		error = dattr->store(container_of(kobj, struct class, kobj),
19506204f8eSHans Petter Selasky 		    dattr, buf, count);
19606204f8eSHans Petter Selasky 	return (error);
19706204f8eSHans Petter Selasky }
19806204f8eSHans Petter Selasky 
19906204f8eSHans Petter Selasky static void
20006204f8eSHans Petter Selasky linux_class_release(struct kobject *kobj)
20106204f8eSHans Petter Selasky {
20206204f8eSHans Petter Selasky 	struct class *class;
20306204f8eSHans Petter Selasky 
20406204f8eSHans Petter Selasky 	class = container_of(kobj, struct class, kobj);
20506204f8eSHans Petter Selasky 	if (class->class_release)
20606204f8eSHans Petter Selasky 		class->class_release(class);
20706204f8eSHans Petter Selasky }
20806204f8eSHans Petter Selasky 
20906204f8eSHans Petter Selasky static const struct sysfs_ops linux_class_sysfs = {
21006204f8eSHans Petter Selasky 	.show  = linux_class_show,
21106204f8eSHans Petter Selasky 	.store = linux_class_store,
21206204f8eSHans Petter Selasky };
21306204f8eSHans Petter Selasky 
21406204f8eSHans Petter Selasky const struct kobj_type linux_class_ktype = {
21506204f8eSHans Petter Selasky 	.release = linux_class_release,
21606204f8eSHans Petter Selasky 	.sysfs_ops = &linux_class_sysfs
21706204f8eSHans Petter Selasky };
21806204f8eSHans Petter Selasky 
21906204f8eSHans Petter Selasky static void
22006204f8eSHans Petter Selasky linux_dev_release(struct kobject *kobj)
22106204f8eSHans Petter Selasky {
22206204f8eSHans Petter Selasky 	struct device *dev;
22306204f8eSHans Petter Selasky 
22406204f8eSHans Petter Selasky 	dev = container_of(kobj, struct device, kobj);
22506204f8eSHans Petter Selasky 	/* This is the precedence defined by linux. */
22606204f8eSHans Petter Selasky 	if (dev->release)
22706204f8eSHans Petter Selasky 		dev->release(dev);
22806204f8eSHans Petter Selasky 	else if (dev->class && dev->class->dev_release)
22906204f8eSHans Petter Selasky 		dev->class->dev_release(dev);
23006204f8eSHans Petter Selasky }
23106204f8eSHans Petter Selasky 
23206204f8eSHans Petter Selasky static ssize_t
23306204f8eSHans Petter Selasky linux_dev_show(struct kobject *kobj, struct attribute *attr, char *buf)
23406204f8eSHans Petter Selasky {
23506204f8eSHans Petter Selasky 	struct device_attribute *dattr;
23606204f8eSHans Petter Selasky 	ssize_t error;
23706204f8eSHans Petter Selasky 
23806204f8eSHans Petter Selasky 	dattr = container_of(attr, struct device_attribute, attr);
23906204f8eSHans Petter Selasky 	error = -EIO;
24006204f8eSHans Petter Selasky 	if (dattr->show)
24106204f8eSHans Petter Selasky 		error = dattr->show(container_of(kobj, struct device, kobj),
24206204f8eSHans Petter Selasky 		    dattr, buf);
24306204f8eSHans Petter Selasky 	return (error);
24406204f8eSHans Petter Selasky }
24506204f8eSHans Petter Selasky 
24606204f8eSHans Petter Selasky static ssize_t
24706204f8eSHans Petter Selasky linux_dev_store(struct kobject *kobj, struct attribute *attr, const char *buf,
24806204f8eSHans Petter Selasky     size_t count)
24906204f8eSHans Petter Selasky {
25006204f8eSHans Petter Selasky 	struct device_attribute *dattr;
25106204f8eSHans Petter Selasky 	ssize_t error;
25206204f8eSHans Petter Selasky 
25306204f8eSHans Petter Selasky 	dattr = container_of(attr, struct device_attribute, attr);
25406204f8eSHans Petter Selasky 	error = -EIO;
25506204f8eSHans Petter Selasky 	if (dattr->store)
25606204f8eSHans Petter Selasky 		error = dattr->store(container_of(kobj, struct device, kobj),
25706204f8eSHans Petter Selasky 		    dattr, buf, count);
25806204f8eSHans Petter Selasky 	return (error);
25906204f8eSHans Petter Selasky }
26006204f8eSHans Petter Selasky 
26106204f8eSHans Petter Selasky static const struct sysfs_ops linux_dev_sysfs = {
26206204f8eSHans Petter Selasky 	.show  = linux_dev_show,
26306204f8eSHans Petter Selasky 	.store = linux_dev_store,
26406204f8eSHans Petter Selasky };
26506204f8eSHans Petter Selasky 
26606204f8eSHans Petter Selasky const struct kobj_type linux_dev_ktype = {
26706204f8eSHans Petter Selasky 	.release = linux_dev_release,
26806204f8eSHans Petter Selasky 	.sysfs_ops = &linux_dev_sysfs
26906204f8eSHans Petter Selasky };
27006204f8eSHans Petter Selasky 
2718d59ecb2SHans Petter Selasky struct device *
2728d59ecb2SHans Petter Selasky device_create(struct class *class, struct device *parent, dev_t devt,
2738d59ecb2SHans Petter Selasky     void *drvdata, const char *fmt, ...)
2748d59ecb2SHans Petter Selasky {
2758d59ecb2SHans Petter Selasky 	struct device *dev;
2768d59ecb2SHans Petter Selasky 	va_list args;
2778d59ecb2SHans Petter Selasky 
2788d59ecb2SHans Petter Selasky 	dev = kzalloc(sizeof(*dev), M_WAITOK);
2798d59ecb2SHans Petter Selasky 	dev->parent = parent;
2808d59ecb2SHans Petter Selasky 	dev->class = class;
2818d59ecb2SHans Petter Selasky 	dev->devt = devt;
2828d59ecb2SHans Petter Selasky 	dev->driver_data = drvdata;
28306204f8eSHans Petter Selasky 	dev->release = linux_device_release;
2848d59ecb2SHans Petter Selasky 	va_start(args, fmt);
2858d59ecb2SHans Petter Selasky 	kobject_set_name_vargs(&dev->kobj, fmt, args);
2868d59ecb2SHans Petter Selasky 	va_end(args);
2878d59ecb2SHans Petter Selasky 	device_register(dev);
2888d59ecb2SHans Petter Selasky 
2898d59ecb2SHans Petter Selasky 	return (dev);
2908d59ecb2SHans Petter Selasky }
2918d59ecb2SHans Petter Selasky 
292702b6875SWarner Losh struct device *
293702b6875SWarner Losh device_create_groups_vargs(struct class *class, struct device *parent,
294702b6875SWarner Losh     dev_t devt, void *drvdata, const struct attribute_group **groups,
295702b6875SWarner Losh     const char *fmt, va_list args)
296702b6875SWarner Losh {
297702b6875SWarner Losh 	struct device *dev = NULL;
298702b6875SWarner Losh 	int retval = -ENODEV;
299702b6875SWarner Losh 
300702b6875SWarner Losh 	if (class == NULL || IS_ERR(class))
301702b6875SWarner Losh 		goto error;
302702b6875SWarner Losh 
303702b6875SWarner Losh 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
304702b6875SWarner Losh 	if (!dev) {
305702b6875SWarner Losh 		retval = -ENOMEM;
306702b6875SWarner Losh 		goto error;
307702b6875SWarner Losh 	}
308702b6875SWarner Losh 
309702b6875SWarner Losh 	dev->devt = devt;
310702b6875SWarner Losh 	dev->class = class;
311702b6875SWarner Losh 	dev->parent = parent;
312702b6875SWarner Losh 	dev->groups = groups;
313702b6875SWarner Losh 	dev->release = device_create_release;
314702b6875SWarner Losh 	/* device_initialize() needs the class and parent to be set */
315702b6875SWarner Losh 	device_initialize(dev);
316702b6875SWarner Losh 	dev_set_drvdata(dev, drvdata);
317702b6875SWarner Losh 
318702b6875SWarner Losh 	retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
319702b6875SWarner Losh 	if (retval)
320702b6875SWarner Losh 		goto error;
321702b6875SWarner Losh 
322702b6875SWarner Losh 	retval = device_add(dev);
323702b6875SWarner Losh 	if (retval)
324702b6875SWarner Losh 		goto error;
325702b6875SWarner Losh 
326702b6875SWarner Losh 	return dev;
327702b6875SWarner Losh 
328702b6875SWarner Losh error:
329702b6875SWarner Losh 	put_device(dev);
330702b6875SWarner Losh 	return ERR_PTR(retval);
331702b6875SWarner Losh }
332702b6875SWarner Losh 
3331341ac9fSWarner Losh struct class *
3341341ac9fSWarner Losh class_create(struct module *owner, const char *name)
3351341ac9fSWarner Losh {
3361341ac9fSWarner Losh 	struct class *class;
3371341ac9fSWarner Losh 	int error;
3381341ac9fSWarner Losh 
3391341ac9fSWarner Losh 	class = kzalloc(sizeof(*class), M_WAITOK);
3401341ac9fSWarner Losh 	class->owner = owner;
3411341ac9fSWarner Losh 	class->name = name;
3421341ac9fSWarner Losh 	class->class_release = linux_class_kfree;
3431341ac9fSWarner Losh 	error = class_register(class);
3441341ac9fSWarner Losh 	if (error) {
3451341ac9fSWarner Losh 		kfree(class);
3461341ac9fSWarner Losh 		return (NULL);
3471341ac9fSWarner Losh 	}
3481341ac9fSWarner Losh 
3491341ac9fSWarner Losh 	return (class);
3501341ac9fSWarner Losh }
3511341ac9fSWarner Losh 
352464d20bcSHans Petter Selasky static void
353a6b28ee0SHans Petter Selasky linux_kq_lock(void *arg)
354a6b28ee0SHans Petter Selasky {
355a6b28ee0SHans Petter Selasky 	spinlock_t *s = arg;
356a6b28ee0SHans Petter Selasky 
357a6b28ee0SHans Petter Selasky 	spin_lock(s);
358a6b28ee0SHans Petter Selasky }
359a6b28ee0SHans Petter Selasky static void
360a6b28ee0SHans Petter Selasky linux_kq_unlock(void *arg)
361a6b28ee0SHans Petter Selasky {
362a6b28ee0SHans Petter Selasky 	spinlock_t *s = arg;
363a6b28ee0SHans Petter Selasky 
364a6b28ee0SHans Petter Selasky 	spin_unlock(s);
365a6b28ee0SHans Petter Selasky }
366a6b28ee0SHans Petter Selasky 
367a6b28ee0SHans Petter Selasky static void
368e90afaa0SMateusz Guzik linux_kq_assert_lock(void *arg, int what)
369a6b28ee0SHans Petter Selasky {
370a6b28ee0SHans Petter Selasky #ifdef INVARIANTS
371a6b28ee0SHans Petter Selasky 	spinlock_t *s = arg;
372a6b28ee0SHans Petter Selasky 
373e90afaa0SMateusz Guzik 	if (what == LA_LOCKED)
374a6b28ee0SHans Petter Selasky 		mtx_assert(&s->m, MA_OWNED);
375e90afaa0SMateusz Guzik 	else
376a6b28ee0SHans Petter Selasky 		mtx_assert(&s->m, MA_NOTOWNED);
377a6b28ee0SHans Petter Selasky #endif
378a6b28ee0SHans Petter Selasky }
379a6b28ee0SHans Petter Selasky 
380a6b28ee0SHans Petter Selasky static void
381e23ae408SHans Petter Selasky linux_file_kqfilter_poll(struct linux_file *, int);
382a6b28ee0SHans Petter Selasky 
383a6b28ee0SHans Petter Selasky struct linux_file *
384a6b28ee0SHans Petter Selasky linux_file_alloc(void)
385a6b28ee0SHans Petter Selasky {
386a6b28ee0SHans Petter Selasky 	struct linux_file *filp;
387a6b28ee0SHans Petter Selasky 
3888a8e86b8SJean-Sébastien Pédron 	filp = kzalloc(sizeof(*filp), GFP_KERNEL);
389a6b28ee0SHans Petter Selasky 
390a6b28ee0SHans Petter Selasky 	/* set initial refcount */
391a6b28ee0SHans Petter Selasky 	filp->f_count = 1;
392a6b28ee0SHans Petter Selasky 
393a6b28ee0SHans Petter Selasky 	/* setup fields needed by kqueue support */
394a6b28ee0SHans Petter Selasky 	spin_lock_init(&filp->f_kqlock);
395a6b28ee0SHans Petter Selasky 	knlist_init(&filp->f_selinfo.si_note, &filp->f_kqlock,
396e90afaa0SMateusz Guzik 	    linux_kq_lock, linux_kq_unlock, linux_kq_assert_lock);
397a6b28ee0SHans Petter Selasky 
398a6b28ee0SHans Petter Selasky 	return (filp);
399a6b28ee0SHans Petter Selasky }
400a6b28ee0SHans Petter Selasky 
401f5a9867bSHans Petter Selasky void
402f5a9867bSHans Petter Selasky linux_file_free(struct linux_file *filp)
403f5a9867bSHans Petter Selasky {
404f5a9867bSHans Petter Selasky 	if (filp->_file == NULL) {
4057d92d483SVladimir Kondratyev 		if (filp->f_op != NULL && filp->f_op->release != NULL)
4067d92d483SVladimir Kondratyev 			filp->f_op->release(filp->f_vnode, filp);
407dac6b88aSMark Johnston 		if (filp->f_shmem != NULL)
408dac6b88aSMark Johnston 			vm_object_deallocate(filp->f_shmem);
409a81b36c6SVladimir Kondratyev 		kfree_rcu(filp, rcu);
410f5a9867bSHans Petter Selasky 	} else {
411f5a9867bSHans Petter Selasky 		/*
412f5a9867bSHans Petter Selasky 		 * The close method of the character device or file
413f5a9867bSHans Petter Selasky 		 * will free the linux_file structure:
414f5a9867bSHans Petter Selasky 		 */
415f5a9867bSHans Petter Selasky 		_fdrop(filp->_file, curthread);
416f5a9867bSHans Petter Selasky 	}
417f5a9867bSHans Petter Selasky }
418f5a9867bSHans Petter Selasky 
419aca0bcbcSWarner Losh struct linux_cdev *
420aca0bcbcSWarner Losh cdev_alloc(void)
421aca0bcbcSWarner Losh {
422aca0bcbcSWarner Losh 	struct linux_cdev *cdev;
423aca0bcbcSWarner Losh 
424aca0bcbcSWarner Losh 	cdev = kzalloc(sizeof(struct linux_cdev), M_WAITOK);
425aca0bcbcSWarner Losh 	kobject_init(&cdev->kobj, &linux_cdev_ktype);
426aca0bcbcSWarner Losh 	cdev->refs = 1;
427aca0bcbcSWarner Losh 	return (cdev);
428aca0bcbcSWarner Losh }
429aca0bcbcSWarner Losh 
4308d59ecb2SHans Petter Selasky static int
43161157228SHans Petter Selasky linux_cdev_pager_fault(vm_object_t vm_obj, vm_ooffset_t offset, int prot,
43261157228SHans Petter Selasky     vm_page_t *mres)
43361157228SHans Petter Selasky {
43461157228SHans Petter Selasky 	struct vm_area_struct *vmap;
43561157228SHans Petter Selasky 
43661157228SHans Petter Selasky 	vmap = linux_cdev_handle_find(vm_obj->handle);
43761157228SHans Petter Selasky 
43861157228SHans Petter Selasky 	MPASS(vmap != NULL);
43961157228SHans Petter Selasky 	MPASS(vmap->vm_private_data == vm_obj->handle);
44061157228SHans Petter Selasky 
44161157228SHans Petter Selasky 	if (likely(vmap->vm_ops != NULL && offset < vmap->vm_len)) {
44261157228SHans Petter Selasky 		vm_paddr_t paddr = IDX_TO_OFF(vmap->vm_pfn) + offset;
44361157228SHans Petter Selasky 		vm_page_t page;
44461157228SHans Petter Selasky 
44561157228SHans Petter Selasky 		if (((*mres)->flags & PG_FICTITIOUS) != 0) {
44661157228SHans Petter Selasky 			/*
44761157228SHans Petter Selasky 			 * If the passed in result page is a fake
44861157228SHans Petter Selasky 			 * page, update it with the new physical
44961157228SHans Petter Selasky 			 * address.
45061157228SHans Petter Selasky 			 */
45161157228SHans Petter Selasky 			page = *mres;
45261157228SHans Petter Selasky 			vm_page_updatefake(page, paddr, vm_obj->memattr);
45361157228SHans Petter Selasky 		} else {
45461157228SHans Petter Selasky 			/*
45561157228SHans Petter Selasky 			 * Replace the passed in "mres" page with our
45661157228SHans Petter Selasky 			 * own fake page and free up the all of the
45761157228SHans Petter Selasky 			 * original pages.
45861157228SHans Petter Selasky 			 */
45961157228SHans Petter Selasky 			VM_OBJECT_WUNLOCK(vm_obj);
46061157228SHans Petter Selasky 			page = vm_page_getfake(paddr, vm_obj->memattr);
46161157228SHans Petter Selasky 			VM_OBJECT_WLOCK(vm_obj);
46261157228SHans Petter Selasky 
4633cf3b4e6SJeff Roberson 			vm_page_replace(page, vm_obj, (*mres)->pindex, *mres);
46461157228SHans Petter Selasky 			*mres = page;
46561157228SHans Petter Selasky 		}
4660012f373SJeff Roberson 		vm_page_valid(page);
46761157228SHans Petter Selasky 		return (VM_PAGER_OK);
46861157228SHans Petter Selasky 	}
46961157228SHans Petter Selasky 	return (VM_PAGER_FAIL);
47061157228SHans Petter Selasky }
47161157228SHans Petter Selasky 
47261157228SHans Petter Selasky static int
4731ea4c857SHans Petter Selasky linux_cdev_pager_populate(vm_object_t vm_obj, vm_pindex_t pidx, int fault_type,
4741ea4c857SHans Petter Selasky     vm_prot_t max_prot, vm_pindex_t *first, vm_pindex_t *last)
4751ea4c857SHans Petter Selasky {
4761ea4c857SHans Petter Selasky 	struct vm_area_struct *vmap;
4771ea4c857SHans Petter Selasky 	int err;
4781ea4c857SHans Petter Selasky 
4791ea4c857SHans Petter Selasky 	/* get VM area structure */
4801ea4c857SHans Petter Selasky 	vmap = linux_cdev_handle_find(vm_obj->handle);
4811ea4c857SHans Petter Selasky 	MPASS(vmap != NULL);
4821ea4c857SHans Petter Selasky 	MPASS(vmap->vm_private_data == vm_obj->handle);
4831ea4c857SHans Petter Selasky 
48461157228SHans Petter Selasky 	VM_OBJECT_WUNLOCK(vm_obj);
48561157228SHans Petter Selasky 
48642f8ef4bSHans Petter Selasky 	linux_set_current(curthread);
48742f8ef4bSHans Petter Selasky 
48861157228SHans Petter Selasky 	down_write(&vmap->vm_mm->mmap_sem);
48961157228SHans Petter Selasky 	if (unlikely(vmap->vm_ops == NULL)) {
49061157228SHans Petter Selasky 		err = VM_FAULT_SIGBUS;
49161157228SHans Petter Selasky 	} else {
49261157228SHans Petter Selasky 		struct vm_fault vmf;
49361157228SHans Petter Selasky 
4941ea4c857SHans Petter Selasky 		/* fill out VM fault structure */
4959362b6a3SKonstantin Belousov 		vmf.virtual_address = (void *)(uintptr_t)IDX_TO_OFF(pidx);
4961ea4c857SHans Petter Selasky 		vmf.flags = (fault_type & VM_PROT_WRITE) ? FAULT_FLAG_WRITE : 0;
4971ea4c857SHans Petter Selasky 		vmf.pgoff = 0;
4981ea4c857SHans Petter Selasky 		vmf.page = NULL;
4990628fc90SHans Petter Selasky 		vmf.vma = vmap;
5001ea4c857SHans Petter Selasky 
5011ea4c857SHans Petter Selasky 		vmap->vm_pfn_count = 0;
5021ea4c857SHans Petter Selasky 		vmap->vm_pfn_pcount = &vmap->vm_pfn_count;
5031ea4c857SHans Petter Selasky 		vmap->vm_obj = vm_obj;
5041ea4c857SHans Petter Selasky 
505062f1500SVladimir Kondratyev 		err = vmap->vm_ops->fault(&vmf);
5061ea4c857SHans Petter Selasky 
5071ea4c857SHans Petter Selasky 		while (vmap->vm_pfn_count == 0 && err == VM_FAULT_NOPAGE) {
50802fb845bSMark Johnston 			kern_yield(PRI_USER);
509062f1500SVladimir Kondratyev 			err = vmap->vm_ops->fault(&vmf);
5101ea4c857SHans Petter Selasky 		}
5111ea4c857SHans Petter Selasky 	}
5121ea4c857SHans Petter Selasky 
5131ea4c857SHans Petter Selasky 	/* translate return code */
5141ea4c857SHans Petter Selasky 	switch (err) {
5151ea4c857SHans Petter Selasky 	case VM_FAULT_OOM:
5161ea4c857SHans Petter Selasky 		err = VM_PAGER_AGAIN;
5171ea4c857SHans Petter Selasky 		break;
5181ea4c857SHans Petter Selasky 	case VM_FAULT_SIGBUS:
5191ea4c857SHans Petter Selasky 		err = VM_PAGER_BAD;
5201ea4c857SHans Petter Selasky 		break;
5211ea4c857SHans Petter Selasky 	case VM_FAULT_NOPAGE:
5221ea4c857SHans Petter Selasky 		/*
5231ea4c857SHans Petter Selasky 		 * By contract the fault handler will return having
5241ea4c857SHans Petter Selasky 		 * busied all the pages itself. If pidx is already
5251ea4c857SHans Petter Selasky 		 * found in the object, it will simply xbusy the first
5261ea4c857SHans Petter Selasky 		 * page and return with vm_pfn_count set to 1.
5271ea4c857SHans Petter Selasky 		 */
5281ea4c857SHans Petter Selasky 		*first = vmap->vm_pfn_first;
5291ea4c857SHans Petter Selasky 		*last = *first + vmap->vm_pfn_count - 1;
5301ea4c857SHans Petter Selasky 		err = VM_PAGER_OK;
5311ea4c857SHans Petter Selasky 		break;
5321ea4c857SHans Petter Selasky 	default:
5331ea4c857SHans Petter Selasky 		err = VM_PAGER_ERROR;
5341ea4c857SHans Petter Selasky 		break;
5351ea4c857SHans Petter Selasky 	}
5361ea4c857SHans Petter Selasky 	up_write(&vmap->vm_mm->mmap_sem);
5371ea4c857SHans Petter Selasky 	VM_OBJECT_WLOCK(vm_obj);
5381ea4c857SHans Petter Selasky 	return (err);
5391ea4c857SHans Petter Selasky }
5401ea4c857SHans Petter Selasky 
5411ea4c857SHans Petter Selasky static struct rwlock linux_vma_lock;
5421ea4c857SHans Petter Selasky static TAILQ_HEAD(, vm_area_struct) linux_vma_head =
5431ea4c857SHans Petter Selasky     TAILQ_HEAD_INITIALIZER(linux_vma_head);
5441ea4c857SHans Petter Selasky 
545cca15f28SHans Petter Selasky static void
546cca15f28SHans Petter Selasky linux_cdev_handle_free(struct vm_area_struct *vmap)
547cca15f28SHans Petter Selasky {
548d56f1ed8SHans Petter Selasky 	/* Drop reference on vm_file */
549d56f1ed8SHans Petter Selasky 	if (vmap->vm_file != NULL)
550d56f1ed8SHans Petter Selasky 		fput(vmap->vm_file);
551d56f1ed8SHans Petter Selasky 
552cca15f28SHans Petter Selasky 	/* Drop reference on mm_struct */
553cca15f28SHans Petter Selasky 	mmput(vmap->vm_mm);
554cca15f28SHans Petter Selasky 
555cca15f28SHans Petter Selasky 	kfree(vmap);
556cca15f28SHans Petter Selasky }
557cca15f28SHans Petter Selasky 
5581ea4c857SHans Petter Selasky static void
5591ea4c857SHans Petter Selasky linux_cdev_handle_remove(struct vm_area_struct *vmap)
5601ea4c857SHans Petter Selasky {
5611ea4c857SHans Petter Selasky 	rw_wlock(&linux_vma_lock);
5621ea4c857SHans Petter Selasky 	TAILQ_REMOVE(&linux_vma_head, vmap, vm_entry);
5631ea4c857SHans Petter Selasky 	rw_wunlock(&linux_vma_lock);
5641ea4c857SHans Petter Selasky }
5651ea4c857SHans Petter Selasky 
5661ea4c857SHans Petter Selasky static struct vm_area_struct *
5671ea4c857SHans Petter Selasky linux_cdev_handle_find(void *handle)
5681ea4c857SHans Petter Selasky {
5691ea4c857SHans Petter Selasky 	struct vm_area_struct *vmap;
5701ea4c857SHans Petter Selasky 
5711ea4c857SHans Petter Selasky 	rw_rlock(&linux_vma_lock);
5721ea4c857SHans Petter Selasky 	TAILQ_FOREACH(vmap, &linux_vma_head, vm_entry) {
5731ea4c857SHans Petter Selasky 		if (vmap->vm_private_data == handle)
5741ea4c857SHans Petter Selasky 			break;
5751ea4c857SHans Petter Selasky 	}
5761ea4c857SHans Petter Selasky 	rw_runlock(&linux_vma_lock);
5771ea4c857SHans Petter Selasky 	return (vmap);
5781ea4c857SHans Petter Selasky }
5791ea4c857SHans Petter Selasky 
5801ea4c857SHans Petter Selasky static int
5811ea4c857SHans Petter Selasky linux_cdev_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
5821ea4c857SHans Petter Selasky 		      vm_ooffset_t foff, struct ucred *cred, u_short *color)
5831ea4c857SHans Petter Selasky {
5841ea4c857SHans Petter Selasky 
585ea67550bSHans Petter Selasky 	MPASS(linux_cdev_handle_find(handle) != NULL);
5861ea4c857SHans Petter Selasky 	*color = 0;
5871ea4c857SHans Petter Selasky 	return (0);
5881ea4c857SHans Petter Selasky }
5891ea4c857SHans Petter Selasky 
5901ea4c857SHans Petter Selasky static void
5911ea4c857SHans Petter Selasky linux_cdev_pager_dtor(void *handle)
5921ea4c857SHans Petter Selasky {
5931ea4c857SHans Petter Selasky 	const struct vm_operations_struct *vm_ops;
5941ea4c857SHans Petter Selasky 	struct vm_area_struct *vmap;
5951ea4c857SHans Petter Selasky 
5961ea4c857SHans Petter Selasky 	vmap = linux_cdev_handle_find(handle);
5971ea4c857SHans Petter Selasky 	MPASS(vmap != NULL);
5981ea4c857SHans Petter Selasky 
599cca15f28SHans Petter Selasky 	/*
600cca15f28SHans Petter Selasky 	 * Remove handle before calling close operation to prevent
601cca15f28SHans Petter Selasky 	 * other threads from reusing the handle pointer.
602cca15f28SHans Petter Selasky 	 */
603cca15f28SHans Petter Selasky 	linux_cdev_handle_remove(vmap);
604cca15f28SHans Petter Selasky 
6051ea4c857SHans Petter Selasky 	down_write(&vmap->vm_mm->mmap_sem);
6061ea4c857SHans Petter Selasky 	vm_ops = vmap->vm_ops;
6071ea4c857SHans Petter Selasky 	if (likely(vm_ops != NULL))
6081ea4c857SHans Petter Selasky 		vm_ops->close(vmap);
6091ea4c857SHans Petter Selasky 	up_write(&vmap->vm_mm->mmap_sem);
6101ea4c857SHans Petter Selasky 
611cca15f28SHans Petter Selasky 	linux_cdev_handle_free(vmap);
6121ea4c857SHans Petter Selasky }
6131ea4c857SHans Petter Selasky 
61461157228SHans Petter Selasky static struct cdev_pager_ops linux_cdev_pager_ops[2] = {
61561157228SHans Petter Selasky   {
61661157228SHans Petter Selasky 	/* OBJT_MGTDEVICE */
6171ea4c857SHans Petter Selasky 	.cdev_pg_populate	= linux_cdev_pager_populate,
6181ea4c857SHans Petter Selasky 	.cdev_pg_ctor	= linux_cdev_pager_ctor,
6191ea4c857SHans Petter Selasky 	.cdev_pg_dtor	= linux_cdev_pager_dtor
62061157228SHans Petter Selasky   },
62161157228SHans Petter Selasky   {
62261157228SHans Petter Selasky 	/* OBJT_DEVICE */
62361157228SHans Petter Selasky 	.cdev_pg_fault	= linux_cdev_pager_fault,
62461157228SHans Petter Selasky 	.cdev_pg_ctor	= linux_cdev_pager_ctor,
62561157228SHans Petter Selasky 	.cdev_pg_dtor	= linux_cdev_pager_dtor
62661157228SHans Petter Selasky   },
6271ea4c857SHans Petter Selasky };
6281ea4c857SHans Petter Selasky 
629e5a3393aSKonstantin Belousov int
630e5a3393aSKonstantin Belousov zap_vma_ptes(struct vm_area_struct *vma, unsigned long address,
631e5a3393aSKonstantin Belousov     unsigned long size)
632e5a3393aSKonstantin Belousov {
633e5a3393aSKonstantin Belousov 	vm_object_t obj;
634e5a3393aSKonstantin Belousov 	vm_page_t m;
635e5a3393aSKonstantin Belousov 
636e5a3393aSKonstantin Belousov 	obj = vma->vm_obj;
637e5a3393aSKonstantin Belousov 	if (obj == NULL || (obj->flags & OBJ_UNMANAGED) != 0)
638e5a3393aSKonstantin Belousov 		return (-ENOTSUP);
639e5a3393aSKonstantin Belousov 	VM_OBJECT_RLOCK(obj);
640e5a3393aSKonstantin Belousov 	for (m = vm_page_find_least(obj, OFF_TO_IDX(address));
641e5a3393aSKonstantin Belousov 	    m != NULL && m->pindex < OFF_TO_IDX(address + size);
642e5a3393aSKonstantin Belousov 	    m = TAILQ_NEXT(m, listq))
643e5a3393aSKonstantin Belousov 		pmap_remove_all(m);
644e5a3393aSKonstantin Belousov 	VM_OBJECT_RUNLOCK(obj);
645e5a3393aSKonstantin Belousov 	return (0);
646e5a3393aSKonstantin Belousov }
647e5a3393aSKonstantin Belousov 
648c72dd0aaSJean-Sébastien Pédron void
649c72dd0aaSJean-Sébastien Pédron vma_set_file(struct vm_area_struct *vma, struct linux_file *file)
650c72dd0aaSJean-Sébastien Pédron {
651c72dd0aaSJean-Sébastien Pédron 	struct linux_file *tmp;
652c72dd0aaSJean-Sébastien Pédron 
653c72dd0aaSJean-Sébastien Pédron 	/* Changing an anonymous vma with this is illegal */
654c72dd0aaSJean-Sébastien Pédron 	get_file(file);
655c72dd0aaSJean-Sébastien Pédron 	tmp = vma->vm_file;
656c72dd0aaSJean-Sébastien Pédron 	vma->vm_file = file;
657c72dd0aaSJean-Sébastien Pédron 	fput(tmp);
658c72dd0aaSJean-Sébastien Pédron }
659c72dd0aaSJean-Sébastien Pédron 
660f823a36eSKonstantin Belousov static struct file_operations dummy_ldev_ops = {
661f823a36eSKonstantin Belousov 	/* XXXKIB */
662f823a36eSKonstantin Belousov };
663f823a36eSKonstantin Belousov 
664f823a36eSKonstantin Belousov static struct linux_cdev dummy_ldev = {
665f823a36eSKonstantin Belousov 	.ops = &dummy_ldev_ops,
666f823a36eSKonstantin Belousov };
667f823a36eSKonstantin Belousov 
668f823a36eSKonstantin Belousov #define	LDEV_SI_DTR	0x0001
669f823a36eSKonstantin Belousov #define	LDEV_SI_REF	0x0002
670f823a36eSKonstantin Belousov 
671f823a36eSKonstantin Belousov static void
672f823a36eSKonstantin Belousov linux_get_fop(struct linux_file *filp, const struct file_operations **fop,
673f823a36eSKonstantin Belousov     struct linux_cdev **dev)
674f823a36eSKonstantin Belousov {
675f823a36eSKonstantin Belousov 	struct linux_cdev *ldev;
676f823a36eSKonstantin Belousov 	u_int siref;
677f823a36eSKonstantin Belousov 
678f823a36eSKonstantin Belousov 	ldev = filp->f_cdev;
679f823a36eSKonstantin Belousov 	*fop = filp->f_op;
680f823a36eSKonstantin Belousov 	if (ldev != NULL) {
681f6b10883SKonstantin Belousov 		if (ldev->kobj.ktype == &linux_cdev_static_ktype) {
682f6b10883SKonstantin Belousov 			refcount_acquire(&ldev->refs);
683f6b10883SKonstantin Belousov 		} else {
684f823a36eSKonstantin Belousov 			for (siref = ldev->siref;;) {
685f823a36eSKonstantin Belousov 				if ((siref & LDEV_SI_DTR) != 0) {
686f823a36eSKonstantin Belousov 					ldev = &dummy_ldev;
687f823a36eSKonstantin Belousov 					*fop = ldev->ops;
688f6b10883SKonstantin Belousov 					siref = ldev->siref;
689f823a36eSKonstantin Belousov 					MPASS((ldev->siref & LDEV_SI_DTR) == 0);
690f6b10883SKonstantin Belousov 				} else if (atomic_fcmpset_int(&ldev->siref,
691f6b10883SKonstantin Belousov 				    &siref, siref + LDEV_SI_REF)) {
692f823a36eSKonstantin Belousov 					break;
693f823a36eSKonstantin Belousov 				}
694f823a36eSKonstantin Belousov 			}
695f823a36eSKonstantin Belousov 		}
696f6b10883SKonstantin Belousov 	}
697f823a36eSKonstantin Belousov 	*dev = ldev;
698f823a36eSKonstantin Belousov }
699f823a36eSKonstantin Belousov 
700f823a36eSKonstantin Belousov static void
701f823a36eSKonstantin Belousov linux_drop_fop(struct linux_cdev *ldev)
702f823a36eSKonstantin Belousov {
703f823a36eSKonstantin Belousov 
704f823a36eSKonstantin Belousov 	if (ldev == NULL)
705f823a36eSKonstantin Belousov 		return;
706f6b10883SKonstantin Belousov 	if (ldev->kobj.ktype == &linux_cdev_static_ktype) {
707f6b10883SKonstantin Belousov 		linux_cdev_deref(ldev);
708f6b10883SKonstantin Belousov 	} else {
709f6b10883SKonstantin Belousov 		MPASS(ldev->kobj.ktype == &linux_cdev_ktype);
710f823a36eSKonstantin Belousov 		MPASS((ldev->siref & ~LDEV_SI_DTR) != 0);
711f823a36eSKonstantin Belousov 		atomic_subtract_int(&ldev->siref, LDEV_SI_REF);
712f823a36eSKonstantin Belousov 	}
713f6b10883SKonstantin Belousov }
714f823a36eSKonstantin Belousov 
715e23ae408SHans Petter Selasky #define	OPW(fp,td,code) ({			\
716e23ae408SHans Petter Selasky 	struct file *__fpop;			\
717f71d0b0dSHans Petter Selasky 	__typeof(code) __retval;		\
718e23ae408SHans Petter Selasky 						\
719e23ae408SHans Petter Selasky 	__fpop = (td)->td_fpop;			\
720e23ae408SHans Petter Selasky 	(td)->td_fpop = (fp);			\
721e23ae408SHans Petter Selasky 	__retval = (code);			\
722e23ae408SHans Petter Selasky 	(td)->td_fpop = __fpop;			\
723e23ae408SHans Petter Selasky 	__retval;				\
724e23ae408SHans Petter Selasky })
725e23ae408SHans Petter Selasky 
7261ea4c857SHans Petter Selasky static int
727f823a36eSKonstantin Belousov linux_dev_fdopen(struct cdev *dev, int fflags, struct thread *td,
728f823a36eSKonstantin Belousov     struct file *file)
7298d59ecb2SHans Petter Selasky {
7308d59ecb2SHans Petter Selasky 	struct linux_cdev *ldev;
7318d59ecb2SHans Petter Selasky 	struct linux_file *filp;
732f823a36eSKonstantin Belousov 	const struct file_operations *fop;
7338d59ecb2SHans Petter Selasky 	int error;
7348d59ecb2SHans Petter Selasky 
7358d59ecb2SHans Petter Selasky 	ldev = dev->si_drv1;
736a6b28ee0SHans Petter Selasky 
737a6b28ee0SHans Petter Selasky 	filp = linux_file_alloc();
7388d59ecb2SHans Petter Selasky 	filp->f_dentry = &filp->f_dentry_store;
7398d59ecb2SHans Petter Selasky 	filp->f_op = ldev->ops;
740e23ae408SHans Petter Selasky 	filp->f_mode = file->f_flag;
7418d59ecb2SHans Petter Selasky 	filp->f_flags = file->f_flag;
7428d59ecb2SHans Petter Selasky 	filp->f_vnode = file->f_vnode;
743f5a9867bSHans Petter Selasky 	filp->_file = file;
744f823a36eSKonstantin Belousov 	refcount_acquire(&ldev->refs);
7459c7b53ccSSlava Shwartsman 	filp->f_cdev = ldev;
746f5a9867bSHans Petter Selasky 
747a6b28ee0SHans Petter Selasky 	linux_set_current(td);
748f823a36eSKonstantin Belousov 	linux_get_fop(filp, &fop, &ldev);
749a6b28ee0SHans Petter Selasky 
750f823a36eSKonstantin Belousov 	if (fop->open != NULL) {
751f823a36eSKonstantin Belousov 		error = -fop->open(file->f_vnode, filp);
752f823a36eSKonstantin Belousov 		if (error != 0) {
753f823a36eSKonstantin Belousov 			linux_drop_fop(ldev);
754f823a36eSKonstantin Belousov 			linux_cdev_deref(filp->f_cdev);
7558d59ecb2SHans Petter Selasky 			kfree(filp);
756464d20bcSHans Petter Selasky 			return (error);
7578d59ecb2SHans Petter Selasky 		}
758e23ae408SHans Petter Selasky 	}
7598d59ecb2SHans Petter Selasky 
760e23ae408SHans Petter Selasky 	/* hold on to the vnode - used for fstat() */
761e23ae408SHans Petter Selasky 	vhold(filp->f_vnode);
7628d59ecb2SHans Petter Selasky 
763e23ae408SHans Petter Selasky 	/* release the file from devfs */
764e23ae408SHans Petter Selasky 	finit(file, filp->f_mode, DTYPE_DEV, filp, &linuxfileops);
765f823a36eSKonstantin Belousov 	linux_drop_fop(ldev);
766e23ae408SHans Petter Selasky 	return (ENXIO);
7678d59ecb2SHans Petter Selasky }
7688d59ecb2SHans Petter Selasky 
7693a8bec33SHans Petter Selasky #define	LINUX_IOCTL_MIN_PTR 0x10000UL
7703a8bec33SHans Petter Selasky #define	LINUX_IOCTL_MAX_PTR (LINUX_IOCTL_MIN_PTR + IOCPARM_MAX)
7713a8bec33SHans Petter Selasky 
7723a8bec33SHans Petter Selasky static inline int
7733a8bec33SHans Petter Selasky linux_remap_address(void **uaddr, size_t len)
7743a8bec33SHans Petter Selasky {
7753a8bec33SHans Petter Selasky 	uintptr_t uaddr_val = (uintptr_t)(*uaddr);
7763a8bec33SHans Petter Selasky 
7773a8bec33SHans Petter Selasky 	if (unlikely(uaddr_val >= LINUX_IOCTL_MIN_PTR &&
7783a8bec33SHans Petter Selasky 	    uaddr_val < LINUX_IOCTL_MAX_PTR)) {
7793a8bec33SHans Petter Selasky 		struct task_struct *pts = current;
7803a8bec33SHans Petter Selasky 		if (pts == NULL) {
7813a8bec33SHans Petter Selasky 			*uaddr = NULL;
7823a8bec33SHans Petter Selasky 			return (1);
7833a8bec33SHans Petter Selasky 		}
7843a8bec33SHans Petter Selasky 
7853a8bec33SHans Petter Selasky 		/* compute data offset */
7863a8bec33SHans Petter Selasky 		uaddr_val -= LINUX_IOCTL_MIN_PTR;
7873a8bec33SHans Petter Selasky 
7883a8bec33SHans Petter Selasky 		/* check that length is within bounds */
7893a8bec33SHans Petter Selasky 		if ((len > IOCPARM_MAX) ||
7903a8bec33SHans Petter Selasky 		    (uaddr_val + len) > pts->bsd_ioctl_len) {
7913a8bec33SHans Petter Selasky 			*uaddr = NULL;
7923a8bec33SHans Petter Selasky 			return (1);
7933a8bec33SHans Petter Selasky 		}
7943a8bec33SHans Petter Selasky 
7953a8bec33SHans Petter Selasky 		/* re-add kernel buffer address */
7963a8bec33SHans Petter Selasky 		uaddr_val += (uintptr_t)pts->bsd_ioctl_data;
7973a8bec33SHans Petter Selasky 
7983a8bec33SHans Petter Selasky 		/* update address location */
7993a8bec33SHans Petter Selasky 		*uaddr = (void *)uaddr_val;
8003a8bec33SHans Petter Selasky 		return (1);
8013a8bec33SHans Petter Selasky 	}
8023a8bec33SHans Petter Selasky 	return (0);
8033a8bec33SHans Petter Selasky }
8043a8bec33SHans Petter Selasky 
8053a8bec33SHans Petter Selasky int
8063a8bec33SHans Petter Selasky linux_copyin(const void *uaddr, void *kaddr, size_t len)
8073a8bec33SHans Petter Selasky {
8083a8bec33SHans Petter Selasky 	if (linux_remap_address(__DECONST(void **, &uaddr), len)) {
8093a8bec33SHans Petter Selasky 		if (uaddr == NULL)
8103a8bec33SHans Petter Selasky 			return (-EFAULT);
8113a8bec33SHans Petter Selasky 		memcpy(kaddr, uaddr, len);
8123a8bec33SHans Petter Selasky 		return (0);
8133a8bec33SHans Petter Selasky 	}
8143a8bec33SHans Petter Selasky 	return (-copyin(uaddr, kaddr, len));
8153a8bec33SHans Petter Selasky }
8163a8bec33SHans Petter Selasky 
8173a8bec33SHans Petter Selasky int
8183a8bec33SHans Petter Selasky linux_copyout(const void *kaddr, void *uaddr, size_t len)
8193a8bec33SHans Petter Selasky {
8203a8bec33SHans Petter Selasky 	if (linux_remap_address(&uaddr, len)) {
8213a8bec33SHans Petter Selasky 		if (uaddr == NULL)
8223a8bec33SHans Petter Selasky 			return (-EFAULT);
8233a8bec33SHans Petter Selasky 		memcpy(uaddr, kaddr, len);
8243a8bec33SHans Petter Selasky 		return (0);
8253a8bec33SHans Petter Selasky 	}
8263a8bec33SHans Petter Selasky 	return (-copyout(kaddr, uaddr, len));
8273a8bec33SHans Petter Selasky }
8283a8bec33SHans Petter Selasky 
8290e05589bSHans Petter Selasky size_t
8300e05589bSHans Petter Selasky linux_clear_user(void *_uaddr, size_t _len)
8310e05589bSHans Petter Selasky {
8320e05589bSHans Petter Selasky 	uint8_t *uaddr = _uaddr;
8330e05589bSHans Petter Selasky 	size_t len = _len;
8340e05589bSHans Petter Selasky 
8350e05589bSHans Petter Selasky 	/* make sure uaddr is aligned before going into the fast loop */
8360e05589bSHans Petter Selasky 	while (((uintptr_t)uaddr & 7) != 0 && len > 7) {
8370e05589bSHans Petter Selasky 		if (subyte(uaddr, 0))
8380e05589bSHans Petter Selasky 			return (_len);
8390e05589bSHans Petter Selasky 		uaddr++;
8400e05589bSHans Petter Selasky 		len--;
8410e05589bSHans Petter Selasky 	}
8420e05589bSHans Petter Selasky 
8430e05589bSHans Petter Selasky 	/* zero 8 bytes at a time */
8440e05589bSHans Petter Selasky 	while (len > 7) {
8453803a97fSHans Petter Selasky #ifdef __LP64__
8460e05589bSHans Petter Selasky 		if (suword64(uaddr, 0))
8470e05589bSHans Petter Selasky 			return (_len);
8483803a97fSHans Petter Selasky #else
8493803a97fSHans Petter Selasky 		if (suword32(uaddr, 0))
8503803a97fSHans Petter Selasky 			return (_len);
8513803a97fSHans Petter Selasky 		if (suword32(uaddr + 4, 0))
8523803a97fSHans Petter Selasky 			return (_len);
8533803a97fSHans Petter Selasky #endif
8540e05589bSHans Petter Selasky 		uaddr += 8;
8550e05589bSHans Petter Selasky 		len -= 8;
8560e05589bSHans Petter Selasky 	}
8570e05589bSHans Petter Selasky 
8580e05589bSHans Petter Selasky 	/* zero fill end, if any */
8590e05589bSHans Petter Selasky 	while (len > 0) {
8600e05589bSHans Petter Selasky 		if (subyte(uaddr, 0))
8610e05589bSHans Petter Selasky 			return (_len);
8620e05589bSHans Petter Selasky 		uaddr++;
8630e05589bSHans Petter Selasky 		len--;
8640e05589bSHans Petter Selasky 	}
8650e05589bSHans Petter Selasky 	return (0);
8660e05589bSHans Petter Selasky }
8670e05589bSHans Petter Selasky 
8680e05589bSHans Petter Selasky int
86947e2723aSJohannes Lundberg linux_access_ok(const void *uaddr, size_t len)
8700e05589bSHans Petter Selasky {
8710e05589bSHans Petter Selasky 	uintptr_t saddr;
8720e05589bSHans Petter Selasky 	uintptr_t eaddr;
8730e05589bSHans Petter Selasky 
8740e05589bSHans Petter Selasky 	/* get start and end address */
8750e05589bSHans Petter Selasky 	saddr = (uintptr_t)uaddr;
8760e05589bSHans Petter Selasky 	eaddr = (uintptr_t)uaddr + len;
8770e05589bSHans Petter Selasky 
8780e05589bSHans Petter Selasky 	/* verify addresses are valid for userspace */
8790e05589bSHans Petter Selasky 	return ((saddr == eaddr) ||
8800e05589bSHans Petter Selasky 	    (eaddr > saddr && eaddr <= VM_MAXUSER_ADDRESS));
8810e05589bSHans Petter Selasky }
8820e05589bSHans Petter Selasky 
88394944062SHans Petter Selasky /*
88494944062SHans Petter Selasky  * This function should return either EINTR or ERESTART depending on
88594944062SHans Petter Selasky  * the signal type sent to this thread:
88694944062SHans Petter Selasky  */
88794944062SHans Petter Selasky static int
88894944062SHans Petter Selasky linux_get_error(struct task_struct *task, int error)
88994944062SHans Petter Selasky {
89094944062SHans Petter Selasky 	/* check for signal type interrupt code */
89194944062SHans Petter Selasky 	if (error == EINTR || error == ERESTARTSYS || error == ERESTART) {
89294944062SHans Petter Selasky 		error = -linux_schedule_get_interrupt_value(task);
89394944062SHans Petter Selasky 		if (error == 0)
89494944062SHans Petter Selasky 			error = EINTR;
89594944062SHans Petter Selasky 	}
89694944062SHans Petter Selasky 	return (error);
89794944062SHans Petter Selasky }
89894944062SHans Petter Selasky 
8998d59ecb2SHans Petter Selasky static int
900e23ae408SHans Petter Selasky linux_file_ioctl_sub(struct file *fp, struct linux_file *filp,
901f823a36eSKonstantin Belousov     const struct file_operations *fop, u_long cmd, caddr_t data,
902f823a36eSKonstantin Belousov     struct thread *td)
9038d59ecb2SHans Petter Selasky {
90494944062SHans Petter Selasky 	struct task_struct *task = current;
9053a8bec33SHans Petter Selasky 	unsigned size;
9068d59ecb2SHans Petter Selasky 	int error;
9078d59ecb2SHans Petter Selasky 
9083a8bec33SHans Petter Selasky 	size = IOCPARM_LEN(cmd);
9093a8bec33SHans Petter Selasky 	/* refer to logic in sys_ioctl() */
9103a8bec33SHans Petter Selasky 	if (size > 0) {
9118d59ecb2SHans Petter Selasky 		/*
9123a8bec33SHans Petter Selasky 		 * Setup hint for linux_copyin() and linux_copyout().
9133a8bec33SHans Petter Selasky 		 *
9143a8bec33SHans Petter Selasky 		 * Background: Linux code expects a user-space address
9153a8bec33SHans Petter Selasky 		 * while FreeBSD supplies a kernel-space address.
9168d59ecb2SHans Petter Selasky 		 */
91794944062SHans Petter Selasky 		task->bsd_ioctl_data = data;
91894944062SHans Petter Selasky 		task->bsd_ioctl_len = size;
9193a8bec33SHans Petter Selasky 		data = (void *)LINUX_IOCTL_MIN_PTR;
9203a8bec33SHans Petter Selasky 	} else {
9213a8bec33SHans Petter Selasky 		/* fetch user-space pointer */
9228d59ecb2SHans Petter Selasky 		data = *(void **)data;
9233a8bec33SHans Petter Selasky 	}
9241fd26da9SDmitry Chagin #ifdef COMPAT_FREEBSD32
925945accf5SDmitry Chagin 	if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
92640f53a7cSHans Petter Selasky 		/* try the compat IOCTL handler first */
927f823a36eSKonstantin Belousov 		if (fop->compat_ioctl != NULL) {
928f823a36eSKonstantin Belousov 			error = -OPW(fp, td, fop->compat_ioctl(filp,
929f823a36eSKonstantin Belousov 			    cmd, (u_long)data));
930f823a36eSKonstantin Belousov 		} else {
93140f53a7cSHans Petter Selasky 			error = ENOTTY;
932f823a36eSKonstantin Belousov 		}
93340f53a7cSHans Petter Selasky 
93440f53a7cSHans Petter Selasky 		/* fallback to the regular IOCTL handler, if any */
935f823a36eSKonstantin Belousov 		if (error == ENOTTY && fop->unlocked_ioctl != NULL) {
936f823a36eSKonstantin Belousov 			error = -OPW(fp, td, fop->unlocked_ioctl(filp,
937f823a36eSKonstantin Belousov 			    cmd, (u_long)data));
938f823a36eSKonstantin Belousov 		}
93940f53a7cSHans Petter Selasky 	} else
94040f53a7cSHans Petter Selasky #endif
941f823a36eSKonstantin Belousov 	{
942f823a36eSKonstantin Belousov 		if (fop->unlocked_ioctl != NULL) {
943f823a36eSKonstantin Belousov 			error = -OPW(fp, td, fop->unlocked_ioctl(filp,
944f823a36eSKonstantin Belousov 			    cmd, (u_long)data));
945f823a36eSKonstantin Belousov 		} else {
9468d59ecb2SHans Petter Selasky 			error = ENOTTY;
947f823a36eSKonstantin Belousov 		}
948f823a36eSKonstantin Belousov 	}
9491e3db1deSHans Petter Selasky 	if (size > 0) {
95094944062SHans Petter Selasky 		task->bsd_ioctl_data = NULL;
95194944062SHans Petter Selasky 		task->bsd_ioctl_len = 0;
9521e3db1deSHans Petter Selasky 	}
9538d59ecb2SHans Petter Selasky 
95467e984c8SHans Petter Selasky 	if (error == EWOULDBLOCK) {
95567e984c8SHans Petter Selasky 		/* update kqfilter status, if any */
956e23ae408SHans Petter Selasky 		linux_file_kqfilter_poll(filp,
95767e984c8SHans Petter Selasky 		    LINUX_KQ_FLAG_HAS_READ | LINUX_KQ_FLAG_HAS_WRITE);
95894944062SHans Petter Selasky 	} else {
95994944062SHans Petter Selasky 		error = linux_get_error(task, error);
96094944062SHans Petter Selasky 	}
9618d59ecb2SHans Petter Selasky 	return (error);
9628d59ecb2SHans Petter Selasky }
9638d59ecb2SHans Petter Selasky 
964627ac5b4SHans Petter Selasky #define	LINUX_POLL_TABLE_NORMAL ((poll_table *)1)
965627ac5b4SHans Petter Selasky 
9666dec7efaSHans Petter Selasky /*
9676dec7efaSHans Petter Selasky  * This function atomically updates the poll wakeup state and returns
9686dec7efaSHans Petter Selasky  * the previous state at the time of update.
9696dec7efaSHans Petter Selasky  */
9706dec7efaSHans Petter Selasky static uint8_t
9716dec7efaSHans Petter Selasky linux_poll_wakeup_state(atomic_t *v, const uint8_t *pstate)
9726dec7efaSHans Petter Selasky {
9736dec7efaSHans Petter Selasky 	int c, old;
9746dec7efaSHans Petter Selasky 
9756dec7efaSHans Petter Selasky 	c = v->counter;
9766dec7efaSHans Petter Selasky 
9776dec7efaSHans Petter Selasky 	while ((old = atomic_cmpxchg(v, c, pstate[c])) != c)
9786dec7efaSHans Petter Selasky 		c = old;
9796dec7efaSHans Petter Selasky 
9806dec7efaSHans Petter Selasky 	return (c);
9816dec7efaSHans Petter Selasky }
9826dec7efaSHans Petter Selasky 
9836dec7efaSHans Petter Selasky static int
9846dec7efaSHans Petter Selasky linux_poll_wakeup_callback(wait_queue_t *wq, unsigned int wq_state, int flags, void *key)
9856dec7efaSHans Petter Selasky {
9866dec7efaSHans Petter Selasky 	static const uint8_t state[LINUX_FWQ_STATE_MAX] = {
9876dec7efaSHans Petter Selasky 		[LINUX_FWQ_STATE_INIT] = LINUX_FWQ_STATE_INIT, /* NOP */
9886dec7efaSHans Petter Selasky 		[LINUX_FWQ_STATE_NOT_READY] = LINUX_FWQ_STATE_NOT_READY, /* NOP */
9896dec7efaSHans Petter Selasky 		[LINUX_FWQ_STATE_QUEUED] = LINUX_FWQ_STATE_READY,
9906dec7efaSHans Petter Selasky 		[LINUX_FWQ_STATE_READY] = LINUX_FWQ_STATE_READY, /* NOP */
9916dec7efaSHans Petter Selasky 	};
9926dec7efaSHans Petter Selasky 	struct linux_file *filp = container_of(wq, struct linux_file, f_wait_queue.wq);
9936dec7efaSHans Petter Selasky 
9946dec7efaSHans Petter Selasky 	switch (linux_poll_wakeup_state(&filp->f_wait_queue.state, state)) {
9956dec7efaSHans Petter Selasky 	case LINUX_FWQ_STATE_QUEUED:
9966dec7efaSHans Petter Selasky 		linux_poll_wakeup(filp);
9976dec7efaSHans Petter Selasky 		return (1);
9986dec7efaSHans Petter Selasky 	default:
9996dec7efaSHans Petter Selasky 		return (0);
10006dec7efaSHans Petter Selasky 	}
10016dec7efaSHans Petter Selasky }
10026dec7efaSHans Petter Selasky 
10036dec7efaSHans Petter Selasky void
10046dec7efaSHans Petter Selasky linux_poll_wait(struct linux_file *filp, wait_queue_head_t *wqh, poll_table *p)
10056dec7efaSHans Petter Selasky {
10066dec7efaSHans Petter Selasky 	static const uint8_t state[LINUX_FWQ_STATE_MAX] = {
10076dec7efaSHans Petter Selasky 		[LINUX_FWQ_STATE_INIT] = LINUX_FWQ_STATE_NOT_READY,
10086dec7efaSHans Petter Selasky 		[LINUX_FWQ_STATE_NOT_READY] = LINUX_FWQ_STATE_NOT_READY, /* NOP */
10096dec7efaSHans Petter Selasky 		[LINUX_FWQ_STATE_QUEUED] = LINUX_FWQ_STATE_QUEUED, /* NOP */
10106dec7efaSHans Petter Selasky 		[LINUX_FWQ_STATE_READY] = LINUX_FWQ_STATE_QUEUED,
10116dec7efaSHans Petter Selasky 	};
10126dec7efaSHans Petter Selasky 
1013627ac5b4SHans Petter Selasky 	/* check if we are called inside the select system call */
1014627ac5b4SHans Petter Selasky 	if (p == LINUX_POLL_TABLE_NORMAL)
10156dec7efaSHans Petter Selasky 		selrecord(curthread, &filp->f_selinfo);
10166dec7efaSHans Petter Selasky 
10176dec7efaSHans Petter Selasky 	switch (linux_poll_wakeup_state(&filp->f_wait_queue.state, state)) {
10186dec7efaSHans Petter Selasky 	case LINUX_FWQ_STATE_INIT:
10196dec7efaSHans Petter Selasky 		/* NOTE: file handles can only belong to one wait-queue */
10206dec7efaSHans Petter Selasky 		filp->f_wait_queue.wqh = wqh;
10216dec7efaSHans Petter Selasky 		filp->f_wait_queue.wq.func = &linux_poll_wakeup_callback;
10226dec7efaSHans Petter Selasky 		add_wait_queue(wqh, &filp->f_wait_queue.wq);
10236dec7efaSHans Petter Selasky 		atomic_set(&filp->f_wait_queue.state, LINUX_FWQ_STATE_QUEUED);
10246dec7efaSHans Petter Selasky 		break;
10256dec7efaSHans Petter Selasky 	default:
10266dec7efaSHans Petter Selasky 		break;
10276dec7efaSHans Petter Selasky 	}
10286dec7efaSHans Petter Selasky }
10296dec7efaSHans Petter Selasky 
10306dec7efaSHans Petter Selasky static void
10316dec7efaSHans Petter Selasky linux_poll_wait_dequeue(struct linux_file *filp)
10326dec7efaSHans Petter Selasky {
10336dec7efaSHans Petter Selasky 	static const uint8_t state[LINUX_FWQ_STATE_MAX] = {
10346dec7efaSHans Petter Selasky 		[LINUX_FWQ_STATE_INIT] = LINUX_FWQ_STATE_INIT,	/* NOP */
10356dec7efaSHans Petter Selasky 		[LINUX_FWQ_STATE_NOT_READY] = LINUX_FWQ_STATE_INIT,
10366dec7efaSHans Petter Selasky 		[LINUX_FWQ_STATE_QUEUED] = LINUX_FWQ_STATE_INIT,
10376dec7efaSHans Petter Selasky 		[LINUX_FWQ_STATE_READY] = LINUX_FWQ_STATE_INIT,
10386dec7efaSHans Petter Selasky 	};
10396dec7efaSHans Petter Selasky 
10406dec7efaSHans Petter Selasky 	seldrain(&filp->f_selinfo);
10416dec7efaSHans Petter Selasky 
10426dec7efaSHans Petter Selasky 	switch (linux_poll_wakeup_state(&filp->f_wait_queue.state, state)) {
10436dec7efaSHans Petter Selasky 	case LINUX_FWQ_STATE_NOT_READY:
10446dec7efaSHans Petter Selasky 	case LINUX_FWQ_STATE_QUEUED:
10456dec7efaSHans Petter Selasky 	case LINUX_FWQ_STATE_READY:
10466dec7efaSHans Petter Selasky 		remove_wait_queue(filp->f_wait_queue.wqh, &filp->f_wait_queue.wq);
10476dec7efaSHans Petter Selasky 		break;
10486dec7efaSHans Petter Selasky 	default:
10496dec7efaSHans Petter Selasky 		break;
10506dec7efaSHans Petter Selasky 	}
10516dec7efaSHans Petter Selasky }
10526dec7efaSHans Petter Selasky 
1053a6b28ee0SHans Petter Selasky void
1054a6b28ee0SHans Petter Selasky linux_poll_wakeup(struct linux_file *filp)
1055a6b28ee0SHans Petter Selasky {
1056a6b28ee0SHans Petter Selasky 	/* this function should be NULL-safe */
1057a6b28ee0SHans Petter Selasky 	if (filp == NULL)
1058a6b28ee0SHans Petter Selasky 		return;
1059a6b28ee0SHans Petter Selasky 
1060a6b28ee0SHans Petter Selasky 	selwakeup(&filp->f_selinfo);
1061a6b28ee0SHans Petter Selasky 
1062a6b28ee0SHans Petter Selasky 	spin_lock(&filp->f_kqlock);
1063a6b28ee0SHans Petter Selasky 	filp->f_kqflags |= LINUX_KQ_FLAG_NEED_READ |
1064a6b28ee0SHans Petter Selasky 	    LINUX_KQ_FLAG_NEED_WRITE;
1065a6b28ee0SHans Petter Selasky 
1066a6b28ee0SHans Petter Selasky 	/* make sure the "knote" gets woken up */
1067a6b28ee0SHans Petter Selasky 	KNOTE_LOCKED(&filp->f_selinfo.si_note, 1);
1068a6b28ee0SHans Petter Selasky 	spin_unlock(&filp->f_kqlock);
1069a6b28ee0SHans Petter Selasky }
1070a6b28ee0SHans Petter Selasky 
1071a6b28ee0SHans Petter Selasky static void
1072e23ae408SHans Petter Selasky linux_file_kqfilter_detach(struct knote *kn)
1073a6b28ee0SHans Petter Selasky {
1074a6b28ee0SHans Petter Selasky 	struct linux_file *filp = kn->kn_hook;
1075a6b28ee0SHans Petter Selasky 
1076a6b28ee0SHans Petter Selasky 	spin_lock(&filp->f_kqlock);
1077a6b28ee0SHans Petter Selasky 	knlist_remove(&filp->f_selinfo.si_note, kn, 1);
1078a6b28ee0SHans Petter Selasky 	spin_unlock(&filp->f_kqlock);
1079a6b28ee0SHans Petter Selasky }
1080a6b28ee0SHans Petter Selasky 
1081a6b28ee0SHans Petter Selasky static int
1082e23ae408SHans Petter Selasky linux_file_kqfilter_read_event(struct knote *kn, long hint)
1083a6b28ee0SHans Petter Selasky {
1084a6b28ee0SHans Petter Selasky 	struct linux_file *filp = kn->kn_hook;
1085a6b28ee0SHans Petter Selasky 
1086a6b28ee0SHans Petter Selasky 	mtx_assert(&filp->f_kqlock.m, MA_OWNED);
1087a6b28ee0SHans Petter Selasky 
1088a6b28ee0SHans Petter Selasky 	return ((filp->f_kqflags & LINUX_KQ_FLAG_NEED_READ) ? 1 : 0);
1089a6b28ee0SHans Petter Selasky }
1090a6b28ee0SHans Petter Selasky 
1091a6b28ee0SHans Petter Selasky static int
1092e23ae408SHans Petter Selasky linux_file_kqfilter_write_event(struct knote *kn, long hint)
1093a6b28ee0SHans Petter Selasky {
1094a6b28ee0SHans Petter Selasky 	struct linux_file *filp = kn->kn_hook;
1095a6b28ee0SHans Petter Selasky 
1096a6b28ee0SHans Petter Selasky 	mtx_assert(&filp->f_kqlock.m, MA_OWNED);
1097a6b28ee0SHans Petter Selasky 
1098a6b28ee0SHans Petter Selasky 	return ((filp->f_kqflags & LINUX_KQ_FLAG_NEED_WRITE) ? 1 : 0);
1099a6b28ee0SHans Petter Selasky }
1100a6b28ee0SHans Petter Selasky 
1101a6b28ee0SHans Petter Selasky static struct filterops linux_dev_kqfiltops_read = {
1102a6b28ee0SHans Petter Selasky 	.f_isfd = 1,
1103e23ae408SHans Petter Selasky 	.f_detach = linux_file_kqfilter_detach,
1104e23ae408SHans Petter Selasky 	.f_event = linux_file_kqfilter_read_event,
1105a6b28ee0SHans Petter Selasky };
1106a6b28ee0SHans Petter Selasky 
1107a6b28ee0SHans Petter Selasky static struct filterops linux_dev_kqfiltops_write = {
1108a6b28ee0SHans Petter Selasky 	.f_isfd = 1,
1109e23ae408SHans Petter Selasky 	.f_detach = linux_file_kqfilter_detach,
1110e23ae408SHans Petter Selasky 	.f_event = linux_file_kqfilter_write_event,
1111a6b28ee0SHans Petter Selasky };
1112a6b28ee0SHans Petter Selasky 
1113a6b28ee0SHans Petter Selasky static void
1114e23ae408SHans Petter Selasky linux_file_kqfilter_poll(struct linux_file *filp, int kqflags)
1115a6b28ee0SHans Petter Selasky {
1116f823a36eSKonstantin Belousov 	struct thread *td;
1117f823a36eSKonstantin Belousov 	const struct file_operations *fop;
1118f823a36eSKonstantin Belousov 	struct linux_cdev *ldev;
1119a6b28ee0SHans Petter Selasky 	int temp;
1120a6b28ee0SHans Petter Selasky 
1121f823a36eSKonstantin Belousov 	if ((filp->f_kqflags & kqflags) == 0)
1122f823a36eSKonstantin Belousov 		return;
1123e23ae408SHans Petter Selasky 
1124f823a36eSKonstantin Belousov 	td = curthread;
1125f823a36eSKonstantin Belousov 
1126f823a36eSKonstantin Belousov 	linux_get_fop(filp, &fop, &ldev);
1127a6b28ee0SHans Petter Selasky 	/* get the latest polling state */
1128f823a36eSKonstantin Belousov 	temp = OPW(filp->_file, td, fop->poll(filp, NULL));
1129f823a36eSKonstantin Belousov 	linux_drop_fop(ldev);
1130a6b28ee0SHans Petter Selasky 
1131a6b28ee0SHans Petter Selasky 	spin_lock(&filp->f_kqlock);
113267e984c8SHans Petter Selasky 	/* clear kqflags */
113367e984c8SHans Petter Selasky 	filp->f_kqflags &= ~(LINUX_KQ_FLAG_NEED_READ |
113467e984c8SHans Petter Selasky 	    LINUX_KQ_FLAG_NEED_WRITE);
113567e984c8SHans Petter Selasky 	/* update kqflags */
1136f823a36eSKonstantin Belousov 	if ((temp & (POLLIN | POLLOUT)) != 0) {
1137f823a36eSKonstantin Belousov 		if ((temp & POLLIN) != 0)
1138a6b28ee0SHans Petter Selasky 			filp->f_kqflags |= LINUX_KQ_FLAG_NEED_READ;
1139f823a36eSKonstantin Belousov 		if ((temp & POLLOUT) != 0)
1140a6b28ee0SHans Petter Selasky 			filp->f_kqflags |= LINUX_KQ_FLAG_NEED_WRITE;
1141a6b28ee0SHans Petter Selasky 
1142a6b28ee0SHans Petter Selasky 		/* make sure the "knote" gets woken up */
1143a6b28ee0SHans Petter Selasky 		KNOTE_LOCKED(&filp->f_selinfo.si_note, 0);
1144a6b28ee0SHans Petter Selasky 	}
114567e984c8SHans Petter Selasky 	spin_unlock(&filp->f_kqlock);
1146a6b28ee0SHans Petter Selasky }
1147a6b28ee0SHans Petter Selasky 
1148a6b28ee0SHans Petter Selasky static int
1149e23ae408SHans Petter Selasky linux_file_kqfilter(struct file *file, struct knote *kn)
1150a6b28ee0SHans Petter Selasky {
1151a6b28ee0SHans Petter Selasky 	struct linux_file *filp;
1152a6b28ee0SHans Petter Selasky 	struct thread *td;
1153a6b28ee0SHans Petter Selasky 	int error;
1154a6b28ee0SHans Petter Selasky 
1155a6b28ee0SHans Petter Selasky 	td = curthread;
1156e23ae408SHans Petter Selasky 	filp = (struct linux_file *)file->f_data;
1157a6b28ee0SHans Petter Selasky 	filp->f_flags = file->f_flag;
1158a6b28ee0SHans Petter Selasky 	if (filp->f_op->poll == NULL)
1159a6b28ee0SHans Petter Selasky 		return (EINVAL);
1160a6b28ee0SHans Petter Selasky 
1161a6b28ee0SHans Petter Selasky 	spin_lock(&filp->f_kqlock);
1162a6b28ee0SHans Petter Selasky 	switch (kn->kn_filter) {
1163a6b28ee0SHans Petter Selasky 	case EVFILT_READ:
1164a6b28ee0SHans Petter Selasky 		filp->f_kqflags |= LINUX_KQ_FLAG_HAS_READ;
1165a6b28ee0SHans Petter Selasky 		kn->kn_fop = &linux_dev_kqfiltops_read;
1166a6b28ee0SHans Petter Selasky 		kn->kn_hook = filp;
1167a6b28ee0SHans Petter Selasky 		knlist_add(&filp->f_selinfo.si_note, kn, 1);
1168e23ae408SHans Petter Selasky 		error = 0;
1169a6b28ee0SHans Petter Selasky 		break;
1170a6b28ee0SHans Petter Selasky 	case EVFILT_WRITE:
1171a6b28ee0SHans Petter Selasky 		filp->f_kqflags |= LINUX_KQ_FLAG_HAS_WRITE;
1172a6b28ee0SHans Petter Selasky 		kn->kn_fop = &linux_dev_kqfiltops_write;
1173a6b28ee0SHans Petter Selasky 		kn->kn_hook = filp;
1174a6b28ee0SHans Petter Selasky 		knlist_add(&filp->f_selinfo.si_note, kn, 1);
1175e23ae408SHans Petter Selasky 		error = 0;
1176a6b28ee0SHans Petter Selasky 		break;
1177a6b28ee0SHans Petter Selasky 	default:
1178a6b28ee0SHans Petter Selasky 		error = EINVAL;
1179a6b28ee0SHans Petter Selasky 		break;
1180a6b28ee0SHans Petter Selasky 	}
1181a6b28ee0SHans Petter Selasky 	spin_unlock(&filp->f_kqlock);
1182a6b28ee0SHans Petter Selasky 
1183a6b28ee0SHans Petter Selasky 	if (error == 0) {
1184a6b28ee0SHans Petter Selasky 		linux_set_current(td);
118567e984c8SHans Petter Selasky 
118667e984c8SHans Petter Selasky 		/* update kqfilter status, if any */
1187e23ae408SHans Petter Selasky 		linux_file_kqfilter_poll(filp,
118867e984c8SHans Petter Selasky 		    LINUX_KQ_FLAG_HAS_READ | LINUX_KQ_FLAG_HAS_WRITE);
1189a6b28ee0SHans Petter Selasky 	}
1190a6b28ee0SHans Petter Selasky 	return (error);
1191a6b28ee0SHans Petter Selasky }
1192a6b28ee0SHans Petter Selasky 
11938d59ecb2SHans Petter Selasky static int
1194f823a36eSKonstantin Belousov linux_file_mmap_single(struct file *fp, const struct file_operations *fop,
1195f823a36eSKonstantin Belousov     vm_ooffset_t *offset, vm_size_t size, struct vm_object **object,
1196904390b4SHans Petter Selasky     int nprot, bool is_shared, struct thread *td)
11978d59ecb2SHans Petter Selasky {
119894944062SHans Petter Selasky 	struct task_struct *task;
11991ea4c857SHans Petter Selasky 	struct vm_area_struct *vmap;
1200cca15f28SHans Petter Selasky 	struct mm_struct *mm;
12018d59ecb2SHans Petter Selasky 	struct linux_file *filp;
12021ea4c857SHans Petter Selasky 	vm_memattr_t attr;
12038d59ecb2SHans Petter Selasky 	int error;
12048d59ecb2SHans Petter Selasky 
1205e23ae408SHans Petter Selasky 	filp = (struct linux_file *)fp->f_data;
1206e23ae408SHans Petter Selasky 	filp->f_flags = fp->f_flag;
12071ea4c857SHans Petter Selasky 
1208f823a36eSKonstantin Belousov 	if (fop->mmap == NULL)
1209e23ae408SHans Petter Selasky 		return (EOPNOTSUPP);
12101ea4c857SHans Petter Selasky 
12111e3db1deSHans Petter Selasky 	linux_set_current(td);
12121ea4c857SHans Petter Selasky 
1213cca15f28SHans Petter Selasky 	/*
1214cca15f28SHans Petter Selasky 	 * The same VM object might be shared by multiple processes
1215cca15f28SHans Petter Selasky 	 * and the mm_struct is usually freed when a process exits.
1216cca15f28SHans Petter Selasky 	 *
1217cca15f28SHans Petter Selasky 	 * The atomic reference below makes sure the mm_struct is
1218cca15f28SHans Petter Selasky 	 * available as long as the vmap is in the linux_vma_head.
1219cca15f28SHans Petter Selasky 	 */
122094944062SHans Petter Selasky 	task = current;
122194944062SHans Petter Selasky 	mm = task->mm;
1222cca15f28SHans Petter Selasky 	if (atomic_inc_not_zero(&mm->mm_users) == 0)
1223cca15f28SHans Petter Selasky 		return (EINVAL);
1224cca15f28SHans Petter Selasky 
12251ea4c857SHans Petter Selasky 	vmap = kzalloc(sizeof(*vmap), GFP_KERNEL);
12261ea4c857SHans Petter Selasky 	vmap->vm_start = 0;
12271ea4c857SHans Petter Selasky 	vmap->vm_end = size;
12281ea4c857SHans Petter Selasky 	vmap->vm_pgoff = *offset / PAGE_SIZE;
12291ea4c857SHans Petter Selasky 	vmap->vm_pfn = 0;
1230ebf85480SHans Petter Selasky 	vmap->vm_flags = vmap->vm_page_prot = (nprot & VM_PROT_ALL);
1231904390b4SHans Petter Selasky 	if (is_shared)
1232904390b4SHans Petter Selasky 		vmap->vm_flags |= VM_SHARED;
12331ea4c857SHans Petter Selasky 	vmap->vm_ops = NULL;
1234d56f1ed8SHans Petter Selasky 	vmap->vm_file = get_file(filp);
1235cca15f28SHans Petter Selasky 	vmap->vm_mm = mm;
12361ea4c857SHans Petter Selasky 
12371ea4c857SHans Petter Selasky 	if (unlikely(down_write_killable(&vmap->vm_mm->mmap_sem))) {
123894944062SHans Petter Selasky 		error = linux_get_error(task, EINTR);
12391ea4c857SHans Petter Selasky 	} else {
1240f823a36eSKonstantin Belousov 		error = -OPW(fp, td, fop->mmap(filp, vmap));
124194944062SHans Petter Selasky 		error = linux_get_error(task, error);
12421ea4c857SHans Petter Selasky 		up_write(&vmap->vm_mm->mmap_sem);
12431ea4c857SHans Petter Selasky 	}
12441ea4c857SHans Petter Selasky 
12451ea4c857SHans Petter Selasky 	if (error != 0) {
1246cca15f28SHans Petter Selasky 		linux_cdev_handle_free(vmap);
12471ea4c857SHans Petter Selasky 		return (error);
12481ea4c857SHans Petter Selasky 	}
12491ea4c857SHans Petter Selasky 
12501ea4c857SHans Petter Selasky 	attr = pgprot2cachemode(vmap->vm_page_prot);
12511ea4c857SHans Petter Selasky 
12521ea4c857SHans Petter Selasky 	if (vmap->vm_ops != NULL) {
12530c19d064SHans Petter Selasky 		struct vm_area_struct *ptr;
12541ea4c857SHans Petter Selasky 		void *vm_private_data;
12550c19d064SHans Petter Selasky 		bool vm_no_fault;
12561ea4c857SHans Petter Selasky 
1257cde3f930SHans Petter Selasky 		if (vmap->vm_ops->open == NULL ||
12581ea4c857SHans Petter Selasky 		    vmap->vm_ops->close == NULL ||
12591ea4c857SHans Petter Selasky 		    vmap->vm_private_data == NULL) {
12600c19d064SHans Petter Selasky 			/* free allocated VM area struct */
1261cca15f28SHans Petter Selasky 			linux_cdev_handle_free(vmap);
12621ea4c857SHans Petter Selasky 			return (EINVAL);
12631ea4c857SHans Petter Selasky 		}
12641ea4c857SHans Petter Selasky 
12651ea4c857SHans Petter Selasky 		vm_private_data = vmap->vm_private_data;
12661ea4c857SHans Petter Selasky 
12670c19d064SHans Petter Selasky 		rw_wlock(&linux_vma_lock);
12680c19d064SHans Petter Selasky 		TAILQ_FOREACH(ptr, &linux_vma_head, vm_entry) {
12690c19d064SHans Petter Selasky 			if (ptr->vm_private_data == vm_private_data)
12700c19d064SHans Petter Selasky 				break;
12710c19d064SHans Petter Selasky 		}
12720c19d064SHans Petter Selasky 		/* check if there is an existing VM area struct */
12730c19d064SHans Petter Selasky 		if (ptr != NULL) {
12740c19d064SHans Petter Selasky 			/* check if the VM area structure is invalid */
12750c19d064SHans Petter Selasky 			if (ptr->vm_ops == NULL ||
12760c19d064SHans Petter Selasky 			    ptr->vm_ops->open == NULL ||
12770c19d064SHans Petter Selasky 			    ptr->vm_ops->close == NULL) {
12780c19d064SHans Petter Selasky 				error = ESTALE;
12790c19d064SHans Petter Selasky 				vm_no_fault = 1;
12800c19d064SHans Petter Selasky 			} else {
12810c19d064SHans Petter Selasky 				error = EEXIST;
12820c19d064SHans Petter Selasky 				vm_no_fault = (ptr->vm_ops->fault == NULL);
12830c19d064SHans Petter Selasky 			}
12840c19d064SHans Petter Selasky 		} else {
12850c19d064SHans Petter Selasky 			/* insert VM area structure into list */
12860c19d064SHans Petter Selasky 			TAILQ_INSERT_TAIL(&linux_vma_head, vmap, vm_entry);
12870c19d064SHans Petter Selasky 			error = 0;
12880c19d064SHans Petter Selasky 			vm_no_fault = (vmap->vm_ops->fault == NULL);
12890c19d064SHans Petter Selasky 		}
12900c19d064SHans Petter Selasky 		rw_wunlock(&linux_vma_lock);
12911ea4c857SHans Petter Selasky 
12920c19d064SHans Petter Selasky 		if (error != 0) {
12930c19d064SHans Petter Selasky 			/* free allocated VM area struct */
12940c19d064SHans Petter Selasky 			linux_cdev_handle_free(vmap);
12950c19d064SHans Petter Selasky 			/* check for stale VM area struct */
12960c19d064SHans Petter Selasky 			if (error != EEXIST)
12970c19d064SHans Petter Selasky 				return (error);
12980c19d064SHans Petter Selasky 		}
12990c19d064SHans Petter Selasky 
13000c19d064SHans Petter Selasky 		/* check if there is no fault handler */
13010c19d064SHans Petter Selasky 		if (vm_no_fault) {
130261157228SHans Petter Selasky 			*object = cdev_pager_allocate(vm_private_data, OBJT_DEVICE,
130361157228SHans Petter Selasky 			    &linux_cdev_pager_ops[1], size, nprot, *offset,
1304e23ae408SHans Petter Selasky 			    td->td_ucred);
130561157228SHans Petter Selasky 		} else {
13061ea4c857SHans Petter Selasky 			*object = cdev_pager_allocate(vm_private_data, OBJT_MGTDEVICE,
130761157228SHans Petter Selasky 			    &linux_cdev_pager_ops[0], size, nprot, *offset,
1308e23ae408SHans Petter Selasky 			    td->td_ucred);
130961157228SHans Petter Selasky 		}
13101ea4c857SHans Petter Selasky 
13110c19d064SHans Petter Selasky 		/* check if allocating the VM object failed */
13121ea4c857SHans Petter Selasky 		if (*object == NULL) {
13130c19d064SHans Petter Selasky 			if (error == 0) {
13140c19d064SHans Petter Selasky 				/* remove VM area struct from list */
13151ea4c857SHans Petter Selasky 				linux_cdev_handle_remove(vmap);
13160c19d064SHans Petter Selasky 				/* free allocated VM area struct */
1317cca15f28SHans Petter Selasky 				linux_cdev_handle_free(vmap);
13180c19d064SHans Petter Selasky 			}
13191ea4c857SHans Petter Selasky 			return (EINVAL);
13201ea4c857SHans Petter Selasky 		}
13211ea4c857SHans Petter Selasky 	} else {
13228d59ecb2SHans Petter Selasky 		struct sglist *sg;
13238d59ecb2SHans Petter Selasky 
13248d59ecb2SHans Petter Selasky 		sg = sglist_alloc(1, M_WAITOK);
132561157228SHans Petter Selasky 		sglist_append_phys(sg,
132661157228SHans Petter Selasky 		    (vm_paddr_t)vmap->vm_pfn << PAGE_SHIFT, vmap->vm_len);
13271ea4c857SHans Petter Selasky 
13281ea4c857SHans Petter Selasky 		*object = vm_pager_allocate(OBJT_SG, sg, vmap->vm_len,
1329e23ae408SHans Petter Selasky 		    nprot, 0, td->td_ucred);
13301ea4c857SHans Petter Selasky 
1331cca15f28SHans Petter Selasky 		linux_cdev_handle_free(vmap);
13321ea4c857SHans Petter Selasky 
13338d59ecb2SHans Petter Selasky 		if (*object == NULL) {
13348d59ecb2SHans Petter Selasky 			sglist_free(sg);
13351ea4c857SHans Petter Selasky 			return (EINVAL);
13368d59ecb2SHans Petter Selasky 		}
13371ea4c857SHans Petter Selasky 	}
13381ea4c857SHans Petter Selasky 
13391ea4c857SHans Petter Selasky 	if (attr != VM_MEMATTR_DEFAULT) {
13408d59ecb2SHans Petter Selasky 		VM_OBJECT_WLOCK(*object);
13411ea4c857SHans Petter Selasky 		vm_object_set_memattr(*object, attr);
13428d59ecb2SHans Petter Selasky 		VM_OBJECT_WUNLOCK(*object);
13438d59ecb2SHans Petter Selasky 	}
13441ea4c857SHans Petter Selasky 	*offset = 0;
13451ea4c857SHans Petter Selasky 	return (0);
13468d59ecb2SHans Petter Selasky }
13478d59ecb2SHans Petter Selasky 
13488d59ecb2SHans Petter Selasky struct cdevsw linuxcdevsw = {
13498d59ecb2SHans Petter Selasky 	.d_version = D_VERSION,
1350e23ae408SHans Petter Selasky 	.d_fdopen = linux_dev_fdopen,
135190b30e65SHans Petter Selasky 	.d_name = "lkpidev",
13528d59ecb2SHans Petter Selasky };
13538d59ecb2SHans Petter Selasky 
13548d59ecb2SHans Petter Selasky static int
13558d59ecb2SHans Petter Selasky linux_file_read(struct file *file, struct uio *uio, struct ucred *active_cred,
13568d59ecb2SHans Petter Selasky     int flags, struct thread *td)
13578d59ecb2SHans Petter Selasky {
13588d59ecb2SHans Petter Selasky 	struct linux_file *filp;
1359f823a36eSKonstantin Belousov 	const struct file_operations *fop;
1360f823a36eSKonstantin Belousov 	struct linux_cdev *ldev;
13618d59ecb2SHans Petter Selasky 	ssize_t bytes;
13628d59ecb2SHans Petter Selasky 	int error;
13638d59ecb2SHans Petter Selasky 
13648d59ecb2SHans Petter Selasky 	error = 0;
13658d59ecb2SHans Petter Selasky 	filp = (struct linux_file *)file->f_data;
13668d59ecb2SHans Petter Selasky 	filp->f_flags = file->f_flag;
1367b3c89b5aSHans Petter Selasky 	/* XXX no support for I/O vectors currently */
13688d59ecb2SHans Petter Selasky 	if (uio->uio_iovcnt != 1)
1369b3c89b5aSHans Petter Selasky 		return (EOPNOTSUPP);
1370f71d0b0dSHans Petter Selasky 	if (uio->uio_resid > DEVFS_IOSIZE_MAX)
1371f71d0b0dSHans Petter Selasky 		return (EINVAL);
13721e3db1deSHans Petter Selasky 	linux_set_current(td);
1373f823a36eSKonstantin Belousov 	linux_get_fop(filp, &fop, &ldev);
1374f823a36eSKonstantin Belousov 	if (fop->read != NULL) {
1375f823a36eSKonstantin Belousov 		bytes = OPW(file, td, fop->read(filp,
1376f823a36eSKonstantin Belousov 		    uio->uio_iov->iov_base,
1377e23ae408SHans Petter Selasky 		    uio->uio_iov->iov_len, &uio->uio_offset));
13788d59ecb2SHans Petter Selasky 		if (bytes >= 0) {
13798d59ecb2SHans Petter Selasky 			uio->uio_iov->iov_base =
13808d59ecb2SHans Petter Selasky 			    ((uint8_t *)uio->uio_iov->iov_base) + bytes;
13818d59ecb2SHans Petter Selasky 			uio->uio_iov->iov_len -= bytes;
13828d59ecb2SHans Petter Selasky 			uio->uio_resid -= bytes;
1383e23ae408SHans Petter Selasky 		} else {
138494944062SHans Petter Selasky 			error = linux_get_error(current, -bytes);
1385e23ae408SHans Petter Selasky 		}
13868d59ecb2SHans Petter Selasky 	} else
13878d59ecb2SHans Petter Selasky 		error = ENXIO;
13888d59ecb2SHans Petter Selasky 
1389e23ae408SHans Petter Selasky 	/* update kqfilter status, if any */
1390e23ae408SHans Petter Selasky 	linux_file_kqfilter_poll(filp, LINUX_KQ_FLAG_HAS_READ);
1391f823a36eSKonstantin Belousov 	linux_drop_fop(ldev);
1392e23ae408SHans Petter Selasky 
1393e23ae408SHans Petter Selasky 	return (error);
1394e23ae408SHans Petter Selasky }
1395e23ae408SHans Petter Selasky 
1396e23ae408SHans Petter Selasky static int
1397e23ae408SHans Petter Selasky linux_file_write(struct file *file, struct uio *uio, struct ucred *active_cred,
1398e23ae408SHans Petter Selasky     int flags, struct thread *td)
1399e23ae408SHans Petter Selasky {
1400e23ae408SHans Petter Selasky 	struct linux_file *filp;
1401f823a36eSKonstantin Belousov 	const struct file_operations *fop;
1402f823a36eSKonstantin Belousov 	struct linux_cdev *ldev;
1403e23ae408SHans Petter Selasky 	ssize_t bytes;
1404e23ae408SHans Petter Selasky 	int error;
1405e23ae408SHans Petter Selasky 
1406e23ae408SHans Petter Selasky 	filp = (struct linux_file *)file->f_data;
1407e23ae408SHans Petter Selasky 	filp->f_flags = file->f_flag;
1408e23ae408SHans Petter Selasky 	/* XXX no support for I/O vectors currently */
1409e23ae408SHans Petter Selasky 	if (uio->uio_iovcnt != 1)
1410e23ae408SHans Petter Selasky 		return (EOPNOTSUPP);
1411f71d0b0dSHans Petter Selasky 	if (uio->uio_resid > DEVFS_IOSIZE_MAX)
1412f71d0b0dSHans Petter Selasky 		return (EINVAL);
1413e23ae408SHans Petter Selasky 	linux_set_current(td);
1414f823a36eSKonstantin Belousov 	linux_get_fop(filp, &fop, &ldev);
1415f823a36eSKonstantin Belousov 	if (fop->write != NULL) {
1416f823a36eSKonstantin Belousov 		bytes = OPW(file, td, fop->write(filp,
1417f823a36eSKonstantin Belousov 		    uio->uio_iov->iov_base,
1418e23ae408SHans Petter Selasky 		    uio->uio_iov->iov_len, &uio->uio_offset));
1419e23ae408SHans Petter Selasky 		if (bytes >= 0) {
1420e23ae408SHans Petter Selasky 			uio->uio_iov->iov_base =
1421e23ae408SHans Petter Selasky 			    ((uint8_t *)uio->uio_iov->iov_base) + bytes;
1422e23ae408SHans Petter Selasky 			uio->uio_iov->iov_len -= bytes;
1423e23ae408SHans Petter Selasky 			uio->uio_resid -= bytes;
1424f823a36eSKonstantin Belousov 			error = 0;
1425e23ae408SHans Petter Selasky 		} else {
142694944062SHans Petter Selasky 			error = linux_get_error(current, -bytes);
1427e23ae408SHans Petter Selasky 		}
1428e23ae408SHans Petter Selasky 	} else
1429e23ae408SHans Petter Selasky 		error = ENXIO;
1430e23ae408SHans Petter Selasky 
1431e23ae408SHans Petter Selasky 	/* update kqfilter status, if any */
1432e23ae408SHans Petter Selasky 	linux_file_kqfilter_poll(filp, LINUX_KQ_FLAG_HAS_WRITE);
1433e23ae408SHans Petter Selasky 
1434f823a36eSKonstantin Belousov 	linux_drop_fop(ldev);
1435f823a36eSKonstantin Belousov 
14368d59ecb2SHans Petter Selasky 	return (error);
14378d59ecb2SHans Petter Selasky }
14388d59ecb2SHans Petter Selasky 
14398d59ecb2SHans Petter Selasky static int
14408d59ecb2SHans Petter Selasky linux_file_poll(struct file *file, int events, struct ucred *active_cred,
14418d59ecb2SHans Petter Selasky     struct thread *td)
14428d59ecb2SHans Petter Selasky {
14438d59ecb2SHans Petter Selasky 	struct linux_file *filp;
1444f823a36eSKonstantin Belousov 	const struct file_operations *fop;
1445f823a36eSKonstantin Belousov 	struct linux_cdev *ldev;
14468d59ecb2SHans Petter Selasky 	int revents;
14478d59ecb2SHans Petter Selasky 
14488d59ecb2SHans Petter Selasky 	filp = (struct linux_file *)file->f_data;
14498d59ecb2SHans Petter Selasky 	filp->f_flags = file->f_flag;
14501e3db1deSHans Petter Selasky 	linux_set_current(td);
1451f823a36eSKonstantin Belousov 	linux_get_fop(filp, &fop, &ldev);
1452f823a36eSKonstantin Belousov 	if (fop->poll != NULL) {
1453f823a36eSKonstantin Belousov 		revents = OPW(file, td, fop->poll(filp,
1454f823a36eSKonstantin Belousov 		    LINUX_POLL_TABLE_NORMAL)) & events;
1455f823a36eSKonstantin Belousov 	} else {
14568d59ecb2SHans Petter Selasky 		revents = 0;
1457f823a36eSKonstantin Belousov 	}
1458f823a36eSKonstantin Belousov 	linux_drop_fop(ldev);
14590754e66cSHans Petter Selasky 	return (revents);
14608d59ecb2SHans Petter Selasky }
14618d59ecb2SHans Petter Selasky 
14628d59ecb2SHans Petter Selasky static int
14638d59ecb2SHans Petter Selasky linux_file_close(struct file *file, struct thread *td)
14648d59ecb2SHans Petter Selasky {
14658d59ecb2SHans Petter Selasky 	struct linux_file *filp;
14661328771dSHans Petter Selasky 	int (*release)(struct inode *, struct linux_file *);
1467f823a36eSKonstantin Belousov 	const struct file_operations *fop;
1468f823a36eSKonstantin Belousov 	struct linux_cdev *ldev;
14698d59ecb2SHans Petter Selasky 	int error;
14708d59ecb2SHans Petter Selasky 
14718d59ecb2SHans Petter Selasky 	filp = (struct linux_file *)file->f_data;
1472e23ae408SHans Petter Selasky 
1473f823a36eSKonstantin Belousov 	KASSERT(file_count(filp) == 0,
1474f823a36eSKonstantin Belousov 	    ("File refcount(%d) is not zero", file_count(filp)));
1475e23ae408SHans Petter Selasky 
1476a7e348d7SMark Johnston 	if (td == NULL)
1477a7e348d7SMark Johnston 		td = curthread;
1478a7e348d7SMark Johnston 
1479f823a36eSKonstantin Belousov 	error = 0;
14808d59ecb2SHans Petter Selasky 	filp->f_flags = file->f_flag;
14811e3db1deSHans Petter Selasky 	linux_set_current(td);
14826dec7efaSHans Petter Selasky 	linux_poll_wait_dequeue(filp);
1483f823a36eSKonstantin Belousov 	linux_get_fop(filp, &fop, &ldev);
14841328771dSHans Petter Selasky 	/*
14851328771dSHans Petter Selasky 	 * Always use the real release function, if any, to avoid
14861328771dSHans Petter Selasky 	 * leaking device resources:
14871328771dSHans Petter Selasky 	 */
14881328771dSHans Petter Selasky 	release = filp->f_op->release;
14891328771dSHans Petter Selasky 	if (release != NULL)
14901328771dSHans Petter Selasky 		error = -OPW(file, td, release(filp->f_vnode, filp));
14918d59ecb2SHans Petter Selasky 	funsetown(&filp->f_sigio);
1492e23ae408SHans Petter Selasky 	if (filp->f_vnode != NULL)
1493e23ae408SHans Petter Selasky 		vdrop(filp->f_vnode);
1494f823a36eSKonstantin Belousov 	linux_drop_fop(ldev);
14957b0125cbSKonstantin Belousov 	ldev = filp->f_cdev;
14967b0125cbSKonstantin Belousov 	if (ldev != NULL)
14977b0125cbSKonstantin Belousov 		linux_cdev_deref(ldev);
1498a81b36c6SVladimir Kondratyev 	linux_synchronize_rcu(RCU_TYPE_REGULAR);
14998d59ecb2SHans Petter Selasky 	kfree(filp);
15008d59ecb2SHans Petter Selasky 
15018d59ecb2SHans Petter Selasky 	return (error);
15028d59ecb2SHans Petter Selasky }
15038d59ecb2SHans Petter Selasky 
15048d59ecb2SHans Petter Selasky static int
15058d59ecb2SHans Petter Selasky linux_file_ioctl(struct file *fp, u_long cmd, void *data, struct ucred *cred,
15068d59ecb2SHans Petter Selasky     struct thread *td)
15078d59ecb2SHans Petter Selasky {
15088d59ecb2SHans Petter Selasky 	struct linux_file *filp;
1509f823a36eSKonstantin Belousov 	const struct file_operations *fop;
1510f823a36eSKonstantin Belousov 	struct linux_cdev *ldev;
15112a3529dfSKonstantin Belousov 	struct fiodgname_arg *fgn;
15122a3529dfSKonstantin Belousov 	const char *p;
15132a3529dfSKonstantin Belousov 	int error, i;
15148d59ecb2SHans Petter Selasky 
1515f823a36eSKonstantin Belousov 	error = 0;
15168d59ecb2SHans Petter Selasky 	filp = (struct linux_file *)fp->f_data;
15178d59ecb2SHans Petter Selasky 	filp->f_flags = fp->f_flag;
1518f823a36eSKonstantin Belousov 	linux_get_fop(filp, &fop, &ldev);
15198d59ecb2SHans Petter Selasky 
15201e3db1deSHans Petter Selasky 	linux_set_current(td);
15218d59ecb2SHans Petter Selasky 	switch (cmd) {
15228d59ecb2SHans Petter Selasky 	case FIONBIO:
15238d59ecb2SHans Petter Selasky 		break;
15248d59ecb2SHans Petter Selasky 	case FIOASYNC:
1525f823a36eSKonstantin Belousov 		if (fop->fasync == NULL)
15268d59ecb2SHans Petter Selasky 			break;
1527f823a36eSKonstantin Belousov 		error = -OPW(fp, td, fop->fasync(0, filp, fp->f_flag & FASYNC));
15288d59ecb2SHans Petter Selasky 		break;
15298d59ecb2SHans Petter Selasky 	case FIOSETOWN:
15308d59ecb2SHans Petter Selasky 		error = fsetown(*(int *)data, &filp->f_sigio);
1531e23ae408SHans Petter Selasky 		if (error == 0) {
1532f823a36eSKonstantin Belousov 			if (fop->fasync == NULL)
1533e23ae408SHans Petter Selasky 				break;
1534f823a36eSKonstantin Belousov 			error = -OPW(fp, td, fop->fasync(0, filp,
1535e23ae408SHans Petter Selasky 			    fp->f_flag & FASYNC));
1536e23ae408SHans Petter Selasky 		}
15378d59ecb2SHans Petter Selasky 		break;
15388d59ecb2SHans Petter Selasky 	case FIOGETOWN:
15398d59ecb2SHans Petter Selasky 		*(int *)data = fgetown(&filp->f_sigio);
15408d59ecb2SHans Petter Selasky 		break;
15412a3529dfSKonstantin Belousov 	case FIODGNAME:
15422a3529dfSKonstantin Belousov #ifdef	COMPAT_FREEBSD32
15432a3529dfSKonstantin Belousov 	case FIODGNAME_32:
15442a3529dfSKonstantin Belousov #endif
15452a3529dfSKonstantin Belousov 		if (filp->f_cdev == NULL || filp->f_cdev->cdev == NULL) {
15462a3529dfSKonstantin Belousov 			error = ENXIO;
15472a3529dfSKonstantin Belousov 			break;
15482a3529dfSKonstantin Belousov 		}
15492a3529dfSKonstantin Belousov 		fgn = data;
15502a3529dfSKonstantin Belousov 		p = devtoname(filp->f_cdev->cdev);
15512a3529dfSKonstantin Belousov 		i = strlen(p) + 1;
15522a3529dfSKonstantin Belousov 		if (i > fgn->len) {
15532a3529dfSKonstantin Belousov 			error = EINVAL;
15542a3529dfSKonstantin Belousov 			break;
15552a3529dfSKonstantin Belousov 		}
15562a3529dfSKonstantin Belousov 		error = copyout(p, fiodgname_buf_get_ptr(fgn, cmd), i);
15572a3529dfSKonstantin Belousov 		break;
15588d59ecb2SHans Petter Selasky 	default:
1559f823a36eSKonstantin Belousov 		error = linux_file_ioctl_sub(fp, filp, fop, cmd, data, td);
15608d59ecb2SHans Petter Selasky 		break;
15618d59ecb2SHans Petter Selasky 	}
1562f823a36eSKonstantin Belousov 	linux_drop_fop(ldev);
15638d59ecb2SHans Petter Selasky 	return (error);
15648d59ecb2SHans Petter Selasky }
15658d59ecb2SHans Petter Selasky 
15668d59ecb2SHans Petter Selasky static int
1567e23ae408SHans Petter Selasky linux_file_mmap_sub(struct thread *td, vm_size_t objsize, vm_prot_t prot,
1568904390b4SHans Petter Selasky     vm_prot_t maxprot, int flags, struct file *fp,
1569f823a36eSKonstantin Belousov     vm_ooffset_t *foff, const struct file_operations *fop, vm_object_t *objp)
1570e23ae408SHans Petter Selasky {
1571e23ae408SHans Petter Selasky 	/*
1572e23ae408SHans Petter Selasky 	 * Character devices do not provide private mappings
1573e23ae408SHans Petter Selasky 	 * of any kind:
1574e23ae408SHans Petter Selasky 	 */
1575904390b4SHans Petter Selasky 	if ((maxprot & VM_PROT_WRITE) == 0 &&
1576e23ae408SHans Petter Selasky 	    (prot & VM_PROT_WRITE) != 0)
1577e23ae408SHans Petter Selasky 		return (EACCES);
1578904390b4SHans Petter Selasky 	if ((flags & (MAP_PRIVATE | MAP_COPY)) != 0)
1579e23ae408SHans Petter Selasky 		return (EINVAL);
1580e23ae408SHans Petter Selasky 
1581f823a36eSKonstantin Belousov 	return (linux_file_mmap_single(fp, fop, foff, objsize, objp,
1582904390b4SHans Petter Selasky 	    (int)prot, (flags & MAP_SHARED) ? true : false, td));
1583e23ae408SHans Petter Selasky }
1584e23ae408SHans Petter Selasky 
1585e23ae408SHans Petter Selasky static int
1586e23ae408SHans Petter Selasky linux_file_mmap(struct file *fp, vm_map_t map, vm_offset_t *addr, vm_size_t size,
1587e23ae408SHans Petter Selasky     vm_prot_t prot, vm_prot_t cap_maxprot, int flags, vm_ooffset_t foff,
1588e23ae408SHans Petter Selasky     struct thread *td)
1589e23ae408SHans Petter Selasky {
1590e23ae408SHans Petter Selasky 	struct linux_file *filp;
1591f823a36eSKonstantin Belousov 	const struct file_operations *fop;
1592f823a36eSKonstantin Belousov 	struct linux_cdev *ldev;
1593e23ae408SHans Petter Selasky 	struct mount *mp;
1594e23ae408SHans Petter Selasky 	struct vnode *vp;
1595e23ae408SHans Petter Selasky 	vm_object_t object;
1596e23ae408SHans Petter Selasky 	vm_prot_t maxprot;
1597e23ae408SHans Petter Selasky 	int error;
1598e23ae408SHans Petter Selasky 
1599e23ae408SHans Petter Selasky 	filp = (struct linux_file *)fp->f_data;
1600e23ae408SHans Petter Selasky 
1601e23ae408SHans Petter Selasky 	vp = filp->f_vnode;
1602e23ae408SHans Petter Selasky 	if (vp == NULL)
1603e23ae408SHans Petter Selasky 		return (EOPNOTSUPP);
1604e23ae408SHans Petter Selasky 
1605e23ae408SHans Petter Selasky 	/*
1606e23ae408SHans Petter Selasky 	 * Ensure that file and memory protections are
1607e23ae408SHans Petter Selasky 	 * compatible.
1608e23ae408SHans Petter Selasky 	 */
1609e23ae408SHans Petter Selasky 	mp = vp->v_mount;
1610e23ae408SHans Petter Selasky 	if (mp != NULL && (mp->mnt_flag & MNT_NOEXEC) != 0) {
1611e23ae408SHans Petter Selasky 		maxprot = VM_PROT_NONE;
1612e23ae408SHans Petter Selasky 		if ((prot & VM_PROT_EXECUTE) != 0)
1613e23ae408SHans Petter Selasky 			return (EACCES);
1614e23ae408SHans Petter Selasky 	} else
1615e23ae408SHans Petter Selasky 		maxprot = VM_PROT_EXECUTE;
1616e23ae408SHans Petter Selasky 	if ((fp->f_flag & FREAD) != 0)
1617e23ae408SHans Petter Selasky 		maxprot |= VM_PROT_READ;
1618e23ae408SHans Petter Selasky 	else if ((prot & VM_PROT_READ) != 0)
1619e23ae408SHans Petter Selasky 		return (EACCES);
1620e23ae408SHans Petter Selasky 
1621e23ae408SHans Petter Selasky 	/*
1622e23ae408SHans Petter Selasky 	 * If we are sharing potential changes via MAP_SHARED and we
1623e23ae408SHans Petter Selasky 	 * are trying to get write permission although we opened it
1624e23ae408SHans Petter Selasky 	 * without asking for it, bail out.
1625e23ae408SHans Petter Selasky 	 *
1626e23ae408SHans Petter Selasky 	 * Note that most character devices always share mappings.
1627e23ae408SHans Petter Selasky 	 *
1628e23ae408SHans Petter Selasky 	 * Rely on linux_file_mmap_sub() to fail invalid MAP_PRIVATE
1629e23ae408SHans Petter Selasky 	 * requests rather than doing it here.
1630e23ae408SHans Petter Selasky 	 */
1631e23ae408SHans Petter Selasky 	if ((flags & MAP_SHARED) != 0) {
1632e23ae408SHans Petter Selasky 		if ((fp->f_flag & FWRITE) != 0)
1633e23ae408SHans Petter Selasky 			maxprot |= VM_PROT_WRITE;
1634e23ae408SHans Petter Selasky 		else if ((prot & VM_PROT_WRITE) != 0)
1635e23ae408SHans Petter Selasky 			return (EACCES);
1636e23ae408SHans Petter Selasky 	}
1637e23ae408SHans Petter Selasky 	maxprot &= cap_maxprot;
1638e23ae408SHans Petter Selasky 
1639f823a36eSKonstantin Belousov 	linux_get_fop(filp, &fop, &ldev);
1640904390b4SHans Petter Selasky 	error = linux_file_mmap_sub(td, size, prot, maxprot, flags, fp,
1641f823a36eSKonstantin Belousov 	    &foff, fop, &object);
1642e23ae408SHans Petter Selasky 	if (error != 0)
1643f823a36eSKonstantin Belousov 		goto out;
1644e23ae408SHans Petter Selasky 
1645e23ae408SHans Petter Selasky 	error = vm_mmap_object(map, addr, size, prot, maxprot, flags, object,
1646e23ae408SHans Petter Selasky 	    foff, FALSE, td);
1647e23ae408SHans Petter Selasky 	if (error != 0)
1648e23ae408SHans Petter Selasky 		vm_object_deallocate(object);
1649f823a36eSKonstantin Belousov out:
1650f823a36eSKonstantin Belousov 	linux_drop_fop(ldev);
1651e23ae408SHans Petter Selasky 	return (error);
1652e23ae408SHans Petter Selasky }
1653e23ae408SHans Petter Selasky 
1654e23ae408SHans Petter Selasky static int
16552b68eb8eSMateusz Guzik linux_file_stat(struct file *fp, struct stat *sb, struct ucred *active_cred)
16568d59ecb2SHans Petter Selasky {
1657e23ae408SHans Petter Selasky 	struct linux_file *filp;
1658e23ae408SHans Petter Selasky 	struct vnode *vp;
1659e23ae408SHans Petter Selasky 	int error;
16608d59ecb2SHans Petter Selasky 
1661e23ae408SHans Petter Selasky 	filp = (struct linux_file *)fp->f_data;
1662e23ae408SHans Petter Selasky 	if (filp->f_vnode == NULL)
16638d59ecb2SHans Petter Selasky 		return (EOPNOTSUPP);
1664e23ae408SHans Petter Selasky 
1665e23ae408SHans Petter Selasky 	vp = filp->f_vnode;
1666e23ae408SHans Petter Selasky 
1667e23ae408SHans Petter Selasky 	vn_lock(vp, LK_SHARED | LK_RETRY);
16682b68eb8eSMateusz Guzik 	error = VOP_STAT(vp, sb, curthread->td_ucred, NOCRED);
1669b249ce48SMateusz Guzik 	VOP_UNLOCK(vp);
1670e23ae408SHans Petter Selasky 
1671e23ae408SHans Petter Selasky 	return (error);
16728d59ecb2SHans Petter Selasky }
16738d59ecb2SHans Petter Selasky 
16748d59ecb2SHans Petter Selasky static int
16758d59ecb2SHans Petter Selasky linux_file_fill_kinfo(struct file *fp, struct kinfo_file *kif,
16768d59ecb2SHans Petter Selasky     struct filedesc *fdp)
16778d59ecb2SHans Petter Selasky {
1678f1863400SKonstantin Belousov 	struct linux_file *filp;
1679f1863400SKonstantin Belousov 	struct vnode *vp;
1680f1863400SKonstantin Belousov 	int error;
16818d59ecb2SHans Petter Selasky 
1682f1863400SKonstantin Belousov 	filp = fp->f_data;
1683f1863400SKonstantin Belousov 	vp = filp->f_vnode;
1684f1863400SKonstantin Belousov 	if (vp == NULL) {
1685f1863400SKonstantin Belousov 		error = 0;
1686f1863400SKonstantin Belousov 		kif->kf_type = KF_TYPE_DEV;
1687f1863400SKonstantin Belousov 	} else {
1688f1863400SKonstantin Belousov 		vref(vp);
1689f1863400SKonstantin Belousov 		FILEDESC_SUNLOCK(fdp);
1690f1863400SKonstantin Belousov 		error = vn_fill_kinfo_vnode(vp, kif);
1691f1863400SKonstantin Belousov 		vrele(vp);
1692f1863400SKonstantin Belousov 		kif->kf_type = KF_TYPE_VNODE;
1693f1863400SKonstantin Belousov 		FILEDESC_SLOCK(fdp);
1694f1863400SKonstantin Belousov 	}
1695f1863400SKonstantin Belousov 	return (error);
16968d59ecb2SHans Petter Selasky }
16978d59ecb2SHans Petter Selasky 
16982b79a966SHans Petter Selasky unsigned int
16992b79a966SHans Petter Selasky linux_iminor(struct inode *inode)
17002b79a966SHans Petter Selasky {
17012b79a966SHans Petter Selasky 	struct linux_cdev *ldev;
17022b79a966SHans Petter Selasky 
17032b79a966SHans Petter Selasky 	if (inode == NULL || inode->v_rdev == NULL ||
17042b79a966SHans Petter Selasky 	    inode->v_rdev->si_devsw != &linuxcdevsw)
17052b79a966SHans Petter Selasky 		return (-1U);
17062b79a966SHans Petter Selasky 	ldev = inode->v_rdev->si_drv1;
17072b79a966SHans Petter Selasky 	if (ldev == NULL)
17082b79a966SHans Petter Selasky 		return (-1U);
17092b79a966SHans Petter Selasky 
17102b79a966SHans Petter Selasky 	return (minor(ldev->dev));
17112b79a966SHans Petter Selasky }
17122b79a966SHans Petter Selasky 
17138d59ecb2SHans Petter Selasky struct fileops linuxfileops = {
17148d59ecb2SHans Petter Selasky 	.fo_read = linux_file_read,
1715e23ae408SHans Petter Selasky 	.fo_write = linux_file_write,
17168d59ecb2SHans Petter Selasky 	.fo_truncate = invfo_truncate,
1717e23ae408SHans Petter Selasky 	.fo_kqfilter = linux_file_kqfilter,
17188d59ecb2SHans Petter Selasky 	.fo_stat = linux_file_stat,
17198d59ecb2SHans Petter Selasky 	.fo_fill_kinfo = linux_file_fill_kinfo,
17208d59ecb2SHans Petter Selasky 	.fo_poll = linux_file_poll,
17218d59ecb2SHans Petter Selasky 	.fo_close = linux_file_close,
17228d59ecb2SHans Petter Selasky 	.fo_ioctl = linux_file_ioctl,
1723e23ae408SHans Petter Selasky 	.fo_mmap = linux_file_mmap,
17248d59ecb2SHans Petter Selasky 	.fo_chmod = invfo_chmod,
17258d59ecb2SHans Petter Selasky 	.fo_chown = invfo_chown,
17268d59ecb2SHans Petter Selasky 	.fo_sendfile = invfo_sendfile,
1727f71d0b0dSHans Petter Selasky 	.fo_flags = DFLAG_PASSABLE,
17288d59ecb2SHans Petter Selasky };
17298d59ecb2SHans Petter Selasky 
17308d59ecb2SHans Petter Selasky /*
17318d59ecb2SHans Petter Selasky  * Hash of vmmap addresses.  This is infrequently accessed and does not
17328d59ecb2SHans Petter Selasky  * need to be particularly large.  This is done because we must store the
17338d59ecb2SHans Petter Selasky  * caller's idea of the map size to properly unmap.
17348d59ecb2SHans Petter Selasky  */
17358d59ecb2SHans Petter Selasky struct vmmap {
17368d59ecb2SHans Petter Selasky 	LIST_ENTRY(vmmap)	vm_next;
17378d59ecb2SHans Petter Selasky 	void 			*vm_addr;
17388d59ecb2SHans Petter Selasky 	unsigned long		vm_size;
17398d59ecb2SHans Petter Selasky };
17408d59ecb2SHans Petter Selasky 
17418d59ecb2SHans Petter Selasky struct vmmaphd {
17428d59ecb2SHans Petter Selasky 	struct vmmap *lh_first;
17438d59ecb2SHans Petter Selasky };
17448d59ecb2SHans Petter Selasky #define	VMMAP_HASH_SIZE	64
17458d59ecb2SHans Petter Selasky #define	VMMAP_HASH_MASK	(VMMAP_HASH_SIZE - 1)
17468d59ecb2SHans Petter Selasky #define	VM_HASH(addr)	((uintptr_t)(addr) >> PAGE_SHIFT) & VMMAP_HASH_MASK
17478d59ecb2SHans Petter Selasky static struct vmmaphd vmmaphead[VMMAP_HASH_SIZE];
17488d59ecb2SHans Petter Selasky static struct mtx vmmaplock;
17498d59ecb2SHans Petter Selasky 
17508d59ecb2SHans Petter Selasky static void
17518d59ecb2SHans Petter Selasky vmmap_add(void *addr, unsigned long size)
17528d59ecb2SHans Petter Selasky {
17538d59ecb2SHans Petter Selasky 	struct vmmap *vmmap;
17548d59ecb2SHans Petter Selasky 
17558a8e86b8SJean-Sébastien Pédron 	vmmap = kmalloc(sizeof(*vmmap), GFP_KERNEL);
17568d59ecb2SHans Petter Selasky 	mtx_lock(&vmmaplock);
17578d59ecb2SHans Petter Selasky 	vmmap->vm_size = size;
17588d59ecb2SHans Petter Selasky 	vmmap->vm_addr = addr;
17598d59ecb2SHans Petter Selasky 	LIST_INSERT_HEAD(&vmmaphead[VM_HASH(addr)], vmmap, vm_next);
17608d59ecb2SHans Petter Selasky 	mtx_unlock(&vmmaplock);
17618d59ecb2SHans Petter Selasky }
17628d59ecb2SHans Petter Selasky 
17638d59ecb2SHans Petter Selasky static struct vmmap *
17648d59ecb2SHans Petter Selasky vmmap_remove(void *addr)
17658d59ecb2SHans Petter Selasky {
17668d59ecb2SHans Petter Selasky 	struct vmmap *vmmap;
17678d59ecb2SHans Petter Selasky 
17688d59ecb2SHans Petter Selasky 	mtx_lock(&vmmaplock);
17698d59ecb2SHans Petter Selasky 	LIST_FOREACH(vmmap, &vmmaphead[VM_HASH(addr)], vm_next)
17708d59ecb2SHans Petter Selasky 		if (vmmap->vm_addr == addr)
17718d59ecb2SHans Petter Selasky 			break;
17728d59ecb2SHans Petter Selasky 	if (vmmap)
17738d59ecb2SHans Petter Selasky 		LIST_REMOVE(vmmap, vm_next);
17748d59ecb2SHans Petter Selasky 	mtx_unlock(&vmmaplock);
17758d59ecb2SHans Petter Selasky 
17768d59ecb2SHans Petter Selasky 	return (vmmap);
17778d59ecb2SHans Petter Selasky }
17788d59ecb2SHans Petter Selasky 
177960d962e0SJessica Clarke #if defined(__i386__) || defined(__amd64__) || defined(__powerpc__) || defined(__aarch64__) || defined(__riscv)
17808d59ecb2SHans Petter Selasky void *
17818d59ecb2SHans Petter Selasky _ioremap_attr(vm_paddr_t phys_addr, unsigned long size, int attr)
17828d59ecb2SHans Petter Selasky {
17838d59ecb2SHans Petter Selasky 	void *addr;
17848d59ecb2SHans Petter Selasky 
17858d59ecb2SHans Petter Selasky 	addr = pmap_mapdev_attr(phys_addr, size, attr);
17868d59ecb2SHans Petter Selasky 	if (addr == NULL)
17878d59ecb2SHans Petter Selasky 		return (NULL);
17888d59ecb2SHans Petter Selasky 	vmmap_add(addr, size);
17898d59ecb2SHans Petter Selasky 
17908d59ecb2SHans Petter Selasky 	return (addr);
17918d59ecb2SHans Petter Selasky }
179286845417SHans Petter Selasky #endif
17938d59ecb2SHans Petter Selasky 
17948d59ecb2SHans Petter Selasky void
17958d59ecb2SHans Petter Selasky iounmap(void *addr)
17968d59ecb2SHans Petter Selasky {
17978d59ecb2SHans Petter Selasky 	struct vmmap *vmmap;
17988d59ecb2SHans Petter Selasky 
17998d59ecb2SHans Petter Selasky 	vmmap = vmmap_remove(addr);
18008d59ecb2SHans Petter Selasky 	if (vmmap == NULL)
18018d59ecb2SHans Petter Selasky 		return;
180260d962e0SJessica Clarke #if defined(__i386__) || defined(__amd64__) || defined(__powerpc__) || defined(__aarch64__) || defined(__riscv)
18037ae99f80SJohn Baldwin 	pmap_unmapdev(addr, vmmap->vm_size);
18048d59ecb2SHans Petter Selasky #endif
18058d59ecb2SHans Petter Selasky 	kfree(vmmap);
18068d59ecb2SHans Petter Selasky }
18078d59ecb2SHans Petter Selasky 
18088d59ecb2SHans Petter Selasky void *
18098d59ecb2SHans Petter Selasky vmap(struct page **pages, unsigned int count, unsigned long flags, int prot)
18108d59ecb2SHans Petter Selasky {
18118d59ecb2SHans Petter Selasky 	vm_offset_t off;
18128d59ecb2SHans Petter Selasky 	size_t size;
18138d59ecb2SHans Petter Selasky 
18148d59ecb2SHans Petter Selasky 	size = count * PAGE_SIZE;
18158d59ecb2SHans Petter Selasky 	off = kva_alloc(size);
18168d59ecb2SHans Petter Selasky 	if (off == 0)
18178d59ecb2SHans Petter Selasky 		return (NULL);
18188d59ecb2SHans Petter Selasky 	vmmap_add((void *)off, size);
18198d59ecb2SHans Petter Selasky 	pmap_qenter(off, pages, count);
18208d59ecb2SHans Petter Selasky 
18218d59ecb2SHans Petter Selasky 	return ((void *)off);
18228d59ecb2SHans Petter Selasky }
18238d59ecb2SHans Petter Selasky 
18248d59ecb2SHans Petter Selasky void
18258d59ecb2SHans Petter Selasky vunmap(void *addr)
18268d59ecb2SHans Petter Selasky {
18278d59ecb2SHans Petter Selasky 	struct vmmap *vmmap;
18288d59ecb2SHans Petter Selasky 
18298d59ecb2SHans Petter Selasky 	vmmap = vmmap_remove(addr);
18308d59ecb2SHans Petter Selasky 	if (vmmap == NULL)
18318d59ecb2SHans Petter Selasky 		return;
18328d59ecb2SHans Petter Selasky 	pmap_qremove((vm_offset_t)addr, vmmap->vm_size / PAGE_SIZE);
18338d59ecb2SHans Petter Selasky 	kva_free((vm_offset_t)addr, vmmap->vm_size);
18348d59ecb2SHans Petter Selasky 	kfree(vmmap);
18358d59ecb2SHans Petter Selasky }
18368d59ecb2SHans Petter Selasky 
1837fa765ca7SBjoern A. Zeeb static char *
1838fa765ca7SBjoern A. Zeeb devm_kvasprintf(struct device *dev, gfp_t gfp, const char *fmt, va_list ap)
18398d59ecb2SHans Petter Selasky {
18408d59ecb2SHans Petter Selasky 	unsigned int len;
18418d59ecb2SHans Petter Selasky 	char *p;
18428d59ecb2SHans Petter Selasky 	va_list aq;
18438d59ecb2SHans Petter Selasky 
18448d59ecb2SHans Petter Selasky 	va_copy(aq, ap);
18458d59ecb2SHans Petter Selasky 	len = vsnprintf(NULL, 0, fmt, aq);
18468d59ecb2SHans Petter Selasky 	va_end(aq);
18478d59ecb2SHans Petter Selasky 
1848fa765ca7SBjoern A. Zeeb 	if (dev != NULL)
1849fa765ca7SBjoern A. Zeeb 		p = devm_kmalloc(dev, len + 1, gfp);
1850fa765ca7SBjoern A. Zeeb 	else
18518d59ecb2SHans Petter Selasky 		p = kmalloc(len + 1, gfp);
18528d59ecb2SHans Petter Selasky 	if (p != NULL)
18538d59ecb2SHans Petter Selasky 		vsnprintf(p, len + 1, fmt, ap);
18548d59ecb2SHans Petter Selasky 
18558d59ecb2SHans Petter Selasky 	return (p);
18568d59ecb2SHans Petter Selasky }
18578d59ecb2SHans Petter Selasky 
18588d59ecb2SHans Petter Selasky char *
1859fa765ca7SBjoern A. Zeeb kvasprintf(gfp_t gfp, const char *fmt, va_list ap)
1860fa765ca7SBjoern A. Zeeb {
1861fa765ca7SBjoern A. Zeeb 
1862fa765ca7SBjoern A. Zeeb 	return (devm_kvasprintf(NULL, gfp, fmt, ap));
1863fa765ca7SBjoern A. Zeeb }
1864fa765ca7SBjoern A. Zeeb 
1865fa765ca7SBjoern A. Zeeb char *
1866fa765ca7SBjoern A. Zeeb lkpi_devm_kasprintf(struct device *dev, gfp_t gfp, const char *fmt, ...)
1867fa765ca7SBjoern A. Zeeb {
1868fa765ca7SBjoern A. Zeeb 	va_list ap;
1869fa765ca7SBjoern A. Zeeb 	char *p;
1870fa765ca7SBjoern A. Zeeb 
1871fa765ca7SBjoern A. Zeeb 	va_start(ap, fmt);
1872fa765ca7SBjoern A. Zeeb 	p = devm_kvasprintf(dev, gfp, fmt, ap);
1873fa765ca7SBjoern A. Zeeb 	va_end(ap);
1874fa765ca7SBjoern A. Zeeb 
1875fa765ca7SBjoern A. Zeeb 	return (p);
1876fa765ca7SBjoern A. Zeeb }
1877fa765ca7SBjoern A. Zeeb 
1878fa765ca7SBjoern A. Zeeb char *
18798d59ecb2SHans Petter Selasky kasprintf(gfp_t gfp, const char *fmt, ...)
18808d59ecb2SHans Petter Selasky {
18818d59ecb2SHans Petter Selasky 	va_list ap;
18828d59ecb2SHans Petter Selasky 	char *p;
18838d59ecb2SHans Petter Selasky 
18848d59ecb2SHans Petter Selasky 	va_start(ap, fmt);
18858d59ecb2SHans Petter Selasky 	p = kvasprintf(gfp, fmt, ap);
18868d59ecb2SHans Petter Selasky 	va_end(ap);
18878d59ecb2SHans Petter Selasky 
18888d59ecb2SHans Petter Selasky 	return (p);
18898d59ecb2SHans Petter Selasky }
18908d59ecb2SHans Petter Selasky 
18918d59ecb2SHans Petter Selasky static void
18928d59ecb2SHans Petter Selasky linux_timer_callback_wrapper(void *context)
18938d59ecb2SHans Petter Selasky {
18948d59ecb2SHans Petter Selasky 	struct timer_list *timer;
18958d59ecb2SHans Petter Selasky 
18968d59ecb2SHans Petter Selasky 	timer = context;
1897dfb33cb0SHans Petter Selasky 
1898dfb33cb0SHans Petter Selasky 	if (linux_set_current_flags(curthread, M_NOWAIT)) {
1899dfb33cb0SHans Petter Selasky 		/* try again later */
1900dfb33cb0SHans Petter Selasky 		callout_reset(&timer->callout, 1,
1901dfb33cb0SHans Petter Selasky 		    &linux_timer_callback_wrapper, timer);
1902dfb33cb0SHans Petter Selasky 		return;
1903dfb33cb0SHans Petter Selasky 	}
1904dfb33cb0SHans Petter Selasky 
19058d59ecb2SHans Petter Selasky 	timer->function(timer->data);
19068d59ecb2SHans Petter Selasky }
19078d59ecb2SHans Petter Selasky 
190842f0f394SEmmanuel Vadot int
1909f6800be3SHans Petter Selasky mod_timer(struct timer_list *timer, int expires)
19108d59ecb2SHans Petter Selasky {
191142f0f394SEmmanuel Vadot 	int ret;
19128d59ecb2SHans Petter Selasky 
19138d59ecb2SHans Petter Selasky 	timer->expires = expires;
191442f0f394SEmmanuel Vadot 	ret = callout_reset(&timer->callout,
19158d59ecb2SHans Petter Selasky 	    linux_timer_jiffies_until(expires),
19168d59ecb2SHans Petter Selasky 	    &linux_timer_callback_wrapper, timer);
191742f0f394SEmmanuel Vadot 
191842f0f394SEmmanuel Vadot 	MPASS(ret == 0 || ret == 1);
191942f0f394SEmmanuel Vadot 
192042f0f394SEmmanuel Vadot 	return (ret == 1);
19218d59ecb2SHans Petter Selasky }
19228d59ecb2SHans Petter Selasky 
19238d59ecb2SHans Petter Selasky void
19248d59ecb2SHans Petter Selasky add_timer(struct timer_list *timer)
19258d59ecb2SHans Petter Selasky {
19268d59ecb2SHans Petter Selasky 
19279555cfd2SHans Petter Selasky 	callout_reset(&timer->callout,
19288d59ecb2SHans Petter Selasky 	    linux_timer_jiffies_until(timer->expires),
19298d59ecb2SHans Petter Selasky 	    &linux_timer_callback_wrapper, timer);
19308d59ecb2SHans Petter Selasky }
19318d59ecb2SHans Petter Selasky 
1932def277d3SHans Petter Selasky void
1933def277d3SHans Petter Selasky add_timer_on(struct timer_list *timer, int cpu)
1934def277d3SHans Petter Selasky {
1935def277d3SHans Petter Selasky 
19369555cfd2SHans Petter Selasky 	callout_reset_on(&timer->callout,
1937def277d3SHans Petter Selasky 	    linux_timer_jiffies_until(timer->expires),
1938def277d3SHans Petter Selasky 	    &linux_timer_callback_wrapper, timer, cpu);
1939def277d3SHans Petter Selasky }
1940def277d3SHans Petter Selasky 
194102927c76SJohannes Lundberg int
194202927c76SJohannes Lundberg del_timer(struct timer_list *timer)
194302927c76SJohannes Lundberg {
194402927c76SJohannes Lundberg 
194502927c76SJohannes Lundberg 	if (callout_stop(&(timer)->callout) == -1)
194602927c76SJohannes Lundberg 		return (0);
194702927c76SJohannes Lundberg 	return (1);
194802927c76SJohannes Lundberg }
194902927c76SJohannes Lundberg 
195042f0f394SEmmanuel Vadot int
195142f0f394SEmmanuel Vadot del_timer_sync(struct timer_list *timer)
195242f0f394SEmmanuel Vadot {
195342f0f394SEmmanuel Vadot 
195442f0f394SEmmanuel Vadot 	if (callout_drain(&(timer)->callout) == -1)
195542f0f394SEmmanuel Vadot 		return (0);
195642f0f394SEmmanuel Vadot 	return (1);
195742f0f394SEmmanuel Vadot }
195842f0f394SEmmanuel Vadot 
19592e07e885SBjoern A. Zeeb int
19602e07e885SBjoern A. Zeeb timer_delete_sync(struct timer_list *timer)
19612e07e885SBjoern A. Zeeb {
19622e07e885SBjoern A. Zeeb 
19632e07e885SBjoern A. Zeeb 	return (del_timer_sync(timer));
19642e07e885SBjoern A. Zeeb }
19652e07e885SBjoern A. Zeeb 
19662e07e885SBjoern A. Zeeb int
19672e07e885SBjoern A. Zeeb timer_shutdown_sync(struct timer_list *timer)
19682e07e885SBjoern A. Zeeb {
19692e07e885SBjoern A. Zeeb 
19702e07e885SBjoern A. Zeeb 	return (del_timer_sync(timer));
19712e07e885SBjoern A. Zeeb }
19722e07e885SBjoern A. Zeeb 
197377632fc7SHans Petter Selasky /* greatest common divisor, Euclid equation */
197477632fc7SHans Petter Selasky static uint64_t
197577632fc7SHans Petter Selasky lkpi_gcd_64(uint64_t a, uint64_t b)
197677632fc7SHans Petter Selasky {
197777632fc7SHans Petter Selasky 	uint64_t an;
197877632fc7SHans Petter Selasky 	uint64_t bn;
197977632fc7SHans Petter Selasky 
198077632fc7SHans Petter Selasky 	while (b != 0) {
198177632fc7SHans Petter Selasky 		an = b;
198277632fc7SHans Petter Selasky 		bn = a % b;
198377632fc7SHans Petter Selasky 		a = an;
198477632fc7SHans Petter Selasky 		b = bn;
198577632fc7SHans Petter Selasky 	}
198677632fc7SHans Petter Selasky 	return (a);
198777632fc7SHans Petter Selasky }
198877632fc7SHans Petter Selasky 
198977632fc7SHans Petter Selasky uint64_t lkpi_nsec2hz_rem;
199077632fc7SHans Petter Selasky uint64_t lkpi_nsec2hz_div = 1000000000ULL;
199177632fc7SHans Petter Selasky uint64_t lkpi_nsec2hz_max;
199277632fc7SHans Petter Selasky 
199377632fc7SHans Petter Selasky uint64_t lkpi_usec2hz_rem;
199477632fc7SHans Petter Selasky uint64_t lkpi_usec2hz_div = 1000000ULL;
199577632fc7SHans Petter Selasky uint64_t lkpi_usec2hz_max;
199677632fc7SHans Petter Selasky 
199777632fc7SHans Petter Selasky uint64_t lkpi_msec2hz_rem;
199877632fc7SHans Petter Selasky uint64_t lkpi_msec2hz_div = 1000ULL;
199977632fc7SHans Petter Selasky uint64_t lkpi_msec2hz_max;
200077632fc7SHans Petter Selasky 
20018d59ecb2SHans Petter Selasky static void
20028d59ecb2SHans Petter Selasky linux_timer_init(void *arg)
20038d59ecb2SHans Petter Selasky {
200477632fc7SHans Petter Selasky 	uint64_t gcd;
20058d59ecb2SHans Petter Selasky 
20068d59ecb2SHans Petter Selasky 	/*
20078d59ecb2SHans Petter Selasky 	 * Compute an internal HZ value which can divide 2**32 to
20088d59ecb2SHans Petter Selasky 	 * avoid timer rounding problems when the tick value wraps
20098d59ecb2SHans Petter Selasky 	 * around 2**32:
20108d59ecb2SHans Petter Selasky 	 */
20118d59ecb2SHans Petter Selasky 	linux_timer_hz_mask = 1;
20128d59ecb2SHans Petter Selasky 	while (linux_timer_hz_mask < (unsigned long)hz)
20138d59ecb2SHans Petter Selasky 		linux_timer_hz_mask *= 2;
20148d59ecb2SHans Petter Selasky 	linux_timer_hz_mask--;
201577632fc7SHans Petter Selasky 
201677632fc7SHans Petter Selasky 	/* compute some internal constants */
201777632fc7SHans Petter Selasky 
201877632fc7SHans Petter Selasky 	lkpi_nsec2hz_rem = hz;
201977632fc7SHans Petter Selasky 	lkpi_usec2hz_rem = hz;
202077632fc7SHans Petter Selasky 	lkpi_msec2hz_rem = hz;
202177632fc7SHans Petter Selasky 
202277632fc7SHans Petter Selasky 	gcd = lkpi_gcd_64(lkpi_nsec2hz_rem, lkpi_nsec2hz_div);
202377632fc7SHans Petter Selasky 	lkpi_nsec2hz_rem /= gcd;
202477632fc7SHans Petter Selasky 	lkpi_nsec2hz_div /= gcd;
202577632fc7SHans Petter Selasky 	lkpi_nsec2hz_max = -1ULL / lkpi_nsec2hz_rem;
202677632fc7SHans Petter Selasky 
202777632fc7SHans Petter Selasky 	gcd = lkpi_gcd_64(lkpi_usec2hz_rem, lkpi_usec2hz_div);
202877632fc7SHans Petter Selasky 	lkpi_usec2hz_rem /= gcd;
202977632fc7SHans Petter Selasky 	lkpi_usec2hz_div /= gcd;
203077632fc7SHans Petter Selasky 	lkpi_usec2hz_max = -1ULL / lkpi_usec2hz_rem;
203177632fc7SHans Petter Selasky 
203277632fc7SHans Petter Selasky 	gcd = lkpi_gcd_64(lkpi_msec2hz_rem, lkpi_msec2hz_div);
203377632fc7SHans Petter Selasky 	lkpi_msec2hz_rem /= gcd;
203477632fc7SHans Petter Selasky 	lkpi_msec2hz_div /= gcd;
203577632fc7SHans Petter Selasky 	lkpi_msec2hz_max = -1ULL / lkpi_msec2hz_rem;
20368d59ecb2SHans Petter Selasky }
20378d59ecb2SHans Petter Selasky SYSINIT(linux_timer, SI_SUB_DRIVERS, SI_ORDER_FIRST, linux_timer_init, NULL);
20388d59ecb2SHans Petter Selasky 
20398d59ecb2SHans Petter Selasky void
20408d59ecb2SHans Petter Selasky linux_complete_common(struct completion *c, int all)
20418d59ecb2SHans Petter Selasky {
20428d59ecb2SHans Petter Selasky 	int wakeup_swapper;
20438d59ecb2SHans Petter Selasky 
20448d59ecb2SHans Petter Selasky 	sleepq_lock(c);
2045be15e133SHans Petter Selasky 	if (all) {
2046be15e133SHans Petter Selasky 		c->done = UINT_MAX;
20478d59ecb2SHans Petter Selasky 		wakeup_swapper = sleepq_broadcast(c, SLEEPQ_SLEEP, 0, 0);
2048be15e133SHans Petter Selasky 	} else {
2049be15e133SHans Petter Selasky 		if (c->done != UINT_MAX)
2050be15e133SHans Petter Selasky 			c->done++;
20518d59ecb2SHans Petter Selasky 		wakeup_swapper = sleepq_signal(c, SLEEPQ_SLEEP, 0, 0);
2052be15e133SHans Petter Selasky 	}
20538d59ecb2SHans Petter Selasky 	sleepq_release(c);
20548d59ecb2SHans Petter Selasky 	if (wakeup_swapper)
20558d59ecb2SHans Petter Selasky 		kick_proc0();
20568d59ecb2SHans Petter Selasky }
20578d59ecb2SHans Petter Selasky 
20588d59ecb2SHans Petter Selasky /*
20598d59ecb2SHans Petter Selasky  * Indefinite wait for done != 0 with or without signals.
20608d59ecb2SHans Petter Selasky  */
2061f6800be3SHans Petter Selasky int
20628d59ecb2SHans Petter Selasky linux_wait_for_common(struct completion *c, int flags)
20638d59ecb2SHans Petter Selasky {
206494944062SHans Petter Selasky 	struct task_struct *task;
2065f6800be3SHans Petter Selasky 	int error;
2066b602c283SMark Johnston 
20678eeb3e17SHans Petter Selasky 	if (SCHEDULER_STOPPED())
206885714218SHans Petter Selasky 		return (0);
20698d59ecb2SHans Petter Selasky 
207094944062SHans Petter Selasky 	task = current;
207194944062SHans Petter Selasky 
20728d59ecb2SHans Petter Selasky 	if (flags != 0)
20738d59ecb2SHans Petter Selasky 		flags = SLEEPQ_INTERRUPTIBLE | SLEEPQ_SLEEP;
20748d59ecb2SHans Petter Selasky 	else
20758d59ecb2SHans Petter Selasky 		flags = SLEEPQ_SLEEP;
2076b602c283SMark Johnston 	error = 0;
20778d59ecb2SHans Petter Selasky 	for (;;) {
20788d59ecb2SHans Petter Selasky 		sleepq_lock(c);
20798d59ecb2SHans Petter Selasky 		if (c->done)
20808d59ecb2SHans Petter Selasky 			break;
20818d59ecb2SHans Petter Selasky 		sleepq_add(c, NULL, "completion", flags, 0);
20828d59ecb2SHans Petter Selasky 		if (flags & SLEEPQ_INTERRUPTIBLE) {
20834b706099SHans Petter Selasky 			DROP_GIANT();
208494944062SHans Petter Selasky 			error = -sleepq_wait_sig(c, 0);
20854b706099SHans Petter Selasky 			PICKUP_GIANT();
208694944062SHans Petter Selasky 			if (error != 0) {
208794944062SHans Petter Selasky 				linux_schedule_save_interrupt_value(task, error);
2088b602c283SMark Johnston 				error = -ERESTARTSYS;
2089b602c283SMark Johnston 				goto intr;
2090b602c283SMark Johnston 			}
20914b706099SHans Petter Selasky 		} else {
20924b706099SHans Petter Selasky 			DROP_GIANT();
20938d59ecb2SHans Petter Selasky 			sleepq_wait(c, 0);
20944b706099SHans Petter Selasky 			PICKUP_GIANT();
20954b706099SHans Petter Selasky 		}
20968d59ecb2SHans Petter Selasky 	}
2097be15e133SHans Petter Selasky 	if (c->done != UINT_MAX)
20988d59ecb2SHans Petter Selasky 		c->done--;
20998d59ecb2SHans Petter Selasky 	sleepq_release(c);
21008d59ecb2SHans Petter Selasky 
2101b602c283SMark Johnston intr:
2102b602c283SMark Johnston 	return (error);
21038d59ecb2SHans Petter Selasky }
21048d59ecb2SHans Petter Selasky 
21058d59ecb2SHans Petter Selasky /*
21068d59ecb2SHans Petter Selasky  * Time limited wait for done != 0 with or without signals.
21078d59ecb2SHans Petter Selasky  */
2108f6800be3SHans Petter Selasky int
2109f6800be3SHans Petter Selasky linux_wait_for_timeout_common(struct completion *c, int timeout, int flags)
21108d59ecb2SHans Petter Selasky {
211194944062SHans Petter Selasky 	struct task_struct *task;
2112f6800be3SHans Petter Selasky 	int end = jiffies + timeout;
2113f6800be3SHans Petter Selasky 	int error;
21148d59ecb2SHans Petter Selasky 
21158eeb3e17SHans Petter Selasky 	if (SCHEDULER_STOPPED())
211685714218SHans Petter Selasky 		return (0);
211785714218SHans Petter Selasky 
211894944062SHans Petter Selasky 	task = current;
211994944062SHans Petter Selasky 
21208d59ecb2SHans Petter Selasky 	if (flags != 0)
21218d59ecb2SHans Petter Selasky 		flags = SLEEPQ_INTERRUPTIBLE | SLEEPQ_SLEEP;
21228d59ecb2SHans Petter Selasky 	else
21238d59ecb2SHans Petter Selasky 		flags = SLEEPQ_SLEEP;
21248d59ecb2SHans Petter Selasky 
2125b602c283SMark Johnston 	for (;;) {
21268d59ecb2SHans Petter Selasky 		sleepq_lock(c);
21278d59ecb2SHans Petter Selasky 		if (c->done)
21288d59ecb2SHans Petter Selasky 			break;
21298d59ecb2SHans Petter Selasky 		sleepq_add(c, NULL, "completion", flags, 0);
21308d59ecb2SHans Petter Selasky 		sleepq_set_timeout(c, linux_timer_jiffies_until(end));
21314b706099SHans Petter Selasky 
21324b706099SHans Petter Selasky 		DROP_GIANT();
21338d59ecb2SHans Petter Selasky 		if (flags & SLEEPQ_INTERRUPTIBLE)
213494944062SHans Petter Selasky 			error = -sleepq_timedwait_sig(c, 0);
21358d59ecb2SHans Petter Selasky 		else
213694944062SHans Petter Selasky 			error = -sleepq_timedwait(c, 0);
21374b706099SHans Petter Selasky 		PICKUP_GIANT();
21384b706099SHans Petter Selasky 
213994944062SHans Petter Selasky 		if (error != 0) {
214094944062SHans Petter Selasky 			/* check for timeout */
214194944062SHans Petter Selasky 			if (error == -EWOULDBLOCK) {
214294944062SHans Petter Selasky 				error = 0;	/* timeout */
214394944062SHans Petter Selasky 			} else {
214494944062SHans Petter Selasky 				/* signal happened */
214594944062SHans Petter Selasky 				linux_schedule_save_interrupt_value(task, error);
2146b602c283SMark Johnston 				error = -ERESTARTSYS;
214794944062SHans Petter Selasky 			}
214894944062SHans Petter Selasky 			goto done;
21498d59ecb2SHans Petter Selasky 		}
21508d59ecb2SHans Petter Selasky 	}
2151be15e133SHans Petter Selasky 	if (c->done != UINT_MAX)
21528d59ecb2SHans Petter Selasky 		c->done--;
21538d59ecb2SHans Petter Selasky 	sleepq_release(c);
21548d59ecb2SHans Petter Selasky 
215594944062SHans Petter Selasky 	/* return how many jiffies are left */
215694944062SHans Petter Selasky 	error = linux_timer_jiffies_until(end);
215794944062SHans Petter Selasky done:
215894944062SHans Petter Selasky 	return (error);
21598d59ecb2SHans Petter Selasky }
21608d59ecb2SHans Petter Selasky 
21618d59ecb2SHans Petter Selasky int
21628d59ecb2SHans Petter Selasky linux_try_wait_for_completion(struct completion *c)
21638d59ecb2SHans Petter Selasky {
21648d59ecb2SHans Petter Selasky 	int isdone;
21658d59ecb2SHans Petter Selasky 
21668d59ecb2SHans Petter Selasky 	sleepq_lock(c);
2167be15e133SHans Petter Selasky 	isdone = (c->done != 0);
2168be15e133SHans Petter Selasky 	if (c->done != 0 && c->done != UINT_MAX)
21698d59ecb2SHans Petter Selasky 		c->done--;
21708d59ecb2SHans Petter Selasky 	sleepq_release(c);
21718d59ecb2SHans Petter Selasky 	return (isdone);
21728d59ecb2SHans Petter Selasky }
21738d59ecb2SHans Petter Selasky 
21748d59ecb2SHans Petter Selasky int
21758d59ecb2SHans Petter Selasky linux_completion_done(struct completion *c)
21768d59ecb2SHans Petter Selasky {
21778d59ecb2SHans Petter Selasky 	int isdone;
21788d59ecb2SHans Petter Selasky 
21798d59ecb2SHans Petter Selasky 	sleepq_lock(c);
2180be15e133SHans Petter Selasky 	isdone = (c->done != 0);
21818d59ecb2SHans Petter Selasky 	sleepq_release(c);
21828d59ecb2SHans Petter Selasky 	return (isdone);
21838d59ecb2SHans Petter Selasky }
21848d59ecb2SHans Petter Selasky 
21858d59ecb2SHans Petter Selasky static void
2186f823a36eSKonstantin Belousov linux_cdev_deref(struct linux_cdev *ldev)
2187f823a36eSKonstantin Belousov {
21887f9867f8SKonstantin Belousov 	if (refcount_release(&ldev->refs) &&
21897f9867f8SKonstantin Belousov 	    ldev->kobj.ktype == &linux_cdev_ktype)
2190f823a36eSKonstantin Belousov 		kfree(ldev);
2191f823a36eSKonstantin Belousov }
2192f823a36eSKonstantin Belousov 
2193f823a36eSKonstantin Belousov static void
219406204f8eSHans Petter Selasky linux_cdev_release(struct kobject *kobj)
219506204f8eSHans Petter Selasky {
219606204f8eSHans Petter Selasky 	struct linux_cdev *cdev;
219706204f8eSHans Petter Selasky 	struct kobject *parent;
219806204f8eSHans Petter Selasky 
219906204f8eSHans Petter Selasky 	cdev = container_of(kobj, struct linux_cdev, kobj);
220006204f8eSHans Petter Selasky 	parent = kobj->parent;
22019c7b53ccSSlava Shwartsman 	linux_destroy_dev(cdev);
2202f823a36eSKonstantin Belousov 	linux_cdev_deref(cdev);
220306204f8eSHans Petter Selasky 	kobject_put(parent);
220406204f8eSHans Petter Selasky }
220506204f8eSHans Petter Selasky 
220606204f8eSHans Petter Selasky static void
220706204f8eSHans Petter Selasky linux_cdev_static_release(struct kobject *kobj)
220806204f8eSHans Petter Selasky {
22097f9867f8SKonstantin Belousov 	struct cdev *cdev;
221028b482e2SKonstantin Belousov 	struct linux_cdev *ldev;
221106204f8eSHans Petter Selasky 
221228b482e2SKonstantin Belousov 	ldev = container_of(kobj, struct linux_cdev, kobj);
22137f9867f8SKonstantin Belousov 	cdev = ldev->cdev;
22147f9867f8SKonstantin Belousov 	if (cdev != NULL) {
22157f9867f8SKonstantin Belousov 		destroy_dev(cdev);
22167f9867f8SKonstantin Belousov 		ldev->cdev = NULL;
22177f9867f8SKonstantin Belousov 	}
22188011fb79SKonstantin Belousov 	kobject_put(kobj->parent);
221906204f8eSHans Petter Selasky }
222006204f8eSHans Petter Selasky 
2221b8f113caSHans Petter Selasky int
2222b8f113caSHans Petter Selasky linux_cdev_device_add(struct linux_cdev *ldev, struct device *dev)
2223b8f113caSHans Petter Selasky {
2224b8f113caSHans Petter Selasky 	int ret;
2225b8f113caSHans Petter Selasky 
2226b8f113caSHans Petter Selasky 	if (dev->devt != 0) {
2227b8f113caSHans Petter Selasky 		/* Set parent kernel object. */
2228b8f113caSHans Petter Selasky 		ldev->kobj.parent = &dev->kobj;
2229b8f113caSHans Petter Selasky 
2230b8f113caSHans Petter Selasky 		/*
2231b8f113caSHans Petter Selasky 		 * Unlike Linux we require the kobject of the
2232b8f113caSHans Petter Selasky 		 * character device structure to have a valid name
2233b8f113caSHans Petter Selasky 		 * before calling this function:
2234b8f113caSHans Petter Selasky 		 */
2235b8f113caSHans Petter Selasky 		if (ldev->kobj.name == NULL)
2236b8f113caSHans Petter Selasky 			return (-EINVAL);
2237b8f113caSHans Petter Selasky 
2238b8f113caSHans Petter Selasky 		ret = cdev_add(ldev, dev->devt, 1);
2239b8f113caSHans Petter Selasky 		if (ret)
2240b8f113caSHans Petter Selasky 			return (ret);
2241b8f113caSHans Petter Selasky 	}
2242b8f113caSHans Petter Selasky 	ret = device_add(dev);
2243b8f113caSHans Petter Selasky 	if (ret != 0 && dev->devt != 0)
2244b8f113caSHans Petter Selasky 		cdev_del(ldev);
2245b8f113caSHans Petter Selasky 	return (ret);
2246b8f113caSHans Petter Selasky }
2247b8f113caSHans Petter Selasky 
2248b8f113caSHans Petter Selasky void
2249b8f113caSHans Petter Selasky linux_cdev_device_del(struct linux_cdev *ldev, struct device *dev)
2250b8f113caSHans Petter Selasky {
2251b8f113caSHans Petter Selasky 	device_del(dev);
2252b8f113caSHans Petter Selasky 
2253b8f113caSHans Petter Selasky 	if (dev->devt != 0)
2254b8f113caSHans Petter Selasky 		cdev_del(ldev);
2255b8f113caSHans Petter Selasky }
2256b8f113caSHans Petter Selasky 
225767807f50SHans Petter Selasky static void
2258f823a36eSKonstantin Belousov linux_destroy_dev(struct linux_cdev *ldev)
22599c7b53ccSSlava Shwartsman {
22609c7b53ccSSlava Shwartsman 
2261f823a36eSKonstantin Belousov 	if (ldev->cdev == NULL)
22629c7b53ccSSlava Shwartsman 		return;
22639c7b53ccSSlava Shwartsman 
2264f823a36eSKonstantin Belousov 	MPASS((ldev->siref & LDEV_SI_DTR) == 0);
22657f9867f8SKonstantin Belousov 	MPASS(ldev->kobj.ktype == &linux_cdev_ktype);
22667f9867f8SKonstantin Belousov 
2267f823a36eSKonstantin Belousov 	atomic_set_int(&ldev->siref, LDEV_SI_DTR);
2268f823a36eSKonstantin Belousov 	while ((atomic_load_int(&ldev->siref) & ~LDEV_SI_DTR) != 0)
2269f823a36eSKonstantin Belousov 		pause("ldevdtr", hz / 4);
22709c7b53ccSSlava Shwartsman 
2271f823a36eSKonstantin Belousov 	destroy_dev(ldev->cdev);
2272f823a36eSKonstantin Belousov 	ldev->cdev = NULL;
22739c7b53ccSSlava Shwartsman }
22749c7b53ccSSlava Shwartsman 
227506204f8eSHans Petter Selasky const struct kobj_type linux_cdev_ktype = {
227606204f8eSHans Petter Selasky 	.release = linux_cdev_release,
227706204f8eSHans Petter Selasky };
227806204f8eSHans Petter Selasky 
227906204f8eSHans Petter Selasky const struct kobj_type linux_cdev_static_ktype = {
228006204f8eSHans Petter Selasky 	.release = linux_cdev_static_release,
228106204f8eSHans Petter Selasky };
228206204f8eSHans Petter Selasky 
228306204f8eSHans Petter Selasky static void
22840c510167SHans Petter Selasky linux_handle_ifnet_link_event(void *arg, struct ifnet *ifp, int linkstate)
22850c510167SHans Petter Selasky {
22860c510167SHans Petter Selasky 	struct notifier_block *nb;
2287fdcfe8a2SBjoern A. Zeeb 	struct netdev_notifier_info ni;
22880c510167SHans Petter Selasky 
22890c510167SHans Petter Selasky 	nb = arg;
2290801cf532SBjoern A. Zeeb 	ni.ifp = ifp;
2291fdcfe8a2SBjoern A. Zeeb 	ni.dev = (struct net_device *)ifp;
22920c510167SHans Petter Selasky 	if (linkstate == LINK_STATE_UP)
2293fdcfe8a2SBjoern A. Zeeb 		nb->notifier_call(nb, NETDEV_UP, &ni);
22940c510167SHans Petter Selasky 	else
2295fdcfe8a2SBjoern A. Zeeb 		nb->notifier_call(nb, NETDEV_DOWN, &ni);
22960c510167SHans Petter Selasky }
22970c510167SHans Petter Selasky 
22980c510167SHans Petter Selasky static void
22990c510167SHans Petter Selasky linux_handle_ifnet_arrival_event(void *arg, struct ifnet *ifp)
23000c510167SHans Petter Selasky {
23010c510167SHans Petter Selasky 	struct notifier_block *nb;
2302fdcfe8a2SBjoern A. Zeeb 	struct netdev_notifier_info ni;
23030c510167SHans Petter Selasky 
23040c510167SHans Petter Selasky 	nb = arg;
2305801cf532SBjoern A. Zeeb 	ni.ifp = ifp;
2306fdcfe8a2SBjoern A. Zeeb 	ni.dev = (struct net_device *)ifp;
2307fdcfe8a2SBjoern A. Zeeb 	nb->notifier_call(nb, NETDEV_REGISTER, &ni);
23080c510167SHans Petter Selasky }
23090c510167SHans Petter Selasky 
23100c510167SHans Petter Selasky static void
23110c510167SHans Petter Selasky linux_handle_ifnet_departure_event(void *arg, struct ifnet *ifp)
23120c510167SHans Petter Selasky {
23130c510167SHans Petter Selasky 	struct notifier_block *nb;
2314fdcfe8a2SBjoern A. Zeeb 	struct netdev_notifier_info ni;
23150c510167SHans Petter Selasky 
23160c510167SHans Petter Selasky 	nb = arg;
2317801cf532SBjoern A. Zeeb 	ni.ifp = ifp;
2318fdcfe8a2SBjoern A. Zeeb 	ni.dev = (struct net_device *)ifp;
2319fdcfe8a2SBjoern A. Zeeb 	nb->notifier_call(nb, NETDEV_UNREGISTER, &ni);
23200c510167SHans Petter Selasky }
23210c510167SHans Petter Selasky 
23220c510167SHans Petter Selasky static void
23230c510167SHans Petter Selasky linux_handle_iflladdr_event(void *arg, struct ifnet *ifp)
23240c510167SHans Petter Selasky {
23250c510167SHans Petter Selasky 	struct notifier_block *nb;
2326fdcfe8a2SBjoern A. Zeeb 	struct netdev_notifier_info ni;
23270c510167SHans Petter Selasky 
23280c510167SHans Petter Selasky 	nb = arg;
2329801cf532SBjoern A. Zeeb 	ni.ifp = ifp;
2330fdcfe8a2SBjoern A. Zeeb 	ni.dev = (struct net_device *)ifp;
2331fdcfe8a2SBjoern A. Zeeb 	nb->notifier_call(nb, NETDEV_CHANGEADDR, &ni);
23320c510167SHans Petter Selasky }
23330c510167SHans Petter Selasky 
23340c510167SHans Petter Selasky static void
23350c510167SHans Petter Selasky linux_handle_ifaddr_event(void *arg, struct ifnet *ifp)
23360c510167SHans Petter Selasky {
23370c510167SHans Petter Selasky 	struct notifier_block *nb;
2338fdcfe8a2SBjoern A. Zeeb 	struct netdev_notifier_info ni;
23390c510167SHans Petter Selasky 
23400c510167SHans Petter Selasky 	nb = arg;
2341801cf532SBjoern A. Zeeb 	ni.ifp = ifp;
2342fdcfe8a2SBjoern A. Zeeb 	ni.dev = (struct net_device *)ifp;
2343fdcfe8a2SBjoern A. Zeeb 	nb->notifier_call(nb, NETDEV_CHANGEIFADDR, &ni);
23440c510167SHans Petter Selasky }
23450c510167SHans Petter Selasky 
23460c510167SHans Petter Selasky int
23470c510167SHans Petter Selasky register_netdevice_notifier(struct notifier_block *nb)
23480c510167SHans Petter Selasky {
23490c510167SHans Petter Selasky 
23500c510167SHans Petter Selasky 	nb->tags[NETDEV_UP] = EVENTHANDLER_REGISTER(
23510c510167SHans Petter Selasky 	    ifnet_link_event, linux_handle_ifnet_link_event, nb, 0);
23520c510167SHans Petter Selasky 	nb->tags[NETDEV_REGISTER] = EVENTHANDLER_REGISTER(
23530c510167SHans Petter Selasky 	    ifnet_arrival_event, linux_handle_ifnet_arrival_event, nb, 0);
23540c510167SHans Petter Selasky 	nb->tags[NETDEV_UNREGISTER] = EVENTHANDLER_REGISTER(
23550c510167SHans Petter Selasky 	    ifnet_departure_event, linux_handle_ifnet_departure_event, nb, 0);
23560c510167SHans Petter Selasky 	nb->tags[NETDEV_CHANGEADDR] = EVENTHANDLER_REGISTER(
23570c510167SHans Petter Selasky 	    iflladdr_event, linux_handle_iflladdr_event, nb, 0);
23580c510167SHans Petter Selasky 
23590c510167SHans Petter Selasky 	return (0);
23600c510167SHans Petter Selasky }
23610c510167SHans Petter Selasky 
23620c510167SHans Petter Selasky int
23630c510167SHans Petter Selasky register_inetaddr_notifier(struct notifier_block *nb)
23640c510167SHans Petter Selasky {
23650c510167SHans Petter Selasky 
23660c510167SHans Petter Selasky 	nb->tags[NETDEV_CHANGEIFADDR] = EVENTHANDLER_REGISTER(
23670c510167SHans Petter Selasky 	    ifaddr_event, linux_handle_ifaddr_event, nb, 0);
23680c510167SHans Petter Selasky 	return (0);
23690c510167SHans Petter Selasky }
23700c510167SHans Petter Selasky 
23710c510167SHans Petter Selasky int
23720c510167SHans Petter Selasky unregister_netdevice_notifier(struct notifier_block *nb)
23730c510167SHans Petter Selasky {
23740c510167SHans Petter Selasky 
23750c510167SHans Petter Selasky 	EVENTHANDLER_DEREGISTER(ifnet_link_event,
23760c510167SHans Petter Selasky 	    nb->tags[NETDEV_UP]);
23770c510167SHans Petter Selasky 	EVENTHANDLER_DEREGISTER(ifnet_arrival_event,
23780c510167SHans Petter Selasky 	    nb->tags[NETDEV_REGISTER]);
23790c510167SHans Petter Selasky 	EVENTHANDLER_DEREGISTER(ifnet_departure_event,
23800c510167SHans Petter Selasky 	    nb->tags[NETDEV_UNREGISTER]);
23810c510167SHans Petter Selasky 	EVENTHANDLER_DEREGISTER(iflladdr_event,
23820c510167SHans Petter Selasky 	    nb->tags[NETDEV_CHANGEADDR]);
23830c510167SHans Petter Selasky 
23840c510167SHans Petter Selasky 	return (0);
23850c510167SHans Petter Selasky }
23860c510167SHans Petter Selasky 
23870c510167SHans Petter Selasky int
23880c510167SHans Petter Selasky unregister_inetaddr_notifier(struct notifier_block *nb)
23890c510167SHans Petter Selasky {
23900c510167SHans Petter Selasky 
23910c510167SHans Petter Selasky 	EVENTHANDLER_DEREGISTER(ifaddr_event,
23920c510167SHans Petter Selasky 	    nb->tags[NETDEV_CHANGEIFADDR]);
23930c510167SHans Petter Selasky 
23940c510167SHans Petter Selasky 	return (0);
23950c510167SHans Petter Selasky }
23960c510167SHans Petter Selasky 
2397aad02fb4SHans Petter Selasky struct list_sort_thunk {
2398aad02fb4SHans Petter Selasky 	int (*cmp)(void *, struct list_head *, struct list_head *);
2399aad02fb4SHans Petter Selasky 	void *priv;
2400aad02fb4SHans Petter Selasky };
2401aad02fb4SHans Petter Selasky 
2402aad02fb4SHans Petter Selasky static inline int
2403af3c7888SEd Schouten linux_le_cmp(const void *d1, const void *d2, void *priv)
2404aad02fb4SHans Petter Selasky {
2405aad02fb4SHans Petter Selasky 	struct list_head *le1, *le2;
2406aad02fb4SHans Petter Selasky 	struct list_sort_thunk *thunk;
2407aad02fb4SHans Petter Selasky 
2408aad02fb4SHans Petter Selasky 	thunk = priv;
2409aad02fb4SHans Petter Selasky 	le1 = *(__DECONST(struct list_head **, d1));
2410aad02fb4SHans Petter Selasky 	le2 = *(__DECONST(struct list_head **, d2));
2411aad02fb4SHans Petter Selasky 	return ((thunk->cmp)(thunk->priv, le1, le2));
2412aad02fb4SHans Petter Selasky }
2413aad02fb4SHans Petter Selasky 
2414aad02fb4SHans Petter Selasky void
2415aad02fb4SHans Petter Selasky list_sort(void *priv, struct list_head *head, int (*cmp)(void *priv,
2416aad02fb4SHans Petter Selasky     struct list_head *a, struct list_head *b))
2417aad02fb4SHans Petter Selasky {
2418aad02fb4SHans Petter Selasky 	struct list_sort_thunk thunk;
2419aad02fb4SHans Petter Selasky 	struct list_head **ar, *le;
2420aad02fb4SHans Petter Selasky 	size_t count, i;
2421aad02fb4SHans Petter Selasky 
2422aad02fb4SHans Petter Selasky 	count = 0;
2423aad02fb4SHans Petter Selasky 	list_for_each(le, head)
2424aad02fb4SHans Petter Selasky 		count++;
2425aad02fb4SHans Petter Selasky 	ar = malloc(sizeof(struct list_head *) * count, M_KMALLOC, M_WAITOK);
2426aad02fb4SHans Petter Selasky 	i = 0;
2427aad02fb4SHans Petter Selasky 	list_for_each(le, head)
2428aad02fb4SHans Petter Selasky 		ar[i++] = le;
2429aad02fb4SHans Petter Selasky 	thunk.cmp = cmp;
2430aad02fb4SHans Petter Selasky 	thunk.priv = priv;
2431af3c7888SEd Schouten 	qsort_r(ar, count, sizeof(struct list_head *), linux_le_cmp, &thunk);
2432aad02fb4SHans Petter Selasky 	INIT_LIST_HEAD(head);
2433aad02fb4SHans Petter Selasky 	for (i = 0; i < count; i++)
2434aad02fb4SHans Petter Selasky 		list_add_tail(ar[i], head);
2435aad02fb4SHans Petter Selasky 	free(ar, M_KMALLOC);
2436aad02fb4SHans Petter Selasky }
2437aad02fb4SHans Petter Selasky 
2438c12488bbSMark Johnston #if defined(__i386__) || defined(__amd64__)
2439c12488bbSMark Johnston int
2440c12488bbSMark Johnston linux_wbinvd_on_all_cpus(void)
2441c12488bbSMark Johnston {
2442c12488bbSMark Johnston 
24436c106233SMark Johnston 	pmap_invalidate_cache();
24446c106233SMark Johnston 	return (0);
2445c12488bbSMark Johnston }
2446c12488bbSMark Johnston #endif
2447c12488bbSMark Johnston 
2448c12488bbSMark Johnston int
2449c12488bbSMark Johnston linux_on_each_cpu(void callback(void *), void *data)
2450c12488bbSMark Johnston {
2451c12488bbSMark Johnston 
2452c12488bbSMark Johnston 	smp_rendezvous(smp_no_rendezvous_barrier, callback,
2453c12488bbSMark Johnston 	    smp_no_rendezvous_barrier, data);
2454c12488bbSMark Johnston 	return (0);
2455c12488bbSMark Johnston }
2456c12488bbSMark Johnston 
2457dff36e69SHans Petter Selasky int
2458dff36e69SHans Petter Selasky linux_in_atomic(void)
2459dff36e69SHans Petter Selasky {
2460dff36e69SHans Petter Selasky 
2461dff36e69SHans Petter Selasky 	return ((curthread->td_pflags & TDP_NOFAULTING) != 0);
2462dff36e69SHans Petter Selasky }
2463dff36e69SHans Petter Selasky 
24641125dbc0SHans Petter Selasky struct linux_cdev *
24651125dbc0SHans Petter Selasky linux_find_cdev(const char *name, unsigned major, unsigned minor)
24661125dbc0SHans Petter Selasky {
24672b79a966SHans Petter Selasky 	dev_t dev = MKDEV(major, minor);
24681125dbc0SHans Petter Selasky 	struct cdev *cdev;
24691125dbc0SHans Petter Selasky 
24701125dbc0SHans Petter Selasky 	dev_lock();
24711125dbc0SHans Petter Selasky 	LIST_FOREACH(cdev, &linuxcdevsw.d_devs, si_list) {
24721125dbc0SHans Petter Selasky 		struct linux_cdev *ldev = cdev->si_drv1;
24732b79a966SHans Petter Selasky 		if (ldev->dev == dev &&
24741125dbc0SHans Petter Selasky 		    strcmp(kobject_name(&ldev->kobj), name) == 0) {
24751125dbc0SHans Petter Selasky 			break;
24761125dbc0SHans Petter Selasky 		}
24771125dbc0SHans Petter Selasky 	}
24781125dbc0SHans Petter Selasky 	dev_unlock();
24791125dbc0SHans Petter Selasky 
24801125dbc0SHans Petter Selasky 	return (cdev != NULL ? cdev->si_drv1 : NULL);
24811125dbc0SHans Petter Selasky }
24821125dbc0SHans Petter Selasky 
24831125dbc0SHans Petter Selasky int
24841125dbc0SHans Petter Selasky __register_chrdev(unsigned int major, unsigned int baseminor,
24851125dbc0SHans Petter Selasky     unsigned int count, const char *name,
24861125dbc0SHans Petter Selasky     const struct file_operations *fops)
24871125dbc0SHans Petter Selasky {
24881125dbc0SHans Petter Selasky 	struct linux_cdev *cdev;
24891125dbc0SHans Petter Selasky 	int ret = 0;
24901125dbc0SHans Petter Selasky 	int i;
24911125dbc0SHans Petter Selasky 
24921125dbc0SHans Petter Selasky 	for (i = baseminor; i < baseminor + count; i++) {
24931125dbc0SHans Petter Selasky 		cdev = cdev_alloc();
2494af248a7cSJohannes Lundberg 		cdev->ops = fops;
24951125dbc0SHans Petter Selasky 		kobject_set_name(&cdev->kobj, name);
24961125dbc0SHans Petter Selasky 
24971125dbc0SHans Petter Selasky 		ret = cdev_add(cdev, makedev(major, i), 1);
24981125dbc0SHans Petter Selasky 		if (ret != 0)
24991125dbc0SHans Petter Selasky 			break;
25001125dbc0SHans Petter Selasky 	}
25011125dbc0SHans Petter Selasky 	return (ret);
25021125dbc0SHans Petter Selasky }
25031125dbc0SHans Petter Selasky 
25041125dbc0SHans Petter Selasky int
25051125dbc0SHans Petter Selasky __register_chrdev_p(unsigned int major, unsigned int baseminor,
25061125dbc0SHans Petter Selasky     unsigned int count, const char *name,
25071125dbc0SHans Petter Selasky     const struct file_operations *fops, uid_t uid,
25081125dbc0SHans Petter Selasky     gid_t gid, int mode)
25091125dbc0SHans Petter Selasky {
25101125dbc0SHans Petter Selasky 	struct linux_cdev *cdev;
25111125dbc0SHans Petter Selasky 	int ret = 0;
25121125dbc0SHans Petter Selasky 	int i;
25131125dbc0SHans Petter Selasky 
25141125dbc0SHans Petter Selasky 	for (i = baseminor; i < baseminor + count; i++) {
25151125dbc0SHans Petter Selasky 		cdev = cdev_alloc();
2516af248a7cSJohannes Lundberg 		cdev->ops = fops;
25171125dbc0SHans Petter Selasky 		kobject_set_name(&cdev->kobj, name);
25181125dbc0SHans Petter Selasky 
25191125dbc0SHans Petter Selasky 		ret = cdev_add_ext(cdev, makedev(major, i), uid, gid, mode);
25201125dbc0SHans Petter Selasky 		if (ret != 0)
25211125dbc0SHans Petter Selasky 			break;
25221125dbc0SHans Petter Selasky 	}
25231125dbc0SHans Petter Selasky 	return (ret);
25241125dbc0SHans Petter Selasky }
25251125dbc0SHans Petter Selasky 
25261125dbc0SHans Petter Selasky void
25271125dbc0SHans Petter Selasky __unregister_chrdev(unsigned int major, unsigned int baseminor,
25281125dbc0SHans Petter Selasky     unsigned int count, const char *name)
25291125dbc0SHans Petter Selasky {
25301125dbc0SHans Petter Selasky 	struct linux_cdev *cdevp;
25311125dbc0SHans Petter Selasky 	int i;
25321125dbc0SHans Petter Selasky 
25331125dbc0SHans Petter Selasky 	for (i = baseminor; i < baseminor + count; i++) {
25341125dbc0SHans Petter Selasky 		cdevp = linux_find_cdev(name, major, i);
25351125dbc0SHans Petter Selasky 		if (cdevp != NULL)
25361125dbc0SHans Petter Selasky 			cdev_del(cdevp);
25371125dbc0SHans Petter Selasky 	}
25381125dbc0SHans Petter Selasky }
25391125dbc0SHans Petter Selasky 
2540e35079dbSHans Petter Selasky void
2541e35079dbSHans Petter Selasky linux_dump_stack(void)
2542e35079dbSHans Petter Selasky {
2543e35079dbSHans Petter Selasky #ifdef STACK
2544e35079dbSHans Petter Selasky 	struct stack st;
2545e35079dbSHans Petter Selasky 
2546e35079dbSHans Petter Selasky 	stack_save(&st);
2547e35079dbSHans Petter Selasky 	stack_print(&st);
2548e35079dbSHans Petter Selasky #endif
2549e35079dbSHans Petter Selasky }
2550e35079dbSHans Petter Selasky 
2551bc042266SBjoern A. Zeeb int
2552bc042266SBjoern A. Zeeb linuxkpi_net_ratelimit(void)
2553bc042266SBjoern A. Zeeb {
2554bc042266SBjoern A. Zeeb 
2555bc042266SBjoern A. Zeeb 	return (ppsratecheck(&lkpi_net_lastlog, &lkpi_net_curpps,
2556bc042266SBjoern A. Zeeb 	   lkpi_net_maxpps));
2557bc042266SBjoern A. Zeeb }
2558bc042266SBjoern A. Zeeb 
2559132b00f9SWarner Losh struct io_mapping *
2560132b00f9SWarner Losh io_mapping_create_wc(resource_size_t base, unsigned long size)
2561132b00f9SWarner Losh {
2562132b00f9SWarner Losh 	struct io_mapping *mapping;
2563132b00f9SWarner Losh 
2564132b00f9SWarner Losh 	mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
2565132b00f9SWarner Losh 	if (mapping == NULL)
2566132b00f9SWarner Losh 		return (NULL);
2567132b00f9SWarner Losh 	return (io_mapping_init_wc(mapping, base, size));
2568132b00f9SWarner Losh }
2569132b00f9SWarner Losh 
257003219fbaSHans Petter Selasky #if defined(__i386__) || defined(__amd64__)
257103219fbaSHans Petter Selasky bool linux_cpu_has_clflush;
2572b2c86006SEmmanuel Vadot struct cpuinfo_x86 boot_cpu_data;
2573*488e8a7fSBjoern A. Zeeb struct cpuinfo_x86 *__cpu_data;
257403219fbaSHans Petter Selasky #endif
257503219fbaSHans Petter Selasky 
2576e2361e04SBjoern A. Zeeb cpumask_t *
2577e2361e04SBjoern A. Zeeb lkpi_get_static_single_cpu_mask(int cpuid)
2578e2361e04SBjoern A. Zeeb {
2579e2361e04SBjoern A. Zeeb 
2580*488e8a7fSBjoern A. Zeeb 	KASSERT((cpuid >= 0 && cpuid <= mp_maxid), ("%s: invalid cpuid %d\n",
2581*488e8a7fSBjoern A. Zeeb 	    __func__, cpuid));
2582*488e8a7fSBjoern A. Zeeb 	KASSERT(!CPU_ABSENT(cpuid), ("%s: cpu with cpuid %d is absent\n",
2583e2361e04SBjoern A. Zeeb 	    __func__, cpuid));
2584e2361e04SBjoern A. Zeeb 
2585*488e8a7fSBjoern A. Zeeb 	return (static_single_cpu_mask[cpuid]);
2586e2361e04SBjoern A. Zeeb }
2587e2361e04SBjoern A. Zeeb 
25880c510167SHans Petter Selasky static void
25898d59ecb2SHans Petter Selasky linux_compat_init(void *arg)
25908d59ecb2SHans Petter Selasky {
25918d59ecb2SHans Petter Selasky 	struct sysctl_oid *rootoid;
25928d59ecb2SHans Petter Selasky 	int i;
25938d59ecb2SHans Petter Selasky 
259403219fbaSHans Petter Selasky #if defined(__i386__) || defined(__amd64__)
259503219fbaSHans Petter Selasky 	linux_cpu_has_clflush = (cpu_feature & CPUID_CLFSH);
2596b2c86006SEmmanuel Vadot 	boot_cpu_data.x86_clflush_size = cpu_clflush_line_size;
259758cf3a69SJean-Sébastien Pédron 	boot_cpu_data.x86_max_cores = mp_ncpus;
2598a27902c1SJean-Sébastien Pédron 	boot_cpu_data.x86 = CPUID_TO_FAMILY(cpu_id);
2599a27902c1SJean-Sébastien Pédron 	boot_cpu_data.x86_model = CPUID_TO_MODEL(cpu_id);
2600a27902c1SJean-Sébastien Pédron 
2601*488e8a7fSBjoern A. Zeeb 	__cpu_data = mallocarray(mp_maxid + 1,
2602*488e8a7fSBjoern A. Zeeb 	    sizeof(*__cpu_data), M_KMALLOC, M_WAITOK | M_ZERO);
2603*488e8a7fSBjoern A. Zeeb 	CPU_FOREACH(i) {
2604a27902c1SJean-Sébastien Pédron 		__cpu_data[i].x86_clflush_size = cpu_clflush_line_size;
2605a27902c1SJean-Sébastien Pédron 		__cpu_data[i].x86_max_cores = mp_ncpus;
2606a27902c1SJean-Sébastien Pédron 		__cpu_data[i].x86 = CPUID_TO_FAMILY(cpu_id);
2607a27902c1SJean-Sébastien Pédron 		__cpu_data[i].x86_model = CPUID_TO_MODEL(cpu_id);
2608a27902c1SJean-Sébastien Pédron 	}
260903219fbaSHans Petter Selasky #endif
26101ea4c857SHans Petter Selasky 	rw_init(&linux_vma_lock, "lkpi-vma-lock");
2611e10c4cc0SHans Petter Selasky 
26128d59ecb2SHans Petter Selasky 	rootoid = SYSCTL_ADD_ROOT_NODE(NULL,
26138d59ecb2SHans Petter Selasky 	    OID_AUTO, "sys", CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, "sys");
261406204f8eSHans Petter Selasky 	kobject_init(&linux_class_root, &linux_class_ktype);
261506204f8eSHans Petter Selasky 	kobject_set_name(&linux_class_root, "class");
261606204f8eSHans Petter Selasky 	linux_class_root.oidp = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(rootoid),
26178d59ecb2SHans Petter Selasky 	    OID_AUTO, "class", CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, "class");
261806204f8eSHans Petter Selasky 	kobject_init(&linux_root_device.kobj, &linux_dev_ktype);
261906204f8eSHans Petter Selasky 	kobject_set_name(&linux_root_device.kobj, "device");
262006204f8eSHans Petter Selasky 	linux_root_device.kobj.oidp = SYSCTL_ADD_NODE(NULL,
262139a3542bSPawel Biernacki 	    SYSCTL_CHILDREN(rootoid), OID_AUTO, "device",
262239a3542bSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "device");
262306204f8eSHans Petter Selasky 	linux_root_device.bsddev = root_bus;
262406204f8eSHans Petter Selasky 	linux_class_misc.name = "misc";
262506204f8eSHans Petter Selasky 	class_register(&linux_class_misc);
26268d59ecb2SHans Petter Selasky 	INIT_LIST_HEAD(&pci_drivers);
26278d59ecb2SHans Petter Selasky 	INIT_LIST_HEAD(&pci_devices);
26288d59ecb2SHans Petter Selasky 	spin_lock_init(&pci_lock);
26298d59ecb2SHans Petter Selasky 	mtx_init(&vmmaplock, "IO Map lock", NULL, MTX_DEF);
26308d59ecb2SHans Petter Selasky 	for (i = 0; i < VMMAP_HASH_SIZE; i++)
26318d59ecb2SHans Petter Selasky 		LIST_INIT(&vmmaphead[i]);
26320e123c13SEmmanuel Vadot 	init_waitqueue_head(&linux_bit_waitq);
26330e123c13SEmmanuel Vadot 	init_waitqueue_head(&linux_var_waitq);
26345fce8027SBjoern A. Zeeb 
26355fce8027SBjoern A. Zeeb 	CPU_COPY(&all_cpus, &cpu_online_mask);
2636e2361e04SBjoern A. Zeeb 	/*
2637e2361e04SBjoern A. Zeeb 	 * Generate a single-CPU cpumask_t for each CPU (possibly) in the system.
2638*488e8a7fSBjoern A. Zeeb 	 * CPUs are indexed from 0..(mp_maxid).  The entry for cpuid 0 will only
2639e2361e04SBjoern A. Zeeb 	 * have itself in the cpumask, cupid 1 only itself on entry 1, and so on.
2640e2361e04SBjoern A. Zeeb 	 * This is used by cpumask_of() (and possibly others in the future) for,
2641e2361e04SBjoern A. Zeeb 	 * e.g., drivers to pass hints to irq_set_affinity_hint().
2642e2361e04SBjoern A. Zeeb 	 */
2643*488e8a7fSBjoern A. Zeeb 	static_single_cpu_mask = mallocarray(mp_maxid + 1,
2644*488e8a7fSBjoern A. Zeeb 	    sizeof(static_single_cpu_mask), M_KMALLOC, M_WAITOK | M_ZERO);
2645*488e8a7fSBjoern A. Zeeb 
2646*488e8a7fSBjoern A. Zeeb 	/*
2647*488e8a7fSBjoern A. Zeeb 	 * When the number of CPUs reach a threshold, we start to save memory
2648*488e8a7fSBjoern A. Zeeb 	 * given the sets are static by overlapping those having their single
2649*488e8a7fSBjoern A. Zeeb 	 * bit set at same position in a bitset word.  Asymptotically, this
2650*488e8a7fSBjoern A. Zeeb 	 * regular scheme is in O(n²) whereas the overlapping one is in O(n)
2651*488e8a7fSBjoern A. Zeeb 	 * only with n being the maximum number of CPUs, so the gain will become
2652*488e8a7fSBjoern A. Zeeb 	 * huge quite quickly.  The threshold for 64-bit architectures is 128
2653*488e8a7fSBjoern A. Zeeb 	 * CPUs.
2654*488e8a7fSBjoern A. Zeeb 	 */
2655*488e8a7fSBjoern A. Zeeb 	if (mp_ncpus < (2 * _BITSET_BITS)) {
2656*488e8a7fSBjoern A. Zeeb 		cpumask_t *sscm_ptr;
2657*488e8a7fSBjoern A. Zeeb 
2658*488e8a7fSBjoern A. Zeeb 		/*
2659*488e8a7fSBjoern A. Zeeb 		 * This represents 'mp_ncpus * __bitset_words(CPU_SETSIZE) *
2660*488e8a7fSBjoern A. Zeeb 		 * (_BITSET_BITS / 8)' bytes (for comparison with the
2661*488e8a7fSBjoern A. Zeeb 		 * overlapping scheme).
2662*488e8a7fSBjoern A. Zeeb 		 */
2663*488e8a7fSBjoern A. Zeeb 		static_single_cpu_mask_lcs = mallocarray(mp_ncpus,
2664*488e8a7fSBjoern A. Zeeb 		    sizeof(*static_single_cpu_mask_lcs),
2665*488e8a7fSBjoern A. Zeeb 		    M_KMALLOC, M_WAITOK | M_ZERO);
2666*488e8a7fSBjoern A. Zeeb 
2667*488e8a7fSBjoern A. Zeeb 		sscm_ptr = static_single_cpu_mask_lcs;
2668*488e8a7fSBjoern A. Zeeb 		CPU_FOREACH(i) {
2669*488e8a7fSBjoern A. Zeeb 			static_single_cpu_mask[i] = sscm_ptr++;
2670*488e8a7fSBjoern A. Zeeb 			CPU_SET(i, static_single_cpu_mask[i]);
2671*488e8a7fSBjoern A. Zeeb 		}
2672*488e8a7fSBjoern A. Zeeb 	} else {
2673*488e8a7fSBjoern A. Zeeb 		/* Pointer to a bitset word. */
2674*488e8a7fSBjoern A. Zeeb 		__typeof(((cpuset_t *)NULL)->__bits[0]) *bwp;
2675*488e8a7fSBjoern A. Zeeb 
2676*488e8a7fSBjoern A. Zeeb 		/*
2677*488e8a7fSBjoern A. Zeeb 		 * Allocate memory for (static) spans of 'cpumask_t' ('cpuset_t'
2678*488e8a7fSBjoern A. Zeeb 		 * really) with a single bit set that can be reused for all
2679*488e8a7fSBjoern A. Zeeb 		 * single CPU masks by making them start at different offsets.
2680*488e8a7fSBjoern A. Zeeb 		 * We need '__bitset_words(CPU_SETSIZE) - 1' bitset words before
2681*488e8a7fSBjoern A. Zeeb 		 * the word having its single bit set, and the same amount
2682*488e8a7fSBjoern A. Zeeb 		 * after.
2683*488e8a7fSBjoern A. Zeeb 		 */
2684*488e8a7fSBjoern A. Zeeb 		static_single_cpu_mask_lcs = mallocarray(_BITSET_BITS,
2685*488e8a7fSBjoern A. Zeeb 		    (2 * __bitset_words(CPU_SETSIZE) - 1) * (_BITSET_BITS / 8),
2686*488e8a7fSBjoern A. Zeeb 		    M_KMALLOC, M_WAITOK | M_ZERO);
2687*488e8a7fSBjoern A. Zeeb 
2688*488e8a7fSBjoern A. Zeeb 		/*
2689*488e8a7fSBjoern A. Zeeb 		 * We rely below on cpuset_t and the bitset generic
2690*488e8a7fSBjoern A. Zeeb 		 * implementation assigning words in the '__bits' array in the
2691*488e8a7fSBjoern A. Zeeb 		 * same order of bits (i.e., little-endian ordering, not to be
2692*488e8a7fSBjoern A. Zeeb 		 * confused with machine endianness, which concerns bits in
2693*488e8a7fSBjoern A. Zeeb 		 * words and other integers).  This is an imperfect test, but it
2694*488e8a7fSBjoern A. Zeeb 		 * will detect a change to big-endian ordering.
2695*488e8a7fSBjoern A. Zeeb 		 */
2696*488e8a7fSBjoern A. Zeeb 		_Static_assert(
2697*488e8a7fSBjoern A. Zeeb 		    __bitset_word(_BITSET_BITS + 1, _BITSET_BITS) == 1,
2698*488e8a7fSBjoern A. Zeeb 		    "Assumes a bitset implementation that is little-endian "
2699*488e8a7fSBjoern A. Zeeb 		    "on its words");
2700*488e8a7fSBjoern A. Zeeb 
2701*488e8a7fSBjoern A. Zeeb 		/* Initialize the single bit of each static span. */
2702*488e8a7fSBjoern A. Zeeb 		bwp = (__typeof(bwp))static_single_cpu_mask_lcs +
2703*488e8a7fSBjoern A. Zeeb 		    (__bitset_words(CPU_SETSIZE) - 1);
2704*488e8a7fSBjoern A. Zeeb 		for (i = 0; i < _BITSET_BITS; i++) {
2705*488e8a7fSBjoern A. Zeeb 			CPU_SET(i, (cpuset_t *)bwp);
2706*488e8a7fSBjoern A. Zeeb 			bwp += (2 * __bitset_words(CPU_SETSIZE) - 1);
2707*488e8a7fSBjoern A. Zeeb 		}
2708*488e8a7fSBjoern A. Zeeb 
2709*488e8a7fSBjoern A. Zeeb 		/*
2710*488e8a7fSBjoern A. Zeeb 		 * Finally set all CPU masks to the proper word in their
2711*488e8a7fSBjoern A. Zeeb 		 * relevant span.
2712*488e8a7fSBjoern A. Zeeb 		 */
2713*488e8a7fSBjoern A. Zeeb 		CPU_FOREACH(i) {
2714*488e8a7fSBjoern A. Zeeb 			bwp = (__typeof(bwp))static_single_cpu_mask_lcs;
2715*488e8a7fSBjoern A. Zeeb 			/* Find the non-zero word of the relevant span. */
2716*488e8a7fSBjoern A. Zeeb 			bwp += (2 * __bitset_words(CPU_SETSIZE) - 1) *
2717*488e8a7fSBjoern A. Zeeb 			    (i % _BITSET_BITS) +
2718*488e8a7fSBjoern A. Zeeb 			    __bitset_words(CPU_SETSIZE) - 1;
2719*488e8a7fSBjoern A. Zeeb 			/* Shift to find the CPU mask start. */
2720*488e8a7fSBjoern A. Zeeb 			bwp -= (i / _BITSET_BITS);
2721*488e8a7fSBjoern A. Zeeb 			static_single_cpu_mask[i] = (cpuset_t *)bwp;
2722*488e8a7fSBjoern A. Zeeb 		}
2723*488e8a7fSBjoern A. Zeeb 	}
2724ad513b4dSBjoern A. Zeeb 
2725ad513b4dSBjoern A. Zeeb 	strlcpy(init_uts_ns.name.release, osrelease, sizeof(init_uts_ns.name.release));
27268d59ecb2SHans Petter Selasky }
27278d59ecb2SHans Petter Selasky SYSINIT(linux_compat, SI_SUB_DRIVERS, SI_ORDER_SECOND, linux_compat_init, NULL);
27288d59ecb2SHans Petter Selasky 
27298d59ecb2SHans Petter Selasky static void
27308d59ecb2SHans Petter Selasky linux_compat_uninit(void *arg)
27318d59ecb2SHans Petter Selasky {
273206204f8eSHans Petter Selasky 	linux_kobject_kfree_name(&linux_class_root);
273306204f8eSHans Petter Selasky 	linux_kobject_kfree_name(&linux_root_device.kobj);
273406204f8eSHans Petter Selasky 	linux_kobject_kfree_name(&linux_class_misc.kobj);
27351ea4c857SHans Petter Selasky 
2736*488e8a7fSBjoern A. Zeeb 	free(static_single_cpu_mask_lcs, M_KMALLOC);
2737*488e8a7fSBjoern A. Zeeb 	free(static_single_cpu_mask, M_KMALLOC);
2738*488e8a7fSBjoern A. Zeeb #if defined(__i386__) || defined(__amd64__)
2739*488e8a7fSBjoern A. Zeeb 	free(__cpu_data, M_KMALLOC);
2740*488e8a7fSBjoern A. Zeeb #endif
2741*488e8a7fSBjoern A. Zeeb 
274247d8a7d4SMark Johnston 	mtx_destroy(&vmmaplock);
274347d8a7d4SMark Johnston 	spin_lock_destroy(&pci_lock);
27441ea4c857SHans Petter Selasky 	rw_destroy(&linux_vma_lock);
27458d59ecb2SHans Petter Selasky }
27468d59ecb2SHans Petter Selasky SYSUNINIT(linux_compat, SI_SUB_DRIVERS, SI_ORDER_SECOND, linux_compat_uninit, NULL);
2747f727a767SHans Petter Selasky 
2748f727a767SHans Petter Selasky /*
2749f727a767SHans Petter Selasky  * NOTE: Linux frequently uses "unsigned long" for pointer to integer
2750f727a767SHans Petter Selasky  * conversion and vice versa, where in FreeBSD "uintptr_t" would be
2751f727a767SHans Petter Selasky  * used. Assert these types have the same size, else some parts of the
2752f727a767SHans Petter Selasky  * LinuxKPI may not work like expected:
2753f727a767SHans Petter Selasky  */
2754f727a767SHans Petter Selasky CTASSERT(sizeof(unsigned long) == sizeof(uintptr_t));
2755