Skip to contentSkip to conversation

target-arm queue

GIT PULL Applied Peter Maydell 2 participants 39 messages Sep 29 – Sep 30

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

Hi; here's a target-arm queue: FEAT_UINJ is the biggest thing here, otherwise largely bugfixes of various kinds.

thanks
-- PMM

The following changes since commit 81ce3a87737aa50716c42db8886082d12783e0a1:

Merge tag 'hw-misc-20260927' of https://github.com/philmd/qemu into staging (2026-09-27 02:02:29 -0700)

are available in the Git repository at:

https://gitlab.com/pm215/qemu.git tags/pull-target-arm-20260929

for you to fetch changes up to 998b6db69e3ec58fd52ddc7a06ece6cf286f3c77:

target/arm: Fix WFxT timeouts when the offset puts the count ahead (2026-09-29 10:58:40 +0100)

----------------------------------------------------------------
target-arm queue:
* whpx: arm64: check for physical address width after WHPX availability
* hw/char/stm32l4x5_usart.c: Support 16-bit writes
* hw/arm: Add GPIO to Allwinner A10 model
* hw/arm/tegra241-cmdqv: Keep VINTF page0 region alive
* hw/char/exynos4210_uart: fix fifo overflow
* arm: handle CCSIDR_EL1 as a demuxed register
* target/arm: model Cortex-M0+ privilege extension
* target/arm: Fix migration of Aarch32 Secure banked registers
* target/arm: Fix PMSAv8 shareability decode for R-profile MPU regions
* target/arm: Enable FEAT_UINJ for -cpu max-v9
* target/arm: Fix timer tick and WFxT timeouts when the offset puts the count ahead

----------------------------------------------------------------
Cornelia Huck (3):
arm: handle demuxed ID registers
arm: handle CCSIDR_EL1 as a demuxed register arm/kvm: get demuxed ID registers from kvm

Fuad Tabba (2):
target/arm: Fix next timer tick when the offset puts the count ahead target/arm: Fix WFxT timeouts when the offset puts the count ahead

Gilles Grimaud (1):
target/arm: model Cortex-M0+ privilege extension

Jacob Whitaker Abrams (1):
hw/char/stm32l4x5_usart.c: Support 16-bit writes

Jean-Francois Bortolotti (1):
target/arm: Fix PMSAv8 shareability decode for R-profile MPU regions

Matthew R. Ochs (1):
hw/arm/tegra241-cmdqv: Keep VINTF page0 region alive

Mohamed Mediouni (1):
whpx: arm64: check for physical address width after WHPX availability

Peter Maydell (8):
target/arm: Keep the S/NS bit in migration data
target/arm: Don't assume secure-banked registers are migrated by 64-bit regs target/arm: Drop uses of reset_ignore for banked AArch32 registers target/arm: Don't use arm_cp_reset_ignore when fieldoffset is 0 target/arm: Move ESR_EL1 definition to vmsa_pmsa_cp_reginfo target/arm: Don't set ARM_FEATURE_VAPA for v7 PMSA
target/arm: Don't register 64-bit TTBR0, TTBR1 for PMSA target/arm: Add a debug check that all sysregs are migrated

Richard Henderson (14):
target/arm: Parenthesize tbflags arguments in TBFLAG macros target/arm: Reorg rebuild_hflags_common* target/arm: Split out rebuild_hflags_common_aprofile target/arm: Sort BE_DATA toward the top of rebuild_hflags_a64 target/arm: Move ID_AA64PFR2 feature tests together target/arm: Add isar_feature_aa64_uinj
target/arm: Drop U from most PSTATE bits target/arm: Clear PSTATE.UINJ when taking exceptions target/arm: Widen cpsr_{read, write}_for_spsr_elx to uint64_t target/arm: Read and write PSTATE.UINJ
target/arm: Add PSTATE.UINJ to tbflags
target/arm: Split out check_il
target/arm: Honor PSTATE.UINJ during translation target/arm: Enable FEAT_UINJ for -cpu max-v9

Strahinja Jankovic (3):
hw/gpio: Allwinner A10 GPIO emulation
hw/arm: Add GPIO to Allwinner A10 model
tests/qtest: Allwinner GPIO tests

jprx (2):
hw/char/exynos4210_uart: fix fifo overflow tests/qtest: add test for exynos4210 uart fifo overflow bug

 MAINTAINERS                                |   3 +
 docs/system/arm/cubieboard.rst             |   1 +
 docs/system/arm/emulation.rst              |   1 +
 hw/arm/Kconfig                             |   1 +
 hw/arm/allwinner-a10.c                     |   8 +
 hw/arm/tegra241-cmdqv.c                    |  54 +--
 hw/arm/tegra241-cmdqv.h                    |   2 +-
 hw/arm/virt.c                              |  19 +-
 hw/char/exynos4210_uart.c                  | 111 ++----
 hw/char/stm32l4x5_usart.c                  |   7 +-
 hw/gpio/Kconfig                            |   3 +
 hw/gpio/allwinner-gpio.c                   | 441 ++++++++++++++++++++++++
 hw/gpio/meson.build                        |   1 +
 hw/gpio/trace-events                       |   6 +
 hw/intc/armv7m_nvic.c                      |   2 +-
 include/hw/arm/allwinner-a10.h             |   2 +
 include/hw/gpio/allwinner-gpio-regs.h      | 227 ++++++++++++
 include/hw/gpio/allwinner-gpio.h           |  41 +++
 target/arm/cpregs.h                        |  21 +-
 target/arm/cpu-features.h                  |  26 +-
 target/arm/cpu-max.c                       |   6 +-
 target/arm/cpu-sysregs.h                   |   9 +
 target/arm/cpu-sysregs.h.inc               |   1 +
 target/arm/cpu.c                           |  10 +-
 target/arm/cpu.h                           |  97 ++++--
 target/arm/cpu64.c                         |  14 +-
 target/arm/helper.c                        | 237 ++++++++++---
 target/arm/internals.h                     |   7 +-
 target/arm/kvm.c                           |  43 +++
 target/arm/machine.c                       |  80 +++++
 target/arm/ptw.c                           |   2 +-
 target/arm/tcg/cpu32.c                     |  26 +-
 target/arm/tcg/cpu64.c                     |  69 ++--
 target/arm/tcg/helper-a64.c                |   2 +
 target/arm/tcg/hflags.c                    | 118 +++----
 target/arm/tcg/m_helper.c                  |   2 +-
 target/arm/tcg/op_helper.c                 |   6 +-
 target/arm/tcg/translate-a64.c             |   8 +-
 target/arm/tcg/translate.c                 |  33 +-
 target/arm/tcg/translate.h                 |   3 +
 target/arm/whpx/whpx-all.c                 |  16 +-
 tests/qtest/allwinner-gpio-test.c          | 535 +++++++++++++++++++++++++++++
 tests/qtest/exynos4210_uart_fifo-test.c    |  86 +++++
 tests/qtest/meson.build                    |   7 +-
 tests/qtest/stm32l4x5_usart-test.c         |  22 +-
 tests/tcg/aarch64/system/meson.build       |   7 +
 tests/tcg/aarch64/system/vtimer.c          |  28 ++
 tests/tcg/aarch64/system/wfxt.c            | 118 +++++++
 tests/tcg/arm/system/meson.build           |   6 +
 tests/tcg/arm/system/test-armv6m-control.S |  66 ++++
 50 files changed, 2278 insertions(+), 363 deletions(-)
 create mode 100644 hw/gpio/allwinner-gpio.c
 create mode 100644 include/hw/gpio/allwinner-gpio-regs.h
 create mode 100644 include/hw/gpio/allwinner-gpio.h
 create mode 100644 tests/qtest/allwinner-gpio-test.c
 create mode 100644 tests/qtest/exynos4210_uart_fifo-test.c
 create mode 100644 tests/tcg/aarch64/system/wfxt.c
 create mode 100644 tests/tcg/arm/system/test-armv6m-control.S
#1 lore raw

[PULL 05/37] tests/qtest: Allwinner GPIO tests

From: Strahinja Jankovic <[email protected]>

Add Allwinner GPIO set of qtests to validate functionality. Tests cover input and output port setting, as well as interrupt functionality.

Message-id: [email protected]

---
 MAINTAINERS                       |   1 +
 tests/qtest/allwinner-gpio-test.c | 535 ++++++++++++++++++++++++++++++
 tests/qtest/meson.build           |   3 +-
 3 files changed, 538 insertions(+), 1 deletion(-)
 create mode 100644 tests/qtest/allwinner-gpio-test.c
MAINTAINERS +1 -0
index 628d2ef22b3..1312bbd1bd8 100644
@@ -749,6 +749,7 @@ F: hw/misc/axp2xx.c
 F: hw/gpio/allwinner-gpio.c
 F: include/hw/gpio/allwinner-gpio*
 F: tests/functional/arm/test_cubieboard.py
+F: tests/qtest/allwinner-gpio-test.c
 
 Allwinner-h3
 M: Niek Linnenbank <[email protected]>
tests/qtest/allwinner-gpio-test.c new file +535 -0

535 lines not on this page · raw message

tests/qtest/meson.build +2 -1
index 33f58b0e95c..a35ae79b03d 100644
@@ -268,7 +268,8 @@ qtests_arm = \
   (config_all_devices.has_key('CONFIG_STM32L4X5_SOC') and
    config_all_devices.has_key('CONFIG_DM163')? ['dm163-test'] : []) + \
   ['arm-cpu-features',
-   'boot-serial-test']
+   'boot-serial-test'] + \
+  (config_all_devices.has_key('CONFIG_ALLWINNER_A10')? ['allwinner-gpio-test'] : [])
 
 # TODO: once aarch64 TCG is fixed on ARM 32 bit host, make bios-tables-test unconditional
 qtests_aarch64 = \
Signature · 1 line
2.43.0
#2 lore raw

[PULL 06/37] hw/arm/tegra241-cmdqv: Keep VINTF page0 region alive

In reply to Peter Maydell

From: "Matthew R. Ochs" <[email protected]>

With CMDQV enabled, resetting a guest after it enables VINTF invokes the VINTF page0 unmap path. The resulting crash is intermittent and has been observed on the RCU reclaim thread as:

reboot: Restarting system
double free or corruption (!prev)
...
#5 address_space_dispatch_free
#6 flatview_destroy
#7 call_rcu_thread

FlatViews retain raw MemoryRegion pointers and release their references asynchronously through RCU. The VINTF page0 unmap path removes the subregion and immediately unparents and frees it. An old FlatView can then access the freed region during teardown, resulting in a use-after-free and heap corruption.

Embed the VINTF page0 MemoryRegion in Tegra241CMDQV and tie its lifetime to the VINTF page0 mmap. Initialize and add the region disabled after successful vIOMMU allocation, and remove and unparent it before releasing the mmap during allocation unwind. Guest VINTF enable and disable then only toggle the region with memory_region_set_enabled(). New FlatViews omit the disabled region, while old views continue to reference valid storage.

Keeping the region initialized across VINTF disable and reset is safe because the vIOMMU association and its VINTF page0 mmap remain stable once the guest CMDQ has been initialized. QEMU blocks hot-unplug of the device that established the association, so later hot-adds reuse it instead of associating the initialized guest CMDQ with a different host SMMUv3. The CMDQV free_viommu path is therefore only invoked while unwinding initial allocation, when the region is still disabled and has never entered a FlatView.

Fixes 5965b81ce283 ("hw/arm/tegra241-cmdqv: Use mmap'd host VINTF page0 for virtual VINTF page0")

Message-id: [email protected]

---
 hw/arm/tegra241-cmdqv.c | 54 +++++++++++++++++++++--------------------
 hw/arm/tegra241-cmdqv.h |  2 +-
 2 files changed, 29 insertions(+), 27 deletions(-)
hw/arm/tegra241-cmdqv.c +28 -26
index 273633e6293..57041408a7f 100644
@@ -131,36 +131,12 @@ static void tegra241_cmdqv_reset_vcmdq_cache(Tegra241CMDQV *cmdqv, int index)
 
 static void tegra241_cmdqv_guest_unmap_vintf_page0(Tegra241CMDQV *cmdqv)
 {
-    if (!cmdqv->mr_vintf_page0) {
-        return;
-    }
-
-    memory_region_del_subregion(&cmdqv->mmio_cmdqv, cmdqv->mr_vintf_page0);
-    object_unparent(OBJECT(cmdqv->mr_vintf_page0));
-    g_free(cmdqv->mr_vintf_page0);
-    cmdqv->mr_vintf_page0 = NULL;
+    memory_region_set_enabled(&cmdqv->mr_vintf_page0, false);
 }
 
 static void tegra241_cmdqv_guest_map_vintf_page0(Tegra241CMDQV *cmdqv)
 {
-    char *name;
-
-    if (cmdqv->mr_vintf_page0) {
-        return;
-    }
-
-    name = g_strdup_printf("%s vintf-page0",
-                           memory_region_name(&cmdqv->mmio_cmdqv));
-    cmdqv->mr_vintf_page0 = g_malloc0(sizeof(*cmdqv->mr_vintf_page0));
-    memory_region_init_ram_device_ptr(cmdqv->mr_vintf_page0,
-                                      memory_region_owner(&cmdqv->mmio_cmdqv),
-                                      name, VINTF_PAGE_SIZE,
-                                      cmdqv->vintf_page0);
-    memory_region_set_skip_iommu_map(cmdqv->mr_vintf_page0, true);
-    memory_region_add_subregion_overlap(&cmdqv->mmio_cmdqv,
-                                        CMDQV_VINTF_PAGE0_BASE,
-                                        cmdqv->mr_vintf_page0, 1);
-    g_free(name);
+    memory_region_set_enabled(&cmdqv->mr_vintf_page0, true);
 }
 
 static void tegra241_cmdqv_free_vcmdq(Tegra241CMDQV *cmdqv, int index)
@@ -899,6 +875,9 @@ static void tegra241_cmdqv_free_viommu(SMMUv3State *s)
         cmdqv->veventq = NULL;
     }
     if (cmdqv->vintf_page0) {
+        memory_region_del_subregion(&cmdqv->mmio_cmdqv,
+                                    &cmdqv->mr_vintf_page0);
+        object_unparent(OBJECT(&cmdqv->mr_vintf_page0));
         munmap(cmdqv->vintf_page0, VINTF_PAGE_SIZE);
         cmdqv->vintf_page0 = NULL;
     }
