xref: /haiku/headers/os/drivers/KernelExport.h (revision e6b30aee0fd7a23d6a6baab9f3718945a0cd838a)
1 /* Kernel only exports for kernel add-ons
2  *
3  * Copyright 2005, Haiku Inc. All Rights Reserved.
4  * Distributed under the terms of the MIT License.
5  */
6 #ifndef _KERNEL_EXPORT_H
7 #define _KERNEL_EXPORT_H
8 
9 
10 #include <SupportDefs.h>
11 #include <OS.h>
12 
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
18 /*-------------------------------------------------------------*/
19 /* interrupts and spinlocks */
20 
21 /* disable/restore interrupts on the current CPU */
22 
23 typedef ulong cpu_status;
24 
25 extern cpu_status	disable_interrupts(void);
26 extern void			restore_interrupts(cpu_status status);
27 
28 
29 /* spinlocks.  Note that acquire/release should be called with
30  * interrupts disabled.
31  */
32 
33 typedef vint32 spinlock;
34 
35 extern void			acquire_spinlock(spinlock *lock);
36 extern void			release_spinlock(spinlock *lock);
37 
38 
39 /* interrupt handling support for device drivers */
40 
41 typedef int32 (*interrupt_handler)(void *data);
42 
43 /* Values returned by interrupt handlers */
44 #define B_UNHANDLED_INTERRUPT	0	/* pass to next handler */
45 #define B_HANDLED_INTERRUPT		1	/* don't pass on */
46 #define B_INVOKE_SCHEDULER		2	/* don't pass on; invoke the scheduler */
47 
48 /* Flags that can be passed to install_io_interrupt_handler() */
49 #define B_NO_ENABLE_COUNTER		1
50 
51 extern status_t		install_io_interrupt_handler(long interrupt_number,
52 						interrupt_handler handler, void *data, ulong flags);
53 extern status_t		remove_io_interrupt_handler(long interrupt_number,
54 						interrupt_handler handler, void	*data);
55 
56 
57 /*-------------------------------------------------------------*/
58 /* timer interrupts services */
59 
60 /* The BeOS qent structure is probably part of a general double linked list
61  * interface used all over the kernel; a struct is required to have a qent
62  * entry struct as first element, so it can be linked to other elements
63  * easily. The key field is probably just an id, eventually used to order
64  * the list.
65  * Since we don't use this kind of interface, but we have to provide it
66  * to keep compatibility, we can use the qent struct for other purposes...
67  *
68  * ToDo: don't do this! Drop source compatibility, but don't overdefine those values!
69  */
70 typedef struct qent {
71 	int64		key;			/* We use this as the sched time */
72 	struct qent	*next;			/* This is used as a pointer to next timer */
73 	struct qent	*prev;			/* This can be used for callback args */
74 } qent;
75 
76 typedef struct timer timer;
77 typedef	int32 (*timer_hook)(timer *);
78 
79 struct timer {
80 	qent		entry;
81 	uint16		flags;
82 	uint16		cpu;
83 	timer_hook	hook;
84 	bigtime_t	period;
85 };
86 
87 #define B_ONE_SHOT_ABSOLUTE_TIMER	1
88 #define	B_ONE_SHOT_RELATIVE_TIMER	2
89 #define	B_PERIODIC_TIMER			3
90 
91 extern status_t		add_timer(timer *t, timer_hook hook, bigtime_t period, int32 flags);
92 extern bool			cancel_timer(timer *t);
93 
94 
95 /*-------------------------------------------------------------*/
96 /* kernel threads */
97 
98 extern thread_id	spawn_kernel_thread(thread_func function, const char *threadName,
99 						int32 priority, void *arg);
100 
101 
102 /*-------------------------------------------------------------*/
103 /* signal functions */
104 
105 extern int			send_signal_etc(pid_t thread, uint sig, uint32 flags);
106 
107 
108 /*-------------------------------------------------------------*/
109 /* virtual memory buffer functions */
110 
111 #define B_DMA_IO		0x00000001
112 #define B_READ_DEVICE	0x00000002
113 
114 typedef struct {
115 	void	*address;	/* address in physical memory */
116 	ulong	size;		/* size of block */
117 } physical_entry;
118 
119 extern long			lock_memory(void *buffer, ulong numBytes, ulong flags);
120 extern long			unlock_memory(void *buffer, ulong numBytes, ulong flags);
121 extern long			get_memory_map(const void *buffer, ulong size,
122 						physical_entry *table, long numEntries);
123 
124 /* address specifications for mapping physical memory */
125 #define	B_ANY_KERNEL_BLOCK_ADDRESS	(B_ANY_KERNEL_ADDRESS + 1)
126 
127 /* area protection flags for the kernel */
128 #define B_KERNEL_READ_AREA			16
129 #define B_KERNEL_WRITE_AREA			32
130 #define B_USER_CLONEABLE_AREA		256
131 
132 /* call to map physical memory - typically used for memory-mapped i/o */
133 
134 extern area_id		map_physical_memory(const char *areaName, void *physicalAddress,
135 						size_t size, uint32 flags, uint32 protection, void **mappedAddress);
136 
137 
138 /* MTR attributes for mapping physical memory (Intel Architecture only) */
139 // ToDo: what have those to do here?
140 #define	B_MTR_UC	0x10000000
141 #define	B_MTR_WC	0x20000000
142 #define	B_MTR_WT	0x30000000
143 #define	B_MTR_WP	0x40000000
144 #define	B_MTR_WB	0x50000000
145 #define	B_MTR_MASK	0xf0000000
146 
147 
148 /*-------------------------------------------------------------*/
149 /* hardware inquiry */
150 
151 /* platform_type return value is defined in OS.h */
152 
153 extern platform_type platform();
154 
155 #if __POWERPC__
156 extern long			motherboard_version(void);
157 extern long			io_card_version(void);
158 #endif
159 
160 /*-------------------------------------------------------------*/
161 /* primitive kernel debugging facilities */
162 
163 /* Standard debug output is on...
164  *	mac: modem port
165  *	pc: com1
166  *	...at 19.2 kbaud, no parity, 8 bit, 1 stop bit.
167  *
168  * Note: the kernel settings file can override these defaults
169  */
170 
171 #if __GNUC__
172 extern void			dprintf(const char *format, ...)		/* just like printf */
173                                   __attribute__ ((format (__printf__, 1, 2)));
174 extern void			kprintf(const char *fmt, ...)			/* only for debugger cmds */
175                                   __attribute__ ((format (__printf__, 1, 2)));
176 #else
177 extern void			dprintf(const char *format, ...);		/* just like printf */
178 extern void			kprintf(const char *fmt, ...);			/* only for debugger cmds */
179 #endif
180 
181 extern void 		dump_block(const char *buffer, int size, const char *prefix);
182 															/* hexdumps given buffer */
183 
184 extern bool			set_dprintf_enabled(bool new_state);	/* returns old state */
185 
186 #if __GNUC__
187 extern void			panic(const char *format, ...) __attribute__ ((format (__printf__, 1, 2)));
188 #else
189 extern void			panic(const char *format, ...);
190 #endif
191 
192 extern void			kernel_debugger(const char *message);	/* enter kernel debugger */
193 extern uint64		parse_expression(const char *string);	/* utility for debugger cmds */
194 
195 /* special return codes for kernel debugger */
196 #define  B_KDEBUG_CONT   2
197 #define  B_KDEBUG_QUIT   3
198 
199 typedef int (*debugger_command_hook)(int argc, char **argv);
200 
201 extern int			add_debugger_command(char *name, debugger_command_hook hook, char *help);
202 extern int			remove_debugger_command(char *name, debugger_command_hook hook);
203 
204 extern status_t 	load_driver_symbols(const char *driverName);
205 
206 
207 /*-------------------------------------------------------------*/
208 /* misc */
209 
210 extern void			spin(bigtime_t microseconds);
211 	/* does a busy delay loop for at least "microseconds" */
212 
213 typedef void (*daemon_hook)(void *arg, int iteration);
214 
215 extern status_t		register_kernel_daemon(daemon_hook hook, void *arg, int frequency);
216 extern status_t		unregister_kernel_daemon(daemon_hook hook, void *arg);
217 
218 extern void			call_all_cpus(void (*f)(void *, int), void *cookie);
219 extern void			call_all_cpus_sync(void (*f)(void *, int), void *cookie);
220 
221 /* safe methods to access user memory without having to lock it */
222 extern status_t		user_memcpy(void *to, const void *from, size_t size);
223 extern ssize_t		user_strlcpy(char *to, const char *from, size_t size);
224 extern status_t		user_memset(void *start, char c, size_t count);
225 
226 #ifdef __cplusplus
227 }
228 #endif
229 
230 #endif	/* _KERNEL_EXPORT_H */
231