diff --git a/sys/compat/linuxkpi/common/include/linux/compiler.h b/sys/compat/linuxkpi/common/include/linux/compiler.h index 59c7d3cc04e7..d59e6faed12d 100644 --- a/sys/compat/linuxkpi/common/include/linux/compiler.h +++ b/sys/compat/linuxkpi/common/include/linux/compiler.h @@ -1,124 +1,127 @@ /*- * Copyright (c) 2010 Isilon Systems, Inc. * Copyright (c) 2010 iX Systems, Inc. * Copyright (c) 2010 Panasas, Inc. * Copyright (c) 2013-2016 Mellanox Technologies, Ltd. * Copyright (c) 2015 François Tigeot * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice unmodified, this list of conditions, and the following * disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifndef _LINUXKPI_LINUX_COMPILER_H_ #define _LINUXKPI_LINUX_COMPILER_H_ #include #define __user #define __kernel #define __safe #define __force #define __nocast #define __iomem #define __chk_user_ptr(x) ((void)0) #define __chk_io_ptr(x) ((void)0) #define __builtin_warning(x, y...) (1) #define __acquires(x) #define __releases(x) #define __acquire(x) do { } while (0) #define __release(x) do { } while (0) #define __cond_lock(x,c) (c) #define __bitwise #define __devinitdata #define __deprecated #define __init #define __initconst #define __devinit #define __devexit #define __exit #define __rcu #define __percpu #define __weak __weak_symbol #define __malloc #define __attribute_const__ __attribute__((__const__)) #undef __always_inline #define __always_inline inline #define noinline __noinline #define noinline_for_stack __noinline #define ____cacheline_aligned __aligned(CACHE_LINE_SIZE) #define ____cacheline_aligned_in_smp __aligned(CACHE_LINE_SIZE) #define fallthrough /* FALLTHROUGH */ do { } while(0) #if __has_attribute(__nonstring__) #define __nonstring __attribute__((__nonstring__)) #else #define __nonstring #endif #if __has_attribute(__counted_by__) #define __counted_by(_x) __attribute__((__counted_by__(_x))) #else #define __counted_by(_x) #endif #define likely(x) __builtin_expect(!!(x), 1) #define unlikely(x) __builtin_expect(!!(x), 0) #define typeof(x) __typeof(x) #define uninitialized_var(x) x = x #define __maybe_unused __unused #define __always_unused __unused #define __must_check __result_use_check #define __printf(a,b) __printflike(a,b) #define barrier() __asm__ __volatile__("": : :"memory") #define lower_32_bits(n) ((u32)(n)) #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16)) #define ___PASTE(a,b) a##b #define __PASTE(a,b) ___PASTE(a,b) #define WRITE_ONCE(x,v) do { \ barrier(); \ (*(volatile __typeof(x) *)(uintptr_t)&(x)) = (v); \ barrier(); \ } while (0) #define READ_ONCE(x) ({ \ __typeof(x) __var = ({ \ barrier(); \ (*(const volatile __typeof(x) *)&(x)); \ }); \ barrier(); \ __var; \ }) #define lockless_dereference(p) READ_ONCE(p) #define _AT(T,X) ((T)(X)) #define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b)) #define __must_be_array(a) __same_type(a, &(a)[0]) #define sizeof_field(_s, _m) sizeof(((_s *)0)->_m) +#define is_signed_type(t) ((t)-1 < (t)1) +#define is_unsigned_type(t) ((t)-1 > (t)1) + #endif /* _LINUXKPI_LINUX_COMPILER_H_ */ diff --git a/sys/compat/linuxkpi/common/include/linux/device.h b/sys/compat/linuxkpi/common/include/linux/device.h index 4ac041298ab7..770c5c334c9a 100644 --- a/sys/compat/linuxkpi/common/include/linux/device.h +++ b/sys/compat/linuxkpi/common/include/linux/device.h @@ -1,699 +1,700 @@ /*- * Copyright (c) 2010 Isilon Systems, Inc. * Copyright (c) 2010 iX Systems, Inc. * Copyright (c) 2010 Panasas, Inc. * Copyright (c) 2013-2016 Mellanox Technologies, Ltd. * All rights reserved. * Copyright (c) 2021-2022 The FreeBSD Foundation * * Portions of this software were developed by Björn Zeeb * under sponsorship from the FreeBSD Foundation. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice unmodified, this list of conditions, and the following * disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifndef _LINUXKPI_LINUX_DEVICE_H_ #define _LINUXKPI_LINUX_DEVICE_H_ #include #include #include #include #include #include #include #include #include #include #include #include +#include #include /* via linux/dev_printk.h */ #include #include #include #include struct device; struct class { const char *name; struct module *owner; struct kobject kobj; devclass_t bsdclass; const struct dev_pm_ops *pm; const struct attribute_group **dev_groups; void (*class_release)(struct class *class); void (*dev_release)(struct device *dev); char * (*devnode)(struct device *dev, umode_t *mode); }; struct dev_pm_ops { int (*prepare)(struct device *dev); void (*complete)(struct device *dev); int (*suspend)(struct device *dev); int (*suspend_late)(struct device *dev); int (*resume)(struct device *dev); int (*resume_early)(struct device *dev); int (*freeze)(struct device *dev); int (*freeze_late)(struct device *dev); int (*thaw)(struct device *dev); int (*thaw_early)(struct device *dev); int (*poweroff)(struct device *dev); int (*poweroff_late)(struct device *dev); int (*restore)(struct device *dev); int (*restore_early)(struct device *dev); int (*runtime_suspend)(struct device *dev); int (*runtime_resume)(struct device *dev); int (*runtime_idle)(struct device *dev); }; struct device_driver { const char *name; const struct dev_pm_ops *pm; }; struct device_type { const char *name; }; struct device { struct device *parent; struct list_head irqents; device_t bsddev; /* * The following flag is used to determine if the LinuxKPI is * responsible for detaching the BSD device or not. If the * LinuxKPI got the BSD device using devclass_get_device(), it * must not try to detach or delete it, because it's already * done somewhere else. */ bool bsddev_attached_here; struct device_driver *driver; struct device_type *type; dev_t devt; struct class *class; void (*release)(struct device *dev); struct kobject kobj; void *dma_priv; void *driver_data; unsigned int irq; #define LINUX_IRQ_INVALID 65535 unsigned int irq_start; unsigned int irq_end; const struct attribute_group **groups; struct fwnode_handle *fwnode; struct cdev *backlight_dev; struct backlight_device *bd; spinlock_t devres_lock; struct list_head devres_head; struct dev_pm_info power; }; extern struct device linux_root_device; extern struct kobject linux_class_root; extern const struct kobj_type linux_dev_ktype; extern const struct kobj_type linux_class_ktype; struct class_attribute { struct attribute attr; ssize_t (*show)(struct class *, struct class_attribute *, char *); ssize_t (*store)(struct class *, struct class_attribute *, const char *, size_t); const void *(*namespace)(struct class *, const struct class_attribute *); }; #define CLASS_ATTR(_name, _mode, _show, _store) \ struct class_attribute class_attr_##_name = \ { { #_name, NULL, _mode }, _show, _store } struct device_attribute { struct attribute attr; ssize_t (*show)(struct device *, struct device_attribute *, char *); ssize_t (*store)(struct device *, struct device_attribute *, const char *, size_t); }; #define DEVICE_ATTR(_name, _mode, _show, _store) \ struct device_attribute dev_attr_##_name = \ __ATTR(_name, _mode, _show, _store) #define DEVICE_ATTR_RO(_name) \ struct device_attribute dev_attr_##_name = __ATTR_RO(_name) #define DEVICE_ATTR_WO(_name) \ struct device_attribute dev_attr_##_name = __ATTR_WO(_name) #define DEVICE_ATTR_RW(_name) \ struct device_attribute dev_attr_##_name = __ATTR_RW(_name) /* Simple class attribute that is just a static string */ struct class_attribute_string { struct class_attribute attr; char *str; }; static inline ssize_t show_class_attr_string(struct class *class, struct class_attribute *attr, char *buf) { struct class_attribute_string *cs; cs = container_of(attr, struct class_attribute_string, attr); return snprintf(buf, PAGE_SIZE, "%s\n", cs->str); } /* Currently read-only only */ #define _CLASS_ATTR_STRING(_name, _mode, _str) \ { __ATTR(_name, _mode, show_class_attr_string, NULL), _str } #define CLASS_ATTR_STRING(_name, _mode, _str) \ struct class_attribute_string class_attr_##_name = \ _CLASS_ATTR_STRING(_name, _mode, _str) #define dev_printk(lvl, dev, fmt, ...) \ device_printf((dev)->bsddev, fmt, ##__VA_ARGS__) #define dev_emerg(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__) #define dev_alert(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__) #define dev_crit(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__) #define dev_err(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__) #define dev_warn(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__) #define dev_notice(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__) #define dev_info(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__) #define dev_dbg(dev, fmt, ...) do { } while (0) #define dev_WARN(dev, fmt, ...) \ device_printf((dev)->bsddev, "%s:%d: " fmt, __func__, __LINE__, ##__VA_ARGS__) #define dev_WARN_ONCE(dev, condition, fmt, ...) do { \ static bool __dev_WARN_ONCE; \ bool __ret_warn_on = (condition); \ if (unlikely(__ret_warn_on)) { \ if (!__dev_WARN_ONCE) { \ __dev_WARN_ONCE = true; \ device_printf((dev)->bsddev, "%s:%d: " fmt, __func__, __LINE__, ##__VA_ARGS__); \ } \ } \ } while (0) #define dev_info_once(dev, ...) do { \ static bool __dev_info_once; \ if (!__dev_info_once) { \ __dev_info_once = true; \ dev_info(dev, __VA_ARGS__); \ } \ } while (0) #define dev_warn_once(dev, ...) do { \ static bool __dev_warn_once; \ if (!__dev_warn_once) { \ __dev_warn_once = 1; \ dev_warn(dev, __VA_ARGS__); \ } \ } while (0) #define dev_err_once(dev, ...) do { \ static bool __dev_err_once; \ if (!__dev_err_once) { \ __dev_err_once = 1; \ dev_err(dev, __VA_ARGS__); \ } \ } while (0) #define dev_dbg_once(dev, ...) do { \ static bool __dev_dbg_once; \ if (!__dev_dbg_once) { \ __dev_dbg_once = 1; \ dev_dbg(dev, __VA_ARGS__); \ } \ } while (0) #define dev_err_ratelimited(dev, ...) do { \ static linux_ratelimit_t __ratelimited; \ if (linux_ratelimited(&__ratelimited)) \ dev_err(dev, __VA_ARGS__); \ } while (0) #define dev_warn_ratelimited(dev, ...) do { \ static linux_ratelimit_t __ratelimited; \ if (linux_ratelimited(&__ratelimited)) \ dev_warn(dev, __VA_ARGS__); \ } while (0) #define dev_dbg_ratelimited(dev, ...) do { \ static linux_ratelimit_t __ratelimited; \ if (linux_ratelimited(&__ratelimited)) \ dev_dbg(dev, __VA_ARGS__); \ } while (0) /* Public and LinuxKPI internal devres functions. */ void *lkpi_devres_alloc(void(*release)(struct device *, void *), size_t, gfp_t); void lkpi_devres_add(struct device *, void *); void lkpi_devres_free(void *); void *lkpi_devres_find(struct device *, void(*release)(struct device *, void *), int (*match)(struct device *, void *, void *), void *); int lkpi_devres_destroy(struct device *, void(*release)(struct device *, void *), int (*match)(struct device *, void *, void *), void *); #define devres_alloc(_r, _s, _g) lkpi_devres_alloc(_r, _s, _g) #define devres_add(_d, _p) lkpi_devres_add(_d, _p) #define devres_free(_p) lkpi_devres_free(_p) #define devres_find(_d, _rfn, _mfn, _mp) \ lkpi_devres_find(_d, _rfn, _mfn, _mp) #define devres_destroy(_d, _rfn, _mfn, _mp) \ lkpi_devres_destroy(_d, _rfn, _mfn, _mp) void lkpi_devres_release_free_list(struct device *); void lkpi_devres_unlink(struct device *, void *); void lkpi_devm_kmalloc_release(struct device *, void *); #define devm_kfree(_d, _p) lkpi_devm_kmalloc_release(_d, _p) static inline const char * dev_driver_string(const struct device *dev) { driver_t *drv; const char *str = ""; if (dev->bsddev != NULL) { drv = device_get_driver(dev->bsddev); if (drv != NULL) str = drv->name; } return (str); } static inline void * dev_get_drvdata(const struct device *dev) { return dev->driver_data; } static inline void dev_set_drvdata(struct device *dev, void *data) { dev->driver_data = data; } static inline struct device * get_device(struct device *dev) { if (dev) kobject_get(&dev->kobj); return (dev); } static inline char * dev_name(const struct device *dev) { return kobject_name(&dev->kobj); } #define dev_set_name(_dev, _fmt, ...) \ kobject_set_name(&(_dev)->kobj, (_fmt), ##__VA_ARGS__) static inline void put_device(struct device *dev) { if (dev) kobject_put(&dev->kobj); } struct class *class_create(struct module *owner, const char *name); static inline int class_register(struct class *class) { class->bsdclass = devclass_create(class->name); kobject_init(&class->kobj, &linux_class_ktype); kobject_set_name(&class->kobj, class->name); kobject_add(&class->kobj, &linux_class_root, class->name); return (0); } static inline void class_unregister(struct class *class) { kobject_put(&class->kobj); } static inline struct device *kobj_to_dev(struct kobject *kobj) { return container_of(kobj, struct device, kobj); } struct device *device_create(struct class *class, struct device *parent, dev_t devt, void *drvdata, const char *fmt, ...); struct device *device_create_groups_vargs(struct class *class, struct device *parent, dev_t devt, void *drvdata, const struct attribute_group **groups, const char *fmt, va_list args); /* * Devices are registered and created for exporting to sysfs. Create * implies register and register assumes the device fields have been * setup appropriately before being called. */ static inline void device_initialize(struct device *dev) { device_t bsddev = NULL; int unit = -1; if (dev->devt) { unit = MINOR(dev->devt); bsddev = devclass_get_device(dev->class->bsdclass, unit); dev->bsddev_attached_here = false; } else if (dev->parent == NULL) { bsddev = devclass_get_device(dev->class->bsdclass, 0); dev->bsddev_attached_here = false; } else { dev->bsddev_attached_here = true; } if (bsddev == NULL && dev->parent != NULL) { bsddev = device_add_child(dev->parent->bsddev, dev->class->kobj.name, unit); } if (bsddev != NULL) device_set_softc(bsddev, dev); dev->bsddev = bsddev; MPASS(dev->bsddev != NULL); kobject_init(&dev->kobj, &linux_dev_ktype); spin_lock_init(&dev->devres_lock); INIT_LIST_HEAD(&dev->devres_head); } static inline int device_add(struct device *dev) { if (dev->bsddev != NULL) { if (dev->devt == 0) dev->devt = makedev(0, device_get_unit(dev->bsddev)); } kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev)); if (dev->groups) return (sysfs_create_groups(&dev->kobj, dev->groups)); return (0); } static inline void device_create_release(struct device *dev) { kfree(dev); } static inline struct device * device_create_with_groups(struct class *class, struct device *parent, dev_t devt, void *drvdata, const struct attribute_group **groups, const char *fmt, ...) { va_list vargs; struct device *dev; va_start(vargs, fmt); dev = device_create_groups_vargs(class, parent, devt, drvdata, groups, fmt, vargs); va_end(vargs); return dev; } static inline bool device_is_registered(struct device *dev) { return (dev->bsddev != NULL); } static inline int device_register(struct device *dev) { device_t bsddev = NULL; int unit = -1; if (device_is_registered(dev)) goto done; if (dev->devt) { unit = MINOR(dev->devt); bsddev = devclass_get_device(dev->class->bsdclass, unit); dev->bsddev_attached_here = false; } else if (dev->parent == NULL) { bsddev = devclass_get_device(dev->class->bsdclass, 0); dev->bsddev_attached_here = false; } else { dev->bsddev_attached_here = true; } if (bsddev == NULL && dev->parent != NULL) { bsddev = device_add_child(dev->parent->bsddev, dev->class->kobj.name, unit); } if (bsddev != NULL) { if (dev->devt == 0) dev->devt = makedev(0, device_get_unit(bsddev)); device_set_softc(bsddev, dev); } dev->bsddev = bsddev; done: kobject_init(&dev->kobj, &linux_dev_ktype); kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev)); sysfs_create_groups(&dev->kobj, dev->class->dev_groups); return (0); } static inline void device_unregister(struct device *dev) { device_t bsddev; sysfs_remove_groups(&dev->kobj, dev->class->dev_groups); bsddev = dev->bsddev; dev->bsddev = NULL; if (bsddev != NULL && dev->bsddev_attached_here) { bus_topo_lock(); device_delete_child(device_get_parent(bsddev), bsddev); bus_topo_unlock(); } put_device(dev); } static inline void device_del(struct device *dev) { device_t bsddev; bsddev = dev->bsddev; dev->bsddev = NULL; if (bsddev != NULL && dev->bsddev_attached_here) { bus_topo_lock(); device_delete_child(device_get_parent(bsddev), bsddev); bus_topo_unlock(); } } static inline void device_destroy(struct class *class, dev_t devt) { device_t bsddev; int unit; unit = MINOR(devt); bsddev = devclass_get_device(class->bsdclass, unit); if (bsddev != NULL) device_unregister(device_get_softc(bsddev)); } static inline void device_release_driver(struct device *dev) { #if 0 /* This leads to panics. Disable temporarily. Keep to rework. */ /* We also need to cleanup LinuxKPI bits. What else? */ lkpi_devres_release_free_list(dev); dev_set_drvdata(dev, NULL); /* Do not call dev->release! */ bus_topo_lock(); if (device_is_attached(dev->bsddev)) device_detach(dev->bsddev); bus_topo_unlock(); #endif } static inline int device_reprobe(struct device *dev) { int error; device_release_driver(dev); bus_topo_lock(); error = device_probe_and_attach(dev->bsddev); bus_topo_unlock(); return (-error); } static inline void device_set_wakeup_enable(struct device *dev __unused, bool enable __unused) { /* * XXX-BZ TODO This is used by wireless drivers supporting WoWLAN which * we currently do not support. */ } static inline int device_wakeup_enable(struct device *dev) { device_set_wakeup_enable(dev, true); return (0); } static inline bool device_iommu_mapped(struct device *dev __unused) { return (false); } #define dev_pm_set_driver_flags(dev, flags) do { \ } while (0) static inline void linux_class_kfree(struct class *class) { kfree(class); } static inline void class_destroy(struct class *class) { if (class == NULL) return; class_unregister(class); } static inline int device_create_file(struct device *dev, const struct device_attribute *attr) { if (dev) return sysfs_create_file(&dev->kobj, &attr->attr); return -EINVAL; } static inline void device_remove_file(struct device *dev, const struct device_attribute *attr) { if (dev) sysfs_remove_file(&dev->kobj, &attr->attr); } static inline int class_create_file(struct class *class, const struct class_attribute *attr) { if (class) return sysfs_create_file(&class->kobj, &attr->attr); return -EINVAL; } static inline void class_remove_file(struct class *class, const struct class_attribute *attr) { if (class) sysfs_remove_file(&class->kobj, &attr->attr); } #define dev_to_node(dev) linux_dev_to_node(dev) #define of_node_to_nid(node) -1 int linux_dev_to_node(struct device *); char *kvasprintf(gfp_t, const char *, va_list); char *kasprintf(gfp_t, const char *, ...); char *lkpi_devm_kasprintf(struct device *, gfp_t, const char *, ...); #define devm_kasprintf(_dev, _gfp, _fmt, ...) \ lkpi_devm_kasprintf(_dev, _gfp, _fmt, ##__VA_ARGS__) static __inline void * devm_kmalloc(struct device *dev, size_t size, gfp_t gfp) { void *p; p = lkpi_devres_alloc(lkpi_devm_kmalloc_release, size, gfp); if (p != NULL) lkpi_devres_add(dev, p); return (p); } static inline void * devm_kmemdup(struct device *dev, const void *src, size_t len, gfp_t gfp) { void *dst; if (len == 0) return (NULL); dst = devm_kmalloc(dev, len, gfp); if (dst != NULL) memcpy(dst, src, len); return (dst); } #define devm_kzalloc(_dev, _size, _gfp) \ devm_kmalloc((_dev), (_size), (_gfp) | __GFP_ZERO) #define devm_kcalloc(_dev, _sizen, _size, _gfp) \ devm_kmalloc((_dev), ((_sizen) * (_size)), (_gfp) | __GFP_ZERO) int lkpi_devm_add_action(struct device *dev, void (*action)(void *), void *data); #define devm_add_action(dev, action, data) \ lkpi_devm_add_action(dev, action, data); int lkpi_devm_add_action_or_reset(struct device *dev, void (*action)(void *), void *data); #define devm_add_action_or_reset(dev, action, data) \ lkpi_devm_add_action_or_reset(dev, action, data) #endif /* _LINUXKPI_LINUX_DEVICE_H_ */ diff --git a/sys/compat/linuxkpi/common/include/linux/kernel.h b/sys/compat/linuxkpi/common/include/linux/kernel.h index ab264d3896de..254b7729b96c 100644 --- a/sys/compat/linuxkpi/common/include/linux/kernel.h +++ b/sys/compat/linuxkpi/common/include/linux/kernel.h @@ -1,716 +1,687 @@ /*- * Copyright (c) 2010 Isilon Systems, Inc. * Copyright (c) 2010 iX Systems, Inc. * Copyright (c) 2010 Panasas, Inc. * Copyright (c) 2013-2016 Mellanox Technologies, Ltd. * Copyright (c) 2014-2015 François Tigeot * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice unmodified, this list of conditions, and the following * disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifndef _LINUXKPI_LINUX_KERNEL_H_ #define _LINUXKPI_LINUX_KERNEL_H_ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define KERN_CONT "" #define KERN_EMERG "<0>" #define KERN_ALERT "<1>" #define KERN_CRIT "<2>" #define KERN_ERR "<3>" #define KERN_WARNING "<4>" #define KERN_NOTICE "<5>" #define KERN_INFO "<6>" #define KERN_DEBUG "<7>" #define S8_C(x) x #define U8_C(x) x ## U #define S16_C(x) x #define U16_C(x) x ## U #define S32_C(x) x #define U32_C(x) x ## U #define S64_C(x) x ## LL #define U64_C(x) x ## ULL #define BUG() panic("BUG at %s:%d", __FILE__, __LINE__) #define BUG_ON(cond) do { \ if (cond) { \ panic("BUG ON %s failed at %s:%d", \ __stringify(cond), __FILE__, __LINE__); \ } \ } while (0) extern int linuxkpi_warn_dump_stack; #define WARN_ON(cond) ({ \ bool __ret = (cond); \ if (__ret) { \ printf("WARNING %s failed at %s:%d\n", \ __stringify(cond), __FILE__, __LINE__); \ if (linuxkpi_warn_dump_stack) \ linux_dump_stack(); \ } \ unlikely(__ret); \ }) #define WARN_ON_SMP(cond) WARN_ON(cond) #define WARN_ON_ONCE(cond) ({ \ static bool __warn_on_once; \ bool __ret = (cond); \ if (__ret && !__warn_on_once) { \ __warn_on_once = 1; \ printf("WARNING %s failed at %s:%d\n", \ __stringify(cond), __FILE__, __LINE__); \ if (linuxkpi_warn_dump_stack) \ linux_dump_stack(); \ } \ unlikely(__ret); \ }) #define oops_in_progress SCHEDULER_STOPPED() #undef ALIGN #define ALIGN(x, y) roundup2((x), (y)) #define ALIGN_DOWN(x, y) rounddown2(x, y) #undef PTR_ALIGN #define PTR_ALIGN(p, a) ((__typeof(p))ALIGN((uintptr_t)(p), (a))) #define IS_ALIGNED(x, a) (((x) & ((__typeof(x))(a) - 1)) == 0) #define DIV_ROUND_UP(x, n) howmany(x, n) #define __KERNEL_DIV_ROUND_UP(x, n) howmany(x, n) #define DIV_ROUND_UP_ULL(x, n) DIV_ROUND_UP((unsigned long long)(x), (n)) #define DIV_ROUND_DOWN_ULL(x, n) (((unsigned long long)(x) / (n)) * (n)) #define FIELD_SIZEOF(t, f) sizeof(((t *)0)->f) #define printk(...) printf(__VA_ARGS__) #define vprintk(f, a) vprintf(f, a) #define asm __asm extern void linux_dump_stack(void); #define dump_stack() linux_dump_stack() struct va_format { const char *fmt; va_list *va; }; static inline int vscnprintf(char *buf, size_t size, const char *fmt, va_list args) { ssize_t ssize = size; int i; i = vsnprintf(buf, size, fmt, args); return ((i >= ssize) ? (ssize - 1) : i); } static inline int scnprintf(char *buf, size_t size, const char *fmt, ...) { va_list args; int i; va_start(args, fmt); i = vscnprintf(buf, size, fmt, args); va_end(args); return (i); } /* * The "pr_debug()" and "pr_devel()" macros should produce zero code * unless DEBUG is defined: */ #ifdef DEBUG extern int linuxkpi_debug; #define pr_debug(fmt, ...) \ do { \ if (linuxkpi_debug) \ log(LOG_DEBUG, fmt, ##__VA_ARGS__); \ } while (0) #define pr_devel(fmt, ...) \ log(LOG_DEBUG, pr_fmt(fmt), ##__VA_ARGS__) #else #define pr_debug(fmt, ...) \ ({ if (0) log(LOG_DEBUG, fmt, ##__VA_ARGS__); 0; }) #define pr_devel(fmt, ...) \ ({ if (0) log(LOG_DEBUG, pr_fmt(fmt), ##__VA_ARGS__); 0; }) #endif #ifndef pr_fmt #define pr_fmt(fmt) fmt #endif /* * Print a one-time message (analogous to WARN_ONCE() et al): */ #define printk_once(...) do { \ static bool __print_once; \ \ if (!__print_once) { \ __print_once = true; \ printk(__VA_ARGS__); \ } \ } while (0) /* * Log a one-time message (analogous to WARN_ONCE() et al): */ #define log_once(level,...) do { \ static bool __log_once; \ \ if (unlikely(!__log_once)) { \ __log_once = true; \ log(level, __VA_ARGS__); \ } \ } while (0) #define pr_emerg(fmt, ...) \ log(LOG_EMERG, pr_fmt(fmt), ##__VA_ARGS__) #define pr_alert(fmt, ...) \ log(LOG_ALERT, pr_fmt(fmt), ##__VA_ARGS__) #define pr_crit(fmt, ...) \ log(LOG_CRIT, pr_fmt(fmt), ##__VA_ARGS__) #define pr_err(fmt, ...) \ log(LOG_ERR, pr_fmt(fmt), ##__VA_ARGS__) #define pr_err_once(fmt, ...) \ log_once(LOG_ERR, pr_fmt(fmt), ##__VA_ARGS__) #define pr_warning(fmt, ...) \ log(LOG_WARNING, pr_fmt(fmt), ##__VA_ARGS__) #define pr_warn(...) \ pr_warning(__VA_ARGS__) #define pr_warn_once(fmt, ...) \ log_once(LOG_WARNING, pr_fmt(fmt), ##__VA_ARGS__) #define pr_notice(fmt, ...) \ log(LOG_NOTICE, pr_fmt(fmt), ##__VA_ARGS__) #define pr_info(fmt, ...) \ log(LOG_INFO, pr_fmt(fmt), ##__VA_ARGS__) #define pr_info_once(fmt, ...) \ log_once(LOG_INFO, pr_fmt(fmt), ##__VA_ARGS__) #define pr_cont(fmt, ...) \ printk(KERN_CONT fmt, ##__VA_ARGS__) #define pr_warn_ratelimited(...) do { \ static linux_ratelimit_t __ratelimited; \ if (linux_ratelimited(&__ratelimited)) \ pr_warning(__VA_ARGS__); \ } while (0) #ifndef WARN #define WARN(condition, ...) ({ \ bool __ret_warn_on = (condition); \ if (unlikely(__ret_warn_on)) \ pr_warning(__VA_ARGS__); \ unlikely(__ret_warn_on); \ }) #endif #ifndef WARN_ONCE #define WARN_ONCE(condition, ...) ({ \ bool __ret_warn_on = (condition); \ if (unlikely(__ret_warn_on)) \ pr_warn_once(__VA_ARGS__); \ unlikely(__ret_warn_on); \ }) #endif #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) #define u64_to_user_ptr(val) ((void *)(uintptr_t)(val)) #define _RET_IP_ __builtin_return_address(0) static inline unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base) { return (strtouq(cp, endp, base)); } static inline long long simple_strtoll(const char *cp, char **endp, unsigned int base) { return (strtoq(cp, endp, base)); } static inline unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base) { return (strtoul(cp, endp, base)); } static inline long simple_strtol(const char *cp, char **endp, unsigned int base) { return (strtol(cp, endp, base)); } static inline int kstrtoul(const char *cp, unsigned int base, unsigned long *res) { char *end; *res = strtoul(cp, &end, base); /* skip newline character, if any */ if (*end == '\n') end++; if (*cp == 0 || *end != 0) return (-EINVAL); return (0); } static inline int kstrtol(const char *cp, unsigned int base, long *res) { char *end; *res = strtol(cp, &end, base); /* skip newline character, if any */ if (*end == '\n') end++; if (*cp == 0 || *end != 0) return (-EINVAL); return (0); } static inline int kstrtoint(const char *cp, unsigned int base, int *res) { char *end; long temp; *res = temp = strtol(cp, &end, base); /* skip newline character, if any */ if (*end == '\n') end++; if (*cp == 0 || *end != 0) return (-EINVAL); if (temp != (int)temp) return (-ERANGE); return (0); } static inline int kstrtouint(const char *cp, unsigned int base, unsigned int *res) { char *end; unsigned long temp; *res = temp = strtoul(cp, &end, base); /* skip newline character, if any */ if (*end == '\n') end++; if (*cp == 0 || *end != 0) return (-EINVAL); if (temp != (unsigned int)temp) return (-ERANGE); return (0); } static inline int kstrtou8(const char *cp, unsigned int base, u8 *res) { char *end; unsigned long temp; *res = temp = strtoul(cp, &end, base); /* skip newline character, if any */ if (*end == '\n') end++; if (*cp == 0 || *end != 0) return (-EINVAL); if (temp != (u8)temp) return (-ERANGE); return (0); } static inline int kstrtou16(const char *cp, unsigned int base, u16 *res) { char *end; unsigned long temp; *res = temp = strtoul(cp, &end, base); /* skip newline character, if any */ if (*end == '\n') end++; if (*cp == 0 || *end != 0) return (-EINVAL); if (temp != (u16)temp) return (-ERANGE); return (0); } static inline int kstrtou32(const char *cp, unsigned int base, u32 *res) { return (kstrtouint(cp, base, res)); } static inline int kstrtou64(const char *cp, unsigned int base, u64 *res) { char *end; *res = strtouq(cp, &end, base); /* skip newline character, if any */ if (*end == '\n') end++; if (*cp == 0 || *end != 0) return (-EINVAL); return (0); } static inline int kstrtoull(const char *cp, unsigned int base, unsigned long long *res) { return (kstrtou64(cp, base, (u64 *)res)); } static inline int kstrtobool(const char *s, bool *res) { int len; if (s == NULL || (len = strlen(s)) == 0 || res == NULL) return (-EINVAL); /* skip newline character, if any */ if (s[len - 1] == '\n') len--; if (len == 1 && strchr("yY1", s[0]) != NULL) *res = true; else if (len == 1 && strchr("nN0", s[0]) != NULL) *res = false; else if (strncasecmp("on", s, len) == 0) *res = true; else if (strncasecmp("off", s, len) == 0) *res = false; else return (-EINVAL); return (0); } static inline int kstrtobool_from_user(const char __user *s, size_t count, bool *res) { char buf[8] = {}; if (count > (sizeof(buf) - 1)) count = (sizeof(buf) - 1); if (copy_from_user(buf, s, count)) return (-EFAULT); return (kstrtobool(buf, res)); } static inline int kstrtoint_from_user(const char __user *s, size_t count, unsigned int base, int *p) { char buf[36] = {}; if (count > (sizeof(buf) - 1)) count = (sizeof(buf) - 1); if (copy_from_user(buf, s, count)) return (-EFAULT); return (kstrtoint(buf, base, p)); } static inline int kstrtouint_from_user(const char __user *s, size_t count, unsigned int base, unsigned int *p) { char buf[36] = {}; if (count > (sizeof(buf) - 1)) count = (sizeof(buf) - 1); if (copy_from_user(buf, s, count)) return (-EFAULT); return (kstrtouint(buf, base, p)); } static inline int kstrtou32_from_user(const char __user *s, size_t count, unsigned int base, unsigned int *p) { return (kstrtouint_from_user(s, count, base, p)); } static inline int kstrtou8_from_user(const char __user *s, size_t count, unsigned int base, u8 *p) { char buf[8] = {}; if (count > (sizeof(buf) - 1)) count = (sizeof(buf) - 1); if (copy_from_user(buf, s, count)) return (-EFAULT); return (kstrtou8(buf, base, p)); } #define min(x, y) ((x) < (y) ? (x) : (y)) #define max(x, y) ((x) > (y) ? (x) : (y)) #define min3(a, b, c) min(a, min(b,c)) #define max3(a, b, c) max(a, max(b,c)) #define min_t(type, x, y) ({ \ type __min1 = (x); \ type __min2 = (y); \ __min1 < __min2 ? __min1 : __min2; }) #define max_t(type, x, y) ({ \ type __max1 = (x); \ type __max2 = (y); \ __max1 > __max2 ? __max1 : __max2; }) #define offsetofend(t, m) \ (offsetof(t, m) + sizeof((((t *)0)->m))) #define clamp_t(type, _x, min, max) min_t(type, max_t(type, _x, min), max) #define clamp(x, lo, hi) min( max(x,lo), hi) #define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi) /* * This looks more complex than it should be. But we need to * get the type for the ~ right in round_down (it needs to be * as wide as the result!), and we want to evaluate the macro * arguments just once each. */ #define __round_mask(x, y) ((__typeof__(x))((y)-1)) #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1) #define round_down(x, y) ((x) & ~__round_mask(x, y)) #define smp_processor_id() PCPU_GET(cpuid) #define num_possible_cpus() mp_ncpus #define num_online_cpus() mp_ncpus #if defined(__i386__) || defined(__amd64__) extern bool linux_cpu_has_clflush; #define cpu_has_clflush linux_cpu_has_clflush #endif /* Swap values of a and b */ #define swap(a, b) do { \ typeof(a) _swap_tmp = a; \ a = b; \ b = _swap_tmp; \ } while (0) #define DIV_ROUND_CLOSEST(x, divisor) (((x) + ((divisor) / 2)) / (divisor)) #define DIV_ROUND_CLOSEST_ULL(x, divisor) ({ \ __typeof(divisor) __d = (divisor); \ unsigned long long __ret = (x) + (__d) / 2; \ __ret /= __d; \ __ret; \ }) static inline uintmax_t mult_frac(uintmax_t x, uintmax_t multiplier, uintmax_t divisor) { uintmax_t q = (x / divisor); uintmax_t r = (x % divisor); return ((q * multiplier) + ((r * multiplier) / divisor)); } typedef struct linux_ratelimit { struct timeval lasttime; int counter; } linux_ratelimit_t; static inline bool linux_ratelimited(linux_ratelimit_t *rl) { return (ppsratecheck(&rl->lasttime, &rl->counter, 1)); } -#define struct_size(ptr, field, num) ({ \ - const size_t __size = offsetof(__typeof(*(ptr)), field); \ - const size_t __max = (SIZE_MAX - __size) / sizeof((ptr)->field[0]); \ - ((num) > __max) ? SIZE_MAX : (__size + sizeof((ptr)->field[0]) * (num)); \ -}) - #define __is_constexpr(x) \ __builtin_constant_p(x) /* * The is_signed() macro below returns true if the passed data type is * signed. Else false is returned. */ #define is_signed(datatype) (((datatype)-1 / (datatype)2) == (datatype)0) -/* - * The type_max() macro below returns the maxium positive value the - * passed data type can hold. - */ -#define type_max(datatype) ( \ - (sizeof(datatype) >= 8) ? (is_signed(datatype) ? INT64_MAX : UINT64_MAX) : \ - (sizeof(datatype) >= 4) ? (is_signed(datatype) ? INT32_MAX : UINT32_MAX) : \ - (sizeof(datatype) >= 2) ? (is_signed(datatype) ? INT16_MAX : UINT16_MAX) : \ - (is_signed(datatype) ? INT8_MAX : UINT8_MAX) \ -) - -/* - * The type_min() macro below returns the minimum value the passed - * data type can hold. For unsigned types the minimum value is always - * zero. For signed types it may vary. - */ -#define type_min(datatype) ( \ - (sizeof(datatype) >= 8) ? (is_signed(datatype) ? INT64_MIN : 0) : \ - (sizeof(datatype) >= 4) ? (is_signed(datatype) ? INT32_MIN : 0) : \ - (sizeof(datatype) >= 2) ? (is_signed(datatype) ? INT16_MIN : 0) : \ - (is_signed(datatype) ? INT8_MIN : 0) \ -) - #define TAINT_WARN 0 #define test_taint(x) (0) #define add_taint(x,y) do { \ } while (0) static inline int _h2b(const char c) { if (c >= '0' && c <= '9') return (c - '0'); if (c >= 'a' && c <= 'f') return (10 + c - 'a'); if (c >= 'A' && c <= 'F') return (10 + c - 'A'); return (-EINVAL); } static inline int hex2bin(uint8_t *bindst, const char *hexsrc, size_t binlen) { int hi4, lo4; while (binlen > 0) { hi4 = _h2b(*hexsrc++); lo4 = _h2b(*hexsrc++); if (hi4 < 0 || lo4 < 0) return (-EINVAL); *bindst++ = (hi4 << 4) | lo4; binlen--; } return (0); } static inline bool mac_pton(const char *macin, uint8_t *macout) { const char *s, *d; uint8_t mac[6], hx, lx;; int i; if (strlen(macin) < (3 * 6 - 1)) return (false); i = 0; s = macin; do { /* Should we also support '-'-delimiters? */ d = strchrnul(s, ':'); hx = lx = 0; while (s < d) { /* Fail on abc:123:xxx:... */ if ((d - s) > 2) return (false); /* We do support non-well-formed strings: 3:45:6:... */ if ((d - s) > 1) { hx = _h2b(*s); if (hx < 0) return (false); s++; } lx = _h2b(*s); if (lx < 0) return (false); s++; } mac[i] = (hx << 4) | lx; i++; if (i >= 6) return (false); } while (d != NULL && *d != '\0'); memcpy(macout, mac, 6); return (true); } #define DECLARE_FLEX_ARRAY(_t, _n) \ struct { struct { } __dummy_ ## _n; _t _n[0]; } #endif /* _LINUXKPI_LINUX_KERNEL_H_ */ diff --git a/sys/compat/linuxkpi/common/include/linux/mm.h b/sys/compat/linuxkpi/common/include/linux/mm.h index 8c50a2bbb3bc..1356d64dde28 100644 --- a/sys/compat/linuxkpi/common/include/linux/mm.h +++ b/sys/compat/linuxkpi/common/include/linux/mm.h @@ -1,370 +1,371 @@ /*- * Copyright (c) 2010 Isilon Systems, Inc. * Copyright (c) 2010 iX Systems, Inc. * Copyright (c) 2010 Panasas, Inc. * Copyright (c) 2013-2017 Mellanox Technologies, Ltd. * Copyright (c) 2015 François Tigeot * Copyright (c) 2015 Matthew Dillon * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice unmodified, this list of conditions, and the following * disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifndef _LINUXKPI_LINUX_MM_H_ #define _LINUXKPI_LINUX_MM_H_ #include #include #include #include #include #include #include +#include #include #include #include #define PAGE_ALIGN(x) ALIGN(x, PAGE_SIZE) /* * Make sure our LinuxKPI defined virtual memory flags don't conflict * with the ones defined by FreeBSD: */ CTASSERT((VM_PROT_ALL & -(1 << 8)) == 0); #define VM_READ VM_PROT_READ #define VM_WRITE VM_PROT_WRITE #define VM_EXEC VM_PROT_EXECUTE #define VM_PFNINTERNAL (1 << 8) /* FreeBSD private flag to vm_insert_pfn() */ #define VM_MIXEDMAP (1 << 9) #define VM_NORESERVE (1 << 10) #define VM_PFNMAP (1 << 11) #define VM_IO (1 << 12) #define VM_MAYWRITE (1 << 13) #define VM_DONTCOPY (1 << 14) #define VM_DONTEXPAND (1 << 15) #define VM_DONTDUMP (1 << 16) #define VM_SHARED (1 << 17) #define VMA_MAX_PREFAULT_RECORD 1 #define FOLL_WRITE (1 << 0) #define FOLL_FORCE (1 << 1) #define VM_FAULT_OOM (1 << 0) #define VM_FAULT_SIGBUS (1 << 1) #define VM_FAULT_MAJOR (1 << 2) #define VM_FAULT_WRITE (1 << 3) #define VM_FAULT_HWPOISON (1 << 4) #define VM_FAULT_HWPOISON_LARGE (1 << 5) #define VM_FAULT_SIGSEGV (1 << 6) #define VM_FAULT_NOPAGE (1 << 7) #define VM_FAULT_LOCKED (1 << 8) #define VM_FAULT_RETRY (1 << 9) #define VM_FAULT_FALLBACK (1 << 10) #define VM_FAULT_ERROR (VM_FAULT_OOM | VM_FAULT_SIGBUS | VM_FAULT_SIGSEGV | \ VM_FAULT_HWPOISON |VM_FAULT_HWPOISON_LARGE | VM_FAULT_FALLBACK) #define FAULT_FLAG_WRITE (1 << 0) #define FAULT_FLAG_MKWRITE (1 << 1) #define FAULT_FLAG_ALLOW_RETRY (1 << 2) #define FAULT_FLAG_RETRY_NOWAIT (1 << 3) #define FAULT_FLAG_KILLABLE (1 << 4) #define FAULT_FLAG_TRIED (1 << 5) #define FAULT_FLAG_USER (1 << 6) #define FAULT_FLAG_REMOTE (1 << 7) #define FAULT_FLAG_INSTRUCTION (1 << 8) #define fault_flag_allow_retry_first(flags) \ (((flags) & (FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_TRIED)) == FAULT_FLAG_ALLOW_RETRY) typedef int (*pte_fn_t)(linux_pte_t *, unsigned long addr, void *data); struct vm_area_struct { vm_offset_t vm_start; vm_offset_t vm_end; vm_offset_t vm_pgoff; pgprot_t vm_page_prot; unsigned long vm_flags; struct mm_struct *vm_mm; void *vm_private_data; const struct vm_operations_struct *vm_ops; struct linux_file *vm_file; /* internal operation */ vm_paddr_t vm_pfn; /* PFN for memory map */ vm_size_t vm_len; /* length for memory map */ vm_pindex_t vm_pfn_first; int vm_pfn_count; int *vm_pfn_pcount; vm_object_t vm_obj; vm_map_t vm_cached_map; TAILQ_ENTRY(vm_area_struct) vm_entry; }; struct vm_fault { unsigned int flags; pgoff_t pgoff; union { /* user-space address */ void *virtual_address; /* < 4.11 */ unsigned long address; /* >= 4.11 */ }; struct page *page; struct vm_area_struct *vma; }; struct vm_operations_struct { void (*open) (struct vm_area_struct *); void (*close) (struct vm_area_struct *); int (*fault) (struct vm_fault *); int (*access) (struct vm_area_struct *, unsigned long, void *, int, int); }; struct sysinfo { uint64_t totalram; /* Total usable main memory size */ uint64_t freeram; /* Available memory size */ uint64_t totalhigh; /* Total high memory size */ uint64_t freehigh; /* Available high memory size */ uint32_t mem_unit; /* Memory unit size in bytes */ }; static inline struct page * virt_to_head_page(const void *p) { return (virt_to_page(p)); } /* * Compute log2 of the power of two rounded up count of pages * needed for size bytes. */ static inline int get_order(unsigned long size) { int order; size = (size - 1) >> PAGE_SHIFT; order = 0; while (size) { order++; size >>= 1; } return (order); } static inline void * lowmem_page_address(struct page *page) { return (page_address(page)); } /* * This only works via memory map operations. */ static inline int io_remap_pfn_range(struct vm_area_struct *vma, unsigned long addr, unsigned long pfn, unsigned long size, vm_memattr_t prot) { vma->vm_page_prot = prot; vma->vm_pfn = pfn; vma->vm_len = size; return (0); } vm_fault_t lkpi_vmf_insert_pfn_prot_locked(struct vm_area_struct *vma, unsigned long addr, unsigned long pfn, pgprot_t prot); static inline vm_fault_t vmf_insert_pfn_prot(struct vm_area_struct *vma, unsigned long addr, unsigned long pfn, pgprot_t prot) { vm_fault_t ret; VM_OBJECT_WLOCK(vma->vm_obj); ret = lkpi_vmf_insert_pfn_prot_locked(vma, addr, pfn, prot); VM_OBJECT_WUNLOCK(vma->vm_obj); return (ret); } #define vmf_insert_pfn_prot(...) \ _Static_assert(false, \ "This function is always called in a loop. Consider using the locked version") static inline int apply_to_page_range(struct mm_struct *mm, unsigned long address, unsigned long size, pte_fn_t fn, void *data) { return (-ENOTSUP); } int zap_vma_ptes(struct vm_area_struct *vma, unsigned long address, unsigned long size); int lkpi_remap_pfn_range(struct vm_area_struct *vma, unsigned long start_addr, unsigned long start_pfn, unsigned long size, pgprot_t prot); static inline int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr, unsigned long pfn, unsigned long size, pgprot_t prot) { return (lkpi_remap_pfn_range(vma, addr, pfn, size, prot)); } static inline unsigned long vma_pages(struct vm_area_struct *vma) { return ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT); } #define offset_in_page(off) ((unsigned long)(off) & (PAGE_SIZE - 1)) static inline void set_page_dirty(struct page *page) { vm_page_dirty(page); } static inline void mark_page_accessed(struct page *page) { vm_page_reference(page); } static inline void get_page(struct page *page) { vm_page_wire(page); } extern long get_user_pages(unsigned long start, unsigned long nr_pages, unsigned int gup_flags, struct page **, struct vm_area_struct **); static inline long pin_user_pages(unsigned long start, unsigned long nr_pages, unsigned int gup_flags, struct page **pages, struct vm_area_struct **vmas) { return get_user_pages(start, nr_pages, gup_flags, pages, vmas); } extern int __get_user_pages_fast(unsigned long start, int nr_pages, int write, struct page **); static inline int pin_user_pages_fast(unsigned long start, int nr_pages, unsigned int gup_flags, struct page **pages) { return __get_user_pages_fast( start, nr_pages, !!(gup_flags & FOLL_WRITE), pages); } extern long get_user_pages_remote(struct task_struct *, struct mm_struct *, unsigned long start, unsigned long nr_pages, unsigned int gup_flags, struct page **, struct vm_area_struct **); static inline long pin_user_pages_remote(struct task_struct *task, struct mm_struct *mm, unsigned long start, unsigned long nr_pages, unsigned int gup_flags, struct page **pages, struct vm_area_struct **vmas) { return get_user_pages_remote( task, mm, start, nr_pages, gup_flags, pages, vmas); } static inline void put_page(struct page *page) { vm_page_unwire(page, PQ_ACTIVE); } #define unpin_user_page(page) put_page(page) #define unpin_user_pages(pages, npages) release_pages(pages, npages) #define copy_highpage(to, from) pmap_copy_page(from, to) static inline pgprot_t vm_get_page_prot(unsigned long vm_flags) { return (vm_flags & VM_PROT_ALL); } static inline struct page * vmalloc_to_page(const void *addr) { vm_paddr_t paddr; paddr = pmap_kextract((vm_offset_t)addr); return (PHYS_TO_VM_PAGE(paddr)); } static inline int trylock_page(struct page *page) { return (vm_page_trylock(page)); } static inline void unlock_page(struct page *page) { vm_page_unlock(page); } extern int is_vmalloc_addr(const void *addr); void si_meminfo(struct sysinfo *si); static inline unsigned long totalram_pages(void) { return ((unsigned long)physmem); } #define unmap_mapping_range(...) lkpi_unmap_mapping_range(__VA_ARGS__) void lkpi_unmap_mapping_range(void *obj, loff_t const holebegin __unused, loff_t const holelen, int even_cows __unused); #define PAGE_ALIGNED(p) __is_aligned(p, PAGE_SIZE) void vma_set_file(struct vm_area_struct *vma, struct linux_file *file); static inline void might_alloc(gfp_t gfp_mask __unused) { } #define is_cow_mapping(flags) (false) #endif /* _LINUXKPI_LINUX_MM_H_ */ diff --git a/sys/compat/linuxkpi/common/include/linux/overflow.h b/sys/compat/linuxkpi/common/include/linux/overflow.h index bb9ac5bc5ffd..9ba9b9500f11 100644 --- a/sys/compat/linuxkpi/common/include/linux/overflow.h +++ b/sys/compat/linuxkpi/common/include/linux/overflow.h @@ -1,51 +1,349 @@ -/*- - * Copyright (c) 2020 The FreeBSD Foundation - * - * This software was developed by Emmanuel Vadot under sponsorship - * from the FreeBSD Foundation. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#ifndef __LINUXKPI_LINUX_OVERFLOW_H__ -#define __LINUXKPI_LINUX_OVERFLOW_H__ - -#include -#include - -#define check_add_overflow(a, b, c) \ - __builtin_add_overflow(a, b, c) - -#define check_mul_overflow(a, b, c) \ - __builtin_mul_overflow(a, b, c) - -static inline size_t -array_size(size_t x, size_t y) +/* SPDX-License-Identifier: GPL-2.0 OR MIT */ +#ifndef _LINUXKPI_LINUX_OVERFLOW_H +#define _LINUXKPI_LINUX_OVERFLOW_H + +#include +#include +#ifdef __linux__ +#include +#endif + +/* + * We need to compute the minimum and maximum values representable in a given + * type. These macros may also be useful elsewhere. It would seem more obvious + * to do something like: + * + * #define type_min(T) (T)(is_signed_type(T) ? (T)1 << (8*sizeof(T)-1) : 0) + * #define type_max(T) (T)(is_signed_type(T) ? ((T)1 << (8*sizeof(T)-1)) - 1 : ~(T)0) + * + * Unfortunately, the middle expressions, strictly speaking, have + * undefined behaviour, and at least some versions of gcc warn about + * the type_max expression (but not if -fsanitize=undefined is in + * effect; in that case, the warning is deferred to runtime...). + * + * The slightly excessive casting in type_min is to make sure the + * macros also produce sensible values for the exotic type _Bool. [The + * overflow checkers only almost work for _Bool, but that's + * a-feature-not-a-bug, since people shouldn't be doing arithmetic on + * _Bools. Besides, the gcc builtins don't allow _Bool* as third + * argument.] + * + * Idea stolen from + * https://mail-index.netbsd.org/tech-misc/2007/02/05/0000.html - + * credit to Christian Biere. + */ +#define __type_half_max(type) ((type)1 << (8*sizeof(type) - 1 - is_signed_type(type))) +#define type_max(T) ((T)((__type_half_max(T) - 1) + __type_half_max(T))) +#define type_min(T) ((T)((T)-type_max(T)-(T)1)) + +/* + * Avoids triggering -Wtype-limits compilation warning, + * while using unsigned data types to check a < 0. + */ +#define is_non_negative(a) ((a) > 0 || (a) == 0) +#define is_negative(a) (!(is_non_negative(a))) + +/* + * Allows for effectively applying __must_check to a macro so we can have + * both the type-agnostic benefits of the macros while also being able to + * enforce that the return value is, in fact, checked. + */ +static inline bool __must_check __must_check_overflow(bool overflow) { - size_t retval; + return unlikely(overflow); +} + +/** + * check_add_overflow() - Calculate addition with overflow checking + * @a: first addend + * @b: second addend + * @d: pointer to store sum + * + * Returns 0 on success. + * + * *@d holds the results of the attempted addition, but is not considered + * "safe for use" on a non-zero return value, which indicates that the + * sum has overflowed or been truncated. + */ +#define check_add_overflow(a, b, d) \ + __must_check_overflow(__builtin_add_overflow(a, b, d)) + +/** + * check_sub_overflow() - Calculate subtraction with overflow checking + * @a: minuend; value to subtract from + * @b: subtrahend; value to subtract from @a + * @d: pointer to store difference + * + * Returns 0 on success. + * + * *@d holds the results of the attempted subtraction, but is not considered + * "safe for use" on a non-zero return value, which indicates that the + * difference has underflowed or been truncated. + */ +#define check_sub_overflow(a, b, d) \ + __must_check_overflow(__builtin_sub_overflow(a, b, d)) + +/** + * check_mul_overflow() - Calculate multiplication with overflow checking + * @a: first factor + * @b: second factor + * @d: pointer to store product + * + * Returns 0 on success. + * + * *@d holds the results of the attempted multiplication, but is not + * considered "safe for use" on a non-zero return value, which indicates + * that the product has overflowed or been truncated. + */ +#define check_mul_overflow(a, b, d) \ + __must_check_overflow(__builtin_mul_overflow(a, b, d)) + +/** + * check_shl_overflow() - Calculate a left-shifted value and check overflow + * @a: Value to be shifted + * @s: How many bits left to shift + * @d: Pointer to where to store the result + * + * Computes *@d = (@a << @s) + * + * Returns true if '*@d' cannot hold the result or when '@a << @s' doesn't + * make sense. Example conditions: + * + * - '@a << @s' causes bits to be lost when stored in *@d. + * - '@s' is garbage (e.g. negative) or so large that the result of + * '@a << @s' is guaranteed to be 0. + * - '@a' is negative. + * - '@a << @s' sets the sign bit, if any, in '*@d'. + * + * '*@d' will hold the results of the attempted shift, but is not + * considered "safe for use" if true is returned. + */ +#define check_shl_overflow(a, s, d) __must_check_overflow(({ \ + typeof(a) _a = a; \ + typeof(s) _s = s; \ + typeof(d) _d = d; \ + u64 _a_full = _a; \ + unsigned int _to_shift = \ + is_non_negative(_s) && _s < 8 * sizeof(*d) ? _s : 0; \ + *_d = (_a_full << _to_shift); \ + (_to_shift != _s || is_negative(*_d) || is_negative(_a) || \ + (*_d >> _to_shift) != _a); \ +})) + +#define __overflows_type_constexpr(x, T) ( \ + is_unsigned_type(typeof(x)) ? \ + (x) > type_max(typeof(T)) : \ + is_unsigned_type(typeof(T)) ? \ + (x) < 0 || (x) > type_max(typeof(T)) : \ + (x) < type_min(typeof(T)) || (x) > type_max(typeof(T))) + +#define __overflows_type(x, T) ({ \ + typeof(T) v = 0; \ + check_add_overflow((x), v, &v); \ +}) + +/** + * overflows_type - helper for checking the overflows between value, variables, + * or data type + * + * @n: source constant value or variable to be checked + * @T: destination variable or data type proposed to store @x + * + * Compares the @x expression for whether or not it can safely fit in + * the storage of the type in @T. @x and @T can have different types. + * If @x is a constant expression, this will also resolve to a constant + * expression. + * + * Returns: true if overflow can occur, false otherwise. + */ +#define overflows_type(n, T) \ + __builtin_choose_expr(__is_constexpr(n), \ + __overflows_type_constexpr(n, T), \ + __overflows_type(n, T)) - if (__builtin_mul_overflow(x, y, &retval)) - retval = SIZE_MAX; - return (retval); +/** + * castable_to_type - like __same_type(), but also allows for casted literals + * + * @n: variable or constant value + * @T: variable or data type + * + * Unlike the __same_type() macro, this allows a constant value as the + * first argument. If this value would not overflow into an assignment + * of the second argument's type, it returns true. Otherwise, this falls + * back to __same_type(). + */ +#define castable_to_type(n, T) \ + __builtin_choose_expr(__is_constexpr(n), \ + !__overflows_type_constexpr(n, T), \ + __same_type(n, T)) + +/** + * size_mul() - Calculate size_t multiplication with saturation at SIZE_MAX + * @factor1: first factor + * @factor2: second factor + * + * Returns: calculate @factor1 * @factor2, both promoted to size_t, + * with any overflow causing the return value to be SIZE_MAX. The + * lvalue must be size_t to avoid implicit type conversion. + */ +static inline size_t __must_check size_mul(size_t factor1, size_t factor2) +{ + size_t bytes; + + if (check_mul_overflow(factor1, factor2, &bytes)) + return SIZE_MAX; + + return bytes; +} + +/** + * size_add() - Calculate size_t addition with saturation at SIZE_MAX + * @addend1: first addend + * @addend2: second addend + * + * Returns: calculate @addend1 + @addend2, both promoted to size_t, + * with any overflow causing the return value to be SIZE_MAX. The + * lvalue must be size_t to avoid implicit type conversion. + */ +static inline size_t __must_check size_add(size_t addend1, size_t addend2) +{ + size_t bytes; + + if (check_add_overflow(addend1, addend2, &bytes)) + return SIZE_MAX; + + return bytes; +} + +/** + * size_sub() - Calculate size_t subtraction with saturation at SIZE_MAX + * @minuend: value to subtract from + * @subtrahend: value to subtract from @minuend + * + * Returns: calculate @minuend - @subtrahend, both promoted to size_t, + * with any overflow causing the return value to be SIZE_MAX. For + * composition with the size_add() and size_mul() helpers, neither + * argument may be SIZE_MAX (or the result with be forced to SIZE_MAX). + * The lvalue must be size_t to avoid implicit type conversion. + */ +static inline size_t __must_check size_sub(size_t minuend, size_t subtrahend) +{ + size_t bytes; + + if (minuend == SIZE_MAX || subtrahend == SIZE_MAX || + check_sub_overflow(minuend, subtrahend, &bytes)) + return SIZE_MAX; + + return bytes; } -#endif /* __LINUXKPI_LINUX_OVERFLOW_H__ */ +/** + * array_size() - Calculate size of 2-dimensional array. + * @a: dimension one + * @b: dimension two + * + * Calculates size of 2-dimensional array: @a * @b. + * + * Returns: number of bytes needed to represent the array or SIZE_MAX on + * overflow. + */ +#define array_size(a, b) size_mul(a, b) + +/** + * array3_size() - Calculate size of 3-dimensional array. + * @a: dimension one + * @b: dimension two + * @c: dimension three + * + * Calculates size of 3-dimensional array: @a * @b * @c. + * + * Returns: number of bytes needed to represent the array or SIZE_MAX on + * overflow. + */ +#define array3_size(a, b, c) size_mul(size_mul(a, b), c) + +/** + * flex_array_size() - Calculate size of a flexible array member + * within an enclosing structure. + * @p: Pointer to the structure. + * @member: Name of the flexible array member. + * @count: Number of elements in the array. + * + * Calculates size of a flexible array of @count number of @member + * elements, at the end of structure @p. + * + * Return: number of bytes needed or SIZE_MAX on overflow. + */ +#define flex_array_size(p, member, count) \ + __builtin_choose_expr(__is_constexpr(count), \ + (count) * sizeof(*(p)->member) + __must_be_array((p)->member), \ + size_mul(count, sizeof(*(p)->member) + __must_be_array((p)->member))) + +/** + * struct_size() - Calculate size of structure with trailing flexible array. + * @p: Pointer to the structure. + * @member: Name of the array member. + * @count: Number of elements in the array. + * + * Calculates size of memory needed for structure of @p followed by an + * array of @count number of @member elements. + * + * Return: number of bytes needed or SIZE_MAX on overflow. + */ +#define struct_size(p, member, count) \ + __builtin_choose_expr(__is_constexpr(count), \ + sizeof(*(p)) + flex_array_size(p, member, count), \ + size_add(sizeof(*(p)), flex_array_size(p, member, count))) + +/** + * struct_size_t() - Calculate size of structure with trailing flexible array + * @type: structure type name. + * @member: Name of the array member. + * @count: Number of elements in the array. + * + * Calculates size of memory needed for structure @type followed by an + * array of @count number of @member elements. Prefer using struct_size() + * when possible instead, to keep calculations associated with a specific + * instance variable of type @type. + * + * Return: number of bytes needed or SIZE_MAX on overflow. + */ +#define struct_size_t(type, member, count) \ + struct_size((type *)NULL, member, count) + +/** + * _DEFINE_FLEX() - helper macro for DEFINE_FLEX() family. + * Enables caller macro to pass (different) initializer. + * + * @type: structure type name, including "struct" keyword. + * @name: Name for a variable to define. + * @member: Name of the array member. + * @count: Number of elements in the array; must be compile-time const. + * @initializer: initializer expression (could be empty for no init). + */ +#define _DEFINE_FLEX(type, name, member, count, initializer) \ + _Static_assert(__builtin_constant_p(count), \ + "onstack flex array members require compile-time const count"); \ + union { \ + u8 bytes[struct_size_t(type, member, count)]; \ + type obj; \ + } name##_u initializer; \ + type *name = (type *)&name##_u + +/** + * DEFINE_FLEX() - Define an on-stack instance of structure with a trailing + * flexible array member. + * + * @type: structure type name, including "struct" keyword. + * @name: Name for a variable to define. + * @member: Name of the array member. + * @count: Number of elements in the array; must be compile-time const. + * + * Define a zeroed, on-stack, instance of @type structure with a trailing + * flexible array member. + * Use __struct_size(@name) to get compile-time size of it afterwards. + */ +#define DEFINE_FLEX(type, name, member, count) \ + _DEFINE_FLEX(type, name, member, count, = {}) + +#endif /* _LINUXKPI_LINUX_OVERFLOW_H */ diff --git a/sys/compat/linuxkpi/common/include/linux/string.h b/sys/compat/linuxkpi/common/include/linux/string.h index 1fae23ad902a..32470312b78b 100644 --- a/sys/compat/linuxkpi/common/include/linux/string.h +++ b/sys/compat/linuxkpi/common/include/linux/string.h @@ -1,310 +1,311 @@ /*- * Copyright (c) 2010 Isilon Systems, Inc. * Copyright (c) 2010 iX Systems, Inc. * Copyright (c) 2010 Panasas, Inc. * Copyright (c) 2013-2017 Mellanox Technologies, Ltd. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice unmodified, this list of conditions, and the following * disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifndef _LINUXKPI_LINUX_STRING_H_ #define _LINUXKPI_LINUX_STRING_H_ #include #include #include #include #include #include #include /* for BITS_PER_LONG */ +#include #include #include #define strnicmp(...) strncasecmp(__VA_ARGS__) static inline int match_string(const char *const *table, int n, const char *key) { int i; for (i = 0; i != n && table[i] != NULL; i++) { if (strcmp(table[i], key) == 0) return (i); } return (-EINVAL); } static inline void * memdup_user(const void *ptr, size_t len) { void *retval; int error; retval = malloc(len, M_KMALLOC, M_WAITOK); error = linux_copyin(ptr, retval, len); if (error != 0) { free(retval, M_KMALLOC); return (ERR_PTR(error)); } return (retval); } static inline void * memdup_user_nul(const void *ptr, size_t len) { char *retval; int error; retval = malloc(len + 1, M_KMALLOC, M_WAITOK); error = linux_copyin(ptr, retval, len); if (error != 0) { free(retval, M_KMALLOC); return (ERR_PTR(error)); } retval[len] = '\0'; return (retval); } static inline void * kmemdup(const void *src, size_t len, gfp_t gfp) { void *dst; dst = kmalloc(len, gfp); if (dst != NULL) memcpy(dst, src, len); return (dst); } /* See slab.h for kvmalloc/kvfree(). */ static inline void * kvmemdup(const void *src, size_t len, gfp_t gfp) { void *dst; dst = kvmalloc(len, gfp); if (dst != NULL) memcpy(dst, src, len); return (dst); } static inline char * strndup_user(const char __user *ustr, long n) { if (n < 1) return (ERR_PTR(-EINVAL)); return (memdup_user_nul(ustr, n - 1)); } static inline char * kstrdup(const char *string, gfp_t gfp) { char *retval; size_t len; if (string == NULL) return (NULL); len = strlen(string) + 1; retval = kmalloc(len, gfp); if (retval != NULL) memcpy(retval, string, len); return (retval); } static inline char * kstrndup(const char *string, size_t len, gfp_t gfp) { char *retval; if (string == NULL) return (NULL); retval = kmalloc(len + 1, gfp); if (retval != NULL) strncpy(retval, string, len); return (retval); } static inline const char * kstrdup_const(const char *src, gfp_t gfp) { return (kmemdup(src, strlen(src) + 1, gfp)); } static inline char * skip_spaces(const char *str) { while (isspace(*str)) ++str; return (__DECONST(char *, str)); } static inline void * memchr_inv(const void *start, int c, size_t length) { const u8 *ptr; const u8 *end; u8 ch; ch = c; ptr = start; end = ptr + length; while (ptr != end) { if (*ptr != ch) return (__DECONST(void *, ptr)); ptr++; } return (NULL); } static inline size_t str_has_prefix(const char *str, const char *prefix) { size_t len; len = strlen(prefix); return (strncmp(str, prefix, len) == 0 ? len : 0); } static inline char * strreplace(char *str, char old, char new) { char *p; p = strchrnul(str, old); while (p != NULL && *p != '\0') { *p = new; p = strchrnul(str, old); } return (p); } static inline ssize_t strscpy(char* dst, const char* src, size_t len) { size_t i; if (len <= INT_MAX) { for (i = 0; i < len; i++) if ('\0' == (dst[i] = src[i])) return ((ssize_t)i); if (i != 0) dst[--i] = '\0'; } return (-E2BIG); } static inline ssize_t strscpy_pad(char* dst, const char* src, size_t len) { bzero(dst, len); return (strscpy(dst, src, len)); } static inline char * strnchr(const char *cp, size_t n, int ch) { char *p; for (p = __DECONST(char *, cp); n--; ++p) { if (*p == ch) return (p); if (*p == '\0') break; } return (NULL); } static inline void * memset32(uint32_t *b, uint32_t c, size_t len) { uint32_t *dst = b; while (len--) *dst++ = c; return (b); } static inline void * memset64(uint64_t *b, uint64_t c, size_t len) { uint64_t *dst = b; while (len--) *dst++ = c; return (b); } static inline void * memset_p(void **p, void *v, size_t n) { if (BITS_PER_LONG == 32) return (memset32((uint32_t *)p, (uintptr_t)v, n)); else return (memset64((uint64_t *)p, (uintptr_t)v, n)); } static inline void memcpy_and_pad(void *dst, size_t dstlen, const void *src, size_t len, int ch) { if (len >= dstlen) { memcpy(dst, src, dstlen); } else { memcpy(dst, src, len); /* Pad with given padding character. */ memset((char *)dst + len, ch, dstlen - len); } } #define memset_startat(ptr, bytepat, smember) \ ({ \ uint8_t *_ptr = (uint8_t *)(ptr); \ int _c = (int)(bytepat); \ size_t _o = offsetof(typeof(*(ptr)), smember); \ memset(_ptr + _o, _c, sizeof(*(ptr)) - _o); \ }) #define memset_after(ptr, bytepat, smember) \ ({ \ uint8_t *_ptr = (uint8_t *)(ptr); \ int _c = (int)(bytepat); \ size_t _o = offsetofend(typeof(*(ptr)), smember); \ memset(_ptr + _o, _c, sizeof(*(ptr)) - _o); \ }) static inline void memzero_explicit(void *p, size_t s) { memset(p, 0, s); __asm__ __volatile__("": :"r"(p) :"memory"); } #endif /* _LINUXKPI_LINUX_STRING_H_ */ diff --git a/sys/compat/linuxkpi/common/include/linux/vmalloc.h b/sys/compat/linuxkpi/common/include/linux/vmalloc.h index 0a5cf0d409bf..00650a2df9b6 100644 --- a/sys/compat/linuxkpi/common/include/linux/vmalloc.h +++ b/sys/compat/linuxkpi/common/include/linux/vmalloc.h @@ -1,41 +1,42 @@ /*- * Copyright (c) 2010 Isilon Systems, Inc. * Copyright (c) 2010 iX Systems, Inc. * Copyright (c) 2010 Panasas, Inc. * Copyright (c) 2013, 2014 Mellanox Technologies, Ltd. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice unmodified, this list of conditions, and the following * disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifndef _LINUXKPI_LINUX_VMALLOC_H_ #define _LINUXKPI_LINUX_VMALLOC_H_ +#include #include #define VM_MAP 0x0000 #define PAGE_KERNEL 0x0000 void *vmap(struct page **pages, unsigned int count, unsigned long flags, int prot); void vunmap(void *addr); #endif /* _LINUXKPI_LINUX_VMALLOC_H_ */