xref: /linux/tools/virtio/linux/kernel.h (revision 3afcb3630944d7565d6ab6106b61461b292aa93f)
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>
11 
12 #include <linux/compiler.h>
13 #include "../../../include/linux/container_of.h"
14 #include <linux/log2.h>
15 #include <linux/types.h>
16 #include <linux/overflow.h>
17 #include <linux/limits.h>
18 #include <linux/list.h>
19 #include <linux/printk.h>
20 #include <linux/bug.h>
21 #include <errno.h>
22 #include <unistd.h>
23 #include <asm/barrier.h>
24 
25 #define CONFIG_SMP
26 
27 #define PAGE_SIZE getpagesize()
28 #define PAGE_MASK (~(PAGE_SIZE-1))
29 #define PAGE_ALIGN(x) ((x + PAGE_SIZE - 1) & PAGE_MASK)
30 
31 /* generic data direction definitions */
32 #define READ                    0
33 #define WRITE                   1
34 
35 typedef unsigned long long dma_addr_t;
36 typedef size_t __kernel_size_t;
37 typedef unsigned int __wsum;
38 
39 struct page {
40 	unsigned long long dummy;
41 };
42 
43 /* Physical == Virtual */
44 #define virt_to_phys(p) ((unsigned long)p)
45 #define phys_to_virt(a) ((void *)(unsigned long)(a))
46 /* Page address: Virtual / 4K */
47 #define page_to_phys(p) ((dma_addr_t)(unsigned long)(p))
48 #define virt_to_page(p) ((struct page *)((unsigned long)p & PAGE_MASK))
49 
50 #define offset_in_page(p) (((unsigned long)p) % PAGE_SIZE)
51 
52 #define __printf(a,b) __attribute__((format(printf,a,b)))
53 
54 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
55 
56 extern void *__kmalloc_fake, *__kfree_ignore_start, *__kfree_ignore_end;
57 static inline void *kmalloc(size_t s, gfp_t gfp)
58 {
59 	if (__kmalloc_fake)
60 		return __kmalloc_fake;
61 	return malloc(s);
62 }
63 static inline void *kmalloc_array(unsigned n, size_t s, gfp_t gfp)
64 {
65 	return kmalloc(n * s, gfp);
66 }
67 
68 #define kmalloc_obj(VAR_OR_TYPE, ...) \
69 	((typeof(VAR_OR_TYPE) *)kmalloc(sizeof(typeof(VAR_OR_TYPE)), 0))
70 
71 #define kmalloc_objs(VAR_OR_TYPE, COUNT, ...) \
72 	((typeof(VAR_OR_TYPE) *)kmalloc(sizeof(typeof(VAR_OR_TYPE)) * (COUNT), 0))
73 
74 static inline void *kzalloc(size_t s, gfp_t gfp)
75 {
76 	void *p = kmalloc(s, gfp);
77 
78 	memset(p, 0, s);
79 	return p;
80 }
81 
82 static inline void *alloc_pages_exact(size_t s, gfp_t gfp)
83 {
84 	return kmalloc(s, gfp);
85 }
86 
87 static inline void kfree(void *p)
88 {
89 	if (p >= __kfree_ignore_start && p < __kfree_ignore_end)
90 		return;
91 	free(p);
92 }
93 
94 static inline void free_pages_exact(void *p, size_t s)
95 {
96 	kfree(p);
97 }
98 
99 static inline void *krealloc(void *p, size_t s, gfp_t gfp)
100 {
101 	return realloc(p, s);
102 }
103 
104 
105 static inline unsigned long __get_free_page(gfp_t gfp)
106 {
107 	void *p;
108 
109 	posix_memalign(&p, PAGE_SIZE, PAGE_SIZE);
110 	return (unsigned long)p;
111 }
112 
113 static inline void free_page(unsigned long addr)
114 {
115 	free((void *)addr);
116 }
117 
118 # ifndef likely
119 #  define likely(x)	(__builtin_expect(!!(x), 1))
120 # endif
121 # ifndef unlikely
122 #  define unlikely(x)	(__builtin_expect(!!(x), 0))
123 # endif
124 
125 static inline void *krealloc_array(void *p, size_t new_n, size_t new_size, gfp_t gfp)
126 {
127 	size_t bytes;
128 
129 	if (unlikely(check_mul_overflow(new_n, new_size, &bytes)))
130 		return NULL;
131 
132 	return krealloc(p, bytes, gfp);
133 }
134 
135 #define pr_err(format, ...) fprintf (stderr, format, ## __VA_ARGS__)
136 #ifdef DEBUG
137 #define pr_debug(format, ...) fprintf (stderr, format, ## __VA_ARGS__)
138 #else
139 #define pr_debug(format, ...) do {} while (0)
140 #endif
141 #define dev_err(dev, format, ...) fprintf (stderr, format, ## __VA_ARGS__)
142 #define dev_warn(dev, format, ...) fprintf (stderr, format, ## __VA_ARGS__)
143 #define dev_warn_once(dev, format, ...) fprintf (stderr, format, ## __VA_ARGS__)
144 
145 #define dev_WARN_ONCE(dev, condition, format...) \
146 	WARN_ONCE(condition, format)
147 
148 static inline bool is_vmalloc_addr(const void *x)
149 {
150 	return false;
151 }
152 
153 #define might_sleep() do { } while (0)
154 
155 static inline void synchronize_rcu(void)
156 {
157 	assert(0);
158 }
159 
160 #define min(x, y) ({				\
161 	typeof(x) _min1 = (x);			\
162 	typeof(y) _min2 = (y);			\
163 	(void) (&_min1 == &_min2);		\
164 	_min1 < _min2 ? _min1 : _min2; })
165 
166 #endif /* KERNEL_H */
167