Monday, September 3, 2012

Pintos Project 2 Argument Passing, User Programs

A brief intro to argument passing in Pintos
Just like in a regular command line argument passing, Pintos already does the parsing of the arguments passed to it via command line. This is done in the file "threads/init.c" in the 'main' function. Running a typical User program involves issuing the following command

>>pintos -q run 'echo x'

This causes the parsing of the arguments like this
"-q">> this is just a pintos option for powering off the system once the processing is done. In the parse_options function appropriate flags are set indicating that the option has been acknowledged and that the PINTOS will be turned off once the task is completed.

All the arguments which are pintos options will get parsed in this function.

Main then calls run_actions with the following argument
" run echo x y " which is passed as a pointer named "argv"

run_actions determines what task needs to be done by using the first string available here i.e. "run", which is nothing but a command which says run the program 'echo' with the arguments 'x' and 'y'.
This leads to the calling of the function "run_task" (due to the argument run) with the argument argv which now points to the string <echo x y>. Run task calls 'process_execute' located in 'userprog/process.c' with the pointer to this string.

process_execute() creates a new thread passing 'start_process' as the function to be performed whose task is to load this user program and the string <echo x y> is passed as well.

start_process creates the interrupt frame and goes on to load the user program and then run it.

inside load is where you need to do the modifications.....

Firstly since the argument file_name_which is passed points to the string <echo x y>, You need to get the name of the user program which needs to loaded which in this case is 'echo'.  You may want to look at the functions present in "lib/string.c" on how this can be done or create your own function to do this.

  file = filesys_open ("echo");          //line 225
the only thing is that instead of this argument, a pointer pointing to this string should be passed.

so that the correct program file will be opened since passing <echo x y> will cause an error as there won't be any such program file.

Then your work is to set up the stack which is done in the function setup_stack called in the line 305.

Insert your code after the page is successfully installed i.e inside
 if (success) { *esp = PHYS_BASE;...................     }

What all you need to do .....
push all the arguments onto the stack in reverse order ...
i.e. PUSH "y\0"
      PUSH "x\0"
      PUSH "echo\0"
You need to remember the addresses  on the stack where these strings are being pushed as u later to need to push these addresses as well.

then include the following line

"*esp=*esp-4+(strlength("x")+1 +strlength("y")+1........)%4;
in order to make the esp pointer a multiple of 4

then push a NULL pointer sentinal of type "uint32_t" corresponding to argv[argc]
here argc is clearly 4

there-after push the addresses the strings on the stack just passed again in the same order.
PUSH addressonstack"y/0"
PUSH addressonstack"x/0"
PUSH addressonstack"echo\0" //******

then push the address of the stack position which contains the address of the string "echo\0", starred in the previous line. Push the value of argc then finally push a fake return point may be NULL.

and you are done...

I haven't given complete details of How this needs to be done which I think you should try before asking some body. Feel free to reply to this thread for any comments, questions or doubts ....









2 comments: