kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [kvm-unit-tests PATCH] x86: Load segments after GDT loading
@ 2019-06-27 11:23 Nadav Amit
  2019-07-02 15:59 ` Paolo Bonzini
  0 siblings, 1 reply; 2+ messages in thread
From: Nadav Amit @ 2019-06-27 11:23 UTC (permalink / raw)
  To: Paolo Bonzini; +Cc: kvm, Nadav Amit

Whenever we load the GDT, we need to reload the segment selectors so
their hidden data (base, limit, type, etc.) would be reloaded.

It appears that loading GS overwrites the GS bases, so reload GS base
after loading the segment to prevent per-cpu variable corruption.

Signed-off-by: Nadav Amit <nadav.amit@gmail.com>
---
 x86/cstart64.S | 23 +++++++++++++++++------
 1 file changed, 17 insertions(+), 6 deletions(-)

diff --git a/x86/cstart64.S b/x86/cstart64.S
index c5561e7..9791282 100644
--- a/x86/cstart64.S
+++ b/x86/cstart64.S
@@ -118,6 +118,21 @@ MSR_GS_BASE = 0xc0000101
 	wrmsr
 .endm
 
+.macro setup_segments
+	mov $MSR_GS_BASE, %ecx
+	rdmsr
+
+	mov $0x10, %bx
+	mov %bx, %ds
+	mov %bx, %es
+	mov %bx, %fs
+	mov %bx, %gs
+	mov %bx, %ss
+
+	/* restore MSR_GS_BASE */
+	wrmsr
+.endm
+
 .globl start
 start:
 	mov %ebx, mb_boot_info
@@ -149,6 +164,7 @@ switch_to_5level:
 
 prepare_64:
 	lgdt gdt64_desc
+	setup_segments
 
 enter_long_mode:
 	mov %cr4, %eax
@@ -196,12 +212,7 @@ sipi_end:
 
 .code32
 ap_start32:
-	mov $0x10, %ax
-	mov %ax, %ds
-	mov %ax, %es
-	mov %ax, %fs
-	mov %ax, %gs
-	mov %ax, %ss
+	setup_segments
 	mov $-4096, %esp
 	lock/xaddl %esp, smp_stacktop
 	setup_percpu_area
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [kvm-unit-tests PATCH] x86: Load segments after GDT loading
  2019-06-27 11:23 [kvm-unit-tests PATCH] x86: Load segments after GDT loading Nadav Amit
@ 2019-07-02 15:59 ` Paolo Bonzini
  0 siblings, 0 replies; 2+ messages in thread
From: Paolo Bonzini @ 2019-07-02 15:59 UTC (permalink / raw)
  To: Nadav Amit; +Cc: kvm

On 27/06/19 13:23, Nadav Amit wrote:
> Whenever we load the GDT, we need to reload the segment selectors so
> their hidden data (base, limit, type, etc.) would be reloaded.
> 
> It appears that loading GS overwrites the GS bases, so reload GS base
> after loading the segment to prevent per-cpu variable corruption.
> 
> Signed-off-by: Nadav Amit <nadav.amit@gmail.com>
> ---
>  x86/cstart64.S | 23 +++++++++++++++++------
>  1 file changed, 17 insertions(+), 6 deletions(-)
> 
> diff --git a/x86/cstart64.S b/x86/cstart64.S
> index c5561e7..9791282 100644
> --- a/x86/cstart64.S
> +++ b/x86/cstart64.S
> @@ -118,6 +118,21 @@ MSR_GS_BASE = 0xc0000101
>  	wrmsr
>  .endm
>  
> +.macro setup_segments
> +	mov $MSR_GS_BASE, %ecx
> +	rdmsr
> +
> +	mov $0x10, %bx
> +	mov %bx, %ds
> +	mov %bx, %es
> +	mov %bx, %fs
> +	mov %bx, %gs
> +	mov %bx, %ss
> +
> +	/* restore MSR_GS_BASE */
> +	wrmsr
> +.endm
> +
>  .globl start
>  start:
>  	mov %ebx, mb_boot_info
> @@ -149,6 +164,7 @@ switch_to_5level:
>  
>  prepare_64:
>  	lgdt gdt64_desc
> +	setup_segments
>  
>  enter_long_mode:
>  	mov %cr4, %eax
> @@ -196,12 +212,7 @@ sipi_end:
>  
>  .code32
>  ap_start32:
> -	mov $0x10, %ax
> -	mov %ax, %ds
> -	mov %ax, %es
> -	mov %ax, %fs
> -	mov %ax, %gs
> -	mov %ax, %ss
> +	setup_segments
>  	mov $-4096, %esp
>  	lock/xaddl %esp, smp_stacktop
>  	setup_percpu_area
> 

Queued, thanks.

Paolo

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2019-07-02 16:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-06-27 11:23 [kvm-unit-tests PATCH] x86: Load segments after GDT loading Nadav Amit
2019-07-02 15:59 ` Paolo Bonzini

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).