Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.96 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 "threads/init.h"
  7. #include "filesys/filesys.h"
  8. #include "filesys/file.h"
  9. #include "devices/input.h"
  10. #include <stdbool.h>
  11.  
  12. static void syscall_handler (struct intr_frame *);
  13.  
  14. //from the lab1 description
  15. void sys_halt (void);
  16. bool sys_create (const char *file_n, unsigned initial_size);
  17. int sys_open (const char *file_n);
  18. void sys_close (int fd);
  19. int sys_read (int fd, void *buffer, unsigned size);
  20. int sys_write (int fd, const void *buffer, unsigned size);
  21. void sys_exit(int status);
  22.  
  23. void syscall_init (void)
  24. {
  25. intr_register_int (0x30, 3, INTR_ON, syscall_handler, "syscall");
  26. }
  27.  
  28. static void syscall_handler (struct intr_frame *f UNUSED){
  29.  
  30. int syscall_number = *((int*) f->esp); //Ändra till syscall nr
  31.  
  32. switch (syscall_number)
  33. {
  34. case SYS_HALT:
  35. {
  36. sys_halt();
  37. break;
  38. }
  39. case SYS_CREATE:
  40. {
  41. const char *fileName = *((char**)(f->esp + 4));
  42. unsigned new_size = *((unsigned*) (f->esp + 8));
  43. f->eax = sys_create(fileName, new_size);
  44. break;
  45. }
  46.  
  47. case SYS_OPEN:
  48. {
  49. const char *fileName = *((char**)(f->esp + 4));
  50. f->eax = sys_open(fileName);
  51. break;
  52. }
  53.  
  54. case SYS_CLOSE:
  55. {
  56. int file_desc = *((int*)(f->esp + 4));
  57. sys_close(file_desc);
  58. break;
  59. }
  60.  
  61. case SYS_READ:
  62. {
  63. int file_desc = *((int*)(f->esp + 4));
  64. void *readBuffer = *((void**)(f->esp + 8));
  65. unsigned size = *((unsigned*)(f->esp + 12));
  66. f->eax = sys_read(file_desc, readBuffer, size);
  67. break;
  68. }
  69.  
  70. case SYS_WRITE:
  71. {
  72. int file_desc = *((int*)(f->esp + 4));
  73. const void *writeBuffer = *((void**)(f->esp + 8));
  74. unsigned size = *((unsigned*)(f->esp + 12));
  75. f->eax = sys_write(file_desc, writeBuffer, size);
  76. break;
  77. }
  78. case SYS_EXIT:
  79. {
  80. sys_exit(0); //Ändra till exit 0.
  81. break;
  82. }
  83. }
  84. //printf ("system call!\n");
  85. // thread_exit ();
  86. }
  87.  
  88.  
  89.  
  90. void sys_halt(void){
  91. power_off();
  92. }
  93.  
  94. bool sys_create (const char *file_n, unsigned initial_size){
  95. return filesys_create(file_n, initial_size);
  96. }
  97.  
  98. int sys_open (const char *file_n){
  99. struct file *file_open = filesys_open(file_n);
  100.  
  101. if(file_open == NULL){ //checks if filesys_open failed and if it did, return -1.
  102. return -1;
  103. }
  104.  
  105. struct thread *current_thread = thread_current();
  106.  
  107. int iterator = 2;
  108. while(iterator < 130){ //Here we iterate through the list of files
  109.  
  110. if(current_thread->list_of_files[iterator] == NULL){ //checks if the current threads file list is equal to NULL at the index equal to the integer value of "iterator"
  111. current_thread->list_of_files[iterator] = file_open;
  112. return iterator; //returns the index the file was opened on
  113. }
  114. ++iterator; //increments the iterator
  115. }
  116. return -1; // returns -1 to indicate that the chosen file couldn't be opened
  117. }
  118.  
  119.  
  120. void sys_close (int file_desc){
  121.  
  122. struct thread *current_thread = thread_current();
  123. struct file *file_to_close = current_thread->list_of_files[file_desc];
  124. current_thread->list_of_files[file_desc] = NULL; //empties the file list
  125. file_close(file_to_close); //Uses file.h and it's file_close method with the file on the index of file_desc
  126. }
  127.  
  128.  
  129. int sys_read (int fd, void *buffer, unsigned size)//kommentera s
  130. {
  131. if(fd < 0 || fd >=128) {
  132. return -1;
  133. }
  134. else if(fd == 1) {
  135. return -1;
  136. }
  137.  
  138. // reads size number of chars into buffer from console
  139. else if(fd == 0) {
  140. for(unsigned i = 0; i < size; i++){
  141. ((uint8_t*)buffer)[i] = input_getc();
  142. }
  143. return size;
  144. }
  145. struct thread *current_thread = thread_current();
  146. struct file *file = current_thread->list_of_files[fd];
  147. return file_read(file, buffer, size);
  148. }
  149.  
  150. int sys_write (int fd, const void *buffer, unsigned size)//kommenter s
  151. {
  152. struct thread *current_thread = thread_current();
  153. struct file *file_name = current_thread->list_of_files[fd];
  154.  
  155. if (file_name != NULL) {
  156. return -1;
  157. }
  158.  
  159. else if(fd < 0 || fd >=128) {
  160. return -1;
  161. }
  162. // writes to console
  163. else if (fd == 1) {
  164. putbuf((char*)buffer, size);
  165. return size;
  166. }
  167.  
  168. return file_write(file, buffer, size);
  169. }
  170.  
  171. void sys_exit(int status){
  172. thread_exit();
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement