/*! Shuts down the machine in the way configured by shutdown_configure(). If the shutdown type is SHUTDOWN_NONE (which is the default), returns without doing anything. */ void shutdown(void) { switch (how) { case SHUTDOWN_POWER_OFF: shutdown_power_off(); break; case SHUTDOWN_REBOOT: shutdown_reboot(); break; default: /* Nothing to do. */ break; } }
/* * System Call: void halt (void) * Terminates Pintos by calling shutdown_power_off() * (declared in devices/shutdown.h). This should be seldom used, * because you lose some information about possible deadlock * situations, etc. */ void halt_handler(void) { shutdown_power_off(); }
static void my_halt(void) { shutdown_power_off(); }
/* exit pintos */ void halt(void) { shutdown_power_off(); }
/* Halt system call. */ static int sys_halt (void) { shutdown_power_off (); }
void sys_halt(void) { shutdown_power_off(); }
static void syscall_handler (struct intr_frame *f) { uint32_t *p = f->esp; check_ptr(p); switch (*(int *)p) { /* Halt the operating system. IN : void OUT: void */ case SYS_HALT: { shutdown_power_off(); break; } /* Terminate this process. IN : int status OUT: void */ case SYS_EXIT: { check_ptr(p + 1); int status = *(int *)(p + 1); exit(status); break; } /* Start another process. IN : const char *file OUT: pid_t */ case SYS_EXEC: { check_string(p + 1); // f->eax = process_execute(*(char **)(p + 1)); pid_t pid = process_execute(*(char **)(p + 1)); struct child_process *cp = get_child_process(pid); if(cp->load == LOAD_SUCCESS) f->eax = pid; else if(cp->load == LOAD_FAIL) f->eax = -1; break; } /* Wait for a child process to die. IN : pid_t OUT: int */ case SYS_WAIT: { check_ptr(p + 1); f->eax = process_wait(*(tid_t *)(p + 1)); break; } /* Create a file. IN :const char *file, unsigned initial_size OUT:bool */ case SYS_CREATE: { check_string(p + 1); check_ptr(p + 2); if(*(char**)(p+1)==NULL||*(int *)(p+1)>=PHYS_BASE|| pagedir_get_page(thread_current()->pagedir, (const void *)*(p+1)) == NULL) exit(-1); f->eax = filesys_create(*(const char **)(p + 1), *(off_t *)(p + 2)); break; } /* Delete a file. IN: const char *file OUT: bool */ case SYS_REMOVE: { check_string(p + 1); if(*(char**)(p+1)==NULL||*(int *)(p+1)>=PHYS_BASE|| pagedir_get_page(thread_current()->pagedir, (const void *)*(p+1)) == NULL) exit(-1); f->eax = filesys_remove(*(char**)(p + 1)); break; } /* Open a file. IN: const char *file OUT: int */ case SYS_OPEN: { check_string(p + 1); struct file *file = filesys_open(*(char**)(p + 1)); if (file == NULL) { f->eax = -1; break; } struct file_desc *desc = malloc(sizeof(struct file_desc)); desc->file = file; desc->fd = thread_current()->fd; thread_current()->fd++; list_push_back(&thread_current()->file_list, &desc->elem); f->eax = desc->fd; break; } /* Obtain a file's size. IN: int fd OUT: int */ case SYS_FILESIZE: { check_ptr(p + 1); int fd = *(p + 1); struct file *file = process_get_file(fd); if (file == NULL) { f->eax = FILE_ERROR; } else { f->eax = file_length(file); } break; } /* Read from a file. IN: int fd, void *buffer, unsigned length OUT: int */ case SYS_READ: { check_ptr(p + 1); check_ptr(p + 2); check_ptr(p + 3); check_buffer(p + 2, *(p + 3)); int fd = *(int *)(p + 1); void *buffer = *(char**)(p + 2); unsigned length = *(unsigned *)(p + 3); //read from keyboard. Fd 0 reads from keyboard using input_getc(): one each time. if (fd == STDIN_FILENO) { uint8_t *into_buffer = (uint8_t *) buffer; unsigned i; for (i = 0; i < length; i++) { into_buffer[i] = input_getc(); } f->eax = length; } else { //read from file into buffer struct file *file = process_get_file(fd); //return -1 if file couldn't be read. if (file == NULL) { f->eax = FILE_ERROR; //-1 } else { int bytes = file_read(file, buffer, length); f->eax = bytes; } } break; } /* Write to a file. IN: int fd, const void *buffer, unsigned length OUT: int */ case SYS_WRITE: { check_ptr(p + 1); check_ptr(p + 2); check_ptr(p + 3); check_buffer(p + 2, *(p + 3)); int fd = *(int *)(p + 1); void *buffer = *(char**)(p + 2); unsigned length = *(off_t *)(p + 3); if (length <= 0) { f->eax = 0; break; } //Fd=1(STDOUT_FILENO) writes to the console. if (fd == STDOUT_FILENO) { putbuf(buffer, length); f->eax = length; } else { // return file by file descriptor. struct file *file = process_get_file(fd); if (file == NULL) { f->eax = FILE_ERROR; //-1 } else { int bytes = file_write(file, buffer, length); f->eax = bytes; } } break; } /* Change position in a file. IN: int fd, unsigned position OUT: void */ case SYS_SEEK: { check_ptr(p + 1); check_ptr(p + 2); int fd = *(int *)(p+1); unsigned position = *(unsigned *)(p+2); struct file *file = process_get_file(fd); if(file != NULL) { file_seek(file, position); } break; } /* Report current position in a file. IN: int fd OUT: unsigned */ case SYS_TELL: { check_ptr(p + 1); int fd = *(int *)(p+1); struct file *file = process_get_file(fd); if(file != NULL) { f->eax = file_tell(file); } else f->eax = FILE_ERROR; //-1 break; } /* Close a file. IN: int fd OUT: void */ case SYS_CLOSE: { check_ptr(p+1); int fd = *(int*)(p+1); struct thread *t = thread_current(); struct list_elem *next, *e = list_begin(&t->file_list); while (e != list_end (&t->file_list)) { next = list_next(e); struct file_desc *fl = list_entry (e, struct file_desc, elem); if (fd == fl->fd) { file_close(fl->file); list_remove(&fl->elem); free(fl); break; } e = next; } break; } default: break; } }
static void syscall_handler (struct intr_frame* frame) { // -------- System Call Handler Overview -------- // Get system call number // switch statement using system call number // collect arguments for system call function if necessary // call system call function // set frame->eax to return value if necessary // ---------------------------------------------- uintptr_t* kpaddr_sp = (uintptr_t*) frame->esp; int syscall_num = -1; if(check_uptr(kpaddr_sp)) syscall_num = next_value(&kpaddr_sp); else sysexit(-1); switch(syscall_num) { case SYS_HALT: { // Terminates Pintos shutdown_power_off(); } break; case SYS_EXIT: { uintptr_t status = -1; if(check_uptr(kpaddr_sp)) status = next_value(&kpaddr_sp); sysexit(status); } break; case SYS_EXEC: //pid_t exec (const char *file); { const char* file = next_charptr(&kpaddr_sp); if(file == NULL) sysexit(-1); unsigned len = strlen(file); if(!check_buffer(file, len)) sysexit(-1); else sysexec(frame, file); } break; case SYS_WAIT: //int wait (pid_t); { uintptr_t childid = -1; if(check_uptr(kpaddr_sp)) childid = next_value(&kpaddr_sp); else sysexit(childid); int retval = process_wait((tid_t) childid); frame->eax = retval; } break; case SYS_CREATE: //bool create (const char *file, unsigned initial_size); { const char* file = next_charptr(&kpaddr_sp); if(file == NULL) sysexit(-1); unsigned len = strlen(file); if(!check_buffer(file, len)) sysexit(-1); uintptr_t size = 0; if(check_uptr(kpaddr_sp)) size = next_value(&kpaddr_sp); else sysexit(-1); syscreate(frame, file, size); } break; case SYS_REMOVE: //bool remove (const char *file); { const char* file = next_charptr(&kpaddr_sp); if(file == NULL) sysexit(-1); unsigned len = strlen(file); if(!check_buffer(file, len)) sysexit(-1); sysremove(frame, file); } break; case SYS_OPEN: { //int open (const char *file); const char* file = next_charptr(&kpaddr_sp); if(file == NULL) sysexit(-1); unsigned len = strlen(file); if(!check_buffer(file, len)) sysexit(-1); sysopen(frame, file); } break; case SYS_FILESIZE: { //int filesize (int fd); int fd = 0; if (check_uptr(kpaddr_sp)) fd = (int) next_value(&kpaddr_sp); else sysexit(-1); sysfilesize(frame, fd); } break; case SYS_READ: { //int read (int fd, void *buffer, unsigned length); int fd = 0; if (check_uptr(kpaddr_sp)) fd = (int) next_value(&kpaddr_sp); else sysexit(-1); const char* file = next_charptr(&kpaddr_sp); if(file == NULL) sysexit(-1); unsigned len = strlen(file); if(!check_buffer(file, len)) sysexit(-1); unsigned length = 0; if (check_uptr(kpaddr_sp)) length = (unsigned) next_value(&kpaddr_sp); else sysexit(-1); sysread(frame, fd, (void*) file, length); } break; case SYS_WRITE: { //int write (int fd, const void *buffer, unsigned length); uintptr_t fd = 0; if(check_uptr(kpaddr_sp)) fd = next_value(&kpaddr_sp); else sysexit(-1); const char* file = next_charptr(&kpaddr_sp); if(file == NULL) sysexit(-1); unsigned len = strlen(file); if(!check_buffer(file, len)) sysexit(-1); uintptr_t length = 0; if(check_uptr(kpaddr_sp)) length = next_value(&kpaddr_sp); else sysexit(-1); if(fd == CONSOLEWRITE) // Write to Console { while(length > 0) { if(length > MAX_SIZE) { putbuf (file, MAX_SIZE); file += MAX_SIZE; length -= MAX_SIZE; } else { putbuf (file, length); length = 0; } } } else { syswrite(frame, fd, file, length); } } break; case SYS_SEEK: { //void seek (int fd, unsigned position); int fd = 0; if (check_uptr(kpaddr_sp)) fd = (int) next_value(&kpaddr_sp); else sysexit(-1); unsigned position = 0; if (check_uptr(kpaddr_sp)) position = (unsigned) next_value(&kpaddr_sp); else sysexit(-1); sysseek(fd, position); } break; case SYS_TELL: { //unsigned tell (int fd); int fd = 0; if (check_uptr(kpaddr_sp)) fd = (int) next_value(&kpaddr_sp); else sysexit(-1); systell(frame, fd); } break; case SYS_CLOSE: { //void close (int fd); int fd = 0; if (check_uptr(kpaddr_sp)) fd = (int) next_value(&kpaddr_sp); else sysexit(-1); sysclose(fd); } break; default: { printf("Unrecognized System Call\n"); sysexit(-1); } break; } }
/* system power off */ void halt(void) { printf("system halt\n"); shutdown_power_off(); }
static void syscall_halt () { shutdown_power_off (); }
static void syscall_handler (struct intr_frame *f) { int nsyscall, i; int *esp = (int *)f -> esp; if(!is_valid(esp)) thread_exit(); int *arg_int[3]; void **arg_ptr[3]; // Get system call number. nsyscall = *(esp++); /* argc number 0: SYS_HALT 1: SYS_EXIT, SYS_EXEC, SYS_WAIT, SYS_TELL, SYS_REMOVE, SYS_OPEN, SYS_CLOSE, SYS_FILESIZE 2: SYS_CREATE, SYS_SEEK 3: SYS_READ, SYS_WRITE */ if(nsyscall == SYS_HALT) { shutdown_power_off(); } else if(nsyscall == SYS_EXIT) { for(i = 0; i < 1; i++) { if(is_valid((esp+i))) { arg_int[i] = esp + i; } else thread_exit(); } syscall_exit(*arg_int[0]); } else if(nsyscall == SYS_EXEC) { for(i = 0; i < 1; i++) { if(is_valid((esp+i))) { arg_ptr[i] = (void **)(esp + i); } else thread_exit(); } if(!is_valid(*arg_ptr[0])) thread_exit(); lock_acquire(&lock_sys); f->eax = process_execute(*arg_ptr[0]); lock_release(&lock_sys); } else if(nsyscall == SYS_WAIT) { for(i = 0; i < 1; i++) { if(is_valid((esp+i))) { arg_int[i] = esp + i; } else thread_exit(); } f->eax = process_wait(*arg_int[0]); } else if(nsyscall == SYS_TELL) { for(i = 0; i < 1; i++) { if(is_valid((esp+i))) { arg_int[i] = esp + i; } else thread_exit(); } lock_acquire(&lock_sys); f->eax = syscall_tell(*arg_int[0]); lock_release(&lock_sys); } else if(nsyscall == SYS_REMOVE) { for(i = 0; i < 1; i++) { if(is_valid((esp+i))) { arg_ptr[i] = (void **)(esp + i); } else thread_exit(); } if(!is_valid(*arg_ptr[0])) thread_exit(); lock_acquire(&lock_sys); f->eax = filesys_remove(*arg_ptr[0]); lock_release(&lock_sys); } else if(nsyscall == SYS_OPEN) { for(i = 0; i < 1; i++) { if(is_valid((esp+i))) { arg_ptr[i] = (void **)(esp + i); } else thread_exit(); } if(!is_valid(*arg_ptr[0])) thread_exit(); lock_acquire(&lock_sys); f->eax = syscall_open(*arg_ptr[0]); lock_release(&lock_sys); } else if(nsyscall == SYS_CLOSE) { for(i = 0; i < 1; i++) { if(is_valid((esp+i))) { arg_int[i] = esp + i; } else thread_exit(); } lock_acquire(&lock_sys); syscall_close(*arg_int[0]); lock_release(&lock_sys); } else if(nsyscall == SYS_FILESIZE) { for(i = 0; i < 1; i++) { if(is_valid((esp+i))) { arg_int[i] = esp + i; } else thread_exit(); } lock_acquire(&lock_sys); f->eax = syscall_filesize(*arg_int[0]); lock_release(&lock_sys); } else if(nsyscall == SYS_CREATE) { for(i = 0; i < 2; i++) { if(is_valid((esp+i))) { arg_int[i] = esp + i; arg_ptr[i] = (void **)(esp + i); } else thread_exit(); } if(!is_valid(*arg_ptr[0])) thread_exit(); lock_acquire(&lock_sys); f->eax = filesys_create(*arg_ptr[0], *arg_int[1]); lock_release(&lock_sys); } else if(nsyscall == SYS_SEEK) { for(i = 0; i < 2; i++) { if(is_valid((esp+i))) { arg_int[i] = esp + i; } else thread_exit(); } lock_acquire(&lock_sys); syscall_seek(*arg_int[0], *arg_int[1]); lock_release(&lock_sys); } else if(nsyscall == SYS_READ) { for(i = 0; i < 3; i++) { if(is_valid((esp+i))) { arg_int[i] = esp + i; arg_ptr[i] = (void **)(esp + i); } else thread_exit(); } if(!is_valid(*arg_ptr[1])) thread_exit(); lock_acquire(&lock_sys); f->eax = syscall_read(*arg_int[0], *arg_ptr[1], *arg_int[2]); lock_release(&lock_sys); } else if(nsyscall == SYS_WRITE) { for(i = 0; i < 3; i++) { if(is_valid((esp+i))) { arg_int[i] = esp + i; arg_ptr[i] = (void **)(esp + i); } else thread_exit(); } if(!is_valid(*arg_ptr[1])) thread_exit(); lock_acquire(&lock_sys); f->eax = syscall_write(*arg_int[0], *arg_ptr[1], *arg_int[2]); lock_release(&lock_sys); } else if(nsyscall == SYS_CHDIR) { for(i = 0; i < 1; i++) { if(is_valid((esp+i))) { arg_ptr[i] = (void **)(esp + i); } else thread_exit(); } if(!is_valid(*arg_ptr[0])) thread_exit(); lock_acquire(&lock_sys); f->eax = syscall_chdir(*arg_ptr[0]); lock_release(&lock_sys); } else if(nsyscall == SYS_MKDIR) { for(i = 0; i < 1; i++) { if(is_valid((esp+i))) { arg_ptr[i] = (void **)(esp + i); } else thread_exit(); } if(!is_valid(*arg_ptr[0])) thread_exit(); lock_acquire(&lock_sys); f->eax = syscall_mkdir(*arg_ptr[0]); lock_release(&lock_sys); } else if(nsyscall == SYS_READDIR) { for(i = 0; i < 2; i++) { if(is_valid((esp+i))) { arg_int[i] = esp + i; arg_ptr[i] = (void **)(esp + i); } else thread_exit(); } if(!is_valid(*arg_ptr[1])) thread_exit(); lock_acquire(&lock_sys); f->eax = syscall_readdir(*arg_int[0], *arg_ptr[1]); lock_release(&lock_sys); } else if(nsyscall == SYS_ISDIR) { for(i = 0; i < 1; i++) { if(is_valid((esp+i))) { arg_int[i] = esp + i; } else thread_exit(); } lock_acquire(&lock_sys); f->eax = syscall_isdir(*arg_int[0]); lock_release(&lock_sys); } else if(nsyscall == SYS_INUMBER) { for(i = 0; i < 1; i++) { if(is_valid((esp+i))) { arg_int[i] = esp + i; } else thread_exit(); } lock_acquire(&lock_sys); f->eax = syscall_inumber(*arg_int[0]); lock_release(&lock_sys); } else thread_exit(); }
static void syscall_handler (struct intr_frame *f) { int nsyscall, argc, i; int *esp = (int *)f->esp; /* get argument pointer */ int *arg_int[3]; void **arg_ptr[3]; /* verify the argument pointer */ if (!IS_VALID(esp)) goto error_end; /* system call number */ nsyscall = *(esp++); /* number of arguments */ switch (nsyscall) { case SYS_HALT: argc = 0; break; case SYS_EXIT: case SYS_EXEC: case SYS_WAIT: case SYS_TELL: case SYS_CLOSE: case SYS_REMOVE: case SYS_OPEN: case SYS_FILESIZE: argc = 1; break; case SYS_CREATE: case SYS_SEEK: argc = 2; break; case SYS_READ: case SYS_WRITE: argc = 3; break; default: goto error_end; } /* verify the argument pointer */ for (i = 0; i < argc; i++) if (IS_VALID(esp + i)) { arg_int[i] = esp + i; arg_ptr[i] = (void**)(esp + i); } else { break; } if (i < argc) goto error_end; /* system call */ switch (nsyscall) { case SYS_HALT: shutdown_power_off(); break; case SYS_EXIT: syscall_exit(*arg_int[0]); break; case SYS_EXEC: if (!IS_VALID(*arg_ptr[0])) goto error_end; lock_acquire (&lock_filesys); f->eax = process_execute(*arg_ptr[0]); lock_release (&lock_filesys); break; case SYS_WAIT: f->eax = process_wait(*arg_int[0]); break; case SYS_TELL: lock_acquire (&lock_filesys); f->eax = syscall_tell(*arg_int[0]); lock_release (&lock_filesys); break; case SYS_CLOSE: lock_acquire (&lock_filesys); process_file_close(*arg_int[0]); lock_release (&lock_filesys); break; case SYS_REMOVE: if (!IS_VALID(*arg_ptr[0])) goto error_end; lock_acquire (&lock_filesys); f->eax = syscall_remove(*arg_ptr[0]); lock_release (&lock_filesys); break; case SYS_OPEN: if (!IS_VALID(*arg_ptr[0])) goto error_end; lock_acquire (&lock_filesys); f->eax = process_file_open(*arg_ptr[0]); lock_release (&lock_filesys); break; case SYS_FILESIZE: lock_acquire (&lock_filesys); f->eax = syscall_filesize(*arg_int[0]); lock_release (&lock_filesys); break; case SYS_CREATE: if (!IS_VALID(*arg_ptr[0])) goto error_end; lock_acquire (&lock_filesys); f->eax = syscall_create(*arg_ptr[0], *arg_int[1]); lock_release (&lock_filesys); break; case SYS_SEEK: lock_acquire (&lock_filesys); syscall_seek(*arg_int[0], *arg_int[1]); lock_release (&lock_filesys); break; case SYS_READ: if (!IS_VALID(*arg_ptr[1])) goto error_end; lock_acquire (&lock_filesys); f->eax = syscall_read(*arg_int[0], *arg_ptr[1], *arg_int[2]); lock_release (&lock_filesys); break; case SYS_WRITE: if (!IS_VALID(*arg_ptr[1])) goto error_end; lock_acquire (&lock_filesys); f->eax = syscall_write(*arg_int[0], *arg_ptr[1], *arg_int[2]); lock_release (&lock_filesys); break; default: printf ("Unknown syscall : %d\n", nsyscall); goto error_end; } return; error_end: thread_exit (); }
void syscall_halt(void){ shutdown_power_off(); return ; }
static void syscall_handler (struct intr_frame *f) { uint32_t* args = ((uint32_t*) f->esp); // OUR CODE HERE verify_user_ptr(args); switch (args[0]) { case SYS_EXIT: { verify_args(args, 1); f->eax = args[1]; exit(args[1]); break; } case SYS_NULL: { verify_args(args, 1); f->eax = args[1] + 1; break; } case SYS_WRITE: { verify_args(args, 3); lock_acquire(&file_lock); f->eax = write(args[1], user_to_kernel((void *) args[2]), args[3]); lock_release(&file_lock); break; } case SYS_HALT: { shutdown_power_off(); break; } case SYS_WAIT: { verify_args(args, 1); f->eax = wait((tid_t) args[1]); break; } case SYS_EXEC: { verify_args(args, 1); f->eax = exec(user_to_kernel((void *) args[1])); break; } case SYS_CREATE: { verify_args(args, 2); f->eax = create(user_to_kernel((void *) args[1]), args[2]); break; } case SYS_REMOVE: { verify_args(args, 1); f->eax = remove(user_to_kernel((void *) args[1])); break; } case SYS_OPEN: { verify_args(args, 1); f->eax = open(user_to_kernel((void *) args[1])); break; } case SYS_FILESIZE: { verify_args(args, 1); f->eax = filesize(args[1]); break; } case SYS_READ: { verify_args(args, 3); f->eax = read(args[1], user_to_kernel((void *) args[2]), args[3]); break; } case SYS_SEEK: { verify_args(args, 2); seek(args[1], args[2]); break; } case SYS_TELL: { verify_args(args, 1); f->eax = tell(args[1]); break; } case SYS_CLOSE: { verify_args(args, 1); close(args[1]); break; } } }
/* Shuts down the system completely */ static void sys_halt () { shutdown_power_off (); NOT_REACHED (); }