:: commit ed5d049c7235db015d9a6581d931c44d6701f489

mintsuki <mintsuki@protonmail.com> — 2020-09-25 20:36

parents: d192bb9101

Fix issue with int 13h DAP potentially not fitting within a single real mode segment; load stage 2 at 0x8000 and disable LTO as it is broken for some unknown reasons now

diff --git a/decompressor/Makefile b/decompressor/Makefile
index 2186687d..9f48cf50 100644
--- a/decompressor/Makefile
+++ b/decompressor/Makefile
@@ -2,9 +2,9 @@ CC = i386-elf-gcc
 LD = i386-elf-gcc
 OBJCOPY = i386-elf-objcopy
 
-CFLAGS = -flto -Os -pipe -Wall -Wextra
+CFLAGS = -Os -pipe -Wall -Wextra
 
-INTERNAL_CFLAGS =  \
+INTERNAL_CFLAGS = \
 	-std=gnu11 \
 	-ffreestanding \
 	-fno-stack-protector \
@@ -20,13 +20,14 @@ INTERNAL_CFLAGS =  \
 	-MMD \
 	-I.
 
-LDFLAGS = -flto -Os
+LDFLAGS = -Os
 
 INTERNAL_LDFLAGS = \
 	-lgcc \
 	-static-libgcc \
 	-nostdlib \
 	-no-pie \
+	-z max-page-size=0x1000 \
 	-static \
 	-Tlinker.ld
 
diff --git a/decompressor/linker.ld b/decompressor/linker.ld
index a5bae43a..707bddf5 100644
--- a/decompressor/linker.ld
+++ b/decompressor/linker.ld
@@ -24,4 +24,8 @@ SECTIONS
         *(.bss*)
         bss_end = .;
     }
+
+    /DISCARD/ : {
+        *(*)
+    }
 }
