Skip to content

Commit 699a0ea

Browse files
paulusmackagraf
authored andcommitted
KVM: PPC: Book3S: Controls for in-kernel sPAPR hypercall handling
This provides a way for userspace controls which sPAPR hcalls get handled in the kernel. Each hcall can be individually enabled or disabled for in-kernel handling, except for H_RTAS. The exception for H_RTAS is because userspace can already control whether individual RTAS functions are handled in-kernel or not via the KVM_PPC_RTAS_DEFINE_TOKEN ioctl, and because the numeric value for H_RTAS is out of the normal sequence of hcall numbers. Hcalls are enabled or disabled using the KVM_ENABLE_CAP ioctl for the KVM_CAP_PPC_ENABLE_HCALL capability on the file descriptor for the VM. The args field of the struct kvm_enable_cap specifies the hcall number in args[0] and the enable/disable flag in args[1]; 0 means disable in-kernel handling (so that the hcall will always cause an exit to userspace) and 1 means enable. Enabling or disabling in-kernel handling of an hcall is effective across the whole VM. The ability for KVM_ENABLE_CAP to be used on a VM file descriptor on PowerPC is new, added by this commit. The KVM_CAP_ENABLE_CAP_VM capability advertises that this ability exists. When a VM is created, an initial set of hcalls are enabled for in-kernel handling. The set that is enabled is the set that have an in-kernel implementation at this point. Any new hcall implementations from this point onwards should not be added to the default set without a good reason. No distinction is made between real-mode and virtual-mode hcall implementations; the one setting controls them both. Signed-off-by: Paul Mackerras <paulus@samba.org> Signed-off-by: Alexander Graf <agraf@suse.de>
1 parent 1f0eeb7 commit 699a0ea

10 files changed

Lines changed: 193 additions & 2 deletions

File tree

‎Documentation/virtual/kvm/api.txt‎

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2863,8 +2863,8 @@ The fields in each entry are defined as follows:
28632863
this function/index combination
28642864

28652865

2866-
6. Capabilities that can be enabled
2867-
-----------------------------------
2866+
6. Capabilities that can be enabled on vCPUs
2867+
--------------------------------------------
28682868

28692869
There are certain capabilities that change the behavior of the virtual CPU when
28702870
enabled. To enable them, please see section 4.37. Below you can find a list of
@@ -3002,3 +3002,40 @@ Parameters: args[0] is the XICS device fd
30023002
args[1] is the XICS CPU number (server ID) for this vcpu
30033003

30043004
This capability connects the vcpu to an in-kernel XICS device.
3005+
3006+
3007+
7. Capabilities that can be enabled on VMs
3008+
------------------------------------------
3009+
3010+
There are certain capabilities that change the behavior of the virtual
3011+
machine when enabled. To enable them, please see section 4.37. Below
3012+
you can find a list of capabilities and what their effect on the VM
3013+
is when enabling them.
3014+
3015+
The following information is provided along with the description:
3016+
3017+
Architectures: which instruction set architectures provide this ioctl.
3018+
x86 includes both i386 and x86_64.
3019+
3020+
Parameters: what parameters are accepted by the capability.
3021+
3022+
Returns: the return value. General error numbers (EBADF, ENOMEM, EINVAL)
3023+
are not detailed, but errors with specific meanings are.
3024+
3025+
3026+
7.1 KVM_CAP_PPC_ENABLE_HCALL
3027+
3028+
Architectures: ppc
3029+
Parameters: args[0] is the sPAPR hcall number
3030+
args[1] is 0 to disable, 1 to enable in-kernel handling
3031+
3032+
This capability controls whether individual sPAPR hypercalls (hcalls)
3033+
get handled by the kernel or not. Enabling or disabling in-kernel
3034+
handling of an hcall is effective across the VM. On creation, an
3035+
initial set of hcalls are enabled for in-kernel handling, which
3036+
consists of those hcalls for which in-kernel handlers were implemented
3037+
before this capability was implemented. If disabled, the kernel will
3038+
not to attempt to handle the hcall, but will always exit to userspace
3039+
to handle it. Note that it may not make sense to enable some and
3040+
disable others of a group of related hcalls, but KVM does not prevent
3041+
userspace from doing that.

