Skip to contentSkip to conversation

x86/S3: put data segment registers into known state upon resume

PATCH In review Jan Beulich 4 participants 8 messages Jul 20, 2020 – Jul 29, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/S3: put data segment registers into known state upon resume

wakeup_32 sets %ds and %es to BOOT_DS, while leaving %fs at what wakeup_start did set it to, and %gs at whatever BIOS did load into it. All of this may end up confusing the first load_segments() to run on the BSP after resume, in particular allowing a non-nul selector value to be left in %fs.

Alongside %ss, also put all other data segment registers into the same state that the boot and CPU bringup paths put them in.

xen/arch/x86/acpi/wakeup_prot.S +10 -0
@@ -52,6 +52,16 @@ ENTRY(s3_resume)
         mov     %eax, %ss
         mov     saved_rsp(%rip), %rsp
 
+        /*
+         * Also put other segment registers into known state, like would
+         * be done on the boot path. This is in particular necessary for
+         * the first load_segments() to work as intended.
+         */
+        mov     %eax, %ds
+        mov     %eax, %es
+        mov     %eax, %fs
+        mov     %eax, %gs
+
         /* Reload code selector */
         pushq   $__HYPERVISOR_CS
         leaq    1f(%rip),%rax
Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · Jan Beulich

wakeup_32 sets %ds and %es to BOOT_DS, while leaving %fs at what wakeup_start did set it to, and %gs at whatever BIOS did load into it. All of this may end up confusing the first load_segments() to run on the BSP after resume, in particular allowing a non-nul selector value to be left in %fs.

Alongside %ss, also put all other data segment registers into the same state that the boot and CPU bringup paths put them in.

I wouldn't mind if the added chunk was placed before loading %ss, so that the context of what's in %eax would be clearer.

Roger.

#2 lore raw
In reply to Jan Beulich
Quoted 15 lines · Jan Beulich

wakeup_32 sets %ds and %es to BOOT_DS, while leaving %fs at what wakeup_start did set it to, and %gs at whatever BIOS did load into it. All of this may end up confusing the first load_segments() to run on the BSP after resume, in particular allowing a non-nul selector value to be left in %fs.

Alongside %ss, also put all other data segment registers into the same state that the boot and CPU bringup paths put them in.

xen/arch/x86/acpi/wakeup_prot.S ENTRY(s3_resume) · line 59

Quoted 8 lines
xen/arch/x86/acpi/wakeup_prot.S +5 -0
@@ -52,6 +52,16 @@ ENTRY(s3_resume)
         mov     %eax, %ss
         mov     saved_rsp(%rip), %rsp
 
+        /*
+         * Also put other segment registers into known state, like would
+         * be done on the boot path. This is in particular necessary for
+         * the first load_segments() to work as intended.
+         */

I don't think the comment is helpful, not least because it refers to a broken behaviour in load_segemnts() which is soon going to change anyway.

We've literally just loaded the GDT, at which point reloading all segments *is* the expected thing to do.

I'd recommend that the diff be simply:

(quoted diff) +0 -0

b/xen/arch/x86/acpi/wakeup_prot.S
index dcc7e2327d..a2c41c4f3f 100644

xen/arch/x86/acpi/wakeup_prot.S +0 -0
@@ -49,6 +49,10 @@ ENTRY(s3_resume)

         mov     %rax, %cr0

         mov     $__HYPERVISOR_DS64, %eax
+        mov     %eax, %ds
+        mov     %eax, %es
+        mov     %eax, %fs
+        mov     %eax, %gs
         mov     %eax, %ss
         mov     saved_rsp(%rip), %rsp

It is a shame that the CR0 load breaks up the obvious connection with lgdt, but IIRC, that was a consequence of how the code was laid out previously.

Preferably with the above diff, Reviewed-by: Andrew Cooper <[email protected]>

#3 lore raw
Quoted 19 lines · Andrew Cooper
Quoted 18 lines · Jan Beulich

wakeup_32 sets %ds and %es to BOOT_DS, while leaving %fs at what wakeup_start did set it to, and %gs at whatever BIOS did load into it. All of this may end up confusing the first load_segments() to run on the BSP after resume, in particular allowing a non-nul selector value to be left in %fs.

Alongside %ss, also put all other data segment registers into the same state that the boot and CPU bringup paths put them in.

xen/arch/x86/acpi/wakeup_prot.S +0 -0
@@ -52,6 +52,16 @@ ENTRY(s3_resume)
         mov     %eax, %ss
         mov     saved_rsp(%rip), %rsp
 
Quoted 8 lines

xen/arch/x86/acpi/wakeup_prot.S ENTRY(s3_resume) · line 59

