26#include <limine/limine.h>
53static bool out_of_memory_test_1 =
false;
54static bool out_of_memory_test_2 =
false;
55static bool double_free_test =
false;
60__attribute__((used, section(
".limine_requests")))
61static volatile uint64_t limine_base_revision[] = LIMINE_BASE_REVISION(6);
63__attribute__((used, section(
".limine_requests_start")))
64static volatile uint64_t limine_requests_start_marker[] = LIMINE_REQUESTS_START_MARKER;
68__attribute__((used, section(
".limine_requests")))
70 .id = LIMINE_BOOTLOADER_INFO_REQUEST_ID,
77__attribute__((used, section(
".limine_requests")))
79 .id = LIMINE_FIRMWARE_TYPE_REQUEST_ID,
86__attribute__((used, section(
".limine_requests")))
88 .id = LIMINE_HHDM_REQUEST_ID,
95__attribute__((used, section(
".limine_requests")))
97 .id = LIMINE_MEMMAP_REQUEST_ID,
104__attribute__((used, section(
".limine_requests")))
106 .id = LIMINE_EXECUTABLE_ADDRESS_REQUEST_ID,
113__attribute__((used, section(
".limine_requests")))
115 .id = LIMINE_FRAMEBUFFER_REQUEST_ID,
123__attribute__((used, section(
".limine_requests")))
125 .id = LIMINE_EXECUTABLE_CMDLINE_REQUEST_ID,
131__attribute__((used, section(
".limine_requests_end")))
132static volatile uint64_t limine_requests_end_marker[] = LIMINE_REQUESTS_END_MARKER;
140 if (!LIMINE_BASE_REVISION_SUPPORTED(limine_base_revision)) {
156 LOG_INFO(
"LumaOS Initialization Complete!\nPress any key to continue...");
178 uint64_t previous_second = 0;
180 previous_second = pit_ticks / pit_frequency;
183 kprintf(
"Uptime %lds\r", previous_second);
202 else if ((ev.
ascii >=
'A' && ev.
ascii <=
'Z'))
208 if (ev.
ascii ==
'\b') {
237 previous_second = pit_ticks / pit_frequency;
241 kprintf(
"Uptime %lds\r", previous_second);
255 if (pit_ticks / pit_frequency != previous_second)
257 previous_second = pit_ticks / pit_frequency;
260 kprintf(
"Uptime %lds\r", previous_second);
276 if (x >= fb->width || y >= fb->height) {
280 uint32_t *pixel_addr = (uint32_t *)(
281 (uint8_t *)fb->address + (y * fb->pitch) + (x * 4)
285 ((uint32_t)red << fb->red_mask_shift) |
286 ((uint32_t)green << fb->green_mask_shift) |
287 ((uint32_t)blue << fb->blue_mask_shift);
297 LOG_INFO(
"Limine Request Section.................");
305 LOG_DEBUG(
"Framebuffer width: %d\n", width);
306 LOG_DEBUG(
"Framebuffer height: %d\n", height);
307 LOG_DEBUG(
"Framebuffer pitch: %d\n", pitch);
308 LOG_DEBUG(
"Framebuffer bytes per pixel: %d\n\n", bpp);
311 if (firmware_type_request.response != NULL) {
312 uint64_t type = firmware_type_request.response->firmware_type;
315 if (type == LIMINE_FIRMWARE_TYPE_X86BIOS) {
316 type_text =
"x86 BIOS";
317 }
else if (type == LIMINE_FIRMWARE_TYPE_EFI64) {
318 type_text =
"64-bit UEFI";
319 }
else if (type == LIMINE_FIRMWARE_TYPE_EFI32) {
320 type_text =
"32-bit UEFI";
322 type_text =
"Unknown";
325 LOG_DEBUG(
"Firmware Type: %s\n\n", type_text);
328 if (hhdm_request.response != NULL) {
329 uint64_t offset = hhdm_request.response->offset;
333 LOG_DEBUG(
"HHDM Offset: %16lx\n\n", offset);
336 LOG_ERROR(
"HHDM REQUEST is NULL\n");
340 if (executable_address_request.response != NULL) {
341 uint64_t virt = executable_address_request.response->virtual_base;
342 uint64_t phys = executable_address_request.response->physical_base;
344 LOG_DEBUG(
"Executable Virtual Address Base: %16lx\n", virt);
345 LOG_DEBUG(
"Executable Physical Address Base: %16lx\n\n", phys);
348 if (memmap_request.response != NULL) {
349 uint64_t entry_count = memmap_request.response->entry_count;
351 LOG_DEBUG(
"MEMMAP:\n");
353 for (
unsigned int i = 0; i < entry_count; i++) {
354 uint64_t base = (*memmap_request.response->entries[i]).base;
355 uint64_t length = (*memmap_request.response->entries[i]).length;
356 uint64_t type = (*memmap_request.response->entries[i]).type;
358 LOG_DEBUG(
"Memmap entry %d:\n", i);
359 LOG_DEBUG(
" Base: %lx\n", base);
360 LOG_DEBUG(
" Length: %ld\n", length);
365 case LIMINE_MEMMAP_USABLE:
368 case LIMINE_MEMMAP_RESERVED:
369 type_str =
"Reserved";
371 case LIMINE_MEMMAP_ACPI_RECLAIMABLE:
372 type_str =
"ACPI Reclaimable";
374 case LIMINE_MEMMAP_ACPI_NVS:
375 type_str =
"ACPI NVS";
377 case LIMINE_MEMMAP_BAD_MEMORY:
378 type_str =
"Bad Memory";
380 case LIMINE_MEMMAP_BOOTLOADER_RECLAIMABLE:
381 type_str =
"Bootloader Reclaimable";
383 case LIMINE_MEMMAP_EXECUTABLE_AND_MODULES:
384 type_str =
"Executable & Modules";
386 case LIMINE_MEMMAP_FRAMEBUFFER:
387 type_str =
"Framebuffer";
389 case LIMINE_MEMMAP_RESERVED_MAPPED:
390 type_str =
"Reserved (Mapped)";
393 type_str =
"Unknown";
397 LOG_DEBUG(
" Type: %s\n", type_str);
404 LOG_ERROR(
"MEMMAP REQUEST is NULL\n");
419 bool serial_success = serial_init_return == 0;
422 if (framebuffer_request.response != NULL) {
427 if (
get_debug_on()) LOG_ERROR(
"Framebuffer is NULL!\n");
434 if (
get_debug_on()) LOG_ERROR(
"Framebuffer is NULL!\n");
439 if (*_binary_font_psf_start == 0x36 &&
440 *(_binary_font_psf_start + 1) == 0x04) {
441 font_type = FONT_PSF1;
443 }
else if (*_binary_font_psf_start == 0x72 &&
444 *(_binary_font_psf_start + 1) == 0xB5 &&
445 *(_binary_font_psf_start + 2) == 0x4A &&
446 *(_binary_font_psf_start + 3) == 0x86) {
447 font_type = FONT_PSF2;
450 LOG_ERROR(
"Font magic number does not match PSF1 or PSF2!\n");
455 if (cmdline_request.response != NULL) {
456 const char *cmdline = cmdline_request.response->cmdline;
464 LOGGING_LEVEL = LOG_LEVEL_ERROR;
467 LOGGING_LEVEL = LOG_LEVEL_WARN;
470 LOGGING_LEVEL = LOG_LEVEL_INFO;
474 LOGGING_LEVEL = LOG_LEVEL_DEBUG;
483 double_free_test =
parse_bool(
"mem.double_free_test", cmdline);
484 out_of_memory_test_1 =
parse_bool(
"mem.oom_test_1", cmdline);
485 out_of_memory_test_2 =
parse_bool(
"mem.oom_test_2", cmdline);
489 LOG_INFO(
"Limine Request Section.................");
494 LOG_INFO(
"Initializing Serial Output.............");
501 LOG_INFO(
"Initializing Console...................\033[92mPASS\n");
503 if (bootloader_info_request.response != NULL) {
504 char *name = bootloader_info_request.response->name;
505 char *version = bootloader_info_request.response->version;
507 LOG_INFO(
"BOOTLOADER INFO:\n");
508 LOG_INFO(
"Name: %s\n", name);
509 LOG_INFO(
"Version: %s\n", version);
520 LOG_INFO(
"Initializing IRQ and PIC Handlers......");
524 uint64_t apic_base = rdmsr(0x1B);
526 if (apic_base & (1ULL << 10)) {
527 apic_base &= ~(1ULL << 10);
528 wrmsr(0x1B, apic_base);
531 apic_base &= ~(1ULL << 11);
532 wrmsr(0x1B, apic_base);
542 asm volatile (
"sti");
552 LOG_INFO(
"Initializing Physical Memory Manager...");
558 vmm_add_region(0xFFFFC00000000000, 0xFFFFC00000000000 + 0x10000, PTE_WRITABLE);
565 LOG_DEBUG(
"PMM Tests\n");
567 if (out_of_memory_test_1)
569 LOG_DEBUG(
"Out of Memory Test...\n");
575 LOG_DEBUG(
"addr: %016lx free frames: %lu\r", addr, free_frames);
583 LOG_DEBUG(
"Out of memory test successful! Reboot with flag \"mem.oom_test_1\" set to false to continue boot regularly.\n");
587 LOG_ERROR(
"Out of memory test failed! Free frames != 0. Reboot with flag \"mem.oom_test_1\" set to false to continue boot regularly.\n");
589 for (;;)
asm volatile(
"hlt");
596 LOG_DEBUG(
"Test 1 & 2: Count Restore & Uniqueness & Alignment\n");
601 uint64_t frames[1000];
602 for (
int i = 0; i < 1000; i++)
608 for (
int i = 0; i < 1000; i++)
611 if (frames[i] % FRAME_SIZE != 0)
613 LOG_ERROR(
"Frame %d is not aligned!\n", i);
614 LOG_ERROR(
"Frame Alignment Test Failed!\n");
615 for (;;)
asm volatile(
"hlt");
620 if (frames[i] < 0x100000)
622 LOG_ERROR(
"Frame %d is not above 1MiB!\n", i);
623 LOG_ERROR(
"Frame Location Test Failed!\n");
624 for (;;)
asm volatile(
"hlt");
629 for (
int j = 0; j < 1000; j++)
631 if (j == i)
continue;
632 ASSERT(frames[i] != frames[j]);
633 if (frames[i] == frames[j])
635 LOG_ERROR(
"Frame %d is not unique! (matches frame %d)\n", i, j);
636 LOG_ERROR(
"Frame Uniqueness Test Failed!\n");
637 for (;;)
asm volatile(
"hlt");
643 LOG_DEBUG(
"All frames are aligned, above 1MiB, and unique\n");
646 for (
int i = 0; i < 1000; i++)
653 ASSERT(free_frames_before == free_frames_after);
654 LOG_DEBUG(
"Free Frames Before: %ld\n"
655 " Free Frames After: %ld\n", free_frames_before, free_frames_after);
658 LOG_DEBUG(
"Test 3: Memory Usage\n");
662 LOG_DEBUG(
"Writing 0xABCDEF0123456789 to the physical address 0x%lx\n", phys_address);
664 *(uint64_t*)virt_address = 0xABCDEF0123456789;
666 ASSERT(*(uint64_t*)virt_address == 0xABCDEF0123456789);
668 LOG_DEBUG(
"Reading back the value from the physical address 0x%lx\n", phys_address);
669 LOG_DEBUG(
"Value read: 0x%lx\n", *(uint64_t*)virt_address);
671 LOG_DEBUG(
"Zeroing Frame...\n");
673 memset((
void*)virt_address, 0, FRAME_SIZE);
677 if (double_free_test)
679 LOG_DEBUG(
"Double Freeing Frame Test...\n");
680 LOG_DEBUG(
"Test is SUCCESSFUL IF THE KERNEL PANICS\n");
681 LOG_DEBUG(
"After test, reboot with flag \"mem.double_free_test\" set to false to continue boot regularly.\n");
683 LOG_ERROR(
"Double free test failed!\n");
684 for (;;)
asm volatile(
"hlt");
688 if (out_of_memory_test_2)
690 LOG_DEBUG(
"Out of Memory Test...\n");
696 LOG_DEBUG(
"addr: %016lx free frames: %lu\r", addr, free_frames);
704 LOG_DEBUG(
"Out of memory test successful! Reboot with flag \"mem.oom_test_2\" set to false to continue boot regularly.\n");
708 LOG_ERROR(
"Out of memory test failed! Free frames != 0. Reboot with flag \"mem.oom_test_2\" set to false to continue boot regularly.\n");
710 for (;;)
asm volatile(
"hlt");
723 LOG_DEBUG(
"Page Translator Tests\n");
730 uint64_t test4 =
translate(0x400000 | (1ULL << 48));
732 LOG_DEBUG(
"kprintf: Virtual ADDR=%16lx Physical ADDR=%16lx\n", (uint64_t)
kprintf, test1);
733 LOG_DEBUG(
"Test 2: Virtual ADDR=%16lx Physical ADDR=%16lx\n", 0x500000 +
get_hddm_offset(), test2);
734 LOG_DEBUG(
"Test 3: Virtual ADDR=%16lx Physical ADDR=%16lx\n", 0x400000, test3);
735 LOG_DEBUG(
"Test 4: Virtual ADDR=%16lx Physical ADDR=%16lx\n", 0x400000 | (1ULL << 48), test4);
744 LOG_DEBUG(
"RUNNING VIRTUAL MEMORY MANAGER TESTS\n");
747 LOG_DEBUG(
"Original free_frame count: %lu\n",
get_pmm_state().free_frames);
754 LOG_DEBUG(
"Allocated new frame\n");
757 LOG_DEBUG(
"New free_frame count: %lu\n",
get_pmm_state().free_frames);
762 if (virt == UINT64_MAX)
764 LOG_ERROR(
"Failed to map frame at VMM_TEST_BASE\n");
765 goto post_vmm_test_1;
768 LOG_DEBUG(
"Mapped frame at VMM_TEST_BASE\n");
773 *(uint64_t*)virt = 0x123456789ABCDEF0;
774 LOG_DEBUG(
"Wrote 0x123456789ABCDEF0 to VMM_TEST_BASE\n");
775 LOG_DEBUG(
"Read back: 0x%lx\n", *(uint64_t*)virt);
781 LOG_DEBUG(
"Unmapping Frame at VMM_TEST_BASE\n");
790 LOG_DEBUG(
"Freeing Frame\n");
794 LOG_DEBUG(
"Restored free_frame count: %lu\n",
get_pmm_state().free_frames);
796 LOG_DEBUG(
"Testing mapping inside HHDM (should fail)\n");
798 LOG_DEBUG(
"Allocating frame\n");
803 LOG_DEBUG(
"Address allocated: 0x%lx\n", addr);
804 LOG_DEBUG(
"Mapping frame\n");
808 if (map == UINT64_MAX)
809 LOG_DEBUG(
"Success! VMM Mapper did not map inside HHDM\n");
811 LOG_DEBUG(
"Failure! VMM Mapper did mapped inside HHDM\n");
818 LOG_DEBUG(
"DEMAND PAGING TESTS\n");
820 LOG_DEBUG(
"Frame count: %lu\n",
get_pmm_state().free_frames);
822 uint64_t *page0 = (uint64_t*)0xFFFFC00000000000;
823 uint64_t *page3 = (uint64_t*)0xFFFFC00000003000;
824 uint64_t *page7 = (uint64_t*)0xFFFFC00000007000;
826 LOG_DEBUG(
"Writing to page 0, 3, and 7\n");
827 *page0 = 0x123456789ABCDEF0;
828 *page3 = 0x123456789ABCDEF1;
829 *page7 = 0x123456789ABCDEF2;
831 LOG_DEBUG(
"Read back from page 0: 0x%lx\n", *page0);
832 LOG_DEBUG(
"Read back from page 3: 0x%lx\n", *page3);
833 LOG_DEBUG(
"Read back from page 7: 0x%lx\n", *page7);
836 LOG_DEBUG(
"Frame count: %lu\n",
get_pmm_state().free_frames);
838 LOG_DEBUG(
"VMM Tests completed!\n");
849 LOG_INFO(
"Initializing GDT & IST.................");
856 asm volatile (
"sgdt %0" :
"=m"(current));
858 uint16_t cs = 0, ss = 0, ds = 0;
859 asm volatile (
"movw %%cs, %0\n\t"
862 "=r"(cs),
"=r"(ss),
"=r"(ds) : :
"memory");
864 LOG_DEBUG(
"GDT BASE: %lp GDT LIMIT: %u\n", current.base, current.limit);
865 LOG_DEBUG(
"CS: 0x%x SS: 0x%x DS: 0x%x\n\n", cs, ss, ds);
867 LOG_DEBUG(
"GDT HEXDUMP:\n");
869 hexdump((
void*)current.base, 56);
880 LOG_INFO(
"Kernel Self Tests......................");
882 uint64_t memcpy_test_ints[3] = {12345,67890,13579};
883 uint32_t memcpy_test_ints_32[6] = {11111,22222,33333,44444,55555,66666};
885 uint64_t memmove_test_ints[3] = {12345,67890,13579};
886 uint32_t memmove_test_ints_32[6] = {11111,22222,33333,44444,55555,66666};
888 bool memcpy_tests =
test_memcpy(memcpy_test_ints[0], memcpy_test_ints[1]) &&
889 test_memcpy(memcpy_test_ints[1], memcpy_test_ints[0]) &&
890 test_memcpy(memcpy_test_ints_32[0], memcpy_test_ints_32[1]);
892 bool memmove_tests =
test_memmove(memmove_test_ints[0], memmove_test_ints[1]) &&
893 test_memmove(memmove_test_ints[1], memmove_test_ints[0]) &&
894 test_memmove(memmove_test_ints_32[0], memmove_test_ints_32[1]);
896 bool memset_tests =
test_memset(12399027537, 12345) &&
900 bool memcmp_tests =
test_memcmp(1234567890, 2345678901, 1) &&
909 if (!(memcpy_tests && memmove_tests && memset_tests && memcmp_tests && strlen_tests && gdt_tests))
914 if (memcpy_tests)
kprintf(
" MEMCPY Test Failed\n");
915 if (memmove_tests)
kprintf(
" MEMMOVE Test Failed\n");
916 if (memset_tests)
kprintf(
" MEMSET Test Failed\n");
917 if (memcmp_tests)
kprintf(
" MEMCMP Test Failed\n");
918 if (strlen_tests)
kprintf(
" STRLEN Test Failed\n");
919 if (gdt_tests)
kprintf(
" GDT Test Failed\n");
927 kprintf(
"KPRINTF TESTS: VALUE (EXPECTED VALUE)\n");
932 kprintf(
"FG: \033[30m#\033[31m#\033[32m#\033[33m#\033[34m#\033[35m#\033[36m#\033[37m#\033[0m\n");
933 kprintf(
"FG BRIGHT: \033[90m#\033[91m#\033[92m#\033[93m#\033[94m#\033[95m#\033[96m#\033[97m#\033[0m\n");
934 kprintf(
"BG: \033[40m#\033[41m#\033[42m#\033[43m#\033[44m#\033[45m#\033[46m#\033[47m#\033[0m\n");
935 kprintf(
"BG BRIGHT: \033[100m#\033[101m#\033[102m#\033[103m#\033[104m#\033[105m#\033[106m#\033[107m#\033[0m\n");
938 kprintf(
"Dim: \033[2m###\033[0m\n");
939 kprintf(
"Reverse: \033[7m###\033[0m\n");
940 kprintf(
"Hide: \033[8m###\033[0m\n");
void console_putc(char c)
Arranges the cursor and prints characters.
void scroll_up(void)
Scrolls the whole console up.
bool get_console_status(void)
Returns the status of the console (initialized or uninitialized).
void console_clear(void)
Clears the console (fills with bg color).
void console_init(void)
Initializes the console struct and fills all the properties.
struct console main_console
The main console info.
Console utilities and configuration.
void init_gdt(void)
Initializes the gdt and loads it.
Initializes and holds the GDT for the kernel.
Hexdump utility for displaying memory contents in hexadecimal.
void hexdump(const void *data, size_t size)
Prints each byte as a hex number from a buffer of bytes.
void init_idt(void)
Initializes the IDT and loads it.
Initializes and holds the IDT for the kernel.
IRQ Handler and utilities.
static uint64_t irq_save(void)
Saves the IRQ and disables interrupts (used when using a variable that is modified by an interrupt).
static void irq_restore(uint64_t flags)
Restores the IRQ and re-enables interrupts (used when using a variable that is modified by an interru...
int kbd_buf_pop(void)
Pops byte from the keyboard key buffer.
bool kbd_decode(uint8_t raw, struct key_event *ev)
Convert a keyboard byte into the correct char.
void keyboard_init(void)
Initialize the keyboard driver and register the handler with the IRQ handler.
int kbd_getchar(void)
Waits for a key to be pressed and returns the character.
Handler and utilities for the keyboard.
Tools for printing to the console and serial output.
void kprintf(const char *fmt,...) __attribute__((format(printf
Wrapper for a standard print.
Utilities for memory and string operations.
int strcmp(const char *s1, const char *s2)
Compares 2 strings.
void * memset(void *dest, int c, size_t n)
Fills n bytes of the destination with c.
Logging utilities for the kernel.
bool get_debug_on(void)
Gets the current value of LOG_ENABLE_DEBUG.
log_level
The logging levels for the log utility.
void set_debug_on(bool is_on)
Sets the debug mode.
static void limine_request_verification(void)
Output the limine request responses.
static void memory_init(void)
Initialize the memory systems (Physical Memory Manager, Virtual Memory Manager, etc) and test them.
static void interrupt_system_init(void)
Initialize the interrupt system.
static void gdt_idt_init(void)
Initialize and test the GDT & IST.
static void kernel_self_tests(void)
Run some kernel self tests.
static bool output_init(void)
Initialize the output systems (serial and framebuffer console) and parse any flags.
static void draw_pixel(struct limine_framebuffer *fb, uint64_t x, uint64_t y, uint8_t red, uint8_t green, uint8_t blue)
Draws a pixel to the screen.
static void hcf(void)
Halts the CPU, outputs the uptime, and outputs the user input to the console.
uint64_t translate(uint64_t virtual_address)
Converts/Translates a virtual address to its corresponding physical address.
Handles memory paging and translation.
Handler for kernel panics.
Parses flags and values from strings.
bool parse_bool(const char *flag, const char *str)
Checks if a flag is present in a string and if its set to true or false.
void pic_remap(uint8_t master_offset, uint8_t slave_offset)
Initializes the PIC using ICWs and mask all lines.
Handles the legacy PIC for IRQ interrupts.
void pit_init(uint16_t frequency)
Initialize the PIT timer.
Handles timer interrupts from the PIT (Programmable Interrupt Timer).
uint64_t pmm_alloc_frame(void)
Allocate a frame from the physical memory manager.
void pmm_free_frame(uint64_t physical_addr)
Frees a frame from the physical memory manager.
void pmm_init(const struct limine_memmap_response *memmap_response)
Initialize the physical memory manager.
struct pmm_state get_pmm_state(void)
Get the current pmm state.
int serial_init(void)
Initializes the serial port & output.
void serial_putc(char c)
Prints a character to serial port.
Serial utilities and configuration.
Console structure, holds all the information about the console.
char ascii
0 if the key has no printable character
bool pressed
true = key down, false = key up
uint64_t free_frames
how many frames are currently free
Tools for testing different parts of the kernel.
bool test_flag_byte(void)
Tests the flag byte utility for the gdt.
bool test_memset(uint64_t dest, int value)
Tests the memset utility.
bool test_memcpy(uint64_t dest, uint64_t b)
Tests the memcpy utility.
bool test_strlen(const char *str, uint8_t expected_value)
Tests the strlen utility.
bool test_memcmp(uint64_t dest, uint64_t b, int expected_value)
Tests the memcmp utility.
void test_kprintf_values(void)
Prints test data and the expected values for visual verification of kprintf.
bool test_access_byte(void)
Tests the access byte utility for the gdt.
bool test_memmove(uint64_t dest, uint64_t b)
Tests the memmove utility.
uint64_t get_hddm_offset(void)
Gets the offset for the HDDM.
void set_hddm_offset(uint64_t value)
Sets the offset for the HDDM.
void vmm_init(void)
Initialize the virtual memory manager and the kernel address space.
uint64_t vmm_unmap(uint64_t pml4_phys, uint64_t virtual_address)
Unmaps a virtual address in the given address space.
void vmm_add_region(uint64_t start, uint64_t end, uint64_t flags)
Add a region to the kernel address space.
uint64_t get_kernel_pml4_phys(void)
Returns the physical address of the top-level table.
uint64_t vmm_map(uint64_t pml4_phys, uint64_t virtual_address, uint64_t physical_address, uint64_t flags)
Maps a virtual address to a physical address in the given address space.