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>
1361d0b5a4SRusty Russell#include <linux/types.h>
14b9ca93bcSPeng Fan#include <linux/overflow.h>
15cb91909eSEugenio Pérez#include <linux/list.h>
1661d0b5a4SRusty Russell#include <linux/printk.h>
1761d0b5a4SRusty Russell#include <linux/bug.h>
1861d0b5a4SRusty Russell#include <errno.h>
1961d0b5a4SRusty Russell#include <unistd.h>
2061d0b5a4SRusty Russell#include <asm/barrier.h>
2161d0b5a4SRusty Russell
2261d0b5a4SRusty Russell#define CONFIG_SMP
2361d0b5a4SRusty Russell
2461d0b5a4SRusty Russell#define PAGE_SIZE getpagesize()
2561d0b5a4SRusty Russell#define PAGE_MASK (~(PAGE_SIZE-1))
266be3ffaaSMichael S. Tsirkin#define PAGE_ALIGN(x) ((x + PAGE_SIZE - 1) & PAGE_MASK)
2761d0b5a4SRusty Russell
28c5c08bedSMichael S. Tsirkin/* generic data direction definitions */
29c5c08bedSMichael S. Tsirkin#define READ                    0
30c5c08bedSMichael S. Tsirkin#define WRITE                   1
31c5c08bedSMichael S. Tsirkin
326be3ffaaSMichael S. Tsirkintypedef unsigned long long phys_addr_t;
3361d0b5a4SRusty Russelltypedef unsigned long long dma_addr_t;
3461d0b5a4SRusty Russelltypedef size_t __kernel_size_t;
35ddab2c0eSMichael S. Tsirkintypedef unsigned int __wsum;
3661d0b5a4SRusty Russell
3761d0b5a4SRusty Russellstruct page {
3861d0b5a4SRusty Russell	unsigned long long dummy;
3961d0b5a4SRusty Russell};
4061d0b5a4SRusty Russell
4161d0b5a4SRusty Russell/* Physical == Virtual */
4261d0b5a4SRusty Russell#define virt_to_phys(p) ((unsigned long)p)
4361d0b5a4SRusty Russell#define phys_to_virt(a) ((void *)(unsigned long)(a))
4461d0b5a4SRusty Russell/* Page address: Virtual / 4K */
4561d0b5a4SRusty Russell#define page_to_phys(p) ((dma_addr_t)(unsigned long)(p))
4661d0b5a4SRusty Russell#define virt_to_page(p) ((struct page *)((unsigned long)p & PAGE_MASK))
4761d0b5a4SRusty Russell
4861d0b5a4SRusty Russell#define offset_in_page(p) (((unsigned long)p) % PAGE_SIZE)
4961d0b5a4SRusty Russell
5061d0b5a4SRusty Russell#define __printf(a,b) __attribute__((format(printf,a,b)))
5161d0b5a4SRusty Russell
5261d0b5a4SRusty Russell#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
5361d0b5a4SRusty Russell
5461d0b5a4SRusty Russellextern void *__kmalloc_fake, *__kfree_ignore_start, *__kfree_ignore_end;
5561d0b5a4SRusty Russellstatic inline void *kmalloc(size_t s, gfp_t gfp)
5661d0b5a4SRusty Russell{
5761d0b5a4SRusty Russell	if (__kmalloc_fake)
5861d0b5a4SRusty Russell		return __kmalloc_fake;
5961d0b5a4SRusty Russell	return malloc(s);
6061d0b5a4SRusty Russell}
61f2467ee0SMichael S. Tsirkinstatic inline void *kmalloc_array(unsigned n, size_t s, gfp_t gfp)
62f2467ee0SMichael S. Tsirkin{
63f2467ee0SMichael S. Tsirkin	return kmalloc(n * s, gfp);
64f2467ee0SMichael S. Tsirkin}
65f2467ee0SMichael S. Tsirkin
66ddab2c0eSMichael S. Tsirkinstatic inline void *kzalloc(size_t s, gfp_t gfp)
67ddab2c0eSMichael S. Tsirkin{
68ddab2c0eSMichael S. Tsirkin	void *p = kmalloc(s, gfp);
69ddab2c0eSMichael S. Tsirkin
70ddab2c0eSMichael S. Tsirkin	memset(p, 0, s);
71ddab2c0eSMichael S. Tsirkin	return p;
72ddab2c0eSMichael S. Tsirkin}
7361d0b5a4SRusty Russell
746be3ffaaSMichael S. Tsirkinstatic inline void *alloc_pages_exact(size_t s, gfp_t gfp)
756be3ffaaSMichael S. Tsirkin{
766be3ffaaSMichael S. Tsirkin	return kmalloc(s, gfp);
776be3ffaaSMichael S. Tsirkin}
786be3ffaaSMichael S. Tsirkin
7961d0b5a4SRusty Russellstatic inline void kfree(void *p)
8061d0b5a4SRusty Russell{
8161d0b5a4SRusty Russell	if (p >= __kfree_ignore_start && p < __kfree_ignore_end)
8261d0b5a4SRusty Russell		return;
8361d0b5a4SRusty Russell	free(p);
8461d0b5a4SRusty Russell}
8561d0b5a4SRusty Russell
866be3ffaaSMichael S. Tsirkinstatic inline void free_pages_exact(void *p, size_t s)
876be3ffaaSMichael S. Tsirkin{
886be3ffaaSMichael S. Tsirkin	kfree(p);
896be3ffaaSMichael S. Tsirkin}
906be3ffaaSMichael S. Tsirkin
9161d0b5a4SRusty Russellstatic inline void *krealloc(void *p, size_t s, gfp_t gfp)
9261d0b5a4SRusty Russell{
9361d0b5a4SRusty Russell	return realloc(p, s);
9461d0b5a4SRusty Russell}
9561d0b5a4SRusty Russell
9661d0b5a4SRusty Russell
9761d0b5a4SRusty Russellstatic inline unsigned long __get_free_page(gfp_t gfp)
9861d0b5a4SRusty Russell{
9961d0b5a4SRusty Russell	void *p;
10061d0b5a4SRusty Russell
10161d0b5a4SRusty Russell	posix_memalign(&p, PAGE_SIZE, PAGE_SIZE);
10261d0b5a4SRusty Russell	return (unsigned long)p;
10361d0b5a4SRusty Russell}
10461d0b5a4SRusty Russell
10561d0b5a4SRusty Russellstatic inline void free_page(unsigned long addr)
10661d0b5a4SRusty Russell{
10761d0b5a4SRusty Russell	free((void *)addr);
10861d0b5a4SRusty Russell}
10961d0b5a4SRusty Russell
11061d0b5a4SRusty Russell#define container_of(ptr, type, member) ({			\
11161d0b5a4SRusty Russell	const typeof( ((type *)0)->member ) *__mptr = (ptr);	\
11261d0b5a4SRusty Russell	(type *)( (char *)__mptr - offsetof(type,member) );})
11361d0b5a4SRusty Russell
11461d0b5a4SRusty Russell# ifndef likely
11561d0b5a4SRusty Russell#  define likely(x)	(__builtin_expect(!!(x), 1))
11661d0b5a4SRusty Russell# endif
11761d0b5a4SRusty Russell# ifndef unlikely
11861d0b5a4SRusty Russell#  define unlikely(x)	(__builtin_expect(!!(x), 0))
11961d0b5a4SRusty Russell# endif
12061d0b5a4SRusty Russell
121b9ca93bcSPeng Fanstatic inline void *krealloc_array(void *p, size_t new_n, size_t new_size, gfp_t gfp)
122b9ca93bcSPeng Fan{
123b9ca93bcSPeng Fan	size_t bytes;
124b9ca93bcSPeng Fan
125b9ca93bcSPeng Fan	if (unlikely(check_mul_overflow(new_n, new_size, &bytes)))
126b9ca93bcSPeng Fan		return NULL;
127b9ca93bcSPeng Fan
128b9ca93bcSPeng Fan	return krealloc(p, bytes, gfp);
129b9ca93bcSPeng Fan}
130b9ca93bcSPeng Fan
13161d0b5a4SRusty Russell#define pr_err(format, ...) fprintf (stderr, format, ## __VA_ARGS__)
13261d0b5a4SRusty Russell#ifdef DEBUG
13361d0b5a4SRusty Russell#define pr_debug(format, ...) fprintf (stderr, format, ## __VA_ARGS__)
13461d0b5a4SRusty Russell#else
13561d0b5a4SRusty Russell#define pr_debug(format, ...) do {} while (0)
13661d0b5a4SRusty Russell#endif
13761d0b5a4SRusty Russell#define dev_err(dev, format, ...) fprintf (stderr, format, ## __VA_ARGS__)
13861d0b5a4SRusty Russell#define dev_warn(dev, format, ...) fprintf (stderr, format, ## __VA_ARGS__)
13961d0b5a4SRusty Russell
14061d0b5a4SRusty Russell#define min(x, y) ({				\
14161d0b5a4SRusty Russell	typeof(x) _min1 = (x);			\
14261d0b5a4SRusty Russell	typeof(y) _min2 = (y);			\
14361d0b5a4SRusty Russell	(void) (&_min1 == &_min2);		\
14461d0b5a4SRusty Russell	_min1 < _min2 ? _min1 : _min2; })
14561d0b5a4SRusty Russell
14661d0b5a4SRusty Russell#endif /* KERNEL_H */
147