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 int32 lock; 24 int32 failed_try_acquire; 25 bigtime_t total_wait; 26 bigtime_t total_held; 27 bigtime_t last_acquired; 28 } spinlock; 29 30 # define B_SPINLOCK_INITIALIZER { 0, 0, 0, 0, 0 } 31 # define B_INITIALIZE_SPINLOCK(spinlock) do { \ 32 (spinlock)->lock = 0; \ 33 (spinlock)->failed_try_acquire = 0; \ 34 (spinlock)->total_wait = 0; \ 35 (spinlock)->total_held = 0; \ 36 (spinlock)->last_acquired = 0; \ 37 } while (false) 38 #else 39 typedef struct { 40 int32 lock; 41 } spinlock; 42 43 # define B_SPINLOCK_INITIALIZER { 0 } 44 # define B_INITIALIZE_SPINLOCK(spinlock) do { \ 45 (spinlock)->lock = 0; \ 46 } while (false) 47 #endif 48 49 #define B_SPINLOCK_IS_LOCKED(spinlock) (atomic_get(&(spinlock)->lock) > 0) 50 51 typedef struct { 52 int32 lock; 53 } rw_spinlock; 54 55 #define B_RW_SPINLOCK_INITIALIZER { 0 } 56 #define B_INITIALIZE_RW_SPINLOCK(rw_spinlock) do { \ 57 (rw_spinlock)->lock = 0; \ 58 } while (false) 59 60 typedef struct { 61 spinlock lock; 62 uint32 count; 63 } seqlock; 64 65 #define B_SEQLOCK_INITIALIZER { B_SPINLOCK_INITIALIZER, 0 } 66 #define B_INITIALIZE_SEQLOCK(seqlock) do { \ 67 B_INITIALIZE_SPINLOCK(&(seqlock)->lock); \ 68 (seqlock)->count = 0; \ 69 } while (false) 70 71 /* interrupt handling support for device drivers */ 72 73 typedef int32 (*interrupt_handler)(void *data); 74 75 /* Values returned by interrupt handlers */ 76 #define B_UNHANDLED_INTERRUPT 0 /* pass to next handler */ 77 #define B_HANDLED_INTERRUPT 1 /* don't pass on */ 78 #define B_INVOKE_SCHEDULER 2 /* don't pass on; invoke the scheduler */ 79 80 /* Flags that can be passed to install_io_interrupt_handler() */ 81 #define B_NO_ENABLE_COUNTER 1 82 83 84 /* timer interrupts services */ 85 86 typedef struct timer timer; 87 typedef int32 (*timer_hook)(timer *); 88 89 struct timer { 90 struct timer *next; 91 int64 schedule_time; 92 void *user_data; 93 uint16 flags; 94 uint16 cpu; 95 timer_hook hook; 96 bigtime_t period; 97 }; 98 99 #define B_ONE_SHOT_ABSOLUTE_TIMER 1 100 #define B_ONE_SHOT_RELATIVE_TIMER 2 101 #define B_PERIODIC_TIMER 3 102 103 104 /* virtual memory buffer functions */ 105 106 #define B_DMA_IO 0x00000001 107 #define B_READ_DEVICE 0x00000002 108 109 typedef struct { 110 phys_addr_t address; /* address in physical memory */ 111 phys_size_t size; /* size of block */ 112 } physical_entry; 113 114 /* address specifications for mapping physical memory */ 115 #define B_ANY_KERNEL_BLOCK_ADDRESS (B_ANY_KERNEL_ADDRESS + 1) 116 117 /* memory types for physical memory */ 118 #define B_UNCACHED_MEMORY (1 << 28) 119 #define B_WRITE_COMBINING_MEMORY (2 << 28) 120 #define B_WRITE_THROUGH_MEMORY (3 << 28) 121 #define B_WRITE_PROTECTED_MEMORY (4 << 28) 122 #define B_WRITE_BACK_MEMORY (5 << 28) 123 #define B_MEMORY_TYPE_MASK (0xf0000000) 124 125 /* area protection flags for the kernel */ 126 #define B_KERNEL_READ_AREA (1 << 4) 127 #define B_KERNEL_WRITE_AREA (1 << 5) 128 #define B_KERNEL_EXECUTE_AREA (1 << 6) 129 #define B_KERNEL_STACK_AREA (1 << 7) 130 131 132 /* kernel daemon service */ 133 134 typedef void (*daemon_hook)(void *arg, int iteration); 135 136 137 /* kernel debugging facilities */ 138 139 /* special return codes for kernel debugger */ 140 #define B_KDEBUG_CONT 2 141 #define B_KDEBUG_QUIT 3 142 143 typedef int (*debugger_command_hook)(int argc, char **argv); 144 145 146 #ifdef __cplusplus 147 extern "C" { 148 #endif 149 150 /* interrupts, spinlock, and timers */ 151 extern cpu_status disable_interrupts(void); 152 extern void restore_interrupts(cpu_status status); 153 154 extern void acquire_spinlock(spinlock *lock); 155 extern void release_spinlock(spinlock *lock); 156 157 extern bool try_acquire_write_spinlock(rw_spinlock* lock); 158 extern void acquire_write_spinlock(rw_spinlock* lock); 159 extern void release_write_spinlock(rw_spinlock* lock); 160 extern bool try_acquire_read_spinlock(rw_spinlock* lock); 161 extern void acquire_read_spinlock(rw_spinlock* lock); 162 extern void release_read_spinlock(rw_spinlock* lock); 163 164 extern bool try_acquire_write_seqlock(seqlock* lock); 165 extern void acquire_write_seqlock(seqlock* lock); 166 extern void release_write_seqlock(seqlock* lock); 167 extern uint32 acquire_read_seqlock(seqlock* lock); 168 extern bool release_read_seqlock(seqlock* lock, uint32 count); 169 170 extern status_t install_io_interrupt_handler(int32 interrupt_number, 171 interrupt_handler handler, void *data, uint32 flags); 172 extern status_t remove_io_interrupt_handler(int32 interrupt_number, 173 interrupt_handler handler, void *data); 174 175 extern status_t add_timer(timer *t, timer_hook hook, bigtime_t period, 176 int32 flags); 177 extern bool cancel_timer(timer *t); 178 179 /* kernel threads */ 180 extern thread_id spawn_kernel_thread(thread_func function, 181 const char *name, int32 priority, void *arg); 182 183 /* signal functions */ 184 extern int send_signal_etc(pid_t thread, uint signal, uint32 flags); 185 186 /* virtual memory */ 187 extern status_t lock_memory_etc(team_id team, void *buffer, size_t numBytes, 188 uint32 flags); 189 extern status_t lock_memory(void *buffer, size_t numBytes, uint32 flags); 190 extern status_t unlock_memory_etc(team_id team, void *address, 191 size_t numBytes, uint32 flags); 192 extern status_t unlock_memory(void *buffer, size_t numBytes, uint32 flags); 193 extern status_t get_memory_map_etc(team_id team, const void *address, 194 size_t numBytes, physical_entry *table, 195 uint32* _numEntries); 196 extern int32 get_memory_map(const void *buffer, size_t size, 197 physical_entry *table, int32 numEntries); 198 extern area_id map_physical_memory(const char *areaName, 199 phys_addr_t physicalAddress, size_t size, uint32 flags, 200 uint32 protection, void **_mappedAddress); 201 202 /* kernel debugging facilities */ 203 #if defined(_KERNEL_MODE) || defined(_BOOT_MODE) 204 extern void dprintf(const char *format, ...) _PRINTFLIKE(1, 2); 205 #endif 206 extern void dvprintf(const char *format, va_list args); 207 extern void kprintf(const char *fmt, ...) _PRINTFLIKE(1, 2); 208 209 extern void dump_block(const char *buffer, int size, const char *prefix); 210 /* TODO: temporary API: hexdumps given buffer */ 211 212 extern bool set_dprintf_enabled(bool new_state); 213 214 extern void panic(const char *format, ...) _PRINTFLIKE(1, 2); 215 216 extern void kernel_debugger(const char *message); 217 extern uint64 parse_expression(const char *string); 218 219 extern int add_debugger_command(const char *name, 220 debugger_command_hook hook, const char *help); 221 extern int remove_debugger_command(const char *name, 222 debugger_command_hook hook); 223 224 /* Miscellaneous */ 225 extern void spin(bigtime_t microseconds); 226 227 extern status_t register_kernel_daemon(daemon_hook hook, void *arg, 228 int frequency); 229 extern status_t unregister_kernel_daemon(daemon_hook hook, void *arg); 230 231 extern void call_all_cpus(void (*func)(void *, int), void *cookie); 232 extern void call_all_cpus_sync(void (*func)(void *, int), void *cookie); 233 extern void memory_read_barrier(void); 234 extern void memory_write_barrier(void); 235 236 /* safe methods to access user memory without having to lock it */ 237 extern status_t user_memcpy(void *to, const void *from, size_t size); 238 extern ssize_t user_strlcpy(char *to, const char *from, size_t size); 239 extern status_t user_memset(void *start, char c, size_t count); 240 241 #ifdef __cplusplus 242 } 243 #endif 244 245 #endif /* _KERNEL_EXPORT_H */ 246