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