Presentation is loading. Please wait.

Presentation is loading. Please wait.

Enforcing Modularity Junehwa Song CS KAIST. Network Computing Lab. How to run multiple modules? Emacs X server Mail Reader File Server.

Similar presentations


Presentation on theme: "Enforcing Modularity Junehwa Song CS KAIST. Network Computing Lab. How to run multiple modules? Emacs X server Mail Reader File Server."— Presentation transcript:

1 Enforcing Modularity Junehwa Song CS KAIST

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

3 Designing a Virtual Memory System

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

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

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

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

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

9 Network Computing Lab. 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()

10 Network Computing Lab. 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*/ }

11 Network Computing Lab. 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.

12 Network Computing Lab. 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 Text Editor Mail Reader Create-AS Delete-AS Add-page Delete-page Switch-AS Map

13 Network Computing Lab. 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 로 주어져야 함.

14 Network Computing Lab. Exceptions and Interrupts Interrupt  Maskable Interrupt  Non-maskable interrupt Exceptions  Processor-detected exceptions: Defer in 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 trap by CPU  For System Call and for notification to debugger of a specific event

15 Network Computing Lab. 잠깐만 ! 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

16 Network Computing Lab. 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. ???

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

18 Network Computing Lab. 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 */ }

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

20 Designing a Virtual Processor System

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

22 Network Computing Lab. 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?

23 Network Computing Lab. 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

24 Network Computing Lab. 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 }

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

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

27 Network Computing Lab. 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[]

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

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

30 Network Computing Lab. Design with Sequence Coordination Sequence coordination with simple version... While (input_byte_count <= processed_byte_count) { yield() } … … 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()

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

32 Network Computing Lab. 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} waitingreadyrunning Wait() Notify()Schedule() Yield() Create-thread() Exit-thread()

33 Network Computing Lab. 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() { }

34 Network Computing Lab. 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 ; } }

35 Network Computing Lab. 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; }

36 Network Computing Lab. 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

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

38 Network Computing Lab. 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

39 Network Computing Lab. 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

40 Network Computing Lab. 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()

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

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

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

44 Network Computing Lab. 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


Download ppt "Enforcing Modularity Junehwa Song CS KAIST. Network Computing Lab. How to run multiple modules? Emacs X server Mail Reader File Server."

Similar presentations


Ads by Google