1. The Golden Rule of OS
Never trust the user's code. All hardware access must be restricted.
- If a Python script had direct access to the hard drive, a bug could accidentally overwrite the operating system itself.
- To prevent this, the OS isolates code into two physical states: User Space and Kernel Space.
2. The Dual-Mode CPU & Registers
Protection is enforced by the physical CPU Hardware.
- Mode Bit (1): User Mode. The CPU will refuse to execute hardware commands.
- Mode Bit (0): Kernel Mode. "God Mode". The CPU executes anything.
- Program Counter (RIP): Points to the exact line of code currently executing.
- Stack Pointer (RSP): Tracks active functions. Crucially, User Space and Kernel Space have completely separate physical Stacks!
3. The System Call (Syscall) / Trap
How does a user program read a file if it's restricted?
- The program triggers a Trap (a deliberate software exception).
- The CPU saves the User Stack, flips the Mode Bit to 0, jumps to the Kernel Stack, verifies permissions, does the work, and hands the data back.