Modul 4 - Virtuelltminne Flashcards
In which direction does heap and stack grow?
- Heap grows downward
- Stack grows upward
What is the address space?
It’s an abstraction of the physical memory and it’s the programs view of memory in the system.
For example the OS can think that it start at the physical address 0, but instead it is loaded at some arbitrary physical address(es).
What does the address space of a process contain?
It contains all of the memory state of the running program.
- The code of the program (the instructions) (static)
- Stack (used by the program) (grows)
- Heap (used by the program) (grows)
- Other things too such as statically-initialized variables
What does it mean when the OS is visualizing memory?
It’s when the OS gives the program an illusion of being loaded at a particular address (say 0) and has a potentially very large address space (say 64-bits), when the reality is different.
What is transparency (in terms of virtual memory)?
The OS implements the virtual memory in a way that is invisible to the running program. Thus, the program isn’t aware of the fact that memory is virtualized; rather, the program behaves as if it has its own private physical memory.
Goals with the virtual memory?
- Transparency (processes should be
unaware of virtualization.) - Efficiency (make virtualization as efficient as possible both in terms of time and space. Execution should be as close to real execution as possible.)
- Protection (processes should not be
able to interfere with each other or the OS, i.e., affect their memory)
What is the hardware-based address translation?
- Transforming a virtual address into a physical address.
- The relocation of the address happens in runtime and is therefore often referred to as dynamic relocation.
With address translation, the hardware transforms each memory access (e.g., an instruction fetch, load, or store), changing the virtual address provided by the instruction to a physical address where the desired information is actually located.
How does base and bounds/dynamic relocation work?
- Two hardware registers within each CPU: one called base register and one called bounds.
- This base-and-bounds pair allow us to place the address space anywhere in physical memory and ensuring the process only can access its own address space.
- When any memory reference is generated by the process, it’s translated by the processor in the following manner:
- physical address = virtual address + base
Each memory reference generated by the process is a virtual address; the hardware in turn adds the contents of the base register to this address and the result is a physical address that can be issued to the memory system.
- The bounds register is there to help with protection.
Specifically, the processor will first check that the memory reference is within bounds to make sure it is legal.
What is the memory management unit MMU?
- It’s the part of the processor that helps with address translation.
- For example the base and bounds registers are hardware structures kept there.
Hardware requirements for address translation?
- Privileged mode (kernel) - Needed to prevent user-mode processes from executing privileged operations
- Base/bounds registers - Need pair of registers per CPU to support address translation and bounds checks.
- Ability to translate virtual addresses and check if within bounds - Circuitry to do translations and check limits; in this case, quite simple.
- Privileged instruction(s) to update base/bounds - OS must be able to set these values before letting a user program run.
- Privileged instruction(s) to register exception handlers - OS must be able to tell hardware what code to run if exception occurs.
- Ability to raise exceptions - When processes try to access privileged instructions or out-of-bounds memory.
Pros and cons with base and bounds?
Pros:
- Transparent to a process.
- Simple to implement.
- Easy to change process.
- Offers protection
Cons:
- How do we share data?
- Hard to run a program when the entire address space dosen’t fit into memory.
- Wasted memory. (Internal fragmentation). Because there becomes a big chunk of “free” space between the stack and the heap.
OS requirements for address translation?
- Memory management:
- Need to allocate memory for new processes;
- Reclaim memory from terminated processes;
- Generally manage memory via free list
- Base/bounds management - Must set base/bounds properly upon context switch (save and restore)
- Exception handling - Code to run when exceptions arise; likely action is to terminate offending process
What is the process control block (PCB)
It’s a per-process structure where the OS saves and store the values of the base and bounds registers in memory.
How to solve the base-bounds cons? That is support large address space and without wasting “free” space between the stack and the heap?
- Segmentation: Generalized Base/Bounds
How is the idea of segmentation?
Instead of having just one base and bounds pair in the MMU, have a base and bounds pair per logical segment of the address space. That is one for the code, stack and heap of a process. This lets the OS place each segment in different parts of physical memory and avoids filling physical memory with unused virtual address space.
In a 14-bit virtual address, which bits tells the hardware which segment an address refers to? (explicit approach)
The top two bits are used to select the segment. The rest of the bits (12) are the offset.
For example if the top two bits are (01) then the hardware knows the address is in the heap. Thus uses the heap base and bounds.