xref: /haiku/headers/private/kernel/kernel.h (revision fc7456e9b1ec38c941134ed6d01c438cf289381e)
1 /*
2  * Copyright 2002-2005, Axel Dörfler, axeld@pinc-software.de.
3  * Distributed under the terms of the MIT License.
4  *
5  * Copyright 2001-2002, Travis Geiselbrecht. All rights reserved.
6  * Distributed under the terms of the NewOS License.
7  */
8 #ifndef _KERNEL_KERNEL_H
9 #define _KERNEL_KERNEL_H
10 
11 
12 #include <config/types.h>
13 
14 #include <arch_kernel.h>
15 #include <arch_config.h>
16 
17 
18 #ifndef KERNEL_LOAD_BASE
19 #	define KERNEL_LOAD_BASE		KERNEL_BASE
20 #endif
21 
22 // macro to check whether an address is in the kernel address space (avoid
23 // always-true checks)
24 #if KERNEL_BASE == 0
25 #	define IS_KERNEL_ADDRESS(x)		((addr_t)(x) <= KERNEL_TOP)
26 #elif KERNEL_TOP == __HAIKU_ADDR_MAX
27 #	define IS_KERNEL_ADDRESS(x)		((addr_t)(x) >= KERNEL_BASE)
28 #else
29 #	define IS_KERNEL_ADDRESS(x) \
30 		((addr_t)(x) >= KERNEL_BASE && (addr_t)(x) <= KERNEL_TOP)
31 #endif
32 
33 #ifndef _BOOT_MODE
34 
35 // Buffers passed in from user-space shouldn't point into the kernel.
36 #if USER_BASE == 0
37 #	define IS_USER_ADDRESS(x)		((addr_t)(x) <= USER_TOP)
38 #elif USER_TOP == __HAIKU_ADDR_MAX
39 #	define IS_USER_ADDRESS(x)		((addr_t)(x) >= USER_BASE)
40 #else
41 #	define IS_USER_ADDRESS(x) \
42 		((addr_t)(x) >= USER_BASE && (addr_t)(x) <= USER_TOP)
43 #endif
44 
45 #ifdef __cplusplus
46 // Validate that an address range is fully in userspace.
47 static inline bool
48 is_user_address_range(const void* addr, size_t size)
49 {
50 	addr_t address = (addr_t)addr;
51 
52 	// Check for overflows on all addresses.
53 	if ((address + size) < address)
54 		return false;
55 
56 	// Validate that both the start and end address are in userspace
57 	return IS_USER_ADDRESS(address) && IS_USER_ADDRESS(address + size - 1);
58 }
59 #endif
60 
61 #endif // !_BOOT_MODE
62 
63 #define DEBUG_KERNEL_STACKS
64 	// Note, debugging kernel stacks doesn't really work yet. Since the
65 	// interrupt will also try to use the stack on a page fault, all
66 	// you get is a double fault.
67 	// At least, you then know that the stack overflows in this case :)
68 
69 /** Size of the kernel stack */
70 #ifdef B_HAIKU_64_BIT
71 	#define KERNEL_STACK_SIZE		(B_PAGE_SIZE * 4)	// 16 kB
72 #else
73 	#define KERNEL_STACK_SIZE		(B_PAGE_SIZE * 3)	// 12 kB
74 #endif
75 
76 #ifdef DEBUG_KERNEL_STACKS
77 #	define KERNEL_STACK_GUARD_PAGES	1
78 #else
79 #	define KERNEL_STACK_GUARD_PAGES	0
80 #endif
81 
82 /** Size of the environmental variables space for a process */
83 #define ENV_SIZE	(B_PAGE_SIZE * 8)
84 
85 
86 #define ROUNDDOWN(a, b)	(((a) / (b)) * (b))
87 #define ROUNDUP(a, b)	ROUNDDOWN((a) + (b) - 1, b)
88 #define HOWMANY(a, b)	(((a) + ((b) - 1)) / (b))
89 
90 
91 #define CHECK_BIT(a, b) ((a) & (1 << (b)))
92 #define SET_BIT(a, b) ((a) | (1 << (b)))
93 #define CLEAR_BIT(a, b) ((a) & (~(1 << (b))))
94 #define GET_BIT(a, b) ((a & b) != 0)
95 #define TOGGLE_BIT(a, b) (a ^= b)
96 
97 
98 /* during kernel startup, interrupts are disabled (among other things) */
99 extern bool gKernelStartup;
100 extern bool gKernelShutdown;
101 
102 
103 #ifdef __cplusplus
104 extern "C" {
105 #endif
106 
107 status_t system_shutdown(bool reboot);
108 status_t _user_shutdown(bool reboot);
109 
110 #ifdef __cplusplus
111 }
112 #endif
113 
114 #endif	/* _KERNEL_KERNEL_H */
115