This repository has been archived on 2021-05-26. You can view files and clone it, but cannot push or open issues or pull requests.
OS/pintos-env/pintos/userprog/syscall.c

89 lines
1.9 KiB
C
Raw Normal View History

#include "userprog/syscall.h"
#include <stdio.h>
#include <syscall-nr.h>
#include "threads/interrupt.h"
#include "threads/thread.h"
2020-04-28 13:35:26 +00:00
#include <string.h>
2020-05-18 12:57:52 +00:00
#include "userprog/process.h"
#include "threads/vaddr.h"
#include "userprog/pagedir.h"
2020-04-28 13:35:26 +00:00
typedef int (*handler) (uint32_t, uint32_t, uint32_t);
static handler syscall_vec[128];
static int sys_write (int fd, const void *buffer, unsigned length);
static int sys_exit (int status);
2020-05-18 12:57:52 +00:00
static int sys_wait (tid_t tid);
static tid_t sys_exec (const char* file_name);
2020-04-28 13:35:26 +00:00
static void syscall_nop(void);
static void syscall_handler (struct intr_frame *);
void
syscall_init (void)
{
intr_register_int (0x30, 3, INTR_ON, syscall_handler, "syscall");
2020-04-28 13:35:26 +00:00
memset(syscall_vec, (int)&syscall_nop, 128);
syscall_vec[SYS_EXIT] = (handler)sys_exit;
2020-05-18 12:57:52 +00:00
syscall_vec[SYS_WAIT] = (handler)sys_wait;
syscall_vec[SYS_EXEC] = (handler)sys_exec;
2020-04-28 13:35:26 +00:00
syscall_vec[SYS_WRITE] = (handler)sys_write;
}
static void
syscall_nop(void) {
printf("Syscall not implemented");
}
static void
2020-04-28 13:35:26 +00:00
syscall_handler (struct intr_frame *f)
{
2020-04-28 13:35:26 +00:00
handler h;
int *p;
int ret;
p = f->esp;
h = syscall_vec[*p];
ret = h (*(p + 1), *(p + 2), *(p + 3));
f->eax = ret;
}
static int
sys_write (int fd, const void *buffer, unsigned length) {
if (fd == 1) { // if stdout
putbuf (buffer, length);
return length;
} else {
return -1;
}
}
2020-04-28 13:35:26 +00:00
int
sys_exit (int status)
{
struct thread* t = thread_current();
printf("%s: exit(%d)\n", t->name, status);
2020-05-18 12:57:52 +00:00
t->exit_status = status;
2020-05-18 15:12:13 +00:00
thread_exit();
2020-04-28 13:35:26 +00:00
return -1;
2020-05-18 12:57:52 +00:00
}
int
sys_wait (tid_t tid)
{
printf("system call wait\n");
2020-05-18 15:12:13 +00:00
int status = process_wait(tid);
//printf("wait has returned status(%d)\n", status);
return status;
2020-05-18 12:57:52 +00:00
}
tid_t
sys_exec (const char* file_name) {
if (file_name == NULL || is_kernel_vaddr(file_name) ||
pagedir_get_page(thread_current()->pagedir, file_name) == NULL) {
return -1;
}
return process_execute(file_name);
2020-04-28 13:35:26 +00:00
}