Rusty Russell | 61d0b5a | 2013-03-18 13:22:19 +1030 | [diff] [blame] | 1 | #ifndef KERNEL_H |
| 2 | #define KERNEL_H |
| 3 | #include <stdbool.h> |
| 4 | #include <stdlib.h> |
| 5 | #include <stddef.h> |
| 6 | #include <stdio.h> |
| 7 | #include <string.h> |
| 8 | #include <assert.h> |
| 9 | #include <stdarg.h> |
| 10 | |
| 11 | #include <linux/types.h> |
| 12 | #include <linux/printk.h> |
| 13 | #include <linux/bug.h> |
| 14 | #include <errno.h> |
| 15 | #include <unistd.h> |
| 16 | #include <asm/barrier.h> |
| 17 | |
| 18 | #define CONFIG_SMP |
| 19 | |
| 20 | #define PAGE_SIZE getpagesize() |
| 21 | #define PAGE_MASK (~(PAGE_SIZE-1)) |
| 22 | |
| 23 | typedef unsigned long long dma_addr_t; |
| 24 | typedef size_t __kernel_size_t; |
Michael S. Tsirkin | ddab2c0 | 2015-09-09 22:03:30 +0300 | [diff] [blame] | 25 | typedef unsigned int __wsum; |
Rusty Russell | 61d0b5a | 2013-03-18 13:22:19 +1030 | [diff] [blame] | 26 | |
| 27 | struct page { |
| 28 | unsigned long long dummy; |
| 29 | }; |
| 30 | |
| 31 | /* Physical == Virtual */ |
| 32 | #define virt_to_phys(p) ((unsigned long)p) |
| 33 | #define phys_to_virt(a) ((void *)(unsigned long)(a)) |
| 34 | /* Page address: Virtual / 4K */ |
| 35 | #define page_to_phys(p) ((dma_addr_t)(unsigned long)(p)) |
| 36 | #define virt_to_page(p) ((struct page *)((unsigned long)p & PAGE_MASK)) |
| 37 | |
| 38 | #define offset_in_page(p) (((unsigned long)p) % PAGE_SIZE) |
| 39 | |
| 40 | #define __printf(a,b) __attribute__((format(printf,a,b))) |
| 41 | |
Rusty Russell | 61d0b5a | 2013-03-18 13:22:19 +1030 | [diff] [blame] | 42 | #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0])) |
| 43 | |
| 44 | extern void *__kmalloc_fake, *__kfree_ignore_start, *__kfree_ignore_end; |
| 45 | static inline void *kmalloc(size_t s, gfp_t gfp) |
| 46 | { |
| 47 | if (__kmalloc_fake) |
| 48 | return __kmalloc_fake; |
| 49 | return malloc(s); |
| 50 | } |
Michael S. Tsirkin | ddab2c0 | 2015-09-09 22:03:30 +0300 | [diff] [blame] | 51 | static inline void *kzalloc(size_t s, gfp_t gfp) |
| 52 | { |
| 53 | void *p = kmalloc(s, gfp); |
| 54 | |
| 55 | memset(p, 0, s); |
| 56 | return p; |
| 57 | } |
Rusty Russell | 61d0b5a | 2013-03-18 13:22:19 +1030 | [diff] [blame] | 58 | |
| 59 | static inline void kfree(void *p) |
| 60 | { |
| 61 | if (p >= __kfree_ignore_start && p < __kfree_ignore_end) |
| 62 | return; |
| 63 | free(p); |
| 64 | } |
| 65 | |
| 66 | static inline void *krealloc(void *p, size_t s, gfp_t gfp) |
| 67 | { |
| 68 | return realloc(p, s); |
| 69 | } |
| 70 | |
| 71 | |
| 72 | static inline unsigned long __get_free_page(gfp_t gfp) |
| 73 | { |
| 74 | void *p; |
| 75 | |
| 76 | posix_memalign(&p, PAGE_SIZE, PAGE_SIZE); |
| 77 | return (unsigned long)p; |
| 78 | } |
| 79 | |
| 80 | static inline void free_page(unsigned long addr) |
| 81 | { |
| 82 | free((void *)addr); |
| 83 | } |
| 84 | |
| 85 | #define container_of(ptr, type, member) ({ \ |
| 86 | const typeof( ((type *)0)->member ) *__mptr = (ptr); \ |
| 87 | (type *)( (char *)__mptr - offsetof(type,member) );}) |
| 88 | |
| 89 | #define uninitialized_var(x) x = x |
| 90 | |
| 91 | # ifndef likely |
| 92 | # define likely(x) (__builtin_expect(!!(x), 1)) |
| 93 | # endif |
| 94 | # ifndef unlikely |
| 95 | # define unlikely(x) (__builtin_expect(!!(x), 0)) |
| 96 | # endif |
| 97 | |
| 98 | #define pr_err(format, ...) fprintf (stderr, format, ## __VA_ARGS__) |
| 99 | #ifdef DEBUG |
| 100 | #define pr_debug(format, ...) fprintf (stderr, format, ## __VA_ARGS__) |
| 101 | #else |
| 102 | #define pr_debug(format, ...) do {} while (0) |
| 103 | #endif |
| 104 | #define dev_err(dev, format, ...) fprintf (stderr, format, ## __VA_ARGS__) |
| 105 | #define dev_warn(dev, format, ...) fprintf (stderr, format, ## __VA_ARGS__) |
| 106 | |
| 107 | #define min(x, y) ({ \ |
| 108 | typeof(x) _min1 = (x); \ |
| 109 | typeof(y) _min2 = (y); \ |
| 110 | (void) (&_min1 == &_min2); \ |
| 111 | _min1 < _min2 ? _min1 : _min2; }) |
| 112 | |
Michael S. Tsirkin | 40c172e | 2015-11-29 12:43:48 +0200 | [diff] [blame] | 113 | /* TODO: empty stubs for now. Broken but enough for virtio_ring.c */ |
| 114 | #define list_add_tail(a, b) do {} while (0) |
| 115 | #define list_del(a) do {} while (0) |
| 116 | #define list_for_each_entry(a, b, c) while (0) |
| 117 | /* end of stubs */ |
| 118 | |
Rusty Russell | 61d0b5a | 2013-03-18 13:22:19 +1030 | [diff] [blame] | 119 | #endif /* KERNEL_H */ |