@@ -910,6 +889,7 @@ tegra241_cmdqv_alloc_viommu(SMMUv3State *s, HostIOMMUDeviceIOMMUFD *idev,
                             uint32_t *out_viommu_id, Error **errp)
 {
     Tegra241CMDQV *cmdqv = s->s_accel->cmdqv;
+    char *name;
     uint32_t viommu_id, veventq_id, veventq_fd;
     IOMMUFDVeventq *veventq;
     int flags;
@@ -955,6 +935,28 @@ tegra241_cmdqv_alloc_viommu(SMMUv3State *s, HostIOMMUDeviceIOMMUFD *idev,
 
     /* Set up event handler for veventq fd */
     qemu_set_fd_handler(veventq_fd, tegra241_cmdqv_event_read, NULL, cmdqv);
+
+    /*
+     * Tie the MemoryRegion lifetime to the VINTF page0 mmap. Initialize it
+     * disabled. If initial vIOMMU setup later unwinds, the region has never
+     * entered a FlatView and can be removed and unparented before releasing
+     * the mmap. After successful setup, keep it parented across guest disable
+     * and reset, and only toggle its enabled state so old FlatViews continue
+     * to reference valid storage.
+     */
+    name = g_strdup_printf("%s vintf-page0",
+                           memory_region_name(&cmdqv->mmio_cmdqv));
+    memory_region_init_ram_device_ptr(&cmdqv->mr_vintf_page0,
+                                      memory_region_owner(&cmdqv->mmio_cmdqv),
+                                      name, VINTF_PAGE_SIZE,
+                                      cmdqv->vintf_page0);
+    memory_region_set_skip_iommu_map(&cmdqv->mr_vintf_page0, true);
+    memory_region_set_enabled(&cmdqv->mr_vintf_page0, false);
+    memory_region_add_subregion_overlap(&cmdqv->mmio_cmdqv,
+                                        CMDQV_VINTF_PAGE0_BASE,
+                                        &cmdqv->mr_vintf_page0, 1);
+    g_free(name);
+
     *out_viommu_id = viommu_id;
     return true;
 
hw/arm/tegra241-cmdqv.h +1 -1
index de4c1e53358..bba985099e3 100644
@@ -49,7 +49,7 @@ typedef struct Tegra241CMDQV {
     IOMMUFDVeventq *veventq;
     IOMMUFDHWqueue *vcmdq[TEGRA241_CMDQV_MAX_CMDQ];
     void *vintf_page0;
-    MemoryRegion *mr_vintf_page0;
+    MemoryRegion mr_vintf_page0;
 
     /* CMDQ-V Config page register cache */
     uint32_t config;
Signature · 1 line
2.43.0
#3 lore raw

[PULL 04/37] hw/arm: Add GPIO to Allwinner A10 model

In reply to Peter Maydell

From: Strahinja Jankovic <[email protected]>

The Allwinner A10 GPIO is added to the Allwinner A10 description, so it is available when Cubieboard is emulated.

Update the documentation for Cubieboard to indicate GPIO availability.

Message-id: [email protected]

---
 docs/system/arm/cubieboard.rst | 1 +
 hw/arm/Kconfig                 | 1 +
 hw/arm/allwinner-a10.c         | 8 ++++++++
 include/hw/arm/allwinner-a10.h | 2 ++
 4 files changed, 12 insertions(+)
docs/system/arm/cubieboard.rst +1 -0
index 90d24c73a13..ee9f489ead2 100644
@@ -12,6 +12,7 @@ Emulated devices:
 - RTC
 - EMAC
 - SDHCI
+- GPIO
 - USB controller
 - SATA controller
 - TWI (I2C) controller
hw/arm/Kconfig +1 -0
index eae06369b02..4445f16f451 100644
@@ -295,6 +295,7 @@ config ALLWINNER_A10
     select ALLWINNER_EMAC
     select ALLWINNER_I2C
     select ALLWINNER_A10_SPI
+    select ALLWINNER_GPIO
     select AXP2XX_PMU
     select SERIAL_MM
     select UNIMP
hw/arm/allwinner-a10.c +8 -0
index 66f30757dd5..66fe484ae0e 100644
@@ -41,6 +41,7 @@
 #define AW_A10_EHCI_BASE        0x01c14000
 #define AW_A10_OHCI_BASE        0x01c14400
 #define AW_A10_SATA_BASE        0x01c18000
+#define AW_A10_GPIO_BASE        0x01c20800
 #define AW_A10_WDT_BASE         0x01c20c90
 #define AW_A10_RTC_BASE         0x01c20d00
 #define AW_A10_I2C0_BASE        0x01c2ac00
@@ -83,6 +84,8 @@ static void aw_a10_init(Object *obj)
 
     object_initialize_child(obj, "spi0", &s->spi0, TYPE_AW_A10_SPI);
 
+    object_initialize_child(obj, "gpio", &s->gpio, TYPE_AW_GPIO);
+
     for (size_t i = 0; i < AW_A10_NUM_USB; i++) {
         object_initialize_child(obj, "ehci[*]", &s->ehci[i],
                                 TYPE_PLATFORM_EHCI);
@@ -203,6 +206,11 @@ static void aw_a10_realize(DeviceState *dev, Error **errp)
     sysbus_mmio_map(SYS_BUS_DEVICE(&s->spi0), 0, AW_A10_SPI0_BASE);
     sysbus_connect_irq(SYS_BUS_DEVICE(&s->spi0), 0, qdev_get_gpio_in(dev, 10));
 
+    /* GPIO */
+    sysbus_realize(SYS_BUS_DEVICE(&s->gpio), &error_fatal);
+    sysbus_mmio_map(SYS_BUS_DEVICE(&s->gpio), 0, AW_A10_GPIO_BASE);
+    sysbus_connect_irq(SYS_BUS_DEVICE(&s->gpio), 0, qdev_get_gpio_in(dev, 28));
+
     /* WDT */
     sysbus_realize(SYS_BUS_DEVICE(&s->wdt), &error_fatal);
     sysbus_mmio_map_overlap(SYS_BUS_DEVICE(&s->wdt), 0, AW_A10_WDT_BASE, 1);
include/hw/arm/allwinner-a10.h +2 -0
index 445ba1be212..8b7736c7bd1 100644
@@ -13,6 +13,7 @@
 #include "hw/misc/allwinner-a10-dramc.h"
 #include "hw/i2c/allwinner-i2c.h"
 #include "hw/ssi/allwinner-a10-spi.h"
+#include "hw/gpio/allwinner-gpio.h"
 #include "hw/watchdog/allwinner-wdt.h"
 #include "system/block-backend.h"
 
@@ -42,6 +43,7 @@ struct AwA10State {
     AwSdHostState mmc0;
     AWI2CState i2c0;
     AWA10SPIState spi0;
+    AWGPIOState gpio;
     AwRtcState rtc;
     AwWdtState wdt;
     MemoryRegion sram_a;
Signature · 1 line
2.43.0
#4 lore raw

[PULL 36/37] target/arm: Fix next timer tick when the offset puts the count ahead

In reply to Peter Maydell

From: Fuad Tabba <[email protected]>

gt_recalc_timer() arms the timer for the ISTATUS 0->1 transition at count == cval + offset, treating an overflow of that sum as "beyond the counter's wrap" and arming at INT64_MAX. That reading is valid only when offset <= count. When offset > count (a CNTVOFF_EL2 or CNTPOFF_EL2 that puts the timer's counter ahead of the physical count), the sum overflows for every cval still in the future and the wrapped value is the correct next tick, which is what the code used before commit 8d37a1425b99. The timer then never fires on its own.

A KVM guest whose counter is set ahead of the host's, as the arch_timer_edge_cases selftest does via KVM_REG_ARM_TIMER_CNT, gets its timer interrupt only when the vCPU is next loaded and KVM rewrites the timer registers, so a guest polling for the interrupt hangs.
Reproduced with VHE, nVHE and pKVM hosts on QEMU 11.0.0. The test passes on hardware.

Test count + (cval - (count - offset)) for overflow instead, so only a physical count past 2^64 is "never", and add a vtimer test case with an offset that puts the count ahead.

Fixes 8d37a1425b99 ("target/arm: Handle overflow in calculation of next timer tick")

Message-id: [email protected]

---
 target/arm/helper.c               |  9 +++++----
 tests/tcg/aarch64/system/vtimer.c | 28 ++++++++++++++++++++++++++++
 2 files changed, 33 insertions(+), 4 deletions(-)
target/arm/helper.c +5 -4
index 9a90ab68071..ff0b766b73d 100644
@@ -1692,11 +1692,12 @@ static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
         } else {
             /*
              * Next transition is when (count - offset) == cval, i.e.
-             * when count == (cval + offset).
-             * If that would overflow, then again we set up the next interrupt
-             * for "as far in the future as possible" for the code below.
+             * cval - (count - offset) ticks from now. If count plus that
+             * overflows, set up "as far in the future as possible" below.
              */
-            if (uadd64_overflow(gt->cval, offset, &nexttick)) {
+            uint64_t remaining = gt->cval - (count - offset);
+
+            if (uadd64_overflow(count, remaining, &nexttick)) {
                 nexttick = UINT64_MAX;
             }
         }
tests/tcg/aarch64/system/vtimer.c +28 -0
index 7d725eced34..e1cfad36be1 100644
@@ -27,6 +27,7 @@
 
 int main(void)
 {
+    uint64_t freq, now;
     int i;
 
     ml_printf("VTimer Test\n");
@@ -44,5 +45,32 @@ int main(void)
         ml_printf("%d: cntv_cval_el0=%lx\n", i, read_sysreg(cntv_cval_el0));
     }
 
+    /*
+     * An offset that puts the virtual count ahead of the physical one,
+     * so cval + cntvoff wraps for every future cval. The timer must
+     * still fire. ISTATUS is set by the expiry, so poll it with a bound.
+     */
+    write_sysreg(cntv_ctl_el0, 0);
+    write_sysreg(cntvoff_el2, -(1ULL << 60));
+    asm volatile("isb");
+
+    freq = read_sysreg(cntfrq_el0);
+    now = read_sysreg(cntvct_el0);
+    write_sysreg(cntv_cval_el0, now + freq / 100);
+    write_sysreg(cntv_ctl_el0, 1);
+
+    ml_printf("cntvoff_el2=%lx\n", read_sysreg(cntvoff_el2));
+    ml_printf("cntvct_el0=%lx\n", now);
+    ml_printf("cntv_cval_el0=%lx\n", read_sysreg(cntv_cval_el0));
+
+    while (!(read_sysreg(cntv_ctl_el0) & 4)) {
+        if (read_sysreg(cntvct_el0) - now > freq) {
+            ml_printf("FAIL: ISTATUS not set within 1s: cntv_ctl_el0=%lx\n",
+                      read_sysreg(cntv_ctl_el0));
+            return 1;
+        }
+    }
+    ml_printf("ISTATUS set at cntvct_el0=%lx\n", read_sysreg(cntvct_el0));
+
     return 0;
 }
Signature · 1 line
2.43.0
#5 lore raw

[PULL 28/37] target/arm: Drop U from most PSTATE bits

In reply to Peter Maydell

From: Richard Henderson <[email protected]>

PSTATE is already 64-bit, but many bits are defined such that ~PSTATE_FOO accidentally clears the high bits.

Except for PSTATE_N, we can just drop the qualifier.

Message-id: [email protected]

---
 target/arm/cpu.h | 40 ++++++++++++++++++++--------------------
 1 file changed, 20 insertions(+), 20 deletions(-)
target/arm/cpu.h +20 -20
index 4785c9c57af..141cfdf6a88 100644
@@ -1582,26 +1582,26 @@ void pmu_init(ARMCPU *cpu);
  * Only these are valid when in AArch64 mode; in
  * AArch32 mode SPSRs are basically CPSR-format.
  */
-#define PSTATE_SP (1U)
-#define PSTATE_M (0xFU)
-#define PSTATE_nRW (1U << 4)
-#define PSTATE_F (1U << 6)
-#define PSTATE_I (1U << 7)
-#define PSTATE_A (1U << 8)
-#define PSTATE_D (1U << 9)
-#define PSTATE_BTYPE (3U << 10)
-#define PSTATE_SSBS (1U << 12)
-#define PSTATE_ALLINT (1U << 13)
-#define PSTATE_IL (1U << 20)
-#define PSTATE_SS (1U << 21)
-#define PSTATE_PAN (1U << 22)
-#define PSTATE_UAO (1U << 23)
-#define PSTATE_DIT (1U << 24)
-#define PSTATE_TCO (1U << 25)
-#define PSTATE_V (1U << 28)
-#define PSTATE_C (1U << 29)
-#define PSTATE_Z (1U << 30)
-#define PSTATE_N (1U << 31)
+#define PSTATE_SP (1)
+#define PSTATE_M (0xF)
+#define PSTATE_nRW (1 << 4)
+#define PSTATE_F (1 << 6)
+#define PSTATE_I (1 << 7)
+#define PSTATE_A (1 << 8)
+#define PSTATE_D (1 << 9)
+#define PSTATE_BTYPE (3 << 10)
+#define PSTATE_SSBS (1 << 12)
+#define PSTATE_ALLINT (1 << 13)
+#define PSTATE_IL (1 << 20)
+#define PSTATE_SS (1 << 21)
+#define PSTATE_PAN (1 << 22)
+#define PSTATE_UAO (1 << 23)
+#define PSTATE_DIT (1 << 24)
+#define PSTATE_TCO (1 << 25)
+#define PSTATE_V (1 << 28)
+#define PSTATE_C (1 << 29)
+#define PSTATE_Z (1 << 30)
+#define PSTATE_N (1ULL << 31)
 #define PSTATE_EXLOCK (1ULL << 34)
 #define PSTATE_NZCV (PSTATE_N | PSTATE_Z | PSTATE_C | PSTATE_V)
 #define PSTATE_DAIF (PSTATE_D | PSTATE_A | PSTATE_I | PSTATE_F)
Signature · 1 line
2.43.0
#6 lore raw

[PULL 01/37] whpx: arm64: check for physical address width after WHPX availability

In reply to Peter Maydell

From: Mohamed Mediouni <[email protected]>

In the case where WHPX isn't supported on the platform, makes the intended error appear instead of failing at getting the IPA width.

Message-id: [email protected]

---
 target/arm/whpx/whpx-all.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
target/arm/whpx/whpx-all.c +8 -8
index d106400686f..46f0b9524e0 100644
@@ -879,14 +879,6 @@ int whpx_accel_init(AccelState *as, MachineState *ms)
         goto error;
     }
 
-    if (mc->get_physical_address_range) {
-        pa_range = mc->get_physical_address_range(ms,
-            whpx_arm_get_ipa_bit_size(), whpx_arm_get_ipa_bit_size());
-        if (pa_range < 0) {
-            return -EINVAL;
-        }
-    }
-
     whpx->mem_quota = ms->ram_size;
 
     hr = whp_dispatch.WHvGetCapability(
@@ -914,6 +906,14 @@ int whpx_accel_init(AccelState *as, MachineState *ms)
         goto error;
     }
 
+    if (mc->get_physical_address_range) {
+        pa_range = mc->get_physical_address_range(ms,
+            whpx_arm_get_ipa_bit_size(), whpx_arm_get_ipa_bit_size());
+        if (pa_range < 0) {
+            return -EINVAL;
+        }
+    }
+
     hr = whp_dispatch.WHvCreatePartition(&whpx->partition);
     if (FAILED(hr)) {
         error_report("WHPX: Failed to create partition, hr=%08lx", hr);
Signature · 1 line
2.43.0
#7 lore raw

[PULL 14/37] target/arm: Don't assume secure-banked registers are migrated by 64-bit regs

In reply to Peter Maydell

In add_cpreg_to_hashtable() we have some code that automatically marks the secure version of an AArch32 banked cpreg as ARM_CP_ALIAS under two conditions:

1) If the register has both 32-bit and 64-bit instances then we can count on the 64-bit instance taking care of the non-secure bank.

2) If ARMv8 is enabled then we can count on a 64-bit version taking care of the secure bank. This requires that separate 32 and 64-bit definitions are provided.

Item 1 is correct: if the ARMCPRegInfo has ARM_CP_STATE_BOTH then we will always add it as an AA32 entry (which then splits into an S entry with bank_fieldoffsets[0] and an NS entry with bank_fieldoffsets[1]) and as an AA64 entry (which uses bank_fieldoffsets[1]), so the AA64 entry will always be present to cover the NS banked AA32 entry.

Item 2 is not correct: the idea was that because typically AArch32 Secure banked registers are (non-architecturally) mapped to EL3 AArch64 registers, the AArch64 EL3 register would handle things (and that we would be adding the AArch64 register to the hashtable even if the CPU didn't have AArch64).

However, this fails in several cases:
(a) registers like CSSELR, where there is only a CSSELR_EL1, and no _EL3 version. CSSELR_S's underlying state isn't migrated
(b) registers like TPIDRURW, where the secure banked register isn't mapped to any AArch64 register, so the state isn't migrated
(c) IFAR_S and DFAR_S are odd special cases, because they map to AArch32 HIFAR and HDFAR, which in turn map to AArch64 FAR_EL2. If the CPU doesn't have EL2, we don't register FAR_EL2 and the IFAR_S and DFAR_S registers don't get migrated.

More generally, the architecture dropped the concept of documenting secure AArch32 registers as being "non-architecturally mapped" to EL3 AArch64 registers and relegated it to an implementation detail, which is why TPIDRURW and friends got mishandled.

It also conflates "has 64-bit registers" with "is ARMv8", which is wrong for the qemu-system-arm "max" type and for any AArch64 CPU started with "aarch64=off".

Instead of marking the S-banked register as an alias if ARM_FEATURE_V8, don't put it into the hashtable at all if ARM_FEATURE_AARCH64 is present.

There are not all that many banked registers, and they're easy to find by searching for regdefs that set bank_fieldoffsets:
DACR, IFAR, DFAR, IFSR, DFSR, CSSELR, MAIR0, MAIR1, TPIDRURW, TPIDRURO, TPIDRPRW, TTBR0, TTBR1, TTBCR, TTBCR2, PAR, VBAR, SCTLR

None of these do complicated things on reset, so having both an AArch64 view and an AArch32 view reset the same register state to the same value and migrate the same data twice is harmless. This is better than sometimes not migrating or resetting it at all.

This changes the set of cpregs we will try to migrate for CPUs with ARM_FEATURE_V8 but not ARM_FEATURE_AARCH64, and is a migration break there. However, the only such CPUs are the qemu-system-aarch32 "max" CPU and AArch64 CPUs with "aarch64=off".

In an ideal world we would also not bother putting the S cpreg into the hashtable if the CPU is AArch32 without EL3 (i.e. it doesn't implement Secure state at all), because nothing will ever look up the S version of the register (access_secure_reg() always returns false). But that would be a migration compat break for those CPUs.

Message-id: [email protected]

---
 target/arm/helper.c | 35 +++++++++++++++++++++++++----------
 1 file changed, 25 insertions(+), 10 deletions(-)
target/arm/helper.c +25 -10
index a608df12111..fe9838b3f6d 100644
@@ -7787,18 +7787,33 @@ static void add_cpreg_to_hashtable(ARMCPU *cpu, ARMCPRegInfo *r,
         if (state == ARM_CP_STATE_AA32) {
             if (isbanked) {
                 /*
-                 * If the register is banked then we don't need to migrate or
-                 * reset the 32-bit instance in certain cases:
+                 * If this is an AArch64 CPU then AArch32 cpregs are never
+                 * banked, and if we put an NS version into the hash table
+                 * it would never be accessed (compare the condition we test
+                 * in access_secure_reg()). So drop it rather than adding it.
                  *
-                 * 1) If the register has both 32-bit and 64-bit instances
-                 *    then we can count on the 64-bit instance taking care
-                 *    of the non-secure bank.
-                 * 2) If ARMv8 is enabled then we can count on a 64-bit
-                 *    version taking care of the secure bank.  This requires
-                 *    that separate 32 and 64-bit definitions are provided.
+                 * Ideally we would also ignore the S banked register here
+                 * for an AArch32 register without EL3; however, that would
+                 * be a migration compatibility break for those CPUs, so we
+                 * continue with having the cpregs in the hashtable.
                  */
-                if ((r->state == ARM_CP_STATE_BOTH && ns) ||
-                    (arm_feature(env, ARM_FEATURE_V8) && !ns)) {
+                if (!ns && arm_feature(env, ARM_FEATURE_AARCH64)) {
+                    g_free(r);
+                    return;
+                }
+                /*
+                 * If the register is banked then we don't need to migrate or
+                 * reset the 32-bit instance if this is a STATE_BOTH regdef.
+                 * This is because we can know for certain that there's a
+                 * 64-bit regdef that's using bank_fieldoffsets[1] as its
+                 * fieldoffset, because the code that handles STATE_BOTH
+                 * always registers it. In other situations either the NS
+                 * or the S banked register might be aliased (architecturally
+                 * or non-architecturally) to a 64-bit register, but it might
+                 * not be. For those we must manually mark the alias in the
+                 * regdef struct if we care.
+                 */
+                if (r->state == ARM_CP_STATE_BOTH && ns) {
                     r->type |= ARM_CP_ALIAS;
                 }
             } else if ((secstate != r->secure) && !ns) {
Signature · 1 line
2.43.0
#8 lore raw

[PULL 03/37] hw/gpio: Allwinner A10 GPIO emulation

In reply to Peter Maydell

From: Strahinja Jankovic <[email protected]>

This patch implements Allwinner A10 GPIO peripheral emulation.

The emulation focuses on input/output and interrupt functionality, whereas the drive and pull up/down functionality is not emulated.

Message-id: [email protected]

---
 MAINTAINERS                           |   2 +
 hw/gpio/Kconfig                       |   3 +
 hw/gpio/allwinner-gpio.c              | 441 ++++++++++++++++++++++++++
 hw/gpio/meson.build                   |   1 +
 hw/gpio/trace-events                  |   6 +
 include/hw/gpio/allwinner-gpio-regs.h | 227 +++++++++++++
 include/hw/gpio/allwinner-gpio.h      |  41 +++
 7 files changed, 721 insertions(+)
 create mode 100644 hw/gpio/allwinner-gpio.c
 create mode 100644 include/hw/gpio/allwinner-gpio-regs.h
 create mode 100644 include/hw/gpio/allwinner-gpio.h
MAINTAINERS +2 -0
index 1b66c65f689..628d2ef22b3 100644
@@ -746,6 +746,8 @@ F: include/hw/*/allwinner*
 F: hw/arm/cubieboard.c
 F: docs/system/arm/cubieboard.rst
 F: hw/misc/axp2xx.c
+F: hw/gpio/allwinner-gpio.c
+F: include/hw/gpio/allwinner-gpio*
 F: tests/functional/arm/test_cubieboard.py
 
 Allwinner-h3
hw/gpio/Kconfig +3 -0
index fcc7c70bd50..ea0fb72b406 100644
@@ -33,3 +33,6 @@ config ZAURUS_SCOOP
 
 config CADENCE_GPIO
     bool
+
+config ALLWINNER_GPIO
+    bool
hw/gpio/allwinner-gpio.c new file +441 -0

441 lines not on this page · raw message

hw/gpio/meson.build +1 -0
index 0555f44b6a7..8ede5e88ae5 100644
@@ -20,3 +20,4 @@ system_ss.add(when: 'CONFIG_ASPEED_SOC', if_true: files('aspeed_sgpio.c'))
 system_ss.add(when: 'CONFIG_SIFIVE_GPIO', if_true: files('sifive_gpio.c'))
 system_ss.add(when: 'CONFIG_PCF8574', if_true: files('pcf8574.c'))
 system_ss.add(when: 'CONFIG_CADENCE_GPIO', if_true: files('cadence_gpio.c'))
+system_ss.add(when: 'CONFIG_ALLWINNER_GPIO', if_true: files('allwinner-gpio.c'))
hw/gpio/trace-events +6 -0
index 80ca783a039..5ef3e0b8fd0 100644
@@ -51,3 +51,9 @@ stm32l4x5_gpio_pins(char *gpio, uint16_t disconnected, uint16_t high) "GPIO%s di
 cdns_gpio_read(const char *path, uint64_t offset, uint32_t value) "%s:reg[0x%04" PRIx64 "] -> 0x%" PRIx32
 cdns_gpio_write(const char *path, uint64_t offset, uint64_t value) "%s:reg[0x%04" PRIx64 "] <- 0x%04" PRIx64
 cdns_gpio_set(const char *path, int line, int level) "%s:[%d] <- %d"
+
+# allwinner_gpio.c
+allwinner_gpio_set(const char *port, int line, int level) "[%s:%d] <- %d"
+allwinner_gpio_read(const char *reg, uint32_t value) "[%s] -> 0x%" PRIx32
+allwinner_gpio_write(const char *reg, uint32_t value) "[%s] <- 0x%" PRIx32
+allwinner_gpio_out_pin(const char *reg, uint32_t pin, uint32_t value) "[%s.%d] <- 0x%" PRIx32
include/hw/gpio/allwinner-gpio-regs.h new file +227 -0

227 lines not on this page · raw message

include/hw/gpio/allwinner-gpio.h new file +41 -0

41 lines not on this page · raw message

Signature · 1 line
2.43.0
#9 lore raw

[PULL 19/37] target/arm: Don't register 64-bit TTBR0, TTBR1 for PMSA

In reply to Peter Maydell

We register the 64-bit versions of TTBR0 and TTBR1 in lpae_cp_reginfo[], marking them as ARM_CP_ALIAS because the AArch64 definitions in vmsa_cp_reginfo[] will handle their migration.

However, this means that we are also defining these registers as present in the Cortex-R52, which is a v8R PMSA CPU. v8R does define AMAIR0, AMAIR1 and the PAR, but does not have TTBR0 or TTBR1.

Split the TTBR0 and TTBR1 definitions into a new lpae_vmsa_cp_reginfo which we only register when we have both LPAE and VMSA.

This is a migration break for Cortex-R52 CPUs (which we don't use in any versioned machine type).

Message-id: [email protected]

---
 target/arm/helper.c | 6 ++++++
 1 file changed, 6 insertions(+)
target/arm/helper.c +6 -0
index 5f930f6fa8f..1fe06a6351c 100644
@@ -3098,6 +3098,9 @@ static const ARMCPRegInfo lpae_cp_reginfo[] = {
       .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
                              offsetof(CPUARMState, cp15.par_ns)} },
+};
+
+static const ARMCPRegInfo lpae_vmsa_cp_reginfo[] = {
     { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
       .access = PL1_RW, .accessfn = access_tvm_trvm,
       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
@@ -7226,6 +7229,9 @@ void register_cp_regs_for_features(ARMCPU *cpu)
     }
     if (arm_feature(env, ARM_FEATURE_LPAE)) {
         define_arm_cp_regs(cpu, lpae_cp_reginfo);
+        if (!arm_feature(env, ARM_FEATURE_PMSA)) {
+            define_arm_cp_regs(cpu, lpae_vmsa_cp_reginfo);
+        }
     }
     if (cpu_isar_feature(aa32_jazelle, cpu)) {
         define_arm_cp_regs(cpu, jazelle_regs);
Signature · 1 line
2.43.0
#10 lore raw

[PULL 15/37] target/arm: Drop uses of reset_ignore for banked AArch32 registers

In reply to Peter Maydell

Some banked AArch32 cpregs set up a resetfn of arm_cp_reset_ignore. This is presumably in an expectation that some other AArch64 register will handle resetting the underlying state. However this isn't true for some of them:

* TPIDRURO's S bank is in cp15.tpidruro_s and this isn't shared with any AArch64 register
* TPIDRURW's S bank similarly is in cp15.tpidrurw_s, not shared with any AArch64 register

Remove the use of arm_cp_reset_ignore here; this means that the state for the NS versions of these registers will be reset twice, but that is fine because it will always be to the same value (and the cp_reg_check_reset() code will assert if we introduce a bug where the AArch64 and AArch32 regs disagree about the reset value).

For the MAIR0 and MAIR1 banked registers, doing nothing on reset is OK because they are backed by state that is always covered by the MAIR_EL1 and MAIR_EL3 AArch64 registers that will always be present if the AArch32 registers are. But it's not consistent with how we handle all the other banked regs, so drop it here too. Again, the cp_reg_check_reset() checks will catch any mismatches in what the reset value should be.

The lack of reset-to-zero for the S banked versions of TPIDRURO and TPIDRURW is very unlikely to have had any guest visible consquences, because these registers are architecturally UNKNOWN at reset and so guest code should be initializing them itself regardless.

Message-id: [email protected]

---
 target/arm/helper.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
target/arm/helper.c +4 -8
index fe9838b3f6d..2bba059b521 100644
@@ -1072,14 +1072,12 @@ static const ARMCPRegInfo v7_cp_reginfo[] = {
       .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
       .access = PL1_RW, .accessfn = access_tvm_trvm,
       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
-                             offsetof(CPUARMState, cp15.mair0_ns) },
-      .resetfn = arm_cp_reset_ignore },
+                             offsetof(CPUARMState, cp15.mair0_ns) }, },
     { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
       .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1,
       .access = PL1_RW, .accessfn = access_tvm_trvm,
       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
-                             offsetof(CPUARMState, cp15.mair1_ns) },
-      .resetfn = arm_cp_reset_ignore },
+                             offsetof(CPUARMState, cp15.mair1_ns) }, },
     { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
       .fgt = FGT_ISR_EL1,
@@ -1136,8 +1134,7 @@ static const ARMCPRegInfo v6k_cp_reginfo[] = {
       .access = PL0_RW,
       .fgt = FGT_TPIDR_EL0,
       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
-                             offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
-      .resetfn = arm_cp_reset_ignore },
+                             offsetoflow32(CPUARMState, cp15.tpidrurw_ns) }, },
     { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
       .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
       .access = PL0_R | PL1_W,
@@ -1148,8 +1145,7 @@ static const ARMCPRegInfo v6k_cp_reginfo[] = {
       .access = PL0_R | PL1_W,
       .fgt = FGT_TPIDRRO_EL0,
       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
-                             offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
-      .resetfn = arm_cp_reset_ignore },
+                             offsetoflow32(CPUARMState, cp15.tpidruro_ns) }, },
     { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
       .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
       .access = PL1_RW,
Signature · 1 line
2.43.0
#11 lore raw

[PULL 07/37] hw/char/exynos4210_uart: fix fifo overflow

In reply to Peter Maydell

From: jprx <[email protected]>

The Exynos 4210 UART FIFO implementation uses a circular buffer with a store and retrieve pointer. When implemented this way, a circular buffer's capacity is 1 less than the size of the backing buffer, because there is no way to disambiguate the empty and full states (when the store pointer == retrieve pointer).

When exynos4210_uart_can_receive is called, it may report that the entire is buffer is available for receiving data, as fifo_empty_elements_number may incorrectly report that the FIFO capacity is equal to the entire buffer size (rather than 1 less than the size).

If this happens, a full FIFO buffer's worth of data will be read into the circular buffer, the store pointer will wrap around back to its starting value, and the FIFO will reset back to the empty state. The next time exynos4210_uart_can_receive is called, it will report the entire buffer is empty instead of full, and promptly allow the data to be overwritten before the guest has received it.

The consequence is that if a message is sent into the UART (eg. via a chardev) longer than the FIFO's buffer length, the first part of the message will be lost due to this overflowing behavior, and only the last part of the message will be received.

This patch fixes this bug by refactoring the Exynos 4210 UART to make use of the fifo8 implementation, which is implemented via a head pointer and length field, and therefore has capacity equal to the buffer length and is immune to this behavior.

The Exynos 4210 UART FIFO test now passes instead of timing out.

This is a migration compatibility break for the exynos4 boards, but that is OK as they are not versioned boards.

Message-id: [email protected]

[PMM: bump version fields to indicate migration compat break]
---
 hw/char/exynos4210_uart.c | 111 ++++++++++----------------------------
 1 file changed, 29 insertions(+), 82 deletions(-)
hw/char/exynos4210_uart.c +29 -82
index 46fc4e31b83..9d1825150d1 100644
@@ -28,6 +28,7 @@
 #include "qemu/timer.h"
 #include "chardev/char-fe.h"
 #include "chardev/char-serial.h"
+#include "qemu/fifo8.h"
 
 #include "hw/arm/exynos4210.h"
 #include "hw/core/irq.h"
@@ -133,12 +134,6 @@ static const Exynos4210UartReg exynos4210_uart_regs[] = {
 #define UERSTAT_FRAME    0x4
 #define UERSTAT_BREAK    0x8
 
-typedef struct {
-    uint8_t    *data;
-    uint32_t    sp, rp; /* store and retrieve pointers */
-    uint32_t    size;
-} Exynos4210UartFIFO;
-
 #define TYPE_EXYNOS4210_UART "exynos4210.uart"
 OBJECT_DECLARE_SIMPLE_TYPE(Exynos4210UartState, EXYNOS4210_UART)
 
@@ -148,8 +143,10 @@ struct Exynos4210UartState {
     MemoryRegion iomem;
 
     uint32_t             reg[EXYNOS4210_UART_REGS_MEM_SIZE / sizeof(uint32_t)];
-    Exynos4210UartFIFO   rx;
-    Exynos4210UartFIFO   tx;
+    Fifo8                rx;
+    Fifo8                tx;
+    uint32_t             rx_capacity;
+    uint32_t             tx_capacity;
 
     QEMUTimer *fifo_timeout_timer;
     uint64_t wordtime;        /* word time in ns */
@@ -178,45 +175,6 @@ static const char *exynos4210_uart_regname(hwaddr  offset)
     return NULL;
 }
 
-
-static void fifo_store(Exynos4210UartFIFO *q, uint8_t ch)
-{
-    q->data[q->sp] = ch;
-    q->sp = (q->sp + 1) % q->size;
-}
-
-static uint8_t fifo_retrieve(Exynos4210UartFIFO *q)
-{
-    uint8_t ret = q->data[q->rp];
-    q->rp = (q->rp + 1) % q->size;
-    return  ret;
-}
-
-static int fifo_elements_number(const Exynos4210UartFIFO *q)
-{
-    if (q->sp < q->rp) {
-        return q->size - q->rp + q->sp;
-    }
-
-    return q->sp - q->rp;
-}
-
-static int fifo_empty_elements_number(const Exynos4210UartFIFO *q)
-{
-    return q->size - fifo_elements_number(q);
-}
-
-static void fifo_reset(Exynos4210UartFIFO *q)
-{
-    g_free(q->data);
-    q->data = NULL;
-
-    q->data = g_malloc0(q->size);
-
-    q->sp = 0;
-    q->rp = 0;
-}
-
 static uint32_t exynos4210_uart_FIFO_trigger_level(uint32_t channel,
                                                    uint32_t reg)
 {
@@ -271,7 +229,7 @@ exynos4210_uart_Rx_FIFO_trigger_level(const Exynos4210UartState *s)
 static void exynos4210_uart_update_dmabusy(Exynos4210UartState *s)
 {
     bool rx_dma_enabled = (s->reg[I_(UCON)] & 0x03) == 0x02;
-    uint32_t count = fifo_elements_number(&s->rx);
+    uint32_t count = fifo8_num_used(&s->rx);
 
     if (rx_dma_enabled && !count) {
         qemu_irq_raise(s->dmairq);
@@ -300,7 +258,7 @@ static void exynos4210_uart_update_irq(Exynos4210UartState *s)
          * Rx interrupt if trigger level is reached or if rx timeout
          * interrupt is disabled and there is data in the receive buffer
          */
-        count = fifo_elements_number(&s->rx);
+        count = fifo8_num_used(&s->rx);
         if ((count && !(s->reg[I_(UCON)] & 0x80)) ||
             count >= exynos4210_uart_Rx_FIFO_trigger_level(s)) {
             exynos4210_uart_update_dmabusy(s);
@@ -416,12 +374,12 @@ static void exynos4210_uart_write(void *opaque, hwaddr offset,
     case UFCON:
         s->reg[I_(UFCON)] = val;
         if (val & UFCON_Rx_FIFO_RESET) {
-            fifo_reset(&s->rx);
+            fifo8_reset(&s->rx);
             s->reg[I_(UFCON)] &= ~UFCON_Rx_FIFO_RESET;
             trace_exynos_uart_rx_fifo_reset(s->channel);
         }
         if (val & UFCON_Tx_FIFO_RESET) {
-            fifo_reset(&s->tx);
+            fifo8_reset(&s->tx);
             s->reg[I_(UFCON)] &= ~UFCON_Tx_FIFO_RESET;
             trace_exynos_uart_tx_fifo_reset(s->channel);
         }
@@ -490,8 +448,8 @@ static uint64_t exynos4210_uart_read(void *opaque, hwaddr offset,
                                exynos4210_uart_regname(offset), res);
         return res;
     case UFSTAT: /* Read Only */
-        s->reg[I_(UFSTAT)] = fifo_elements_number(&s->rx) & 0xff;
-        if (fifo_empty_elements_number(&s->rx) == 0) {
+        s->reg[I_(UFSTAT)] = fifo8_num_used(&s->rx) & 0xff;
+        if (fifo8_num_free(&s->rx) == 0) {
             s->reg[I_(UFSTAT)] |= UFSTAT_Rx_FIFO_FULL;
             s->reg[I_(UFSTAT)] &= ~0xff;
         }
@@ -501,10 +459,10 @@ static uint64_t exynos4210_uart_read(void *opaque, hwaddr offset,
         return s->reg[I_(UFSTAT)];
     case URXH:
         if (s->reg[I_(UFCON)] & UFCON_FIFO_ENABLE) {
-            if (fifo_elements_number(&s->rx)) {
-                res = fifo_retrieve(&s->rx);
+            if (fifo8_num_used(&s->rx)) {
+                res = fifo8_pop(&s->rx);
                 trace_exynos_uart_rx(s->channel, res);
-                if (!fifo_elements_number(&s->rx)) {
+                if (!fifo8_num_used(&s->rx)) {
                     s->reg[I_(UTRSTAT)] &= ~UTRSTAT_Rx_BUFFER_DATA_READY;
                 } else {
                     s->reg[I_(UTRSTAT)] |= UTRSTAT_Rx_BUFFER_DATA_READY;
@@ -555,7 +513,7 @@ static int exynos4210_uart_can_receive(void *opaque)
     Exynos4210UartState *s = (Exynos4210UartState *)opaque;
 
     if (s->reg[I_(UFCON)] & UFCON_FIFO_ENABLE) {
-        return fifo_empty_elements_number(&s->rx);
+        return fifo8_num_free(&s->rx);
     } else {
         return !(s->reg[I_(UTRSTAT)] & UTRSTAT_Rx_BUFFER_DATA_READY);
     }
@@ -567,12 +525,12 @@ static void exynos4210_uart_receive(void *opaque, const uint8_t *buf, int size)
     int i;
 
     if (s->reg[I_(UFCON)] & UFCON_FIFO_ENABLE) {
-        if (fifo_empty_elements_number(&s->rx) < size) {
-            size = fifo_empty_elements_number(&s->rx);
+        if (fifo8_num_free(&s->rx) < size) {
+            size = fifo8_num_free(&s->rx);
             s->reg[I_(UINTSP)] |= UINTSP_ERROR;
         }
         for (i = 0; i < size; i++) {
-            fifo_store(&s->rx, buf[i]);
+            fifo8_push(&s->rx, buf[i]);
         }
         exynos4210_uart_rx_timeout_set(s);
     } else {
@@ -591,7 +549,7 @@ static void exynos4210_uart_event(void *opaque, QEMUChrEvent event)
     if (event == CHR_EVENT_BREAK) {
         /* When the RxDn is held in logic 0, then a null byte is pushed into the
          * fifo */
-        fifo_store(&s->rx, '\0');
+        fifo8_push(&s->rx, '\0');
         s->reg[I_(UERSTAT)] |= UERSTAT_BREAK;
         exynos4210_uart_update_irq(s);
     }
@@ -608,10 +566,10 @@ static void exynos4210_uart_reset(DeviceState *dev)
                 exynos4210_uart_regs[i].reset_value;
     }
 
-    fifo_reset(&s->rx);
-    fifo_reset(&s->tx);
+    fifo8_reset(&s->rx);
+    fifo8_reset(&s->tx);
 
-    trace_exynos_uart_rxsize(s->channel, s->rx.size);
+    trace_exynos_uart_rxsize(s->channel, s->rx_capacity);
 }
 
 static int exynos4210_uart_post_load(void *opaque, int version_id)
@@ -624,26 +582,13 @@ static int exynos4210_uart_post_load(void *opaque, int version_id)
     return 0;
 }
 
-static const VMStateDescription vmstate_exynos4210_uart_fifo = {
-    .name = "exynos4210.uart.fifo",
-    .version_id = 1,
-    .minimum_version_id = 1,
-    .fields = (const VMStateField[]) {
-        VMSTATE_UINT32(sp, Exynos4210UartFIFO),
-        VMSTATE_UINT32(rp, Exynos4210UartFIFO),
-        VMSTATE_VBUFFER_UINT32(data, Exynos4210UartFIFO, 1, NULL, size),
-        VMSTATE_END_OF_LIST()
-    }
-};
-
 static const VMStateDescription vmstate_exynos4210_uart = {
     .name = "exynos4210.uart",
-    .version_id = 1,
-    .minimum_version_id = 1,
+    .version_id = 2,
+    .minimum_version_id = 2,
     .post_load = exynos4210_uart_post_load,
     .fields = (const VMStateField[]) {
-        VMSTATE_STRUCT(rx, Exynos4210UartState, 1,
-                       vmstate_exynos4210_uart_fifo, Exynos4210UartFIFO),
+        VMSTATE_FIFO8(rx, Exynos4210UartState),
         VMSTATE_UINT32_ARRAY(reg, Exynos4210UartState,
                              EXYNOS4210_UART_REGS_MEM_SIZE / sizeof(uint32_t)),
         VMSTATE_END_OF_LIST()
@@ -699,6 +644,8 @@ static void exynos4210_uart_realize(DeviceState *dev, Error **errp)
     s->fifo_timeout_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
                                          exynos4210_uart_timeout_int, s);
 
+    fifo8_create(&s->rx, s->rx_capacity);
+    fifo8_create(&s->tx, s->tx_capacity);
     qemu_chr_fe_set_handlers(&s->chr, exynos4210_uart_can_receive,
                              exynos4210_uart_receive, exynos4210_uart_event,
                              NULL, s, NULL, true);
@@ -707,8 +654,8 @@ static void exynos4210_uart_realize(DeviceState *dev, Error **errp)
 static const Property exynos4210_uart_properties[] = {
     DEFINE_PROP_CHR("chardev", Exynos4210UartState, chr),
     DEFINE_PROP_UINT32("channel", Exynos4210UartState, channel, 0),
-    DEFINE_PROP_UINT32("rx-size", Exynos4210UartState, rx.size, 16),
-    DEFINE_PROP_UINT32("tx-size", Exynos4210UartState, tx.size, 16),
+    DEFINE_PROP_UINT32("rx-size", Exynos4210UartState, rx_capacity, 16),
+    DEFINE_PROP_UINT32("tx-size", Exynos4210UartState, tx_capacity, 16),
 };
 
 static void exynos4210_uart_class_init(ObjectClass *klass, const void *data)
Signature · 1 line
2.43.0
#12 lore raw

[PULL 29/37] target/arm: Clear PSTATE.UINJ when taking exceptions

In reply to Peter Maydell

From: Richard Henderson <[email protected]>

Clear UINJ when taking an exception.
Clear UINJ when exception return is invalid.

In both cases, perform the clear without feature check, since the bit is otherwise RES0.

Message-id: [email protected]

---
 target/arm/cpu.h            | 1 +
 target/arm/helper.c         | 9 +++++++++
 target/arm/tcg/helper-a64.c | 2 ++
 3 files changed, 12 insertions(+)
target/arm/cpu.h +1 -0
index 141cfdf6a88..78bf4576a58 100644
@@ -1603,6 +1603,7 @@ void pmu_init(ARMCPU *cpu);
 #define PSTATE_Z (1 << 30)
 #define PSTATE_N (1ULL << 31)
 #define PSTATE_EXLOCK (1ULL << 34)
+#define PSTATE_UINJ (1ULL << 36)
 #define PSTATE_NZCV (PSTATE_N | PSTATE_Z | PSTATE_C | PSTATE_V)
 #define PSTATE_DAIF (PSTATE_D | PSTATE_A | PSTATE_I | PSTATE_F)
 #define CACHED_PSTATE_BITS (PSTATE_NZCV | PSTATE_DAIF | PSTATE_BTYPE)
target/arm/helper.c +9 -0
index 9ba924e5de3..76956cfd97b 100644
@@ -9744,6 +9744,15 @@ static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
 
         env->condexec_bits = 0;
     }
+
+    /*
+     * R_XKSNJ: If FEAT_UINJ, PSTATE.UINJ is set to 0 *and* the resulting
+     * value stored in SPSR_ELx is 0.  The new PSTATE is automatically
+     * handled by construction of a new value from 0; clear it from the
+     * old state and as UINJ is RES0 otherwise, skip the feature check.
+     */
+    old_mode &= ~PSTATE_UINJ;
+
     env->banked_spsr[aarch64_banked_spsr_index(new_el)] = old_mode;
 
     qemu_log_mask(CPU_LOG_INT, "...with SPSR 0x%" PRIx64 "\n", old_mode);
target/arm/tcg/helper-a64.c +2 -0
index 9d805231a08..f76d89990c3 100644
@@ -764,10 +764,12 @@ illegal_return:
      * mandated behaviour:
      * restore NZCV and DAIF from SPSR_ELx
      * set PSTATE.IL
+     * reset PSTATE.UINJ
      * restore PC from ELR_ELx
      * no change to exception level, execution state or stack pointer
      */
     env->pstate |= PSTATE_IL;
+    env->pstate &= ~PSTATE_UINJ;
     env->pc = new_pc;
     spsr &= PSTATE_NZCV | PSTATE_DAIF | PSTATE_ALLINT;
     spsr |= pstate_read(env) & ~(PSTATE_NZCV | PSTATE_DAIF | PSTATE_ALLINT);
Signature · 1 line
2.43.0
#13 lore raw

[PULL 08/37] tests/qtest: add test for exynos4210 uart fifo overflow bug

In reply to Peter Maydell

From: jprx <[email protected]>

The Exynos 4210 UART FIFO implementation will overflow if a large buffer is sent via a chardev. This test demonstrates the issue by sending an input message that is 4x the size of the UART0 FIFO length.

A correct implementation will return the complete message, and an incorrect one will timeout. This is because the FIFO overflows when receiving data from the chardev, skipping over the first part of the input message, resulting in us not reading the full message and timing out.

Message-id: [email protected]

---
 tests/qtest/exynos4210_uart_fifo-test.c | 86 +++++++++++++++++++++++++
 tests/qtest/meson.build                 |  4 ++
 2 files changed, 90 insertions(+)
 create mode 100644 tests/qtest/exynos4210_uart_fifo-test.c
tests/qtest/exynos4210_uart_fifo-test.c new file +86 -0
new file mode 100644
index 00000000000..54c09a6de72
@@ -0,0 +1,86 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+#include "qemu/osdep.h"
+#include "libqtest.h"
+
+/* defs from exynos4210.c */
+#define UART0_BASE_ADDR     0x13800000
+#define UART0_FIFO_SIZE     256
+#define UFCON      0x0008 /* FIFO Control             */
+#define UTRSTAT    0x0010 /* Tx/Rx Status             */
+#define URXH       0x0024 /* Receive Buffer           */
+#define UFCON_FIFO_ENABLE                    0x1
+#define UTRSTAT_Rx_BUFFER_DATA_READY         0x1
+
+#define TEST_BUFFER_LEN        ((4 * UART0_FIFO_SIZE))
+
+static bool uart_wait_for_flag(QTestState *qts, uint32_t event_addr,
+                               uint32_t flag)
+{
+    while (true) {
+        if ((qtest_readl(qts, event_addr) & flag)) {
+            return true;
+        }
+        g_usleep(1000);
+    }
+
+    return false;
+}
+
+static void uart_receive_string(QTestState *qts, int sock_fd, const char *in,
+                                char *out)
+{
+    size_t i, in_len = strlen(in);
+
+    g_assert_true(send(sock_fd, in, in_len, 0) == in_len);
+    for (i = 0; i < in_len; i++) {
+        g_assert_true(uart_wait_for_flag(qts,
+            UART0_BASE_ADDR + UTRSTAT, UTRSTAT_Rx_BUFFER_DATA_READY));
+        out[i] = qtest_readl(qts, UART0_BASE_ADDR + URXH);
+    }
+    out[i] = '\0';
+}
+
+static void enable_uart_fifo(QTestState *qts)
+{
+    qtest_writel(qts, UART0_BASE_ADDR + UFCON, UFCON_FIFO_ENABLE);
+}
+
+/*
+ * If the FIFO overflows when receiving data from the chardev, early input
+ * chunks will be lost, and this will timeout waiting to read the full
+ * buffer. If the FIFO behaves correctly, we will receive the full buffer.
+ */
+static void test_recv_large_str(void)
+{
+    int sock_fd;
+    char *sendbuf, *recvbuf;
+    QTestState *qts = qtest_init_with_serial("-M smdkc210", &sock_fd);
+
+    enable_uart_fifo(qts);
+
+    sendbuf = g_new0(char, TEST_BUFFER_LEN);
+    recvbuf = g_new0(char, TEST_BUFFER_LEN);
+    memset(sendbuf, 'A', TEST_BUFFER_LEN);
+    sendbuf[TEST_BUFFER_LEN - 1] = '\x00';
+
+    g_assert_true(0 == strlen(recvbuf));
+    g_assert_true(TEST_BUFFER_LEN - 1 == strlen(sendbuf));
+
+    uart_receive_string(qts, sock_fd, sendbuf, recvbuf);
+
+    g_assert_true(TEST_BUFFER_LEN - 1 == strlen(recvbuf));
+    g_assert_true(strcmp(sendbuf, recvbuf) == 0);
+
+    close(sock_fd);
+
+    qtest_quit(qts);
+}
+
+int main(int argc, char **argv)
+{
+    g_test_init(&argc, &argv, NULL);
+    g_test_set_nonfatal_assertions();
+
+    qtest_add_func("exynos4210/uart/receive_large_str", test_recv_large_str);
+    return g_test_run();
+}
tests/qtest/meson.build +4 -0
index a35ae79b03d..3aed0c3a00a 100644
@@ -249,6 +249,9 @@ qtests_stm32l4x5 = \
    'stm32l4x5_gpio-test',
    'stm32l4x5_usart-test']
 
+qtests_exynos4210 = \
+  ['exynos4210_uart_fifo-test']
+
 qtests_arm = \
   (config_all_devices.has_key('CONFIG_MPS2') ? ['sse-timer-test'] : []) + \
   (config_all_devices.has_key('CONFIG_CMSDK_APB_DUALTIMER') ? ['cmsdk-apb-dualtimer-test'] : []) + \
@@ -283,6 +286,7 @@ qtests_aarch64 = \
    config_all_devices.has_key('CONFIG_TPM_TIS_I2C') ? ['tpm-tis-i2c-test', 'tpm-tis-i2c-swtpm-test'] : []) + \
   (config_all_devices.has_key('CONFIG_ASPEED_SOC') ? qtests_aspeed64 : []) + \
   (config_all_devices.has_key('CONFIG_NPCM8XX') ? qtests_npcm8xx : []) + \
+  (config_all_devices.has_key('CONFIG_EXYNOS4') ? qtests_exynos4210 : []) + \
   (config_all_devices.has_key('CONFIG_IOMMU_TESTDEV') and
    config_all_devices.has_key('CONFIG_ARM_VIRT') ?
    ['iommu-smmuv3-test'] : []) + \
Signature · 1 line
2.43.0
#14 lore raw

[PULL 26/37] target/arm: Move ID_AA64PFR2 feature tests together

In reply to Peter Maydell

From: Richard Henderson <[email protected]>

Currently the MTE feature tests are clustered together, but those are spread across two ID registers. Sort the functions by ID reg and bitfield order, like most others.

Message-id: [email protected]

---
 target/arm/cpu-features.h | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
target/arm/cpu-features.h +10 -10
index eecbc3b889e..06318d12f9f 100644
@@ -1207,16 +1207,6 @@ static inline bool isar_feature_aa64_mte3(const ARMISARegisters *id)
     return FIELD_EX64_IDREG(id, ID_AA64PFR1, MTE) >= 3;
 }
 
-static inline bool isar_feature_aa64_mteperm(const ARMISARegisters *id)
-{
-    return FIELD_EX64_IDREG(id, ID_AA64PFR2, MTEPERM) >= 1;
-}
-
-static inline bool isar_feature_aa64_mte_store_only(const ARMISARegisters *id)
-{
-    return FIELD_EX64_IDREG(id, ID_AA64PFR2, MTESTOREONLY) == 1;
-}
-
 static inline bool isar_feature_aa64_mte_mtx(const ARMISARegisters *id)
 {
     return FIELD_EX64_IDREG(id, ID_AA64PFR1, MTEX) != 0;
@@ -1237,6 +1227,16 @@ static inline bool isar_feature_aa64_gcs(const ARMISARegisters *id)
     return FIELD_EX64_IDREG(id, ID_AA64PFR1, GCS) != 0;
 }
 
+static inline bool isar_feature_aa64_mteperm(const ARMISARegisters *id)
+{
+    return FIELD_EX64_IDREG(id, ID_AA64PFR2, MTEPERM) >= 1;
+}
+
+static inline bool isar_feature_aa64_mte_store_only(const ARMISARegisters *id)
+{
+    return FIELD_EX64_IDREG(id, ID_AA64PFR2, MTESTOREONLY) == 1;
+}
+
 static inline bool isar_feature_aa64_gcie(const ARMISARegisters *id)
 {
     return FIELD_EX64_IDREG(id, ID_AA64PFR2, GCIE) != 0;
Signature · 1 line
2.43.0
#15 lore raw

[PULL 16/37] target/arm: Don't use arm_cp_reset_ignore when fieldoffset is 0

In reply to Peter Maydell

In cp_reg_reset(), we treat an ARMCPRegInfo::fieldoffset value of 0 as "reset is handled elsewhere". So we don't need to explicitly set ri->resetfn to arm_cp_reset_ignore() when we haven't defined a fieldoffset in the cpreg struct.

Remove the setting of resetfn to arm_cp_reset_ignore in the handful of places where we were doing this unnecessarily. These are all timer registers, and doing this brings them in to line with the other timer registers (e.g. CNTPCT_EL0 has no resetfn, and now CNTPCT doesn't either).

Message-id: [email protected]

---
 target/arm/helper.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
target/arm/helper.c +4 -4
index 2bba059b521..c2e94153634 100644
@@ -2209,7 +2209,7 @@ static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
     { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
       .accessfn = gt_pct_access,
-      .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
+      .readfn = gt_cnt_read,
     },
     { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
@@ -2219,7 +2219,7 @@ static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
     { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
       .accessfn = gt_vct_access,
-      .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
+      .readfn = gt_virt_cnt_read,
     },
     { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
@@ -2310,7 +2310,7 @@ static const ARMCPRegInfo gen_timer_ecv_cp_reginfo[] = {
     { .name = "CNTVCTSS", .cp = 15, .crm = 14, .opc1 = 9,
       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
       .accessfn = gt_vct_access,
-      .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
+      .readfn = gt_virt_cnt_read,
     },
     { .name = "CNTVCTSS_EL0", .state = ARM_CP_STATE_AA64,
       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 6,
@@ -2320,7 +2320,7 @@ static const ARMCPRegInfo gen_timer_ecv_cp_reginfo[] = {
     { .name = "CNTPCTSS", .cp = 15, .crm = 14, .opc1 = 8,
       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
       .accessfn = gt_pct_access,
-      .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
+      .readfn = gt_cnt_read,
     },
     { .name = "CNTPCTSS_EL0", .state = ARM_CP_STATE_AA64,
       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 5,
Signature · 1 line
2.43.0
#16 lore raw

[PULL 20/37] target/arm: Add a debug check that all sysregs are migrated

In reply to Peter Maydell

We mark some sysregs with ARM_CP_ALIAS (either manually or automatically) to indicate that they are accessing underlying guest CPU state whose migration and reset is handled by some other register. (For example, typically we make the 32-bit view of a register an alias and handle migration for the 64-bit view.)

Add a debug check that we actually do handle the migration elsewhere:
for every sysreg in the hashtable (i.e. which might be visible to the guest), check that some register in the migration list has a field offset that matches. This isn't perfect but it can catch some bugs. In particular it did fire for the various secure banked registers that we were not migrating before the fixes in the previous patches.

Because the check is more of a debugging aid than something I'm confident will never produce a false-positive report for some niche combination of CPU and machine options, we only do it for debug builds, and make it a warning printed to the user rather than an assertion failure.

Message-id: [email protected]

---
 target/arm/helper.c | 121 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 121 insertions(+)
target/arm/helper.c +121 -0
index 1fe06a6351c..9ba924e5de3 100644
@@ -230,6 +230,125 @@ bool write_list_to_cpustate(ARMCPU *cpu)
     return ok;
 }
 
+#ifdef CONFIG_DEBUG_TCG
+/* Data to pass through to check_cpreg() */
+typedef struct CheckCPRegData {
+    ARMCPU *cpu;
+    bool warned;
+} CheckCPRegData;
+
+static void check_cpreg(gpointer key, gpointer value, gpointer opaque)
+{
+    CheckCPRegData *data = opaque;
+    ARMCPU *cpu = data->cpu;
+    CPUARMState *env = &cpu->env;
+    bool match = false;
+    const ARMCPRegInfo *ri = value;
+
+    if ((ri->type & (ARM_CP_NO_RAW | ARM_CP_CONST)) ||
+        ri->fieldoffset == 0) {
+        return;
+    }
+
+    /*
+     * If we don't have EL3 then the guest can't get at S-only
+     * registers even if they're in the hashtable. This avoids
+     * false-positive complaints about e.g. TTBCR_S and DFSR_S: they
+     * are migrated via ESR_EL3 and TCR_EL3, but we only register those
+     * _EL3 regs if ARM_FEATURE_EL3.
+     */
+    if (ri->secure == ARM_CP_SECSTATE_S && !arm_feature(env, ARM_FEATURE_EL3)) {
+        return;
+    }
+
+    /*
+     * Some special cases for sysregs that are backed by fields that we
+     * migrate in the main vmstate, not the cpregs array.
+     */
+    if (ri->fieldoffset >= offsetof(CPUARMState, banked_spsr[BANK_USRSYS]) &&
+        ri->fieldoffset <= offsetof(CPUARMState, banked_spsr[BANK_MON])) {
+        return;
+    }
+    if (ri->fieldoffset >= offsetof(CPUARMState, elr_el[0]) &&
+        ri->fieldoffset <= offsetof(CPUARMState, elr_el[3])) {
+        return;
+    }
+    if (ri->fieldoffset >= offsetof(CPUARMState, sp_el[0]) &&
+        ri->fieldoffset <= offsetof(CPUARMState, sp_el[3])) {
+        return;
+    }
+    if (ri->fieldoffset == offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC])) {
+        return;
+    }
+
+    for (int i = 0; i < cpu->cpreg_array_len; i++) {
+        uint32_t other_regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
+        const ARMCPRegInfo *other_ri = get_arm_cp_reginfo(cpu->cp_regs,
+                                                          other_regidx);
+
+        if (!other_ri || (other_ri->type & (ARM_CP_NO_RAW | ARM_CP_CONST)) ||
+            other_ri->fieldoffset == 0) {
+            continue;
+        }
+
+        /*
+         * If the field offsets match exactly, or this 32-bit cpreg
+         * is in the second half of a 64-bit field, consider it to
+         * be handled.
+         */
+        if (ri->fieldoffset == other_ri->fieldoffset ||
+            (cpreg_field_type(ri) == MO_32 &&
+             cpreg_field_type(other_ri) == MO_64 &&
+             ri->fieldoffset == other_ri->fieldoffset + 4)) {
+            match = true;
+            break;
+        }
+    }
+    if (!match) {
+        warn_report("check_cpreg: no migration entry found for %s", ri->name);
+        data->warned = true;
+    }
+}
+#endif /* CONFIG_DEBUG_TCG */
+
+static void arm_check_cpreg_coverage(ARMCPU *cpu)
+{
+    /*
+     * Try to catch bugs where we don't actually migrate a cpreg.
+     * Specifically, here we check that every cpreg in the hash table
+     * (and thus potentially visible to the guest) which specifies
+     * a fieldoffset has some entry in the cpreg_indexes[] array that
+     * handles that same fieldoffset.
+     * The typical bug that will be flagged up here is if a register
+     * is marked as an alias of something else, but there isn't actually
+     * anything else that handles the field.
+     *
+     * Note that the checks are more of a best-effort, are somewhat
+     * expensive at O(n^2) in the number of sysregs, and need some
+     * cases to be excluded where the sysreg data isn't migrated via
+     * the cpreg arrays. So we only do them in --enable-debug builds,
+     * to avoid potentially causing problems for users.
+     */
+#ifdef CONFIG_DEBUG_TCG
+    CheckCPRegData data = {
+        .cpu = cpu,
+        .warned = false,
+    };
+
+    /* We'll only use the cpregs for migration with TCG */
+    if (!tcg_enabled()) {
+        return;
+    }
+    g_hash_table_foreach(cpu->cp_regs, check_cpreg, &data);
+
+    if (data.warned) {
+        error_printf("check_cpreg errors are a QEMU bug that may cause "
+                     "migration to fail. Please report this with the full "
+                     "QEMU command line and version.\n");
+    }
+#endif
+}
+
 static void add_cpreg_to_list(gpointer key, gpointer value, gpointer opaque)
 {
     ARMCPU *cpu = opaque;
@@ -281,6 +400,8 @@ void arm_init_cpreg_list(ARMCPU *cpu)
     if (arraylen) {
         qsort(cpu->cpreg_indexes, arraylen, sizeof(uint64_t), compare_u64);
     }
+
+    arm_check_cpreg_coverage(cpu);
 }
 
 bool arm_pan_enabled(CPUARMState *env)
Signature · 1 line
2.43.0
#17 lore raw

[PULL 10/37] arm: handle CCSIDR_EL1 as a demuxed register

In reply to Peter Maydell

From: Cornelia Huck <[email protected]>

Move handling of CCSIDR_EL1 over to the new *_IDREG_DEMUX infrastructure.

Note: the existing size of 16 is kept and might be extended at a later time.

Message-id: [email protected]

---
 hw/arm/virt.c                | 19 ++++------
 hw/intc/armv7m_nvic.c        |  2 +-
 target/arm/cpu-max.c         |  6 ++--
 target/arm/cpu-sysregs.h.inc |  1 +
 target/arm/cpu.h             | 12 +++----
 target/arm/cpu64.c           |  6 ++--
 target/arm/helper.c          |  2 +-
 target/arm/tcg/cpu32.c       | 26 +++++++-------
 target/arm/tcg/cpu64.c       | 68 ++++++++++++++++++------------------
 9 files changed, 69 insertions(+), 73 deletions(-)
hw/arm/virt.c +7 -12
index 3eecf094bcc..bdac33a1de8 100644
@@ -306,24 +306,19 @@ void set_cpu_cache(CPUCoreCaches *cpu_cache, enum CacheType cache_type,
     int bank_index = ((cache_level - 1) * 2) | is_i_cache0;
     ARMCPU *armcpu = ARM_CPU(qemu_get_cpu(0));
     bool ccidx = cpu_isar_feature(any_ccidx, armcpu);
+    uint64_t ccsidr = GET_IDREG_DEMUX(&armcpu->isar, CCSIDR_EL1, bank_index);
 
     if (ccidx) {
         *cpu_cache = (CPUCoreCaches){
-            .linesize = 1 << (FIELD_EX64(armcpu->ccsidr[bank_index], CCSIDR_EL1,
-                                         CCIDX_LINESIZE) + 4),
-            .associativity = FIELD_EX64(armcpu->ccsidr[bank_index], CCSIDR_EL1,
-                                        CCIDX_ASSOCIATIVITY) + 1,
-            .sets = FIELD_EX64(armcpu->ccsidr[bank_index], CCSIDR_EL1,
-                               CCIDX_NUMSETS) + 1,
+            .linesize = 1 << (FIELD_EX64(ccsidr, CCSIDR_EL1, CCIDX_LINESIZE) + 4),
+            .associativity = FIELD_EX64(ccsidr, CCSIDR_EL1, CCIDX_ASSOCIATIVITY) + 1,
+            .sets = FIELD_EX64(ccsidr, CCSIDR_EL1, CCIDX_NUMSETS) + 1,
         };
     } else {
         *cpu_cache = (CPUCoreCaches){
-            .linesize = 1 << (FIELD_EX64(armcpu->ccsidr[bank_index], CCSIDR_EL1,
-                                         LINESIZE) + 4),
-            .associativity = FIELD_EX64(armcpu->ccsidr[bank_index], CCSIDR_EL1,
-                                        ASSOCIATIVITY) + 1,
-            .sets =
-                FIELD_EX64(armcpu->ccsidr[bank_index], CCSIDR_EL1, NUMSETS) + 1,
+            .linesize = 1 << (FIELD_EX64(ccsidr, CCSIDR_EL1, LINESIZE) + 4),
+            .associativity = FIELD_EX64(ccsidr, CCSIDR_EL1, ASSOCIATIVITY) + 1,
+            .sets = FIELD_EX64(ccsidr, CCSIDR_EL1, NUMSETS) + 1,
         };
     }
     cpu_cache->type = cache_type;
hw/intc/armv7m_nvic.c +1 -1
index 4136c91f235..6bc392b0e33 100644
@@ -1360,7 +1360,7 @@ static uint32_t nvic_readl(NVICState *s, uint32_t offset, MemTxAttrs attrs)
     case 0xd80: /* CSSIDR */
     {
         int idx = cpu->env.v7m.csselr[attrs.secure] & R_V7M_CSSELR_INDEX_MASK;
-        return cpu->ccsidr[idx];
+        return GET_IDREG_DEMUX(&cpu->isar, CCSIDR_EL1, idx);
     }
     case 0xd84: /* CSSELR */
         return cpu->env.v7m.csselr[attrs.secure];
target/arm/cpu-max.c +3 -3
index 739500a04fc..2ed1cfef9be 100644
@@ -71,11 +71,11 @@ void aarch64_aa32_a57_init(ARMCPU *cpu, bool aarch64_enabled)
     cpu->isar.reset_pmcr_el0 = 0x41013000;
     SET_IDREG(isar, CLIDR, 0x0a200023);
     /* 32KB L1 dcache */
-    cpu->ccsidr[0] = make_ccsidr(CCSIDR_FORMAT_LEGACY, 4, 64, 32 * KiB, 7);
+    SET_IDREG_DEMUX(isar, CCSIDR_EL1, 0, make_ccsidr(CCSIDR_FORMAT_LEGACY, 4, 64, 32 * KiB, 7));
     /* 48KB L1 icache */
-    cpu->ccsidr[1] = make_ccsidr(CCSIDR_FORMAT_LEGACY, 3, 64, 48 * KiB, 2);
+    SET_IDREG_DEMUX(isar, CCSIDR_EL1, 1, make_ccsidr(CCSIDR_FORMAT_LEGACY, 3, 64, 48 * KiB, 2));
     /* 2048KB L2 cache */
-    cpu->ccsidr[2] = make_ccsidr(CCSIDR_FORMAT_LEGACY, 16, 64, 2 * MiB, 7);
+    SET_IDREG_DEMUX(isar, CCSIDR_EL1, 2, make_ccsidr(CCSIDR_FORMAT_LEGACY, 16, 64, 2 * MiB, 7));
     if (aarch64_enabled) {
         set_dczid_bs(cpu, 4); /* 64 bytes */
         cpu->gic_num_lrs = 4;
target/arm/cpu-sysregs.h.inc +1 -0
index 6e8b335b8f6..d9e058a57e5 100644
@@ -39,6 +39,7 @@ DEF(MVFR2_EL1, 3, 0, 0, 3, 2)
 DEF(ID_PFR2_EL1, 3, 0, 0, 3, 4)
 DEF(ID_DFR1_EL1, 3, 0, 0, 3, 5)
 DEF(ID_MMFR5_EL1, 3, 0, 0, 3, 6)
+DEF_MUX(CCSIDR_EL1, 3, 1, 0, 0, 0, 16)
 DEF(CLIDR_EL1, 3, 1, 0, 0, 1)
 DEF(ID_AA64ZFR0_EL1, 3, 0, 0, 4, 4)
 DEF(CTR_EL0, 3, 3, 0, 0, 1)
target/arm/cpu.h +6 -6
index 6d1bad1dfc6..5c8ea5a7ed2 100644
@@ -1106,6 +1106,12 @@ struct ArchCPU {
      * kvm_arm_get_host_cpu_features() function to correctly populate the
      * field by reading the value from the KVM vCPU. If it is an AArch64
      * ID register then you also must update arm_clear_aarch64_idregs().
+     *
+     * The multiplexed register CCSIDR is handled as part of idregs[] -
+     * these contain values for each cache, in the order L1DCache, L1ICache,
+     * L2DCache, L2ICache, etc. Currently only 16 indexes into CCSIDR are
+     * supported because we don't implement separate MTE Allocation Tag caches.
+     * See {G,S}ET_IDREG_DEMUX() accessors.
      */
     struct ARMISARegisters {
         uint32_t mvfr0;
@@ -1125,10 +1131,6 @@ struct ArchCPU {
     uint64_t pmceid0;
     uint64_t pmceid1;
     uint64_t mp_affinity; /* MP ID without feature bits */
-    /* The elements of this array are the CCSIDR values for each cache,
-     * in the order L1DCache, L1ICache, L2DCache, L2ICache, etc.
-     */
-    uint64_t ccsidr[16];
     uint64_t reset_cbar;
     uint32_t reset_auxcr;
     bool reset_hivecs;
@@ -2142,8 +2144,6 @@ FIELD(MFAR, FPA, 12, 40)
 FIELD(MFAR, NSE, 62, 1)
 FIELD(MFAR, NS, 63, 1)
 
-QEMU_BUILD_BUG_ON(ARRAY_SIZE(((ARMCPU *)0)->ccsidr) <= R_V7M_CSSELR_INDEX_MASK);
-
 /* If adding a feature bit which corresponds to a Linux ELF
  * HWCAP bit, remember to update the feature-bit-to-hwcap
  * mapping in linux-user/elfload.c:get_elf_hwcap().
target/arm/cpu64.c +3 -3
index c11ab2da210..dcd0d698b30 100644
@@ -749,11 +749,11 @@ static void aarch64_a53_initfn(Object *obj)
     cpu->isar.reset_pmcr_el0 = 0x41033000;
     SET_IDREG(isar, CLIDR, 0x0a200023);
     /* 32KB L1 dcache */
-    cpu->ccsidr[0] = make_ccsidr(CCSIDR_FORMAT_LEGACY, 4, 64, 32 * KiB, 7);
+    SET_IDREG_DEMUX(isar, CCSIDR_EL1, 0, make_ccsidr(CCSIDR_FORMAT_LEGACY, 4, 64, 32 * KiB, 7));
     /* 32KB L1 icache */
-    cpu->ccsidr[1] = make_ccsidr(CCSIDR_FORMAT_LEGACY, 1, 64, 32 * KiB, 2);
+    SET_IDREG_DEMUX(isar, CCSIDR_EL1, 1, make_ccsidr(CCSIDR_FORMAT_LEGACY, 1, 64, 32 * KiB, 2));
     /* 1024KB L2 cache */
-    cpu->ccsidr[2] = make_ccsidr(CCSIDR_FORMAT_LEGACY, 16, 64, 1 * MiB, 7);
+    SET_IDREG_DEMUX(isar, CCSIDR_EL1, 2, make_ccsidr(CCSIDR_FORMAT_LEGACY, 16, 64, 1 * MiB, 7));
     set_dczid_bs(cpu, 4); /* 64 bytes */
     cpu->gic_num_lrs = 4;
     cpu->gic_vpribits = 5;
target/arm/helper.c +1 -1
index c3f607e6d6b..a608df12111 100644
@@ -925,7 +925,7 @@ static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
     uint32_t index = A32_BANKED_REG_GET(env, csselr,
                                         ri->secure & ARM_CP_SECSTATE_S);
 
-    return cpu->ccsidr[index];
+    return GET_IDREG_DEMUX(&cpu->isar, CCSIDR_EL1, index);
 }
 
 static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
target/arm/tcg/cpu32.c +13 -13
index 1999e502904..f95051458df 100644
@@ -278,9 +278,9 @@ static void cortex_a8_initfn(Object *obj)
     SET_IDREG(isar, ID_ISAR4, 0x00111142);
     cpu->isar.dbgdidr = 0x15141000;
     SET_IDREG(isar, CLIDR, (1 << 27) | (2 << 24) | 3);
-    cpu->ccsidr[0] = 0xe007e01a; /* 16k L1 dcache. */
-    cpu->ccsidr[1] = 0x2007e01a; /* 16k L1 icache. */
-    cpu->ccsidr[2] = 0xf0000000; /* No L2 icache. */
+    SET_IDREG_DEMUX(isar, CCSIDR_EL1, 0, 0xe007e01a); /* 16k L1 dcache. */
+    SET_IDREG_DEMUX(isar, CCSIDR_EL1, 1, 0x2007e01a); /* 16k L1 icache. */
+    SET_IDREG_DEMUX(isar, CCSIDR_EL1, 2, 0xf0000000); /* No L2 icache. */
     cpu->reset_auxcr = 2;
     cpu->isar.reset_pmcr_el0 = 0x41002000;
     define_arm_cp_regs(cpu, cortexa8_cp_reginfo);
@@ -356,8 +356,8 @@ static void cortex_a9_initfn(Object *obj)
     SET_IDREG(isar, ID_ISAR4, 0x00111142);
     cpu->isar.dbgdidr = 0x35141000;
     SET_IDREG(isar, CLIDR, (1 << 27) | (1 << 24) | 3);
-    cpu->ccsidr[0] = 0xe00fe019; /* 16k L1 dcache. */
-    cpu->ccsidr[1] = 0x200fe019; /* 16k L1 icache. */
+    SET_IDREG_DEMUX(isar, CCSIDR_EL1, 0, 0xe00fe019); /* 16k L1 dcache. */
+    SET_IDREG_DEMUX(isar, CCSIDR_EL1, 1, 0x200fe019); /* 16k L1 icache. */
     cpu->isar.reset_pmcr_el0 = 0x41093000;
     define_arm_cp_regs(cpu, cortexa9_cp_reginfo);
 }
@@ -430,9 +430,9 @@ static void cortex_a7_initfn(Object *obj)
     cpu->isar.dbgdevid = 0x01110f13;
     cpu->isar.dbgdevid1 = 0x1;
     SET_IDREG(isar, CLIDR, 0x0a200023);
-    cpu->ccsidr[0] = 0x701fe00a; /* 32K L1 dcache */
-    cpu->ccsidr[1] = 0x201fe00a; /* 32K L1 icache */
-    cpu->ccsidr[2] = 0x711fe07a; /* 4096K L2 unified cache */
+    SET_IDREG_DEMUX(isar, CCSIDR_EL1, 0, 0x701fe00a); /* 32K L1 dcache */
+    SET_IDREG_DEMUX(isar, CCSIDR_EL1, 1, 0x201fe00a); /* 32K L1 icache */
+    SET_IDREG_DEMUX(isar, CCSIDR_EL1, 2, 0x711fe07a); /* 4096K L2 unified cache */
     cpu->isar.reset_pmcr_el0 = 0x41072000;
     define_arm_cp_regs(cpu, cortexa15_cp_reginfo); /* Same as A15 */
 }
@@ -479,9 +479,9 @@ static void cortex_a15_initfn(Object *obj)
     cpu->isar.dbgdevid = 0x01110f13;
     cpu->isar.dbgdevid1 = 0x0;
     SET_IDREG(isar, CLIDR, 0x0a200023);
-    cpu->ccsidr[0] = 0x701fe00a; /* 32K L1 dcache */
-    cpu->ccsidr[1] = 0x201fe00a; /* 32K L1 icache */
-    cpu->ccsidr[2] = 0x711fe07a; /* 4096K L2 unified cache */
+    SET_IDREG_DEMUX(isar, CCSIDR_EL1, 0, 0x701fe00a); /* 32K L1 dcache */
+    SET_IDREG_DEMUX(isar, CCSIDR_EL1, 1, 0x201fe00a); /* 32K L1 icache */
+    SET_IDREG_DEMUX(isar, CCSIDR_EL1, 2, 0x711fe07a); /* 4096K L2 unified cache */
     cpu->isar.reset_pmcr_el0 = 0x410F3000;
     define_arm_cp_regs(cpu, cortexa15_cp_reginfo);
 }
@@ -671,8 +671,8 @@ static void cortex_r52_initfn(Object *obj)
     SET_IDREG(isar, ID_ISAR5, 0x00010001);
     cpu->isar.dbgdidr = 0x77168000;
     SET_IDREG(isar, CLIDR, (1 << 27) | (1 << 24) | 0x3);
-    cpu->ccsidr[0] = 0x700fe01a; /* 32KB L1 dcache */
-    cpu->ccsidr[1] = 0x201fe00a; /* 32KB L1 icache */
+    SET_IDREG_DEMUX(isar, CCSIDR_EL1, 0, 0x700fe01a); /* 32KB L1 dcache */
+    SET_IDREG_DEMUX(isar, CCSIDR_EL1, 1, 0x201fe00a); /* 32KB L1 icache */
 
     cpu->pmsav7_dregion = 16;
     cpu->pmsav8r_hdregion = 16;
target/arm/tcg/cpu64.c +34 -34

157 lines not on this page · raw message

Signature · 1 line
2.43.0
#18 lore raw

[PULL 09/37] arm: handle demuxed ID registers

In reply to Peter Maydell

From: Cornelia Huck <[email protected]>

For some registers, we do not have a single ID register, but actually an array of values (e.g. CCSIDR_EL1, where the actual value is determined by whatever CSSELR_EL1 points to.) If we want to avoid using a different way to handle registers like that for every instance, we should provide some kind of infrastructure. Therefore, add accessors {GET,SET}_IDREG_DEMUX that are similar to the accessors we already use for regular ID registers.

Message-id: [email protected]

---
 target/arm/cpu-sysregs.h |  9 +++++++++
 target/arm/cpu.h         | 16 ++++++++++++++++
 target/arm/cpu64.c       |  8 ++++++++
 3 files changed, 33 insertions(+)
target/arm/cpu-sysregs.h +9 -0
index 7877a3b06a8..a4b9621a7e1 100644
@@ -20,20 +20,29 @@
 
 #define DEF(NAME, OP0, OP1, CRN, CRM, OP2) NAME##_IDX,
 
+#define DEF_MUX(NAME, OP0, OP1, CRN, CRM, OP2, NUM)     \
+    NAME##_IDX,                                         \
+    NAME##_IDX_LAST = NAME##_IDX + NUM - 1,
+
 typedef enum ARMIDRegisterIdx {
 #include "cpu-sysregs.h.inc"
     NUM_ID_IDX,
 } ARMIDRegisterIdx;
 
 #undef DEF
+#undef DEF_MUX
 #define DEF(NAME, OP0, OP1, CRN, CRM, OP2) \
     SYS_##NAME = ENCODE_ID_REG(OP0, OP1, CRN, CRM, OP2),
 
+#define DEF_MUX(NAME, OP0, OP1, CRN, CRM, OP2, NUM)     \
+    DEF(NAME, OP0, OP1, CRN, CRM, OP2)
+
 typedef enum ARMSysRegs {
 #include "cpu-sysregs.h.inc"
 } ARMSysRegs;
 
 #undef DEF
+#undef DEF_MUX
 
 extern const uint32_t id_register_sysreg[NUM_ID_IDX];
 
target/arm/cpu.h +16 -0
index e3f931dba26..6d1bad1dfc6 100644
@@ -919,6 +919,22 @@ typedef struct {
         i_->idregs[REG ## _EL1_IDX];                                    \
     })
 
+#define SET_IDREG_DEMUX(ISAR, REG, INDEX, VALUE)                        \
+    ({                                                                  \
+        ARMISARegisters *i_ = (ISAR);                                   \
+        unsigned int idx_ = REG ## _IDX + INDEX;                        \
+        assert(idx_ <= REG ## _IDX_LAST);                               \
+        i_->idregs[idx_] = VALUE;                                       \
+    })
+
+#define GET_IDREG_DEMUX(ISAR, REG, INDEX)                               \
+    ({                                                                  \
+        ARMISARegisters *i_ = (ISAR);                                   \
+        unsigned int idx_ = REG ## _IDX + INDEX;                        \
+        assert(idx_ <= REG ## _IDX_LAST);                               \
+        i_->idregs[idx_];                                               \
+    })
+
 /**
  * ARMCPU:
  * @env: #CPUARMState
target/arm/cpu64.c +8 -0
index 4e8c4725308..c11ab2da210 100644
@@ -42,14 +42,21 @@
 #define DEF(NAME, OP0, OP1, CRN, CRM, OP2)      \
     [NAME##_IDX] = SYS_##NAME,
 
+#define DEF_MUX(NAME, OP0, OP1, CRN, CRM, OP2, NUM)     \
+    DEF(NAME, OP0, OP1, CRN, CRM, OP2)
+
 const uint32_t id_register_sysreg[NUM_ID_IDX] = {
 #include "cpu-sysregs.h.inc"
 };
 
 #undef DEF
+#undef DEF_MUX
 #define DEF(NAME, OP0, OP1, CRN, CRM, OP2) \
     case SYS_##NAME: return NAME##_IDX;
 
+#define DEF_MUX(NAME, OP0, OP1, CRN, CRM, OP2, NUM)     \
+    DEF(NAME, OP0, OP1, CRN, CRM, OP2)
+
 int get_sysreg_idx(ARMSysRegs sysreg)
 {
     switch (sysreg) {
@@ -59,6 +66,7 @@ int get_sysreg_idx(ARMSysRegs sysreg)
 }
 
 #undef DEF
+#undef DEF_MUX
 
 void aarch64_cpu_sve_finalize(ARMCPU *cpu, Error **errp)
 {
Signature · 1 line
2.43.0
#19 lore raw

[PULL 02/37] hw/char/stm32l4x5_usart.c: Support 16-bit writes

In reply to Peter Maydell

From: Jacob Whitaker Abrams <[email protected]>

Official STM32CubeL4 drivers use and require support for 16-bit writes to UART registers for proper function despite datasheet claims.

As the only 16 bit accesses we need to handle are ones to a register's base address for registers that have no defined fields in their top 16 bits, the emulation of 16-bit accesses provided by the memory system core is good enough, and we can implement this by updating .valid.min_access_size.

Message-id: [email protected]

[PMM: expanded commit message]
---
 hw/char/stm32l4x5_usart.c          |  7 ++++++-
 tests/qtest/stm32l4x5_usart-test.c | 22 +++++++++++++++++++++-
 2 files changed, 27 insertions(+), 2 deletions(-)
hw/char/stm32l4x5_usart.c +6 -1
index 0b02dde1f8c..c4ffc0d6ce5 100644
@@ -522,7 +522,12 @@ static const MemoryRegionOps stm32l4x5_usart_base_ops = {
     .endianness = DEVICE_NATIVE_ENDIAN,
     .valid = {
         .max_access_size = 4,
-        .min_access_size = 4,
+        /*
+         * Official STM32CubeL4 drivers use and require support for 16-bit
+         * writes to UART registers for proper function despite datasheet
+         * claims.
+         */
+        .min_access_size = 2,
         .unaligned = false
     },
     .impl = {
tests/qtest/stm32l4x5_usart-test.c +21 -1
index a72c5a685d1..3c6437e75d4 100644
@@ -332,6 +332,26 @@ static void test_ack(void)
     qtest_quit(qts);
 }
 
+static void test_16bit_access(void)
+{
+    QTestState *qts = qtest_init("-M b-l475e-iot01a");
+
+    /* GTPR is declared uint16_t in the official STM driver. */
+    qtest_writew(qts, USART1_BASE_ADDR + A_GTPR, 0xABCD);
+    uint16_t gtpr16 = qtest_readw(qts, USART1_BASE_ADDR + A_GTPR);
+    g_assert_cmpuint(gtpr16, ==, 0xABCD);
+
+    /* TDR is uint16_t and r/w, register is 9-bits */
+    qtest_writew(qts, USART1_BASE_ADDR + A_TDR, 0x01CD);
+    uint16_t tdr16 = qtest_readw(qts, USART1_BASE_ADDR + A_TDR);
+    g_assert_cmpuint(tdr16, ==, 0x01CD);
+
+    /* RDR is uint16_t and read only, register is 9-bits */
+    qtest_writew(qts, USART1_BASE_ADDR + A_RDR, 0x01FA);
+
+    qtest_quit(qts);
+}
+
 static void check_clock(QTestState *qts, const char *path, uint32_t rcc_reg,
                         uint32_t reg_offset)
 {
@@ -370,6 +390,6 @@ int main(int argc, char **argv)
     qtest_add_func("stm32l4x5/usart/send_str", test_send_str);
     qtest_add_func("stm32l4x5/usart/ack", test_ack);
     qtest_add_func("stm32l4x5/usart/clock_enable", test_clock_enable);
+    qtest_add_func("stm32l4x5/usart/16bit_access", test_16bit_access);
     return g_test_run();
 }
-
Signature · 1 line
2.43.0
#20 lore raw

[PULL 34/37] target/arm: Honor PSTATE.UINJ during translation

In reply to Peter Maydell

From: Richard Henderson <[email protected]>

Message-id: [email protected]

---
 target/arm/tcg/translate-a64.c |  2 +-
 target/arm/tcg/translate.c     | 10 +++++++---
 target/arm/tcg/translate.h     |  2 +-
 3 files changed, 9 insertions(+), 5 deletions(-)
target/arm/tcg/translate-a64.c +1 -1
index 7f858654c43..5d5b11e2da9 100644
@@ -11174,7 +11174,7 @@ static void aarch64_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
     s->fp_access_checked = 0;
     s->sve_access_checked = 0;
 
-    if (check_il(s)) {
+    if (check_il_uinj(s)) {
         return;
     }
 
target/arm/tcg/translate.c +7 -3
index fbacc3b7574..6983153ffc6 100644
@@ -1164,12 +1164,16 @@ void unallocated_encoding(DisasContext *s)
  * must be checked after reading the instruction.  They have more priority
  * than the AArch64 BTI exception.
  */
-bool check_il(DisasContext *s)
+bool check_il_uinj(DisasContext *s)
 {
     if (s->pstate_il) {
         gen_exception_insn(s, 0, EXCP_UDEF, syn_illegalstate());
         return true;
     }
+    if (s->pstate_uinj) {
+        unallocated_encoding(s);
+        return true;
+    }
     return false;
 }
 
@@ -6167,7 +6171,7 @@ static void disas_arm_insn(DisasContext *s, unsigned int insn)
         return;
     }
 
-    if (check_il(s)) {
+    if (check_il_uinj(s)) {
         return;
     }
 
@@ -6737,7 +6741,7 @@ static void thumb_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
     dc->base.pc_next = pc;
     dc->insn = insn;
 
-    if (check_il(dc)) {
+    if (check_il_uinj(dc)) {
         return;
     }
 
target/arm/tcg/translate.h +1 -1
index 7eba9b764ce..57e45ea8724 100644
@@ -382,7 +382,7 @@ void arm_jump_cc(DisasCompare *cmp, TCGLabel *label);
 void arm_gen_test_cc(int cc, TCGLabel *label);
 MemOp pow2_align(unsigned i);
 void unallocated_encoding(DisasContext *s);
-bool check_il(DisasContext *s);
+bool check_il_uinj(DisasContext *s);
 void gen_exception_internal(int excp);
 void gen_exception_insn_el(DisasContext *s, int64_t pc_diff, int excp,
                            uint32_t syn, uint32_t target_el);
Signature · 1 line
2.43.0
#21 lore raw

[PULL 27/37] target/arm: Add isar_feature_aa64_uinj

In reply to Peter Maydell

From: Richard Henderson <[email protected]>

Message-id: [email protected]

---
 target/arm/cpu-features.h | 6 ++++++
 1 file changed, 6 insertions(+)
target/arm/cpu-features.h +6 -0
index 06318d12f9f..a9eebb45564 100644
@@ -290,6 +290,7 @@ FIELD(ID_AA64PFR2, MTEPERM, 0, 4)
 FIELD(ID_AA64PFR2, MTESTOREONLY, 4, 4)
 FIELD(ID_AA64PFR2, MTEFAR, 8, 4)
 FIELD(ID_AA64PFR2, GCIE, 12, 4)
+FIELD(ID_AA64PFR2, UINJ, 16, 4)
 FIELD(ID_AA64PFR2, FPMR, 32, 4)
 
 FIELD(ID_AA64MMFR0, PARANGE, 0, 4)
@@ -1242,6 +1243,11 @@ static inline bool isar_feature_aa64_gcie(const ARMISARegisters *id)
     return FIELD_EX64_IDREG(id, ID_AA64PFR2, GCIE) != 0;
 }
 
+static inline bool isar_feature_aa64_uinj(const ARMISARegisters *id)
+{
+    return FIELD_EX64_IDREG(id, ID_AA64PFR2, UINJ) != 0;
+}
+
 static inline bool isar_feature_aa64_fpmr(const ARMISARegisters *id)
 {
     return FIELD_EX64_IDREG(id, ID_AA64PFR2, FPMR) != 0;
Signature · 1 line
2.43.0
#22 lore raw

[PULL 23/37] target/arm: Reorg rebuild_hflags_common*

In reply to Peter Maydell

From: Richard Henderson <[email protected]>

Invert the call ordering of rebuild_hflags_common_32 and rebuild_hflags_common vs the rest of the flags setting.

Message-id: [email protected]

---
 target/arm/tcg/hflags.c | 44 +++++++++++++++++++++--------------------
 1 file changed, 23 insertions(+), 21 deletions(-)
target/arm/tcg/hflags.c +23 -21
index c7739fc61b2..eb1aff91d5f 100644
@@ -87,9 +87,10 @@ bool access_secure_reg(CPUARMState *env)
 }
 
 static CPUARMTBFlags rebuild_hflags_common(CPUARMState *env, int fp_el,
-                                           ARMMMUIdx mmu_idx,
-                                           CPUARMTBFlags flags)
+                                           ARMMMUIdx mmu_idx)
 {
+    CPUARMTBFlags flags = {};
+
     DP_TBFLAG_ANY(flags, FPEXC_EL, fp_el);
     DP_TBFLAG_ANY(flags, MMUIDX, arm_to_core_mmu_idx(mmu_idx));
 
@@ -100,28 +101,27 @@ static CPUARMTBFlags rebuild_hflags_common(CPUARMState *env, int fp_el,
     return flags;
 }
 
-static CPUARMTBFlags rebuild_hflags_common_32(CPUARMState *env, int fp_el,
-                                              ARMMMUIdx mmu_idx,
-                                              CPUARMTBFlags flags)
+static void rebuild_hflags_common_32(CPUARMTBFlags *flags, CPUARMState *env)
 {
     bool sctlr_b = arm_sctlr_b(env);
 
     if (sctlr_b) {
-        DP_TBFLAG_A32(flags, SCTLR__B, 1);
+        DP_TBFLAG_A32(*flags, SCTLR__B, 1);
     }
     if (arm_cpu_data_is_big_endian_a32(env, sctlr_b)) {
-        DP_TBFLAG_ANY(flags, BE_DATA, 1);
+        DP_TBFLAG_ANY(*flags, BE_DATA, 1);
     }
-    DP_TBFLAG_A32(flags, NS, !access_secure_reg(env));
-
-    return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
+    DP_TBFLAG_A32(*flags, NS, !access_secure_reg(env));
 }
 
 static CPUARMTBFlags rebuild_hflags_m32(CPUARMState *env, int fp_el,
                                         ARMMMUIdx mmu_idx)
 {
-    CPUARMTBFlags flags = {};
-    uint32_t ccr = env->v7m.ccr[env->v7m.secure];
+    CPUARMTBFlags flags = rebuild_hflags_common(env, fp_el, mmu_idx);
+    uint32_t ccr;
+
+    rebuild_hflags_common_32(&flags, env);
+    ccr = env->v7m.ccr[env->v7m.secure];
 
     /* Without HaveMainExt, CCR.UNALIGN_TRP is RES1. */
     if (ccr & R_V7M_CCR_UNALIGN_TRP_MASK) {
@@ -146,8 +146,7 @@ static CPUARMTBFlags rebuild_hflags_m32(CPUARMState *env, int fp_el,
     if (arm_feature(env, ARM_FEATURE_M_SECURITY) && env->v7m.secure) {
         DP_TBFLAG_M32(flags, SECURE, 1);
     }
-
-    return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
+    return flags;
 }
 
 /* This corresponds to the ARM pseudocode function IsFullA64Enabled(). */
@@ -277,10 +276,14 @@ static bool arm_d32dis(CPUARMState *env, int cur_el)
 static CPUARMTBFlags rebuild_hflags_a32(CPUARMState *env, int fp_el,
                                         ARMMMUIdx mmu_idx)
 {
-    CPUARMTBFlags flags = {};
-    int el = arm_current_el(env);
-    uint64_t sctlr = arm_sctlr(env, el);
+    CPUARMTBFlags flags = rebuild_hflags_common(env, fp_el, mmu_idx);
+    int el;
+    uint64_t sctlr;
 
+    rebuild_hflags_common_32(&flags, env);
+
+    el = arm_current_el(env);
+    sctlr = arm_sctlr(env, el);
     if (aprofile_require_alignment(env, el, sctlr)) {
         DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
     }
@@ -323,7 +326,7 @@ static CPUARMTBFlags rebuild_hflags_a32(CPUARMState *env, int fp_el,
 
     DP_TBFLAG_A32(flags, D32DIS, arm_d32dis(env, el));
 
-    return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
+    return flags;
 }
 
 /*
@@ -392,7 +395,7 @@ static int fpmr_exception_el(CPUARMState *env, int el)
 static CPUARMTBFlags rebuild_hflags_a64(CPUARMState *env, int el, int fp_el,
                                         ARMMMUIdx mmu_idx)
 {
-    CPUARMTBFlags flags = {};
+    CPUARMTBFlags flags = rebuild_hflags_common(env, fp_el, mmu_idx);
     ARMMMUIdx stage1 = stage_1_mmu_idx(mmu_idx);
     uint64_t tcr = regime_tcr(env, mmu_idx);
     uint64_t hcr = arm_hcr_el2_eff(env);
@@ -672,8 +675,7 @@ static CPUARMTBFlags rebuild_hflags_a64(CPUARMState *env, int el, int fp_el,
     if (cpu_isar_feature(aa64_fpmr, env_archcpu(env))) {
         DP_TBFLAG_A64(flags, FPMR_EL, fpmr_exception_el(env, el));
     }
-
-    return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
+    return flags;
 }
 
 static CPUARMTBFlags rebuild_hflags_internal(CPUARMState *env)
Signature · 1 line
2.43.0
#23 lore raw

[PULL 30/37] target/arm: Widen cpsr_{read, write}_for_spsr_elx to uint64_t

In reply to Peter Maydell

From: Richard Henderson <[email protected]>

We will shortly need to handle bit 36: PSTATE.UINJ.

Message-id: [email protected]

---
 target/arm/helper.c    | 6 +++---
 target/arm/internals.h | 4 ++--
 2 files changed, 5 insertions(+), 5 deletions(-)
target/arm/helper.c +3 -3
index 76956cfd97b..f843f281fcd 100644
@@ -9514,9 +9514,9 @@ static int aarch64_regnum(CPUARMState *env, int aarch32_reg)
     }
 }
 
-uint32_t cpsr_read_for_spsr_elx(CPUARMState *env)
+uint64_t cpsr_read_for_spsr_elx(CPUARMState *env)
 {
-    uint32_t ret = cpsr_read(env);
+    uint64_t ret = cpsr_read(env);
 
     /* Move DIT to the correct location for SPSR_ELx */
     if (ret & CPSR_DIT) {
@@ -9529,7 +9529,7 @@ uint32_t cpsr_read_for_spsr_elx(CPUARMState *env)
     return ret;
 }
 
-void cpsr_write_from_spsr_elx(CPUARMState *env, uint32_t val)
+void cpsr_write_from_spsr_elx(CPUARMState *env, uint64_t val)
 {
     uint32_t mask;
 
target/arm/internals.h +2 -2
index 1efda543270..843e12487ec 100644
@@ -2016,8 +2016,8 @@ void vfp_clear_float_status_exc_flags(CPUARMState *env);
  */
 void vfp_set_fpcr_to_host(CPUARMState *env, uint32_t val, uint32_t mask);
 bool arm_pan_enabled(CPUARMState *env);
-uint32_t cpsr_read_for_spsr_elx(CPUARMState *env);
-void cpsr_write_from_spsr_elx(CPUARMState *env, uint32_t val);
+uint64_t cpsr_read_for_spsr_elx(CPUARMState *env);
+void cpsr_write_from_spsr_elx(CPUARMState *env, uint64_t val);
 
 /* Compare uint64_t for qsort and bsearch. */
 int compare_u64(const void *a, const void *b);
Signature · 1 line
2.43.0
#24 lore raw

[PULL 18/37] target/arm: Don't set ARM_FEATURE_VAPA for v7 PMSA

In reply to Peter Maydell

The features VAPA (which gets you the ATS instructions and the PAR register) and LPAE (which gets you a 64-bit PAR and the long-format page table descriptors). were originally defined in v7 as being A-profile only. For v8, the ATS insns and PAR are added to R-profile; although there are no page table descriptors to be long or short, the v8R PAR format is always long-descriptor style, so it makes more sense to set ARM_FEATURE_LPAE than not (and matches how we already return true from regime_using_lpae_format() for v8R PMSA).

Our "feature X implies feature Y" code was only taking account of A-profile; adjust it so that for PMSA:
* V8 implies V7VE, LPAE, V7, and VAPA (no change from currently)
* V7 does not imply VAPA (new)

(v8R wants V7VE because this implies things like the existence of ERET and the PMOVSSET insns.)

This will stop us incorrectly exposing the PAR and ATS insns for v7R CPUs (and it will be a migration break on those CPUs, which is fine as none of them are usable with versioned machine types).

Message-id: [email protected]

---
 target/arm/cpu.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
target/arm/cpu.c +5 -1
index be2316bec1c..10ac6c98318 100644
@@ -1507,7 +1507,11 @@ static void arm_cpu_propagate_feature_implications(ARMCPU *cpu)
         set_feature(env, ARM_FEATURE_V7);
     }
     if (arm_feature(env, ARM_FEATURE_V7)) {
-        set_feature(env, ARM_FEATURE_VAPA);
+        /* VAPA appears in v7A, but not in R profile until v8R */
+        if (arm_feature(env, ARM_FEATURE_V8) ||
+            !arm_feature(env, ARM_FEATURE_PMSA)) {
+            set_feature(env, ARM_FEATURE_VAPA);
+        }
         set_feature(env, ARM_FEATURE_THUMB2);
         set_feature(env, ARM_FEATURE_MPIDR);
         if (!arm_feature(env, ARM_FEATURE_M)) {
Signature · 1 line
2.43.0
#25 lore raw

[PULL 22/37] target/arm: Parenthesize tbflags arguments in TBFLAG macros

In reply to Peter Maydell

From: Richard Henderson <[email protected]>

Message-id: [email protected]

---
 target/arm/cpu.h | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
target/arm/cpu.h +10 -10
index 418fe78809f..4785c9c57af 100644
@@ -2606,21 +2606,21 @@ FIELD(TBFLAG_A64, MTX, 49, 2)
  * word either is or might be 32 bits only.
  */
 #define DP_TBFLAG_ANY(DST, WHICH, VAL) \
-    (DST.flags = FIELD_DP32(DST.flags, TBFLAG_ANY, WHICH, VAL))
+    ((DST).flags = FIELD_DP32((DST).flags, TBFLAG_ANY, WHICH, VAL))
 #define DP_TBFLAG_A64(DST, WHICH, VAL) \
-    (DST.flags2 = FIELD_DP64(DST.flags2, TBFLAG_A64, WHICH, VAL))
+    ((DST).flags2 = FIELD_DP64((DST).flags2, TBFLAG_A64, WHICH, VAL))
 #define DP_TBFLAG_A32(DST, WHICH, VAL) \
-    (DST.flags2 = FIELD_DP32(DST.flags2, TBFLAG_A32, WHICH, VAL))
+    ((DST).flags2 = FIELD_DP32((DST).flags2, TBFLAG_A32, WHICH, VAL))
 #define DP_TBFLAG_M32(DST, WHICH, VAL) \
-    (DST.flags2 = FIELD_DP32(DST.flags2, TBFLAG_M32, WHICH, VAL))
+    ((DST).flags2 = FIELD_DP32((DST).flags2, TBFLAG_M32, WHICH, VAL))
 #define DP_TBFLAG_AM32(DST, WHICH, VAL) \
-    (DST.flags2 = FIELD_DP32(DST.flags2, TBFLAG_AM32, WHICH, VAL))
+    ((DST).flags2 = FIELD_DP32((DST).flags2, TBFLAG_AM32, WHICH, VAL))
 
-#define EX_TBFLAG_ANY(IN, WHICH)   FIELD_EX32(IN.flags, TBFLAG_ANY, WHICH)
-#define EX_TBFLAG_A64(IN, WHICH)   FIELD_EX64(IN.flags2, TBFLAG_A64, WHICH)
-#define EX_TBFLAG_A32(IN, WHICH)   FIELD_EX32(IN.flags2, TBFLAG_A32, WHICH)
-#define EX_TBFLAG_M32(IN, WHICH)   FIELD_EX32(IN.flags2, TBFLAG_M32, WHICH)
-#define EX_TBFLAG_AM32(IN, WHICH)  FIELD_EX32(IN.flags2, TBFLAG_AM32, WHICH)
+#define EX_TBFLAG_ANY(IN, WHICH)   FIELD_EX32((IN).flags, TBFLAG_ANY, WHICH)
+#define EX_TBFLAG_A64(IN, WHICH)   FIELD_EX64((IN).flags2, TBFLAG_A64, WHICH)
+#define EX_TBFLAG_A32(IN, WHICH)   FIELD_EX32((IN).flags2, TBFLAG_A32, WHICH)
+#define EX_TBFLAG_M32(IN, WHICH)   FIELD_EX32((IN).flags2, TBFLAG_M32, WHICH)
+#define EX_TBFLAG_AM32(IN, WHICH)  FIELD_EX32((IN).flags2, TBFLAG_AM32, WHICH)
 
 /**
  * sve_vq
Signature · 1 line
2.43.0
#26 lore raw

[PULL 21/37] target/arm: Fix PMSAv8 shareability decode for R-profile MPU regions

In reply to Peter Maydell

From: Jean-Francois Bortolotti <[email protected]>

In Armv8-R AArch32 the shareability field of an MPU region lives in PRBAR, not PRLAR:

PRBAR: [31:6] BASE [5] RES0 [4:3] SH [2:1] AP [0] XN
PRLAR: [31:6] LIMIT [5:4] RES0 [3:1] AttrIndx [0] EN

(Arm Cortex-R52 TRM 100026_0103_00_en: Tables 3-80 and 3-83 for HPRBAR/HPRLAR, Tables 3-124 and 3-127 for PRBAR/PRLAR.)

Without this fix, address translation instructions like ATS1HR will return wrong value for PAR.SH.

Fixes fca45e3467f7 ("target/arm: Add PMSAv8r functionality")

Message-id: [email protected]

---
 target/arm/ptw.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/arm/ptw.c +1 -1
index f537502d3b1..de0435a58b8 100644
@@ -3092,7 +3092,7 @@ bool pmsav8_mpu_lookup(CPUARMState *env, uint32_t address,
         if (!arm_feature(env, ARM_FEATURE_M)) {
             uint8_t attrindx = extract32(matched_rlar, 1, 3);
             uint64_t mair = env->cp15.mair_el[regime_el(mmu_idx)];
-            uint8_t sh = extract32(matched_rlar, 3, 2);
+            uint8_t sh = extract32(matched_rbar, 3, 2);
 
             if (regime_sctlr(env, mmu_idx) & SCTLR_WXN &&
                 result->f.prot & PAGE_WRITE && mmu_idx != ARMMMUIdx_Stage2) {
Signature · 1 line
2.43.0
#27 lore raw

[PULL 32/37] target/arm: Add PSTATE.UINJ to tbflags

In reply to Peter Maydell

From: Richard Henderson <[email protected]>

Message-id: [email protected]

---
 target/arm/cpu.h               | 1 +
 target/arm/tcg/hflags.c        | 3 +++
 target/arm/tcg/translate-a64.c | 1 +
 target/arm/tcg/translate.c     | 1 +
 target/arm/tcg/translate.h     | 2 ++
 5 files changed, 8 insertions(+)
target/arm/cpu.h +1 -0
index 78bf4576a58..06ebed4a8fd 100644
@@ -2506,6 +2506,7 @@ FIELD(TBFLAG_ANY, ALIGN_MEM, 10, 1)
 FIELD(TBFLAG_ANY, PSTATE__IL, 11, 1)
 FIELD(TBFLAG_ANY, FGT_ACTIVE, 12, 1)
 FIELD(TBFLAG_ANY, FGT_SVC, 13, 1)
+FIELD(TBFLAG_ANY, PSTATE__UINJ, 14, 1)
 
 /*
  * Bit usage when in AArch32 state, both A- and M-profile.
target/arm/tcg/hflags.c +3 -0
index cf7d49be908..c0b47f484e6 100644
@@ -121,6 +121,9 @@ static void rebuild_hflags_common_aprofile(CPUARMTBFlags *flags,
     if (il) {
         DP_TBFLAG_ANY(*flags, PSTATE__IL, 1);
     }
+    if (env->pstate & PSTATE_UINJ) {
+        DP_TBFLAG_ANY(*flags, PSTATE__UINJ, 1);
+    }
     if (aprofile_require_alignment(env, el, sctlr)) {
         DP_TBFLAG_ANY(*flags, ALIGN_MEM, 1);
     }
target/arm/tcg/translate-a64.c +1 -0
index c60f4c4103d..b3438235828 100644
@@ -11028,6 +11028,7 @@ static void aarch64_tr_init_disas_context(DisasContextBase *dcbase,
     dc->fp_excp_el = EX_TBFLAG_ANY(tb_flags, FPEXC_EL);
     dc->align_mem = EX_TBFLAG_ANY(tb_flags, ALIGN_MEM);
     dc->pstate_il = EX_TBFLAG_ANY(tb_flags, PSTATE__IL);
+    dc->pstate_uinj = EX_TBFLAG_ANY(tb_flags, PSTATE__UINJ);
     dc->fgt_active = EX_TBFLAG_ANY(tb_flags, FGT_ACTIVE);
     dc->fgt_svc = EX_TBFLAG_ANY(tb_flags, FGT_SVC);
     dc->trap_eret = EX_TBFLAG_A64(tb_flags, TRAP_ERET);
target/arm/tcg/translate.c +1 -0
index 8c56348a665..f32200cc47e 100644
@@ -6418,6 +6418,7 @@ static void arm_tr_init_disas_context(DisasContextBase *dcbase, CPUState *cs)
     dc->fp_excp_el = EX_TBFLAG_ANY(tb_flags, FPEXC_EL);
     dc->align_mem = EX_TBFLAG_ANY(tb_flags, ALIGN_MEM);
     dc->pstate_il = EX_TBFLAG_ANY(tb_flags, PSTATE__IL);
+    dc->pstate_uinj = EX_TBFLAG_ANY(tb_flags, PSTATE__UINJ);
     dc->fgt_active = EX_TBFLAG_ANY(tb_flags, FGT_ACTIVE);
     dc->fgt_svc = EX_TBFLAG_ANY(tb_flags, FGT_SVC);
 
target/arm/tcg/translate.h +2 -0
index 31f52a34a68..f633aa265f0 100644
@@ -155,6 +155,8 @@ typedef struct DisasContext {
     bool align_mem;
     /* True if PSTATE.IL is set */
     bool pstate_il;
+    /* True if PSTATE.UINJ is set */
+    bool pstate_uinj;
     /* True if PSTATE.SM is set. */
     bool pstate_sm;
     /* True if PSTATE.ZA is set. */
Signature · 1 line
2.43.0
#28 lore raw

[PULL 33/37] target/arm: Split out check_il

In reply to Peter Maydell

From: Richard Henderson <[email protected]>

Message-id: [email protected]

---
 target/arm/tcg/translate-a64.c |  7 +------
 target/arm/tcg/translate.c     | 28 ++++++++++++++++------------
 target/arm/tcg/translate.h     |  1 +
 3 files changed, 18 insertions(+), 18 deletions(-)
target/arm/tcg/translate-a64.c +1 -6
index b3438235828..7f858654c43 100644
@@ -11174,12 +11174,7 @@ static void aarch64_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
     s->fp_access_checked = 0;
     s->sve_access_checked = 0;
 
-    if (s->pstate_il) {
-        /*
-         * Illegal execution state. This has priority over BTI
-         * exceptions, but comes after instruction abort exceptions.
-         */
-        gen_exception_insn(s, 0, EXCP_UDEF, syn_illegalstate());
+    if (check_il(s)) {
         return;
     }
 
target/arm/tcg/translate.c +16 -12
index f32200cc47e..fbacc3b7574 100644
@@ -1159,6 +1159,20 @@ void unallocated_encoding(DisasContext *s)
     gen_exception_insn(s, 0, EXCP_UDEF, syn_uncategorized());
 }
 
+/*
+ * These conditions have less priority than instruction abort, and thus
+ * must be checked after reading the instruction.  They have more priority
+ * than the AArch64 BTI exception.
+ */
+bool check_il(DisasContext *s)
+{
+    if (s->pstate_il) {
+        gen_exception_insn(s, 0, EXCP_UDEF, syn_illegalstate());
+        return true;
+    }
+    return false;
+}
+
 /* Force a TB lookup after an instruction that changes the CPU state.  */
 void gen_lookup_tb(DisasContext *s)
 {
@@ -6153,12 +6167,7 @@ static void disas_arm_insn(DisasContext *s, unsigned int insn)
         return;
     }
 
-    if (s->pstate_il) {
-        /*
-         * Illegal execution state. This has priority over BTI
-         * exceptions, but comes after instruction abort exceptions.
-         */
-        gen_exception_insn(s, 0, EXCP_UDEF, syn_illegalstate());
+    if (check_il(s)) {
         return;
     }
 
@@ -6728,12 +6737,7 @@ static void thumb_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
     dc->base.pc_next = pc;
     dc->insn = insn;
 
-    if (dc->pstate_il) {
-        /*
-         * Illegal execution state. This has priority over BTI
-         * exceptions, but comes after instruction abort exceptions.
-         */
-        gen_exception_insn(dc, 0, EXCP_UDEF, syn_illegalstate());
+    if (check_il(dc)) {
         return;
     }
 
target/arm/tcg/translate.h +1 -0
index f633aa265f0..7eba9b764ce 100644
@@ -382,6 +382,7 @@ void arm_jump_cc(DisasCompare *cmp, TCGLabel *label);
 void arm_gen_test_cc(int cc, TCGLabel *label);
 MemOp pow2_align(unsigned i);
 void unallocated_encoding(DisasContext *s);
+bool check_il(DisasContext *s);
 void gen_exception_internal(int excp);
 void gen_exception_insn_el(DisasContext *s, int64_t pc_diff, int excp,
                            uint32_t syn, uint32_t target_el);
Signature · 1 line
2.43.0
#29 lore raw

[PULL 24/37] target/arm: Split out rebuild_hflags_common_aprofile

In reply to Peter Maydell

From: Richard Henderson <[email protected]>

Alignment, FGT, and IL are common to a32 and a64, though the latter comes from different places.

Message-id: [email protected]

---
 target/arm/tcg/hflags.c | 65 +++++++++++++++++++----------------------
 1 file changed, 30 insertions(+), 35 deletions(-)
target/arm/tcg/hflags.c +30 -35
index eb1aff91d5f..8da02ae1ecf 100644
@@ -114,6 +114,24 @@ static void rebuild_hflags_common_32(CPUARMTBFlags *flags, CPUARMState *env)
     DP_TBFLAG_A32(*flags, NS, !access_secure_reg(env));
 }
 
+static void rebuild_hflags_common_aprofile(CPUARMTBFlags *flags,
+                                           CPUARMState *env, int el,
+                                           uint64_t sctlr, bool il)
+{
+    if (il) {
+        DP_TBFLAG_ANY(*flags, PSTATE__IL, 1);
+    }
+    if (aprofile_require_alignment(env, el, sctlr)) {
+        DP_TBFLAG_ANY(*flags, ALIGN_MEM, 1);
+    }
+    if (arm_fgt_active(env, el)) {
+        DP_TBFLAG_ANY(*flags, FGT_ACTIVE, 1);
+        if (fgt_svc(env, el)) {
+            DP_TBFLAG_ANY(*flags, FGT_SVC, 1);
+        }
+    }
+}
+
 static CPUARMTBFlags rebuild_hflags_m32(CPUARMState *env, int fp_el,
                                         ARMMMUIdx mmu_idx)
 {
@@ -284,9 +302,8 @@ static CPUARMTBFlags rebuild_hflags_a32(CPUARMState *env, int fp_el,
 
     el = arm_current_el(env);
     sctlr = arm_sctlr(env, el);
-    if (aprofile_require_alignment(env, el, sctlr)) {
-        DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
-    }
+    rebuild_hflags_common_aprofile(&flags, env, el, sctlr,
+                                   env->uncached_cpsr & CPSR_IL);
 
     if (arm_el_is_aa64(env, 1)) {
         DP_TBFLAG_A32(flags, VFPEN, 1);
@@ -297,17 +314,6 @@ static CPUARMTBFlags rebuild_hflags_a32(CPUARMState *env, int fp_el,
         DP_TBFLAG_A32(flags, HSTR_ACTIVE, 1);
     }
 
-    if (arm_fgt_active(env, el)) {
-        DP_TBFLAG_ANY(flags, FGT_ACTIVE, 1);
-        if (fgt_svc(env, el)) {
-            DP_TBFLAG_ANY(flags, FGT_SVC, 1);
-        }
-    }
-
-    if (env->uncached_cpsr & CPSR_IL) {
-        DP_TBFLAG_ANY(flags, PSTATE__IL, 1);
-    }
-
     /*
      * The SME exception we are testing for is raised via
      * AArch64.CheckFPAdvSIMDEnabled(), as called from
@@ -397,14 +403,17 @@ static CPUARMTBFlags rebuild_hflags_a64(CPUARMState *env, int el, int fp_el,
 {
     CPUARMTBFlags flags = rebuild_hflags_common(env, fp_el, mmu_idx);
     ARMMMUIdx stage1 = stage_1_mmu_idx(mmu_idx);
-    uint64_t tcr = regime_tcr(env, mmu_idx);
-    uint64_t hcr = arm_hcr_el2_eff(env);
-    uint64_t sctlr;
+    uint64_t sctlr = regime_sctlr(env, stage1);
+    uint64_t tcr, hcr;
     int tbii, tbid, mtx;
 
+    rebuild_hflags_common_aprofile(&flags, env, el, sctlr,
+                                   env->pstate & PSTATE_IL);
+
     DP_TBFLAG_ANY(flags, AARCH64_STATE, 1);
 
     /* Get control bits for tagged addresses.  */
+    tcr = regime_tcr(env, mmu_idx);
     tbid = aa64_va_parameter_tbi(tcr, mmu_idx);
     tbii = tbid & ~aa64_va_parameter_tbid(tcr, mmu_idx);
     mtx = cpu_isar_feature(aa64_mte_mtx, env_archcpu(env)) ?
@@ -415,6 +424,7 @@ static CPUARMTBFlags rebuild_hflags_a64(CPUARMState *env, int el, int fp_el,
     DP_TBFLAG_A64(flags, TBID, tbid);
 
     /* E2H is used by both VHE and NV2. */
+    hcr = arm_hcr_el2_eff(env);
     if (hcr & HCR_E2H) {
         DP_TBFLAG_A64(flags, E2H, 1);
     }
@@ -465,12 +475,6 @@ static CPUARMTBFlags rebuild_hflags_a64(CPUARMState *env, int el, int fp_el,
         }
     }
 
-    sctlr = regime_sctlr(env, stage1);
-
-    if (aprofile_require_alignment(env, el, sctlr)) {
-        DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
-    }
-
     if (arm_cpu_data_is_big_endian_a64(el, sctlr)) {
         DP_TBFLAG_ANY(flags, BE_DATA, 1);
     }
@@ -525,18 +529,9 @@ static CPUARMTBFlags rebuild_hflags_a64(CPUARMState *env, int el, int fp_el,
         }
     }
 
-    if (env->pstate & PSTATE_IL) {
-        DP_TBFLAG_ANY(flags, PSTATE__IL, 1);
-    }
-
-    if (arm_fgt_active(env, el)) {
-        DP_TBFLAG_ANY(flags, FGT_ACTIVE, 1);
-        if (FIELD_EX64(env->cp15.fgt_exec[FGTREG_HFGITR], HFGITR_EL2, ERET)) {
-            DP_TBFLAG_A64(flags, TRAP_ERET, 1);
-        }
-        if (fgt_svc(env, el)) {
-            DP_TBFLAG_ANY(flags, FGT_SVC, 1);
-        }
+    if (EX_TBFLAG_ANY(flags, FGT_ACTIVE) &&
+        FIELD_EX64(env->cp15.fgt_exec[FGTREG_HFGITR], HFGITR_EL2, ERET)) {
+        DP_TBFLAG_A64(flags, TRAP_ERET, 1);
     }
 
     /*
Signature · 1 line
2.43.0
#30 lore raw

[PULL 31/37] target/arm: Read and write PSTATE.UINJ

In reply to Peter Maydell

From: Richard Henderson <[email protected]>

With FEAT_UINJ, allow PSTATE.UINJ to be set from aa64 and aa32 modes. Read the value back for SPSR_ELx from aa32 mode.

Message-id: [email protected]

---
 target/arm/helper.c    | 13 +++++++++++--
 target/arm/internals.h |  3 +++
 2 files changed, 14 insertions(+), 2 deletions(-)
target/arm/helper.c +11 -2
index f843f281fcd..9a90ab68071 100644
@@ -9523,8 +9523,9 @@ uint64_t cpsr_read_for_spsr_elx(CPUARMState *env)
         ret &= ~CPSR_DIT;
         ret |= PSTATE_DIT;
     }
-    /* Merge PSTATE.SS into SPSR_ELx */
-    ret |= env->pstate & PSTATE_SS;
+
+    /* Merge PSTATE.{SS,UINJ} into SPSR_ELx */
+    ret |= env->pstate & (PSTATE_SS | PSTATE_UINJ);
 
     return ret;
 }
@@ -9533,6 +9534,14 @@ void cpsr_write_from_spsr_elx(CPUARMState *env, uint64_t val)
 {
     uint32_t mask;
 
+    /* Save SPSR_ELx.UINJ into PSTATE. */
+    if (unlikely(val & PSTATE_UINJ)
+        && arm_feature(env, ARM_FEATURE_AARCH64)
+        && cpu_isar_feature(aa64_uinj, env_archcpu(env))) {
+        env->pstate |= PSTATE_UINJ;
+        val &= ~PSTATE_UINJ;
+    }
+
     /* Save SPSR_ELx.SS into PSTATE. */
     env->pstate = (env->pstate & ~PSTATE_SS) | (val & PSTATE_SS);
     val &= ~PSTATE_SS;
target/arm/internals.h +3 -0
index 843e12487ec..c41fa5225c7 100644
@@ -1392,6 +1392,9 @@ static inline uint32_t aarch64_pstate_valid_mask(const ARMISARegisters *id)
     if (isar_feature_aa64_nmi(id)) {
         valid |= PSTATE_ALLINT;
     }
+    if (isar_feature_aa64_uinj(id)) {
+        valid |= PSTATE_UINJ;
+    }
 
     return valid;
 }
Signature · 1 line
2.43.0
#31 lore raw

[PULL 35/37] target/arm: Enable FEAT_UINJ for -cpu max-v9

In reply to Peter Maydell

From: Richard Henderson <[email protected]>

Message-id: [email protected]

---
 docs/system/arm/emulation.rst | 1 +
 target/arm/tcg/cpu64.c        | 1 +
 2 files changed, 2 insertions(+)
docs/system/arm/emulation.rst +1 -0
index 34b63f9867f..3bd5c42a664 100644
@@ -223,6 +223,7 @@ the following architecture extensions:
 - FEAT_TTL (Translation Table Level)
 - FEAT_TTST (Small translation tables)
 - FEAT_UAO (Unprivileged Access Override control)
+- FEAT_UINJ (Injection of Undefined Instruction exceptions)
 - FEAT_VHE (Virtualization Host Extensions)
 - FEAT_VMID16 (16-bit VMID)
 - FEAT_WFxT (WFE and WFI instructions with timeout)
target/arm/tcg/cpu64.c +1 -0
index e156baf35d2..59692dddcfa 100644
@@ -1457,6 +1457,7 @@ void aarch64_max_v9_tcg_initfn(Object *obj)
     SET_IDREG(isar, ID_AA64PFR1, t);
 
     t = GET_IDREG(isar, ID_AA64PFR2);
+    t = FIELD_DP64(t, ID_AA64PFR2, UINJ, 1);      /* v9.0: FEAT_UINJ */
     t = FIELD_DP64(t, ID_AA64PFR2, FPMR, 1);      /* v9.2: FEAT_FPMR */
     SET_IDREG(isar, ID_AA64PFR2, t);
 
Signature · 1 line
2.43.0
#32 lore raw

[PULL 25/37] target/arm: Sort BE_DATA toward the top of rebuild_hflags_a64

In reply to Peter Maydell

From: Richard Henderson <[email protected]>

This is the last of the "ANY" class of flags. Sort to the top with the other "ANY" flags.

Message-id: [email protected]

---
 target/arm/tcg/hflags.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
target/arm/tcg/hflags.c +4 -4
index 8da02ae1ecf..cf7d49be908 100644
@@ -412,6 +412,10 @@ static CPUARMTBFlags rebuild_hflags_a64(CPUARMState *env, int el, int fp_el,
 
     DP_TBFLAG_ANY(flags, AARCH64_STATE, 1);
 
+    if (arm_cpu_data_is_big_endian_a64(el, sctlr)) {
+        DP_TBFLAG_ANY(flags, BE_DATA, 1);
+    }
+
     /* Get control bits for tagged addresses.  */
     tcr = regime_tcr(env, mmu_idx);
     tbid = aa64_va_parameter_tbi(tcr, mmu_idx);
@@ -475,10 +479,6 @@ static CPUARMTBFlags rebuild_hflags_a64(CPUARMState *env, int el, int fp_el,
         }
     }
 
-    if (arm_cpu_data_is_big_endian_a64(el, sctlr)) {
-        DP_TBFLAG_ANY(flags, BE_DATA, 1);
-    }
-
     if (cpu_isar_feature(aa64_pauth, env_archcpu(env))) {
         /*
          * In order to save space in flags, we record only whether
Signature · 1 line
2.43.0
#33 lore raw

[PULL 12/37] target/arm: model Cortex-M0+ privilege extension

In reply to Peter Maydell

From: Gilles Grimaud <[email protected]>

The Armv6-M Unprivileged/Privileged Extension is optional. Cortex-M0 does not implement it, while Cortex-M0+ may implement it. The RP2040 Cortex-M0+ configuration provides the extension.

Add an ARM feature bit that represents the optional privileged/unprivileged extension. Make ARM_FEATURE_M_MAIN imply ARM_FEATURE_M_UNPRIV (handled in arm_cpu_propagate_feature_implications) so that privileged writes to CONTROL.nPRIV are allowed when the Main Extension is present (or when the optional privilege extension is present without the Main Extension).

Update the microbit Cortex-M0 test to verify that writes to nPRIV are ignored.

---
 target/arm/cpu.c                           |  4 ++
 target/arm/cpu.h                           |  1 +
 target/arm/tcg/m_helper.c                  |  2 +-
 tests/tcg/arm/system/meson.build           |  6 ++
 tests/tcg/arm/system/test-armv6m-control.S | 66 ++++++++++++++++++++++
 5 files changed, 78 insertions(+), 1 deletion(-)
 create mode 100644 tests/tcg/arm/system/test-armv6m-control.S
target/arm/cpu.c +4 -0
index 090470da40a..be2316bec1c 100644
@@ -1467,6 +1467,10 @@ static void arm_cpu_propagate_feature_implications(ARMCPU *cpu)
         set_feature(env, ARM_FEATURE_PMSA);
     }
 
+    if (arm_feature(env, ARM_FEATURE_M_MAIN)) {
+        set_feature(env, ARM_FEATURE_M_UNPRIV);
+    }
+
     if (arm_feature(env, ARM_FEATURE_V8)) {
         if (arm_feature(env, ARM_FEATURE_M)) {
             set_feature(env, ARM_FEATURE_V7);
target/arm/cpu.h +1 -0
index 5c8ea5a7ed2..f07cadb45c7 100644
@@ -2184,6 +2184,7 @@ enum arm_features {
     ARM_FEATURE_VBAR, /* has cp15 VBAR */
     ARM_FEATURE_M_SECURITY, /* M profile Security Extension */
     ARM_FEATURE_M_MAIN, /* M profile Main Extension */
+    ARM_FEATURE_M_UNPRIV, /* M profile Unprivileged/Privileged Extension */
     ARM_FEATURE_V8_1M, /* M profile extras only in v8.1M and later */
     /*
      * ARM_FEATURE_BACKCOMPAT_CNTFRQ makes the CPU default cntfrq be 62.5MHz
target/arm/tcg/m_helper.c +1 -1
index 33c9e7c55bc..65dcf97adef 100644
@@ -2775,7 +2775,7 @@ void HELPER(v7m_msr)(CPUARMState *env, uint32_t maskreg, uint32_t val)
                            !arm_v7m_is_handler_mode(env))) {
             write_v7m_control_spsel(env, (val & R_V7M_CONTROL_SPSEL_MASK) != 0);
         }
-        if (cur_el > 0 && arm_feature(env, ARM_FEATURE_M_MAIN)) {
+        if (cur_el > 0 && arm_feature(env, ARM_FEATURE_M_UNPRIV)) {
             env->v7m.control[env->v7m.secure] &= ~R_V7M_CONTROL_NPRIV_MASK;
             env->v7m.control[env->v7m.secure] |= val & R_V7M_CONTROL_NPRIV_MASK;
         }
tests/tcg/arm/system/meson.build +6 -0
index 89734f95605..80c5461e620 100644
@@ -45,6 +45,12 @@ tests += {
                '-T', files('test-armv6m-undef.ld')],
     'qemu_args': ['-M', 'microbit', qemu_base_args],
   },
+  'test-armv6m-control.S': {
+    'cflags': ['-mcpu=cortex-m0', '-mfloat-abi=soft', '-nostdlib',
+               '-Wl,--build-id=none',
+               '-T', files('test-armv6m-undef.ld')],
+    'qemu_args': ['-M', 'microbit', qemu_base_args],
+  },
   'semiconsole.c': {
     'cflags': cflags,
     'qemu_args': ['-serial', 'none', '-chardev', 'stdio,mux=on,id=stdio0',
tests/tcg/arm/system/test-armv6m-control.S new file +66 -0

66 lines not on this page · raw message

Signature · 1 line
2.43.0
#34 lore raw

[PULL 17/37] target/arm: Move ESR_EL1 definition to vmsa_pmsa_cp_reginfo

In reply to Peter Maydell

Currently we define ESR_EL1 in vmsa_cp_reginfo[], but ESR_EL1 isn't VMSA specific. This doesn't matter for "is the register visible when it should be?", because currently all our AArch64 CPUs are VMSA. But it does matter for migration, because the DFSR definition in vmsa_pmsa_cp_reginfo[] is marked ARM_CP_ALIAS on the assumption that the migration of cp15.dfsr_ns will be handled by migrating esr_el[1], which is the same underlying field in the CPU state.

Move ESR_EL1 to vmsa_pmsa_cp_reginfo[]; this will fix the lack of migration of DFSR for R-profile CPUs. This is a migration break for those CPUs, but none of them are used in versioned machine types.

(The S banked DFSR is already migrated, because we register ESR_EL3 in el3_cp_reginfo[] if EL3 exists, regardless of whether we have VMSA or PMSA.)

Message-id: [email protected]

---
 target/arm/helper.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
target/arm/helper.c +8 -8
index c2e94153634..5f930f6fa8f 100644
@@ -2894,6 +2894,14 @@ static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
 }
 
 static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
+    { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
+      .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
+      .access = PL1_RW, .accessfn = access_tvm_trvm,
+      .fgt = FGT_ESR_EL1,
+      .nv2_redirect_offset = 0x138 | NV2_REDIR_NV1,
+      .vhe_redir_to_el2 = ENCODE_AA64_CP_REG(3, 4, 5, 2, 0),
+      .vhe_redir_to_el01 = ENCODE_AA64_CP_REG(3, 5, 5, 2, 0),
+      .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
     { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
       .access = PL1_RW, .accessfn = access_tvm_trvm, .type = ARM_CP_ALIAS,
       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
@@ -2918,14 +2926,6 @@ static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
 };
 
 static const ARMCPRegInfo vmsa_cp_reginfo[] = {
-    { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
-      .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
-      .access = PL1_RW, .accessfn = access_tvm_trvm,
-      .fgt = FGT_ESR_EL1,
-      .nv2_redirect_offset = 0x138 | NV2_REDIR_NV1,
-      .vhe_redir_to_el2 = ENCODE_AA64_CP_REG(3, 4, 5, 2, 0),
-      .vhe_redir_to_el01 = ENCODE_AA64_CP_REG(3, 5, 5, 2, 0),
-      .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
     { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
       .access = PL1_RW, .accessfn = access_tvm_trvm,
Signature · 1 line
2.43.0
#35 lore raw

[PULL 37/37] target/arm: Fix WFxT timeouts when the offset puts the count ahead

In reply to Peter Maydell

From: Fuad Tabba <[email protected]>

The WFIT and WFET helpers arm the wakeup timer at count == timeout + offset, treating an overflow of that sum as "beyond the counter's wrap" and arming at INT64_MAX. As in gt_recalc_timer(), that reading is valid only when offset <= count: with a CNTVOFF_EL2 that puts the virtual count ahead of the physical count, the sum overflows for every timeout still in the future and the wrapped value was the correct wakeup. The CPU then waits until an interrupt or event instead of waking at its timeout. A Linux guest uses WFIT and WFET in __delay() when FEAT_WFxT is present, which -cpu max advertises.

Arm cntval + (timeout - cntvct) instead, so only a physical count past 2^64 is "never", and add a tcg system test that issues WFIT and WFET with such an offset, with a timer interrupt 1s out so a broken WFxT still returns: before this change both wake at the interrupt, after it at their timeout.

Fixes a96edb687e76 ("target/arm: Implement FEAT WFxT and enable for '-cpu max'")
Fixes da9b86c35fa8 ("target/arm: implement WFET")

Message-id: [email protected]

---
 target/arm/tcg/op_helper.c           |   6 +-
 tests/tcg/aarch64/system/meson.build |   7 ++
 tests/tcg/aarch64/system/wfxt.c      | 118 +++++++++++++++++++++++++++
 3 files changed, 129 insertions(+), 2 deletions(-)
 create mode 100644 tests/tcg/aarch64/system/wfxt.c
target/arm/tcg/op_helper.c +4 -2
index 643b1482523..af56eff9f18 100644
@@ -448,7 +448,8 @@ void HELPER(wfit)(CPUARMState *env, uint32_t rd)
         raise_exception(env, excp, syn_wfx(1, 0xe, rd, true, WFIT, false), target_el);
     }
 
-    if (uadd64_overflow(timeout, offset, &nexttick)) {
+    /* Physical count at the timeout. Only an overflow of it is "never". */
+    if (uadd64_overflow(cntval, timeout - cntvct, &nexttick)) {
         nexttick = UINT64_MAX;
     }
     if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
@@ -705,7 +706,8 @@ void HELPER(wfet)(CPUARMState *env, uint32_t rd)
      * The WFET should time out when CNTVCT_EL0 >= the specified value.
      */
     cpu = env_archcpu(env);
-    if (uadd64_overflow(timeout, offset, &nexttick)) {
+    /* Physical count at the timeout. Only an overflow of it is "never". */
+    if (uadd64_overflow(cntval, timeout - cntvct, &nexttick)) {
         nexttick = UINT64_MAX;
     }
     if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
tests/tcg/aarch64/system/meson.build +7 -0
index f9c4448abaf..f51feb253f9 100644
@@ -72,6 +72,13 @@ tests += {
                   '-semihosting-config', 'enable=on,arg=2',
                   qemu_base_args]
   },
+  'wfxt.c': {
+    'cflags': cflags,
+    'qemu_args': ['-M', 'virt,virtualization=on,gic-version=2',
+                  '-cpu', 'max', '-smp', '1',
+                  '-semihosting-config', 'enable=on,arg=2',
+                  qemu_base_args]
+  },
 }
 
 tests += {
tests/tcg/aarch64/system/wfxt.c new file +118 -0

118 lines not on this page · raw message

Signature · 1 line
2.43.0
#36 lore raw

[PULL 11/37] arm/kvm: get demuxed ID registers from kvm

In reply to Peter Maydell

From: Cornelia Huck <[email protected]>

We now have the infrastructure in place to handle demuxed ID registers from kvm. Use it to get the values that kvm emulates for CCSIDR_EL1.

Message-id: [email protected]

[PMM: rewrapped comment with overlength lines]
---
 target/arm/kvm.c | 43 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 43 insertions(+)
target/arm/kvm.c +43 -0
index ed99be7fd80..3d905f8565d 100644
@@ -244,6 +244,45 @@ static int get_host_cpu_reg(int fd, ARMHostCPUFeatures *ahcf,
     return ret;
 }
 
+
+/* CSSELR values supported by kvm; used to index KVM_REG_ARM_DEMUX_ID_CCSIDR */
+#define KVM_CSSELR_MAX 14
+/*
+ * QEMU historically supported CSSELR values 0..15. This difference
+ * doen't matter since CSSELR_EL1.Level == 0b111 is reserved and thus
+ * CSSELR values 14..15 will make CCSIDR read as zero. Larger CSSELR
+ * values will only be necessary when support for MTE Allocation Tag
+ * caches is added.
+ */
+#define QEMU_CSSELR_MAX 16
+
+static int get_host_cpu_reg_demux(int fd, ARMHostCPUFeatures *ahcf,
+                                  ARMIDRegisterIdx index, int subindex)
+{
+
+    struct kvm_one_reg one_reg = {
+        .id = KVM_REG_ARM64 | KVM_REG_SIZE_U32 | KVM_REG_ARM_DEMUX,
+    };
+
+    switch (index) {
+    case CCSIDR_EL1_IDX:
+        if (subindex >= QEMU_CSSELR_MAX) {
+            return -EINVAL;
+        } else if (subindex >= KVM_CSSELR_MAX) {
+            /* CSSELR_EL1.Level == 0b111 is reserved. */
+            ahcf->isar.idregs[index + subindex] = 0;
+            return 0;
+        }
+        one_reg.id |= KVM_REG_ARM_DEMUX_ID_CCSIDR | subindex;
+        one_reg.addr = (uintptr_t)&ahcf->isar.idregs[index + subindex];
+        break;
+    default:
+        return -EINVAL;
+    }
+
+    return ioctl(fd, KVM_GET_ONE_REG, &one_reg);
+}
+
 static uint32_t kvm_arm_sve_get_vls(int fd)
 {
     uint64_t vls[KVM_ARM64_SVE_VLS_WORDS];
@@ -454,6 +493,10 @@ static void kvm_arm_get_host_cpu_features(ARMHostCPUFeatures *ahcf)
             /* Read the set of supported vector lengths. */
             arm_host_cpu_features.sve_vq_supported = kvm_arm_sve_get_vls(fd);
         }
+        /* Grab demuxed registers. */
+        for (int i = 0; i < QEMU_CSSELR_MAX; i++) {
+            err |= get_host_cpu_reg_demux(fd, ahcf, CCSIDR_EL1_IDX, i);
+        }
     }
 
     kvm_arm_destroy_scratch_host_vcpu(fdarray);
Signature · 1 line
2.43.0
#37 lore raw

[PULL 13/37] target/arm: Keep the S/NS bit in migration data

In reply to Peter Maydell

For Arm, the format we use on the wire for migration of sysregs (cpregs on AArch32) is a set of arrays, where cpreg_indexes[] holds the register IDs and cpreg_values[] holds the corresponding values. The ID format we use is KVM's ID format, which is 64 bits. This is not quite the same as the 32-bit ID format QEMU uses internally for things like the coprocessor register hashtable keys, and so we have conversion functions kvm_to_cpreg_id() and cpreg_to_kvm_id() to swap between them.

This conversion has never handled AArch32 Secure coprocessor registers correctly: the KVM ID format has no way to indicate a Secure register, because KVM VMs on AArch32 were always NonSecure. The current code doesn't touch the CP_REG_AA32_NS_MASK bit when converting from the 32-bit ID format to a KVM ID, and it forces the NS bit to 1 when converting from a KVM ID to a cpreg. The effect of this on migration is that the indexes in cpreg_indexes[] have NS = 0 for the Secure banked registers and NS = 1 for the NS banked registers, but when we read and write the values into the CPUState by finding the register using kvm_to_cpreg_id(cpreg_indexes[i]) we always find the NS register, and so the S register values aren't actually migrated.

This went unnoticed most of the time because a typical Linux guest is running only in NS and doesn't really care about the S register state, but it breaks migration of a guest which is running in S, such as Linux on the imx boards.

This would be more awkward to fix if KVM still supported AArch32 hosts, but luckily that was dropped some years ago, and AArch32 guests on an AArch64 host expose the sysregs as CP_REG_ARM64, not CP_REG_ARM32. So the only place where we use a CP_REG_ARM32 KVM register ID is for TCG migrations. We can therefore (continue to) steal a bit in the KVM format for the NS bit, with the semantics we've given it on the wire in the past. We just need to let kvm_to_cpreg_id() pass it through rather than forcing it to 1.

To retain compatibility with migration from older QEMU versions without this fix, we need to add a subsection to the migration data that tells the destination that it can trust the values for Secure banked registers. For incoming migrations, if we don't see this then we ignore all the values for Secure banked registers, giving the same behaviour as before. This means we continue to work for migrations where the Secure world exists but the guest is basically not using it, such as a direct kernel boot on boards which don't set arm_boot_info::secure_boot to true; this includes the cubieboard, bananapi and orangepi.

Attempting migration from a new QEMU to an older QEMU version will now fail cleanly (because the destination doesn't recognize the new subsection) rather than silently corrupting the Secure banked registers.

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/467

Message-id: [email protected]

---
 target/arm/cpregs.h  | 21 ++++++++----
 target/arm/cpu.h     |  6 ++++
 target/arm/machine.c | 80 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 100 insertions(+), 7 deletions(-)
target/arm/cpregs.h +14 -7
index 9f2532fe667..ce651a4faa6 100644
@@ -215,7 +215,20 @@ enum {
 
 /*
  * Convert a full 64 bit KVM register ID to the truncated 32 bit
- * version used as a key for the coprocessor register hashtable
+ * version used as a key for the coprocessor register hashtable.
+ *
+ * Note that we deviate slightly from the KVM register ID format as
+ * used by the kernel for AArch32 cpregs, by using bit 29 as "1
+ * for NonSecure, 0 for Secure". (When KVM still supported AArch32
+ * hosts it didn't set this bit; all sysregs for KVM guests are
+ * NonSecure anyway.) This shouldn't cause any issues as KVM no longer
+ * supports AArch32 hosts (and other accelerators never did), so the
+ * only thing that generates KVM regids for AArch32 cpregs is QEMU
+ * TCG.
+ *
+ * The NS bit being in the KVM ID is implicit in the fact that we
+ * don't mask out CP_REG_AA32_NS_MASK in the conversions to and from
+ * the QEMU hashtable key ID format.
  */
 static inline uint32_t kvm_to_cpreg_id(uint64_t kvmid)
 {
@@ -226,12 +239,6 @@ static inline uint32_t kvm_to_cpreg_id(uint64_t kvmid)
         if ((kvmid & CP_REG_SIZE_MASK) == CP_REG_SIZE_U64) {
             cpregid |= CP_REG_AA32_64BIT_MASK;
         }
-
-        /*
-         * KVM is always non-secure so add the NS flag on AArch32 register
-         * entries.
-         */
-         cpregid |= CP_REG_AA32_NS_MASK;
     }
     return cpregid;
 }
target/arm/cpu.h +6 -0
index f07cadb45c7..418fe78809f 100644
@@ -1146,6 +1146,12 @@ struct ArchCPU {
     bool prop_pauth_qarma5;
     bool prop_lpa2;
 
+    /*
+     * Used only during migration, to handle back-compat with older QEMU
+     * that mishandled migration of AArch32 banked cpregs.
+     */
+    bool secure_banked_regs_ok;
+
     /* GM blocksize, in log_2(words), ie low 4 bits of GMID_EL0 */
     uint8_t gm_blocksize;
 
target/arm/machine.c +80 -0

122 lines not on this page · raw message

Signature · 1 line
2.43.0
#38 lore raw
In reply to Peter Maydell
Quoted 18 lines · Peter Maydell

Hi; here's a target-arm queue: FEAT_UINJ is the biggest thing here, otherwise largely bugfixes of various kinds.

thanks
-- PMM

The following changes since commit 81ce3a87737aa50716c42db8886082d12783e0a1:

   Merge tag 'hw-misc-20260927' ofhttps://github.com/philmd/qemu into staging (2026-09-27 02:02:29 -0700)

are available in the Git repository at:

   https://gitlab.com/pm215/qemu.git tags/pull-target-arm-20260929

for you to fetch changes up to 998b6db69e3ec58fd52ddc7a06ece6cf286f3c77:

target/arm: Fix WFxT timeouts when the offset puts the count ahead (2026-09-29 10:58:40 +0100)

Quoted 13 lines

----------------------------------------------------------------
target-arm queue:
* whpx: arm64: check for physical address width after WHPX availability
* hw/char/stm32l4x5_usart.c: Support 16-bit writes
* hw/arm: Add GPIO to Allwinner A10 model
* hw/arm/tegra241-cmdqv: Keep VINTF page0 region alive
* hw/char/exynos4210_uart: fix fifo overflow
* arm: handle CCSIDR_EL1 as a demuxed register
* target/arm: model Cortex-M0+ privilege extension
* target/arm: Fix migration of Aarch32 Secure banked registers
* target/arm: Fix PMSAv8 shareability decode for R-profile MPU regions
* target/arm: Enable FEAT_UINJ for -cpu max-v9
* target/arm: Fix timer tick and WFxT timeouts when the offset puts the count ahead

Applied, thanks.

r~