From c372bc177a366808bac29120f4abd4b2c69baf24 Mon Sep 17 00:00:00 2001 From: xiaoyi1212 Date: Sat, 7 Sep 2024 23:26:22 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=94=A8=E6=88=B7=E8=BF=9B?= =?UTF-8?q?=E7=A8=8B=E5=88=9B=E5=BB=BA=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/init/init.c | 6 +--- apps/libs/syscall.c | 1 + apps/shell/neofetch.c | 1 + src/driver/pci.c | 1 - src/fs/iso9660.c | 1 + src/fs/vfs.c | 68 +++++++++++++++++++++++++++++++++++++------ src/include/vfs.h | 1 + src/kernel/kernel.c | 6 ++-- src/kernel/page.c | 4 +-- src/kernel/syscall.c | 3 +- src/kernel/task.c | 25 ++++++++++------ 11 files changed, 86 insertions(+), 31 deletions(-) diff --git a/apps/init/init.c b/apps/init/init.c index ce941a3..c643d1b 100644 --- a/apps/init/init.c +++ b/apps/init/init.c @@ -3,10 +3,6 @@ int main(){ printf("Init service launched.\n"); - syscall_vfs_change_path("apps"); - int pid = exec_elf("shell.bin"); - if(pid != -1){ - printf("Shell launch win! PID:[%d]\n",pid); - } else printf("Error: Cannot launch shell\n"); + exec_elf("shell.bin"); return 0; } diff --git a/apps/libs/syscall.c b/apps/libs/syscall.c index 37d3941..73a2ed9 100644 --- a/apps/libs/syscall.c +++ b/apps/libs/syscall.c @@ -1,4 +1,5 @@ #include "../include/syscall.h" +#include "../include/stdio.h" void syscall_print(char* c){ uint32_t rets; diff --git a/apps/shell/neofetch.c b/apps/shell/neofetch.c index 73f73bb..c205b1c 100644 --- a/apps/shell/neofetch.c +++ b/apps/shell/neofetch.c @@ -32,6 +32,7 @@ void print_info(){ printf(" :*@@@@@@@@@@@@@@@@@@@@@@@@@@@&+: \n"); printf(" :+#@@@@@@@@@@@@@@@@@@@@#+: \n"); printf(" :=+*#&@@@@@@&#*+-: \n"); + free_info(info); } \ No newline at end of file diff --git a/src/driver/pci.c b/src/driver/pci.c index 211e8a1..819441c 100644 --- a/src/driver/pci.c +++ b/src/driver/pci.c @@ -105,7 +105,6 @@ void pci_config(unsigned int bus, unsigned int f, unsigned int equipment, unsign } void init_pci(){ - PCI_ADDR_BASE = kmalloc(1 * 1024 * 1024); unsigned int i, BUS, Equipment, F, ADDER, *i1; unsigned char *PCI_DATA = PCI_ADDR_BASE, *PCI_DATA1; diff --git a/src/fs/iso9660.c b/src/fs/iso9660.c index b3b2067..3ef5af5 100644 --- a/src/fs/iso9660.c +++ b/src/fs/iso9660.c @@ -389,6 +389,7 @@ bool ISO_ReadFile(struct vfs_t *vfs, char *path, char *buffer) { } return false; // not found } + for (;;) { size_t read; l9660_read(&file, buffer, 128, &read); diff --git a/src/fs/vfs.c b/src/fs/vfs.c index b2cdd1a..8858bbe 100644 --- a/src/fs/vfs.c +++ b/src/fs/vfs.c @@ -117,10 +117,12 @@ bool vfs_readfile(char *path, char *buffer) { char *new_path = kmalloc(strlen(path) + 1); strcpy(new_path, path); vfs_t *vfs = ParsePath(new_path); + if (vfs == NULL) { kfree(new_path); return false; } + int result = vfs->ReadFile(vfs, new_path, buffer); kfree(new_path); return result; @@ -321,9 +323,6 @@ vfs_file *get_cur_file(char* filename){ } DeleteList(ls); kfree(ls); - - - return file; } @@ -389,6 +388,25 @@ void vfs_getPath_no_drive(char *buffer) { } } +void vfs_getPath_no_drive_src(vfs_t *src,char *buffer) { + char *path; + List *l; + buffer[0] = 0; + int pos = strlen(buffer); + int i; + for (i = 1; FindForCount(i, src->path) != NULL; i++) { + l = FindForCount(i, src->path); + path = (char *)l->val; + insert_char(buffer, pos, '/'); + pos++; + insert_str(buffer, path, pos); + pos += strlen(path); + } + if (i == 1) { + insert_char(buffer, 0, '/'); + } +} + void init_vfs() { for (int i = 0; i < 26; i++) { vfsstl[i].flag = 0; @@ -419,11 +437,43 @@ bool vfs_register_fs(vfs_t vfs) { void vfs_copy(struct task_struct *task,vfs_t* src){ vfs_change_disk(task, src->drive); - List *l; - char *path; - for (int i = 1; FindForCount(i, task->vfs_now->path) != NULL; i++) { - l = FindForCount(i, task->vfs_now->path); - path = (char *)l->val; - task->vfs_now->cd(task->vfs_now, path); + char* buffer[255]; + + vfs_getPath_no_drive_src(src,buffer); + + char *buf = kmalloc(strlen(buffer) + 1); + char *r = buf; + memcpy(buf, buffer, strlen(buffer) + 1); + int i = 0; + + if (buf[i] == '/' || buf[i] == '\\') { + if (!task->vfs_now->cd(task->vfs_now, "/")) { + kfree(r); + return; + } + i++; + buf++; } + + for (;; i++) { + if (buf[i] == '/' || buf[i] == '\\') { + buf[i] = 0; + if (!task->vfs_now->cd(task->vfs_now, buf)) { + kfree(r); + return; + } + buf += strlen(buf) + 1; + } + if (buf[i] == '\0') { + int bol = task->vfs_now->cd(task->vfs_now, buf); + + if (!(bol)) { + kfree(r); + return; + } else{ + break; + } + } + } + kfree(r); } \ No newline at end of file diff --git a/src/include/vfs.h b/src/include/vfs.h index 77b21a1..3f57b97 100644 --- a/src/include/vfs.h +++ b/src/include/vfs.h @@ -75,5 +75,6 @@ bool vfs_register_fs(vfs_t vfs); void init_vfs(); vfs_file *get_cur_file(char* filename); void vfs_copy(struct task_struct *task,vfs_t* src); +void vfs_getPath_no_drive_src(vfs_t *src,char *buffer); #endif diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index d67e9d5..953b6e3 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -159,10 +159,10 @@ void kernel_main(multiboot_t *multiboot) { clock_sleep(25); vfs_change_path("apps"); - //klogf(user_process("init.bin","InitService") != -1,"Init service process init.\n"); + klogf(user_process("init.bin","InitService") != -1,"Init service process init.\n"); - int pid = user_process("shell.bin","UserShell"); - kernel_thread(check_task_usershell,&pid,"CTU"); + //int pid = user_process("shell.bin","UserShell"); + //kernel_thread(check_task_usershell,&pid,"CTU"); // int pid = kernel_thread(setup_shell,NULL,"CPOS-Shell"); // klogf(pid != -1,"Launch kernel shell.\n"); diff --git a/src/kernel/page.c b/src/kernel/page.c index 2aa97e9..5ed043b 100644 --- a/src/kernel/page.c +++ b/src/kernel/page.c @@ -166,6 +166,7 @@ void page_fault(registers_t *regs) { } sleep(1); + while (1) io_hlt(); io_sti(); } @@ -252,9 +253,6 @@ void init_page(multiboot_t *mboot) { j += 0x1000; } - - - register_interrupt_handler(14, page_fault); switch_page_directory(kernel_directory); diff --git a/src/kernel/syscall.c b/src/kernel/syscall.c index 201ca9a..cf9c629 100644 --- a/src/kernel/syscall.c +++ b/src/kernel/syscall.c @@ -120,7 +120,8 @@ static void* syscall_sysinfo(uint32_t ebx,uint32_t ecx,uint32_t edx,uint32_t esi } static int syscall_exec(uint32_t ebx,uint32_t ecx,uint32_t edx,uint32_t esi,uint32_t edi){ - return user_process(ebx,ebx); + int pid = user_process(ebx,ebx); + return pid; } void *sycall_handlers[MAX_SYSCALLS] = { diff --git a/src/kernel/task.c b/src/kernel/task.c index 8a92908..fe4d926 100644 --- a/src/kernel/task.c +++ b/src/kernel/task.c @@ -137,10 +137,12 @@ void task_kill(int pid) { struct task_struct *argv = found_task_pid(pid); if (argv == NULL) { printf("Cannot found task Pid:[%d].\n", pid); + io_sti(); return; } if (argv->pid == 0) { printf("\033ff3030;[kernel]: Taskkill cannot terminate kernel processes.\033c6c6c6;\n"); + io_sti(); return; } argv->state = TASK_DEATH; @@ -191,14 +193,16 @@ void change_task_to(registers_t *reg,struct task_struct *next) { int32_t user_process(char *path, char *name){ // 用户进程创建 can_sche = 0; if(path == NULL){ - return -1; + return NULL; } io_sti(); uint32_t size = vfs_filesize(path); + if(size == -1){ - return -1; + return NULL; } + io_cli(); struct task_struct *new_task = (struct task_struct *) kmalloc(STACK_SIZE); @@ -221,13 +225,17 @@ int32_t user_process(char *path, char *name){ // 用户进程创建 new_task->vfs_now = NULL; new_task->tty = kmalloc(sizeof(tty_t)); init_default_tty(new_task); + io_sti(); vfs_copy(new_task,get_current()->vfs_now); - io_sti(); + char* ker_path = kmalloc(strlen(path) + 1); + strcpy(ker_path,path); + page_switch(page); + for (int i = USER_START; i < USER_END + 0x1000;i++) { //用户堆以及用户栈映射 page_t *pg = get_page(i,1,page, false); alloc_frame(pg,0,1); @@ -241,12 +249,14 @@ int32_t user_process(char *path, char *name){ // 用户进程创建 char* buffer = USER_EXEC_FILE_START; memset(buffer,0,size); - vfs_readfile(path,buffer); + int r = vfs_readfile(ker_path,buffer); Elf32_Ehdr *ehdr = buffer; + if(!elf32Validate(ehdr)){ printf("Unknown exec file format.\n"); - return -1; + kfree(ker_path); + return NULL; } uint32_t main = ehdr->e_entry; load_elf(ehdr,page); @@ -264,6 +274,7 @@ int32_t user_process(char *path, char *name){ // 用户进程创建 // 设置新任务的标志寄存器未屏蔽中断,很重要 new_task->context.eflags = (0 << 12 | 0b10 | 1 << 9); new_task->next = running_proc_head; + kfree(ker_path); page_switch(kernel_directory); @@ -369,16 +380,12 @@ void switch_to_user_mode(uint32_t func) { iframe.edx = 6; iframe.ecx = 7; iframe.eax = 8; - iframe.gs = GET_SEL(4 * 8, SA_RPL3); iframe.ds = GET_SEL(4 * 8, SA_RPL3); iframe.es = GET_SEL(4 * 8, SA_RPL3); iframe.fs = GET_SEL(4 * 8, SA_RPL3); - iframe.ss = GET_SEL(4 * 8, SA_RPL3); iframe.cs = GET_SEL(3 * 8, SA_RPL3); - //set_tss_ss0(iframe.ss); - iframe.eip = func; //用户可执行程序入口 iframe.eflags = (0 << 12 | 0b10 | 1 << 9); iframe.esp = esp; // 设置用户态堆栈