OS
OS Principles & Practice Volume 1: Kernels and Processes
telomere37
2024. 2. 17. 22:20
해당 글의 목적
석사 졸업 요건 중 하나인 논문제출자격시험의 시즌이 돌아왔다. 지난 1년 반동안 총 세 번의 시험에 떨어진 나로서는 이제 배수의 진을 치고 마지막이라는 마음으로 시험에 임하게 되었다. 매번 OS를 다시 공부할 때 다 아는 내용이지만 까먹은 것들이 틈틈이 숨어있고, 어떤 수업자료를 봐야 모든 내용들을 커버할 수 있을지 감이 잡히지 않았다. 그런 의미에서, 여태까지 공부했던 모든 내용들을 해당 글에 (적어도 내가) 이해하기 쉽게 정리하려고 한다.
Operating Systems : Past, Present and Future
- Batch Operating System - works on a queue of tasks.
- Host operating system (Virutal machine monitor) - Underlying operating systems in virtual machine environment
Kernel Abstraction
- Process data
- Execution stack: state of local variables
- Heap: Dynamically allocated data structures
- Data, Text
- PCB (Process Control Block)
- Stores all the information the operating system needs about a particular process: where it is stored in memory, where its executable image resides on disk, which user asked it to texecute, what privileges the process has, and forth
- Dual-Mode operation (Processor Status Register)
- User Mode: Processor checks each instruction before executing it to verify that it is permitted to be performed by that process
- Kernel Mode: Operating System executes with protection checks turned off
- x86 supports four privilege levels (Not used in MacOS, Windows, Linux)
- Things that hardware should support for protection
- Privileged Instructions: All potentially unsafe instructions are prohibited when executing in user mode
- Memory Protection: All memory accesses unsafe instructions are prohibited when executing in user mode
- MS/DOS did not provide memory protections (Reliability, Security, Portability, Evolvability)
- Early Memory Protection: Base & Bound registers, Check Physical Memory Bounds
- Limitations: Non-expandable, No memory sharing, Exploits physical address, Memory fragmentation
- Address space layout randomization: Randomizing the virtual addresses that a program uses each time it runs
- Timer Interrupts: Regardless of what the process does, the kernel must have a way to periodically regain control from the current process
- MacOS (until 2002) - All applications were told to return their control to the kernel
- Hardware Timer: Interrupt the processor after a specified delay
- Types of Mode Transfer
- User --> Kernel Mode
- Interrupts (Async)
- Alternative option to polling
- Interprocessor interrupts: Used to coordinate actions across the multiprocessor
- Processor Exception (Sync) (Trap: Sync transfer from user to kernel)
- Hardware event caused by user program behavior that causes a transfer of control to the kernel
- Privileged instruction
- Accessing memory outside of its own memory region
- System Call (Sync) (Trap)
- Interrupts (Async)
- Kernel --> User Mode
- New Process
- Resume after an Interrupt, processor exception, or a system call
- Switch to a different process
- User-level upcall
- Safe Mode transfer support
- Limited entry into the kernel
- atomic changes to processor state
- transparent, restartable execution
- Interrupt Vector Table
- x86: 0~31 processor exception, 32~255 interrupts
- Interrupt Stack - Used when interrupt, process exception, or system trap occur
- Per Processor (or even farther, per process) -> for context switch inside interrupt handler, system call handler
- Reliability: The process's user-level stack might be a valid memory address
- Security: Other threads running in the same process might modify the context
- Interrupt masking is a privileged action
- Top half & Bottom half
- Bottom half: Interrupt disabled, fast, notifies the scheduler that the top half should be ran
- Top half
- Example of Interrupt in x86
- Mask Interrupt
- Save User stack pointer (SS, ESP register) (stack segment, stack pointer)
- Save PC (CS, EIP register) (code segment, instruction pointer)
- Save Processor Status (EFLAGS)
- The stack pointer, PC, Processor status is first saved in internal hardware registers
- Switch to kernel interrupt stack, and saves them (trapframe)
- Optionally save an erro code
- Jump to interrupt vector table
- Handler starts running
- pushad instruction - save the remaining registers onto the kernel stack (32bit integer registers)
- <done handling>
- popad - pop all integer registers (pops the error code too)
- iret - return from interrupt (SS, ESP, CS, EIP, EFLAGS poped)
- +) the instruction pointer could be increased prior/subsequent (if it was a synchronous interrupt such as trap)
- SPARC Architecture
- Register Window - hardware stack, switch window when context switch(interrupt)
- Fast mode switch but slow context switch
- Precise Interrupt
- Hardware first completes all instructions that occur, in program order, before the interrupted instruction. The hardware annuls any instruction that occurs, in probram order, after the interrupt or trap, even if the instruction is in progress when the processor detects the interrupt.
- System Call Kernel Stub
- Locate system call arguments - If there are arguments in the user stack, verify and translate the virtual address to physical address and pass it to the system call handler
- Validate Parameters
- Copy before check - prevent time of check vs time of use (TOCTOU) attack
- Copy back any results
- Kernel actions to start a process
- Copy arguments into user memory
- Transfer control to user mode
- upcall
- Unix - signals
- Can use a signal stack allocated by the user
- Windows - Async events
- virtualized interrupts and exceptions
- Usage
- Preemptive user-level threads
- Asynchronous I/O notification
- Interprocess communication
- User-level exception handling
- User-level resource allocation
- Unix - signals
- BIOS (Basic Input/Output System)
- Why not put the whole OS inside BIOS? 1) Update is hard 2) ROM is slow, expensive
- Boot stage
- BIOS copies bootloader (needs to read raw bytes from disk)
- Bootloader copies OS (needs to be aware about the file system)
- OS copies login applications
- User --> Kernel Mode
- Hardware support for OS
- Privilege levels
- privileged instructions
- memory translation
- processor exceptions
- timer interrupts
- device interrupts
- interprocessor interrupts
- interrupt masking
- system calls
- return from interrupt
- Boot ROM
- Atomic read -modify-write instructions
- Types of Kernel
- Microkernel - extreme version of approach, to isolate privileged but less critical parts of the OS from the rest of the kernel
- Monolithic Kernel - Most of the os functionality is linked together inside the kernel
- Hardware abstraction layer
- Dynamically loaded device drivers
- Code inspection
- bug tracking
- user-level device drivers
- virtual machine device drivers
- driver sandboxing
- Starting a new process
- fork - creates a new process
- exec - does not create a new process
- wait - pauses the parent until the child finishes, crashes or is terminated
- dup2 - Modify the file descriptor
- Interprocess communication (IPC)
- Producer-Consumer
- Client-Server
- File system