Merge basic system calls with stack set-up infrastructure #27

Merged
td1223 merged 31 commits from user-programs into user-programs-stdout 2024-11-06 22:21:28 +00:00
2 changed files with 71 additions and 125 deletions
Showing only changes of commit d626b7a392 - Show all commits

View File

@@ -17,6 +17,8 @@ typedef uintptr_t (*syscall_function) (uintptr_t, uintptr_t, uintptr_t);
static void halt (void);
static void exit (int status);
static void *validate_user_pointer(void *ptr, size_t size);
/* A struct defining a pair of a syscall_function along with its arity. */
typedef struct {
syscall_function function;
@@ -29,6 +31,9 @@ static const syscall_arguments syscall_lookup[] = {
[SYS_EXIT] = {(syscall_function) exit, 1},
};
static const int lookup_size
= sizeof (syscall_lookup) / sizeof (syscall_arguments);
void
syscall_init (void)
{
@@ -36,10 +41,23 @@ syscall_init (void)
}
static void
syscall_handler (struct intr_frame *f UNUSED)
syscall_handler (struct intr_frame *f)
{
printf ("system call!\n");
validate_user_pointer(f->esp, 1);
int syscall_number = *(int *) f->esp;
if (syscall_number < 0 || syscall_number >= lookup_size)
thread_exit ();
syscall_arguments syscall = syscall_lookup[syscall_number];
validate_user_pointer (f->esp, syscall.arity);
uintptr_t args[3];
for (int i=0; i < syscall.arity; i++)
args[i] = *(uintptr_t *) (f->esp + 1 + i);
f->eax = syscall.function(args[0], args[1], args[2]);
}
static void