Skip to contentSkip to conversation

selftests/sgx: Use rip relative addressing for encl_stack

PATCH In review Jarkko Sakkinen 2 participants 3 messages Mar 30, 2022 – Mar 31, 2022

Subsystems (2)
lore
Share & tools
lore

Jarkko Sakkinen

author maintainer
#0 lore raw

PATCH v2 1/1 selftests/sgx: Use rip relative addressing for encl_stack

Simplify the test_encl_bootstrap.S flow by using RIP-relative addressing. The compiler automatically puts relative addresses for RIP index addresses.

In order to get a clean and tweakless solution, define separate entry point for each TCS.

Cc Reinette Chatre <[email protected]>
Cc Dave Hansen <[email protected]>

---
v2:
* Based on Reinette's example, make proper structuring with separate entry points for each TCS.

---
 .../selftests/sgx/test_encl_bootstrap.S       | 30 +++++++++++--------
 1 file changed, 18 insertions(+), 12 deletions(-)
tools/testing/selftests/sgx/test_encl_bootstrap.S +18 -12
index 82fb0dfcbd23..cc2353f38bcc 100644
@@ -10,12 +10,13 @@
 	.section ".tcs", "aw"
 	.balign	4096
 
+encl_tcs1:
 	.fill	1, 8, 0			# STATE (set by CPU)
 	.fill	1, 8, 0			# FLAGS
 	.quad	encl_ssa_tcs1		# OSSA
 	.fill	1, 4, 0			# CSSA (set by CPU)
 	.fill	1, 4, 1			# NSSA
-	.quad	encl_entry		# OENTRY
+	.quad	encl_entry1		# OENTRY
 	.fill	1, 8, 0			# AEP (set by EENTER and ERESUME)
 	.fill	1, 8, 0			# OFSBASE
 	.fill	1, 8, 0			# OGSBASE
@@ -23,13 +24,13 @@
 	.fill	1, 4, 0xFFFFFFFF	# GSLIMIT
 	.fill	4024, 1, 0		# Reserved
 
-	# TCS2
+encl_tcs2:
 	.fill	1, 8, 0			# STATE (set by CPU)
 	.fill	1, 8, 0			# FLAGS
 	.quad	encl_ssa_tcs2		# OSSA
 	.fill	1, 4, 0			# CSSA (set by CPU)
 	.fill	1, 4, 1			# NSSA
-	.quad	encl_entry		# OENTRY
+	.quad	encl_entry2		# OENTRY
 	.fill	1, 8, 0			# AEP (set by EENTER and ERESUME)
 	.fill	1, 8, 0			# OFSBASE
 	.fill	1, 8, 0			# OGSBASE
@@ -39,15 +40,19 @@
 
 	.text
 
-encl_entry:
-	# RBX contains the base address for TCS, which is the first address
-	# inside the enclave for TCS #1 and one page into the enclave for
-	# TCS #2. By adding the value of encl_stack to it, we get
-	# the absolute address for the stack.
-	lea	(encl_stack)(%rbx), %rax
+encl_entry1:
+	lea	(encl_stack1)(%rip), %rax
 	xchg	%rsp, %rax
 	push	%rax
+	jmp	encl_continue
 
+encl_entry2:
+	lea	(encl_stack2)(%rip), %rax
+	xchg	%rsp, %rax
+	push	%rax
+	jmp	encl_continue
+
+encl_continue:
 	push	%rcx # push the address after EENTER
 	push	%rbx # push the enclave base address
 
@@ -84,13 +89,14 @@ encl_entry:
 
 encl_ssa_tcs1:
 	.space 4096
+
 encl_ssa_tcs2:
 	.space 4096
 
+encl_stack1:
 	.balign 4096
-	# Stack of TCS #1
 	.space 4096
-encl_stack:
+
+encl_stack2:
 	.balign 4096
-	# Stack of TCS #2
 	.space 4096
Signature · 1 line
2.35.1
#1 lore raw

Hi Jarkko,

Quoted 1 line · Jarkko Sakkinen

Simplify the test_encl_bootstrap.S flow by using RIP-relative addressing.

It is not clear to me how this is simpler. At this point there is no functional change (except for what appears to be an unintended bug - more below). At this time the change seems more code utilizing subtle compiler features to accomplish the same.

Could you please share more about your plans following this change? I need to understand this better since it is also an area changed by the SGX2 testing code.

Quoted 1 line

The compiler automatically puts relative addresses for RIP index addresses.

I was not aware of this. A comment would be helpful to understand the implementation.

Quoted 62 lines

In order to get a clean and tweakless solution, define separate entry point for each TCS.

