-
Notifications
You must be signed in to change notification settings - Fork 700
/
Copy pathmachine.tex
3800 lines (3347 loc) · 148 KB
/
machine.tex
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
\chapter{Machine-Level ISA, Version 1.12}
\label{machine}
This chapter describes the machine-level operations available in
machine-mode (M-mode), which is the highest privilege mode in a RISC-V
system. M-mode is used for low-level access to a
hardware platform and is the first mode entered at reset. M-mode can
also be used to implement features that are too difficult or expensive
to implement in hardware directly. The RISC-V machine-level ISA
contains a common core that is extended depending on which other
privilege levels are supported and other details of the hardware
implementation.
\section{Machine-Level CSRs}
In addition to the machine-level CSRs described in this section,
M-mode code can access all CSRs at lower privilege levels.
\subsection{Machine ISA Register {\tt misa}}
\label{sec:misa}
The {\tt misa} CSR is a \warl\ read-write register
reporting the ISA supported by the hart. This register must be
readable in any implementation, but a value of zero can be returned to
indicate the {\tt misa} register has not been implemented, requiring
that CPU capabilities be determined through a separate non-standard
mechanism.
\begin{figure*}[h!]
{\footnotesize
\begin{center}
\begin{tabular}{c@{}c@{}L}
\instbitrange{MXLEN-1}{MXLEN-2} &
\instbitrange{MXLEN-3}{26} &
\instbitrange{25}{0} \\
\hline
\multicolumn{1}{|c|}{MXL[1:0] (\warl)} &
\multicolumn{1}{c|}{0 (\warl)} &
\multicolumn{1}{c|}{Extensions[25:0] (\warl)} \\
\hline
2 & MXLEN-28 & 26 \\
\end{tabular}
\end{center}
}
\vspace{-0.1in}
\caption{Machine ISA register ({\tt misa}).}
\label{misareg}
\end{figure*}
The MXL (Machine XLEN) field encodes the native base integer ISA width
as shown in Table~\ref{misabase}. The MXL field may be writable in
implementations that support multiple base ISAs. The effective
XLEN in M-mode, {\em MXLEN}, is given by the setting of MXL, or has a
fixed value if {\tt misa} is zero. The MXL field is always set to the
widest supported ISA variant at reset.
\begin{table*}[h!]
\begin{center}
\begin{tabular}{|r|r|}
\hline
MXL & XLEN \\
\hline
1 & 32 \\
2 & 64 \\
3 & 128 \\
\hline
\end{tabular}
\end{center}
\caption{Encoding of MXL field in {\tt misa}}
\label{misabase}
\end{table*}
The {\tt misa} CSR is MXLEN bits wide. If the value read from {\tt misa} is
nonzero, field MXL of that value always denotes the current MXLEN. If a write
to {\tt misa} causes MXLEN to change, the position of MXL moves to the
most-significant two bits of {\tt misa} at the new width.
\begin{commentary}
The base width can be quickly ascertained using branches on the sign
of the returned {\tt misa} value, and possibly a shift left by one and
a second branch on the sign. These checks can be written in assembly
code without knowing the register width (XLEN) of the machine. The
base width is given by $\mbox{XLEN}=2^{\mbox{MXL+4}}$.
The base width can also be found if {\tt misa} is zero, by placing the
immediate 4 in a register then shifting the register left by 31 bits
at a time. If zero after one shift, then the machine is RV32. If
zero after two shifts, then the machine is RV64, else RV128.
\end{commentary}
The Extensions field encodes the presence of the standard extensions,
with a single bit per letter of the alphabet (bit 0 encodes presence
of extension ``A'' , bit 1 encodes presence of extension ``B'',
through to bit 25 which encodes ``Z''). The ``I'' bit will be set for
RV32I, RV64I, RV128I base ISAs, and the ``E'' bit will be set for
RV32E. The Extensions field is a \warl\ field that can contain writable bits
where the implementation allows the supported ISA to be modified. At
reset, the Extensions field shall contain the maximal set of supported
extensions, and I shall be selected over E if both are available.
When a standard extension is disabled by clearing its bit in {\tt misa}, the instructions and CSRs defined or modified by the extension revert to their defined or reserved behaviors as if the extension is not implemented.
The design of the RV128I base ISA is not yet complete, and while much of the remainder of this
specification is expected to apply to RV128, this version of the document
focuses only on RV32 and RV64.
The ``U'' and ``S'' bits will be set if there is support for user and
supervisor modes respectively.
The ``X'' bit will be set if there are any non-standard extensions.
\begin{table*}
\begin{center}
\begin{tabular}{|r|r|l|}
\hline
Bit & Character & Description \\
\hline
0 & A & Atomic extension \\
1 & B & {\em Tentatively reserved for Bit-Manipulation extension} \\
2 & C & Compressed extension \\
3 & D & Double-precision floating-point extension \\
4 & E & RV32E base ISA \\
5 & F & Single-precision floating-point extension \\
6 & G & {\em Reserved} \\
7 & H & Hypervisor extension \\
8 & I & RV32I/64I/128I base ISA \\
9 & J & {\em Tentatively reserved for Dynamically Translated Languages extension} \\
10 & K & {\em Reserved} \\
11 & L & {\em Reserved} \\
12 & M & Integer Multiply/Divide extension \\
13 & N & {\em Tentatively reserved for User-Level Interrupts extension} \\
14 & O & {\em Reserved} \\
15 & P & {\em Tentatively reserved for Packed-SIMD extension} \\
16 & Q & Quad-precision floating-point extension \\
17 & R & {\em Reserved} \\
18 & S & Supervisor mode implemented \\
19 & T & {\em Reserved} \\
20 & U & User mode implemented \\
21 & V & {\em Tentatively reserved for Vector extension} \\
22 & W & {\em Reserved} \\
23 & X & Non-standard extensions present \\
24 & Y & {\em Reserved} \\
25 & Z & {\em Reserved} \\
\hline
\end{tabular}
\end{center}
\caption{Encoding of Extensions field in {\tt misa}. All bits that are
reserved for future use must return zero when read.}
\label{misaletters}
\end{table*}
\begin{commentary}
The {\tt misa} CSR exposes a rudimentary catalog of CPU features
to machine-mode code. More extensive information can be obtained in
machine mode by probing other machine registers, and examining other
ROM storage in the system as part of the boot process.
We require that lower privilege levels execute environment calls
instead of reading CPU registers to determine features available at
each privilege level. This enables virtualization layers to alter the
ISA observed at any level, and supports a much richer command
interface without burdening hardware designs.
\end{commentary}
The ``E'' bit is read-only. Unless {\tt misa} is all read-only zero, the ``E''
bit always reads as the complement of the ``I'' bit. An implementation that
supports both RV32E and RV32I can select RV32E by clearing the ``I'' bit.
If an ISA feature {\em x} depends on an ISA feature {\em y}, then attempting
to enable feature {\em x} but disable feature {\em y} results in both features
being disabled. For example, setting ``F''=0 and ``D''=1 results in both
``F'' and ``D'' being cleared.
An implementation may impose additional constraints on the collective setting
of two or more {\tt misa} fields, in which case they function collectively as
a single \warl\ field. An attempt to write an unsupported combination causes
those bits to be set to some supported combination.
Writing {\tt misa} may increase IALIGN, e.g., by disabling the ``C''
extension.
If an instruction that would write {\tt misa} increases IALIGN, and
the subsequent instruction's address is not IALIGN-bit aligned, the
write to {\tt misa} is suppressed, leaving {\tt misa} unchanged.
When software enables an extension that was previously disabled, then all
state uniquely associated with that extension is \unspecified, unless
otherwise specified by that extension.
\subsection{Machine Vendor ID Register {\tt mvendorid}}
The {\tt mvendorid} CSR is a 32-bit read-only register providing
the JEDEC manufacturer ID of the provider of the core. This register
must be readable in any implementation, but a value of 0 can be
returned to indicate the field is not implemented or that this is a
non-commercial implementation.
\begin{figure*}[h!]
{\footnotesize
\begin{center}
\begin{tabular}{JS}
\instbitrange{31}{7} &
\instbitrange{6}{0} \\
\hline
\multicolumn{1}{|c|}{Bank} &
\multicolumn{1}{c|}{Offset} \\
\hline
25 & 7 \\
\end{tabular}
\end{center}
}
\vspace{-0.1in}
\caption{Vendor ID register ({\tt mvendorid}).}
\label{mvendorreg}
\end{figure*}
JEDEC manufacturer IDs are ordinarily encoded as a sequence of one-byte
continuation codes {\tt 0x7f}, terminated by a one-byte ID not equal to
{\tt 0x7f}, with an odd parity bit in the most-significant bit of each byte.
{\tt mvendorid} encodes the number of one-byte continuation
codes in the Bank field, and encodes the final byte in the Offset field,
discarding the parity bit. For example, the JEDEC manufacturer ID
{\tt 0x7f 0x7f 0x7f 0x7f 0x7f 0x7f 0x7f 0x7f 0x7f 0x7f 0x7f 0x7f 0x8a}
(twelve continuation codes followed by {\tt 0x8a}) would be encoded in the
{\tt mvendorid} CSR as {\tt 0x60a}.
\begin{commentary}
In JEDEC's parlance, the bank number is one greater than the number of
continuation codes; hence, the {\tt mvendorid} Bank field encodes a value
that is one less than the JEDEC bank number.
\end{commentary}
\begin{commentary}
Previously the vendor ID was to be a number allocated by RISC-V
International, but this duplicates the work of JEDEC in maintaining a
manufacturer ID standard. At time of writing, registering a
manufacturer ID with JEDEC has a one-time cost of \$500.
\end{commentary}
\subsection{Machine Architecture ID Register {\tt marchid}}
The {\tt marchid} CSR is an MXLEN-bit read-only register encoding the
base microarchitecture of the hart. This register must be readable in
any implementation, but a value of 0 can be returned to indicate the
field is not implemented. The combination of {\tt mvendorid} and {\tt
marchid} should uniquely identify the type of hart microarchitecture
that is implemented.
\begin{figure*}[h!]
{\footnotesize
\begin{center}
\begin{tabular}{J}
\instbitrange{MXLEN-1}{0} \\
\hline
\multicolumn{1}{|c|}{Architecture ID} \\
\hline
MXLEN \\
\end{tabular}
\end{center}
}
\vspace{-0.1in}
\caption{Machine Architecture ID register ({\tt marchid}).}
\label{marchreg}
\end{figure*}
Open-source project architecture IDs are allocated globally by
RISC-V International, and have non-zero architecture IDs with a zero
most-significant-bit (MSB). Commercial architecture IDs are allocated
by each commercial vendor independently, but must have the MSB set and
cannot contain zero in the remaining MXLEN-1 bits.
\begin{commentary}
The intent is for the architecture ID to represent the
microarchitecture associated with the repo around which development
occurs rather than a particular organization. Commercial fabrications
of open-source designs should (and might be required by the license
to) retain the original architecture ID. This will aid in reducing
fragmentation and tool support costs, as well as provide attribution.
Open-source architecture IDs are administered by RISC-V International
and should only be allocated to released, functioning open-source
projects. Commercial architecture IDs can be managed independently by
any registered vendor but are required to have IDs disjoint from the
open-source architecture IDs (MSB set) to prevent collisions if a
vendor wishes to use both closed-source and open-source
microarchitectures.
The convention adopted within the following Implementation field can
be used to segregate branches of the same architecture design,
including by organization. The {\tt misa} register also helps
distinguish different variants of a design.
\end{commentary}
\subsection{Machine Implementation ID Register {\tt mimpid}}
The {\tt mimpid} CSR provides a unique encoding of the version of the
processor implementation. This register must be readable in any
implementation, but a value of 0 can be returned to indicate that the
field is not implemented. The Implementation value should reflect the
design of the RISC-V processor itself and not any surrounding system.
\begin{figure*}[h!]
{\footnotesize
\begin{center}
\begin{tabular}{J}
\instbitrange{MXLEN-1}{0} \\
\hline
\multicolumn{1}{|c|}{Implementation} \\
\hline
MXLEN \\
\end{tabular}
\end{center}
}
\vspace{-0.1in}
\caption{Machine Implementation ID register ({\tt mimpid}).}
\label{mimpidreg}
\end{figure*}
\begin{commentary}
The format of this field is left to the provider of the architecture
source code, but will often be printed by standard tools as a
hexadecimal string without any leading or trailing zeros, so the
Implementation value can be left-justified (i.e., filled in from
most-significant nibble down) with subfields aligned on nibble
boundaries to ease human readability.
\end{commentary}
\subsection{Hart ID Register {\tt mhartid}}
The {\tt mhartid} CSR is an MXLEN-bit read-only register
containing the integer ID of the hardware thread running the code.
This register must be readable in any implementation. Hart IDs might
not necessarily be numbered contiguously in a multiprocessor system,
but at least one hart must have a hart ID of zero. Hart IDs must be
unique within the execution environment.
\begin{figure*}[h!]
{\footnotesize
\begin{center}
\begin{tabular}{J}
\instbitrange{MXLEN-1}{0} \\
\hline
\multicolumn{1}{|c|}{Hart ID}\\
\hline
MXLEN \\
\end{tabular}
\end{center}
}
\vspace{-0.1in}
\caption{Hart ID register ({\tt mhartid}).}
\label{mhartidreg}
\end{figure*}
\begin{commentary}
In certain cases, we must ensure exactly one hart runs some code
(e.g., at reset), and so require one hart to have a known hart ID of
zero.
For efficiency, system implementers should aim to reduce the magnitude
of the largest hart ID used in a system.
\end{commentary}
\subsection{Machine Status Registers ({\tt mstatus} and {\tt mstatush})}
The {\tt mstatus} register is an MXLEN-bit read/write register
formatted as shown in Figure~\ref{mstatusreg-rv32} for RV32 and
Figure~\ref{mstatusreg} for RV64. The {\tt mstatus}
register keeps track of and controls the hart's current operating
state. A restricted view of {\tt mstatus} appears as the
{\tt sstatus} register in the S-level ISA.
\begin{figure*}[h!]
{\footnotesize
\begin{center}
\setlength{\tabcolsep}{4pt}
\begin{tabular}{cKccccccc}
\\
\instbit{31} &
\instbitrange{30}{23} &
\instbit{22} &
\instbit{21} &
\instbit{20} &
\instbit{19} &
\instbit{18} &
\instbit{17} &
\\
\hline
\multicolumn{1}{|c|}{SD} &
\multicolumn{1}{c|}{\wpri} &
\multicolumn{1}{c|}{TSR} &
\multicolumn{1}{c|}{TW} &
\multicolumn{1}{c|}{TVM} &
\multicolumn{1}{c|}{MXR} &
\multicolumn{1}{c|}{SUM} &
\multicolumn{1}{c|}{MPRV} &
\\
\hline
1 & 8 & 1 & 1 & 1 & 1 & 1 & 1 & \\
\end{tabular}
\begin{tabular}{cWWcWccccccccc}
\\
&
\instbitrange{16}{15} &
\instbitrange{14}{13} &
\instbitrange{12}{11} &
\instbitrange{10}{9} &
\instbit{8} &
\instbit{7} &
\instbit{6} &
\instbit{5} &
\instbit{4} &
\instbit{3} &
\instbit{2} &
\instbit{1} &
\instbit{0} \\
\hline
&
\multicolumn{1}{|c|}{XS[1:0]} &
\multicolumn{1}{c|}{FS[1:0]} &
\multicolumn{1}{c|}{MPP[1:0]} &
\multicolumn{1}{c|}{VS[1:0]} &
\multicolumn{1}{c|}{SPP} &
\multicolumn{1}{c|}{MPIE} &
\multicolumn{1}{c|}{UBE} &
\multicolumn{1}{c|}{SPIE} &
\multicolumn{1}{c|}{\wpri} &
\multicolumn{1}{c|}{MIE} &
\multicolumn{1}{c|}{\wpri} &
\multicolumn{1}{c|}{SIE} &
\multicolumn{1}{c|}{\wpri} \\
\hline
& 2 & 2 & 2 & 2 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 \\
\end{tabular}
\end{center}
}
\vspace{-0.1in}
\caption{Machine-mode status register ({\tt mstatus}) for RV32.}
\label{mstatusreg-rv32}
\end{figure*}
\begin{figure*}[h!]
{\footnotesize
\begin{center}
\setlength{\tabcolsep}{4pt}
\scalebox{0.95}{
\begin{tabular}{cRccccYcccccc}
\\
\instbit{63} &
\instbitrange{62}{38} &
\instbit{37} &
\instbit{36} &
\instbitrange{35}{34} &
\instbitrange{33}{32} &
\instbitrange{31}{23} &
\instbit{22} &
\instbit{21} &
\instbit{20} &
\instbit{19} &
\instbit{18} &
\\
\hline
\multicolumn{1}{|c|}{SD} &
\multicolumn{1}{c|}{\wpri} &
\multicolumn{1}{c|}{MBE} &
\multicolumn{1}{c|}{SBE} &
\multicolumn{1}{c|}{SXL[1:0]} &
\multicolumn{1}{c|}{UXL[1:0]} &
\multicolumn{1}{c|}{\wpri} &
\multicolumn{1}{c|}{TSR} &
\multicolumn{1}{c|}{TW} &
\multicolumn{1}{c|}{TVM} &
\multicolumn{1}{c|}{MXR} &
\multicolumn{1}{c|}{SUM} &
\\
\hline
1 & 25 & 1 & 1 & 2 & 2 & 9 & 1 & 1 & 1 & 1 & 1 & \\
\end{tabular}}
\scalebox{0.95}{
\begin{tabular}{ccWWcWccccccccc}
\\
&
\instbit{17} &
\instbitrange{16}{15} &
\instbitrange{14}{13} &
\instbitrange{12}{11} &
\instbitrange{10}{9} &
\instbit{8} &
\instbit{7} &
\instbit{6} &
\instbit{5} &
\instbit{4} &
\instbit{3} &
\instbit{2} &
\instbit{1} &
\instbit{0} \\
\hline
&
\multicolumn{1}{|c|}{MPRV} &
\multicolumn{1}{c|}{XS[1:0]} &
\multicolumn{1}{c|}{FS[1:0]} &
\multicolumn{1}{c|}{MPP[1:0]} &
\multicolumn{1}{c|}{VS[1:0]} &
\multicolumn{1}{c|}{SPP} &
\multicolumn{1}{c|}{MPIE} &
\multicolumn{1}{c|}{UBE} &
\multicolumn{1}{c|}{SPIE} &
\multicolumn{1}{c|}{\wpri} &
\multicolumn{1}{c|}{MIE} &
\multicolumn{1}{c|}{\wpri} &
\multicolumn{1}{c|}{SIE} &
\multicolumn{1}{c|}{\wpri} \\
\hline
& 1 & 2 & 2 & 2 & 2 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 \\
\end{tabular}}
\end{center}
}
\vspace{-0.1in}
\caption{Machine-mode status register ({\tt mstatus}) for RV64.}
\label{mstatusreg}
\end{figure*}
For RV32 only, {\tt mstatush} is a 32-bit read/write register formatted
as shown in Figure~\ref{mstatushreg}.
Bits 30:4 of {\tt mstatush} generally contain the same fields found in
bits 62:36 of {\tt mstatus} for RV64.
Fields SD, SXL, and UXL do not exist in {\tt mstatush}.
\begin{figure*}[h!]
{\footnotesize
\begin{center}
\setlength{\tabcolsep}{4pt}
\begin{tabular}{JccF}
\\
\instbitrange{31}{6} &
\instbit{5} &
\instbit{4} &
\instbitrange{3}{0} \\
\hline
\multicolumn{1}{|c|}{\wpri} &
\multicolumn{1}{c|}{MBE} &
\multicolumn{1}{c|}{SBE} &
\multicolumn{1}{c|}{\wpri} \\
\hline
26 & 1 & 1 & 4 \\
\end{tabular}
\end{center}
}
\vspace{-0.1in}
\caption{Additional machine-mode status register ({\tt mstatush}) for RV32.}
\label{mstatushreg}
\end{figure*}
\subsubsection{Privilege and Global Interrupt-Enable Stack in {\tt mstatus} register}
\label{privstack}
Global interrupt-enable bits, MIE and SIE, are provided for M-mode and
S-mode respectively.
These bits are primarily used to guarantee atomicity
with respect to interrupt handlers in the current privilege mode.
\begin{commentary}
The global {\em x}\/IE bits are located in the low-order bits of {\tt mstatus},
allowing them to be atomically set or cleared with a single CSR
instruction.
\end{commentary}
When a hart is executing in privilege mode {\em x}, interrupts are
globally enabled when {\em x}\/IE=1 and globally disabled when {\em
x}\/IE=0. Interrupts for lower-privilege modes, {\em w}$<${\em x},
are always globally disabled regardless of the setting of any global
{\em w}\/IE bit for the lower-privilege mode.
Interrupts for
higher-privilege modes, {\em y}$>${\em x}, are always globally enabled
regardless of the setting of the global {\em y}\/IE bit for the
higher-privilege mode.
Higher-privilege-level code can use separate
per-interrupt enable bits to disable selected higher-privilege-mode
interrupts before ceding control to a lower-privilege mode.
\begin{commentary}
A higher-privilege mode {\em y} could disable all of its interrupts
before ceding control to a lower-privilege mode but this would be
unusual as it would leave only a synchronous trap, non-maskable
interrupt, or reset as means to regain control of the hart.
\end{commentary}
To support nested traps, each privilege mode {\em x} that can respond to
interrupts has a two-level
stack of interrupt-enable bits and privilege modes. {\em x}\/PIE
holds the value of the interrupt-enable bit active prior to the trap,
and {\em x}\/PP holds the previous privilege mode. The {\em x}\/PP
fields can only hold privilege modes up to {\em x}, so MPP is
two bits wide and SPP is one bit wide. When
a trap is taken from privilege mode {\em y} into privilege mode {\em
x}, {\em x}\/PIE is set to the value of {\em x}\/IE; {\em x}\/IE is set to
0; and {\em x}\/PP is set to {\em y}.
\begin{commentary}
For lower privilege modes, any trap (synchronous or asynchronous) is
usually taken at a higher privilege mode with interrupts disabled upon entry.
The higher-level trap handler will either service the trap and return
using the stacked information, or, if not returning immediately to the
interrupted context, will save the privilege stack before re-enabling
interrupts, so only one entry per stack is required.
\end{commentary}
An MRET or SRET instruction is used to return from
a trap in M-mode or S-mode respectively. When
executing an {\em x}\/RET instruction, supposing {\em x}\/PP holds the
value {\em y}, {\em x}\/IE is set to {\em x}\/PIE; the privilege mode
is changed to {\em y}; {\em x}\/PIE is set to 1; and {\em x}\/PP is
set to the least-privileged supported mode (U if U-mode is implemented, else M).
If {\em x}\/PP$\neq$M, {\em x}\/RET also sets MPRV=0.
\begin{commentary}
Setting {\em x}\/PP to the least-privileged supported mode on an {\em x}\/RET
helps identify software bugs in the management of the two-level privilege-mode
stack.
\end{commentary}
{\em x}\/PP fields are \warl\ fields that can hold only privilege mode {\em x}
and any implemented privilege mode lower than {\em x}. If privilege mode {\em
x} is not implemented, then {\em x}\/PP must be read-only~0.
\begin{commentary}
M-mode software can determine whether a privilege mode is implemented
by writing that mode to MPP then reading it back.
If the machine provides only U and M modes, then only a single
hardware storage bit is required to represent either 00 or 11 in MPP.
\end{commentary}
\subsubsection{Base ISA Control in {\tt mstatus} Register}
\label{xlen-control}
For RV64 systems, the SXL and UXL fields are \warl\ fields
that control the value of XLEN for S-mode and U-mode,
respectively. The encoding of these fields is the same as the MXL
field of {\tt misa}, shown in Table~\ref{misabase}. The effective
XLEN in S-mode and U-mode are termed {\em SXLEN} and {\em UXLEN},
respectively.
For RV32 systems, the SXL and UXL fields do not exist, and
SXLEN=32 and UXLEN=32.
For RV64 systems, if S-mode is not supported, then SXL is read-only
zero. Otherwise, it is a \warl\ field that encodes the current value of
SXLEN. In particular, an implementation may make SXL be a read-only
field whose value always ensures that SXLEN=MXLEN.
For RV64 systems, if U-mode is not supported, then UXL is read-only
zero. Otherwise, it is a \warl\ field that encodes the current value of
UXLEN. In particular, an implementation may make UXL be a read-only
field whose value always ensures that UXLEN=MXLEN or UXLEN=SXLEN.
Whenever XLEN in any mode is set to a value less than the widest
supported XLEN, all operations must ignore source operand register
bits above the configured XLEN, and must sign-extend results to fill
the entire widest supported XLEN in the destination register.
Similarly, {\tt pc} bits above XLEN are ignored, and when the {\tt pc}
is written, it is sign-extended to fill the widest supported XLEN.
\begin{commentary}
We require that operations always fill the entire underlying hardware
registers with defined values to avoid implementation-defined
behavior.
To reduce hardware complexity, the architecture imposes no checks that
lower-privilege modes have XLEN settings less than or equal to the
next-higher privilege mode. In practice, such settings would almost
always be a software bug, but machine operation is well-defined even in this
case.
\end{commentary}
If MXLEN is changed from 32 to a wider width, each of {\tt mstatus} fields SXL and
UXL, if not restricted to a single value, gets the value corresponding to the
widest supported width not wider than the new MXLEN.
\subsubsection{Memory Privilege in {\tt mstatus} Register}
The MPRV (Modify PRiVilege) bit modifies the {\em effective privilege mode},
i.e., the privilege level at which loads
and stores execute. When MPRV=0, loads and stores
behave as normal, using the translation and protection mechanisms of the
current privilege mode.
When MPRV=1, load and store memory addresses are translated and protected, and
endianness is applied, as though the current privilege mode were set to MPP.
Instruction address-translation and protection are unaffected by the setting
of MPRV. MPRV is read-only 0 if U-mode is not supported.
An MRET or SRET instruction that changes the privilege mode to a mode
less privileged than M also sets MPRV=0.
The MXR (Make eXecutable Readable) bit modifies the privilege with which loads
access virtual memory. When MXR=0, only loads from pages marked readable (R=1
in Figure~\ref{sv32pte}) will succeed. When MXR=1, loads from pages marked
either readable or executable (R=1 or X=1) will succeed. MXR has no effect
when page-based virtual memory is not in effect. MXR is read-only 0 if
S-mode is not supported.
\begin{commentary}
The MPRV and MXR mechanisms were conceived to improve the efficiency of M-mode
routines that emulate missing hardware features, e.g., misaligned loads and
stores. MPRV obviates the need to perform address translation in software.
MXR allows instruction words to be loaded from pages marked execute-only.
The current privilege mode and the privilege mode specified by MPP might have
different XLEN settings. When MPRV=1, load and store memory addresses are
treated as though the current XLEN were set to MPP's XLEN, following the rules
in Section~\ref{xlen-control}.
\end{commentary}
The SUM (permit Supervisor User Memory access) bit modifies the privilege with
which S-mode loads and stores access virtual memory.
When SUM=0, S-mode memory accesses to pages that are accessible by U-mode (U=1
in Figure~\ref{sv32pte}) will fault. When SUM=1, these accesses are
permitted. SUM has no effect when page-based virtual memory is not in effect.
Note that, while SUM is ordinarily ignored when not executing in S-mode, it
{\em is} in effect when MPRV=1 and MPP=S. SUM is read-only 0 if S-mode is
not supported or if {\tt satp}.MODE is read-only~0.
The MXR and SUM mechanisms only affect the interpretation of permissions
encoded in page-table entries. In particular, they have no impact on whether
access-fault exceptions are raised due to PMAs or PMP.
\subsubsection{Endianness Control in {\tt mstatus} and {\tt mstatush} Registers}
The MBE, SBE, and UBE bits in {\tt mstatus} and {\tt mstatush} are
\warl\ fields that control the endianness of memory accesses other than
instruction fetches.
Instruction fetches are always little-endian.
MBE controls whether non-instruction-fetch memory accesses made from
M-mode (assuming {\tt mstatus}.MPRV=0) are little-endian (MBE=0) or
big-endian (MBE=1).
If S-mode is not supported, SBE is read-only~0.
Otherwise, SBE controls whether explicit load and store memory accesses made
from S-mode are little-endian (SBE=0) or big-endian (SBE=1).
If U-mode is not supported, UBE is read-only~0.
Otherwise, UBE controls whether explicit load and store memory accesses made
from U-mode are little-endian (UBE=0) or big-endian (UBE=1).
For {\em implicit} accesses to supervisor-level memory management data
structures, such as page tables, endianness is always controlled by SBE.
Since changing SBE alters the implementation's interpretation of these data
structures, if any such data structures remain in use across a change to SBE,
M-mode software must follow such a change to SBE by executing an
SFENCE.VMA instruction with {\em rs1}={\tt x0} and {\em rs2}={\tt x0}.
\begin{commentary}
Only in contrived scenarios will a given memory-management data structure be
interpreted as both little-endian and big-endian.
In practice, SBE will only be changed at runtime on world switches, in which
case neither the old nor new memory-management data structure will be
reinterpreted in a different endianness.
In this case, no additional SFENCE.VMA is necessary, beyond what would
ordinarily be required for a world switch.
\end{commentary}
If S-mode is supported, an implementation may make SBE be a read-only
copy of MBE.
If U-mode is supported, an implementation may make UBE be a read-only
copy of either MBE or SBE.
\begin{commentary}
An implementation supports only little-endian memory accesses if fields
MBE, SBE, and UBE are all read-only~0.
An implementation supports only big-endian memory accesses (aside from
instruction fetches) if MBE is read-only 1 and SBE and UBE are each
read-only 1 when S-mode and U-mode are supported.
\end{commentary}
\begin{commentary}
Volume I defines a hart's address space as a circular sequence of
$2^{\text{XLEN}}$ bytes at consecutive addresses.
The correspondence between addresses and byte locations is fixed and not
affected by any endianness mode.
Rather, the applicable endianness mode determines the order of mapping
between memory bytes and a multibyte quantity (halfword, word, etc.).
\end{commentary}
\begin{commentary}
Standard RISC-V ABIs are expected to be purely little-endian-only or
big-endian-only, with no accommodation for mixing endianness.
Nevertheless, endianness control has been defined so as to permit, for
instance, an OS of one endianness to execute user-mode programs of the
opposite endianness.
Consideration has been given also to the possibility of nonstandard
usages whereby software flips the endianness of memory accesses as
needed.
\end{commentary}
\begin{commentary}
RISC-V instructions are uniformly little-endian to decouple instruction
encoding from the current endianness settings, for the benefit of both
hardware and software.
Otherwise, for instance, a RISC-V assembler or disassembler would always
need to know the intended active endianness, despite that the endianness
mode might change dynamically during execution.
In contrast, by giving instructions a fixed endianness, it is sometimes
possible for carefully written software to be endianness-agnostic even in
binary form, much like position-independent code.
The choice to have instructions be only little-endian does have
consequences, however, for RISC-V software that encodes or decodes
machine instructions.
In big-endian mode, such software must account for the fact that explicit
loads and stores have endianness opposite that of instructions, for
example by swapping byte order after loads and before stores.
\end{commentary}
\subsubsection{Virtualization Support in {\tt mstatus} Register}
\label{virt-control}
The TVM (Trap Virtual Memory) bit is a \warl\ field that supports intercepting
supervisor virtual-memory management operations. When TVM=1,
attempts to read or write the {\tt satp} CSR or execute an SFENCE.VMA or
SINVAL.VMA instruction while executing in S-mode will raise an illegal instruction
exception. When TVM=0, these operations are permitted in S-mode.
TVM is read-only 0 when S-mode is not supported.
\begin{commentary}
The TVM mechanism improves virtualization efficiency by permitting guest
operating systems to execute in S-mode, rather than classically virtualizing
them in U-mode. This approach obviates the need to trap accesses to most
S-mode CSRs.
Trapping {\tt satp} accesses and the SFENCE.VMA and SINVAL.VMA instructions
provides the hooks necessary to lazily populate shadow page tables.
\end{commentary}
The TW (Timeout Wait) bit is a \warl\ field that supports intercepting the WFI
instruction (see Section~\ref{wfi}).
When TW=0, the WFI instruction may execute in lower
privilege modes when not prevented for some other reason. When TW=1,
then if WFI is executed in any less-privileged mode, and it does not complete
within an implementation-specific, bounded time limit, the WFI instruction
causes an illegal instruction exception. The time limit may always be 0, in which
case WFI always causes an illegal instruction exception in less-privileged modes
when TW=1. TW is read-only 0 when there are no modes less privileged than
M.
\begin{commentary}
Trapping the WFI
instruction can trigger a world switch to another guest OS, rather than
wastefully idling in the current guest.
\end{commentary}
When S-mode is implemented, then executing WFI in U-mode causes an illegal
instruction exception, unless it completes within an implementation-specific,
bounded time limit. A future revision of this specification might add
a feature that allows S-mode to selectively permit WFI in U-mode. Such
a feature would only be active when TW=0.
The TSR (Trap SRET) bit is a \warl\ field that
supports intercepting the supervisor exception return
instruction, SRET. When TSR=1, attempts to execute SRET while executing in
S-mode will raise an illegal instruction exception. When TSR=0, this
operation is permitted in S-mode. TSR is read-only 0 when S-mode is not
supported.
\begin{commentary}
Trapping SRET is necessary to emulate the hypervisor extension
(see Chapter~\ref{hypervisor}) on implementations that do not provide it.
\end{commentary}
\subsubsection{Extension Context Status in {\tt mstatus} Register}
Supporting substantial extensions is one of the primary goals of
RISC-V, and hence we define a standard interface to allow unchanged
privileged-mode code, particularly a supervisor-level OS, to support
arbitrary user-mode state extensions.
\begin{commentary}
To date, the V extension is the only standard extension that defines
additional state beyond the floating-point CSR and data registers.
\end{commentary}
The FS[1:0] and VS[1:0] \warl\ fields and the XS[1:0] read-only field are used
to reduce the cost of context save and restore by setting and tracking
the current state of the floating-point unit and any other user-mode
extensions respectively.
The FS field encodes the status of the floating-point unit state, including
the floating-point registers {\tt f0}--{\tt f31} and the CSRs
{\tt fcsr}, {\tt frm}, and {\tt fflags}.
The VS field encodes the status of the vector extension state, including
the vector registers {\tt v0}--{\tt v31} and the CSRs
{\tt vcsr}, {\tt vxrm}, {\tt vxsat}, {\tt vstart}, {\tt vl}, {\tt vtype},
and {\tt vlenb}.
The XS field encodes the
status of additional user-mode extensions and associated state.
These fields can be checked by a context switch routine to quickly
determine whether a state save or restore is required. If a save or
restore is required, additional instructions and CSRs are typically
required to effect and optimize the process.
\begin{commentary}
The design anticipates that most context switches will not need to
save/restore state in either or both of the floating-point unit or
other extensions, so provides a fast check via the SD bit.
\end{commentary}
The FS, VS, and XS fields use the same status encoding as shown in
Table~\ref{fsxsencoding}, with the four possible status values being
Off, Initial, Clean, and Dirty.
\begin{table*}[h!]
\begin{center}
\begin{tabular}{|r|l|l|}
\hline
Status & FS and VS Meaning & XS Meaning\\
\hline
0 & Off & All off \\
1 & Initial & None dirty or clean, some on\\
2 & Clean & None dirty, some clean \\
3 & Dirty & Some dirty \\
\hline
\end{tabular}
\end{center}
\caption{Encoding of FS[1:0], VS[1:0], and XS[1:0] status fields.}
\label{fsxsencoding}
\end{table*}
If the F extension is implemented, the FS field shall not be
read-only zero.
If neither the F extension nor S-mode is implemented, then FS is
read-only zero.
If S-mode is implemented but the F extension is not, FS may optionally
be read-only zero.
\begin{commentary}
Implementations with S-mode but without the F extension are
permitted, but not required, to make the FS field be read-only zero.
Some such implementations will choose {\em not} to have the FS
field be read-only zero, so as to enable emulation of the F extension for
both S-mode and U-mode via invisible traps into M-mode.
\end{commentary}
If the {\tt v} registers are implemented, the VS field shall not be
read-only zero.
If neither the {\tt v} registers nor S-mode is implemented, then VS
is read-only zero.
If S-mode is implemented but the {\tt v} registers are not, VS may
optionally be read-only zero.
In systems without additional user extensions requiring new state, the
XS field is read-only zero. Every additional extension with state
provides a CSR field that encodes the equivalent of the XS states.
The XS field represents a summary of all
extensions' status as shown in Table~\ref{fsxsencoding}.
\begin{commentary}
The XS field effectively reports the maximum status value across all
user-extension status fields, though individual extensions can use a
different encoding than XS.
\end{commentary}
The SD bit is a read-only bit that summarizes whether either the FS,
VS, or XS fields signal the presence of some dirty state that will
require saving extended user context to memory. If FS, XS, and VS are all
read-only zero, then SD is also always zero.
When an extension's status is set to Off, any instruction that
attempts to read or write the corresponding state will cause an illegal instruction
exception. When the status is Initial, the corresponding state should
have an initial constant value. When the status is Clean, the
corresponding state is potentially different from the initial value,
but matches the last value stored on a context swap. When the status
is Dirty, the corresponding state has potentially been modified since
the last context save.
During a context save, the responsible privileged code need only write
out the corresponding state if its status is Dirty, and can then reset
the extension's status to Clean. During a context restore, the
context need only be loaded from memory if the status is Clean (it
should never be Dirty at restore). If the status is Initial, the
context must be set to an initial constant value on context restore to
avoid a security hole, but this can be done without accessing memory.
For example, the floating-point registers can all be initialized to
the immediate value 0.
The FS and XS fields are read by the privileged code before saving the
context. The FS field is set directly by privileged code when
resuming a user context, while the XS field is set indirectly by
writing to the status register of the individual extensions. The
status fields will also be updated during execution of instructions,
regardless of privilege mode.
Extensions to the user-mode ISA often include additional user-mode
state, and this state can be considerably larger than the base integer
registers. The extensions might only be used for some applications,
or might only be needed for short phases within a single application.
To improve performance, the user-mode extension can define additional
instructions to allow user-mode software to return the unit to an
initial state or even to turn off the unit.
For example, a coprocessor might require to be configured before use