Advertisement
Guest User

dimadimachi

a guest
May 26th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.18 KB | None | 0 0
  1. #include "userprog/syscall.h"
  2. #include <stdio.h>
  3. #include <syscall-nr.h>
  4. #include "threads/thread.h"
  5. #include "devices/shutdown.h"
  6. #include "threads/vaddr.h"
  7. #include "pagedir.h"
  8. #include "process.h"
  9. #include "filesys/filesys.h"
  10. #include <string.h>
  11. #include "filesys/file.h"
  12. #include "threads/malloc.h"
  13. #include "devices/input.h"
  14.  
  15. static struct lock file_sys_lock;
  16. static struct list open_files_list;
  17. static int curr_file_desc = 1;
  18.  
  19. struct file_opened {
  20.     struct list_elem elem;
  21.     struct file* file;
  22.     int fd;
  23. };
  24.  
  25. void validate_addr(void* addr , struct intr_frame *f UNUSED){
  26.     if(addr == NULL || !is_user_vaddr(addr) ||
  27.     (uint32_t)addr < 0x08048000 ||  !pagedir_get_page(thread_current()->pagedir, addr)){
  28.         f->eax = -1;
  29.         printf("%s: exit(%d)\n", thread_current ()->name, -1);
  30.         thread_exit();
  31.     }
  32. }
  33.  
  34. static void syscall_handler (struct intr_frame *);
  35.  
  36. void
  37. syscall_init (void)
  38. {
  39.     list_init(&open_files_list);
  40.     lock_init(&file_sys_lock);
  41.     intr_register_int (0x30, 3, INTR_ON, syscall_handler, "syscall");
  42. }
  43.  
  44. static void
  45. syscall_handler (struct intr_frame *f UNUSED){
  46.     uint32_t* args = ((uint32_t*) f->esp);
  47.     validate_addr(args , f);
  48.     if (args[0] == SYS_EXIT) {
  49.         validate_addr(&args[1], f);
  50.         f->eax = args[1];
  51.         thread_current()->parent->child_exit_code = f->eax;
  52.         printf("%s: exit(%d)\n", thread_current ()->name, args[1]);
  53.         thread_exit();
  54.     }
  55.     if (args[0] == SYS_PRACTICE){
  56.         validate_addr(&args[1], f);
  57.         int i = args[1];
  58.         f->eax = i + 1;
  59.         return;
  60.     }
  61.     if (args[0] == SYS_HALT){
  62.         shutdown_power_off();
  63.         return;
  64.     }
  65.     if (args[0] == SYS_EXEC){
  66.         validate_addr(&args[1], f);
  67.         validate_addr((void*)args[1], f);
  68.         f->eax = process_execute((char*)args[1]);
  69.  
  70.         return;
  71.     }
  72.     if(args[0] == SYS_WAIT){
  73.         validate_addr(&args[1], f);
  74.         f->eax = process_wait((tid_t)args[1]);
  75.         thread_current()->child_exit_code = -1;
  76.         return;
  77.     }
  78.     if(args[0] == SYS_CREATE) {
  79.         validate_addr(&args[1] , f);
  80.         validate_addr(&args[2] , f);
  81.         validate_addr((void*)args[1] , f);
  82.         lock_acquire(&file_sys_lock);
  83.         f->eax = filesys_create((char*)args[1] , (int)args[2]);
  84.         lock_release(&file_sys_lock);
  85.         return;
  86.     }
  87.     if(args[0] == SYS_REMOVE) {
  88.         validate_addr(&args[1] , f);
  89.         validate_addr((void*)args[1] , f);
  90.         lock_acquire(&file_sys_lock);
  91.         f->eax = filesys_remove((char*)args[1]);
  92.         lock_release(&file_sys_lock);
  93.         return;
  94.     }
  95.     if(args[0] == SYS_OPEN) {
  96.         validate_addr(&args[1] , f);
  97.         validate_addr((void*)args[1] , f);
  98.         lock_acquire(&file_sys_lock);
  99.         struct file* openFile = filesys_open((char*)args[1]);
  100.         if(openFile != NULL) {
  101.             struct file_opened* currFile = malloc(sizeof(struct file_opened));
  102.             currFile->file = openFile;
  103.             curr_file_desc++;
  104.             currFile->fd = curr_file_desc;
  105.             list_push_back(&open_files_list, &(currFile->elem));
  106.             f->eax = curr_file_desc;
  107.             lock_release(&file_sys_lock);
  108.             return;
  109.         }
  110.         f->eax = -1;
  111.         lock_release(&file_sys_lock);
  112.         return;
  113.     }
  114.     if(args[0] == SYS_FILESIZE) {
  115.         validate_addr(&args[1] , f);
  116.         lock_acquire(&file_sys_lock);
  117.         struct file_opened* found_file = NULL;
  118.         struct list_elem* curr = list_begin(&open_files_list);
  119.         while(curr!= list_end(&open_files_list)){
  120.             struct file_opened* curr_elem = list_entry(curr, struct file_opened, elem);
  121.             if (curr_elem->fd == (int)args[1]){
  122.                 found_file = curr_elem;
  123.                 break;
  124.             }
  125.             curr = list_next(curr);
  126.         }
  127.         if(found_file != NULL) {
  128.             f->eax = file_length(found_file->file);
  129.         } else {
  130.             f->eax = -1;
  131.         }
  132.         lock_release(&file_sys_lock);
  133.         return;
  134.     }
  135.     if(args[0] == SYS_READ) {
  136.         validate_addr(&args[1] , f);
  137.         validate_addr(&args[2] , f);
  138.         validate_addr((void*)args[2] , f);
  139.         validate_addr(&args[3] , f);
  140.         lock_acquire(&file_sys_lock);
  141.         struct file_opened* found_file = NULL;
  142.         struct list_elem* curr = list_begin(&open_files_list);
  143.         while(curr!= list_end(&open_files_list)){
  144.             struct file_opened* curr_elem = list_entry(curr, struct file_opened, elem);
  145.             if (curr_elem->fd == (int)args[1]){
  146.                 found_file = curr_elem;
  147.                 break;
  148.             }
  149.             curr = list_next(curr);
  150.         }
  151.         if((int)args[1] == 0) {
  152.             int i;
  153.             for(i = 0; i < (int)args[3]; i++)
  154.                 ((char*)args[2])[i] = input_getc();
  155.         }else if(found_file != NULL) {
  156.             unsigned int size = args[3];
  157.             f->eax = file_read(found_file->file, (void*)args[2], size);
  158.         }else{
  159.             f->eax = -1;
  160.         }
  161.         lock_release(&file_sys_lock);
  162.         return;
  163.     }
  164.     if(args[0] == SYS_WRITE) {
  165.         validate_addr(&args[1] , f);
  166.         validate_addr(&args[2] , f);
  167.         validate_addr((void*)args[2] , f);
  168.         validate_addr(&args[3] , f);
  169.         lock_acquire(&file_sys_lock);
  170.         struct file_opened* found_file = NULL;
  171.         struct list_elem* curr = list_begin(&open_files_list);
  172.         while(curr!= list_end(&open_files_list)){
  173.             struct file_opened* curr_elem = list_entry(curr, struct file_opened, elem);
  174.             if (curr_elem->fd == (int)args[1]){
  175.                 found_file = curr_elem;
  176.                 break;
  177.             }
  178.             curr = list_next(curr);
  179.         }
  180.  
  181.         if(found_file != NULL) {
  182.             unsigned int size = args[3];
  183.             f->eax = file_write(found_file->file, (void*)args[2], size);
  184.         }else if((int)args[1] == 1){
  185.             putbuf((char*)args[2], (int)args[3]);
  186.             f->eax = (int)args[3];
  187.         }else{
  188.             f->eax = -1;
  189.         }
  190.  
  191.         lock_release(&file_sys_lock);
  192.         return;
  193.     }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement