Skip to contentSkip to conversation

Slow-path for atomic instruction translation

RFC Superseded Alvise Rigo 7 participants 60 messages Dec 14, 2015 – Jan 11, 2016 Kbuild

v6 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

v6 cover Slow-path for atomic instruction translation

This is the sixth iteration of the patch series which applies to the upstream branch of QEMU (v2.5.0-rc3).

Changes versus previous versions are at the bottom of this cover letter.

The code is also available at following repository:
https://git.virtualopensystems.com/dev/qemu-mt.git
branch:
slowpath-for-atomic-v6-no-mttcg

This patch series provides an infrastructure for atomic instruction implementation in QEMU, thus offering a 'legacy' solution for translating guest atomic instructions. Moreover, it can be considered as a first step toward a multi-thread TCG.

The underlying idea is to provide new TCG helpers (sort of softmmu helpers) that guarantee atomicity to some memory accesses or in general a way to define memory transactions.

More specifically, the new softmmu helpers behave as LoadLink and StoreConditional instructions, and are called from TCG code by means of target specific helpers. This work includes the implementation for all the ARM atomic instructions, see target-arm/op_helper.c.

The implementation heavily uses the software TLB together with a new bitmap that has been added to the ram_list structure which flags, on a per-CPU basis, all the memory pages that are in the middle of a LoadLink (LL), StoreConditional (SC) operation. Since all these pages can be accessed directly through the fast-path and alter a vCPU's linked value, the new bitmap has been coupled with a new TLB flag for the TLB virtual address which forces the slow-path execution for all the accesses to a page containing a linked address.

The new slow-path is implemented such that:
- the LL behaves as a normal load slow-path, except for clearing the dirty flag in the bitmap. The cputlb.c code while generating a TLB entry, checks if there is at least one vCPU that has the bit cleared in the exclusive bitmap, it that case the TLB entry will have the EXCL flag set, thus forcing the slow-path. In order to ensure that all the vCPUs will follow the slow-path for that page, we flush the TLB cache of all the other vCPUs.

The LL will also set the linked address and size of the access in a vCPU's private variable. After the corresponding SC, this address will be set to a reset value.

- the SC can fail returning 1, or succeed, returning 0. It has to come always after a LL and has to access the same address 'linked' by the previous LL, otherwise it will fail. If in the time window delimited by a legit pair of LL/SC operations another write access happens to the linked address, the SC will fail.

In theory, the provided implementation of TCG LoadLink/StoreConditional can be used to properly handle atomic instructions on any architecture.

The code has been tested with bare-metal test cases and by booting Linux.

* Performance considerations
The new slow-path adds some overhead to the translation of the ARM atomic instructions, since their emulation doesn't happen anymore only in the guest (by mean of pure TCG generated code), but requires the execution of two helpers functions. Despite this, the additional time required to boot an ARM Linux kernel on an i7 clocked at 2.5GHz is negligible.
Instead, on a LL/SC bound test scenario - like:
https://git.virtualopensystems.com/dev/tcg_baremetal_tests.git - this solution requires 30% (1 million iterations) and 70% (10 millions iterations) of additional time for the test to complete.

Changes from v5:
- The exclusive memory region is now set through a CPUClass hook, allowing any architecture to decide the memory area that will be protected during a LL/SC operation [PATCH 3]
- The runtime helpers dropped any target dependency and are now in a common file [PATCH 5]
- Improved the way we restore a guest page as non-exclusive [PATCH 9]
- Included MMIO memory as possible target of LL/SC instructions. This also required to somehow simplify the helper_*_st_name helpers in softmmu_template.h [PATCH 8-14]

Changes from v4:
- Reworked the exclusive bitmap to be of fixed size (8 bits per address)
- The slow-path is now TCG backend independent, no need to touch tcg/* anymore as suggested by Aurelien Jarno.

Changes from v3:
- based on upstream QEMU
- addressed comments from Alex Bennée
- the slow path can be enabled by the user with:
./configure --enable-tcg-ldst-excl only if the backend supports it
- all the ARM ldex/stex instructions make now use of the slow path
- added aarch64 TCG backend support
- part of the code has been rewritten

Changes from v2:
- the bitmap accessors are now atomic
- a rendezvous between vCPUs and a simple callback support before executing a TB have been added to handle the TLB flush support
- the softmmu_template and softmmu_llsc_template have been adapted to work on real multi-threading

Changes from v1:
- The ram bitmap is not reversed anymore, 1 = dirty, 0 = exclusive
- The way how the offset to access the bitmap is calculated has been improved and fixed
- A page to be set as dirty requires a vCPU to target the protected address and not just an address in the page
- Addressed comments from Richard Henderson to improve the logic in softmmu_template.h and to simplify the methods generation through softmmu_llsc_template.h
- Added initial implementation of qemu_{ldlink,stcond}_i32 for tcg/i386

This work has been sponsored by Huawei Technologies Duesseldorf GmbH.

Alvise Rigo (14):
exec.c: Add new exclusive bitmap to ram_list
softmmu: Add new TLB_EXCL flag
Add CPUClass hook to set exclusive range
softmmu: Add helpers for a new slowpath
tcg: Create new runtime helpers for excl accesses
configure: Use slow-path for atomic only when the softmmu is enabled
target-arm: translate: Use ld/st excl for atomic insns
target-arm: Add atomic_clear helper for CLREX insn
softmmu: Add history of excl accesses
softmmu: Simplify helper_*_st_name, wrap unaligned code
softmmu: Simplify helper_*_st_name, wrap MMIO code
softmmu: Simplify helper_*_st_name, wrap RAM code
softmmu: Include MMIO/invalid exclusive accesses
softmmu: Protect MMIO exclusive range

 Makefile.target             |   2 +-
 configure                   |   4 +
 cputlb.c                    |  67 ++++++++-
 exec.c                      |   8 +-
 include/exec/cpu-all.h      |   8 ++
 include/exec/cpu-defs.h     |   1 +
 include/exec/helper-gen.h   |   1 +
 include/exec/helper-proto.h |   1 +
 include/exec/helper-tcg.h   |   1 +
 include/exec/memory.h       |   4 +-
 include/exec/ram_addr.h     |  76 ++++++++++
 include/qom/cpu.h           |  21 +++
 qom/cpu.c                   |   7 +
 softmmu_llsc_template.h     | 144 +++++++++++++++++++
 softmmu_template.h          | 338 +++++++++++++++++++++++++++++++++-----------
 target-arm/helper.h         |   2 +
 target-arm/op_helper.c      |   6 +
 target-arm/translate.c      | 102 ++++++++++++-
 tcg-llsc-helper.c           | 109 ++++++++++++++
 tcg-llsc-helper.h           |  35 +++++
 tcg/tcg-llsc-gen-helper.h   |  32 +++++
 tcg/tcg.h                   |  31 ++++
 22 files changed, 909 insertions(+), 91 deletions(-)
 create mode 100644 softmmu_llsc_template.h
 create mode 100644 tcg-llsc-helper.c
 create mode 100644 tcg-llsc-helper.h
 create mode 100644 tcg/tcg-llsc-gen-helper.h
Signature · 1 line
2.6.4
#1 lore raw

RFC v6 1/14 exec.c: Add new exclusive bitmap to ram_list

The purpose of this new bitmap is to flag the memory pages that are in the middle of LL/SC operations (after a LL, before a SC) on a per-vCPU basis.
For all these pages, the corresponding TLB entries will be generated in such a way to force the slow-path if at least one vCPU has the bit not set.
When the system starts, the whole memory is dirty (all the bitmap is set). A page, after being marked as exclusively-clean, will be restored as dirty after the SC.

For each page we keep 8 bits to be shared among all the vCPUs available in the system. In general, the to the vCPU n correspond the bit n % 8.

---
 exec.c                  |  8 ++++--
 include/exec/memory.h   |  3 +-
 include/exec/ram_addr.h | 76 +++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 84 insertions(+), 3 deletions(-)
exec.c +6 -2
index 0bf0a6e..e66d232 100644
@@ -1548,11 +1548,15 @@ static ram_addr_t ram_block_add(RAMBlock *new_block, Error **errp)
         int i;
 
         /* ram_list.dirty_memory[] is protected by the iothread lock.  */
-        for (i = 0; i < DIRTY_MEMORY_NUM; i++) {
+        for (i = 0; i < DIRTY_MEMORY_EXCLUSIVE; i++) {
             ram_list.dirty_memory[i] =
                 bitmap_zero_extend(ram_list.dirty_memory[i],
                                    old_ram_size, new_ram_size);
-       }
+        }
+        ram_list.dirty_memory[DIRTY_MEMORY_EXCLUSIVE] = bitmap_zero_extend(
+                ram_list.dirty_memory[DIRTY_MEMORY_EXCLUSIVE],
+                old_ram_size * EXCL_BITMAP_CELL_SZ,
+                new_ram_size * EXCL_BITMAP_CELL_SZ);
     }
     cpu_physical_memory_set_dirty_range(new_block->offset,
                                         new_block->used_length,
include/exec/memory.h +2 -1
index 0f07159..2782c77 100644
@@ -19,7 +19,8 @@
 #define DIRTY_MEMORY_VGA       0
 #define DIRTY_MEMORY_CODE      1
 #define DIRTY_MEMORY_MIGRATION 2
-#define DIRTY_MEMORY_NUM       3        /* num of dirty bits */
+#define DIRTY_MEMORY_EXCLUSIVE 3
+#define DIRTY_MEMORY_NUM       4        /* num of dirty bits */
 
 #include <stdint.h>
 #include <stdbool.h>
include/exec/ram_addr.h +76 -0

99 lines not on this page · raw message

Signature · 1 line
2.6.4
#2 lore raw

Re: [Qemu-devel] [RFC v6 01/14] exec.c: Add new exclusive bitmap to ram_list

Quoted 32 lines · Alvise Rigo

The purpose of this new bitmap is to flag the memory pages that are in the middle of LL/SC operations (after a LL, before a SC) on a per-vCPU basis.
For all these pages, the corresponding TLB entries will be generated in such a way to force the slow-path if at least one vCPU has the bit not set.
When the system starts, the whole memory is dirty (all the bitmap is set). A page, after being marked as exclusively-clean, will be restored as dirty after the SC.

For each page we keep 8 bits to be shared among all the vCPUs available in the system. In general, the to the vCPU n correspond the bit n % 8.

---
 exec.c                  |  8 ++++--
 include/exec/memory.h   |  3 +-
 include/exec/ram_addr.h | 76 +++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 84 insertions(+), 3 deletions(-)
exec.c +1 -1
index 0bf0a6e..e66d232 100644
@@ -1548,11 +1548,15 @@ static ram_addr_t ram_block_add(RAMBlock *new_block, Error **errp)
         int i;
 
         /* ram_list.dirty_memory[] is protected by the iothread lock.  */
-        for (i = 0; i < DIRTY_MEMORY_NUM; i++) {
+        for (i = 0; i < DIRTY_MEMORY_EXCLUSIVE; i++) {

exec.c static ram_addr_t ram_block_add(RAMBlock *new_block, Error **errp) · line 1560

Quoted 10 lines
exec.c +5 -1
             ram_list.dirty_memory[i] =
                 bitmap_zero_extend(ram_list.dirty_memory[i],
                                    old_ram_size, new_ram_size);
-       }
+        }
+        ram_list.dirty_memory[DIRTY_MEMORY_EXCLUSIVE] = bitmap_zero_extend(
+                ram_list.dirty_memory[DIRTY_MEMORY_EXCLUSIVE],
+                old_ram_size * EXCL_BITMAP_CELL_SZ,
+                new_ram_size * EXCL_BITMAP_CELL_SZ);
     }

I'm wondering is old/new_ram_size should be renamed to old/new_ram_pages?

So as I understand it we have created a bitmap which has EXCL_BITMAP_CELL_SZ bits per page.

