diff --git a/student-distrib/proc.c b/student-distrib/proc.c index 25eb4ac954272c56f14cff1cfe664048de213ca4..2621e745839f062f1833e5ac3c7741e88fe65fb8 100644 --- a/student-distrib/proc.c +++ b/student-distrib/proc.c @@ -35,7 +35,8 @@ int32_t fd_check(int32_t fd) if (fd >= MIN_FILE_NUM && fd < MAX_FILE_NUM ) { /* Now check if file is present */ - if ( terminal[ running_terminal].pcb->fd[fd].flags != 0) + // // use running_terminal instead of visible_terminal !!! + if ( terminal[running_terminal].pcb->fd[fd].flags != 0) return 0; } return -1; @@ -44,13 +45,14 @@ int32_t fd_check(int32_t fd) //clearing fd void fd_clear(int32_t fd) { - terminal[ running_terminal].pcb->fd[fd].fileop_ptr.read = 0; - terminal[ running_terminal].pcb->fd[fd].fileop_ptr.write = 0; - terminal[ running_terminal].pcb->fd[fd].fileop_ptr.open = 0; - terminal[ running_terminal].pcb->fd[fd].fileop_ptr.close = 0; - terminal[ running_terminal].pcb->fd[fd].inode_index = 0; - terminal[ running_terminal].pcb->fd[fd].file_pos = 0; - terminal[ running_terminal].pcb->fd[fd].flags = 0; + // use running_terminal instead of visible_terminal !!! + terminal[running_terminal].pcb->fd[fd].fileop_ptr.read = 0; + terminal[running_terminal].pcb->fd[fd].fileop_ptr.write = 0; + terminal[running_terminal].pcb->fd[fd].fileop_ptr.open = 0; + terminal[running_terminal].pcb->fd[fd].fileop_ptr.close = 0; + terminal[running_terminal].pcb->fd[fd].inode_index = 0; + terminal[running_terminal].pcb->fd[fd].file_pos = 0; + terminal[running_terminal].pcb->fd[fd].flags = 0; return; } diff --git a/student-distrib/syscall.c b/student-distrib/syscall.c index f581486da377dd23026afb588c3ac038b2dfe8ab..f8963f29a622a54b7058983b192e5eb14330458f 100644 --- a/student-distrib/syscall.c +++ b/student-distrib/syscall.c @@ -6,20 +6,16 @@ * * DESCRIPTION: This function terminates a process, returning the specified value to its parent process. * - * INPUT: status -- + * INPUT: status * - * RETURN: + * RETURN: always return 0 */ -int32_t halt (uint8_t status) -{ +int32_t halt (uint8_t status) { int i; cli(); pcb_t* pcb_cur = terminal[running_terminal].pcb; - - // printf("[halt] running_terminal = %d\n", running_terminal); - pcb_t* pcb_prev = pcb_cur->parent_pcb; /* set all present flags in PCB to "Not In Use" */ @@ -38,6 +34,7 @@ int32_t halt (uint8_t status) terminal[pcb_cur->running_terminal].terminal_prog_count--; } + // exiting "root shell" if(terminal[running_terminal].terminal_prog_count == 0) { printf("[halt] executing new shell...\n"); @@ -57,13 +54,12 @@ int32_t halt (uint8_t status) clear_pid(pcb_cur->pid); - //------------------ Jump to execute return -------------------------------- asm volatile( "movl %0, %%eax;" - "movl %1, %%esp;" - "movl %2, %%ebp;" + "movl %1, %%esp;" // restore parent_esp + "movl %2, %%ebp;" // restore parent_ebp "jmp exec_ret" : :"r"((uint32_t)status), "r"(pcb_cur->parent_esp), "r"(pcb_cur->parent_ebp) @@ -94,18 +90,12 @@ int32_t execute(const uint8_t* command) cli(); - // if command == NULL, abort + // invalid command if(command == NULL) return -1; - // if(strncmp("\n", command, 1) == 0) - // return 0; - if(strlen((int8_t *) command) == 1) return 0; - // printf("[EXE] running_terminal = %d\n", running_terminal); - // printf("[EXE] (command) = %s\n", command); - //------------------ 0 : Parse args ---------------------------------------- uint8_t parsed_cmd[MAX_COMMAND_SIZE]; @@ -116,24 +106,15 @@ int32_t execute(const uint8_t* command) ret = parse_cmd(command, parsed_cmd, argv); - /* For keyboard interrupts */ - //sti(); - - //set argument number for pcb initialization - + // set argument number for pcb initialization arg_num = (ret > -1) ? ret : 0; - if(ret == -1) - - return -1; - - else if(ret == -2) + if(ret == -1) return -1; - exit(); + if(ret == -2) exit(); //------------------ 1: Executable check ----------------------------------- - // if cpu reaches its maximum control, abort if(total_prog_count >= MAX_PROCESS) { printf("[execute] Reached maximum number of processes %d\n", MAX_PROCESS); @@ -153,20 +134,19 @@ int32_t execute(const uint8_t* command) { return -1; } + // if not an executable file if ( (buf[0] != AS_DEL) || (buf[1] != AS_E) || (buf[2] != AS_L) || (buf[3] != AS_F) ) { return -1; } - // Get free PID index + // Get a new pid pid = set_pid(); - /* Test variables */ total_prog_count++; terminal[running_terminal].terminal_prog_count++; //get entry point from bytes 24 to 27 in exe file read_data( dentry.inode_index, ENTRY_POINT, buf, 4); - entry_point = *((uint32_t*)buf); //-------------------- 3: Paging ------------------------------------------- @@ -215,16 +195,7 @@ int32_t execute(const uint8_t* command) if(strncmp("shell", (int8_t *) parsed_cmd, 5) == 0) terminal[running_terminal].pcb->shell_flag = 1; - //printf("[EXE] using pcb = %x\n", terminal[running_terminal].pcb); - - //printf("[EXE] set pcb ebp to %x , esp to %x\n", terminal[running_terminal].pcb->current_ebp, terminal[running_terminal].pcb->current_esp); - //printf("[EXE] should be ebp %x , esp %x\n", temp_ebp, temp_esp); - //--------------------- 6: scheduling initilization ----------------------- - - - - - //--------------------- 7: context switch----------------------------------- + //--------------------- 6: context switch----------------------------------- // update SS0 and ESP0 in Task State Segment when switching context tss.ss0 = KERNEL_DS; @@ -308,8 +279,6 @@ int32_t write (int32_t fd, const void* buf, int32_t nbytes){ int32_t ret; - //printf("[sys write] before = %d\n", running_terminal); - if (fd_check(fd) < 0 || fd == STDIN_FD) return -1; // invalid fd if (buf == NULL) return -1; // invalid buf @@ -318,8 +287,6 @@ int32_t write (int32_t fd, const void* buf, int32_t nbytes){ ret = terminal[running_terminal].pcb->fd[fd].fileop_ptr.write(fd, buf, nbytes); - //printf("[sys write] after = %d\n", running_terminal); - return ret; } @@ -339,41 +306,38 @@ int32_t open (const uint8_t* filename){ int i; int fd = -1; - int type; dentry_t dentry; - //terminal[ running_terminal].pcb = current_pcb(); - - /* Valid file name check */ + // invalid file name if( strlen( (char*)filename ) == 0 ) return -1; - + /* File does not exist */ if( read_dentry_by_name(filename, &(dentry)) != 0 ) return -1; /* File type checks */ - type = dentry.file_type; - if( type < 0 || type > 2) return -1; + if( dentry.file_type < 0 || dentry.file_type > 2) return -1; /* Looking for available space in FD table */ for(i = USER_FILE_START; i < MAX_FILE_NUM; i++){ - /* Check if FD table entry is unused */ + // if we can find a free fd if( terminal[ running_terminal].pcb->fd[i].flags == 0) { fd = i; break; } - /* FD table is full */ + // no free fd if(i == MAX_FILE_NUM-1) return -1; - } - terminal[ running_terminal].pcb->fd[fd].inode_index = (type == 2)? dentry.inode_index : 0; - terminal[ running_terminal].pcb->fd[fd].file_pos = 0; - terminal[ running_terminal].pcb->fd[fd].flags = 1; + // only regular file has inode, for other file types, set inode to 0 + terminal[running_terminal].pcb->fd[fd].inode_index = (dentry.file_type == 2)? dentry.inode_index : 0; + terminal[running_terminal].pcb->fd[fd].file_pos = 0; + terminal[running_terminal].pcb->fd[fd].flags = 1; - switch (type) { + // insert file specific operation table + switch (dentry.file_type) { case 0: terminal[ running_terminal].pcb->fd[fd].fileop_ptr = rtc_jumptable; break; @@ -385,9 +349,7 @@ int32_t open (const uint8_t* filename){ break; } - //sti(); - - terminal[running_terminal].pcb->fd[fd].fileop_ptr.open(filename); + terminal[running_terminal].pcb->fd[fd].fileop_ptr.open(filename); return fd; } @@ -408,8 +370,10 @@ int32_t open (const uint8_t* filename){ */ int32_t close (int32_t fd) { + + // invalid fd if(fd_check(fd) < 0 || fd < USER_FILE_START) - return -1; // invalid fd + return -1; terminal[running_terminal].pcb->fd[fd].fileop_ptr.close(fd); @@ -437,25 +401,19 @@ int32_t getargs (uint8_t* buf, int32_t nbytes){ uint8_t* argument; - //terminal[ running_terminal].pcb = current_pcb(); argument = (uint8_t*) (terminal[running_terminal].pcb->argv); - if( terminal[running_terminal].pcb->arg_num == 0 || nbytes <= 0 ) + //If there are no arguments + if ( terminal[running_terminal].pcb->arg_num == 0 || nbytes <= 0 ) { - //printf("[getargs] return -1\n"); - return -1; //If there are no arguments + return -1; } - //if the arguments and a terminal NULL (0-byte) do not fit in the buffer. + // if the arguments and a terminal NULL (0-byte) do not fit in the buffer. nbytes = (nbytes > MAX_ARGUMENT_SIZE * MAX_ARGUMENT_NUM) ? MAX_ARGUMENT_SIZE * MAX_ARGUMENT_NUM : nbytes; memcpy(buf, argument, nbytes); - // uint32_t i; - // //printf("[getargs] argument number = %d\n", terminal[ running_terminal].pcb->arg_num); - // for(i = 0; i < terminal[ running_terminal].pcb->arg_num; i++) - // //printf("[getargs] buf[%d] = %s\n", i, buf+i*MAX_ARGUMENT_SIZE); - //printf("[getargs] return 0\n"); return 0; } @@ -467,7 +425,7 @@ int32_t getargs (uint8_t* buf, int32_t nbytes){ * Although the address returned is always the same, it should be written into the memory * location provided by the caller (which must be checked for validity). * - * INPUT: screen_start -- + * INPUT: screen_start * * RETURN: -1 If the location is invalid. * @@ -492,7 +450,7 @@ int32_t vidmap (uint8_t** screen_start) remap_vidmem(); - *screen_start = (uint8_t *)(0x8400000); + *screen_start = (uint8_t *)(0x8400000); // 132MB, video memory starting address return 0x8400000; }