Skip to contentSkip to conversation

x86/boot: Don't return encryption mask from __startup_64()

PATCH RESEND Superseded Khalid Ali 2 participants 2 messages Jun 25, 2025 – Jun 30, 2025 x86

v5 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

Khalid Ali

author
#0 lore raw

PATCH RESEND v5 1/1 x86/boot: Don't return encryption mask from __startup_64()

From: Khalid Ali <[email protected]>

Currently, __startup_64() returns encryption mask to the caller, however caller can directly access the encryption.

The C code can access encryption by including arch/x86/include/asm/setup.h and calling sme_get_me_mask(). The assembly code can access directly via "sme_me_mask" variable.

This patches accounts that, by adjusting __startup_64() to not return encryption mask, and update startup_64() to access "sme_me_mask" only if CONFIG_AMD_MEM_ENCRYPT is set.

This cleans up the function and does seperation of concern. __startup_64() should focus on action like encrypting the kernel, and let the caller retrieve the mask directly.

CHanges in v5:
* Improve commit message for better clarity.
* Fix some issues returned by kernel test robot.
* Add Huang, Kai Ack tag.

---
 arch/x86/boot/startup/map_kernel.c | 11 +++--------
 arch/x86/include/asm/setup.h       |  2 +-
 arch/x86/kernel/head_64.S          |  8 +++-----
 3 files changed, 7 insertions(+), 14 deletions(-)
arch/x86/boot/startup/map_kernel.c +3 -8
index 332dbe6688c4..0425d49be16e 100644
@@ -30,7 +30,7 @@ static inline bool check_la57_support(void)
 	return true;
 }
 
-static unsigned long __head sme_postprocess_startup(struct boot_params *bp,
+static void __head sme_postprocess_startup(struct boot_params *bp,
 						    pmdval_t *pmd,
 						    unsigned long p2v_offset)
 {
@@ -68,11 +68,6 @@ static unsigned long __head sme_postprocess_startup(struct boot_params *bp,
 		}
 	}
 
-	/*
-	 * Return the SME encryption mask (if SME is active) to be used as a
-	 * modifier for the initial pgdir entry programmed into CR3.
-	 */
-	return sme_get_me_mask();
 }
 
 /*
@@ -84,7 +79,7 @@ static unsigned long __head sme_postprocess_startup(struct boot_params *bp,
  * the 1:1 mapping of memory. Kernel virtual addresses can be determined by
  * subtracting p2v_offset from the RIP-relative address.
  */
