Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision
  • master
1 result

Target

Select target project
  • y2-rhymansaib/pintos_student
1 result
Select Git revision
  • master
1 result
Show changes
Commits on Source (3)
...@@ -86,6 +86,7 @@ struct thread ...@@ -86,6 +86,7 @@ struct thread
tid_t tid; /* Thread identifier. */ tid_t tid; /* Thread identifier. */
enum thread_status status; /* Thread state. */ enum thread_status status; /* Thread state. */
char name[16]; /* Name (for debugging purposes). */ char name[16]; /* Name (for debugging purposes). */
int8_t exit_code; /* Exit code. */
uint8_t *stack; /* Saved stack pointer. */ uint8_t *stack; /* Saved stack pointer. */
int priority; /* Priority. */ int priority; /* Priority. */
struct list_elem allelem; /* List element for all threads list. */ struct list_elem allelem; /* List element for all threads list. */
...@@ -95,6 +96,7 @@ struct thread ...@@ -95,6 +96,7 @@ struct thread
#ifdef USERPROG #ifdef USERPROG
/* Owned by userprog/process.c. */ /* Owned by userprog/process.c. */
int exit_status;
uint32_t *pagedir; /* Page directory. */ uint32_t *pagedir; /* Page directory. */
#endif #endif
......
...@@ -28,7 +28,7 @@ static bool load (const char *cmdline, void (**eip) (void), void **esp); ...@@ -28,7 +28,7 @@ static bool load (const char *cmdline, void (**eip) (void), void **esp);
tid_t tid_t
process_execute (const char *file_name) process_execute (const char *file_name)
{ {
char *fn_copy; char *fn_copy, *filename_, *args;
tid_t tid; tid_t tid;
/* Make a copy of FILE_NAME. /* Make a copy of FILE_NAME.
...@@ -38,8 +38,13 @@ process_execute (const char *file_name) ...@@ -38,8 +38,13 @@ process_execute (const char *file_name)
return TID_ERROR; return TID_ERROR;
strlcpy (fn_copy, file_name, PGSIZE); strlcpy (fn_copy, file_name, PGSIZE);
int len = strlen(file_name) + 1;
char file_copy[len];
strlcpy(file_copy, file_name, len);
filename_ = strtok_r(file_copy, " ", &args);
/* Create a new thread to execute FILE_NAME. */ /* Create a new thread to execute FILE_NAME. */
tid = thread_create (file_name, PRI_DEFAULT, start_process, fn_copy); tid = thread_create (filename_, PRI_DEFAULT, start_process, fn_copy);
if (tid == TID_ERROR) if (tid == TID_ERROR)
palloc_free_page (fn_copy); palloc_free_page (fn_copy);
...@@ -103,6 +108,8 @@ process_exit (void) ...@@ -103,6 +108,8 @@ process_exit (void)
struct thread *cur = thread_current (); struct thread *cur = thread_current ();
uint32_t *pd; uint32_t *pd;
printf("%s: exit_code(%d)\n",cur->name,cur->exit_code);
/* Destroy the current process's page directory and switch back /* Destroy the current process's page directory and switch back
to the kernel-only page directory. */ to the kernel-only page directory. */
pd = cur->pagedir; pd = cur->pagedir;
...@@ -200,12 +207,12 @@ struct Elf32_Phdr ...@@ -200,12 +207,12 @@ struct Elf32_Phdr
#define PF_W 2 /* Writable. */ #define PF_W 2 /* Writable. */
#define PF_R 4 /* Readable. */ #define PF_R 4 /* Readable. */
static bool setup_stack (void **esp); static bool setup_stack (void **esp, char *filename, char *argu);
static bool validate_segment (const struct Elf32_Phdr *, struct file *); static bool validate_segment (const struct Elf32_Phdr *, struct file *);
static bool load_segment (struct file *file, off_t ofs, uint8_t *upage, static bool load_segment (struct file *file, off_t ofs, uint8_t *upage,
uint32_t read_bytes, uint32_t zero_bytes, uint32_t read_bytes, uint32_t zero_bytes,
bool writable); bool writable);
void handle_argsinstack(void **esp, char *file_name, char *args);
/* Loads an ELF executable from FILE_NAME into the current thread. /* Loads an ELF executable from FILE_NAME into the current thread.
Stores the executable's entry point into *EIP Stores the executable's entry point into *EIP
and its initial stack pointer into *ESP. and its initial stack pointer into *ESP.
...@@ -219,6 +226,11 @@ load (const char *file_name, void (**eip) (void), void **esp) ...@@ -219,6 +226,11 @@ load (const char *file_name, void (**eip) (void), void **esp)
off_t file_ofs; off_t file_ofs;
bool success = false; bool success = false;
int i; int i;
int len = strlen(file_name) + 1;
char file_copy[len];
strlcpy(file_copy, file_name, len);
char *args, *file_name_;
file_name_ = strtok_r(file_copy, " ", &args);
/* Allocate and activate page directory. */ /* Allocate and activate page directory. */
t->pagedir = pagedir_create (); t->pagedir = pagedir_create ();
...@@ -227,11 +239,11 @@ load (const char *file_name, void (**eip) (void), void **esp) ...@@ -227,11 +239,11 @@ load (const char *file_name, void (**eip) (void), void **esp)
process_activate (); process_activate ();
/* Open executable file. */ /* Open executable file. */
file = filesys_open (file_name); file = filesys_open (file_name_);
if (file == NULL) if (file == NULL)
{ {
printf ("load: %s: open failed\n", file_name); printf ("load: %s: open failed\n", file_name_);
goto done; goto done;
} }
...@@ -244,7 +256,7 @@ load (const char *file_name, void (**eip) (void), void **esp) ...@@ -244,7 +256,7 @@ load (const char *file_name, void (**eip) (void), void **esp)
|| ehdr.e_phentsize != sizeof (struct Elf32_Phdr) || ehdr.e_phentsize != sizeof (struct Elf32_Phdr)
|| ehdr.e_phnum > 1024) || ehdr.e_phnum > 1024)
{ {
printf ("load: %s: error loading executable\n", file_name); printf ("load: %s: error loading executable\n", file_name_);
goto done; goto done;
} }
...@@ -308,7 +320,7 @@ load (const char *file_name, void (**eip) (void), void **esp) ...@@ -308,7 +320,7 @@ load (const char *file_name, void (**eip) (void), void **esp)
} }
/* Set up stack. */ /* Set up stack. */
if (!setup_stack (esp)) if (!setup_stack (esp, file_name_, args))
goto done; goto done;
/* Start address. */ /* Start address. */
...@@ -433,7 +445,7 @@ load_segment (struct file *file, off_t ofs, uint8_t *upage, ...@@ -433,7 +445,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 /* Create a minimal stack by mapping a zeroed page at the top of
user virtual memory. */ user virtual memory. */
static bool static bool
setup_stack (void **esp) setup_stack (void **esp, char *filename, char *argu)
{ {
uint8_t *kpage; uint8_t *kpage;
bool success = false; bool success = false;
...@@ -444,6 +456,10 @@ setup_stack (void **esp) ...@@ -444,6 +456,10 @@ setup_stack (void **esp)
success = install_page (((uint8_t *) PHYS_BASE) - PGSIZE, kpage, true); success = install_page (((uint8_t *) PHYS_BASE) - PGSIZE, kpage, true);
if (success) { if (success) {
*esp = PHYS_BASE; *esp = PHYS_BASE;
//push the arguments on stack
handle_argsinstack(esp, filename, argu);
//print stack
hex_dump((uintptr_t)*esp, *esp, sizeof(char) * 50, true);
} else } else
palloc_free_page (kpage); palloc_free_page (kpage);
} }
...@@ -470,4 +486,47 @@ install_page (void *upage, void *kpage, bool writable) ...@@ -470,4 +486,47 @@ install_page (void *upage, void *kpage, bool writable)
&& pagedir_set_page (t->pagedir, upage, kpage, writable)); && pagedir_set_page (t->pagedir, upage, kpage, writable));
} }
/*Helper function push name of file and arguments on esp stack*/
void
handle_argsinstack(void **esp, char *file_name, char *args)
{
int arg_c = 0, t_arg_s = 0;
char* argu_arr[25];
for(char *arg = strtok_r(NULL," ",&args) ; arg != NULL ;
arg = strtok_r(NULL," ",&args))
{
argu_arr[++arg_c]=arg;
}
int arg_len = 0;
char* argus[arg_c + 1];
//push the argument on esp stack
for (int i = arg_c; i > 0; i--)
{
arg_len = strlen(argu_arr[i]) + 1;
t_arg_s += arg_len;
*esp -= arg_len;
memcpy(*esp,argu_arr[i],arg_len);
argus[arg_c - i + 1] = *esp;
}
//push the name of file on esp stack
int size = strlen(file_name) + 1;
*esp -= size;
memcpy(*esp,file_name,size);
argus[0] = *esp;
argus[arg_c + 1] = NULL;
//Add Padding to align the esp stack
int pad = 4 * ((t_arg_s) / 4);
*esp -= pad;
memset(*esp,0,pad);
// push the pointer array on esp stack
*esp -= 4*(arg_c + 3);
memcpy(*esp,argus,4*(arg_c + 1));
}
//-------------------------------------------------------------------- //--------------------------------------------------------------------
#include "userprog/syscall.h" #include "userprog/syscall.h"
#include <stdio.h> #include <stdio.h>
#include <syscall-nr.h> #include <syscall-nr.h>
#include "devices/shutdown.h"
#include "threads/synch.h"
#include "threads/interrupt.h" #include "threads/interrupt.h"
#include "threads/thread.h" #include "threads/thread.h"
#include "userprog/process.h"
#include "filesys/filesys.h"
typedef int pid_p;
static struct lock f_lock;
static void syscall_handler (struct intr_frame *); static void syscall_handler (struct intr_frame *);
static uint32_t load_stack(struct intr_frame *f, int offset);
/*Function for system call*/
void syscall_halt(void);
void syscall_exit(int);
int syscall_wait (pid_p pid);
bool syscall_create (const char *file, unsigned initial_size);
/*End of system call function*/
void void
syscall_init (void) syscall_init (void)
...@@ -12,10 +26,71 @@ syscall_init (void) ...@@ -12,10 +26,71 @@ syscall_init (void)
intr_register_int (0x30, 3, INTR_ON, syscall_handler, "syscall"); intr_register_int (0x30, 3, INTR_ON, syscall_handler, "syscall");
} }
static uint32_t load_stack(struct intr_frame *f, int offset)
{
return *((uint32_t*)(f->esp + offset));
}
static void static void
syscall_handler (struct intr_frame *f UNUSED) syscall_handler (struct intr_frame *f UNUSED)
{ {
printf ("system call!\n"); int code = (int)load_stack(f, 0);
lock_init(&f_lock);
switch (code)
{
case SYS_HALT:
{
syscall_halt();
break;
}
case SYS_EXIT:
{
syscall_exit((int)load_stack(f,4));
break;
}
case SYS_WAIT:
{
f->eax = syscall_wait((int)load_stack(f,4));
break;
}
case SYS_CREATE:
{
f->eax = syscall_create((const char*)load_stack(f,4), (unsigned)load_stack(f,8));
break;
}
default:
{
printf ("system call (%d) not implemented!\n", code);
thread_exit (); thread_exit ();
} }
}
}
void syscall_halt (void)
{
shutdown_power_off();
}
void syscall_exit (int status)
{
struct thread *cur = thread_current ();
cur->exit_status = status;
thread_exit();
}
int syscall_wait (pid_p pid)
{
return process_wait (pid);
}
bool syscall_create (const char *file, unsigned initial_size)
{
lock_acquire(&f_lock);
bool success = filesys_create(file, initial_size);
lock_release(&f_lock);
return success;
}