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.
47static inline bool
48is_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
89
90#define CHECK_BIT(a, b) ((a) & (1 << (b)))
91#define SET_BIT(a, b) ((a) | (1 << (b)))
92#define CLEAR_BIT(a, b) ((a) & (~(1 << (b))))
93#define GET_BIT(a, b) ((a & b) != 0)
94#define TOGGLE_BIT(a, b) (a ^= b)
95
96
97/* during kernel startup, interrupts are disabled (among other things) */
98extern bool gKernelStartup;
99extern bool gKernelShutdown;
100
101
102#ifdef __cplusplus
103extern "C" {
104#endif
105
106status_t system_shutdown(bool reboot);
107status_t _user_shutdown(bool reboot);
108
109#ifdef __cplusplus
110}
111#endif
112
113#endif	/* _KERNEL_KERNEL_H */
114