From 50dc3ab1c7cc52bf7e284c749dc05d310d3c5410 Mon Sep 17 00:00:00 2001 From: ice-bit Date: Mon, 17 Aug 2020 17:57:06 +0200 Subject: [PATCH] Improved kernel's loading function --- kernel/cpu/Makefile | 4 +- kernel/cpu/kernel_loader.asm | 39 ++++++++---- kernel/cpu/multiboot.asm | 20 ------ kernel/kernel_main.c | 3 +- kernel/libc/multiboot.h | 117 ----------------------------------- 5 files changed, 30 insertions(+), 153 deletions(-) delete mode 100644 kernel/cpu/multiboot.asm delete mode 100644 kernel/libc/multiboot.h diff --git a/kernel/cpu/Makefile b/kernel/cpu/Makefile index c563e23..4098b6a 100644 --- a/kernel/cpu/Makefile +++ b/kernel/cpu/Makefile @@ -1,8 +1,8 @@ -OBJS = multiboot.asm.o kernel_loader.asm.o ports.asm.o \ +OBJS = kernel_loader.asm.o ports.asm.o \ gdt.asm.o idt.asm.o interrupts.asm.o ASM = nasm ASMFLAGS = -f elf all: $(OBJS) %.asm.o: %.asm - $(ASM) $(ASMFLAGS) $< -o $@ \ No newline at end of file + $(ASM) $(ASMFLAGS) $< -o $@ diff --git a/kernel/cpu/kernel_loader.asm b/kernel/cpu/kernel_loader.asm index 2ad44d6..c8153ef 100644 --- a/kernel/cpu/kernel_loader.asm +++ b/kernel/cpu/kernel_loader.asm @@ -1,28 +1,43 @@ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; iceOS Kernel ; ; Developed by Marco 'icebit' Cetica ; -; (c) 2019 ; +; (c) 2019-2020 ; ; Released under GPLv3 ; ; https://github.com/ice-bit/iceOS ; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -global kernel_loader -extern kernel_main +[BITS 32] ; We should be in protected mode +section .multiboot + +head_s: + dd 0xE85250D6 ; Multiboot header magic number + dd 0 ; Protected mode flag + dd head_e - head_s ; Header length + dd 0x100000000 - (0xE85250D6 + 0 + (head_e - head_s)) ; Checksum of above + + ; Other flags + dw 0 ; type + dw 0 ; flags + dd 0 ; size + +head_e: + +GLOBAL kernel_loader +EXTERN kernel_main section .text kernel_loader: - mov esp, kernel_stack + KERNEL_STACK_SZ ; Stack pointer - push ebx - call kernel_main ; jump to kernel's main function + mov esp, kernel_stack + KERNEL_STACK_SZ ; Define stack pointer + push ebx ; Set multiboot header + call kernel_main ; Jump into kernel's main function .loop: - jmp .loop ; endless loop + jmp .loop ; If the kernel returns, go into an infinite loop. + ; This will prevent the CPU to run non-kernel instructions + ; from the memory -KERNEL_STACK_SZ equ 4096 ; 4 KB for the stack +KERNEL_STACK_SZ equ 4096 ; Stack size(4KiB) section .bss align 4 kernel_stack: - resb KERNEL_STACK_SZ ; Reserver 4 KB - - - + resb KERNEL_STACK_SZ ; Reserve 4 KiB diff --git a/kernel/cpu/multiboot.asm b/kernel/cpu/multiboot.asm deleted file mode 100644 index 3344761..0000000 --- a/kernel/cpu/multiboot.asm +++ /dev/null @@ -1,20 +0,0 @@ -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -; iceOS Kernel ; -; Developed by Marco 'icebit' Cetica ; -; (c) 2019 ; -; Released under GPLv3 ; -; https://github.com/ice-bit/iceOS ; -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; - -section .multiboot -head_s: - dd 0xe85250d6 ; Magic number - dd 0 ; Code for protected mode - dd head_e - head_s ; Header length - dd 0x100000000 - (0xe85250d6 + 0 + (head_e - head_s)) ; Checksum of above - - ; Various flags - dw 0 ; type - dw 0 ; flags - dd 8 ; size -head_e: \ No newline at end of file diff --git a/kernel/kernel_main.c b/kernel/kernel_main.c index 5b09a48..659628c 100644 --- a/kernel/kernel_main.c +++ b/kernel/kernel_main.c @@ -14,7 +14,6 @@ #include "mem/kheap.h" #include "shell/shell.h" #include "libc/stdio.h" -#include "libc/multiboot.h" #include "libc/assert.h" #include @@ -59,4 +58,4 @@ void kernel_main() { iceos_ascii_logo(); init_prompt(); // Initialize frame buffer -} \ No newline at end of file +} diff --git a/kernel/libc/multiboot.h b/kernel/libc/multiboot.h deleted file mode 100644 index 4c8105d..0000000 --- a/kernel/libc/multiboot.h +++ /dev/null @@ -1,117 +0,0 @@ - -/* multiboot.h - the header for Multiboot */ -/* Copyright (C) 1999, 2001 Free Software Foundation, Inc. - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ - -/* Macros. */ - -/* The magic number for the Multiboot header. */ -#define MULTIBOOT_HEADER_MAGIC 0x1BADB002 - -/* The flags for the Multiboot header. */ -#ifdef __ELF__ -# define MULTIBOOT_HEADER_FLAGS 0x00000003 -#else -# define MULTIBOOT_HEADER_FLAGS 0x00010003 -#endif - -/* The magic number passed by a Multiboot-compliant boot loader. */ -#define MULTIBOOT_BOOTLOADER_MAGIC 0x2BADB002 - -/* The size of our stack (16KB). */ -#define STACK_SIZE 0x4000 - -/* C symbol format. HAVE_ASM_USCORE is defined by configure. */ -#ifdef HAVE_ASM_USCORE -# define EXT_C(sym) _ ## sym -#else -# define EXT_C(sym) sym -#endif - -#ifndef ASM -/* Do not include here in boot.S. */ - -/* Types. */ - -/* The Multiboot header. */ -typedef struct multiboot_header -{ - unsigned long magic; - unsigned long flags; - unsigned long checksum; - unsigned long header_addr; - unsigned long load_addr; - unsigned long load_end_addr; - unsigned long bss_end_addr; - unsigned long entry_addr; -} multiboot_header_t; - -/* The symbol table for a.out. */ -typedef struct aout_symbol_table -{ - unsigned long tabsize; - unsigned long strsize; - unsigned long addr; - unsigned long reserved; -} aout_symbol_table_t; - -/* The section header table for ELF. */ -typedef struct elf_section_header_table -{ - unsigned long num; - unsigned long size; - unsigned long addr; - unsigned long shndx; -} elf_section_header_table_t; - -/* The Multiboot information. */ -typedef struct multiboot_info -{ - unsigned long flags; - unsigned long mem_lower; - unsigned long mem_upper; - unsigned long boot_device; - unsigned long cmdline; - unsigned long mods_count; - unsigned long mods_addr; - union - { - aout_symbol_table_t aout_sym; - elf_section_header_table_t elf_sec; - } u; - unsigned long mmap_length; - unsigned long mmap_addr; -} multiboot_info_t; - -/* The module structure. */ -typedef struct module -{ - unsigned long mod_start; - unsigned long mod_end; - unsigned long string; - unsigned long reserved; -} module_t; - -/* The memory map. Be careful that the offset 0 is base_addr_low - but no size. */ -typedef struct memory_map -{ - unsigned long size; - unsigned long base_addr_low; - unsigned long base_addr_high; - unsigned long length_low; - unsigned long length_high; - unsigned long type; -} memory_map_t; - -#endif /* ! ASM */ \ No newline at end of file