Presentation is loading. Please wait.

Presentation is loading. Please wait.

Operating Systems : Project #2-1. Pintos User Program.

Similar presentations


Presentation on theme: "Operating Systems : Project #2-1. Pintos User Program."— Presentation transcript:

1 Operating Systems : Project #2-1. Pintos User Program

2 Distributed and Cloud Computing Lab. 1.Prerequisites  Background  Making & Using the File System for Pintos  How User Program Work  Virtual Memory Layout  Accessing User Memory 2.Requirements  Process Termination Messages  Argument Passing  System Calls  Denying Writes to Executables 3.Suggested Order of Implementation 4.Evaluation 5.Documentation 6.Submission Contents 2

3 Distributed and Cloud Computing Lab. Prerequisites 3

4 Distributed and Cloud Computing Lab. 1.Pintos Project : User Programs  Base code supports loading and running user programs minimally, but no I/O or interactivity is possible.  Working out of the ‘pintos/src/userprog’ directory.  Need to modify… process.h / process.c pagedir.h / pagedir.c syscall.h / syscall.c exception.h / exception.c gdt.h / gdt.c tss.h / tss.c Background 4

5 Distributed and Cloud Computing Lab. 1.User programs are loaded from the file system!! 2.Commands for the simulated file system disk  Creating $ pintos–mkdisk filesys.dsk -–filesys-size=2  Formatting $ pintos –f –q  Copying ‘user program file’ into the Pintos file system $ pintos –p../../examples/echo –a echo –– –q  Running $ pintos –q run ‘echo x’  Combined command (example) $ pintos-mkdisk filesys.dsk -–filesys-size=2 $ pintos –p../../examples/echo –a echo -- -f –q run ‘echo x’ or $ pintos -–filesys-size=2 –p../../examples/echo –a echo -- -f –q run ‘echo x’ Making & Using the File System 5

6 Distributed and Cloud Computing Lab. 1.Pintos can load run regular ELF executables. 2.To run user program, you must copy (put) the user program to the simulated file system disk How User Program Work Hardware (ex. cspro) Hardware (PPC, SPARC...) Host OS (Linux, …) Host OS (Linux, …) Guest OS (Pintos, …) Hardware Emulator (Bochs, QEMU,..) Apps (Pintos Application) Hardware Emulator (Bochs or QEMU) Pintos File System (Disk) Regular ELF executable PUT Simulated File system disk 6

7 Distributed and Cloud Computing Lab. Code Level Flow pintos -v -k -T 60 --bochs --fs-disk=2 -- -q -f run 'args-single onearg' threads/init.c int main(void) { some initializations…; syscall_init(); …; run_actions(argv); … } threads/init.c void run_task() { process_wait(process_execute(task)); } process.c process_execute() { thread_create() with execute_thread(filename); } process.c bool load(filename, eip, esp) { allocate_pagedir; parse_filename(); load ELF executable; setup_stack(esp); construct_ESP(esp); } “onearg” “args-single” NULL &”onearg” &”args-single” &(^^^) 2 0 process.c execute_thread() { load(…); start_user_process; } “args-single”, “onearg” 7

8 Distributed and Cloud Computing Lab. 1.Pintos 는 32bit 메모리 주소를 가지며, 각각의 프로세스 (thread) 는 독립 적인 3G 의 공간을 갖는다. 2.Pintos 의 커널은 1G 의 Global 공간을 할당 받는다 (PHYS_BASE(3G)~4G 영역 ). 3.Pintos 에서 물리적인 메모리를 처리하는 단위는 Page 이며 4K 의 PAGE_SIZE 를 갖는다. 4.Pintos 에서 수행되는 각각의 Thread 는 하나의 Page (PCB : process control block) 를 할당 받으며, 수행에 필요한 정보를 포함한다. (threads/thread.h:struct thread) 5.Pintos 의 유저프로그램은 page_directory, page_table 을 통해 실제 physical page 에 접근한다. Virtual Memory 8

9 Distributed and Cloud Computing Lab. struct thread 9

10 Distributed and Cloud Computing Lab. 1.Pintos kernel 이 process 를 control 하기 위하여 사용하는 structure (defined in pintos/src/thread/thread.h)  UNIX system 에서의 process control block 과 같은 역할  thread 를 다루기 위한 다양한 API 들이 thread.h 에서 제공된다. struct thread 10

11 Distributed and Cloud Computing Lab. Virtual Memory Overview PHYS_BASE Physical Memory Kernel Memory PoolUser Memory Pool Kernel Pages (Global) Max 1GB Thread A {… pagedir …} Thread B {… pagedir …} User Stack Growth → 3GB 0 User Pages (per thread) User Pages (per thread) 3GB 0 switching – process_activate() Swap Disk 11

