From 4947a84591191564447eda0455162f6ccfdca7dd Mon Sep 17 00:00:00 2001 From: mintsuki Date: Mon, 1 Jun 2020 04:47:55 +0200 Subject: [PATCH] Make sure to mask and flush IRQs for stivale protocol --- STIVALE.md | 4 +++ qloader2.bin | Bin 32768 -> 32768 bytes src/lib/real.c | 82 +++++++++++++++++++++++++++++++++++++++++++ src/lib/real.h | 2 ++ src/protos/stivale.c | 3 ++ 5 files changed, 91 insertions(+) diff --git a/STIVALE.md b/STIVALE.md index fd501657..54448c59 100644 --- a/STIVALE.md +++ b/STIVALE.md @@ -67,6 +67,8 @@ If stivale header flag bit 1 is set, then, if available, 5-level paging is enabl The A20 gate is enabled. +PIC/APIC IRQs are all masked. + `rsp` is set to the requested stack as per stivale header. `rdi` will point to the stivale structure (described below). @@ -91,6 +93,8 @@ PE is enabled (`cr0`). The A20 gate is enabled. +PIC/APIC IRQs are all masked. + `esp` is set to the requested stack as per stivale header. A pointer to the stivale structure (described below) is pushed onto this stack diff --git a/qloader2.bin b/qloader2.bin index 4c7ebf33fc884b741297fea33a622dac92f1b77b..c76758797b3e52e3ff7a6fa90d98ad62cee97b3f 100644 GIT binary patch delta 5433 zcma)AeOOc18Nc_ENYp^0e2SovFG2hO9Txep(GrJfy9<#7q4-fXUTL?rYcjjFNV=HK zYZ|-l*r$88c9jMGP!#8*_*>jlQZFtl2=S^qRVOzCMONJx_KsZ0cg|X|AhUDL)p(Qa)Np4vok3p>BT% zFXWztxX~%#MW!a^C-;+5^&-yLPkyF$aFr*?J$2#A2j`+Qt!}^j>gSTg!~GiG1p%%3 zJcg-Vtvlep0T__d9eCk7jM#2oXh3-`$g68W&fE*j?}g+Z@{2K3+1=z#xc-gwjA`KR z%OiYbt@b@I3~DMWd11^cav*Xo_uX8Qq*=(#Iz?(U*<9Tz;?^u+1LTNiiiU?3ZI8?C zCm(4H_vgZ-^_AzxGu!l?M#@FU=+2Dzq3D~0(#_0~XvE3ruWauY_<7RQsoFbdX zO>RVCDf|W02Dqlf-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_i^y1J$ zkDPh106!LRCm%qkQYXOlUDBej;dhYF^r^xN*NLo8vwe>E{hOF_$nf(_K|+O5WLyPC z8~%*(b4lAG;q+!EczXiV$E|G#i*y+KqSIXx?{t)5XnA0V%`L^0NH0JqLCZ)> z(19-G08ip^Y_Y`|wB=K1GSS7FP0lmXvhy%e4!}N1>IxnITe5UTf{=EB7+0*dJqb4G zGsAfF@W@LWaBYG$oV^3gy&VxfAegcjRr15qc0?WyBLGH8?<1rGNDei0{Lm&S3Vi`Q zd&(gjKK0|FB)Rla9vwq60$c-Z7&Ox$@t(B+Iuq8x)$DS-45rMUWw3BLehjpy49F43 zOF;6LEX>t%03<{ul#)vDvJfux(?7HH1l4r&aq^4u628@Yp}de6)}Qw#t`vA)MzcG@RqJUHl64qUi%;E`i#i{ diff --git a/src/lib/real.c b/src/lib/real.c index b1d57389..9000d05a 100644 --- a/src/lib/real.c +++ b/src/lib/real.c @@ -1,6 +1,88 @@ #include #include +__attribute__((naked)) +void rm_flush_irqs(void) { + asm ( + // Mask PICs + "mov al, 0xff\n\t" + "out 0x21, al\n\t" + "out 0xa1, al\n\t" + + // Save GDT in case BIOS overwrites it + "sgdt [8f]\n\t" + + // Save non-scratch GPRs + "push ebx\n\t" + "push esi\n\t" + "push edi\n\t" + "push ebp\n\t" + + // Jump to real mode + "jmp 0x08:1f\n\t" + "1: .code16\n\t" + "mov ax, 0x10\n\t" + "mov ds, ax\n\t" + "mov es, ax\n\t" + "mov fs, ax\n\t" + "mov gs, ax\n\t" + "mov ss, ax\n\t" + "mov eax, cr0\n\t" + "and al, 0xfe\n\t" + "mov cr0, eax\n\t" + "jmp 0:2f\n\t" + "2:\n\t" + "mov ax, 0\n\t" + "mov ds, ax\n\t" + "mov es, ax\n\t" + "mov fs, ax\n\t" + "mov gs, ax\n\t" + "mov ss, ax\n\t" + + "sti\n\t" + "call 2f\n\t" // call await + "cli\n\t" + + // Restore GDT + "lgdt [8f]\n\t" + + // Jump back to pmode + "mov eax, cr0\n\t" + "or al, 1\n\t" + "mov cr0, eax\n\t" + "jmp 0x18:4f\n\t" + "4: .code32\n\t" + "mov ax, 0x20\n\t" + "mov ds, ax\n\t" + "mov es, ax\n\t" + "mov fs, ax\n\t" + "mov gs, ax\n\t" + "mov ss, ax\n\t" + + // Restore non-scratch GPRs + "pop ebp\n\t" + "pop edi\n\t" + "pop esi\n\t" + "pop ebx\n\t" + + // Exit + "ret\n\t" + + // gdt + "1: .long 0\n\t" + " .long 0\n\t" + + // Await + ".code16\n\t" + "2: xor al, al\n\t" + "mov cx, 0x1000\n\t" + "3: out 0x80, al\n\t" + "loop 3b\n\t" + "ret\n\t" + ".code32\n\t" + ); +} + __attribute__((naked)) void rm_int(uint8_t int_no, struct rm_regs *out_regs, struct rm_regs *in_regs) { asm ( diff --git a/src/lib/real.h b/src/lib/real.h index 545cbded..b04eb146 100644 --- a/src/lib/real.h +++ b/src/lib/real.h @@ -24,4 +24,6 @@ struct rm_regs { void rm_int(uint8_t int_no, struct rm_regs *out_regs, struct rm_regs *in_regs); +void rm_flush_irqs(void); + #endif diff --git a/src/protos/stivale.c b/src/protos/stivale.c index 0b641175..59a0c4e2 100644 --- a/src/protos/stivale.c +++ b/src/protos/stivale.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -226,6 +227,8 @@ void stivale_load(char *cmdline, int boot_drive) { deinit_vga_textmode(); } + rm_flush_irqs(); + if (bits == 64) { void *pagemap_ptr; if (level5pg && (stivale_hdr.flags & (1 << 1))) {