[PATCH v2] hw/arm_sysctl: fix extracting 31th bit of val

Anastasia Belova posted 1 patch 1 year, 1 month ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20241220125429.7552-1-abelova@astralinux.ru
Maintainers: Peter Maydell <peter.maydell@linaro.org>
hw/misc/arm_sysctl.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH v2] hw/arm_sysctl: fix extracting 31th bit of val
Posted by Anastasia Belova 1 year, 1 month ago
1 << 31 is casted to uint64_t while bitwise and with val.
So this value may become 0xffffffff80000000 but only
31th "start" bit is required.

Use the bitfield extract() API instead.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Signed-off-by: Anastasia Belova <abelova@astralinux.ru>
---
v2: make extracting more clear
 hw/misc/arm_sysctl.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/hw/misc/arm_sysctl.c b/hw/misc/arm_sysctl.c
index 69e379fa10..c4e93da3cd 100644
--- a/hw/misc/arm_sysctl.c
+++ b/hw/misc/arm_sysctl.c
@@ -520,7 +520,7 @@ static void arm_sysctl_write(void *opaque, hwaddr offset,
          * as zero.
          */
         s->sys_cfgctrl = val & ~((3 << 18) | (1 << 31));
-        if (val & (1 << 31)) {
+        if (extract64(val, 31, 1)) {
             /* Start bit set -- actually do something */
             unsigned int dcc = extract32(s->sys_cfgctrl, 26, 4);
             unsigned int function = extract32(s->sys_cfgctrl, 20, 6);
-- 
2.47.0
Re: [PATCH v2] hw/arm_sysctl: fix extracting 31th bit of val
Posted by Peter Maydell 1 year ago
On Fri, 20 Dec 2024 at 12:54, Anastasia Belova <abelova@astralinux.ru> wrote:
>
> 1 << 31 is casted to uint64_t while bitwise and with val.
> So this value may become 0xffffffff80000000 but only
> 31th "start" bit is required.

This can't happen, because the MemoryRegionOps uses the
default max access size of 4 bytes, and so the high bytes
of val will always be zero. But the extract API is clearer
anyway, so the change is worthwhile.


Applied to target-arm.next with a clarification to the
commit message that there's no guest-visible wrong
behaviour; thanks.

-- PMM