|
| 1 | +use core::{arch::asm, mem::size_of}; |
| 2 | + |
| 3 | +static GDT: GdtProtectedMode = GdtProtectedMode::new(); |
| 4 | + |
| 5 | +#[repr(C)] |
| 6 | +pub struct GdtProtectedMode { |
| 7 | + zero: u64, |
| 8 | + code: u64, |
| 9 | + data: u64, |
| 10 | +} |
| 11 | + |
| 12 | +impl GdtProtectedMode { |
| 13 | + const fn new() -> Self { |
| 14 | + let limit = { |
| 15 | + let limit_low = 0xffff; |
| 16 | + let limit_high = 0xf << 48; |
| 17 | + limit_high | limit_low |
| 18 | + }; |
| 19 | + let access_common = { |
| 20 | + let present = 1 << 47; |
| 21 | + let user_segment = 1 << 44; |
| 22 | + let read_write = 1 << 41; |
| 23 | + present | user_segment | read_write |
| 24 | + }; |
| 25 | + let protected_mode = 1 << 54; |
| 26 | + let granularity = 1 << 55; |
| 27 | + let base_flags = protected_mode | granularity | access_common | limit; |
| 28 | + let executable = 1 << 43; |
| 29 | + Self { |
| 30 | + zero: 0, |
| 31 | + code: base_flags | executable, |
| 32 | + data: base_flags, |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + fn clear_interrupts_and_load(&'static self) { |
| 37 | + let pointer = GdtPointer { |
| 38 | + base: &GDT, |
| 39 | + limit: (3 * size_of::<u64>() - 1) as u16, |
| 40 | + }; |
| 41 | + |
| 42 | + unsafe { |
| 43 | + asm!("cli", "lgdt [{}]", in(reg) &pointer, options(readonly, nostack, preserves_flags)); |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +#[repr(C, packed(2))] |
| 49 | +pub struct GdtPointer { |
| 50 | + /// Size of the DT. |
| 51 | + pub limit: u16, |
| 52 | + /// Pointer to the memory region containing the DT. |
| 53 | + pub base: *const GdtProtectedMode, |
| 54 | +} |
| 55 | + |
| 56 | +unsafe impl Send for GdtPointer {} |
| 57 | +unsafe impl Sync for GdtPointer {} |
| 58 | + |
| 59 | +pub fn enter_unreal_mode() { |
| 60 | + let ds: u16; |
| 61 | + unsafe { |
| 62 | + asm!("mov {0:x}, ds", out(reg) ds, options(nomem, nostack, preserves_flags)); |
| 63 | + } |
| 64 | + |
| 65 | + GDT.clear_interrupts_and_load(); |
| 66 | + |
| 67 | + // set protected mode bit |
| 68 | + let mut cr0: u32; |
| 69 | + unsafe { |
| 70 | + asm!("mov {}, cr0", out(reg) cr0, options(nomem, nostack, preserves_flags)); |
| 71 | + } |
| 72 | + let cr0_protected = cr0 | 1; |
| 73 | + write_cr0(cr0_protected); |
| 74 | + |
| 75 | + unsafe { |
| 76 | + asm!("mov bx, 0x10", "mov ds, bx"); |
| 77 | + } |
| 78 | + |
| 79 | + write_cr0(cr0); |
| 80 | + |
| 81 | + unsafe { |
| 82 | + asm!("mov ds, {0:x}", in(reg) ds, options(nostack, preserves_flags)); |
| 83 | + asm!("sti"); |
| 84 | + |
| 85 | + asm!("mov bx, 0x0f01", "mov eax, 0xb8000", "mov [eax], bx"); |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +fn write_cr0(val: u32) { |
| 90 | + unsafe { asm!("mov cr0, {}", in(reg) val, options(nostack, preserves_flags)) }; |
| 91 | +} |
0 commit comments