xref: /haiku/headers/os/drivers/KernelExport.h (revision fc1ca2da5cfcb00ffdf791606d5ae97fdd58a638)
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 typedef vint32 spinlock;
17 
18 /* interrupt handling support for device drivers */
19 
20 typedef int32 (*interrupt_handler)(void *data);
21 
22 /* Values returned by interrupt handlers */
23 #define B_UNHANDLED_INTERRUPT	0	/* pass to next handler */
24 #define B_HANDLED_INTERRUPT		1	/* don't pass on */
25 #define B_INVOKE_SCHEDULER		2	/* don't pass on; invoke the scheduler */
26 
27 /* Flags that can be passed to install_io_interrupt_handler() */
28 #define B_NO_ENABLE_COUNTER		1
29 #define B_NO_LOCK_VECTOR		2
30 
31 
32 /* timer interrupts services */
33 
34 typedef struct timer timer;
35 typedef	int32 (*timer_hook)(timer *);
36 
37 struct timer {
38 	struct timer *next;
39 	int64		schedule_time;
40 	void		*user_data;
41 	uint16		flags;
42 	uint16		cpu;
43 	timer_hook	hook;
44 	bigtime_t	period;
45 };
46 
47 #define B_ONE_SHOT_ABSOLUTE_TIMER	1
48 #define	B_ONE_SHOT_RELATIVE_TIMER	2
49 #define	B_PERIODIC_TIMER			3
50 
51 
52 /* virtual memory buffer functions */
53 
54 #define B_DMA_IO		0x00000001
55 #define B_READ_DEVICE	0x00000002
56 
57 typedef struct {
58 	void	*address;	/* address in physical memory */
59 	ulong	size;		/* size of block */
60 } physical_entry;
61 
62 /* address specifications for mapping physical memory */
63 #define	B_ANY_KERNEL_BLOCK_ADDRESS	(B_ANY_KERNEL_ADDRESS + 1)
64 
65 /* area protection flags for the kernel */
66 #define B_KERNEL_READ_AREA			16
67 #define B_KERNEL_WRITE_AREA			32
68 #define B_USER_CLONEABLE_AREA		256
69 
70 /* MTR attributes for mapping physical memory (Intel Architecture only) */
71 // TODO: rename those to something more meaningful
72 #define	B_MTR_UC	0x10000000
73 #define	B_MTR_WC	0x20000000
74 #define	B_MTR_WT	0x30000000
75 #define	B_MTR_WP	0x40000000
76 #define	B_MTR_WB	0x50000000
77 #define	B_MTR_MASK	0xf0000000
78 
79 
80 /* kernel daemon service */
81 
82 typedef void (*daemon_hook)(void *arg, int iteration);
83 
84 
85 /* kernel debugging facilities */
86 
87 /* special return codes for kernel debugger */
88 #define  B_KDEBUG_CONT   2
89 #define  B_KDEBUG_QUIT   3
90 
91 typedef int (*debugger_command_hook)(int argc, char **argv);
92 
93 
94 #ifdef __cplusplus
95 extern "C" {
96 #endif
97 
98 /* interrupts, spinlock, and timers */
99 extern cpu_status	disable_interrupts(void);
100 extern void			restore_interrupts(cpu_status status);
101 
102 extern void			acquire_spinlock(spinlock *lock);
103 extern void			release_spinlock(spinlock *lock);
104 
105 extern status_t		install_io_interrupt_handler(long interrupt_number,
106 						interrupt_handler handler, void *data, ulong flags);
107 extern status_t		remove_io_interrupt_handler(long interrupt_number,
108 						interrupt_handler handler, void	*data);
109 
110 extern status_t		add_timer(timer *t, timer_hook hook, bigtime_t period,
111 						int32 flags);
112 extern bool			cancel_timer(timer *t);
113 
114 /* kernel threads */
115 extern thread_id	spawn_kernel_thread(thread_func function,
116 						const char *name, int32 priority, void *arg);
117 
118 /* signal functions */
119 extern int			send_signal_etc(pid_t thread, uint signal, uint32 flags);
120 
121 /* virtual memory */
122 extern long			lock_memory(void *buffer, ulong numBytes, ulong flags);
123 extern long			unlock_memory(void *buffer, ulong numBytes, ulong flags);
124 extern long			get_memory_map(const void *buffer, ulong size,
125 						physical_entry *table, long numEntries);
126 extern area_id		map_physical_memory(const char *areaName,
127 						void *physicalAddress, size_t size, uint32 flags,
128 						uint32 protection, void **_mappedAddress);
129 
130 /* kernel debugging facilities */
131 extern void			dprintf(const char *format, ...) _PRINTFLIKE(1, 2);
132 extern void			kprintf(const char *fmt, ...) _PRINTFLIKE(1, 2);
133 
134 extern void 		dump_block(const char *buffer, int size, const char *prefix);
135 						/* TODO: temporary API: hexdumps given buffer */
136 
137 extern bool			set_dprintf_enabled(bool new_state);
138 
139 extern void			panic(const char *format, ...) _PRINTFLIKE(1, 2);
140 
141 extern void			kernel_debugger(const char *message);
142 extern uint64		parse_expression(const char *string);
143 
144 extern int			add_debugger_command(char *name, debugger_command_hook hook, char *help);
145 extern int			remove_debugger_command(char *name,
146 						debugger_command_hook hook);
147 
148 /* Miscellaneous */
149 extern void			spin(bigtime_t microseconds);
150 
151 extern status_t		register_kernel_daemon(daemon_hook hook, void *arg,
152 						int frequency);
153 extern status_t		unregister_kernel_daemon(daemon_hook hook, void *arg);
154 
155 extern void			call_all_cpus(void (*func)(void *, int), void *cookie);
156 extern void			call_all_cpus_sync(void (*func)(void *, int), void *cookie);
157 
158 /* safe methods to access user memory without having to lock it */
159 extern status_t		user_memcpy(void *to, const void *from, size_t size);
160 extern ssize_t		user_strlcpy(char *to, const char *from, size_t size);
161 extern status_t		user_memset(void *start, char c, size_t count);
162 
163 #ifdef __cplusplus
164 }
165 #endif
166 
167 #endif	/* _KERNEL_EXPORT_H */
168