From 1ff119e8ecfcf2c3dafdafa906970343d42d5e56 Mon Sep 17 00:00:00 2001 From: Alex Stratford <alexander3.stratford@live.uwe.ac.uk> Date: Thu, 28 Nov 2019 21:11:01 +0000 Subject: [PATCH] Alex - Added Argument Passing --- .gitignore | 1 + userprog/process.c | 49 ++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 0ab34c0..68194cd 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ cscope.files cscope.out TAGS tags +/.vs diff --git a/userprog/process.c b/userprog/process.c index 0065486..ec839e9 100644 --- a/userprog/process.c +++ b/userprog/process.c @@ -28,6 +28,9 @@ static bool load (const char *cmdline, void (**eip) (void), void **esp); tid_t process_execute (const char *file_name) { + char *save_ptr, *real_name; + real_name = strtok_r(file_name, " ", &save_ptr); + puts(real_name) char *fn_copy; tid_t tid; @@ -207,7 +210,7 @@ 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 **argv, int argc); 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, @@ -227,6 +230,21 @@ load (const char *file_name, void (**eip) (void), void **esp) bool success = false; int i; + /* extract arguments */ + // create a copy of file_name and operate on it (modifying it) + char file_name_copy[100]; + strlcpy(file_name_copy, file_name, 100); + char *argv[255]; + int argc; + char *save_ptr; + argv[0] = strtok_r(file_name_copy, " ", &save_ptr); + char *token; + argc = 1; + while ((token = strtok_r(NULL, " ", &save_ptr)) != NULL) + { + argv[argc++] = token; + } + /* Allocate and activate page directory. */ t->pagedir = pagedir_create (); if (t->pagedir == NULL) @@ -315,7 +333,7 @@ load (const char *file_name, void (**eip) (void), void **esp) } /* Set up stack. */ - if (!setup_stack (esp)) + if (!setup_stack(esp, argv, argc)) goto done; /* Start address. */ @@ -440,7 +458,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 **argv, int argc) { uint8_t *kpage; bool success = false; @@ -450,7 +468,30 @@ setup_stack (void **esp) { success = install_page (((uint8_t *) PHYS_BASE) - PGSIZE, kpage, true); if (success) { - *esp = PHYS_BASE - 12; + *esp = PHYS_BASE; + int i = argc; + // this array holds reference to differences arguments in the stack + uint32_t * arr[argc]; + while (--i >= 0) + { + *esp = *esp - (strlen(argv[i]) + 1) * sizeof(char); + arr[i] = (uint32_t *)*esp; + memcpy(*esp, argv[i], strlen(argv[i]) + 1); + } + *esp = *esp - 4; + (*(int *)(*esp)) = 0;//sentinel + i = argc; + while (--i >= 0) + { + *esp = *esp - 4;//32bit + (*(uint32_t **)(*esp)) = arr[i]; + } + *esp = *esp - 4; + (*(uintptr_t **)(*esp)) = (*esp + 4); + *esp = *esp - 4; + *(int *)(*esp) = argc; + *esp = *esp - 4; + (*(int *)(*esp)) = 0; } else palloc_free_page (kpage); } -- GitLab