Cc Reinette Chatre <[email protected]>
Cc Dave Hansen <[email protected]>

---
v2:
* Based on Reinette's example, make proper structuring with separate entry points for each TCS.

---
 .../selftests/sgx/test_encl_bootstrap.S       | 30 +++++++++++--------
 1 file changed, 18 insertions(+), 12 deletions(-)
tools/testing/selftests/sgx/test_encl_bootstrap.S +6 -9
index 82fb0dfcbd23..cc2353f38bcc 100644
@@ -10,12 +10,13 @@
 	.section ".tcs", "aw"
 	.balign	4096
 
+encl_tcs1:
 	.fill	1, 8, 0			# STATE (set by CPU)
 	.fill	1, 8, 0			# FLAGS
 	.quad	encl_ssa_tcs1		# OSSA
 	.fill	1, 4, 0			# CSSA (set by CPU)
 	.fill	1, 4, 1			# NSSA
-	.quad	encl_entry		# OENTRY
+	.quad	encl_entry1		# OENTRY
 	.fill	1, 8, 0			# AEP (set by EENTER and ERESUME)
 	.fill	1, 8, 0			# OFSBASE
 	.fill	1, 8, 0			# OGSBASE
@@ -23,13 +24,13 @@
 	.fill	1, 4, 0xFFFFFFFF	# GSLIMIT
 	.fill	4024, 1, 0		# Reserved
 
-	# TCS2
+encl_tcs2:
 	.fill	1, 8, 0			# STATE (set by CPU)
 	.fill	1, 8, 0			# FLAGS
 	.quad	encl_ssa_tcs2		# OSSA
 	.fill	1, 4, 0			# CSSA (set by CPU)
 	.fill	1, 4, 1			# NSSA
-	.quad	encl_entry		# OENTRY
+	.quad	encl_entry2		# OENTRY
 	.fill	1, 8, 0			# AEP (set by EENTER and ERESUME)
 	.fill	1, 8, 0			# OFSBASE
 	.fill	1, 8, 0			# OGSBASE
@@ -39,15 +40,19 @@
 
 	.text
 
-encl_entry:
-	# RBX contains the base address for TCS, which is the first address
-	# inside the enclave for TCS #1 and one page into the enclave for
-	# TCS #2. By adding the value of encl_stack to it, we get
-	# the absolute address for the stack.
-	lea	(encl_stack)(%rbx), %rax
+encl_entry1:
+	lea	(encl_stack1)(%rip), %rax

tools/testing/selftests/sgx/test_encl_bootstrap.S line 54

Quoted 10 lines
tools/testing/selftests/sgx/test_encl_bootstrap.S +7 -0
 	xchg	%rsp, %rax
 	push	%rax
+	jmp	encl_continue
 
+encl_entry2:
+	lea	(encl_stack2)(%rip), %rax
+	xchg	%rsp, %rax
+	push	%rax
+	jmp	encl_continue
+

The code duplication (xchg and push) is not needed.

tools/testing/selftests/sgx/test_encl_bootstrap.S encl_entry: · line 96

Quoted 13 lines
tools/testing/selftests/sgx/test_encl_bootstrap.S +3 -0
+encl_continue:
 	push	%rcx # push the address after EENTER
 	push	%rbx # push the enclave base address
 
@@ -84,13 +89,14 @@ encl_entry:
 
 encl_ssa_tcs1:
 	.space 4096
+
 encl_ssa_tcs2:
 	.space 4096
 
+encl_stack1:

Stack grows the other way so by placing the entry here the stack of TCS #1 will clobber the SSA of TCS #2.

tools/testing/selftests/sgx/test_encl_bootstrap.S encl_entry: · line 100

Quoted 6 lines
tools/testing/selftests/sgx/test_encl_bootstrap.S +2 -2
 	.balign 4096
-	# Stack of TCS #1
 	.space 4096
-encl_stack:
+
+encl_stack2:

Here the stack of TCS #2 will actually use the stack of TCS #1.

tools/testing/selftests/sgx/test_encl_bootstrap.S encl_entry: · line 102

Quoted 3 lines
tools/testing/selftests/sgx/test_encl_bootstrap.S +0 -1
 	.balign 4096
-	# Stack of TCS #2
 	.space 4096

Last page will be unused.

Reinette

author maintainer
#2 lore raw
Quoted 119 lines · Reinette Chatre

Hi Jarkko,

Quoted 1 line · Jarkko Sakkinen

Simplify the test_encl_bootstrap.S flow by using RIP-relative addressing.

It is not clear to me how this is simpler. At this point there is no functional change (except for what appears to be an unintended bug - more below). At this time the change seems more code utilizing subtle compiler features to accomplish the same.

