1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//! Context and ring switching

use super::gdt::SegmentSelector;

/// Prepare for return from interrupt to user,
/// the expected current stack layout is:
///
/// ```text
///  _____________________________
/// | User Data Segment Selector  |
/// | User Stack Pointer          |
/// | EFLAGS (Interrupts enabled) |
/// | User Code Segment Selector  |
/// | Target Instruction Pointer  |
/// ```
pub extern "C" fn switch_to_user(
    ds: SegmentSelector,
    sp: *mut u32,
    cs: SegmentSelector,
    ip: *mut (),
    syscall_ret: u32,
) -> ! {
    unsafe {
        todo!("BST B1 - Implement ring switch")
    }
}

/// Starts the thread for the first time
#[inline(never)]
pub unsafe extern "C" fn launch(thread: &Registers) {
    todo!("BSB A4")
}

/// Switches between two threads
#[inline(never)]
pub unsafe extern "C" fn switch(current: &mut Registers, next: &Registers) {
    todo!("BSB A4")
}

/// Non-volatile registers that have to survive function calls.
///
/// See <https://wiki.osdev.org/Calling_Conventions>
#[derive(Copy, Clone, Debug)]
#[repr(C)]
pub struct Registers {
    pub ebx: u32,
    pub esi: u32,
    pub edi: u32,
    pub ebp: u32,
    pub esp: u32,
}

impl Registers {
    pub const fn new() -> Self {
        Self {
            ebx: 0,
            esi: 0,
            edi: 0,
            ebp: 0,
            esp: 0,
        }
    }
}