Skip to contentSkip to conversation

gdbstub/next (MemTxAttrs and re-factoring)

PATCH Superseded Alex Bennée 4 participants 28 messages Sep 26, 2022 – Sep 27, 2022

Subsystems (2)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v2 cover gdbstub/next (MemTxAttrs and re-factoring)

Hi,

This is v3 of the MexTxAttrs update alongside the current state of gdbstub/next (as fixing current_cpu from gdbstub was the original motivation).

Following feedback I've drastically simplified the first patch and push the all the setting of requester_id to the per-arch tlb_fill and get_phys_page_attrs_debug helpers to fill in correctly. There is also a new patch to fix up HVF on Aarch64 which rely mostly on software modelled devices.

The later re-factoring is unchanged having sailed through review without any comments.

The following are left for review:

- qtest: make read/write operation appear to be from CPU
- target/arm: ensure HVF traps set appropriate MemTxAttrs
- target/arm: enable tracking of CPU index in MemTxAttrs
- hw: encode accessing CPU index in MemTxAttrs

Alex Bennée (11):
hw: encode accessing CPU index in MemTxAttrs target/arm: enable tracking of CPU index in MemTxAttrs target/arm: ensure HVF traps set appropriate MemTxAttrs
qtest: make read/write operation appear to be from CPU hw/intc/gic: use MxTxAttrs to divine accessing CPU hw/timer: convert mptimer access to attrs to derive cpu index
configure: move detected gdb to TCG's config-host.mak
gdbstub: move into its own sub directory
gdbstub: move sstep flags probing into AccelClass
gdbstub: move breakpoint logic to accel ops
gdbstub: move guest debug support check to ops

 configure                      |   7 ++
 meson.build                    |   4 +-
 accel/kvm/kvm-cpus.h           |   4 +
 gdbstub/internals.h            |  17 ++++
 gdbstub/trace.h                |   1 +
 include/exec/memattrs.h        |  17 +++-
 include/qemu/accel.h           |  12 +++
 include/sysemu/accel-ops.h     |   7 ++
 include/sysemu/cpus.h          |   3 +
 include/sysemu/kvm.h           |  20 -----
 accel/accel-common.c           |  10 +++
 accel/kvm/kvm-accel-ops.c      |   9 ++
 accel/kvm/kvm-all.c            |  44 +++++-----
 accel/stubs/kvm-stub.c         |  16 ----
 accel/tcg/tcg-accel-ops.c      |  98 +++++++++++++++++++++
 accel/tcg/tcg-all.c            |  17 ++++
 gdbstub.c => gdbstub/gdbstub.c | 156 ++++-----------------------------
 gdbstub/softmmu.c              |  51 +++++++++++
 gdbstub/user.c                 |  68 ++++++++++++++
 hw/intc/arm_gic.c              |  39 +++++----
 hw/timer/arm_mptimer.c         |  25 +++---
 softmmu/cpus.c                 |   7 ++
 softmmu/qtest.c                |  26 +++---
 target/arm/hvf/hvf.c           |   4 +-
 target/arm/ptw.c               |   3 +
 MAINTAINERS                    |   2 +-
 gdbstub/meson.build            |   9 ++
 gdbstub/trace-events           |  29 ++++++
 trace-events                   |  28 ------
 29 files changed, 461 insertions(+), 272 deletions(-)
 create mode 100644 gdbstub/internals.h
 create mode 100644 gdbstub/trace.h
 rename gdbstub.c => gdbstub/gdbstub.c (95%)
 create mode 100644 gdbstub/softmmu.c
 create mode 100644 gdbstub/user.c
 create mode 100644 gdbstub/meson.build
 create mode 100644 gdbstub/trace-events
Signature · 1 line
2.34.1
#1 lore raw

PATCH v2 1/11 hw: encode accessing CPU index in MemTxAttrs

We currently have hacks across the hw/ to reference current_cpu to work out what the current accessing CPU is. This breaks in some cases including using gdbstub to access HW state. As we have MemTxAttrs to describe details about the access lets extend it to mention if this is a CPU access and which one it is.

There are a number of places we need to fix up including:

CPU helpers directly calling address_space_*() fns models in hw/ fishing the data out of current_cpu hypervisors offloading device emulation to QEMU

I'll start addressing some of these in following patches.

---
v2
- use separate field cpu_index
- bool for requester_is_cpu
v3
- switch to enum MemTxRequesterType
- move helper #define to patch
- revert to overloading requester_id
- mention hypervisors in commit message
- drop cputlb tweaks, they will move to target specific code

---
 include/exec/memattrs.h | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)
include/exec/memattrs.h +16 -1
index 9fb98bc1ef..0fb5f29d25 100644
@@ -14,6 +14,15 @@
 #ifndef MEMATTRS_H
 #define MEMATTRS_H
 
+/*
+ * Where the memory transaction comes from
+ */
+typedef enum MemTxRequesterType {
+    MEMTXATTRS_CPU,
+    MEMTXATTRS_MSI,
+} MemTxRequesterType;
+
+
 /* Every memory transaction has associated with it a set of
  * attributes. Some of these are generic (such as the ID of
  * the bus master); some are specific to a particular kind of
@@ -43,7 +52,9 @@ typedef struct MemTxAttrs {
      * (see MEMTX_ACCESS_ERROR).
      */
     unsigned int memory:1;
-    /* Requester ID (for MSI for example) */
+    /* Requester type (e.g. CPU or PCI MSI) */
+    MemTxRequesterType requester_type:2;
+    /* Requester ID */
     unsigned int requester_id:16;
     /* Invert endianness for this page */
     unsigned int byte_swap:1;
@@ -66,6 +77,10 @@ typedef struct MemTxAttrs {
  */
 #define MEMTXATTRS_UNSPECIFIED ((MemTxAttrs) { .unspecified = 1 })
 
+/* Helper for setting a basic CPU sourced transaction */
+#define MEMTXATTRS_CPU(id) ((MemTxAttrs) \
+                            {.requester_type = MEMTXATTRS_CPU, .requester_id = id})
+
 /* New-style MMIO accessors can indicate that the transaction failed.
  * A zero (MEMTX_OK) response means success; anything else is a failure
  * of some kind. The memory subsystem will bitwise-OR together results
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH v2 01/11] hw: encode accessing CPU index in MemTxAttrs

Quoted 17 lines · Alex Bennée

We currently have hacks across the hw/ to reference current_cpu to work out what the current accessing CPU is. This breaks in some cases including using gdbstub to access HW state. As we have MemTxAttrs to describe details about the access lets extend it to mention if this is a CPU access and which one it is.

There are a number of places we need to fix up including:

CPU helpers directly calling address_space_*() fns models in hw/ fishing the data out of current_cpu hypervisors offloading device emulation to QEMU

I'll start addressing some of these in following patches.

Quoted 10 lines

---
v2
- use separate field cpu_index
- bool for requester_is_cpu
v3
- switch to enum MemTxRequesterType
- move helper #define to patch
- revert to overloading requester_id
- mention hypervisors in commit message
- drop cputlb tweaks, they will move to target specific code

I still don't see anything in this patchset that updates the code which currently assumes requester_id to be a PCI index to check that it hasn't been handed a MemTxAttrs that uses requester_id as a CPU number.

I also still need to go and look up how hardware does this, so please don't queue this patchset yet. In particular, we should think about whether we want this to be:
* a CPU number, but only set opt-in by some target archs
* a CPU number, valid for all target archs
* a unique-within-the-machine identifier of the transaction master (i.e. which can be set by DMA controllers, etc, not just CPUs)

I would also like some input from Edgar since I know Xilinx have some more extensive out-of-tree uses of requester_id. We aren't obligated to not break out-of-tree code, but that seems like a bunch of experience and knowledge about how real hardware works that would be useful for informing how we design this.

thanks
-- PMM

#3 lore raw

Re: [PATCH v2 01/11] hw: encode accessing CPU index in MemTxAttrs

Quoted 29 lines · Peter Maydell
Quoted 17 lines · Alex Bennée

We currently have hacks across the hw/ to reference current_cpu to work out what the current accessing CPU is. This breaks in some cases including using gdbstub to access HW state. As we have MemTxAttrs to describe details about the access lets extend it to mention if this is a CPU access and which one it is.

There are a number of places we need to fix up including:

CPU helpers directly calling address_space_*() fns models in hw/ fishing the data out of current_cpu hypervisors offloading device emulation to QEMU

I'll start addressing some of these in following patches.

Quoted 10 lines

---
v2
- use separate field cpu_index
- bool for requester_is_cpu
v3
- switch to enum MemTxRequesterType
- move helper #define to patch
- revert to overloading requester_id
- mention hypervisors in commit message
- drop cputlb tweaks, they will move to target specific code

Quoted 4 lines

I still don't see anything in this patchset that updates the code which currently assumes requester_id to be a PCI index to check that it hasn't been handed a MemTxAttrs that uses requester_id as a CPU number.

OK I'll update so all the existing cases setting requester_id also set the type to MEMTXATTRS_MSI.

I also noticed the GIC ITS code checks requester ID. Should we assert (or hw_error?) if it's not the case?

Quoted 4 lines

I also still need to go and look up how hardware does this, so please don't queue this patchset yet. In particular, we should think about whether we want this to be:
* a CPU number, but only set opt-in by some target archs

Given a whole bunch of arches currently use MEMTXATTRS_UNSPECIFIED I think for now it's worth confining to just ARM where we know we have devices that care about the cpu_index and have tagged the various paths with the correct data.

Quoted 4 lines

* a CPU number, valid for all target archs
* a unique-within-the-machine identifier of the transaction master (i.e. which can be set by DMA controllers, etc, not just CPUs)

That would require something to keep a map of requester_id's to source/index right?

Quoted 6 lines

I would also like some input from Edgar since I know Xilinx have some more extensive out-of-tree uses of requester_id. We aren't obligated to not break out-of-tree code, but that seems like a bunch of experience and knowledge about how real hardware works that would be useful for informing how we design this.

His comment against the last iteration was:

"CPU's can also have a Master-IDs (Requester IDs) which are unrelated to the Clusters CPU index. This is used for example in the Xilinx ZynqMP and Xilinx Versal and the XMPU (Memory Protection Units).

Anyway, I think this approach is an improvement from the current state but would rather see a new separate field from requester_id. Overloading requester_id will break some of our use-cases (in the Xilinx tree)...

IIRC a real GIC differentiates between the connected CPU's through different ports, not by looking at master-ids but I'm not 100% sure..."

at the same time Richard's not keen about adding extra fields (especially as some arches have INT32_MAX bounds for cpu_index). However one approach would be to expand the requester_id field and you could then expand MemTxRequesterType to and have a multiplexed type although I admit it's hard to imagine HW that cares about both the CPU and bus id at the same time.

Quoted 3 lines

thanks
-- PMM

Signature · 1 line
Alex Bennée
#4 lore raw

Re: [PATCH v2 01/11] hw: encode accessing CPU index in MemTxAttrs

Quoted 10 lines · Alex Bennée
Quoted 4 lines · Peter Maydell

I still don't see anything in this patchset that updates the code which currently assumes requester_id to be a PCI index to check that it hasn't been handed a MemTxAttrs that uses requester_id as a CPU number.

OK I'll update so all the existing cases setting requester_id also set the type to MEMTXATTRS_MSI.

The problem is not the places that set requester_id (you can arrange for the default to be MSI for back-compat if you don't want to explicitly set it), but the places that *read* it.

-- PMM

#5 lore raw

Re: [PATCH v2 01/11] hw: encode accessing CPU index in MemTxAttrs

In reply to Alex Bennée
Quoted 35 lines · Alex Bennée

We currently have hacks across the hw/ to reference current_cpu to work out what the current accessing CPU is. This breaks in some cases including using gdbstub to access HW state. As we have MemTxAttrs to describe details about the access lets extend it to mention if this is a CPU access and which one it is.

There are a number of places we need to fix up including:

CPU helpers directly calling address_space_*() fns models in hw/ fishing the data out of current_cpu hypervisors offloading device emulation to QEMU

I'll start addressing some of these in following patches.

---
v2
- use separate field cpu_index
- bool for requester_is_cpu
v3
- switch to enum MemTxRequesterType
- move helper #define to patch
- revert to overloading requester_id
- mention hypervisors in commit message
- drop cputlb tweaks, they will move to target specific code

---
  include/exec/memattrs.h | 17 ++++++++++++++++-
  1 file changed, 16 insertions(+), 1 deletion(-)
include/exec/memattrs.h +0 -0
index 9fb98bc1ef..0fb5f29d25 100644

include/exec/memattrs.h line 22

Quoted 9 lines
include/exec/memattrs.h +6 -0
@@ -14,6 +14,15 @@
  #ifndef MEMATTRS_H
  #define MEMATTRS_H
 
+/*
+ * Where the memory transaction comes from
+ */
+typedef enum MemTxRequesterType {
+    MEMTXATTRS_CPU,
+    MEMTXATTRS_MSI,

I don't think MSI is any sensible identifier here. What you actually are trying to describe are bus messages that may carry additional source information - and what you describe as "MSI" here most likely means "no further information".

How about you just call it "BUS", "DMA" or literally "UNSPECIFIED" for now? Also, why is unspecified not part of the requester type enum?

Alex

Quoted 28 lines
include/exec/memattrs.h +10 -1
+} MemTxRequesterType;
+
+
  /* Every memory transaction has associated with it a set of
   * attributes. Some of these are generic (such as the ID of
   * the bus master); some are specific to a particular kind of
@@ -43,7 +52,9 @@ typedef struct MemTxAttrs {
       * (see MEMTX_ACCESS_ERROR).
       */
      unsigned int memory:1;
