FlexCAN fixes for-11.1
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
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
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
@@ -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
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 overlappingfifounion view. This makes it explicit that the operation copies within a contiguous mailbox array and avoids Coverity CID 1662971.Reported-by Peter Maydell <[email protected]>Signed-off-by Bernhard Beschow <[email protected]>--- hw/net/can/flexcan.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
PATCH 2/9 hw/net/flexcan: Drop unused RX FIFO register overlay
In reply to Bernhard BeschowRemove 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
@@ -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
Re: [PATCH 2/9] hw/net/flexcan: Drop unused RX FIFO register overlay
Quoted 7 lines · Bernhard Beschow
Remove the unused
FlexcanRegsRXFifounion view now that FIFO accesses use the underlyingmbs[]array directly.Signed-off-by Bernhard Beschow <[email protected]>--- include/hw/net/flexcan.h | 11 ----------- 1 file changed, 11 deletions(-)
PATCH 3/9 hw/net/can/flexcan: Fix out-of-bounds access in flexcan_mx_rx()
In reply to Bernhard BeschowRequire 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
@@ -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
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 >= 0before indexingmbs[]. This prevents a possible-1array index and fixes Coverity CID 1662790.Reported-by Peter Maydell <[email protected]>Signed-off-by Bernhard Beschow <[email protected]>--- hw/net/can/flexcan.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)hw/net/can/flexcan.c +1
-2@@ -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
PATCH 4/9 hw/net/can/flexcan: Fix mailbox index calculation in flexcan_mem_write()
In reply to Bernhard BeschowCalculate 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
@@ -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
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 rawmb[]view. This prevents accessing mailbox entries beyond the valid array range and fixes Coverity CID 1662974.Reported-by Peter Maydell <[email protected]>Signed-off-by Bernhard Beschow <[email protected]>--- hw/net/can/flexcan.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
thanks
-- PMM
PATCH 5/9 hw/net/can/flexcan: Fix mailbox index calculation in flexcan_mem_read()
In reply to Bernhard BeschowCalculate 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
@@ -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
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 rawmb[]view. This prevents accessing mailbox entries beyond the valid array range and fixes Coverity CID 1662974.Reported-by Peter Maydell <[email protected]>Signed-off-by Bernhard Beschow <[email protected]>--- hw/net/can/flexcan.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-)hw/net/can/flexcan.c +2
-0@@ -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
PATCH 6/9 hw/net/can/flexcan: Fix mailbox reset mask initialization
In reply to Bernhard BeschowThe 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
@@ -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
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 thembs[]view instead in order to drop the redundant and oversizedmb[]view in the next commit.Signed-off-by Bernhard Beschow <[email protected]>--- hw/net/can/flexcan.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
PATCH 7/9 hw/net/flexcan: Drop oversized 'mb[]' mailbox view
In reply to Bernhard BeschowAs 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
@@ -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
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 cover0x400(1024) bytes. However, themb[]array covers four times the size sincesizeof(FlexcanRegsMessageBuffer)returns the size in bytes instead of counting the number ofuint32_tfields. This shifts the subsequent register offsets by0xc00which is not intended. Fix the size and thus the offsets by dropping the now unusedmb[]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
Signed-off-by Bernhard Beschow <[email protected]>--- include/hw/net/flexcan.h | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
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 cover0x400(1024) bytes. However, themb[]array covers four times the size sincesizeof(FlexcanRegsMessageBuffer)returns the size in bytes instead of counting the number ofuint32_tfields. This shifts the subsequent register offsets by0xc00which is not intended. Fix the size and thus the offsets by dropping the now unusedmb[]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
Reviewed-by Philippe Mathieu-Daudé <[email protected]>
Thanks Phil for the very quick responses!
Best regards,
Berngard
Quoted 5 lines
Quoted 5 lines
Signed-off-by Bernhard Beschow <[email protected]>--- include/hw/net/flexcan.h | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
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
cover0x400(1024) bytes. However, themb[]array covers four times
the size sincesizeof(FlexcanRegsMessageBuffer)returns the size in
bytes instead of counting the number ofuint32_tfields. This shifts the
subsequent register offsets by0xc00which is not intended. Fix the size
and thus the offsets by dropping the now unusedmb[]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/homeRe: [PATCH 7/9] hw/net/flexcan: Drop oversized 'mb[]' mailbox view
In reply to Bernhard BeschowHello 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é"
Quoted 34 lines
Quoted 34 lines · Bernhard Beschow
Quoted 9 lines
As indicated in the comments, the
mb[]a.k.a.mbs []views should cover0x400(1024) bytes. However, themb[]array covers four times the size sincesizeof(FlexcanRegsMessageBuffer)returns the size in bytes instead of counting the number ofuint32_tfields. This shifts the subsequent register offsets by0xc00which is not intended. Fix the size and thus the offsets by dropping the now unusedmb[]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
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é"
Quoted 34 lines
Quoted 34 lines · Bernhard Beschow
Quoted 9 lines
As indicated in the comments, the
mb[]a.k.a.mbs []views should cover0x400(1024) bytes. However, themb[]array covers four times the size sincesizeof(FlexcanRegsMessageBuffer)returns the size in bytes instead of counting the number ofuint32_tfields. This shifts the subsequent register offsets by0xc00which is not intended. Fix the size and thus the offsets by dropping the now unusedmb[]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_tarray. 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 ofuint32_tin the future, that is remove fieldFlexcanRegsMessageBuffer 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_tmbs[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/ppisaCAN 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
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é"
Quoted 34 lines
Quoted 34 lines · Bernhard Beschow
Quoted 9 lines
As indicated in the comments, the
mb[]a.k.a.mbs []views should cover0x400(1024) bytes. However, themb[]array covers four times the size sincesizeof(FlexcanRegsMessageBuffer)returns the size in bytes instead of counting the number ofuint32_tfields. This shifts the subsequent register offsets by0xc00which is not intended. Fix the size and thus the offsets by dropping the now unusedmb[]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_tarray. 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 ofuint32_tin the future, that is remove fieldFlexcanRegsMessageBuffer 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_tmbs[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
PATCH 8/9 hw/net/can/flexcan: Fix RXIMR reset mask
In reply to Bernhard BeschowThe 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
@@ -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
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.
Signed-off-by Bernhard Beschow <[email protected]>--- hw/net/can/flexcan.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
PATCH 9/9 hw/net/can/flexcan: Remove RX SMB raw view
In reply to Bernhard BeschowUse 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
@@ -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
@@ -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
Re: [PATCH 9/9] hw/net/can/flexcan: Remove RX SMB raw view
Quoted 9 lines · Bernhard Beschow
Use the existing
rx_smb0message buffer view for register masks instead of the raw register array. Since all accesses now use the structured view, remove the redundantrx_smb0_rawunion member.Signed-off-by Bernhard Beschow <[email protected]>--- include/hw/net/flexcan.h | 5 +---- hw/net/can/flexcan.c | 12 ++++++++++-- 2 files changed, 11 insertions(+), 6 deletions(-)
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
* Runcangenandcandumpon 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 inflexcan_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
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
* Runcangenandcandumpon the host and guest in both directions
Thanks for fixing these; applied to target-arm.next.
-- PMM