cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

kernel.h (3370B)


      1/* SPDX-License-Identifier: GPL-2.0 */
      2#ifndef KERNEL_H
      3#define KERNEL_H
      4#include <stdbool.h>
      5#include <stdlib.h>
      6#include <stddef.h>
      7#include <stdio.h>
      8#include <string.h>
      9#include <assert.h>
     10#include <stdarg.h>
     11
     12#include <linux/compiler.h>
     13#include <linux/types.h>
     14#include <linux/overflow.h>
     15#include <linux/list.h>
     16#include <linux/printk.h>
     17#include <linux/bug.h>
     18#include <errno.h>
     19#include <unistd.h>
     20#include <asm/barrier.h>
     21
     22#define CONFIG_SMP
     23
     24#define PAGE_SIZE getpagesize()
     25#define PAGE_MASK (~(PAGE_SIZE-1))
     26#define PAGE_ALIGN(x) ((x + PAGE_SIZE - 1) & PAGE_MASK)
     27
     28/* generic data direction definitions */
     29#define READ                    0
     30#define WRITE                   1
     31
     32typedef unsigned long long phys_addr_t;
     33typedef unsigned long long dma_addr_t;
     34typedef size_t __kernel_size_t;
     35typedef unsigned int __wsum;
     36
     37struct page {
     38	unsigned long long dummy;
     39};
     40
     41/* Physical == Virtual */
     42#define virt_to_phys(p) ((unsigned long)p)
     43#define phys_to_virt(a) ((void *)(unsigned long)(a))
     44/* Page address: Virtual / 4K */
     45#define page_to_phys(p) ((dma_addr_t)(unsigned long)(p))
     46#define virt_to_page(p) ((struct page *)((unsigned long)p & PAGE_MASK))
     47
     48#define offset_in_page(p) (((unsigned long)p) % PAGE_SIZE)
     49
     50#define __printf(a,b) __attribute__((format(printf,a,b)))
     51
     52#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
     53
     54extern void *__kmalloc_fake, *__kfree_ignore_start, *__kfree_ignore_end;
     55static inline void *kmalloc(size_t s, gfp_t gfp)
     56{
     57	if (__kmalloc_fake)
     58		return __kmalloc_fake;
     59	return malloc(s);
     60}
     61static inline void *kmalloc_array(unsigned n, size_t s, gfp_t gfp)
     62{
     63	return kmalloc(n * s, gfp);
     64}
     65
     66static inline void *kzalloc(size_t s, gfp_t gfp)
     67{
     68	void *p = kmalloc(s, gfp);
     69
     70	memset(p, 0, s);
     71	return p;
     72}
     73
     74static inline void *alloc_pages_exact(size_t s, gfp_t gfp)
     75{
     76	return kmalloc(s, gfp);
     77}
     78
     79static inline void kfree(void *p)
     80{
     81	if (p >= __kfree_ignore_start && p < __kfree_ignore_end)
     82		return;
     83	free(p);
     84}
     85
     86static inline void free_pages_exact(void *p, size_t s)
     87{
     88	kfree(p);
     89}
     90
     91static inline void *krealloc(void *p, size_t s, gfp_t gfp)
     92{
     93	return realloc(p, s);
     94}
     95
     96
     97static inline unsigned long __get_free_page(gfp_t gfp)
     98{
     99	void *p;
    100
    101	posix_memalign(&p, PAGE_SIZE, PAGE_SIZE);
    102	return (unsigned long)p;
    103}
    104
    105static inline void free_page(unsigned long addr)
    106{
    107	free((void *)addr);
    108}
    109
    110#define container_of(ptr, type, member) ({			\
    111	const typeof( ((type *)0)->member ) *__mptr = (ptr);	\
    112	(type *)( (char *)__mptr - offsetof(type,member) );})
    113
    114# ifndef likely
    115#  define likely(x)	(__builtin_expect(!!(x), 1))
    116# endif
    117# ifndef unlikely
    118#  define unlikely(x)	(__builtin_expect(!!(x), 0))
    119# endif
    120
    121static inline void *krealloc_array(void *p, size_t new_n, size_t new_size, gfp_t gfp)
    122{
    123	size_t bytes;
    124
    125	if (unlikely(check_mul_overflow(new_n, new_size, &bytes)))
    126		return NULL;
    127
    128	return krealloc(p, bytes, gfp);
    129}
    130
    131#define pr_err(format, ...) fprintf (stderr, format, ## __VA_ARGS__)
    132#ifdef DEBUG
    133#define pr_debug(format, ...) fprintf (stderr, format, ## __VA_ARGS__)
    134#else
    135#define pr_debug(format, ...) do {} while (0)
    136#endif
    137#define dev_err(dev, format, ...) fprintf (stderr, format, ## __VA_ARGS__)
    138#define dev_warn(dev, format, ...) fprintf (stderr, format, ## __VA_ARGS__)
    139
    140#define min(x, y) ({				\
    141	typeof(x) _min1 = (x);			\
    142	typeof(y) _min2 = (y);			\
    143	(void) (&_min1 == &_min2);		\
    144	_min1 < _min2 ? _min1 : _min2; })
    145
    146#endif /* KERNEL_H */