12 Distributed and Cloud Computing Lab. 1.User program can pass a invalid pointer. (e.g. null ptr, unmapped virtual memory, ptr to kernel address space…) 2.Invalid pointers must be rejected without harm to kernel or other running process. 3.Can implement this in 2 ways  Verify the validity of a user-provided pointer, then dereference it.  Check only that a user pointer points below PHYS_BASE, then dereference it. If the pointer is invalid, it will cause a “page fault” that you can handle by modifying the code for page_fault() in ‘userprog/exception.c’. (recommended) 4.Helpful comment and sample code is provided at pintos document 3.1.5 Accessing User Memory (pp. 26~27) Accessing User Memory 12

13 Distributed and Cloud Computing Lab. Must Control referenced address (mean under PHYS_BASE) HOW? Accessing User Memory : Page Fault 13

14 Distributed and Cloud Computing Lab. 1.Memory Pool 로부터 page 얻어오기  threads/palloc.c:palloc_get_page(flags) – 직접적으로 사용할 필요는 없음 2.User Page 를 위한 Page Directory 관련  userprog/pagedir.c pagedir_create() - pagedir 생성 (struct thread 가 갖는다 ) pagedir_get_page() – user virtual address 에 대해 kernel virtual address 를 구해준다 (pagedir->pagetable->physical page), NULL if unmapped. pagedir_set_page() – pagedirectory 에 맵핑을 추가한다. 3. 기타 ‘userprog/pagedir.c’, ‘threads/vaddr.h’ 의 API 를 활용가능 Virtual Memory APIs 14

15 Distributed and Cloud Computing Lab. Requirements 15

16 Distributed and Cloud Computing Lab. 1. 유저 프로그램이 종료될 때 (system call – exit(int exitstatus) 호출 ), 커널이 종료 메시지를 출력한다.  ProcessName: exit(exitstatus)\n 형식  Ex) echo: exit(23)  SystemCall – exit() 를 올바르게 구현해야 동작한다.  Pintos 문서 3.3.2 참고 Project Requirements :Process Termination Messages 16

17 Distributed and Cloud Computing Lab. 1.User 가 수행하고자 하는 프로그램은 다양한 argument 를 가질 수 있다. 2.Argument 를 해석하고 80x86 Calling Convention 에 맞추어 메모리에 배치해야 올바른 수행을 기대할 수 있다.  Pintos 문서 3.5 80x86 Calling Convention 참고 3.Argument 의 길이는 4K 이하라고 가정  테스트는 약 128 bytes 이하로 이루어진다. 4.Argument 의 해석 후 ESP(stack pointer) 의 가장 밑 부분에 차례대로 채운다. Project Requirements :Argument Passing 17

18 Distributed and Cloud Computing Lab. 1.Stack grows downward! 2.Should think about ‘How control stack pointer?’ Project Requirements :Argument Passing 18

19 Distributed and Cloud Computing Lab. 1.Example: main(int argc, char** argv)  cp filea fileb 2.Stack overview  “fileb”  “filea”  “cp”   NULL  & “fileb”  & “filea”  & “cp”  & (^^^) (argv - address of the word above this word)  3 (argc)  Return address Project Requirements :Argument Passing 19

20 Distributed and Cloud Computing Lab. 1.Actual Example  ‘/bin/ls –l foo bar’ -> ‘/bin/ls’, ‘-l’, ‘foo’, ‘bar’(parse)  Base Address is 0xC0000000, Why? Project Requirements :Argument Passing 20

21 Distributed and Cloud Computing Lab. 1.Result of hex_dump()  This help-function is very useful for debug 2.PHYS_BASE 에 대한 포인터는 다음 함수에서 주어진다. process.c:static bool setup_stack(void **esp) Project Requirements :Argument Passing 21

22 Distributed and Cloud Computing Lab. 1. 명시된 System Call 들을 모두 구현한다.  System Call 의 요구사항은 pintos 문서 3.3.4 에 기술되어 있음 2.Process 관련  halt, exit, exec, wait, read, write  ( 주 : Pintos 의 exec 는 Unix 의 exec 와 다르다 )  File System 관련 system call (create, remove, open, filesize, read, wirte, seek, tell, close) 은 이번 프로젝트에서는 구현 대상에서 제외하였음  단, read, write 의 경우 standard input/output 의 기능은 수행할 수 있 도록 구현하여야 함 3.Pintos document 3.3.4 의 system call 목록과 prototype, return value, 기능 설명을 참조하여 구현한다. Project Requirements :System Calls 22

