[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 11/11] x86/boot: Check the kind of symbols passed to the bootsym helpers



bootsym_phys() must be used for code in the trampoline, while
bootsym_data_phys(), and so bootsym(), must be used for data with a
writable copy.  Using the wrong one compiles, but gives a wrong address.

Add is_function(), comparing the type of the address of a symbol with
the type of the symbol after decay, which only match for functions, and
check the symbols at build time.

bootsym_phys() was also used for trampoline_start and trampoline_end,
which are arrays; use trampoline_phys directly for them.

Signed-off-by: Frediano Ziglio <freddy77@xxxxxxxxx>
Co-Authored-By: Claude Opus 5.5 <noreply@xxxxxxxxxxxxx>
---
 xen/arch/x86/include/asm/trampoline.h | 17 ++++++++++++-----
 xen/arch/x86/setup.c                  |  6 ++++--
 xen/arch/x86/tboot.c                  |  2 +-
 3 files changed, 17 insertions(+), 8 deletions(-)

diff --git a/xen/arch/x86/include/asm/trampoline.h 
b/xen/arch/x86/include/asm/trampoline.h
index 37a91c28ca..d15d3c61a1 100644
--- a/xen/arch/x86/include/asm/trampoline.h
+++ b/xen/arch/x86/include/asm/trampoline.h
@@ -111,6 +111,7 @@
 #ifndef __ASSEMBLER__
 
 #include <xen/compiler.h>
+#include <xen/macros.h>
 #include <xen/types.h>
 
 /*
@@ -141,21 +142,27 @@ extern char trampoline_data_base[];
  */
 extern uint32_t trampoline_phys;
 
+/* Whether x is a function, as opposed to an object. */
+#define is_function(x) \
+    __builtin_types_compatible_p(typeof(&(x)), typeof((void)0, (x)))
+
 /*
  * Calculate the physical address of a symbol in the trampoline.
  *
- * bootsym_phys() is for code symbols (entry points, trampoline_start[] and
- * trampoline_end[]), bootsym_data_phys() for data with a writable copy
- * (defined with GLOBAL_COPY()).  Specifying other symbols will compile but
+ * bootsym_phys() is for functions (the entry points), bootsym_data_phys() for
+ * data with a writable copy (defined with GLOBAL_COPY()), which is checked at
+ * build time.  Specifying other symbols of the right kind will compile but
  * malfunction when used, as will using these helpers before the trampoline
  * is placed.
  */
 #define bootsym_phys(sym)                                       \
-    (trampoline_phys + ((unsigned long)&(sym) -                 \
+    (BUILD_BUG_ON_ZERO(!is_function(sym)) +                     \
+     trampoline_phys + ((unsigned long)&(sym) -                 \
                         (unsigned long)trampoline_start))
 
 #define bootsym_data_phys(sym)                                  \
-    (trampoline_phys + ((unsigned long)&(sym) -                 \
+    (BUILD_BUG_ON_ZERO(is_function(sym)) +                      \
+     trampoline_phys + ((unsigned long)&(sym) -                 \
                         (unsigned long)trampoline_data_base))
 
 /* Given a trampoline data symbol, construct a pointer to it in the directmap. 
*/
diff --git a/xen/arch/x86/setup.c b/xen/arch/x86/setup.c
index 17f4f9de4f..33f975c328 100644
--- a/xen/arch/x86/setup.c
+++ b/xen/arch/x86/setup.c
@@ -2266,11 +2266,13 @@ __initcall(init_xen_cap_info);
 
 int __hwdom_init remove_xen_ranges(struct rangeset *r)
 {
+    unsigned long tramp_end =
+        trampoline_phys + (trampoline_end - trampoline_start);
     int rc;
 
     /* S3 resume code (and other real mode trampoline code) */
-    rc = rangeset_remove_range(r, PFN_DOWN(bootsym_phys(trampoline_start)),
-                               PFN_DOWN(bootsym_phys(trampoline_end) - 1));
+    rc = rangeset_remove_range(r, PFN_DOWN(trampoline_phys),
+                               PFN_DOWN(tramp_end - 1));
     if ( rc )
         return rc;
 
diff --git a/xen/arch/x86/tboot.c b/xen/arch/x86/tboot.c
index 180da67d6c..576131603d 100644
--- a/xen/arch/x86/tboot.c
+++ b/xen/arch/x86/tboot.c
@@ -328,7 +328,7 @@ void tboot_shutdown(uint32_t shutdown_type)
          */
         g_tboot_shared->num_mac_regions = 3;
         /* S3 resume code (and other real mode trampoline code) */
-        g_tboot_shared->mac_regions[0].start = bootsym_phys(trampoline_start);
+        g_tboot_shared->mac_regions[0].start = trampoline_phys;
         g_tboot_shared->mac_regions[0].size = trampoline_end - 
trampoline_start;
         /* hypervisor .text + .rodata */
         g_tboot_shared->mac_regions[1].start = (uint64_t)__pa(&_stext);
-- 
2.43.0




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.