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