generated from riscv/docs-spec-template
-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathriscv-integration.adoc
1247 lines (1020 loc) · 52 KB
/
riscv-integration.adoc
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
[#section_rv_integration]
== Integrating {cheri_base_ext_name} with the RISC-V Base Integer Instruction Set
{cheri_base_ext_name} is an extension to the RISC-V ISA. The extension adds a
carefully selected set of instructions and CSRs that are sufficient to
implement new security features in the ISA. To ensure compatibility,
{cheri_base_ext_name} also requires some changes to the primary base integer
variants: RV32I, providing 32-bit addresses with 64-bit capabilities, and
RV64I, providing 64-bit addresses with 128-bit capabilities. The remainder of
this chapter describes these changes for both the unprivileged and privileged
components of the base integer RISC-V ISAs.
NOTE: The changes described in this specification also ensure that
{cheri_base_ext_name} is compatible with RV32E.
NOTE: RV128 is not currently supported by any CHERI extension.
NOTE: In line with the base RISC-V ISA, the unprivileged component
with its corresponding {cheri_base_ext_name} changes
as described in this chapter can be used with
an entirely different privileged-level design. The changes for the privileged
component described in this chapter are designed to support existing
popular operating systems, and assume the standard
privileged architecture specified in the RISC-V ISA.
=== Memory
A hart supporting {cheri_base_ext_name} has a single byte-addressable address
space of 2^XLEN^ bytes for all memory accesses. Each memory region capable of
holding a capability also stores a tag bit for each naturally aligned CLEN bits
(e.g. 16 bytes in RV64), so that capabilities with their tag set can only be
stored in naturally aligned addresses. Tags must be atomically bound to the
data they protect.
The memory address space is circular, so the byte at address
2^XLEN^ - 1 is adjacent to the byte at address zero. A capability's
<<section_cap_representable_check>> described in xref:section_cap_encoding[xrefstyle=short] is
also circular, so address 0 is within the <<section_cap_representable_check>> of a capability
where address 2^MXLEN^ - 1 is within the bounds.
However, the decoded top field of a capability is MXLEN + 1 bits wide and does *not* wrap, so
a capability with base 2^MXLEN^ - 1 and top 2^MXLEN^ + 1 is not a subset of the
<<infinite-cap>> capability and does not authorise access to the byte at address 0.
Like malformed bounds (see xref:section_cap_malformed[xrefstyle=short]), it is impossible for
a CHERI core to generate a tagged capability with top > 2^MXLEN^.
If such a capability exists then it must have been caused by a logic or memory fault.
Unlike malformed bounds, the top overflowing is not treated as a special case in the
architecture: normal bounds check rules should be followed.
[#section_riscv_programmers_model]
=== Programmer's Model for {cheri_base_ext_name}
For {cheri_base_ext_name}, the 32 unprivileged *x* registers of the base
integer ISA are extended so that they are able to hold a capability as well
as renamed to *c* registers. Therefore, each *c* register is CLEN bits wide
and has an out-of-band tag bit. The *x* notation refers to the address field
of the capability in an unprivileged register while the *c* notation is used
to refer to the full capability (i.e. address, metadata and tag) held in the
same unprivileged register.
The tag of the unprivileged *c* registers must be reset to zero. The reset
values of the metadata and address fields are UNSPECIFIED for all unprivileged
*c* registers except *c0*.
Register *c0* is hardwired with all bits, including the capability metadata and
tag, equal to 0. In other words, *c0* is hardwired to the <<null-cap>>
capability.
[#pcc,reftext="pcc"]
==== PCC - The Program Counter Capability
An authorising capability with appropriate permissions is required to execute
instructions in {cheri_base_ext_name}. Therefore, the unprivileged program
counter (*pc*) register is extended so that it is able to hold a capability.
The extended register is called the program counter capability (<<pcc>>). The
<<pcc>> address field is effectively the *pc* in the base RISC-V ISA so that the
hardware automatically increments as instructions are executed. The <<pcc>>'s
metadata and tag are reset to the <<infinite-cap>> capability metadata and tag
with the address field set to the core boot address.
The hardware performs the following checks on <<pcc>> for each instruction
executed in addition to the checks already required by the base RISC-V ISA. A
failing check causes a CHERI exception.
* The tag must be set
* The capability must not be sealed
* The capability must grant execute permission
* All bytes of the instruction must be in bounds
NOTE: Operations that update <<pcc>>, such as changing privilege or executing
jump instructions, unseal capabilities prior to writing. Therefore,
implementations do not need to check that that <<pcc>> is unsealed when
executing each instruction. However, this property has not yet been formally verified and may not hold if additional CHERI extensions beyond {cheri_base_ext_name} are implemented.
NOTE: It is common for implementations to not allow executing *pc* relative
instructions, such as <<AUIPC>> or <<JAL>>, in debug mode.
.Program Counter Capability
[#pcc-format]
include::img/pccreg.edn[]
<<pcc>> is an executable
vector, so it need not be able to hold all possible invalid addresses.
[#section_cap_instructions]
=== Capability Instructions
ifdef::cheri_v9_annotations[]
NOTE: *CHERI v9 Note:* Some instructions from the original CHERI
specification were removed to save encoding space, or because they relate
to features which are not yet in this specification. Instructions were removed
if they do not harm performance and can be emulated using other instructions.
endif::[]
{cheri_base_ext_name} introduces new instructions to the base RISC-V integer
ISA to inspect and operate on capabilities held in registers.
==== Capability Inspection Instructions
These instructions allow software to inspect the fields of a capability held
in a *c* register. The output is an integer value written to an *x* register
representing the decoded field of the capability, such as the permissions or
bounds. These instructions do not cause exceptions.
* <<GCTAG>>: inspects the tag of the input capability. The output is 1 if the
tag is set and 0 otherwise
* <<GCPERM>>: outputs the architectural (AP) and software-defined (SDP)
permission fields of the input capability
* <<GCTYPE>>: outputs the type (e.g. unsealed or sentry) of the input capability
* <<GCBASE>>: outputs the expanded base address of the input capability
* <<GCLEN>>: outputs the length of the input capability. Length is defined as
`top - base`. The output is 2^MXLEN^-1 when the capability's length is
2^MXLEN^
* <<CRAM>>: outputs the nearest bounds alignment that a valid capability can
represent
* <<GCHI>>: outputs the compressed capability metadata
* <<SCEQ>>: compares two capabilities including tag, metadata and
address
* <<SCSS>>: tests whether the bounds and permissions of a capability are a
subset of those from another capability
NOTE: <<GCBASE>> and <<GCLEN>> output 0 when a capability with malformed
bounds is provided as an input (see
xref:section_cap_malformed[xrefstyle=short]).
==== Capability Manipulation Instructions
These instructions allow software to manipulate the fields of a capability held
in a *c* register. The output is a capability written to a *c* register with
its fields modified. The output capability has its tag set to 0 if the
input capability did not have a tag set, the output capability has more
permissions or larger bounds compared to the input capability, or the operation
results in a capability with malformed bounds. These instructions do not give
rise to exceptions.
* <<SCADDR>>: set the address of a capability to an arbitrary address
* <<CADD>>, <<CADDI>>: increment the address of the input capability
by an arbitrary offset
* <<SCHI>>: replace a capability's metadata with an arbitrary value. The
output tag is always 0
* <<ACPERM>>: bitwise AND of a mask value with a bit map representation of the
architectural (AP) and software-defined (SDP) permissions fields
* <<SCBNDS>>: set the base and length of a capability. The tag is
cleared, if the encoding cannot represent the bounds exactly
* <<SCBNDSR>>: set the base and length of a capability. The base will be
rounded down and/or the length will be rounded up if the encoding cannot represent
the bounds exactly
* <<SENTRY>>: seal capability as a sentry capability
* <<CBLD>>: replace the base, top, address, permissions and mode fields of a
capability with the fields from another capability
* <<CMV>>: move a capability from a *c* register to another *c* register
ifdef::cheri_v9_annotations[]
NOTE: *CHERI v9 Note:* <<SCBNDS>> and <<SCBNDSI>> perform the role
of the old CSETBOUNDSEXACT while the <<SCBNDSR>> is the old
CSETBOUNDS.
endif::[]
==== Capability Load and Store Instructions
A load capability instruction, <<LC>>, reads CLEN bits from memory together with
its tag and writes the result to a *c* register. The capability authorising the
memory access is provided in a *c* source register, so the effective address is
obtained by incrementing that capability with the sign-extended 12-bit offset.
A store capability instruction, <<SC>>, writes CLEN bits and the tag in a *c* register
to memory. The capability authorising the memory access is provided in a *c*
source register, so the effective address is obtained by incrementing that
capability with the sign-extended 12-bit offset.
<<LC>> and <<SC>> instructions cause CHERI exceptions if the
authorising capability fails any of the following checks:
* The tag is zero
* The capability is sealed
* At least one byte of the memory access is outside the capability's bounds
* For loads, the read permission must be set in AP
* For stores, the write permission must be set in AP
Capability load and store instructions also cause load or store/AMO address
misaligned exceptions if the address is not naturally aligned to a CLEN
boundary.
Misaligned capability loads and stores are errors. Implementations must
generate exceptions for misaligned capability loads and stores even if they
allow misaligned integer loads and stores to complete normally. Execution
environments must report misaligned capability loads and stores as errors
and not attempt to emulate them using byte access. The Zicclsm extension
does not affect capability loads and stores. Software which uses capability
loads and stores to copy data other than capabilities must ensure that
addresses are aligned.
NOTE: Since there is only one tag per aligned CLEN bit block in memory, it is not
possible to represent a capability value complete with its tag at an
address not aligned to CLEN. Therefore, <<LC>> and <<SC>> give rise to
misaligned address fault exceptions when the effective address to access is
misaligned, even if the implementation supports Zicclsm. To transfer CLEN
misaligned bits without a tag, use integer loads and stores.
For loads, the tag of the capability loaded from memory is cleared if the
authorising capability does not grant permission to read capabilities (i.e.
both <<r_perm>> and <<c_perm>> must be set in AP). For stores, the tag of the
capability written to memory is cleared if the authorising capability does not
grant permission to write capabilities (i.e. both <<w_perm>> and <<c_perm>>
must be set in AP).
WARNING: #TODO: these cases may cause exceptions in the future - we need a way
for software to discover and/or control the behaviour#
[#section_existing_riscv_insns]
=== Existing RISC-V Instructions
The operands or behavior of some instructions in the base RISC-V ISA changes in
{cheri_base_ext_name}.
==== Integer Computational Instructions
Most integer computational instructions operate on XLEN bits of values held in
*x* registers. Therefore, these instructions only operate on the address field
if the input register of the instruction holds a capability. The output is XLEN
bits written to an *x* register; the tag and capability metadata of that
register are zeroed.
The add upper immediate to <<pcc>> instruction (<<AUIPC>>) is used to
build <<pcc>>-relative capabilities. <<AUIPC>> forms a 32-bit offset from the 20-bit
immediate and filling the lowest 12 bits with zeros. The <<pcc>> address is then
incremented by the offset and a representability check is performed so the
capability's tag is cleared if the new address is outside the <<pcc>>'s
<<section_cap_representable_check>>. The resulting CLEN value along with the new tag are
written to a *c* register.
==== Control Transfer Instructions
Control transfer instructions operate as described in the base RISC-V
ISA. They also may cause metadata updates and/or cause exceptions in addition
to the base behaviour as described below.
===== Unconditional Jumps
<<JAL>> sign-extends the offset and adds it to the address of
the jump instruction to form the target address. The target address is
installed in the address field of <<pcc>>. The capability with the address of the
instruction following the jump is sealed and written to a *c* register.
<<JALR>>
allows unconditional, indirect jumps to a target capability. The target capability is
obtained by incrementing the capability in the *c* register operand by the
sign-extended 12-bit offset, then setting the
least significant bit of the result to zero. The target capability is unsealed if it is a sentry with zero offset. The capability
with the address of the instruction following the jump is sealed
and written to a *c* register.
All jumps cause CHERI exceptions when a minimum sized instruction
at the target address is not within the bounds of the <<pcc>>.
<<JALR>> causes a CHERI exception when:
* The target capability's tag is zero
* The target capability is sealed and the immediate is not zero
* A minimum sized instruction at the target capability's address is not
within bounds
* The target capability does not grant execute permission
<<JAL>> and <<JALR>> can also cause instruction address misaligned exceptions
following the standard RISC-V rules.
[#condbr-purecap]
===== Conditional Branches
Branch instructions (see xref:insns-conbr-32bit[xrefstyle=short]) encode signed
offsets in multiples of 2 bytes. The offset is sign-extended and added to the
address of the branch instruction to form the target address.
Branch instructions compare two *x* registers as described in the base RISC-V
ISA, so the metadata and tag values are disregarded in the comparison if the
operand registers hold capabilities. If the comparison evaluates to true, then
the target address is installed in the <<pcc>>'s address field. These instructions cause CHERI exceptions
when a minimum sized instruction at the target address is not within the
<<pcc>>'s bounds.
[#section_int_load_store_insns]
==== Integer Load and Store Instructions
Integer load and store instructions transfer the amount of integer data described in
the base RISC-V ISA between the registers and memory. For example, <<LD>> and
<<LW>> load 64-bit and 32-bit values respectively from memory into an *x*
register. However, the address operands for load and store instructions are
interpreted differently in {cheri_base_ext_name}: the capability authorising
the access is in the *c* register operand and the memory address is given by
incrementing the address of that capability by the sign-extended 12-bit
immediate offset.
All load and store instructions cause CHERI exceptions if the
authorising capability fails any of the following checks:
* The tag is set
* The capability is unsealed
* All bytes of accessed memory are inside the capability's bounds
* For loads, the read permission must be set in AP
* For stores, the write permission must be set in AP
Integer load instructions always zero the tag and metadata of the result register.
Integer stores write zero to the tag associated with the memory locations that
are naturally aligned to CLEN. Therefore, misaligned stores may clear up to
two tag bits in memory.
=== Zicsr, Control and Status Register (CSR) Instructions
{cheri_base_ext_name} requires that RISC-V CSRs intended to hold addresses,
like <<mtvec>>, are now able to hold capabilities. Therefore, such registers are
renamed and extended to CLEN-bit in {cheri_base_ext_name}.
Reading or writing any part of a CLEN-bit CSR may cause
side effects. For example, the CSR's tag bit may be cleared if a new address
is outside the <<section_cap_representable_check>> of a CSR capability being written.
This section describes how the CSR instructions operate on these CSRs in
{cheri_base_ext_name}.
The CLEN-bit CSRs are summarised in xref:clen_csr_summary[xrefstyle=short].
[#zicsr-section-purecap]
==== CSR Instructions
ifdef::cheri_v9_annotations[]
NOTE: *CHERI v9 Note:* CSpecialRW is removed. Its role is assumed by
<<CSRRW>>.
endif::[]
All CSR instructions atomically read-modify-write a single CSR. If the CSR
accessed is of capability size then the
capability's tag, metadata and address are all accessed atomically.
When the <<CSRRW>> instruction is accessing a capability width CSR, then the source
and destination operands are *c* registers and it atomically swaps the values in the
whole CSR with the CLEN width register operand.
There are special rules for updating specific CLEN-wide CSRs as shown in <<extended_CSR_writing>>.
When <<CSRRS>> and <<CSRRC>> instructions are accessing a capability width CSR,
such as <<mtvecc>>, then the destination operand is a *c* register and the
source operand is an *x* register. Therefore, the instructions atomically read
CLEN bits from the CSR, calculate the final address using standard RISC-V
behaviour (set bits, clear bits, etc.), and that final address is written to the
CSR capability's address field. The update typically uses the semantics of a
<<SCADDR>> instruction which clears the tag if the capability is sealed, or
if the updated address is not representable. <<extended_CSR_writing>> shows the
exact action taken for each capability width CSR.
The <<CSRRWI>>, <<CSRRSI>> and <<CSRRCI>> variants are similar to <<CSRRW>>,
<<CSRRS>>, and <<CSRRC>> respectively, when accessing a capability width CSR
except that they update the capability's address only using an XLEN-bit value
obtained by zero-extending a 5-bit unsigned immediate field.
All CSR instructions cause CHERI exceptions if the <<pcc>> does not grant
<<asr_perm>> and the CSR accessed is privileged.
[#csr-numbers-section]
=== Control and Status Registers (CSRs)
{cheri_base_ext_name} extends the CSRs listed in
xref:dcsrnames-renamed[xrefstyle=short],
xref:mcsrnames-renamed[xrefstyle=short],
xref:scsrnames-renamed[xrefstyle=short],
xref:vscsrnames-renamed[xrefstyle=short] and
xref:ucsrnames-renamed[xrefstyle=short] from the base RISC-V ISA and its
extensions. The CSRs are renamed to reflect the fact that they are extended to
CLEN+1 bits wide, as the *x* registers are renamed to *c* registers.
[[dcsrnames-renamed]]
.Renamed debug-mode CSRs in {cheri_base_ext_name}
[%autowidth,float="center",align="center",cols="<,<,<,<,<,<",options="header"]
|===
include::generated/csr_renamed_purecap_mode_d_table_body.adoc[]
|===
[[mcsrnames-renamed]]
.Renamed machine-mode CSRs in {cheri_base_ext_name}
[%autowidth,float="center",align="center",cols="<,<,<,<,<,<",options="header"]
|===
include::generated/csr_renamed_purecap_mode_m_table_body.adoc[]
|===
[[scsrnames-renamed]]
.Renamed supervisor-mode CSRs in {cheri_base_ext_name}
[%autowidth,float="center",align="center",cols="<,<,<,<,<,<",options="header"]
|===
include::generated/csr_renamed_purecap_mode_s_table_body.adoc[]
|===
[[vscsrnames-renamed]]
.Renamed virtual supervisor-mode CSRs in {cheri_base_ext_name}
[%autowidth,float="center",align="center",cols="<,<,<,<,<,<",options="header"]
|===
include::generated/csr_renamed_purecap_mode_vs_table_body.adoc[]
|===
[[ucsrnames-renamed]]
.Renamed user-mode CSRs in {cheri_base_ext_name}
[%autowidth,float="center",align="center",cols="<,<,<,<,<,<",options="header"]
|===
include::generated/csr_renamed_purecap_mode_u_table_body.adoc[]
|===
=== Machine-Level CSRs
{cheri_base_ext_name} extends some M-mode CSRs to hold capabilities or
otherwise add new functions. <<pcc>> must grant <<asr_perm>> to access M-mode
CSRs regardless of the RISC-V privilege mode.
[#mstatus,reftext="mstatus"]
==== Machine Status Registers (mstatus and mstatush)
The *mstatus* and *mstatush* registers operate as described in
cite:[riscv-priv-spec] except for the SXL and UXL fields that control the
value of XLEN for S-mode and U-mode, respectively, and the MBE, SBE, and UBE
fields that control the memory system endianness for M-mode, S-mode,
and U-mode, respectively.
The encoding of the SXL and UXL fields is the same as the MXL field of *misa*.
Only 1 and 2 are supported values for SXL and UXL and the fields must be
read-only in implementations supporting {cheri_base_ext_name}. The effective
XLEN in S-mode and U-mode are termed SXLEN and UXLEN, respectively.
The MBE, SBE, and UBE fields determine the endianness of memory accesses other
than instruction fetches performed from M-mode, S-mode, or U-mode,
respectively. xBE=0 indicates little endian and xBE=1 is big endian. MBE must
be read-only. SBE and UBE must be read only and equal to MBE, if S-mode or
U-mode, respectively, is implemented, or read-only zero otherwise.
NOTE: A further CHERI extension, {cheri_default_ext_name}, optionally makes SXL,
UXL, MBE, SBE, and UBE writeable, so implementations that support multiple base
ISAs must support both {cheri_base_ext_name} and {cheri_default_ext_name}.
[#mtvec, reftext="mtvec"]
==== Machine Trap Vector Base Address Register (mtvec)
The <<mtvec>> register is as defined in cite:[riscv-priv-spec]. It is an
MXLEN-bit register used as the executable vector jumped to when taking traps
into machine mode. It is extended into <<mtvecc>>.
.Machine-mode trap-vector base-address register
include::img/mtvecreg.edn[]
[#mtvecc,reftext="mtvecc"]
==== Machine Trap Vector Base Address Capability Register (mtvecc)
The <<mtvecc>> register is a renamed extension of <<mtvec>> that holds a
capability. Its reset value is the <<infinite-cap>> capability. The capability
represents an executable vector.
.Machine-mode trap-vector base-capability register
include::img/mtveccreg.edn[]
The metadata is WARL as not all fields need to be implemented, for example the
reserved fields will always read as zero.
When interpreting <<mtvecc>> as a capability, as for <<mtvec>>, address bits
[1:0] are always zero (as they are reused by the MODE field).
When MODE=Vectored, all synchronous exceptions into machine mode
cause the <<pcc>> to be set to the capability, whereas
interrupts cause the <<pcc>> to be set to the capability with
its address incremented by four times the interrupt cause number.
Capabilities written to <<mtvecc>> also include writing the MODE field in
**mtvecc.address[1:0]**. As a result, a representability and sealing check is
performed on the capability with the legalized (WARL) MODE field included in
the address. The tag of the capability written to <<mtvecc>> is cleared if
either check fails.
Additionally, when MODE=Vectored the capability has its tag bit cleared if the
capability address + 4 x HICAUSE is not within the representable bounds.
HICAUSE is the largest exception cause value that the implementation can write
to <<mcause>> when an interrupt is taken.
NOTE: When MODE=Vectored, it is only required that address + 4 x HICAUSE is
within representable bounds instead of the capability's bounds. This ensures
that software is not forced to allocate a capability granting access to more
memory for the trap-vector than necessary to handle the trap causes that
actually occur in the system.
[#mscratch, reftext="mscratch"]
==== Machine Scratch Register (mscratch)
The <<mscratch>> register is as defined in cite:[riscv-priv-spec]. It is an
MXLEN-bit read/write register dedicated for use by machine mode. Typically, it
is used to hold a pointer to a machine-mode hart-local context space and
swapped with a user register upon entry to an M-mode trap handler. <<mscratch>>
is extended into <<mscratchc>>.
.Machine-mode scratch register
include::img/mscratchreg.edn[]
[#mscratchc, reftext="mscratchc"]
==== Machine Scratch Capability Register (mscratchc)
The <<mscratchc>> register is a renamed extension of <<mscratch>> that is able to
hold a capability.
{TAG_RESET_CSR}
It is not WARL, all capability fields must be implemented.
.Machine-mode scratch capability register
include::img/mscratchcreg.edn[]
[#mepc,reftext="mepc"]
==== Machine Exception Program Counter (mepc)
The <<mepc>> register is as defined in cite:[riscv-priv-spec]. It is extended
into <<mepcc>>.
.Machine exception program counter register
include::img/mepcreg.edn[]
[#mepcc,reftext="mepcc"]
==== Machine Exception Program Counter Capability (mepcc)
The <<mepcc>> register is a renamed extension of <<mepc>> that is able to hold a
capability. Its reset value is the <<infinite-cap>> capability.
.Machine exception program counter capability register
include::img/mepccreg.edn[]
Capabilities written to <<mepcc>> must be legalised by implicitly zeroing bit
**mepcc[0]**. Additionally, if an implementation allows IALIGN to be
either 16 or 32, then whenever IALIGN=32, the capability read from <<mepcc>>
must be legalised by implicitly zeroing **mepcc[1]**. Therefore, the
capability read or written has its tag bit cleared if the legalised address is
not within the <<section_cap_representable_check>> or if the legalisation
changes the address and the capability is sealed.
NOTE: When reading or writing a sealed capability in <<mepcc>>, the
tag is not cleared if the original address equals the legalized
address.
When a trap is taken into M-mode, <<mepcc>> is written with the <<pcc>>
including the virtual address of the instruction that was interrupted or that
encountered an exception. Otherwise, <<mepcc>> is never written by the
implementation, though it may be explicitly written by software.
As shown in xref:CSR_exevectors[xrefstyle=short], <<mepcc>> is an executable
vector, so it does not need to be able to hold all possible invalid addresses.
Additionally, the capability in <<mepcc>> is unsealed when it is installed in
<<pcc>> on execution of an <<MRET>> instruction.
[#mcause,reftext="mcause"]
==== Machine Cause Register (mcause)
{cheri_base_ext_name} adds a new exception code for CHERI exceptions that
<<mcause>> must be able to represent. The new exception code and its
priority are listed in xref:mcauses[xrefstyle=short] and
xref:exception-priority[xrefstyle=short] respectively. The behavior and usage
of <<mcause>> otherwise remains as described in cite:[riscv-priv-spec].
.Machine cause register
include::img/mcausereg.edn[]
[[mcauses]]
.Machine cause register (mcause) values after trap. Entries added in {cheri_base_ext_name} are in *bold*
[%autowidth,float="center",align="center",cols=">,>,<",options="header",]
|===
|Interrupt |Exception Code |Description
|1 +
1 +
1 +
1
|0 +
1 +
2 +
3
|_Reserved_ +
Supervisor software interrupt +
_Reserved_ +
Machine software interrupt
|1 +
1 +
1 +
1
|4 +
5 +
6 +
7
|_Reserved_ +
Supervisor timer interrupt +
_Reserved_ +
Machine timer interrupt
|1 +
1 +
1 +
1
|8 +
9 +
10 +
11
|_Reserved_ +
Supervisor external interrupt +
_Reserved_ +
Machine external interrupt
|1 +
1
|12-15 +
≥16
|_Reserved_ +
_Designated for platform use_
|0 +
0 +
0 +
0 +
0 +
0 +
0 +
0 +
0 +
0 +
0 +
0 +
0 +
0 +
0 +
0 +
0 +
0 +
*0* +
0 +
0 +
0
|0 +
1 +
2 +
3 +
4 +
5 +
6 +
7 +
8 +
9 +
10 +
11 +
12 +
13 +
14 +
15 +
16-23 +
24-27 +
*{cheri_excep_mcause}* +
29-31 +
32-47 +
48-63 +
≥64
|Instruction address misaligned +
Instruction access fault +
Illegal instruction +
Breakpoint +
Load address misaligned +
Load access fault +
Store/AMO address misaligned +
Store/AMO access fault +
Environment call from U-mode +
Environment call from S-mode +
_Reserved_ +
Environment call from M-mode +
Instruction page fault +
Load page fault +
_Reserved_ +
Store/AMO page fault +
_Reserved_ +
_Designated for custom use_ +
*CHERI fault* +
_Designated for custom use_ +
_Reserved_ +
_Designated for custom use_ +
_Reserved_
|===
[[exception-priority]]
.Synchronous exception priority in decreasing priority order. Entries added in {cheri_base_ext_name} are in *bold*
[%autowidth,float="center",align="center",cols="<,>,<",options="header"]
|===
|Priority |Exc.Code |Description
|_Highest_ |3 |Instruction address breakpoint
| .>|*{cheri_excep_mcause}* .<|*Prior to instruction address translation:* +
*CHERI fault due to PCC checks (tag, execute permission, invalid address and bounds)*
| .>|12, 1 .<|During instruction address translation: +
First encountered page fault or access fault
| .>|1 .<|With physical address for instruction: +
Instruction access fault
| .>|2 +
0 +
8,9,11 +
3 +
3 .<|Illegal instruction +
Instruction address misaligned +
Environment call +
Environment break +
Load/store/AMO address breakpoint
| .>| *{cheri_excep_mcause}* .<| *CHERI faults due to:* +
*PCC <<asr_perm>> clear* +
*Branch/jump target address checks (tag, execute permissions, invalid address and bounds)*
| .>|*{cheri_excep_mcause}* .<|*Prior to address translation for an explicit memory access:* +
*CHERI fault due to capability checks (tag, permissions, invalid address and bounds)*
| .>|4,6 .<|*Load/store/AMO capability address misaligned* +
Optionally: +
Load/store/AMO address misaligned
| .>|13, 15, 5, 7 .<|During address translation for an explicit memory access: +
First encountered page fault or access fault
| .>|5,7 .<|With physical address for an explicit memory access: +
Load/store/AMO access fault
.>|_Lowest_ .>|4,6 .<|If not higher priority: +
Load/store/AMO address misaligned
|===
NOTE: The full details of the CHERI exceptions are in xref:cheri_exception_combs_descriptions[xrefstyle=short].
[#medeleg,reftext="medeleg"]
==== Machine Trap Delegation Register (medeleg)
Bit 28 of <<medeleg>> now refers to a valid exception and so can be used to
delegate CHERI exceptions to supervisor mode.
[#mtval,reftext="mtval"]
==== Machine Trap Value Register (mtval)
ifdef::cheri_v9_annotations[]
WARNING: *CHERI v9 Note:* Encoding and values changed, and generally were
simplified.
endif::[]
The <<mtval>> register is an MXLEN-bit read-write register formatted as shown
in xref:mtval-format[xrefstyle=short]. When a data memory access gives rise to
a CHERI fault taken into M-mode, <<mtval>> is written with the
MXLEN-bit effective address which caused the fault according to the existing
rules for reporting load/store addresses from cite:[riscv-priv-spec]. In this case
the TYPE field of <<mtval2>> shown in xref:mtval2-cheri-type[xrefstyle=short] is
set to {cheri_excep_type_data}. For all other CHERI faults it is set to zero.
The behavior of <<mtval>> is otherwise as described in cite:[riscv-priv-spec].
If the hardware platform specifies that no exceptions set <<mtval>> to a
non-zero value, then <<mtval>> is read-only zero for all CHERI exceptions.
.Machine trap value register
[#mtval-format]
include::img/mtvalreg.edn[]
[#mtval2,reftext="mtval2"]
==== Machine Trap Value Register 2 (mtval2)
ifdef::cheri_v9_annotations[]
WARNING: *CHERI v9 Note:* This CSR was not part of CHERIv9.
endif::[]
The <<mtval2>> register is an MXLEN-bit read-write register, which is added as part of the
Hypervisor extension cite:[riscv-priv-spec]. {cheri_base_ext_name} also requires the implementation of this CSR.
When a CHERI fault is taken into M-mode, <<mtval2>> is written
with additional CHERI-specific exception information with the format shown in
xref:mtval2-format[xrefstyle=short] to assist software in handling the trap.
If <<mtval>> is read-only zero for CHERI exceptions then <<mtval2>> is also read-only zero
for CHERI exceptions.
.Machine trap value register 2 format for CHERI faults
[#mtval2-format]
include::img/mtval2reg.edn[]
NOTE: <<mtval2>> is also used for Hypervisor guest physical addresses, and so the implemented bits must also cover that use case.
If Hypervisor is not implemented then all WPRI fields in <<mtval2-format>> are read-only-zero.
TYPE is a CHERI-specific fault type that caused the exception while CAUSE
is the cause of the fault. The possible CHERI types and causes are encoded as
shown in xref:mtval2-cheri-type[xrefstyle=short] and
xref:mtval2-cheri-causes[xrefstyle=short] respectively.
.Encoding of TYPE field
[#mtval2-cheri-type,width=65%,float="center",align="center",options=header,cols="30%,70%"]
|==============================================================================
| CHERI Type Code | Description
| {cheri_excep_type_pcc} | CHERI instruction fetch fault
| {cheri_excep_type_data} | CHERI data fault due to load, store or AMO
| {cheri_excep_type_jump} | CHERI jump or branch fault
| 3-15 | Reserved
|==============================================================================
.Encoding of CAUSE field
[#mtval2-cheri-causes,width=55%,float="center",align="center",options=header]
|==============================================================================
| CHERI Cause Code | Description
| {cheri_excep_cause_tag} | Tag violation
| {cheri_excep_cause_seal} | Seal violation
| {cheri_excep_cause_perm} | Permission violation
| {cheri_excep_cause_inv_addr} | Invalid address violation
| {cheri_excep_cause_bounds} | Bounds violation
| 5-15 | Reserved
|==============================================================================
CHERI violations have the following order in priority:
. Tag violation (_Highest_)
. Seal violation
. Permission violation
. Invalid address violation
. Bounds violation (_Lowest_)
[#supervisor-level-csrs-section]
=== Supervisor-Level CSRs
{cheri_base_ext_name} extends some of the existing RISC-V CSRs to be able to
hold capabilities or with other new functions. <<pcc>> must grant <<asr_perm>>
to access S-mode CSRs regardless of the RISC-V privilege mode.
[#stvec,reftext="stvec"]
==== Supervisor Trap Vector Base Address Register (stvec)
The <<stvec>> register is as defined in cite:[riscv-priv-spec]. It is an
SXLEN-bit register used as the executable vector jumped to when taking traps
into supervisor mode. It is extended into <<stvecc>>.
.Supervisor trap-vector base-address register
include::img/stvecreg.edn[]
[#stvecc,reftext="stvecc"]
==== Supervisor Trap Vector Base Address Capability Register (stvecc)
The <<stvec>> register is an SXLEN-bit WARL read/write register that holds the
trap vector configuration, consisting of a vector base address (BASE) and a
vector mode (MODE). The <<stvecc>> register is a renamed extension of <<stvec>>
that is able to hold a capability. Its reset value is the <<infinite-cap>>
capability.
.Supervisor trap-vector base-capability register
include::img/stveccreg.edn[]
The handling of <<stvecc>> is otherwise identical to <<mtvecc>>, but in
supervisor mode.
[#sscratch, reftext="sscratch"]
==== Supervisor Scratch Register (sscratch)
The <<sscratch>> register is as defined in cite:[riscv-priv-spec]. It is an
MXLEN-bit read/write register dedicated for use by supervisor mode. Typically,
it is used to hold a pointer to a supervisor-mode hart-local context space and
swapped with a user register upon entry to an S-mode trap handler. <<sscratch>>
is extended into <<sscratchc>>.
.Supervisor-mode scratch register
include::img/sscratchreg.edn[]
[#sscratchc, reftext="sscratchc"]
==== Supervisor Scratch Capability Register (sscratchc)
The <<sscratchc>> register is a renamed extension of <<sscratch>> that is able to
hold a capability.
{TAG_RESET_CSR}
It is not WARL, all capability fields must be implemented.
.Supervisor scratch capability register
include::img/sscratchcreg.edn[]
[#sepc,reftext="sepc"]
==== Supervisor Exception Program Counter (sepc)
The <<sepc>> register is as defined in cite:[riscv-priv-spec]. It is extended
into <<sepcc>>.
.Supervisor exception program counter register
include::img/sepcreg.edn[]
[#sepcc,reftext="sepcc"]
==== Supervisor Exception Program Counter Capability (sepcc)
The <<sepcc>> register is a renamed extension of <<sepc>> that is able to hold a
capability. Its reset value is the <<infinite-cap>> capability.
As shown in xref:CSR_exevectors[xrefstyle=short], <<sepcc>> is an executable
vector, so it need not be able to hold all possible invalid addresses.
Additionally, the capability in <<sepcc>> is unsealed when it is installed in
<<pcc>> on execution of an <<SRET>> instruction. The handling of <<sepcc>> is
otherwise identical to <<mepcc>>, but in supervisor mode.
.Supervisor exception program counter capability register
include::img/sepccreg.edn[]
[#scause,reftext="scause"]
==== Supervisor Cause Register (scause)
{cheri_base_ext_name} adds a new exception code for CHERI exceptions that
<<scause>> must be able to represent. The new exception code and its priority
are listed in xref:scauses[xrefstyle=short] and
xref:exception-priority[xrefstyle=short] respectively. The behavior and usage
of <<scause>> otherwise remains as described in cite:[riscv-priv-spec].
.Supervisor cause register
include::img/scausereg.edn[]
[[scauses]]
.Supervisor cause register (scause) values after trap. Causes added in {cheri_base_ext_name} are in *bold*
[%autowidth,float="center",align="center",cols=">,>,3",options="header"]
|===
|Interrupt |Exception Code |Description
|1 +
1 +
1 +
1 +
1 +
1 +
1 +
1
|0 +
1 +
2-4 +
5 +
6-8 +
9 +
10-15 +
≥16
|_Reserved_ +
Supervisor software interrupt +
_Reserved_ +
Supervisor timer interrupt +
_Reserved_ +
Supervisor external interrupt +
_Reserved_ +
_Designated for platform use_
|0 +
0 +
0 +
0 +
0 +
0 +
0 +
0 +
0 +
0 +
0 +
0 +
0 +
0 +
0 +
0 +
0 +
*0* +
0 +
0 +
0
|0 +
1 +
2 +
3 +
4 +
5 +
6 +
7 +
8 +
9 +
10-11 +
12 +
13 +
14 +
15 +
16-23 +
24-27 +
*28* +
29-31 +
32-47 +
48-63 +
≥64
|Instruction address misaligned +
Instruction access fault +
Illegal instruction +
Breakpoint +