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"
- //my
- #include "threads/vaddr.h"
- #include "lib/kernel/list.h"
- #include "lib/string.h"
- #include "process.h"
- #include "threads/synch.h"
- #include "userprog/pagedir.h"
- #include "filesys/filesys.h"
- #include "filesys/file.h"
- #include "lib/string.h"
- #include "lib/kernel/stdio.h"
- #include "devices/input.h"
- #define MAX_OPEN_FILES 128
- static struct file* open_files[MAX_OPEN_FILES];
- static int open_files_cnt;
- static struct lock file_lock;
- //end my
- static void syscall_handler (struct intr_frame*);
- bool bad_ptr(void* ptr);
- bool is_open(struct file* file);
- void sys_exit(int exit_code);
- struct file* find_file_by_fd(int fd);
- void SHOW_OPEN_FILES()
- {
- printf("Open files: ");
- for (int i = 0; i < open_files_cnt; ++i) {
- printf("%x ", open_files[i]);
- }
- printf("\n");
- }
- void
- syscall_init (void)
- {
- intr_register_int (0x30, 3, INTR_ON, syscall_handler, "syscall");
- //my
- lock_init(&file_lock);
- for (int i = 0; i < MAX_OPEN_FILES; ++i) {
- open_files[i] = NULL;
- }
- open_files_cnt = 2;
- //end my
- }
- static void
- syscall_handler (struct intr_frame *f)
- {
- //my
- //printf("syscall_handler --> ");
- void* esp = f->esp;
- int syscall_number = *(int*)esp;
- if (syscall_number == SYS_EXIT) {
- int exit_status = *(int*)(esp + 4);
- sys_exit(exit_status);
- }
- else if (syscall_number == SYS_EXEC) {
- //printf("SYS_EXEC\n");
- char* cmd = *(char**)(esp + 4);
- if (bad_ptr(cmd)) {
- f->eax = -1;
- return;
- }
- lock_acquire(&file_lock);
- f->eax = process_execute(cmd);
- lock_release(&file_lock);
- return;
- }
- else if (syscall_number == SYS_WAIT) {
- //printf("SYS_WAIT\n");
- tid_t tid = *(int*)(esp + 4);
- f->eax = process_wait(tid);
- return;
- }
- else if (syscall_number == SYS_CREATE) {
- char* file_name = *(char**)(esp + 4);
- uint32_t size = *(uint32_t*)(esp + 8);
- //printf("sys_create, file_name=%s, size=%d\n", file_name, size);
- if (bad_ptr(file_name)) {
- sys_exit(-1);
- }
- f->eax = filesys_create (file_name, size);
- return;
- }
- else if (syscall_number == SYS_OPEN) {
- char* file_name = *(char**)(esp + 4);
- struct file* new_file;
- if (bad_ptr(file_name)) {
- sys_exit(-1);
- }
- if (file_name[0] == '\0') {
- f->eax = -1;
- return;
- }
- new_file = filesys_open(file_name);
- if (new_file == NULL) {
- f->eax = -1;
- return;
- }
- if (!is_open(new_file)) {
- open_files[open_files_cnt] = new_file;
- open_files_cnt++;
- }
- f->eax = open_files_cnt - 1;
- return;
- }
- else if (syscall_number == SYS_CLOSE) {
- int fd = *(int*)(esp + 4);
- if (fd == 1) {
- sys_exit(-1);
- }
- struct file* file = find_file_by_fd(fd);
- if (file == NULL) {
- sys_exit(-1);
- }
- file_close(file);
- open_files[fd] = NULL;
- return;
- }
- else if (syscall_number == SYS_FILESIZE) {
- int fd = *(int*)(esp + 4);
- //printf("SYS file size, fd=%d\n", fd);
- struct file* file = find_file_by_fd(fd);
- if (file == NULL) {
- sys_exit(-1);
- }
- f->eax = file_length(file);
- return;
- }
- else if (syscall_number == SYS_READ) {
- int fd = *(int*)(esp + 4);
- void* buffer = *(void**)(esp + 8);
- unsigned int size = *(unsigned int*)(esp + 12);
- //printf("SYS_read, fd=%d, buf=%x, size=%d\n", fd, buffer, size);
- if (bad_ptr(buffer) || fd == 1) {
- sys_exit(-1);
- }
- if (fd == 0) {
- input_getc();
- return;
- }
- struct file* file_to_write;
- file_to_write = find_file_by_fd(fd);
- if (file_to_write == NULL) {
- sys_exit(-1);
- }
- lock_acquire(&file_lock);
- f->eax = file_read(file_to_write, buffer, size);
- lock_release(&file_lock);
- return;
- }
- else if (syscall_number == SYS_WRITE) {
- int fd = *(int*)(esp + 4);
- void* buffer = *(void**)(esp + 8);
- unsigned int size = *(unsigned int*)(esp + 12);
- //printf("SYS_WRITE, fd=%d, buf=%x, size=%d\n", fd, buffer, size);
- if (bad_ptr(buffer) || fd == 0) {
- sys_exit(-1);
- }
- if (fd == 1) {
- putbuf(buffer, size);
- return;
- }
- struct file* file_to_write;
- file_to_write = find_file_by_fd(fd);
- if (file_to_write == NULL) {
- sys_exit(-1);
- }
- lock_acquire(&file_lock);
- f->eax = file_write(file_to_write, buffer, size);
- lock_release(&file_lock);
- return;
- }
- //end my
- printf ("system call %d!\n", syscall_number);
- thread_exit ();
- }
- bool bad_ptr (void* ptr)
- {
- return (ptr == NULL) || (!is_user_vaddr (ptr)) || (pagedir_get_page(thread_current()->pagedir, ptr) == NULL);
- }
- bool is_open(struct file* file)
- {
- for (int i = 0; i < open_files_cnt; ++i) {
- if (open_files[i] == file) {
- return true;
- }
- }
- return false;
- }
- void sys_exit(int exit_code)
- {
- thread_current()->exit_status = exit_code;
- thread_exit();
- }
- struct file* find_file_by_fd(int fd)
- {
- if (fd >= MAX_OPEN_FILES) {
- return NULL;
- }
- return open_files[fd];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement