From 6c227820a80fb18c9c04b588aa1cb7aa23fbc4ac Mon Sep 17 00:00:00 2001
From: Anton Kling <anton@kling.gg>
Date: Tue, 31 Oct 2023 14:28:15 +0100
Subject: Kernel: Remove all O0 optimizations on functions.

The kernel can now fully compile under O3 without any noticable
problems :)
---
 kernel/arch/i386/mmu.c   | 19 +++++++------------
 kernel/drivers/ata.c     | 15 ++++++---------
 kernel/sched/scheduler.c |  4 ++--
 3 files changed, 15 insertions(+), 23 deletions(-)

(limited to 'kernel')

diff --git a/kernel/arch/i386/mmu.c b/kernel/arch/i386/mmu.c
index f4246d4..6b2f704 100644
--- a/kernel/arch/i386/mmu.c
+++ b/kernel/arch/i386/mmu.c
@@ -430,18 +430,15 @@ void *virtual_to_physical(void *address, PageDirectory *directory) {
 }
 
 extern uint32_t inital_esp;
-void __attribute__((optimize("O0")))
+void
 move_stack(uint32_t new_stack_address, uint32_t size) {
-  mmu_allocate_region((void *)(new_stack_address - size), size,
-                      MMU_FLAG_KERNEL, NULL);
+  mmu_allocate_region((void *)(new_stack_address - size), size, MMU_FLAG_KERNEL,
+                      NULL);
 
   uint32_t old_stack_pointer, old_base_pointer;
 
-  register uint32_t eax asm("eax");
-  asm volatile("mov %esp, %eax");
-  old_stack_pointer = eax;
-  asm volatile("mov %ebp, %eax");
-  old_base_pointer = eax;
+  asm volatile("mov %%esp, %0" : "=r"(old_stack_pointer));
+  asm volatile("mov %%ebp, %0" : "=r"(old_base_pointer));
 
   uint32_t new_stack_pointer =
       old_stack_pointer + ((uint32_t)new_stack_address - inital_esp);
@@ -463,10 +460,8 @@ move_stack(uint32_t new_stack_address, uint32_t size) {
 
   inital_esp = new_stack_pointer;
   // Actually change the stack
-  eax = new_stack_pointer;
-  asm volatile("mov %eax, %esp");
-  eax = new_base_pointer;
-  asm volatile("mov %eax, %ebp");
+  asm volatile("mov %0, %%esp" ::"irm"(new_stack_pointer));
+  asm volatile("mov %0, %%ebp" ::"irm"(new_base_pointer));
 }
 
 // C strings have a unknown length so it does not makes sense to check
diff --git a/kernel/drivers/ata.c b/kernel/drivers/ata.c
index fd9b504..82e745b 100644
--- a/kernel/drivers/ata.c
+++ b/kernel/drivers/ata.c
@@ -105,7 +105,7 @@ int poll_status(void) {
 }
 
 // Instructions from: https://wiki.osdev.org/ATA_PIO_Mode#28_bit_PIO
-void __attribute__((optimize("O0")))
+void
 setup_drive_for_command(uint32_t lba, uint32_t sector_count) {
   // 1. Send 0xE0 for the "master" or 0xF0 for
   //    the "slave", ORed with the highest 4 bits
@@ -139,9 +139,8 @@ void delayed_rep_outsw(size_t n, uint16_t port, volatile uint8_t *buffer) {
   }
 }
 
-void __attribute__((optimize("O0")))
-ata_write_lba28(uint32_t lba, uint32_t sector_count,
-                volatile const uint8_t *buffer) {
+void ata_write_lba28(uint32_t lba, uint32_t sector_count,
+                     volatile const uint8_t *buffer) {
   setup_drive_for_command(lba, sector_count);
 
   outb(io_base + COMMAND_PORT, WRITE_SECTORS);
@@ -169,8 +168,8 @@ ata_write_lba28(uint32_t lba, uint32_t sector_count,
 }
 
 // Instructions from: https://wiki.osdev.org/ATA_PIO_Mode#28_bit_PIO
-void __attribute__((optimize("O0")))
-ata_read_lba28(uint32_t lba, uint32_t sector_count, volatile void *address) {
+void ata_read_lba28(uint32_t lba, uint32_t sector_count,
+                    volatile void *address) {
   // Step 1-6 is done in this function.
   setup_drive_for_command(lba, sector_count);
 
@@ -211,12 +210,10 @@ void ata_init(void) {
   select_drive(1);
 }
 
-void __attribute__((optimize("O0")))
-read_lba(uint32_t lba, void *address, size_t size, size_t offset) {
+void read_lba(uint32_t lba, void *address, size_t size, size_t offset) {
   uintptr_t ptr = (uintptr_t)address;
   lba += offset / SECTOR_SIZE;
   offset = offset % SECTOR_SIZE;
-  asm("cli");
   size_t total_read = 0;
   for (int i = 0; size > 0; i++) {
     uint32_t read_len =
diff --git a/kernel/sched/scheduler.c b/kernel/sched/scheduler.c
index a6ace7d..697bcda 100644
--- a/kernel/sched/scheduler.c
+++ b/kernel/sched/scheduler.c
@@ -93,7 +93,7 @@ int get_free_fd(process_t *p, int allocate) {
 void tasking_init(void) { current_task = ready_queue = create_process(NULL); }
 
 int i = 0;
-void __attribute__((optimize("O0"))) free_process(void) {
+void free_process(void) {
   kprintf("Exiting process: %s\n", get_current_task()->program_name);
   // free_process() will purge all contents such as allocated frames
   // out of the current process. This will be called by exit() and
@@ -212,7 +212,7 @@ int exec(const char *filename, char **argv) {
   return 0;
 }
 
-int __attribute__((optimize("O0"))) fork(void) {
+int fork(void) {
   process_t *parent_task = (process_t *)current_task;
 
   process_t *new_task = create_process(parent_task);
-- 
cgit v1.2.3