]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/compat/linuxkpi/common/src/linux_compat.c
Merge compiler-rt release_38 branch r258968.
[FreeBSD/FreeBSD.git] / sys / compat / linuxkpi / common / src / linux_compat.c
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-2016 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 <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/malloc.h>
36 #include <sys/kernel.h>
37 #include <sys/sysctl.h>
38 #include <sys/proc.h>
39 #include <sys/sglist.h>
40 #include <sys/sleepqueue.h>
41 #include <sys/lock.h>
42 #include <sys/mutex.h>
43 #include <sys/bus.h>
44 #include <sys/fcntl.h>
45 #include <sys/file.h>
46 #include <sys/filio.h>
47 #include <sys/rwlock.h>
48
49 #include <vm/vm.h>
50 #include <vm/pmap.h>
51
52 #include <machine/stdarg.h>
53 #include <machine/pmap.h>
54
55 #include <linux/kobject.h>
56 #include <linux/device.h>
57 #include <linux/slab.h>
58 #include <linux/module.h>
59 #include <linux/cdev.h>
60 #include <linux/file.h>
61 #include <linux/sysfs.h>
62 #include <linux/mm.h>
63 #include <linux/io.h>
64 #include <linux/vmalloc.h>
65 #include <linux/netdevice.h>
66 #include <linux/timer.h>
67 #include <linux/workqueue.h>
68 #include <linux/rcupdate.h>
69 #include <linux/interrupt.h>
70
71 #include <vm/vm_pager.h>
72
73 MALLOC_DEFINE(M_KMALLOC, "linux", "Linux kmalloc compat");
74
75 #include <linux/rbtree.h>
76 /* Undo Linux compat changes. */
77 #undef RB_ROOT
78 #undef file
79 #undef cdev
80 #define RB_ROOT(head)   (head)->rbh_root
81
82 struct kobject linux_class_root;
83 struct device linux_root_device;
84 struct class linux_class_misc;
85 struct list_head pci_drivers;
86 struct list_head pci_devices;
87 struct net init_net;
88 spinlock_t pci_lock;
89 struct sx linux_global_rcu_lock;
90
91 unsigned long linux_timer_hz_mask;
92
93 int
94 panic_cmp(struct rb_node *one, struct rb_node *two)
95 {
96         panic("no cmp");
97 }
98
99 RB_GENERATE(linux_root, rb_node, __entry, panic_cmp);
100
101 int
102 kobject_set_name_vargs(struct kobject *kobj, const char *fmt, va_list args)
103 {
104         va_list tmp_va;
105         int len;
106         char *old;
107         char *name;
108         char dummy;
109
110         old = kobj->name;
111
112         if (old && fmt == NULL)
113                 return (0);
114
115         /* compute length of string */
116         va_copy(tmp_va, args);
117         len = vsnprintf(&dummy, 0, fmt, tmp_va);
118         va_end(tmp_va);
119
120         /* account for zero termination */
121         len++;
122
123         /* check for error */
124         if (len < 1)
125                 return (-EINVAL);
126
127         /* allocate memory for string */
128         name = kzalloc(len, GFP_KERNEL);
129         if (name == NULL)
130                 return (-ENOMEM);
131         vsnprintf(name, len, fmt, args);
132         kobj->name = name;
133
134         /* free old string */
135         kfree(old);
136
137         /* filter new string */
138         for (; *name != '\0'; name++)
139                 if (*name == '/')
140                         *name = '!';
141         return (0);
142 }
143
144 int
145 kobject_set_name(struct kobject *kobj, const char *fmt, ...)
146 {
147         va_list args;
148         int error;
149
150         va_start(args, fmt);
151         error = kobject_set_name_vargs(kobj, fmt, args);
152         va_end(args);
153
154         return (error);
155 }
156
157 static int
158 kobject_add_complete(struct kobject *kobj, struct kobject *parent)
159 {
160         const struct kobj_type *t;
161         int error;
162
163         kobj->parent = parent;
164         error = sysfs_create_dir(kobj);
165         if (error == 0 && kobj->ktype && kobj->ktype->default_attrs) {
166                 struct attribute **attr;
167                 t = kobj->ktype;
168
169                 for (attr = t->default_attrs; *attr != NULL; attr++) {
170                         error = sysfs_create_file(kobj, *attr);
171                         if (error)
172                                 break;
173                 }
174                 if (error)
175                         sysfs_remove_dir(kobj);
176                 
177         }
178         return (error);
179 }
180
181 int
182 kobject_add(struct kobject *kobj, struct kobject *parent, const char *fmt, ...)
183 {
184         va_list args;
185         int error;
186
187         va_start(args, fmt);
188         error = kobject_set_name_vargs(kobj, fmt, args);
189         va_end(args);
190         if (error)
191                 return (error);
192
193         return kobject_add_complete(kobj, parent);
194 }
195
196 void
197 linux_kobject_release(struct kref *kref)
198 {
199         struct kobject *kobj;
200         char *name;
201
202         kobj = container_of(kref, struct kobject, kref);
203         sysfs_remove_dir(kobj);
204         name = kobj->name;
205         if (kobj->ktype && kobj->ktype->release)
206                 kobj->ktype->release(kobj);
207         kfree(name);
208 }
209
210 static void
211 linux_kobject_kfree(struct kobject *kobj)
212 {
213         kfree(kobj);
214 }
215
216 static void
217 linux_kobject_kfree_name(struct kobject *kobj)
218 {
219         if (kobj) {
220                 kfree(kobj->name);
221         }
222 }
223
224 const struct kobj_type linux_kfree_type = {
225         .release = linux_kobject_kfree
226 };
227
228 static void
229 linux_device_release(struct device *dev)
230 {
231         pr_debug("linux_device_release: %s\n", dev_name(dev));
232         kfree(dev);
233 }
234
235 static ssize_t
236 linux_class_show(struct kobject *kobj, struct attribute *attr, char *buf)
237 {
238         struct class_attribute *dattr;
239         ssize_t error;
240
241         dattr = container_of(attr, struct class_attribute, attr);
242         error = -EIO;
243         if (dattr->show)
244                 error = dattr->show(container_of(kobj, struct class, kobj),
245                     dattr, buf);
246         return (error);
247 }
248
249 static ssize_t
250 linux_class_store(struct kobject *kobj, struct attribute *attr, const char *buf,
251     size_t count)
252 {
253         struct class_attribute *dattr;
254         ssize_t error;
255
256         dattr = container_of(attr, struct class_attribute, attr);
257         error = -EIO;
258         if (dattr->store)
259                 error = dattr->store(container_of(kobj, struct class, kobj),
260                     dattr, buf, count);
261         return (error);
262 }
263
264 static void
265 linux_class_release(struct kobject *kobj)
266 {
267         struct class *class;
268
269         class = container_of(kobj, struct class, kobj);
270         if (class->class_release)
271                 class->class_release(class);
272 }
273
274 static const struct sysfs_ops linux_class_sysfs = {
275         .show  = linux_class_show,
276         .store = linux_class_store,
277 };
278
279 const struct kobj_type linux_class_ktype = {
280         .release = linux_class_release,
281         .sysfs_ops = &linux_class_sysfs
282 };
283
284 static void
285 linux_dev_release(struct kobject *kobj)
286 {
287         struct device *dev;
288
289         dev = container_of(kobj, struct device, kobj);
290         /* This is the precedence defined by linux. */
291         if (dev->release)
292                 dev->release(dev);
293         else if (dev->class && dev->class->dev_release)
294                 dev->class->dev_release(dev);
295 }
296
297 static ssize_t
298 linux_dev_show(struct kobject *kobj, struct attribute *attr, char *buf)
299 {
300         struct device_attribute *dattr;
301         ssize_t error;
302
303         dattr = container_of(attr, struct device_attribute, attr);
304         error = -EIO;
305         if (dattr->show)
306                 error = dattr->show(container_of(kobj, struct device, kobj),
307                     dattr, buf);
308         return (error);
309 }
310
311 static ssize_t
312 linux_dev_store(struct kobject *kobj, struct attribute *attr, const char *buf,
313     size_t count)
314 {
315         struct device_attribute *dattr;
316         ssize_t error;
317
318         dattr = container_of(attr, struct device_attribute, attr);
319         error = -EIO;
320         if (dattr->store)
321                 error = dattr->store(container_of(kobj, struct device, kobj),
322                     dattr, buf, count);
323         return (error);
324 }
325
326 static const struct sysfs_ops linux_dev_sysfs = {
327         .show  = linux_dev_show,
328         .store = linux_dev_store,
329 };
330
331 const struct kobj_type linux_dev_ktype = {
332         .release = linux_dev_release,
333         .sysfs_ops = &linux_dev_sysfs
334 };
335
336 struct device *
337 device_create(struct class *class, struct device *parent, dev_t devt,
338     void *drvdata, const char *fmt, ...)
339 {
340         struct device *dev;
341         va_list args;
342
343         dev = kzalloc(sizeof(*dev), M_WAITOK);
344         dev->parent = parent;
345         dev->class = class;
346         dev->devt = devt;
347         dev->driver_data = drvdata;
348         dev->release = linux_device_release;
349         va_start(args, fmt);
350         kobject_set_name_vargs(&dev->kobj, fmt, args);
351         va_end(args);
352         device_register(dev);
353
354         return (dev);
355 }
356
357 int
358 kobject_init_and_add(struct kobject *kobj, const struct kobj_type *ktype,
359     struct kobject *parent, const char *fmt, ...)
360 {
361         va_list args;
362         int error;
363
364         kobject_init(kobj, ktype);
365         kobj->ktype = ktype;
366         kobj->parent = parent;
367         kobj->name = NULL;
368
369         va_start(args, fmt);
370         error = kobject_set_name_vargs(kobj, fmt, args);
371         va_end(args);
372         if (error)
373                 return (error);
374         return kobject_add_complete(kobj, parent);
375 }
376
377 static void
378 linux_file_dtor(void *cdp)
379 {
380         struct linux_file *filp;
381
382         filp = cdp;
383         filp->f_op->release(filp->f_vnode, filp);
384         vdrop(filp->f_vnode);
385         kfree(filp);
386 }
387
388 static int
389 linux_dev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
390 {
391         struct linux_cdev *ldev;
392         struct linux_file *filp;
393         struct file *file;
394         int error;
395
396         file = curthread->td_fpop;
397         ldev = dev->si_drv1;
398         if (ldev == NULL)
399                 return (ENODEV);
400         filp = kzalloc(sizeof(*filp), GFP_KERNEL);
401         filp->f_dentry = &filp->f_dentry_store;
402         filp->f_op = ldev->ops;
403         filp->f_flags = file->f_flag;
404         vhold(file->f_vnode);
405         filp->f_vnode = file->f_vnode;
406         if (filp->f_op->open) {
407                 error = -filp->f_op->open(file->f_vnode, filp);
408                 if (error) {
409                         kfree(filp);
410                         return (error);
411                 }
412         }
413         error = devfs_set_cdevpriv(filp, linux_file_dtor);
414         if (error) {
415                 filp->f_op->release(file->f_vnode, filp);
416                 kfree(filp);
417                 return (error);
418         }
419
420         return 0;
421 }
422
423 static int
424 linux_dev_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
425 {
426         struct linux_cdev *ldev;
427         struct linux_file *filp;
428         struct file *file;
429         int error;
430
431         file = curthread->td_fpop;
432         ldev = dev->si_drv1;
433         if (ldev == NULL)
434                 return (0);
435         if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
436                 return (error);
437         filp->f_flags = file->f_flag;
438         devfs_clear_cdevpriv();
439         
440
441         return (0);
442 }
443
444 static int
445 linux_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
446     struct thread *td)
447 {
448         struct linux_cdev *ldev;
449         struct linux_file *filp;
450         struct file *file;
451         int error;
452
453         file = curthread->td_fpop;
454         ldev = dev->si_drv1;
455         if (ldev == NULL)
456                 return (0);
457         if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
458                 return (error);
459         filp->f_flags = file->f_flag;
460         /*
461          * Linux does not have a generic ioctl copyin/copyout layer.  All
462          * linux ioctls must be converted to void ioctls which pass a
463          * pointer to the address of the data.  We want the actual user
464          * address so we dereference here.
465          */
466         data = *(void **)data;
467         if (filp->f_op->unlocked_ioctl)
468                 error = -filp->f_op->unlocked_ioctl(filp, cmd, (u_long)data);
469         else
470                 error = ENOTTY;
471
472         return (error);
473 }
474
475 static int
476 linux_dev_read(struct cdev *dev, struct uio *uio, int ioflag)
477 {
478         struct linux_cdev *ldev;
479         struct linux_file *filp;
480         struct file *file;
481         ssize_t bytes;
482         int error;
483
484         file = curthread->td_fpop;
485         ldev = dev->si_drv1;
486         if (ldev == NULL)
487                 return (0);
488         if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
489                 return (error);
490         filp->f_flags = file->f_flag;
491         if (uio->uio_iovcnt != 1)
492                 panic("linux_dev_read: uio %p iovcnt %d",
493                     uio, uio->uio_iovcnt);
494         if (filp->f_op->read) {
495                 bytes = filp->f_op->read(filp, uio->uio_iov->iov_base,
496                     uio->uio_iov->iov_len, &uio->uio_offset);
497                 if (bytes >= 0) {
498                         uio->uio_iov->iov_base =
499                             ((uint8_t *)uio->uio_iov->iov_base) + bytes;
500                         uio->uio_iov->iov_len -= bytes;
501                         uio->uio_resid -= bytes;
502                 } else
503                         error = -bytes;
504         } else
505                 error = ENXIO;
506
507         return (error);
508 }
509
510 static int
511 linux_dev_write(struct cdev *dev, struct uio *uio, int ioflag)
512 {
513         struct linux_cdev *ldev;
514         struct linux_file *filp;
515         struct file *file;
516         ssize_t bytes;
517         int error;
518
519         file = curthread->td_fpop;
520         ldev = dev->si_drv1;
521         if (ldev == NULL)
522                 return (0);
523         if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
524                 return (error);
525         filp->f_flags = file->f_flag;
526         if (uio->uio_iovcnt != 1)
527                 panic("linux_dev_write: uio %p iovcnt %d",
528                     uio, uio->uio_iovcnt);
529         if (filp->f_op->write) {
530                 bytes = filp->f_op->write(filp, uio->uio_iov->iov_base,
531                     uio->uio_iov->iov_len, &uio->uio_offset);
532                 if (bytes >= 0) {
533                         uio->uio_iov->iov_base =
534                             ((uint8_t *)uio->uio_iov->iov_base) + bytes;
535                         uio->uio_iov->iov_len -= bytes;
536                         uio->uio_resid -= bytes;
537                 } else
538                         error = -bytes;
539         } else
540                 error = ENXIO;
541
542         return (error);
543 }
544
545 static int
546 linux_dev_poll(struct cdev *dev, int events, struct thread *td)
547 {
548         struct linux_cdev *ldev;
549         struct linux_file *filp;
550         struct file *file;
551         int revents;
552         int error;
553
554         file = curthread->td_fpop;
555         ldev = dev->si_drv1;
556         if (ldev == NULL)
557                 return (0);
558         if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
559                 return (error);
560         filp->f_flags = file->f_flag;
561         if (filp->f_op->poll)
562                 revents = filp->f_op->poll(filp, NULL) & events;
563         else
564                 revents = 0;
565
566         return (revents);
567 }
568
569 static int
570 linux_dev_mmap_single(struct cdev *dev, vm_ooffset_t *offset,
571     vm_size_t size, struct vm_object **object, int nprot)
572 {
573         struct linux_cdev *ldev;
574         struct linux_file *filp;
575         struct file *file;
576         struct vm_area_struct vma;
577         int error;
578
579         file = curthread->td_fpop;
580         ldev = dev->si_drv1;
581         if (ldev == NULL)
582                 return (ENODEV);
583         if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
584                 return (error);
585         filp->f_flags = file->f_flag;
586         vma.vm_start = 0;
587         vma.vm_end = size;
588         vma.vm_pgoff = *offset / PAGE_SIZE;
589         vma.vm_pfn = 0;
590         vma.vm_page_prot = VM_MEMATTR_DEFAULT;
591         if (filp->f_op->mmap) {
592                 error = -filp->f_op->mmap(filp, &vma);
593                 if (error == 0) {
594                         struct sglist *sg;
595
596                         sg = sglist_alloc(1, M_WAITOK);
597                         sglist_append_phys(sg,
598                             (vm_paddr_t)vma.vm_pfn << PAGE_SHIFT, vma.vm_len);
599                         *object = vm_pager_allocate(OBJT_SG, sg, vma.vm_len,
600                             nprot, 0, curthread->td_ucred);
601                         if (*object == NULL) {
602                                 sglist_free(sg);
603                                 return (EINVAL);
604                         }
605                         *offset = 0;
606                         if (vma.vm_page_prot != VM_MEMATTR_DEFAULT) {
607                                 VM_OBJECT_WLOCK(*object);
608                                 vm_object_set_memattr(*object,
609                                     vma.vm_page_prot);
610                                 VM_OBJECT_WUNLOCK(*object);
611                         }
612                 }
613         } else
614                 error = ENODEV;
615
616         return (error);
617 }
618
619 struct cdevsw linuxcdevsw = {
620         .d_version = D_VERSION,
621         .d_flags = D_TRACKCLOSE,
622         .d_open = linux_dev_open,
623         .d_close = linux_dev_close,
624         .d_read = linux_dev_read,
625         .d_write = linux_dev_write,
626         .d_ioctl = linux_dev_ioctl,
627         .d_mmap_single = linux_dev_mmap_single,
628         .d_poll = linux_dev_poll,
629 };
630
631 static int
632 linux_file_read(struct file *file, struct uio *uio, struct ucred *active_cred,
633     int flags, struct thread *td)
634 {
635         struct linux_file *filp;
636         ssize_t bytes;
637         int error;
638
639         error = 0;
640         filp = (struct linux_file *)file->f_data;
641         filp->f_flags = file->f_flag;
642         if (uio->uio_iovcnt != 1)
643                 panic("linux_file_read: uio %p iovcnt %d",
644                     uio, uio->uio_iovcnt);
645         if (filp->f_op->read) {
646                 bytes = filp->f_op->read(filp, uio->uio_iov->iov_base,
647                     uio->uio_iov->iov_len, &uio->uio_offset);
648                 if (bytes >= 0) {
649                         uio->uio_iov->iov_base =
650                             ((uint8_t *)uio->uio_iov->iov_base) + bytes;
651                         uio->uio_iov->iov_len -= bytes;
652                         uio->uio_resid -= bytes;
653                 } else
654                         error = -bytes;
655         } else
656                 error = ENXIO;
657
658         return (error);
659 }
660
661 static int
662 linux_file_poll(struct file *file, int events, struct ucred *active_cred,
663     struct thread *td)
664 {
665         struct linux_file *filp;
666         int revents;
667
668         filp = (struct linux_file *)file->f_data;
669         filp->f_flags = file->f_flag;
670         if (filp->f_op->poll)
671                 revents = filp->f_op->poll(filp, NULL) & events;
672         else
673                 revents = 0;
674
675         return (0);
676 }
677
678 static int
679 linux_file_close(struct file *file, struct thread *td)
680 {
681         struct linux_file *filp;
682         int error;
683
684         filp = (struct linux_file *)file->f_data;
685         filp->f_flags = file->f_flag;
686         error = -filp->f_op->release(NULL, filp);
687         funsetown(&filp->f_sigio);
688         kfree(filp);
689
690         return (error);
691 }
692
693 static int
694 linux_file_ioctl(struct file *fp, u_long cmd, void *data, struct ucred *cred,
695     struct thread *td)
696 {
697         struct linux_file *filp;
698         int error;
699
700         filp = (struct linux_file *)fp->f_data;
701         filp->f_flags = fp->f_flag;
702         error = 0;
703
704         switch (cmd) {
705         case FIONBIO:
706                 break;
707         case FIOASYNC:
708                 if (filp->f_op->fasync == NULL)
709                         break;
710                 error = filp->f_op->fasync(0, filp, fp->f_flag & FASYNC);
711                 break;
712         case FIOSETOWN:
713                 error = fsetown(*(int *)data, &filp->f_sigio);
714                 if (error == 0)
715                         error = filp->f_op->fasync(0, filp,
716                             fp->f_flag & FASYNC);
717                 break;
718         case FIOGETOWN:
719                 *(int *)data = fgetown(&filp->f_sigio);
720                 break;
721         default:
722                 error = ENOTTY;
723                 break;
724         }
725         return (error);
726 }
727
728 static int
729 linux_file_stat(struct file *fp, struct stat *sb, struct ucred *active_cred,
730     struct thread *td)
731 {
732
733         return (EOPNOTSUPP);
734 }
735
736 static int
737 linux_file_fill_kinfo(struct file *fp, struct kinfo_file *kif,
738     struct filedesc *fdp)
739 {
740
741         return (0);
742 }
743
744 struct fileops linuxfileops = {
745         .fo_read = linux_file_read,
746         .fo_write = invfo_rdwr,
747         .fo_truncate = invfo_truncate,
748         .fo_kqfilter = invfo_kqfilter,
749         .fo_stat = linux_file_stat,
750         .fo_fill_kinfo = linux_file_fill_kinfo,
751         .fo_poll = linux_file_poll,
752         .fo_close = linux_file_close,
753         .fo_ioctl = linux_file_ioctl,
754         .fo_chmod = invfo_chmod,
755         .fo_chown = invfo_chown,
756         .fo_sendfile = invfo_sendfile,
757 };
758
759 /*
760  * Hash of vmmap addresses.  This is infrequently accessed and does not
761  * need to be particularly large.  This is done because we must store the
762  * caller's idea of the map size to properly unmap.
763  */
764 struct vmmap {
765         LIST_ENTRY(vmmap)       vm_next;
766         void                    *vm_addr;
767         unsigned long           vm_size;
768 };
769
770 struct vmmaphd {
771         struct vmmap *lh_first;
772 };
773 #define VMMAP_HASH_SIZE 64
774 #define VMMAP_HASH_MASK (VMMAP_HASH_SIZE - 1)
775 #define VM_HASH(addr)   ((uintptr_t)(addr) >> PAGE_SHIFT) & VMMAP_HASH_MASK
776 static struct vmmaphd vmmaphead[VMMAP_HASH_SIZE];
777 static struct mtx vmmaplock;
778
779 static void
780 vmmap_add(void *addr, unsigned long size)
781 {
782         struct vmmap *vmmap;
783
784         vmmap = kmalloc(sizeof(*vmmap), GFP_KERNEL);
785         mtx_lock(&vmmaplock);
786         vmmap->vm_size = size;
787         vmmap->vm_addr = addr;
788         LIST_INSERT_HEAD(&vmmaphead[VM_HASH(addr)], vmmap, vm_next);
789         mtx_unlock(&vmmaplock);
790 }
791
792 static struct vmmap *
793 vmmap_remove(void *addr)
794 {
795         struct vmmap *vmmap;
796
797         mtx_lock(&vmmaplock);
798         LIST_FOREACH(vmmap, &vmmaphead[VM_HASH(addr)], vm_next)
799                 if (vmmap->vm_addr == addr)
800                         break;
801         if (vmmap)
802                 LIST_REMOVE(vmmap, vm_next);
803         mtx_unlock(&vmmaplock);
804
805         return (vmmap);
806 }
807
808 #if defined(__i386__) || defined(__amd64__)
809 void *
810 _ioremap_attr(vm_paddr_t phys_addr, unsigned long size, int attr)
811 {
812         void *addr;
813
814         addr = pmap_mapdev_attr(phys_addr, size, attr);
815         if (addr == NULL)
816                 return (NULL);
817         vmmap_add(addr, size);
818
819         return (addr);
820 }
821 #endif
822
823 void
824 iounmap(void *addr)
825 {
826         struct vmmap *vmmap;
827
828         vmmap = vmmap_remove(addr);
829         if (vmmap == NULL)
830                 return;
831 #if defined(__i386__) || defined(__amd64__)
832         pmap_unmapdev((vm_offset_t)addr, vmmap->vm_size);
833 #endif
834         kfree(vmmap);
835 }
836
837
838 void *
839 vmap(struct page **pages, unsigned int count, unsigned long flags, int prot)
840 {
841         vm_offset_t off;
842         size_t size;
843
844         size = count * PAGE_SIZE;
845         off = kva_alloc(size);
846         if (off == 0)
847                 return (NULL);
848         vmmap_add((void *)off, size);
849         pmap_qenter(off, pages, count);
850
851         return ((void *)off);
852 }
853
854 void
855 vunmap(void *addr)
856 {
857         struct vmmap *vmmap;
858
859         vmmap = vmmap_remove(addr);
860         if (vmmap == NULL)
861                 return;
862         pmap_qremove((vm_offset_t)addr, vmmap->vm_size / PAGE_SIZE);
863         kva_free((vm_offset_t)addr, vmmap->vm_size);
864         kfree(vmmap);
865 }
866
867 char *
868 kvasprintf(gfp_t gfp, const char *fmt, va_list ap)
869 {
870         unsigned int len;
871         char *p;
872         va_list aq;
873
874         va_copy(aq, ap);
875         len = vsnprintf(NULL, 0, fmt, aq);
876         va_end(aq);
877
878         p = kmalloc(len + 1, gfp);
879         if (p != NULL)
880                 vsnprintf(p, len + 1, fmt, ap);
881
882         return (p);
883 }
884
885 char *
886 kasprintf(gfp_t gfp, const char *fmt, ...)
887 {
888         va_list ap;
889         char *p;
890
891         va_start(ap, fmt);
892         p = kvasprintf(gfp, fmt, ap);
893         va_end(ap);
894
895         return (p);
896 }
897
898 static int
899 linux_timer_jiffies_until(unsigned long expires)
900 {
901         int delta = expires - jiffies;
902         /* guard against already expired values */
903         if (delta < 1)
904                 delta = 1;
905         return (delta);
906 }
907
908 static void
909 linux_timer_callback_wrapper(void *context)
910 {
911         struct timer_list *timer;
912
913         timer = context;
914         timer->function(timer->data);
915 }
916
917 void
918 mod_timer(struct timer_list *timer, unsigned long expires)
919 {
920
921         timer->expires = expires;
922         callout_reset(&timer->timer_callout,                  
923             linux_timer_jiffies_until(expires),
924             &linux_timer_callback_wrapper, timer);
925 }
926
927 void
928 add_timer(struct timer_list *timer)
929 {
930
931         callout_reset(&timer->timer_callout,
932             linux_timer_jiffies_until(timer->expires),
933             &linux_timer_callback_wrapper, timer);
934 }
935
936 static void
937 linux_timer_init(void *arg)
938 {
939
940         /*
941          * Compute an internal HZ value which can divide 2**32 to
942          * avoid timer rounding problems when the tick value wraps
943          * around 2**32:
944          */
945         linux_timer_hz_mask = 1;
946         while (linux_timer_hz_mask < (unsigned long)hz)
947                 linux_timer_hz_mask *= 2;
948         linux_timer_hz_mask--;
949 }
950 SYSINIT(linux_timer, SI_SUB_DRIVERS, SI_ORDER_FIRST, linux_timer_init, NULL);
951
952 void
953 linux_complete_common(struct completion *c, int all)
954 {
955         int wakeup_swapper;
956
957         sleepq_lock(c);
958         c->done++;
959         if (all)
960                 wakeup_swapper = sleepq_broadcast(c, SLEEPQ_SLEEP, 0, 0);
961         else
962                 wakeup_swapper = sleepq_signal(c, SLEEPQ_SLEEP, 0, 0);
963         sleepq_release(c);
964         if (wakeup_swapper)
965                 kick_proc0();
966 }
967
968 /*
969  * Indefinite wait for done != 0 with or without signals.
970  */
971 long
972 linux_wait_for_common(struct completion *c, int flags)
973 {
974
975         if (flags != 0)
976                 flags = SLEEPQ_INTERRUPTIBLE | SLEEPQ_SLEEP;
977         else
978                 flags = SLEEPQ_SLEEP;
979         for (;;) {
980                 sleepq_lock(c);
981                 if (c->done)
982                         break;
983                 sleepq_add(c, NULL, "completion", flags, 0);
984                 if (flags & SLEEPQ_INTERRUPTIBLE) {
985                         if (sleepq_wait_sig(c, 0) != 0)
986                                 return (-ERESTARTSYS);
987                 } else
988                         sleepq_wait(c, 0);
989         }
990         c->done--;
991         sleepq_release(c);
992
993         return (0);
994 }
995
996 /*
997  * Time limited wait for done != 0 with or without signals.
998  */
999 long
1000 linux_wait_for_timeout_common(struct completion *c, long timeout, int flags)
1001 {
1002         long end = jiffies + timeout;
1003
1004         if (flags != 0)
1005                 flags = SLEEPQ_INTERRUPTIBLE | SLEEPQ_SLEEP;
1006         else
1007                 flags = SLEEPQ_SLEEP;
1008         for (;;) {
1009                 int ret;
1010
1011                 sleepq_lock(c);
1012                 if (c->done)
1013                         break;
1014                 sleepq_add(c, NULL, "completion", flags, 0);
1015                 sleepq_set_timeout(c, linux_timer_jiffies_until(end));
1016                 if (flags & SLEEPQ_INTERRUPTIBLE)
1017                         ret = sleepq_timedwait_sig(c, 0);
1018                 else
1019                         ret = sleepq_timedwait(c, 0);
1020                 if (ret != 0) {
1021                         /* check for timeout or signal */
1022                         if (ret == EWOULDBLOCK)
1023                                 return (0);
1024                         else
1025                                 return (-ERESTARTSYS);
1026                 }
1027         }
1028         c->done--;
1029         sleepq_release(c);
1030
1031         /* return how many jiffies are left */
1032         return (linux_timer_jiffies_until(end));
1033 }
1034
1035 int
1036 linux_try_wait_for_completion(struct completion *c)
1037 {
1038         int isdone;
1039
1040         isdone = 1;
1041         sleepq_lock(c);
1042         if (c->done)
1043                 c->done--;
1044         else
1045                 isdone = 0;
1046         sleepq_release(c);
1047         return (isdone);
1048 }
1049
1050 int
1051 linux_completion_done(struct completion *c)
1052 {
1053         int isdone;
1054
1055         isdone = 1;
1056         sleepq_lock(c);
1057         if (c->done == 0)
1058                 isdone = 0;
1059         sleepq_release(c);
1060         return (isdone);
1061 }
1062
1063 void
1064 linux_delayed_work_fn(void *arg)
1065 {
1066         struct delayed_work *work;
1067
1068         work = arg;
1069         taskqueue_enqueue(work->work.taskqueue, &work->work.work_task);
1070 }
1071
1072 void
1073 linux_work_fn(void *context, int pending)
1074 {
1075         struct work_struct *work;
1076
1077         work = context;
1078         work->fn(work);
1079 }
1080
1081 void
1082 linux_flush_fn(void *context, int pending)
1083 {
1084 }
1085
1086 struct workqueue_struct *
1087 linux_create_workqueue_common(const char *name, int cpus)
1088 {
1089         struct workqueue_struct *wq;
1090
1091         wq = kmalloc(sizeof(*wq), M_WAITOK);
1092         wq->taskqueue = taskqueue_create(name, M_WAITOK,
1093             taskqueue_thread_enqueue,  &wq->taskqueue);
1094         atomic_set(&wq->draining, 0);
1095         taskqueue_start_threads(&wq->taskqueue, cpus, PWAIT, "%s", name);
1096
1097         return (wq);
1098 }
1099
1100 void
1101 destroy_workqueue(struct workqueue_struct *wq)
1102 {
1103         taskqueue_free(wq->taskqueue);
1104         kfree(wq);
1105 }
1106
1107 static void
1108 linux_cdev_release(struct kobject *kobj)
1109 {
1110         struct linux_cdev *cdev;
1111         struct kobject *parent;
1112
1113         cdev = container_of(kobj, struct linux_cdev, kobj);
1114         parent = kobj->parent;
1115         if (cdev->cdev)
1116                 destroy_dev(cdev->cdev);
1117         kfree(cdev);
1118         kobject_put(parent);
1119 }
1120
1121 static void
1122 linux_cdev_static_release(struct kobject *kobj)
1123 {
1124         struct linux_cdev *cdev;
1125         struct kobject *parent;
1126
1127         cdev = container_of(kobj, struct linux_cdev, kobj);
1128         parent = kobj->parent;
1129         if (cdev->cdev)
1130                 destroy_dev(cdev->cdev);
1131         kobject_put(parent);
1132 }
1133
1134 const struct kobj_type linux_cdev_ktype = {
1135         .release = linux_cdev_release,
1136 };
1137
1138 const struct kobj_type linux_cdev_static_ktype = {
1139         .release = linux_cdev_static_release,
1140 };
1141
1142 static void
1143 linux_handle_ifnet_link_event(void *arg, struct ifnet *ifp, int linkstate)
1144 {
1145         struct notifier_block *nb;
1146
1147         nb = arg;
1148         if (linkstate == LINK_STATE_UP)
1149                 nb->notifier_call(nb, NETDEV_UP, ifp);
1150         else
1151                 nb->notifier_call(nb, NETDEV_DOWN, ifp);
1152 }
1153
1154 static void
1155 linux_handle_ifnet_arrival_event(void *arg, struct ifnet *ifp)
1156 {
1157         struct notifier_block *nb;
1158
1159         nb = arg;
1160         nb->notifier_call(nb, NETDEV_REGISTER, ifp);
1161 }
1162
1163 static void
1164 linux_handle_ifnet_departure_event(void *arg, struct ifnet *ifp)
1165 {
1166         struct notifier_block *nb;
1167
1168         nb = arg;
1169         nb->notifier_call(nb, NETDEV_UNREGISTER, ifp);
1170 }
1171
1172 static void
1173 linux_handle_iflladdr_event(void *arg, struct ifnet *ifp)
1174 {
1175         struct notifier_block *nb;
1176
1177         nb = arg;
1178         nb->notifier_call(nb, NETDEV_CHANGEADDR, ifp);
1179 }
1180
1181 static void
1182 linux_handle_ifaddr_event(void *arg, struct ifnet *ifp)
1183 {
1184         struct notifier_block *nb;
1185
1186         nb = arg;
1187         nb->notifier_call(nb, NETDEV_CHANGEIFADDR, ifp);
1188 }
1189
1190 int
1191 register_netdevice_notifier(struct notifier_block *nb)
1192 {
1193
1194         nb->tags[NETDEV_UP] = EVENTHANDLER_REGISTER(
1195             ifnet_link_event, linux_handle_ifnet_link_event, nb, 0);
1196         nb->tags[NETDEV_REGISTER] = EVENTHANDLER_REGISTER(
1197             ifnet_arrival_event, linux_handle_ifnet_arrival_event, nb, 0);
1198         nb->tags[NETDEV_UNREGISTER] = EVENTHANDLER_REGISTER(
1199             ifnet_departure_event, linux_handle_ifnet_departure_event, nb, 0);
1200         nb->tags[NETDEV_CHANGEADDR] = EVENTHANDLER_REGISTER(
1201             iflladdr_event, linux_handle_iflladdr_event, nb, 0);
1202
1203         return (0);
1204 }
1205
1206 int
1207 register_inetaddr_notifier(struct notifier_block *nb)
1208 {
1209
1210         nb->tags[NETDEV_CHANGEIFADDR] = EVENTHANDLER_REGISTER(
1211             ifaddr_event, linux_handle_ifaddr_event, nb, 0);
1212         return (0);
1213 }
1214
1215 int
1216 unregister_netdevice_notifier(struct notifier_block *nb)
1217 {
1218
1219         EVENTHANDLER_DEREGISTER(ifnet_link_event,
1220             nb->tags[NETDEV_UP]);
1221         EVENTHANDLER_DEREGISTER(ifnet_arrival_event,
1222             nb->tags[NETDEV_REGISTER]);
1223         EVENTHANDLER_DEREGISTER(ifnet_departure_event,
1224             nb->tags[NETDEV_UNREGISTER]);
1225         EVENTHANDLER_DEREGISTER(iflladdr_event,
1226             nb->tags[NETDEV_CHANGEADDR]);
1227
1228         return (0);
1229 }
1230
1231 int
1232 unregister_inetaddr_notifier(struct notifier_block *nb)
1233 {
1234
1235         EVENTHANDLER_DEREGISTER(ifaddr_event,
1236             nb->tags[NETDEV_CHANGEIFADDR]);
1237
1238         return (0);
1239 }
1240
1241 void
1242 linux_irq_handler(void *ent)
1243 {
1244         struct irq_ent *irqe;
1245
1246         irqe = ent;
1247         irqe->handler(irqe->irq, irqe->arg);
1248 }
1249
1250 static void
1251 linux_compat_init(void *arg)
1252 {
1253         struct sysctl_oid *rootoid;
1254         int i;
1255
1256         sx_init(&linux_global_rcu_lock, "LinuxGlobalRCU");
1257
1258         rootoid = SYSCTL_ADD_ROOT_NODE(NULL,
1259             OID_AUTO, "sys", CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, "sys");
1260         kobject_init(&linux_class_root, &linux_class_ktype);
1261         kobject_set_name(&linux_class_root, "class");
1262         linux_class_root.oidp = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(rootoid),
1263             OID_AUTO, "class", CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, "class");
1264         kobject_init(&linux_root_device.kobj, &linux_dev_ktype);
1265         kobject_set_name(&linux_root_device.kobj, "device");
1266         linux_root_device.kobj.oidp = SYSCTL_ADD_NODE(NULL,
1267             SYSCTL_CHILDREN(rootoid), OID_AUTO, "device", CTLFLAG_RD, NULL,
1268             "device");
1269         linux_root_device.bsddev = root_bus;
1270         linux_class_misc.name = "misc";
1271         class_register(&linux_class_misc);
1272         INIT_LIST_HEAD(&pci_drivers);
1273         INIT_LIST_HEAD(&pci_devices);
1274         spin_lock_init(&pci_lock);
1275         mtx_init(&vmmaplock, "IO Map lock", NULL, MTX_DEF);
1276         for (i = 0; i < VMMAP_HASH_SIZE; i++)
1277                 LIST_INIT(&vmmaphead[i]);
1278 }
1279 SYSINIT(linux_compat, SI_SUB_DRIVERS, SI_ORDER_SECOND, linux_compat_init, NULL);
1280
1281 static void
1282 linux_compat_uninit(void *arg)
1283 {
1284         linux_kobject_kfree_name(&linux_class_root);
1285         linux_kobject_kfree_name(&linux_root_device.kobj);
1286         linux_kobject_kfree_name(&linux_class_misc.kobj);
1287
1288         synchronize_rcu();
1289         sx_destroy(&linux_global_rcu_lock);
1290 }
1291 SYSUNINIT(linux_compat, SI_SUB_DRIVERS, SI_ORDER_SECOND, linux_compat_uninit, NULL);
1292
1293 /*
1294  * NOTE: Linux frequently uses "unsigned long" for pointer to integer
1295  * conversion and vice versa, where in FreeBSD "uintptr_t" would be
1296  * used. Assert these types have the same size, else some parts of the
1297  * LinuxKPI may not work like expected:
1298  */
1299 CTASSERT(sizeof(unsigned long) == sizeof(uintptr_t));