I/O Memory Management and Virtualisation

A device that writes to memory by itself needs a guard of its own. The guard gives each device a small view of memory, through a table that the device cannot change. Two such tables in a row keep a whole operating system in its box.

You Will Be Able To

  • Translate an address by hand: swap the page number, keep the offset.
  • Say what stops a write of a device before memory, and how to prove that it was stopped.
  • Follow one address through two tables, and say who wrote each one.
Lesson
S10.7
Time
28 min
Runs on
Your browser
Check
5 questions

Two operating systems share one phone, and the camera belongs to one of them. Its driver gives the camera a buffer at 403c.

The other system has a 403c too. One frame later, each must still find only its own data there.

Before You Start

You need High-Speed I/O Subsystems: a controller writes to memory by DMA, at an address that software gave it.

You need DMA: a DMA controller is a manager of its own on the bus.

You need Addresses and Memory-Mapped I/O: addresses in hex.

You need Virtual Memory: Pages and Frames: pages, the offset, a page table and the fault. It helps to know The TLB: a cache of translations.

It helps to know Security Architecture: a manager that software can program acts for that software.

Five questions this page answers
Question
1What does a translation change in an address, and what does it keep?
2Why does the DMA of a device need a guard of its own?
3What tells the IOMMU which tables to use for a request?
4Why are two tables needed when several operating systems share a chip?
5A device writes one byte further on. What does the lab print last?

The Idea: A View of Memory for Each Device

The controller of a fast port writes its data to memory by itself, at an address that software gave it. Nothing so far checks that address. One wrong value in one register, and a frame of the camera lands on the data of another program.

The cure is to give each device a small view of memory of its own. The device uses addresses of its own. A block on the way to memory turns each one into a real address, with a table that trusted software wrote. An address that is not in the table goes nowhere. That block is the IOMMU.

The same block solves a second problem. A large chip can run several operating systems at once, each in a box of its own. Each believes that it owns the machine, and each hands its devices addresses that it made up.

So there is a second table, kept by the software beneath them all. It turns the made-up addresses into real ones. With two tables in a row, an operating system can use its camera directly and still cannot leave its box.

New Words

New words on this page
WordMeaning
Page of memoryOne of the equal pieces that memory is cut into. Here a page holds 4096 bytes, which is 1000 in hex. Page for short.
Page numberThe hex digits of an address in front of the last three. They say which page.
Offset in a pageThe last three hex digits of an address. They say where a byte lies inside its page. Offset for short.
Page tableA list that gives, for a page number, the page number to use in its place. One line of it is an entry.
Address translationSwapping the page number of an address for the one in the table. Translation for short.
Translation faultThe refusal of an access whose page has no entry, or whose entry does not allow it. Fault for short.
Physical addressA real address: the one that memory itself sees.
I/O virtual address (IOVA)The address that a device uses, before any translation.
Stream IDA number that travels with every request of a device and says which device it is. Stream for short.
Translation contextThe tables that belong to one stream. Context for short.
Virtual machineA make-believe computer, made by software. An operating system runs inside it as if it were alone.
Guest systemAn operating system that runs inside a virtual machine. Guest for short.
HypervisorThe software beneath the guests. It shares the real chip between them.
Guest physical addressWhat a guest takes for a real address. It is real only inside the box of that guest.
Stage 1, stage 2The two translations in a row: first the table of the guest, then the table of the hypervisor.

A Reminder: Pages and Page Tables

Memory is cut into pages of 4096 bytes. A page table swaps the page number of an address, and the offset stays. The address 403c, with page 4 in frame 9, becomes 903c.

All of this is taught in Virtual Memory: Pages and Frames. The cache of translations that this page mentions later is The TLB.

One Picture: One Write on Its Way to Memory

One Write on Its Way to MemoryStep through the picture. It follows one write of the camera through the IOMMU, and then shows who wrote each table.
  • Processor
  • Peripheral
  • Bus
  • Memory
