Guest User

Untitled

a guest
May 16th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. static void
  2. start_process (void *file_name_)
  3. {
  4.  
  5. lock_acquire(&process_load_lock);
  6.  
  7. char *file_name = file_name_;
  8. struct intr_frame if_;
  9. bool success;
  10. int nrOfArgs = -1;
  11.  
  12. /* Initialize interrupt frame and load executable. */
  13. memset (&if_, 0, sizeof if_);
  14. if_.gs = if_.fs = if_.es = if_.ds = if_.ss = SEL_UDSEG;
  15. if_.cs = SEL_UCSEG;
  16. if_.eflags = FLAG_IF | FLAG_MBS;
  17.  
  18.  
  19.  
  20. char *token, *save_ptr;
  21. int i = 0;
  22. for (i=0,token = strtok_r (file_name, " ", &save_ptr);i<MAX_NR_ARGS && token != NULL; i++, token = strtok_r (NULL, " ", &save_ptr))
  23. {
  24. argv_global[i] = token;
  25. }
  26. nrOfArgs = i;
  27. success = load (file_name, &if_.eip, &if_.esp);
  28.  
  29. if(success)
  30. {
  31. int tokenSize = 0;
  32.  
  33. for(i= nrOfArgs-1 ; i>=0; i-- )
  34. {
  35. tokenSize = strlen( argv_global[i] );
  36. if_.esp = if_.esp - (tokenSize+1);
  37. strlcpy((char*)(if_.esp), argv_global[i], PGSIZE);
  38. argv_global[i] = (char*)(if_.esp);
  39.  
  40. //printf("start address of array %i = %x\n",i,(int)(argv_global[i]));
  41. }
  42.  
  43. int stackAddr = (int)(if_.esp);
  44.  
  45. /* the only solution we found to word align the code ROUND_DOWN does not work , \
  46. * probably because of some compiler optimization*/
  47. while(stackAddr%4 != 0)
  48. {
  49. stackAddr--;
  50. *((char*)stackAddr) = 0;
  51. }
  52. if_.esp = (void*)stackAddr;
  53.  
  54. if_.esp = if_.esp - 4;
  55. *((int*)if_.esp) = 0;
  56.  
  57. for(i= nrOfArgs-1 ; i>=0; i-- )
  58. {
  59. if_.esp = if_.esp - 4;
  60. *((int*)if_.esp)= (int)argv_global[i];
  61. }
  62. //we save the address of argv
  63. stackAddr = (int)if_.esp;
  64. //we put argv on the stack
  65. if_.esp = if_.esp - 4;
  66. *((int*)if_.esp) = stackAddr;
  67.  
  68. //we put argc on the stack
  69. if_.esp = if_.esp - 4;
  70. *((int*)if_.esp) = nrOfArgs;
  71.  
  72. //we put the return address on the stack
  73. if_.esp = if_.esp - 4;
  74. *((int*)if_.esp) = 0;
  75. }
  76. for(i =0 ; i< MAX_NR_ARGS; i++ )
  77. argv_global[i] = NULL;
  78.  
  79. lock_release(&process_load_lock);
  80.  
  81. /* If load failed, quit. */
  82. palloc_free_page (file_name);
  83.  
  84. thread_current()->statusForParent->loaded = success;
  85. sema_up(&thread_current()->statusForParent->sem);
  86. if (!success)
  87. thread_exit ();
  88.  
  89. /* Start the user process by simulating a return from an
  90. interrupt, implemented by intr_exit (in
  91. threads/intr-stubs.S). Because intr_exit takes all of its
  92. arguments on the stack in the form of a `struct intr_frame',
  93. we just point the stack pointer (%esp) to our stack frame
  94. and jump to it. */
  95. asm volatile ("movl %0, %%esp; jmp intr_exit" : : "g" (&if_) : "memory");
  96. NOT_REACHED ();
  97. }
Add Comment
Please, Sign In to add comment