Access Control on the Bus
An address says where a transfer goes. It does not say who is asking. Two attributes beside the address do, and a filter in front of the register reads them.
You Will Be Able To
- Tell privilege from security state, and read each from its own attribute bit.
- Place the permission check in front of the register, and follow a refused transfer to its reply.
- Say who may set an attribute, and why a manager with no software needs rules of its own.
- Lesson
- S4.11
- Time
- 24 min
- Runs on
- Your browser
- Check
- 5 questions
The start-up code reads a service register and gets 68. An application reads the same address and gets zero. The decoder is not at fault, for the address matched to the last bit. Something else travelled with the address.
Before You Start
You need the APB signals and the edge at which a transfer counts from S0.4. You need PSLVERR, the APB error wire, from Bridges. You need the processor of S0.6.
| Question | |
|---|---|
| 1 | What is privilege, and what is security state? |
| 2 | Why are they two attributes and not one? |
| 3 | Where must the permission check sit, and why not behind the register? |
| 4 | Who completes a transfer that is refused, and how does the manager learn of it? |
| 5 | Who is allowed to set the attributes of a transfer? |
The Idea: Who Is Asking
Until now the address was everything. The decoder looked at it and picked a subordinate. Any program on the chip could reach any register.
A real chip cannot allow that. An application must not change the clock settings. Code that arrived over a network must not read a stored key. So every transfer says, beside its address, who is asking.
It says two separate things. Privilege is how far the processor trusts the code that is running. An operating system runs privileged, and an application does not. Security state is which of two worlds the transfer belongs to. The Secure world holds keys and the code that guards them. The Non-secure world holds everything else, a whole operating system included.
These two things travel as a few extra bits, called attributes. A filter in front of a protected register reads them. It decides before the register sees anything.
New Words
| Word | Meaning |
|---|---|
| Privilege | How far the processor trusts the running code. Privileged code may do what unprivileged code may not. |
| Security state | Which of two worlds a transfer belongs to: Secure or Non-secure. |
| Attribute | A few bits that travel with the address and describe the transfer. |
| Access filter | Logic in front of a register. It passes the select on only if the attributes allow it. |
| PPROT | The three APB wires that carry the attributes. |
The same two attributes exist on all three buses. On AHB the protection wires are named HPROT, and one more wire is named HNONSEC. On AXI the wires are named ARPROT for a read. For a write they are named AWPROT. The bit positions differ, which is a trap of its own.
| Bus | Privilege | Security state |
|---|---|---|
| APB | bit 0 of PPROT: 1 is privileged | bit 1 of PPROT: 1 is Non-secure |
| AHB | bit 1 of HPROT: 1 is privileged | the wire HNONSEC: 1 is Non-secure |
| AXI | bit 0 of ARPROT or AWPROT: 1 is privileged | bit 1 of ARPROT or AWPROT: 1 is Non-secure |
Bit 1 means privileged on AHB, and Non-secure on the other two.
The lab on this page prints these names.
| In the lab | Meaning |
|---|---|
| priv | the privilege attribute: 1 is privileged |
| target | the select that reaches the register, behind the filter |
| enable | PENABLE: 0 in the setup phase, 1 in the access phase |
| ready | PREADY: 1 when the transfer can end |
| err | PSLVERR: 1 when the transfer failed |
| data | the byte that is read, in hexadecimal |
| wr | 1 for a write, 0 for a read |
| quiet | a setting of the lab: 1 makes the filter answer a refused read with no error |
One Picture: Two Questions, Four Corners, Two Bits
- Debug
- Processor
- Bus
- Peripheral
- Question 1: how far is this code trusted?
Left to right is privilege. The processor lets an operating system do things that it forbids an application. It knows at every moment which of the two is running.
- Question 2: which world is it in?
Top to bottom is security state. The Secure world holds keys and the code that guards them. The Non-secure world holds everything else.
- Four corners
The two answers are independent, so there are four kinds of code. An operating system is privileged, and it is still in the Non-secure world.
- Two bits beside the address
Each transfer carries both answers as attributes. Two transfers to the same address can differ in nothing else.
- The filter decides
The filter compares the attributes with the rule of the register behind it. Only if they fit does the register see a select.
Read the diagram as text
- An application: unprivileged, in the Non-secure world
- An operating system: privileged, in the Non-secure world
- A key service: unprivileged, in the Secure world
- Secure start-up code: privileged, in the Secure world
- Access filter: reads the two attributes that travel beside the address
- Protected register: is selected only if the filter agrees
- An application → An operating system: more privilege
- An application → A key service: the other world
- An operating system → Access filter: address, privileged, Non-secure
- Secure start-up code → Access filter: address, privileged, Secure
- Access filter → Protected register: select, if allowed
The four names on the left are examples. The lab on this page checks privilege only. Security state would be a second input of the same filter.
Keep the third step in mind. "Privileged" is not a stronger form of "Secure". A register that only the Secure world may read is closed to a privileged operating system.
The Smallest Example, Worked by Hand: One Register, Four Transfers
A service register holds the byte 68. Only privileged code may touch it. Four transfers arrive at its address, one after the other. Work out what the filter does with each.
| Who asks | What | Select reaches the register? | Reply |
|---|---|---|---|
| privileged | a read | yes | the data 68, and no error |
| unprivileged | a read | no | an error, and the data 00 |
| unprivileged | a write | no | an error, and the register keeps 68 |
| privileged | a read | yes | the data 68: the refused write changed nothing |
- Processor
- Debug
- Bus
- Peripheral
- The privileged read
The filter raises the select of the register. The register drives 68.
- The unprivileged read
The filter keeps the select at 0. It completes the transfer by itself, with an error.
Read the diagram as text
- Privileged read: priv is 1
- Unprivileged read: priv is 0
- Access filter: passes the select on only when priv is 1
- Service register: holds the byte 68
- Reply of the filter: ready, an error and the data 00
- Privileged read → Access filter: priv=1
- Unprivileged read → Access filter: priv=0
- Access filter → Service register: select
- Access filter → Reply of the filter: refused
The register never learns that the second read was tried.
Why not let the register answer, and hide the data afterwards? Because a read can change a register. Many status registers clear a flag when they are read. If the select got through, the flag would be gone before anyone checked permission.
The Rules
Rule 1: Privilege and security state are two separate attributes.
Each has its own bit. Neither can be worked out from the other.
Rule 2: The attributes belong to one transfer, and travel with its address.
A bridge stores them with the address. It must not ask later who is running now, for by then it may be other code.
Rule 3: The check sits in front of the register.
A refused transfer never raises the select of the register. Hiding the data afterwards is too late.
Rule 4: A refused transfer still gets a reply.
The filter completes it. On APB the error wire counts at the one edge where the transfer ends.
Rule 5: Only trusted hardware sets an attribute.
The processor derives it from its own state. A manager with no software, such as DMA, needs rules of its own. Data inside a transfer cannot vouch for its sender.
Sources for These Rules
Arm AMBA APB Protocol Specification, IHI 0024 issue E. PPROT and the meaning of its three bits: section 3.5. That section calls bit 2 a hint, and makes the signal optional. The error wire, and the one edge at which it counts: section 3.4.
Arm AMBA AHB Protocol Specification, IHI 0033 issue C. HPROT: section 3.8. HNONSEC, which exists only on interfaces with secure transfers: section 3.10.
Arm AMBA AXI Protocol Specification, IHI 0022 issue L, sections A4.5.2 and A4.5.3. A newer extension of AXI and APB adds more worlds than two. This page leaves it out.
Where a filter sits, and what it answers to a refused transfer, is the choice of each chip. The specifications define the attributes and leave the policy open. The service register and the "quiet" setting are choices of this lab.
Now Run It
The lab at the end of this page is the hand example as a circuit. Test code plays the bridge on the APB side, and sets the privilege attribute directly. It makes five transfers to the service register. Each prints one row in its setup phase and one in its access phase.
setup priv=1 target=1 enable=0 ready=1 err=0 data=68
access wr=0 quiet=0 target=1 enable=1 ready=1 err=0 data=68
setup priv=0 target=0 enable=0 ready=1 err=0 data=00
access wr=0 quiet=0 target=0 enable=1 ready=1 err=1 data=00
setup priv=0 target=0 enable=0 ready=1 err=0 data=00
access wr=0 quiet=1 target=0 enable=1 ready=1 err=0 data=00
setup priv=0 target=0 enable=0 ready=1 err=0 data=00
access wr=1 quiet=1 target=0 enable=1 ready=1 err=1 data=00
setup priv=1 target=1 enable=0 ready=1 err=0 data=68
access wr=0 quiet=0 target=1 enable=1 ready=1 err=0 data=68Rule 3 at work: in the first setup row priv is 1 and target is 1. In the second setup row priv is 0 and target is 0. The register is not selected, in the setup phase or in the access phase.
The second transfer of the run, built from its two recorded rows.
Rule 4 at work: in the access row of that read, ready is 1 and err is 1. The filter ended the transfer, and told the manager why the data is 00.
The third transfer is the same read with quiet at 1. It ends with err at 0 and data at 00. To the manager this looks like a successful read of a register that holds zero.
The fourth transfer is a write by unprivileged code. It gets err at 1 even with quiet at 1. The fifth is a privileged read, and it still finds 68.
This lab leaves things out. It checks privilege only. It stops at the APB side, so the error is not carried back to AHB. The test code sets the attribute, where a real chip takes it from the processor.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| Two attributes | One "trusted" bit | A chip can keep keys from its own operating system. Every filter must say which attribute it asks about. |
| The filter in front of the register | The register answers, and the data is hidden | A refused read has no side effect. The filter must make a reply of its own. |
| An error for a refused transfer | A quiet reply of zero | Software learns that it was refused. A program that probes for registers learns which addresses exist. |
The last row is a real choice, and chips make it both ways. A quiet reply gives nothing away to an attacker. It also gives nothing to the engineer who is looking for a bug.
The Trap: A Zero from the Filter
Back to the application that read zero. Nothing reported an error. The driver took the zero for the real content of the register and carried on, with a wrong setting, for weeks.
The chip had chosen quiet replies for refused reads. A zero from the filter and a zero from the register look the same at the manager. They differ in one place only. In the first case the register was never selected.
When a read returns a value you did not expect, do not start at the data. Ask whether the register was reached at all. Then ask which attributes the transfer carried, and who set them.
At Scale
This section looks ahead. One register and one way to it make an easy review. A large chip has thousands of registers and several ways to each.
Start at the register
List every path that can raise its select or see its data. Include DMA, the debug port and anything that runs before software does.
Follow the attributes
At each bridge, compare the attributes that leave with those that came in with the address. Add wait states to the test, so that a change of running code falls in the middle.
Check both sides
For every refused transfer, check the reply at the manager and the state of the register. The fifth transfer of the lab does the second half.
The exact bits
The Protocols track gives each bus a lesson: protection on APB and security on AHB.
Try It
Run the lab. For each of the five transfers, say whether the register was selected, and which rule decided it.
In the test code, change the first value of the register from 8'h68 to another byte. Predict which rows change, then run.
Change the fourth transfer into a privileged write: access(1,1,1). Predict the data of the last row.
setup priv=1 target=1 enable=0 ready=1 err=0 data=68 access wr=0 quiet=0 target=1 enable=1 ready=1 err=0 data=68 setup priv=0 target=0 enable=0 ready=1 err=0 data=00 access wr=0 quiet=0 target=0 enable=1 ready=1 err=1 data=00 setup priv=0 target=0 enable=0 ready=1 err=0 data=00 access wr=0 quiet=1 target=0 enable=1 ready=1 err=0 data=00 setup priv=0 target=0 enable=0 ready=1 err=0 data=00 access wr=1 quiet=1 target=0 enable=1 ready=1 err=1 data=00 setup priv=1 target=1 enable=0 ready=1 err=0 data=68 access wr=0 quiet=0 target=1 enable=1 ready=1 err=0 data=68Read included file: soc-s411-apb-gate.sv
// The parent decoder selects this APB page. Its single service word is privileged.
// The teaching target is always ready and has no wait states.
module ap_service_gate(
input logic psel, penable, pwrite, privileged, quiet_read,
input logic [7:0] service_data,
output logic target_sel, pready, pslverr,
output logic [7:0] prdata
);
assign target_sel = psel && privileged;
assign pready = 1'b1;
assign pslverr = psel && penable && !privileged && (pwrite || !quiet_read);
assign prdata = target_sel && !pwrite ? service_data : 8'h00;
endmodule
Check Yourself
Common Mistakes
Wrong Read "privileged" as "Secure".
They are two attributes that answer two questions.
Fix Check the attribute that the rule of the register names.
Wrong Let the register be selected, and hide its data on a refusal.
A read can change the register before permission is checked.
Fix Keep the select at 0 for a refused transfer.
Wrong Take a read of zero for a success.
A filter with quiet replies also answers zero.
Fix Read the error reply, and know which policy the chip chose.
Remember
- An address says where; two attributes say who is asking.
- Privilege and security state are separate, and each has its own bit.
- The filter sits in front of the register, so a refused transfer never selects it.
- A refused transfer still gets a reply, and an error tells the manager why.
- Only trusted hardware sets an attribute, for every manager and every way in.