‎arch/powerpc/include/asm/kvm_book3s.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ extern void kvmppc_hv_entry_trampoline(void);
187187
extern u32 kvmppc_alignment_dsisr(struct kvm_vcpu *vcpu, unsigned int inst);
188188
extern ulong kvmppc_alignment_dar(struct kvm_vcpu *vcpu, unsigned int inst);
189189
extern int kvmppc_h_pr(struct kvm_vcpu *vcpu, unsigned long cmd);
190+
extern void kvmppc_pr_init_default_hcalls(struct kvm *kvm);
190191
extern void kvmppc_copy_to_svcpu(struct kvmppc_book3s_shadow_vcpu *svcpu,
191192
struct kvm_vcpu *vcpu);
192193
extern void kvmppc_copy_from_svcpu(struct kvm_vcpu *vcpu,

‎arch/powerpc/include/asm/kvm_host.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <asm/processor.h>
3535
#include <asm/page.h>
3636
#include <asm/cacheflush.h>
37+
#include <asm/hvcall.h>
3738

3839
#define KVM_MAX_VCPUS NR_CPUS
3940
#define KVM_MAX_VCORES NR_CPUS
@@ -263,6 +264,7 @@ struct kvm_arch {
263264
#ifdef CONFIG_PPC_BOOK3S_64
264265
struct list_head spapr_tce_tables;
265266
struct list_head rtas_tokens;
267+
DECLARE_BITMAP(enabled_hcalls, MAX_HCALL_OPCODE/4 + 1);
266268
#endif
267269
#ifdef CONFIG_KVM_MPIC
268270
struct openpic *mpic;

‎arch/powerpc/kernel/asm-offsets.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,7 @@ int main(void)
493493
DEFINE(KVM_HOST_SDR1, offsetof(struct kvm, arch.host_sdr1));
494494
DEFINE(KVM_TLBIE_LOCK, offsetof(struct kvm, arch.tlbie_lock));
495495
DEFINE(KVM_NEED_FLUSH, offsetof(struct kvm, arch.need_tlb_flush.bits));
496+
DEFINE(KVM_ENABLED_HCALLS, offsetof(struct kvm, arch.enabled_hcalls));
496497
DEFINE(KVM_LPCR, offsetof(struct kvm, arch.lpcr));
497498
DEFINE(KVM_RMOR, offsetof(struct kvm, arch.rmor));
498499
DEFINE(KVM_VRMA_SLB_V, offsetof(struct kvm, arch.vrma_slb_v));

‎arch/powerpc/kvm/book3s_hv.c‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@
6767
/* Used as a "null" value for timebase values */
6868
#define TB_NIL (~(u64)0)
6969

70+
static DECLARE_BITMAP(default_enabled_hcalls, MAX_HCALL_OPCODE/4 + 1);
71+
7072
static void kvmppc_end_cede(struct kvm_vcpu *vcpu);
7173
static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu);
7274

@@ -562,6 +564,10 @@ int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
562564
struct kvm_vcpu *tvcpu;
563565
int idx, rc;
564566

567+
if (req <= MAX_HCALL_OPCODE &&
568+
!test_bit(req/4, vcpu->kvm->arch.enabled_hcalls))
569+
return RESUME_HOST;
570+
565571
switch (req) {
566572
case H_ENTER:
567573
idx = srcu_read_lock(&vcpu->kvm->srcu);
@@ -2269,6 +2275,10 @@ static int kvmppc_core_init_vm_hv(struct kvm *kvm)
22692275
*/
22702276
cpumask_setall(&kvm->arch.need_tlb_flush);
22712277

2278+
/* Start out with the default set of hcalls enabled */
2279+
memcpy(kvm->arch.enabled_hcalls, default_enabled_hcalls,
2280+
sizeof(kvm->arch.enabled_hcalls));
2281+
22722282
kvm->arch.rma = NULL;
22732283

22742284
kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
@@ -2407,6 +2417,45 @@ static long kvm_arch_vm_ioctl_hv(struct file *filp,
24072417
return r;
24082418
}
24092419

2420+
/*
2421+
* List of hcall numbers to enable by default.
2422+
* For compatibility with old userspace, we enable by default
2423+
* all hcalls that were implemented before the hcall-enabling
2424+
* facility was added. Note this list should not include H_RTAS.
2425+
*/
2426+
static unsigned int default_hcall_list[] = {
2427+
H_REMOVE,
2428+
H_ENTER,
2429+
H_READ,
2430+
H_PROTECT,
2431+
H_BULK_REMOVE,
2432+
H_GET_TCE,
2433+
H_PUT_TCE,
2434+
H_SET_DABR,
2435+
H_SET_XDABR,
2436+
H_CEDE,
2437+
H_PROD,
2438+
H_CONFER,
2439+
H_REGISTER_VPA,
2440+
#ifdef CONFIG_KVM_XICS
2441+
H_EOI,
2442+
H_CPPR,
2443+
H_IPI,
2444+
H_IPOLL,
2445+
H_XIRR,
2446+
H_XIRR_X,
2447+
#endif
2448+
0
2449+
};
2450+
2451+
static void init_default_hcalls(void)
2452+
{
2453+
int i;
2454+
2455+
for (i = 0; default_hcall_list[i]; ++i)
2456+
__set_bit(default_hcall_list[i] / 4, default_enabled_hcalls);
2457+
}
2458+
24102459
static struct kvmppc_ops kvm_ops_hv = {
24112460
.get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv,
24122461
.set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv,
@@ -2454,6 +2503,8 @@ static int kvmppc_book3s_init_hv(void)
24542503
kvm_ops_hv.owner = THIS_MODULE;
24552504
kvmppc_hv_ops = &kvm_ops_hv;
24562505

2506+
init_default_hcalls();
2507+
24572508
r = kvmppc_mmu_hv_init();
24582509
return r;
24592510
}

‎arch/powerpc/kvm/book3s_hv_rmhandlers.S‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1909,6 +1909,17 @@ hcall_try_real_mode:
19091909
clrrdi r3,r3,2
19101910
cmpldi r3,hcall_real_table_end - hcall_real_table
19111911
bge guest_exit_cont
1912+
/* See if this hcall is enabled for in-kernel handling */
1913+
ld r4, VCPU_KVM(r9)
1914+
srdi r0, r3, 8 /* r0 = (r3 / 4) >> 6 */
1915+
sldi r0, r0, 3 /* index into kvm->arch.enabled_hcalls[] */
1916+
add r4, r4, r0
1917+
ld r0, KVM_ENABLED_HCALLS(r4)
1918+
rlwinm r4, r3, 32-2, 0x3f /* r4 = (r3 / 4) & 0x3f */
1919+
srd r0, r0, r4
1920+
andi. r0, r0, 1
1921+
beq guest_exit_cont
1922+
/* Get pointer to handler, if any, and call it */
19121923
LOAD_REG_ADDR(r4, hcall_real_table)
19131924
lwax r3,r3,r4
19141925
cmpwi r3,0

‎arch/powerpc/kvm/book3s_pr.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1597,6 +1597,11 @@ static int kvmppc_core_init_vm_pr(struct kvm *kvm)
15971597
{
15981598
mutex_init(&kvm->arch.hpt_mutex);
15991599

1600+
#ifdef CONFIG_PPC_BOOK3S_64
1601+
/* Start out with the default set of hcalls enabled */
1602+
kvmppc_pr_init_default_hcalls(kvm);
1603+
#endif
1604+
16001605
if (firmware_has_feature(FW_FEATURE_SET_MODE)) {
16011606
spin_lock(&kvm_global_user_count_lock);
16021607
if (++kvm_global_user_count == 1)

‎arch/powerpc/kvm/book3s_pr_papr.c‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,10 @@ static int kvmppc_h_pr_xics_hcall(struct kvm_vcpu *vcpu, u32 cmd)
267267

268268
int kvmppc_h_pr(struct kvm_vcpu *vcpu, unsigned long cmd)
269269
{
270+
if (cmd <= MAX_HCALL_OPCODE &&
271+
!test_bit(cmd/4, vcpu->kvm->arch.enabled_hcalls))
272+
return EMULATE_FAIL;
273+
270274
switch (cmd) {
271275
case H_ENTER:
272276
return kvmppc_h_pr_enter(vcpu);
@@ -304,3 +308,36 @@ int kvmppc_h_pr(struct kvm_vcpu *vcpu, unsigned long cmd)
304308

305309
return EMULATE_FAIL;
306310
}
311+
312+
313+
/*
314+
* List of hcall numbers to enable by default.
315+
* For compatibility with old userspace, we enable by default
316+
* all hcalls that were implemented before the hcall-enabling
317+
* facility was added. Note this list should not include H_RTAS.
318+
*/
319+
static unsigned int default_hcall_list[] = {
320+
H_ENTER,
321+
H_REMOVE,
322+
H_PROTECT,
323+
H_BULK_REMOVE,
324+
H_PUT_TCE,
325+
H_CEDE,
326+
#ifdef CONFIG_KVM_XICS
327+
H_XIRR,
328+
H_CPPR,
329+
H_EOI,
330+
H_IPI,
331+
H_IPOLL,
332+
H_XIRR_X,
333+
#endif
334+
0
335+
};
336+
337+
void kvmppc_pr_init_default_hcalls(struct kvm *kvm)
338+
{
339+
int i;
340+
341+
for (i = 0; default_hcall_list[i]; ++i)
342+
__set_bit(default_hcall_list[i] / 4, kvm->arch.enabled_hcalls);
343+
}

‎arch/powerpc/kvm/powerpc.c‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ int kvm_dev_ioctl_check_extension(long ext)
387387
case KVM_CAP_PPC_UNSET_IRQ:
388388
case KVM_CAP_PPC_IRQ_LEVEL:
389389
case KVM_CAP_ENABLE_CAP:
390+
case KVM_CAP_ENABLE_CAP_VM:
390391
case KVM_CAP_ONE_REG:
391392
case KVM_CAP_IOEVENTFD:
392393
case KVM_CAP_DEVICE_CTRL:
@@ -417,6 +418,7 @@ int kvm_dev_ioctl_check_extension(long ext)
417418
case KVM_CAP_PPC_ALLOC_HTAB:
418419
case KVM_CAP_PPC_RTAS:
419420
case KVM_CAP_PPC_FIXUP_HCALL:
421+
case KVM_CAP_PPC_ENABLE_HCALL:
420422
#ifdef CONFIG_KVM_XICS
421423
case KVM_CAP_IRQ_XICS:
422424
#endif
@@ -1099,6 +1101,40 @@ int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event,
10991101
return 0;
11001102
}
11011103

1104+
1105+
static int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
1106+
struct kvm_enable_cap *cap)
1107+
{
1108+
int r;
1109+
1110+
if (cap->flags)
1111+
return -EINVAL;
1112+
1113+
switch (cap->cap) {
1114+
#ifdef CONFIG_KVM_BOOK3S_64_HANDLER
1115+
case KVM_CAP_PPC_ENABLE_HCALL: {
1116+
unsigned long hcall = cap->args[0];
1117+
1118+
r = -EINVAL;
1119+
if (hcall > MAX_HCALL_OPCODE || (hcall & 3) ||
1120+
cap->args[1] > 1)
1121+
break;
1122+
if (cap->args[1])
1123+
set_bit(hcall / 4, kvm->arch.enabled_hcalls);
1124+
else
1125+
clear_bit(hcall / 4, kvm->arch.enabled_hcalls);
1126+
r = 0;
1127+
break;
1128+
}
1129+
#endif
1130+
default:
1131+
r = -EINVAL;
1132+
break;
1133+
}
1134+
1135+
return r;
1136+
}
1137+
11021138
long kvm_arch_vm_ioctl(struct file *filp,
11031139
unsigned int ioctl, unsigned long arg)
11041140
{
@@ -1118,6 +1154,15 @@ long kvm_arch_vm_ioctl(struct file *filp,
11181154

11191155
break;
11201156
}
1157+
case KVM_ENABLE_CAP:
1158+
{
1159+
struct kvm_enable_cap cap;
1160+
r = -EFAULT;
1161+
if (copy_from_user(&cap, argp, sizeof(cap)))
1162+
goto out;
1163+
r = kvm_vm_ioctl_enable_cap(kvm, &cap);
1164+
break;
1165+
}
11211166
#ifdef CONFIG_PPC_BOOK3S_64
11221167
case KVM_CREATE_SPAPR_TCE: {
11231168
struct kvm_create_spapr_tce create_tce;

‎include/uapi/linux/kvm.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,7 @@ struct kvm_ppc_smmu_info {
758758
#define KVM_CAP_VM_ATTRIBUTES 101
759759
#define KVM_CAP_ARM_PSCI_0_2 102
760760
#define KVM_CAP_PPC_FIXUP_HCALL 103
761+
#define KVM_CAP_PPC_ENABLE_HCALL 104
761762

762763
#ifdef KVM_CAP_IRQ_ROUTING
763764

0 commit comments

Comments
 (0)