CPU-mechanisms Flashcards

1
Q

One of the central challenges in building an operating system

A

Obtaining high performance while maintaining control

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is direct execution?

A

Run the program directly on the CPU.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How does the OS achieve direct execution of a program on the CPU?

A
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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What would the OS be if we didnt set any limits on running programs?

A

Just a library!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Advantages of direct execution:

A

Fast (program runs natively on the hardware CPU).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Explain what happens when a system call is being initiaded from an user process. Like open() for example.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is user mode?

A

Code that runs in the user mode is restricted in what it can do. Cant issue I/O requests.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is kernel mode?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does the systems provides to programs?

A

Allow the kernel to carefully expose certain key pieces of functionality to user programs, communicating with other processes, allocating more memory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How does the hardware assists the OS?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What happens when a program executes a system call?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What executes the trap?

A

The hardware

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What happens when the hardware executes the trap?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How does the trap know which code to run inside the OS?

A

Kernel sets up a trap table at boot time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What happens at boot?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How does the hardware know what system call to run?

A

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.

17
Q

Can you execute the instruction to tell hardware where the trap tables are in user mode?

A

No, it is a privileged operation. Hardware wont let me.

18
Q

What are the two phases in the LDE protocol?

A

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.

19
Q

What happens when exit()

A

Traps into OS, OS cleans up and we are done.

20
Q

What is a cooperative approach?

A

Waits for system calls in order for OS to regain control over the system.

21
Q

What is a non-cooperative approach?

A

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.

22
Q

How does the OS initialize timer interrupt?

A

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.

23
Q

What is the hardware responsibility when an interrupt occurs?

A

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.

24
Q

Context switch

A

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.

25
Q

What is the context of currently-running process?

A

General purpose registers, PC, kernel stack pointer of currently running proces.

26
Q

Explain what happens during context switch

A

Process A is running and then is interrupted by the timer interrupt. The hardware saves its registers (onto its kernel stack) and enters the kernel (switching to kernl mode). In the timer interrupt handler, the OS decides to switch from running Process A to Process B. At that point, it calls the switch() routine, which carefully saves current register values (into the rpcoess structure of A), restores the registers of Process B(from its process structure entry), and then switches contexts, specifically by changing the stack pointer to use B’s kernel stack. OS returns-from-trap which restores B’s registers and starts running it.

27
Q

How does the OS deal with concurrency when interrupts happens during a system call?

A

Disable interrupt, locking schemes to protect concurrent access to internal data structures.

28
Q

Explain limited direct execution

A

Programs run directly on CPU but are limited in what they can do in the system (not being able to access memory directly requesting I/O).