Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/arch/powerpc/kvm/book3s_hv_nested.c
26442 views
1
// SPDX-License-Identifier: GPL-2.0
2
/*
3
* Copyright IBM Corporation, 2018
4
* Authors Suraj Jitindar Singh <[email protected]>
5
* Paul Mackerras <[email protected]>
6
*
7
* Description: KVM functions specific to running nested KVM-HV guests
8
* on Book3S processors (specifically POWER9 and later).
9
*/
10
11
#include <linux/kernel.h>
12
#include <linux/kvm_host.h>
13
#include <linux/llist.h>
14
#include <linux/pgtable.h>
15
16
#include <asm/kvm_ppc.h>
17
#include <asm/kvm_book3s.h>
18
#include <asm/mmu.h>
19
#include <asm/pgalloc.h>
20
#include <asm/pte-walk.h>
21
#include <asm/reg.h>
22
#include <asm/plpar_wrappers.h>
23
#include <asm/firmware.h>
24
25
static struct patb_entry *pseries_partition_tb;
26
27
static void kvmhv_update_ptbl_cache(struct kvm_nested_guest *gp);
28
static void kvmhv_free_memslot_nest_rmap(struct kvm_memory_slot *free);
29
30
void kvmhv_save_hv_regs(struct kvm_vcpu *vcpu, struct hv_guest_state *hr)
31
{
32
struct kvmppc_vcore *vc = vcpu->arch.vcore;
33
34
hr->pcr = vc->pcr | PCR_MASK;
35
hr->dpdes = vcpu->arch.doorbell_request;
36
hr->hfscr = vcpu->arch.hfscr;
37
hr->tb_offset = vc->tb_offset;
38
hr->dawr0 = vcpu->arch.dawr0;
39
hr->dawrx0 = vcpu->arch.dawrx0;
40
hr->ciabr = vcpu->arch.ciabr;
41
hr->purr = vcpu->arch.purr;
42
hr->spurr = vcpu->arch.spurr;
43
hr->ic = vcpu->arch.ic;
44
hr->vtb = vc->vtb;
45
hr->srr0 = vcpu->arch.shregs.srr0;
46
hr->srr1 = vcpu->arch.shregs.srr1;
47
hr->sprg[0] = vcpu->arch.shregs.sprg0;
48
hr->sprg[1] = vcpu->arch.shregs.sprg1;
49
hr->sprg[2] = vcpu->arch.shregs.sprg2;
50
hr->sprg[3] = vcpu->arch.shregs.sprg3;
51
hr->pidr = vcpu->arch.pid;
52
hr->cfar = vcpu->arch.cfar;
53
hr->ppr = vcpu->arch.ppr;
54
hr->dawr1 = vcpu->arch.dawr1;
55
hr->dawrx1 = vcpu->arch.dawrx1;
56
}
57
58
/* Use noinline_for_stack due to https://llvm.org/pr49610 */
59
static noinline_for_stack void byteswap_pt_regs(struct pt_regs *regs)
60
{
61
unsigned long *addr = (unsigned long *) regs;
62
63
for (; addr < ((unsigned long *) (regs + 1)); addr++)
64
*addr = swab64(*addr);
65
}
66
67
static void byteswap_hv_regs(struct hv_guest_state *hr)
68
{
69
hr->version = swab64(hr->version);
70
hr->lpid = swab32(hr->lpid);
71
hr->vcpu_token = swab32(hr->vcpu_token);
72
hr->lpcr = swab64(hr->lpcr);
73
hr->pcr = swab64(hr->pcr) | PCR_MASK;
74
hr->amor = swab64(hr->amor);
75
hr->dpdes = swab64(hr->dpdes);
76
hr->hfscr = swab64(hr->hfscr);
77
hr->tb_offset = swab64(hr->tb_offset);
78
hr->dawr0 = swab64(hr->dawr0);
79
hr->dawrx0 = swab64(hr->dawrx0);
80
hr->ciabr = swab64(hr->ciabr);
81
hr->hdec_expiry = swab64(hr->hdec_expiry);
82
hr->purr = swab64(hr->purr);
83
hr->spurr = swab64(hr->spurr);
84
hr->ic = swab64(hr->ic);
85
hr->vtb = swab64(hr->vtb);
86
hr->hdar = swab64(hr->hdar);
87
hr->hdsisr = swab64(hr->hdsisr);
88
hr->heir = swab64(hr->heir);
89
hr->asdr = swab64(hr->asdr);
90
hr->srr0 = swab64(hr->srr0);
91
hr->srr1 = swab64(hr->srr1);
92
hr->sprg[0] = swab64(hr->sprg[0]);
93
hr->sprg[1] = swab64(hr->sprg[1]);
94
hr->sprg[2] = swab64(hr->sprg[2]);
95
hr->sprg[3] = swab64(hr->sprg[3]);
96
hr->pidr = swab64(hr->pidr);
97
hr->cfar = swab64(hr->cfar);
98
hr->ppr = swab64(hr->ppr);
99
hr->dawr1 = swab64(hr->dawr1);
100
hr->dawrx1 = swab64(hr->dawrx1);
101
}
102
103
static void save_hv_return_state(struct kvm_vcpu *vcpu,
104
struct hv_guest_state *hr)
105
{
106
struct kvmppc_vcore *vc = vcpu->arch.vcore;
107
108
hr->dpdes = vcpu->arch.doorbell_request;
109
hr->purr = vcpu->arch.purr;
110
hr->spurr = vcpu->arch.spurr;
111
hr->ic = vcpu->arch.ic;
112
hr->vtb = vc->vtb;
113
hr->srr0 = vcpu->arch.shregs.srr0;
114
hr->srr1 = vcpu->arch.shregs.srr1;
115
hr->sprg[0] = vcpu->arch.shregs.sprg0;
116
hr->sprg[1] = vcpu->arch.shregs.sprg1;
117
hr->sprg[2] = vcpu->arch.shregs.sprg2;
118
hr->sprg[3] = vcpu->arch.shregs.sprg3;
119
hr->pidr = vcpu->arch.pid;
120
hr->cfar = vcpu->arch.cfar;
121
hr->ppr = vcpu->arch.ppr;
122
switch (vcpu->arch.trap) {
123
case BOOK3S_INTERRUPT_H_DATA_STORAGE:
124
hr->hdar = vcpu->arch.fault_dar;
125
hr->hdsisr = vcpu->arch.fault_dsisr;
126
hr->asdr = vcpu->arch.fault_gpa;
127
break;
128
case BOOK3S_INTERRUPT_H_INST_STORAGE:
129
hr->asdr = vcpu->arch.fault_gpa;
130
break;
131
case BOOK3S_INTERRUPT_H_FAC_UNAVAIL:
132
hr->hfscr = ((~HFSCR_INTR_CAUSE & hr->hfscr) |
133
(HFSCR_INTR_CAUSE & vcpu->arch.hfscr));
134
break;
135
case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
136
hr->heir = vcpu->arch.emul_inst;
137
break;
138
}
139
}
140
141
static void restore_hv_regs(struct kvm_vcpu *vcpu, const struct hv_guest_state *hr)
142
{
143
struct kvmppc_vcore *vc = vcpu->arch.vcore;
144
145
vc->pcr = hr->pcr | PCR_MASK;
146
vcpu->arch.doorbell_request = hr->dpdes;
147
vcpu->arch.hfscr = hr->hfscr;
148
vcpu->arch.dawr0 = hr->dawr0;
149
vcpu->arch.dawrx0 = hr->dawrx0;
150
vcpu->arch.ciabr = hr->ciabr;
151
vcpu->arch.purr = hr->purr;
152
vcpu->arch.spurr = hr->spurr;
153
vcpu->arch.ic = hr->ic;
154
vc->vtb = hr->vtb;
155
vcpu->arch.shregs.srr0 = hr->srr0;
156
vcpu->arch.shregs.srr1 = hr->srr1;
157
vcpu->arch.shregs.sprg0 = hr->sprg[0];
158
vcpu->arch.shregs.sprg1 = hr->sprg[1];
159
vcpu->arch.shregs.sprg2 = hr->sprg[2];
160
vcpu->arch.shregs.sprg3 = hr->sprg[3];
161
vcpu->arch.pid = hr->pidr;
162
vcpu->arch.cfar = hr->cfar;
163
vcpu->arch.ppr = hr->ppr;
164
vcpu->arch.dawr1 = hr->dawr1;
165
vcpu->arch.dawrx1 = hr->dawrx1;
166
}
167
168
void kvmhv_restore_hv_return_state(struct kvm_vcpu *vcpu,
169
struct hv_guest_state *hr)
170
{
171
struct kvmppc_vcore *vc = vcpu->arch.vcore;
172
173
/*
174
* This L2 vCPU might have received a doorbell while H_ENTER_NESTED was being handled.
175
* Make sure we preserve the doorbell if it was either:
176
* a) Sent after H_ENTER_NESTED was called on this vCPU (arch.doorbell_request would be 1)
177
* b) Doorbell was not handled and L2 exited for some other reason (hr->dpdes would be 1)
178
*/
179
vcpu->arch.doorbell_request = vcpu->arch.doorbell_request | hr->dpdes;
180
vcpu->arch.hfscr = hr->hfscr;
181
vcpu->arch.purr = hr->purr;
182
vcpu->arch.spurr = hr->spurr;
183
vcpu->arch.ic = hr->ic;
184
vc->vtb = hr->vtb;
185
vcpu->arch.fault_dar = hr->hdar;
186
vcpu->arch.fault_dsisr = hr->hdsisr;
187
vcpu->arch.fault_gpa = hr->asdr;
188
vcpu->arch.emul_inst = hr->heir;
189
vcpu->arch.shregs.srr0 = hr->srr0;
190
vcpu->arch.shregs.srr1 = hr->srr1;
191
vcpu->arch.shregs.sprg0 = hr->sprg[0];
192
vcpu->arch.shregs.sprg1 = hr->sprg[1];
193
vcpu->arch.shregs.sprg2 = hr->sprg[2];
194
vcpu->arch.shregs.sprg3 = hr->sprg[3];
195
vcpu->arch.pid = hr->pidr;
196
vcpu->arch.cfar = hr->cfar;
197
vcpu->arch.ppr = hr->ppr;
198
}
199
200
static void kvmhv_nested_mmio_needed(struct kvm_vcpu *vcpu, u64 regs_ptr)
201
{
202
/* No need to reflect the page fault to L1, we've handled it */
203
vcpu->arch.trap = 0;
204
205
/*
206
* Since the L2 gprs have already been written back into L1 memory when
207
* we complete the mmio, store the L1 memory location of the L2 gpr
208
* being loaded into by the mmio so that the loaded value can be
209
* written there in kvmppc_complete_mmio_load()
210
*/
211
if (((vcpu->arch.io_gpr & KVM_MMIO_REG_EXT_MASK) == KVM_MMIO_REG_GPR)
212
&& (vcpu->mmio_is_write == 0)) {
213
vcpu->arch.nested_io_gpr = (gpa_t) regs_ptr +
214
offsetof(struct pt_regs,
215
gpr[vcpu->arch.io_gpr]);
216
vcpu->arch.io_gpr = KVM_MMIO_REG_NESTED_GPR;
217
}
218
}
219
220
static int kvmhv_read_guest_state_and_regs(struct kvm_vcpu *vcpu,
221
struct hv_guest_state *l2_hv,
222
struct pt_regs *l2_regs,
223
u64 hv_ptr, u64 regs_ptr)
224
{
225
int size;
226
227
if (kvm_vcpu_read_guest(vcpu, hv_ptr, &l2_hv->version,
228
sizeof(l2_hv->version)))
229
return -1;
230
231
if (kvmppc_need_byteswap(vcpu))
232
l2_hv->version = swab64(l2_hv->version);
233
234
size = hv_guest_state_size(l2_hv->version);
235
if (size < 0)
236
return -1;
237
238
return kvm_vcpu_read_guest(vcpu, hv_ptr, l2_hv, size) ||
239
kvm_vcpu_read_guest(vcpu, regs_ptr, l2_regs,
240
sizeof(struct pt_regs));
241
}
242
243
static int kvmhv_write_guest_state_and_regs(struct kvm_vcpu *vcpu,
244
struct hv_guest_state *l2_hv,
245
struct pt_regs *l2_regs,
246
u64 hv_ptr, u64 regs_ptr)
247
{
248
int size;
249
250
size = hv_guest_state_size(l2_hv->version);
251
if (size < 0)
252
return -1;
253
254
return kvm_vcpu_write_guest(vcpu, hv_ptr, l2_hv, size) ||
255
kvm_vcpu_write_guest(vcpu, regs_ptr, l2_regs,
256
sizeof(struct pt_regs));
257
}
258
259
static void load_l2_hv_regs(struct kvm_vcpu *vcpu,
260
const struct hv_guest_state *l2_hv,
261
const struct hv_guest_state *l1_hv, u64 *lpcr)
262
{
263
struct kvmppc_vcore *vc = vcpu->arch.vcore;
264
u64 mask;
265
266
restore_hv_regs(vcpu, l2_hv);
267
268
/*
269
* Don't let L1 change LPCR bits for the L2 except these:
270
*/
271
mask = LPCR_DPFD | LPCR_ILE | LPCR_TC | LPCR_AIL | LPCR_LD | LPCR_MER;
272
273
/*
274
* Additional filtering is required depending on hardware
275
* and configuration.
276
*/
277
*lpcr = kvmppc_filter_lpcr_hv(vcpu->kvm,
278
(vc->lpcr & ~mask) | (*lpcr & mask));
279
280
/*
281
* Don't let L1 enable features for L2 which we don't allow for L1,
282
* but preserve the interrupt cause field.
283
*/
284
vcpu->arch.hfscr = l2_hv->hfscr & (HFSCR_INTR_CAUSE | vcpu->arch.hfscr_permitted);
285
286
/* Don't let data address watchpoint match in hypervisor state */
287
vcpu->arch.dawrx0 = l2_hv->dawrx0 & ~DAWRX_HYP;
288
vcpu->arch.dawrx1 = l2_hv->dawrx1 & ~DAWRX_HYP;
289
290
/* Don't let completed instruction address breakpt match in HV state */
291
if ((l2_hv->ciabr & CIABR_PRIV) == CIABR_PRIV_HYPER)
292
vcpu->arch.ciabr = l2_hv->ciabr & ~CIABR_PRIV;
293
}
294
295
long kvmhv_enter_nested_guest(struct kvm_vcpu *vcpu)
296
{
297
long int err, r;
298
struct kvm_nested_guest *l2;
299
struct pt_regs l2_regs, saved_l1_regs;
300
struct hv_guest_state l2_hv = {0}, saved_l1_hv;
301
struct kvmppc_vcore *vc = vcpu->arch.vcore;
302
u64 hv_ptr, regs_ptr;
303
u64 hdec_exp, lpcr;
304
s64 delta_purr, delta_spurr, delta_ic, delta_vtb;
305
306
if (vcpu->kvm->arch.l1_ptcr == 0)
307
return H_NOT_AVAILABLE;
308
309
if (MSR_TM_TRANSACTIONAL(vcpu->arch.shregs.msr))
310
return H_BAD_MODE;
311
312
/* copy parameters in */
313
hv_ptr = kvmppc_get_gpr(vcpu, 4);
314
regs_ptr = kvmppc_get_gpr(vcpu, 5);
315
kvm_vcpu_srcu_read_lock(vcpu);
316
err = kvmhv_read_guest_state_and_regs(vcpu, &l2_hv, &l2_regs,
317
hv_ptr, regs_ptr);
318
kvm_vcpu_srcu_read_unlock(vcpu);
319
if (err)
320
return H_PARAMETER;
321
322
if (kvmppc_need_byteswap(vcpu))
323
byteswap_hv_regs(&l2_hv);
324
if (l2_hv.version > HV_GUEST_STATE_VERSION)
325
return H_P2;
326
327
if (kvmppc_need_byteswap(vcpu))
328
byteswap_pt_regs(&l2_regs);
329
if (l2_hv.vcpu_token >= NR_CPUS)
330
return H_PARAMETER;
331
332
/*
333
* L1 must have set up a suspended state to enter the L2 in a
334
* transactional state, and only in that case. These have to be
335
* filtered out here to prevent causing a TM Bad Thing in the
336
* host HRFID. We could synthesize a TM Bad Thing back to the L1
337
* here but there doesn't seem like much point.
338
*/
339
if (MSR_TM_SUSPENDED(vcpu->arch.shregs.msr)) {
340
if (!MSR_TM_ACTIVE(l2_regs.msr))
341
return H_BAD_MODE;
342
} else {
343
if (l2_regs.msr & MSR_TS_MASK)
344
return H_BAD_MODE;
345
if (WARN_ON_ONCE(vcpu->arch.shregs.msr & MSR_TS_MASK))
346
return H_BAD_MODE;
347
}
348
349
/* translate lpid */
350
l2 = kvmhv_get_nested(vcpu->kvm, l2_hv.lpid, true);
351
if (!l2)
352
return H_PARAMETER;
353
if (!l2->l1_gr_to_hr) {
354
mutex_lock(&l2->tlb_lock);
355
kvmhv_update_ptbl_cache(l2);
356
mutex_unlock(&l2->tlb_lock);
357
}
358
359
/* save l1 values of things */
360
vcpu->arch.regs.msr = vcpu->arch.shregs.msr;
361
saved_l1_regs = vcpu->arch.regs;
362
kvmhv_save_hv_regs(vcpu, &saved_l1_hv);
363
364
/* convert TB values/offsets to host (L0) values */
365
hdec_exp = l2_hv.hdec_expiry - vc->tb_offset;
366
vc->tb_offset += l2_hv.tb_offset;
367
vcpu->arch.dec_expires += l2_hv.tb_offset;
368
369
/* set L1 state to L2 state */
370
vcpu->arch.nested = l2;
371
vcpu->arch.nested_vcpu_id = l2_hv.vcpu_token;
372
vcpu->arch.nested_hfscr = l2_hv.hfscr;
373
vcpu->arch.regs = l2_regs;
374
375
/* Guest must always run with ME enabled, HV disabled. */
376
vcpu->arch.shregs.msr = (vcpu->arch.regs.msr | MSR_ME) & ~MSR_HV;
377
378
lpcr = l2_hv.lpcr;
379
load_l2_hv_regs(vcpu, &l2_hv, &saved_l1_hv, &lpcr);
380
381
vcpu->arch.ret = RESUME_GUEST;
382
vcpu->arch.trap = 0;
383
do {
384
r = kvmhv_run_single_vcpu(vcpu, hdec_exp, lpcr);
385
} while (is_kvmppc_resume_guest(r));
386
387
/* save L2 state for return */
388
l2_regs = vcpu->arch.regs;
389
l2_regs.msr = vcpu->arch.shregs.msr;
390
delta_purr = vcpu->arch.purr - l2_hv.purr;
391
delta_spurr = vcpu->arch.spurr - l2_hv.spurr;
392
delta_ic = vcpu->arch.ic - l2_hv.ic;
393
delta_vtb = vc->vtb - l2_hv.vtb;
394
save_hv_return_state(vcpu, &l2_hv);
395
396
/* restore L1 state */
397
vcpu->arch.nested = NULL;
398
vcpu->arch.regs = saved_l1_regs;
399
vcpu->arch.shregs.msr = saved_l1_regs.msr & ~MSR_TS_MASK;
400
/* set L1 MSR TS field according to L2 transaction state */
401
if (l2_regs.msr & MSR_TS_MASK)
402
vcpu->arch.shregs.msr |= MSR_TS_S;
403
vc->tb_offset = saved_l1_hv.tb_offset;
404
/* XXX: is this always the same delta as saved_l1_hv.tb_offset? */
405
vcpu->arch.dec_expires -= l2_hv.tb_offset;
406
restore_hv_regs(vcpu, &saved_l1_hv);
407
vcpu->arch.purr += delta_purr;
408
vcpu->arch.spurr += delta_spurr;
409
vcpu->arch.ic += delta_ic;
410
vc->vtb += delta_vtb;
411
412
kvmhv_put_nested(l2);
413
414
/* copy l2_hv_state and regs back to guest */
415
if (kvmppc_need_byteswap(vcpu)) {
416
byteswap_hv_regs(&l2_hv);
417
byteswap_pt_regs(&l2_regs);
418
}
419
kvm_vcpu_srcu_read_lock(vcpu);
420
err = kvmhv_write_guest_state_and_regs(vcpu, &l2_hv, &l2_regs,
421
hv_ptr, regs_ptr);
422
kvm_vcpu_srcu_read_unlock(vcpu);
423
if (err)
424
return H_AUTHORITY;
425
426
if (r == -EINTR)
427
return H_INTERRUPT;
428
429
if (vcpu->mmio_needed) {
430
kvmhv_nested_mmio_needed(vcpu, regs_ptr);
431
return H_TOO_HARD;
432
}
433
434
return vcpu->arch.trap;
435
}
436
437
unsigned long nested_capabilities;
438
439
long kvmhv_nested_init(void)
440
{
441
long int ptb_order;
442
unsigned long ptcr, host_capabilities;
443
long rc;
444
445
if (!kvmhv_on_pseries())
446
return 0;
447
if (!radix_enabled())
448
return -ENODEV;
449
450
rc = plpar_guest_get_capabilities(0, &host_capabilities);
451
if (rc == H_SUCCESS) {
452
unsigned long capabilities = 0;
453
454
if (cpu_has_feature(CPU_FTR_P11_PVR))
455
capabilities |= H_GUEST_CAP_POWER11;
456
if (cpu_has_feature(CPU_FTR_ARCH_31))
457
capabilities |= H_GUEST_CAP_POWER10;
458
if (cpu_has_feature(CPU_FTR_ARCH_300))
459
capabilities |= H_GUEST_CAP_POWER9;
460
461
nested_capabilities = capabilities & host_capabilities;
462
rc = plpar_guest_set_capabilities(0, nested_capabilities);
463
if (rc != H_SUCCESS) {
464
pr_err("kvm-hv: Could not configure parent hypervisor capabilities (rc=%ld)",
465
rc);
466
return -ENODEV;
467
}
468
469
static_branch_enable(&__kvmhv_is_nestedv2);
470
return 0;
471
}
472
473
pr_info("kvm-hv: nestedv2 get capabilities hcall failed, falling back to nestedv1 (rc=%ld)\n",
474
rc);
475
/* Partition table entry is 1<<4 bytes in size, hence the 4. */
476
ptb_order = KVM_MAX_NESTED_GUESTS_SHIFT + 4;
477
/* Minimum partition table size is 1<<12 bytes */
478
if (ptb_order < 12)
479
ptb_order = 12;
480
pseries_partition_tb = kmalloc(sizeof(struct patb_entry) << ptb_order,
481
GFP_KERNEL);
482
if (!pseries_partition_tb) {
483
pr_err("kvm-hv: failed to allocated nested partition table\n");
484
return -ENOMEM;
485
}
486
487
ptcr = __pa(pseries_partition_tb) | (ptb_order - 12);
488
rc = plpar_hcall_norets(H_SET_PARTITION_TABLE, ptcr);
489
if (rc != H_SUCCESS) {
490
pr_err("kvm-hv: Parent hypervisor does not support nesting (rc=%ld)\n",
491
rc);
492
kfree(pseries_partition_tb);
493
pseries_partition_tb = NULL;
494
return -ENODEV;
495
}
496
497
return 0;
498
}
499
500
void kvmhv_nested_exit(void)
501
{
502
/*
503
* N.B. the kvmhv_on_pseries() test is there because it enables
504
* the compiler to remove the call to plpar_hcall_norets()
505
* when CONFIG_PPC_PSERIES=n.
506
*/
507
if (kvmhv_on_pseries() && pseries_partition_tb) {
508
plpar_hcall_norets(H_SET_PARTITION_TABLE, 0);
509
kfree(pseries_partition_tb);
510
pseries_partition_tb = NULL;
511
}
512
}
513
514
void kvmhv_flush_lpid(u64 lpid)
515
{
516
long rc;
517
518
if (!kvmhv_on_pseries()) {
519
radix__flush_all_lpid(lpid);
520
return;
521
}
522
523
if (!firmware_has_feature(FW_FEATURE_RPT_INVALIDATE))
524
rc = plpar_hcall_norets(H_TLB_INVALIDATE, H_TLBIE_P1_ENC(2, 0, 1),
525
lpid, TLBIEL_INVAL_SET_LPID);
526
else
527
rc = pseries_rpt_invalidate(lpid, H_RPTI_TARGET_CMMU,
528
H_RPTI_TYPE_NESTED |
529
H_RPTI_TYPE_TLB | H_RPTI_TYPE_PWC |
530
H_RPTI_TYPE_PAT,
531
H_RPTI_PAGE_ALL, 0, -1UL);
532
if (rc)
533
pr_err("KVM: TLB LPID invalidation hcall failed, rc=%ld\n", rc);
534
}
535
536
void kvmhv_set_ptbl_entry(u64 lpid, u64 dw0, u64 dw1)
537
{
538
if (!kvmhv_on_pseries()) {
539
mmu_partition_table_set_entry(lpid, dw0, dw1, true);
540
return;
541
}
542
543
if (kvmhv_is_nestedv1()) {
544
pseries_partition_tb[lpid].patb0 = cpu_to_be64(dw0);
545
pseries_partition_tb[lpid].patb1 = cpu_to_be64(dw1);
546
/* L0 will do the necessary barriers */
547
kvmhv_flush_lpid(lpid);
548
}
549
550
if (kvmhv_is_nestedv2())
551
kvmhv_nestedv2_set_ptbl_entry(lpid, dw0, dw1);
552
}
553
554
static void kvmhv_set_nested_ptbl(struct kvm_nested_guest *gp)
555
{
556
unsigned long dw0;
557
558
dw0 = PATB_HR | radix__get_tree_size() |
559
__pa(gp->shadow_pgtable) | RADIX_PGD_INDEX_SIZE;
560
kvmhv_set_ptbl_entry(gp->shadow_lpid, dw0, gp->process_table);
561
}
562
563
/*
564
* Handle the H_SET_PARTITION_TABLE hcall.
565
* r4 = guest real address of partition table + log_2(size) - 12
566
* (formatted as for the PTCR).
567
*/
568
long kvmhv_set_partition_table(struct kvm_vcpu *vcpu)
569
{
570
struct kvm *kvm = vcpu->kvm;
571
unsigned long ptcr = kvmppc_get_gpr(vcpu, 4);
572
int srcu_idx;
573
long ret = H_SUCCESS;
574
575
srcu_idx = srcu_read_lock(&kvm->srcu);
576
/* Check partition size and base address. */
577
if ((ptcr & PRTS_MASK) + 12 - 4 > KVM_MAX_NESTED_GUESTS_SHIFT ||
578
!kvm_is_visible_gfn(vcpu->kvm, (ptcr & PRTB_MASK) >> PAGE_SHIFT))
579
ret = H_PARAMETER;
580
srcu_read_unlock(&kvm->srcu, srcu_idx);
581
if (ret == H_SUCCESS)
582
kvm->arch.l1_ptcr = ptcr;
583
584
return ret;
585
}
586
587
/*
588
* Handle the H_COPY_TOFROM_GUEST hcall.
589
* r4 = L1 lpid of nested guest
590
* r5 = pid
591
* r6 = eaddr to access
592
* r7 = to buffer (L1 gpa)
593
* r8 = from buffer (L1 gpa)
594
* r9 = n bytes to copy
595
*/
596
long kvmhv_copy_tofrom_guest_nested(struct kvm_vcpu *vcpu)
597
{
598
struct kvm_nested_guest *gp;
599
int l1_lpid = kvmppc_get_gpr(vcpu, 4);
600
int pid = kvmppc_get_gpr(vcpu, 5);
601
gva_t eaddr = kvmppc_get_gpr(vcpu, 6);
602
gpa_t gp_to = (gpa_t) kvmppc_get_gpr(vcpu, 7);
603
gpa_t gp_from = (gpa_t) kvmppc_get_gpr(vcpu, 8);
604
void *buf;
605
unsigned long n = kvmppc_get_gpr(vcpu, 9);
606
bool is_load = !!gp_to;
607
long rc;
608
609
if (gp_to && gp_from) /* One must be NULL to determine the direction */
610
return H_PARAMETER;
611
612
if (eaddr & (0xFFFUL << 52))
613
return H_PARAMETER;
614
615
buf = kzalloc(n, GFP_KERNEL | __GFP_NOWARN);
616
if (!buf)
617
return H_NO_MEM;
618
619
gp = kvmhv_get_nested(vcpu->kvm, l1_lpid, false);
620
if (!gp) {
621
rc = H_PARAMETER;
622
goto out_free;
623
}
624
625
mutex_lock(&gp->tlb_lock);
626
627
if (is_load) {
628
/* Load from the nested guest into our buffer */
629
rc = __kvmhv_copy_tofrom_guest_radix(gp->shadow_lpid, pid,
630
eaddr, buf, NULL, n);
631
if (rc)
632
goto not_found;
633
634
/* Write what was loaded into our buffer back to the L1 guest */
635
kvm_vcpu_srcu_read_lock(vcpu);
636
rc = kvm_vcpu_write_guest(vcpu, gp_to, buf, n);
637
kvm_vcpu_srcu_read_unlock(vcpu);
638
if (rc)
639
goto not_found;
640
} else {
641
/* Load the data to be stored from the L1 guest into our buf */
642
kvm_vcpu_srcu_read_lock(vcpu);
643
rc = kvm_vcpu_read_guest(vcpu, gp_from, buf, n);
644
kvm_vcpu_srcu_read_unlock(vcpu);
645
if (rc)
646
goto not_found;
647
648
/* Store from our buffer into the nested guest */
649
rc = __kvmhv_copy_tofrom_guest_radix(gp->shadow_lpid, pid,
650
eaddr, NULL, buf, n);
651
if (rc)
652
goto not_found;
653
}
654
655
out_unlock:
656
mutex_unlock(&gp->tlb_lock);
657
kvmhv_put_nested(gp);
658
out_free:
659
kfree(buf);
660
return rc;
661
not_found:
662
rc = H_NOT_FOUND;
663
goto out_unlock;
664
}
665
666
/*
667
* Reload the partition table entry for a guest.
668
* Caller must hold gp->tlb_lock.
669
*/
670
static void kvmhv_update_ptbl_cache(struct kvm_nested_guest *gp)
671
{
672
int ret;
673
struct patb_entry ptbl_entry;
674
unsigned long ptbl_addr;
675
struct kvm *kvm = gp->l1_host;
676
677
ret = -EFAULT;
678
ptbl_addr = (kvm->arch.l1_ptcr & PRTB_MASK) + (gp->l1_lpid << 4);
679
if (gp->l1_lpid < (1ul << ((kvm->arch.l1_ptcr & PRTS_MASK) + 12 - 4))) {
680
int srcu_idx = srcu_read_lock(&kvm->srcu);
681
ret = kvm_read_guest(kvm, ptbl_addr,
682
&ptbl_entry, sizeof(ptbl_entry));
683
srcu_read_unlock(&kvm->srcu, srcu_idx);
684
}
685
if (ret) {
686
gp->l1_gr_to_hr = 0;
687
gp->process_table = 0;
688
} else {
689
gp->l1_gr_to_hr = be64_to_cpu(ptbl_entry.patb0);
690
gp->process_table = be64_to_cpu(ptbl_entry.patb1);
691
}
692
kvmhv_set_nested_ptbl(gp);
693
}
694
695
void kvmhv_vm_nested_init(struct kvm *kvm)
696
{
697
idr_init(&kvm->arch.kvm_nested_guest_idr);
698
}
699
700
static struct kvm_nested_guest *__find_nested(struct kvm *kvm, int lpid)
701
{
702
return idr_find(&kvm->arch.kvm_nested_guest_idr, lpid);
703
}
704
705
static bool __prealloc_nested(struct kvm *kvm, int lpid)
706
{
707
if (idr_alloc(&kvm->arch.kvm_nested_guest_idr,
708
NULL, lpid, lpid + 1, GFP_KERNEL) != lpid)
709
return false;
710
return true;
711
}
712
713
static void __add_nested(struct kvm *kvm, int lpid, struct kvm_nested_guest *gp)
714
{
715
if (idr_replace(&kvm->arch.kvm_nested_guest_idr, gp, lpid))
716
WARN_ON(1);
717
}
718
719
static void __remove_nested(struct kvm *kvm, int lpid)
720
{
721
idr_remove(&kvm->arch.kvm_nested_guest_idr, lpid);
722
}
723
724
static struct kvm_nested_guest *kvmhv_alloc_nested(struct kvm *kvm, unsigned int lpid)
725
{
726
struct kvm_nested_guest *gp;
727
long shadow_lpid;
728
729
gp = kzalloc(sizeof(*gp), GFP_KERNEL);
730
if (!gp)
731
return NULL;
732
gp->l1_host = kvm;
733
gp->l1_lpid = lpid;
734
mutex_init(&gp->tlb_lock);
735
gp->shadow_pgtable = pgd_alloc(kvm->mm);
736
if (!gp->shadow_pgtable)
737
goto out_free;
738
shadow_lpid = kvmppc_alloc_lpid();
739
if (shadow_lpid < 0)
740
goto out_free2;
741
gp->shadow_lpid = shadow_lpid;
742
gp->radix = 1;
743
744
memset(gp->prev_cpu, -1, sizeof(gp->prev_cpu));
745
746
return gp;
747
748
out_free2:
749
pgd_free(kvm->mm, gp->shadow_pgtable);
750
out_free:
751
kfree(gp);
752
return NULL;
753
}
754
755
/*
756
* Free up any resources allocated for a nested guest.
757
*/
758
static void kvmhv_release_nested(struct kvm_nested_guest *gp)
759
{
760
struct kvm *kvm = gp->l1_host;
761
762
if (gp->shadow_pgtable) {
763
/*
764
* No vcpu is using this struct and no call to
765
* kvmhv_get_nested can find this struct,
766
* so we don't need to hold kvm->mmu_lock.
767
*/
768
kvmppc_free_pgtable_radix(kvm, gp->shadow_pgtable,
769
gp->shadow_lpid);
770
pgd_free(kvm->mm, gp->shadow_pgtable);
771
}
772
kvmhv_set_ptbl_entry(gp->shadow_lpid, 0, 0);
773
kvmppc_free_lpid(gp->shadow_lpid);
774
kfree(gp);
775
}
776
777
static void kvmhv_remove_nested(struct kvm_nested_guest *gp)
778
{
779
struct kvm *kvm = gp->l1_host;
780
int lpid = gp->l1_lpid;
781
long ref;
782
783
spin_lock(&kvm->mmu_lock);
784
if (gp == __find_nested(kvm, lpid)) {
785
__remove_nested(kvm, lpid);
786
--gp->refcnt;
787
}
788
ref = gp->refcnt;
789
spin_unlock(&kvm->mmu_lock);
790
if (ref == 0)
791
kvmhv_release_nested(gp);
792
}
793
794
/*
795
* Free up all nested resources allocated for this guest.
796
* This is called with no vcpus of the guest running, when
797
* switching the guest to HPT mode or when destroying the
798
* guest.
799
*/
800
void kvmhv_release_all_nested(struct kvm *kvm)
801
{
802
int lpid;
803
struct kvm_nested_guest *gp;
804
struct kvm_nested_guest *freelist = NULL;
805
struct kvm_memory_slot *memslot;
806
int srcu_idx, bkt;
807
808
spin_lock(&kvm->mmu_lock);
809
idr_for_each_entry(&kvm->arch.kvm_nested_guest_idr, gp, lpid) {
810
__remove_nested(kvm, lpid);
811
if (--gp->refcnt == 0) {
812
gp->next = freelist;
813
freelist = gp;
814
}
815
}
816
idr_destroy(&kvm->arch.kvm_nested_guest_idr);
817
/* idr is empty and may be reused at this point */
818
spin_unlock(&kvm->mmu_lock);
819
while ((gp = freelist) != NULL) {
820
freelist = gp->next;
821
kvmhv_release_nested(gp);
822
}
823
824
srcu_idx = srcu_read_lock(&kvm->srcu);
825
kvm_for_each_memslot(memslot, bkt, kvm_memslots(kvm))
826
kvmhv_free_memslot_nest_rmap(memslot);
827
srcu_read_unlock(&kvm->srcu, srcu_idx);
828
}
829
830
/* caller must hold gp->tlb_lock */
831
static void kvmhv_flush_nested(struct kvm_nested_guest *gp)
832
{
833
struct kvm *kvm = gp->l1_host;
834
835
spin_lock(&kvm->mmu_lock);
836
kvmppc_free_pgtable_radix(kvm, gp->shadow_pgtable, gp->shadow_lpid);
837
spin_unlock(&kvm->mmu_lock);
838
kvmhv_flush_lpid(gp->shadow_lpid);
839
kvmhv_update_ptbl_cache(gp);
840
if (gp->l1_gr_to_hr == 0)
841
kvmhv_remove_nested(gp);
842
}
843
844
struct kvm_nested_guest *kvmhv_get_nested(struct kvm *kvm, int l1_lpid,
845
bool create)
846
{
847
struct kvm_nested_guest *gp, *newgp;
848
849
if (l1_lpid >= (1ul << ((kvm->arch.l1_ptcr & PRTS_MASK) + 12 - 4)))
850
return NULL;
851
852
spin_lock(&kvm->mmu_lock);
853
gp = __find_nested(kvm, l1_lpid);
854
if (gp)
855
++gp->refcnt;
856
spin_unlock(&kvm->mmu_lock);
857
858
if (gp || !create)
859
return gp;
860
861
newgp = kvmhv_alloc_nested(kvm, l1_lpid);
862
if (!newgp)
863
return NULL;
864
865
if (!__prealloc_nested(kvm, l1_lpid)) {
866
kvmhv_release_nested(newgp);
867
return NULL;
868
}
869
870
spin_lock(&kvm->mmu_lock);
871
gp = __find_nested(kvm, l1_lpid);
872
if (!gp) {
873
__add_nested(kvm, l1_lpid, newgp);
874
++newgp->refcnt;
875
gp = newgp;
876
newgp = NULL;
877
}
878
++gp->refcnt;
879
spin_unlock(&kvm->mmu_lock);
880
881
if (newgp)
882
kvmhv_release_nested(newgp);
883
884
return gp;
885
}
886
887
void kvmhv_put_nested(struct kvm_nested_guest *gp)
888
{
889
struct kvm *kvm = gp->l1_host;
890
long ref;
891
892
spin_lock(&kvm->mmu_lock);
893
ref = --gp->refcnt;
894
spin_unlock(&kvm->mmu_lock);
895
if (ref == 0)
896
kvmhv_release_nested(gp);
897
}
898
899
pte_t *find_kvm_nested_guest_pte(struct kvm *kvm, unsigned long lpid,
900
unsigned long ea, unsigned *hshift)
901
{
902
struct kvm_nested_guest *gp;
903
pte_t *pte;
904
905
gp = __find_nested(kvm, lpid);
906
if (!gp)
907
return NULL;
908
909
VM_WARN(!spin_is_locked(&kvm->mmu_lock),
910
"%s called with kvm mmu_lock not held \n", __func__);
911
pte = __find_linux_pte(gp->shadow_pgtable, ea, NULL, hshift);
912
913
return pte;
914
}
915
916
static inline bool kvmhv_n_rmap_is_equal(u64 rmap_1, u64 rmap_2)
917
{
918
return !((rmap_1 ^ rmap_2) & (RMAP_NESTED_LPID_MASK |
919
RMAP_NESTED_GPA_MASK));
920
}
921
922
void kvmhv_insert_nest_rmap(struct kvm *kvm, unsigned long *rmapp,
923
struct rmap_nested **n_rmap)
924
{
925
struct llist_node *entry = ((struct llist_head *) rmapp)->first;
926
struct rmap_nested *cursor;
927
u64 rmap, new_rmap = (*n_rmap)->rmap;
928
929
/* Are there any existing entries? */
930
if (!(*rmapp)) {
931
/* No -> use the rmap as a single entry */
932
*rmapp = new_rmap | RMAP_NESTED_IS_SINGLE_ENTRY;
933
return;
934
}
935
936
/* Do any entries match what we're trying to insert? */
937
for_each_nest_rmap_safe(cursor, entry, &rmap) {
938
if (kvmhv_n_rmap_is_equal(rmap, new_rmap))
939
return;
940
}
941
942
/* Do we need to create a list or just add the new entry? */
943
rmap = *rmapp;
944
if (rmap & RMAP_NESTED_IS_SINGLE_ENTRY) /* Not previously a list */
945
*rmapp = 0UL;
946
llist_add(&((*n_rmap)->list), (struct llist_head *) rmapp);
947
if (rmap & RMAP_NESTED_IS_SINGLE_ENTRY) /* Not previously a list */
948
(*n_rmap)->list.next = (struct llist_node *) rmap;
949
950
/* Set NULL so not freed by caller */
951
*n_rmap = NULL;
952
}
953
954
static void kvmhv_update_nest_rmap_rc(struct kvm *kvm, u64 n_rmap,
955
unsigned long clr, unsigned long set,
956
unsigned long hpa, unsigned long mask)
957
{
958
unsigned long gpa;
959
unsigned int shift, lpid;
960
pte_t *ptep;
961
962
gpa = n_rmap & RMAP_NESTED_GPA_MASK;
963
lpid = (n_rmap & RMAP_NESTED_LPID_MASK) >> RMAP_NESTED_LPID_SHIFT;
964
965
/* Find the pte */
966
ptep = find_kvm_nested_guest_pte(kvm, lpid, gpa, &shift);
967
/*
968
* If the pte is present and the pfn is still the same, update the pte.
969
* If the pfn has changed then this is a stale rmap entry, the nested
970
* gpa actually points somewhere else now, and there is nothing to do.
971
* XXX A future optimisation would be to remove the rmap entry here.
972
*/
973
if (ptep && pte_present(*ptep) && ((pte_val(*ptep) & mask) == hpa)) {
974
__radix_pte_update(ptep, clr, set);
975
kvmppc_radix_tlbie_page(kvm, gpa, shift, lpid);
976
}
977
}
978
979
/*
980
* For a given list of rmap entries, update the rc bits in all ptes in shadow
981
* page tables for nested guests which are referenced by the rmap list.
982
*/
983
void kvmhv_update_nest_rmap_rc_list(struct kvm *kvm, unsigned long *rmapp,
984
unsigned long clr, unsigned long set,
985
unsigned long hpa, unsigned long nbytes)
986
{
987
struct llist_node *entry = ((struct llist_head *) rmapp)->first;
988
struct rmap_nested *cursor;
989
unsigned long rmap, mask;
990
991
if ((clr | set) & ~(_PAGE_DIRTY | _PAGE_ACCESSED))
992
return;
993
994
mask = PTE_RPN_MASK & ~(nbytes - 1);
995
hpa &= mask;
996
997
for_each_nest_rmap_safe(cursor, entry, &rmap)
998
kvmhv_update_nest_rmap_rc(kvm, rmap, clr, set, hpa, mask);
999
}
1000
1001
static void kvmhv_remove_nest_rmap(struct kvm *kvm, u64 n_rmap,
1002
unsigned long hpa, unsigned long mask)
1003
{
1004
struct kvm_nested_guest *gp;
1005
unsigned long gpa;
1006
unsigned int shift, lpid;
1007
pte_t *ptep;
1008
1009
gpa = n_rmap & RMAP_NESTED_GPA_MASK;
1010
lpid = (n_rmap & RMAP_NESTED_LPID_MASK) >> RMAP_NESTED_LPID_SHIFT;
1011
gp = __find_nested(kvm, lpid);
1012
if (!gp)
1013
return;
1014
1015
/* Find and invalidate the pte */
1016
ptep = find_kvm_nested_guest_pte(kvm, lpid, gpa, &shift);
1017
/* Don't spuriously invalidate ptes if the pfn has changed */
1018
if (ptep && pte_present(*ptep) && ((pte_val(*ptep) & mask) == hpa))
1019
kvmppc_unmap_pte(kvm, ptep, gpa, shift, NULL, gp->shadow_lpid);
1020
}
1021
1022
static void kvmhv_remove_nest_rmap_list(struct kvm *kvm, unsigned long *rmapp,
1023
unsigned long hpa, unsigned long mask)
1024
{
1025
struct llist_node *entry = llist_del_all((struct llist_head *) rmapp);
1026
struct rmap_nested *cursor;
1027
unsigned long rmap;
1028
1029
for_each_nest_rmap_safe(cursor, entry, &rmap) {
1030
kvmhv_remove_nest_rmap(kvm, rmap, hpa, mask);
1031
kfree(cursor);
1032
}
1033
}
1034
1035
/* called with kvm->mmu_lock held */
1036
void kvmhv_remove_nest_rmap_range(struct kvm *kvm,
1037
const struct kvm_memory_slot *memslot,
1038
unsigned long gpa, unsigned long hpa,
1039
unsigned long nbytes)
1040
{
1041
unsigned long gfn, end_gfn;
1042
unsigned long addr_mask;
1043
1044
if (!memslot)
1045
return;
1046
gfn = (gpa >> PAGE_SHIFT) - memslot->base_gfn;
1047
end_gfn = gfn + (nbytes >> PAGE_SHIFT);
1048
1049
addr_mask = PTE_RPN_MASK & ~(nbytes - 1);
1050
hpa &= addr_mask;
1051
1052
for (; gfn < end_gfn; gfn++) {
1053
unsigned long *rmap = &memslot->arch.rmap[gfn];
1054
kvmhv_remove_nest_rmap_list(kvm, rmap, hpa, addr_mask);
1055
}
1056
}
1057
1058
static void kvmhv_free_memslot_nest_rmap(struct kvm_memory_slot *free)
1059
{
1060
unsigned long page;
1061
1062
for (page = 0; page < free->npages; page++) {
1063
unsigned long rmap, *rmapp = &free->arch.rmap[page];
1064
struct rmap_nested *cursor;
1065
struct llist_node *entry;
1066
1067
entry = llist_del_all((struct llist_head *) rmapp);
1068
for_each_nest_rmap_safe(cursor, entry, &rmap)
1069
kfree(cursor);
1070
}
1071
}
1072
1073
static bool kvmhv_invalidate_shadow_pte(struct kvm_vcpu *vcpu,
1074
struct kvm_nested_guest *gp,
1075
long gpa, int *shift_ret)
1076
{
1077
struct kvm *kvm = vcpu->kvm;
1078
bool ret = false;
1079
pte_t *ptep;
1080
int shift;
1081
1082
spin_lock(&kvm->mmu_lock);
1083
ptep = find_kvm_nested_guest_pte(kvm, gp->l1_lpid, gpa, &shift);
1084
if (!shift)
1085
shift = PAGE_SHIFT;
1086
if (ptep && pte_present(*ptep)) {
1087
kvmppc_unmap_pte(kvm, ptep, gpa, shift, NULL, gp->shadow_lpid);
1088
ret = true;
1089
}
1090
spin_unlock(&kvm->mmu_lock);
1091
1092
if (shift_ret)
1093
*shift_ret = shift;
1094
return ret;
1095
}
1096
1097
static inline int get_ric(unsigned int instr)
1098
{
1099
return (instr >> 18) & 0x3;
1100
}
1101
1102
static inline int get_prs(unsigned int instr)
1103
{
1104
return (instr >> 17) & 0x1;
1105
}
1106
1107
static inline int get_r(unsigned int instr)
1108
{
1109
return (instr >> 16) & 0x1;
1110
}
1111
1112
static inline int get_lpid(unsigned long r_val)
1113
{
1114
return r_val & 0xffffffff;
1115
}
1116
1117
static inline int get_is(unsigned long r_val)
1118
{
1119
return (r_val >> 10) & 0x3;
1120
}
1121
1122
static inline int get_ap(unsigned long r_val)
1123
{
1124
return (r_val >> 5) & 0x7;
1125
}
1126
1127
static inline long get_epn(unsigned long r_val)
1128
{
1129
return r_val >> 12;
1130
}
1131
1132
static int kvmhv_emulate_tlbie_tlb_addr(struct kvm_vcpu *vcpu, int lpid,
1133
int ap, long epn)
1134
{
1135
struct kvm *kvm = vcpu->kvm;
1136
struct kvm_nested_guest *gp;
1137
long npages;
1138
int shift, shadow_shift;
1139
unsigned long addr;
1140
1141
shift = ap_to_shift(ap);
1142
addr = epn << 12;
1143
if (shift < 0)
1144
/* Invalid ap encoding */
1145
return -EINVAL;
1146
1147
addr &= ~((1UL << shift) - 1);
1148
npages = 1UL << (shift - PAGE_SHIFT);
1149
1150
gp = kvmhv_get_nested(kvm, lpid, false);
1151
if (!gp) /* No such guest -> nothing to do */
1152
return 0;
1153
mutex_lock(&gp->tlb_lock);
1154
1155
/* There may be more than one host page backing this single guest pte */
1156
do {
1157
kvmhv_invalidate_shadow_pte(vcpu, gp, addr, &shadow_shift);
1158
1159
npages -= 1UL << (shadow_shift - PAGE_SHIFT);
1160
addr += 1UL << shadow_shift;
1161
} while (npages > 0);
1162
1163
mutex_unlock(&gp->tlb_lock);
1164
kvmhv_put_nested(gp);
1165
return 0;
1166
}
1167
1168
static void kvmhv_emulate_tlbie_lpid(struct kvm_vcpu *vcpu,
1169
struct kvm_nested_guest *gp, int ric)
1170
{
1171
struct kvm *kvm = vcpu->kvm;
1172
1173
mutex_lock(&gp->tlb_lock);
1174
switch (ric) {
1175
case 0:
1176
/* Invalidate TLB */
1177
spin_lock(&kvm->mmu_lock);
1178
kvmppc_free_pgtable_radix(kvm, gp->shadow_pgtable,
1179
gp->shadow_lpid);
1180
kvmhv_flush_lpid(gp->shadow_lpid);
1181
spin_unlock(&kvm->mmu_lock);
1182
break;
1183
case 1:
1184
/*
1185
* Invalidate PWC
1186
* We don't cache this -> nothing to do
1187
*/
1188
break;
1189
case 2:
1190
/* Invalidate TLB, PWC and caching of partition table entries */
1191
kvmhv_flush_nested(gp);
1192
break;
1193
default:
1194
break;
1195
}
1196
mutex_unlock(&gp->tlb_lock);
1197
}
1198
1199
static void kvmhv_emulate_tlbie_all_lpid(struct kvm_vcpu *vcpu, int ric)
1200
{
1201
struct kvm *kvm = vcpu->kvm;
1202
struct kvm_nested_guest *gp;
1203
int lpid;
1204
1205
spin_lock(&kvm->mmu_lock);
1206
idr_for_each_entry(&kvm->arch.kvm_nested_guest_idr, gp, lpid) {
1207
spin_unlock(&kvm->mmu_lock);
1208
kvmhv_emulate_tlbie_lpid(vcpu, gp, ric);
1209
spin_lock(&kvm->mmu_lock);
1210
}
1211
spin_unlock(&kvm->mmu_lock);
1212
}
1213
1214
static int kvmhv_emulate_priv_tlbie(struct kvm_vcpu *vcpu, unsigned int instr,
1215
unsigned long rsval, unsigned long rbval)
1216
{
1217
struct kvm *kvm = vcpu->kvm;
1218
struct kvm_nested_guest *gp;
1219
int r, ric, prs, is, ap;
1220
int lpid;
1221
long epn;
1222
int ret = 0;
1223
1224
ric = get_ric(instr);
1225
prs = get_prs(instr);
1226
r = get_r(instr);
1227
lpid = get_lpid(rsval);
1228
is = get_is(rbval);
1229
1230
/*
1231
* These cases are invalid and are not handled:
1232
* r != 1 -> Only radix supported
1233
* prs == 1 -> Not HV privileged
1234
* ric == 3 -> No cluster bombs for radix
1235
* is == 1 -> Partition scoped translations not associated with pid
1236
* (!is) && (ric == 1 || ric == 2) -> Not supported by ISA
1237
*/
1238
if ((!r) || (prs) || (ric == 3) || (is == 1) ||
1239
((!is) && (ric == 1 || ric == 2)))
1240
return -EINVAL;
1241
1242
switch (is) {
1243
case 0:
1244
/*
1245
* We know ric == 0
1246
* Invalidate TLB for a given target address
1247
*/
1248
epn = get_epn(rbval);
1249
ap = get_ap(rbval);
1250
ret = kvmhv_emulate_tlbie_tlb_addr(vcpu, lpid, ap, epn);
1251
break;
1252
case 2:
1253
/* Invalidate matching LPID */
1254
gp = kvmhv_get_nested(kvm, lpid, false);
1255
if (gp) {
1256
kvmhv_emulate_tlbie_lpid(vcpu, gp, ric);
1257
kvmhv_put_nested(gp);
1258
}
1259
break;
1260
case 3:
1261
/* Invalidate ALL LPIDs */
1262
kvmhv_emulate_tlbie_all_lpid(vcpu, ric);
1263
break;
1264
default:
1265
ret = -EINVAL;
1266
break;
1267
}
1268
1269
return ret;
1270
}
1271
1272
/*
1273
* This handles the H_TLB_INVALIDATE hcall.
1274
* Parameters are (r4) tlbie instruction code, (r5) rS contents,
1275
* (r6) rB contents.
1276
*/
1277
long kvmhv_do_nested_tlbie(struct kvm_vcpu *vcpu)
1278
{
1279
int ret;
1280
1281
ret = kvmhv_emulate_priv_tlbie(vcpu, kvmppc_get_gpr(vcpu, 4),
1282
kvmppc_get_gpr(vcpu, 5), kvmppc_get_gpr(vcpu, 6));
1283
if (ret)
1284
return H_PARAMETER;
1285
return H_SUCCESS;
1286
}
1287
1288
static long do_tlb_invalidate_nested_all(struct kvm_vcpu *vcpu,
1289
unsigned long lpid, unsigned long ric)
1290
{
1291
struct kvm *kvm = vcpu->kvm;
1292
struct kvm_nested_guest *gp;
1293
1294
gp = kvmhv_get_nested(kvm, lpid, false);
1295
if (gp) {
1296
kvmhv_emulate_tlbie_lpid(vcpu, gp, ric);
1297
kvmhv_put_nested(gp);
1298
}
1299
return H_SUCCESS;
1300
}
1301
1302
/*
1303
* Number of pages above which we invalidate the entire LPID rather than
1304
* flush individual pages.
1305
*/
1306
static unsigned long tlb_range_flush_page_ceiling __read_mostly = 33;
1307
1308
static long do_tlb_invalidate_nested_tlb(struct kvm_vcpu *vcpu,
1309
unsigned long lpid,
1310
unsigned long pg_sizes,
1311
unsigned long start,
1312
unsigned long end)
1313
{
1314
int ret = H_P4;
1315
unsigned long addr, nr_pages;
1316
struct mmu_psize_def *def;
1317
unsigned long psize, ap, page_size;
1318
bool flush_lpid;
1319
1320
for (psize = 0; psize < MMU_PAGE_COUNT; psize++) {
1321
def = &mmu_psize_defs[psize];
1322
if (!(pg_sizes & def->h_rpt_pgsize))
1323
continue;
1324
1325
nr_pages = (end - start) >> def->shift;
1326
flush_lpid = nr_pages > tlb_range_flush_page_ceiling;
1327
if (flush_lpid)
1328
return do_tlb_invalidate_nested_all(vcpu, lpid,
1329
RIC_FLUSH_TLB);
1330
addr = start;
1331
ap = mmu_get_ap(psize);
1332
page_size = 1UL << def->shift;
1333
do {
1334
ret = kvmhv_emulate_tlbie_tlb_addr(vcpu, lpid, ap,
1335
get_epn(addr));
1336
if (ret)
1337
return H_P4;
1338
addr += page_size;
1339
} while (addr < end);
1340
}
1341
return ret;
1342
}
1343
1344
/*
1345
* Performs partition-scoped invalidations for nested guests
1346
* as part of H_RPT_INVALIDATE hcall.
1347
*/
1348
long do_h_rpt_invalidate_pat(struct kvm_vcpu *vcpu, unsigned long lpid,
1349
unsigned long type, unsigned long pg_sizes,
1350
unsigned long start, unsigned long end)
1351
{
1352
/*
1353
* If L2 lpid isn't valid, we need to return H_PARAMETER.
1354
*
1355
* However, nested KVM issues a L2 lpid flush call when creating
1356
* partition table entries for L2. This happens even before the
1357
* corresponding shadow lpid is created in HV which happens in
1358
* H_ENTER_NESTED call. Since we can't differentiate this case from
1359
* the invalid case, we ignore such flush requests and return success.
1360
*/
1361
if (!__find_nested(vcpu->kvm, lpid))
1362
return H_SUCCESS;
1363
1364
/*
1365
* A flush all request can be handled by a full lpid flush only.
1366
*/
1367
if ((type & H_RPTI_TYPE_NESTED_ALL) == H_RPTI_TYPE_NESTED_ALL)
1368
return do_tlb_invalidate_nested_all(vcpu, lpid, RIC_FLUSH_ALL);
1369
1370
/*
1371
* We don't need to handle a PWC flush like process table here,
1372
* because intermediate partition scoped table in nested guest doesn't
1373
* really have PWC. Only level we have PWC is in L0 and for nested
1374
* invalidate at L0 we always do kvm_flush_lpid() which does
1375
* radix__flush_all_lpid(). For range invalidate at any level, we
1376
* are not removing the higher level page tables and hence there is
1377
* no PWC invalidate needed.
1378
*
1379
* if (type & H_RPTI_TYPE_PWC) {
1380
* ret = do_tlb_invalidate_nested_all(vcpu, lpid, RIC_FLUSH_PWC);
1381
* if (ret)
1382
* return H_P4;
1383
* }
1384
*/
1385
1386
if (start == 0 && end == -1)
1387
return do_tlb_invalidate_nested_all(vcpu, lpid, RIC_FLUSH_TLB);
1388
1389
if (type & H_RPTI_TYPE_TLB)
1390
return do_tlb_invalidate_nested_tlb(vcpu, lpid, pg_sizes,
1391
start, end);
1392
return H_SUCCESS;
1393
}
1394
1395
/* Used to convert a nested guest real address to a L1 guest real address */
1396
static int kvmhv_translate_addr_nested(struct kvm_vcpu *vcpu,
1397
struct kvm_nested_guest *gp,
1398
unsigned long n_gpa, unsigned long dsisr,
1399
struct kvmppc_pte *gpte_p)
1400
{
1401
u64 fault_addr, flags = dsisr & DSISR_ISSTORE;
1402
int ret;
1403
1404
ret = kvmppc_mmu_walk_radix_tree(vcpu, n_gpa, gpte_p, gp->l1_gr_to_hr,
1405
&fault_addr);
1406
1407
if (ret) {
1408
/* We didn't find a pte */
1409
if (ret == -EINVAL) {
1410
/* Unsupported mmu config */
1411
flags |= DSISR_UNSUPP_MMU;
1412
} else if (ret == -ENOENT) {
1413
/* No translation found */
1414
flags |= DSISR_NOHPTE;
1415
} else if (ret == -EFAULT) {
1416
/* Couldn't access L1 real address */
1417
flags |= DSISR_PRTABLE_FAULT;
1418
vcpu->arch.fault_gpa = fault_addr;
1419
} else {
1420
/* Unknown error */
1421
return ret;
1422
}
1423
goto forward_to_l1;
1424
} else {
1425
/* We found a pte -> check permissions */
1426
if (dsisr & DSISR_ISSTORE) {
1427
/* Can we write? */
1428
if (!gpte_p->may_write) {
1429
flags |= DSISR_PROTFAULT;
1430
goto forward_to_l1;
1431
}
1432
} else if (vcpu->arch.trap == BOOK3S_INTERRUPT_H_INST_STORAGE) {
1433
/* Can we execute? */
1434
if (!gpte_p->may_execute) {
1435
flags |= SRR1_ISI_N_G_OR_CIP;
1436
goto forward_to_l1;
1437
}
1438
} else {
1439
/* Can we read? */
1440
if (!gpte_p->may_read && !gpte_p->may_write) {
1441
flags |= DSISR_PROTFAULT;
1442
goto forward_to_l1;
1443
}
1444
}
1445
}
1446
1447
return 0;
1448
1449
forward_to_l1:
1450
vcpu->arch.fault_dsisr = flags;
1451
if (vcpu->arch.trap == BOOK3S_INTERRUPT_H_INST_STORAGE) {
1452
vcpu->arch.shregs.msr &= SRR1_MSR_BITS;
1453
vcpu->arch.shregs.msr |= flags;
1454
}
1455
return RESUME_HOST;
1456
}
1457
1458
static long kvmhv_handle_nested_set_rc(struct kvm_vcpu *vcpu,
1459
struct kvm_nested_guest *gp,
1460
unsigned long n_gpa,
1461
struct kvmppc_pte gpte,
1462
unsigned long dsisr)
1463
{
1464
struct kvm *kvm = vcpu->kvm;
1465
bool writing = !!(dsisr & DSISR_ISSTORE);
1466
u64 pgflags;
1467
long ret;
1468
1469
/* Are the rc bits set in the L1 partition scoped pte? */
1470
pgflags = _PAGE_ACCESSED;
1471
if (writing)
1472
pgflags |= _PAGE_DIRTY;
1473
if (pgflags & ~gpte.rc)
1474
return RESUME_HOST;
1475
1476
spin_lock(&kvm->mmu_lock);
1477
/* Set the rc bit in the pte of our (L0) pgtable for the L1 guest */
1478
ret = kvmppc_hv_handle_set_rc(kvm, false, writing,
1479
gpte.raddr, kvm->arch.lpid);
1480
if (!ret) {
1481
ret = -EINVAL;
1482
goto out_unlock;
1483
}
1484
1485
/* Set the rc bit in the pte of the shadow_pgtable for the nest guest */
1486
ret = kvmppc_hv_handle_set_rc(kvm, true, writing,
1487
n_gpa, gp->l1_lpid);
1488
if (!ret)
1489
ret = -EINVAL;
1490
else
1491
ret = 0;
1492
1493
out_unlock:
1494
spin_unlock(&kvm->mmu_lock);
1495
return ret;
1496
}
1497
1498
static inline int kvmppc_radix_level_to_shift(int level)
1499
{
1500
switch (level) {
1501
case 2:
1502
return PUD_SHIFT;
1503
case 1:
1504
return PMD_SHIFT;
1505
default:
1506
return PAGE_SHIFT;
1507
}
1508
}
1509
1510
static inline int kvmppc_radix_shift_to_level(int shift)
1511
{
1512
if (shift == PUD_SHIFT)
1513
return 2;
1514
if (shift == PMD_SHIFT)
1515
return 1;
1516
if (shift == PAGE_SHIFT)
1517
return 0;
1518
WARN_ON_ONCE(1);
1519
return 0;
1520
}
1521
1522
/* called with gp->tlb_lock held */
1523
static long int __kvmhv_nested_page_fault(struct kvm_vcpu *vcpu,
1524
struct kvm_nested_guest *gp)
1525
{
1526
struct kvm *kvm = vcpu->kvm;
1527
struct kvm_memory_slot *memslot;
1528
struct rmap_nested *n_rmap;
1529
struct kvmppc_pte gpte;
1530
pte_t pte, *pte_p;
1531
unsigned long mmu_seq;
1532
unsigned long dsisr = vcpu->arch.fault_dsisr;
1533
unsigned long ea = vcpu->arch.fault_dar;
1534
unsigned long *rmapp;
1535
unsigned long n_gpa, gpa, gfn, perm = 0UL;
1536
unsigned int shift, l1_shift, level;
1537
bool writing = !!(dsisr & DSISR_ISSTORE);
1538
long int ret;
1539
1540
if (!gp->l1_gr_to_hr) {
1541
kvmhv_update_ptbl_cache(gp);
1542
if (!gp->l1_gr_to_hr)
1543
return RESUME_HOST;
1544
}
1545
1546
/* Convert the nested guest real address into a L1 guest real address */
1547
1548
n_gpa = vcpu->arch.fault_gpa & ~0xF000000000000FFFULL;
1549
if (!(dsisr & DSISR_PRTABLE_FAULT))
1550
n_gpa |= ea & 0xFFF;
1551
ret = kvmhv_translate_addr_nested(vcpu, gp, n_gpa, dsisr, &gpte);
1552
1553
/*
1554
* If the hardware found a translation but we don't now have a usable
1555
* translation in the l1 partition-scoped tree, remove the shadow pte
1556
* and let the guest retry.
1557
*/
1558
if (ret == RESUME_HOST &&
1559
(dsisr & (DSISR_PROTFAULT | DSISR_BADACCESS | DSISR_NOEXEC_OR_G |
1560
DSISR_BAD_COPYPASTE)))
1561
goto inval;
1562
if (ret)
1563
return ret;
1564
1565
/* Failed to set the reference/change bits */
1566
if (dsisr & DSISR_SET_RC) {
1567
ret = kvmhv_handle_nested_set_rc(vcpu, gp, n_gpa, gpte, dsisr);
1568
if (ret == RESUME_HOST)
1569
return ret;
1570
if (ret)
1571
goto inval;
1572
dsisr &= ~DSISR_SET_RC;
1573
if (!(dsisr & (DSISR_BAD_FAULT_64S | DSISR_NOHPTE |
1574
DSISR_PROTFAULT)))
1575
return RESUME_GUEST;
1576
}
1577
1578
/*
1579
* We took an HISI or HDSI while we were running a nested guest which
1580
* means we have no partition scoped translation for that. This means
1581
* we need to insert a pte for the mapping into our shadow_pgtable.
1582
*/
1583
1584
l1_shift = gpte.page_shift;
1585
if (l1_shift < PAGE_SHIFT) {
1586
/* We don't support l1 using a page size smaller than our own */
1587
pr_err("KVM: L1 guest page shift (%d) less than our own (%d)\n",
1588
l1_shift, PAGE_SHIFT);
1589
return -EINVAL;
1590
}
1591
gpa = gpte.raddr;
1592
gfn = gpa >> PAGE_SHIFT;
1593
1594
/* 1. Get the corresponding host memslot */
1595
1596
memslot = gfn_to_memslot(kvm, gfn);
1597
if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID)) {
1598
if (dsisr & (DSISR_PRTABLE_FAULT | DSISR_BADACCESS)) {
1599
/* unusual error -> reflect to the guest as a DSI */
1600
kvmppc_core_queue_data_storage(vcpu,
1601
kvmppc_get_msr(vcpu) & SRR1_PREFIXED,
1602
ea, dsisr);
1603
return RESUME_GUEST;
1604
}
1605
1606
/* passthrough of emulated MMIO case */
1607
return kvmppc_hv_emulate_mmio(vcpu, gpa, ea, writing);
1608
}
1609
if (memslot->flags & KVM_MEM_READONLY) {
1610
if (writing) {
1611
/* Give the guest a DSI */
1612
kvmppc_core_queue_data_storage(vcpu,
1613
kvmppc_get_msr(vcpu) & SRR1_PREFIXED,
1614
ea, DSISR_ISSTORE | DSISR_PROTFAULT);
1615
return RESUME_GUEST;
1616
}
1617
}
1618
1619
/* 2. Find the host pte for this L1 guest real address */
1620
1621
/* Used to check for invalidations in progress */
1622
mmu_seq = kvm->mmu_invalidate_seq;
1623
smp_rmb();
1624
1625
/* See if can find translation in our partition scoped tables for L1 */
1626
pte = __pte(0);
1627
spin_lock(&kvm->mmu_lock);
1628
pte_p = find_kvm_secondary_pte(kvm, gpa, &shift);
1629
if (!shift)
1630
shift = PAGE_SHIFT;
1631
if (pte_p)
1632
pte = *pte_p;
1633
spin_unlock(&kvm->mmu_lock);
1634
1635
if (!pte_present(pte) || (writing && !(pte_val(pte) & _PAGE_WRITE))) {
1636
/* No suitable pte found -> try to insert a mapping */
1637
ret = kvmppc_book3s_instantiate_page(vcpu, gpa, memslot,
1638
writing, &pte, &level);
1639
if (ret == -EAGAIN)
1640
return RESUME_GUEST;
1641
else if (ret)
1642
return ret;
1643
shift = kvmppc_radix_level_to_shift(level);
1644
}
1645
/* Align gfn to the start of the page */
1646
gfn = (gpa & ~((1UL << shift) - 1)) >> PAGE_SHIFT;
1647
1648
/* 3. Compute the pte we need to insert for nest_gpa -> host r_addr */
1649
1650
/* The permissions is the combination of the host and l1 guest ptes */
1651
perm |= gpte.may_read ? 0UL : _PAGE_READ;
1652
perm |= gpte.may_write ? 0UL : _PAGE_WRITE;
1653
perm |= gpte.may_execute ? 0UL : _PAGE_EXEC;
1654
/* Only set accessed/dirty (rc) bits if set in host and l1 guest ptes */
1655
perm |= (gpte.rc & _PAGE_ACCESSED) ? 0UL : _PAGE_ACCESSED;
1656
perm |= ((gpte.rc & _PAGE_DIRTY) && writing) ? 0UL : _PAGE_DIRTY;
1657
pte = __pte(pte_val(pte) & ~perm);
1658
1659
/* What size pte can we insert? */
1660
if (shift > l1_shift) {
1661
u64 mask;
1662
unsigned int actual_shift = PAGE_SHIFT;
1663
if (PMD_SHIFT < l1_shift)
1664
actual_shift = PMD_SHIFT;
1665
mask = (1UL << shift) - (1UL << actual_shift);
1666
pte = __pte(pte_val(pte) | (gpa & mask));
1667
shift = actual_shift;
1668
}
1669
level = kvmppc_radix_shift_to_level(shift);
1670
n_gpa &= ~((1UL << shift) - 1);
1671
1672
/* 4. Insert the pte into our shadow_pgtable */
1673
1674
n_rmap = kzalloc(sizeof(*n_rmap), GFP_KERNEL);
1675
if (!n_rmap)
1676
return RESUME_GUEST; /* Let the guest try again */
1677
n_rmap->rmap = (n_gpa & RMAP_NESTED_GPA_MASK) |
1678
(((unsigned long) gp->l1_lpid) << RMAP_NESTED_LPID_SHIFT);
1679
rmapp = &memslot->arch.rmap[gfn - memslot->base_gfn];
1680
ret = kvmppc_create_pte(kvm, gp->shadow_pgtable, pte, n_gpa, level,
1681
mmu_seq, gp->shadow_lpid, rmapp, &n_rmap);
1682
kfree(n_rmap);
1683
if (ret == -EAGAIN)
1684
ret = RESUME_GUEST; /* Let the guest try again */
1685
1686
return ret;
1687
1688
inval:
1689
kvmhv_invalidate_shadow_pte(vcpu, gp, n_gpa, NULL);
1690
return RESUME_GUEST;
1691
}
1692
1693
long int kvmhv_nested_page_fault(struct kvm_vcpu *vcpu)
1694
{
1695
struct kvm_nested_guest *gp = vcpu->arch.nested;
1696
long int ret;
1697
1698
mutex_lock(&gp->tlb_lock);
1699
ret = __kvmhv_nested_page_fault(vcpu, gp);
1700
mutex_unlock(&gp->tlb_lock);
1701
return ret;
1702
}
1703
1704
int kvmhv_nested_next_lpid(struct kvm *kvm, int lpid)
1705
{
1706
int ret = lpid + 1;
1707
1708
spin_lock(&kvm->mmu_lock);
1709
if (!idr_get_next(&kvm->arch.kvm_nested_guest_idr, &ret))
1710
ret = -1;
1711
spin_unlock(&kvm->mmu_lock);
1712
1713
return ret;
1714
}
1715
1716