cpu/x86/mp_init.c: Generate a C header to get start32 offset

In the current design the relocatable parameters are used to know the
offset of the 32bit startpoint. This requires back and forward
interaction between the stub, the loader and the mp init code. This
makes the code hard to read.

This is static information known at buildtime, so a better way to deal
with this is to generate a header that contains this offset.

Change-Id: Ic01badd2af11a6e1dbc27c8e928916fedf104b5b
Signed-off-by: Arthur Heymans <arthur@aheymans.xyz>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/64625
Reviewed-by: Patrick Rudolph <siro@das-labor.org>
Reviewed-by: Maximilian Brune <maximilian.brune@9elements.com>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
diff --git a/src/cpu/x86/Makefile.inc b/src/cpu/x86/Makefile.inc
index 3ef3a90..2381b6f 100644
--- a/src/cpu/x86/Makefile.inc
+++ b/src/cpu/x86/Makefile.inc
@@ -8,6 +8,20 @@
 subdirs-y += cache
 
 subdirs-$(CONFIG_PARALLEL_MP) += name
+
+ifeq ($(CONFIG_HAVE_SMI_HANDLER),y)
+$(obj)/ramstage/cpu/x86/smm_start32_offset.h: $(dir)/smm_start32_offset.h.template $(obj)/smmstub/smmstub.elf
+	cp $< $@.temp
+	sed -i 's/##START32_OFFSET##/0x'$$($(NM_smmstub) -an $(obj)/smmstub/smmstub.elf | grep smm_trampolin | cut -d' ' -f1)'/' $@.temp
+	mv $@.temp $@
+else
+$(obj)/ramstage/cpu/x86/smm_start32_offset.h: $(dir)/smm_start32_offset.h.template
+	cp $< $@.temp
+	sed -i 's/##START32_OFFSET##/0x0/' $@.temp
+	mv $@.temp $@
+endif
+
+$(call src-to-obj,ramstage,$(dir)/mp_init.c): $(obj)/ramstage/cpu/x86/smm_start32_offset.h
 ramstage-$(CONFIG_PARALLEL_MP) += mp_init.c
 
 ramstage-y += backup_default_smm.c
diff --git a/src/cpu/x86/mp_init.c b/src/cpu/x86/mp_init.c
index 2909022..01ca64e 100644
--- a/src/cpu/x86/mp_init.c
+++ b/src/cpu/x86/mp_init.c
@@ -24,6 +24,9 @@
 #include <thread.h>
 #include <types.h>
 
+/* Generated header */
+#include <ramstage/cpu/x86/smm_start32_offset.h>
+
 #include <security/intel/stm/SmmStm.h>
 
 struct mp_callback {
@@ -672,7 +675,6 @@
 	uintptr_t perm_smbase;
 	size_t perm_smsize;
 	size_t smm_save_state_size;
-	uintptr_t reloc_start32_offset;
 	bool do_smm;
 } mp_state;
 
@@ -738,7 +740,7 @@
 		stm_setup(mseg, p->cpu,
 				perm_smbase,
 				mp_state.perm_smbase,
-				mp_state.reloc_start32_offset);
+				SMM_START32_OFFSET);
 	}
 }
 
@@ -770,8 +772,6 @@
 	}
 	adjust_smm_apic_id_map(&smm_params);
 
-	mp_state.reloc_start32_offset = smm_params.stub_params->start32_offset;
-
 	return CB_SUCCESS;
 }
 
diff --git a/src/cpu/x86/smm/smm_module_loader.c b/src/cpu/x86/smm/smm_module_loader.c
index 6452707..6cd9956 100644
--- a/src/cpu/x86/smm/smm_module_loader.c
+++ b/src/cpu/x86/smm/smm_module_loader.c
@@ -277,8 +277,6 @@
 	printk(BIOS_DEBUG, "%s: stack_top = 0x%x\n", __func__, stub_params->stack_top);
 	printk(BIOS_DEBUG, "%s: per cpu stack_size = 0x%x\n", __func__,
 	       stub_params->stack_size);
-	printk(BIOS_DEBUG, "%s: runtime.start32_offset = 0x%x\n", __func__,
-	       stub_params->start32_offset);
 	printk(BIOS_DEBUG, "%s: runtime.smm_size = 0x%zx\n", __func__, smm_size);
 
 	smm_stub_place_staggered_entry_points(params);
diff --git a/src/cpu/x86/smm/smm_stub.S b/src/cpu/x86/smm/smm_stub.S
index 02532a4..19e9c50 100644
--- a/src/cpu/x86/smm/smm_stub.S
+++ b/src/cpu/x86/smm/smm_stub.S
@@ -31,9 +31,6 @@
  * into the table. */
 apic_to_cpu_num:
 .fill CONFIG_MAX_CPUS,2,0xffff
-/* allows the STM to bring up SMM in 32-bit mode */
-start32_offset:
-.long smm_trampoline32 - _start
 
 .data
 /* Provide fallback stack to use when a valid CPU number cannot be found. */
diff --git a/src/cpu/x86/smm_start32_offset.h.template b/src/cpu/x86/smm_start32_offset.h.template
new file mode 100644
index 0000000..023bca7
--- /dev/null
+++ b/src/cpu/x86/smm_start32_offset.h.template
@@ -0,0 +1,9 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#ifndef _SMM_START32_OFFSET_H
+#define _SMM_START32_OFFSET_H
+
+/* This gets filled in after building the SMM stub */
+#define SMM_START32_OFFSET ##START32_OFFSET##
+
+#endif
diff --git a/src/include/cpu/x86/smm.h b/src/include/cpu/x86/smm.h
index d281972..efafa53 100644
--- a/src/include/cpu/x86/smm.h
+++ b/src/include/cpu/x86/smm.h
@@ -107,8 +107,6 @@
 	 * contiguous like the 1:1 mapping it is up to the caller of the stub
 	 * loader to adjust this mapping. */
 	u16 apic_id_to_cpu[CONFIG_MAX_CPUS];
-	/* STM's 32bit entry into SMI handler */
-	u32 start32_offset;
 } __packed;
 
 /* smm_handler_t is called with arg of smm_module_params pointer. */