-    /* Requester ID (for MSI for example) */
+    /* Requester type (e.g. CPU or PCI MSI) */
+    MemTxRequesterType requester_type:2;
+    /* Requester ID */
      unsigned int requester_id:16;
      /* Invert endianness for this page */
      unsigned int byte_swap:1;
@@ -66,6 +77,10 @@ typedef struct MemTxAttrs {
   */
  #define MEMTXATTRS_UNSPECIFIED ((MemTxAttrs) { .unspecified = 1 })
 
+/* Helper for setting a basic CPU sourced transaction */
+#define MEMTXATTRS_CPU(id) ((MemTxAttrs) \
+                            {.requester_type = MEMTXATTRS_CPU, .requester_id = id})
+
  /* New-style MMIO accessors can indicate that the transaction failed.
   * A zero (MEMTX_OK) response means success; anything else is a failure
   * of some kind. The memory subsystem will bitwise-OR together results
#6 lore raw

PATCH v2 2/11 target/arm: enable tracking of CPU index in MemTxAttrs

In reply to Alex Bennée

Both arm_cpu_tlb_fill (for normal IO) and arm_cpu_get_phys_page_attrs_debug (for debug access) come through get_phys_addr which is setting the other memory attributes for the transaction. As these are all by definition CPU accesses we can also set the requested_type/index as appropriate.

---
 target/arm/ptw.c | 3 +++
 1 file changed, 3 insertions(+)
target/arm/ptw.c +3 -0
index 3261039d93..644d450662 100644
@@ -2315,6 +2315,9 @@ bool get_phys_addr(CPUARMState *env, target_ulong address,
 {
     ARMMMUIdx s1_mmu_idx = stage_1_mmu_idx(mmu_idx);
 
+    attrs->requester_type = MEMTXATTRS_CPU;
+    attrs->requester_id = env_cpu(env)->cpu_index;
+
     if (mmu_idx != s1_mmu_idx) {
         /*
          * Call ourselves recursively to do the stage 1 and then stage 2
Signature · 1 line
2.34.1
#7 lore raw

Re: [PATCH v2 02/11] target/arm: enable tracking of CPU index in MemTxAttrs

Quoted 17 lines · Alex Bennée

Both arm_cpu_tlb_fill (for normal IO) and arm_cpu_get_phys_page_attrs_debug (for debug access) come through get_phys_addr which is setting the other memory attributes for the transaction. As these are all by definition CPU accesses we can also set the requested_type/index as appropriate.

---
 target/arm/ptw.c | 3 +++
 1 file changed, 3 insertions(+)
target/arm/ptw.c +0 -0
index 3261039d93..644d450662 100644

target/arm/ptw.c bool get_phys_addr(CPUARMState *env, target_ulong address, · line 2320

Quoted 6 lines
target/arm/ptw.c +3 -0
@@ -2315,6 +2315,9 @@ bool get_phys_addr(CPUARMState *env, target_ulong address,
 {
     ARMMMUIdx s1_mmu_idx = stage_1_mmu_idx(mmu_idx);
 
+    attrs->requester_type = MEMTXATTRS_CPU;
+    attrs->requester_id = env_cpu(env)->cpu_index;
+

This only catches the case where the memory access is done via something that does a virtual-to-physical translation. It misses memory accesses done directly on physical addresses, such as those in arm_ldl_ptw() and arm_ldq_ptw(), plus various M-profile specific ones.

thanks
-- PMM

#8 lore raw

Re: [PATCH v2 02/11] target/arm: enable tracking of CPU index in MemTxAttrs

Quoted 21 lines · Peter Maydell
Quoted 20 lines · Alex Bennée

Both arm_cpu_tlb_fill (for normal IO) and arm_cpu_get_phys_page_attrs_debug (for debug access) come through get_phys_addr which is setting the other memory attributes for the transaction. As these are all by definition CPU accesses we can also set the requested_type/index as appropriate.

---
 target/arm/ptw.c | 3 +++
 1 file changed, 3 insertions(+)
target/arm/ptw.c +0 -0
index 3261039d93..644d450662 100644
@@ -2315,6 +2315,9 @@ bool get_phys_addr(CPUARMState *env, target_ulong address,
 {
     ARMMMUIdx s1_mmu_idx = stage_1_mmu_idx(mmu_idx);
 
Quoted 9 lines

target/arm/ptw.c bool get_phys_addr(CPUARMState *env, target_ulong address, · line 2320

Quoted 3 lines
target/arm/ptw.c +3 -0
+    attrs->requester_type = MEMTXATTRS_CPU;
+    attrs->requester_id = env_cpu(env)->cpu_index;
+

This only catches the case where the memory access is done via something that does a virtual-to-physical translation. It misses memory accesses done directly on physical addresses, such as those in arm_ldl_ptw() and arm_ldq_ptw(), plus various M-profile specific ones.

I thought they were just used for the page table walk. Can you place your page tables aliases with a piece of HW?

Quoted 3 lines

thanks
-- PMM

Signature · 1 line
Alex Bennée
#9 lore raw

Re: [PATCH v2 02/11] target/arm: enable tracking of CPU index in MemTxAttrs

Quoted 11 lines · Alex Bennée
Quoted 6 lines · Peter Maydell

On Mon, 26 Sept 2022 at 14:39, Alex Bennée <[email protected]> wrote:
This only catches the case where the memory access is
done via something that does a virtual-to-physical translation. It misses memory accesses done directly on physical addresses, such as those in arm_ldl_ptw() and arm_ldq_ptw(), plus various M-profile specific ones.

Quoted 2 lines

I thought they were just used for the page table walk. Can you place your page tables aliases with a piece of HW?

They are just used for the page table walk, but they are nonetheless still memory transactions initiated by the CPU, so if we're saying those should be marked up in the transaction-attributes struct then they should count.

thanks
-- PMM

#10 lore raw

PATCH v2 3/11 target/arm: ensure HVF traps set appropriate MemTxAttrs

In reply to Alex Bennée

As most HVF devices are done purely in software we need to make sure we properly encode the source CPU in MemTxAttrs. This will allow the device emulations to use those attributes rather than relying on current_cpu (although current_cpu will still be correct in this case).

Cc Mads Ynddal <[email protected]>
Cc Alexander Graf <[email protected]>
---
 target/arm/hvf/hvf.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
target/arm/hvf/hvf.c +2 -2
index 060aa0ccf4..13b7971560 100644
@@ -1233,11 +1233,11 @@ int hvf_vcpu_exec(CPUState *cpu)
             val = hvf_get_reg(cpu, srt);
             address_space_write(&address_space_memory,
                                 hvf_exit->exception.physical_address,
-                                MEMTXATTRS_UNSPECIFIED, &val, len);
+                                MEMTXATTRS_CPU(cpu->cpu_index), &val, len);
         } else {
             address_space_read(&address_space_memory,
                                hvf_exit->exception.physical_address,
-                               MEMTXATTRS_UNSPECIFIED, &val, len);
+                               MEMTXATTRS_CPU(cpu->cpu_index), &val, len);
             hvf_set_reg(cpu, srt, val);
         }
 
Signature · 1 line
2.34.1
#11 lore raw

Re: [PATCH v2 03/11] target/arm: ensure HVF traps set appropriate MemTxAttrs

Quoted 20 lines · Alex Bennée

As most HVF devices are done purely in software we need to make sure we properly encode the source CPU in MemTxAttrs. This will allow the device emulations to use those attributes rather than relying on current_cpu (although current_cpu will still be correct in this case).

Cc Mads Ynddal <[email protected]>
Cc Alexander Graf <[email protected]>
---
 target/arm/hvf/hvf.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
target/arm/hvf/hvf.c +0 -0
index 060aa0ccf4..13b7971560 100644
@@ -1233,11 +1233,11 @@ int hvf_vcpu_exec(CPUState *cpu)
             val = hvf_get_reg(cpu, srt);
             address_space_write(&address_space_memory,

target/arm/hvf/hvf.c int hvf_vcpu_exec(CPUState *cpu) · line 1242

Quoted 10 lines
target/arm/hvf/hvf.c +2 -2
                                 hvf_exit->exception.physical_address,
-                                MEMTXATTRS_UNSPECIFIED, &val, len);
+                                MEMTXATTRS_CPU(cpu->cpu_index), &val, len);
         } else {
             address_space_read(&address_space_memory,
                                hvf_exit->exception.physical_address,
-                               MEMTXATTRS_UNSPECIFIED, &val, len);
+                               MEMTXATTRS_CPU(cpu->cpu_index), &val, len);
             hvf_set_reg(cpu, srt, val);
         }

Don't we need a similar thing for KVM ? (In that case it's in the generic code in accel/kvm/kvm-all.c, for the KVM_EXIT_MMIO handling.)

-- PMM

#12 lore raw

Re: [PATCH v2 03/11] target/arm: ensure HVF traps set appropriate MemTxAttrs

Quoted 27 lines · Peter Maydell
Quoted 26 lines · Alex Bennée

As most HVF devices are done purely in software we need to make sure we properly encode the source CPU in MemTxAttrs. This will allow the device emulations to use those attributes rather than relying on current_cpu (although current_cpu will still be correct in this case).

Cc Mads Ynddal <[email protected]>
Cc Alexander Graf <[email protected]>
---
 target/arm/hvf/hvf.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
target/arm/hvf/hvf.c +1 -1
index 060aa0ccf4..13b7971560 100644
@@ -1233,11 +1233,11 @@ int hvf_vcpu_exec(CPUState *cpu)
             val = hvf_get_reg(cpu, srt);
             address_space_write(&address_space_memory,
                                 hvf_exit->exception.physical_address,
-                                MEMTXATTRS_UNSPECIFIED, &val, len);
+                                MEMTXATTRS_CPU(cpu->cpu_index), &val, len);
         } else {
             address_space_read(&address_space_memory,
                                hvf_exit->exception.physical_address,
Quoted 8 lines

target/arm/hvf/hvf.c int hvf_vcpu_exec(CPUState *cpu) · line 1242

Quoted 4 lines
target/arm/hvf/hvf.c +1 -1
-                               MEMTXATTRS_UNSPECIFIED, &val, len);
+                               MEMTXATTRS_CPU(cpu->cpu_index), &val, len);
             hvf_set_reg(cpu, srt, val);
         }

Don't we need a similar thing for KVM ? (In that case it's in the generic code in accel/kvm/kvm-all.c, for the KVM_EXIT_MMIO handling.)

Added to MemTxAttrs kvm_arch_post_run(CPUState *cs, struct kvm_run *run) which at least anticipates an out-of-kernel GIC.

Quoted 2 lines

-- PMM

Signature · 1 line
Alex Bennée
Acked-by on v2 3/11
#13 lore raw

Re: [PATCH v2 03/11] target/arm: ensure HVF traps set appropriate MemTxAttrs

In reply to Alex Bennée
Quoted 17 lines · Alex Bennée

As most HVF devices are done purely in software we need to make sure we properly encode the source CPU in MemTxAttrs. This will allow the device emulations to use those attributes rather than relying on current_cpu (although current_cpu will still be correct in this case).

Cc Mads Ynddal <[email protected]>
Cc Alexander Graf <[email protected]>
---
  target/arm/hvf/hvf.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
target/arm/hvf/hvf.c +0 -0
index 060aa0ccf4..13b7971560 100644

target/arm/hvf/hvf.c int hvf_vcpu_exec(CPUState *cpu) · line 1236

Quoted 5 lines
target/arm/hvf/hvf.c +1 -1
@@ -1233,11 +1233,11 @@ int hvf_vcpu_exec(CPUState *cpu)
              val = hvf_get_reg(cpu, srt);
              address_space_write(&address_space_memory,
                                  hvf_exit->exception.physical_address,
-                                MEMTXATTRS_UNSPECIFIED, &val, len);
+                                MEMTXATTRS_CPU(cpu->cpu_index), &val, len);

I think it would make a safer API if MEMTXATTRS_CPU() would take CPUState * as argument so you can just pass in cpu here.

For the HVF part however,

Alex

#14 lore raw

PATCH v2 4/11 qtest: make read/write operation appear to be from CPU

In reply to Alex Bennée

The point of qtest is to simulate how running code might interact with the system. However because it's not a real system we have places in the code which especially handle check qtest_enabled() before referencing current_cpu. Now we can encode these details in the MemTxAttrs lets do that so we can start removing them.

---
v2
- use a common macro instead of specific MEMTXATTRS_QTEST
v3
- macro moved to earlier

---
 softmmu/qtest.c | 26 +++++++++++++-------------
 1 file changed, 13 insertions(+), 13 deletions(-)
softmmu/qtest.c +13 -13
index f8acef2628..3aa2218b95 100644
@@ -520,22 +520,22 @@ static void qtest_process_command(CharBackend *chr, gchar **words)
 
         if (words[0][5] == 'b') {
             uint8_t data = value;
-            address_space_write(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
+            address_space_write(first_cpu->as, addr, MEMTXATTRS_CPU(first_cpu->cpu_index),
                                 &data, 1);
         } else if (words[0][5] == 'w') {
             uint16_t data = value;
             tswap16s(&data);
-            address_space_write(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
+            address_space_write(first_cpu->as, addr, MEMTXATTRS_CPU(first_cpu->cpu_index),
                                 &data, 2);
         } else if (words[0][5] == 'l') {
             uint32_t data = value;
             tswap32s(&data);
-            address_space_write(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
+            address_space_write(first_cpu->as, addr, MEMTXATTRS_CPU(first_cpu->cpu_index),
                                 &data, 4);
         } else if (words[0][5] == 'q') {
             uint64_t data = value;
             tswap64s(&data);
-            address_space_write(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
+            address_space_write(first_cpu->as, addr, MEMTXATTRS_CPU(first_cpu->cpu_index),
                                 &data, 8);
         }
         qtest_send_prefix(chr);
@@ -554,21 +554,21 @@ static void qtest_process_command(CharBackend *chr, gchar **words)
 
         if (words[0][4] == 'b') {
             uint8_t data;
-            address_space_read(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
+            address_space_read(first_cpu->as, addr, MEMTXATTRS_CPU(first_cpu->cpu_index),
                                &data, 1);
             value = data;
         } else if (words[0][4] == 'w') {
             uint16_t data;
-            address_space_read(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
+            address_space_read(first_cpu->as, addr, MEMTXATTRS_CPU(first_cpu->cpu_index),
                                &data, 2);
             value = tswap16(data);
         } else if (words[0][4] == 'l') {
             uint32_t data;
-            address_space_read(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
+            address_space_read(first_cpu->as, addr, MEMTXATTRS_CPU(first_cpu->cpu_index),
                                &data, 4);
             value = tswap32(data);
         } else if (words[0][4] == 'q') {
-            address_space_read(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
+            address_space_read(first_cpu->as, addr, MEMTXATTRS_CPU(first_cpu->cpu_index),
                                &value, 8);
             tswap64s(&value);
         }
@@ -589,7 +589,7 @@ static void qtest_process_command(CharBackend *chr, gchar **words)
         g_assert(len);
 
         data = g_malloc(len);
-        address_space_read(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED, data,
+        address_space_read(first_cpu->as, addr, MEMTXATTRS_CPU(first_cpu->cpu_index), data,
                            len);
 
         enc = g_malloc(2 * len + 1);
@@ -615,7 +615,7 @@ static void qtest_process_command(CharBackend *chr, gchar **words)
         g_assert(ret == 0);
 
         data = g_malloc(len);
-        address_space_read(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED, data,
+        address_space_read(first_cpu->as, addr, MEMTXATTRS_CPU(first_cpu->cpu_index), data,
                            len);
         b64_data = g_base64_encode(data, len);
         qtest_send_prefix(chr);
@@ -650,7 +650,7 @@ static void qtest_process_command(CharBackend *chr, gchar **words)
                 data[i] = 0;
             }
         }
-        address_space_write(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED, data,
+        address_space_write(first_cpu->as, addr, MEMTXATTRS_CPU(first_cpu->cpu_index), data,
                             len);
         g_free(data);
 
@@ -673,7 +673,7 @@ static void qtest_process_command(CharBackend *chr, gchar **words)
         if (len) {
             data = g_malloc(len);
             memset(data, pattern, len);
-            address_space_write(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
+            address_space_write(first_cpu->as, addr, MEMTXATTRS_CPU(first_cpu->cpu_index),
                                 data, len);
             g_free(data);
         }
@@ -707,7 +707,7 @@ static void qtest_process_command(CharBackend *chr, gchar **words)
             out_len = MIN(out_len, len);
         }
 
-        address_space_write(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED, data,
+        address_space_write(first_cpu->as, addr, MEMTXATTRS_CPU(first_cpu->cpu_index), data,
                             len);
 
         qtest_send_prefix(chr);
Signature · 1 line
2.34.1
Acked-by on v2 4/11
#15 lore raw

Re: [PATCH v2 04/11] qtest: make read/write operation appear to be from CPU

Quoted 5 lines · Alex Bennée

The point of qtest is to simulate how running code might interact with the system. However because it's not a real system we have places in the code which especially handle check qtest_enabled() before referencing current_cpu. Now we can encode these details in the MemTxAttrs lets do that so we can start removing them.

s/lets/let's/

Quoted 10 lines

---
v2
- use a common macro instead of specific MEMTXATTRS_QTEST
v3
- macro moved to earlier

---
  softmmu/qtest.c | 26 +++++++++++++-------------
  1 file changed, 13 insertions(+), 13 deletions(-)
#16 lore raw

PATCH v2 5/11 hw/intc/gic: use MxTxAttrs to divine accessing CPU

In reply to Alex Bennée

Now that MxTxAttrs encodes a CPU we should use that to figure it out. This solves edge cases like accessing via gdbstub or qtest.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/124

---
v2
- update for new field
- bool asserts

---
 hw/intc/arm_gic.c | 39 ++++++++++++++++++++++-----------------
 1 file changed, 22 insertions(+), 17 deletions(-)
hw/intc/arm_gic.c +22 -17
index 492b2421ab..d907df3884 100644
@@ -56,17 +56,22 @@ static const uint8_t gic_id_gicv2[] = {
     0x04, 0x00, 0x00, 0x00, 0x90, 0xb4, 0x2b, 0x00, 0x0d, 0xf0, 0x05, 0xb1
 };
 
-static inline int gic_get_current_cpu(GICState *s)
+static inline int gic_get_current_cpu(GICState *s, MemTxAttrs attrs)
 {
-    if (!qtest_enabled() && s->num_cpu > 1) {
-        return current_cpu->cpu_index;
-    }
-    return 0;
+    /*
+     * Something other than a CPU accessing the GIC would be a bug as
+     * would a CPU index higher than the GICState expects to be
+     * handling
+     */
+    g_assert(attrs.requester_type == MEMTXATTRS_CPU);
+    g_assert(attrs.requester_id < s->num_cpu);
+
+    return attrs.requester_id;
 }
 