One Write on Its Way to MemoryStep through the picture. It follows one write of the camera through the IOMMU, and then shows who wrote each table. component Guest one operating systemin its box component Hypervisor the software beneathevery guest component Camera controller writes by DMA, asstream 3 component Stream lookup finds the tables ofthis stream component Stage 1 table page 4 becomes page2 component Stage 2 table page 2 becomes page80009 component Memory sees only realaddresses stream 3, address403ctables found203c8000903cwrites the entrieswrites the entries
  1. component Camera controllerwrites by DMA, as stream 3
    • stream 3, address 403c → Stream lookup
  2. component Stream lookupfinds the tables of this stream
    • tables found → Stage 1 table
  3. component Stage 1 tablepage 4 becomes page 2
    • 203c → Stage 2 table
  4. component Stage 2 tablepage 2 becomes page 80009
    • 8000903c → Memory
  5. component Memorysees only real addresses
  6. component Guestone operating system in its box
    • writes the entries → Stage 1 table
  7. component Hypervisorthe software beneath every guest
    • writes the entries → Stage 2 table
  1. The request

    The camera controller writes one byte. The request carries the number of its stream, 3, and the address that software gave it, 403c.

  2. Whose tables

    The stream number picks the tables. A stream that has none gets a fault here, and nothing more happens.

  3. Stage 1

    The first table swaps page 4 for page 2. The offset 03c stays. The result, 203c, is not yet a real address.

  4. Stage 2

    The second table swaps page 2 for page 80009. A page with no entry here ends in a fault, and memory sees nothing.

  5. Who wrote the tables

    The guest wrote the first table, and the hypervisor wrote the second. The device wrote neither.

Read the diagram as text
  • Guest: one operating system in its box
  • Hypervisor: the software beneath every guest
  • Camera controller: writes by DMA, as stream 3
  • Stream lookup: finds the tables of this stream
  • Stage 1 table: page 4 becomes page 2
  • Stage 2 table: page 2 becomes page 80009
  • Memory: sees only real addresses
  • Camera controller → Stream lookup: stream 3, address 403c
  • Stream lookup → Stage 1 table: tables found
  • Stage 1 table → Stage 2 table: 203c
  • Stage 2 table → Memory: 8000903c
  • Guest → Stage 1 table: writes the entries
  • Hypervisor → Stage 2 table: writes the entries

The IOMMU of this page is the three boxes between the camera controller and memory. The numbers are those of the lab, in hex.

The Smallest Example, Worked by Hand

No simulator yet. The camera is stream 3, and only stream 3 has a context. Stage 1, the table of the guest, has two entries: page 4 becomes page 2, and page 5 becomes page 3. Stage 2, the table of the hypervisor, has one: page 2 becomes page 80009.

Three requests, by hand
StreamAddressContextAfter stage 1After stage 2Result
3403cfound203c8000903cwritten
3503cfound303cno entry for page 3fault, nothing written
7403cnonefault, nothing written

Look at the second row. Stage 1 has nothing against it: a guest may map its page 5 as it likes. But the hypervisor never gave this guest a real page for its page 3. Stage 2 is where the box closes.

The third row fails earlier still. The address is the good one of the first row, and it does not help. A stream without a context is turned away before any table is read.

The Rules

  1. Rule 1: A translation swaps the page number and keeps the offset.

    A byte keeps its place inside the page. Only the page moves.

  2. Rule 2: Every request of a device carries its stream ID, and the stream chooses the tables.

    Hardware sets the stream ID. If software could change it, any driver could claim to be the camera.

  3. Rule 3: No entry, no access.

    A fault stops the request inside the IOMMU. Memory never sees it.

  4. Rule 4: With guests there are two stages, and both must allow the access.

    Stage 1 belongs to the guest. Stage 2 belongs to the hypervisor, and it has the last word.

  5. Rule 5: The device writes neither table.

    It can only use the view that trusted software drew for it.

  6. Rule 6: The proof of a refusal is at the target.

    Count the writes that memory accepted. An address on a refused request proves nothing.

