From 5ba30e5f8b1540fce7fb50c23f2937bac7e1f38d Mon Sep 17 00:00:00 2001 From: Eric-Paul Ickhorn Date: Tue, 6 Aug 2024 13:20:40 +0200 Subject: [PATCH] Fix GDT length being insufficient The value of the GDTR register was set to be 8 bytes too small, with the effect that an accurate emulation software would error out because of trying to read over the buffer size, as was noticed when using KVM. This was due to the perception that the 0th index wouldn't be taken into account; for that reason, the length of the GDTR was one index short of what it should've been. --- i386/loader/src-asm/memory/gdt.asm | 38 +++++++++++++++++------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/i386/loader/src-asm/memory/gdt.asm b/i386/loader/src-asm/memory/gdt.asm index 897caa1..7d1cfcb 100755 --- a/i386/loader/src-asm/memory/gdt.asm +++ b/i386/loader/src-asm/memory/gdt.asm @@ -239,28 +239,40 @@ load_flat_gdt: .set_segments: push ebp mov ebp, esp - push dword 2 ; Segment Index - push dword 0 ; Start Offset - push dword 0xfffff ; Page Count - push dword 0 ; Required privilege level + push dword 2 ; Segment Index + push dword 0 ; Start Offset + push dword 0x000fffff ; Page Count + push dword 0 ; Required privilege level call make_regular_data_segment_in_gdt mov esp, ebp pop ebp push ebp mov ebp, esp - push dword 1 ; Segment Index - push dword 0x0000 ; Start Offset - push dword 0xfffff ; Page Count - push dword 0 ; Required privilege level + push dword 1 ; Segment Index + push dword 0x0000 ; Start Offset + push dword 0x000fffff ; Page Count + push dword 0 ; Required privilege level call make_code_segment_in_gdt mov esp, ebp pop ebp mov bx, 0 mov ds, bx + mov es, bx + mov fs, bx + mov gs, bx + mov ss, bx lgdt [.gdtr] + mov ebx, cr0 + or ebx, 1 + mov cr0, ebx + + jmp dword (1 << 3):.epilog + +bits 32 +.epilog: mov bx, (2 << 3) mov ds, bx mov es, bx @@ -268,14 +280,6 @@ load_flat_gdt: mov gs, bx mov ss, bx - mov ebx, cr0 - or ebx, 1 - mov cr0, ebx - - jmp dword 0x08:.epilog - -bits 32 -.epilog: mov edi, [esi + (64 - 20)] mov edx, [esi + (64 - 16)] mov ecx, [esi + (64 - 12)] @@ -287,7 +291,7 @@ bits 32 align 16 .gdtr: .gdtr.length: - dw 15 + dw 23 .gdtr.pointer: dd GDT_MEMORY_AREA