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.
How does the hardware know what system call to run?
A system-call number is assigned to each system call. The user program places this number onto a register or at a specific location on the stack; OS examines this number, ensures it is valid, executes the corresponding code.
Can you execute the instruction to tell hardware where the trap tables are in user mode?
No, it is a privileged operation. Hardware wont let me.
What are the two phases in the LDE protocol?
At boot time the kernel initialized the trap table, and CPU remembers its location. The kernel does so via a privileged instruction.
When running a process, the kernel sets up a few things before using a return-from-trap to start the execution of the process; this switches the CPU to user mode and begins running the process.
What happens when exit()
Traps into OS, OS cleans up and we are done.
What is a cooperative approach?
Waits for system calls in order for OS to regain control over the system.
What is a non-cooperative approach?
Hardware feature of timer interrupt is an essential feature in helping the OS maintain control of the machine. When interrupt is raised, current process is halted, and a pre-config interrupt handler in the OS runs.
How does the OS initialize timer interrupt?
At boot time, trap table is installed and an privilege instruction is run by the OS in kernel mode which tells the hardware where trap table is located. OS then starts the timer.
What is the hardware responsibility when an interrupt occurs?
Save enough of the state of the program that was running when the interrupt occured, so return-from-trap instruction will be able to resume the running program correctly.
Context switch
Low-level piece of code. All the OS has to do is save few register values for the currently-executing process (onto its kernel stack, for example) and restore a few for the soon-to-be-executing process. By doing so, the OS thus ensures that when the return from trap is executed the system resumes execution of another process.