From 0670465278b5898fe79b23417f6c1781291f47ae Mon Sep 17 00:00:00 2001 From: Di Ferrari Date: Sun, 3 Aug 2025 00:00:00 +0000 Subject: [PATCH 01/50] [CONSOLE] fixed newline issue --- kernel/console/kconsole/kconsole.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/kernel/console/kconsole/kconsole.cpp b/kernel/console/kconsole/kconsole.cpp index 6cd1ff38..f6eb17df 100644 --- a/kernel/console/kconsole/kconsole.cpp +++ b/kernel/console/kconsole/kconsole.cpp @@ -63,9 +63,9 @@ void KernelConsole::newline(){ if (!check_ready()) return; uint32_t row_index; if (row_ring.pop(row_index)){ + row_ring.push(row_index); char* line = row_data + row_index * columns; for (uint32_t x = cursor_x; x < columns; x++) line[x] = 0; - row_ring.push(row_index); } cursor_x = 0; cursor_y++; @@ -77,11 +77,9 @@ void KernelConsole::newline(){ void KernelConsole::scroll(){ if (!check_ready()) return; - uint32_t row_index; - if (row_ring.pop(row_index)){ + if (uint32_t row_index = row_ring.peek()){ char* line = row_data + row_index * columns; for (uint32_t x = 0; x < columns; x++) line[x] = 0; - row_ring.push(row_index); } redraw(); } From f66bac7569c2c54770632e4f2be0e53d857197dd Mon Sep 17 00:00:00 2001 From: Di Ferrari Date: Sun, 3 Aug 2025 00:00:00 +0000 Subject: [PATCH 02/50] [AUDIO] made sound shorter --- kernel/audio/virtio_audio_pci.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/audio/virtio_audio_pci.cpp b/kernel/audio/virtio_audio_pci.cpp index 0cfabcd7..c25bd0c2 100644 --- a/kernel/audio/virtio_audio_pci.cpp +++ b/kernel/audio/virtio_audio_pci.cpp @@ -199,7 +199,7 @@ bool VirtioAudioDriver::config_streams(uint32_t streams){ kprintf("Playing from stream %i",stream); select_queue(&audio_dev, TRANSMIT_QUEUE); - for (uint16_t i = 0; i < 100; i++){ + for (uint16_t i = 0; i < 10; i++){ size_t total_size = sizeof(virtio_snd_pcm_status) + sizeof(virtio_snd_pcm_xfer) + TOTAL_BUF_SIZE; uintptr_t full_buffer = (uintptr_t)kalloc(audio_dev.memory_page, total_size, ALIGN_4KB, true, true); virtio_snd_pcm_xfer *header = (virtio_snd_pcm_xfer*)full_buffer; From 3d6fd94de6951550ae7ab7393402ef662f2aa7fa Mon Sep 17 00:00:00 2001 From: Di Ferrari Date: Sun, 3 Aug 2025 00:00:00 +0000 Subject: [PATCH 03/50] [CONSOLE] made continuous buffer for prints --- kernel/console/kio.c | 50 ++++++++++++++++++++++++++++++++------------ 1 file changed, 37 insertions(+), 13 deletions(-) diff --git a/kernel/console/kio.c b/kernel/console/kio.c index f8196e67..428e12da 100644 --- a/kernel/console/kio.c +++ b/kernel/console/kio.c @@ -3,9 +3,21 @@ #include "kconsole/kconsole.h" #include "std/string.h" #include "memory/page_allocator.h" +#include "std/memfunctions.h" static bool use_visual = true; void* print_buf; +uintptr_t cursor; + +void reset_buffer(){ + cursor = ((uintptr_t)print_buf); + memset(print_buf, 0, 0x3000); +} + +void init_print_buf(){ + print_buf = palloc(0x3000,true, false, true); + reset_buffer(); +} bool console_init(){ enable_uart(); @@ -62,22 +74,26 @@ void putc(const char c){ kconsole_putc(c); } -void init_print_buf(){ - print_buf = palloc(0x1000,true, false, false); -} - void kprintf(const char *fmt, ...){ if (!print_buf) init_print_buf(); va_list args; va_start(args, fmt); - char* buf = kalloc(print_buf, 256, ALIGN_64B, true, false); - size_t len = string_format_va_buf(fmt, buf, args); + + //TODO: If we don't read this value, the logs crash. Could it be stack overflow? We probably don't need KSP + mem_page *info = (mem_page*)print_buf; + info->next_free_mem_ptr = info->next_free_mem_ptr; + + if (cursor >= ((uintptr_t)print_buf) + 0x2F00){ + reset_buffer(); + } + + //TODO: string_format_va_buf should be given a maximum size + size_t len = string_format_va_buf(fmt, (char*)cursor, args); va_end(args); - puts(buf); + puts((char*)cursor); putc('\r'); putc('\n'); - //TODO: these buffers should be freed sometime, maybe after writing them to disk, and even those should be wiped eventually - // kfree((void*)buf, 256); + cursor += len; } void kprint(const char *fmt){ @@ -90,11 +106,19 @@ void kputf(const char *fmt, ...){ if (!print_buf) init_print_buf(); va_list args; va_start(args, fmt); - char* buf = kalloc(print_buf, 256, ALIGN_64B, true, false); - size_t len = string_format_va_buf(fmt, buf, args); + + //TODO: If we don't read this value, the logs crash + mem_page *info = (mem_page*)print_buf; + info->next_free_mem_ptr = info->next_free_mem_ptr; + + if (cursor >= ((uintptr_t)print_buf) + 0x2F00){ + reset_buffer(); + } + + size_t len = string_format_va_buf(fmt, (char*)cursor, args); va_end(args); - puts(buf); - // kfree((void*)buf, 256); + puts((char*)cursor); + cursor += len; } void disable_visual(){ From cd6b1d20bd864e0a5b4155dda59fef7b851ba8d3 Mon Sep 17 00:00:00 2001 From: Di Ferrari Date: Sun, 3 Aug 2025 00:00:00 +0000 Subject: [PATCH 04/50] [FS, MEM] Kernel always uses the same stack, fs has global fd_ids --- kernel/boot.S | 2 +- kernel/dev/driver_base.c | 7 +++++++ kernel/dev/driver_base.h | 8 ++++++++ kernel/filesystem/fat32.cpp | 10 +++------- kernel/filesystem/filesystem.cpp | 11 +++++------ kernel/linker.ld | 1 + kernel/process/scheduler.c | 3 --- 7 files changed, 25 insertions(+), 17 deletions(-) create mode 100644 kernel/dev/driver_base.c diff --git a/kernel/boot.S b/kernel/boot.S index 489f2c5b..62c3b5f1 100644 --- a/kernel/boot.S +++ b/kernel/boot.S @@ -102,7 +102,7 @@ el2_entry: eret stack_setup: - ldr x1, =stack_top + ldr x1, =ksp mov sp, x1 mov x29, xzr diff --git a/kernel/dev/driver_base.c b/kernel/dev/driver_base.c new file mode 100644 index 00000000..6aba402c --- /dev/null +++ b/kernel/dev/driver_base.c @@ -0,0 +1,7 @@ +#include "driver_base.h" + +uint64_t fd_id = 0; + +uint64_t reserve_fd_id(){ + return ++fd_id; +} \ No newline at end of file diff --git a/kernel/dev/driver_base.h b/kernel/dev/driver_base.h index 0caffff4..59d739f2 100644 --- a/kernel/dev/driver_base.h +++ b/kernel/dev/driver_base.h @@ -18,6 +18,14 @@ typedef enum FS_RESULT { #define VERSION_NUM(major,minor,patch,build) (uint64_t)((((uint64_t)major) << 48) | (((uint64_t)minor) << 32) | (((uint64_t)patch) << 16) | ((uint64_t)build)) +#ifdef __cplusplus +extern "C" { +#endif +uint64_t reserve_fd_id(); +#ifdef __cplusplus +} +#endif + typedef struct driver_module { const char* name; const char* mount; diff --git a/kernel/filesystem/fat32.cpp b/kernel/filesystem/fat32.cpp index 7803bdf0..ce8c0e7b 100644 --- a/kernel/filesystem/fat32.cpp +++ b/kernel/filesystem/fat32.cpp @@ -34,11 +34,6 @@ bool FAT32FS::init(uint32_t partition_sector){ if (mbs->boot_signature != 0xAA55){ kprintf("[fat32] Wrong boot signature %x",mbs->boot_signature); - uint8_t *bytes = ((uint8_t*)mbs); - for (int i = 0; i < 512; i++){ - kputf("%x",bytes[i]); - } - kprintf("Failed to read"); return false; } if (mbs->signature != 0x29 && mbs->signature != 0x28){ @@ -263,15 +258,16 @@ FS_RESULT FAT32FS::open_file(const char* path, file* descriptor){ sizedptr buf_ptr = walk_directory(count, mbs->first_cluster_of_root_directory, path, read_entry_handler); void *buf = (void*)buf_ptr.ptr; if (!buf) return FS_RESULT_NOTFOUND; - descriptor->id = open_files.size(); + descriptor->id = reserve_fd_id(); descriptor->size = buf_ptr.size; - open_files.add(descriptor->id, buf); + open_files.add(reserve_fd_id(), buf); //TODO: go back to using a linked list, and a static id for the file, ideally global for system return FS_RESULT_SUCCESS; } size_t FAT32FS::read_file(file *descriptor, void* buf, size_t size){ void* file = open_files[descriptor->id]; + //TODO: keep track of file size and limit copy to only that or less memcpy(buf, file, size); return size; } diff --git a/kernel/filesystem/filesystem.cpp b/kernel/filesystem/filesystem.cpp index 535630a7..f2e27312 100644 --- a/kernel/filesystem/filesystem.cpp +++ b/kernel/filesystem/filesystem.cpp @@ -6,6 +6,7 @@ #include "console/kio.h" #include "dev/module_loader.h" #include "memory/page_allocator.h" +#include "math/math.h" FAT32FS *fs_driver; @@ -64,23 +65,21 @@ bool init_boot_filesystem(){ void* read_file(const char *path, size_t size){ const char *search_path = path; - kprintf("Getting module for path %s",(uintptr_t)search_path); driver_module *mod = get_module(&search_path); - kprintf("Got module %x for path %s",(uintptr_t)mod,(uintptr_t)search_path); if (!mod) return 0; file fd = {0,0}; mod->open(search_path, &fd); void* pg = palloc(PAGE_SIZE, true, false, false); - char *TMP_BUF = (char*)kalloc(pg, fd.size, ALIGN_64B, true, false); - mod->read(&fd, TMP_BUF, fd.size, 0); + //TODO: TMP_BUF is not supposed to be used, you allocate your own memory + //TODO: There should be a separate open function, and keep track of which module handles which fd + char *TMP_BUF = (char*)kalloc(pg, min(fd.size,size), ALIGN_64B, true, false); + mod->read(&fd, TMP_BUF, min(fd.size,size), 0); return TMP_BUF; } sizedptr list_directory_contents(const char *path){ const char *search_path = path; - kprintf("Getting module for path %s",(uintptr_t)search_path); driver_module *mod = get_module(&search_path); - kprintf("Got module %x for path %s",(uintptr_t)mod,(uintptr_t)search_path); if (!mod) return {0,0}; return mod->readdir(search_path); } \ No newline at end of file diff --git a/kernel/linker.ld b/kernel/linker.ld index 543b8d78..d82c6028 100644 --- a/kernel/linker.ld +++ b/kernel/linker.ld @@ -22,6 +22,7 @@ SECTIONS { . = ALIGN(16); stack_bottom = .; .stack_fill : { FILL(0); . = . + 0x10000; } + ksp = .; stack_top = .; . = ALIGN(4096); diff --git a/kernel/process/scheduler.c b/kernel/process/scheduler.c index d7344adf..fddf3b73 100644 --- a/kernel/process/scheduler.c +++ b/kernel/process/scheduler.c @@ -29,8 +29,6 @@ typedef struct sleep_tracker { sleep_tracker sleeping[MAX_PROCS]; uint16_t sleep_count; -uint64_t ksp; - void save_context_registers(){ save_context(&processes[current_proc]); } @@ -123,7 +121,6 @@ void init_main_process(){ proc->heap = (uintptr_t)palloc(0x1000, true, false, false); proc->stack_size = 0x1000; proc->stack = (uintptr_t)palloc(proc->stack_size,true,false,true); - ksp = proc->stack + proc->stack_size; proc->sp = ksp; name_process(proc, "kernel"); proc_count++; From 46ad5e2d1ca348b5350f476bcd81d553e915cde5 Mon Sep 17 00:00:00 2001 From: Di Ferrari Date: Sun, 3 Aug 2025 00:00:00 +0000 Subject: [PATCH 05/50] [CONSOLE] fixing console crash --- kernel/console/kio.c | 41 +++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/kernel/console/kio.c b/kernel/console/kio.c index 428e12da..25b4e7c8 100644 --- a/kernel/console/kio.c +++ b/kernel/console/kio.c @@ -4,18 +4,24 @@ #include "std/string.h" #include "memory/page_allocator.h" #include "std/memfunctions.h" +#include "math/math.h" +#include "input/input_dispatch.h" +#include "kernel_processes/windows/windows.h" static bool use_visual = true; -void* print_buf; +char* print_buf; uintptr_t cursor; +#define CONSOLE_BUF_SIZE 0x3000 + void reset_buffer(){ - cursor = ((uintptr_t)print_buf); - memset(print_buf, 0, 0x3000); + cursor = (uintptr_t)print_buf; + memset(print_buf, 0, CONSOLE_BUF_SIZE); } + void init_print_buf(){ - print_buf = palloc(0x3000,true, false, true); + print_buf = palloc(CONSOLE_BUF_SIZE,true, false, true); reset_buffer(); } @@ -29,18 +35,24 @@ bool console_fini(){ } FS_RESULT console_open(const char *path, file *out_fd){ + out_fd->id = reserve_fd_id(); + out_fd->size = CONSOLE_BUF_SIZE; return FS_RESULT_SUCCESS; } size_t console_read(file *fd, char *out_buf, size_t size, file_offset offset){ + uart_puthex(size); + uart_puts(string_ca_max(print_buf+offset, size).data); + memcpy(out_buf, print_buf+offset, min(size,CONSOLE_BUF_SIZE)); return 0; } size_t console_write(file *fd, const char *buf, size_t size, file_offset offset){ + //TODO: kinda allowing arbitrary buffers here kprintf(buf); + return size; } - file_offset console_seek(file *fd, file_offset offset){ return 0; } @@ -93,13 +105,22 @@ void kprintf(const char *fmt, ...){ puts((char*)cursor); putc('\r'); putc('\n'); - cursor += len; + cursor += len-1; + *(char*)(cursor++) = '\r'; + *(char*)(cursor++) = '\n'; } void kprint(const char *fmt){ puts(fmt); putc('\r'); putc('\n'); + + while (*fmt != '\0') { + *(char*)(cursor++) = *fmt; + fmt++; + } + *(char*)(cursor++) = '\r'; + *(char*)(cursor++) = '\n'; } void kputf(const char *fmt, ...){ @@ -107,18 +128,14 @@ void kputf(const char *fmt, ...){ va_list args; va_start(args, fmt); - //TODO: If we don't read this value, the logs crash - mem_page *info = (mem_page*)print_buf; - info->next_free_mem_ptr = info->next_free_mem_ptr; - - if (cursor >= ((uintptr_t)print_buf) + 0x2F00){ + if (cursor >= ((uintptr_t)print_buf) + CONSOLE_BUF_SIZE - 0x100){ reset_buffer(); } size_t len = string_format_va_buf(fmt, (char*)cursor, args); va_end(args); puts((char*)cursor); - cursor += len; + cursor += len-1; } void disable_visual(){ From 212ded09cfd84f99cc7acf403cfaf5846c06ef48 Mon Sep 17 00:00:00 2001 From: Di Ferrari Date: Mon, 4 Aug 2025 00:00:00 +0000 Subject: [PATCH 06/50] [AUDIO] disabling audio buzz --- kernel/audio/virtio_audio_pci.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/kernel/audio/virtio_audio_pci.cpp b/kernel/audio/virtio_audio_pci.cpp index c25bd0c2..cbe3d28f 100644 --- a/kernel/audio/virtio_audio_pci.cpp +++ b/kernel/audio/virtio_audio_pci.cpp @@ -195,6 +195,7 @@ bool VirtioAudioDriver::config_streams(uint32_t streams){ kprintf("[VIRTIO_AUDIO error] Failed to configure stream %i",stream); } +#if false if (stream_info[stream].direction == VIRTIO_SND_D_OUTPUT){ kprintf("Playing from stream %i",stream); select_queue(&audio_dev, TRANSMIT_QUEUE); @@ -218,8 +219,8 @@ bool VirtioAudioDriver::config_streams(uint32_t streams){ select_queue(&audio_dev, CONTROL_QUEUE); } +#endif } - return true; } From da829a97374bb7de08401aefda5c1973265b4d01 Mon Sep 17 00:00:00 2001 From: Di Ferrari Date: Mon, 4 Aug 2025 00:00:00 +0000 Subject: [PATCH 07/50] [CONSOLE, WIP] re-enabling terminal by shortcut --- kernel/console/kio.c | 26 +++++++++++++++++++ kernel/console/kio.h | 3 +++ .../kernel_processes/boot/bootprocess_sm.cpp | 1 + 3 files changed, 30 insertions(+) diff --git a/kernel/console/kio.c b/kernel/console/kio.c index 25b4e7c8..7faeb9cf 100644 --- a/kernel/console/kio.c +++ b/kernel/console/kio.c @@ -140,8 +140,34 @@ void kputf(const char *fmt, ...){ void disable_visual(){ use_visual = false; + resume_window_draw(); + kconsole_clear(); } void enable_visual(){ use_visual = true; + pause_window_draw(); +} + +__attribute__((section(".text.kcoreprocesses"))) +void toggle_visual(){ + keypress kp = { + .modifier = KEY_MOD_ALT, + .keys[0] = 0x13//P + }; + uint16_t shortcut = sys_subscribe_shortcut_current(kp); + bool active = false; + while (1){ + if (sys_shortcut_triggered_current(shortcut)){ + if (active) + enable_visual(); + else + disable_visual(); + active = !active; + } + } +} + +process_t* start_terminal(){ + return create_kernel_process("terminal",toggle_visual); } \ No newline at end of file diff --git a/kernel/console/kio.h b/kernel/console/kio.h index 63764e66..0773bfd5 100644 --- a/kernel/console/kio.h +++ b/kernel/console/kio.h @@ -2,6 +2,7 @@ #include "types.h" #include "dev/driver_base.h" +#include "kernel_processes/kprocess_loader.h" #ifdef __cplusplus extern "C" { @@ -17,6 +18,8 @@ void putc(const char c); void disable_visual(); void enable_visual(); +process_t* start_terminal(); + extern driver_module console_module; #ifdef __cplusplus diff --git a/kernel/kernel_processes/boot/bootprocess_sm.cpp b/kernel/kernel_processes/boot/bootprocess_sm.cpp index 6a590036..03b5813b 100644 --- a/kernel/kernel_processes/boot/bootprocess_sm.cpp +++ b/kernel/kernel_processes/boot/bootprocess_sm.cpp @@ -9,6 +9,7 @@ BootSM::BootSM(){ } void BootSM::initialize(){ + start_terminal(); AdvanceToState(Bootscreen); } From 204f6fc1c22ec0916fc5729e4525d988d89afd01 Mon Sep 17 00:00:00 2001 From: Di Ferrari Date: Mon, 4 Aug 2025 00:00:00 +0000 Subject: [PATCH 08/50] [CONSOLE, WIP] showing latest logs in console (wrong offset) --- kernel/console/kconsole/console.cpp | 5 +++++ kernel/console/kconsole/kconsole.cpp | 11 ++++++++++- kernel/console/kconsole/kconsole.h | 1 + kernel/console/kconsole/kconsole.hpp | 8 ++++++-- kernel/console/kio.c | 5 +++-- kernel/kernel.c | 2 -- 6 files changed, 25 insertions(+), 7 deletions(-) diff --git a/kernel/console/kconsole/console.cpp b/kernel/console/kconsole/console.cpp index a013e2e5..de96131b 100644 --- a/kernel/console/kconsole/console.cpp +++ b/kernel/console/kconsole/console.cpp @@ -15,4 +15,9 @@ extern "C" void kconsole_puts(const char *s) { extern "C" void kconsole_clear() { kconsole.clear(); +} + +extern "C" void kconsole_refresh(){ + kconsole.refresh_input(); + gpu_flush(); } \ No newline at end of file diff --git a/kernel/console/kconsole/kconsole.cpp b/kernel/console/kconsole/kconsole.cpp index f6eb17df..bae04cb0 100644 --- a/kernel/console/kconsole/kconsole.cpp +++ b/kernel/console/kconsole/kconsole.cpp @@ -1,6 +1,7 @@ #include "kconsole.hpp" #include "console/serial/uart.h" #include "memory/page_allocator.h" +#include "filesystem/filesystem.h" KernelConsole::KernelConsole() : cursor_x(0), cursor_y(0), is_initialized(false){ resize(); @@ -84,6 +85,14 @@ void KernelConsole::scroll(){ redraw(); } +void KernelConsole::refresh_input(){ + resize(); + clear(); + void *content = read_file("/dev/console", buffer_data_size); + row_data = (char*)content; + redraw(); +} + void KernelConsole::redraw(){ screen_clear(); for (uint32_t y = 0; y < rows; y++){ @@ -113,4 +122,4 @@ void KernelConsole::clear(){ } } cursor_x = cursor_y = 0; -} +} \ No newline at end of file diff --git a/kernel/console/kconsole/kconsole.h b/kernel/console/kconsole/kconsole.h index c9a2e807..1a99d949 100644 --- a/kernel/console/kconsole/kconsole.h +++ b/kernel/console/kconsole/kconsole.h @@ -7,6 +7,7 @@ extern "C" { void kconsole_putc(char c); void kconsole_puts(const char *s); void kconsole_clear(); +void kconsole_refresh(); #ifdef __cplusplus } diff --git a/kernel/console/kconsole/kconsole.hpp b/kernel/console/kconsole/kconsole.hpp index 8ae368ac..faea90c3 100644 --- a/kernel/console/kconsole/kconsole.hpp +++ b/kernel/console/kconsole/kconsole.hpp @@ -16,8 +16,11 @@ class KernelConsole{ void scroll(); void clear(); void resize(); - -private: + void refresh_input(); + + void set_active(bool active); + + private: bool check_ready(); void screen_clear(); void redraw(); @@ -35,6 +38,7 @@ class KernelConsole{ uint32_t buffer_data_size; void *mem_page; + bool active = true; }; extern KernelConsole kconsole; diff --git a/kernel/console/kio.c b/kernel/console/kio.c index 7faeb9cf..aa6a1b68 100644 --- a/kernel/console/kio.c +++ b/kernel/console/kio.c @@ -105,7 +105,7 @@ void kprintf(const char *fmt, ...){ puts((char*)cursor); putc('\r'); putc('\n'); - cursor += len-1; + cursor += len; *(char*)(cursor++) = '\r'; *(char*)(cursor++) = '\n'; } @@ -147,6 +147,7 @@ void disable_visual(){ void enable_visual(){ use_visual = true; pause_window_draw(); + kconsole_refresh(); } __attribute__((section(".text.kcoreprocesses"))) @@ -159,11 +160,11 @@ void toggle_visual(){ bool active = false; while (1){ if (sys_shortcut_triggered_current(shortcut)){ + active = !active; if (active) enable_visual(); else disable_visual(); - active = !active; } } } diff --git a/kernel/kernel.c b/kernel/kernel.c index f6135671..a792cba8 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -82,8 +82,6 @@ void kernel_main() { if (network_available) launch_net_process(); init_bootprocess(); - - console_module.write(0, "Hello from module", 0, 0); kprint("Starting scheduler"); From 355aa7b2144cdfe8987924edecda007b049973bf Mon Sep 17 00:00:00 2001 From: Di Ferrari Date: Mon, 4 Aug 2025 00:00:00 +0000 Subject: [PATCH 09/50] [CONSOLE] refresh after newlines in put_string --- kernel/console/kconsole/kconsole.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/kernel/console/kconsole/kconsole.cpp b/kernel/console/kconsole/kconsole.cpp index bae04cb0..af988ea9 100644 --- a/kernel/console/kconsole/kconsole.cpp +++ b/kernel/console/kconsole/kconsole.cpp @@ -56,7 +56,11 @@ void KernelConsole::put_char(char c){ void KernelConsole::put_string(const char* str){ if (!check_ready()) return; - for (uint32_t i = 0; str[i]; i++) put_char(str[i]); + for (uint32_t i = 0; str[i]; i++){ + char c = str[i]; + put_char(c); + if (c == '\n') gpu_flush(); + } gpu_flush(); } From 77c98f1a802bffb2432e73034f4a8491639248cc Mon Sep 17 00:00:00 2001 From: Di Ferrari Date: Mon, 4 Aug 2025 00:00:00 +0000 Subject: [PATCH 10/50] [CONSOLE] created new instance of the console --- kernel/console/kconsole/console.cpp | 31 ++++++++++++++++++++++++--- kernel/console/kconsole/kconsole.cpp | 20 ++++++++--------- kernel/console/kconsole/kconsole.h | 5 ++++- kernel/console/kconsole/kconsole.hpp | 5 ++++- kernel/console/kio.c | 32 +--------------------------- 5 files changed, 47 insertions(+), 46 deletions(-) diff --git a/kernel/console/kconsole/console.cpp b/kernel/console/kconsole/console.cpp index de96131b..c8b7e216 100644 --- a/kernel/console/kconsole/console.cpp +++ b/kernel/console/kconsole/console.cpp @@ -1,6 +1,8 @@ #include "kconsole.hpp" #include "kconsole.h" #include "graph/graphics.h" +#include "input/input_dispatch.h" +#include "kernel_processes/windows/windows.h" KernelConsole kconsole; @@ -17,7 +19,30 @@ extern "C" void kconsole_clear() { kconsole.clear(); } -extern "C" void kconsole_refresh(){ - kconsole.refresh_input(); - gpu_flush(); +extern "C" void toggle_visual(){ + keypress kp = { + .modifier = KEY_MOD_ALT, + .rsvd = 0, + .keys = {0x13}, + }; + uint16_t shortcut = sys_subscribe_shortcut_current(kp); + bool active = false; + KernelConsole *console = new KernelConsole(); + console->initialize(); + while (1){ + if (sys_shortcut_triggered_current(shortcut)){ + active = !active; + if (active){ + pause_window_draw(); + console->refresh(); + } else { + resume_window_draw(); + console->clear(); + } + } + } +} + +process_t* start_terminal(){ + return create_kernel_process("terminal",toggle_visual); } \ No newline at end of file diff --git a/kernel/console/kconsole/kconsole.cpp b/kernel/console/kconsole/kconsole.cpp index af988ea9..d562782e 100644 --- a/kernel/console/kconsole/kconsole.cpp +++ b/kernel/console/kconsole/kconsole.cpp @@ -4,6 +4,12 @@ #include "filesystem/filesystem.h" KernelConsole::KernelConsole() : cursor_x(0), cursor_y(0), is_initialized(false){ + initialize(); +} + +void KernelConsole::initialize(){ + is_initialized = true; + mem_page = palloc(PAGE_SIZE, true, true, false); resize(); clear(); } @@ -11,10 +17,7 @@ KernelConsole::KernelConsole() : cursor_x(0), cursor_y(0), is_initialized(false) bool KernelConsole::check_ready(){ if (!gpu_ready()) return false; if (!is_initialized){ - is_initialized= true; - mem_page = palloc(PAGE_SIZE, true, true, false); - resize(); - clear(); + initialize(); } return true; } @@ -26,8 +29,6 @@ void KernelConsole::resize(){ if (row_data) kfree(row_data, buffer_data_size); buffer_data_size = rows * columns; - uart_puts("Data Size "); - uart_puthex(buffer_data_size); row_data = (char*)kalloc(mem_page, buffer_data_size, ALIGN_16B, true, true); if (!row_data){ rows = columns = 0; @@ -74,7 +75,7 @@ void KernelConsole::newline(){ } cursor_x = 0; cursor_y++; - if (cursor_y >= rows){ + if (cursor_y >= rows - 1){ scroll(); cursor_y = rows - 1; } @@ -89,12 +90,11 @@ void KernelConsole::scroll(){ redraw(); } -void KernelConsole::refresh_input(){ +void KernelConsole::refresh(){ resize(); clear(); - void *content = read_file("/dev/console", buffer_data_size); - row_data = (char*)content; redraw(); + gpu_flush(); } void KernelConsole::redraw(){ diff --git a/kernel/console/kconsole/kconsole.h b/kernel/console/kconsole/kconsole.h index 1a99d949..893de9a2 100644 --- a/kernel/console/kconsole/kconsole.h +++ b/kernel/console/kconsole/kconsole.h @@ -1,5 +1,8 @@ #pragma once +#include "process/process.h" +#include "kernel_processes/kprocess_loader.h" + #ifdef __cplusplus extern "C" { #endif @@ -7,7 +10,7 @@ extern "C" { void kconsole_putc(char c); void kconsole_puts(const char *s); void kconsole_clear(); -void kconsole_refresh(); +process_t* start_terminal(); #ifdef __cplusplus } diff --git a/kernel/console/kconsole/kconsole.hpp b/kernel/console/kconsole/kconsole.hpp index faea90c3..41c46259 100644 --- a/kernel/console/kconsole/kconsole.hpp +++ b/kernel/console/kconsole/kconsole.hpp @@ -9,6 +9,8 @@ class KernelConsole{ public: KernelConsole(); + void initialize(); + void put_char(char c); void put_string(const char* str); @@ -16,7 +18,8 @@ class KernelConsole{ void scroll(); void clear(); void resize(); - void refresh_input(); + + void refresh(); void set_active(bool active); diff --git a/kernel/console/kio.c b/kernel/console/kio.c index aa6a1b68..5665f5f6 100644 --- a/kernel/console/kio.c +++ b/kernel/console/kio.c @@ -5,8 +5,6 @@ #include "memory/page_allocator.h" #include "std/memfunctions.h" #include "math/math.h" -#include "input/input_dispatch.h" -#include "kernel_processes/windows/windows.h" static bool use_visual = true; char* print_buf; @@ -41,8 +39,6 @@ FS_RESULT console_open(const char *path, file *out_fd){ } size_t console_read(file *fd, char *out_buf, size_t size, file_offset offset){ - uart_puthex(size); - uart_puts(string_ca_max(print_buf+offset, size).data); memcpy(out_buf, print_buf+offset, min(size,CONSOLE_BUF_SIZE)); return 0; } @@ -91,7 +87,7 @@ void kprintf(const char *fmt, ...){ va_list args; va_start(args, fmt); - //TODO: If we don't read this value, the logs crash. Could it be stack overflow? We probably don't need KSP + //TODO: If we don't read this value, the logs crash. Could it be stack overflow? mem_page *info = (mem_page*)print_buf; info->next_free_mem_ptr = info->next_free_mem_ptr; @@ -140,35 +136,9 @@ void kputf(const char *fmt, ...){ void disable_visual(){ use_visual = false; - resume_window_draw(); kconsole_clear(); } void enable_visual(){ use_visual = true; - pause_window_draw(); - kconsole_refresh(); -} - -__attribute__((section(".text.kcoreprocesses"))) -void toggle_visual(){ - keypress kp = { - .modifier = KEY_MOD_ALT, - .keys[0] = 0x13//P - }; - uint16_t shortcut = sys_subscribe_shortcut_current(kp); - bool active = false; - while (1){ - if (sys_shortcut_triggered_current(shortcut)){ - active = !active; - if (active) - enable_visual(); - else - disable_visual(); - } - } -} - -process_t* start_terminal(){ - return create_kernel_process("terminal",toggle_visual); } \ No newline at end of file From 65613cfb8879db8af7835d6d8088c9c4c6e0dafb Mon Sep 17 00:00:00 2001 From: Di Ferrari Date: Mon, 4 Aug 2025 00:00:00 +0000 Subject: [PATCH 11/50] [GPU] removed unused font import from gpu --- kernel/graph/drivers/ramfb_driver/ramfb.cpp | 1 - kernel/graph/drivers/videocore/videocore.cpp | 1 - kernel/graph/drivers/virtio_gpu_pci/virtio_gpu_pci.cpp | 1 - 3 files changed, 3 deletions(-) diff --git a/kernel/graph/drivers/ramfb_driver/ramfb.cpp b/kernel/graph/drivers/ramfb_driver/ramfb.cpp index 63494c34..c0b1c9e0 100644 --- a/kernel/graph/drivers/ramfb_driver/ramfb.cpp +++ b/kernel/graph/drivers/ramfb_driver/ramfb.cpp @@ -2,7 +2,6 @@ #include "fw/fw_cfg.h" #include "memory/kalloc.h" #include "console/kio.h" -#include "graph/font8x8_bridge.h" #include "ui/draw/draw.h" #include "memory/memory_access.h" #include "std/std.hpp" diff --git a/kernel/graph/drivers/videocore/videocore.cpp b/kernel/graph/drivers/videocore/videocore.cpp index 7fefd0f2..d0c2aedd 100644 --- a/kernel/graph/drivers/videocore/videocore.cpp +++ b/kernel/graph/drivers/videocore/videocore.cpp @@ -2,7 +2,6 @@ #include "fw/fw_cfg.h" #include "memory/kalloc.h" #include "console/kio.h" -#include "graph/font8x8_bridge.h" #include "ui/draw/draw.h" #include "memory/memory_access.h" #include "std/std.hpp" diff --git a/kernel/graph/drivers/virtio_gpu_pci/virtio_gpu_pci.cpp b/kernel/graph/drivers/virtio_gpu_pci/virtio_gpu_pci.cpp index bd6102f0..df4e91e3 100644 --- a/kernel/graph/drivers/virtio_gpu_pci/virtio_gpu_pci.cpp +++ b/kernel/graph/drivers/virtio_gpu_pci/virtio_gpu_pci.cpp @@ -2,7 +2,6 @@ #include "pci.h" #include "memory/kalloc.h" #include "console/kio.h" -#include "graph/font8x8_bridge.h" #include "ui/draw/draw.h" #include "std/std.hpp" From 7101b7abe433fa4acb2a3afe1a7e522d7d59c5fe Mon Sep 17 00:00:00 2001 From: Di Ferrari Date: Mon, 4 Aug 2025 00:00:00 +0000 Subject: [PATCH 12/50] [CONSOLE] added input to terminal --- kernel/console/kconsole/console.cpp | 4 +++ kernel/console/kconsole/kconsole.cpp | 15 +++++++++++ kernel/console/kconsole/kconsole.hpp | 3 ++- kernel/graph/font8x8_bridge.h | 1 + kernel/kernel_processes/boot/login_screen.c | 18 ------------- shared/input_keycodes.c | 5 ++++ shared/input_keycodes.h | 28 ++++++++++++++++++--- 7 files changed, 51 insertions(+), 23 deletions(-) create mode 100644 shared/input_keycodes.c diff --git a/kernel/console/kconsole/console.cpp b/kernel/console/kconsole/console.cpp index c8b7e216..e1b22488 100644 --- a/kernel/console/kconsole/console.cpp +++ b/kernel/console/kconsole/console.cpp @@ -34,12 +34,16 @@ extern "C" void toggle_visual(){ active = !active; if (active){ pause_window_draw(); + sys_focus_current(); console->refresh(); } else { resume_window_draw(); console->clear(); } } + if (active){ + console->handle_input(); + } } } diff --git a/kernel/console/kconsole/kconsole.cpp b/kernel/console/kconsole/kconsole.cpp index d562782e..0329efdc 100644 --- a/kernel/console/kconsole/kconsole.cpp +++ b/kernel/console/kconsole/kconsole.cpp @@ -2,6 +2,7 @@ #include "console/serial/uart.h" #include "memory/page_allocator.h" #include "filesystem/filesystem.h" +#include "input/input_dispatch.h" KernelConsole::KernelConsole() : cursor_x(0), cursor_y(0), is_initialized(false){ initialize(); @@ -126,4 +127,18 @@ void KernelConsole::clear(){ } } cursor_x = cursor_y = 0; +} + +void KernelConsole::handle_input(){ + keypress kp; + while (sys_read_input_current(&kp)){ + for (int i = 0; i < 6; i++){ + char key = kp.keys[i]; + char readable = hid_to_char((uint8_t)key); + if (readable){ + put_char(readable); + gpu_flush(); + } + } + } } \ No newline at end of file diff --git a/kernel/console/kconsole/kconsole.hpp b/kernel/console/kconsole/kconsole.hpp index 41c46259..7eabd5b6 100644 --- a/kernel/console/kconsole/kconsole.hpp +++ b/kernel/console/kconsole/kconsole.hpp @@ -22,8 +22,9 @@ class KernelConsole{ void refresh(); void set_active(bool active); + void handle_input(); - private: +private: bool check_ready(); void screen_clear(); void redraw(); diff --git a/kernel/graph/font8x8_bridge.h b/kernel/graph/font8x8_bridge.h index 1a70f8e6..bbc9a286 100644 --- a/kernel/graph/font8x8_bridge.h +++ b/kernel/graph/font8x8_bridge.h @@ -3,6 +3,7 @@ extern "C" { #endif #include "types.h" +//TODO: remove this header, use just #ifdef and else const uint8_t* get_font8x8(uint8_t c); #ifdef __cplusplus diff --git a/kernel/kernel_processes/boot/login_screen.c b/kernel/kernel_processes/boot/login_screen.c index c89ce3fa..ee1d00b7 100644 --- a/kernel/kernel_processes/boot/login_screen.c +++ b/kernel/kernel_processes/boot/login_screen.c @@ -10,24 +10,6 @@ #include "std/string.h" #include "syscalls/syscalls.h" -//TODO: properly handle keypad -static const char hid_keycode_to_char[256] = { - [0x04] = 'a', [0x05] = 'b', [0x06] = 'c', [0x07] = 'd', - [0x08] = 'e', [0x09] = 'f', [0x0A] = 'g', [0x0B] = 'h', - [0x0C] = 'i', [0x0D] = 'j', [0x0E] = 'k', [0x0F] = 'l', - [0x10] = 'm', [0x11] = 'n', [0x12] = 'o', [0x13] = 'p', - [0x14] = 'q', [0x15] = 'r', [0x16] = 's', [0x17] = 't', - [0x18] = 'u', [0x19] = 'v', [0x1A] = 'w', [0x1B] = 'x', - [0x1C] = 'y', [0x1D] = 'z', - [0x1E] = '1', [0x1F] = '2', [0x20] = '3', [0x21] = '4', - [0x22] = '5', [0x23] = '6', [0x24] = '7', [0x25] = '8', - [0x26] = '9', [0x27] = '0', - [0x28] = '\n', [0x2C] = ' ', [0x2D] = '-', [0x2E] = '=', - [0x2F] = '[', [0x30] = ']', [0x31] = '\\', [0x33] = ';', - [0x34] = '\'', [0x35] = '`', [0x36] = ',', [0x37] = '.', - [0x38] = '/', [0x58] = '\n', -}; - bool keypress_contains(keypress *kp, char key, uint8_t modifier){ if (kp->modifier != modifier) return false;//TODO: This is not entirely accurate, some modifiers do not change key diff --git a/shared/input_keycodes.c b/shared/input_keycodes.c new file mode 100644 index 00000000..67cf6f7a --- /dev/null +++ b/shared/input_keycodes.c @@ -0,0 +1,5 @@ +#include "input_keycodes.h" + +char hid_to_char(unsigned char c){ + return hid_keycode_to_char[c]; +} \ No newline at end of file diff --git a/shared/input_keycodes.h b/shared/input_keycodes.h index 14576eb5..80e693b6 100644 --- a/shared/input_keycodes.h +++ b/shared/input_keycodes.h @@ -1,8 +1,5 @@ #pragma once -#ifdef __cplusplus -extern "C" { -#endif #define KEY_ARROW_UP 0x52 #define KEY_ARROW_DOWN 0x51 #define KEY_ARROW_LEFT 0x50 @@ -15,6 +12,29 @@ extern "C" { #define KEY_MOD_CMD 0x8 #define KEY_MOD_ALT 0x4 #define KEY_MOD_CTRL 0x1 + +#ifndef __cplusplus +//TODO: properly handle keypad +static const char hid_keycode_to_char[256] = { + [0x04] = 'a', [0x05] = 'b', [0x06] = 'c', [0x07] = 'd', + [0x08] = 'e', [0x09] = 'f', [0x0A] = 'g', [0x0B] = 'h', + [0x0C] = 'i', [0x0D] = 'j', [0x0E] = 'k', [0x0F] = 'l', + [0x10] = 'm', [0x11] = 'n', [0x12] = 'o', [0x13] = 'p', + [0x14] = 'q', [0x15] = 'r', [0x16] = 's', [0x17] = 't', + [0x18] = 'u', [0x19] = 'v', [0x1A] = 'w', [0x1B] = 'x', + [0x1C] = 'y', [0x1D] = 'z', + [0x1E] = '1', [0x1F] = '2', [0x20] = '3', [0x21] = '4', + [0x22] = '5', [0x23] = '6', [0x24] = '7', [0x25] = '8', + [0x26] = '9', [0x27] = '0', + [0x28] = '\n', [0x2C] = ' ', [0x2D] = '-', [0x2E] = '=', + [0x2F] = '[', [0x30] = ']', [0x31] = '\\', [0x33] = ';', + [0x34] = '\'', [0x35] = '`', [0x36] = ',', [0x37] = '.', + [0x38] = '/', [0x58] = '\n', +}; +#else +extern "C" { +#endif +char hid_to_char(unsigned char c); #ifdef __cplusplus } -#endif \ No newline at end of file +#endif \ No newline at end of file From 94f1c4636f72c3b396ab85b9cdfe3e5154cdac4c Mon Sep 17 00:00:00 2001 From: Di Ferrari Date: Mon, 4 Aug 2025 00:00:00 +0000 Subject: [PATCH 13/50] [CONSOLE] fixed weird log buffer crash --- kernel/console/kio.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/kernel/console/kio.c b/kernel/console/kio.c index 5665f5f6..fdda19d1 100644 --- a/kernel/console/kio.c +++ b/kernel/console/kio.c @@ -87,10 +87,6 @@ void kprintf(const char *fmt, ...){ va_list args; va_start(args, fmt); - //TODO: If we don't read this value, the logs crash. Could it be stack overflow? - mem_page *info = (mem_page*)print_buf; - info->next_free_mem_ptr = info->next_free_mem_ptr; - if (cursor >= ((uintptr_t)print_buf) + 0x2F00){ reset_buffer(); } From 2edc845426e3e76079e5712f3f73d3c7a7dc5000 Mon Sep 17 00:00:00 2001 From: Di Ferrari Date: Mon, 4 Aug 2025 00:00:00 +0000 Subject: [PATCH 14/50] [CONSOLE] no I didn't :( --- kernel/console/kio.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/kernel/console/kio.c b/kernel/console/kio.c index fdda19d1..fb0f7f56 100644 --- a/kernel/console/kio.c +++ b/kernel/console/kio.c @@ -87,6 +87,10 @@ void kprintf(const char *fmt, ...){ va_list args; va_start(args, fmt); + //TODO: If we don't read this value, the logs crash. Could it be stack overflow? + mem_page *info = (mem_page*)print_buf; + // info->next_free_mem_ptr = info->next_free_mem_ptr; + if (cursor >= ((uintptr_t)print_buf) + 0x2F00){ reset_buffer(); } From ae9d7cf13e2e2a22336b8471c647ae550d9f1a9d Mon Sep 17 00:00:00 2001 From: Di Ferrari Date: Mon, 4 Aug 2025 00:00:00 +0000 Subject: [PATCH 15/50] [CONSOLE] handling double keys (99% accuracy) --- kernel/console/kconsole/kconsole.cpp | 5 ++++- kernel/console/kio.c | 3 +-- kernel/input/USBKeyboard.cpp | 4 +++- kernel/input/input_dispatch.cpp | 14 ++++++++++++++ kernel/input/input_dispatch.h | 2 ++ kernel/kernel_processes/boot/login_screen.c | 9 --------- kernel/memory/page_allocator.c | 3 +++ 7 files changed, 27 insertions(+), 13 deletions(-) diff --git a/kernel/console/kconsole/kconsole.cpp b/kernel/console/kconsole/kconsole.cpp index 0329efdc..f2fa5e39 100644 --- a/kernel/console/kconsole/kconsole.cpp +++ b/kernel/console/kconsole/kconsole.cpp @@ -129,11 +129,14 @@ void KernelConsole::clear(){ cursor_x = cursor_y = 0; } +// #include "../kio.h" + void KernelConsole::handle_input(){ keypress kp; - while (sys_read_input_current(&kp)){ + if (sys_read_input_current(&kp)){ for (int i = 0; i < 6; i++){ char key = kp.keys[i]; + // kprintf("Key[%i] %i", i, key); char readable = hid_to_char((uint8_t)key); if (readable){ put_char(readable); diff --git a/kernel/console/kio.c b/kernel/console/kio.c index fb0f7f56..f45b903d 100644 --- a/kernel/console/kio.c +++ b/kernel/console/kio.c @@ -89,8 +89,7 @@ void kprintf(const char *fmt, ...){ //TODO: If we don't read this value, the logs crash. Could it be stack overflow? mem_page *info = (mem_page*)print_buf; - // info->next_free_mem_ptr = info->next_free_mem_ptr; - + if (cursor >= ((uintptr_t)print_buf) + 0x2F00){ reset_buffer(); } diff --git a/kernel/input/USBKeyboard.cpp b/kernel/input/USBKeyboard.cpp index 8ba43a40..58b113ce 100644 --- a/kernel/input/USBKeyboard.cpp +++ b/kernel/input/USBKeyboard.cpp @@ -33,8 +33,10 @@ void USBKeyboard::process_data(USBDriver *driver){ void USBKeyboard::process_keypress(keypress *rkp){ keypress kp; if (is_new_keypress(rkp, &last_keypress) || repeated_keypresses > 3){ - if (is_new_keypress(rkp, &last_keypress)) + if (is_new_keypress(rkp, &last_keypress)){ repeated_keypresses = 0; + remove_double_keypresses(rkp, &last_keypress); + } kp.modifier = rkp->modifier; // kprintf("Mod: %i", kp.modifier); for (int i = 0; i < 6; i++){ diff --git a/kernel/input/input_dispatch.cpp b/kernel/input/input_dispatch.cpp index 1c3433f3..ae901786 100644 --- a/kernel/input/input_dispatch.cpp +++ b/kernel/input/input_dispatch.cpp @@ -89,6 +89,20 @@ bool is_new_keypress(keypress* current, keypress* previous) { return false; } +void remove_double_keypresses(keypress* current, keypress* previous){ + for (int i = 0; i < 6; i++) + if (keypress_contains(previous, current->keys[i], previous->modifier)) current->keys[i] = 0; +} + +bool keypress_contains(keypress *kp, char key, uint8_t modifier){ + if (kp->modifier != modifier) return false;//TODO: This is not entirely accurate, some modifiers do not change key + + for (int i = 0; i < 6; i++) + if (kp->keys[i] == key) + return true; + return false; +} + bool sys_read_input(int pid, keypress *out){ process_t *process = get_proc_by_pid(pid); if (process->input_buffer.read_index == process->input_buffer.write_index) return false; diff --git a/kernel/input/input_dispatch.h b/kernel/input/input_dispatch.h index aa969517..dfa09e0d 100644 --- a/kernel/input/input_dispatch.h +++ b/kernel/input/input_dispatch.h @@ -24,6 +24,8 @@ bool sys_shortcut_triggered_current(uint16_t sid); bool sys_shortcut_triggered(uint16_t pid, uint16_t sid); bool is_new_keypress(keypress* current, keypress* previous); +bool keypress_contains(keypress *kp, char key, uint8_t modifier); +void remove_double_keypresses(keypress* current, keypress* previous); bool input_init(); diff --git a/kernel/kernel_processes/boot/login_screen.c b/kernel/kernel_processes/boot/login_screen.c index ee1d00b7..3bd4dcd7 100644 --- a/kernel/kernel_processes/boot/login_screen.c +++ b/kernel/kernel_processes/boot/login_screen.c @@ -10,15 +10,6 @@ #include "std/string.h" #include "syscalls/syscalls.h" -bool keypress_contains(keypress *kp, char key, uint8_t modifier){ - if (kp->modifier != modifier) return false;//TODO: This is not entirely accurate, some modifiers do not change key - - for (int i = 0; i < 6; i++) - if (kp->keys[i] == key) - return true; - return false; -} - __attribute__((section(".text.kcoreprocesses"))) void login_screen(){ sys_focus_current(); diff --git a/kernel/memory/page_allocator.c b/kernel/memory/page_allocator.c index 0d7ebf7c..114c5ba8 100644 --- a/kernel/memory/page_allocator.c +++ b/kernel/memory/page_allocator.c @@ -36,6 +36,7 @@ void page_allocator_init() { } void pfree(void* ptr, uint64_t size) { + //TODO: review this, we're not using size uint64_t addr = (uint64_t)ptr; addr /= PAGE_SIZE; uint64_t table_index = addr/64; @@ -58,7 +59,9 @@ void* palloc(uint64_t size, bool kernel, bool device, bool full) { uint64_t bit = __builtin_ctzll(inv); do { bool found = true; + //TODO: check bounds for (uint64_t b = bit; b < bit + (page_count - 1); b++){ + //TODO: Review parentheses here if (!mem_bitmap[i] >> b & 1){ bit += page_count; found = false; From 61a592e1c6e9daacb9a99a3f880da5bc9878aa92 Mon Sep 17 00:00:00 2001 From: Di Ferrari Date: Tue, 5 Aug 2025 00:00:00 +0000 Subject: [PATCH 16/50] [CONSOLE] fixed va_list alignment issue crash --- kernel/console/kio.c | 5 +---- kernel/linker.ld | 2 +- shared/std/string.c | 2 +- shared/syscalls/syscalls.c | 2 +- 4 files changed, 4 insertions(+), 7 deletions(-) diff --git a/kernel/console/kio.c b/kernel/console/kio.c index f45b903d..af145786 100644 --- a/kernel/console/kio.c +++ b/kernel/console/kio.c @@ -84,11 +84,8 @@ void putc(const char c){ void kprintf(const char *fmt, ...){ if (!print_buf) init_print_buf(); - va_list args; + __attribute__((aligned(16))) va_list args; va_start(args, fmt); - - //TODO: If we don't read this value, the logs crash. Could it be stack overflow? - mem_page *info = (mem_page*)print_buf; if (cursor >= ((uintptr_t)print_buf) + 0x2F00){ reset_buffer(); diff --git a/kernel/linker.ld b/kernel/linker.ld index d82c6028..1d3833dd 100644 --- a/kernel/linker.ld +++ b/kernel/linker.ld @@ -14,7 +14,7 @@ SECTIONS { bss = .; *(.bss) } - . = ALIGN(4096); /* align to page size */ + . = ALIGN(4096); __bss_end = .; __bss_size = __bss_end - __bss_start; .vectors : { KEEP(*(.vectors)) } diff --git a/shared/std/string.c b/shared/std/string.c index 54f3e682..264a6a44 100644 --- a/shared/std/string.c +++ b/shared/std/string.c @@ -127,7 +127,7 @@ bool string_equals(string a, string b){ string string_format(const char *fmt, ...){ if (fmt == NULL) return (string){ .data = NULL, .length = 0, .mem_length = 0}; - va_list args; + __attribute__((aligned(16))) va_list args; va_start(args, fmt); string result = string_format_va(fmt, args); va_end(args); diff --git a/shared/syscalls/syscalls.c b/shared/syscalls/syscalls.c index 5811ee72..807858f2 100644 --- a/shared/syscalls/syscalls.c +++ b/shared/syscalls/syscalls.c @@ -2,7 +2,7 @@ #include "std/string.h" void printf(const char *fmt, ...){ - va_list args; + __attribute__((aligned(16))) va_list args; va_start(args, fmt); string str = string_format_va(fmt, args); va_end(args); From 35379d2291da3a1870d780a1317fb84930b8de25 Mon Sep 17 00:00:00 2001 From: Di Ferrari Date: Wed, 6 Aug 2025 00:00:00 +0000 Subject: [PATCH 17/50] [CONSOLE] fixed alignment in putf --- kernel/console/kio.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/console/kio.c b/kernel/console/kio.c index af145786..70512750 100644 --- a/kernel/console/kio.c +++ b/kernel/console/kio.c @@ -117,7 +117,7 @@ void kprint(const char *fmt){ void kputf(const char *fmt, ...){ if (!print_buf) init_print_buf(); - va_list args; + __attribute__((aligned(16))) va_list args; va_start(args, fmt); if (cursor >= ((uintptr_t)print_buf) + CONSOLE_BUF_SIZE - 0x100){ From 93f2c396dccfbd10558329295b8da5a7854a7d84 Mon Sep 17 00:00:00 2001 From: Di Ferrari Date: Wed, 6 Aug 2025 00:00:00 +0000 Subject: [PATCH 18/50] [CONSOLE] added cursor to console, separated input into terminal class --- kernel/console/kconsole/console.cpp | 11 +++---- kernel/console/kconsole/kconsole.cpp | 45 ++++++++++++++-------------- kernel/console/kconsole/kconsole.hpp | 11 ++++--- kernel/console/kconsole/terminal.cpp | 20 +++++++++++++ kernel/console/kconsole/terminal.hpp | 9 ++++++ kernel/theme/RedactedOS.h | 5 +++- shared/ui/draw/draw.c | 8 ++--- shared/ui/draw/draw.h | 2 ++ 8 files changed, 74 insertions(+), 37 deletions(-) create mode 100644 kernel/console/kconsole/terminal.cpp create mode 100644 kernel/console/kconsole/terminal.hpp diff --git a/kernel/console/kconsole/console.cpp b/kernel/console/kconsole/console.cpp index e1b22488..e1ce78a6 100644 --- a/kernel/console/kconsole/console.cpp +++ b/kernel/console/kconsole/console.cpp @@ -3,6 +3,7 @@ #include "graph/graphics.h" #include "input/input_dispatch.h" #include "kernel_processes/windows/windows.h" +#include "terminal.hpp" KernelConsole kconsole; @@ -27,22 +28,22 @@ extern "C" void toggle_visual(){ }; uint16_t shortcut = sys_subscribe_shortcut_current(kp); bool active = false; - KernelConsole *console = new KernelConsole(); - console->initialize(); + Terminal *terminal = new Terminal(); + terminal->initialize(); while (1){ if (sys_shortcut_triggered_current(shortcut)){ active = !active; if (active){ pause_window_draw(); sys_focus_current(); - console->refresh(); + terminal->refresh(); } else { resume_window_draw(); - console->clear(); + terminal->clear(); } } if (active){ - console->handle_input(); + terminal->handle_input(); } } } diff --git a/kernel/console/kconsole/kconsole.cpp b/kernel/console/kconsole/kconsole.cpp index f2fa5e39..4c58ee98 100644 --- a/kernel/console/kconsole/kconsole.cpp +++ b/kernel/console/kconsole/kconsole.cpp @@ -2,7 +2,7 @@ #include "console/serial/uart.h" #include "memory/page_allocator.h" #include "filesystem/filesystem.h" -#include "input/input_dispatch.h" +#include "theme/theme.h" KernelConsole::KernelConsole() : cursor_x(0), cursor_y(0), is_initialized(false){ initialize(); @@ -26,7 +26,7 @@ bool KernelConsole::check_ready(){ void KernelConsole::resize(){ gpu_size screen_size = gpu_get_screen_size(); columns = screen_size.width / char_width; - rows = screen_size.height / char_height; + rows = screen_size.height / line_height; if (row_data) kfree(row_data, buffer_data_size); buffer_data_size = rows * columns; @@ -45,6 +45,7 @@ void KernelConsole::put_char(char c){ if (!check_ready()) return; if (c == '\n'){ newline(); + gpu_flush(); return; } if (cursor_x >= columns) newline(); @@ -52,17 +53,29 @@ void KernelConsole::put_char(char c){ uint32_t row_index = row_ring.peek(); char* line = row_data + row_index * columns; line[cursor_x] = c; - gpu_draw_char({cursor_x * char_width, cursor_y * char_height}, c, 1, 0xFFFFFFFF); + gpu_draw_char({cursor_x * char_width, (cursor_y * line_height)+(line_height/2)}, c, 1, COLOR_WHITE); cursor_x++; } +void KernelConsole::draw_cursor(){ + if (last_drawn_cursor_x >= 0 && last_drawn_cursor_y >= 0){ + gpu_fill_rect({{last_drawn_cursor_x*char_width, last_drawn_cursor_y * line_height}, {char_width, line_height}}, COLOR_BLACK); + char *line = row_data + (last_drawn_cursor_y * columns); + uart_putc(line[last_drawn_cursor_x]); + gpu_draw_char({last_drawn_cursor_x * char_width, (last_drawn_cursor_y * line_height)+(line_height/2)}, line[last_drawn_cursor_x], 1, COLOR_WHITE); + } + gpu_fill_rect({{cursor_x*char_width, cursor_y * line_height}, {char_width, line_height}}, COLOR_WHITE); + last_drawn_cursor_x = cursor_x; + last_drawn_cursor_y = cursor_y; +} + void KernelConsole::put_string(const char* str){ if (!check_ready()) return; for (uint32_t i = 0; str[i]; i++){ char c = str[i]; put_char(c); - if (c == '\n') gpu_flush(); } + draw_cursor(); gpu_flush(); } @@ -106,14 +119,17 @@ void KernelConsole::redraw(){ row_ring.push(row_index); char* line = row_data + row_index * columns; for (uint32_t x = 0; x < columns; x++){ - gpu_draw_char({x * char_width, y * char_height}, line[x], 1, 0xFFFFFFFF); + gpu_draw_char({x * char_width, (y * line_height)+(line_height/2)}, line[x], 1, COLOR_WHITE); } } } + draw_cursor(); } void KernelConsole::screen_clear(){ - gpu_clear(0x0); + gpu_clear(COLOR_BLACK); + last_drawn_cursor_x = -1; + last_drawn_cursor_y = -1; } void KernelConsole::clear(){ @@ -128,20 +144,3 @@ void KernelConsole::clear(){ } cursor_x = cursor_y = 0; } - -// #include "../kio.h" - -void KernelConsole::handle_input(){ - keypress kp; - if (sys_read_input_current(&kp)){ - for (int i = 0; i < 6; i++){ - char key = kp.keys[i]; - // kprintf("Key[%i] %i", i, key); - char readable = hid_to_char((uint8_t)key); - if (readable){ - put_char(readable); - gpu_flush(); - } - } - } -} \ No newline at end of file diff --git a/kernel/console/kconsole/kconsole.hpp b/kernel/console/kconsole/kconsole.hpp index 7eabd5b6..f7bbd699 100644 --- a/kernel/console/kconsole/kconsole.hpp +++ b/kernel/console/kconsole/kconsole.hpp @@ -4,6 +4,7 @@ #include "data_struct/ring_buffer.hpp" #include "graph/graphics.h" #include "memory/kalloc.h" +#include "ui/draw/draw.h" class KernelConsole{ public: @@ -22,23 +23,25 @@ class KernelConsole{ void refresh(); void set_active(bool active); - void handle_input(); -private: +protected: bool check_ready(); void screen_clear(); void redraw(); + void draw_cursor(); uint32_t cursor_x, cursor_y; + int32_t last_drawn_cursor_x, last_drawn_cursor_y; uint32_t columns, rows; bool is_initialized; - static constexpr uint32_t char_width=8; - static constexpr uint32_t char_height=16; + static constexpr uint32_t char_width=CHAR_SIZE; + static constexpr uint32_t line_height=CHAR_SIZE*2; static constexpr uint32_t max_rows=128; RingBuffer row_ring; char* row_data; + uint32_t gap_start, gap_end; uint32_t buffer_data_size; void *mem_page; diff --git a/kernel/console/kconsole/terminal.cpp b/kernel/console/kconsole/terminal.cpp new file mode 100644 index 00000000..f63650e9 --- /dev/null +++ b/kernel/console/kconsole/terminal.cpp @@ -0,0 +1,20 @@ +#include "terminal.hpp" +#include "input/input_dispatch.h" +// #include "../kio.h" + +void Terminal::handle_input(){ + keypress kp; + if (sys_read_input_current(&kp)){ + for (int i = 0; i < 6; i++){ + char key = kp.keys[i]; + // kprintf("Key[%i] %i", i, key); + char readable = hid_to_char((uint8_t)key); + if (readable){ + put_char(readable); + draw_cursor(); + gpu_flush(); + } + } + } +} + diff --git a/kernel/console/kconsole/terminal.hpp b/kernel/console/kconsole/terminal.hpp new file mode 100644 index 00000000..0d2e9ded --- /dev/null +++ b/kernel/console/kconsole/terminal.hpp @@ -0,0 +1,9 @@ +#pragma once + +#include "kconsole.hpp" + +class Terminal: public KernelConsole { +public: + void handle_input(); + +}; \ No newline at end of file diff --git a/kernel/theme/RedactedOS.h b/kernel/theme/RedactedOS.h index 1da14ffb..a367a439 100644 --- a/kernel/theme/RedactedOS.h +++ b/kernel/theme/RedactedOS.h @@ -32,4 +32,7 @@ #define BG_COLOR 0xFF222233 -#define default_pwd "hi" \ No newline at end of file +#define default_pwd "hi" + +#define COLOR_WHITE 0xFFFFFFFF +#define COLOR_BLACK 0 \ No newline at end of file diff --git a/shared/ui/draw/draw.c b/shared/ui/draw/draw.c index 5348c88f..0e1322fb 100644 --- a/shared/ui/draw/draw.c +++ b/shared/ui/draw/draw.c @@ -107,15 +107,15 @@ gpu_rect fb_draw_line(uint32_t* fb, uint32_t x0, uint32_t y0, uint32_t x1, uint3 void fb_draw_char(uint32_t* fb, uint32_t x, uint32_t y, char c, uint32_t scale, uint32_t color){ const uint8_t* glyph = get_font8x8((uint8_t)c); - for (uint32_t row = 0; row < (8 * scale); row++) { + for (uint32_t row = 0; row < (CHAR_SIZE * scale); row++) { uint8_t bits = glyph[row/scale]; - for (uint32_t col = 0; col < (8 * scale); col++) { + for (uint32_t col = 0; col < (CHAR_SIZE * scale); col++) { if (bits & (1 << (7 - (col / scale)))) { fb_draw_pixel(fb, x + col, y + row, color); } } } - mark_dirty(x,y,8*scale,8*scale); + mark_dirty(x,y,CHAR_SIZE*scale,CHAR_SIZE*scale); } gpu_size fb_draw_string(uint32_t* fb, string s, uint32_t x0, uint32_t y0, uint32_t scale, uint32_t color){ @@ -150,7 +150,7 @@ gpu_size fb_draw_string(uint32_t* fb, string s, uint32_t x0, uint32_t y0, uint32 } uint32_t fb_get_char_size(uint32_t scale){ - return 8 * scale; + return CHAR_SIZE * scale; } void fb_set_stride(uint32_t new_stride){ diff --git a/shared/ui/draw/draw.h b/shared/ui/draw/draw.h index 0b949c8a..e441468a 100644 --- a/shared/ui/draw/draw.h +++ b/shared/ui/draw/draw.h @@ -6,6 +6,8 @@ extern "C" { #include "ui/graphic_types.h" #include "std/string.h" +#define CHAR_SIZE 8 + int try_merge(gpu_rect* a, gpu_rect* b); void mark_dirty(uint32_t x, uint32_t y, uint32_t w, uint32_t h); From b94b2e890e3237575db3693244c5c6ebd88c89eb Mon Sep 17 00:00:00 2001 From: Di Ferrari Date: Wed, 6 Aug 2025 00:00:00 +0000 Subject: [PATCH 19/50] [PROJ] fixed minor formatting --- kernel/graph/drivers/videocore/videocore.cpp | 3 +-- kernel/theme/RedactedOS.h | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/kernel/graph/drivers/videocore/videocore.cpp b/kernel/graph/drivers/videocore/videocore.cpp index d0c2aedd..ad836927 100644 --- a/kernel/graph/drivers/videocore/videocore.cpp +++ b/kernel/graph/drivers/videocore/videocore.cpp @@ -89,8 +89,7 @@ void VideoCoreGPUDriver::flush(){ uint32_t* src = (uint32_t*)&bfb[dest_y * (stride / 4) + r.point.x]; uint32_t copy_width = r.size.width; - if (r.point.x + copy_width > screen_size.width) - copy_width = screen_size.width - r.point.x; + if (r.point.x + copy_width > screen_size.width) copy_width = screen_size.width - r.point.x; memcpy(dst, src, copy_width * sizeof(uint32_t)); } diff --git a/kernel/theme/RedactedOS.h b/kernel/theme/RedactedOS.h index a367a439..b665c4a7 100644 --- a/kernel/theme/RedactedOS.h +++ b/kernel/theme/RedactedOS.h @@ -29,10 +29,9 @@ #define BOOTSCREEN_LOWER_Y_CONST 30 #define PANIC_TEXT "FATAL ERROR" +#define default_pwd "hi" #define BG_COLOR 0xFF222233 -#define default_pwd "hi" - #define COLOR_WHITE 0xFFFFFFFF #define COLOR_BLACK 0 \ No newline at end of file From fd96c697eff5fe72fbcd87dd9ca0949cfc2c8557 Mon Sep 17 00:00:00 2001 From: Di Ferrari Date: Wed, 6 Aug 2025 00:00:00 +0000 Subject: [PATCH 20/50] [CONSOLE] added backspace as a simple input --- kernel/console/kconsole/kconsole.cpp | 24 +++ kernel/console/kconsole/kconsole.hpp | 1 + kernel/console/kconsole/terminal.cpp | 6 +- shared/input_keycodes.h | 293 +++++++++++++++++++++++++-- 4 files changed, 311 insertions(+), 13 deletions(-) diff --git a/kernel/console/kconsole/kconsole.cpp b/kernel/console/kconsole/kconsole.cpp index 4c58ee98..f9eb2335 100644 --- a/kernel/console/kconsole/kconsole.cpp +++ b/kernel/console/kconsole/kconsole.cpp @@ -57,6 +57,30 @@ void KernelConsole::put_char(char c){ cursor_x++; } +void KernelConsole::delete_last_char(){ + if (cursor_x > 0){ + cursor_x--; + } else if (cursor_y > 0){ + cursor_y--; + cursor_x = 0; + char* line = row_data + cursor_y * columns; + while (*line){ + uart_putc(*line); + line++; + cursor_x++; + } + draw_cursor(); + gpu_flush(); + return; + } else return; + + char* line = row_data + cursor_y * columns; + line[cursor_x] = 0; + gpu_fill_rect({{cursor_x*char_width, cursor_y * line_height}, {char_width, line_height}}, COLOR_BLACK); + draw_cursor(); + gpu_flush(); +} + void KernelConsole::draw_cursor(){ if (last_drawn_cursor_x >= 0 && last_drawn_cursor_y >= 0){ gpu_fill_rect({{last_drawn_cursor_x*char_width, last_drawn_cursor_y * line_height}, {char_width, line_height}}, COLOR_BLACK); diff --git a/kernel/console/kconsole/kconsole.hpp b/kernel/console/kconsole/kconsole.hpp index f7bbd699..6530c6bc 100644 --- a/kernel/console/kconsole/kconsole.hpp +++ b/kernel/console/kconsole/kconsole.hpp @@ -23,6 +23,7 @@ class KernelConsole{ void refresh(); void set_active(bool active); + void delete_last_char(); protected: bool check_ready(); diff --git a/kernel/console/kconsole/terminal.cpp b/kernel/console/kconsole/terminal.cpp index f63650e9..ec12fb85 100644 --- a/kernel/console/kconsole/terminal.cpp +++ b/kernel/console/kconsole/terminal.cpp @@ -1,6 +1,7 @@ #include "terminal.hpp" #include "input/input_dispatch.h" -// #include "../kio.h" +#include "../kio.h" +#include "../serial/uart.h" void Terminal::handle_input(){ keypress kp; @@ -13,6 +14,9 @@ void Terminal::handle_input(){ put_char(readable); draw_cursor(); gpu_flush(); + } else if (key == KEY_BACKSPACE){ + uart_puts("Backspace"); + delete_last_char(); } } } diff --git a/shared/input_keycodes.h b/shared/input_keycodes.h index 80e693b6..be4a992c 100644 --- a/shared/input_keycodes.h +++ b/shared/input_keycodes.h @@ -1,17 +1,286 @@ +/** + * USB HID Keyboard scan codes as per USB spec 1.11 + * plus some additional codes + * + * Created by MightyPork, 2016 + * Public domain + * + * Adapted from: + * https://source.android.com/devices/input/keyboard-devices.html + */ #pragma once -#define KEY_ARROW_UP 0x52 -#define KEY_ARROW_DOWN 0x51 -#define KEY_ARROW_LEFT 0x50 -#define KEY_ARROW_RIGHT 0x4F -#define KEY_BACKSPACE 0x2A -#define KEY_ENTER 0x28 -#define KEY_KEYPAD_ENTER 0x58 -#define KEY_ESC 0x29 - -#define KEY_MOD_CMD 0x8 -#define KEY_MOD_ALT 0x4 -#define KEY_MOD_CTRL 0x1 +/** + * Modifier masks - used for the first byte in the HID report. + * NOTE: The second byte in the report is reserved, 0x00 + */ +#define KEY_MOD_LCTRL 0x01 +#define KEY_MOD_LSHIFT 0x02 +#define KEY_MOD_LALT 0x04 +#define KEY_MOD_LMETA 0x08 +#define KEY_MOD_RCTRL 0x10 +#define KEY_MOD_RSHIFT 0x20 +#define KEY_MOD_RALT 0x40 +#define KEY_MOD_RMETA 0x80 + +/** + * Scan codes - last N slots in the HID report (usually 6). + * 0x00 if no key pressed. + * + * If more than N keys are pressed, the HID reports + * KEY_ERR_OVF in all slots to indicate this condition. + */ + +#define KEY_NONE 0x00 // No key pressed +#define KEY_ERR_OVF 0x01 // Keyboard Error Roll Over - used for all slots if too many keys are pressed ("Phantom key") +// 0x02 // Keyboard POST Fail +// 0x03 // Keyboard Error Undefined +#define KEY_A 0x04 // Keyboard a and A +#define KEY_B 0x05 // Keyboard b and B +#define KEY_C 0x06 // Keyboard c and C +#define KEY_D 0x07 // Keyboard d and D +#define KEY_E 0x08 // Keyboard e and E +#define KEY_F 0x09 // Keyboard f and F +#define KEY_G 0x0a // Keyboard g and G +#define KEY_H 0x0b // Keyboard h and H +#define KEY_I 0x0c // Keyboard i and I +#define KEY_J 0x0d // Keyboard j and J +#define KEY_K 0x0e // Keyboard k and K +#define KEY_L 0x0f // Keyboard l and L +#define KEY_M 0x10 // Keyboard m and M +#define KEY_N 0x11 // Keyboard n and N +#define KEY_O 0x12 // Keyboard o and O +#define KEY_P 0x13 // Keyboard p and P +#define KEY_Q 0x14 // Keyboard q and Q +#define KEY_R 0x15 // Keyboard r and R +#define KEY_S 0x16 // Keyboard s and S +#define KEY_T 0x17 // Keyboard t and T +#define KEY_U 0x18 // Keyboard u and U +#define KEY_V 0x19 // Keyboard v and V +#define KEY_W 0x1a // Keyboard w and W +#define KEY_X 0x1b // Keyboard x and X +#define KEY_Y 0x1c // Keyboard y and Y +#define KEY_Z 0x1d // Keyboard z and Z + +#define KEY_1 0x1e // Keyboard 1 and ! +#define KEY_2 0x1f // Keyboard 2 and @ +#define KEY_3 0x20 // Keyboard 3 and # +#define KEY_4 0x21 // Keyboard 4 and $ +#define KEY_5 0x22 // Keyboard 5 and % +#define KEY_6 0x23 // Keyboard 6 and ^ +#define KEY_7 0x24 // Keyboard 7 and & +#define KEY_8 0x25 // Keyboard 8 and * +#define KEY_9 0x26 // Keyboard 9 and ( +#define KEY_0 0x27 // Keyboard 0 and ) + +#define KEY_ENTER 0x28 // Keyboard Return (ENTER) +#define KEY_ESC 0x29 // Keyboard ESCAPE +#define KEY_BACKSPACE 0x2a // Keyboard DELETE (Backspace) +#define KEY_TAB 0x2b // Keyboard Tab +#define KEY_SPACE 0x2c // Keyboard Spacebar +#define KEY_MINUS 0x2d // Keyboard - and _ +#define KEY_EQUAL 0x2e // Keyboard = and + +#define KEY_LEFTBRACE 0x2f // Keyboard [ and { +#define KEY_RIGHTBRACE 0x30 // Keyboard ] and } +#define KEY_BACKSLASH 0x31 // Keyboard \ and | +#define KEY_HASHTILDE 0x32 // Keyboard Non-US # and ~ +#define KEY_SEMICOLON 0x33 // Keyboard ; and : +#define KEY_APOSTROPHE 0x34 // Keyboard ' and " +#define KEY_GRAVE 0x35 // Keyboard ` and ~ +#define KEY_COMMA 0x36 // Keyboard , and < +#define KEY_DOT 0x37 // Keyboard . and > +#define KEY_SLASH 0x38 // Keyboard / and ? +#define KEY_CAPSLOCK 0x39 // Keyboard Caps Lock + +#define KEY_F1 0x3a // Keyboard F1 +#define KEY_F2 0x3b // Keyboard F2 +#define KEY_F3 0x3c // Keyboard F3 +#define KEY_F4 0x3d // Keyboard F4 +#define KEY_F5 0x3e // Keyboard F5 +#define KEY_F6 0x3f // Keyboard F6 +#define KEY_F7 0x40 // Keyboard F7 +#define KEY_F8 0x41 // Keyboard F8 +#define KEY_F9 0x42 // Keyboard F9 +#define KEY_F10 0x43 // Keyboard F10 +#define KEY_F11 0x44 // Keyboard F11 +#define KEY_F12 0x45 // Keyboard F12 + +#define KEY_SYSRQ 0x46 // Keyboard Print Screen +#define KEY_SCROLLLOCK 0x47 // Keyboard Scroll Lock +#define KEY_PAUSE 0x48 // Keyboard Pause +#define KEY_INSERT 0x49 // Keyboard Insert +#define KEY_HOME 0x4a // Keyboard Home +#define KEY_PAGEUP 0x4b // Keyboard Page Up +#define KEY_DELETE 0x4c // Keyboard Delete Forward +#define KEY_END 0x4d // Keyboard End +#define KEY_PAGEDOWN 0x4e // Keyboard Page Down +#define KEY_RIGHT 0x4f // Keyboard Right Arrow +#define KEY_LEFT 0x50 // Keyboard Left Arrow +#define KEY_DOWN 0x51 // Keyboard Down Arrow +#define KEY_UP 0x52 // Keyboard Up Arrow + +#define KEY_NUMLOCK 0x53 // Keyboard Num Lock and Clear +#define KEY_KPSLASH 0x54 // Keypad / +#define KEY_KPASTERISK 0x55 // Keypad * +#define KEY_KPMINUS 0x56 // Keypad - +#define KEY_KPPLUS 0x57 // Keypad + +#define KEY_KPENTER 0x58 // Keypad ENTER +#define KEY_KP1 0x59 // Keypad 1 and End +#define KEY_KP2 0x5a // Keypad 2 and Down Arrow +#define KEY_KP3 0x5b // Keypad 3 and PageDn +#define KEY_KP4 0x5c // Keypad 4 and Left Arrow +#define KEY_KP5 0x5d // Keypad 5 +#define KEY_KP6 0x5e // Keypad 6 and Right Arrow +#define KEY_KP7 0x5f // Keypad 7 and Home +#define KEY_KP8 0x60 // Keypad 8 and Up Arrow +#define KEY_KP9 0x61 // Keypad 9 and Page Up +#define KEY_KP0 0x62 // Keypad 0 and Insert +#define KEY_KPDOT 0x63 // Keypad . and Delete + +#define KEY_102ND 0x64 // Keyboard Non-US \ and | +#define KEY_COMPOSE 0x65 // Keyboard Application +#define KEY_POWER 0x66 // Keyboard Power +#define KEY_KPEQUAL 0x67 // Keypad = + +#define KEY_F13 0x68 // Keyboard F13 +#define KEY_F14 0x69 // Keyboard F14 +#define KEY_F15 0x6a // Keyboard F15 +#define KEY_F16 0x6b // Keyboard F16 +#define KEY_F17 0x6c // Keyboard F17 +#define KEY_F18 0x6d // Keyboard F18 +#define KEY_F19 0x6e // Keyboard F19 +#define KEY_F20 0x6f // Keyboard F20 +#define KEY_F21 0x70 // Keyboard F21 +#define KEY_F22 0x71 // Keyboard F22 +#define KEY_F23 0x72 // Keyboard F23 +#define KEY_F24 0x73 // Keyboard F24 + +#define KEY_OPEN 0x74 // Keyboard Execute +#define KEY_HELP 0x75 // Keyboard Help +#define KEY_PROPS 0x76 // Keyboard Menu +#define KEY_FRONT 0x77 // Keyboard Select +#define KEY_STOP 0x78 // Keyboard Stop +#define KEY_AGAIN 0x79 // Keyboard Again +#define KEY_UNDO 0x7a // Keyboard Undo +#define KEY_CUT 0x7b // Keyboard Cut +#define KEY_COPY 0x7c // Keyboard Copy +#define KEY_PASTE 0x7d // Keyboard Paste +#define KEY_FIND 0x7e // Keyboard Find +#define KEY_MUTE 0x7f // Keyboard Mute +#define KEY_VOLUMEUP 0x80 // Keyboard Volume Up +#define KEY_VOLUMEDOWN 0x81 // Keyboard Volume Down +// 0x82 Keyboard Locking Caps Lock +// 0x83 Keyboard Locking Num Lock +// 0x84 Keyboard Locking Scroll Lock +#define KEY_KPCOMMA 0x85 // Keypad Comma +// 0x86 Keypad Equal Sign +#define KEY_RO 0x87 // Keyboard International1 +#define KEY_KATAKANAHIRAGANA 0x88 // Keyboard International2 +#define KEY_YEN 0x89 // Keyboard International3 +#define KEY_HENKAN 0x8a // Keyboard International4 +#define KEY_MUHENKAN 0x8b // Keyboard International5 +#define KEY_KPJPCOMMA 0x8c // Keyboard International6 +// 0x8d Keyboard International7 +// 0x8e Keyboard International8 +// 0x8f Keyboard International9 +#define KEY_HANGEUL 0x90 // Keyboard LANG1 +#define KEY_HANJA 0x91 // Keyboard LANG2 +#define KEY_KATAKANA 0x92 // Keyboard LANG3 +#define KEY_HIRAGANA 0x93 // Keyboard LANG4 +#define KEY_ZENKAKUHANKAKU 0x94 // Keyboard LANG5 +// 0x95 Keyboard LANG6 +// 0x96 Keyboard LANG7 +// 0x97 Keyboard LANG8 +// 0x98 Keyboard LANG9 +// 0x99 Keyboard Alternate Erase +// 0x9a Keyboard SysReq/Attention +// 0x9b Keyboard Cancel +// 0x9c Keyboard Clear +// 0x9d Keyboard Prior +// 0x9e Keyboard Return +// 0x9f Keyboard Separator +// 0xa0 Keyboard Out +// 0xa1 Keyboard Oper +// 0xa2 Keyboard Clear/Again +// 0xa3 Keyboard CrSel/Props +// 0xa4 Keyboard ExSel + +// 0xb0 Keypad 00 +// 0xb1 Keypad 000 +// 0xb2 Thousands Separator +// 0xb3 Decimal Separator +// 0xb4 Currency Unit +// 0xb5 Currency Sub-unit +#define KEY_KPLEFTPAREN 0xb6 // Keypad ( +#define KEY_KPRIGHTPAREN 0xb7 // Keypad ) +// 0xb8 Keypad { +// 0xb9 Keypad } +// 0xba Keypad Tab +// 0xbb Keypad Backspace +// 0xbc Keypad A +// 0xbd Keypad B +// 0xbe Keypad C +// 0xbf Keypad D +// 0xc0 Keypad E +// 0xc1 Keypad F +// 0xc2 Keypad XOR +// 0xc3 Keypad ^ +// 0xc4 Keypad % +// 0xc5 Keypad < +// 0xc6 Keypad > +// 0xc7 Keypad & +// 0xc8 Keypad && +// 0xc9 Keypad | +// 0xca Keypad || +// 0xcb Keypad : +// 0xcc Keypad # +// 0xcd Keypad Space +// 0xce Keypad @ +// 0xcf Keypad ! +// 0xd0 Keypad Memory Store +// 0xd1 Keypad Memory Recall +// 0xd2 Keypad Memory Clear +// 0xd3 Keypad Memory Add +// 0xd4 Keypad Memory Subtract +// 0xd5 Keypad Memory Multiply +// 0xd6 Keypad Memory Divide +// 0xd7 Keypad +/- +// 0xd8 Keypad Clear +// 0xd9 Keypad Clear Entry +// 0xda Keypad Binary +// 0xdb Keypad Octal +// 0xdc Keypad Decimal +// 0xdd Keypad Hexadecimal + +#define KEY_LEFTCTRL 0xe0 // Keyboard Left Control +#define KEY_LEFTSHIFT 0xe1 // Keyboard Left Shift +#define KEY_LEFTALT 0xe2 // Keyboard Left Alt +#define KEY_LEFTMETA 0xe3 // Keyboard Left GUI +#define KEY_RIGHTCTRL 0xe4 // Keyboard Right Control +#define KEY_RIGHTSHIFT 0xe5 // Keyboard Right Shift +#define KEY_RIGHTALT 0xe6 // Keyboard Right Alt +#define KEY_RIGHTMETA 0xe7 // Keyboard Right GUI + +#define KEY_MEDIA_PLAYPAUSE 0xe8 +#define KEY_MEDIA_STOPCD 0xe9 +#define KEY_MEDIA_PREVIOUSSONG 0xea +#define KEY_MEDIA_NEXTSONG 0xeb +#define KEY_MEDIA_EJECTCD 0xec +#define KEY_MEDIA_VOLUMEUP 0xed +#define KEY_MEDIA_VOLUMEDOWN 0xee +#define KEY_MEDIA_MUTE 0xef +#define KEY_MEDIA_WWW 0xf0 +#define KEY_MEDIA_BACK 0xf1 +#define KEY_MEDIA_FORWARD 0xf2 +#define KEY_MEDIA_STOP 0xf3 +#define KEY_MEDIA_FIND 0xf4 +#define KEY_MEDIA_SCROLLUP 0xf5 +#define KEY_MEDIA_SCROLLDOWN 0xf6 +#define KEY_MEDIA_EDIT 0xf7 +#define KEY_MEDIA_SLEEP 0xf8 +#define KEY_MEDIA_COFFEE 0xf9 +#define KEY_MEDIA_REFRESH 0xfa +#define KEY_MEDIA_CALC 0xfb #ifndef __cplusplus //TODO: properly handle keypad From 98259c0878e9cdeba521e68f7406316751f29e1a Mon Sep 17 00:00:00 2001 From: Di Ferrari Date: Fri, 8 Aug 2025 00:00:00 +0000 Subject: [PATCH 21/50] [INPUT] fixed keys --- kernel/console/kconsole/console.cpp | 2 +- kernel/console/kconsole/kconsole.cpp | 1 + kernel/kernel_processes/boot/login_screen.c | 2 +- kernel/kernel_processes/monitor/monitor_processes.c | 6 +++--- kernel/kernel_processes/windows/desktop.cpp | 10 +++++----- 5 files changed, 11 insertions(+), 10 deletions(-) diff --git a/kernel/console/kconsole/console.cpp b/kernel/console/kconsole/console.cpp index e1ce78a6..44c78004 100644 --- a/kernel/console/kconsole/console.cpp +++ b/kernel/console/kconsole/console.cpp @@ -22,7 +22,7 @@ extern "C" void kconsole_clear() { extern "C" void toggle_visual(){ keypress kp = { - .modifier = KEY_MOD_ALT, + .modifier = KEY_MOD_LALT, .rsvd = 0, .keys = {0x13}, }; diff --git a/kernel/console/kconsole/kconsole.cpp b/kernel/console/kconsole/kconsole.cpp index f9eb2335..0fa64a63 100644 --- a/kernel/console/kconsole/kconsole.cpp +++ b/kernel/console/kconsole/kconsole.cpp @@ -57,6 +57,7 @@ void KernelConsole::put_char(char c){ cursor_x++; } +//TODO: generalize this function to handle movement in general void KernelConsole::delete_last_char(){ if (cursor_x > 0){ cursor_x--; diff --git a/kernel/kernel_processes/boot/login_screen.c b/kernel/kernel_processes/boot/login_screen.c index 1e4fb2b7..a7e40870 100644 --- a/kernel/kernel_processes/boot/login_screen.c +++ b/kernel/kernel_processes/boot/login_screen.c @@ -42,7 +42,7 @@ void login_screen(){ for (int i = 0; i < 6; i++){ char key = kp.keys[i]; if (hid_keycode_to_char[(uint8_t)key]){ - if (key == KEY_ENTER || key == KEY_KEYPAD_ENTER){ + if (key == KEY_ENTER || key == KEY_KPENTER){ if (strcmp(buf,default_pwd, false) == 0){ free(buf, 256); free(s.data,s.mem_length); diff --git a/kernel/kernel_processes/monitor/monitor_processes.c b/kernel/kernel_processes/monitor/monitor_processes.c index bcdb151f..42b9fb66 100644 --- a/kernel/kernel_processes/monitor/monitor_processes.c +++ b/kernel/kernel_processes/monitor/monitor_processes.c @@ -86,9 +86,9 @@ void draw_process_view(){ keypress kp; while (sys_read_input_current(&kp)){ - if (kp.keys[0] == KEY_ARROW_LEFT) + if (kp.keys[0] == KEY_LEFT) scroll_index = max(scroll_index - 1, 0); - if (kp.keys[0] == KEY_ARROW_RIGHT) + if (kp.keys[0] == KEY_RIGHT) scroll_index = min(scroll_index + 1,MAX_PROCS); } @@ -151,7 +151,7 @@ void draw_process_view(){ __attribute__((section(".text.kcoreprocesses"))) void monitor_procs(){ keypress kp = { - .modifier = KEY_MOD_ALT, + .modifier = KEY_MOD_LALT, .keys[0] = 0x15//R }; uint16_t shortcut = sys_subscribe_shortcut_current(kp); diff --git a/kernel/kernel_processes/windows/desktop.cpp b/kernel/kernel_processes/windows/desktop.cpp index d7aa1bc5..dd64099c 100644 --- a/kernel/kernel_processes/windows/desktop.cpp +++ b/kernel/kernel_processes/windows/desktop.cpp @@ -62,20 +62,20 @@ void Desktop::draw_desktop(){ while (sys_read_input_current(&kp)){ for (int i = 0; i < 6; i++){ char key = kp.keys[i]; - if (key == KEY_ENTER || key == KEY_KEYPAD_ENTER){ + if (key == KEY_ENTER || key == KEY_KPENTER){ activate_current(); return; } - if (key == KEY_ARROW_RIGHT){ + if (key == KEY_RIGHT){ selected.x = (selected.x + 1) % MAX_COLS; } - if (key == KEY_ARROW_LEFT){ + if (key == KEY_LEFT){ selected.x = (selected.x - 1 + MAX_COLS) % MAX_COLS; } - if (key == KEY_ARROW_DOWN){ + if (key == KEY_DOWN){ selected.y = (selected.y + 1) % MAX_ROWS; } - if (key == KEY_ARROW_UP){ + if (key == KEY_UP){ selected.y = (selected.y - 1 + MAX_ROWS) % MAX_ROWS; } } From 1f55ca402c13214c045039ca9759fa5b7dd53f3a Mon Sep 17 00:00:00 2001 From: Di Ferrari Date: Fri, 8 Aug 2025 00:00:00 +0000 Subject: [PATCH 22/50] [CONSOLE] removed ring buffer in console --- kernel/console/kconsole/kconsole.cpp | 44 ++++++++++------------------ kernel/console/kconsole/kconsole.hpp | 2 +- 2 files changed, 16 insertions(+), 30 deletions(-) diff --git a/kernel/console/kconsole/kconsole.cpp b/kernel/console/kconsole/kconsole.cpp index 0fa64a63..50337c03 100644 --- a/kernel/console/kconsole/kconsole.cpp +++ b/kernel/console/kconsole/kconsole.cpp @@ -3,6 +3,7 @@ #include "memory/page_allocator.h" #include "filesystem/filesystem.h" #include "theme/theme.h" +#include "std/memfunctions.h" KernelConsole::KernelConsole() : cursor_x(0), cursor_y(0), is_initialized(false){ initialize(); @@ -33,12 +34,10 @@ void KernelConsole::resize(){ row_data = (char*)kalloc(mem_page, buffer_data_size, ALIGN_16B, true, true); if (!row_data){ rows = columns = 0; - row_ring.clear(); return; } - row_ring.clear(); - for (uint32_t i = 0; i < rows; i++) row_ring.push(i); + scroll_row_offset = 0; } void KernelConsole::put_char(char c){ @@ -50,7 +49,7 @@ void KernelConsole::put_char(char c){ } if (cursor_x >= columns) newline(); - uint32_t row_index = row_ring.peek(); + uint32_t row_index = (scroll_row_offset + cursor_y) % rows; char* line = row_data + row_index * columns; line[cursor_x] = c; gpu_draw_char({cursor_x * char_width, (cursor_y * line_height)+(line_height/2)}, c, 1, COLOR_WHITE); @@ -106,12 +105,9 @@ void KernelConsole::put_string(const char* str){ void KernelConsole::newline(){ if (!check_ready()) return; - uint32_t row_index; - if (row_ring.pop(row_index)){ - row_ring.push(row_index); - char* line = row_data + row_index * columns; - for (uint32_t x = cursor_x; x < columns; x++) line[x] = 0; - } + uint32_t row_index = (scroll_row_offset + cursor_y) % rows; + char* line = row_data + row_index * columns; + for (uint32_t x = cursor_x; x < columns; x++) line[x] = 0; cursor_x = 0; cursor_y++; if (cursor_y >= rows - 1){ @@ -122,10 +118,10 @@ void KernelConsole::newline(){ void KernelConsole::scroll(){ if (!check_ready()) return; - if (uint32_t row_index = row_ring.peek()){ - char* line = row_data + row_index * columns; - for (uint32_t x = 0; x < columns; x++) line[x] = 0; - } + scroll_row_offset = (scroll_row_offset + 1) % rows; + uint32_t row_index = (scroll_row_offset + cursor_y) % rows; + char* line = row_data + row_index * columns; + for (uint32_t x = 0; x < columns; x++) line[x] = 0; redraw(); } @@ -139,13 +135,10 @@ void KernelConsole::refresh(){ void KernelConsole::redraw(){ screen_clear(); for (uint32_t y = 0; y < rows; y++){ - uint32_t row_index; - if (row_ring.pop(row_index)){ - row_ring.push(row_index); - char* line = row_data + row_index * columns; - for (uint32_t x = 0; x < columns; x++){ - gpu_draw_char({x * char_width, (y * line_height)+(line_height/2)}, line[x], 1, COLOR_WHITE); - } + uint32_t row_index = (scroll_row_offset + y) % rows; + char* line = row_data + row_index * columns; + for (uint32_t x = 0; x < columns; x++){ + gpu_draw_char({x * char_width, (y * line_height)+(line_height/2)}, line[x], 1, COLOR_WHITE); } } draw_cursor(); @@ -159,13 +152,6 @@ void KernelConsole::screen_clear(){ void KernelConsole::clear(){ screen_clear(); - for (uint32_t i = 0; i < rows; i++){ - uint32_t row_index; - if (row_ring.pop(row_index)){ - char* line = row_data + row_index * columns; - for (uint32_t x = 0; x < columns; x++) line[x] = 0; - row_ring.push(row_index); - } - } + memset(row_data, 0, buffer_data_size); cursor_x = cursor_y = 0; } diff --git a/kernel/console/kconsole/kconsole.hpp b/kernel/console/kconsole/kconsole.hpp index bcff9901..1c904422 100644 --- a/kernel/console/kconsole/kconsole.hpp +++ b/kernel/console/kconsole/kconsole.hpp @@ -40,7 +40,7 @@ class KernelConsole{ static constexpr uint32_t line_height=CHAR_SIZE*2; static constexpr uint32_t max_rows=128; - RingBuffer row_ring; + int32_t scroll_row_offset = 0; char* row_data; uint32_t gap_start, gap_end; uint32_t buffer_data_size; From 565f7a1b6369f495f95674618cdbbd4ef2a6fd74 Mon Sep 17 00:00:00 2001 From: Di Ferrari Date: Fri, 8 Aug 2025 00:00:00 +0000 Subject: [PATCH 23/50] [CONSOLE, TMP] added cat command (temporarily in terminal) --- kernel/console/kconsole/console.cpp | 2 +- kernel/console/kconsole/kconsole.cpp | 7 ++- kernel/console/kconsole/kconsole.hpp | 1 + kernel/console/kconsole/terminal.cpp | 68 ++++++++++++++++++++++++++-- kernel/console/kconsole/terminal.hpp | 9 ++++ kernel/console/kio.c | 6 ++- kernel/filesystem/filesystem.cpp | 7 ++- shared/std/std.hpp | 3 +- shared/std/string.c | 2 +- 9 files changed, 93 insertions(+), 12 deletions(-) diff --git a/kernel/console/kconsole/console.cpp b/kernel/console/kconsole/console.cpp index 44c78004..270c0053 100644 --- a/kernel/console/kconsole/console.cpp +++ b/kernel/console/kconsole/console.cpp @@ -43,7 +43,7 @@ extern "C" void toggle_visual(){ } } if (active){ - terminal->handle_input(); + terminal->update(); } } } diff --git a/kernel/console/kconsole/kconsole.cpp b/kernel/console/kconsole/kconsole.cpp index 50337c03..646c706c 100644 --- a/kernel/console/kconsole/kconsole.cpp +++ b/kernel/console/kconsole/kconsole.cpp @@ -65,7 +65,6 @@ void KernelConsole::delete_last_char(){ cursor_x = 0; char* line = row_data + cursor_y * columns; while (*line){ - uart_putc(*line); line++; cursor_x++; } @@ -85,7 +84,6 @@ void KernelConsole::draw_cursor(){ if (last_drawn_cursor_x >= 0 && last_drawn_cursor_y >= 0){ gpu_fill_rect({{last_drawn_cursor_x*char_width, last_drawn_cursor_y * line_height}, {char_width, line_height}}, COLOR_BLACK); char *line = row_data + (last_drawn_cursor_y * columns); - uart_putc(line[last_drawn_cursor_x]); gpu_draw_char({last_drawn_cursor_x * char_width, (last_drawn_cursor_y * line_height)+(line_height/2)}, line[last_drawn_cursor_x], 1, COLOR_WHITE); } gpu_fill_rect({{cursor_x*char_width, cursor_y * line_height}, {char_width, line_height}}, COLOR_WHITE); @@ -155,3 +153,8 @@ void KernelConsole::clear(){ memset(row_data, 0, buffer_data_size); cursor_x = cursor_y = 0; } + +const char* KernelConsole::get_current_line(){ + uint32_t row_index = (scroll_row_offset + cursor_y) % rows; + return row_data + row_index * columns; +} \ No newline at end of file diff --git a/kernel/console/kconsole/kconsole.hpp b/kernel/console/kconsole/kconsole.hpp index 1c904422..540960f9 100644 --- a/kernel/console/kconsole/kconsole.hpp +++ b/kernel/console/kconsole/kconsole.hpp @@ -30,6 +30,7 @@ class KernelConsole{ void screen_clear(); void redraw(); void draw_cursor(); + const char* get_current_line(); uint32_t cursor_x, cursor_y; int32_t last_drawn_cursor_x, last_drawn_cursor_y; diff --git a/kernel/console/kconsole/terminal.cpp b/kernel/console/kconsole/terminal.cpp index ec12fb85..44228c5e 100644 --- a/kernel/console/kconsole/terminal.cpp +++ b/kernel/console/kconsole/terminal.cpp @@ -2,20 +2,82 @@ #include "input/input_dispatch.h" #include "../kio.h" #include "../serial/uart.h" +#include "std/std.hpp" +#include "filesystem/filesystem.h" + +void Terminal::update(){ + if (!command_running) handle_input(); + else { + end_command(); + } +} + +void Terminal::end_command(){ + command_running = false; + put_char('\r'); + put_char('\n'); + draw_cursor(); + gpu_flush(); +} + +const char* Terminal::seek_to(const char *string, char character){ + while (*string != character && *string != '\0') + string++; + string++; + return string; +} + +void Terminal::TMP_cat(const char *args){ + file fd; + if (open_file(args, &fd) != FS_RESULT_SUCCESS) { + string s = string_format("Path not found %s", args); + put_string(s.data); + free(s.data,s.mem_length); + return; + } + size_t req_size = 0x100; + char* buf = (char*)malloc(req_size); + if (read_file(&fd, buf, req_size) == 0){ + put_string("Error reading file"); + return; + } + put_string(buf); +} + +void Terminal::run_command(){ + const char* fullcmd = get_current_line(); + const char* args = seek_to(fullcmd, ' '); + string cmd = string_ca_max(fullcmd, args - fullcmd - 1); + string s = string_format("Executing command %s with args %s", cmd.data, args); + + put_char('\r'); + put_char('\n'); + + if (strcmp(cmd.data, "cat", true) == 0){ + TMP_cat(args); + } else put_string(s.data); + + free(s.data, s.mem_length); + free(cmd.data, cmd.mem_length); + + draw_cursor(); + gpu_flush(); + command_running = true; +} void Terminal::handle_input(){ keypress kp; if (sys_read_input_current(&kp)){ for (int i = 0; i < 6; i++){ char key = kp.keys[i]; - // kprintf("Key[%i] %i", i, key); char readable = hid_to_char((uint8_t)key); - if (readable){ + if (key == KEY_ENTER || key == KEY_KPENTER){ + run_command(); + } else if (readable){ put_char(readable); draw_cursor(); gpu_flush(); } else if (key == KEY_BACKSPACE){ - uart_puts("Backspace"); delete_last_char(); } } diff --git a/kernel/console/kconsole/terminal.hpp b/kernel/console/kconsole/terminal.hpp index 0d2e9ded..e3015ebe 100644 --- a/kernel/console/kconsole/terminal.hpp +++ b/kernel/console/kconsole/terminal.hpp @@ -4,6 +4,15 @@ class Terminal: public KernelConsole { public: + void update(); +protected: void handle_input(); + void end_command(); + void run_command(); + const char* seek_to(const char *string, char character); + //TODO: proper commands + void TMP_cat(const char *args); + + bool command_running; }; \ No newline at end of file diff --git a/kernel/console/kio.c b/kernel/console/kio.c index 70512750..293cd231 100644 --- a/kernel/console/kio.c +++ b/kernel/console/kio.c @@ -39,8 +39,9 @@ FS_RESULT console_open(const char *path, file *out_fd){ } size_t console_read(file *fd, char *out_buf, size_t size, file_offset offset){ - memcpy(out_buf, print_buf+offset, min(size,CONSOLE_BUF_SIZE)); - return 0; + size = min(size,CONSOLE_BUF_SIZE); + memcpy(out_buf, print_buf+offset, size); + return size; } size_t console_write(file *fd, const char *buf, size_t size, file_offset offset){ @@ -82,6 +83,7 @@ void putc(const char c){ kconsole_putc(c); } +//TODO: __attribute__((format)) void kprintf(const char *fmt, ...){ if (!print_buf) init_print_buf(); __attribute__((aligned(16))) va_list args; diff --git a/kernel/filesystem/filesystem.cpp b/kernel/filesystem/filesystem.cpp index 192857db..bf33cba0 100644 --- a/kernel/filesystem/filesystem.cpp +++ b/kernel/filesystem/filesystem.cpp @@ -76,7 +76,7 @@ bool init_boot_filesystem(){ FS_RESULT open_file(const char* path, file* descriptor){ const char *search_path = path; driver_module *mod = get_module(&search_path); - kprintf("Got module %x for path %s",(uintptr_t)mod,(uintptr_t)search_path); + kprintf("Got module %x for path %s",(uintptr_t)mod,search_path); if (!mod) return FS_RESULT_NOTFOUND; FS_RESULT result = mod->open(search_path, descriptor); if (!open_files) @@ -89,7 +89,10 @@ FS_RESULT open_file(const char* path, file* descriptor){ } size_t read_file(file *descriptor, char* buf, size_t size){ - if (!open_files) return 0; + if (!open_files){ + kprintf("[FS] No open files"); + return 0; + } driver_module *mod = open_files->find([descriptor](file_mod_kvp kvp){ return descriptor->id == kvp.file_id; })->data.mod; diff --git a/shared/std/std.hpp b/shared/std/std.hpp index 2beffcfc..80a04a5a 100644 --- a/shared/std/std.hpp +++ b/shared/std/std.hpp @@ -1,4 +1,5 @@ #pragma once #include "allocator.hpp" -#include "data_struct/data_struct.hpp" \ No newline at end of file +#include "data_struct/data_struct.hpp" +#include "../syscalls/syscalls.h" \ No newline at end of file diff --git a/shared/std/string.c b/shared/std/string.c index e0ea4036..27267992 100644 --- a/shared/std/string.c +++ b/shared/std/string.c @@ -160,7 +160,7 @@ size_t string_format_va_buf(const char *fmt, char *buf, va_list args){ buf[len++] = (char)val; } else if (fmt[i] == 's') { - char *str = ( char *)va_arg(args, uintptr_t); + char *str = ( char *)va_arg(args, uintptr_t); for (uint32_t j = 0; str[j] && len < 255; j++) buf[len++] = str[j]; } else if (fmt[i] == 'i') { From 48be0b2f4d67e4557435b5d20b10ea2763f14f01 Mon Sep 17 00:00:00 2001 From: Di Ferrari Date: Fri, 8 Aug 2025 00:00:00 +0000 Subject: [PATCH 24/50] [FS] keeping track of file size --- kernel/filesystem/filesystem.cpp | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/kernel/filesystem/filesystem.cpp b/kernel/filesystem/filesystem.cpp index bf33cba0..6f2b9c94 100644 --- a/kernel/filesystem/filesystem.cpp +++ b/kernel/filesystem/filesystem.cpp @@ -12,10 +12,11 @@ FAT32FS *fs_driver; typedef struct { uint64_t file_id; + size_t file_size; driver_module* mod; -} file_mod_kvp; +} open_file_descriptors; -LinkedList *open_files; +LinkedList *open_files; bool boot_partition_init(){ uint32_t f32_partition = mbr_find_partition(0xC); @@ -80,9 +81,10 @@ FS_RESULT open_file(const char* path, file* descriptor){ if (!mod) return FS_RESULT_NOTFOUND; FS_RESULT result = mod->open(search_path, descriptor); if (!open_files) - open_files = new LinkedList(); + open_files = new LinkedList(); open_files->push_front({ .file_id = descriptor->id, + .file_size = descriptor->size, .mod = mod }); return result; @@ -93,11 +95,11 @@ size_t read_file(file *descriptor, char* buf, size_t size){ kprintf("[FS] No open files"); return 0; } - driver_module *mod = open_files->find([descriptor](file_mod_kvp kvp){ + open_file_descriptors file = open_files->find([descriptor](open_file_descriptors kvp){ return descriptor->id == kvp.file_id; - })->data.mod; - size_t adj_size = min(size,descriptor->size);//TODO: still possible to modify the fd's size - return mod->read(descriptor, buf, adj_size, 0); + })->data; + size_t adj_size = min(size,file.file_size);//TODO: still possible to modify the fd's size + return file.mod->read(descriptor, buf, adj_size, 0); } sizedptr list_directory_contents(const char *path){ From 1f07562c1506f025b94328e385b35927d5ae7194 Mon Sep 17 00:00:00 2001 From: Di Ferrari Date: Fri, 8 Aug 2025 00:00:00 +0000 Subject: [PATCH 25/50] [CONSOLE] added non-stantard text colors --- kernel/console/kconsole/kconsole.cpp | 12 +++++++++--- kernel/console/kconsole/kconsole.hpp | 5 +++++ kernel/console/kconsole/terminal.cpp | 27 ++++++++++++++++++--------- kernel/console/kconsole/terminal.hpp | 1 + kernel/filesystem/fat32.cpp | 1 + shared/std/string.c | 9 ++++++++- shared/std/string.h | 3 ++- 7 files changed, 44 insertions(+), 14 deletions(-) diff --git a/kernel/console/kconsole/kconsole.cpp b/kernel/console/kconsole/kconsole.cpp index 646c706c..c458cf17 100644 --- a/kernel/console/kconsole/kconsole.cpp +++ b/kernel/console/kconsole/kconsole.cpp @@ -14,6 +14,8 @@ void KernelConsole::initialize(){ mem_page = palloc(PAGE_SIZE, true, true, false); resize(); clear(); + default_text_color = 0xFFFFFFFF; + text_color = default_text_color; } bool KernelConsole::check_ready(){ @@ -52,7 +54,7 @@ void KernelConsole::put_char(char c){ uint32_t row_index = (scroll_row_offset + cursor_y) % rows; char* line = row_data + row_index * columns; line[cursor_x] = c; - gpu_draw_char({cursor_x * char_width, (cursor_y * line_height)+(line_height/2)}, c, 1, COLOR_WHITE); + gpu_draw_char({cursor_x * char_width, (cursor_y * line_height)+(line_height/2)}, c, 1, text_color); cursor_x++; } @@ -84,7 +86,7 @@ void KernelConsole::draw_cursor(){ if (last_drawn_cursor_x >= 0 && last_drawn_cursor_y >= 0){ gpu_fill_rect({{last_drawn_cursor_x*char_width, last_drawn_cursor_y * line_height}, {char_width, line_height}}, COLOR_BLACK); char *line = row_data + (last_drawn_cursor_y * columns); - gpu_draw_char({last_drawn_cursor_x * char_width, (last_drawn_cursor_y * line_height)+(line_height/2)}, line[last_drawn_cursor_x], 1, COLOR_WHITE); + gpu_draw_char({last_drawn_cursor_x * char_width, (last_drawn_cursor_y * line_height)+(line_height/2)}, line[last_drawn_cursor_x], 1, text_color); } gpu_fill_rect({{cursor_x*char_width, cursor_y * line_height}, {char_width, line_height}}, COLOR_WHITE); last_drawn_cursor_x = cursor_x; @@ -136,7 +138,7 @@ void KernelConsole::redraw(){ uint32_t row_index = (scroll_row_offset + y) % rows; char* line = row_data + row_index * columns; for (uint32_t x = 0; x < columns; x++){ - gpu_draw_char({x * char_width, (y * line_height)+(line_height/2)}, line[x], 1, COLOR_WHITE); + gpu_draw_char({x * char_width, (y * line_height)+(line_height/2)}, line[x], 1, text_color); } } draw_cursor(); @@ -157,4 +159,8 @@ void KernelConsole::clear(){ const char* KernelConsole::get_current_line(){ uint32_t row_index = (scroll_row_offset + cursor_y) % rows; return row_data + row_index * columns; +} + +void KernelConsole::set_text_color(uint32_t hex){ + text_color = hex | 0xFF000000; } \ No newline at end of file diff --git a/kernel/console/kconsole/kconsole.hpp b/kernel/console/kconsole/kconsole.hpp index 540960f9..42cb5b9b 100644 --- a/kernel/console/kconsole/kconsole.hpp +++ b/kernel/console/kconsole/kconsole.hpp @@ -32,6 +32,8 @@ class KernelConsole{ void draw_cursor(); const char* get_current_line(); + void set_text_color(uint32_t color); + uint32_t cursor_x, cursor_y; int32_t last_drawn_cursor_x, last_drawn_cursor_y; uint32_t columns, rows; @@ -41,6 +43,9 @@ class KernelConsole{ static constexpr uint32_t line_height=CHAR_SIZE*2; static constexpr uint32_t max_rows=128; + uint32_t default_text_color = 0xFFFFFFFF; + uint32_t text_color = default_text_color; + int32_t scroll_row_offset = 0; char* row_data; uint32_t gap_start, gap_end; diff --git a/kernel/console/kconsole/terminal.cpp b/kernel/console/kconsole/terminal.cpp index 44228c5e..3b531fb6 100644 --- a/kernel/console/kconsole/terminal.cpp +++ b/kernel/console/kconsole/terminal.cpp @@ -18,13 +18,7 @@ void Terminal::end_command(){ put_char('\n'); draw_cursor(); gpu_flush(); -} - -const char* Terminal::seek_to(const char *string, char character){ - while (*string != character && *string != '\0') - string++; - string++; - return string; + set_text_color(default_text_color); } void Terminal::TMP_cat(const char *args){ @@ -53,9 +47,11 @@ void Terminal::run_command(){ put_char('\r'); put_char('\n'); - if (strcmp(cmd.data, "cat", true) == 0){ + if (strcmp(cmd.data, "cat", true) == 0) TMP_cat(args); - } else put_string(s.data); + if (strcmp(cmd.data, "test", true) == 0) + TMP_test(args); + else put_string(s.data); free(s.data, s.mem_length); free(cmd.data, cmd.mem_length); @@ -65,6 +61,19 @@ void Terminal::run_command(){ command_running = true; } +//TODO: implement the full state machine explained at https://vt100.net/emu/dec_ansi_parser & https://en.wikipedia.org/wiki/ANSI_escape_code#Colors +//The current implementation is not standard compliant and uses hex colors as [FF0000; +void Terminal::TMP_test(const char* args){ + // const char *term = seek_to(args, '\033'); + // if (*term == 0) return; + const char *term = seek_to(args, '['); + if (*term == 0) return; + const char *next = seek_to(term, ';'); + uint64_t color = parse_hex_u64(term, next - term); + set_text_color(color); + put_string(next); +} + void Terminal::handle_input(){ keypress kp; if (sys_read_input_current(&kp)){ diff --git a/kernel/console/kconsole/terminal.hpp b/kernel/console/kconsole/terminal.hpp index e3015ebe..36314678 100644 --- a/kernel/console/kconsole/terminal.hpp +++ b/kernel/console/kconsole/terminal.hpp @@ -13,6 +13,7 @@ class Terminal: public KernelConsole { //TODO: proper commands void TMP_cat(const char *args); + void TMP_test(const char *args); bool command_running; }; \ No newline at end of file diff --git a/kernel/filesystem/fat32.cpp b/kernel/filesystem/fat32.cpp index ce8c0e7b..d9b4bb01 100644 --- a/kernel/filesystem/fat32.cpp +++ b/kernel/filesystem/fat32.cpp @@ -14,6 +14,7 @@ }\ }) +//TODO: use seek_to const char* FAT32FS::advance_path(const char *path){ while (*path != '/' && *path != '\0') path++; diff --git a/shared/std/string.c b/shared/std/string.c index 27267992..f5c67f8e 100644 --- a/shared/std/string.c +++ b/shared/std/string.c @@ -316,7 +316,7 @@ bool utf16tochar(uint16_t* str_in, char* out_str, size_t max_len){ return true; } -uint64_t parse_hex_u64(char* str, size_t size){ +uint64_t parse_hex_u64(const char* str, size_t size){ uint64_t result = 0; for (uint32_t i = 0; i < size; i++){ char c = str[i]; @@ -377,4 +377,11 @@ void string_append_bytes(string *dest, const void *buf, uint32_t len) if (!len) return; string tmp = { (char *)buf, len, len }; string_concat_inplace(dest, tmp); +} + +const char* seek_to(const char *string, char character){ + while (*string != character && *string != '\0') + string++; + string++; + return string; } \ No newline at end of file diff --git a/shared/std/string.h b/shared/std/string.h index 8da8b39e..48ead22c 100644 --- a/shared/std/string.h +++ b/shared/std/string.h @@ -38,7 +38,7 @@ int strstart(const char *a, const char *b, bool case_insensitive); int strend(const char *a, const char *b, bool case_insensitive); int strindex(const char *a, const char *b); -uint64_t parse_hex_u64(char* str, size_t size); +uint64_t parse_hex_u64(const char* str, size_t size); bool utf16tochar( uint16_t* str_in, char* out_str, size_t max_len); @@ -46,6 +46,7 @@ string string_from_const(const char *literal); string string_concat(string a, string b); void string_concat_inplace(string *dest, string src); void string_append_bytes(string *dest, const void *buf, uint32_t len); +const char* seek_to(const char *string, char character); #ifdef __cplusplus } From f19d7d969d3d0d21c2634fb023c9a45861ec1532 Mon Sep 17 00:00:00 2001 From: Di Ferrari Date: Fri, 8 Aug 2025 00:00:00 +0000 Subject: [PATCH 26/50] [CONSOLE] limiting color to 24 bit --- kernel/console/kconsole/terminal.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/console/kconsole/terminal.cpp b/kernel/console/kconsole/terminal.cpp index 3b531fb6..b12490c6 100644 --- a/kernel/console/kconsole/terminal.cpp +++ b/kernel/console/kconsole/terminal.cpp @@ -70,7 +70,7 @@ void Terminal::TMP_test(const char* args){ if (*term == 0) return; const char *next = seek_to(term, ';'); uint64_t color = parse_hex_u64(term, next - term); - set_text_color(color); + set_text_color(color & 0xFFFFFF); put_string(next); } From 045f223e8bb7f86a6b8f9abc54da14d0afe10b0c Mon Sep 17 00:00:00 2001 From: Di Ferrari Date: Fri, 8 Aug 2025 00:00:00 +0000 Subject: [PATCH 27/50] [CONSOLE] removed unused function --- kernel/console/kconsole/terminal.hpp | 1 - 1 file changed, 1 deletion(-) diff --git a/kernel/console/kconsole/terminal.hpp b/kernel/console/kconsole/terminal.hpp index 36314678..b19f99ed 100644 --- a/kernel/console/kconsole/terminal.hpp +++ b/kernel/console/kconsole/terminal.hpp @@ -9,7 +9,6 @@ class Terminal: public KernelConsole { void handle_input(); void end_command(); void run_command(); - const char* seek_to(const char *string, char character); //TODO: proper commands void TMP_cat(const char *args); From b17898cf5c719c686da28f7e220b5ae01fabe55f Mon Sep 17 00:00:00 2001 From: Di Ferrari Date: Sat, 9 Aug 2025 00:00:00 +0000 Subject: [PATCH 28/50] merge main into console --- kernel/filesystem/filesystem.cpp | 12 +++++++++++- kernel/filesystem/filesystem.h | 1 + kernel/kernel_processes/boot/bootscreen.c | 1 + kernel/kernel_processes/kprocess_loader.c | 2 ++ kernel/process/loading/process_loader.c | 2 ++ kernel/process/process.h | 3 +++ kernel/process/scheduler.c | 3 +++ kernel/process/syscall.c | 9 ++++++++- 8 files changed, 31 insertions(+), 2 deletions(-) diff --git a/kernel/filesystem/filesystem.cpp b/kernel/filesystem/filesystem.cpp index 6f2b9c94..00ca4109 100644 --- a/kernel/filesystem/filesystem.cpp +++ b/kernel/filesystem/filesystem.cpp @@ -98,10 +98,20 @@ size_t read_file(file *descriptor, char* buf, size_t size){ open_file_descriptors file = open_files->find([descriptor](open_file_descriptors kvp){ return descriptor->id == kvp.file_id; })->data; - size_t adj_size = min(size,file.file_size);//TODO: still possible to modify the fd's size + if (!file.mod) return 0; + size_t adj_size = min(size,file.file_size); return file.mod->read(descriptor, buf, adj_size, 0); } +size_t write_file(file *descriptor, const char* buf, size_t size){ + if (!open_files) return 0; + open_file_descriptors file = open_files->find([descriptor](open_file_descriptors kvp){ + return descriptor->id == kvp.file_id; + })->data; + if (!file.mod) return 0; + return file.mod->write(descriptor, buf, size, 0); +} + sizedptr list_directory_contents(const char *path){ const char *search_path = path; driver_module *mod = get_module(&search_path); diff --git a/kernel/filesystem/filesystem.h b/kernel/filesystem/filesystem.h index b3572f0f..44a4baf1 100644 --- a/kernel/filesystem/filesystem.h +++ b/kernel/filesystem/filesystem.h @@ -10,6 +10,7 @@ extern "C" { FS_RESULT open_file(const char* path, file* descriptor); size_t read_file(file *descriptor, char* buf, size_t size); +size_t write_file(file *descriptor, const char* buf, size_t size); sizedptr list_directory_contents(const char *path); bool init_boot_filesystem(); diff --git a/kernel/kernel_processes/boot/bootscreen.c b/kernel/kernel_processes/boot/bootscreen.c index 264102de..950fbe78 100644 --- a/kernel/kernel_processes/boot/bootscreen.c +++ b/kernel/kernel_processes/boot/bootscreen.c @@ -11,6 +11,7 @@ __attribute__((section(".text.kcoreprocesses"))) void boot_draw_name(gpu_point screen_middle,int xoffset, int yoffset){ + printf("Hello buffer"); const char* name = BOOTSCREEN_TEXT; string s = string_l(name); int scale = 2; diff --git a/kernel/kernel_processes/kprocess_loader.c b/kernel/kernel_processes/kprocess_loader.c index f5beb114..075fd33b 100644 --- a/kernel/kernel_processes/kprocess_loader.c +++ b/kernel/kernel_processes/kprocess_loader.c @@ -34,6 +34,8 @@ process_t *create_kernel_process(const char *name, void (*func)()){ proc->spsr = 0x205; proc->state = READY; + proc->output = (uintptr_t)palloc(0x1000, true, false, true); + enable_interrupt(); return proc; diff --git a/kernel/process/loading/process_loader.c b/kernel/process/loading/process_loader.c index c21d5f61..71332b0d 100644 --- a/kernel/process/loading/process_loader.c +++ b/kernel/process/loading/process_loader.c @@ -284,6 +284,8 @@ process_t* create_process(const char *name, void *content, uint64_t content_size proc->spsr = 0; proc->state = READY; + proc->output = (uintptr_t)palloc(0x1000, false, false, true); + enable_interrupt(); return proc; diff --git a/kernel/process/process.h b/kernel/process/process.h index be636578..b607b4b9 100644 --- a/kernel/process/process.h +++ b/kernel/process/process.h @@ -7,6 +7,7 @@ extern "C" { #include "types.h" #include "keypress.h" #include "net/network_types.h" +#include "dev/driver_base.h" #define INPUT_BUFFER_CAPACITY 64 #define PACKET_BUFFER_CAPACITY 128 @@ -36,6 +37,8 @@ typedef struct { uintptr_t stack; uint64_t stack_size; uintptr_t heap; + uintptr_t output; + file out_fd; bool focused; enum process_state { STOPPED, READY, RUNNING, BLOCKED } state; input_buffer_t input_buffer; diff --git a/kernel/process/scheduler.c b/kernel/process/scheduler.c index cac4ac38..0fd552cb 100644 --- a/kernel/process/scheduler.c +++ b/kernel/process/scheduler.c @@ -9,6 +9,7 @@ #include "exceptions/timer.h" #include "console/kconsole/kconsole.h" #include "syscalls/syscalls.h" +#include "std/string.h" extern void save_context(process_t* proc); extern void save_pc_interrupt(process_t* proc); @@ -127,6 +128,7 @@ void init_main_process(){ proc->stack_size = 0x1000; proc->stack = (uintptr_t)palloc(proc->stack_size,true,false,true); proc->sp = ksp; + proc->output = (uintptr_t)palloc(0x1000, true, false, true); name_process(proc, "kernel"); proc_count++; } @@ -259,6 +261,7 @@ sizedptr list_processes(const char *path){ FS_RESULT open_proc(const char *path, file *descriptor){ kprintf("OPEN: %s",path); + // path = seek_to() return FS_RESULT_DRIVER_ERROR; } diff --git a/kernel/process/syscall.c b/kernel/process/syscall.c index 8882644a..490b4800 100644 --- a/kernel/process/syscall.c +++ b/kernel/process/syscall.c @@ -14,6 +14,7 @@ #include "exceptions/timer.h" #include "networking/network.h" #include "networking/port_manager.h" +#include "filesystem/filesystem.h" void sync_el0_handler_c(){ save_context_registers(); @@ -66,9 +67,15 @@ void sync_el0_handler_c(){ kfree((void*)x0, x1); break; case 3: + // process_t *proc = get_current_proc(); + // if (proc->out_fd.id == 0){ + // string s = string_format("/proc/%i/out",proc->id); + // open_file(s.data, &proc->out_fd); + // kfree(s.data, s.mem_length); + // } + // write_file(&proc->out_fd, (const char *)x0, strlen((const char *)x0,256)); kprint((const char *)x0); break; - case 5: keypress *kp = (keypress*)x0; result = sys_read_input_current(kp); From 0fc4df735b11d9523d3040be6e48997164cdd7a6 Mon Sep 17 00:00:00 2001 From: Di Ferrari Date: Sat, 9 Aug 2025 00:00:00 +0000 Subject: [PATCH 29/50] [STRING] parse integer --- shared/std/string.c | 11 +++++++++++ shared/std/string.h | 1 + 2 files changed, 12 insertions(+) diff --git a/shared/std/string.c b/shared/std/string.c index f5c67f8e..b6c87e85 100644 --- a/shared/std/string.c +++ b/shared/std/string.c @@ -330,6 +330,17 @@ uint64_t parse_hex_u64(const char* str, size_t size){ return result; } +uint64_t parse_int_u64(const char* str, size_t size){ + uint64_t result = 0; + for (uint32_t i = 0; i < size; i++){ + char c = str[i]; + uint8_t digit = 0; + if (c >= '0' && c <= '9') digit = c - '0'; + else break; + result = (result * 10) + digit; + } + return result; +} string string_from_const(const char *lit) { diff --git a/shared/std/string.h b/shared/std/string.h index 48ead22c..7f6a35f9 100644 --- a/shared/std/string.h +++ b/shared/std/string.h @@ -39,6 +39,7 @@ int strend(const char *a, const char *b, bool case_insensitive); int strindex(const char *a, const char *b); uint64_t parse_hex_u64(const char* str, size_t size); +uint64_t parse_int_u64(const char* str, size_t size); bool utf16tochar( uint16_t* str_in, char* out_str, size_t max_len); From 3a304f43828b4542e3a91a79956ba03a76cfaf67 Mon Sep 17 00:00:00 2001 From: Di Ferrari Date: Sat, 9 Aug 2025 00:00:00 +0000 Subject: [PATCH 30/50] [PROJ] restoring linux-compatible rundebug script --- rundebug | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/rundebug b/rundebug index 965618f0..e9563519 100755 --- a/rundebug +++ b/rundebug @@ -10,9 +10,11 @@ for arg in "$@"; do esac done -osascript < Date: Sat, 9 Aug 2025 00:00:00 +0000 Subject: [PATCH 31/50] [FS] introducting cursor to file descriptor --- kernel/dev/driver_base.h | 1 + 1 file changed, 1 insertion(+) diff --git a/kernel/dev/driver_base.h b/kernel/dev/driver_base.h index 59d739f2..7332ccb8 100644 --- a/kernel/dev/driver_base.h +++ b/kernel/dev/driver_base.h @@ -6,6 +6,7 @@ typedef struct file { uint64_t id; size_t size; + uint64_t cursor; } file; typedef uint64_t file_offset; From 4b3690881fb4549553191de4b5f1ace0bf383bf7 Mon Sep 17 00:00:00 2001 From: Di Ferrari Date: Sat, 9 Aug 2025 00:00:00 +0000 Subject: [PATCH 32/50] [FS] removing some logs from modules --- kernel/dev/module_loader.c | 2 -- kernel/filesystem/filesystem.cpp | 1 - 2 files changed, 3 deletions(-) diff --git a/kernel/dev/module_loader.c b/kernel/dev/module_loader.c index 74a99881..05e72ca4 100644 --- a/kernel/dev/module_loader.c +++ b/kernel/dev/module_loader.c @@ -17,7 +17,6 @@ bool unload_module(driver_module *module){ int fs_search(void *node, void *key){ driver_module* module = (driver_module*)node; const char** path = (const char**)key; - kprintf("Comparing %s to %s",(uintptr_t)*path,(uintptr_t)module->mount); int index = strstart(*path, module->mount, false); if (index == (int)strlen(module->mount,0)){ *path += index; @@ -27,7 +26,6 @@ int fs_search(void *node, void *key){ } driver_module* get_module(const char **full_path){ - kprintf("Seeking module %s",(uintptr_t)*full_path); clinkedlist_node_t *node = clinkedlist_find(modules, (void*)full_path, fs_search); return ((driver_module*)node->data); } \ No newline at end of file diff --git a/kernel/filesystem/filesystem.cpp b/kernel/filesystem/filesystem.cpp index 00ca4109..645c75a6 100644 --- a/kernel/filesystem/filesystem.cpp +++ b/kernel/filesystem/filesystem.cpp @@ -77,7 +77,6 @@ bool init_boot_filesystem(){ FS_RESULT open_file(const char* path, file* descriptor){ const char *search_path = path; driver_module *mod = get_module(&search_path); - kprintf("Got module %x for path %s",(uintptr_t)mod,search_path); if (!mod) return FS_RESULT_NOTFOUND; FS_RESULT result = mod->open(search_path, descriptor); if (!open_files) From a85ed9e8280b919c66b2fc6327a18aa93366ba6c Mon Sep 17 00:00:00 2001 From: Di Ferrari Date: Sat, 9 Aug 2025 00:00:00 +0000 Subject: [PATCH 33/50] [FS,FAIL] trying to write to console as file (seems like nested syscalls cause issues) --- kernel/process/scheduler.c | 61 +++++++++++++++++++++++++++++++++++--- kernel/process/syscall.c | 28 ++++++++++------- 2 files changed, 75 insertions(+), 14 deletions(-) diff --git a/kernel/process/scheduler.c b/kernel/process/scheduler.c index 0fd552cb..d46de8c4 100644 --- a/kernel/process/scheduler.c +++ b/kernel/process/scheduler.c @@ -10,6 +10,8 @@ #include "console/kconsole/kconsole.h" #include "syscalls/syscalls.h" #include "std/string.h" +#include "data_struct/linked_list.h" +#include "std/memfunctions.h" extern void save_context(process_t* proc); extern void save_pc_interrupt(process_t* proc); @@ -32,6 +34,17 @@ typedef struct sleep_tracker { sleep_tracker sleeping[MAX_PROCS]; uint16_t sleep_count; +clinkedlist_t *proc_opened_files; + +typedef struct proc_open_file { + uint64_t fid; + size_t file_size; + uintptr_t buffer; + uint16_t pid; +} proc_open_file; + +void* proc_page; + void save_context_registers(){ save_context(&processes[current_proc]); } @@ -120,6 +133,7 @@ void reset_process(process_t *proc){ } void init_main_process(){ + proc_page = palloc(0x1000, true, false, false); process_t* proc = &processes[0]; reset_process(proc); proc->id = next_proc_index++; @@ -259,18 +273,57 @@ sizedptr list_processes(const char *path){ return (sizedptr){(uintptr_t)list_buffer,size}; } +#define PROC_OUT_BUF 0x1000 + FS_RESULT open_proc(const char *path, file *descriptor){ - kprintf("OPEN: %s",path); - // path = seek_to() - return FS_RESULT_DRIVER_ERROR; + const char *pid_s = seek_to(path, '/'); + path = seek_to(pid_s, '/'); + uint64_t pid = parse_int_u64(pid_s, path - pid_s); + process_t *proc = get_proc_by_pid(pid); + descriptor->id = reserve_fd_id(); + descriptor->size = PROC_OUT_BUF; + if (!proc_opened_files) + proc_opened_files = kalloc(proc_page, sizeof(clinkedlist_t), ALIGN_64B, true, false); + proc_open_file *file = kalloc(proc_page, sizeof(proc_open_file), ALIGN_64B, true, false); + file->fid = descriptor->id; + file->buffer = proc->output; + file->pid = proc->id; + file->file_size = descriptor->size; + clinkedlist_push_front(proc_opened_files, (void*)file); + return FS_RESULT_SUCCESS; } size_t read_proc(file* fd, char *buf, size_t size, file_offset offset){ + return 0; +} +int find_open_proc_file(void *node, void* key){ + uart_puts("Processing file against key "); + uint64_t *fid = (uint64_t*)key; + uart_puthex(*fid); + proc_open_file *file = (proc_open_file*)node; + if (file->fid == *fid) + return 0; + return -1; } size_t write_proc(file* fd, const char *buf, size_t size, file_offset offset){ - + if (!proc_opened_files){ + kprint("No files open"); + return 0; + } + clinkedlist_node_t *node = clinkedlist_find(proc_opened_files, (void*)&fd->id, find_open_proc_file); + // if (!node->data) return 0; + // proc_open_file *file = (proc_open_file*)node->data; + // if (size >= PROC_OUT_BUF){ + // kprint("Output buffer too large"); + // return 0; + // } + // if (fd->cursor + size >= PROC_OUT_BUF) + // fd->cursor = 0; + // memcpy((void*)(file->buffer + fd->cursor), buf, size); + // kprintf("Wrote %i bytes into process %i's output buffer", size, file->pid); + return size; } driver_module scheduler_module = (driver_module){ diff --git a/kernel/process/syscall.c b/kernel/process/syscall.c index 490b4800..20793412 100644 --- a/kernel/process/syscall.c +++ b/kernel/process/syscall.c @@ -16,9 +16,13 @@ #include "networking/port_manager.h" #include "filesystem/filesystem.h" +int syscall_depth = 0; + void sync_el0_handler_c(){ save_context_registers(); save_return_address_interrupt(); + + syscall_depth++; if (ksp > 0) asm volatile ("mov sp, %0" :: "r"(ksp)); @@ -57,7 +61,7 @@ void sync_el0_handler_c(){ switch (iss) { case 0: - void* page_ptr = (void*)get_current_heap(); + void* page_ptr = syscall_depth > 1 ? (void*)get_proc_by_pid(1)->heap : (void*)get_current_heap(); if ((uintptr_t)page_ptr == 0x0){ handle_exception_with_info("Wrong process heap state", iss); } @@ -67,13 +71,17 @@ void sync_el0_handler_c(){ kfree((void*)x0, x1); break; case 3: - // process_t *proc = get_current_proc(); - // if (proc->out_fd.id == 0){ - // string s = string_format("/proc/%i/out",proc->id); - // open_file(s.data, &proc->out_fd); - // kfree(s.data, s.mem_length); - // } - // write_file(&proc->out_fd, (const char *)x0, strlen((const char *)x0,256)); + process_t *proc = get_current_proc(); + if (proc->out_fd.id == 0){ + string s = string_format("/proc/%i/out",proc->id); + if (open_file(s.data, &proc->out_fd) != FS_RESULT_SUCCESS){ + kprint("Failed to open process output"); + kfree(s.data, s.mem_length); + break; + } + kfree(s.data, s.mem_length); + } + write_file(&proc->out_fd, (const char *)x0, strlen((const char *)x0,256)); kprint((const char *)x0); break; case 5: @@ -183,8 +191,8 @@ void sync_el0_handler_c(){ stop_current_process(); } } + syscall_depth--; if (result > 0) save_syscall_return(result); process_restore(); -} - +} \ No newline at end of file From 9076d0988933ef94e3f209df5edba23aa46ff05d Mon Sep 17 00:00:00 2001 From: Di Ferrari Date: Sat, 9 Aug 2025 00:00:00 +0000 Subject: [PATCH 34/50] [CONSOLE, TMP] fixing printf issue by using the file functions outside of the syscall --- kernel/kernel_processes/boot/bootscreen.c | 10 +++++++++- kernel/process/syscall.c | 11 ----------- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/kernel/kernel_processes/boot/bootscreen.c b/kernel/kernel_processes/boot/bootscreen.c index 950fbe78..9097c15c 100644 --- a/kernel/kernel_processes/boot/bootscreen.c +++ b/kernel/kernel_processes/boot/bootscreen.c @@ -8,10 +8,18 @@ #include "process/scheduler.h" #include "math/math.h" #include "syscalls/syscalls.h" +#include "filesystem/filesystem.h" __attribute__((section(".text.kcoreprocesses"))) void boot_draw_name(gpu_point screen_middle,int xoffset, int yoffset){ - printf("Hello buffer"); + file fd; + uint16_t pid = get_current_proc_pid(); + string proc_out = string_format("/proc/%i/out",pid); + open_file(proc_out.data, &fd); + free(proc_out.data, proc_out.mem_length); + + write_file(&fd, "hello buffer", 12); + const char* name = BOOTSCREEN_TEXT; string s = string_l(name); int scale = 2; diff --git a/kernel/process/syscall.c b/kernel/process/syscall.c index 20793412..107a6e6a 100644 --- a/kernel/process/syscall.c +++ b/kernel/process/syscall.c @@ -71,17 +71,6 @@ void sync_el0_handler_c(){ kfree((void*)x0, x1); break; case 3: - process_t *proc = get_current_proc(); - if (proc->out_fd.id == 0){ - string s = string_format("/proc/%i/out",proc->id); - if (open_file(s.data, &proc->out_fd) != FS_RESULT_SUCCESS){ - kprint("Failed to open process output"); - kfree(s.data, s.mem_length); - break; - } - kfree(s.data, s.mem_length); - } - write_file(&proc->out_fd, (const char *)x0, strlen((const char *)x0,256)); kprint((const char *)x0); break; case 5: From fe5bcdaebbd23f02c8a90e3bc718f632ac94a116 Mon Sep 17 00:00:00 2001 From: Di Ferrari Date: Sat, 9 Aug 2025 00:00:00 +0000 Subject: [PATCH 35/50] [LINK] removed redundant kcoreprocesses section --- kernel/kernel_processes/boot/bootprocess.cpp | 2 +- kernel/kernel_processes/boot/bootscreen.c | 4 ---- kernel/kernel_processes/boot/login_screen.c | 1 - kernel/kernel_processes/monitor/monitor_processes.c | 4 ---- kernel/kernel_processes/windows/windows.cpp | 4 +--- kernel/linker.ld | 11 ----------- 6 files changed, 2 insertions(+), 24 deletions(-) diff --git a/kernel/kernel_processes/boot/bootprocess.cpp b/kernel/kernel_processes/boot/bootprocess.cpp index b18528a6..d13d6865 100644 --- a/kernel/kernel_processes/boot/bootprocess.cpp +++ b/kernel/kernel_processes/boot/bootprocess.cpp @@ -5,7 +5,7 @@ BootSM *state_machine; -extern "C" __attribute__((section(".text.kcoreprocesses"))) void eval_bootscreen() { +extern "C" void eval_bootscreen() { while (1){ state_machine->eval_state(); } diff --git a/kernel/kernel_processes/boot/bootscreen.c b/kernel/kernel_processes/boot/bootscreen.c index 9097c15c..0ec622e2 100644 --- a/kernel/kernel_processes/boot/bootscreen.c +++ b/kernel/kernel_processes/boot/bootscreen.c @@ -10,7 +10,6 @@ #include "syscalls/syscalls.h" #include "filesystem/filesystem.h" -__attribute__((section(".text.kcoreprocesses"))) void boot_draw_name(gpu_point screen_middle,int xoffset, int yoffset){ file fd; uint16_t pid = get_current_proc_pid(); @@ -32,10 +31,8 @@ void boot_draw_name(gpu_point screen_middle,int xoffset, int yoffset){ free(s.data,s.mem_length); } -__attribute__((section(".rodata.kcoreprocesses"))) const gpu_point offsets[BOOTSCREEN_NUM_SYMBOLS] = BOOTSCREEN_OFFSETS; -__attribute__((section(".text.kcoreprocesses"))) gpu_point boot_calc_point(gpu_point offset, gpu_size screen_size, gpu_point screen_middle){ int xoff = (screen_size.width/BOOTSCREEN_DIV) * offset.x; int yoff = (screen_size.height/BOOTSCREEN_DIV) * offset.y; @@ -95,7 +92,6 @@ void boot_draw_lines(gpu_point current_point, gpu_point next_point, gpu_size siz } } -__attribute__((section(".text.kcoreprocesses"))) void bootscreen(){ disable_visual(); gpu_clear(BG_COLOR); diff --git a/kernel/kernel_processes/boot/login_screen.c b/kernel/kernel_processes/boot/login_screen.c index a7e40870..4a04e263 100644 --- a/kernel/kernel_processes/boot/login_screen.c +++ b/kernel/kernel_processes/boot/login_screen.c @@ -10,7 +10,6 @@ #include "std/string.h" #include "syscalls/syscalls.h" -__attribute__((section(".text.kcoreprocesses"))) void login_screen(){ sys_focus_current(); sys_set_secure(true); diff --git a/kernel/kernel_processes/monitor/monitor_processes.c b/kernel/kernel_processes/monitor/monitor_processes.c index 42b9fb66..345dc5a4 100644 --- a/kernel/kernel_processes/monitor/monitor_processes.c +++ b/kernel/kernel_processes/monitor/monitor_processes.c @@ -12,7 +12,6 @@ #include "syscalls/syscalls.h" #include "memory/memory_types.h" -__attribute__((section(".text.kcoreprocesses"))) char* parse_proc_state(int state){ switch (state) { @@ -37,7 +36,6 @@ uint64_t calc_heap(uintptr_t ptr){ return size; } -__attribute__((section(".text.kcoreprocesses"))) void print_process_info(){ process_t *processes = get_all_processes(); for (int i = 0; i < MAX_PROCS; i++){ @@ -75,7 +73,6 @@ void draw_memory(char *name,int x, int y, int width, int full_height, int used, free(str.data,str.mem_length); } -__attribute__((section(".text.kcoreprocesses"))) void draw_process_view(){ gpu_clear(BG_COLOR+0x112211); process_t *processes = get_all_processes(); @@ -148,7 +145,6 @@ void draw_process_view(){ print_process_info(); } -__attribute__((section(".text.kcoreprocesses"))) void monitor_procs(){ keypress kp = { .modifier = KEY_MOD_LALT, diff --git a/kernel/kernel_processes/windows/windows.cpp b/kernel/kernel_processes/windows/windows.cpp index 0eea918c..000c2244 100644 --- a/kernel/kernel_processes/windows/windows.cpp +++ b/kernel/kernel_processes/windows/windows.cpp @@ -9,7 +9,7 @@ WindowManager manager; bool screen_overlay; -extern "C" __attribute__((section(".text.kcoreprocesses"))) void manage_windows(){ +extern "C" void manage_windows(){ manager.initialize(); while (1) { @@ -17,12 +17,10 @@ extern "C" __attribute__((section(".text.kcoreprocesses"))) void manage_windows( } } -__attribute__((section(".text.kcoreprocesses"))) void pause_window_draw(){ manager.pause(); } -__attribute__((section(".text.kcoreprocesses"))) void resume_window_draw(){ manager.resume(); } diff --git a/kernel/linker.ld b/kernel/linker.ld index 1d3833dd..8c7f977a 100644 --- a/kernel/linker.ld +++ b/kernel/linker.ld @@ -30,20 +30,9 @@ SECTIONS { .heap_fill : { FILL(0); . = . + 0x2000000; } heap_limit = .; - .kcoreprocesses : ALIGN(8) { - kcoreprocesses_rodata_start = .; - KEEP(*(.rodata.kcoreprocesses)) - KEEP(*(.data.kcoreprocesses)) - kcoreprocesses_rodata_end = .; - kcoreprocesses_start = .; - KEEP(*(.text.kcoreprocesses)) - kcoreprocesses_end = .; - } . = ALIGN(0x200000); kcode_end = .; - . = . + 0x200000; - .shared : ALIGN(4096) { shared_start = .; KEEP(*(.shared)) From b23834ba627e7ebcbf8f4c066e9aad5a1c94b2ba Mon Sep 17 00:00:00 2001 From: differrari Date: Sat, 9 Aug 2025 00:00:00 +0000 Subject: [PATCH 36/50] [PROC, CONSOLE] output file reading --- kernel/kernel_processes/boot/bootscreen.c | 8 ++-- kernel/process/scheduler.c | 46 ++++++++++++++--------- 2 files changed, 34 insertions(+), 20 deletions(-) diff --git a/kernel/kernel_processes/boot/bootscreen.c b/kernel/kernel_processes/boot/bootscreen.c index 0ec622e2..d7330f2a 100644 --- a/kernel/kernel_processes/boot/bootscreen.c +++ b/kernel/kernel_processes/boot/bootscreen.c @@ -10,14 +10,16 @@ #include "syscalls/syscalls.h" #include "filesystem/filesystem.h" +file boot_fd; + void boot_draw_name(gpu_point screen_middle,int xoffset, int yoffset){ - file fd; uint16_t pid = get_current_proc_pid(); string proc_out = string_format("/proc/%i/out",pid); - open_file(proc_out.data, &fd); + if (boot_fd.size == 0) + open_file(proc_out.data, &boot_fd); free(proc_out.data, proc_out.mem_length); - write_file(&fd, "hello buffer", 12); + write_file(&boot_fd, "hello buffer", 12); const char* name = BOOTSCREEN_TEXT; string s = string_l(name); diff --git a/kernel/process/scheduler.c b/kernel/process/scheduler.c index d46de8c4..bb8a9dd8 100644 --- a/kernel/process/scheduler.c +++ b/kernel/process/scheduler.c @@ -12,6 +12,7 @@ #include "std/string.h" #include "data_struct/linked_list.h" #include "std/memfunctions.h" +#include "math/math.h" extern void save_context(process_t* proc); extern void save_pc_interrupt(process_t* proc); @@ -282,6 +283,7 @@ FS_RESULT open_proc(const char *path, file *descriptor){ process_t *proc = get_proc_by_pid(pid); descriptor->id = reserve_fd_id(); descriptor->size = PROC_OUT_BUF; + descriptor->cursor = 0; if (!proc_opened_files) proc_opened_files = kalloc(proc_page, sizeof(clinkedlist_t), ALIGN_64B, true, false); proc_open_file *file = kalloc(proc_page, sizeof(proc_open_file), ALIGN_64B, true, false); @@ -293,36 +295,46 @@ FS_RESULT open_proc(const char *path, file *descriptor){ return FS_RESULT_SUCCESS; } -size_t read_proc(file* fd, char *buf, size_t size, file_offset offset){ - return 0; -} int find_open_proc_file(void *node, void* key){ - uart_puts("Processing file against key "); uint64_t *fid = (uint64_t*)key; - uart_puthex(*fid); proc_open_file *file = (proc_open_file*)node; - if (file->fid == *fid) - return 0; + if (file->fid == *fid) return 0; return -1; } +size_t read_proc(file* fd, char *buf, size_t size, file_offset offset){ + if (!proc_opened_files){ + kprint("No files open"); + return 0; + } + clinkedlist_node_t *node = clinkedlist_find(proc_opened_files, (void*)&fd->id, find_open_proc_file); + if (!node->data) return 0; + proc_open_file *file = (proc_open_file*)node->data; + size = min(size, file->file_size); + memcpy(buf, (void*)(file->buffer + fd->cursor), size); + return size; +} + size_t write_proc(file* fd, const char *buf, size_t size, file_offset offset){ if (!proc_opened_files){ kprint("No files open"); return 0; } clinkedlist_node_t *node = clinkedlist_find(proc_opened_files, (void*)&fd->id, find_open_proc_file); - // if (!node->data) return 0; - // proc_open_file *file = (proc_open_file*)node->data; - // if (size >= PROC_OUT_BUF){ - // kprint("Output buffer too large"); - // return 0; - // } - // if (fd->cursor + size >= PROC_OUT_BUF) - // fd->cursor = 0; - // memcpy((void*)(file->buffer + fd->cursor), buf, size); - // kprintf("Wrote %i bytes into process %i's output buffer", size, file->pid); + if (!node->data) return 0; + proc_open_file *file = (proc_open_file*)node->data; + if (size >= PROC_OUT_BUF){ + kprint("Output buffer too large"); + return 0; + } + if (fd->cursor + size >= PROC_OUT_BUF){ + fd->cursor = 0; + memset((void*)file->buffer, 0, file->file_size); + } + memcpy((void*)(file->buffer + fd->cursor), buf, size); + fd->cursor += size; + kprintf("Wrote %i bytes into process %i's output buffer", size, file->pid); return size; } From 693d8993da0b4074d5182919847b252e2964cd3e Mon Sep 17 00:00:00 2001 From: differrari Date: Sun, 10 Aug 2025 00:00:00 +0000 Subject: [PATCH 37/50] [CONSOLE] implemented cat --- kernel/bin/cat.c | 29 +++++++++++++++++++++++ kernel/bin/cat.h | 13 ++++++++++ kernel/console/kconsole/terminal.cpp | 27 ++++++++++----------- kernel/filesystem/filesystem.cpp | 4 +++- kernel/kernel_processes/boot/bootscreen.c | 8 +++---- 5 files changed, 62 insertions(+), 19 deletions(-) create mode 100644 kernel/bin/cat.c create mode 100644 kernel/bin/cat.h diff --git a/kernel/bin/cat.c b/kernel/bin/cat.c new file mode 100644 index 00000000..1567a24a --- /dev/null +++ b/kernel/bin/cat.c @@ -0,0 +1,29 @@ +#include "cat.h" +#include "kernel_processes/kprocess_loader.h" +#include "filesystem/filesystem.h" +#include "process/scheduler.h" +#include "syscalls/syscalls.h" + +void run_cat(){ + const char* arg1 = "/proc/8/out"; + size_t arg2 = 100; + file fd; + open_file(arg1, &fd); + if (fd.size == 0) stop_current_process();// 1; + if (arg2 == 0) arg2 = fd.size; + char* buf = (char*)malloc(arg2); + read_file(&fd, buf, arg2); + + uint16_t pid = get_current_proc_pid(); + + string s = string_format("/proc/%i/out",pid); + file fd2; + open_file(s.data, &fd2); + write_file(&fd2, buf, arg2); + free(s.data, s.mem_length); + stop_current_process(); +} + +process_t* create_cat_process(const char *args){ + return create_kernel_process("cat", run_cat); +} \ No newline at end of file diff --git a/kernel/bin/cat.h b/kernel/bin/cat.h new file mode 100644 index 00000000..2a783228 --- /dev/null +++ b/kernel/bin/cat.h @@ -0,0 +1,13 @@ +#pragma once + +#include "process/process.h" + +#ifdef __cplusplus +extern "C" { +#endif + +process_t* create_cat_process(const char *args); + +#ifdef __cplusplus +} +#endif diff --git a/kernel/console/kconsole/terminal.cpp b/kernel/console/kconsole/terminal.cpp index b12490c6..d00b8ba6 100644 --- a/kernel/console/kconsole/terminal.cpp +++ b/kernel/console/kconsole/terminal.cpp @@ -4,6 +4,7 @@ #include "../serial/uart.h" #include "std/std.hpp" #include "filesystem/filesystem.h" +#include "bin/cat.h" void Terminal::update(){ if (!command_running) handle_input(); @@ -22,34 +23,32 @@ void Terminal::end_command(){ } void Terminal::TMP_cat(const char *args){ + process_t *cat = create_cat_process(args); + string s = string_format("/proc/%i/out",cat->id); file fd; - if (open_file(args, &fd) != FS_RESULT_SUCCESS) { - string s = string_format("Path not found %s", args); - put_string(s.data); - free(s.data,s.mem_length); - return; - } - size_t req_size = 0x100; - char* buf = (char*)malloc(req_size); - if (read_file(&fd, buf, req_size) == 0){ - put_string("Error reading file"); - return; + open_file(s.data, &fd); + free(s.data, s.mem_length); + while (cat->state != process_t::STOPPED){ + size_t amount = 0x100; + char *buf = (char*)malloc(amount); + read_file(&fd, buf, amount); + put_string(buf); + free(buf, amount); } - put_string(buf); } void Terminal::run_command(){ const char* fullcmd = get_current_line(); const char* args = seek_to(fullcmd, ' '); string cmd = string_ca_max(fullcmd, args - fullcmd - 1); - string s = string_format("Executing command %s with args %s", cmd.data, args); + string s = string_format("Unknown command %s with args %s", cmd.data, args); put_char('\r'); put_char('\n'); if (strcmp(cmd.data, "cat", true) == 0) TMP_cat(args); - if (strcmp(cmd.data, "test", true) == 0) + else if (strcmp(cmd.data, "test", true) == 0) TMP_test(args); else put_string(s.data); diff --git a/kernel/filesystem/filesystem.cpp b/kernel/filesystem/filesystem.cpp index 645c75a6..cd5903cc 100644 --- a/kernel/filesystem/filesystem.cpp +++ b/kernel/filesystem/filesystem.cpp @@ -99,7 +99,9 @@ size_t read_file(file *descriptor, char* buf, size_t size){ })->data; if (!file.mod) return 0; size_t adj_size = min(size,file.file_size); - return file.mod->read(descriptor, buf, adj_size, 0); + size_t amount_read = file.mod->read(descriptor, buf, adj_size, 0); + descriptor->cursor += amount_read; + return amount_read; } size_t write_file(file *descriptor, const char* buf, size_t size){ diff --git a/kernel/kernel_processes/boot/bootscreen.c b/kernel/kernel_processes/boot/bootscreen.c index d7330f2a..fe6f9080 100644 --- a/kernel/kernel_processes/boot/bootscreen.c +++ b/kernel/kernel_processes/boot/bootscreen.c @@ -14,11 +14,11 @@ file boot_fd; void boot_draw_name(gpu_point screen_middle,int xoffset, int yoffset){ uint16_t pid = get_current_proc_pid(); - string proc_out = string_format("/proc/%i/out",pid); - if (boot_fd.size == 0) + if (boot_fd.size == 0){ + string proc_out = string_format("/proc/%i/out",pid); open_file(proc_out.data, &boot_fd); - free(proc_out.data, proc_out.mem_length); - + free(proc_out.data, proc_out.mem_length); + } write_file(&boot_fd, "hello buffer", 12); const char* name = BOOTSCREEN_TEXT; From f44959861c0e80e682b81ae2bab37b1129a485d3 Mon Sep 17 00:00:00 2001 From: differrari Date: Sun, 10 Aug 2025 00:00:00 +0000 Subject: [PATCH 38/50] [PROC] added arguments to processes (used by cat) and properly exiting processes --- kernel/bin/cat.c | 28 +++++---- kernel/bin/cat.h | 2 +- kernel/console/kconsole/console.cpp | 5 +- kernel/console/kconsole/terminal.cpp | 57 +++++++++++++++---- kernel/console/kconsole/terminal.hpp | 5 +- kernel/input/input_dispatch.cpp | 10 ++-- kernel/kernel_processes/boot/bootprocess.cpp | 7 ++- kernel/kernel_processes/boot/bootscreen.c | 5 +- kernel/kernel_processes/boot/login_screen.c | 5 +- kernel/kernel_processes/kprocess_loader.c | 6 +- kernel/kernel_processes/kprocess_loader.h | 2 +- .../monitor/monitor_processes.c | 7 ++- kernel/kernel_processes/windows/windows.cpp | 4 +- kernel/networking/network.cpp | 5 +- kernel/networking/network.h | 2 +- kernel/networking/network_dispatch.cpp | 5 +- kernel/networking/network_dispatch.hpp | 2 +- kernel/networking/processes/net_proc.c | 20 +++---- kernel/process/syscall.c | 9 +++ shared/net/application_layer/dhcp_daemon.c | 4 +- shared/net/application_layer/dhcp_daemon.h | 2 +- shared/net/link_layer/arp.c | 2 +- shared/net/link_layer/arp.h | 3 +- shared/std/string.c | 19 +++++++ shared/std/string.h | 2 + 25 files changed, 150 insertions(+), 68 deletions(-) diff --git a/kernel/bin/cat.c b/kernel/bin/cat.c index 1567a24a..38bc0750 100644 --- a/kernel/bin/cat.c +++ b/kernel/bin/cat.c @@ -3,27 +3,31 @@ #include "filesystem/filesystem.h" #include "process/scheduler.h" #include "syscalls/syscalls.h" +#include "console/kio.h" -void run_cat(){ - const char* arg1 = "/proc/8/out"; - size_t arg2 = 100; +int run_cat(int argc, char* argv[]){ + if (argc != 2) return 2; + kprintf("Cat with %i arguments. %s", argc, argv[0]); + const char* path = argv[0]; + kprintf("Cat with %i arguments. %s", argc, argv[1]); + size_t size = parse_int_u64(argv[1], UINT32_MAX); file fd; - open_file(arg1, &fd); - if (fd.size == 0) stop_current_process();// 1; - if (arg2 == 0) arg2 = fd.size; - char* buf = (char*)malloc(arg2); - read_file(&fd, buf, arg2); + open_file(path, &fd); + if (fd.size == 0) return 1; + if (size == 0) size = fd.size; + char* buf = (char*)malloc(size); + read_file(&fd, buf, size); uint16_t pid = get_current_proc_pid(); string s = string_format("/proc/%i/out",pid); file fd2; open_file(s.data, &fd2); - write_file(&fd2, buf, arg2); + write_file(&fd2, buf, size); free(s.data, s.mem_length); - stop_current_process(); + return 0; } -process_t* create_cat_process(const char *args){ - return create_kernel_process("cat", run_cat); +process_t* create_cat_process(int argc, const char *argv[]){ + return create_kernel_process("cat", run_cat, argc, argv); } \ No newline at end of file diff --git a/kernel/bin/cat.h b/kernel/bin/cat.h index 2a783228..f7730899 100644 --- a/kernel/bin/cat.h +++ b/kernel/bin/cat.h @@ -6,7 +6,7 @@ extern "C" { #endif -process_t* create_cat_process(const char *args); +process_t* create_cat_process(int argc, const char *argv[]); #ifdef __cplusplus } diff --git a/kernel/console/kconsole/console.cpp b/kernel/console/kconsole/console.cpp index 270c0053..95dc721d 100644 --- a/kernel/console/kconsole/console.cpp +++ b/kernel/console/kconsole/console.cpp @@ -20,7 +20,7 @@ extern "C" void kconsole_clear() { kconsole.clear(); } -extern "C" void toggle_visual(){ +extern "C" int toggle_visual(int argc, char* argv[]){ keypress kp = { .modifier = KEY_MOD_LALT, .rsvd = 0, @@ -46,8 +46,9 @@ extern "C" void toggle_visual(){ terminal->update(); } } + return 1; } process_t* start_terminal(){ - return create_kernel_process("terminal",toggle_visual); + return create_kernel_process("terminal",toggle_visual, 0, 0); } \ No newline at end of file diff --git a/kernel/console/kconsole/terminal.cpp b/kernel/console/kconsole/terminal.cpp index d00b8ba6..f8efbe44 100644 --- a/kernel/console/kconsole/terminal.cpp +++ b/kernel/console/kconsole/terminal.cpp @@ -22,8 +22,8 @@ void Terminal::end_command(){ set_text_color(default_text_color); } -void Terminal::TMP_cat(const char *args){ - process_t *cat = create_cat_process(args); +void Terminal::TMP_cat(int argc, const char *args[]){ + process_t *cat = create_cat_process(argc, args); string s = string_format("/proc/%i/out",cat->id); file fd; open_file(s.data, &fd); @@ -37,23 +37,60 @@ void Terminal::TMP_cat(const char *args){ } } +const char** Terminal::parse_arguments(char *args, int *count){ + *count = 0; + const char* prev = args; + char* next_args; + const char **argv = (const char**)malloc(16 * sizeof(uintptr_t)); + do { + next_args = (char*)seek_to(args, ' '); + argv[*count] = prev; + (*count)++; + prev = next_args; + *(next_args - 1) = 0; + kprintf("Found an argument %s",prev); + } while(prev != next_args); + if (*next_args){ + argv[*count] = prev; + (*count)++; + kprintf("Ended at %s",next_args); + } + return argv; +} + void Terminal::run_command(){ const char* fullcmd = get_current_line(); const char* args = seek_to(fullcmd, ' '); - string cmd = string_ca_max(fullcmd, args - fullcmd - 1); - string s = string_format("Unknown command %s with args %s", cmd.data, args); + + string cmd; + int argc = 0; + const char** argv; + string args_copy; + + if (fullcmd == args){ + cmd = string_l(fullcmd); + } else { + cmd = string_ca_max(fullcmd, args - fullcmd - 1); + args_copy = string_l(args); + argv = parse_arguments(args_copy.data, &argc); + kprintf("There are %i arguments",argc); + } put_char('\r'); put_char('\n'); if (strcmp(cmd.data, "cat", true) == 0) - TMP_cat(args); + TMP_cat(argc, argv); else if (strcmp(cmd.data, "test", true) == 0) - TMP_test(args); - else put_string(s.data); + TMP_test(argc, argv); + else { + string s = string_format("Unknown command %s with args %s", cmd.data, args); + put_string(s.data); + free(s.data, s.mem_length); + } - free(s.data, s.mem_length); free(cmd.data, cmd.mem_length); + if (args_copy.mem_length) free(args_copy.data, args_copy.mem_length); draw_cursor(); gpu_flush(); @@ -62,10 +99,10 @@ void Terminal::run_command(){ //TODO: implement the full state machine explained at https://vt100.net/emu/dec_ansi_parser & https://en.wikipedia.org/wiki/ANSI_escape_code#Colors //The current implementation is not standard compliant and uses hex colors as [FF0000; -void Terminal::TMP_test(const char* args){ +void Terminal::TMP_test(int argc, const char* args[]){ // const char *term = seek_to(args, '\033'); // if (*term == 0) return; - const char *term = seek_to(args, '['); + const char *term = seek_to(*args, '['); if (*term == 0) return; const char *next = seek_to(term, ';'); uint64_t color = parse_hex_u64(term, next - term); diff --git a/kernel/console/kconsole/terminal.hpp b/kernel/console/kconsole/terminal.hpp index b19f99ed..a503f171 100644 --- a/kernel/console/kconsole/terminal.hpp +++ b/kernel/console/kconsole/terminal.hpp @@ -9,10 +9,11 @@ class Terminal: public KernelConsole { void handle_input(); void end_command(); void run_command(); + const char** parse_arguments(char *args, int *count); //TODO: proper commands - void TMP_cat(const char *args); - void TMP_test(const char *args); + void TMP_cat(int argc, const char *args[]); + void TMP_test(int argc, const char *args[]); bool command_running; }; \ No newline at end of file diff --git a/kernel/input/input_dispatch.cpp b/kernel/input/input_dispatch.cpp index 045f6e97..3611a701 100644 --- a/kernel/input/input_dispatch.cpp +++ b/kernel/input/input_dispatch.cpp @@ -136,23 +136,25 @@ bool input_init(){ } } -void input_process_poll(){ +int input_process_poll(int argc, char* argv[]){ while (1){ input_driver->poll_inputs(); } + return 1; } -void input_process_fake_interrupts(){ +int input_process_fake_interrupts(int argc, char* argv[]){ while (1){ input_driver->handle_interrupt(); } + return 1; } void init_input_process(){ if (!input_driver->use_interrupts) - create_kernel_process("input_poll", &input_process_poll); + create_kernel_process("input_poll", &input_process_poll, 0, 0); if (input_driver->quirk_simulate_interrupts) - create_kernel_process("input_int_mock", &input_process_fake_interrupts); + create_kernel_process("input_int_mock", &input_process_fake_interrupts, 0, 0); } void handle_input_interrupt(){ diff --git a/kernel/kernel_processes/boot/bootprocess.cpp b/kernel/kernel_processes/boot/bootprocess.cpp index d13d6865..faa46d63 100644 --- a/kernel/kernel_processes/boot/bootprocess.cpp +++ b/kernel/kernel_processes/boot/bootprocess.cpp @@ -5,14 +5,17 @@ BootSM *state_machine; -extern "C" void eval_bootscreen() { +//TODO: This is overengineered, just use C +extern "C" int eval_bootscreen(int argc, char* argv[]) { + kprintf(">>>> There are %i arguments"); while (1){ state_machine->eval_state(); } + return 1; } extern "C" void init_bootprocess() { state_machine = new BootSM(); - create_kernel_process("bootsm",eval_bootscreen); + create_kernel_process("bootsm",eval_bootscreen, 0, 0); state_machine->initialize(); } diff --git a/kernel/kernel_processes/boot/bootscreen.c b/kernel/kernel_processes/boot/bootscreen.c index fe6f9080..783a979f 100644 --- a/kernel/kernel_processes/boot/bootscreen.c +++ b/kernel/kernel_processes/boot/bootscreen.c @@ -94,7 +94,7 @@ void boot_draw_lines(gpu_point current_point, gpu_point next_point, gpu_size siz } } -void bootscreen(){ +int bootscreen(){ disable_visual(); gpu_clear(BG_COLOR); sys_focus_current(); @@ -114,8 +114,9 @@ void bootscreen(){ } sleep(1000); } + return 0; } process_t* start_bootscreen(){ - return create_kernel_process("bootscreen",bootscreen); + return create_kernel_process("bootscreen",bootscreen, 0, 0); } \ No newline at end of file diff --git a/kernel/kernel_processes/boot/login_screen.c b/kernel/kernel_processes/boot/login_screen.c index 4a04e263..4042974d 100644 --- a/kernel/kernel_processes/boot/login_screen.c +++ b/kernel/kernel_processes/boot/login_screen.c @@ -10,7 +10,7 @@ #include "std/string.h" #include "syscalls/syscalls.h" -void login_screen(){ +int login_screen(){ sys_focus_current(); sys_set_secure(true); char* buf = (char*)malloc(256); @@ -69,8 +69,9 @@ void login_screen(){ gpu_flush(); free(s.data,s.mem_length); } + return 1; } process_t* present_login(){ - return create_kernel_process("login",login_screen); + return create_kernel_process("login",login_screen, 0, 0); } \ No newline at end of file diff --git a/kernel/kernel_processes/kprocess_loader.c b/kernel/kernel_processes/kprocess_loader.c index 075fd33b..16ec8122 100644 --- a/kernel/kernel_processes/kprocess_loader.c +++ b/kernel/kernel_processes/kprocess_loader.c @@ -4,7 +4,7 @@ #include "memory/page_allocator.h" #include "exceptions/irq.h" -process_t *create_kernel_process(const char *name, void (*func)()){ +process_t *create_kernel_process(const char *name, int (*func)(int argc, char* argv[]), int argc, const char* argv[]){ disable_interrupt(); @@ -30,9 +30,11 @@ process_t *create_kernel_process(const char *name, void (*func)()){ proc->sp = proc->stack; proc->pc = (uintptr_t)func; - kprintf("Kernel process %s allocated with address at %x, stack at %x, heap at %x", (uintptr_t)name, proc->pc, proc->sp, proc->heap); + kprintf("Kernel process %s allocated with address at %x, stack at %x, heap at %x. %i argument(s)", (uintptr_t)name, proc->pc, proc->sp, proc->heap, argc); proc->spsr = 0x205; proc->state = READY; + proc->regs[14] = argc; + proc->regs[13] = (uintptr_t)argv; proc->output = (uintptr_t)palloc(0x1000, true, false, true); diff --git a/kernel/kernel_processes/kprocess_loader.h b/kernel/kernel_processes/kprocess_loader.h index a5a973c2..62b05b4d 100644 --- a/kernel/kernel_processes/kprocess_loader.h +++ b/kernel/kernel_processes/kprocess_loader.h @@ -7,7 +7,7 @@ extern "C" { #include "types.h" #include "process/process.h" -process_t *create_kernel_process(const char *name, void (*func)()); +process_t *create_kernel_process(const char *name, int (*func)(int argc, char* argv[]), int argc, const char* argv[]); #ifdef __cplusplus } diff --git a/kernel/kernel_processes/monitor/monitor_processes.c b/kernel/kernel_processes/monitor/monitor_processes.c index 345dc5a4..22506f31 100644 --- a/kernel/kernel_processes/monitor/monitor_processes.c +++ b/kernel/kernel_processes/monitor/monitor_processes.c @@ -145,7 +145,7 @@ void draw_process_view(){ print_process_info(); } -void monitor_procs(){ +int monitor_procs(){ keypress kp = { .modifier = KEY_MOD_LALT, .keys[0] = 0x15//R @@ -163,13 +163,14 @@ void monitor_procs(){ if (active) draw_process_view(); } + return 1; } process_t* start_process_monitor(){ #if QEMU - return create_kernel_process("procmonitor",monitor_procs); + return create_kernel_process("procmonitor",monitor_procs, 0, 0); #else //TODO: disabled process monitor since shortcuts seem broken on rpi - return 0x0;//create_kernel_process("procmonitor",monitor_procs); + return 0x0;//create_kernel_process("procmonitor",monitor_procs, 0, 0); #endif } \ No newline at end of file diff --git a/kernel/kernel_processes/windows/windows.cpp b/kernel/kernel_processes/windows/windows.cpp index 000c2244..47c99bff 100644 --- a/kernel/kernel_processes/windows/windows.cpp +++ b/kernel/kernel_processes/windows/windows.cpp @@ -9,7 +9,7 @@ WindowManager manager; bool screen_overlay; -extern "C" void manage_windows(){ +extern "C" int manage_windows(int argc, char* argv[]){ manager.initialize(); while (1) { @@ -26,5 +26,5 @@ void resume_window_draw(){ } extern "C" process_t* start_windows(){ - return create_kernel_process("winmanager",manage_windows); + return create_kernel_process("winmanager",manage_windows, 0, 0); } \ No newline at end of file diff --git a/kernel/networking/network.cpp b/kernel/networking/network.cpp index effa3743..ed8dcf95 100644 --- a/kernel/networking/network.cpp +++ b/kernel/networking/network.cpp @@ -17,8 +17,9 @@ void network_handle_upload_interrupt() { if (dispatch) dispatch->handle_upload_interrupt(); } -void network_net_task_entry() { - if (dispatch) dispatch->net_task(); +int network_net_task_entry(int argc, char* argv[]) { + if (dispatch) return dispatch->net_task(); + return 0; } int net_tx_frame(uintptr_t frame_ptr, uint32_t frame_len) { diff --git a/kernel/networking/network.h b/kernel/networking/network.h index d68f6000..1726d7f7 100644 --- a/kernel/networking/network.h +++ b/kernel/networking/network.h @@ -18,7 +18,7 @@ uint16_t network_net_get_pid(); bool network_init(); void network_handle_download_interrupt(); void network_handle_upload_interrupt(); -void network_net_task_entry(); +int network_net_task_entry(int argc, char* argv[]); int net_tx_frame(uintptr_t frame_ptr, uint32_t frame_len); int net_rx_frame(sizedptr *out_frame); diff --git a/kernel/networking/network_dispatch.cpp b/kernel/networking/network_dispatch.cpp index 901b23e6..c49b0c04 100644 --- a/kernel/networking/network_dispatch.cpp +++ b/kernel/networking/network_dispatch.cpp @@ -79,20 +79,18 @@ bool NetworkDispatch::enqueue_frame(const sizedptr &frame) return true; } -void NetworkDispatch::net_task() +int NetworkDispatch::net_task() { for (;;) { bool did_work = false; sizedptr pkt; - //rx if (!rx_queue.is_empty() && rx_queue.dequeue(pkt)) { did_work = true; eth_input(pkt.ptr, pkt.size); free_frame(pkt); } - //tx if (!tx_queue.is_empty() && tx_queue.dequeue(pkt)) { did_work = true; driver->send_packet(pkt); @@ -101,6 +99,7 @@ void NetworkDispatch::net_task() if (!did_work) sleep(10); } + return 0; } bool NetworkDispatch::dequeue_packet_for(uint16_t pid, sizedptr *out) diff --git a/kernel/networking/network_dispatch.hpp b/kernel/networking/network_dispatch.hpp index 4093389c..35bba52d 100644 --- a/kernel/networking/network_dispatch.hpp +++ b/kernel/networking/network_dispatch.hpp @@ -15,7 +15,7 @@ class NetworkDispatch { void handle_download_interrupt(); void handle_upload_interrupt(); bool enqueue_frame(const sizedptr&); - void net_task(); + int net_task(); bool dequeue_packet_for(uint16_t, sizedptr*); void set_net_pid(uint16_t pid); diff --git a/kernel/networking/processes/net_proc.c b/kernel/networking/processes/net_proc.c index 8ce94e60..3cb4d808 100644 --- a/kernel/networking/processes/net_proc.c +++ b/kernel/networking/processes/net_proc.c @@ -277,41 +277,41 @@ void test_network() } } -void net_test_entry(){ +int net_test_entry(){ test_network(); - stop_current_process(); + return 0; } -void ip_waiter_entry() +int ip_waiter_entry() { for (;;) { const net_cfg_t *cfg = ipv4_get_cfg(); if (cfg && cfg->mode != NET_MODE_DISABLED && cfg->ip != 0) { - create_kernel_process("net_test", net_test_entry); + create_kernel_process("net_test", net_test_entry, 0, 0); break; } sleep(200); } - stop_current_process(); + return 0; } process_t* launch_net_process() { const net_cfg_t *cfg = ipv4_get_cfg(); - process_t* net = create_kernel_process("net_net", network_net_task_entry); + process_t* net = create_kernel_process("net_net", network_net_task_entry, 0, 0); network_net_set_pid(net ? net->id : 0xFFFF); - process_t* arp = create_kernel_process("arp_daemon", arp_daemon_entry); + process_t* arp = create_kernel_process("arp_daemon", arp_daemon_entry, 0, 0); arp_set_pid(arp ? arp->id : 0xFFFF); if (cfg && cfg->mode != NET_MODE_DISABLED && cfg->ip != 0) { - create_kernel_process("net_test", net_test_entry); + create_kernel_process("net_test", net_test_entry, 0, 0); return NULL; } - process_t* dhcp = create_kernel_process("dhcp_daemon", dhcp_daemon_entry); + process_t* dhcp = create_kernel_process("dhcp_daemon", dhcp_daemon_entry, 0, 0); dhcp_set_pid(dhcp ? dhcp->id : 0xFFFF); - create_kernel_process("ip_waiter", ip_waiter_entry); + create_kernel_process("ip_waiter", ip_waiter_entry, 0, 0); return dhcp; } diff --git a/kernel/process/syscall.c b/kernel/process/syscall.c index 107a6e6a..2db0d0c4 100644 --- a/kernel/process/syscall.c +++ b/kernel/process/syscall.c @@ -170,6 +170,15 @@ void sync_el0_handler_c(){ break; } } else { + switch (ec) { + case 0x21: + uint64_t far; + asm volatile ("mrs %0, far_el1" : "=r"(far)); + if (far == 0){ + kprintf("Process has exited %x",x0); + stop_current_process(); + } + } //We could handle more exceptions now, such as x25 (unmasked x96) = data abort if (currentEL == 1) handle_exception_with_info("UNEXPECTED EXCEPTION",ec); diff --git a/shared/net/application_layer/dhcp_daemon.c b/shared/net/application_layer/dhcp_daemon.c index 9fcc99c4..ff093f52 100644 --- a/shared/net/application_layer/dhcp_daemon.c +++ b/shared/net/application_layer/dhcp_daemon.c @@ -247,13 +247,13 @@ static void dhcp_fsm_once() if (old != g_state) log_state_change(old, g_state); } -void dhcp_daemon_entry(){ +int dhcp_daemon_entry(){ KP("[DHCP] daemon start pid=%i", get_current_proc_pid()); g_pid_dhcpd = (uint16_t)get_current_proc_pid(); g_sock = udp_socket_create(SOCK_ROLE_SERVER, g_pid_dhcpd); if(socket_bind_udp(g_sock, 68) != 0){ KP("[DHCP] bind failed\n"); - return; + return 1; } for(;;){ diff --git a/shared/net/application_layer/dhcp_daemon.h b/shared/net/application_layer/dhcp_daemon.h index fcf501dc..0b530d84 100644 --- a/shared/net/application_layer/dhcp_daemon.h +++ b/shared/net/application_layer/dhcp_daemon.h @@ -5,7 +5,7 @@ extern "C" { #endif -void dhcp_daemon_entry(); +int dhcp_daemon_entry(); uint16_t get_dhcp_pid(); bool dhcp_is_running(); void dhcp_set_pid(uint16_t pid); diff --git a/shared/net/link_layer/arp.c b/shared/net/link_layer/arp.c index 18eac6db..0e950e4f 100644 --- a/shared/net/link_layer/arp.c +++ b/shared/net/link_layer/arp.c @@ -153,7 +153,7 @@ bool arp_can_reply() { return (cfg && cfg->ip != 0 && cfg->mode != NET_MODE_DISABLED); } -void arp_daemon_entry() { +int arp_daemon_entry() { while (1){ const net_cfg_t *cfg = ipv4_get_cfg(); if(cfg && cfg->ip != 0 && cfg->mode != NET_MODE_DISABLED) break; diff --git a/shared/net/link_layer/arp.h b/shared/net/link_layer/arp.h index 4e5454a3..0066a149 100644 --- a/shared/net/link_layer/arp.h +++ b/shared/net/link_layer/arp.h @@ -43,9 +43,8 @@ void arp_table_init_static_defaults(); void arp_send_request(uint32_t target_ip); -void arp_daemon_entry(); +int arp_daemon_entry(); bool arp_can_reply(); -void arp_daemon_entry(); void arp_set_pid(uint16_t pid); uint16_t arp_get_pid(); void arp_input(uintptr_t frame_ptr, uint32_t frame_len); diff --git a/shared/std/string.c b/shared/std/string.c index b6c87e85..0966f311 100644 --- a/shared/std/string.c +++ b/shared/std/string.c @@ -124,6 +124,16 @@ bool string_equals(string a, string b){ return strcmp(a.data,b.data, false) == 0; } +string string_replace(const char *str, char orig, char repl){ + size_t str_size = strlen(str, 0); + char *buf = (char*)malloc(str_size+1); + for (size_t i = 0; i < str_size && str[i]; i++){ + buf[i] = str[i] == orig ? repl : str[i]; + } + buf[str_size] = 0; + return (string){ .data = buf, .length = str_size, .mem_length = str_size + 1}; +} + string string_format(const char *fmt, ...){ if (fmt == NULL) return (string){ .data = NULL, .length = 0, .mem_length = 0}; @@ -306,6 +316,15 @@ bool strcont(const char *a, const char *b){ return 0; } +int count_occurrences(const char* str, char c){ + int count = 0; + while (*str) { + if (*str == c) count++; + str++; + } + return count; +} + bool utf16tochar(uint16_t* str_in, char* out_str, size_t max_len){ size_t out_i = 0; for (size_t i = 0; i < max_len && str_in[i]; i++){ diff --git a/shared/std/string.h b/shared/std/string.h index 7f6a35f9..76dec23c 100644 --- a/shared/std/string.h +++ b/shared/std/string.h @@ -24,6 +24,7 @@ string string_ca_max(const char *array, uint32_t max_length); string string_c(const char c); string string_from_hex(uint64_t value); bool string_equals(string a, string b); +string string_replace(const char *str, char orig, char repl); string string_format(const char *fmt, ...); string string_format_va(const char *fmt, va_list args); __attribute__((used)) @@ -37,6 +38,7 @@ bool strcont(const char *a, const char *b); int strstart(const char *a, const char *b, bool case_insensitive); int strend(const char *a, const char *b, bool case_insensitive); int strindex(const char *a, const char *b); +int count_occurrences(const char* str, char c); uint64_t parse_hex_u64(const char* str, size_t size); uint64_t parse_int_u64(const char* str, size_t size); From 54867886fb2a04ad2333ffc6e9dcafa8feba3e17 Mon Sep 17 00:00:00 2001 From: differrari Date: Sun, 10 Aug 2025 00:00:00 +0000 Subject: [PATCH 39/50] [CAT] added usage and error messages to cat --- kernel/bin/cat.c | 25 ++++++++++++++++--------- kernel/console/kconsole/terminal.cpp | 1 - kernel/dev/module_loader.c | 2 +- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/kernel/bin/cat.c b/kernel/bin/cat.c index 38bc0750..04562cd5 100644 --- a/kernel/bin/cat.c +++ b/kernel/bin/cat.c @@ -6,23 +6,30 @@ #include "console/kio.h" int run_cat(int argc, char* argv[]){ - if (argc != 2) return 2; - kprintf("Cat with %i arguments. %s", argc, argv[0]); + uint16_t pid = get_current_proc_pid(); + string s = string_format("/proc/%i/out",pid); + file fd2; + open_file(s.data, &fd2); + if (argc != 2){ + string err_msg = string_l("Usage cat "); + write_file(&fd2, err_msg.data, err_msg.length); + free(err_msg.data, err_msg.mem_length); + return 2; + } const char* path = argv[0]; - kprintf("Cat with %i arguments. %s", argc, argv[1]); size_t size = parse_int_u64(argv[1], UINT32_MAX); file fd; open_file(path, &fd); - if (fd.size == 0) return 1; + if (fd.size == 0){ + string err_msg = string_format("Couldn't find file %s", argv[0]); + write_file(&fd2, err_msg.data, err_msg.length); + free(err_msg.data, err_msg.mem_length); + return 1; + } if (size == 0) size = fd.size; char* buf = (char*)malloc(size); read_file(&fd, buf, size); - uint16_t pid = get_current_proc_pid(); - - string s = string_format("/proc/%i/out",pid); - file fd2; - open_file(s.data, &fd2); write_file(&fd2, buf, size); free(s.data, s.mem_length); return 0; diff --git a/kernel/console/kconsole/terminal.cpp b/kernel/console/kconsole/terminal.cpp index f8efbe44..8dcd89f6 100644 --- a/kernel/console/kconsole/terminal.cpp +++ b/kernel/console/kconsole/terminal.cpp @@ -73,7 +73,6 @@ void Terminal::run_command(){ cmd = string_ca_max(fullcmd, args - fullcmd - 1); args_copy = string_l(args); argv = parse_arguments(args_copy.data, &argc); - kprintf("There are %i arguments",argc); } put_char('\r'); diff --git a/kernel/dev/module_loader.c b/kernel/dev/module_loader.c index 05e72ca4..fd3b5bcf 100644 --- a/kernel/dev/module_loader.c +++ b/kernel/dev/module_loader.c @@ -27,5 +27,5 @@ int fs_search(void *node, void *key){ driver_module* get_module(const char **full_path){ clinkedlist_node_t *node = clinkedlist_find(modules, (void*)full_path, fs_search); - return ((driver_module*)node->data); + return node ? ((driver_module*)node->data) : 0; } \ No newline at end of file From 8cb9e8aa664d6befb16e34998970a3c96979672d Mon Sep 17 00:00:00 2001 From: differrari Date: Sun, 10 Aug 2025 00:00:00 +0000 Subject: [PATCH 40/50] [PROC] added exit codes --- kernel/console/kconsole/terminal.cpp | 3 +++ kernel/kernel_processes/boot/bootscreen.c | 2 +- kernel/kernel_processes/boot/login_screen.c | 2 +- kernel/networking/processes/net_proc.c | 6 +++--- kernel/process/process.h | 1 + kernel/process/scheduler.c | 8 +++++--- kernel/process/scheduler.h | 4 ++-- kernel/process/syscall.c | 8 ++++---- shared/syscalls/syscalls.h | 2 +- user/default_process.c | 3 ++- 10 files changed, 23 insertions(+), 16 deletions(-) diff --git a/kernel/console/kconsole/terminal.cpp b/kernel/console/kconsole/terminal.cpp index 8dcd89f6..72a69086 100644 --- a/kernel/console/kconsole/terminal.cpp +++ b/kernel/console/kconsole/terminal.cpp @@ -35,6 +35,9 @@ void Terminal::TMP_cat(int argc, const char *args[]){ put_string(buf); free(buf, amount); } + string exit_msg = string_format("Process %i ended with exit code %i.",cat->id, cat->exit_code); + put_string(exit_msg.data); + free(exit_msg.data, exit_msg.mem_length); } const char** Terminal::parse_arguments(char *args, int *count){ diff --git a/kernel/kernel_processes/boot/bootscreen.c b/kernel/kernel_processes/boot/bootscreen.c index 783a979f..2c5cafe3 100644 --- a/kernel/kernel_processes/boot/bootscreen.c +++ b/kernel/kernel_processes/boot/bootscreen.c @@ -88,7 +88,7 @@ void boot_draw_lines(gpu_point current_point, gpu_point next_point, gpu_size siz keypress kp; if (sys_read_input_current(&kp)) if (kp.keys[0] != 0){ - stop_current_process(); + stop_current_process(0); } gpu_flush(); } diff --git a/kernel/kernel_processes/boot/login_screen.c b/kernel/kernel_processes/boot/login_screen.c index 4042974d..8561078d 100644 --- a/kernel/kernel_processes/boot/login_screen.c +++ b/kernel/kernel_processes/boot/login_screen.c @@ -48,7 +48,7 @@ int login_screen(){ free(title.data,title.mem_length); free(subtitle.data,subtitle.mem_length); sys_set_secure(false); - stop_current_process(); + stop_current_process(0); } else break; } diff --git a/kernel/networking/processes/net_proc.c b/kernel/networking/processes/net_proc.c index 3cb4d808..61a92f29 100644 --- a/kernel/networking/processes/net_proc.c +++ b/kernel/networking/processes/net_proc.c @@ -115,20 +115,20 @@ void http_server_hello_entry() uint16_t pid = get_current_proc_pid(); http_server_handle_t srv = http_server_create(pid); if (!srv) { - stop_current_process(); + stop_current_process(1); return; } if (http_server_bind(srv, 80) < 0) { http_server_destroy(srv); - stop_current_process(); + stop_current_process(2); return; } if (http_server_listen(srv, 4) < 0) { http_server_close(srv); http_server_destroy(srv); - stop_current_process(); + stop_current_process(3); return; } diff --git a/kernel/process/process.h b/kernel/process/process.h index b607b4b9..c834870a 100644 --- a/kernel/process/process.h +++ b/kernel/process/process.h @@ -39,6 +39,7 @@ typedef struct { uintptr_t heap; uintptr_t output; file out_fd; + uint64_t exit_code; bool focused; enum process_state { STOPPED, READY, RUNNING, BLOCKED } state; input_buffer_t input_buffer; diff --git a/kernel/process/scheduler.c b/kernel/process/scheduler.c index bb8a9dd8..b9366778 100644 --- a/kernel/process/scheduler.c +++ b/kernel/process/scheduler.c @@ -114,6 +114,7 @@ void reset_process(process_t *proc){ pfree((void*)proc->stack-proc->stack_size,proc->stack_size); proc->pc = 0; proc->spsr = 0; + proc->exit_code = 0; for (int j = 0; j < 31; j++) proc->regs[j] = 0; for (int k = 0; k < MAX_PROC_NAME_LENGTH; k++) @@ -179,11 +180,12 @@ void name_process(process_t *proc, const char *name){ proc->name[i] = name[i]; } -void stop_process(uint16_t pid){ +void stop_process(uint16_t pid, uint32_t exit_code){ disable_interrupt(); process_t *proc = get_proc_by_pid(pid); if (proc->state != READY) return; proc->state = STOPPED; + proc->exit_code = exit_code; if (proc->focused) sys_unset_focus(); //TODO: we don't wipe the process' data. If we do, we corrupt our sp, since we're still in the process' sp. @@ -192,9 +194,9 @@ void stop_process(uint16_t pid){ switch_proc(HALT); } -void stop_current_process(){ +void stop_current_process(uint32_t exit_code){ disable_interrupt(); - stop_process(processes[current_proc].id); + stop_process(processes[current_proc].id, exit_code); } uint16_t process_count(){ diff --git a/kernel/process/scheduler.h b/kernel/process/scheduler.h index 455d7f61..daf139f5 100644 --- a/kernel/process/scheduler.h +++ b/kernel/process/scheduler.h @@ -21,8 +21,8 @@ process_t* init_process(); void save_syscall_return(uint64_t value); void process_restore(); -void stop_process(uint16_t pid); -void stop_current_process(); +void stop_process(uint16_t pid, uint32_t exit_code); +void stop_current_process(uint32_t exit_code); void name_process(process_t *proc, const char *name); diff --git a/kernel/process/syscall.c b/kernel/process/syscall.c index 2db0d0c4..3e8d6eac 100644 --- a/kernel/process/syscall.c +++ b/kernel/process/syscall.c @@ -129,7 +129,7 @@ void sync_el0_handler_c(){ break; case 33: - stop_current_process(); + stop_current_process(x0); break; case 40: @@ -176,17 +176,17 @@ void sync_el0_handler_c(){ asm volatile ("mrs %0, far_el1" : "=r"(far)); if (far == 0){ kprintf("Process has exited %x",x0); - stop_current_process(); + stop_current_process(x0); } } - //We could handle more exceptions now, such as x25 (unmasked x96) = data abort + //We could handle more exceptions now, such as x25 (unmasked x96) = data abort. 0x21 at end of 0x25 = alignment fault if (currentEL == 1) handle_exception_with_info("UNEXPECTED EXCEPTION",ec); else { uint64_t far; asm volatile ("mrs %0, far_el1" : "=r"(far)); kprintf("Process has crashed. ESR: %x. ELR: %x. FAR: %x", esr, elr, far); - stop_current_process(); + stop_current_process(ec); } } syscall_depth--; diff --git a/shared/syscalls/syscalls.h b/shared/syscalls/syscalls.h index 673a8ea5..c31154ea 100644 --- a/shared/syscalls/syscalls.h +++ b/shared/syscalls/syscalls.h @@ -18,7 +18,7 @@ extern void free(void *ptr, size_t size); extern bool read_key(keypress *kp); extern void sleep(uint64_t time); -extern void halt(); +extern void halt(uint32_t exit_code); extern void clear_screen(color color); extern void gpu_flush_data(); diff --git a/user/default_process.c b/user/default_process.c index c75b6dad..599660da 100644 --- a/user/default_process.c +++ b/user/default_process.c @@ -11,7 +11,7 @@ void proc_func() { printf("Print console test %f", (get_time()/1000.f)); while (read_key(&kp)){ if (kp.keys[0] == KEY_ESC) - halt(); + halt(0); } clear_screen(0xFFFFFFFF); draw_primitive_rect(&rect, 0xFF222233); @@ -20,4 +20,5 @@ void proc_func() { free(s.data,s.mem_length); gpu_flush_data(); } + halt(1); } \ No newline at end of file From 25b93255e525249b0d504970a21ca356a664d624 Mon Sep 17 00:00:00 2001 From: differrari Date: Sun, 10 Aug 2025 00:00:00 +0000 Subject: [PATCH 41/50] [CONSOLE] load commands from bin --- kernel/bin/bin_mod.c | 52 ++++++++++++++++++++++++++++ kernel/bin/bin_mod.h | 16 +++++++++ kernel/bin/cat.c | 4 --- kernel/bin/cat.h | 2 +- kernel/console/kconsole/terminal.cpp | 30 ++++++++-------- kernel/console/kconsole/terminal.hpp | 2 +- 6 files changed, 86 insertions(+), 20 deletions(-) create mode 100644 kernel/bin/bin_mod.c create mode 100644 kernel/bin/bin_mod.h diff --git a/kernel/bin/bin_mod.c b/kernel/bin/bin_mod.c new file mode 100644 index 00000000..98cf49e8 --- /dev/null +++ b/kernel/bin/bin_mod.c @@ -0,0 +1,52 @@ +#include "bin_mod.h" +#include "cat.h" +#include "kernel_processes/kprocess_loader.h" + +#define N_ARR(arr) (sizeof(arr)/sizeof((arr)[0])) + +bool init_bin(){ + return true; +} + +typedef struct open_bin_ref { + char *name; + int (*func)(int argc, char* argv[]); +} open_bin_ref; + +open_bin_ref available_cmds[] = { + { "cat", run_cat } +}; + +process_t* exec(const char* prog_name, int argc, const char* argv[]){ + for (uint32_t i = 0; i < N_ARR(available_cmds); i++){ + if (strcmp(available_cmds[i].name, prog_name, false) == 0){ + return create_kernel_process(available_cmds[i].name, available_cmds[i].func, argc, argv); + } + } + return 0; +} + +FS_RESULT open_bin(){ + return FS_RESULT_DRIVER_ERROR; +} + +size_t read_bin(){ + return 0; +} + +sizedptr list_bin(const char *path){ + return (sizedptr){0,0}; +} + +driver_module bin_module = (driver_module){ + .name = "bin", + .mount = "/bin", + .version = VERSION_NUM(0, 1, 0, 1), + .init = init_bin, + .fini = 0, + .open = 0, + .read = read_bin, + .write = 0, + .seek = 0, + .readdir = list_bin, +}; \ No newline at end of file diff --git a/kernel/bin/bin_mod.h b/kernel/bin/bin_mod.h new file mode 100644 index 00000000..9edc457b --- /dev/null +++ b/kernel/bin/bin_mod.h @@ -0,0 +1,16 @@ +#pragma once + +#include "dev/driver_base.h" +#include "process/process.h" + +#ifdef __cplusplus +extern "C" { +#endif + +process_t* exec(const char* prog_name, int argc, const char* argv[]); + +#ifdef __cplusplus +} +#endif + +extern driver_module bin_module; \ No newline at end of file diff --git a/kernel/bin/cat.c b/kernel/bin/cat.c index 04562cd5..050f7515 100644 --- a/kernel/bin/cat.c +++ b/kernel/bin/cat.c @@ -33,8 +33,4 @@ int run_cat(int argc, char* argv[]){ write_file(&fd2, buf, size); free(s.data, s.mem_length); return 0; -} - -process_t* create_cat_process(int argc, const char *argv[]){ - return create_kernel_process("cat", run_cat, argc, argv); } \ No newline at end of file diff --git a/kernel/bin/cat.h b/kernel/bin/cat.h index f7730899..9a517a31 100644 --- a/kernel/bin/cat.h +++ b/kernel/bin/cat.h @@ -6,7 +6,7 @@ extern "C" { #endif -process_t* create_cat_process(int argc, const char *argv[]); +int run_cat(int argc, char* argv[]); #ifdef __cplusplus } diff --git a/kernel/console/kconsole/terminal.cpp b/kernel/console/kconsole/terminal.cpp index 72a69086..d4974bb5 100644 --- a/kernel/console/kconsole/terminal.cpp +++ b/kernel/console/kconsole/terminal.cpp @@ -4,7 +4,7 @@ #include "../serial/uart.h" #include "std/std.hpp" #include "filesystem/filesystem.h" -#include "bin/cat.h" +#include "bin/bin_mod.h" void Terminal::update(){ if (!command_running) handle_input(); @@ -22,22 +22,24 @@ void Terminal::end_command(){ set_text_color(default_text_color); } -void Terminal::TMP_cat(int argc, const char *args[]){ - process_t *cat = create_cat_process(argc, args); - string s = string_format("/proc/%i/out",cat->id); +bool Terminal::exec_cmd(const char *cmd, int argc, const char *argv[]){ + process_t *proc = exec(cmd, argc, argv); + if (!proc) return false; + string s = string_format("/proc/%i/out",proc->id); file fd; open_file(s.data, &fd); free(s.data, s.mem_length); - while (cat->state != process_t::STOPPED){ + while (proc->state != process_t::STOPPED){ size_t amount = 0x100; char *buf = (char*)malloc(amount); read_file(&fd, buf, amount); put_string(buf); free(buf, amount); } - string exit_msg = string_format("Process %i ended with exit code %i.",cat->id, cat->exit_code); + string exit_msg = string_format("Process %i ended with exit code %i.",proc->id, proc->exit_code); put_string(exit_msg.data); free(exit_msg.data, exit_msg.mem_length); + return true; } const char** Terminal::parse_arguments(char *args, int *count){ @@ -81,14 +83,14 @@ void Terminal::run_command(){ put_char('\r'); put_char('\n'); - if (strcmp(cmd.data, "cat", true) == 0) - TMP_cat(argc, argv); - else if (strcmp(cmd.data, "test", true) == 0) - TMP_test(argc, argv); - else { - string s = string_format("Unknown command %s with args %s", cmd.data, args); - put_string(s.data); - free(s.data, s.mem_length); + if (!exec_cmd(cmd.data, argc, argv)){ + if (strcmp(cmd.data, "test", true) == 0){ + TMP_test(argc, argv); + } else { + string s = string_format("Unknown command %s with args %s", cmd.data, args); + put_string(s.data); + free(s.data, s.mem_length); + } } free(cmd.data, cmd.mem_length); diff --git a/kernel/console/kconsole/terminal.hpp b/kernel/console/kconsole/terminal.hpp index a503f171..972c2188 100644 --- a/kernel/console/kconsole/terminal.hpp +++ b/kernel/console/kconsole/terminal.hpp @@ -12,7 +12,7 @@ class Terminal: public KernelConsole { const char** parse_arguments(char *args, int *count); //TODO: proper commands - void TMP_cat(int argc, const char *args[]); + bool exec_cmd(const char *cmd, int argc, const char *args[]); void TMP_test(int argc, const char *args[]); bool command_running; From a702bc0a653914427b4fe97ec7649324ec544656 Mon Sep 17 00:00:00 2001 From: Matthew Brush Date: Sun, 10 Aug 2025 12:18:30 -0700 Subject: [PATCH 42/50] Use GCC dependency generation in Makefiles This causes source files which depend on headers to be rebuilt when the header changes. `-MMD` generates the dependencies in `*.d` files, excluding system headers. `-MP` adds fake rules for headers to the dependencies file so build doesn't break if a header is removed. Update `.gitignore` to ignore new `*.d` files. --- .gitignore | 1 + kernel/Makefile | 12 ++++++++---- shared/Makefile | 12 ++++++++---- user/Makefile | 12 ++++++++---- 4 files changed, 25 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index d25af32d..cb75e7f2 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,4 @@ dump .cache/* compile_commands.json .clangd +*.d diff --git a/kernel/Makefile b/kernel/Makefile index b50e98e6..8d936996 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -9,11 +9,13 @@ endif LDFLAGS := $(LDFLAGS_BASE) -T $(shell ls *.ld) --defsym=LOAD_ADDR=$(LOAD_ADDR) CLEAN_OBJS := $(shell find . -name '*.o') +CLEAN_DEPS := $(shell find . -name '*.d') C_SRC := $(shell find . -name '*.c') ASM_SRC := $(shell find . -name '*.S') CPP_SRC := $(shell find . -name '*.cpp') OBJ := $(C_SRC:.c=.o) $(ASM_SRC:.S=.o) $(CPP_SRC:.cpp=.o) OBJL := $(filter-out ./boot.o,$(OBJ)) +DEP := $(C_SRC:.c=.d) $(ASM_SRC:.S=.d) $(CPP_SRC:.cpp=.d) ELF := ../kernel.elf TARGET := ../kernel.img @@ -25,11 +27,13 @@ $(TARGET): ../shared/libshared.a $(OBJ) $(OBJCOPY) -O binary $(ELF) $@ %.o: %.S - $(CC) $(CFLAGS) -c $< -o $@ + $(CC) $(CFLAGS) -c -MMD -MP $< -o $@ %.o: %.c - $(CC) $(CFLAGS) $(CONLY_FLAGS_BASE) -c $< -o $@ + $(CC) $(CFLAGS) $(CONLY_FLAGS_BASE) -c -MMD -MP $< -o $@ %.o: %.cpp - $(CC) $(CFLAGS) -fno-rtti -c $< -o $@ + $(CC) $(CFLAGS) -fno-rtti -c -MMD -MP $< -o $@ clean: - rm -f $(CLEAN_OBJS) $(ELF) $(TARGET) + rm -f $(CLEAN_OBJS) $(CLEAN_DEPS) $(ELF) $(TARGET) + +-include $(DEP) diff --git a/shared/Makefile b/shared/Makefile index 36a78307..d815120d 100644 --- a/shared/Makefile +++ b/shared/Makefile @@ -2,10 +2,12 @@ CFLAGS := $(CFLAGS_BASE) -I. -I../kernel -Wno-unused-parameter CLEAN_OBJS := $(shell find . -name '*.o') +CLEAN_DEPS := $(shell find . -name '*.d') C_SRC := $(shell find . -name '*.c') CPP_SRC := $(shell find . -name '*.cpp') ASM_SRC := $(shell find . -name '*.S') OBJ := $(C_SRC:.c=.o) $(ASM_SRC:.S=.o) $(CPP_SRC:.cpp=.o) +DEP := $(C_SRC:.c=.d) $(ASM_SRC:.S=.d) $(CPP_SRC:.cpp=.d) TARGET := libshared.a @@ -17,13 +19,15 @@ $(TARGET): $(OBJ) $(AR) rcs $@ $^ %.o: %.S - $(CC) $(CFLAGS) -c $< -o $@ + $(CC) $(CFLAGS) -c -MMD -MP $< -o $@ %.o: %.c - $(CC) $(CFLAGS) $(CONLY_FLAGS_BASE) -c $< -o $@ + $(CC) $(CFLAGS) $(CONLY_FLAGS_BASE) -c -MMD -MP $< -o $@ %.o: %.cpp - $(CC) $(CFLAGS) -fno-rtti -c $< -o $@ + $(CC) $(CFLAGS) -fno-rtti -c -MMD -MP $< -o $@ clean: - rm -f $(CLEAN_OBJS) $(TARGET) + rm -f $(CLEAN_OBJS) $(CLEAN_DEPS) $(TARGET) + +-include $(DEP) diff --git a/user/Makefile b/user/Makefile index 9aa864f1..3f9ce25e 100644 --- a/user/Makefile +++ b/user/Makefile @@ -3,9 +3,11 @@ CFLAGS := $(CFLAGS_BASE) -I. -I../shared -Wno-unused-parameter LDFLAGS := -T $(shell ls *.ld) CLEAN_OBJS := $(shell find . -name '*.o') +CLEAN_DEPS := $(shell find . -name '*.d') C_SRC := $(shell find . -name '*.c') CPP_SRC := $(shell find . -name '*.cpp') OBJ := $(C_SRC:.c=.o) $(CPP_SRC:.cpp=.o) +DEP := $(C_SRC:.c=.d) $(CPP_SRC:.cpp=.d) NAME := $(notdir $(CURDIR)) ELF := $(NAME).elf @@ -21,13 +23,15 @@ $(LOCATION)$(TARGET): $(OBJ) $(OBJCOPY) -O binary $(LOCATION)$(ELF) $@ %.o: %.S - $(CC) $(CFLAGS) -c $< -o $@ + $(CC) $(CFLAGS) -c -MMD -MP $< -o $@ %.o: %.c - $(CC) $(CFLAGS) $(CONLY_FLAGS_BASE) -c $< -o $@ + $(CC) $(CFLAGS) $(CONLY_FLAGS_BASE) -c -MMD -MP $< -o $@ %.o: %.cpp - $(CC) $(CFLAGS) -fno-rtti -c $< -o $@ + $(CC) $(CFLAGS) -fno-rtti -c -MMD -MP $< -o $@ clean: - rm -f $(CLEAN_OBJS) $(TARGET) + rm -f $(CLEAN_OBJS) $(CLEAN_DEPS) $(TARGET) + +-include $(DEP) From f33d27f8fa1d48fbc01b04cab718d8e87b2e85ec Mon Sep 17 00:00:00 2001 From: Matthew Brush Date: Sun, 10 Aug 2025 12:38:30 -0700 Subject: [PATCH 43/50] Use ARCH make variable in dump target --- Makefile | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 282b54e6..e7dfd903 100644 --- a/Makefile +++ b/Makefile @@ -63,11 +63,11 @@ run: debug: $(MAKE) $(MODE) ./rundebug MODE=$(MODE) $(ARGS) - + dump: $(OBJCOPY) -O binary kernel.elf kernel.img - aarch64-none-elf-objdump -D kernel.elf > dump - + $(ARCH)-objdump -D kernel.elf > dump + install: $(MAKE) clean $(MAKE) LOAD_ADDR=0x80000 XHCI_CTX_SIZE=64 QEMU=false all From 939a9999218017e3d60ad98647600d4e2b5b7c35 Mon Sep 17 00:00:00 2001 From: Matthew Brush Date: Sun, 10 Aug 2025 12:42:27 -0700 Subject: [PATCH 44/50] Use building make variable RM (for `rm -f`) --- Makefile | 4 ++-- kernel/Makefile | 2 +- shared/Makefile | 2 +- user/Makefile | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index e7dfd903..11de4c4c 100644 --- a/Makefile +++ b/Makefile @@ -46,9 +46,9 @@ clean: $(MAKE) -C user clean $(MAKE) -C kernel clean @echo "removing fs dirs" - rm -rf $(FS_DIRS) + $(RM) -r $(FS_DIRS) @echo "removing images" - rm -f kernel.img kernel.elf disk.img dump + $(RM) kernel.img kernel.elf disk.img dump raspi: $(MAKE) LOAD_ADDR=0x80000 XHCI_CTX_SIZE=64 QEMU=true all diff --git a/kernel/Makefile b/kernel/Makefile index 8d936996..c1d2f4e6 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -34,6 +34,6 @@ $(TARGET): ../shared/libshared.a $(OBJ) $(CC) $(CFLAGS) -fno-rtti -c -MMD -MP $< -o $@ clean: - rm -f $(CLEAN_OBJS) $(CLEAN_DEPS) $(ELF) $(TARGET) + $(RM) $(CLEAN_OBJS) $(CLEAN_DEPS) $(ELF) $(TARGET) -include $(DEP) diff --git a/shared/Makefile b/shared/Makefile index d815120d..c9af41dd 100644 --- a/shared/Makefile +++ b/shared/Makefile @@ -28,6 +28,6 @@ $(TARGET): $(OBJ) $(CC) $(CFLAGS) -fno-rtti -c -MMD -MP $< -o $@ clean: - rm -f $(CLEAN_OBJS) $(CLEAN_DEPS) $(TARGET) + $(RM) $(CLEAN_OBJS) $(CLEAN_DEPS) $(TARGET) -include $(DEP) diff --git a/user/Makefile b/user/Makefile index 3f9ce25e..818baaa9 100644 --- a/user/Makefile +++ b/user/Makefile @@ -32,6 +32,6 @@ $(LOCATION)$(TARGET): $(OBJ) $(CC) $(CFLAGS) -fno-rtti -c -MMD -MP $< -o $@ clean: - rm -f $(CLEAN_OBJS) $(CLEAN_DEPS) $(TARGET) + $(RM) $(CLEAN_OBJS) $(CLEAN_DEPS) $(TARGET) -include $(DEP) From e832de3ec1f279ada60ddeedcc345af613e933ee Mon Sep 17 00:00:00 2001 From: Matthew Brush Date: Sun, 10 Aug 2025 13:21:11 -0700 Subject: [PATCH 45/50] Split C and C++ compilers and flags Instead of using `gcc` to compile both C and C++, use `gcc` for C and `g++` for C++. This allows to split flags more cleanly. Re-organize flag variables a bit, adding `CPPFLAGS` for C preprocessor flags, `CFLAGS` for C compiler flags and `CXXFLAGS` for C++ compiler flags. --- Makefile | 16 ++++++++++------ kernel/Makefile | 15 ++++++++++----- shared/Makefile | 8 +++++--- user/Makefile | 10 ++++++---- 4 files changed, 31 insertions(+), 18 deletions(-) diff --git a/Makefile b/Makefile index 11de4c4c..ab1b652c 100644 --- a/Makefile +++ b/Makefile @@ -1,21 +1,25 @@ ARCH ?= aarch64-none-elf CC := $(ARCH)-gcc +CXX := $(ARCH)-g++ LD := $(ARCH)-ld AR := $(ARCH)-ar OBJCOPY := $(ARCH)-objcopy -CFLAGS_BASE ?= -g -O0 -nostdlib -ffreestanding \ - -fno-exceptions -fno-unwind-tables -fno-asynchronous-unwind-tables \ - -Wall -Wextra -Wno-unused-parameter -Wno-address-of-packed-member -mcpu=cortex-a72 -CONLY_FLAGS_BASE ?= -std=c17 -LDFLAGS_BASE ?= +COMMON_FLAGS ?= -ffreestanding -nostdlib -fno-exceptions -fno-unwind-tables \ + -fno-asynchronous-unwind-tables -g -O0 -Wall -Wextra \ + -Wno-unused-parameter -Wno-address-of-packed-member \ + -mcpu=cortex-a72 + +CFLAGS_BASE ?= $(COMMON_FLAGS) -std=c17 +CXXFLAGS_BASE ?= $(COMMON_FLAGS) -fno-rtti +LDFLAGS_BASE ?= LOAD_ADDR ?= 0x41000000 XHCI_CTX_SIZE ?= 32 QEMU ?= true MODE ?= virt -export ARCH CC LD AR OBJCOPY CFLAGS_BASE CONLY_FLAGS_BASE LDFLAGS_BASE LOAD_ADDR XHCI_CTX_SIZE QEMU +export ARCH CC CXX LD AR OBJCOPY COMMON_FLAGS CFLAGS_BASE CXXFLAGS_BASE LDFLAGS_BASE LOAD_ADDR XHCI_CTX_SIZE QEMU OS := $(shell uname) FS_DIRS := fs/redos/user diff --git a/kernel/Makefile b/kernel/Makefile index c1d2f4e6..a32a8108 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -1,12 +1,15 @@ #kernel # toolchain (inherited from top-level) -CFLAGS := $(CFLAGS_BASE) -I. -I../shared -I../user -DXHCI_CTX_SIZE=$(XHCI_CTX_SIZE) +CPPFLAGS := -I. -I../shared -I../user -DXHCI_CTX_SIZE=$(XHCI_CTX_SIZE) + ifeq ($(QEMU),true) - CFLAGS += -DQEMU + CPPFLAGS += -DQEMU endif -LDFLAGS := $(LDFLAGS_BASE) -T $(shell ls *.ld) --defsym=LOAD_ADDR=$(LOAD_ADDR) +CFLAGS := $(CFLAGS_BASE) $(CPPFLAGS) +CXXFLAGS := $(CXXFLAGS_BASE) $(CPPFLAGS) +LDFLAGS := $(LDFLAGS_BASE) -T $(shell ls *.ld) --defsym=LOAD_ADDR=$(LOAD_ADDR) CLEAN_OBJS := $(shell find . -name '*.o') CLEAN_DEPS := $(shell find . -name '*.d') @@ -28,10 +31,12 @@ $(TARGET): ../shared/libshared.a $(OBJ) %.o: %.S $(CC) $(CFLAGS) -c -MMD -MP $< -o $@ + %.o: %.c - $(CC) $(CFLAGS) $(CONLY_FLAGS_BASE) -c -MMD -MP $< -o $@ + $(CC) $(CFLAGS) -c -MMD -MP $< -o $@ + %.o: %.cpp - $(CC) $(CFLAGS) -fno-rtti -c -MMD -MP $< -o $@ + $(CXX) $(CXXFLAGS) -c -MMD -MP $< -o $@ clean: $(RM) $(CLEAN_OBJS) $(CLEAN_DEPS) $(ELF) $(TARGET) diff --git a/shared/Makefile b/shared/Makefile index c9af41dd..7814cced 100644 --- a/shared/Makefile +++ b/shared/Makefile @@ -1,5 +1,7 @@ #shared -CFLAGS := $(CFLAGS_BASE) -I. -I../kernel -Wno-unused-parameter +CPPFLAGS := -I. -I../kernel +CFLAGS := $(CFLAGS_BASE) $(CPPFLAGS) +CXXFLAGS := $(CXXFLAGS_BASE) $(CPPFLAGS) CLEAN_OBJS := $(shell find . -name '*.o') CLEAN_DEPS := $(shell find . -name '*.d') @@ -22,10 +24,10 @@ $(TARGET): $(OBJ) $(CC) $(CFLAGS) -c -MMD -MP $< -o $@ %.o: %.c - $(CC) $(CFLAGS) $(CONLY_FLAGS_BASE) -c -MMD -MP $< -o $@ + $(CC) $(CFLAGS) -c -MMD -MP $< -o $@ %.o: %.cpp - $(CC) $(CFLAGS) -fno-rtti -c -MMD -MP $< -o $@ + $(CXX) $(CXXFLAGS) -c -MMD -MP $< -o $@ clean: $(RM) $(CLEAN_OBJS) $(CLEAN_DEPS) $(TARGET) diff --git a/user/Makefile b/user/Makefile index 818baaa9..5c1c88a7 100644 --- a/user/Makefile +++ b/user/Makefile @@ -1,6 +1,8 @@ #user -CFLAGS := $(CFLAGS_BASE) -I. -I../shared -Wno-unused-parameter -LDFLAGS := -T $(shell ls *.ld) +CPPFLAGS := -I. -I../shared +CFLAGS := $(CFLAGS_BASE) $(CPPFLAGS) +CXXFLAGS := $(CXXFLAGS_BASE) $(CPPFLAGS) +LDFLAGS := -T $(shell ls *.ld) CLEAN_OBJS := $(shell find . -name '*.o') CLEAN_DEPS := $(shell find . -name '*.d') @@ -26,10 +28,10 @@ $(LOCATION)$(TARGET): $(OBJ) $(CC) $(CFLAGS) -c -MMD -MP $< -o $@ %.o: %.c - $(CC) $(CFLAGS) $(CONLY_FLAGS_BASE) -c -MMD -MP $< -o $@ + $(CC) $(CFLAGS) -c -MMD -MP $< -o $@ %.o: %.cpp - $(CC) $(CFLAGS) -fno-rtti -c -MMD -MP $< -o $@ + $(CXX) $(CXXFLAGS) -c -MMD -MP $< -o $@ clean: $(RM) $(CLEAN_OBJS) $(CLEAN_DEPS) $(TARGET) From 4070d03c632f005a159d1186d236fdcdd4ea031c Mon Sep 17 00:00:00 2001 From: Matthew Brush Date: Sun, 10 Aug 2025 13:46:44 -0700 Subject: [PATCH 46/50] Implement "silent" Make rules by default This makes it easier to see the build output/warnings. Full build output can be enabled with `make V=1`. --- Makefile | 22 ++++++++++++++++++++-- kernel/Makefile | 8 ++++---- shared/Makefile | 8 ++++---- user/Makefile | 8 ++++---- 4 files changed, 32 insertions(+), 14 deletions(-) diff --git a/Makefile b/Makefile index ab1b652c..152dbeb9 100644 --- a/Makefile +++ b/Makefile @@ -19,7 +19,23 @@ XHCI_CTX_SIZE ?= 32 QEMU ?= true MODE ?= virt -export ARCH CC CXX LD AR OBJCOPY COMMON_FLAGS CFLAGS_BASE CXXFLAGS_BASE LDFLAGS_BASE LOAD_ADDR XHCI_CTX_SIZE QEMU +ifeq ($(V), 1) + VAR = $(AR) + VAS = $(CC) + VCC = $(CC) + VCXX = $(CXX) + VLD = $(LD) +else + VAR = @echo " [AR] $@" && $(AR) + VAS = @echo " [AS] $@" && $(CC) + VCC = @echo " [CC] $@" && $(CC) + VCXX = @echo " [CXX] $@" && $(CXX) + VLD = @echo " [LD] $@" && $(LD) +endif + +export AR AS CC CXX LD OBJCOPY +export VAR VAS VCC VCXX VLD +export ARCH COMMON_FLAGS CFLAGS_BASE CXXFLAGS_BASE LDFLAGS_BASE LOAD_ADDR XHCI_CTX_SIZE QEMU OS := $(shell uname) FS_DIRS := fs/redos/user @@ -93,4 +109,6 @@ help: make debug build and run with debugger\n\ make dump disassemble kernel.elf\n\ make install create raspi kernel and mount it on a bootable partition\n\ - make prepare-fs create directories for the filesystem\n\n" + make prepare-fs create directories for the filesystem\n\n"\ + \n\ + Use 'make V=1' for verbose build output. diff --git a/kernel/Makefile b/kernel/Makefile index a32a8108..fb7ffb5c 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -26,17 +26,17 @@ TARGET := ../kernel.img all: $(TARGET) $(TARGET): ../shared/libshared.a $(OBJ) - $(LD) $(LDFLAGS) -o $(ELF) $(OBJL) ../shared/libshared.a + $(VLD) $(LDFLAGS) -o $(ELF) $(OBJL) ../shared/libshared.a $(OBJCOPY) -O binary $(ELF) $@ %.o: %.S - $(CC) $(CFLAGS) -c -MMD -MP $< -o $@ + $(VAS) $(CFLAGS) -c $< -o $@ %.o: %.c - $(CC) $(CFLAGS) -c -MMD -MP $< -o $@ + $(VCC) $(CFLAGS) -c -MMD -MP $< -o $@ %.o: %.cpp - $(CXX) $(CXXFLAGS) -c -MMD -MP $< -o $@ + $(VCXX) $(CXXFLAGS) -c -MMD -MP $< -o $@ clean: $(RM) $(CLEAN_OBJS) $(CLEAN_DEPS) $(ELF) $(TARGET) diff --git a/shared/Makefile b/shared/Makefile index 7814cced..0c1f4f98 100644 --- a/shared/Makefile +++ b/shared/Makefile @@ -18,16 +18,16 @@ TARGET := libshared.a all: $(TARGET) $(TARGET): $(OBJ) - $(AR) rcs $@ $^ + $(VAR) rcs $@ $^ %.o: %.S - $(CC) $(CFLAGS) -c -MMD -MP $< -o $@ + $(VAS) $(CFLAGS) -c $< -o $@ %.o: %.c - $(CC) $(CFLAGS) -c -MMD -MP $< -o $@ + $(VCC) $(CFLAGS) -c -MMD -MP $< -o $@ %.o: %.cpp - $(CXX) $(CXXFLAGS) -c -MMD -MP $< -o $@ + $(VCXX) $(CXXFLAGS) -c -MMD -MP $< -o $@ clean: $(RM) $(CLEAN_OBJS) $(CLEAN_DEPS) $(TARGET) diff --git a/user/Makefile b/user/Makefile index 5c1c88a7..e479a372 100644 --- a/user/Makefile +++ b/user/Makefile @@ -21,17 +21,17 @@ LOCATION := ../fs/redos/user/ all: $(LOCATION)$(TARGET) $(LOCATION)$(TARGET): $(OBJ) - $(LD) $(LDFLAGS) -o $(LOCATION)$(ELF) $(OBJ) ../shared/libshared.a + $(VLD) $(LDFLAGS) -o $(LOCATION)$(ELF) $(OBJ) ../shared/libshared.a $(OBJCOPY) -O binary $(LOCATION)$(ELF) $@ %.o: %.S - $(CC) $(CFLAGS) -c -MMD -MP $< -o $@ + $(VAS) $(CFLAGS) -c $< -o $@ %.o: %.c - $(CC) $(CFLAGS) -c -MMD -MP $< -o $@ + $(VCC) $(CFLAGS) -c -MMD -MP $< -o $@ %.o: %.cpp - $(CXX) $(CXXFLAGS) -c -MMD -MP $< -o $@ + $(VCXX) $(CXXFLAGS) -c -MMD -MP $< -o $@ clean: $(RM) $(CLEAN_OBJS) $(CLEAN_DEPS) $(TARGET) From af5c969b1c9cfe4218bfd1f73d430524328d12e4 Mon Sep 17 00:00:00 2001 From: Matthew Brush Date: Sun, 10 Aug 2025 13:53:08 -0700 Subject: [PATCH 47/50] Use instead of repeating targets --- Makefile | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 152dbeb9..8809e711 100644 --- a/Makefile +++ b/Makefile @@ -62,9 +62,9 @@ kernel: $(MAKE) -C kernel LOAD_ADDR=$(LOAD_ADDR) XHCI_CTX_SIZE=$(XHCI_CTX_SIZE) QEMU=$(QEMU) clean: - $(MAKE) -C shared clean - $(MAKE) -C user clean - $(MAKE) -C kernel clean + $(MAKE) -C shared $@ + $(MAKE) -C user $@ + $(MAKE) -C kernel $@ @echo "removing fs dirs" $(RM) -r $(FS_DIRS) @echo "removing images" From d66a325fe36c22ee6923117f7c001a6c4ea4c548 Mon Sep 17 00:00:00 2001 From: Matthew Brush Date: Sun, 10 Aug 2025 14:01:52 -0700 Subject: [PATCH 48/50] Extract common variables into common.mk Include common.mk from the other Makefiles instead of exporting variables from the top-level Makefile so they get expanded correctly. --- Makefile | 39 +-------------------------------------- common.mk | 34 ++++++++++++++++++++++++++++++++++ kernel/Makefile | 2 ++ shared/Makefile | 3 +++ user/Makefile | 3 +++ 5 files changed, 43 insertions(+), 38 deletions(-) create mode 100644 common.mk diff --git a/Makefile b/Makefile index 8809e711..3255c719 100644 --- a/Makefile +++ b/Makefile @@ -1,41 +1,4 @@ -ARCH ?= aarch64-none-elf -CC := $(ARCH)-gcc -CXX := $(ARCH)-g++ -LD := $(ARCH)-ld -AR := $(ARCH)-ar -OBJCOPY := $(ARCH)-objcopy - -COMMON_FLAGS ?= -ffreestanding -nostdlib -fno-exceptions -fno-unwind-tables \ - -fno-asynchronous-unwind-tables -g -O0 -Wall -Wextra \ - -Wno-unused-parameter -Wno-address-of-packed-member \ - -mcpu=cortex-a72 - -CFLAGS_BASE ?= $(COMMON_FLAGS) -std=c17 -CXXFLAGS_BASE ?= $(COMMON_FLAGS) -fno-rtti -LDFLAGS_BASE ?= - -LOAD_ADDR ?= 0x41000000 -XHCI_CTX_SIZE ?= 32 -QEMU ?= true -MODE ?= virt - -ifeq ($(V), 1) - VAR = $(AR) - VAS = $(CC) - VCC = $(CC) - VCXX = $(CXX) - VLD = $(LD) -else - VAR = @echo " [AR] $@" && $(AR) - VAS = @echo " [AS] $@" && $(CC) - VCC = @echo " [CC] $@" && $(CC) - VCXX = @echo " [CXX] $@" && $(CXX) - VLD = @echo " [LD] $@" && $(LD) -endif - -export AR AS CC CXX LD OBJCOPY -export VAR VAS VCC VCXX VLD -export ARCH COMMON_FLAGS CFLAGS_BASE CXXFLAGS_BASE LDFLAGS_BASE LOAD_ADDR XHCI_CTX_SIZE QEMU +include common.mk OS := $(shell uname) FS_DIRS := fs/redos/user diff --git a/common.mk b/common.mk new file mode 100644 index 00000000..676b839e --- /dev/null +++ b/common.mk @@ -0,0 +1,34 @@ +ARCH ?= aarch64-none-elf +CC := $(ARCH)-gcc +CXX := $(ARCH)-g++ +LD := $(ARCH)-ld +AR := $(ARCH)-ar +OBJCOPY := $(ARCH)-objcopy + +COMMON_FLAGS ?= -ffreestanding -nostdlib -fno-exceptions -fno-unwind-tables \ + -fno-asynchronous-unwind-tables -g -O0 -Wall -Wextra \ + -Wno-unused-parameter -Wno-address-of-packed-member \ + -mcpu=cortex-a72 + +CFLAGS_BASE ?= $(COMMON_FLAGS) -std=c17 +CXXFLAGS_BASE ?= $(COMMON_FLAGS) -fno-rtti +LDFLAGS_BASE ?= + +LOAD_ADDR ?= 0x41000000 +XHCI_CTX_SIZE ?= 32 +QEMU ?= true +MODE ?= virt + +ifeq ($(V), 1) + VAR = $(AR) + VAS = $(CC) + VCC = $(CC) + VCXX = $(CXX) + VLD = $(LD) +else + VAR = @echo " [AR] $@" && $(AR) + VAS = @echo " [AS] $@" && $(CC) + VCC = @echo " [CC] $@" && $(CC) + VCXX = @echo " [CXX] $@" && $(CXX) + VLD = @echo " [LD] $@" && $(LD) +endif diff --git a/kernel/Makefile b/kernel/Makefile index fb7ffb5c..db1c553c 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -1,6 +1,8 @@ #kernel # toolchain (inherited from top-level) +include ../common.mk + CPPFLAGS := -I. -I../shared -I../user -DXHCI_CTX_SIZE=$(XHCI_CTX_SIZE) ifeq ($(QEMU),true) diff --git a/shared/Makefile b/shared/Makefile index 0c1f4f98..cd1b61bc 100644 --- a/shared/Makefile +++ b/shared/Makefile @@ -1,4 +1,7 @@ #shared + +include ../common.mk + CPPFLAGS := -I. -I../kernel CFLAGS := $(CFLAGS_BASE) $(CPPFLAGS) CXXFLAGS := $(CXXFLAGS_BASE) $(CPPFLAGS) diff --git a/user/Makefile b/user/Makefile index e479a372..5e5cabf1 100644 --- a/user/Makefile +++ b/user/Makefile @@ -1,4 +1,7 @@ #user + +include ../common.mk + CPPFLAGS := -I. -I../shared CFLAGS := $(CFLAGS_BASE) $(CPPFLAGS) CXXFLAGS := $(CXXFLAGS_BASE) $(CPPFLAGS) From 16a98b871a203829c88ce16fe388bb975d5c55b5 Mon Sep 17 00:00:00 2001 From: differrari Date: Sun, 10 Aug 2025 00:00:00 +0000 Subject: [PATCH 49/50] [GRAPH] added explicit screen resolutions --- kernel/graph/graphics.cpp | 2 +- run_virt | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/kernel/graph/graphics.cpp b/kernel/graph/graphics.cpp index 73af56ad..77726e68 100644 --- a/kernel/graph/graphics.cpp +++ b/kernel/graph/graphics.cpp @@ -15,7 +15,7 @@ GPUDriver *gpu_driver; bool gpu_init(){ kprint("Initializing GPU"); - gpu_size preferred_screen_size = {1080,720}; + gpu_size preferred_screen_size = {1920,1080}; if (BOARD_TYPE == 1){ if (VirtioGPUDriver *vgd = VirtioGPUDriver::try_init(preferred_screen_size)){ gpu_driver = vgd; diff --git a/run_virt b/run_virt index 9055b39e..3df29834 100755 --- a/run_virt +++ b/run_virt @@ -18,7 +18,7 @@ fi OS_TYPE="$(uname)" DISPLAY_MODE="default" -SELECTED_GPU="virtio-gpu-pci" +SELECTED_GPU="virtio-gpu-pci,xres=1920,yres=1080" if [ "$OS_TYPE" = "Darwin" ]; then NETDEV="-netdev vmnet-bridged,id=net0,ifname=en0" @@ -27,6 +27,7 @@ if [ "$OS_TYPE" = "Darwin" ]; then elif [ "$OS_TYPE" = "Linux" ]; then NETDEV="-netdev user,id=net0" PRIVILEGE="" + DISPLAY_MODE="sdl" else echo "Unknown OS: $OS_TYPE" >&2 exit 1 From 045dcb11f4c2dba2164187614662fa11cfb07c6f Mon Sep 17 00:00:00 2001 From: Diego Ferrari Date: Mon, 11 Aug 2025 00:00:00 +0000 Subject: [PATCH 50/50] Removing some hardcoded values --- kernel/console/kconsole/kconsole.cpp | 2 +- kernel/console/kconsole/terminal.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/kernel/console/kconsole/kconsole.cpp b/kernel/console/kconsole/kconsole.cpp index c458cf17..13a64031 100644 --- a/kernel/console/kconsole/kconsole.cpp +++ b/kernel/console/kconsole/kconsole.cpp @@ -14,7 +14,7 @@ void KernelConsole::initialize(){ mem_page = palloc(PAGE_SIZE, true, true, false); resize(); clear(); - default_text_color = 0xFFFFFFFF; + default_text_color = COLOR_WHITE; text_color = default_text_color; } diff --git a/kernel/console/kconsole/terminal.cpp b/kernel/console/kconsole/terminal.cpp index d4974bb5..7a7c9774 100644 --- a/kernel/console/kconsole/terminal.cpp +++ b/kernel/console/kconsole/terminal.cpp @@ -110,7 +110,7 @@ void Terminal::TMP_test(int argc, const char* args[]){ if (*term == 0) return; const char *next = seek_to(term, ';'); uint64_t color = parse_hex_u64(term, next - term); - set_text_color(color & 0xFFFFFF); + set_text_color(color & UINT32_MAX); put_string(next); }