Sources for These Rules

Pages, the page table, the offset in a page and the fault: Patterson and Hennessy, Computer Organization and Design, ARM edition, section 5.7.

Virtual machines, the guest and the hypervisor, which that book also calls a virtual machine monitor: the same book, section 5.6.

A block that translates the addresses of managers other than the processor: Arm AMBA AXI Protocol Specification, IHI 0022 issue L, section A13.1. Arm documents call it a system memory management unit.

A stream ID that picks the context: the same document, section A13.4.2.

Two stages of translation for a device under a hypervisor are common usage. No IOMMU specification is in the library of this site, and the page cites none.

Now Run It

The lab at the end of this page makes the three requests of the hand table. Its IOMMU is a small circuit with the tables built in, and it takes one tick for each table. A bench plays the camera and the memory.

The First Request, Tick by Tick
0123clkrequeststate0123writefault
Scroll horizontally ↔
  1. Cycle 0

    The request is offered. The IOMMU sees stream 3 and finds a context.

  2. Cycle 1

    State 1: stage 1. Page 4 becomes page 2, and the offset 03c is kept.

  3. Cycle 2

    State 2: stage 2. The hypervisor has an entry for page 2.

  4. Cycle 3

    State 3: only now does write go to 1. Memory sees one write.

Rising-edge samples from the runnable Icarus lab. Step through one request.

Printed by the lab on Icarus: for each request the offer, the result, and what memory saw. The rows of the figure are left out here.
offer stream=3 iova=403c
result write=1 fault=0 physical=8000903c
target writes=1 sram[903c]=63
offer stream=3 iova=503c
result write=0 fault=1 physical=00000000
target writes=1 sram[903c]=63
offer stream=7 iova=403c
result write=0 fault=1 physical=00000000
target writes=1 sram[903c]=63

Rule 1 at work, in the first result row. The offer says 403c, and the result says physical=8000903c. The page changed twice, and 03c rode along.

Rule 3, in the second result row. The offer of 503c ends with write=0 and fault=1. The target row after it still says writes=1.

Rule 2, in the third offer. Stream 7 uses the good address 403c and is refused all the same.

Rule 6, in every target row. The count stays at 1, and the byte at 903c stays 63. That is the proof. The physical=00000000 of a refused request is only what the lab drives while no write is sent.

This lab leaves things out. It has one guest, and a real chip has several, each with a table of its own at stage 2. Its tables cannot be written at all. It makes no interrupt.

Why It Is Built This Way

ChoiceThe alternativeIts cost
A guard on the way of the deviceCheck the register write of the driverThat check sees the core write a number. The DMA write comes later, from another manager, and passes unchecked.
Translation, and not only a yes or noA list of allowed real addressesEvery driver would need to know the real map. With a table, a guest can use any addresses it likes.
Two stagesOne table, written by the hypervisor aloneEvery change of a buffer by a guest would be a call into the hypervisor. With two stages the guest edits its own table.

The price is time. Each table that must be read delays the access, so real chips remember the last answers. That memory is one more thing to clear when a page changes hands.

The Trap: A Fault That Looked Like a Write to the Boot ROM

A trace shows a request from the camera with physical=00000000. Address 0 is the Boot ROM. Somebody reports that the camera tried to overwrite the boot code, and a day goes into the wrong hunt.

Nothing was written. The request had ended in a fault, and the address wires were idle. The write signal was 0, and the count at memory had not moved. Read rule 6 again.

The second trap trusts stage 1 alone. The guest wrote that table, so it proves only what the guest wanted. A guest physical address sent straight to memory lets a guest name any page it likes.

The third trap is direction. A buffer that a guest may read is not a buffer that its camera may write. An entry needs the right for the access that is made, at both stages.

