Sat, 18 Mar 2023 01:46:06 +0100
13 files changed,
141 insertions(+),
79 deletions(-)
A
create.sh
@@ -0,0 +1,18 @@
+dd if=/dev/zero of=cedos.img count=144 +parted cedos.img mklabel msdos +parted cedos.img mkpart primary FAT32 8s 128s -s + +dd if=/dev/zero of=fat.img count=128 +mkfs.fat fat.img + +mkdir -p ./mnt +sudo mount fat.img ./mnt +sudo cp ./build/release/components/kernel.bin ./mnt +sudo cp ./build/release/components/apps.o ./mnt +sudo umount ./mnt + +dd if=fat.img of=cedos.img seek=8 conv=notrunc +dd bs=1 if=./build/release/components/boot.bin of=cedos.img count=446 conv=notrunc +dd if=./build/release/components/boot.bin of=cedos.img skip=1 seek=1 count=7 conv=notrunc + +parted cedos.img print list all
M
makefile
→
makefile
@@ -60,30 +60,38 @@ $(DIRS):
> $(MKDIR) $@ .PHONY: build -build: $(GLOBAL_BUILD)/base.img $(GLOBAL_BUILD)/base.o +build: $(GLOBAL_BUILD)/cedos.img -$(GLOBAL_BUILD)/base.o: $(MODULES) -> $(LD) $(OBJECTS) -r -T link.txt -Map=$(LOG_DIR)/elf_mapfile.txt --oformat elf32-i386 -o $@ +$(GLOBAL_BUILD)/fat.img: $(MODULES) +# > $(LD) $(OBJECTS) -r -T link.txt -Map=$(LOG_DIR)/elf_mapfile.txt --oformat elf32-i386 -o $@ +> dd if=/dev/zero of=$@ count=128 +> mkfs.fat $@ +> mkdir -p ./mnt +> sudo mount $@ ./mnt +> sudo cp $(LOCAL_BUILD)/kernel.bin ./mnt +> sudo cp $(LOCAL_BUILD)/apps.o ./mnt +> sudo umount ./mnt -$(GLOBAL_BUILD)/base.img: $(MODULES) -> $(LD) $(OBJECTS) -T link.txt -Map=$(LOG_DIR)/bin_mapfile.txt --oformat binary --nostdlib -o $@ +$(GLOBAL_BUILD)/cedos.img: $(GLOBAL_BUILD)/fat.img | $(MODULES) +> dd if=/dev/zero of=$@ count=144 +> parted $@ mklabel msdos +> parted $@ mkpart primary FAT32 8s 128s -s +> dd if=$< of=$@ seek=8 conv=notrunc +> dd bs=1 if=$(LOCAL_BUILD)/boot.bin of=$@ count=446 conv=notrunc +> dd if=$(LOCAL_BUILD)/boot.bin of=$@ skip=1 seek=1 count=7 conv=notrunc +> parted $@ print list all +# > $(LD) $(OBJECTS) -T link.txt -Map=$(LOG_DIR)/bin_mapfile.txt --oformat binary --nostdlib -o $@ .PHONY: logs logs: $(LOG_DIR)/base.sym $(LOG_DIR)/objdump.txt -$(LOG_DIR)/base.sym: $(GLOBAL_BUILD)/base.o -> $(OBJCOPY) --only-keep-debug $< $@ - -$(LOG_DIR)/objdump.txt: $(GLOBAL_BUILD)/base.o -> $(OBJDUMP) -D $< > $@ - .PHONY: boot boot: -> $(MAKE) GLOBAL_BUILD=$(LOCAL_BUILD) -C src/boot $(LOCAL_BUILD)/boot.o +> $(MAKE) GLOBAL_BUILD=$(LOCAL_BUILD) -C src/boot $(LOCAL_BUILD)/boot.bin .PHONY: kernel kernel: -> $(MAKE) GLOBAL_BUILD=$(LOCAL_BUILD) -C src/kernel $(LOCAL_BUILD)/kernel.o +> $(MAKE) GLOBAL_BUILD=$(LOCAL_BUILD) -C src/kernel $(LOCAL_BUILD)/kernel.bin .PHONY: apps apps:
M
run.sh
→
run.sh
@@ -1,1 +1,1 @@
-qemu-system-i386 -drive index=0,if=floppy,format=raw,file=build/release/base.img -m 64 -monitor stdio -no-reboot -d int,cpu_reset,exec,in_asm -vnc :0 2> log/run_err.log+qemu-system-i386 -drive index=0,if=floppy,format=raw,file=./build/release/cedos.img -m 64 -monitor stdio -no-reboot -d int,cpu_reset,exec,in_asm -vnc :0 2> log/run_err.log
M
src/apps/link.txt
→
src/apps/link.txt
@@ -1,6 +1,8 @@
OUTPUT_ARCH(i386) OUTPUT_FORMAT(elf32-i386) +ENTRY(start) + MEMORY { HEADER : ORIGIN = 0x00000000, LENGTH = 0x10000000@@ -9,9 +11,8 @@ }
SECTIONS { - .text : AT(0x0000) + .text : AT(0x8600) { - */start.c.o(.*) *.*(.*) } >CODE }
M
src/apps/makefile
→
src/apps/makefile
@@ -20,9 +20,9 @@
.PHONY: build build: $(GLOBAL_BUILD)/apps.o $(GLOBAL_BUILD)/apps.o: $(OBJECTS) -> $(LD) -T link.txt $^ -o $(GLOBAL_BUILD)/apps.o --oformat elf32-i386 -> $(OBJCOPY) -I binary -O elf32-i386 $(GLOBAL_BUILD)/apps.o $(GLOBAL_BUILD)/apps_raw.o -> $(LD) -T link.txt -r $^ -o $(GLOBAL_BUILD)/apps.o --oformat elf32-i386 +> $(LD) -T link.txt -Map=$(LOG_DIR)/apps_mapfile.txt $^ -o $@ +# > $(OBJCOPY) -I binary -O elf32-i386 $(GLOBAL_BUILD)/apps.o $(GLOBAL_BUILD)/apps_raw.o +# > $(LD) -T link.txt -r $^ -o $(GLOBAL_BUILD)/apps.o --oformat elf32-i386 $(LOCAL_BUILD)/%.s.o: %.s
M
src/boot/entry.s
→
src/boot/entry.s
@@ -44,8 +44,50 @@ lea bootdriv_id, %si
movb (%si), %dl int $0x13 - jc error + jnc bl_loaded + + # auxiliary functions +error: + movw $fail_msg, %si + call print + +error_loop: + jmp error_loop + # ############################################ + # print + # prints out a string on the screen + # %si: points to the message to be printed + # ############################################ +print: + movw $0x0000, %bx + movb $0x0E, %ah + +print_loop: + lodsb + or %al, %al + jz print_end + int $0x10 + jmp print_loop +print_end: + ret + +print_done: + mov $done_msg, %si + jmp print + +print_fail: + mov $fail_msg, %si + jmp print + + +# this string needs to stay outside of data section +# because it is used before the data section is loaded +load_bl_msg: + .ascii "Loading bootloader..." + .byte 0 + +.section .text bl_loaded: mov $done_msg, %si call print@@ -272,50 +314,6 @@ # jmp error_loop
ljmp $0x8, $protected - - # auxiliary functions -error: - movw $fail_msg, %si - call print - -error_loop: - jmp error_loop - - # ############################################ - # print - # prints out a string on the screen - # %si: points to the message to be printed - # ############################################ -print: - movw $0x0000, %bx - movb $0x0E, %ah - -print_loop: - lodsb - or %al, %al - jz print_end - int $0x10 - jmp print_loop -print_end: - ret - -print_done: - mov $done_msg, %si - jmp print - -print_fail: - mov $fail_msg, %si - jmp print - - -# this string needs to stay outside of data section -# because it is used before the data section is loaded -load_bl_msg: - .ascii "Loading bootloader..." - .byte 0 - -.section .text - # ############################################ # checkA20 # checks if A20 line is enabled@@ -362,6 +360,8 @@ mov %eax, %es
mov %eax, %fs mov %eax, %gs mov %eax, %ss + + call ss_copy # create a page directory for the kernel that maps it to 0xC0000000 # and identity maps the first 1M of memory@@ -376,7 +376,7 @@ or $0x80000010, %eax
movl %eax, %cr0 # jump to kernel code - ljmp $8, $__KERNEL_START + ljmp $8, $0xC0000000 # loop until the heat death of the universe loop:
M
src/boot/link.txt
→
src/boot/link.txt
@@ -1,5 +1,5 @@
OUTPUT_ARCH(i386) -OUTPUT_FORMAT(elf32-i386) +OUTPUT_FORMAT(binary) PAGE_SIZE = 1 << 12;
M
src/boot/makefile
→
src/boot/makefile
@@ -18,9 +18,9 @@ $(DIRS):
> $(MKDIR) $(DIRS) .PHONY: build -build: $(GLOBAL_BUILD)/boot.o -$(GLOBAL_BUILD)/boot.o: $(OBJECTS) -> $(LD) $^ -r -T link.txt -Map=$(LOG_DIR)/boot_mapfile.txt -o $@ --oformat elf32-i386 +build: $(GLOBAL_BUILD)/boot.bin +$(GLOBAL_BUILD)/boot.bin: $(OBJECTS) +> $(LD) $^ -T link.txt -Map=$(LOG_DIR)/boot_mapfile.txt -o $@ $(LOCAL_BUILD)/%.s.o: %.s > $(AS) -o $@ $<
M
src/boot/paging.c
→
src/boot/paging.c
@@ -8,6 +8,16 @@ }
return ptr; } +void ss_copy() { + char *src = (char*)(0x14E00); + char *dest = (char*)(0x40000); + int num = 0x80 * 0x200; + + for (uint32_t i = 0; i < num; i++) { + dest[i] = src[i]; + } +} + void *create_kernel_environment() { PAGE_DIR_ENTRY (*pdir)[PAGE_ENTRY_COUNT] = (void*)(0 * PAGE_SIZE); PAGE_TABLE_ENTRY (*kernel)[PAGE_ENTRY_COUNT] = (void*)(1 * PAGE_SIZE);@@ -20,10 +30,10 @@ // map page directory to itself
(*pdir)[PAGE_ENTRY_COUNT - 1].entry = MAKE_PAGE_ENTRY(pdir, 0b000000000011); // map 4M of kernel to 0xC0000000 - (*pdir)[PAGE_DIR_INDEX(KERNEL_VMA)].entry = MAKE_PAGE_ENTRY(kernel, 0b000000000011); + (*pdir)[PAGE_DIR_INDEX(0xC0000000)].entry = MAKE_PAGE_ENTRY(kernel, 0b000000000011); for (uint32_t i = 0; i < 1 << 10; i++) { - (*kernel)[i].entry = MAKE_PAGE_ENTRY(0x10000 + PAGE_SIZE * i, 0b000000000011); + (*kernel)[i].entry = MAKE_PAGE_ENTRY(0x40000 + PAGE_SIZE * i, 0b000000000011); } // identity map first 4M of memory
A
src/kernel/link.txt
@@ -0,0 +1,25 @@
+OUTPUT_ARCH(i386) +OUTPUT_FORMAT(binary) + +PAGE_SIZE = 1 << 12; + +MEMORY +{ + KERNEL_VMA : ORIGIN = 0xC0000000, LENGTH = 0x30000000 +} + +SECTIONS +{ + KERNEL : AT(0x4E00) + { + */*(.text) + */*(.data) + */*(.*) + } >KERNEL_VMA +} + +__KERNEL_LMA = LOADADDR(KERNEL); +__KERNEL_VMA = ADDR(KERNEL); +__KERNEL_SIZE = SIZEOF(KERNEL); + +__KERNEL_STACK_ADDR = 0xC0400000;
M
src/kernel/main.c
→
src/kernel/main.c
@@ -138,11 +138,11 @@ // create test tasks
printk("Creating tasks.\n"); printk("Loading ELF executable.\n"); - VIRT_ADDR addr = (uint32_t)(KERNEL_VMA) + (uint32_t)(ELF_LMA - KERNEL_LMA); + VIRT_ADDR addr = (uint32_t)(KERNEL_VMA) + (uint32_t)(0x8600 - (int)KERNEL_LMA); printk("ELF address: %p\n", addr); - elf_exec(addr, ELF_SIZE, "app1", "Hello World!"); - elf_exec(addr, ELF_SIZE, "app2", "Hello World!"); - elf_exec(addr, ELF_SIZE, "app3", "Hello World!"); + elf_exec(addr, 0x1000, "app1", "Hello World!"); + elf_exec(addr, 0x1000, "app2", "Hello World!"); + elf_exec(addr, 0x1000, "app3", "Hello World!"); printk("Starting scheduler.\n"); sched_start();
M
src/kernel/makefile
→
src/kernel/makefile
@@ -19,8 +19,8 @@ > $(MKDIR) $(DIRS)
.PHONY: build build: folder -$(GLOBAL_BUILD)/kernel.o: $(OBJECTS) | folder -> $(LD) $^ -r -o $@ --oformat elf32-i386 +$(GLOBAL_BUILD)/kernel.bin: $(OBJECTS) | folder +> $(LD) $^ -o $@ -T link.txt -Map=$(LOG_DIR)/kernel_mapfile.txt .PHONY: folder folder: