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	use crate::EXTERN_SYM_PTR;
59	// symbols from the assembly
60	EXTERN_SYM_PTR!(pub GDT ,            gdt);
61	EXTERN_SYM_PTR!(pub GDT_80 ,         gdt_80);
62	EXTERN_SYM_PTR!(pub TSS_DESC ,       tss_desc);
63	EXTERN_SYM_PTR!(pub TSS0,            tss0);
64
65	EXTERN_SYM_PTR!(pub VECTORS_START,  vectors_start);
66	EXTERN_SYM_PTR!(pub IDT,            idt);
67	EXTERN_SYM_PTR!(pub IDT_DESCR,      idt_descr);
68	// symbols produced by linker script
69	EXTERN_SYM_PTR!(pub KERNEL_PM_START, ___KERNEL_PM_START__);
70	EXTERN_SYM_PTR!(pub KERNEL_PM_END,   ___KERNEL_PM_END__);
71	EXTERN_SYM_PTR!(pub BSS_START,       ___BSS_START__);
72	EXTERN_SYM_PTR!(pub BSS_END,         ___BSS_END__);
73	EXTERN_SYM_PTR!(pub RAMFS_START,     ___RAMFS_START__);
74	EXTERN_SYM_PTR!(pub RAMFS_END,       ___RAMFS_END__);
75	// a chunk (8M) of reserved memory, optionally used by the stack based
76	// physical frame allocator. This naive pma is deprecated, and you must not
77	// use this symbol unless you adjust the startup code to reserve memory of
78	// cooresponding size and alignment. This is deprecated
79	EXTERN_SYM_PTR!(pub FREE_PAGE_STACK, ___FREE_PAGE_STACK__);
80
81	// TODO grouping symbols into archs
82	// #[cfg(target_arch = "x86_64")]
83	// pub use crate::arch::x86_64::ExternSyms::*;
84}
85
86#[cfg(target_arch = "x86_64")]
87pub mod HWDefs {
88	/// number of entries in IDT
89	pub const IDT_CAPACITY: usize = 256;
90	/// 32 exceptions + 16 irqs from PIC = 48 valid interrupts
91	pub const IDT_VALID: usize = 48;
92	/// size of interrupt handler wrapper routine (vector)
93	pub const VECTOR_SIZE: usize = 16;
94}
95
96pub mod Limits {
97	// initialize some queue structs with a reserved capacity to avoid runtime
98	// allocation
99	pub const SEM_WAIT_QUEUE_MIN_CAP: usize = 16;
100	pub const SCHED_RUN_QUEUE_MIN_CAP: usize = 24;
101}
102
103/// convert VA <-> PA wrt. the kernel id mapping
104/// from 0xffff_8000_0000_0000 ~ 0xffff_800f_ffff_ffff virtual
105/// to 0x0 ~ 0xf_ffff_ffff physical (64G)
106#[allow(non_snake_case)]
107#[inline]
108pub const fn V2P(va: u64) -> Option<u64> {
109	if va >= Mem::ID_MAP_END || va < Mem::ID_MAP_START {
110		return None;
111	}
112	return Some(va - Mem::ID_MAP_START);
113}
114
115/// physical address to virtual. reverse of [V2P]
116#[allow(non_snake_case)]
117#[inline]
118pub const fn P2V(pa: u64) -> Option<u64> {
119	if pa >= Mem::MAX_PHY_MEM {
120		return None;
121	}
122	return Some(pa + Mem::ID_MAP_START);
123}
124
125/// interrut numbers. Not complete, add more when needed
126/// (see docs/interrupt.txt)
127pub struct IntNumber {}
128impl IntNumber {
129	pub const PAGEFAULT: u16 = 0xe;
130	pub const TIMER: u16 = 0x20;
131	pub const KEYBOARD: u16 = 0x21;
132	pub const SYSCALL: u16 = 0x80;
133}