Skip to contentSkip to conversation

xen/arm: address violations of Rule 11.3

PATCH In review [email protected] 5 participants 11 messages Jun 24, 2025 – Jul 15, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 xen/arm: address violations of Rule 11.3

From: Nicola Vetrini <[email protected]>

Use {get,put}_unaligned_t to ensure that reads and writes are safe to perform even on potentially misaligned pointers.

---
Cc: Andrew Cooper <[email protected]>
Cc: Anthony PERARD <[email protected]>
Cc: Michal Orzel <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Roger Pau Monné <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Nicola Vetrini <[email protected]>
Cc: Federico Serafini <[email protected]>
Cc: Bertrand Marquis <[email protected]>

---
 xen/arch/arm/efi/efi-boot.h      | 9 +++++----
 xen/common/device-tree/bootfdt.c | 3 ++-
 2 files changed, 7 insertions(+), 5 deletions(-)
xen/arch/arm/efi/efi-boot.h +5 -4
index ee80560e13..12dbb6961f 100644
@@ -9,6 +9,7 @@
 
 #include <xen/device_tree.h>
 #include <xen/libfdt/libfdt.h>
+#include <xen/unaligned.h>
 #include <asm/setup.h>
 #include <asm/smp.h>
 
@@ -85,7 +86,7 @@ static int __init setup_chosen_node(void *fdt, int *addr_cells, int *size_cells)
         *addr_cells = 2;
     }
     else
-        *addr_cells = fdt32_to_cpu(*((uint32_t *)prop->data));
+        *addr_cells = fdt32_to_cpu(get_unaligned_t(uint32_t, prop->data));
 
     prop = fdt_get_property(fdt, node, "#size-cells", &len);
     if ( !prop )
@@ -96,7 +97,7 @@ static int __init setup_chosen_node(void *fdt, int *addr_cells, int *size_cells)
         *size_cells = 2;
     }
     else