diff --git a/decompressor/main.c b/decompressor/main.c
index 68cd0a67..7c221ec0 100644
--- a/decompressor/main.c
+++ b/decompressor/main.c
@@ -4,8 +4,8 @@
 
 __attribute__((noreturn))
 void entry(uint8_t *compressed_stage2, size_t stage2_size, uint8_t boot_drive) {
-    // The decompressor should decompress compressed_stage2 to address 0x4000.
-    uint8_t *dest = (uint8_t *)0x4000;
+    // The decompressor should decompress compressed_stage2 to address 0x8000.
+    uint8_t *dest = (uint8_t *)0x8000;
 
     tinf_gzip_uncompress(dest, compressed_stage2, stage2_size);
 
diff --git a/limine.bin b/limine.bin
index b92fdfbc..ab7bd0c1 100644
Binary files a/limine.bin and b/limine.bin differ
diff --git a/stage2/Makefile b/stage2/Makefile
index 97728305..80cf9a6a 100644
--- a/stage2/Makefile
+++ b/stage2/Makefile
@@ -2,9 +2,9 @@ CC = i386-elf-gcc
 LD = i386-elf-gcc
 OBJCOPY = i386-elf-objcopy
 
-CFLAGS = -flto -Os -pipe -Wall -Wextra
+CFLAGS = -Os -pipe -Wall -Wextra
 
-INTERNAL_CFLAGS =  \
+INTERNAL_CFLAGS = \
 	-std=gnu11 \
 	-fplan9-extensions \
 	-ffreestanding \
@@ -22,13 +22,14 @@ INTERNAL_CFLAGS =  \
 	-I. \
 	-I..
 
-LDFLAGS = -flto -Os
+LDFLAGS = -Os
 
 INTERNAL_LDFLAGS = \
 	-lgcc \
 	-static-libgcc \
 	-nostdlib \
 	-no-pie \
+	-z max-page-size=0x1000 \
 	-static \
 	-Tlinker.ld
 
diff --git a/stage2/drivers/disk.c b/stage2/drivers/disk.c
index a81bd41b..bc888e03 100644
--- a/stage2/drivers/disk.c
+++ b/stage2/drivers/disk.c
@@ -17,35 +17,44 @@
 static uint8_t *cache        = NULL;
 static uint64_t cached_block = CACHE_INVALID;
 
-static struct {
+struct dap {
     uint16_t size;
     uint16_t count;
     uint16_t offset;
     uint16_t segment;
     uint64_t lba;
-} dap = { 16, BLOCK_SIZE_IN_SECTORS, 0, 0, 0 };
+};
+
+static struct dap *dap = NULL;
 
 static int cache_block(int drive, uint64_t block) {
     if (block == cached_block)
         return 0;
 
+    if (!dap) {
+        dap = conv_mem_alloc(sizeof(struct dap));
+        dap->size  = 16;
+        dap->count = BLOCK_SIZE_IN_SECTORS;
+    }
+
     if (!cache)
         cache = conv_mem_alloc_aligned(BLOCK_SIZE, 16);
 
-    dap.segment = rm_seg(cache);
-    dap.offset  = rm_off(cache);
-    dap.lba     = block * BLOCK_SIZE_IN_SECTORS;
+    dap->segment = rm_seg(cache);
+    dap->offset  = rm_off(cache);
+    dap->lba     = block * BLOCK_SIZE_IN_SECTORS;
 
     struct rm_regs r = {0};
     r.eax = 0x4200;
     r.edx = drive;
-    r.esi = (uint32_t)&dap;
+    r.esi = (uint32_t)rm_off(dap);
+    r.ds  = rm_seg(dap);
 
     rm_int(0x13, &r, &r);
 
     if (r.eflags & EFLAGS_CF) {
         int ah = (r.eax >> 8) & 0xff;
-        panic("Disk error %x. Drive %x, LBA %x.\n", ah, drive, dap.lba);
+        panic("Disk error %x. Drive %x, LBA %x.\n", ah, drive, dap->lba);
         cached_block = CACHE_INVALID;
         return ah;
     }
diff --git a/stage2/linker.ld b/stage2/linker.ld
index 3f0d47c1..8551fe38 100644
--- a/stage2/linker.ld
+++ b/stage2/linker.ld
@@ -3,7 +3,7 @@ ENTRY(_start)
 
 SECTIONS
 {
-    . = 0x4000;
+    . = 0x8000;
 
     .text : {
         KEEP(*(.entry*))
@@ -25,4 +25,8 @@ SECTIONS
         *(.bss*)
         bss_end = .;
     }
+
+    /DISCARD/ : {
+        *(*)
+    }
 }
diff --git a/stage2/mm/pmm.c b/stage2/mm/pmm.c
index 652bc7d0..3d56a17d 100644
--- a/stage2/mm/pmm.c
+++ b/stage2/mm/pmm.c
@@ -280,7 +280,7 @@ void *conv_mem_alloc_aligned(size_t count, size_t alignment) {
             uint16_t limit;
             uint32_t ptr;
         } __attribute__((packed)) gdtr;
-        asm volatile ("sgdt %0" :: "m"(gdtr));
+        asm volatile ("sgdt %0" :: "m"(gdtr) : "memory");
         bump_allocator_limit = gdtr.ptr;
     }
 
diff --git a/test/limine.cfg b/test/limine.cfg
index 3617dc05..21b61ebc 100644
--- a/test/limine.cfg
+++ b/test/limine.cfg
@@ -1,6 +1,6 @@
 TIMEOUT=3
 
-THEME_BLACK=33000000
+THEME_BLACK=50000000
 THEME_RED=aa0000
 THEME_GREEN=00aaff
 THEME_BROWN=aa5500
@@ -9,7 +9,7 @@ THEME_MAGENTA=aa00aa
 THEME_CYAN=9076DE
 THEME_GREY=aaaaaa
 THEME_WHITE=ffffff
-THEME_MARGIN=16
+THEME_MARGIN=64
 
 BACKGROUND_PARTITION=0
 BACKGROUND_PATH=bg.bmp
tab: 248 wrap: offon