From 5145ed1e2adefc9c66e20681532a3a7cd6c2de01 Mon Sep 17 00:00:00 2001 From: John Tsiombikas Date: Thu, 17 Nov 2022 21:14:57 +0200 Subject: [PATCH] reorg done --- .gitignore | 2 +- kern/Makefile | 25 ++++++++++++++++++------- kern/kern.ld | 16 +++++++++------- kern/src/boot.asm | 20 +++++++++----------- kern/src/main.c | 2 +- kern/src/startup.asm | 13 ++++++++++--- 6 files changed, 48 insertions(+), 30 deletions(-) diff --git a/.gitignore b/.gitignore index a3492c9..8a10a5b 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,4 @@ dis1 dis2 *.log -bootldr +86kern diff --git a/kern/Makefile b/kern/Makefile index 44d9d66..70e479b 100644 --- a/kern/Makefile +++ b/kern/Makefile @@ -1,27 +1,38 @@ +csrc = $(wildcard src/*.c) +ssrc = $(wildcard src/*.asm) +obj = $(csrc:.c=.o) $(ssrc:.asm=.o) +dep = $(csrc:.c=.d) + disk_numsec = 720 #disk_numsec = 2880 -bin = bootldr -img = bootldr.img +bin = 86kern +img = 86kern.img CC = ia16-elf-gcc LD = ia16-elf-ld -CFLAGS = -march=i8088 -mtune=i8088 +CFLAGS = -march=i8088 -mtune=i8088 -MMD $(img): $(bin) dd if=/dev/zero of=$@ bs=512 count=$(disk_numsec) dd if=$< of=$@ bs=512 conv=notrunc -$(bin): boot/boot.o boot/boot2.o boot/main.o - $(LD) -T boot.ld -Map boot.map -o $@ $^ +$(bin): $(obj) + $(LD) -T kern.ld -Map kern.map -o $@ $(obj) + +-include $(dep) %.o: %.asm nasm -o $@ -f elf $< .PHONY: clean clean: - rm -f boot/*.o $(bin) + rm -f $(obj) $(bin) + +.PHONY: cleandep +cleandep: + rm -f $(dep) .PHONY: run @@ -35,4 +46,4 @@ debug: $(img) .PHONY: disasm disasm: $(bin) ndisasm -o 0x7c00 $< >dis1 - ndisasm -o 0x80000 -e 512 $< >dis2 + ndisasm -o 0x98000 -e 512 $< >dis2 diff --git a/kern/kern.ld b/kern/kern.ld index 99bc701..3922811 100644 --- a/kern/kern.ld +++ b/kern/kern.ld @@ -8,10 +8,12 @@ SECTIONS { } _bootsect_end = .; - /* load high out of the way, to allow stage2 to load the kernel low */ - . = 0x80000; - _stage2_start = .; - _stage2_start_seg = _stage2_start >> 4; + /* load high out of the way, to leave the rest of RAM for userland + * reserving the top 32kb for the kernel for now + */ + . = 0x98000; + _kern_start = .; + _kern_start_seg = _kern_start >> 4; .text : AT(_bootsect_end) { * (.startup); * (.text*); @@ -22,13 +24,13 @@ SECTIONS { } .bss ALIGN(4) (NOLOAD): { _bss_start = .; - _bss_start_off = _bss_start - _stage2_start; + _bss_start_off = _bss_start - _kern_start; * (.bss*); * (COMMON); . = ALIGN(4); } _bss_size = SIZEOF(.bss); . = ALIGN(4); - _stage2_end = .; - _stage2_size = _stage2_end - _stage2_start; + _kern_end = .; + _kern_size = _kern_end - _kern_start; }; diff --git a/kern/src/boot.asm b/kern/src/boot.asm index 96506de..5be618b 100644 --- a/kern/src/boot.asm +++ b/kern/src/boot.asm @@ -3,8 +3,8 @@ bits 16 section .bootsect -extern _stage2_start_seg -extern _stage2_size +extern _kern_start_seg +extern _kern_size boot_driveno equ 7b00h num_read_tries equ 7b06h ; 2 bytes @@ -57,7 +57,7 @@ bios_param_block: db 0 ; 25h: winnt flags db 28h ; 26h: signature(?) dd 0 ; 27h: volume serial number - db "80 SIX BOOT"; 2bh: volume label, 11 bytes + db "86BOOT "; 2bh: volume label, 11 bytes db "FAT12 " ; 36h: filesystem id, 8 bytes start: @@ -65,12 +65,10 @@ start: xor ax, ax mov ds, ax mov es, ax + mov ss, ax jmp 00:.setcs .setcs: - ; put the stack high - mov ax, 0x7f00 - mov ss, ax - xor sp, sp + mov sp, 0x7b00 ; temp stack below our vars mov [boot_driveno], dl ; query sectors per track @@ -92,13 +90,13 @@ start: .querydone: ; load the rest of the code high - mov ax, _stage2_size + mov ax, _kern_size add ax, 511 mov cl, 9 shr ax, cl inc ax mov [sect_pending], ax - mov ax, _stage2_start_seg + mov ax, _kern_start_seg mov es, ax ; destination segment mov word [destptr], 0 mov word [start_sect], 1 ; start from sector 1 to skip boot sector @@ -173,7 +171,7 @@ start: jnz .rdloop ; loaded sucessfully, load segment registers and jump -.done: mov ax, _stage2_start_seg +.done: mov ax, _kern_start_seg mov ds, ax mov es, ax push ax @@ -248,7 +246,7 @@ print_hex_digit: str_rdtrack2 db " from ",0 str_rdtrack3 db "/",0 -str_load_fail db "Failed to load 2nd stage!",0 +str_load_fail db "Failed to load kernel!",0 str_newline db 13,10,0 times 510-($-$$) db 0 diff --git a/kern/src/main.c b/kern/src/main.c index 7110581..1ca879b 100644 --- a/kern/src/main.c +++ b/kern/src/main.c @@ -1,4 +1,4 @@ -void bootmain(void) +void kmain(void) { int i, j; unsigned short __far *vmem = (void __far*)0xb8000000ul; diff --git a/kern/src/startup.asm b/kern/src/startup.asm index c90cef6..c64684a 100644 --- a/kern/src/startup.asm +++ b/kern/src/startup.asm @@ -3,8 +3,10 @@ bits 16 section .startup -extern bootmain -extern _stage2_start_seg +; memory reserved at the top of RAM for the kernel stack +STACKSZ equ 4096 + +extern kmain extern _bss_start_off extern _bss_size @@ -12,6 +14,11 @@ global _start _start: ; TODO floppy off if necessary + ; move stack to the top of RAM (TODO: detect and avoid Ext. BIOS data area) + mov ax, (0xa0000 - STACKSZ) >> 4 + mov ss, ax + mov sp, STACKSZ + ; zero .bss mov di, _bss_start_off mov cx, _bss_size @@ -20,7 +27,7 @@ _start: xor ax, ax mov es, ax - call bootmain + call kmain hang: hlt jmp hang -- 1.7.10.4