Skip to content

Commit 5358da9

Browse files
Merge branch 'scheduler_refactor'
2 parents d7d01e7 + 467ceb9 commit 5358da9

File tree

14 files changed

+306
-373
lines changed

14 files changed

+306
-373
lines changed

src/aero_kernel/src/apic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ impl From<FeatureInfo> for ApicType {
108108
}
109109
}
110110

111-
fn lapic_error_handler(stack: &mut InterruptStack) {
111+
fn lapic_error_handler(_stack: &mut InterruptStack) {
112112
log::error!("Local apic error");
113113
log::error!("ESR={:#0x}", self::get_local_apic().get_esr());
114114
}

src/aero_kernel/src/arch/x86_64/gdt.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ bitflags::bitflags! {
6363
#[derive(Debug, Clone, Copy, PartialEq)]
6464
pub enum Ring {
6565
Ring0 = 0b00,
66-
Ring3 = 0b11,
6766
}
6867

6968
const BOOT_GDT_ENTRY_COUNT: usize = 4;
@@ -273,22 +272,22 @@ impl GdtEntry {
273272
/// mode and its not directly related to task switching mechanism.
274273
#[repr(C, packed)]
275274
pub struct Tss {
276-
reserved: u32,
275+
reserved: u32, // offset 0x00
277276

278277
/// The full 64-bit canonical forms of the stack pointers (RSP) for
279278
/// privilege levels 0-2.
280-
pub rsp: [u64; 3],
281-
reserved2: u64,
279+
pub rsp: [u64; 3], // offset 0x04
280+
reserved2: u64, // offset 0x1C
282281

283282
/// The full 64-bit canonical forms of the interrupt stack table
284283
/// (IST) pointers.
285-
pub ist: [u64; 7],
286-
reserved3: u64,
287-
reserved4: u16,
284+
pub ist: [u64; 7], // offset 0x24
285+
reserved3: u64, // offset 0x5c
286+
reserved4: u16, // offset 0x64
288287

289288
/// The 16-bit offset to the I/O permission bit map from the 64-bit
290289
/// TSS base.
291-
pub iomap_base: u16,
290+
pub iomap_base: u16, // offset 0x66
292291
}
293292

294293
// Processor Control Region

src/aero_kernel/src/arch/x86_64/interrupts/exceptions.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use crate::mem::paging::PageFaultErrorCode;
2424

2525
use crate::unwind;
2626
use crate::userland::scheduler;
27+
use crate::utils::io;
2728

2829
macro interrupt_exception(fn $name:ident() => $message:expr) {
2930
pub fn $name(stack: &mut InterruptErrorStack) {
@@ -82,6 +83,18 @@ pub(super) fn page_fault(stack: &mut InterruptErrorStack) {
8283
// a non-mapped memory region while in RPL_3.
8384
let userland_last_address = super::super::task::userland_last_address();
8485

86+
// prints out the error information for this page fault.
87+
let print_info = || {
88+
log::error!("");
89+
log::error!("FS={:#x}", unsafe { io::rdmsr(io::IA32_FS_BASE) },);
90+
log::error!("GS={:#x}", unsafe { io::rdmsr(io::IA32_GS_BASE) });
91+
log::error!("");
92+
log::error!("accessed address: {:#x}", accessed_address);
93+
log::error!("reason: {:?}", reason);
94+
log::error!("");
95+
log::error!("stack: {:#x?}", stack);
96+
};
97+
8598
if accessed_address < userland_last_address && scheduler::is_initialized() {
8699
let signal = scheduler::get_scheduler()
87100
.current_task()
@@ -90,10 +103,7 @@ pub(super) fn page_fault(stack: &mut InterruptErrorStack) {
90103

91104
if !signal && stack.stack.iret.is_user() {
92105
log::error!("Segmentation fault");
93-
log::error!("");
94-
log::error!("accessed address: {:#x}", accessed_address);
95-
log::error!("reason: {:?}", reason);
96-
log::error!("");
106+
print_info();
97107

98108
if stack.stack.iret.is_user() {
99109
let task = scheduler::get_scheduler().current_task();
@@ -105,8 +115,6 @@ pub(super) fn page_fault(stack: &mut InterruptErrorStack) {
105115
);
106116
}
107117

108-
log::error!("stack: {:#x?}", stack);
109-
110118
scheduler::get_scheduler().current_task().vm.log();
111119
scheduler::get_scheduler().current_task().file_table.log();
112120

@@ -124,13 +132,8 @@ pub(super) fn page_fault(stack: &mut InterruptErrorStack) {
124132

125133
unwind::prepare_panic();
126134

127-
log::error!("EXCEPTION: Page Fault");
128-
log::error!("");
129-
log::error!("Accessed Address: {:#x}", accessed_address);
130-
log::error!("Error: {:?}", reason);
131-
log::error!("");
132-
133-
log::error!("Stack: {:#x?}", stack);
135+
log::error!("Page fault");
136+
print_info();
134137

135138
unwind::unwind_stack_trace();
136139

src/aero_kernel/src/arch/x86_64/interrupts/idt.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ impl IdtEntry {
100100
}
101101
}
102102

103-
#[derive(Debug, Clone, Copy)]
103+
#[derive(Debug, Clone, Copy, Default)]
104104
#[repr(C)]
105105
pub struct ScratchRegisters {
106106
pub r11: u64,
@@ -114,7 +114,7 @@ pub struct ScratchRegisters {
114114
pub rax: u64,
115115
}
116116

117-
#[derive(Debug, Clone, Copy)]
117+
#[derive(Debug, Clone, Copy, Default)]
118118
#[repr(C)]
119119
pub struct PreservedRegisters {
120120
pub r15: u64,
@@ -125,7 +125,7 @@ pub struct PreservedRegisters {
125125
pub rbx: u64,
126126
}
127127

128-
#[derive(Debug, Clone, Copy)]
128+
#[derive(Debug, Clone, Copy, Default)]
129129
#[repr(C)]
130130
pub struct IretRegisters {
131131
pub rip: u64,
@@ -141,7 +141,7 @@ impl IretRegisters {
141141
}
142142
}
143143

144-
#[derive(Debug, Clone, Copy)]
144+
#[derive(Debug, Clone, Copy, Default)]
145145
#[repr(C)]
146146
pub struct InterruptStack {
147147
pub preserved: PreservedRegisters,

src/aero_kernel/src/arch/x86_64/signals.rs

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
* along with Aero. If not, see <https://www.gnu.org/licenses/>.
1818
*/
1919

20-
use crate::syscall::{RegistersFrame, SyscallFrame};
2120
use crate::userland;
2221
use crate::userland::scheduler;
2322
use crate::utils::StackHelper;
@@ -94,20 +93,16 @@ pub fn interrupt_check_signals(stack: &mut InterruptStack) {
9493
}
9594

9695
/// Helper function to check for any pending signals from a sycall.
97-
pub fn syscall_check_signals(
98-
_syscall_result: isize,
99-
_syscall: &mut SyscallFrame,
100-
_registers: &mut RegistersFrame,
101-
) {
96+
pub fn syscall_check_signals(_syscall_result: isize, _stack: &mut InterruptStack) {
10297
if let Some((_signal, entry)) = userland::signals::check_for_signals() {
10398
if let aero_syscall::signal::SignalHandler::Handle(_) = entry.handler() {
10499
todo!()
105100
}
106101
}
107102
}
108103

109-
pub fn sigreturn(sys: &mut SyscallFrame, regs: &mut RegistersFrame) -> usize {
110-
let mut writer = StackHelper::new(&mut sys.rsp);
104+
pub fn sigreturn(stack: &mut InterruptStack) -> usize {
105+
let mut writer = StackHelper::new(&mut stack.iret.rsp);
111106
let signal_frame = unsafe { writer.get::<SignalFrame>() };
112107

113108
let current_task = scheduler::get_scheduler().current_task();
@@ -121,33 +116,11 @@ pub fn sigreturn(sys: &mut SyscallFrame, regs: &mut RegistersFrame) -> usize {
121116
writer.get_by(REDZONE_SIZE);
122117

123118
let result = signal_frame.frame.scratch.rax;
124-
125-
let ret_regs = RegistersFrame {
126-
cr2: 0, // TODO: we have to fill up the cr2 as well
127-
rax: signal_frame.frame.scratch.rax,
128-
rbx: signal_frame.frame.preserved.rbx,
129-
rcx: signal_frame.frame.scratch.rcx,
130-
rdx: signal_frame.frame.scratch.rdx,
131-
rsi: signal_frame.frame.scratch.rsi,
132-
rdi: signal_frame.frame.scratch.rdi,
133-
rbp: signal_frame.frame.preserved.rbp,
134-
r8: signal_frame.frame.scratch.r8,
135-
r9: signal_frame.frame.scratch.r9,
136-
r10: signal_frame.frame.scratch.r10,
137-
r11: signal_frame.frame.scratch.r11,
138-
r12: signal_frame.frame.preserved.r12,
139-
r13: signal_frame.frame.preserved.r13,
140-
r14: signal_frame.frame.preserved.r14,
141-
r15: signal_frame.frame.preserved.r15,
142-
};
143-
144-
sys.rflags = signal_frame.frame.iret.rflags;
145-
sys.rip = signal_frame.frame.iret.rip;
119+
*stack = signal_frame.frame;
146120

147121
if signal_frame.restart_syscall != u64::MAX {
148-
sys.rip -= SYSCALL_INSTRUCTION_SIZE;
122+
stack.iret.rip -= SYSCALL_INSTRUCTION_SIZE;
149123
}
150124

151-
*regs = ret_regs;
152125
result as usize
153126
}

src/aero_kernel/src/arch/x86_64/task.asm

Lines changed: 36 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -18,100 +18,77 @@
1818
global jump_userland_exec
1919
global task_spinup
2020
global iretq_init
21-
global sysret_fork_init
22-
23-
extern restore_user_tls
21+
global fork_init
2422

2523
jump_userland_exec:
2624
push rdi ; Param: stack
2725
push rsi ; Param: RIP
2826
push rdx ; Param: RFLAGS
2927

3028
cli
31-
call restore_user_tls
3229

3330
pop r11
3431
pop rcx
3532
pop rsp
3633

3734
swapgs
38-
3935
o64 sysret
4036

41-
iretq_init:
42-
pop rdi
43-
iretq
44-
45-
sysret_fork_init:
46-
cli
47-
call restore_user_tls
37+
fork_init:
38+
swapgs
39+
jmp iretq_init
4840

49-
pop rax
50-
mov cr2, rax
51-
pop rax
41+
iretq_init:
42+
; pop the preserved registers
43+
pop r15
44+
pop r14
45+
pop r13
46+
pop r12
47+
pop rbp
5248
pop rbx
53-
pop rcx
54-
pop rdx
49+
50+
; pop the scratch registers
51+
pop r11
52+
pop r10
53+
pop r9
54+
pop r8
5555
pop rsi
5656
pop rdi
57-
pop rbp
58-
pop r8
59-
pop r9
60-
pop r10
61-
pop r11
62-
pop r12
63-
pop r13
64-
pop r14
65-
pop r15
66-
67-
pop r11 ; Restore rflags
68-
pop rcx ; Restore rip
69-
70-
push rdx
71-
72-
mov rdx, rsp
73-
add rdx, 16 ; Skip RDX and user RSP currently on the stack
74-
mov [gs:0x04], rdx ; Stash kernel stack
75-
7657
pop rdx
77-
pop rsp ; Restore user stack
78-
79-
swapgs
58+
pop rcx
59+
pop rax
8060

81-
o64 sysret
61+
iretq
8262

8363
; extern "C" fn task_spinup(prev: &mut Context, next: &mut Context)
8464
;
8565
; Saves the current context into `prev` and restore the context from `next`.
8666
task_spinup:
87-
pushfq
88-
89-
cli
90-
67+
; save callee-saved registers and this must match
68+
; the ordering of the fields in the `Context` struct.
9169
push rbp
92-
push r15
93-
push r14
94-
push r13
95-
push r12
9670
push rbx
71+
push r12
72+
push r13
73+
push r14
74+
push r15
9775

98-
mov rax, cr3 ; Save CR3
76+
mov rax, cr3 ; save CR3
9977
push rax
10078

101-
mov [rdi], rsp ; Update old context pointer with current stack pointer
102-
mov rsp, rsi ; Switch to new stack
79+
mov [rdi], rsp ; update old context pointer with current stack pointer
80+
mov rsp, rsi ; switch to new stack
10381

104-
pop rax ; Restore CR3
82+
pop rax ; restore CR3
10583
mov cr3, rax
10684

107-
pop rbx
108-
pop r12
109-
pop r13
110-
pop r14
85+
; restore callee-saved registers
11186
pop r15
87+
pop r14
88+
pop r13
89+
pop r12
90+
pop rbx
11291
pop rbp
11392

114-
popfq
115-
116-
; Resume the next thread.
93+
; resume the next thread
11794
ret

0 commit comments

Comments
 (0)