diff --git a/Makefile b/Makefile index f7028a7..84a9b7f 100644 --- a/Makefile +++ b/Makefile @@ -1,19 +1,22 @@ # Define the object files OBJS = bin/boot.o bin/kernel.o bin/idt.o bin/isr.o bin/screen.o \ - bin/utils.o bin/string.o bin/stdio.o bin/disk.o bin/pic.o bin/pit.o \ - bin/keyboard.o bin/paging.o bin/heap.o bin/page_alloc.o bin/page_tables.o \ - bin/fat.o bin/task.o bin/switch.o bin/sys_exit.o bin/gdt.o + bin/utils.o bin/string.o bin/stdio.o bin/disk.o bin/pic.o bin/pit.o \ + bin/keyboard.o bin/paging.o bin/heap.o bin/page_alloc.o bin/page_tables.o \ + bin/fat.o bin/task.o bin/switch.o bin/sys_exit.o bin/gdt.o # Define the compiler and assembler +#CC = i686-elf-gcc -D__is_katauos CC = gcc -D__is_katauos AS = nasm +#LD = i686-elf-ld +LD = ld # Define the compiler flags -CFLAGS = -I./include/ -std=gnu99 -ffreestanding -O0 -Wall -Wextra -m32 -g +CFLAGS = -I./include/ -std=gnu99 -ffreestanding -O0 -Wall -Wextra -m32 -fno-pie -fno-common -g ASFLAGS = -f elf32 # Define the linker flags -LDFLAGS = -ffreestanding -O0 -nostdlib -m32 -g -lgcc +LDFLAGS = -m elf_i386 -L$(shell $(CC) -print-libgcc-file-name | xargs dirname) LDSCRIPT = link.ld # Define the output file @@ -28,10 +31,9 @@ create-image: parted boot.iso --script mkpart primary fat32 1MiB 100% sudo losetup /dev/loop0 boot.iso sudo losetup /dev/loop1 boot.iso -o 1048576 - #sudo mkdosfs -F32 -f 2 /dev/loop1 sudo mkfs.vfat -F32 -f 2 /dev/loop1 sudo mount /dev/loop1 /mnt - sudo grub-install --root-directory=/mnt --no-floppy --modules="normal part_msdos ext2 multiboot biosdisk" /dev/loop0 #there was biosdev also + sudo grub-install --root-directory=/mnt --no-floppy --modules="normal part_msdos ext2 multiboot biosdisk" /dev/loop0 sudo cp -r grubshit/* /mnt/ sync sudo umount /mnt @@ -49,16 +51,15 @@ image: test: qemu-system-i386 -drive file=boot.iso,media=disk,format=raw -m 4096M -d int - #qemu-system-i386 -s -S -kernel myos.bin debug: - qemu-system-i386 -s -S -kernel myos.bin + qemu-system-i386 -s -S -kernel myos.bin -monitor stdio test-bochs: bochs -f bochsrc $(OUTPUT): $(OBJS) - $(CC) -T $(LDSCRIPT) -o $@ $(OBJS) $(LDFLAGS) + $(LD) -N -T $(LDSCRIPT) -nostdlib -o $@ $(OBJS) $(LDFLAGS) bin/boot.o: src/boot.asm $(AS) $(ASFLAGS) $< -o $@ @@ -126,6 +127,6 @@ bin/gdt.o: src/kernel/gdt.c clean: rm -rf bin/ rm -f $(OUTPUT) - rm boot.iso + rm -f boot.iso .PHONY: all clean diff --git a/link.ld b/link.ld index b68a930..6e01543 100644 --- a/link.ld +++ b/link.ld @@ -1,5 +1,6 @@ -OUTPUT_FORMAT(elf32-i386) -ENTRY(start) +OUTPUT_FORMAT("elf32-i386") +OUTPUT_ARCH("i386") +ENTRY(_start) vaddr = 0xC0000000; /* virtual start address */ paddr = 0x100000; /* physical address at 1MB */ @@ -15,7 +16,7 @@ SECTIONS . += vaddr; - .text BLOCK(4K) : ALIGN(4K) { + .text : AT(ADDR(.text)-vaddr) { *(.text) } _etext = .; diff --git a/src/boot.asm b/src/boot.asm index c115b94..258c2d9 100644 --- a/src/boot.asm +++ b/src/boot.asm @@ -1,5 +1,10 @@ bits 32 +%define MULTIBOOT_PAGE_ALIGN 0x00000001 +%define MULTIBOOT_MEMORY_INFO 0x00000002 +%define MULTIBOOT_HEADER_MAGIC 0x1BADB002 +%define MULTIBOOT_HEADER_FLAGS (MULTIBOOT_PAGE_ALIGN | MULTIBOOT_MEMORY_INFO) + %define CR0_MP 0x00000002 /* bit 01 -> enable monitor coprocessor */ %define CR0_NE 0x00000020 /* bit 05 -> enable native x87 FPU mode */ %define CR0_WP 0x00010000 /* bit 16 -> enable write protect (for CoW) */ @@ -9,89 +14,85 @@ bits 32 %define PAGE_OFFSET 0xC0000000 %define GDT_BASE (0xFFFFFFFF - (PAGE_OFFSET - 1)) -section .setup +%define KERNEL_CS 0x8 +%define KERNEL_DS 0x10 + +section .setup + align 4 tmp_gdtr: - dw ((3 * 8) - 1) + dw ((3 * 8) - 1) tmp_gdta: - dd tmp_gdt + dd tmp_gdt align 4 tmp_gdt: - ; null entry - dw 0x0000 - dw 0x0000 - dw 0x0000 - dw 0x0000 + ; NULL DESCRIPTOR + dw 0x0000 + dw 0x0000 + dw 0x0000 + dw 0x0000 - ; /* KERNEL CODE */ - dw 0xFFFF ; /* segment limit 15-00 */ - dw 0x0000 ; /* base address 15-00 */ - db 0x00 ; /* base address 23-16 */ - db 0x9A ; /* P=1 DPL=00 S=1 TYPE=1010 (exec/read) */ - db 0xCF ; /* G=1 DB=1 0=0 AVL=0 SEGLIM=1111 */ - db GDT_BASE >> 24 ; /* base address 31-24 */ + ; KERNEL CODE + dw 0xFFFF ; segment limit 15-00 + dw 0x0000 ; base address 15-00 + db 0x00 ; base address 23-16 + db 0x9A ; P=1 DPL=00 S=1 TYPE=1010 (exec/read) + db 0xCF ; G=1 DB=1 0=0 AVL=0 SEGLIM=1111 + db (GDT_BASE >> 24) ; base address 31-24 (0x40) - ; /* KERNEL DATA */ - dw 0xFFFF ; /* segment limit 15-00 */ - dw 0x0000 ; /* base address 15-00 */ - db 0x00 ; /* base address 23-16 */ - db 0x92 ; /* P=1 DPL=00 S=1 TYPE=0010 (read/write) */ - db 0xCF ; /* G=1 DB=1 0=0 AVL=0 SEGLIM=1111 */ - db GDT_BASE >> 24 ; /* base address 31-24 */ + ; KERNEL DATA + dw 0xFFFF ; segment limit 15-00 + dw 0x0000 ; base address 15-00 + db 0x00 ; base address 23-16 + db 0x92 ; P=1 DPL=00 S=1 TYPE=0010 (read/write) + db 0xCF ; G=1 DB=1 0=0 AVL=0 SEGLIM=1111 + db (GDT_BASE >> 24) ; base address 31-24 (0x40) align 4 multiboot_header: - dd 0x1BADB002 - dd 0x00 - dd - (0x1BADB002 + 0x00) + dd MULTIBOOT_HEADER_MAGIC + dd MULTIBOOT_HEADER_FLAGS + dd -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS) + dd 0 ; header_addr + dd 0 ; load_addr + dd 0 ; load_end_addr + dd 0 ; bss_end_addr + dd 0 ; entry_addr + dd 0 ; mode_type + dd 0 ; width + dd 0 ; height + dd 0 ; depth - dd 0 - dd 0 - dd 0 - dd 0 - dd 0 - dd 0 - dd 0 - dd 0 - dd 0 +section .text +global _start -; section .text -global start:function (start.end - start) -extern kmain ; this function is gonna be located in our c code(kernel.c) -extern get_last_boot_addr -extern gdt_init -extern bss_init extern setup_tmp_pgdir +extern bss_init +extern gdt_init +extern get_last_boot_addr +extern kmain + +_start: + cli + lgdt [tmp_gdtr] ; load GDTR with the temporary GDT + mov cx, KERNEL_DS + mov ds, cx + mov es, cx + mov fs, cx + mov gs, cx + mov ss, cx + jmp KERNEL_CS:setup_kernel align 4 -start: -; mov esp, stack_top - cli - ; push ebx ; Сохраняем оригинальный ebx (указатель Multiboot) - - ;lgdt [gdt] - lgdt [tmp_gdtr] - - mov cx, 0x10 ; data segment - mov ds, cx - mov es, cx - mov fs, cx - mov gs, cx - mov ss, cx - jmp 0x8:.huita ; 0x8 - code segment - -.text - -align 4 -.huita: - mov esp, PAGE_OFFSET + 0x10000 ; default stack address - push 0 - popf ; reset EFLAGS +global setup_kernel +setup_kernel: + mov esp, PAGE_OFFSET + 0x10000 ; default stack address (0xC0000000 + 0x10000) + push 0 ; reset EFLAGS + popf push ebx ; save Multiboot info structure push eax ; save Multiboot magic value - call setup_tmp_pgdir mov cr3, eax @@ -101,22 +102,18 @@ align 4 or eax, 0x40000 ; CR0_AM ; enable AM or eax, 0x10000 ; CR0_WP ; enable WP or eax, 0x20 ; CR0_NE ; enable NE - or eax, 0x2 ; CR0_MP ; enable MP + or eax, 0x02 ; CR0_MP ; enable MP mov cr0, eax call bss_init ; initialize BSS segment - call gdt_init ; setup and load definitive GDT + call gdt_init ; setup and load the definitive GDT call get_last_boot_addr - pop ecx ; restore Multiboot magic value pop ebx ; restore Multiboot info structure - push eax ; save last boot address + push eax ; save the last boot address push ebx ; save Multiboot info structure push ecx ; save Multiboot magic value - - pop ebx ; Восстанавливаем оригинальный ebx - push ebx ; Передаем его в kmain call kmain - hlt -.end: + + jmp $ diff --git a/src/drivers/fat.c b/src/drivers/fat.c index fde4613..b798523 100644 --- a/src/drivers/fat.c +++ b/src/drivers/fat.c @@ -149,9 +149,9 @@ static void fmt_put(char** buf, char* end, char c) } //------------------------------------------------------------------------------ -static void fmt_put_num(long long val, int flags, int width, char** buf, char* end) +static void fmt_put_num(long val, int flags, int width, char** buf, char* end) { - unsigned long long uval = (unsigned long long)val; + unsigned long uval = (unsigned long)val; int size = 0; char tmp[65]; // Binary digits in uint64_t plus sign @@ -162,7 +162,7 @@ static void fmt_put_num(long long val, int flags, int width, char** buf, char* e if (!(flags & FMT_UNSIGNED) && val < 0) { - uval = (unsigned long long)-val; + uval = (unsigned long)-val; if (!(flags & FMT_NO_SIGN)) sign = '-'; }