Quoted 5 lines
xen/arch/x86/acpi/wakeup_prot.S +5 -0
+        /*
+         * Also put other segment registers into known state, like would
+         * be done on the boot path. This is in particular necessary for
+         * the first load_segments() to work as intended.
+         */

I don't think the comment is helpful, not least because it refers to a broken behaviour in load_segemnts() which is soon going to change anyway.

Well, I can drop it. I merely thought I'd be nice and comment my code once in a while (and the comment could be dropped / adjusted when load_segments() changes)...

Quoted 2 lines

We've literally just loaded the GDT, at which point reloading all segments *is* the expected thing to do.

In a way, unless some/all are assumed to already hold a nul selector.

Quoted 9 lines

I'd recommend that the diff be simply:

xen/arch/x86/acpi/wakeup_prot.S +0 -0
index dcc7e2327d..a2c41c4f3f 100644
@@ -49,6 +49,10 @@ ENTRY(s3_resume)

         mov     %rax, %cr0

Quoted 8 lines


         mov     $__HYPERVISOR_DS64, %eax
+        mov     %eax, %ds
+        mov     %eax, %es
+        mov     %eax, %fs
+        mov     %eax, %gs
         mov     %eax, %ss
         mov     saved_rsp(%rip), %rsp

So I had specifically elected to not put the addition there, to make sure the stack would get established first. But seeing both Roger and you ask me to do otherwise - well, so be it then.

Quoted 6 lines

It is a shame that the CR0 load breaks up the obvious connection with lgdt, but IIRC, that was a consequence of how the code was laid out previously.

Preferably with the above diff, Reviewed-by: Andrew Cooper <[email protected]>

Thanks, Jan

#4 lore raw
Quoted 47 lines · Jan Beulich
Quoted 19 lines · Andrew Cooper
Quoted 18 lines · Jan Beulich

wakeup_32 sets %ds and %es to BOOT_DS, while leaving %fs at what wakeup_start did set it to, and %gs at whatever BIOS did load into it. All of this may end up confusing the first load_segments() to run on the BSP after resume, in particular allowing a non-nul selector value to be left in %fs.

Alongside %ss, also put all other data segment registers into the same state that the boot and CPU bringup paths put them in.

xen/arch/x86/acpi/wakeup_prot.S +0 -0
@@ -52,6 +52,16 @@ ENTRY(s3_resume)
         mov     %eax, %ss
         mov     saved_rsp(%rip), %rsp
 
Quoted 7 lines

xen/arch/x86/acpi/wakeup_prot.S ENTRY(s3_resume) · line 59

Quoted 5 lines
xen/arch/x86/acpi/wakeup_prot.S +5 -0
+        /*
+         * Also put other segment registers into known state, like would
+         * be done on the boot path. This is in particular necessary for
+         * the first load_segments() to work as intended.
+         */

I don't think the comment is helpful, not least because it refers to a broken behaviour in load_segemnts() which is soon going to change anyway.

Well, I can drop it. I merely thought I'd be nice and comment my code once in a while (and the comment could be dropped / adjusted when load_segments() changes)...

Quoted 2 lines

We've literally just loaded the GDT, at which point reloading all segments *is* the expected thing to do.

In a way, unless some/all are assumed to already hold a nul selector.

Quoted 12 lines

I'd recommend that the diff be simply:

xen/arch/x86/acpi/wakeup_prot.S +0 -0
index dcc7e2327d..a2c41c4f3f 100644
@@ -49,6 +49,10 @@ ENTRY(s3_resume)

         mov     %rax, %cr0

         mov     $__HYPERVISOR_DS64, %eax
+        mov     %eax, %ds

Quoted 8 lines
Quoted 5 lines

+        mov     %eax, %es
+        mov     %eax, %fs
+        mov     %eax, %gs
         mov     %eax, %ss
         mov     saved_rsp(%rip), %rsp

So I had specifically elected to not put the addition there, to make sure the stack would get established first. But seeing both Roger and you ask me to do otherwise - well, so be it then.

There is no IDT.  Any fault is will be triple, irrespective of the exact code layout.

This sequence actually matches what we have in __high_start().

I don't think it is wise to write code which presumes that __HYPERVISOR_DS64 is 0 (it happens to be, but could easily be 0xe010 as well), or that the trampoline has fixed behaviours for the segments.

~Andrew

#5 lore raw
Quoted 57 lines · Andrew Cooper
Quoted 45 lines · Jan Beulich
Quoted 19 lines · Andrew Cooper
Quoted 18 lines · Jan Beulich

