xref: /qemu/hw/ppc/spapr_nested.c (revision 1d7e6318afcf060f4cd6ae3681d29a6cddf294ff)
1 #include "qemu/osdep.h"
2 #include "qemu/cutils.h"
3 #include "exec/exec-all.h"
4 #include "helper_regs.h"
5 #include "hw/ppc/ppc.h"
6 #include "hw/ppc/spapr.h"
7 #include "hw/ppc/spapr_cpu_core.h"
8 #include "hw/ppc/spapr_nested.h"
9 #include "mmu-book3s-v3.h"
10 #include "cpu-models.h"
11 #include "qemu/log.h"
12 
13 void spapr_nested_reset(SpaprMachineState *spapr)
14 {
15     if (spapr_get_cap(spapr, SPAPR_CAP_NESTED_KVM_HV)) {
16         spapr_unregister_nested_hv();
17         spapr_register_nested_hv();
18     } else if (spapr_get_cap(spapr, SPAPR_CAP_NESTED_PAPR)) {
19         spapr->nested.capabilities_set = false;
20         spapr_unregister_nested_papr();
21         spapr_register_nested_papr();
22         spapr_nested_gsb_init();
23     } else {
24         spapr->nested.api = 0;
25     }
26 }
27 
28 uint8_t spapr_nested_api(SpaprMachineState *spapr)
29 {
30     return spapr->nested.api;
31 }
32 
33 #ifdef CONFIG_TCG
34 
35 bool spapr_get_pate_nested_hv(SpaprMachineState *spapr, PowerPCCPU *cpu,
36                               target_ulong lpid, ppc_v3_pate_t *entry)
37 {
38     uint64_t patb, pats;
39 
40     assert(lpid != 0);
41 
42     patb = spapr->nested.ptcr & PTCR_PATB;
43     pats = spapr->nested.ptcr & PTCR_PATS;
44 
45     /* Check if partition table is properly aligned */
46     if (patb & MAKE_64BIT_MASK(0, pats + 12)) {
47         return false;
48     }
49 
50     /* Calculate number of entries */
51     pats = 1ull << (pats + 12 - 4);
52     if (pats <= lpid) {
53         return false;
54     }
55 
56     /* Grab entry */
57     patb += 16 * lpid;
58     entry->dw0 = ldq_phys(CPU(cpu)->as, patb);
59     entry->dw1 = ldq_phys(CPU(cpu)->as, patb + 8);
60     return true;
61 }
62 
63 static
64 SpaprMachineStateNestedGuest *spapr_get_nested_guest(SpaprMachineState *spapr,
65                                                      target_ulong guestid)
66 {
67     SpaprMachineStateNestedGuest *guest;
68 
69     guest = g_hash_table_lookup(spapr->nested.guests, GINT_TO_POINTER(guestid));
70     return guest;
71 }
72 
73 bool spapr_get_pate_nested_papr(SpaprMachineState *spapr, PowerPCCPU *cpu,
74                                 target_ulong lpid, ppc_v3_pate_t *entry)
75 {
76     SpaprMachineStateNestedGuest *guest;
77     assert(lpid != 0);
78     guest = spapr_get_nested_guest(spapr, lpid);
79     if (!guest) {
80         return false;
81     }
82 
83     entry->dw0 = guest->parttbl[0];
84     entry->dw1 = guest->parttbl[1];
85     return true;
86 }
87 
88 #define PRTS_MASK      0x1f
89 
90 static target_ulong h_set_ptbl(PowerPCCPU *cpu,
91                                SpaprMachineState *spapr,
92                                target_ulong opcode,
93                                target_ulong *args)
94 {
95     target_ulong ptcr = args[0];
96 
97     if (!spapr_get_cap(spapr, SPAPR_CAP_NESTED_KVM_HV)) {
98         return H_FUNCTION;
99     }
100 
101     if ((ptcr & PRTS_MASK) + 12 - 4 > 12) {
102         return H_PARAMETER;
103     }
104 
105     spapr->nested.ptcr = ptcr; /* Save new partition table */
106 
107     return H_SUCCESS;
108 }
109 
110 static target_ulong h_tlb_invalidate(PowerPCCPU *cpu,
111                                      SpaprMachineState *spapr,
112                                      target_ulong opcode,
113                                      target_ulong *args)
114 {
115     /*
116      * The spapr virtual hypervisor nested HV implementation retains no L2
117      * translation state except for TLB. And the TLB is always invalidated
118      * across L1<->L2 transitions, so nothing is required here.
119      */
120 
121     return H_SUCCESS;
122 }
123 
124 static target_ulong h_copy_tofrom_guest(PowerPCCPU *cpu,
125                                         SpaprMachineState *spapr,
126                                         target_ulong opcode,
127                                         target_ulong *args)
128 {
129     /*
130      * This HCALL is not required, L1 KVM will take a slow path and walk the
131      * page tables manually to do the data copy.
132      */
133     return H_FUNCTION;
134 }
135 
136 static void nested_save_state(struct nested_ppc_state *save, PowerPCCPU *cpu)
137 {
138     CPUPPCState *env = &cpu->env;
139     SpaprMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
140 
141     memcpy(save->gpr, env->gpr, sizeof(save->gpr));
142 
143     save->lr = env->lr;
144     save->ctr = env->ctr;
145     save->cfar = env->cfar;
146     save->msr = env->msr;
147     save->nip = env->nip;
148 
149     save->cr = ppc_get_cr(env);
150     save->xer = cpu_read_xer(env);
151 
152     save->lpcr = env->spr[SPR_LPCR];
153     save->lpidr = env->spr[SPR_LPIDR];
154     save->pcr = env->spr[SPR_PCR];
155     save->dpdes = env->spr[SPR_DPDES];
156     save->hfscr = env->spr[SPR_HFSCR];
157     save->srr0 = env->spr[SPR_SRR0];
158     save->srr1 = env->spr[SPR_SRR1];
159     save->sprg0 = env->spr[SPR_SPRG0];
160     save->sprg1 = env->spr[SPR_SPRG1];
161     save->sprg2 = env->spr[SPR_SPRG2];
162     save->sprg3 = env->spr[SPR_SPRG3];
163     save->pidr = env->spr[SPR_BOOKS_PID];
164     save->ppr = env->spr[SPR_PPR];
165 
166     if (spapr_nested_api(spapr) == NESTED_API_PAPR) {
167         save->amor = env->spr[SPR_AMOR];
168         save->dawr0 = env->spr[SPR_DAWR0];
169         save->dawrx0 = env->spr[SPR_DAWRX0];
170         save->ciabr = env->spr[SPR_CIABR];
171         save->purr = env->spr[SPR_PURR];
172         save->spurr = env->spr[SPR_SPURR];
173         save->ic = env->spr[SPR_IC];
174         save->vtb = env->spr[SPR_VTB];
175         save->hdar = env->spr[SPR_HDAR];
176         save->hdsisr = env->spr[SPR_HDSISR];
177         save->heir = env->spr[SPR_HEIR];
178         save->asdr = env->spr[SPR_ASDR];
179         save->dawr1 = env->spr[SPR_DAWR1];
180         save->dawrx1 = env->spr[SPR_DAWRX1];
181         save->dexcr = env->spr[SPR_DEXCR];
182         save->hdexcr = env->spr[SPR_HDEXCR];
183         save->hashkeyr = env->spr[SPR_HASHKEYR];
184         save->hashpkeyr = env->spr[SPR_HASHPKEYR];
185         memcpy(save->vsr, env->vsr, sizeof(save->vsr));
186         save->ebbhr = env->spr[SPR_EBBHR];
187         save->tar = env->spr[SPR_TAR];
188         save->ebbrr = env->spr[SPR_EBBRR];
189         save->bescr = env->spr[SPR_BESCR];
190         save->iamr = env->spr[SPR_IAMR];
191         save->amr = env->spr[SPR_AMR];
192         save->uamor = env->spr[SPR_UAMOR];
193         save->dscr = env->spr[SPR_DSCR];
194         save->fscr = env->spr[SPR_FSCR];
195         save->pspb = env->spr[SPR_PSPB];
196         save->ctrl = env->spr[SPR_CTRL];
197         save->vrsave = env->spr[SPR_VRSAVE];
198         save->dar = env->spr[SPR_DAR];
199         save->dsisr = env->spr[SPR_DSISR];
200         save->pmc1 = env->spr[SPR_POWER_PMC1];
201         save->pmc2 = env->spr[SPR_POWER_PMC2];
202         save->pmc3 = env->spr[SPR_POWER_PMC3];
203         save->pmc4 = env->spr[SPR_POWER_PMC4];
204         save->pmc5 = env->spr[SPR_POWER_PMC5];
205         save->pmc6 = env->spr[SPR_POWER_PMC6];
206         save->mmcr0 = env->spr[SPR_POWER_MMCR0];
207         save->mmcr1 = env->spr[SPR_POWER_MMCR1];
208         save->mmcr2 = env->spr[SPR_POWER_MMCR2];
209         save->mmcra = env->spr[SPR_POWER_MMCRA];
210         save->sdar = env->spr[SPR_POWER_SDAR];
211         save->siar = env->spr[SPR_POWER_SIAR];
212         save->sier = env->spr[SPR_POWER_SIER];
213         save->vscr = ppc_get_vscr(env);
214         save->fpscr = env->fpscr;
215     } else if (spapr_nested_api(spapr) == NESTED_API_KVM_HV) {
216         save->tb_offset = env->tb_env->tb_offset;
217     }
218 }
219 
220 static void nested_post_load_state(CPUPPCState *env, CPUState *cs)
221 {
222     /*
223      * compute hflags and possible interrupts.
224      */
225     hreg_compute_hflags(env);
226     ppc_maybe_interrupt(env);
227     /*
228      * Nested HV does not tag TLB entries between L1 and L2, so must
229      * flush on transition.
230      */
231     tlb_flush(cs);
232     env->reserve_addr = -1; /* Reset the reservation */
233 }
234 
235 static void nested_load_state(PowerPCCPU *cpu, struct nested_ppc_state *load)
236 {
237     CPUPPCState *env = &cpu->env;
238     SpaprMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
239 
240     memcpy(env->gpr, load->gpr, sizeof(env->gpr));
241 
242     env->lr = load->lr;
243     env->ctr = load->ctr;
244     env->cfar = load->cfar;
245     env->msr = load->msr;
246     env->nip = load->nip;
247 
248     ppc_set_cr(env, load->cr);
249     cpu_write_xer(env, load->xer);
250 
251     env->spr[SPR_LPCR] = load->lpcr;
252     env->spr[SPR_LPIDR] = load->lpidr;
253     env->spr[SPR_PCR] = load->pcr;
254     env->spr[SPR_DPDES] = load->dpdes;
255     env->spr[SPR_HFSCR] = load->hfscr;
256     env->spr[SPR_SRR0] = load->srr0;
257     env->spr[SPR_SRR1] = load->srr1;
258     env->spr[SPR_SPRG0] = load->sprg0;
259     env->spr[SPR_SPRG1] = load->sprg1;
260     env->spr[SPR_SPRG2] = load->sprg2;
261     env->spr[SPR_SPRG3] = load->sprg3;
262     env->spr[SPR_BOOKS_PID] = load->pidr;
263     env->spr[SPR_PPR] = load->ppr;
264 
265     if (spapr_nested_api(spapr) == NESTED_API_PAPR) {
266         env->spr[SPR_AMOR] = load->amor;
267         env->spr[SPR_DAWR0] = load->dawr0;
268         env->spr[SPR_DAWRX0] = load->dawrx0;
269         env->spr[SPR_CIABR] = load->ciabr;
270         env->spr[SPR_PURR] = load->purr;
271         env->spr[SPR_SPURR] = load->purr;
272         env->spr[SPR_IC] = load->ic;
273         env->spr[SPR_VTB] = load->vtb;
274         env->spr[SPR_HDAR] = load->hdar;
275         env->spr[SPR_HDSISR] = load->hdsisr;
276         env->spr[SPR_HEIR] = load->heir;
277         env->spr[SPR_ASDR] = load->asdr;
278         env->spr[SPR_DAWR1] = load->dawr1;
279         env->spr[SPR_DAWRX1] = load->dawrx1;
280         env->spr[SPR_DEXCR] = load->dexcr;
281         env->spr[SPR_HDEXCR] = load->hdexcr;
282         env->spr[SPR_HASHKEYR] = load->hashkeyr;
283         env->spr[SPR_HASHPKEYR] = load->hashpkeyr;
284         memcpy(env->vsr, load->vsr, sizeof(env->vsr));
285         env->spr[SPR_EBBHR] = load->ebbhr;
286         env->spr[SPR_TAR] = load->tar;
287         env->spr[SPR_EBBRR] = load->ebbrr;
288         env->spr[SPR_BESCR] = load->bescr;
289         env->spr[SPR_IAMR] = load->iamr;
290         env->spr[SPR_AMR] = load->amr;
291         env->spr[SPR_UAMOR] = load->uamor;
292         env->spr[SPR_DSCR] = load->dscr;
293         env->spr[SPR_FSCR] = load->fscr;
294         env->spr[SPR_PSPB] = load->pspb;
295         env->spr[SPR_CTRL] = load->ctrl;
296         env->spr[SPR_VRSAVE] = load->vrsave;
297         env->spr[SPR_DAR] = load->dar;
298         env->spr[SPR_DSISR] = load->dsisr;
299         env->spr[SPR_POWER_PMC1] = load->pmc1;
300         env->spr[SPR_POWER_PMC2] = load->pmc2;
301         env->spr[SPR_POWER_PMC3] = load->pmc3;
302         env->spr[SPR_POWER_PMC4] = load->pmc4;
303         env->spr[SPR_POWER_PMC5] = load->pmc5;
304         env->spr[SPR_POWER_PMC6] = load->pmc6;
305         env->spr[SPR_POWER_MMCR0] = load->mmcr0;
306         env->spr[SPR_POWER_MMCR1] = load->mmcr1;
307         env->spr[SPR_POWER_MMCR2] = load->mmcr2;
308         env->spr[SPR_POWER_MMCRA] = load->mmcra;
309         env->spr[SPR_POWER_SDAR] = load->sdar;
310         env->spr[SPR_POWER_SIAR] = load->siar;
311         env->spr[SPR_POWER_SIER] = load->sier;
312         ppc_store_vscr(env, load->vscr);
313         ppc_store_fpscr(env, load->fpscr);
314     } else if (spapr_nested_api(spapr) == NESTED_API_KVM_HV) {
315         env->tb_env->tb_offset = load->tb_offset;
316     }
317 }
318 
319 /*
320  * When this handler returns, the environment is switched to the L2 guest
321  * and TCG begins running that. spapr_exit_nested() performs the switch from
322  * L2 back to L1 and returns from the H_ENTER_NESTED hcall.
323  */
324 static target_ulong h_enter_nested(PowerPCCPU *cpu,
325                                    SpaprMachineState *spapr,
326                                    target_ulong opcode,
327                                    target_ulong *args)
328 {
329     PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
330     CPUPPCState *env = &cpu->env;
331     CPUState *cs = CPU(cpu);
332     SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu);
333     struct nested_ppc_state l2_state;
334     target_ulong hv_ptr = args[0];
335     target_ulong regs_ptr = args[1];
336     target_ulong hdec, now = cpu_ppc_load_tbl(env);
337     target_ulong lpcr, lpcr_mask;
338     struct kvmppc_hv_guest_state *hvstate;
339     struct kvmppc_hv_guest_state hv_state;
340     struct kvmppc_pt_regs *regs;
341     hwaddr len;
342 
343     if (spapr->nested.ptcr == 0) {
344         return H_NOT_AVAILABLE;
345     }
346 
347     len = sizeof(*hvstate);
348     hvstate = address_space_map(CPU(cpu)->as, hv_ptr, &len, false,
349                                 MEMTXATTRS_UNSPECIFIED);
350     if (len != sizeof(*hvstate)) {
351         address_space_unmap(CPU(cpu)->as, hvstate, len, 0, false);
352         return H_PARAMETER;
353     }
354 
355     memcpy(&hv_state, hvstate, len);
356 
357     address_space_unmap(CPU(cpu)->as, hvstate, len, len, false);
358 
359     /*
360      * We accept versions 1 and 2. Version 2 fields are unused because TCG
361      * does not implement DAWR*.
362      */
363     if (hv_state.version > HV_GUEST_STATE_VERSION) {
364         return H_PARAMETER;
365     }
366 
367     if (hv_state.lpid == 0) {
368         return H_PARAMETER;
369     }
370 
371     spapr_cpu->nested_host_state = g_try_new(struct nested_ppc_state, 1);
372     if (!spapr_cpu->nested_host_state) {
373         return H_NO_MEM;
374     }
375 
376     assert(env->spr[SPR_LPIDR] == 0);
377     assert(env->spr[SPR_DPDES] == 0);
378     nested_save_state(spapr_cpu->nested_host_state, cpu);
379 
380     len = sizeof(*regs);
381     regs = address_space_map(CPU(cpu)->as, regs_ptr, &len, false,
382                                 MEMTXATTRS_UNSPECIFIED);
383     if (!regs || len != sizeof(*regs)) {
384         address_space_unmap(CPU(cpu)->as, regs, len, 0, false);
385         g_free(spapr_cpu->nested_host_state);
386         return H_P2;
387     }
388 
389     len = sizeof(l2_state.gpr);
390     assert(len == sizeof(regs->gpr));
391     memcpy(l2_state.gpr, regs->gpr, len);
392 
393     l2_state.lr = regs->link;
394     l2_state.ctr = regs->ctr;
395     l2_state.xer = regs->xer;
396     l2_state.cr = regs->ccr;
397     l2_state.msr = regs->msr;
398     l2_state.nip = regs->nip;
399 
400     address_space_unmap(CPU(cpu)->as, regs, len, len, false);
401 
402     l2_state.cfar = hv_state.cfar;
403     l2_state.lpidr = hv_state.lpid;
404 
405     lpcr_mask = LPCR_DPFD | LPCR_ILE | LPCR_AIL | LPCR_LD | LPCR_MER;
406     lpcr = (env->spr[SPR_LPCR] & ~lpcr_mask) | (hv_state.lpcr & lpcr_mask);
407     lpcr |= LPCR_HR | LPCR_UPRT | LPCR_GTSE | LPCR_HVICE | LPCR_HDICE;
408     lpcr &= ~LPCR_LPES0;
409     l2_state.lpcr = lpcr & pcc->lpcr_mask;
410 
411     l2_state.pcr = hv_state.pcr;
412     /* hv_state.amor is not used */
413     l2_state.dpdes = hv_state.dpdes;
414     l2_state.hfscr = hv_state.hfscr;
415     /* TCG does not implement DAWR*, CIABR, PURR, SPURR, IC, VTB, HEIR SPRs*/
416     l2_state.srr0 = hv_state.srr0;
417     l2_state.srr1 = hv_state.srr1;
418     l2_state.sprg0 = hv_state.sprg[0];
419     l2_state.sprg1 = hv_state.sprg[1];
420     l2_state.sprg2 = hv_state.sprg[2];
421     l2_state.sprg3 = hv_state.sprg[3];
422     l2_state.pidr = hv_state.pidr;
423     l2_state.ppr = hv_state.ppr;
424     l2_state.tb_offset = env->tb_env->tb_offset + hv_state.tb_offset;
425 
426     /*
427      * Switch to the nested guest environment and start the "hdec" timer.
428      */
429     nested_load_state(cpu, &l2_state);
430     nested_post_load_state(env, cs);
431 
432     hdec = hv_state.hdec_expiry - now;
433     cpu_ppc_hdecr_init(env);
434     cpu_ppc_store_hdecr(env, hdec);
435 
436     /*
437      * The hv_state.vcpu_token is not needed. It is used by the KVM
438      * implementation to remember which L2 vCPU last ran on which physical
439      * CPU so as to invalidate process scope translations if it is moved
440      * between physical CPUs. For now TLBs are always flushed on L1<->L2
441      * transitions so this is not a problem.
442      *
443      * Could validate that the same vcpu_token does not attempt to run on
444      * different L1 vCPUs at the same time, but that would be a L1 KVM bug
445      * and it's not obviously worth a new data structure to do it.
446      */
447 
448     spapr_cpu->in_nested = true;
449 
450     /*
451      * The spapr hcall helper sets env->gpr[3] to the return value, but at
452      * this point the L1 is not returning from the hcall but rather we
453      * start running the L2, so r3 must not be clobbered, so return env->gpr[3]
454      * to leave it unchanged.
455      */
456     return env->gpr[3];
457 }
458 
459 static void spapr_exit_nested_hv(PowerPCCPU *cpu, int excp)
460 {
461     CPUPPCState *env = &cpu->env;
462     CPUState *cs = CPU(cpu);
463     SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu);
464     struct nested_ppc_state l2_state;
465     target_ulong hv_ptr = spapr_cpu->nested_host_state->gpr[4];
466     target_ulong regs_ptr = spapr_cpu->nested_host_state->gpr[5];
467     target_ulong hsrr0, hsrr1, hdar, asdr, hdsisr;
468     struct kvmppc_hv_guest_state *hvstate;
469     struct kvmppc_pt_regs *regs;
470     hwaddr len;
471 
472     nested_save_state(&l2_state, cpu);
473     hsrr0 = env->spr[SPR_HSRR0];
474     hsrr1 = env->spr[SPR_HSRR1];
475     hdar = env->spr[SPR_HDAR];
476     hdsisr = env->spr[SPR_HDSISR];
477     asdr = env->spr[SPR_ASDR];
478 
479     /*
480      * Switch back to the host environment (including for any error).
481      */
482     assert(env->spr[SPR_LPIDR] != 0);
483     nested_load_state(cpu, spapr_cpu->nested_host_state);
484     nested_post_load_state(env, cs);
485     env->gpr[3] = env->excp_vectors[excp]; /* hcall return value */
486 
487     cpu_ppc_hdecr_exit(env);
488 
489     spapr_cpu->in_nested = false;
490 
491     g_free(spapr_cpu->nested_host_state);
492     spapr_cpu->nested_host_state = NULL;
493 
494     len = sizeof(*hvstate);
495     hvstate = address_space_map(CPU(cpu)->as, hv_ptr, &len, true,
496                                 MEMTXATTRS_UNSPECIFIED);
497     if (len != sizeof(*hvstate)) {
498         address_space_unmap(CPU(cpu)->as, hvstate, len, 0, true);
499         env->gpr[3] = H_PARAMETER;
500         return;
501     }
502 
503     hvstate->cfar = l2_state.cfar;
504     hvstate->lpcr = l2_state.lpcr;
505     hvstate->pcr = l2_state.pcr;
506     hvstate->dpdes = l2_state.dpdes;
507     hvstate->hfscr = l2_state.hfscr;
508 
509     if (excp == POWERPC_EXCP_HDSI) {
510         hvstate->hdar = hdar;
511         hvstate->hdsisr = hdsisr;
512         hvstate->asdr = asdr;
513     } else if (excp == POWERPC_EXCP_HISI) {
514         hvstate->asdr = asdr;
515     }
516 
517     /* HEIR should be implemented for HV mode and saved here. */
518     hvstate->srr0 = l2_state.srr0;
519     hvstate->srr1 = l2_state.srr1;
520     hvstate->sprg[0] = l2_state.sprg0;
521     hvstate->sprg[1] = l2_state.sprg1;
522     hvstate->sprg[2] = l2_state.sprg2;
523     hvstate->sprg[3] = l2_state.sprg3;
524     hvstate->pidr = l2_state.pidr;
525     hvstate->ppr = l2_state.ppr;
526 
527     /* Is it okay to specify write length larger than actual data written? */
528     address_space_unmap(CPU(cpu)->as, hvstate, len, len, true);
529 
530     len = sizeof(*regs);
531     regs = address_space_map(CPU(cpu)->as, regs_ptr, &len, true,
532                                 MEMTXATTRS_UNSPECIFIED);
533     if (!regs || len != sizeof(*regs)) {
534         address_space_unmap(CPU(cpu)->as, regs, len, 0, true);
535         env->gpr[3] = H_P2;
536         return;
537     }
538 
539     len = sizeof(env->gpr);
540     assert(len == sizeof(regs->gpr));
541     memcpy(regs->gpr, l2_state.gpr, len);
542 
543     regs->link = l2_state.lr;
544     regs->ctr = l2_state.ctr;
545     regs->xer = l2_state.xer;
546     regs->ccr = l2_state.cr;
547 
548     if (excp == POWERPC_EXCP_MCHECK ||
549         excp == POWERPC_EXCP_RESET ||
550         excp == POWERPC_EXCP_SYSCALL) {
551         regs->nip = l2_state.srr0;
552         regs->msr = l2_state.srr1 & env->msr_mask;
553     } else {
554         regs->nip = hsrr0;
555         regs->msr = hsrr1 & env->msr_mask;
556     }
557 
558     /* Is it okay to specify write length larger than actual data written? */
559     address_space_unmap(CPU(cpu)->as, regs, len, len, true);
560 }
561 
562 static bool spapr_nested_vcpu_check(SpaprMachineStateNestedGuest *guest,
563                                     target_ulong vcpuid, bool inoutbuf)
564 {
565     struct SpaprMachineStateNestedGuestVcpu *vcpu;
566     /*
567      * Perform sanity checks for the provided vcpuid of a guest.
568      * For now, ensure its valid, allocated and enabled for use.
569      */
570 
571     if (vcpuid >= PAPR_NESTED_GUEST_VCPU_MAX) {
572         return false;
573     }
574 
575     if (!(vcpuid < guest->nr_vcpus)) {
576         return false;
577     }
578 
579     vcpu = &guest->vcpus[vcpuid];
580     if (!vcpu->enabled) {
581         return false;
582     }
583 
584     if (!inoutbuf) {
585         return true;
586     }
587 
588     /* Check to see if the in/out buffers are registered */
589     if (vcpu->runbufin.addr && vcpu->runbufout.addr) {
590         return true;
591     }
592 
593     return false;
594 }
595 
596 static void *get_vcpu_state_ptr(SpaprMachineStateNestedGuest *guest,
597                               target_ulong vcpuid)
598 {
599     assert(spapr_nested_vcpu_check(guest, vcpuid, false));
600     return &guest->vcpus[vcpuid].state;
601 }
602 
603 static void *get_vcpu_ptr(SpaprMachineStateNestedGuest *guest,
604                                    target_ulong vcpuid)
605 {
606     assert(spapr_nested_vcpu_check(guest, vcpuid, false));
607     return &guest->vcpus[vcpuid];
608 }
609 
610 static void *get_guest_ptr(SpaprMachineStateNestedGuest *guest,
611                            target_ulong vcpuid)
612 {
613     return guest; /* for GSBE_NESTED */
614 }
615 
616 /*
617  * set=1 means the L1 is trying to set some state
618  * set=0 means the L1 is trying to get some state
619  */
620 static void copy_state_8to8(void *a, void *b, bool set)
621 {
622     /* set takes from the Big endian element_buf and sets internal buffer */
623 
624     if (set) {
625         *(uint64_t *)a = be64_to_cpu(*(uint64_t *)b);
626     } else {
627         *(uint64_t *)b = cpu_to_be64(*(uint64_t *)a);
628     }
629 }
630 
631 static void copy_state_4to4(void *a, void *b, bool set)
632 {
633     if (set) {
634         *(uint32_t *)a = be32_to_cpu(*(uint32_t *)b);
635     } else {
636         *(uint32_t *)b = cpu_to_be32(*((uint32_t *)a));
637     }
638 }
639 
640 static void copy_state_16to16(void *a, void *b, bool set)
641 {
642     uint64_t *src, *dst;
643 
644     if (set) {
645         src = b;
646         dst = a;
647 
648         dst[1] = be64_to_cpu(src[0]);
649         dst[0] = be64_to_cpu(src[1]);
650     } else {
651         src = a;
652         dst = b;
653 
654         dst[1] = cpu_to_be64(src[0]);
655         dst[0] = cpu_to_be64(src[1]);
656     }
657 }
658 
659 static void copy_state_4to8(void *a, void *b, bool set)
660 {
661     if (set) {
662         *(uint64_t *)a  = (uint64_t) be32_to_cpu(*(uint32_t *)b);
663     } else {
664         *(uint32_t *)b = cpu_to_be32((uint32_t) (*((uint64_t *)a)));
665     }
666 }
667 
668 static void copy_state_pagetbl(void *a, void *b, bool set)
669 {
670     uint64_t *pagetbl;
671     uint64_t *buf; /* 3 double words */
672     uint64_t rts;
673 
674     assert(set);
675 
676     pagetbl = a;
677     buf = b;
678 
679     *pagetbl = be64_to_cpu(buf[0]);
680     /* as per ISA section 6.7.6.1 */
681     *pagetbl |= PATE0_HR; /* Host Radix bit is 1 */
682 
683     /* RTS */
684     rts = be64_to_cpu(buf[1]);
685     assert(rts == 52);
686     rts = rts - 31; /* since radix tree size = 2^(RTS+31) */
687     *pagetbl |=  ((rts & 0x7) << 5); /* RTS2 is bit 56:58 */
688     *pagetbl |=  (((rts >> 3) & 0x3) << 61); /* RTS1 is bit 1:2 */
689 
690     /* RPDS {Size = 2^(RPDS+3) , RPDS >=5} */
691     *pagetbl |= 63 - clz64(be64_to_cpu(buf[2])) - 3;
692 }
693 
694 static void copy_state_proctbl(void *a, void *b, bool set)
695 {
696     uint64_t *proctbl;
697     uint64_t *buf; /* 2 double words */
698 
699     assert(set);
700 
701     proctbl = a;
702     buf = b;
703     /* PRTB: Process Table Base */
704     *proctbl = be64_to_cpu(buf[0]);
705     /* PRTS: Process Table Size = 2^(12+PRTS) */
706     if (be64_to_cpu(buf[1]) == (1ULL << 12)) {
707             *proctbl |= 0;
708     } else if (be64_to_cpu(buf[1]) == (1ULL << 24)) {
709             *proctbl |= 12;
710     } else {
711         g_assert_not_reached();
712     }
713 }
714 
715 static void copy_state_runbuf(void *a, void *b, bool set)
716 {
717     uint64_t *buf; /* 2 double words */
718     struct SpaprMachineStateNestedGuestVcpuRunBuf *runbuf;
719 
720     assert(set);
721 
722     runbuf = a;
723     buf = b;
724 
725     runbuf->addr = be64_to_cpu(buf[0]);
726     assert(runbuf->addr);
727 
728     /* per spec */
729     assert(be64_to_cpu(buf[1]) <= 16384);
730 
731     /*
732      * This will also hit in the input buffer but should be fine for
733      * now. If not we can split this function.
734      */
735     assert(be64_to_cpu(buf[1]) >= VCPU_OUT_BUF_MIN_SZ);
736 
737     runbuf->size = be64_to_cpu(buf[1]);
738 }
739 
740 /* tell the L1 how big we want the output vcpu run buffer */
741 static void out_buf_min_size(void *a, void *b, bool set)
742 {
743     uint64_t *buf; /* 1 double word */
744 
745     assert(!set);
746 
747     buf = b;
748 
749     buf[0] = cpu_to_be64(VCPU_OUT_BUF_MIN_SZ);
750 }
751 
752 static void copy_logical_pvr(void *a, void *b, bool set)
753 {
754     SpaprMachineStateNestedGuest *guest;
755     uint32_t *buf; /* 1 word */
756     uint32_t *pvr_logical_ptr;
757     uint32_t pvr_logical;
758     target_ulong pcr = 0;
759 
760     pvr_logical_ptr = a;
761     buf = b;
762 
763     if (!set) {
764         buf[0] = cpu_to_be32(*pvr_logical_ptr);
765         return;
766     }
767 
768     pvr_logical = be32_to_cpu(buf[0]);
769 
770     *pvr_logical_ptr = pvr_logical;
771 
772     if (*pvr_logical_ptr) {
773         switch (*pvr_logical_ptr) {
774         case CPU_POWERPC_LOGICAL_3_10:
775             pcr = PCR_COMPAT_3_10 | PCR_COMPAT_3_00;
776             break;
777         case CPU_POWERPC_LOGICAL_3_00:
778             pcr = PCR_COMPAT_3_00;
779             break;
780         default:
781             qemu_log_mask(LOG_GUEST_ERROR,
782                           "Could not set PCR for LPVR=0x%08x\n",
783                           *pvr_logical_ptr);
784             return;
785         }
786     }
787 
788     guest = container_of(pvr_logical_ptr,
789                          struct SpaprMachineStateNestedGuest,
790                          pvr_logical);
791     for (int i = 0; i < guest->nr_vcpus; i++) {
792         guest->vcpus[i].state.pcr = ~pcr | HVMASK_PCR;
793     }
794 }
795 
796 static void copy_tb_offset(void *a, void *b, bool set)
797 {
798     SpaprMachineStateNestedGuest *guest;
799     uint64_t *buf; /* 1 double word */
800     uint64_t *tb_offset_ptr;
801     uint64_t tb_offset;
802 
803     tb_offset_ptr = a;
804     buf = b;
805 
806     if (!set) {
807         buf[0] = cpu_to_be64(*tb_offset_ptr);
808         return;
809     }
810 
811     tb_offset = be64_to_cpu(buf[0]);
812     /* need to copy this to the individual tb_offset for each vcpu */
813     guest = container_of(tb_offset_ptr,
814                          struct SpaprMachineStateNestedGuest,
815                          tb_offset);
816     for (int i = 0; i < guest->nr_vcpus; i++) {
817         guest->vcpus[i].tb_offset = tb_offset;
818     }
819 }
820 
821 static void copy_state_hdecr(void *a, void *b, bool set)
822 {
823     uint64_t *buf; /* 1 double word */
824     uint64_t *hdecr_expiry_tb;
825 
826     hdecr_expiry_tb = a;
827     buf = b;
828 
829     if (!set) {
830         buf[0] = cpu_to_be64(*hdecr_expiry_tb);
831         return;
832     }
833 
834     *hdecr_expiry_tb = be64_to_cpu(buf[0]);
835 }
836 
837 struct guest_state_element_type guest_state_element_types[] = {
838     GUEST_STATE_ELEMENT_NOP(GSB_HV_VCPU_IGNORED_ID, 0),
839     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_GPR0,  gpr[0]),
840     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_GPR1,  gpr[1]),
841     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_GPR2,  gpr[2]),
842     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_GPR3,  gpr[3]),
843     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_GPR4,  gpr[4]),
844     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_GPR5,  gpr[5]),
845     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_GPR6,  gpr[6]),
846     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_GPR7,  gpr[7]),
847     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_GPR8,  gpr[8]),
848     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_GPR9,  gpr[9]),
849     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_GPR10, gpr[10]),
850     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_GPR11, gpr[11]),
851     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_GPR12, gpr[12]),
852     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_GPR13, gpr[13]),
853     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_GPR14, gpr[14]),
854     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_GPR15, gpr[15]),
855     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_GPR16, gpr[16]),
856     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_GPR17, gpr[17]),
857     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_GPR18, gpr[18]),
858     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_GPR19, gpr[19]),
859     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_GPR20, gpr[20]),
860     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_GPR21, gpr[21]),
861     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_GPR22, gpr[22]),
862     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_GPR23, gpr[23]),
863     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_GPR24, gpr[24]),
864     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_GPR25, gpr[25]),
865     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_GPR26, gpr[26]),
866     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_GPR27, gpr[27]),
867     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_GPR28, gpr[28]),
868     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_GPR29, gpr[29]),
869     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_GPR30, gpr[30]),
870     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_GPR31, gpr[31]),
871     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_SPR_NIA, nip),
872     GSE_ENV_DWM(GSB_VCPU_SPR_MSR, msr, HVMASK_MSR),
873     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_SPR_CTR, ctr),
874     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_SPR_LR, lr),
875     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_SPR_XER, xer),
876     GUEST_STATE_ELEMENT_ENV_WW(GSB_VCPU_SPR_CR, cr),
877     GUEST_STATE_ELEMENT_NOP_DW(GSB_VCPU_SPR_MMCR3),
878     GUEST_STATE_ELEMENT_NOP_DW(GSB_VCPU_SPR_SIER2),
879     GUEST_STATE_ELEMENT_NOP_DW(GSB_VCPU_SPR_SIER3),
880     GUEST_STATE_ELEMENT_NOP_W(GSB_VCPU_SPR_WORT),
881     GSE_ENV_DWM(GSB_VCPU_SPR_LPCR, lpcr, HVMASK_LPCR),
882     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_SPR_AMOR, amor),
883     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_SPR_HFSCR, hfscr),
884     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_SPR_DAWR0, dawr0),
885     GUEST_STATE_ELEMENT_ENV_W(GSB_VCPU_SPR_DAWRX0, dawrx0),
886     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_SPR_CIABR, ciabr),
887     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_SPR_PURR,  purr),
888     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_SPR_SPURR, spurr),
889     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_SPR_IC,    ic),
890     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_SPR_VTB,   vtb),
891     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_SPR_HDAR,  hdar),
892     GUEST_STATE_ELEMENT_ENV_W(GSB_VCPU_SPR_HDSISR, hdsisr),
893     GUEST_STATE_ELEMENT_ENV_W(GSB_VCPU_SPR_HEIR,   heir),
894     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_SPR_ASDR,  asdr),
895     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_SPR_SRR0,  srr0),
896     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_SPR_SRR1,  srr1),
897     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_SPR_SPRG0, sprg0),
898     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_SPR_SPRG1, sprg1),
899     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_SPR_SPRG2, sprg2),
900     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_SPR_SPRG3, sprg3),
901     GUEST_STATE_ELEMENT_ENV_W(GSB_VCPU_SPR_PIDR,   pidr),
902     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_SPR_CFAR,  cfar),
903     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_SPR_PPR,   ppr),
904     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_SPR_DAWR1, dawr1),
905     GUEST_STATE_ELEMENT_ENV_W(GSB_VCPU_SPR_DAWRX1, dawrx1),
906     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_SPR_DEXCR, dexcr),
907     GSE_ENV_DWM(GSB_VCPU_SPR_HDEXCR, hdexcr, HVMASK_HDEXCR),
908     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_SPR_HASHKEYR, hashkeyr),
909     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_SPR_HASHPKEYR, hashpkeyr),
910     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR0, vsr[0]),
911     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR1, vsr[1]),
912     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR2, vsr[2]),
913     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR3, vsr[3]),
914     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR4, vsr[4]),
915     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR5, vsr[5]),
916     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR6, vsr[6]),
917     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR7, vsr[7]),
918     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR8, vsr[8]),
919     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR9, vsr[9]),
920     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR10, vsr[10]),
921     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR11, vsr[11]),
922     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR12, vsr[12]),
923     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR13, vsr[13]),
924     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR14, vsr[14]),
925     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR15, vsr[15]),
926     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR16, vsr[16]),
927     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR17, vsr[17]),
928     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR18, vsr[18]),
929     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR19, vsr[19]),
930     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR20, vsr[20]),
931     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR21, vsr[21]),
932     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR22, vsr[22]),
933     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR23, vsr[23]),
934     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR24, vsr[24]),
935     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR25, vsr[25]),
936     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR26, vsr[26]),
937     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR27, vsr[27]),
938     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR28, vsr[28]),
939     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR29, vsr[29]),
940     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR30, vsr[30]),
941     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR31, vsr[31]),
942     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR32, vsr[32]),
943     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR33, vsr[33]),
944     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR34, vsr[34]),
945     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR35, vsr[35]),
946     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR36, vsr[36]),
947     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR37, vsr[37]),
948     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR38, vsr[38]),
949     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR39, vsr[39]),
950     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR40, vsr[40]),
951     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR41, vsr[41]),
952     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR42, vsr[42]),
953     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR43, vsr[43]),
954     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR44, vsr[44]),
955     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR45, vsr[45]),
956     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR46, vsr[46]),
957     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR47, vsr[47]),
958     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR48, vsr[48]),
959     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR49, vsr[49]),
960     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR50, vsr[50]),
961     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR51, vsr[51]),
962     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR52, vsr[52]),
963     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR53, vsr[53]),
964     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR54, vsr[54]),
965     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR55, vsr[55]),
966     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR56, vsr[56]),
967     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR57, vsr[57]),
968     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR58, vsr[58]),
969     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR59, vsr[59]),
970     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR60, vsr[60]),
971     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR61, vsr[61]),
972     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR62, vsr[62]),
973     GUEST_STATE_ELEMENT_ENV_QW(GSB_VCPU_SPR_VSR63, vsr[63]),
974     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_SPR_EBBHR, ebbhr),
975     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_SPR_TAR,   tar),
976     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_SPR_EBBRR, ebbrr),
977     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_SPR_BESCR, bescr),
978     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_SPR_IAMR,  iamr),
979     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_SPR_AMR,   amr),
980     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_SPR_UAMOR, uamor),
981     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_SPR_DSCR,  dscr),
982     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_SPR_FSCR,  fscr),
983     GUEST_STATE_ELEMENT_ENV_W(GSB_VCPU_SPR_PSPB,   pspb),
984     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_SPR_CTRL,  ctrl),
985     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_SPR_DPDES, dpdes),
986     GUEST_STATE_ELEMENT_ENV_W(GSB_VCPU_SPR_VRSAVE, vrsave),
987     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_SPR_DAR,   dar),
988     GUEST_STATE_ELEMENT_ENV_W(GSB_VCPU_SPR_DSISR,  dsisr),
989     GUEST_STATE_ELEMENT_ENV_W(GSB_VCPU_SPR_PMC1,   pmc1),
990     GUEST_STATE_ELEMENT_ENV_W(GSB_VCPU_SPR_PMC2,   pmc2),
991     GUEST_STATE_ELEMENT_ENV_W(GSB_VCPU_SPR_PMC3,   pmc3),
992     GUEST_STATE_ELEMENT_ENV_W(GSB_VCPU_SPR_PMC4,   pmc4),
993     GUEST_STATE_ELEMENT_ENV_W(GSB_VCPU_SPR_PMC5,   pmc5),
994     GUEST_STATE_ELEMENT_ENV_W(GSB_VCPU_SPR_PMC6,   pmc6),
995     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_SPR_MMCR0, mmcr0),
996     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_SPR_MMCR1, mmcr1),
997     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_SPR_MMCR2, mmcr2),
998     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_SPR_MMCRA, mmcra),
999     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_SPR_SDAR , sdar),
1000     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_SPR_SIAR , siar),
1001     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_SPR_SIER , sier),
1002     GUEST_STATE_ELEMENT_ENV_WW(GSB_VCPU_SPR_VSCR,  vscr),
1003     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_SPR_FPSCR, fpscr),
1004     GUEST_STATE_ELEMENT_ENV_DW(GSB_VCPU_DEC_EXPIRE_TB, dec_expiry_tb),
1005     GSBE_NESTED(GSB_PART_SCOPED_PAGETBL, 0x18, parttbl[0],  copy_state_pagetbl),
1006     GSBE_NESTED(GSB_PROCESS_TBL,         0x10, parttbl[1],  copy_state_proctbl),
1007     GSBE_NESTED(GSB_VCPU_LPVR,           0x4,  pvr_logical, copy_logical_pvr),
1008     GSBE_NESTED_MSK(GSB_TB_OFFSET, 0x8, tb_offset, copy_tb_offset,
1009                     HVMASK_TB_OFFSET),
1010     GSBE_NESTED_VCPU(GSB_VCPU_IN_BUFFER, 0x10, runbufin,    copy_state_runbuf),
1011     GSBE_NESTED_VCPU(GSB_VCPU_OUT_BUFFER, 0x10, runbufout,   copy_state_runbuf),
1012     GSBE_NESTED_VCPU(GSB_VCPU_OUT_BUF_MIN_SZ, 0x8, runbufout, out_buf_min_size),
1013     GSBE_NESTED_VCPU(GSB_VCPU_HDEC_EXPIRY_TB, 0x8, hdecr_expiry_tb,
1014                      copy_state_hdecr)
1015 };
1016 
1017 void spapr_nested_gsb_init(void)
1018 {
1019     struct guest_state_element_type *type;
1020 
1021     /* Init the guest state elements lookup table, flags for now */
1022     for (int i = 0; i < ARRAY_SIZE(guest_state_element_types); i++) {
1023         type = &guest_state_element_types[i];
1024 
1025         assert(type->id <= GSB_LAST);
1026         if (type->id >= GSB_VCPU_SPR_HDAR)
1027             /* 0xf000 - 0xf005 Thread + RO */
1028             type->flags = GUEST_STATE_ELEMENT_TYPE_FLAG_READ_ONLY;
1029         else if (type->id >= GSB_VCPU_IN_BUFFER)
1030             /* 0x0c00 - 0xf000 Thread + RW */
1031             type->flags = 0;
1032         else if (type->id >= GSB_VCPU_LPVR)
1033             /* 0x0003 - 0x0bff Guest + RW */
1034             type->flags = GUEST_STATE_ELEMENT_TYPE_FLAG_GUEST_WIDE;
1035         else if (type->id >= GSB_HV_VCPU_STATE_SIZE)
1036             /* 0x0001 - 0x0002 Guest + RO */
1037             type->flags = GUEST_STATE_ELEMENT_TYPE_FLAG_READ_ONLY |
1038                           GUEST_STATE_ELEMENT_TYPE_FLAG_GUEST_WIDE;
1039     }
1040 }
1041 
1042 static struct guest_state_element *guest_state_element_next(
1043     struct guest_state_element *element,
1044     int64_t *len,
1045     int64_t *num_elements)
1046 {
1047     uint16_t size;
1048 
1049     /* size is of element->value[] only. Not whole guest_state_element */
1050     size = be16_to_cpu(element->size);
1051 
1052     if (len) {
1053         *len -= size + offsetof(struct guest_state_element, value);
1054     }
1055 
1056     if (num_elements) {
1057         *num_elements -= 1;
1058     }
1059 
1060     return (struct guest_state_element *)(element->value + size);
1061 }
1062 
1063 static
1064 struct guest_state_element_type *guest_state_element_type_find(uint16_t id)
1065 {
1066     int i;
1067 
1068     for (i = 0; i < ARRAY_SIZE(guest_state_element_types); i++)
1069         if (id == guest_state_element_types[i].id) {
1070             return &guest_state_element_types[i];
1071         }
1072 
1073     return NULL;
1074 }
1075 
1076 static void log_element(struct guest_state_element *element,
1077                         struct guest_state_request *gsr)
1078 {
1079     qemu_log_mask(LOG_GUEST_ERROR, "h_guest_%s_state id:0x%04x size:0x%04x",
1080                   gsr->flags & GUEST_STATE_REQUEST_SET ? "set" : "get",
1081                   be16_to_cpu(element->id), be16_to_cpu(element->size));
1082     qemu_log_mask(LOG_GUEST_ERROR, "buf:0x%016"PRIx64" ...\n",
1083                   be64_to_cpu(*(uint64_t *)element->value));
1084 }
1085 
1086 static bool guest_state_request_check(struct guest_state_request *gsr)
1087 {
1088     int64_t num_elements, len = gsr->len;
1089     struct guest_state_buffer *gsb = gsr->gsb;
1090     struct guest_state_element *element;
1091     struct guest_state_element_type *type;
1092     uint16_t id, size;
1093 
1094     /* gsb->num_elements = 0 == 32 bits long */
1095     assert(len >= 4);
1096 
1097     num_elements = be32_to_cpu(gsb->num_elements);
1098     element = gsb->elements;
1099     len -= sizeof(gsb->num_elements);
1100 
1101     /* Walk the buffer to validate the length */
1102     while (num_elements) {
1103 
1104         id = be16_to_cpu(element->id);
1105         size = be16_to_cpu(element->size);
1106 
1107         if (false) {
1108             log_element(element, gsr);
1109         }
1110         /* buffer size too small */
1111         if (len < 0) {
1112             return false;
1113         }
1114 
1115         type = guest_state_element_type_find(id);
1116         if (!type) {
1117             qemu_log_mask(LOG_GUEST_ERROR, "Element ID %04x unknown\n", id);
1118             log_element(element, gsr);
1119             return false;
1120         }
1121 
1122         if (id == GSB_HV_VCPU_IGNORED_ID) {
1123             goto next_element;
1124         }
1125 
1126         if (size != type->size) {
1127             qemu_log_mask(LOG_GUEST_ERROR, "Size mismatch. Element ID:%04x."
1128                           "Size Exp:%i Got:%i\n", id, type->size, size);
1129             log_element(element, gsr);
1130             return false;
1131         }
1132 
1133         if ((type->flags & GUEST_STATE_ELEMENT_TYPE_FLAG_READ_ONLY) &&
1134             (gsr->flags & GUEST_STATE_REQUEST_SET)) {
1135             qemu_log_mask(LOG_GUEST_ERROR, "Trying to set a read-only Element "
1136                           "ID:%04x.\n", id);
1137             return false;
1138         }
1139 
1140         if (type->flags & GUEST_STATE_ELEMENT_TYPE_FLAG_GUEST_WIDE) {
1141             /* guest wide element type */
1142             if (!(gsr->flags & GUEST_STATE_REQUEST_GUEST_WIDE)) {
1143                 qemu_log_mask(LOG_GUEST_ERROR, "trying to set a guest wide "
1144                               "Element ID:%04x.\n", id);
1145                 return false;
1146             }
1147         } else {
1148             /* thread wide element type */
1149             if (gsr->flags & GUEST_STATE_REQUEST_GUEST_WIDE) {
1150                 qemu_log_mask(LOG_GUEST_ERROR, "trying to set a thread wide "
1151                               "Element ID:%04x.\n", id);
1152                 return false;
1153             }
1154         }
1155 next_element:
1156         element = guest_state_element_next(element, &len, &num_elements);
1157 
1158     }
1159     return true;
1160 }
1161 
1162 static bool is_gsr_invalid(struct guest_state_request *gsr,
1163                                    struct guest_state_element *element,
1164                                    struct guest_state_element_type *type)
1165 {
1166     if ((gsr->flags & GUEST_STATE_REQUEST_SET) &&
1167         (*(uint64_t *)(element->value) & ~(type->mask))) {
1168         log_element(element, gsr);
1169         qemu_log_mask(LOG_GUEST_ERROR, "L1 can't set reserved bits "
1170                       "(allowed mask: 0x%08"PRIx64")\n", type->mask);
1171         return true;
1172     }
1173     return false;
1174 }
1175 
1176 static target_ulong h_guest_get_capabilities(PowerPCCPU *cpu,
1177                                              SpaprMachineState *spapr,
1178                                              target_ulong opcode,
1179                                              target_ulong *args)
1180 {
1181     CPUPPCState *env = &cpu->env;
1182     target_ulong flags = args[0];
1183 
1184     if (flags) { /* don't handle any flags capabilities for now */
1185         return H_PARAMETER;
1186     }
1187 
1188     /* P10 capabilities */
1189     if (ppc_check_compat(cpu, CPU_POWERPC_LOGICAL_3_10, 0,
1190         spapr->max_compat_pvr)) {
1191         env->gpr[4] |= H_GUEST_CAPABILITIES_P10_MODE;
1192     }
1193 
1194     /* P9 capabilities */
1195     if (ppc_check_compat(cpu, CPU_POWERPC_LOGICAL_3_00, 0,
1196         spapr->max_compat_pvr)) {
1197         env->gpr[4] |= H_GUEST_CAPABILITIES_P9_MODE;
1198     }
1199 
1200     return H_SUCCESS;
1201 }
1202 
1203 static target_ulong h_guest_set_capabilities(PowerPCCPU *cpu,
1204                                              SpaprMachineState *spapr,
1205                                              target_ulong opcode,
1206                                               target_ulong *args)
1207 {
1208     CPUPPCState *env = &cpu->env;
1209     target_ulong flags = args[0];
1210     target_ulong capabilities = args[1];
1211     env->gpr[4] = 0;
1212 
1213     if (flags) { /* don't handle any flags capabilities for now */
1214         return H_PARAMETER;
1215     }
1216 
1217     if (capabilities & H_GUEST_CAPABILITIES_COPY_MEM) {
1218         env->gpr[4] = 1;
1219         return H_P2; /* isn't supported */
1220     }
1221 
1222     /*
1223      * If there are no capabilities configured, set the R5 to the index of
1224      * the first supported Power Processor Mode
1225      */
1226     if (!capabilities) {
1227         env->gpr[4] = 1;
1228 
1229         /* set R5 to the first supported Power Processor Mode */
1230         if (ppc_check_compat(cpu, CPU_POWERPC_LOGICAL_3_10, 0,
1231                              spapr->max_compat_pvr)) {
1232             env->gpr[5] = H_GUEST_CAP_P10_MODE_BMAP;
1233         } else if (ppc_check_compat(cpu, CPU_POWERPC_LOGICAL_3_00, 0,
1234                                     spapr->max_compat_pvr)) {
1235             env->gpr[5] = H_GUEST_CAP_P9_MODE_BMAP;
1236         }
1237 
1238         return H_P2;
1239     }
1240 
1241     /*
1242      * If an invalid capability is set, R5 should contain the index of the
1243      * invalid capability bit
1244      */
1245     if (capabilities & ~H_GUEST_CAP_VALID_MASK) {
1246         env->gpr[4] = 1;
1247 
1248         /* Set R5 to the index of the invalid capability */
1249         env->gpr[5] = 63 - ctz64(capabilities);
1250 
1251         return H_P2;
1252     }
1253 
1254     if (!spapr->nested.capabilities_set) {
1255         spapr->nested.capabilities_set = true;
1256         spapr->nested.pvr_base = env->spr[SPR_PVR];
1257         return H_SUCCESS;
1258     } else {
1259         return H_STATE;
1260     }
1261 }
1262 
1263 static void
1264 destroy_guest_helper(gpointer value)
1265 {
1266     struct SpaprMachineStateNestedGuest *guest = value;
1267     g_free(guest->vcpus);
1268     g_free(guest);
1269 }
1270 
1271 static target_ulong h_guest_create(PowerPCCPU *cpu,
1272                                    SpaprMachineState *spapr,
1273                                    target_ulong opcode,
1274                                    target_ulong *args)
1275 {
1276     CPUPPCState *env = &cpu->env;
1277     target_ulong flags = args[0];
1278     target_ulong continue_token = args[1];
1279     uint64_t guestid;
1280     int nguests = 0;
1281     struct SpaprMachineStateNestedGuest *guest;
1282 
1283     if (flags) { /* don't handle any flags for now */
1284         return H_UNSUPPORTED_FLAG;
1285     }
1286 
1287     if (continue_token != -1) {
1288         return H_P2;
1289     }
1290 
1291     if (!spapr->nested.capabilities_set) {
1292         return H_STATE;
1293     }
1294 
1295     if (!spapr->nested.guests) {
1296         spapr->nested.guests = g_hash_table_new_full(NULL,
1297                                                      NULL,
1298                                                      NULL,
1299                                                      destroy_guest_helper);
1300     }
1301 
1302     nguests = g_hash_table_size(spapr->nested.guests);
1303 
1304     if (nguests == PAPR_NESTED_GUEST_MAX) {
1305         return H_NO_MEM;
1306     }
1307 
1308     /* Lookup for available guestid */
1309     for (guestid = 1; guestid < PAPR_NESTED_GUEST_MAX; guestid++) {
1310         if (!(g_hash_table_lookup(spapr->nested.guests,
1311                                   GINT_TO_POINTER(guestid)))) {
1312             break;
1313         }
1314     }
1315 
1316     if (guestid == PAPR_NESTED_GUEST_MAX) {
1317         return H_NO_MEM;
1318     }
1319 
1320     guest = g_try_new0(struct SpaprMachineStateNestedGuest, 1);
1321     if (!guest) {
1322         return H_NO_MEM;
1323     }
1324 
1325     guest->pvr_logical = spapr->nested.pvr_base;
1326     g_hash_table_insert(spapr->nested.guests, GINT_TO_POINTER(guestid), guest);
1327     env->gpr[4] = guestid;
1328 
1329     return H_SUCCESS;
1330 }
1331 
1332 static target_ulong h_guest_delete(PowerPCCPU *cpu,
1333                                    SpaprMachineState *spapr,
1334                                    target_ulong opcode,
1335                                    target_ulong *args)
1336 {
1337     target_ulong flags = args[0];
1338     target_ulong guestid = args[1];
1339     struct SpaprMachineStateNestedGuest *guest;
1340 
1341     /*
1342      * handle flag deleteAllGuests, if set:
1343      * guestid is ignored and all guests are deleted
1344      *
1345      */
1346     if (flags & ~H_GUEST_DELETE_ALL_FLAG) {
1347         return H_UNSUPPORTED_FLAG; /* other flag bits reserved */
1348     } else if (flags & H_GUEST_DELETE_ALL_FLAG) {
1349         g_hash_table_destroy(spapr->nested.guests);
1350         return H_SUCCESS;
1351     }
1352 
1353     guest = g_hash_table_lookup(spapr->nested.guests, GINT_TO_POINTER(guestid));
1354     if (!guest) {
1355         return H_P2;
1356     }
1357 
1358     g_hash_table_remove(spapr->nested.guests, GINT_TO_POINTER(guestid));
1359 
1360     return H_SUCCESS;
1361 }
1362 
1363 static target_ulong h_guest_create_vcpu(PowerPCCPU *cpu,
1364                                         SpaprMachineState *spapr,
1365                                         target_ulong opcode,
1366                                         target_ulong *args)
1367 {
1368     target_ulong flags = args[0];
1369     target_ulong guestid = args[1];
1370     target_ulong vcpuid = args[2];
1371     SpaprMachineStateNestedGuest *guest;
1372 
1373     if (flags) { /* don't handle any flags for now */
1374         return H_UNSUPPORTED_FLAG;
1375     }
1376 
1377     guest = spapr_get_nested_guest(spapr, guestid);
1378     if (!guest) {
1379         return H_P2;
1380     }
1381 
1382     if (vcpuid < guest->nr_vcpus) {
1383         qemu_log_mask(LOG_UNIMP, "vcpuid " TARGET_FMT_ld " already in use.",
1384                       vcpuid);
1385         return H_IN_USE;
1386     }
1387     /* linear vcpuid allocation only */
1388     assert(vcpuid == guest->nr_vcpus);
1389 
1390     if (guest->nr_vcpus >= PAPR_NESTED_GUEST_VCPU_MAX) {
1391         return H_P3;
1392     }
1393 
1394     SpaprMachineStateNestedGuestVcpu *vcpus, *curr_vcpu;
1395     vcpus = g_try_renew(struct SpaprMachineStateNestedGuestVcpu,
1396                         guest->vcpus,
1397                         guest->nr_vcpus + 1);
1398     if (!vcpus) {
1399         return H_NO_MEM;
1400     }
1401     guest->vcpus = vcpus;
1402     curr_vcpu = &vcpus[guest->nr_vcpus];
1403     memset(curr_vcpu, 0, sizeof(SpaprMachineStateNestedGuestVcpu));
1404 
1405     curr_vcpu->enabled = true;
1406     guest->nr_vcpus++;
1407 
1408     return H_SUCCESS;
1409 }
1410 
1411 static target_ulong getset_state(SpaprMachineStateNestedGuest *guest,
1412                                  uint64_t vcpuid,
1413                                  struct guest_state_request *gsr)
1414 {
1415     void *ptr;
1416     uint16_t id;
1417     struct guest_state_element *element;
1418     struct guest_state_element_type *type;
1419     int64_t lenleft, num_elements;
1420 
1421     lenleft = gsr->len;
1422 
1423     if (!guest_state_request_check(gsr)) {
1424         return H_P3;
1425     }
1426 
1427     num_elements = be32_to_cpu(gsr->gsb->num_elements);
1428     element = gsr->gsb->elements;
1429     /* Process the elements */
1430     while (num_elements) {
1431         type = NULL;
1432         /* log_element(element, gsr); */
1433 
1434         id = be16_to_cpu(element->id);
1435         if (id == GSB_HV_VCPU_IGNORED_ID) {
1436             goto next_element;
1437         }
1438 
1439         type = guest_state_element_type_find(id);
1440         assert(type);
1441 
1442         /* Get pointer to guest data to get/set */
1443         if (type->location && type->copy) {
1444             ptr = type->location(guest, vcpuid);
1445             assert(ptr);
1446             if (!~(type->mask) && is_gsr_invalid(gsr, element, type)) {
1447                 return H_INVALID_ELEMENT_VALUE;
1448             }
1449             type->copy(ptr + type->offset, element->value,
1450                        gsr->flags & GUEST_STATE_REQUEST_SET ? true : false);
1451         }
1452 
1453 next_element:
1454         element = guest_state_element_next(element, &lenleft, &num_elements);
1455     }
1456 
1457     return H_SUCCESS;
1458 }
1459 
1460 static target_ulong map_and_getset_state(PowerPCCPU *cpu,
1461                                          SpaprMachineStateNestedGuest *guest,
1462                                          uint64_t vcpuid,
1463                                          struct guest_state_request *gsr)
1464 {
1465     target_ulong rc;
1466     int64_t len;
1467     bool is_write;
1468 
1469     len = gsr->len;
1470     /* only get_state would require write access to the provided buffer */
1471     is_write = (gsr->flags & GUEST_STATE_REQUEST_SET) ? false : true;
1472     gsr->gsb = address_space_map(CPU(cpu)->as, gsr->buf, (uint64_t *)&len,
1473                                  is_write, MEMTXATTRS_UNSPECIFIED);
1474     if (!gsr->gsb) {
1475         rc = H_P3;
1476         goto out1;
1477     }
1478 
1479     if (len != gsr->len) {
1480         rc = H_P3;
1481         goto out1;
1482     }
1483 
1484     rc = getset_state(guest, vcpuid, gsr);
1485 
1486 out1:
1487     address_space_unmap(CPU(cpu)->as, gsr->gsb, len, is_write, len);
1488     return rc;
1489 }
1490 
1491 static target_ulong h_guest_getset_state(PowerPCCPU *cpu,
1492                                          SpaprMachineState *spapr,
1493                                          target_ulong *args,
1494                                          bool set)
1495 {
1496     target_ulong flags = args[0];
1497     target_ulong lpid = args[1];
1498     target_ulong vcpuid = args[2];
1499     target_ulong buf = args[3];
1500     target_ulong buflen = args[4];
1501     struct guest_state_request gsr;
1502     SpaprMachineStateNestedGuest *guest;
1503 
1504     guest = spapr_get_nested_guest(spapr, lpid);
1505     if (!guest) {
1506         return H_P2;
1507     }
1508     gsr.buf = buf;
1509     assert(buflen <= GSB_MAX_BUF_SIZE);
1510     gsr.len = buflen;
1511     gsr.flags = 0;
1512     if (flags & H_GUEST_GETSET_STATE_FLAG_GUEST_WIDE) {
1513         gsr.flags |= GUEST_STATE_REQUEST_GUEST_WIDE;
1514     }
1515     if (flags & ~H_GUEST_GETSET_STATE_FLAG_GUEST_WIDE) {
1516         return H_PARAMETER; /* flag not supported yet */
1517     }
1518 
1519     if (set) {
1520         gsr.flags |= GUEST_STATE_REQUEST_SET;
1521     }
1522     return map_and_getset_state(cpu, guest, vcpuid, &gsr);
1523 }
1524 
1525 static target_ulong h_guest_set_state(PowerPCCPU *cpu,
1526                                       SpaprMachineState *spapr,
1527                                       target_ulong opcode,
1528                                       target_ulong *args)
1529 {
1530     return h_guest_getset_state(cpu, spapr, args, true);
1531 }
1532 
1533 static target_ulong h_guest_get_state(PowerPCCPU *cpu,
1534                                       SpaprMachineState *spapr,
1535                                       target_ulong opcode,
1536                                       target_ulong *args)
1537 {
1538     return h_guest_getset_state(cpu, spapr, args, false);
1539 }
1540 
1541 static void exit_nested_store_l2(PowerPCCPU *cpu, int excp,
1542                                  SpaprMachineStateNestedGuestVcpu *vcpu)
1543 {
1544     CPUPPCState *env = &cpu->env;
1545     SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu);
1546     target_ulong now, hdar, hdsisr, asdr;
1547 
1548     assert(sizeof(env->gpr) == sizeof(vcpu->state.gpr)); /* sanity check */
1549 
1550     now = cpu_ppc_load_tbl(env); /* L2 timebase */
1551     now -= vcpu->tb_offset; /* L1 timebase */
1552     vcpu->state.dec_expiry_tb = now - cpu_ppc_load_decr(env);
1553     cpu_ppc_store_decr(env, spapr_cpu->nested_host_state->dec_expiry_tb - now);
1554     /* backup hdar, hdsisr, asdr if reqd later below */
1555     hdar   = vcpu->state.hdar;
1556     hdsisr = vcpu->state.hdsisr;
1557     asdr   = vcpu->state.asdr;
1558 
1559     nested_save_state(&vcpu->state, cpu);
1560 
1561     if (excp == POWERPC_EXCP_MCHECK ||
1562         excp == POWERPC_EXCP_RESET ||
1563         excp == POWERPC_EXCP_SYSCALL) {
1564         vcpu->state.nip = env->spr[SPR_SRR0];
1565         vcpu->state.msr = env->spr[SPR_SRR1] & env->msr_mask;
1566     } else {
1567         vcpu->state.nip = env->spr[SPR_HSRR0];
1568         vcpu->state.msr = env->spr[SPR_HSRR1] & env->msr_mask;
1569     }
1570 
1571     /* hdar, hdsisr, asdr should be retained unless certain exceptions */
1572     if ((excp != POWERPC_EXCP_HDSI) && (excp != POWERPC_EXCP_HISI)) {
1573         vcpu->state.asdr = asdr;
1574     } else if (excp != POWERPC_EXCP_HDSI) {
1575         vcpu->state.hdar   = hdar;
1576         vcpu->state.hdsisr = hdsisr;
1577     }
1578 }
1579 
1580 static int get_exit_ids(uint64_t srr0, uint16_t ids[16])
1581 {
1582     int nr;
1583 
1584     switch (srr0) {
1585     case 0xc00:
1586         nr = 10;
1587         ids[0] = GSB_VCPU_GPR3;
1588         ids[1] = GSB_VCPU_GPR4;
1589         ids[2] = GSB_VCPU_GPR5;
1590         ids[3] = GSB_VCPU_GPR6;
1591         ids[4] = GSB_VCPU_GPR7;
1592         ids[5] = GSB_VCPU_GPR8;
1593         ids[6] = GSB_VCPU_GPR9;
1594         ids[7] = GSB_VCPU_GPR10;
1595         ids[8] = GSB_VCPU_GPR11;
1596         ids[9] = GSB_VCPU_GPR12;
1597         break;
1598     case 0xe00:
1599         nr = 5;
1600         ids[0] = GSB_VCPU_SPR_HDAR;
1601         ids[1] = GSB_VCPU_SPR_HDSISR;
1602         ids[2] = GSB_VCPU_SPR_ASDR;
1603         ids[3] = GSB_VCPU_SPR_NIA;
1604         ids[4] = GSB_VCPU_SPR_MSR;
1605         break;
1606     case 0xe20:
1607         nr = 4;
1608         ids[0] = GSB_VCPU_SPR_HDAR;
1609         ids[1] = GSB_VCPU_SPR_ASDR;
1610         ids[2] = GSB_VCPU_SPR_NIA;
1611         ids[3] = GSB_VCPU_SPR_MSR;
1612         break;
1613     case 0xe40:
1614         nr = 3;
1615         ids[0] = GSB_VCPU_SPR_HEIR;
1616         ids[1] = GSB_VCPU_SPR_NIA;
1617         ids[2] = GSB_VCPU_SPR_MSR;
1618         break;
1619     case 0xf80:
1620         nr = 3;
1621         ids[0] = GSB_VCPU_SPR_HFSCR;
1622         ids[1] = GSB_VCPU_SPR_NIA;
1623         ids[2] = GSB_VCPU_SPR_MSR;
1624         break;
1625     default:
1626         nr = 0;
1627         break;
1628     }
1629 
1630     return nr;
1631 }
1632 
1633 static void exit_process_output_buffer(PowerPCCPU *cpu,
1634                                        SpaprMachineStateNestedGuest *guest,
1635                                        target_ulong vcpuid,
1636                                        target_ulong *r3)
1637 {
1638     SpaprMachineStateNestedGuestVcpu *vcpu = &guest->vcpus[vcpuid];
1639     struct guest_state_request gsr;
1640     struct guest_state_buffer *gsb;
1641     struct guest_state_element *element;
1642     struct guest_state_element_type *type;
1643     int exit_id_count = 0;
1644     uint16_t exit_cause_ids[16];
1645     hwaddr len;
1646 
1647     len = vcpu->runbufout.size;
1648     gsb = address_space_map(CPU(cpu)->as, vcpu->runbufout.addr, &len, true,
1649                             MEMTXATTRS_UNSPECIFIED);
1650     if (!gsb || len != vcpu->runbufout.size) {
1651         address_space_unmap(CPU(cpu)->as, gsb, len, true, len);
1652         *r3 = H_P2;
1653         return;
1654     }
1655 
1656     exit_id_count = get_exit_ids(*r3, exit_cause_ids);
1657 
1658     /* Create a buffer of elements to send back */
1659     gsb->num_elements = cpu_to_be32(exit_id_count);
1660     element = gsb->elements;
1661     for (int i = 0; i < exit_id_count; i++) {
1662         type = guest_state_element_type_find(exit_cause_ids[i]);
1663         assert(type);
1664         element->id = cpu_to_be16(exit_cause_ids[i]);
1665         element->size = cpu_to_be16(type->size);
1666         element = guest_state_element_next(element, NULL, NULL);
1667     }
1668     gsr.gsb = gsb;
1669     gsr.len = VCPU_OUT_BUF_MIN_SZ;
1670     gsr.flags = 0; /* get + never guest wide */
1671     getset_state(guest, vcpuid, &gsr);
1672 
1673     address_space_unmap(CPU(cpu)->as, gsb, len, true, len);
1674     return;
1675 }
1676 
1677 static
1678 void spapr_exit_nested_papr(SpaprMachineState *spapr, PowerPCCPU *cpu, int excp)
1679 {
1680     CPUPPCState *env = &cpu->env;
1681     CPUState *cs = CPU(cpu);
1682     SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu);
1683     target_ulong r3_return = env->excp_vectors[excp]; /* hcall return value */
1684     target_ulong lpid = 0, vcpuid = 0;
1685     struct SpaprMachineStateNestedGuestVcpu *vcpu = NULL;
1686     struct SpaprMachineStateNestedGuest *guest = NULL;
1687 
1688     lpid = spapr_cpu->nested_host_state->gpr[5];
1689     vcpuid = spapr_cpu->nested_host_state->gpr[6];
1690     guest = spapr_get_nested_guest(spapr, lpid);
1691     assert(guest);
1692     spapr_nested_vcpu_check(guest, vcpuid, false);
1693     vcpu = &guest->vcpus[vcpuid];
1694 
1695     exit_nested_store_l2(cpu, excp, vcpu);
1696     /* do the output buffer for run_vcpu*/
1697     exit_process_output_buffer(cpu, guest, vcpuid, &r3_return);
1698 
1699     assert(env->spr[SPR_LPIDR] != 0);
1700     nested_load_state(cpu, spapr_cpu->nested_host_state);
1701     cpu_ppc_decrease_tb_by_offset(env, vcpu->tb_offset);
1702     env->gpr[3] = H_SUCCESS;
1703     env->gpr[4] = r3_return;
1704     nested_post_load_state(env, cs);
1705     cpu_ppc_hdecr_exit(env);
1706 
1707     spapr_cpu->in_nested = false;
1708     g_free(spapr_cpu->nested_host_state);
1709     spapr_cpu->nested_host_state = NULL;
1710 }
1711 
1712 void spapr_exit_nested(PowerPCCPU *cpu, int excp)
1713 {
1714     SpaprMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
1715     SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu);
1716 
1717     assert(spapr_cpu->in_nested);
1718     if (spapr_nested_api(spapr) == NESTED_API_KVM_HV) {
1719         spapr_exit_nested_hv(cpu, excp);
1720     } else if (spapr_nested_api(spapr) == NESTED_API_PAPR) {
1721         spapr_exit_nested_papr(spapr, cpu, excp);
1722     } else {
1723         g_assert_not_reached();
1724     }
1725 }
1726 
1727 static void nested_papr_load_l2(PowerPCCPU *cpu,
1728                                 CPUPPCState *env,
1729                                 SpaprMachineStateNestedGuestVcpu *vcpu,
1730                                 target_ulong now)
1731 {
1732     PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
1733     target_ulong lpcr, lpcr_mask, hdec;
1734     lpcr_mask = LPCR_DPFD | LPCR_ILE | LPCR_AIL | LPCR_LD | LPCR_MER;
1735 
1736     assert(vcpu);
1737     assert(sizeof(env->gpr) == sizeof(vcpu->state.gpr));
1738     nested_load_state(cpu, &vcpu->state);
1739     lpcr = (env->spr[SPR_LPCR] & ~lpcr_mask) |
1740            (vcpu->state.lpcr & lpcr_mask);
1741     lpcr |= LPCR_HR | LPCR_UPRT | LPCR_GTSE | LPCR_HVICE | LPCR_HDICE;
1742     lpcr &= ~LPCR_LPES0;
1743     env->spr[SPR_LPCR] = lpcr & pcc->lpcr_mask;
1744 
1745     hdec = vcpu->hdecr_expiry_tb - now;
1746     cpu_ppc_store_decr(env, vcpu->state.dec_expiry_tb - now);
1747     cpu_ppc_hdecr_init(env);
1748     cpu_ppc_store_hdecr(env, hdec);
1749 
1750     cpu_ppc_increase_tb_by_offset(env, vcpu->tb_offset);
1751 }
1752 
1753 static void nested_papr_run_vcpu(PowerPCCPU *cpu,
1754                                  uint64_t lpid,
1755                                  SpaprMachineStateNestedGuestVcpu *vcpu)
1756 {
1757     SpaprMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
1758     CPUPPCState *env = &cpu->env;
1759     CPUState *cs = CPU(cpu);
1760     SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu);
1761     target_ulong now = cpu_ppc_load_tbl(env);
1762 
1763     assert(env->spr[SPR_LPIDR] == 0);
1764     assert(spapr->nested.api); /* ensure API version is initialized */
1765     spapr_cpu->nested_host_state = g_try_new(struct nested_ppc_state, 1);
1766     assert(spapr_cpu->nested_host_state);
1767     nested_save_state(spapr_cpu->nested_host_state, cpu);
1768     spapr_cpu->nested_host_state->dec_expiry_tb = now - cpu_ppc_load_decr(env);
1769     nested_papr_load_l2(cpu, env, vcpu, now);
1770     env->spr[SPR_LPIDR] = lpid; /* post load l2 */
1771 
1772     spapr_cpu->in_nested = true;
1773     nested_post_load_state(env, cs);
1774 }
1775 
1776 static target_ulong h_guest_run_vcpu(PowerPCCPU *cpu,
1777                                      SpaprMachineState *spapr,
1778                                      target_ulong opcode,
1779                                      target_ulong *args)
1780 {
1781     CPUPPCState *env = &cpu->env;
1782     target_ulong flags = args[0];
1783     target_ulong lpid = args[1];
1784     target_ulong vcpuid = args[2];
1785     struct SpaprMachineStateNestedGuestVcpu *vcpu;
1786     struct guest_state_request gsr;
1787     SpaprMachineStateNestedGuest *guest;
1788     target_ulong rc;
1789 
1790     if (flags) /* don't handle any flags for now */
1791         return H_PARAMETER;
1792 
1793     guest = spapr_get_nested_guest(spapr, lpid);
1794     if (!guest) {
1795         return H_P2;
1796     }
1797     if (!spapr_nested_vcpu_check(guest, vcpuid, true)) {
1798         return H_P3;
1799     }
1800 
1801     if (guest->parttbl[0] == 0) {
1802         /* At least need a partition scoped radix tree */
1803         return H_NOT_AVAILABLE;
1804     }
1805 
1806     vcpu = &guest->vcpus[vcpuid];
1807 
1808     /* Read run_vcpu input buffer to update state */
1809     gsr.buf = vcpu->runbufin.addr;
1810     gsr.len = vcpu->runbufin.size;
1811     gsr.flags = GUEST_STATE_REQUEST_SET; /* Thread wide + writing */
1812     rc = map_and_getset_state(cpu, guest, vcpuid, &gsr);
1813     if (rc == H_SUCCESS) {
1814         nested_papr_run_vcpu(cpu, lpid, vcpu);
1815     } else {
1816         env->gpr[3] = rc;
1817     }
1818     return env->gpr[3];
1819 }
1820 
1821 void spapr_register_nested_hv(void)
1822 {
1823     spapr_register_hypercall(KVMPPC_H_SET_PARTITION_TABLE, h_set_ptbl);
1824     spapr_register_hypercall(KVMPPC_H_ENTER_NESTED, h_enter_nested);
1825     spapr_register_hypercall(KVMPPC_H_TLB_INVALIDATE, h_tlb_invalidate);
1826     spapr_register_hypercall(KVMPPC_H_COPY_TOFROM_GUEST, h_copy_tofrom_guest);
1827 }
1828 
1829 void spapr_unregister_nested_hv(void)
1830 {
1831     spapr_unregister_hypercall(KVMPPC_H_SET_PARTITION_TABLE);
1832     spapr_unregister_hypercall(KVMPPC_H_ENTER_NESTED);
1833     spapr_unregister_hypercall(KVMPPC_H_TLB_INVALIDATE);
1834     spapr_unregister_hypercall(KVMPPC_H_COPY_TOFROM_GUEST);
1835 }
1836 
1837 void spapr_register_nested_papr(void)
1838 {
1839     spapr_register_hypercall(H_GUEST_GET_CAPABILITIES,
1840                              h_guest_get_capabilities);
1841     spapr_register_hypercall(H_GUEST_SET_CAPABILITIES,
1842                              h_guest_set_capabilities);
1843     spapr_register_hypercall(H_GUEST_CREATE, h_guest_create);
1844     spapr_register_hypercall(H_GUEST_DELETE, h_guest_delete);
1845     spapr_register_hypercall(H_GUEST_CREATE_VCPU, h_guest_create_vcpu);
1846     spapr_register_hypercall(H_GUEST_SET_STATE, h_guest_set_state);
1847     spapr_register_hypercall(H_GUEST_GET_STATE, h_guest_get_state);
1848     spapr_register_hypercall(H_GUEST_RUN_VCPU, h_guest_run_vcpu);
1849 }
1850 
1851 void spapr_unregister_nested_papr(void)
1852 {
1853     spapr_unregister_hypercall(H_GUEST_GET_CAPABILITIES);
1854     spapr_unregister_hypercall(H_GUEST_SET_CAPABILITIES);
1855     spapr_unregister_hypercall(H_GUEST_CREATE);
1856     spapr_unregister_hypercall(H_GUEST_DELETE);
1857     spapr_unregister_hypercall(H_GUEST_CREATE_VCPU);
1858     spapr_unregister_hypercall(H_GUEST_SET_STATE);
1859     spapr_unregister_hypercall(H_GUEST_GET_STATE);
1860     spapr_unregister_hypercall(H_GUEST_RUN_VCPU);
1861 }
1862 
1863 #else
1864 void spapr_exit_nested(PowerPCCPU *cpu, int excp)
1865 {
1866     g_assert_not_reached();
1867 }
1868 
1869 void spapr_register_nested_hv(void)
1870 {
1871     /* DO NOTHING */
1872 }
1873 
1874 void spapr_unregister_nested_hv(void)
1875 {
1876     /* DO NOTHING */
1877 }
1878 
1879 bool spapr_get_pate_nested_hv(SpaprMachineState *spapr, PowerPCCPU *cpu,
1880                               target_ulong lpid, ppc_v3_pate_t *entry)
1881 {
1882     return false;
1883 }
1884 
1885 bool spapr_get_pate_nested_papr(SpaprMachineState *spapr, PowerPCCPU *cpu,
1886                                 target_ulong lpid, ppc_v3_pate_t *entry)
1887 {
1888     return false;
1889 }
1890 
1891 void spapr_register_nested_papr(void)
1892 {
1893     /* DO NOTHING */
1894 }
1895 
1896 void spapr_unregister_nested_papr(void)
1897 {
1898     /* DO NOTHING */
1899 }
1900 
1901 void spapr_nested_gsb_init(void)
1902 {
1903     /* DO NOTHING */
1904 }
1905 
1906 #endif
1907