All of lore.kernel.org
 help / color / mirror / Atom feed
From: Frediano Ziglio <freddy77@gmail.com>
To: xen-devel@lists.xenproject.org
Cc: "Frediano Ziglio" <freddy77@gmail.com>,
	"Jan Beulich" <jbeulich@suse.com>,
	"Andrew Cooper" <andrew.cooper3@citrix.com>,
	"Roger Pau Monné" <roger@xenproject.org>,
	"Teddy Astie" <teddy.astie@vates.tech>,
	"Lukasz Hawrylko" <lukasz@hawrylko.pl>,
	"Daniel P. Smith" <dpsmith@apertussolutions.com>,
	"Mateusz Mówka" <mateusz.mowka@intel.com>,
	"Claude Opus 5 . 5" <noreply@anthropic.com>
Subject: [PATCH 11/11] x86/boot: Check the kind of symbols passed to the bootsym helpers
Date: Fri,  4 Dec 2026 20:50:44 +0000	[thread overview]
Message-ID: <20261204205045.596093-12-freddy77@gmail.com> (raw)
In-Reply-To: <20261204205045.596093-1-freddy77@gmail.com>

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@gmail.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---
 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



      parent reply	other threads:[~2026-12-04 20:51 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 20:50 [PATCH 00/11] x86/boot: Improve trampoline code Frediano Ziglio
2026-12-04 20:50 ` [PATCH 01/11] x86/boot: Prevent some possible firmware misbehaviour Frediano Ziglio
2026-12-04 20:50 ` [PATCH 02/11] x86/boot: Avoid PE relocations in the trampoline Frediano Ziglio
2026-12-04 20:50 ` [PATCH 03/11] x86/boot: Check at build time that the trampoline needs no relocations Frediano Ziglio
2026-12-04 20:50 ` [PATCH 04/11] x86/boot: Reduce relocations in the trampoline Frediano Ziglio
2026-12-04 20:50 ` [PATCH 05/11] x86/boot: Share the switch to 64-bit mode between SIPI and S3 paths Frediano Ziglio
2026-12-04 20:50 ` [PATCH 06/11] x86/boot: Use stack to remove a relocation from trampoline code Frediano Ziglio
2026-12-04 20:50 ` [PATCH 07/11] x86/boot: Apply relocations in the trampoline after the copy Frediano Ziglio
2026-12-04 20:50 ` [PATCH 08/11] x86/boot: Group trampoline code and data using subsections Frediano Ziglio
2026-12-04 20:50 ` [PATCH 09/11] x86/boot: Use a writable copy of the trampoline data Frediano Ziglio
2026-12-04 20:50 ` [PATCH 10/11] x86/boot: Assemble the trampoline separately from head.S Frediano Ziglio
2026-12-04 20:50 ` Frediano Ziglio [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261204205045.596093-12-freddy77@gmail.com \
    --to=freddy77@gmail.com \
    --cc=andrew.cooper3@citrix.com \
    --cc=dpsmith@apertussolutions.com \
    --cc=jbeulich@suse.com \
    --cc=lukasz@hawrylko.pl \
    --cc=mateusz.mowka@intel.com \
    --cc=noreply@anthropic.com \
    --cc=roger@xenproject.org \
    --cc=teddy.astie@vates.tech \
    --cc=xen-devel@lists.xenproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.