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