Could you please share more about your plans following this change? I need to understand this better since it is also an area changed by the SGX2 testing code.

Quoted 1 line

The compiler automatically puts relative addresses for RIP index addresses.

I was not aware of this. A comment would be helpful to understand the implementation.

Quoted 66 lines

In order to get a clean and tweakless solution, define separate entry point for each TCS.

Cc Reinette Chatre <[email protected]>
Cc Dave Hansen <[email protected]>

---
v2:
* Based on Reinette's example, make proper structuring with separate entry points for each TCS.
---
 .../selftests/sgx/test_encl_bootstrap.S       | 30 +++++++++++--------

 1 file changed, 18 insertions(+), 12 deletions(-)
tools/testing/selftests/sgx/test_encl_bootstrap.S +0 -0
index 82fb0dfcbd23..cc2353f38bcc 100644
@@ -10,12 +10,13 @@

        .section ".tcs", "aw"
        .balign 4096

+encl_tcs1:
        .fill   1, 8, 0                 # STATE (set by CPU) .fill   1, 8, 0                 # FLAGS
        .quad   encl_ssa_tcs1           # OSSA
        .fill   1, 4, 0                 # CSSA (set by CPU) .fill   1, 4, 1                 # NSSA
-       .quad   encl_entry              # OENTRY
+       .quad   encl_entry1             # OENTRY
        .fill   1, 8, 0                 # AEP (set by EENTER and ERESUME) .fill   1, 8, 0                 # OFSBASE
        .fill   1, 8, 0                 # OGSBASE

tools/testing/selftests/sgx/test_encl_bootstrap.S +0 -0
@@ -23,13 +24,13 @@

        .fill   1, 4, 0xFFFFFFFF        # GSLIMIT
        .fill   4024, 1, 0              # Reserved

-       # TCS2
+encl_tcs2:
        .fill   1, 8, 0                 # STATE (set by CPU) .fill   1, 8, 0                 # FLAGS
        .quad   encl_ssa_tcs2           # OSSA
        .fill   1, 4, 0                 # CSSA (set by CPU) .fill   1, 4, 1                 # NSSA
-       .quad   encl_entry              # OENTRY
+       .quad   encl_entry2             # OENTRY
        .fill   1, 8, 0                 # AEP (set by EENTER and ERESUME) .fill   1, 8, 0                 # OFSBASE
        .fill   1, 8, 0                 # OGSBASE

tools/testing/selftests/sgx/test_encl_bootstrap.S +0 -0
@@ -39,15 +40,19 @@


        .text

tools/testing/selftests/sgx/test_encl_bootstrap.S +2 -6
-encl_entry:
-       # RBX contains the base address for TCS, which is the first address
-       # inside the enclave for TCS #1 and one page into the enclave for
-       # TCS #2. By adding the value of encl_stack to it, we get
-       # the absolute address for the stack.
-       lea     (encl_stack)(%rbx), %rax
+encl_entry1:
+       lea     (encl_stack1)(%rip), %rax

        xchg    %rsp, %rax
        push    %rax
+       jmp     encl_continue

tools/testing/selftests/sgx/test_encl_bootstrap.S line 54

Quoted 6 lines
tools/testing/selftests/sgx/test_encl_bootstrap.S +6 -0
+encl_entry2:
+       lea     (encl_stack2)(%rip), %rax
+       xchg    %rsp, %rax
+       push    %rax
+       jmp     encl_continue
+

The code duplication (xchg and push) is not needed.

Quoted 5 lines

+encl_continue:
        push    %rcx # push the address after EENTER push    %rbx # push the enclave base address

tools/testing/selftests/sgx/test_encl_bootstrap.S +0 -0
@@ -84,13 +89,14 @@ encl_entry:
Quoted 8 lines


 encl_ssa_tcs1:
        .space 4096
+
 encl_ssa_tcs2:
        .space 4096

+encl_stack1:

Stack grows the other way so by placing the entry here the stack of TCS #1 will clobber the SSA of TCS #2.

tools/testing/selftests/sgx/test_encl_bootstrap.S encl_entry: · line 100

Quoted 6 lines

        .balign 4096
-       # Stack of TCS #1
        .space 4096

tools/testing/selftests/sgx/test_encl_bootstrap.S +2 -1
-encl_stack:
+
+encl_stack2:

Here the stack of TCS #2 will actually use the stack of TCS #1.

Quoted 7 lines
Quoted 3 lines

        .balign 4096
-       # Stack of TCS #2
        .space 4096

Last page will be unused.

Reinette

Thanks for the remarks. I'll fix the issues.

BR, Jarkko