Contributors to this document include (in alphabetical order): Jason Zhijingcheng Yu, Mingkai Li
Version Information: Draft version. Refer to the commit hash.
1. Introduction
The Capstone project is an effort to explore the design of a new CPU instruction set architecture that achieves multiple security goals including memory safety and isolation with one unified hardware abstraction.
Other formats: This document is also available in the following formats:
1.1. Goals
The ultimate goal of Capstone is to unify the numerous hardware abstracts that have been added as extensions to existing architectures as afterthought mitigations to security vulnerabilities. This goal requires a high level of flexibility and extensibility of the Capstone architecture. More specifically, we aim to support the following in a unified manner.
- Exclusive access
-
Software should be guaranteed exclusive access to certain memory regions if needed. This is in spite of the existence of software traditionally entitled to higher privileges such as the OS kernel and the hypervisor.
- Revocable delegation
-
Software components should be able to delegate authority to other components in a revocable manner. For example, after an untrusted library function has been granted access to a memory region, the caller should be able to revoke this access.
- Dynamically extensible hierarchy
-
The hierarchy of authority should be dynamically extensible, unlike traditional platforms which follow a static hierarchy of hypervisor-kernel-user. This makes it possible to use the same set of abstractions for memory isolation and memory sharing regardless of where a software component lies in the hierarchy.
- Safe context switching
-
A mechanism of context switching without trusting any other software component should be provided. This allows for a minimal TCB if necessary in case of a highly security-critical application.
1.2. Major Design Elements
The Capstone architecture design is based on the idea of capabilities, which are unforgeable tokens that represent authority to perform memory accesses and control flow transfers. Capstone extends the traditional capability model with new capability types including the following.
- Linear capabilities
-
Linear capabilities are guaranteed not to alias with other capabilities. Operations on linear capabilities maintain this property. For example, linear capabilities cannot be duplicated. Instead, they can only be moved around across different registers or between registers and memory. They can hence enable safe exclusive access to memory regions. Capabilities that do not have this property are called non-linear capabilities.
- Revocation capabilities
-
Revocation capabilities cannot be used to perform memory accesses or control flow transfers. Instead, they convey the authority to revoke other capabilities. Each revocation capability is derived from a linear capability and can later be used to revoke (i.e., invalidate) capability derived from the same linear capability. This mechanism enables revocable and arbitrarily extensible chains of delegation of authority.
- Uninitialised capabilities
-
Uninitialised capabilities convey write-only authority to memory. They can be turned into linear capabilities after the memory region has been “initialised”, that is, when the whole memory region has been overwritten with fresh data. Uninitialised capabilities enable safe initialisation of memory regions and prevent secret leakage without incurring extra performance overhead.
1.3. Capstone-RISC-V ISA Overview
While Capstone does not assume any specific modern ISA, we choose to propose a Capstone extension to RISC-V due to its open nature and the availability of toolchains and simulators.
The Capstone-RISC-V ISA is an RV64IZicsr extension that makes the following types of changes to the base architecture:
-
Each general-purpose register is extended to 129 bits to accommodate 128-bit capabilities.
-
New instructions for manipulating capabilities are added.
-
New instructions for memory accesses using capabilities are added.
-
New instructions for control flow transfers using capabilities are added.
-
New instructions for machine state control are added.
-
Semantics of a small number of existing instructions are changed to support capabilities.
-
Semantics of interrupts and exceptions are changed to support capabilities.
1.4. Capstone Variants
In addition to Capstone, which is referred to as Pure Capstone in the Capstone-RISC-V ISA, we propose a variant of Capstone, called TransCapstone. While memory accesses and control flow transfers are only possible using capabilities in Pure Capstone, TransCapstone fuses capabilities with privilege levels and virtual memory found in traditional architectures, which allows for a smooth transition from existing architectures to Capstone.
The following types of changes are made to Pure Capstone to obtain TransCapstone:
-
The physical memory is partitioned into two disjoint regions, one exclusively for accesses through capabilities and the other exclusively for accesses through the virtual memory.
-
Software components are allowed to run in either of the 2 worlds, i.e., the normal world and the secure world.
-
The normal world follows the traditional privilege levels, allows both capability-based accesses and virtual memory accesses, and is therefore compatible with existing softwares.
-
The secure world follows the Pure Capstone design, limits memory accesses to capability-based accesses and provides the security guarantees of Capstone.
-
-
A world switching mechanism is added to support the secure switching between the 2 worlds.
-
Semantics of a small number of Pure Capstone instructions are changed to support the normal world and the secure world.
-
Semantics of interrupts and exceptions are extended to support the normal world and the secure world.
World | MMU | Capabilities |
---|---|---|
Normal World |
Yes |
Yes |
Secure World |
- |
Yes |
1.5. Assembly Mnemonics
Each Capstone-RISC-V instruction is given a mnemonic prefixed with CS.
.
In contexts where it is clear we are discussing Capstone-RISC-V instructions,
we will omit the CS.
prefix for brevity.
In assembly code, the list of operands to an instruction is supplied following the
instruction mnemonic, with the operands separated by commas, in the order of
rd
, rs1
, rs2
, imm
for any operand the instruction expects.
1.6. Notations
When specifying the semantics of instructions, we use the following notations to represent the type of each operand:
- I
-
Integer register.
- C
-
Capability register.
- S
-
Sign-extended immediate.
- Z
-
Zero-extended immediate.
1.7. Bibliography
The initial design of Capstone has been discussed in the following paper:
-
Capstone: A Capability-based Foundation for Trustless Secure Memory Access by Jason Zhijingcheng Yu, Conrad Watt, Aditya Badole, Trevor E. Carlson, Prateek Saxena. In Proceedings of the 32nd USENIX Security Symposium. Anaheim, CA, USA. August 2023.
2. Programming Model
The Capstone-RISC-V ISA has extended part of the machine state, including both some registers and the memory, to enable the storage and handling of capabilities.
2.1. Capabilities
2.1.1. Width
The width of a capability is 128 bits. We represent this as
CLEN = 128
and CLENBYTES = 16
. Note that this does not
affect the width of a raw address, which is XLEN = 64
bits,
or equivalently, XLENBYTES = 8
bytes, same as
in RV64IZicsr.
2.1.2. Fields
Each capability has the following architecturally-visible fields:
Name | Range | Description |
---|---|---|
|
|
Whether the capability is valid: |
|
|
The type of the capability:
|
|
|
Not applicable when |
|
|
Not applicable when |
|
|
Not applicable when |
|
|
Not applicable when |
|
|
Only applicable when |
|
|
Only applicable when |
The range of the perms
field has a partial order <=p
defined as follows:
<=p = { (0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (1, 1), (1, 3), (1, 5), (1, 7), (2, 2), (2, 3), (2, 6), (2, 7), (3, 3), (3, 7), (4, 4), (4, 5), (4, 6), (4, 7), (5, 5), (5, 7), (6, 6), (6, 7), (7, 7) }
We say a capability c
aliases with a capability d
if and only if the intersection
between [c.base, c.end)
and [d.base, d.end)
is non-empty.
For two revocation capabilities c
and d
(i.e., c.type = d.type = 2
),
we say c <t d
if and only if
-
c
aliases withd
-
The creation of
c
was earlier than the creation ofd
In addition to the above fields, an implementation also needs to maintain
sufficient metadata to test the <t
relation.
It will be clear that for any pair of aliasing revocation capabilities,
the order of their creations is well-defined.
2.2. Extension to General-Purpose Registers
The Capstone-RISC-V ISA extends each of the 32 general-purpose
registers, so it contains either a capability or a raw XLEN
-bit
integer.
The type of data contained in a register is maintained and confusion
of the type is not allowed, except for x0
/c0
as discussed below.
In assembly code, the type of data expected in a register operand
is indicated by the alias used for the register, as summarised
in the following table.
Index | XLEN -bit integer |
Capability |
---|---|---|
0 |
|
|
1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
x0
/c0
is a read-only register that can be used both as an
integer and as a capability, depending on the context. When used
as an integer, it has the value 0
.
When used as a capability, it has the value
{ valid = 0, type = 0, cursor = 0, base = 0, end = 0, perms = 0 }
.
Any attempt to write to x0
/c0
will be silently ignored (no
exceptions are raised).
In this document,
for i = 0, 1, …, 31
, we use x[i]
to refer to the general-purpose
register with index i
.
2.3. Extension to Other Registers
2.3.1. Program Counter
The following changes are made to the program counter (pc
):
-
Pure Capstone: The program counter (
pc
) is extended to contain a capability only. -
TransCapstone: Similar to the general-purpose registers, the program counter (
pc
) is also extended to contain a capability or an integer.
During the instruction fetch stage, an exception is raised when any of the following conditions are met (in priority order):
Pure Capstone
-
Instruction access fault (1)
-
pc.valid
is0
(invalid). -
pc.type
is neither0
(linear) nor1
(non-linear). -
pc.perms
is not executable (i.e.,1 <=p pc.perms
does not hold). -
pc.cursor
is not in the range[pc.base, pc.end - 4]
.
-
-
Instruction address misaligned (0)
-
pc.cursor
is not aligned to4
.
-
TransCapstone
If no exception is raised:
-
Pure Capstone: The instruction pointed to by
pc.cursor
is fetched and executed. Thepc.cursor
is then incremented by4
(i.e.,pc.cursor += 4
). -
TransCapstone:
2.4. Extension to Memory
The memory is addressed using an XLEN
-bit integer at byte-level
granularity.
In addition to raw integers, each CLEN
-bit aligned address can
also store a capability.
The type of data contained in a memory location is maintained and
confusion of the type is not allowed.
In Pure Capstone, the memory can only be accessed through capabilities.
Address Space | Access Method |
---|---|
|
Capabilities |
In TransCapstone, the physical memory is divided into two disjoint regions:
the normal memory and the secure memory.
While the normal memory is only accessible through MMU (Memory Management Unit),
the secure memory can only be accessed through capabilities.
The bounds of the secure memory [SBASE, SEND)
are implementation-defined.
Memory Region | Address Space | Access Method |
---|---|---|
Normal Memory |
|
MMU |
Secure Memory |
|
Capabilities |
2.5. Added Registers
The Capstone-RISC-V ISA adds the following registers:
Capstone Variant | Additional Registers | ||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Pure Capstone |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
TransCapstone |
|
Some of the registers only allow capability values and have special semantics related to the system-wide machine state. They are referred to as capability control and status registers (CCSRs). Under their respective constraints, CCSRs can be manipulated using CCSR manipulation instructions.
The manipulation constraints for each CCSR are indicated below.
Mnemonic | Read | Write |
---|---|---|
|
Pure Capstone or secure world |
Pure Capstone or secure world |
|
- |
Pure Capstone or secure world; the original content must not be a capability |
|
Pure Capstone or normal world; one-time only |
- |
|
Normal world |
Normal world |
The manipulation constraints for each additional CSR are indicated below.
Mnemonic | Read | Write |
---|---|---|
|
|
|
2.6. Instruction Set
The Capstone-RISC-V instruction set is based on the RV64IZicsr instruction set.
The (uncompressed) instructions are fixed 32-bit wide, and laid out in memory
in little-endian order. In the encoding space of the RV64IZicsr instruction set,
Capstone-RISC-V instructions occupies the “custom-2” subset, i.e., the opcode
of all Capstone-RISC-V instructions is 0b1011011
.
Capstone-RISC-V instruction encodings follow two basic formats: R-type and I-type, as described below (more details are also provided in the RISC-V ISA Manual).
R-type instructions receive up to three register operands, and I-type instructions receive up to two register operands and a 12-bit-wide immediate operand.
2.7. Reset
Upon reset, the system state must conform to the following specifications.
2.7.1. Pure Capstone
-
Each general-purpose register either contains an integer, or a capability with
valid = 0
. -
No addressable memory location can contain a capability.
-
ceh
,cih
, andepc
contain either integers or capabilities withvalid = 0
(invalid). -
cis = 0
. -
cinit = { valid = 1, type = 0, cursor = INIT_DATA_BASE, base = INIT_DATA_BASE, end = INIT_DATA_END, perms = 7 }
, andpc = { valid = 1, type = 0, cursor = INIT_CODE_BASE, base = INIT_CODE_BASE, end = INIT_CODE_END, perms = 7 }
, whereINIT_DATA_BASE
,INIT_DATA_END
,INIT_CODE_BASE
, andINIT_CODE_END
are implementation-defined, and[INIT_CODE_BASE, INIT_CODE_END)
does not overlap with[INIT_DATA_BASE, INIT_DATA_END)
.
2.7.2. TransCapstone
-
Each general-purpose register either contains an integer, or a capability with
valid = 0
. -
No addressable memory location can contain a capability.
-
ceh
contains either an integer or a capability withvalid = 0
(invalid). -
cwrld = 0
(normal world). -
cinit = { valid = 1, type = 0, cursor = SBASE, base = SBASE, end = SEND, perms = 7 }
. -
Specifications for RV64IZicsr.
3. Capability Manipulation Instructions
Capstone provides instructions for creating, modifying, and destroying capabilities. Note that due to the guarantee of provenance of capabilities, those instructions are the only way to manipulate capabilities. In particular, it is not possible to manipulate capabilities by manipulating the content of a memory location or register using other instructions.
3.1. Cursor, Bounds, and Permissions Manipulation
3.1.1. Capability Movement
Capabilities can be moved between registers with the MOVC instruction.
An exception is raised when any of the following conditions are met:
If no exception is raised:
-
If
rs1 = rd
, the instruction is a no-op. -
Otherwise
-
Write
x[rs1]
tox[rd]
-
If
x[rs1]
is not a non-linear capability (i.e.,type != 1
), writecnull
tox[rs1]
.
-
3.1.2. Cursor Increment
The CINCOFFSET and CINCOFFSETIMM instructions increment the cursor
of a
capability by a give amount (offset).
CINCOFFSET
An exception is raised when any of the following conditions are met:
If no exception is raised:
-
Set
x[rs1].cursor
tox[rs1].cursor + x[rs2]
. -
MOVC rd, rs1
CINCOFFSETIMM
An exception is raised when any of the following conditions are met:
If no exception is raised:
-
Set
x[rs1].cursor
tox[rs1].cursor + imm
. -
MOVC rd, rs1
3.1.3. Cursor Setter
The cursor
field of a capability can also be directly set with the SCC instruction.
An exception is raised if any of the following conditions are met:
If no exception is raised:
-
Set
x[rs1].cursor
tox[rs2]
. -
MOVC rd, rs1
3.1.4. Field Query
The LCC instruction is used to read a field from a capability.
An exception is raised if any of the following conditions are met:
If no exception is raised:
-
If
rs2 > 7
, writezero
tox[rd]
-
Otherwise, write
field
tox[rd]
according to the LCC multiplexing table.
rs2 |
field |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3.1.5. Bounds Shrinking
The bounds (base
and end
fields) of a capability can be shrunk with the SHRINK instruction.
An exception is raised when any of the following conditions are met:
If no exception is raised:
-
Set
x[rd].base
tox[rs1]
andx[rd].end
tox[rs2]
. -
If
x[rd].type
is3
(uninitialised) andx[rd].cursor < x[rs1]
, setx[rd].cursor
tox[rs1]
.
3.1.6. Bounds Splitting
The SPLIT instruction can split a capability into two by splitting the bounds.
It attempts to split the capability x[rs1]
into two capabilities,
one with bounds [x[rs1].base, x[rs2])
and the other with bounds [x[rs2], x[rs1].end)
.
An exception is raised when any of the following conditions are met:
If no exception is raised:
-
Write
x[rs1]
tox[rd]
. -
Set
x[rs1].end
tox[rs2]
. -
Set
x[rd].base
tox[rs2]
.
3.1.7. Permission Tightening
The TIGHTEN instruction tightens the permissions (perms
field) of a capability.
An exception is raised when any of the following conditions are met:
If no exception is raised:
-
If
rs2 > 7
, setx[rs1].perms
to0
. Otherwise, setx[rs1].perms
tors2
. -
MOVC rd, rs1
.
3.2. Type Manipulation
Some instructions can affect the type
field of a capability directly.
In general, the type
field cannot be set arbitrarily.
Instead, it is changed as the side effect of certain semantically significant operations.
3.2.1. Delinearisation
The DELIN instruction delinearises a linear capability.
An exception is raised when any of the following conditions are met:
If no exception is raised:
-
Set
x[rd].type
to1
(non-linear).
3.2.2. Initialisation
The INIT instruction transforms an uninitialised capability into a linear capability after its associated memory region has been fully initialised (written with new data).
An exception is raised when any of the following conditions are met:
If no exception is raised:
-
Set
x[rs1].type
to0
(linear), andx[rs1].cursor
tox[rs2]
. -
MOVC rd, rs1
.
3.2.3. Sealing
The SEAL instruction seals a linear capability.
An exception is raised when any of the following conditions are met:
If no exception is raised:
-
Set
x[rs1].type
to2
(sealed), andx[rs1].async
to0
(synchronous). -
MOVC rd, rs1
.
3.3. Dropping
The DROP instruction invalidates a capability.
An exception is raised when any of the following conditions are met:
If no exception is raised:
-
Set
x[rs1].valid
to0
(invalid).
3.4. Revocation
3.4.1. Revocation Capability Creation
The MREV instruction creates a revocation capability.
An exception is raised when any of the following conditions are met:
If no exception is raised:
-
Write
x[rs1]
tox[rd]
. -
Set
x[rd].type
to2
(revocation).
3.4.2. Revocation Operation
The REVOKE instruction revokes a capability.
An exception is raised when any of the following conditions are met:
If no exception is raised:
-
For any capability
c
in the system (in either a register or memory location),c.valid
is set to0
(invalid) if any of the following conditions are met: -
x[rs1].type
is set to0
(linear) if, for any invalidated capabilityc
, at least one of the following conditions are met:-
The type of
c
is non-linear (i.e.,c.type
is1
). -
2 <=p c.perms
does not hold.
-
-
Otherwise, set
x[rs1].type
to3
(uninitialised), andx[rs1].cursor
tox[rs1].base
.
4. Memory Access Instructions
Capstone provides instructions to load and store capabilities from/to memory regions.
4.1. Pure Capstone
In Pure Capstone, two instructions (i.e., LDC and LTC) are used to load and store capabilities.
4.1.1. Load Capabilities
The LDC instruction loads a capability from the memory.
An exception is raised when any of the following conditions are met:
If no exception is raised:
-
Load the capability at the memory location
[x[rs1].cursor + imm, x[rs1].cursor + imm + CLENBYTES)
intox[rd]
. -
If
x[rd].type
is not1
(non-linear), writecnull
to the memory location[x[rs1].cursor + imm, x[rs1].cursor + imm + CLENBYTES)
.
4.1.2. Store Capabilities
The STC instruction stores a capability to the memory.
An exception is raised when any of the following conditions are met:
If no exception is raised:
-
Store
x[rs2]
to the memory location[x[rs1].cursor + imm, x[rs1].cursor + imm + CLENBYTES)
. -
If
x[rs2].type
is not1
(non-linear), writecnull
tox[rs2]
. -
If
x[rs1].type
is3
(uninitialised), setx[rs1].cursor
tox[rs1].cursor + CLENBYTES
.
4.2. TransCapstone
In TransCapstone, the LDC and STC instructions are extended to support loading and storing capabilities from/to the normal memory using raw addresses.
4.2.1. Load Capabilities in integer encoding mode
When cwrld
is 0
(normal world) and emode
is 0
(integer encoding mode),
the LDC instruction loads a capability from the normal memory using raw addresses.
An exception is raised when any of the following conditions are met:
If no exception is raised:
-
Load the capability at the memory location
[x[rs1] + imm, x[rs1] + imm + CLENBYTES)
intox[rd]
. -
If
x[rd].type
is not1
(non-linear), writecnull
to the memory location[x[rs1] + imm, x[rs1] + imm + CLENBYTES)
.
4.2.2. Store Capabilities in integer encoding mode
When cwrld
is 0
(normal world) and emode
is 0
(integer encoding mode),
the STC instruction stores a capability to the normal memory using raw addresses.
An exception is raised when any of the following conditions are met:
If no exception is raised:
-
Store
x[rs2]
to the memory location[x[rs1] + imm, x[rs1] + imm + CLENBYTES)
. -
If
x[rs2].type
is not1
(non-linear), writecnull
tox[rs2]
.
5. Control Flow Instructions
5.1. Jump to Capabilities
The CJALR and CBNZ instructions allow jumping to a capability, i.e., setting the program counter to a given capability, in a unconditional or conditional manner.
5.1.1. CJALR
An exception is raised when any of the following conditions are met:
If no exception is raised:
-
Set
pc.cursor
topc.cursor + 4
, andx[rs1].cursor
tox[rs1].cursor + imm
. -
Write
x[rs1]
topc
, andpc
tox[rd]
. -
If
rs1 != rd
andx[rs1].type != 1
, writecnull
tox[rs1]
.
5.1.2. CBNZ
An exception is raised when any of the following conditions are met:
If no exception is raised:
-
If
x[rs1]
is0
, the instruction is a no-op. -
Otherwise
-
Set
x[rd].cursor
tox[rd].cursor + imm
. -
Write
x[rd]
topc
. -
If
x[rd].type != 1
, writecnull
tox[rd]
.
-
5.2. Domain Crossing
Domains in Capstone-RISC-V are individual software compartments that are protected by a safe context switching mechanism, i.e., domain crossing. The mechanism is provided by the CALL and RETURN instructions.
5.2.1. CALL
The CALL instruction is used to call a sealed capability, i.e., to switch to another domain.
An exception is raised when any of the following conditions are met:
If no exception is raised:
-
MOVC cra, rs1
. -
Swap the program counter (
pc
) with the content at the memory location[cra.base, cra.base + CLENBYTES)
. -
Swap
ceh
with the content at the memory location[cra.base + CLENBYTES, cra.base + 2 * CLENBYTES)
. -
Swap
csp
with the content at the memory location[cra.base + 2 * CLENBYTES, cra.base + 3 * CLENBYTES)
. -
Set
cra.type
to5
(sealed-return),cra.cursor
tocra.base
,cra.reg
tord
, andcra.async
to0
(synchronous).
5.2.2. RETURN
An exception is raised when any of the following conditions are met:
If no exception is raised:
If rs1 = 0
:
-
Set
pc.cursor
tox[rs2]
. -
Write
epc
topc
, andpc
toceh
. -
If
epc.type != 1
, writecnull
toepc
.
Otherwise:
When x[rs1].async = 0
(synchronous):
-
Write
x[rs1]
tocap
andcnull
tox[rs1]
. -
Set
pc.cursor
tox[rs2]
, and swap the program counter (pc
) with the content at the memory location[cap.base, cap.base + CLENBYTES)
. -
Swap
ceh
with the content at the memory location[cap.base + CLENBYTES, cap.base + 2 * CLENBYTES)
. -
Swap
csp
with the content at the memory location[cap.base + 2 * CLENBYTES, cap.base + 3 * CLENBYTES)
. -
Write
cap
tox[cap.reg]
and setx[cap.reg].type
to4
(sealed).
When x[rs1].async = 1
(upon exception):
-
Set
pc.cursor
tox[rs2]
, and swap the program counter (pc
) with the content at the memory location[x[rs1].base, x[rs1].base + CLENBYTES)
. -
Store
ceh
to the memory location[x[rs1].base + CLENBYTES, x[rs1].base + 2 * CLENBYTES)
. -
Set
x[rs1].type
to4
(sealed),x[rs1].async
to0
(synchronous). -
Write the resulting
x[rs1]
toceh
, andcnull
tox[rs1]
. -
For
i = 1, 2, …, 31
, Swapx[i]
with the content at the memory location[ceh.base + (i + 1) * CLENBYTES, ceh.base + (i + 2) * CLENBYTES)
.
When x[rs1].async = 2
(upon interrupt):
-
Set
pc.cursor
tox[rs2]
, and swap the program counter (pc
) with the content at the memory location[x[rs1].base, x[rs1].base + CLENBYTES)
. -
Swap
ceh
with the content at the memory location[x[rs1].base + CLENBYTES, x[rs1].base + 2 * CLENBYTES)
. -
Set
x[rs1].type
to4
(sealed),x[rs1].async
to0
(synchronous). -
Write the resulting
x[rs1]
tocih
, andcnull
tox[rs1]
. -
For
i = 1, 2, …, 31
, Swapx[i]
with the content at the memory location[cih.base + (i + 1) * CLENBYTES, cih.base + (i + 2) * CLENBYTES)
.
5.3. A World Switching Extension for TransCapstone
In TransCapstone, a pair of extra instructions, i.e., CAPENTER and CAPEXIT, is added to support switching between the secure world and the normal world.
5.3.1. CAPENTER
The CAPENTER instruction causes an entry into the secure world from the normal world. And it is only available in the normal world.
An exception is raised when any of the following conditions are met:
If no exception is raised:
When x[rs1].async = 0
(synchronous):
-
MOVC cra, rs1
. -
Write
pc
andsp
tonormal_pc
andnormal_sp
respectively. -
Write
cnull
topc
,csp
andceh
. -
Swap the program counter (
pc
) with the content at the memory location[cra.base, cra.base + CLENBYTES)
. -
Swap
ceh
with the content at the memory location[cra.base + CLENBYTES, cra.base + 2 * CLENBYTES)
. -
Swap
csp
with the content at the memory location[cra.base + 2 * CLENBYTES, cra.base + 3 * CLENBYTES)
. -
Set
cra.type
to6
(exit),cra.cursor
tocra.base
. -
Write
rs1
toswitch_reg
,rd
toexit_reg
. -
Set
cwrld
to1
(secure world).
When x[rs1].async
is 1
(upon exception) or 2
(upon interrupt):
-
Write
x[rs1]
toswitch_cap
, andcnull
tox[rs1]
. -
Write
pc
andsp
tonormal_pc
andnormal_sp
respectively. -
Write
cnull
topc
andceh
. -
Swap the program counter (
pc
) with the content at the memory location[cra.base, cra.base + CLENBYTES)
. -
Swap
ceh
with the content at the memory location[cra.base + CLENBYTES, cra.base + 2 * CLENBYTES)
. -
For
i = 1, 2, …, 31
, writezero
tox[i]
, and then swapx[i]
with the content at the memory location[switch_cap.base + (i + 1) * CLENBYTES, switch_cap.base + (i + 2) * CLENBYTES)
. -
Set
switch_cap.async
to0
(synchronous). -
Write
rs1
toswitch_reg
,rd
toexit_reg
. -
Set
cwrld
to1
(secure world).
Note: the purpose of the rd
operand
5.3.2. CAPEXIT
The CAPEXIT instruction causes an exit from the secure world into the normal world. It is only available in the secure world and can only be used with an exit capability.
An exception is raised when any of the following conditions are met:
If no exception is raised:
-
Write
x[rs1]
tocap
, andcnull
tox[rs1]
. -
Set
pc.cursor
tox[rs2]
, and writepc
,ceh
, andcsp
to the memory location[cap.base, cap.base + CLENBYTES)
,[cap.base + CLENBYTES, cap.base + 2 * CLENBYTES)
, and[cap.base + 2 * CLENBYTES, cap.base + 3 * CLENBYTES)
respectively. -
Write the content of
normal_pc
andnormal_sp
topc
andsp
respectively. -
Write
cnull
toceh
. -
Set
cap.type
to4
(sealed),cap.async
to0
(synchronous), and write the resultingcap
tox[switch_reg]
. -
Set
x[exit_reg]
to0
(normal exit). -
Set
cwrld
to0
(normal world).
6. Control and Status Instructions
The CCSRRW instruction is used to read and write specified capability CSRs (CCSRs).
An exception is raised when any of the following conditions are met:
-
Unexpected operand type (24)
-
x[rs1]
is not a capability.
-
-
Illegal operand value (29)
-
The immediate value
imm
does not correspond to the encoding of a valid capability CSR.
-
If no exception is raised:
-
Read from capability CSR
-
If the read constraint is satisfied, the content of the capability CSR specified by the immediate value
imm
is written tox[rd]
. If the current content of the capability CSR is neither a non-linear capability (i.e.,type != 1
) nor an exit capability (i.e.,type != 6
), it will be set to the content ofcnull
. -
Otherwise,
x[rd]
is set to the content ofcnull
.
-
-
Write to capability CSR
-
If the write constraint is satisfied,
x[rs1]
is written to the capability CSR specified by the immediate valueimm
. Ifx[rs1]
is neither a non-linear capability (i.e.,type != 1
) nor an exit capability (i.e.,type != 6
), it will be set to the content ofcnull
. -
Otherwise, the original current of the capability CSR is preserved.
-
7. Adjustments to Existing Instructions
For most existing instructions in RV64IZicsr, the adjustments are straightforward.
Their behaviour is unchanged, and an “unexpected operand type (24)” exception is raised if any of the operands
(i.e., x[rs1]
, x[rs2]
or x[rd]
) is a capability.
Apart from this operand constraint, the following instructions in RV64IZicsr are adjusted in Capstone:
-
For memory access instructions, they are extended to use capabilities as addresses for memory access.
-
For control flow instructions, they are slightly adjusted to support capability-aware control flow.
-
Certain instructions, especially those belonging to the privileged ISA, are illegal under certain circumstances.
7.1. Memory Access Instructions
In RV64IZicsr, memory access instructions include load instructions
(i.e., lb
, lh
, lw
, lbu
, lhu
, lwu
, ld
), and store instructions (i.e., sb
, sh
, sw
, sd
).
In RV64IZicsr, these instructions take an integer as a raw address, and load or store a value from/to this address.
In Capstone, these instructions are extended to take a capability as an address.
7.1.1. Pure Capstone
Load Instructions
In Pure Capstone, RV64IZicsr load instructions are modified to load integers of different sizes using capabilities.
Signed Load Instructions
lb
, lh
, lw
, ld
instructions are modified to load an integer in the size of byte, halfword, word, and doubleword
(i.e., XLENBYTES/8
, XLENBYTES/4
, XLENBYTES/2
, and XLENBYTES
bytes), using capabilities respectively.
The loaded integer is sign-extended to the size of the destination register (i.e., XLEN
bits).
An exception is raised when any of the following conditions are met:
-
Unexpected operand type (24)
-
x[rs1]
is not a capability.
-
-
Invalid capability (25)
-
x[rs1].valid
is0
(invalid).
-
-
Unexpected capability type (26)
-
x[rs1].type
is neither0
(linear) nor1
(non-linear).
-
-
Insufficient capability permissions (27)
-
4 <=p x[rs1].perms
does not hold.
-
-
Capability out of bound (28)
-
x[rs1].cursor + imm
is not in the range[x[rs1].base, x[rs1].end - size]
, wheresize
is the size (in bytes) of the integer being loaded.
-
-
Load address misaligned (4)
-
x[rs1].cursor + imm
is not aligned to the size of the integer being loaded.
-
If no exception is raised:
The content at the memory location [x[rs1].cursor + imm, x[rs1].cursor + imm + size)
is loaded
as a signed integer to x[rd]
, where size
is the size of the integer being loaded
(i.e., XLENBYTES/8
, XLENBYTES/4
, XLENBYTES/2
, and XLENBYTES
bytes for lb
, lh
, lw
, and ld
respectively).
Unsigned Load Instructions
lbu
, lhu
, lwu
instructions are modified to load an integer in the size of byte, halfword, and word
(i.e., XLENBYTES/8
, XLENBYTES/4
, and XLENBYTES/2
bytes), using capabilities respectively.
The loaded integer is zero-extended to the size of the destination register (i.e., XLEN
bits).
An exception is raised when any of the following conditions are met:
-
Unexpected operand type (24)
-
x[rs1]
is not a capability.
-
-
Invalid capability (25)
-
x[rs1].valid
is0
(invalid).
-
-
Unexpected capability type (26)
-
x[rs1].type
is neither0
(linear) nor1
(non-linear).
-
-
Insufficient capability permissions (27)
-
4 <=p x[rs1].perms
does not hold.
-
-
Capability out of bound (28)
-
x[rs1].cursor + imm
is not in the range[x[rs1].base, x[rs1].end - size]
, wheresize
is the size (in bytes) of the integer being loaded.
-
-
Load address misaligned (4)
-
x[rs1].cursor + imm
is not aligned to the size of the integer being loaded.
-
If no exception is raised:
The content at the memory location [x[rs1].cursor + imm, x[rs1].cursor + imm + size)
is loaded
as an unsigned integer to x[rd]
, where size
is the size of the integer being loaded
(i.e., XLENBYTES/8
, XLENBYTES/4
, and XLENBYTES/2
bytes for lbu
, lhu
, and lwu
respectively).
Store Instructions
sb
, sh
, sw
, sd
instructions are modified to store an integer in the size of byte, halfword, word, and doubleword
(i.e., XLENBYTES/8
, XLENBYTES/4
, XLENBYTES/2
, and XLENBYTES
bytes), using capabilities respectively.
An exception is raised when any of the following conditions are met:
-
Unexpected operand type (24)
-
x[rs1]
is not a capability. -
x[rs2]
is not an integer.
-
-
Invalid capability (25)
-
x[rs1].valid
is0
(invalid).
-
-
Unexpected capability type (26)
-
x[rs1].type
is not0
,1
, or3
(linear, non-linear, or uninitialised).
-
-
Illegal operand value (29)
-
x[rs1].type
is3
(uninitialised) andimm
is not0
.
-
-
Insufficient capability permissions (27)
-
x[rs1].perms
is neither6
(read-write) nor7
(read-write-execute).
-
-
Capability out of bound (28)
-
x[rs1].cursor + imm
is not in the range[x[rs1].base, x[rs1].end - size]
, wheresize
is the size (in bytes) of the integer being stored.
-
-
Store/AMO address misaligned (6)
-
x[rs1].cursor + imm
is not aligned to the size of the integer being stored.
-
If no exception is raised:
-
The content of
x[rs2]
is stored as an integer to the memory location[x[rs1].cursor + imm, x[rs1].cursor + imm + size)
, wheresize
is the size of the integer being stored (i.e.,XLENBYTES/8
,XLENBYTES/4
,XLENBYTES/2
, andXLENBYTES
bytes forsb
,sh
,sw
, andsd
respectively). -
If
x[rs1].type
is3
(uninitialised),x[rs1].cursor
is set tox[rs1].cursor + size
. -
The content in the
CLEN
-bit aligned memory location[cbase, cend)
, which alias with memory location[x[rs1].cursor + imm, x[rs1].cursor + imm + size)
, is set to integer type, wherecbase = (x[rs1].cursor + imm) & ~(CLENBYTES - 1)
andcend = cbase + CLENBYTES
.
7.1.2. TransCapstone
In TransCapstone, RV64IZicsr memory access instructions behave the same as in Pure Capstone
if cwrld
is 1
(secure world). However, if cwrld
is 0
(normal world), these instructions behave
differently in different encoding modes.
-
When
cwrld
is0
(normal world) andemode
is1
(capability encoding mode), these instructions behave the same as in Pure Capstone. -
When
cwrld
is0
(normal world) andemode
is0
(integer encoding mode), these instructions behave the same as in RV64IZicsr except that the following adjustments are made to these instructions:-
An “Unexpected operand type (24)” exception is raised if any of
x[rs1]
,x[rs2]
andx[rd]
contains a capability. -
An “Capability out of bound (28)” exception is raised if the address to be accessed (i.e.,
x[rs1] + imm
) is within the range(SBASE - size, SEND)
, wheresize
is the size (in bytes) of the integer to be loaded/stored. -
For store instructions (i.e.,
sb
,sh
,sw
,sd
), the content in theCLEN
-bit aligned memory location[cbase, cend)
, which alias with memory location[x[rs1] + imm, x[rs1] + imm + size)
, is set to integer type, wherecbase = (x[rs1] + imm) & ~(CLENBYTES - 1)
andcend = cbase + CLENBYTES
.
-
7.2. Control Flow Instructions
In RV64IZicsr, conditional branch instructions (i.e., beq
, bne
, blt
, bge
, bltu
, and bgeu
),
and unconditional jump instructions (i.e., jal
and jalr
) are used to control the flow of execution.
In Capstone, these instructions are adjusted to support the situation where the program counter is a capability.
7.2.1. Branch Instructions
The following adjustments are made to these instructions:
Pure Capstone
-
An “Unexpected operand type (24)” exception is raised if
x[rs1]
orx[rs2]
is a capability. -
pc.cursor
, instead ofpc
, is changed by the instruction.
TransCapstone
-
An “Unexpected operand type (24)” exception is raised if
x[rs1]
orx[rs2]
is a capability. -
When
cwrld
is1
(secure world),pc.cursor
, instead ofpc
, is changed by the instruction.
7.2.2. Jump Instructions
The following adjustments are made to these instructions:
Pure Capstone
-
An “Unexpected operand type (24)” exception is raised if
x[rs1]
(if existed) orx[rd]
is a capability. -
pc.cursor
, instead ofpc
, is changed by the instruction. -
pc.cursor
(i.e., the address of the next instruction) is written tox[rd]
.
TransCapstone
-
An “Unexpected operand type (24)” exception is raised if
x[rs1]
(if existed) orx[rd]
is a capability. -
When
cwrld
is1
(secure world),pc.cursor
, instead ofpc
, is changed by the instruction. -
When
cwrld
is1
(secure world), formerpc.cursor
(i.e., the address of the next instruction) is written tox[rd]
.
7.3. Instructions Made Illegal
Some instructions in RV64IZicsr now raise “illegal instruction (2)” exceptions when executed in Pure Capstone or TransCapstone secure world, under all or some circumstances.
-
All instructions defined in the privileged ISA of RV64IZicsr
-
All instructions defined in the Zicsr extension, namely instructions that directly access CSRs, when the CSR specified is not one defined in Capstone-RISC-V, or when the read/write constraints are not satisfied
-
ECALL
-
EBREAK
8. Interrupts and Exceptions
8.1. Exception and Exit Codes
Note: where are the exception codes relevant?
The exception code is what the exception handler domain receives as an argument when an exception occurs on Pure Capstone or in TransCapstone secure world. It is an integer value that indicates what the type of the exception is.
TransCapstone also has exit codes, which are the values returned to the CAPENTER instruction in case the exception cannot be handled in the secure world.
We define the exception code and the exit code for each type of exception below. It aligns with the exception codes defined in RV64IZicsr, where applicable, for ease of implementation and interoperability.
Exception | Exception code | TransCapstone exit code |
---|---|---|
Instruction address misaligned |
0 |
1 |
Instruction access fault |
1 |
1 |
Illegal instruction |
2 |
1 |
Breakpoint |
3 |
1 |
Load address misaligned |
4 |
1 |
Load access fault |
5 |
1 |
Store/AMO address misaligned |
6 |
1 |
Store/AMO access fault |
7 |
1 |
Unexpected operand type |
24 |
1 |
Invalid capability |
25 |
1 |
Unexpected capability type |
26 |
1 |
Insufficient capability permissions |
27 |
1 |
Capability out of bound |
28 |
1 |
Illegal operand value |
29 |
1 |
Unhandleable exception |
63 |
N/A in TransCapstone |
For interrupts, the same encodings as in RV64IZicsr are used.
Note: TransCapstone exit code
8.2. Exception Data
For Pure Capstone and the secure world in TransCapstone, the exception-related
data is stored in the tval
CSR, similar to RV64IZicsr. The exception handler
can use the value to decide how to handle the exception.
However, such data is available only for in-domain exception handling, where the
exception handling process does not involve a domain switch.
For exception handling that crosses domain (i.e., when ceh
is a valid sealed capability)
or world boundaries (i.e., when the normal world ends up handling the exception),
the exception data (i.e., the data in tval
) is not available.
This is to protect the confidentiality of domain execution.
Note that this design does not stop the excepted domain from selectively
trusting a different domain with such data.
For exceptions defined in RV64IZicsr, the same data as in it is written to tval
.
For the added exceptions, the following data is written to tval
:
Exception | Data |
---|---|
|
The instruction itself (or the lowest XLEN bits if it is wider than XLEN) |
|
The instruction itself (or the lowest XLEN bits if it is wider than XLEN) |
|
The instruction itself (or the lowest XLEN bits if it is wider than XLEN) |
|
The instruction itself (or the lowest XLEN bits if it is wider than XLEN) |
|
The instruction itself (or the lowest XLEN bits if it is wider than XLEN) |
|
The instruction itself (or the lowest XLEN bits if it is wider than XLEN) |
|
N/A |
8.3. Pure Capstone
For Pure Capstone, the handling of interrupts and exceptions is relatively
straightforward. Regardless of whether the event is an interrupt or an
exception (and what the type of the interrupt or exception is), the processor
core will always transfer the control flow to the corresponding handler domain
(specified in the ceh
register for exceptions and
the cih
register for interrupts).
The current context is saved and sealed in a sealed-return capability which is then supplied to the exception/interrupt handler domain as an argument.
When exception/interrupt handling is complete, the exception/interrupt handler domain can use the RETURN instruction to resume the execution of the excepted domain. This process resembles that of a CALL-RETURN pair, except that it is asynchronous, rather than synchronous, to the execution of the original domain.
8.3.1. Interrupt Status
The cis
CSR encodes the control and status associated with interrupts.
The diagram below shows its layout.
cis
CSR layoutEach pair of xIP
and xIE
fields describes the status of
the interrupt type x
.
The interrupt type x
is pending if the xIP
field is set to
1
, and enabled if the xIE
field is set to 1
.
Currently, three types of interrupts
are supported: external interrupts (E
), timer interrupts (T
),
and software interrupts (S
).
The definitions for those interrupt types match those in RV64IZicsr.
All the fields are read-write, but only when cih
contains a capability.
Note: why not require a valid sealed capability?
8.3.2. Interrupt Delivery
The interrupt delivery process starts with a certain event
typically asynchronous to the execution of the hardware thread.
The sources of such events include the external interrupt controller,
the timer, and other CPU cores, which correspond to the external,
timer, and software interrupt types (i.e., x = E
, T
, and S
).
When such an event occurs, the xIP
field in the cis
register
is set to 1
to indicate that the interrupt is pending.
At any point during the execution of a hardware thread,
if any pair of xIP
and xIE
fields are both 1
and at the same
time the cih
register contains
a capability, the interrupt is delivered to the interrupt handler
domain.
Note: global interrupt enable/disable
8.3.3. Handling of Interrupts
The interrupt is ignored if any of the following conditions is met:
Otherwise:
-
Swap
pc
with the content at the memory location[cih.base, cih.base + CLENBYTES)
. -
Swap
ceh
with the content at the memory location[cih.base + CLENBYTES, cih.base + 2 * CLENBYTES)
. -
For
i = 1, 2, …, 31
, Swapx[i]
with the content at memory location[cih.base + (i + 1) * CLENBYTES, cih.base + (i + 2) * CLENBYTES)
. -
Set
cih.type
to5
(sealed-return),cih.cursor
tocih.base
,cih.reg
to0
, andcih.async
to2
(upon interrupt). -
Write
cih
to the registercra
, andcnull
to the registercih
. -
Write the exception code to the register
a0
.
8.3.4. Handling of Exceptions
Note: the stack of exception handler domains
Follow the interrupt handling procedure with exception code unhandleable exception (63)
if any of the following conditions is met:
Otherwise:
If the content in ceh
is a valid sealed capability:
-
Swap
pc
with the content at the memory location[ceh.base, ceh.base + CLENBYTES)
. -
For
i = 1, 2, …, 31
, Swapx[i]
with the content at the memory location[ceh.base + (i + 1) * CLENBYTES, ceh.base + (i + 2) * CLENBYTES)
. -
Set
ceh.type
to5
(sealed-return),ceh.cursor
toceh.base
,ceh.reg
to0
, andceh.async
to1
(upon exception). -
Write
ceh
to the registercra
, andcnull
to the registerceh
. -
Swap
ceh
with the content at the memory location[cra.base + CLENBYTES, cra.base + 2 * CLENBYTES)
. -
Write the exception code to the register
a0
.
If the content is ceh
is a valid executable non-linear capability or linear capability:
-
Write
pc
toepc
. -
Write
ceh
topc
. Ifceh.type != 1
, writecnull
toceh
. -
Write the exception code to
cause
. -
Write extra exception data to
tval
.
Otherwise, the CPU core enters the state of panic.
Note: sealing mechanism of in-domain exception handling
8.3.5. Panic
When a CPU core is unable to handle an exception, it enters a state called panic. The actual behaviour of the CPU core in this state is implementation-defined, but must be one of the following:
-
Enter an infinite loop.
-
Scrub all general-purpose registers, and then load a capability that is not otherwise available into
pc
, and a set of capabilities that are not otherwise available into general-purpose registers.
The aim of the constraints above is to uphold the invariants of the capability model and in turn the security guarantees of the system.
8.4. TransCapstone
TransCapstone retains the same interrupt and exception handling mechanism for the normal world as in RV64IZicsr. For the secure world in TransCapstone, the handling of interrupts and exceptions is more complex, and it becomes relevant whether the event is an interrupt or an exception.
Note: overview of interrupt handling in the secure world
Note: overview of exception handling in the secure world
Below we discuss the details of the handling of interrupts and exceptions generated in the secure world.
8.4.1. Handling of Secure-World Interrupts
When an interrupt occurs in the secure world, the processor core directly saves the full context, scrubs it, and exits to the normal world. It then generates a corresponding interrupt in the normal world, and follows the normal-world interrupt handling process thereafter.
If the content in switch_cap
is a valid sealed capability:
-
Store
pc
to the memory location[switch_cap.base, switch_cap.base + CLENBYTES)
. -
Store
ceh
to the memory location[switch_cap.base + CLENBYTES, switch_cap.base + 2 * CLENBYTES)
, and writecnull
toceh
. -
For
i = 1, 2, …, 31
, store the content ofx[i]
to the memory location[switch_cap.base + (i + 1) * CLENBYTES, switch_cap.base + (i + 2) * CLENBYTES)
. -
Load the program counter
pc
and the stack pointersp
fromnormal_pc
andnormal_sp
respectively. -
Set
switch_cap.aync
to2
(upon interrupt). -
Write
switch_cap
to the registerx[switch_reg]
, andcnull
toswitch_cap
. -
Scrub the other general-purpose registers (i.e., write
zero
tox[i]
wherei != 2
andi != switch_reg
). -
Set the
cwrld
register to0
(normal world). -
Trigger an interrupt in the normal world.
Otherwise:
-
Load the program counter
pc
and the stack pointersp
fromnormal_pc
andnormal_sp
respectively. -
Write
cnull
tox[switch_reg]
. -
Scrub the other general-purpose registers (i.e., write
zero
tox[i]
wherei != 2
andi != switch_reg
). -
Set the
cwrld
register to0
(normal world). -
Trigger an interrupt in the normal world.
Note that in this case, there will be another exception in the normal world
when the user process resumes execution after the interrupt has been handled
by the OS, due to the invalid switch_cap
value written to the CAPENTER
operand.
8.4.2. Handling of Secure-World Exceptions
When an exception occurs, the processor core first attempts to handle the exception in the secure world, in the similar way as in Pure Capstone. If this fails, the processor core saves the full context if it can and exits to the normal world with a proper error code.
If the content in ceh
is a valid sealed capability:
-
Swap
pc
with the content at memory location[ceh.base, ceh.base + CLENBYTES)
. -
For
i = 1, 2, …, 31
, Swapx[i]
with the content at the memory location[ceh.base + (i + 1) * CLENBYTES, ceh.base + (i + 2) * CLENBYTES)
. -
Set the
ceh.type
to5
(sealed-return),ceh.cursor
toceh.base
, andceh.async
to1
(upon exception). -
Write
ceh
to the registercra
, andcnull
to the registerceh
. -
Swap
ceh
with the content at the memory location[cra.base + CLENBYTES, cra.base + 2 * CLENBYTES)
. -
Write the exception code to the register
a0
.
Note that this is exactly the same as the handling of exceptions in Pure Capstone.
If the content is ceh
is a valid executable non-linear capability or linear capability:
-
Write
pc
toepc
. -
Write
ceh
topc
. Ifceh.type != 1
, writecnull
toceh
. -
Write the exception code to
cause
-
Write extra exception data to
tval
.
Otherwise:
If the content in switch_cap
is a valid sealed capability:
-
Store the current value of the program counter (
pc
) to the memory location[switch_cap.base, switch_cap.base + CLENBYTES)
. -
Store
ceh
to the memory location[switch_cap.base + CLENBYTES, switch_cap.base + 2 * CLENBYTES)
, and writecnull
toceh
. -
For
i = 1, 2, …, 31
, store the content ofx[i]
to the memory location[switch_cap.base + (i + 1) * CLENBYTES, switch_cap.base + (i + 2) * CLENBYTES)
. -
Load the program counter
pc
and the stack pointersp
fromnormal_pc
andnormal_sp
respectively. -
Set
switch_cap.async
to1
(upon exception). -
Write the content of
switch_cap
tox[switch_reg]
, andcnull
toswitch_cap
. -
Scrub the other general-purpose registers (i.e., write
zero
tox[i]
wherei != 2
andi != switch_reg
). -
Write the exit code to
x[exit_reg]
. -
Set the
cwrld
register to0
(normal world).
Otherwise:
-
Load the program counter
pc
and the stack pointersp
fromnormal_pc
andnormal_sp
respectively. -
Write
cnull
tox[switch_reg]
. -
Scrub the other general-purpose registers (i.e., write
zero
tox[i]
wherei != 2
andi != switch_reg
). -
Write the exit code to
x[exit_reg]
. -
Set the
cwrld
register to0
(normal world).
Note: comparison between synchronous and asynchronous exit
9. Memory Consistency Model
TODO
Appendix A: Debugging Instructions (Non-Normative)
A.1. World Switching
The instructions SETWORLD and ONPARTITION are related to world switching in TransCapstone.
The instructions load their operands from
the register x[rs1]
, which expects
an integer.
SETWORLD directly sets the core to the specified
world (0
for normal world and non-zero for secure world).
The program counter will also be made into a capability or an integer
correspondingly while retaining the cursor
value.
ONPARTITION switches on (non-zero) or off (0
) the world partitioning checks
in memory.
The instructions make it easy to set up the environment for testing either Pure Capstone or TransCapstone:
-
Pure Capstone: secure world, world partitioning checks off
-
TransCapstone: normal world, world partitioning checks on
A.2. Exception Handling
The instructions SETEH and ONNORMALEH affect the behaviours of interrupt and exception handling.
The SETEH instruction sets the secure-world
exception handler domain (i.e., ceh
) to the specified capability
x[rs1]
.
The ONNORMALEH instruction checks x[rs1]
and switches on (non-zero) or off (0
) normal world handling of secure-world exceptions.
When this is on, an exception that occurs in the secure world will trap to the normal world
first before being handled by the secure-world exception handler (ceh
), which is the
expected behaviour in TransCapstone.
When it is off, the exception will be directly handled by the secure-world exception handler,
as is expected in Pure Capstone.
Appendix B: Instruction Listing
B.1. Debugging Instructions
Mnemonic | Format | Func3 | Func7 | rs1 | rs2 | rd | imm[11:0] | World | Variant |
---|---|---|---|---|---|---|---|---|---|
QUERY |
R |
|
|
I |
- |
- |
- |
* |
* |
RCUPDATE |
R |
|
|
I |
- |
I |
- |
* |
* |
ALLOC |
R |
|
|
I |
- |
I |
- |
* |
* |
REV |
R |
|
|
I |
- |
- |
- |
* |
* |
CAPCREATE |
R |
|
|
- |
- |
C |
- |
* |
* |
CAPTYPE |
R |
|
|
I |
- |
C |
- |
* |
* |
CAPNODE |
R |
|
|
I |
- |
C |
- |
* |
* |
CAPPERM |
R |
|
|
I |
- |
C |
- |
* |
* |
CAPBOUND |
R |
|
|
I |
I |
C |
- |
* |
* |
CAPPRINT |
R |
|
|
I |
- |
- |
- |
* |
* |
TAGSET |
R |
|
|
I |
I |
- |
- |
* |
* |
TAGGET |
R |
|
|
I |
- |
I |
- |
* |
* |
R |
|
|
I |
- |
- |
- |
* |
T |
|
R |
|
|
I |
- |
- |
- |
* |
T |
|
R |
|
|
C |
- |
- |
- |
* |
T |
|
R |
|
|
I |
- |
- |
- |
* |
T |
B.2. Capstone Instructions
Mnemonic | Format | Func3 | Func7 | rs1 | rs2 | rd | imm[11:0] | World | Variant |
---|---|---|---|---|---|---|---|---|---|
R |
|
|
C |
- |
- |
- |
* |
* |
|
R |
|
|
I |
I |
C |
- |
* |
* |
|
R |
|
|
C |
Z |
C |
- |
* |
* |
|
R |
|
|
- |
- |
C |
- |
* |
* |
|
R |
|
|
C |
Z |
I |
- |
* |
* |
|
R |
|
|
I |
- |
C |
- |
* |
* |
|
R |
|
|
C |
I |
C |
- |
* |
* |
|
R |
|
|
C |
- |
C |
- |
* |
* |
|
R |
|
|
C |
- |
C |
- |
* |
* |
|
R |
|
|
C |
I |
C |
- |
* |
* |
|
R |
|
|
C |
- |
C |
- |
* |
* |
|
R |
|
|
C |
- |
- |
- |
* |
* |
|
R |
|
|
C |
I |
C |
- |
* |
* |
|
I |
|
- |
C |
- |
C |
S |
* |
* |
Mnemonic | Format | emode |
Func3 | Func7 | rs1 | rs2 | rd | imm[11:0] | World | Variant |
---|---|---|---|---|---|---|---|---|---|---|
I |
|
|
- |
I |
- |
C |
S |
N |
T |
|
I |
|
|
- |
C |
- |
C |
S |
N |
T |
|
I |
- |
|
- |
C |
- |
C |
S |
S |
T |
|
I |
- |
|
- |
C |
- |
C |
S |
- |
P |
|
S |
|
|
- |
I |
C |
- |
S |
N |
T |
|
I |
|
|
- |
C |
C |
- |
S |
N |
T |
|
I |
- |
|
- |
C |
C |
- |
S |
S |
T |
|
I |
- |
|
- |
C |
C |
- |
S |
- |
P |
Mnemonic | Format | Func3 | Func7 | rs1 | rs2 | rd | imm[11:0] | World | Variant |
---|---|---|---|---|---|---|---|---|---|
R |
|
|
C |
- |
Z |
- |
S |
T |
|
R |
|
|
C |
- |
Z |
- |
- |
P |
|
R |
|
|
C |
I |
- |
- |
S |
T |
|
R |
|
|
C |
I |
- |
- |
- |
P |
|
I |
|
- |
C |
- |
C |
S |
S |
T |
|
I |
|
- |
C |
- |
C |
S |
- |
P |
|
I |
|
- |
I |
- |
C |
S |
S |
T |
|
I |
|
- |
I |
- |
C |
S |
- |
P |
|
R |
|
|
C |
- |
Z |
- |
N |
T |
|
R |
|
|
C |
I |
- |
- |
S |
T |
Mnemonic | Format | Func3 | Func7 | rs1 | rs2 | rd | imm[11:0] | World | Variant |
---|---|---|---|---|---|---|---|---|---|
I |
|
- |
C |
- |
C |
Z |
* |
* |
B.3. Extended RV64IZicsr Memory Access Instructions
Mnemonic | Format | emode |
Func3 | Func7 | rs1 | rs2 | rd | imm[11:0] | World | Variant |
---|---|---|---|---|---|---|---|---|---|---|
I |
|
|
- |
I |
- |
I |
S |
N |
T |
|
I |
|
|
- |
C |
- |
I |
S |
N |
T |
|
I |
- |
|
- |
C |
- |
I |
S |
S |
T |
|
I |
- |
|
- |
C |
- |
I |
S |
- |
P |
|
I |
|
|
- |
I |
- |
I |
S |
N |
T |
|
I |
|
|
- |
C |
- |
I |
S |
N |
T |
|
I |
- |
|
- |
C |
- |
I |
S |
S |
T |
|
I |
- |
|
- |
C |
- |
I |
S |
- |
P |
|
I |
|
|
- |
I |
- |
I |
S |
N |
T |
|
I |
|
|
- |
C |
- |
I |
S |
N |
T |
|
I |
- |
|
- |
C |
- |
I |
S |
S |
T |
|
I |
- |
|
- |
C |
- |
I |
S |
- |
P |
|
I |
|
|
- |
I |
- |
I |
S |
N |
T |
|
I |
|
|
- |
C |
- |
I |
S |
N |
T |
|
I |
- |
|
- |
C |
- |
I |
S |
S |
T |
|
I |
- |
|
- |
C |
- |
I |
S |
- |
P |
|
I |
|
|
- |
I |
- |
I |
S |
N |
T |
|
I |
|
|
- |
C |
- |
I |
S |
N |
T |
|
I |
- |
|
- |
C |
- |
I |
S |
S |
T |
|
I |
- |
|
- |
C |
- |
I |
S |
- |
P |
|
I |
|
|
- |
I |
- |
I |
S |
N |
T |
|
I |
|
|
- |
C |
- |
I |
S |
N |
T |
|
I |
- |
|
- |
C |
- |
I |
S |
S |
T |
|
I |
- |
|
- |
C |
- |
I |
S |
- |
P |
|
I |
|
|
- |
I |
- |
I |
S |
N |
T |
|
I |
|
|
- |
C |
- |
I |
S |
N |
T |
|
I |
- |
|
- |
C |
- |
I |
S |
S |
T |
|
I |
- |
|
- |
C |
- |
I |
S |
- |
P |
Mnemonic | Format | emode |
Func3 | Func7 | rs1 | rs2 | rd | imm[11:0] | World | Variant |
---|---|---|---|---|---|---|---|---|---|---|
S |
|
|
- |
I |
I |
- |
S |
N |
T |
|
S |
|
|
- |
C |
I |
- |
S |
N |
T |
|
S |
- |
|
- |
C |
I |
- |
S |
S |
T |
|
S |
- |
|
- |
C |
I |
- |
S |
- |
P |
|
S |
|
|
- |
I |
I |
- |
S |
N |
T |
|
S |
|
|
- |
C |
I |
- |
S |
N |
T |
|
S |
- |
|
- |
C |
I |
- |
S |
S |
T |
|
S |
- |
|
- |
C |
I |
- |
S |
- |
P |
|
S |
|
|
- |
I |
I |
- |
S |
N |
T |
|
S |
|
|
- |
C |
I |
- |
S |
N |
T |
|
S |
- |
|
- |
C |
I |
- |
S |
S |
T |
|
S |
- |
|
- |
C |
I |
- |
S |
- |
P |
|
S |
|
|
- |
I |
I |
- |
S |
N |
T |
|
S |
|
|
- |
C |
I |
- |
S |
N |
T |
|
S |
- |
|
- |
C |
I |
- |
S |
S |
T |
|
S |
- |
|
- |
C |
I |
- |
S |
- |
P |
Note: the meaning of abbreviations in the table
Appendix C: Assembly Code Examples
TODO
Appendix D: Abstract Binary Interface (Non-Normative)
TODO