Allow mstatus.MPP to store bad values; instead, validate on MRET
[riscv-isa-sim.git] / riscv / processor.h
1 // See LICENSE for license details.
2 #ifndef _RISCV_PROCESSOR_H
3 #define _RISCV_PROCESSOR_H
4
5 #include "decode.h"
6 #include "config.h"
7 #include "devices.h"
8 #include <string>
9 #include <vector>
10 #include <map>
11
12 class processor_t;
13 class mmu_t;
14 typedef reg_t (*insn_func_t)(processor_t*, insn_t, reg_t);
15 class sim_t;
16 class trap_t;
17 class extension_t;
18 class disassembler_t;
19
20 struct insn_desc_t
21 {
22 insn_bits_t match;
23 insn_bits_t mask;
24 insn_func_t rv32;
25 insn_func_t rv64;
26 };
27
28 struct commit_log_reg_t
29 {
30 reg_t addr;
31 reg_t data;
32 };
33
34 typedef struct
35 {
36 uint8_t prv;
37 bool step;
38 bool ebreakm;
39 bool ebreakh;
40 bool ebreaks;
41 bool ebreaku;
42 bool halt;
43 uint8_t cause;
44 } dcsr_t;
45
46 // architectural state of a RISC-V hart
47 struct state_t
48 {
49 void reset();
50
51 reg_t pc;
52 regfile_t<reg_t, NXPR, true> XPR;
53 regfile_t<freg_t, NFPR, false> FPR;
54
55 // control and status registers
56 reg_t prv;
57 reg_t mstatus;
58 reg_t mepc;
59 reg_t mbadaddr;
60 reg_t mscratch;
61 reg_t mtvec;
62 reg_t mcause;
63 reg_t minstret;
64 reg_t mie;
65 reg_t mip;
66 reg_t medeleg;
67 reg_t mideleg;
68 reg_t mucounteren;
69 reg_t mscounteren;
70 reg_t sepc;
71 reg_t sbadaddr;
72 reg_t sscratch;
73 reg_t stvec;
74 reg_t sptbr;
75 reg_t scause;
76 reg_t dpc;
77 reg_t dscratch;
78 dcsr_t dcsr;
79
80 uint32_t fflags;
81 uint32_t frm;
82 bool serialized; // whether timer CSRs are in a well-defined state
83
84 // When true, execute a single instruction and then enter debug mode. This
85 // can only be set by executing dret.
86 enum {
87 STEP_NONE,
88 STEP_STEPPING,
89 STEP_STEPPED
90 } single_step;
91
92 reg_t load_reservation;
93
94 #ifdef RISCV_ENABLE_COMMITLOG
95 commit_log_reg_t log_reg_write;
96 reg_t last_inst_priv;
97 #endif
98 };
99
100 // this class represents one processor in a RISC-V machine.
101 class processor_t : public abstract_device_t
102 {
103 public:
104 processor_t(const char* isa, sim_t* sim, uint32_t id, bool halt_on_reset=false);
105 ~processor_t();
106
107 void set_debug(bool value);
108 void set_histogram(bool value);
109 void reset();
110 void step(size_t n); // run for n cycles
111 void set_csr(int which, reg_t val);
112 void raise_interrupt(reg_t which);
113 reg_t get_csr(int which);
114 mmu_t* get_mmu() { return mmu; }
115 state_t* get_state() { return &state; }
116 extension_t* get_extension() { return ext; }
117 bool supports_extension(unsigned char ext) {
118 if (ext >= 'a' && ext <= 'z') ext += 'A' - 'a';
119 return ext >= 'A' && ext <= 'Z' && ((isa >> (ext - 'A')) & 1);
120 }
121 void set_privilege(reg_t);
122 void yield_load_reservation() { state.load_reservation = (reg_t)-1; }
123 void update_histogram(reg_t pc);
124 const disassembler_t* get_disassembler() { return disassembler; }
125
126 void register_insn(insn_desc_t);
127 void register_extension(extension_t*);
128
129 // MMIO slave interface
130 bool load(reg_t addr, size_t len, uint8_t* bytes);
131 bool store(reg_t addr, size_t len, const uint8_t* bytes);
132
133 // When true, display disassembly of each instruction that's executed.
134 bool debug;
135
136 private:
137 sim_t* sim;
138 mmu_t* mmu; // main memory is always accessed via the mmu
139 extension_t* ext;
140 disassembler_t* disassembler;
141 state_t state;
142 uint32_t id;
143 unsigned max_xlen;
144 unsigned xlen;
145 reg_t isa;
146 std::string isa_string;
147 bool histogram_enabled;
148 bool halt_on_reset;
149
150 std::vector<insn_desc_t> instructions;
151 std::map<reg_t,uint64_t> pc_histogram;
152
153 static const size_t OPCODE_CACHE_SIZE = 8191;
154 insn_desc_t opcode_cache[OPCODE_CACHE_SIZE];
155
156 void check_timer();
157 void take_interrupt(); // take a trap if any interrupts are pending
158 void take_trap(trap_t& t, reg_t epc); // take an exception
159 void disasm(insn_t insn); // disassemble and print an instruction
160 int paddr_bits();
161
162 void enter_debug_mode(uint8_t cause);
163
164 friend class sim_t;
165 friend class mmu_t;
166 friend class rtc_t;
167 friend class extension_t;
168
169 void parse_isa_string(const char* isa);
170 void build_opcode_map();
171 void register_base_instructions();
172 insn_func_t decode_insn(insn_t insn);
173 };
174
175 reg_t illegal_instruction(processor_t* p, insn_t insn, reg_t pc);
176
177 #define REGISTER_INSN(proc, name, match, mask) \
178 extern reg_t rv32_##name(processor_t*, insn_t, reg_t); \
179 extern reg_t rv64_##name(processor_t*, insn_t, reg_t); \
180 proc->register_insn((insn_desc_t){match, mask, rv32_##name, rv64_##name});
181
182 #endif