Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "userprog/syscall.h"
- #include <stdio.h>
- #include <syscall-nr.h>
- #include "threads/thread.h"
- #include "devices/shutdown.h"
- #include "threads/vaddr.h"
- #include "pagedir.h"
- #include "process.h"
- #include "filesys/filesys.h"
- #include <string.h>
- #include "filesys/file.h"
- #include "threads/malloc.h"
- #include "devices/input.h"
- static struct lock file_sys_lock;
- static struct list open_files_list;
- static int curr_file_desc = 1;
- struct file_opened {
- struct list_elem elem;
- struct file* file;
- int fd;
- };
- void validate_addr(void* addr , struct intr_frame *f UNUSED){
- if(addr == NULL || !is_user_vaddr(addr) ||
- (uint32_t)addr < 0x08048000 || !pagedir_get_page(thread_current()->pagedir, addr)){
- f->eax = -1;
- printf("%s: exit(%d)\n", thread_current ()->name, -1);
- thread_exit();
- }
- }
- static void syscall_handler (struct intr_frame *);
- void
- syscall_init (void)
- {
- list_init(&open_files_list);
- lock_init(&file_sys_lock);
- intr_register_int (0x30, 3, INTR_ON, syscall_handler, "syscall");
- }
- static void
- syscall_handler (struct intr_frame *f UNUSED){
- uint32_t* args = ((uint32_t*) f->esp);
- validate_addr(args , f);
- if (args[0] == SYS_EXIT) {
- validate_addr(&args[1], f);
- f->eax = args[1];
- thread_current()->parent->child_exit_code = f->eax;
- printf("%s: exit(%d)\n", thread_current ()->name, args[1]);
- thread_exit();
- }
- if (args[0] == SYS_PRACTICE){
- validate_addr(&args[1], f);
- int i = args[1];
- f->eax = i + 1;
- return;
- }
- if (args[0] == SYS_HALT){
- shutdown_power_off();
- return;
- }
- if (args[0] == SYS_EXEC){
- validate_addr(&args[1], f);
- validate_addr((void*)args[1], f);
- f->eax = process_execute((char*)args[1]);
- return;
- }
- if(args[0] == SYS_WAIT){
- validate_addr(&args[1], f);
- f->eax = process_wait((tid_t)args[1]);
- thread_current()->child_exit_code = -1;
- return;
- }
- if(args[0] == SYS_CREATE) {
- validate_addr(&args[1] , f);
- validate_addr(&args[2] , f);
- validate_addr((void*)args[1] , f);
- lock_acquire(&file_sys_lock);
- f->eax = filesys_create((char*)args[1] , (int)args[2]);
- lock_release(&file_sys_lock);
- return;
- }
- if(args[0] == SYS_REMOVE) {
- validate_addr(&args[1] , f);
- validate_addr((void*)args[1] , f);
- lock_acquire(&file_sys_lock);
- f->eax = filesys_remove((char*)args[1]);
- lock_release(&file_sys_lock);
- return;
- }
- if(args[0] == SYS_OPEN) {
- validate_addr(&args[1] , f);
- validate_addr((void*)args[1] , f);
- lock_acquire(&file_sys_lock);
- struct file* openFile = filesys_open((char*)args[1]);
- if(openFile != NULL) {
- struct file_opened* currFile = malloc(sizeof(struct file_opened));
- currFile->file = openFile;
- curr_file_desc++;
- currFile->fd = curr_file_desc;
- list_push_back(&open_files_list, &(currFile->elem));
- f->eax = curr_file_desc;
- lock_release(&file_sys_lock);
- return;
- }
- f->eax = -1;
- lock_release(&file_sys_lock);
- return;
- }
- if(args[0] == SYS_FILESIZE) {
- validate_addr(&args[1] , f);
- lock_acquire(&file_sys_lock);
- struct file_opened* found_file = NULL;
- struct list_elem* curr = list_begin(&open_files_list);
- while(curr!= list_end(&open_files_list)){
- struct file_opened* curr_elem = list_entry(curr, struct file_opened, elem);
- if (curr_elem->fd == (int)args[1]){
- found_file = curr_elem;
- break;
- }
- curr = list_next(curr);
- }
- if(found_file != NULL) {
- f->eax = file_length(found_file->file);
- } else {
- f->eax = -1;
- }
- lock_release(&file_sys_lock);
- return;
- }
- if(args[0] == SYS_READ) {
- validate_addr(&args[1] , f);
- validate_addr(&args[2] , f);
- validate_addr((void*)args[2] , f);
- validate_addr(&args[3] , f);
- lock_acquire(&file_sys_lock);
- struct file_opened* found_file = NULL;
- struct list_elem* curr = list_begin(&open_files_list);
- while(curr!= list_end(&open_files_list)){
- struct file_opened* curr_elem = list_entry(curr, struct file_opened, elem);
- if (curr_elem->fd == (int)args[1]){
- found_file = curr_elem;
- break;
- }
- curr = list_next(curr);
- }
- if((int)args[1] == 0) {
- int i;
- for(i = 0; i < (int)args[3]; i++)
- ((char*)args[2])[i] = input_getc();
- }else if(found_file != NULL) {
- unsigned int size = args[3];
- f->eax = file_read(found_file->file, (void*)args[2], size);
- }else{
- f->eax = -1;
- }
- lock_release(&file_sys_lock);
- return;
- }
- if(args[0] == SYS_WRITE) {
- validate_addr(&args[1] , f);
- validate_addr(&args[2] , f);
- validate_addr((void*)args[2] , f);
- validate_addr(&args[3] , f);
- lock_acquire(&file_sys_lock);
- struct file_opened* found_file = NULL;
- struct list_elem* curr = list_begin(&open_files_list);
- while(curr!= list_end(&open_files_list)){
- struct file_opened* curr_elem = list_entry(curr, struct file_opened, elem);
- if (curr_elem->fd == (int)args[1]){
- found_file = curr_elem;
- break;
- }
- curr = list_next(curr);
- }
- if(found_file != NULL) {
- unsigned int size = args[3];
- f->eax = file_write(found_file->file, (void*)args[2], size);
- }else if((int)args[1] == 1){
- putbuf((char*)args[2], (int)args[3]);
- f->eax = (int)args[3];
- }else{
- f->eax = -1;
- }
- lock_release(&file_sys_lock);
- return;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement