xref: /openbmc/qemu/target/riscv/machine.c (revision cdfb2905)
1 /*
2  * RISC-V VMState Description
3  *
4  * Copyright (c) 2020 Huawei Technologies Co., Ltd
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU General Public License,
8  * version 2 or later, as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include "qemu/osdep.h"
20 #include "cpu.h"
21 #include "qemu/error-report.h"
22 #include "sysemu/kvm.h"
23 #include "migration/cpu.h"
24 #include "sysemu/cpu-timers.h"
25 #include "debug.h"
26 
27 static bool pmp_needed(void *opaque)
28 {
29     RISCVCPU *cpu = opaque;
30     CPURISCVState *env = &cpu->env;
31 
32     return riscv_feature(env, RISCV_FEATURE_PMP);
33 }
34 
35 static int pmp_post_load(void *opaque, int version_id)
36 {
37     RISCVCPU *cpu = opaque;
38     CPURISCVState *env = &cpu->env;
39     int i;
40 
41     for (i = 0; i < MAX_RISCV_PMPS; i++) {
42         pmp_update_rule_addr(env, i);
43     }
44     pmp_update_rule_nums(env);
45 
46     return 0;
47 }
48 
49 static const VMStateDescription vmstate_pmp_entry = {
50     .name = "cpu/pmp/entry",
51     .version_id = 1,
52     .minimum_version_id = 1,
53     .fields = (VMStateField[]) {
54         VMSTATE_UINTTL(addr_reg, pmp_entry_t),
55         VMSTATE_UINT8(cfg_reg, pmp_entry_t),
56         VMSTATE_END_OF_LIST()
57     }
58 };
59 
60 static const VMStateDescription vmstate_pmp = {
61     .name = "cpu/pmp",
62     .version_id = 1,
63     .minimum_version_id = 1,
64     .needed = pmp_needed,
65     .post_load = pmp_post_load,
66     .fields = (VMStateField[]) {
67         VMSTATE_STRUCT_ARRAY(env.pmp_state.pmp, RISCVCPU, MAX_RISCV_PMPS,
68                              0, vmstate_pmp_entry, pmp_entry_t),
69         VMSTATE_END_OF_LIST()
70     }
71 };
72 
73 static bool hyper_needed(void *opaque)
74 {
75     RISCVCPU *cpu = opaque;
76     CPURISCVState *env = &cpu->env;
77 
78     return riscv_has_ext(env, RVH);
79 }
80 
81 static const VMStateDescription vmstate_hyper = {
82     .name = "cpu/hyper",
83     .version_id = 2,
84     .minimum_version_id = 2,
85     .needed = hyper_needed,
86     .fields = (VMStateField[]) {
87         VMSTATE_UINTTL(env.hstatus, RISCVCPU),
88         VMSTATE_UINTTL(env.hedeleg, RISCVCPU),
89         VMSTATE_UINT64(env.hideleg, RISCVCPU),
90         VMSTATE_UINTTL(env.hcounteren, RISCVCPU),
91         VMSTATE_UINTTL(env.htval, RISCVCPU),
92         VMSTATE_UINTTL(env.htinst, RISCVCPU),
93         VMSTATE_UINTTL(env.hgatp, RISCVCPU),
94         VMSTATE_UINTTL(env.hgeie, RISCVCPU),
95         VMSTATE_UINTTL(env.hgeip, RISCVCPU),
96         VMSTATE_UINT64(env.htimedelta, RISCVCPU),
97         VMSTATE_UINT64(env.vstimecmp, RISCVCPU),
98 
99         VMSTATE_UINTTL(env.hvictl, RISCVCPU),
100         VMSTATE_UINT8_ARRAY(env.hviprio, RISCVCPU, 64),
101 
102         VMSTATE_UINT64(env.vsstatus, RISCVCPU),
103         VMSTATE_UINTTL(env.vstvec, RISCVCPU),
104         VMSTATE_UINTTL(env.vsscratch, RISCVCPU),
105         VMSTATE_UINTTL(env.vsepc, RISCVCPU),
106         VMSTATE_UINTTL(env.vscause, RISCVCPU),
107         VMSTATE_UINTTL(env.vstval, RISCVCPU),
108         VMSTATE_UINTTL(env.vsatp, RISCVCPU),
109         VMSTATE_UINTTL(env.vsiselect, RISCVCPU),
110 
111         VMSTATE_UINTTL(env.mtval2, RISCVCPU),
112         VMSTATE_UINTTL(env.mtinst, RISCVCPU),
113 
114         VMSTATE_UINTTL(env.stvec_hs, RISCVCPU),
115         VMSTATE_UINTTL(env.sscratch_hs, RISCVCPU),
116         VMSTATE_UINTTL(env.sepc_hs, RISCVCPU),
117         VMSTATE_UINTTL(env.scause_hs, RISCVCPU),
118         VMSTATE_UINTTL(env.stval_hs, RISCVCPU),
119         VMSTATE_UINTTL(env.satp_hs, RISCVCPU),
120         VMSTATE_UINT64(env.mstatus_hs, RISCVCPU),
121 
122         VMSTATE_END_OF_LIST()
123     }
124 };
125 
126 static bool vector_needed(void *opaque)
127 {
128     RISCVCPU *cpu = opaque;
129     CPURISCVState *env = &cpu->env;
130 
131     return riscv_has_ext(env, RVV);
132 }
133 
134 static const VMStateDescription vmstate_vector = {
135     .name = "cpu/vector",
136     .version_id = 2,
137     .minimum_version_id = 2,
138     .needed = vector_needed,
139     .fields = (VMStateField[]) {
140             VMSTATE_UINT64_ARRAY(env.vreg, RISCVCPU, 32 * RV_VLEN_MAX / 64),
141             VMSTATE_UINTTL(env.vxrm, RISCVCPU),
142             VMSTATE_UINTTL(env.vxsat, RISCVCPU),
143             VMSTATE_UINTTL(env.vl, RISCVCPU),
144             VMSTATE_UINTTL(env.vstart, RISCVCPU),
145             VMSTATE_UINTTL(env.vtype, RISCVCPU),
146             VMSTATE_BOOL(env.vill, RISCVCPU),
147             VMSTATE_END_OF_LIST()
148         }
149 };
150 
151 static bool pointermasking_needed(void *opaque)
152 {
153     RISCVCPU *cpu = opaque;
154     CPURISCVState *env = &cpu->env;
155 
156     return riscv_has_ext(env, RVJ);
157 }
158 
159 static const VMStateDescription vmstate_pointermasking = {
160     .name = "cpu/pointer_masking",
161     .version_id = 1,
162     .minimum_version_id = 1,
163     .needed = pointermasking_needed,
164     .fields = (VMStateField[]) {
165         VMSTATE_UINTTL(env.mmte, RISCVCPU),
166         VMSTATE_UINTTL(env.mpmmask, RISCVCPU),
167         VMSTATE_UINTTL(env.mpmbase, RISCVCPU),
168         VMSTATE_UINTTL(env.spmmask, RISCVCPU),
169         VMSTATE_UINTTL(env.spmbase, RISCVCPU),
170         VMSTATE_UINTTL(env.upmmask, RISCVCPU),
171         VMSTATE_UINTTL(env.upmbase, RISCVCPU),
172 
173         VMSTATE_END_OF_LIST()
174     }
175 };
176 
177 static bool rv128_needed(void *opaque)
178 {
179     RISCVCPU *cpu = opaque;
180     CPURISCVState *env = &cpu->env;
181 
182     return env->misa_mxl_max == MXL_RV128;
183 }
184 
185 static const VMStateDescription vmstate_rv128 = {
186     .name = "cpu/rv128",
187     .version_id = 1,
188     .minimum_version_id = 1,
189     .needed = rv128_needed,
190     .fields = (VMStateField[]) {
191         VMSTATE_UINTTL_ARRAY(env.gprh, RISCVCPU, 32),
192         VMSTATE_UINT64(env.mscratchh, RISCVCPU),
193         VMSTATE_UINT64(env.sscratchh, RISCVCPU),
194         VMSTATE_END_OF_LIST()
195     }
196 };
197 
198 static bool kvmtimer_needed(void *opaque)
199 {
200     return kvm_enabled();
201 }
202 
203 static int cpu_post_load(void *opaque, int version_id)
204 {
205     RISCVCPU *cpu = opaque;
206     CPURISCVState *env = &cpu->env;
207 
208     env->kvm_timer_dirty = true;
209     return 0;
210 }
211 
212 static const VMStateDescription vmstate_kvmtimer = {
213     .name = "cpu/kvmtimer",
214     .version_id = 1,
215     .minimum_version_id = 1,
216     .needed = kvmtimer_needed,
217     .post_load = cpu_post_load,
218     .fields = (VMStateField[]) {
219         VMSTATE_UINT64(env.kvm_timer_time, RISCVCPU),
220         VMSTATE_UINT64(env.kvm_timer_compare, RISCVCPU),
221         VMSTATE_UINT64(env.kvm_timer_state, RISCVCPU),
222         VMSTATE_END_OF_LIST()
223     }
224 };
225 
226 static bool debug_needed(void *opaque)
227 {
228     RISCVCPU *cpu = opaque;
229 
230     return cpu->cfg.debug;
231 }
232 
233 static int debug_post_load(void *opaque, int version_id)
234 {
235     RISCVCPU *cpu = opaque;
236     CPURISCVState *env = &cpu->env;
237 
238     if (icount_enabled()) {
239         env->itrigger_enabled = riscv_itrigger_enabled(env);
240     }
241 
242     return 0;
243 }
244 
245 static const VMStateDescription vmstate_debug = {
246     .name = "cpu/debug",
247     .version_id = 2,
248     .minimum_version_id = 2,
249     .needed = debug_needed,
250     .post_load = debug_post_load,
251     .fields = (VMStateField[]) {
252         VMSTATE_UINTTL(env.trigger_cur, RISCVCPU),
253         VMSTATE_UINTTL_ARRAY(env.tdata1, RISCVCPU, RV_MAX_TRIGGERS),
254         VMSTATE_UINTTL_ARRAY(env.tdata2, RISCVCPU, RV_MAX_TRIGGERS),
255         VMSTATE_UINTTL_ARRAY(env.tdata3, RISCVCPU, RV_MAX_TRIGGERS),
256         VMSTATE_END_OF_LIST()
257     }
258 };
259 
260 static int riscv_cpu_post_load(void *opaque, int version_id)
261 {
262     RISCVCPU *cpu = opaque;
263     CPURISCVState *env = &cpu->env;
264 
265     env->xl = cpu_recompute_xl(env);
266     riscv_cpu_update_mask(env);
267     return 0;
268 }
269 
270 static bool smstateen_needed(void *opaque)
271 {
272     RISCVCPU *cpu = opaque;
273 
274     return cpu->cfg.ext_smstateen;
275 }
276 
277 static const VMStateDescription vmstate_smstateen = {
278     .name = "cpu/smtateen",
279     .version_id = 1,
280     .minimum_version_id = 1,
281     .needed = smstateen_needed,
282     .fields = (VMStateField[]) {
283         VMSTATE_UINT64_ARRAY(env.mstateen, RISCVCPU, 4),
284         VMSTATE_UINT64_ARRAY(env.hstateen, RISCVCPU, 4),
285         VMSTATE_UINT64_ARRAY(env.sstateen, RISCVCPU, 4),
286         VMSTATE_END_OF_LIST()
287     }
288 };
289 
290 static bool envcfg_needed(void *opaque)
291 {
292     RISCVCPU *cpu = opaque;
293     CPURISCVState *env = &cpu->env;
294 
295     return (env->priv_ver >= PRIV_VERSION_1_12_0 ? 1 : 0);
296 }
297 
298 static const VMStateDescription vmstate_envcfg = {
299     .name = "cpu/envcfg",
300     .version_id = 1,
301     .minimum_version_id = 1,
302     .needed = envcfg_needed,
303     .fields = (VMStateField[]) {
304         VMSTATE_UINT64(env.menvcfg, RISCVCPU),
305         VMSTATE_UINTTL(env.senvcfg, RISCVCPU),
306         VMSTATE_UINT64(env.henvcfg, RISCVCPU),
307         VMSTATE_END_OF_LIST()
308     }
309 };
310 
311 static bool pmu_needed(void *opaque)
312 {
313     RISCVCPU *cpu = opaque;
314 
315     return cpu->cfg.pmu_num;
316 }
317 
318 static const VMStateDescription vmstate_pmu_ctr_state = {
319     .name = "cpu/pmu",
320     .version_id = 1,
321     .minimum_version_id = 1,
322     .needed = pmu_needed,
323     .fields = (VMStateField[]) {
324         VMSTATE_UINTTL(mhpmcounter_val, PMUCTRState),
325         VMSTATE_UINTTL(mhpmcounterh_val, PMUCTRState),
326         VMSTATE_UINTTL(mhpmcounter_prev, PMUCTRState),
327         VMSTATE_UINTTL(mhpmcounterh_prev, PMUCTRState),
328         VMSTATE_BOOL(started, PMUCTRState),
329         VMSTATE_END_OF_LIST()
330     }
331 };
332 
333 const VMStateDescription vmstate_riscv_cpu = {
334     .name = "cpu",
335     .version_id = 6,
336     .minimum_version_id = 6,
337     .post_load = riscv_cpu_post_load,
338     .fields = (VMStateField[]) {
339         VMSTATE_UINTTL_ARRAY(env.gpr, RISCVCPU, 32),
340         VMSTATE_UINT64_ARRAY(env.fpr, RISCVCPU, 32),
341         VMSTATE_UINT8_ARRAY(env.miprio, RISCVCPU, 64),
342         VMSTATE_UINT8_ARRAY(env.siprio, RISCVCPU, 64),
343         VMSTATE_UINTTL(env.pc, RISCVCPU),
344         VMSTATE_UINTTL(env.load_res, RISCVCPU),
345         VMSTATE_UINTTL(env.load_val, RISCVCPU),
346         VMSTATE_UINTTL(env.frm, RISCVCPU),
347         VMSTATE_UINTTL(env.badaddr, RISCVCPU),
348         VMSTATE_UINTTL(env.guest_phys_fault_addr, RISCVCPU),
349         VMSTATE_UINTTL(env.priv_ver, RISCVCPU),
350         VMSTATE_UINTTL(env.vext_ver, RISCVCPU),
351         VMSTATE_UINT32(env.misa_mxl, RISCVCPU),
352         VMSTATE_UINT32(env.misa_ext, RISCVCPU),
353         VMSTATE_UINT32(env.misa_mxl_max, RISCVCPU),
354         VMSTATE_UINT32(env.misa_ext_mask, RISCVCPU),
355         VMSTATE_UINT32(env.features, RISCVCPU),
356         VMSTATE_UINTTL(env.priv, RISCVCPU),
357         VMSTATE_UINTTL(env.virt, RISCVCPU),
358         VMSTATE_UINT64(env.resetvec, RISCVCPU),
359         VMSTATE_UINTTL(env.mhartid, RISCVCPU),
360         VMSTATE_UINT64(env.mstatus, RISCVCPU),
361         VMSTATE_UINT64(env.mip, RISCVCPU),
362         VMSTATE_UINT64(env.miclaim, RISCVCPU),
363         VMSTATE_UINT64(env.mie, RISCVCPU),
364         VMSTATE_UINT64(env.mideleg, RISCVCPU),
365         VMSTATE_UINTTL(env.satp, RISCVCPU),
366         VMSTATE_UINTTL(env.stval, RISCVCPU),
367         VMSTATE_UINTTL(env.medeleg, RISCVCPU),
368         VMSTATE_UINTTL(env.stvec, RISCVCPU),
369         VMSTATE_UINTTL(env.sepc, RISCVCPU),
370         VMSTATE_UINTTL(env.scause, RISCVCPU),
371         VMSTATE_UINTTL(env.mtvec, RISCVCPU),
372         VMSTATE_UINTTL(env.mepc, RISCVCPU),
373         VMSTATE_UINTTL(env.mcause, RISCVCPU),
374         VMSTATE_UINTTL(env.mtval, RISCVCPU),
375         VMSTATE_UINTTL(env.miselect, RISCVCPU),
376         VMSTATE_UINTTL(env.siselect, RISCVCPU),
377         VMSTATE_UINTTL(env.scounteren, RISCVCPU),
378         VMSTATE_UINTTL(env.mcounteren, RISCVCPU),
379         VMSTATE_UINTTL(env.mcountinhibit, RISCVCPU),
380         VMSTATE_STRUCT_ARRAY(env.pmu_ctrs, RISCVCPU, RV_MAX_MHPMCOUNTERS, 0,
381                              vmstate_pmu_ctr_state, PMUCTRState),
382         VMSTATE_UINTTL_ARRAY(env.mhpmevent_val, RISCVCPU, RV_MAX_MHPMEVENTS),
383         VMSTATE_UINTTL_ARRAY(env.mhpmeventh_val, RISCVCPU, RV_MAX_MHPMEVENTS),
384         VMSTATE_UINTTL(env.sscratch, RISCVCPU),
385         VMSTATE_UINTTL(env.mscratch, RISCVCPU),
386         VMSTATE_UINT64(env.stimecmp, RISCVCPU),
387 
388         VMSTATE_END_OF_LIST()
389     },
390     .subsections = (const VMStateDescription * []) {
391         &vmstate_pmp,
392         &vmstate_hyper,
393         &vmstate_vector,
394         &vmstate_pointermasking,
395         &vmstate_rv128,
396         &vmstate_kvmtimer,
397         &vmstate_envcfg,
398         &vmstate_debug,
399         &vmstate_smstateen,
400         NULL
401     }
402 };
403