🎨 将shell独立成单独的app

This commit is contained in:
fslongjin 2022-05-24 14:36:24 +08:00
parent 1eb9a299b6
commit afeca18206
8 changed files with 41 additions and 18 deletions

View File

@ -17,6 +17,7 @@ endif
all: all:
mkdir -p bin/kernel/ mkdir -p bin/kernel/
mkdir -p bin/user/ mkdir -p bin/user/
mkdir -p bin/tmp/
@list='$(SUBDIRS)'; for subdir in $$list; do \ @list='$(SUBDIRS)'; for subdir in $$list; do \
echo "make all in $$subdir";\ echo "make all in $$subdir";\
cd $$subdir;\ cd $$subdir;\

View File

@ -573,13 +573,13 @@ ul initial_kernel_thread(ul arg)
// kdebug("current_pcb->thread->rsp=%#018lx", current_pcb->thread->rsp); // kdebug("current_pcb->thread->rsp=%#018lx", current_pcb->thread->rsp);
current_pcb->flags = 0; current_pcb->flags = 0;
// 将返回用户层的代码压入堆栈向rdx传入regs的地址然后jmp到do_execve这个系统调用api的处理函数 这里的设计思路和switch_proc类似 // 将返回用户层的代码压入堆栈向rdx传入regs的地址然后jmp到do_execve这个系统调用api的处理函数 这里的设计思路和switch_proc类似
// 加载用户态程序:init.bin // 加载用户态程序:shell.elf
char init_path[] = "/init.bin"; char init_path[] = "/shell.elf";
uint64_t addr = (uint64_t)&init_path; uint64_t addr = (uint64_t)&init_path;
__asm__ __volatile__("movq %1, %%rsp \n\t" __asm__ __volatile__("movq %1, %%rsp \n\t"
"pushq %2 \n\t" "pushq %2 \n\t"
"jmp do_execve \n\t" ::"D"(current_pcb->thread->rsp), "jmp do_execve \n\t" ::"D"(current_pcb->thread->rsp),
"m"(current_pcb->thread->rsp), "m"(current_pcb->thread->rip), "S"("/init.bin") "m"(current_pcb->thread->rsp), "m"(current_pcb->thread->rip), "S"("/shell.elf")
: "memory"); : "memory");
return 1; return 1;

4
run.sh
View File

@ -88,10 +88,10 @@ else
flag_can_run=1 flag_can_run=1
fi fi
# 拷贝init文件到硬盘 # 拷贝shell到硬盘
cd tools cd tools
bash m* bash m*
sudo cp ${root_folder}/bin/user/init.bin ${root_folder}/bin/disk_mount sudo cp ${root_folder}/bin/user/shell.elf ${root_folder}/bin/disk_mount
sync sync
bash u* bash u*
cd .. cd ..

View File

@ -1,4 +1,4 @@
user_sub_dirs = libs user_sub_dirs = libs apps
SUBDIR_ROOTS := . SUBDIR_ROOTS := .
DIRS := . $(shell find $(SUBDIR_ROOTS) -type d) DIRS := . $(shell find $(SUBDIR_ROOTS) -type d)
@ -6,9 +6,11 @@ GARBAGE_PATTERNS := *.o *.s~ *.s *.S~ *.c~ *.h~ sys_api_lib
GARBAGE := $(foreach DIR,$(DIRS),$(addprefix $(DIR)/,$(GARBAGE_PATTERNS))) GARBAGE := $(foreach DIR,$(DIRS),$(addprefix $(DIR)/,$(GARBAGE_PATTERNS)))
objs := tmp_output_dir=$(ROOT_PATH)/bin/tmp/user
CFLAGS := $(GLOBAL_CFLAGS) output_dir=$(ROOT_PATH)/bin/user
current_CFLAGS := $(CFLAGS) -I $(shell pwd)/libs
CFLAGS := $(GLOBAL_CFLAGS) -I $(shell pwd)/libs
current_CFLAGS := $(CFLAGS)
all: all:
@list='$(user_sub_dirs)'; for subdir in $$list; do \ @list='$(user_sub_dirs)'; for subdir in $$list; do \
@ -17,18 +19,25 @@ all:
$(MAKE) all CFLAGS="$(CFLAGS)";\ $(MAKE) all CFLAGS="$(CFLAGS)";\
cd ..;\ cd ..;\
done done
$(MAKE) init.o
$(MAKE) sys_api_lib
$(shell if [ ! -e $(tmp_output_dir) ];then mkdir -p $(tmp_output_dir); fi)
$(shell if [ ! -e $(output_dir) ];then mkdir -p $(output_dir); fi)
$(MAKE) sys_api_lib
$(MAKE) shell
# objcopy -I elf64-x86-64 -S -R ".eh_frame" -R ".comment" -O binary sys_api_lib $(ROOT_PATH)/bin/user/init.bin # objcopy -I elf64-x86-64 -S -R ".eh_frame" -R ".comment" -O binary sys_api_lib $(ROOT_PATH)/bin/user/init.bin
objcopy -I elf64-x86-64 -S -R ".eh_frame" -R ".comment" -O elf64-x86-64 sys_api_lib $(ROOT_PATH)/bin/user/init.bin #objcopy -I elf64-x86-64 -S -R ".eh_frame" -R ".comment" -O elf64-x86-64 sys_api_lib $(ROOT_PATH)/bin/user/init.bin
sys_api_lib: init.o sys_api_lib:
ld -b elf64-x86-64 -z muldefs -o sys_api_lib $(shell find . -name "*.o") ld -b elf64-x86-64 -z muldefs -o $(tmp_output_dir)/sys_api_lib $(shell find ./libs -name "*.o")
#ld -b elf64-x86-64 -z muldefs -o sys_api_lib init.o $(shell find . -name "*.o") -T init.lds #ld -b elf64-x86-64 -z muldefs -o sys_api_lib init.o $(shell find . -name "*.o") -T init.lds
init.o: init.c shell:
gcc $(current_CFLAGS) -c init.c -o init.o ld -b elf64-x86-64 -z muldefs -o $(tmp_output_dir)/shell $(shell find ./apps/shell -name "*.o") $(shell find ./libs -name "*.o")
objcopy -I elf64-x86-64 -S -R ".eh_frame" -R ".comment" -O elf64-x86-64 $(tmp_output_dir)/shell $(output_dir)/shell.elf
clean: clean:
rm -rf $(GARBAGE) rm -rf $(GARBAGE)

10
user/apps/Makefile Normal file
View File

@ -0,0 +1,10 @@
user_apps_sub_dirs=shell
all:
@list='$(user_apps_sub_dirs)'; for subdir in $$list; do \
echo "make all in $$subdir";\
cd $$subdir;\
$(MAKE) all CFLAGS="$(CFLAGS) -I $(shell pwd)";\
cd ..;\
done

4
user/apps/shell/Makefile Normal file
View File

@ -0,0 +1,4 @@
all: shell.o
shell.o: shell.c
gcc $(CFLAGS) -c shell.c -o shell.o

View File

@ -5,7 +5,6 @@
#include <libKeyboard/keyboard.h> #include <libKeyboard/keyboard.h>
int main() int main()
{ {
char string[] = "/333.txt"; char string[] = "/333.txt";
uint8_t buf[128] = {0}; uint8_t buf[128] = {0};
char tips_str[] = "The first application 'init.bin' started successfully!\n"; char tips_str[] = "The first application 'init.bin' started successfully!\n";