PintOS Project 2: UserProg

핀토스 프로젝트 2를 진행하며 중간중간 적었던 내용들을 정리했다.‌‌‌‌‌‌
직접적인 코드 언급은 없으니 후배님들은 치팅 걱정 없이 참고해도 될 것이다.

process_create_initd

  • tokenize file name and call thread_create with initd

exit

  • print exit message in syscall handler.(skeleton says to implement in process_exit, but it may be complex because of arguments)
  • up semaphore is_running to notify the waiting thread
  • change thread->exit_status to exit status

load

  • added length argument to implement argument passing.
    • it is utilized for memcpy to rsp-length-1.
  • filename should be tokenized(space changed to '\0') before
  • fill alignment
  • fill the pointer to each token string
  • add fake address

Halt

  • just calling power_off()

fork

  • changed argument of __do_fork from void * to void **
    • And process_fork passes not only thread_current() but also interrupt frame to __do_fork
  • duplicate_pte:
    • check va -> palloc -> memcpy -> return;
  • __do_fork:
    • copy fd table
      • change fd target value by adding offset
    • change return value(if_.R.rax) to 0

exec

  • implemented by calling process_exec()
  • syscall first copy filename to palloced new page.
  • process_exec tokenize f_name and call load()
  • implemented write protection by keep opening file and save file struct to thread struct (exec_file)

wait

  • each thread has the list of child threads.
  • first, search target pid in child threads
  • if found, wait.(using semaphore is_running)
    • after waiting, remove target pid in child process.
    • return exit status of waited process
  • if not, return -1

FD Table structure

  • Implemented using list(so it can make infinite fds)

  • each thread has struct list fdtable

    • fdtable consists a list of struct fd_element
    • fd_element is defined in syscall.h
      • it consists num, target. num is the fd number, target is the pointer to file struct.
  • Implemented using malloced array(list is too slow)

    • each thread has struct fdtable_element *, which points array of the file descriptor element.
      • fdtable_element has two fields: num-for large fds(not using), fd_element *target - indicated file
    • each thread has struct fd_element *, which points array of the file struct pointer.
      • fd_element has two fields: stack - for number of opened fds with this file, target - file struct.
    • dup2 implementation finished. may need to change idea if large fd is allowed.
      • Since testcases do not have such large fd, I did not change the implementation.
    • stdin and stdout is expressed as struct fd_elment *1 and 2. (this address is impossible address)
    • FD table free 제대로 안 하면 multi-oom 테스트 fail

chk_pointer function

  • checking pointer address. If wrong, kill process
  • also, exception.c is modified not to show pagefault content.

create/remove

  • First check pointer of file name.
  • If name is valid, call filesys_create/remove

open

  • malloc fdtable.
  • malloc filetable.
  • walk through filetable and find an available slot
    • save file struct pointer(which returned by filesys_open()) to the target of filetable.
  • If file opened successfully, walk through fd table and find available fd.
    • since fd table is sorted with fds, it is sufficient to increment fd and compare in each iteration.
  • set fdstruct's num to available fd and insert to the fd table.

filesize/tell/seek

  • walk through fd table and find corresponding fd.
  • If found, return file_length(fp)/file_tell(fp)/file_seek(fp, num_size)
  • else, return -1;

read

  • if fd table is allocated(has opened a file),
    • walk through fd table and find corresponding fd.
    • If found, return file_read(fp, buffer, size)
    • else, return -1;
  • else
    • check thread_current()->stdin_target and stdout_target

write

  • if fd table is allocated(has opened a file),
    • walk through fd table and find corresponding fd.
    • If found, return file_write(fp, buffer, size)
    • else, return -1;
  • else
    • check thread_current()->stdin_target and stdout_target

close

  • walk through fd table and find corresponding fd.
  • If found, decrement stack value in fd_element in filetable
    • if stack value is zero, file_close()
  • If target is 1 or 2(stdin/stdout), just change it to null

dup2

  • Need to consider fork() and exit()