xref: /openbmc/qemu/hw/ppc/spapr_nested.c (revision 21a8d22f58b7e8eb70f5cf48ba119d7865a37fc0)
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 
11 void spapr_nested_reset(SpaprMachineState *spapr)
12 {
13     if (spapr_get_cap(spapr, SPAPR_CAP_NESTED_KVM_HV)) {
14         spapr->nested.api = NESTED_API_KVM_HV;
15         spapr_unregister_nested_hv();
16         spapr_register_nested_hv();
17     } else {
18         spapr->nested.api = 0;
19     }
20 }
21 
22 uint8_t spapr_nested_api(SpaprMachineState *spapr)
23 {
24     return spapr->nested.api;
25 }
26 
27 #ifdef CONFIG_TCG
28 
29 bool spapr_get_pate_nested_hv(SpaprMachineState *spapr, PowerPCCPU *cpu,
30                               target_ulong lpid, ppc_v3_pate_t *entry)
31 {
32     uint64_t patb, pats;
33 
34     assert(lpid != 0);
35 
36     patb = spapr->nested.ptcr & PTCR_PATB;
37     pats = spapr->nested.ptcr & PTCR_PATS;
38 
39     /* Check if partition table is properly aligned */
40     if (patb & MAKE_64BIT_MASK(0, pats + 12)) {
41         return false;
42     }
43 
44     /* Calculate number of entries */
45     pats = 1ull << (pats + 12 - 4);
46     if (pats <= lpid) {
47         return false;
48     }
49 
50     /* Grab entry */
51     patb += 16 * lpid;
52     entry->dw0 = ldq_phys(CPU(cpu)->as, patb);
53     entry->dw1 = ldq_phys(CPU(cpu)->as, patb + 8);
54     return true;
55 }
56 
57 #define PRTS_MASK      0x1f
58 
59 static target_ulong h_set_ptbl(PowerPCCPU *cpu,
60                                SpaprMachineState *spapr,
61                                target_ulong opcode,
62                                target_ulong *args)
63 {
64     target_ulong ptcr = args[0];
65 
66     if (!spapr_get_cap(spapr, SPAPR_CAP_NESTED_KVM_HV)) {
67         return H_FUNCTION;
68     }
69 
70     if ((ptcr & PRTS_MASK) + 12 - 4 > 12) {
71         return H_PARAMETER;
72     }
73 
74     spapr->nested.ptcr = ptcr; /* Save new partition table */
75 
76     return H_SUCCESS;
77 }
78 
79 static target_ulong h_tlb_invalidate(PowerPCCPU *cpu,
80                                      SpaprMachineState *spapr,
81                                      target_ulong opcode,
82                                      target_ulong *args)
83 {
84     /*
85      * The spapr virtual hypervisor nested HV implementation retains no L2
86      * translation state except for TLB. And the TLB is always invalidated
87      * across L1<->L2 transitions, so nothing is required here.
88      */
89 
90     return H_SUCCESS;
91 }
92 
93 static target_ulong h_copy_tofrom_guest(PowerPCCPU *cpu,
94                                         SpaprMachineState *spapr,
95                                         target_ulong opcode,
96                                         target_ulong *args)
97 {
98     /*
99      * This HCALL is not required, L1 KVM will take a slow path and walk the
100      * page tables manually to do the data copy.
101      */
102     return H_FUNCTION;
103 }
104 
105 static void nested_save_state(struct nested_ppc_state *save, PowerPCCPU *cpu)
106 {
107     CPUPPCState *env = &cpu->env;
108 
109     memcpy(save->gpr, env->gpr, sizeof(save->gpr));
110 
111     save->lr = env->lr;
112     save->ctr = env->ctr;
113     save->cfar = env->cfar;
114     save->msr = env->msr;
115     save->nip = env->nip;
116 
117     save->cr = ppc_get_cr(env);
118     save->xer = cpu_read_xer(env);
119 
120     save->lpcr = env->spr[SPR_LPCR];
121     save->lpidr = env->spr[SPR_LPIDR];
122     save->pcr = env->spr[SPR_PCR];
123     save->dpdes = env->spr[SPR_DPDES];
124     save->hfscr = env->spr[SPR_HFSCR];
125     save->srr0 = env->spr[SPR_SRR0];
126     save->srr1 = env->spr[SPR_SRR1];
127     save->sprg0 = env->spr[SPR_SPRG0];
128     save->sprg1 = env->spr[SPR_SPRG1];
129     save->sprg2 = env->spr[SPR_SPRG2];
130     save->sprg3 = env->spr[SPR_SPRG3];
131     save->pidr = env->spr[SPR_BOOKS_PID];
132     save->ppr = env->spr[SPR_PPR];
133 
134     save->tb_offset = env->tb_env->tb_offset;
135 }
136 
137 static void nested_load_state(PowerPCCPU *cpu, struct nested_ppc_state *load)
138 {
139     CPUState *cs = CPU(cpu);
140     CPUPPCState *env = &cpu->env;
141 
142     memcpy(env->gpr, load->gpr, sizeof(env->gpr));
143 
144     env->lr = load->lr;
145     env->ctr = load->ctr;
146     env->cfar = load->cfar;
147     env->msr = load->msr;
148     env->nip = load->nip;
149 
150     ppc_set_cr(env, load->cr);
151     cpu_write_xer(env, load->xer);
152 
153     env->spr[SPR_LPCR] = load->lpcr;
154     env->spr[SPR_LPIDR] = load->lpidr;
155     env->spr[SPR_PCR] = load->pcr;
156     env->spr[SPR_DPDES] = load->dpdes;
157     env->spr[SPR_HFSCR] = load->hfscr;
158     env->spr[SPR_SRR0] = load->srr0;
159     env->spr[SPR_SRR1] = load->srr1;
160     env->spr[SPR_SPRG0] = load->sprg0;
161     env->spr[SPR_SPRG1] = load->sprg1;
162     env->spr[SPR_SPRG2] = load->sprg2;
163     env->spr[SPR_SPRG3] = load->sprg3;
164     env->spr[SPR_BOOKS_PID] = load->pidr;
165     env->spr[SPR_PPR] = load->ppr;
166 
167     env->tb_env->tb_offset = load->tb_offset;
168 
169     /*
170      * MSR updated, compute hflags and possible interrupts.
171      */
172     hreg_compute_hflags(env);
173     ppc_maybe_interrupt(env);
174 
175     /*
176      * Nested HV does not tag TLB entries between L1 and L2, so must
177      * flush on transition.
178      */
179     tlb_flush(cs);
180     env->reserve_addr = -1; /* Reset the reservation */
181 }
182 
183 /*
184  * When this handler returns, the environment is switched to the L2 guest
185  * and TCG begins running that. spapr_exit_nested() performs the switch from
186  * L2 back to L1 and returns from the H_ENTER_NESTED hcall.
187  */
188 static target_ulong h_enter_nested(PowerPCCPU *cpu,
189                                    SpaprMachineState *spapr,
190                                    target_ulong opcode,
191                                    target_ulong *args)
192 {
193     PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
194     CPUPPCState *env = &cpu->env;
195     SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu);
196     struct nested_ppc_state l2_state;
197     target_ulong hv_ptr = args[0];
198     target_ulong regs_ptr = args[1];
199     target_ulong hdec, now = cpu_ppc_load_tbl(env);
200     target_ulong lpcr, lpcr_mask;
201     struct kvmppc_hv_guest_state *hvstate;
202     struct kvmppc_hv_guest_state hv_state;
203     struct kvmppc_pt_regs *regs;
204     hwaddr len;
205 
206     if (spapr->nested.ptcr == 0) {
207         return H_NOT_AVAILABLE;
208     }
209 
210     len = sizeof(*hvstate);
211     hvstate = address_space_map(CPU(cpu)->as, hv_ptr, &len, false,
212                                 MEMTXATTRS_UNSPECIFIED);
213     if (len != sizeof(*hvstate)) {
214         address_space_unmap(CPU(cpu)->as, hvstate, len, 0, false);
215         return H_PARAMETER;
216     }
217 
218     memcpy(&hv_state, hvstate, len);
219 
220     address_space_unmap(CPU(cpu)->as, hvstate, len, len, false);
221 
222     /*
223      * We accept versions 1 and 2. Version 2 fields are unused because TCG
224      * does not implement DAWR*.
225      */
226     if (hv_state.version > HV_GUEST_STATE_VERSION) {
227         return H_PARAMETER;
228     }
229 
230     if (hv_state.lpid == 0) {
231         return H_PARAMETER;
232     }
233 
234     spapr_cpu->nested_host_state = g_try_new(struct nested_ppc_state, 1);
235     if (!spapr_cpu->nested_host_state) {
236         return H_NO_MEM;
237     }
238 
239     assert(env->spr[SPR_LPIDR] == 0);
240     assert(env->spr[SPR_DPDES] == 0);
241     nested_save_state(spapr_cpu->nested_host_state, cpu);
242 
243     len = sizeof(*regs);
244     regs = address_space_map(CPU(cpu)->as, regs_ptr, &len, false,
245                                 MEMTXATTRS_UNSPECIFIED);
246     if (!regs || len != sizeof(*regs)) {
247         address_space_unmap(CPU(cpu)->as, regs, len, 0, false);
248         g_free(spapr_cpu->nested_host_state);
249         return H_P2;
250     }
251 
252     len = sizeof(l2_state.gpr);
253     assert(len == sizeof(regs->gpr));
254     memcpy(l2_state.gpr, regs->gpr, len);
255 
256     l2_state.lr = regs->link;
257     l2_state.ctr = regs->ctr;
258     l2_state.xer = regs->xer;
259     l2_state.cr = regs->ccr;
260     l2_state.msr = regs->msr;
261     l2_state.nip = regs->nip;
262 
263     address_space_unmap(CPU(cpu)->as, regs, len, len, false);
264 
265     l2_state.cfar = hv_state.cfar;
266     l2_state.lpidr = hv_state.lpid;
267 
268     lpcr_mask = LPCR_DPFD | LPCR_ILE | LPCR_AIL | LPCR_LD | LPCR_MER;
269     lpcr = (env->spr[SPR_LPCR] & ~lpcr_mask) | (hv_state.lpcr & lpcr_mask);
270     lpcr |= LPCR_HR | LPCR_UPRT | LPCR_GTSE | LPCR_HVICE | LPCR_HDICE;
271     lpcr &= ~LPCR_LPES0;
272     l2_state.lpcr = lpcr & pcc->lpcr_mask;
273 
274     l2_state.pcr = hv_state.pcr;
275     /* hv_state.amor is not used */
276     l2_state.dpdes = hv_state.dpdes;
277     l2_state.hfscr = hv_state.hfscr;
278     /* TCG does not implement DAWR*, CIABR, PURR, SPURR, IC, VTB, HEIR SPRs*/
279     l2_state.srr0 = hv_state.srr0;
280     l2_state.srr1 = hv_state.srr1;
281     l2_state.sprg0 = hv_state.sprg[0];
282     l2_state.sprg1 = hv_state.sprg[1];
283     l2_state.sprg2 = hv_state.sprg[2];
284     l2_state.sprg3 = hv_state.sprg[3];
285     l2_state.pidr = hv_state.pidr;
286     l2_state.ppr = hv_state.ppr;
287     l2_state.tb_offset = env->tb_env->tb_offset + hv_state.tb_offset;
288 
289     /*
290      * Switch to the nested guest environment and start the "hdec" timer.
291      */
292     nested_load_state(cpu, &l2_state);
293 
294     hdec = hv_state.hdec_expiry - now;
295     cpu_ppc_hdecr_init(env);
296     cpu_ppc_store_hdecr(env, hdec);
297 
298     /*
299      * The hv_state.vcpu_token is not needed. It is used by the KVM
300      * implementation to remember which L2 vCPU last ran on which physical
301      * CPU so as to invalidate process scope translations if it is moved
302      * between physical CPUs. For now TLBs are always flushed on L1<->L2
303      * transitions so this is not a problem.
304      *
305      * Could validate that the same vcpu_token does not attempt to run on
306      * different L1 vCPUs at the same time, but that would be a L1 KVM bug
307      * and it's not obviously worth a new data structure to do it.
308      */
309 
310     spapr_cpu->in_nested = true;
311 
312     /*
313      * The spapr hcall helper sets env->gpr[3] to the return value, but at
314      * this point the L1 is not returning from the hcall but rather we
315      * start running the L2, so r3 must not be clobbered, so return env->gpr[3]
316      * to leave it unchanged.
317      */
318     return env->gpr[3];
319 }
320 
321 static void spapr_exit_nested_hv(PowerPCCPU *cpu, int excp)
322 {
323     CPUPPCState *env = &cpu->env;
324     SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu);
325     struct nested_ppc_state l2_state;
326     target_ulong hv_ptr = spapr_cpu->nested_host_state->gpr[4];
327     target_ulong regs_ptr = spapr_cpu->nested_host_state->gpr[5];
328     target_ulong hsrr0, hsrr1, hdar, asdr, hdsisr;
329     struct kvmppc_hv_guest_state *hvstate;
330     struct kvmppc_pt_regs *regs;
331     hwaddr len;
332 
333     nested_save_state(&l2_state, cpu);
334     hsrr0 = env->spr[SPR_HSRR0];
335     hsrr1 = env->spr[SPR_HSRR1];
336     hdar = env->spr[SPR_HDAR];
337     hdsisr = env->spr[SPR_HDSISR];
338     asdr = env->spr[SPR_ASDR];
339 
340     /*
341      * Switch back to the host environment (including for any error).
342      */
343     assert(env->spr[SPR_LPIDR] != 0);
344     nested_load_state(cpu, spapr_cpu->nested_host_state);
345     env->gpr[3] = env->excp_vectors[excp]; /* hcall return value */
346 
347     cpu_ppc_hdecr_exit(env);
348 
349     spapr_cpu->in_nested = false;
350 
351     g_free(spapr_cpu->nested_host_state);
352     spapr_cpu->nested_host_state = NULL;
353 
354     len = sizeof(*hvstate);
355     hvstate = address_space_map(CPU(cpu)->as, hv_ptr, &len, true,
356                                 MEMTXATTRS_UNSPECIFIED);
357     if (len != sizeof(*hvstate)) {
358         address_space_unmap(CPU(cpu)->as, hvstate, len, 0, true);
359         env->gpr[3] = H_PARAMETER;
360         return;
361     }
362 
363     hvstate->cfar = l2_state.cfar;
364     hvstate->lpcr = l2_state.lpcr;
365     hvstate->pcr = l2_state.pcr;
366     hvstate->dpdes = l2_state.dpdes;
367     hvstate->hfscr = l2_state.hfscr;
368 
369     if (excp == POWERPC_EXCP_HDSI) {
370         hvstate->hdar = hdar;
371         hvstate->hdsisr = hdsisr;
372         hvstate->asdr = asdr;
373     } else if (excp == POWERPC_EXCP_HISI) {
374         hvstate->asdr = asdr;
375     }
376 
377     /* HEIR should be implemented for HV mode and saved here. */
378     hvstate->srr0 = l2_state.srr0;
379     hvstate->srr1 = l2_state.srr1;
380     hvstate->sprg[0] = l2_state.sprg0;
381     hvstate->sprg[1] = l2_state.sprg1;
382     hvstate->sprg[2] = l2_state.sprg2;
383     hvstate->sprg[3] = l2_state.sprg3;
384     hvstate->pidr = l2_state.pidr;
385     hvstate->ppr = l2_state.ppr;
386 
387     /* Is it okay to specify write length larger than actual data written? */
388     address_space_unmap(CPU(cpu)->as, hvstate, len, len, true);
389 
390     len = sizeof(*regs);
391     regs = address_space_map(CPU(cpu)->as, regs_ptr, &len, true,
392                                 MEMTXATTRS_UNSPECIFIED);
393     if (!regs || len != sizeof(*regs)) {
394         address_space_unmap(CPU(cpu)->as, regs, len, 0, true);
395         env->gpr[3] = H_P2;
396         return;
397     }
398 
399     len = sizeof(env->gpr);
400     assert(len == sizeof(regs->gpr));
401     memcpy(regs->gpr, l2_state.gpr, len);
402 
403     regs->link = l2_state.lr;
404     regs->ctr = l2_state.ctr;
405     regs->xer = l2_state.xer;
406     regs->ccr = l2_state.cr;
407 
408     if (excp == POWERPC_EXCP_MCHECK ||
409         excp == POWERPC_EXCP_RESET ||
410         excp == POWERPC_EXCP_SYSCALL) {
411         regs->nip = l2_state.srr0;
412         regs->msr = l2_state.srr1 & env->msr_mask;
413     } else {
414         regs->nip = hsrr0;
415         regs->msr = hsrr1 & env->msr_mask;
416     }
417 
418     /* Is it okay to specify write length larger than actual data written? */
419     address_space_unmap(CPU(cpu)->as, regs, len, len, true);
420 }
421 
422 void spapr_exit_nested(PowerPCCPU *cpu, int excp)
423 {
424     SpaprMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
425     SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu);
426 
427     assert(spapr_cpu->in_nested);
428     if (spapr_nested_api(spapr) == NESTED_API_KVM_HV) {
429         spapr_exit_nested_hv(cpu, excp);
430     } else {
431         g_assert_not_reached();
432     }
433 }
434 
435 void spapr_register_nested_hv(void)
436 {
437     spapr_register_hypercall(KVMPPC_H_SET_PARTITION_TABLE, h_set_ptbl);
438     spapr_register_hypercall(KVMPPC_H_ENTER_NESTED, h_enter_nested);
439     spapr_register_hypercall(KVMPPC_H_TLB_INVALIDATE, h_tlb_invalidate);
440     spapr_register_hypercall(KVMPPC_H_COPY_TOFROM_GUEST, h_copy_tofrom_guest);
441 }
442 
443 void spapr_unregister_nested_hv(void)
444 {
445     spapr_unregister_hypercall(KVMPPC_H_SET_PARTITION_TABLE);
446     spapr_unregister_hypercall(KVMPPC_H_ENTER_NESTED);
447     spapr_unregister_hypercall(KVMPPC_H_TLB_INVALIDATE);
448     spapr_unregister_hypercall(KVMPPC_H_COPY_TOFROM_GUEST);
449 }
450 #else
451 void spapr_exit_nested(PowerPCCPU *cpu, int excp)
452 {
453     g_assert_not_reached();
454 }
455 
456 void spapr_register_nested_hv(void)
457 {
458     /* DO NOTHING */
459 }
460 
461 void spapr_unregister_nested_hv(void)
462 {
463     /* DO NOTHING */
464 }
465 
466 bool spapr_get_pate_nested_hv(SpaprMachineState *spapr, PowerPCCPU *cpu,
467                               target_ulong lpid, ppc_v3_pate_t *entry)
468 {
469     return false;
470 }
471 #endif
472