Memory Management
Address-space concept
- Protect processes from one another
- Protect the OS from user processes
- Provide efficient management of available storage
Virtual Address
-
Who uses virtual address?
- user processes
- kernel processes
- pretty much every piece of software
-
Who uses physical address?
- anything sitting on the bus & hardware
- The OS manages the physical address space by using the buddy system
-
Why would you want to access a memory location?
- To fetch a machine instruction
- EIP (on an X86 machine), which contains a virtual address
- To push EAX onto the stack
- You need to specify a memory location to store the content of EAX
- How do you know which memory location to write to?
- ESP, which contains a virtual address
- x = 123, where x is a local variable
- EBP, which contains a virtual address
-
Is there any CPU register that contains a physical address? NO
Address Translation
- Memory Management Unit (MMU)
- attached to the bus
- translates virtual address into physical address
- MMU uses registers associated with the process the inner core is using
- Can be used for
- protection/isolation
- illusion of large memory
- sharing
- new abstraction
Memory Fence
- MMU uses a register that serves like a fence
- if virtual address < fence
- physical address == virtual address
- else
- MMU gives fault, traps into the OS, OS kills the program
- What if the user program won't fit in memory?
- use overlays (piece of program)
- programs are divided into overlays
- only one overlay can be in physical memory at a time
Base and Bounds Registers
-
OS maintains a pair of base and bounds registers for each user process
- bounds register: address space size of the user process
- base register: start of physical memory for the user process
-
Address relative to the base register
-
Virtual memory reference >= 0 and < bounds, independent of base (this is known as position independence)
-
MMU registers are part of the context of a process
Generalization of Base and Bounds: Segmentation
- One pair of base and bounds registers per segment
- code, data, heap, stack, and may be more
- e.g. compiler compiles programs into segments
Access Control with Segmentation
- Add every bit of Read-only or Read/Write to base & bound register pair in MMU
Sharing segments
- Can simply setup base and bounds registers to share segments
- Read-only can be shared
- Read-write should have their own segments
Segmentation Fault
Cases
1. Virtual address is not within range of any base-bounds registers
2. Access is incompatible (e.g. Write to read-only)
Memory Mapped File
- the
mmap()
system call
- can map an entire file (or part of it) into a segment
- extra base and bound register is needed
Copy-On-Write
- A process gets a private copy of the segment after a thread in the process performs a write for the first time
Steps
1. When the process performs a write, the OS gets fault and sees that the memory segment is private, it does not generate a seg fault
2. It takes the data and makes a copy of it
3. Then changes the address to a new address of the copy, and changes from RO to RW
4. The user program tries again to write to the new data (private copy)