Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "userprog/syscall.h"
- #include <stdio.h>
- #include <syscall-nr.h>
- #include "threads/interrupt.h"
- #include "threads/thread.h"
- #include "threads/init.h"
- #include "filesys/filesys.h"
- #include "filesys/file.h"
- #include "devices/input.h"
- #include <stdbool.h>
- static void syscall_handler (struct intr_frame *);
- //from the lab1 description
- void sys_halt (void);
- bool sys_create (const char *file_n, unsigned initial_size);
- int sys_open (const char *file_n);
- void sys_close (int fd);
- int sys_read (int fd, void *buffer, unsigned size);
- int sys_write (int fd, const void *buffer, unsigned size);
- void sys_exit(int status);
- void syscall_init (void)
- {
- intr_register_int (0x30, 3, INTR_ON, syscall_handler, "syscall");
- }
- static void syscall_handler (struct intr_frame *f UNUSED){
- int syscall_number = *((int*) f->esp); //Ändra till syscall nr
- switch (syscall_number)
- {
- case SYS_HALT:
- {
- sys_halt();
- break;
- }
- case SYS_CREATE:
- {
- const char *fileName = *((char**)(f->esp + 4));
- unsigned new_size = *((unsigned*) (f->esp + 8));
- f->eax = sys_create(fileName, new_size);
- break;
- }
- case SYS_OPEN:
- {
- const char *fileName = *((char**)(f->esp + 4));
- f->eax = sys_open(fileName);
- break;
- }
- case SYS_CLOSE:
- {
- int file_desc = *((int*)(f->esp + 4));
- sys_close(file_desc);
- break;
- }
- case SYS_READ:
- {
- int file_desc = *((int*)(f->esp + 4));
- void *readBuffer = *((void**)(f->esp + 8));
- unsigned size = *((unsigned*)(f->esp + 12));
- f->eax = sys_read(file_desc, readBuffer, size);
- break;
- }
- case SYS_WRITE:
- {
- int file_desc = *((int*)(f->esp + 4));
- const void *writeBuffer = *((void**)(f->esp + 8));
- unsigned size = *((unsigned*)(f->esp + 12));
- f->eax = sys_write(file_desc, writeBuffer, size);
- break;
- }
- case SYS_EXIT:
- {
- sys_exit(0); //Ändra till exit 0.
- break;
- }
- }
- //printf ("system call!\n");
- // thread_exit ();
- }
- void sys_halt(void){
- power_off();
- }
- bool sys_create (const char *file_n, unsigned initial_size){
- return filesys_create(file_n, initial_size);
- }
- int sys_open (const char *file_n){
- struct file *file_open = filesys_open(file_n);
- if(file_open == NULL){ //checks if filesys_open failed and if it did, return -1.
- return -1;
- }
- struct thread *current_thread = thread_current();
- int iterator = 2;
- while(iterator < 130){ //Here we iterate through the list of files
- 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"
- current_thread->list_of_files[iterator] = file_open;
- return iterator; //returns the index the file was opened on
- }
- ++iterator; //increments the iterator
- }
- return -1; // returns -1 to indicate that the chosen file couldn't be opened
- }
- void sys_close (int file_desc){
- struct thread *current_thread = thread_current();
- struct file *file_to_close = current_thread->list_of_files[file_desc];
- current_thread->list_of_files[file_desc] = NULL; //empties the file list
- file_close(file_to_close); //Uses file.h and it's file_close method with the file on the index of file_desc
- }
- int sys_read (int fd, void *buffer, unsigned size)//kommentera s
- {
- if(fd < 0 || fd >=128) {
- return -1;
- }
- else if(fd == 1) {
- return -1;
- }
- // reads size number of chars into buffer from console
- else if(fd == 0) {
- for(unsigned i = 0; i < size; i++){
- ((uint8_t*)buffer)[i] = input_getc();
- }
- return size;
- }
- struct thread *current_thread = thread_current();
- struct file *file = current_thread->list_of_files[fd];
- return file_read(file, buffer, size);
- }
- int sys_write (int fd, const void *buffer, unsigned size)//kommenter s
- {
- struct thread *current_thread = thread_current();
- struct file *file_name = current_thread->list_of_files[fd];
- if (file_name != NULL) {
- return -1;
- }
- else if(fd < 0 || fd >=128) {
- return -1;
- }
- // writes to console
- else if (fd == 1) {
- putbuf((char*)buffer, size);
- return size;
- }
- return file_write(file, buffer, size);
- }
- void sys_exit(int status){
- thread_exit();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement