moved stuff around

This commit is contained in:
2026-06-10 09:39:37 -04:00
parent 20fd32f978
commit 0f8d5f8ce7
8 changed files with 58 additions and 76 deletions

24
kernel/kernel.ld Normal file
View File

@@ -0,0 +1,24 @@
ENTRY(_start)
SECTIONS
{
. = 0x10000;
.text : {
*(.text)
}
.rodata : {
*(.rodata)
}
.data : {
*(.data)
}
.bss : {
*(.bss)
}
_end = .;
}

28
kernel/kmain.c Normal file
View File

@@ -0,0 +1,28 @@
#include <stdint.h>
extern void halt();
extern void idt_load();
extern void reboot();
typedef struct {
uint16_t offset_low;
uint16_t selector;
uint8_t zero;
uint8_t flags;
uint16_t offset_high;
} idt_entry_t __attribute__((packed));
typedef struct {
uint16_t limit;
uint32_t base;
} idt_ptr_t;
idt_entry_t idt[256];
idt_ptr_t idtr;
void load_idt() {}
void kmain() {
load_idt();
halt();
}

118
kernel/prekernel.s Normal file
View File

@@ -0,0 +1,118 @@
.code16
.text
.global _start
_start:
jmp everything
.org 20
.global kernel_size
kernel_size:
.long _end - _start
everything:
mov $0x1000, %ax
mov %ax, %es
mov %ax, %ds
# reset disk
mov $0, %ah
int $0x13
# stop the cursor
mov $1, %ah
mov $0,%cl
mov $0x20,%ch
int $0x11
# get the system memory, if this call fails then halt
clc
mov $0, %bx
mov $0xe801, %ax
int $0x15
jc halt
shr $10, %ax
shr $4, %bx
add %ax, %bx
mov %bx, total_mem_mb-_start
# vga text mode
mov $0x0003, %ax
int $0x10
enable_a20:
#enable the a20 gate
inb $0x92, %al
orb $2, %al
outb %al, $0x92
prep_protected:
cli
lgdt gdt_init-_start
mov %cr0, %eax
or $0x01, %eax
mov %eax, %cr0 # protected bit set
ljmpl $8, $protected
halt16:
jmp halt16
.code32
protected:
mov $16, %ax
mov %ax, %es
mov %ax, %ds
mov %ax, %ss
mov $5*8, %ax
ltr %ax
mov $0, %ax
mov %ax, %fs
mov %ax, %gs
mov $0xFFF0, %sp
mov $0xFFF0, %bp
ljmpl $8, $kmain
# some assembly stubs we can call from the kernel
.global idt_load
.extern idtr
idt_load:
lidt idtr
ret
.global reboot
reboot:
cli
lidt reboot_struct
int $1
reboot_struct:
.word 0
.long 0
.global halt
halt:
cli
hlt
jmp halt
# kernel data structures
.align 16
.global gdt
gdt:
.word 0,0,0,0
.word 0xffff, 0x0000, 0x9a00, 0x00cf
.word 0xffff, 0x0000, 0x9200, 0x00cf
.word 0xffff, 0x0000, 0xfa00, 0x00cf
.word 0xffff, 0x0000, 0xf200, 0x00cf
.word 0x0068, (tss-_start), 0x8901, 0x00cf
gdt_init:
.word gdt_init-gdt
.long gdt
.align 16
.global tss
tss:
.long 0
.global total_mem_mb
total_mem_mb:
.word 0