All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Lluís Vilanova" <vilanova@ac.upc.edu>
To: qemu-devel@nongnu.org
Cc: "Markus Armbruster" <armbru@redhat.com>,
	"Eric Blake" <eblake@redhat.com>,
	"Emilio G. Cota" <cota@braap.org>,
	"Stefan Hajnoczi" <stefanha@redhat.com>,
	"Lluís Vilanova" <vilanova@ac.upc.edu>
Subject: [Qemu-devel] [PATCH v5 13/22] instrument: Support synchronous modification of vCPU state
Date: Wed, 13 Sep 2017 00:54:12 +0300	[thread overview]
Message-ID: <150525325219.15988.2418594999948440514.stgit@frigg.lan> (raw)
In-Reply-To: <150525010239.15988.8172586618197849619.stgit@frigg.lan>

Stops all vCPUs to allow performing management operations like TB
invalidations. These are later necessary to ensure translated code does not
reference unloaded instrumentation libraries.

Signed-off-by: Lluís Vilanova <vilanova@ac.upc.edu>
---
 instrument/control.c     |   66 ++++++++++++++++++++++++++++++++++++++++++++++
 instrument/control.h     |   26 ++++++++++++++++++
 instrument/control.inc.h |   11 ++++++++
 3 files changed, 103 insertions(+)

diff --git a/instrument/control.c b/instrument/control.c
index c4b3ca0440..20ddffdc28 100644
--- a/instrument/control.c
+++ b/instrument/control.c
@@ -13,6 +13,7 @@
 #include "instrument/load.h"
 #include "instrument/qemu-instr/control.h"
 #include "qemu/compiler.h"
+#include "qemu/main-loop.h"
 #include "qom/cpu.h"
 
 
@@ -40,6 +41,71 @@ void instr_cpu_remove(CPUState *vcpu)
 }
 
 
+static void instr_cpu_stop_all__cb(CPUState *cpu, run_on_cpu_data data)
+{
+    InstrCPUStop *info = data.host_ptr;
+    /* run posted function */
+    if (info->fun) {
+        info->fun(cpu, info->data);
+    }
+#if !defined(CONFIG_USER_ONLY)
+    /* signal we're out of the main vCPU loop */
+    unsigned int count = atomic_load_acquire(&info->count);
+    atomic_store_release(&info->count, count + 1);
+    atomic_store_release(&info->stopped, true);
+    /* wait until we're good to go again */
+    qemu_cond_wait(&info->cond, &info->mutex);
+    count = atomic_load_acquire(&info->count);
+    atomic_store_release(&info->count, count - 1);
+    qemu_mutex_unlock(&info->mutex);
+#endif
+}
+
+void instr_cpu_stop_all_begin(InstrCPUStop *info,
+                              instr_cpu_stop_fun fun, void *data)
+{
+    CPUState *cpu;
+
+    info->fun = fun;
+    info->data = data;
+
+#if !defined(CONFIG_USER_ONLY)
+    info->count = 0;
+    qemu_cond_init(&info->cond);
+    qemu_mutex_init(&info->mutex);
+
+    /* main dispatch loop and run_on_cpu() lock the BQL */
+    qemu_mutex_unlock_iothread();
+#endif
+
+    CPU_FOREACH(cpu) {
+#if !defined(CONFIG_USER_ONLY)
+        atomic_store_release(&info->stopped, false);
+        qemu_mutex_lock(&info->mutex);
+        async_run_on_cpu(cpu, instr_cpu_stop_all__cb, RUN_ON_CPU_HOST_PTR(info));
+        while (!atomic_load_acquire(&info->stopped)) {
+            /* wait for vCPU to signal it's stopped */
+        }
+#else
+        instr_cpu_stop_all__cb(cpu, RUN_ON_CPU_HOST_PTR(info));
+#endif
+    }
+}
+
+void instr_cpu_stop_all_end(InstrCPUStop *info)
+{
+#if !defined(CONFIG_USER_ONLY)
+    qemu_cond_broadcast(&info->cond);
+    while (atomic_load_acquire(&info->count)) {
+        /* wait for all vCPUs to continue before we can destroy info */
+    }
+    qemu_cond_destroy(&info->cond);
+    qemu_mutex_destroy(&info->mutex);
+    qemu_mutex_lock_iothread();
+#endif
+}
+
+
 qi_fini_fn instr_event__fini_fn;
 void *instr_event__fini_data;
 
diff --git a/instrument/control.h b/instrument/control.h
index 57cea07fa7..03e87b2b8f 100644
--- a/instrument/control.h
+++ b/instrument/control.h
@@ -46,6 +46,32 @@ static inline QICPU instr_cpu_to_qicpu(CPUState *vcpu);
  */
 static inline CPUState *instr_cpu_from_qicpu(QICPU vcpu);
 