Quoted 29 lines
exec.c +0 -0
     cpu_physical_memory_set_dirty_range(new_block->offset,
                                         new_block->used_length,
include/exec/memory.h +2 -1
index 0f07159..2782c77 100644
@@ -19,7 +19,8 @@
 #define DIRTY_MEMORY_VGA       0
 #define DIRTY_MEMORY_CODE      1
 #define DIRTY_MEMORY_MIGRATION 2
-#define DIRTY_MEMORY_NUM       3        /* num of dirty bits */
+#define DIRTY_MEMORY_EXCLUSIVE 3
+#define DIRTY_MEMORY_NUM       4        /* num of dirty bits */
 
 #include <stdint.h>
 #include <stdbool.h>
include/exec/ram_addr.h +1 -0
index 7115154..b48af27 100644
@@ -21,6 +21,7 @@
 
 #ifndef CONFIG_USER_ONLY
 #include "hw/xen/xen.h"
+#include "sysemu/sysemu.h"
 
 struct RAMBlock {
     struct rcu_head rcu;

include/exec/ram_addr.h int qemu_ram_resize(ram_addr_t base, ram_addr_t newsize, Error **errp); · line 92

Quoted 10 lines
include/exec/ram_addr.h +7 -0
@@ -82,6 +83,13 @@ int qemu_ram_resize(ram_addr_t base, ram_addr_t newsize, Error **errp);
 #define DIRTY_CLIENTS_ALL     ((1 << DIRTY_MEMORY_NUM) - 1)
 #define DIRTY_CLIENTS_NOCODE  (DIRTY_CLIENTS_ALL & ~(1 << DIRTY_MEMORY_CODE))
 
+/* Exclusive bitmap support. */
+#define EXCL_BITMAP_CELL_SZ 8
+#define EXCL_BITMAP_GET_BIT_OFFSET(addr) \
+        (EXCL_BITMAP_CELL_SZ * (addr >> TARGET_PAGE_BITS))
+#define EXCL_BITMAP_GET_BYTE_OFFSET(addr) (addr >> TARGET_PAGE_BITS)
+#define EXCL_IDX(cpu) (cpu % EXCL_BITMAP_CELL_SZ)
+

I think some of the explanation of what CELL_SZ means from your commit message needs to go here.

Quoted 31 lines
include/exec/ram_addr.h +17 -0
 static inline bool cpu_physical_memory_get_dirty(ram_addr_t start,
                                                  ram_addr_t length,
                                                  unsigned client)
@@ -173,6 +181,11 @@ static inline void cpu_physical_memory_set_dirty_range(ram_addr_t start,
     if (unlikely(mask & (1 << DIRTY_MEMORY_CODE))) {
         bitmap_set_atomic(d[DIRTY_MEMORY_CODE], page, end - page);
     }
+    if (unlikely(mask & (1 << DIRTY_MEMORY_EXCLUSIVE))) {
+        bitmap_set_atomic(d[DIRTY_MEMORY_EXCLUSIVE],
+                        page * EXCL_BITMAP_CELL_SZ,
+                        (end - page) * EXCL_BITMAP_CELL_SZ);
+    }
     xen_modified_memory(start, length);
 }
 
@@ -288,5 +301,68 @@ uint64_t cpu_physical_memory_sync_dirty_bitmap(unsigned long *dest,
 }
 
 void migration_bitmap_extend(ram_addr_t old, ram_addr_t new);
+
+/* One cell for each page. The n-th bit of a cell describes all the i-th vCPUs
+ * such that (i % EXCL_BITMAP_CELL_SZ) == n.
+ * A bit set to zero ensures that all the vCPUs described by the bit have the
+ * EXCL_BIT set for the page. */
+static inline void cpu_physical_memory_unset_excl(ram_addr_t addr, uint32_t cpu)
+{
+    set_bit_atomic(EXCL_BITMAP_GET_BIT_OFFSET(addr) + EXCL_IDX(cpu),
+            ram_list.dirty_memory[DIRTY_MEMORY_EXCLUSIVE]);
+}
+
+/* Return true if there is at least one cpu with the EXCL bit set for the page

include/exec/ram_addr.h uint64_t cpu_physical_memory_sync_dirty_bitmap(unsigned long *dest, · line 325

Quoted 10 lines
include/exec/ram_addr.h +10 -0
+ * of @addr. */
+static inline int cpu_physical_memory_atleast_one_excl(ram_addr_t addr)
+{
+    uint8_t *bitmap;
+
+    bitmap = (uint8_t *)(ram_list.dirty_memory[DIRTY_MEMORY_EXCLUSIVE]);
+
+    /* This is safe even if smp_cpus < 8 since the unused bits are always 1. */
+    return bitmap[EXCL_BITMAP_GET_BYTE_OFFSET(addr)] != UCHAR_MAX;
+}

So I'm getting a little confused as to why we need EXCL_BITMAP_CELL_SZ bits rather that just one bit shared amongst the CPUs. What's so special about 8 if we could say have a 16 cpu system?

Ultimately if any page has an exclusive operation going on all vCPUs are forced down the slow-path for that pages access? Where is the benefit in splitting that bitfield across these vCPUs if the only test is "is at least one vCPU doing an excl right now?".

Quoted 33 lines
include/exec/ram_addr.h +33 -0
+
+/* Return true if the @cpu has the bit set (not exclusive) for the page of
+ * @addr.  If @cpu == smp_cpus return true if at least one vCPU has the dirty
+ * bit set for that page. */
+static inline int cpu_physical_memory_not_excl(ram_addr_t addr,
+                                               unsigned long cpu)
+{
+    uint8_t *bitmap;
+
+    bitmap = (uint8_t *)ram_list.dirty_memory[DIRTY_MEMORY_EXCLUSIVE];
+
+    if (cpu == smp_cpus) {
+        if (smp_cpus >= EXCL_BITMAP_CELL_SZ) {
+            return bitmap[EXCL_BITMAP_GET_BYTE_OFFSET(addr)];
+        } else {
+            return bitmap[EXCL_BITMAP_GET_BYTE_OFFSET(addr)] &
+                                            ((1 << smp_cpus) - 1);
+        }
+    } else {
+        return bitmap[EXCL_BITMAP_GET_BYTE_OFFSET(addr)] & (1 << EXCL_IDX(cpu));
+    }
+}
+
+/* Clean the dirty bit of @cpu (i.e. set the page as exclusive). If @cpu ==
+ * smp_cpus clean the dirty bit for all the vCPUs. */
+static inline int cpu_physical_memory_set_excl(ram_addr_t addr, uint32_t cpu)
+{
+    if (cpu == smp_cpus) {
+        int nr = (smp_cpus >= EXCL_BITMAP_CELL_SZ) ?
+                            EXCL_BITMAP_CELL_SZ : smp_cpus;
+
+        return bitmap_test_and_clear_atomic(
+                        ram_list.dirty_memory[DIRTY_MEMORY_EXCLUSIVE],

include/exec/ram_addr.h uint64_t cpu_physical_memory_sync_dirty_bitmap(unsigned long *dest, · line 368

Quoted 10 lines
include/exec/ram_addr.h +8 -0
+                        EXCL_BITMAP_GET_BIT_OFFSET(addr), nr);
+    } else {
+        return bitmap_test_and_clear_atomic(
+                        ram_list.dirty_memory[DIRTY_MEMORY_EXCLUSIVE],
+                        EXCL_BITMAP_GET_BIT_OFFSET(addr) + EXCL_IDX(cpu), 1);
+    }
+}
+
 #endif
 #endif

Maybe this will get clearer as I read on but at the moment the bitfield split confuses me.

--
Alex Bennée

#3 lore raw

Re: [Qemu-devel] [RFC v6 01/14] exec.c: Add new exclusive bitmap to ram_list

Quoted 32 lines · Alex Bennée
Quoted 29 lines · Alvise Rigo

The purpose of this new bitmap is to flag the memory pages that are in the middle of LL/SC operations (after a LL, before a SC) on a per-vCPU basis.
For all these pages, the corresponding TLB entries will be generated in such a way to force the slow-path if at least one vCPU has the bit not set.
When the system starts, the whole memory is dirty (all the bitmap is set). A page, after being marked as exclusively-clean, will be restored as dirty after the SC.

For each page we keep 8 bits to be shared among all the vCPUs available in the system. In general, the to the vCPU n correspond the bit n % 8.

---
 exec.c                  |  8 ++++--
 include/exec/memory.h   |  3 +-
 include/exec/ram_addr.h | 76 +++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 84 insertions(+), 3 deletions(-)
exec.c +0 -0
index 0bf0a6e..e66d232 100644
@@ -1548,11 +1548,15 @@ static ram_addr_t ram_block_add(RAMBlock *new_block, Error **errp)
         int i;
 
Quoted 16 lines

exec.c static ram_addr_t ram_block_add(RAMBlock *new_block, Error **errp) · line 1560

Quoted 13 lines
exec.c +6 -2
         /* ram_list.dirty_memory[] is protected by the iothread lock.  */
-        for (i = 0; i < DIRTY_MEMORY_NUM; i++) {
+        for (i = 0; i < DIRTY_MEMORY_EXCLUSIVE; i++) {
             ram_list.dirty_memory[i] =
                 bitmap_zero_extend(ram_list.dirty_memory[i],
                                    old_ram_size, new_ram_size);
-       }
+        }
+        ram_list.dirty_memory[DIRTY_MEMORY_EXCLUSIVE] = bitmap_zero_extend(
+                ram_list.dirty_memory[DIRTY_MEMORY_EXCLUSIVE],
+                old_ram_size * EXCL_BITMAP_CELL_SZ,
+                new_ram_size * EXCL_BITMAP_CELL_SZ);
     }

I'm wondering is old/new_ram_size should be renamed to old/new_ram_pages?

Yes, I think it make more sense.

Quoted 36 lines

So as I understand it we have created a bitmap which has EXCL_BITMAP_CELL_SZ bits per page.

Quoted 32 lines
exec.c +0 -0
     cpu_physical_memory_set_dirty_range(new_block->offset,
                                         new_block->used_length,
include/exec/memory.h +2 -1
index 0f07159..2782c77 100644
@@ -19,7 +19,8 @@
 #define DIRTY_MEMORY_VGA       0
 #define DIRTY_MEMORY_CODE      1
 #define DIRTY_MEMORY_MIGRATION 2
-#define DIRTY_MEMORY_NUM       3        /* num of dirty bits */
+#define DIRTY_MEMORY_EXCLUSIVE 3
+#define DIRTY_MEMORY_NUM       4        /* num of dirty bits */
 
 #include <stdint.h>
 #include <stdbool.h>
include/exec/ram_addr.h +1 -0
index 7115154..b48af27 100644
@@ -21,6 +21,7 @@
 
 #ifndef CONFIG_USER_ONLY
 #include "hw/xen/xen.h"
+#include "sysemu/sysemu.h"
 
 struct RAMBlock {
     struct rcu_head rcu;
@@ -82,6 +83,13 @@ int qemu_ram_resize(ram_addr_t base, ram_addr_t newsize, Error **errp);
 #define DIRTY_CLIENTS_ALL     ((1 << DIRTY_MEMORY_NUM) - 1)
 #define DIRTY_CLIENTS_NOCODE  (DIRTY_CLIENTS_ALL & ~(1 << DIRTY_MEMORY_CODE))
 
Quoted 10 lines

include/exec/ram_addr.h int qemu_ram_resize(ram_addr_t base, ram_addr_t newsize, Error **errp); · line 92

Quoted 7 lines
include/exec/ram_addr.h +7 -0
+/* Exclusive bitmap support. */
+#define EXCL_BITMAP_CELL_SZ 8
+#define EXCL_BITMAP_GET_BIT_OFFSET(addr) \
+        (EXCL_BITMAP_CELL_SZ * (addr >> TARGET_PAGE_BITS))
+#define EXCL_BITMAP_GET_BYTE_OFFSET(addr) (addr >> TARGET_PAGE_BITS)
+#define EXCL_IDX(cpu) (cpu % EXCL_BITMAP_CELL_SZ)
+

I think some of the explanation of what CELL_SZ means from your commit message needs to go here.

OK.

Quoted 38 lines
Quoted 38 lines
include/exec/ram_addr.h +23 -0
 static inline bool cpu_physical_memory_get_dirty(ram_addr_t start,
                                                  ram_addr_t length,
                                                  unsigned client)
@@ -173,6 +181,11 @@ static inline void cpu_physical_memory_set_dirty_range(ram_addr_t start,
     if (unlikely(mask & (1 << DIRTY_MEMORY_CODE))) {
         bitmap_set_atomic(d[DIRTY_MEMORY_CODE], page, end - page);
     }
+    if (unlikely(mask & (1 << DIRTY_MEMORY_EXCLUSIVE))) {
+        bitmap_set_atomic(d[DIRTY_MEMORY_EXCLUSIVE],
+                        page * EXCL_BITMAP_CELL_SZ,
+                        (end - page) * EXCL_BITMAP_CELL_SZ);
+    }
     xen_modified_memory(start, length);
 }
 
@@ -288,5 +301,68 @@ uint64_t cpu_physical_memory_sync_dirty_bitmap(unsigned long *dest,
 }
 
 void migration_bitmap_extend(ram_addr_t old, ram_addr_t new);
+
+/* One cell for each page. The n-th bit of a cell describes all the i-th vCPUs
+ * such that (i % EXCL_BITMAP_CELL_SZ) == n.
+ * A bit set to zero ensures that all the vCPUs described by the bit have the
+ * EXCL_BIT set for the page. */
+static inline void cpu_physical_memory_unset_excl(ram_addr_t addr, uint32_t cpu)
+{
+    set_bit_atomic(EXCL_BITMAP_GET_BIT_OFFSET(addr) + EXCL_IDX(cpu),
+            ram_list.dirty_memory[DIRTY_MEMORY_EXCLUSIVE]);
+}
+
+/* Return true if there is at least one cpu with the EXCL bit set for the page
+ * of @addr. */
+static inline int cpu_physical_memory_atleast_one_excl(ram_addr_t addr)
+{
+    uint8_t *bitmap;
+
+    bitmap = (uint8_t *)(ram_list.dirty_memory[DIRTY_MEMORY_EXCLUSIVE]);
Quoted 8 lines

include/exec/ram_addr.h uint64_t cpu_physical_memory_sync_dirty_bitmap(unsigned long *dest, · line 325

Quoted 4 lines
include/exec/ram_addr.h +4 -0
+
+    /* This is safe even if smp_cpus < 8 since the unused bits are always 1. */
+    return bitmap[EXCL_BITMAP_GET_BYTE_OFFSET(addr)] != UCHAR_MAX;
+}

So I'm getting a little confused as to why we need EXCL_BITMAP_CELL_SZ bits rather that just one bit shared amongst the CPUs. What's so special about 8 if we could say have a 16 cpu system?

Here the ideal solution would be to have one bit per CPU, but since this is too expensive, we group them in cluster of 8.
Of course the value 8 could be changed, but I would not go much higher since DIRTY_MEMORY_EXCLUSIVE is already 8 times the size of the other bitmaps.

Quoted 5 lines

Ultimately if any page has an exclusive operation going on all vCPUs are forced down the slow-path for that pages access? Where is the benefit in splitting that bitfield across these vCPUs if the only test is "is at least one vCPU doing an excl right now?".

Clustering the CPUs in this way, we make the flushing procedure a bit more efficient since we perform TLB flushes at a cluster granularity. In practice, when handling a LL operation we query the flush only to those clusters with the bit "dirty".

Regards,
avise

Quoted 50 lines
Quoted 34 lines
include/exec/ram_addr.h +33 -0
+
+/* Return true if the @cpu has the bit set (not exclusive) for the page of
+ * @addr.  If @cpu == smp_cpus return true if at least one vCPU has the dirty
+ * bit set for that page. */
+static inline int cpu_physical_memory_not_excl(ram_addr_t addr,
+                                               unsigned long cpu)
+{
+    uint8_t *bitmap;
+
+    bitmap = (uint8_t *)ram_list.dirty_memory[DIRTY_MEMORY_EXCLUSIVE];
+
+    if (cpu == smp_cpus) {
+        if (smp_cpus >= EXCL_BITMAP_CELL_SZ) {
+            return bitmap[EXCL_BITMAP_GET_BYTE_OFFSET(addr)];
+        } else {
+            return bitmap[EXCL_BITMAP_GET_BYTE_OFFSET(addr)] &
+                                            ((1 << smp_cpus) - 1);
+        }
+    } else {
+        return bitmap[EXCL_BITMAP_GET_BYTE_OFFSET(addr)] & (1 << EXCL_IDX(cpu));
+    }
+}
+
+/* Clean the dirty bit of @cpu (i.e. set the page as exclusive). If @cpu ==
+ * smp_cpus clean the dirty bit for all the vCPUs. */
+static inline int cpu_physical_memory_set_excl(ram_addr_t addr, uint32_t cpu)
+{
+    if (cpu == smp_cpus) {
+        int nr = (smp_cpus >= EXCL_BITMAP_CELL_SZ) ?
+                            EXCL_BITMAP_CELL_SZ : smp_cpus;
+
+        return bitmap_test_and_clear_atomic(
+                        ram_list.dirty_memory[DIRTY_MEMORY_EXCLUSIVE],

include/exec/ram_addr.h uint64_t cpu_physical_memory_sync_dirty_bitmap(unsigned long *dest, · line 368

Quoted 10 lines
include/exec/ram_addr.h +8 -0
+                        EXCL_BITMAP_GET_BIT_OFFSET(addr), nr);
+    } else {
+        return bitmap_test_and_clear_atomic(
+                        ram_list.dirty_memory[DIRTY_MEMORY_EXCLUSIVE],
+                        EXCL_BITMAP_GET_BIT_OFFSET(addr) + EXCL_IDX(cpu), 1);
+    }
+}
+
 #endif
 #endif

Maybe this will get clearer as I read on but at the moment the bitfield split confuses me.

--
Alex Bennée

#4 lore raw

RFC v6 2/14 softmmu: Add new TLB_EXCL flag

In reply to Alvise Rigo

Add a new TLB flag to force all the accesses made to a page to follow the slow-path.

In the case we remove a TLB entry marked as EXCL, we unset the corresponding exclusive bit in the bitmap.

---
 cputlb.c                |  38 +++++++++++++++-
 include/exec/cpu-all.h  |   8 ++++
 include/exec/cpu-defs.h |   1 +
 include/qom/cpu.h       |  14 ++++++
 softmmu_template.h      | 114 ++++++++++++++++++++++++++++++++++++++----------
 5 files changed, 152 insertions(+), 23 deletions(-)
cputlb.c +37 -1
index bf1d50a..7ee0c89 100644
@@ -394,6 +394,16 @@ void tlb_set_page_with_attrs(CPUState *cpu, target_ulong vaddr,
     env->tlb_v_table[mmu_idx][vidx] = *te;
     env->iotlb_v[mmu_idx][vidx] = env->iotlb[mmu_idx][index];
 
+    if (unlikely(!(te->addr_write & TLB_MMIO) && (te->addr_write & TLB_EXCL))) {
+        /* We are removing an exclusive entry, set the page to dirty. This
+         * is not be necessary if the vCPU has performed both SC and LL. */
+        hwaddr hw_addr = (env->iotlb[mmu_idx][index].addr & TARGET_PAGE_MASK) +
+                                          (te->addr_write & TARGET_PAGE_MASK);
+        if (!cpu->ll_sc_context) {
+            cpu_physical_memory_unset_excl(hw_addr, cpu->cpu_index);
+        }
+    }
+
     /* refill the tlb */
     env->iotlb[mmu_idx][index].addr = iotlb - vaddr;
     env->iotlb[mmu_idx][index].attrs = attrs;
@@ -419,7 +429,15 @@ void tlb_set_page_with_attrs(CPUState *cpu, target_ulong vaddr,
                                                    + xlat)) {
             te->addr_write = address | TLB_NOTDIRTY;
         } else {
-            te->addr_write = address;
+            if (!(address & TLB_MMIO) &&
+                cpu_physical_memory_atleast_one_excl(section->mr->ram_addr
+                                                           + xlat)) {
+                /* There is at least one vCPU that has flagged the address as
+                 * exclusive. */
+                te->addr_write = address | TLB_EXCL;
+            } else {
+                te->addr_write = address;
+            }
         }
     } else {
         te->addr_write = -1;
@@ -471,6 +489,24 @@ tb_page_addr_t get_page_addr_code(CPUArchState *env1, target_ulong addr)
     return qemu_ram_addr_from_host_nofail(p);
 }
 
+/* For every vCPU compare the exclusive address and reset it in case of a
+ * match. Since only one vCPU is running at once, no lock has to be held to
+ * guard this operation. */
+static inline void lookup_and_reset_cpus_ll_addr(hwaddr addr, hwaddr size)
+{
+    CPUState *cpu;
+
+    CPU_FOREACH(cpu) {
+        if (cpu->excl_protected_range.begin != EXCLUSIVE_RESET_ADDR &&
+            ranges_overlap(cpu->excl_protected_range.begin,
+                           cpu->excl_protected_range.end -
+                           cpu->excl_protected_range.begin,
+                           addr, size)) {
+            cpu->excl_protected_range.begin = EXCLUSIVE_RESET_ADDR;
+        }
+    }
+}
+
 #define MMUSUFFIX _mmu
 
 #define SHIFT 0
include/exec/cpu-all.h +8 -0
index 83b1781..f8d8feb 100644
@@ -277,6 +277,14 @@ CPUArchState *cpu_copy(CPUArchState *env);
 #define TLB_NOTDIRTY    (1 << 4)
 /* Set if TLB entry is an IO callback.  */
 #define TLB_MMIO        (1 << 5)
+/* Set if TLB entry references a page that requires exclusive access.  */
+#define TLB_EXCL        (1 << 6)
+
+/* Do not allow a TARGET_PAGE_MASK which covers one or more bits defined
+ * above. */
+#if TLB_EXCL >= TARGET_PAGE_SIZE
+#error TARGET_PAGE_MASK covering the low bits of the TLB virtual address
+#endif
 
 void dump_exec_info(FILE *f, fprintf_function cpu_fprintf);
 void dump_opcount_info(FILE *f, fprintf_function cpu_fprintf);
include/exec/cpu-defs.h +1 -0
index 5093be2..b34d7ae 100644
@@ -27,6 +27,7 @@
 #include <inttypes.h>
 #include "qemu/osdep.h"
 #include "qemu/queue.h"
+#include "qemu/range.h"
 #include "tcg-target.h"
 #ifndef CONFIG_USER_ONLY
 #include "exec/hwaddr.h"
include/qom/cpu.h +14 -0
index 51a1323..c6bb6b6 100644
@@ -29,6 +29,7 @@
 #include "qemu/queue.h"
 #include "qemu/thread.h"
 #include "qemu/typedefs.h"
+#include "qemu/range.h"
 
 typedef int (*WriteCoreDumpFunction)(const void *buf, size_t size,
                                      void *opaque);
@@ -210,6 +211,9 @@ struct kvm_run;
 #define TB_JMP_CACHE_BITS 12
 #define TB_JMP_CACHE_SIZE (1 << TB_JMP_CACHE_BITS)
 
+/* Atomic insn translation TLB support. */
+#define EXCLUSIVE_RESET_ADDR ULLONG_MAX
+
 /**
  * CPUState:
  * @cpu_index: CPU index (informative).
@@ -329,6 +333,16 @@ struct CPUState {
      */
     bool throttle_thread_scheduled;
 
+    /* Used by the atomic insn translation backend. */
+    int ll_sc_context;
+    /* vCPU current exclusive addresses range.
+     * The address is set to EXCLUSIVE_RESET_ADDR if the vCPU is not.
+     * in the middle of a LL/SC. */
+    struct Range excl_protected_range;
+    /* Used to carry the SC result but also to flag a normal (legacy)
+     * store access made by a stcond (see softmmu_template.h). */
+    int excl_succeeded;
+
     /* Note that this is accessed at the start of every TB via a negative
        offset from AREG0.  Leave this field at the end so as to make the
        (absolute value) offset as small as possible.  This reduces code
softmmu_template.h +92 -22

130 lines not on this page · raw message

Signature · 1 line
2.6.4
#5 lore raw

Re: [Qemu-devel] [RFC v6 02/14] softmmu: Add new TLB_EXCL flag

Quoted 22 lines · Alvise Rigo

Add a new TLB flag to force all the accesses made to a page to follow the slow-path.

In the case we remove a TLB entry marked as EXCL, we unset the corresponding exclusive bit in the bitmap.

---
 cputlb.c                |  38 +++++++++++++++-
 include/exec/cpu-all.h  |   8 ++++
 include/exec/cpu-defs.h |   1 +
 include/qom/cpu.h       |  14 ++++++
 softmmu_template.h      | 114 ++++++++++++++++++++++++++++++++++++++----------
 5 files changed, 152 insertions(+), 23 deletions(-)
cputlb.c +0 -0
index bf1d50a..7ee0c89 100644

cputlb.c void tlb_set_page_with_attrs(CPUState *cpu, target_ulong vaddr, · line 398

Quoted 5 lines
cputlb.c +1 -0
@@ -394,6 +394,16 @@ void tlb_set_page_with_attrs(CPUState *cpu, target_ulong vaddr,
     env->tlb_v_table[mmu_idx][vidx] = *te;
     env->iotlb_v[mmu_idx][vidx] = env->iotlb[mmu_idx][index];
 
+    if (unlikely(!(te->addr_write & TLB_MMIO) && (te->addr_write &
 TLB_EXCL))) {

Why do we care about TLB_MMIO flags here? Does it actually happen? Would bad things happen if we enforced exclusivity for an MMIO write? Do the other flags matter?

There should be a comment as to why MMIO is mentioned I think.

Quoted 110 lines
cputlb.c +9 -0
+        /* We are removing an exclusive entry, set the page to dirty. This
+         * is not be necessary if the vCPU has performed both SC and LL. */
+        hwaddr hw_addr = (env->iotlb[mmu_idx][index].addr & TARGET_PAGE_MASK) +
+                                          (te->addr_write & TARGET_PAGE_MASK);
+        if (!cpu->ll_sc_context) {
+            cpu_physical_memory_unset_excl(hw_addr, cpu->cpu_index);
+        }
+    }
+
     /* refill the tlb */
     env->iotlb[mmu_idx][index].addr = iotlb - vaddr;
     env->iotlb[mmu_idx][index].attrs = attrs;
cputlb.c +27 -1
@@ -419,7 +429,15 @@ void tlb_set_page_with_attrs(CPUState *cpu, target_ulong vaddr,
                                                    + xlat)) {
             te->addr_write = address | TLB_NOTDIRTY;
         } else {
-            te->addr_write = address;
+            if (!(address & TLB_MMIO) &&
+                cpu_physical_memory_atleast_one_excl(section->mr->ram_addr
+                                                           + xlat)) {
+                /* There is at least one vCPU that has flagged the address as
+                 * exclusive. */
+                te->addr_write = address | TLB_EXCL;
+            } else {
+                te->addr_write = address;
+            }
         }
     } else {
         te->addr_write = -1;
@@ -471,6 +489,24 @@ tb_page_addr_t get_page_addr_code(CPUArchState *env1, target_ulong addr)
     return qemu_ram_addr_from_host_nofail(p);
 }
 
+/* For every vCPU compare the exclusive address and reset it in case of a
+ * match. Since only one vCPU is running at once, no lock has to be held to
+ * guard this operation. */
+static inline void lookup_and_reset_cpus_ll_addr(hwaddr addr, hwaddr size)
+{
+    CPUState *cpu;
+
+    CPU_FOREACH(cpu) {
+        if (cpu->excl_protected_range.begin != EXCLUSIVE_RESET_ADDR &&
+            ranges_overlap(cpu->excl_protected_range.begin,
+                           cpu->excl_protected_range.end -
+                           cpu->excl_protected_range.begin,
+                           addr, size)) {
+            cpu->excl_protected_range.begin = EXCLUSIVE_RESET_ADDR;
+        }
+    }
+}
+
 #define MMUSUFFIX _mmu
 
 #define SHIFT 0
include/exec/cpu-all.h +8 -0
index 83b1781..f8d8feb 100644
@@ -277,6 +277,14 @@ CPUArchState *cpu_copy(CPUArchState *env);
 #define TLB_NOTDIRTY    (1 << 4)
 /* Set if TLB entry is an IO callback.  */
 #define TLB_MMIO        (1 << 5)
+/* Set if TLB entry references a page that requires exclusive access.  */
+#define TLB_EXCL        (1 << 6)
+
+/* Do not allow a TARGET_PAGE_MASK which covers one or more bits defined
+ * above. */
+#if TLB_EXCL >= TARGET_PAGE_SIZE
+#error TARGET_PAGE_MASK covering the low bits of the TLB virtual address
+#endif
 
 void dump_exec_info(FILE *f, fprintf_function cpu_fprintf);
 void dump_opcount_info(FILE *f, fprintf_function cpu_fprintf);
include/exec/cpu-defs.h +1 -0
index 5093be2..b34d7ae 100644
@@ -27,6 +27,7 @@
 #include <inttypes.h>
 #include "qemu/osdep.h"
 #include "qemu/queue.h"
+#include "qemu/range.h"
 #include "tcg-target.h"
 #ifndef CONFIG_USER_ONLY
 #include "exec/hwaddr.h"
include/qom/cpu.h +4 -0
index 51a1323..c6bb6b6 100644
@@ -29,6 +29,7 @@
 #include "qemu/queue.h"
 #include "qemu/thread.h"
 #include "qemu/typedefs.h"
+#include "qemu/range.h"
 
 typedef int (*WriteCoreDumpFunction)(const void *buf, size_t size,
                                      void *opaque);
@@ -210,6 +211,9 @@ struct kvm_run;
 #define TB_JMP_CACHE_BITS 12
 #define TB_JMP_CACHE_SIZE (1 << TB_JMP_CACHE_BITS)
 
+/* Atomic insn translation TLB support. */
+#define EXCLUSIVE_RESET_ADDR ULLONG_MAX
+
 /**
  * CPUState:
  * @cpu_index: CPU index (informative).
@@ -329,6 +333,16 @@ struct CPUState {
      */
     bool throttle_thread_scheduled;

include/qom/cpu.h struct CPUState { · line 344

Quoted 10 lines
include/qom/cpu.h +9 -0
 
+    /* Used by the atomic insn translation backend. */
+    int ll_sc_context;
+    /* vCPU current exclusive addresses range.
+     * The address is set to EXCLUSIVE_RESET_ADDR if the vCPU is not.
+     * in the middle of a LL/SC. */
+    struct Range excl_protected_range;
+    /* Used to carry the SC result but also to flag a normal (legacy)
+     * store access made by a stcond (see softmmu_template.h). */
+    int excl_succeeded;

It might be clearer if excl_succeeded was defined as a bool?

Quoted 19 lines
include/qom/cpu.h +0 -0
     /* Note that this is accessed at the start of every TB via a negative
        offset from AREG0.  Leave this field at the end so as to make the
        (absolute value) offset as small as possible.  This reduces code
softmmu_template.h +3 -4
index 6803890..24d29b7 100644
@@ -395,19 +395,54 @@ void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
         tlb_addr = env->tlb_table[mmu_idx][index].addr_write;
     }
 
-    /* Handle an IO access.  */
+    /* Handle an IO access or exclusive access.  */
     if (unlikely(tlb_addr & ~TARGET_PAGE_MASK)) {
-        CPUIOTLBEntry *iotlbentry;
-        if ((addr & (DATA_SIZE - 1)) != 0) {
-            goto do_unaligned_access;
+        CPUIOTLBEntry *iotlbentry = &env->iotlb[mmu_idx][index];
+

softmmu_template.h void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val, · line 411

Quoted 10 lines
softmmu_template.h +10 -0
+        if ((tlb_addr & ~TARGET_PAGE_MASK) == TLB_EXCL) {
+            CPUState *cpu = ENV_GET_CPU(env);
+            /* The slow-path has been forced since we are writing to
+             * exclusive-protected memory. */
+            hwaddr hw_addr = (iotlbentry->addr & TARGET_PAGE_MASK) + addr;
+
+            /* The function lookup_and_reset_cpus_ll_addr could have reset the
+             * exclusive address. Fail the SC in this case.
+             * N.B.: Here excl_succeeded == 0 means that helper_le_st_name has
+             * not been called by a softmmu_llsc_template.h. */

Could this be better worded (along with bool-ising) as:

"excl_succeeded is set by helper_le_st_name (softmmu_llsc_template)."

But having said that grepping for helper_le_st_name I see that's defined in softmmu_template.h so now the comments has confused me.

It also might be worth mentioning the subtly that exclusive addresses are based on the real hwaddr (hence the iotlb lookup?).

softmmu_template.h void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val, · line 416

Quoted 5 lines
softmmu_template.h +5 -0
+            if (cpu->excl_succeeded) {
+                if (cpu->excl_protected_range.begin != hw_addr) {
+                    /* The vCPU is SC-ing to an unprotected address. */
+                    cpu->excl_protected_range.begin = EXCLUSIVE_RESET_ADDR;
+                    cpu->excl_succeeded = 0;

cpu->excl_succeeded = false;

softmmu_template.h void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val, · line 429

Quoted 13 lines
softmmu_template.h +13 -0
+
+                    return;
+                }
+
+                cpu_physical_memory_unset_excl(hw_addr, cpu->cpu_index);
+            }
+
+            haddr = addr + env->tlb_table[mmu_idx][index].addend;
+        #if DATA_SIZE == 1
+            glue(glue(st, SUFFIX), _p)((uint8_t *)haddr, val);
+        #else
+            glue(glue(st, SUFFIX), _le_p)((uint8_t *)haddr, val);
+        #endif

Why the special casing for byte access? Isn't this something the glue + SUFFIX magic is meant to sort out?

softmmu_template.h void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val, · line 438

Quoted 9 lines
softmmu_template.h +9 -0
+
+            lookup_and_reset_cpus_ll_addr(hw_addr, DATA_SIZE);
+
+            return;
+        } else {
+            if ((addr & (DATA_SIZE - 1)) != 0) {
+                goto do_unaligned_access;
+            }
+            iotlbentry = &env->iotlb[mmu_idx][index];

Are we re-loading the TLB entry here?

softmmu_template.h void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,

Quoted 6 lines
softmmu_template.h +5 -0
+
+            /* ??? Note that the io helpers always read data in the target
+               byte ordering.  We should push the LE/BE request down into io.  */
+            val = TGT_LE(val);
+            glue(io_write, SUFFIX)(env, iotlbentry, val, addr,
 retaddr);

What happens if the software does and exclusive operation on a io address?

Quoted 13 lines
softmmu_template.h +1 -7
+            return;
         }
-        iotlbentry = &env->iotlb[mmu_idx][index];
-
-        /* ??? Note that the io helpers always read data in the target
-           byte ordering.  We should push the LE/BE request down into io.  */
-        val = TGT_LE(val);
-        glue(io_write, SUFFIX)(env, iotlbentry, val, addr, retaddr);
-        return;
     }
 
     /* Handle slow unaligned access (it spans two pages or IO).  */

softmmu_template.h void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val, · line 513

Quoted 5 lines
softmmu_template.h +1 -1
@@ -475,19 +510,54 @@ void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
         tlb_addr = env->tlb_table[mmu_idx][index].addr_write;
     }
 
-    /* Handle an IO access.  */
+    /* Handle an IO access or exclusive access.  */

Hmm there looks like a massive amount of duplication (not your fault, it was like that when you got here ;-) but maybe this can be re-factored away somehow?

Quoted 50 lines
softmmu_template.h +45 -3
     if (unlikely(tlb_addr & ~TARGET_PAGE_MASK)) {
-        CPUIOTLBEntry *iotlbentry;
-        if ((addr & (DATA_SIZE - 1)) != 0) {
-            goto do_unaligned_access;
+        CPUIOTLBEntry *iotlbentry = &env->iotlb[mmu_idx][index];
+
+        if ((tlb_addr & ~TARGET_PAGE_MASK) == TLB_EXCL) {
+            CPUState *cpu = ENV_GET_CPU(env);
+            /* The slow-path has been forced since we are writing to
+             * exclusive-protected memory. */
+            hwaddr hw_addr = (iotlbentry->addr & TARGET_PAGE_MASK) + addr;
+
+            /* The function lookup_and_reset_cpus_ll_addr could have reset the
+             * exclusive address. Fail the SC in this case.
+             * N.B.: Here excl_succeeded == 0 means that helper_le_st_name has
+             * not been called by a softmmu_llsc_template.h. */
+            if (cpu->excl_succeeded) {
+                if (cpu->excl_protected_range.begin != hw_addr) {
+                    /* The vCPU is SC-ing to an unprotected address. */
+                    cpu->excl_protected_range.begin = EXCLUSIVE_RESET_ADDR;
+                    cpu->excl_succeeded = 0;
+
+                    return;
+                }
+
+                cpu_physical_memory_unset_excl(hw_addr, cpu->cpu_index);
+            }
+
+            haddr = addr + env->tlb_table[mmu_idx][index].addend;
+        #if DATA_SIZE == 1
+            glue(glue(st, SUFFIX), _p)((uint8_t *)haddr, val);
+        #else
+            glue(glue(st, SUFFIX), _le_p)((uint8_t *)haddr, val);
+        #endif
+
+            lookup_and_reset_cpus_ll_addr(hw_addr, DATA_SIZE);
+
+            return;
+        } else {
+            if ((addr & (DATA_SIZE - 1)) != 0) {
+                goto do_unaligned_access;
+            }
+            iotlbentry = &env->iotlb[mmu_idx][index];
+
+            /* ??? Note that the io helpers always read data in the target
+               byte ordering.  We should push the LE/BE request down into io.  */
+            val = TGT_BE(val);
+            glue(io_write, SUFFIX)(env, iotlbentry, val, addr, retaddr);
+            return;
         }

softmmu_template.h void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val, · line 563

Quoted 10 lines
softmmu_template.h +0 -7
-        iotlbentry = &env->iotlb[mmu_idx][index];
-
-        /* ??? Note that the io helpers always read data in the target
-           byte ordering.  We should push the LE/BE request down into io.  */
-        val = TGT_BE(val);
-        glue(io_write, SUFFIX)(env, iotlbentry, val, addr, retaddr);
-        return;
     }
 
     /* Handle slow unaligned access (it spans two pages or IO).  */

--
Alex Bennée

#6 lore raw

Re: [Qemu-devel] [RFC v6 02/14] softmmu: Add new TLB_EXCL flag

Quoted 28 lines · Alex Bennée
Quoted 25 lines · Alvise Rigo

Add a new TLB flag to force all the accesses made to a page to follow the slow-path.

In the case we remove a TLB entry marked as EXCL, we unset the corresponding exclusive bit in the bitmap.

---
 cputlb.c                |  38 +++++++++++++++-
 include/exec/cpu-all.h  |   8 ++++
 include/exec/cpu-defs.h |   1 +
 include/qom/cpu.h       |  14 ++++++
 softmmu_template.h      | 114 ++++++++++++++++++++++++++++++++++++++----------
 5 files changed, 152 insertions(+), 23 deletions(-)
cputlb.c +0 -0
index bf1d50a..7ee0c89 100644
@@ -394,6 +394,16 @@ void tlb_set_page_with_attrs(CPUState *cpu, target_ulong vaddr,
     env->tlb_v_table[mmu_idx][vidx] = *te;
     env->iotlb_v[mmu_idx][vidx] = env->iotlb[mmu_idx][index];
 
Quoted 6 lines

cputlb.c void tlb_set_page_with_attrs(CPUState *cpu, target_ulong vaddr, · line 398

Quoted 2 lines
cputlb.c +1 -0
+    if (unlikely(!(te->addr_write & TLB_MMIO) && (te->addr_write &
 TLB_EXCL))) {

Why do we care about TLB_MMIO flags here? Does it actually happen? Would bad things happen if we enforced exclusivity for an MMIO write? Do the other flags matter?

In the previous version of the patch series it came out that the accesses to MMIO regions have to be supported since, for instance, the GDB stub relies on them.
The last two patches actually finalize the MMIO support.

Quoted 2 lines

There should be a comment as to why MMIO is mentioned I think.

OK.

Quoted 112 lines
Quoted 112 lines
cputlb.c +9 -0
+        /* We are removing an exclusive entry, set the page to dirty. This
+         * is not be necessary if the vCPU has performed both SC and LL. */
+        hwaddr hw_addr = (env->iotlb[mmu_idx][index].addr & TARGET_PAGE_MASK) +
+                                          (te->addr_write & TARGET_PAGE_MASK);
+        if (!cpu->ll_sc_context) {
+            cpu_physical_memory_unset_excl(hw_addr, cpu->cpu_index);
+        }
+    }
+
     /* refill the tlb */
     env->iotlb[mmu_idx][index].addr = iotlb - vaddr;
     env->iotlb[mmu_idx][index].attrs = attrs;
cputlb.c +27 -1
@@ -419,7 +429,15 @@ void tlb_set_page_with_attrs(CPUState *cpu, target_ulong vaddr,
                                                    + xlat)) {
             te->addr_write = address | TLB_NOTDIRTY;
         } else {
-            te->addr_write = address;
+            if (!(address & TLB_MMIO) &&
+                cpu_physical_memory_atleast_one_excl(section->mr->ram_addr
+                                                           + xlat)) {
+                /* There is at least one vCPU that has flagged the address as
+                 * exclusive. */
+                te->addr_write = address | TLB_EXCL;
+            } else {
+                te->addr_write = address;
+            }
         }
     } else {
         te->addr_write = -1;
@@ -471,6 +489,24 @@ tb_page_addr_t get_page_addr_code(CPUArchState *env1, target_ulong addr)
     return qemu_ram_addr_from_host_nofail(p);
 }
 
+/* For every vCPU compare the exclusive address and reset it in case of a
+ * match. Since only one vCPU is running at once, no lock has to be held to
+ * guard this operation. */
+static inline void lookup_and_reset_cpus_ll_addr(hwaddr addr, hwaddr size)
+{
+    CPUState *cpu;
+
+    CPU_FOREACH(cpu) {
+        if (cpu->excl_protected_range.begin != EXCLUSIVE_RESET_ADDR &&
+            ranges_overlap(cpu->excl_protected_range.begin,
+                           cpu->excl_protected_range.end -
+                           cpu->excl_protected_range.begin,
+                           addr, size)) {
+            cpu->excl_protected_range.begin = EXCLUSIVE_RESET_ADDR;
+        }
+    }
+}
+
 #define MMUSUFFIX _mmu
 
 #define SHIFT 0
include/exec/cpu-all.h +8 -0
index 83b1781..f8d8feb 100644
@@ -277,6 +277,14 @@ CPUArchState *cpu_copy(CPUArchState *env);
 #define TLB_NOTDIRTY    (1 << 4)
 /* Set if TLB entry is an IO callback.  */
 #define TLB_MMIO        (1 << 5)
+/* Set if TLB entry references a page that requires exclusive access.  */
+#define TLB_EXCL        (1 << 6)
+
+/* Do not allow a TARGET_PAGE_MASK which covers one or more bits defined
+ * above. */
+#if TLB_EXCL >= TARGET_PAGE_SIZE
+#error TARGET_PAGE_MASK covering the low bits of the TLB virtual address
+#endif
 
 void dump_exec_info(FILE *f, fprintf_function cpu_fprintf);
 void dump_opcount_info(FILE *f, fprintf_function cpu_fprintf);
include/exec/cpu-defs.h +1 -0
index 5093be2..b34d7ae 100644
@@ -27,6 +27,7 @@
 #include <inttypes.h>
 #include "qemu/osdep.h"
 #include "qemu/queue.h"
+#include "qemu/range.h"
 #include "tcg-target.h"
 #ifndef CONFIG_USER_ONLY
 #include "exec/hwaddr.h"
include/qom/cpu.h +4 -0
index 51a1323..c6bb6b6 100644
@@ -29,6 +29,7 @@
 #include "qemu/queue.h"
 #include "qemu/thread.h"
 #include "qemu/typedefs.h"
+#include "qemu/range.h"
 
 typedef int (*WriteCoreDumpFunction)(const void *buf, size_t size,
                                      void *opaque);
@@ -210,6 +211,9 @@ struct kvm_run;
 #define TB_JMP_CACHE_BITS 12
 #define TB_JMP_CACHE_SIZE (1 << TB_JMP_CACHE_BITS)
 
+/* Atomic insn translation TLB support. */
+#define EXCLUSIVE_RESET_ADDR ULLONG_MAX
+
 /**
  * CPUState:
  * @cpu_index: CPU index (informative).
@@ -329,6 +333,16 @@ struct CPUState {
      */
     bool throttle_thread_scheduled;
 
Quoted 11 lines

include/qom/cpu.h struct CPUState { · line 344

Quoted 9 lines
include/qom/cpu.h +9 -0
+    /* Used by the atomic insn translation backend. */
+    int ll_sc_context;
+    /* vCPU current exclusive addresses range.
+     * The address is set to EXCLUSIVE_RESET_ADDR if the vCPU is not.
+     * in the middle of a LL/SC. */
+    struct Range excl_protected_range;
+    /* Used to carry the SC result but also to flag a normal (legacy)
+     * store access made by a stcond (see softmmu_template.h). */
+    int excl_succeeded;

It might be clearer if excl_succeeded was defined as a bool?

Yes, that might be a good idea.

Quoted 33 lines
Quoted 20 lines
include/qom/cpu.h +0 -0
     /* Note that this is accessed at the start of every TB via a negative
        offset from AREG0.  Leave this field at the end so as to make the
        (absolute value) offset as small as possible.  This reduces code
softmmu_template.h +3 -4
index 6803890..24d29b7 100644
@@ -395,19 +395,54 @@ void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
         tlb_addr = env->tlb_table[mmu_idx][index].addr_write;
     }
 
-    /* Handle an IO access.  */
+    /* Handle an IO access or exclusive access.  */
     if (unlikely(tlb_addr & ~TARGET_PAGE_MASK)) {
-        CPUIOTLBEntry *iotlbentry;
-        if ((addr & (DATA_SIZE - 1)) != 0) {
-            goto do_unaligned_access;
+        CPUIOTLBEntry *iotlbentry = &env->iotlb[mmu_idx][index];
+

softmmu_template.h void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val, · line 411

Quoted 10 lines
softmmu_template.h +10 -0
+        if ((tlb_addr & ~TARGET_PAGE_MASK) == TLB_EXCL) {
+            CPUState *cpu = ENV_GET_CPU(env);
+            /* The slow-path has been forced since we are writing to
+             * exclusive-protected memory. */
+            hwaddr hw_addr = (iotlbentry->addr & TARGET_PAGE_MASK) + addr;
+
+            /* The function lookup_and_reset_cpus_ll_addr could have reset the
+             * exclusive address. Fail the SC in this case.
+             * N.B.: Here excl_succeeded == 0 means that helper_le_st_name has
+             * not been called by a softmmu_llsc_template.h. */

Could this be better worded (along with bool-ising) as:

Quoted 4 lines

"excl_succeeded is set by helper_le_st_name (softmmu_llsc_template)."

But having said that grepping for helper_le_st_name I see that's defined in softmmu_template.h so now the comments has confused me.

I see now that the comment refers to softmmu_llsc_template that will be created later on. Please consider this fixed.
In any case excl_succeeded, as the name suggests, is used by helper_stcond_name to know if the exclusive access went well. However, it is also used by softmmu_template to know whether we came from softmmu_llsc_template or not. This behaviour is pointed out in a comment is softmmu_llsc_template.

Quoted 3 lines

It also might be worth mentioning the subtly that exclusive addresses are based on the real hwaddr (hence the iotlb lookup?).

OK.

Quoted 9 lines

softmmu_template.h void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val, · line 416

Quoted 6 lines
softmmu_template.h +5 -0
+            if (cpu->excl_succeeded) {
+                if (cpu->excl_protected_range.begin != hw_addr) {
+                    /* The vCPU is SC-ing to an unprotected address. */
+                    cpu->excl_protected_range.begin = EXCLUSIVE_RESET_ADDR;
+                    cpu->excl_succeeded = 0;

cpu->excl_succeeded = false;

Quoted 16 lines

softmmu_template.h void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val, · line 429

Quoted 13 lines
softmmu_template.h +13 -0
+
+                    return;
+                }
+
+                cpu_physical_memory_unset_excl(hw_addr, cpu->cpu_index);
+            }
+
+            haddr = addr + env->tlb_table[mmu_idx][index].addend;
+        #if DATA_SIZE == 1
+            glue(glue(st, SUFFIX), _p)((uint8_t *)haddr, val);
+        #else
+            glue(glue(st, SUFFIX), _le_p)((uint8_t *)haddr, val);
+        #endif

Why the special casing for byte access? Isn't this something the glue + SUFFIX magic is meant to sort out?

For the byte access the byte ordering is irrelevant, in fact there is only one version of stb_p.

Quoted 12 lines

softmmu_template.h void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val, · line 438

Quoted 10 lines
softmmu_template.h +9 -0
+
+            lookup_and_reset_cpus_ll_addr(hw_addr, DATA_SIZE);
+
+            return;
+        } else {
+            if ((addr & (DATA_SIZE - 1)) != 0) {
+                goto do_unaligned_access;
+            }
+            iotlbentry = &env->iotlb[mmu_idx][index];

Are we re-loading the TLB entry here?

Indeed, that should not be there (anyhow it will go away when we refactor this helper in patches 10,11,12).

Quoted 10 lines

softmmu_template.h void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,

Quoted 7 lines
softmmu_template.h +5 -0
+
+            /* ??? Note that the io helpers always read data in the target
+               byte ordering.  We should push the LE/BE request down into io.  */
+            val = TGT_LE(val);
+            glue(io_write, SUFFIX)(env, iotlbentry, val, addr,
 retaddr);

What happens if the software does and exclusive operation on a io address?

At this stage of the patch series such operations are not supported. Should I add an hw_error in case a software tries to do that? As written above, patches 13 and 14 add the missing pieces to support exclusive operations to MMIO regions.

Quoted 17 lines
Quoted 17 lines
softmmu_template.h +1 -7
+            return;
         }
-        iotlbentry = &env->iotlb[mmu_idx][index];
-
-        /* ??? Note that the io helpers always read data in the target
-           byte ordering.  We should push the LE/BE request down into io.  */
-        val = TGT_LE(val);
-        glue(io_write, SUFFIX)(env, iotlbentry, val, addr, retaddr);
-        return;
     }
 
     /* Handle slow unaligned access (it spans two pages or IO).  */
softmmu_template.h +0 -0
@@ -475,19 +510,54 @@ void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
         tlb_addr = env->tlb_table[mmu_idx][index].addr_write;
     }
 
Quoted 6 lines

softmmu_template.h void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val, · line 513

Quoted 2 lines
softmmu_template.h +1 -1
-    /* Handle an IO access.  */
+    /* Handle an IO access or exclusive access.  */

Hmm there looks like a massive amount of duplication (not your fault, it was like that when you got here ;-) but maybe this can be re-factored away somehow?

That's true. This is why patches 10,11,12 try to alleviate this problem by making the code a little bit more compact and readable.

Regards,
alvise

Quoted 65 lines
Quoted 51 lines
softmmu_template.h +45 -3
     if (unlikely(tlb_addr & ~TARGET_PAGE_MASK)) {
-        CPUIOTLBEntry *iotlbentry;
-        if ((addr & (DATA_SIZE - 1)) != 0) {
-            goto do_unaligned_access;
+        CPUIOTLBEntry *iotlbentry = &env->iotlb[mmu_idx][index];
+
+        if ((tlb_addr & ~TARGET_PAGE_MASK) == TLB_EXCL) {
+            CPUState *cpu = ENV_GET_CPU(env);
+            /* The slow-path has been forced since we are writing to
+             * exclusive-protected memory. */
+            hwaddr hw_addr = (iotlbentry->addr & TARGET_PAGE_MASK) + addr;
+
+            /* The function lookup_and_reset_cpus_ll_addr could have reset the
+             * exclusive address. Fail the SC in this case.
+             * N.B.: Here excl_succeeded == 0 means that helper_le_st_name has
+             * not been called by a softmmu_llsc_template.h. */
+            if (cpu->excl_succeeded) {
+                if (cpu->excl_protected_range.begin != hw_addr) {
+                    /* The vCPU is SC-ing to an unprotected address. */
+                    cpu->excl_protected_range.begin = EXCLUSIVE_RESET_ADDR;
+                    cpu->excl_succeeded = 0;
+
+                    return;
+                }
+
+                cpu_physical_memory_unset_excl(hw_addr, cpu->cpu_index);
+            }
+
+            haddr = addr + env->tlb_table[mmu_idx][index].addend;
+        #if DATA_SIZE == 1
+            glue(glue(st, SUFFIX), _p)((uint8_t *)haddr, val);
+        #else
+            glue(glue(st, SUFFIX), _le_p)((uint8_t *)haddr, val);
+        #endif
+
+            lookup_and_reset_cpus_ll_addr(hw_addr, DATA_SIZE);
+
+            return;
+        } else {
+            if ((addr & (DATA_SIZE - 1)) != 0) {
+                goto do_unaligned_access;
+            }
+            iotlbentry = &env->iotlb[mmu_idx][index];
+
+            /* ??? Note that the io helpers always read data in the target
+               byte ordering.  We should push the LE/BE request down into io.  */
+            val = TGT_BE(val);
+            glue(io_write, SUFFIX)(env, iotlbentry, val, addr, retaddr);
+            return;
         }

softmmu_template.h void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val, · line 563

Quoted 10 lines
softmmu_template.h +0 -7
-        iotlbentry = &env->iotlb[mmu_idx][index];
-
-        /* ??? Note that the io helpers always read data in the target
-           byte ordering.  We should push the LE/BE request down into io.  */
-        val = TGT_BE(val);
-        glue(io_write, SUFFIX)(env, iotlbentry, val, addr, retaddr);
-        return;
     }
 
     /* Handle slow unaligned access (it spans two pages or IO).  */

--
Alex Bennée

#7 lore raw

Re: [Qemu-devel] [RFC v6 02/14] softmmu: Add new TLB_EXCL flag

Quoted 250 lines · alvise rigo
Quoted 28 lines · Alex Bennée
Quoted 25 lines · Alvise Rigo

Add a new TLB flag to force all the accesses made to a page to follow the slow-path.

In the case we remove a TLB entry marked as EXCL, we unset the corresponding exclusive bit in the bitmap.

---
 cputlb.c                |  38 +++++++++++++++-
 include/exec/cpu-all.h  |   8 ++++
 include/exec/cpu-defs.h |   1 +
 include/qom/cpu.h       |  14 ++++++
 softmmu_template.h      | 114 ++++++++++++++++++++++++++++++++++++++----------
 5 files changed, 152 insertions(+), 23 deletions(-)
cputlb.c +0 -0
index bf1d50a..7ee0c89 100644
@@ -394,6 +394,16 @@ void tlb_set_page_with_attrs(CPUState *cpu, target_ulong vaddr,
     env->tlb_v_table[mmu_idx][vidx] = *te;
     env->iotlb_v[mmu_idx][vidx] = env->iotlb[mmu_idx][index];
 
Quoted 6 lines

cputlb.c void tlb_set_page_with_attrs(CPUState *cpu, target_ulong vaddr, · line 398

Quoted 2 lines
cputlb.c +1 -0
+    if (unlikely(!(te->addr_write & TLB_MMIO) && (te->addr_write &
 TLB_EXCL))) {

Why do we care about TLB_MMIO flags here? Does it actually happen? Would bad things happen if we enforced exclusivity for an MMIO write? Do the other flags matter?

In the previous version of the patch series it came out that the accesses to MMIO regions have to be supported since, for instance, the GDB stub relies on them.
The last two patches actually finalize the MMIO support.

Quoted 2 lines

There should be a comment as to why MMIO is mentioned I think.

OK.

Quoted 112 lines
Quoted 112 lines
cputlb.c +9 -0
+        /* We are removing an exclusive entry, set the page to dirty. This
+         * is not be necessary if the vCPU has performed both SC and LL. */
+        hwaddr hw_addr = (env->iotlb[mmu_idx][index].addr & TARGET_PAGE_MASK) +
+                                          (te->addr_write & TARGET_PAGE_MASK);
+        if (!cpu->ll_sc_context) {
+            cpu_physical_memory_unset_excl(hw_addr, cpu->cpu_index);
+        }
+    }
+
     /* refill the tlb */
     env->iotlb[mmu_idx][index].addr = iotlb - vaddr;
     env->iotlb[mmu_idx][index].attrs = attrs;
cputlb.c +27 -1
@@ -419,7 +429,15 @@ void tlb_set_page_with_attrs(CPUState *cpu, target_ulong vaddr,
                                                    + xlat)) {
             te->addr_write = address | TLB_NOTDIRTY;
         } else {
-            te->addr_write = address;
+            if (!(address & TLB_MMIO) &&
+                cpu_physical_memory_atleast_one_excl(section->mr->ram_addr
+                                                           + xlat)) {
+                /* There is at least one vCPU that has flagged the address as
+                 * exclusive. */
+                te->addr_write = address | TLB_EXCL;
+            } else {
+                te->addr_write = address;
+            }
         }
     } else {
         te->addr_write = -1;
@@ -471,6 +489,24 @@ tb_page_addr_t get_page_addr_code(CPUArchState *env1, target_ulong addr)
     return qemu_ram_addr_from_host_nofail(p);
 }
 
+/* For every vCPU compare the exclusive address and reset it in case of a
+ * match. Since only one vCPU is running at once, no lock has to be held to
+ * guard this operation. */
+static inline void lookup_and_reset_cpus_ll_addr(hwaddr addr, hwaddr size)
+{
+    CPUState *cpu;
+
+    CPU_FOREACH(cpu) {
+        if (cpu->excl_protected_range.begin != EXCLUSIVE_RESET_ADDR &&
+            ranges_overlap(cpu->excl_protected_range.begin,
+                           cpu->excl_protected_range.end -
+                           cpu->excl_protected_range.begin,
+                           addr, size)) {
+            cpu->excl_protected_range.begin = EXCLUSIVE_RESET_ADDR;
+        }
+    }
+}
+
 #define MMUSUFFIX _mmu
 
 #define SHIFT 0
include/exec/cpu-all.h +8 -0
index 83b1781..f8d8feb 100644
@@ -277,6 +277,14 @@ CPUArchState *cpu_copy(CPUArchState *env);
 #define TLB_NOTDIRTY    (1 << 4)
 /* Set if TLB entry is an IO callback.  */
 #define TLB_MMIO        (1 << 5)
+/* Set if TLB entry references a page that requires exclusive access.  */
+#define TLB_EXCL        (1 << 6)
+
+/* Do not allow a TARGET_PAGE_MASK which covers one or more bits defined
+ * above. */
+#if TLB_EXCL >= TARGET_PAGE_SIZE
+#error TARGET_PAGE_MASK covering the low bits of the TLB virtual address
+#endif
 
 void dump_exec_info(FILE *f, fprintf_function cpu_fprintf);
 void dump_opcount_info(FILE *f, fprintf_function cpu_fprintf);
include/exec/cpu-defs.h +1 -0
index 5093be2..b34d7ae 100644
@@ -27,6 +27,7 @@
 #include <inttypes.h>
 #include "qemu/osdep.h"
 #include "qemu/queue.h"
+#include "qemu/range.h"
 #include "tcg-target.h"
 #ifndef CONFIG_USER_ONLY
 #include "exec/hwaddr.h"
include/qom/cpu.h +4 -0
index 51a1323..c6bb6b6 100644
@@ -29,6 +29,7 @@
 #include "qemu/queue.h"
 #include "qemu/thread.h"
 #include "qemu/typedefs.h"
+#include "qemu/range.h"
 
 typedef int (*WriteCoreDumpFunction)(const void *buf, size_t size,
                                      void *opaque);
@@ -210,6 +211,9 @@ struct kvm_run;
 #define TB_JMP_CACHE_BITS 12
 #define TB_JMP_CACHE_SIZE (1 << TB_JMP_CACHE_BITS)
 
+/* Atomic insn translation TLB support. */
+#define EXCLUSIVE_RESET_ADDR ULLONG_MAX
+
 /**
  * CPUState:
  * @cpu_index: CPU index (informative).
@@ -329,6 +333,16 @@ struct CPUState {
      */
     bool throttle_thread_scheduled;
 
Quoted 11 lines

include/qom/cpu.h struct CPUState { · line 344

Quoted 9 lines
include/qom/cpu.h +9 -0
+    /* Used by the atomic insn translation backend. */
+    int ll_sc_context;
+    /* vCPU current exclusive addresses range.
+     * The address is set to EXCLUSIVE_RESET_ADDR if the vCPU is not.
+     * in the middle of a LL/SC. */
+    struct Range excl_protected_range;
+    /* Used to carry the SC result but also to flag a normal (legacy)
+     * store access made by a stcond (see softmmu_template.h). */
+    int excl_succeeded;

It might be clearer if excl_succeeded was defined as a bool?

Yes, that might be a good idea.

Quoted 33 lines
Quoted 20 lines
include/qom/cpu.h +0 -0
     /* Note that this is accessed at the start of every TB via a negative
        offset from AREG0.  Leave this field at the end so as to make the
        (absolute value) offset as small as possible.  This reduces code
softmmu_template.h +3 -4
index 6803890..24d29b7 100644
@@ -395,19 +395,54 @@ void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
         tlb_addr = env->tlb_table[mmu_idx][index].addr_write;
     }
 
-    /* Handle an IO access.  */
+    /* Handle an IO access or exclusive access.  */
     if (unlikely(tlb_addr & ~TARGET_PAGE_MASK)) {
-        CPUIOTLBEntry *iotlbentry;
-        if ((addr & (DATA_SIZE - 1)) != 0) {
-            goto do_unaligned_access;
+        CPUIOTLBEntry *iotlbentry = &env->iotlb[mmu_idx][index];
+

softmmu_template.h void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val, · line 411

Quoted 10 lines
softmmu_template.h +10 -0
+        if ((tlb_addr & ~TARGET_PAGE_MASK) == TLB_EXCL) {
+            CPUState *cpu = ENV_GET_CPU(env);
+            /* The slow-path has been forced since we are writing to
+             * exclusive-protected memory. */
+            hwaddr hw_addr = (iotlbentry->addr & TARGET_PAGE_MASK) + addr;
+
+            /* The function lookup_and_reset_cpus_ll_addr could have reset the
+             * exclusive address. Fail the SC in this case.
+             * N.B.: Here excl_succeeded == 0 means that helper_le_st_name has
+             * not been called by a softmmu_llsc_template.h. */

Could this be better worded (along with bool-ising) as:

Quoted 4 lines

"excl_succeeded is set by helper_le_st_name (softmmu_llsc_template)."

But having said that grepping for helper_le_st_name I see that's defined in softmmu_template.h so now the comments has confused me.

I see now that the comment refers to softmmu_llsc_template that will be created later on. Please consider this fixed.
In any case excl_succeeded, as the name suggests, is used by helper_stcond_name to know if the exclusive access went well. However, it is also used by softmmu_template to know whether we came from softmmu_llsc_template or not. This behaviour is pointed out in a comment is softmmu_llsc_template.

Quoted 3 lines

It also might be worth mentioning the subtly that exclusive addresses are based on the real hwaddr (hence the iotlb lookup?).

OK.

Quoted 9 lines

softmmu_template.h void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val, · line 416

Quoted 6 lines
softmmu_template.h +5 -0
+            if (cpu->excl_succeeded) {
+                if (cpu->excl_protected_range.begin != hw_addr) {
+                    /* The vCPU is SC-ing to an unprotected address. */
+                    cpu->excl_protected_range.begin = EXCLUSIVE_RESET_ADDR;
+                    cpu->excl_succeeded = 0;

cpu->excl_succeeded = false;

Quoted 16 lines

softmmu_template.h void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val, · line 429

Quoted 13 lines
softmmu_template.h +13 -0
+
+                    return;
+                }
+
+                cpu_physical_memory_unset_excl(hw_addr, cpu->cpu_index);
+            }
+
+            haddr = addr + env->tlb_table[mmu_idx][index].addend;
+        #if DATA_SIZE == 1
+            glue(glue(st, SUFFIX), _p)((uint8_t *)haddr, val);
+        #else
+            glue(glue(st, SUFFIX), _le_p)((uint8_t *)haddr, val);
+        #endif

Why the special casing for byte access? Isn't this something the glue + SUFFIX magic is meant to sort out?

Quoted 2 lines

For the byte access the byte ordering is irrelevant, in fact there is only one version of stb_p.

I'm just wondering why this little detail isn't hidden in the st_SUFFIX_le_p helpers rather than having to be explicit here.

Quoted 25 lines
Quoted 13 lines

softmmu_template.h void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val, · line 438

Quoted 11 lines
softmmu_template.h +9 -0
+
+            lookup_and_reset_cpus_ll_addr(hw_addr, DATA_SIZE);
+
+            return;
+        } else {
+            if ((addr & (DATA_SIZE - 1)) != 0) {
+                goto do_unaligned_access;
+            }
+            iotlbentry = &env->iotlb[mmu_idx][index];

Are we re-loading the TLB entry here?

Indeed, that should not be there (anyhow it will go away when we refactor this helper in patches 10,11,12).

Quoted 8 lines
Quoted 7 lines
softmmu_template.h +5 -0
+
+            /* ??? Note that the io helpers always read data in the target
+               byte ordering.  We should push the LE/BE request down into io.  */
+            val = TGT_LE(val);
+            glue(io_write, SUFFIX)(env, iotlbentry, val, addr,
 retaddr);
Quoted 5 lines
Quoted 2 lines

What happens if the software does and exclusive operation on a io address?

At this stage of the patch series such operations are not supported. Should I add an hw_error in case a software tries to do that?

If it is a known limitation then I think it might be worth it.

Quoted 28 lines

As
written above, patches 13 and 14 add the missing pieces to support exclusive operations to MMIO regions.

Quoted 17 lines
Quoted 17 lines
softmmu_template.h +1 -7
+            return;
         }
-        iotlbentry = &env->iotlb[mmu_idx][index];
-
-        /* ??? Note that the io helpers always read data in the target
-           byte ordering.  We should push the LE/BE request down into io.  */
-        val = TGT_LE(val);
-        glue(io_write, SUFFIX)(env, iotlbentry, val, addr, retaddr);
-        return;
     }
 
     /* Handle slow unaligned access (it spans two pages or IO).  */
softmmu_template.h +0 -0
@@ -475,19 +510,54 @@ void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
         tlb_addr = env->tlb_table[mmu_idx][index].addr_write;
     }
 
Quoted 6 lines

softmmu_template.h void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val, · line 513

Quoted 2 lines
softmmu_template.h +1 -1
-    /* Handle an IO access.  */
+    /* Handle an IO access or exclusive access.  */

Hmm there looks like a massive amount of duplication (not your fault, it was like that when you got here ;-) but maybe this can be re-factored away somehow?

Quoted 2 lines

That's true. This is why patches 10,11,12 try to alleviate this problem by making the code a little bit more compact and readable.

Although I was actually comparing the two helpers on the final tree state and there was loads of duplication still. If there is general re-factoring outside of the LL/SC case it would be worth having them at the start of the patch series so those improvements can get pulled into the tree and the LL/SC code is simpler when applied.

Quoted 69 lines

Regards,
alvise

Quoted 65 lines
Quoted 51 lines
softmmu_template.h +45 -3
     if (unlikely(tlb_addr & ~TARGET_PAGE_MASK)) {
-        CPUIOTLBEntry *iotlbentry;
-        if ((addr & (DATA_SIZE - 1)) != 0) {
-            goto do_unaligned_access;
+        CPUIOTLBEntry *iotlbentry = &env->iotlb[mmu_idx][index];
+
+        if ((tlb_addr & ~TARGET_PAGE_MASK) == TLB_EXCL) {
+            CPUState *cpu = ENV_GET_CPU(env);
+            /* The slow-path has been forced since we are writing to
+             * exclusive-protected memory. */
+            hwaddr hw_addr = (iotlbentry->addr & TARGET_PAGE_MASK) + addr;
+
+            /* The function lookup_and_reset_cpus_ll_addr could have reset the
+             * exclusive address. Fail the SC in this case.
+             * N.B.: Here excl_succeeded == 0 means that helper_le_st_name has
+             * not been called by a softmmu_llsc_template.h. */
+            if (cpu->excl_succeeded) {
+                if (cpu->excl_protected_range.begin != hw_addr) {
+                    /* The vCPU is SC-ing to an unprotected address. */
+                    cpu->excl_protected_range.begin = EXCLUSIVE_RESET_ADDR;
+                    cpu->excl_succeeded = 0;
+
+                    return;
+                }
+
+                cpu_physical_memory_unset_excl(hw_addr, cpu->cpu_index);
+            }
+
+            haddr = addr + env->tlb_table[mmu_idx][index].addend;
+        #if DATA_SIZE == 1
+            glue(glue(st, SUFFIX), _p)((uint8_t *)haddr, val);
+        #else
+            glue(glue(st, SUFFIX), _le_p)((uint8_t *)haddr, val);
+        #endif
+
+            lookup_and_reset_cpus_ll_addr(hw_addr, DATA_SIZE);
+
+            return;
+        } else {
+            if ((addr & (DATA_SIZE - 1)) != 0) {
+                goto do_unaligned_access;
+            }
+            iotlbentry = &env->iotlb[mmu_idx][index];
+
+            /* ??? Note that the io helpers always read data in the target
+               byte ordering.  We should push the LE/BE request down into io.  */
+            val = TGT_BE(val);
+            glue(io_write, SUFFIX)(env, iotlbentry, val, addr, retaddr);
+            return;
         }

softmmu_template.h void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val, · line 563

Quoted 10 lines
softmmu_template.h +0 -7
-        iotlbentry = &env->iotlb[mmu_idx][index];
-
-        /* ??? Note that the io helpers always read data in the target
-           byte ordering.  We should push the LE/BE request down into io.  */
-        val = TGT_BE(val);
-        glue(io_write, SUFFIX)(env, iotlbentry, val, addr, retaddr);
-        return;
     }
 
     /* Handle slow unaligned access (it spans two pages or IO).  */

--
Alex Bennée

--
Alex Bennée

#8 lore raw

RFC v6 3/14 Add CPUClass hook to set exclusive range

In reply to Alvise Rigo

Allow each architecture to set the exclusive range at any LoadLink operation through a CPUClass hook.
This comes in handy to emulate, for instance, the exclusive monitor implemented in some ARM architectures (more precisely, the Exclusive Reservation Granule).

---
 include/qom/cpu.h | 4 ++++
 qom/cpu.c         | 7 +++++++
 2 files changed, 11 insertions(+)
include/qom/cpu.h +4 -0
index c6bb6b6..9e409ce 100644
@@ -175,6 +175,10 @@ typedef struct CPUClass {
     void (*cpu_exec_exit)(CPUState *cpu);
     bool (*cpu_exec_interrupt)(CPUState *cpu, int interrupt_request);
 
+    /* Atomic instruction handling */
+    void (*cpu_set_excl_protected_range)(CPUState *cpu, hwaddr addr,
+                                         hwaddr size);
+
     void (*disas_set_info)(CPUState *cpu, disassemble_info *info);
 } CPUClass;
 
qom/cpu.c +7 -0
index fb80d13..a5c25a8 100644
@@ -203,6 +203,12 @@ static bool cpu_common_exec_interrupt(CPUState *cpu, int int_req)
     return false;
 }
 
+static void cpu_common_set_excl_range(CPUState *cpu, hwaddr addr, hwaddr size)
+{
+    cpu->excl_protected_range.begin = addr;
+    cpu->excl_protected_range.end = addr + size;
+}
+
 void cpu_dump_state(CPUState *cpu, FILE *f, fprintf_function cpu_fprintf,
                     int flags)
 {
@@ -355,6 +361,7 @@ static void cpu_class_init(ObjectClass *klass, void *data)
     k->cpu_exec_enter = cpu_common_noop;
     k->cpu_exec_exit = cpu_common_noop;
     k->cpu_exec_interrupt = cpu_common_exec_interrupt;
+    k->cpu_set_excl_protected_range = cpu_common_set_excl_range;
     dc->realize = cpu_common_realizefn;
     /*
      * Reason: CPUs still need special care by board code: wiring up
Signature · 1 line
2.6.4
Reviewed-by on v6 3/14
#9 lore raw

Re: [Qemu-devel] [RFC v6 03/14] Add CPUClass hook to set exclusive range

Quoted 2 lines · Alvise Rigo

Allow each architecture to set the exclusive range at any LoadLink operation through a CPUClass hook.

nit: space or continue paragraph.

Quoted 7 lines

This comes in handy to emulate, for instance, the exclusive monitor implemented in some ARM architectures (more precisely, the Exclusive Reservation Granule).

Quoted 38 lines
---
 include/qom/cpu.h | 4 ++++
 qom/cpu.c         | 7 +++++++
 2 files changed, 11 insertions(+)
include/qom/cpu.h +4 -0
index c6bb6b6..9e409ce 100644
@@ -175,6 +175,10 @@ typedef struct CPUClass {
     void (*cpu_exec_exit)(CPUState *cpu);
     bool (*cpu_exec_interrupt)(CPUState *cpu, int interrupt_request);
 
+    /* Atomic instruction handling */
+    void (*cpu_set_excl_protected_range)(CPUState *cpu, hwaddr addr,
+                                         hwaddr size);
+
     void (*disas_set_info)(CPUState *cpu, disassemble_info *info);
 } CPUClass;
 
qom/cpu.c +6 -0
index fb80d13..a5c25a8 100644
@@ -203,6 +203,12 @@ static bool cpu_common_exec_interrupt(CPUState *cpu, int int_req)
     return false;
 }
 
+static void cpu_common_set_excl_range(CPUState *cpu, hwaddr addr, hwaddr size)
+{
+    cpu->excl_protected_range.begin = addr;
+    cpu->excl_protected_range.end = addr + size;
+}
+
 void cpu_dump_state(CPUState *cpu, FILE *f, fprintf_function cpu_fprintf,
                     int flags)
 {

qom/cpu.c static void cpu_class_init(ObjectClass *klass, void *data) · line 367

Quoted 7 lines
qom/cpu.c +1 -0
@@ -355,6 +361,7 @@ static void cpu_class_init(ObjectClass *klass, void *data)
     k->cpu_exec_enter = cpu_common_noop;
     k->cpu_exec_exit = cpu_common_noop;
     k->cpu_exec_interrupt = cpu_common_exec_interrupt;
+    k->cpu_set_excl_protected_range = cpu_common_set_excl_range;
     dc->realize = cpu_common_realizefn;
     /*
      * Reason: CPUs still need special care by board code: wiring up

--
Alex Bennée

#10 lore raw

RFC v6 4/14 softmmu: Add helpers for a new slowpath

In reply to Alvise Rigo

The new helpers rely on the legacy ones to perform the actual read/write.

The LoadLink helper (helper_ldlink_name) prepares the way for the following SC operation. It sets the linked address and the size of the access.
These helper also update the TLB entry of the page involved in the LL/SC for those vCPUs that have the bit set (dirty), so that the following accesses made by all the vCPUs will follow the slow path.

The StoreConditional helper (helper_stcond_name) returns 1 if the store has to fail due to a concurrent access to the same page by another vCPU. A 'concurrent access' can be a store made by *any* vCPU (although, some implementations allow stores made by the CPU that issued the LoadLink).

---
 cputlb.c                |   3 ++
 softmmu_llsc_template.h | 134 ++++++++++++++++++++++++++++++++++++++++++++++++
 softmmu_template.h      |  12 +++++
 tcg/tcg.h               |  31 +++++++++++
 4 files changed, 180 insertions(+)
 create mode 100644 softmmu_llsc_template.h
cputlb.c +3 -0
index 7ee0c89..70b6404 100644
@@ -509,6 +509,8 @@ static inline void lookup_and_reset_cpus_ll_addr(hwaddr addr, hwaddr size)
 
 #define MMUSUFFIX _mmu
 
+/* Generates LoadLink/StoreConditional helpers in softmmu_template.h */
+#define GEN_EXCLUSIVE_HELPERS
 #define SHIFT 0
 #include "softmmu_template.h"
 
@@ -521,6 +523,7 @@ static inline void lookup_and_reset_cpus_ll_addr(hwaddr addr, hwaddr size)
 #define SHIFT 3
 #include "softmmu_template.h"
 #undef MMUSUFFIX
+#undef GEN_EXCLUSIVE_HELPERS
 
 #define MMUSUFFIX _cmmu
 #undef GETPC_ADJ
softmmu_llsc_template.h new file +134 -0

134 lines not on this page · raw message

softmmu_template.h +12 -0
index 24d29b7..d3d5902 100644
@@ -620,6 +620,18 @@ void probe_write(CPUArchState *env, target_ulong addr, int mmu_idx,
 #endif
 #endif /* !defined(SOFTMMU_CODE_ACCESS) */
 
+#ifdef GEN_EXCLUSIVE_HELPERS
+
+#if DATA_SIZE > 1 /* The 8-bit helpers are generate along with LE helpers */
+#define BIGENDIAN_EXCLUSIVE_HELPERS
+#include "softmmu_llsc_template.h"
+#undef BIGENDIAN_EXCLUSIVE_HELPERS
+#endif
+
+#include "softmmu_llsc_template.h"
+
+#endif /* !defined(GEN_EXCLUSIVE_HELPERS) */
+
 #undef READ_ACCESS_TYPE
 #undef SHIFT
 #undef DATA_TYPE
tcg/tcg.h +31 -0
index a696922..3e050a4 100644
@@ -968,6 +968,21 @@ tcg_target_ulong helper_be_ldul_mmu(CPUArchState *env, target_ulong addr,
                                     TCGMemOpIdx oi, uintptr_t retaddr);
 uint64_t helper_be_ldq_mmu(CPUArchState *env, target_ulong addr,
                            TCGMemOpIdx oi, uintptr_t retaddr);
+/* Exclusive variants */
+tcg_target_ulong helper_ret_ldlinkub_mmu(CPUArchState *env, target_ulong addr,
+                                            TCGMemOpIdx oi, uintptr_t retaddr);
+tcg_target_ulong helper_le_ldlinkuw_mmu(CPUArchState *env, target_ulong addr,
+                                            TCGMemOpIdx oi, uintptr_t retaddr);
+tcg_target_ulong helper_le_ldlinkul_mmu(CPUArchState *env, target_ulong addr,
+                                            TCGMemOpIdx oi, uintptr_t retaddr);
+uint64_t helper_le_ldlinkq_mmu(CPUArchState *env, target_ulong addr,
+                                            TCGMemOpIdx oi, uintptr_t retaddr);
+tcg_target_ulong helper_be_ldlinkuw_mmu(CPUArchState *env, target_ulong addr,
+                                            TCGMemOpIdx oi, uintptr_t retaddr);
+tcg_target_ulong helper_be_ldlinkul_mmu(CPUArchState *env, target_ulong addr,
+                                            TCGMemOpIdx oi, uintptr_t retaddr);
+uint64_t helper_be_ldlinkq_mmu(CPUArchState *env, target_ulong addr,
+                                            TCGMemOpIdx oi, uintptr_t retaddr);
 
 /* Value sign-extended to tcg register size.  */
 tcg_target_ulong helper_ret_ldsb_mmu(CPUArchState *env, target_ulong addr,
@@ -1010,6 +1025,22 @@ uint32_t helper_be_ldl_cmmu(CPUArchState *env, target_ulong addr,
                             TCGMemOpIdx oi, uintptr_t retaddr);
 uint64_t helper_be_ldq_cmmu(CPUArchState *env, target_ulong addr,
                             TCGMemOpIdx oi, uintptr_t retaddr);
+/* Exclusive variants */
+tcg_target_ulong helper_ret_stcondb_mmu(CPUArchState *env, target_ulong addr,
+                            uint8_t val, TCGMemOpIdx oi, uintptr_t retaddr);
+tcg_target_ulong helper_le_stcondw_mmu(CPUArchState *env, target_ulong addr,
+                            uint16_t val, TCGMemOpIdx oi, uintptr_t retaddr);
+tcg_target_ulong helper_le_stcondl_mmu(CPUArchState *env, target_ulong addr,
+                            uint32_t val, TCGMemOpIdx oi, uintptr_t retaddr);
+uint64_t helper_le_stcondq_mmu(CPUArchState *env, target_ulong addr,
+                            uint64_t val, TCGMemOpIdx oi, uintptr_t retaddr);
+tcg_target_ulong helper_be_stcondw_mmu(CPUArchState *env, target_ulong addr,
+                            uint16_t val, TCGMemOpIdx oi, uintptr_t retaddr);
+tcg_target_ulong helper_be_stcondl_mmu(CPUArchState *env, target_ulong addr,
+                            uint32_t val, TCGMemOpIdx oi, uintptr_t retaddr);
+uint64_t helper_be_stcondq_mmu(CPUArchState *env, target_ulong addr,
+                            uint64_t val, TCGMemOpIdx oi, uintptr_t retaddr);
+
 
 /* Temporary aliases until backends are converted.  */
 #ifdef TARGET_WORDS_BIGENDIAN
Signature · 1 line
2.6.4
#11 lore raw

Re: [Qemu-devel] [RFC v6 04/14] softmmu: Add helpers for a new slowpath

Quoted 5 lines · Alvise Rigo

The new helpers rely on the legacy ones to perform the actual read/write.

The LoadLink helper (helper_ldlink_name) prepares the way for the following SC operation. It sets the linked address and the size of the access.

nit: extra line or continue paragraph

Quoted 115 lines

These helper also update the TLB entry of the page involved in the LL/SC for those vCPUs that have the bit set (dirty), so that the following accesses made by all the vCPUs will follow the slow path.

The StoreConditional helper (helper_stcond_name) returns 1 if the store has to fail due to a concurrent access to the same page by another vCPU. A 'concurrent access' can be a store made by *any* vCPU (although, some implementations allow stores made by the CPU that issued the LoadLink).

---
 cputlb.c                |   3 ++
 softmmu_llsc_template.h | 134 ++++++++++++++++++++++++++++++++++++++++++++++++
 softmmu_template.h      |  12 +++++
 tcg/tcg.h               |  31 +++++++++++
 4 files changed, 180 insertions(+)
 create mode 100644 softmmu_llsc_template.h
cputlb.c +3 -0
index 7ee0c89..70b6404 100644
@@ -509,6 +509,8 @@ static inline void lookup_and_reset_cpus_ll_addr(hwaddr addr, hwaddr size)
 
 #define MMUSUFFIX _mmu
 
+/* Generates LoadLink/StoreConditional helpers in softmmu_template.h */
+#define GEN_EXCLUSIVE_HELPERS
 #define SHIFT 0
 #include "softmmu_template.h"
 
@@ -521,6 +523,7 @@ static inline void lookup_and_reset_cpus_ll_addr(hwaddr addr, hwaddr size)
 #define SHIFT 3
 #include "softmmu_template.h"
 #undef MMUSUFFIX
+#undef GEN_EXCLUSIVE_HELPERS
 
 #define MMUSUFFIX _cmmu
 #undef GETPC_ADJ
softmmu_llsc_template.h new file +67 -0
new file mode 100644
index 0000000..586bb2e
@@ -0,0 +1,134 @@
+/*
+ *  Software MMU support (esclusive load/store operations)
+ *
+ * Generate helpers used by TCG for qemu_ldlink/stcond ops.
+ *
+ * Included from softmmu_template.h only.
+ *
+ * Copyright (c) 2015 Virtual Open Systems
+ *
+ * Authors:
+ *  Alvise Rigo <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+/* This template does not generate together the le and be version, but only one
+ * of the two depending on whether BIGENDIAN_EXCLUSIVE_HELPERS has been set.
+ * The same nomenclature as softmmu_template.h is used for the exclusive
+ * helpers.  */
+
+#ifdef BIGENDIAN_EXCLUSIVE_HELPERS
+
+#define helper_ldlink_name  glue(glue(helper_be_ldlink, USUFFIX), MMUSUFFIX)
+#define helper_stcond_name  glue(glue(helper_be_stcond, SUFFIX), MMUSUFFIX)
+#define helper_ld glue(glue(helper_be_ld, USUFFIX), MMUSUFFIX)
+#define helper_st glue(glue(helper_be_st, SUFFIX), MMUSUFFIX)
+
+#else /* LE helpers + 8bit helpers (generated only once for both LE end BE) */
+
+#if DATA_SIZE > 1
+#define helper_ldlink_name  glue(glue(helper_le_ldlink, USUFFIX), MMUSUFFIX)
+#define helper_stcond_name  glue(glue(helper_le_stcond, SUFFIX), MMUSUFFIX)
+#define helper_ld glue(glue(helper_le_ld, USUFFIX), MMUSUFFIX)
+#define helper_st glue(glue(helper_le_st, SUFFIX), MMUSUFFIX)
+#else /* DATA_SIZE <= 1 */
+#define helper_ldlink_name  glue(glue(helper_ret_ldlink, USUFFIX), MMUSUFFIX)
+#define helper_stcond_name  glue(glue(helper_ret_stcond, SUFFIX), MMUSUFFIX)
+#define helper_ld glue(glue(helper_ret_ld, USUFFIX), MMUSUFFIX)
+#define helper_st glue(glue(helper_ret_st, SUFFIX), MMUSUFFIX)
+#endif
+
+#endif
+
+WORD_TYPE helper_ldlink_name(CPUArchState *env, target_ulong addr,
+                                TCGMemOpIdx oi, uintptr_t retaddr)
+{
+    WORD_TYPE ret;
+    int index;
+    CPUState *cpu, *this = ENV_GET_CPU(env);
+    CPUClass *cc = CPU_GET_CLASS(this);
+    hwaddr hw_addr;
+    unsigned mmu_idx = get_mmuidx(oi);
+
+    /* Use the proper load helper from cpu_ldst.h */
+    ret = helper_ld(env, addr, mmu_idx, retaddr);
+

softmmu_llsc_template.h line 77

Quoted 10 lines
softmmu_llsc_template.h new file +10 -0
+    index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
+
+    /* hw_addr = hwaddr of the page (i.e. section->mr->ram_addr + xlat)
+     * plus the offset (i.e. addr & ~TARGET_PAGE_MASK) */
+    hw_addr = (env->iotlb[mmu_idx][index].addr & TARGET_PAGE_MASK) + addr;
+
+    cpu_physical_memory_set_excl(hw_addr, this->cpu_index);
+    /* If all the vCPUs have the EXCL bit set for this page there is no need
+     * to request any flush. */
+    if (cpu_physical_memory_not_excl(hw_addr, smp_cpus)) {

Having done a double take reading this I think maybe a different helper for the smp_cpus case would be useful, maybe cpu_any_memory_not_excl().

softmmu_llsc_template.h line 79

Quoted 2 lines
softmmu_llsc_template.h new file +2 -0
+        CPU_FOREACH(cpu) {
+            if (current_cpu != cpu) {

At this point this == current_cpu? As we are skipping because we have already done the work above maybe we could be consistent and use this (this_cpu?) or current_cpu.

Quoted 5 lines
softmmu_llsc_template.h new file +5 -0
+                if (cpu_physical_memory_not_excl(hw_addr, cpu->cpu_index)) {
+                    cpu_physical_memory_set_excl(hw_addr, cpu->cpu_index);
+                    tlb_flush(cpu, 1);
+                }
+            }

softmmu_llsc_template.h line 94

Quoted 10 lines
softmmu_llsc_template.h new file +10 -0
+        }
+    }
+
+    cc->cpu_set_excl_protected_range(this, hw_addr, DATA_SIZE);
+
+    /* For this vCPU, just update the TLB entry, no need to flush. */
+    env->tlb_table[mmu_idx][index].addr_write |= TLB_EXCL;
+
+    /* From now on we are in LL/SC context */
+    this->ll_sc_context = 1;

apropos my previous emails, ll_sc_context should probably be a bool as well.

Quoted 21 lines
softmmu_llsc_template.h new file +21 -0
+
+    return ret;
+}
+
+WORD_TYPE helper_stcond_name(CPUArchState *env, target_ulong addr,
+                             DATA_TYPE val, TCGMemOpIdx oi,
+                             uintptr_t retaddr)
+{
+    WORD_TYPE ret;
+    unsigned mmu_idx = get_mmuidx(oi);
+    CPUState *cpu = ENV_GET_CPU(env);
+
+    if (!cpu->ll_sc_context) {
+        cpu->excl_succeeded = 0;
+        ret = 1;
+    } else {
+        /* We set it preventively to true to distinguish the following legacy
+         * access as one made by the store conditional wrapper. If the store
+         * conditional does not succeed, the value will be set to 0.*/
+        cpu->excl_succeeded = 1;
+        helper_st(env, addr, val, mmu_idx, retaddr);

softmmu_llsc_template.h line 125

Quoted 10 lines
softmmu_llsc_template.h new file +10 -0
+
+        if (cpu->excl_succeeded) {
+            cpu->excl_succeeded = 0;
+            ret = 0;
+        } else {
+            ret = 1;
+        }
+
+        /* Unset LL/SC context */
+        cpu->ll_sc_context = 0;

excl_succeeded and ll_sc_context should use bool values.

Quoted 71 lines
softmmu_llsc_template.h new file +9 -0
+    }
+
+    return ret;
+}
+
+#undef helper_ldlink_name
+#undef helper_stcond_name
+#undef helper_ld
+#undef helper_st
softmmu_template.h +12 -0
index 24d29b7..d3d5902 100644
@@ -620,6 +620,18 @@ void probe_write(CPUArchState *env, target_ulong addr, int mmu_idx,
 #endif
 #endif /* !defined(SOFTMMU_CODE_ACCESS) */
 
+#ifdef GEN_EXCLUSIVE_HELPERS
+
+#if DATA_SIZE > 1 /* The 8-bit helpers are generate along with LE helpers */
+#define BIGENDIAN_EXCLUSIVE_HELPERS
+#include "softmmu_llsc_template.h"
+#undef BIGENDIAN_EXCLUSIVE_HELPERS
+#endif
+
+#include "softmmu_llsc_template.h"
+
+#endif /* !defined(GEN_EXCLUSIVE_HELPERS) */
+
 #undef READ_ACCESS_TYPE
 #undef SHIFT
 #undef DATA_TYPE
tcg/tcg.h +24 -0
index a696922..3e050a4 100644
@@ -968,6 +968,21 @@ tcg_target_ulong helper_be_ldul_mmu(CPUArchState *env, target_ulong addr,
                                     TCGMemOpIdx oi, uintptr_t retaddr);
 uint64_t helper_be_ldq_mmu(CPUArchState *env, target_ulong addr,
                            TCGMemOpIdx oi, uintptr_t retaddr);
+/* Exclusive variants */
+tcg_target_ulong helper_ret_ldlinkub_mmu(CPUArchState *env, target_ulong addr,
+                                            TCGMemOpIdx oi, uintptr_t retaddr);
+tcg_target_ulong helper_le_ldlinkuw_mmu(CPUArchState *env, target_ulong addr,
+                                            TCGMemOpIdx oi, uintptr_t retaddr);
+tcg_target_ulong helper_le_ldlinkul_mmu(CPUArchState *env, target_ulong addr,
+                                            TCGMemOpIdx oi, uintptr_t retaddr);
+uint64_t helper_le_ldlinkq_mmu(CPUArchState *env, target_ulong addr,
+                                            TCGMemOpIdx oi, uintptr_t retaddr);
+tcg_target_ulong helper_be_ldlinkuw_mmu(CPUArchState *env, target_ulong addr,
+                                            TCGMemOpIdx oi, uintptr_t retaddr);
+tcg_target_ulong helper_be_ldlinkul_mmu(CPUArchState *env, target_ulong addr,
+                                            TCGMemOpIdx oi, uintptr_t retaddr);
+uint64_t helper_be_ldlinkq_mmu(CPUArchState *env, target_ulong addr,
+                                            TCGMemOpIdx oi, uintptr_t retaddr);
 
 /* Value sign-extended to tcg register size.  */
 tcg_target_ulong helper_ret_ldsb_mmu(CPUArchState *env, target_ulong addr,
@@ -1010,6 +1025,22 @@ uint32_t helper_be_ldl_cmmu(CPUArchState *env, target_ulong addr,
                             TCGMemOpIdx oi, uintptr_t retaddr);
 uint64_t helper_be_ldq_cmmu(CPUArchState *env, target_ulong addr,
                             TCGMemOpIdx oi, uintptr_t retaddr);
+/* Exclusive variants */
+tcg_target_ulong helper_ret_stcondb_mmu(CPUArchState *env, target_ulong addr,
+                            uint8_t val, TCGMemOpIdx oi, uintptr_t retaddr);
+tcg_target_ulong helper_le_stcondw_mmu(CPUArchState *env, target_ulong addr,
+                            uint16_t val, TCGMemOpIdx oi, uintptr_t retaddr);
+tcg_target_ulong helper_le_stcondl_mmu(CPUArchState *env, target_ulong addr,
+                            uint32_t val, TCGMemOpIdx oi, uintptr_t retaddr);
+uint64_t helper_le_stcondq_mmu(CPUArchState *env, target_ulong addr,
+                            uint64_t val, TCGMemOpIdx oi, uintptr_t retaddr);

tcg/tcg.h uint32_t helper_be_ldl_cmmu(CPUArchState *env, target_ulong addr, · line 1046

Quoted 10 lines
tcg/tcg.h +7 -0
+tcg_target_ulong helper_be_stcondw_mmu(CPUArchState *env, target_ulong addr,
+                            uint16_t val, TCGMemOpIdx oi, uintptr_t retaddr);
+tcg_target_ulong helper_be_stcondl_mmu(CPUArchState *env, target_ulong addr,
+                            uint32_t val, TCGMemOpIdx oi, uintptr_t retaddr);
+uint64_t helper_be_stcondq_mmu(CPUArchState *env, target_ulong addr,
+                            uint64_t val, TCGMemOpIdx oi, uintptr_t retaddr);
+
 
 /* Temporary aliases until backends are converted.  */
 #ifdef TARGET_WORDS_BIGENDIAN

--
Alex Bennée

#12 lore raw

RFC v6 5/14 tcg: Create new runtime helpers for excl accesses

In reply to Alvise Rigo

Introduce a set of new runtime helpers do handle exclusive instructions. This helpers are used as hooks to call the respective LL/SC helpers in softmmu_llsc_template.h from TCG code.

---
 Makefile.target             |   2 +-
 include/exec/helper-gen.h   |   1 +
 include/exec/helper-proto.h |   1 +
 include/exec/helper-tcg.h   |   1 +
 tcg-llsc-helper.c           | 109 ++++++++++++++++++++++++++++++++++++++++++++
 tcg-llsc-helper.h           |  35 ++++++++++++++
 tcg/tcg-llsc-gen-helper.h   |  32 +++++++++++++
 7 files changed, 180 insertions(+), 1 deletion(-)
 create mode 100644 tcg-llsc-helper.c
 create mode 100644 tcg-llsc-helper.h
 create mode 100644 tcg/tcg-llsc-gen-helper.h
Makefile.target +1 -1
index 962d004..e9fbcdc 100644
@@ -135,7 +135,7 @@ obj-y += arch_init.o cpus.o monitor.o gdbstub.o balloon.o ioport.o numa.o
 obj-y += qtest.o bootdevice.o
 obj-y += hw/
 obj-$(CONFIG_KVM) += kvm-all.o
-obj-y += memory.o cputlb.o
+obj-y += memory.o cputlb.o tcg-llsc-helper.o
 obj-y += memory_mapping.o
 obj-y += dump.o
 obj-y += migration/ram.o migration/savevm.o
include/exec/helper-gen.h +1 -0
index 0d0da3a..d45cdad2 100644
@@ -60,6 +60,7 @@ static inline void glue(gen_helper_, name)(dh_retvar_decl(ret)          \
 #include "trace/generated-helpers.h"
 #include "trace/generated-helpers-wrappers.h"
 #include "tcg-runtime.h"
+#include "tcg-llsc-gen-helper.h"
 
 #undef DEF_HELPER_FLAGS_0
 #undef DEF_HELPER_FLAGS_1
include/exec/helper-proto.h +1 -0
index effdd43..90be2fd 100644
@@ -29,6 +29,7 @@ dh_ctype(ret) HELPER(name) (dh_ctype(t1), dh_ctype(t2), dh_ctype(t3), \
 #include "helper.h"
 #include "trace/generated-helpers.h"
 #include "tcg-runtime.h"
+#include "tcg/tcg-llsc-gen-helper.h"
 
 #undef DEF_HELPER_FLAGS_0
 #undef DEF_HELPER_FLAGS_1
include/exec/helper-tcg.h +1 -0
index 79fa3c8..0c0dc71 100644
@@ -38,6 +38,7 @@
 #include "helper.h"
 #include "trace/generated-helpers.h"
 #include "tcg-runtime.h"
+#include "tcg-llsc-gen-helper.h"
 
 #undef DEF_HELPER_FLAGS_0
 #undef DEF_HELPER_FLAGS_1
tcg-llsc-helper.c new file +109 -0

109 lines not on this page · raw message

tcg-llsc-helper.h new file +35 -0
new file mode 100644
index 0000000..bbe42c3
@@ -0,0 +1,35 @@
+#ifndef HELPER_LLSC_HEAD_H
+#define HELPER_LLSC_HEAD_H 1
+
+uint32_t HELPER(ldlink_aa32_i8)(CPUArchState *env, uint32_t addr,
+                                uint32_t index);
+uint32_t HELPER(ldlink_aa32_i16_be)(CPUArchState *env, uint32_t addr,
+                                    uint32_t index);
+uint32_t HELPER(ldlink_aa32_i32_be)(CPUArchState *env, uint32_t addr,
+                                    uint32_t index);
+uint64_t HELPER(ldlink_aa32_i64_be)(CPUArchState *env, uint32_t addr,
+                                    uint32_t index);
+uint32_t HELPER(ldlink_aa32_i16_le)(CPUArchState *env, uint32_t addr,
+                                    uint32_t index);
+uint32_t HELPER(ldlink_aa32_i32_le)(CPUArchState *env, uint32_t addr,
+                                    uint32_t index);
+uint64_t HELPER(ldlink_aa32_i64_le)(CPUArchState *env, uint32_t addr,
+                                    uint32_t index);
+
+
+uint32_t HELPER(stcond_aa32_i8)(CPUArchState *env, uint32_t addr,
+                                uint32_t val, uint32_t index);
+uint32_t HELPER(stcond_aa32_i16_be)(CPUArchState *env, uint32_t addr,
+                                    uint32_t val, uint32_t index);
+uint32_t HELPER(stcond_aa32_i32_be)(CPUArchState *env, uint32_t addr,
+                                    uint32_t val, uint32_t index);
+uint32_t HELPER(stcond_aa32_i64_be)(CPUArchState *env, uint32_t addr,
+                                    uint64_t val, uint32_t index);
+uint32_t HELPER(stcond_aa32_i16_le)(CPUArchState *env, uint32_t addr,
+                                    uint32_t val, uint32_t index);
+uint32_t HELPER(stcond_aa32_i32_le)(CPUArchState *env, uint32_t addr,
+                                    uint32_t val, uint32_t index);
+uint32_t HELPER(stcond_aa32_i64_le)(CPUArchState *env, uint32_t addr,
+                                    uint64_t val, uint32_t index);
+
+#endif
tcg/tcg-llsc-gen-helper.h new file +32 -0
new file mode 100644
index 0000000..2b647cd
@@ -0,0 +1,32 @@
+DEF_HELPER_3(ldlink_aa32_i8, i32, env, i32, i32)
+DEF_HELPER_3(ldlink_aa32_i16_be, i32, env, i32, i32)
+DEF_HELPER_3(ldlink_aa32_i32_be, i32, env, i32, i32)
+DEF_HELPER_3(ldlink_aa32_i64_be, i64, env, i32, i32)
+DEF_HELPER_3(ldlink_aa32_i16_le, i32, env, i32, i32)
+DEF_HELPER_3(ldlink_aa32_i32_le, i32, env, i32, i32)
+DEF_HELPER_3(ldlink_aa32_i64_le, i64, env, i32, i32)
+
+DEF_HELPER_4(stcond_aa32_i8, i32, env, i32, i32, i32)
+DEF_HELPER_4(stcond_aa32_i16_be, i32, env, i32, i32, i32)
+DEF_HELPER_4(stcond_aa32_i32_be, i32, env, i32, i32, i32)
+DEF_HELPER_4(stcond_aa32_i64_be, i32, env, i32, i64, i32)
+DEF_HELPER_4(stcond_aa32_i16_le, i32, env, i32, i32, i32)
+DEF_HELPER_4(stcond_aa32_i32_le, i32, env, i32, i32, i32)
+DEF_HELPER_4(stcond_aa32_i64_le, i32, env, i32, i64, i32)
+
+/* Convenient aliases */
+#ifdef TARGET_WORDS_BIGENDIAN
+#define gen_helper_stcond_aa32_i16 gen_helper_stcond_aa32_i16_be
+#define gen_helper_stcond_aa32_i32 gen_helper_stcond_aa32_i32_be
+#define gen_helper_stcond_aa32_i64 gen_helper_stcond_aa32_i64_be
+#define gen_helper_ldlink_aa32_i16 gen_helper_ldlink_aa32_i16_be
+#define gen_helper_ldlink_aa32_i32 gen_helper_ldlink_aa32_i32_be
+#define gen_helper_ldlink_aa32_i64 gen_helper_ldlink_aa32_i64_be
+#else
+#define gen_helper_stcond_aa32_i16 gen_helper_stcond_aa32_i16_le
+#define gen_helper_stcond_aa32_i32 gen_helper_stcond_aa32_i32_le
+#define gen_helper_stcond_aa32_i64 gen_helper_stcond_aa32_i64_le
+#define gen_helper_ldlink_aa32_i16 gen_helper_ldlink_aa32_i16_le
+#define gen_helper_ldlink_aa32_i32 gen_helper_ldlink_aa32_i32_le
+#define gen_helper_ldlink_aa32_i64 gen_helper_ldlink_aa32_i64_le
+#endif
Signature · 1 line
2.6.4
#13 lore raw

Re: [Qemu-devel] [RFC v6 05/14] tcg: Create new runtime helpers for excl accesses

tcg/tcg-llsc-gen-helper.h line 7

Quoted 13 lines · Alvise Rigo
tcg/tcg-llsc-gen-helper.h new file +7 -0
new file mode 100644
index 0000000..2b647cd
@@ -0,0 +1,32 @@
+DEF_HELPER_3(ldlink_aa32_i8, i32, env, i32, i32)
+DEF_HELPER_3(ldlink_aa32_i16_be, i32, env, i32, i32)
+DEF_HELPER_3(ldlink_aa32_i32_be, i32, env, i32, i32)
+DEF_HELPER_3(ldlink_aa32_i64_be, i64, env, i32, i32)
+DEF_HELPER_3(ldlink_aa32_i16_le, i32, env, i32, i32)
+DEF_HELPER_3(ldlink_aa32_i32_le, i32, env, i32, i32)
+DEF_HELPER_3(ldlink_aa32_i64_le, i64, env, i32, i32)

"aa32" probably should be removed?

Paolo

Quoted 1 line
tcg/tcg-llsc-gen-helper.h new file +1 -0
+DEF_HELPER_4(stcond_aa32_i8, i32, env, i32, i32, i32)
#14 lore raw

RFC v6 6/14 configure: Use slow-path for atomic only when the softmmu is enabled

In reply to Alvise Rigo

Use the new slow path for atomic instruction translation when the softmmu is enabled.

---
 configure | 4 ++++
 1 file changed, 4 insertions(+)
configure +4 -0
index b9552fd..cc3891a 100755
@@ -4794,6 +4794,7 @@ echo "Install blobs     $blobs"
 echo "KVM support       $kvm"
 echo "RDMA support      $rdma"
 echo "TCG interpreter   $tcg_interpreter"
+echo "use ld/st excl    $softmmu"
 echo "fdt support       $fdt"
 echo "preadv support    $preadv"
 echo "fdatasync         $fdatasync"
@@ -5186,6 +5187,9 @@ fi
 if test "$tcg_interpreter" = "yes" ; then
   echo "CONFIG_TCG_INTERPRETER=y" >> $config_host_mak
 fi
+if test "$softmmu" = "yes" ; then
+  echo "CONFIG_TCG_USE_LDST_EXCL=y" >> $config_host_mak
+fi
 if test "$fdatasync" = "yes" ; then
   echo "CONFIG_FDATASYNC=y" >> $config_host_mak
 fi
Signature · 1 line
2.6.4
#15 lore raw

Re: [Qemu-devel] [RFC v6 06/14] configure: Use slow-path for atomic only when the softmmu is enabled

Quoted 23 lines · Alvise Rigo

Use the new slow path for atomic instruction translation when the softmmu is enabled.

---
 configure | 4 ++++
 1 file changed, 4 insertions(+)
configure +1 -0
index b9552fd..cc3891a 100755
@@ -4794,6 +4794,7 @@ echo "Install blobs     $blobs"
 echo "KVM support       $kvm"
 echo "RDMA support      $rdma"
 echo "TCG interpreter   $tcg_interpreter"
+echo "use ld/st excl    $softmmu"
 echo "fdt support       $fdt"
 echo "preadv support    $preadv"
 echo "fdatasync         $fdatasync"

configure fi · line 5192

Quoted 6 lines
configure +3 -0
@@ -5186,6 +5187,9 @@ fi
 if test "$tcg_interpreter" = "yes" ; then
   echo "CONFIG_TCG_INTERPRETER=y" >> $config_host_mak
 fi
+if test "$softmmu" = "yes" ; then
+  echo "CONFIG_TCG_USE_LDST_EXCL=y" >> $config_host_mak
+fi

Just use CONFIG_SOFTMMU in translate.c, no?

A target other than ARM might need ll/sc in user-mode emulation as well.

Paolo

Quoted 4 lines
configure +0 -0
 if test "$fdatasync" = "yes" ; then
   echo "CONFIG_FDATASYNC=y" >> $config_host_mak
 fi
#16 lore raw

Re: [Qemu-devel] [RFC v6 06/14] configure: Use slow-path for atomic only when the softmmu is enabled

Quoted 6 lines · Paolo Bonzini
Quoted 3 lines

configure fi · line 5192

Quoted 3 lines
configure +3 -0
+if test "$softmmu" = "yes" ; then
+  echo "CONFIG_TCG_USE_LDST_EXCL=y" >> $config_host_mak
+fi

Just use CONFIG_SOFTMMU in translate.c, no?

A target other than ARM might need ll/sc in user-mode emulation as well.

Sorry, that makes no sense. Couldn't hit cancel in time. :)

Paolo

#17 lore raw

Re: [Qemu-devel] [RFC v6 06/14] configure: Use slow-path for atomic only when the softmmu is enabled

In reply to Alvise Rigo
Quoted 23 lines · Alvise Rigo

Use the new slow path for atomic instruction translation when the softmmu is enabled.

---
 configure | 4 ++++
 1 file changed, 4 insertions(+)
configure +1 -0
index b9552fd..cc3891a 100755
@@ -4794,6 +4794,7 @@ echo "Install blobs     $blobs"
 echo "KVM support       $kvm"
 echo "RDMA support      $rdma"
 echo "TCG interpreter   $tcg_interpreter"
+echo "use ld/st excl    $softmmu"
 echo "fdt support       $fdt"
 echo "preadv support    $preadv"
 echo "fdatasync         $fdatasync"

configure fi · line 5192

Quoted 6 lines
configure +3 -0
@@ -5186,6 +5187,9 @@ fi
 if test "$tcg_interpreter" = "yes" ; then
   echo "CONFIG_TCG_INTERPRETER=y" >> $config_host_mak
 fi
+if test "$softmmu" = "yes" ; then
+  echo "CONFIG_TCG_USE_LDST_EXCL=y" >> $config_host_mak
+fi

why is this "$softmmu" and not "$target_softmmu" ?

Quoted 4 lines
configure +0 -0
 if test "$fdatasync" = "yes" ; then
   echo "CONFIG_FDATASYNC=y" >> $config_host_mak
 fi
#18 lore raw

Re: [Qemu-devel] [RFC v6 06/14] configure: Use slow-path for atomic only when the softmmu is enabled

Hi,

Quoted 26 lines · Laurent Vivier
Quoted 23 lines · Alvise Rigo

Use the new slow path for atomic instruction translation when the softmmu is enabled.

---
 configure | 4 ++++
 1 file changed, 4 insertions(+)
configure +1 -0
index b9552fd..cc3891a 100755
@@ -4794,6 +4794,7 @@ echo "Install blobs     $blobs"
 echo "KVM support       $kvm"
 echo "RDMA support      $rdma"
 echo "TCG interpreter   $tcg_interpreter"
+echo "use ld/st excl    $softmmu"
 echo "fdt support       $fdt"
 echo "preadv support    $preadv"
 echo "fdatasync         $fdatasync"
@@ -5186,6 +5187,9 @@ fi
Quoted 8 lines

configure fi · line 5192

Quoted 6 lines
configure +3 -0
 if test "$tcg_interpreter" = "yes" ; then
   echo "CONFIG_TCG_INTERPRETER=y" >> $config_host_mak
 fi
+if test "$softmmu" = "yes" ; then
+  echo "CONFIG_TCG_USE_LDST_EXCL=y" >> $config_host_mak
+fi

why is this "$softmmu" and not "$target_softmmu" ?

I see now that is $target_softmmu setting CONFIG_SOFTMMU=y. So for my understanding, which are the cases where $softmmu is set while $target_softmmu is not?

Thank you,
alvise

Quoted 5 lines
Quoted 5 lines
configure +0 -0
 if test "$fdatasync" = "yes" ; then
   echo "CONFIG_FDATASYNC=y" >> $config_host_mak
 fi
#19 lore raw

Re: [Qemu-devel] [RFC v6 06/14] configure: Use slow-path for atomic only when the softmmu is enabled

Quoted 38 lines · alvise rigo

Hi,

Quoted 26 lines · Laurent Vivier
Quoted 23 lines · Alvise Rigo

Use the new slow path for atomic instruction translation when the softmmu is enabled.

---
 configure | 4 ++++
 1 file changed, 4 insertions(+)
configure +1 -0
index b9552fd..cc3891a 100755
@@ -4794,6 +4794,7 @@ echo "Install blobs     $blobs"
 echo "KVM support       $kvm"
 echo "RDMA support      $rdma"
 echo "TCG interpreter   $tcg_interpreter"
+echo "use ld/st excl    $softmmu"
 echo "fdt support       $fdt"
 echo "preadv support    $preadv"
 echo "fdatasync         $fdatasync"
@@ -5186,6 +5187,9 @@ fi
Quoted 8 lines

configure fi · line 5192

Quoted 6 lines
configure +3 -0
 if test "$tcg_interpreter" = "yes" ; then
   echo "CONFIG_TCG_INTERPRETER=y" >> $config_host_mak
 fi
+if test "$softmmu" = "yes" ; then
+  echo "CONFIG_TCG_USE_LDST_EXCL=y" >> $config_host_mak
+fi

why is this "$softmmu" and not "$target_softmmu" ?

Quoted 3 lines

I see now that is $target_softmmu setting CONFIG_SOFTMMU=y. So for my understanding, which are the cases where $softmmu is set while $target_softmmu is not?

When compiling foo-linux-user.

Paolo

#20 lore raw

Re: [Qemu-devel] [RFC v6 06/14] configure: Use slow-path for atomic only when the softmmu is enabled

Quoted 41 lines · Paolo Bonzini
Quoted 38 lines · alvise rigo

Hi,

Quoted 34 lines · Laurent Vivier
Quoted 23 lines · Alvise Rigo

Use the new slow path for atomic instruction translation when the softmmu is enabled.

---
 configure | 4 ++++
 1 file changed, 4 insertions(+)
configure +1 -0
index b9552fd..cc3891a 100755
@@ -4794,6 +4794,7 @@ echo "Install blobs     $blobs"
 echo "KVM support       $kvm"
 echo "RDMA support      $rdma"
 echo "TCG interpreter   $tcg_interpreter"
+echo "use ld/st excl    $softmmu"
 echo "fdt support       $fdt"
 echo "preadv support    $preadv"
 echo "fdatasync         $fdatasync"

configure fi · line 5192

Quoted 6 lines
configure +3 -0
@@ -5186,6 +5187,9 @@ fi
 if test "$tcg_interpreter" = "yes" ; then
   echo "CONFIG_TCG_INTERPRETER=y" >> $config_host_mak
 fi
+if test "$softmmu" = "yes" ; then
+  echo "CONFIG_TCG_USE_LDST_EXCL=y" >> $config_host_mak
+fi

why is this "$softmmu" and not "$target_softmmu" ?

Quoted 5 lines
Quoted 3 lines

I see now that is $target_softmmu setting CONFIG_SOFTMMU=y. So for my understanding, which are the cases where $softmmu is set while $target_softmmu is not?

When compiling foo-linux-user.

In fact, after having asked the question, I've found that it is right to use softmmu which is a host parameter (while target_softmmu is guest parameter) and softmmu is set to true if there is at least one *-softmmu target.

Laurent

#21 lore raw

RFC v6 7/14 target-arm: translate: Use ld/st excl for atomic insns

In reply to Alvise Rigo

Use the new LL/SC runtime helpers to handle the ARM atomic instructions in softmmu_llsc_template.h.

In general, the helper generator gen_helper_{ldlink,stcond}_aa32_i{8,16,32,64}() calls the function helper_{le,be}_{ldlink,stcond}{ub,uw,ulq}_mmu() implemented in softmmu_llsc_template.h.

---
 target-arm/translate.c | 101 +++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 97 insertions(+), 4 deletions(-)
target-arm/translate.c +97 -4
index 5d22879..e88d8a3 100644
@@ -64,8 +64,10 @@ TCGv_ptr cpu_env;
 static TCGv_i64 cpu_V0, cpu_V1, cpu_M0;
 static TCGv_i32 cpu_R[16];
 TCGv_i32 cpu_CF, cpu_NF, cpu_VF, cpu_ZF;
+#ifndef CONFIG_TCG_USE_LDST_EXCL
 TCGv_i64 cpu_exclusive_addr;
 TCGv_i64 cpu_exclusive_val;
+#endif
 #ifdef CONFIG_USER_ONLY
 TCGv_i64 cpu_exclusive_test;
 TCGv_i32 cpu_exclusive_info;
@@ -98,10 +100,12 @@ void arm_translate_init(void)
     cpu_VF = tcg_global_mem_new_i32(TCG_AREG0, offsetof(CPUARMState, VF), "VF");
     cpu_ZF = tcg_global_mem_new_i32(TCG_AREG0, offsetof(CPUARMState, ZF), "ZF");
 
+#ifndef CONFIG_TCG_USE_LDST_EXCL
     cpu_exclusive_addr = tcg_global_mem_new_i64(TCG_AREG0,
         offsetof(CPUARMState, exclusive_addr), "exclusive_addr");
     cpu_exclusive_val = tcg_global_mem_new_i64(TCG_AREG0,
         offsetof(CPUARMState, exclusive_val), "exclusive_val");
+#endif
 #ifdef CONFIG_USER_ONLY
     cpu_exclusive_test = tcg_global_mem_new_i64(TCG_AREG0,
         offsetof(CPUARMState, exclusive_test), "exclusive_test");
@@ -7414,15 +7418,59 @@ static void gen_logicq_cc(TCGv_i32 lo, TCGv_i32 hi)
     tcg_gen_or_i32(cpu_ZF, lo, hi);
 }
 
-/* Load/Store exclusive instructions are implemented by remembering
+/* If the softmmu is enabled, the translation of Load/Store exclusive
+ * instructions will rely on the gen_helper_{ldlink,stcond} helpers,
+ * offloading most of the work to the softmmu_llsc_template.h functions.
+
+   Otherwise, these instructions are implemented by remembering
    the value/address loaded, and seeing if these are the same
    when the store is performed. This should be sufficient to implement
    the architecturally mandated semantics, and avoids having to monitor
    regular stores.
 
-   In system emulation mode only one CPU will be running at once, so
-   this sequence is effectively atomic.  In user emulation mode we
-   throw an exception and handle the atomic operation elsewhere.  */
+   In user emulation mode we throw an exception and handle the atomic
+   operation elsewhere.  */
+#ifdef CONFIG_TCG_USE_LDST_EXCL
+static void gen_load_exclusive(DisasContext *s, int rt, int rt2,
+                               TCGv_i32 addr, int size)
+ {
+    TCGv_i32 tmp = tcg_temp_new_i32();
+    TCGv_i32 mem_idx = tcg_temp_new_i32();
+
+    tcg_gen_movi_i32(mem_idx, get_mem_index(s));
+
+    if (size != 3) {
+        switch (size) {
+        case 0:
+            gen_helper_ldlink_aa32_i8(tmp, cpu_env, addr, mem_idx);
+            break;
+        case 1:
+            gen_helper_ldlink_aa32_i16(tmp, cpu_env, addr, mem_idx);
+            break;
+        case 2:
+            gen_helper_ldlink_aa32_i32(tmp, cpu_env, addr, mem_idx);
+            break;
+        default:
+            abort();
+        }
+
+        store_reg(s, rt, tmp);
+    } else {
+        TCGv_i64 tmp64 = tcg_temp_new_i64();
+        TCGv_i32 tmph = tcg_temp_new_i32();
+
+        gen_helper_ldlink_aa32_i64(tmp64, cpu_env, addr, mem_idx);
+        tcg_gen_extr_i64_i32(tmp, tmph, tmp64);
+
+        store_reg(s, rt, tmp);
+        store_reg(s, rt2, tmph);
+
+        tcg_temp_free_i64(tmp64);
+    }
+
+    tcg_temp_free_i32(mem_idx);
+}
+#else
 static void gen_load_exclusive(DisasContext *s, int rt, int rt2,
                                TCGv_i32 addr, int size)
 {
@@ -7461,10 +7509,14 @@ static void gen_load_exclusive(DisasContext *s, int rt, int rt2,
     store_reg(s, rt, tmp);
     tcg_gen_extu_i32_i64(cpu_exclusive_addr, addr);
 }
+#endif
 
 static void gen_clrex(DisasContext *s)
 {
+#ifdef CONFIG_TCG_USE_LDST_EXCL
+#else
     tcg_gen_movi_i64(cpu_exclusive_addr, -1);
+#endif
 }
 
 #ifdef CONFIG_USER_ONLY
@@ -7476,6 +7528,47 @@ static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
                      size | (rd << 4) | (rt << 8) | (rt2 << 12));
     gen_exception_internal_insn(s, 4, EXCP_STREX);
 }
+#elif defined CONFIG_TCG_USE_LDST_EXCL
+static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
+                                TCGv_i32 addr, int size)
+{
+    TCGv_i32 tmp, mem_idx;
+
+    mem_idx = tcg_temp_new_i32();
+
+    tcg_gen_movi_i32(mem_idx, get_mem_index(s));
+    tmp = load_reg(s, rt);
+
+    if (size != 3) {
+        switch (size) {
+        case 0:
+            gen_helper_stcond_aa32_i8(cpu_R[rd], cpu_env, addr, tmp, mem_idx);
+            break;
+        case 1:
+            gen_helper_stcond_aa32_i16(cpu_R[rd], cpu_env, addr, tmp, mem_idx);
+            break;
+        case 2:
+            gen_helper_stcond_aa32_i32(cpu_R[rd], cpu_env, addr, tmp, mem_idx);
+            break;
+        default:
+            abort();
+        }
+    } else {
+        TCGv_i64 tmp64;
+        TCGv_i32 tmp2;
+
+        tmp64 = tcg_temp_new_i64();
+        tmp2 = load_reg(s, rt2);
+        tcg_gen_concat_i32_i64(tmp64, tmp, tmp2);
+        gen_helper_stcond_aa32_i64(cpu_R[rd], cpu_env, addr, tmp64, mem_idx);
+
+        tcg_temp_free_i32(tmp2);
+        tcg_temp_free_i64(tmp64);
+    }
+
+    tcg_temp_free_i32(tmp);
+    tcg_temp_free_i32(mem_idx);
+}
 #else
 static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
                                 TCGv_i32 addr, int size)
Signature · 1 line
2.6.4
#22 lore raw

Re: [Qemu-devel] [RFC v6 07/14] target-arm: translate: Use ld/st excl for atomic insns

Quoted 108 lines · Alvise Rigo

Use the new LL/SC runtime helpers to handle the ARM atomic instructions in softmmu_llsc_template.h.

In general, the helper generator gen_helper_{ldlink,stcond}_aa32_i{8,16,32,64}() calls the function helper_{le,be}_{ldlink,stcond}{ub,uw,ulq}_mmu() implemented in softmmu_llsc_template.h.

---
 target-arm/translate.c | 101 +++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 97 insertions(+), 4 deletions(-)
target-arm/translate.c +52 -4
index 5d22879..e88d8a3 100644
@@ -64,8 +64,10 @@ TCGv_ptr cpu_env;
 static TCGv_i64 cpu_V0, cpu_V1, cpu_M0;
 static TCGv_i32 cpu_R[16];
 TCGv_i32 cpu_CF, cpu_NF, cpu_VF, cpu_ZF;
+#ifndef CONFIG_TCG_USE_LDST_EXCL
 TCGv_i64 cpu_exclusive_addr;
 TCGv_i64 cpu_exclusive_val;
+#endif
 #ifdef CONFIG_USER_ONLY
 TCGv_i64 cpu_exclusive_test;
 TCGv_i32 cpu_exclusive_info;
@@ -98,10 +100,12 @@ void arm_translate_init(void)
     cpu_VF = tcg_global_mem_new_i32(TCG_AREG0, offsetof(CPUARMState, VF), "VF");
     cpu_ZF = tcg_global_mem_new_i32(TCG_AREG0, offsetof(CPUARMState, ZF), "ZF");
 
+#ifndef CONFIG_TCG_USE_LDST_EXCL
     cpu_exclusive_addr = tcg_global_mem_new_i64(TCG_AREG0,
         offsetof(CPUARMState, exclusive_addr), "exclusive_addr");
     cpu_exclusive_val = tcg_global_mem_new_i64(TCG_AREG0,
         offsetof(CPUARMState, exclusive_val), "exclusive_val");
+#endif
 #ifdef CONFIG_USER_ONLY
     cpu_exclusive_test = tcg_global_mem_new_i64(TCG_AREG0,
         offsetof(CPUARMState, exclusive_test), "exclusive_test");
@@ -7414,15 +7418,59 @@ static void gen_logicq_cc(TCGv_i32 lo, TCGv_i32 hi)
     tcg_gen_or_i32(cpu_ZF, lo, hi);
 }
 
-/* Load/Store exclusive instructions are implemented by remembering
+/* If the softmmu is enabled, the translation of Load/Store exclusive
+ * instructions will rely on the gen_helper_{ldlink,stcond} helpers,
+ * offloading most of the work to the softmmu_llsc_template.h functions.
+
+   Otherwise, these instructions are implemented by remembering
    the value/address loaded, and seeing if these are the same
    when the store is performed. This should be sufficient to implement
    the architecturally mandated semantics, and avoids having to monitor
    regular stores.
 
-   In system emulation mode only one CPU will be running at once, so
-   this sequence is effectively atomic.  In user emulation mode we
-   throw an exception and handle the atomic operation elsewhere.  */
+   In user emulation mode we throw an exception and handle the atomic
+   operation elsewhere.  */
+#ifdef CONFIG_TCG_USE_LDST_EXCL
+static void gen_load_exclusive(DisasContext *s, int rt, int rt2,
+                               TCGv_i32 addr, int size)
+ {
+    TCGv_i32 tmp = tcg_temp_new_i32();
+    TCGv_i32 mem_idx = tcg_temp_new_i32();
+
+    tcg_gen_movi_i32(mem_idx, get_mem_index(s));
+
+    if (size != 3) {
+        switch (size) {
+        case 0:
+            gen_helper_ldlink_aa32_i8(tmp, cpu_env, addr, mem_idx);
+            break;
+        case 1:
+            gen_helper_ldlink_aa32_i16(tmp, cpu_env, addr, mem_idx);
+            break;
+        case 2:
+            gen_helper_ldlink_aa32_i32(tmp, cpu_env, addr, mem_idx);
+            break;
+        default:
+            abort();
+        }
+
+        store_reg(s, rt, tmp);
+    } else {
+        TCGv_i64 tmp64 = tcg_temp_new_i64();
+        TCGv_i32 tmph = tcg_temp_new_i32();
+
+        gen_helper_ldlink_aa32_i64(tmp64, cpu_env, addr, mem_idx);
+        tcg_gen_extr_i64_i32(tmp, tmph, tmp64);
+
+        store_reg(s, rt, tmp);
+        store_reg(s, rt2, tmph);
+
+        tcg_temp_free_i64(tmp64);
+    }
+
+    tcg_temp_free_i32(mem_idx);
+}
+#else
 static void gen_load_exclusive(DisasContext *s, int rt, int rt2,
                                TCGv_i32 addr, int size)
 {

target-arm/translate.c static void gen_load_exclusive(DisasContext *s, int rt, int rt2, · line 7516

Quoted 8 lines
target-arm/translate.c +2 -0
@@ -7461,10 +7509,14 @@ static void gen_load_exclusive(DisasContext *s, int rt, int rt2,
     store_reg(s, rt, tmp);
     tcg_gen_extu_i32_i64(cpu_exclusive_addr, addr);
 }
+#endif
 
 static void gen_clrex(DisasContext *s)
 {
+#ifdef CONFIG_TCG_USE_LDST_EXCL

I don't think it would be correct to ignore clrex in softmmu mode. Assuming the code path had used it we may well be creating slow-path transitions for no reason.

Quoted 44 lines
target-arm/translate.c +36 -0
+#else
     tcg_gen_movi_i64(cpu_exclusive_addr, -1);
+#endif
 }
 
 #ifdef CONFIG_USER_ONLY
@@ -7476,6 +7528,47 @@ static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
                      size | (rd << 4) | (rt << 8) | (rt2 << 12));
     gen_exception_internal_insn(s, 4, EXCP_STREX);
 }
+#elif defined CONFIG_TCG_USE_LDST_EXCL
+static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
+                                TCGv_i32 addr, int size)
+{
+    TCGv_i32 tmp, mem_idx;
+
+    mem_idx = tcg_temp_new_i32();
+
+    tcg_gen_movi_i32(mem_idx, get_mem_index(s));
+    tmp = load_reg(s, rt);
+
+    if (size != 3) {
+        switch (size) {
+        case 0:
+            gen_helper_stcond_aa32_i8(cpu_R[rd], cpu_env, addr, tmp, mem_idx);
+            break;
+        case 1:
+            gen_helper_stcond_aa32_i16(cpu_R[rd], cpu_env, addr, tmp, mem_idx);
+            break;
+        case 2:
+            gen_helper_stcond_aa32_i32(cpu_R[rd], cpu_env, addr, tmp, mem_idx);
+            break;
+        default:
+            abort();
+        }
+    } else {
+        TCGv_i64 tmp64;
+        TCGv_i32 tmp2;
+
+        tmp64 = tcg_temp_new_i64();
+        tmp2 = load_reg(s, rt2);
+        tcg_gen_concat_i32_i64(tmp64, tmp, tmp2);
+        gen_helper_stcond_aa32_i64(cpu_R[rd], cpu_env, addr, tmp64, mem_idx);
+

target-arm/translate.c static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2, · line 7574

Quoted 10 lines
target-arm/translate.c +7 -0
+        tcg_temp_free_i32(tmp2);
+        tcg_temp_free_i64(tmp64);
+    }
+
+    tcg_temp_free_i32(tmp);
+    tcg_temp_free_i32(mem_idx);
+}
 #else
 static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
                                 TCGv_i32 addr, int size)

--
Alex Bennée

#23 lore raw

RFC v6 8/14 target-arm: Add atomic_clear helper for CLREX insn

In reply to Alvise Rigo

Add a simple helper function to emulate the CLREX instruction.

---
 target-arm/helper.h    | 2 ++
 target-arm/op_helper.c | 6 ++++++
 target-arm/translate.c | 1 +
 3 files changed, 9 insertions(+)
target-arm/helper.h +2 -0
index c2a85c7..37cec49 100644
@@ -532,6 +532,8 @@ DEF_HELPER_2(dc_zva, void, env, i64)
 DEF_HELPER_FLAGS_2(neon_pmull_64_lo, TCG_CALL_NO_RWG_SE, i64, i64, i64)
 DEF_HELPER_FLAGS_2(neon_pmull_64_hi, TCG_CALL_NO_RWG_SE, i64, i64, i64)
 
+DEF_HELPER_1(atomic_clear, void, env)
+
 #ifdef TARGET_AARCH64
 #include "helper-a64.h"
 #endif
target-arm/op_helper.c +6 -0
index 6cd54c8..5a67557 100644
@@ -50,6 +50,12 @@ static int exception_target_el(CPUARMState *env)
     return target_el;
 }
 
+void HELPER(atomic_clear)(CPUARMState *env)
+{
+    ENV_GET_CPU(env)->excl_protected_range.begin = -1;
+    ENV_GET_CPU(env)->ll_sc_context = false;
+}
+
 uint32_t HELPER(neon_tbl)(CPUARMState *env, uint32_t ireg, uint32_t def,
                           uint32_t rn, uint32_t maxindex)
 {
target-arm/translate.c +1 -0
index e88d8a3..e0362e0 100644
@@ -7514,6 +7514,7 @@ static void gen_load_exclusive(DisasContext *s, int rt, int rt2,
 static void gen_clrex(DisasContext *s)
 {
 #ifdef CONFIG_TCG_USE_LDST_EXCL
+    gen_helper_atomic_clear(cpu_env);
 #else
     tcg_gen_movi_i64(cpu_exclusive_addr, -1);
 #endif
Signature · 1 line
2.6.4
#24 lore raw

Re: [Qemu-devel] [RFC v6 08/14] target-arm: Add atomic_clear helper for CLREX insn

Quoted 1 line · Alvise Rigo

Add a simple helper function to emulate the CLREX instruction.

And now I see ;-)

I suspect this should be merged with the other helpers as a generic helper.

Quoted 28 lines
---
 target-arm/helper.h    | 2 ++
 target-arm/op_helper.c | 6 ++++++
 target-arm/translate.c | 1 +
 3 files changed, 9 insertions(+)
target-arm/helper.h +2 -0
index c2a85c7..37cec49 100644
@@ -532,6 +532,8 @@ DEF_HELPER_2(dc_zva, void, env, i64)
 DEF_HELPER_FLAGS_2(neon_pmull_64_lo, TCG_CALL_NO_RWG_SE, i64, i64, i64)
 DEF_HELPER_FLAGS_2(neon_pmull_64_hi, TCG_CALL_NO_RWG_SE, i64, i64, i64)
 
+DEF_HELPER_1(atomic_clear, void, env)
+
 #ifdef TARGET_AARCH64
 #include "helper-a64.h"
 #endif
target-arm/op_helper.c +0 -0
index 6cd54c8..5a67557 100644

target-arm/op_helper.c static int exception_target_el(CPUARMState *env) · line 55

Quoted 6 lines
target-arm/op_helper.c +3 -0
@@ -50,6 +50,12 @@ static int exception_target_el(CPUARMState *env)
     return target_el;
 }
 
+void HELPER(atomic_clear)(CPUARMState *env)
+{
+    ENV_GET_CPU(env)->excl_protected_range.begin = -1;

Is there not a defined reset value EXCLUSIVE_RESET_ADDR we should use here?

Quoted 11 lines
target-arm/op_helper.c +3 -0
+    ENV_GET_CPU(env)->ll_sc_context = false;
+}
+
 uint32_t HELPER(neon_tbl)(CPUARMState *env, uint32_t ireg, uint32_t def,
                           uint32_t rn, uint32_t maxindex)
 {
target-arm/translate.c +0 -0
index e88d8a3..e0362e0 100644

target-arm/translate.c static void gen_load_exclusive(DisasContext *s, int rt, int rt2, · line 7520

Quoted 7 lines
target-arm/translate.c +1 -0
@@ -7514,6 +7514,7 @@ static void gen_load_exclusive(DisasContext *s, int rt, int rt2,
 static void gen_clrex(DisasContext *s)
 {
 #ifdef CONFIG_TCG_USE_LDST_EXCL
+    gen_helper_atomic_clear(cpu_env);
 #else
     tcg_gen_movi_i64(cpu_exclusive_addr, -1);
 #endif

--
Alex Bennée

#25 lore raw

Re: [Qemu-devel] [RFC v6 08/14] target-arm: Add atomic_clear helper for CLREX insn

Quoted 8 lines · Alex Bennée
Quoted 1 line · Alvise Rigo

Add a simple helper function to emulate the CLREX instruction.

And now I see ;-)

I suspect this should be merged with the other helpers as a generic helper.

Agreed.

Quoted 32 lines
Quoted 32 lines
---
 target-arm/helper.h    | 2 ++
 target-arm/op_helper.c | 6 ++++++
 target-arm/translate.c | 1 +
 3 files changed, 9 insertions(+)
target-arm/helper.h +2 -0
index c2a85c7..37cec49 100644
@@ -532,6 +532,8 @@ DEF_HELPER_2(dc_zva, void, env, i64)
 DEF_HELPER_FLAGS_2(neon_pmull_64_lo, TCG_CALL_NO_RWG_SE, i64, i64, i64)
 DEF_HELPER_FLAGS_2(neon_pmull_64_hi, TCG_CALL_NO_RWG_SE, i64, i64, i64)
 
+DEF_HELPER_1(atomic_clear, void, env)
+
 #ifdef TARGET_AARCH64
 #include "helper-a64.h"
 #endif
target-arm/op_helper.c +0 -0
index 6cd54c8..5a67557 100644
@@ -50,6 +50,12 @@ static int exception_target_el(CPUARMState *env)
     return target_el;
 }
 
Quoted 5 lines

target-arm/op_helper.c static int exception_target_el(CPUARMState *env) · line 55

Quoted 3 lines
target-arm/op_helper.c +3 -0
+void HELPER(atomic_clear)(CPUARMState *env)
+{
+    ENV_GET_CPU(env)->excl_protected_range.begin = -1;

Is there not a defined reset value EXCLUSIVE_RESET_ADDR we should use here?

Yes, I will move the EXCLUSIVE_RESET_ADDR definition somewhere else in order to include it in this file.

Quoted 23 lines
Quoted 12 lines
target-arm/op_helper.c +3 -0
+    ENV_GET_CPU(env)->ll_sc_context = false;
+}
+
 uint32_t HELPER(neon_tbl)(CPUARMState *env, uint32_t ireg, uint32_t def,
                           uint32_t rn, uint32_t maxindex)
 {
target-arm/translate.c +0 -0
index e88d8a3..e0362e0 100644

target-arm/translate.c static void gen_load_exclusive(DisasContext *s, int rt, int rt2, · line 7520

Quoted 7 lines
target-arm/translate.c +1 -0
@@ -7514,6 +7514,7 @@ static void gen_load_exclusive(DisasContext *s, int rt, int rt2,
 static void gen_clrex(DisasContext *s)
 {
 #ifdef CONFIG_TCG_USE_LDST_EXCL
+    gen_helper_atomic_clear(cpu_env);
 #else
     tcg_gen_movi_i64(cpu_exclusive_addr, -1);
 #endif

--
Alex Bennée

#26 lore raw

RFC v6 9/14 softmmu: Add history of excl accesses

In reply to Alvise Rigo

Add a circular buffer to store the hw addresses used in the last EXCLUSIVE_HISTORY_LEN exclusive accesses.

When an address is pop'ed from the buffer, its page will be set as not exclusive. In this way, we avoid:
- frequent set/unset of a page (causing frequent flushes as well)
- the possibility to forget the EXCL bit set.

---
 cputlb.c                | 32 ++++++++++++++++++++++----------
 include/qom/cpu.h       |  3 +++
 softmmu_llsc_template.h |  2 ++
 3 files changed, 27 insertions(+), 10 deletions(-)
cputlb.c +22 -10
index 70b6404..372877e 100644
@@ -394,16 +394,6 @@ void tlb_set_page_with_attrs(CPUState *cpu, target_ulong vaddr,
     env->tlb_v_table[mmu_idx][vidx] = *te;
     env->iotlb_v[mmu_idx][vidx] = env->iotlb[mmu_idx][index];
 
-    if (unlikely(!(te->addr_write & TLB_MMIO) && (te->addr_write & TLB_EXCL))) {
-        /* We are removing an exclusive entry, set the page to dirty. This
-         * is not be necessary if the vCPU has performed both SC and LL. */
-        hwaddr hw_addr = (env->iotlb[mmu_idx][index].addr & TARGET_PAGE_MASK) +
-                                          (te->addr_write & TARGET_PAGE_MASK);
-        if (!cpu->ll_sc_context) {
-            cpu_physical_memory_unset_excl(hw_addr, cpu->cpu_index);
-        }
-    }
-
     /* refill the tlb */
     env->iotlb[mmu_idx][index].addr = iotlb - vaddr;
     env->iotlb[mmu_idx][index].attrs = attrs;
@@ -507,6 +497,28 @@ static inline void lookup_and_reset_cpus_ll_addr(hwaddr addr, hwaddr size)
     }
 }
 
+static inline void excl_history_put_addr(CPUState *cpu, hwaddr addr)
+{
+    /* Avoid some overhead if the address we are about to put is equal to
+     * the last one */
+    if (cpu->excl_protected_addr[cpu->excl_protected_last] !=
+                                    (addr & TARGET_PAGE_MASK)) {
+        cpu->excl_protected_last = (cpu->excl_protected_last + 1) %
+                                            EXCLUSIVE_HISTORY_LEN;
+        /* Unset EXCL bit of the oldest entry */
+        if (cpu->excl_protected_addr[cpu->excl_protected_last] !=
+                                            EXCLUSIVE_RESET_ADDR) {
+            cpu_physical_memory_unset_excl(
+                cpu->excl_protected_addr[cpu->excl_protected_last],
+                cpu->cpu_index);
+        }
+
+        /* Add a new address, overwriting the oldest one */
+        cpu->excl_protected_addr[cpu->excl_protected_last] =
+                                            addr & TARGET_PAGE_MASK;
+    }
+}
+
 #define MMUSUFFIX _mmu
 
 /* Generates LoadLink/StoreConditional helpers in softmmu_template.h */
include/qom/cpu.h +3 -0
index 9e409ce..5f65ebf 100644
@@ -217,6 +217,7 @@ struct kvm_run;
 
 /* Atomic insn translation TLB support. */
 #define EXCLUSIVE_RESET_ADDR ULLONG_MAX
+#define EXCLUSIVE_HISTORY_LEN 8
 
 /**
  * CPUState:
@@ -343,6 +344,8 @@ struct CPUState {
      * The address is set to EXCLUSIVE_RESET_ADDR if the vCPU is not.
      * in the middle of a LL/SC. */
     struct Range excl_protected_range;
+    hwaddr excl_protected_addr[EXCLUSIVE_HISTORY_LEN];
+    int excl_protected_last;
     /* Used to carry the SC result but also to flag a normal (legacy)
      * store access made by a stcond (see softmmu_template.h). */
     int excl_succeeded;
softmmu_llsc_template.h +2 -0
index 586bb2e..becb90b 100644
@@ -72,6 +72,7 @@ WORD_TYPE helper_ldlink_name(CPUArchState *env, target_ulong addr,
     hw_addr = (env->iotlb[mmu_idx][index].addr & TARGET_PAGE_MASK) + addr;
 
     cpu_physical_memory_set_excl(hw_addr, this->cpu_index);
+    excl_history_put_addr(this, hw_addr);
     /* If all the vCPUs have the EXCL bit set for this page there is no need
      * to request any flush. */
     if (cpu_physical_memory_not_excl(hw_addr, smp_cpus)) {
@@ -80,6 +81,7 @@ WORD_TYPE helper_ldlink_name(CPUArchState *env, target_ulong addr,
                 if (cpu_physical_memory_not_excl(hw_addr, cpu->cpu_index)) {
                     cpu_physical_memory_set_excl(hw_addr, cpu->cpu_index);
                     tlb_flush(cpu, 1);
+                    excl_history_put_addr(cpu, hw_addr);
                 }
             }
         }
Signature · 1 line
2.6.4
#27 lore raw

Re: [Qemu-devel] [RFC v6 09/14] softmmu: Add history of excl accesses

cputlb.c static inline void lookup_and_reset_cpus_ll_addr(hwaddr addr, hwaddr size) · line 507

Quoted 8 lines · Alvise Rigo
cputlb.c +8 -0
+static inline void excl_history_put_addr(CPUState *cpu, hwaddr addr)
+{
+    /* Avoid some overhead if the address we are about to put is equal to
+     * the last one */
+    if (cpu->excl_protected_addr[cpu->excl_protected_last] !=
+                                    (addr & TARGET_PAGE_MASK)) {
+        cpu->excl_protected_last = (cpu->excl_protected_last + 1) %
+                                            EXCLUSIVE_HISTORY_LEN;

Either use "&" here...

Quoted 30 lines
cputlb.c +14 -0
+        /* Unset EXCL bit of the oldest entry */
+        if (cpu->excl_protected_addr[cpu->excl_protected_last] !=
+                                            EXCLUSIVE_RESET_ADDR) {
+            cpu_physical_memory_unset_excl(
+                cpu->excl_protected_addr[cpu->excl_protected_last],
+                cpu->cpu_index);
+        }
+
+        /* Add a new address, overwriting the oldest one */
+        cpu->excl_protected_addr[cpu->excl_protected_last] =
+                                            addr & TARGET_PAGE_MASK;
+    }
+}
+
 #define MMUSUFFIX _mmu
 
 /* Generates LoadLink/StoreConditional helpers in softmmu_template.h */
include/qom/cpu.h +1 -0
index 9e409ce..5f65ebf 100644
@@ -217,6 +217,7 @@ struct kvm_run;
 
 /* Atomic insn translation TLB support. */
 #define EXCLUSIVE_RESET_ADDR ULLONG_MAX
+#define EXCLUSIVE_HISTORY_LEN 8
 
 /**
  * CPUState:

include/qom/cpu.h struct CPUState { · line 348

Quoted 5 lines
include/qom/cpu.h +2 -0
@@ -343,6 +344,8 @@ struct CPUState {
      * The address is set to EXCLUSIVE_RESET_ADDR if the vCPU is not.
      * in the middle of a LL/SC. */
     struct Range excl_protected_range;
+    hwaddr excl_protected_addr[EXCLUSIVE_HISTORY_LEN];
+    int excl_protected_last;

... or make this an "unsigned int". Otherwise the code will contain an actual (and slow) modulo operation.

Paolo

Quoted 3 lines
include/qom/cpu.h +0 -0
     /* Used to carry the SC result but also to flag a normal (legacy)
      * store access made by a stcond (see softmmu_template.h). */
     int excl_succeeded;
#28 lore raw

Re: [Qemu-devel] [RFC v6 09/14] softmmu: Add history of excl accesses

Quoted 41 lines · Paolo Bonzini

cputlb.c static inline void lookup_and_reset_cpus_ll_addr(hwaddr addr, hwaddr size) · line 507

Quoted 8 lines · Alvise Rigo
cputlb.c +8 -0
+static inline void excl_history_put_addr(CPUState *cpu, hwaddr addr)
+{
+    /* Avoid some overhead if the address we are about to put is equal to
+     * the last one */
+    if (cpu->excl_protected_addr[cpu->excl_protected_last] !=
+                                    (addr & TARGET_PAGE_MASK)) {
+        cpu->excl_protected_last = (cpu->excl_protected_last + 1) %
+                                            EXCLUSIVE_HISTORY_LEN;

Either use "&" here...

Quoted 27 lines
cputlb.c +14 -0
+        /* Unset EXCL bit of the oldest entry */
+        if (cpu->excl_protected_addr[cpu->excl_protected_last] !=
+                                            EXCLUSIVE_RESET_ADDR) {
+            cpu_physical_memory_unset_excl(
+                cpu->excl_protected_addr[cpu->excl_protected_last],
+                cpu->cpu_index);
+        }
+
+        /* Add a new address, overwriting the oldest one */
+        cpu->excl_protected_addr[cpu->excl_protected_last] =
+                                            addr & TARGET_PAGE_MASK;
+    }
+}
+
 #define MMUSUFFIX _mmu
 
 /* Generates LoadLink/StoreConditional helpers in softmmu_template.h */
include/qom/cpu.h +1 -0
index 9e409ce..5f65ebf 100644
@@ -217,6 +217,7 @@ struct kvm_run;
 
 /* Atomic insn translation TLB support. */
 #define EXCLUSIVE_RESET_ADDR ULLONG_MAX
+#define EXCLUSIVE_HISTORY_LEN 8
 
Quoted 11 lines

include/qom/cpu.h struct CPUState { · line 348

Quoted 8 lines
include/qom/cpu.h +2 -0
 /**
  * CPUState:
@@ -343,6 +344,8 @@ struct CPUState {
      * The address is set to EXCLUSIVE_RESET_ADDR if the vCPU is not.
      * in the middle of a LL/SC. */
     struct Range excl_protected_range;
+    hwaddr excl_protected_addr[EXCLUSIVE_HISTORY_LEN];
+    int excl_protected_last;

... or make this an "unsigned int". Otherwise the code will contain an actual (and slow) modulo operation.

Absolutely true.

Thank you,
alvise

Quoted 6 lines

Paolo

Quoted 3 lines
include/qom/cpu.h +0 -0
     /* Used to carry the SC result but also to flag a normal (legacy)
      * store access made by a stcond (see softmmu_template.h). */
     int excl_succeeded;
#29 lore raw

RFC v6 10/14 softmmu: Simplify helper_*_st_name, wrap unaligned code

In reply to Alvise Rigo

Attempting to simplify the helper_*_st_name, wrap the do_unaligned_access code into an inline function. Remove also the goto statement.

---
 softmmu_template.h | 96 ++++++++++++++++++++++++++++++++++--------------------
 1 file changed, 60 insertions(+), 36 deletions(-)
softmmu_template.h +60 -36
index d3d5902..92f92b1 100644
@@ -370,6 +370,32 @@ static inline void glue(io_write, SUFFIX)(CPUArchState *env,
                                  iotlbentry->attrs);
 }
 
+static inline void glue(helper_le_st_name, _do_unl_access)(CPUArchState *env,
+                                                           DATA_TYPE val,
+                                                           target_ulong addr,
+                                                           TCGMemOpIdx oi,
+                                                           unsigned mmu_idx,
+                                                           uintptr_t retaddr)
+{
+    int i;
+
+    if ((get_memop(oi) & MO_AMASK) == MO_ALIGN) {
+        cpu_unaligned_access(ENV_GET_CPU(env), addr, MMU_DATA_STORE,
+                             mmu_idx, retaddr);
+    }
+    /* XXX: not efficient, but simple */
+    /* Note: relies on the fact that tlb_fill() does not remove the
+     * previous page from the TLB cache.  */
+    for (i = DATA_SIZE - 1; i >= 0; i--) {
+        /* Little-endian extract.  */
+        uint8_t val8 = val >> (i * 8);
+        /* Note the adjustment at the beginning of the function.
+           Undo that for the recursion.  */
+        glue(helper_ret_stb, MMUSUFFIX)(env, addr + i, val8,
+                                        oi, retaddr + GETPC_ADJ);
+    }
+}
+
 void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
                        TCGMemOpIdx oi, uintptr_t retaddr)
 {
@@ -433,7 +459,8 @@ void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
             return;
         } else {
             if ((addr & (DATA_SIZE - 1)) != 0) {
-                goto do_unaligned_access;
+                glue(helper_le_st_name, _do_unl_access)(env, val, addr, mmu_idx,
+                                                        oi, retaddr);
             }
             iotlbentry = &env->iotlb[mmu_idx][index];
 
@@ -449,23 +476,8 @@ void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
     if (DATA_SIZE > 1
         && unlikely((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1
                      >= TARGET_PAGE_SIZE)) {
-        int i;
-    do_unaligned_access:
-        if ((get_memop(oi) & MO_AMASK) == MO_ALIGN) {
-            cpu_unaligned_access(ENV_GET_CPU(env), addr, MMU_DATA_STORE,
-                                 mmu_idx, retaddr);
-        }
-        /* XXX: not efficient, but simple */
-        /* Note: relies on the fact that tlb_fill() does not remove the
-         * previous page from the TLB cache.  */
-        for (i = DATA_SIZE - 1; i >= 0; i--) {
-            /* Little-endian extract.  */
-            uint8_t val8 = val >> (i * 8);
-            /* Note the adjustment at the beginning of the function.
-               Undo that for the recursion.  */
-            glue(helper_ret_stb, MMUSUFFIX)(env, addr + i, val8,
-                                            oi, retaddr + GETPC_ADJ);
-        }
+        glue(helper_le_st_name, _do_unl_access)(env, val, addr, oi, mmu_idx,
+                                                retaddr);
         return;
     }
 
@@ -485,6 +497,32 @@ void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
 }
 
 #if DATA_SIZE > 1
+static inline void glue(helper_be_st_name, _do_unl_access)(CPUArchState *env,
+                                                           DATA_TYPE val,
+                                                           target_ulong addr,
+                                                           TCGMemOpIdx oi,
+                                                           unsigned mmu_idx,
+                                                           uintptr_t retaddr)
+{
+    int i;
+
+    if ((get_memop(oi) & MO_AMASK) == MO_ALIGN) {
+        cpu_unaligned_access(ENV_GET_CPU(env), addr, MMU_DATA_STORE,
+                             mmu_idx, retaddr);
+    }
+    /* XXX: not efficient, but simple */
+    /* Note: relies on the fact that tlb_fill() does not remove the
+     * previous page from the TLB cache.  */
+    for (i = DATA_SIZE - 1; i >= 0; i--) {
+        /* Big-endian extract.  */
+        uint8_t val8 = val >> (((DATA_SIZE - 1) * 8) - (i * 8));
+        /* Note the adjustment at the beginning of the function.
+           Undo that for the recursion.  */
+        glue(helper_ret_stb, MMUSUFFIX)(env, addr + i, val8,
+                                        oi, retaddr + GETPC_ADJ);
+    }
+}
+
 void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
                        TCGMemOpIdx oi, uintptr_t retaddr)
 {
@@ -548,7 +586,8 @@ void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
             return;
         } else {
             if ((addr & (DATA_SIZE - 1)) != 0) {
-                goto do_unaligned_access;
+                glue(helper_be_st_name, _do_unl_access)(env, val, addr, mmu_idx,
+                                                        oi, retaddr);
             }
             iotlbentry = &env->iotlb[mmu_idx][index];
 
@@ -564,23 +603,8 @@ void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
     if (DATA_SIZE > 1
         && unlikely((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1
                      >= TARGET_PAGE_SIZE)) {
-        int i;
-    do_unaligned_access:
-        if ((get_memop(oi) & MO_AMASK) == MO_ALIGN) {
-            cpu_unaligned_access(ENV_GET_CPU(env), addr, MMU_DATA_STORE,
-                                 mmu_idx, retaddr);
-        }
-        /* XXX: not efficient, but simple */
-        /* Note: relies on the fact that tlb_fill() does not remove the
-         * previous page from the TLB cache.  */
-        for (i = DATA_SIZE - 1; i >= 0; i--) {
-            /* Big-endian extract.  */
-            uint8_t val8 = val >> (((DATA_SIZE - 1) * 8) - (i * 8));
-            /* Note the adjustment at the beginning of the function.
-               Undo that for the recursion.  */
-            glue(helper_ret_stb, MMUSUFFIX)(env, addr + i, val8,
-                                            oi, retaddr + GETPC_ADJ);
-        }
+        glue(helper_be_st_name, _do_unl_access)(env, val, addr, oi, mmu_idx,
+                                                retaddr);
         return;
     }
 
Signature · 1 line
2.6.4
#30 lore raw

Re: [Qemu-devel] [RFC v6 10/14] softmmu: Simplify helper_*_st_name, wrap unaligned code

Quoted 3 lines · Alvise Rigo

Attempting to simplify the helper_*_st_name, wrap the do_unaligned_access code into an inline function. Remove also the goto statement.

As I said in the other thread I think these sort of clean-ups can come before the ll/sc implementations and potentially get merged ahead of the rest of it.

Quoted 31 lines
---
 softmmu_template.h | 96 ++++++++++++++++++++++++++++++++++--------------------
 1 file changed, 60 insertions(+), 36 deletions(-)
softmmu_template.h +15 -0
index d3d5902..92f92b1 100644
@@ -370,6 +370,32 @@ static inline void glue(io_write, SUFFIX)(CPUArchState *env,
                                  iotlbentry->attrs);
 }
 
+static inline void glue(helper_le_st_name, _do_unl_access)(CPUArchState *env,
+                                                           DATA_TYPE val,
+                                                           target_ulong addr,
+                                                           TCGMemOpIdx oi,
+                                                           unsigned mmu_idx,
+                                                           uintptr_t retaddr)
+{
+    int i;
+
+    if ((get_memop(oi) & MO_AMASK) == MO_ALIGN) {
+        cpu_unaligned_access(ENV_GET_CPU(env), addr, MMU_DATA_STORE,
+                             mmu_idx, retaddr);
+    }
+    /* XXX: not efficient, but simple */
+    /* Note: relies on the fact that tlb_fill() does not remove the

softmmu_template.h static inline void glue(io_write, SUFFIX)(CPUArchState *env, · line 397

Quoted 10 lines
softmmu_template.h +10 -0
+     * previous page from the TLB cache.  */
+    for (i = DATA_SIZE - 1; i >= 0; i--) {
+        /* Little-endian extract.  */
+        uint8_t val8 = val >> (i * 8);
+        /* Note the adjustment at the beginning of the function.
+           Undo that for the recursion.  */
+        glue(helper_ret_stb, MMUSUFFIX)(env, addr + i, val8,
+                                        oi, retaddr + GETPC_ADJ);
+    }
+}

There is still duplication of 99% of the code here which is silly given the compiler inlines the code anyway. If we gave the helper a more generic name and passed the endianess in via args I would hope the compiler did the sensible thing and constant fold the code. Something like:

static inline void glue(helper_generic_st_name, _do_unl_access)
(CPUArchState *env,
bool little_endian,
DATA_TYPE val,
target_ulong addr, TCGMemOpIdx oi,
unsigned mmu_idx,
uintptr_t retaddr)

{
    int i;

    if ((get_memop(oi) & MO_AMASK) == MO_ALIGN) {
        cpu_unaligned_access(ENV_GET_CPU(env), addr, MMU_DATA_STORE,
                             mmu_idx, retaddr);
    }
    /* Note: relies on the fact that tlb_fill() does not remove the
     * previous page from the TLB cache.  */
    for (i = DATA_SIZE - 1; i >= 0; i--) {
        if (little_endian) {
                /* Little-endian extract.  */
                uint8_t val8 = val >> (i * 8);
        } else {
                /* Big-endian extract.  */
                uint8_t val8 = val >> (((DATA_SIZE - 1) * 8) - (i * 8));
        }
        /* Note the adjustment at the beginning of the function.
           Undo that for the recursion.  */
        glue(helper_ret_stb, MMUSUFFIX)(env, addr + i, val8,
                                        oi, retaddr + GETPC_ADJ);
    }
}
Quoted 59 lines
softmmu_template.h +20 -18
+
 void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
                        TCGMemOpIdx oi, uintptr_t retaddr)
 {
@@ -433,7 +459,8 @@ void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
             return;
         } else {
             if ((addr & (DATA_SIZE - 1)) != 0) {
-                goto do_unaligned_access;
+                glue(helper_le_st_name, _do_unl_access)(env, val, addr, mmu_idx,
+                                                        oi, retaddr);
             }
             iotlbentry = &env->iotlb[mmu_idx][index];
 
@@ -449,23 +476,8 @@ void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
     if (DATA_SIZE > 1
         && unlikely((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1
                      >= TARGET_PAGE_SIZE)) {
-        int i;
-    do_unaligned_access:
-        if ((get_memop(oi) & MO_AMASK) == MO_ALIGN) {
-            cpu_unaligned_access(ENV_GET_CPU(env), addr, MMU_DATA_STORE,
-                                 mmu_idx, retaddr);
-        }
-        /* XXX: not efficient, but simple */
-        /* Note: relies on the fact that tlb_fill() does not remove the
-         * previous page from the TLB cache.  */
-        for (i = DATA_SIZE - 1; i >= 0; i--) {
-            /* Little-endian extract.  */
-            uint8_t val8 = val >> (i * 8);
-            /* Note the adjustment at the beginning of the function.
-               Undo that for the recursion.  */
-            glue(helper_ret_stb, MMUSUFFIX)(env, addr + i, val8,
-                                            oi, retaddr + GETPC_ADJ);
-        }
+        glue(helper_le_st_name, _do_unl_access)(env, val, addr, oi, mmu_idx,
+                                                retaddr);
         return;
     }
 
@@ -485,6 +497,32 @@ void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
 }
 
 #if DATA_SIZE > 1
+static inline void glue(helper_be_st_name, _do_unl_access)(CPUArchState *env,
+                                                           DATA_TYPE val,
+                                                           target_ulong addr,
+                                                           TCGMemOpIdx oi,
+                                                           unsigned mmu_idx,
+                                                           uintptr_t retaddr)
+{
+    int i;
+
+    if ((get_memop(oi) & MO_AMASK) == MO_ALIGN) {
+        cpu_unaligned_access(ENV_GET_CPU(env), addr, MMU_DATA_STORE,
+                             mmu_idx, retaddr);
+    }
+    /* XXX: not efficient, but simple */
+    /* Note: relies on the fact that tlb_fill() does not remove the

softmmu_template.h void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val, · line 524

Quoted 10 lines
softmmu_template.h +10 -0
+     * previous page from the TLB cache.  */
+    for (i = DATA_SIZE - 1; i >= 0; i--) {
+        /* Big-endian extract.  */
+        uint8_t val8 = val >> (((DATA_SIZE - 1) * 8) - (i * 8));
+        /* Note the adjustment at the beginning of the function.
+           Undo that for the recursion.  */
+        glue(helper_ret_stb, MMUSUFFIX)(env, addr + i, val8,
+                                        oi, retaddr + GETPC_ADJ);
+    }
+}

Not that it matters if you combine to two as suggested because anything not called shouldn't generate the code.

Quoted 28 lines
softmmu_template.h +2 -12
 void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
                        TCGMemOpIdx oi, uintptr_t retaddr)
 {
@@ -548,7 +586,8 @@ void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
             return;
         } else {
             if ((addr & (DATA_SIZE - 1)) != 0) {
-                goto do_unaligned_access;
+                glue(helper_be_st_name, _do_unl_access)(env, val, addr, mmu_idx,
+                                                        oi, retaddr);
             }
             iotlbentry = &env->iotlb[mmu_idx][index];
 
@@ -564,23 +603,8 @@ void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
     if (DATA_SIZE > 1
         && unlikely((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1
                      >= TARGET_PAGE_SIZE)) {
-        int i;
-    do_unaligned_access:
-        if ((get_memop(oi) & MO_AMASK) == MO_ALIGN) {
-            cpu_unaligned_access(ENV_GET_CPU(env), addr, MMU_DATA_STORE,
-                                 mmu_idx, retaddr);
-        }
-        /* XXX: not efficient, but simple */
-        /* Note: relies on the fact that tlb_fill() does not remove the
-         * previous page from the TLB cache.  */
-        for (i = DATA_SIZE - 1; i >= 0; i--) {
-            /* Big-endian extract.  */

softmmu_template.h void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val, · line 609

Quoted 10 lines
softmmu_template.h +2 -6
-            uint8_t val8 = val >> (((DATA_SIZE - 1) * 8) - (i * 8));
-            /* Note the adjustment at the beginning of the function.
-               Undo that for the recursion.  */
-            glue(helper_ret_stb, MMUSUFFIX)(env, addr + i, val8,
-                                            oi, retaddr + GETPC_ADJ);
-        }
+        glue(helper_be_st_name, _do_unl_access)(env, val, addr, oi, mmu_idx,
+                                                retaddr);
         return;
     }

--
Alex Bennée

#31 lore raw

Re: [Qemu-devel] [RFC v6 10/14] softmmu: Simplify helper_*_st_name, wrap unaligned code

Quoted 46 lines · Alex Bennée
Quoted 3 lines · Alvise Rigo

Attempting to simplify the helper_*_st_name, wrap the do_unaligned_access code into an inline function. Remove also the goto statement.

As I said in the other thread I think these sort of clean-ups can come before the ll/sc implementations and potentially get merged ahead of the rest of it.

Quoted 35 lines
---
 softmmu_template.h | 96 ++++++++++++++++++++++++++++++++++--------------------
 1 file changed, 60 insertions(+), 36 deletions(-)
softmmu_template.h +19 -0
index d3d5902..92f92b1 100644
@@ -370,6 +370,32 @@ static inline void glue(io_write, SUFFIX)(CPUArchState *env,
                                  iotlbentry->attrs);
 }
 
+static inline void glue(helper_le_st_name, _do_unl_access)(CPUArchState *env,
+                                                           DATA_TYPE val,
+                                                           target_ulong addr,
+                                                           TCGMemOpIdx oi,
+                                                           unsigned mmu_idx,
+                                                           uintptr_t retaddr)
+{
+    int i;
+
+    if ((get_memop(oi) & MO_AMASK) == MO_ALIGN) {
+        cpu_unaligned_access(ENV_GET_CPU(env), addr, MMU_DATA_STORE,
+                             mmu_idx, retaddr);
+    }
+    /* XXX: not efficient, but simple */
+    /* Note: relies on the fact that tlb_fill() does not remove the
+     * previous page from the TLB cache.  */
+    for (i = DATA_SIZE - 1; i >= 0; i--) {
+        /* Little-endian extract.  */
+        uint8_t val8 = val >> (i * 8);
Quoted 8 lines

softmmu_template.h static inline void glue(io_write, SUFFIX)(CPUArchState *env, · line 397

Quoted 6 lines
softmmu_template.h +6 -0
+        /* Note the adjustment at the beginning of the function.
+           Undo that for the recursion.  */
+        glue(helper_ret_stb, MMUSUFFIX)(env, addr + i, val8,
+                                        oi, retaddr + GETPC_ADJ);
+    }
+}

There is still duplication of 99% of the code here which is silly given

Then why should we keep this template-like design in the first place? I tried to keep the code duplication for performance reasons (otherwise how can we justify the two almost identical versions of the helper?), while making the code more compact and readable.

Quoted 13 lines

the compiler inlines the code anyway. If we gave the helper a more generic name and passed the endianess in via args I would hope the compiler did the sensible thing and constant fold the code. Something like:

static inline void glue(helper_generic_st_name, _do_unl_access)
(CPUArchState *env,
bool little_endian,
DATA_TYPE val,
target_ulong addr, TCGMemOpIdx oi,
unsigned mmu_idx,
uintptr_t retaddr)

Quoted 11 lines
{
    int i;

    if ((get_memop(oi) & MO_AMASK) == MO_ALIGN) {
        cpu_unaligned_access(ENV_GET_CPU(env), addr, MMU_DATA_STORE,
                             mmu_idx, retaddr);
    }
    /* Note: relies on the fact that tlb_fill() does not remove the
     * previous page from the TLB cache.  */
    for (i = DATA_SIZE - 1; i >= 0; i--) {
        if (little_endian) {

little_endian will have >99% of the time the same value, does it make sense to have a branch here?

Thank you,
alvise

Quoted 129 lines
                /* Little-endian extract.  */
                uint8_t val8 = val >> (i * 8);
        } else {
                /* Big-endian extract.  */
                uint8_t val8 = val >> (((DATA_SIZE - 1) * 8) - (i * 8));
        }
        /* Note the adjustment at the beginning of the function.
           Undo that for the recursion.  */
        glue(helper_ret_stb, MMUSUFFIX)(env, addr + i, val8,
                                        oi, retaddr + GETPC_ADJ);
    }
}
Quoted 59 lines
softmmu_template.h +20 -18
+
 void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
                        TCGMemOpIdx oi, uintptr_t retaddr)
 {
@@ -433,7 +459,8 @@ void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
             return;
         } else {
             if ((addr & (DATA_SIZE - 1)) != 0) {
-                goto do_unaligned_access;
+                glue(helper_le_st_name, _do_unl_access)(env, val, addr, mmu_idx,
+                                                        oi, retaddr);
             }
             iotlbentry = &env->iotlb[mmu_idx][index];
 
@@ -449,23 +476,8 @@ void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
     if (DATA_SIZE > 1
         && unlikely((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1
                      >= TARGET_PAGE_SIZE)) {
-        int i;
-    do_unaligned_access:
-        if ((get_memop(oi) & MO_AMASK) == MO_ALIGN) {
-            cpu_unaligned_access(ENV_GET_CPU(env), addr, MMU_DATA_STORE,
-                                 mmu_idx, retaddr);
-        }
-        /* XXX: not efficient, but simple */
-        /* Note: relies on the fact that tlb_fill() does not remove the
-         * previous page from the TLB cache.  */
-        for (i = DATA_SIZE - 1; i >= 0; i--) {
-            /* Little-endian extract.  */
-            uint8_t val8 = val >> (i * 8);
-            /* Note the adjustment at the beginning of the function.
-               Undo that for the recursion.  */
-            glue(helper_ret_stb, MMUSUFFIX)(env, addr + i, val8,
-                                            oi, retaddr + GETPC_ADJ);
-        }
+        glue(helper_le_st_name, _do_unl_access)(env, val, addr, oi, mmu_idx,
+                                                retaddr);
         return;
     }
 
@@ -485,6 +497,32 @@ void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
 }
 
 #if DATA_SIZE > 1
+static inline void glue(helper_be_st_name, _do_unl_access)(CPUArchState *env,
+                                                           DATA_TYPE val,
+                                                           target_ulong addr,
+                                                           TCGMemOpIdx oi,
+                                                           unsigned mmu_idx,
+                                                           uintptr_t retaddr)
+{
+    int i;
+
+    if ((get_memop(oi) & MO_AMASK) == MO_ALIGN) {
+        cpu_unaligned_access(ENV_GET_CPU(env), addr, MMU_DATA_STORE,
+                             mmu_idx, retaddr);
+    }
+    /* XXX: not efficient, but simple */
+    /* Note: relies on the fact that tlb_fill() does not remove the

softmmu_template.h void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val, · line 524

Quoted 10 lines
softmmu_template.h +10 -0
+     * previous page from the TLB cache.  */
+    for (i = DATA_SIZE - 1; i >= 0; i--) {
+        /* Big-endian extract.  */
+        uint8_t val8 = val >> (((DATA_SIZE - 1) * 8) - (i * 8));
+        /* Note the adjustment at the beginning of the function.
+           Undo that for the recursion.  */
+        glue(helper_ret_stb, MMUSUFFIX)(env, addr + i, val8,
+                                        oi, retaddr + GETPC_ADJ);
+    }
+}

Not that it matters if you combine to two as suggested because anything not called shouldn't generate the code.

Quoted 28 lines
softmmu_template.h +2 -12
 void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
                        TCGMemOpIdx oi, uintptr_t retaddr)
 {
@@ -548,7 +586,8 @@ void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
             return;
         } else {
             if ((addr & (DATA_SIZE - 1)) != 0) {
-                goto do_unaligned_access;
+                glue(helper_be_st_name, _do_unl_access)(env, val, addr, mmu_idx,
+                                                        oi, retaddr);
             }
             iotlbentry = &env->iotlb[mmu_idx][index];
 
@@ -564,23 +603,8 @@ void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
     if (DATA_SIZE > 1
         && unlikely((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1
                      >= TARGET_PAGE_SIZE)) {
-        int i;
-    do_unaligned_access:
-        if ((get_memop(oi) & MO_AMASK) == MO_ALIGN) {
-            cpu_unaligned_access(ENV_GET_CPU(env), addr, MMU_DATA_STORE,
-                                 mmu_idx, retaddr);
-        }
-        /* XXX: not efficient, but simple */
-        /* Note: relies on the fact that tlb_fill() does not remove the
-         * previous page from the TLB cache.  */
-        for (i = DATA_SIZE - 1; i >= 0; i--) {
-            /* Big-endian extract.  */

softmmu_template.h void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val, · line 609

Quoted 10 lines
softmmu_template.h +2 -6
-            uint8_t val8 = val >> (((DATA_SIZE - 1) * 8) - (i * 8));
-            /* Note the adjustment at the beginning of the function.
-               Undo that for the recursion.  */
-            glue(helper_ret_stb, MMUSUFFIX)(env, addr + i, val8,
-                                            oi, retaddr + GETPC_ADJ);
-        }
+        glue(helper_be_st_name, _do_unl_access)(env, val, addr, oi, mmu_idx,
+                                                retaddr);
         return;
     }

--
Alex Bennée

#32 lore raw

Re: [Qemu-devel] [RFC v6 10/14] softmmu: Simplify helper_*_st_name, wrap unaligned code

Quoted 56 lines · alvise rigo
Quoted 46 lines · Alex Bennée
Quoted 3 lines · Alvise Rigo

Attempting to simplify the helper_*_st_name, wrap the do_unaligned_access code into an inline function. Remove also the goto statement.

As I said in the other thread I think these sort of clean-ups can come before the ll/sc implementations and potentially get merged ahead of the rest of it.

Quoted 35 lines
---
 softmmu_template.h | 96 ++++++++++++++++++++++++++++++++++--------------------
 1 file changed, 60 insertions(+), 36 deletions(-)
softmmu_template.h +19 -0
index d3d5902..92f92b1 100644
@@ -370,6 +370,32 @@ static inline void glue(io_write, SUFFIX)(CPUArchState *env,
                                  iotlbentry->attrs);
 }
 
+static inline void glue(helper_le_st_name, _do_unl_access)(CPUArchState *env,
+                                                           DATA_TYPE val,
+                                                           target_ulong addr,
+                                                           TCGMemOpIdx oi,
+                                                           unsigned mmu_idx,
+                                                           uintptr_t retaddr)
+{
+    int i;
+
+    if ((get_memop(oi) & MO_AMASK) == MO_ALIGN) {
+        cpu_unaligned_access(ENV_GET_CPU(env), addr, MMU_DATA_STORE,
+                             mmu_idx, retaddr);
+    }
+    /* XXX: not efficient, but simple */
+    /* Note: relies on the fact that tlb_fill() does not remove the
+     * previous page from the TLB cache.  */
+    for (i = DATA_SIZE - 1; i >= 0; i--) {
+        /* Little-endian extract.  */
+        uint8_t val8 = val >> (i * 8);
Quoted 8 lines

softmmu_template.h static inline void glue(io_write, SUFFIX)(CPUArchState *env, · line 397

Quoted 6 lines
softmmu_template.h +6 -0
+        /* Note the adjustment at the beginning of the function.
+           Undo that for the recursion.  */
+        glue(helper_ret_stb, MMUSUFFIX)(env, addr + i, val8,
+                                        oi, retaddr + GETPC_ADJ);
+    }
+}

There is still duplication of 99% of the code here which is silly given

Quoted 4 lines

Then why should we keep this template-like design in the first place? I tried to keep the code duplication for performance reasons (otherwise how can we justify the two almost identical versions of the helper?), while making the code more compact and readable.

We shouldn't really - code duplication is bad for all the well known reasons. The main reason we need explicit helpers for the be/le case are because they are called directly from the TCG code which encodes the endianess decision in the call it makes. However that doesn't stop us making generic inline helpers (helpers for the helpers ;-) which the compiler can sort out.

Quoted 26 lines
Quoted 14 lines

the compiler inlines the code anyway. If we gave the helper a more generic name and passed the endianess in via args I would hope the compiler did the sensible thing and constant fold the code. Something like:

static inline void glue(helper_generic_st_name, _do_unl_access)
(CPUArchState *env,
bool little_endian,
DATA_TYPE val,
target_ulong addr, TCGMemOpIdx oi,
unsigned mmu_idx,
uintptr_t retaddr)

Quoted 11 lines
{
    int i;

    if ((get_memop(oi) & MO_AMASK) == MO_ALIGN) {
        cpu_unaligned_access(ENV_GET_CPU(env), addr, MMU_DATA_STORE,
                             mmu_idx, retaddr);
    }
    /* Note: relies on the fact that tlb_fill() does not remove the
     * previous page from the TLB cache.  */
    for (i = DATA_SIZE - 1; i >= 0; i--) {
        if (little_endian) {
Quoted 2 lines

little_endian will have >99% of the time the same value, does it make sense to have a branch here?

The compiler should detect that little_endian is constant when it inlines the code and not bother generating a test/branch case for something that will never happen.

Even if it did though I doubt a local branch would stall the processor that much, have you counted how many instructions we execute once we are on the slow path?

Quoted 133 lines

Thank you,
alvise

Quoted 129 lines
                /* Little-endian extract.  */
                uint8_t val8 = val >> (i * 8);
        } else {
                /* Big-endian extract.  */
                uint8_t val8 = val >> (((DATA_SIZE - 1) * 8) - (i * 8));
        }
        /* Note the adjustment at the beginning of the function.
           Undo that for the recursion.  */
        glue(helper_ret_stb, MMUSUFFIX)(env, addr + i, val8,
                                        oi, retaddr + GETPC_ADJ);
    }
}
Quoted 59 lines
softmmu_template.h +20 -18
+
 void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
                        TCGMemOpIdx oi, uintptr_t retaddr)
 {
@@ -433,7 +459,8 @@ void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
             return;
         } else {
             if ((addr & (DATA_SIZE - 1)) != 0) {
-                goto do_unaligned_access;
+                glue(helper_le_st_name, _do_unl_access)(env, val, addr, mmu_idx,
+                                                        oi, retaddr);
             }
             iotlbentry = &env->iotlb[mmu_idx][index];
 
@@ -449,23 +476,8 @@ void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
     if (DATA_SIZE > 1
         && unlikely((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1
                      >= TARGET_PAGE_SIZE)) {
-        int i;
-    do_unaligned_access:
-        if ((get_memop(oi) & MO_AMASK) == MO_ALIGN) {
-            cpu_unaligned_access(ENV_GET_CPU(env), addr, MMU_DATA_STORE,
-                                 mmu_idx, retaddr);
-        }
-        /* XXX: not efficient, but simple */
-        /* Note: relies on the fact that tlb_fill() does not remove the
-         * previous page from the TLB cache.  */
-        for (i = DATA_SIZE - 1; i >= 0; i--) {
-            /* Little-endian extract.  */
-            uint8_t val8 = val >> (i * 8);
-            /* Note the adjustment at the beginning of the function.
-               Undo that for the recursion.  */
-            glue(helper_ret_stb, MMUSUFFIX)(env, addr + i, val8,
-                                            oi, retaddr + GETPC_ADJ);
-        }
+        glue(helper_le_st_name, _do_unl_access)(env, val, addr, oi, mmu_idx,
+                                                retaddr);
         return;
     }
 
@@ -485,6 +497,32 @@ void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
 }
 
 #if DATA_SIZE > 1
+static inline void glue(helper_be_st_name, _do_unl_access)(CPUArchState *env,
+                                                           DATA_TYPE val,
+                                                           target_ulong addr,
+                                                           TCGMemOpIdx oi,
+                                                           unsigned mmu_idx,
+                                                           uintptr_t retaddr)
+{
+    int i;
+
+    if ((get_memop(oi) & MO_AMASK) == MO_ALIGN) {
+        cpu_unaligned_access(ENV_GET_CPU(env), addr, MMU_DATA_STORE,
+                             mmu_idx, retaddr);
+    }
+    /* XXX: not efficient, but simple */
+    /* Note: relies on the fact that tlb_fill() does not remove the

softmmu_template.h void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val, · line 524

Quoted 10 lines
softmmu_template.h +10 -0
+     * previous page from the TLB cache.  */
+    for (i = DATA_SIZE - 1; i >= 0; i--) {
+        /* Big-endian extract.  */
+        uint8_t val8 = val >> (((DATA_SIZE - 1) * 8) - (i * 8));
+        /* Note the adjustment at the beginning of the function.
+           Undo that for the recursion.  */
+        glue(helper_ret_stb, MMUSUFFIX)(env, addr + i, val8,
+                                        oi, retaddr + GETPC_ADJ);
+    }
+}

Not that it matters if you combine to two as suggested because anything not called shouldn't generate the code.

Quoted 28 lines
softmmu_template.h +2 -12
 void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
                        TCGMemOpIdx oi, uintptr_t retaddr)
 {
@@ -548,7 +586,8 @@ void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
             return;
         } else {
             if ((addr & (DATA_SIZE - 1)) != 0) {
-                goto do_unaligned_access;
+                glue(helper_be_st_name, _do_unl_access)(env, val, addr, mmu_idx,
+                                                        oi, retaddr);
             }
             iotlbentry = &env->iotlb[mmu_idx][index];
 
@@ -564,23 +603,8 @@ void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
     if (DATA_SIZE > 1
         && unlikely((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1
                      >= TARGET_PAGE_SIZE)) {
-        int i;
-    do_unaligned_access:
-        if ((get_memop(oi) & MO_AMASK) == MO_ALIGN) {
-            cpu_unaligned_access(ENV_GET_CPU(env), addr, MMU_DATA_STORE,
-                                 mmu_idx, retaddr);
-        }
-        /* XXX: not efficient, but simple */
-        /* Note: relies on the fact that tlb_fill() does not remove the
-         * previous page from the TLB cache.  */
-        for (i = DATA_SIZE - 1; i >= 0; i--) {
-            /* Big-endian extract.  */

softmmu_template.h void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val, · line 609

Quoted 10 lines
softmmu_template.h +2 -6
-            uint8_t val8 = val >> (((DATA_SIZE - 1) * 8) - (i * 8));
-            /* Note the adjustment at the beginning of the function.
-               Undo that for the recursion.  */
-            glue(helper_ret_stb, MMUSUFFIX)(env, addr + i, val8,
-                                            oi, retaddr + GETPC_ADJ);
-        }
+        glue(helper_be_st_name, _do_unl_access)(env, val, addr, oi, mmu_idx,
+                                                retaddr);
         return;
     }

--
Alex Bennée

--
Alex Bennée

#33 lore raw

Re: [Qemu-devel] [RFC v6 10/14] softmmu: Simplify helper_*_st_name, wrap unaligned code

Quoted 64 lines · Alex Bennée
Quoted 56 lines · alvise rigo
Quoted 46 lines · Alex Bennée
Quoted 3 lines · Alvise Rigo

Attempting to simplify the helper_*_st_name, wrap the do_unaligned_access code into an inline function. Remove also the goto statement.

As I said in the other thread I think these sort of clean-ups can come before the ll/sc implementations and potentially get merged ahead of the rest of it.

Quoted 35 lines
---
 softmmu_template.h | 96 ++++++++++++++++++++++++++++++++++--------------------
 1 file changed, 60 insertions(+), 36 deletions(-)
softmmu_template.h +19 -0
index d3d5902..92f92b1 100644
@@ -370,6 +370,32 @@ static inline void glue(io_write, SUFFIX)(CPUArchState *env,
                                  iotlbentry->attrs);
 }
 
+static inline void glue(helper_le_st_name, _do_unl_access)(CPUArchState *env,
+                                                           DATA_TYPE val,
+                                                           target_ulong addr,
+                                                           TCGMemOpIdx oi,
+                                                           unsigned mmu_idx,
+                                                           uintptr_t retaddr)
+{
+    int i;
+
+    if ((get_memop(oi) & MO_AMASK) == MO_ALIGN) {
+        cpu_unaligned_access(ENV_GET_CPU(env), addr, MMU_DATA_STORE,
+                             mmu_idx, retaddr);
+    }
+    /* XXX: not efficient, but simple */
+    /* Note: relies on the fact that tlb_fill() does not remove the
+     * previous page from the TLB cache.  */
+    for (i = DATA_SIZE - 1; i >= 0; i--) {
+        /* Little-endian extract.  */
+        uint8_t val8 = val >> (i * 8);
Quoted 8 lines

softmmu_template.h static inline void glue(io_write, SUFFIX)(CPUArchState *env, · line 397

Quoted 6 lines
softmmu_template.h +6 -0
+        /* Note the adjustment at the beginning of the function.
+           Undo that for the recursion.  */
+        glue(helper_ret_stb, MMUSUFFIX)(env, addr + i, val8,
+                                        oi, retaddr + GETPC_ADJ);
+    }
+}

There is still duplication of 99% of the code here which is silly given

Quoted 4 lines

Then why should we keep this template-like design in the first place? I tried to keep the code duplication for performance reasons (otherwise how can we justify the two almost identical versions of the helper?), while making the code more compact and readable.

Quoted 6 lines

We shouldn't really - code duplication is bad for all the well known reasons. The main reason we need explicit helpers for the be/le case are because they are called directly from the TCG code which encodes the endianess decision in the call it makes. However that doesn't stop us making generic inline helpers (helpers for the helpers ;-) which the compiler can sort out.

I thought you wanted to make conditional all the le/be differences not just those helpers for the helpers...
So, if we are allowed to introduce this small overhead, all the helper_{le,be}_st_name_do_{unl,mmio,ram}_access can be squashed to helper_generic_st_do_{unl,mmio,ram}_access. I think this is want you proposed in the POC, right?

Quoted 30 lines
Quoted 27 lines
Quoted 15 lines

the compiler inlines the code anyway. If we gave the helper a more generic name and passed the endianess in via args I would hope the compiler did the sensible thing and constant fold the code. Something like:

static inline void glue(helper_generic_st_name, _do_unl_access)
(CPUArchState *env,
bool little_endian,
DATA_TYPE val,
target_ulong addr, TCGMemOpIdx oi,
unsigned mmu_idx,
uintptr_t retaddr)

Quoted 11 lines
{
    int i;

    if ((get_memop(oi) & MO_AMASK) == MO_ALIGN) {
        cpu_unaligned_access(ENV_GET_CPU(env), addr, MMU_DATA_STORE,
                             mmu_idx, retaddr);
    }
    /* Note: relies on the fact that tlb_fill() does not remove the
     * previous page from the TLB cache.  */
    for (i = DATA_SIZE - 1; i >= 0; i--) {
        if (little_endian) {
Quoted 2 lines

little_endian will have >99% of the time the same value, does it make sense to have a branch here?

Quoted 7 lines

The compiler should detect that little_endian is constant when it inlines the code and not bother generating a test/branch case for something that will never happen.

Even if it did though I doubt a local branch would stall the processor that much, have you counted how many instructions we execute once we are on the slow path?

Too many :)

Regards,
alvise

Quoted 138 lines
Quoted 134 lines

Thank you,
alvise

Quoted 129 lines
                /* Little-endian extract.  */
                uint8_t val8 = val >> (i * 8);
        } else {
                /* Big-endian extract.  */
                uint8_t val8 = val >> (((DATA_SIZE - 1) * 8) - (i * 8));
        }
        /* Note the adjustment at the beginning of the function.
           Undo that for the recursion.  */
        glue(helper_ret_stb, MMUSUFFIX)(env, addr + i, val8,
                                        oi, retaddr + GETPC_ADJ);
    }
}
Quoted 59 lines
softmmu_template.h +20 -18
+
 void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
                        TCGMemOpIdx oi, uintptr_t retaddr)
 {
@@ -433,7 +459,8 @@ void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
             return;
         } else {
             if ((addr & (DATA_SIZE - 1)) != 0) {
-                goto do_unaligned_access;
+                glue(helper_le_st_name, _do_unl_access)(env, val, addr, mmu_idx,
+                                                        oi, retaddr);
             }
             iotlbentry = &env->iotlb[mmu_idx][index];
 
@@ -449,23 +476,8 @@ void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
     if (DATA_SIZE > 1
         && unlikely((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1
                      >= TARGET_PAGE_SIZE)) {
-        int i;
-    do_unaligned_access:
-        if ((get_memop(oi) & MO_AMASK) == MO_ALIGN) {
-            cpu_unaligned_access(ENV_GET_CPU(env), addr, MMU_DATA_STORE,
-                                 mmu_idx, retaddr);
-        }
-        /* XXX: not efficient, but simple */
-        /* Note: relies on the fact that tlb_fill() does not remove the
-         * previous page from the TLB cache.  */
-        for (i = DATA_SIZE - 1; i >= 0; i--) {
-            /* Little-endian extract.  */
-            uint8_t val8 = val >> (i * 8);
-            /* Note the adjustment at the beginning of the function.
-               Undo that for the recursion.  */
-            glue(helper_ret_stb, MMUSUFFIX)(env, addr + i, val8,
-                                            oi, retaddr + GETPC_ADJ);
-        }
+        glue(helper_le_st_name, _do_unl_access)(env, val, addr, oi, mmu_idx,
+                                                retaddr);
         return;
     }
 
@@ -485,6 +497,32 @@ void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
 }
 
 #if DATA_SIZE > 1
+static inline void glue(helper_be_st_name, _do_unl_access)(CPUArchState *env,
+                                                           DATA_TYPE val,
+                                                           target_ulong addr,
+                                                           TCGMemOpIdx oi,
+                                                           unsigned mmu_idx,
+                                                           uintptr_t retaddr)
+{
+    int i;
+
+    if ((get_memop(oi) & MO_AMASK) == MO_ALIGN) {
+        cpu_unaligned_access(ENV_GET_CPU(env), addr, MMU_DATA_STORE,
+                             mmu_idx, retaddr);
+    }
+    /* XXX: not efficient, but simple */
+    /* Note: relies on the fact that tlb_fill() does not remove the

softmmu_template.h void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val, · line 524

Quoted 10 lines
softmmu_template.h +10 -0
+     * previous page from the TLB cache.  */
+    for (i = DATA_SIZE - 1; i >= 0; i--) {
+        /* Big-endian extract.  */
+        uint8_t val8 = val >> (((DATA_SIZE - 1) * 8) - (i * 8));
+        /* Note the adjustment at the beginning of the function.
+           Undo that for the recursion.  */
+        glue(helper_ret_stb, MMUSUFFIX)(env, addr + i, val8,
+                                        oi, retaddr + GETPC_ADJ);
+    }
+}

Not that it matters if you combine to two as suggested because anything not called shouldn't generate the code.

Quoted 28 lines
softmmu_template.h +2 -12
 void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
                        TCGMemOpIdx oi, uintptr_t retaddr)
 {
@@ -548,7 +586,8 @@ void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
             return;
         } else {
             if ((addr & (DATA_SIZE - 1)) != 0) {
-                goto do_unaligned_access;
+                glue(helper_be_st_name, _do_unl_access)(env, val, addr, mmu_idx,
+                                                        oi, retaddr);
             }
             iotlbentry = &env->iotlb[mmu_idx][index];
 
@@ -564,23 +603,8 @@ void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
     if (DATA_SIZE > 1
         && unlikely((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1
                      >= TARGET_PAGE_SIZE)) {
-        int i;
-    do_unaligned_access:
-        if ((get_memop(oi) & MO_AMASK) == MO_ALIGN) {
-            cpu_unaligned_access(ENV_GET_CPU(env), addr, MMU_DATA_STORE,
-                                 mmu_idx, retaddr);
-        }
-        /* XXX: not efficient, but simple */
-        /* Note: relies on the fact that tlb_fill() does not remove the
-         * previous page from the TLB cache.  */
-        for (i = DATA_SIZE - 1; i >= 0; i--) {
-            /* Big-endian extract.  */

softmmu_template.h void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val, · line 609

Quoted 10 lines
softmmu_template.h +2 -6
-            uint8_t val8 = val >> (((DATA_SIZE - 1) * 8) - (i * 8));
-            /* Note the adjustment at the beginning of the function.
-               Undo that for the recursion.  */
-            glue(helper_ret_stb, MMUSUFFIX)(env, addr + i, val8,
-                                            oi, retaddr + GETPC_ADJ);
-        }
+        glue(helper_be_st_name, _do_unl_access)(env, val, addr, oi, mmu_idx,
+                                                retaddr);
         return;
     }

--
Alex Bennée

--
Alex Bennée

#34 lore raw

Re: [Qemu-devel] [RFC v6 10/14] softmmu: Simplify helper_*_st_name, wrap unaligned code

Quoted 72 lines · alvise rigo
Quoted 64 lines · Alex Bennée
Quoted 56 lines · alvise rigo
Quoted 46 lines · Alex Bennée
Quoted 3 lines · Alvise Rigo

Attempting to simplify the helper_*_st_name, wrap the do_unaligned_access code into an inline function. Remove also the goto statement.

As I said in the other thread I think these sort of clean-ups can come before the ll/sc implementations and potentially get merged ahead of the rest of it.

Quoted 35 lines
---
 softmmu_template.h | 96 ++++++++++++++++++++++++++++++++++--------------------
 1 file changed, 60 insertions(+), 36 deletions(-)
softmmu_template.h +19 -0
index d3d5902..92f92b1 100644
@@ -370,6 +370,32 @@ static inline void glue(io_write, SUFFIX)(CPUArchState *env,
                                  iotlbentry->attrs);
 }
 
+static inline void glue(helper_le_st_name, _do_unl_access)(CPUArchState *env,
+                                                           DATA_TYPE val,
+                                                           target_ulong addr,
+                                                           TCGMemOpIdx oi,
+                                                           unsigned mmu_idx,
+                                                           uintptr_t retaddr)
+{
+    int i;
+
+    if ((get_memop(oi) & MO_AMASK) == MO_ALIGN) {
+        cpu_unaligned_access(ENV_GET_CPU(env), addr, MMU_DATA_STORE,
+                             mmu_idx, retaddr);
+    }
+    /* XXX: not efficient, but simple */
+    /* Note: relies on the fact that tlb_fill() does not remove the
+     * previous page from the TLB cache.  */
+    for (i = DATA_SIZE - 1; i >= 0; i--) {
+        /* Little-endian extract.  */
+        uint8_t val8 = val >> (i * 8);
Quoted 8 lines

softmmu_template.h static inline void glue(io_write, SUFFIX)(CPUArchState *env, · line 397

Quoted 6 lines
softmmu_template.h +6 -0
+        /* Note the adjustment at the beginning of the function.
+           Undo that for the recursion.  */
+        glue(helper_ret_stb, MMUSUFFIX)(env, addr + i, val8,
+                                        oi, retaddr + GETPC_ADJ);
+    }
+}

There is still duplication of 99% of the code here which is silly given

Quoted 4 lines

Then why should we keep this template-like design in the first place? I tried to keep the code duplication for performance reasons (otherwise how can we justify the two almost identical versions of the helper?), while making the code more compact and readable.

Quoted 6 lines

We shouldn't really - code duplication is bad for all the well known reasons. The main reason we need explicit helpers for the be/le case are because they are called directly from the TCG code which encodes the endianess decision in the call it makes. However that doesn't stop us making generic inline helpers (helpers for the helpers ;-) which the compiler can sort out.

Quoted 2 lines

I thought you wanted to make conditional all the le/be differences not just those helpers for the helpers...

That would be nice for it all but that involves tweaking the TCG->helper calls themselves. However if we are re-factoring common stuff from those helpers into inlines then we can at least reduce the duplication there.

Quoted 4 lines

So, if we are allowed to introduce this small overhead, all the helper_{le,be}_st_name_do_{unl,mmio,ram}_access can be squashed to helper_generic_st_do_{unl,mmio,ram}_access. I think this is want you proposed in the POC, right?

Well in theory it shouldn't introduce any overhead. However my proof is currently waiting on a bug fix to GDB's dissas command so I can show you the side by side assembly dump ;-)

Quoted 32 lines
Quoted 32 lines
Quoted 25 lines
Quoted 13 lines

the compiler inlines the code anyway. If we gave the helper a more generic name and passed the endianess in via args I would hope the compiler did the sensible thing and constant fold the code. Something like:

static inline void glue(helper_generic_st_name, _do_unl_access)
(CPUArchState *env,
bool little_endian,
DATA_TYPE val,
target_ulong addr, TCGMemOpIdx oi,
unsigned mmu_idx,
uintptr_t retaddr)

Quoted 11 lines
{
    int i;

    if ((get_memop(oi) & MO_AMASK) == MO_ALIGN) {
        cpu_unaligned_access(ENV_GET_CPU(env), addr, MMU_DATA_STORE,
                             mmu_idx, retaddr);
    }
    /* Note: relies on the fact that tlb_fill() does not remove the
     * previous page from the TLB cache.  */
    for (i = DATA_SIZE - 1; i >= 0; i--) {
        if (little_endian) {
Quoted 2 lines

little_endian will have >99% of the time the same value, does it make sense to have a branch here?

The compiler should detect that little_endian is constant when it inlines the code and not bother generating a test/branch case for something that will never happen.

Quoted 5 lines
Quoted 3 lines

Even if it did though I doubt a local branch would stall the processor that much, have you counted how many instructions we execute once we are on the slow path?

Too many :)

Indeed, that is why its SLOOOOW ;-)

Quoted 142 lines

Regards,
alvise

Quoted 138 lines
Quoted 134 lines

Thank you,
alvise

Quoted 129 lines
                /* Little-endian extract.  */
                uint8_t val8 = val >> (i * 8);
        } else {
                /* Big-endian extract.  */
                uint8_t val8 = val >> (((DATA_SIZE - 1) * 8) - (i * 8));
        }
        /* Note the adjustment at the beginning of the function.
           Undo that for the recursion.  */
        glue(helper_ret_stb, MMUSUFFIX)(env, addr + i, val8,
                                        oi, retaddr + GETPC_ADJ);
    }
}
Quoted 59 lines
softmmu_template.h +20 -18
+
 void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
                        TCGMemOpIdx oi, uintptr_t retaddr)
 {
@@ -433,7 +459,8 @@ void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
             return;
         } else {
             if ((addr & (DATA_SIZE - 1)) != 0) {
-                goto do_unaligned_access;
+                glue(helper_le_st_name, _do_unl_access)(env, val, addr, mmu_idx,
+                                                        oi, retaddr);
             }
             iotlbentry = &env->iotlb[mmu_idx][index];
 
@@ -449,23 +476,8 @@ void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
     if (DATA_SIZE > 1
         && unlikely((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1
                      >= TARGET_PAGE_SIZE)) {
-        int i;
-    do_unaligned_access:
-        if ((get_memop(oi) & MO_AMASK) == MO_ALIGN) {
-            cpu_unaligned_access(ENV_GET_CPU(env), addr, MMU_DATA_STORE,
-                                 mmu_idx, retaddr);
-        }
-        /* XXX: not efficient, but simple */
-        /* Note: relies on the fact that tlb_fill() does not remove the
-         * previous page from the TLB cache.  */
-        for (i = DATA_SIZE - 1; i >= 0; i--) {
-            /* Little-endian extract.  */
-            uint8_t val8 = val >> (i * 8);
-            /* Note the adjustment at the beginning of the function.
-               Undo that for the recursion.  */
-            glue(helper_ret_stb, MMUSUFFIX)(env, addr + i, val8,
-                                            oi, retaddr + GETPC_ADJ);
-        }
+        glue(helper_le_st_name, _do_unl_access)(env, val, addr, oi, mmu_idx,
+                                                retaddr);
         return;
     }
 
@@ -485,6 +497,32 @@ void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
 }
 
 #if DATA_SIZE > 1
+static inline void glue(helper_be_st_name, _do_unl_access)(CPUArchState *env,
+                                                           DATA_TYPE val,
+                                                           target_ulong addr,
+                                                           TCGMemOpIdx oi,
+                                                           unsigned mmu_idx,
+                                                           uintptr_t retaddr)
+{
+    int i;
+
+    if ((get_memop(oi) & MO_AMASK) == MO_ALIGN) {
+        cpu_unaligned_access(ENV_GET_CPU(env), addr, MMU_DATA_STORE,
+                             mmu_idx, retaddr);
+    }
+    /* XXX: not efficient, but simple */
+    /* Note: relies on the fact that tlb_fill() does not remove the

softmmu_template.h void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val, · line 524

Quoted 10 lines
softmmu_template.h +10 -0
+     * previous page from the TLB cache.  */
+    for (i = DATA_SIZE - 1; i >= 0; i--) {
+        /* Big-endian extract.  */
+        uint8_t val8 = val >> (((DATA_SIZE - 1) * 8) - (i * 8));
+        /* Note the adjustment at the beginning of the function.
+           Undo that for the recursion.  */
+        glue(helper_ret_stb, MMUSUFFIX)(env, addr + i, val8,
+                                        oi, retaddr + GETPC_ADJ);
+    }
+}

Not that it matters if you combine to two as suggested because anything not called shouldn't generate the code.

Quoted 28 lines
softmmu_template.h +2 -12
 void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
                        TCGMemOpIdx oi, uintptr_t retaddr)
 {
@@ -548,7 +586,8 @@ void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
             return;
         } else {
             if ((addr & (DATA_SIZE - 1)) != 0) {
-                goto do_unaligned_access;
+                glue(helper_be_st_name, _do_unl_access)(env, val, addr, mmu_idx,
+                                                        oi, retaddr);
             }
             iotlbentry = &env->iotlb[mmu_idx][index];
 
@@ -564,23 +603,8 @@ void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
     if (DATA_SIZE > 1
         && unlikely((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1
                      >= TARGET_PAGE_SIZE)) {
-        int i;
-    do_unaligned_access:
-        if ((get_memop(oi) & MO_AMASK) == MO_ALIGN) {
-            cpu_unaligned_access(ENV_GET_CPU(env), addr, MMU_DATA_STORE,
-                                 mmu_idx, retaddr);
-        }
-        /* XXX: not efficient, but simple */
-        /* Note: relies on the fact that tlb_fill() does not remove the
-         * previous page from the TLB cache.  */
-        for (i = DATA_SIZE - 1; i >= 0; i--) {
-            /* Big-endian extract.  */

softmmu_template.h void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val, · line 609

Quoted 10 lines
softmmu_template.h +2 -6
-            uint8_t val8 = val >> (((DATA_SIZE - 1) * 8) - (i * 8));
-            /* Note the adjustment at the beginning of the function.
-               Undo that for the recursion.  */
-            glue(helper_ret_stb, MMUSUFFIX)(env, addr + i, val8,
-                                            oi, retaddr + GETPC_ADJ);
-        }
+        glue(helper_be_st_name, _do_unl_access)(env, val, addr, oi, mmu_idx,
+                                                retaddr);
         return;
     }

--
Alex Bennée

--
Alex Bennée

--
Alex Bennée

#35 lore raw

Re: [Qemu-devel] [RFC v6 10/14] softmmu: Simplify helper_*_st_name, wrap unaligned code

In reply to Alvise Rigo
Quoted 49 lines · Alvise Rigo

Attempting to simplify the helper_*_st_name, wrap the do_unaligned_access code into an inline function. Remove also the goto statement.

---
 softmmu_template.h | 96 ++++++++++++++++++++++++++++++++++--------------------
 1 file changed, 60 insertions(+), 36 deletions(-)
softmmu_template.h +26 -0
index d3d5902..92f92b1 100644
@@ -370,6 +370,32 @@ static inline void glue(io_write, SUFFIX)(CPUArchState *env,
                                  iotlbentry->attrs);
 }
 
+static inline void glue(helper_le_st_name, _do_unl_access)(CPUArchState *env,
+                                                           DATA_TYPE val,
+                                                           target_ulong addr,
+                                                           TCGMemOpIdx oi,
+                                                           unsigned mmu_idx,
+                                                           uintptr_t retaddr)
+{
+    int i;
+
+    if ((get_memop(oi) & MO_AMASK) == MO_ALIGN) {
+        cpu_unaligned_access(ENV_GET_CPU(env), addr, MMU_DATA_STORE,
+                             mmu_idx, retaddr);
+    }
+    /* XXX: not efficient, but simple */
+    /* Note: relies on the fact that tlb_fill() does not remove the
+     * previous page from the TLB cache.  */
+    for (i = DATA_SIZE - 1; i >= 0; i--) {
+        /* Little-endian extract.  */
+        uint8_t val8 = val >> (i * 8);
+        /* Note the adjustment at the beginning of the function.
+           Undo that for the recursion.  */
+        glue(helper_ret_stb, MMUSUFFIX)(env, addr + i, val8,
+                                        oi, retaddr + GETPC_ADJ);
+    }
+}
+
 void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
                        TCGMemOpIdx oi, uintptr_t retaddr)
 {

softmmu_template.h void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val, · line 463

Quoted 6 lines
softmmu_template.h +2 -1
@@ -433,7 +459,8 @@ void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
             return;
         } else {
             if ((addr & (DATA_SIZE - 1)) != 0) {
-                goto do_unaligned_access;
+                glue(helper_le_st_name, _do_unl_access)(env, val, addr, mmu_idx,
+                                                        oi, retaddr);

I've just noticed this drops and implicit return. However I'm seeing if I can put together an RFC cleanup patch set for softmmu based on these plus a few other clean-ups. I'll CC when done.

Quoted 87 lines
softmmu_template.h +30 -29
             }
             iotlbentry = &env->iotlb[mmu_idx][index];
 
@@ -449,23 +476,8 @@ void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
     if (DATA_SIZE > 1
         && unlikely((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1
                      >= TARGET_PAGE_SIZE)) {
-        int i;
-    do_unaligned_access:
-        if ((get_memop(oi) & MO_AMASK) == MO_ALIGN) {
-            cpu_unaligned_access(ENV_GET_CPU(env), addr, MMU_DATA_STORE,
-                                 mmu_idx, retaddr);
-        }
-        /* XXX: not efficient, but simple */
-        /* Note: relies on the fact that tlb_fill() does not remove the
-         * previous page from the TLB cache.  */
-        for (i = DATA_SIZE - 1; i >= 0; i--) {
-            /* Little-endian extract.  */
-            uint8_t val8 = val >> (i * 8);
-            /* Note the adjustment at the beginning of the function.
-               Undo that for the recursion.  */
-            glue(helper_ret_stb, MMUSUFFIX)(env, addr + i, val8,
-                                            oi, retaddr + GETPC_ADJ);
-        }
+        glue(helper_le_st_name, _do_unl_access)(env, val, addr, oi, mmu_idx,
+                                                retaddr);
         return;
     }
 
@@ -485,6 +497,32 @@ void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
 }
 
 #if DATA_SIZE > 1
+static inline void glue(helper_be_st_name, _do_unl_access)(CPUArchState *env,
+                                                           DATA_TYPE val,
+                                                           target_ulong addr,
+                                                           TCGMemOpIdx oi,
+                                                           unsigned mmu_idx,
+                                                           uintptr_t retaddr)
+{
+    int i;
+
+    if ((get_memop(oi) & MO_AMASK) == MO_ALIGN) {
+        cpu_unaligned_access(ENV_GET_CPU(env), addr, MMU_DATA_STORE,
+                             mmu_idx, retaddr);
+    }
+    /* XXX: not efficient, but simple */
+    /* Note: relies on the fact that tlb_fill() does not remove the
+     * previous page from the TLB cache.  */
+    for (i = DATA_SIZE - 1; i >= 0; i--) {
+        /* Big-endian extract.  */
+        uint8_t val8 = val >> (((DATA_SIZE - 1) * 8) - (i * 8));
+        /* Note the adjustment at the beginning of the function.
+           Undo that for the recursion.  */
+        glue(helper_ret_stb, MMUSUFFIX)(env, addr + i, val8,
+                                        oi, retaddr + GETPC_ADJ);
+    }
+}
+
 void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
                        TCGMemOpIdx oi, uintptr_t retaddr)
 {
@@ -548,7 +586,8 @@ void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
             return;
         } else {
             if ((addr & (DATA_SIZE - 1)) != 0) {
-                goto do_unaligned_access;
+                glue(helper_be_st_name, _do_unl_access)(env, val, addr, mmu_idx,
+                                                        oi, retaddr);
             }
             iotlbentry = &env->iotlb[mmu_idx][index];
 
@@ -564,23 +603,8 @@ void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
     if (DATA_SIZE > 1
         && unlikely((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1
                      >= TARGET_PAGE_SIZE)) {
-        int i;
-    do_unaligned_access:
-        if ((get_memop(oi) & MO_AMASK) == MO_ALIGN) {
-            cpu_unaligned_access(ENV_GET_CPU(env), addr, MMU_DATA_STORE,
-                                 mmu_idx, retaddr);
-        }
-        /* XXX: not efficient, but simple */
-        /* Note: relies on the fact that tlb_fill() does not remove the
-         * previous page from the TLB cache.  */
-        for (i = DATA_SIZE - 1; i >= 0; i--) {
-            /* Big-endian extract.  */

softmmu_template.h void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val, · line 609

Quoted 10 lines
softmmu_template.h +2 -6
-            uint8_t val8 = val >> (((DATA_SIZE - 1) * 8) - (i * 8));
-            /* Note the adjustment at the beginning of the function.
-               Undo that for the recursion.  */
-            glue(helper_ret_stb, MMUSUFFIX)(env, addr + i, val8,
-                                            oi, retaddr + GETPC_ADJ);
-        }
+        glue(helper_be_st_name, _do_unl_access)(env, val, addr, oi, mmu_idx,
+                                                retaddr);
         return;
     }

--
Alex Bennée

#36 lore raw

RFC v6 11/14 softmmu: Simplify helper_*_st_name, wrap MMIO code

In reply to Alvise Rigo

Attempting to simplify the helper_*_st_name, wrap the MMIO code into an inline function.

---
 softmmu_template.h | 64 +++++++++++++++++++++++++++++++++++++-----------------
 1 file changed, 44 insertions(+), 20 deletions(-)
softmmu_template.h +44 -20
index 92f92b1..2ebf527 100644
@@ -396,6 +396,26 @@ static inline void glue(helper_le_st_name, _do_unl_access)(CPUArchState *env,
     }
 }
 
+static inline void glue(helper_le_st_name, _do_mmio_access)(CPUArchState *env,
+                                                            DATA_TYPE val,
+                                                            target_ulong addr,
+                                                            TCGMemOpIdx oi,
+                                                            unsigned mmu_idx,
+                                                            int index,
+                                                            uintptr_t retaddr)
+{
+    CPUIOTLBEntry *iotlbentry = &env->iotlb[mmu_idx][index];
+
+    if ((addr & (DATA_SIZE - 1)) != 0) {
+        glue(helper_le_st_name, _do_unl_access)(env, val, addr, mmu_idx,
+                                                oi, retaddr);
+    }
+    /* ??? Note that the io helpers always read data in the target
+       byte ordering.  We should push the LE/BE request down into io.  */
+    val = TGT_LE(val);
+    glue(io_write, SUFFIX)(env, iotlbentry, val, addr, retaddr);
+}
+
 void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
                        TCGMemOpIdx oi, uintptr_t retaddr)
 {
@@ -458,16 +478,8 @@ void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
 
             return;
         } else {
-            if ((addr & (DATA_SIZE - 1)) != 0) {
-                glue(helper_le_st_name, _do_unl_access)(env, val, addr, mmu_idx,
-                                                        oi, retaddr);
-            }
-            iotlbentry = &env->iotlb[mmu_idx][index];
-
-            /* ??? Note that the io helpers always read data in the target
-               byte ordering.  We should push the LE/BE request down into io.  */
-            val = TGT_LE(val);
-            glue(io_write, SUFFIX)(env, iotlbentry, val, addr, retaddr);
+            glue(helper_le_st_name, _do_mmio_access)(env, val, addr, oi,
+                                                     mmu_idx, index, retaddr);
             return;
         }
     }
@@ -523,6 +535,26 @@ static inline void glue(helper_be_st_name, _do_unl_access)(CPUArchState *env,
     }
 }
 
+static inline void glue(helper_be_st_name, _do_mmio_access)(CPUArchState *env,
+                                                            DATA_TYPE val,
+                                                            target_ulong addr,
+                                                            TCGMemOpIdx oi,
+                                                            unsigned mmu_idx,
+                                                            int index,
+                                                            uintptr_t retaddr)
+{
+    CPUIOTLBEntry *iotlbentry = &env->iotlb[mmu_idx][index];
+
+    if ((addr & (DATA_SIZE - 1)) != 0) {
+        glue(helper_be_st_name, _do_unl_access)(env, val, addr, mmu_idx,
+                                                oi, retaddr);
+    }
+    /* ??? Note that the io helpers always read data in the target
+       byte ordering.  We should push the LE/BE request down into io.  */
+    val = TGT_BE(val);
+    glue(io_write, SUFFIX)(env, iotlbentry, val, addr, retaddr);
+}
+
 void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
                        TCGMemOpIdx oi, uintptr_t retaddr)
 {
@@ -585,16 +617,8 @@ void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
 
             return;
         } else {
-            if ((addr & (DATA_SIZE - 1)) != 0) {
-                glue(helper_be_st_name, _do_unl_access)(env, val, addr, mmu_idx,
-                                                        oi, retaddr);
-            }
-            iotlbentry = &env->iotlb[mmu_idx][index];
-
-            /* ??? Note that the io helpers always read data in the target
-               byte ordering.  We should push the LE/BE request down into io.  */
-            val = TGT_BE(val);
-            glue(io_write, SUFFIX)(env, iotlbentry, val, addr, retaddr);
+            glue(helper_be_st_name, _do_mmio_access)(env, val, addr, oi,
+                                                     mmu_idx, index, retaddr);
             return;
         }
     }
Signature · 1 line
2.6.4
#37 lore raw

Re: [Qemu-devel] [RFC v6 11/14] softmmu: Simplify helper_*_st_name, wrap MMIO code

Quoted 28 lines · Alvise Rigo

Attempting to simplify the helper_*_st_name, wrap the MMIO code into an inline function.

---
 softmmu_template.h | 64 +++++++++++++++++++++++++++++++++++++-----------------
 1 file changed, 44 insertions(+), 20 deletions(-)
softmmu_template.h +10 -0
index 92f92b1..2ebf527 100644
@@ -396,6 +396,26 @@ static inline void glue(helper_le_st_name, _do_unl_access)(CPUArchState *env,
     }
 }
 
+static inline void glue(helper_le_st_name, _do_mmio_access)(CPUArchState *env,
+                                                            DATA_TYPE val,
+                                                            target_ulong addr,
+                                                            TCGMemOpIdx oi,
+                                                            unsigned mmu_idx,
+                                                            int index,
+                                                            uintptr_t retaddr)
+{
+    CPUIOTLBEntry *iotlbentry = &env->iotlb[mmu_idx][index];
+

softmmu_template.h static inline void glue(helper_le_st_name, _do_unl_access)(CPUArchState *env, · line 418

Quoted 10 lines
softmmu_template.h +10 -0
+    if ((addr & (DATA_SIZE - 1)) != 0) {
+        glue(helper_le_st_name, _do_unl_access)(env, val, addr, mmu_idx,
+                                                oi, retaddr);
+    }
+    /* ??? Note that the io helpers always read data in the target
+       byte ordering.  We should push the LE/BE request down into io.  */
+    val = TGT_LE(val);
+    glue(io_write, SUFFIX)(env, iotlbentry, val, addr, retaddr);
+}
+

Some comment as previous patches. I think we can have a single function that is shared between both helpers.

Quoted 58 lines
softmmu_template.h +22 -15
 void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
                        TCGMemOpIdx oi, uintptr_t retaddr)
 {
@@ -458,16 +478,8 @@ void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
 
             return;
         } else {
-            if ((addr & (DATA_SIZE - 1)) != 0) {
-                glue(helper_le_st_name, _do_unl_access)(env, val, addr, mmu_idx,
-                                                        oi, retaddr);
-            }
-            iotlbentry = &env->iotlb[mmu_idx][index];
-
-            /* ??? Note that the io helpers always read data in the target
-               byte ordering.  We should push the LE/BE request down into io.  */
-            val = TGT_LE(val);
-            glue(io_write, SUFFIX)(env, iotlbentry, val, addr, retaddr);
+            glue(helper_le_st_name, _do_mmio_access)(env, val, addr, oi,
+                                                     mmu_idx, index, retaddr);
             return;
         }
     }
@@ -523,6 +535,26 @@ static inline void glue(helper_be_st_name, _do_unl_access)(CPUArchState *env,
     }
 }
 
+static inline void glue(helper_be_st_name, _do_mmio_access)(CPUArchState *env,
+                                                            DATA_TYPE val,
+                                                            target_ulong addr,
+                                                            TCGMemOpIdx oi,
+                                                            unsigned mmu_idx,
+                                                            int index,
+                                                            uintptr_t retaddr)
+{
+    CPUIOTLBEntry *iotlbentry = &env->iotlb[mmu_idx][index];
+
+    if ((addr & (DATA_SIZE - 1)) != 0) {
+        glue(helper_be_st_name, _do_unl_access)(env, val, addr, mmu_idx,
+                                                oi, retaddr);
+    }
+    /* ??? Note that the io helpers always read data in the target
+       byte ordering.  We should push the LE/BE request down into io.  */
+    val = TGT_BE(val);
+    glue(io_write, SUFFIX)(env, iotlbentry, val, addr, retaddr);
+}
+
 void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
                        TCGMemOpIdx oi, uintptr_t retaddr)
 {
@@ -585,16 +617,8 @@ void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
 
             return;
         } else {
-            if ((addr & (DATA_SIZE - 1)) != 0) {
-                glue(helper_be_st_name, _do_unl_access)(env, val, addr, mmu_idx,
-                                                        oi, retaddr);
-            }
-            iotlbentry = &env->iotlb[mmu_idx][index];

softmmu_template.h void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val, · line 624

Quoted 10 lines
softmmu_template.h +2 -5
-
-            /* ??? Note that the io helpers always read data in the target
-               byte ordering.  We should push the LE/BE request down into io.  */
-            val = TGT_BE(val);
-            glue(io_write, SUFFIX)(env, iotlbentry, val, addr, retaddr);
+            glue(helper_be_st_name, _do_mmio_access)(env, val, addr, oi,
+                                                     mmu_idx, index, retaddr);
             return;
         }
     }

--
Alex Bennée

#38 lore raw

Re: [Qemu-devel] [RFC v6 11/14] softmmu: Simplify helper_*_st_name, wrap MMIO code

Quoted 36 lines · Alex Bennée
Quoted 33 lines · Alvise Rigo

Attempting to simplify the helper_*_st_name, wrap the MMIO code into an inline function.

---
 softmmu_template.h | 64 +++++++++++++++++++++++++++++++++++++-----------------
 1 file changed, 44 insertions(+), 20 deletions(-)
softmmu_template.h +15 -0
index 92f92b1..2ebf527 100644
@@ -396,6 +396,26 @@ static inline void glue(helper_le_st_name, _do_unl_access)(CPUArchState *env,
     }
 }
 
+static inline void glue(helper_le_st_name, _do_mmio_access)(CPUArchState *env,
+                                                            DATA_TYPE val,
+                                                            target_ulong addr,
+                                                            TCGMemOpIdx oi,
+                                                            unsigned mmu_idx,
+                                                            int index,
+                                                            uintptr_t retaddr)
+{
+    CPUIOTLBEntry *iotlbentry = &env->iotlb[mmu_idx][index];
+
+    if ((addr & (DATA_SIZE - 1)) != 0) {
+        glue(helper_le_st_name, _do_unl_access)(env, val, addr, mmu_idx,
+                                                oi, retaddr);
+    }
+    /* ??? Note that the io helpers always read data in the target
Quoted 8 lines

softmmu_template.h static inline void glue(helper_le_st_name, _do_unl_access)(CPUArchState *env, · line 418

Quoted 5 lines
softmmu_template.h +5 -0
+       byte ordering.  We should push the LE/BE request down into io.  */
+    val = TGT_LE(val);
+    glue(io_write, SUFFIX)(env, iotlbentry, val, addr, retaddr);
+}
+

Some comment as previous patches. I think we can have a single function that is shared between both helpers.

Of course. If the objdump you got from this version and the version with single helper is basically the same, then there's no reason to make two distinct variants.

Thank you,
alvise

Quoted 73 lines
Quoted 59 lines
softmmu_template.h +22 -15
 void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
                        TCGMemOpIdx oi, uintptr_t retaddr)
 {
@@ -458,16 +478,8 @@ void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
 
             return;
         } else {
-            if ((addr & (DATA_SIZE - 1)) != 0) {
-                glue(helper_le_st_name, _do_unl_access)(env, val, addr, mmu_idx,
-                                                        oi, retaddr);
-            }
-            iotlbentry = &env->iotlb[mmu_idx][index];
-
-            /* ??? Note that the io helpers always read data in the target
-               byte ordering.  We should push the LE/BE request down into io.  */
-            val = TGT_LE(val);
-            glue(io_write, SUFFIX)(env, iotlbentry, val, addr, retaddr);
+            glue(helper_le_st_name, _do_mmio_access)(env, val, addr, oi,
+                                                     mmu_idx, index, retaddr);
             return;
         }
     }
