Loading 64-bit segments into all selectors

This commit is contained in:
Thomas Oltmann 2025-07-05 21:31:37 +02:00
parent c24a6014f7
commit 04f6352b77
4 changed files with 37 additions and 12 deletions

View file

@ -13,8 +13,11 @@ config.mk: | config.default.mk
boot.bin: boot.elf
objcopy -O binary --only-section=.text boot.elf $@
boot.elf: lboot.o fernlader.ld
$(LD) $(LDFLAGS) -o $@ lboot.o
boot.elf: lboot.o loader.o fernlader.ld
$(LD) $(LDFLAGS) -o $@ lboot.o loader.o
.S.o:
lboot.o: lboot.S
$(CC) $(CFLAGS) -c -o $@ $(@:.o=.S)
loader.o: loader.c
$(CC) $(CFLAGS) -m64 -Os -c -o $@ $(@:.o=.c)

View file

@ -1,6 +1,7 @@
SECTIONS {
. = 0x7C00;
.text : {
*(*)
.text 0x7C00: {
lboot.o(.text*, .data*)
loader.o(.text*, .data*, .bss*)
}
memmap = ALIGN(8);
}

28
lboot.S
View file

@ -255,9 +255,15 @@ long:
or $0x80000001, %eax
mov %eax, %cr0
lgdt gdt64_ptr
// Linearize stack address
mov %ss, %eax
shl $4, %eax
add %eax, %esp
mov %esp, %ebp
ret
// Load long mode GDT, switch to 64-bit CS
lgdt gdt64_ptr
ljmp $0x8, $trampo64
// hang: sleep indefinitely
hang: hlt
@ -303,7 +309,7 @@ gdt64: // entry 0: null descriptor
.word 0
.word 0
.byte 0
.byte 0x90
.byte 0x92
.byte 0x00
.byte 0
.set gdt64_size, .-gdt64
@ -328,7 +334,17 @@ pd_ptr: .long 0
pdp_ptr: .long 0
pml4_ptr: .long 0
// Memory map area
// Points to the end of the memory map
memmap_end: .word 0
.align 8
memmap: // begins at end of binary
.code64
// trampo64: Trampoline function to load long-mode segments
// before entering the loader.
trampo64:
mov $0x10, %eax
mov %eax, %ds
mov %eax, %es
mov %eax, %fs
mov %eax, %gs
mov %eax, %ss // FIXME why does this crash?
jmp loader_main

5
loader.c Normal file
View file

@ -0,0 +1,5 @@
void
loader_main(void)
{
for (;;) {}
}