Refactor: Modular kernel structure & WIP keyboard driver

- Reorganized src/inc into arch, drivers, core, shell
- Updated CMakeLists.txt for multi-arch support
- Added basic scancode-to-ASCII mapping (US QWERTY)
- Initial KSH (Kernel Shell) skeleton
- WIP: Keyboard modifier handling
This commit is contained in:
Karina
2025-12-27 22:10:24 +04:00
parent e644bc5577
commit 794dae7084
37 changed files with 230 additions and 130 deletions
+33 -37
View File
@@ -5,42 +5,34 @@ set(CMAKE_C_STANDARD 23)
set(CMAKE_C_STANDARD_REQUIRED ON) set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS OFF) set(CMAKE_C_EXTENSIONS OFF)
add_library(shitgui STATIC src/modules/shitgui.c) if (NOT DEFINED ARCH)
target_include_directories(shitgui PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include") set(ARCH "x86_64")
endif()
add_executable(kernel message(STATUS "TermOS Kernel: Building for architecture '${ARCH}'")
src/entry.asm
src/asm/interrupts.asm
src/kmain.c file(GLOB_RECURSE KERNEL_SOURCES
src/modules/pic.c "src/kmain.c"
src/modules/gdt.c
src/modules/idt.c "src/arch/${ARCH}/*.c"
src/modules/interrupts.c "src/arch/${ARCH}/*.asm"
src/modules/pmm.c
src/modules/vmm.c "src/core/*.c"
src/modules/memory.c "src/drivers/*.c"
src/modules/serial.c "src/mm/*.c"
src/modules/console.c "src/shell/*.c"
src/modules/rand.c
src/modules/panic.c "data/*.c"
src/modules/keyboard.c
src/modules/kfetch.c
data/font8x16.c
) )
add_executable(kernel ${KERNEL_SOURCES})
target_include_directories(kernel PRIVATE target_include_directories(kernel PRIVATE
"${CMAKE_CURRENT_SOURCE_DIR}/../common" "${CMAKE_CURRENT_SOURCE_DIR}/../common"
"${CMAKE_CURRENT_SOURCE_DIR}/include" "${CMAKE_CURRENT_SOURCE_DIR}/inc"
) "${CMAKE_CURRENT_SOURCE_DIR}/inc/arch/${ARCH}"
"${CMAKE_CURRENT_SOURCE_DIR}/data"
target_link_libraries(kernel PRIVATE shitgui)
target_link_options(kernel PRIVATE
-nostdlib
-static
-no-pie
-T "${CMAKE_CURRENT_SOURCE_DIR}/linker.ld"
-Wl,-Map,"${CMAKE_BINARY_DIR}/kernel.map"
) )
target_compile_options(kernel PRIVATE target_compile_options(kernel PRIVATE
@@ -54,6 +46,15 @@ target_compile_options(kernel PRIVATE
> >
) )
target_link_options(kernel PRIVATE
-nostdlib
-static
-no-pie
-T "${CMAKE_CURRENT_SOURCE_DIR}/linker.ld"
-Wl,-Map,"${CMAKE_BINARY_DIR}/kernel.map"
-z max-page-size=0x1000
)
set_target_properties(kernel PROPERTIES set_target_properties(kernel PROPERTIES
OUTPUT_NAME "kernel.elf" OUTPUT_NAME "kernel.elf"
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin" RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
@@ -61,10 +62,5 @@ set_target_properties(kernel PROPERTIES
add_custom_command(TARGET kernel POST_BUILD add_custom_command(TARGET kernel POST_BUILD
COMMAND objcopy -O binary $<TARGET_FILE:kernel> $<TARGET_FILE_DIR:kernel>/kernel.bin COMMAND objcopy -O binary $<TARGET_FILE:kernel> $<TARGET_FILE_DIR:kernel>/kernel.bin
COMMENT "Stripping ELF headers -> kernel.bin" COMMENT "Generating raw binary kernel.bin..."
)
file(GLOB_RECURSE SOURCES
"src/*.c"
"data/*.c"
) )
+1 -1
View File
@@ -3,7 +3,7 @@
// Copyright (c) 2025 0xKarinyash // Copyright (c) 2025 0xKarinyash
#pragma once #pragma once
#include "shitgui.h" #include <drivers/shitgui.h>
#define LOGO_TRANSPARENCY_KEY 0xFF00FF #define LOGO_TRANSPARENCY_KEY 0xFF00FF
@@ -5,7 +5,7 @@
#ifndef GDT_H #ifndef GDT_H
#define GDT_H #define GDT_H
#include "types.h" #include <types.h>
typedef struct { typedef struct {
u16 limit_low; u16 limit_low;
@@ -4,7 +4,7 @@
#ifndef IDT_H #ifndef IDT_H
#define IDT_H #define IDT_H
#pragma once #pragma once
#include "types.h" #include <types.h>
typedef struct { typedef struct {
u16 offset_low; u16 offset_low;
@@ -4,7 +4,7 @@
#ifndef PIC_H #ifndef PIC_H
#define PIC_H #define PIC_H
#include "types.h" #include <types.h>
u16 pic_remap(); u16 pic_remap();
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: GPL-3.0-or-later // SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (c) 2025 0xKarinyash // Copyright (c) 2025 0xKarinyash
#include "types.h" #include <types.h>
#define MAX(a, b) ({ \ #define MAX(a, b) ({ \
auto _a = (a); \ auto _a = (a); \
@@ -4,7 +4,7 @@
#pragma once #pragma once
#ifndef PANIC_H #ifndef PANIC_H
#define PANIC_H #define PANIC_H
#include "types.h" #include <types.h>
__attribute__((noreturn)) void panic_exception(Registers *regs); __attribute__((noreturn)) void panic_exception(Registers *regs);
__attribute__((noreturn)) void panic(const char* msg); __attribute__((noreturn)) void panic(const char* msg);
@@ -3,7 +3,8 @@
#ifndef RAND_H #ifndef RAND_H
#define RAND_H #define RAND_H
#include "types.h"
#include <types.h>
u64 shitrand(); u64 shitrand();
@@ -4,7 +4,7 @@
#ifndef CONSOLE_H #ifndef CONSOLE_H
#define CONSOLE_H #define CONSOLE_H
#include "shitgui.h" #include <drivers/shitgui.h>
void console_init(SG_Context *ctx); void console_init(SG_Context *ctx);
void console_clear(u32 color); void console_clear(u32 color);
@@ -1,7 +1,7 @@
#ifndef FONT_H #ifndef FONT_H
#define FONT_H #define FONT_H
#include "types.h" #include <types.h>
extern u8 font8x16[128][16]; extern u8 font8x16[128][16];
@@ -1,7 +1,7 @@
#ifndef KEYBOARD_H #ifndef KEYBOARD_H
#define KEYBOARD_H #define KEYBOARD_H
#include "types.h" #include <types.h>
void kb_handler(Registers *regs); void kb_handler(Registers *regs);
@@ -4,7 +4,7 @@
#ifndef SHITGUI_H #ifndef SHITGUI_H
#define SHITGUI_H #define SHITGUI_H
#include "types.h" #include <types.h>
typedef struct { typedef struct {
volatile u32 *fb; volatile u32 *fb;
@@ -4,7 +4,7 @@
#ifndef MEMORY_H #ifndef MEMORY_H
#define MEMORY_H #define MEMORY_H
#include "types.h" #include <types.h>
void *memset(void *ptr, int value, usize num); void *memset(void *ptr, int value, usize num);
+1 -1
View File
@@ -4,7 +4,7 @@
#ifndef PMM_H #ifndef PMM_H
#define PMM_H #define PMM_H
#include "../common/bootinfo.h" #include "bootinfo.h"
#define PAGE_SIZE 4096 #define PAGE_SIZE 4096
#define BLOCKS_PER_BYTE 8 #define BLOCKS_PER_BYTE 8
+2 -2
View File
@@ -4,8 +4,8 @@
#ifndef VMM_H #ifndef VMM_H
#define VMM_H #define VMM_H
#include "../common/bootinfo.h" #include "bootinfo.h"
#include "types.h" #include <types.h>
#define PTE_PRESENT (1ULL << 0) // 0 -- not present: page fault when trying to access; 1 -- present, can RW #define PTE_PRESENT (1ULL << 0) // 0 -- not present: page fault when trying to access; 1 -- present, can RW
#define PTE_RW (1ULL << 1) // 0 -- RO: page fault trying to write; 1 -- RW #define PTE_RW (1ULL << 1) // 0 -- RO: page fault trying to write; 1 -- RW
@@ -51,4 +51,11 @@ typedef enum {
EfiMaxMemoryType EfiMaxMemoryType
} EFI_MEMORY_TYPE; } EFI_MEMORY_TYPE;
#ifndef __cplusplus
#define bool _Bool
#define true 1
#define false 0
#endif
#endif #endif
@@ -3,7 +3,7 @@
// just fucking kill me already // just fucking kill me already
#include "gdt.h" #include <gdt.h>
GDTDescriptor gdt[5]; GDTDescriptor gdt[5];
GDTPtr gdt_ptr; GDTPtr gdt_ptr;
@@ -1,8 +1,8 @@
// SPDX-License-Identifier: GPL-3.0-or-later // SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (c) 2025 0xKarinyash // Copyright (c) 2025 0xKarinyash
#include "idt.h" #include <idt.h>
#include "types.h" #include <types.h>
IDTEntry idt[256]; IDTEntry idt[256];
IDTPtr idt_ptr; IDTPtr idt_ptr;
@@ -1,10 +1,10 @@
// SPDX-License-Identifier: GPL-3.0-or-later // SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (c) 2025 0xKarinyash // Copyright (c) 2025 0xKarinyash
#include "io.h" #include <io.h>
#include "panic.h" #include <core/panic.h>
#include "keyboard.h" #include <drivers/keyboard.h>
#include "types.h" #include <types.h>
void isr_handler_c(Registers *regs) { void isr_handler_c(Registers *regs) {
@@ -1,14 +1,10 @@
// SPDX-License-Identifier: GPL-3.0-or-later // SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (c) 2025 0xKarinyash // Copyright (c) 2025 0xKarinyash
#include "pic.h" #include <pic.h>
#include "io.h" #include <io.h>
#include "types.h" #include <types.h>
#define MASTER_COMMAND 0x20
#define MASTER_DATA 0x21
#define SLAVE_COMMAND 0xA0
#define SLAVE_DATA 0xA1
u16 pic_remap() { u16 pic_remap() {
u8 curr_master = inb(MASTER_DATA); u8 curr_master = inb(MASTER_DATA);
@@ -30,5 +26,9 @@ u16 pic_remap() {
outb_wait(MASTER_DATA, 0x01); outb_wait(MASTER_DATA, 0x01);
outb_wait(SLAVE_DATA, 0x01); outb_wait(SLAVE_DATA, 0x01);
outb_wait(MASTER_DATA, 0xFD); // 1111 1101
// 0 -- means 'on'
outb_wait(SLAVE_DATA, 0xFF); // Everything ON
return ((u16) curr_master << 8) | curr_slave; return ((u16) curr_master << 8) | curr_slave;
} }
@@ -1,11 +1,13 @@
// SPDX-License-Identifier: GPL-3.0-or-later // SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (c) 2025 0xKarinyash // Copyright (c) 2025 0xKarinyash
#include "panic.h" #include <core/panic.h>
#include "console.h" #include <core/rand.h>
#include "rand.h"
#include "types.h" #include <drivers/shitgui.h>
#include "shitgui.h" #include <drivers/console.h>
#include <types.h>
const char* fun_messages[] = { const char* fun_messages[] = {
"Execution finished abnormally with code: 0x_x", "Execution finished abnormally with code: 0x_x",
@@ -1,8 +1,8 @@
// SPDX-License-Identifier: GPL-3.0-or-later // SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (c) 2025 0xKarinyash // Copyright (c) 2025 0xKarinyash
#include "rand.h" #include <core/rand.h>
#include "types.h" #include <types.h>
// really shiity rand lol. // really shiity rand lol.
// not shitty.. basic. // not shitty.. basic.
@@ -1,12 +1,14 @@
// SPDX-License-Identifier: GPL-3.0-or-later // SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (c) 2025 0xKarinyash // Copyright (c) 2025 0xKarinyash
#include "console.h" #include <drivers/console.h>
#include "serial.h" #include <drivers/serial.h>
#include "font.h" #include <drivers/font.h>
#include "shitgui.h" #include <drivers/shitgui.h>
#include "math.h"
#include "types.h" #include <core/math.h>
#include <types.h>
#include <stdarg.h> #include <stdarg.h>
#define COLOR_RED 0xFF5555 #define COLOR_RED 0xFF5555
+109
View File
@@ -0,0 +1,109 @@
#include <drivers/keyboard.h>
#include <drivers/console.h>
#include <io.h>
#include <types.h>
unsigned char keyboard_map[128] = {
0, 27, '1', '2', '3', '4', '5', '6', '7', '8', /* 0x00 - 0x09 */
'9', '0', '-', '=', '\b', /* Backspace */
'\t', /* Tab */
'q', 'w', 'e', 'r', /* 0x10 - 0x13 */
't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n', /* Enter key */
0, /* 0x1D - Control */
'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', /* 0x1E - 0x27 */
'\'', '`', 0, /* Left shift */
'\\', 'z', 'x', 'c', 'v', 'b', 'n', /* 0x2C - 0x32 */
'm', ',', '.', '/', 0, /* Right shift */
'*',
0, /* Alt */
' ', /* Space bar */
0, /* Caps lock */
0, /* 0x3B - F1 key ... > */
0, 0, 0, 0, 0, 0, 0, 0,
0, /* < ... F10 */
0, /* 0x45 - Num lock*/
0, /* Scroll Lock */
0, /* Home key */
0, /* Up Arrow */
0, /* Page Up */
'-',
0, /* Left Arrow */
0,
0, /* Right Arrow */
'+',
0, /* 0x4F - End key*/
0, /* Down Arrow */
0, /* Page Down */
0, /* Insert Key */
0, /* Delete Key */
0, 0, 0,
0, /* F11 Key */
0, /* F12 Key */
0, /* All other keys are undefined */
};
unsigned char keyboard_map_shifted[128] = {
0, 27, '!', '@', '#', '$', '%', '^', '&', '*', /* 0x00 - 0x09 */
'(', ')', '_', '+', '\b', /* Backspace */
'\t', /* Tab */
'Q', 'W', 'E', 'R', /* 0x10 - 0x13 */
'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', '\n', /* Enter key */
0, /* 0x1D - Control */
'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', /* 0x1E - 0x27 */
'"', '~', 0, /* Left shift (0x2A) */
'|', 'Z', 'X', 'C', 'V', 'B', 'N', /* 0x2C - 0x32 */
'M', '<', '>', '?', 0, /* Right shift (0x36) */
'*',
0, /* Alt */
' ', /* Space bar */
0, /* Caps lock */
0, /* 0x3B - F1 key ... > */
0, 0, 0, 0, 0, 0, 0, 0,
0, /* < ... F10 */
0, /* 0x45 - Num lock*/
0, /* Scroll Lock */
0, /* Home key */
0, /* Up Arrow */
0, /* Page Up */
'-',
0, /* Left Arrow */
0,
0, /* Right Arrow */
'+',
0, /* 0x4F - End key*/
0, /* Down Arrow */
0, /* Page Down */
0, /* Insert Key */
0, /* Delete Key */
0, 0, 0,
0, /* F11 Key */
0, /* F12 Key */
0, /* All other keys */
};
bool shift_pressed = false;
void kb_handler(Registers *regs) {
u8 scancode = inb(0x60);
// make code 0x00 - 0x7F; break code = make code + 0x80
if (scancode < 0x80) {
unsigned char* arr = shift_pressed ? keyboard_map_shifted : keyboard_map;
char ascii = arr[scancode];
if (ascii) {
kprintf("%c", ascii);
} else {
switch (scancode) {
case 0x2A: shift_pressed = true; break;
default: break;
}
}
} else {
switch (scancode) {
case 0xAA: shift_pressed = false; break;
default: break;
}
}
outb(MASTER_COMMAND, 0x20);
// kprintf("Key: %x; shift is %spressed\n", scancode, shift_pressed ? "" : "not ");
}
@@ -1,8 +1,8 @@
// SPDX-License-Identifier: GPL-3.0-or-later // SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (c) 2025 0xKarinyash // Copyright (c) 2025 0xKarinyash
#include "serial.h" #include <drivers/serial.h>
#include "io.h" #include <io.h>
#define PORT 0x3F8 // com1 #define PORT 0x3F8 // com1
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: GPL-3.0-or-later // SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (c) 2025 0xKarinyash // Copyright (c) 2025 0xKarinyash
#include "shitgui.h" #include <drivers/shitgui.h>
#define SHITGUI_TRANSPARENCY_KEY 0xFF00FF #define SHITGUI_TRANSPARENCY_KEY 0xFF00FF
+13 -22
View File
@@ -1,22 +1,20 @@
// SPDX-License-Identifier: GPL-3.0-or-later // SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (c) 2025 0xKarinyash // Copyright (c) 2025 0xKarinyash
#include "../../common/bootinfo.h" #include "bootinfo.h"
#include "types.h"
#include "shitgui.h"
#include "serial.h"
#include "console.h"
#include "panic.h" // IWYU pragma: keep
#include "gdt.h"
#include "idt.h"
#include "pic.h"
#include "pmm.h"
#include "vmm.h"
#include "../data/logo.h" #include "../data/logo.h"
#include "vmm.h"
#include <types.h>
#include <drivers/shitgui.h>
#include <drivers/serial.h>
#include <drivers/console.h>
#include <gdt.h>
#include <idt.h>
#include <pic.h>
#include <mm/pmm.h>
#include <mm/vmm.h>
extern u64 _kernel_end; extern u64 _kernel_end;
extern u8* bitmap; extern u8* bitmap;
@@ -54,15 +52,8 @@ void kmain(Bootinfo* info) {
pmm_init(info->mem); pmm_init(info->mem);
kprintf("MemoryMap located at ^g%x^0 (^r%X^0); \
\nMemory map size is ^g%x^0\
\nKernel ends at ^g%x^0\
\nBITMAP located at ^g%x^0 (^r%x^0)", (u64)info->mem.map, (u64)info->mem.map,(u64)info->mem.map_size, &_kernel_end, bitmap, bitmap_size_g);
vmm_init(info); vmm_init(info);
kprintf("\nIM ALIVE :D");
__asm__ volatile ("sti"); __asm__ volatile ("sti");
while (1) { __asm__("hlt"); } while (1) { __asm__("hlt"); }
@@ -1,8 +1,8 @@
// SPDX-License-Identifier: GPL-3.0-or-later // SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (c) 2025 0xKarinyash // Copyright (c) 2025 0xKarinyash
#include "memory.h" #include <mm/memory.h>
#include "types.h" #include <types.h>
void *memset(void *ptr, int value, usize num) { void *memset(void *ptr, int value, usize num) {
u8 *p = (u8 *)ptr; u8 *p = (u8 *)ptr;
@@ -1,12 +1,14 @@
// SPDX-License-Identifier: GPL-3.0-or-later // SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (c) 2025 0xKarinyash // Copyright (c) 2025 0xKarinyash
#include "pmm.h" #include <mm/pmm.h>
#include <core/panic.h>
#include <mm/memory.h>
#include <core/math.h>
#include <types.h>
#include "bootinfo.h" #include "bootinfo.h"
#include "panic.h"
#include "types.h"
#include "memory.h" // IWYU pragma: keep // clangd is stupid af
#include "math.h"
extern u64 _kernel_start; extern u64 _kernel_start;
extern u64 _kernel_end; extern u64 _kernel_end;
@@ -1,14 +1,14 @@
// SPDX-License-Identifier: GPL-3.0-or-later // SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (c) 2025 0xKarinyash // Copyright (c) 2025 0xKarinyash
#include <mm/vmm.h>
#include <mm/pmm.h>
#include <mm/memory.h>
#include "../common/bootinfo.h" #include <gdt.h>
#include <idt.h>
#include "vmm.h" #include <types.h>
#include "gdt.h" #include "bootinfo.h"
#include "idt.h"
#include "pmm.h"
#include "memory.h" // IWYU pragma: keep // shut the fuck up I DONT NEED <string.h> CLANGD PLEASE
#include "types.h"
u64* pml4_kernel = nullptr; u64* pml4_kernel = nullptr;
-10
View File
@@ -1,10 +0,0 @@
#include "keyboard.h"
#include "console.h"
#include "io.h"
#include "types.h"
void kb_handler(Registers *regs) {
u8 scancode = inb(0x60);
outb(MASTER_COMMAND, 0x20);
kprintf("Key: %x\n", scancode);
}
@@ -4,7 +4,7 @@
// yes. *fetch in kernel // yes. *fetch in kernel
// WHY FUCKING NOT?? its my OS, my rules // WHY FUCKING NOT?? its my OS, my rules
#include "console.h" #include <drivers/console.h>
const char* ascii_logo[] = { const char* ascii_logo[] = {
" /\\___/\\ ", " /\\___/\\ ",