-        *size_cells = fdt32_to_cpu(*((uint32_t *)prop->data));
+        *size_cells = fdt32_to_cpu(get_unaligned_t(uint32_t, prop->data));
 
     /*
      * Make sure ranges is empty if it exists, otherwise create empty ranges
@@ -824,7 +825,7 @@ static int __init handle_dom0less_domain_node(const EFI_LOADED_IMAGE *loaded_ima
         return ERROR_MISSING_DT_PROPERTY;
     }
 
-    addr_cells = fdt32_to_cpu(*((uint32_t *)prop->data));
+    addr_cells = fdt32_to_cpu(get_unaligned_t(uint32_t, prop->data));
 
     prop = fdt_get_property(fdt_efi, domain_node, "#size-cells", &len);
     if ( !prop )
@@ -833,7 +834,7 @@ static int __init handle_dom0less_domain_node(const EFI_LOADED_IMAGE *loaded_ima
         return ERROR_MISSING_DT_PROPERTY;
     }
 
-    size_cells = fdt32_to_cpu(*((uint32_t *)prop->data));
+    size_cells = fdt32_to_cpu(get_unaligned_t(uint32_t, prop->data));
 
     /* Check for nodes compatible with multiboot,module inside this node */
     for ( module_node = fdt_first_subnode(fdt_efi, domain_node);
xen/common/device-tree/bootfdt.c +2 -1
index 529c91e603..9f255027ea 100644
@@ -13,6 +13,7 @@
 #include <xen/lib.h>
 #include <xen/libfdt/libfdt-xen.h>
 #include <xen/sort.h>
+#include <xen/unaligned.h>
 #include <xsm/xsm.h>
 #include <asm/setup.h>
 #ifdef CONFIG_STATIC_SHM
@@ -213,7 +214,7 @@ u32 __init device_tree_get_u32(const void *fdt, int node,
     if ( !prop || prop->len < sizeof(u32) )
         return dflt;
 
-    return fdt32_to_cpu(*(uint32_t*)prop->data);
+    return fdt32_to_cpu(get_unaligned_t(uint32_t, prop->data));
 }
 
 /**

--
2.25.1

#1 lore raw

PATCH 2/2 xen/x86: address violations of Rule 11.3

From: Nicola Vetrini <[email protected]>

Use {get,put}_unaligned_t to ensure that reads and writes are safe to perform even on potentially misaligned pointers.

---
Cc: Andrew Cooper <[email protected]>
Cc: Anthony PERARD <[email protected]>
Cc: Michal Orzel <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Roger Pau Monné <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Nicola Vetrini <[email protected]>
Cc: Federico Serafini <[email protected]>
Cc: Bertrand Marquis <[email protected]>

---
 xen/arch/x86/alternative.c               | 12 ++++++++----
 xen/arch/x86/hvm/emulate.c               |  8 ++++----
 xen/arch/x86/include/asm/hvm/vlapic.h    |  5 +++--
 xen/arch/x86/setup.c                     |  2 +-
 xen/drivers/passthrough/amd/iommu_init.c |  2 +-
 5 files changed, 17 insertions(+), 12 deletions(-)
xen/arch/x86/alternative.c +8 -4
index d4fe56b3da..200cf2e44c 100644
@@ -17,6 +17,7 @@
 #include <asm/nmi.h>
 #include <asm/nops.h>
 #include <xen/livepatch.h>
+#include <xen/unaligned.h>
 
 #define MAX_PATCH_LEN (255-1)
 
@@ -168,7 +169,7 @@ void *place_ret(void *ptr)
         BUG_ON((int32_t)disp != disp);
 
         *p++ = 0xe9;
-        *(int32_t *)p = disp;
+        put_unaligned_t(int32_t, disp, p);
         p += 4;
     }
     else
@@ -345,8 +346,11 @@ static int init_or_livepatch _apply_alternatives(struct alt_instr *start,
 
         /* 0xe8/0xe9 are relative branches; fix the offset. */
         if ( a->repl_len >= 5 && (*buf & 0xfe) == 0xe8 )
-            *(int32_t *)(buf + 1) += repl - orig;
+        {
+            int32_t val = get_unaligned_t(int32_t, buf + 1);
 
+            put_unaligned_t(int32_t, val + repl - orig, buf + 1);
+        }
         a->priv = 1;
 
         add_nops(buf + a->repl_len, total_len - a->repl_len);
@@ -388,7 +392,7 @@ static int init_or_livepatch apply_alt_calls(
             return -EINVAL;
         }
 
-        disp = *(int32_t *)(orig + 2);
+        disp = get_unaligned_t(int32_t, orig + 2);
         dest = *(const void **)(orig + 6 + disp);
 
         if ( dest )
@@ -420,7 +424,7 @@ static int init_or_livepatch apply_alt_calls(
 
             buf[0] = 0x2e;
             buf[1] = 0xe8;
-            *(int32_t *)(buf + 2) = disp;
+            put_unaligned_t(int32_t, disp, buf + 2);
         }
         else
         {
xen/arch/x86/hvm/emulate.c +4 -4
index 91f004d233..3b644eed12 100644
@@ -68,17 +68,17 @@ static void hvmtrace_io_assist(const ioreq_t *p)
     else
         event = p->dir ? TRC_HVM_IOPORT_READ : TRC_HVM_IOPORT_WRITE;
 
-    *(uint64_t *)buffer = p->addr;
-    size = (p->addr != (u32)p->addr) ? 8 : 4;
+    put_unaligned_t(uint64_t, p->addr, buffer);
+    size = (p->addr != (uint32_t)p->addr) ? 8 : 4;
     if ( size == 8 )
         event |= TRC_64_FLAG;
 
     if ( !p->data_is_ptr )
     {
         if ( size == 4 )
-            *(uint32_t *)&buffer[size] = p->data;
+            put_unaligned_t(uint32_t, p->data, &buffer[size]);
         else
-            *(uint64_t *)&buffer[size] = p->data;
+            put_unaligned_t(uint64_t, p->data, &buffer[size]);
         size *= 2;
     }
 
xen/arch/x86/include/asm/hvm/vlapic.h +3 -2
index c388551198..c7a26c0a0f 100644
@@ -10,6 +10,7 @@
 #define __ASM_X86_HVM_VLAPIC_H__
 
 #include <xen/tasklet.h>
+#include <xen/unaligned.h>
 #include <asm/hvm/vpt.h>
 
 #define vcpu_vlapic(x)   (&(x)->arch.hvm.vlapic)
@@ -85,13 +86,13 @@ struct vlapic {
 static inline uint32_t vlapic_get_reg(const struct vlapic *vlapic,
                                       uint32_t reg)
 {
-    return *((uint32_t *)(&vlapic->regs->data[reg]));
+    return get_unaligned_t(uint32_t, &vlapic->regs->data[reg]);
 }
 
 static inline void vlapic_set_reg(
     struct vlapic *vlapic, uint32_t reg, uint32_t val)
 {
-    *((uint32_t *)(&vlapic->regs->data[reg])) = val;
+    put_unaligned_t(uint32_t, val, &vlapic->regs->data[reg]);
 }
 
 void vlapic_reg_write(struct vcpu *v, unsigned int reg, uint32_t val);
xen/arch/x86/setup.c +1 -1
index 1f5cb67bd0..5919e94f5b 100644
@@ -1249,7 +1249,7 @@ void asmlinkage __init noreturn __start_xen(void)
                (caps & 2) ? " V2" : "",
                !(caps & 3) ? " none" : "");
         printk("EDID transfer time: %d seconds\n", caps >> 8);
-        if ( *(u32 *)bootsym(boot_edid_info) == 0x13131313 )
+        if ( get_unaligned_t(u32, bootsym(boot_edid_info)) == 0x13131313 )
         {
             printk(" EDID info not retrieved because ");
             if ( !(caps & 3) )
xen/drivers/passthrough/amd/iommu_init.c +1 -1
index bb25b55c85..c00aa64983 100644
@@ -562,7 +562,7 @@ static void cf_check parse_event_log_entry(struct amd_iommu *iommu, u32 entry[])
         unsigned int device_id = MASK_EXTR(entry[0], IOMMU_EVENT_DEVICE_ID_MASK);
         unsigned int domain_id = MASK_EXTR(entry[1], IOMMU_EVENT_DOMAIN_ID_MASK);
         unsigned int flags = MASK_EXTR(entry[1], IOMMU_EVENT_FLAGS_MASK);
-        uint64_t addr = *(uint64_t *)(entry + 2);
+        uint64_t addr = get_unaligned_t(uint64_t, entry + 2);
 
         printk(XENLOG_ERR "AMD-Vi: %s: %pp d%u addr %016"PRIx64
                " flags %#x%s%s%s%s%s%s%s%s%s%s\n",

--
2.25.1

#2 lore raw

Re: [PATCH 2/2] xen/x86: address violations of Rule 11.3

Quoted 4 lines · On 24.06.2025 02:20, [email protected] wrote:

From: Nicola Vetrini <[email protected]>

Use {get,put}_unaligned_t to ensure that reads and writes are safe to perform even on potentially misaligned pointers.

Also applicable to the Arm patch: Please can such patches mention the main subject of the rule, not just the number?

Overall I'm unconvinced we really want or need this on x86; I'm curious what Andrew and Roger think. Further, even beyond the respective remark below, I'd be pretty surprised if these were all of the places that would need fiddling with. Mind me asking how the places to touch were identified? (This may actually be a good thing to mention in the description.)

xen/arch/x86/alternative.c static int init_or_livepatch apply_alt_calls( · line 396

Quoted 7 lines
xen/arch/x86/alternative.c +1 -1
@@ -388,7 +392,7 @@ static int init_or_livepatch apply_alt_calls(
             return -EINVAL;
         }
 
-        disp = *(int32_t *)(orig + 2);
+        disp = get_unaligned_t(int32_t, orig + 2);
         dest = *(const void **)(orig + 6 + disp);

Why is this latter line not also adjusted? The field is expected to be aligned, yes, but for the code here there's no guarantee. Imo if this was left alone along with applying the suggested change, a code comment would need adding.

Quoted 11 lines
xen/arch/x86/include/asm/hvm/vlapic.h +1 -0
@@ -10,6 +10,7 @@
 #define __ASM_X86_HVM_VLAPIC_H__
 
 #include <xen/tasklet.h>
+#include <xen/unaligned.h>
 #include <asm/hvm/vpt.h>
 
 #define vcpu_vlapic(x)   (&(x)->arch.hvm.vlapic)

xen/arch/x86/include/asm/hvm/vlapic.h struct vlapic { · line 89

Quoted 5 lines
xen/arch/x86/include/asm/hvm/vlapic.h +1 -1
@@ -85,13 +86,13 @@ struct vlapic {
 static inline uint32_t vlapic_get_reg(const struct vlapic *vlapic,
                                       uint32_t reg)
 {
-    return *((uint32_t *)(&vlapic->regs->data[reg]));
+    return get_unaligned_t(uint32_t, &vlapic->regs->data[reg]);

This, aiui (or should I say "I hope"), also addresses another violation (casting away of const). Such will want mentioning in the description, imo.

xen/arch/x86/setup.c void asmlinkage __init noreturn __start_xen(void) · line 1252

Quoted 8 lines
xen/arch/x86/setup.c +1 -1
@@ -1249,7 +1249,7 @@ void asmlinkage __init noreturn __start_xen(void)
                (caps & 2) ? " V2" : "",
                !(caps & 3) ? " none" : "");
         printk("EDID transfer time: %d seconds\n", caps >> 8);
-        if ( *(u32 *)bootsym(boot_edid_info) == 0x13131313 )
+        if ( get_unaligned_t(u32, bootsym(boot_edid_info)) == 0x13131313 )

When touching such, please can you also convert to uint<N>_t?

Jan

#3 lore raw

Re: [PATCH 2/2] xen/x86: address violations of Rule 11.3

Quoted 8 lines · Jan Beulich
Quoted 4 lines · On 24.06.2025 02:20, [email protected] wrote:

From: Nicola Vetrini <[email protected]>

Use {get,put}_unaligned_t to ensure that reads and writes are safe to perform even on potentially misaligned pointers.

Also applicable to the Arm patch: Please can such patches mention the main subject of the rule, not just the number?

+1

Quoted 2 lines

Overall I'm unconvinced we really want or need this on x86; I'm curious what Andrew and Roger think.

To be honest, I had a similar reaction to you, which is why I suggested on Matrix to:

- deviate the rule in its entirety on x86
- deviate the rule for all mappings except for devmem mappings on ARM

Leaving aside ARM for a second (this is exactly the kind of very arch-specific behavior that is OK to device differently per architecture), would you be OK with deviating the rule in its entirety on x86?

Or do you prefer to continue with this patch?

Quoted 5 lines

Further, even beyond the respective remark
below, I'd be pretty surprised if these were all of the places that would need fiddling with. Mind me asking how the places to touch were identified? (This may actually be a good thing to mention in the description.)

The patch is simply based on the violations reported by the ECLAIR scan.

Quoted 47 lines

xen/arch/x86/alternative.c static int init_or_livepatch apply_alt_calls( · line 396

Quoted 7 lines
xen/arch/x86/alternative.c +1 -1
@@ -388,7 +392,7 @@ static int init_or_livepatch apply_alt_calls(
             return -EINVAL;
         }
 
-        disp = *(int32_t *)(orig + 2);
+        disp = get_unaligned_t(int32_t, orig + 2);
         dest = *(const void **)(orig + 6 + disp);

Why is this latter line not also adjusted? The field is expected to be aligned, yes, but for the code here there's no guarantee. Imo if this was left alone along with applying the suggested change, a code comment would need adding.

Quoted 11 lines
xen/arch/x86/include/asm/hvm/vlapic.h +1 -0
@@ -10,6 +10,7 @@
 #define __ASM_X86_HVM_VLAPIC_H__
 
 #include <xen/tasklet.h>
+#include <xen/unaligned.h>
 #include <asm/hvm/vpt.h>
 
 #define vcpu_vlapic(x)   (&(x)->arch.hvm.vlapic)

xen/arch/x86/include/asm/hvm/vlapic.h struct vlapic { · line 89

Quoted 5 lines
xen/arch/x86/include/asm/hvm/vlapic.h +1 -1
@@ -85,13 +86,13 @@ struct vlapic {
 static inline uint32_t vlapic_get_reg(const struct vlapic *vlapic,
                                       uint32_t reg)
 {
-    return *((uint32_t *)(&vlapic->regs->data[reg]));
+    return get_unaligned_t(uint32_t, &vlapic->regs->data[reg]);

This, aiui (or should I say "I hope"), also addresses another violation (casting away of const). Such will want mentioning in the description, imo.

xen/arch/x86/setup.c void asmlinkage __init noreturn __start_xen(void) · line 1252

Quoted 8 lines
xen/arch/x86/setup.c +1 -1
@@ -1249,7 +1249,7 @@ void asmlinkage __init noreturn __start_xen(void)
                (caps & 2) ? " V2" : "",
                !(caps & 3) ? " none" : "");
         printk("EDID transfer time: %d seconds\n", caps >> 8);
-        if ( *(u32 *)bootsym(boot_edid_info) == 0x13131313 )
+        if ( get_unaligned_t(u32, bootsym(boot_edid_info)) == 0x13131313 )

When touching such, please can you also convert to uint<N>_t?

Jan

#4 lore raw

Re: [PATCH 2/2] xen/x86: address violations of Rule 11.3

Quoted 22 lines · Stefano Stabellini
Quoted 8 lines · Jan Beulich
Quoted 4 lines · On 24.06.2025 02:20, [email protected] wrote:

From: Nicola Vetrini <[email protected]>

Use {get,put}_unaligned_t to ensure that reads and writes are safe to perform even on potentially misaligned pointers.

Also applicable to the Arm patch: Please can such patches mention the main subject of the rule, not just the number?

+1

Quoted 2 lines

Overall I'm unconvinced we really want or need this on x86; I'm curious what Andrew and Roger think.

To be honest, I had a similar reaction to you, which is why I suggested on Matrix to:

- deviate the rule in its entirety on x86
- deviate the rule for all mappings except for devmem mappings on ARM

Quoted 6 lines

Leaving aside ARM for a second (this is exactly the kind of very arch-specific behavior that is OK to device differently per architecture), would you be OK with deviating the rule in its entirety on x86?

Or do you prefer to continue with this patch?

Neither. Imo globally deviating rules needs to be done with care. There are, in principle, misaligned accesses in x86 which can be made fault (and I think this was mentioned before). We want to know of such risks. Hence for a rule like this one more fine grained deviation is on order, imo.

Jan

#5 lore raw

Re: [PATCH 2/2] xen/x86: address violations of Rule 11.3

Quoted 28 lines · Jan Beulich
Quoted 27 lines · Stefano Stabellini
Quoted 8 lines · Jan Beulich
Quoted 4 lines · On 24.06.2025 02:20, [email protected] wrote:

From: Nicola Vetrini <[email protected]>

Use {get,put}_unaligned_t to ensure that reads and writes are safe to perform even on potentially misaligned pointers.

Also applicable to the Arm patch: Please can such patches mention the main subject of the rule, not just the number?

+1

Quoted 2 lines

Overall I'm unconvinced we really want or need this on x86; I'm curious what Andrew and Roger think.

To be honest, I had a similar reaction to you, which is why I suggested on Matrix to:

- deviate the rule in its entirety on x86
- deviate the rule for all mappings except for devmem mappings on ARM

Leaving aside ARM for a second (this is exactly the kind of very arch-specific behavior that is OK to device differently per architecture), would you be OK with deviating the rule in its entirety on x86?

Quoted 7 lines
Quoted 1 line

Or do you prefer to continue with this patch?

Neither. Imo globally deviating rules needs to be done with care. There are, in principle, misaligned accesses in x86 which can be made fault (and I think this was mentioned before). We want to know of such risks. Hence for a rule like this one more fine grained deviation is on order, imo.

What fine grained deviation do you have in mind?

#6 lore raw

Re: [PATCH 2/2] xen/x86: address violations of Rule 11.3

Quoted 31 lines · Stefano Stabellini
Quoted 30 lines · Jan Beulich
Quoted 28 lines · Stefano Stabellini
Quoted 8 lines · Jan Beulich
Quoted 4 lines · On 24.06.2025 02:20, [email protected] wrote:

From: Nicola Vetrini <[email protected]>

Use {get,put}_unaligned_t to ensure that reads and writes are safe to perform even on potentially misaligned pointers.

Also applicable to the Arm patch: Please can such patches mention the main subject of the rule, not just the number?

+1

Quoted 2 lines

Overall I'm unconvinced we really want or need this on x86; I'm curious what Andrew and Roger think.

To be honest, I had a similar reaction to you, which is why I suggested on Matrix to:

- deviate the rule in its entirety on x86
- deviate the rule for all mappings except for devmem mappings on ARM

Leaving aside ARM for a second (this is exactly the kind of very arch-specific behavior that is OK to device differently per architecture), would you be OK with deviating the rule in its entirety on x86?

Or do you prefer to continue with this patch?

Quoted 7 lines
Quoted 5 lines

Neither. Imo globally deviating rules needs to be done with care. There are, in principle, misaligned accesses in x86 which can be made fault (and I think this was mentioned before). We want to know of such risks. Hence for a rule like this one more fine grained deviation is on order, imo.

What fine grained deviation do you have in mind?

Ones for almost(?) everything that is having actual code changes right now in this series.

Jan

#7 lore raw

Re: [PATCH 2/2] xen/x86: address violations of Rule 11.3

Quoted 4 lines · Jan Beulich
Quoted 1 line

What fine grained deviation do you have in mind?

Ones for almost(?) everything that is having actual code changes right now in this series.

We could easily deviate alternative.c based on the file name alone. But for the rest:

emulate.c: casts from unsigned char* (byte aligned) to uint64_t* (8 bytes aligned)

vlapic.h: casts from uint8_t* (byte aligned) to uint32_t* (4 bytes aligned)

setup.c: games with symbols

iommu_init.c: cast from uint32_t* (4 bytes aligned) to uint64_t* (8 bytes aligned)

How would you deviate these in general terms? Or are you suggesting to use SAF tags for each of them?

I suggest we consider taking the patch as is, or globally deviating this rule for x86 (or keep the rule but not enabling it in ECLAIR for x86).

#8 lore raw

Re: [PATCH 2/2] xen/x86: address violations of Rule 11.3

Quoted 11 lines · Stefano Stabellini
Quoted 4 lines · Jan Beulich
Quoted 1 line

What fine grained deviation do you have in mind?

Ones for almost(?) everything that is having actual code changes right now in this series.

We could easily deviate alternative.c based on the file name alone. But for the rest:

emulate.c: casts from unsigned char* (byte aligned) to uint64_t* (8 bytes aligned)

This one may indeed be fine to patch.

Quoted 1 line

vlapic.h: casts from uint8_t* (byte aligned) to uint32_t* (4 bytes aligned)

These only give the impression of increasing alignment. struct hvm_hw_lapic_regs is containing solely an uint8_t[1024] array, and all instances (created from vlapic_init()) actually end up at page boundaries. What I don't know is whether adding __aligned(PAGE_SIZE) to the struct vlapic field declaration would convince Eclair of there being no issue here. Probably not, as the array index used in both of the accesses isn't known (to it) to be 16-byte aligned.

Quoted 1 line

setup.c: games with symbols

The change here may again be acceptable; better may be to use memchr_inv() there, as being less obfuscating _and_ eliminating the cast there altogether.

Quoted 2 lines

iommu_init.c: cast from uint32_t* (4 bytes aligned) to uint64_t* (8 bytes aligned)

This imo would better be done by reconstructing the 64-bit value from the two involved 32-bit array elements.

Quoted 2 lines

How would you deviate these in general terms? Or are you suggesting to use SAF tags for each of them?

If no other solution can be found for the vlapic.h issue, "yes" there. For all others I suggested alternative approaches. Subject to other x86 folks possibly objecting, though.

Jan

#9 lore raw

Re: [PATCH 2/2] xen/x86: address violations of Rule 11.3

Hi Jan,

I apologize for the confusion. I initially believed this patch would reduce the outstanding violations to zero on x86, but in reality, there are still 1,415 remaining.

https://gitlab.com/xen-project/people/sstabellini/xen/-/jobs/10660702979

Given this, I believe we should consider globally deviating the rule on x86, or at least apply it opportunistically, as we have done with other rules. In such cases, the rule is accepted but not actively scanned with ECLAIR, and we provide alternatives, such as using GCC ASAN, along with an explanation that misaligned access typically does not lead to crashes on x86.

Quoted 44 lines · Jan Beulich
Quoted 11 lines · Stefano Stabellini
Quoted 4 lines · Jan Beulich
Quoted 1 line

What fine grained deviation do you have in mind?

Ones for almost(?) everything that is having actual code changes right now in this series.

We could easily deviate alternative.c based on the file name alone. But for the rest:

emulate.c: casts from unsigned char* (byte aligned) to uint64_t* (8 bytes aligned)

This one may indeed be fine to patch.

Quoted 1 line

vlapic.h: casts from uint8_t* (byte aligned) to uint32_t* (4 bytes aligned)

These only give the impression of increasing alignment. struct hvm_hw_lapic_regs is containing solely an uint8_t[1024] array, and all instances (created from vlapic_init()) actually end up at page boundaries. What I don't know is whether adding __aligned(PAGE_SIZE) to the struct vlapic field declaration would convince Eclair of there being no issue here. Probably not, as the array index used in both of the accesses isn't known (to it) to be 16-byte aligned.

Quoted 1 line

setup.c: games with symbols

The change here may again be acceptable; better may be to use memchr_inv() there, as being less obfuscating _and_ eliminating the cast there altogether.

Quoted 2 lines

iommu_init.c: cast from uint32_t* (4 bytes aligned) to uint64_t* (8 bytes aligned)

This imo would better be done by reconstructing the 64-bit value from the two involved 32-bit array elements.

Quoted 2 lines

How would you deviate these in general terms? Or are you suggesting to use SAF tags for each of them?

If no other solution can be found for the vlapic.h issue, "yes" there. For all others I suggested alternative approaches. Subject to other x86 folks possibly objecting, though.

Jan

Acked-by on v1 1/2
#10 lore raw
In reply to [email protected]

Hi Nicola,

Quoted 7 lines · On 24 Jun 2025, at 02:20, [email protected] wrote:

From: Nicola Vetrini <[email protected]>

Use {get,put}_unaligned_t to ensure that reads and writes are safe to perform even on potentially misaligned pointers.

Make sense as we convert a void* to uint32_t * so might be misaligned.

Quoted 2 lines

Cheers
Bertrand

Quoted 87 lines

---
Cc: Andrew Cooper <[email protected]>
Cc: Anthony PERARD <[email protected]>
Cc: Michal Orzel <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Roger Pau Monné <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Nicola Vetrini <[email protected]>
Cc: Federico Serafini <[email protected]>
Cc: Bertrand Marquis <[email protected]>
---
xen/arch/arm/efi/efi-boot.h | 9 +++++----
xen/common/device-tree/bootfdt.c | 3 ++-

2 files changed, 7 insertions(+), 5 deletions(-)
xen/arch/arm/efi/efi-boot.h +5 -4
index ee80560e13..12dbb6961f 100644
@@ -9,6 +9,7 @@
 
 #include <xen/device_tree.h>
 #include <xen/libfdt/libfdt.h>
+#include <xen/unaligned.h>
 #include <asm/setup.h>
 #include <asm/smp.h>
 
@@ -85,7 +86,7 @@ static int __init setup_chosen_node(void *fdt, int *addr_cells, int *size_cells)
        *addr_cells = 2;
    }
    else
-        *addr_cells = fdt32_to_cpu(*((uint32_t *)prop->data));
+        *addr_cells = fdt32_to_cpu(get_unaligned_t(uint32_t, prop->data));
 
    prop = fdt_get_property(fdt, node, "#size-cells", &len);
    if ( !prop )
@@ -96,7 +97,7 @@ static int __init setup_chosen_node(void *fdt, int *addr_cells, int *size_cells)
        *size_cells = 2;
    }
    else
-        *size_cells = fdt32_to_cpu(*((uint32_t *)prop->data));
+        *size_cells = fdt32_to_cpu(get_unaligned_t(uint32_t, prop->data));
 
    /*
     * Make sure ranges is empty if it exists, otherwise create empty ranges
@@ -824,7 +825,7 @@ static int __init handle_dom0less_domain_node(const EFI_LOADED_IMAGE *loaded_ima
        return ERROR_MISSING_DT_PROPERTY;
    }
 
-    addr_cells = fdt32_to_cpu(*((uint32_t *)prop->data));
+    addr_cells = fdt32_to_cpu(get_unaligned_t(uint32_t, prop->data));
 
    prop = fdt_get_property(fdt_efi, domain_node, "#size-cells", &len);
    if ( !prop )
@@ -833,7 +834,7 @@ static int __init handle_dom0less_domain_node(const EFI_LOADED_IMAGE *loaded_ima
        return ERROR_MISSING_DT_PROPERTY;
    }
 
-    size_cells = fdt32_to_cpu(*((uint32_t *)prop->data));
+    size_cells = fdt32_to_cpu(get_unaligned_t(uint32_t, prop->data));
 
    /* Check for nodes compatible with multiboot,module inside this node */
    for ( module_node = fdt_first_subnode(fdt_efi, domain_node);
xen/common/device-tree/bootfdt.c +2 -2
index 529c91e603..9f255027ea 100644
@@ -13,6 +13,7 @@
 #include <xen/lib.h>
 #include <xen/libfdt/libfdt-xen.h>
 #include <xen/sort.h>
+#include <xen/unaligned.h>
 #include <xsm/xsm.h>
 #include <asm/setup.h>
 #ifdef CONFIG_STATIC_SHM
@@ -213,7 +214,7 @@ u32 __init device_tree_get_u32(const void *fdt, int node,
    if ( !prop || prop->len < sizeof(u32) )
        return dflt;
 
-    return fdt32_to_cpu(*(uint32_t*)prop->data);
+    return fdt32_to_cpu(get_unaligned_t(uint32_t, prop->data));
 }
 
 /**
--

2.25.1