Skip to content

Commit 61d0b5a

Browse files
committed
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>
1 parent a9a0fef commit 61d0b5a

21 files changed

Lines changed: 450 additions & 147 deletions

‎tools/virtio/asm/barrier.h‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#if defined(__i386__) || defined(__x86_64__)
2+
#define barrier() asm volatile("" ::: "memory")
3+
#define mb() __sync_synchronize()
4+
5+
#define smp_mb() mb()
6+
# define smp_rmb() barrier()
7+
# define smp_wmb() barrier()
8+
/* Weak barriers should be used. If not - it's a bug */
9+
# define rmb() abort()
10+
# define wmb() abort()
11+
#else
12+
#error Please fill in barrier macros
13+
#endif
14+

‎tools/virtio/linux/bug.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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 */

‎tools/virtio/linux/err.h‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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 */

‎tools/virtio/linux/export.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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)

‎tools/virtio/linux/irqreturn.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "../../../include/linux/irqreturn.h"

‎tools/virtio/linux/kernel.h‎

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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 */

‎tools/virtio/linux/module.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include <linux/export.h>

‎tools/virtio/linux/printk.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#include "../../../include/linux/kern_levels.h"
2+
3+
#define printk printf
4+
#define vprintk vprintf

‎tools/virtio/linux/ratelimit.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#define DEFINE_RATELIMIT_STATE(name, interval_init, burst_init) int name = 0
2+
3+
#define __ratelimit(x) (*(x))
4+

‎tools/virtio/linux/scatterlist.h‎

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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 */

0 commit comments

Comments
 (0)