xref: /openbmc/linux/tools/virtio/linux/kernel.h (revision e9c4962c)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
261d0b5a4SRusty Russell #ifndef KERNEL_H
361d0b5a4SRusty Russell #define KERNEL_H
461d0b5a4SRusty Russell #include <stdbool.h>
561d0b5a4SRusty Russell #include <stdlib.h>
661d0b5a4SRusty Russell #include <stddef.h>
761d0b5a4SRusty Russell #include <stdio.h>
861d0b5a4SRusty Russell #include <string.h>
961d0b5a4SRusty Russell #include <assert.h>
1061d0b5a4SRusty Russell #include <stdarg.h>
1161d0b5a4SRusty Russell 
12a7c49033SMichael S. Tsirkin #include <linux/compiler.h>
13*e9c4962cSShunsuke Mie #include "../../../include/linux/container_of.h"
143f7b75abSShunsuke Mie #include <linux/log2.h>
1561d0b5a4SRusty Russell #include <linux/types.h>
16b9ca93bcSPeng Fan #include <linux/overflow.h>
17cb91909eSEugenio Pérez #include <linux/list.h>
1861d0b5a4SRusty Russell #include <linux/printk.h>
1961d0b5a4SRusty Russell #include <linux/bug.h>
2061d0b5a4SRusty Russell #include <errno.h>
2161d0b5a4SRusty Russell #include <unistd.h>
2261d0b5a4SRusty Russell #include <asm/barrier.h>
2361d0b5a4SRusty Russell 
2461d0b5a4SRusty Russell #define CONFIG_SMP
2561d0b5a4SRusty Russell 
2661d0b5a4SRusty Russell #define PAGE_SIZE getpagesize()
2761d0b5a4SRusty Russell #define PAGE_MASK (~(PAGE_SIZE-1))
286be3ffaaSMichael S. Tsirkin #define PAGE_ALIGN(x) ((x + PAGE_SIZE - 1) & PAGE_MASK)
2961d0b5a4SRusty Russell 
30c5c08bedSMichael S. Tsirkin /* generic data direction definitions */
31c5c08bedSMichael S. Tsirkin #define READ                    0
32c5c08bedSMichael S. Tsirkin #define WRITE                   1
33c5c08bedSMichael S. Tsirkin 
3461d0b5a4SRusty Russell typedef unsigned long long dma_addr_t;
3561d0b5a4SRusty Russell typedef size_t __kernel_size_t;
36ddab2c0eSMichael S. Tsirkin typedef unsigned int __wsum;
3761d0b5a4SRusty Russell 
3861d0b5a4SRusty Russell struct page {
3961d0b5a4SRusty Russell 	unsigned long long dummy;
4061d0b5a4SRusty Russell };
4161d0b5a4SRusty Russell 
4261d0b5a4SRusty Russell /* Physical == Virtual */
4361d0b5a4SRusty Russell #define virt_to_phys(p) ((unsigned long)p)
4461d0b5a4SRusty Russell #define phys_to_virt(a) ((void *)(unsigned long)(a))
4561d0b5a4SRusty Russell /* Page address: Virtual / 4K */
4661d0b5a4SRusty Russell #define page_to_phys(p) ((dma_addr_t)(unsigned long)(p))
4761d0b5a4SRusty Russell #define virt_to_page(p) ((struct page *)((unsigned long)p & PAGE_MASK))
4861d0b5a4SRusty Russell 
4961d0b5a4SRusty Russell #define offset_in_page(p) (((unsigned long)p) % PAGE_SIZE)
5061d0b5a4SRusty Russell 
5161d0b5a4SRusty Russell #define __printf(a,b) __attribute__((format(printf,a,b)))
5261d0b5a4SRusty Russell 
5361d0b5a4SRusty Russell #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
5461d0b5a4SRusty Russell 
5561d0b5a4SRusty Russell extern void *__kmalloc_fake, *__kfree_ignore_start, *__kfree_ignore_end;
kmalloc(size_t s,gfp_t gfp)5661d0b5a4SRusty Russell static inline void *kmalloc(size_t s, gfp_t gfp)
5761d0b5a4SRusty Russell {
5861d0b5a4SRusty Russell 	if (__kmalloc_fake)
5961d0b5a4SRusty Russell 		return __kmalloc_fake;
6061d0b5a4SRusty Russell 	return malloc(s);
6161d0b5a4SRusty Russell }
kmalloc_array(unsigned n,size_t s,gfp_t gfp)62f2467ee0SMichael S. Tsirkin static inline void *kmalloc_array(unsigned n, size_t s, gfp_t gfp)
63f2467ee0SMichael S. Tsirkin {
64f2467ee0SMichael S. Tsirkin 	return kmalloc(n * s, gfp);
65f2467ee0SMichael S. Tsirkin }
66f2467ee0SMichael S. Tsirkin 
kzalloc(size_t s,gfp_t gfp)67ddab2c0eSMichael S. Tsirkin static inline void *kzalloc(size_t s, gfp_t gfp)
68ddab2c0eSMichael S. Tsirkin {
69ddab2c0eSMichael S. Tsirkin 	void *p = kmalloc(s, gfp);
70ddab2c0eSMichael S. Tsirkin 
71ddab2c0eSMichael S. Tsirkin 	memset(p, 0, s);
72ddab2c0eSMichael S. Tsirkin 	return p;
73ddab2c0eSMichael S. Tsirkin }
7461d0b5a4SRusty Russell 
alloc_pages_exact(size_t s,gfp_t gfp)756be3ffaaSMichael S. Tsirkin static inline void *alloc_pages_exact(size_t s, gfp_t gfp)
766be3ffaaSMichael S. Tsirkin {
776be3ffaaSMichael S. Tsirkin 	return kmalloc(s, gfp);
786be3ffaaSMichael S. Tsirkin }
796be3ffaaSMichael S. Tsirkin 
kfree(void * p)8061d0b5a4SRusty Russell static inline void kfree(void *p)
8161d0b5a4SRusty Russell {
8261d0b5a4SRusty Russell 	if (p >= __kfree_ignore_start && p < __kfree_ignore_end)
8361d0b5a4SRusty Russell 		return;
8461d0b5a4SRusty Russell 	free(p);
8561d0b5a4SRusty Russell }
8661d0b5a4SRusty Russell 
free_pages_exact(void * p,size_t s)876be3ffaaSMichael S. Tsirkin static inline void free_pages_exact(void *p, size_t s)
886be3ffaaSMichael S. Tsirkin {
896be3ffaaSMichael S. Tsirkin 	kfree(p);
906be3ffaaSMichael S. Tsirkin }
916be3ffaaSMichael S. Tsirkin 
krealloc(void * p,size_t s,gfp_t gfp)9261d0b5a4SRusty Russell static inline void *krealloc(void *p, size_t s, gfp_t gfp)
9361d0b5a4SRusty Russell {
9461d0b5a4SRusty Russell 	return realloc(p, s);
9561d0b5a4SRusty Russell }
9661d0b5a4SRusty Russell 
9761d0b5a4SRusty Russell 
__get_free_page(gfp_t gfp)9861d0b5a4SRusty Russell static inline unsigned long __get_free_page(gfp_t gfp)
9961d0b5a4SRusty Russell {
10061d0b5a4SRusty Russell 	void *p;
10161d0b5a4SRusty Russell 
10261d0b5a4SRusty Russell 	posix_memalign(&p, PAGE_SIZE, PAGE_SIZE);
10361d0b5a4SRusty Russell 	return (unsigned long)p;
10461d0b5a4SRusty Russell }
10561d0b5a4SRusty Russell 
free_page(unsigned long addr)10661d0b5a4SRusty Russell static inline void free_page(unsigned long addr)
10761d0b5a4SRusty Russell {
10861d0b5a4SRusty Russell 	free((void *)addr);
10961d0b5a4SRusty Russell }
11061d0b5a4SRusty Russell 
11161d0b5a4SRusty Russell # ifndef likely
11261d0b5a4SRusty Russell #  define likely(x)	(__builtin_expect(!!(x), 1))
11361d0b5a4SRusty Russell # endif
11461d0b5a4SRusty Russell # ifndef unlikely
11561d0b5a4SRusty Russell #  define unlikely(x)	(__builtin_expect(!!(x), 0))
11661d0b5a4SRusty Russell # endif
11761d0b5a4SRusty Russell 
krealloc_array(void * p,size_t new_n,size_t new_size,gfp_t gfp)118b9ca93bcSPeng Fan static inline void *krealloc_array(void *p, size_t new_n, size_t new_size, gfp_t gfp)
119b9ca93bcSPeng Fan {
120b9ca93bcSPeng Fan 	size_t bytes;
121b9ca93bcSPeng Fan 
122b9ca93bcSPeng Fan 	if (unlikely(check_mul_overflow(new_n, new_size, &bytes)))
123b9ca93bcSPeng Fan 		return NULL;
124b9ca93bcSPeng Fan 
125b9ca93bcSPeng Fan 	return krealloc(p, bytes, gfp);
126b9ca93bcSPeng Fan }
127b9ca93bcSPeng Fan 
12861d0b5a4SRusty Russell #define pr_err(format, ...) fprintf (stderr, format, ## __VA_ARGS__)
12961d0b5a4SRusty Russell #ifdef DEBUG
13061d0b5a4SRusty Russell #define pr_debug(format, ...) fprintf (stderr, format, ## __VA_ARGS__)
13161d0b5a4SRusty Russell #else
13261d0b5a4SRusty Russell #define pr_debug(format, ...) do {} while (0)
13361d0b5a4SRusty Russell #endif
13461d0b5a4SRusty Russell #define dev_err(dev, format, ...) fprintf (stderr, format, ## __VA_ARGS__)
13561d0b5a4SRusty Russell #define dev_warn(dev, format, ...) fprintf (stderr, format, ## __VA_ARGS__)
136d650f830SStefano Garzarella #define dev_warn_once(dev, format, ...) fprintf (stderr, format, ## __VA_ARGS__)
13761d0b5a4SRusty Russell 
13861d0b5a4SRusty Russell #define min(x, y) ({				\
13961d0b5a4SRusty Russell 	typeof(x) _min1 = (x);			\
14061d0b5a4SRusty Russell 	typeof(y) _min2 = (y);			\
14161d0b5a4SRusty Russell 	(void) (&_min1 == &_min2);		\
14261d0b5a4SRusty Russell 	_min1 < _min2 ? _min1 : _min2; })
14361d0b5a4SRusty Russell 
14461d0b5a4SRusty Russell #endif /* KERNEL_H */
145