-static inline int gic_get_current_vcpu(GICState *s)
+static inline int gic_get_current_vcpu(GICState *s, MemTxAttrs attrs)
 {
-    return gic_get_current_cpu(s) + GIC_NCPU;
+    return gic_get_current_cpu(s, attrs) + GIC_NCPU;
 }
 
 /* Return true if this GIC config has interrupt groups, which is
@@ -951,7 +956,7 @@ static uint32_t gic_dist_readb(void *opaque, hwaddr offset, MemTxAttrs attrs)
     int cm;
     int mask;
 
-    cpu = gic_get_current_cpu(s);
+    cpu = gic_get_current_cpu(s, attrs);
     cm = 1 << cpu;
     if (offset < 0x100) {
         if (offset == 0) {      /* GICD_CTLR */
@@ -1182,7 +1187,7 @@ static void gic_dist_writeb(void *opaque, hwaddr offset,
     int i;
     int cpu;
 
-    cpu = gic_get_current_cpu(s);
+    cpu = gic_get_current_cpu(s, attrs);
     if (offset < 0x100) {
         if (offset == 0) {
             if (s->security_extn && !attrs.secure) {
@@ -1476,7 +1481,7 @@ static void gic_dist_writel(void *opaque, hwaddr offset,
         int mask;
         int target_cpu;
 
-        cpu = gic_get_current_cpu(s);
+        cpu = gic_get_current_cpu(s, attrs);
         irq = value & 0xf;
         switch ((value >> 24) & 3) {
         case 0:
@@ -1780,7 +1785,7 @@ static MemTxResult gic_thiscpu_read(void *opaque, hwaddr addr, uint64_t *data,
                                     unsigned size, MemTxAttrs attrs)
 {
     GICState *s = (GICState *)opaque;
-    return gic_cpu_read(s, gic_get_current_cpu(s), addr, data, attrs);
+    return gic_cpu_read(s, gic_get_current_cpu(s, attrs), addr, data, attrs);
 }
 
 static MemTxResult gic_thiscpu_write(void *opaque, hwaddr addr,
@@ -1788,7 +1793,7 @@ static MemTxResult gic_thiscpu_write(void *opaque, hwaddr addr,
                                      MemTxAttrs attrs)
 {
     GICState *s = (GICState *)opaque;
-    return gic_cpu_write(s, gic_get_current_cpu(s), addr, value, attrs);
+    return gic_cpu_write(s, gic_get_current_cpu(s, attrs), addr, value, attrs);
 }
 
 /* Wrappers to read/write the GIC CPU interface for a specific CPU.
@@ -1818,7 +1823,7 @@ static MemTxResult gic_thisvcpu_read(void *opaque, hwaddr addr, uint64_t *data,
 {
     GICState *s = (GICState *)opaque;
 
-    return gic_cpu_read(s, gic_get_current_vcpu(s), addr, data, attrs);
+    return gic_cpu_read(s, gic_get_current_vcpu(s, attrs), addr, data, attrs);
 }
 
 static MemTxResult gic_thisvcpu_write(void *opaque, hwaddr addr,
@@ -1827,7 +1832,7 @@ static MemTxResult gic_thisvcpu_write(void *opaque, hwaddr addr,
 {
     GICState *s = (GICState *)opaque;
 
-    return gic_cpu_write(s, gic_get_current_vcpu(s), addr, value, attrs);
+    return gic_cpu_write(s, gic_get_current_vcpu(s, attrs), addr, value, attrs);
 }
 
 static uint32_t gic_compute_eisr(GICState *s, int cpu, int lr_start)
@@ -1860,7 +1865,7 @@ static uint32_t gic_compute_elrsr(GICState *s, int cpu, int lr_start)
 
 static void gic_vmcr_write(GICState *s, uint32_t value, MemTxAttrs attrs)
 {
-    int vcpu = gic_get_current_vcpu(s);
+    int vcpu = gic_get_current_vcpu(s, attrs);
     uint32_t ctlr;
     uint32_t abpr;
     uint32_t bpr;
@@ -1995,7 +2000,7 @@ static MemTxResult gic_thiscpu_hyp_read(void *opaque, hwaddr addr, uint64_t *dat
 {
     GICState *s = (GICState *)opaque;
 
-    return gic_hyp_read(s, gic_get_current_cpu(s), addr, data, attrs);
+    return gic_hyp_read(s, gic_get_current_cpu(s, attrs), addr, data, attrs);
 }
 
 static MemTxResult gic_thiscpu_hyp_write(void *opaque, hwaddr addr,
@@ -2004,7 +2009,7 @@ static MemTxResult gic_thiscpu_hyp_write(void *opaque, hwaddr addr,
 {
     GICState *s = (GICState *)opaque;
 
-    return gic_hyp_write(s, gic_get_current_cpu(s), addr, value, attrs);
+    return gic_hyp_write(s, gic_get_current_cpu(s, attrs), addr, value, attrs);
 }
 
 static MemTxResult gic_do_hyp_read(void *opaque, hwaddr addr, uint64_t *data,
Signature · 1 line
2.34.1
#17 lore raw

Re: [PATCH v2 05/11] hw/intc/gic: use MxTxAttrs to divine accessing CPU

Quoted 28 lines · Alex Bennée

Now that MxTxAttrs encodes a CPU we should use that to figure it out. This solves edge cases like accessing via gdbstub or qtest.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/124

---
v2
- update for new field
- bool asserts

---
 hw/intc/arm_gic.c | 39 ++++++++++++++++++++++-----------------
 1 file changed, 22 insertions(+), 17 deletions(-)
hw/intc/arm_gic.c +1 -2
index 492b2421ab..d907df3884 100644
@@ -56,17 +56,22 @@ static const uint8_t gic_id_gicv2[] = {
     0x04, 0x00, 0x00, 0x00, 0x90, 0xb4, 0x2b, 0x00, 0x0d, 0xf0, 0x05, 0xb1
 };
 
-static inline int gic_get_current_cpu(GICState *s)
+static inline int gic_get_current_cpu(GICState *s, MemTxAttrs attrs)
 {
-    if (!qtest_enabled() && s->num_cpu > 1) {

hw/intc/arm_gic.c static const uint8_t gic_id_gicv2[] = { · line 67

Quoted 10 lines
hw/intc/arm_gic.c +7 -3
-        return current_cpu->cpu_index;
-    }
-    return 0;
+    /*
+     * Something other than a CPU accessing the GIC would be a bug as
+     * would a CPU index higher than the GICState expects to be
+     * handling
+     */
+    g_assert(attrs.requester_type == MEMTXATTRS_CPU);
+    g_assert(attrs.requester_id < s->num_cpu);

Would it be a QEMU bug, or a guest code bug ? If it's possible for the guest to mis-program a DMA controller to do a read that goes through this function, we shouldn't assert. (Whether that can happen will depend on how the board/SoC code puts together the MemoryRegion hierarchy, I think.)

thanks
-- PMM

#18 lore raw

Re: [PATCH v2 05/11] hw/intc/gic: use MxTxAttrs to divine accessing CPU

Quoted 37 lines · Peter Maydell
Quoted 36 lines · Alex Bennée

Now that MxTxAttrs encodes a CPU we should use that to figure it out. This solves edge cases like accessing via gdbstub or qtest.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/124

---
v2
- update for new field
- bool asserts

---
 hw/intc/arm_gic.c | 39 ++++++++++++++++++++++-----------------
 1 file changed, 22 insertions(+), 17 deletions(-)
hw/intc/arm_gic.c +6 -5
index 492b2421ab..d907df3884 100644
@@ -56,17 +56,22 @@ static const uint8_t gic_id_gicv2[] = {
     0x04, 0x00, 0x00, 0x00, 0x90, 0xb4, 0x2b, 0x00, 0x0d, 0xf0, 0x05, 0xb1
 };
 
-static inline int gic_get_current_cpu(GICState *s)
+static inline int gic_get_current_cpu(GICState *s, MemTxAttrs attrs)
 {
-    if (!qtest_enabled() && s->num_cpu > 1) {
-        return current_cpu->cpu_index;
-    }
-    return 0;
+    /*
+     * Something other than a CPU accessing the GIC would be a bug as
+     * would a CPU index higher than the GICState expects to be
+     * handling
+     */
Quoted 8 lines

hw/intc/arm_gic.c static const uint8_t gic_id_gicv2[] = { · line 67

Quoted 2 lines
hw/intc/arm_gic.c +2 -0
+    g_assert(attrs.requester_type == MEMTXATTRS_CPU);
+    g_assert(attrs.requester_id < s->num_cpu);

Would it be a QEMU bug, or a guest code bug ? If it's possible for the guest to mis-program a DMA controller to do a read that goes through this function, we shouldn't assert. (Whether that can happen will depend on how the board/SoC code puts together the MemoryRegion hierarchy, I think.)

Most likely a QEMU bug - how would a DMA master even access the GIC?

Quoted 3 lines

thanks
-- PMM

Signature · 1 line
Alex Bennée
#19 lore raw

Re: [PATCH v2 05/11] hw/intc/gic: use MxTxAttrs to divine accessing CPU

Quoted 23 lines · Alex Bennée
Quoted 13 lines · Peter Maydell
Quoted 12 lines · Alex Bennée
hw/intc/arm_gic.c +6 -5
-static inline int gic_get_current_cpu(GICState *s)
+static inline int gic_get_current_cpu(GICState *s, MemTxAttrs attrs)
 {
-    if (!qtest_enabled() && s->num_cpu > 1) {
-        return current_cpu->cpu_index;
-    }
-    return 0;
+    /*
+     * Something other than a CPU accessing the GIC would be a bug as
+     * would a CPU index higher than the GICState expects to be
+     * handling
+     */
Quoted 8 lines

hw/intc/arm_gic.c static const uint8_t gic_id_gicv2[] = { · line 67

Quoted 2 lines
hw/intc/arm_gic.c +2 -0
+    g_assert(attrs.requester_type == MEMTXATTRS_CPU);
+    g_assert(attrs.requester_id < s->num_cpu);

Would it be a QEMU bug, or a guest code bug ? If it's possible for the guest to mis-program a DMA controller to do a read that goes through this function, we shouldn't assert. (Whether that can happen will depend on how the board/SoC code puts together the MemoryRegion hierarchy, I think.)

Quoted 1 line

Most likely a QEMU bug - how would a DMA master even access the GIC?

If it's mapped into the system address space, the same way as it does any memory access. For instance on the virt board we just map the distributor MemoryRegion straight into the system address space, and any DMA master can talk to it. This is of course not how the hardware really works (where the GIC is part of the CPU itself), but, as noted in previous threads, up-ending the MemoryRegion handling in order to be able to put the GIC only in the address space(s) that the CPU sees would be a lot of work, which is why we didn't try to solve the "how do you figure out which CPU is writing to the GIC" problem that way.

thanks
-- PMM

#20 lore raw

Re: [PATCH v2 05/11] hw/intc/gic: use MxTxAttrs to divine accessing CPU

Quoted 26 lines · Peter Maydell
Quoted 23 lines · Alex Bennée
Quoted 13 lines · Peter Maydell
Quoted 12 lines · Alex Bennée
hw/intc/arm_gic.c +6 -5
-static inline int gic_get_current_cpu(GICState *s)
+static inline int gic_get_current_cpu(GICState *s, MemTxAttrs attrs)
 {
-    if (!qtest_enabled() && s->num_cpu > 1) {
-        return current_cpu->cpu_index;
-    }
-    return 0;
+    /*
+     * Something other than a CPU accessing the GIC would be a bug as
+     * would a CPU index higher than the GICState expects to be
+     * handling
+     */
Quoted 8 lines

hw/intc/arm_gic.c static const uint8_t gic_id_gicv2[] = { · line 67

Quoted 2 lines
hw/intc/arm_gic.c +2 -0
+    g_assert(attrs.requester_type == MEMTXATTRS_CPU);
+    g_assert(attrs.requester_id < s->num_cpu);

Would it be a QEMU bug, or a guest code bug ? If it's possible for the guest to mis-program a DMA controller to do a read that goes through this function, we shouldn't assert. (Whether that can happen will depend on how the board/SoC code puts together the MemoryRegion hierarchy, I think.)

Quoted 1 line

Most likely a QEMU bug - how would a DMA master even access the GIC?

Quoted 11 lines

If it's mapped into the system address space, the same way as it does any memory access. For instance on the virt board we just map the distributor MemoryRegion straight into the system address space, and any DMA master can talk to it. This is of course not how the hardware really works (where the GIC is part of the CPU itself), but, as noted in previous threads, up-ending the MemoryRegion handling in order to be able to put the GIC only in the address space(s) that the CPU sees would be a lot of work, which is why we didn't try to solve the "how do you figure out which CPU is writing to the GIC" problem that way.

So hw_error?

I don't think there is a way we can safely continue unless we just want to fallback to "it was vCPU 0 what did it".

Quoted 3 lines

thanks
-- PMM

Signature · 1 line
Alex Bennée
#21 lore raw

Re: [PATCH v2 05/11] hw/intc/gic: use MxTxAttrs to divine accessing CPU

Quoted 42 lines · Alex Bennée
Quoted 26 lines · Peter Maydell
Quoted 23 lines · Alex Bennée
Quoted 13 lines · Peter Maydell
Quoted 12 lines · Alex Bennée
hw/intc/arm_gic.c +6 -5
-static inline int gic_get_current_cpu(GICState *s)
+static inline int gic_get_current_cpu(GICState *s, MemTxAttrs attrs)
 {
-    if (!qtest_enabled() && s->num_cpu > 1) {
-        return current_cpu->cpu_index;
-    }
-    return 0;
+    /*
+     * Something other than a CPU accessing the GIC would be a bug as
+     * would a CPU index higher than the GICState expects to be
+     * handling
+     */
Quoted 8 lines

hw/intc/arm_gic.c static const uint8_t gic_id_gicv2[] = { · line 67

Quoted 2 lines
hw/intc/arm_gic.c +2 -0
+    g_assert(attrs.requester_type == MEMTXATTRS_CPU);
+    g_assert(attrs.requester_id < s->num_cpu);

Would it be a QEMU bug, or a guest code bug ? If it's possible for the guest to mis-program a DMA controller to do a read that goes through this function, we shouldn't assert. (Whether that can happen will depend on how the board/SoC code puts together the MemoryRegion hierarchy, I think.)

Quoted 1 line

Most likely a QEMU bug - how would a DMA master even access the GIC?

Quoted 11 lines

If it's mapped into the system address space, the same way as it does any memory access. For instance on the virt board we just map the distributor MemoryRegion straight into the system address space, and any DMA master can talk to it. This is of course not how the hardware really works (where the GIC is part of the CPU itself), but, as noted in previous threads, up-ending the MemoryRegion handling in order to be able to put the GIC only in the address space(s) that the CPU sees would be a lot of work, which is why we didn't try to solve the "how do you figure out which CPU is writing to the GIC" problem that way.

Quoted 1 line

So hw_error?

That's just an assert by another name, and isn't any better.

Quoted 2 lines

I don't think there is a way we can safely continue unless we just want to fallback to "it was vCPU 0 what did it".

You can do that, or just make the whole memory transaction return 0, or return a suitable memtx error.

-- PMM

#22 lore raw

PATCH v2 6/11 hw/timer: convert mptimer access to attrs to derive cpu index

In reply to Alex Bennée

This removes the hacks to deal with empty current_cpu.

---
v2
- update for new fields
- bool asserts

---
 hw/timer/arm_mptimer.c | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)
hw/timer/arm_mptimer.c +14 -11
index cdfca3000b..813ba3b7e8 100644
@@ -41,9 +41,10 @@
  * which is used in both the ARM11MPCore and Cortex-A9MP.
  */
 
-static inline int get_current_cpu(ARMMPTimerState *s)
+static inline int get_current_cpu(ARMMPTimerState *s, MemTxAttrs attrs)
 {
-    int cpu_id = current_cpu ? current_cpu->cpu_index : 0;
+    int cpu_id = attrs.requester_id;
+    g_assert(attrs.requester_type == MEMTXATTRS_CPU);
 
     if (cpu_id >= s->num_cpu) {
         hw_error("arm_mptimer: num-cpu %d but this cpu is %d!\n",
@@ -178,25 +179,27 @@ static void timerblock_write(void *opaque, hwaddr addr,
 /* Wrapper functions to implement the "read timer/watchdog for
  * the current CPU" memory regions.
  */
-static uint64_t arm_thistimer_read(void *opaque, hwaddr addr,
-                                   unsigned size)
+static MemTxResult arm_thistimer_read(void *opaque, hwaddr addr, uint64_t *data,
+                                      unsigned size, MemTxAttrs attrs)
 {
     ARMMPTimerState *s = (ARMMPTimerState *)opaque;
-    int id = get_current_cpu(s);
-    return timerblock_read(&s->timerblock[id], addr, size);
+    int id = get_current_cpu(s, attrs);
+    *data = timerblock_read(&s->timerblock[id], addr, size);
+    return MEMTX_OK;
 }
 
-static void arm_thistimer_write(void *opaque, hwaddr addr,
-                                uint64_t value, unsigned size)
+static MemTxResult arm_thistimer_write(void *opaque, hwaddr addr,
+                                uint64_t value, unsigned size, MemTxAttrs attrs)
 {
     ARMMPTimerState *s = (ARMMPTimerState *)opaque;
-    int id = get_current_cpu(s);
+    int id = get_current_cpu(s, attrs);
     timerblock_write(&s->timerblock[id], addr, value, size);
+    return MEMTX_OK;
 }
 
 static const MemoryRegionOps arm_thistimer_ops = {
-    .read = arm_thistimer_read,
-    .write = arm_thistimer_write,
+    .read_with_attrs = arm_thistimer_read,
+    .write_with_attrs = arm_thistimer_write,
     .valid = {
         .min_access_size = 4,
         .max_access_size = 4,
Signature · 1 line
2.34.1
#23 lore raw

PATCH v2 7/11 configure: move detected gdb to TCG's config-host.mak

In reply to Alex Bennée

When tests/tcg gained it's own config-host.mak we forgot to move the GDB detection.

Fixes 544f4a2578 (tests/tcg: isolate from QEMU's config-host.mak)
---
 configure | 7 +++++++
 1 file changed, 7 insertions(+)
configure +7 -0
index 0bbf9d28af..fce677bd4a 100755
@@ -2481,6 +2481,8 @@ if test -n "$gdb_bin"; then
     gdb_version=$($gdb_bin --version | head -n 1)
     if version_ge ${gdb_version##* } 9.1; then
         echo "HAVE_GDB_BIN=$gdb_bin" >> $config_host_mak
+    else
+        gdb_bin=""
     fi
 fi
 
@@ -2565,6 +2567,11 @@ echo "# Automatically generated by configure - do not modify" > $config_host_mak
 echo "SRC_PATH=$source_path" >> $config_host_mak
 echo "HOST_CC=$host_cc" >> $config_host_mak
 
+# versioned checked in the main config_host.mak above
+if test -n "$gdb_bin"; then
+    echo "HAVE_GDB_BIN=$gdb_bin" >> $config_host_mak
+fi
+
 tcg_tests_targets=
 for target in $target_list; do
   arch=${target%%-*}
Signature · 1 line
2.34.1
#24 lore raw

PATCH v2 8/11 gdbstub: move into its own sub directory

In reply to Alex Bennée

This is in preparation of future refactoring as well as cleaning up the source tree. Aside from the minor tweaks to meson and trace.h this is pure code motion.

---
 meson.build                    |  4 +++-
 gdbstub/trace.h                |  1 +
 gdbstub.c => gdbstub/gdbstub.c |  2 +-
 MAINTAINERS                    |  2 +-
 gdbstub/meson.build            |  1 +
 gdbstub/trace-events           | 29 +++++++++++++++++++++++++++++
 trace-events                   | 28 ----------------------------
 7 files changed, 36 insertions(+), 31 deletions(-)
 create mode 100644 gdbstub/trace.h
 rename gdbstub.c => gdbstub/gdbstub.c (99%)
 create mode 100644 gdbstub/meson.build
 create mode 100644 gdbstub/trace-events
meson.build +3 -1
index 3885fc1076..2c9209c2b8 100644
@@ -2914,6 +2914,7 @@ trace_events_subdirs = [
   'qom',
   'monitor',
   'util',
+  'gdbstub',
 ]
 if have_linux_user
   trace_events_subdirs += [ 'linux-user' ]
@@ -3037,6 +3038,7 @@ subdir('authz')
 subdir('crypto')
 subdir('ui')
 subdir('hw')
+subdir('gdbstub')
 
 
 if enable_modules
@@ -3114,7 +3116,7 @@ common_ss.add(files('cpus-common.c'))
 subdir('softmmu')
 
 common_ss.add(capstone)
-specific_ss.add(files('cpu.c', 'disas.c', 'gdbstub.c'), capstone)
+specific_ss.add(files('cpu.c', 'disas.c'), capstone)
 
 # Work around a gcc bug/misfeature wherein constant propagation looks
 # through an alias:
gdbstub/trace.h new file +1 -0
new file mode 100644
index 0000000000..dee87b1238
@@ -0,0 +1 @@
+#include "trace/trace-gdbstub.h"
gdbstub/gdbstub.c renamed from gdbstub.c +1 -1
similarity index 99%
rename from gdbstub.c
rename to gdbstub/gdbstub.c
index cf869b10e3..7d8fe475b3 100644
@@ -29,7 +29,7 @@
 #include "qemu/ctype.h"
 #include "qemu/cutils.h"
 #include "qemu/module.h"
-#include "trace/trace-root.h"
+#include "trace.h"
 #include "exec/gdbstub.h"
 #ifdef CONFIG_USER_ONLY
 #include "qemu.h"
MAINTAINERS +1 -1
index 738c4eb647..82575b2486 100644
@@ -2670,7 +2670,7 @@ GDB stub
 M: Alex Bennée <[email protected]>
 R: Philippe Mathieu-Daudé <[email protected]>
 S: Maintained
-F: gdbstub*
+F: gdbstub/*
 F: include/exec/gdbstub.h
 F: gdb-xml/
 F: tests/tcg/multiarch/gdbstub/
gdbstub/meson.build new file +1 -0
new file mode 100644
index 0000000000..6d4ae2d03c
@@ -0,0 +1 @@
+specific_ss.add(files('gdbstub.c'))
gdbstub/trace-events new file +29 -0
new file mode 100644
index 0000000000..03f0c303bf
@@ -0,0 +1,29 @@
+# See docs/devel/tracing.rst for syntax documentation.
+
+# gdbstub.c
+gdbstub_op_start(const char *device) "Starting gdbstub using device %s"
+gdbstub_op_exiting(uint8_t code) "notifying exit with code=0x%02x"
+gdbstub_op_continue(void) "Continuing all CPUs"
+gdbstub_op_continue_cpu(int cpu_index) "Continuing CPU %d"
+gdbstub_op_stepping(int cpu_index) "Stepping CPU %d"
+gdbstub_op_extra_info(const char *info) "Thread extra info: %s"
+gdbstub_hit_watchpoint(const char *type, int cpu_gdb_index, uint64_t vaddr) "Watchpoint hit, type=\"%s\" cpu=%d, vaddr=0x%" PRIx64 ""
+gdbstub_hit_internal_error(void) "RUN_STATE_INTERNAL_ERROR"
+gdbstub_hit_break(void) "RUN_STATE_DEBUG"
+gdbstub_hit_paused(void) "RUN_STATE_PAUSED"
+gdbstub_hit_shutdown(void) "RUN_STATE_SHUTDOWN"
+gdbstub_hit_io_error(void) "RUN_STATE_IO_ERROR"
+gdbstub_hit_watchdog(void) "RUN_STATE_WATCHDOG"
+gdbstub_hit_unknown(int state) "Unknown run state=0x%x"
+gdbstub_io_reply(const char *message) "Sent: %s"
+gdbstub_io_binaryreply(size_t ofs, const char *line) "0x%04zx: %s"
+gdbstub_io_command(const char *command) "Received: %s"
+gdbstub_io_got_ack(void) "Got ACK"
+gdbstub_io_got_unexpected(uint8_t ch) "Got 0x%02x when expecting ACK/NACK"
+gdbstub_err_got_nack(void) "Got NACK, retransmitting"
+gdbstub_err_garbage(uint8_t ch) "received garbage between packets: 0x%02x"
+gdbstub_err_overrun(void) "command buffer overrun, dropping command"
+gdbstub_err_invalid_repeat(uint8_t ch) "got invalid RLE count: 0x%02x"
+gdbstub_err_invalid_rle(void) "got invalid RLE sequence"
+gdbstub_err_checksum_invalid(uint8_t ch) "got invalid command checksum digit: 0x%02x"
+gdbstub_err_checksum_incorrect(uint8_t expected, uint8_t got) "got command packet with incorrect checksum, expected=0x%02x, received=0x%02x"
trace-events +0 -28
index bc71006675..035f3d570d 100644
@@ -46,34 +46,6 @@ ram_block_discard_range(const char *rbname, void *hva, size_t length, bool need_
 memory_notdirty_write_access(uint64_t vaddr, uint64_t ram_addr, unsigned size) "0x%" PRIx64 " ram_addr 0x%" PRIx64 " size %u"
 memory_notdirty_set_dirty(uint64_t vaddr) "0x%" PRIx64
 
-# gdbstub.c
-gdbstub_op_start(const char *device) "Starting gdbstub using device %s"
-gdbstub_op_exiting(uint8_t code) "notifying exit with code=0x%02x"
-gdbstub_op_continue(void) "Continuing all CPUs"
-gdbstub_op_continue_cpu(int cpu_index) "Continuing CPU %d"
-gdbstub_op_stepping(int cpu_index) "Stepping CPU %d"
-gdbstub_op_extra_info(const char *info) "Thread extra info: %s"
-gdbstub_hit_watchpoint(const char *type, int cpu_gdb_index, uint64_t vaddr) "Watchpoint hit, type=\"%s\" cpu=%d, vaddr=0x%" PRIx64 ""
-gdbstub_hit_internal_error(void) "RUN_STATE_INTERNAL_ERROR"
-gdbstub_hit_break(void) "RUN_STATE_DEBUG"
-gdbstub_hit_paused(void) "RUN_STATE_PAUSED"
-gdbstub_hit_shutdown(void) "RUN_STATE_SHUTDOWN"
-gdbstub_hit_io_error(void) "RUN_STATE_IO_ERROR"
-gdbstub_hit_watchdog(void) "RUN_STATE_WATCHDOG"
-gdbstub_hit_unknown(int state) "Unknown run state=0x%x"
-gdbstub_io_reply(const char *message) "Sent: %s"
-gdbstub_io_binaryreply(size_t ofs, const char *line) "0x%04zx: %s"
-gdbstub_io_command(const char *command) "Received: %s"
-gdbstub_io_got_ack(void) "Got ACK"
-gdbstub_io_got_unexpected(uint8_t ch) "Got 0x%02x when expecting ACK/NACK"
-gdbstub_err_got_nack(void) "Got NACK, retransmitting"
-gdbstub_err_garbage(uint8_t ch) "received garbage between packets: 0x%02x"
-gdbstub_err_overrun(void) "command buffer overrun, dropping command"
-gdbstub_err_invalid_repeat(uint8_t ch) "got invalid RLE count: 0x%02x"
-gdbstub_err_invalid_rle(void) "got invalid RLE sequence"
-gdbstub_err_checksum_invalid(uint8_t ch) "got invalid command checksum digit: 0x%02x"
-gdbstub_err_checksum_incorrect(uint8_t expected, uint8_t got) "got command packet with incorrect checksum, expected=0x%02x, received=0x%02x"
-
 # job.c
 job_state_transition(void *job,  int ret, const char *legal, const char *s0, const char *s1) "job %p (ret: %d) attempting %s transition (%s-->%s)"
 job_apply_verb(void *job, const char *state, const char *verb, const char *legal) "job %p in state %s; applying verb %s (%s)"
Signature · 1 line
2.34.1
#25 lore raw

PATCH v2 9/11 gdbstub: move sstep flags probing into AccelClass

In reply to Alex Bennée

The support of single-stepping is very much dependent on support from the accelerator we are using. To avoid special casing in gdbstub move the probing out to an AccelClass function so future accelerators can put their code there.

---
 include/qemu/accel.h | 12 ++++++++++++
 include/sysemu/kvm.h |  8 --------
 accel/accel-common.c | 10 ++++++++++
 accel/kvm/kvm-all.c  | 14 +++++++++++++-
 accel/tcg/tcg-all.c  | 17 +++++++++++++++++
 gdbstub/gdbstub.c    | 22 ++++------------------
 6 files changed, 56 insertions(+), 27 deletions(-)
include/qemu/accel.h +12 -0
index be56da1b99..ce4747634a 100644
@@ -43,6 +43,10 @@ typedef struct AccelClass {
     bool (*has_memory)(MachineState *ms, AddressSpace *as,
                        hwaddr start_addr, hwaddr size);
 #endif
+
+    /* gdbstub related hooks */
+    int (*gdbstub_supported_sstep_flags)(void);
+
     bool *allowed;
     /*
      * Array of global properties that would be applied when specific
@@ -92,4 +96,12 @@ void accel_cpu_instance_init(CPUState *cpu);
  */
 bool accel_cpu_realizefn(CPUState *cpu, Error **errp);
 
+/**
+ * accel_supported_gdbstub_sstep_flags:
+ *
+ * Returns the supported single step modes for the configured
+ * accelerator.
+ */
+int accel_supported_gdbstub_sstep_flags(void);
+
 #endif /* QEMU_ACCEL_H */
include/sysemu/kvm.h +0 -8
index efd6dee818..a20ad51aad 100644
@@ -47,7 +47,6 @@ extern bool kvm_direct_msi_allowed;
 extern bool kvm_ioeventfd_any_length_allowed;
 extern bool kvm_msi_use_devid;
 extern bool kvm_has_guest_debug;
-extern int kvm_sstep_flags;
 
 #define kvm_enabled()           (kvm_allowed)
 /**
@@ -174,12 +173,6 @@ extern int kvm_sstep_flags;
  */
 #define kvm_supports_guest_debug() (kvm_has_guest_debug)
 
-/*
- * kvm_supported_sstep_flags
- * Returns: SSTEP_* flags that KVM supports for guest debug
- */
-#define kvm_get_supported_sstep_flags() (kvm_sstep_flags)
-
 #else
 
 #define kvm_enabled()           (0)
@@ -198,7 +191,6 @@ extern int kvm_sstep_flags;
 #define kvm_ioeventfd_any_length_enabled() (false)
 #define kvm_msi_devid_required() (false)
 #define kvm_supports_guest_debug() (false)
-#define kvm_get_supported_sstep_flags() (0)
 
 #endif  /* CONFIG_KVM_IS_POSSIBLE */
 
accel/accel-common.c +10 -0
index 50035bda55..df72cc989a 100644
@@ -129,6 +129,16 @@ bool accel_cpu_realizefn(CPUState *cpu, Error **errp)
     return true;
 }
 
+int accel_supported_gdbstub_sstep_flags(void)
+{
+    AccelState *accel = current_accel();
+    AccelClass *acc = ACCEL_GET_CLASS(accel);
+    if (acc->gdbstub_supported_sstep_flags) {
+        return acc->gdbstub_supported_sstep_flags();
+    }
+    return 0;
+}
+
 static const TypeInfo accel_cpu_type = {
     .name = TYPE_ACCEL_CPU,
     .parent = TYPE_OBJECT,
accel/kvm/kvm-all.c +13 -1
index 5acab1767f..c55938453a 100644
@@ -175,7 +175,7 @@ bool kvm_direct_msi_allowed;
 bool kvm_ioeventfd_any_length_allowed;
 bool kvm_msi_use_devid;
 bool kvm_has_guest_debug;
-int kvm_sstep_flags;
+static int kvm_sstep_flags;
 static bool kvm_immediate_exit;
 static hwaddr kvm_max_slot_size = ~0;
 
@@ -3712,6 +3712,17 @@ static void kvm_accel_instance_init(Object *obj)
     s->kvm_dirty_ring_size = 0;
 }
 
+/**
+ * kvm_gdbstub_sstep_flags():
+ *
+ * Returns: SSTEP_* flags that KVM supports for guest debug. The
+ * support is probed during kvm_init()
+ */
+static int kvm_gdbstub_sstep_flags(void)
+{
+    return kvm_sstep_flags;
+}
+
 static void kvm_accel_class_init(ObjectClass *oc, void *data)
 {
     AccelClass *ac = ACCEL_CLASS(oc);
@@ -3719,6 +3730,7 @@ static void kvm_accel_class_init(ObjectClass *oc, void *data)
     ac->init_machine = kvm_init;
     ac->has_memory = kvm_accel_has_memory;
     ac->allowed = &kvm_allowed;
+    ac->gdbstub_supported_sstep_flags = kvm_gdbstub_sstep_flags;
 
     object_class_property_add(oc, "kernel-irqchip", "on|off|split",
         NULL, kvm_set_kernel_irqchip,
accel/tcg/tcg-all.c +17 -0
index 47952eecd7..30b503fb22 100644
@@ -25,6 +25,7 @@
 
 #include "qemu/osdep.h"
 #include "sysemu/tcg.h"
+#include "sysemu/replay.h"
 #include "sysemu/cpu-timers.h"
 #include "tcg/tcg.h"
 #include "qapi/error.h"
@@ -207,12 +208,28 @@ static void tcg_set_splitwx(Object *obj, bool value, Error **errp)
     s->splitwx_enabled = value;
 }
 
+static int tcg_gdbstub_supported_sstep_flags(void)
+{
+    /*
+     * In replay mode all events will come from the log and can't be
+     * suppressed otherwise we would break determinism. However as those
+     * events are tied to the number of executed instructions we won't see
+     * them occurring every time we single step.
+     */
+    if (replay_mode != REPLAY_MODE_NONE) {
+        return SSTEP_ENABLE;
+    } else {
+        return SSTEP_ENABLE | SSTEP_NOIRQ | SSTEP_NOTIMER;
+    }
+}
+
 static void tcg_accel_class_init(ObjectClass *oc, void *data)
 {
     AccelClass *ac = ACCEL_CLASS(oc);
     ac->name = "tcg";
     ac->init_machine = tcg_init_machine;
     ac->allowed = &tcg_allowed;
+    ac->gdbstub_supported_sstep_flags = tcg_gdbstub_supported_sstep_flags;
 
     object_class_property_add_str(oc, "thread",
                                   tcg_get_thread,
gdbstub/gdbstub.c +4 -18
index 7d8fe475b3..a0755e6505 100644
@@ -383,27 +383,13 @@ static void init_gdbserver_state(void)
     gdbserver_state.last_packet = g_byte_array_sized_new(MAX_PACKET_LENGTH + 4);
 
     /*
-     * In replay mode all events will come from the log and can't be
-     * suppressed otherwise we would break determinism. However as those
-     * events are tied to the number of executed instructions we won't see
-     * them occurring every time we single step.
-     */
-    if (replay_mode != REPLAY_MODE_NONE) {
-        gdbserver_state.supported_sstep_flags = SSTEP_ENABLE;
-    } else if (kvm_enabled()) {
-        gdbserver_state.supported_sstep_flags = kvm_get_supported_sstep_flags();
-    } else {
-        gdbserver_state.supported_sstep_flags =
-            SSTEP_ENABLE | SSTEP_NOIRQ | SSTEP_NOTIMER;
-    }
-
-    /*
-     * By default use no IRQs and no timers while single stepping so as to
-     * make single stepping like an ICE HW step.
+     * What single-step modes are supported is accelerator dependent.
+     * By default try to use no IRQs and no timers while single
+     * stepping so as to make single stepping like a typical ICE HW step.
      */
+    gdbserver_state.supported_sstep_flags = accel_supported_gdbstub_sstep_flags();
     gdbserver_state.sstep_flags = SSTEP_ENABLE | SSTEP_NOIRQ | SSTEP_NOTIMER;
     gdbserver_state.sstep_flags &= gdbserver_state.supported_sstep_flags;
-
 }
 
 #ifndef CONFIG_USER_ONLY
Signature · 1 line
2.34.1
#26 lore raw

PATCH v2 10/11 gdbstub: move breakpoint logic to accel ops

In reply to Alex Bennée

As HW virtualization requires specific support to handle breakpoints lets push out special casing out of the core gdbstub code and into AccelOpsClass. This will make it easier to add other accelerator support and reduces some of the stub shenanigans.

---
 accel/kvm/kvm-cpus.h       |   3 +
 gdbstub/internals.h        |  16 +++++
 include/sysemu/accel-ops.h |   6 ++
 include/sysemu/cpus.h      |   3 +
 include/sysemu/kvm.h       |   5 --
 accel/kvm/kvm-accel-ops.c  |   8 +++
 accel/kvm/kvm-all.c        |  24 +------
 accel/stubs/kvm-stub.c     |  16 -----
 accel/tcg/tcg-accel-ops.c  |  92 +++++++++++++++++++++++++++
 gdbstub/gdbstub.c          | 127 +++----------------------------------
 gdbstub/softmmu.c          |  42 ++++++++++++
 gdbstub/user.c             |  62 ++++++++++++++++++
 softmmu/cpus.c             |   7 ++
 gdbstub/meson.build        |   8 +++
 14 files changed, 259 insertions(+), 160 deletions(-)
 create mode 100644 gdbstub/internals.h
 create mode 100644 gdbstub/softmmu.c
 create mode 100644 gdbstub/user.c
accel/kvm/kvm-cpus.h +3 -0
index bf0bd1bee4..33e435d62b 100644
@@ -18,5 +18,8 @@ void kvm_destroy_vcpu(CPUState *cpu);
 void kvm_cpu_synchronize_post_reset(CPUState *cpu);
 void kvm_cpu_synchronize_post_init(CPUState *cpu);
 void kvm_cpu_synchronize_pre_loadvm(CPUState *cpu);
+int kvm_insert_breakpoint(CPUState *cpu, int type, hwaddr addr, hwaddr len);
+int kvm_remove_breakpoint(CPUState *cpu, int type, hwaddr addr, hwaddr len);
+void kvm_remove_all_breakpoints(CPUState *cpu);
 
 #endif /* KVM_CPUS_H */
gdbstub/internals.h new file +16 -0
new file mode 100644
index 0000000000..41e2e72dbf
@@ -0,0 +1,16 @@
+/*
+ * gdbstub internals
+ *
+ * Copyright (c) 2022 Linaro Ltd
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef _INTERNALS_H_
+#define _INTERNALS_H_
+
+int gdb_breakpoint_insert(CPUState *cs, int type, hwaddr addr, hwaddr len);
+int gdb_breakpoint_remove(CPUState *cs, int type, hwaddr addr, hwaddr len);
+void gdb_breakpoint_remove_all(CPUState *cs);
+
+#endif /* _INTERNALS_H_ */
include/sysemu/accel-ops.h +6 -0
index a0572ea87a..86794ac273 100644
@@ -10,6 +10,7 @@
 #ifndef ACCEL_OPS_H
 #define ACCEL_OPS_H
 
+#include "exec/hwaddr.h"
 #include "qom/object.h"
 
 #define ACCEL_OPS_SUFFIX "-ops"
@@ -44,6 +45,11 @@ struct AccelOpsClass {
 
     int64_t (*get_virtual_clock)(void);
     int64_t (*get_elapsed_ticks)(void);
+
+    /* gdbstub hooks */
+    int (*insert_breakpoint)(CPUState *cpu, int type, hwaddr addr, hwaddr len);
+    int (*remove_breakpoint)(CPUState *cpu, int type, hwaddr addr, hwaddr len);
+    void (*remove_all_breakpoints)(CPUState *cpu);
 };
 
 #endif /* ACCEL_OPS_H */
include/sysemu/cpus.h +3 -0
index b5c87d48b3..1bace3379b 100644
@@ -7,6 +7,9 @@
 /* register accel-specific operations */
 void cpus_register_accel(const AccelOpsClass *i);
 
+/* return registers ops */
+const AccelOpsClass *cpus_get_accel(void);
+
 /* accel/dummy-cpus.c */
 
 /* Create a dummy vcpu for AccelOpsClass->create_vcpu_thread */
include/sysemu/kvm.h +0 -5
index a20ad51aad..21d3f1d01e 100644
@@ -254,11 +254,6 @@ int kvm_on_sigbus(int code, void *addr);
 
 void kvm_flush_coalesced_mmio_buffer(void);
 
-int kvm_insert_breakpoint(CPUState *cpu, target_ulong addr,
-                          target_ulong len, int type);
-int kvm_remove_breakpoint(CPUState *cpu, target_ulong addr,
-                          target_ulong len, int type);
-void kvm_remove_all_breakpoints(CPUState *cpu);
 int kvm_update_guest_debug(CPUState *cpu, unsigned long reinject_trap);
 
 /* internal API */
accel/kvm/kvm-accel-ops.c +8 -0
index c4244a23c6..5c0e37514c 100644
@@ -16,12 +16,14 @@
 #include "qemu/osdep.h"
 #include "qemu/error-report.h"
 #include "qemu/main-loop.h"
+#include "sysemu/kvm.h"
 #include "sysemu/kvm_int.h"
 #include "sysemu/runstate.h"
 #include "sysemu/cpus.h"
 #include "qemu/guest-random.h"
 #include "qapi/error.h"
 
+#include <linux/kvm.h>
 #include "kvm-cpus.h"
 
 static void *kvm_vcpu_thread_fn(void *arg)
@@ -95,6 +97,12 @@ static void kvm_accel_ops_class_init(ObjectClass *oc, void *data)
     ops->synchronize_post_init = kvm_cpu_synchronize_post_init;
     ops->synchronize_state = kvm_cpu_synchronize_state;
     ops->synchronize_pre_loadvm = kvm_cpu_synchronize_pre_loadvm;
+
+#ifdef KVM_CAP_SET_GUEST_DEBUG
+    ops->insert_breakpoint = kvm_insert_breakpoint;
+    ops->remove_breakpoint = kvm_remove_breakpoint;
+    ops->remove_all_breakpoints = kvm_remove_all_breakpoints;
+#endif
 }
 
 static const TypeInfo kvm_accel_ops_type = {
accel/kvm/kvm-all.c +3 -21
index c55938453a..b8c734fe3a 100644
@@ -3287,8 +3287,7 @@ int kvm_update_guest_debug(CPUState *cpu, unsigned long reinject_trap)
     return data.err;
 }
 
-int kvm_insert_breakpoint(CPUState *cpu, target_ulong addr,
-                          target_ulong len, int type)
+int kvm_insert_breakpoint(CPUState *cpu, int type, hwaddr addr, hwaddr len)
 {
     struct kvm_sw_breakpoint *bp;
     int err;
@@ -3326,8 +3325,7 @@ int kvm_insert_breakpoint(CPUState *cpu, target_ulong addr,
     return 0;
 }
 
-int kvm_remove_breakpoint(CPUState *cpu, target_ulong addr,
-                          target_ulong len, int type)
+int kvm_remove_breakpoint(CPUState *cpu, int type, hwaddr addr, hwaddr len)
 {
     struct kvm_sw_breakpoint *bp;
     int err;
@@ -3393,26 +3391,10 @@ void kvm_remove_all_breakpoints(CPUState *cpu)
 
 #else /* !KVM_CAP_SET_GUEST_DEBUG */
 
-int kvm_update_guest_debug(CPUState *cpu, unsigned long reinject_trap)
+static int kvm_update_guest_debug(CPUState *cpu, unsigned long reinject_trap)
 {
     return -EINVAL;
 }
-
-int kvm_insert_breakpoint(CPUState *cpu, target_ulong addr,
-                          target_ulong len, int type)
-{
-    return -EINVAL;
-}
-
-int kvm_remove_breakpoint(CPUState *cpu, target_ulong addr,
-                          target_ulong len, int type)
-{
-    return -EINVAL;
-}
-
-void kvm_remove_all_breakpoints(CPUState *cpu)
-{
-}
 #endif /* !KVM_CAP_SET_GUEST_DEBUG */
 
 static int kvm_set_signal_mask(CPUState *cpu, const sigset_t *sigset)
accel/stubs/kvm-stub.c +0 -16
index 2ac5f9c036..2d79333143 100644
@@ -51,22 +51,6 @@ int kvm_update_guest_debug(CPUState *cpu, unsigned long reinject_trap)
     return -ENOSYS;
 }
 
-int kvm_insert_breakpoint(CPUState *cpu, target_ulong addr,
-                          target_ulong len, int type)
-{
-    return -EINVAL;
-}
-
-int kvm_remove_breakpoint(CPUState *cpu, target_ulong addr,
-                          target_ulong len, int type)
-{
-    return -EINVAL;
-}
-
-void kvm_remove_all_breakpoints(CPUState *cpu)
-{
-}
-
 int kvm_on_sigbus_vcpu(CPUState *cpu, int code, void *addr)
 {
     return 1;
accel/tcg/tcg-accel-ops.c +92 -0

110 lines not on this page · raw message

gdbstub/gdbstub.c +9 -118

168 lines not on this page · raw message

gdbstub/softmmu.c new file +42 -0

42 lines not on this page · raw message

gdbstub/user.c new file +62 -0

62 lines not on this page · raw message

softmmu/cpus.c +7 -0
index 23b30484b2..61b27ff59d 100644
@@ -617,6 +617,13 @@ void cpus_register_accel(const AccelOpsClass *ops)
     cpus_accel = ops;
 }
 
+const AccelOpsClass *cpus_get_accel(void)
+{
+    /* broken if we call this early */
+    assert(cpus_accel);
+    return cpus_accel;
+}
+
 void qemu_init_vcpu(CPUState *cpu)
 {
     MachineState *ms = MACHINE(qdev_get_machine());
gdbstub/meson.build +8 -0
index 6d4ae2d03c..fc895a2c39 100644
@@ -1 +1,9 @@
+#
+# The main gdbstub still relies on per-build definitions of various
+# types. The bits pushed to softmmu/user.c try to use guest agnostic
+# types such as hwaddr.
+#
+
 specific_ss.add(files('gdbstub.c'))
+softmmu_ss.add(files('softmmu.c'))
+user_ss.add(files('user.c'))
Signature · 1 line
2.34.1
#27 lore raw

PATCH v2 11/11 gdbstub: move guest debug support check to ops

In reply to Alex Bennée

This removes the final hard coding of kvm_enabled() in gdbstub and moves the check to an AccelOps.

---
 accel/kvm/kvm-cpus.h       | 1 +
 gdbstub/internals.h        | 1 +
 include/sysemu/accel-ops.h | 1 +
 include/sysemu/kvm.h       | 7 -------
 accel/kvm/kvm-accel-ops.c  | 1 +
 accel/kvm/kvm-all.c        | 6 ++++++
 accel/tcg/tcg-accel-ops.c  | 6 ++++++
 gdbstub/gdbstub.c          | 5 ++---
 gdbstub/softmmu.c          | 9 +++++++++
 gdbstub/user.c             | 6 ++++++
 10 files changed, 33 insertions(+), 10 deletions(-)
accel/kvm/kvm-cpus.h +1 -0
index 33e435d62b..fd63fe6a59 100644
@@ -18,6 +18,7 @@ void kvm_destroy_vcpu(CPUState *cpu);
 void kvm_cpu_synchronize_post_reset(CPUState *cpu);
 void kvm_cpu_synchronize_post_init(CPUState *cpu);
 void kvm_cpu_synchronize_pre_loadvm(CPUState *cpu);
+bool kvm_supports_guest_debug(void);
 int kvm_insert_breakpoint(CPUState *cpu, int type, hwaddr addr, hwaddr len);
 int kvm_remove_breakpoint(CPUState *cpu, int type, hwaddr addr, hwaddr len);
 void kvm_remove_all_breakpoints(CPUState *cpu);
gdbstub/internals.h +1 -0
index 41e2e72dbf..eabb0341d1 100644
@@ -9,6 +9,7 @@
 #ifndef _INTERNALS_H_
 #define _INTERNALS_H_
 
+bool gdb_supports_guest_debug(void);
 int gdb_breakpoint_insert(CPUState *cs, int type, hwaddr addr, hwaddr len);
 int gdb_breakpoint_remove(CPUState *cs, int type, hwaddr addr, hwaddr len);
 void gdb_breakpoint_remove_all(CPUState *cs);
include/sysemu/accel-ops.h +1 -0
index 86794ac273..8cc7996def 100644
@@ -47,6 +47,7 @@ struct AccelOpsClass {
     int64_t (*get_elapsed_ticks)(void);
 
     /* gdbstub hooks */
+    bool (*supports_guest_debug)(void);
     int (*insert_breakpoint)(CPUState *cpu, int type, hwaddr addr, hwaddr len);
     int (*remove_breakpoint)(CPUState *cpu, int type, hwaddr addr, hwaddr len);
     void (*remove_all_breakpoints)(CPUState *cpu);
include/sysemu/kvm.h +0 -7
index 21d3f1d01e..6e1bd01725 100644
@@ -46,7 +46,6 @@ extern bool kvm_readonly_mem_allowed;
 extern bool kvm_direct_msi_allowed;
 extern bool kvm_ioeventfd_any_length_allowed;
 extern bool kvm_msi_use_devid;
-extern bool kvm_has_guest_debug;
 
 #define kvm_enabled()           (kvm_allowed)
 /**
@@ -168,11 +167,6 @@ extern bool kvm_has_guest_debug;
  */
 #define kvm_msi_devid_required() (kvm_msi_use_devid)
 
-/*
- * Does KVM support guest debugging
- */
-#define kvm_supports_guest_debug() (kvm_has_guest_debug)
-
 #else
 
 #define kvm_enabled()           (0)
@@ -190,7 +184,6 @@ extern bool kvm_has_guest_debug;
 #define kvm_direct_msi_enabled() (false)
 #define kvm_ioeventfd_any_length_enabled() (false)
 #define kvm_msi_devid_required() (false)
-#define kvm_supports_guest_debug() (false)
 
 #endif  /* CONFIG_KVM_IS_POSSIBLE */
 
accel/kvm/kvm-accel-ops.c +1 -0
index 5c0e37514c..fbf4fe3497 100644
@@ -99,6 +99,7 @@ static void kvm_accel_ops_class_init(ObjectClass *oc, void *data)
     ops->synchronize_pre_loadvm = kvm_cpu_synchronize_pre_loadvm;
 
 #ifdef KVM_CAP_SET_GUEST_DEBUG
+    ops->supports_guest_debug = kvm_supports_guest_debug;
     ops->insert_breakpoint = kvm_insert_breakpoint;
     ops->remove_breakpoint = kvm_remove_breakpoint;
     ops->remove_all_breakpoints = kvm_remove_all_breakpoints;
accel/kvm/kvm-all.c +6 -0
index b8c734fe3a..6ebff6e5a6 100644
@@ -3287,6 +3287,12 @@ int kvm_update_guest_debug(CPUState *cpu, unsigned long reinject_trap)
     return data.err;
 }
 
+bool kvm_supports_guest_debug(void)
+{
+    /* probed during kvm_init() */
+    return kvm_has_guest_debug;
+}
+
 int kvm_insert_breakpoint(CPUState *cpu, int type, hwaddr addr, hwaddr len)
 {
     struct kvm_sw_breakpoint *bp;
accel/tcg/tcg-accel-ops.c +6 -0
index 965c2ad581..19cbf1db3a 100644
@@ -93,6 +93,11 @@ void tcg_handle_interrupt(CPUState *cpu, int mask)
     }
 }
 
+static bool tcg_supports_guest_debug(void)
+{
+    return true;
+}
+
 /* Translate GDB watchpoint type to a flags value for cpu_watchpoint_* */
 static inline int xlat_gdb_type(CPUState *cpu, int gdbtype)
 {
@@ -198,6 +203,7 @@ static void tcg_accel_ops_init(AccelOpsClass *ops)
         }
     }
 
+    ops->supports_guest_debug = tcg_supports_guest_debug;
     ops->insert_breakpoint = tcg_insert_breakpoint;
     ops->remove_breakpoint = tcg_remove_breakpoint;
     ops->remove_all_breakpoints = tcg_remove_all_breakpoints;
gdbstub/gdbstub.c +2 -3
index ff9f3f9586..be88ca0d71 100644
@@ -45,7 +45,6 @@
 
 #include "qemu/sockets.h"
 #include "sysemu/hw_accel.h"
-#include "sysemu/kvm.h"
 #include "sysemu/runstate.h"
 #include "semihosting/semihost.h"
 #include "exec/exec-all.h"
@@ -3447,8 +3446,8 @@ int gdbserver_start(const char *device)
         return -1;
     }
 
-    if (kvm_enabled() && !kvm_supports_guest_debug()) {
-        error_report("gdbstub: KVM doesn't support guest debugging");
+    if (!gdb_supports_guest_debug()) {
+        error_report("gdbstub: current accelerator doesn't support guest debugging");
         return -1;
     }
 
gdbstub/softmmu.c +9 -0
index 4e73890379..f208c6cf15 100644
@@ -15,6 +15,15 @@
 #include "sysemu/cpus.h"
 #include "internals.h"
 
+bool gdb_supports_guest_debug(void)
+{
+    const AccelOpsClass *ops = cpus_get_accel();
+    if (ops->supports_guest_debug) {
+        return ops->supports_guest_debug();
+    }
+    return false;
+}
+
 int gdb_breakpoint_insert(CPUState *cs, int type, hwaddr addr, hwaddr len)
 {
     const AccelOpsClass *ops = cpus_get_accel();
gdbstub/user.c +6 -0
index 42652b28a7..033e5fdd71 100644
@@ -14,6 +14,12 @@
 #include "hw/core/cpu.h"
 #include "internals.h"
 
+bool gdb_supports_guest_debug(void)
+{
+    /* user-mode == TCG == supported */
+    return true;
+}
+
 int gdb_breakpoint_insert(CPUState *cs, int type, hwaddr addr, hwaddr len)
 {
     CPUState *cpu;
Signature · 1 line
2.34.1