diff options
| author | Rusty Russell <rusty@rustcorp.com.au> | 2013-03-17 22:52:19 -0400 |
|---|---|---|
| committer | Rusty Russell <rusty@rustcorp.com.au> | 2013-03-19 23:30:42 -0400 |
| commit | 61d0b5a4b2777dcf5daef245e212b3c1fa8091ca (patch) | |
| tree | 647f8fd729d5400341e5701d0d1eb668aad97f89 /tools/virtio/linux | |
| parent | a9a0fef779074838230e04a322fd2bdc921f4f4f (diff) | |
tools/virtio: separate headers more.
This makes them a bit more like the kernel headers, so we can include more
real kernel headers in our tests.
In addition this means that we don't break tools/virtio with the next
patch.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Diffstat (limited to 'tools/virtio/linux')
| -rw-r--r-- | tools/virtio/linux/bug.h | 10 | ||||
| -rw-r--r-- | tools/virtio/linux/err.h | 26 | ||||
| -rw-r--r-- | tools/virtio/linux/export.h | 5 | ||||
| -rw-r--r-- | tools/virtio/linux/irqreturn.h | 1 | ||||
| -rw-r--r-- | tools/virtio/linux/kernel.h | 112 | ||||
| -rw-r--r-- | tools/virtio/linux/module.h | 1 | ||||
| -rw-r--r-- | tools/virtio/linux/printk.h | 4 | ||||
| -rw-r--r-- | tools/virtio/linux/ratelimit.h | 4 | ||||
| -rw-r--r-- | tools/virtio/linux/scatterlist.h | 173 | ||||
| -rw-r--r-- | tools/virtio/linux/types.h | 28 | ||||
| -rw-r--r-- | tools/virtio/linux/uaccess.h | 50 | ||||
| -rw-r--r-- | tools/virtio/linux/uio.h | 1 | ||||
| -rw-r--r-- | tools/virtio/linux/virtio.h | 150 | ||||
| -rw-r--r-- | tools/virtio/linux/virtio_config.h | 6 | ||||
| -rw-r--r-- | tools/virtio/linux/virtio_ring.h | 1 | ||||
| -rw-r--r-- | tools/virtio/linux/vringh.h | 1 |
16 files changed, 426 insertions, 147 deletions
diff --git a/tools/virtio/linux/bug.h b/tools/virtio/linux/bug.h new file mode 100644 index 000000000000..fb94f0787c47 --- /dev/null +++ b/tools/virtio/linux/bug.h | |||
| @@ -0,0 +1,10 @@ | |||
| 1 | #ifndef BUG_H | ||
| 2 | #define BUG_H | ||
| 3 | |||
| 4 | #define BUG_ON(__BUG_ON_cond) assert(!(__BUG_ON_cond)) | ||
| 5 | |||
| 6 | #define BUILD_BUG_ON(x) | ||
| 7 | |||
| 8 | #define BUG() abort() | ||
| 9 | |||
| 10 | #endif /* BUG_H */ | ||
diff --git a/tools/virtio/linux/err.h b/tools/virtio/linux/err.h new file mode 100644 index 000000000000..e32eff8b2a14 --- /dev/null +++ b/tools/virtio/linux/err.h | |||
| @@ -0,0 +1,26 @@ | |||
| 1 | #ifndef ERR_H | ||
| 2 | #define ERR_H | ||
| 3 | #define MAX_ERRNO 4095 | ||
| 4 | |||
| 5 | #define IS_ERR_VALUE(x) unlikely((x) >= (unsigned long)-MAX_ERRNO) | ||
| 6 | |||
| 7 | static inline void * __must_check ERR_PTR(long error) | ||
| 8 | { | ||
| 9 | return (void *) error; | ||
| 10 | } | ||
| 11 | |||
| 12 | static inline long __must_check PTR_ERR(const void *ptr) | ||
| 13 | { | ||
| 14 | return (long) ptr; | ||
| 15 | } | ||
| 16 | |||
| 17 | static inline long __must_check IS_ERR(const void *ptr) | ||
| 18 | { | ||
| 19 | return IS_ERR_VALUE((unsigned long)ptr); | ||
| 20 | } | ||
| 21 | |||
| 22 | static inline long __must_check IS_ERR_OR_NULL(const void *ptr) | ||
| 23 | { | ||
| 24 | return !ptr || IS_ERR_VALUE((unsigned long)ptr); | ||
| 25 | } | ||
| 26 | #endif /* ERR_H */ | ||
diff --git a/tools/virtio/linux/export.h b/tools/virtio/linux/export.h new file mode 100644 index 000000000000..7311d326894a --- /dev/null +++ b/tools/virtio/linux/export.h | |||
| @@ -0,0 +1,5 @@ | |||
| 1 | #define EXPORT_SYMBOL(sym) | ||
| 2 | #define EXPORT_SYMBOL_GPL(sym) | ||
| 3 | #define EXPORT_SYMBOL_GPL_FUTURE(sym) | ||
| 4 | #define EXPORT_UNUSED_SYMBOL(sym) | ||
| 5 | #define EXPORT_UNUSED_SYMBOL_GPL(sym) | ||
diff --git a/tools/virtio/linux/irqreturn.h b/tools/virtio/linux/irqreturn.h new file mode 100644 index 000000000000..a3c4e7be7089 --- /dev/null +++ b/tools/virtio/linux/irqreturn.h | |||
| @@ -0,0 +1 @@ | |||
| #include "../../../include/linux/irqreturn.h" | |||
diff --git a/tools/virtio/linux/kernel.h b/tools/virtio/linux/kernel.h new file mode 100644 index 000000000000..fba705963968 --- /dev/null +++ b/tools/virtio/linux/kernel.h | |||
| @@ -0,0 +1,112 @@ | |||
| 1 | #ifndef KERNEL_H | ||
| 2 | #define KERNEL_H | ||
| 3 | #include <stdbool.h> | ||
| 4 | #include <stdlib.h> | ||
| 5 | #include <stddef.h> | ||
| 6 | #include <stdio.h> | ||
| 7 | #include <string.h> | ||
| 8 | #include <assert.h> | ||
| 9 | #include <stdarg.h> | ||
| 10 | |||
| 11 | #include <linux/types.h> | ||
| 12 | #include <linux/printk.h> | ||
| 13 | #include <linux/bug.h> | ||
| 14 | #include <errno.h> | ||
| 15 | #include <unistd.h> | ||
| 16 | #include <asm/barrier.h> | ||
| 17 | |||
| 18 | #define CONFIG_SMP | ||
| 19 | |||
| 20 | #define PAGE_SIZE getpagesize() | ||
| 21 | #define PAGE_MASK (~(PAGE_SIZE-1)) | ||
| 22 | |||
| 23 | typedef unsigned long long dma_addr_t; | ||
| 24 | typedef size_t __kernel_size_t; | ||
| 25 | |||
| 26 | struct page { | ||
| 27 | unsigned long long dummy; | ||
| 28 | }; | ||
| 29 | |||
| 30 | /* Physical == Virtual */ | ||
| 31 | #define virt_to_phys(p) ((unsigned long)p) | ||
| 32 | #define phys_to_virt(a) ((void *)(unsigned long)(a)) | ||
| 33 | /* Page address: Virtual / 4K */ | ||
| 34 | #define page_to_phys(p) ((dma_addr_t)(unsigned long)(p)) | ||
| 35 | #define virt_to_page(p) ((struct page *)((unsigned long)p & PAGE_MASK)) | ||
| 36 | |||
| 37 | #define offset_in_page(p) (((unsigned long)p) % PAGE_SIZE) | ||
| 38 | |||
| 39 | #define __printf(a,b) __attribute__((format(printf,a,b))) | ||
| 40 | |||
| 41 | typedef enum { | ||
| 42 | GFP_KERNEL, | ||
| 43 | GFP_ATOMIC, | ||
| 44 | __GFP_HIGHMEM, | ||
| 45 | __GFP_HIGH | ||
| 46 | } gfp_t; | ||
| 47 | |||
| 48 | #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0])) | ||
| 49 | |||
| 50 | extern void *__kmalloc_fake, *__kfree_ignore_start, *__kfree_ignore_end; | ||
| 51 | static inline void *kmalloc(size_t s, gfp_t gfp) | ||
| 52 | { | ||
| 53 | if (__kmalloc_fake) | ||
| 54 | return __kmalloc_fake; | ||
| 55 | return malloc(s); | ||
| 56 | } | ||
| 57 | |||
| 58 | static inline void kfree(void *p) | ||
| 59 | { | ||
| 60 | if (p >= __kfree_ignore_start && p < __kfree_ignore_end) | ||
| 61 | return; | ||
| 62 | free(p); | ||
| 63 | } | ||
| 64 | |||
| 65 | static inline void *krealloc(void *p, size_t s, gfp_t gfp) | ||
| 66 | { | ||
| 67 | return realloc(p, s); | ||
| 68 | } | ||
| 69 | |||
| 70 | |||
| 71 | static inline unsigned long __get_free_page(gfp_t gfp) | ||
| 72 | { | ||
| 73 | void *p; | ||
| 74 | |||
| 75 | posix_memalign(&p, PAGE_SIZE, PAGE_SIZE); | ||
| 76 | return (unsigned long)p; | ||
| 77 | } | ||
| 78 | |||
| 79 | static inline void free_page(unsigned long addr) | ||
| 80 | { | ||
| 81 | free((void *)addr); | ||
| 82 | } | ||
| 83 | |||
| 84 | #define container_of(ptr, type, member) ({ \ | ||
| 85 | const typeof( ((type *)0)->member ) *__mptr = (ptr); \ | ||
| 86 | (type *)( (char *)__mptr - offsetof(type,member) );}) | ||
| 87 | |||
| 88 | #define uninitialized_var(x) x = x | ||
| 89 | |||
| 90 | # ifndef likely | ||
| 91 | # define likely(x) (__builtin_expect(!!(x), 1)) | ||
| 92 | # endif | ||
| 93 | # ifndef unlikely | ||
| 94 | # define unlikely(x) (__builtin_expect(!!(x), 0)) | ||
| 95 | # endif | ||
| 96 | |||
| 97 | #define pr_err(format, ...) fprintf (stderr, format, ## __VA_ARGS__) | ||
| 98 | #ifdef DEBUG | ||
| 99 | #define pr_debug(format, ...) fprintf (stderr, format, ## __VA_ARGS__) | ||
| 100 | #else | ||
| 101 | #define pr_debug(format, ...) do {} while (0) | ||
| 102 | #endif | ||
| 103 | #define dev_err(dev, format, ...) fprintf (stderr, format, ## __VA_ARGS__) | ||
| 104 | #define dev_warn(dev, format, ...) fprintf (stderr, format, ## __VA_ARGS__) | ||
| 105 | |||
| 106 | #define min(x, y) ({ \ | ||
| 107 | typeof(x) _min1 = (x); \ | ||
| 108 | typeof(y) _min2 = (y); \ | ||
| 109 | (void) (&_min1 == &_min2); \ | ||
| 110 | _min1 < _min2 ? _min1 : _min2; }) | ||
| 111 | |||
| 112 | #endif /* KERNEL_H */ | ||
diff --git a/tools/virtio/linux/module.h b/tools/virtio/linux/module.h index e69de29bb2d1..3039a7e972b6 100644 --- a/tools/virtio/linux/module.h +++ b/tools/virtio/linux/module.h | |||
| @@ -0,0 +1 @@ | |||
| #include <linux/export.h> | |||
diff --git a/tools/virtio/linux/printk.h b/tools/virtio/linux/printk.h new file mode 100644 index 000000000000..9f2423bd89c2 --- /dev/null +++ b/tools/virtio/linux/printk.h | |||
| @@ -0,0 +1,4 @@ | |||
| 1 | #include "../../../include/linux/kern_levels.h" | ||
| 2 | |||
| 3 | #define printk printf | ||
| 4 | #define vprintk vprintf | ||
diff --git a/tools/virtio/linux/ratelimit.h b/tools/virtio/linux/ratelimit.h new file mode 100644 index 000000000000..dcce1725f90d --- /dev/null +++ b/tools/virtio/linux/ratelimit.h | |||
| @@ -0,0 +1,4 @@ | |||
| 1 | #define DEFINE_RATELIMIT_STATE(name, interval_init, burst_init) int name = 0 | ||
| 2 | |||
| 3 | #define __ratelimit(x) (*(x)) | ||
| 4 | |||
diff --git a/tools/virtio/linux/scatterlist.h b/tools/virtio/linux/scatterlist.h new file mode 100644 index 000000000000..b2cf7d0f6133 --- /dev/null +++ b/tools/virtio/linux/scatterlist.h | |||
| @@ -0,0 +1,173 @@ | |||
| 1 | #ifndef SCATTERLIST_H | ||
| 2 | #define SCATTERLIST_H | ||
| 3 | #include <linux/kernel.h> | ||
| 4 | |||
| 5 | struct scatterlist { | ||
| 6 | unsigned long page_link; | ||
| 7 | unsigned int offset; | ||
| 8 | unsigned int length; | ||
| 9 | dma_addr_t dma_address; | ||
| 10 | }; | ||
| 11 | |||
| 12 | /* Scatterlist helpers, stolen from linux/scatterlist.h */ | ||
| 13 | #define sg_is_chain(sg) ((sg)->page_link & 0x01) | ||
| 14 | #define sg_is_last(sg) ((sg)->page_link & 0x02) | ||
| 15 | #define sg_chain_ptr(sg) \ | ||
| 16 | ((struct scatterlist *) ((sg)->page_link & ~0x03)) | ||
| 17 | |||
| 18 | /** | ||
| 19 | * sg_assign_page - Assign a given page to an SG entry | ||
| 20 | * @sg: SG entry | ||
| 21 | * @page: The page | ||
| 22 | * | ||
| 23 | * Description: | ||
| 24 | * Assign page to sg entry. Also see sg_set_page(), the most commonly used | ||
| 25 | * variant. | ||
| 26 | * | ||
| 27 | **/ | ||
| 28 | static inline void sg_assign_page(struct scatterlist *sg, struct page *page) | ||
| 29 | { | ||
| 30 | unsigned long page_link = sg->page_link & 0x3; | ||
| 31 | |||
| 32 | /* | ||
| 33 | * In order for the low bit stealing approach to work, pages | ||
| 34 | * must be aligned at a 32-bit boundary as a minimum. | ||
| 35 | */ | ||
| 36 | BUG_ON((unsigned long) page & 0x03); | ||
| 37 | #ifdef CONFIG_DEBUG_SG | ||
| 38 | BUG_ON(sg->sg_magic != SG_MAGIC); | ||
| 39 | BUG_ON(sg_is_chain(sg)); | ||
| 40 | #endif | ||
| 41 | sg->page_link = page_link | (unsigned long) page; | ||
| 42 | } | ||
| 43 | |||
| 44 | /** | ||
| 45 | * sg_set_page - Set sg entry to point at given page | ||
| 46 | * @sg: SG entry | ||
| 47 | * @page: The page | ||
| 48 | * @len: Length of data | ||
| 49 | * @offset: Offset into page | ||
| 50 | * | ||
| 51 | * Description: | ||
| 52 | * Use this function to set an sg entry pointing at a page, never assign | ||
| 53 | * the page directly. We encode sg table information in the lower bits | ||
| 54 | * of the page pointer. See sg_page() for looking up the page belonging | ||
| 55 | * to an sg entry. | ||
| 56 | * | ||
| 57 | **/ | ||
| 58 | static inline void sg_set_page(struct scatterlist *sg, struct page *page, | ||
| 59 | unsigned int len, unsigned int offset) | ||
| 60 | { | ||
| 61 | sg_assign_page(sg, page); | ||
| 62 | sg->offset = offset; | ||
| 63 | sg->length = len; | ||
| 64 | } | ||
| 65 | |||
| 66 | static inline struct page *sg_page(struct scatterlist *sg) | ||
| 67 | { | ||
| 68 | #ifdef CONFIG_DEBUG_SG | ||
| 69 | BUG_ON(sg->sg_magic != SG_MAGIC); | ||
| 70 | BUG_ON(sg_is_chain(sg)); | ||
| 71 | #endif | ||
| 72 | return (struct page *)((sg)->page_link & ~0x3); | ||
| 73 | } | ||
| 74 | |||
| 75 | /* | ||
| 76 | * Loop over each sg element, following the pointer to a new list if necessary | ||
| 77 | */ | ||
| 78 | #define for_each_sg(sglist, sg, nr, __i) \ | ||
| 79 | for (__i = 0, sg = (sglist); __i < (nr); __i++, sg = sg_next(sg)) | ||
| 80 | |||
| 81 | /** | ||
| 82 | * sg_chain - Chain two sglists together | ||
| 83 | * @prv: First scatterlist | ||
| 84 | * @prv_nents: Number of entries in prv | ||
| 85 | * @sgl: Second scatterlist | ||
| 86 | * | ||
| 87 | * Description: | ||
| 88 | * Links @prv@ and @sgl@ together, to form a longer scatterlist. | ||
| 89 | * | ||
| 90 | **/ | ||
| 91 | static inline void sg_chain(struct scatterlist *prv, unsigned int prv_nents, | ||
| 92 | struct scatterlist *sgl) | ||
| 93 | { | ||
| 94 | /* | ||
| 95 | * offset and length are unused for chain entry. Clear them. | ||
| 96 | */ | ||
| 97 | prv[prv_nents - 1].offset = 0; | ||
| 98 | prv[prv_nents - 1].length = 0; | ||
| 99 | |||
| 100 | /* | ||
| 101 | * Set lowest bit to indicate a link pointer, and make sure to clear | ||
| 102 | * the termination bit if it happens to be set. | ||
| 103 | */ | ||
| 104 | prv[prv_nents - 1].page_link = ((unsigned long) sgl | 0x01) & ~0x02; | ||
| 105 | } | ||
| 106 | |||
| 107 | /** | ||
| 108 | * sg_mark_end - Mark the end of the scatterlist | ||
| 109 | * @sg: SG entryScatterlist | ||
| 110 | * | ||
| 111 | * Description: | ||
| 112 | * Marks the passed in sg entry as the termination point for the sg | ||
| 113 | * table. A call to sg_next() on this entry will return NULL. | ||
| 114 | * | ||
| 115 | **/ | ||
| 116 | static inline void sg_mark_end(struct scatterlist *sg) | ||
| 117 | { | ||
| 118 | #ifdef CONFIG_DEBUG_SG | ||
| 119 | BUG_ON(sg->sg_magic != SG_MAGIC); | ||
| 120 | #endif | ||
| 121 | /* | ||
| 122 | * Set termination bit, clear potential chain bit | ||
| 123 | */ | ||
| 124 | sg->page_link |= 0x02; | ||
| 125 | sg->page_link &= ~0x01; | ||
| 126 | } | ||
| 127 | |||
| 128 | static inline struct scatterlist *sg_next(struct scatterlist *sg) | ||
| 129 | { | ||
| 130 | #ifdef CONFIG_DEBUG_SG | ||
| 131 | BUG_ON(sg->sg_magic != SG_MAGIC); | ||
| 132 | #endif | ||
| 133 | if (sg_is_last(sg)) | ||
| 134 | return NULL; | ||
| 135 | |||
| 136 | sg++; | ||
| 137 | if (unlikely(sg_is_chain(sg))) | ||
| 138 | sg = sg_chain_ptr(sg); | ||
| 139 | |||
| 140 | return sg; | ||
| 141 | } | ||
| 142 | |||
| 143 | static inline void sg_init_table(struct scatterlist *sgl, unsigned int nents) | ||
| 144 | { | ||
| 145 | memset(sgl, 0, sizeof(*sgl) * nents); | ||
| 146 | #ifdef CONFIG_DEBUG_SG | ||
| 147 | { | ||
| 148 | unsigned int i; | ||
| 149 | for (i = 0; i < nents; i++) | ||
| 150 | sgl[i].sg_magic = SG_MAGIC; | ||
| 151 | } | ||
| 152 | #endif | ||
| 153 | sg_mark_end(&sgl[nents - 1]); | ||
| 154 | } | ||
| 155 | |||
| 156 | static inline dma_addr_t sg_phys(struct scatterlist *sg) | ||
| 157 | { | ||
| 158 | return page_to_phys(sg_page(sg)) + sg->offset; | ||
| 159 | } | ||
| 160 | |||
| 161 | static inline void sg_set_buf(struct scatterlist *sg, const void *buf, | ||
| 162 | unsigned int buflen) | ||
| 163 | { | ||
| 164 | sg_set_page(sg, virt_to_page(buf), buflen, offset_in_page(buf)); | ||
| 165 | } | ||
| 166 | |||
| 167 | static inline void sg_init_one(struct scatterlist *sg, | ||
| 168 | const void *buf, unsigned int buflen) | ||
| 169 | { | ||
| 170 | sg_init_table(sg, 1); | ||
| 171 | sg_set_buf(sg, buf, buflen); | ||
| 172 | } | ||
| 173 | #endif /* SCATTERLIST_H */ | ||
diff --git a/tools/virtio/linux/types.h b/tools/virtio/linux/types.h new file mode 100644 index 000000000000..f8ebb9a2b3d6 --- /dev/null +++ b/tools/virtio/linux/types.h | |||
| @@ -0,0 +1,28 @@ | |||
| 1 | #ifndef TYPES_H | ||
| 2 | #define TYPES_H | ||
| 3 | #include <stdint.h> | ||
| 4 | |||
| 5 | #define __force | ||
| 6 | #define __user | ||
| 7 | #define __must_check | ||
| 8 | #define __cold | ||
| 9 | |||
| 10 | typedef uint64_t u64; | ||
| 11 | typedef int64_t s64; | ||
| 12 | typedef uint32_t u32; | ||
| 13 | typedef int32_t s32; | ||
| 14 | typedef uint16_t u16; | ||
| 15 | typedef int16_t s16; | ||
| 16 | typedef uint8_t u8; | ||
| 17 | typedef int8_t s8; | ||
| 18 | |||
| 19 | typedef uint64_t __u64; | ||
| 20 | typedef int64_t __s64; | ||
| 21 | typedef uint32_t __u32; | ||
| 22 | typedef int32_t __s32; | ||
| 23 | typedef uint16_t __u16; | ||
| 24 | typedef int16_t __s16; | ||
| 25 | typedef uint8_t __u8; | ||
| 26 | typedef int8_t __s8; | ||
| 27 | |||
| 28 | #endif /* TYPES_H */ | ||
diff --git a/tools/virtio/linux/uaccess.h b/tools/virtio/linux/uaccess.h new file mode 100644 index 000000000000..0a578fe18653 --- /dev/null +++ b/tools/virtio/linux/uaccess.h | |||
| @@ -0,0 +1,50 @@ | |||
| 1 | #ifndef UACCESS_H | ||
| 2 | #define UACCESS_H | ||
| 3 | extern void *__user_addr_min, *__user_addr_max; | ||
| 4 | |||
| 5 | #define ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x)) | ||
| 6 | |||
| 7 | static inline void __chk_user_ptr(const volatile void *p, size_t size) | ||
| 8 | { | ||
| 9 | assert(p >= __user_addr_min && p + size <= __user_addr_max); | ||
| 10 | } | ||
| 11 | |||
| 12 | #define put_user(x, ptr) \ | ||
| 13 | ({ \ | ||
| 14 | typeof(ptr) __pu_ptr = (ptr); \ | ||
| 15 | __chk_user_ptr(__pu_ptr, sizeof(*__pu_ptr)); \ | ||
| 16 | ACCESS_ONCE(*(__pu_ptr)) = x; \ | ||
| 17 | 0; \ | ||
| 18 | }) | ||
| 19 | |||
| 20 | #define get_user(x, ptr) \ | ||
| 21 | ({ \ | ||
| 22 | typeof(ptr) __pu_ptr = (ptr); \ | ||
| 23 | __chk_user_ptr(__pu_ptr, sizeof(*__pu_ptr)); \ | ||
| 24 | x = ACCESS_ONCE(*(__pu_ptr)); \ | ||
| 25 | 0; \ | ||
| 26 | }) | ||
| 27 | |||
| 28 | static void volatile_memcpy(volatile char *to, const volatile char *from, | ||
| 29 | unsigned long n) | ||
| 30 | { | ||
| 31 | while (n--) | ||
| 32 | *(to++) = *(from++); | ||
| 33 | } | ||
| 34 | |||
| 35 | static inline int copy_from_user(void *to, const void __user volatile *from, | ||
| 36 | unsigned long n) | ||
| 37 | { | ||
| 38 | __chk_user_ptr(from, n); | ||
| 39 | volatile_memcpy(to, from, n); | ||
| 40 | return 0; | ||
| 41 | } | ||
| 42 | |||
| 43 | static inline int copy_to_user(void __user volatile *to, const void *from, | ||
| 44 | unsigned long n) | ||
| 45 | { | ||
| 46 | __chk_user_ptr(to, n); | ||
| 47 | volatile_memcpy(to, from, n); | ||
| 48 | return 0; | ||
| 49 | } | ||
| 50 | #endif /* UACCESS_H */ | ||
diff --git a/tools/virtio/linux/uio.h b/tools/virtio/linux/uio.h new file mode 100644 index 000000000000..ecbdf922b0f4 --- /dev/null +++ b/tools/virtio/linux/uio.h | |||
| @@ -0,0 +1 @@ | |||
| #include "../../../include/linux/uio.h" | |||
diff --git a/tools/virtio/linux/virtio.h b/tools/virtio/linux/virtio.h index 390c4cb3b018..e4af6591f5ff 100644 --- a/tools/virtio/linux/virtio.h +++ b/tools/virtio/linux/virtio.h | |||
| @@ -1,129 +1,7 @@ | |||
| 1 | #ifndef LINUX_VIRTIO_H | 1 | #ifndef LINUX_VIRTIO_H |
| 2 | #define LINUX_VIRTIO_H | 2 | #define LINUX_VIRTIO_H |
| 3 | 3 | #include <linux/scatterlist.h> | |
| 4 | #include <stdbool.h> | 4 | #include <linux/kernel.h> |
| 5 | #include <stdlib.h> | ||
| 6 | #include <stddef.h> | ||
| 7 | #include <stdio.h> | ||
| 8 | #include <string.h> | ||
| 9 | #include <assert.h> | ||
| 10 | |||
| 11 | #include <linux/types.h> | ||
| 12 | #include <errno.h> | ||
| 13 | |||
| 14 | typedef unsigned long long dma_addr_t; | ||
| 15 | |||
| 16 | struct scatterlist { | ||
| 17 | unsigned long page_link; | ||
| 18 | unsigned int offset; | ||
| 19 | unsigned int length; | ||
| 20 | dma_addr_t dma_address; | ||
| 21 | }; | ||
| 22 | |||
| 23 | struct page { | ||
| 24 | unsigned long long dummy; | ||
| 25 | }; | ||
| 26 | |||
| 27 | #define BUG_ON(__BUG_ON_cond) assert(!(__BUG_ON_cond)) | ||
| 28 | |||
| 29 | /* Physical == Virtual */ | ||
| 30 | #define virt_to_phys(p) ((unsigned long)p) | ||
| 31 | #define phys_to_virt(a) ((void *)(unsigned long)(a)) | ||
| 32 | /* Page address: Virtual / 4K */ | ||
| 33 | #define virt_to_page(p) ((struct page*)((virt_to_phys(p) / 4096) * \ | ||
| 34 | sizeof(struct page))) | ||
| 35 | #define offset_in_page(p) (((unsigned long)p) % 4096) | ||
| 36 | #define sg_phys(sg) ((sg->page_link & ~0x3) / sizeof(struct page) * 4096 + \ | ||
| 37 | sg->offset) | ||
| 38 | static inline void sg_mark_end(struct scatterlist *sg) | ||
| 39 | { | ||
| 40 | /* | ||
| 41 | * Set termination bit, clear potential chain bit | ||
| 42 | */ | ||
| 43 | sg->page_link |= 0x02; | ||
| 44 | sg->page_link &= ~0x01; | ||
| 45 | } | ||
| 46 | static inline void sg_init_table(struct scatterlist *sgl, unsigned int nents) | ||
| 47 | { | ||
| 48 | memset(sgl, 0, sizeof(*sgl) * nents); | ||
| 49 | sg_mark_end(&sgl[nents - 1]); | ||
| 50 | } | ||
| 51 | static inline void sg_assign_page(struct scatterlist *sg, struct page *page) | ||
| 52 | { | ||
| 53 | unsigned long page_link = sg->page_link & 0x3; | ||
| 54 | |||
| 55 | /* | ||
| 56 | * In order for the low bit stealing approach to work, pages | ||
| 57 | * must be aligned at a 32-bit boundary as a minimum. | ||
| 58 | */ | ||
| 59 | BUG_ON((unsigned long) page & 0x03); | ||
| 60 | sg->page_link = page_link | (unsigned long) page; | ||
| 61 | } | ||
| 62 | |||
| 63 | static inline void sg_set_page(struct scatterlist *sg, struct page *page, | ||
| 64 | unsigned int len, unsigned int offset) | ||
| 65 | { | ||
| 66 | sg_assign_page(sg, page); | ||
| 67 | sg->offset = offset; | ||
| 68 | sg->length = len; | ||
| 69 | } | ||
| 70 | |||
| 71 | static inline void sg_set_buf(struct scatterlist *sg, const void *buf, | ||
| 72 | unsigned int buflen) | ||
| 73 | { | ||
| 74 | sg_set_page(sg, virt_to_page(buf), buflen, offset_in_page(buf)); | ||
| 75 | } | ||
| 76 | |||
| 77 | static inline void sg_init_one(struct scatterlist *sg, const void *buf, unsigned int buflen) | ||
| 78 | { | ||
| 79 | sg_init_table(sg, 1); | ||
| 80 | sg_set_buf(sg, buf, buflen); | ||
| 81 | } | ||
| 82 | |||
| 83 | typedef __u16 u16; | ||
| 84 | |||
| 85 | typedef enum { | ||
| 86 | GFP_KERNEL, | ||
| 87 | GFP_ATOMIC, | ||
| 88 | __GFP_HIGHMEM, | ||
| 89 | __GFP_HIGH | ||
| 90 | } gfp_t; | ||
| 91 | typedef enum { | ||
| 92 | IRQ_NONE, | ||
| 93 | IRQ_HANDLED | ||
| 94 | } irqreturn_t; | ||
| 95 | |||
| 96 | static inline void *kmalloc(size_t s, gfp_t gfp) | ||
| 97 | { | ||
| 98 | return malloc(s); | ||
| 99 | } | ||
| 100 | |||
| 101 | static inline void kfree(void *p) | ||
| 102 | { | ||
| 103 | free(p); | ||
| 104 | } | ||
| 105 | |||
| 106 | #define container_of(ptr, type, member) ({ \ | ||
| 107 | const typeof( ((type *)0)->member ) *__mptr = (ptr); \ | ||
| 108 | (type *)( (char *)__mptr - offsetof(type,member) );}) | ||
| 109 | |||
| 110 | #define uninitialized_var(x) x = x | ||
| 111 | |||
| 112 | # ifndef likely | ||
| 113 | # define likely(x) (__builtin_expect(!!(x), 1)) | ||
| 114 | # endif | ||
| 115 | # ifndef unlikely | ||
| 116 | # define unlikely(x) (__builtin_expect(!!(x), 0)) | ||
| 117 | # endif | ||
| 118 | |||
| 119 | #define pr_err(format, ...) fprintf (stderr, format, ## __VA_ARGS__) | ||
| 120 | #ifdef DEBUG | ||
| 121 | #define pr_debug(format, ...) fprintf (stderr, format, ## __VA_ARGS__) | ||
| 122 | #else | ||
| 123 | #define pr_debug(format, ...) do {} while (0) | ||
| 124 | #endif | ||
| 125 | #define dev_err(dev, format, ...) fprintf (stderr, format, ## __VA_ARGS__) | ||
| 126 | #define dev_warn(dev, format, ...) fprintf (stderr, format, ## __VA_ARGS__) | ||
| 127 | 5 | ||
| 128 | /* TODO: empty stubs for now. Broken but enough for virtio_ring.c */ | 6 | /* TODO: empty stubs for now. Broken but enough for virtio_ring.c */ |
| 129 | #define list_add_tail(a, b) do {} while (0) | 7 | #define list_add_tail(a, b) do {} while (0) |
| @@ -133,6 +11,7 @@ static inline void kfree(void *p) | |||
| 133 | #define BITS_PER_BYTE 8 | 11 | #define BITS_PER_BYTE 8 |
| 134 | #define BITS_PER_LONG (sizeof(long) * BITS_PER_BYTE) | 12 | #define BITS_PER_LONG (sizeof(long) * BITS_PER_BYTE) |
| 135 | #define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG)) | 13 | #define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG)) |
| 14 | |||
| 136 | /* TODO: Not atomic as it should be: | 15 | /* TODO: Not atomic as it should be: |
| 137 | * we don't use this for anything important. */ | 16 | * we don't use this for anything important. */ |
| 138 | static inline void clear_bit(int nr, volatile unsigned long *addr) | 17 | static inline void clear_bit(int nr, volatile unsigned long *addr) |
| @@ -147,10 +26,6 @@ static inline int test_bit(int nr, const volatile unsigned long *addr) | |||
| 147 | { | 26 | { |
| 148 | return 1UL & (addr[BIT_WORD(nr)] >> (nr & (BITS_PER_LONG-1))); | 27 | return 1UL & (addr[BIT_WORD(nr)] >> (nr & (BITS_PER_LONG-1))); |
| 149 | } | 28 | } |
| 150 | |||
| 151 | /* The only feature we care to support */ | ||
| 152 | #define virtio_has_feature(dev, feature) \ | ||
| 153 | test_bit((feature), (dev)->features) | ||
| 154 | /* end of stubs */ | 29 | /* end of stubs */ |
| 155 | 30 | ||
| 156 | struct virtio_device { | 31 | struct virtio_device { |
| @@ -170,28 +45,9 @@ struct virtqueue { | |||
| 170 | void *priv; | 45 | void *priv; |
| 171 | }; | 46 | }; |
| 172 | 47 | ||
| 173 | #define EXPORT_SYMBOL_GPL(__EXPORT_SYMBOL_GPL_name) \ | ||
| 174 | void __EXPORT_SYMBOL_GPL##__EXPORT_SYMBOL_GPL_name() { \ | ||
| 175 | } | ||
| 176 | #define MODULE_LICENSE(__MODULE_LICENSE_value) \ | 48 | #define MODULE_LICENSE(__MODULE_LICENSE_value) \ |
| 177 | const char *__MODULE_LICENSE_name = __MODULE_LICENSE_value | 49 | const char *__MODULE_LICENSE_name = __MODULE_LICENSE_value |
| 178 | 50 | ||
| 179 | #define CONFIG_SMP | ||
| 180 | |||
| 181 | #if defined(__i386__) || defined(__x86_64__) | ||
| 182 | #define barrier() asm volatile("" ::: "memory") | ||
| 183 | #define mb() __sync_synchronize() | ||
| 184 | |||
| 185 | #define smp_mb() mb() | ||
| 186 | # define smp_rmb() barrier() | ||
| 187 | # define smp_wmb() barrier() | ||
| 188 | /* Weak barriers should be used. If not - it's a bug */ | ||
| 189 | # define rmb() abort() | ||
| 190 | # define wmb() abort() | ||
| 191 | #else | ||
| 192 | #error Please fill in barrier macros | ||
| 193 | #endif | ||
| 194 | |||
| 195 | /* Interfaces exported by virtio_ring. */ | 51 | /* Interfaces exported by virtio_ring. */ |
| 196 | int virtqueue_add_buf(struct virtqueue *vq, | 52 | int virtqueue_add_buf(struct virtqueue *vq, |
| 197 | struct scatterlist sg[], | 53 | struct scatterlist sg[], |
diff --git a/tools/virtio/linux/virtio_config.h b/tools/virtio/linux/virtio_config.h new file mode 100644 index 000000000000..5049967f99f7 --- /dev/null +++ b/tools/virtio/linux/virtio_config.h | |||
| @@ -0,0 +1,6 @@ | |||
| 1 | #define VIRTIO_TRANSPORT_F_START 28 | ||
| 2 | #define VIRTIO_TRANSPORT_F_END 32 | ||
| 3 | |||
| 4 | #define virtio_has_feature(dev, feature) \ | ||
| 5 | test_bit((feature), (dev)->features) | ||
| 6 | |||
diff --git a/tools/virtio/linux/virtio_ring.h b/tools/virtio/linux/virtio_ring.h new file mode 100644 index 000000000000..8949c4e2772c --- /dev/null +++ b/tools/virtio/linux/virtio_ring.h | |||
| @@ -0,0 +1 @@ | |||
| #include "../../../include/linux/virtio_ring.h" | |||
diff --git a/tools/virtio/linux/vringh.h b/tools/virtio/linux/vringh.h new file mode 100644 index 000000000000..9348957be56e --- /dev/null +++ b/tools/virtio/linux/vringh.h | |||
| @@ -0,0 +1 @@ | |||
| #include "../../../include/linux/vringh.h" | |||
