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.
Unified · Split
| @@ -2863,8 +2863,8 @@ The fields in each entry are defined as follows: · 2862 unmodified lines | ||
| 2863 | 2863 | this function/index combination |
| 2864 | 2864 | |
| 2865 | 2865 | |
| 2866 | −6. Capabilities that can be enabled | |
| 2867 | −----------------------------------- | |
| 2866 | +6. Capabilities that can be enabled on vCPUs | |
| 2867 | +-------------------------------------------- | |
| 2868 | 2868 | |
| 2869 | 2869 | There are certain capabilities that change the behavior of the virtual CPU when |
| 2870 | 2870 | 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 · 131 unmodified lines | ||
| 3002 | 3002 | args[1] is the XICS CPU number (server ID) for this vcpu |
| 3003 | 3003 | |
| 3004 | 3004 | 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. | |
| @@ -187,6 +187,7 @@ extern void kvmppc_hv_entry_trampoline(void); · 186 unmodified lines | ||
| 187 | 187 | extern u32 kvmppc_alignment_dsisr(struct kvm_vcpu *vcpu, unsigned int inst); |
| 188 | 188 | extern ulong kvmppc_alignment_dar(struct kvm_vcpu *vcpu, unsigned int inst); |
| 189 | 189 | extern int kvmppc_h_pr(struct kvm_vcpu *vcpu, unsigned long cmd); |
| 190 | +extern void kvmppc_pr_init_default_hcalls(struct kvm *kvm); | |
| 190 | 191 | extern void kvmppc_copy_to_svcpu(struct kvmppc_book3s_shadow_vcpu *svcpu, |
| 191 | 192 | struct kvm_vcpu *vcpu); |
| 192 | 193 | extern void kvmppc_copy_from_svcpu(struct kvm_vcpu *vcpu, |
| @@ -34,6 +34,7 @@ · 33 unmodified lines | ||
| 34 | 34 | #include <asm/processor.h> |
| 35 | 35 | #include <asm/page.h> |
| 36 | 36 | #include <asm/cacheflush.h> |
| 37 | +#include <asm/hvcall.h> | |
| 37 | 38 | |
| 38 | 39 | #define KVM_MAX_VCPUS NR_CPUS |
| 39 | 40 | #define KVM_MAX_VCORES NR_CPUS |
| @@ -263,6 +264,7 @@ struct kvm_arch { · 223 unmodified lines | ||
| 263 | 264 | #ifdef CONFIG_PPC_BOOK3S_64 |
| 264 | 265 | struct list_head spapr_tce_tables; |
| 265 | 266 | struct list_head rtas_tokens; |
| 267 | + DECLARE_BITMAP(enabled_hcalls, MAX_HCALL_OPCODE/4 + 1); | |
| 266 | 268 | #endif |
| 267 | 269 | #ifdef CONFIG_KVM_MPIC |
| 268 | 270 | struct openpic *mpic; |
| @@ -493,6 +493,7 @@ int main(void) · 492 unmodified lines | ||
| 493 | 493 | DEFINE(KVM_HOST_SDR1, offsetof(struct kvm, arch.host_sdr1)); |
| 494 | 494 | DEFINE(KVM_TLBIE_LOCK, offsetof(struct kvm, arch.tlbie_lock)); |
| 495 | 495 | DEFINE(KVM_NEED_FLUSH, offsetof(struct kvm, arch.need_tlb_flush.bits)); |
| 496 | + DEFINE(KVM_ENABLED_HCALLS, offsetof(struct kvm, arch.enabled_hcalls)); | |
| 496 | 497 | DEFINE(KVM_LPCR, offsetof(struct kvm, arch.lpcr)); |
| 497 | 498 | DEFINE(KVM_RMOR, offsetof(struct kvm, arch.rmor)); |
| 498 | 499 | DEFINE(KVM_VRMA_SLB_V, offsetof(struct kvm, arch.vrma_slb_v)); |
| @@ -67,6 +67,8 @@ · 66 unmodified lines | ||
| 67 | 67 | /* Used as a "null" value for timebase values */ |
| 68 | 68 | #define TB_NIL (~(u64)0) |
| 69 | 69 | |
| 70 | +static DECLARE_BITMAP(default_enabled_hcalls, MAX_HCALL_OPCODE/4 + 1); | |
| 71 | + | |
| 70 | 72 | static void kvmppc_end_cede(struct kvm_vcpu *vcpu); |
| 71 | 73 | static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu); |
| 72 | 74 | |
| @@ -562,6 +564,10 @@ int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu) · 489 unmodified lines | ||
| 562 | 564 | struct kvm_vcpu *tvcpu; |
| 563 | 565 | int idx, rc; |
| 564 | 566 | |
| 567 | + if (req <= MAX_HCALL_OPCODE && | |
| 568 | + !test_bit(req/4, vcpu->kvm->arch.enabled_hcalls)) | |
| 569 | + return RESUME_HOST; | |
| 570 | + | |
| 565 | 571 | switch (req) { |
| 566 | 572 | case H_ENTER: |
| 567 | 573 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
| @@ -2269,6 +2275,10 @@ static int kvmppc_core_init_vm_hv(struct kvm *kvm) · 1701 unmodified lines | ||
| 2269 | 2275 | */ |
| 2270 | 2276 | cpumask_setall(&kvm->arch.need_tlb_flush); |
| 2271 | 2277 | |
| 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 | + | |
| 2272 | 2282 | kvm->arch.rma = NULL; |
| 2273 | 2283 | |
| 2274 | 2284 | kvm->arch.host_sdr1 = mfspr(SPRN_SDR1); |
| @@ -2407,6 +2417,45 @@ static long kvm_arch_vm_ioctl_hv(struct file *filp, · 132 unmodified lines | ||
| 2407 | 2417 | return r; |
| 2408 | 2418 | } |
| 2409 | 2419 | |
| 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 | + | |
| 2410 | 2459 | static struct kvmppc_ops kvm_ops_hv = { |
| 2411 | 2460 | .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv, |
| 2412 | 2461 | .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv, |
| @@ -2454,6 +2503,8 @@ static int kvmppc_book3s_init_hv(void) · 41 unmodified lines | ||
| 2454 | 2503 | kvm_ops_hv.owner = THIS_MODULE; |
| 2455 | 2504 | kvmppc_hv_ops = &kvm_ops_hv; |
| 2456 | 2505 | |
| 2506 | + init_default_hcalls(); | |
| 2507 | + | |
| 2457 | 2508 | r = kvmppc_mmu_hv_init(); |
| 2458 | 2509 | return r; |
| 2459 | 2510 | } |
| @@ -1909,6 +1909,17 @@ hcall_try_real_mode: · 1908 unmodified lines | ||
| 1909 | 1909 | clrrdi r3,r3,2 |
| 1910 | 1910 | cmpldi r3,hcall_real_table_end - hcall_real_table |
| 1911 | 1911 | 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 */ | |
| 1912 | 1923 | LOAD_REG_ADDR(r4, hcall_real_table) |
| 1913 | 1924 | lwax r3,r3,r4 |
| 1914 | 1925 | cmpwi r3,0 |
| @@ -1597,6 +1597,11 @@ static int kvmppc_core_init_vm_pr(struct kvm *kvm) · 1596 unmodified lines | ||
| 1597 | 1597 | { |
| 1598 | 1598 | mutex_init(&kvm->arch.hpt_mutex); |
| 1599 | 1599 | |
| 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 | + | |
| 1600 | 1605 | if (firmware_has_feature(FW_FEATURE_SET_MODE)) { |
| 1601 | 1606 | spin_lock(&kvm_global_user_count_lock); |
| 1602 | 1607 | if (++kvm_global_user_count == 1) |
| @@ -267,6 +267,10 @@ static int kvmppc_h_pr_xics_hcall(struct kvm_vcpu *vcpu, u32 cmd) · 266 unmodified lines | ||
| 267 | 267 | |
| 268 | 268 | int kvmppc_h_pr(struct kvm_vcpu *vcpu, unsigned long cmd) |
| 269 | 269 | { |
| 270 | + if (cmd <= MAX_HCALL_OPCODE && | |
| 271 | + !test_bit(cmd/4, vcpu->kvm->arch.enabled_hcalls)) | |
| 272 | + return EMULATE_FAIL; | |
| 273 | + | |
| 270 | 274 | switch (cmd) { |
| 271 | 275 | case H_ENTER: |
| 272 | 276 | return kvmppc_h_pr_enter(vcpu); |
| @@ -304,3 +308,36 @@ int kvmppc_h_pr(struct kvm_vcpu *vcpu, unsigned long cmd) · 31 unmodified lines | ||
| 304 | 308 | |
| 305 | 309 | return EMULATE_FAIL; |
| 306 | 310 | } |
| 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 | +} | |
| @@ -387,6 +387,7 @@ int kvm_dev_ioctl_check_extension(long ext) · 386 unmodified lines | ||
| 387 | 387 | case KVM_CAP_PPC_UNSET_IRQ: |
| 388 | 388 | case KVM_CAP_PPC_IRQ_LEVEL: |
| 389 | 389 | case KVM_CAP_ENABLE_CAP: |
| 390 | + case KVM_CAP_ENABLE_CAP_VM: | |
| 390 | 391 | case KVM_CAP_ONE_REG: |
| 391 | 392 | case KVM_CAP_IOEVENTFD: |
| 392 | 393 | case KVM_CAP_DEVICE_CTRL: |
| @@ -417,6 +418,7 @@ int kvm_dev_ioctl_check_extension(long ext) · 24 unmodified lines | ||
| 417 | 418 | case KVM_CAP_PPC_ALLOC_HTAB: |
| 418 | 419 | case KVM_CAP_PPC_RTAS: |
| 419 | 420 | case KVM_CAP_PPC_FIXUP_HCALL: |
| 421 | + case KVM_CAP_PPC_ENABLE_HCALL: | |
| 420 | 422 | #ifdef CONFIG_KVM_XICS |
| 421 | 423 | case KVM_CAP_IRQ_XICS: |
| 422 | 424 | #endif |
| @@ -1099,6 +1101,40 @@ int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event, · 676 unmodified lines | ||
| 1099 | 1101 | return 0; |
| 1100 | 1102 | } |
| 1101 | 1103 | |
| 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 | + | |
| 1102 | 1138 | long kvm_arch_vm_ioctl(struct file *filp, |
| 1103 | 1139 | unsigned int ioctl, unsigned long arg) |
| 1104 | 1140 | { |
| @@ -1118,6 +1154,15 @@ long kvm_arch_vm_ioctl(struct file *filp, · 13 unmodified lines | ||
| 1118 | 1154 | |
| 1119 | 1155 | break; |
| 1120 | 1156 | } |
| 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 | + } | |
| 1121 | 1166 | #ifdef CONFIG_PPC_BOOK3S_64 |
| 1122 | 1167 | case KVM_CREATE_SPAPR_TCE: { |
| 1123 | 1168 | struct kvm_create_spapr_tce create_tce; |
| @@ -758,6 +758,7 @@ struct kvm_ppc_smmu_info { · 757 unmodified lines | ||
| 758 | 758 | #define KVM_CAP_VM_ATTRIBUTES 101 |
| 759 | 759 | #define KVM_CAP_ARM_PSCI_0_2 102 |
| 760 | 760 | #define KVM_CAP_PPC_FIXUP_HCALL 103 |
| 761 | +#define KVM_CAP_PPC_ENABLE_HCALL 104 | |
| 761 | 762 | |
| 762 | 763 | #ifdef KVM_CAP_IRQ_ROUTING |
| 763 | 764 | |