From f02487d856e6fff03de7574f9774bc46b7d73ddc Mon Sep 17 00:00:00 2001 From: Yann MAGNIN Date: Fri, 20 Mar 2020 22:47:05 +0100 Subject: [PATCH] Update librairies part + auto-load TTY device when STD[OUT/ERR/IN]_FILENO is specified + Try to create dynamic libs (not work) --- global.mk | 2 +- include/kernel/process.h | 38 ++++++++-- include/lib/stdio.h | 13 +++- include/lib/unistd.h | 4 + include/user/builtin.h | 4 +- src/kernel/fs/syscall/close.c | 12 ++- src/kernel/fs/syscall/lseek.c | 10 ++- src/kernel/fs/syscall/open.c | 6 +- src/kernel/fs/syscall/read.c | 12 ++- src/kernel/fs/syscall/write.c | 10 ++- src/kernel/scheduler/process/create.c | 10 +-- src/kernel/scheduler/syscall/sys_fexecve.c | 29 ++++++-- src/lib/Makefile | 75 +++++++++++-------- src/lib/libc/stdio/printf.c | 13 ++++ src/lib/libc/stdio/putc.c | 11 +++ src/lib/libc/stdio/puts.c | 13 ++++ src/lib/libc/stdio/sprintf.c | 12 +++ src/lib/libc/stdio/vdprintf.c | 8 ++ src/lib/{ => libc}/stdio/vsprintf.c | 1 + src/lib/{ => libc}/string/memcpy.c | 0 src/lib/{ => libc}/string/memset.c | 0 src/lib/{ => libc}/string/strcat.c | 0 src/lib/{ => libc}/string/strchr.c | 0 src/lib/{ => libc}/string/strcmp.c | 0 src/lib/{ => libc}/string/strcpy.c | 0 src/lib/{ => libc}/string/strlen.c | 0 src/lib/{ => libc}/unistd/close.S | 0 src/lib/{ => libc}/unistd/fexecve.S | 0 src/lib/{ => libc}/unistd/fork.S | 0 src/lib/{ => libc}/unistd/lseek.S | 0 src/lib/{ => libc}/unistd/open.S | 0 src/lib/{ => libc}/unistd/read.S | 0 src/lib/{ => libc}/unistd/waitpid.S | 0 src/lib/{ => libc}/unistd/write.S | 0 src/lib/stdio/sprintf.c | 10 --- src/lib/user_shared_lib.ld | 86 ++++++++++++++++++++++ src/user/shell/builtin/proc.c | 35 ++------- src/user/shell/main.c | 22 +++--- src/user/shell/util/check_builtin.c | 2 +- src/user/test/Makefile | 12 ++- src/user/test/test.ld | 59 ++++++++++----- 41 files changed, 367 insertions(+), 142 deletions(-) create mode 100644 src/lib/libc/stdio/printf.c create mode 100644 src/lib/libc/stdio/putc.c create mode 100644 src/lib/libc/stdio/puts.c create mode 100644 src/lib/libc/stdio/sprintf.c create mode 100644 src/lib/libc/stdio/vdprintf.c rename src/lib/{ => libc}/stdio/vsprintf.c (99%) rename src/lib/{ => libc}/string/memcpy.c (100%) rename src/lib/{ => libc}/string/memset.c (100%) rename src/lib/{ => libc}/string/strcat.c (100%) rename src/lib/{ => libc}/string/strchr.c (100%) rename src/lib/{ => libc}/string/strcmp.c (100%) rename src/lib/{ => libc}/string/strcpy.c (100%) rename src/lib/{ => libc}/string/strlen.c (100%) rename src/lib/{ => libc}/unistd/close.S (100%) rename src/lib/{ => libc}/unistd/fexecve.S (100%) rename src/lib/{ => libc}/unistd/fork.S (100%) rename src/lib/{ => libc}/unistd/lseek.S (100%) rename src/lib/{ => libc}/unistd/open.S (100%) rename src/lib/{ => libc}/unistd/read.S (100%) rename src/lib/{ => libc}/unistd/waitpid.S (100%) rename src/lib/{ => libc}/unistd/write.S (100%) delete mode 100644 src/lib/stdio/sprintf.c create mode 100644 src/lib/user_shared_lib.ld diff --git a/global.mk b/global.mk index 4bf9387..27d6fa8 100644 --- a/global.mk +++ b/global.mk @@ -1,7 +1,7 @@ # Global option use in each Makefile # Tools -COMPILER := sh3eb-elf- +COMPILER := sh-elf- CC := $(COMPILER)gcc LD := $(COMPILER)ld OBJCOPY := $(COMPILER)objcopy diff --git a/include/kernel/process.h b/include/kernel/process.h index e98c5f1..b5541ea 100644 --- a/include/kernel/process.h +++ b/include/kernel/process.h @@ -18,19 +18,29 @@ //TODO: signal ! struct process { + //--- // Used when interrupt or exception occur + //--- struct { uint32_t kernel; uint32_t user; } stack; + //--- // Context management + //--- common_context_t context; - // Process name. + + //--- + // Private data + //--- char name[PROCESS_NAME_LENGHT]; - // Open file management + + //--- + // Shared (child / parent) data informations + //--- struct { enum { PROCESS_FILE_SLOT_UNUSED, @@ -39,16 +49,22 @@ struct process FILE file; } opfile[PROCESS_NB_OPEN_FILE]; struct dentry *working_dir; + FILE tty; - // ignals management. + //--- + // Signals management. + //--- //sighandler_t signal[NSIG]; + + //--- // Virtual / Physical memory management. + // // @note - // For now, we can not use the MMU - // so we just save all physical allocated - // space. This is an hardcode of each - // process memory management. + // For now, we can not use the MMU so we just + // save all physical allocated space. This is an + // hardcode of each process memory management. + //--- struct { struct { uint32_t user; @@ -67,13 +83,17 @@ struct process uint32_t size; } exit; } memory; - + + + //--- // Other process management. + //--- struct process *parent; struct process *child; struct process *next; }; + // Internal struct used by the // static process stack struct process_stack @@ -88,12 +108,14 @@ struct process_stack struct process process; }; + // Functions. extern struct process *process_create(const char *name); extern struct process *process_get(pid_t pid); extern pid_t process_get_pid(struct process *process); extern int process_switch(pid_t pid); + // Internal function. extern pid_t process_alloc(struct process **process); extern int process_free(struct process *process); diff --git a/include/lib/stdio.h b/include/lib/stdio.h index a5cb49a..d07a568 100644 --- a/include/lib/stdio.h +++ b/include/lib/stdio.h @@ -6,7 +6,16 @@ #include /* vsprintf(), sprintf() - formatted output conversion. */ -int vsprintf(char *str, char const *format, va_list ap); -void sprintf(char *str, char const *format, ...); +extern int vsprintf(char *str, char const *format, va_list ap); +extern int sprintf(char *str, char const *format, ...); + +/* dprintf(), printf() - display formatted output */ +extern int printf(const char *format, ...); +extern int dprintf(int fd, const char *format, ...); +extern int vdprintf(int fd, const char *format, va_list ap); + +/* putx() - display char / string */ +extern int putchar(int c); +extern int puts(const char *s); #endif /*__LIB_STDIO_H__*/ diff --git a/include/lib/unistd.h b/include/lib/unistd.h index f494c74..64fea56 100644 --- a/include/lib/unistd.h +++ b/include/lib/unistd.h @@ -8,6 +8,10 @@ // Define syscall LIST #include +// TODO: move me +#define STDOUT_FILENO 0 +#define STDERR_FILENO 1 +#define STDIN_FILENO 2 //TODO: move me #define WNOHANG 0 diff --git a/include/user/builtin.h b/include/user/builtin.h index 1baf890..1a1fb37 100644 --- a/include/user/builtin.h +++ b/include/user/builtin.h @@ -27,8 +27,8 @@ struct builtin_s }; // Builtin list -//extern int builtin_proc(void); -//extern int builtin_ram(void); +extern int builtin_proc(void); +extern int builtin_ram(void); #endif /*__USER_BUILTIN_H__*/ diff --git a/src/kernel/fs/syscall/close.c b/src/kernel/fs/syscall/close.c index 9867d5c..3049446 100644 --- a/src/kernel/fs/syscall/close.c +++ b/src/kernel/fs/syscall/close.c @@ -5,10 +5,14 @@ int sys_close(int fd) { extern struct process *process_current; - // Check fd - if (fd < 0 || fd >= PROCESS_NB_OPEN_FILE) + // Check fd validity + if (fd < 0 || fd - 3 >= PROCESS_NB_OPEN_FILE) return (-1); - // call VFS close primitive - return (vfs_close(&process_current->opfile[fd].file)); + // Check virtual file (TTY) + if (fd < 3) + return (0); + + // Call VFS close primitive + return (vfs_close(&process_current->opfile[fd - 3].file)); } diff --git a/src/kernel/fs/syscall/lseek.c b/src/kernel/fs/syscall/lseek.c index 187a694..ae5599e 100644 --- a/src/kernel/fs/syscall/lseek.c +++ b/src/kernel/fs/syscall/lseek.c @@ -6,9 +6,13 @@ off_t sys_lseek(int fd, off_t offset, int whence) extern struct process *process_current; // Check fd - if (fd < 0 || fd >= PROCESS_NB_OPEN_FILE) + if (fd < 0 || fd - 3 >= PROCESS_NB_OPEN_FILE) return (-1); - // call VFS read primitive - return (vfs_lseek(&process_current->opfile[fd].file, offset, whence)); + // Check virtual file (TTY) + if (fd < 3) + return (0); + + // Call VFS lseek primitive + return (vfs_lseek(&process_current->opfile[fd - 3].file, offset, whence)); } diff --git a/src/kernel/fs/syscall/open.c b/src/kernel/fs/syscall/open.c index 89847ab..627ece7 100644 --- a/src/kernel/fs/syscall/open.c +++ b/src/kernel/fs/syscall/open.c @@ -24,5 +24,9 @@ int sys_open(const char *pathname, int flags, ...) return (-1); // Return the file descriptor - return (fd); + // @note: + // * fd = 0 -> STDOUT_FILENO + // * fd = 1 -> STDERR_FILENO + // * fd = 2 -> STDIN_FILENO + return (fd + 3); } diff --git a/src/kernel/fs/syscall/read.c b/src/kernel/fs/syscall/read.c index 8530bd6..3dba265 100644 --- a/src/kernel/fs/syscall/read.c +++ b/src/kernel/fs/syscall/read.c @@ -6,9 +6,17 @@ ssize_t sys_read(int fd, void *buf, size_t count) extern struct process *process_current; // Check fd - if (fd < 0 || fd >= PROCESS_NB_OPEN_FILE) + if (fd < 0 || fd - 3 >= PROCESS_NB_OPEN_FILE) return (-1); + // Check virtual file (TTY) and open it if needed + if (fd < 3) { + if (process_current->tty.private == NULL && + vfs_open(&process_current->tty, "/dev/tty", O_RDWR) != 0) + return (-1); + return (vfs_read(&process_current->tty, buf, count)); + } + // call VFS read primitive - return (vfs_read(&process_current->opfile[fd].file, buf, count)); + return (vfs_read(&process_current->opfile[fd - 3].file, buf, count)); } diff --git a/src/kernel/fs/syscall/write.c b/src/kernel/fs/syscall/write.c index cabb7cf..8071b6d 100644 --- a/src/kernel/fs/syscall/write.c +++ b/src/kernel/fs/syscall/write.c @@ -6,9 +6,17 @@ ssize_t sys_write(int fd, const void *buf, size_t count) extern struct process *process_current; // Check fd - if (fd < 0 || fd >= PROCESS_NB_OPEN_FILE) + if (fd < 0 || fd - 3 >= PROCESS_NB_OPEN_FILE) return (-1); + // Check virtual file (TTY) and open it if needed + if (fd < 3) { + if (process_current->tty.private == NULL && + vfs_open(&process_current->tty, "/dev/tty", O_RDWR) != 0) + return (-1); + return (vfs_write(&process_current->tty, buf, count)); + } + // call VFS read primitive return (vfs_write(&process_current->opfile[fd].file, buf, count)); } diff --git a/src/kernel/scheduler/process/create.c b/src/kernel/scheduler/process/create.c index 2ac5534..e3f1c94 100644 --- a/src/kernel/scheduler/process/create.c +++ b/src/kernel/scheduler/process/create.c @@ -50,7 +50,6 @@ struct process *process_create(const char *name) process->stack.kernel = process->memory.stack.kernel + process->memory.stack.size.kernel; // initialize "exit" part. - // FIXME: add xor r4, r4 uint8_t callexit[8] = { 0b00100100, 0b01001010, // xor r4, r4 0b11000011, __NR_exit, // trapa #__NR_exit @@ -93,12 +92,13 @@ struct process *process_create(const char *name) process->opfile[i].file.cursor = 0; } process->working_dir = vfs_root_node; + process->tty.private = NULL; // DEBUG ! - earlyterm_write("proc_create: success !\n"); - earlyterm_write("* user stack: %p\n", process->context.reg[15]); - earlyterm_write("* kernel stack: %p\n", process->memory.stack.kernel); - DBG_WAIT; + //earlyterm_write("proc_create: success !\n"); + //earlyterm_write("* user stack: %p\n", process->context.reg[15]); + //earlyterm_write("* kernel stack: %p\n", process->memory.stack.kernel); + //DBG_WAIT; // Link new process with his parent. // @Note: diff --git a/src/kernel/scheduler/syscall/sys_fexecve.c b/src/kernel/scheduler/syscall/sys_fexecve.c index 7a0a84f..a2c4b10 100644 --- a/src/kernel/scheduler/syscall/sys_fexecve.c +++ b/src/kernel/scheduler/syscall/sys_fexecve.c @@ -3,12 +3,26 @@ #include #include #include +#include + +static void proc_dump_shared(struct process *child, struct process *parent) +{ + // Dump all opened file + for (int i = 0 ; i < PROCESS_NB_OPEN_FILE ; ++i) + { + memcpy(&child->opfile[i].file, &parent->opfile[i].file, sizeof(FILE)); + child->opfile[i].status = parent->opfile[i].status; + } + + // Dump specific + memcpy(&child->tty, &parent->tty, sizeof(FILE)); +} pid_t sys_fexecve(const char *pathname) { + extern struct process *process_current; struct process *proc; pid_t child_pid; - int error; // Start atomic operation atomic_start(); @@ -17,12 +31,16 @@ pid_t sys_fexecve(const char *pathname) proc = process_create(pathname); if (proc == NULL) { - earlyterm_write("sys_fexecve: alloc error !"); + earlyterm_write("sys_fexecve: process_create error !\n"); DBG_WAIT; atomic_stop(); return (-1); } + // Dump parent process shared informations + if (process_current != NULL) + proc_dump_shared(proc, process_current); + // Try to load binary into physical memory if (loader(proc, pathname) != 0) { @@ -33,13 +51,8 @@ pid_t sys_fexecve(const char *pathname) return (-1); } - // Debug - earlyterm_write("New proc loaded !"); - DBG_WAIT; - // Add new process into task queue - error = sched_add_task(proc); - if (error != 0) + if (sched_add_task(proc)) { earlyterm_write("sys_fexecve: scheduler error !"); DBG_WAIT; diff --git a/src/lib/Makefile b/src/lib/Makefile index 9d602ef..15d928c 100644 --- a/src/lib/Makefile +++ b/src/lib/Makefile @@ -12,8 +12,10 @@ include ../../global.mk # Generic variables # #------- --------# HEADER := -I../../include -BUILD := ../../build/lib/ -TARGET-MODULES := stdio string unistd display +BUILD-STATIC := ../../build/lib/static +BUILD-DYNAMIC := ../../build/lib/dynamic +TARGET-MODULES := libc display +DEBUG-DYNLIB := ../../debug_bin @@ -21,14 +23,20 @@ TARGET-MODULES := stdio string unistd display #------- --------# # Automated variables # #------- --------# -$(foreach mod,$(TARGET-MODULES),$(eval \ - target-$(mod)-src := $$(wildcard $(mod)/*.c) $n\ - target-$(mod)-src += $$(wildcard $(mod)/*.s) $n\ - target-$(mod)-src += $$(wildcard $(mod)/*.S) $n\ - target-$(mod)-obj := $$(subst /,_,$$(basename $$(target-$(mod)-src))) $n\ - target-$(mod)-obj := $$(patsubst %,$(BUILD)%.o,$$(target-$(mod)-obj)) $n\ +$(foreach mod,$(TARGET-MODULES),$(eval \ + target-$(mod)-dir := $(shell find $(mod) -type d) $n\ + target-$(mod)-src := $n\ + $$(foreach path,$$(target-$(mod)-dir),$$(eval $$n\ + target-$(mod)-src += $$$$(wildcard $$(path)/*.c) $$n\ + target-$(mod)-src += $$$$(wildcard $$(path)/*.s) $$n\ + target-$(mod)-src += $$$$(wildcard $$(path)/*.S) $$n\ + )) $n\ + target-$(mod)-obj := $$(subst /,_,$$(basename $$(target-$(mod)-src))) $n\ + target-$(mod)-obj-static := $$(patsubst %,$(BUILD-STATIC)/%.o,$$(target-$(mod)-obj)) $n\ + target-$(mod)-obj-dynamic := $$(patsubst %,$(BUILD-DYNAMIC)/%.o,$$(target-$(mod)-obj)) $n\ )) -TARGET-LIBS := $(patsubst %,lib%.a,$(TARGET-MODULES)) +TARGET-LIBS-STATIC := $(patsubst %,lib%.a,$(TARGET-MODULES)) +TARGET-LIBS-DYNAMIC := $(patsubst %,lib%_dyn.so,$(TARGET-MODULES)) @@ -36,49 +44,56 @@ TARGET-LIBS := $(patsubst %,lib%.a,$(TARGET-MODULES)) #------- --------# # Generic rules # #------- --------# -all: $(TARGET-LIBS) +all: $(TARGET-LIBS-STATIC) $(TARGET-LIBS-DYNAMIC) -$(BUILD): +$(BUILD-STATIC) $(BUILD-DYNAMIC): @ printf "Create $(blue)$@$(nocolor) directory\n" @ mkdir -p $@ -debug: - @ echo 'lib: $(TARGET-LIBS)' - @ echo 'src: $(target-stdio-src)' - @ echo 'obj: $(target-stdio-obj)' - @ echo 'directory: $(TARGET-MODULES)' - @ echo 'debug: $(BUILD)' - - - #------- --------# # Rule functions # #------- --------# -define rule-src -$(patsubst %,$(BUILD)%.o,$(subst /,_,$(basename $1))): $1 | $(BUILD) +define rule-src-static +$(patsubst %,$(BUILD-STATIC)/%.o,$(subst /,_,$(basename $1))): $1 | $(BUILD-STATIC) @ printf "compiling $(white)$$<$(nocolor)..." @ $(CC) $(CFLAGS) -c $$< -o $$@ $(HEADER) @ printf "$(green)[ok]$(nocolor)\n" endef -define rule-link +define rule-link-static $(patsubst %,lib%.a,$1): $2 @ printf "Link $(green)$$@$(nocolor) lib\n" $(AR) crs $$@ $$^ endef +define rule-src-dynamic +$(patsubst %,$(BUILD-DYNAMIC)/%.o,$(subst /,_,$(basename $1))): $1 | $(BUILD-DYNAMIC) + @ printf "compiling $(white)$$<$(nocolor)..." + @ $(CC) -fPIC $(CFLAGS) -c $$< -o $$@ $(HEADER) + @ printf "$(green)[ok]$(nocolor)\n" +endef +define rule-link-dynamic +$(patsubst %,lib%_dyn.so,$1): $2 | $(DEBUG-DYNLIB) + @ printf "Link $(green)$$@$(nocolor) lib\n" + $(CC) -shared -o $$@ $$^ -nostdlib -lgcc +endef #------- --------# # Automated rules # #------- --------# -$(foreach mod,$(TARGET-MODULES), \ - $(foreach source,$(target-$(mod)-src), \ - $(eval $(call rule-src,$(source),$(mod)))) \ - $(eval $(call rule-link,$(mod),$(target-$(mod)-obj))) \ +$(foreach mod,$(TARGET-MODULES), \ + $(foreach source,$(target-$(mod)-src), \ + $(eval $(call rule-src-static,$(source)))) \ + $(eval $(call rule-link-static,$(mod),$(target-$(mod)-obj-static))) \ ) +$(foreach mod,$(TARGET-MODULES), \ + $(foreach source,$(target-$(mod)-src), \ + $(eval $(call rule-src-dynamic,$(source)))) \ + $(eval $(call rule-link-dynamic,$(mod),$(target-$(mod)-obj-dynamic))) \ +) @@ -86,10 +101,12 @@ $(foreach mod,$(TARGET-MODULES), \ # Clean rules # #------- --------# clean: - rm -rf $(BUILD) + rm -rf $(BUILD-STATIC) + rm -rf $(BUILD-DYNAMIC) fclean: clean - rm -f $(TARGET-LIBS) + rm -f $(TARGET-LIBS-STATIC) + rm -f $(TARGET-LIBS-DYNAMIC) re: fclean all diff --git a/src/lib/libc/stdio/printf.c b/src/lib/libc/stdio/printf.c new file mode 100644 index 0000000..563d7c2 --- /dev/null +++ b/src/lib/libc/stdio/printf.c @@ -0,0 +1,13 @@ +#include +#include + +int printf(const char *format, ...) +{ + va_list ap; + int ret; + + va_start(ap, format); + ret = vdprintf(STDOUT_FILENO, format, ap); + va_end(ap); + return (ret); +} diff --git a/src/lib/libc/stdio/putc.c b/src/lib/libc/stdio/putc.c new file mode 100644 index 0000000..60cfb49 --- /dev/null +++ b/src/lib/libc/stdio/putc.c @@ -0,0 +1,11 @@ +#include +#include + +int putc(int c) +{ + char n; + + n = (char)c; + write(STDOUT_FILENO, &n, 1); + return (n); +} diff --git a/src/lib/libc/stdio/puts.c b/src/lib/libc/stdio/puts.c new file mode 100644 index 0000000..7c74d24 --- /dev/null +++ b/src/lib/libc/stdio/puts.c @@ -0,0 +1,13 @@ +#include +#include +#include + +int puts(const char *s) +{ + size_t size; + size_t n; + + size = strlen(s); + n = write(STDOUT_FILENO, s, size); + return (-(n == size)); +} diff --git a/src/lib/libc/stdio/sprintf.c b/src/lib/libc/stdio/sprintf.c new file mode 100644 index 0000000..a2327d7 --- /dev/null +++ b/src/lib/libc/stdio/sprintf.c @@ -0,0 +1,12 @@ +#include + +int sprintf(char *str, char const *format, ...) +{ + va_list ap; + int ret; + + va_start(ap, format); + ret = vsprintf(str, format, ap); + va_end(ap); + return (ret); +} diff --git a/src/lib/libc/stdio/vdprintf.c b/src/lib/libc/stdio/vdprintf.c new file mode 100644 index 0000000..5993f76 --- /dev/null +++ b/src/lib/libc/stdio/vdprintf.c @@ -0,0 +1,8 @@ +#include +#include + +//TODO x_x +int vdprintf(int fd, const char *format, va_list ap) +{ + return (-1); +} diff --git a/src/lib/stdio/vsprintf.c b/src/lib/libc/stdio/vsprintf.c similarity index 99% rename from src/lib/stdio/vsprintf.c rename to src/lib/libc/stdio/vsprintf.c index 8c1608a..380198f 100644 --- a/src/lib/stdio/vsprintf.c +++ b/src/lib/libc/stdio/vsprintf.c @@ -44,6 +44,7 @@ static size_t strndump(char *dest, char const *src, size_t size) return (i); } +//TODO: update me !! int vsprintf(char *str, char const *format, va_list ap) { const char *tmp; diff --git a/src/lib/string/memcpy.c b/src/lib/libc/string/memcpy.c similarity index 100% rename from src/lib/string/memcpy.c rename to src/lib/libc/string/memcpy.c diff --git a/src/lib/string/memset.c b/src/lib/libc/string/memset.c similarity index 100% rename from src/lib/string/memset.c rename to src/lib/libc/string/memset.c diff --git a/src/lib/string/strcat.c b/src/lib/libc/string/strcat.c similarity index 100% rename from src/lib/string/strcat.c rename to src/lib/libc/string/strcat.c diff --git a/src/lib/string/strchr.c b/src/lib/libc/string/strchr.c similarity index 100% rename from src/lib/string/strchr.c rename to src/lib/libc/string/strchr.c diff --git a/src/lib/string/strcmp.c b/src/lib/libc/string/strcmp.c similarity index 100% rename from src/lib/string/strcmp.c rename to src/lib/libc/string/strcmp.c diff --git a/src/lib/string/strcpy.c b/src/lib/libc/string/strcpy.c similarity index 100% rename from src/lib/string/strcpy.c rename to src/lib/libc/string/strcpy.c diff --git a/src/lib/string/strlen.c b/src/lib/libc/string/strlen.c similarity index 100% rename from src/lib/string/strlen.c rename to src/lib/libc/string/strlen.c diff --git a/src/lib/unistd/close.S b/src/lib/libc/unistd/close.S similarity index 100% rename from src/lib/unistd/close.S rename to src/lib/libc/unistd/close.S diff --git a/src/lib/unistd/fexecve.S b/src/lib/libc/unistd/fexecve.S similarity index 100% rename from src/lib/unistd/fexecve.S rename to src/lib/libc/unistd/fexecve.S diff --git a/src/lib/unistd/fork.S b/src/lib/libc/unistd/fork.S similarity index 100% rename from src/lib/unistd/fork.S rename to src/lib/libc/unistd/fork.S diff --git a/src/lib/unistd/lseek.S b/src/lib/libc/unistd/lseek.S similarity index 100% rename from src/lib/unistd/lseek.S rename to src/lib/libc/unistd/lseek.S diff --git a/src/lib/unistd/open.S b/src/lib/libc/unistd/open.S similarity index 100% rename from src/lib/unistd/open.S rename to src/lib/libc/unistd/open.S diff --git a/src/lib/unistd/read.S b/src/lib/libc/unistd/read.S similarity index 100% rename from src/lib/unistd/read.S rename to src/lib/libc/unistd/read.S diff --git a/src/lib/unistd/waitpid.S b/src/lib/libc/unistd/waitpid.S similarity index 100% rename from src/lib/unistd/waitpid.S rename to src/lib/libc/unistd/waitpid.S diff --git a/src/lib/unistd/write.S b/src/lib/libc/unistd/write.S similarity index 100% rename from src/lib/unistd/write.S rename to src/lib/libc/unistd/write.S diff --git a/src/lib/stdio/sprintf.c b/src/lib/stdio/sprintf.c deleted file mode 100644 index a9bcdd0..0000000 --- a/src/lib/stdio/sprintf.c +++ /dev/null @@ -1,10 +0,0 @@ -#include - -void sprintf(char *str, char const *format, ...) -{ - va_list ap; - - va_start(ap, format); - vsprintf(str, format, ap); - va_end(ap); -} diff --git a/src/lib/user_shared_lib.ld b/src/lib/user_shared_lib.ld new file mode 100644 index 0000000..2951d7a --- /dev/null +++ b/src/lib/user_shared_lib.ld @@ -0,0 +1,86 @@ +OUTPUT_FORMAT("elf32-sh", "elf32-sh", "elf32-sh") +OUTPUT_ARCH(sh3) + +/* +** Linker script for user executables. +*/ +MEMORY +{ + /* virtual memory, read-write segment */ + userram (WX) : o = 0x00000000, l = 256k +} + +SECTIONS +{ + /* Code */ + .text : { + *(.text); + *(.text.*); + + } > userram + + /* Read-only sections */ + .rodata : { + /* Read-Only data */ + *(.rodata); + *(.rodata.*); + + /* Dynamic symbols */ + *(.hash) + *(.dynsym) + *(.dynstr) + *(.dynbss) + *(.dynamic) + + /* Procedure Linkage Table */ + *(.plt) + + /* GLobal Offset Table */ + *(.got.plt) + *(.got.*) + *(.got) + + /* ???? */ + *(.rofixup) + } > userram + + /* Relocatable sections */ + .rela.dyn : { + *(.rela.plt) + *(.rela.got) + *(.rela.got.*) + *(.rela.*) + *(.rela.text) + *(.real.data) + } > userram + + /* readable / writable data */ + .data ALIGN(4) : { + + /* Data sections */ + *(.data); + *(.data.*); + + /* bss section included to avoid missaligned segment */ + *(.bss); + *(.bss.*); + *(COMMON); + } > userram + + /* unwanted section */ + /DISCARD/ : { + *(.gnu.*) + *(.debug_info) + *(.debug_abbrev) + *(.debug_loc) + *(.debug_aranges) + *(.debug_ranges) + *(.debug_line) + *(.debug_str) + *(.jcr) + *(.eh_frame_hdr) + *(.eh_frame) + *(.comment) + *(.interp) + } +} diff --git a/src/user/shell/builtin/proc.c b/src/user/shell/builtin/proc.c index 0b8f5ba..c6a5ff7 100644 --- a/src/user/shell/builtin/proc.c +++ b/src/user/shell/builtin/proc.c @@ -1,43 +1,24 @@ #include #include +#include #include int builtin_proc(void) { -// pid_t child; + pid_t child; + int wstatus; - // Debug -// dclear(); -// dprint(0, 0, "Proc builtin !"); -// dupdate(); - for (int i = 0 ; i < 3000000 ; i++); + puts("proc test entry :)\n"); // Try to create first child - /*child = fork(); + child = fexecve("/mnt/casio/VHEX/test.elf"); if (child == -1) { - dprint(0, 1, "fork() fail !"); - dupdate(); - for (int i = 0 ; i < 3000000 ; i++); + puts("fexecve fail :(\n"); return (0); } - // Check parent - if (child_pid != 0) - { - int counter = 0; - while (1) - { - dprint(0, 1, "parent: %d", counter++); - for (int i = 0 ; i < 3000000 ; i++); - } - } else { - int counter = 0; - while (1) - { - dprint(0, 2, "child: %d", counter++); - for (int i = 0 ; i < 9000000 ; i++); - } - }*/ + // Wait child death + waitpid(child, &wstatus, WUNTRACED); return (0); } diff --git a/src/user/shell/main.c b/src/user/shell/main.c index 806aed4..694d82c 100644 --- a/src/user/shell/main.c +++ b/src/user/shell/main.c @@ -9,15 +9,15 @@ int main(void) int cmd_size; //char **argv; //int argc; - int fd; + //int STDOUT_FILENO; // Try to open TTY // @note: // We use O_DIRECT to avoid cache // generation because we do not have a // lot of memory. - fd = open("/dev/tty", O_DIRECT); - if (fd < 0) + /*STDOUT_FILENO = open("/dev/tty", O_DIRECT); + if (STDOUT_FILENO < 0) { // Display error. display_t disp; @@ -35,15 +35,15 @@ int main(void) // TODO: use sleep syscall ! __asm__ volatile ("sleep"); } - } + }*/ // Shell main loop. - write(fd, "Boot Complete !\n", 16); + write(STDOUT_FILENO, "Boot Complete !\n", 16); while (1) { // Get user command. - write(fd, ">", 1); - cmd_size = read(fd, input, 12); + write(STDOUT_FILENO, ">", 1); + cmd_size = read(STDIN_FILENO, input, 12); // Remove '\n' char. // FIXME: create argc, argv !! @@ -52,11 +52,11 @@ int main(void) // Check buit-in. if (check_builtin(input) != 0) { - write(fd, input, cmd_size - 1); - write(fd, ": command not found\n", 20); + write(STDOUT_FILENO, input, cmd_size - 1); + write(STDOUT_FILENO, ": command not found\n", 20); } else { - write(fd, input, cmd_size - 1); - write(fd, ": command found :D !\n", 21); + write(STDOUT_FILENO, input, cmd_size - 1); + write(STDOUT_FILENO, ": command found :D !\n", 21); } } return (0); diff --git a/src/user/shell/util/check_builtin.c b/src/user/shell/util/check_builtin.c index 253a216..4b68d12 100644 --- a/src/user/shell/util/check_builtin.c +++ b/src/user/shell/util/check_builtin.c @@ -10,7 +10,7 @@ struct builtin_s builtin[2] = { { .name = "proc", - .entry = NULL + .entry = (void*)&builtin_proc }, { .name = "ram", diff --git a/src/user/test/Makefile b/src/user/test/Makefile index f5a83ba..d8d18cb 100644 --- a/src/user/test/Makefile +++ b/src/user/test/Makefile @@ -42,12 +42,10 @@ OBJ := $(patsubst ._%,$(BUILD)/%.o,$(subst /,_,$(subst src/,,$(basename $(SRC))) ##--- ## General rules ##--- -all: | $(BUILD) $(DEBUG) $(EXEC) +all: $(EXEC) -$(EXEC): $(OBJ) - $(CC) -fPIC -Wl,-M $(LDFLAG) $(CFLAGS) -o $(DEBUG)/$(NAME).elf.big $(OBJ) $(HEADER) $(LIBS) > $(MEMORY_MAP) - $(OBJCOPY) -S $(DEBUG)/$(NAME).elf.big $@ - rm -f $(DEBUG)/$(NAME).elf.big +$(EXEC): $(OBJ) | $(DEBUG) + $(CC) -pie -Wl,-M $(LDFLAG) $(CFLAGS) -o $@ $(OBJ) $(HEADER) $(LIBS) > $(MEMORY_MAP) $(BUILD) $(DEBUG): @ printf "Create $(blue)$@$(nocolor) directory\n" @@ -75,9 +73,9 @@ sec: ## Automated rules ##--- define rule-src -$(patsubst ._%,$(BUILD)/%.o,$(subst /,_,$(subst src/,,$(basename $1)))): $1 +$(patsubst ._%,$(BUILD)/%.o,$(subst /,_,$(subst src/,,$(basename $1)))): $1 | $(BUILD) @ printf "compiling $(white)$$<$(nocolor)..." - @ $(CC) -fPIC $(CFLAGS) -o $$@ -c $$< $(HEADER) -lgcc + @ $(CC) -pie $(CFLAGS) -o $$@ -c $$< $(HEADER) -lgcc @ printf "$(green)[ok]$(nocolor)\n" endef diff --git a/src/user/test/test.ld b/src/user/test/test.ld index 7729fdd..33f3cb3 100644 --- a/src/user/test/test.ld +++ b/src/user/test/test.ld @@ -1,5 +1,5 @@ +OUTPUT_FORMAT("elf32-sh", "elf32-sh", "elf32-sh") OUTPUT_ARCH(sh3) -OUTPUT_FORMAT(elf32-sh) ENTRY(_main) /* @@ -11,26 +11,51 @@ MEMORY userram (WX) : o = 0x00000000, l = 256k } -PHDRS -{ - text PT_LOAD ; - data PT_LOAD ; -} - SECTIONS { + /* Read-only sections */ .text : { + /* Code */ *(.text); *(.text.*); - } : text - .rodata : { + /* Read-Only data */ *(.rodata); *(.rodata.*); - } + /* Dynamic symbols */ + *(.hash) + *(.dynsym) + *(.dynstr) + *(.dynbss) + *(.dynamic) + + /* Procedure Linkage Table */ + *(.plt) + + /* GLobal Offset Table */ + *(.got.plt) + *(.got.*) + *(.got) + + /* ???? */ + *(.rofixup) + } > userram + + /* Relocatable sections */ + .rela.dyn : { + *(.rela.plt) + *(.rela.got) + *(.rela.got.*) + *(.rela.*) + *(.rela.text) + *(.real.data) + } > userram + + /* readable / writable data */ .data ALIGN(4) : { - *(.plt); + + /* Data sections */ *(.data); *(.data.*); @@ -38,18 +63,11 @@ SECTIONS *(.bss); *(.bss.*); *(COMMON); - } : data - - - .relocgot : { - BRELOC_GOT = . ; - *(.got.plt) - *(.got) - ERELOC_GOT = . ; - } + } > userram /* unwanted section */ /DISCARD/ : { + *(.gnu.*) *(.debug_info) *(.debug_abbrev) *(.debug_loc) @@ -61,5 +79,6 @@ SECTIONS *(.eh_frame_hdr) *(.eh_frame) *(.comment) + *(.interp) } }