From db7fc030e9e04c881f5f12a0e23baa8db4e20ee1 Mon Sep 17 00:00:00 2001 From: Minep Date: Sun, 1 Jan 2023 01:44:46 +0000 Subject: [PATCH 1/1] feat: wrapper function for bootstraping user program feat: user runtime library for writing user space program wip: initd refactor: separate user space code from kernel code (syscall and some inits) refactor: rewrite makefile to make it more flexible and modular --- lunaix-os/.gitignore | 3 +- lunaix-os/.vscode/c_cpp_properties.json | 3 +- lunaix-os/config/make-cc | 1 + lunaix-os/config/make-locations | 3 +- lunaix-os/config/make-os | 8 +- lunaix-os/includes/lunaix/foptions.h | 2 +- lunaix-os/includes/lunaix/ioctl.h | 2 +- lunaix-os/includes/lunaix/ld.h | 15 ++ lunaix-os/includes/lunaix/mm/mm.h | 2 +- lunaix-os/includes/lunaix/signal.h | 2 +- lunaix-os/includes/lunaix/types.h | 2 +- lunaix-os/kernel/demos/dir_read.c | 33 --- lunaix-os/kernel/demos/input_test.c | 40 ---- lunaix-os/kernel/demos/iotest.c | 65 ------ lunaix-os/kernel/demos/signal_demo.c | 78 ------- lunaix-os/kernel/demos/simple_sh.c | 210 ------------------ lunaix-os/kernel/device/devfs.c | 2 +- lunaix-os/kernel/fs/iso9660/directory.c | 2 +- lunaix-os/kernel/fs/vfs.c | 2 +- lunaix-os/kernel/loader/exec.c | 69 ++++-- lunaix-os/kernel/lxinit.c | 90 -------- lunaix-os/kernel/proc0.c | 144 +++++------- lunaix-os/kernel/process/taskfs.c | 2 +- lunaix-os/libs/ulibc/printf.c | 2 +- lunaix-os/makefile | 48 ++-- lunaix-os/makefile.ker | 41 ++++ lunaix-os/makefile.prog | 12 + lunaix-os/makefile.usr | 32 +++ lunaix-os/uprog/init.c | 23 ++ lunaix-os/usr/api/dirent.c | 2 +- lunaix-os/usr/api/errno.c | 2 +- lunaix-os/usr/api/fcntl.c | 2 +- lunaix-os/usr/api/ioctl.c | 2 +- lunaix-os/usr/api/lunaix.c | 2 +- lunaix-os/usr/api/mann.c | 2 +- lunaix-os/usr/api/mount.c | 2 +- lunaix-os/usr/api/signal.c | 2 +- lunaix-os/usr/api/unistd.c | 2 +- .../{includes/usr => usr/includes}/errno.h | 0 .../{includes/usr => usr/includes}/fcntl.h | 4 +- .../usr => usr/includes}/fcntl_defs.h | 0 .../{includes/usr => usr/includes}/signal.h | 4 +- .../usr => usr/includes}/signal_defs.h | 0 .../usr => usr/includes}/sys/dirent.h | 2 +- .../usr => usr/includes}/sys/dirent_defs.h | 0 .../usr => usr/includes}/sys/ioctl.h | 0 .../usr => usr/includes}/sys/ioctl_defs.h | 0 .../usr => usr/includes}/sys/lunaix.h | 2 +- .../{includes/usr => usr/includes}/sys/mann.h | 4 +- .../usr => usr/includes}/sys/mann_flags.h | 0 .../usr => usr/includes}/sys/mount.h | 2 +- .../usr => usr/includes}/sys/types.h | 0 .../{includes/usr => usr/includes}/unistd.h | 2 +- lunaix-os/usr/link-usr.ld | 5 + lunaix-os/usr/uinit.c | 10 + lunaix-os/usr/uwrap.S | 23 ++ 56 files changed, 320 insertions(+), 694 deletions(-) delete mode 100644 lunaix-os/kernel/demos/dir_read.c delete mode 100644 lunaix-os/kernel/demos/input_test.c delete mode 100644 lunaix-os/kernel/demos/iotest.c delete mode 100644 lunaix-os/kernel/demos/signal_demo.c delete mode 100644 lunaix-os/kernel/demos/simple_sh.c delete mode 100644 lunaix-os/kernel/lxinit.c create mode 100644 lunaix-os/makefile.ker create mode 100644 lunaix-os/makefile.prog create mode 100644 lunaix-os/makefile.usr create mode 100644 lunaix-os/uprog/init.c rename lunaix-os/{includes/usr => usr/includes}/errno.h (100%) rename lunaix-os/{includes/usr => usr/includes}/fcntl.h (70%) rename lunaix-os/{includes/usr => usr/includes}/fcntl_defs.h (100%) rename lunaix-os/{includes/usr => usr/includes}/signal.h (83%) rename lunaix-os/{includes/usr => usr/includes}/signal_defs.h (100%) rename lunaix-os/{includes/usr => usr/includes}/sys/dirent.h (81%) rename lunaix-os/{includes/usr => usr/includes}/sys/dirent_defs.h (100%) rename lunaix-os/{includes/usr => usr/includes}/sys/ioctl.h (100%) rename lunaix-os/{includes/usr => usr/includes}/sys/ioctl_defs.h (100%) rename lunaix-os/{includes/usr => usr/includes}/sys/lunaix.h (91%) rename lunaix-os/{includes/usr => usr/includes}/sys/mann.h (78%) rename lunaix-os/{includes/usr => usr/includes}/sys/mann_flags.h (100%) rename lunaix-os/{includes/usr => usr/includes}/sys/mount.h (91%) rename lunaix-os/{includes/usr => usr/includes}/sys/types.h (100%) rename lunaix-os/{includes/usr => usr/includes}/unistd.h (98%) create mode 100644 lunaix-os/usr/link-usr.ld create mode 100644 lunaix-os/usr/uinit.c create mode 100644 lunaix-os/usr/uwrap.S diff --git a/lunaix-os/.gitignore b/lunaix-os/.gitignore index b20e749..251d992 100644 --- a/lunaix-os/.gitignore +++ b/lunaix-os/.gitignore @@ -7,4 +7,5 @@ playground/ bx_enh_dbg.ini machine/ draft/ -iso_inspect/ \ No newline at end of file +iso_inspect/ +unused/ \ No newline at end of file diff --git a/lunaix-os/.vscode/c_cpp_properties.json b/lunaix-os/.vscode/c_cpp_properties.json index c68b4c7..0fdf995 100644 --- a/lunaix-os/.vscode/c_cpp_properties.json +++ b/lunaix-os/.vscode/c_cpp_properties.json @@ -3,7 +3,8 @@ { "name": "OS-DEV", "includePath": [ - "${workspaceFolder}/includes" + "${workspaceFolder}/includes", + "${workspaceFolder}/usr/includes" ], "compilerArgs": [ "-ffreestanding", diff --git a/lunaix-os/config/make-cc b/lunaix-os/config/make-cc index 4491370..6fd8494 100644 --- a/lunaix-os/config/make-cc +++ b/lunaix-os/config/make-cc @@ -1,5 +1,6 @@ CC := i686-elf-gcc AS := i686-elf-as +AR := i686-elf-ar ARCH_OPT := -D__ARCH_IA32 -include flags.h diff --git a/lunaix-os/config/make-locations b/lunaix-os/config/make-locations index b60503e..093f7ba 100644 --- a/lunaix-os/config/make-locations +++ b/lunaix-os/config/make-locations @@ -3,7 +3,8 @@ KERNEL_DIR := kernel OBJECT_DIR := $(BUILD_DIR)/obj BIN_DIR := $(BUILD_DIR)/bin ISO_DIR := $(BUILD_DIR)/iso +USR_DIR := $(BUILD_DIR)/usr ISO_BOOT_DIR := $(ISO_DIR)/boot ISO_GRUB_DIR := $(ISO_BOOT_DIR)/grub -INCLUDES_DIR := includes \ No newline at end of file +INCLUDES := -Iincludes -Iusr/includes \ No newline at end of file diff --git a/lunaix-os/config/make-os b/lunaix-os/config/make-os index e3c26b7..7ea5c01 100644 --- a/lunaix-os/config/make-os +++ b/lunaix-os/config/make-os @@ -1,4 +1,6 @@ OS_ARCH := x86 -OS_NAME = lunaix -OS_BIN = $(OS_NAME).bin -OS_ISO = $(OS_NAME).iso \ No newline at end of file +OS_NAME := lunaix +OS_BIN := $(OS_NAME).bin +OS_ISO := $(OS_NAME).iso + +USR_LIB := liblxusrt.a \ No newline at end of file diff --git a/lunaix-os/includes/lunaix/foptions.h b/lunaix-os/includes/lunaix/foptions.h index 46b1500..625c0b2 100644 --- a/lunaix-os/includes/lunaix/foptions.h +++ b/lunaix-os/includes/lunaix/foptions.h @@ -1,6 +1,6 @@ #ifndef __LUNAIX_FOPTIONS_H #define __LUNAIX_FOPTIONS_H -#include +#include #endif /* __LUNAIX_FOPTIONS_H */ diff --git a/lunaix-os/includes/lunaix/ioctl.h b/lunaix-os/includes/lunaix/ioctl.h index f72d41e..6dcd83c 100644 --- a/lunaix-os/includes/lunaix/ioctl.h +++ b/lunaix-os/includes/lunaix/ioctl.h @@ -1,6 +1,6 @@ #ifndef __LUNAIX_IOCTL_H #define __LUNAIX_IOCTL_H -#include +#include #endif /* __LUNAIX_IOCTL_H */ diff --git a/lunaix-os/includes/lunaix/ld.h b/lunaix-os/includes/lunaix/ld.h index e6732ef..690f95f 100644 --- a/lunaix-os/includes/lunaix/ld.h +++ b/lunaix-os/includes/lunaix/ld.h @@ -16,6 +16,9 @@ struct ld_info ptr_t base; ptr_t end; ptr_t mem_sz; + + ptr_t stack_top; + ptr_t entry; }; struct ld_param @@ -40,6 +43,18 @@ struct usr_exec_param int elf_load(struct ld_param* ldparam, struct v_file* elfile); +int +exec_load_byname(struct ld_param* param, + const char* filename, + const char** argv, + const char** envp); + +int +exec_load(struct ld_param* param, + struct v_file* executable, + const char** argv, + const char** envp); + void ld_create_param(struct ld_param* param, struct proc_info* proc, ptr_t vms); #endif diff --git a/lunaix-os/includes/lunaix/mm/mm.h b/lunaix-os/includes/lunaix/mm/mm.h index 218d1d5..74868b0 100644 --- a/lunaix-os/includes/lunaix/mm/mm.h +++ b/lunaix-os/includes/lunaix/mm/mm.h @@ -6,7 +6,7 @@ #include #include -#include +#include /** * @brief 私有区域,该区域中的页无法进行任何形式的共享。 diff --git a/lunaix-os/includes/lunaix/signal.h b/lunaix-os/includes/lunaix/signal.h index 55859ba..c815c4f 100644 --- a/lunaix-os/includes/lunaix/signal.h +++ b/lunaix-os/includes/lunaix/signal.h @@ -1,7 +1,7 @@ #ifndef __LUNAIX_SIGNAL_H #define __LUNAIX_SIGNAL_H -#include +#include #define _SIG_NUM 16 diff --git a/lunaix-os/includes/lunaix/types.h b/lunaix-os/includes/lunaix/types.h index 7e6562c..a98ae97 100644 --- a/lunaix-os/includes/lunaix/types.h +++ b/lunaix-os/includes/lunaix/types.h @@ -3,7 +3,7 @@ #include #include -#include +#include #define PACKED __attribute__((packed)) diff --git a/lunaix-os/kernel/demos/dir_read.c b/lunaix-os/kernel/demos/dir_read.c deleted file mode 100644 index c97dfc9..0000000 --- a/lunaix-os/kernel/demos/dir_read.c +++ /dev/null @@ -1,33 +0,0 @@ -#include -#include -#include -#include - -void -_readdir_main() -{ - int fd = open("/dev/./../dev/.", 0); - if (fd == -1) { - printf("fail to open (%d)\n", errno); - return; - } - - char path[129]; - int len = realpathat(fd, path, 128); - if (len < 0) { - printf("fail to read (%d)\n", errno); - } else { - path[len] = 0; - printf("%s\n", path); - } - - struct lx_dirent ent = { .d_offset = 0 }; - - while (sys_readdir(fd, &ent) == 1) { - printf("%s\n", ent.d_name); - } - - close(fd); - - return; -} \ No newline at end of file diff --git a/lunaix-os/kernel/demos/input_test.c b/lunaix-os/kernel/demos/input_test.c deleted file mode 100644 index f24a519..0000000 --- a/lunaix-os/kernel/demos/input_test.c +++ /dev/null @@ -1,40 +0,0 @@ -#include - -#include -#include -#include -#include -#include - -#define STDIN 1 -#define STDOUT 0 - -void -input_test() -{ - int fd = open("/dev/input/i8042-kbd", 0); - - if (fd < 0) { - printf("fail to open (%d)", errno); - return; - } - - struct input_evt_pkt event; - - while (read(fd, &event, sizeof(event)) > 0) { - char* action; - if (event.pkt_type == PKT_PRESS) { - action = "pressed"; - } else { - action = "release"; - } - - printf("%u: %s '%c', class=0x%x, scan=%x\n", - event.timestamp, - action, - event.sys_code & 0xff, - (event.sys_code & 0xff00) >> 8, - event.scan_code); - } - return; -} \ No newline at end of file diff --git a/lunaix-os/kernel/demos/iotest.c b/lunaix-os/kernel/demos/iotest.c deleted file mode 100644 index 91c3571..0000000 --- a/lunaix-os/kernel/demos/iotest.c +++ /dev/null @@ -1,65 +0,0 @@ -#include -#include -#include -#include -#include - -void -_iotest_main() -{ - char test_sequence[] = "Once upon a time, in a magical land of Equestria. " - "There were two regal sisters who ruled together " - "and created harmony for all the land."; - char read_out[256]; - - // 切换工作目录至 /dev - int status = chdir("/dev"); - if (status) { - write(stdout, "fail to chdir", 15); - return; - } - - if (getcwd(read_out, sizeof(read_out))) { - printf("current working dir: %s\n", read_out); - } - - // sda 设备 - 硬盘 - // sda设备属于容积设备(Volumetric Device), - // Lunaix会尽可能缓存任何对此设备的上层读写,并使用延迟写入策略。(FO_DIRECT可用于屏蔽该功能) - int fd = open("./sda", 0); - - if (fd < 0) { - printf("fail to open (%d)\n", errno); - return; - } - - // 移动指针至512字节,在大多数情况下,这是第二个逻辑扇区的起始处 - lseek(fd, 512, FSEEK_SET); - - // 总共写入 64 * 136 字节,会产生3个页作为缓存 - for (size_t i = 0; i < 64; i++) { - write(fd, test_sequence, sizeof(test_sequence)); - } - - // 随机读写测试 - lseek(fd, 4 * 4096, FSEEK_SET); - write(fd, test_sequence, sizeof(test_sequence)); - - printf("input: "); - int size = read(stdin, read_out, 256); - - printf("your said: %s\n", read_out); - - write(fd, read_out, size); - - // 读出我们写的内容 - lseek(fd, 512, FSEEK_SET); - read(fd, read_out, sizeof(read_out)); - - // 将读出的内容直接写入tty设备 - write(stdout, read_out, sizeof(read_out)); - write(stdout, "\n", 1); - - // 关闭文件,这同时会将页缓存中的数据下发到底层驱动 - close(fd); -} \ No newline at end of file diff --git a/lunaix-os/kernel/demos/signal_demo.c b/lunaix-os/kernel/demos/signal_demo.c deleted file mode 100644 index 4d7897e..0000000 --- a/lunaix-os/kernel/demos/signal_demo.c +++ /dev/null @@ -1,78 +0,0 @@ -#include - -#include - -#include -#include -#include - -void __USER__ -sigchild_handler(int signum) -{ - printf("SIGCHLD received\n"); -} - -void __USER__ -sigsegv_handler(int signum) -{ - pid_t pid = getpid(); - printf("SIGSEGV received on process %d\n", pid); - _exit(signum); -} - -void __USER__ -sigalrm_handler(int signum) -{ - pid_t pid = getpid(); - printf("I, pid %d, have received an alarm!\n", pid); -} - -void __USER__ -_signal_demo_main() -{ - signal(SIGCHLD, sigchild_handler); - signal(SIGSEGV, sigsegv_handler); - signal(SIGALRM, sigalrm_handler); - - alarm(5); - - int status; - pid_t p = 0; - - printf("Child sleep 3s, parent pause.\n"); - if (!fork()) { - sleep(3); - _exit(0); - } - - pause(); - - printf("Parent resumed on SIGCHILD\n"); - - for (int i = 0; i < 5; i++) { - pid_t pid = 0; - if (!(pid = fork())) { - signal(SIGSEGV, sigsegv_handler); - sleep(i); - if (i == 3) { - i = *(int*)0xdeadc0de; // seg fault! - } - printf("%d\n", i); - _exit(0); - } - printf("Forked %d\n", pid); - } - - while ((p = wait(&status)) >= 0) { - short code = WEXITSTATUS(status); - if (WIFSIGNALED(status)) { - printf("Process %d terminated by signal, exit_code: %d\n", p, code); - } else if (WIFEXITED(status)) { - printf("Process %d exited with code %d\n", p, code); - } else { - printf("Process %d aborted with code %d\n", p, code); - } - } - - printf("done\n"); -} \ No newline at end of file diff --git a/lunaix-os/kernel/demos/simple_sh.c b/lunaix-os/kernel/demos/simple_sh.c deleted file mode 100644 index 26aa3be..0000000 --- a/lunaix-os/kernel/demos/simple_sh.c +++ /dev/null @@ -1,210 +0,0 @@ -#include -#include - -#include -#include -#include -#include -#include -#include - -#include -#include - -char pwd[512]; -char cat_buf[1024]; - -/* - Simple shell - (actually this is not even a shell) - It just to make the testing more easy. -*/ - -void -parse_cmdline(char* line, char** cmd, char** arg_part) -{ - strrtrim(line); - line = strltrim_safe(line); - int l = 0; - char c = 0; - while ((c = line[l])) { - if (c == ' ') { - line[l++] = 0; - break; - } - l++; - } - *cmd = line; - *arg_part = strltrim_safe(line + l); -} - -void -sh_printerr() -{ - int errno = geterrno(); - switch (errno) { - case ENOTDIR: - printf("Error: Not a directory\n"); - break; - case ENOENT: - printf("Error: No such file or directory\n"); - break; - case EINVAL: - printf("Error: Invalid parameter or operation\n"); - break; - case ENOTSUP: - printf("Error: Not supported\n"); - break; - case EROFS: - printf("Error: File system is read only\n"); - break; - case ENOMEM: - printf("Error: Out of memory\n"); - break; - case EISDIR: - printf("Error: This is a directory\n"); - break; - default: - printf("Error: (%d)\n", errno); - break; - } -} - -void -sigint_handle(int signum) -{ - return; -} - -void -do_cat(const char* file) -{ - int fd = open(file, 0); - if (fd < 0) { - sh_printerr(); - } else { - int sz; - while ((sz = read(fd, cat_buf, 1024)) > 0) { - write(stdout, cat_buf, sz); - } - if (sz < 0) { - sh_printerr(); - } - close(fd); - printf("\n"); - } -} - -void -do_ls(const char* path) -{ - int fd = open(path, 0); - if (fd < 0) { - sh_printerr(); - } else { - struct lx_dirent ent = { .d_offset = 0 }; - int status; - while ((status = sys_readdir(fd, &ent)) == 1) { - if (ent.d_type == DT_DIR) { - printf(" \033[3m%s\033[39;49m\n", ent.d_name); - } else { - printf(" %s\n", ent.d_name); - } - } - - if (status < 0) - sh_printerr(); - - close(fd); - } -} - -void -do_mcat(const char* file) -{ - int fd = open(file, 0); - if (fd < 0) { - sh_printerr(); - } else { - void* p = mmap(NULL, 2048, 0, 0, fd, 0); - if ((int)p < 0) { - sh_printerr(); - } else { - printf("%s\n", p); - } - munmap(p, 1); - close(fd); - printf("\n"); - } -} - -void -sh_loop() -{ - char buf[512]; - char *cmd, *argpart; - pid_t p; - signal(SIGINT, sigint_handle); - - // set our shell as foreground process - // (unistd.h:tcsetpgrp is essentially a wrapper of this) - // stdout (by default, unless user did smth) is the tty we are currently at - ioctl(stdout, TIOCSPGRP, getpgid()); - - while (1) { - getcwd(pwd, 512); - printf("[\033[2m%s\033[39;49m]$ ", pwd); - size_t sz = read(stdin, buf, 511); - if (sz < 0) { - printf("fail to read user input (%d)\n", geterrno()); - return; - } - buf[sz] = '\0'; - parse_cmdline(buf, &cmd, &argpart); - if (cmd[0] == 0) { - printf("\n"); - goto cont; - } - if (streq(cmd, "cd")) { - if (chdir(argpart) < 0) { - sh_printerr(); - } - goto cont; - } else if (streq(cmd, "clear")) { - ioctl(stdout, TIOCCLSBUF); - goto cont; - } else if (streq(cmd, "ls")) { - if (!(p = fork())) { - do_ls(argpart); - _exit(0); - } - } else if (streq(cmd, "cat")) { - if (!(p = fork())) { - do_cat(argpart); - _exit(0); - } - } else if (streq(cmd, "mcat")) { - if (!(p = fork())) { - do_mcat(argpart); - _exit(0); - } - } else { - printf("unknow command\n"); - goto cont; - } - setpgid(p, getpgid()); - waitpid(p, NULL, 0); - cont: - printf("\n"); - } -} - -void -sh_main() -{ - printf("\n Simple shell. Use or to scroll.\n\n"); - if (!fork()) { - sh_loop(); - _exit(0); - } - wait(NULL); -} \ No newline at end of file diff --git a/lunaix-os/kernel/device/devfs.c b/lunaix-os/kernel/device/devfs.c index 69de7fa..867f93d 100644 --- a/lunaix-os/kernel/device/devfs.c +++ b/lunaix-os/kernel/device/devfs.c @@ -3,7 +3,7 @@ #include #include -#include +#include extern struct v_inode_ops devfs_inode_ops; extern struct v_file_ops devfs_file_ops; diff --git a/lunaix-os/kernel/fs/iso9660/directory.c b/lunaix-os/kernel/fs/iso9660/directory.c index 71da904..1d35978 100644 --- a/lunaix-os/kernel/fs/iso9660/directory.c +++ b/lunaix-os/kernel/fs/iso9660/directory.c @@ -6,7 +6,7 @@ #include -#include +#include extern struct cake_pile* drec_cache_pile; diff --git a/lunaix-os/kernel/fs/vfs.c b/lunaix-os/kernel/fs/vfs.c index d3d1023..8a4a8a1 100644 --- a/lunaix-os/kernel/fs/vfs.c +++ b/lunaix-os/kernel/fs/vfs.c @@ -56,7 +56,7 @@ #include -#include +#include static struct cake_pile* dnode_pile; static struct cake_pile* inode_pile; diff --git a/lunaix-os/kernel/loader/exec.c b/lunaix-os/kernel/loader/exec.c index 3c40f6c..1c206d2 100644 --- a/lunaix-os/kernel/loader/exec.c +++ b/lunaix-os/kernel/loader/exec.c @@ -13,6 +13,11 @@ size_t exec_str_size(const char** str_arr, size_t* length) { + if (!str_arr) { + *length = 0; + return 0; + } + const char* chr = *str_arr; size_t sz = 0, len = 0; @@ -58,10 +63,10 @@ __exec_remap_heap(struct ld_param* param, struct proc_mm* pvms) } int -exec_loadto(struct ld_param* param, - struct v_file* executable, - const char** argv, - const char** envp) +exec_load(struct ld_param* param, + struct v_file* executable, + const char** argv, + const char** envp) { int errno = 0; @@ -88,7 +93,6 @@ exec_loadto(struct ld_param* param, .mlen = MAX_VAR_PAGES * PG_SIZE }; void* mapped; - isr_param* intr_ctx = ¶m->proc->intr_ctx; if ((errno = __exec_remap_heap(param, pvms))) { goto done; @@ -103,8 +107,10 @@ exec_loadto(struct ld_param* param, // make some handy infos available to user space ptr_t arg_start = mapped + sizeof(struct usr_exec_param); - memcpy(arg_start, (void*)argv, sz_argv); - memcpy(arg_start + sz_argv, (void*)envp, sz_envp); + if (argv) + memcpy(arg_start, (void*)argv, sz_argv); + if (envp) + memcpy(arg_start + sz_argv, (void*)envp, sz_envp); struct usr_exec_param* param = mapped; *param = (struct usr_exec_param){ .argc = argv_len, @@ -114,28 +120,22 @@ exec_loadto(struct ld_param* param, .info = param->info }; ptr_t* ustack = (ptr_t*)USTACK_TOP; ustack[-1] = (ptr_t)mapped; - intr_ctx->esp = &ustack[-1]; + param->info.stack_top = &ustack[-1]; } else { // TODO need to find a way to inject argv and envp remotely fail("not implemented"); } - intr_ctx->eip = param->info.ehdr_out.e_entry; - // we will jump to new entry point (_u_start) upon syscall's - // return so execve 'will not return' from the perspective of it's invoker - + param->info.entry = param->info.ehdr_out.e_entry; done: return errno; } -__DEFINE_LXSYSCALL3(int, - execve, - const char*, - filename, - const char*, - argv[], - const char*, - envp[]) +int +exec_load_byname(struct ld_param* param, + const char* filename, + const char** argv, + const char** envp) { int errno = 0; struct v_dnode* dnode; @@ -149,12 +149,28 @@ __DEFINE_LXSYSCALL3(int, goto done; } + if ((errno = exec_load(param, file, argv, envp))) { + vfs_pclose(file, __current->pid); + } + +done: + return errno; +} + +__DEFINE_LXSYSCALL3(int, + execve, + const char*, + filename, + const char*, + argv[], + const char*, + envp[]) +{ + int errno = 0; struct ld_param ldparam; ld_create_param(&ldparam, __current, VMS_SELF); - if ((errno = exec_loadto(&ldparam, file, argv, envp))) { - vfs_pclose(file, __current->pid); - + if ((errno = exec_load_byname(&ldparam, filename, argv, envp))) { if ((ldparam.status & LD_STAT_FKUP)) { // we fucked up our address space. terminate_proc(11451); @@ -163,6 +179,13 @@ __DEFINE_LXSYSCALL3(int, } } + isr_param* intr_ctx = &__current->intr_ctx; + intr_ctx->esp = ldparam.info.stack_top; + intr_ctx->eip = ldparam.info.entry; + + // we will jump to new entry point (_u_start) upon syscall's + // return so execve 'will not return' from the perspective of it's invoker + done: return errno; } \ No newline at end of file diff --git a/lunaix-os/kernel/lxinit.c b/lunaix-os/kernel/lxinit.c deleted file mode 100644 index 8d4980d..0000000 --- a/lunaix-os/kernel/lxinit.c +++ /dev/null @@ -1,90 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -extern uint8_t __kernel_start; - -LOG_MODULE("INIT") - -// #define FORK_BOMB_DEMO -#define WAIT_DEMO -#define IN_USER_MODE - -void __USER__ -_lxinit_main() -{ -#ifdef FORK_BOMB_DEMO - // fork炸弹 - for (;;) { - pid_t p; - if ((p = fork())) { - kprintf(KDEBUG "Pinkie Pie #%d: FUN!\n", p); - } - } -#endif - - int status; -#ifdef WAIT_DEMO - // 测试wait - kprintf("I am parent, going to fork my child and wait.\n"); - if (!fork()) { - kprintf("I am child, going to sleep for 2 seconds\n"); - sleep(2); - kprintf("I am child, I am about to terminated\n"); - _exit(1); - } - wait(&status); - pid_t child = wait(&status); - kprintf("I am parent, my child (%d) terminated normally with code: %d.\n", - child, - WEXITSTATUS(status)); -#endif - - pid_t p = 0; - - if (!fork()) { - kprintf("Test no hang!\n"); - sleep(6); - _exit(0); - } - - waitpid(-1, &status, WNOHANG); - - for (size_t i = 0; i < 5; i++) { - pid_t pid = 0; - if (!(pid = fork())) { - sleep(i); - if (i == 3) { - i = *(int*)0xdeadc0de; // seg fault! - } - kprintf(KINFO "%d\n", i); - _exit(0); - } - kprintf(KINFO "Forked %d\n", pid); - } - - while ((p = wait(&status)) >= 0) { - short code = WEXITSTATUS(status); - if (WIFEXITED(status)) { - kprintf(KINFO "Process %d exited with code %d\n", p, code); - } else { - kprintf(KWARN "Process %d aborted with code %d\n", p, code); - } - } - - char buf[64]; - - kprintf(KINFO "Hello processes!\n"); - - cpu_get_brand(buf); - kprintf("CPU: %s\n\n", buf); - - _exit(0); -} \ No newline at end of file diff --git a/lunaix-os/kernel/proc0.c b/lunaix-os/kernel/proc0.c index 6f39299..ef97c77 100644 --- a/lunaix-os/kernel/proc0.c +++ b/lunaix-os/kernel/proc0.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -15,10 +16,6 @@ #include #include -#include -#include -#include - #include #include @@ -32,13 +29,8 @@ #include -#include - LOG_MODULE("PROC0") -extern void -_lxinit_main(); /* lxinit.c */ - void init_platform(); @@ -51,84 +43,59 @@ unlock_reserved_memory(); void __do_reserved_memory(int unlock); -#define USE_DEMO -// #define DEMO_SIGNAL -// #define DEMO_READDIR -// #define DEMO_IOTEST -// #define DEMO_INPUT_TEST -#define DEMO_SIMPLE_SH - -extern void -_pconsole_main(); - -extern void -_signal_demo_main(); - -extern void -_lxinit_main(); - -extern void -_readdir_main(); - -extern void -_iotest_main(); - -extern void -input_test(); - -extern void -sh_main(); - -void __USER__ -__setup_dir() +int +mount_bootmedium() { - int errno; - mkdir("/mnt"); - mkdir("/mnt/lunaix-os"); + struct v_dnode* dnode; + int errno = 0; + if ((errno = vfs_walk_proc("/dev/sdb", &dnode, NULL, 0))) { + kprintf(KERROR "fail to acquire device. (%d)", errno); + return 0; + } - if ((errno = mount("/dev/sdb", "/mnt/lunaix-os", "iso9660", 0))) { - syslog(2, "fail mounting boot medium. (%d)\n", errno); + struct device* dev = (struct device*)dnode->inode->data; + if ((errno = vfs_mount("/mnt/lunaix-os", "iso9660", dev, 0))) { + kprintf(KERROR "fail to boot medium. (%d)", errno); + return 0; } + + return 1; } -void __USER__ -__proc0_usr() +int +exec_initd() { - // 打开tty设备(控制台),作为标准输入输出。 - // tty设备属于序列设备(Sequential Device),该类型设备的上层读写 - // 无须经过Lunaix的缓存层,而是直接下发到底层驱动。(不受FO_DIRECT的影响) - int fdstdout = open("/dev/tty", 0); - int fdstdin = dup2(stdout, 1); - - __setup_dir(); - - pid_t p; - - if (!(p = fork())) { -#ifndef USE_DEMO - _exit(0); -#elif defined DEMO_SIGNAL - _signal_demo_main(); -#elif defined DEMO_READDIR - _readdir_main(); -#elif defined DEMO_IOTEST - _iotest_main(); -#elif defined DEMO_INPUT_TEST - input_test(); -#elif defined DEMO_SIMPLE_SH - sh_main(); -#else - _lxinit_main(); -#endif - printf("==== test end ====\n"); - _exit(0); - } + int errno = 0; + struct ld_param param; + char filename[] = "/mnt/lunaix-os/usr/initd"; - waitpid(p, 0, 0); + ld_create_param(¶m, __current, VMS_SELF); - while (1) { - yield(); + if ((errno = exec_load_byname(¶m, filename, NULL, NULL))) { + goto fail; } + + // user space + asm volatile("movw %0, %%ax\n" + "movw %%ax, %%es\n" + "movw %%ax, %%ds\n" + "movw %%ax, %%fs\n" + "movw %%ax, %%gs\n" + "pushl %0\n" + "pushl %1\n" + "pushl %2\n" + "pushl %3\n" + "retf" ::"i"(UDATA_SEG), + "r"(param.info.stack_top), + "i"(UCODE_SEG), + "r"(param.info.entry) + : "eax", "memory"); + + fail("should not reach"); + +fail: + kprintf(KERROR "fail to load initd. (%d)", errno); + return 0; } /** @@ -146,21 +113,12 @@ __proc0() init_proc_user_space(__current); - // user space - asm volatile("movw %0, %%ax\n" - "movw %%ax, %%es\n" - "movw %%ax, %%ds\n" - "movw %%ax, %%fs\n" - "movw %%ax, %%gs\n" - "pushl %0\n" - "pushl %1\n" - "pushl %2\n" - "pushl %3\n" - "retf" ::"i"(UDATA_SEG), - "i"(USTACK_TOP & ~0xf), - "i"(UCODE_SEG), - "r"(__proc0_usr) - : "eax", "memory"); + if (!mount_bootmedium() || !exec_initd()) { + while (1) { + asm("hlt"); + } + // should not reach + } } extern uint8_t __kernel_start; /* link/linker.ld */ diff --git a/lunaix-os/kernel/process/taskfs.c b/lunaix-os/kernel/process/taskfs.c index 6507eb1..ca7f61e 100644 --- a/lunaix-os/kernel/process/taskfs.c +++ b/lunaix-os/kernel/process/taskfs.c @@ -7,7 +7,7 @@ #include #include -#include +#include #define COUNTER_MASK ((1 << 16) - 1) diff --git a/lunaix-os/libs/ulibc/printf.c b/lunaix-os/libs/ulibc/printf.c index 86c0b18..3d0b0f6 100644 --- a/lunaix-os/libs/ulibc/printf.c +++ b/lunaix-os/libs/ulibc/printf.c @@ -2,7 +2,7 @@ #include #include -#include +#include // This is VERY bad implementation as it mixes both kernel and user space // code together. It is here however, just for the convenience of our testing diff --git a/lunaix-os/makefile b/lunaix-os/makefile index ede34f4..c1c800c 100644 --- a/lunaix-os/makefile +++ b/lunaix-os/makefile @@ -3,11 +3,7 @@ include config/make-os include config/make-cc include config/make-debug-tool -DEPS := $(CC) $(LD) xorriso grub-mkrescue - -INCLUDES := $(patsubst %, -I%, $(INCLUDES_DIR)) -SOURCE_FILES := $(shell find -name "*.[cS]") -SRC := $(patsubst ./%, $(OBJECT_DIR)/%.o, $(SOURCE_FILES)) +DEPS := $(CC) $(LD) $(AR) xorriso grub-mkrescue $(DEPS): @echo -n "checking $@ .... " @@ -21,47 +17,45 @@ check-cc: @echo -n "checking target i686-elf.... " @test "`i686-elf-gcc -dumpmachine`" = "i686-elf" && echo ok || (echo "failed" && exit 1) -check: $(DEPS) check-cc - $(OBJECT_DIR): @mkdir -p $(OBJECT_DIR) $(BIN_DIR): @mkdir -p $(BIN_DIR) +$(USR_DIR): + @mkdir -p $(USR_DIR) + $(ISO_DIR): @mkdir -p $(ISO_DIR) @mkdir -p $(ISO_BOOT_DIR) @mkdir -p $(ISO_GRUB_DIR) -$(OBJECT_DIR)/%.S.o: %.S - @mkdir -p $(@D) - @echo " CC $<" - @$(CC) $(INCLUDES) -c $< -o $@ +check: $(DEPS) check-cc GRUB_TEMPLATE + +prepare: check $(OBJECT_DIR) $(BIN_DIR) $(ISO_DIR) $(USR_DIR) + +usrlib: prepare makefile.usr + @make -f makefile.usr usr-runtime + +usrlib-debug: prepare makefile.usr + @make -f makefile.usr usr-runtime-debug -$(OBJECT_DIR)/%.c.o: %.c - @mkdir -p $(@D) - @echo " CC $<" - @$(CC) $(INCLUDES) -c $< -o $@ $(CFLAGS) +usr-prog: prepare usrlib makefile.prog + @make -f makefile.prog all -$(BIN_DIR)/$(OS_BIN): $(OBJECT_DIR) $(BIN_DIR) $(SRC) - @echo " LD $(BIN_DIR)/$(OS_BIN)" - @$(CC) -T link/linker.ld -o $(BIN_DIR)/$(OS_BIN) $(SRC) $(LDFLAGS) +bootable: usr-prog usrlib makefile.ker + @make -f makefile.ker all -$(BUILD_DIR)/$(OS_ISO): check $(ISO_DIR) $(BIN_DIR)/$(OS_BIN) GRUB_TEMPLATE - @./config-grub.sh ${OS_NAME} $(ISO_GRUB_DIR)/grub.cfg - @cp $(BIN_DIR)/$(OS_BIN) $(ISO_BOOT_DIR) - @grub-mkrescue -o $(BUILD_DIR)/$(OS_ISO) $(ISO_DIR) +bootable-debug: usr-prog usrlib-debug makefile.ker + @make -f makefile.ker all-debug -all: $(BUILD_DIR)/$(OS_ISO) +all: bootable instable: CFLAGS := -g -std=gnu99 -ffreestanding $(O) $(W) $(ARCH_OPT) -D__LUNAIXOS_DEBUG__ instable: all -all-debug: O := -Og -all-debug: CFLAGS := -g -std=gnu99 -ffreestanding $(O) $(W) $(ARCH_OPT) -D__LUNAIXOS_DEBUG__ -all-debug: LDFLAGS := -g -ffreestanding $(O) -nostdlib -lgcc -all-debug: clean $(BUILD_DIR)/$(OS_ISO) +all-debug: clean bootable-debug @echo "Dumping the disassembled kernel code to $(BUILD_DIR)/kdump.txt" @i686-elf-objdump -S $(BIN_DIR)/$(OS_BIN) > $(BUILD_DIR)/kdump.txt diff --git a/lunaix-os/makefile.ker b/lunaix-os/makefile.ker new file mode 100644 index 0000000..8411140 --- /dev/null +++ b/lunaix-os/makefile.ker @@ -0,0 +1,41 @@ +include config/make-os +include config/make-cc +include config/make-debug-tool +include config/make-locations + +SRC_DIRS := kernel \ + hal \ + debug \ + libs \ + arch \ + +SRC_FILES := $(foreach f, $(SRC_DIRS), $(shell find $(f) -name "*.[cS]")) + +OBJS := $(foreach f, $(SRC_FILES), $(OBJECT_DIR)/$(f).o) + +$(OBJECT_DIR)/%.S.o: %.S + @mkdir -p $(@D) + @echo " CC $<" + @$(CC) $(INCLUDES) -c $< -o $@ + +$(OBJECT_DIR)/%.c.o: %.c + @mkdir -p $(@D) + @echo " CC $<" + @$(CC) $(INCLUDES) -c $< -o $@ $(CFLAGS) + +$(BIN_DIR)/$(OS_BIN): $(OBJECT_DIR) $(BIN_DIR) $(OBJS) + @echo " LD $(BIN_DIR)/$(OS_BIN)" + @$(CC) -T link/linker.ld -o $(BIN_DIR)/$(OS_BIN) $(OBJS) $(BIN_DIR)/$(USR_LIB) $(LDFLAGS) + +$(BUILD_DIR)/$(OS_ISO): $(BIN_DIR)/$(OS_BIN) + @./config-grub.sh ${OS_NAME} $(ISO_GRUB_DIR)/grub.cfg + @cp $(BIN_DIR)/$(OS_BIN) $(ISO_BOOT_DIR) + @cp -r $(USR_DIR) $(ISO_DIR) + @grub-mkrescue -o $(BUILD_DIR)/$(OS_ISO) $(ISO_DIR) + +all: $(BUILD_DIR)/$(OS_ISO) + +all-debug: O := -Og +all-debug: CFLAGS := -g -std=gnu99 -ffreestanding $(O) $(W) $(ARCH_OPT) -D__LUNAIXOS_DEBUG__ +all-debug: LDFLAGS := -g -ffreestanding $(O) -nostdlib -lgcc +all-debug: all \ No newline at end of file diff --git a/lunaix-os/makefile.prog b/lunaix-os/makefile.prog new file mode 100644 index 0000000..1c09c78 --- /dev/null +++ b/lunaix-os/makefile.prog @@ -0,0 +1,12 @@ +include config/make-locations +include config/make-os +include config/make-cc + +SRC_FILES := $(wildcard uprog/*.c) +PROGRAMES := $(patsubst uprog/%.c, $(USR_DIR)/%, $(SRC_FILES)) + +$(USR_DIR)/%: + @echo " BUILD $(*F)" + @$(CC) -T usr/link-usr.ld $(INCLUDES) $(CFLAGS) $(LDFLAGS) uprog/$(*F).c $(BIN_DIR)/$(USR_LIB) -o $@ + +all: $(PROGRAMES) \ No newline at end of file diff --git a/lunaix-os/makefile.usr b/lunaix-os/makefile.usr new file mode 100644 index 0000000..d71ad71 --- /dev/null +++ b/lunaix-os/makefile.usr @@ -0,0 +1,32 @@ +include config/make-cc +include config/make-os +include config/make-locations + +SRC_DIRS := usr + +SRC_FILES := $(foreach f, $(SRC_DIRS), $(shell find $(f) -name "*.[cS]")) + +OBJS := $(foreach f, $(SRC_FILES), $(OBJECT_DIR)/$(f).o) + +$(OBJECT_DIR)/%.S.o: %.S + @mkdir -p $(@D) + @echo " CC $<" + @$(CC) $(INCLUDES) -c $< -o $@ + +$(OBJECT_DIR)/%.c.o: %.c + @mkdir -p $(@D) + @echo " CC $<" + @$(CC) $(INCLUDES) -c $< -o $@ $(CFLAGS) + +$(BIN_DIR)/$(USR_LIB): $(OBJS) + @echo " AR $@" + @$(AR) rcs $@ $^ + +usr-runtime: $(BIN_DIR)/$(USR_LIB) + +usr-runtime-debug: O := -Og +usr-runtime-debug: CFLAGS := -g -std=gnu99 -ffreestanding $(O) $(W) $(ARCH_OPT) -D__LUNAIXOS_DEBUG__ +usr-runtime-debug: LDFLAGS := -g -ffreestanding $(O) -nostdlib -lgcc +usr-runtime-debug: usr-runtime + +usr-objs: $(OBJS) \ No newline at end of file diff --git a/lunaix-os/uprog/init.c b/lunaix-os/uprog/init.c new file mode 100644 index 0000000..a197245 --- /dev/null +++ b/lunaix-os/uprog/init.c @@ -0,0 +1,23 @@ +#include +#include +#include + +int +main(int argc, const char** argv) +{ + int errno = 0; + + if ((errno = open("/dev/tty", 0))) { + syslog(2, "fail to open tty (%d)\n", errno); + return 0; + } + + if ((errno = dup(errno))) { + syslog(2, "fail to setup tty i/o (%d)\n", errno); + return 0; + } + + syslog(0, "user space!\n"); + + return 0; +} \ No newline at end of file diff --git a/lunaix-os/usr/api/dirent.c b/lunaix-os/usr/api/dirent.c index 403d4e3..10d9213 100644 --- a/lunaix-os/usr/api/dirent.c +++ b/lunaix-os/usr/api/dirent.c @@ -1,4 +1,4 @@ #include -#include +#include __LXSYSCALL2(int, sys_readdir, int, fd, struct lx_dirent*, dent) diff --git a/lunaix-os/usr/api/errno.c b/lunaix-os/usr/api/errno.c index 27b4124..ac0d004 100644 --- a/lunaix-os/usr/api/errno.c +++ b/lunaix-os/usr/api/errno.c @@ -1,4 +1,4 @@ #include -#include +#include __LXSYSCALL(int, geterrno); \ No newline at end of file diff --git a/lunaix-os/usr/api/fcntl.c b/lunaix-os/usr/api/fcntl.c index 05a2cd4..92e2fa3 100644 --- a/lunaix-os/usr/api/fcntl.c +++ b/lunaix-os/usr/api/fcntl.c @@ -1,4 +1,4 @@ #include -#include +#include __LXSYSCALL2(int, open, const char*, path, int, options) diff --git a/lunaix-os/usr/api/ioctl.c b/lunaix-os/usr/api/ioctl.c index d9ee35b..bb2126a 100644 --- a/lunaix-os/usr/api/ioctl.c +++ b/lunaix-os/usr/api/ioctl.c @@ -1,4 +1,4 @@ #include -#include +#include __LXSYSCALL2_VARG(int, ioctl, int, fd, int, req); \ No newline at end of file diff --git a/lunaix-os/usr/api/lunaix.c b/lunaix-os/usr/api/lunaix.c index c6c5f81..59fe79a 100644 --- a/lunaix-os/usr/api/lunaix.c +++ b/lunaix-os/usr/api/lunaix.c @@ -1,5 +1,5 @@ #include -#include +#include __LXSYSCALL(void, yield); diff --git a/lunaix-os/usr/api/mann.c b/lunaix-os/usr/api/mann.c index 3c167a3..e211cad 100644 --- a/lunaix-os/usr/api/mann.c +++ b/lunaix-os/usr/api/mann.c @@ -1,5 +1,5 @@ #include -#include +#include __LXSYSCALL2_VARG(void*, sys_mmap, void*, addr, size_t, length); diff --git a/lunaix-os/usr/api/mount.c b/lunaix-os/usr/api/mount.c index e073f37..854c96b 100644 --- a/lunaix-os/usr/api/mount.c +++ b/lunaix-os/usr/api/mount.c @@ -1,5 +1,5 @@ #include -#include +#include __LXSYSCALL4(int, mount, diff --git a/lunaix-os/usr/api/signal.c b/lunaix-os/usr/api/signal.c index 6e9039c..191e8d8 100644 --- a/lunaix-os/usr/api/signal.c +++ b/lunaix-os/usr/api/signal.c @@ -1,5 +1,5 @@ #include -#include +#include __LXSYSCALL2(int, signal, int, signum, sighandler_t, handler); diff --git a/lunaix-os/usr/api/unistd.c b/lunaix-os/usr/api/unistd.c index 78891d1..e8a4a0a 100644 --- a/lunaix-os/usr/api/unistd.c +++ b/lunaix-os/usr/api/unistd.c @@ -1,5 +1,5 @@ #include -#include +#include __LXSYSCALL(pid_t, fork) diff --git a/lunaix-os/includes/usr/errno.h b/lunaix-os/usr/includes/errno.h similarity index 100% rename from lunaix-os/includes/usr/errno.h rename to lunaix-os/usr/includes/errno.h diff --git a/lunaix-os/includes/usr/fcntl.h b/lunaix-os/usr/includes/fcntl.h similarity index 70% rename from lunaix-os/includes/usr/fcntl.h rename to lunaix-os/usr/includes/fcntl.h index 2a83c93..0ce31d8 100644 --- a/lunaix-os/includes/usr/fcntl.h +++ b/lunaix-os/usr/includes/fcntl.h @@ -1,8 +1,8 @@ #ifndef __LUNAIX_SYS_FCNTL_H #define __LUNAIX_SYS_FCNTL_H -#include -#include +#include +#include int open(const char* path, int flags); diff --git a/lunaix-os/includes/usr/fcntl_defs.h b/lunaix-os/usr/includes/fcntl_defs.h similarity index 100% rename from lunaix-os/includes/usr/fcntl_defs.h rename to lunaix-os/usr/includes/fcntl_defs.h diff --git a/lunaix-os/includes/usr/signal.h b/lunaix-os/usr/includes/signal.h similarity index 83% rename from lunaix-os/includes/usr/signal.h rename to lunaix-os/usr/includes/signal.h index cc7c7ed..56da91e 100644 --- a/lunaix-os/includes/usr/signal.h +++ b/lunaix-os/usr/includes/signal.h @@ -1,8 +1,8 @@ #ifndef __LUNAIX_SYS_SIGNAL_H #define __LUNAIX_SYS_SIGNAL_H -#include -#include +#include +#include int signal(int signum, sighandler_t handler); diff --git a/lunaix-os/includes/usr/signal_defs.h b/lunaix-os/usr/includes/signal_defs.h similarity index 100% rename from lunaix-os/includes/usr/signal_defs.h rename to lunaix-os/usr/includes/signal_defs.h diff --git a/lunaix-os/includes/usr/sys/dirent.h b/lunaix-os/usr/includes/sys/dirent.h similarity index 81% rename from lunaix-os/includes/usr/sys/dirent.h rename to lunaix-os/usr/includes/sys/dirent.h index 54334d3..101f050 100644 --- a/lunaix-os/includes/usr/sys/dirent.h +++ b/lunaix-os/usr/includes/sys/dirent.h @@ -1,7 +1,7 @@ #ifndef __LUNAIX_SYS_DIRENT_H #define __LUNAIX_SYS_DIRENT_H -#include +#include int sys_readdir(int fd, struct lx_dirent* dirent); diff --git a/lunaix-os/includes/usr/sys/dirent_defs.h b/lunaix-os/usr/includes/sys/dirent_defs.h similarity index 100% rename from lunaix-os/includes/usr/sys/dirent_defs.h rename to lunaix-os/usr/includes/sys/dirent_defs.h diff --git a/lunaix-os/includes/usr/sys/ioctl.h b/lunaix-os/usr/includes/sys/ioctl.h similarity index 100% rename from lunaix-os/includes/usr/sys/ioctl.h rename to lunaix-os/usr/includes/sys/ioctl.h diff --git a/lunaix-os/includes/usr/sys/ioctl_defs.h b/lunaix-os/usr/includes/sys/ioctl_defs.h similarity index 100% rename from lunaix-os/includes/usr/sys/ioctl_defs.h rename to lunaix-os/usr/includes/sys/ioctl_defs.h diff --git a/lunaix-os/includes/usr/sys/lunaix.h b/lunaix-os/usr/includes/sys/lunaix.h similarity index 91% rename from lunaix-os/includes/usr/sys/lunaix.h rename to lunaix-os/usr/includes/sys/lunaix.h index b5ba65e..febcb59 100644 --- a/lunaix-os/includes/usr/sys/lunaix.h +++ b/lunaix-os/usr/includes/sys/lunaix.h @@ -1,7 +1,7 @@ #ifndef __LUNAIX_SYS_LUNAIX_H #define __LUNAIX_SYS_LUNAIX_H -#include +#include void yield(); diff --git a/lunaix-os/includes/usr/sys/mann.h b/lunaix-os/usr/includes/sys/mann.h similarity index 78% rename from lunaix-os/includes/usr/sys/mann.h rename to lunaix-os/usr/includes/sys/mann.h index 140ef35..0e424d4 100644 --- a/lunaix-os/includes/usr/sys/mann.h +++ b/lunaix-os/usr/includes/sys/mann.h @@ -1,8 +1,8 @@ #ifndef __LUNAIX_SYS_MANN_H #define __LUNAIX_SYS_MANN_H -#include -#include +#include +#include void* mmap(void* addr, size_t length, int proct, int flags, int fd, off_t offset); diff --git a/lunaix-os/includes/usr/sys/mann_flags.h b/lunaix-os/usr/includes/sys/mann_flags.h similarity index 100% rename from lunaix-os/includes/usr/sys/mann_flags.h rename to lunaix-os/usr/includes/sys/mann_flags.h diff --git a/lunaix-os/includes/usr/sys/mount.h b/lunaix-os/usr/includes/sys/mount.h similarity index 91% rename from lunaix-os/includes/usr/sys/mount.h rename to lunaix-os/usr/includes/sys/mount.h index 0a5a5a5..5d12df8 100644 --- a/lunaix-os/includes/usr/sys/mount.h +++ b/lunaix-os/usr/includes/sys/mount.h @@ -1,7 +1,7 @@ #ifndef __LUNAIX_SYS_MOUNT_H #define __LUNAIX_SYS_MOUNT_H -#include +#include int mount(const char* source, const char* target, const char* fstype, int flags); diff --git a/lunaix-os/includes/usr/sys/types.h b/lunaix-os/usr/includes/sys/types.h similarity index 100% rename from lunaix-os/includes/usr/sys/types.h rename to lunaix-os/usr/includes/sys/types.h diff --git a/lunaix-os/includes/usr/unistd.h b/lunaix-os/usr/includes/unistd.h similarity index 98% rename from lunaix-os/includes/usr/unistd.h rename to lunaix-os/usr/includes/unistd.h index 5232a80..90740a0 100644 --- a/lunaix-os/includes/usr/unistd.h +++ b/lunaix-os/usr/includes/unistd.h @@ -1,7 +1,7 @@ #ifndef __LUNAIX_SYS_UNISTD_H #define __LUNAIX_SYS_UNISTD_H -#include +#include pid_t fork(); diff --git a/lunaix-os/usr/link-usr.ld b/lunaix-os/usr/link-usr.ld new file mode 100644 index 0000000..49fb4c9 --- /dev/null +++ b/lunaix-os/usr/link-usr.ld @@ -0,0 +1,5 @@ +ENTRY(_u_start) + +SECTIONS { + . = 0x400000; +} \ No newline at end of file diff --git a/lunaix-os/usr/uinit.c b/lunaix-os/usr/uinit.c new file mode 100644 index 0000000..ab383c2 --- /dev/null +++ b/lunaix-os/usr/uinit.c @@ -0,0 +1,10 @@ +#define __USR_WRAPPER__ +#include + +int +usr_pre_init(struct usr_exec_param* param) +{ + // TODO some inits before executing user program + + return 0; +} \ No newline at end of file diff --git a/lunaix-os/usr/uwrap.S b/lunaix-os/usr/uwrap.S new file mode 100644 index 0000000..1f07722 --- /dev/null +++ b/lunaix-os/usr/uwrap.S @@ -0,0 +1,23 @@ +#define __ASM__ +#include + +.section .text + .global _u_start + _u_start: + call usr_pre_init + jnz 1f + + popl %eax + + pushl (%eax) // argc + pushl 4(%eax) // argv + + xorl %eax, %eax + call main + + 1: + movl %eax, %ebx + movl $__SYSCALL__exit, %eax + int $LUNAIX_SYS_CALL + + ud2 // should not reach \ No newline at end of file -- 2.27.0