📄 kernelenv.h
字号:
/* * Copyright (c) 2003,2004 Jeremy Kerr & Rusty Russell This file is part of nfsim. It has been modified by Michael Richardson <mcr@xelerance.com> to be used by the Openswan KLIPS unit testing. Based upon SVN revision 3615 as released in 20050423 tarball. $Id: kernelenv.h,v 1.4 2005/04/28 21:18:36 mcr Exp $ nfsim is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. nfsim is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with nfsim; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*//** * Functions to provide a simlulated kernel environment. */#ifndef __HAVE_SIMULATOR_H#define __HAVE_SIMULATOR_H 1#define __KERNEL__ 1#include <linux/config.h>#include <linux/version.h>#include <stddef.h>#include <stdint.h>#include <limits.h>#include <string.h>#include <stdio.h>#include <errno.h>#include <stdlib.h>#include <assert.h>#include <ctype.h>#include <list.h>/* Types */typedef uint8_t u8;typedef int8_t s8;typedef uint16_t u16;typedef int16_t s16;typedef uint32_t u32;typedef int32_t s32;typedef uint64_t u64;typedef int64_t s64;#define __u8 u8#define __s8 s8#define __u16 u16#define __s16 s16#define __u32 u32#define __s32 s32#define __u64 u64#define __s64 u64/* avoid inclusion of asm/types.h */#define _I386_TYPES_H/* Hacky, but works for now */#define BITS_PER_LONG (ULONG_MAX == 0xFFFFFFFFUL ? 32 : 64)/* casts are necessary for constants, because we never know how for sure * how U/UL/ULL map to u16, u32, u64. At least not in a portable way. */#define swab16(x) \ ((u16)( \ (((u16)(x) & (u16)0x00ffU) << 8) | \ (((u16)(x) & (u16)0xff00U) >> 8) ))#define swab32(x) \ ((u32)( \ (((u32)(x) & (u32)0x000000ffUL) << 24) | \ (((u32)(x) & (u32)0x0000ff00UL) << 8) | \ (((u32)(x) & (u32)0x00ff0000UL) >> 8) | \ (((u32)(x) & (u32)0xff000000UL) >> 24) ))#define swab64(x) \ ((u64)( \ (u64)(((u64)(x) & (u64)0x00000000000000ffULL) << 56) | \ (u64)(((u64)(x) & (u64)0x000000000000ff00ULL) << 40) | \ (u64)(((u64)(x) & (u64)0x0000000000ff0000ULL) << 24) | \ (u64)(((u64)(x) & (u64)0x00000000ff000000ULL) << 8) | \ (u64)(((u64)(x) & (u64)0x000000ff00000000ULL) >> 8) | \ (u64)(((u64)(x) & (u64)0x0000ff0000000000ULL) >> 24) | \ (u64)(((u64)(x) & (u64)0x00ff000000000000ULL) >> 40) | \ (u64)(((u64)(x) & (u64)0xff00000000000000ULL) >> 56) ))#define swab16p(p) (swab16(*(p)))#define swab32p(p) (swab32(*(p)))#define swab64p(p) (swab64(*(p)))#include "kernelenv_endian.h"u32 htonl(u32 hostlong);u16 htons(u16 hostshort);u32 ntohl(u32 netlong);u16 ntohs(u16 netshort);#define smp_wmb()#define wmb()#define barrier()#define mb()/* Put v in *ptr atomically and return old *ptr value. */#define xchg(ptr,v) ({ __typeof__(*ptr) __a, *__p = (ptr); __a = *__p; *__p = (v); __a; })#define __user#define unlikely(x) (x)#define likely(x) (x)#define __stringify_1(x) #x#define __stringify(x) __stringify_1(x)/* * min()/max() macros that also do * strict type-checking.. See the * "unnecessary" pointer comparison. */#define min(x,y) ({ \ const typeof(x) _x = (x); \ const typeof(y) _y = (y); \ (void) (&_x == &_y); \ _x < _y ? _x : _y; })#define max(x,y) ({ \ const typeof(x) _x = (x); \ const typeof(y) _y = (y); \ (void) (&_x == &_y); \ _x > _y ? _x : _y; })/* * ..and if you can't take the strict * types, you can specify one yourself. * * Or not use min/max at all, of course. */#define min_t(type,x,y) \ ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })#define max_t(type,x,y) \ ({ type __x = (x); type __y = (y); __x > __y ? __x: __y; })/* logging */#include <log.h>#define u_int8_t uint8_t#define u_int16_t uint16_t#define u_int32_t uint32_t#define u_int64_t uint64_t#define __init#define __inline#define ____cacheline_aligned __attribute__((aligned(8)))#include <talloc.h>extern void *__vmalloc_ctx;extern void *__kmalloc_atomic_ctx;extern void *__kmalloc_ctx;void *__malloc(unsigned int, void *ctx, const char *location);#define vmalloc(s) __malloc((s), __vmalloc_ctx, __location__)#define kmalloc(s,f) __malloc((s), (f) & GFP_ATOMIC ? __kmalloc_atomic_ctx : __kmalloc_ctx, __location__)#define vfree(p) talloc_unlink(__vmalloc_ctx, (p))#define kfree(p) talloc_free(p)#define synchronize_net() #define dump_stack()void schedule(void);#define BUG_ON(x) do { if (x) barf("%s:%u", __FILE__, __LINE__); } while(0)#define BUG() BUG_ON(1)#define cli()#define sti()#define NR_CPUS 1#define SMP_CACHE_BYTES (1<<7)#if LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0)#define cpu_possible(cpu) ((cpu) == 0)#endif#define smp_processor_id() 0#define PAGE_SHIFT 12#define PAGE_SIZE (1UL << PAGE_SHIFT)#define PAGE_MASK (~(PAGE_SIZE-1))#define SLAB_HWCACHE_ALIGN 0x00002000UL#define local_bh_disable()#define local_bh_enable()#define stricmp strcasecmp#define strnicmp strncasecmpextern unsigned long num_physpages;extern void barf(const char *fmt, ...);#define panic barf/* kernel.h */#define NIPQUAD(addr) \ ((unsigned char *)&addr)[0], \ ((unsigned char *)&addr)[1], \ ((unsigned char *)&addr)[2], \ ((unsigned char *)&addr)[3]#if defined(__LITTLE_ENDIAN)#define HIPQUAD(addr) \ ((unsigned char *)&addr)[3], \ ((unsigned char *)&addr)[2], \ ((unsigned char *)&addr)[1], \ ((unsigned char *)&addr)[0]#elif defined(__BIG_ENDIAN)#define HIPQUAD NIPQUAD#else#error "Please fix asm/byteorder.h"#endif /* __LITTLE_ENDIAN */#define HZ 100#define KERN_EMERG "EMERG:"/* system is unusable */#define KERN_ALERT "ALERT:"/* action must be taken immediately */#define KERN_CRIT "CRIT:" /* critical conditions */#define KERN_ERR "ERR;" /* error conditions */#define KERN_WARNING "WARN:" /* warning conditions */#define KERN_NOTICE "NOTICE:"/* normal but significant condition */#define KERN_INFO "INFO:" /* informational */#define KERN_DEBUG "DEBUG:"/* debug-level messages */#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))#define ALIGN(x,a) (((x)+(a)-1)&~((a)-1))/* err.h */#define ERR_PTR(x) ((void *)(x))#define PTR_ERR(x) ((long)(x))int IS_ERR(const void *ptr);/* we start at time 0 */#define INITIAL_JIFFIES 0extern unsigned long jiffies;#define typecheck(type,x) \({ type __dummy; \ typeof(x) __dummy2; \ (void)(&__dummy == &__dummy2); \ 1; \})#define time_after(a,b) \ (typecheck(unsigned long, a) && \ typecheck(unsigned long, b) && \ ((long)(b) - (long)(a) < 0))#define time_before(a,b) time_after(b,a)#define time_after_eq(a,b) \ (typecheck(unsigned long, a) && \ typecheck(unsigned long, b) && \ ((long)(a) - (long)(b) >= 0))#define time_before_eq(a,b) time_after_eq(b,a)#define container_of(ptr, type, member) ({ \ typeof( ((type *)0)->member ) *__mptr = (ptr); \ (type *)( (char *)__mptr - offsetof(type,member) );})/* atomic.h */#define __ARCH_I386_ATOMIC__ 1typedef struct { volatile int counter; } atomic_t;#define ATOMIC_INIT(i) { (i) }#define atomic_read(v) ((v)->counter)#define atomic_set(v,i) (((v)->counter) = (i))#define atomic_add(v,i) (((v)->counter) += (i))void atomic_inc(atomic_t *v);void atomic_dec(atomic_t *v);int atomic_dec_and_test(atomic_t *v);/* rc_update.h */struct rcu_head { struct list_head list; void (*func)(void *obj); void *arg;};/* list.h's RCU stuff *//** * list_for_each_rcu - iterate over an rcu-protected list * @pos: the &struct list_head to use as a loop counter. * @head: the head for your list. */#define list_for_each_rcu(pos, head) \ for (pos = (head)->next; pos != (head); \ pos = pos->next, ({ smp_read_barrier_depends(); 0;}), prefetch(pos->next)) #define __list_for_each_rcu(pos, head) \ for (pos = (head)->next; pos != (head); \ pos = pos->next, ({ smp_read_barrier_depends(); 0;})) /** * list_for_each_safe_rcu - iterate over an rcu-protected list safe * against removal of list entry * @pos: the &struct list_head to use as a loop counter. * @n: another &struct list_head to use as temporary storage * @head: the head for your list. */#define list_for_each_safe_rcu(pos, n, head) \ for (pos = (head)->next, n = pos->next; pos != (head); \ pos = n, ({ smp_read_barrier_depends(); 0;}), n = pos->next)/** * list_for_each_entry_rcu - iterate over rcu list of given type * @pos: the type * to use as a loop counter. * @head: the head for your list. * @member: the name of the list_struct within the struct. */#define list_for_each_entry_rcu(pos, head, member) \ for (pos = list_entry((head)->next, typeof(*pos), member), \ prefetch(pos->member.next); \ &pos->member != (head); \ pos = list_entry(pos->member.next, typeof(*pos), member), \ ({ smp_read_barrier_depends(); 0;}), \ prefetch(pos->member.next))/** * list_for_each_continue_rcu - iterate over an rcu-protected list * continuing after existing point. * @pos: the &struct list_head to use as a loop counter. * @head: the head for your list. */#define list_for_each_continue_rcu(pos, head) \ for ((pos) = (pos)->next, prefetch((pos)->next); (pos) != (head); \ (pos) = (pos)->next, ({ smp_read_barrier_depends(); 0;}), prefetch((pos)->next))/* spinlock.h *//* no difference between spin and rw locks at present */typedef struct { volatile int lock; char *location;} spinlock_t;typedef spinlock_t rwlock_t;#define RW_LOCK_UNLOCKED (rwlock_t) { 0, NULL }#define SPIN_LOCK_UNLOCKED (spinlock_t) { 0, NULL }#define DEFINE_SPINLOCK(x) spinlock_t x = SPIN_LOCK_UNLOCKED#define DEFINE_RWLOCK(x) rwlock_t x = RW_LOCK_UNLOCKED#define spin_lock_init(x) \ do { \ (x)->lock = 0; \ (x)->location = NULL; \ } while (0)#define rwlock_init(x) spin_lock_init(x)#define spin_lock(x) __generic_write_lock((x), __location__)#define spin_unlock(x) __generic_write_unlock((x), __location__)#define spin_lock_bh(x) __generic_write_lock((x), __location__)#define spin_unlock_bh(x) __generic_write_unlock((x), __location__)#define spin_lock_irq(x) __generic_write_lock((x), __location__)#define spin_unlock_irq(x) __generic_write_unlock((x), __location__)#define spin_lock_irqsave(x,f) __generic_write_lock((x), __location__); f++#define spin_unlock_irqrestore(x,f) __generic_write_unlock((x), __location__); f--#define read_lock_bh(x) __generic_read_lock((x), __location__)#define read_unlock_bh(x) __generic_read_unlock((x), __location__)#define read_lock(x) __generic_read_lock((x), __location__)#define read_unlock(x) __generic_read_unlock((x), __location__)#define write_lock_bh(x) __generic_write_lock((x), __location__)#define write_unlock_bh(x) __generic_write_unlock((x), __location__)#define rcu_read_lock()#define rcu_read_unlock()#define rcu_dereference(p) (p)#define rcu_assign_pointer(p, v) ({ \ smp_wmb(); \ (p) = (v); \ })void __generic_read_lock(spinlock_t *lock, const char *location);void __generic_read_unlock(spinlock_t *lock, const char *location); void __generic_write_lock(spinlock_t *lock, const char *location);void __generic_write_unlock(spinlock_t *lock, const char *location); #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)/* brlock.h */#define br_write_lock_bh(lock)#define br_write_unlock_bh(lock)#define br_write_lock(lock)#define br_write_unlock(lock)#define br_read_lock_bh(lock)#define br_read_unlock_bh(lock)#endif/* vsprintf.h */unsigned long int strtoul(const char *nptr, char **endptr, int base);#define simple_strtoul strtoul
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -