From 547b9c4b7e45e8c89cbaf15bc71fe0bb1d2c440f Mon Sep 17 00:00:00 2001 From: Mathias Krause Date: Fri, 15 Oct 2021 00:27:02 +0200 Subject: [PATCH 1/2] drop __naked attribute were used wrongly According to [1] the __naked attribute is only meant for functions with basic asm bodies. But neither kernel_main() nor ap_startup() is, so drop the bogus attribute. It's, at best, a micro optimization not worth the trouble. However, make kernel_main() a __noreturn function as that's what it is. [1] https://gcc.gnu.org/onlinedocs/gcc/x86-Function-Attributes.html#index-naked-function-attribute_002c-x86 Signed-off-by: Mathias Krause --- common/kernel.c | 4 ++-- include/ktf.h | 2 +- smp/smp.c | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/common/kernel.c b/common/kernel.c index 4ae7adce..b47d42cd 100644 --- a/common/kernel.c +++ b/common/kernel.c @@ -43,14 +43,14 @@ int usermode_call(user_func_t fn, void *fn_arg) { PERCPU_OFFSET(user_stack)); } -static void echo_loop(void) { +static void __noreturn echo_loop(void) { while (1) { io_delay(); keyboard_process_keys(); } } -void __naked kernel_main(void) { +void kernel_main(void) { printk("\nKTF - Kernel Test Framework!\n\n"); if (kernel_cmdline) diff --git a/include/ktf.h b/include/ktf.h index 8d57ec15..24d613bf 100644 --- a/include/ktf.h +++ b/include/ktf.h @@ -44,7 +44,7 @@ extern bool opt_debug; extern int usermode_call(user_func_t fn, void *fn_arg); -extern void kernel_main(void); +extern void kernel_main(void) __noreturn; extern void test_main(void); #endif /* __ASSEMBLY__ */ diff --git a/smp/smp.c b/smp/smp.c index 4f81e139..0ebddfc9 100644 --- a/smp/smp.c +++ b/smp/smp.c @@ -47,7 +47,7 @@ static __data_init bool ap_callin; static __data_init void *ap_new_sp; cr3_t __data_init ap_cr3; -void __noreturn __naked ap_startup(void) { +void __noreturn ap_startup(void) { WRITE_SP(ap_new_sp); init_traps(ap_cpuid); From b8023487e1b05bf99090e9ab004744c813423efc Mon Sep 17 00:00:00 2001 From: Mathias Krause Date: Fri, 15 Oct 2021 00:43:45 +0200 Subject: [PATCH 2/2] boot: simplify head.S Various minor cleanups: - use 16bit source registers when assigning segment registers to have matching register sizes - don't obfuscate CR0 initialization, make it plain obvious we're enabling protected mode - do the same for CR4 initalization, make it obvious we're setting it to PSE|PAE - drop the unneded CLD, as we've fully assigned RFLAGS already with the POPF Signed-off-by: Mathias Krause --- arch/x86/boot/head.S | 42 +++++++++++++++++++----------------------- 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/arch/x86/boot/head.S b/arch/x86/boot/head.S index 0e3813a7..925d86d7 100644 --- a/arch/x86/boot/head.S +++ b/arch/x86/boot/head.S @@ -33,32 +33,30 @@ SECTION(.text.init, "ax", 16) GLOBAL(_start) /* Save multiboot bootloader magic */ - mov %eax, %edi - mov %ebx, %esi + mov %eax, %edi + mov %ebx, %esi mov %cs, %ax mov %ax, %ds lgdt boot_gdt_ptr - xor %ax, %ax - inc %ax - lmsw %ax + mov $X86_CR0_PE, %eax + mov %eax, %cr0 ljmp $__KERN_CS32, $.Lprot_mode .Lprot_mode: - mov $__KERN_DS32, %eax - mov %eax, %ds - mov %eax, %es - mov %eax, %gs - mov %eax, %fs - mov %eax, %ss + mov $__KERN_DS32, %ax + mov %ax, %ds + mov %ax, %es + mov %ax, %gs + mov %ax, %fs + mov %ax, %ss mov $_boot_stack_top, %esp mov %esp, %ebp - mov %cr4, %eax - or $(X86_CR4_PAE | X86_CR4_PSE), %eax + mov $(X86_CR4_PAE | X86_CR4_PSE), %eax mov %eax, %cr4 mov $l4_pt_entries, %eax @@ -100,17 +98,15 @@ END_FUNC(prot_to_real) .code64 .Llong_mode: - xor %rax, %rax - mov %rax, %ds - mov %rax, %es - mov %rax, %fs - mov %rax, %gs - mov %rax, %ss - - push $X86_EFLAGS_MBS - popf + xor %eax, %eax + mov %ax, %ds + mov %ax, %es + mov %ax, %fs + mov %ax, %gs + mov %ax, %ss - cld + pushq $X86_EFLAGS_MBS + popfq jmp kernel_start