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