Advertisement
Guest User

Untitled

a guest
Jan 19th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 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.  
  7. static void syscall_handler(struct intr_frame *);
  8.  
  9. void syscall_init(void) {
  10. intr_register_int(0x30, 3, INTR_ON, syscall_handler, "syscall");
  11. }
  12.  
  13. /*
  14. SYS_CREATE,
  15. SYS_REMOVE,
  16. SYS_OPEN,
  17. SYS_FILESIZE,
  18. SYS_READ,
  19. SYS_WRITE,
  20. SYS_SEEK,
  21. SYS_TELL,
  22. SYS_CLOSE,
  23. */
  24.  
  25. static void syscall_handler(struct intr_frame *f UNUSED) {
  26. valid_address(f->esp);
  27. int * system_call = f->esp;
  28. int args[3];
  29.  
  30. switch (system_call) {
  31.  
  32. case SYS_CREATE:
  33. stack_args(f, &args[0], 2);
  34. acquire_file_lock();
  35. f->eax = filesys_create(&args[0], args[1]);
  36. release_file_lock();
  37. break;
  38.  
  39. case SYS_REMOVE:
  40. stack_args(f, args, 1);
  41. acquire_filesys_lock();
  42. bool status = filesys_remove(*(args[0]));
  43. if (status)
  44. f->eax = true;
  45. else
  46. f->eax = false;
  47. release_filesys_lock();
  48. break;
  49.  
  50. case SYS_OPEN:
  51. stack_args(f, &args[0], 1);
  52. acquire_file_lock();
  53. struct file* fptr = filesys_open(*args[0]);
  54. check_file(fptr);
  55. release_file_lock();
  56. break;
  57.  
  58. case SYS_CLOSE:
  59. stack_args(f, &args[0], 1);
  60. acquire_file_lock();
  61. close_all_thread_files(thread_current()->files);
  62. file_close(&args[0]);
  63. release_file_lock();
  64. break;
  65.  
  66. case SYS_SEEK:
  67. stack_args(f, &args[0], 2);
  68. acquire_file_lock();
  69. file_seek(list_search(&thread_current()->files, args[0]), args[1]);
  70. release_file_lock();
  71. break;
  72.  
  73. case SYS_TELL:
  74. stack_args(f, &args[0], 1);
  75. acquire_file_lock();
  76. f->eax = file_tell(list_search(&thread_current()->files, args[0]));
  77. release_file_lock();
  78. break;
  79.  
  80. case SYS_READ:
  81. stack_args(f, &args[0], 3);
  82. read(f, args);
  83. break;
  84.  
  85. case SYS_WRITE:
  86. stack_args(f, &args[0], 3);
  87. write(f, args);
  88. break;
  89.  
  90. case SYS_FILESIZE:
  91. stack_args(f, &args[0], 1);
  92. acquire_file_lock();
  93. f->eax = file_length(list_search(&thread_current()->files, args[0]));
  94. release_file_lock();
  95. break;
  96.  
  97. }
  98. }
  99.  
  100. void read(struct intr_frame *f, int *args) {
  101. if (*(args[0]) == 0) {
  102. int *buffer = *(args[1]);
  103. for (int i = 0; i < *(args[2]); i++)
  104. buffer[i] = input_getc();
  105. f->eax = *(args[2]);
  106. } else {
  107. struct file_struct* fptr = list_search(&thread_current()->files,
  108. *(args[0]));
  109. if (fptr == NULL)
  110. f->eax = -1;
  111. else {
  112. acquire_file_lock();
  113. f->eax = file_read(fptr->ptr, *(args[1]), *(args[2]));
  114. release_file_lock();
  115. }
  116. }
  117. }
  118.  
  119. void write(struct intr_frame *f, int *args) {
  120. if (*(args[0]) == 1) {
  121. putbuf(*(args[1]), *(args[2]));
  122. f->eax = *(args[2]);
  123. } else {
  124. struct proc_file* fptr = list_search(&thread_current()->files,
  125. *args[0]);
  126. if (fptr == NULL)
  127. f->eax = -1;
  128. else {
  129. acquire_filesys_lock();
  130. f->eax = file_write(fptr->ptr, *args[1], *args[2]);
  131. release_filesys_lock();
  132. }
  133. }
  134. }
  135.  
  136. struct file_struct* list_search(struct list* files, int fd) {
  137.  
  138. struct list_elem *e;
  139.  
  140. for (e = list_begin(files); e != list_end(files); e = list_next(e)) {
  141. struct file_struct *f = list_entry(e, struct file_struct, elem);
  142. if (f->fd == fd)
  143. return f;
  144. }
  145. return NULL;
  146. }
  147.  
  148. check_file(struct intr_frame *f, struct file* fptr) {
  149. if (fptr == NULL)
  150. f->eax = -1;
  151. else {
  152. struct file_struct *file = malloc(sizeof(*file));
  153. file->ptr = fptr;
  154. file->fd = thread_current()->fd_count;
  155. thread_current()->fd_count++;
  156. list_push_back(&thread_current()->files, &file->elem);
  157. f->eax = file->fd;
  158. }
  159. }
  160.  
  161. void stack_args(struct intr_frame *f, int *args, int num_of_args) {
  162. int * ptr = f->esp;
  163. for (int i = 1; i <= num_of_args; ++i) {
  164. vlid_address((const void *) (ptr + i));
  165. args[i] = *ptr;
  166. }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement