Skip to content

Commit 5997efb

Browse files
tlendackyIngo Molnar
authored andcommitted
x86/boot: Use memremap() to map the MPF and MPC data
The SMP MP-table is built by UEFI and placed in memory in a decrypted state. These tables are accessed using a mix of early_memremap(), early_memunmap(), phys_to_virt() and virt_to_phys(). Change all accesses to use early_memremap()/early_memunmap(). This allows for proper setting of the encryption mask so that the data can be successfully accessed when SME is active. Signed-off-by: Tom Lendacky <thomas.lendacky@amd.com> Reviewed-by: Thomas Gleixner <tglx@linutronix.de> Reviewed-by: Borislav Petkov <bp@suse.de> Cc: Alexander Potapenko <glider@google.com> Cc: Andrey Ryabinin <aryabinin@virtuozzo.com> Cc: Andy Lutomirski <luto@kernel.org> Cc: Arnd Bergmann <arnd@arndb.de> Cc: Borislav Petkov <bp@alien8.de> Cc: Brijesh Singh <brijesh.singh@amd.com> Cc: Dave Young <dyoung@redhat.com> Cc: Dmitry Vyukov <dvyukov@google.com> Cc: Jonathan Corbet <corbet@lwn.net> Cc: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com> Cc: Larry Woodman <lwoodman@redhat.com> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Matt Fleming <matt@codeblueprint.co.uk> Cc: Michael S. Tsirkin <mst@redhat.com> Cc: Paolo Bonzini <pbonzini@redhat.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Radim Krčmář <rkrcmar@redhat.com> Cc: Rik van Riel <riel@redhat.com> Cc: Toshimitsu Kani <toshi.kani@hpe.com> Cc: kasan-dev@googlegroups.com Cc: kvm@vger.kernel.org Cc: linux-arch@vger.kernel.org Cc: linux-doc@vger.kernel.org Cc: linux-efi@vger.kernel.org Cc: linux-mm@kvack.org Link: http://lkml.kernel.org/r/d9464b0d7c861021ed8f494e4a40d6cd10f1eddd.1500319216.git.thomas.lendacky@amd.com Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 8f716c9 commit 5997efb

File tree

1 file changed

+70
-28
lines changed

1 file changed

+70
-28
lines changed

arch/x86/kernel/mpparse.c

Lines changed: 70 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ static inline void __init construct_default_ISA_mptable(int mpc_default_type)
429429
}
430430
}
431431

432-
static struct mpf_intel *mpf_found;
432+
static unsigned long mpf_base;
433433

