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

Re: [PATCH 09/22] x86/traps: Move load_system_tables() into traps-setup.c


  • To: Jan Beulich <jbeulich@xxxxxxxx>
  • From: Nicola Vetrini <nicola.vetrini@xxxxxxxxxxx>
  • Date: Fri, 15 Aug 2025 10:40:12 +0200
  • Arc-authentication-results: i=1; bugseng.com; arc=none smtp.remote-ip=162.55.131.47
  • Arc-message-signature: i=1; d=bugseng.com; s=openarc; a=rsa-sha256; c=relaxed/relaxed; t=1755247212; h=DKIM-Signature:MIME-Version:Date:From:To:Cc:Subject:In-Reply-To: References:Message-ID:X-Sender:Organization:Content-Type: Content-Transfer-Encoding; bh=y3MHDgRKmuqaGju4Zy5thsD6qx9/BTglp6ydF5MPld0=; b=icQ2liYMAjXEeeakC12qL+jXR1qMQd8DTU6JVcNL+M4UbCU7lXLQkYb/xOv7b24a0LDt fxU7Ku8Ed9qvEowvVtrZ4epQq3C6Y3HcH7fdFXHdtNg4K2xGxKD7YsRrlHHGkjtdZejEf sB1ndDK01IGj8jYheEz5yX9HrNfLbSHct0TJwKzLU18VZDzAWjZSYBXrJ99RxC1hP7FE7 oTi5Ip/2IEJFH/voJXCHCCLGsG1dhe8Vm+OepfTYTVa3hys3bUL41GbUJJ9dRVR3AVWZ7 VrR7Yk+9GWKmqKwG9dIE43sIDiAyxAmaA1fTkDFRlR/DwkO131yPtTbDJJzDhY9sh9tih 5QWgGkbWUI2MRxJt7B1+cmV3Nr0+wA3XTuVyjJ7J5tKwv5tWOBJmKI0W+UdJ14DbTaubP 0YWNY3WSD3Vzx3D5iyf863cKhSrzQFgPEwit/HOoj5E6sytt3+oWG6ywbco3wn/cWBJYB UKlQdfMVwkpg/kambcZUYZ2aUd9z1vnrD1rtoMWA2XZG+jutSW5OcA7yFi7rnMKVjUj8B rWCfgQSmP5LUxCOKtyXVYVdMxU/QwEjFiyDXI0//t94xzPXO4NoHFTflxKDysdzi+KzOT uySEcPGiXVaiCPI2q38tT8IDGkCbTXQk3kUE7jyaLWQeHD3Y6wlEN+GtXTHe6CQ=
  • Arc-seal: i=1; d=bugseng.com; s=openarc; a=rsa-sha256; cv=none; t=1755247212; b=cEk+yBtUZ7MIO9XLbsl8uG1LqBJNlQSZw7CmcA7Uz0kc1K/B5r17T/ZI+/6ev38MGgrd zoSyhGzJRqUgbynffIpD3wOOreVIUjSMffkFrDiFY7QXLrxtCwnJelXrTvHxD7k8+Qq0Q 6I//vqM82c3CMprYph2PpOYFs7i/NNJyYCkW8rp9DIu9c2P50vo4RkPICMN/1vtA5Y2QL MMTQLHxGHW5Jgdo8oJUw00TaOlEyniQEL/5TVO30MZb5oXSR+pLz4aVvnbp+JqhmSpCv7 k112opJxqbL6xMMqkKDzU7kwhD7mkJv0fzCXt5AtWepCf+dDVgGbBPhoxMZwfltMNsG9/ J1b1Dwo6UnphK8NZyxB2JgoYBafJdhVg3B+FtO+HpSrFY9Y9LPdvY9H2VFO3N0hnNcJuM JSVzlzf/LTj70mYrigyJt17Ql7XFiK4wceuBOgaY8lQVW8y+rsuVEGS2evxln/n70x8s9 K8B91QkItXjwBE2GVWukN98ir7eU0NaBYnY9sX0pe8WPqxY6RkqehYwGWJN/19a1mtGeS ozAHIUNWnCaQ76aT9bFgFspdk9dishDpJmhVkyPStoA9WfGv3e/Indtcgafx3CmGDtqMh X5PQxP2XWyiR2WqTyFXwb6qUXVbvk9uZoQnU9jVMnvpaop+oED6L8q8faGYbqfI=
  • Authentication-results: bugseng.com; arc=none smtp.remote-ip=162.55.131.47
  • Cc: Andrew Cooper <andrew.cooper3@xxxxxxxxxx>, Xen-devel <xen-devel@xxxxxxxxxxxxxxxxxxxx>, Roger Pau Monné <roger.pau@xxxxxxxxxx>
  • Delivery-date: Fri, 15 Aug 2025 08:40:16 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

