x86: Prevent APIC base address from changing in test_enable_x2apic()
PATCH 1/1 x86: Prevent APIC base address from changing in test_enable_x2apic()
From: Nadav Amit <[email protected]>
test_enable_x2apic() unintentionally changes the APIC base address to zero and resets the BSP indication. This actually causes the local APIC to overlap the IDT area, which is wrong. Prevent it from happening by keeping the APIC base address and BSP-bit as it was before.
--- x86/apic.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
x86/apic.c +2 -2
@@ -90,11 +90,11 @@ static void test_enable_x2apic(void)report ("disabled to x2apic enabled" ,test_write_apicbase_exception (APIC_EN |APIC_EXTD )); -wrmsr (MSR_IA32_APICBASE ,APIC_EN ); +wrmsr (MSR_IA32_APICBASE ,APIC_EN |APIC_DEFAULT_PHYS_BASE |APIC_BSP );report ("apic enabled to invalid state" ,test_write_apicbase_exception (APIC_EXTD )); -wrmsr (MSR_IA32_APICBASE ,APIC_EN |APIC_EXTD ); +wrmsr (MSR_IA32_APICBASE ,APIC_EN |APIC_EXTD |APIC_DEFAULT_PHYS_BASE |APIC_BSP );apic_write (APIC_SPIV ,0x1ff ); }else {printf ("x2apic not detected\n" );
Signature · 1 line
2.17.1
Quoted 17 lines · On Mon, Apr 15, 2019 at 11:31:00AM -0700, [email protected] wrote:
From: Nadav Amit <[email protected]>
test_enable_x2apic()unintentionally changes the APIC base address to zero and resets the BSP indication. This actually causes the local APIC to overlap the IDT area, which is wrong. Prevent it from happening by keeping the APIC base address and BSP-bit as it was before.Signed-off-by Nadav Amit <[email protected]>--- x86/apic.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)x86/apic.c +0
-0
x86/apic.c static void test_enable_x2apic(void) · line 97
Quoted 10 lines
x86/apic.c +2
-2@@ -90,11 +90,11 @@ static void test_enable_x2apic(void)report ("disabled to x2apic enabled" ,test_write_apicbase_exception (APIC_EN |APIC_EXTD )); -wrmsr (MSR_IA32_APICBASE ,APIC_EN ); +wrmsr (MSR_IA32_APICBASE ,APIC_EN |APIC_DEFAULT_PHYS_BASE |APIC_BSP );report ("apic enabled to invalid state" ,test_write_apicbase_exception (APIC_EXTD )); -wrmsr (MSR_IA32_APICBASE ,APIC_EN |APIC_EXTD ); +wrmsr (MSR_IA32_APICBASE ,APIC_EN |APIC_EXTD |APIC_DEFAULT_PHYS_BASE |APIC_BSP );
It probably doesn't matter since AFAIK kvm-unit-tests always uses the default base, but preserving the current base+BSP would be preferred. The #GP tests get away with using APIC_DEFAULT_PHYS_BASE because the WRMSR will never succeed, but even that is poor form. And the test should also reset to xAPIC when it's done.
I think the attached patch covers everything.
Quoted 6 lines
x86/apic.c +0
-0apic_write (APIC_SPIV ,0x1ff ); }else {printf ("x2apic not detected\n" );Signature · 1 line
2.17.1
From 0905302c23f20f67483b281e33f1930135087e3a Mon Sep 17 00:00:00 2001
From: Sean Christopherson <[email protected]>
Date: Mon, 15 Apr 2019 11:53:00 -0700
Subject: [PATCH] x86: apic: Preserve APIC base and BSP bits during x2APIC
tests
...and reset to xAPIC mode unless x2APIC was already enabled.
--- x86/apic.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-)
x86/apic.c +15 -9
@@ -69,33 +69,39 @@ static void do_write_apicbase(void *data)static bool test_write_apicbase_exception (u64 data) { - data |=APIC_DEFAULT_PHYS_BASE |APIC_BSP ;return test_for_exception (GP_VECTOR , do_write_apicbase, &data); }static void test_enable_x2apic (void ) { +u64 orig_apicbase =rdmsr (MSR_IA32_APICBASE ); +u64 apicbase; +if (enable_x2apic ()) {printf ("x2apic enabled\n" ); + apicbase = orig_apicbase & ~(APIC_EN |APIC_EXTD );report ("x2apic enabled to invalid state" , -test_write_apicbase_exception (APIC_EXTD )); +test_write_apicbase_exception (apicbase |APIC_EXTD ));report ("x2apic enabled to apic enabled" , -test_write_apicbase_exception (APIC_EN )); +test_write_apicbase_exception (apicbase |APIC_EN ));report ("x2apic enabled to disabled state" , - !test_write_apicbase_exception (0 )); + !test_write_apicbase_exception (apicbase |0 ));report ("disabled to invalid state" , -test_write_apicbase_exception (APIC_EXTD )); +test_write_apicbase_exception (apicbase |APIC_EXTD ));report ("disabled to x2apic enabled" , -test_write_apicbase_exception (APIC_EN |APIC_EXTD )); +test_write_apicbase_exception (apicbase |APIC_EN |APIC_EXTD )); -wrmsr (MSR_IA32_APICBASE ,APIC_EN ); +wrmsr (MSR_IA32_APICBASE , apicbase |APIC_EN );report ("apic enabled to invalid state" , -test_write_apicbase_exception (APIC_EXTD )); +test_write_apicbase_exception (apicbase |APIC_EXTD )); -wrmsr (MSR_IA32_APICBASE ,APIC_EN |APIC_EXTD ); +wrmsr (MSR_IA32_APICBASE , apicbase |APIC_EN |APIC_EXTD );apic_write (APIC_SPIV ,0x1ff ); + +if (!(orig_apicbase &APIC_EXTD )) +reset_apic (); }else {printf ("x2apic not detected\n" );
Signature · 1 line
2.21.0
Quoted 37 lines · Sean Christopherson
Quoted 17 lines · On Mon, Apr 15, 2019 at 11:31:00AM -0700, [email protected] wrote:
From: Nadav Amit <[email protected]>
test_enable_x2apic()unintentionally changes the APIC base address to zero and resets the BSP indication. This actually causes the local APIC to overlap the IDT area, which is wrong. Prevent it from happening by keeping the APIC base address and BSP-bit as it was before.Signed-off-by Nadav Amit <[email protected]>---
x86/apic.c| 4 ++--1 file changed, 2 insertions(+), 2 deletions(-)x86/apic.c +0
-0x86/apic.c static void test_enable_x2apic(void) · line 97
Quoted 10 lines
x86/apic.c +2
-2@@ -90,11 +90,11 @@ static void test_enable_x2apic(void)report ("disabled to x2apic enabled" ,test_write_apicbase_exception (APIC_EN |APIC_EXTD )); -wrmsr (MSR_IA32_APICBASE ,APIC_EN ); +wrmsr (MSR_IA32_APICBASE ,APIC_EN |APIC_DEFAULT_PHYS_BASE |APIC_BSP );report ("apic enabled to invalid state" ,test_write_apicbase_exception (APIC_EXTD )); -wrmsr (MSR_IA32_APICBASE ,APIC_EN |APIC_EXTD ); +wrmsr (MSR_IA32_APICBASE ,APIC_EN |APIC_EXTD |APIC_DEFAULT_PHYS_BASE |APIC_BSP );It probably doesn't matter since AFAIK kvm-unit-tests always uses the default base, but preserving the current base+BSP would be preferred. The #GP tests get away with using
APIC_DEFAULT_PHYS_BASEbecause the WRMSR will never succeed, but even that is poor form. And the test should also reset to xAPIC when it's done.
Quoted 1 line
I think the attached patch covers everything.
Thanks. The base is indeed the default, so it should not matter, but your change should make the code more robust.
One important comment regarding your patch:
x86/apic.c static void do_write_apicbase(void *data) · line 104
Quoted 2 lines
x86/apic.c +2
-0+if (!(orig_apicbase &APIC_EXTD )) +reset_apic ();
This is not needed, since enable_x2apic() will only return true if it succeeded in enabling x2apic. In addition, this is wrong, since reset_apic() should (usually, and specifically in this case) be followed with:
apic_write(APIC_SPIV, 0x1ff);
And not the other way around (there are actually a couple of missing writes to software-enable the APIC after reset_apic(), which I’ll send later).
Quoted 49 lines · Nadav Amit
Quoted 38 lines · Sean Christopherson
Quoted 17 lines · On Mon, Apr 15, 2019 at 11:31:00AM -0700, [email protected] wrote:
From: Nadav Amit <[email protected]>
test_enable_x2apic()unintentionally changes the APIC base address to zero and resets the BSP indication. This actually causes the local APIC to overlap the IDT area, which is wrong. Prevent it from happening by keeping the APIC base address and BSP-bit as it was before.Signed-off-by Nadav Amit <[email protected]>---
x86/apic.c| 4 ++--1 file changed, 2 insertions(+), 2 deletions(-)x86/apic.c +0
-0x86/apic.c static void test_enable_x2apic(void) · line 97
Quoted 10 lines
x86/apic.c +2
-2@@ -90,11 +90,11 @@ static void test_enable_x2apic(void)report ("disabled to x2apic enabled" ,test_write_apicbase_exception (APIC_EN |APIC_EXTD )); -wrmsr (MSR_IA32_APICBASE ,APIC_EN ); +wrmsr (MSR_IA32_APICBASE ,APIC_EN |APIC_DEFAULT_PHYS_BASE |APIC_BSP );report ("apic enabled to invalid state" ,test_write_apicbase_exception (APIC_EXTD )); -wrmsr (MSR_IA32_APICBASE ,APIC_EN |APIC_EXTD ); +wrmsr (MSR_IA32_APICBASE ,APIC_EN |APIC_EXTD |APIC_DEFAULT_PHYS_BASE |APIC_BSP );It probably doesn't matter since AFAIK kvm-unit-tests always uses the default base, but preserving the current base+BSP would be preferred. The #GP tests get away with using
APIC_DEFAULT_PHYS_BASEbecause the WRMSR will never succeed, but even that is poor form. And the test should also reset to xAPIC when it's done.Quoted 1 line
I think the attached patch covers everything.
Thanks. The base is indeed the default, so it should not matter, but your change should make the code more robust.
One important comment regarding your patch:
x86/apic.c static void do_write_apicbase(void *data) · line 104
Quoted 2 lines
x86/apic.c +2
-0+if (!(orig_apicbase &APIC_EXTD )) +reset_apic ();
Quoted 8 lines
This is not needed, since
enable_x2apic()will only return true if it succeeded in enabling x2apic. In addition, this is wrong, sincereset_apic()should (usually, and specifically in this case) be followed with:apic_write(APIC_SPIV, 0x1ff);And not the other way around (there are actually a couple of missing writes to software-enable the APIC after
reset_apic(), which I’ll send later).
Correcting myself - I understand (now) that you want to reset back to xapic, so that’s fine. Just keep "apic_write(APIC_SPIV, 0x1ff);” after the reset, please.
Quoted 53 lines · Nadav Amit
Quoted 53 lines · Nadav Amit
Quoted 38 lines · Sean Christopherson
Quoted 17 lines · On Mon, Apr 15, 2019 at 11:31:00AM -0700, [email protected] wrote:
From: Nadav Amit <[email protected]>
test_enable_x2apic()unintentionally changes the APIC base address to zero and resets the BSP indication. This actually causes the local APIC to overlap the IDT area, which is wrong. Prevent it from happening by keeping the APIC base address and BSP-bit as it was before.Signed-off-by Nadav Amit <[email protected]>---
x86/apic.c| 4 ++--1 file changed, 2 insertions(+), 2 deletions(-)x86/apic.c +0
-0x86/apic.c static void test_enable_x2apic(void) · line 97
Quoted 10 lines
x86/apic.c +2
-2@@ -90,11 +90,11 @@ static void test_enable_x2apic(void)report ("disabled to x2apic enabled" ,test_write_apicbase_exception (APIC_EN |APIC_EXTD )); -wrmsr (MSR_IA32_APICBASE ,APIC_EN ); +wrmsr (MSR_IA32_APICBASE ,APIC_EN |APIC_DEFAULT_PHYS_BASE |APIC_BSP );report ("apic enabled to invalid state" ,test_write_apicbase_exception (APIC_EXTD )); -wrmsr (MSR_IA32_APICBASE ,APIC_EN |APIC_EXTD ); +wrmsr (MSR_IA32_APICBASE ,APIC_EN |APIC_EXTD |APIC_DEFAULT_PHYS_BASE |APIC_BSP );It probably doesn't matter since AFAIK kvm-unit-tests always uses the default base, but preserving the current base+BSP would be preferred. The #GP tests get away with using
APIC_DEFAULT_PHYS_BASEbecause the WRMSR will never succeed, but even that is poor form. And the test should also reset to xAPIC when it's done.Quoted 1 line
I think the attached patch covers everything.
Thanks. The base is indeed the default, so it should not matter, but your change should make the code more robust.
One important comment regarding your patch:
x86/apic.c static void do_write_apicbase(void *data) · line 104
Quoted 2 lines
x86/apic.c +2
-0+if (!(orig_apicbase &APIC_EXTD )) +reset_apic ();This is not needed, since
enable_x2apic()will only return true if it succeeded in enabling x2apic. In addition, this is wrong, sincereset_apic()should (usually, and specifically in this case) be followed with:
Quoted 7 lines
Quoted 4 lines
apic_write(APIC_SPIV, 0x1ff);And not the other way around (there are actually a couple of missing writes to software-enable the APIC after
reset_apic(), which I’ll send later).Correcting myself - I understand (now) that you want to reset back to xapic, so that’s fine. Just keep "
apic_write(APIC_SPIV, 0x1ff);” after the reset, please.
Ah, will do, I glazed over the apic_write() and didn't consider its purpose.
Quoted 58 lines · Sean Christopherson
Quoted 55 lines · Nadav Amit
Quoted 55 lines · Nadav Amit
Quoted 38 lines · Sean Christopherson
Quoted 17 lines · On Mon, Apr 15, 2019 at 11:31:00AM -0700, [email protected] wrote:
From: Nadav Amit <[email protected]>
test_enable_x2apic()unintentionally changes the APIC base address to zero and resets the BSP indication. This actually causes the local APIC to overlap the IDT area, which is wrong. Prevent it from happening by keeping the APIC base address and BSP-bit as it was before.Signed-off-by Nadav Amit <[email protected]>---
x86/apic.c| 4 ++--1 file changed, 2 insertions(+), 2 deletions(-)x86/apic.c +0
-0x86/apic.c static void test_enable_x2apic(void) · line 97
Quoted 10 lines
x86/apic.c +2
-2@@ -90,11 +90,11 @@ static void test_enable_x2apic(void)report ("disabled to x2apic enabled" ,test_write_apicbase_exception (APIC_EN |APIC_EXTD )); -wrmsr (MSR_IA32_APICBASE ,APIC_EN ); +wrmsr (MSR_IA32_APICBASE ,APIC_EN |APIC_DEFAULT_PHYS_BASE |APIC_BSP );report ("apic enabled to invalid state" ,test_write_apicbase_exception (APIC_EXTD )); -wrmsr (MSR_IA32_APICBASE ,APIC_EN |APIC_EXTD ); +wrmsr (MSR_IA32_APICBASE ,APIC_EN |APIC_EXTD |APIC_DEFAULT_PHYS_BASE |APIC_BSP );It probably doesn't matter since AFAIK kvm-unit-tests always uses the default base, but preserving the current base+BSP would be preferred. The #GP tests get away with using
APIC_DEFAULT_PHYS_BASEbecause the WRMSR will never succeed, but even that is poor form. And the test should also reset to xAPIC when it's done.Quoted 1 line
I think the attached patch covers everything.
Thanks. The base is indeed the default, so it should not matter, but your change should make the code more robust.
One important comment regarding your patch:
x86/apic.c static void do_write_apicbase(void *data) · line 104
Quoted 2 lines
x86/apic.c +2
-0+if (!(orig_apicbase &APIC_EXTD )) +reset_apic ();This is not needed, since
enable_x2apic()will only return true if it succeeded in enabling x2apic. In addition, this is wrong, sincereset_apic()should (usually, and specifically in this case) be followed with:apic_write(APIC_SPIV, 0x1ff);
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
And not the other way around (there are actually a couple of missing writes to software-enable the APIC after
reset_apic(), which I’ll send later).Correcting myself - I understand (now) that you want to reset back to xapic, so that’s fine. Just keep "
apic_write(APIC_SPIV, 0x1ff);” after the reset, please.Ah, will do, I glazed over the
apic_write()and didn't consider its purpose.
Sorry for not being consistent, but actually the apic_reset() seems wrong in general.
enable_x2apic() is used in the context of test_enable_x2apic() to figure out whether x2apic was enabled before. It is actually initially enabled after boot (see cstart64.S). So disabling it here would not be appropriate.
Quoted 64 lines · Nadav Amit
Quoted 64 lines · Sean Christopherson
Quoted 60 lines · Nadav Amit
Quoted 49 lines · Nadav Amit
Quoted 38 lines · Sean Christopherson
Quoted 17 lines · On Mon, Apr 15, 2019 at 11:31:00AM -0700, [email protected] wrote:
From: Nadav Amit <[email protected]>
test_enable_x2apic()unintentionally changes the APIC base address to zero and resets the BSP indication. This actually causes the local APIC to overlap the IDT area, which is wrong. Prevent it from happening by keeping the APIC base address and BSP-bit as it was before.Signed-off-by Nadav Amit <[email protected]>---
x86/apic.c| 4 ++--1 file changed, 2 insertions(+), 2 deletions(-)x86/apic.c +0
-0x86/apic.c static void test_enable_x2apic(void) · line 97
Quoted 10 lines
x86/apic.c +2
-2@@ -90,11 +90,11 @@ static void test_enable_x2apic(void)report ("disabled to x2apic enabled" ,test_write_apicbase_exception (APIC_EN |APIC_EXTD )); -wrmsr (MSR_IA32_APICBASE ,APIC_EN ); +wrmsr (MSR_IA32_APICBASE ,APIC_EN |APIC_DEFAULT_PHYS_BASE |APIC_BSP );report ("apic enabled to invalid state" ,test_write_apicbase_exception (APIC_EXTD )); -wrmsr (MSR_IA32_APICBASE ,APIC_EN |APIC_EXTD ); +wrmsr (MSR_IA32_APICBASE ,APIC_EN |APIC_EXTD |APIC_DEFAULT_PHYS_BASE |APIC_BSP );It probably doesn't matter since AFAIK kvm-unit-tests always uses the default base, but preserving the current base+BSP would be preferred. The #GP tests get away with using
APIC_DEFAULT_PHYS_BASEbecause the WRMSR will never succeed, but even that is poor form. And the test should also reset to xAPIC when it's done.Quoted 1 line
I think the attached patch covers everything.
Thanks. The base is indeed the default, so it should not matter, but your change should make the code more robust.
One important comment regarding your patch:
x86/apic.c static void do_write_apicbase(void *data) · line 104
Quoted 2 lines
x86/apic.c +2
-0+if (!(orig_apicbase &APIC_EXTD )) +reset_apic ();Quoted 8 lines
This is not needed, since
enable_x2apic()will only return true if it succeeded in enabling x2apic. In addition, this is wrong, sincereset_apic()should (usually, and specifically in this case) be followed with:apic_write(APIC_SPIV, 0x1ff);And not the other way around (there are actually a couple of missing writes to software-enable the APIC after
reset_apic(), which I’ll send later).Correcting myself - I understand (now) that you want to reset back to xapic, so that’s fine. Just keep "
apic_write(APIC_SPIV, 0x1ff);” after the reset, please.
Quoted 8 lines
Quoted 1 line
Ah, will do, I glazed over the
apic_write()and didn't consider its purpose.Sorry for not being consistent, but actually the
apic_reset()seems wrong in general.
enable_x2apic()is used in the context oftest_enable_x2apic()to figure out whether x2apic was enabled before. It is actually initially enabled after boot (seecstart64.S). So disabling it here would not be appropriate.
I more or less noticed the same thing. Actually, I noticed we end up in legacy xAPIC and so ended up with:
if (orig_apicbase & APIC_EXTD)
enable_x2apic();
else
reset_apic();Along with a blurb in the changelog stating that reset_apic() is overkill since the vCPU is already in xAPIC.