At Scale

This lab1 stream, 3 entries
ap_phonea stream for each device that writes to memory

This section looks ahead, to tables in memory, to interrupts for a guest, and to a page that changes hands.

  1. Tables in memory

    Real tables live in memory and have several levels. Reading them is called a table walk. A small cache in the IOMMU keeps the last translations, so that most requests need no walk.

  2. The news goes another way

    The frame lands through the IOMMU. The interrupt that reports it takes its own route. The hypervisor must deliver it to the guest that owns the camera, and to no other.

  3. A page changes hands

    Before a page goes to another guest, stop the old stream and wait for its last write. Then remove the entry, and clear every remembered translation. Skip one step and an old frame lands in the new owner's data.

  4. One page too far

    A write that runs over the end of a page continues on the next page number. That page may have no entry. Check every page that a buffer touches, before the first byte.

How the secure part of a chip is kept apart continues in The Secure World.

Try It

Run the lab. Its rows that start with e belong to the stepped figure of this page. Read the other nine.

Change the first address from 403c to 4ffc. Predict the physical address in the result row.

Give the second request the stream 7. Predict whether it now fails earlier or later than before.

Names Printed by the Lab
NameMeaning
offerA request of a device: its stream, and the address it uses, as iova.
resultWhat the IOMMU decided. Exactly one of write and fault is 1.
physicalThe address sent on to memory. It is 0 while no write is sent.
targetWhat memory saw: the count of writes so far, and the byte at 903c.
request, stateIn the e rows: a request is offered, and where the IOMMU is. 0 waits, 1 is stage 1, 2 is stage 2, 3 sends the write.
Sources and What the Lab Leaves Out

The lab holds its two tables as a few fixed lines of a small circuit, capture_filter. A real IOMMU reads its tables from memory. Entries here allow reads and writes alike.

