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