Skip to contentSkip to conversation

x86/emul: Work around MISRA R13.2 complaint

PATCH In review Andrew Cooper 2 participants 2 messages May 10, 2025 – May 12, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/emul: Work around MISRA R13.2 complaint

Rule 13.2 states: "The value of an expression and its persistent side effects shall be the same under all permitted evaluation orders".

Eclair complains about a Rule 13.2 violations because validate_far_branch() assigns to rc, and the entirety of commit_far_branch() is also assigned to rc.

I'm unsure that the complaint is accurate, but rewriting commit_far_branch() to use the comma operator seems to make Eclair happy.

---
CC: Stefano Stabellini <[email protected]>
CC: Nicola Vetrini <[email protected]>

---
 xen/arch/x86/x86_emulate/x86_emulate.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
xen/arch/x86/x86_emulate/x86_emulate.c +8 -6
index 8e14ebb35b0e..6ee64cb85987 100644
@@ -333,12 +333,14 @@ do {                                                                    \
                               : (ip) > (cs)->limit, X86_EXC_GP, 0);     \
 })
 
-#define commit_far_branch(cs, newip) ({                                 \
-    validate_far_branch(cs, newip);                                     \
-    _regs.r(ip) = (newip);                                              \
-    singlestep = _regs.eflags & X86_EFLAGS_TF;                          \
-    ops->write_segment(x86_seg_cs, cs, ctxt);                           \
-})
+#define commit_far_branch(cs, newip) (                                  \
+        ({                                                              \
+            validate_far_branch(cs, newip);                             \
+            _regs.r(ip) = (newip);                                      \
+            singlestep = _regs.eflags & X86_EFLAGS_TF;                  \
+        }),                                                             \
+        ops->write_segment(x86_seg_cs, cs, ctxt)                        \
+    )
 
 int x86emul_get_fpu(
     enum x86_emulate_fpu_type type,

base-commit: 9b3a02e66f058ebd77db6628e3144352857bdf2b

Signature · 1 line
2.39.5
#1 lore raw
Quoted 5 lines · Andrew Cooper

Rule 13.2 states: "The value of an expression and its persistent side effects shall be the same under all permitted evaluation orders".

Eclair complains about a Rule 13.2 violations because validate_far_branch() assigns to rc,

Followed by "goto", i.e. not taking the path to ...

Quoted 1 line

and the entirety of commit_far_branch() is also assigned to rc.

... that assignment. This pretty clearly looks like a tool shortcoming to me, and I think it would be a good idea to ...

Quoted 10 lines
xen/arch/x86/x86_emulate/x86_emulate.c +0 -4
@@ -333,12 +333,14 @@ do {                                                                    \
                               : (ip) > (cs)->limit, X86_EXC_GP, 0);     \
 })
 
-#define commit_far_branch(cs, newip) ({                                 \
-    validate_far_branch(cs, newip);                                     \
-    _regs.r(ip) = (newip);                                              \
-    singlestep = _regs.eflags & X86_EFLAGS_TF;                          \

xen/arch/x86/x86_emulate/x86_emulate.c do { \ · line 343

Quoted 10 lines
xen/arch/x86/x86_emulate/x86_emulate.c +8 -2
-    ops->write_segment(x86_seg_cs, cs, ctxt);                           \
-})
+#define commit_far_branch(cs, newip) (                                  \
+        ({                                                              \
+            validate_far_branch(cs, newip);                             \
+            _regs.r(ip) = (newip);                                      \
+            singlestep = _regs.eflags & X86_EFLAGS_TF;                  \
+        }),                                                             \
+        ops->write_segment(x86_seg_cs, cs, ctxt)                        \
+    )

... add a brief comment here clarifying why this odd a statement is used.

Jan