23 Distributed and Cloud Computing Lab. 1. 기존 Pintos 에 다음의 새로운 system call 들을 추가한다  int pibonacci (int n) N 번째 피보나치 수열의 값을 리턴한다.  int sum_of_four_integers (int a, int b, int c, int d) a, b, c, d 의 합을 구해서 반환한다 2. 추가한 system call 을 이용하는 user level program 을 작성한다  pintos/src/examples 디렉토리에 sum.c 파일을 만든다  위의 system call 을 이용하는 간단한 예제를 작성한다  실행 파일의 이름은 sum 으로 한다  Usage :./sum [num 1] [num 2] [num 3] [num 4]  Function : num 1 을 parameter 로 하여 pibonacci 를 수행하고 num 1, 2, 3, 4 를 parameter 로 하여 sum_of_four_integer 를 수행한 후 결과를 출력한다  Example Input :./sum 5 5 1 4 Output : 5 15 Add new system calls 23

24 Distributed and Cloud Computing Lab. 1.System-Call 의 번호는 어떻게 식별하는가 ? 2.System Call 의 결과값을 어떻게 돌려주는가 ?  syscall.c:syscall_handler() 에 전달되는 intr_frame 에 어떤 정보 가 있는지 잘 살펴본다 3.Pintos 는 System Call 에 의해 시스템이 손상되지 않도록 해 야 한다.  유저 프로그램이 커널영역 메모리 주소를 참조할 수 없어야 한다 (pintos 문서 3.1.5 참고 ).  open(NULL);  exec(“no-such-file”); Project Requirements :System Calls 24

25 Distributed and Cloud Computing Lab. 1.exec() and wait()  exec() 로 프로세스를 생성하고 wait() 로 종료를 기다리는 다 양한 시나리오를 고려한다. wait( exec(“a.out”) ); ?  각종 동기화 기법이 필요할 수 있다.  pintos/src/tests/userprogram, pintos/src/tests/filesys 의 다양한 예제 소스를 분석해본다. Project Requirements :System Calls 25

26 Distributed and Cloud Computing Lab. 1.write() and read()  File Descriptor( 표준 C 의 FILE* 와 비슷하다 ) open(), create() 등의 return value. Pintos 에서는 각 thread 가 독립적인 FD 를 가지며, 관리한다.  File Descriptors of STDIN, STDOUT STDIN = 0 STDOUT = 1  read(0) 의 구현은 다음 함수를 이용하면 된다. pintos/src/devices/input.c:uint8_t input_getc(void)  write(1) 의 구현은 다음 함수를 이용하면 된다. pintos/src/lib/kernel/console.c:void putbuf(…) Project Requirements :System Calls 26

27 Distributed and Cloud Computing Lab. System Call : supplementary notes 27

28 Distributed and Cloud Computing Lab. System calls provide the interface between a running program and the operating system.  Generally available as assembly-language instructions.  Languages defined to replace assembly language for systems programming allow system calls to be made directly (e.g., C, C++) Three general methods are used to pass parameters between a running program and the operating system.  Pass parameters in registers.  Store the parameters in a table in memory, and the table address is passed as a parameter in a register.  Push (store) the parameters onto the stack by the program, and pop off the stack by operating system. System-Call(from 2004 OS ppt) 28

29 Distributed and Cloud Computing Lab. System-Call in Linux(from 2004 OS ppt) Linking User Program Libraries _syscall() int 0x80 ret User Kernel CallReturn Interrupt Vector _system_call 0x80 arch/i386/kernel/entry.S _system_call: ….. fn = get function # call (sys_call_table+fn) ….. _sys_call_table:.long sys_func_1.long sys_func_2 …...long sys_func_n fn sys_func_2() { return; } 29

30 Distributed and Cloud Computing Lab. System-Call in Pintos User Program (echo.c) Linking Libraries syscall3() pushl int 0x30 ret User Kernel Call(printf(…)) Return Interrupt Vector Table threads/interrupt.c syscall 0x30 struct intr_frame *f; syscallnum = f->esp; handle(syscallnum, f); …... save syscall_result; end of syscall handler; Registered Interrupt handler for 0x30 [syscall.c:syscall_handler()] write(…) lib/user/ syscall.h lib/user/ syscall.c 30

31 Distributed and Cloud Computing Lab. 1.System-Call 의 번호는 어떻게 식별하는가 ?  lib/syscall-nr.h 파일에 enumeration 으로 정의되어 있다. System-Call in Pintos 31

