Skip to content

Commit b392e24

Browse files
committed
Update memory enc kvm.c
Signed-off-by: Shivam Tiwari <[email protected]>
1 parent 7cb1b46 commit b392e24

File tree

1 file changed

+67
-44
lines changed

1 file changed

+67
-44
lines changed

arch/x86/kernel/kvm.c

Lines changed: 67 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -927,65 +927,88 @@ static bool __init kvm_msi_ext_dest_id(void)
927927

928928
static void kvm_sev_hc_page_enc_status(unsigned long pfn, int npages, bool enc)
929929
{
930-
kvm_sev_hypercall3(KVM_HC_MAP_GPA_RANGE, pfn << PAGE_SHIFT, npages,
931-
KVM_MAP_GPA_RANGE_ENC_STAT(enc) | KVM_MAP_GPA_RANGE_PAGE_SZ_4K);
930+
unsigned long end_pfn = pfn + npages;
931+
932+
// Input validation: Ensure that the page frame numbers are aligned and within bounds
933+
if (pfn % PAGE_SIZE != 0) {
934+
pr_err("Invalid memory address: pfn is not page-aligned\n");
935+
return;
936+
}
937+
938+
if (end_pfn > MAX_MEMORY_PFN) {
939+
pr_err("Memory range exceeds maximum allowed physical address space\n");
940+
return;
941+
}
942+
943+
if (npages <= 0) {
944+
pr_err("Invalid number of pages: npages must be positive\n");
945+
return;
946+
}
947+
948+
// Debugging: Log the memory encryption status change for traceability
949+
pr_info("Changing encryption status for memory range: [0x%lx - 0x%lx] to %s\n", pfn, end_pfn - 1, enc ? "encrypted" : "decrypted");
950+
951+
// Perform the hypercall to update encryption status
952+
if (kvm_sev_hypercall3(KVM_HC_MAP_GPA_RANGE, pfn << PAGE_SHIFT, npages,
953+
KVM_MAP_GPA_RANGE_ENC_STAT(enc) | KVM_MAP_GPA_RANGE_PAGE_SZ_4K)) {
954+
pr_err("Failed to update memory encryption status for range [0x%lx - 0x%lx]\n", pfn, end_pfn - 1);
955+
}
932956
}
933957

934958
static void __init kvm_init_platform(void)
935959
{
936-
if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT) &&
937-
kvm_para_has_feature(KVM_FEATURE_MIGRATION_CONTROL)) {
938-
unsigned long nr_pages;
939-
int i;
960+
if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT) && kvm_para_has_feature(KVM_FEATURE_MIGRATION_CONTROL)) {
961+
unsigned long nr_pages;
962+
int i;
940963

941-
pv_ops.mmu.notify_page_enc_status_changed =
942-
kvm_sev_hc_page_enc_status;
964+
pv_ops.mmu.notify_page_enc_status_changed = kvm_sev_hc_page_enc_status;
943965

944-
/*
945-
* Reset the host's shared pages list related to kernel
946-
* specific page encryption status settings before we load a
947-
* new kernel by kexec. Reset the page encryption status
948-
* during early boot instead of just before kexec to avoid SMP
949-
* races during kvm_pv_guest_cpu_reboot().
950-
* NOTE: We cannot reset the complete shared pages list
951-
* here as we need to retain the UEFI/OVMF firmware
952-
* specific settings.
953-
*/
966+
for (i = 0; i < e820_table->nr_entries; i++) {
967+
struct e820_entry *entry = &e820_table->entries[i];
954968

955-
for (i = 0; i < e820_table->nr_entries; i++) {
956-
struct e820_entry *entry = &e820_table->entries[i];
969+
if (entry->type != E820_TYPE_RAM)
970+
continue;
957971

958-
if (entry->type != E820_TYPE_RAM)
959-
continue;
972+
nr_pages = DIV_ROUND_UP(entry->size, PAGE_SIZE);
960973

961-
nr_pages = DIV_ROUND_UP(entry->size, PAGE_SIZE);
974+
// Input validation for memory range
975+
if (entry->addr % PAGE_SIZE != 0) {
976+
pr_err("Invalid memory address in e820 entry (not page-aligned): 0x%lx\n", entry->addr);
977+
continue;
978+
}
962979

963-
kvm_sev_hypercall3(KVM_HC_MAP_GPA_RANGE, entry->addr,
964-
nr_pages,
965-
KVM_MAP_GPA_RANGE_ENCRYPTED | KVM_MAP_GPA_RANGE_PAGE_SZ_4K);
966-
}
980+
if (entry->addr + entry->size > MAX_MEMORY_ADDR) {
981+
pr_err("Memory range in e820 entry exceeds maximum allowed address space: 0x%lx\n", entry->addr);
982+
continue;
983+
}
967984

968-
/*
969-
* Ensure that _bss_decrypted section is marked as decrypted in the
970-
* shared pages list.
971-
*/
972-
early_set_mem_enc_dec_hypercall((unsigned long)__start_bss_decrypted,
973-
__end_bss_decrypted - __start_bss_decrypted, 0);
985+
// Log memory encryption status for debugging
986+
pr_info("Encrypting memory range in e820 entry: [0x%lx - 0x%lx]\n", entry->addr, entry->addr + entry->size - 1);
974987

975-
/*
976-
* If not booted using EFI, enable Live migration support.
977-
*/
978-
if (!efi_enabled(EFI_BOOT))
979-
wrmsrl(MSR_KVM_MIGRATION_CONTROL,
980-
KVM_MIGRATION_READY);
981-
}
982-
kvmclock_init();
983-
x86_platform.apic_post_init = kvm_apic_init;
988+
// Perform memory encryption for the range
989+
kvm_sev_hypercall3(KVM_HC_MAP_GPA_RANGE, entry->addr, nr_pages,
990+
KVM_MAP_GPA_RANGE_ENCRYPTED | KVM_MAP_GPA_RANGE_PAGE_SZ_4K);
991+
}
992+
993+
// Ensure that _bss_decrypted section is marked as decrypted
994+
early_set_mem_enc_dec_hypercall((unsigned long)__start_bss_decrypted,
995+
__end_bss_decrypted - __start_bss_decrypted, 0);
996+
997+
// Log that the memory is being decrypted
998+
pr_info("Marking _bss_decrypted section as decrypted\n");
984999

985-
/* Set WB as the default cache mode for SEV-SNP and TDX */
986-
mtrr_overwrite_state(NULL, 0, MTRR_TYPE_WRBACK);
1000+
if (!efi_enabled(EFI_BOOT))
1001+
wrmsrl(MSR_KVM_MIGRATION_CONTROL, KVM_MIGRATION_READY);
1002+
}
1003+
1004+
kvmclock_init();
1005+
x86_platform.apic_post_init = kvm_apic_init;
1006+
1007+
// Set WB as the default cache mode for SEV-SNP and TDX
1008+
mtrr_overwrite_state(NULL, 0, MTRR_TYPE_WRBACK);
9871009
}
9881010

1011+
9891012
#if defined(CONFIG_AMD_MEM_ENCRYPT)
9901013
static void kvm_sev_es_hcall_prepare(struct ghcb *ghcb, struct pt_regs *regs)
9911014
{

0 commit comments

Comments
 (0)