MMU -Linux Memory Managment

What is Virtual Memory?
● Virtual Memory is a system that uses an
address mapping
● Maps virtual address space to
physical address space
– Maps virtual addresses to physical RAM
– Maps virtual addresses to hardware devices
● PCI devices
● GPU RAM
● On-SoC IP blocks

                                                          x86 Physical Memory Map
Two address spaces :
--------------------
● Physical addresses
– Addresses as used by the hardware
● DMA, peripherals
● Virtual addresses
– Addresses as used by software
● Load/Store instructions (RISC)
● Any instruction accessing memory (CISC)



Virtual Memory Details:
-----------------------
● Mapping is performed in hardware
● No performance penalty for accessing alreadymapped
RAM regions
● Permissions are handled without penalty
● The same CPU instructions are used for
accessing RAM and mapped hardware
● Software, during its normal operation,
will only use virtual addresses.
– Includes kernel and user space.

Memory-Management Unit:
-----------------------
● The memory-management unit (MMU) is the
hardware responsible for implementing virtual
memory.
● Sits between the CPU core and memory
● Most often part of the physical CPU itself.
– On ARM, it's part of the licensed core.
● Separate from the RAM controller
– DDR controller is a separate IP block
● Transparently handles all memory accesses from
Load/Store instructions
– Maps memory accesses using virtual
addresses to system RAM
– Maps accesses using virtual addresses to
memory-mapped peripheral hardware
– Handles permissions
– Generates an exception (page fault)
on an invalid access
● Unmapped address or insufficient
permissions


Translation Lookaside Buffer:
-----------------------------
● The TLB is a list of mappings from virtual to
physical address space in hardware
● Also holds permission bits
● There are a fixed number of entires
in the TLB, which varies by CPU.
● The TLB is part of the MMU system.
● TLB is consulted by the MMU when
the CPU accesses a virtual address
● If the virtual address is in the TLB,
the MMU can look up the physical
resource (RAM or hardware).
● If the virtual address is not in the TLB,
the MMU will generate a page fault
exception and interrupt the CPU.
– If the address is in the TLB, but the
permissions are insufficient,
the MMU will generate a page fault.

Page Faults:
-----------
A page fault is a CPU exception, generated
when software attempts to use an invalid
virtual address. There are three cases:
● The virtual address is not mapped for the
process requesting it.
● The processes has insufficient
permissions for the address.
● The virtual address is valid, but
swapped out.
– This is a software condition

Virtual Addresses – Linux:
-------------------------
● Kernel address space is the area above
CONFIG_PAGE_OFFSET.
● For 32-bit, this is configurable at kernel build time.
– The kernel can be given a different amount of
address space as desired
● See CONFIG_VMSPLIT_1G,
CONFIG_VMSPLIT_2G, etc.
● For 64-bit, the split varies by
architecture, but it's high enough:
– 0x8000000000000000 – ARM64
– 0xffff880000000000 – x86_64


+++
● Three kinds of Virtual Addresses
● Kernel:
– Kernel Logical Address
– Kernel Virtual Address
● User space:
– User Virtual Address

Kernel Logical Addresses:
========================
● Kernel Logical Addresses
● Normal address space of the kernel
– kmalloc()
● Virtual addresses are a fixed offset from
their physical addresses.
– Virt: 0xc0000000 → Phys: 0x00000000
● This makes converting between
physical and virtual addresses easy


● Kernel logical addresses can be converted to
and from physical addresses using the macros:
__pa(x)
__va(x)
● For small-memory systems (below ~1G of RAM) Kernel Logical address
space starts at PAGE_OFFSET and goes through the end of physical memory

● Kernel logical address space includes:
● Memory allocated with kmalloc() and
most other allocation methods
● Kernel stacks (per process)
● Kernel logical memory can never be swapped out!
➢ Note that just because all physical addresses could have a kernel logical
address, it doesn't mean the kernel is actually using every byte of memory
on the system.

● Kernel Logical Addresses use a fixed mapping
between physical and virtual address space.
● This means virtually-contiguous regions
are by nature also physically
contiguous.
● This, combined with the inability to be
swapped out, makes them suitable for
DMA transfers.

● For 32-bit large-memory systems (more than
~1GB RAM), not all of the physical RAM can be
mapped into the kernel's address space.
● Kernel address space is the top 1GB of
virtual address space, by default.
● Further, ~104 MB is reserved at the top
of the kernel's memory space for
non-contiguous allocations
– See vmalloc() described later

Thus, in a large memory situation, only the
bottom part of physical RAM is mapped
directly into kernel logical address space.
● Or rather, only the bottom part of physical RAM has a kernel logical address
● Note that on 64-bit systems, this case never happens.
● There is always enough kernel address space to accommodate all the RAM.

Post a Comment

0 Comments