diff --git a/.editorconfig b/.editorconfig index 023b9a7..059f2de 100644 --- a/.editorconfig +++ b/.editorconfig @@ -5,4 +5,4 @@ end_of_line = lf insert_final_newline = true charset = utf-8 indent_style = tab -indent_size = 4 \ No newline at end of file +indent_size = 4 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d03d4d5..aa3fb55 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,7 +11,7 @@ jobs: - name: Install dependencies run: | sudo apt-get update - sudo apt-get install build-essential llvm lld nasm + sudo apt-get install build-essential llvm lld nasm mtools - name: Build run: | make diff --git a/.gitignore b/.gitignore index 17661a9..b793d89 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,13 @@ *.o *.img *.bin +*.iso *~ *.elf +*.a *.dump +vmstupid + + +/sysroot diff --git a/Makefile b/Makefile index 653ab56..ec8134c 100644 --- a/Makefile +++ b/Makefile @@ -5,10 +5,13 @@ AS = nasm LD = ld.lld OBJDUMP = llvm-objdump +QEMU = qemu-system-i386 RM = rm -f +MKCWD = mkdir -p $(@D) +INSTALL = install BIN_DIR = bin -DOC_DIR = doc +DOC_DIR = docs KERNEL_DIR = kernel LIB_DIR = lib TOOLS_DIR = tools @@ -16,22 +19,28 @@ TOOLS_DIR = tools include $(TOOLS_DIR)/build.mk ASFLAGS = -DSTUPID_VERSION=\"$(shell $(GIT-VERSION))\" -Ilib +QEMUFLAGS = -serial stdio -GARBADGE = stupid.img +GARBADGE = stupid.iso include $(KERNEL_DIR)/build.mk +include $(LIB_DIR)/build.mk +include $(BIN_DIR)/build.mk -all: stupid.img +all: stupid.iso -stupid.img: $(KERNEL_BIN) $(KERNEL_DUMP) +sysroot: $(KERNEL_BIN) $(KERNEL_DUMP) $(LIBS_BIN) + $(INSTALL) -d $@ + $(INSTALL) -d $@/bin + $(INSTALL) -d $@/lib + $(INSTALL) $(KERNEL_BIN) $@ + $(INSTALL) $(LIBS_BIN) $@/lib -#stupid.img: floppy.bin kern.bin -# mkdosfs -F 12 -C $@ 1440 -# dd status=noxfer conv=notrunc if=floppy.bin of=$@ -# mcopy -i $@ kern.bin ::kern.sys +stupid.iso: sysroot + $(CREATE-ISO) $@ $< -%.dump: %.elf - $(OBJDUMP) -D $^ > $@ +run: stupid.iso + $(QEMU) $(QEMUFLAGS) -cdrom $< clean: $(RM) $(GARBADGE) diff --git a/README.md b/README.md index 1bf5023..917a5eb 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,3 @@ # StupidOS 32-bit Operating System in assembly. - diff --git a/bin/build.mk b/bin/build.mk new file mode 100644 index 0000000..e69de29 diff --git a/kernel/build.mk b/kernel/build.mk index 3bae25e..e3e97cd 100644 --- a/kernel/build.mk +++ b/kernel/build.mk @@ -1,9 +1,9 @@ include kernel/drivers/build.mk -KERNEL_SRCS = head.s gdt.s idt.s paging.s lib/log.s +KERNEL_SRCS = head.s gdt.s pic.s isr.s idt.s paging.s lib/log.s KERNEL_OBJS = $(addprefix kernel/, $(KERNEL_SRCS:.s=.o) $(DRIVERS_OBJS)) -KERNEL_BIN = stupid.elf -KERNEL_DUMP = $(KERNEL_BIN:.elf=.dump) +KERNEL_BIN = vmstupid +KERNEL_DUMP = $(KERNEL_BIN).dump KERNEL_ASFLAGS = $(ASFLAGS) -D__KERNEL__ @@ -12,8 +12,11 @@ GARBADGE += $(KERNEL_OBJS) $(KERNEL_BIN) $(KERNEL_DUMP) $(KERNEL_BIN): $(KERNEL_OBJS) $(LD) -T $(KERNEL_DIR)/linker.ld -nostdlib -o $@ $^ +$(KERNEL_DUMP): $(KERNEL_BIN) + $(OBJDUMP) -D $^ > $@ + kernel/lib/%.o: lib/base/%.s - mkdir -p $(@D) + @$(MKCWD) $(AS) -felf -o $@ $< $(KERNEL_ASFLAGS) kernel/%.o: kernel/%.s diff --git a/kernel/head.s b/kernel/head.s index 8bb1fe1..856e758 100644 --- a/kernel/head.s +++ b/kernel/head.s @@ -29,14 +29,21 @@ entry: extern serial_init call serial_init + LOG msg_hello_world + extern setup_gdt call setup_gdt + extern setup_pic + call setup_pic + extern setup_idt call setup_idt - extern setup_paging - call setup_paging + int3 + + ;extern setup_paging + ;call setup_paging LOG file @@ -44,4 +51,8 @@ hang: hlt jmp hang + +section .rodata + +msg_hello_world db "StupidOS ", STUPID_VERSION, 0 file db __FILE__, 0 diff --git a/kernel/idt.s b/kernel/idt.s index 3c4202e..99c93ef 100644 --- a/kernel/idt.s +++ b/kernel/idt.s @@ -2,15 +2,33 @@ section .text global setup_idt setup_idt: +%assign i 0 +extern isr %+ i + mov eax, isr %+ i + mov [idt_entries + i * 4], ax + shr eax, 16 + mov word [idt_entries + i * 4 + 2], 0x8 + mov byte [idt_entries + i * 4 + 5], 0x8E + mov [idt_entries + i * 4 + 6], ax + +%assign i i+1 +%rep 32 +%endrep lidt [idt_ptr] + sti ret section .data idt_ptr: - dw idt_entries.end - idt_entries - 1 + dw 256 * 8 dd idt_entries idt_entries: times 256 dd 0x00000000, 0x00000000 + ;; dw isr_low + ;; dw kernel cs + ;; db zero + ;; db attr + ;; dw isr_high .end: diff --git a/kernel/isr.s b/kernel/isr.s new file mode 100644 index 0000000..f93f057 --- /dev/null +++ b/kernel/isr.s @@ -0,0 +1,87 @@ +[BITS 32] + +%include "base.inc" + +%macro ISR_NO_ERR 1 +global isr%1 +isr%1: + cli + push byte 0 + push byte %1 + jmp isr_handler +%endmacro + +%macro ISR_ERR 1 +global isr%1 +isr%1: + cli + push byte 1 + push byte %1 + jmp isr_handler +%endmacro + +section .text + +ISR_NO_ERR 0 +ISR_NO_ERR 1 +ISR_NO_ERR 2 +ISR_NO_ERR 3 +ISR_NO_ERR 4 +ISR_NO_ERR 5 +ISR_NO_ERR 6 +ISR_NO_ERR 7 +ISR_ERR 8 +ISR_NO_ERR 9 +ISR_ERR 10 +ISR_ERR 11 +ISR_ERR 12 +ISR_ERR 13 +ISR_ERR 14 +ISR_NO_ERR 15 +ISR_NO_ERR 16 +ISR_NO_ERR 17 +ISR_NO_ERR 18 +ISR_NO_ERR 19 +ISR_NO_ERR 20 +ISR_NO_ERR 21 +ISR_NO_ERR 22 +ISR_NO_ERR 23 +ISR_NO_ERR 24 +ISR_NO_ERR 25 +ISR_NO_ERR 26 +ISR_NO_ERR 27 +ISR_NO_ERR 28 +ISR_NO_ERR 29 +ISR_NO_ERR 30 +ISR_NO_ERR 31 + +global isr_handler +isr_handler: + pusha + + mov ax, ds + push eax + mov ax, 0x10 + mov ds, ax + mov es, ax + mov fs, ax + mov gs, ax + + LOG msg_interrupt + + pop eax + mov ds, ax + mov es, ax + mov fs, ax + mov gs, ax + + popa + add esp, 8 + + sti + iret + +section .rodata + +msg_interrupt db "interrupt", 0 +file db __FILE__, 0 diff --git a/kernel/pic.s b/kernel/pic.s new file mode 100644 index 0000000..c7f8712 --- /dev/null +++ b/kernel/pic.s @@ -0,0 +1,24 @@ +[BITS 32] +section .text + +global setup_pic +setup_pic: + mov al, 0x11 + out 0x20, al + out 0xA0, al + + mov al, 0x20 + out 0x21, al + mov al, 0x28 + out 0xA1, al + + mov al, 4 + out 0x21, al + mov al, 2 + out 0xA1, al + + ; mask all + mov al, 0xFF + out 0x21, al + out 0xA1, al + ret diff --git a/lib/base.inc b/lib/base.inc index 394835d..ca61406 100644 --- a/lib/base.inc +++ b/lib/base.inc @@ -1,10 +1,16 @@ extern log_impl -%macro LOG 1 +%macro LOG 1-* +%rep %0 +%rotate -1 push %1 +%endrep push file call log_impl - add esp, 8 + add esp, 4 +%rep %0 + add esp, 4 +%endrep %endmacro diff --git a/lib/base/build.mk b/lib/base/build.mk new file mode 100644 index 0000000..36185ec --- /dev/null +++ b/lib/base/build.mk @@ -0,0 +1,2 @@ +LIBS += base +base_SRCS = log.s diff --git a/lib/base/log.s b/lib/base/log.s index c65a0e0..8f7fa89 100644 --- a/lib/base/log.s +++ b/lib/base/log.s @@ -56,10 +56,13 @@ log_impl: add esp, 4 %else %endif + +.loop: mov eax, [ebp + 12] push eax call putstr add esp, 4 +.end: %ifdef __KERNEL__ mov al, 0xA diff --git a/lib/build.mk b/lib/build.mk new file mode 100644 index 0000000..8ef4299 --- /dev/null +++ b/lib/build.mk @@ -0,0 +1,24 @@ +LIBS_BIN = + +define LIBS_TEMPLATE + +$(1)_BIN = lib$(1).a + +$(1)_OBJS = $$(addprefix lib/$(1)/, $$($(1)_SRCS:.s=.o)) + +$$($(1)_BIN): $$($(1)_OBJS) + ar rcs $$@ $$^ + +GARBADGE += $$($(1)_OBJS) $$($(1)_BIN) +LIBS_BIN += $$($(1)_BIN) + +endef + +LIBS = + +include lib/*/build.mk + +$(foreach lib, $(LIBS), $(eval $(call LIBS_TEMPLATE,$(lib)))) + +lib/%.o: lib/%.s + $(AS) $(ASFLAGS) -o $@ $< diff --git a/tools/build.mk b/tools/build.mk index 30b7f5b..f3e8494 100644 --- a/tools/build.mk +++ b/tools/build.mk @@ -4,6 +4,6 @@ $(1) = $$(addprefix $$(TOOLS_DIR)/, $$(shell echo -n $(1) | tr A-Z a-z)) endef -TOOLS = GIT-VERSION +TOOLS = GIT-VERSION CREATE-ISO -$(foreach tool, $(TOOLS), $(eval $(call TOOLS_TEMPLATE, $(tool)))) +$(foreach tool, $(TOOLS), $(eval $(call TOOLS_TEMPLATE,$(tool)))) diff --git a/tools/create-iso b/tools/create-iso new file mode 100755 index 0000000..a187b6d --- /dev/null +++ b/tools/create-iso @@ -0,0 +1,26 @@ +#!/bin/env sh + +grub_config=$(cat < "$2/boot/grub/grub.cfg" + sha256sum "$2"/vmstupid > "$2/boot/hashfile" + + grub-mkrescue -o $1 $2 +} + +gen_iso_file "$1" "$2"