fix for bug 1859021
cover fix for bug 1859021
Hi,
This is a fairly trivial fix for a uint64_t overflow however I spent some time making sure I understood why we got stuck in a busy loop and adding tests to both tcg and kvm-unit-tests.
Alex Bennée (2):
target/arm: detect 64 bit overflow caused by high cval + voff tests/tcg: add a vtimer test for aarch64
target/arm/helper.c | 3 + tests/tcg/aarch64/system/vtimer.c | 80 +++++++++++++++++++++++ tests/tcg/aarch64/Makefile.softmmu-target | 4 ++ 3 files changed, 87 insertions(+) create mode 100644 tests/tcg/aarch64/system/vtimer.c
Signature · 1 line
2.20.1
PATCH 1/2 target/arm: detect 64 bit overflow caused by high cval + voff
If we don't detect this we will be stuck in a busy loop as we schedule a timer for before now which will continually trigger gt_recalc_timer even though we haven't reached the state required to trigger the IRQ.
--- target/arm/helper.c | 3 +++ 1 file changed, 3 insertions(+)
target/arm/helper.c +3 -0
@@ -2481,6 +2481,9 @@ static void gt_recalc_timer(ARMCPU *cpu, int timeridx) }else {/* Next transition is when we hit cval */ nexttick = gt->cval + offset; +if (nexttick < gt->cval) { + nexttick =UINT64_MAX ; + } }/* Note that the desired next expiry time might be beyond the * signed-64-bit range of a QEMUTimer -- in this case we just
Signature · 1 line
2.20.1
Re: [PATCH v1 1/2] target/arm: detect 64 bit overflow caused by high cval + voff
Quoted 17 lines · Alex Bennée
If we don't detect this we will be stuck in a busy loop as we schedule a timer for before now which will continually trigger
gt_recalc_timereven though we haven't reached the state required to trigger the IRQ.Signed-off-by Alex Bennée <[email protected]>--- target/arm/helper.c | 3 +++ 1 file changed, 3 insertions(+)target/arm/helper.c +0
-0
target/arm/helper.c static void gt_recalc_timer(ARMCPU *cpu, int timeridx) · line 2487
Quoted 7 lines
target/arm/helper.c +3
-0@@ -2481,6 +2481,9 @@ static void gt_recalc_timer(ARMCPU *cpu, int timeridx) }else {/* Next transition is when we hit cval */ nexttick = gt->cval + offset; +if (nexttick < gt->cval) { + nexttick =UINT64_MAX ; + } }
There's something odd going on with this code. Adding a bit of context:
uint64_t offset = timeridx == GTIMER_VIRT ?
cpu->env.cp15.cntvoff_el2 : 0;
uint64_t count = gt_get_countervalue(&cpu->env);
/* Note that this must be unsigned 64 bit arithmetic: */
int istatus = count - offset >= gt->cval;
[...]
if (istatus) {
/* Next transition is when count rolls back over to zero */
nexttick = UINT64_MAX;
} else {
/* Next transition is when we hit cval */
nexttick = gt->cval + offset;
}I think this patch is correct, in that the 'nexttick' values are all absolute and this cval/offset combination implies that the next timer interrupt is going to be in a future so distant we can't even fit the duration in a uint64_t.
But the other half of the 'if' also looks wrong: that's for the case of "timer has fired, how long until the wraparound causes the interrupt line to go low again?". UINT64_MAX is right for the EL1 case where offset is 0, but the offset might actually be set such that the wrap around happens fairly soon. We want to calculate the tick when (count - offset) hits 0, saturated to UINT64_MAX. It's getting late here and I couldn't figure out what that expression should be with 15 minutes of fiddling around with pen and paper diagrams. I'll have another go tomorrow if nobody else gets there first...
thanks
-- PMM
Re: [PATCH v1 1/2] target/arm: detect 64 bit overflow caused by high cval + voff
Quoted 21 lines · Peter Maydell
There's something odd going on with this code. Adding a bit of context:
uint64_t offset = timeridx == GTIMER_VIRT ? cpu->env.cp15.cntvoff_el2 : 0; uint64_t count = gt_get_countervalue(&cpu->env); /* Note that this must be unsigned 64 bit arithmetic: */ int istatus = count - offset >= gt->cval; [...] if (istatus) { /* Next transition is when count rolls back over to zero */ nexttick = UINT64_MAX; } else { /* Next transition is when we hit cval */ nexttick = gt->cval + offset; }I think this patch is correct, in that the 'nexttick' values are all absolute and this cval/offset combination implies that the next timer interrupt is going to be in a future so distant we can't even fit the duration in a
uint64_t.
Quoted 11 lines
But the other half of the 'if' also looks wrong: that's for the case of "timer has fired, how long until the wraparound causes the interrupt line to go low again?".
UINT64_MAXis right for the EL1 case where offset is 0, but the offset might actually be set such that the wrap around happens fairly soon. We want to calculate the tick when (count - offset) hits 0, saturated toUINT64_MAX. It's getting late here and I couldn't figure out what that expression should be with 15 minutes of fiddling around with pen and paper diagrams. I'll have another go tomorrow if nobody else gets there first...
With a fresher brain:
For the if (istatus) branch we want the absolute tick when (count - offset) wraps round to 0, saturated to UINT64_MAX. I think this is:
if (offset <= count) {
nexttick = UINT64_MAX;
} else {
nexttick = offset;
}Should we consider this a separate bugfix to go in its own patch?
thanks
-- PMM
PATCH 2/2 tests/tcg: add a vtimer test for aarch64
In reply to Alex Bennée--- tests/tcg/aarch64/system/vtimer.c | 80 +++++++++++++++++++++++ tests/tcg/aarch64/Makefile.softmmu-target | 4 ++ 2 files changed, 84 insertions(+) create mode 100644 tests/tcg/aarch64/system/vtimer.c
tests/tcg/aarch64/system/vtimer.c new file +80 -0
@@ -0,0 +1,80 @@ +/* +* Simple Virtual Timer Tests +* +* Note: kvm-unit-tests has a much more comprehensive exercising of +* the timer sub-system. However this test case can tweak _EL2 values +* to trigger bugs which can't be done with that. +* +* Copyright (c) 2020 Linaro Ltd +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#include <inttypes.h> +#include <minilib.h> + +/* grabbed from Linux */ +#define __stringify_1(x...) #x +#define __stringify(x...) __stringify_1(x) + +#define read_sysreg (r) ({ \ + uint64_t __val; \ + asm volatile("mrs %0, " __stringify(r) : "=r" (__val)); \ + __val; \ +}) + +#define write_sysreg (r, v) do { \ + uint64_t __val = (uint64_t)(v); \ + asm volatile("msr " __stringify(r) ", %x0" \ + : : "rZ" (__val)); \ +} while (0) + +/* Physical Counter */ +static uint64_t last_pct; +/* Timer Values */ +static uint32_t last_phys_tval; +static uint32_t last_virt_tval; + +static void dump_status (void ) +{ +uint64_t pct =read_sysreg (cntpct_el0); +uint32_t phys_tval =read_sysreg (cntp_tval_el0); +uint32_t virt_tval =read_sysreg (cntv_tval_el0); + +ml_printf ("timer values:\n" ); +/* the physical timer monotonically increments */ +ml_printf ("cntpct_el0=%ld (+%ld)\n" , pct, pct - last_pct); +/* the various tvals decrement based on cval */ +ml_printf ("cntp_tval_el0=%ld (-%ld)\n" , phys_tval, + last_phys_tval - phys_tval); +ml_printf ("cntv_tval_el0=%ld (-%ld)\n" , virt_tval, + last_virt_tval - virt_tval); + + last_pct = pct; + last_phys_tval = phys_tval; + last_virt_tval = virt_tval; +} + +int main (void ) +{ +int i; + +ml_printf ("VTimer Tests\n" ); + +dump_status (); + +ml_printf ("Tweaking voff_el2 and cval\n" ); +write_sysreg (cntvoff_el2,1 ); +write_sysreg (cntv_cval_el0,-1 ); + +dump_status (); + +ml_printf ("Enabling timer IRQs\n" ); +write_sysreg (cntv_ctl_el0,1 ); +/* for bug 1859021 we hang here */ + +dump_status (); + +ml_printf ("End of Vtimer test\n" ); +return 0 ; +}
tests/tcg/aarch64/Makefile.softmmu-target +4 -0
@@ -62,3 +62,7 @@ run-memory-replay: memory-replay run-memory-record "$< on $(TARGET_NAME)") EXTRA_TESTS+=memory-record memory-replay + +# vtimer test +QEMU_EL2_MACHINE=-machine virt,virtualization=on,gic-version=2 -cpu cortex-a57 -smp 4 +run-vtimer: QEMU_OPTS=$(QEMU_EL2_MACHINE) $(QEMU_SEMIHOST) -kernel
Signature · 1 line
2.20.1
Re: [PATCH v1 2/2] tests/tcg: add a vtimer test for aarch64
Quoted 24 lines · Alex Bennée
Signed-off-by Alex Bennée <[email protected]>--- tests/tcg/aarch64/system/vtimer.c | 80 +++++++++++++++++++++++ tests/tcg/aarch64/Makefile.softmmu-target | 4 ++ 2 files changed, 84 insertions(+) create mode 100644 tests/tcg/aarch64/system/vtimer.ctests/tcg/aarch64/system/vtimer.c new file +8
-0@@ -0,0 +1,80 @@ +/* +* Simple Virtual Timer Tests +* +* Note: kvm-unit-tests has a much more comprehensive exercising of +* the timer sub-system. However this test case can tweak _EL2 values +* to trigger bugs which can't be done with that. +* +* Copyright (c) 2020 Linaro Ltd
tests/tcg/aarch64/system/vtimer.c line 18
Quoted 10 lines
tests/tcg/aarch64/system/vtimer.c new file +10
-0+* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#include <inttypes.h> +#include <minilib.h> + +/* grabbed from Linux */ +#define __stringify_1(x...) #x +#define __stringify(x...) __stringify_1(x)
Code 'grabbed from Linux' is unlikely to be GPL-2-or-later...
QEMU already has a stringify() macro in compiler.h.
thanks
-- PMM
Re: [PATCH v1 2/2] tests/tcg: add a vtimer test for aarch64
Quoted 38 lines · Peter Maydell
Quoted 24 lines · Alex Bennée
Signed-off-by Alex Bennée <[email protected]>--- tests/tcg/aarch64/system/vtimer.c | 80 +++++++++++++++++++++++ tests/tcg/aarch64/Makefile.softmmu-target | 4 ++ 2 files changed, 84 insertions(+) create mode 100644 tests/tcg/aarch64/system/vtimer.ctests/tcg/aarch64/system/vtimer.c new file +8
-0@@ -0,0 +1,80 @@ +/* +* Simple Virtual Timer Tests +* +* Note: kvm-unit-tests has a much more comprehensive exercising of +* the timer sub-system. However this test case can tweak _EL2 values +* to trigger bugs which can't be done with that. +* +* Copyright (c) 2020 Linaro Ltd tests/tcg/aarch64/system/vtimer.c line 18
Quoted 10 lines
tests/tcg/aarch64/system/vtimer.c new file +10
-0+* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#include <inttypes.h> +#include <minilib.h> + +/* grabbed from Linux */ +#define __stringify_1(x...) #x +#define __stringify(x...) __stringify_1(x)Code 'grabbed from Linux' is unlikely to be GPL-2-or-later...
Quoted 1 line
QEMU already has a
stringify()macro incompiler.h.
Hmm I don't think I can include compiler.h in a system mode test. I can certainly copy and paste our local version though ;-)
Quoted 3 lines
thanks
-- PMM
Signature · 1 line
Alex Bennée