int waitpid(int pid, int *store) { return sys_wait(pid, store); }
void trap_kernel_handler(struct user_context *user_ctx) { _debug("...... in %s, code = %p\n", __func__, user_ctx->code & ~YALNIX_PREFIX); switch (user_ctx->code) { case YALNIX_FORK: SET_RET(user_ctx, sys_fork(user_ctx)); break; case YALNIX_EXEC: sys_exec((char *)user_ctx->regs[0], (char **)user_ctx->regs[1], user_ctx); break; case YALNIX_EXIT: sys_exit(user_ctx->regs[0], user_ctx); break; case YALNIX_WAIT: if (!FROM_USER_SPACE(user_ctx->regs[0])) { _error("Bad man! Please pass user space pointer!\n"); sys_exit(ERROR, user_ctx); } SET_RET(user_ctx, sys_wait((int *)user_ctx->regs[0], user_ctx)); break; case YALNIX_GETPID: SET_RET(user_ctx, sys_getpid()); break; case YALNIX_BRK: SET_RET(user_ctx, sys_brk(user_ctx->regs[0])); break; case YALNIX_DELAY: SET_RET(user_ctx, sys_delay(user_ctx->regs[0], user_ctx)); break; case YALNIX_TTY_READ: if (!FROM_USER_SPACE(user_ctx->regs[1])) { _error("Bad man! Please pass user space pointer!\n"); sys_exit(ERROR, user_ctx); } SET_RET(user_ctx, sys_tty_read(user_ctx->regs[0], (char *)user_ctx->regs[1], user_ctx->regs[2], user_ctx)); break; case YALNIX_TTY_WRITE: if (!FROM_USER_SPACE(user_ctx->regs[1])) { _error("Bad man! Please pass user space pointer!\n"); sys_exit(ERROR, user_ctx); } SET_RET(user_ctx, sys_tty_write(user_ctx->regs[0], (char *)user_ctx->regs[1], user_ctx->regs[2], user_ctx)); break; case YALNIX_PIPE_INIT: if (!FROM_USER_SPACE(user_ctx->regs[0])) { _error("Bad man! Please pass user space pointer!\n"); sys_exit(ERROR, user_ctx); } SET_RET(user_ctx, sys_pipe_init((unsigned int *) user_ctx->regs[0])); break; case YALNIX_PIPE_READ: if (!FROM_USER_SPACE(user_ctx->regs[1])) { _error("Bad man! Please pass user space pointer!\n"); sys_exit(ERROR, user_ctx); } SET_RET(user_ctx, sys_pipe_read(user_ctx->regs[0], (char *)user_ctx->regs[1], user_ctx->regs[2], user_ctx)); break; case YALNIX_PIPE_WRITE: if (!FROM_USER_SPACE(user_ctx->regs[1])) { _error("Bad man! Please pass user space pointer!\n"); sys_exit(ERROR, user_ctx); } SET_RET(user_ctx, sys_pipe_write(user_ctx->regs[0], (char *)user_ctx->regs[1], user_ctx->regs[2], user_ctx)); break; case YALNIX_LOCK_INIT: if (!FROM_USER_SPACE(user_ctx->regs[0])) { _error("Bad man! Please pass user space pointer!\n"); sys_exit(ERROR, user_ctx); } SET_RET(user_ctx, sys_lock_init((unsigned int *) user_ctx->regs[0])); break; case YALNIX_LOCK_ACQUIRE: SET_RET(user_ctx, sys_lock_acquire(user_ctx->regs[0], user_ctx)); break; case YALNIX_LOCK_RELEASE: SET_RET(user_ctx, sys_lock_release(user_ctx->regs[0])); break; case YALNIX_CVAR_INIT: if (!FROM_USER_SPACE(user_ctx->regs[0])) { _error("Bad man! Please pass user space pointer!\n"); sys_exit(ERROR, user_ctx); } SET_RET(user_ctx, sys_cvar_init((unsigned int *) user_ctx->regs[0])); break; case YALNIX_CVAR_WAIT: SET_RET(user_ctx, sys_cvar_wait(user_ctx->regs[0], user_ctx->regs[1], user_ctx)); break; case YALNIX_CVAR_SIGNAL: SET_RET(user_ctx, sys_cvar_signal(user_ctx->regs[0])); break; case YALNIX_CVAR_BROADCAST: SET_RET(user_ctx, sys_cvar_broadcast(user_ctx->regs[0])); break; case YALNIX_RECLAIM: SET_RET(user_ctx, sys_reclaim(user_ctx->regs[0])); break; case YALNIX_CUSTOM_0: SET_RET(user_ctx, sys_fork_share(user_ctx)); break; } return; }
int wait(void) { return sys_wait(0, NULL); }
static void syscall_handler (struct intr_frame *f) { int sys_vector; sys_vector=*(int *)(f->esp); switch(sys_vector) { case SYS_HALT: esp_under_phys_base(f, 0); sys_halt (); break; case SYS_EXIT: if ((void *)((int *)f->esp + 1) >= PHYS_BASE) sys_exit (-1); else sys_exit (*((int *)f->esp + 1)); break; case SYS_EXEC: esp_under_phys_base(f, 1); under_phys_base (*((int **)f->esp + 1)); sys_exec (*((int **)f->esp + 1), f); break; case SYS_WAIT: esp_under_phys_base(f, 1); sys_wait (*((int *)f->esp + 1), f); break; case SYS_CREATE: if ((void*)*((int **)f->esp + 1) == NULL) sys_exit(-1); esp_under_phys_base(f, 2); under_phys_base (*((int **)f->esp + 1)); sys_create (*((int **)f->esp + 1), *((int *)f->esp + 2), f); break; case SYS_REMOVE: esp_under_phys_base(f, 1); under_phys_base (*((int **)f->esp + 1)); sys_remove (*((int **)f->esp + 1), f); break; case SYS_OPEN: if ((void*)*((int **)f->esp + 1) == NULL) sys_exit(-1); esp_under_phys_base(f, 1); under_phys_base (*((int **)f->esp + 1)); sys_open (*((int **)f->esp + 1), f); break; case SYS_FILESIZE: esp_under_phys_base(f, 1); check_fd(*((int *)f->esp + 1), -1, f); sys_filesize (*((int *)f->esp + 1), f); break; case SYS_READ: esp_under_phys_base(f, 3); under_phys_base (*((int **)f->esp + 2)); check_fd(*((int *)f->esp + 1), -1, f) sys_read (*((int *)f->esp + 1), *((int **)f->esp + 2), *((int *)f->esp + 3), f); break; case SYS_WRITE: esp_under_phys_base(f, 3); under_phys_base (*((int **)f->esp + 2)); check_fd(*((int *)f->esp + 1), -1, f) sys_write (*((int *)f->esp + 1), *((int **)f->esp + 2), *((int *)f->esp + 3), f); break; case SYS_SEEK: esp_under_phys_base(f, 2); check_fd(*((int *)f->esp + 1), 0, f) sys_seek (*((int *)f->esp + 1), *((int *)f->esp + 2), f); break; case SYS_TELL: esp_under_phys_base(f, 1); check_fd(*((int *)f->esp + 1), 0, f) sys_tell (*((int *)f->esp + 1), f); break; case SYS_CLOSE: esp_under_phys_base(f, 1); check_fd(*((int *)f->esp + 1), 0, f) sys_close (*((int *)f->esp + 1), f); break; } }
unsigned long trapHandler(struct rtrapframe* tf, long cause, long epc, long badaddr)//long* tf { uint32_t sp1=read_sp(); uint32_t saved=tf; long funRe=0; long returnValue = 0; long sysNum=(long)(tf->a7); long args[5]; args[0]=(long)(tf->a0); args[1]=(long)(tf->a1); args[2]=(long)(tf->a2); args[3]=(long)(tf->a3); args[4]=(long)(tf->a4); struct rtrapframe *otf = current->tf; current->tf = tf; bool ifuser=tf->t0; funRe=((ifuser==1)?-1:0); switch(cause) { case CAUSE_MACHINE_ECALL: case CAUSE_SUPERVISOR_ECALL: { switch(sysNum) { case SYS_exit: { cprintf("exit\n"); sys_exit(args[0]); break; } case SYS_write: { returnValue=sys_write(sysNum,args[0],args[1],args[2]); current->tf=otf; tf->a0 = (uint32_t)returnValue; //asm volatile("csrw mepc, %0"::"r"(epc+4)); return funRe; break; } case SYS_exec: { const char *name = (const char *)args[0]; size_t len = (size_t)args[1]; returnValue = sys_execve(name, len); funRe=-1; break; } case SYS_S2M: { set_mstatus_field(MSTATUS_PRV1,3); break; } case SYS_S2U: { set_mstatus_field(MSTATUS_PRV1,0); break; } case SYS_getpid: { returnValue = sys_getpid(); break; } case SYS_fork: { returnValue=sys_fork(); //funRe=-1; // cprintf("hello\n"); break; } case SYS_yield: { //cprintf("yield\n"); returnValue=sys_yield(); break; } case SYS_wait: { returnValue=sys_wait(args[0],args[1]); break; } } break; } case CAUSE_USER_ECALL: { switch(sysNum) { case SYS_write: { returnValue = sys_write(sysNum, args[0], args[1], args[2]); current->tf=otf; tf->a0 = (uint32_t)returnValue; return funRe; break; } case SYS_getpid: { returnValue = sys_getpid(); break; } case SYS_exit: { returnValue = sys_exit(args[0]); break; } } break; } case CAUSE_FAULT_LOAD: case CAUSE_FAULT_STORE: case CAUSE_ILLEGAL_INSTRUCTION: { extern struct mm_struct *check_mm_struct; uint32_t mstatus=read_csr(mstatus); print_pgfault(mstatus,cause,badaddr); if (check_mm_struct != NULL) { if(do_pgfault(check_mm_struct, cause, badaddr, mstatus)!=0) panic("unhandled page fault in function.\n"); } else { if(current==NULL) panic("unhandled page fault.\n"); do_pgfault(current->mm, cause, badaddr, mstatus); } break; } default: { prvSyscallExit(cause); } } //uint32_t sp3=read_sp(); //cprintf("sysNum=%d\n",sysNum); //cprintf("sp3=%08x\n",sp3); current->tf=otf; tf->a0 = (uint32_t)returnValue; //asm volatile("csrw mepc, %0"::"r"(epc+4)); return funRe; }
/* Syscall handler calls the appropriate function. */ static void syscall_handler (struct intr_frame *f) { int ret = 0; int *syscall_nr = (int *) f->esp; void *arg1 = (int *) f->esp + 1; void *arg2 = (int *) f->esp + 2; void *arg3 = (int *) f->esp + 3; /* Check validate pointer. */ if (!is_user_vaddr (syscall_nr) || !is_user_vaddr (arg1) || !is_user_vaddr (arg2) || !is_user_vaddr (arg3)) sys_exit (-1); switch (*syscall_nr) { case SYS_HALT: sys_halt (); break; case SYS_EXIT: sys_exit (*(int *) arg1); break; case SYS_EXEC: ret = sys_exec (*(char **) arg1); break; case SYS_WAIT: ret = sys_wait (*(pid_t *) arg1); break; case SYS_CREATE: ret = sys_create (*(char **) arg1, *(unsigned *) arg2); break; case SYS_REMOVE: ret = sys_remove (*(char **) arg1); break; case SYS_OPEN: ret = sys_open (*(char **) arg1); break; case SYS_FILESIZE: ret = sys_filesize (*(int *) arg1); break; case SYS_READ: ret = sys_read (*(int *) arg1, *(void **) arg2, *(unsigned *) arg3); break; case SYS_WRITE: ret = sys_write (*(int *) arg1, *(void **) arg2, *(unsigned *) arg3); break; case SYS_SEEK: sys_seek (*(int *) arg1, *(unsigned *) arg2); break; case SYS_TELL: ret = sys_tell (*(int *) arg1); break; case SYS_CLOSE: sys_close (*(int *) arg1); break; default: printf (" (%s) system call! (%d)\n", thread_name (), *syscall_nr); sys_exit (-1); break; } f->eax = ret; }
static void syscall_handler (struct intr_frame *f) { /* Check to see that we can read supplied user memory pointer, using the check_mem_ptr helper() function, in get_word_from_stack(). If the check fails, the process is terminated. */ int syscall_number = (int)get_word_on_stack(f, 0); switch(syscall_number) { case SYS_HALT: { sys_halt(); break; } case SYS_EXIT: { int status = (int)get_word_on_stack(f, 1); sys_exit(status); /* Returns exit status to the kernel. */ f->eax = status; break; } case SYS_EXEC: { const char *cmd_line = (const char *)get_word_on_stack(f, 1); pid_t pid = sys_exec(cmd_line); /* Returns new processes pid. */ f->eax = pid; break; } case SYS_WAIT: { pid_t pid = (pid_t)get_word_on_stack(f, 1); /* Returns child's exit status (pid argument is pid of this child). */ f->eax = sys_wait(pid); break; } case SYS_CREATE: { const char *filename = (const char *)get_word_on_stack(f, 1); unsigned initial_size = (unsigned)get_word_on_stack(f, 2); /* Returns true to the kernel if creation is successful. */ f->eax = (int)sys_create(filename, initial_size); break; } case SYS_REMOVE: { const char *filename = (const char *)get_word_on_stack(f, 1); /* Returns true if successful, and false otherwise. */ f->eax = sys_remove(filename); break; } case SYS_OPEN: { const char *filename = (const char *)get_word_on_stack(f, 1); /* Returns file descriptor of opened file, or -1 if it could not be opened. */ f->eax = sys_open(filename); break; } case SYS_FILESIZE: { int fd = (int)get_word_on_stack(f, 1); /* Returns size of file in bytes. */ f->eax = sys_filesize(fd); break; } case SYS_READ: { int fd = (int)get_word_on_stack(f, 1); void *buffer = (void *)get_word_on_stack(f, 2); unsigned size = (unsigned)get_word_on_stack(f, 3); /* Returns number of bytes actually read, or -1 if it could not be read. */ f->eax = sys_read(fd, buffer, size, f); break; } case SYS_WRITE: { int fd = (int)get_word_on_stack(f, 1); void *buffer = (void *)get_word_on_stack(f, 2); unsigned size = (unsigned)get_word_on_stack(f, 3); /* Returns number of bytes written. */ f->eax = sys_write(fd, buffer, size); break; } case SYS_SEEK: { int fd = (int)get_word_on_stack(f, 1); unsigned position = (int)get_word_on_stack(f, 2); sys_seek(fd, position); break; } case SYS_TELL: { int fd = (int)get_word_on_stack(f, 1); /* Returns the position of the next byte to be read or written in open file 'fd' (in bytes, from start of file). */ f->eax = sys_tell(fd); break; } case SYS_CLOSE: { int fd = (int)get_word_on_stack(f, 1); sys_close(fd); break; } case SYS_MMAP: { int fd = (int)get_word_on_stack(f, 1); void *addr = (void *)get_word_on_stack(f, 2); f->eax = sys_mmap(fd, addr); break; } case SYS_MUNMAP: { mapid_t mapping = (mapid_t)get_word_on_stack(f, 1); sys_munmap(mapping); break; } default: { NOT_REACHED(); } } }
static void syscall_handler (struct intr_frame *f) { int *sys_call; int *status; int *fd; const char **buf; int *size; const char **file; tid_t *pid; const char **cmd_line; unsigned *initial_size; unsigned *position; void **buffer; sys_call = (int*) f->esp; check_ptr(sys_call); switch(*sys_call) { case SYS_HALT: sys_halt (); break; case SYS_EXIT: status = (int *) get_arg_ptr (f->esp, 1); sys_exit(*status); NOT_REACHED(); break; case SYS_EXEC: cmd_line = (const char **) get_arg_ptr (f->esp, 1); f->eax = sys_exec (*cmd_line); break; case SYS_WAIT: pid = (tid_t *) get_arg_ptr (f->esp, 1); f->eax = sys_wait (*pid); break; case SYS_CREATE: file = (const char **) get_arg_ptr (f->esp, 1); initial_size = (unsigned *) get_arg_ptr (f->esp, 2); f->eax = sys_create (*file, *initial_size); break; case SYS_REMOVE: file = (const char **) get_arg_ptr (f->esp, 1); f->eax = sys_remove (*file); break; case SYS_OPEN: file = (const char **) get_arg_ptr(f->esp, 1); f->eax = sys_open (*file); break; case SYS_FILESIZE: fd = (int *) get_arg_ptr (f->esp, 1); f->eax = sys_filesize (*fd); break; case SYS_READ: fd = (int *) get_arg_ptr (f->esp, 1); buffer = (void **) get_arg_ptr (f->esp, 2); size = (int *) get_arg_ptr (f->esp, 3); f->eax = sys_read (*fd, *buffer, *size); break; case SYS_WRITE: fd = (int *) get_arg_ptr (f->esp, 1); buf = (const void **) get_arg_ptr (f->esp, 2); size = (int *) get_arg_ptr (f->esp, 3); f->eax = sys_write (*fd, *buf, *size); break; case SYS_SEEK: fd = (int *) get_arg_ptr (f->esp, 1); position = (unsigned *) get_arg_ptr (f->esp, 2); sys_seek (*fd, *position); break; case SYS_TELL: fd = (int *) get_arg_ptr (f->esp, 1); f->eax = sys_tell (*fd); break; case SYS_CLOSE: fd = (int *) get_arg_ptr (f->esp, 1); sys_close (*fd); break; default: printf ("Unrecognized system call!\n"); thread_exit (); } }