transfer control to C

This commit is contained in:
rami 2024-06-25 22:47:21 -04:00
parent 203483d8b2
commit 2b1b7568b0
4 changed files with 99 additions and 6 deletions

View File

@ -2,7 +2,9 @@ CC = i686-elf-gcc
AS = nasm
BUILDDIR := build
CFLAGS := -ffreestanding -Wall -Wextra -Werror
INCLUDEDIR := include
CFLAGS := -ffreestanding -Wall -Wextra -Werror -I $(INCLUDEDIR)
LDFLAGS := -ffreestanding -nostdlib -lgcc -T kernel/kernel.ld
KSRC := $(wildcard kernel/*.asm kernel/*.c)

View File

@ -0,0 +1,29 @@
#ifndef HAZEL_MULTIBOOT_H_
#define HAZEL_MULTIBOOT_H_
#include <stdint.h>
typedef struct {
uint32_t flags; //required
uint32_t memlower; //if bit 0 in flags are set
uint32_t memupper; //if bit 0 in flags are set
uint32_t bootdevice; //if bit 1 in flags are set
uint32_t commandline; //if bit 2 in flags are set
uint32_t modulecount; //if bit 3 in flags are set
uint32_t moduleaddress; //if bit 3 in flags are set
uint32_t syms[4]; //if bits 4 or 5 in flags are set
uint32_t memmaplength; //if bit 6 in flags is set
uint32_t memmapaddress; //if bit 6 in flags is set
uint32_t driveslength; //if bit 7 in flags is set
uint32_t drivesaddress; //if bit 7 in flags is set
uint32_t configtable; //if bit 8 in flags is set
uint32_t apmtable; //if bit 9 in flags is set
uint32_t vbecontrolinfo; //if bit 10 in flags is set
uint32_t vbemodeinfo; //if bit 11 in flags is set
uint32_t vbemode; // all vbe_* set if bit 12 in flags are set
uint32_t vbeinterfaceseg;
uint32_t vbeinterfaceoff;
uint32_t vbeinterfacelength;
} multiboot_info_t;
#endif

View File

@ -9,8 +9,64 @@ dd -(MAGIC + FLAGS)
section .text
global _start
_start:
mov byte [0xb8000], 'X'
mov byte [0xb8001], 0x0f
cli
cli
jmp $
; Check if EAX contains this magic value that our bootloader should've set
cmp eax, 0x2BADB002
jne halt
; Load GDT
lgdt [gdtp]
; Offset to kernel data descriptor
mov ax, 0x10
mov es, ax
mov ds, ax
mov fs, ax
mov gs, ax
mov ss, ax
; Offset to kernel code descriptor
jmp 0x8:.use_code_seg
.use_code_seg:
mov esp, stack_bottom
mov ebp, esp
push ebx
and esp, 0xfffffff0
extern kernel
call kernel
cli
halt:
hlt
jmp halt
section .data
; GDT for a flat memory layout
; We get access to all memory and can utilize paging
global gdt
gdt:
gdt_null:
dq 0
gdt_kern_code:
dw 0xffff
dw 0x0000
db 0x00
db 0b10011010
db 0b11001111
db 0x00
gdt_kern_data:
dw 0xffff
dw 0x0000
db 0x00
db 0b10010010
db 0b11001111
db 0x00
gdt_end:
gdtp:
dw gdt_end - gdt - 1
dd gdt
section .bss
align 16
stack_top:
resb 16384
stack_bottom:

View File

@ -1,4 +1,10 @@
void kernel() {
#include <kernel/multiboot.h>
void kernel(multiboot_info_t *info) {
char *vmem = (char *)0xb8000;
vmem[0] = info->flags;
vmem[1] = 0x0f;
asm volatile ("cli" ::);
for (;;) {}
}