strat9_kernel/arch/x86_64/
tss.rs1use core::{
30 mem::MaybeUninit,
31 panic::Location,
32 sync::atomic::{AtomicBool, Ordering},
33};
34use x86_64::{structures::tss::TaskStateSegment, VirtAddr};
35
36#[repr(C, packed)]
37struct DescriptorTableRegister {
38 limit: u16,
39 base: u64,
40}
41
42#[derive(Clone, Copy, Debug)]
43pub struct LoadedTssInfo {
44 pub tr_selector: u16,
45 pub tss_base: u64,
46 pub rsp0: u64,
47}
48
49pub const DOUBLE_FAULT_IST_INDEX: u16 = 0;
51
52const IST_STACK_SIZE: usize = 4096 * 5;
54
55static mut IST_STACKS: [[u8; IST_STACK_SIZE]; crate::arch::x86_64::percpu::MAX_CPUS] =
57 [[0; IST_STACK_SIZE]; crate::arch::x86_64::percpu::MAX_CPUS];
58
59static mut TSS: [MaybeUninit<TaskStateSegment>; crate::arch::x86_64::percpu::MAX_CPUS] =
61 [const { MaybeUninit::uninit() }; crate::arch::x86_64::percpu::MAX_CPUS];
62
63static TSS_INIT: [AtomicBool; crate::arch::x86_64::percpu::MAX_CPUS] =
64 [const { AtomicBool::new(false) }; crate::arch::x86_64::percpu::MAX_CPUS];
65
66pub fn init() {
70 init_cpu(0);
71}
72
73pub fn init_cpu(cpu_index: usize) {
75 assert!(
77 cpu_index < crate::arch::x86_64::percpu::MAX_CPUS,
78 "TSS init_cpu: cpu_index {} >= MAX_CPUS {}",
79 cpu_index,
80 crate::arch::x86_64::percpu::MAX_CPUS,
81 );
82 unsafe {
84 let stack_ptr = &raw const IST_STACKS[cpu_index] as *const u8;
85 let stack_end = VirtAddr::from_ptr(stack_ptr) + IST_STACK_SIZE as u64;
86 let mut tss = TaskStateSegment::new();
87 tss.interrupt_stack_table[DOUBLE_FAULT_IST_INDEX as usize] = stack_end;
88
89 TSS[cpu_index].write(tss);
90 TSS_INIT[cpu_index].store(true, Ordering::Release);
91
92 let _ist_addr = VirtAddr::from_ptr(stack_ptr);
93 for &b in b"TSS init OK\n" {
95 core::arch::asm!("out 0xe9, al", in("al") b, options(nomem, nostack));
96 }
97 }
98}
99
100pub fn tss_for(cpu_index: usize) -> &'static TaskStateSegment {
102 assert!(
103 cpu_index < crate::arch::x86_64::percpu::MAX_CPUS,
104 "tss_for: cpu_index {} >= MAX_CPUS",
105 cpu_index,
106 );
107 if !TSS_INIT[cpu_index].load(Ordering::Acquire) {
108 panic!("TSS for CPU{} not initialized", cpu_index);
109 }
110 unsafe { &*TSS[cpu_index].as_ptr() }
112}
113
114pub fn kernel_stack_for(cpu_index: usize) -> Option<VirtAddr> {
116 if cpu_index >= crate::arch::x86_64::percpu::MAX_CPUS {
117 return None;
118 }
119 if !TSS_INIT[cpu_index].load(Ordering::Acquire) {
120 return None;
121 }
122 unsafe {
124 let tss = &*TSS[cpu_index].as_ptr();
125 Some(tss.privilege_stack_table[0])
126 }
127}
128
129pub fn loaded_tss_info() -> Option<LoadedTssInfo> {
131 let mut gdtr = DescriptorTableRegister { limit: 0, base: 0 };
132 let tr_selector: u16;
133 unsafe {
135 core::arch::asm!(
136 "sgdt [{}]",
137 in(reg) &mut gdtr,
138 options(nostack, preserves_flags),
139 );
140 core::arch::asm!(
141 "str {0:x}",
142 out(reg) tr_selector,
143 options(nostack, nomem, preserves_flags),
144 );
145 }
146
147 if tr_selector == 0 {
148 return None;
149 }
150
151 let entry_offset = (tr_selector & !0x7) as usize;
152 if entry_offset + 16 > gdtr.limit as usize + 1 {
153 return None;
154 }
155
156 let (low, high) = unsafe {
158 let entry_ptr = (gdtr.base + entry_offset as u64) as *const u64;
159 (
160 core::ptr::read_unaligned(entry_ptr),
161 core::ptr::read_unaligned(entry_ptr.add(1)),
162 )
163 };
164
165 let base_low =
166 ((low >> 16) & 0xFFFF) | (((low >> 32) & 0xFF) << 16) | (((low >> 56) & 0xFF) << 24);
167 let tss_base = base_low | (high << 32);
168 if tss_base == 0 {
169 return None;
170 }
171
172 let rsp0 = unsafe {
174 let tss = &*(tss_base as *const TaskStateSegment);
175 tss.privilege_stack_table[0].as_u64()
176 };
177
178 Some(LoadedTssInfo {
179 tr_selector,
180 tss_base,
181 rsp0,
182 })
183}
184
185#[track_caller]
190pub fn set_kernel_stack(stack_top: VirtAddr) {
191 let cpu_index = crate::arch::x86_64::percpu::current_cpu_index();
192 set_kernel_stack_for(cpu_index, stack_top);
193}
194
195#[track_caller]
197pub fn set_kernel_stack_for(cpu_index: usize, stack_top: VirtAddr) {
198 if cpu_index >= crate::arch::x86_64::percpu::MAX_CPUS {
199 log::warn!("set_kernel_stack_for: cpu_index {} out of range", cpu_index);
200 return;
201 }
202 if !TSS_INIT[cpu_index].load(Ordering::Acquire) {
205 return;
206 }
207 let caller = Location::caller();
208 unsafe {
209 let tss = &raw mut *TSS[cpu_index].as_mut_ptr();
210 let old_stack_top = (*tss).privilege_stack_table[0];
211 (*tss).privilege_stack_table[0] = stack_top;
212 if crate::debug_cfg::E9_TRACE_TSS {
213 crate::e9_println!(
214 "[tss-set] cpu={} old={:#x} new={:#x} caller={}:{}",
215 cpu_index,
216 old_stack_top.as_u64(),
217 stack_top.as_u64(),
218 caller.file(),
219 caller.line()
220 );
221 }
222 }
223}