修复用户进程创建问题

This commit is contained in:
XIAOYI12 2024-09-07 23:26:22 +08:00
parent 91c097aad6
commit c372bc177a
11 changed files with 86 additions and 31 deletions

View File

@ -3,10 +3,6 @@
int main(){ int main(){
printf("Init service launched.\n"); printf("Init service launched.\n");
syscall_vfs_change_path("apps"); exec_elf("shell.bin");
int pid = exec_elf("shell.bin");
if(pid != -1){
printf("Shell launch win! PID:[%d]\n",pid);
} else printf("Error: Cannot launch shell\n");
return 0; return 0;
} }

View File

@ -1,4 +1,5 @@
#include "../include/syscall.h" #include "../include/syscall.h"
#include "../include/stdio.h"
void syscall_print(char* c){ void syscall_print(char* c){
uint32_t rets; uint32_t rets;

View File

@ -33,5 +33,6 @@ void print_info(){
printf(" :+#@@@@@@@@@@@@@@@@@@@@#+: \n"); printf(" :+#@@@@@@@@@@@@@@@@@@@@#+: \n");
printf(" :=+*#&@@@@@@&#*+-: \n"); printf(" :=+*#&@@@@@@&#*+-: \n");
free_info(info); free_info(info);
} }

View File

@ -105,7 +105,6 @@ void pci_config(unsigned int bus, unsigned int f, unsigned int equipment, unsign
} }
void init_pci(){ void init_pci(){
PCI_ADDR_BASE = kmalloc(1 * 1024 * 1024); PCI_ADDR_BASE = kmalloc(1 * 1024 * 1024);
unsigned int i, BUS, Equipment, F, ADDER, *i1; unsigned int i, BUS, Equipment, F, ADDER, *i1;
unsigned char *PCI_DATA = PCI_ADDR_BASE, *PCI_DATA1; unsigned char *PCI_DATA = PCI_ADDR_BASE, *PCI_DATA1;

View File

@ -389,6 +389,7 @@ bool ISO_ReadFile(struct vfs_t *vfs, char *path, char *buffer) {
} }
return false; // not found return false; // not found
} }
for (;;) { for (;;) {
size_t read; size_t read;
l9660_read(&file, buffer, 128, &read); l9660_read(&file, buffer, 128, &read);

View File

@ -117,10 +117,12 @@ bool vfs_readfile(char *path, char *buffer) {
char *new_path = kmalloc(strlen(path) + 1); char *new_path = kmalloc(strlen(path) + 1);
strcpy(new_path, path); strcpy(new_path, path);
vfs_t *vfs = ParsePath(new_path); vfs_t *vfs = ParsePath(new_path);
if (vfs == NULL) { if (vfs == NULL) {
kfree(new_path); kfree(new_path);
return false; return false;
} }
int result = vfs->ReadFile(vfs, new_path, buffer); int result = vfs->ReadFile(vfs, new_path, buffer);
kfree(new_path); kfree(new_path);
return result; return result;
@ -321,9 +323,6 @@ vfs_file *get_cur_file(char* filename){
} }
DeleteList(ls); DeleteList(ls);
kfree(ls); kfree(ls);
return file; 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() { void init_vfs() {
for (int i = 0; i < 26; i++) { for (int i = 0; i < 26; i++) {
vfsstl[i].flag = 0; 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){ void vfs_copy(struct task_struct *task,vfs_t* src){
vfs_change_disk(task, src->drive); vfs_change_disk(task, src->drive);
List *l; char* buffer[255];
char *path;
for (int i = 1; FindForCount(i, task->vfs_now->path) != NULL; i++) { vfs_getPath_no_drive_src(src,buffer);
l = FindForCount(i, task->vfs_now->path);
path = (char *)l->val; char *buf = kmalloc(strlen(buffer) + 1);
task->vfs_now->cd(task->vfs_now, path); 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);
} }

View File

@ -75,5 +75,6 @@ bool vfs_register_fs(vfs_t vfs);
void init_vfs(); void init_vfs();
vfs_file *get_cur_file(char* filename); vfs_file *get_cur_file(char* filename);
void vfs_copy(struct task_struct *task,vfs_t* src); void vfs_copy(struct task_struct *task,vfs_t* src);
void vfs_getPath_no_drive_src(vfs_t *src,char *buffer);
#endif #endif

View File

@ -159,10 +159,10 @@ void kernel_main(multiboot_t *multiboot) {
clock_sleep(25); clock_sleep(25);
vfs_change_path("apps"); 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"); //int pid = user_process("shell.bin","UserShell");
kernel_thread(check_task_usershell,&pid,"CTU"); //kernel_thread(check_task_usershell,&pid,"CTU");
// int pid = kernel_thread(setup_shell,NULL,"CPOS-Shell"); // int pid = kernel_thread(setup_shell,NULL,"CPOS-Shell");
// klogf(pid != -1,"Launch kernel shell.\n"); // klogf(pid != -1,"Launch kernel shell.\n");

View File

@ -166,6 +166,7 @@ void page_fault(registers_t *regs) {
} }
sleep(1); sleep(1);
while (1) io_hlt();
io_sti(); io_sti();
} }
@ -252,9 +253,6 @@ void init_page(multiboot_t *mboot) {
j += 0x1000; j += 0x1000;
} }
register_interrupt_handler(14, page_fault); register_interrupt_handler(14, page_fault);
switch_page_directory(kernel_directory); switch_page_directory(kernel_directory);

View File

@ -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){ 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] = { void *sycall_handlers[MAX_SYSCALLS] = {

View File

@ -137,10 +137,12 @@ void task_kill(int pid) {
struct task_struct *argv = found_task_pid(pid); struct task_struct *argv = found_task_pid(pid);
if (argv == NULL) { if (argv == NULL) {
printf("Cannot found task Pid:[%d].\n", pid); printf("Cannot found task Pid:[%d].\n", pid);
io_sti();
return; return;
} }
if (argv->pid == 0) { if (argv->pid == 0) {
printf("\033ff3030;[kernel]: Taskkill cannot terminate kernel processes.\033c6c6c6;\n"); printf("\033ff3030;[kernel]: Taskkill cannot terminate kernel processes.\033c6c6c6;\n");
io_sti();
return; return;
} }
argv->state = TASK_DEATH; 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){ // 用户进程创建 int32_t user_process(char *path, char *name){ // 用户进程创建
can_sche = 0; can_sche = 0;
if(path == NULL){ if(path == NULL){
return -1; return NULL;
} }
io_sti(); io_sti();
uint32_t size = vfs_filesize(path); uint32_t size = vfs_filesize(path);
if(size == -1){ if(size == -1){
return -1; return NULL;
} }
io_cli(); io_cli();
struct task_struct *new_task = (struct task_struct *) kmalloc(STACK_SIZE); 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->vfs_now = NULL;
new_task->tty = kmalloc(sizeof(tty_t)); new_task->tty = kmalloc(sizeof(tty_t));
init_default_tty(new_task); init_default_tty(new_task);
io_sti();
vfs_copy(new_task,get_current()->vfs_now); vfs_copy(new_task,get_current()->vfs_now);
io_sti(); char* ker_path = kmalloc(strlen(path) + 1);
strcpy(ker_path,path);
page_switch(page); page_switch(page);
for (int i = USER_START; i < USER_END + 0x1000;i++) { //用户堆以及用户栈映射 for (int i = USER_START; i < USER_END + 0x1000;i++) { //用户堆以及用户栈映射
page_t *pg = get_page(i,1,page, false); page_t *pg = get_page(i,1,page, false);
alloc_frame(pg,0,1); alloc_frame(pg,0,1);
@ -241,12 +249,14 @@ int32_t user_process(char *path, char *name){ // 用户进程创建
char* buffer = USER_EXEC_FILE_START; char* buffer = USER_EXEC_FILE_START;
memset(buffer,0,size); memset(buffer,0,size);
vfs_readfile(path,buffer); int r = vfs_readfile(ker_path,buffer);
Elf32_Ehdr *ehdr = buffer; Elf32_Ehdr *ehdr = buffer;
if(!elf32Validate(ehdr)){ if(!elf32Validate(ehdr)){
printf("Unknown exec file format.\n"); printf("Unknown exec file format.\n");
return -1; kfree(ker_path);
return NULL;
} }
uint32_t main = ehdr->e_entry; uint32_t main = ehdr->e_entry;
load_elf(ehdr,page); 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->context.eflags = (0 << 12 | 0b10 | 1 << 9);
new_task->next = running_proc_head; new_task->next = running_proc_head;
kfree(ker_path);
page_switch(kernel_directory); page_switch(kernel_directory);
@ -369,16 +380,12 @@ void switch_to_user_mode(uint32_t func) {
iframe.edx = 6; iframe.edx = 6;
iframe.ecx = 7; iframe.ecx = 7;
iframe.eax = 8; iframe.eax = 8;
iframe.gs = GET_SEL(4 * 8, SA_RPL3); iframe.gs = GET_SEL(4 * 8, SA_RPL3);
iframe.ds = GET_SEL(4 * 8, SA_RPL3); iframe.ds = GET_SEL(4 * 8, SA_RPL3);
iframe.es = GET_SEL(4 * 8, SA_RPL3); iframe.es = GET_SEL(4 * 8, SA_RPL3);
iframe.fs = GET_SEL(4 * 8, SA_RPL3); iframe.fs = GET_SEL(4 * 8, SA_RPL3);
iframe.ss = GET_SEL(4 * 8, SA_RPL3); iframe.ss = GET_SEL(4 * 8, SA_RPL3);
iframe.cs = GET_SEL(3 * 8, SA_RPL3); iframe.cs = GET_SEL(3 * 8, SA_RPL3);
//set_tss_ss0(iframe.ss);
iframe.eip = func; //用户可执行程序入口 iframe.eip = func; //用户可执行程序入口
iframe.eflags = (0 << 12 | 0b10 | 1 << 9); iframe.eflags = (0 << 12 | 0b10 | 1 << 9);
iframe.esp = esp; // 设置用户态堆栈 iframe.esp = esp; // 设置用户态堆栈