enforcing modularity
DESCRIPTION
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. ……. …. - PowerPoint PPT PresentationTRANSCRIPT
Enforcing Modularity
Junehwa Song
CS
KAIST
Network Computing Lab.
How to run multiple modules?
Emacs
X server
Mail Reader File Server
Designing a Virtual Memory System
Network Computing Lab.
Can multiple Modules share MM?
Partition MM space?
Intentional/unintentional intrusion LOAD STORE
…
…
…
Emacs
X server
Mail……
Network Computing Lab.
Then, What Can We Do???
PROCESSOR Main Memory
Address
Read/Write
Data
PROCESSOR Main MemoryRead/Write
Data
Virtual MemoryManager
Virtual Address
PhysicalAddress
Read/Write
Network Computing Lab.
Address Translation
Page Map (Page Table) Virtual Address Space Segmentation
int PageTable[size]int translate(int virtual) /* HW */
Address TranslationPage #
Block #
Byte Offset
Byte Offset
Block #
Virtual address
Physical address
Page Map (Page Table)
BTW, where do you want to put Page Map?
Network Computing Lab.
Can multiple Modules share MM?
PROCESSOR
Read/Write
Data
Virtual MemoryManager
Virtual Address
Read/Write
300Page Map Address Register
Page # Offset Block #Offset
Physical Address
0
100
200
300
400
500
101112
1004000
1213
100800
Page Map of VM1
Page Map of VM2
Page 10 (VM1)
Page 10 (VM1)
Page 12 (VM2)
Page 11 (VM1)
Unused block
Thus, a Page Map defines an address space
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()
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*/}
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.
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 spacePut all page maps as well as the virtual memory manager programs in KERNELDefine 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
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 로 주어져야 함 .
Network Computing Lab.
Exceptions and Interrupts
Interrupt Maskable Interrupt Non-maskable interruptExceptions 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
Network Computing Lab.
잠깐만 ! Our Interpreter Model
“4-register with Interrupt” Interpreter Model4 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.
Things to do to enter/leave Kernel
The following can be common to three different kernel access methodsLet’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.
Mode-Change Operations
Very expensive operation Number of instructions Invalidate or Clean up things (e.g.,
pipeline, cache, etc)
Network Computing Lab.
Switching address spaces
Switch-AS(ID) {1. Change the address space to ID; 2. Load page map address register with the address of
PAGEMAP(ID); }
only kernel can change page map address register, thus
Switch-AS(ID) {1. Change to KERNEL; /* change-mode-enter-kernel */2. Load page map address register to PAGEMAP(ID); 3. Change to the address space, ID; /*change-mode-
leave-kernel */}
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?
Designing a Virtual Processor System
Network Computing Lab.
Abstraction of a Module in Execution
Running multiple modules in a processor?
Module A Module B
Temporarily stop Start execution
Temporarily stopResume execution
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?
Network Computing Lab.
Thread
An abstraction of a running program and its stateWe 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.
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() {1. Save the current value of SP (current stack pointer) to
ThreadTable[]2. Select next thread to run // this part is a “scheduling” task3. Load the stack pointer of the next thread to SP}
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
200
0
1042
200
200
100
0
Thread 6 in execution yield Thread 0 resumes
Network Computing Lab.
First Trial!!! (we still need a bit more)
Creat-Thread()Exit-Thread()Destroy-Thread()
Network Computing Lab.
First Trial!!!
Create-Thread (module_address)1. Allocate space for a new stack2. Initialize the stack
1. Push the address of “exit-thread()”2. Push the address of (start of) module
3. Initialize the entry in the Thread-Table[]
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
61042 200
200
0
1042
100
200
0
….
Stack for Thread 7
300
392
396
388 7
….388
2048
Address of “exit-thread()”
Network Computing Lab.
First Trial!!!
Exit-Thread ()1. De-allocate space for a stack2. De-allocate the entry in the
Thread-Table[]
Network Computing Lab.
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.
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.
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}
waiting ready running
Wait()
Notify() Schedule()
Yield()
Create-thread()
Exit-thread()
Network Computing Lab.
Virtual Thread Manager with Wait and Notify
Struct thread {int SP ; //value of the stack pointerInt state ; //wait, ready, or runInt *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.
Virtual Thread Manager with Wait and Notify
Struct thread {int SP ; //value of the stack pointerInt state ; //wait, ready, or runInt *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.
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.
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.
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
Network Computing Lab.
Design of a Simple OS
Initialization Sequence Reset Boot Kernel initialization Initialization of initial applicationsReset Turn on of computer Load boot program from ROM
Virtual and physical address 0Boot 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.
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, 11, etc
Fill up the PageMapAddress register With KERNELPAGEMAP Then, address translation starts
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()
Network Computing Lab.
Kernel Organization: Monolithic Kernel
Text Editor Mail Reader
VM File Manager
Window Manager Network Manager
Thread Manager
Network Computing Lab.
Kernel Organization: Microkernel
Text EditorMail
Reader
VM
File Manager
Window Manager
Network Manager
Thread Manager
Communication Manager
Network Computing Lab.
Kernel Organization
Which is better? Monolithic .vs. Micro-kernel
Any other ways? E.g., exokernel ????
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 itWe may extend or restrict the “4 –register with interrupt” interpreter modelEg. 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