@@ -523,6 +535,26 @@ static inline void glue(helper_be_st_name, _do_unl_access)(CPUArchState *env,
     }
 }
 
+static inline void glue(helper_be_st_name, _do_mmio_access)(CPUArchState *env,
+                                                            DATA_TYPE val,
+                                                            target_ulong addr,
+                                                            TCGMemOpIdx oi,
+                                                            unsigned mmu_idx,
+                                                            int index,
+                                                            uintptr_t retaddr)
+{
+    CPUIOTLBEntry *iotlbentry = &env->iotlb[mmu_idx][index];
+
+    if ((addr & (DATA_SIZE - 1)) != 0) {
+        glue(helper_be_st_name, _do_unl_access)(env, val, addr, mmu_idx,
+                                                oi, retaddr);
+    }
+    /* ??? Note that the io helpers always read data in the target
+       byte ordering.  We should push the LE/BE request down into io.  */
+    val = TGT_BE(val);
+    glue(io_write, SUFFIX)(env, iotlbentry, val, addr, retaddr);
+}
+
 void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
                        TCGMemOpIdx oi, uintptr_t retaddr)
 {
@@ -585,16 +617,8 @@ void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
 
             return;
         } else {
-            if ((addr & (DATA_SIZE - 1)) != 0) {
-                glue(helper_be_st_name, _do_unl_access)(env, val, addr, mmu_idx,
-                                                        oi, retaddr);
-            }
-            iotlbentry = &env->iotlb[mmu_idx][index];

softmmu_template.h void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val, · line 624

Quoted 10 lines
softmmu_template.h +2 -5
-
-            /* ??? Note that the io helpers always read data in the target
-               byte ordering.  We should push the LE/BE request down into io.  */
-            val = TGT_BE(val);
-            glue(io_write, SUFFIX)(env, iotlbentry, val, addr, retaddr);
+            glue(helper_be_st_name, _do_mmio_access)(env, val, addr, oi,
+                                                     mmu_idx, index, retaddr);
             return;
         }
     }

