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.
| Question | |
|---|---|
| 1 | Why is the operating system not trusted with everything on the chip? |
| 2 | Both worlds know the address of a register. What protects it? |
| 3 | How does the operating system get something done that only the secure world may do? |
| 4 | An interrupt reaches a handler of the wrong world. What happens? |
| 5 | Four 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
| Word | Meaning |
|---|---|
| Secure world of a chip | Secure 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 chip | Normal world for short. Everything that runs in the Non-secure security state: the operating system and its programs. Also an Arm name. |
| TrustZone | The name that Arm gives to this split of one chip into two worlds. |
| Secure firmware | The small program of the secure world. It offers a few services to the normal world. |
| Secure monitor | The 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 switch | The core leaves one world and enters the other. Only the monitor can do it. |
| Chip resource | A memory region, a device with its registers, or an interrupt source. Resource for short. |
| Owner world | The world to which a resource is assigned. Owner for short. |
| World gate | Logic in front of a resource. It lets a transfer through only if the requester may use that resource. |
| Service state | A life-cycle state in which the chip may be examined through the debug port. |
| Debug session | One 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
- Power
- Bus
- Debug
- Processor
- Peripheral
- Two tenants
The operating system lives in the normal world. A small firmware lives in the secure world. Both run on the same cores.
- 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.
- A store from the right world
The secure firmware writes to the same address. Its transfer says Secure, and the gate lets it through.
- The other world
UART0 belongs to the normal world. A store of the operating system reaches it.
- 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.
- 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.
| Store from | To | World of the target | Same world? | Reaches the register? |
|---|---|---|---|---|
| Normal world | Clock control, 10004000 | Secure world | No | No |
| Secure world | Clock control, 10004000 | Secure world | Yes | Yes |
| Normal world | UART0, 10000000 | Normal world | Yes | Yes |
| Secure world | UART0, 10000000 | Normal world | No | No |
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.
| World of the source | World of the handler | May it clear the source? | Pending afterwards |
|---|---|---|---|
| Secure world | Normal world | No | 1 |
| Secure world | Secure world | Yes | 0 |
In the first row the handler returns and the source is still pending. So the interrupt is delivered again, and again.
The Rules
Rule 1: Every resource has exactly one owner world.
That holds for memory regions, for the registers of devices and for interrupt sources.
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.
Rule 3: The core changes world only through the monitor.
Code of the normal world cannot declare itself Secure. It can only ask.
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.
Rule 5: The debug port is in neither world.
It gets a grant only in a service state, and only after a fresh proof.
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.
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=0Rule 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
| Choice | The alternative | Its cost |
|---|---|---|
| Two worlds | One world with privilege levels only | The privileged part of an operating system is huge. One fault in it would open every key and every clock. |
| One door, the monitor | Let the operating system set its own security state | Then the state would say nothing. Any code that wanted in would mark itself Secure. |
| A gate at each resource | Trust each manager to behave | A 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 section looks ahead, to memory, to the other managers and to the question of who checks the secure world itself.
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.
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.
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.
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
| Name | Meaning |
|---|---|
| secure | The world of the requester of a store: 1 is the secure world, 0 the normal world. |
| address | The target of the store. 10004000 is the clock control, and 10000000 is UART0. |
| sysctl, uart | The write strobes behind the gate. A 1 means that the store reached that register. |
| owner_secure, handler_secure | The world that owns an interrupt source, and the world of the handler that tries to clear it. |
| pending | The pending bit of that source after the try. |
| service | 1 while the life-cycle state allows the chip to be examined. |
| proof | 1 on the tick at which the debug tool shows a proof that the chip accepts. |
| grant | 1 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.
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=0Read 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
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.