diff --git a/aux/limine_iso_amd64.sh b/aux/limine_iso_amd64.sh index ff683ab..64c2f55 100755 --- a/aux/limine_iso_amd64.sh +++ b/aux/limine_iso_amd64.sh @@ -1,6 +1,7 @@ #!/bin/sh make -C boot/limine +rm -rf iso_root mkdir -p iso_root/boot/limine mkdir -p iso_root/EFI/BOOT diff --git a/ce/.gitignore b/ce/.gitignore new file mode 100644 index 0000000..2c156f6 --- /dev/null +++ b/ce/.gitignore @@ -0,0 +1,2 @@ +*.o +ce diff --git a/ce/Makefile b/ce/Makefile new file mode 100644 index 0000000..d16094b --- /dev/null +++ b/ce/Makefile @@ -0,0 +1 @@ +include ../make/user.mk diff --git a/ce/app.mk b/ce/app.mk new file mode 100644 index 0000000..72926d4 --- /dev/null +++ b/ce/app.mk @@ -0,0 +1 @@ +app := ce diff --git a/ce/ce.c b/ce/ce.c new file mode 100644 index 0000000..b81db84 --- /dev/null +++ b/ce/ce.c @@ -0,0 +1,7 @@ +#include + +void app_main (void) { + for (;;) { + test ('x'); + } +} diff --git a/ce/doc.txt b/ce/doc.txt new file mode 100644 index 0000000..8671612 --- /dev/null +++ b/ce/doc.txt @@ -0,0 +1,3 @@ +CE - Command Executor + +This is a shell application. TODO! diff --git a/ce/src.mk b/ce/src.mk new file mode 100644 index 0000000..8c16599 --- /dev/null +++ b/ce/src.mk @@ -0,0 +1,3 @@ +c += ce.c + +o += ce.o diff --git a/include/m/status.h b/include/m/status.h index 7c13a69..f31d84b 100644 --- a/include/m/status.h +++ b/include/m/status.h @@ -13,5 +13,6 @@ #define ST_EXISTS 9 #define ST_OOB_ERROR 10 #define ST_BAD_PATH 11 +#define ST_EXEC_ERROR 12 #endif // _M_STATUS_H diff --git a/include/m/syscall_defs.h b/include/m/syscall_defs.h index 7238ed9..4f3a685 100644 --- a/include/m/syscall_defs.h +++ b/include/m/syscall_defs.h @@ -13,5 +13,6 @@ #define SYS_MUTEX_UNLOCK 10 #define SYS_ARGUMENT_PTR 11 #define SYS_DEVICE_DO 12 +#define SYS_EXEC 13 #endif // _M_SYSCALL_DEFS_H diff --git a/init/.gitignore b/init/.gitignore index 25a7384..808d629 100644 --- a/init/.gitignore +++ b/init/.gitignore @@ -1,2 +1,2 @@ *.o -*.exe +init diff --git a/init/app.mk b/init/app.mk index 2303205..1b5ad65 100644 --- a/init/app.mk +++ b/init/app.mk @@ -1 +1 @@ -app := init.exe +app := init diff --git a/init/init.c b/init/init.c index 8be1150..524719b 100644 --- a/init/init.c +++ b/init/init.c @@ -34,6 +34,8 @@ void app_main (void) { letter = 'a'; + process_exec ("ramdisk:/ce"); + process_spawn (&app_proc, (void*)'b'); process_spawn (&app_proc, (void*)'c'); process_spawn (&app_proc, (void*)'d'); diff --git a/kernel/amd64/smp.c b/kernel/amd64/smp.c index fd63323..7ada72a 100644 --- a/kernel/amd64/smp.c +++ b/kernel/amd64/smp.c @@ -87,7 +87,7 @@ static void amd64_smp_bootstrap (struct limine_mp_info* mp_info) { atomic_fetch_sub (&cpu_counter, 1); - struct proc* spin_proc = proc_from_file ("ramdisk", "/spin.exe"); + struct proc* spin_proc = proc_from_file ("ramdisk", "/spin"); struct cpu* spin_cpu = thiscpu; proc_register (spin_proc, &spin_cpu); diff --git a/kernel/fs/path.c b/kernel/fs/path.c index 0109745..0e9eb91 100644 --- a/kernel/fs/path.c +++ b/kernel/fs/path.c @@ -4,7 +4,7 @@ bool path_validate_char (char ch) { return ((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || - (ch == '_') || (ch == '_')); + (ch == '_') || (ch == '-') || (ch == '/')); } bool path_validate (const char* path) { @@ -13,21 +13,6 @@ bool path_validate (const char* path) { const char* ptr = path; - if (*ptr == ':') - return false; - - while (*ptr != ':' && *ptr != '\0') { - if (!path_validate_char (*ptr)) - return false; - - ptr++; - } - - if (*ptr != ':') - return false; - - ptr++; - if (*ptr != '/') return false; @@ -41,7 +26,7 @@ bool path_validate (const char* path) { ptr++; } - if (ptr > path && *(ptr - 1) == '/' && *(ptr - 2) != ':') + if (ptr > path + 1 && *(ptr - 1) == '/') return false; return true; diff --git a/kernel/fs/ramdiskfs.c b/kernel/fs/ramdiskfs.c index 86dcc30..12e7bc6 100644 --- a/kernel/fs/ramdiskfs.c +++ b/kernel/fs/ramdiskfs.c @@ -6,6 +6,7 @@ #include #include #include +#include struct ramdisk_tar_header { char filename[100]; @@ -24,15 +25,16 @@ struct ramdisk_tar_file { size_t size; }; -#define RAMDISK_FILES_MAX 128 -#define RAMDISK_PATH "/boot/mop3dist.tar" +#define RAMDISK_FILES_MAX 128 +#define RAMDISK_FILENAME_MAX 128 +#define RAMDISK_PATH "/boot/mop3dist.tar" static struct ramdisk_tar_file ramdisk_files[RAMDISK_FILES_MAX]; static struct ramdisk_tar_file* ramdisk_get_file (const char* filename) { for (size_t i = 0; i < RAMDISK_FILES_MAX; i++) { if ((ramdisk_files[i].header != NULL) && - (memcmp (ramdisk_files[i].header->filename, filename, strlen (filename)) == 0)) + (strncmp (ramdisk_files[i].header->filename, filename, RAMDISK_FILENAME_MAX) == 0)) return &ramdisk_files[i]; } return NULL; @@ -52,10 +54,10 @@ static size_t ramdisk_tar_get_size (uint8_t* in) { static size_t ramdisk_tar_parse (uint8_t* addr) { size_t i; - for (i = 0;; i++) { + for (i = 0; i < RAMDISK_FILES_MAX; i++) { struct ramdisk_tar_header* hdr = (struct ramdisk_tar_header*)addr; - if (hdr->filename[i] == '\0') + if (hdr->filename[0] == '\0') break; size_t size = ramdisk_tar_get_size (hdr->size); @@ -64,10 +66,7 @@ static size_t ramdisk_tar_parse (uint8_t* addr) { ramdisk_files[i].content = (uint8_t*)((uintptr_t)hdr + 512); ramdisk_files[i].size = ramdisk_tar_get_size ((uint8_t*)hdr->size); - addr += ((size / 512) + 1) * 512; - - if (size % 512) - addr += 512; + addr += 512 + ((size + 511) & ~511); } return i; @@ -83,7 +82,7 @@ bool ramdiskfs_mount (struct vfs_mountpoint* mountpoint) { for (size_t i = 0; i < module->module_count; i++) { struct limine_file* file = module->modules[i]; - if (memcmp (file->path, RAMDISK_PATH, strlen (RAMDISK_PATH)) == 0) { + if (strncmp (file->path, RAMDISK_PATH, strlen (RAMDISK_PATH)) == 0) { rd_addr = file->address; } } diff --git a/kernel/libk/string.c b/kernel/libk/string.c index 37877b7..a5e5a64 100644 --- a/kernel/libk/string.c +++ b/kernel/libk/string.c @@ -44,3 +44,16 @@ int memcmp (const void* s1, const void* s2, size_t n) { } return 0; } + +int strncmp (const char* s1, const char* s2, size_t n) { + while (n && *s1 && (*s1 == *s2)) { + ++s1; + ++s2; + --n; + } + if (n == 0) { + return 0; + } else { + return (*(unsigned char*)s1 - *(unsigned char*)s2); + } +} diff --git a/kernel/libk/string.h b/kernel/libk/string.h index 7bb5d16..78cf691 100644 --- a/kernel/libk/string.h +++ b/kernel/libk/string.h @@ -8,6 +8,7 @@ size_t memcpy (void* dst, const void* src, size_t n); void strncpy (char* dst, const char* src, size_t n); size_t strlen (const char* str); int memcmp (const void* s1, const void* s2, size_t n); +int strncmp (const char* s1, const char* s2, size_t n); #define strlen_null(x) (strlen ((x)) + 1) diff --git a/kernel/proc/proc.c b/kernel/proc/proc.c index ed1c1ee..2ba9018 100644 --- a/kernel/proc/proc.c +++ b/kernel/proc/proc.c @@ -299,11 +299,11 @@ void proc_init (void) { irq_attach (&proc_irq_sched, NULL, CPU_REQUEST_SCHED); #endif - struct proc* spin_proc = proc_from_file ("ramdisk", "/spin.exe"); + struct proc* spin_proc = proc_from_file ("ramdisk", "/spin"); struct cpu* spin_cpu = thiscpu; proc_register (spin_proc, &spin_cpu); - struct proc* init = proc_from_file ("ramdisk", "/init.exe"); + struct proc* init = proc_from_file ("ramdisk", "/init"); init->procgroup->capabilities |= PROC_CAP_TERMINAL; struct cpu* init_cpu = thiscpu; proc_register (init, &init_cpu); diff --git a/kernel/syscall/syscall.c b/kernel/syscall/syscall.c index 4e47894..6f917bb 100644 --- a/kernel/syscall/syscall.c +++ b/kernel/syscall/syscall.c @@ -1,7 +1,10 @@ #include #include +#include +#include #include #include +#include #include #include #include @@ -181,7 +184,7 @@ DEFINE_SYSCALL (sys_device_do) { uintptr_t out_paddr; if (!(cmd >= 0 && cmd < (int)fieldlengthof (struct device, ops))) - return -ST_BAD_DEVICE_OP; + return SYSRESULT (-ST_BAD_DEVICE_OP); spin_lock (&proc->procgroup->lock); @@ -206,7 +209,7 @@ DEFINE_SYSCALL (sys_device_do) { struct device* device = device_find (device_id); if (device == NULL) - return -ST_NOT_FOUND; + return SYSRESULT (-ST_NOT_FOUND); spin_lock (&device->lock); @@ -214,7 +217,43 @@ DEFINE_SYSCALL (sys_device_do) { spin_unlock (&device->lock); - return ret; + return SYSRESULT (ret); +} + +/* int exec (char* path) */ +DEFINE_SYSCALL (sys_exec) { + uintptr_t uvaddr_path = a1; + + struct limine_hhdm_response* hhdm = limine_hhdm_request.response; + + uintptr_t out_paddr; + + spin_lock (&proc->procgroup->lock); + out_paddr = mm_v2p (&proc->procgroup->pd, uvaddr_path); + spin_unlock (&proc->procgroup->lock); + + if (out_paddr == 0) + return SYSRESULT (-ST_BAD_ADDRESS_SPACE); + + const char* path = (const char*)((uintptr_t)hhdm->offset + out_paddr); + + char mountpoint[fieldsizeof (struct vfs_mountpoint, key)]; + const char* subpath = NULL; + + if (!path_parse (path, mountpoint, &subpath)) + return SYSRESULT (-ST_BAD_PATH); + + struct proc* new = proc_from_file (mountpoint, subpath); + + if (new == NULL) + return SYSRESULT (-ST_EXEC_ERROR); + + int pid = new->pid; + + if (proc_register (new, reschedule_cpu) == PROC_NEED_RESCHEDULE) + *reschedule = true; + + return SYSRESULT (pid); } static syscall_handler_func_t handler_table[] = { @@ -230,6 +269,7 @@ static syscall_handler_func_t handler_table[] = { [SYS_MUTEX_LOCK] = &sys_mutex_lock, [SYS_MUTEX_UNLOCK] = &sys_mutex_unlock, [SYS_DEVICE_DO] = &sys_device_do, + [SYS_EXEC] = &sys_exec, }; syscall_handler_func_t syscall_find_handler (int syscall_num) { diff --git a/libmsl/m/system.c b/libmsl/m/system.c index 81e8b13..3c94306 100644 --- a/libmsl/m/system.c +++ b/libmsl/m/system.c @@ -38,3 +38,5 @@ void* argument_ptr (void) { return (void*)do_syscall (SYS_ARGUMENT_PTR, 0); } int device_do (int device_id, int cmd, void* a1, void* a2, void* a3, void* a4) { return (int)do_syscall (SYS_DEVICE_DO, device_id, cmd, a1, a2, a3, a4); } + +int exec (const char* path) { return (int)do_syscall (SYS_EXEC, path); } diff --git a/libmsl/m/system.h b/libmsl/m/system.h index d6404d1..5c4cd8f 100644 --- a/libmsl/m/system.h +++ b/libmsl/m/system.h @@ -49,4 +49,7 @@ void* argument_ptr (void); /* Call a device command */ int device_do (int device_id, int cmd, void* a1, void* a2, void* a3, void* a4); +/* Run external ELF program */ +int exec (const char* path); + #endif // _LIBMSL_M_SYSTEM_H diff --git a/libprocess/process.c b/libprocess/process.c index 1780dd9..b87b3da 100644 --- a/libprocess/process.c +++ b/libprocess/process.c @@ -17,3 +17,5 @@ int process_spawn (process_func_t func, void* argument_ptr) { int process_quit (void) { return quit (); } void* process_argument (void) { return argument_ptr (); } + +int process_exec (const char* path) { return exec (path); } diff --git a/libprocess/process.h b/libprocess/process.h index 9f21ffd..f272ee6 100644 --- a/libprocess/process.h +++ b/libprocess/process.h @@ -18,4 +18,7 @@ int process_quit (void); /* Get process argument pointer */ void* process_argument (void); +/* Run ELF process */ +int process_exec (const char* path); + #endif // _LIBPROCESS_PROCESS_PROCESS_H diff --git a/make/apps.mk b/make/apps.mk index 0ee4058..65ec7eb 100644 --- a/make/apps.mk +++ b/make/apps.mk @@ -1,4 +1,7 @@ -apps := init spin +apps := \ + init \ + spin \ + ce all_apps: @for d in $(apps); do make -C $$d platform=$(platform) all; done diff --git a/make/dist.mk b/make/dist.mk index 662ba6d..743c048 100644 --- a/make/dist.mk +++ b/make/dist.mk @@ -1,4 +1,4 @@ -exe := $(foreach d,$(apps),$(wildcard $(d)/*.exe)) +exe := $(foreach d,$(apps),$(wildcard $(d)/$(d))) all_dist: mop3dist.tar diff --git a/spin/.gitignore b/spin/.gitignore index 25a7384..8aaa5a2 100644 --- a/spin/.gitignore +++ b/spin/.gitignore @@ -1,2 +1,2 @@ *.o -*.exe +spin diff --git a/spin/app.mk b/spin/app.mk index 5a386ed..f2e6633 100644 --- a/spin/app.mk +++ b/spin/app.mk @@ -1 +1 @@ -app := spin.exe +app := spin