--
Alex Bennée

#39 lore raw

RFC v6 12/14 softmmu: Simplify helper_*_st_name, wrap RAM code

In reply to Alvise Rigo

Attempting to simplify the helper_*_st_name, wrap the code relative to a RAM access into an inline function.

---
 softmmu_template.h | 110 +++++++++++++++++++++++++++++++++--------------------
 1 file changed, 68 insertions(+), 42 deletions(-)
softmmu_template.h +68 -42
index 2ebf527..262c95f 100644
@@ -416,13 +416,46 @@ static inline void glue(helper_le_st_name, _do_mmio_access)(CPUArchState *env,
     glue(io_write, SUFFIX)(env, iotlbentry, val, addr, retaddr);
 }
 
+static inline void glue(helper_le_st_name, _do_ram_access)(CPUArchState *env,
+                                                           DATA_TYPE val,
+                                                           target_ulong addr,
+                                                           TCGMemOpIdx oi,
+                                                           unsigned mmu_idx,
+                                                           int index,
+                                                           uintptr_t retaddr)
+{
+    uintptr_t haddr;
+
+    /* Handle slow unaligned access (it spans two pages or IO).  */
+    if (DATA_SIZE > 1
+        && unlikely((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1
+                     >= TARGET_PAGE_SIZE)) {
+        glue(helper_le_st_name, _do_unl_access)(env, val, addr, oi, mmu_idx,
+                                                retaddr);
+        return;
+    }
+
+    /* Handle aligned access or unaligned access in the same page.  */
+    if ((addr & (DATA_SIZE - 1)) != 0
+        && (get_memop(oi) & MO_AMASK) == MO_ALIGN) {
+        cpu_unaligned_access(ENV_GET_CPU(env), addr, MMU_DATA_STORE,
+                             mmu_idx, retaddr);
+    }
+
+    haddr = addr + env->tlb_table[mmu_idx][index].addend;
+#if DATA_SIZE == 1
+    glue(glue(st, SUFFIX), _p)((uint8_t *)haddr, val);
+#else
+    glue(glue(st, SUFFIX), _le_p)((uint8_t *)haddr, val);
+#endif
+}
+
 void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
                        TCGMemOpIdx oi, uintptr_t retaddr)
 {
     unsigned mmu_idx = get_mmuidx(oi);
     int index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
     target_ulong tlb_addr = env->tlb_table[mmu_idx][index].addr_write;
-    uintptr_t haddr;
 
     /* Adjust the given return address.  */
     retaddr -= GETPC_ADJ;
@@ -484,28 +517,8 @@ void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
         }
     }
 