wakeup_32 sets %ds and %es to BOOT_DS, while leaving %fs at what wakeup_start did set it to, and %gs at whatever BIOS did load into it. All of this may end up confusing the first load_segments() to run on the BSP after resume, in particular allowing a non-nul selector value to be left in %fs.

Alongside %ss, also put all other data segment registers into the same state that the boot and CPU bringup paths put them in.

xen/arch/x86/acpi/wakeup_prot.S +0 -0
@@ -52,6 +52,16 @@ ENTRY(s3_resume)
          mov     %eax, %ss
          mov     saved_rsp(%rip), %rsp
 
Quoted 7 lines

xen/arch/x86/acpi/wakeup_prot.S ENTRY(s3_resume) · line 59

Quoted 5 lines
xen/arch/x86/acpi/wakeup_prot.S +5 -0
+        /*
+         * Also put other segment registers into known state, like would
+         * be done on the boot path. This is in particular necessary for
+         * the first load_segments() to work as intended.
+         */

I don't think the comment is helpful, not least because it refers to a broken behaviour in load_segemnts() which is soon going to change anyway.

Well, I can drop it. I merely thought I'd be nice and comment my code once in a while (and the comment could be dropped / adjusted when load_segments() changes)...

Quoted 2 lines

We've literally just loaded the GDT, at which point reloading all segments *is* the expected thing to do.

In a way, unless some/all are assumed to already hold a nul selector.

Quoted 10 lines

I'd recommend that the diff be simply:

xen/arch/x86/acpi/wakeup_prot.S +0 -0
index dcc7e2327d..a2c41c4f3f 100644
@@ -49,6 +49,10 @@ ENTRY(s3_resume)
          mov     %rax, %cr0
 
Quoted 10 lines

xen/arch/x86/acpi/wakeup_prot.S ENTRY(s3_resume) · line 57

Quoted 7 lines
xen/arch/x86/acpi/wakeup_prot.S +4 -0
          mov     $__HYPERVISOR_DS64, %eax
+        mov     %eax, %ds
+        mov     %eax, %es
+        mov     %eax, %fs
+        mov     %eax, %gs
          mov     %eax, %ss
          mov     saved_rsp(%rip), %rsp

So I had specifically elected to not put the addition there, to make sure the stack would get established first. But seeing both Roger and you ask me to do otherwise - well, so be it then.

Quoted 8 lines

There is no IDT.  Any fault is will be triple, irrespective of the exact code layout.

This sequence actually matches what we have in __high_start().

I don't think it is wise to write code which presumes that __HYPERVISOR_DS64 is 0 (it happens to be, but could easily be 0xe010 as well), or that the trampoline has fixed behaviours for the segments.

Hello Jan and Andrew,

Is there anything I can do to help with the delivery/merging of this patch?

If it would help, I can prepare and publish a patch according to Andrew's comments. Given that the patch is not my work though, I assume that it would be appropriate for me credit both of you in the commit message and add a Signed-off-by tag in the commit message for each of you.

Vefa

#6 lore raw
Quoted 66 lines · M. Vefa Bicakci
Quoted 65 lines · Andrew Cooper
Quoted 50 lines · Jan Beulich
Quoted 15 lines · Andrew Cooper
Quoted 14 lines · Jan Beulich

wakeup_32 sets %ds and %es to BOOT_DS, while leaving %fs at what wakeup_start did set it to, and %gs at whatever BIOS did load into it.
All of this may end up confusing the first load_segments() to run on the BSP after resume, in particular allowing a non-nul selector value to be left in %fs.

Alongside %ss, also put all other data segment registers into the same
state that the boot and CPU bringup paths put them in.

Quoted 15 lines
Quoted 4 lines
xen/arch/x86/acpi/wakeup_prot.S +0 -0
@@ -52,6 +52,16 @@ ENTRY(s3_resume)

          mov     %eax, %ss

Quoted 8 lines

          mov     saved_rsp(%rip), %rsp
  +        /*
+         * Also put other segment registers into known state, like would
+         * be done on the boot path. This is in particular necessary for
+         * the first load_segments() to work as intended. +         */

I don't think the comment is helpful, not least because it refers to a broken behaviour in load_segemnts() which is soon going to change anyway.

Well, I can drop it. I merely thought I'd be nice and comment my code once in a while (and the comment could be dropped / adjusted when load_segments() changes)...

Quoted 2 lines

We've literally just loaded the GDT, at which point reloading all segments *is* the expected thing to do.

In a way, unless some/all are assumed to already hold a nul selector.

Quoted 11 lines

I'd recommend that the diff be simply:

