The Secure World: Two Worlds on One Chip

A large chip is cut in two from top to bottom. A small trusted firmware lives in one world, and the operating system in the other. Every memory region, device and interrupt belongs to one of them.

You Will Be Able To

  • Say why a large chip has two worlds, and what lives in each.
  • Decide, for a store or an interrupt, whether the owner world and the requester agree.
  • Say why the debug port needs a grant of its own, and what closes it again.
Lesson
S10.8
Time
25 min
Runs on
Your browser
Check
5 questions

A game on the phone has a fault, and an attacker now runs code inside the operating system. He knows the address of every register on the chip.

He writes to the clock control, and nothing happens. The address was right, so something else about his write was wrong.

Before You Start

You need Access Control on the Bus: the security state, Secure or Non-secure, as an attribute that travels with a transfer.

You need Security Architecture: a wall in front of an asset, and a manager that acts for the software that programs it.

You need Secure Boot, Life Cycle and Debug Lock: the life-cycle state and the debug lock.

You need The Interrupt Path: a handler must clear the source, or the interrupt comes again.

Five questions this page answers
Question
1Why is the operating system not trusted with everything on the chip?
2Both worlds know the address of a register. What protects it?
3How does the operating system get something done that only the secure world may do?
4An interrupt reaches a handler of the wrong world. What happens?
5Four stores come in a new order. What does the lab print last?

The Idea: One Owner for Everything

A firewall on ap_soc kept one key away from code that must not read it. A large chip has more to guard: the control of its clocks, a watchdog, payment code and the keys of its films. And it runs an operating system of millions of lines, which nobody can check in full.

So a large chip is cut in two, from top to bottom. The security state of a transfer already said which of two worlds it came from. Now the worlds get tenants. The normal world runs the operating system and its programs. The secure world runs a small firmware, short enough to be checked line by line.

Every resource of the chip gets one owner: each memory region, each device, each interrupt. The world of the requester travels with every transfer, and a gate in front of the resource compares the two. An address protects nothing, because both worlds know every address.

The core itself moves between the worlds, through one door only. And one requester lives in neither world: the debug port. It needs a grant of its own.

New Words

New words on this page
WordMeaning
Secure world of a chipSecure world for short. Everything that runs in the Secure security state, and all that belongs to it. The name comes from Arm.
Normal world of a chipNormal world for short. Everything that runs in the Non-secure security state: the operating system and its programs. Also an Arm name.
TrustZoneThe name that Arm gives to this split of one chip into two worlds.
Secure firmwareThe small program of the secure world. It offers a few services to the normal world.
Secure monitorThe part of the secure firmware through which every change of world passes. Monitor for short. An Arm name: on a RISC-V chip, firmware in machine mode has this place.
World switchThe core leaves one world and enters the other. Only the monitor can do it.
Chip resourceA memory region, a device with its registers, or an interrupt source. Resource for short.
Owner worldThe world to which a resource is assigned. Owner for short.
World gateLogic in front of a resource. It lets a transfer through only if the requester may use that resource.
Service stateA life-cycle state in which the chip may be examined through the debug port.
Debug sessionOne stretch of access through the debug port. It opens with a proof that the chip checks, and a reset of the port closes it.

One Picture: Two Worlds, One Gate

Two Worlds, One GateStep through the picture. It starts with the two tenants, sends three stores to the gate, and then adds the door and the debug port.
  • Power
  • Bus
  • Debug
  • Processor
  • Peripheral
Two Worlds, One GateStep through the picture. It starts with the two tenants, sends three stores to the gate, and then adds the door and the debug port. component Secure firmware small, and checked lineby line component Clock control belongs to the secureworld component Monitor the one door betweenthe worlds component World gate compares therequester with theworld of the target component Debug port in neither world component Operating system large, and never fullychecked component UART0 belongs to the normalworld Secure storeNon-secure storefrom its own worldonlyfrom its own worldonlyasks for a serviceswitches the worldneeds its own grant
  1. component Secure firmwaresmall, and checked line by line
    • Secure store → World gate
  2. component Operating systemlarge, and never fully checked
    • Non-secure store → World gate
    • asks for a service → Monitor
  3. component World gatecompares the requester with the world of the target
    • from its own world only → Clock control
    • from its own world only → UART0
  4. component Clock controlbelongs to the secure world
  5. component UART0belongs to the normal world
  6. component Monitorthe one door between the worlds
    • switches the world → Secure firmware
  7. component Debug portin neither world
    • needs its own grant → World gate
  1. Two tenants

    The operating system lives in the normal world. A small firmware lives in the secure world. Both run on the same cores.

  2. A store from the wrong world

    The operating system writes to the clock control. Its transfer says Non-secure, the register belongs to the secure world, and the gate stops it.

  3. A store from the right world

    The secure firmware writes to the same address. Its transfer says Secure, and the gate lets it through.

  4. The other world

    UART0 belongs to the normal world. A store of the operating system reaches it.

  5. The one door

    The operating system cannot change the clock, so it asks. The request goes through the monitor, which switches the core to the secure world and back.

  6. Outside both worlds

    The debug port can make transfers of its own. No boot ever checked the person who holds the probe, so the port needs a grant of its own.

