Motivation
When changed from state to another state or interrupted, operating system(OS) must maintain current context for OS to fulfill the consistency.
In particular, we have to save hardware context which involve in CPU registers and I/O registers.
Intuition
- All of the change of context is made of occurring Interrupt. Moreover, Context Switching is a kind of Interrupt Service Routine. Whenever Interrupt occurs, we have to save all of the registers known as a state vector which involve a program counter, stack pointer and so on, to PCB of the current process.
- There is a context switch function in kernel memory, that can allocate CPU resource.
- In Linux system, context switch function is known as schedule function and When calling system call such as read, wait, exit it invokes schedule function.
Review
- Interrupt occurs in anytime, even if system is in the smallest process step such as ADD instruction. Because after finishing every instruction, in hardware mechanism, system checks the interrupt bit that can know whether interrupt occurs or not.
- Every program needs two stacks, Kernel Stack which involve PCB and User Stack used for function. To call a kernel function like system call, a kernel stack always resides in the the memory of kernel space.
- Process State(context) consists of Memory Context which has data, stack and heap segment and Hardware Context which has CPU registers, I/O registers and Kernel Context(System Context)
- Hardware Context is preserved by Context Switching and Memory Context is preserved by Swapping which take it from memory to disk when the degree of multiprogramming is up to the maximum.
Mechanism
based on MIPS.
Step 1
- OSPCBCur : the global variable which operating system has, points the current process of PCB.
- StkPtr(OS-PCB) : the perspective of Operating System.
- CPU's SP : the perspective of CPU.
- MEM : Kernel Stack.
Step 2
Interrupt Occurrence!
As soon as occurring, in hardware mechanism, system pushes PSW(Program Status Word) and Return Address made of SEG task and OFF task. Because of Segmented Addressing for Intel Processor, there are two words.
Before running Interrupt Service Routine, hardware components have a minimum support that they push PSW and Return Address to Kernel Stack(PCB) in order for Operating System to run a dispatcher function smoothly.
Step 3
Interrupt Service Routine is executed!
By using PUSH instruction, the rest of all registers are pushed. As far as here, it is the completion of context save.
Step 4
Allocate StkPtr!
Now, Stack Pointer is saved at PCB involved in Operating System.
Step 5
If interrupt is a timer, after saving context, operating system invokes scheduler which points PCT of the next process.
OSPCBCur points next StkPtr!
Step 6
Allocate CPU's SP!
Now, CPU understands where stack pointer is and then CPU executes POP instruction that can move all registers from kernel stack to CPU.
Step 7
At the same time as popping Return Address, the new process can run.
Return Address points User Stack, exactly points the next one from the instruction executed in last time.
Extra Step
In order to have the structure like that, all of the process executes at least one more ISR. When OS creates process, it make process to have a fake stack like that, as if the process is given Context Switching only once.
Logical View
하드웨어와 소프트웨어의 조합이 인상적이다. 빠른 시간이 요구되는 매커니즘은 하드웨어가 지원해주고, 2가지 패러다임(하드웨어,소프트웨어)을 상호보완적으로 사용하면서 복잡한 컴퓨터 신호의 흐름들을 원활히 제어한다. 컴퓨터 시스템은 가격과 속도 측면에서 하드웨어와 소프트웨어가 가장 잘 조합된 구조라고 생각한다.
Reference
[1] 운영체제의 기초 (the basic of operating system)
댓글
댓글 쓰기