-    /* Handle slow unaligned access (it spans two pages or IO).  */
-    if (DATA_SIZE > 1
-        && unlikely((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1
-                     >= TARGET_PAGE_SIZE)) {
-        glue(helper_le_st_name, _do_unl_access)(env, val, addr, oi, mmu_idx,
-                                                retaddr);
-        return;
-    }
-
-    /* Handle aligned access or unaligned access in the same page.  */
-    if ((addr & (DATA_SIZE - 1)) != 0
-        && (get_memop(oi) & MO_AMASK) == MO_ALIGN) {
-        cpu_unaligned_access(ENV_GET_CPU(env), addr, MMU_DATA_STORE,
-                             mmu_idx, retaddr);
-    }
-
-    haddr = addr + env->tlb_table[mmu_idx][index].addend;
-#if DATA_SIZE == 1
-    glue(glue(st, SUFFIX), _p)((uint8_t *)haddr, val);
-#else
-    glue(glue(st, SUFFIX), _le_p)((uint8_t *)haddr, val);
-#endif
+    glue(helper_le_st_name, _do_ram_access)(env, val, addr, oi, mmu_idx, index,
+                                            retaddr);
 }
 
 #if DATA_SIZE > 1
@@ -555,13 +568,42 @@ static inline void glue(helper_be_st_name, _do_mmio_access)(CPUArchState *env,
     glue(io_write, SUFFIX)(env, iotlbentry, val, addr, retaddr);
 }
 
