Advertisement
Guest User

Untitled

a guest
Jun 25th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 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/vaddr.h"
  7. #include "filesys/file.h"
  8.  
  9. struct file_des{
  10. struct list_elem elem;
  11. int fd;
  12. struct file *fd_file;
  13. };
  14.  
  15.  
  16. void shutdown_power_off(void);
  17. uint8_t input_getc (void);
  18. struct file *filesys_open (const char *name);
  19.  
  20. struct list fd_list;
  21.  
  22. static bool
  23. is_in(int x){
  24. struct list_elem *e;
  25. for (e = list_begin (&fd_list); e != list_end (&fd_list);
  26. e = list_next (e))
  27. {
  28. struct file_des *f = list_entry (e, struct file_des, elem);
  29. if (f->fd == x) {
  30. return true;
  31. }else{
  32. return false;
  33. }
  34.  
  35. }
  36. return false;
  37. }
  38.  
  39. static struct file_des *
  40. find_fd(uint8_t fd){
  41. struct list_elem *e;
  42. for(e = list_begin (&fd_list); e != list_end (&fd_list); e = list_next(e)){
  43. struct file_des *f = list_entry (e, struct file_des, elem);
  44. if (f->fd == fd){
  45. return f;
  46. }else{
  47. return NULL;
  48. }
  49. }
  50. return NULL;
  51. }
  52.  
  53. /* Reads a byte at user virtual address UADDR.
  54. UADDR must be below PHYS_BASE.
  55. Returns the byte value if successful, -1 if a segfault
  56. occurred. */
  57. static int
  58. get_user (const uint8_t *uaddr)
  59. {
  60. static int result;
  61. asm ("movl $1f, %0; movzbl %1, %0; 1:"
  62. : "=&a" (result) : "m" (*uaddr));
  63. return result;
  64. }
  65.  
  66. /* Writes BYTE to user address UDST.
  67. UDST must be below PHYS_BASE.
  68. Returns true if successful, false if a segfault occurred. */
  69. static bool
  70. put_user (uint8_t *udst, uint8_t byte)
  71. {
  72. int error_code;
  73. asm ("movl $1f, %0; movb %b2, %1; 1:"
  74. : "=&a" (error_code), "=m" (*udst) : "q" (byte));
  75. return error_code != -1;
  76. }
  77.  
  78. static void syscall_handler (struct intr_frame *);
  79.  
  80. void
  81. syscall_init (void)
  82. {
  83. intr_register_int (0x30, 3, INTR_ON, syscall_handler, "syscall");
  84. //list_init(&fd_list);
  85. printf("saldfjlsdf\n");
  86. }
  87.  
  88. static void
  89. syscall_handler (struct intr_frame *f UNUSED)
  90. {
  91. printf("sys init\n");
  92.  
  93. uint8_t *stack_ptr = (uint8_t *) f->esp;
  94. //retreive return pointer
  95. uint8_t *return_val = (uint8_t *) f->eax;
  96. //retreive sys call number
  97. int sys_num = -1;
  98.  
  99. if ((uint32_t)stack_ptr < (uint32_t)PHYS_BASE){
  100. printf("successful stack_ptr\n");
  101. sys_num = get_user(stack_ptr);
  102. }else{
  103. printf("Invalid User Address\n");
  104. thread_exit();
  105. }
  106. printf("Pass initial life\n");
  107. if (sys_num == SYS_EXIT){
  108. printf("Thread suceesfully exit\n");
  109. thread_exit();
  110. }
  111. printf("Bye Bye\n");
  112. thread_exit();
  113.  
  114. // if (sys_num == SYS_READ){
  115. // stack_ptr--;
  116. // int fd = get_user(stack_ptr);
  117. // stack_ptr--;
  118. // char *buf = (char *) get_user(stack_ptr);
  119. // stack_ptr--;
  120. // unsigned int size = get_user(stack_ptr);
  121.  
  122. // if (fd == 0){
  123. // for (unsigned int i = 0; i < size; i++){
  124. // buf[i] = input_getc();
  125. // }
  126. // put_user(return_val, size);
  127.  
  128. // }else{
  129. // struct file_des *get_fd = find_fd(fd);
  130. // unsigned int size_c = 0;
  131. // while(size_c <= size){
  132. // size_c+=file_read(get_fd->fd_file, buf, size_c);
  133. // }
  134. // }
  135.  
  136. // }
  137.  
  138. // if (sys_num == SYS_OPEN){
  139. // stack_ptr--;
  140. // char* file_name = (char *) get_user(stack_ptr);
  141. // int finding_fd = 2;
  142.  
  143. // while (is_in(finding_fd)) {
  144. // finding_fd++;
  145. // }
  146.  
  147. // struct file_des adding_file;
  148. // adding_file.fd = finding_fd;
  149. // adding_file.fd_file = (struct file*)filesys_open(file_name);
  150. // list_insert(list_end(&fd_list),&adding_file.elem);
  151. // put_user(return_val, finding_fd);
  152. // }
  153.  
  154. // if (sys_num == SYS_WRITE){
  155. // stack_ptr--;
  156. // int fd = get_user(stack_ptr);
  157. // stack_ptr--;
  158. // char *buf = (char *) get_user(stack_ptr);
  159. // stack_ptr--;
  160. // unsigned int size = get_user(stack_ptr);
  161. // //find file descriptor in the list
  162. // if (fd == 1){
  163. // putbuf(buf, size);
  164. // put_user(return_val, size);
  165. // }else{
  166. // struct file_des *get_fd = find_fd(fd);
  167. // unsigned int size_c = 0;
  168. // while(size_c <= size){
  169. // size_c+=file_write(get_fd->fd_file, buf, size_c);
  170. // }
  171. // put_user(return_val, size_c);
  172. // }
  173.  
  174. // }
  175.  
  176. // if (sys_num == SYS_CLOSE){
  177.  
  178. // }
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement