From 1fa4b5c70a3a23617ecd0f2effd85adf1eb829f7 Mon Sep 17 00:00:00 2001 From: xamidev <121681048+xamidev@users.noreply.github.com> Date: Mon, 23 Sep 2024 15:46:58 +0200 Subject: [PATCH] Add: exec feature for binaries --- docs/DEVELOPERS.md | 2 ++ docs/USERS.md | 4 ++++ src/kernel/kmain.c | 11 ----------- src/kernel/loader.s | 2 +- src/kernel/shell.c | 1 + src/kernel/syscalls.c | 4 ++-- src/utils/misc.c | 27 +++++++++++++++++++++++++-- src/utils/utils.h | 3 +++ 8 files changed, 38 insertions(+), 16 deletions(-) diff --git a/docs/DEVELOPERS.md b/docs/DEVELOPERS.md index 045ccdc..262ed34 100644 --- a/docs/DEVELOPERS.md +++ b/docs/DEVELOPERS.md @@ -42,6 +42,8 @@ sudo qemu-system-i386 -drive if=pflash,format=raw,readonly=on,file=/usr/share/OV Be warned, these are not actual programs in the sense you'd expect. These are indeed functions that are called from the shell, and embedded in the kernel ELF binary. Real programs apart from the kernel are not yet a thing here, but might be one day. +(Now, there is a 'beginning' of something that we could call real programs, but I still suck at making the syscalls work. One day, one day...) + ### Step 1 - Making the program and the entry point To make a program for the OS, first create the appropriate C source file and header file in the `src/programs` subfolder. Name it appropriately, for example `myprogram.c`. diff --git a/docs/USERS.md b/docs/USERS.md index a0fe44e..554c431 100644 --- a/docs/USERS.md +++ b/docs/USERS.md @@ -143,3 +143,7 @@ Starts a simplified and buggy snake game. You can choose the speed by setting th Controls: - `q` to quit - `wasd` to move + +#### `exec ` + +Executes a binary file. Warning: this is highly broken and syscalls aren't working. It's written in a childish manner. Help is always appreciated (lol). diff --git a/src/kernel/kmain.c b/src/kernel/kmain.c index b1e60a8..45a1820 100644 --- a/src/kernel/kmain.c +++ b/src/kernel/kmain.c @@ -120,17 +120,6 @@ void kmain(multiboot2_info *mb_info) // usually the place where i do testing - void* binary_file = load_file_from_initrd((uint8_t*)initrd_addr, "./hello.bin"); - if (binary_file == NULL) - { - printf("[debug] Failed to load test program!\n"); - } else { - printf("[debug] Test program loaded!\n"); - } - - void (*program_entry)() = (void (*)())binary_file; - program_entry(); - timer_install(); keyboard_install(); printf("[kernel] spawning shell...\n"); diff --git a/src/kernel/loader.s b/src/kernel/loader.s index 1011219..1029284 100644 --- a/src/kernel/loader.s +++ b/src/kernel/loader.s @@ -225,7 +225,7 @@ syscall_common_stub: mov eax, ds push eax ; save ds - mov ax, 0x10 ; kernel segment + mov ax, 0x01 ; kernel segment YES I CHEATED I KNOW THIS SUCKS mov ds, ax mov es, ax diff --git a/src/kernel/shell.c b/src/kernel/shell.c index c1e75be..febd98c 100644 --- a/src/kernel/shell.c +++ b/src/kernel/shell.c @@ -146,6 +146,7 @@ void shell_install() register_command("lspci", program_lspci); register_command("naval", program_navalbattle); register_command("snake", program_snake); + register_command("exec", program_exec); for (;;) { diff --git a/src/kernel/syscalls.c b/src/kernel/syscalls.c index a2e20dd..aac4650 100644 --- a/src/kernel/syscalls.c +++ b/src/kernel/syscalls.c @@ -5,7 +5,7 @@ #include "../libc/stdio.h" -void handle_syscall(int syscall_number, void* arg) +void handle_syscall(int syscall_number) { switch(syscall_number) { @@ -29,5 +29,5 @@ void syscall_handler() printf("[syscall] syscall_number=%d, arg=%p\n", syscall_number, arg); - handle_syscall(syscall_number, arg); + handle_syscall(syscall_number); } diff --git a/src/utils/misc.c b/src/utils/misc.c index c8f364d..717ff31 100644 --- a/src/utils/misc.c +++ b/src/utils/misc.c @@ -11,6 +11,8 @@ #include "../drivers/rtc.h" #include "../kernel/io.h" #include "../drivers/pci.h" +#include "../kernel/initrd.h" +#include "../kernel/kmain.h" // Print a rainbow colorful text for testing @@ -74,7 +76,7 @@ void program_uptime() void program_help() { - printf("help\tpanic\twords\tprimes\trainbow\tclear\nmath\tbf\t uptime echo\t sysinfo\tconway\nrot13 morse\tcowsay time\t read\t reboot\npi\t ls\t cat\t bmp\t lspci\t naval\nsnake\n"); + printf("help\tpanic\twords\tprimes\trainbow\tclear\nmath\tbf\t uptime echo\t sysinfo\tconway\nrot13 morse\tcowsay time\t read\t reboot\npi\t ls\t cat\t bmp\t lspci\t naval\nsnake exec\n"); } // Panic @@ -133,7 +135,7 @@ void program_read(int argc, char* argv[]) } } -// Reboots the machine (might just shutdown) +// Reboots the machine (might just shutdown) (or do nothing if youre lucky) void program_reboot() { @@ -151,3 +153,24 @@ void program_lspci() { scan_pci_bus(); } + +// Executes binary file + +void program_exec(int argc, char* argv[]) +{ + if (argc < 2) + { + puts("Usage: exec \n"); + return; + } + void* binary_file = load_file_from_initrd((uint8_t*)initrd_addr, argv[1]); + + if (binary_file == NULL) + { + printf("[exec] Failed to load program '%s'.\n", argv[1]); + return; + } + + void (*program_entry)() = (void (*)())binary_file; + program_entry(); +} diff --git a/src/utils/utils.h b/src/utils/utils.h index 80e6e51..419974f 100644 --- a/src/utils/utils.h +++ b/src/utils/utils.h @@ -45,4 +45,7 @@ void program_navalbattle(); void program_conway(); void program_snake(); +// Binaries loading and execution +void program_exec(); + #endif