+static inline void glue(helper_be_st_name, _do_ram_access)(CPUArchState *env,
+                                                           DATA_TYPE val,
+                                                           target_ulong addr,
+                                                           TCGMemOpIdx oi,
+                                                           unsigned mmu_idx,
+                                                           int index,
+                                                           uintptr_t retaddr)
+{
+    uintptr_t haddr;
+
+    /* Handle slow unaligned access (it spans two pages or IO).  */
+    if (DATA_SIZE > 1
+        && unlikely((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1
+                     >= TARGET_PAGE_SIZE)) {
+        glue(helper_be_st_name, _do_unl_access)(env, val, addr, oi, mmu_idx,
+                                                retaddr);
+        return;
+    }
+
+    /* Handle aligned access or unaligned access in the same page.  */
+    if ((addr & (DATA_SIZE - 1)) != 0
+        && (get_memop(oi) & MO_AMASK) == MO_ALIGN) {
+        cpu_unaligned_access(ENV_GET_CPU(env), addr, MMU_DATA_STORE,
+                             mmu_idx, retaddr);
+    }
+
+    haddr = addr + env->tlb_table[mmu_idx][index].addend;
+    glue(glue(st, SUFFIX), _be_p)((uint8_t *)haddr, val);
+}
+
 void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
                        TCGMemOpIdx oi, uintptr_t retaddr)
 {
     unsigned mmu_idx = get_mmuidx(oi);
     int index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
     target_ulong tlb_addr = env->tlb_table[mmu_idx][index].addr_write;
-    uintptr_t haddr;
 
     /* Adjust the given return address.  */
     retaddr -= GETPC_ADJ;
@@ -623,24 +665,8 @@ void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
         }
     }
 