On 2025-08-15 10:30, Jan Beulich wrote:
On 14.08.2025 20:20, Andrew Cooper wrote:
On 14/08/2025 8:26 am, Jan Beulich wrote:
On 13.08.2025 13:36, Andrew Cooper wrote:
On 12/08/2025 10:43 am, Nicola Vetrini wrote:
On 2025-08-08 22:23, Andrew Cooper wrote:
diff --git a/xen/arch/x86/traps-setup.c b/xen/arch/x86/traps-setup.c
index 8ca379c9e4cb..13b8fcf0ba51 100644
--- a/xen/arch/x86/traps-setup.c
+++ b/xen/arch/x86/traps-setup.c
@@ -19,6 +20,124 @@ boolean_param("ler", opt_ler);

 void nocall entry_PF(void);

+/*
+ * Sets up system tables and descriptors for IDT devliery.
+ *
+ * - Sets up TSS with stack pointers, including ISTs
+ * - Inserts TSS selector into regular and compat GDTs
+ * - Loads GDT, IDT, TR then null LDT
+ * - Sets up IST references in the IDT
+ */
+static void load_system_tables(void)
+{
+    unsigned int i, cpu = smp_processor_id();
+    unsigned long stack_bottom = get_stack_bottom(),
+        stack_top = stack_bottom & ~(STACK_SIZE - 1);
+    /*
+     * NB: define tss_page as a local variable because clang 3.5
doesn't
+     * support using ARRAY_SIZE against per-cpu variables.
+     */
+    struct tss_page *tss_page = &this_cpu(tss_page);
+    idt_entry_t *idt = this_cpu(idt);
+
Given the clang baseline this might not be needed anymore?
Hmm.  While true, looking at 51461114e26, the code is definitely better written with the tss_page variable and we wouldn't want to go back to
the old form.

I think that I'll simply drop the comment.

~Andrew

P.S.

Generally speaking, because of the RELOC_HIDE() in this_cpu(), any time you ever want two accesses to a variable, it's better (code gen wise) to
construct a pointer to it and use the point multiple times.

I don't understand why there's a RELOC_HIDE() in this_cpu().  The
justification doesn't make sense, but I've not had time to explore what
happens if we take it out.
There's no justification in xen/percpu.h?

Well, it's given in compiler.h by RELOC_HIDE().

/* This macro obfuscates arithmetic on a variable address so that gcc
   shouldn't recognize the original var, and make assumptions about it */


But this is far from convincing.


My understanding is that we simply may not expose any accesses to per_cpu_* variables directly to the compiler, or there's a risk that it might access
the "master" variable (i.e. CPU0's on at least x86).

RELOC_HIDE() doesn't do anything about the correctness of the pointer
arithmetic expression to make the access work.

I don't see how a correct expression can ever access CPU0's data by
accident.

Hmm, upon another look I agree. I wonder whether we inherited this from
Linux, where in turn it may have been merely a workaround to deal with
preemptible code not correctly accessing per-CPU data (i.e. not
accounting for get_per_cpu_offset() not being stable across preemption).
Yet then per_cpu() would have been of similar concern when "cpu" isn't
properly re-fetched after any possible preemption point ...

Jan

Probably inherited with a stripped-down comment on top of RELOC_HIDE, see [1]. In a way it does make sense that the compiler may decide to optimize based on this assumption, though I don't know whether wrapping is meant to happen with per-CPU variables.

[1] https://elixir.bootlin.com/linux/v6.16/source/include/linux/compiler-gcc.h#L31

--
Nicola Vetrini, B.Sc.
Software Engineer
BUGSENG (https://bugseng.com)
LinkedIn: https://www.linkedin.com/in/nicola-vetrini-a42471253



 


Rackspace

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