Read the diagram as text
  • Secure firmware: small, and checked line by line
  • Clock control: belongs to the secure world
  • Monitor: the one door between the worlds
  • World gate: compares the requester with the world of the target
  • Debug port: in neither world
  • Operating system: large, and never fully checked
  • UART0: belongs to the normal world
  • Secure firmware → World gate: Secure store
  • Operating system → World gate: Non-secure store
  • World gate → Clock control: from its own world only
  • World gate → UART0: from its own world only
  • Operating system → Monitor: asks for a service
  • Monitor → Secure firmware: switches the world
  • Debug port → World gate: needs its own grant

The addresses of the lab are those of UART0 and of the system control block of ap_soc. The worlds are an exercise of ap_phone: ap_soc has one world only.

The Smallest Example, Worked by Hand

No simulator yet. Two registers belong to one world each. The clock control at 10004000 belongs to the secure world. UART0 at 10000000 belongs to the normal world. In this exercise only that world may write.

Four stores, by hand
Store fromToWorld of the targetSame world?Reaches the register?
Normal worldClock control, 10004000Secure worldNoNo
Secure worldClock control, 10004000Secure worldYesYes
Normal worldUART0, 10000000Normal worldYesYes
Secure worldUART0, 10000000Normal worldNoNo

The first two rows have the same address and different answers. That is the whole point: the gate reads the world of the requester, not the address alone.

Interrupts belong to a world too. Say a timer of the secure world raises its interrupt. By mistake it is sent to a handler of the normal world.

One interrupt, two handlers
World of the sourceWorld of the handlerMay it clear the source?Pending afterwards
Secure worldNormal worldNo1
Secure worldSecure worldYes0

In the first row the handler returns and the source is still pending. So the interrupt is delivered again, and again.

The Rules

  1. Rule 1: Every resource has exactly one owner world.

    That holds for memory regions, for the registers of devices and for interrupt sources.

  2. Rule 2: The world of the requester travels with every transfer, and a gate compares it with the world of the target.

    The address alone protects nothing.

  3. Rule 3: The core changes world only through the monitor.

    Code of the normal world cannot declare itself Secure. It can only ask.

  4. Rule 4: An interrupt goes to the world of its source.

    Only that world can clear it. Sent to the other world, it stays pending.

  5. Rule 5: The debug port is in neither world.

    It gets a grant only in a service state, and only after a fresh proof.

  6. Rule 6: A reset of the debug port closes its session.

    A good boot of the software never opens one.

Sources for These Rules

A request can carry a mark for its address space, Secure or Non-secure. The same address can then lead to different places. See the Arm AMBA AXI Protocol Specification, IHI 0022 issue L, section A4.5.3.

The security state on APB and AHB, and the wall in front of an asset: the earlier lessons of this track.

Secure world, normal world, monitor and TrustZone are Arm names. No TrustZone document is in the library of this site, so rules 3 to 6 are given as common usage. No RISC-V manual is in the library either.

Which world owns which register, and the rule that only that world may write, are choices of this exercise.

Now Run It

The lab at the end of this page makes the four stores of the hand table. Then it tries twice to clear the interrupt. Last come four requests of a technician at the debug port. A bench plays every requester.

Printed by the lab on Icarus: four stores, two tries to clear one interrupt, four moments at the debug port.
store secure=0 address=10004000 sysctl=0 uart=0
store secure=1 address=10004000 sysctl=1 uart=0
store secure=0 address=10000000 sysctl=0 uart=1
store secure=1 address=10000000 sysctl=0 uart=0
irq owner_secure=1 handler_secure=0 pending=1
irq owner_secure=1 handler_secure=1 pending=0
debug service=0 proof=1 grant=0
debug service=1 proof=0 grant=0
debug service=1 proof=1 grant=1
debug after_port_reset grant=0

Rule 2 at work, in the first two rows. Both name address 10004000. With secure=0 the strobe sysctl stays 0. With secure=1 it is 1.

The fourth row is the strict choice of this lab: UART0 does not belong to the secure world, so uart stays 0.

Rule 4, in the irq rows. A handler of the normal world leaves pending=1. A handler of the right world clears it.