-    /* Handle slow unaligned access (it spans two pages or IO).  */
-    if (DATA_SIZE > 1
-        && unlikely((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1
-                     >= TARGET_PAGE_SIZE)) {
-        glue(helper_be_st_name, _do_unl_access)(env, val, addr, oi, mmu_idx,
-                                                retaddr);
-        return;
-    }
-
-    /* Handle aligned access or unaligned access in the same page.  */
-    if ((addr & (DATA_SIZE - 1)) != 0
-        && (get_memop(oi) & MO_AMASK) == MO_ALIGN) {
-        cpu_unaligned_access(ENV_GET_CPU(env), addr, MMU_DATA_STORE,
-                             mmu_idx, retaddr);
-    }
-
-    haddr = addr + env->tlb_table[mmu_idx][index].addend;
-    glue(glue(st, SUFFIX), _be_p)((uint8_t *)haddr, val);
+    glue(helper_be_st_name, _do_ram_access)(env, val, addr, oi, mmu_idx, index,
+                                            retaddr);
 }
 #endif /* DATA_SIZE > 1 */
 
Signature · 1 line
2.6.4
#40 lore raw

Re: [Qemu-devel] [RFC v6 12/14] softmmu: Simplify helper_*_st_name, wrap RAM code

Quoted 2 lines · Alvise Rigo

Attempting to simplify the helper_*_st_name, wrap the code relative to a RAM access into an inline function.

This commit breaks a default x86_64-softmmu build:

CC x86_64-softmmu/../hw/audio/pcspk.o
In file included from /home/alex/lsrc/qemu/qemu.git/cputlb.c:527:0:
/home/alex/lsrc/qemu/qemu.git/softmmu_template.h: In function ‘helper_ret_stb_mmu’:
/home/alex/lsrc/qemu/qemu.git/softmmu_template.h:503:13: error: ‘haddr’ undeclared (first use in this function)

             haddr = addr + env->tlb_table[mmu_idx][index].addend;
             ^

/home/alex/lsrc/qemu/qemu.git/softmmu_template.h:503:13: note: each undeclared identifier is reported only once for each function it appears in In file included from /home/alex/lsrc/qemu/qemu.git/cputlb.c:530:0:
/home/alex/lsrc/qemu/qemu.git/softmmu_template.h: In function ‘helper_le_stw_mmu’:
/home/alex/lsrc/qemu/qemu.git/softmmu_template.h:503:13: error: ‘haddr’ undeclared (first use in this function)

             haddr = addr + env->tlb_table[mmu_idx][index].addend;
             ^
/home/alex/lsrc/qemu/qemu.git/softmmu_template.h: In function ‘helper_be_stw_mmu’:
/home/alex/lsrc/qemu/qemu.git/softmmu_template.h:651:13: error: ‘haddr’ undeclared (first use in this function)
             haddr = addr + env->tlb_table[mmu_idx][index].addend;
             ^
In file included from /home/alex/lsrc/qemu/qemu.git/cputlb.c:533:0:
/home/alex/lsrc/qemu/qemu.git/softmmu_template.h: In function ‘helper_le_stl_mmu’:
/home/alex/lsrc/qemu/qemu.git/softmmu_template.h:503:13: error: ‘haddr’ undeclared (first use in this function)
             haddr = addr + env->tlb_table[mmu_idx][index].addend;
             ^
/home/alex/lsrc/qemu/qemu.git/softmmu_template.h: In function ‘helper_be_stl_mmu’:
/home/alex/lsrc/qemu/qemu.git/softmmu_template.h:651:13: error: ‘haddr’ undeclared (first use in this function)
             haddr = addr + env->tlb_table[mmu_idx][index].addend;
             ^
In file included from /home/alex/lsrc/qemu/qemu.git/cputlb.c:536:0:
/home/alex/lsrc/qemu/qemu.git/softmmu_template.h: In function ‘helper_le_stq_mmu’:
/home/alex/lsrc/qemu/qemu.git/softmmu_template.h:503:13: error: ‘haddr’ undeclared (first use in this function)
             haddr = addr + env->tlb_table[mmu_idx][index].addend;
             ^
/home/alex/lsrc/qemu/qemu.git/softmmu_template.h: In function ‘helper_be_stq_mmu’:
/home/alex/lsrc/qemu/qemu.git/softmmu_template.h:651:13: error: ‘haddr’ undeclared (first use in this function)
             haddr = addr + env->tlb_table[mmu_idx][index].addend;
             ^

make[1]: *** [cputlb.o] Error 1
make[1]: *** Waiting for unfinished jobs....
CC x86_64-softmmu/../hw/block/fdc.o
make: *** [subdir-x86_64-softmmu] Error 2

ERROR: commit 3a371deaf11ce944127a00eadbc7e811b6798de1 failed to build! commit 3a371deaf11ce944127a00eadbc7e811b6798de1

Author: Alvise Rigo <[email protected]>
Date:   Thu Dec 10 17:26:54 2015 +0100

softmmu: Simplify helper_*_st_name, wrap RAM code

Found while checking with Jeff's compile-check script:

https://github.com/codyprime/git-scripts

git compile-check -r c3626ca7df027dabf0568284360a23faf18f0884..HEAD

Quoted 151 lines
---
 softmmu_template.h | 110 +++++++++++++++++++++++++++++++++--------------------
 1 file changed, 68 insertions(+), 42 deletions(-)
softmmu_template.h +66 -36
index 2ebf527..262c95f 100644
@@ -416,13 +416,46 @@ static inline void glue(helper_le_st_name, _do_mmio_access)(CPUArchState *env,
     glue(io_write, SUFFIX)(env, iotlbentry, val, addr, retaddr);
 }
 
+static inline void glue(helper_le_st_name, _do_ram_access)(CPUArchState *env,
+                                                           DATA_TYPE val,
+                                                           target_ulong addr,
+                                                           TCGMemOpIdx oi,
+                                                           unsigned mmu_idx,
+                                                           int index,
+                                                           uintptr_t retaddr)
+{
+    uintptr_t haddr;
+
+    /* Handle slow unaligned access (it spans two pages or IO).  */
+    if (DATA_SIZE > 1
+        && unlikely((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1
+                     >= TARGET_PAGE_SIZE)) {
+        glue(helper_le_st_name, _do_unl_access)(env, val, addr, oi, mmu_idx,
+                                                retaddr);
+        return;
+    }
+
+    /* Handle aligned access or unaligned access in the same page.  */
+    if ((addr & (DATA_SIZE - 1)) != 0
+        && (get_memop(oi) & MO_AMASK) == MO_ALIGN) {
+        cpu_unaligned_access(ENV_GET_CPU(env), addr, MMU_DATA_STORE,
+                             mmu_idx, retaddr);
+    }
+
+    haddr = addr + env->tlb_table[mmu_idx][index].addend;
+#if DATA_SIZE == 1
+    glue(glue(st, SUFFIX), _p)((uint8_t *)haddr, val);
+#else
+    glue(glue(st, SUFFIX), _le_p)((uint8_t *)haddr, val);
+#endif
+}
+
 void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
                        TCGMemOpIdx oi, uintptr_t retaddr)
 {
     unsigned mmu_idx = get_mmuidx(oi);
     int index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
     target_ulong tlb_addr = env->tlb_table[mmu_idx][index].addr_write;
-    uintptr_t haddr;
 
     /* Adjust the given return address.  */
     retaddr -= GETPC_ADJ;
@@ -484,28 +517,8 @@ void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
         }
     }
 
-    /* Handle slow unaligned access (it spans two pages or IO).  */
-    if (DATA_SIZE > 1
-        && unlikely((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1
-                     >= TARGET_PAGE_SIZE)) {
-        glue(helper_le_st_name, _do_unl_access)(env, val, addr, oi, mmu_idx,
-                                                retaddr);
-        return;
-    }
-
-    /* Handle aligned access or unaligned access in the same page.  */
-    if ((addr & (DATA_SIZE - 1)) != 0
-        && (get_memop(oi) & MO_AMASK) == MO_ALIGN) {
-        cpu_unaligned_access(ENV_GET_CPU(env), addr, MMU_DATA_STORE,
-                             mmu_idx, retaddr);
-    }
-
-    haddr = addr + env->tlb_table[mmu_idx][index].addend;
-#if DATA_SIZE == 1
-    glue(glue(st, SUFFIX), _p)((uint8_t *)haddr, val);
-#else
-    glue(glue(st, SUFFIX), _le_p)((uint8_t *)haddr, val);
-#endif
+    glue(helper_le_st_name, _do_ram_access)(env, val, addr, oi, mmu_idx, index,
+                                            retaddr);
 }
 
 #if DATA_SIZE > 1
@@ -555,13 +568,42 @@ static inline void glue(helper_be_st_name, _do_mmio_access)(CPUArchState *env,
     glue(io_write, SUFFIX)(env, iotlbentry, val, addr, retaddr);
 }
 
+static inline void glue(helper_be_st_name, _do_ram_access)(CPUArchState *env,
+                                                           DATA_TYPE val,
+                                                           target_ulong addr,
+                                                           TCGMemOpIdx oi,
+                                                           unsigned mmu_idx,
+                                                           int index,
+                                                           uintptr_t retaddr)
+{
+    uintptr_t haddr;
+
+    /* Handle slow unaligned access (it spans two pages or IO).  */
+    if (DATA_SIZE > 1
+        && unlikely((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1
+                     >= TARGET_PAGE_SIZE)) {
+        glue(helper_be_st_name, _do_unl_access)(env, val, addr, oi, mmu_idx,
+                                                retaddr);
+        return;
+    }
+
+    /* Handle aligned access or unaligned access in the same page.  */
+    if ((addr & (DATA_SIZE - 1)) != 0
+        && (get_memop(oi) & MO_AMASK) == MO_ALIGN) {
+        cpu_unaligned_access(ENV_GET_CPU(env), addr, MMU_DATA_STORE,
+                             mmu_idx, retaddr);
+    }
+
+    haddr = addr + env->tlb_table[mmu_idx][index].addend;
+    glue(glue(st, SUFFIX), _be_p)((uint8_t *)haddr, val);
+}
+
 void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
                        TCGMemOpIdx oi, uintptr_t retaddr)
 {
     unsigned mmu_idx = get_mmuidx(oi);
     int index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
     target_ulong tlb_addr = env->tlb_table[mmu_idx][index].addr_write;
-    uintptr_t haddr;
 
     /* Adjust the given return address.  */
     retaddr -= GETPC_ADJ;
@@ -623,24 +665,8 @@ void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
         }
     }
 
-    /* Handle slow unaligned access (it spans two pages or IO).  */
-    if (DATA_SIZE > 1
-        && unlikely((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1
-                     >= TARGET_PAGE_SIZE)) {
-        glue(helper_be_st_name, _do_unl_access)(env, val, addr, oi, mmu_idx,
-                                                retaddr);
-        return;
-    }
-
-    /* Handle aligned access or unaligned access in the same page.  */
-    if ((addr & (DATA_SIZE - 1)) != 0
-        && (get_memop(oi) & MO_AMASK) == MO_ALIGN) {

softmmu_template.h void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val, · line 671

Quoted 10 lines
softmmu_template.h +2 -6
-        cpu_unaligned_access(ENV_GET_CPU(env), addr, MMU_DATA_STORE,
-                             mmu_idx, retaddr);
-    }
-
-    haddr = addr + env->tlb_table[mmu_idx][index].addend;
-    glue(glue(st, SUFFIX), _be_p)((uint8_t *)haddr, val);
+    glue(helper_be_st_name, _do_ram_access)(env, val, addr, oi, mmu_idx, index,
+                                            retaddr);
 }
 #endif /* DATA_SIZE > 1 */

--
Alex Bennée

#41 lore raw

Re: [Qemu-devel] [RFC v6 12/14] softmmu: Simplify helper_*_st_name, wrap RAM code

Quoted 7 lines · Alex Bennée
Quoted 2 lines · Alvise Rigo

Attempting to simplify the helper_*_st_name, wrap the code relative to a RAM access into an inline function.

This commit breaks a default x86_64-softmmu build:

I see. Would these three commits make more sense if squashed together? Or better to leave them distinct and fix the compilation issue?

BTW, I will now start using that script.

Thank you,
alvise

Quoted 218 lines

CC x86_64-softmmu/../hw/audio/pcspk.o
In file included from /home/alex/lsrc/qemu/qemu.git/cputlb.c:527:0:
/home/alex/lsrc/qemu/qemu.git/softmmu_template.h: In function ‘helper_ret_stb_mmu’:
/home/alex/lsrc/qemu/qemu.git/softmmu_template.h:503:13: error: ‘haddr’ undeclared (first use in this function)

             haddr = addr + env->tlb_table[mmu_idx][index].addend;
             ^

/home/alex/lsrc/qemu/qemu.git/softmmu_template.h:503:13: note: each undeclared identifier is reported only once for each function it appears in In file included from /home/alex/lsrc/qemu/qemu.git/cputlb.c:530:0:
/home/alex/lsrc/qemu/qemu.git/softmmu_template.h: In function ‘helper_le_stw_mmu’:
/home/alex/lsrc/qemu/qemu.git/softmmu_template.h:503:13: error: ‘haddr’ undeclared (first use in this function)

             haddr = addr + env->tlb_table[mmu_idx][index].addend;
             ^
/home/alex/lsrc/qemu/qemu.git/softmmu_template.h: In function ‘helper_be_stw_mmu’:
/home/alex/lsrc/qemu/qemu.git/softmmu_template.h:651:13: error: ‘haddr’ undeclared (first use in this function)
             haddr = addr + env->tlb_table[mmu_idx][index].addend;
             ^
In file included from /home/alex/lsrc/qemu/qemu.git/cputlb.c:533:0:
/home/alex/lsrc/qemu/qemu.git/softmmu_template.h: In function ‘helper_le_stl_mmu’:
/home/alex/lsrc/qemu/qemu.git/softmmu_template.h:503:13: error: ‘haddr’ undeclared (first use in this function)
             haddr = addr + env->tlb_table[mmu_idx][index].addend;
             ^
/home/alex/lsrc/qemu/qemu.git/softmmu_template.h: In function ‘helper_be_stl_mmu’:
/home/alex/lsrc/qemu/qemu.git/softmmu_template.h:651:13: error: ‘haddr’ undeclared (first use in this function)
             haddr = addr + env->tlb_table[mmu_idx][index].addend;
             ^
In file included from /home/alex/lsrc/qemu/qemu.git/cputlb.c:536:0:
/home/alex/lsrc/qemu/qemu.git/softmmu_template.h: In function ‘helper_le_stq_mmu’:
/home/alex/lsrc/qemu/qemu.git/softmmu_template.h:503:13: error: ‘haddr’ undeclared (first use in this function)
             haddr = addr + env->tlb_table[mmu_idx][index].addend;
             ^
/home/alex/lsrc/qemu/qemu.git/softmmu_template.h: In function ‘helper_be_stq_mmu’:
/home/alex/lsrc/qemu/qemu.git/softmmu_template.h:651:13: error: ‘haddr’ undeclared (first use in this function)
             haddr = addr + env->tlb_table[mmu_idx][index].addend;
             ^

make[1]: *** [cputlb.o] Error 1
make[1]: *** Waiting for unfinished jobs....
CC x86_64-softmmu/../hw/block/fdc.o
make: *** [subdir-x86_64-softmmu] Error 2

ERROR: commit 3a371deaf11ce944127a00eadbc7e811b6798de1 failed to build! commit 3a371deaf11ce944127a00eadbc7e811b6798de1

Author: Alvise Rigo <[email protected]>
Date:   Thu Dec 10 17:26:54 2015 +0100

softmmu: Simplify helper_*_st_name, wrap RAM code

Found while checking with Jeff's compile-check script:

https://github.com/codyprime/git-scripts

git compile-check -r c3626ca7df027dabf0568284360a23faf18f0884..HEAD

Quoted 151 lines
---
 softmmu_template.h | 110 +++++++++++++++++++++++++++++++++--------------------
 1 file changed, 68 insertions(+), 42 deletions(-)
softmmu_template.h +66 -36
index 2ebf527..262c95f 100644
@@ -416,13 +416,46 @@ static inline void glue(helper_le_st_name, _do_mmio_access)(CPUArchState *env,
     glue(io_write, SUFFIX)(env, iotlbentry, val, addr, retaddr);
 }
 
+static inline void glue(helper_le_st_name, _do_ram_access)(CPUArchState *env,
+                                                           DATA_TYPE val,
+                                                           target_ulong addr,
+                                                           TCGMemOpIdx oi,
+                                                           unsigned mmu_idx,
+                                                           int index,
+                                                           uintptr_t retaddr)
+{
+    uintptr_t haddr;
+
+    /* Handle slow unaligned access (it spans two pages or IO).  */
+    if (DATA_SIZE > 1
+        && unlikely((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1
+                     >= TARGET_PAGE_SIZE)) {
+        glue(helper_le_st_name, _do_unl_access)(env, val, addr, oi, mmu_idx,
+                                                retaddr);
+        return;
+    }
+
+    /* Handle aligned access or unaligned access in the same page.  */
+    if ((addr & (DATA_SIZE - 1)) != 0
+        && (get_memop(oi) & MO_AMASK) == MO_ALIGN) {
+        cpu_unaligned_access(ENV_GET_CPU(env), addr, MMU_DATA_STORE,
+                             mmu_idx, retaddr);
+    }
+
+    haddr = addr + env->tlb_table[mmu_idx][index].addend;
+#if DATA_SIZE == 1
+    glue(glue(st, SUFFIX), _p)((uint8_t *)haddr, val);
+#else
+    glue(glue(st, SUFFIX), _le_p)((uint8_t *)haddr, val);
+#endif
+}
+
 void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
                        TCGMemOpIdx oi, uintptr_t retaddr)
 {
     unsigned mmu_idx = get_mmuidx(oi);
     int index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
     target_ulong tlb_addr = env->tlb_table[mmu_idx][index].addr_write;
-    uintptr_t haddr;
 
     /* Adjust the given return address.  */
     retaddr -= GETPC_ADJ;
@@ -484,28 +517,8 @@ void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
         }
     }
 
-    /* Handle slow unaligned access (it spans two pages or IO).  */
-    if (DATA_SIZE > 1
-        && unlikely((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1
-                     >= TARGET_PAGE_SIZE)) {
-        glue(helper_le_st_name, _do_unl_access)(env, val, addr, oi, mmu_idx,
-                                                retaddr);
-        return;
-    }
-
-    /* Handle aligned access or unaligned access in the same page.  */
-    if ((addr & (DATA_SIZE - 1)) != 0
-        && (get_memop(oi) & MO_AMASK) == MO_ALIGN) {
-        cpu_unaligned_access(ENV_GET_CPU(env), addr, MMU_DATA_STORE,
-                             mmu_idx, retaddr);
-    }
-
-    haddr = addr + env->tlb_table[mmu_idx][index].addend;
-#if DATA_SIZE == 1
-    glue(glue(st, SUFFIX), _p)((uint8_t *)haddr, val);
-#else
-    glue(glue(st, SUFFIX), _le_p)((uint8_t *)haddr, val);
-#endif
+    glue(helper_le_st_name, _do_ram_access)(env, val, addr, oi, mmu_idx, index,
+                                            retaddr);
 }
 
 #if DATA_SIZE > 1
@@ -555,13 +568,42 @@ static inline void glue(helper_be_st_name, _do_mmio_access)(CPUArchState *env,
     glue(io_write, SUFFIX)(env, iotlbentry, val, addr, retaddr);
 }
 
+static inline void glue(helper_be_st_name, _do_ram_access)(CPUArchState *env,
+                                                           DATA_TYPE val,
+                                                           target_ulong addr,
+                                                           TCGMemOpIdx oi,
+                                                           unsigned mmu_idx,
+                                                           int index,
+                                                           uintptr_t retaddr)
+{
+    uintptr_t haddr;
+
+    /* Handle slow unaligned access (it spans two pages or IO).  */
+    if (DATA_SIZE > 1
+        && unlikely((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1
+                     >= TARGET_PAGE_SIZE)) {
+        glue(helper_be_st_name, _do_unl_access)(env, val, addr, oi, mmu_idx,
+                                                retaddr);
+        return;
+    }
+
+    /* Handle aligned access or unaligned access in the same page.  */
+    if ((addr & (DATA_SIZE - 1)) != 0
+        && (get_memop(oi) & MO_AMASK) == MO_ALIGN) {
+        cpu_unaligned_access(ENV_GET_CPU(env), addr, MMU_DATA_STORE,
+                             mmu_idx, retaddr);
+    }
+
+    haddr = addr + env->tlb_table[mmu_idx][index].addend;
+    glue(glue(st, SUFFIX), _be_p)((uint8_t *)haddr, val);
+}
+
 void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
                        TCGMemOpIdx oi, uintptr_t retaddr)
 {
     unsigned mmu_idx = get_mmuidx(oi);
     int index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
     target_ulong tlb_addr = env->tlb_table[mmu_idx][index].addr_write;
-    uintptr_t haddr;
 
     /* Adjust the given return address.  */
     retaddr -= GETPC_ADJ;
@@ -623,24 +665,8 @@ void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
         }
     }
 
-    /* Handle slow unaligned access (it spans two pages or IO).  */
-    if (DATA_SIZE > 1
-        && unlikely((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1
-                     >= TARGET_PAGE_SIZE)) {
-        glue(helper_be_st_name, _do_unl_access)(env, val, addr, oi, mmu_idx,
-                                                retaddr);
-        return;
-    }
-
-    /* Handle aligned access or unaligned access in the same page.  */
-    if ((addr & (DATA_SIZE - 1)) != 0
-        && (get_memop(oi) & MO_AMASK) == MO_ALIGN) {

softmmu_template.h void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val, · line 671

Quoted 10 lines
softmmu_template.h +2 -6
-        cpu_unaligned_access(ENV_GET_CPU(env), addr, MMU_DATA_STORE,
-                             mmu_idx, retaddr);
-    }
-
-    haddr = addr + env->tlb_table[mmu_idx][index].addend;
-    glue(glue(st, SUFFIX), _be_p)((uint8_t *)haddr, val);
+    glue(helper_be_st_name, _do_ram_access)(env, val, addr, oi, mmu_idx, index,
+                                            retaddr);
 }
 #endif /* DATA_SIZE > 1 */

--
Alex Bennée

#42 lore raw

Re: [Qemu-devel] [RFC v6 12/14] softmmu: Simplify helper_*_st_name, wrap RAM code

Quoted 11 lines · alvise rigo
Quoted 7 lines · Alex Bennée
Quoted 2 lines · Alvise Rigo

Attempting to simplify the helper_*_st_name, wrap the code relative to a RAM access into an inline function.

This commit breaks a default x86_64-softmmu build:

I see. Would these three commits make more sense if squashed together? Or better to leave them distinct and fix the compilation issue?

Keep them distinct if you can as long as the compilation is ok for bisectablity.

Quoted 224 lines

BTW, I will now start using that script.

Thank you,
alvise

Quoted 218 lines

CC x86_64-softmmu/../hw/audio/pcspk.o
In file included from /home/alex/lsrc/qemu/qemu.git/cputlb.c:527:0:
/home/alex/lsrc/qemu/qemu.git/softmmu_template.h: In function ‘helper_ret_stb_mmu’:
/home/alex/lsrc/qemu/qemu.git/softmmu_template.h:503:13: error: ‘haddr’ undeclared (first use in this function)

             haddr = addr + env->tlb_table[mmu_idx][index].addend;
             ^

/home/alex/lsrc/qemu/qemu.git/softmmu_template.h:503:13: note: each undeclared identifier is reported only once for each function it appears in In file included from /home/alex/lsrc/qemu/qemu.git/cputlb.c:530:0:
/home/alex/lsrc/qemu/qemu.git/softmmu_template.h: In function ‘helper_le_stw_mmu’:
/home/alex/lsrc/qemu/qemu.git/softmmu_template.h:503:13: error: ‘haddr’ undeclared (first use in this function)

             haddr = addr + env->tlb_table[mmu_idx][index].addend;
             ^
/home/alex/lsrc/qemu/qemu.git/softmmu_template.h: In function ‘helper_be_stw_mmu’:
/home/alex/lsrc/qemu/qemu.git/softmmu_template.h:651:13: error: ‘haddr’ undeclared (first use in this function)
             haddr = addr + env->tlb_table[mmu_idx][index].addend;
             ^
In file included from /home/alex/lsrc/qemu/qemu.git/cputlb.c:533:0:
/home/alex/lsrc/qemu/qemu.git/softmmu_template.h: In function ‘helper_le_stl_mmu’:
/home/alex/lsrc/qemu/qemu.git/softmmu_template.h:503:13: error: ‘haddr’ undeclared (first use in this function)
             haddr = addr + env->tlb_table[mmu_idx][index].addend;
             ^
/home/alex/lsrc/qemu/qemu.git/softmmu_template.h: In function ‘helper_be_stl_mmu’:
/home/alex/lsrc/qemu/qemu.git/softmmu_template.h:651:13: error: ‘haddr’ undeclared (first use in this function)
             haddr = addr + env->tlb_table[mmu_idx][index].addend;
             ^
In file included from /home/alex/lsrc/qemu/qemu.git/cputlb.c:536:0:
/home/alex/lsrc/qemu/qemu.git/softmmu_template.h: In function ‘helper_le_stq_mmu’:
/home/alex/lsrc/qemu/qemu.git/softmmu_template.h:503:13: error: ‘haddr’ undeclared (first use in this function)
             haddr = addr + env->tlb_table[mmu_idx][index].addend;
             ^
/home/alex/lsrc/qemu/qemu.git/softmmu_template.h: In function ‘helper_be_stq_mmu’:
/home/alex/lsrc/qemu/qemu.git/softmmu_template.h:651:13: error: ‘haddr’ undeclared (first use in this function)
             haddr = addr + env->tlb_table[mmu_idx][index].addend;
             ^

make[1]: *** [cputlb.o] Error 1
make[1]: *** Waiting for unfinished jobs....
CC x86_64-softmmu/../hw/block/fdc.o
make: *** [subdir-x86_64-softmmu] Error 2

ERROR: commit 3a371deaf11ce944127a00eadbc7e811b6798de1 failed to build! commit 3a371deaf11ce944127a00eadbc7e811b6798de1

Author: Alvise Rigo <[email protected]>
Date:   Thu Dec 10 17:26:54 2015 +0100

softmmu: Simplify helper_*_st_name, wrap RAM code

Found while checking with Jeff's compile-check script:

https://github.com/codyprime/git-scripts

git compile-check -r c3626ca7df027dabf0568284360a23faf18f0884..HEAD

Quoted 151 lines
---
 softmmu_template.h | 110 +++++++++++++++++++++++++++++++++--------------------
 1 file changed, 68 insertions(+), 42 deletions(-)
softmmu_template.h +66 -36
index 2ebf527..262c95f 100644
@@ -416,13 +416,46 @@ static inline void glue(helper_le_st_name, _do_mmio_access)(CPUArchState *env,
     glue(io_write, SUFFIX)(env, iotlbentry, val, addr, retaddr);
 }
 
+static inline void glue(helper_le_st_name, _do_ram_access)(CPUArchState *env,
+                                                           DATA_TYPE val,
+                                                           target_ulong addr,
+                                                           TCGMemOpIdx oi,
+                                                           unsigned mmu_idx,
+                                                           int index,
+                                                           uintptr_t retaddr)
+{
+    uintptr_t haddr;
+
+    /* Handle slow unaligned access (it spans two pages or IO).  */
+    if (DATA_SIZE > 1
+        && unlikely((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1
+                     >= TARGET_PAGE_SIZE)) {
+        glue(helper_le_st_name, _do_unl_access)(env, val, addr, oi, mmu_idx,
+                                                retaddr);
+        return;
+    }
+
+    /* Handle aligned access or unaligned access in the same page.  */
+    if ((addr & (DATA_SIZE - 1)) != 0
+        && (get_memop(oi) & MO_AMASK) == MO_ALIGN) {
+        cpu_unaligned_access(ENV_GET_CPU(env), addr, MMU_DATA_STORE,
+                             mmu_idx, retaddr);
+    }
+
+    haddr = addr + env->tlb_table[mmu_idx][index].addend;
+#if DATA_SIZE == 1
+    glue(glue(st, SUFFIX), _p)((uint8_t *)haddr, val);
+#else
+    glue(glue(st, SUFFIX), _le_p)((uint8_t *)haddr, val);
+#endif
+}
+
 void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
                        TCGMemOpIdx oi, uintptr_t retaddr)
 {
     unsigned mmu_idx = get_mmuidx(oi);
     int index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
     target_ulong tlb_addr = env->tlb_table[mmu_idx][index].addr_write;
-    uintptr_t haddr;
 
     /* Adjust the given return address.  */
     retaddr -= GETPC_ADJ;
@@ -484,28 +517,8 @@ void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
         }
     }
 
-    /* Handle slow unaligned access (it spans two pages or IO).  */
-    if (DATA_SIZE > 1
-        && unlikely((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1
-                     >= TARGET_PAGE_SIZE)) {
-        glue(helper_le_st_name, _do_unl_access)(env, val, addr, oi, mmu_idx,
-                                                retaddr);
-        return;
-    }
-
-    /* Handle aligned access or unaligned access in the same page.  */
-    if ((addr & (DATA_SIZE - 1)) != 0
-        && (get_memop(oi) & MO_AMASK) == MO_ALIGN) {
-        cpu_unaligned_access(ENV_GET_CPU(env), addr, MMU_DATA_STORE,
-                             mmu_idx, retaddr);
-    }
-
-    haddr = addr + env->tlb_table[mmu_idx][index].addend;
-#if DATA_SIZE == 1
-    glue(glue(st, SUFFIX), _p)((uint8_t *)haddr, val);
-#else
-    glue(glue(st, SUFFIX), _le_p)((uint8_t *)haddr, val);
-#endif
+    glue(helper_le_st_name, _do_ram_access)(env, val, addr, oi, mmu_idx, index,
+                                            retaddr);
 }
 
 #if DATA_SIZE > 1
@@ -555,13 +568,42 @@ static inline void glue(helper_be_st_name, _do_mmio_access)(CPUArchState *env,
     glue(io_write, SUFFIX)(env, iotlbentry, val, addr, retaddr);
 }
 
+static inline void glue(helper_be_st_name, _do_ram_access)(CPUArchState *env,
+                                                           DATA_TYPE val,
+                                                           target_ulong addr,
+                                                           TCGMemOpIdx oi,
+                                                           unsigned mmu_idx,
+                                                           int index,
+                                                           uintptr_t retaddr)
+{
+    uintptr_t haddr;
+
+    /* Handle slow unaligned access (it spans two pages or IO).  */
+    if (DATA_SIZE > 1
+        && unlikely((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1
+                     >= TARGET_PAGE_SIZE)) {
+        glue(helper_be_st_name, _do_unl_access)(env, val, addr, oi, mmu_idx,
+                                                retaddr);
+        return;
+    }
+
+    /* Handle aligned access or unaligned access in the same page.  */
+    if ((addr & (DATA_SIZE - 1)) != 0
+        && (get_memop(oi) & MO_AMASK) == MO_ALIGN) {
+        cpu_unaligned_access(ENV_GET_CPU(env), addr, MMU_DATA_STORE,
+                             mmu_idx, retaddr);
+    }
+
+    haddr = addr + env->tlb_table[mmu_idx][index].addend;
+    glue(glue(st, SUFFIX), _be_p)((uint8_t *)haddr, val);
+}
+
 void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
                        TCGMemOpIdx oi, uintptr_t retaddr)
 {
     unsigned mmu_idx = get_mmuidx(oi);
     int index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
     target_ulong tlb_addr = env->tlb_table[mmu_idx][index].addr_write;
-    uintptr_t haddr;
 
     /* Adjust the given return address.  */
     retaddr -= GETPC_ADJ;
@@ -623,24 +665,8 @@ void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
         }
     }
 
-    /* Handle slow unaligned access (it spans two pages or IO).  */
-    if (DATA_SIZE > 1
-        && unlikely((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1
-                     >= TARGET_PAGE_SIZE)) {
-        glue(helper_be_st_name, _do_unl_access)(env, val, addr, oi, mmu_idx,
-                                                retaddr);
-        return;
-    }
-
-    /* Handle aligned access or unaligned access in the same page.  */
-    if ((addr & (DATA_SIZE - 1)) != 0
-        && (get_memop(oi) & MO_AMASK) == MO_ALIGN) {

softmmu_template.h void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val, · line 671

Quoted 10 lines
softmmu_template.h +2 -6
-        cpu_unaligned_access(ENV_GET_CPU(env), addr, MMU_DATA_STORE,
-                             mmu_idx, retaddr);
-    }
-
-    haddr = addr + env->tlb_table[mmu_idx][index].addend;
-    glue(glue(st, SUFFIX), _be_p)((uint8_t *)haddr, val);
+    glue(helper_be_st_name, _do_ram_access)(env, val, addr, oi, mmu_idx, index,
+                                            retaddr);
 }
 #endif /* DATA_SIZE > 1 */

--
Alex Bennée

--
Alex Bennée

#43 lore raw

RFC v6 13/14 softmmu: Include MMIO/invalid exclusive accesses

In reply to Alvise Rigo

Enable exclusive accesses when the MMIO/invalid flag is set in the TLB entry.
In case a LL access is done to MMIO memory, we treat it differently from a RAM access in that we do not rely on the EXCL bitmap to flag the page as exclusive. In fact, we don't even need the TLB_EXCL flag to force the slow path, since it is always forced anyway.

This commit does not take care of invalidating an MMIO exclusive range from other non-exclusive accesses i.e. CPU1 LoadLink to MMIO address X and CPU2 writes to X. This will be addressed in the following commit.

---
 cputlb.c                | 20 +++++++++++---------
 softmmu_llsc_template.h | 25 ++++++++++++++-----------
 softmmu_template.h      | 38 ++++++++++++++++++++------------------
 3 files changed, 45 insertions(+), 38 deletions(-)
