1 /*-
2  * Copyright (c) 2010 Isilon Systems, Inc.
3  * Copyright (c) 2010 iX Systems, Inc.
4  * Copyright (c) 2010 Panasas, Inc.
5  * Copyright (c) 2013-2021 Mellanox Technologies, Ltd.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice unmodified, this list of conditions, and the following
13  *    disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include "opt_stack.h"
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/malloc.h>
38 #include <sys/kernel.h>
39 #include <sys/sysctl.h>
40 #include <sys/proc.h>
41 #include <sys/sglist.h>
42 #include <sys/sleepqueue.h>
43 #include <sys/refcount.h>
44 #include <sys/lock.h>
45 #include <sys/mutex.h>
46 #include <sys/bus.h>
47 #include <sys/eventhandler.h>
48 #include <sys/fcntl.h>
49 #include <sys/file.h>
50 #include <sys/filio.h>
51 #include <sys/rwlock.h>
52 #include <sys/mman.h>
53 #include <sys/stack.h>
54 #include <sys/sysent.h>
55 #include <sys/time.h>
56 #include <sys/user.h>
57 
58 #include <vm/vm.h>
59 #include <vm/pmap.h>
60 #include <vm/vm_object.h>
61 #include <vm/vm_page.h>
62 #include <vm/vm_pager.h>
63 
64 #include <machine/stdarg.h>
65 
66 #if defined(__i386__) || defined(__amd64__)
67 #include <machine/md_var.h>
68 #endif
69 
70 #include <linux/kobject.h>
71 #include <linux/cpu.h>
72 #include <linux/device.h>
73 #include <linux/slab.h>
74 #include <linux/module.h>
75 #include <linux/moduleparam.h>
76 #include <linux/cdev.h>
77 #include <linux/file.h>
78 #include <linux/sysfs.h>
79 #include <linux/mm.h>
80 #include <linux/io.h>
81 #include <linux/vmalloc.h>
82 #include <linux/netdevice.h>
83 #include <linux/timer.h>
84 #include <linux/interrupt.h>
85 #include <linux/uaccess.h>
86 #include <linux/list.h>
87 #include <linux/kthread.h>
88 #include <linux/kernel.h>
89 #include <linux/compat.h>
90 #include <linux/poll.h>
91 #include <linux/smp.h>
92 #include <linux/wait_bit.h>
93 #include <linux/rcupdate.h>
94 #include <linux/interval_tree.h>
95 #include <linux/interval_tree_generic.h>
96 
97 #if defined(__i386__) || defined(__amd64__)
98 #include <asm/smp.h>
99 #endif
100 
101 SYSCTL_NODE(_compat, OID_AUTO, linuxkpi, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
102     "LinuxKPI parameters");
103 
104 int linuxkpi_debug;
105 SYSCTL_INT(_compat_linuxkpi, OID_AUTO, debug, CTLFLAG_RWTUN,
106     &linuxkpi_debug, 0, "Set to enable pr_debug() prints. Clear to disable.");
107 
108 int linuxkpi_warn_dump_stack = 0;
109 SYSCTL_INT(_compat_linuxkpi, OID_AUTO, warn_dump_stack, CTLFLAG_RWTUN,
110     &linuxkpi_warn_dump_stack, 0,
111     "Set to enable stack traces from WARN_ON(). Clear to disable.");
112 
113 static struct timeval lkpi_net_lastlog;
114 static int lkpi_net_curpps;
115 static int lkpi_net_maxpps = 99;
116 SYSCTL_INT(_compat_linuxkpi, OID_AUTO, net_ratelimit, CTLFLAG_RWTUN,
117     &lkpi_net_maxpps, 0, "Limit number of LinuxKPI net messages per second.");
118 
119 MALLOC_DEFINE(M_KMALLOC, "lkpikmalloc", "Linux kmalloc compat");
120 
121 #include <linux/rbtree.h>
122 /* Undo Linux compat changes. */
123 #undef RB_ROOT
124 #undef file
125 #undef cdev
126 #define	RB_ROOT(head)	(head)->rbh_root
127 
128 static void linux_destroy_dev(struct linux_cdev *);
129 static void linux_cdev_deref(struct linux_cdev *ldev);
130 static struct vm_area_struct *linux_cdev_handle_find(void *handle);
131 
132 cpumask_t cpu_online_mask;
133 struct kobject linux_class_root;
134 struct device linux_root_device;
135 struct class linux_class_misc;
136 struct list_head pci_drivers;
137 struct list_head pci_devices;
138 spinlock_t pci_lock;
139 
140 unsigned long linux_timer_hz_mask;
141 
142 wait_queue_head_t linux_bit_waitq;
143 wait_queue_head_t linux_var_waitq;
144 
145 int
146 panic_cmp(struct rb_node *one, struct rb_node *two)
147 {
148 	panic("no cmp");
149 }
150 
151 RB_GENERATE(linux_root, rb_node, __entry, panic_cmp);
152 
153 #define	START(node)	((node)->start)
154 #define	LAST(node)	((node)->last)
155 
156 INTERVAL_TREE_DEFINE(struct interval_tree_node, rb, unsigned long,, START,
157     LAST,, lkpi_interval_tree)
158 
159 struct kobject *
160 kobject_create(void)
161 {
162 	struct kobject *kobj;
163 
164 	kobj = kzalloc(sizeof(*kobj), GFP_KERNEL);
165 	if (kobj == NULL)
166 		return (NULL);
167 	kobject_init(kobj, &linux_kfree_type);
168 
169 	return (kobj);
170 }
171 
172 
173 int
174 kobject_set_name_vargs(struct kobject *kobj, const char *fmt, va_list args)
175 {
176 	va_list tmp_va;
177 	int len;
178 	char *old;
179 	char *name;
180 	char dummy;
181 
182 	old = kobj->name;
183 
184 	if (old && fmt == NULL)
185 		return (0);
186 
187 	/* compute length of string */
188 	va_copy(tmp_va, args);
189 	len = vsnprintf(&dummy, 0, fmt, tmp_va);
190 	va_end(tmp_va);
191 
192 	/* account for zero termination */
193 	len++;
194 
195 	/* check for error */
196 	if (len < 1)
197 		return (-EINVAL);
198 
199 	/* allocate memory for string */
200 	name = kzalloc(len, GFP_KERNEL);
201 	if (name == NULL)
202 		return (-ENOMEM);
203 	vsnprintf(name, len, fmt, args);
204 	kobj->name = name;
205 
206 	/* free old string */
207 	kfree(old);
208 
209 	/* filter new string */
210 	for (; *name != '\0'; name++)
211 		if (*name == '/')
212 			*name = '!';
213 	return (0);
214 }
215 
216 int
217 kobject_set_name(struct kobject *kobj, const char *fmt, ...)
218 {
219 	va_list args;
220 	int error;
221 
222 	va_start(args, fmt);
223 	error = kobject_set_name_vargs(kobj, fmt, args);
224 	va_end(args);
225 
226 	return (error);
227 }
228 
229 static int
230 kobject_add_complete(struct kobject *kobj, struct kobject *parent)
231 {
232 	const struct kobj_type *t;
233 	int error;
234 
235 	kobj->parent = parent;
236 	error = sysfs_create_dir(kobj);
237 	if (error == 0 && kobj->ktype && kobj->ktype->default_attrs) {
238 		struct attribute **attr;
239 		t = kobj->ktype;
240 
241 		for (attr = t->default_attrs; *attr != NULL; attr++) {
242 			error = sysfs_create_file(kobj, *attr);
243 			if (error)
244 				break;
245 		}
246 		if (error)
247 			sysfs_remove_dir(kobj);
248 	}
249 	return (error);
250 }
251 
252 int
253 kobject_add(struct kobject *kobj, struct kobject *parent, const char *fmt, ...)
254 {
255 	va_list args;
256 	int error;
257 
258 	va_start(args, fmt);
259 	error = kobject_set_name_vargs(kobj, fmt, args);
260 	va_end(args);
261 	if (error)
262 		return (error);
263 
264 	return kobject_add_complete(kobj, parent);
265 }
266 
267 void
268 linux_kobject_release(struct kref *kref)
269 {
270 	struct kobject *kobj;
271 	char *name;
272 
273 	kobj = container_of(kref, struct kobject, kref);
274 	sysfs_remove_dir(kobj);
275 	name = kobj->name;
276 	if (kobj->ktype && kobj->ktype->release)
277 		kobj->ktype->release(kobj);
278 	kfree(name);
279 }
280 
281 static void
282 linux_kobject_kfree(struct kobject *kobj)
283 {
284 	kfree(kobj);
285 }
286 
287 static void
288 linux_kobject_kfree_name(struct kobject *kobj)
289 {
290 	if (kobj) {
291 		kfree(kobj->name);
292 	}
293 }
294 
295 const struct kobj_type linux_kfree_type = {
296 	.release = linux_kobject_kfree
297 };
298 
299 static ssize_t
300 lkpi_kobj_attr_show(struct kobject *kobj, struct attribute *attr, char *buf)
301 {
302 	struct kobj_attribute *ka =
303 	    container_of(attr, struct kobj_attribute, attr);
304 
305 	if (ka->show == NULL)
306 		return (-EIO);
307 
308 	return (ka->show(kobj, ka, buf));
309 }
310 
311 static ssize_t
312 lkpi_kobj_attr_store(struct kobject *kobj, struct attribute *attr,
313     const char *buf, size_t count)
314 {
315 	struct kobj_attribute *ka =
316 	    container_of(attr, struct kobj_attribute, attr);
317 
318 	if (ka->store == NULL)
319 		return (-EIO);
320 
321 	return (ka->store(kobj, ka, buf, count));
322 }
323 
324 const struct sysfs_ops kobj_sysfs_ops = {
325 	.show	= lkpi_kobj_attr_show,
326 	.store	= lkpi_kobj_attr_store,
327 };
328 
329 static void
330 linux_device_release(struct device *dev)
331 {
332 	pr_debug("linux_device_release: %s\n", dev_name(dev));
333 	kfree(dev);
334 }
335 
336 static ssize_t
337 linux_class_show(struct kobject *kobj, struct attribute *attr, char *buf)
338 {
339 	struct class_attribute *dattr;
340 	ssize_t error;
341 
342 	dattr = container_of(attr, struct class_attribute, attr);
343 	error = -EIO;
344 	if (dattr->show)
345 		error = dattr->show(container_of(kobj, struct class, kobj),
346 		    dattr, buf);
347 	return (error);
348 }
349 
350 static ssize_t
351 linux_class_store(struct kobject *kobj, struct attribute *attr, const char *buf,
352     size_t count)
353 {
354 	struct class_attribute *dattr;
355 	ssize_t error;
356 
357 	dattr = container_of(attr, struct class_attribute, attr);
358 	error = -EIO;
359 	if (dattr->store)
360 		error = dattr->store(container_of(kobj, struct class, kobj),
361 		    dattr, buf, count);
362 	return (error);
363 }
364 
365 static void
366 linux_class_release(struct kobject *kobj)
367 {
368 	struct class *class;
369 
370 	class = container_of(kobj, struct class, kobj);
371 	if (class->class_release)
372 		class->class_release(class);
373 }
374 
375 static const struct sysfs_ops linux_class_sysfs = {
376 	.show  = linux_class_show,
377 	.store = linux_class_store,
378 };
379 
380 const struct kobj_type linux_class_ktype = {
381 	.release = linux_class_release,
382 	.sysfs_ops = &linux_class_sysfs
383 };
384 
385 static void
386 linux_dev_release(struct kobject *kobj)
387 {
388 	struct device *dev;
389 
390 	dev = container_of(kobj, struct device, kobj);
391 	/* This is the precedence defined by linux. */
392 	if (dev->release)
393 		dev->release(dev);
394 	else if (dev->class && dev->class->dev_release)
395 		dev->class->dev_release(dev);
396 }
397 
398 static ssize_t
399 linux_dev_show(struct kobject *kobj, struct attribute *attr, char *buf)
400 {
401 	struct device_attribute *dattr;
402 	ssize_t error;
403 
404 	dattr = container_of(attr, struct device_attribute, attr);
405 	error = -EIO;
406 	if (dattr->show)
407 		error = dattr->show(container_of(kobj, struct device, kobj),
408 		    dattr, buf);
409 	return (error);
410 }
411 
412 static ssize_t
413 linux_dev_store(struct kobject *kobj, struct attribute *attr, const char *buf,
414     size_t count)
415 {
416 	struct device_attribute *dattr;
417 	ssize_t error;
418 
419 	dattr = container_of(attr, struct device_attribute, attr);
420 	error = -EIO;
421 	if (dattr->store)
422 		error = dattr->store(container_of(kobj, struct device, kobj),
423 		    dattr, buf, count);
424 	return (error);
425 }
426 
427 static const struct sysfs_ops linux_dev_sysfs = {
428 	.show  = linux_dev_show,
429 	.store = linux_dev_store,
430 };
431 
432 const struct kobj_type linux_dev_ktype = {
433 	.release = linux_dev_release,
434 	.sysfs_ops = &linux_dev_sysfs
435 };
436 
437 struct device *
438 device_create(struct class *class, struct device *parent, dev_t devt,
439     void *drvdata, const char *fmt, ...)
440 {
441 	struct device *dev;
442 	va_list args;
443 
444 	dev = kzalloc(sizeof(*dev), M_WAITOK);
445 	dev->parent = parent;
446 	dev->class = class;
447 	dev->devt = devt;
448 	dev->driver_data = drvdata;
449 	dev->release = linux_device_release;
450 	va_start(args, fmt);
451 	kobject_set_name_vargs(&dev->kobj, fmt, args);
452 	va_end(args);
453 	device_register(dev);
454 
455 	return (dev);
456 }
457 
458 struct device *
459 device_create_groups_vargs(struct class *class, struct device *parent,
460     dev_t devt, void *drvdata, const struct attribute_group **groups,
461     const char *fmt, va_list args)
462 {
463 	struct device *dev = NULL;
464 	int retval = -ENODEV;
465 
466 	if (class == NULL || IS_ERR(class))
467 		goto error;
468 
469 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
470 	if (!dev) {
471 		retval = -ENOMEM;
472 		goto error;
473 	}
474 
475 	dev->devt = devt;
476 	dev->class = class;
477 	dev->parent = parent;
478 	dev->groups = groups;
479 	dev->release = device_create_release;
480 	/* device_initialize() needs the class and parent to be set */
481 	device_initialize(dev);
482 	dev_set_drvdata(dev, drvdata);
483 
484 	retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
485 	if (retval)
486 		goto error;
487 
488 	retval = device_add(dev);
489 	if (retval)
490 		goto error;
491 
492 	return dev;
493 
494 error:
495 	put_device(dev);
496 	return ERR_PTR(retval);
497 }
498 
499 struct class *
500 class_create(struct module *owner, const char *name)
501 {
502 	struct class *class;
503 	int error;
504 
505 	class = kzalloc(sizeof(*class), M_WAITOK);
506 	class->owner = owner;
507 	class->name = name;
508 	class->class_release = linux_class_kfree;
509 	error = class_register(class);
510 	if (error) {
511 		kfree(class);
512 		return (NULL);
513 	}
514 
515 	return (class);
516 }
517 
518 int
519 kobject_init_and_add(struct kobject *kobj, const struct kobj_type *ktype,
520     struct kobject *parent, const char *fmt, ...)
521 {
522 	va_list args;
523 	int error;
524 
525 	kobject_init(kobj, ktype);
526 	kobj->ktype = ktype;
527 	kobj->parent = parent;
528 	kobj->name = NULL;
529 
530 	va_start(args, fmt);
531 	error = kobject_set_name_vargs(kobj, fmt, args);
532 	va_end(args);
533 	if (error)
534 		return (error);
535 	return kobject_add_complete(kobj, parent);
536 }
537 
538 static void
539 linux_kq_lock(void *arg)
540 {
541 	spinlock_t *s = arg;
542 
543 	spin_lock(s);
544 }
545 static void
546 linux_kq_unlock(void *arg)
547 {
548 	spinlock_t *s = arg;
549 
550 	spin_unlock(s);
551 }
552 
553 static void
554 linux_kq_assert_lock(void *arg, int what)
555 {
556 #ifdef INVARIANTS
557 	spinlock_t *s = arg;
558 
559 	if (what == LA_LOCKED)
560 		mtx_assert(&s->m, MA_OWNED);
561 	else
562 		mtx_assert(&s->m, MA_NOTOWNED);
563 #endif
564 }
565 
566 static void
567 linux_file_kqfilter_poll(struct linux_file *, int);
568 
569 struct linux_file *
570 linux_file_alloc(void)
571 {
572 	struct linux_file *filp;
573 
574 	filp = kzalloc(sizeof(*filp), GFP_KERNEL);
575 
576 	/* set initial refcount */
577 	filp->f_count = 1;
578 
579 	/* setup fields needed by kqueue support */
580 	spin_lock_init(&filp->f_kqlock);
581 	knlist_init(&filp->f_selinfo.si_note, &filp->f_kqlock,
582 	    linux_kq_lock, linux_kq_unlock, linux_kq_assert_lock);
583 
584 	return (filp);
585 }
586 
587 void
588 linux_file_free(struct linux_file *filp)
589 {
590 	if (filp->_file == NULL) {
591 		if (filp->f_op != NULL && filp->f_op->release != NULL)
592 			filp->f_op->release(filp->f_vnode, filp);
593 		if (filp->f_shmem != NULL)
594 			vm_object_deallocate(filp->f_shmem);
595 		kfree_rcu(filp, rcu);
596 	} else {
597 		/*
598 		 * The close method of the character device or file
599 		 * will free the linux_file structure:
600 		 */
601 		_fdrop(filp->_file, curthread);
602 	}
603 }
604 
605 static int
606 linux_cdev_pager_fault(vm_object_t vm_obj, vm_ooffset_t offset, int prot,
607     vm_page_t *mres)
608 {
609 	struct vm_area_struct *vmap;
610 
611 	vmap = linux_cdev_handle_find(vm_obj->handle);
612 
613 	MPASS(vmap != NULL);
614 	MPASS(vmap->vm_private_data == vm_obj->handle);
615 
616 	if (likely(vmap->vm_ops != NULL && offset < vmap->vm_len)) {
617 		vm_paddr_t paddr = IDX_TO_OFF(vmap->vm_pfn) + offset;
618 		vm_page_t page;
619 
620 		if (((*mres)->flags & PG_FICTITIOUS) != 0) {
621 			/*
622 			 * If the passed in result page is a fake
623 			 * page, update it with the new physical
624 			 * address.
625 			 */
626 			page = *mres;
627 			vm_page_updatefake(page, paddr, vm_obj->memattr);
628 		} else {
629 			/*
630 			 * Replace the passed in "mres" page with our
631 			 * own fake page and free up the all of the
632 			 * original pages.
633 			 */
634 			VM_OBJECT_WUNLOCK(vm_obj);
635 			page = vm_page_getfake(paddr, vm_obj->memattr);
636 			VM_OBJECT_WLOCK(vm_obj);
637 
638 			vm_page_replace(page, vm_obj, (*mres)->pindex, *mres);
639 			*mres = page;
640 		}
641 		vm_page_valid(page);
642 		return (VM_PAGER_OK);
643 	}
644 	return (VM_PAGER_FAIL);
645 }
646 
647 static int
648 linux_cdev_pager_populate(vm_object_t vm_obj, vm_pindex_t pidx, int fault_type,
649     vm_prot_t max_prot, vm_pindex_t *first, vm_pindex_t *last)
650 {
651 	struct vm_area_struct *vmap;
652 	int err;
653 
654 	/* get VM area structure */
655 	vmap = linux_cdev_handle_find(vm_obj->handle);
656 	MPASS(vmap != NULL);
657 	MPASS(vmap->vm_private_data == vm_obj->handle);
658 
659 	VM_OBJECT_WUNLOCK(vm_obj);
660 
661 	linux_set_current(curthread);
662 
663 	down_write(&vmap->vm_mm->mmap_sem);
664 	if (unlikely(vmap->vm_ops == NULL)) {
665 		err = VM_FAULT_SIGBUS;
666 	} else {
667 		struct vm_fault vmf;
668 
669 		/* fill out VM fault structure */
670 		vmf.virtual_address = (void *)(uintptr_t)IDX_TO_OFF(pidx);
671 		vmf.flags = (fault_type & VM_PROT_WRITE) ? FAULT_FLAG_WRITE : 0;
672 		vmf.pgoff = 0;
673 		vmf.page = NULL;
674 		vmf.vma = vmap;
675 
676 		vmap->vm_pfn_count = 0;
677 		vmap->vm_pfn_pcount = &vmap->vm_pfn_count;
678 		vmap->vm_obj = vm_obj;
679 
680 		err = vmap->vm_ops->fault(&vmf);
681 
682 		while (vmap->vm_pfn_count == 0 && err == VM_FAULT_NOPAGE) {
683 			kern_yield(PRI_USER);
684 			err = vmap->vm_ops->fault(&vmf);
685 		}
686 	}
687 
688 	/* translate return code */
689 	switch (err) {
690 	case VM_FAULT_OOM:
691 		err = VM_PAGER_AGAIN;
692 		break;
693 	case VM_FAULT_SIGBUS:
694 		err = VM_PAGER_BAD;
695 		break;
696 	case VM_FAULT_NOPAGE:
697 		/*
698 		 * By contract the fault handler will return having
699 		 * busied all the pages itself. If pidx is already
700 		 * found in the object, it will simply xbusy the first
701 		 * page and return with vm_pfn_count set to 1.
702 		 */
703 		*first = vmap->vm_pfn_first;
704 		*last = *first + vmap->vm_pfn_count - 1;
705 		err = VM_PAGER_OK;
706 		break;
707 	default:
708 		err = VM_PAGER_ERROR;
709 		break;
710 	}
711 	up_write(&vmap->vm_mm->mmap_sem);
712 	VM_OBJECT_WLOCK(vm_obj);
713 	return (err);
714 }
715 
716 static struct rwlock linux_vma_lock;
717 static TAILQ_HEAD(, vm_area_struct) linux_vma_head =
718     TAILQ_HEAD_INITIALIZER(linux_vma_head);
719 
720 static void
721 linux_cdev_handle_free(struct vm_area_struct *vmap)
722 {
723 	/* Drop reference on vm_file */
724 	if (vmap->vm_file != NULL)
725 		fput(vmap->vm_file);
726 
727 	/* Drop reference on mm_struct */
728 	mmput(vmap->vm_mm);
729 
730 	kfree(vmap);
731 }
732 
733 static void
734 linux_cdev_handle_remove(struct vm_area_struct *vmap)
735 {
736 	rw_wlock(&linux_vma_lock);
737 	TAILQ_REMOVE(&linux_vma_head, vmap, vm_entry);
738 	rw_wunlock(&linux_vma_lock);
739 }
740 
741 static struct vm_area_struct *
742 linux_cdev_handle_find(void *handle)
743 {
744 	struct vm_area_struct *vmap;
745 
746 	rw_rlock(&linux_vma_lock);
747 	TAILQ_FOREACH(vmap, &linux_vma_head, vm_entry) {
748 		if (vmap->vm_private_data == handle)
749 			break;
750 	}
751 	rw_runlock(&linux_vma_lock);
752 	return (vmap);
753 }
754 
755 static int
756 linux_cdev_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
757 		      vm_ooffset_t foff, struct ucred *cred, u_short *color)
758 {
759 
760 	MPASS(linux_cdev_handle_find(handle) != NULL);
761 	*color = 0;
762 	return (0);
763 }
764 
765 static void
766 linux_cdev_pager_dtor(void *handle)
767 {
768 	const struct vm_operations_struct *vm_ops;
769 	struct vm_area_struct *vmap;
770 
771 	vmap = linux_cdev_handle_find(handle);
772 	MPASS(vmap != NULL);
773 
774 	/*
775 	 * Remove handle before calling close operation to prevent
776 	 * other threads from reusing the handle pointer.
777 	 */
778 	linux_cdev_handle_remove(vmap);
779 
780 	down_write(&vmap->vm_mm->mmap_sem);
781 	vm_ops = vmap->vm_ops;
782 	if (likely(vm_ops != NULL))
783 		vm_ops->close(vmap);
784 	up_write(&vmap->vm_mm->mmap_sem);
785 
786 	linux_cdev_handle_free(vmap);
787 }
788 
789 static struct cdev_pager_ops linux_cdev_pager_ops[2] = {
790   {
791 	/* OBJT_MGTDEVICE */
792 	.cdev_pg_populate	= linux_cdev_pager_populate,
793 	.cdev_pg_ctor	= linux_cdev_pager_ctor,
794 	.cdev_pg_dtor	= linux_cdev_pager_dtor
795   },
796   {
797 	/* OBJT_DEVICE */
798 	.cdev_pg_fault	= linux_cdev_pager_fault,
799 	.cdev_pg_ctor	= linux_cdev_pager_ctor,
800 	.cdev_pg_dtor	= linux_cdev_pager_dtor
801   },
802 };
803 
804 int
805 zap_vma_ptes(struct vm_area_struct *vma, unsigned long address,
806     unsigned long size)
807 {
808 	vm_object_t obj;
809 	vm_page_t m;
810 
811 	obj = vma->vm_obj;
812 	if (obj == NULL || (obj->flags & OBJ_UNMANAGED) != 0)
813 		return (-ENOTSUP);
814 	VM_OBJECT_RLOCK(obj);
815 	for (m = vm_page_find_least(obj, OFF_TO_IDX(address));
816 	    m != NULL && m->pindex < OFF_TO_IDX(address + size);
817 	    m = TAILQ_NEXT(m, listq))
818 		pmap_remove_all(m);
819 	VM_OBJECT_RUNLOCK(obj);
820 	return (0);
821 }
822 
823 static struct file_operations dummy_ldev_ops = {
824 	/* XXXKIB */
825 };
826 
827 static struct linux_cdev dummy_ldev = {
828 	.ops = &dummy_ldev_ops,
829 };
830 
831 #define	LDEV_SI_DTR	0x0001
832 #define	LDEV_SI_REF	0x0002
833 
834 static void
835 linux_get_fop(struct linux_file *filp, const struct file_operations **fop,
836     struct linux_cdev **dev)
837 {
838 	struct linux_cdev *ldev;
839 	u_int siref;
840 
841 	ldev = filp->f_cdev;
842 	*fop = filp->f_op;
843 	if (ldev != NULL) {
844 		if (ldev->kobj.ktype == &linux_cdev_static_ktype) {
845 			refcount_acquire(&ldev->refs);
846 		} else {
847 			for (siref = ldev->siref;;) {
848 				if ((siref & LDEV_SI_DTR) != 0) {
849 					ldev = &dummy_ldev;
850 					*fop = ldev->ops;
851 					siref = ldev->siref;
852 					MPASS((ldev->siref & LDEV_SI_DTR) == 0);
853 				} else if (atomic_fcmpset_int(&ldev->siref,
854 				    &siref, siref + LDEV_SI_REF)) {
855 					break;
856 				}
857 			}
858 		}
859 	}
860 	*dev = ldev;
861 }
862 
863 static void
864 linux_drop_fop(struct linux_cdev *ldev)
865 {
866 
867 	if (ldev == NULL)
868 		return;
869 	if (ldev->kobj.ktype == &linux_cdev_static_ktype) {
870 		linux_cdev_deref(ldev);
871 	} else {
872 		MPASS(ldev->kobj.ktype == &linux_cdev_ktype);
873 		MPASS((ldev->siref & ~LDEV_SI_DTR) != 0);
874 		atomic_subtract_int(&ldev->siref, LDEV_SI_REF);
875 	}
876 }
877 
878 #define	OPW(fp,td,code) ({			\
879 	struct file *__fpop;			\
880 	__typeof(code) __retval;		\
881 						\
882 	__fpop = (td)->td_fpop;			\
883 	(td)->td_fpop = (fp);			\
884 	__retval = (code);			\
885 	(td)->td_fpop = __fpop;			\
886 	__retval;				\
887 })
888 
889 static int
890 linux_dev_fdopen(struct cdev *dev, int fflags, struct thread *td,
891     struct file *file)
892 {
893 	struct linux_cdev *ldev;
894 	struct linux_file *filp;
895 	const struct file_operations *fop;
896 	int error;
897 
898 	ldev = dev->si_drv1;
899 
900 	filp = linux_file_alloc();
901 	filp->f_dentry = &filp->f_dentry_store;
902 	filp->f_op = ldev->ops;
903 	filp->f_mode = file->f_flag;
904 	filp->f_flags = file->f_flag;
905 	filp->f_vnode = file->f_vnode;
906 	filp->_file = file;
907 	refcount_acquire(&ldev->refs);
908 	filp->f_cdev = ldev;
909 
910 	linux_set_current(td);
911 	linux_get_fop(filp, &fop, &ldev);
912 
913 	if (fop->open != NULL) {
914 		error = -fop->open(file->f_vnode, filp);
915 		if (error != 0) {
916 			linux_drop_fop(ldev);
917 			linux_cdev_deref(filp->f_cdev);
918 			kfree(filp);
919 			return (error);
920 		}
921 	}
922 
923 	/* hold on to the vnode - used for fstat() */
924 	vhold(filp->f_vnode);
925 
926 	/* release the file from devfs */
927 	finit(file, filp->f_mode, DTYPE_DEV, filp, &linuxfileops);
928 	linux_drop_fop(ldev);
929 	return (ENXIO);
930 }
931 
932 #define	LINUX_IOCTL_MIN_PTR 0x10000UL
933 #define	LINUX_IOCTL_MAX_PTR (LINUX_IOCTL_MIN_PTR + IOCPARM_MAX)
934 
935 static inline int
936 linux_remap_address(void **uaddr, size_t len)
937 {
938 	uintptr_t uaddr_val = (uintptr_t)(*uaddr);
939 
940 	if (unlikely(uaddr_val >= LINUX_IOCTL_MIN_PTR &&
941 	    uaddr_val < LINUX_IOCTL_MAX_PTR)) {
942 		struct task_struct *pts = current;
943 		if (pts == NULL) {
944 			*uaddr = NULL;
945 			return (1);
946 		}
947 
948 		/* compute data offset */
949 		uaddr_val -= LINUX_IOCTL_MIN_PTR;
950 
951 		/* check that length is within bounds */
952 		if ((len > IOCPARM_MAX) ||
953 		    (uaddr_val + len) > pts->bsd_ioctl_len) {
954 			*uaddr = NULL;
955 			return (1);
956 		}
957 
958 		/* re-add kernel buffer address */
959 		uaddr_val += (uintptr_t)pts->bsd_ioctl_data;
960 
961 		/* update address location */
962 		*uaddr = (void *)uaddr_val;
963 		return (1);
964 	}
965 	return (0);
966 }
967 
968 int
969 linux_copyin(const void *uaddr, void *kaddr, size_t len)
970 {
971 	if (linux_remap_address(__DECONST(void **, &uaddr), len)) {
972 		if (uaddr == NULL)
973 			return (-EFAULT);
974 		memcpy(kaddr, uaddr, len);
975 		return (0);
976 	}
977 	return (-copyin(uaddr, kaddr, len));
978 }
979 
980 int
981 linux_copyout(const void *kaddr, void *uaddr, size_t len)
982 {
983 	if (linux_remap_address(&uaddr, len)) {
984 		if (uaddr == NULL)
985 			return (-EFAULT);
986 		memcpy(uaddr, kaddr, len);
987 		return (0);
988 	}
989 	return (-copyout(kaddr, uaddr, len));
990 }
991 
992 size_t
993 linux_clear_user(void *_uaddr, size_t _len)
994 {
995 	uint8_t *uaddr = _uaddr;
996 	size_t len = _len;
997 
998 	/* make sure uaddr is aligned before going into the fast loop */
999 	while (((uintptr_t)uaddr & 7) != 0 && len > 7) {
1000 		if (subyte(uaddr, 0))
1001 			return (_len);
1002 		uaddr++;
1003 		len--;
1004 	}
1005 
1006 	/* zero 8 bytes at a time */
1007 	while (len > 7) {
1008 #ifdef __LP64__
1009 		if (suword64(uaddr, 0))
1010 			return (_len);
1011 #else
1012 		if (suword32(uaddr, 0))
1013 			return (_len);
1014 		if (suword32(uaddr + 4, 0))
1015 			return (_len);
1016 #endif
1017 		uaddr += 8;
1018 		len -= 8;
1019 	}
1020 
1021 	/* zero fill end, if any */
1022 	while (len > 0) {
1023 		if (subyte(uaddr, 0))
1024 			return (_len);
1025 		uaddr++;
1026 		len--;
1027 	}
1028 	return (0);
1029 }
1030 
1031 int
1032 linux_access_ok(const void *uaddr, size_t len)
1033 {
1034 	uintptr_t saddr;
1035 	uintptr_t eaddr;
1036 
1037 	/* get start and end address */
1038 	saddr = (uintptr_t)uaddr;
1039 	eaddr = (uintptr_t)uaddr + len;
1040 
1041 	/* verify addresses are valid for userspace */
1042 	return ((saddr == eaddr) ||
1043 	    (eaddr > saddr && eaddr <= VM_MAXUSER_ADDRESS));
1044 }
1045 
1046 /*
1047  * This function should return either EINTR or ERESTART depending on
1048  * the signal type sent to this thread:
1049  */
1050 static int
1051 linux_get_error(struct task_struct *task, int error)
1052 {
1053 	/* check for signal type interrupt code */
1054 	if (error == EINTR || error == ERESTARTSYS || error == ERESTART) {
1055 		error = -linux_schedule_get_interrupt_value(task);
1056 		if (error == 0)
1057 			error = EINTR;
1058 	}
1059 	return (error);
1060 }
1061 
1062 static int
1063 linux_file_ioctl_sub(struct file *fp, struct linux_file *filp,
1064     const struct file_operations *fop, u_long cmd, caddr_t data,
1065     struct thread *td)
1066 {
1067 	struct task_struct *task = current;
1068 	unsigned size;
1069 	int error;
1070 
1071 	size = IOCPARM_LEN(cmd);
1072 	/* refer to logic in sys_ioctl() */
1073 	if (size > 0) {
1074 		/*
1075 		 * Setup hint for linux_copyin() and linux_copyout().
1076 		 *
1077 		 * Background: Linux code expects a user-space address
1078 		 * while FreeBSD supplies a kernel-space address.
1079 		 */
1080 		task->bsd_ioctl_data = data;
1081 		task->bsd_ioctl_len = size;
1082 		data = (void *)LINUX_IOCTL_MIN_PTR;
1083 	} else {
1084 		/* fetch user-space pointer */
1085 		data = *(void **)data;
1086 	}
1087 #ifdef COMPAT_FREEBSD32
1088 	if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
1089 		/* try the compat IOCTL handler first */
1090 		if (fop->compat_ioctl != NULL) {
1091 			error = -OPW(fp, td, fop->compat_ioctl(filp,
1092 			    cmd, (u_long)data));
1093 		} else {
1094 			error = ENOTTY;
1095 		}
1096 
1097 		/* fallback to the regular IOCTL handler, if any */
1098 		if (error == ENOTTY && fop->unlocked_ioctl != NULL) {
1099 			error = -OPW(fp, td, fop->unlocked_ioctl(filp,
1100 			    cmd, (u_long)data));
1101 		}
1102 	} else
1103 #endif
1104 	{
1105 		if (fop->unlocked_ioctl != NULL) {
1106 			error = -OPW(fp, td, fop->unlocked_ioctl(filp,
1107 			    cmd, (u_long)data));
1108 		} else {
1109 			error = ENOTTY;
1110 		}
1111 	}
1112 	if (size > 0) {
1113 		task->bsd_ioctl_data = NULL;
1114 		task->bsd_ioctl_len = 0;
1115 	}
1116 
1117 	if (error == EWOULDBLOCK) {
1118 		/* update kqfilter status, if any */
1119 		linux_file_kqfilter_poll(filp,
1120 		    LINUX_KQ_FLAG_HAS_READ | LINUX_KQ_FLAG_HAS_WRITE);
1121 	} else {
1122 		error = linux_get_error(task, error);
1123 	}
1124 	return (error);
1125 }
1126 
1127 #define	LINUX_POLL_TABLE_NORMAL ((poll_table *)1)
1128 
1129 /*
1130  * This function atomically updates the poll wakeup state and returns
1131  * the previous state at the time of update.
1132  */
1133 static uint8_t
1134 linux_poll_wakeup_state(atomic_t *v, const uint8_t *pstate)
1135 {
1136 	int c, old;
1137 
1138 	c = v->counter;
1139 
1140 	while ((old = atomic_cmpxchg(v, c, pstate[c])) != c)
1141 		c = old;
1142 
1143 	return (c);
1144 }
1145 
1146 static int
1147 linux_poll_wakeup_callback(wait_queue_t *wq, unsigned int wq_state, int flags, void *key)
1148 {
1149 	static const uint8_t state[LINUX_FWQ_STATE_MAX] = {
1150 		[LINUX_FWQ_STATE_INIT] = LINUX_FWQ_STATE_INIT, /* NOP */
1151 		[LINUX_FWQ_STATE_NOT_READY] = LINUX_FWQ_STATE_NOT_READY, /* NOP */
1152 		[LINUX_FWQ_STATE_QUEUED] = LINUX_FWQ_STATE_READY,
1153 		[LINUX_FWQ_STATE_READY] = LINUX_FWQ_STATE_READY, /* NOP */
1154 	};
1155 	struct linux_file *filp = container_of(wq, struct linux_file, f_wait_queue.wq);
1156 
1157 	switch (linux_poll_wakeup_state(&filp->f_wait_queue.state, state)) {
1158 	case LINUX_FWQ_STATE_QUEUED:
1159 		linux_poll_wakeup(filp);
1160 		return (1);
1161 	default:
1162 		return (0);
1163 	}
1164 }
1165 
1166 void
1167 linux_poll_wait(struct linux_file *filp, wait_queue_head_t *wqh, poll_table *p)
1168 {
1169 	static const uint8_t state[LINUX_FWQ_STATE_MAX] = {
1170 		[LINUX_FWQ_STATE_INIT] = LINUX_FWQ_STATE_NOT_READY,
1171 		[LINUX_FWQ_STATE_NOT_READY] = LINUX_FWQ_STATE_NOT_READY, /* NOP */
1172 		[LINUX_FWQ_STATE_QUEUED] = LINUX_FWQ_STATE_QUEUED, /* NOP */
1173 		[LINUX_FWQ_STATE_READY] = LINUX_FWQ_STATE_QUEUED,
1174 	};
1175 
1176 	/* check if we are called inside the select system call */
1177 	if (p == LINUX_POLL_TABLE_NORMAL)
1178 		selrecord(curthread, &filp->f_selinfo);
1179 
1180 	switch (linux_poll_wakeup_state(&filp->f_wait_queue.state, state)) {
1181 	case LINUX_FWQ_STATE_INIT:
1182 		/* NOTE: file handles can only belong to one wait-queue */
1183 		filp->f_wait_queue.wqh = wqh;
1184 		filp->f_wait_queue.wq.func = &linux_poll_wakeup_callback;
1185 		add_wait_queue(wqh, &filp->f_wait_queue.wq);
1186 		atomic_set(&filp->f_wait_queue.state, LINUX_FWQ_STATE_QUEUED);
1187 		break;
1188 	default:
1189 		break;
1190 	}
1191 }
1192 
1193 static void
1194 linux_poll_wait_dequeue(struct linux_file *filp)
1195 {
1196 	static const uint8_t state[LINUX_FWQ_STATE_MAX] = {
1197 		[LINUX_FWQ_STATE_INIT] = LINUX_FWQ_STATE_INIT,	/* NOP */
1198 		[LINUX_FWQ_STATE_NOT_READY] = LINUX_FWQ_STATE_INIT,
1199 		[LINUX_FWQ_STATE_QUEUED] = LINUX_FWQ_STATE_INIT,
1200 		[LINUX_FWQ_STATE_READY] = LINUX_FWQ_STATE_INIT,
1201 	};
1202 
1203 	seldrain(&filp->f_selinfo);
1204 
1205 	switch (linux_poll_wakeup_state(&filp->f_wait_queue.state, state)) {
1206 	case LINUX_FWQ_STATE_NOT_READY:
1207 	case LINUX_FWQ_STATE_QUEUED:
1208 	case LINUX_FWQ_STATE_READY:
1209 		remove_wait_queue(filp->f_wait_queue.wqh, &filp->f_wait_queue.wq);
1210 		break;
1211 	default:
1212 		break;
1213 	}
1214 }
1215 
1216 void
1217 linux_poll_wakeup(struct linux_file *filp)
1218 {
1219 	/* this function should be NULL-safe */
1220 	if (filp == NULL)
1221 		return;
1222 
1223 	selwakeup(&filp->f_selinfo);
1224 
1225 	spin_lock(&filp->f_kqlock);
1226 	filp->f_kqflags |= LINUX_KQ_FLAG_NEED_READ |
1227 	    LINUX_KQ_FLAG_NEED_WRITE;
1228 
1229 	/* make sure the "knote" gets woken up */
1230 	KNOTE_LOCKED(&filp->f_selinfo.si_note, 1);
1231 	spin_unlock(&filp->f_kqlock);
1232 }
1233 
1234 static void
1235 linux_file_kqfilter_detach(struct knote *kn)
1236 {
1237 	struct linux_file *filp = kn->kn_hook;
1238 
1239 	spin_lock(&filp->f_kqlock);
1240 	knlist_remove(&filp->f_selinfo.si_note, kn, 1);
1241 	spin_unlock(&filp->f_kqlock);
1242 }
1243 
1244 static int
1245 linux_file_kqfilter_read_event(struct knote *kn, long hint)
1246 {
1247 	struct linux_file *filp = kn->kn_hook;
1248 
1249 	mtx_assert(&filp->f_kqlock.m, MA_OWNED);
1250 
1251 	return ((filp->f_kqflags & LINUX_KQ_FLAG_NEED_READ) ? 1 : 0);
1252 }
1253 
1254 static int
1255 linux_file_kqfilter_write_event(struct knote *kn, long hint)
1256 {
1257 	struct linux_file *filp = kn->kn_hook;
1258 
1259 	mtx_assert(&filp->f_kqlock.m, MA_OWNED);
1260 
1261 	return ((filp->f_kqflags & LINUX_KQ_FLAG_NEED_WRITE) ? 1 : 0);
1262 }
1263 
1264 static struct filterops linux_dev_kqfiltops_read = {
1265 	.f_isfd = 1,
1266 	.f_detach = linux_file_kqfilter_detach,
1267 	.f_event = linux_file_kqfilter_read_event,
1268 };
1269 
1270 static struct filterops linux_dev_kqfiltops_write = {
1271 	.f_isfd = 1,
1272 	.f_detach = linux_file_kqfilter_detach,
1273 	.f_event = linux_file_kqfilter_write_event,
1274 };
1275 
1276 static void
1277 linux_file_kqfilter_poll(struct linux_file *filp, int kqflags)
1278 {
1279 	struct thread *td;
1280 	const struct file_operations *fop;
1281 	struct linux_cdev *ldev;
1282 	int temp;
1283 
1284 	if ((filp->f_kqflags & kqflags) == 0)
1285 		return;
1286 
1287 	td = curthread;
1288 
1289 	linux_get_fop(filp, &fop, &ldev);
1290 	/* get the latest polling state */
1291 	temp = OPW(filp->_file, td, fop->poll(filp, NULL));
1292 	linux_drop_fop(ldev);
1293 
1294 	spin_lock(&filp->f_kqlock);
1295 	/* clear kqflags */
1296 	filp->f_kqflags &= ~(LINUX_KQ_FLAG_NEED_READ |
1297 	    LINUX_KQ_FLAG_NEED_WRITE);
1298 	/* update kqflags */
1299 	if ((temp & (POLLIN | POLLOUT)) != 0) {
1300 		if ((temp & POLLIN) != 0)
1301 			filp->f_kqflags |= LINUX_KQ_FLAG_NEED_READ;
1302 		if ((temp & POLLOUT) != 0)
1303 			filp->f_kqflags |= LINUX_KQ_FLAG_NEED_WRITE;
1304 
1305 		/* make sure the "knote" gets woken up */
1306 		KNOTE_LOCKED(&filp->f_selinfo.si_note, 0);
1307 	}
1308 	spin_unlock(&filp->f_kqlock);
1309 }
1310 
1311 static int
1312 linux_file_kqfilter(struct file *file, struct knote *kn)
1313 {
1314 	struct linux_file *filp;
1315 	struct thread *td;
1316 	int error;
1317 
1318 	td = curthread;
1319 	filp = (struct linux_file *)file->f_data;
1320 	filp->f_flags = file->f_flag;
1321 	if (filp->f_op->poll == NULL)
1322 		return (EINVAL);
1323 
1324 	spin_lock(&filp->f_kqlock);
1325 	switch (kn->kn_filter) {
1326 	case EVFILT_READ:
1327 		filp->f_kqflags |= LINUX_KQ_FLAG_HAS_READ;
1328 		kn->kn_fop = &linux_dev_kqfiltops_read;
1329 		kn->kn_hook = filp;
1330 		knlist_add(&filp->f_selinfo.si_note, kn, 1);
1331 		error = 0;
1332 		break;
1333 	case EVFILT_WRITE:
1334 		filp->f_kqflags |= LINUX_KQ_FLAG_HAS_WRITE;
1335 		kn->kn_fop = &linux_dev_kqfiltops_write;
1336 		kn->kn_hook = filp;
1337 		knlist_add(&filp->f_selinfo.si_note, kn, 1);
1338 		error = 0;
1339 		break;
1340 	default:
1341 		error = EINVAL;
1342 		break;
1343 	}
1344 	spin_unlock(&filp->f_kqlock);
1345 
1346 	if (error == 0) {
1347 		linux_set_current(td);
1348 
1349 		/* update kqfilter status, if any */
1350 		linux_file_kqfilter_poll(filp,
1351 		    LINUX_KQ_FLAG_HAS_READ | LINUX_KQ_FLAG_HAS_WRITE);
1352 	}
1353 	return (error);
1354 }
1355 
1356 static int
1357 linux_file_mmap_single(struct file *fp, const struct file_operations *fop,
1358     vm_ooffset_t *offset, vm_size_t size, struct vm_object **object,
1359     int nprot, bool is_shared, struct thread *td)
1360 {
1361 	struct task_struct *task;
1362 	struct vm_area_struct *vmap;
1363 	struct mm_struct *mm;
1364 	struct linux_file *filp;
1365 	vm_memattr_t attr;
1366 	int error;
1367 
1368 	filp = (struct linux_file *)fp->f_data;
1369 	filp->f_flags = fp->f_flag;
1370 
1371 	if (fop->mmap == NULL)
1372 		return (EOPNOTSUPP);
1373 
1374 	linux_set_current(td);
1375 
1376 	/*
1377 	 * The same VM object might be shared by multiple processes
1378 	 * and the mm_struct is usually freed when a process exits.
1379 	 *
1380 	 * The atomic reference below makes sure the mm_struct is
1381 	 * available as long as the vmap is in the linux_vma_head.
1382 	 */
1383 	task = current;
1384 	mm = task->mm;
1385 	if (atomic_inc_not_zero(&mm->mm_users) == 0)
1386 		return (EINVAL);
1387 
1388 	vmap = kzalloc(sizeof(*vmap), GFP_KERNEL);
1389 	vmap->vm_start = 0;
1390 	vmap->vm_end = size;
1391 	vmap->vm_pgoff = *offset / PAGE_SIZE;
1392 	vmap->vm_pfn = 0;
1393 	vmap->vm_flags = vmap->vm_page_prot = (nprot & VM_PROT_ALL);
1394 	if (is_shared)
1395 		vmap->vm_flags |= VM_SHARED;
1396 	vmap->vm_ops = NULL;
1397 	vmap->vm_file = get_file(filp);
1398 	vmap->vm_mm = mm;
1399 
1400 	if (unlikely(down_write_killable(&vmap->vm_mm->mmap_sem))) {
1401 		error = linux_get_error(task, EINTR);
1402 	} else {
1403 		error = -OPW(fp, td, fop->mmap(filp, vmap));
1404 		error = linux_get_error(task, error);
1405 		up_write(&vmap->vm_mm->mmap_sem);
1406 	}
1407 
1408 	if (error != 0) {
1409 		linux_cdev_handle_free(vmap);
1410 		return (error);
1411 	}
1412 
1413 	attr = pgprot2cachemode(vmap->vm_page_prot);
1414 
1415 	if (vmap->vm_ops != NULL) {
1416 		struct vm_area_struct *ptr;
1417 		void *vm_private_data;
1418 		bool vm_no_fault;
1419 
1420 		if (vmap->vm_ops->open == NULL ||
1421 		    vmap->vm_ops->close == NULL ||
1422 		    vmap->vm_private_data == NULL) {
1423 			/* free allocated VM area struct */
1424 			linux_cdev_handle_free(vmap);
1425 			return (EINVAL);
1426 		}
1427 
1428 		vm_private_data = vmap->vm_private_data;
1429 
1430 		rw_wlock(&linux_vma_lock);
1431 		TAILQ_FOREACH(ptr, &linux_vma_head, vm_entry) {
1432 			if (ptr->vm_private_data == vm_private_data)
1433 				break;
1434 		}
1435 		/* check if there is an existing VM area struct */
1436 		if (ptr != NULL) {
1437 			/* check if the VM area structure is invalid */
1438 			if (ptr->vm_ops == NULL ||
1439 			    ptr->vm_ops->open == NULL ||
1440 			    ptr->vm_ops->close == NULL) {
1441 				error = ESTALE;
1442 				vm_no_fault = 1;
1443 			} else {
1444 				error = EEXIST;
1445 				vm_no_fault = (ptr->vm_ops->fault == NULL);
1446 			}
1447 		} else {
1448 			/* insert VM area structure into list */
1449 			TAILQ_INSERT_TAIL(&linux_vma_head, vmap, vm_entry);
1450 			error = 0;
1451 			vm_no_fault = (vmap->vm_ops->fault == NULL);
1452 		}
1453 		rw_wunlock(&linux_vma_lock);
1454 
1455 		if (error != 0) {
1456 			/* free allocated VM area struct */
1457 			linux_cdev_handle_free(vmap);
1458 			/* check for stale VM area struct */
1459 			if (error != EEXIST)
1460 				return (error);
1461 		}
1462 
1463 		/* check if there is no fault handler */
1464 		if (vm_no_fault) {
1465 			*object = cdev_pager_allocate(vm_private_data, OBJT_DEVICE,
1466 			    &linux_cdev_pager_ops[1], size, nprot, *offset,
1467 			    td->td_ucred);
1468 		} else {
1469 			*object = cdev_pager_allocate(vm_private_data, OBJT_MGTDEVICE,
1470 			    &linux_cdev_pager_ops[0], size, nprot, *offset,
1471 			    td->td_ucred);
1472 		}
1473 
1474 		/* check if allocating the VM object failed */
1475 		if (*object == NULL) {
1476 			if (error == 0) {
1477 				/* remove VM area struct from list */
1478 				linux_cdev_handle_remove(vmap);
1479 				/* free allocated VM area struct */
1480 				linux_cdev_handle_free(vmap);
1481 			}
1482 			return (EINVAL);
1483 		}
1484 	} else {
1485 		struct sglist *sg;
1486 
1487 		sg = sglist_alloc(1, M_WAITOK);
1488 		sglist_append_phys(sg,
1489 		    (vm_paddr_t)vmap->vm_pfn << PAGE_SHIFT, vmap->vm_len);
1490 
1491 		*object = vm_pager_allocate(OBJT_SG, sg, vmap->vm_len,
1492 		    nprot, 0, td->td_ucred);
1493 
1494 		linux_cdev_handle_free(vmap);
1495 
1496 		if (*object == NULL) {
1497 			sglist_free(sg);
1498 			return (EINVAL);
1499 		}
1500 	}
1501 
1502 	if (attr != VM_MEMATTR_DEFAULT) {
1503 		VM_OBJECT_WLOCK(*object);
1504 		vm_object_set_memattr(*object, attr);
1505 		VM_OBJECT_WUNLOCK(*object);
1506 	}
1507 	*offset = 0;
1508 	return (0);
1509 }
1510 
1511 struct cdevsw linuxcdevsw = {
1512 	.d_version = D_VERSION,
1513 	.d_fdopen = linux_dev_fdopen,
1514 	.d_name = "lkpidev",
1515 };
1516 
1517 static int
1518 linux_file_read(struct file *file, struct uio *uio, struct ucred *active_cred,
1519     int flags, struct thread *td)
1520 {
1521 	struct linux_file *filp;
1522 	const struct file_operations *fop;
1523 	struct linux_cdev *ldev;
1524 	ssize_t bytes;
1525 	int error;
1526 
1527 	error = 0;
1528 	filp = (struct linux_file *)file->f_data;
1529 	filp->f_flags = file->f_flag;
1530 	/* XXX no support for I/O vectors currently */
1531 	if (uio->uio_iovcnt != 1)
1532 		return (EOPNOTSUPP);
1533 	if (uio->uio_resid > DEVFS_IOSIZE_MAX)
1534 		return (EINVAL);
1535 	linux_set_current(td);
1536 	linux_get_fop(filp, &fop, &ldev);
1537 	if (fop->read != NULL) {
1538 		bytes = OPW(file, td, fop->read(filp,
1539 		    uio->uio_iov->iov_base,
1540 		    uio->uio_iov->iov_len, &uio->uio_offset));
1541 		if (bytes >= 0) {
1542 			uio->uio_iov->iov_base =
1543 			    ((uint8_t *)uio->uio_iov->iov_base) + bytes;
1544 			uio->uio_iov->iov_len -= bytes;
1545 			uio->uio_resid -= bytes;
1546 		} else {
1547 			error = linux_get_error(current, -bytes);
1548 		}
1549 	} else
1550 		error = ENXIO;
1551 
1552 	/* update kqfilter status, if any */
1553 	linux_file_kqfilter_poll(filp, LINUX_KQ_FLAG_HAS_READ);
1554 	linux_drop_fop(ldev);
1555 
1556 	return (error);
1557 }
1558 
1559 static int
1560 linux_file_write(struct file *file, struct uio *uio, struct ucred *active_cred,
1561     int flags, struct thread *td)
1562 {
1563 	struct linux_file *filp;
1564 	const struct file_operations *fop;
1565 	struct linux_cdev *ldev;
1566 	ssize_t bytes;
1567 	int error;
1568 
1569 	filp = (struct linux_file *)file->f_data;
1570 	filp->f_flags = file->f_flag;
1571 	/* XXX no support for I/O vectors currently */
1572 	if (uio->uio_iovcnt != 1)
1573 		return (EOPNOTSUPP);
1574 	if (uio->uio_resid > DEVFS_IOSIZE_MAX)
1575 		return (EINVAL);
1576 	linux_set_current(td);
1577 	linux_get_fop(filp, &fop, &ldev);
1578 	if (fop->write != NULL) {
1579 		bytes = OPW(file, td, fop->write(filp,
1580 		    uio->uio_iov->iov_base,
1581 		    uio->uio_iov->iov_len, &uio->uio_offset));
1582 		if (bytes >= 0) {
1583 			uio->uio_iov->iov_base =
1584 			    ((uint8_t *)uio->uio_iov->iov_base) + bytes;
1585 			uio->uio_iov->iov_len -= bytes;
1586 			uio->uio_resid -= bytes;
1587 			error = 0;
1588 		} else {
1589 			error = linux_get_error(current, -bytes);
1590 		}
1591 	} else
1592 		error = ENXIO;
1593 
1594 	/* update kqfilter status, if any */
1595 	linux_file_kqfilter_poll(filp, LINUX_KQ_FLAG_HAS_WRITE);
1596 
1597 	linux_drop_fop(ldev);
1598 
1599 	return (error);
1600 }
1601 
1602 static int
1603 linux_file_poll(struct file *file, int events, struct ucred *active_cred,
1604     struct thread *td)
1605 {
1606 	struct linux_file *filp;
1607 	const struct file_operations *fop;
1608 	struct linux_cdev *ldev;
1609 	int revents;
1610 
1611 	filp = (struct linux_file *)file->f_data;
1612 	filp->f_flags = file->f_flag;
1613 	linux_set_current(td);
1614 	linux_get_fop(filp, &fop, &ldev);
1615 	if (fop->poll != NULL) {
1616 		revents = OPW(file, td, fop->poll(filp,
1617 		    LINUX_POLL_TABLE_NORMAL)) & events;
1618 	} else {
1619 		revents = 0;
1620 	}
1621 	linux_drop_fop(ldev);
1622 	return (revents);
1623 }
1624 
1625 static int
1626 linux_file_close(struct file *file, struct thread *td)
1627 {
1628 	struct linux_file *filp;
1629 	int (*release)(struct inode *, struct linux_file *);
1630 	const struct file_operations *fop;
1631 	struct linux_cdev *ldev;
1632 	int error;
1633 
1634 	filp = (struct linux_file *)file->f_data;
1635 
1636 	KASSERT(file_count(filp) == 0,
1637 	    ("File refcount(%d) is not zero", file_count(filp)));
1638 
1639 	if (td == NULL)
1640 		td = curthread;
1641 
1642 	error = 0;
1643 	filp->f_flags = file->f_flag;
1644 	linux_set_current(td);
1645 	linux_poll_wait_dequeue(filp);
1646 	linux_get_fop(filp, &fop, &ldev);
1647 	/*
1648 	 * Always use the real release function, if any, to avoid
1649 	 * leaking device resources:
1650 	 */
1651 	release = filp->f_op->release;
1652 	if (release != NULL)
1653 		error = -OPW(file, td, release(filp->f_vnode, filp));
1654 	funsetown(&filp->f_sigio);
1655 	if (filp->f_vnode != NULL)
1656 		vdrop(filp->f_vnode);
1657 	linux_drop_fop(ldev);
1658 	ldev = filp->f_cdev;
1659 	if (ldev != NULL)
1660 		linux_cdev_deref(ldev);
1661 	linux_synchronize_rcu(RCU_TYPE_REGULAR);
1662 	kfree(filp);
1663 
1664 	return (error);
1665 }
1666 
1667 static int
1668 linux_file_ioctl(struct file *fp, u_long cmd, void *data, struct ucred *cred,
1669     struct thread *td)
1670 {
1671 	struct linux_file *filp;
1672 	const struct file_operations *fop;
1673 	struct linux_cdev *ldev;
1674 	struct fiodgname_arg *fgn;
1675 	const char *p;
1676 	int error, i;
1677 
1678 	error = 0;
1679 	filp = (struct linux_file *)fp->f_data;
1680 	filp->f_flags = fp->f_flag;
1681 	linux_get_fop(filp, &fop, &ldev);
1682 
1683 	linux_set_current(td);
1684 	switch (cmd) {
1685 	case FIONBIO:
1686 		break;
1687 	case FIOASYNC:
1688 		if (fop->fasync == NULL)
1689 			break;
1690 		error = -OPW(fp, td, fop->fasync(0, filp, fp->f_flag & FASYNC));
1691 		break;
1692 	case FIOSETOWN:
1693 		error = fsetown(*(int *)data, &filp->f_sigio);
1694 		if (error == 0) {
1695 			if (fop->fasync == NULL)
1696 				break;
1697 			error = -OPW(fp, td, fop->fasync(0, filp,
1698 			    fp->f_flag & FASYNC));
1699 		}
1700 		break;
1701 	case FIOGETOWN:
1702 		*(int *)data = fgetown(&filp->f_sigio);
1703 		break;
1704 	case FIODGNAME:
1705 #ifdef	COMPAT_FREEBSD32
1706 	case FIODGNAME_32:
1707 #endif
1708 		if (filp->f_cdev == NULL || filp->f_cdev->cdev == NULL) {
1709 			error = ENXIO;
1710 			break;
1711 		}
1712 		fgn = data;
1713 		p = devtoname(filp->f_cdev->cdev);
1714 		i = strlen(p) + 1;
1715 		if (i > fgn->len) {
1716 			error = EINVAL;
1717 			break;
1718 		}
1719 		error = copyout(p, fiodgname_buf_get_ptr(fgn, cmd), i);
1720 		break;
1721 	default:
1722 		error = linux_file_ioctl_sub(fp, filp, fop, cmd, data, td);
1723 		break;
1724 	}
1725 	linux_drop_fop(ldev);
1726 	return (error);
1727 }
1728 
1729 static int
1730 linux_file_mmap_sub(struct thread *td, vm_size_t objsize, vm_prot_t prot,
1731     vm_prot_t maxprot, int flags, struct file *fp,
1732     vm_ooffset_t *foff, const struct file_operations *fop, vm_object_t *objp)
1733 {
1734 	/*
1735 	 * Character devices do not provide private mappings
1736 	 * of any kind:
1737 	 */
1738 	if ((maxprot & VM_PROT_WRITE) == 0 &&
1739 	    (prot & VM_PROT_WRITE) != 0)
1740 		return (EACCES);
1741 	if ((flags & (MAP_PRIVATE | MAP_COPY)) != 0)
1742 		return (EINVAL);
1743 
1744 	return (linux_file_mmap_single(fp, fop, foff, objsize, objp,
1745 	    (int)prot, (flags & MAP_SHARED) ? true : false, td));
1746 }
1747 
1748 static int
1749 linux_file_mmap(struct file *fp, vm_map_t map, vm_offset_t *addr, vm_size_t size,
1750     vm_prot_t prot, vm_prot_t cap_maxprot, int flags, vm_ooffset_t foff,
1751     struct thread *td)
1752 {
1753 	struct linux_file *filp;
1754 	const struct file_operations *fop;
1755 	struct linux_cdev *ldev;
1756 	struct mount *mp;
1757 	struct vnode *vp;
1758 	vm_object_t object;
1759 	vm_prot_t maxprot;
1760 	int error;
1761 
1762 	filp = (struct linux_file *)fp->f_data;
1763 
1764 	vp = filp->f_vnode;
1765 	if (vp == NULL)
1766 		return (EOPNOTSUPP);
1767 
1768 	/*
1769 	 * Ensure that file and memory protections are
1770 	 * compatible.
1771 	 */
1772 	mp = vp->v_mount;
1773 	if (mp != NULL && (mp->mnt_flag & MNT_NOEXEC) != 0) {
1774 		maxprot = VM_PROT_NONE;
1775 		if ((prot & VM_PROT_EXECUTE) != 0)
1776 			return (EACCES);
1777 	} else
1778 		maxprot = VM_PROT_EXECUTE;
1779 	if ((fp->f_flag & FREAD) != 0)
1780 		maxprot |= VM_PROT_READ;
1781 	else if ((prot & VM_PROT_READ) != 0)
1782 		return (EACCES);
1783 
1784 	/*
1785 	 * If we are sharing potential changes via MAP_SHARED and we
1786 	 * are trying to get write permission although we opened it
1787 	 * without asking for it, bail out.
1788 	 *
1789 	 * Note that most character devices always share mappings.
1790 	 *
1791 	 * Rely on linux_file_mmap_sub() to fail invalid MAP_PRIVATE
1792 	 * requests rather than doing it here.
1793 	 */
1794 	if ((flags & MAP_SHARED) != 0) {
1795 		if ((fp->f_flag & FWRITE) != 0)
1796 			maxprot |= VM_PROT_WRITE;
1797 		else if ((prot & VM_PROT_WRITE) != 0)
1798 			return (EACCES);
1799 	}
1800 	maxprot &= cap_maxprot;
1801 
1802 	linux_get_fop(filp, &fop, &ldev);
1803 	error = linux_file_mmap_sub(td, size, prot, maxprot, flags, fp,
1804 	    &foff, fop, &object);
1805 	if (error != 0)
1806 		goto out;
1807 
1808 	error = vm_mmap_object(map, addr, size, prot, maxprot, flags, object,
1809 	    foff, FALSE, td);
1810 	if (error != 0)
1811 		vm_object_deallocate(object);
1812 out:
1813 	linux_drop_fop(ldev);
1814 	return (error);
1815 }
1816 
1817 static int
1818 linux_file_stat(struct file *fp, struct stat *sb, struct ucred *active_cred)
1819 {
1820 	struct linux_file *filp;
1821 	struct vnode *vp;
1822 	int error;
1823 
1824 	filp = (struct linux_file *)fp->f_data;
1825 	if (filp->f_vnode == NULL)
1826 		return (EOPNOTSUPP);
1827 
1828 	vp = filp->f_vnode;
1829 
1830 	vn_lock(vp, LK_SHARED | LK_RETRY);
1831 	error = VOP_STAT(vp, sb, curthread->td_ucred, NOCRED);
1832 	VOP_UNLOCK(vp);
1833 
1834 	return (error);
1835 }
1836 
1837 static int
1838 linux_file_fill_kinfo(struct file *fp, struct kinfo_file *kif,
1839     struct filedesc *fdp)
1840 {
1841 	struct linux_file *filp;
1842 	struct vnode *vp;
1843 	int error;
1844 
1845 	filp = fp->f_data;
1846 	vp = filp->f_vnode;
1847 	if (vp == NULL) {
1848 		error = 0;
1849 		kif->kf_type = KF_TYPE_DEV;
1850 	} else {
1851 		vref(vp);
1852 		FILEDESC_SUNLOCK(fdp);
1853 		error = vn_fill_kinfo_vnode(vp, kif);
1854 		vrele(vp);
1855 		kif->kf_type = KF_TYPE_VNODE;
1856 		FILEDESC_SLOCK(fdp);
1857 	}
1858 	return (error);
1859 }
1860 
1861 unsigned int
1862 linux_iminor(struct inode *inode)
1863 {
1864 	struct linux_cdev *ldev;
1865 
1866 	if (inode == NULL || inode->v_rdev == NULL ||
1867 	    inode->v_rdev->si_devsw != &linuxcdevsw)
1868 		return (-1U);
1869 	ldev = inode->v_rdev->si_drv1;
1870 	if (ldev == NULL)
1871 		return (-1U);
1872 
1873 	return (minor(ldev->dev));
1874 }
1875 
1876 struct fileops linuxfileops = {
1877 	.fo_read = linux_file_read,
1878 	.fo_write = linux_file_write,
1879 	.fo_truncate = invfo_truncate,
1880 	.fo_kqfilter = linux_file_kqfilter,
1881 	.fo_stat = linux_file_stat,
1882 	.fo_fill_kinfo = linux_file_fill_kinfo,
1883 	.fo_poll = linux_file_poll,
1884 	.fo_close = linux_file_close,
1885 	.fo_ioctl = linux_file_ioctl,
1886 	.fo_mmap = linux_file_mmap,
1887 	.fo_chmod = invfo_chmod,
1888 	.fo_chown = invfo_chown,
1889 	.fo_sendfile = invfo_sendfile,
1890 	.fo_flags = DFLAG_PASSABLE,
1891 };
1892 
1893 /*
1894  * Hash of vmmap addresses.  This is infrequently accessed and does not
1895  * need to be particularly large.  This is done because we must store the
1896  * caller's idea of the map size to properly unmap.
1897  */
1898 struct vmmap {
1899 	LIST_ENTRY(vmmap)	vm_next;
1900 	void 			*vm_addr;
1901 	unsigned long		vm_size;
1902 };
1903 
1904 struct vmmaphd {
1905 	struct vmmap *lh_first;
1906 };
1907 #define	VMMAP_HASH_SIZE	64
1908 #define	VMMAP_HASH_MASK	(VMMAP_HASH_SIZE - 1)
1909 #define	VM_HASH(addr)	((uintptr_t)(addr) >> PAGE_SHIFT) & VMMAP_HASH_MASK
1910 static struct vmmaphd vmmaphead[VMMAP_HASH_SIZE];
1911 static struct mtx vmmaplock;
1912 
1913 static void
1914 vmmap_add(void *addr, unsigned long size)
1915 {
1916 	struct vmmap *vmmap;
1917 
1918 	vmmap = kmalloc(sizeof(*vmmap), GFP_KERNEL);
1919 	mtx_lock(&vmmaplock);
1920 	vmmap->vm_size = size;
1921 	vmmap->vm_addr = addr;
1922 	LIST_INSERT_HEAD(&vmmaphead[VM_HASH(addr)], vmmap, vm_next);
1923 	mtx_unlock(&vmmaplock);
1924 }
1925 
1926 static struct vmmap *
1927 vmmap_remove(void *addr)
1928 {
1929 	struct vmmap *vmmap;
1930 
1931 	mtx_lock(&vmmaplock);
1932 	LIST_FOREACH(vmmap, &vmmaphead[VM_HASH(addr)], vm_next)
1933 		if (vmmap->vm_addr == addr)
1934 			break;
1935 	if (vmmap)
1936 		LIST_REMOVE(vmmap, vm_next);
1937 	mtx_unlock(&vmmaplock);
1938 
1939 	return (vmmap);
1940 }
1941 
1942 #if defined(__i386__) || defined(__amd64__) || defined(__powerpc__) || defined(__aarch64__) || defined(__riscv)
1943 void *
1944 _ioremap_attr(vm_paddr_t phys_addr, unsigned long size, int attr)
1945 {
1946 	void *addr;
1947 
1948 	addr = pmap_mapdev_attr(phys_addr, size, attr);
1949 	if (addr == NULL)
1950 		return (NULL);
1951 	vmmap_add(addr, size);
1952 
1953 	return (addr);
1954 }
1955 #endif
1956 
1957 void
1958 iounmap(void *addr)
1959 {
1960 	struct vmmap *vmmap;
1961 
1962 	vmmap = vmmap_remove(addr);
1963 	if (vmmap == NULL)
1964 		return;
1965 #if defined(__i386__) || defined(__amd64__) || defined(__powerpc__) || defined(__aarch64__) || defined(__riscv)
1966 	pmap_unmapdev((vm_offset_t)addr, vmmap->vm_size);
1967 #endif
1968 	kfree(vmmap);
1969 }
1970 
1971 void *
1972 vmap(struct page **pages, unsigned int count, unsigned long flags, int prot)
1973 {
1974 	vm_offset_t off;
1975 	size_t size;
1976 
1977 	size = count * PAGE_SIZE;
1978 	off = kva_alloc(size);
1979 	if (off == 0)
1980 		return (NULL);
1981 	vmmap_add((void *)off, size);
1982 	pmap_qenter(off, pages, count);
1983 
1984 	return ((void *)off);
1985 }
1986 
1987 void
1988 vunmap(void *addr)
1989 {
1990 	struct vmmap *vmmap;
1991 
1992 	vmmap = vmmap_remove(addr);
1993 	if (vmmap == NULL)
1994 		return;
1995 	pmap_qremove((vm_offset_t)addr, vmmap->vm_size / PAGE_SIZE);
1996 	kva_free((vm_offset_t)addr, vmmap->vm_size);
1997 	kfree(vmmap);
1998 }
1999 
2000 static char *
2001 devm_kvasprintf(struct device *dev, gfp_t gfp, const char *fmt, va_list ap)
2002 {
2003 	unsigned int len;
2004 	char *p;
2005 	va_list aq;
2006 
2007 	va_copy(aq, ap);
2008 	len = vsnprintf(NULL, 0, fmt, aq);
2009 	va_end(aq);
2010 
2011 	if (dev != NULL)
2012 		p = devm_kmalloc(dev, len + 1, gfp);
2013 	else
2014 		p = kmalloc(len + 1, gfp);
2015 	if (p != NULL)
2016 		vsnprintf(p, len + 1, fmt, ap);
2017 
2018 	return (p);
2019 }
2020 
2021 char *
2022 kvasprintf(gfp_t gfp, const char *fmt, va_list ap)
2023 {
2024 
2025 	return (devm_kvasprintf(NULL, gfp, fmt, ap));
2026 }
2027 
2028 char *
2029 lkpi_devm_kasprintf(struct device *dev, gfp_t gfp, const char *fmt, ...)
2030 {
2031 	va_list ap;
2032 	char *p;
2033 
2034 	va_start(ap, fmt);
2035 	p = devm_kvasprintf(dev, gfp, fmt, ap);
2036 	va_end(ap);
2037 
2038 	return (p);
2039 }
2040 
2041 char *
2042 kasprintf(gfp_t gfp, const char *fmt, ...)
2043 {
2044 	va_list ap;
2045 	char *p;
2046 
2047 	va_start(ap, fmt);
2048 	p = kvasprintf(gfp, fmt, ap);
2049 	va_end(ap);
2050 
2051 	return (p);
2052 }
2053 
2054 static void
2055 linux_timer_callback_wrapper(void *context)
2056 {
2057 	struct timer_list *timer;
2058 
2059 	timer = context;
2060 
2061 	if (linux_set_current_flags(curthread, M_NOWAIT)) {
2062 		/* try again later */
2063 		callout_reset(&timer->callout, 1,
2064 		    &linux_timer_callback_wrapper, timer);
2065 		return;
2066 	}
2067 
2068 	timer->function(timer->data);
2069 }
2070 
2071 int
2072 mod_timer(struct timer_list *timer, int expires)
2073 {
2074 	int ret;
2075 
2076 	timer->expires = expires;
2077 	ret = callout_reset(&timer->callout,
2078 	    linux_timer_jiffies_until(expires),
2079 	    &linux_timer_callback_wrapper, timer);
2080 
2081 	MPASS(ret == 0 || ret == 1);
2082 
2083 	return (ret == 1);
2084 }
2085 
2086 void
2087 add_timer(struct timer_list *timer)
2088 {
2089 
2090 	callout_reset(&timer->callout,
2091 	    linux_timer_jiffies_until(timer->expires),
2092 	    &linux_timer_callback_wrapper, timer);
2093 }
2094 
2095 void
2096 add_timer_on(struct timer_list *timer, int cpu)
2097 {
2098 
2099 	callout_reset_on(&timer->callout,
2100 	    linux_timer_jiffies_until(timer->expires),
2101 	    &linux_timer_callback_wrapper, timer, cpu);
2102 }
2103 
2104 int
2105 del_timer(struct timer_list *timer)
2106 {
2107 
2108 	if (callout_stop(&(timer)->callout) == -1)
2109 		return (0);
2110 	return (1);
2111 }
2112 
2113 int
2114 del_timer_sync(struct timer_list *timer)
2115 {
2116 
2117 	if (callout_drain(&(timer)->callout) == -1)
2118 		return (0);
2119 	return (1);
2120 }
2121 
2122 /* greatest common divisor, Euclid equation */
2123 static uint64_t
2124 lkpi_gcd_64(uint64_t a, uint64_t b)
2125 {
2126 	uint64_t an;
2127 	uint64_t bn;
2128 
2129 	while (b != 0) {
2130 		an = b;
2131 		bn = a % b;
2132 		a = an;
2133 		b = bn;
2134 	}
2135 	return (a);
2136 }
2137 
2138 uint64_t lkpi_nsec2hz_rem;
2139 uint64_t lkpi_nsec2hz_div = 1000000000ULL;
2140 uint64_t lkpi_nsec2hz_max;
2141 
2142 uint64_t lkpi_usec2hz_rem;
2143 uint64_t lkpi_usec2hz_div = 1000000ULL;
2144 uint64_t lkpi_usec2hz_max;
2145 
2146 uint64_t lkpi_msec2hz_rem;
2147 uint64_t lkpi_msec2hz_div = 1000ULL;
2148 uint64_t lkpi_msec2hz_max;
2149 
2150 static void
2151 linux_timer_init(void *arg)
2152 {
2153 	uint64_t gcd;
2154 
2155 	/*
2156 	 * Compute an internal HZ value which can divide 2**32 to
2157 	 * avoid timer rounding problems when the tick value wraps
2158 	 * around 2**32:
2159 	 */
2160 	linux_timer_hz_mask = 1;
2161 	while (linux_timer_hz_mask < (unsigned long)hz)
2162 		linux_timer_hz_mask *= 2;
2163 	linux_timer_hz_mask--;
2164 
2165 	/* compute some internal constants */
2166 
2167 	lkpi_nsec2hz_rem = hz;
2168 	lkpi_usec2hz_rem = hz;
2169 	lkpi_msec2hz_rem = hz;
2170 
2171 	gcd = lkpi_gcd_64(lkpi_nsec2hz_rem, lkpi_nsec2hz_div);
2172 	lkpi_nsec2hz_rem /= gcd;
2173 	lkpi_nsec2hz_div /= gcd;
2174 	lkpi_nsec2hz_max = -1ULL / lkpi_nsec2hz_rem;
2175 
2176 	gcd = lkpi_gcd_64(lkpi_usec2hz_rem, lkpi_usec2hz_div);
2177 	lkpi_usec2hz_rem /= gcd;
2178 	lkpi_usec2hz_div /= gcd;
2179 	lkpi_usec2hz_max = -1ULL / lkpi_usec2hz_rem;
2180 
2181 	gcd = lkpi_gcd_64(lkpi_msec2hz_rem, lkpi_msec2hz_div);
2182 	lkpi_msec2hz_rem /= gcd;
2183 	lkpi_msec2hz_div /= gcd;
2184 	lkpi_msec2hz_max = -1ULL / lkpi_msec2hz_rem;
2185 }
2186 SYSINIT(linux_timer, SI_SUB_DRIVERS, SI_ORDER_FIRST, linux_timer_init, NULL);
2187 
2188 void
2189 linux_complete_common(struct completion *c, int all)
2190 {
2191 	int wakeup_swapper;
2192 
2193 	sleepq_lock(c);
2194 	if (all) {
2195 		c->done = UINT_MAX;
2196 		wakeup_swapper = sleepq_broadcast(c, SLEEPQ_SLEEP, 0, 0);
2197 	} else {
2198 		if (c->done != UINT_MAX)
2199 			c->done++;
2200 		wakeup_swapper = sleepq_signal(c, SLEEPQ_SLEEP, 0, 0);
2201 	}
2202 	sleepq_release(c);
2203 	if (wakeup_swapper)
2204 		kick_proc0();
2205 }
2206 
2207 /*
2208  * Indefinite wait for done != 0 with or without signals.
2209  */
2210 int
2211 linux_wait_for_common(struct completion *c, int flags)
2212 {
2213 	struct task_struct *task;
2214 	int error;
2215 
2216 	if (SCHEDULER_STOPPED())
2217 		return (0);
2218 
2219 	task = current;
2220 
2221 	if (flags != 0)
2222 		flags = SLEEPQ_INTERRUPTIBLE | SLEEPQ_SLEEP;
2223 	else
2224 		flags = SLEEPQ_SLEEP;
2225 	error = 0;
2226 	for (;;) {
2227 		sleepq_lock(c);
2228 		if (c->done)
2229 			break;
2230 		sleepq_add(c, NULL, "completion", flags, 0);
2231 		if (flags & SLEEPQ_INTERRUPTIBLE) {
2232 			DROP_GIANT();
2233 			error = -sleepq_wait_sig(c, 0);
2234 			PICKUP_GIANT();
2235 			if (error != 0) {
2236 				linux_schedule_save_interrupt_value(task, error);
2237 				error = -ERESTARTSYS;
2238 				goto intr;
2239 			}
2240 		} else {
2241 			DROP_GIANT();
2242 			sleepq_wait(c, 0);
2243 			PICKUP_GIANT();
2244 		}
2245 	}
2246 	if (c->done != UINT_MAX)
2247 		c->done--;
2248 	sleepq_release(c);
2249 
2250 intr:
2251 	return (error);
2252 }
2253 
2254 /*
2255  * Time limited wait for done != 0 with or without signals.
2256  */
2257 int
2258 linux_wait_for_timeout_common(struct completion *c, int timeout, int flags)
2259 {
2260 	struct task_struct *task;
2261 	int end = jiffies + timeout;
2262 	int error;
2263 
2264 	if (SCHEDULER_STOPPED())
2265 		return (0);
2266 
2267 	task = current;
2268 
2269 	if (flags != 0)
2270 		flags = SLEEPQ_INTERRUPTIBLE | SLEEPQ_SLEEP;
2271 	else
2272 		flags = SLEEPQ_SLEEP;
2273 
2274 	for (;;) {
2275 		sleepq_lock(c);
2276 		if (c->done)
2277 			break;
2278 		sleepq_add(c, NULL, "completion", flags, 0);
2279 		sleepq_set_timeout(c, linux_timer_jiffies_until(end));
2280 
2281 		DROP_GIANT();
2282 		if (flags & SLEEPQ_INTERRUPTIBLE)
2283 			error = -sleepq_timedwait_sig(c, 0);
2284 		else
2285 			error = -sleepq_timedwait(c, 0);
2286 		PICKUP_GIANT();
2287 
2288 		if (error != 0) {
2289 			/* check for timeout */
2290 			if (error == -EWOULDBLOCK) {
2291 				error = 0;	/* timeout */
2292 			} else {
2293 				/* signal happened */
2294 				linux_schedule_save_interrupt_value(task, error);
2295 				error = -ERESTARTSYS;
2296 			}
2297 			goto done;
2298 		}
2299 	}
2300 	if (c->done != UINT_MAX)
2301 		c->done--;
2302 	sleepq_release(c);
2303 
2304 	/* return how many jiffies are left */
2305 	error = linux_timer_jiffies_until(end);
2306 done:
2307 	return (error);
2308 }
2309 
2310 int
2311 linux_try_wait_for_completion(struct completion *c)
2312 {
2313 	int isdone;
2314 
2315 	sleepq_lock(c);
2316 	isdone = (c->done != 0);
2317 	if (c->done != 0 && c->done != UINT_MAX)
2318 		c->done--;
2319 	sleepq_release(c);
2320 	return (isdone);
2321 }
2322 
2323 int
2324 linux_completion_done(struct completion *c)
2325 {
2326 	int isdone;
2327 
2328 	sleepq_lock(c);
2329 	isdone = (c->done != 0);
2330 	sleepq_release(c);
2331 	return (isdone);
2332 }
2333 
2334 static void
2335 linux_cdev_deref(struct linux_cdev *ldev)
2336 {
2337 	if (refcount_release(&ldev->refs) &&
2338 	    ldev->kobj.ktype == &linux_cdev_ktype)
2339 		kfree(ldev);
2340 }
2341 
2342 static void
2343 linux_cdev_release(struct kobject *kobj)
2344 {
2345 	struct linux_cdev *cdev;
2346 	struct kobject *parent;
2347 
2348 	cdev = container_of(kobj, struct linux_cdev, kobj);
2349 	parent = kobj->parent;
2350 	linux_destroy_dev(cdev);
2351 	linux_cdev_deref(cdev);
2352 	kobject_put(parent);
2353 }
2354 
2355 static void
2356 linux_cdev_static_release(struct kobject *kobj)
2357 {
2358 	struct cdev *cdev;
2359 	struct linux_cdev *ldev;
2360 
2361 	ldev = container_of(kobj, struct linux_cdev, kobj);
2362 	cdev = ldev->cdev;
2363 	if (cdev != NULL) {
2364 		destroy_dev(cdev);
2365 		ldev->cdev = NULL;
2366 	}
2367 	kobject_put(kobj->parent);
2368 }
2369 
2370 int
2371 linux_cdev_device_add(struct linux_cdev *ldev, struct device *dev)
2372 {
2373 	int ret;
2374 
2375 	if (dev->devt != 0) {
2376 		/* Set parent kernel object. */
2377 		ldev->kobj.parent = &dev->kobj;
2378 
2379 		/*
2380 		 * Unlike Linux we require the kobject of the
2381 		 * character device structure to have a valid name
2382 		 * before calling this function:
2383 		 */
2384 		if (ldev->kobj.name == NULL)
2385 			return (-EINVAL);
2386 
2387 		ret = cdev_add(ldev, dev->devt, 1);
2388 		if (ret)
2389 			return (ret);
2390 	}
2391 	ret = device_add(dev);
2392 	if (ret != 0 && dev->devt != 0)
2393 		cdev_del(ldev);
2394 	return (ret);
2395 }
2396 
2397 void
2398 linux_cdev_device_del(struct linux_cdev *ldev, struct device *dev)
2399 {
2400 	device_del(dev);
2401 
2402 	if (dev->devt != 0)
2403 		cdev_del(ldev);
2404 }
2405 
2406 static void
2407 linux_destroy_dev(struct linux_cdev *ldev)
2408 {
2409 
2410 	if (ldev->cdev == NULL)
2411 		return;
2412 
2413 	MPASS((ldev->siref & LDEV_SI_DTR) == 0);
2414 	MPASS(ldev->kobj.ktype == &linux_cdev_ktype);
2415 
2416 	atomic_set_int(&ldev->siref, LDEV_SI_DTR);
2417 	while ((atomic_load_int(&ldev->siref) & ~LDEV_SI_DTR) != 0)
2418 		pause("ldevdtr", hz / 4);
2419 
2420 	destroy_dev(ldev->cdev);
2421 	ldev->cdev = NULL;
2422 }
2423 
2424 const struct kobj_type linux_cdev_ktype = {
2425 	.release = linux_cdev_release,
2426 };
2427 
2428 const struct kobj_type linux_cdev_static_ktype = {
2429 	.release = linux_cdev_static_release,
2430 };
2431 
2432 static void
2433 linux_handle_ifnet_link_event(void *arg, struct ifnet *ifp, int linkstate)
2434 {
2435 	struct notifier_block *nb;
2436 	struct netdev_notifier_info ni;
2437 
2438 	nb = arg;
2439 	ni.ifp = ifp;
2440 	ni.dev = (struct net_device *)ifp;
2441 	if (linkstate == LINK_STATE_UP)
2442 		nb->notifier_call(nb, NETDEV_UP, &ni);
2443 	else
2444 		nb->notifier_call(nb, NETDEV_DOWN, &ni);
2445 }
2446 
2447 static void
2448 linux_handle_ifnet_arrival_event(void *arg, struct ifnet *ifp)
2449 {
2450 	struct notifier_block *nb;
2451 	struct netdev_notifier_info ni;
2452 
2453 	nb = arg;
2454 	ni.ifp = ifp;
2455 	ni.dev = (struct net_device *)ifp;
2456 	nb->notifier_call(nb, NETDEV_REGISTER, &ni);
2457 }
2458 
2459 static void
2460 linux_handle_ifnet_departure_event(void *arg, struct ifnet *ifp)
2461 {
2462 	struct notifier_block *nb;
2463 	struct netdev_notifier_info ni;
2464 
2465 	nb = arg;
2466 	ni.ifp = ifp;
2467 	ni.dev = (struct net_device *)ifp;
2468 	nb->notifier_call(nb, NETDEV_UNREGISTER, &ni);
2469 }
2470 
2471 static void
2472 linux_handle_iflladdr_event(void *arg, struct ifnet *ifp)
2473 {
2474 	struct notifier_block *nb;
2475 	struct netdev_notifier_info ni;
2476 
2477 	nb = arg;
2478 	ni.ifp = ifp;
2479 	ni.dev = (struct net_device *)ifp;
2480 	nb->notifier_call(nb, NETDEV_CHANGEADDR, &ni);
2481 }
2482 
2483 static void
2484 linux_handle_ifaddr_event(void *arg, struct ifnet *ifp)
2485 {
2486 	struct notifier_block *nb;
2487 	struct netdev_notifier_info ni;
2488 
2489 	nb = arg;
2490 	ni.ifp = ifp;
2491 	ni.dev = (struct net_device *)ifp;
2492 	nb->notifier_call(nb, NETDEV_CHANGEIFADDR, &ni);
2493 }
2494 
2495 int
2496 register_netdevice_notifier(struct notifier_block *nb)
2497 {
2498 
2499 	nb->tags[NETDEV_UP] = EVENTHANDLER_REGISTER(
2500 	    ifnet_link_event, linux_handle_ifnet_link_event, nb, 0);
2501 	nb->tags[NETDEV_REGISTER] = EVENTHANDLER_REGISTER(
2502 	    ifnet_arrival_event, linux_handle_ifnet_arrival_event, nb, 0);
2503 	nb->tags[NETDEV_UNREGISTER] = EVENTHANDLER_REGISTER(
2504 	    ifnet_departure_event, linux_handle_ifnet_departure_event, nb, 0);
2505 	nb->tags[NETDEV_CHANGEADDR] = EVENTHANDLER_REGISTER(
2506 	    iflladdr_event, linux_handle_iflladdr_event, nb, 0);
2507 
2508 	return (0);
2509 }
2510 
2511 int
2512 register_inetaddr_notifier(struct notifier_block *nb)
2513 {
2514 
2515 	nb->tags[NETDEV_CHANGEIFADDR] = EVENTHANDLER_REGISTER(
2516 	    ifaddr_event, linux_handle_ifaddr_event, nb, 0);
2517 	return (0);
2518 }
2519 
2520 int
2521 unregister_netdevice_notifier(struct notifier_block *nb)
2522 {
2523 
2524 	EVENTHANDLER_DEREGISTER(ifnet_link_event,
2525 	    nb->tags[NETDEV_UP]);
2526 	EVENTHANDLER_DEREGISTER(ifnet_arrival_event,
2527 	    nb->tags[NETDEV_REGISTER]);
2528 	EVENTHANDLER_DEREGISTER(ifnet_departure_event,
2529 	    nb->tags[NETDEV_UNREGISTER]);
2530 	EVENTHANDLER_DEREGISTER(iflladdr_event,
2531 	    nb->tags[NETDEV_CHANGEADDR]);
2532 
2533 	return (0);
2534 }
2535 
2536 int
2537 unregister_inetaddr_notifier(struct notifier_block *nb)
2538 {
2539 
2540 	EVENTHANDLER_DEREGISTER(ifaddr_event,
2541 	    nb->tags[NETDEV_CHANGEIFADDR]);
2542 
2543 	return (0);
2544 }
2545 
2546 struct list_sort_thunk {
2547 	int (*cmp)(void *, struct list_head *, struct list_head *);
2548 	void *priv;
2549 };
2550 
2551 static inline int
2552 linux_le_cmp(void *priv, const void *d1, const void *d2)
2553 {
2554 	struct list_head *le1, *le2;
2555 	struct list_sort_thunk *thunk;
2556 
2557 	thunk = priv;
2558 	le1 = *(__DECONST(struct list_head **, d1));
2559 	le2 = *(__DECONST(struct list_head **, d2));
2560 	return ((thunk->cmp)(thunk->priv, le1, le2));
2561 }
2562 
2563 void
2564 list_sort(void *priv, struct list_head *head, int (*cmp)(void *priv,
2565     struct list_head *a, struct list_head *b))
2566 {
2567 	struct list_sort_thunk thunk;
2568 	struct list_head **ar, *le;
2569 	size_t count, i;
2570 
2571 	count = 0;
2572 	list_for_each(le, head)
2573 		count++;
2574 	ar = malloc(sizeof(struct list_head *) * count, M_KMALLOC, M_WAITOK);
2575 	i = 0;
2576 	list_for_each(le, head)
2577 		ar[i++] = le;
2578 	thunk.cmp = cmp;
2579 	thunk.priv = priv;
2580 	qsort_r(ar, count, sizeof(struct list_head *), &thunk, linux_le_cmp);
2581 	INIT_LIST_HEAD(head);
2582 	for (i = 0; i < count; i++)
2583 		list_add_tail(ar[i], head);
2584 	free(ar, M_KMALLOC);
2585 }
2586 
2587 #if defined(__i386__) || defined(__amd64__)
2588 int
2589 linux_wbinvd_on_all_cpus(void)
2590 {
2591 
2592 	pmap_invalidate_cache();
2593 	return (0);
2594 }
2595 #endif
2596 
2597 int
2598 linux_on_each_cpu(void callback(void *), void *data)
2599 {
2600 
2601 	smp_rendezvous(smp_no_rendezvous_barrier, callback,
2602 	    smp_no_rendezvous_barrier, data);
2603 	return (0);
2604 }
2605 
2606 int
2607 linux_in_atomic(void)
2608 {
2609 
2610 	return ((curthread->td_pflags & TDP_NOFAULTING) != 0);
2611 }
2612 
2613 struct linux_cdev *
2614 linux_find_cdev(const char *name, unsigned major, unsigned minor)
2615 {
2616 	dev_t dev = MKDEV(major, minor);
2617 	struct cdev *cdev;
2618 
2619 	dev_lock();
2620 	LIST_FOREACH(cdev, &linuxcdevsw.d_devs, si_list) {
2621 		struct linux_cdev *ldev = cdev->si_drv1;
2622 		if (ldev->dev == dev &&
2623 		    strcmp(kobject_name(&ldev->kobj), name) == 0) {
2624 			break;
2625 		}
2626 	}
2627 	dev_unlock();
2628 
2629 	return (cdev != NULL ? cdev->si_drv1 : NULL);
2630 }
2631 
2632 int
2633 __register_chrdev(unsigned int major, unsigned int baseminor,
2634     unsigned int count, const char *name,
2635     const struct file_operations *fops)
2636 {
2637 	struct linux_cdev *cdev;
2638 	int ret = 0;
2639 	int i;
2640 
2641 	for (i = baseminor; i < baseminor + count; i++) {
2642 		cdev = cdev_alloc();
2643 		cdev->ops = fops;
2644 		kobject_set_name(&cdev->kobj, name);
2645 
2646 		ret = cdev_add(cdev, makedev(major, i), 1);
2647 		if (ret != 0)
2648 			break;
2649 	}
2650 	return (ret);
2651 }
2652 
2653 int
2654 __register_chrdev_p(unsigned int major, unsigned int baseminor,
2655     unsigned int count, const char *name,
2656     const struct file_operations *fops, uid_t uid,
2657     gid_t gid, int mode)
2658 {
2659 	struct linux_cdev *cdev;
2660 	int ret = 0;
2661 	int i;
2662 
2663 	for (i = baseminor; i < baseminor + count; i++) {
2664 		cdev = cdev_alloc();
2665 		cdev->ops = fops;
2666 		kobject_set_name(&cdev->kobj, name);
2667 
2668 		ret = cdev_add_ext(cdev, makedev(major, i), uid, gid, mode);
2669 		if (ret != 0)
2670 			break;
2671 	}
2672 	return (ret);
2673 }
2674 
2675 void
2676 __unregister_chrdev(unsigned int major, unsigned int baseminor,
2677     unsigned int count, const char *name)
2678 {
2679 	struct linux_cdev *cdevp;
2680 	int i;
2681 
2682 	for (i = baseminor; i < baseminor + count; i++) {
2683 		cdevp = linux_find_cdev(name, major, i);
2684 		if (cdevp != NULL)
2685 			cdev_del(cdevp);
2686 	}
2687 }
2688 
2689 void
2690 linux_dump_stack(void)
2691 {
2692 #ifdef STACK
2693 	struct stack st;
2694 
2695 	stack_save(&st);
2696 	stack_print(&st);
2697 #endif
2698 }
2699 
2700 int
2701 linuxkpi_net_ratelimit(void)
2702 {
2703 
2704 	return (ppsratecheck(&lkpi_net_lastlog, &lkpi_net_curpps,
2705 	   lkpi_net_maxpps));
2706 }
2707 
2708 #if defined(__i386__) || defined(__amd64__)
2709 bool linux_cpu_has_clflush;
2710 #endif
2711 
2712 static void
2713 linux_compat_init(void *arg)
2714 {
2715 	struct sysctl_oid *rootoid;
2716 	int i;
2717 
2718 #if defined(__i386__) || defined(__amd64__)
2719 	linux_cpu_has_clflush = (cpu_feature & CPUID_CLFSH);
2720 #endif
2721 	rw_init(&linux_vma_lock, "lkpi-vma-lock");
2722 
2723 	rootoid = SYSCTL_ADD_ROOT_NODE(NULL,
2724 	    OID_AUTO, "sys", CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, "sys");
2725 	kobject_init(&linux_class_root, &linux_class_ktype);
2726 	kobject_set_name(&linux_class_root, "class");
2727 	linux_class_root.oidp = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(rootoid),
2728 	    OID_AUTO, "class", CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, "class");
2729 	kobject_init(&linux_root_device.kobj, &linux_dev_ktype);
2730 	kobject_set_name(&linux_root_device.kobj, "device");
2731 	linux_root_device.kobj.oidp = SYSCTL_ADD_NODE(NULL,
2732 	    SYSCTL_CHILDREN(rootoid), OID_AUTO, "device",
2733 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "device");
2734 	linux_root_device.bsddev = root_bus;
2735 	linux_class_misc.name = "misc";
2736 	class_register(&linux_class_misc);
2737 	INIT_LIST_HEAD(&pci_drivers);
2738 	INIT_LIST_HEAD(&pci_devices);
2739 	spin_lock_init(&pci_lock);
2740 	mtx_init(&vmmaplock, "IO Map lock", NULL, MTX_DEF);
2741 	for (i = 0; i < VMMAP_HASH_SIZE; i++)
2742 		LIST_INIT(&vmmaphead[i]);
2743 	init_waitqueue_head(&linux_bit_waitq);
2744 	init_waitqueue_head(&linux_var_waitq);
2745 
2746 	CPU_COPY(&all_cpus, &cpu_online_mask);
2747 }
2748 SYSINIT(linux_compat, SI_SUB_DRIVERS, SI_ORDER_SECOND, linux_compat_init, NULL);
2749 
2750 static void
2751 linux_compat_uninit(void *arg)
2752 {
2753 	linux_kobject_kfree_name(&linux_class_root);
2754 	linux_kobject_kfree_name(&linux_root_device.kobj);
2755 	linux_kobject_kfree_name(&linux_class_misc.kobj);
2756 
2757 	mtx_destroy(&vmmaplock);
2758 	spin_lock_destroy(&pci_lock);
2759 	rw_destroy(&linux_vma_lock);
2760 }
2761 SYSUNINIT(linux_compat, SI_SUB_DRIVERS, SI_ORDER_SECOND, linux_compat_uninit, NULL);
2762 
2763 /*
2764  * NOTE: Linux frequently uses "unsigned long" for pointer to integer
2765  * conversion and vice versa, where in FreeBSD "uintptr_t" would be
2766  * used. Assert these types have the same size, else some parts of the
2767  * LinuxKPI may not work like expected:
2768  */
2769 CTASSERT(sizeof(unsigned long) == sizeof(uintptr_t));
2770