-unsigned long __head __startup_64(unsigned long p2v_offset,
+void __head __startup_64(unsigned long p2v_offset,
 				  struct boot_params *bp)
 {
 	pmd_t (*early_pgts)[PTRS_PER_PMD] = rip_rel_ptr(early_dynamic_pgts);
@@ -213,5 +208,5 @@ unsigned long __head __startup_64(unsigned long p2v_offset,
 	for (; i < PTRS_PER_PMD; i++)
 		pmd[i] &= ~_PAGE_PRESENT;
 
-	return sme_postprocess_startup(bp, pmd, p2v_offset);
+	sme_postprocess_startup(bp, pmd, p2v_offset);
 }
arch/x86/include/asm/setup.h +1 -1
index 692af46603a1..29ea24bb85ff 100644
@@ -50,7 +50,7 @@ extern unsigned long acpi_realmode_flags;
 
 extern void reserve_standard_io_resources(void);
 extern void i386_reserve_resources(void);
-extern unsigned long __startup_64(unsigned long p2v_offset, struct boot_params *bp);
+extern void __startup_64(unsigned long p2v_offset, struct boot_params *bp);
 extern void startup_64_setup_gdt_idt(void);
 extern void startup_64_load_idt(void *vc_handler);
 extern void early_setup_idt(void);
arch/x86/kernel/head_64.S +3 -5
index 4d654ed7982e..c46a175a2a12 100644
@@ -106,18 +106,16 @@ SYM_CODE_START_NOALIGN(startup_64)
 
 	/*
 	 * Perform pagetable fixups. Additionally, if SME is active, encrypt
-	 * the kernel and retrieve the modifier (SME encryption mask if SME
-	 * is active) to be added to the initial pgdir entry that will be
-	 * programmed into CR3.
+	 * the kernel.
 	 */
 	movq	%r15, %rsi
 	call	__startup_64
 
 	/* Form the CR3 value being sure to include the CR3 modifier */
-	leaq	early_top_pgt(%rip), %rcx
-	addq	%rcx, %rax
+	leaq	early_top_pgt(%rip), %rax
 
 #ifdef CONFIG_AMD_MEM_ENCRYPT
+	addq	sme_me_mask(%rip), %rax
 	mov	%rax, %rdi
 
 	/*
Signature · 1 line
2.49.0
#1 lore raw

Khalid!

Quoted 10 lines · Khalid Ali

From: Khalid Ali <[email protected]>

Currently, __startup_64() returns encryption mask to the caller, however caller can directly access the encryption.

The C code can access encryption by including arch/x86/include/asm/setup.h and calling sme_get_me_mask(). The assembly code can access directly via "sme_me_mask" variable.

This patches accounts that, by adjusting __startup_64() to not return

This patch.... I pointed you to the tip tree documentation before ....

Quoted 11 lines

encryption mask, and update startup_64() to access "sme_me_mask" only if CONFIG_AMD_MEM_ENCRYPT is set.

This cleans up the function and does seperation of concern. __startup_64() should focus on action like encrypting the kernel, and let the caller retrieve the mask directly.

CHanges in v5:
* Improve commit message for better clarity.
* Fix some issues returned by kernel test robot.
* Add Huang, Kai Ack tag.

Please put this behind the --- seperator. It's not part of the change log. That's documented too.

Quoted 13 lines
---
 arch/x86/boot/startup/map_kernel.c | 11 +++--------
 arch/x86/include/asm/setup.h       |  2 +-
 arch/x86/kernel/head_64.S          |  8 +++-----
 3 files changed, 7 insertions(+), 14 deletions(-)
arch/x86/boot/startup/map_kernel.c +0 -0
index 332dbe6688c4..0425d49be16e 100644

arch/x86/boot/startup/map_kernel.c static inline bool check_la57_support(void) · line 35

Quoted 7 lines
arch/x86/boot/startup/map_kernel.c +1 -1
@@ -30,7 +30,7 @@ static inline bool check_la57_support(void)
 	return true;
 }
 
-static unsigned long __head sme_postprocess_startup(struct boot_params *bp,
+static void __head sme_postprocess_startup(struct boot_params *bp,
 						    pmdval_t *pmd,
 						    unsigned long p2v_offset)

See documentation about parameter alignment.

Quoted 14 lines
arch/x86/boot/startup/map_kernel.c +0 -5
 {
@@ -68,11 +68,6 @@ static unsigned long __head sme_postprocess_startup(struct boot_params *bp,
 		}
 	}
 
-	/*
-	 * Return the SME encryption mask (if SME is active) to be used as a
-	 * modifier for the initial pgdir entry programmed into CR3.
-	 */
-	return sme_get_me_mask();
 }
 
 /*

arch/x86/boot/startup/map_kernel.c static unsigned long __head sme_postprocess_startup(struct boot_params *bp, · line 83

Quoted 6 lines
arch/x86/boot/startup/map_kernel.c +1 -1
@@ -84,7 +79,7 @@ static unsigned long __head sme_postprocess_startup(struct boot_params *bp,
  * the 1:1 mapping of memory. Kernel virtual addresses can be determined by
  * subtracting p2v_offset from the RIP-relative address.
  */
-unsigned long __head __startup_64(unsigned long p2v_offset,
+void __head __startup_64(unsigned long p2v_offset,
 				  struct boot_params *bp)

Ditto

arch/x86/boot/startup/map_kernel.c static unsigned long __head sme_postprocess_startup(struct boot_params *bp,

Quoted 6 lines
arch/x86/boot/startup/map_kernel.c +1 -3
 	/*
 	 * Perform pagetable fixups. Additionally, if SME is active, encrypt
-	 * the kernel and retrieve the modifier (SME encryption mask if SME
-	 * is active) to be added to the initial pgdir entry that will be
-	 * programmed into CR3.
+	 * the kernel.

Why are you dropping valuable information from that comment, instead of moving the important information about the SME mask next to the code which retrieves it?

Thanks,

tglx