From 8184057d57435d38c5943752af868ce2384439b8 Mon Sep 17 00:00:00 2001 From: Zhiyuan Shao Date: Mon, 8 May 2023 15:43:57 +0800 Subject: [PATCH] init commit of lab4_challenge2 --- Makefile | 27 ++++++++++--- user/app_exec.c | 12 ++++++ user/app_hardlink.c | 94 --------------------------------------------- user/app_ls.c | 31 +++++++++++++++ user/user_lib.h | 1 - 5 files changed, 64 insertions(+), 101 deletions(-) create mode 100644 user/app_exec.c delete mode 100644 user/app_hardlink.c create mode 100644 user/app_ls.c diff --git a/Makefile b/Makefile index 980e035..fde82f7 100644 --- a/Makefile +++ b/Makefile @@ -14,6 +14,7 @@ SRC_DIR := . OBJ_DIR := obj SPROJS_INCLUDE := -I. +HOSTFS_ROOT := hostfs_root ifneq (,) march := -march= is_32bit := $(findstring 32,$(march)) @@ -63,12 +64,17 @@ SPIKE_INF_LIB := $(OBJ_DIR)/spike_interface.a #--------------------- user ----------------------- -USER_CPPS := user/*.c +USER_CPPS := user/app_exec.c user/user_lib.c -USER_CPPS := $(wildcard $(USER_CPPS)) USER_OBJS := $(addprefix $(OBJ_DIR)/, $(patsubst %.c,%.o,$(USER_CPPS))) -USER_TARGET := $(OBJ_DIR)/app_hardlink +USER_TARGET := $(HOSTFS_ROOT)/bin/app_exec + +USER_E_CPPS := user/app_ls.c user/user_lib.c + +USER_E_OBJS := $(addprefix $(OBJ_DIR)/, $(patsubst %.c,%.o,$(USER_E_CPPS))) + +USER_E_TARGET := $(HOSTFS_ROOT)/bin/app_ls #------------------------targets------------------------ $(OBJ_DIR): @-mkdir -p $(OBJ_DIR) @@ -76,6 +82,7 @@ $(OBJ_DIR): @-mkdir -p $(dir $(SPIKE_INF_OBJS)) @-mkdir -p $(dir $(KERNEL_OBJS)) @-mkdir -p $(dir $(USER_OBJS)) + @-mkdir -p $(dir $(USER_E_OBJS)) $(OBJ_DIR)/%.o : %.c @echo "compiling" $< @@ -102,20 +109,28 @@ $(KERNEL_TARGET): $(OBJ_DIR) $(UTIL_LIB) $(SPIKE_INF_LIB) $(KERNEL_OBJS) $(KERNE $(USER_TARGET): $(OBJ_DIR) $(UTIL_LIB) $(USER_OBJS) @echo "linking" $@ ... + -@mkdir -p $(HOSTFS_ROOT)/bin @$(COMPILE) --entry=main $(USER_OBJS) $(UTIL_LIB) -o $@ @echo "User app has been built into" \"$@\" + @cp $@ $(OBJ_DIR) + +$(USER_E_TARGET): $(OBJ_DIR) $(UTIL_LIB) $(USER_E_OBJS) + @echo "linking" $@ ... + -@mkdir -p $(HOSTFS_ROOT)/bin + @$(COMPILE) --entry=main $(USER_E_OBJS) $(UTIL_LIB) -o $@ + @echo "User app has been built into" \"$@\" -include $(wildcard $(OBJ_DIR)/*/*.d) -include $(wildcard $(OBJ_DIR)/*/*/*.d) .DEFAULT_GOAL := $(all) -all: $(KERNEL_TARGET) $(USER_TARGET) +all: $(KERNEL_TARGET) $(USER_TARGET) $(USER_E_TARGET) .PHONY:all run: $(KERNEL_TARGET) $(USER_TARGET) @echo "********************HUST PKE********************" - spike $(KERNEL_TARGET) $(USER_TARGET) + spike $(KERNEL_TARGET) /bin/app_exec # need openocd! gdb:$(KERNEL_TARGET) $(USER_TARGET) @@ -146,4 +161,4 @@ format: @python ./format.py ./ clean: - rm -fr ${OBJ_DIR} + rm -fr ${OBJ_DIR} ${HOSTFS_ROOT}/bin diff --git a/user/app_exec.c b/user/app_exec.c new file mode 100644 index 0000000..de6282e --- /dev/null +++ b/user/app_exec.c @@ -0,0 +1,12 @@ +#include "user_lib.h" +#include "util/types.h" + +int main(int argc, char *argv[]) { + printu("\n======== exec /bin/app_ls in app_exec ========\n"); + int ret = exec("/bin/app_ls"); + if (ret == -1) + printu("exec failed!\n"); + + exit(0); + return 0; +} diff --git a/user/app_hardlink.c b/user/app_hardlink.c deleted file mode 100644 index 7e209e5..0000000 --- a/user/app_hardlink.c +++ /dev/null @@ -1,94 +0,0 @@ -#include "user_lib.h" -#include "util/string.h" -#include "util/types.h" - -void ls(char *path) { - int dir_fd = opendir_u(path); - printu("------------------------------\n"); - printu("ls \"%s\":\n", path); - printu("[name] [inode_num]\n"); - struct dir dir; - int width = 20; - while(readdir_u(dir_fd, &dir) == 0) { - // we do not have %ms :( - char name[width + 1]; - memset(name, ' ', width + 1); - name[width] = '\0'; - if (strlen(dir.name) < width) { - strcpy(name, dir.name); - name[strlen(dir.name)] = ' '; - printu("%s %d\n", name, dir.inum); - } - else - printu("%s %d\n", dir.name, dir.inum); - } - printu("------------------------------\n"); - closedir_u(dir_fd); -} - -int main(int argc, char *argv[]) { - int MAXBUF = 512; - char str[] = "hello world"; - char buf[MAXBUF]; - int fd1, fd2; - - printu("\n======== establish the file ========\n"); - - fd1 = open("/RAMDISK0/ramfile", O_RDWR | O_CREAT); - printu("create file: /RAMDISK0/ramfile\n"); - close(fd1); - - printu("\n======== Test 1: hard link ========\n"); - - link_u("/RAMDISK0/ramfile", "/RAMDISK0/ramfile2"); - printu("create hard link: /RAMDISK0/ramfile2 -> /RAMDISK0/ramfile\n"); - - fd1 = open("/RAMDISK0/ramfile", O_RDWR); - fd2 = open("/RAMDISK0/ramfile2", O_RDWR); - - printu("file descriptor fd1 (ramfile): %d\n", fd1); - printu("file descriptor fd2 (ramfile2): %d\n", fd2); - - // check the number of hard links to ramfile on disk - struct istat st; - disk_stat_u(fd1, &st); - printu("ramfile hard links: %d\n", st.st_nlinks); - if (st.st_nlinks != 2) { - printu("ERROR: the number of hard links to ramfile should be 2, but it is %d\n", - st.st_nlinks); - exit(-1); - } - - write_u(fd1, str, strlen(str)); - printu("/RAMDISK0/ramfile write content: \n%s\n", str); - - read_u(fd2, buf, MAXBUF); - printu("/RAMDISK0/ramfile2 read content: \n%s\n", buf); - - close(fd1); - close(fd2); - - printu("\n======== Test 2: unlink ========\n"); - - ls("/RAMDISK0"); - - unlink_u("/RAMDISK0/ramfile"); - printu("unlink: /RAMDISK0/ramfile\n"); - - ls("/RAMDISK0"); - - // check the number of hard links to ramfile2 on disk - fd2 = open("/RAMDISK0/ramfile2", O_RDWR); - disk_stat_u(fd2, &st); - printu("ramfile2 hard links: %d\n", st.st_nlinks); - if (st.st_nlinks != 1) { - printu("ERROR: the number of hard links to ramfile should be 1, but it is %d\n", - st.st_nlinks); - exit(-1); - } - close(fd2); - - printu("\nAll tests passed!\n\n"); - exit(0); - return 0; -} diff --git a/user/app_ls.c b/user/app_ls.c new file mode 100644 index 0000000..dc74462 --- /dev/null +++ b/user/app_ls.c @@ -0,0 +1,31 @@ +#include "user_lib.h" +#include "util/string.h" +#include "util/types.h" + +int main(int argc, char *argv[]) { + char path[] = "/RAMDISK0"; + int dir_fd = opendir_u(path); + printu("------------------------------\n"); + printu("ls \"%s\":\n", path); + printu("[name] [inode_num]\n"); + struct dir dir; + int width = 20; + while(readdir_u(dir_fd, &dir) == 0) { + // we do not have %ms :( + char name[width + 1]; + memset(name, ' ', width + 1); + name[width] = '\0'; + if (strlen(dir.name) < width) { + strcpy(name, dir.name); + name[strlen(dir.name)] = ' '; + printu("%s %d\n", name, dir.inum); + } + else + printu("%s %d\n", dir.name, dir.inum); + } + printu("------------------------------\n"); + closedir_u(dir_fd); + + exit(0); + return 0; +} \ No newline at end of file diff --git a/user/user_lib.h b/user/user_lib.h index 95a686d..f8df3bd 100644 --- a/user/user_lib.h +++ b/user/user_lib.h @@ -33,5 +33,4 @@ int closedir_u(int fd); int link_u(const char *fn1, const char *fn2); int unlink_u(const char *fn); - #endif