init commit of lab4_challenge2

lab4_challenge2_exec
Zhiyuan Shao 2 years ago
parent 38183534a3
commit 8184057d57

@ -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

@ -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;
}

@ -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;
}

@ -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;
}

@ -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

Loading…
Cancel
Save