Skip to content

Commit 69eaede

Browse files
committed
KVM: Introduce KVM_MEMORY_ENCRYPT_{UN,}REG_REGION ioctl
If hardware supports memory encryption then KVM_MEMORY_ENCRYPT_REG_REGION and KVM_MEMORY_ENCRYPT_UNREG_REGION ioctl's can be used by userspace to register/unregister the guest memory regions which may contain the encrypted data (e.g guest RAM, PCI BAR, SMRAM etc). Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Ingo Molnar <mingo@redhat.com> Cc: "H. Peter Anvin" <hpa@zytor.com> Cc: Paolo Bonzini <pbonzini@redhat.com> Cc: "Radim Krčmář" <rkrcmar@redhat.com> Cc: Joerg Roedel <joro@8bytes.org> Cc: Borislav Petkov <bp@suse.de> Cc: Tom Lendacky <thomas.lendacky@amd.com> Cc: x86@kernel.org Cc: kvm@vger.kernel.org Cc: linux-kernel@vger.kernel.org Improvements-by: Borislav Petkov <bp@suse.de> Signed-off-by: Brijesh Singh <brijesh.singh@amd.com> Reviewed-by: Borislav Petkov <bp@suse.de>
1 parent 5acc5c0 commit 69eaede

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed

Documentation/virtual/kvm/api.txt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3410,6 +3410,40 @@ Currently, this ioctl is used for issuing Secure Encrypted Virtualization
34103410
(SEV) commands on AMD Processors. The SEV commands are defined in
34113411
Documentation/virtual/kvm/amd-memory-encryption.txt.
34123412

3413+
4.110 KVM_MEMORY_ENCRYPT_REG_REGION
3414+
3415+
Capability: basic
3416+
Architectures: x86
3417+
Type: system
3418+
Parameters: struct kvm_enc_region (in)
3419+
Returns: 0 on success; -1 on error
3420+
3421+
This ioctl can be used to register a guest memory region which may
3422+
contain encrypted data (e.g. guest RAM, SMRAM etc).
3423+
3424+
It is used in the SEV-enabled guest. When encryption is enabled, a guest
3425+
memory region may contain encrypted data. The SEV memory encryption
3426+
engine uses a tweak such that two identical plaintext pages, each at
3427+
different locations will have differing ciphertexts. So swapping or
3428+
moving ciphertext of those pages will not result in plaintext being
3429+
swapped. So relocating (or migrating) physical backing pages for the SEV
3430+
guest will require some additional steps.
3431+
3432+
Note: The current SEV key management spec does not provide commands to
3433+
swap or migrate (move) ciphertext pages. Hence, for now we pin the guest
3434+
memory region registered with the ioctl.
3435+
3436+
4.111 KVM_MEMORY_ENCRYPT_UNREG_REGION
3437+
3438+
Capability: basic
3439+
Architectures: x86
3440+
Type: system
3441+
Parameters: struct kvm_enc_region (in)
3442+
Returns: 0 on success; -1 on error
3443+
3444+
This ioctl can be used to unregister the guest memory region registered
3445+
with KVM_MEMORY_ENCRYPT_REG_REGION ioctl above.
3446+
34133447
5. The kvm_run structure
34143448
------------------------
34153449

arch/x86/include/asm/kvm_host.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,6 +1068,8 @@ struct kvm_x86_ops {
10681068
int (*enable_smi_window)(struct kvm_vcpu *vcpu);
10691069

10701070
int (*mem_enc_op)(struct kvm *kvm, void __user *argp);
1071+
int (*mem_enc_reg_region)(struct kvm *kvm, struct kvm_enc_region *argp);
1072+
int (*mem_enc_unreg_region)(struct kvm *kvm, struct kvm_enc_region *argp);
10711073
};
10721074

10731075
struct kvm_arch_async_pf {

arch/x86/kvm/x86.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4287,6 +4287,30 @@ long kvm_arch_vm_ioctl(struct file *filp,
42874287
r = kvm_x86_ops->mem_enc_op(kvm, argp);
42884288
break;
42894289
}
4290+
case KVM_MEMORY_ENCRYPT_REG_REGION: {
4291+
struct kvm_enc_region region;
4292+
4293+
r = -EFAULT;
4294+
if (copy_from_user(&region, argp, sizeof(region)))
4295+
goto out;
4296+
4297+
r = -ENOTTY;
4298+
if (kvm_x86_ops->mem_enc_reg_region)
4299+
r = kvm_x86_ops->mem_enc_reg_region(kvm, &region);
4300+
break;
4301+
}
4302+
case KVM_MEMORY_ENCRYPT_UNREG_REGION: {
4303+
struct kvm_enc_region region;
4304+
4305+
r = -EFAULT;
4306+
if (copy_from_user(&region, argp, sizeof(region)))
4307+
goto out;
4308+
4309+
r = -ENOTTY;
4310+
if (kvm_x86_ops->mem_enc_unreg_region)
4311+
r = kvm_x86_ops->mem_enc_unreg_region(kvm, &region);
4312+
break;
4313+
}
42904314
default:
42914315
r = -ENOTTY;
42924316
}

include/uapi/linux/kvm.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,6 +1361,14 @@ struct kvm_s390_ucas_mapping {
13611361
/* Memory Encryption Commands */
13621362
#define KVM_MEMORY_ENCRYPT_OP _IOWR(KVMIO, 0xba, unsigned long)
13631363

1364+
struct kvm_enc_region {
1365+
__u64 addr;
1366+
__u64 size;
1367+
};
1368+
1369+
#define KVM_MEMORY_ENCRYPT_REG_REGION _IOR(KVMIO, 0xbb, struct kvm_enc_region)
1370+
#define KVM_MEMORY_ENCRYPT_UNREG_REGION _IOR(KVMIO, 0xbc, struct kvm_enc_region)
1371+
13641372
#define KVM_DEV_ASSIGN_ENABLE_IOMMU (1 << 0)
13651373
#define KVM_DEV_ASSIGN_PCI_2_3 (1 << 1)
13661374
#define KVM_DEV_ASSIGN_MASK_INTX (1 << 2)

0 commit comments

Comments
 (0)