Rule 5, in the first three debug rows. A proof outside the service state gives grant=0. The service state without a proof gives grant=0 too. Only both together open the session.

Rule 6, in the last row. After a reset of the port the grant is 0 again, although nothing else on the chip was reset.

This lab leaves things out. It has no core, so no world switch is run: the bench sets the world of each store. It guards two registers and no memory region.

Why It Is Built This Way

ChoiceThe alternativeIts cost
Two worldsOne world with privilege levels onlyThe privileged part of an operating system is huge. One fault in it would open every key and every clock.
One door, the monitorLet the operating system set its own security stateThen the state would say nothing. Any code that wanted in would mark itself Secure.
A gate at each resourceTrust each manager to behaveA DMA controller or a debug port makes transfers too. A rule that lives in the core never sees them.

The price is a second software stack to write, to check and to update. That is why the secure world is kept small.

The Trap: The Interrupt Nobody Could Clear

A phone hangs a few seconds after start. Every core is busy, and the trace shows one handler entered thousands of times a second. The handler looks correct: it writes the clear bit and returns.

The source was a timer of the secure world. A wrong line in the routing table sent its interrupt to the normal world. The clear from there was stopped at the gate, the source stayed pending, and the handler ran again. Rule 4 was broken in one table entry.

The second trap is the hidden address. A register is left out of the public map, and its address is taken for a secret. Addresses leak. Only a gate with a rule protects a register.

The third trap is the debug port that a good boot opens. A clean boot says that the software is genuine. It says nothing about who holds the probe.

At Scale

This lab2 registers, 1 interrupt
ap_phonea world for each of 40 subordinates and 240 interrupt sources

This section looks ahead, to memory, to the other managers and to the question of who checks the secure world itself.

  1. Memory is shared out too

    Part of the DRAM is given to the secure world. The gate for it sits in front of the memory controller and compares each address with a list of regions.

  2. Every manager has a world

    A DMA side or a compute engine carries the world of the software that programs it. Give it more, and that software has found a way round the gate.

  3. More than two

    Newer chips add further worlds beside these two. One use is a box for each guest that even the hypervisor cannot read.

  4. Who checks the secure firmware?

    The secure world is trusted because it is small and because it was checked before it ran. That check is a chain that starts in the Boot ROM.

The chain continues in The Multi-Stage Secure Boot Chain.

Try It

Run the lab. Read the four store rows against the hand table, then the two irq rows, then the four debug rows.

Change the address of the second store to 10000000. Predict both strobes.

In the debug part, set service_lifecycle to 1 before the first row. Predict the grant of that row.

Names Printed by the Lab
NameMeaning
secureThe world of the requester of a store: 1 is the secure world, 0 the normal world.
addressThe target of the store. 10004000 is the clock control, and 10000000 is UART0.
sysctl, uartThe write strobes behind the gate. A 1 means that the store reached that register.
owner_secure, handler_secureThe world that owns an interrupt source, and the world of the handler that tries to clear it.
pendingThe pending bit of that source after the try.
service1 while the life-cycle state allows the chip to be examined.
proof1 on the tick at which the debug tool shows a proof that the chip accepts.
grant1 while a read through the debug port is allowed.
Sources and What the Lab Leaves Out

The gate and the debug port are two small circuits, world_port and service_debug. The interrupt rows are test code. The lab has no core and no monitor: the bench sets the world of each store.

The gate of this lab lets only the owner write, so the secure world cannot write UART0. That is a choice of the lab. The life-cycle state called service here has the place of RMA on the chip of the earlier lesson.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: store secure=0 address=10004000 sysctl=0 uart=0 store secure=1 address=10004000 sysctl=1 uart=0 store secure=0 address=10000000 sysctl=0 uart=1 store secure=1 address=10000000 sysctl=0 uart=0 irq owner_secure=1 handler_secure=0 pending=1 irq owner_secure=1 handler_secure=1 pending=0 debug service=0 proof=1 grant=0 debug service=1 proof=0 grant=0 debug service=1 proof=1 grant=1 debug after_port_reset grant=0
Read included file: soc-s107-boot.sv
// Each verdict stands for verification of the exact candidate and its signed
// version with the key delegated by the current stage. No cryptography here.
module boot_handoff (
  input logic clk, reset, check_image, verified,
  input logic [3:0] image_version, minimum_version,
  output logic [1:0] stage,
  output logic stopped
);
  always_ff @(posedge clk) begin
    if (reset) begin
      stage <= 0;
      stopped <= 0;
    end else if (check_image && !stopped && stage != 3) begin
      if (verified && image_version >= minimum_version)
        stage <= stage + 1'b1;
      else
        stopped <= 1;
    end
  end
endmodule

