rustubs/
defs.rs

1//! system level definitions
2
3/// multiboot magic value, it must be 0x2BAD8002. This value is set at runtime
4/// by init asm code. CARE: the release build will not treat these as volatile
5/// and they may hardcode the initial value (0) because they sees "static const
6/// zero". Therefore when reading them must do a volatile read.
7#[no_mangle]
8pub static mb_magic: u64 = 0;
9/// _physical_ address of multiboot info block. This value is set at run time by
10/// init asm code. Both values are safe to read, but [mb_info_pm_addr] must be
11/// converted to the virtual mapping via P2V before dereferencing.
12#[no_mangle]
13pub static mb_info_pm_addr: u64 = 0;
14
15#[inline]
16pub fn roundup_4k(addr: u64) -> u64 { (addr + 0xfff) & !0xfff }
17
18#[inline]
19pub fn rounddown_4k(addr: u64) -> u64 { addr & !0xfff }
20
21#[inline]
22pub fn is_aligned_4k(addr: u64) -> bool { (addr & 0xfff) == 0 }
23
24/// memory definitions
25pub mod Mem {
26	// units
27	pub const K: u64 = 1024;
28	pub const M: u64 = 1024 * K;
29	pub const G: u64 = 1024 * M;
30	// 4 lv 4K paging
31	pub const PAGE_SIZE: u64 = 0x1000;
32	pub const PAGE_SHIFT: u64 = 12;
33	pub const PAGE_MASK: u64 = 0xfff;
34	// 64 GiB available memory
35	pub const MAX_PHY_MEM: u64 = 0x1000000000;
36	// we should have at least 64 MiB free physical memory (excluding the kernel it self)
37	pub const MIN_PHY_MEM: u64 = 64 * M;
38	pub const ID_MAP_START: u64 = 0xffff_8000_0000_0000;
39	pub const ID_MAP_END: u64 = 0xffff_8010_0000_0000;
40	// kernel image:0xffff_8020_0000_0000 ~ 0xffff_802f_0000_0000;
41	pub const KERNEL_OFFSET: u64 = 0xffff_8020_0000_0000;
42	// kernel heap: 0xffff_8030_0000_0000 ~ 0xffff_803f_0000_0000;
43	// (64 GiB)
44	pub const KERNEL_HEAP_START: u64 = 0xffff_8030_0000_0000;
45	pub const KERNEL_HEAP_END: u64 = 0xffff_8040_0000_0000;
46	// unlike the initial "thread" that has 64K stack, new tasks have 4 pages of
47	// kernel stack.
48	pub const KERNEL_STACK_SIZE: u64 = 0x4000;
49	pub const KERNEL_STACK_MASK: u64 = KERNEL_STACK_SIZE - 1;
50	pub const KERNEL_STACK_TASK_MAGIC: u64 = 0x1A2B3C4D5E6F6969;
51	// user (psuedo)
52	pub const USER_STACK_START: u64 = 0x0000_7000_0000_0000;
53	pub const USER_STACK_SIZE: u64 = 8 * M;
54}
55
56// TODO use a consistent naming convention for extern symbols
57pub mod ExternSyms {
58	extern "C" {
59		pub fn ___KERNEL_PM_START__();
60		pub fn ___KERNEL_PM_END__();
61		pub fn ___BSS_START__();
62		pub fn ___BSS_END__();
63		pub fn ___RAMFS_START__();
64		pub fn ___RAMFS_END__();
65		/// a chunk (8M) of reserved memory, optionally used by the stack based
66		/// physical frame allocator. This naive pma is deprecated, and you must not
67		/// use this symbol unless you adjust the startup code to reserve
68		/// memory of cooresponding size and alignment. This is deprecated
69		pub fn ___FREE_PAGE_STACK__();
70	}
71	#[cfg(target_arch = "x86_64")]
72	pub use crate::arch::x86_64::ExternSyms::*;
73}
74
75#[cfg(target_arch = "x86_64")]
76pub mod HWDefs {
77	/// number of entries in IDT
78	pub const IDT_CAPACITY: usize = 256;
79	/// 32 exceptions + 16 irqs from PIC = 48 valid interrupts
80	pub const IDT_VALID: usize = 48;
81	/// size of interrupt handler wrapper routine (vector)
82	pub const VECTOR_SIZE: usize = 16;
83}
84
85pub mod Limits {
86	// initialize some queue structs with a reserved capacity to avoid runtime
87	// allocation
88	pub const SEM_WAIT_QUEUE_MIN_CAP: usize = 16;
89	pub const SCHED_RUN_QUEUE_MIN_CAP: usize = 24;
90}
91
92/// convert VA <-> PA wrt. the kernel id mapping
93/// from 0xffff_8000_0000_0000 ~ 0xffff_800f_ffff_ffff virtual
94/// to 0x0 ~ 0xf_ffff_ffff physical (64G)
95#[allow(non_snake_case)]
96#[inline]
97pub const fn V2P(va: u64) -> Option<u64> {
98	if va >= Mem::ID_MAP_END || va < Mem::ID_MAP_START {
99		return None;
100	}
101	return Some(va - Mem::ID_MAP_START);
102}
103
104/// physical address to virtual. reverse of [V2P]
105#[allow(non_snake_case)]
106#[inline]
107pub const fn P2V(pa: u64) -> Option<u64> {
108	if pa >= Mem::MAX_PHY_MEM {
109		return None;
110	}
111	return Some(pa + Mem::ID_MAP_START);
112}
113
114/// interrut numbers. Not complete, add more when needed
115/// (see docs/interrupt.txt)
116pub struct IntNumber {}
117impl IntNumber {
118	pub const PAGEFAULT: u16 = 0xe;
119	pub const TIMER: u16 = 0x20;
120	pub const KEYBOARD: u16 = 0x21;
121	pub const SYSCALL: u16 = 0x80;
122}