blob: 0338499482159883bb3e30452d5afc0f77af577e [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))
23
24typedef unsigned long long dma_addr_t;
25typedef size_t __kernel_size_t;
Michael S. Tsirkinddab2c02015-09-09 22:03:30 +030026typedef unsigned int __wsum;
Rusty Russell61d0b5a42013-03-18 13:22:19 +103027
28struct page {
29 unsigned long long dummy;
30};
31
32/* Physical == Virtual */
33#define virt_to_phys(p) ((unsigned long)p)
34#define phys_to_virt(a) ((void *)(unsigned long)(a))
35/* Page address: Virtual / 4K */
36#define page_to_phys(p) ((dma_addr_t)(unsigned long)(p))
37#define virt_to_page(p) ((struct page *)((unsigned long)p & PAGE_MASK))
38
39#define offset_in_page(p) (((unsigned long)p) % PAGE_SIZE)
40
41#define __printf(a,b) __attribute__((format(printf,a,b)))
42
Rusty Russell61d0b5a42013-03-18 13:22:19 +103043#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
44
45extern void *__kmalloc_fake, *__kfree_ignore_start, *__kfree_ignore_end;
46static inline void *kmalloc(size_t s, gfp_t gfp)
47{
48 if (__kmalloc_fake)
49 return __kmalloc_fake;
50 return malloc(s);
51}
Michael S. Tsirkinddab2c02015-09-09 22:03:30 +030052static inline void *kzalloc(size_t s, gfp_t gfp)
53{
54 void *p = kmalloc(s, gfp);
55
56 memset(p, 0, s);
57 return p;
58}
Rusty Russell61d0b5a42013-03-18 13:22:19 +103059
60static inline void kfree(void *p)
61{
62 if (p >= __kfree_ignore_start && p < __kfree_ignore_end)
63 return;
64 free(p);
65}
66
67static inline void *krealloc(void *p, size_t s, gfp_t gfp)
68{
69 return realloc(p, s);
70}
71
72
73static inline unsigned long __get_free_page(gfp_t gfp)
74{
75 void *p;
76
77 posix_memalign(&p, PAGE_SIZE, PAGE_SIZE);
78 return (unsigned long)p;
79}
80
81static inline void free_page(unsigned long addr)
82{
83 free((void *)addr);
84}
85
86#define container_of(ptr, type, member) ({ \
87 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
88 (type *)( (char *)__mptr - offsetof(type,member) );})
89
90#define uninitialized_var(x) x = x
91
92# ifndef likely
93# define likely(x) (__builtin_expect(!!(x), 1))
94# endif
95# ifndef unlikely
96# define unlikely(x) (__builtin_expect(!!(x), 0))
97# endif
98
99#define pr_err(format, ...) fprintf (stderr, format, ## __VA_ARGS__)
100#ifdef DEBUG
101#define pr_debug(format, ...) fprintf (stderr, format, ## __VA_ARGS__)
102#else
103#define pr_debug(format, ...) do {} while (0)
104#endif
105#define dev_err(dev, format, ...) fprintf (stderr, format, ## __VA_ARGS__)
106#define dev_warn(dev, format, ...) fprintf (stderr, format, ## __VA_ARGS__)
107
108#define min(x, y) ({ \
109 typeof(x) _min1 = (x); \
110 typeof(y) _min2 = (y); \
111 (void) (&_min1 == &_min2); \
112 _min1 < _min2 ? _min1 : _min2; })
113
Michael S. Tsirkin40c172e2015-11-29 12:43:48 +0200114/* TODO: empty stubs for now. Broken but enough for virtio_ring.c */
115#define list_add_tail(a, b) do {} while (0)
116#define list_del(a) do {} while (0)
117#define list_for_each_entry(a, b, c) while (0)
118/* end of stubs */
119
Rusty Russell61d0b5a42013-03-18 13:22:19 +1030120#endif /* KERNEL_H */