Began rewrite

This commit is contained in:
2025-02-16 16:27:46 +01:00
parent 295e3d9c18
commit 7d41cf45ed
82 changed files with 99 additions and 3028 deletions
+24
View File
@@ -0,0 +1,24 @@
#ifndef _KERNEL_H
#define _KERNEL_H
/*
* Attribute definitions
*/
#define __align(_b) __attribute__((aligned(_b)))
#define __depricated __attribute__((depricated))
#define __packed __attribute__((packed))
#define __noreturn __attribute__((noreturn))
#define __const __attribute__((const))
#define __pure __attribute__((pure))
#define __inline __attribute__((always_inline))
#define __no_inline __attribute__((never_inline))
#define __section(_s) __attribute__((section(_s)))
#define __cold __attribute__((cold))
#define __hot __attribute__((hot))
#define __naked __attribute__((naked))
/*
* Utility macros
*/
#define sizeof_array(_a) ()
#endif
-22
View File
@@ -1,22 +0,0 @@
#ifndef _STDINT_H
#define _STDINT_H
#define NULL ((void*)0)
// Naitive size
typedef unsigned int size_t;
typedef signed int ssize_t;
// Signed ints
typedef signed char int8_t;
typedef signed short int16_t;
typedef signed int int32_t;
typedef signed long long int64_t;
// Unsigned ints
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
typedef unsigned long long uint64_t;
#endif
+9
View File
@@ -0,0 +1,9 @@
#ifndef _TTY_H
#define _TTY_H
int tty_init(void);
int tty_write(char* buf, size_t len);
int tty_putc(char c);
int tty_puts(char* s);
#endif
+13 -34
View File
@@ -1,52 +1,31 @@
OUTPUT_FORMAT(binary)
ENTRY(start)
OUTPUT_FORMAT("elf32-i386")
ENTRY(_start)
SECTIONS {
. = 0x00100000;
kernel_start = .;
/* 1MB is a conventional place for kernels */
. = 1M;
/* Multiboot Header */
.multiboot ALIGN(4K) : {
/* We need the multiboot header in the first 8192 bytes */
.text BLOCK(4K) : ALIGN(4K) {
*(.multiboot)
}
.boot ALIGN(4K) : {
*(.boot)
}
. += 0xC000000;
/* Kernel Code */
.text ALIGN(4) : AT(ADDR(.text) - 0xC000000) {
*(.text)
*(.text.*)
}
/* Read-Only Data */
.rodata ALIGN(4) : AT(ADDR(.rodata) - 0xC000000) {
/* Read-only data */
.rodata BLOCK(4K) : ALIGN(4K) {
*(.rodata)
*(.rodata.*)
}
/* Read/Write Data */
.data ALIGN(4) : AT(ADDR(.data) - 0xC000000) {
/* Initalized r/w data */
.data BLOCK(4K) : ALIGN(4K) {
*(.data)
*(.data.*)
}
/* BSS Section */
.bss ALIGN(4) : AT(ADDR(.bss) - 0xC000000) {
/* Uninitialized r/w data and stack */
.bss BLOCK(4K) : ALIGN(4K) {
*(COMMON)
*(.bss)
*(.bss.*)
}
/* Discarded Sections */
/DISCARD/ : {
*(.note)
*(.note.*)
*(.comment)
}
kernel_end = .;
. = 4M;
}
+10 -13
View File
@@ -1,30 +1,27 @@
include ../defaults.mk
INC = ./include
SRC = ./src
BIN = ./bin
SRCS_C = $(shell find $(SRC) -name '*.c')
SRCS_S = $(shell find $(SRC) -name '*.s')
OBJS = $(patsubst $(SRC)/%.c, $(BIN)/%.o, $(SRCS_C)) \
$(patsubst $(SRC)/%.s, $(BIN)/%.s.o, $(SRCS_S))
INC = ./include
SRCS_C = $(wildcard $(SRC)/*.c)
SRCS_S = $(wildcard $(SRC)/*.s)
OBJS_C = $(patsubst $(SRC)/%.c, $(BIN)/%.o, $(SRCS_C))
OBJS_S = $(patsubst $(SRC)/%.s, $(BIN)/%.o, $(SRCS_S))
OUT = ./kernel.elf
LINKER = ./linker.ld
OUT = ./kern.bin
.PHONY: all clean
all: $(OUT)
$(OUT): $(OBJS)
$(OUT): $(OBJS_C) $(OBJS_S)
$(LD) $(LDFLAGS) -T$(LINKER) -o $@ $^
$(BIN)/%.o: $(SRC)/%.c
@mkdir -p $(dir $@)
$(CC) $(CFLAGS) -I$(INC) -c -o $@ $<
$(CC) $(CCFLAGS) -I$(INC) -c -o $@ $^
$(BIN)/%.s.o: $(SRC)/%.s
$(BIN)/%.o: $(SRC)/%.s
@mkdir -p $(dir $@)
$(AS) $(ASFLAGS) -felf32 -o $@ $<
$(AS) $(ASFLAGS) -felf32 -o $@ $^
clean:
rm -fr $(BIN) $(OUT)
+27 -95
View File
@@ -1,108 +1,40 @@
;
; Kernel entry point
;
MBH_MAGIC equ 0xe85250d6
MBH_ARCH equ 0x00000000
STACK_SIZE equ 4096
global VIRT_BASE
KERN_VIRT_BASE equ 0xC000000
KERN_PAGE_NUM equ (KERN_VIRT_BASE >> 22)
bits 32
; Multiboot2 Header
; Flag bits
%define ALIGN 1<<0
%define MEMINFO 1<<1
; Field values
%define MAGIC 0x1BADB002
%define FLAGS (ALIGN | MEMINFO)
%define CHECK -(MAGIC + FLAGS)
; Multiboot 1 header
section .multiboot
align 64
mbh:
; Magic fields
.magic: dd MBH_MAGIC
.arch: dd MBH_ARCH
.size: dd (mbh.end - mbh)
.checksum: dd 0x10000000 - (MBH_MAGIC + MBH_ARCH + (mbh.end - mbh))
align 4
multiboot:
dd MAGIC
dd FLAGS
dd CHECK
; Framebuffer Tag
align 8
dw 5 ; Type - Framebuffer
dw 1 ; Flags - Optional
dd 20 ; Size
dd 1920 ; Width
dd 1080 ; Height
dd 32 ; Depth
; Zero Tag
align 8
dw 0 ; Type - Zero
dw 0 ; Flags
dd 8 ; Size
.end:
; Stack
; Initialize stack
section .bss
align 16
stack_bottom:
resb 16384*8
resb 16384 ; 16kB
stack_top:
; Entry section
section .boot
global start
extern kmain
start:
; Init temporary paging
mov eax, (initpd - 0xC0000000)
mov cr3, eax
mov ecx, cr4
or ecx, 0x10 ; Paging enable bit
mov cr4, ecx
mov ecx, cr0
or ecx, 0x8000000
mov cr0, ecx
jmp higher_half
Shutdown:
mov ax, 0x1000
mov ss, ax
mov sp, 0xf000
mov ax, 0x5307
mov bx, 0x0001
mov cx, 0x0003
int 0x15
; Entry point
section .text
higher_half:
mov esp, stack_top ; Stack Pointer
global _start
extern kmain
_start:
mov esp, stack_top
add ebx, KERN_VIRT_BASE ; Make address in EBX virtual
push ebx ; Push multiboot mem info pointer
call kmain ; Call kernel main
call kmain
; Halt on return
cli
h: hlt
jmp h
section .data
; Inital Page Directory
align 4096
global initpd
initpd:
dd 0b10000011 ; Initial 4MB identity map
times 768-1 dd 0 ; Padding
; Higher half kernel start, map 16MB
dd (0 << 22) | 0b10000011
dd (1 << 22) | 0b10000011
dd (2 << 22) | 0b10000011
dd (3 << 22) | 0b10000011
times 256-4 dd 0 ; More padding
; Hang if kernel returns
cli
.hlt: hlt
jmp .hlt
+6 -2
View File
@@ -1,4 +1,8 @@
#include <stdint.h>
#include <kernel.h>
void kmain(uint32_t addr) {}
__noreturn
void kmain(void) {
*(char*)0xb8000 = 'a';
while (1) __asm__ volatile ("movl $69, %eax");
}