1 /* 2 * Copyright 2005-2008, Haiku Inc. All Rights Reserved. 3 * Distributed under the terms of the MIT License. 4 */ 5 #ifndef _KERNEL_EXPORT_H 6 #define _KERNEL_EXPORT_H 7 8 9 #include <SupportDefs.h> 10 #include <OS.h> 11 12 13 /* interrupts and spinlocks */ 14 15 typedef ulong cpu_status; 16 17 // WARNING: For Haiku debugging only! This changes the spinlock type in a 18 // binary incompatible way! 19 //#define B_DEBUG_SPINLOCK_CONTENTION 1 20 21 #if B_DEBUG_SPINLOCK_CONTENTION 22 typedef struct { 23 vint32 lock; 24 vint32 count_low; 25 vint32 count_high; 26 } spinlock; 27 28 # define B_SPINLOCK_INITIALIZER { 0, 0, 0 } 29 # define B_INITIALIZE_SPINLOCK(spinlock) do { \ 30 (spinlock)->lock = 0; \ 31 (spinlock)->count_low = 0; \ 32 (spinlock)->count_high = 0; \ 33 } while (false) 34 # define B_SPINLOCK_IS_LOCKED(spinlock) ((spinlock)->lock > 0) 35 #else 36 typedef vint32 spinlock; 37 38 # define B_SPINLOCK_INITIALIZER 0 39 # define B_INITIALIZE_SPINLOCK(lock) do { *(lock) = 0; } while (false) 40 # define B_SPINLOCK_IS_LOCKED(lock) (*(lock) > 0) 41 #endif 42 43 /* interrupt handling support for device drivers */ 44 45 typedef int32 (*interrupt_handler)(void *data); 46 47 /* Values returned by interrupt handlers */ 48 #define B_UNHANDLED_INTERRUPT 0 /* pass to next handler */ 49 #define B_HANDLED_INTERRUPT 1 /* don't pass on */ 50 #define B_INVOKE_SCHEDULER 2 /* don't pass on; invoke the scheduler */ 51 52 /* Flags that can be passed to install_io_interrupt_handler() */ 53 #define B_NO_ENABLE_COUNTER 1 54 #define B_NO_LOCK_VECTOR 2 55 56 57 /* timer interrupts services */ 58 59 typedef struct timer timer; 60 typedef int32 (*timer_hook)(timer *); 61 62 struct timer { 63 struct timer *next; 64 int64 schedule_time; 65 void *user_data; 66 uint16 flags; 67 uint16 cpu; 68 timer_hook hook; 69 bigtime_t period; 70 }; 71 72 #define B_ONE_SHOT_ABSOLUTE_TIMER 1 73 #define B_ONE_SHOT_RELATIVE_TIMER 2 74 #define B_PERIODIC_TIMER 3 75 76 77 /* virtual memory buffer functions */ 78 79 #define B_DMA_IO 0x00000001 80 #define B_READ_DEVICE 0x00000002 81 82 typedef struct { 83 void *address; /* address in physical memory */ 84 ulong size; /* size of block */ 85 } physical_entry; 86 87 /* address specifications for mapping physical memory */ 88 #define B_ANY_KERNEL_BLOCK_ADDRESS (B_ANY_KERNEL_ADDRESS + 1) 89 #define B_PHYSICAL_BASE_ADDRESS (B_ANY_KERNEL_ADDRESS + 2) 90 91 /* area protection flags for the kernel */ 92 #define B_KERNEL_READ_AREA 16 93 #define B_KERNEL_WRITE_AREA 32 94 #define B_USER_CLONEABLE_AREA 256 95 96 /* MTR attributes for mapping physical memory (Intel Architecture only) */ 97 // TODO: rename those to something more meaningful 98 #define B_MTR_UC 0x10000000 99 #define B_MTR_WC 0x20000000 100 #define B_MTR_WT 0x30000000 101 #define B_MTR_WP 0x40000000 102 #define B_MTR_WB 0x50000000 103 #define B_MTR_MASK 0xf0000000 104 105 106 /* kernel daemon service */ 107 108 typedef void (*daemon_hook)(void *arg, int iteration); 109 110 111 /* kernel debugging facilities */ 112 113 /* special return codes for kernel debugger */ 114 #define B_KDEBUG_CONT 2 115 #define B_KDEBUG_QUIT 3 116 117 typedef int (*debugger_command_hook)(int argc, char **argv); 118 119 120 #ifdef __cplusplus 121 extern "C" { 122 #endif 123 124 /* interrupts, spinlock, and timers */ 125 extern cpu_status disable_interrupts(void); 126 extern void restore_interrupts(cpu_status status); 127 128 extern void acquire_spinlock(spinlock *lock); 129 extern void release_spinlock(spinlock *lock); 130 131 extern status_t install_io_interrupt_handler(long interrupt_number, 132 interrupt_handler handler, void *data, ulong flags); 133 extern status_t remove_io_interrupt_handler(long interrupt_number, 134 interrupt_handler handler, void *data); 135 136 extern status_t add_timer(timer *t, timer_hook hook, bigtime_t period, 137 int32 flags); 138 extern bool cancel_timer(timer *t); 139 140 /* kernel threads */ 141 extern thread_id spawn_kernel_thread(thread_func function, 142 const char *name, int32 priority, void *arg); 143 144 /* signal functions */ 145 extern int send_signal_etc(pid_t thread, uint signal, uint32 flags); 146 147 /* virtual memory */ 148 extern status_t lock_memory_etc(team_id team, void *buffer, size_t numBytes, 149 uint32 flags); 150 extern status_t lock_memory(void *buffer, size_t numBytes, uint32 flags); 151 extern status_t unlock_memory_etc(team_id team, void *address, 152 size_t numBytes, uint32 flags); 153 extern status_t unlock_memory(void *buffer, size_t numBytes, uint32 flags); 154 extern status_t get_memory_map_etc(team_id team, const void *address, 155 size_t numBytes, physical_entry *table, 156 uint32* _numEntries); 157 extern long get_memory_map(const void *buffer, ulong size, 158 physical_entry *table, long numEntries); 159 extern area_id map_physical_memory(const char *areaName, 160 void *physicalAddress, size_t size, uint32 flags, 161 uint32 protection, void **_mappedAddress); 162 163 /* kernel debugging facilities */ 164 extern void dprintf(const char *format, ...) _PRINTFLIKE(1, 2); 165 extern void kprintf(const char *fmt, ...) _PRINTFLIKE(1, 2); 166 167 extern void dump_block(const char *buffer, int size, const char *prefix); 168 /* TODO: temporary API: hexdumps given buffer */ 169 170 extern bool set_dprintf_enabled(bool new_state); 171 172 extern void panic(const char *format, ...) _PRINTFLIKE(1, 2); 173 174 extern void kernel_debugger(const char *message); 175 extern uint64 parse_expression(const char *string); 176 177 extern int add_debugger_command(char *name, debugger_command_hook hook, char *help); 178 extern int remove_debugger_command(char *name, 179 debugger_command_hook hook); 180 181 /* Miscellaneous */ 182 extern void spin(bigtime_t microseconds); 183 184 extern status_t register_kernel_daemon(daemon_hook hook, void *arg, 185 int frequency); 186 extern status_t unregister_kernel_daemon(daemon_hook hook, void *arg); 187 188 extern void call_all_cpus(void (*func)(void *, int), void *cookie); 189 extern void call_all_cpus_sync(void (*func)(void *, int), void *cookie); 190 extern void memory_read_barrier(void); 191 extern void memory_write_barrier(void); 192 193 /* safe methods to access user memory without having to lock it */ 194 extern status_t user_memcpy(void *to, const void *from, size_t size); 195 extern ssize_t user_strlcpy(char *to, const char *from, size_t size); 196 extern status_t user_memset(void *start, char c, size_t count); 197 198 #ifdef __cplusplus 199 } 200 #endif 201 202 #endif /* _KERNEL_EXPORT_H */ 203