Skip to contentSkip to conversation

FlexCAN fixes for-11.1

PATCH In review Bernhard Beschow 5 participants 26 messages Jul 23 – Jul 27

lore
Share & tools
lore
#0 lore raw

cover FlexCAN fixes for-11.1

This series attempts to fix the Coveriy issues pointed out by Peter in the FlexCAN emulation [1]. In addition, it fixes some reset masks to preserve register contents across resets as intended. It also fixes some register offsets due to a union being inadvertantly too large; this would affect migration if addressed after the release. Last but not least it eliminates an unneeded union.

Testing done:
* make check
* Run cangen and candump on the host and guest in both directions

[1] https://lore.kernel.org/qemu-devel/CAFEAcA-QBiPfgN4YJXD9FQULobpBVNgeJHSqG7Eqg1CzyKTfaA@mail.gmail.com/

Bernhard Beschow (9):
hw/net/can/flexcan: Use mbs[] array for FIFO pop hw/net/flexcan: Drop unused RX FIFO register overlay hw/net/can/flexcan: Fix out-of-bounds access in flexcan_mx_rx() hw/net/can/flexcan: Fix mailbox index calculation in
flexcan_mem_write()
hw/net/can/flexcan: Fix mailbox index calculation in
flexcan_mem_read()
hw/net/can/flexcan: Fix mailbox reset mask initialization hw/net/flexcan: Drop oversized 'mb[]' mailbox view hw/net/can/flexcan: Fix RXIMR reset mask hw/net/can/flexcan: Remove RX SMB raw view

 include/hw/net/flexcan.h | 22 +++----------------
 hw/net/can/flexcan.c     | 47 ++++++++++++++++++++++++----------------
 2 files changed, 31 insertions(+), 38 deletions(-)
Signature · 1 line
2.55.0
#1 lore raw

PATCH 1/9 hw/net/can/flexcan: Use mbs[] array for FIFO pop

Implement FIFO entry shifting using the underlying mbs[] array instead of the overlapping fifo union view. This makes it explicit that the operation copies within a contiguous mailbox array and avoids Coverity CID 1662971.

---
 hw/net/can/flexcan.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
