os

Toy OS
git clone git://git.margiolis.net/os.git
Log | Files | Refs | README | LICENSE

commit 7dc87eb0921c8cf455acdef80edaa27632270d73
parent 99e45b41b99f1cab5196fef4d5163c448b84eb82
Author: Christos Margiolis <christos@margiolis.net>
Date:   Thu,  9 Dec 2021 03:13:36 +0200

improve build system (still bad)

Diffstat:
MMakefile | 6+++---
Mkern/Makefile | 50++++----------------------------------------------
Mkern/boot.s | 15++++++++++++---
Mkern/idt.c | 16++++++++++------
Mkern/kbd.c | 4++++
Mkern/link.ld | 1-
Mkern/vm_page.h | 9---------
Amk/kern.mk | 43+++++++++++++++++++++++++++++++++++++++++++
8 files changed, 76 insertions(+), 68 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,11 +1,11 @@ -TGTDIR= build +BINDIR= build all: cd kern && make install clean && cd - run: - qemu-system-i386 -hda ${TGTDIR}/os.bin -d cpu -monitor stdio + qemu-system-i386 -hda ${BINDIR}/os.img -d cpu -monitor stdio clean: - rm -rf ${TGTDIR} + rm -rf ${BINDIR} cd kern && make clean && cd - diff --git a/kern/Makefile b/kern/Makefile @@ -1,23 +1,11 @@ -BIN= os.bin +BIN= os.img BINDIR= ../build +PREFIX= .. # base dir -CC= cc -ASM= nasm -LD= ld -ARCH= i386 # FIXME: make this automatic -ARCHINCDIR= ../${ARCH} -INCDIR= ../include LINKSCRIPT= link.ld -CFLAGS= -g -m32 -Wall -Wextra -Werror -std=c99 -O2 \ - -nostdlib -nodefaultlibs \ - -ffreestanding -fno-stack-protector -fno-builtin \ - -I${ARCHINCDIR} -I${INCDIR} -LDFLAGS= -T${LINKSCRIPT} --oformat binary -melf_${ARCH} - -BOOTFILE= boot.s +BOOT_FILE= boot.s BOOT_BIN= boot.bin KERNEL_BIN= kernel.bin -SRC= *.c *.s OBJ= kern_main.o \ libk.o \ idt.o \ @@ -28,34 +16,4 @@ OBJ= kern_main.o \ timer.o \ vm_page.o -all: options ${BIN} - -options: - @echo ${BIN} build options - @echo ARCH = ${ARCH} - @echo CC = ${CC} - @echo ASM = ${ASM} - @echo LD = ${LD} - @echo CFLAGS = ${CFLAGS} - @echo LDFLAGS = ${LDFLAGS} - -${BIN}: ${OBJ} - mkdir -p ${BINDIR} - ${ASM} -fbin ${BOOTFILE} -o ${BOOT_BIN} - ${LD} ${LDFLAGS} ${OBJ} -o ${KERNEL_BIN} - dd if=/dev/zero bs=1000000 count=1 >> ${KERNEL_BIN} - cat ${BOOT_BIN} ${KERNEL_BIN} > $@ - -.c.o: - ${CC} -c ${CFLAGS} $< - -.s.o: - ${ASM} -felf $< - -install: all - mv ${BIN} ${BINDIR} - -clean: - rm -rf *.bin *.o - -.PHONY: all options install clean +include ../mk/kern.mk diff --git a/kern/boot.s b/kern/boot.s @@ -13,6 +13,8 @@ section .text ; Entry point. _start: cli ; Disable interrupts. + jmp 0x0000:zeroseg +zeroseg: xor ax, ax ; Clear segment registers. mov ds, ax mov es, ax @@ -64,10 +66,10 @@ a20_test: pop bx mov bx, 0x7e0e - mov dx, [es:bx] ; If the A20 line is disabled we get 0xaa55. + mov dx, [es:bx] cmp ax, dx - je cont ; If they're equal, the A20 line might be disabled. + je cont ; If 0xaa55, the A20 line might be disabled. popa mov ax, 0x01 ; Success code 1. ret @@ -265,10 +267,17 @@ pm_init: mov gs, ax mov ss, ax + xor eax, eax + mov ebx, eax + mov ecx, eax + mov edx, eax + mov esi, eax + mov edi, eax + mov ebp, kern_stack_bottom mov esp, kern_stack_top ; Hand control over to the C kernel. Godspeed! You Black Emperor. - call KERNOFF + jmp GDT_CODESEG:KERNOFF jmp $ ; Print a null-terminated string. diff --git a/kern/idt.c b/kern/idt.c @@ -19,12 +19,16 @@ idt_set_gate(struct gate_desc *gd, void *func, u_int sel, u_int dpl, u_int type) gd->gd_lo = (sel << 16) | (addr & 0xffff); } -extern void INTVEC(div), INTVEC(dbg), INTVEC(nmsk), INTVEC(bpt), INTVEC(ofl), +/* + * u_char so `-pedantic` won't complain about taking the + * address of `void`. + */ +extern u_char INTVEC(div), INTVEC(dbg), INTVEC(nmsk), INTVEC(bpt), INTVEC(ofl), INTVEC(bnd), INTVEC(ill), INTVEC(dna), INTVEC(dbl), INTVEC(fpusegm), INTVEC(tss), INTVEC(missing), INTVEC(stk), INTVEC(prot), INTVEC(page), INTVEC(rsvd), INTVEC(fpu), INTVEC(align), INTVEC(mchk), INTVEC(simd); -extern void INTVEC(irq0), INTVEC(irq1), INTVEC(irq2), INTVEC(irq3), +extern u_char INTVEC(irq0), INTVEC(irq1), INTVEC(irq2), INTVEC(irq3), INTVEC(irq4), INTVEC(irq5), INTVEC(irq6), INTVEC(irq7), INTVEC(irq8), INTVEC(irq9), INTVEC(irq10), INTVEC(irq11), INTVEC(irq12), INTVEC(irq13), INTVEC(irq14), INTVEC(irq15); @@ -150,10 +154,10 @@ dump_regs(struct reg *r) r->r_eax, r->r_ebx, r->r_ecx, r->r_edx); printf("esi=%#08x\tedi=%#08x\tebp=%#08x\tesp=%#08x\n", r->r_esi, r->r_edi, r->r_ebp, r->r_esp); - printf("ds=%#08x \tes=%#08x \tfs=%#08x \tgs=%#08x\n", - r->r_ds, r->r_es, r->r_fs, r->r_gs); - printf("eip=%#08x\tcs=%#08x \tss=%#08x \teflags=%#08x\n", - r->r_eip, r->r_cs, r->r_ss, r->r_eflags); + printf("ds=%#04x \t\tes=%#04x \t\tfs=%#04x \t\tgs=%#04x\n", + (u_short)r->r_ds, (u_short)r->r_es, (u_short)r->r_fs, (u_short)r->r_gs); + printf("cs=%#04x \t\tss=%#04x \t\teip=%#08x\teflags=%#08x\n", + (u_short)r->r_cs, (u_short)r->r_ss, r->r_eip, r->r_eflags); printf("int=%#08x\terr=%#08x\tuesp=%#08x\n", r->r_intrno, r->r_err, r->r_uesp); } diff --git a/kern/kbd.c b/kern/kbd.c @@ -99,6 +99,10 @@ kbd_callback(struct reg *r) if ((sc = inb(KBD_CMD)) & KBD_PRESSED) { } else { /* TODO: shift */ + if (kbdus_lower[sc] == 'r') { + dump_regs(r); + return; + } vga_putc(shift ? kbdus_upper[sc] : kbdus_lower[sc]); } UNUSED(r); diff --git a/kern/link.ld b/kern/link.ld @@ -2,7 +2,6 @@ ENTRY(_start) SECTIONS { .text 0x1000 : { - *(text.prologue) *(.text) . = ALIGN(4096); } diff --git a/kern/vm_page.h b/kern/vm_page.h @@ -1,15 +1,6 @@ #ifndef _VM_PAGE_H_ #define _VM_PAGE_H_ -struct vm_page_entry { -}; - -struct vm_page_table { -}; - -struct vm_page_dir { -}; - void vm_page_init(void); #endif /* _VM_PAGE_H_ */ diff --git a/mk/kern.mk b/mk/kern.mk @@ -0,0 +1,43 @@ +CC= cc +AS= nasm +LD= ld +ARCH= i386 # FIXME: make this automatic +ARCHINCDIR+= ${PREFIX}/${ARCH} +INCDIR+= ${PREFIX}/include +CFLAGS+= -g -m32 -Wall -Wextra -Werror -pedantic -std=c99 -O2 \ + -nostdlib -nodefaultlibs \ + -ffreestanding -fno-stack-protector -fno-builtin \ + -I${ARCHINCDIR} -I${INCDIR} +LDFLAGS+= -T${LINKSCRIPT} --oformat binary -melf_${ARCH} # FIXME: elf sig osdev + +all: options ${BIN} + +options: + @echo ${BIN} build options + @echo "ARCH = ${ARCH}" + @echo "CC = ${CC}" + @echo "AS = ${AS}" + @echo "LD = ${LD}" + @echo "CFLAGS = ${CFLAGS}" + @echo "LDFLAGS = ${LDFLAGS}" + +${BIN}: ${OBJ} + mkdir -p ${BINDIR} + ${AS} -fbin ${BOOT_FILE} -o ${BOOT_BIN} + ${LD} ${LDFLAGS} ${OBJ} -o ${KERNEL_BIN} + dd if=/dev/zero bs=1000000 count=1 >> ${KERNEL_BIN} + cat ${BOOT_BIN} ${KERNEL_BIN} > $@ + +.c.o: + ${CC} -c ${CFLAGS} $< + +.s.o: + ${AS} -felf $< + +install: all + mv ${BIN} ${BINDIR} + +clean: + rm -rf *.bin *.img *.o + +.PHONY: all options install clean