CPU-mechanisms Flashcards
One of the central challenges in building an operating system
Obtaining high performance while maintaining control
What is direct execution?
Run the program directly on the CPU.
How does the OS achieve direct execution of a program on the CPU?
OS: Create entry for process list Allocate memory for program load program into memory set up stack with argc/argv clear registers execute call main()
Program: Run main() Execute return from main()
OS:
Free memory of process,
remove from process list
What would the OS be if we didnt set any limits on running programs?
Just a library!
Advantages of direct execution:
Fast (program runs natively on the hardware CPU).
Explain what happens when a system call is being initiaded from an user process. Like open() for example.
open(), read() is a procedure call, but hidden inside that procedure call is the famous trap instruction. The library uses an agreed-upon calling convention with the kernel to put the arguments to open in well-known locations (stack, or in specific registers), puts the system-call number into a well-known location (onto the stack or a register), and then executes the aforementioned trap instruction. The code in the library after the trap unpacks return values and returns control to the program that issued the system call. The parts of the C library that make system calls are hand-coded in assembly.
What is user mode?
Code that runs in the user mode is restricted in what it can do. Cant issue I/O requests.
What is kernel mode?
OS runs in this mode, code that runs can do what it likes, including privileged operation such as issuing I/O requests and executing all types of restricted instructions.
What does the systems provides to programs?
Allow the kernel to carefully expose certain key pieces of functionality to user programs, communicating with other processes, allocating more memory.
How does the hardware assists the OS?
Different type of access depending on mode. Special instructions to trap into the kernel and return-from-trap back to user-mode programs, as well as instructions that allow the OS to tell the hardware where the trap table resides in memory.
What happens when a program executes a system call?
It executes a special trap instruction. This instruction simultaneously jumps into the kernel and reaises the privilege level to kernel mode; once in the kernel, the system can now perform whatever privilege operations are needed and allowed, and the required work for the calling process, when finished the OS calls a special return from trap instruction.
What executes the trap?
The hardware
What happens when the hardware executes the trap?
The processor will push the program counter, flags, and a few other registers onto a per-process kernel stack; the return fromtrap will pop these values off the stack and resume execution of the usermode program.
How does the trap know which code to run inside the OS?
Kernel sets up a trap table at boot time.
What happens at boot?
OS runs in kernel mode, tells that hardware what code to run when certion execptional events occur. What code should run when a harddisk interrupt takes place, keyboard interrupt, or when a program makes a system all. OS informs the hardware of the location of these trap handlers with some special instruction.
The hardware remebers the location of these handlers until the machine is rebooted.