diff --git a/Makefile.build b/Makefile.build index e997d2787543cb4860edff352660dea9ae350bcf..823d5af3f240461c01f294d8907cfee38a496ac4 100644 --- a/Makefile.build +++ b/Makefile.build @@ -55,6 +55,8 @@ lib/kernel_SRC += lib/kernel/console.c # printf(), putchar(). # User process code. userprog_SRC = userprog/process.c # Process loading. +userprog_SRC += userprog/parse_arguments.c # Parses arguments from command line string. +userprog_SRC += userprog/populate_stack.c # Populates stack with program arguments. userprog_SRC += userprog/pagedir.c # Page directories. userprog_SRC += userprog/exception.c # User exception handler. userprog_SRC += userprog/syscall.c # System call handler. diff --git a/userprog/argument_parsing.h b/userprog/argument_parsing.h new file mode 100644 index 0000000000000000000000000000000000000000..78f4e0eca38b95a4c16e3963d9944105bc1906ef --- /dev/null +++ b/userprog/argument_parsing.h @@ -0,0 +1,21 @@ +#ifndef USERPROG_ARGUMENT_PARSING_H +#define USERPROG_ARGUMENT_PARSING_H + +/* + * Given a string containing the command invoking the program `command_line` + * and a pointer to an array of C strings (pointer to `char* argv[]`), parse + * `command_line` into individual arguments, populate `argv` with these and + * return the number of arguments that were parsed (this is the value that can + * be used for `argc`). + */ +int parse_arguments(const char* command_line, char***argv); + +/* + * Given stack pointer `esp`, argument count `argc` and arguments array `argv`, + * Populates the stack pointed to by the stack pointer with the given arguments. + * TODO: clarify what, if anything, `esp` stands for and rename it to something + * clearer if possible. + */ +void populate_stack(void* esp, int argc, char** argv); + +#endif /* userprog/argument_parsing.h */ diff --git a/userprog/parse_arguments.c b/userprog/parse_arguments.c new file mode 100644 index 0000000000000000000000000000000000000000..aeaefd0778156808a54f8c2b1da7b1197886efbc --- /dev/null +++ b/userprog/parse_arguments.c @@ -0,0 +1,6 @@ +#include "userprog/argument_parsing.h" + + +int parse_arguments(const char* command_line, char***argv) { + #warning "Implement me" +} diff --git a/userprog/populate_stack.c b/userprog/populate_stack.c new file mode 100644 index 0000000000000000000000000000000000000000..6a7bb2e4897aa6e05b7cd050e2ebff4e41445525 --- /dev/null +++ b/userprog/populate_stack.c @@ -0,0 +1,6 @@ +#include "userprog/argument_parsing.h" + + +void populate_stack(void* esp, int argc, char** argv) { + #warning "Implement me" +} diff --git a/userprog/process.c b/userprog/process.c index 1f6f72a73c1c984ecdca3ed54da0f8bca28e4a27..7aa158189374917306d5021e01bc54caea8b38fe 100644 --- a/userprog/process.c +++ b/userprog/process.c @@ -21,28 +21,35 @@ static thread_func start_process NO_RETURN; static bool load (const char *cmdline, void (**eip) (void), void **esp); -/* Starts a new thread running a user program loaded from - FILENAME. The new thread may be scheduled (and may even exit) +/* Starts a new thread running a user program loaded by parsing + COMMAND. The new thread may be scheduled (and may even exit) before process_execute() returns. Returns the new process's thread id, or TID_ERROR if the thread cannot be created. */ tid_t -process_execute (const char *file_name) +process_execute (const char *command) { - char *fn_copy; + char *command_copy; tid_t tid; - /* Make a copy of FILE_NAME. + /* Make a copy of COMMAND. Otherwise there's a race between the caller and load(). */ - fn_copy = palloc_get_page (0); - if (fn_copy == NULL) + command_copy = palloc_get_page (0); + if (command_copy == NULL) return TID_ERROR; - strlcpy (fn_copy, file_name, PGSIZE); - - /* Create a new thread to execute FILE_NAME. */ - tid = thread_create (file_name, PRI_DEFAULT, start_process, fn_copy); + strlcpy (command_copy, command, PGSIZE); + + /* Create a new thread to execute COMMAND. */ + /* + * FIXME: right now, COMMAND is assumed to be just the filename on its own + * + * If this is not the case (if the user passed a program name with arguments), + * then loading will fail. + * NOTE: remove this comment block when argument parsing is implemented. + */ + tid = thread_create (command, PRI_DEFAULT, start_process, command_copy); if (tid == TID_ERROR) - palloc_free_page (fn_copy); + palloc_free_page (command_copy); return tid; } diff --git a/userprog/process.h b/userprog/process.h index 688cd2a37fd2c1bd7fdcb417f834026a25fa4945..ad49fecdbd9c79a0019f7a9bc102e0ad8e018240 100644 --- a/userprog/process.h +++ b/userprog/process.h @@ -3,7 +3,7 @@ #include "threads/thread.h" -tid_t process_execute (const char *file_name); +tid_t process_execute (const char *command); int process_wait (tid_t); void process_exit (void); void process_activate (void);