cputlb.c +11 -9
index 372877e..7c2669c 100644
@@ -413,22 +413,24 @@ void tlb_set_page_with_attrs(CPUState *cpu, target_ulong vaddr,
         if ((memory_region_is_ram(section->mr) && section->readonly)
             || memory_region_is_romd(section->mr)) {
             /* Write access calls the I/O callback.  */
-            te->addr_write = address | TLB_MMIO;
+            address |= TLB_MMIO;
         } else if (memory_region_is_ram(section->mr)
                    && cpu_physical_memory_is_clean(section->mr->ram_addr
                                                    + xlat)) {
-            te->addr_write = address | TLB_NOTDIRTY;
-        } else {
-            if (!(address & TLB_MMIO) &&
-                cpu_physical_memory_atleast_one_excl(section->mr->ram_addr
-                                                           + xlat)) {
+            address |= TLB_NOTDIRTY;
+        }
+
+        /* Since the MMIO accesses follow always the slow path, we do not need
+         * to set any flag to trap the access */
+        if (!(address & TLB_MMIO)) {
+            if (cpu_physical_memory_atleast_one_excl(
+                                        section->mr->ram_addr + xlat)) {
                 /* There is at least one vCPU that has flagged the address as
                  * exclusive. */
-                te->addr_write = address | TLB_EXCL;
-            } else {
-                te->addr_write = address;
+                address |= TLB_EXCL;
             }
         }
+        te->addr_write = address;
     } else {
         te->addr_write = -1;
     }
softmmu_llsc_template.h +14 -11
index becb90b..bbc820e 100644
@@ -71,17 +71,20 @@ WORD_TYPE helper_ldlink_name(CPUArchState *env, target_ulong addr,
      * plus the offset (i.e. addr & ~TARGET_PAGE_MASK) */
     hw_addr = (env->iotlb[mmu_idx][index].addr & TARGET_PAGE_MASK) + addr;
 
-    cpu_physical_memory_set_excl(hw_addr, this->cpu_index);
-    excl_history_put_addr(this, hw_addr);
-    /* If all the vCPUs have the EXCL bit set for this page there is no need
-     * to request any flush. */
-    if (cpu_physical_memory_not_excl(hw_addr, smp_cpus)) {
-        CPU_FOREACH(cpu) {
-            if (current_cpu != cpu) {
-                if (cpu_physical_memory_not_excl(hw_addr, cpu->cpu_index)) {
-                    cpu_physical_memory_set_excl(hw_addr, cpu->cpu_index);
-                    tlb_flush(cpu, 1);
-                    excl_history_put_addr(cpu, hw_addr);
+    /* No need to flush for MMIO addresses, the slow path is always used */
+    if (likely(!(env->tlb_table[mmu_idx][index].addr_read & TLB_MMIO))) {
+        cpu_physical_memory_set_excl(hw_addr, this->cpu_index);
+        excl_history_put_addr(this, hw_addr);
+        /* If all the vCPUs have the EXCL bit set for this page there is no need
+         * to request any flush. */
+        if (cpu_physical_memory_not_excl(hw_addr, smp_cpus)) {
+            CPU_FOREACH(cpu) {
+                if (current_cpu != cpu) {
+                    if (cpu_physical_memory_not_excl(hw_addr, cpu->cpu_index)) {
+                        cpu_physical_memory_set_excl(hw_addr, cpu->cpu_index);
+                        tlb_flush(cpu, 1);
+                        excl_history_put_addr(cpu, hw_addr);
+                    }
                 }
             }
         }
softmmu_template.h +20 -18
index 262c95f..196beec 100644
@@ -476,9 +476,8 @@ void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
 
     /* Handle an IO access or exclusive access.  */
     if (unlikely(tlb_addr & ~TARGET_PAGE_MASK)) {
-        CPUIOTLBEntry *iotlbentry = &env->iotlb[mmu_idx][index];
-
-        if ((tlb_addr & ~TARGET_PAGE_MASK) == TLB_EXCL) {
+        if (tlb_addr & TLB_EXCL) {
+            CPUIOTLBEntry *iotlbentry = &env->iotlb[mmu_idx][index];
             CPUState *cpu = ENV_GET_CPU(env);
             /* The slow-path has been forced since we are writing to
              * exclusive-protected memory. */
@@ -500,12 +499,14 @@ void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
                 cpu_physical_memory_unset_excl(hw_addr, cpu->cpu_index);
             }
 
-            haddr = addr + env->tlb_table[mmu_idx][index].addend;
-        #if DATA_SIZE == 1
-            glue(glue(st, SUFFIX), _p)((uint8_t *)haddr, val);
-        #else
-            glue(glue(st, SUFFIX), _le_p)((uint8_t *)haddr, val);
-        #endif
+            if (tlb_addr & ~(TARGET_PAGE_MASK | TLB_EXCL)) { /* MMIO access */
+                glue(helper_le_st_name, _do_mmio_access)(env, val, addr, oi,
+                                                         mmu_idx, index,
+                                                         retaddr);
+            } else {
+                glue(helper_le_st_name, _do_ram_access)(env, val, addr, oi,
+                                                        mmu_idx, index,retaddr);
+            }
 
             lookup_and_reset_cpus_ll_addr(hw_addr, DATA_SIZE);
 
@@ -624,9 +625,8 @@ void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
 
     /* Handle an IO access or exclusive access.  */
     if (unlikely(tlb_addr & ~TARGET_PAGE_MASK)) {
-        CPUIOTLBEntry *iotlbentry = &env->iotlb[mmu_idx][index];
-
-        if ((tlb_addr & ~TARGET_PAGE_MASK) == TLB_EXCL) {
+        if (tlb_addr & TLB_EXCL) {
+            CPUIOTLBEntry *iotlbentry = &env->iotlb[mmu_idx][index];
             CPUState *cpu = ENV_GET_CPU(env);
             /* The slow-path has been forced since we are writing to
              * exclusive-protected memory. */
@@ -648,12 +648,14 @@ void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
                 cpu_physical_memory_unset_excl(hw_addr, cpu->cpu_index);
             }
 
-            haddr = addr + env->tlb_table[mmu_idx][index].addend;
-        #if DATA_SIZE == 1
-            glue(glue(st, SUFFIX), _p)((uint8_t *)haddr, val);
-        #else
-            glue(glue(st, SUFFIX), _le_p)((uint8_t *)haddr, val);
-        #endif
+            if (tlb_addr & ~(TARGET_PAGE_MASK | TLB_EXCL)) { /* MMIO access */
+                glue(helper_be_st_name, _do_mmio_access)(env, val, addr, oi,
+                                                         mmu_idx, index,
+                                                         retaddr);
+            } else {
+                glue(helper_be_st_name, _do_ram_access)(env, val, addr, oi,
+                                                        mmu_idx, index,retaddr);
+            }
 
             lookup_and_reset_cpus_ll_addr(hw_addr, DATA_SIZE);
 
Signature · 1 line
2.6.4
#44 lore raw

RFC v6 14/14 softmmu: Protect MMIO exclusive range

In reply to Alvise Rigo

As for the RAM case, also the MMIO exclusive ranges have to be protected by other CPU's accesses. In order to do that, we flag the accessed MemoryRegion to mark that an exclusive access has been performed and is not concluded yet.
This flag will force the other CPUs to invalidate the exclusive range in case of collision.

---
 cputlb.c                | 20 +++++++++++++-------
 include/exec/memory.h   |  1 +
 softmmu_llsc_template.h | 11 ++++++++---
 softmmu_template.h      | 22 ++++++++++++++++++++++
 4 files changed, 44 insertions(+), 10 deletions(-)
cputlb.c +13 -7
index 7c2669c..7348c5f 100644
@@ -484,19 +484,25 @@ tb_page_addr_t get_page_addr_code(CPUArchState *env1, target_ulong addr)
 /* For every vCPU compare the exclusive address and reset it in case of a
  * match. Since only one vCPU is running at once, no lock has to be held to
  * guard this operation. */
-static inline void lookup_and_reset_cpus_ll_addr(hwaddr addr, hwaddr size)
+static inline bool lookup_and_reset_cpus_ll_addr(hwaddr addr, hwaddr size)
 {
     CPUState *cpu;
+    bool ret = false;
 
     CPU_FOREACH(cpu) {
-        if (cpu->excl_protected_range.begin != EXCLUSIVE_RESET_ADDR &&
-            ranges_overlap(cpu->excl_protected_range.begin,
-                           cpu->excl_protected_range.end -
-                           cpu->excl_protected_range.begin,
-                           addr, size)) {
-            cpu->excl_protected_range.begin = EXCLUSIVE_RESET_ADDR;
+        if (current_cpu != cpu) {
+            if (cpu->excl_protected_range.begin != EXCLUSIVE_RESET_ADDR &&
+                ranges_overlap(cpu->excl_protected_range.begin,
+                               cpu->excl_protected_range.end -
+                               cpu->excl_protected_range.begin,
+                               addr, size)) {
+                cpu->excl_protected_range.begin = EXCLUSIVE_RESET_ADDR;
+                ret = true;
+            }
         }
     }
+
+    return ret;
 }
 
 static inline void excl_history_put_addr(CPUState *cpu, hwaddr addr)
include/exec/memory.h +1 -0
index 2782c77..80961c2 100644
@@ -181,6 +181,7 @@ struct MemoryRegion {
     bool warning_printed; /* For reservations */
     bool flush_coalesced_mmio;
     bool global_locking;
+    bool pending_excl_access; /* A vCPU issued an exclusive access */
     uint8_t vga_logging_count;
     MemoryRegion *alias;
     hwaddr alias_offset;
softmmu_llsc_template.h +8 -3
index bbc820e..07e18ce 100644
@@ -88,13 +88,17 @@ WORD_TYPE helper_ldlink_name(CPUArchState *env, target_ulong addr,
                 }
             }
         }
+        /* For this vCPU, just update the TLB entry, no need to flush. */
+        env->tlb_table[mmu_idx][index].addr_write |= TLB_EXCL;
+    } else {
+        /* Set a pending exclusive access in the MemoryRegion */
+        MemoryRegion *mr = iotlb_to_region(this,
+                                           env->iotlb[mmu_idx][index].addr);
+        mr->pending_excl_access = true;
     }
 
     cc->cpu_set_excl_protected_range(this, hw_addr, DATA_SIZE);
 
-    /* For this vCPU, just update the TLB entry, no need to flush. */
-    env->tlb_table[mmu_idx][index].addr_write |= TLB_EXCL;
-
     /* From now on we are in LL/SC context */
     this->ll_sc_context = 1;
 
@@ -128,6 +132,7 @@ WORD_TYPE helper_stcond_name(CPUArchState *env, target_ulong addr,
 
         /* Unset LL/SC context */
         cpu->ll_sc_context = 0;
+        cpu->excl_protected_range.begin = EXCLUSIVE_RESET_ADDR;
     }
 
     return ret;
softmmu_template.h +22 -0
index 196beec..65cce0a 100644
@@ -360,6 +360,14 @@ static inline void glue(io_write, SUFFIX)(CPUArchState *env,
     MemoryRegion *mr = iotlb_to_region(cpu, physaddr);
 
     physaddr = (physaddr & TARGET_PAGE_MASK) + addr;
+
+    /* Invalidate the exclusive range that overlaps this access */
+    if (mr->pending_excl_access) {
+        if (lookup_and_reset_cpus_ll_addr(physaddr, 1 << SHIFT)) {
+            mr->pending_excl_access = false;
+        }
+    }
+
     if (mr != &io_mem_rom && mr != &io_mem_notdirty && !cpu->can_do_io) {
         cpu_io_recompile(cpu, retaddr);
     }
@@ -503,6 +511,13 @@ void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
                 glue(helper_le_st_name, _do_mmio_access)(env, val, addr, oi,
                                                          mmu_idx, index,
                                                          retaddr);
+                /* N.B.: Here excl_succeeded == 1 means that this access comes
+                 * from an exclusive instruction. */
+                if (cpu->excl_succeeded) {
+                    MemoryRegion *mr = iotlb_to_region(cpu,
+                                            env->iotlb[mmu_idx][index].addr);
+                    mr->pending_excl_access = false;
+                }
             } else {
                 glue(helper_le_st_name, _do_ram_access)(env, val, addr, oi,
                                                         mmu_idx, index,retaddr);
@@ -652,6 +667,13 @@ void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
                 glue(helper_be_st_name, _do_mmio_access)(env, val, addr, oi,
                                                          mmu_idx, index,
                                                          retaddr);
+                /* N.B.: Here excl_succeeded == 1 means that this access comes
+                 * from an exclusive instruction. */
+                if (cpu->excl_succeeded) {
+                    MemoryRegion *mr = iotlb_to_region(cpu,
+                                            env->iotlb[mmu_idx][index].addr);
+                    mr->pending_excl_access = false;
+                }
             } else {
                 glue(helper_be_st_name, _do_ram_access)(env, val, addr, oi,
                                                         mmu_idx, index,retaddr);
Signature · 1 line
2.6.4
#45 lore raw
In reply to Alvise Rigo
Quoted 2 lines · Alvise Rigo

In theory, the provided implementation of TCG LoadLink/StoreConditional can be used to properly handle atomic instructions on any architecture.

No, _in theory_ this implementation is wrong. If a normal store can make a concurrent LL-SC pair fail, it's provably _impossible_ to handle LL/SC with a wait-free fast path for normal stores.

If we decide that it's "good enough", because the race is incredibly rare and doesn't happen anyway for spinlocks, then fine. But it should be represented correctly in the commit messages.

Paolo

#46 lore raw

Hi Paolo,

Thank you for your feedback.

Quoted 7 lines · Paolo Bonzini
Quoted 2 lines · Alvise Rigo

In theory, the provided implementation of TCG LoadLink/StoreConditional can be used to properly handle atomic instructions on any architecture.

Quoted 7 lines

No, _in theory_ this implementation is wrong. If a normal store can make a concurrent LL-SC pair fail, it's provably _impossible_ to handle LL/SC with a wait-free fast path for normal stores.

If we decide that it's "good enough", because the race is incredibly rare and doesn't happen anyway for spinlocks, then fine. But it should be represented correctly in the commit messages.

I did not yet commented extensively this issue since this is still the "single-threaded" version of the patch series.
As soon as the next version of mttcg will be released, I will rebase this series on top of the multi-threaded code.

In any case, what I proposed in the mttcg based v5 was:
- A LL ensures that the TLB_EXCL flag is set on all the CPU's TLB. This is done by querying a TLB flush to all (not exactly all...) the CPUs. To be 100% safe, probably we should also wait that the flush is actually performed
- A TLB_EXCL flag set always forces the slow-path, allowing the CPUs to check for possible collision with a "exclusive memory region"

Now, why the fact of querying the flush (and possibly ensuring that the flush has been actually done) should not be enough?

Thank you,
alvise

Quoted 3 lines

Paolo

#47 lore raw
Quoted 10 lines · alvise rigo

In any case, what I proposed in the mttcg based v5 was:
- A LL ensures that the TLB_EXCL flag is set on all the CPU's TLB. This is done by querying a TLB flush to all (not exactly all...) the CPUs. To be 100% safe, probably we should also wait that the flush is actually performed
- A TLB_EXCL flag set always forces the slow-path, allowing the CPUs to check for possible collision with a "exclusive memory region"

Now, why the fact of querying the flush (and possibly ensuring that the flush has been actually done) should not be enough?

There will always be a race where the normal store fails. While I haven't studied your code enough to do a constructive proof, it's enough to prove the impossibility of what you're trying to do. Mind, I also believed for a long time that it was possible to do it!

If we have two CPUs, with CPU 0 executing LL and the CPU 1 executing a store, you can model this as a consensus problem. For example, CPU 0 could propose that the subsequent SC succeeds, while CPU 1 proposes that it fails. The outcome of the SC instruction depends on who wins.

Therefore, implementing LL/SC problem requires---on both CPU 0 and CPU 1, and hence for both LL/SC and normal store---an atomic primitive with consensus number >= 2. Other than LL/SC itself, the commonly-available operations satisfying this requirement are test-and-set (consensus number 2) and compare-and-swap (infinite consensus number). Normal memory reads and writes (called "atomic registers" in multi-processing research lingo) have consensus number 1; it's not enough.

If the host had LL/SC, CPU 1 could in principle delegate its side of the consensus problem to the processor; but even that is not a solution because processors constrain the instructions that can appear between the load and the store, and this could cause an infinite sequence of spurious failed SCs. Another option is transactional memory, but it's also too slow for normal stores.

The simplest solution is not to implement full LL/SC semantics; instead, similar to linux-user, a SC operation can perform a cmpxchg from the value fetched by LL to the argument of SC. This bypasses the issue because stores do not have to be instrumented at all, but it does mean that the emulation suffers from the ABA problem.

TLB_EXCL is also a middle-ground, a little bit stronger than cmpxchg. It's more complex and more accurate, but also not perfect. Which is okay, but has to be documented.

Paolo

#48 lore raw

Hi Paolo,

Quoted 19 lines · Paolo Bonzini
Quoted 10 lines · alvise rigo

In any case, what I proposed in the mttcg based v5 was:
- A LL ensures that the TLB_EXCL flag is set on all the CPU's TLB. This is done by querying a TLB flush to all (not exactly all...) the CPUs. To be 100% safe, probably we should also wait that the flush is actually performed
- A TLB_EXCL flag set always forces the slow-path, allowing the CPUs to check for possible collision with a "exclusive memory region"

Now, why the fact of querying the flush (and possibly ensuring that the flush has been actually done) should not be enough?

There will always be a race where the normal store fails. While I haven't studied your code enough to do a constructive proof, it's enough to prove the impossibility of what you're trying to do. Mind, I also believed for a long time that it was possible to do it!

Quoted 4 lines

If we have two CPUs, with CPU 0 executing LL and the CPU 1 executing a store, you can model this as a consensus problem. For example, CPU 0 could propose that the subsequent SC succeeds, while CPU 1 proposes that it fails. The outcome of the SC instruction depends on who wins.

I see your point. This, as you wrote, holds only when we attempt to make the fast path wait-free.
However, the implementation I proposed is not wait-free and somehow serializes the accesses made to the shared resources (that will determine if the access was successful or not) by means of a mutex. The assumption I made - and somehow verified - is that the "colliding fast accesses" are rare. I guess you also agree on this, otherwise how could a wait-free implementation possibly work without being coupled with primitives with appropriate consensus number?

Thank you,
alvise

Quoted 27 lines

Therefore, implementing LL/SC problem requires---on both CPU 0 and CPU 1, and hence for both LL/SC and normal store---an atomic primitive with consensus number >= 2. Other than LL/SC itself, the commonly-available operations satisfying this requirement are test-and-set (consensus number 2) and compare-and-swap (infinite consensus number). Normal memory reads and writes (called "atomic registers" in multi-processing research lingo) have consensus number 1; it's not enough.

If the host had LL/SC, CPU 1 could in principle delegate its side of the consensus problem to the processor; but even that is not a solution because processors constrain the instructions that can appear between the load and the store, and this could cause an infinite sequence of spurious failed SCs. Another option is transactional memory, but it's also too slow for normal stores.

The simplest solution is not to implement full LL/SC semantics; instead, similar to linux-user, a SC operation can perform a cmpxchg from the value fetched by LL to the argument of SC. This bypasses the issue because stores do not have to be instrumented at all, but it does mean that the emulation suffers from the ABA problem.

TLB_EXCL is also a middle-ground, a little bit stronger than cmpxchg. It's more complex and more accurate, but also not perfect. Which is okay, but has to be documented.

Paolo

#49 lore raw
Quoted 11 lines · alvise rigo
Quoted 4 lines
Quoted 4 lines

If we have two CPUs, with CPU 0 executing LL and the CPU 1 executing a store, you can model this as a consensus problem. For example, CPU 0 could propose that the subsequent SC succeeds, while CPU 1 proposes that it fails. The outcome of the SC instruction depends on who wins.

I see your point. This, as you wrote, holds only when we attempt to make the fast path wait-free.
However, the implementation I proposed is not wait-free and somehow serializes the accesses made to the shared resources (that will determine if the access was successful or not) by means of a mutex. The assumption I made - and somehow verified - is that the "colliding fast accesses" are rare.

Isn't the fast path (where TLB_EXCL is not set) wait-free?

This is enough to mess up the theory, though in practice it works.

Quoted 3 lines

I guess you also agree on this, otherwise how
could a wait-free implementation possibly work without being coupled with primitives with appropriate consensus number?

It couldn't. :)

Paolo

#50 lore raw
Quoted 15 lines · Paolo Bonzini
Quoted 11 lines · alvise rigo
Quoted 4 lines
Quoted 4 lines

If we have two CPUs, with CPU 0 executing LL and the CPU 1 executing a store, you can model this as a consensus problem. For example, CPU 0 could propose that the subsequent SC succeeds, while CPU 1 proposes that it fails. The outcome of the SC instruction depends on who wins.

I see your point. This, as you wrote, holds only when we attempt to make the fast path wait-free.
However, the implementation I proposed is not wait-free and somehow serializes the accesses made to the shared resources (that will determine if the access was successful or not) by means of a mutex. The assumption I made - and somehow verified - is that the "colliding fast accesses" are rare.

Quoted 1 line

Isn't the fast path (where TLB_EXCL is not set) wait-free?

There is no such a fast path if we force every CPU to exit the TB and flush the TLB.
I though that with "fast path" you were referring to a slow path forced through TLB_EXCL, sorry.

alvise

Quoted 10 lines

This is enough to mess up the theory, though in practice it works.

Quoted 3 lines

I guess you also agree on this, otherwise how
could a wait-free implementation possibly work without being coupled with primitives with appropriate consensus number?

It couldn't. :)

Paolo

#51 lore raw
In reply to Alvise Rigo

Alvise,

Quoted 9 lines · Alvise Rigo

This is the sixth iteration of the patch series which applies to the upstream branch of QEMU (v2.5.0-rc3).

Changes versus previous versions are at the bottom of this cover letter.

The code is also available at following repository:
https://git.virtualopensystems.com/dev/qemu-mt.git
branch:
slowpath-for-atomic-v6-no-mttcg

Thank you very much for this work. I tried to rebase myself, but it was over my head.

I'm looking for a qemu solution where I can use my cores.

My use case is doing gcc porting for aarch64-*-freebsd*. I think it doesn't matter which OS. This arch has not enough real affordable HW solutions on the market yet. So I was looking for your solution. Claudio gave me a hint about it.

Your recent merge/rebase only covers arm itself, not aarch64, right?

Linking fails with unreferenced cpu_exclusive_addr stuff in target-arm/translate-a64.c

Are you working on this already? Or Claudio?

Quoted 1 line

This work has been sponsored by Huawei Technologies Duesseldorf GmbH.

...

Thank you!
Andreas

#52 lore raw

Hi Andreas,

Quoted 18 lines · Andreas Tobler

Alvise,

Quoted 10 lines · Alvise Rigo

This is the sixth iteration of the patch series which applies to the upstream branch of QEMU (v2.5.0-rc3).

Changes versus previous versions are at the bottom of this cover letter.

The code is also available at following repository:
https://git.virtualopensystems.com/dev/qemu-mt.git
branch:
slowpath-for-atomic-v6-no-mttcg

Thank you very much for this work. I tried to rebase myself, but it was over my head.

Quoted 8 lines

I'm looking for a qemu solution where I can use my cores.

My use case is doing gcc porting for aarch64-*-freebsd*. I think it doesn't matter which OS. This arch has not enough real affordable HW solutions on the market yet. So I was looking for your solution. Claudio gave me a hint about it.

Your recent merge/rebase only covers arm itself, not aarch64, right?

Indeed, only arm. Keep in mind that this patch series applies to the upstream version of QEMU, not to the mttcg branch.
In other words, the repo includes a version of QEMU which is single-threaded with some changes for the atomic instructions handling in sight of a multi-threaded emulation.

Quoted 3 lines

Linking fails with unreferenced cpu_exclusive_addr stuff in target-arm/translate-a64.c

Even if aarch64 is not supported, this error should not happen. My fault, I will fix it in the coming version.

Quoted 2 lines

Are you working on this already? Or Claudio?

As soon as the mttcg branch will be updated, I will rebase this patch series on top of the new branch, and possibly I will also cover the aarch64 architecture.

Thank you,
alvise

Quoted 9 lines
Quoted 2 lines

This work has been sponsored by Huawei Technologies Duesseldorf GmbH.

...

Thank you!
Andreas

#53 lore raw
In reply to Alvise Rigo
Quoted 9 lines · Alvise Rigo

This is the sixth iteration of the patch series which applies to the upstream branch of QEMU (v2.5.0-rc3).

Changes versus previous versions are at the bottom of this cover letter.

The code is also available at following repository:
https://git.virtualopensystems.com/dev/qemu-mt.git
branch:
slowpath-for-atomic-v6-no-mttcg

I'm starting to look through this now. However one problem that immediately comes up is the aarch64 breakage. Because there is an intrinsic link between a lot of the arm and aarch64 code it breaks the other targets.

You could fix this by ensuring that CONFIG_TCG_USE_LDST_EXCL doesn't get passed to the aarch64 build (tricky as aarch64-softmmu.mak includes arm-softmmu.mak) or bite the bullet now and add the 64 bit helpers that will be needed to convert the aarch64 exclusive equivalents.

Quoted 140 lines

This patch series provides an infrastructure for atomic instruction implementation in QEMU, thus offering a 'legacy' solution for translating guest atomic instructions. Moreover, it can be considered as a first step toward a multi-thread TCG.

The underlying idea is to provide new TCG helpers (sort of softmmu helpers) that guarantee atomicity to some memory accesses or in general a way to define memory transactions.

More specifically, the new softmmu helpers behave as LoadLink and StoreConditional instructions, and are called from TCG code by means of target specific helpers. This work includes the implementation for all the ARM atomic instructions, see target-arm/op_helper.c.

The implementation heavily uses the software TLB together with a new bitmap that has been added to the ram_list structure which flags, on a per-CPU basis, all the memory pages that are in the middle of a LoadLink (LL), StoreConditional (SC) operation. Since all these pages can be accessed directly through the fast-path and alter a vCPU's linked value, the new bitmap has been coupled with a new TLB flag for the TLB virtual address which forces the slow-path execution for all the accesses to a page containing a linked address.

The new slow-path is implemented such that:
- the LL behaves as a normal load slow-path, except for clearing the dirty flag in the bitmap. The cputlb.c code while generating a TLB entry, checks if there is at least one vCPU that has the bit cleared in the exclusive bitmap, it that case the TLB entry will have the EXCL flag set, thus forcing the slow-path. In order to ensure that all the vCPUs will follow the slow-path for that page, we flush the TLB cache of all the other vCPUs.

The LL will also set the linked address and size of the access in a vCPU's private variable. After the corresponding SC, this address will be set to a reset value.

- the SC can fail returning 1, or succeed, returning 0. It has to come always after a LL and has to access the same address 'linked' by the previous LL, otherwise it will fail. If in the time window delimited by a legit pair of LL/SC operations another write access happens to the linked address, the SC will fail.

In theory, the provided implementation of TCG LoadLink/StoreConditional can be used to properly handle atomic instructions on any architecture.

The code has been tested with bare-metal test cases and by booting Linux.

* Performance considerations
The new slow-path adds some overhead to the translation of the ARM atomic instructions, since their emulation doesn't happen anymore only in the guest (by mean of pure TCG generated code), but requires the execution of two helpers functions. Despite this, the additional time required to boot an ARM Linux kernel on an i7 clocked at 2.5GHz is negligible.
Instead, on a LL/SC bound test scenario - like:
https://git.virtualopensystems.com/dev/tcg_baremetal_tests.git - this solution requires 30% (1 million iterations) and 70% (10 millions iterations) of additional time for the test to complete.

Changes from v5:
- The exclusive memory region is now set through a CPUClass hook, allowing any architecture to decide the memory area that will be protected during a LL/SC operation [PATCH 3]
- The runtime helpers dropped any target dependency and are now in a common file [PATCH 5]
- Improved the way we restore a guest page as non-exclusive [PATCH 9]
- Included MMIO memory as possible target of LL/SC instructions. This also required to somehow simplify the helper_*_st_name helpers in softmmu_template.h [PATCH 8-14]

Changes from v4:
- Reworked the exclusive bitmap to be of fixed size (8 bits per address)
- The slow-path is now TCG backend independent, no need to touch tcg/* anymore as suggested by Aurelien Jarno.

Changes from v3:
- based on upstream QEMU
- addressed comments from Alex Bennée
- the slow path can be enabled by the user with:
./configure --enable-tcg-ldst-excl only if the backend supports it
- all the ARM ldex/stex instructions make now use of the slow path
- added aarch64 TCG backend support
- part of the code has been rewritten

Changes from v2:
- the bitmap accessors are now atomic
- a rendezvous between vCPUs and a simple callback support before executing a TB have been added to handle the TLB flush support
- the softmmu_template and softmmu_llsc_template have been adapted to work on real multi-threading

Changes from v1:
- The ram bitmap is not reversed anymore, 1 = dirty, 0 = exclusive
- The way how the offset to access the bitmap is calculated has been improved and fixed
- A page to be set as dirty requires a vCPU to target the protected address and not just an address in the page
- Addressed comments from Richard Henderson to improve the logic in softmmu_template.h and to simplify the methods generation through softmmu_llsc_template.h
- Added initial implementation of qemu_{ldlink,stcond}_i32 for tcg/i386

This work has been sponsored by Huawei Technologies Duesseldorf GmbH.

Alvise Rigo (14):
exec.c: Add new exclusive bitmap to ram_list
softmmu: Add new TLB_EXCL flag
Add CPUClass hook to set exclusive range
softmmu: Add helpers for a new slowpath
tcg: Create new runtime helpers for excl accesses
configure: Use slow-path for atomic only when the softmmu is enabled
target-arm: translate: Use ld/st excl for atomic insns
target-arm: Add atomic_clear helper for CLREX insn
softmmu: Add history of excl accesses
softmmu: Simplify helper_*_st_name, wrap unaligned code
softmmu: Simplify helper_*_st_name, wrap MMIO code
softmmu: Simplify helper_*_st_name, wrap RAM code
softmmu: Include MMIO/invalid exclusive accesses
softmmu: Protect MMIO exclusive range

 Makefile.target             |   2 +-
 configure                   |   4 +
 cputlb.c                    |  67 ++++++++-
 exec.c                      |   8 +-
 include/exec/cpu-all.h      |   8 ++
 include/exec/cpu-defs.h     |   1 +
 include/exec/helper-gen.h   |   1 +
 include/exec/helper-proto.h |   1 +
 include/exec/helper-tcg.h   |   1 +
 include/exec/memory.h       |   4 +-
 include/exec/ram_addr.h     |  76 ++++++++++
 include/qom/cpu.h           |  21 +++
 qom/cpu.c                   |   7 +
 softmmu_llsc_template.h     | 144 +++++++++++++++++++
 softmmu_template.h          | 338 +++++++++++++++++++++++++++++++++-----------
 target-arm/helper.h         |   2 +
 target-arm/op_helper.c      |   6 +
 target-arm/translate.c      | 102 ++++++++++++-
 tcg-llsc-helper.c           | 109 ++++++++++++++
Quoted 8 lines
 tcg-llsc-helper.h           |  35 +++++
 tcg/tcg-llsc-gen-helper.h   |  32 +++++
 tcg/tcg.h                   |  31 ++++
 22 files changed, 909 insertions(+), 91 deletions(-)
 create mode 100644 softmmu_llsc_template.h
 create mode 100644 tcg-llsc-helper.c
 create mode 100644 tcg-llsc-helper.h
 create mode 100644 tcg/tcg-llsc-gen-helper.h

--
Alex Bennée

#54 lore raw

Hi Alex,

Quoted 6 lines · Alex Bennée
Quoted 3 lines · Alvise Rigo

This is the sixth iteration of the patch series which applies to the upstream branch of QEMU (v2.5.0-rc3).

Quoted 9 lines
Quoted 6 lines

Changes versus previous versions are at the bottom of this cover letter.

The code is also available at following repository:
https://git.virtualopensystems.com/dev/qemu-mt.git
branch:
slowpath-for-atomic-v6-no-mttcg

I'm starting to look through this now. However one problem that

Thank you for this.

Quoted 9 lines

immediately comes up is the aarch64 breakage. Because there is an intrinsic link between a lot of the arm and aarch64 code it breaks the other targets.

You could fix this by ensuring that CONFIG_TCG_USE_LDST_EXCL doesn't get passed to the aarch64 build (tricky as aarch64-softmmu.mak includes arm-softmmu.mak) or bite the bullet now and add the 64 bit helpers that will be needed to convert the aarch64 exclusive equivalents.

This is what I'm doing right now :)

Best regards,
alvise

Quoted 158 lines
Quoted 86 lines

This patch series provides an infrastructure for atomic instruction implementation in QEMU, thus offering a 'legacy' solution for translating guest atomic instructions. Moreover, it can be considered as a first step toward a multi-thread TCG.

The underlying idea is to provide new TCG helpers (sort of softmmu helpers) that guarantee atomicity to some memory accesses or in general a way to define memory transactions.

More specifically, the new softmmu helpers behave as LoadLink and StoreConditional instructions, and are called from TCG code by means of target specific helpers. This work includes the implementation for all the ARM atomic instructions, see target-arm/op_helper.c.

The implementation heavily uses the software TLB together with a new bitmap that has been added to the ram_list structure which flags, on a per-CPU basis, all the memory pages that are in the middle of a LoadLink (LL), StoreConditional (SC) operation. Since all these pages can be accessed directly through the fast-path and alter a vCPU's linked value, the new bitmap has been coupled with a new TLB flag for the TLB virtual address which forces the slow-path execution for all the accesses to a page containing a linked address.

The new slow-path is implemented such that:
- the LL behaves as a normal load slow-path, except for clearing the dirty flag in the bitmap. The cputlb.c code while generating a TLB entry, checks if there is at least one vCPU that has the bit cleared in the exclusive bitmap, it that case the TLB entry will have the EXCL flag set, thus forcing the slow-path. In order to ensure that all the vCPUs will follow the slow-path for that page, we flush the TLB cache of all the other vCPUs.

The LL will also set the linked address and size of the access in a vCPU's private variable. After the corresponding SC, this address will be set to a reset value.

- the SC can fail returning 1, or succeed, returning 0. It has to come always after a LL and has to access the same address 'linked' by the previous LL, otherwise it will fail. If in the time window delimited by a legit pair of LL/SC operations another write access happens to the linked address, the SC will fail.

In theory, the provided implementation of TCG LoadLink/StoreConditional can be used to properly handle atomic instructions on any architecture.

The code has been tested with bare-metal test cases and by booting Linux.

* Performance considerations
The new slow-path adds some overhead to the translation of the ARM atomic instructions, since their emulation doesn't happen anymore only in the guest (by mean of pure TCG generated code), but requires the execution of two helpers functions. Despite this, the additional time required to boot an ARM Linux kernel on an i7 clocked at 2.5GHz is negligible.
Instead, on a LL/SC bound test scenario - like:
https://git.virtualopensystems.com/dev/tcg_baremetal_tests.git - this solution requires 30% (1 million iterations) and 70% (10 millions iterations) of additional time for the test to complete.

Changes from v5:
- The exclusive memory region is now set through a CPUClass hook, allowing any architecture to decide the memory area that will be protected during a LL/SC operation [PATCH 3]
- The runtime helpers dropped any target dependency and are now in a common file [PATCH 5]
- Improved the way we restore a guest page as non-exclusive [PATCH 9]
- Included MMIO memory as possible target of LL/SC instructions. This also required to somehow simplify the helper_*_st_name helpers in softmmu_template.h [PATCH 8-14]

Changes from v4:
- Reworked the exclusive bitmap to be of fixed size (8 bits per address)
- The slow-path is now TCG backend independent, no need to touch tcg/* anymore as suggested by Aurelien Jarno.

Changes from v3:
- based on upstream QEMU
- addressed comments from Alex Bennée
- the slow path can be enabled by the user with:
./configure --enable-tcg-ldst-excl only if the backend supports it
- all the ARM ldex/stex instructions make now use of the slow path
- added aarch64 TCG backend support
- part of the code has been rewritten

Quoted 3 lines

Changes from v2:
- the bitmap accessors are now atomic
- a rendezvous between vCPUs and a simple callback support before

executing

Quoted 2 lines

a TB have been added to handle the TLB flush support
- the softmmu_template and softmmu_llsc_template have been adapted to

work

Quoted 7 lines

on real multi-threading

Changes from v1:
- The ram bitmap is not reversed anymore, 1 = dirty, 0 = exclusive
- The way how the offset to access the bitmap is calculated has been improved and fixed
- A page to be set as dirty requires a vCPU to target the protected

address

Quoted 24 lines

and not just an address in the page
- Addressed comments from Richard Henderson to improve the logic in softmmu_template.h and to simplify the methods generation through softmmu_llsc_template.h
- Added initial implementation of qemu_{ldlink,stcond}_i32 for tcg/i386

This work has been sponsored by Huawei Technologies Duesseldorf GmbH.

Alvise Rigo (14):
exec.c: Add new exclusive bitmap to ram_list
softmmu: Add new TLB_EXCL flag
Add CPUClass hook to set exclusive range
softmmu: Add helpers for a new slowpath
tcg: Create new runtime helpers for excl accesses
configure: Use slow-path for atomic only when the softmmu is enabled
target-arm: translate: Use ld/st excl for atomic insns
target-arm: Add atomic_clear helper for CLREX insn
softmmu: Add history of excl accesses
softmmu: Simplify helper_*_st_name, wrap unaligned code
softmmu: Simplify helper_*_st_name, wrap MMIO code
softmmu: Simplify helper_*_st_name, wrap RAM code
softmmu: Include MMIO/invalid exclusive accesses
softmmu: Protect MMIO exclusive range

Quoted 15 lines
 Makefile.target             |   2 +-
 configure                   |   4 +
 cputlb.c                    |  67 ++++++++-
 exec.c                      |   8 +-
 include/exec/cpu-all.h      |   8 ++
 include/exec/cpu-defs.h     |   1 +
 include/exec/helper-gen.h   |   1 +
 include/exec/helper-proto.h |   1 +
 include/exec/helper-tcg.h   |   1 +
 include/exec/memory.h       |   4 +-
 include/exec/ram_addr.h     |  76 ++++++++++
 include/qom/cpu.h           |  21 +++
 qom/cpu.c                   |   7 +
 softmmu_llsc_template.h     | 144 +++++++++++++++++++
 softmmu_template.h          | 338

+++++++++++++++++++++++++++++++++-----------

Quoted 4 lines
 target-arm/helper.h         |   2 +
 target-arm/op_helper.c      |   6 +
 target-arm/translate.c      | 102 ++++++++++++-
 tcg-llsc-helper.c           | 109 ++++++++++++++
Quoted 8 lines
 tcg-llsc-helper.h           |  35 +++++
 tcg/tcg-llsc-gen-helper.h   |  32 +++++
 tcg/tcg.h                   |  31 ++++
 22 files changed, 909 insertions(+), 91 deletions(-)
 create mode 100644 softmmu_llsc_template.h
 create mode 100644 tcg-llsc-helper.c
 create mode 100644 tcg-llsc-helper.h
 create mode 100644 tcg/tcg-llsc-gen-helper.h

--
Alex Bennée

#55 lore raw
In reply to Alvise Rigo

Hi,

Quoted 21 lines

From: [email protected] [mailto:qemu-devel- [email protected]] On Behalf Of Alvise Rigo
Sent: Monday, 14 December 2015 00:41

This is the sixth iteration of the patch series which applies to the upstream branch of QEMU (v2.5.0-rc3).

Changes versus previous versions are at the bottom of this cover letter.

The code is also available at following repository:
https://git.virtualopensystems.com/dev/qemu-mt.git
branch:
slowpath-for-atomic-v6-no-mttcg

This patch series provides an infrastructure for atomic instruction implementation in QEMU, thus offering a 'legacy' solution for translating guest atomic instructions. Moreover, it can be considered as a first step toward a multi-thread TCG.

Quoted 8 lines

The underlying idea is to provide new TCG helpers (sort of softmmu helpers) that guarantee atomicity to some memory accesses or in general a way to define memory transactions.

More specifically, the new softmmu helpers behave as LoadLink and StoreConditional instructions, and are called from TCG code by means of target specific helpers. This work includes the implementation for all the ARM atomic instructions, see target-arm/op_helper.c.

As a heads up, we just added support for alignment checks in LDREX:
https://github.com/qemu/qemu/commit/30901475b91ef1f46304404ab4bfe89097f61b96

Hopefully it is an easy change to ensure that the same check happens for the relevant loads when CONFIG_TCG_USE_LDST_EXCL is enabled?

Thanks,
Andrew

#56 lore raw

Hi,

Quoted 28 lines · Andrew Baumann

Hi,

Quoted 25 lines

From: [email protected] [mailto:qemu-devel- [email protected]] On Behalf Of Alvise Rigo
Sent: Monday, 14 December 2015 00:41

This is the sixth iteration of the patch series which applies to the upstream branch of QEMU (v2.5.0-rc3).

Changes versus previous versions are at the bottom of this cover letter.

The code is also available at following repository:
https://git.virtualopensystems.com/dev/qemu-mt.git
branch:
slowpath-for-atomic-v6-no-mttcg

This patch series provides an infrastructure for atomic instruction implementation in QEMU, thus offering a 'legacy' solution for translating guest atomic instructions. Moreover, it can be considered as a first step toward a multi-thread TCG.

The underlying idea is to provide new TCG helpers (sort of softmmu helpers) that guarantee atomicity to some memory accesses or in general a way to define memory transactions.

Quoted 7 lines
Quoted 4 lines

More specifically, the new softmmu helpers behave as LoadLink and StoreConditional instructions, and are called from TCG code by means of target specific helpers. This work includes the implementation for all the ARM atomic instructions, see target-arm/op_helper.c.

As a heads up, we just added support for alignment checks in LDREX:
https://github.com/qemu/qemu/commit/30901475b91ef1f46304404ab4bfe89097f61b96

Thank you for the update.

Quoted 2 lines

Hopefully it is an easy change to ensure that the same check happens for the relevant loads when CONFIG_TCG_USE_LDST_EXCL is enabled?

It should be if we add an aligned variant for each of the exclusive helper. BTW, why don't we make the check also for the STREX instruction?

Regards,
alvise

Quoted 3 lines

Thanks,
Andrew

#57 lore raw
Quoted 9 lines · alvise rigo

Hi,

Quoted 2 lines · Andrew Baumann

As a heads up, we just added support for alignment checks in LDREX:
https://github.com/qemu/qemu/commit/30901475b91ef1f46304404ab4bfe89097f61b96

It should be if we add an aligned variant for each of the exclusive helper. BTW, why don't we make the check also for the STREX instruction?

Andrew's patch only changed the bits Windows cares about, I think. We should indeed extend this to cover also STREX and the A64 instructions as well, I think.

thanks
-- PMM

#58 lore raw
Quoted 8 lines · Peter Maydell
Quoted 6 lines · alvise rigo

Hi,

Quoted 2 lines · Andrew Baumann

As a heads up, we just added support for alignment checks in LDREX:
https://github.com/qemu/qemu/commit/30901475b91ef1f46304404ab4bfe89097f61b96

Quoted 6 lines
Quoted 2 lines

It should be if we add an aligned variant for each of the exclusive helper. BTW, why don't we make the check also for the STREX instruction?

Andrew's patch only changed the bits Windows cares about, I think. We should indeed extend this to cover also STREX and the A64 instructions as well, I think.

The alignment check is easily doable in general. The only tricky part I found is the A64's STXP instruction that requires quadword alignment for the 64bit paired access.
In that case, the translation of the instruction will rely on a aarch64-only helper. The alternative solution would be to extend softmmu_template.h to generate 128bit accesses, but I don't believe this is the right way to go.

Regards,
alvise

Quoted 3 lines

thanks
-- PMM

#59 lore raw
Quoted 16 lines · alvise rigo
Quoted 8 lines · Peter Maydell
Quoted 6 lines · alvise rigo

Hi,

Quoted 2 lines · Andrew Baumann

As a heads up, we just added support for alignment checks in LDREX:
https://github.com/qemu/qemu/commit/30901475b91ef1f46304404ab4bfe89097f61b96

Quoted 6 lines
Quoted 2 lines

It should be if we add an aligned variant for each of the exclusive helper. BTW, why don't we make the check also for the STREX instruction?

Andrew's patch only changed the bits Windows cares about, I think. We should indeed extend this to cover also STREX and the A64 instructions as well, I think.

Quoted 7 lines

The alignment check is easily doable in general. The only tricky part I found is the A64's STXP instruction that requires quadword alignment for the 64bit paired access.
In that case, the translation of the instruction will rely on a aarch64-only helper. The alternative solution would be to extend softmmu_template.h to generate 128bit accesses, but I don't believe this is the right way to go.

Yes, 128-bit alignment check is not currently easy. We should do the others first and then think about the right approach for the 128 bit stuff. (I forget what rth's view about that was.)

thanks
-- PMM