434434
static unsigned long __init get_mpc_size(unsigned long physptr)
435435
{
@@ -451,6 +451,7 @@ static int __init check_physptr(struct mpf_intel *mpf, unsigned int early)
451451

452452
size = get_mpc_size(mpf->physptr);
453453
mpc = early_memremap(mpf->physptr, size);
454+
454455
/*
455456
* Read the physical hardware table. Anything here will
456457
* override the defaults.
@@ -497,12 +498,12 @@ static int __init check_physptr(struct mpf_intel *mpf, unsigned int early)
497498
*/
498499
void __init default_get_smp_config(unsigned int early)
499500
{
500-
struct mpf_intel *mpf = mpf_found;
501+
struct mpf_intel *mpf;
501502

502503
if (!smp_found_config)
503504
return;
504505

505-
if (!mpf)
506+
if (!mpf_base)
506507
return;
507508

508509
if (acpi_lapic && early)
@@ -515,6 +516,12 @@ void __init default_get_smp_config(unsigned int early)
515516
if (acpi_lapic && acpi_ioapic)
516517
return;
517518

519+
mpf = early_memremap(mpf_base, sizeof(*mpf));
520+
if (!mpf) {
521+
pr_err("MPTABLE: error mapping MP table\n");
522+
return;
523+
}
524+
518525
pr_info("Intel MultiProcessor Specification v1.%d\n",
519526
mpf->specification);
520527
#if defined(CONFIG_X86_LOCAL_APIC) && defined(CONFIG_X86_32)
@@ -529,7 +536,7 @@ void __init default_get_smp_config(unsigned int early)
529536
/*
530537
* Now see if we need to read further.
531538
*/
532-
if (mpf->feature1 != 0) {
539+
if (mpf->feature1) {
533540
if (early) {
534541
/*
535542
* local APIC has default address
@@ -542,8 +549,10 @@ void __init default_get_smp_config(unsigned int early)
542549
construct_default_ISA_mptable(mpf->feature1);
543550

544551
} else if (mpf->physptr) {
545-
if (check_physptr(mpf, early))
552+
if (check_physptr(mpf, early)) {
553+
early_memunmap(mpf, sizeof(*mpf));
546554
return;
555+
}
547556
} else
548557
BUG();
549558

@@ -552,6 +561,8 @@ void __init default_get_smp_config(unsigned int early)
552561
/*
553562
* Only use the first configuration found.
554563
*/
564+
565+
early_memunmap(mpf, sizeof(*mpf));
555566
}
556567

557568
static void __init smp_reserve_memory(struct mpf_intel *mpf)
@@ -561,15 +572,16 @@ static void __init smp_reserve_memory(struct mpf_intel *mpf)
561572

562573
static int __init smp_scan_config(unsigned long base, unsigned long length)
563574
{
564-
unsigned int *bp = phys_to_virt(base);
575+
unsigned int *bp;
565576
struct mpf_intel *mpf;
566-
unsigned long mem;
577+
int ret = 0;
567578

568579
apic_printk(APIC_VERBOSE, "Scan for SMP in [mem %#010lx-%#010lx]\n",
569580
base, base + length - 1);
570581
BUILD_BUG_ON(sizeof(*mpf) != 16);
571582

572583
while (length > 0) {
584+
bp = early_memremap(base, length);
573585
mpf = (struct mpf_intel *)bp;
574586
if ((*bp == SMP_MAGIC_IDENT) &&
575587
(mpf->length == 1) &&
@@ -579,24 +591,26 @@ static int __init smp_scan_config(unsigned long base, unsigned long length)
579591
#ifdef CONFIG_X86_LOCAL_APIC
580592
smp_found_config = 1;
581593
#endif
582-
mpf_found = mpf;
594+
mpf_base = base;
583595

584-
pr_info("found SMP MP-table at [mem %#010llx-%#010llx] mapped at [%p]\n",
585-
(unsigned long long) virt_to_phys(mpf),
586-
(unsigned long long) virt_to_phys(mpf) +
587-
sizeof(*mpf) - 1, mpf);
596+
pr_info("found SMP MP-table at [mem %#010lx-%#010lx] mapped at [%p]\n",
597+
base, base + sizeof(*mpf) - 1, mpf);
588598

589-
mem = virt_to_phys(mpf);
590-
memblock_reserve(mem, sizeof(*mpf));
599+
memblock_reserve(base, sizeof(*mpf));
591600
if (mpf->physptr)
592601
smp_reserve_memory(mpf);
593602

594-
return 1;
603+
ret = 1;
595604
}
596-
bp += 4;
605+
early_memunmap(bp, length);
606+
607+
if (ret)
608+
break;
609+
610+
base += 16;
597611
length -= 16;
598612
}
599-
return 0;
613+
return ret;
600614
}
601615

602616
void __init default_find_smp_config(void)
@@ -838,29 +852,40 @@ static int __init update_mp_table(void)
838852
char oem[10];
839853
struct mpf_intel *mpf;
840854
struct mpc_table *mpc, *mpc_new;
855+
unsigned long size;
841856

842857
if (!enable_update_mptable)
843858
return 0;
844859

845-
mpf = mpf_found;
846-
if (!mpf)
860+
if (!mpf_base)
861+
return 0;
862+
863+
mpf = early_memremap(mpf_base, sizeof(*mpf));
864+
if (!mpf) {
865+
pr_err("MPTABLE: mpf early_memremap() failed\n");
847866
return 0;
867+
}
848868

849869
/*
850870
* Now see if we need to go further.
851871
*/
852-
if (mpf->feature1 != 0)
853-
return 0;
872+
if (mpf->feature1)
873+
goto do_unmap_mpf;
854874

855875
if (!mpf->physptr)
856-
return 0;
876+
goto do_unmap_mpf;
857877

858-
mpc = phys_to_virt(mpf->physptr);
878+
size = get_mpc_size(mpf->physptr);
879+
mpc = early_memremap(mpf->physptr, size);
880+
if (!mpc) {
881+
pr_err("MPTABLE: mpc early_memremap() failed\n");
882+
goto do_unmap_mpf;
883+
}
859884

860885
if (!smp_check_mpc(mpc, oem, str))
861-
return 0;
886+
goto do_unmap_mpc;
862887

863-
pr_info("mpf: %llx\n", (u64)virt_to_phys(mpf));
888+
pr_info("mpf: %llx\n", (u64)mpf_base);
864889
pr_info("physptr: %x\n", mpf->physptr);
865890

866891
if (mpc_new_phys && mpc->length > mpc_new_length) {
@@ -878,21 +903,32 @@ static int __init update_mp_table(void)
878903
new = mpf_checksum((unsigned char *)mpc, mpc->length);
879904
if (old == new) {
880905
pr_info("mpc is readonly, please try alloc_mptable instead\n");
881-
return 0;
906+
goto do_unmap_mpc;
882907
}
883908
pr_info("use in-position replacing\n");
884909
} else {
910+
mpc_new = early_memremap(mpc_new_phys, mpc_new_length);
911+
if (!mpc_new) {
912+
pr_err("MPTABLE: new mpc early_memremap() failed\n");
913+
goto do_unmap_mpc;
914+
}
885915
mpf->physptr = mpc_new_phys;
886-
mpc_new = phys_to_virt(mpc_new_phys);
887916
memcpy(mpc_new, mpc, mpc->length);
917+
early_memunmap(mpc, size);
888918
mpc = mpc_new;
919+
size = mpc_new_length;
889920
/* check if we can modify that */
890921
if (mpc_new_phys - mpf->physptr) {
891922
struct mpf_intel *mpf_new;
892923
/* steal 16 bytes from [0, 1k) */
924+
mpf_new = early_memremap(0x400 - 16, sizeof(*mpf_new));
925+
if (!mpf_new) {
926+
pr_err("MPTABLE: new mpf early_memremap() failed\n");
927+
goto do_unmap_mpc;
928+
}
893929
pr_info("mpf new: %x\n", 0x400 - 16);
894-
mpf_new = phys_to_virt(0x400 - 16);
895930
memcpy(mpf_new, mpf, 16);
931+
early_memunmap(mpf, sizeof(*mpf));
896932
mpf = mpf_new;
897933
mpf->physptr = mpc_new_phys;
898934
}
@@ -909,6 +945,12 @@ static int __init update_mp_table(void)
909945
*/
910946
replace_intsrc_all(mpc, mpc_new_phys, mpc_new_length);
911947

948+
do_unmap_mpc:
949+
early_memunmap(mpc, size);
950+
951+
do_unmap_mpf:
952+
early_memunmap(mpf, sizeof(*mpf));
953+
912954
return 0;
913955
}
914956

0 commit comments

Comments
 (0)