diff --git a/src/userprog/process.c b/src/userprog/process.c index d51339a4aff8ec7aae340ae87035ef64358853fd..45c2fd527cbfb828b5b33a21b51ea4998a797975 100644 --- a/src/userprog/process.c +++ b/src/userprog/process.c @@ -28,7 +28,7 @@ static bool load (const char *cmdline, void (**eip) (void), void **esp); tid_t process_execute (const char *file_name) { - char *fn_copy; + char *fn_copy, *filename_, *args; tid_t tid; /* Make a copy of FILE_NAME. @@ -38,8 +38,13 @@ process_execute (const char *file_name) return TID_ERROR; strlcpy (fn_copy, file_name, PGSIZE); + int len = strlen(file_name) + 1; + char file_copy[len]; + strlcpy(file_copy, file_name, len); + filename_ = strtok_r(file_copy, " ", &args); + /* Create a new thread to execute FILE_NAME. */ - tid = thread_create (file_name, PRI_DEFAULT, start_process, fn_copy); + tid = thread_create (filename_, PRI_DEFAULT, start_process, fn_copy); if (tid == TID_ERROR) palloc_free_page (fn_copy); @@ -103,6 +108,8 @@ process_exit (void) struct thread *cur = thread_current (); uint32_t *pd; + printf("%s: exit_code(%d)\n",cur->name,cur->exit_code); + /* Destroy the current process's page directory and switch back to the kernel-only page directory. */ pd = cur->pagedir; @@ -200,12 +207,12 @@ struct Elf32_Phdr #define PF_W 2 /* Writable. */ #define PF_R 4 /* Readable. */ -static bool setup_stack (void **esp); +static bool setup_stack (void **esp, char *filename, char *argu); static bool validate_segment (const struct Elf32_Phdr *, struct file *); static bool load_segment (struct file *file, off_t ofs, uint8_t *upage, uint32_t read_bytes, uint32_t zero_bytes, bool writable); - +void handle_argsinstack(void **esp, char *file_name, char *args); /* Loads an ELF executable from FILE_NAME into the current thread. Stores the executable's entry point into *EIP and its initial stack pointer into *ESP. @@ -219,6 +226,11 @@ load (const char *file_name, void (**eip) (void), void **esp) off_t file_ofs; bool success = false; int i; + int len = strlen(file_name) + 1; + char file_copy[len]; + strlcpy(file_copy, file_name, len); + char *args, *file_name_; + file_name_ = strtok_r(file_copy, " ", &args); /* Allocate and activate page directory. */ t->pagedir = pagedir_create (); @@ -227,11 +239,11 @@ load (const char *file_name, void (**eip) (void), void **esp) process_activate (); /* Open executable file. */ - file = filesys_open (file_name); + file = filesys_open (file_name_); if (file == NULL) { - printf ("load: %s: open failed\n", file_name); + printf ("load: %s: open failed\n", file_name_); goto done; } @@ -244,7 +256,7 @@ load (const char *file_name, void (**eip) (void), void **esp) || ehdr.e_phentsize != sizeof (struct Elf32_Phdr) || ehdr.e_phnum > 1024) { - printf ("load: %s: error loading executable\n", file_name); + printf ("load: %s: error loading executable\n", file_name_); goto done; } @@ -308,7 +320,7 @@ load (const char *file_name, void (**eip) (void), void **esp) } /* Set up stack. */ - if (!setup_stack (esp)) + if (!setup_stack (esp, file_name_, args)) goto done; /* Start address. */ @@ -433,7 +445,7 @@ load_segment (struct file *file, off_t ofs, uint8_t *upage, /* Create a minimal stack by mapping a zeroed page at the top of user virtual memory. */ static bool -setup_stack (void **esp) +setup_stack (void **esp, char *filename, char *argu) { uint8_t *kpage; bool success = false; @@ -444,6 +456,10 @@ setup_stack (void **esp) success = install_page (((uint8_t *) PHYS_BASE) - PGSIZE, kpage, true); if (success) { *esp = PHYS_BASE; + //push the arguments on stack + handle_argsinstack(esp, filename, argu); + //print stack + hex_dump((uintptr_t)*esp, *esp, sizeof(char) * 50, true); } else palloc_free_page (kpage); } @@ -470,4 +486,47 @@ install_page (void *upage, void *kpage, bool writable) && pagedir_set_page (t->pagedir, upage, kpage, writable)); } +/*Helper function push name of file and arguments on esp stack*/ +void +handle_argsinstack(void **esp, char *file_name, char *args) +{ + int arg_c = 0, t_arg_s = 0; + char* argu_arr[25]; + for(char *arg = strtok_r(NULL," ",&args) ; arg != NULL ; + arg = strtok_r(NULL," ",&args)) + { + argu_arr[++arg_c]=arg; + } + int arg_len = 0; + char* argus[arg_c + 1]; + + //push the argument on esp stack + for (int i = arg_c; i > 0; i--) + { + arg_len = strlen(argu_arr[i]) + 1; + t_arg_s += arg_len; + *esp -= arg_len; + memcpy(*esp,argu_arr[i],arg_len); + argus[arg_c - i + 1] = *esp; + } + + //push the name of file on esp stack + int size = strlen(file_name) + 1; + *esp -= size; + memcpy(*esp,file_name,size); + argus[0] = *esp; + + argus[arg_c + 1] = NULL; + + //Add Padding to align the esp stack + int pad = 4 * ((t_arg_s) / 4); + *esp -= pad; + memset(*esp,0,pad); + + // push the pointer array on esp stack + *esp -= 4*(arg_c + 3); + memcpy(*esp,argus,4*(arg_c + 1)); + +} + //--------------------------------------------------------------------