hw/net/can/flexcan.c +5 -5
index da36d10bd3..3a97edf7a4 100644
@@ -879,22 +879,22 @@ static bool flexcan_can_receive(CanBusClientState *client)
  */
 static void flexcan_fifo_pop(FlexcanState *s)
 {
-    if (s->regs.fifo.mb_back.can_ctrl != 0) {
+    if (s->regs.mbs[0].can_ctrl != 0) {
         /* move queue elements forward */
-        memmove(&s->regs.fifo.mb_back, &s->regs.fifo.mbs_queue[0],
-                sizeof(s->regs.fifo.mbs_queue));
+        memmove(&s->regs.mbs[0], &s->regs.mbs[1],
+                sizeof(s->regs.mbs[0]) * (FLEXCAN_FIFO_DEPTH - 1));
 
         /* clear the first-in slot */
         memset(&s->regs.mbs[FLEXCAN_FIFO_DEPTH - 1], 0,
                sizeof(FlexcanRegsMessageBuffer));
 
         trace_flexcan_fifo_pop(DEVICE(s)->canonical_path, 1,
-                               s->regs.fifo.mb_back.can_ctrl != 0);
+                               s->regs.mbs[0].can_ctrl != 0);
     } else {
         trace_flexcan_fifo_pop(DEVICE(s)->canonical_path, 0, 0);
     }
 
-    if (s->regs.fifo.mb_back.can_ctrl != 0) {
+    if (s->regs.mbs[0].can_ctrl != 0) {
         flexcan_irq_iflag_set(s, I_FIFO_AVAILABLE);
     } else {
         flexcan_irq_iflag_clear(s, I_FIFO_AVAILABLE);
Signature · 1 line
2.55.0
Reviewed-by on v1 1/9
#2 lore raw

Re: [PATCH 1/9] hw/net/can/flexcan: Use mbs[] array for FIFO pop

Quoted 10 lines · Bernhard Beschow

Implement FIFO entry shifting using the underlying mbs[] array instead of the overlapping fifo union view. This makes it explicit that the operation copies within a contiguous mailbox array and avoids Coverity CID 1662971.

---
  hw/net/can/flexcan.c | 10 +++++-----
  1 file changed, 5 insertions(+), 5 deletions(-)
#3 lore raw

PATCH 2/9 hw/net/flexcan: Drop unused RX FIFO register overlay

In reply to Bernhard Beschow

Remove the unused FlexcanRegsRXFifo union view now that FIFO accesses use the underlying mbs[] array directly.

---
 include/hw/net/flexcan.h | 11 -----------
 1 file changed, 11 deletions(-)
include/hw/net/flexcan.h +0 -11
index 32de7b904a..d8159475e0 100644
@@ -35,16 +35,6 @@ typedef struct FlexcanRegsMessageBuffer {
     uint32_t data[2];
 } FlexcanRegsMessageBuffer;
 
-/* RX FIFO view of message buffer registers */
-typedef struct FlexcanRegsRXFifo {
-    /* 6 message buffer deep queue, queue back first */
-    FlexcanRegsMessageBuffer mb_back;
-    FlexcanRegsMessageBuffer mbs_queue[FLEXCAN_FIFO_DEPTH - 1];
-
-    /* number of filter elements active depends on ctrl2 | FLEXCAN_CTRL2_RFFN */
-    uint32_t                 filter_table_els[128];
-} FlexcanRegsRXFifo;
-
 /* FlexCAN register in hw layout */
 typedef struct FlexcanRegs {
     uint32_t mcr;                /* 0x00 */
@@ -78,7 +68,6 @@ typedef struct FlexcanRegs {
     union {                      /* 0x80 - not affected by soft reset */
         uint32_t mb[sizeof(FlexcanRegsMessageBuffer) * FLEXCAN_MAILBOX_COUNT];
         FlexcanRegsMessageBuffer mbs[FLEXCAN_MAILBOX_COUNT];
-        FlexcanRegsRXFifo fifo;
     };
     uint32_t _reserved4[256];    /* 0x480 */
     uint32_t rximr[64];          /* 0x880 - not affected by soft reset */
Signature · 1 line
2.55.0
#5 lore raw

PATCH 3/9 hw/net/can/flexcan: Fix out-of-bounds access in flexcan_mx_rx()

In reply to Bernhard Beschow

Require last_not_free_to_receive_mbid >= 0 before indexing mbs[]. This prevents a possible -1 array index and fixes Coverity CID 1662790.

---
 hw/net/can/flexcan.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/net/can/flexcan.c +1 -1
index 3a97edf7a4..dd92a38337 100644
@@ -1075,7 +1075,7 @@ static enum FlexcanRx flexcan_mb_rx(FlexcanState *s, const qemu_can_frame *buf)
         }
     }
 
-    if (last_not_free_to_receive_mbid >= -1) {
+    if (last_not_free_to_receive_mbid >= 0) {
         if (last_not_free_to_receive_locked) {
             /*
              * copy to temporary mailbox (SMB)
Signature · 1 line
2.55.0
Reviewed-by on v1 3/9
#6 lore raw

Re: [PATCH 3/9] hw/net/can/flexcan: Fix out-of-bounds access in flexcan_mx_rx()

Quoted 25 lines · Bernhard Beschow

Require last_not_free_to_receive_mbid >= 0 before indexing mbs[]. This prevents a possible -1 array index and fixes Coverity CID 1662790.

---
 hw/net/can/flexcan.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/net/can/flexcan.c +1 -2
index 3a97edf7a4..dd92a38337 100644
@@ -1075,7 +1075,7 @@ static enum FlexcanRx flexcan_mb_rx(FlexcanState *s, const qemu_can_frame *buf)
         }
     }
 
-    if (last_not_free_to_receive_mbid >= -1) {
+    if (last_not_free_to_receive_mbid >= 0) {
         if (last_not_free_to_receive_locked) {
             /*
              * copy to temporary mailbox (SMB)
--
Quoted 1 line

2.55.0

thanks
-- PMM

#7 lore raw

PATCH 4/9 hw/net/can/flexcan: Fix mailbox index calculation in flexcan_mem_write()

In reply to Bernhard Beschow

Calculate mailbox indices from the mbs[] array layout instead of the oversized raw mb[] view. This prevents accessing mailbox entries beyond the valid array range and fixes Coverity CID 1662974.

---
 hw/net/can/flexcan.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
hw/net/can/flexcan.c +3 -4
index dd92a38337..651904fa37 100644
@@ -1151,6 +1151,8 @@ static void flexcan_mem_write(void *opaque, hwaddr addr, uint64_t val,
                               unsigned size)
 {
     FlexcanState *s = opaque;
+    const int mbid = (addr - offsetof(FlexcanRegs, mbs)) /
+        sizeof(s->regs.mbs[0]);
     uint32_t write_mask = ((const uint32_t *)
         &flexcan_regs_write_mask)[addr / 4];
     uint32_t old_value = s->regs_raw[addr / 4];
@@ -1208,11 +1210,8 @@ static void flexcan_mem_write(void *opaque, hwaddr addr, uint64_t val,
     default:
         s->regs_raw[addr / 4] = (val & write_mask) | (old_value & ~write_mask);
 
-        if (addr >= offsetof(FlexcanRegs, mb) &&
-            addr < offsetof(FlexcanRegs, _reserved4)) {
+        if (0 <= mbid && mbid < ARRAY_SIZE(s->regs.mbs)) {
             /* access to mailbox */
-            int mbid = (addr - offsetof(FlexcanRegs, mb)) /
-                            sizeof(FlexcanRegsMessageBuffer);
 
             if (s->locked_mbidx == mbid) {
                 flexcan_mb_unlock(s);
Signature · 1 line
2.55.0
Reviewed-by on v1 4/9
#8 lore raw

Re: [PATCH 4/9] hw/net/can/flexcan: Fix mailbox index calculation in flexcan_mem_write()

Quoted 10 lines · Bernhard Beschow

Calculate mailbox indices from the mbs[] array layout instead of the oversized raw mb[] view. This prevents accessing mailbox entries beyond the valid array range and fixes Coverity CID 1662974.

---
 hw/net/can/flexcan.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

thanks
-- PMM

#9 lore raw

PATCH 5/9 hw/net/can/flexcan: Fix mailbox index calculation in flexcan_mem_read()

In reply to Bernhard Beschow

Calculate mailbox indices from the mbs[] array layout instead of the oversized raw mb[] view. This prevents accessing mailbox entries beyond the valid array range and fixes Coverity CID 1662974.

---
 hw/net/can/flexcan.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
hw/net/can/flexcan.c +3 -5
index 651904fa37..8d680f1832 100644
@@ -1239,14 +1239,12 @@ static void flexcan_mem_write(void *opaque, hwaddr addr, uint64_t val,
 static uint64_t flexcan_mem_read(void *opqaue, hwaddr addr, unsigned size)
 {
     FlexcanState *s = opqaue;
+    const int mbid = (addr - offsetof(FlexcanRegs, mbs)) /
+        sizeof(s->regs.mbs[0]);
     uint32_t rv = s->regs_raw[addr >> 2];
 
-    if (addr >= offsetof(FlexcanRegs, mb) &&
-        addr < offsetof(FlexcanRegs, _reserved4)) {
+    if (0 <= mbid && mbid < ARRAY_SIZE(s->regs.mbs)) {
         /* reading from mailbox */
-        hwaddr offset = addr - offsetof(FlexcanRegs, mb);
-        int mbid = offset / sizeof(FlexcanRegsMessageBuffer);
-
         if (addr % 16 == 0 && s->locked_mbidx != mbid) {
             /* reading control word locks the mailbox */
             flexcan_mb_unlock(s);
Signature · 1 line
2.55.0
Reviewed-by on v1 5/9
#10 lore raw

Re: [PATCH 5/9] hw/net/can/flexcan: Fix mailbox index calculation in flexcan_mem_read()

Quoted 23 lines · Bernhard Beschow

Calculate mailbox indices from the mbs[] array layout instead of the oversized raw mb[] view. This prevents accessing mailbox entries beyond the valid array range and fixes Coverity CID 1662974.

---
 hw/net/can/flexcan.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
hw/net/can/flexcan.c +2 -0
index 651904fa37..8d680f1832 100644
@@ -1239,14 +1239,12 @@ static void flexcan_mem_write(void *opaque, hwaddr addr, uint64_t val,
 static uint64_t flexcan_mem_read(void *opqaue, hwaddr addr, unsigned size)
 {
     FlexcanState *s = opqaue;
+    const int mbid = (addr - offsetof(FlexcanRegs, mbs)) /
+        sizeof(s->regs.mbs[0]);
     uint32_t rv = s->regs_raw[addr >> 2];
 

hw/net/can/flexcan.c static void flexcan_mem_write(void *opaque, hwaddr addr, uint64_t val, · line 1250

Quoted 10 lines
hw/net/can/flexcan.c +1 -5
-    if (addr >= offsetof(FlexcanRegs, mb) &&
-        addr < offsetof(FlexcanRegs, _reserved4)) {
+    if (0 <= mbid && mbid < ARRAY_SIZE(s->regs.mbs)) {
         /* reading from mailbox */
-        hwaddr offset = addr - offsetof(FlexcanRegs, mb);
-        int mbid = offset / sizeof(FlexcanRegsMessageBuffer);
-
         if (addr % 16 == 0 && s->locked_mbidx != mbid) {
             /* reading control word locks the mailbox */
             flexcan_mb_unlock(s);

thanks
-- PMM

#11 lore raw

PATCH 6/9 hw/net/can/flexcan: Fix mailbox reset mask initialization

In reply to Bernhard Beschow

The mailbox reset mask initialized only the first word of the raw mb[] array. Fix the initialization to cover the whole mailbox, as indicated in the field description in the header. While at it, use the mbs[] view instead in order to drop the redundant and oversized mb[] view in the next commit.

---
 hw/net/can/flexcan.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
hw/net/can/flexcan.c +5 -1
index 8d680f1832..f84f090a7f 100644
@@ -134,7 +134,11 @@ static const FlexcanRegs flexcan_regs_reset_mask = {
     ._reserved2 = 0,
     .dbg1 = 0,
     .dbg2 = 0,
-    .mb = {0xFFFFFFFF},
+    .mbs = { [0 ... FLEXCAN_MAILBOX_COUNT - 1] = {
+        .can_ctrl = 0xFFFFFFFF,
+        .can_id = 0xFFFFFFFF,
+        .data = { 0xFFFFFFFF, 0xFFFFFFFF },
+    } },
     ._reserved4 = {0},
     .rximr = {0xFFFFFFFF},
     ._reserved5 = {0},
Signature · 1 line
2.55.0
Reviewed-by on v1 6/9
#12 lore raw

Re: [PATCH 6/9] hw/net/can/flexcan: Fix mailbox reset mask initialization

Quoted 10 lines · Bernhard Beschow

The mailbox reset mask initialized only the first word of the raw mb[] array. Fix the initialization to cover the whole mailbox, as indicated in the field description in the header. While at it, use the mbs[] view instead in order to drop the redundant and oversized mb[] view in the next commit.

---
  hw/net/can/flexcan.c | 6 +++++-
  1 file changed, 5 insertions(+), 1 deletion(-)
#13 lore raw

PATCH 7/9 hw/net/flexcan: Drop oversized 'mb[]' mailbox view

In reply to Bernhard Beschow

As indicated in the comments, the mb[] a.k.a. mbs [] views should cover 0x400 (1024) bytes. However, the mb[] array covers four times the size since sizeof(FlexcanRegsMessageBuffer) returns the size in bytes instead of counting the number of uint32_t fields. This shifts the subsequent register offsets by 0xc00 which is not intended. Fix the size and thus the offsets by dropping the now unused mb[] view.

Note that the different size changes the migration layout. Since the device model is new, no change in the version fields is needed.

---
 include/hw/net/flexcan.h | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
include/hw/net/flexcan.h +2 -4
index d8159475e0..9affdff837 100644
@@ -65,10 +65,8 @@ typedef struct FlexcanRegs {
     uint32_t dbg1;               /* 0x58, unused */
     uint32_t dbg2;               /* 0x5C, unused */
     uint32_t _reserved3[8];      /* 0x60 */
-    union {                      /* 0x80 - not affected by soft reset */
-        uint32_t mb[sizeof(FlexcanRegsMessageBuffer) * FLEXCAN_MAILBOX_COUNT];
-        FlexcanRegsMessageBuffer mbs[FLEXCAN_MAILBOX_COUNT];
-    };
+    /* 0x80 - not affected by soft reset */
+    FlexcanRegsMessageBuffer mbs[FLEXCAN_MAILBOX_COUNT];
     uint32_t _reserved4[256];    /* 0x480 */
     uint32_t rximr[64];          /* 0x880 - not affected by soft reset */
     uint32_t _reserved5[24];     /* 0x980 */
Signature · 1 line
2.55.0
Reviewed-by on v1 7/9
#14 lore raw

Re: [PATCH 7/9] hw/net/flexcan: Drop oversized 'mb[]' mailbox view

Quoted 9 lines · Bernhard Beschow

As indicated in the comments, the mb[] a.k.a. mbs [] views should cover 0x400 (1024) bytes. However, the mb[] array covers four times the size since sizeof(FlexcanRegsMessageBuffer) returns the size in bytes instead of counting the number of uint32_t fields. This shifts the subsequent register offsets by 0xc00 which is not intended. Fix the size and thus the offsets by dropping the now unused mb[] view.

Note that the different size changes the migration layout. Since the device model is new, no change in the version fields is needed.

typedef struct FlexcanState {
    ...
    union {
        FlexcanRegs     regs;
        uint32_t        regs_raw[sizeof(FlexcanRegs) / 4];
    };
    ...
} FlexcanState;
static const VMStateDescription vmstate_can = {
    .name = TYPE_CAN_FLEXCAN,
    .version_id = 1,
    .minimum_version_id = 1,
    .fields = (const VMStateField[]) {
        ...
        VMSTATE_UINT32_ARRAY(regs_raw, FlexcanState, 

sizeof(FlexcanRegs) / 4),

        ^^^

This "consider this structure as a casted array of u32" is very fragile anyway, and will unlikely scale over time.

        ...
        VMSTATE_END_OF_LIST(),
    },
};
Quoted 4 lines
---
  include/hw/net/flexcan.h | 6 ++----
  1 file changed, 2 insertions(+), 4 deletions(-)
#15 lore raw

Re: [PATCH 7/9] hw/net/flexcan: Drop oversized 'mb[]' mailbox view

Am 23. Juli 2026 08:20:34 UTC schrieb "Philippe Mathieu-Daudé" <[email protected]>:

Quoted 30 lines · Bernhard Beschow
Quoted 9 lines

As indicated in the comments, the mb[] a.k.a. mbs [] views should cover 0x400 (1024) bytes. However, the mb[] array covers four times the size since sizeof(FlexcanRegsMessageBuffer) returns the size in bytes instead of counting the number of uint32_t fields. This shifts the subsequent register offsets by 0xc00 which is not intended. Fix the size and thus the offsets by dropping the now unused mb[] view.

Note that the different size changes the migration layout. Since the device model is new, no change in the version fields is needed.

typedef struct FlexcanState {
   ...
   union {
       FlexcanRegs     regs;
       uint32_t        regs_raw[sizeof(FlexcanRegs) / 4];
   };
   ...
} FlexcanState;
static const VMStateDescription vmstate_can = {
   .name = TYPE_CAN_FLEXCAN,
   .version_id = 1,
   .minimum_version_id = 1,
   .fields = (const VMStateField[]) {
       ...
       VMSTATE_UINT32_ARRAY(regs_raw, FlexcanState, sizeof(FlexcanRegs) / 4),

       ^^^
Quoted 7 lines

This "consider this structure as a casted array of u32" is very fragile anyway, and will unlikely scale over time.

       ...
       VMSTATE_END_OF_LIST(),

},
};

Any suggestions what to do instead?

Quoted 2 lines

Thanks Phil for the very quick responses!

Best regards,
Berngard

Quoted 5 lines
Quoted 5 lines
---
  include/hw/net/flexcan.h | 6 ++----
  1 file changed, 2 insertions(+), 4 deletions(-)
#16 lore raw

Re: [PATCH 7/9] hw/net/flexcan: Drop oversized 'mb[]' mailbox view

Quoted 45 lines · Bernhard Beschow

Am 23. Juli 2026 08:20:34 UTC schrieb "Philippe Mathieu-Daudé" <[email protected]>:

Quoted 41 lines · Bernhard Beschow
Quoted 12 lines

As indicated in the comments, the mb[] a.k.a. mbs [] views should
cover 0x400 (1024) bytes. However, the mb[] array covers four times
the size since sizeof(FlexcanRegsMessageBuffer) returns the size in
bytes instead of counting the number of uint32_t fields. This shifts the
subsequent register offsets by 0xc00 which is not intended. Fix the size
and thus the offsets by dropping the now unused mb[] view.

Quoted 3 lines

Note that the different size changes the migration layout. Since the device
model is new, no change in the version fields is needed.

typedef struct FlexcanState {
   ...
   union {
       FlexcanRegs     regs;
       uint32_t        regs_raw[sizeof(FlexcanRegs) / 4]; };
   ...
} FlexcanState;

static const VMStateDescription vmstate_can = { .name = TYPE_CAN_FLEXCAN,
   .version_id = 1,
   .minimum_version_id = 1,
   .fields = (const VMStateField[]) {
       ...
       VMSTATE_UINT32_ARRAY(regs_raw, FlexcanState, sizeof(FlexcanRegs) / 4),

       ^^^

This "consider this structure as a casted array of u32" is very fragile
anyway, and will unlikely scale over time.

Quoted 6 lines
Quoted 4 lines

       ...
       VMSTATE_END_OF_LIST(),
   },
};

Any suggestions what to do instead?

The oversized mb[] view is my mistake, it should have been the same length as mbs[], i forgot to divide its size by sizeof(u32)... I realy meant the mailboxes to be an u32 array, and the struct laid over it was just a convenience. It should have been

uint32_t mb[sizeof(FlexcanRegsMessageBuffer) * FLEXCAN_MAILBOX_COUNT / 4];

The regs_raw entry in FlexcanState is the same idea. We tried to avoid any endianess-related bugs by treating the registers as an array of u32's, and always accessing them as dwords. I see now it's not the ideal solution, and I should have documented this choice.

Sorry, Matt

Signature · 8 lines
                Matyáš (Matt) Bobek

    Email:      [email protected]

    Department of Control Engineering FEE CVUT
    Karlovo namesti 13, 121 35, Prague 2
    Open Technologies Research Education and Exchange Services
    https://gitlab.fel.cvut.cz/otrees/org/-/wikis/home
#17 lore raw

Re: [PATCH 7/9] hw/net/flexcan: Drop oversized 'mb[]' mailbox view

In reply to Bernhard Beschow

Hello Bernhard and Philippe,

Quoted 1 line · On Thursday 23 of July 2026 12:26:41 Bernhard Beschow wrote:

Am 23. Juli 2026 08:20:34 UTC schrieb "Philippe Mathieu-Daudé"

<[email protected]>:

Quoted 34 lines
Quoted 34 lines · Bernhard Beschow
Quoted 9 lines

As indicated in the comments, the mb[] a.k.a. mbs [] views should cover 0x400 (1024) bytes. However, the mb[] array covers four times the size since sizeof(FlexcanRegsMessageBuffer) returns the size in bytes instead of counting the number of uint32_t fields. This shifts the subsequent register offsets by 0xc00 which is not intended. Fix the size and thus the offsets by dropping the now unused mb[] view.

Note that the different size changes the migration layout. Since the device model is new, no change in the version fields is needed.

typedef struct FlexcanState {
   ...
   union {
       FlexcanRegs     regs;
       uint32_t        regs_raw[sizeof(FlexcanRegs) / 4];
   };
   ...
} FlexcanState;

static const VMStateDescription vmstate_can = {
.name = TYPE_CAN_FLEXCAN,
.version_id = 1,
.minimum_version_id = 1,
.fields = (const VMStateField[]) {
...
VMSTATE_UINT32_ARRAY(regs_raw, FlexcanState, sizeof(FlexcanRegs) /
4),

       ^^^

This "consider this structure as a casted array of u32" is very fragile anyway, and will unlikely scale over time.

Quoted 7 lines
Quoted 4 lines
       ...
       VMSTATE_END_OF_LIST(),

},
};

Any suggestions what to do instead?

I would suggest to keep the state as uint32_t array. It is accessed by this and only by this size as the FlexCAN MemoryRegionOps are defined. In the fact I would like/plan in longer term, even to switch the mbs array to be plain area of uint32_t in the future, that is remove field

  FlexcanRegsMessageBuffer mbs[FLEXCAN_MAILBOX_COUNT];

in FlexcanRegs.

There should be only function which would return start of given FlexcanRegsMessageBuffer based on index argument and actual message buffers configuration. It is required for CAN FD where message buffer size is configurable. The raw/uint32_t area is kept the same, but it is distributed to the indexed buffers different way.

So my preference is to move from structured view in direction to the offsets in the words array. Even the registers could be defined as offsets to this array in the theory but as the overlay structure for register accesses is used even in the Linux kernel driver, I do not think that this is necessary.

But replacement of

  FlexcanRegsMessageBuffer mbs[FLEXCAN_MAILBOX_COUNT];

with appropriate

  uint32_t mbs[FLEXCAN_MAILBOX_AREA_WORD_COUNT];

and access functions would please me, because it helps with future CAN FD support.

Best wishes,

Pavel Pisa
phone: +420 603531357
e-mail: [email protected]
Department of Control Engineering FEE CVUT Karlovo namesti 13, 121 35, Prague 2
university: http://control.fel.cvut.cz/
personal: http://cmp.felk.cvut.cz/~pisa
social: https://social.kernel.org/ppisa
projects: https://www.openhub.net/accounts/ppisa
CAN related:http://canbus.pages.fel.cvut.cz/ RISC-V education: https://comparch.edu.cvut.cz/
Open Technologies Research Education and Exchange Services
https://gitlab.fel.cvut.cz/otrees/org/-/wikis/home

#18 lore raw

Re: [PATCH 7/9] hw/net/flexcan: Drop oversized 'mb[]' mailbox view

Am 23. Juli 2026 10:51:05 UTC schrieb Pavel Pisa <[email protected]>:

Quoted 1 line

Hello Bernhard and Philippe,

Hi Pavel,

Quoted 74 lines · On Thursday 23 of July 2026 12:26:41 Bernhard Beschow wrote:
Quoted 1 line

Am 23. Juli 2026 08:20:34 UTC schrieb "Philippe Mathieu-Daudé"

<[email protected]>:

Quoted 34 lines
Quoted 34 lines · Bernhard Beschow
Quoted 9 lines

As indicated in the comments, the mb[] a.k.a. mbs [] views should cover 0x400 (1024) bytes. However, the mb[] array covers four times the size since sizeof(FlexcanRegsMessageBuffer) returns the size in bytes instead of counting the number of uint32_t fields. This shifts the subsequent register offsets by 0xc00 which is not intended. Fix the size and thus the offsets by dropping the now unused mb[] view.

Note that the different size changes the migration layout. Since the device model is new, no change in the version fields is needed.

typedef struct FlexcanState {
   ...
   union {
       FlexcanRegs     regs;
       uint32_t        regs_raw[sizeof(FlexcanRegs) / 4];
   };
   ...
} FlexcanState;

static const VMStateDescription vmstate_can = {
.name = TYPE_CAN_FLEXCAN,
.version_id = 1,
.minimum_version_id = 1,
.fields = (const VMStateField[]) {
...
VMSTATE_UINT32_ARRAY(regs_raw, FlexcanState, sizeof(FlexcanRegs) /
4),

       ^^^

This "consider this structure as a casted array of u32" is very fragile anyway, and will unlikely scale over time.

Quoted 7 lines
Quoted 4 lines
       ...
       VMSTATE_END_OF_LIST(),

},
};

Any suggestions what to do instead?

I would suggest to keep the state as uint32_t array. It is accessed by this and only by this size as the FlexCAN MemoryRegionOps are defined. In the fact I would like/plan in longer term, even to switch the mbs array to be plain area of uint32_t in the future, that is remove field

FlexcanRegsMessageBuffer mbs[FLEXCAN_MAILBOX_COUNT];

in FlexcanRegs.

There should be only function which would return start of given FlexcanRegsMessageBuffer based on index argument and actual message buffers configuration. It is required for CAN FD where message buffer size is configurable. The raw/uint32_t area is kept the same, but it is distributed to the indexed buffers different way.

So my preference is to move from structured view in direction to the offsets in the words array. Even the registers could be defined as offsets to this array in the theory but as the overlay structure for register accesses is used even in the Linux kernel driver, I do not think that this is necessary.

But replacement of

Quoted 8 lines

FlexcanRegsMessageBuffer mbs[FLEXCAN_MAILBOX_COUNT];

with appropriate

uint32_t mbs[FLEXCAN_MAILBOX_AREA_WORD_COUNT];

and access functions would please me, because it helps with future CAN FD support.

Having CAN FD support would be nice indeed. Let's keep the code simple for now though until it gets implemented. Having just one way of doing things helps avoid traps such as those fixed in this series. We can change the code any time as we need it.

Best regards,
Bernhard

Quoted 16 lines

Best wishes,

Pavel Pisa
phone: +420 603531357
e-mail: [email protected]
Department of Control Engineering FEE CVUT Karlovo namesti 13, 121 35, Prague 2
university: http://control.fel.cvut.cz/

   personal:   http://cmp.felk.cvut.cz/~pisa
   social:     https://social.kernel.org/ppisa
   projects:   https://www.openhub.net/accounts/ppisa

CAN related:http://canbus.pages.fel.cvut.cz/ RISC-V education: https://comparch.edu.cvut.cz/
Open Technologies Research Education and Exchange Services
https://gitlab.fel.cvut.cz/otrees/org/-/wikis/home

#19 lore raw

Re: [PATCH 7/9] hw/net/flexcan: Drop oversized 'mb[]' mailbox view

Hello Bernhard,

Quoted 85 lines · On Thursday 23 of July 2026 20:31:00 Bernhard Beschow wrote:

Am 23. Juli 2026 10:51:05 UTC schrieb Pavel Pisa <[email protected]>:

Quoted 1 line

Hello Bernhard and Philippe,

Hi Pavel,

Quoted 80 lines · On Thursday 23 of July 2026 12:26:41 Bernhard Beschow wrote:
Quoted 1 line

Am 23. Juli 2026 08:20:34 UTC schrieb "Philippe Mathieu-Daudé"

<[email protected]>:

Quoted 34 lines
Quoted 34 lines · Bernhard Beschow
Quoted 9 lines

As indicated in the comments, the mb[] a.k.a. mbs [] views should cover 0x400 (1024) bytes. However, the mb[] array covers four times the size since sizeof(FlexcanRegsMessageBuffer) returns the size in bytes instead of counting the number of uint32_t fields. This shifts the subsequent register offsets by 0xc00 which is not intended. Fix the size and thus the offsets by dropping the now unused mb[] view.

Note that the different size changes the migration layout. Since the device model is new, no change in the version fields is needed.

typedef struct FlexcanState {
   ...
   union {
       FlexcanRegs     regs;
       uint32_t        regs_raw[sizeof(FlexcanRegs) / 4];
   };
   ...
} FlexcanState;
static const VMStateDescription vmstate_can = {
   .name = TYPE_CAN_FLEXCAN,
   .version_id = 1,
   .minimum_version_id = 1,
   .fields = (const VMStateField[]) {
       ...
       VMSTATE_UINT32_ARRAY(regs_raw, FlexcanState,

sizeof(FlexcanRegs) / 4),

       ^^^

This "consider this structure as a casted array of u32" is very fragile anyway, and will unlikely scale over time.

Quoted 6 lines
Quoted 4 lines
       ...
       VMSTATE_END_OF_LIST(),

},
};

Any suggestions what to do instead?

I would suggest to keep the state as uint32_t array. It is accessed by this and only by this size as the FlexCAN MemoryRegionOps are defined. In the fact I would like/plan in longer term, even to switch the mbs array to be plain area of uint32_t in the future, that is remove field

FlexcanRegsMessageBuffer mbs[FLEXCAN_MAILBOX_COUNT];

in FlexcanRegs.

There should be only function which would return start of given FlexcanRegsMessageBuffer based on index argument and actual message buffers configuration. It is required for CAN FD where message buffer size is configurable. The raw/uint32_t area is kept the same, but it is distributed to the indexed buffers different way.

So my preference is to move from structured view in direction to the offsets in the words array. Even the registers could be defined as offsets to this array in the theory but as the overlay structure for register accesses is used even in the Linux kernel driver, I do not think that this is necessary.

But replacement of

FlexcanRegsMessageBuffer mbs[FLEXCAN_MAILBOX_COUNT];

with appropriate

uint32_t mbs[FLEXCAN_MAILBOX_AREA_WORD_COUNT];

Quoted 7 lines
Quoted 2 lines

and access functions would please me, because it helps with future CAN FD support.

Having CAN FD support would be nice indeed. Let's keep the code simple for now though until it gets implemented. Having just one way of doing things helps avoid traps such as those fixed in this series. We can change the code any time as we need it.

My main point is that attempt to implement VMStateDescription some more structural way, i.e. mbs as array of corresponding structures (which can be what has Philippe in mind), is counterproductive from the longer term perspective, because the mbs role is the array of words (uint32_t) which is interpretted according to other registers in the FlexCAN3. In FlexCAN2, interpretation is fixed.
So storing whole area including control registers as array
of uint32_t matches reality quite well.

So I agree with keeping VMStateDescription unchanged.

Best wishes,

Pavel

Pavel Pisa
phone: +420 603531357
e-mail: [email protected]
Department of Control Engineering FEE CVUT Karlovo namesti 13, 121 35, Prague 2
university: http://control.fel.cvut.cz/
personal: http://cmp.felk.cvut.cz/~pisa
social: https://social.kernel.org/ppisa
projects: https://www.openhub.net/accounts/ppisa
CAN related:http://canbus.pages.fel.cvut.cz/ RISC-V education: https://comparch.edu.cvut.cz/
Open Technologies Research Education and Exchange Services
https://gitlab.fel.cvut.cz/otrees/org/-/wikis/home

#20 lore raw

PATCH 8/9 hw/net/can/flexcan: Fix RXIMR reset mask

In reply to Bernhard Beschow

The RXIMR registers are not affected by soft reset, so their reset mask must preserve all entries. Set the mask for the entire array instead of only the first register.

---
 hw/net/can/flexcan.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/net/can/flexcan.c +1 -1
index f84f090a7f..205d0d6413 100644
@@ -140,7 +140,7 @@ static const FlexcanRegs flexcan_regs_reset_mask = {
         .data = { 0xFFFFFFFF, 0xFFFFFFFF },
     } },
     ._reserved4 = {0},
-    .rximr = {0xFFFFFFFF},
+    .rximr = { [0 ... 63] = 0xFFFFFFFF },
     ._reserved5 = {0},
     .gfwr_mx6 = 0,
     ._reserved6 = {0},
Signature · 1 line
2.55.0
Reviewed-by on v1 8/9
#21 lore raw

Re: [PATCH 8/9] hw/net/can/flexcan: Fix RXIMR reset mask

Quoted 8 lines · Bernhard Beschow

The RXIMR registers are not affected by soft reset, so their reset mask must preserve all entries. Set the mask for the entire array instead of only the first register.

---
  hw/net/can/flexcan.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
#22 lore raw

PATCH 9/9 hw/net/can/flexcan: Remove RX SMB raw view

In reply to Bernhard Beschow

Use the existing rx_smb0 message buffer view for register masks instead of the raw register array. Since all accesses now use the structured view, remove the redundant rx_smb0_raw union member.

---
 include/hw/net/flexcan.h |  5 +----
 hw/net/can/flexcan.c     | 12 ++++++++++--
 2 files changed, 11 insertions(+), 6 deletions(-)
include/hw/net/flexcan.h +1 -4
index 9affdff837..eee70bad42 100644
@@ -81,10 +81,7 @@ typedef struct FlexcanRegs {
     uint32_t _rx14mask;          /* 0xAA8 */
     uint32_t _rx15mask;          /* 0xAAC */
     uint32_t tx_smb[4];          /* 0xAB0 */
-    union {                      /* 0xAC0, used for SMB emulation */
-        uint32_t rx_smb0_raw[4];
-        FlexcanRegsMessageBuffer rx_smb0;
-    };
+    FlexcanRegsMessageBuffer rx_smb0; /* 0xAC0, used for SMB emulation */
     uint32_t rx_smb1[4];         /* 0xAD0 */
     uint32_t mecr;               /* 0xAE0 */
     uint32_t erriar;             /* 0xAE4 */
hw/net/can/flexcan.c +10 -2
index 205d0d6413..a8bad83cb8 100644
@@ -106,7 +106,11 @@ static const FlexcanRegs flexcan_regs_write_mask = {
     .gfwr_mx6 = 0xFFFFFFFF,
     ._reserved6 = {0},
     ._reserved8 = {0},
-    .rx_smb0_raw = {0, 0, 0, 0},
+    .rx_smb0 = {
+        .can_ctrl = 0,
+        .can_id = 0,
+        .data = { 0, 0 },
+    },
     .rx_smb1 = {0, 0, 0, 0},
 };
 static const FlexcanRegs flexcan_regs_reset_mask = {
@@ -145,7 +149,11 @@ static const FlexcanRegs flexcan_regs_reset_mask = {
     .gfwr_mx6 = 0,
     ._reserved6 = {0},
     ._reserved8 = {0},
-    .rx_smb0_raw = {0, 0, 0, 0},
+    .rx_smb0 = {
+        .can_ctrl = 0,
+        .can_id = 0,
+        .data = { 0, 0 },
+    },
     .rx_smb1 = {0, 0, 0, 0},
 };
 
Signature · 1 line
2.55.0
Reviewed-by on v1 9/9
#23 lore raw

Re: [PATCH 9/9] hw/net/can/flexcan: Remove RX SMB raw view

Quoted 9 lines · Bernhard Beschow

Use the existing rx_smb0 message buffer view for register masks instead of the raw register array. Since all accesses now use the structured view, remove the redundant rx_smb0_raw union member.

---
  include/hw/net/flexcan.h |  5 +----
  hw/net/can/flexcan.c     | 12 ++++++++++--
  2 files changed, 11 insertions(+), 6 deletions(-)
Tested-by on the v1 series
#24 lore raw
In reply to Bernhard Beschow

Hello Bernhard and others,

Quoted 28 lines · On Thursday 23 of July 2026 09:00:50 Bernhard Beschow wrote:

This series attempts to fix the Coveriy issues pointed out by Peter in the FlexCAN emulation [1]. In addition, it fixes some reset masks to preserve register contents across resets as intended. It also fixes some register offsets due to a union being inadvertantly too large; this would affect migration if addressed after the release. Last but not least it eliminates an unneeded union.

Testing done:
* make check
* Run cangen and candump on the host and guest in both directions

[1]
https://lore.kernel.org/qemu-devel/CAFEAcA-QBiPfgN4YJXD9FQULobpBVNgeJHSqG7E
[email protected]/

Bernhard Beschow (9):
hw/net/can/flexcan: Use mbs[] array for FIFO pop hw/net/flexcan: Drop unused RX FIFO register overlay hw/net/can/flexcan: Fix out-of-bounds access in flexcan_mx_rx() hw/net/can/flexcan: Fix mailbox index calculation in
flexcan_mem_write()
hw/net/can/flexcan: Fix mailbox index calculation in
flexcan_mem_read()
hw/net/can/flexcan: Fix mailbox reset mask initialization hw/net/flexcan: Drop oversized 'mb[]' mailbox view hw/net/can/flexcan: Fix RXIMR reset mask hw/net/can/flexcan: Remove RX SMB raw view

Quoted 3 lines
 include/hw/net/flexcan.h | 22 +++----------------
 hw/net/can/flexcan.c     | 47 ++++++++++++++++++++++++----------------
 2 files changed, 31 insertions(+), 38 deletions(-)

I have tested QEMU mainline with your patches applied to behave correctly with SableLitte machine emulation and sending of CAN frames from it and to it from the host system. You can add my confirmation individual patches as well if you want.

best wishes,

Pavel Pisa
phone: +420 603531357
e-mail: [email protected]
Department of Control Engineering FEE CVUT Karlovo namesti 13, 121 35, Prague 2
university: http://control.fel.cvut.cz/
personal: http://cmp.felk.cvut.cz/~pisa
social: https://social.kernel.org/ppisa
projects: https://www.openhub.net/accounts/ppisa
CAN related:http://canbus.pages.fel.cvut.cz/ RISC-V education: https://comparch.edu.cvut.cz/
Open Technologies Research Education and Exchange Services
https://gitlab.fel.cvut.cz/otrees/org/-/wikis/home

#25 lore raw
In reply to Bernhard Beschow
Quoted 8 lines · Bernhard Beschow

This series attempts to fix the Coveriy issues pointed out by Peter in the FlexCAN emulation [1]. In addition, it fixes some reset masks to preserve register contents across resets as intended. It also fixes some register offsets due to a union being inadvertantly too large; this would affect migration if addressed after the release. Last but not least it eliminates an unneeded union.

Quoted 6 lines

Testing done:
* make check
* Run cangen and candump on the host and guest in both directions

[1] https://lore.kernel.org/qemu-devel/CAFEAcA-QBiPfgN4YJXD9FQULobpBVNgeJHSqG7Eqg1CzyKTfaA@mail.gmail.com/

Thanks for fixing these; applied to target-arm.next.

-- PMM