아주대학교 김상훈 교수님의 운영체제 강의와 강의 자료를 바탕으로 작성된 글입니다.
Problems of the Direct Execution Protocol
Q1. How can the OS make sure the program doesn’t do anything that
we don’t want it to do?
Q2. How does the OS stop a running process and switch to another
one?
이 외에도 user/kernel mode의 구분이 없을 땐, user 모드에서 컴퓨터 작동에 치명적인 instr까지 모두 실행이 가능하여 불안정적이라는 문제가 있었음
Privileged Instructions
- A set of instructions that should be executed carefully
- Accessing/manipulating system registers
- Interrupt descriptor table
- Control registers
- Accessing I/O devices
- Memory state management
Multi-mode CPU
- CPU has two modes: user mode and kernel mode
- Privileged instructions are only executable in the kernel mode
- Allow OS to protect itself and other system components
- Modern CPUs support more tha 2 modes
Transition from User to Kernel Mode
- Applications run at the user mode and may transit to the other mode
- By system call
- Synchronous (system call을 요청하는 함수를 부름과 동시에 kernel mode에 진입하므로)
- Usually by software
- By interrupt
- Asynchronous (언제 interrupt가 발생할지 모름)
- Usually by hardware
- Switched to the kernel mode before jumping to the interrupt handler
System Calls
- Programming interface to the services provided by the OS
- POSIX: Portable OS Interface
Transition from User to Kernel Mode
- By system call
- CPU가 interrupt나 system call을 받으면 model change가 됨. (user -> kernel)
- kernel mode의 cpu는 요청받은 system call에 대응하는 번호를 확인
- kernel mode의 cpu는 그 번호에 맞는 service routine을 호출
- 다 끝나면 CPU mode change (kernel -> user)

- 예시
- application이 open이라는 system call을 호출하면 CPU의 모드가 kernel mode로 전환됨. 이 과정에서 open의 system call number가 같이 kernel로 넘어감
- system call number로 system call table을 indexing
(system call table에는 각 system call routine의 시작 주소가 있다.)
- system call body 부분이 실행된다. (application은 이 body 부분이 어떻게 구현됐는지 알 필요가 X)
- system call이 완료되면 kernel은 결괏값을 반환하고 user mode로 전환됨. 이때 application은 system call의 성공 또는 실패 여부를 반환값으로 확인할 수 있음.