// Policy slice: one protected control word and one normal console word.
module world_port (
  input logic request, secure,
  input logic [31:0] address,
  output logic sysctl_write, uart_write
);
  assign sysctl_write = request && secure && address == 32'h10004000;
  assign uart_write = request && !secure && address == 32'h10000000;
endmodule

// Exercise a separate debug requester. A reset closes the service session.
module service_debug (
  input logic clk, reset, service_lifecycle, authenticated, read_request,
  output logic read_grant
);
  logic session;
  always_ff @(posedge clk) begin
    if (reset || !service_lifecycle)
      session <= 0;
    else if (authenticated)
      session <= 1;
  end
  assign read_grant = read_request && service_lifecycle && session && !reset;
endmodule

Check Yourself

Predict the outputThis run makes four stores only. The last one comes from the normal world and names the clock control. What is the last line? Its form is: store secure=.. address=.. sysctl=.. uart=..
module tb;
  bit clk, reset, request, secure;
  logic [31:0] address;
  wire sysctl_write, uart_write;
  world_port port_model (.*);
  // The debug port is a requester of its own.
  bit debug_reset, service_lifecycle, authenticated, read_request;
  wire read_grant;
  service_debug service_port (.reset(reset || debug_reset), .*);
  always #5 clk = !clk;
  bit pending;

  // One store. The world of the requester travels with the address.
  task automatic store_word(input bit world, input logic [31:0] target);
    @(negedge clk);
    request = 1;
    secure = world;
    address = target;
    @(posedge clk);
    $display("store secure=%b address=%08h sysctl=%b uart=%b", secure, address, sysctl_write, uart_write);
    @(negedge clk);
    request = 0;
  endtask

  // One interrupt source with an owner. Only a handler of the owning world can clear it.
  task automatic acknowledge(input bit owner_secure, handler_secure);
    if (handler_secure == owner_secure) pending = 0;
    $display("irq owner_secure=%b handler_secure=%b pending=%b", owner_secure, handler_secure, pending);
  endtask

  initial begin
    reset = 1;
    @(negedge clk);
    reset = 0;
    store_word(1, 32'h10000000);
    store_word(0, 32'h10000000);
    store_word(1, 32'h10004000);
    store_word(0, 32'h10004000);
    $finish;
  end
endmodule
Read included file: soc-s107-boot.sv
// Each verdict stands for verification of the exact candidate and its signed
// version with the key delegated by the current stage. No cryptography here.
module boot_handoff (
  input logic clk, reset, check_image, verified,
  input logic [3:0] image_version, minimum_version,
  output logic [1:0] stage,
  output logic stopped
);
  always_ff @(posedge clk) begin
    if (reset) begin
      stage <= 0;
      stopped <= 0;
    end else if (check_image && !stopped && stage != 3) begin
      if (verified && image_version >= minimum_version)
        stage <= stage + 1'b1;
      else
        stopped <= 1;
    end
  end
endmodule

// Policy slice: one protected control word and one normal console word.
module world_port (
  input logic request, secure,
  input logic [31:0] address,
  output logic sysctl_write, uart_write
);
  assign sysctl_write = request && secure && address == 32'h10004000;
  assign uart_write = request && !secure && address == 32'h10000000;
endmodule

// Exercise a separate debug requester. A reset closes the service session.
module service_debug (
  input logic clk, reset, service_lifecycle, authenticated, read_request,
  output logic read_grant
);
  logic session;
  always_ff @(posedge clk) begin
    if (reset || !service_lifecycle)
      session <= 0;
    else if (authenticated)
      session <= 1;
  end
  assign read_grant = read_request && service_lifecycle && session && !reset;
endmodule
ConceptWhy is the operating system not trusted with everything on the chip?
ConceptBoth worlds know the address of the clock control. What protects it?
ConceptHow does the operating system get a slower clock?
ConceptAn interrupt of a secure source is sent to a handler of the normal world. What happens?

Common Mistakes

Wrong Protect a register by keeping its address out of the map.

Both worlds can reach every address.

Fix Give it to one world, and put a gate in front of it.

Wrong Send an interrupt to a world that cannot clear its source.

The source stays pending, and the handler runs again and again.

Fix Send each interrupt to the world of its source.

Wrong Open the debug port because the boot was clean.

The debug port is a requester of its own, outside both worlds.

Fix Ask for a service state and a fresh proof, and close the session at a reset of the port.

Remember

  • A large chip has two worlds: a small secure firmware and a large operating system.
  • Every memory region, device and interrupt belongs to one world, and a gate compares.
  • The core changes world only through the monitor.
  • The debug port is in neither world. It needs a service state and a fresh proof.

Practice

Report a problem with this page