You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MMU(内存管理单元)与虚拟内存:地址转换机制及示例请求

MMU地址转换:一步一步的具体示例

Hey there! I totally get how confusing MMU address translation can feel—even after digging through multiple articles, it often still feels like an opaque process. Let’s break it down with a concrete, number-heavy example that walks you through every step of how the MMU maps a virtual address to a physical one.

先明确几个基础概念(快速回顾)

Before we dive in, let’s make sure we’re on the same page:

  • 虚拟地址: The address your program uses (e.g., from malloc() or hardcoded pointers). It’s not a real location in RAM—it’s an abstraction.
  • 物理地址: The actual location in your computer’s main memory (RAM). Only the MMU knows how to map virtual addresses to these.
  • 页: Both virtual and physical memory are split into fixed-size chunks called pages. For this example, we’ll use a common size: 4KB (4096 bytes). That means each page is 2^12 bytes, so the last 12 bits of any address are the page offset (the position within the page).
  • 页表: A table stored in RAM that the MMU uses to look up which physical page corresponds to a given virtual page. The CPU has a special register called the Page Table Base Register (PTBR) that holds the starting physical address of this table.
  • TLB: A tiny, fast cache in the MMU that stores the most recently used virtual-to-physical page mappings. It’s there to avoid slow trips to RAM for every address lookup.

具体示例:从虚拟地址到物理地址

Let’s use a 32-bit system for simplicity. Here’s our scenario:

  • Virtual address we want to translate: 0x12345678 (hexadecimal)
  • Page size: 4KB (so 12 bits for offset, 20 bits for virtual page number)
  • PTBR (physical address of the start of the page table): 0x80000000
  • Each page table entry (PTE) is 4 bytes (32 bits)

Step 1: Split the virtual address into page number and offset

First, we split the 32-bit virtual address into two parts:

  • Virtual Page Number (VPN): The first 20 bits (since 32-12=20)
  • Page Offset: The last 12 bits

Let’s convert 0x12345678 to hex segments (no need for full binary):

  • Hex: 0x12345 (first 5 hex digits = 20 bits) + 0x678 (last 3 hex digits = 12 bits)
  • So VPN = 0x12345, Offset = 0x678

The offset stays the same for the physical address—because it’s just the position within the page, which doesn’t change when we map virtual to physical pages.

Step 2: Check the TLB first

The MMU always checks the TLB first for the VPN 0x12345. If it’s there (a TLB hit), we get the corresponding Physical Page Number (PPN) immediately. Let’s say in this case, it’s a miss—so we have to go to the page table in RAM.

Step 3: Calculate the physical address of the page table entry

The page table is an array of PTEs. To find the PTE for our VPN:

PTE physical address = PTBR + (VPN * size of each PTE)

Plugging in our numbers:

  • PTBR = 0x80000000
  • VPN = 0x12345
  • Size of PTE = 4 bytes = 0x4

So:

0x80000000 + (0x12345 * 0x4) = 0x80000000 + 0x48D14 = 0x80048D14

That’s the physical address in RAM where our PTE is stored.

Step 4: Read the PTE to get the Physical Page Number (PPN)

Let’s say when we read 0x80048D14, the PTE contains:

  • PPN: 0xABCDE
  • Valid bit: 1 (meaning this page is currently in RAM—no need to fetch from disk)
  • Read/write permissions: 1 (allowing writes to this page)

The valid bit is crucial here—if it was 0, the MMU would trigger a page fault (a CPU interrupt), and the operating system would have to load the page from disk into RAM, update the PTE, and then retry the address translation.

Step 5: Combine PPN and offset to get the physical address

Now we just put the PPN and the original offset together:

Physical address = (PPN << 12) + Offset

Because the offset is 12 bits, shifting the PPN left by 12 bits makes room for the offset at the end.

Calculating:

  • 0xABCDE << 12 = 0xABCDE000 (adding three zeros at the end in hex)
  • Add offset 0x678: 0xABCDE000 + 0x678 = 0xABCDE678

That’s our final physical address! The MMU sends this address to RAM, and the CPU gets the data it needs from that location.


Quick recap of the full flow

  • CPU sends virtual address 0x12345678 to MMU
  • MMU splits into VPN 0x12345 and offset 0x678
  • MMU checks TLB: miss
  • MMU calculates PTE address in RAM using PTBR and VPN
  • MMU reads PTE, gets PPN 0xABCDE (valid bit is 1)
  • MMU combines PPN and offset to get physical address 0xABCDE678
  • MMU sends physical address to RAM, returns data to CPU

If the valid bit was 0, the OS would take over:

  1. OS finds the page on disk
  2. OS finds an empty physical page in RAM (or evicts an unused one)
  3. OS copies the disk page to that physical page
  4. OS updates the PTE with the new PPN and sets valid bit to 1
  5. OS tells the CPU to retry the original instruction

内容的提问来源于stack exchange,提问作者Ali Zain

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 10:33:31