diff options
Diffstat (limited to 'userprog/process.c')
| -rw-r--r-- | userprog/process.c | 64 |
1 files changed, 32 insertions, 32 deletions
diff --git a/userprog/process.c b/userprog/process.c index bc8a1a9..fdfb5dd 100644 --- a/userprog/process.c +++ b/userprog/process.c | |||
| @@ -33,7 +33,7 @@ struct lock filesys_lock; | |||
| 33 | /* prototypes */ | 33 | /* prototypes */ |
| 34 | static thread_func start_process NO_RETURN; | 34 | static thread_func start_process NO_RETURN; |
| 35 | static bool load (const char *args, void (**eip) (void), void **esp); | 35 | static bool load (const char *args, void (**eip) (void), void **esp); |
| 36 | static bool setup_stack (void **esp, const char *args); | 36 | static bool setup_stack (uint32_t **esp, const char *args); |
| 37 | static bool init_fd_table (struct fd_table * table); | 37 | static bool init_fd_table (struct fd_table * table); |
| 38 | 38 | ||
| 39 | /* Initialize the filesystem lock */ | 39 | /* Initialize the filesystem lock */ |
| @@ -457,7 +457,7 @@ load (const char *args, void (**eip) (void), void **esp) | |||
| 457 | } | 457 | } |
| 458 | 458 | ||
| 459 | /* Set up stack. */ | 459 | /* Set up stack. */ |
| 460 | if (!setup_stack (esp, args)) | 460 | if (!setup_stack ((uint32_t **) esp, args)) |
| 461 | goto done; | 461 | goto done; |
| 462 | 462 | ||
| 463 | /* Start address. */ | 463 | /* Start address. */ |
| @@ -589,13 +589,13 @@ load_segment (struct file *file, off_t ofs, uint8_t *upage, | |||
| 589 | You will implement this function in the Project 0. | 589 | You will implement this function in the Project 0. |
| 590 | Consider using `hex_dump` for debugging purposes */ | 590 | Consider using `hex_dump` for debugging purposes */ |
| 591 | static bool | 591 | static bool |
| 592 | setup_stack (void **esp, const char *args) | 592 | setup_stack (uint32_t **esp, const char *args) |
| 593 | { | 593 | { |
| 594 | uint8_t *kpage = NULL; | 594 | uint8_t *kpage = NULL; |
| 595 | const char *name = thread_current ()->name; | 595 | const char *name = thread_current ()->name; |
| 596 | uint8_t *argv_cur, *argv_end = PHYS_BASE; | 596 | char *argv_cur; |
| 597 | int argc = 0; | 597 | uint32_t argc = 0; |
| 598 | unsigned argslen = strlen(args); | 598 | unsigned namelen, argslen = strlen(args); |
| 599 | 599 | ||
| 600 | kpage = palloc_get_page (PAL_USER | PAL_ZERO); | 600 | kpage = palloc_get_page (PAL_USER | PAL_ZERO); |
| 601 | if (kpage == NULL) | 601 | if (kpage == NULL) |
| @@ -612,30 +612,30 @@ setup_stack (void **esp, const char *args) | |||
| 612 | if (argslen > 0) | 612 | if (argslen > 0) |
| 613 | { | 613 | { |
| 614 | argslen += 1; /* add the trailing \0 */ | 614 | argslen += 1; /* add the trailing \0 */ |
| 615 | *esp -= argslen; | 615 | *(char **) esp -= argslen; |
| 616 | memcpy(*esp, args, argslen); | 616 | memcpy(*esp, args, argslen); |
| 617 | argv_end = *esp; | ||
| 618 | } | 617 | } |
| 619 | 618 | ||
| 620 | /* copy executable name to stack */ | 619 | /* copy executable name to stack */ |
| 621 | *esp -= strlen(name) + 1; | 620 | namelen = strlen(name) + 1; |
| 622 | memcpy(*esp, name, strlen(name) + 1); | 621 | *(char **) esp -= namelen; |
| 622 | memcpy(*esp, name, namelen); | ||
| 623 | 623 | ||
| 624 | /* align our stack so far by word-size */ | 624 | /* align our currend address by word-size (thanks to thomas & edy) */ |
| 625 | *esp -= (sizeof(uint32_t) - (PHYS_BASE - *esp) % sizeof(uint32_t)); /* thanks to thomas & edy */ | 625 | *(char **) esp -= (sizeof(uint32_t) - (PHYS_BASE - *(void **) esp) % sizeof(uint32_t)); |
| 626 | 626 | ||
| 627 | /* terminate argv[] array by NULL ptr */ | 627 | /* terminate argv[] array by NULL ptr */ |
| 628 | *esp -= sizeof(uint32_t); | 628 | (*esp)--; |
| 629 | *(uint32_t *) *esp = '\0'; | 629 | **esp = '\0'; |
| 630 | 630 | ||
| 631 | /* push the argv[] entries */ | 631 | /* push the argv[] entries */ |
| 632 | if (argslen > 0) | 632 | if (argslen > 0) |
| 633 | { | 633 | { |
| 634 | /* we walk through our already pushed arguments in reverse order and | 634 | /* we walk through our already pushed arguments in reverse order and |
| 635 | replace every occurrence of the space-character with '\0'-character | 635 | replace every occurrence of the space-character with '\0'-character. |
| 636 | due to the fact we we walk in reverse order we can easily populate | 636 | due to the fact we walk in reverse order we can easily push |
| 637 | the pointers to the elements of argv[] too */ | 637 | the pointers to the elements of argv[] too. */ |
| 638 | for(argv_cur = PHYS_BASE - 1; argv_cur >= argv_end; argv_cur--) | 638 | for(argv_cur = PHYS_BASE - 1; argv_cur >= (char *)PHYS_BASE - argslen; argv_cur--) |
| 639 | { | 639 | { |
| 640 | /* check for space-character and replace if necessary */ | 640 | /* check for space-character and replace if necessary */ |
| 641 | if (*argv_cur == ' ') | 641 | if (*argv_cur == ' ') |
| @@ -644,12 +644,12 @@ setup_stack (void **esp, const char *args) | |||
| 644 | 644 | ||
| 645 | /* since we walk in reverse order and already have replaced a | 645 | /* since we walk in reverse order and already have replaced a |
| 646 | character we simply check the preceding character. if it's | 646 | character we simply check the preceding character. if it's |
| 647 | a non-'\0'-character we know an argument starts here */ | 647 | a non-'\0'-character we know an argument starts here. */ |
| 648 | if (*(argv_cur + 1) != '\0') | 648 | if (*(argv_cur + 1) != '\0') |
| 649 | { | 649 | { |
| 650 | /* push pointer to argument */ | 650 | /* push pointer to argument */ |
| 651 | *esp -= sizeof(uint32_t); | 651 | (*esp)--; |
| 652 | *(uint32_t *) *esp = (uint32_t) argv_cur + 1; | 652 | **esp = (uint32_t) argv_cur + 1; |
| 653 | argc++; | 653 | argc++; |
| 654 | } | 654 | } |
| 655 | } | 655 | } |
| @@ -659,28 +659,28 @@ setup_stack (void **esp, const char *args) | |||
| 659 | end of the loop. so let's do that */ | 659 | end of the loop. so let's do that */ |
| 660 | if (*(argv_cur + 1) != '\0') | 660 | if (*(argv_cur + 1) != '\0') |
| 661 | { | 661 | { |
| 662 | *esp -= sizeof(uint32_t); | 662 | (*esp)--; |
| 663 | *(uint32_t *) *esp = (uint32_t) argv_cur + 1; | 663 | **esp = (uint32_t) argv_cur + 1; |
| 664 | argc++; | 664 | argc++; |
| 665 | } | 665 | } |
| 666 | } | 666 | } |
| 667 | 667 | ||
| 668 | /* push argv[0] (executable name) */ | 668 | /* push argv[0] (executable name) */ |
| 669 | *esp -= sizeof(uint32_t); | 669 | (*esp)--; |
| 670 | *(uint32_t *) *esp = (uint32_t) (PHYS_BASE - (argslen + strlen(name) + 1)); | 670 | **esp = (uint32_t) (PHYS_BASE - (argslen + namelen)); |
| 671 | argc++; | 671 | argc++; |
| 672 | 672 | ||
| 673 | /* push pointer to argv[]. this is simple as it's our current address */ | 673 | /* push pointer to argv[]. this is simple as it's our last address */ |
| 674 | *(uint32_t *) (*esp - 4) = *(uint32_t *) esp; | 674 | (*esp)--; |
| 675 | *esp -= sizeof(uint32_t); | 675 | **esp = (uint32_t) (*esp + 1); |
| 676 | 676 | ||
| 677 | /* push argc */ | 677 | /* push argc */ |
| 678 | *esp -= sizeof(uint32_t); | 678 | (*esp)--; |
| 679 | *(uint32_t *) *esp = argc; | 679 | **esp = (uint32_t) argc; |
| 680 | 680 | ||
| 681 | /* push fake return address */ | 681 | /* push fake return address */ |
| 682 | *esp -= sizeof(uint32_t); | 682 | (*esp)--; |
| 683 | *(uint32_t *) *esp = 0; | 683 | **esp = 0; |
| 684 | 684 | ||
| 685 | return true; | 685 | return true; |
| 686 | } | 686 | } |
