Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / tools / virtio / linux / kernel.h
blobfca8381bbe041315d955f96e7e045d70d1cc89fb
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef KERNEL_H
3 #define KERNEL_H
4 #include <stdbool.h>
5 #include <stdlib.h>
6 #include <stddef.h>
7 #include <stdio.h>
8 #include <string.h>
9 #include <assert.h>
10 #include <stdarg.h>
12 #include <linux/compiler.h>
13 #include <linux/types.h>
14 #include <linux/printk.h>
15 #include <linux/bug.h>
16 #include <errno.h>
17 #include <unistd.h>
18 #include <asm/barrier.h>
20 #define CONFIG_SMP
22 #define PAGE_SIZE getpagesize()
23 #define PAGE_MASK (~(PAGE_SIZE-1))
24 #define PAGE_ALIGN(x) ((x + PAGE_SIZE - 1) & PAGE_MASK)
26 typedef unsigned long long phys_addr_t;
27 typedef unsigned long long dma_addr_t;
28 typedef size_t __kernel_size_t;
29 typedef unsigned int __wsum;
31 struct page {
32 unsigned long long dummy;
35 /* Physical == Virtual */
36 #define virt_to_phys(p) ((unsigned long)p)
37 #define phys_to_virt(a) ((void *)(unsigned long)(a))
38 /* Page address: Virtual / 4K */
39 #define page_to_phys(p) ((dma_addr_t)(unsigned long)(p))
40 #define virt_to_page(p) ((struct page *)((unsigned long)p & PAGE_MASK))
42 #define offset_in_page(p) (((unsigned long)p) % PAGE_SIZE)
44 #define __printf(a,b) __attribute__((format(printf,a,b)))
46 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
48 extern void *__kmalloc_fake, *__kfree_ignore_start, *__kfree_ignore_end;
49 static inline void *kmalloc(size_t s, gfp_t gfp)
51 if (__kmalloc_fake)
52 return __kmalloc_fake;
53 return malloc(s);
55 static inline void *kzalloc(size_t s, gfp_t gfp)
57 void *p = kmalloc(s, gfp);
59 memset(p, 0, s);
60 return p;
63 static inline void *alloc_pages_exact(size_t s, gfp_t gfp)
65 return kmalloc(s, gfp);
68 static inline void kfree(void *p)
70 if (p >= __kfree_ignore_start && p < __kfree_ignore_end)
71 return;
72 free(p);
75 static inline void free_pages_exact(void *p, size_t s)
77 kfree(p);
80 static inline void *krealloc(void *p, size_t s, gfp_t gfp)
82 return realloc(p, s);
86 static inline unsigned long __get_free_page(gfp_t gfp)
88 void *p;
90 posix_memalign(&p, PAGE_SIZE, PAGE_SIZE);
91 return (unsigned long)p;
94 static inline void free_page(unsigned long addr)
96 free((void *)addr);
99 #define container_of(ptr, type, member) ({ \
100 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
101 (type *)( (char *)__mptr - offsetof(type,member) );})
103 #define uninitialized_var(x) x = x
105 # ifndef likely
106 # define likely(x) (__builtin_expect(!!(x), 1))
107 # endif
108 # ifndef unlikely
109 # define unlikely(x) (__builtin_expect(!!(x), 0))
110 # endif
112 #define pr_err(format, ...) fprintf (stderr, format, ## __VA_ARGS__)
113 #ifdef DEBUG
114 #define pr_debug(format, ...) fprintf (stderr, format, ## __VA_ARGS__)
115 #else
116 #define pr_debug(format, ...) do {} while (0)
117 #endif
118 #define dev_err(dev, format, ...) fprintf (stderr, format, ## __VA_ARGS__)
119 #define dev_warn(dev, format, ...) fprintf (stderr, format, ## __VA_ARGS__)
121 #define WARN_ON_ONCE(cond) ((cond) ? fprintf (stderr, "WARNING\n") : 0)
123 #define min(x, y) ({ \
124 typeof(x) _min1 = (x); \
125 typeof(y) _min2 = (y); \
126 (void) (&_min1 == &_min2); \
127 _min1 < _min2 ? _min1 : _min2; })
129 /* TODO: empty stubs for now. Broken but enough for virtio_ring.c */
130 #define list_add_tail(a, b) do {} while (0)
131 #define list_del(a) do {} while (0)
132 #define list_for_each_entry(a, b, c) while (0)
133 /* end of stubs */
135 #endif /* KERNEL_H */