From 429762dca4d313140067d14e1c448ee68d1644d2 Mon Sep 17 00:00:00 2001 From: Samuel Thibault Date: Mon, 5 Feb 2024 23:22:00 +0100 Subject: apboot: avoid self-modifying code self-modifying code is generally frowned upon, Intel largely says the support is model-dependent. We can as well just relocate from the C code like we did for the temporary gdt. --- i386/i386/cpuboot.S | 18 ++++-------------- i386/i386at/model_dep.c | 7 ++++++- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/i386/i386/cpuboot.S b/i386/i386/cpuboot.S index b2f9e520..3e4d5420 100644 --- a/i386/i386/cpuboot.S +++ b/i386/i386/cpuboot.S @@ -100,7 +100,7 @@ apboot_percpu_med: apboot_percpu_high: .byte 0 -.globl apboot, apbootend, gdt_descr_tmp +.globl apboot, apbootend, gdt_descr_tmp, apboot_jmp_offset .align 16 .code16 @@ -128,21 +128,11 @@ _apboot: orl $CR0_SET_FLAGS, %eax movl %eax, %cr0 - xorl %eax, %eax - mov %cs, %ax - shll $4, %eax - addl $M(0f), %eax - movl %eax, M(ljmp_offset32) - - /* Flush cached instruction queue */ - jmp 1f -1: - - /* ljmpl with relocation */ + /* ljmpl with relocation from machine_init */ .byte 0x66 .byte 0xea -ljmp_offset32: - .long 0xffffffff +apboot_jmp_offset: + .long M(0f) .word BOOT_CS 0: diff --git a/i386/i386at/model_dep.c b/i386/i386at/model_dep.c index e0995c96..173b99f5 100644 --- a/i386/i386at/model_dep.c +++ b/i386/i386at/model_dep.c @@ -129,6 +129,9 @@ extern char version[]; /* Realmode temporary GDT */ extern struct pseudo_descriptor gdt_descr_tmp; +/* Realmode relocated jmp */ +extern uint32_t apboot_jmp_offset; + /* If set, reboot the system on ctrl-alt-delete. */ boolean_t rebootflag = FALSE; /* exported to kdintr */ @@ -221,9 +224,11 @@ void machine_init(void) assert (apboot_addr < 0x100000); /* - * Patch the realmode gdt with the correct offset + * Patch the realmode gdt with the correct offset and the first jmp to + * protected mode with the correct target. */ gdt_descr_tmp.linear_base += apboot_addr; + apboot_jmp_offset += apboot_addr; #endif } -- cgit v1.2.3