From 2461cd921d45e3d75245568bc59831c1f0a43331 Mon Sep 17 00:00:00 2001 From: EmulatedSeasons <89668582+EmulatedSeasons@users.noreply.github.com> Date: Fri, 3 May 2024 01:02:21 -0400 Subject: changed some makefile variables to export --- makefile | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'makefile') diff --git a/makefile b/makefile index 8ec9152..ccd252c 100644 --- a/makefile +++ b/makefile @@ -2,13 +2,13 @@ OS_NAME := emuos ARCH = i386 -SYSROOT = $(PWD)/sysroot +export SYSROOT = $(PWD)/sysroot # Variables for easy access of tools like gcc and nasm -CC = i686-elf-gcc -CXX = i686-elf-g++ -LD = i686-elf-ld -NASM = nasm +export CC = i686-elf-gcc +export CXX = i686-elf-g++ +export LD = i686-elf-ld +export NASM = nasm QEMU = qemu-system-i386 ASMFLAGS = -felf32 #CXXFLAGS := -ffreestanding -O2 -Wall -Wextra -fno-exceptions -fno-rtti @@ -20,11 +20,11 @@ all: libck.a kernel.bin grub libck.a: $(info [INFO] Building libck) - $(MAKE) -C ./libs/libck/ ARCH=$(ARCH) PREFIX=$(PWD) CC=$(CC) CXX=$(CXX) LD=$(LD) NASM=$(NASM) SYSROOT=$(SYSROOT) + $(MAKE) -C ./libs/libck/ ARCH=$(ARCH) PREFIX=$(PWD) kernel.bin: $(info [INFO] Building kernel) - $(MAKE) -C ./kernel/ ARCH=$(ARCH) PREFIX=$(PWD) CC=$(CC) CXX=$(CXX) LD=$(LD) NASM=$(NASM) SYSROOT=$(SYSROOT) + $(MAKE) -C ./kernel/ ARCH=$(ARCH) PREFIX=$(PWD) grub: kernel.bin grub.cfg grub-file --is-x86-multiboot $< @@ -38,13 +38,13 @@ qemu: grub install: install-headers install-libraries install-headers: - $(MAKE) -C ./kernel/ install-headers SYSROOT=$(SYSROOT) - $(MAKE) -C ./libs/libck/ install-headers SYSROOT=$(SYSROOT) + $(MAKE) -C ./kernel/ install-headers + $(MAKE) -C ./libs/libck/ install-headers install-libraries: - $(MAKE) -C ./libs/libck/ install-lib SYSROOT=$(SYSROOT) + $(MAKE) -C ./libs/libck/ install-lib clean: - -@$(MAKE) -C ./kernel/ clean SYSROOT=$(SYSROOT) - -@$(MAKE) -C ./libs/libck/ clean SYSROOT=$(SYSROOT) + -@$(MAKE) -C ./kernel/ clean + -@$(MAKE) -C ./libs/libck/ clean -@$(RM) $(wildcard *.bin *.a) -- cgit v1.2.3-70-g09d2 From 2d59766eb330b9d15a9bcbcd7e1695809fc099c5 Mon Sep 17 00:00:00 2001 From: EmulatedSeasons <89668582+EmulatedSeasons@users.noreply.github.com> Date: Fri, 3 May 2024 03:39:47 -0400 Subject: renamed i386 to x86 --- kernel/arch/i386/boot.asm | 103 ------------ kernel/arch/i386/crti.asm | 12 -- kernel/arch/i386/crtn.asm | 9 - kernel/arch/i386/gdt.asm | 35 ---- kernel/arch/i386/gdt2.cpp | 51 ------ kernel/arch/i386/idt.cpp | 133 --------------- kernel/arch/i386/isr.asm | 324 ------------------------------------ kernel/arch/i386/isr_handler.cpp | 21 --- kernel/arch/i386/linker.ld | 38 ----- kernel/arch/i386/multiboot.cpp | 30 ---- kernel/arch/i386/multiboot.h | 274 ------------------------------ kernel/arch/i386/pic.cpp | 97 ----------- kernel/arch/i386/ps2_controller.cpp | 191 --------------------- kernel/arch/i386/tmtty.cpp | 78 --------- kernel/arch/i386/vga.h | 35 ---- kernel/arch/x86/boot.asm | 103 ++++++++++++ kernel/arch/x86/crti.asm | 12 ++ kernel/arch/x86/crtn.asm | 9 + kernel/arch/x86/gdt.asm | 35 ++++ kernel/arch/x86/gdt2.cpp | 51 ++++++ kernel/arch/x86/idt.cpp | 133 +++++++++++++++ kernel/arch/x86/isr.asm | 324 ++++++++++++++++++++++++++++++++++++ kernel/arch/x86/isr_handler.cpp | 21 +++ kernel/arch/x86/linker.ld | 38 +++++ kernel/arch/x86/multiboot.cpp | 30 ++++ kernel/arch/x86/multiboot.h | 274 ++++++++++++++++++++++++++++++ kernel/arch/x86/pic.cpp | 97 +++++++++++ kernel/arch/x86/ps2_controller.cpp | 191 +++++++++++++++++++++ kernel/arch/x86/tmtty.cpp | 78 +++++++++ kernel/arch/x86/vga.h | 35 ++++ kernel/makefile | 7 - makefile | 12 +- 32 files changed, 1437 insertions(+), 1444 deletions(-) delete mode 100644 kernel/arch/i386/boot.asm delete mode 100644 kernel/arch/i386/crti.asm delete mode 100644 kernel/arch/i386/crtn.asm delete mode 100644 kernel/arch/i386/gdt.asm delete mode 100644 kernel/arch/i386/gdt2.cpp delete mode 100644 kernel/arch/i386/idt.cpp delete mode 100644 kernel/arch/i386/isr.asm delete mode 100644 kernel/arch/i386/isr_handler.cpp delete mode 100644 kernel/arch/i386/linker.ld delete mode 100644 kernel/arch/i386/multiboot.cpp delete mode 100644 kernel/arch/i386/multiboot.h delete mode 100644 kernel/arch/i386/pic.cpp delete mode 100644 kernel/arch/i386/ps2_controller.cpp delete mode 100644 kernel/arch/i386/tmtty.cpp delete mode 100644 kernel/arch/i386/vga.h create mode 100644 kernel/arch/x86/boot.asm create mode 100644 kernel/arch/x86/crti.asm create mode 100644 kernel/arch/x86/crtn.asm create mode 100644 kernel/arch/x86/gdt.asm create mode 100644 kernel/arch/x86/gdt2.cpp create mode 100644 kernel/arch/x86/idt.cpp create mode 100644 kernel/arch/x86/isr.asm create mode 100644 kernel/arch/x86/isr_handler.cpp create mode 100644 kernel/arch/x86/linker.ld create mode 100644 kernel/arch/x86/multiboot.cpp create mode 100644 kernel/arch/x86/multiboot.h create mode 100644 kernel/arch/x86/pic.cpp create mode 100644 kernel/arch/x86/ps2_controller.cpp create mode 100644 kernel/arch/x86/tmtty.cpp create mode 100644 kernel/arch/x86/vga.h (limited to 'makefile') diff --git a/kernel/arch/i386/boot.asm b/kernel/arch/i386/boot.asm deleted file mode 100644 index 42c11e6..0000000 --- a/kernel/arch/i386/boot.asm +++ /dev/null @@ -1,103 +0,0 @@ -; multiboot header constants -MBALIGN equ 1 << 0 -MEMINFO equ 1 << 1 -MBFLAGS equ MBALIGN | MEMINFO -MAGIC equ 0x1BADB002 -CHECKSUM equ -(MAGIC+MBFLAGS) - -; multiboot header -section .multiboot -align 4 - dd MAGIC - dd MBFLAGS - dd CHECKSUM - -section .data -%include "arch/i386/gdt.asm" - -; defines the stack -section .bss -align 16 -stack_bottom: -resb 16384 ; 16 KiB -stack_top: - -; temporary paging tables -align 4 -boot_page_directory: - resb 4096 -boot_page_table: - resb 4096 - - -section .text -global _start:function (_start.end - _start) -_start: - cli - ; setup stack - mov esp, stack_top - - ; push the multiboot header data into the stack for later use - push eax - push ebx - - ; get multiboot header data (for memory map) - ;extern multiboot_main - ;call multiboot_main - - ;extern _init - ;Scall _init - - lgdt [gdt_descriptor] - jmp 0x08:.gdt_jmp - -.gdt_jmp: - mov ax, DATA_SEG - mov ds, ax - mov es, ax - mov fs, ax - mov gs, ax - mov ss, ax - jmp .gdt_jmp2 - -.gdt_jmp2: - ; setup paging (to map kernel to the higher half) - ;mov eax, 0 - ;mov cr3, eax - ;mov eax, cr0 - ;or eax, 0x80000001 - ;mov cr0, eax - - ; setup idt - extern idt_setup - call idt_setup - extern idtr - lidt [idtr] - - ; initialize pic - push ebp - mov ebp, esp - mov ebx, 0x28 - push ebx - mov ebx, 0x20 - push ebx - - extern pic_remap - call pic_remap - - pop ebx - pop ebx - mov esp, ebp - pop ebp - - sti - - ; enter the kernel - extern kernel_main - call kernel_main - - ; indefinite loop once system exits - ;cli -.hang: hlt - jmp .hang -.end: \ No newline at end of file diff --git a/kernel/arch/i386/crti.asm b/kernel/arch/i386/crti.asm deleted file mode 100644 index 51f27d9..0000000 --- a/kernel/arch/i386/crti.asm +++ /dev/null @@ -1,12 +0,0 @@ -; x86 crti.asm -section .init -global _init:function -_init: - push ebp - mov ebp, esp - -section .fini -global _fini:function -_fini: - push ebp - mov ebp, esp \ No newline at end of file diff --git a/kernel/arch/i386/crtn.asm b/kernel/arch/i386/crtn.asm deleted file mode 100644 index 370f4ae..0000000 --- a/kernel/arch/i386/crtn.asm +++ /dev/null @@ -1,9 +0,0 @@ -; x86 crtn.asm - -section .init - pop ebp - ret - -section .fini - pop ebp - ret \ No newline at end of file diff --git a/kernel/arch/i386/gdt.asm b/kernel/arch/i386/gdt.asm deleted file mode 100644 index e3af8bd..0000000 --- a/kernel/arch/i386/gdt.asm +++ /dev/null @@ -1,35 +0,0 @@ -; gdt.asm -; GDT is defined here - -gdt_start: - -gdt_null: ; Null segment - dd 0x0 - dd 0x0 - -gdt_kcode: ; Kernel code segment. Kernel has seperate segment from users for ring level - ; flag and access byte description on osdev wiki - dw 0xffff ; low limit - dw 0x0 ;low base - db 0x0 ; mid base - db 10011010b ; order (reversed): ac, rw, dc, ex, s, privl, pr - db 11001111b ; flags then limit. order (reversed): 0, 0, size bit, granularity bit - db 0x0 ; high limit - -gdt_kdata: ; same as gdt_kcode but for the data segment - dw 0xffff ;low limit - dw 0x0 ;low base - db 0x0 ;mid base - db 10010010b ; order: same as code seg. changed for data seg - db 11001111b ; same as kcode - db 0x0 ; high limit - -gdt_end: - -gdt_descriptor: ; describes the gdt to the lgdt register - dw gdt_end - gdt_start - 1 - - dd gdt_start - -CODE_SEG equ gdt_kcode - gdt_start -DATA_SEG equ gdt_kdata - gdt_start \ No newline at end of file diff --git a/kernel/arch/i386/gdt2.cpp b/kernel/arch/i386/gdt2.cpp deleted file mode 100644 index 0acdb0e..0000000 --- a/kernel/arch/i386/gdt2.cpp +++ /dev/null @@ -1,51 +0,0 @@ -#include -#include -/* -// structure to be encoded at encodeGdtEntry() -struct GDT -{ - uint32_t base; - uint32_t limit; - uint8_t access_byte; - uint8_t flags; -}; - -GDT strnulldesc {0, 0, 0, 0}; -GDT strkernelcode {0, 0xFFFFF, 0x9A, 0xC}; -GDT strkerneldata {0, 0xFFFFF, 0x92, 0xC}; -GDT strusercode {0, 0xFFFFF, 0xFA, 0xC}; -GDT struserdata {0, 0xFFFFF, 0xF2, 0xC}; - -uint8_t* nulldesc; -uint8_t* kernelcode; -uint8_t* kerneldata; -uint8_t* usercode; -uint8_t* userdata; - - -void encodeGdtEntry(uint8_t *target, struct GDT source) -{ - // Check the limit to make sure that it can be encoded - if (source.limit > 0xFFFFF) {printf("GDT cannot encode limits larger than 0xFFFFF\n");} - - // Encode the limit - target[0] = source.limit & 0xFF; - target[1] = (source.limit >> 8) & 0xFF; - target[6] = (source.limit >> 16) & 0x0F; - - // Encode the base - target[2] = source.base & 0xFF; - target[3] = (source.base >> 8) & 0xFF; - target[4] = (source.base >> 16) & 0xFF; - target[7] = (source.base >> 24) & 0xFF; - - // Encode the access byte - target[5] = source.access_byte; - - // Encode the flags - target[6] |= (source.flags << 4); -} - -extern "C" void setupGdt() { - -} */ \ No newline at end of file diff --git a/kernel/arch/i386/idt.cpp b/kernel/arch/i386/idt.cpp deleted file mode 100644 index a8f3ff4..0000000 --- a/kernel/arch/i386/idt.cpp +++ /dev/null @@ -1,133 +0,0 @@ -#include -#include - -/* Function prototypes for the ISR functions. IDT[0] to IDT[31] are reserved for hardware exceptions */ -extern "C" void isr0(); -extern "C" void isr1(); -extern "C" void isr2(); -extern "C" void isr3(); -extern "C" void isr4(); -extern "C" void isr5(); -extern "C" void isr6(); -extern "C" void isr7(); -extern "C" void isr8(); -extern "C" void isr9(); -extern "C" void isr10(); -extern "C" void isr11(); -extern "C" void isr12(); -extern "C" void isr13(); -extern "C" void isr14(); -extern "C" void isr15(); -extern "C" void isr16(); -extern "C" void isr17(); -extern "C" void isr18(); -extern "C" void isr19(); -extern "C" void isr20(); -extern "C" void isr21(); -extern "C" void isr22(); -extern "C" void isr23(); -extern "C" void isr24(); -extern "C" void isr25(); -extern "C" void isr26(); -extern "C" void isr27(); -extern "C" void isr28(); -extern "C" void isr29(); -extern "C" void isr30(); -extern "C" void isr31(); -extern "C" void isr32(); -extern "C" void isr33(); -extern "C" void isr34(); -extern "C" void isr35(); -extern "C" void isr36(); -extern "C" void isr37(); -extern "C" void isr38(); -extern "C" void isr39(); -extern "C" void isr40(); -extern "C" void isr41(); -extern "C" void isr42(); -extern "C" void isr43(); -extern "C" void isr44(); -extern "C" void isr45(); -extern "C" void isr46(); -extern "C" void isr47(); - -struct idt_entry -{ - uint16_t offset_low; - uint16_t selector; - uint8_t always0; - uint8_t type_attributes; - uint16_t offset_high; -}__attribute__((packed)); - -struct str_idtr { - uint16_t limit; - uint32_t base; -}__attribute__((packed)); - -idt_entry idt[256]; -str_idtr idtr; - -void idt_new_entry(int num, uint32_t offset, uint16_t sel, uint8_t type_attribs) { - idt[num].offset_low = (offset & 0xFFFF); - idt[num].selector = sel; - idt[num].always0 = 0; - idt[num].type_attributes = type_attribs; - idt[num].offset_high = (offset >> 16) & 0xFFFF; -} - -extern "C" void idt_setup() { - idtr.limit = (sizeof(struct idt_entry) * 256) - 1; - idtr.base = (uint32_t)idt; - - // todo: is there really no other way? - idt_new_entry(0, (unsigned)isr0, 0x08, 0x8E); - idt_new_entry(1, (unsigned)isr1, 0x08, 0x8E); - idt_new_entry(2, (unsigned)isr2, 0x08, 0x8E); - idt_new_entry(3, (unsigned)isr3, 0x08, 0x8E); - idt_new_entry(4, (unsigned)isr4, 0x08, 0x8E); - idt_new_entry(5, (unsigned)isr5, 0x08, 0x8E); - idt_new_entry(6, (unsigned)isr6, 0x08, 0x8E); - idt_new_entry(7, (unsigned)isr7, 0x08, 0x8E); - idt_new_entry(8, (unsigned)isr8, 0x08, 0x8E); - idt_new_entry(9, (unsigned)isr9, 0x08, 0x8E); - idt_new_entry(10, (unsigned)isr10, 0x08, 0x8E); - idt_new_entry(11, (unsigned)isr11, 0x08, 0x8E); - idt_new_entry(12, (unsigned)isr12, 0x08, 0x8E); - idt_new_entry(13, (unsigned)isr13, 0x08, 0x8E); - idt_new_entry(14, (unsigned)isr14, 0x08, 0x8E); - idt_new_entry(15, (unsigned)isr15, 0x08, 0x8E); - idt_new_entry(16, (unsigned)isr16, 0x08, 0x8E); - idt_new_entry(17, (unsigned)isr17, 0x08, 0x8E); - idt_new_entry(18, (unsigned)isr18, 0x08, 0x8E); - idt_new_entry(19, (unsigned)isr19, 0x08, 0x8E); - idt_new_entry(20, (unsigned)isr20, 0x08, 0x8E); - idt_new_entry(21, (unsigned)isr21, 0x08, 0x8E); - idt_new_entry(22, (unsigned)isr22, 0x08, 0x8E); - idt_new_entry(23, (unsigned)isr23, 0x08, 0x8E); - idt_new_entry(24, (unsigned)isr24, 0x08, 0x8E); - idt_new_entry(25, (unsigned)isr25, 0x08, 0x8E); - idt_new_entry(26, (unsigned)isr26, 0x08, 0x8E); - idt_new_entry(27, (unsigned)isr27, 0x08, 0x8E); - idt_new_entry(28, (unsigned)isr28, 0x08, 0x8E); - idt_new_entry(29, (unsigned)isr29, 0x08, 0x8E); - idt_new_entry(30, (unsigned)isr30, 0x08, 0x8E); - idt_new_entry(31, (unsigned)isr31, 0x08, 0x8E); - //PIC entries - idt_new_entry(32, (unsigned)isr32, 0x08, 0x8E); - idt_new_entry(33, (unsigned)isr33, 0x08, 0x8E); - idt_new_entry(34, (unsigned)isr34, 0x08, 0x8E); - idt_new_entry(35, (unsigned)isr35, 0x08, 0x8E); - idt_new_entry(36, (unsigned)isr36, 0x08, 0x8E); - idt_new_entry(37, (unsigned)isr37, 0x08, 0x8E); - idt_new_entry(38, (unsigned)isr38, 0x08, 0x8E); - idt_new_entry(39, (unsigned)isr39, 0x08, 0x8E); - idt_new_entry(40, (unsigned)isr40, 0x08, 0x8E); - idt_new_entry(41, (unsigned)isr41, 0x08, 0x8E); - idt_new_entry(42, (unsigned)isr42, 0x08, 0x8E); - idt_new_entry(43, (unsigned)isr43, 0x08, 0x8E); - idt_new_entry(44, (unsigned)isr44, 0x08, 0x8E); - idt_new_entry(45, (unsigned)isr45, 0x08, 0x8E); - idt_new_entry(46, (unsigned)isr46, 0x08, 0x8E); - idt_new_entry(47, (unsigned)isr47, 0x08, 0x8E); -} \ No newline at end of file diff --git a/kernel/arch/i386/isr.asm b/kernel/arch/i386/isr.asm deleted file mode 100644 index 4ccbaf9..0000000 --- a/kernel/arch/i386/isr.asm +++ /dev/null @@ -1,324 +0,0 @@ -; isr.asm -; ISR functions are defined here. Most code is done in isr_handlers.cpp - -global isr0 -global isr1 -global isr2 -global isr3 -global isr4 -global isr5 -global isr6 -global isr7 -global isr8 -global isr9 -global isr10 -global isr11 -global isr12 -global isr13 -global isr14 -global isr15 -global isr16 -global isr17 -global isr18 -global isr19 -global isr20 -global isr21 -global isr22 -global isr23 -global isr24 -global isr25 -global isr26 -global isr27 -global isr28 -global isr29 -global isr30 -global isr31 -global isr32 -global isr33 -global isr34 -global isr35 -global isr36 -global isr37 -global isr38 -global isr39 -global isr40 -global isr41 -global isr42 -global isr43 -global isr44 -global isr45 -global isr46 -global isr47 - -section .text -align 4 - -isr0: - pushad - mov ebx, 0 - call exception_stub - hlt - popad - iret - -isr1: - pushad - popad - iret - -isr2: - pushad - popad - iret - -isr3: - pushad - popad - iret - -isr4: - pushad - popad - iret - -isr5: - pushad - popad - iret - -isr6: - pushad - mov ebx, 6 - call exception_stub - popad - iret - -isr7: - pushad - popad - iret - -isr8: - pushad - mov ebx, 8 - call exception_stub - popad - iret - -isr9: - pushad - popad - iret - -isr10: - pushad - popad - iret - -isr11: - pushad - mov ebx, 11 - call exception_stub - popad - iret - -isr12: - pushad - popad - iret - -isr13: - pushad - mov ebx, 13 - call exception_stub - popad - iret - -isr14: - pushad - mov ebx, 14 - call exception_stub - popad - iret - -isr15: - pushad - popad - iret - -isr16: - pushad - popad - iret - -isr17: - pushad - popad - iret - -isr18: - pushad - popad - iret - -isr19: - pushad - popad - iret - -isr20: - pushad - popad - iret - -isr21: - pushad - popad - iret - -isr22: - pushad - popad - iret - -isr23: - pushad - popad - iret - -isr24: - pushad - popad - iret - -isr25: - pushad - popad - iret - -isr26: - pushad - popad - iret - -isr27: - pushad - popad - iret -isr28: - pushad - popad - iret - -isr29: - pushad - popad - iret - -isr30: - pushad - popad - iret - -isr31: - pushad - popad - iret - -isr32: - pushad - ;extern pit_handler - ;call pit_handler - popad - iret - -isr33: - pushad - extern ps2_handler - call ps2_handler - popad - iret - -isr34: - pushad - popad - iret - -isr35: - pushad - popad - iret - -isr36: - pushad - popad - iret -isr37: - pushad - popad - iret - -isr38: - pushad - popad - iret - -isr39: - pushad - popad - iret - -isr40: - pushad - xchg bx, bx - popad - iret - -isr41: - pushad - popad - iret - -isr42: - pushad - popad - iret - -isr43: - pushad - popad - iret - -isr44: - pushad - popad - iret - -isr45: - pushad - popad - iret - -isr46: - pushad - popad - iret - -isr47: - pushad - popad - iret - -exception_stub: - push ebp - mov ebp, esp - - push ebx - ;cld - extern exception_handler - call exception_handler - pop ebx - - mov esp, ebp - pop ebp - ret \ No newline at end of file diff --git a/kernel/arch/i386/isr_handler.cpp b/kernel/arch/i386/isr_handler.cpp deleted file mode 100644 index ac8fb49..0000000 --- a/kernel/arch/i386/isr_handler.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include - -extern "C" void exception_handler(int e) { - //unsigned char errorcode[2]{((unsigned char) e) + 48}; - //terminal_initialize(); - printf("Exception %x\n", e); -} - -#define PS2_DATA 0x60 -extern "C" void ps2_handler() { - keyboard_write(inb(PS2_DATA)); - - pic_send_eoi(1); -} \ No newline at end of file diff --git a/kernel/arch/i386/linker.ld b/kernel/arch/i386/linker.ld deleted file mode 100644 index c4bfa3b..0000000 --- a/kernel/arch/i386/linker.ld +++ /dev/null @@ -1,38 +0,0 @@ -/* Excecution start point symbol */ -ENTRY(_start) - -SECTIONS -{ - . = 1M; - - /* Used to get the beginning and end address of the kernel */ - _kernel_begin = .; - - /* code. multiboot header comes first */ - .text BLOCK(4K) : ALIGN(4K) - { - *(.multiboot) - *(.text) - } - - /* read only data */ - .rodata BLOCK(4K) : ALIGN(4K) - { - *(.rodata) - } - - /* read write data (initialized) */ - .data BLOCK(4K) : ALIGN(4K) - { - *(.data) - } - - /* read write data (uninitialized) and stack */ - .bss BLOCK(4K) : ALIGN(4K) - { - *(COMMON) - *(.bss) - } - - _kernel_end = .; -} \ No newline at end of file diff --git a/kernel/arch/i386/multiboot.cpp b/kernel/arch/i386/multiboot.cpp deleted file mode 100644 index 9f3eedb..0000000 --- a/kernel/arch/i386/multiboot.cpp +++ /dev/null @@ -1,30 +0,0 @@ -#include -#include -#include -#include "multiboot.h" -#include - -// gets the memory map and stuff -extern "C" void multiboot_main(multiboot_info_t* mbd, uint32_t magic) { - terminal_initialize(); - //printf("multiboot struc addr: %x\n", mbd); - if (magic != MULTIBOOT_BOOTLOADER_MAGIC) { - printf("Invalid multiboot magic number\n"); - abort(); - } - - // checks bit 6 to see if there's a valid memory map - if (!(mbd->flags >> 6 & 0x1)) { - printf("Invalid GRUB memory map\n"); - abort(); - } - - // loop through memory map and display the values - for (int i = 0; i < mbd->mmap_length; i += sizeof(multiboot_memory_map_t)) { - multiboot_memory_map_t* mmmt = (multiboot_memory_map_t*)(mbd->mmap_addr + i); - - printf("Start Addr: 0x%lx | Length: 0x%lx | Size: 0x%x | Type: %d\n", - mmmt->addr, mmmt->len, mmmt->size, mmmt->type); - //printf("%lx,%x,%x,%d\n", mmmt->size, mmmt->size, mmmt->size, mmmt->size); - } -} \ No newline at end of file diff --git a/kernel/arch/i386/multiboot.h b/kernel/arch/i386/multiboot.h deleted file mode 100644 index 0951ebe..0000000 --- a/kernel/arch/i386/multiboot.h +++ /dev/null @@ -1,274 +0,0 @@ -/* 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. - */ - -#ifndef MULTIBOOT_HEADER -#define MULTIBOOT_HEADER 1 - -/* How many bytes from the start of the file we search for the header. */ -#define MULTIBOOT_SEARCH 8192 -#define MULTIBOOT_HEADER_ALIGN 4 - -/* 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 -{ - /* Must be MULTIBOOT_MAGIC - see above. */ - multiboot_uint32_t magic; - - /* Feature flags. */ - multiboot_uint32_t flags; - - /* The above fields plus this one must equal 0 mod 2^32. */ - multiboot_uint32_t checksum; - - /* 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; - - /* 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; -}; - -/* 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; - -/* 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; - -struct multiboot_info -{ - /* Multiboot info version number */ - multiboot_uint32_t flags; - - /* Available memory from BIOS */ - multiboot_uint32_t mem_lower; - multiboot_uint32_t mem_upper; - - /* "root" partition */ - multiboot_uint32_t boot_device; - - /* Kernel command line */ - multiboot_uint32_t cmdline; - - /* Boot-Module list */ - multiboot_uint32_t mods_count; - multiboot_uint32_t mods_addr; - - 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/kernel/arch/i386/pic.cpp b/kernel/arch/i386/pic.cpp deleted file mode 100644 index 78cbcf8..0000000 --- a/kernel/arch/i386/pic.cpp +++ /dev/null @@ -1,97 +0,0 @@ -#include -#include -#include -#include - -#define PIC1 0x20 -#define PIC2 0xA0 -#define PIC1_CMD PIC1 -#define PIC1_DATA (PIC1 + 1) -#define PIC2_CMD PIC2 -#define PIC2_DATA (PIC2 + 1) -#define PIC_READ_IRR 0x0a // OCW3 irq ready next CMD read -#define PIC_READ_ISR 0x0b // OCW3 irq service next CMD read - -#define PIC_EOI 0x20 - -// pic reinitialization constants -#define ICW1_ICW4 0x01 // indicates icw4 will be present -#define ICW1_SINGLE 0x02 // single cascade mode -#define ICW1_INTERVAL4 0x04 // call address interval 4 -#define ICW1_LEVEL 0x08 // level triggered (edge) mode -#define ICW1_INIT 0x10 // initialization - -#define ICW4_8086 0x01 // 8086 mode -#define ICW4_AUTO 0x02 // auto (normal) eoi -#define ICW4_BUF_SLAVE 0x08 // buffered mode/slave -#define ICW4_BUF_MASTER 0x0C // buffered mode/master -#define ICW4_SFNM 0x10 // special fully nested (not) - -void pic_send_eoi(unsigned char irq) { - if (irq >= 8) { - outb(PIC2_CMD, PIC_EOI); - } - outb(PIC1_CMD, PIC_EOI); -} - -// offset 1 is for master, offset 2 is for slave -void pic_remap(int offset1, int offset2) { - unsigned char a1, a2; - - a1 = inb(PIC1_DATA); // save masks - a2 = inb(PIC2_DATA); - - outb(PIC1_CMD, ICW1_INIT | ICW1_ICW4); // starts initialization in cascade mode - io_wait(); - outb(PIC2_CMD, ICW1_INIT | ICW1_ICW4); - io_wait(); - outb(PIC1_DATA, offset1); // icw2: master and slave vector offset set - io_wait(); - outb(PIC2_DATA, offset2); - io_wait(); - outb(PIC1_DATA, 4); // tells master pic theres a slave pic at irq2 - io_wait(); - outb(PIC2_DATA, 2); // tells slave pic its cascade identity - io_wait(); - - outb(PIC1_DATA, ICW4_8086); - io_wait(); - outb(PIC2_DATA, ICW4_8086); - io_wait(); - - outb(PIC1_DATA, a1); // restore masks - outb(PIC2_DATA, a2); - - for (char i = 0; i < 15; i++) - { - irq_set_mask(i); - } -} - -void irq_set_mask(unsigned char irq) { - uint16_t port; - uint8_t value; - - if (irq < 8) { - port = PIC1_DATA; - } else { - port = PIC2_DATA; - irq -= 8; - } - value = inb(port) | (1 << irq); - outb(port, value); -} - -void irq_clear_mask(unsigned char irq) { - uint16_t port; - uint8_t value; - - if (irq < 8) { - port = PIC1_DATA; - } else { - port = PIC2_DATA; - irq -= 8; - } - value = inb(port) & ~(1 << irq); - outb(port, value); -} \ No newline at end of file diff --git a/kernel/arch/i386/ps2_controller.cpp b/kernel/arch/i386/ps2_controller.cpp deleted file mode 100644 index 9424233..0000000 --- a/kernel/arch/i386/ps2_controller.cpp +++ /dev/null @@ -1,191 +0,0 @@ -#include -#include -#include -#include - -#define PS2_DATA 0x60 // data port -#define PS2_STATUS 0x64 // status register -#define PS2_COMMAND 0x64 // command register - -// set to true if 2nd ps2 port is available -bool ps2SecondaryEnabled = false; - -void initialize_ps2_controller() { - inputbuffer_wait(); - outb(PS2_COMMAND, 0xAD); // disable devices - inputbuffer_wait(); - outb(PS2_COMMAND, 0xA7); - - outputbuffer_wait(); - inb(PS2_DATA); // flush output buffer - inb(PS2_DATA); - inb(PS2_DATA); - inb(PS2_DATA); - inb(PS2_DATA); - inb(PS2_DATA); - inb(PS2_DATA); - inb(PS2_DATA); - inb(PS2_DATA); - inb(PS2_DATA); - - - inputbuffer_wait(); - outb(PS2_COMMAND, 0x20); // set controller configuration byte - outputbuffer_wait(); - uint8_t conconfig = inb(PS2_DATA); - conconfig &= 0b00110100; - inputbuffer_wait(); - outb(PS2_COMMAND, 0x60); - inputbuffer_wait(); - outb(PS2_DATA, conconfig); - - inputbuffer_wait(); - outb(PS2_COMMAND, 0xAA); // performs controller self test - outputbuffer_wait(); - if (inb(PS2_DATA) == 0xFC) { - printf("PS/2 controller failed self test\n"); - } - inputbuffer_wait(); - outb(PS2_COMMAND, 0x60); // in case the controller resets from the test - inputbuffer_wait(); - outb(PS2_DATA, conconfig); - - int portcount = 1; - inputbuffer_wait(); - outb(PS2_COMMAND, 0xA8); // check if ps2 controller is dual channel - inputbuffer_wait(); - outb(PS2_COMMAND, 0x20); - outputbuffer_wait(); - conconfig = inb(PS2_DATA); - if ((conconfig & 0b00100000) == 0b00000000) { // checks if bit 5 is cleared - ps2SecondaryEnabled = true; - //printf("Dual channel PS/2\n"); - portcount = 2; - } else { - //printf("Single channel PS/2\n"); - } - - inputbuffer_wait(); - outb(PS2_COMMAND, 0xAB); // tests ps2 ports - outputbuffer_wait(); - if (inb(PS2_DATA) != 0x00) { - printf("PS/2 port 1 failed self test\n"); - --portcount; - } - if (ps2SecondaryEnabled) { - inputbuffer_wait(); - outb(PS2_COMMAND, 0xA9); - outputbuffer_wait(); - if (inb(PS2_DATA) != 0x00) { - printf("PS/2 port 2 failed self test\n"); - --portcount; - } - } - if (!portcount) { - printf("PS/2 initialization failed\n"); - return; - } - - conconfig |= 0b00000011; - inputbuffer_wait(); - outb(PS2_COMMAND, 0xAE); // enable ps2 ports - conconfig |= 0b00000001; - if (ps2SecondaryEnabled) { - inputbuffer_wait(); - outb(PS2_COMMAND, 0xA8); - outb(PS2_COMMAND, 0x60); - conconfig |= 0b00000011; - outb(PS2_DATA, conconfig); - } else { - inputbuffer_wait(); - outb(PS2_COMMAND, 0x60); - conconfig |= 0b00000001; - outb(PS2_DATA, conconfig); - } - - //ps2_dev_send_command(0, 0xFF); // reset devices - //inb(PS2_DATA); - //io_wait(); - if (ps2SecondaryEnabled) { - //ps2_dev_send_command(1, 0xFF); - //inb(PS2_DATA); - //io_wait(); - } - - //printf("PS/2 controller initialized\n"); -} - -// waits for input buffer (write) to be filled -void inputbuffer_wait() { - for (int i = 0; i < 15; i++) { - uint8_t status_reg = inb(PS2_STATUS); - if ((status_reg & 0b00000010)) { - return; - } - io_wait(); - } -} - -// waits for outbut buffer (read) to be filled -void outputbuffer_wait() { - for (int i = 0; i < 15; i++) { - uint8_t status_reg = inb(PS2_STATUS); - if ((status_reg & 0b00000001)) { - return; - } - io_wait(); - } -} - -// checks if inpbut buffer is set -bool inputbuffer_check() { - uint8_t status_reg = inb(PS2_STATUS); - if ((status_reg & 0b00000010)) { - return true; - } else { - return false; - } -} - -bool outputbuffer_check() { - uint8_t status_reg = inb(PS2_STATUS); - if ((status_reg & 0b00000001)) { - return true; - } else { - return false; - } -} - -/* Sends a command byte to a ps2 device -Port numbers start from 0 */ -int ps2_dev_send_command(int port, unsigned char command) { - //bool commandRecieved = false; - switch (port) - { - case 0: - for (int i = 0; i < 5; i++) { - uint8_t status_reg = inb(PS2_STATUS); - if ((status_reg & 0b00000010) == 0b00000000) { - outb(PS2_DATA, command); - return 0; - } - inb(PS2_DATA); - io_wait(); - } - return 1; // error returned if timeout expires - case 1: - outb(PS2_COMMAND, 0xD4); - for (int i = 0; i < 5; i++) { - uint8_t status_reg = inb(PS2_STATUS); - if ((status_reg & 0b00000010) == 0b00000000) { - outb(PS2_DATA, command); - return 0; - } - inb(PS2_DATA); - io_wait(); - } - return 1; - default: - return 2; // out of port range - } -} \ No newline at end of file diff --git a/kernel/arch/i386/tmtty.cpp b/kernel/arch/i386/tmtty.cpp deleted file mode 100644 index d4dc772..0000000 --- a/kernel/arch/i386/tmtty.cpp +++ /dev/null @@ -1,78 +0,0 @@ -#include -#include -#include -#include -#include - -#include "vga.h" - -/* Text mode tty */ - -static const size_t VGA_WIDTH = 80; -static const size_t VGA_HEIGHT = 25; -static uint16_t* const VGA_MEMORY = (uint16_t*) 0xB8000; - -size_t terminal_row; -size_t terminal_column; -uint8_t terminal_color; -uint16_t* terminal_buffer; - -void terminal_clear(void) { - terminal_row = 0; - terminal_column = 0; - for (size_t y = 0; y < VGA_HEIGHT; y++) { - for (size_t x = 0; x < VGA_WIDTH; x++) { - const size_t i = y * VGA_WIDTH + x; - terminal_buffer[i] = vga_entry(' ', terminal_color); - } - } -} - -void terminal_initialize(void) { - terminal_color = vga_entry_color(VGA_COLOR_WHITE, VGA_COLOR_BLACK); - terminal_buffer = VGA_MEMORY; - terminal_clear(); -} - -void terminal_setcolor(uint8_t color) { - terminal_color = color; -} - -void terminal_putentryat(char c, uint8_t color, size_t x, size_t y) { - const size_t i = y * VGA_WIDTH + x; - terminal_buffer[i] = vga_entry(c, color); -} - -void terminal_scroll() { - memmove(VGA_MEMORY, (VGA_MEMORY + VGA_WIDTH), (VGA_HEIGHT * VGA_WIDTH) - VGA_WIDTH); - memset(VGA_MEMORY + (VGA_WIDTH * VGA_HEIGHT) - VGA_WIDTH, 0x0, VGA_WIDTH * 2); - --terminal_row; -} - -void terminal_putchar(char c) { - unsigned char uc = c; - if (uc == '\n') { - terminal_column = 0; - if (terminal_row++ == VGA_HEIGHT) - terminal_scroll(); - return; - } - terminal_putentryat(uc, terminal_color, terminal_column, terminal_row); - - if (++terminal_column == VGA_WIDTH) { - terminal_column = 0; - if (++terminal_row == VGA_HEIGHT) { - terminal_scroll(); - } - } -} - -void terminal_write(const char* data, size_t size) { - for (size_t i = 0; i < size; i++) { - terminal_putchar(data[i]); - } -} - -void terminal_writestring(const char* data) { - terminal_write(data, strlen(data)); -} \ No newline at end of file diff --git a/kernel/arch/i386/vga.h b/kernel/arch/i386/vga.h deleted file mode 100644 index 5b6b351..0000000 --- a/kernel/arch/i386/vga.h +++ /dev/null @@ -1,35 +0,0 @@ -#ifndef ARCH_I386_VGA_H -#define ARCH_I386_VGA_H - -#include - -// hardware text mode constants -enum vga_color { - VGA_COLOR_BLACK = 0, - VGA_COLOR_BLUE = 1, - VGA_COLOR_GREEN = 2, - VGA_COLOR_CYAN = 3, - VGA_COLOR_RED = 4, - VGA_COLOR_MAGENTA = 5, - VGA_COLOR_BROWN = 6, - VGA_COLOR_LIGHT_GREY = 7, - VGA_COLOR_DARK_GREY = 8, - VGA_COLOR_LIGHT_BLUE = 9, - VGA_COLOR_LIGHT_GREEN = 10, - VGA_COLOR_LIGHT_CYAN = 11, - VGA_COLOR_LIGHT_RED = 12, - VGA_COLOR_LIGHT_MAGENTA = 13, - VGA_COLOR_LIGHT_BROWN = 14, - VGA_COLOR_WHITE = 15, -}; - -static inline uint8_t vga_entry_color(enum vga_color fg, enum vga_color bg) { - return fg | bg << 4; -} - -// returns the character/color of a terminal entry -static inline uint16_t vga_entry(unsigned char uc, uint8_t color) { - return (uint16_t) uc | (uint16_t) color << 8; -} - -#endif \ No newline at end of file diff --git a/kernel/arch/x86/boot.asm b/kernel/arch/x86/boot.asm new file mode 100644 index 0000000..42c11e6 --- /dev/null +++ b/kernel/arch/x86/boot.asm @@ -0,0 +1,103 @@ +; multiboot header constants +MBALIGN equ 1 << 0 +MEMINFO equ 1 << 1 +MBFLAGS equ MBALIGN | MEMINFO +MAGIC equ 0x1BADB002 +CHECKSUM equ -(MAGIC+MBFLAGS) + +; multiboot header +section .multiboot +align 4 + dd MAGIC + dd MBFLAGS + dd CHECKSUM + +section .data +%include "arch/i386/gdt.asm" + +; defines the stack +section .bss +align 16 +stack_bottom: +resb 16384 ; 16 KiB +stack_top: + +; temporary paging tables +align 4 +boot_page_directory: + resb 4096 +boot_page_table: + resb 4096 + + +section .text +global _start:function (_start.end - _start) +_start: + cli + ; setup stack + mov esp, stack_top + + ; push the multiboot header data into the stack for later use + push eax + push ebx + + ; get multiboot header data (for memory map) + ;extern multiboot_main + ;call multiboot_main + + ;extern _init + ;Scall _init + + lgdt [gdt_descriptor] + jmp 0x08:.gdt_jmp + +.gdt_jmp: + mov ax, DATA_SEG + mov ds, ax + mov es, ax + mov fs, ax + mov gs, ax + mov ss, ax + jmp .gdt_jmp2 + +.gdt_jmp2: + ; setup paging (to map kernel to the higher half) + ;mov eax, 0 + ;mov cr3, eax + ;mov eax, cr0 + ;or eax, 0x80000001 + ;mov cr0, eax + + ; setup idt + extern idt_setup + call idt_setup + extern idtr + lidt [idtr] + + ; initialize pic + push ebp + mov ebp, esp + mov ebx, 0x28 + push ebx + mov ebx, 0x20 + push ebx + + extern pic_remap + call pic_remap + + pop ebx + pop ebx + mov esp, ebp + pop ebp + + sti + + ; enter the kernel + extern kernel_main + call kernel_main + + ; indefinite loop once system exits + ;cli +.hang: hlt + jmp .hang +.end: \ No newline at end of file diff --git a/kernel/arch/x86/crti.asm b/kernel/arch/x86/crti.asm new file mode 100644 index 0000000..51f27d9 --- /dev/null +++ b/kernel/arch/x86/crti.asm @@ -0,0 +1,12 @@ +; x86 crti.asm +section .init +global _init:function +_init: + push ebp + mov ebp, esp + +section .fini +global _fini:function +_fini: + push ebp + mov ebp, esp \ No newline at end of file diff --git a/kernel/arch/x86/crtn.asm b/kernel/arch/x86/crtn.asm new file mode 100644 index 0000000..370f4ae --- /dev/null +++ b/kernel/arch/x86/crtn.asm @@ -0,0 +1,9 @@ +; x86 crtn.asm + +section .init + pop ebp + ret + +section .fini + pop ebp + ret \ No newline at end of file diff --git a/kernel/arch/x86/gdt.asm b/kernel/arch/x86/gdt.asm new file mode 100644 index 0000000..e3af8bd --- /dev/null +++ b/kernel/arch/x86/gdt.asm @@ -0,0 +1,35 @@ +; gdt.asm +; GDT is defined here + +gdt_start: + +gdt_null: ; Null segment + dd 0x0 + dd 0x0 + +gdt_kcode: ; Kernel code segment. Kernel has seperate segment from users for ring level + ; flag and access byte description on osdev wiki + dw 0xffff ; low limit + dw 0x0 ;low base + db 0x0 ; mid base + db 10011010b ; order (reversed): ac, rw, dc, ex, s, privl, pr + db 11001111b ; flags then limit. order (reversed): 0, 0, size bit, granularity bit + db 0x0 ; high limit + +gdt_kdata: ; same as gdt_kcode but for the data segment + dw 0xffff ;low limit + dw 0x0 ;low base + db 0x0 ;mid base + db 10010010b ; order: same as code seg. changed for data seg + db 11001111b ; same as kcode + db 0x0 ; high limit + +gdt_end: + +gdt_descriptor: ; describes the gdt to the lgdt register + dw gdt_end - gdt_start - 1 + + dd gdt_start + +CODE_SEG equ gdt_kcode - gdt_start +DATA_SEG equ gdt_kdata - gdt_start \ No newline at end of file diff --git a/kernel/arch/x86/gdt2.cpp b/kernel/arch/x86/gdt2.cpp new file mode 100644 index 0000000..0acdb0e --- /dev/null +++ b/kernel/arch/x86/gdt2.cpp @@ -0,0 +1,51 @@ +#include +#include +/* +// structure to be encoded at encodeGdtEntry() +struct GDT +{ + uint32_t base; + uint32_t limit; + uint8_t access_byte; + uint8_t flags; +}; + +GDT strnulldesc {0, 0, 0, 0}; +GDT strkernelcode {0, 0xFFFFF, 0x9A, 0xC}; +GDT strkerneldata {0, 0xFFFFF, 0x92, 0xC}; +GDT strusercode {0, 0xFFFFF, 0xFA, 0xC}; +GDT struserdata {0, 0xFFFFF, 0xF2, 0xC}; + +uint8_t* nulldesc; +uint8_t* kernelcode; +uint8_t* kerneldata; +uint8_t* usercode; +uint8_t* userdata; + + +void encodeGdtEntry(uint8_t *target, struct GDT source) +{ + // Check the limit to make sure that it can be encoded + if (source.limit > 0xFFFFF) {printf("GDT cannot encode limits larger than 0xFFFFF\n");} + + // Encode the limit + target[0] = source.limit & 0xFF; + target[1] = (source.limit >> 8) & 0xFF; + target[6] = (source.limit >> 16) & 0x0F; + + // Encode the base + target[2] = source.base & 0xFF; + target[3] = (source.base >> 8) & 0xFF; + target[4] = (source.base >> 16) & 0xFF; + target[7] = (source.base >> 24) & 0xFF; + + // Encode the access byte + target[5] = source.access_byte; + + // Encode the flags + target[6] |= (source.flags << 4); +} + +extern "C" void setupGdt() { + +} */ \ No newline at end of file diff --git a/kernel/arch/x86/idt.cpp b/kernel/arch/x86/idt.cpp new file mode 100644 index 0000000..a8f3ff4 --- /dev/null +++ b/kernel/arch/x86/idt.cpp @@ -0,0 +1,133 @@ +#include +#include + +/* Function prototypes for the ISR functions. IDT[0] to IDT[31] are reserved for hardware exceptions */ +extern "C" void isr0(); +extern "C" void isr1(); +extern "C" void isr2(); +extern "C" void isr3(); +extern "C" void isr4(); +extern "C" void isr5(); +extern "C" void isr6(); +extern "C" void isr7(); +extern "C" void isr8(); +extern "C" void isr9(); +extern "C" void isr10(); +extern "C" void isr11(); +extern "C" void isr12(); +extern "C" void isr13(); +extern "C" void isr14(); +extern "C" void isr15(); +extern "C" void isr16(); +extern "C" void isr17(); +extern "C" void isr18(); +extern "C" void isr19(); +extern "C" void isr20(); +extern "C" void isr21(); +extern "C" void isr22(); +extern "C" void isr23(); +extern "C" void isr24(); +extern "C" void isr25(); +extern "C" void isr26(); +extern "C" void isr27(); +extern "C" void isr28(); +extern "C" void isr29(); +extern "C" void isr30(); +extern "C" void isr31(); +extern "C" void isr32(); +extern "C" void isr33(); +extern "C" void isr34(); +extern "C" void isr35(); +extern "C" void isr36(); +extern "C" void isr37(); +extern "C" void isr38(); +extern "C" void isr39(); +extern "C" void isr40(); +extern "C" void isr41(); +extern "C" void isr42(); +extern "C" void isr43(); +extern "C" void isr44(); +extern "C" void isr45(); +extern "C" void isr46(); +extern "C" void isr47(); + +struct idt_entry +{ + uint16_t offset_low; + uint16_t selector; + uint8_t always0; + uint8_t type_attributes; + uint16_t offset_high; +}__attribute__((packed)); + +struct str_idtr { + uint16_t limit; + uint32_t base; +}__attribute__((packed)); + +idt_entry idt[256]; +str_idtr idtr; + +void idt_new_entry(int num, uint32_t offset, uint16_t sel, uint8_t type_attribs) { + idt[num].offset_low = (offset & 0xFFFF); + idt[num].selector = sel; + idt[num].always0 = 0; + idt[num].type_attributes = type_attribs; + idt[num].offset_high = (offset >> 16) & 0xFFFF; +} + +extern "C" void idt_setup() { + idtr.limit = (sizeof(struct idt_entry) * 256) - 1; + idtr.base = (uint32_t)idt; + + // todo: is there really no other way? + idt_new_entry(0, (unsigned)isr0, 0x08, 0x8E); + idt_new_entry(1, (unsigned)isr1, 0x08, 0x8E); + idt_new_entry(2, (unsigned)isr2, 0x08, 0x8E); + idt_new_entry(3, (unsigned)isr3, 0x08, 0x8E); + idt_new_entry(4, (unsigned)isr4, 0x08, 0x8E); + idt_new_entry(5, (unsigned)isr5, 0x08, 0x8E); + idt_new_entry(6, (unsigned)isr6, 0x08, 0x8E); + idt_new_entry(7, (unsigned)isr7, 0x08, 0x8E); + idt_new_entry(8, (unsigned)isr8, 0x08, 0x8E); + idt_new_entry(9, (unsigned)isr9, 0x08, 0x8E); + idt_new_entry(10, (unsigned)isr10, 0x08, 0x8E); + idt_new_entry(11, (unsigned)isr11, 0x08, 0x8E); + idt_new_entry(12, (unsigned)isr12, 0x08, 0x8E); + idt_new_entry(13, (unsigned)isr13, 0x08, 0x8E); + idt_new_entry(14, (unsigned)isr14, 0x08, 0x8E); + idt_new_entry(15, (unsigned)isr15, 0x08, 0x8E); + idt_new_entry(16, (unsigned)isr16, 0x08, 0x8E); + idt_new_entry(17, (unsigned)isr17, 0x08, 0x8E); + idt_new_entry(18, (unsigned)isr18, 0x08, 0x8E); + idt_new_entry(19, (unsigned)isr19, 0x08, 0x8E); + idt_new_entry(20, (unsigned)isr20, 0x08, 0x8E); + idt_new_entry(21, (unsigned)isr21, 0x08, 0x8E); + idt_new_entry(22, (unsigned)isr22, 0x08, 0x8E); + idt_new_entry(23, (unsigned)isr23, 0x08, 0x8E); + idt_new_entry(24, (unsigned)isr24, 0x08, 0x8E); + idt_new_entry(25, (unsigned)isr25, 0x08, 0x8E); + idt_new_entry(26, (unsigned)isr26, 0x08, 0x8E); + idt_new_entry(27, (unsigned)isr27, 0x08, 0x8E); + idt_new_entry(28, (unsigned)isr28, 0x08, 0x8E); + idt_new_entry(29, (unsigned)isr29, 0x08, 0x8E); + idt_new_entry(30, (unsigned)isr30, 0x08, 0x8E); + idt_new_entry(31, (unsigned)isr31, 0x08, 0x8E); + //PIC entries + idt_new_entry(32, (unsigned)isr32, 0x08, 0x8E); + idt_new_entry(33, (unsigned)isr33, 0x08, 0x8E); + idt_new_entry(34, (unsigned)isr34, 0x08, 0x8E); + idt_new_entry(35, (unsigned)isr35, 0x08, 0x8E); + idt_new_entry(36, (unsigned)isr36, 0x08, 0x8E); + idt_new_entry(37, (unsigned)isr37, 0x08, 0x8E); + idt_new_entry(38, (unsigned)isr38, 0x08, 0x8E); + idt_new_entry(39, (unsigned)isr39, 0x08, 0x8E); + idt_new_entry(40, (unsigned)isr40, 0x08, 0x8E); + idt_new_entry(41, (unsigned)isr41, 0x08, 0x8E); + idt_new_entry(42, (unsigned)isr42, 0x08, 0x8E); + idt_new_entry(43, (unsigned)isr43, 0x08, 0x8E); + idt_new_entry(44, (unsigned)isr44, 0x08, 0x8E); + idt_new_entry(45, (unsigned)isr45, 0x08, 0x8E); + idt_new_entry(46, (unsigned)isr46, 0x08, 0x8E); + idt_new_entry(47, (unsigned)isr47, 0x08, 0x8E); +} \ No newline at end of file diff --git a/kernel/arch/x86/isr.asm b/kernel/arch/x86/isr.asm new file mode 100644 index 0000000..4ccbaf9 --- /dev/null +++ b/kernel/arch/x86/isr.asm @@ -0,0 +1,324 @@ +; isr.asm +; ISR functions are defined here. Most code is done in isr_handlers.cpp + +global isr0 +global isr1 +global isr2 +global isr3 +global isr4 +global isr5 +global isr6 +global isr7 +global isr8 +global isr9 +global isr10 +global isr11 +global isr12 +global isr13 +global isr14 +global isr15 +global isr16 +global isr17 +global isr18 +global isr19 +global isr20 +global isr21 +global isr22 +global isr23 +global isr24 +global isr25 +global isr26 +global isr27 +global isr28 +global isr29 +global isr30 +global isr31 +global isr32 +global isr33 +global isr34 +global isr35 +global isr36 +global isr37 +global isr38 +global isr39 +global isr40 +global isr41 +global isr42 +global isr43 +global isr44 +global isr45 +global isr46 +global isr47 + +section .text +align 4 + +isr0: + pushad + mov ebx, 0 + call exception_stub + hlt + popad + iret + +isr1: + pushad + popad + iret + +isr2: + pushad + popad + iret + +isr3: + pushad + popad + iret + +isr4: + pushad + popad + iret + +isr5: + pushad + popad + iret + +isr6: + pushad + mov ebx, 6 + call exception_stub + popad + iret + +isr7: + pushad + popad + iret + +isr8: + pushad + mov ebx, 8 + call exception_stub + popad + iret + +isr9: + pushad + popad + iret + +isr10: + pushad + popad + iret + +isr11: + pushad + mov ebx, 11 + call exception_stub + popad + iret + +isr12: + pushad + popad + iret + +isr13: + pushad + mov ebx, 13 + call exception_stub + popad + iret + +isr14: + pushad + mov ebx, 14 + call exception_stub + popad + iret + +isr15: + pushad + popad + iret + +isr16: + pushad + popad + iret + +isr17: + pushad + popad + iret + +isr18: + pushad + popad + iret + +isr19: + pushad + popad + iret + +isr20: + pushad + popad + iret + +isr21: + pushad + popad + iret + +isr22: + pushad + popad + iret + +isr23: + pushad + popad + iret + +isr24: + pushad + popad + iret + +isr25: + pushad + popad + iret + +isr26: + pushad + popad + iret + +isr27: + pushad + popad + iret +isr28: + pushad + popad + iret + +isr29: + pushad + popad + iret + +isr30: + pushad + popad + iret + +isr31: + pushad + popad + iret + +isr32: + pushad + ;extern pit_handler + ;call pit_handler + popad + iret + +isr33: + pushad + extern ps2_handler + call ps2_handler + popad + iret + +isr34: + pushad + popad + iret + +isr35: + pushad + popad + iret + +isr36: + pushad + popad + iret +isr37: + pushad + popad + iret + +isr38: + pushad + popad + iret + +isr39: + pushad + popad + iret + +isr40: + pushad + xchg bx, bx + popad + iret + +isr41: + pushad + popad + iret + +isr42: + pushad + popad + iret + +isr43: + pushad + popad + iret + +isr44: + pushad + popad + iret + +isr45: + pushad + popad + iret + +isr46: + pushad + popad + iret + +isr47: + pushad + popad + iret + +exception_stub: + push ebp + mov ebp, esp + + push ebx + ;cld + extern exception_handler + call exception_handler + pop ebx + + mov esp, ebp + pop ebp + ret \ No newline at end of file diff --git a/kernel/arch/x86/isr_handler.cpp b/kernel/arch/x86/isr_handler.cpp new file mode 100644 index 0000000..ac8fb49 --- /dev/null +++ b/kernel/arch/x86/isr_handler.cpp @@ -0,0 +1,21 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +extern "C" void exception_handler(int e) { + //unsigned char errorcode[2]{((unsigned char) e) + 48}; + //terminal_initialize(); + printf("Exception %x\n", e); +} + +#define PS2_DATA 0x60 +extern "C" void ps2_handler() { + keyboard_write(inb(PS2_DATA)); + + pic_send_eoi(1); +} \ No newline at end of file diff --git a/kernel/arch/x86/linker.ld b/kernel/arch/x86/linker.ld new file mode 100644 index 0000000..c4bfa3b --- /dev/null +++ b/kernel/arch/x86/linker.ld @@ -0,0 +1,38 @@ +/* Excecution start point symbol */ +ENTRY(_start) + +SECTIONS +{ + . = 1M; + + /* Used to get the beginning and end address of the kernel */ + _kernel_begin = .; + + /* code. multiboot header comes first */ + .text BLOCK(4K) : ALIGN(4K) + { + *(.multiboot) + *(.text) + } + + /* read only data */ + .rodata BLOCK(4K) : ALIGN(4K) + { + *(.rodata) + } + + /* read write data (initialized) */ + .data BLOCK(4K) : ALIGN(4K) + { + *(.data) + } + + /* read write data (uninitialized) and stack */ + .bss BLOCK(4K) : ALIGN(4K) + { + *(COMMON) + *(.bss) + } + + _kernel_end = .; +} \ No newline at end of file diff --git a/kernel/arch/x86/multiboot.cpp b/kernel/arch/x86/multiboot.cpp new file mode 100644 index 0000000..9f3eedb --- /dev/null +++ b/kernel/arch/x86/multiboot.cpp @@ -0,0 +1,30 @@ +#include +#include +#include +#include "multiboot.h" +#include + +// gets the memory map and stuff +extern "C" void multiboot_main(multiboot_info_t* mbd, uint32_t magic) { + terminal_initialize(); + //printf("multiboot struc addr: %x\n", mbd); + if (magic != MULTIBOOT_BOOTLOADER_MAGIC) { + printf("Invalid multiboot magic number\n"); + abort(); + } + + // checks bit 6 to see if there's a valid memory map + if (!(mbd->flags >> 6 & 0x1)) { + printf("Invalid GRUB memory map\n"); + abort(); + } + + // loop through memory map and display the values + for (int i = 0; i < mbd->mmap_length; i += sizeof(multiboot_memory_map_t)) { + multiboot_memory_map_t* mmmt = (multiboot_memory_map_t*)(mbd->mmap_addr + i); + + printf("Start Addr: 0x%lx | Length: 0x%lx | Size: 0x%x | Type: %d\n", + mmmt->addr, mmmt->len, mmmt->size, mmmt->type); + //printf("%lx,%x,%x,%d\n", mmmt->size, mmmt->size, mmmt->size, mmmt->size); + } +} \ No newline at end of file diff --git a/kernel/arch/x86/multiboot.h b/kernel/arch/x86/multiboot.h new file mode 100644 index 0000000..0951ebe --- /dev/null +++ b/kernel/arch/x86/multiboot.h @@ -0,0 +1,274 @@ +/* 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. + */ + +#ifndef MULTIBOOT_HEADER +#define MULTIBOOT_HEADER 1 + +/* How many bytes from the start of the file we search for the header. */ +#define MULTIBOOT_SEARCH 8192 +#define MULTIBOOT_HEADER_ALIGN 4 + +/* 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 +{ + /* Must be MULTIBOOT_MAGIC - see above. */ + multiboot_uint32_t magic; + + /* Feature flags. */ + multiboot_uint32_t flags; + + /* The above fields plus this one must equal 0 mod 2^32. */ + multiboot_uint32_t checksum; + + /* 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; + + /* 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; +}; + +/* 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; + +/* 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; + +struct multiboot_info +{ + /* Multiboot info version number */ + multiboot_uint32_t flags; + + /* Available memory from BIOS */ + multiboot_uint32_t mem_lower; + multiboot_uint32_t mem_upper; + + /* "root" partition */ + multiboot_uint32_t boot_device; + + /* Kernel command line */ + multiboot_uint32_t cmdline; + + /* Boot-Module list */ + multiboot_uint32_t mods_count; + multiboot_uint32_t mods_addr; + + 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/kernel/arch/x86/pic.cpp b/kernel/arch/x86/pic.cpp new file mode 100644 index 0000000..78cbcf8 --- /dev/null +++ b/kernel/arch/x86/pic.cpp @@ -0,0 +1,97 @@ +#include +#include +#include +#include + +#define PIC1 0x20 +#define PIC2 0xA0 +#define PIC1_CMD PIC1 +#define PIC1_DATA (PIC1 + 1) +#define PIC2_CMD PIC2 +#define PIC2_DATA (PIC2 + 1) +#define PIC_READ_IRR 0x0a // OCW3 irq ready next CMD read +#define PIC_READ_ISR 0x0b // OCW3 irq service next CMD read + +#define PIC_EOI 0x20 + +// pic reinitialization constants +#define ICW1_ICW4 0x01 // indicates icw4 will be present +#define ICW1_SINGLE 0x02 // single cascade mode +#define ICW1_INTERVAL4 0x04 // call address interval 4 +#define ICW1_LEVEL 0x08 // level triggered (edge) mode +#define ICW1_INIT 0x10 // initialization + +#define ICW4_8086 0x01 // 8086 mode +#define ICW4_AUTO 0x02 // auto (normal) eoi +#define ICW4_BUF_SLAVE 0x08 // buffered mode/slave +#define ICW4_BUF_MASTER 0x0C // buffered mode/master +#define ICW4_SFNM 0x10 // special fully nested (not) + +void pic_send_eoi(unsigned char irq) { + if (irq >= 8) { + outb(PIC2_CMD, PIC_EOI); + } + outb(PIC1_CMD, PIC_EOI); +} + +// offset 1 is for master, offset 2 is for slave +void pic_remap(int offset1, int offset2) { + unsigned char a1, a2; + + a1 = inb(PIC1_DATA); // save masks + a2 = inb(PIC2_DATA); + + outb(PIC1_CMD, ICW1_INIT | ICW1_ICW4); // starts initialization in cascade mode + io_wait(); + outb(PIC2_CMD, ICW1_INIT | ICW1_ICW4); + io_wait(); + outb(PIC1_DATA, offset1); // icw2: master and slave vector offset set + io_wait(); + outb(PIC2_DATA, offset2); + io_wait(); + outb(PIC1_DATA, 4); // tells master pic theres a slave pic at irq2 + io_wait(); + outb(PIC2_DATA, 2); // tells slave pic its cascade identity + io_wait(); + + outb(PIC1_DATA, ICW4_8086); + io_wait(); + outb(PIC2_DATA, ICW4_8086); + io_wait(); + + outb(PIC1_DATA, a1); // restore masks + outb(PIC2_DATA, a2); + + for (char i = 0; i < 15; i++) + { + irq_set_mask(i); + } +} + +void irq_set_mask(unsigned char irq) { + uint16_t port; + uint8_t value; + + if (irq < 8) { + port = PIC1_DATA; + } else { + port = PIC2_DATA; + irq -= 8; + } + value = inb(port) | (1 << irq); + outb(port, value); +} + +void irq_clear_mask(unsigned char irq) { + uint16_t port; + uint8_t value; + + if (irq < 8) { + port = PIC1_DATA; + } else { + port = PIC2_DATA; + irq -= 8; + } + value = inb(port) & ~(1 << irq); + outb(port, value); +} \ No newline at end of file diff --git a/kernel/arch/x86/ps2_controller.cpp b/kernel/arch/x86/ps2_controller.cpp new file mode 100644 index 0000000..9424233 --- /dev/null +++ b/kernel/arch/x86/ps2_controller.cpp @@ -0,0 +1,191 @@ +#include +#include +#include +#include + +#define PS2_DATA 0x60 // data port +#define PS2_STATUS 0x64 // status register +#define PS2_COMMAND 0x64 // command register + +// set to true if 2nd ps2 port is available +bool ps2SecondaryEnabled = false; + +void initialize_ps2_controller() { + inputbuffer_wait(); + outb(PS2_COMMAND, 0xAD); // disable devices + inputbuffer_wait(); + outb(PS2_COMMAND, 0xA7); + + outputbuffer_wait(); + inb(PS2_DATA); // flush output buffer + inb(PS2_DATA); + inb(PS2_DATA); + inb(PS2_DATA); + inb(PS2_DATA); + inb(PS2_DATA); + inb(PS2_DATA); + inb(PS2_DATA); + inb(PS2_DATA); + inb(PS2_DATA); + + + inputbuffer_wait(); + outb(PS2_COMMAND, 0x20); // set controller configuration byte + outputbuffer_wait(); + uint8_t conconfig = inb(PS2_DATA); + conconfig &= 0b00110100; + inputbuffer_wait(); + outb(PS2_COMMAND, 0x60); + inputbuffer_wait(); + outb(PS2_DATA, conconfig); + + inputbuffer_wait(); + outb(PS2_COMMAND, 0xAA); // performs controller self test + outputbuffer_wait(); + if (inb(PS2_DATA) == 0xFC) { + printf("PS/2 controller failed self test\n"); + } + inputbuffer_wait(); + outb(PS2_COMMAND, 0x60); // in case the controller resets from the test + inputbuffer_wait(); + outb(PS2_DATA, conconfig); + + int portcount = 1; + inputbuffer_wait(); + outb(PS2_COMMAND, 0xA8); // check if ps2 controller is dual channel + inputbuffer_wait(); + outb(PS2_COMMAND, 0x20); + outputbuffer_wait(); + conconfig = inb(PS2_DATA); + if ((conconfig & 0b00100000) == 0b00000000) { // checks if bit 5 is cleared + ps2SecondaryEnabled = true; + //printf("Dual channel PS/2\n"); + portcount = 2; + } else { + //printf("Single channel PS/2\n"); + } + + inputbuffer_wait(); + outb(PS2_COMMAND, 0xAB); // tests ps2 ports + outputbuffer_wait(); + if (inb(PS2_DATA) != 0x00) { + printf("PS/2 port 1 failed self test\n"); + --portcount; + } + if (ps2SecondaryEnabled) { + inputbuffer_wait(); + outb(PS2_COMMAND, 0xA9); + outputbuffer_wait(); + if (inb(PS2_DATA) != 0x00) { + printf("PS/2 port 2 failed self test\n"); + --portcount; + } + } + if (!portcount) { + printf("PS/2 initialization failed\n"); + return; + } + + conconfig |= 0b00000011; + inputbuffer_wait(); + outb(PS2_COMMAND, 0xAE); // enable ps2 ports + conconfig |= 0b00000001; + if (ps2SecondaryEnabled) { + inputbuffer_wait(); + outb(PS2_COMMAND, 0xA8); + outb(PS2_COMMAND, 0x60); + conconfig |= 0b00000011; + outb(PS2_DATA, conconfig); + } else { + inputbuffer_wait(); + outb(PS2_COMMAND, 0x60); + conconfig |= 0b00000001; + outb(PS2_DATA, conconfig); + } + + //ps2_dev_send_command(0, 0xFF); // reset devices + //inb(PS2_DATA); + //io_wait(); + if (ps2SecondaryEnabled) { + //ps2_dev_send_command(1, 0xFF); + //inb(PS2_DATA); + //io_wait(); + } + + //printf("PS/2 controller initialized\n"); +} + +// waits for input buffer (write) to be filled +void inputbuffer_wait() { + for (int i = 0; i < 15; i++) { + uint8_t status_reg = inb(PS2_STATUS); + if ((status_reg & 0b00000010)) { + return; + } + io_wait(); + } +} + +// waits for outbut buffer (read) to be filled +void outputbuffer_wait() { + for (int i = 0; i < 15; i++) { + uint8_t status_reg = inb(PS2_STATUS); + if ((status_reg & 0b00000001)) { + return; + } + io_wait(); + } +} + +// checks if inpbut buffer is set +bool inputbuffer_check() { + uint8_t status_reg = inb(PS2_STATUS); + if ((status_reg & 0b00000010)) { + return true; + } else { + return false; + } +} + +bool outputbuffer_check() { + uint8_t status_reg = inb(PS2_STATUS); + if ((status_reg & 0b00000001)) { + return true; + } else { + return false; + } +} + +/* Sends a command byte to a ps2 device +Port numbers start from 0 */ +int ps2_dev_send_command(int port, unsigned char command) { + //bool commandRecieved = false; + switch (port) + { + case 0: + for (int i = 0; i < 5; i++) { + uint8_t status_reg = inb(PS2_STATUS); + if ((status_reg & 0b00000010) == 0b00000000) { + outb(PS2_DATA, command); + return 0; + } + inb(PS2_DATA); + io_wait(); + } + return 1; // error returned if timeout expires + case 1: + outb(PS2_COMMAND, 0xD4); + for (int i = 0; i < 5; i++) { + uint8_t status_reg = inb(PS2_STATUS); + if ((status_reg & 0b00000010) == 0b00000000) { + outb(PS2_DATA, command); + return 0; + } + inb(PS2_DATA); + io_wait(); + } + return 1; + default: + return 2; // out of port range + } +} \ No newline at end of file diff --git a/kernel/arch/x86/tmtty.cpp b/kernel/arch/x86/tmtty.cpp new file mode 100644 index 0000000..d4dc772 --- /dev/null +++ b/kernel/arch/x86/tmtty.cpp @@ -0,0 +1,78 @@ +#include +#include +#include +#include +#include + +#include "vga.h" + +/* Text mode tty */ + +static const size_t VGA_WIDTH = 80; +static const size_t VGA_HEIGHT = 25; +static uint16_t* const VGA_MEMORY = (uint16_t*) 0xB8000; + +size_t terminal_row; +size_t terminal_column; +uint8_t terminal_color; +uint16_t* terminal_buffer; + +void terminal_clear(void) { + terminal_row = 0; + terminal_column = 0; + for (size_t y = 0; y < VGA_HEIGHT; y++) { + for (size_t x = 0; x < VGA_WIDTH; x++) { + const size_t i = y * VGA_WIDTH + x; + terminal_buffer[i] = vga_entry(' ', terminal_color); + } + } +} + +void terminal_initialize(void) { + terminal_color = vga_entry_color(VGA_COLOR_WHITE, VGA_COLOR_BLACK); + terminal_buffer = VGA_MEMORY; + terminal_clear(); +} + +void terminal_setcolor(uint8_t color) { + terminal_color = color; +} + +void terminal_putentryat(char c, uint8_t color, size_t x, size_t y) { + const size_t i = y * VGA_WIDTH + x; + terminal_buffer[i] = vga_entry(c, color); +} + +void terminal_scroll() { + memmove(VGA_MEMORY, (VGA_MEMORY + VGA_WIDTH), (VGA_HEIGHT * VGA_WIDTH) - VGA_WIDTH); + memset(VGA_MEMORY + (VGA_WIDTH * VGA_HEIGHT) - VGA_WIDTH, 0x0, VGA_WIDTH * 2); + --terminal_row; +} + +void terminal_putchar(char c) { + unsigned char uc = c; + if (uc == '\n') { + terminal_column = 0; + if (terminal_row++ == VGA_HEIGHT) + terminal_scroll(); + return; + } + terminal_putentryat(uc, terminal_color, terminal_column, terminal_row); + + if (++terminal_column == VGA_WIDTH) { + terminal_column = 0; + if (++terminal_row == VGA_HEIGHT) { + terminal_scroll(); + } + } +} + +void terminal_write(const char* data, size_t size) { + for (size_t i = 0; i < size; i++) { + terminal_putchar(data[i]); + } +} + +void terminal_writestring(const char* data) { + terminal_write(data, strlen(data)); +} \ No newline at end of file diff --git a/kernel/arch/x86/vga.h b/kernel/arch/x86/vga.h new file mode 100644 index 0000000..5b6b351 --- /dev/null +++ b/kernel/arch/x86/vga.h @@ -0,0 +1,35 @@ +#ifndef ARCH_I386_VGA_H +#define ARCH_I386_VGA_H + +#include + +// hardware text mode constants +enum vga_color { + VGA_COLOR_BLACK = 0, + VGA_COLOR_BLUE = 1, + VGA_COLOR_GREEN = 2, + VGA_COLOR_CYAN = 3, + VGA_COLOR_RED = 4, + VGA_COLOR_MAGENTA = 5, + VGA_COLOR_BROWN = 6, + VGA_COLOR_LIGHT_GREY = 7, + VGA_COLOR_DARK_GREY = 8, + VGA_COLOR_LIGHT_BLUE = 9, + VGA_COLOR_LIGHT_GREEN = 10, + VGA_COLOR_LIGHT_CYAN = 11, + VGA_COLOR_LIGHT_RED = 12, + VGA_COLOR_LIGHT_MAGENTA = 13, + VGA_COLOR_LIGHT_BROWN = 14, + VGA_COLOR_WHITE = 15, +}; + +static inline uint8_t vga_entry_color(enum vga_color fg, enum vga_color bg) { + return fg | bg << 4; +} + +// returns the character/color of a terminal entry +static inline uint16_t vga_entry(unsigned char uc, uint8_t color) { + return (uint16_t) uc | (uint16_t) color << 8; +} + +#endif \ No newline at end of file diff --git a/kernel/makefile b/kernel/makefile index 88659d9..7d02b32 100644 --- a/kernel/makefile +++ b/kernel/makefile @@ -1,25 +1,18 @@ # Kernel makefile -ifndef GAS - GAS = i686-elf-as -endif - ifndef CFLAGS CFLAGS = -ffreestanding -Wall -Wextra -g -std=gnu99 -O2 -Iinclude - CFLAGS += --sysroot="$(SYSROOT)" CFLAGS += -isystem="/usr/include" endif ifndef CXXFLAGS CXXFLAGS = -ffreestanding -Wall -Wextra -fno-exceptions -fno-rtti -D__is_kernel -g -O2 -Iinclude CXXFLAGS += -Iinclude - CXXFLAGS += --sysroot="$(SYSROOT)" CXXFLAGS += -isystem="/usr/include" endif ifndef LDFLAGS LDFLAGS = -T arch/$(ARCH)/linker.ld -ffreestanding -g -O2 -Iinclude - LDFLAGS += --sysroot="$(SYSROOT)" LDFLAGS += -isystem="/usr/include" endif diff --git a/makefile b/makefile index ccd252c..c52dd4c 100644 --- a/makefile +++ b/makefile @@ -1,16 +1,16 @@ # The makefile OS_NAME := emuos -ARCH = i386 +ARCH ?= x86_64 export SYSROOT = $(PWD)/sysroot # Variables for easy access of tools like gcc and nasm -export CC = i686-elf-gcc -export CXX = i686-elf-g++ -export LD = i686-elf-ld +export CC = $(ARCH)-elf-gcc +export CXX = $(ARCH)-elf-g++ +export LD = $(ARCH)-elf-ld export NASM = nasm -QEMU = qemu-system-i386 -ASMFLAGS = -felf32 +QEMU = qemu-system-x86_64 +#ASMFLAGS = -felf32 #CXXFLAGS := -ffreestanding -O2 -Wall -Wextra -fno-exceptions -fno-rtti #LDFLAGS := -ffreestanding -O2 -nostdlib -- cgit v1.2.3-70-g09d2 From 4aa74dbe2a35a45668b33d688f17b680c2232572 Mon Sep 17 00:00:00 2001 From: EmulatedSeasons <89668582+EmulatedSeasons@users.noreply.github.com> Date: Sun, 5 May 2024 01:08:00 -0400 Subject: improved makefile somewhat --- .gitignore | 6 +- isodir/boot/grub/grub.cfg | 9 + isodir/boot/limine/limine.cfg | 6 + kernel/arch/x86_64/crti.asm | 12 + kernel/arch/x86_64/crtn.asm | 9 + kernel/arch/x86_64/limine.h | 587 +++++++++++++++++++++++++++++++++++++++++ kernel/makefile | 35 ++- libc/README.txt | 5 + libc/abort.c | 11 + libc/include/stdio.h | 18 ++ libc/include/stdlib.h | 15 ++ libc/include/string.h | 20 ++ libc/include/sys/cdefs.h | 6 + libc/makefile | 79 ++++++ libc/memcmp.c | 13 + libc/memcpy.c | 9 + libc/memmove.c | 14 + libc/memset.c | 8 + libc/printf.c | 191 ++++++++++++++ libc/putchar.c | 9 + libc/puts.c | 5 + libc/strlen.c | 9 + libs/libck/README.txt | 5 - libs/libck/abort.c | 11 - libs/libck/include/stdio.h | 18 -- libs/libck/include/stdlib.h | 15 -- libs/libck/include/string.h | 20 -- libs/libck/include/sys/cdefs.h | 6 - libs/libck/makefile | 79 ------ libs/libck/memcmp.c | 13 - libs/libck/memcpy.c | 9 - libs/libck/memmove.c | 14 - libs/libck/memset.c | 8 - libs/libck/printf.c | 191 -------------- libs/libck/putchar.c | 9 - libs/libck/puts.c | 5 - libs/libck/strlen.c | 9 - makefile | 35 +-- 38 files changed, 1073 insertions(+), 450 deletions(-) create mode 100644 isodir/boot/grub/grub.cfg create mode 100644 isodir/boot/limine/limine.cfg create mode 100644 kernel/arch/x86_64/crti.asm create mode 100644 kernel/arch/x86_64/crtn.asm create mode 100644 kernel/arch/x86_64/limine.h create mode 100644 libc/README.txt create mode 100644 libc/abort.c create mode 100644 libc/include/stdio.h create mode 100644 libc/include/stdlib.h create mode 100644 libc/include/string.h create mode 100644 libc/include/sys/cdefs.h create mode 100644 libc/makefile create mode 100644 libc/memcmp.c create mode 100644 libc/memcpy.c create mode 100644 libc/memmove.c create mode 100644 libc/memset.c create mode 100644 libc/printf.c create mode 100644 libc/putchar.c create mode 100644 libc/puts.c create mode 100644 libc/strlen.c delete mode 100644 libs/libck/README.txt delete mode 100644 libs/libck/abort.c delete mode 100644 libs/libck/include/stdio.h delete mode 100644 libs/libck/include/stdlib.h delete mode 100644 libs/libck/include/string.h delete mode 100644 libs/libck/include/sys/cdefs.h delete mode 100644 libs/libck/makefile delete mode 100644 libs/libck/memcmp.c delete mode 100644 libs/libck/memcpy.c delete mode 100644 libs/libck/memmove.c delete mode 100644 libs/libck/memset.c delete mode 100644 libs/libck/printf.c delete mode 100644 libs/libck/putchar.c delete mode 100644 libs/libck/puts.c delete mode 100644 libs/libck/strlen.c (limited to 'makefile') diff --git a/.gitignore b/.gitignore index cc56a26..be76e0b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,4 @@ # directories that have to exist but shouldn't be added to git -isodir/ sysroot/ build/ @@ -9,12 +8,15 @@ build/ *.o *.d *.a +*.sys +*.efi # i have no idea what this is for bx_enh_dbg.ini -# temporarily ignored cause i have no idea how to set up cmake with this and dont really have much else to use it for yet misc/ # not important make.png + +.vscode/ diff --git a/isodir/boot/grub/grub.cfg b/isodir/boot/grub/grub.cfg new file mode 100644 index 0000000..71b0917 --- /dev/null +++ b/isodir/boot/grub/grub.cfg @@ -0,0 +1,9 @@ +set timeout_style=menu +if [ "${timeout}" = 0 ]; then + #set timeout=10 # comment this original + set timeout=0 # add this +fi + +menuentry "emuos" { + multiboot /boot/kernel.bin +} \ No newline at end of file diff --git a/isodir/boot/limine/limine.cfg b/isodir/boot/limine/limine.cfg new file mode 100644 index 0000000..dd5e60b --- /dev/null +++ b/isodir/boot/limine/limine.cfg @@ -0,0 +1,6 @@ +TIMEOUT=5 + +:emuos + PROTOCOL=limine + KERNEL_PATH=boot:///boot/kernel.bin + KASLR=off \ No newline at end of file diff --git a/kernel/arch/x86_64/crti.asm b/kernel/arch/x86_64/crti.asm new file mode 100644 index 0000000..73ac499 --- /dev/null +++ b/kernel/arch/x86_64/crti.asm @@ -0,0 +1,12 @@ +; x86 crti.asm +section .init +global _init:function +_init: + push rbp + mov rbp, rsp + +section .fini +global _fini:function +_fini: + push rbp + mov rbp, rsp \ No newline at end of file diff --git a/kernel/arch/x86_64/crtn.asm b/kernel/arch/x86_64/crtn.asm new file mode 100644 index 0000000..322b3a2 --- /dev/null +++ b/kernel/arch/x86_64/crtn.asm @@ -0,0 +1,9 @@ +; x86 crtn.asm + +section .init + pop rbp + ret + +section .fini + pop rbp + ret \ No newline at end of file diff --git a/kernel/arch/x86_64/limine.h b/kernel/arch/x86_64/limine.h new file mode 100644 index 0000000..0e7e87a --- /dev/null +++ b/kernel/arch/x86_64/limine.h @@ -0,0 +1,587 @@ +/* BSD Zero Clause License */ + +/* Copyright (C) 2022-2024 mintsuki and contributors. + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION + * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN + * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef LIMINE_H +#define LIMINE_H 1 + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +/* Misc */ + +#ifdef LIMINE_NO_POINTERS +# define LIMINE_PTR(TYPE) uint64_t +#else +# define LIMINE_PTR(TYPE) TYPE +#endif + +#ifdef __GNUC__ +# define LIMINE_DEPRECATED __attribute__((__deprecated__)) +# define LIMINE_DEPRECATED_IGNORE_START \ + _Pragma("GCC diagnostic push") \ + _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"") +# define LIMINE_DEPRECATED_IGNORE_END \ + _Pragma("GCC diagnostic pop") +#else +# define LIMINE_DEPRECATED +# define LIMINE_DEPRECATED_IGNORE_START +# define LIMINE_DEPRECATED_IGNORE_END +#endif + +#define LIMINE_REQUESTS_START_MARKER \ + uint64_t limine_requests_start_marker[4] = { 0xf6b8f4b39de7d1ae, 0xfab91a6940fcb9cf, \ + 0x785c6ed015d3e316, 0x181e920a7852b9d9 }; +#define LIMINE_REQUESTS_END_MARKER \ + uint64_t limine_requests_end_marker[2] = { 0xadc0e0531bb10d03, 0x9572709f31764c62 }; + +#define LIMINE_REQUESTS_DELIMITER LIMINE_REQUESTS_END_MARKER + +#define LIMINE_BASE_REVISION(N) \ + uint64_t limine_base_revision[3] = { 0xf9562b2d5c95a6c8, 0x6a7b384944536bdc, (N) }; + +#define LIMINE_BASE_REVISION_SUPPORTED (limine_base_revision[2] == 0) + +#define LIMINE_COMMON_MAGIC 0xc7b1dd30df4c8b88, 0x0a82e883a194f07b + +struct limine_uuid { + uint32_t a; + uint16_t b; + uint16_t c; + uint8_t d[8]; +}; + +#define LIMINE_MEDIA_TYPE_GENERIC 0 +#define LIMINE_MEDIA_TYPE_OPTICAL 1 +#define LIMINE_MEDIA_TYPE_TFTP 2 + +struct limine_file { + uint64_t revision; + LIMINE_PTR(void *) address; + uint64_t size; + LIMINE_PTR(char *) path; + LIMINE_PTR(char *) cmdline; + uint32_t media_type; + uint32_t unused; + uint32_t tftp_ip; + uint32_t tftp_port; + uint32_t partition_index; + uint32_t mbr_disk_id; + struct limine_uuid gpt_disk_uuid; + struct limine_uuid gpt_part_uuid; + struct limine_uuid part_uuid; +}; + +/* Boot info */ + +#define LIMINE_BOOTLOADER_INFO_REQUEST { LIMINE_COMMON_MAGIC, 0xf55038d8e2a1202f, 0x279426fcf5f59740 } + +struct limine_bootloader_info_response { + uint64_t revision; + LIMINE_PTR(char *) name; + LIMINE_PTR(char *) version; +}; + +struct limine_bootloader_info_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_bootloader_info_response *) response; +}; + +/* Stack size */ + +#define LIMINE_STACK_SIZE_REQUEST { LIMINE_COMMON_MAGIC, 0x224ef0460a8e8926, 0xe1cb0fc25f46ea3d } + +struct limine_stack_size_response { + uint64_t revision; +}; + +struct limine_stack_size_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_stack_size_response *) response; + uint64_t stack_size; +}; + +/* HHDM */ + +#define LIMINE_HHDM_REQUEST { LIMINE_COMMON_MAGIC, 0x48dcf1cb8ad2b852, 0x63984e959a98244b } + +struct limine_hhdm_response { + uint64_t revision; + uint64_t offset; +}; + +struct limine_hhdm_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_hhdm_response *) response; +}; + +/* Framebuffer */ + +#define LIMINE_FRAMEBUFFER_REQUEST { LIMINE_COMMON_MAGIC, 0x9d5827dcd881dd75, 0xa3148604f6fab11b } + +#define LIMINE_FRAMEBUFFER_RGB 1 + +struct limine_video_mode { + uint64_t pitch; + uint64_t width; + uint64_t height; + uint16_t bpp; + uint8_t memory_model; + uint8_t red_mask_size; + uint8_t red_mask_shift; + uint8_t green_mask_size; + uint8_t green_mask_shift; + uint8_t blue_mask_size; + uint8_t blue_mask_shift; +}; + +struct limine_framebuffer { + LIMINE_PTR(void *) address; + uint64_t width; + uint64_t height; + uint64_t pitch; + uint16_t bpp; + uint8_t memory_model; + uint8_t red_mask_size; + uint8_t red_mask_shift; + uint8_t green_mask_size; + uint8_t green_mask_shift; + uint8_t blue_mask_size; + uint8_t blue_mask_shift; + uint8_t unused[7]; + uint64_t edid_size; + LIMINE_PTR(void *) edid; + /* Response revision 1 */ + uint64_t mode_count; + LIMINE_PTR(struct limine_video_mode **) modes; +}; + +struct limine_framebuffer_response { + uint64_t revision; + uint64_t framebuffer_count; + LIMINE_PTR(struct limine_framebuffer **) framebuffers; +}; + +struct limine_framebuffer_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_framebuffer_response *) response; +}; + +/* Terminal */ + +#define LIMINE_TERMINAL_REQUEST { LIMINE_COMMON_MAGIC, 0xc8ac59310c2b0844, 0xa68d0c7265d38878 } + +#define LIMINE_TERMINAL_CB_DEC 10 +#define LIMINE_TERMINAL_CB_BELL 20 +#define LIMINE_TERMINAL_CB_PRIVATE_ID 30 +#define LIMINE_TERMINAL_CB_STATUS_REPORT 40 +#define LIMINE_TERMINAL_CB_POS_REPORT 50 +#define LIMINE_TERMINAL_CB_KBD_LEDS 60 +#define LIMINE_TERMINAL_CB_MODE 70 +#define LIMINE_TERMINAL_CB_LINUX 80 + +#define LIMINE_TERMINAL_CTX_SIZE ((uint64_t)(-1)) +#define LIMINE_TERMINAL_CTX_SAVE ((uint64_t)(-2)) +#define LIMINE_TERMINAL_CTX_RESTORE ((uint64_t)(-3)) +#define LIMINE_TERMINAL_FULL_REFRESH ((uint64_t)(-4)) + +/* Response revision 1 */ +#define LIMINE_TERMINAL_OOB_OUTPUT_GET ((uint64_t)(-10)) +#define LIMINE_TERMINAL_OOB_OUTPUT_SET ((uint64_t)(-11)) + +#define LIMINE_TERMINAL_OOB_OUTPUT_OCRNL (1 << 0) +#define LIMINE_TERMINAL_OOB_OUTPUT_OFDEL (1 << 1) +#define LIMINE_TERMINAL_OOB_OUTPUT_OFILL (1 << 2) +#define LIMINE_TERMINAL_OOB_OUTPUT_OLCUC (1 << 3) +#define LIMINE_TERMINAL_OOB_OUTPUT_ONLCR (1 << 4) +#define LIMINE_TERMINAL_OOB_OUTPUT_ONLRET (1 << 5) +#define LIMINE_TERMINAL_OOB_OUTPUT_ONOCR (1 << 6) +#define LIMINE_TERMINAL_OOB_OUTPUT_OPOST (1 << 7) + +LIMINE_DEPRECATED_IGNORE_START + +struct LIMINE_DEPRECATED limine_terminal; + +typedef void (*limine_terminal_write)(struct limine_terminal *, const char *, uint64_t); +typedef void (*limine_terminal_callback)(struct limine_terminal *, uint64_t, uint64_t, uint64_t, uint64_t); + +struct LIMINE_DEPRECATED limine_terminal { + uint64_t columns; + uint64_t rows; + LIMINE_PTR(struct limine_framebuffer *) framebuffer; +}; + +struct LIMINE_DEPRECATED limine_terminal_response { + uint64_t revision; + uint64_t terminal_count; + LIMINE_PTR(struct limine_terminal **) terminals; + LIMINE_PTR(limine_terminal_write) write; +}; + +struct LIMINE_DEPRECATED limine_terminal_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_terminal_response *) response; + LIMINE_PTR(limine_terminal_callback) callback; +}; + +LIMINE_DEPRECATED_IGNORE_END + +/* Paging mode */ + +#define LIMINE_PAGING_MODE_REQUEST { LIMINE_COMMON_MAGIC, 0x95c1a0edab0944cb, 0xa4e5cb3842f7488a } + +#if defined (__x86_64__) || defined (__i386__) +#define LIMINE_PAGING_MODE_X86_64_4LVL 0 +#define LIMINE_PAGING_MODE_X86_64_5LVL 1 +#define LIMINE_PAGING_MODE_MAX LIMINE_PAGING_MODE_X86_64_5LVL +#define LIMINE_PAGING_MODE_DEFAULT LIMINE_PAGING_MODE_X86_64_4LVL +#elif defined (__aarch64__) +#define LIMINE_PAGING_MODE_AARCH64_4LVL 0 +#define LIMINE_PAGING_MODE_AARCH64_5LVL 1 +#define LIMINE_PAGING_MODE_MAX LIMINE_PAGING_MODE_AARCH64_5LVL +#define LIMINE_PAGING_MODE_DEFAULT LIMINE_PAGING_MODE_AARCH64_4LVL +#elif defined (__riscv) && (__riscv_xlen == 64) +#define LIMINE_PAGING_MODE_RISCV_SV39 0 +#define LIMINE_PAGING_MODE_RISCV_SV48 1 +#define LIMINE_PAGING_MODE_RISCV_SV57 2 +#define LIMINE_PAGING_MODE_MAX LIMINE_PAGING_MODE_RISCV_SV57 +#define LIMINE_PAGING_MODE_DEFAULT LIMINE_PAGING_MODE_RISCV_SV48 +#else +#error Unknown architecture +#endif + +struct limine_paging_mode_response { + uint64_t revision; + uint64_t mode; + uint64_t flags; +}; + +struct limine_paging_mode_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_paging_mode_response *) response; + uint64_t mode; + uint64_t flags; +}; + +/* 5-level paging */ + +#define LIMINE_5_LEVEL_PAGING_REQUEST { LIMINE_COMMON_MAGIC, 0x94469551da9b3192, 0xebe5e86db7382888 } + +LIMINE_DEPRECATED_IGNORE_START + +struct LIMINE_DEPRECATED limine_5_level_paging_response { + uint64_t revision; +}; + +struct LIMINE_DEPRECATED limine_5_level_paging_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_5_level_paging_response *) response; +}; + +LIMINE_DEPRECATED_IGNORE_END + +/* SMP */ + +#define LIMINE_SMP_REQUEST { LIMINE_COMMON_MAGIC, 0x95a67b819a1b857e, 0xa0b61b723b6a73e0 } + +struct limine_smp_info; + +typedef void (*limine_goto_address)(struct limine_smp_info *); + +#if defined (__x86_64__) || defined (__i386__) + +#define LIMINE_SMP_X2APIC (1 << 0) + +struct limine_smp_info { + uint32_t processor_id; + uint32_t lapic_id; + uint64_t reserved; + LIMINE_PTR(limine_goto_address) goto_address; + uint64_t extra_argument; +}; + +struct limine_smp_response { + uint64_t revision; + uint32_t flags; + uint32_t bsp_lapic_id; + uint64_t cpu_count; + LIMINE_PTR(struct limine_smp_info **) cpus; +}; + +#elif defined (__aarch64__) + +struct limine_smp_info { + uint32_t processor_id; + uint32_t gic_iface_no; + uint64_t mpidr; + uint64_t reserved; + LIMINE_PTR(limine_goto_address) goto_address; + uint64_t extra_argument; +}; + +struct limine_smp_response { + uint64_t revision; + uint64_t flags; + uint64_t bsp_mpidr; + uint64_t cpu_count; + LIMINE_PTR(struct limine_smp_info **) cpus; +}; + +#elif defined (__riscv) && (__riscv_xlen == 64) + +struct limine_smp_info { + uint64_t processor_id; + uint64_t hartid; + uint64_t reserved; + LIMINE_PTR(limine_goto_address) goto_address; + uint64_t extra_argument; +}; + +struct limine_smp_response { + uint64_t revision; + uint64_t flags; + uint64_t bsp_hartid; + uint64_t cpu_count; + LIMINE_PTR(struct limine_smp_info **) cpus; +}; + +#else +#error Unknown architecture +#endif + +struct limine_smp_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_smp_response *) response; + uint64_t flags; +}; + +/* Memory map */ + +#define LIMINE_MEMMAP_REQUEST { LIMINE_COMMON_MAGIC, 0x67cf3d9d378a806f, 0xe304acdfc50c3c62 } + +#define LIMINE_MEMMAP_USABLE 0 +#define LIMINE_MEMMAP_RESERVED 1 +#define LIMINE_MEMMAP_ACPI_RECLAIMABLE 2 +#define LIMINE_MEMMAP_ACPI_NVS 3 +#define LIMINE_MEMMAP_BAD_MEMORY 4 +#define LIMINE_MEMMAP_BOOTLOADER_RECLAIMABLE 5 +#define LIMINE_MEMMAP_KERNEL_AND_MODULES 6 +#define LIMINE_MEMMAP_FRAMEBUFFER 7 + +struct limine_memmap_entry { + uint64_t base; + uint64_t length; + uint64_t type; +}; + +struct limine_memmap_response { + uint64_t revision; + uint64_t entry_count; + LIMINE_PTR(struct limine_memmap_entry **) entries; +}; + +struct limine_memmap_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_memmap_response *) response; +}; + +/* Entry point */ + +#define LIMINE_ENTRY_POINT_REQUEST { LIMINE_COMMON_MAGIC, 0x13d86c035a1cd3e1, 0x2b0caa89d8f3026a } + +typedef void (*limine_entry_point)(void); + +struct limine_entry_point_response { + uint64_t revision; +}; + +struct limine_entry_point_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_entry_point_response *) response; + LIMINE_PTR(limine_entry_point) entry; +}; + +/* Kernel File */ + +#define LIMINE_KERNEL_FILE_REQUEST { LIMINE_COMMON_MAGIC, 0xad97e90e83f1ed67, 0x31eb5d1c5ff23b69 } + +struct limine_kernel_file_response { + uint64_t revision; + LIMINE_PTR(struct limine_file *) kernel_file; +}; + +struct limine_kernel_file_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_kernel_file_response *) response; +}; + +/* Module */ + +#define LIMINE_MODULE_REQUEST { LIMINE_COMMON_MAGIC, 0x3e7e279702be32af, 0xca1c4f3bd1280cee } + +#define LIMINE_INTERNAL_MODULE_REQUIRED (1 << 0) +#define LIMINE_INTERNAL_MODULE_COMPRESSED (1 << 1) + +struct limine_internal_module { + LIMINE_PTR(const char *) path; + LIMINE_PTR(const char *) cmdline; + uint64_t flags; +}; + +struct limine_module_response { + uint64_t revision; + uint64_t module_count; + LIMINE_PTR(struct limine_file **) modules; +}; + +struct limine_module_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_module_response *) response; + + /* Request revision 1 */ + uint64_t internal_module_count; + LIMINE_PTR(struct limine_internal_module **) internal_modules; +}; + +/* RSDP */ + +#define LIMINE_RSDP_REQUEST { LIMINE_COMMON_MAGIC, 0xc5e77b6b397e7b43, 0x27637845accdcf3c } + +struct limine_rsdp_response { + uint64_t revision; + LIMINE_PTR(void *) address; +}; + +struct limine_rsdp_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_rsdp_response *) response; +}; + +/* SMBIOS */ + +#define LIMINE_SMBIOS_REQUEST { LIMINE_COMMON_MAGIC, 0x9e9046f11e095391, 0xaa4a520fefbde5ee } + +struct limine_smbios_response { + uint64_t revision; + LIMINE_PTR(void *) entry_32; + LIMINE_PTR(void *) entry_64; +}; + +struct limine_smbios_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_smbios_response *) response; +}; + +/* EFI system table */ + +#define LIMINE_EFI_SYSTEM_TABLE_REQUEST { LIMINE_COMMON_MAGIC, 0x5ceba5163eaaf6d6, 0x0a6981610cf65fcc } + +struct limine_efi_system_table_response { + uint64_t revision; + LIMINE_PTR(void *) address; +}; + +struct limine_efi_system_table_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_efi_system_table_response *) response; +}; + +/* EFI memory map */ + +#define LIMINE_EFI_MEMMAP_REQUEST { LIMINE_COMMON_MAGIC, 0x7df62a431d6872d5, 0xa4fcdfb3e57306c8 } + +struct limine_efi_memmap_response { + uint64_t revision; + LIMINE_PTR(void *) memmap; + uint64_t memmap_size; + uint64_t desc_size; + uint64_t desc_version; +}; + +struct limine_efi_memmap_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_efi_memmap_response *) response; +}; + +/* Boot time */ + +#define LIMINE_BOOT_TIME_REQUEST { LIMINE_COMMON_MAGIC, 0x502746e184c088aa, 0xfbc5ec83e6327893 } + +struct limine_boot_time_response { + uint64_t revision; + int64_t boot_time; +}; + +struct limine_boot_time_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_boot_time_response *) response; +}; + +/* Kernel address */ + +#define LIMINE_KERNEL_ADDRESS_REQUEST { LIMINE_COMMON_MAGIC, 0x71ba76863cc55f63, 0xb2644a48c516a487 } + +struct limine_kernel_address_response { + uint64_t revision; + uint64_t physical_base; + uint64_t virtual_base; +}; + +struct limine_kernel_address_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_kernel_address_response *) response; +}; + +/* Device Tree Blob */ + +#define LIMINE_DTB_REQUEST { LIMINE_COMMON_MAGIC, 0xb40ddb48fb54bac7, 0x545081493f81ffb7 } + +struct limine_dtb_response { + uint64_t revision; + LIMINE_PTR(void *) dtb_ptr; +}; + +struct limine_dtb_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_dtb_response *) response; +}; + +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file diff --git a/kernel/makefile b/kernel/makefile index 7d02b32..b2a2660 100644 --- a/kernel/makefile +++ b/kernel/makefile @@ -1,19 +1,15 @@ # Kernel makefile -ifndef CFLAGS - CFLAGS = -ffreestanding -Wall -Wextra -g -std=gnu99 -O2 -Iinclude - CFLAGS += -isystem="/usr/include" -endif - -ifndef CXXFLAGS - CXXFLAGS = -ffreestanding -Wall -Wextra -fno-exceptions -fno-rtti -D__is_kernel -g -O2 -Iinclude - CXXFLAGS += -Iinclude - CXXFLAGS += -isystem="/usr/include" -endif - -ifndef LDFLAGS - LDFLAGS = -T arch/$(ARCH)/linker.ld -ffreestanding -g -O2 -Iinclude - LDFLAGS += -isystem="/usr/include" +CFLAGS := -ffreestanding -Wall -Wextra -g -std=gnu99 -O2 -Iinclude +CXXFLAGS := -ffreestanding -Wall -Wextra -fno-exceptions -fno-rtti -g -O2 -Iinclude +LDFLAGS := -T arch/$(ARCH)/linker.ld -ffreestanding -g -O2 -Iinclude +ASMFLAGS := + +ifeq ($(ARCH),x86_64) +CFLAGS += -mno-red-zone +CXXFLAGS += -mno-red-zone +LDFLAGS += -mno-red-zone +ASMFLAGS += -felf64 endif ifndef SYSROOT @@ -21,14 +17,13 @@ ifndef SYSROOT endif -LIBS = -nostdlib -lgcc -lck +LIBS = -nostdlib -lck -lgcc #Find all the source files C_SOURCES := $(shell find $(PWD)/kernel -type f -name '*.c') CPP_SOURCES := $(shell find $(PWD)/kernel -type f -name '*.cpp') HEADERS := $(shell find $(PWD) -type f -name '*.h') ASMFILES := $(shell find $(PWD) -type f -name '*.asm' ! -name 'crti.asm' ! -name 'crtn.asm') -GASFILES := $(wildcard *.s) CRTBEGIN := $(shell $(CXX) $(CXXFLAGS) -print-file-name=crtbegin.o) CRTEND := $(shell $(CXX) $(CXXFLAGS) -print-file-name=crtend.o) @@ -54,7 +49,7 @@ all: crti.o crtn.o kernel.bin kernel.bin: ${OBJECTS} $(info [INFO] Linking kernel) - $(CXX) ${LDFLAGS} -o ${PREFIX}/kernel.bin $(LINKLST) + $(CXX) ${LDFLAGS} -o $@ $(LINKLST) %.o: %.cpp $(info [INFO] Compiling $<) @@ -66,13 +61,13 @@ kernel.bin: ${OBJECTS} %.o: %.asm $(info [INFO] Assembling $<) - $(NASM) $< -felf32 -o $@ -g + $(NASM) $< $(ASMFLAGS) -o $@ -g crti.o: - $(NASM) arch/$(ARCH)/crti.asm -felf32 -o $@ + $(NASM) arch/$(ARCH)/crti.asm $(ASMFLAGS) -o $@ crtn.o: - $(NASM) arch/$(ARCH)/crtn.asm -felf32 -o $@ + $(NASM) arch/$(ARCH)/crtn.asm $(ASMFLAGS) -o $@ install-headers: cp -r --preserve=timestamps include/. $(SYSROOT)/usr/include diff --git a/libc/README.txt b/libc/README.txt new file mode 100644 index 0000000..6806bd4 --- /dev/null +++ b/libc/README.txt @@ -0,0 +1,5 @@ +a temporary(?) libc for the kernel + +code based off meaty skeleton from osdev.org + +todo: \ No newline at end of file diff --git a/libc/abort.c b/libc/abort.c new file mode 100644 index 0000000..83873e8 --- /dev/null +++ b/libc/abort.c @@ -0,0 +1,11 @@ +#include +#include + +__attribute__((__noreturn__)) +void abort(void) { + // TODO: Add proper kernel panic. + printf("kernel: panic: abort()\n"); + asm volatile("hlt"); + while (1) { } + __builtin_unreachable(); +} \ No newline at end of file diff --git a/libc/include/stdio.h b/libc/include/stdio.h new file mode 100644 index 0000000..eb4e45b --- /dev/null +++ b/libc/include/stdio.h @@ -0,0 +1,18 @@ +#ifndef _STDIO_H +#define _STDIO_H 1 + +#define EOF (-1) + +#ifdef __cplusplus +extern "C" { +#endif + +int printf(const char* __restrict, ...); +int putchar(int); +int puts(const char*); + +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file diff --git a/libc/include/stdlib.h b/libc/include/stdlib.h new file mode 100644 index 0000000..1d1ee32 --- /dev/null +++ b/libc/include/stdlib.h @@ -0,0 +1,15 @@ +#ifndef _STDLIB_H +#define _STDLIB_H 1 + +#ifdef __cplusplus +extern "C" { +#endif + +__attribute__((__noreturn__)) +void abort(void); + +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file diff --git a/libc/include/string.h b/libc/include/string.h new file mode 100644 index 0000000..4f6b5bd --- /dev/null +++ b/libc/include/string.h @@ -0,0 +1,20 @@ +#ifndef _STRING_H +#define _STRING_H 1 + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +int memcmp(const void*, const void*, size_t); +void* memcpy(void* __restrict, const void* __restrict, size_t); +void* memmove(void*, const void*, size_t); +void* memset(void*, int, size_t); +size_t strlen(const char*); + +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file diff --git a/libc/include/sys/cdefs.h b/libc/include/sys/cdefs.h new file mode 100644 index 0000000..1e2c277 --- /dev/null +++ b/libc/include/sys/cdefs.h @@ -0,0 +1,6 @@ +#ifndef _SYS_CDEFS_H +#define _SYS_CDEFS_H 1 + + + +#endif \ No newline at end of file diff --git a/libc/makefile b/libc/makefile new file mode 100644 index 0000000..2e1912e --- /dev/null +++ b/libc/makefile @@ -0,0 +1,79 @@ +# libck makefile + +ifndef AR + AR = i686-elf-ar +endif + +ifndef CFLAGS + CFLAGS = -ffreestanding -Wall -Wextra -g -std=gnu11 -O2 -Iinclude + CFLAGS += --sysroot="$(SYSROOT)" + CFLAGS += -isystem="/usr/include" +endif + +ifndef CXXFLAGS + CXXFLAGS = -ffreestanding -Wall -Wextra -fno-exceptions -fno-rtti -D__is_kernel -g -O2 -Iinclude + CXXFLAGS += -Iinclude + CXXFLAGS += --sysroot="$(SYSROOT)" + CXXFLAGS += -isystem="/usr/include" +endif + +ifndef LDFLAGS + LDFLAGS = -T arch/$(ARCH)/linker.ld -ffreestanding -g -O2 -Iinclude + LDFLAGS += --sysroot="$(SYSROOT)" + LDFLAGS += -isystem="/usr/include" +endif + +ifndef SYSROOT + $(error No sysroot specified) +endif + + +LIBS = -nostdlib -lgcc + +#Find all the source files +C_SOURCES := $(shell find $(PWD) -type f -name '*.c') +#CPP_SOURCES := $(shell find $(PWD)/kernel -type f -name '*.cpp') +HEADERS := $(shell find $(PWD) -type f -name '*.h') +#ASMFILES := $(shell find $(PWD) -type f -name '*.asm') + +OBJECTS := $(patsubst %.c,%.o,$(C_SOURCES)) +# OBJECTS += $(patsubst %.cpp,%.o,$(CPP_SOURCES)) +# OBJECTS += $(patsubst %.asm,%.o,$(ASMFILES)) + +DEPFILES := $(patsubst %.c,%.d,$(C_SOURCES)) +# DEPFILES += $(patsubst %.cpp,%.d,$(CPP_SOURCES)) + +LINKLST += $(OBJECTS) +LINKLST += $(LIBS) + +-include $(DEPFILES) + +.PHONY: all install-headers install-lib clean + +all: libc.a install-lib + +libc.a: ${OBJECTS} + $(info [INFO] Linking $<) + $(AR) rcs $@ $(OBJECTS) + +%.o: %.cpp + $(info [INFO] Compiling $<) + $(CXX) ${CXXFLAGS} -MMD -MP -c $< -o $@ + +%.o: %.c + $(info [INFO] Compiling $<) + $(CC) $(CFLAGS) -MMD -MP -c $< -o $@ + +%.o: %.asm + $(info [INFO] Assembling $<) + $(NASM) $< -felf32 -o $@ -g + +install-headers: + cp -r --preserve=timestamps include/. $(SYSROOT)/usr/include + +install-lib: libck.a + cp -r $< $(SYSROOT)/usr/lib/. + +clean: + $(info [INFO] Cleaning) + $(RM) ${OBJECTS} $(DEPFILES) \ No newline at end of file diff --git a/libc/memcmp.c b/libc/memcmp.c new file mode 100644 index 0000000..93141e1 --- /dev/null +++ b/libc/memcmp.c @@ -0,0 +1,13 @@ +#include + +int memcmp(const void* aptr, const void* bptr, size_t size) { + const unsigned char* a = (const unsigned char*) aptr; + const unsigned char* b = (const unsigned char*) bptr; + for (size_t i = 0; i < size; i++) { + if (a[i] < b[i]) + return -1; + else if (b[i] < a[i]) + return 1; + } + return 0; +} \ No newline at end of file diff --git a/libc/memcpy.c b/libc/memcpy.c new file mode 100644 index 0000000..7e09be7 --- /dev/null +++ b/libc/memcpy.c @@ -0,0 +1,9 @@ +#include + +void* memcpy(void* restrict dstptr, const void* restrict srcptr, size_t size) { + unsigned char* dst = (unsigned char*) dstptr; + const unsigned char* src = (const unsigned char*) srcptr; + for (size_t i=0; i < size; i++) + dst[i] = src[i]; + return dstptr; +} \ No newline at end of file diff --git a/libc/memmove.c b/libc/memmove.c new file mode 100644 index 0000000..98c787f --- /dev/null +++ b/libc/memmove.c @@ -0,0 +1,14 @@ +#include + +void* memmove(void* dstptr, const void* srcptr, size_t size) { + unsigned char* dst = (unsigned char*) dstptr; + const unsigned char* src = (const unsigned char*) srcptr; + if (dst < src) { + for (size_t i = 0; i < size; i++) + dst[i] = src[i]; + } else { + for (size_t i = size; i != 0; i--) + dst[i-1] = src[i-1]; + } + return dstptr; +} \ No newline at end of file diff --git a/libc/memset.c b/libc/memset.c new file mode 100644 index 0000000..67dadfe --- /dev/null +++ b/libc/memset.c @@ -0,0 +1,8 @@ +#include + +void* memset(void* bufptr, int value, size_t size) { + unsigned char* buf = (unsigned char*) bufptr; + for (size_t i = 0; i < size; i++) + buf[i] = (unsigned char) value; + return bufptr; +} \ No newline at end of file diff --git a/libc/printf.c b/libc/printf.c new file mode 100644 index 0000000..72a4749 --- /dev/null +++ b/libc/printf.c @@ -0,0 +1,191 @@ +#include +#include +#include +#include +#include +#include + +static bool print(const char* data, size_t length) { + const unsigned char* bytes = (const unsigned char*) data; + for (size_t i = 0; i < length; i++) + if (putchar(bytes[i]) == EOF) + return false; + return true; +} + +// for %d and %x. +static const char* itoa(unsigned int num, char* str, unsigned int base) { + int cou = 0; // nt + + if (num == 0) { // 0 + str[cou] = '0'; + str[++cou] = '\0'; + return str; + } + + while (num) + { + unsigned int rem = (num % base); + // brings the number up to ascii 0 + the digit + if (rem > 9) { + str[cou] = rem + '7'; // 7 = 55 + 10(A) = 65 + } else { + str[cou] = rem + '0'; + } + num /= base; + ++cou; + } + + // reverse the string + for (int i = 0; i < cou; i++) { + char temp = str[i]; + str[i] = str[cou - i - 1]; + str[cou - 1] = temp; + } + + str[cou] = '\0'; + return str; +} + +// have no idea if there's a better way to do this. for %lx +static const char* itoa_u64(unsigned long long num, char* str, int base) { + int cou = 0; // nt + + if (num == 0) { // 0 + str[cou] = '0'; + str[++cou] = '\0'; + return str; + } + + while (num) + { + unsigned long long rem = num % base; + // brings the number up to ascii 0 + the digit + if (rem > 9) { + str[cou] = rem + '7'; // 7 = 55 + 10(A) = 65 + } else { + str[cou] = rem + '0'; + } + num /= base; + ++cou; + } + + // reverse the string + for (int i = 0; i < cou; i++) { + char temp = str[i]; + str[i] = str[cou - i - 1]; + str[cou - 1] = temp; + } + + str[cou] = '\0'; + return str; +} + +int printf(const char* restrict format, ...) { + va_list parameters; + va_start(parameters, format); + + int written = 0; + + while (*format != '\0') { + size_t maxrem = INT_MAX - written; + + if (format[0] != '%' || format[1] == '%') { + if (format[0] == '%') + format++; + size_t amount = 1; + while (format[amount] && format[amount] != '%') + amount++; + if (maxrem < amount) { + // TODO: Set errno to EOVERFLOW. + return -1; + } + if (!print(format, amount)) + return -1; + format += amount; + written += amount; + continue; + } + + const char* format_begun_at = format++; + + if (*format == 'c') { + format++; + char c = (char) va_arg(parameters, int /* char promotes to int */); + if (!maxrem) { + // TODO: Set errno to EOVERFLOW. + return -1; + } + if (!print(&c, sizeof(c))) + return -1; + written++; + } else if (*format == 'd') { + format++; + char str[20]; // probably a long enough buffer + unsigned int d = va_arg(parameters, unsigned int); + itoa(d, str, 10); + size_t len = strlen(str); + if (maxrem < len) { + // TODO: Set errno to EOVERFLOW. + return -1; + } + if (!print(str, len)) + return -1; + written += len; + } else if (*format == 'x') { + format++; + char str[20]; // probably a long enough buffer + unsigned int d = va_arg(parameters, unsigned int); + itoa(d, str, 16); + size_t len = strlen(str); + if (maxrem < len) { + // TODO: Set errno to EOVERFLOW. + return -1; + } + if (!print(str, len)) + return -1; + written += len; + } else if (*format == 'l') { // for %lx and others that start with l + format++; + if (*format == 'x') { + format++; + char str[20]; // probably a long enough buffer + unsigned long long d = va_arg(parameters, unsigned long long); + itoa_u64(d, str, 16); + size_t len = strlen(str); + if (maxrem < len) { + // TODO: Set errno to EOVERFLOW. + return -1; + } + if (!print(str, len)) + return -1; + written += len; + } + } else if (*format == 's') { + format++; + const char* str = va_arg(parameters, const char*); + size_t len = strlen(str); + if (maxrem < len) { + // TODO: Set errno to EOVERFLOW. + return -1; + } + if (!print(str, len)) + return -1; + written += len; + } else { + format = format_begun_at; + size_t len = strlen(format); + if (maxrem < len) { + // TODO: Set errno to EOVERFLOW. + return -1; + } + if (!print(format, len)) + return -1; + written += len; + format += len; + } + } + + va_end(parameters); + return written; +} \ No newline at end of file diff --git a/libc/putchar.c b/libc/putchar.c new file mode 100644 index 0000000..c5a7976 --- /dev/null +++ b/libc/putchar.c @@ -0,0 +1,9 @@ +#include + +#include + +int putchar(int ic) { + char c = (char) ic; + terminal_write(&c, sizeof(c)); + return ic; +} \ No newline at end of file diff --git a/libc/puts.c b/libc/puts.c new file mode 100644 index 0000000..8c8c036 --- /dev/null +++ b/libc/puts.c @@ -0,0 +1,5 @@ +#include + +int puts(const char* string) { + return printf("%s\n", string); +} \ No newline at end of file diff --git a/libc/strlen.c b/libc/strlen.c new file mode 100644 index 0000000..441622a --- /dev/null +++ b/libc/strlen.c @@ -0,0 +1,9 @@ +#include + +size_t strlen(const char* str) { + size_t len = 0; + while (str[len]) { + ++len; + } + return len; +} \ No newline at end of file diff --git a/libs/libck/README.txt b/libs/libck/README.txt deleted file mode 100644 index 6806bd4..0000000 --- a/libs/libck/README.txt +++ /dev/null @@ -1,5 +0,0 @@ -a temporary(?) libc for the kernel - -code based off meaty skeleton from osdev.org - -todo: \ No newline at end of file diff --git a/libs/libck/abort.c b/libs/libck/abort.c deleted file mode 100644 index 83873e8..0000000 --- a/libs/libck/abort.c +++ /dev/null @@ -1,11 +0,0 @@ -#include -#include - -__attribute__((__noreturn__)) -void abort(void) { - // TODO: Add proper kernel panic. - printf("kernel: panic: abort()\n"); - asm volatile("hlt"); - while (1) { } - __builtin_unreachable(); -} \ No newline at end of file diff --git a/libs/libck/include/stdio.h b/libs/libck/include/stdio.h deleted file mode 100644 index eb4e45b..0000000 --- a/libs/libck/include/stdio.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef _STDIO_H -#define _STDIO_H 1 - -#define EOF (-1) - -#ifdef __cplusplus -extern "C" { -#endif - -int printf(const char* __restrict, ...); -int putchar(int); -int puts(const char*); - -#ifdef __cplusplus -} -#endif - -#endif \ No newline at end of file diff --git a/libs/libck/include/stdlib.h b/libs/libck/include/stdlib.h deleted file mode 100644 index 1d1ee32..0000000 --- a/libs/libck/include/stdlib.h +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef _STDLIB_H -#define _STDLIB_H 1 - -#ifdef __cplusplus -extern "C" { -#endif - -__attribute__((__noreturn__)) -void abort(void); - -#ifdef __cplusplus -} -#endif - -#endif \ No newline at end of file diff --git a/libs/libck/include/string.h b/libs/libck/include/string.h deleted file mode 100644 index 4f6b5bd..0000000 --- a/libs/libck/include/string.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef _STRING_H -#define _STRING_H 1 - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -int memcmp(const void*, const void*, size_t); -void* memcpy(void* __restrict, const void* __restrict, size_t); -void* memmove(void*, const void*, size_t); -void* memset(void*, int, size_t); -size_t strlen(const char*); - -#ifdef __cplusplus -} -#endif - -#endif \ No newline at end of file diff --git a/libs/libck/include/sys/cdefs.h b/libs/libck/include/sys/cdefs.h deleted file mode 100644 index 1e2c277..0000000 --- a/libs/libck/include/sys/cdefs.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef _SYS_CDEFS_H -#define _SYS_CDEFS_H 1 - - - -#endif \ No newline at end of file diff --git a/libs/libck/makefile b/libs/libck/makefile deleted file mode 100644 index 5f7b270..0000000 --- a/libs/libck/makefile +++ /dev/null @@ -1,79 +0,0 @@ -# libck makefile - -ifndef AR - AR = i686-elf-ar -endif - -ifndef CFLAGS - CFLAGS = -ffreestanding -Wall -Wextra -g -std=gnu11 -O2 -Iinclude - CFLAGS += --sysroot="$(SYSROOT)" - CFLAGS += -isystem="/usr/include" -endif - -ifndef CXXFLAGS - CXXFLAGS = -ffreestanding -Wall -Wextra -fno-exceptions -fno-rtti -D__is_kernel -g -O2 -Iinclude - CXXFLAGS += -Iinclude - CXXFLAGS += --sysroot="$(SYSROOT)" - CXXFLAGS += -isystem="/usr/include" -endif - -ifndef LDFLAGS - LDFLAGS = -T arch/$(ARCH)/linker.ld -ffreestanding -g -O2 -Iinclude - LDFLAGS += --sysroot="$(SYSROOT)" - LDFLAGS += -isystem="/usr/include" -endif - -ifndef SYSROOT - $(error No sysroot specified) -endif - - -LIBS = -nostdlib -lgcc - -#Find all the source files -C_SOURCES := $(shell find $(PWD) -type f -name '*.c') -#CPP_SOURCES := $(shell find $(PWD)/kernel -type f -name '*.cpp') -HEADERS := $(shell find $(PWD) -type f -name '*.h') -#ASMFILES := $(shell find $(PWD) -type f -name '*.asm') - -OBJECTS := $(patsubst %.c,%.o,$(C_SOURCES)) -# OBJECTS += $(patsubst %.cpp,%.o,$(CPP_SOURCES)) -# OBJECTS += $(patsubst %.asm,%.o,$(ASMFILES)) - -DEPFILES := $(patsubst %.c,%.d,$(C_SOURCES)) -# DEPFILES += $(patsubst %.cpp,%.d,$(CPP_SOURCES)) - -LINKLST += $(OBJECTS) -LINKLST += $(LIBS) - --include $(DEPFILES) - -.PHONY: all install-headers install-lib clean - -all: libck.a install-headers install-lib - -libck.a: ${OBJECTS} - $(info [INFO] Linking $<) - $(AR) rcs $@ $(OBJECTS) - -%.o: %.cpp - $(info [INFO] Compiling $<) - $(CXX) ${CXXFLAGS} -MMD -MP -c $< -o $@ - -%.o: %.c - $(info [INFO] Compiling $<) - $(CC) $(CFLAGS) -MMD -MP -c $< -o $@ - -%.o: %.asm - $(info [INFO] Assembling $<) - $(NASM) $< -felf32 -o $@ -g - -install-headers: - cp -r --preserve=timestamps include/. $(SYSROOT)/usr/include - -install-lib: libck.a - cp -r $< $(SYSROOT)/usr/lib/. - -clean: - $(info [INFO] Cleaning) - $(RM) ${OBJECTS} $(DEPFILES) \ No newline at end of file diff --git a/libs/libck/memcmp.c b/libs/libck/memcmp.c deleted file mode 100644 index 93141e1..0000000 --- a/libs/libck/memcmp.c +++ /dev/null @@ -1,13 +0,0 @@ -#include - -int memcmp(const void* aptr, const void* bptr, size_t size) { - const unsigned char* a = (const unsigned char*) aptr; - const unsigned char* b = (const unsigned char*) bptr; - for (size_t i = 0; i < size; i++) { - if (a[i] < b[i]) - return -1; - else if (b[i] < a[i]) - return 1; - } - return 0; -} \ No newline at end of file diff --git a/libs/libck/memcpy.c b/libs/libck/memcpy.c deleted file mode 100644 index 7e09be7..0000000 --- a/libs/libck/memcpy.c +++ /dev/null @@ -1,9 +0,0 @@ -#include - -void* memcpy(void* restrict dstptr, const void* restrict srcptr, size_t size) { - unsigned char* dst = (unsigned char*) dstptr; - const unsigned char* src = (const unsigned char*) srcptr; - for (size_t i=0; i < size; i++) - dst[i] = src[i]; - return dstptr; -} \ No newline at end of file diff --git a/libs/libck/memmove.c b/libs/libck/memmove.c deleted file mode 100644 index 98c787f..0000000 --- a/libs/libck/memmove.c +++ /dev/null @@ -1,14 +0,0 @@ -#include - -void* memmove(void* dstptr, const void* srcptr, size_t size) { - unsigned char* dst = (unsigned char*) dstptr; - const unsigned char* src = (const unsigned char*) srcptr; - if (dst < src) { - for (size_t i = 0; i < size; i++) - dst[i] = src[i]; - } else { - for (size_t i = size; i != 0; i--) - dst[i-1] = src[i-1]; - } - return dstptr; -} \ No newline at end of file diff --git a/libs/libck/memset.c b/libs/libck/memset.c deleted file mode 100644 index 67dadfe..0000000 --- a/libs/libck/memset.c +++ /dev/null @@ -1,8 +0,0 @@ -#include - -void* memset(void* bufptr, int value, size_t size) { - unsigned char* buf = (unsigned char*) bufptr; - for (size_t i = 0; i < size; i++) - buf[i] = (unsigned char) value; - return bufptr; -} \ No newline at end of file diff --git a/libs/libck/printf.c b/libs/libck/printf.c deleted file mode 100644 index 72a4749..0000000 --- a/libs/libck/printf.c +++ /dev/null @@ -1,191 +0,0 @@ -#include -#include -#include -#include -#include -#include - -static bool print(const char* data, size_t length) { - const unsigned char* bytes = (const unsigned char*) data; - for (size_t i = 0; i < length; i++) - if (putchar(bytes[i]) == EOF) - return false; - return true; -} - -// for %d and %x. -static const char* itoa(unsigned int num, char* str, unsigned int base) { - int cou = 0; // nt - - if (num == 0) { // 0 - str[cou] = '0'; - str[++cou] = '\0'; - return str; - } - - while (num) - { - unsigned int rem = (num % base); - // brings the number up to ascii 0 + the digit - if (rem > 9) { - str[cou] = rem + '7'; // 7 = 55 + 10(A) = 65 - } else { - str[cou] = rem + '0'; - } - num /= base; - ++cou; - } - - // reverse the string - for (int i = 0; i < cou; i++) { - char temp = str[i]; - str[i] = str[cou - i - 1]; - str[cou - 1] = temp; - } - - str[cou] = '\0'; - return str; -} - -// have no idea if there's a better way to do this. for %lx -static const char* itoa_u64(unsigned long long num, char* str, int base) { - int cou = 0; // nt - - if (num == 0) { // 0 - str[cou] = '0'; - str[++cou] = '\0'; - return str; - } - - while (num) - { - unsigned long long rem = num % base; - // brings the number up to ascii 0 + the digit - if (rem > 9) { - str[cou] = rem + '7'; // 7 = 55 + 10(A) = 65 - } else { - str[cou] = rem + '0'; - } - num /= base; - ++cou; - } - - // reverse the string - for (int i = 0; i < cou; i++) { - char temp = str[i]; - str[i] = str[cou - i - 1]; - str[cou - 1] = temp; - } - - str[cou] = '\0'; - return str; -} - -int printf(const char* restrict format, ...) { - va_list parameters; - va_start(parameters, format); - - int written = 0; - - while (*format != '\0') { - size_t maxrem = INT_MAX - written; - - if (format[0] != '%' || format[1] == '%') { - if (format[0] == '%') - format++; - size_t amount = 1; - while (format[amount] && format[amount] != '%') - amount++; - if (maxrem < amount) { - // TODO: Set errno to EOVERFLOW. - return -1; - } - if (!print(format, amount)) - return -1; - format += amount; - written += amount; - continue; - } - - const char* format_begun_at = format++; - - if (*format == 'c') { - format++; - char c = (char) va_arg(parameters, int /* char promotes to int */); - if (!maxrem) { - // TODO: Set errno to EOVERFLOW. - return -1; - } - if (!print(&c, sizeof(c))) - return -1; - written++; - } else if (*format == 'd') { - format++; - char str[20]; // probably a long enough buffer - unsigned int d = va_arg(parameters, unsigned int); - itoa(d, str, 10); - size_t len = strlen(str); - if (maxrem < len) { - // TODO: Set errno to EOVERFLOW. - return -1; - } - if (!print(str, len)) - return -1; - written += len; - } else if (*format == 'x') { - format++; - char str[20]; // probably a long enough buffer - unsigned int d = va_arg(parameters, unsigned int); - itoa(d, str, 16); - size_t len = strlen(str); - if (maxrem < len) { - // TODO: Set errno to EOVERFLOW. - return -1; - } - if (!print(str, len)) - return -1; - written += len; - } else if (*format == 'l') { // for %lx and others that start with l - format++; - if (*format == 'x') { - format++; - char str[20]; // probably a long enough buffer - unsigned long long d = va_arg(parameters, unsigned long long); - itoa_u64(d, str, 16); - size_t len = strlen(str); - if (maxrem < len) { - // TODO: Set errno to EOVERFLOW. - return -1; - } - if (!print(str, len)) - return -1; - written += len; - } - } else if (*format == 's') { - format++; - const char* str = va_arg(parameters, const char*); - size_t len = strlen(str); - if (maxrem < len) { - // TODO: Set errno to EOVERFLOW. - return -1; - } - if (!print(str, len)) - return -1; - written += len; - } else { - format = format_begun_at; - size_t len = strlen(format); - if (maxrem < len) { - // TODO: Set errno to EOVERFLOW. - return -1; - } - if (!print(format, len)) - return -1; - written += len; - format += len; - } - } - - va_end(parameters); - return written; -} \ No newline at end of file diff --git a/libs/libck/putchar.c b/libs/libck/putchar.c deleted file mode 100644 index c5a7976..0000000 --- a/libs/libck/putchar.c +++ /dev/null @@ -1,9 +0,0 @@ -#include - -#include - -int putchar(int ic) { - char c = (char) ic; - terminal_write(&c, sizeof(c)); - return ic; -} \ No newline at end of file diff --git a/libs/libck/puts.c b/libs/libck/puts.c deleted file mode 100644 index 8c8c036..0000000 --- a/libs/libck/puts.c +++ /dev/null @@ -1,5 +0,0 @@ -#include - -int puts(const char* string) { - return printf("%s\n", string); -} \ No newline at end of file diff --git a/libs/libck/strlen.c b/libs/libck/strlen.c deleted file mode 100644 index 441622a..0000000 --- a/libs/libck/strlen.c +++ /dev/null @@ -1,9 +0,0 @@ -#include - -size_t strlen(const char* str) { - size_t len = 0; - while (str[len]) { - ++len; - } - return len; -} \ No newline at end of file diff --git a/makefile b/makefile index c52dd4c..21c698c 100644 --- a/makefile +++ b/makefile @@ -7,30 +7,33 @@ export SYSROOT = $(PWD)/sysroot # Variables for easy access of tools like gcc and nasm export CC = $(ARCH)-elf-gcc export CXX = $(ARCH)-elf-g++ -export LD = $(ARCH)-elf-ld +export AR = $(ARCH)-elf-ar export NASM = nasm -QEMU = qemu-system-x86_64 +QEMU = qemu-system-x86_64 #ASMFLAGS = -felf32 #CXXFLAGS := -ffreestanding -O2 -Wall -Wextra -fno-exceptions -fno-rtti #LDFLAGS := -ffreestanding -O2 -nostdlib -.PHONY: all kernel.bin grub multiboot_test clean +.PHONY: all grub clean build-all -all: libck.a kernel.bin grub +all: build-all grub -libck.a: - $(info [INFO] Building libck) - $(MAKE) -C ./libs/libck/ ARCH=$(ARCH) PREFIX=$(PWD) +build-all: kernel/kernel.bin libc.a -kernel.bin: +libc/libc.a: install-headers + $(info [INFO] Building libc) + $(MAKE) -C ./libc/ ARCH=$(ARCH) PREFIX=$(PWD) + +kernel/kernel.bin: libc/libc.a install-headers $(info [INFO] Building kernel) $(MAKE) -C ./kernel/ ARCH=$(ARCH) PREFIX=$(PWD) -grub: kernel.bin grub.cfg - grub-file --is-x86-multiboot $< - cp kernel.bin isodir/boot - cp grub.cfg isodir/boot/grub - grub-mkrescue -o $(OS_NAME).iso isodir +grub: build-all grub.cfg + xorriso -as mkisofs -b boot/limine/limine-bios-cd.bin \ + -no-emul-boot -boot-load-size 4 -boot-info-table \ + --efi-boot boot/limine/limine-uefi-cd.bin \ + -efi-boot-part --efi-boot-image --protective-msdos-label \ + isodir -o emuos.iso qemu: grub $(QEMU) -no-shutdown -no-reboot --serial stdio -s -m 512 -hda $(OS_NAME).iso @@ -39,12 +42,12 @@ install: install-headers install-libraries install-headers: $(MAKE) -C ./kernel/ install-headers - $(MAKE) -C ./libs/libck/ install-headers + $(MAKE) -C ./libs/libc/ install-headers install-libraries: - $(MAKE) -C ./libs/libck/ install-lib + $(MAKE) -C ./libs/libc/ install-lib clean: -@$(MAKE) -C ./kernel/ clean - -@$(MAKE) -C ./libs/libck/ clean + -@$(MAKE) -C ./libs/libc/ clean -@$(RM) $(wildcard *.bin *.a) -- cgit v1.2.3-70-g09d2 From 33bc3446d237a95f1bbf9d5d70a170fa4c4241cd Mon Sep 17 00:00:00 2001 From: EmulatedSeasons <89668582+EmulatedSeasons@users.noreply.github.com> Date: Mon, 6 May 2024 23:00:36 -0400 Subject: Improved makefile a bit more and got it working with the other files --- kernel/arch/x86_64/limine.cpp | 6 ++-- kernel/arch/x86_64/limine.h | 2 +- kernel/arch/x86_64/linker.ld | 78 +++++++++++++++++++++++++++++++++++++++++++ kernel/dev/ps2kbd.cpp | 34 +++++++++---------- kernel/kernel.cpp | 6 ++-- kernel/makefile | 68 ++++++++++++++++++------------------- libc/makefile | 4 +-- libc/putchar.c | 4 +-- makefile | 25 +++++++------- run-qemu.bat | 2 +- setup_sysroot.sh | 9 +++++ 11 files changed, 163 insertions(+), 75 deletions(-) create mode 100644 kernel/arch/x86_64/linker.ld create mode 100644 setup_sysroot.sh (limited to 'makefile') diff --git a/kernel/arch/x86_64/limine.cpp b/kernel/arch/x86_64/limine.cpp index 84f8642..8c67c52 100644 --- a/kernel/arch/x86_64/limine.cpp +++ b/kernel/arch/x86_64/limine.cpp @@ -1,4 +1,3 @@ -#include #include #include #include "limine.h" @@ -37,6 +36,7 @@ void hcf() { extern void (*__init_array[])(); extern void (*__init_array_end[])(); +extern "C" void kernel_main(); extern "C" void _start() { if (!LIMINE_BASE_REVISION_SUPPORTED) { @@ -58,11 +58,13 @@ extern "C" void _start() { limine_framebuffer *framebuffer = framebuffer_request.response->framebuffers[0]; // Note: we assume the framebuffer model is RGB with 32-bit pixels. - for (std::size_t i = 0; i < 100; i++) { + for (size_t i = 0; i < 100; i++) { volatile uint32_t *fb_ptr = static_cast(framebuffer->address); fb_ptr[i * (framebuffer->pitch / 4) + i] = 0xffffff; } + + // We're done, just hang... hcf(); } \ No newline at end of file diff --git a/kernel/arch/x86_64/limine.h b/kernel/arch/x86_64/limine.h index 0e7e87a..b4afb43 100644 --- a/kernel/arch/x86_64/limine.h +++ b/kernel/arch/x86_64/limine.h @@ -584,4 +584,4 @@ struct limine_dtb_request { } #endif -#endif \ No newline at end of file +#endif diff --git a/kernel/arch/x86_64/linker.ld b/kernel/arch/x86_64/linker.ld new file mode 100644 index 0000000..5724ecf --- /dev/null +++ b/kernel/arch/x86_64/linker.ld @@ -0,0 +1,78 @@ +OUTPUT_FORMAT(elf64-x86-64) +OUTPUT_ARCH(i386:x86-64) + +/* Excecution entry point symbol */ +ENTRY(_start) + +PHDRS +{ + requests PT_LOAD FLAGS((1 << 1) | (1 << 2)); /* Write + Read */ + text PT_LOAD FLAGS((1 << 0) | (1 << 2)); /* Execute + Read */ + rodata PT_LOAD FLAGS((1 << 2)); /* Read only */ + data PT_LOAD FLAGS((1 << 1) | (1 << 2)); /* Write + Read */ + dynamic PT_DYNAMIC FLAGS((1 << 1) | (1 << 2)); /* Dynamic PHDR for relocations */ +} + +SECTIONS +{ + /* Places kernel in the top 2GiB of the address space */ + . = 0xffffffff80000000; + + /* Used to get the beginning and end address of the kernel */ + __kernel_begin = .; + + .requests : { + KEEP(*(.requests_start_marker)) + KEEP(*(.requests)) + KEEP(*(.requests_end_marker)) + } :requests + + . += CONSTANT(MAXPAGESIZE); + + /* code. multiboot header comes first */ + .text : { + *(.text .text.*) + } :text + + . += CONSTANT(MAXPAGESIZE); + + /* read only data */ + .rodata : { + *(. .rodata.*) + } :rodata + + /* init array symbols used to get start and end address of the array */ + .init_array : { + __init_array = .; + *(.init_array .init_array.*) + __init_array_end = .; + } :rodata + + . += CONSTANT(MAXPAGESIZE); + + /* read write data (initialized) */ + .data : { + *(.data .data.*) + } :data + + /* Dynamic section for relocations, both in its own PHDR and inside data PHDR */ + .dynamic : { + *(.dynamic) + } :data :dynamic + + . += CONSTANT(MAXPAGESIZE); + + /* read write data (uninitialized) and stack */ + .bss : { + *(.bss .bss.*) + *(COMMON) + } :data + + /* Discard .note.* and .eh_frame since they may cause issues on some hosts */ + /DISCARD/ : { + *(.eh_frame) + *(.note .note.*) + } + + __kernel_end = .; +} \ No newline at end of file diff --git a/kernel/dev/ps2kbd.cpp b/kernel/dev/ps2kbd.cpp index ee282b6..ffb1189 100644 --- a/kernel/dev/ps2kbd.cpp +++ b/kernel/dev/ps2kbd.cpp @@ -1,25 +1,25 @@ #include -#include -#include +//#include +//#include #include // Keycodes are sorted from left to right, up to down starting with esc // 0x80 is reserved for nonexistant scancodes // used for scan code set 2 -constexpr uint8_t scancodes[] = { - 9, 0x80, 5, 3, 1, 2, 12, 0x80, - 10, 8, 6, 4, 27, -}; +// constexpr uint8_t scancodes[] = { +// 9, 0x80, 5, 3, 1, 2, 12, 0x80, +// 10, 8, 6, 4, 27, +// }; -void keyboard_init() { - irq_clear_mask(1); - outb(0x60, 0xF4); - inb(0x60); -} +// void keyboard_init() { +// irq_clear_mask(1); +// outb(0x60, 0xF4); +// inb(0x60); +// } -void keyboard_write(uint8_t scancode) { - printf("a\n"); - /*for (int i = 0; i < 7; i++) { - inputbuffer |= inb(PS2_DATA) << (i * 8); // lsb is the first scan code - }*/ -} \ No newline at end of file +// void keyboard_write(uint8_t scancode) { +// printf("a\n"); +// /*for (int i = 0; i < 7; i++) { +// inputbuffer |= inb(PS2_DATA) << (i * 8); // lsb is the first scan code +// }*/ +// } \ No newline at end of file diff --git a/kernel/kernel.cpp b/kernel/kernel.cpp index f8f36b5..a0394ea 100644 --- a/kernel/kernel.cpp +++ b/kernel/kernel.cpp @@ -1,7 +1,7 @@ #include -#include -#include -#include +//#include +//#include +//#include //#include //#include diff --git a/kernel/makefile b/kernel/makefile index b2a2660..8c2832d 100644 --- a/kernel/makefile +++ b/kernel/makefile @@ -1,14 +1,14 @@ # Kernel makefile -CFLAGS := -ffreestanding -Wall -Wextra -g -std=gnu99 -O2 -Iinclude -CXXFLAGS := -ffreestanding -Wall -Wextra -fno-exceptions -fno-rtti -g -O2 -Iinclude -LDFLAGS := -T arch/$(ARCH)/linker.ld -ffreestanding -g -O2 -Iinclude +CFLAGS := -ffreestanding -Wall -Wextra -g -std=gnu99 -O2 -Iinclude --sysroot=$(SYSROOT) +CXXFLAGS := -ffreestanding -Wall -Wextra -fno-exceptions -fno-rtti -g -O2 -Iinclude --sysroot=$(SYSROOT) +LDFLAGS := -T arch/$(ARCH)/linker.ld -ffreestanding -g -O2 -Iinclude --sysroot=$(SYSROOT) ASMFLAGS := ifeq ($(ARCH),x86_64) -CFLAGS += -mno-red-zone -CXXFLAGS += -mno-red-zone -LDFLAGS += -mno-red-zone +CFLAGS += -mno-red-zone -mcmodel=kernel -mno-sse -mno-sse2 -mno-mmx +CXXFLAGS += -mno-red-zone -mcmodel=kernel -mno-sse -mno-sse2 -mno-mmx +LDFLAGS += -mno-red-zone -mcmodel=kernel -mno-sse -mno-sse2 -mno-mmx ASMFLAGS += -felf64 endif @@ -16,38 +16,36 @@ ifndef SYSROOT $(error No sysroot specified) endif - -LIBS = -nostdlib -lck -lgcc +LIBS = -nostdlib -lc -lgcc #Find all the source files -C_SOURCES := $(shell find $(PWD)/kernel -type f -name '*.c') -CPP_SOURCES := $(shell find $(PWD)/kernel -type f -name '*.cpp') -HEADERS := $(shell find $(PWD) -type f -name '*.h') -ASMFILES := $(shell find $(PWD) -type f -name '*.asm' ! -name 'crti.asm' ! -name 'crtn.asm') -CRTBEGIN := $(shell $(CXX) $(CXXFLAGS) -print-file-name=crtbegin.o) -CRTEND := $(shell $(CXX) $(CXXFLAGS) -print-file-name=crtend.o) - -OBJECTS := $(patsubst %.c,%.o,$(C_SOURCES)) -OBJECTS += $(patsubst %.cpp,%.o,$(CPP_SOURCES)) -OBJECTS += $(patsubst %.asm,%.o,$(ASMFILES)) - -DEPFILES := $(patsubst %.c,%.d,$(C_SOURCES)) -DEPFILES += $(patsubst %.cpp,%.d,$(CPP_SOURCES)) - -LINKLST := crti.o -LINKLST += $(CRTBEGIN) -LINKLST += $(OBJECTS) +CPP_SRCS := $(shell find . -type f -name '*.cpp' -not -path './arch/*') \ + $(shell find $(PWD)/kernel/arch/$(ARCH) -type f -name '*.cpp') +#HEADERS := $(shell find $(PWD) -type f -name '*.h') +#ASMFILES := $(shell find $(PWD) -type f -name '*.asm' -prune $(PWD)/arch) \ + $(shell find $(PWD)/kernel/arch/$(ARCH) -type f -name '*.asm') + +#OBJS := $(patsubst %.c,%.o,$(C_SOURCES)) +OBJS := $(patsubst %.cpp,%.o,$(CPP_SRCS)) +OBJS += $(patsubst %.asm,%.o,$(ASMFILES)) + +#DEPFILES := $(patsubst %.c,%.d,$(C_SOURCES)) +DEPFILES := $(patsubst %.cpp,%.d,$(CPP_SRCS)) + +# LINKLST := crti.o +# LINKLST += $(CRTBEGIN) +LINKLST += $(OBJS) LINKLST += $(LIBS) -LINKLST += $(CRTEND) -LINKLST += crtn.o +# LINKLST += $(CRTEND) +# LINKLST += crtn.o -include $(DEPFILES) -.PHONY: all crt clean kernel.bin +.PHONY: all clean -all: crti.o crtn.o kernel.bin +all: kernel.bin -kernel.bin: ${OBJECTS} +kernel.bin: ${OBJS} $(info [INFO] Linking kernel) $(CXX) ${LDFLAGS} -o $@ $(LINKLST) @@ -63,15 +61,15 @@ kernel.bin: ${OBJECTS} $(info [INFO] Assembling $<) $(NASM) $< $(ASMFLAGS) -o $@ -g -crti.o: - $(NASM) arch/$(ARCH)/crti.asm $(ASMFLAGS) -o $@ +# crti.o: +# $(NASM) arch/$(ARCH)/crti.asm $(ASMFLAGS) -o $@ -crtn.o: - $(NASM) arch/$(ARCH)/crtn.asm $(ASMFLAGS) -o $@ +# crtn.o: +# $(NASM) arch/$(ARCH)/crtn.asm $(ASMFLAGS) -o $@ install-headers: cp -r --preserve=timestamps include/. $(SYSROOT)/usr/include clean: $(info [INFO] Cleaning) - $(RM) ${OBJECTS} $(DEPFILES) crti.o crtn.o \ No newline at end of file + $(RM) ${OBJS} $(DEPFILES) \ No newline at end of file diff --git a/libc/makefile b/libc/makefile index 2e1912e..560efb7 100644 --- a/libc/makefile +++ b/libc/makefile @@ -1,4 +1,4 @@ -# libck makefile +# libc makefile ifndef AR AR = i686-elf-ar @@ -71,7 +71,7 @@ libc.a: ${OBJECTS} install-headers: cp -r --preserve=timestamps include/. $(SYSROOT)/usr/include -install-lib: libck.a +install-lib: libc.a cp -r $< $(SYSROOT)/usr/lib/. clean: diff --git a/libc/putchar.c b/libc/putchar.c index c5a7976..48147bf 100644 --- a/libc/putchar.c +++ b/libc/putchar.c @@ -1,9 +1,9 @@ #include -#include +//#include int putchar(int ic) { char c = (char) ic; - terminal_write(&c, sizeof(c)); + //terminal_write(&c, sizeof(c)); return ic; } \ No newline at end of file diff --git a/makefile b/makefile index 21c698c..2581e21 100644 --- a/makefile +++ b/makefile @@ -1,24 +1,24 @@ # The makefile -OS_NAME := emuos -ARCH ?= x86_64 -export SYSROOT = $(PWD)/sysroot +OS_NAME := emuos +export ARCH ?= x86_64 +export SYSROOT = $(PWD)/sysroot # Variables for easy access of tools like gcc and nasm export CC = $(ARCH)-elf-gcc export CXX = $(ARCH)-elf-g++ export AR = $(ARCH)-elf-ar export NASM = nasm -QEMU = qemu-system-x86_64 +QEMU = qemu-system-$(ARCH) #ASMFLAGS = -felf32 #CXXFLAGS := -ffreestanding -O2 -Wall -Wextra -fno-exceptions -fno-rtti #LDFLAGS := -ffreestanding -O2 -nostdlib -.PHONY: all grub clean build-all +.PHONY: all limine clean build-all -all: build-all grub +all: build-all limine -build-all: kernel/kernel.bin libc.a +build-all: kernel/kernel.bin libc/libc.a libc/libc.a: install-headers $(info [INFO] Building libc) @@ -28,26 +28,27 @@ kernel/kernel.bin: libc/libc.a install-headers $(info [INFO] Building kernel) $(MAKE) -C ./kernel/ ARCH=$(ARCH) PREFIX=$(PWD) -grub: build-all grub.cfg +limine: build-all + cp kernel/kernel.bin isodir/boot xorriso -as mkisofs -b boot/limine/limine-bios-cd.bin \ -no-emul-boot -boot-load-size 4 -boot-info-table \ --efi-boot boot/limine/limine-uefi-cd.bin \ -efi-boot-part --efi-boot-image --protective-msdos-label \ isodir -o emuos.iso -qemu: grub +qemu: limine $(QEMU) -no-shutdown -no-reboot --serial stdio -s -m 512 -hda $(OS_NAME).iso install: install-headers install-libraries install-headers: $(MAKE) -C ./kernel/ install-headers - $(MAKE) -C ./libs/libc/ install-headers + $(MAKE) -C ./libc/ install-headers install-libraries: - $(MAKE) -C ./libs/libc/ install-lib + $(MAKE) -C ./libc/ install-lib clean: -@$(MAKE) -C ./kernel/ clean - -@$(MAKE) -C ./libs/libc/ clean + -@$(MAKE) -C ./libc/ clean -@$(RM) $(wildcard *.bin *.a) diff --git a/run-qemu.bat b/run-qemu.bat index 19a5ae8..ddf7afa 100644 --- a/run-qemu.bat +++ b/run-qemu.bat @@ -1,2 +1,2 @@ @echo off -qemu-system-i386 -m 128 -drive format=raw,media=cdrom,file=emuos.iso \ No newline at end of file +qemu-system-x86_64 -m 128 -drive format=raw,media=cdrom,file=emuos.iso \ No newline at end of file diff --git a/setup_sysroot.sh b/setup_sysroot.sh new file mode 100644 index 0000000..5c48677 --- /dev/null +++ b/setup_sysroot.sh @@ -0,0 +1,9 @@ +# Copies include/ header files to sysroot/ + +# creates the needed sysroot folders if non existant +mkdir -p sysroot/usr/include +mkdir -p sysroot/usr/lib + +# copies all needed header files for compiling +cp -r kernel/include/* sysroot/usr/include +cp -r libc/include/* sysroot/usr/include \ No newline at end of file -- cgit v1.2.3-70-g09d2 From abaab98bd757cd0818cfcddc983eee25ab7672ed Mon Sep 17 00:00:00 2001 From: EmulatedSeasons <89668582+EmulatedSeasons@users.noreply.github.com> Date: Tue, 7 May 2024 11:07:03 -0400 Subject: Made framebuffer class, serial write functions, and finally got makefile to build without having to make clean --- .gitignore | 1 + kernel/arch/x86_64/limine.cpp | 8 ++++++++ kernel/arch/x86_64/serial.cpp | 23 +++++++++++++++++++++++ kernel/framebuffer.cpp | 22 ++++++++++++++++++++++ kernel/include/arch/x86_64/serial.h | 19 +++++++++++++++++++ kernel/include/framebuffer.h | 18 ++++++++++++++++++ kernel/makefile | 19 +++++++++++-------- libc/makefile | 14 +++++++------- libc/putchar.c | 2 ++ makefile | 4 ++-- run-qemu.bat | 2 +- 11 files changed, 114 insertions(+), 18 deletions(-) create mode 100644 kernel/arch/x86_64/serial.cpp create mode 100644 kernel/framebuffer.cpp create mode 100644 kernel/include/arch/x86_64/serial.h create mode 100644 kernel/include/framebuffer.h (limited to 'makefile') diff --git a/.gitignore b/.gitignore index ce75dcb..2d6918b 100644 --- a/.gitignore +++ b/.gitignore @@ -19,5 +19,6 @@ misc/ # not important make.png +.clangd .vscode/ diff --git a/kernel/arch/x86_64/limine.cpp b/kernel/arch/x86_64/limine.cpp index 8c67c52..9ea2bbc 100644 --- a/kernel/arch/x86_64/limine.cpp +++ b/kernel/arch/x86_64/limine.cpp @@ -1,6 +1,9 @@ #include #include +#include +#include #include "limine.h" +#include namespace { __attribute__((used, section(".requests"))) @@ -63,7 +66,12 @@ extern "C" void _start() { fb_ptr[i * (framebuffer->pitch / 4) + i] = 0xffffff; } + Framebuffer fb((uint32_t*)framebuffer->address, framebuffer->width, framebuffer->height); + fb.drawpixel(727, 727, 0x9528fd); + printf("video modes: %lx", framebuffer->mode_count); + + kernel_main(); // We're done, just hang... hcf(); diff --git a/kernel/arch/x86_64/serial.cpp b/kernel/arch/x86_64/serial.cpp new file mode 100644 index 0000000..e90bcde --- /dev/null +++ b/kernel/arch/x86_64/serial.cpp @@ -0,0 +1,23 @@ +#include +#include + +#define PORT 0x3f8 + +int is_transmit_empty() { + return inb(PORT + 5) & 0x20; +} + +extern "C" void serial_putchar(char c) { + while (is_transmit_empty() == 0); + outb(PORT, c); +} + +void serial_write(const char* data, size_t size) { + for (size_t i = 0; i < size; i++) { + serial_putchar(data[i]); + } +} + +void serial_writestring(const char* data) { + serial_write(data, strlen(data)); +} \ No newline at end of file diff --git a/kernel/framebuffer.cpp b/kernel/framebuffer.cpp new file mode 100644 index 0000000..a07d61a --- /dev/null +++ b/kernel/framebuffer.cpp @@ -0,0 +1,22 @@ +#include +#include +#include + +Framebuffer::Framebuffer(uint32_t* addr, const uint64_t width, const uint64_t height) + : addr {addr}, width {width}, height {height} { + clear(); +} + +void Framebuffer::clear() { + for (size_t y = 0; y < height; y++) { + for (size_t x = 0; x < width; x++) { + const size_t i = y * width + x; + addr[i] = 0x000000; + } + } +} + +void Framebuffer::drawpixel(const uint64_t x, const uint64_t y, const uint32_t color) { + const size_t pos = y * width + x; + addr[pos] = color; +} \ No newline at end of file diff --git a/kernel/include/arch/x86_64/serial.h b/kernel/include/arch/x86_64/serial.h new file mode 100644 index 0000000..2ee63c5 --- /dev/null +++ b/kernel/include/arch/x86_64/serial.h @@ -0,0 +1,19 @@ +#ifndef SERIAL_H +#define SERIAL_H + +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + +void serial_putchar(char c); +void serial_write(const char* data, size_t size); +void serial_writestring(const char* data); + +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file diff --git a/kernel/include/framebuffer.h b/kernel/include/framebuffer.h new file mode 100644 index 0000000..6080810 --- /dev/null +++ b/kernel/include/framebuffer.h @@ -0,0 +1,18 @@ +#ifndef FRAMEBUFFER_H +#define FRAMEBUFFER_H + +#include + +class Framebuffer { + private: + uint32_t* addr; + const uint64_t width; + const uint64_t height; + + public: + Framebuffer(uint32_t* addr, const uint64_t width, const uint64_t height); + void clear(); + void drawpixel(const uint64_t x, const uint64_t y, const uint32_t color); +}; + +#endif \ No newline at end of file diff --git a/kernel/makefile b/kernel/makefile index 8c2832d..33ab941 100644 --- a/kernel/makefile +++ b/kernel/makefile @@ -1,8 +1,11 @@ # Kernel makefile -CFLAGS := -ffreestanding -Wall -Wextra -g -std=gnu99 -O2 -Iinclude --sysroot=$(SYSROOT) -CXXFLAGS := -ffreestanding -Wall -Wextra -fno-exceptions -fno-rtti -g -O2 -Iinclude --sysroot=$(SYSROOT) -LDFLAGS := -T arch/$(ARCH)/linker.ld -ffreestanding -g -O2 -Iinclude --sysroot=$(SYSROOT) +CFLAGS := -ffreestanding -Wall -Wextra -g -std=gnu99 -O2 -Iinclude --sysroot=$(SYSROOT) \ + -isystem="/usr/include" +CXXFLAGS := -ffreestanding -Wall -Wextra -fno-exceptions -fno-rtti -g -O2 -Iinclude --sysroot=$(SYSROOT) \ + -isystem="/usr/include" +LDFLAGS := -T arch/$(ARCH)/linker.ld -ffreestanding -g -O2 -Iinclude --sysroot=$(SYSROOT) \ + -isystem="/usr/include" ASMFLAGS := ifeq ($(ARCH),x86_64) @@ -21,7 +24,7 @@ LIBS = -nostdlib -lc -lgcc #Find all the source files CPP_SRCS := $(shell find . -type f -name '*.cpp' -not -path './arch/*') \ $(shell find $(PWD)/kernel/arch/$(ARCH) -type f -name '*.cpp') -#HEADERS := $(shell find $(PWD) -type f -name '*.h') +HEADERS := $(shell find ./include -type f -name '*.h') #ASMFILES := $(shell find $(PWD) -type f -name '*.asm' -prune $(PWD)/arch) \ $(shell find $(PWD)/kernel/arch/$(ARCH) -type f -name '*.asm') @@ -50,11 +53,11 @@ kernel.bin: ${OBJS} $(CXX) ${LDFLAGS} -o $@ $(LINKLST) %.o: %.cpp - $(info [INFO] Compiling $<) +# $(info [INFO] Compiling $<) $(CXX) ${CXXFLAGS} -MMD -MP -c $< -o $@ %.o: %.c - $(info [INFO] Compiling $<) +# $(info [INFO] Compiling $<) $(CC) $(CFLAGS) -MMD -MP -c $< -o $@ %.o: %.asm @@ -67,9 +70,9 @@ kernel.bin: ${OBJS} # crtn.o: # $(NASM) arch/$(ARCH)/crtn.asm $(ASMFLAGS) -o $@ -install-headers: +install-headers: $(HEADERS) cp -r --preserve=timestamps include/. $(SYSROOT)/usr/include clean: $(info [INFO] Cleaning) - $(RM) ${OBJS} $(DEPFILES) \ No newline at end of file + $(RM) ${OBJS} $(DEPFILES) kernel.bin \ No newline at end of file diff --git a/libc/makefile b/libc/makefile index 560efb7..aec21ef 100644 --- a/libc/makefile +++ b/libc/makefile @@ -6,21 +6,21 @@ endif ifndef CFLAGS CFLAGS = -ffreestanding -Wall -Wextra -g -std=gnu11 -O2 -Iinclude - CFLAGS += --sysroot="$(SYSROOT)" - CFLAGS += -isystem="/usr/include" + CFLAGS += --sysroot=$(SYSROOT) + CFLAGS += -isystem $(SYSROOT)/usr/include endif ifndef CXXFLAGS CXXFLAGS = -ffreestanding -Wall -Wextra -fno-exceptions -fno-rtti -D__is_kernel -g -O2 -Iinclude CXXFLAGS += -Iinclude - CXXFLAGS += --sysroot="$(SYSROOT)" - CXXFLAGS += -isystem="/usr/include" + CXXFLAGS += --sysroot=$(SYSROOT) + CXXFLAGS += -isystem $(SYSROOT)/usr/include endif ifndef LDFLAGS LDFLAGS = -T arch/$(ARCH)/linker.ld -ffreestanding -g -O2 -Iinclude - LDFLAGS += --sysroot="$(SYSROOT)" - LDFLAGS += -isystem="/usr/include" + LDFLAGS += --sysroot=$(SYSROOT) + LDFLAGS += -isystem $(SYSROOT)/usr/include endif ifndef SYSROOT @@ -76,4 +76,4 @@ install-lib: libc.a clean: $(info [INFO] Cleaning) - $(RM) ${OBJECTS} $(DEPFILES) \ No newline at end of file + $(RM) ${OBJECTS} $(DEPFILES) libc.a \ No newline at end of file diff --git a/libc/putchar.c b/libc/putchar.c index 48147bf..f0f5783 100644 --- a/libc/putchar.c +++ b/libc/putchar.c @@ -1,9 +1,11 @@ #include //#include +#include int putchar(int ic) { char c = (char) ic; + serial_putchar(c); //terminal_write(&c, sizeof(c)); return ic; } \ No newline at end of file diff --git a/makefile b/makefile index 2581e21..d8535bc 100644 --- a/makefile +++ b/makefile @@ -22,11 +22,11 @@ build-all: kernel/kernel.bin libc/libc.a libc/libc.a: install-headers $(info [INFO] Building libc) - $(MAKE) -C ./libc/ ARCH=$(ARCH) PREFIX=$(PWD) + $(MAKE) -C ./libc/ all kernel/kernel.bin: libc/libc.a install-headers $(info [INFO] Building kernel) - $(MAKE) -C ./kernel/ ARCH=$(ARCH) PREFIX=$(PWD) + $(MAKE) -C ./kernel/ all limine: build-all cp kernel/kernel.bin isodir/boot diff --git a/run-qemu.bat b/run-qemu.bat index ddf7afa..b9c277b 100644 --- a/run-qemu.bat +++ b/run-qemu.bat @@ -1,2 +1,2 @@ @echo off -qemu-system-x86_64 -m 128 -drive format=raw,media=cdrom,file=emuos.iso \ No newline at end of file +qemu-system-x86_64 -m 128 -drive format=raw,media=cdrom,file=emuos.iso -serial stdio \ No newline at end of file -- cgit v1.2.3-70-g09d2 From 1bd17dce9a8297f86f3bb34b3f440490b601eca6 Mon Sep 17 00:00:00 2001 From: EmulatedSeasons <89668582+EmulatedSeasons@users.noreply.github.com> Date: Fri, 31 May 2024 21:08:22 -0400 Subject: started working on pmm and fixed printf doing weird things to the numbers --- .bochsrc | 8 +- kernel/arch/x86_64/limine.cpp | 45 +++- kernel/arch/x86_64/limine.h | 587 ------------------------------------------ kernel/include/limine.h | 587 ++++++++++++++++++++++++++++++++++++++++++ kernel/include/mm/pmm.h | 27 ++ kernel/kernel.cpp | 2 +- kernel/makefile | 11 +- kernel/mm/pmm.cpp | 55 ++++ libc/printf.c | 26 +- makefile | 5 +- 10 files changed, 740 insertions(+), 613 deletions(-) delete mode 100644 kernel/arch/x86_64/limine.h create mode 100644 kernel/include/limine.h create mode 100644 kernel/include/mm/pmm.h create mode 100644 kernel/mm/pmm.cpp (limited to 'makefile') diff --git a/.bochsrc b/.bochsrc index 8cd280b..924ea32 100644 --- a/.bochsrc +++ b/.bochsrc @@ -3,13 +3,15 @@ boot: cdrom megs: 256 -display_library: sdl2 -#display_library: sdl2, options="gui_debug" +#display_library: sdl2 +display_library: sdl2, options="gui_debug" vga: extension=vbe magic_break: enabled=1 -clock: sync=none, time0=local +clock: sync=realtime, time0=local cpu: reset_on_triple_fault=0 + +com1: enabled=1, mode=file, dev=misc/bochs.txt \ No newline at end of file diff --git a/kernel/arch/x86_64/limine.cpp b/kernel/arch/x86_64/limine.cpp index 6d7984e..b592592 100644 --- a/kernel/arch/x86_64/limine.cpp +++ b/kernel/arch/x86_64/limine.cpp @@ -2,10 +2,11 @@ #include #include #include -#include "limine.h" +#include #include "gdt.h" #include "idt.h" #include +#include namespace { __attribute__((used, section(".requests"))) @@ -21,6 +22,24 @@ volatile limine_framebuffer_request framebuffer_request = { }; } +namespace { +__attribute__((used, section(".requests"))) +volatile limine_memmap_request memmap_request = { + .id = LIMINE_MEMMAP_REQUEST, + .revision = 0, + .response = nullptr +}; +} + +namespace { +__attribute__((used, section(".requests"))) +volatile limine_hhdm_request hhdm_request = { + .id = LIMINE_HHDM_REQUEST, + .revision = 0, + .response = nullptr +}; +} + namespace { __attribute__((used, section(".requests_start_marker"))) volatile LIMINE_REQUESTS_START_MARKER; @@ -30,7 +49,7 @@ volatile LIMINE_REQUESTS_END_MARKER; } namespace { -void hcf() { +[[noreturn]] void hcf() { asm("cli"); for (;;) { asm("hlt"); @@ -41,7 +60,7 @@ void hcf() { extern void (*__init_array[])(); extern void (*__init_array_end[])(); -extern "C" void kernel_main(); +void kernel_main(); extern "C" void _start() { asm("cli"); @@ -75,8 +94,12 @@ extern "C" void _start() { hcf(); } + if (memmap_request.response == nullptr) { + hcf(); + } + // Fetch the first framebuffer. - limine_framebuffer *framebuffer = framebuffer_request.response->framebuffers[0]; + limine_framebuffer* framebuffer = framebuffer_request.response->framebuffers[0]; // Note: we assume the framebuffer model is RGB with 32-bit pixels. // for (size_t i = 0; i < 100; i++) { @@ -84,11 +107,10 @@ extern "C" void _start() { // fb_ptr[i * (framebuffer->pitch / 4) + i] = 0xffffff; // } - asm("xchgw %bx, %bx"); fb_init((uint32_t*)framebuffer->address, framebuffer->width, framebuffer->height); draw_pixel(727, 727, 0x9528fd); draw_pixel(0, 0, 0xff0000); - draw_pixel(1919, 1079, 0x00ff00); + draw_pixel(framebuffer->width - 1, framebuffer->height - 1, 0x00ff00); // for (size_t i = 0; i < framebuffer->mode_count; i++) { // printf("Mode %d\n", i); // printf("Pitch %d\nWidth %d\nHeight %d\nbpp %d\nmem_model %d\n", @@ -100,6 +122,17 @@ extern "C" void _start() { printf("Pitch %d\nWidth %d\nHeight %d\nbpp %d\nmem_model %d\n", framebuffer->pitch, framebuffer->width, framebuffer->height, framebuffer->bpp, framebuffer->memory_model); + + limine_memmap_response* memmap = memmap_request.response; + for (int i = 0; i < memmap->entry_count; i++) { + printf("base: %lx\nlength: %lx\ntype: %d\n\n", + memmap->entries[i]->base, memmap->entries[i]->length, memmap->entries[i]->type); + } + + // broken printf mirroring 64 bit values for some reason + //uint64_t tester = 0xFFFE0000000000AE + 1; + //printf("aaaa: %lx\n", tester); + pmm_init(memmap_request.response, hhdm_request.response->offset); kernel_main(); diff --git a/kernel/arch/x86_64/limine.h b/kernel/arch/x86_64/limine.h deleted file mode 100644 index b4afb43..0000000 --- a/kernel/arch/x86_64/limine.h +++ /dev/null @@ -1,587 +0,0 @@ -/* BSD Zero Clause License */ - -/* Copyright (C) 2022-2024 mintsuki and contributors. - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY - * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION - * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN - * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#ifndef LIMINE_H -#define LIMINE_H 1 - -#ifdef __cplusplus -extern "C" { -#endif - -#include - -/* Misc */ - -#ifdef LIMINE_NO_POINTERS -# define LIMINE_PTR(TYPE) uint64_t -#else -# define LIMINE_PTR(TYPE) TYPE -#endif - -#ifdef __GNUC__ -# define LIMINE_DEPRECATED __attribute__((__deprecated__)) -# define LIMINE_DEPRECATED_IGNORE_START \ - _Pragma("GCC diagnostic push") \ - _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"") -# define LIMINE_DEPRECATED_IGNORE_END \ - _Pragma("GCC diagnostic pop") -#else -# define LIMINE_DEPRECATED -# define LIMINE_DEPRECATED_IGNORE_START -# define LIMINE_DEPRECATED_IGNORE_END -#endif - -#define LIMINE_REQUESTS_START_MARKER \ - uint64_t limine_requests_start_marker[4] = { 0xf6b8f4b39de7d1ae, 0xfab91a6940fcb9cf, \ - 0x785c6ed015d3e316, 0x181e920a7852b9d9 }; -#define LIMINE_REQUESTS_END_MARKER \ - uint64_t limine_requests_end_marker[2] = { 0xadc0e0531bb10d03, 0x9572709f31764c62 }; - -#define LIMINE_REQUESTS_DELIMITER LIMINE_REQUESTS_END_MARKER - -#define LIMINE_BASE_REVISION(N) \ - uint64_t limine_base_revision[3] = { 0xf9562b2d5c95a6c8, 0x6a7b384944536bdc, (N) }; - -#define LIMINE_BASE_REVISION_SUPPORTED (limine_base_revision[2] == 0) - -#define LIMINE_COMMON_MAGIC 0xc7b1dd30df4c8b88, 0x0a82e883a194f07b - -struct limine_uuid { - uint32_t a; - uint16_t b; - uint16_t c; - uint8_t d[8]; -}; - -#define LIMINE_MEDIA_TYPE_GENERIC 0 -#define LIMINE_MEDIA_TYPE_OPTICAL 1 -#define LIMINE_MEDIA_TYPE_TFTP 2 - -struct limine_file { - uint64_t revision; - LIMINE_PTR(void *) address; - uint64_t size; - LIMINE_PTR(char *) path; - LIMINE_PTR(char *) cmdline; - uint32_t media_type; - uint32_t unused; - uint32_t tftp_ip; - uint32_t tftp_port; - uint32_t partition_index; - uint32_t mbr_disk_id; - struct limine_uuid gpt_disk_uuid; - struct limine_uuid gpt_part_uuid; - struct limine_uuid part_uuid; -}; - -/* Boot info */ - -#define LIMINE_BOOTLOADER_INFO_REQUEST { LIMINE_COMMON_MAGIC, 0xf55038d8e2a1202f, 0x279426fcf5f59740 } - -struct limine_bootloader_info_response { - uint64_t revision; - LIMINE_PTR(char *) name; - LIMINE_PTR(char *) version; -}; - -struct limine_bootloader_info_request { - uint64_t id[4]; - uint64_t revision; - LIMINE_PTR(struct limine_bootloader_info_response *) response; -}; - -/* Stack size */ - -#define LIMINE_STACK_SIZE_REQUEST { LIMINE_COMMON_MAGIC, 0x224ef0460a8e8926, 0xe1cb0fc25f46ea3d } - -struct limine_stack_size_response { - uint64_t revision; -}; - -struct limine_stack_size_request { - uint64_t id[4]; - uint64_t revision; - LIMINE_PTR(struct limine_stack_size_response *) response; - uint64_t stack_size; -}; - -/* HHDM */ - -#define LIMINE_HHDM_REQUEST { LIMINE_COMMON_MAGIC, 0x48dcf1cb8ad2b852, 0x63984e959a98244b } - -struct limine_hhdm_response { - uint64_t revision; - uint64_t offset; -}; - -struct limine_hhdm_request { - uint64_t id[4]; - uint64_t revision; - LIMINE_PTR(struct limine_hhdm_response *) response; -}; - -/* Framebuffer */ - -#define LIMINE_FRAMEBUFFER_REQUEST { LIMINE_COMMON_MAGIC, 0x9d5827dcd881dd75, 0xa3148604f6fab11b } - -#define LIMINE_FRAMEBUFFER_RGB 1 - -struct limine_video_mode { - uint64_t pitch; - uint64_t width; - uint64_t height; - uint16_t bpp; - uint8_t memory_model; - uint8_t red_mask_size; - uint8_t red_mask_shift; - uint8_t green_mask_size; - uint8_t green_mask_shift; - uint8_t blue_mask_size; - uint8_t blue_mask_shift; -}; - -struct limine_framebuffer { - LIMINE_PTR(void *) address; - uint64_t width; - uint64_t height; - uint64_t pitch; - uint16_t bpp; - uint8_t memory_model; - uint8_t red_mask_size; - uint8_t red_mask_shift; - uint8_t green_mask_size; - uint8_t green_mask_shift; - uint8_t blue_mask_size; - uint8_t blue_mask_shift; - uint8_t unused[7]; - uint64_t edid_size; - LIMINE_PTR(void *) edid; - /* Response revision 1 */ - uint64_t mode_count; - LIMINE_PTR(struct limine_video_mode **) modes; -}; - -struct limine_framebuffer_response { - uint64_t revision; - uint64_t framebuffer_count; - LIMINE_PTR(struct limine_framebuffer **) framebuffers; -}; - -struct limine_framebuffer_request { - uint64_t id[4]; - uint64_t revision; - LIMINE_PTR(struct limine_framebuffer_response *) response; -}; - -/* Terminal */ - -#define LIMINE_TERMINAL_REQUEST { LIMINE_COMMON_MAGIC, 0xc8ac59310c2b0844, 0xa68d0c7265d38878 } - -#define LIMINE_TERMINAL_CB_DEC 10 -#define LIMINE_TERMINAL_CB_BELL 20 -#define LIMINE_TERMINAL_CB_PRIVATE_ID 30 -#define LIMINE_TERMINAL_CB_STATUS_REPORT 40 -#define LIMINE_TERMINAL_CB_POS_REPORT 50 -#define LIMINE_TERMINAL_CB_KBD_LEDS 60 -#define LIMINE_TERMINAL_CB_MODE 70 -#define LIMINE_TERMINAL_CB_LINUX 80 - -#define LIMINE_TERMINAL_CTX_SIZE ((uint64_t)(-1)) -#define LIMINE_TERMINAL_CTX_SAVE ((uint64_t)(-2)) -#define LIMINE_TERMINAL_CTX_RESTORE ((uint64_t)(-3)) -#define LIMINE_TERMINAL_FULL_REFRESH ((uint64_t)(-4)) - -/* Response revision 1 */ -#define LIMINE_TERMINAL_OOB_OUTPUT_GET ((uint64_t)(-10)) -#define LIMINE_TERMINAL_OOB_OUTPUT_SET ((uint64_t)(-11)) - -#define LIMINE_TERMINAL_OOB_OUTPUT_OCRNL (1 << 0) -#define LIMINE_TERMINAL_OOB_OUTPUT_OFDEL (1 << 1) -#define LIMINE_TERMINAL_OOB_OUTPUT_OFILL (1 << 2) -#define LIMINE_TERMINAL_OOB_OUTPUT_OLCUC (1 << 3) -#define LIMINE_TERMINAL_OOB_OUTPUT_ONLCR (1 << 4) -#define LIMINE_TERMINAL_OOB_OUTPUT_ONLRET (1 << 5) -#define LIMINE_TERMINAL_OOB_OUTPUT_ONOCR (1 << 6) -#define LIMINE_TERMINAL_OOB_OUTPUT_OPOST (1 << 7) - -LIMINE_DEPRECATED_IGNORE_START - -struct LIMINE_DEPRECATED limine_terminal; - -typedef void (*limine_terminal_write)(struct limine_terminal *, const char *, uint64_t); -typedef void (*limine_terminal_callback)(struct limine_terminal *, uint64_t, uint64_t, uint64_t, uint64_t); - -struct LIMINE_DEPRECATED limine_terminal { - uint64_t columns; - uint64_t rows; - LIMINE_PTR(struct limine_framebuffer *) framebuffer; -}; - -struct LIMINE_DEPRECATED limine_terminal_response { - uint64_t revision; - uint64_t terminal_count; - LIMINE_PTR(struct limine_terminal **) terminals; - LIMINE_PTR(limine_terminal_write) write; -}; - -struct LIMINE_DEPRECATED limine_terminal_request { - uint64_t id[4]; - uint64_t revision; - LIMINE_PTR(struct limine_terminal_response *) response; - LIMINE_PTR(limine_terminal_callback) callback; -}; - -LIMINE_DEPRECATED_IGNORE_END - -/* Paging mode */ - -#define LIMINE_PAGING_MODE_REQUEST { LIMINE_COMMON_MAGIC, 0x95c1a0edab0944cb, 0xa4e5cb3842f7488a } - -#if defined (__x86_64__) || defined (__i386__) -#define LIMINE_PAGING_MODE_X86_64_4LVL 0 -#define LIMINE_PAGING_MODE_X86_64_5LVL 1 -#define LIMINE_PAGING_MODE_MAX LIMINE_PAGING_MODE_X86_64_5LVL -#define LIMINE_PAGING_MODE_DEFAULT LIMINE_PAGING_MODE_X86_64_4LVL -#elif defined (__aarch64__) -#define LIMINE_PAGING_MODE_AARCH64_4LVL 0 -#define LIMINE_PAGING_MODE_AARCH64_5LVL 1 -#define LIMINE_PAGING_MODE_MAX LIMINE_PAGING_MODE_AARCH64_5LVL -#define LIMINE_PAGING_MODE_DEFAULT LIMINE_PAGING_MODE_AARCH64_4LVL -#elif defined (__riscv) && (__riscv_xlen == 64) -#define LIMINE_PAGING_MODE_RISCV_SV39 0 -#define LIMINE_PAGING_MODE_RISCV_SV48 1 -#define LIMINE_PAGING_MODE_RISCV_SV57 2 -#define LIMINE_PAGING_MODE_MAX LIMINE_PAGING_MODE_RISCV_SV57 -#define LIMINE_PAGING_MODE_DEFAULT LIMINE_PAGING_MODE_RISCV_SV48 -#else -#error Unknown architecture -#endif - -struct limine_paging_mode_response { - uint64_t revision; - uint64_t mode; - uint64_t flags; -}; - -struct limine_paging_mode_request { - uint64_t id[4]; - uint64_t revision; - LIMINE_PTR(struct limine_paging_mode_response *) response; - uint64_t mode; - uint64_t flags; -}; - -/* 5-level paging */ - -#define LIMINE_5_LEVEL_PAGING_REQUEST { LIMINE_COMMON_MAGIC, 0x94469551da9b3192, 0xebe5e86db7382888 } - -LIMINE_DEPRECATED_IGNORE_START - -struct LIMINE_DEPRECATED limine_5_level_paging_response { - uint64_t revision; -}; - -struct LIMINE_DEPRECATED limine_5_level_paging_request { - uint64_t id[4]; - uint64_t revision; - LIMINE_PTR(struct limine_5_level_paging_response *) response; -}; - -LIMINE_DEPRECATED_IGNORE_END - -/* SMP */ - -#define LIMINE_SMP_REQUEST { LIMINE_COMMON_MAGIC, 0x95a67b819a1b857e, 0xa0b61b723b6a73e0 } - -struct limine_smp_info; - -typedef void (*limine_goto_address)(struct limine_smp_info *); - -#if defined (__x86_64__) || defined (__i386__) - -#define LIMINE_SMP_X2APIC (1 << 0) - -struct limine_smp_info { - uint32_t processor_id; - uint32_t lapic_id; - uint64_t reserved; - LIMINE_PTR(limine_goto_address) goto_address; - uint64_t extra_argument; -}; - -struct limine_smp_response { - uint64_t revision; - uint32_t flags; - uint32_t bsp_lapic_id; - uint64_t cpu_count; - LIMINE_PTR(struct limine_smp_info **) cpus; -}; - -#elif defined (__aarch64__) - -struct limine_smp_info { - uint32_t processor_id; - uint32_t gic_iface_no; - uint64_t mpidr; - uint64_t reserved; - LIMINE_PTR(limine_goto_address) goto_address; - uint64_t extra_argument; -}; - -struct limine_smp_response { - uint64_t revision; - uint64_t flags; - uint64_t bsp_mpidr; - uint64_t cpu_count; - LIMINE_PTR(struct limine_smp_info **) cpus; -}; - -#elif defined (__riscv) && (__riscv_xlen == 64) - -struct limine_smp_info { - uint64_t processor_id; - uint64_t hartid; - uint64_t reserved; - LIMINE_PTR(limine_goto_address) goto_address; - uint64_t extra_argument; -}; - -struct limine_smp_response { - uint64_t revision; - uint64_t flags; - uint64_t bsp_hartid; - uint64_t cpu_count; - LIMINE_PTR(struct limine_smp_info **) cpus; -}; - -#else -#error Unknown architecture -#endif - -struct limine_smp_request { - uint64_t id[4]; - uint64_t revision; - LIMINE_PTR(struct limine_smp_response *) response; - uint64_t flags; -}; - -/* Memory map */ - -#define LIMINE_MEMMAP_REQUEST { LIMINE_COMMON_MAGIC, 0x67cf3d9d378a806f, 0xe304acdfc50c3c62 } - -#define LIMINE_MEMMAP_USABLE 0 -#define LIMINE_MEMMAP_RESERVED 1 -#define LIMINE_MEMMAP_ACPI_RECLAIMABLE 2 -#define LIMINE_MEMMAP_ACPI_NVS 3 -#define LIMINE_MEMMAP_BAD_MEMORY 4 -#define LIMINE_MEMMAP_BOOTLOADER_RECLAIMABLE 5 -#define LIMINE_MEMMAP_KERNEL_AND_MODULES 6 -#define LIMINE_MEMMAP_FRAMEBUFFER 7 - -struct limine_memmap_entry { - uint64_t base; - uint64_t length; - uint64_t type; -}; - -struct limine_memmap_response { - uint64_t revision; - uint64_t entry_count; - LIMINE_PTR(struct limine_memmap_entry **) entries; -}; - -struct limine_memmap_request { - uint64_t id[4]; - uint64_t revision; - LIMINE_PTR(struct limine_memmap_response *) response; -}; - -/* Entry point */ - -#define LIMINE_ENTRY_POINT_REQUEST { LIMINE_COMMON_MAGIC, 0x13d86c035a1cd3e1, 0x2b0caa89d8f3026a } - -typedef void (*limine_entry_point)(void); - -struct limine_entry_point_response { - uint64_t revision; -}; - -struct limine_entry_point_request { - uint64_t id[4]; - uint64_t revision; - LIMINE_PTR(struct limine_entry_point_response *) response; - LIMINE_PTR(limine_entry_point) entry; -}; - -/* Kernel File */ - -#define LIMINE_KERNEL_FILE_REQUEST { LIMINE_COMMON_MAGIC, 0xad97e90e83f1ed67, 0x31eb5d1c5ff23b69 } - -struct limine_kernel_file_response { - uint64_t revision; - LIMINE_PTR(struct limine_file *) kernel_file; -}; - -struct limine_kernel_file_request { - uint64_t id[4]; - uint64_t revision; - LIMINE_PTR(struct limine_kernel_file_response *) response; -}; - -/* Module */ - -#define LIMINE_MODULE_REQUEST { LIMINE_COMMON_MAGIC, 0x3e7e279702be32af, 0xca1c4f3bd1280cee } - -#define LIMINE_INTERNAL_MODULE_REQUIRED (1 << 0) -#define LIMINE_INTERNAL_MODULE_COMPRESSED (1 << 1) - -struct limine_internal_module { - LIMINE_PTR(const char *) path; - LIMINE_PTR(const char *) cmdline; - uint64_t flags; -}; - -struct limine_module_response { - uint64_t revision; - uint64_t module_count; - LIMINE_PTR(struct limine_file **) modules; -}; - -struct limine_module_request { - uint64_t id[4]; - uint64_t revision; - LIMINE_PTR(struct limine_module_response *) response; - - /* Request revision 1 */ - uint64_t internal_module_count; - LIMINE_PTR(struct limine_internal_module **) internal_modules; -}; - -/* RSDP */ - -#define LIMINE_RSDP_REQUEST { LIMINE_COMMON_MAGIC, 0xc5e77b6b397e7b43, 0x27637845accdcf3c } - -struct limine_rsdp_response { - uint64_t revision; - LIMINE_PTR(void *) address; -}; - -struct limine_rsdp_request { - uint64_t id[4]; - uint64_t revision; - LIMINE_PTR(struct limine_rsdp_response *) response; -}; - -/* SMBIOS */ - -#define LIMINE_SMBIOS_REQUEST { LIMINE_COMMON_MAGIC, 0x9e9046f11e095391, 0xaa4a520fefbde5ee } - -struct limine_smbios_response { - uint64_t revision; - LIMINE_PTR(void *) entry_32; - LIMINE_PTR(void *) entry_64; -}; - -struct limine_smbios_request { - uint64_t id[4]; - uint64_t revision; - LIMINE_PTR(struct limine_smbios_response *) response; -}; - -/* EFI system table */ - -#define LIMINE_EFI_SYSTEM_TABLE_REQUEST { LIMINE_COMMON_MAGIC, 0x5ceba5163eaaf6d6, 0x0a6981610cf65fcc } - -struct limine_efi_system_table_response { - uint64_t revision; - LIMINE_PTR(void *) address; -}; - -struct limine_efi_system_table_request { - uint64_t id[4]; - uint64_t revision; - LIMINE_PTR(struct limine_efi_system_table_response *) response; -}; - -/* EFI memory map */ - -#define LIMINE_EFI_MEMMAP_REQUEST { LIMINE_COMMON_MAGIC, 0x7df62a431d6872d5, 0xa4fcdfb3e57306c8 } - -struct limine_efi_memmap_response { - uint64_t revision; - LIMINE_PTR(void *) memmap; - uint64_t memmap_size; - uint64_t desc_size; - uint64_t desc_version; -}; - -struct limine_efi_memmap_request { - uint64_t id[4]; - uint64_t revision; - LIMINE_PTR(struct limine_efi_memmap_response *) response; -}; - -/* Boot time */ - -#define LIMINE_BOOT_TIME_REQUEST { LIMINE_COMMON_MAGIC, 0x502746e184c088aa, 0xfbc5ec83e6327893 } - -struct limine_boot_time_response { - uint64_t revision; - int64_t boot_time; -}; - -struct limine_boot_time_request { - uint64_t id[4]; - uint64_t revision; - LIMINE_PTR(struct limine_boot_time_response *) response; -}; - -/* Kernel address */ - -#define LIMINE_KERNEL_ADDRESS_REQUEST { LIMINE_COMMON_MAGIC, 0x71ba76863cc55f63, 0xb2644a48c516a487 } - -struct limine_kernel_address_response { - uint64_t revision; - uint64_t physical_base; - uint64_t virtual_base; -}; - -struct limine_kernel_address_request { - uint64_t id[4]; - uint64_t revision; - LIMINE_PTR(struct limine_kernel_address_response *) response; -}; - -/* Device Tree Blob */ - -#define LIMINE_DTB_REQUEST { LIMINE_COMMON_MAGIC, 0xb40ddb48fb54bac7, 0x545081493f81ffb7 } - -struct limine_dtb_response { - uint64_t revision; - LIMINE_PTR(void *) dtb_ptr; -}; - -struct limine_dtb_request { - uint64_t id[4]; - uint64_t revision; - LIMINE_PTR(struct limine_dtb_response *) response; -}; - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/kernel/include/limine.h b/kernel/include/limine.h new file mode 100644 index 0000000..b4afb43 --- /dev/null +++ b/kernel/include/limine.h @@ -0,0 +1,587 @@ +/* BSD Zero Clause License */ + +/* Copyright (C) 2022-2024 mintsuki and contributors. + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION + * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN + * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef LIMINE_H +#define LIMINE_H 1 + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +/* Misc */ + +#ifdef LIMINE_NO_POINTERS +# define LIMINE_PTR(TYPE) uint64_t +#else +# define LIMINE_PTR(TYPE) TYPE +#endif + +#ifdef __GNUC__ +# define LIMINE_DEPRECATED __attribute__((__deprecated__)) +# define LIMINE_DEPRECATED_IGNORE_START \ + _Pragma("GCC diagnostic push") \ + _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"") +# define LIMINE_DEPRECATED_IGNORE_END \ + _Pragma("GCC diagnostic pop") +#else +# define LIMINE_DEPRECATED +# define LIMINE_DEPRECATED_IGNORE_START +# define LIMINE_DEPRECATED_IGNORE_END +#endif + +#define LIMINE_REQUESTS_START_MARKER \ + uint64_t limine_requests_start_marker[4] = { 0xf6b8f4b39de7d1ae, 0xfab91a6940fcb9cf, \ + 0x785c6ed015d3e316, 0x181e920a7852b9d9 }; +#define LIMINE_REQUESTS_END_MARKER \ + uint64_t limine_requests_end_marker[2] = { 0xadc0e0531bb10d03, 0x9572709f31764c62 }; + +#define LIMINE_REQUESTS_DELIMITER LIMINE_REQUESTS_END_MARKER + +#define LIMINE_BASE_REVISION(N) \ + uint64_t limine_base_revision[3] = { 0xf9562b2d5c95a6c8, 0x6a7b384944536bdc, (N) }; + +#define LIMINE_BASE_REVISION_SUPPORTED (limine_base_revision[2] == 0) + +#define LIMINE_COMMON_MAGIC 0xc7b1dd30df4c8b88, 0x0a82e883a194f07b + +struct limine_uuid { + uint32_t a; + uint16_t b; + uint16_t c; + uint8_t d[8]; +}; + +#define LIMINE_MEDIA_TYPE_GENERIC 0 +#define LIMINE_MEDIA_TYPE_OPTICAL 1 +#define LIMINE_MEDIA_TYPE_TFTP 2 + +struct limine_file { + uint64_t revision; + LIMINE_PTR(void *) address; + uint64_t size; + LIMINE_PTR(char *) path; + LIMINE_PTR(char *) cmdline; + uint32_t media_type; + uint32_t unused; + uint32_t tftp_ip; + uint32_t tftp_port; + uint32_t partition_index; + uint32_t mbr_disk_id; + struct limine_uuid gpt_disk_uuid; + struct limine_uuid gpt_part_uuid; + struct limine_uuid part_uuid; +}; + +/* Boot info */ + +#define LIMINE_BOOTLOADER_INFO_REQUEST { LIMINE_COMMON_MAGIC, 0xf55038d8e2a1202f, 0x279426fcf5f59740 } + +struct limine_bootloader_info_response { + uint64_t revision; + LIMINE_PTR(char *) name; + LIMINE_PTR(char *) version; +}; + +struct limine_bootloader_info_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_bootloader_info_response *) response; +}; + +/* Stack size */ + +#define LIMINE_STACK_SIZE_REQUEST { LIMINE_COMMON_MAGIC, 0x224ef0460a8e8926, 0xe1cb0fc25f46ea3d } + +struct limine_stack_size_response { + uint64_t revision; +}; + +struct limine_stack_size_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_stack_size_response *) response; + uint64_t stack_size; +}; + +/* HHDM */ + +#define LIMINE_HHDM_REQUEST { LIMINE_COMMON_MAGIC, 0x48dcf1cb8ad2b852, 0x63984e959a98244b } + +struct limine_hhdm_response { + uint64_t revision; + uint64_t offset; +}; + +struct limine_hhdm_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_hhdm_response *) response; +}; + +/* Framebuffer */ + +#define LIMINE_FRAMEBUFFER_REQUEST { LIMINE_COMMON_MAGIC, 0x9d5827dcd881dd75, 0xa3148604f6fab11b } + +#define LIMINE_FRAMEBUFFER_RGB 1 + +struct limine_video_mode { + uint64_t pitch; + uint64_t width; + uint64_t height; + uint16_t bpp; + uint8_t memory_model; + uint8_t red_mask_size; + uint8_t red_mask_shift; + uint8_t green_mask_size; + uint8_t green_mask_shift; + uint8_t blue_mask_size; + uint8_t blue_mask_shift; +}; + +struct limine_framebuffer { + LIMINE_PTR(void *) address; + uint64_t width; + uint64_t height; + uint64_t pitch; + uint16_t bpp; + uint8_t memory_model; + uint8_t red_mask_size; + uint8_t red_mask_shift; + uint8_t green_mask_size; + uint8_t green_mask_shift; + uint8_t blue_mask_size; + uint8_t blue_mask_shift; + uint8_t unused[7]; + uint64_t edid_size; + LIMINE_PTR(void *) edid; + /* Response revision 1 */ + uint64_t mode_count; + LIMINE_PTR(struct limine_video_mode **) modes; +}; + +struct limine_framebuffer_response { + uint64_t revision; + uint64_t framebuffer_count; + LIMINE_PTR(struct limine_framebuffer **) framebuffers; +}; + +struct limine_framebuffer_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_framebuffer_response *) response; +}; + +/* Terminal */ + +#define LIMINE_TERMINAL_REQUEST { LIMINE_COMMON_MAGIC, 0xc8ac59310c2b0844, 0xa68d0c7265d38878 } + +#define LIMINE_TERMINAL_CB_DEC 10 +#define LIMINE_TERMINAL_CB_BELL 20 +#define LIMINE_TERMINAL_CB_PRIVATE_ID 30 +#define LIMINE_TERMINAL_CB_STATUS_REPORT 40 +#define LIMINE_TERMINAL_CB_POS_REPORT 50 +#define LIMINE_TERMINAL_CB_KBD_LEDS 60 +#define LIMINE_TERMINAL_CB_MODE 70 +#define LIMINE_TERMINAL_CB_LINUX 80 + +#define LIMINE_TERMINAL_CTX_SIZE ((uint64_t)(-1)) +#define LIMINE_TERMINAL_CTX_SAVE ((uint64_t)(-2)) +#define LIMINE_TERMINAL_CTX_RESTORE ((uint64_t)(-3)) +#define LIMINE_TERMINAL_FULL_REFRESH ((uint64_t)(-4)) + +/* Response revision 1 */ +#define LIMINE_TERMINAL_OOB_OUTPUT_GET ((uint64_t)(-10)) +#define LIMINE_TERMINAL_OOB_OUTPUT_SET ((uint64_t)(-11)) + +#define LIMINE_TERMINAL_OOB_OUTPUT_OCRNL (1 << 0) +#define LIMINE_TERMINAL_OOB_OUTPUT_OFDEL (1 << 1) +#define LIMINE_TERMINAL_OOB_OUTPUT_OFILL (1 << 2) +#define LIMINE_TERMINAL_OOB_OUTPUT_OLCUC (1 << 3) +#define LIMINE_TERMINAL_OOB_OUTPUT_ONLCR (1 << 4) +#define LIMINE_TERMINAL_OOB_OUTPUT_ONLRET (1 << 5) +#define LIMINE_TERMINAL_OOB_OUTPUT_ONOCR (1 << 6) +#define LIMINE_TERMINAL_OOB_OUTPUT_OPOST (1 << 7) + +LIMINE_DEPRECATED_IGNORE_START + +struct LIMINE_DEPRECATED limine_terminal; + +typedef void (*limine_terminal_write)(struct limine_terminal *, const char *, uint64_t); +typedef void (*limine_terminal_callback)(struct limine_terminal *, uint64_t, uint64_t, uint64_t, uint64_t); + +struct LIMINE_DEPRECATED limine_terminal { + uint64_t columns; + uint64_t rows; + LIMINE_PTR(struct limine_framebuffer *) framebuffer; +}; + +struct LIMINE_DEPRECATED limine_terminal_response { + uint64_t revision; + uint64_t terminal_count; + LIMINE_PTR(struct limine_terminal **) terminals; + LIMINE_PTR(limine_terminal_write) write; +}; + +struct LIMINE_DEPRECATED limine_terminal_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_terminal_response *) response; + LIMINE_PTR(limine_terminal_callback) callback; +}; + +LIMINE_DEPRECATED_IGNORE_END + +/* Paging mode */ + +#define LIMINE_PAGING_MODE_REQUEST { LIMINE_COMMON_MAGIC, 0x95c1a0edab0944cb, 0xa4e5cb3842f7488a } + +#if defined (__x86_64__) || defined (__i386__) +#define LIMINE_PAGING_MODE_X86_64_4LVL 0 +#define LIMINE_PAGING_MODE_X86_64_5LVL 1 +#define LIMINE_PAGING_MODE_MAX LIMINE_PAGING_MODE_X86_64_5LVL +#define LIMINE_PAGING_MODE_DEFAULT LIMINE_PAGING_MODE_X86_64_4LVL +#elif defined (__aarch64__) +#define LIMINE_PAGING_MODE_AARCH64_4LVL 0 +#define LIMINE_PAGING_MODE_AARCH64_5LVL 1 +#define LIMINE_PAGING_MODE_MAX LIMINE_PAGING_MODE_AARCH64_5LVL +#define LIMINE_PAGING_MODE_DEFAULT LIMINE_PAGING_MODE_AARCH64_4LVL +#elif defined (__riscv) && (__riscv_xlen == 64) +#define LIMINE_PAGING_MODE_RISCV_SV39 0 +#define LIMINE_PAGING_MODE_RISCV_SV48 1 +#define LIMINE_PAGING_MODE_RISCV_SV57 2 +#define LIMINE_PAGING_MODE_MAX LIMINE_PAGING_MODE_RISCV_SV57 +#define LIMINE_PAGING_MODE_DEFAULT LIMINE_PAGING_MODE_RISCV_SV48 +#else +#error Unknown architecture +#endif + +struct limine_paging_mode_response { + uint64_t revision; + uint64_t mode; + uint64_t flags; +}; + +struct limine_paging_mode_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_paging_mode_response *) response; + uint64_t mode; + uint64_t flags; +}; + +/* 5-level paging */ + +#define LIMINE_5_LEVEL_PAGING_REQUEST { LIMINE_COMMON_MAGIC, 0x94469551da9b3192, 0xebe5e86db7382888 } + +LIMINE_DEPRECATED_IGNORE_START + +struct LIMINE_DEPRECATED limine_5_level_paging_response { + uint64_t revision; +}; + +struct LIMINE_DEPRECATED limine_5_level_paging_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_5_level_paging_response *) response; +}; + +LIMINE_DEPRECATED_IGNORE_END + +/* SMP */ + +#define LIMINE_SMP_REQUEST { LIMINE_COMMON_MAGIC, 0x95a67b819a1b857e, 0xa0b61b723b6a73e0 } + +struct limine_smp_info; + +typedef void (*limine_goto_address)(struct limine_smp_info *); + +#if defined (__x86_64__) || defined (__i386__) + +#define LIMINE_SMP_X2APIC (1 << 0) + +struct limine_smp_info { + uint32_t processor_id; + uint32_t lapic_id; + uint64_t reserved; + LIMINE_PTR(limine_goto_address) goto_address; + uint64_t extra_argument; +}; + +struct limine_smp_response { + uint64_t revision; + uint32_t flags; + uint32_t bsp_lapic_id; + uint64_t cpu_count; + LIMINE_PTR(struct limine_smp_info **) cpus; +}; + +#elif defined (__aarch64__) + +struct limine_smp_info { + uint32_t processor_id; + uint32_t gic_iface_no; + uint64_t mpidr; + uint64_t reserved; + LIMINE_PTR(limine_goto_address) goto_address; + uint64_t extra_argument; +}; + +struct limine_smp_response { + uint64_t revision; + uint64_t flags; + uint64_t bsp_mpidr; + uint64_t cpu_count; + LIMINE_PTR(struct limine_smp_info **) cpus; +}; + +#elif defined (__riscv) && (__riscv_xlen == 64) + +struct limine_smp_info { + uint64_t processor_id; + uint64_t hartid; + uint64_t reserved; + LIMINE_PTR(limine_goto_address) goto_address; + uint64_t extra_argument; +}; + +struct limine_smp_response { + uint64_t revision; + uint64_t flags; + uint64_t bsp_hartid; + uint64_t cpu_count; + LIMINE_PTR(struct limine_smp_info **) cpus; +}; + +#else +#error Unknown architecture +#endif + +struct limine_smp_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_smp_response *) response; + uint64_t flags; +}; + +/* Memory map */ + +#define LIMINE_MEMMAP_REQUEST { LIMINE_COMMON_MAGIC, 0x67cf3d9d378a806f, 0xe304acdfc50c3c62 } + +#define LIMINE_MEMMAP_USABLE 0 +#define LIMINE_MEMMAP_RESERVED 1 +#define LIMINE_MEMMAP_ACPI_RECLAIMABLE 2 +#define LIMINE_MEMMAP_ACPI_NVS 3 +#define LIMINE_MEMMAP_BAD_MEMORY 4 +#define LIMINE_MEMMAP_BOOTLOADER_RECLAIMABLE 5 +#define LIMINE_MEMMAP_KERNEL_AND_MODULES 6 +#define LIMINE_MEMMAP_FRAMEBUFFER 7 + +struct limine_memmap_entry { + uint64_t base; + uint64_t length; + uint64_t type; +}; + +struct limine_memmap_response { + uint64_t revision; + uint64_t entry_count; + LIMINE_PTR(struct limine_memmap_entry **) entries; +}; + +struct limine_memmap_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_memmap_response *) response; +}; + +/* Entry point */ + +#define LIMINE_ENTRY_POINT_REQUEST { LIMINE_COMMON_MAGIC, 0x13d86c035a1cd3e1, 0x2b0caa89d8f3026a } + +typedef void (*limine_entry_point)(void); + +struct limine_entry_point_response { + uint64_t revision; +}; + +struct limine_entry_point_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_entry_point_response *) response; + LIMINE_PTR(limine_entry_point) entry; +}; + +/* Kernel File */ + +#define LIMINE_KERNEL_FILE_REQUEST { LIMINE_COMMON_MAGIC, 0xad97e90e83f1ed67, 0x31eb5d1c5ff23b69 } + +struct limine_kernel_file_response { + uint64_t revision; + LIMINE_PTR(struct limine_file *) kernel_file; +}; + +struct limine_kernel_file_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_kernel_file_response *) response; +}; + +/* Module */ + +#define LIMINE_MODULE_REQUEST { LIMINE_COMMON_MAGIC, 0x3e7e279702be32af, 0xca1c4f3bd1280cee } + +#define LIMINE_INTERNAL_MODULE_REQUIRED (1 << 0) +#define LIMINE_INTERNAL_MODULE_COMPRESSED (1 << 1) + +struct limine_internal_module { + LIMINE_PTR(const char *) path; + LIMINE_PTR(const char *) cmdline; + uint64_t flags; +}; + +struct limine_module_response { + uint64_t revision; + uint64_t module_count; + LIMINE_PTR(struct limine_file **) modules; +}; + +struct limine_module_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_module_response *) response; + + /* Request revision 1 */ + uint64_t internal_module_count; + LIMINE_PTR(struct limine_internal_module **) internal_modules; +}; + +/* RSDP */ + +#define LIMINE_RSDP_REQUEST { LIMINE_COMMON_MAGIC, 0xc5e77b6b397e7b43, 0x27637845accdcf3c } + +struct limine_rsdp_response { + uint64_t revision; + LIMINE_PTR(void *) address; +}; + +struct limine_rsdp_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_rsdp_response *) response; +}; + +/* SMBIOS */ + +#define LIMINE_SMBIOS_REQUEST { LIMINE_COMMON_MAGIC, 0x9e9046f11e095391, 0xaa4a520fefbde5ee } + +struct limine_smbios_response { + uint64_t revision; + LIMINE_PTR(void *) entry_32; + LIMINE_PTR(void *) entry_64; +}; + +struct limine_smbios_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_smbios_response *) response; +}; + +/* EFI system table */ + +#define LIMINE_EFI_SYSTEM_TABLE_REQUEST { LIMINE_COMMON_MAGIC, 0x5ceba5163eaaf6d6, 0x0a6981610cf65fcc } + +struct limine_efi_system_table_response { + uint64_t revision; + LIMINE_PTR(void *) address; +}; + +struct limine_efi_system_table_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_efi_system_table_response *) response; +}; + +/* EFI memory map */ + +#define LIMINE_EFI_MEMMAP_REQUEST { LIMINE_COMMON_MAGIC, 0x7df62a431d6872d5, 0xa4fcdfb3e57306c8 } + +struct limine_efi_memmap_response { + uint64_t revision; + LIMINE_PTR(void *) memmap; + uint64_t memmap_size; + uint64_t desc_size; + uint64_t desc_version; +}; + +struct limine_efi_memmap_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_efi_memmap_response *) response; +}; + +/* Boot time */ + +#define LIMINE_BOOT_TIME_REQUEST { LIMINE_COMMON_MAGIC, 0x502746e184c088aa, 0xfbc5ec83e6327893 } + +struct limine_boot_time_response { + uint64_t revision; + int64_t boot_time; +}; + +struct limine_boot_time_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_boot_time_response *) response; +}; + +/* Kernel address */ + +#define LIMINE_KERNEL_ADDRESS_REQUEST { LIMINE_COMMON_MAGIC, 0x71ba76863cc55f63, 0xb2644a48c516a487 } + +struct limine_kernel_address_response { + uint64_t revision; + uint64_t physical_base; + uint64_t virtual_base; +}; + +struct limine_kernel_address_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_kernel_address_response *) response; +}; + +/* Device Tree Blob */ + +#define LIMINE_DTB_REQUEST { LIMINE_COMMON_MAGIC, 0xb40ddb48fb54bac7, 0x545081493f81ffb7 } + +struct limine_dtb_response { + uint64_t revision; + LIMINE_PTR(void *) dtb_ptr; +}; + +struct limine_dtb_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_dtb_response *) response; +}; + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/kernel/include/mm/pmm.h b/kernel/include/mm/pmm.h new file mode 100644 index 0000000..bfe21c5 --- /dev/null +++ b/kernel/include/mm/pmm.h @@ -0,0 +1,27 @@ +#ifndef PMM_H +#define PMM_H + +#include +#include +#include + +enum mm_type { + MEM_USABLE, + MEM_RESERVED +}; + +struct MMEntry { + uint64_t base; + uint64_t length; + mm_type type; +}; + +struct PBitmap { + uint64_t* addr; + size_t size; + MMEntry entry; +}; + +void pmm_init(limine_memmap_response* memmap, uint64_t hhdm_offset); + +#endif \ No newline at end of file diff --git a/kernel/kernel.cpp b/kernel/kernel.cpp index a0394ea..e72500e 100644 --- a/kernel/kernel.cpp +++ b/kernel/kernel.cpp @@ -12,7 +12,7 @@ unsigned int _kernel_begin; unsigned int _kernel_end; -extern "C" void kernel_main(void) { +void kernel_main(void) { // Initialize terminal //terminal_initialize(); diff --git a/kernel/makefile b/kernel/makefile index dfced86..030caaa 100644 --- a/kernel/makefile +++ b/kernel/makefile @@ -3,15 +3,16 @@ CFLAGS := -ffreestanding -Wall -Wextra -g -std=gnu99 -O2 -Iinclude --sysroot=$(SYSROOT) \ -isystem="/usr/include" CXXFLAGS := -ffreestanding -Wall -Wextra -fno-exceptions -fno-rtti -g -O2 -Iinclude --sysroot=$(SYSROOT) \ - -isystem="/usr/include" + -isystem="/usr/include" -std=gnu++20 LDFLAGS := -T arch/$(ARCH)/linker.ld -ffreestanding -g -O2 -Iinclude --sysroot=$(SYSROOT) \ -isystem="/usr/include" ASMFLAGS := ifeq ($(ARCH),x86_64) -CFLAGS += -mno-red-zone -mcmodel=kernel -mno-sse -mno-sse2 -mno-mmx -CXXFLAGS += -mno-red-zone -mcmodel=kernel -mno-sse -mno-sse2 -mno-mmx -LDFLAGS += -mno-red-zone -mcmodel=kernel -mno-sse -mno-sse2 -mno-mmx +CFLAGS += -mno-red-zone -mcmodel=kernel -mno-80387 -mno-sse -mno-sse2 -mno-mmx +CXXFLAGS += -mno-red-zone -mcmodel=kernel -mno-80387 -mno-sse -mno-sse2 -mno-mmx +LDFLAGS += -mno-red-zone -mcmodel=kernel -mno-80387 -mno-sse -mno-sse2 -mno-mmx \ + -z max-page-size=0x1000 ASMFLAGS += -felf64 endif @@ -75,4 +76,4 @@ install-headers: $(HEADERS) clean: $(info [INFO] Cleaning) - $(RM) ${OBJS} $(DEPFILES) kernel.bin \ No newline at end of file + $(RM) ${OBJS} $(DEPFILES) kernel.bin diff --git a/kernel/mm/pmm.cpp b/kernel/mm/pmm.cpp new file mode 100644 index 0000000..bd8f12c --- /dev/null +++ b/kernel/mm/pmm.cpp @@ -0,0 +1,55 @@ +#include +#include + +PBitmap bitmap; + +// Finds the biggest chunk of memory +static MMEntry find_mem_chunk(limine_memmap_response* memmap) { + uint64_t highestmem = 0; + size_t highestindex = 0; + limine_memmap_entry* entries = memmap->entries[0]; + for (size_t i = 0; i < memmap->entry_count; i++) { + if (entries[i].type == LIMINE_MEMMAP_USABLE && entries[i].length > highestmem) { + highestmem = entries[i].length; + highestindex = i; + } + } + + return MMEntry{ + entries[highestindex].base, + entries[highestindex].length, + MEM_USABLE + }; +} + +void pmm_init(limine_memmap_response* memmap, uint64_t hhdm_offset) { + MMEntry mem_entry = find_mem_chunk(memmap); + + // Round any numbers needed to 4K boudaries + //mem_entry.base += 4096 - (mem_entry.base % 4096); + mem_entry.length -= mem_entry.length % 4096; + + // setup bitmap at the beginning of the chunk + bitmap.entry = mem_entry; + bitmap.addr = (uint64_t*)(bitmap.entry.base + hhdm_offset); + // divided by byte size since size is for iterating over the bitmap + bitmap.size = mem_entry.length / 4096 / sizeof(uint64_t); + + printf("addr: %lx\nsize: %lx\n", (uint64_t)bitmap.addr, bitmap.size); + printf("hhdm offset: %lx\n", hhdm_offset); + + // write test + for (size_t i = 0; i < 100; i++) { + bitmap.addr[i] = i; + //printf("value %d: %lx\n", i, bitmap.addr[i]); + } + + // read test + for (size_t i = 0; i < bitmap.size; i++) { + //printf("value %d: %lx\n"); + } +} + +uint64_t get_phys_page() { + +} \ No newline at end of file diff --git a/libc/printf.c b/libc/printf.c index 72a4749..0bb5506 100644 --- a/libc/printf.c +++ b/libc/printf.c @@ -37,10 +37,14 @@ static const char* itoa(unsigned int num, char* str, unsigned int base) { } // reverse the string - for (int i = 0; i < cou; i++) { - char temp = str[i]; - str[i] = str[cou - i - 1]; - str[cou - 1] = temp; + size_t start = 0; + size_t end = cou - 1; + while (start < end) { + char temp = str[start]; + str[start] = str[end]; + str[end] = temp; + ++start; + --end; } str[cou] = '\0'; @@ -49,7 +53,7 @@ static const char* itoa(unsigned int num, char* str, unsigned int base) { // have no idea if there's a better way to do this. for %lx static const char* itoa_u64(unsigned long long num, char* str, int base) { - int cou = 0; // nt + size_t cou = 0; // nt if (num == 0) { // 0 str[cou] = '0'; @@ -71,10 +75,14 @@ static const char* itoa_u64(unsigned long long num, char* str, int base) { } // reverse the string - for (int i = 0; i < cou; i++) { - char temp = str[i]; - str[i] = str[cou - i - 1]; - str[cou - 1] = temp; + size_t start = 0; + size_t end = cou - 1; + while (start < end) { + char temp = str[start]; + str[start] = str[end]; + str[end] = temp; + ++start; + --end; } str[cou] = '\0'; diff --git a/makefile b/makefile index d8535bc..8717518 100644 --- a/makefile +++ b/makefile @@ -34,10 +34,11 @@ limine: build-all -no-emul-boot -boot-load-size 4 -boot-info-table \ --efi-boot boot/limine/limine-uefi-cd.bin \ -efi-boot-part --efi-boot-image --protective-msdos-label \ - isodir -o emuos.iso + isodir -o $(OS_NAME).iso + limine bios-install $(OS_NAME).iso qemu: limine - $(QEMU) -no-shutdown -no-reboot --serial stdio -s -m 512 -hda $(OS_NAME).iso + $(QEMU) -no-shutdown -no-reboot --serial stdio -s -m 1024 -hda $(OS_NAME).iso install: install-headers install-libraries -- cgit v1.2.3-70-g09d2