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