blob: d9554fc3f3403c2adef3d883fb767cabaa82015e [file] [log] [blame]
Rusty Russell61d0b5a42013-03-18 13:22:19 +10301#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
Michael S. Tsirkina7c49032016-01-20 21:12:58 +020011#include <linux/compiler.h>
Rusty Russell61d0b5a42013-03-18 13:22:19 +103012#include <linux/types.h>
13#include <linux/printk.h>
14#include <linux/bug.h>
15#include <errno.h>
16#include <unistd.h>
17#include <asm/barrier.h>
18
19#define CONFIG_SMP
20
21#define PAGE_SIZE getpagesize()
22#define PAGE_MASK (~(PAGE_SIZE-1))
Michael S. Tsirkin6be3ffa2016-08-15 04:50:55 +030023#define PAGE_ALIGN(x) ((x + PAGE_SIZE - 1) & PAGE_MASK)
Rusty Russell61d0b5a42013-03-18 13:22:19 +103024
Michael S. Tsirkin6be3ffa2016-08-15 04:50:55 +030025typedef unsigned long long phys_addr_t;
Rusty Russell61d0b5a42013-03-18 13:22:19 +103026typedef unsigned long long dma_addr_t;
27typedef size_t __kernel_size_t;
Michael S. Tsirkinddab2c02015-09-09 22:03:30 +030028typedef unsigned int __wsum;
Rusty Russell61d0b5a42013-03-18 13:22:19 +103029
30struct page {
31 unsigned long long dummy;
32};
33
34/* Physical == Virtual */
35#define virt_to_phys(p) ((unsigned long)p)
36#define phys_to_virt(a) ((void *)(unsigned long)(a))
37/* Page address: Virtual / 4K */
38#define page_to_phys(p) ((dma_addr_t)(unsigned long)(p))
39#define virt_to_page(p) ((struct page *)((unsigned long)p & PAGE_MASK))
40
41#define offset_in_page(p) (((unsigned long)p) % PAGE_SIZE)
42
43#define __printf(a,b) __attribute__((format(printf,a,b)))
44
Rusty Russell61d0b5a42013-03-18 13:22:19 +103045#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
46
47extern void *__kmalloc_fake, *__kfree_ignore_start, *__kfree_ignore_end;
48static inline void *kmalloc(size_t s, gfp_t gfp)
49{
50 if (__kmalloc_fake)
51 return __kmalloc_fake;
52 return malloc(s);
53}
Michael S. Tsirkinddab2c02015-09-09 22:03:30 +030054static inline void *kzalloc(size_t s, gfp_t gfp)
55{
56 void *p = kmalloc(s, gfp);
57
58 memset(p, 0, s);
59 return p;
60}
Rusty Russell61d0b5a42013-03-18 13:22:19 +103061
Michael S. Tsirkin6be3ffa2016-08-15 04:50:55 +030062static inline void *alloc_pages_exact(size_t s, gfp_t gfp)
63{
64 return kmalloc(s, gfp);
65}
66
Rusty Russell61d0b5a42013-03-18 13:22:19 +103067static inline void kfree(void *p)
68{
69 if (p >= __kfree_ignore_start && p < __kfree_ignore_end)
70 return;
71 free(p);
72}
73
Michael S. Tsirkin6be3ffa2016-08-15 04:50:55 +030074static inline void free_pages_exact(void *p, size_t s)
75{
76 kfree(p);
77}
78
Rusty Russell61d0b5a42013-03-18 13:22:19 +103079static inline void *krealloc(void *p, size_t s, gfp_t gfp)
80{
81 return realloc(p, s);
82}
83
84
85static inline unsigned long __get_free_page(gfp_t gfp)
86{
87 void *p;
88
89 posix_memalign(&p, PAGE_SIZE, PAGE_SIZE);
90 return (unsigned long)p;
91}
92
93static inline void free_page(unsigned long addr)
94{
95 free((void *)addr);
96}
97
98#define container_of(ptr, type, member) ({ \
99 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
100 (type *)( (char *)__mptr - offsetof(type,member) );})
101
102#define uninitialized_var(x) x = x
103
104# ifndef likely
105# define likely(x) (__builtin_expect(!!(x), 1))
106# endif
107# ifndef unlikely
108# define unlikely(x) (__builtin_expect(!!(x), 0))
109# endif
110
111#define pr_err(format, ...) fprintf (stderr, format, ## __VA_ARGS__)
112#ifdef DEBUG
113#define pr_debug(format, ...) fprintf (stderr, format, ## __VA_ARGS__)
114#else
115#define pr_debug(format, ...) do {} while (0)
116#endif
117#define dev_err(dev, format, ...) fprintf (stderr, format, ## __VA_ARGS__)
118#define dev_warn(dev, format, ...) fprintf (stderr, format, ## __VA_ARGS__)
119
Michael S. Tsirkin6be3ffa2016-08-15 04:50:55 +0300120#define WARN_ON_ONCE(cond) ((cond) && fprintf (stderr, "WARNING\n"))
121
Rusty Russell61d0b5a42013-03-18 13:22:19 +1030122#define min(x, y) ({ \
123 typeof(x) _min1 = (x); \
124 typeof(y) _min2 = (y); \
125 (void) (&_min1 == &_min2); \
126 _min1 < _min2 ? _min1 : _min2; })
127
Michael S. Tsirkin40c172e2015-11-29 12:43:48 +0200128/* TODO: empty stubs for now. Broken but enough for virtio_ring.c */
129#define list_add_tail(a, b) do {} while (0)
130#define list_del(a) do {} while (0)
131#define list_for_each_entry(a, b, c) while (0)
132/* end of stubs */
133
Rusty Russell61d0b5a42013-03-18 13:22:19 +1030134#endif /* KERNEL_H */