Stream numbers, page numbers and the one tick for each table are choices of the lab. The real address is in the SRAM of ap_soc, because the lab borrows that map. The address map of ap_phone is not drawn in this module.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: offer stream=3 iova=403c e0 request=1 state=0 write=0 fault=0 e1 request=0 state=1 write=0 fault=0 e2 request=0 state=2 write=0 fault=0 result write=1 fault=0 physical=8000903c e3 request=0 state=3 write=1 fault=0 target writes=1 sram[903c]=63 offer stream=3 iova=503c result write=0 fault=1 physical=00000000 target writes=1 sram[903c]=63 offer stream=7 iova=403c result write=0 fault=1 physical=00000000 target writes=1 sram[903c]=63
Read included file: soc-s107-dma.sv
// A page lookup per model step. Addresses count bytes; requests write one byte.
// Context 3 belongs to the capture guest. Both guest pages are writable at S1.
module capture_filter (
  input logic clk, reset, request,
  input logic [3:0] stream,
  input logic [15:0] iova,
  output logic bus_write, fault,
  output logic [31:0] physical
);
  typedef enum {IDLE, GUEST, HOST, ISSUE, DENY} state_t;
  state_t state;
  logic [15:0] saved;
  logic [3:0] guest_page;

  always_ff @(posedge clk) begin
    if (reset) state <= IDLE;
    else case (state)
      IDLE: if (request) begin
        saved <= iova;
        state <= stream == 3 ? GUEST : DENY;
      end
      GUEST: case (saved[15:12])
        4, 5: begin
          guest_page <= saved[15:12] - 2;
          state <= HOST;
        end
        default: state <= DENY;
      endcase
      HOST: state <= guest_page == 2 ? ISSUE : DENY;
      ISSUE, DENY: state <= IDLE;
    endcase
  end
  assign bus_write = state == ISSUE;
  assign fault = state == DENY;
  assign physical = bus_write ? {20'h80009, saved[11:0]} : 32'b0;
endmodule

Check Yourself

Predict the outputIn this run the first request uses the address 403d. The lab adds one row at the end. What is it? Its form is: final writes=.. sram[903c]=..
module tb;
  bit clk, reset, request;
  logic [3:0] stream;
  logic [15:0] iova;
  wire bus_write, fault;
  wire [31:0] physical;
  capture_filter filter (.*);
  always #5 clk = !clk;
  byte unsigned sram [0:65535];
  int writes, edge_no;
  bit trace_first = 1;

  always @(posedge clk)
    if (trace_first && (request || filter.state != filter.IDLE)) begin
      $display("e%0d request=%b state=%0d write=%b fault=%b", edge_no, request, filter.state, bus_write, fault);
      edge_no++;
    end

  always @(posedge clk)
    if (bus_write && physical[31:16] == 16'h8000) begin
      sram[physical[15:0]] = 8'h63;
      writes++;
    end

  task automatic capture(input logic [3:0] id, input logic [15:0] addr);
    @(negedge clk);
    stream = id;
    iova = addr;
    request = 1;
    $display("offer stream=%0d iova=%04h", id, addr);
    @(negedge clk);
    request = 0;
    while (!bus_write && !fault) @(negedge clk);
    $display("result write=%b fault=%b physical=%08h", bus_write, fault, physical);
    @(negedge clk);
    trace_first = 0;
    $display("target writes=%0d sram[903c]=%02h", writes, sram[16'h903c]);
  endtask

  initial begin
    reset = 1;
    @(negedge clk); reset = 0;
    capture(3, 16'h403d);
    capture(3, 16'h503c);
    capture(7, 16'h403c);
    $display("final writes=%0d sram[903c]=%02h", writes, sram[16'h903c]);
    $finish;
  end
endmodule
Read included file: soc-s107-dma.sv
// A page lookup per model step. Addresses count bytes; requests write one byte.
// Context 3 belongs to the capture guest. Both guest pages are writable at S1.
module capture_filter (
  input logic clk, reset, request,
  input logic [3:0] stream,
  input logic [15:0] iova,
  output logic bus_write, fault,
  output logic [31:0] physical
);
  typedef enum {IDLE, GUEST, HOST, ISSUE, DENY} state_t;
  state_t state;
  logic [15:0] saved;
  logic [3:0] guest_page;

  always_ff @(posedge clk) begin
    if (reset) state <= IDLE;
    else case (state)
      IDLE: if (request) begin
        saved <= iova;
        state <= stream == 3 ? GUEST : DENY;
      end
      GUEST: case (saved[15:12])
        4, 5: begin
          guest_page <= saved[15:12] - 2;
          state <= HOST;
        end
        default: state <= DENY;
      endcase
      HOST: state <= guest_page == 2 ? ISSUE : DENY;
      ISSUE, DENY: state <= IDLE;
    endcase
  end
  assign bus_write = state == ISSUE;
  assign fault = state == DENY;
  assign physical = bus_write ? {20'h80009, saved[11:0]} : 32'b0;
endmodule
ConceptWhat does a translation keep unchanged?
ConceptWhy does the DMA of a device need a guard of its own?
ConceptWhat tells the IOMMU which tables to use?
ConceptWhy does a chip with several guests need a second table?

Common Mistakes

Wrong Send the guest physical address straight to memory.

The guest can name pages that it was never given.

Fix Apply the table of the hypervisor, with its rights, first.

Wrong Check only the store of the core to the DMA registers.

The data takes another way, from another manager.

Fix Guard the write of the device itself, and watch it at the target.

Wrong Read an address of 0 on a fault as a write to the Boot ROM.

The address is idle when no write is sent.

Fix Count the writes that memory accepted.

Remember

  • A translation swaps the page number and keeps the offset.
  • The stream ID of a device chooses its tables, and no entry means no access.
  • With guests, stage 1 belongs to the guest and stage 2 to the hypervisor.
  • Prove a refusal at the target: count the writes that memory accepted.

Practice

Report a problem with this page