32 Distributed and Cloud Computing Lab. 1.In ‘pintos/src/threads/synch.c’  void lock_init(struct lock *)  void lock_acquire(struct lock *)  bool lock_try_acquire(struct lock *)  void lock_release(struct lock *)  bool lock_held_by_current_thread(const struct lock *) Synchronization APIs 32

33 Distributed and Cloud Computing Lab. 이번 프로젝트는 일정 수준이상 완성하기 전에는 전혀 결과 를 stdout 으로 확인 할 수 없다. 따라서 pintos 문서를 매우 신 중하게 읽고 확실한 전략 수립 후 진행해 나가는것이 좋다. 1) Argument Passing: 프로그램 실행의 기본이 되기 때문에, 반드시 hex_dump() 를 이용해 확인한다 (word-alignment 등 ). 2) User Memory Access: Memory Access 는 pintos 의 평가에서 중요한 부분이므로 어떻게 보호할 것인지 명확한 전략을 세 운다 (System Call argument 의 주소 등 ). Suggested Order of Implementation 33

34 Distributed and Cloud Computing Lab. 3) System Call Infrastructure: syscall_handler() 에 System Call 을 처리하기 위한 기본구조를 만든다. 특히 가시적인 결과를 주는 exec(), exit(), write(), read() 등에 우선순위를 두고 구현 한다. 4) System Call Implementation: file system Call 과 wait() 의 동 기화 등 남은 부분에 대해 구현한다. 5) Denying Writes to Executables 6) Tune the pintos: 76 개의 다양한 예제에서 제공하는 예외 상황을 해결한다. Suggested Order of Implementation 34

35 Distributed and Cloud Computing Lab. 1.Project 2 의 평가 테스트는 총 76 개이며 모든 테스트를 통 과 해야만 한다. Process 에 관련된 테스트를 통과해야 한 다. 2. 각각의 테스트는 다른 점수를 갖는다. 3.Pintos 의 Grading script(make grade) 로 평가하며 Functionality 와 Robustness 로 구분된다. 4. 각 테스트의 비중은 다음을 참고한다. pintos/src/tests/userprog/Grading pintos/src/tests/userprog/Rubric* pintos/src/tests/userprog/no-vm/Rubric pintos/src/tests/filesys/base/Rubric Evaluation 35

36 Distributed and Cloud Computing Lab. 1.pass tests/userprog/args-none 2.pass tests/userprog/args-single 3.pass tests/userprog/args-multiple 4.pass tests/userprog/args-many 5.pass tests/userprog/args-dbl-space 6.pass tests/userprog/sc-bad-sp 7.pass tests/userprog/sc-bad-arg 8.pass tests/userprog/sc-boundary 9.pass tests/userprog/sc-boundary-2 10.pass tests/userprog/halt 11.pass tests/userprog/exit 12.pass tests/userprog/exec-once 13.pass tests/userprog/exec-arg 14.pass tests/userprog/exec-multiple 15.pass tests/userprog/exec-missing 16.pass tests/userprog/exec-bad-ptr 17.pass tests/userprog/wait-simple 18.pass tests/userprog/wait-twice 19.pass tests/userprog/wait-killed 20.pass tests/userprog/wait-bad-pid 21.pass tests/userprog/multi-recurse 22.21 tests Tests to pass 36

37 Distributed and Cloud Computing Lab. 1.Pintos Project 는 ABEEK 의 프로젝트 형식을 따라야 하므로, 추후 문서 작성 양식이 공지될 것 Documentation 37

38 Distributed and Cloud Computing Lab. 1.Team project 2.Deadline: 10 월 28 일 ( 금 ) 오후 5 시 3.E-mail 제출  pintos/src 에서 make clean 수행한 후,  조 번호로 디렉토리를 만들고 그 안에 pintos 디렉토리와 document 복사  조 번호 디렉토리 전체를 os_prj2_1_ 반번호 _ 조번호.tar.gz 으로 압축 ( 조번호는 두자리 숫자로 ) Ex. 2 반 3 조의 경우 tar –czvf os_prj2_1_2_03.tar.gz./02  메일 제목 : [OS_PRJ_#1] 0 반 00 조  oshw2016@gmail.com 으로 제출 oshw2016@gmail.com 메일제목, 압축파일명 등이 양식에 어긋날 경우, 제대로 제출처리가 되지 않을 수 있으니 반드시 신경 써서 양식에 맞게 제출해 주시기 바랍니다 4.Document  AS818 에 hardcopy 제출 Document deadline 도 source code deadline 과 같습니다. Submission 38


Download ppt "Operating Systems : Project #2-1. Pintos User Program."

Similar presentations


Ads by Google