1 / 44

Enforcing Modularity

Enforcing Modularity. Junehwa Song CS KAIST. How to run multiple modules?. X server. Emacs. Mail Reader. File Server. Designing a Virtual Memory System. Can multiple Modules share MM?. Partition MM space? Intentional/unintentional intrusion LOAD STORE. …. Emacs. Mail. ……. ….

borna
Télécharger la présentation

Enforcing Modularity

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Enforcing Modularity Junehwa Song CS KAIST

  2. How to run multiple modules? X server Emacs Mail Reader File Server Network Computing Lab.

  3. Designing a Virtual Memory System

  4. Can multiple Modules share MM? • Partition MM space? • Intentional/unintentional intrusion • LOAD • STORE … Emacs Mail …… … … X server Network Computing Lab.

  5. Then, What Can We Do??? Address Main Memory PROCESSOR Read/Write Data Virtual Address Physical Address Main Memory Virtual Memory Manager PROCESSOR Read/Write Read/Write Data Network Computing Lab.

  6. Address Translation • Page Map (Page Table) • Virtual Address Space • Segmentation • int PageTable[size] • int translate(int virtual) /* HW */ Network Computing Lab.

  7. Virtual address Address Translation Page # Byte Offset Block # Page Map (Page Table) Block # Byte Offset BTW, where do you want to put Page Map? Physical address

  8. Can multiple Modules share MM? 0 Page 10 (VM1) Physical Address 100 Virtual Address Page 10 (VM1) Page # Offset Block # Offset Virtual Memory Manager Page 12 (VM2) PROCESSOR 200 Read/Write Read/Write Unused block 300 300 10 100 Data Page Map of VM1 Page Map Address Register 11 400 12 0 400 Page 11 (VM1) Thus, a Page Map defines an address space 500 Page Map of VM2 12 100 13 800 Network Computing Lab.

  9. What do we further need to do? • Things to do • Create/Delete an address space • Grow an address space • Map a device to an address space • Switch an address space to another • Create-AS() { 1. identify an unused memory block; 2. generate a new address space id, ID ; 3. make a page map, PAGEMAP(ID) and initialize a page map ; 4. return (ID); } • Delete-AS(ID) { for each block of each entry in PAGEMAP(ID) free(block) ; free(PAGEMAP(ID)); } • Add-page(ID, page#) { 1. search for an unused block. Let’ the block be NEWBLOCK ; 2. make an entry (page#, NEWBLOCK) in PageMap(ID); } • Delete-page(ID, page#) • MAP(ID, page#, block) • Swich-AS() Network Computing Lab.

  10. Things to do • Create-AS() { } • Delete-AS(ID) { } • Add-page(ID, page#) { } • Delete-page(ID, page#) { 1. Search for the entry (page #, block) in PAGEMAP(ID) ; 2. Free(block); 3. Remove the entry from PAGEMAP(ID); } • MAP(ID, page#, block) { insert a new entry (page#, block) to PAGEMAP(ID); } • Swich-AS(ID) { 1. Change the address space to ID; 2. Load page map address register with the address of PAGEMAP(ID); /* we will come back to this later again*/ } Network Computing Lab.

  11. Virtual Memory System • Now, multiple programs can share a main memory • Each module has its own virtual memory space • Memory operations, e.g., LOAD, STORE, or control sequence instructions such as JMP are localized to its virtual address space. Network Computing Lab.

  12. Do we need a protected area in memory? • What if a module accidentally change a page map or page map address register? • Separate, special address space, called KERNEL address space • Put all page maps as well as the virtual memory manager programs in KERNEL • Define a flag bit (in a flag register) to designate if we are currently in KERNEL MODE or in user mode • In user mode, nobody can change the value the page map address register Kernel Mail Reader • Create-AS • Delete-AS • Add-page • Delete-page • Switch-AS • Map Text Editor Network Computing Lab.

  13. Interfaces to Kernel • Different ways to get a service from Kernel • A device signals an Interrupt • A user module executes an illegal instruction (exception), e.g., divide by zero • A user module executes a special instruction (system-call) • Create-AS, Delete-AS, Add-page, Delete-page, Switch-AS, Map 등등이 user module에게 주어지려면 system-call로 주어져야 함. Network Computing Lab.

  14. Exceptions and Interrupts • Interrupt • Maskable Interrupt • Non-maskable interrupt • Exceptions • Processor-detected exceptions: Deferin the addresses stored in STACK • Fault : the address of the instruction which generated Fault is saved • E.g., page fault exception handler • Trap : that of the next instruction • Generated when there is no need to re-execute the instruction • Mainly used for debugging, i.e., to notify the debugger that a specific instruction has been executed • Abort : cannot save anything • Process will be terminated • Programmed Exceptions (Software Interrupt) • E.g., in linux, generated by INT, INT3, and conditionally by INT0 and BOUND • Handled as a trapby CPU • For System Call and for notification to debugger of a specific event Network Computing Lab.

  15. 잠깐만! Our Interpreter Model • “4-register with Interrupt” Interpreter Model • 4 registers: • IC (Instruction Counter) • SP (Stack Pointer) • this means that each process (or thread) is given a stack • Flag (Flag Register) • Interrupt bit, kernel mode bit 포함 • PMAR (Page Map Address Register) • Interrupt is provided • Software interrupt is also provided Network Computing Lab.

  16. Things to do to enter/leave Kernel • The following can be common to three different kernel access methods • Let’s assume 4-register with Interrupt model • Change-mode-enter-Kernel (SYSCALL instruction) • Change mode from user to kernel : set kernel mode flag on • Save Page map address register (stack) and load that of the kernel • Save flags register (Stack) • Save Instruction counter (stack) and reload that of the kernel /* handling SP will be considered later….. E.g., ThreadTable entry에 …*/ • Change-mode-leave-Kernel (RTE instruction) • Change mode from kernel to user : set kernel mode flag off • Reload Page map address register (stack) • Pop flags register (stack) • Reload Instruction counter (stack) • Should be atomic actions • If not? • Processors implement the sequences differently • Can be completely or partly done in HW • E.g. ??? Network Computing Lab.

  17. Mode-Change Operations • Very expensive operation • Number of instructions • Invalidate or Clean up things (e.g., pipeline, cache, etc) Network Computing Lab.

  18. Switching address spaces • Switch-AS(ID) { • Change the address space to ID; • Load page map address register with the address of PAGEMAP(ID); } only kernel can change page map address register, thus • Switch-AS(ID) { • Change to KERNEL; /* change-mode-enter-kernel */ • Load page map address register to PAGEMAP(ID); • Change to the address space, ID; /*change-mode-leave-kernel */ } Network Computing Lab.

  19. Q/A • Isn’t it too slow doing address translation? • MMU • TLB • Do we want to see the details of a PageMap? Network Computing Lab.

  20. Designing a Virtual Processor System

  21. Running multiple modules in a processor? Abstraction of a Module in Execution Module A Module B Start execution Temporarily stop Resume execution Temporarily stop Network Computing Lab.

  22. Abstraction of a Module in Execution • An abstraction of a running program and its state • so that we can stop and resume an execution of a program at any time • Then, we can simulate a virtual processor for each module. • What do we need? Network Computing Lab.

  23. Thread • An abstraction of a running program and its state • We should be able to save and load a thread state, including • Next step of a thread • Environment of a thread • Registers • general purpose registers, • stack pointer, • flag register, etc • Pointer to address space: page map address register Network Computing Lab.

  24. First Trial !!! • Let’s make it simple • A very simple Virtual Thread Manager • Let’s assume that • the state of each thread is stored in its own stack • A function called yield() • ThreadTable[] yield() { • Save the current value of SP (current stack pointer) to ThreadTable[] • Select next thread to run // this part is a “scheduling” task • Load the stack pointer of the next thread to SP } Network Computing Lab.

  25. First Trial !!! (example) Thread 6 in execution  yield Thread 0 resumes Thread Table 100 0 Stack for Thread 6 1 204 …. 1042 1042 200 200 6 200 …. Next thread 0 0 6 Stack for Thread 0 200 204 200 100 Stack Pointer 3 104 100 Network Computing Lab.

  26. First Trial!!! (we still need a bit more) • Creat-Thread() • Exit-Thread() • Destroy-Thread() Network Computing Lab.

  27. First Trial!!! • Create-Thread (module_address) • Allocate space for a new stack • Initialize the stack • Push the address of “exit-thread()” • Push the address of (start of) module • Initialize the entry in the Thread-Table[] Network Computing Lab.

  28. 2048 Address of “exit-thread()” 396 Stack for Thread 7 392 388 Thread Table …. 100 0 300 1 …. Stack for Thread 6 …. 200 200 6 204 1042 1042 7 388 200 …. Next thread 6 0 0 204 200 100 Stack Pointer Stack for Thread 0 3 104 100 Network Computing Lab.

  29. First Trial!!! • Exit-Thread () • De-allocate space for a stack • De-allocate the entry in the Thread-Table[] Network Computing Lab.

  30. Design with Sequence Coordination • Sequence coordination with simple version <Editor module> ... While (input_byte_count <= processed_byte_count) { yield() } … <Keyboard module> … While a char is ready { input_byte_count++; do something; } … • Problems: • The shared variable input_byte_count should be carefully used • Editor module should repeatedly call yield() Network Computing Lab.

  31. Design with Sequence Coordination • Can we do any better? • What about making something similar to “interrupt”? • Wait() and notify() • Notify(eventcount) • Wait(eventcount, value) <Editor Module> … Eventcount input_byte_count; While (input_byte_count <= processed_byte_count) { wait(input_byte_count, processed_byte_count); } … <Keyboard module> … Input_byte_count++; notify(input_byte_count); … Network Computing Lab.

  32. Design with Sequence Coordination • What do we need to do to implement “virtual processor” with wait() and notify()? • We are making a thread to be a wait state, and ready (to run) state. Thus, a thread can be in a {running, ready, waiting} Create-thread() Notify() Schedule() waiting ready running Yield() Exit-thread() Wait() Network Computing Lab.

  33. Virtual Thread Manager with Wait and Notify • Struct thread { int SP ; //value of the stack pointer Int state ; //wait, ready, or run Int *event //if waiting, the eventcount we are waiting for } ThreadTable[] • Yield() { ThreadTable[me].SP = SP; //save my stack pointer scheduler() ; } • Scheduler() { do { // select a runnable thread next_thread = (next_thread +1) %7 ; //select a thread by a certain policy, here “round robin” } while (ThreadTable[next_thread].state == waiting) ; SP = ThreadTable[next_thread].SP ; //load SP register return ; // pop return address from stack } • Wait() { } • Notify() { } Network Computing Lab.

  34. Virtual Thread Manager with Wait and Notify • Struct thread { int SP ; //value of the stack pointer Int state ; //wait, ready, or run Int *event //if waiting, the eventcount we are waiting for } ThreadTable[] • Yield() { } • Scheduler() { } • Wait(eventcount, value) { ThreadTable[me].event = &eventcount; ThreadTable[me].state = waiting; if ( *eventcount > value) ThreadTable[me].state = ready ; scheduler() ; } • Notify(eventcount) { for ( i=0 ; i < size ; i++) { if ( (ThreadTable[i].state == WAITING) &&(ThreadTable[i].event==&eventcount) ) ThreadTable[i].state = ready ; } } Network Computing Lab.

  35. Virtual Thread Manager with Wait and Notify • Again, we should be cautious in using a shared variable, especially when it is updated. In this case, the updated shared variable is “ThreadTable[me].state” • What if Wait(eventcount, value) { ThreadTable[me].event = &eventcount; ThreadTable[me].state = waiting; if ( *eventcount > value) { ThreadTable[me].state = ready ; scheduler() ; } else ThreadTable[me].state = waiting; } Network Computing Lab.

  36. Virtual Thread Manager with Wait and Notify • Be cautious in using Wait() and Notify() • What if everybody waits and nobody notifies? • What can we do for the case? • Deadlock Network Computing Lab.

  37. Interface of our Simple Kernel Text Editor System Call Interrupt Mail Reader Exception • Create-AS • Delete-AS • Add-page • Delete-page • Switch-AS • Map • Create-thread • Exit-thread • Destroy-thread • yield • Register-gate • Transfer-to-gate We can say that the above is A simple Kernel model based on 4-register with interrupt interpreter model Network Computing Lab.

  38. Design of a Simple OS • Initialization Sequence • Reset • Boot • Kernel initialization • Initialization of initial applications • Reset • Turn on of computer • Load boot program from ROM • Virtual and physical address 0 • Boot • Read kernel program from disk and initialize • Kernel is located in a pre-agreed address of disk (or floppy disk) • Kernel is loaded into a pre-agreed address of the physical memory • CPU starts the first instruction of the kernel Network Computing Lab.

  39. Design of a simple OS • Kernel initialization • Sets on the supervisor bit (kernel mode bit) • Sets off the interrupt bit • Assume that interrupt does not occur in kernel mode • Simplification to make the kernel design simple • Allocate the Kernel stack • Preparation for Procedure call in kernel • Use add-page() • Make its own page table • Allocate several blocks • Start fromm a specific address, say KERNELPAGEMAP • Make the map • For simplification, use a simple mapping • 0  0, 11, etc • Fill up the PageMapAddress register • With KERNELPAGEMAP • Then, address translation starts Network Computing Lab.

  40. Design of a simple OS • Initialization of initial applications • Assume that initial applications are located in a pre-agreed places on DISK • Use Create-AS() for an application • Allocate several blocks • Use Add-Page() • Starts from a pre-agreed address, say, FIRSTUSER • Read program into the blocks • Make a Page Map • Allocate blocks for Page Map • Make the map, Assign FIRSTUSER virtual address 0 • Make a Stack • From the other end of the address space • Push 0 to the stack • To consider RTE instruction • Switch to the application • Switch-AS() Network Computing Lab.

  41. Kernel Organization: Monolithic Kernel Text Editor Mail Reader File Manager VM Thread Manager Window Manager Network Manager Network Computing Lab.

  42. Kernel Organization: Microkernel Text Editor Window Manager Mail Reader Network Manager File Manager Communication Manager VM Thread Manager Network Computing Lab.

  43. Kernel Organization • Which is better? • Monolithic .vs. Micro-kernel • Any other ways? • E.g., exokernel ???? Network Computing Lab.

  44. Design Project • So far, we designed a simple OS and Simple Kernel based on 4-register with interrupt interpreter model. Then, we want to design a different one. • Give specification of a simple artificial machine and have students design a simple OS for it • We may extend or restrict the “4 –register with interrupt” interpreter model • Eg. A machine • A program can be loaded into at maximum 3 blocks • 5 programs can run at the same time (5 threads) • DISK access operations are given • Give a simple vocabulary • No interrupt in a kernel mode • Size of the physical memory • Block size x number of blocks • Initial applications and the kernel program are located in specific addresses of the disk Network Computing Lab.

More Related