Broke it :)

This commit is contained in:
2025-03-22 18:19:26 +01:00
parent c3f8d7d8f2
commit 4f543f3fd0
4 changed files with 56 additions and 35 deletions
+1
View File
@@ -110,6 +110,7 @@ struct tss_pointer {
uint32_t ssp; uint32_t ssp;
} __packed; } __packed;
void tss_esp0(uint32_t esp0);
int gdt_init(void); int gdt_init(void);
#endif #endif
+17 -30
View File
@@ -2,47 +2,34 @@ OUTPUT_FORMAT("elf32-i386")
OUTPUT_ARCH("i386"); OUTPUT_ARCH("i386");
ENTRY(_start) ENTRY(_start)
/* Define correct program headers */
PHDRS {
text PT_LOAD FLAGS((1 << 0) | (1 << 2)); /* Read, Execute */
rodata PT_LOAD FLAGS((1 << 0)); /* Read */
data PT_LOAD FLAGS((1 << 0) | (1 << 1)); /* Read, Write */
}
/* Define sections */ /* Define sections */
SECTIONS { SECTIONS {
/* 1MB is a conventional place for kernels */ . = 0x00100000;
. = 1M; kernel_start = .;
__kernel_start = .; .boot ALIGN(4K) : {
.text BLOCK(4K) : ALIGN(4K) {
*(.multiboot) *(.multiboot)
*(.boot)
}
. += 0xC0000000;
.text ALIGN(4K) : AT(ADDR(.text) - 0xC0000000) {
*(.text) *(.text)
} : text }
.rodata BLOCK(4K) : ALIGN(4K) { .rodata ALIGN(4K) : AT(ADDR(.rodata) - 0xC0000000) {
*(.rodata) *(.rodata)
} : rodata }
.data BLOCK(4K) : ALIGN(4K) { .data ALIGN(4K) : AT(ADDR(.data) - 0xC0000000) {
*(.data) *(.data)
} : data }
.bss BLOCK(4K) : ALIGN(4K) { .bss ALIGN(4K) : AT(ADDR(.bss) - 0xC0000000) {
*(COMMON) *(COMMON)
*(.bss) *(.bss)
} : data
/* Stack */
. = ALIGN(16);
__kernel_stack_bottom = .;
. += 16K;
__kernel_stack_top = .;
__kernel_end = .;
/DISCARD/ : {
*(.eh_frame)
*(.note*)
} }
kernel_end = .;
} }
+35 -4
View File
@@ -28,13 +28,28 @@ __kernel_stack_bottom:
__kernel_stack_top: __kernel_stack_top:
; Entry point ; Entry point
section .text section .boot
global _start global _start
extern kmain extern kmain
_start: _start:
; Temporary paging
mov edx, (init_pagedir - 0xC0000000)
mov cr3, edx
mov ecx, cr0
or ecx, 0x80000000
mov cr0, ecx
jmp $
jmp higher_half
section .text
higher_half:
mov esp, __kernel_stack_top mov esp, __kernel_stack_top
push eax add ebx, 0xC0000000 ; Make it a virtual address
push ebx push eax ; Magic
push ebx ; Multiboot info
call kmain ; Call kernel main call kmain ; Call kernel main
; Hang if kernel returns ; Hang if kernel returns
@@ -42,3 +57,19 @@ _start:
.hlt: hlt .hlt: hlt
jmp .hlt jmp .hlt
section .data
align 4096
global init_pagedir
init_pagedir:
dd 0b10000011 ; initial 4mb identity map, unmapped later
times 768-1 dd 0 ; padding
; hh kernel start, map 16 mb
dd (0 << 22) | 0b10000011 ; 0xC0000000
dd (1 << 22) | 0b10000011
dd (2 << 22) | 0b10000011
dd (3 << 22) | 0b10000011
times 256-4 dd 0 ; padding
; dd initial_page_dir | 11b
+3 -1
View File
@@ -29,6 +29,8 @@ void tss_reload(void) {
gdt[5].access |= 0x02; gdt[5].access |= 0x02;
} }
void tss_esp0(uint32_t esp0) { tss.esp0 = esp0; }
int gdt_init(void) { int gdt_init(void) {
// Null descriptor (0) // Null descriptor (0)
gdt[0] = GDT_DESC(0, 0, 0, 0); gdt[0] = GDT_DESC(0, 0, 0, 0);
@@ -47,7 +49,7 @@ int gdt_init(void) {
// TSS (40) // TSS (40)
memset(&tss, 0, sizeof(tss)); memset(&tss, 0, sizeof(tss));
gdt[5] = GDT_DESC(sizeof(tss) - 1, (uint32_t)&tss, 0x89, 0x00); gdt[5] = GDT_DESC(sizeof(tss) - 1, (uint32_t)&tss, 0x89, 0x40);
tss.iopb = sizeof(struct tss_pointer); tss.iopb = sizeof(struct tss_pointer);
tss.esp0 = __kernel_stack_top; tss.esp0 = __kernel_stack_top;
tss.ss0 = GDT_KERNEL_DATA; tss.ss0 = GDT_KERNEL_DATA;