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