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