+typedef struct InstrCPUStop InstrCPUStop;
+typedef void (*instr_cpu_stop_fun)(CPUState *cpu, void *data);
+
+/**
+ * instr_cpu_stop_all_begin:
+ * @info: Opaque structure describing the operation.
+ * @fun: Function to run on the context of each vCPU once stopped.
+ * @data: Pointer to pass to @fun.
+ *
+ * Ensure all vCPUs stop executing guest code, and execute @fun on their context
+ * in turn. Returns with all vCPUs still stopped.
+ *
+ * Assumes cpu_list_lock() and that the QBL is locked before calling.
+ */
+void instr_cpu_stop_all_begin(InstrCPUStop *info,
+                              instr_cpu_stop_fun fun, void *data);
+
+/**
+ * instr_cpu_stop_all_end:
+ * @info: Opaque structure passed to a previous instr_cpu_stop_all_begin()
+ *     call.
+ *
+ * Resume execution on all vCPUs stopped by instr_cpu_stop_all_begin().
+ */
+void instr_cpu_stop_all_end(InstrCPUStop *info);
+
 
 /**
  * InstrState:
diff --git a/instrument/control.inc.h b/instrument/control.inc.h
index 45daae7d1d..6d65b23ead 100644
--- a/instrument/control.inc.h
+++ b/instrument/control.inc.h
@@ -15,6 +15,17 @@
 #include <stdint.h>
 
 
+struct InstrCPUStop {
+    instr_cpu_stop_fun fun;
+    void *data;
+#if !defined(CONFIG_USER_ONLY)
+    bool stopped;
+    unsigned int count;
+    QemuCond cond;
+    QemuMutex mutex;
+#endif
+};
+
 extern unsigned int instr_cpus_count;
 extern CPUState **instr_cpus;
 

  parent reply	other threads:[~2017-09-12 21:54 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-09-12 21:01 [Qemu-devel] [PATCH v5 00/22] instrument: Add basic event instrumentation Lluís Vilanova
2017-09-12 21:05 ` [Qemu-devel] [PATCH v5 01/22] instrument: Add documentation Lluís Vilanova
2017-09-12 21:09 ` [Qemu-devel] [PATCH v5 02/22] instrument: Add configure-time flag Lluís Vilanova
2017-09-12 21:13 ` [Qemu-devel] [PATCH v5 03/22] instrument: Add generic library loader Lluís Vilanova
2017-09-12 21:17 ` [Qemu-devel] [PATCH v5 04/22] instrument: [linux-user] Add command line " Lluís Vilanova
2017-09-12 21:21 ` [Qemu-devel] [PATCH v5 05/22] instrument: [bsd-user] " Lluís Vilanova
2017-09-12 21:25 ` [Qemu-devel] [PATCH v5 06/22] instrument: [softmmu] " Lluís Vilanova
2017-09-12 21:29 ` [Qemu-devel] [PATCH v5 07/22] instrument: [qapi] Add " Lluís Vilanova
2017-09-12 21:34 ` [Qemu-devel] [PATCH v5 08/22] instrument: [hmp] " Lluís Vilanova
2017-09-12 21:38 ` [Qemu-devel] [PATCH v5 09/22] instrument: Add basic control interface Lluís Vilanova
2017-09-12 21:42 ` [Qemu-devel] [PATCH v5 10/22] instrument: Add support for tracing events Lluís Vilanova
2017-09-12 21:46 ` [Qemu-devel] [PATCH v5 11/22] instrument: Track vCPUs Lluís Vilanova
2017-09-12 21:50 ` [Qemu-devel] [PATCH v5 12/22] instrument: Add event 'guest_cpu_enter' Lluís Vilanova
2017-09-12 21:54 ` Lluís Vilanova [this message]
2017-09-12 21:58 ` [Qemu-devel] [PATCH v5 14/22] exec: Add function to synchronously flush TB on a stopped vCPU Lluís Vilanova
2017-09-12 22:02 ` [Qemu-devel] [PATCH v5 15/22] instrument: Add event 'guest_cpu_exit' Lluís Vilanova
2017-09-12 22:06 ` [Qemu-devel] [PATCH v5 16/22] instrument: Add event 'guest_cpu_reset' Lluís Vilanova
2017-09-12 22:10 ` [Qemu-devel] [PATCH v5 17/22] trace: Introduce a proper structure to describe memory accesses Lluís Vilanova
2017-09-12 22:14 ` [Qemu-devel] [PATCH v5 18/22] instrument: Add event 'guest_mem_before_trans' Lluís Vilanova
2017-09-12 22:18 ` [Qemu-devel] [PATCH v5 19/22] instrument: Add event 'guest_mem_before_exec' Lluís Vilanova
2017-09-12 22:22 ` [Qemu-devel] [PATCH v5 20/22] instrument: Add event 'guest_user_syscall' Lluís Vilanova
2017-09-12 22:26 ` [Qemu-devel] [PATCH v5 21/22] instrument: Add event 'guest_user_syscall_ret' Lluís Vilanova
2017-09-12 22:30 ` [Qemu-devel] [PATCH v5 22/22] instrument: Add API to manipulate guest memory Lluís Vilanova
2017-09-12 22:34 ` [Qemu-devel] [PATCH v5 00/22] instrument: Add basic event instrumentation no-reply
2017-09-13  9:45   ` Lluís Vilanova
2017-09-12 22:36 ` no-reply
2017-09-13  9:50   ` Lluís Vilanova
2017-09-14 14:54 ` Peter Maydell
2017-09-15 13:45   ` Lluís Vilanova
2017-09-15 13:49     ` Peter Maydell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=150525325219.15988.2418594999948440514.stgit@frigg.lan \
    --to=vilanova@ac.upc.edu \
    --cc=armbru@redhat.com \
    --cc=cota@braap.org \
    --cc=eblake@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.