정리 노트

Limited Direct Execution(1) - Trap 본문

개념 정리/운영체제

Limited Direct Execution(1) - Trap

꿈만 꾸는 학부생 2023. 7. 23. 18:01
728x90

이 포스트는 국민대학교 소프트웨어학부 '운영체제' 강의를 듣고 요약하는 포스트입니다. 원하시는 정보가 없을 수도 있습니다. 이 점 유의 바랍니다. 오류 지적은 매우 환영합니다!


Direct Execution

해석하면 '직접 실행'입니다. 이는 성능을 위해서 CPU가 사용자 프로그램이던, 운영 체제던 직접 처리한다는 것을 의미합니다. 조금 다르게 얘기하면 CPU에 있는 PC 레지스터가 가리키는 값이 user code 부분을 가리켜서 CPU가 사용자 프로그램을 직접 실행한다는 이야기입니다.

출처: https://pages.cs.wisc.edu/~remzi/OSTEP/cpu-mechanisms.pdf, 1쪽
(전략)“direct execution” part of the idea is simple: just run the program directly on the CPU.

source: https://pages.cs.wisc.edu/~remzi/OSTEP/cpu-mechanisms.pdf, 2쪽

이를 위해 OS는 프로세스 리스트에 실행시킬 프로세스의 entry를 만들고, 메모리를 할당해 주고, 할당된 메모리에 프로그램을 로딩하고 메인 함수를 실행합니다. 그림에 있는 'argc/argv'는 프로그램을 실행할 때 command line에 작성된 argument들로 이 값들을 초기에 사용자 stack에 올려져야 합니다.

 

하지만 direct execution은 운영 체제가 CPU를 컨트롤할 수 없게 됩니다. 운영 체제는 CPU라는 자원을 각 프로세스에 분배해서 time sharing을 해야 하는데 이를 할 수 없게 됩니다. 또한 direct execution을 하는 경우, I/O와 같이 다루기 민감한 명령들을 권한 확인 등의 과정 및 제한 없이 실행할 수 있습니다.

따라서 컨트롤을 유지하며 성능을 높일 수 있는 방법을 고민했고 나온 방법이 limited direct execution(LDE)입니다.

CPU의 2가지 모드

LDE에서는 CPU는 물리적으로 1개지만 2가지 mode가 있다고 설명합니다.

  • user mode: 사용자 프로그램을 실행하고 있을 때를 이야기하며, 이 모드에서는 I/O 등의 민감한 명령들을 직접 실행할 수 없고, system call을 통해 간접적으로 명령을 수행할 수 있습니다.
  • kernel mode: user mode의 반대의 모드로, I/O를 포함한 모든 명령을 실행할 수 있습니다.
출처: https://pages.cs.wisc.edu/~remzi/OSTEP/cpu-mechanisms.pdf, 3쪽
Thus, the approach we take is to introduce a new processor mode, known as user mode; code that runs in user mode is restricted in what it can do. For example, when running in user mode, a process can’t issue I/O requests; doing so would result in the processor raising an exception; the OS would then likely kill the process.
In contrast to user mode is kernel mode, which the operating system (or kernel) runs in. In this mode, code that runs can do what it likes, including privileged operations such as issuing I/O requests and executing all types of restricted instructions.

Trap

사용자 프로그램에서 작성된 어떤 특수한 명령어들로 인해 CPU가 함정에 빠진 것처럼 user mode에서 kernel mode로 바뀌고 kernel mode에서만 할 수 있는 과정들을 실행하고 다시 user mode로 돌아오는 때도 있습니다. 이러한 상황을 trap에 걸리고, return-from-trap을 통해 빠져나왔다고 표현합니다.

출처: https://pages.cs.wisc.edu/~remzi/OSTEP/cpu-mechanisms.pdf, 4쪽
Special instructions to trap into the kernel and return-from-trap back to user-mode programs are also provided, (후략)

Trap 걸리는 원인들로 3가지가 있습니다.

  • Exception: 예외(Zero Division Error 등), 내부(CPU와 Memory) interrupt(Page Fault 등)를 의미
  • (External)Interrupt: 외부 장지에서 발생한 interrupt
  • System call(SW Trap)

Booting 단계에서 메모리에 trap table이 만들어지고, trap table에는 어떤 trap에 빠졌을 때 어떤 동작들을 해야 하는지 작성한 trap handler의 위치를 작성합니다. Trap table의 위치는 이미 CPU와 약속됐기에 trap에 걸리면 CPU의 PC 레지스터 값이 곧장 trap table을 가리키게 됩니다.

출처: https://pages.cs.wisc.edu/~remzi/OSTEP/cpu-mechanisms.pdf, 4-5쪽
The kernel does so by setting up a trap table at boot time.
(중략)
The OS informs the hardware of the locations of these trap handlers, usually with some kind of special instruction.

source: https://pages.cs.wisc.edu/~remzi/OSTEP/cpu-mechanisms.pdf

위의 그림을 이해하기 위해서는 몇 가지 설명이 더 필요합니다.

메모리는 trap table이 있는 곳을 포함해서 kernel mode에서만 접근할 수 있는 영역을 가지고 있으며, 이 영역에 kernel stack이 있습니다. 여기에 CPU의 register 값들이나 사용자 프로그램의 main 함수 시작 위치를 저장합니다.

OS에서 return-from-trap 명령어가 실행되면, 하드웨어는 kernel stack에 있는 레지스터 값들을 CPU로 옮깁니다.

사용자 프로그램에서 trap에 걸리면 현재 CPU의 레지스터 값들을 kernel stack에 저장하고 PC 값을 trap handler로 가리키게 합니다.(kernel mode로 전환)

728x90