xen/arch/x86/acpi/wakeup_prot.S +0 -0
index dcc7e2327d..a2c41c4f3f 100644
@@ -49,6 +49,10 @@ ENTRY(s3_resume)

          mov     %rax, %cr0
            mov     $__HYPERVISOR_DS64, %eax
+        mov     %eax, %ds

Quoted 8 lines
Quoted 5 lines

+        mov     %eax, %es
+        mov     %eax, %fs
+        mov     %eax, %gs
          mov     %eax, %ss
          mov     saved_rsp(%rip), %rsp

So I had specifically elected to not put the addition there, to make sure the stack would get established first. But seeing both Roger and you ask me to do otherwise - well, so be it then.

There is no IDT.  Any fault is will be triple, irrespective of the exact code layout.

This sequence actually matches what we have in __high_start().

Quoted 8 lines
Quoted 3 lines

I don't think it is wise to write code which presumes that __HYPERVISOR_DS64 is 0 (it happens to be, but could easily be 0xe010 as well), or that the trampoline has fixed behaviours for the segments.

Hello Jan and Andrew,

Is there anything I can do to help with the delivery/merging of this patch?

It was committed last Friday.

https://xenbits.xen.org/gitweb/?p=xen.git;a=commitdiff;h=55f8c389d4348cc517946fdcb10794112458e81e

I presume Jan will backport it to stable trees when he's not OoO.

~Andrew

#7 lore raw
Quoted 73 lines · Andrew Cooper
Quoted 72 lines · M. Vefa Bicakci
Quoted 60 lines · Andrew Cooper
Quoted 50 lines · Jan Beulich
Quoted 15 lines · Andrew Cooper
Quoted 14 lines · Jan Beulich

wakeup_32 sets %ds and %es to BOOT_DS, while leaving %fs at what wakeup_start did set it to, and %gs at whatever BIOS did load into it.
All of this may end up confusing the first load_segments() to run on the BSP after resume, in particular allowing a non-nul selector value to be left in %fs.

Alongside %ss, also put all other data segment registers into the same
state that the boot and CPU bringup paths put them in.

Quoted 15 lines
Quoted 7 lines
xen/arch/x86/acpi/wakeup_prot.S +1 -0
@@ -52,6 +52,16 @@ ENTRY(s3_resume)
           mov     %eax, %ss
           mov     saved_rsp(%rip), %rsp
   +        /*
+         * Also put other segment registers into known state,
Quoted 5 lines

like would
+         * be done on the boot path. This is in particular necessary for
+         * the first load_segments() to work as intended. +         */

I don't think the comment is helpful, not least because it refers to a broken behaviour in load_segemnts() which is soon going to change anyway.

Well, I can drop it. I merely thought I'd be nice and comment my code once in a while (and the comment could be dropped / adjusted when load_segments() changes)...

Quoted 2 lines

We've literally just loaded the GDT, at which point reloading all segments *is* the expected thing to do.

In a way, unless some/all are assumed to already hold a nul selector.

Quoted 11 lines

I'd recommend that the diff be simply:

xen/arch/x86/acpi/wakeup_prot.S +1 -0
index dcc7e2327d..a2c41c4f3f 100644
@@ -49,6 +49,10 @@ ENTRY(s3_resume)
           mov     %rax, %cr0
             mov     $__HYPERVISOR_DS64, %eax
+        mov     %eax, %ds
Quoted 8 lines

xen/arch/x86/acpi/wakeup_prot.S ENTRY(s3_resume) · line 56

Quoted 5 lines
xen/arch/x86/acpi/wakeup_prot.S +3 -0
+        mov     %eax, %es
+        mov     %eax, %fs
+        mov     %eax, %gs
           mov     %eax, %ss
           mov     saved_rsp(%rip), %rsp

So I had specifically elected to not put the addition there, to make sure the stack would get established first. But seeing both Roger and you ask me to do otherwise - well, so be it then.

Quoted 8 lines

There is no IDT.  Any fault is will be triple, irrespective of the exact code layout.

This sequence actually matches what we have in __high_start().

I don't think it is wise to write code which presumes that __HYPERVISOR_DS64 is 0 (it happens to be, but could easily be 0xe010 as well), or that the trampoline has fixed behaviours for the segments.

Hello Jan and Andrew,

Quoted 8 lines
Quoted 2 lines

Is there anything I can do to help with the delivery/merging of this patch?

It was committed last Friday.

https://xenbits.xen.org/gitweb/?p=xen.git;a=commitdiff;h=55f8c389d4348cc517946fdcb10794112458e81e

I presume Jan will backport it to stable trees when he's not OoO.

Great -- thanks! (And sorry for not checking the git tree prior to sending my e-mail.)

Vefa