Advertisement
CSenshi

Pintos - Project 2 (for push)

May 24th, 2019
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.94 KB | None | 0 0
  1. #include "userprog/syscall.h"
  2. #include <stdio.h>
  3. #include <syscall-nr.h>
  4. #include "threads/interrupt.h"
  5. #include "threads/thread.h"
  6. #include "devices/shutdown.h"
  7. #include "userprog/pagedir.h"
  8. #include "filesys/filesys.h"
  9. #include "threads/vaddr.h"
  10.  
  11. static void syscall_handler(struct intr_frame *);
  12.  
  13. typedef void syscall_fun(struct intr_frame *, uint32_t *);
  14.  
  15. bool check_ptr(char *ptr);
  16.  
  17. /* Projects 2 and later. */
  18. syscall_fun call_SYS_HALT, call_SYS_EXIT, call_SYS_EXEC, call_SYS_WAIT, call_SYS_CREATE,
  19.     call_SYS_REMOVE, call_SYS_OPEN, call_SYS_FILESIZE, call_SYS_READ, call_SYS_WRITE,
  20.     call_SYS_SEEK, call_SYS_TELL, call_SYS_CLOSE, call_SYS_PRACTICE;
  21.  
  22. /* Project 3 and optionally project 4. */
  23. syscall_fun call_SYS_MMAP, call_SYS_MUNMAP;
  24.  
  25. /* Project 4 only. */
  26. syscall_fun call_SYS_CHDIR, call_SYS_MKDIR, call_SYS_READDIR, call_SYS_ISDIR, call_SYS_INUMBER;
  27.  
  28. /* System Call struct */
  29. typedef struct fun_desc
  30. {
  31.   syscall_fun *fun;
  32. } fun_desc;
  33.  
  34. /* System call lookup table */
  35. fun_desc sys_call_table[] = {
  36.     {call_SYS_HALT},
  37.     {call_SYS_EXIT},
  38.     {call_SYS_EXEC},
  39.     {call_SYS_WAIT},
  40.     {call_SYS_CREATE},
  41.     {call_SYS_REMOVE},
  42.     {call_SYS_OPEN},
  43.     {call_SYS_FILESIZE},
  44.     {call_SYS_READ},
  45.     {call_SYS_WRITE},
  46.     {call_SYS_SEEK},
  47.     {call_SYS_TELL},
  48.     {call_SYS_CLOSE},
  49.     {call_SYS_PRACTICE},
  50.     {call_SYS_MMAP},
  51.     {call_SYS_MUNMAP},
  52.     {call_SYS_CHDIR},
  53.     {call_SYS_MKDIR},
  54.     {call_SYS_READDIR},
  55.     {call_SYS_ISDIR},
  56.     {call_SYS_INUMBER},
  57. };
  58.  
  59. void syscall_init(void)
  60. {
  61.   intr_register_int(0x30, 3, INTR_ON, syscall_handler, "syscall");
  62. }
  63.  
  64. static void
  65. syscall_handler(struct intr_frame *f UNUSED)
  66. {
  67.   uint32_t *args = ((uint32_t *)f->esp);
  68.   int SC_NO = args[0];
  69.  
  70.   if (!(0 <= SC_NO && SC_NO <= SYS_INUMBER))
  71.   {
  72.  
  73.     printf("Error: System call out of boundaries. SC_NO = %d\n", SC_NO);
  74.     return;
  75.   }
  76.   sys_call_table[SC_NO].fun(f, args);
  77. }
  78.  
  79. /* System Call Implementations */
  80. void call_SYS_HALT(struct intr_frame *f UNUSED, uint32_t *args UNUSED)
  81. {
  82.   shutdown_power_off();
  83.   return;
  84. }
  85.  
  86. void call_SYS_EXIT(struct intr_frame *f UNUSED, uint32_t *args UNUSED)
  87. {
  88.   f->eax = args[1];
  89.   printf("%s: exit(%d)\n", thread_current()->name, args[1]);
  90.   thread_exit();
  91. }
  92.  
  93. void call_SYS_EXEC(struct intr_frame *f UNUSED, uint32_t *args UNUSED)
  94. {
  95.   // TODO Project 2
  96.   return;
  97. }
  98.  
  99. void call_SYS_WAIT(struct intr_frame *f UNUSED, uint32_t *args UNUSED)
  100. {
  101.   // TODO Project 2
  102.   return;
  103. }
  104.  
  105. void call_SYS_CREATE(struct intr_frame *f UNUSED, uint32_t *args UNUSED)
  106. {
  107.   /* Arguments */
  108.   char *file_name = (char *)args[1];
  109.   int file_size = (int)args[2];
  110.  
  111.   /* check fo valid pointer */
  112.   if (!check_ptr(file_name))
  113.   {
  114.     args[1] = -1;
  115.     call_SYS_EXIT(f, args);
  116.     return;
  117.   }
  118.  
  119.   /* return value */
  120.   f->eax = filesys_create(file_name, file_size);
  121. }
  122.  
  123. void call_SYS_REMOVE(struct intr_frame *f UNUSED, uint32_t *args UNUSED)
  124. {
  125.   // TODO Project 2
  126.   return;
  127. }
  128.  
  129. void call_SYS_OPEN(struct intr_frame *f UNUSED, uint32_t *args UNUSED)
  130. {
  131.   // TODO Project 2
  132.   return;
  133. }
  134.  
  135. void call_SYS_FILESIZE(struct intr_frame *f UNUSED, uint32_t *args UNUSED)
  136. {
  137.   // TODO Project 2
  138.   return;
  139. }
  140.  
  141. void call_SYS_READ(struct intr_frame *f UNUSED, uint32_t *args UNUSED)
  142. {
  143.   // TODO Project 2
  144.   f->eax = 1000;
  145.   return;
  146. }
  147.  
  148. void call_SYS_WRITE(struct intr_frame *f UNUSED, uint32_t *args UNUSED)
  149. {
  150.   /* Arguments */
  151.   int fd = args[1];
  152.   void *buffer = (void *)args[2];
  153.   int size = args[3];
  154.  
  155.   /* write in buffer */
  156.   putbuf(buffer, size);
  157.  
  158.   /* save return value */
  159.   f->eax = size;
  160. }
  161.  
  162. void call_SYS_SEEK(struct intr_frame *f UNUSED, uint32_t *args UNUSED)
  163. {
  164.   // TODO Project 2
  165.   return;
  166. }
  167.  
  168. void call_SYS_TELL(struct intr_frame *f UNUSED, uint32_t *args UNUSED)
  169. {
  170.   // TODO Project 2
  171.   return;
  172. }
  173.  
  174. void call_SYS_CLOSE(struct intr_frame *f UNUSED, uint32_t *args UNUSED)
  175. {
  176.   // TODO Project 2
  177.   return;
  178. }
  179.  
  180. void call_SYS_PRACTICE(struct intr_frame *f UNUSED, uint32_t *args UNUSED)
  181. {
  182.   /* Arguments */
  183.   int practice = args[1];
  184.  
  185.   practice += 1;
  186.  
  187.   /* return value */
  188.   f->eax = practice;
  189.   return;
  190. }
  191.  
  192. void call_SYS_MMAP(struct intr_frame *f UNUSED, uint32_t *args UNUSED)
  193. {
  194.   return;
  195. }
  196.  
  197. void call_SYS_MUNMAP(struct intr_frame *f UNUSED, uint32_t *args UNUSED)
  198. {
  199.   return;
  200. }
  201.  
  202. void call_SYS_CHDIR(struct intr_frame *f UNUSED, uint32_t *args UNUSED)
  203. {
  204.   return;
  205. }
  206.  
  207. void call_SYS_MKDIR(struct intr_frame *f UNUSED, uint32_t *args UNUSED)
  208. {
  209.   return;
  210. }
  211.  
  212. void call_SYS_READDIR(struct intr_frame *f UNUSED, uint32_t *args UNUSED)
  213. {
  214.   return;
  215. }
  216.  
  217. void call_SYS_ISDIR(struct intr_frame *f UNUSED, uint32_t *args UNUSED)
  218. {
  219.   return;
  220. }
  221.  
  222. void call_SYS_INUMBER(struct intr_frame *f UNUSED, uint32_t *args UNUSED)
  223. {
  224.   return;
  225. }
  226.  
  227. /* checks given pointer whether it's valid or not */
  228. bool check_ptr(char *ptr)
  229. {
  230.   if (pagedir_get_page(thread_current()->pagedir, ptr) == NULL || !is_user_vaddr(ptr))
  231.   {
  232.     return false;
  233.   }
  234.   return true;
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement