diff --git a/.clang-format b/.clang-format index 77127bc..a4b01d1 100644 --- a/.clang-format +++ b/.clang-format @@ -1,5 +1,5 @@ IndentWidth: 4 -ColumnLimit: 100 +ColumnLimit: 120 # Pointer formatting DerivePointerAlignment: false diff --git a/makefile b/makefile index fcaad29..7feef78 100644 --- a/makefile +++ b/makefile @@ -2,11 +2,11 @@ CC = x86_64-elf-gcc LD = x86_64-elf-ld AS = nasm -CFLAGS = -m64 -ffreestanding -fno-builtin -Wall -Wextra -Wshadow -std=c23 -g +CFLAGS = -m64 -ffreestanding -fno-builtin -Wall -Wextra -Wshadow -std=c11 -g LDFLAGS = -g ASFLAGS = -f elf64 -g -F dwarf -SRC_C := src/kernel.c src/string.c src/vga.c src/interrupts.c src/keyboard.c +SRC_C := src/kernel.c src/mem.c src/pmm.c src/vmm.c src/string.c src/vga.c src/interrupts.c src/keyboard.c SRC_ASM := src/start.asm OBJ_C := $(SRC_C:src/%.c=.build/%.o) diff --git a/src/interrupts.c b/src/interrupts.c index 815e006..1de19ce 100644 --- a/src/interrupts.c +++ b/src/interrupts.c @@ -86,8 +86,7 @@ void disable_pic() void handle_exception(const isr_frame_t* frame) { - kprintf("exception[%d]: %s, error code: %d\n", frame->int_no, exception_messages[frame->int_no], - frame->err_code); + kprintf("exception[%d]: %s, error code: %d\n", frame->int_no, exception_messages[frame->int_no], frame->err_code); kpanic(); } diff --git a/src/kernel.c b/src/kernel.c index e25d008..e7b1e13 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -1,34 +1,21 @@ #include "kernel.h" #include "interrupts.h" -#include "keyboard.h" #include "multiboot.h" +#include "pmm.h" #include "string.h" #include "vga.h" #include -void handle_keypress(const keyboard_event_t* event) -{ - if (event->pressed && event->ascii) - { - kprintf("%c", event->ascii); - } -} - -void kmain(multiboot_info_t* multiboot_info) +void kmain(multiboot_info_t* mbd) { vga_clear(); + + init_pmm(mbd); + remap_pic(); - - if (cpu_has_apic()) - { - disable_pic(); - enable_apic(); - } - enable_interrupts(); - register_keypress_handler(handle_keypress); - kprintf("Welcome to nub OS\n"); + kprintf("Welcome to nub OS :)\n"); khalt(); } @@ -89,6 +76,16 @@ void kprintf(const char* fmt, ...) vga_put_char(buf[j], VGA_DEFAULT_COLOR); } } + else if (fmt[i] == 'x') + { + uint64_t val = va_arg(args, uint64_t); + char buf[17]; + uitoa64_hex(val, buf); + for (size_t j = 0; buf[j] != '\0'; j++) + { + vga_put_char(buf[j], VGA_DEFAULT_COLOR); + } + } else { vga_put_char(fmt[i], VGA_DEFAULT_COLOR); diff --git a/src/kernel.h b/src/kernel.h index 0bfa091..e88ab5f 100644 --- a/src/kernel.h +++ b/src/kernel.h @@ -1,13 +1,15 @@ #pragma once +#include + void kpanic(); +void kprintf(const char* fmt, ...); + static inline void khalt() { while (true) { __asm__ volatile("hlt"); } -} - -void kprintf(const char* fmt, ...); \ No newline at end of file +} \ No newline at end of file diff --git a/src/mem.c b/src/mem.c new file mode 100644 index 0000000..10cb3ea --- /dev/null +++ b/src/mem.c @@ -0,0 +1,10 @@ +#include "mem.h" +#include + +void kmemset(void* destination, uint8_t value, size_t length) +{ + for (size_t i = 0; i < length; i++) + { + ((uint8_t*)destination)[i] = value; + } +} \ No newline at end of file diff --git a/src/mem.h b/src/mem.h new file mode 100644 index 0000000..8511354 --- /dev/null +++ b/src/mem.h @@ -0,0 +1,6 @@ +#pragma once + +#include +#include + +void kmemset(void* destination, uint8_t value, size_t length); \ No newline at end of file diff --git a/src/multiboot.h b/src/multiboot.h index e13c35c..f3387d9 100644 --- a/src/multiboot.h +++ b/src/multiboot.h @@ -1,71 +1,276 @@ -#pragma once +/* multiboot.h - Multiboot header file. */ +/* Copyright (C) 1999,2003,2007,2008,2009,2010 Free Software Foundation, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL ANY + * DEVELOPER OR DISTRIBUTOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR + * IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ -#include +#ifndef MULTIBOOT_HEADER +#define MULTIBOOT_HEADER 1 -#define MULTIBOOT_FLAG_MEMIFO 0b0000000000001 -#define MULTIBOOT_FLAG_BOOT_DEVICE 0b0000000000010 -#define MULTIBOOT_FLAG_CMDLINE 0b0000000000100 -#define MULTIBOOT_FLAG_MODINFO 0b0000000001000 -#define MULTIBOOT_FLAG_MMAP 0b0000001000000 -#define MULTIBOOT_FLAG_DRIVES 0b0000010000000 -#define MULTIBOOT_FLAG_CONFIG_TABLE 0b0000100000000 -#define MULTIBOOT_FLAG_BOOTLOADER_NAME 0b0001000000000 -#define MULTIBOOT_FLAG_APM_TABLE 0b0010000000000 -#define MULTIBOOT_FLAG_VBEINFO 0b0100000000000 -#define MULTIBOOT_FLAG_FRAMEBUFFER_INFO 0b1000000000000 +/* How many bytes from the start of the file we search for the header. */ +#define MULTIBOOT_SEARCH 8192 +#define MULTIBOOT_HEADER_ALIGN 4 -typedef struct multiboot_info +/* The magic field should contain this. */ +#define MULTIBOOT_HEADER_MAGIC 0x1BADB002 + +/* This should be in %eax. */ +#define MULTIBOOT_BOOTLOADER_MAGIC 0x2BADB002 + +/* Alignment of multiboot modules. */ +#define MULTIBOOT_MOD_ALIGN 0x00001000 + +/* Alignment of the multiboot info structure. */ +#define MULTIBOOT_INFO_ALIGN 0x00000004 + +/* Flags set in the ’flags’ member of the multiboot header. */ + +/* Align all boot modules on i386 page (4KB) boundaries. */ +#define MULTIBOOT_PAGE_ALIGN 0x00000001 + +/* Must pass memory information to OS. */ +#define MULTIBOOT_MEMORY_INFO 0x00000002 + +/* Must pass video information to OS. */ +#define MULTIBOOT_VIDEO_MODE 0x00000004 + +/* This flag indicates the use of the address fields in the header. */ +#define MULTIBOOT_AOUT_KLUDGE 0x00010000 + +/* Flags to be set in the ’flags’ member of the multiboot info structure. */ + +/* is there basic lower/upper memory information? */ +#define MULTIBOOT_INFO_MEMORY 0x00000001 +/* is there a boot device set? */ +#define MULTIBOOT_INFO_BOOTDEV 0x00000002 +/* is the command-line defined? */ +#define MULTIBOOT_INFO_CMDLINE 0x00000004 +/* are there modules to do something with? */ +#define MULTIBOOT_INFO_MODS 0x00000008 + +/* These next two are mutually exclusive */ + +/* is there a symbol table loaded? */ +#define MULTIBOOT_INFO_AOUT_SYMS 0x00000010 +/* is there an ELF section header table? */ +#define MULTIBOOT_INFO_ELF_SHDR 0X00000020 + +/* is there a full memory map? */ +#define MULTIBOOT_INFO_MEM_MAP 0x00000040 + +/* Is there drive info? */ +#define MULTIBOOT_INFO_DRIVE_INFO 0x00000080 + +/* Is there a config table? */ +#define MULTIBOOT_INFO_CONFIG_TABLE 0x00000100 + +/* Is there a boot loader name? */ +#define MULTIBOOT_INFO_BOOT_LOADER_NAME 0x00000200 + +/* Is there a APM table? */ +#define MULTIBOOT_INFO_APM_TABLE 0x00000400 + +/* Is there video information? */ +#define MULTIBOOT_INFO_VBE_INFO 0x00000800 +#define MULTIBOOT_INFO_FRAMEBUFFER_INFO 0x00001000 + +#ifndef ASM_FILE + +typedef unsigned char multiboot_uint8_t; +typedef unsigned short multiboot_uint16_t; +typedef unsigned int multiboot_uint32_t; +typedef unsigned long long multiboot_uint64_t; + +struct multiboot_header { - uint32_t flags; // required + /* Must be MULTIBOOT_MAGIC - see above. */ + multiboot_uint32_t magic; - // Available if flags[0] is set - uint32_t mem_lower; - uint32_t mem_upper; + /* Feature flags. */ + multiboot_uint32_t flags; - // Available if flags[1] is set - uint32_t boot_device; + /* The above fields plus this one must equal 0 mod 2^32. */ + multiboot_uint32_t checksum; - // Available if flags[2] is set - uint32_t cmdline; + /* These are only valid if MULTIBOOT_AOUT_KLUDGE is set. */ + multiboot_uint32_t header_addr; + multiboot_uint32_t load_addr; + multiboot_uint32_t load_end_addr; + multiboot_uint32_t bss_end_addr; + multiboot_uint32_t entry_addr; - // Available if flags[3] is set - uint32_t mods_count; - uint32_t mods_addr; + /* These are only valid if MULTIBOOT_VIDEO_MODE is set. */ + multiboot_uint32_t mode_type; + multiboot_uint32_t width; + multiboot_uint32_t height; + multiboot_uint32_t depth; +}; - // Available if flags[4] or flags[5] is set - uint8_t syms[16]; // 28 - 40 bytes, exact layout may differ depending on a.out or ELF +/* The symbol table for a.out. */ +struct multiboot_aout_symbol_table +{ + multiboot_uint32_t tabsize; + multiboot_uint32_t strsize; + multiboot_uint32_t addr; + multiboot_uint32_t reserved; +}; +typedef struct multiboot_aout_symbol_table multiboot_aout_symbol_table_t; - // Available if flags[6] is set - uint32_t mmap_length; - uint32_t mmap_addr; +/* The section header table for ELF. */ +struct multiboot_elf_section_header_table +{ + multiboot_uint32_t num; + multiboot_uint32_t size; + multiboot_uint32_t addr; + multiboot_uint32_t shndx; +}; +typedef struct multiboot_elf_section_header_table multiboot_elf_section_header_table_t; - // Available if flags[7] is set - uint32_t drives_length; - uint32_t drives_addr; +struct multiboot_info +{ + /* Multiboot info version number */ + multiboot_uint32_t flags; - // Available if flags[8] is set - uint32_t config_table; + /* Available memory from BIOS */ + multiboot_uint32_t mem_lower; + multiboot_uint32_t mem_upper; - // Available if flags[9] is set - uint32_t boot_loader_name; + /* "root" partition */ + multiboot_uint32_t boot_device; - // Available if flags[10] is set - uint32_t apm_table; + /* Kernel command line */ + multiboot_uint32_t cmdline; - // Available if flags[11] is set - uint32_t vbe_control_info; - uint32_t vbe_mode_info; - uint16_t vbe_mode; - uint16_t vbe_interface_seg; - uint16_t vbe_interface_off; - uint16_t vbe_interface_len; + /* Boot-Module list */ + multiboot_uint32_t mods_count; + multiboot_uint32_t mods_addr; - // Available if flags[12] is set - uint64_t framebuffer_addr; // 64-bit for large memory addresses - uint32_t framebuffer_pitch; - uint32_t framebuffer_width; - uint32_t framebuffer_height; - uint8_t framebuffer_bpp; - uint8_t framebuffer_type; - uint8_t color_info[6]; // 110-115 bytes -} __attribute__((packed)) multiboot_info_t; \ No newline at end of file + union + { + multiboot_aout_symbol_table_t aout_sym; + multiboot_elf_section_header_table_t elf_sec; + } u; + + /* Memory Mapping buffer */ + multiboot_uint32_t mmap_length; + multiboot_uint32_t mmap_addr; + + /* Drive Info buffer */ + multiboot_uint32_t drives_length; + multiboot_uint32_t drives_addr; + + /* ROM configuration table */ + multiboot_uint32_t config_table; + + /* Boot Loader Name */ + multiboot_uint32_t boot_loader_name; + + /* APM table */ + multiboot_uint32_t apm_table; + + /* Video */ + multiboot_uint32_t vbe_control_info; + multiboot_uint32_t vbe_mode_info; + multiboot_uint16_t vbe_mode; + multiboot_uint16_t vbe_interface_seg; + multiboot_uint16_t vbe_interface_off; + multiboot_uint16_t vbe_interface_len; + + multiboot_uint64_t framebuffer_addr; + multiboot_uint32_t framebuffer_pitch; + multiboot_uint32_t framebuffer_width; + multiboot_uint32_t framebuffer_height; + multiboot_uint8_t framebuffer_bpp; +#define MULTIBOOT_FRAMEBUFFER_TYPE_INDEXED 0 +#define MULTIBOOT_FRAMEBUFFER_TYPE_RGB 1 +#define MULTIBOOT_FRAMEBUFFER_TYPE_EGA_TEXT 2 + multiboot_uint8_t framebuffer_type; + + union + { + struct + { + multiboot_uint32_t framebuffer_palette_addr; + multiboot_uint16_t framebuffer_palette_num_colors; + }; + + struct + { + multiboot_uint8_t framebuffer_red_field_position; + multiboot_uint8_t framebuffer_red_mask_size; + multiboot_uint8_t framebuffer_green_field_position; + multiboot_uint8_t framebuffer_green_mask_size; + multiboot_uint8_t framebuffer_blue_field_position; + multiboot_uint8_t framebuffer_blue_mask_size; + }; + }; +}; +typedef struct multiboot_info multiboot_info_t; + +struct multiboot_color +{ + multiboot_uint8_t red; + multiboot_uint8_t green; + multiboot_uint8_t blue; +}; + +struct multiboot_mmap_entry +{ + multiboot_uint32_t size; + multiboot_uint64_t addr; + multiboot_uint64_t len; +#define MULTIBOOT_MEMORY_AVAILABLE 1 +#define MULTIBOOT_MEMORY_RESERVED 2 +#define MULTIBOOT_MEMORY_ACPI_RECLAIMABLE 3 +#define MULTIBOOT_MEMORY_NVS 4 +#define MULTIBOOT_MEMORY_BADRAM 5 + multiboot_uint32_t type; +} __attribute__((packed)); +typedef struct multiboot_mmap_entry multiboot_memory_map_t; + +struct multiboot_mod_list +{ + /* the memory used goes from bytes ’mod_start’ to ’mod_end-1’ inclusive */ + multiboot_uint32_t mod_start; + multiboot_uint32_t mod_end; + + /* Module command line */ + multiboot_uint32_t cmdline; + + /* padding to take it to 16 bytes (must be zero) */ + multiboot_uint32_t pad; +}; +typedef struct multiboot_mod_list multiboot_module_t; + +/* APM BIOS info. */ +struct multiboot_apm_info +{ + multiboot_uint16_t version; + multiboot_uint16_t cseg; + multiboot_uint32_t offset; + multiboot_uint16_t cseg_16; + multiboot_uint16_t dseg; + multiboot_uint16_t flags; + multiboot_uint16_t cseg_len; + multiboot_uint16_t cseg_16_len; + multiboot_uint16_t dseg_len; +}; + +#endif /* ! ASM_FILE */ + +#endif /* ! MULTIBOOT_HEADER */ \ No newline at end of file diff --git a/src/pmm.c b/src/pmm.c new file mode 100644 index 0000000..a1bed2a --- /dev/null +++ b/src/pmm.c @@ -0,0 +1,124 @@ +#include "pmm.h" +#include "kernel.h" +#include "mem.h" +#include +#include + +#define BITMAP_SIZE 32768 // Supports up to 1GB of RAM +#define USABLE_REGION_SIZE 32 + +typedef struct +{ + uint64_t base_address; + uint64_t length; +} memory_region_t; + +static uint8_t page_bitmap[BITMAP_SIZE]; +static uint64_t total_pages = 0; +static uint64_t free_pages = 0; + +void init_pmm(multiboot_info_t* mbd) +{ + memory_region_t usable_regions[USABLE_REGION_SIZE]; + size_t num_regions = 0; + + if (!(mbd->flags & (1 << 6))) + { + kprintf("Invalid memory map given by bootloader\n"); + kpanic(); + } + + uint64_t offset = 0; + while (offset < mbd->mmap_length) + { + multiboot_memory_map_t* mmmt = (multiboot_memory_map_t*)(mbd->mmap_addr + offset); + + if (mmmt->type == MULTIBOOT_MEMORY_AVAILABLE && num_regions < USABLE_REGION_SIZE) + { + usable_regions[num_regions] = (memory_region_t){ + .base_address = mmmt->addr, + .length = mmmt->len, + }; + + num_regions++; + + kprintf("Available memory: 0x%x - 0x%x (%u KB)\n", mmmt->addr, mmmt->addr + mmmt->len, mmmt->len / 1024); + } + + offset += mmmt->size + sizeof(mmmt->size); + } + + kmemset(page_bitmap, 0xFF, BITMAP_SIZE); + + for (size_t i = 0; i < num_regions; i++) + { + memory_region_t region = usable_regions[i]; + + uint64_t start_page = region.base_address / PAGE_SIZE; + uint64_t num_pages = region.length / PAGE_SIZE; + + for (uint64_t page = start_page; page < start_page + num_pages; page++) + { + if (page < BITMAP_SIZE * 8) + { + page_bitmap[page / 8] &= ~(1 << (page % 8)); + free_pages++; + total_pages++; + } + else + { + kprintf("System has more ram than the bitmap allows!\n"); + break; + } + } + } + + // Reserve first 64MB which is reserved by boot code + for (uint64_t page = 0; page < (64 * 1024 * 1024) / PAGE_SIZE; page++) + { + if (page < BITMAP_SIZE * 8) + { + if (!(page_bitmap[page / 8] & (1 << (page % 8)))) + { + page_bitmap[page / 8] |= (1 << (page % 8)); + free_pages--; + } + } + } + + kprintf("PMM initialized\n"); +} + +uint64_t pmm_alloc_page() +{ + for (size_t i = 0; i < BITMAP_SIZE; i++) + { + if (page_bitmap[i] != 0xFF) + { + for (int bit = 0; bit < 8; bit++) + { + if (!(page_bitmap[i] & (1 << bit))) + { + page_bitmap[i] |= (1 << bit); + free_pages--; + return ((i * 8 + bit) * PAGE_SIZE); + } + } + } + } + + return 0; +} + +void pmm_free_page(uint64_t addr) +{ + uint64_t page = addr / PAGE_SIZE; + if (page < BITMAP_SIZE * 8) + { + if (page_bitmap[page / 8] & (1 << (page % 8))) + { + page_bitmap[page / 8] &= ~(1 << (page % 8)); + free_pages++; + } + } +} \ No newline at end of file diff --git a/src/pmm.h b/src/pmm.h new file mode 100644 index 0000000..500b58b --- /dev/null +++ b/src/pmm.h @@ -0,0 +1,10 @@ +#pragma once + +#include "multiboot.h" +#include + +#define PAGE_SIZE 4096 + +void init_pmm(multiboot_info_t* mbd); +uint64_t pmm_alloc_page(); +void pmm_free_page(uint64_t addr); \ No newline at end of file diff --git a/src/start.asm b/src/start.asm index 33e4715..1890481 100644 --- a/src/start.asm +++ b/src/start.asm @@ -1,6 +1,7 @@ global _start extern kmain extern handle_isr +extern pml4 %define FLAGS 0b10 %define MAGIC 0x1BADB002 diff --git a/src/string.c b/src/string.c index 9d2492d..9a9c1a4 100644 --- a/src/string.c +++ b/src/string.c @@ -98,4 +98,30 @@ void uitoa64(uint64_t value, char* buffer) buffer[j++] = temp[--i]; } buffer[j] = '\0'; +} + +void uitoa64_hex(uint64_t value, char* buffer) +{ + const char* digits = "0123456789abcdef"; + char temp[17]; + int pos = 0; + + if (value == 0) + { + buffer[0] = '0'; + buffer[1] = '\0'; + return; + } + + while (value > 0 && pos < 16) + { + temp[pos++] = digits[value & 0xF]; + value >>= 4; + } + + for (int i = 0; i < pos; i++) + { + buffer[i] = temp[pos - i - 1]; + } + buffer[pos] = '\0'; } \ No newline at end of file diff --git a/src/string.h b/src/string.h index 4bda586..4a395e8 100644 --- a/src/string.h +++ b/src/string.h @@ -6,4 +6,5 @@ int strcmp(const char* a, const char* b); void reverse(char* str, size_t length); void uitoa64(uint64_t value, char* buffer); -void itoa64(int64_t value, char* buffer); \ No newline at end of file +void itoa64(int64_t value, char* buffer); +void uitoa64_hex(uint64_t value, char* buffer); \ No newline at end of file diff --git a/src/vmm.c b/src/vmm.c new file mode 100644 index 0000000..e69de29 diff --git a/src/vmm.h b/src/vmm.h new file mode 100644 index 0000000..6f70f09 --- /dev/null +++ b/src/vmm.h @@ -0,0 +1 @@ +#pragma once