All of lore.kernel.org
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH v7 00/38] Preliminary libqtest cleanups
@ 2017-09-11 17:19 Eric Blake
  2017-09-11 17:19 ` [Qemu-devel] [PATCH v7 01/38] test-qga: Kill broken and dead QGA_TEST_SIDE_EFFECTING code Eric Blake
                   ` (37 more replies)
  0 siblings, 38 replies; 75+ messages in thread
From: Eric Blake @ 2017-09-11 17:19 UTC (permalink / raw)
  To: qemu-devel; +Cc: armbru, pbonzini, thuth

I'm trying to get rid of some of the duplication in libqtest.h
before tackling the project of making qmp() easier to use. v6
got some positive review on the direction of making dependence
on global_qtest explicit (compared to v5 where I went the
opposite direction and tried to use global_qtest even more than
it was already being abused for), so most of the changes in
this posting are related to additional cleanups or addressing
good review comments.

Still to come: I still have work pending on overhauling the
qmp() and qtest_qmp() code, using naming that makes more
sense, as well as getting us to the point that we can reliably
use -Wformat checking on qmp().

Available as a tag at:
git fetch git://repo.or.cz/qemu/ericb.git qapi-qmp-cleanup-v7

Since v6 (https://lists.gnu.org/archive/html/qemu-devel/2017-09/msg00308.html):
- Add review tags where appropriate
- merge a couple of patches [Thomas]
- split ahci into multiple patches [Thomas]
- add qtest_[v]startf rather than overloading (and churn) on
qtest_start [Thomas]
- catch more depedencies on global_qtest under tests/libqos/
- lots more patches added to make use of global_qtest obvious
- drop .gitignore changes for a later series

001/38:[----] [--] 'test-qga: Kill broken and dead QGA_TEST_SIDE_EFFECTING code'
002/38:[----] [--] 'qtest: Don't perform side effects inside assertion'
003/38:[----] [--] 'numa-test: Use hmp()'
004/38:[----] [--] 'tests: Clean up wait for event'
005/38:[----] [--] 'libqtest: Remove dead qtest_instances variable'
006/38:[----] [--] 'libqtest: Use qemu_strtoul()'
007/38:[0020] [FC] 'libqtest: Inline qtest_query_target_endianness()'
008/38:[0019] [FC] 'libqos: Track QTestState with QPCIBus'
009/38:[down] 'libqos: Track QTestState with QVirtioBus'
010/38:[down] 'libqos: Move/rename qpci_unplug_acpi_device_test() to pci.c'
011/38:[0045] [FC] 'libqos: Use explicit QTestState for pci operations'
012/38:[0179] [FC] 'libqos: Use explicit QTestState for virtio operations'
013/38:[0011] [FC] 'libqos: Use explicit QTestState for fw_cfg operations'
014/38:[0015] [FC] 'libqos: Use explicit QTestState for rtas operations'
015/38:[down] 'libqos: Use explicit QTestState for i2c operations'
016/38:[down] 'libqos: Use explicit QTestState for ahci operations'
017/38:[down] 'libqos: Use explicit QTestState for remaining libqos operations'
018/38:[0079] [FC] 'ahci-test: Drop dependence on global_qtest'
019/38:[0012] [FC] 'ivshmem-test: Drop dependence on global_qtest'
020/38:[----] [--] 'postcopy-test: Drop dependence on global_qtest'
021/38:[----] [--] 'vhost-user-test: Drop dependence on global_qtest'
022/38:[----] [-C] 'qmp-test: Drop dependence on global_qtest'
023/38:[----] [--] 'tests/boot-sector: Drop dependence on global_qtest'
024/38:[0079] [FC] 'tests/acpi-utils: Drop dependence on global_qtest'
025/38:[----] [--] 'wdt_ib700-test: Drop dependence on global_qtest'
026/38:[down] 'libqtest: Merge qtest_end() into qtest_quit()'
027/38:[down] 'libqtest: Swap order of qtest_init() and qtest_start()'
028/38:[down] 'libqtest: Add qtest_[v]startf()'
029/38:[down] 'libqtest: Merge qtest_init() into qtest_start()'
030/38:[0008] [FC] 'qtest: Avoid passing raw strings through hmp()'
031/38:[down] 'libqtest: Merge qtest_clock_*() with clock_*()'
032/38:[down] 'libqtest: Merge qtest_irq*() with irq*()'
033/38:[down] 'libqtest: Merge qtest_{in,out}[bwl]() with {in,out}[bwl]()'
034/38:[down] 'libqtest: Merge qtest_{read,write}[bwlq]() with {read,write}[bwlq]()'
035/38:[down] 'libqtest: Merge qtest_{mem,buf}{read,write}() with {mem,buf}{read,write}()'
036/38:[down] 'libqtest: Merge qtest_memset() with qmemset()'
037/38:[0088] [FC] 'libqtest: Separate qmp_discard_response() from command'
038/38:[down] 'libqtest: Merge qtest_hmp() with hmp()'

Eric Blake (37):
  test-qga: Kill broken and dead QGA_TEST_SIDE_EFFECTING code
  qtest: Don't perform side effects inside assertion
  numa-test: Use hmp()
  libqtest: Remove dead qtest_instances variable
  libqtest: Use qemu_strtoul()
  libqtest: Inline qtest_query_target_endianness()
  libqos: Track QTestState with QPCIBus
  libqos: Track QTestState with QVirtioBus
  libqos: Move/rename qpci_unplug_acpi_device_test() to pci.c
  libqos: Use explicit QTestState for pci operations
  libqos: Use explicit QTestState for virtio operations
  libqos: Use explicit QTestState for fw_cfg operations
  libqos: Use explicit QTestState for rtas operations
  libqos: Use explicit QTestState for i2c operations
  libqos: Use explicit QTestState for ahci operations
  libqos: Use explicit QTestState for remaining libqos operations
  ahci-test: Drop dependence on global_qtest
  ivshmem-test: Drop dependence on global_qtest
  postcopy-test: Drop dependence on global_qtest
  vhost-user-test: Drop dependence on global_qtest
  qmp-test: Drop dependence on global_qtest
  tests/boot-sector: Drop dependence on global_qtest
  tests/acpi-utils: Drop dependence on global_qtest
  wdt_ib700-test: Drop dependence on global_qtest
  libqtest: Merge qtest_end() into qtest_quit()
  libqtest: Swap order of qtest_init() and qtest_start()
  libqtest: Add qtest_[v]startf()
  libqtest: Merge qtest_init() into qtest_start()
  qtest: Avoid passing raw strings through hmp()
  libqtest: Merge qtest_clock_*() with clock_*()
  libqtest: Merge qtest_irq*() with irq*()
  libqtest: Merge qtest_{in,out}[bwl]() with {in,out}[bwl]()
  libqtest: Merge qtest_{read,write}[bwlq]() with {read,write}[bwlq]()
  libqtest: Merge qtest_{mem,buf}{read,write}() with
    {mem,buf}{read,write}()
  libqtest: Merge qtest_memset() with qmemset()
  libqtest: Separate qmp_discard_response() from command
  libqtest: Merge qtest_hmp() with hmp()

Markus Armbruster (1):
  tests: Clean up wait for event

 tests/libqtest.h               | 522 +++++++----------------------------------
 tests/acpi-utils.h             |  67 +++---
 tests/boot-sector.h            |   6 +-
 tests/libqos/ahci.h            |   2 +-
 tests/libqos/fw_cfg.h          |  10 +-
 tests/libqos/i2c.h             |   7 +-
 tests/libqos/libqos.h          |   5 +-
 tests/libqos/malloc-pc.h       |   4 +-
 tests/libqos/malloc-spapr.h    |   2 +-
 tests/libqos/malloc.h          |   1 +
 tests/libqos/pci-pc.h          |   2 +-
 tests/libqos/pci-spapr.h       |   2 +-
 tests/libqos/pci.h             |   5 +-
 tests/libqos/rtas.h            |   9 +-
 tests/libqos/virtio-mmio.h     |   6 +-
 tests/libqos/virtio-pci.h      |   2 -
 tests/libqos/virtio.h          |  13 +-
 tests/multiboot/libc.h         |   2 +-
 tests/libqtest.c               | 192 ++++++++-------
 qtest.c                        |  80 +++++--
 tests/ac97-test.c              |   4 +-
 tests/acpi-utils.c             |  19 +-
 tests/ahci-test.c              |  92 ++++----
 tests/bios-tables-test.c       | 199 ++++++++--------
 tests/boot-order-test.c        |  31 ++-
 tests/boot-sector.c            |   8 +-
 tests/boot-serial-test.c       |  12 +-
 tests/device-introspect-test.c |  30 +--
 tests/display-vga-test.c       |  18 +-
 tests/drive_del-test.c         |  26 +-
 tests/ds1338-test.c            |   6 +-
 tests/e1000-test.c             |  10 +-
 tests/e1000e-test.c            |  46 ++--
 tests/eepro100-test.c          |  11 +-
 tests/endianness-test.c        |  57 ++---
 tests/es1370-test.c            |   4 +-
 tests/fdc-test.c               | 144 ++++++------
 tests/fw_cfg-test.c            |  13 +-
 tests/hd-geo-test.c            |  20 +-
 tests/i440fx-test.c            |  24 +-
 tests/i82801b11-test.c         |   5 +-
 tests/ide-test.c               |  58 +++--
 tests/intel-hda-test.c         |   9 +-
 tests/ioh3420-test.c           |   7 +-
 tests/ipmi-bt-test.c           |  29 +--
 tests/ipmi-kcs-test.c          |  23 +-
 tests/ipoctal232-test.c        |   5 +-
 tests/ivshmem-test.c           |  27 +--
 tests/libqos/ahci.c            |  47 ++--
 tests/libqos/fw_cfg.c          |  14 +-
 tests/libqos/i2c-imx.c         |  67 +++---
 tests/libqos/i2c-omap.c        |  45 ++--
 tests/libqos/libqos-pc.c       |   2 +-
 tests/libqos/libqos.c          |  52 +---
 tests/libqos/malloc-pc.c       |   8 +-
 tests/libqos/malloc-spapr.c    |   4 +-
 tests/libqos/pci-pc.c          |  81 +++----
 tests/libqos/pci-spapr.c       |  45 ++--
 tests/libqos/pci.c             |  22 +-
 tests/libqos/rtas.c            |  36 +--
 tests/libqos/virtio-mmio.c     |  61 +++--
 tests/libqos/virtio-pci.c      |  14 +-
 tests/libqos/virtio.c          |  87 ++++---
 tests/m25p80-test.c            |  89 ++++---
 tests/m48t59-test.c            |  14 +-
 tests/megasas-test.c           |   7 +-
 tests/multiboot/libc.c         |   2 +-
 tests/ne2000-test.c            |   4 +-
 tests/numa-test.c              | 102 +++-----
 tests/nvme-test.c              |   7 +-
 tests/pc-cpu-test.c            |  24 +-
 tests/pcnet-test.c             |   4 +-
 tests/pnv-xscom-test.c         |  18 +-
 tests/postcopy-test.c          |  93 ++++----
 tests/prom-env-test.c          |  15 +-
 tests/pvpanic-test.c           |   8 +-
 tests/pxe-test.c               |  16 +-
 tests/q35-test.c               |  24 +-
 tests/qmp-test.c               |  43 ++--
 tests/qom-test.c               |   7 +-
 tests/rtas-test.c              |   3 +-
 tests/rtc-test.c               |  94 ++++----
 tests/rtl8139-test.c           |  17 +-
 tests/spapr-phb-test.c         |   5 +-
 tests/tco-test.c               |  45 ++--
 tests/test-arm-mptimer.c       |  43 ++--
 tests/test-filter-mirror.c     |  19 +-
 tests/test-filter-redirector.c |  66 +++---
 tests/test-hmp.c               |  13 +-
 tests/test-netfilter.c         |   9 +-
 tests/test-qga.c               |  90 -------
 tests/test-x86-cpuid-compat.c  |  13 +-
 tests/tmp105-test.c            |   7 +-
 tests/tpci200-test.c           |   4 +-
 tests/usb-hcd-ehci-test.c      |  27 ++-
 tests/usb-hcd-ohci-test.c      |   4 +-
 tests/usb-hcd-uhci-test.c      |   7 +-
 tests/usb-hcd-xhci-test.c      |  19 +-
 tests/vhost-user-test.c        | 105 +++------
 tests/virtio-9p-test.c         |   5 +-
 tests/virtio-balloon-test.c    |   4 +-
 tests/virtio-blk-test.c        |  83 ++++---
 tests/virtio-console-test.c    |  10 +-
 tests/virtio-net-test.c        |  30 ++-
 tests/virtio-rng-test.c        |   9 +-
 tests/virtio-scsi-test.c       |  22 +-
 tests/virtio-serial-test.c     |   4 +-
 tests/vmgenid-test.c           |  51 ++--
 tests/vmxnet3-test.c           |   4 +-
 tests/wdt_ib700-test.c         |  92 ++++----
 110 files changed, 1632 insertions(+), 2210 deletions(-)

-- 
2.13.5

^ permalink raw reply	[flat|nested] 75+ messages in thread

* [Qemu-devel] [PATCH v7 01/38] test-qga: Kill broken and dead QGA_TEST_SIDE_EFFECTING code
  2017-09-11 17:19 [Qemu-devel] [PATCH v7 00/38] Preliminary libqtest cleanups Eric Blake
@ 2017-09-11 17:19 ` Eric Blake
  2017-09-11 17:19 ` [Qemu-devel] [PATCH v7 02/38] qtest: Don't perform side effects inside assertion Eric Blake
                   ` (36 subsequent siblings)
  37 siblings, 0 replies; 75+ messages in thread
From: Eric Blake @ 2017-09-11 17:19 UTC (permalink / raw)
  To: qemu-devel; +Cc: armbru, pbonzini, thuth

Back when the test was introduced, in commit 62c39b307, the
test was set up to run qemu-ga directly on the host performing
the test, and defaults to limiting itself to safe commands.  At
the time, it was envisioned that setting QGA_TEST_SIDE_EFFECTING
in the environment could cover a few more commands, while noting
the potential danger of those side effects running in the host.

But this has NEVER been tested: if you enable the environment
variable, the test WILL fail.  One obvious reason: if you are not
running as root, you'll probably get a permission failure when
trying to freeze the file systems, or when changing system time.
Less obvious: if you run the test as root (wow, you're brave), you
could end up hanging if the test tries to log things to a
temporarily frozen filesystem.  But the cutest reason of all: if
you get past the above hurdles, the test uses invalid JSON in
test_qga_fstrim() (missing '' around the dictionary key 'minimum'),
and will thus fail an assertion in qmp_fd().

Rather than leave this untested time-bomb in place, rip it out.
Hopefully, as originally envisioned, we can find an opportunity
to test an actual sandboxed guest where the guest-agent has
full permissions and will not unduly affect the host running
the test - if so, 'git revert' can be used if desired, for
salvaging any useful parts of this attempt.

Signed-off-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: Thomas Huth <thuth@redhat.com>
---
 tests/test-qga.c | 90 --------------------------------------------------------
 1 file changed, 90 deletions(-)

diff --git a/tests/test-qga.c b/tests/test-qga.c
index 06783e7585..fd6bc7690f 100644
--- a/tests/test-qga.c
+++ b/tests/test-qga.c
@@ -642,65 +642,6 @@ static void test_qga_get_time(gconstpointer fix)
     QDECREF(ret);
 }

-static void test_qga_set_time(gconstpointer fix)
-{
-    const TestFixture *fixture = fix;
-    QDict *ret;
-    int64_t current, time;
-    gchar *cmd;
-
-    /* get current time */
-    ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-time'}");
-    g_assert_nonnull(ret);
-    qmp_assert_no_error(ret);
-    current = qdict_get_int(ret, "return");
-    g_assert_cmpint(current, >, 0);
-    QDECREF(ret);
-
-    /* set some old time */
-    ret = qmp_fd(fixture->fd, "{'execute': 'guest-set-time',"
-                 " 'arguments': { 'time': 1000 } }");
-    g_assert_nonnull(ret);
-    qmp_assert_no_error(ret);
-    QDECREF(ret);
-
-    /* check old time */
-    ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-time'}");
-    g_assert_nonnull(ret);
-    qmp_assert_no_error(ret);
-    time = qdict_get_int(ret, "return");
-    g_assert_cmpint(time / 1000, <, G_USEC_PER_SEC * 10);
-    QDECREF(ret);
-
-    /* set back current time */
-    cmd = g_strdup_printf("{'execute': 'guest-set-time',"
-                          " 'arguments': { 'time': %" PRId64 " } }",
-                          current + time * 1000);
-    ret = qmp_fd(fixture->fd, cmd);
-    g_free(cmd);
-    g_assert_nonnull(ret);
-    qmp_assert_no_error(ret);
-    QDECREF(ret);
-}
-
-static void test_qga_fstrim(gconstpointer fix)
-{
-    const TestFixture *fixture = fix;
-    QDict *ret;
-    QList *list;
-    const QListEntry *entry;
-
-    ret = qmp_fd(fixture->fd, "{'execute': 'guest-fstrim',"
-                 " arguments: { minimum: 4194304 } }");
-    g_assert_nonnull(ret);
-    qmp_assert_no_error(ret);
-    list = qdict_get_qlist(ret, "return");
-    entry = qlist_first(list);
-    g_assert(qdict_haskey(qobject_to_qdict(entry->value), "paths"));
-
-    QDECREF(ret);
-}
-
 static void test_qga_blacklist(gconstpointer data)
 {
     TestFixture fix;
@@ -831,30 +772,6 @@ static void test_qga_fsfreeze_status(gconstpointer fix)
     QDECREF(ret);
 }

-static void test_qga_fsfreeze_and_thaw(gconstpointer fix)
-{
-    const TestFixture *fixture = fix;
-    QDict *ret;
-    const gchar *status;
-
-    ret = qmp_fd(fixture->fd, "{'execute': 'guest-fsfreeze-freeze'}");
-    g_assert_nonnull(ret);
-    qmp_assert_no_error(ret);
-    QDECREF(ret);
-
-    ret = qmp_fd(fixture->fd, "{'execute': 'guest-fsfreeze-status'}");
-    g_assert_nonnull(ret);
-    qmp_assert_no_error(ret);
-    status = qdict_get_try_str(ret, "return");
-    g_assert_cmpstr(status, ==, "frozen");
-    QDECREF(ret);
-
-    ret = qmp_fd(fixture->fd, "{'execute': 'guest-fsfreeze-thaw'}");
-    g_assert_nonnull(ret);
-    qmp_assert_no_error(ret);
-    QDECREF(ret);
-}
-
 static void test_qga_guest_exec(gconstpointer fix)
 {
     const TestFixture *fixture = fix;
@@ -1029,13 +946,6 @@ int main(int argc, char **argv)
     g_test_add_data_func("/qga/guest-get-osinfo", &fix,
                          test_qga_guest_get_osinfo);

-    if (g_getenv("QGA_TEST_SIDE_EFFECTING")) {
-        g_test_add_data_func("/qga/fsfreeze-and-thaw", &fix,
-                             test_qga_fsfreeze_and_thaw);
-        g_test_add_data_func("/qga/set-time", &fix, test_qga_set_time);
-        g_test_add_data_func("/qga/fstrim", &fix, test_qga_fstrim);
-    }
-
     ret = g_test_run();

     fixture_tear_down(&fix, NULL);
-- 
2.13.5

^ permalink raw reply related	[flat|nested] 75+ messages in thread

* [Qemu-devel] [PATCH v7 02/38] qtest: Don't perform side effects inside assertion
  2017-09-11 17:19 [Qemu-devel] [PATCH v7 00/38] Preliminary libqtest cleanups Eric Blake
  2017-09-11 17:19 ` [Qemu-devel] [PATCH v7 01/38] test-qga: Kill broken and dead QGA_TEST_SIDE_EFFECTING code Eric Blake
@ 2017-09-11 17:19 ` Eric Blake
  2017-09-11 17:19 ` [Qemu-devel] [PATCH v7 03/38] numa-test: Use hmp() Eric Blake
                   ` (35 subsequent siblings)
  37 siblings, 0 replies; 75+ messages in thread
From: Eric Blake @ 2017-09-11 17:19 UTC (permalink / raw)
  To: qemu-devel; +Cc: armbru, pbonzini, thuth

Assertions should be separate from the side effects, since in
theory, g_assert() can be disabled (in practice, we can't really
ever do that).

Signed-off-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Thomas Huth <thuth@redhat.com>
---
 qtest.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++-------------------
 1 file changed, 57 insertions(+), 23 deletions(-)

diff --git a/qtest.c b/qtest.c
index 88a09e9afc..cbbfb71114 100644
--- a/qtest.c
+++ b/qtest.c
@@ -332,10 +332,13 @@ static void qtest_process_command(CharBackend *chr, gchar **words)
                strcmp(words[0], "outl") == 0) {
         unsigned long addr;
         unsigned long value;
+        int ret;

         g_assert(words[1] && words[2]);
-        g_assert(qemu_strtoul(words[1], NULL, 0, &addr) == 0);
-        g_assert(qemu_strtoul(words[2], NULL, 0, &value) == 0);
+        ret = qemu_strtoul(words[1], NULL, 0, &addr);
+        g_assert(ret == 0);
+        ret = qemu_strtoul(words[2], NULL, 0, &value);
+        g_assert(ret == 0);
         g_assert(addr <= 0xffff);

         if (words[0][3] == 'b') {
@@ -352,9 +355,11 @@ static void qtest_process_command(CharBackend *chr, gchar **words)
         strcmp(words[0], "inl") == 0) {
         unsigned long addr;
         uint32_t value = -1U;
+        int ret;

         g_assert(words[1]);
-        g_assert(qemu_strtoul(words[1], NULL, 0, &addr) == 0);
+        ret = qemu_strtoul(words[1], NULL, 0, &addr);
+        g_assert(ret == 0);
         g_assert(addr <= 0xffff);

         if (words[0][2] == 'b') {
@@ -372,10 +377,13 @@ static void qtest_process_command(CharBackend *chr, gchar **words)
                strcmp(words[0], "writeq") == 0) {
         uint64_t addr;
         uint64_t value;
+        int ret;

         g_assert(words[1] && words[2]);
-        g_assert(qemu_strtou64(words[1], NULL, 0, &addr) == 0);
-        g_assert(qemu_strtou64(words[2], NULL, 0, &value) == 0);
+        ret = qemu_strtou64(words[1], NULL, 0, &addr);
+        g_assert(ret == 0);
+        ret = qemu_strtou64(words[2], NULL, 0, &value);
+        g_assert(ret == 0);

         if (words[0][5] == 'b') {
             uint8_t data = value;
@@ -401,9 +409,11 @@ static void qtest_process_command(CharBackend *chr, gchar **words)
                strcmp(words[0], "readq") == 0) {
         uint64_t addr;
         uint64_t value = UINT64_C(-1);
+        int ret;

         g_assert(words[1]);
-        g_assert(qemu_strtou64(words[1], NULL, 0, &addr) == 0);
+        ret = qemu_strtou64(words[1], NULL, 0, &addr);
+        g_assert(ret == 0);

         if (words[0][4] == 'b') {
             uint8_t data;
@@ -427,10 +437,13 @@ static void qtest_process_command(CharBackend *chr, gchar **words)
         uint64_t addr, len, i;
         uint8_t *data;
         char *enc;
+        int ret;

         g_assert(words[1] && words[2]);
-        g_assert(qemu_strtou64(words[1], NULL, 0, &addr) == 0);
-        g_assert(qemu_strtou64(words[2], NULL, 0, &len) == 0);
+        ret = qemu_strtou64(words[1], NULL, 0, &addr);
+        g_assert(ret == 0);
+        ret = qemu_strtou64(words[2], NULL, 0, &len);
+        g_assert(ret == 0);
         /* We'd send garbage to libqtest if len is 0 */
         g_assert(len);

@@ -451,10 +464,13 @@ static void qtest_process_command(CharBackend *chr, gchar **words)
         uint64_t addr, len;
         uint8_t *data;
         gchar *b64_data;
+        int ret;

         g_assert(words[1] && words[2]);
-        g_assert(qemu_strtou64(words[1], NULL, 0, &addr) == 0);
-        g_assert(qemu_strtou64(words[2], NULL, 0, &len) == 0);
+        ret = qemu_strtou64(words[1], NULL, 0, &addr);
+        g_assert(ret == 0);
+        ret = qemu_strtou64(words[2], NULL, 0, &len);
+        g_assert(ret == 0);

         data = g_malloc(len);
         cpu_physical_memory_read(addr, data, len);
@@ -468,10 +484,13 @@ static void qtest_process_command(CharBackend *chr, gchar **words)
         uint64_t addr, len, i;
         uint8_t *data;
         size_t data_len;
+        int ret;

         g_assert(words[1] && words[2] && words[3]);
-        g_assert(qemu_strtou64(words[1], NULL, 0, &addr) == 0);
-        g_assert(qemu_strtou64(words[2], NULL, 0, &len) == 0);
+        ret = qemu_strtou64(words[1], NULL, 0, &addr);
+        g_assert(ret == 0);
+        ret = qemu_strtou64(words[2], NULL, 0, &len);
+        g_assert(ret == 0);

         data_len = strlen(words[3]);
         if (data_len < 3) {
@@ -497,11 +516,15 @@ static void qtest_process_command(CharBackend *chr, gchar **words)
         uint64_t addr, len;
         uint8_t *data;
         unsigned long pattern;
+        int ret;

         g_assert(words[1] && words[2] && words[3]);
-        g_assert(qemu_strtou64(words[1], NULL, 0, &addr) == 0);
-        g_assert(qemu_strtou64(words[2], NULL, 0, &len) == 0);
-        g_assert(qemu_strtoul(words[3], NULL, 0, &pattern) == 0);
+        ret = qemu_strtou64(words[1], NULL, 0, &addr);
+        g_assert(ret == 0);
+        ret = qemu_strtou64(words[2], NULL, 0, &len);
+        g_assert(ret == 0);
+        ret = qemu_strtoul(words[3], NULL, 0, &pattern);
+        g_assert(ret == 0);

         if (len) {
             data = g_malloc(len);
@@ -517,10 +540,13 @@ static void qtest_process_command(CharBackend *chr, gchar **words)
         uint8_t *data;
         size_t data_len;
         gsize out_len;
+        int ret;

         g_assert(words[1] && words[2] && words[3]);
-        g_assert(qemu_strtou64(words[1], NULL, 0, &addr) == 0);
-        g_assert(qemu_strtou64(words[2], NULL, 0, &len) == 0);
+        ret = qemu_strtou64(words[1], NULL, 0, &addr);
+        g_assert(ret == 0);
+        ret = qemu_strtou64(words[2], NULL, 0, &len);
+        g_assert(ret == 0);

         data_len = strlen(words[3]);
         if (data_len < 3) {
@@ -551,11 +577,16 @@ static void qtest_process_command(CharBackend *chr, gchar **words)
     } else if (strcmp(words[0], "rtas") == 0) {
         uint64_t res, args, ret;
         unsigned long nargs, nret;
+        int rc;

-        g_assert(qemu_strtoul(words[2], NULL, 0, &nargs) == 0);
-        g_assert(qemu_strtou64(words[3], NULL, 0, &args) == 0);
-        g_assert(qemu_strtoul(words[4], NULL, 0, &nret) == 0);
-        g_assert(qemu_strtou64(words[5], NULL, 0, &ret) == 0);
+        rc = qemu_strtoul(words[2], NULL, 0, &nargs);
+        g_assert(rc == 0);
+        rc = qemu_strtou64(words[3], NULL, 0, &args);
+        g_assert(rc == 0);
+        rc = qemu_strtoul(words[4], NULL, 0, &nret);
+        g_assert(rc == 0);
+        rc = qemu_strtou64(words[5], NULL, 0, &ret);
+        g_assert(rc == 0);
         res = qtest_rtas_call(words[1], nargs, args, nret, ret);

         qtest_send_prefix(chr);
@@ -565,7 +596,8 @@ static void qtest_process_command(CharBackend *chr, gchar **words)
         int64_t ns;

         if (words[1]) {
-            g_assert(qemu_strtoi64(words[1], NULL, 0, &ns) == 0);
+            int ret = qemu_strtoi64(words[1], NULL, 0, &ns);
+            g_assert(ret == 0);
         } else {
             ns = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
         }
@@ -575,9 +607,11 @@ static void qtest_process_command(CharBackend *chr, gchar **words)
                     (int64_t)qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
     } else if (qtest_enabled() && strcmp(words[0], "clock_set") == 0) {
         int64_t ns;
+        int ret;

         g_assert(words[1]);
-        g_assert(qemu_strtoi64(words[1], NULL, 0, &ns) == 0);
+        ret = qemu_strtoi64(words[1], NULL, 0, &ns);
+        g_assert(ret == 0);
         qtest_clock_warp(ns);
         qtest_send_prefix(chr);
         qtest_sendf(chr, "OK %"PRIi64"\n",
-- 
2.13.5

^ permalink raw reply related	[flat|nested] 75+ messages in thread

* [Qemu-devel] [PATCH v7 03/38] numa-test: Use hmp()
  2017-09-11 17:19 [Qemu-devel] [PATCH v7 00/38] Preliminary libqtest cleanups Eric Blake
  2017-09-11 17:19 ` [Qemu-devel] [PATCH v7 01/38] test-qga: Kill broken and dead QGA_TEST_SIDE_EFFECTING code Eric Blake
  2017-09-11 17:19 ` [Qemu-devel] [PATCH v7 02/38] qtest: Don't perform side effects inside assertion Eric Blake
@ 2017-09-11 17:19 ` Eric Blake
  2017-09-11 17:19 ` [Qemu-devel] [PATCH v7 04/38] tests: Clean up wait for event Eric Blake
                   ` (34 subsequent siblings)
  37 siblings, 0 replies; 75+ messages in thread
From: Eric Blake @ 2017-09-11 17:19 UTC (permalink / raw)
  To: qemu-devel; +Cc: armbru, pbonzini, thuth

Don't open-code something that has a convenient helper available.

Signed-off-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Reviewed-by: Thomas Huth <thuth@redhat.com>
---
 tests/numa-test.c | 21 +++------------------
 1 file changed, 3 insertions(+), 18 deletions(-)

diff --git a/tests/numa-test.c b/tests/numa-test.c
index 3f636840b1..e1b6152244 100644
--- a/tests/numa-test.c
+++ b/tests/numa-test.c
@@ -17,21 +17,6 @@ static char *make_cli(const char *generic_cli, const char *test_cli)
     return g_strdup_printf("%s %s", generic_cli ? generic_cli : "", test_cli);
 }

-static char *hmp_info_numa(void)
-{
-    QDict *resp;
-    char *s;
-
-    resp = qmp("{ 'execute': 'human-monitor-command', 'arguments': "
-                      "{ 'command-line': 'info numa '} }");
-    g_assert(resp);
-    g_assert(qdict_haskey(resp, "return"));
-    s = g_strdup(qdict_get_str(resp, "return"));
-    g_assert(s);
-    QDECREF(resp);
-    return s;
-}
-
 static void test_mon_explicit(const void *data)
 {
     char *s;
@@ -42,7 +27,7 @@ static void test_mon_explicit(const void *data)
                    "-numa node,nodeid=1,cpus=4-7 ");
     qtest_start(cli);

-    s = hmp_info_numa();
+    s = hmp("info numa");
     g_assert(strstr(s, "node 0 cpus: 0 1 2 3"));
     g_assert(strstr(s, "node 1 cpus: 4 5 6 7"));
     g_free(s);
@@ -59,7 +44,7 @@ static void test_mon_default(const void *data)
     cli = make_cli(data, "-smp 8 -numa node -numa node");
     qtest_start(cli);

-    s = hmp_info_numa();
+    s = hmp("info numa");
     g_assert(strstr(s, "node 0 cpus: 0 2 4 6"));
     g_assert(strstr(s, "node 1 cpus: 1 3 5 7"));
     g_free(s);
@@ -78,7 +63,7 @@ static void test_mon_partial(const void *data)
                    "-numa node,nodeid=1,cpus=4-5 ");
     qtest_start(cli);

-    s = hmp_info_numa();
+    s = hmp("info numa");
     g_assert(strstr(s, "node 0 cpus: 0 1 2 3 6 7"));
     g_assert(strstr(s, "node 1 cpus: 4 5"));
     g_free(s);
-- 
2.13.5

^ permalink raw reply related	[flat|nested] 75+ messages in thread

* [Qemu-devel] [PATCH v7 04/38] tests: Clean up wait for event
  2017-09-11 17:19 [Qemu-devel] [PATCH v7 00/38] Preliminary libqtest cleanups Eric Blake
                   ` (2 preceding siblings ...)
  2017-09-11 17:19 ` [Qemu-devel] [PATCH v7 03/38] numa-test: Use hmp() Eric Blake
@ 2017-09-11 17:19 ` Eric Blake
  2017-09-11 17:19 ` [Qemu-devel] [PATCH v7 05/38] libqtest: Remove dead qtest_instances variable Eric Blake
                   ` (33 subsequent siblings)
  37 siblings, 0 replies; 75+ messages in thread
From: Eric Blake @ 2017-09-11 17:19 UTC (permalink / raw)
  To: qemu-devel; +Cc: armbru, pbonzini, thuth, Gerd Hoffmann

From: Markus Armbruster <armbru@redhat.com>

We still use hacks like qmp("") to wait for an event, even though we
have qmp_eventwait() since commit 8fe941f, and qmp_eventwait_ref()
since commit 7ffe312.  Both commits neglected to convert all the
existing hacks.  Make up what they missed.

Bonus: gets rid of empty format strings.  A step towards compile-time
format string checking without triggering -Wformat-zero-length.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
Message-Id: <1500645206-13559-8-git-send-email-armbru@redhat.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Signed-off-by: Eric Blake <eblake@redhat.com>

---
v4: hoist earlier in series
---
 tests/boot-order-test.c   |  2 +-
 tests/libqos/pci-pc.c     |  6 +-----
 tests/tco-test.c          |  3 +--
 tests/usb-hcd-uhci-test.c |  6 +-----
 tests/usb-hcd-xhci-test.c | 12 ++----------
 tests/wdt_ib700-test.c    | 35 ++++++++++-------------------------
 6 files changed, 16 insertions(+), 48 deletions(-)

diff --git a/tests/boot-order-test.c b/tests/boot-order-test.c
index fc1e7941f7..9d516830dd 100644
--- a/tests/boot-order-test.c
+++ b/tests/boot-order-test.c
@@ -43,7 +43,7 @@ static void test_a_boot_order(const char *machine,
      * system_reset only requests reset.  We get a RESET event after
      * the actual reset completes.  Need to wait for that.
      */
-    qmp_discard_response("");   /* HACK: wait for event */
+    qmp_eventwait("RESET");
     actual = read_boot_order();
     g_assert_cmphex(actual, ==, expected_reboot);
     qtest_quit(global_qtest);
diff --git a/tests/libqos/pci-pc.c b/tests/libqos/pci-pc.c
index ded1c54c06..02ce49927a 100644
--- a/tests/libqos/pci-pc.c
+++ b/tests/libqos/pci-pc.c
@@ -173,9 +173,5 @@ void qpci_unplug_acpi_device_test(const char *id, uint8_t slot)

     outb(ACPI_PCIHP_ADDR + PCI_EJ_BASE, 1 << slot);

-    response = qmp("");
-    g_assert(response);
-    g_assert(qdict_haskey(response, "event"));
-    g_assert(!strcmp(qdict_get_str(response, "event"), "DEVICE_DELETED"));
-    QDECREF(response);
+    qmp_eventwait("DEVICE_DELETED");
 }
diff --git a/tests/tco-test.c b/tests/tco-test.c
index c4c264eb3d..f2ed6ed91c 100644
--- a/tests/tco-test.c
+++ b/tests/tco-test.c
@@ -237,9 +237,8 @@ static void test_tco_max_timeout(void)

 static QDict *get_watchdog_action(void)
 {
-    QDict *ev = qmp("");
+    QDict *ev = qmp_eventwait_ref("WATCHDOG");
     QDict *data;
-    g_assert(!strcmp(qdict_get_str(ev, "event"), "WATCHDOG"));

     data = qdict_get_qdict(ev, "data");
     QINCREF(data);
diff --git a/tests/usb-hcd-uhci-test.c b/tests/usb-hcd-uhci-test.c
index 5b500fedb0..0fb7f8d223 100644
--- a/tests/usb-hcd-uhci-test.c
+++ b/tests/usb-hcd-uhci-test.c
@@ -68,11 +68,7 @@ static void test_usb_storage_hotplug(void)
     g_assert(!qdict_haskey(response, "error"));
     QDECREF(response);

-    response = qmp("");
-    g_assert(response);
-    g_assert(qdict_haskey(response, "event"));
-    g_assert(!strcmp(qdict_get_str(response, "event"), "DEVICE_DELETED"));
-    QDECREF(response);
+    qmp_eventwait("DEVICE_DELETED");
 }

 int main(int argc, char **argv)
diff --git a/tests/usb-hcd-xhci-test.c b/tests/usb-hcd-xhci-test.c
index 031764da6d..c05a339894 100644
--- a/tests/usb-hcd-xhci-test.c
+++ b/tests/usb-hcd-xhci-test.c
@@ -57,11 +57,7 @@ static void test_usb_uas_hotplug(void)
     g_assert(!qdict_haskey(response, "error"));
     QDECREF(response);

-    response = qmp("");
-    g_assert(qdict_haskey(response, "event"));
-    g_assert(!strcmp(qdict_get_str(response, "event"), "DEVICE_DELETED"));
-    QDECREF(response);
-
+    qmp_eventwait("DEVICE_DELETED");

     response = qmp("{'execute': 'device_del',"
                            " 'arguments': {"
@@ -71,11 +67,7 @@ static void test_usb_uas_hotplug(void)
     g_assert(!qdict_haskey(response, "error"));
     QDECREF(response);

-    response = qmp("");
-    g_assert(response);
-    g_assert(qdict_haskey(response, "event"));
-    g_assert(!strcmp(qdict_get_str(response, "event"), "DEVICE_DELETED"));
-    QDECREF(response);
+    qmp_eventwait("DEVICE_DELETED");
 }

 int main(int argc, char **argv)
diff --git a/tests/wdt_ib700-test.c b/tests/wdt_ib700-test.c
index 49f4f0c221..4fc8eeae86 100644
--- a/tests/wdt_ib700-test.c
+++ b/tests/wdt_ib700-test.c
@@ -18,26 +18,10 @@ static void qmp_check_no_event(void)
     QDECREF(resp);
 }

-static QDict *qmp_get_event(const char *name)
-{
-    QDict *event = qmp("");
-    QDict *data;
-    g_assert(qdict_haskey(event, "event"));
-    g_assert(!strcmp(qdict_get_str(event, "event"), name));
-
-    if (qdict_haskey(event, "data")) {
-        data = qdict_get_qdict(event, "data");
-        QINCREF(data);
-    } else {
-        data = NULL;
-    }
-
-    QDECREF(event);
-    return data;
-}
-
 static QDict *ib700_program_and_wait(QTestState *s)
 {
+    QDict *event, *data;
+
     clock_step(NANOSECONDS_PER_SECOND * 40);
     qmp_check_no_event();

@@ -61,7 +45,11 @@ static QDict *ib700_program_and_wait(QTestState *s)
     clock_step(3 * NANOSECONDS_PER_SECOND);
     qmp_check_no_event();
     clock_step(2 * NANOSECONDS_PER_SECOND);
-    return qmp_get_event("WATCHDOG");
+    event = qmp_eventwait_ref("WATCHDOG");
+    data = qdict_get_qdict(event, "data");
+    QINCREF(data);
+    QDECREF(event);
+    return data;
 }


@@ -73,8 +61,7 @@ static void ib700_pause(void)
     d = ib700_program_and_wait(s);
     g_assert(!strcmp(qdict_get_str(d, "action"), "pause"));
     QDECREF(d);
-    d = qmp_get_event("STOP");
-    QDECREF(d);
+    qmp_eventwait("STOP");
     qtest_end();
 }

@@ -86,8 +73,7 @@ static void ib700_reset(void)
     d = ib700_program_and_wait(s);
     g_assert(!strcmp(qdict_get_str(d, "action"), "reset"));
     QDECREF(d);
-    d = qmp_get_event("RESET");
-    QDECREF(d);
+    qmp_eventwait("RESET");
     qtest_end();
 }

@@ -99,8 +85,7 @@ static void ib700_shutdown(void)
     d = ib700_program_and_wait(s);
     g_assert(!strcmp(qdict_get_str(d, "action"), "reset"));
     QDECREF(d);
-    d = qmp_get_event("SHUTDOWN");
-    QDECREF(d);
+    qmp_eventwait("SHUTDOWN");
     qtest_end();
 }

-- 
2.13.5

^ permalink raw reply related	[flat|nested] 75+ messages in thread

* [Qemu-devel] [PATCH v7 05/38] libqtest: Remove dead qtest_instances variable
  2017-09-11 17:19 [Qemu-devel] [PATCH v7 00/38] Preliminary libqtest cleanups Eric Blake
                   ` (3 preceding siblings ...)
  2017-09-11 17:19 ` [Qemu-devel] [PATCH v7 04/38] tests: Clean up wait for event Eric Blake
@ 2017-09-11 17:19 ` Eric Blake
  2017-09-11 17:19 ` [Qemu-devel] [PATCH v7 06/38] libqtest: Use qemu_strtoul() Eric Blake
                   ` (32 subsequent siblings)
  37 siblings, 0 replies; 75+ messages in thread
From: Eric Blake @ 2017-09-11 17:19 UTC (permalink / raw)
  To: qemu-devel; +Cc: armbru, pbonzini, thuth

Prior to commit 063c23d9, we were tracking a list of parallel
qtest objects, in order to safely clean up a SIGABRT handler
only after the last connection quits.  But when we switched to
more of glib's infrastructure, the list became dead code that
is never assigned to.

Signed-off-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Reviewed-by: Thomas Huth <thuth@redhat.com>
---
 tests/libqtest.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)

diff --git a/tests/libqtest.c b/tests/libqtest.c
index b9a1f180e1..3f956f09fc 100644
--- a/tests/libqtest.c
+++ b/tests/libqtest.c
@@ -42,7 +42,6 @@ struct QTestState
 };

 static GHookList abrt_hooks;
-static GList *qtest_instances;
 static struct sigaction sigact_old;

 #define g_assert_no_errno(ret) do { \
@@ -240,13 +239,10 @@ QTestState *qtest_init(const char *extra_args)

 void qtest_quit(QTestState *s)
 {
-    qtest_instances = g_list_remove(qtest_instances, s);
     g_hook_destroy_link(&abrt_hooks, g_hook_find_data(&abrt_hooks, TRUE, s));

     /* Uninstall SIGABRT handler on last instance */
-    if (!qtest_instances) {
-        cleanup_sigabrt_handler();
-    }
+    cleanup_sigabrt_handler();

     kill_qemu(s);
     close(s->fd);
-- 
2.13.5

^ permalink raw reply related	[flat|nested] 75+ messages in thread

* [Qemu-devel] [PATCH v7 06/38] libqtest: Use qemu_strtoul()
  2017-09-11 17:19 [Qemu-devel] [PATCH v7 00/38] Preliminary libqtest cleanups Eric Blake
                   ` (4 preceding siblings ...)
  2017-09-11 17:19 ` [Qemu-devel] [PATCH v7 05/38] libqtest: Remove dead qtest_instances variable Eric Blake
@ 2017-09-11 17:19 ` Eric Blake
  2017-09-11 17:19 ` [Qemu-devel] [PATCH v7 07/38] libqtest: Inline qtest_query_target_endianness() Eric Blake
                   ` (31 subsequent siblings)
  37 siblings, 0 replies; 75+ messages in thread
From: Eric Blake @ 2017-09-11 17:19 UTC (permalink / raw)
  To: qemu-devel; +Cc: armbru, pbonzini, thuth

This will keep checkpatch happy when the next patch does code motion.
Fix the include order to match HACKING when adding the needed header.

Signed-off-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Reviewed-by: Thomas Huth <thuth@redhat.com>
---
 tests/libqtest.c | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)

diff --git a/tests/libqtest.c b/tests/libqtest.c
index 3f956f09fc..95d4159b38 100644
--- a/tests/libqtest.c
+++ b/tests/libqtest.c
@@ -15,12 +15,13 @@
  *
  */
 #include "qemu/osdep.h"
-#include "libqtest.h"

 #include <sys/socket.h>
 #include <sys/wait.h>
 #include <sys/un.h>

+#include "libqtest.h"
+#include "qemu/cutils.h"
 #include "qapi/error.h"
 #include "qapi/qmp/json-parser.h"
 #include "qapi/qmp/json-streamer.h"
@@ -331,12 +332,14 @@ redo:
     g_string_free(line, TRUE);

     if (strcmp(words[0], "IRQ") == 0) {
-        int irq;
+        long irq;
+        int ret;

         g_assert(words[1] != NULL);
         g_assert(words[2] != NULL);

-        irq = strtoul(words[2], NULL, 0);
+        ret = qemu_strtol(words[2], NULL, 0, &irq);
+        g_assert(!ret);
         g_assert_cmpint(irq, >=, 0);
         g_assert_cmpint(irq, <, MAX_IRQ);

@@ -699,11 +702,13 @@ void qtest_outl(QTestState *s, uint16_t addr, uint32_t value)
 static uint32_t qtest_in(QTestState *s, const char *cmd, uint16_t addr)
 {
     gchar **args;
-    uint32_t value;
+    int ret;
+    unsigned long value;

     qtest_sendf(s, "%s 0x%x\n", cmd, addr);
     args = qtest_rsp(s, 2);
-    value = strtoul(args[1], NULL, 0);
+    ret = qemu_strtoul(args[1], NULL, 0, &value);
+    g_assert(!ret && value <= UINT32_MAX);
     g_strfreev(args);

     return value;
@@ -754,11 +759,13 @@ void qtest_writeq(QTestState *s, uint64_t addr, uint64_t value)
 static uint64_t qtest_read(QTestState *s, const char *cmd, uint64_t addr)
 {
     gchar **args;
+    int ret;
     uint64_t value;

     qtest_sendf(s, "%s 0x%" PRIx64 "\n", cmd, addr);
     args = qtest_rsp(s, 2);
-    value = strtoull(args[1], NULL, 0);
+    ret = qemu_strtou64(args[1], NULL, 0, &value);
+    g_assert(!ret);
     g_strfreev(args);

     return value;
-- 
2.13.5

^ permalink raw reply related	[flat|nested] 75+ messages in thread

* [Qemu-devel] [PATCH v7 07/38] libqtest: Inline qtest_query_target_endianness()
  2017-09-11 17:19 [Qemu-devel] [PATCH v7 00/38] Preliminary libqtest cleanups Eric Blake
                   ` (5 preceding siblings ...)
  2017-09-11 17:19 ` [Qemu-devel] [PATCH v7 06/38] libqtest: Use qemu_strtoul() Eric Blake
@ 2017-09-11 17:19 ` Eric Blake
  2017-09-12  6:32   ` Thomas Huth
  2017-09-11 17:19 ` [Qemu-devel] [PATCH v7 08/38] libqos: Track QTestState with QPCIBus Eric Blake
                   ` (30 subsequent siblings)
  37 siblings, 1 reply; 75+ messages in thread
From: Eric Blake @ 2017-09-11 17:19 UTC (permalink / raw)
  To: qemu-devel; +Cc: armbru, pbonzini, thuth

There was only one caller; it's easier to inline things.  (It
trades one forward declaration for two others, but it's not
worth the additional churn of topological sorting, even though
that would be possible.)

Signed-off-by: Eric Blake <eblake@redhat.com>

---
v7: Drop the topological sorting
---
 tests/libqtest.c | 26 +++++++++-----------------
 1 file changed, 9 insertions(+), 17 deletions(-)

diff --git a/tests/libqtest.c b/tests/libqtest.c
index 95d4159b38..1710d63276 100644
--- a/tests/libqtest.c
+++ b/tests/libqtest.c
@@ -49,7 +49,9 @@ static struct sigaction sigact_old;
     g_assert_cmpint(ret, !=, -1); \
 } while (0)

-static int qtest_query_target_endianness(QTestState *s);
+static void qtest_sendf(QTestState *s, const char *fmt, ...)
+    GCC_FMT_ATTR(2, 3);
+static gchar **qtest_rsp(QTestState *s, int expected_args);

 static int init_socket(const char *socket_path)
 {
@@ -158,6 +160,7 @@ QTestState *qtest_init_without_qmp_handshake(const char *extra_args)
     gchar *qmp_socket_path;
     gchar *command;
     const char *qemu_binary;
+    gchar **args;

     qemu_binary = getenv("QTEST_QEMU_BINARY");
     if (!qemu_binary) {
@@ -221,8 +224,11 @@ QTestState *qtest_init_without_qmp_handshake(const char *extra_args)
     }

     /* ask endianness of the target */
-
-    s->big_endian = qtest_query_target_endianness(s);
+    qtest_sendf(s, "endianness\n");
+    args = qtest_rsp(s, 1);
+    g_assert(strcmp(args[1], "big") == 0 || strcmp(args[1], "little") == 0);
+    s->big_endian = strcmp(args[1], "big") == 0;
+    g_strfreev(args);

     return s;
 }
@@ -367,20 +373,6 @@ redo:
     return words;
 }

-static int qtest_query_target_endianness(QTestState *s)
-{
-    gchar **args;
-    int big_endian;
-
-    qtest_sendf(s, "endianness\n");
-    args = qtest_rsp(s, 1);
-    g_assert(strcmp(args[1], "big") == 0 || strcmp(args[1], "little") == 0);
-    big_endian = strcmp(args[1], "big") == 0;
-    g_strfreev(args);
-
-    return big_endian;
-}
-
 typedef struct {
     JSONMessageParser parser;
     QDict *response;
-- 
2.13.5

^ permalink raw reply related	[flat|nested] 75+ messages in thread

* [Qemu-devel] [PATCH v7 08/38] libqos: Track QTestState with QPCIBus
  2017-09-11 17:19 [Qemu-devel] [PATCH v7 00/38] Preliminary libqtest cleanups Eric Blake
                   ` (6 preceding siblings ...)
  2017-09-11 17:19 ` [Qemu-devel] [PATCH v7 07/38] libqtest: Inline qtest_query_target_endianness() Eric Blake
@ 2017-09-11 17:19 ` Eric Blake
  2017-09-11 23:46   ` John Snow
  2017-09-12  7:05   ` Thomas Huth
  2017-09-11 17:19 ` [Qemu-devel] [PATCH v7 09/38] libqos: Track QTestState with QVirtioBus Eric Blake
                   ` (29 subsequent siblings)
  37 siblings, 2 replies; 75+ messages in thread
From: Eric Blake @ 2017-09-11 17:19 UTC (permalink / raw)
  To: qemu-devel
  Cc: armbru, pbonzini, thuth, John Snow, David Gibson, Alexander Graf,
	Gerd Hoffmann, open list:IDE, open list:sPAPR

When initializing a QPCIBus, track which QTestState the bus is
associated with (so that a later patch can then explicitly use
that test state for all communication on the bus, rather than
blindly relying on global_qtest).  Update the initialization
functions to take another parameter, and update all callers to
pass in state (for now, most callers get away with passing the
current global_qtest as the current state, although this required
fixing the order of initialization to ensure qtest_start() is
called before qpci_init*() in rtl8139-test, and provided an
opportunity to pass in the allocator in e1000e-test).

Touch up some allocations to use g_new0() rather than g_malloc()
while in the area, and simplify some code (all implementations
of QOSOps provide a .init_allocator() that never fails).

Signed-off-by: Eric Blake <eblake@redhat.com>

---
v7: use g_new0() instead of g_malloc(), drop dead conditionals
---
 tests/libqos/ahci.h       |  2 +-
 tests/libqos/libqos.h     |  2 +-
 tests/libqos/pci-pc.h     |  2 +-
 tests/libqos/pci-spapr.h  |  2 +-
 tests/libqos/pci.h        |  1 +
 tests/ahci-test.c         |  2 +-
 tests/e1000e-test.c       |  6 +++---
 tests/i440fx-test.c       |  2 +-
 tests/ide-test.c          |  2 +-
 tests/libqos/ahci.c       |  4 ++--
 tests/libqos/libqos.c     | 10 +++-------
 tests/libqos/pci-pc.c     |  7 ++++---
 tests/libqos/pci-spapr.c  |  7 ++++---
 tests/q35-test.c          |  4 ++--
 tests/rtl8139-test.c      |  5 +++--
 tests/tco-test.c          |  2 +-
 tests/usb-hcd-ehci-test.c |  2 +-
 tests/vhost-user-test.c   |  4 ++--
 18 files changed, 33 insertions(+), 33 deletions(-)

diff --git a/tests/libqos/ahci.h b/tests/libqos/ahci.h
index 5f9627bb0f..715ca1e226 100644
--- a/tests/libqos/ahci.h
+++ b/tests/libqos/ahci.h
@@ -571,7 +571,7 @@ void ahci_free(AHCIQState *ahci, uint64_t addr);
 void ahci_clean_mem(AHCIQState *ahci);

 /* Device management */
-QPCIDevice *get_ahci_device(uint32_t *fingerprint);
+QPCIDevice *get_ahci_device(QTestState *qts, uint32_t *fingerprint);
 void free_ahci_device(QPCIDevice *dev);
 void ahci_pci_enable(AHCIQState *ahci);
 void start_ahci_device(AHCIQState *ahci);
diff --git a/tests/libqos/libqos.h b/tests/libqos/libqos.h
index 231969766f..78e5c044a0 100644
--- a/tests/libqos/libqos.h
+++ b/tests/libqos/libqos.h
@@ -10,7 +10,7 @@ typedef struct QOSState QOSState;
 typedef struct QOSOps {
     QGuestAllocator *(*init_allocator)(QAllocOpts);
     void (*uninit_allocator)(QGuestAllocator *);
-    QPCIBus *(*qpci_init)(QGuestAllocator *alloc);
+    QPCIBus *(*qpci_init)(QTestState *qts, QGuestAllocator *alloc);
     void (*qpci_free)(QPCIBus *bus);
     void (*shutdown)(QOSState *);
 } QOSOps;
diff --git a/tests/libqos/pci-pc.h b/tests/libqos/pci-pc.h
index 9479b51642..491eeac756 100644
--- a/tests/libqos/pci-pc.h
+++ b/tests/libqos/pci-pc.h
@@ -16,7 +16,7 @@
 #include "libqos/pci.h"
 #include "libqos/malloc.h"

-QPCIBus *qpci_init_pc(QGuestAllocator *alloc);
+QPCIBus *qpci_init_pc(QTestState *qts, QGuestAllocator *alloc);
 void     qpci_free_pc(QPCIBus *bus);

 #endif
diff --git a/tests/libqos/pci-spapr.h b/tests/libqos/pci-spapr.h
index 4192126d86..387686dfc8 100644
--- a/tests/libqos/pci-spapr.h
+++ b/tests/libqos/pci-spapr.h
@@ -11,7 +11,7 @@
 #include "libqos/malloc.h"
 #include "libqos/pci.h"

-QPCIBus *qpci_init_spapr(QGuestAllocator *alloc);
+QPCIBus *qpci_init_spapr(QTestState *qts, QGuestAllocator *alloc);
 void     qpci_free_spapr(QPCIBus *bus);

 #endif
diff --git a/tests/libqos/pci.h b/tests/libqos/pci.h
index ed480614ff..429c382282 100644
--- a/tests/libqos/pci.h
+++ b/tests/libqos/pci.h
@@ -48,6 +48,7 @@ struct QPCIBus {
     void (*config_writel)(QPCIBus *bus, int devfn,
                           uint8_t offset, uint32_t value);

+    QTestState *qts;
     uint16_t pio_alloc_ptr;
     uint64_t mmio_alloc_ptr, mmio_limit;
 };
diff --git a/tests/ahci-test.c b/tests/ahci-test.c
index 999121bb7c..c94d1bd712 100644
--- a/tests/ahci-test.c
+++ b/tests/ahci-test.c
@@ -160,7 +160,7 @@ static AHCIQState *ahci_vboot(const char *cli, va_list ap)
     alloc_set_flags(s->parent->alloc, ALLOC_LEAK_ASSERT);

     /* Verify that we have an AHCI device present. */
-    s->dev = get_ahci_device(&s->fingerprint);
+    s->dev = get_ahci_device(s->parent->qts, &s->fingerprint);

     return s;
 }
diff --git a/tests/e1000e-test.c b/tests/e1000e-test.c
index c612dc64ec..d8085d944e 100644
--- a/tests/e1000e-test.c
+++ b/tests/e1000e-test.c
@@ -392,12 +392,12 @@ static void data_test_init(e1000e_device *d)
     qtest_start(cmdline);
     g_free(cmdline);

-    test_bus = qpci_init_pc(NULL);
-    g_assert_nonnull(test_bus);
-
     test_alloc = pc_alloc_init();
     g_assert_nonnull(test_alloc);

+    test_bus = qpci_init_pc(global_qtest, test_alloc);
+    g_assert_nonnull(test_bus);
+
     e1000e_device_init(test_bus, d);
 }

diff --git a/tests/i440fx-test.c b/tests/i440fx-test.c
index e9d05c87d1..4390e5591e 100644
--- a/tests/i440fx-test.c
+++ b/tests/i440fx-test.c
@@ -38,7 +38,7 @@ static QPCIBus *test_start_get_bus(const TestData *s)
     cmdline = g_strdup_printf("-smp %d", s->num_cpus);
     qtest_start(cmdline);
     g_free(cmdline);
-    return qpci_init_pc(NULL);
+    return qpci_init_pc(global_qtest, NULL);
 }

 static void test_i440fx_defaults(gconstpointer opaque)
diff --git a/tests/ide-test.c b/tests/ide-test.c
index aa9de065fc..b2237b6158 100644
--- a/tests/ide-test.c
+++ b/tests/ide-test.c
@@ -143,7 +143,7 @@ static QPCIDevice *get_pci_device(QPCIBar *bmdma_bar, QPCIBar *ide_bar)
     uint16_t vendor_id, device_id;

     if (!pcibus) {
-        pcibus = qpci_init_pc(NULL);
+        pcibus = qpci_init_pc(global_qtest, NULL);
     }

     /* Find PCI device and verify it's the right one */
diff --git a/tests/libqos/ahci.c b/tests/libqos/ahci.c
index 1ca7f456b5..790ef991b3 100644
--- a/tests/libqos/ahci.c
+++ b/tests/libqos/ahci.c
@@ -123,13 +123,13 @@ bool is_atapi(AHCIQState *ahci, uint8_t port)
 /**
  * Locate, verify, and return a handle to the AHCI device.
  */
-QPCIDevice *get_ahci_device(uint32_t *fingerprint)
+QPCIDevice *get_ahci_device(QTestState *qts, uint32_t *fingerprint)
 {
     QPCIDevice *ahci;
     uint32_t ahci_fingerprint;
     QPCIBus *pcibus;

-    pcibus = qpci_init_pc(NULL);
+    pcibus = qpci_init_pc(qts, NULL);

     /* Find the AHCI PCI device and verify it's the right one. */
     ahci = qpci_device_find(pcibus, QPCI_DEVFN(0x1F, 0x02));
diff --git a/tests/libqos/libqos.c b/tests/libqos/libqos.c
index 6226546c28..7a76c1ef34 100644
--- a/tests/libqos/libqos.c
+++ b/tests/libqos/libqos.c
@@ -17,18 +17,14 @@ QOSState *qtest_vboot(QOSOps *ops, const char *cmdline_fmt, va_list ap)
 {
     char *cmdline;

-    struct QOSState *qs = g_malloc(sizeof(QOSState));
+    QOSState *qs = g_new0(QOSState, 1);

     cmdline = g_strdup_vprintf(cmdline_fmt, ap);
     qs->qts = qtest_start(cmdline);
     qs->ops = ops;
     if (ops) {
-        if (ops->init_allocator) {
-            qs->alloc = ops->init_allocator(ALLOC_NO_FLAGS);
-        }
-        if (ops->qpci_init && qs->alloc) {
-            qs->pcibus = ops->qpci_init(qs->alloc);
-        }
+        qs->alloc = ops->init_allocator(ALLOC_NO_FLAGS);
+        qs->pcibus = ops->qpci_init(qs->qts, qs->alloc);
     }

     g_free(cmdline);
diff --git a/tests/libqos/pci-pc.c b/tests/libqos/pci-pc.c
index 02ce49927a..e267fd1a44 100644
--- a/tests/libqos/pci-pc.c
+++ b/tests/libqos/pci-pc.c
@@ -115,11 +115,11 @@ static void qpci_pc_config_writel(QPCIBus *bus, int devfn, uint8_t offset, uint3
     outl(0xcfc, value);
 }

-QPCIBus *qpci_init_pc(QGuestAllocator *alloc)
+QPCIBus *qpci_init_pc(QTestState *qts, QGuestAllocator *alloc)
 {
-    QPCIBusPC *ret;
+    QPCIBusPC *ret = g_new0(QPCIBusPC, 1);

-    ret = g_malloc(sizeof(*ret));
+    assert(qts);

     ret->bus.pio_readb = qpci_pc_pio_readb;
     ret->bus.pio_readw = qpci_pc_pio_readw;
@@ -142,6 +142,7 @@ QPCIBus *qpci_init_pc(QGuestAllocator *alloc)
     ret->bus.config_writew = qpci_pc_config_writew;
     ret->bus.config_writel = qpci_pc_config_writel;

+    ret->bus.qts = qts;
     ret->bus.pio_alloc_ptr = 0xc000;
     ret->bus.mmio_alloc_ptr = 0xE0000000;
     ret->bus.mmio_limit = 0x100000000ULL;
diff --git a/tests/libqos/pci-spapr.c b/tests/libqos/pci-spapr.c
index 2043f1e123..ef52fcbe2b 100644
--- a/tests/libqos/pci-spapr.c
+++ b/tests/libqos/pci-spapr.c
@@ -154,11 +154,11 @@ static void qpci_spapr_config_writel(QPCIBus *bus, int devfn, uint8_t offset,
 #define SPAPR_PCI_MMIO32_WIN_SIZE    0x80000000 /* 2 GiB */
 #define SPAPR_PCI_IO_WIN_SIZE        0x10000

-QPCIBus *qpci_init_spapr(QGuestAllocator *alloc)
+QPCIBus *qpci_init_spapr(QTestState *qts, QGuestAllocator *alloc)
 {
-    QPCIBusSPAPR *ret;
+    QPCIBusSPAPR *ret = g_new0(QPCIBusSPAPR, 1);

-    ret = g_malloc(sizeof(*ret));
+    assert(qts);

     ret->alloc = alloc;

@@ -197,6 +197,7 @@ QPCIBus *qpci_init_spapr(QGuestAllocator *alloc)
     ret->mmio32.pci_base = SPAPR_PCI_MMIO32_WIN_SIZE;
     ret->mmio32.size = SPAPR_PCI_MMIO32_WIN_SIZE;

+    ret->bus.qts = qts;
     ret->bus.pio_alloc_ptr = 0xc000;
     ret->bus.mmio_alloc_ptr = ret->mmio32.pci_base;
     ret->bus.mmio_limit = ret->mmio32.pci_base + ret->mmio32.size;
diff --git a/tests/q35-test.c b/tests/q35-test.c
index f98bed7a2d..e149c4c51d 100644
--- a/tests/q35-test.c
+++ b/tests/q35-test.c
@@ -86,7 +86,7 @@ static void test_smram_lock(void)

     qtest_start("-M q35");

-    pcibus = qpci_init_pc(NULL);
+    pcibus = qpci_init_pc(global_qtest, NULL);
     g_assert(pcibus != NULL);

     pcidev = qpci_device_find(pcibus, 0);
@@ -145,7 +145,7 @@ static void test_tseg_size(const void *data)
     g_free(cmdline);

     /* locate the DRAM controller */
-    pcibus = qpci_init_pc(NULL);
+    pcibus = qpci_init_pc(global_qtest, NULL);
     g_assert(pcibus != NULL);
     pcidev = qpci_device_find(pcibus, 0);
     g_assert(pcidev != NULL);
diff --git a/tests/rtl8139-test.c b/tests/rtl8139-test.c
index 7de7dc45ae..68bfc42178 100644
--- a/tests/rtl8139-test.c
+++ b/tests/rtl8139-test.c
@@ -35,7 +35,7 @@ static QPCIDevice *get_device(void)
 {
     QPCIDevice *dev;

-    pcibus = qpci_init_pc(NULL);
+    pcibus = qpci_init_pc(global_qtest, NULL);
     qpci_device_foreach(pcibus, 0x10ec, 0x8139, save_fn, &dev);
     g_assert(dev != NULL);

@@ -197,11 +197,12 @@ int main(int argc, char **argv)
 {
     int ret;

+    qtest_start("-device rtl8139");
+
     g_test_init(&argc, &argv, NULL);
     qtest_add_func("/rtl8139/nop", nop);
     qtest_add_func("/rtl8139/timer", test_init);

-    qtest_start("-device rtl8139");
     ret = g_test_run();

     qtest_end();
diff --git a/tests/tco-test.c b/tests/tco-test.c
index f2ed6ed91c..0387971953 100644
--- a/tests/tco-test.c
+++ b/tests/tco-test.c
@@ -64,7 +64,7 @@ static void test_init(TestData *d)
     qtest_irq_intercept_in(qs, "ioapic");
     g_free(s);

-    d->bus = qpci_init_pc(NULL);
+    d->bus = qpci_init_pc(qs, NULL);
     d->dev = qpci_device_find(d->bus, QPCI_DEVFN(0x1f, 0x00));
     g_assert(d->dev != NULL);

diff --git a/tests/usb-hcd-ehci-test.c b/tests/usb-hcd-ehci-test.c
index 944eb1c088..55d4743a2a 100644
--- a/tests/usb-hcd-ehci-test.c
+++ b/tests/usb-hcd-ehci-test.c
@@ -52,7 +52,7 @@ static void ehci_port_test(struct qhc *hc, int port, uint32_t expect)

 static void test_init(void)
 {
-    pcibus = qpci_init_pc(NULL);
+    pcibus = qpci_init_pc(global_qtest, NULL);
     g_assert(pcibus != NULL);

     qusb_pci_init_one(pcibus, &uhci1, QPCI_DEVFN(0x1d, 0), 4);
diff --git a/tests/vhost-user-test.c b/tests/vhost-user-test.c
index 4b98018478..f762b2ebc1 100644
--- a/tests/vhost-user-test.c
+++ b/tests/vhost-user-test.c
@@ -166,7 +166,7 @@ static void init_virtio_dev(TestServer *s)
     QVirtioPCIDevice *dev;
     uint32_t features;

-    s->bus = qpci_init_pc(NULL);
+    s->bus = qpci_init_pc(global_qtest, NULL);
     g_assert_nonnull(s->bus);

     dev = qvirtio_pci_device_find(s->bus, VIRTIO_ID_NET);
@@ -893,7 +893,7 @@ static void test_multiqueue(void)
     qtest_start(cmd);
     g_free(cmd);

-    bus = qpci_init_pc(NULL);
+    bus = qpci_init_pc(global_qtest, NULL);
     dev = virtio_net_pci_init(bus, PCI_SLOT);

     alloc = pc_alloc_init();
-- 
2.13.5

^ permalink raw reply related	[flat|nested] 75+ messages in thread

* [Qemu-devel] [PATCH v7 09/38] libqos: Track QTestState with QVirtioBus
  2017-09-11 17:19 [Qemu-devel] [PATCH v7 00/38] Preliminary libqtest cleanups Eric Blake
                   ` (7 preceding siblings ...)
  2017-09-11 17:19 ` [Qemu-devel] [PATCH v7 08/38] libqos: Track QTestState with QPCIBus Eric Blake
@ 2017-09-11 17:19 ` Eric Blake
  2017-09-12  7:21   ` Thomas Huth
  2017-09-11 17:19 ` [Qemu-devel] [PATCH v7 10/38] libqos: Move/rename qpci_unplug_acpi_device_test() to pci.c Eric Blake
                   ` (28 subsequent siblings)
  37 siblings, 1 reply; 75+ messages in thread
From: Eric Blake @ 2017-09-11 17:19 UTC (permalink / raw)
  To: qemu-devel; +Cc: armbru, pbonzini, thuth, Stefan Hajnoczi, open list:virtio-blk

When initializing a QVirtioDevice (which always has an associated
QVirtioBus), we want to track which QTestState to use for all
I/O processed through that bus and device.  Copy the paradigm
used for QPCIBus, and track the test state at the bus level; this
in turn requires a separate bus object per device (and associated
cleanup) rather than just sharing a const version of the dispatch
table.  Update all affected callers.

Likewise, let QVirtQueue trace back to its associated QVirtioDevice.

A later patch can then explicitly use the QTestState associated
with the bus rather than blindly relying on global_qtest.

Signed-off-by: Eric Blake <eblake@redhat.com>
---
 tests/libqos/virtio-mmio.h |  6 +++---
 tests/libqos/virtio-pci.h  |  2 --
 tests/libqos/virtio.h      |  7 ++++++-
 tests/libqos/virtio-mmio.c | 12 ++++++++++--
 tests/libqos/virtio-pci.c  |  6 ++++--
 tests/libqos/virtio.c      | 10 ++++++++++
 tests/virtio-blk-test.c    |  5 +++--
 7 files changed, 36 insertions(+), 12 deletions(-)

diff --git a/tests/libqos/virtio-mmio.h b/tests/libqos/virtio-mmio.h
index e3e52b9ce1..fe7fa636b8 100644
--- a/tests/libqos/virtio-mmio.h
+++ b/tests/libqos/virtio-mmio.h
@@ -39,8 +39,8 @@ typedef struct QVirtioMMIODevice {
     uint32_t features; /* As it cannot be read later, save it */
 } QVirtioMMIODevice;

-extern const QVirtioBus qvirtio_mmio;
-
-QVirtioMMIODevice *qvirtio_mmio_init_device(uint64_t addr, uint32_t page_size);
+QVirtioMMIODevice *qvirtio_mmio_init_device(QTestState *qts, uint64_t addr,
+                                            uint32_t page_size);
+void qvirtio_mmio_device_free(QVirtioMMIODevice *dev);

 #endif
diff --git a/tests/libqos/virtio-pci.h b/tests/libqos/virtio-pci.h
index 6ef19094cb..bcd0594245 100644
--- a/tests/libqos/virtio-pci.h
+++ b/tests/libqos/virtio-pci.h
@@ -29,8 +29,6 @@ typedef struct QVirtQueuePCI {
     uint32_t msix_data;
 } QVirtQueuePCI;

-extern const QVirtioBus qvirtio_pci;
-
 QVirtioPCIDevice *qvirtio_pci_device_find(QPCIBus *bus, uint16_t device_type);
 QVirtioPCIDevice *qvirtio_pci_device_find_slot(QPCIBus *bus,
                                                uint16_t device_type, int slot);
diff --git a/tests/libqos/virtio.h b/tests/libqos/virtio.h
index 8fbcd1869c..def46ad9bb 100644
--- a/tests/libqos/virtio.h
+++ b/tests/libqos/virtio.h
@@ -18,12 +18,13 @@
 typedef struct QVirtioBus QVirtioBus;

 typedef struct QVirtioDevice {
-    const QVirtioBus *bus;
+    QVirtioBus *bus;
     /* Device type */
     uint16_t device_type;
 } QVirtioDevice;

 typedef struct QVirtQueue {
+    QVirtioDevice *dev;
     uint64_t desc; /* This points to an array of struct vring_desc */
     uint64_t avail; /* This points to a struct vring_avail */
     uint64_t used; /* This points to a struct vring_used */
@@ -88,6 +89,8 @@ struct QVirtioBus {

     /* Notify changes in virtqueue */
     void (*virtqueue_kick)(QVirtioDevice *d, QVirtQueue *vq);
+
+    QTestState *qts;
 };

 static inline bool qvirtio_is_big_endian(QVirtioDevice *d)
@@ -103,6 +106,8 @@ static inline uint32_t qvring_size(uint32_t num, uint32_t align)
         + sizeof(uint16_t) * 3 + sizeof(struct vring_used_elem) * num;
 }

+QVirtioBus *qvirtio_init_bus(QTestState *qts, const QVirtioBus *base);
+
 uint8_t qvirtio_config_readb(QVirtioDevice *d, uint64_t addr);
 uint16_t qvirtio_config_readw(QVirtioDevice *d, uint64_t addr);
 uint32_t qvirtio_config_readl(QVirtioDevice *d, uint64_t addr);
diff --git a/tests/libqos/virtio-mmio.c b/tests/libqos/virtio-mmio.c
index 7aa8383338..12770319cd 100644
--- a/tests/libqos/virtio-mmio.c
+++ b/tests/libqos/virtio-mmio.c
@@ -131,6 +131,7 @@ static QVirtQueue *qvirtio_mmio_virtqueue_setup(QVirtioDevice *d,
     qvirtio_mmio_queue_select(d, index);
     writel(dev->addr + QVIRTIO_MMIO_QUEUE_ALIGN, dev->page_size);

+    vq->dev = d;
     vq->index = index;
     vq->size = qvirtio_mmio_get_queue_size(d);
     vq->free_head = 0;
@@ -187,7 +188,8 @@ const QVirtioBus qvirtio_mmio = {
     .virtqueue_kick = qvirtio_mmio_virtqueue_kick,
 };

-QVirtioMMIODevice *qvirtio_mmio_init_device(uint64_t addr, uint32_t page_size)
+QVirtioMMIODevice *qvirtio_mmio_init_device(QTestState *qts, uint64_t addr,
+                                            uint32_t page_size)
 {
     QVirtioMMIODevice *dev;
     uint32_t magic;
@@ -199,9 +201,15 @@ QVirtioMMIODevice *qvirtio_mmio_init_device(uint64_t addr, uint32_t page_size)
     dev->addr = addr;
     dev->page_size = page_size;
     dev->vdev.device_type = readl(addr + QVIRTIO_MMIO_DEVICE_ID);
-    dev->vdev.bus = &qvirtio_mmio;
+    dev->vdev.bus = qvirtio_init_bus(qts, &qvirtio_mmio);

     writel(addr + QVIRTIO_MMIO_GUEST_PAGE_SIZE, page_size);

     return dev;
 }
+
+void qvirtio_mmio_device_free(QVirtioMMIODevice *dev)
+{
+    g_free(dev->vdev.bus);
+    g_free(dev);
+}
diff --git a/tests/libqos/virtio-pci.c b/tests/libqos/virtio-pci.c
index 7ac15c04e1..6225e6df9b 100644
--- a/tests/libqos/virtio-pci.c
+++ b/tests/libqos/virtio-pci.c
@@ -31,6 +31,7 @@ typedef struct QVirtioPCIForeachData {

 void qvirtio_pci_device_free(QVirtioPCIDevice *dev)
 {
+    g_free(dev->vdev.bus);
     g_free(dev->pdev);
     g_free(dev);
 }
@@ -233,6 +234,7 @@ static QVirtQueue *qvirtio_pci_virtqueue_setup(QVirtioDevice *d,
     feat = qvirtio_pci_get_guest_features(d);

     qvirtio_pci_queue_select(d, index);
+    vqpci->vq.dev = d;
     vqpci->vq.index = index;
     vqpci->vq.size = qvirtio_pci_get_queue_size(d);
     vqpci->vq.free_head = 0;
@@ -315,7 +317,7 @@ QVirtioPCIDevice *qvirtio_pci_device_find(QPCIBus *bus, uint16_t device_type)
     qvirtio_pci_foreach(bus, device_type, false, 0,
                         qvirtio_pci_assign_device, &dev);

-    dev->vdev.bus = &qvirtio_pci;
+    dev->vdev.bus = qvirtio_init_bus(bus->qts, &qvirtio_pci);

     return dev;
 }
@@ -328,7 +330,7 @@ QVirtioPCIDevice *qvirtio_pci_device_find_slot(QPCIBus *bus,
     qvirtio_pci_foreach(bus, device_type, true, slot,
                         qvirtio_pci_assign_device, &dev);

-    dev->vdev.bus = &qvirtio_pci;
+    dev->vdev.bus = qvirtio_init_bus(bus->qts, &qvirtio_pci);

     return dev;
 }
diff --git a/tests/libqos/virtio.c b/tests/libqos/virtio.c
index 9880a6964e..2b7d9e62c4 100644
--- a/tests/libqos/virtio.c
+++ b/tests/libqos/virtio.c
@@ -13,6 +13,16 @@
 #include "standard-headers/linux/virtio_config.h"
 #include "standard-headers/linux/virtio_ring.h"

+QVirtioBus *qvirtio_init_bus(QTestState *qts, const QVirtioBus *base)
+{
+    QVirtioBus *bus = g_new0(QVirtioBus, 1);
+
+    assert(qts);
+    *bus = *base;
+    bus->qts = qts;
+    return bus;
+}
+
 uint8_t qvirtio_config_readb(QVirtioDevice *d, uint64_t addr)
 {
     return d->bus->config_readb(d, addr);
diff --git a/tests/virtio-blk-test.c b/tests/virtio-blk-test.c
index 0576cb16ba..ca4ee645b7 100644
--- a/tests/virtio-blk-test.c
+++ b/tests/virtio-blk-test.c
@@ -687,7 +687,8 @@ static void mmio_basic(void)

     arm_test_start();

-    dev = qvirtio_mmio_init_device(MMIO_DEV_BASE_ADDR, MMIO_PAGE_SIZE);
+    dev = qvirtio_mmio_init_device(global_qtest, MMIO_DEV_BASE_ADDR,
+                                   MMIO_PAGE_SIZE);
     g_assert(dev != NULL);
     g_assert_cmphex(dev->vdev.device_type, ==, VIRTIO_ID_BLOCK);

@@ -711,7 +712,7 @@ static void mmio_basic(void)

     /* End test */
     qvirtqueue_cleanup(dev->vdev.bus, vq, alloc);
-    g_free(dev);
+    qvirtio_mmio_device_free(dev);
     generic_alloc_uninit(alloc);
     test_end();
 }
-- 
2.13.5

^ permalink raw reply related	[flat|nested] 75+ messages in thread

* [Qemu-devel] [PATCH v7 10/38] libqos: Move/rename qpci_unplug_acpi_device_test() to pci.c
  2017-09-11 17:19 [Qemu-devel] [PATCH v7 00/38] Preliminary libqtest cleanups Eric Blake
                   ` (8 preceding siblings ...)
  2017-09-11 17:19 ` [Qemu-devel] [PATCH v7 09/38] libqos: Track QTestState with QVirtioBus Eric Blake
@ 2017-09-11 17:19 ` Eric Blake
  2017-09-12  7:29   ` Thomas Huth
  2017-09-11 17:19 ` [Qemu-devel] [PATCH v7 11/38] libqos: Use explicit QTestState for pci operations Eric Blake
                   ` (27 subsequent siblings)
  37 siblings, 1 reply; 75+ messages in thread
From: Eric Blake @ 2017-09-11 17:19 UTC (permalink / raw)
  To: qemu-devel
  Cc: armbru, pbonzini, thuth, Stefan Hajnoczi, Jason Wang, Amit Shah,
	open list:virtio-blk

Commit 2f8b2767 originally added qpci_plug_device_test() and
qpci_unplug_acpi_device_test() as a pair, both in pci-pc.c.
Later, commit cf716b31 moved one half of the pair to pci.c
when adding PPC64 support.  Keep the implementations of the
two functions together, and shorten the name to
qpci_unplug_device_test(), since all callers use the two
functions in tandem.

Signed-off-by: Eric Blake <eblake@redhat.com>
---
 tests/libqos/pci.h      |  2 +-
 tests/e1000e-test.c     |  2 +-
 tests/ivshmem-test.c    |  2 +-
 tests/libqos/pci-pc.c   | 23 -----------------------
 tests/libqos/pci.c      | 23 +++++++++++++++++++++++
 tests/virtio-blk-test.c |  2 +-
 tests/virtio-net-test.c |  2 +-
 tests/virtio-rng-test.c |  2 +-
 8 files changed, 29 insertions(+), 29 deletions(-)

diff --git a/tests/libqos/pci.h b/tests/libqos/pci.h
index 429c382282..fdda7eca6e 100644
--- a/tests/libqos/pci.h
+++ b/tests/libqos/pci.h
@@ -111,5 +111,5 @@ QPCIBar qpci_legacy_iomap(QPCIDevice *dev, uint16_t addr);

 void qpci_plug_device_test(const char *driver, const char *id,
                            uint8_t slot, const char *opts);
-void qpci_unplug_acpi_device_test(const char *id, uint8_t slot);
+void qpci_unplug_device_test(const char *id, uint8_t slot);
 #endif
diff --git a/tests/e1000e-test.c b/tests/e1000e-test.c
index d8085d944e..4c663a3019 100644
--- a/tests/e1000e-test.c
+++ b/tests/e1000e-test.c
@@ -461,7 +461,7 @@ static void test_e1000e_hotplug(gconstpointer data)
     qtest_start("-device e1000e");

     qpci_plug_device_test("e1000e", "e1000e_net", slot, NULL);
-    qpci_unplug_acpi_device_test("e1000e_net", slot);
+    qpci_unplug_device_test("e1000e_net", slot);

     qtest_end();
 }
diff --git a/tests/ivshmem-test.c b/tests/ivshmem-test.c
index 37763425ee..8c9ed6a568 100644
--- a/tests/ivshmem-test.c
+++ b/tests/ivshmem-test.c
@@ -427,7 +427,7 @@ static void test_ivshmem_hotplug(void)

     qpci_plug_device_test("ivshmem", "iv1", PCI_SLOT_HP, opts);
     if (strcmp(arch, "ppc64") != 0) {
-        qpci_unplug_acpi_device_test("iv1", PCI_SLOT_HP);
+        qpci_unplug_device_test("iv1", PCI_SLOT_HP);
     }

     qtest_end();
diff --git a/tests/libqos/pci-pc.c b/tests/libqos/pci-pc.c
index e267fd1a44..6305d142a5 100644
--- a/tests/libqos/pci-pc.c
+++ b/tests/libqos/pci-pc.c
@@ -19,9 +19,6 @@
 #include "qemu-common.h"


-#define ACPI_PCIHP_ADDR         0xae00
-#define PCI_EJ_BASE             0x0008
-
 typedef struct QPCIBusPC
 {
     QPCIBus bus;
@@ -156,23 +153,3 @@ void qpci_free_pc(QPCIBus *bus)

     g_free(s);
 }
-
-void qpci_unplug_acpi_device_test(const char *id, uint8_t slot)
-{
-    QDict *response;
-    char *cmd;
-
-    cmd = g_strdup_printf("{'execute': 'device_del',"
-                          " 'arguments': {"
-                          "   'id': '%s'"
-                          "}}", id);
-    response = qmp(cmd);
-    g_free(cmd);
-    g_assert(response);
-    g_assert(!qdict_haskey(response, "error"));
-    QDECREF(response);
-
-    outb(ACPI_PCIHP_ADDR + PCI_EJ_BASE, 1 << slot);
-
-    qmp_eventwait("DEVICE_DELETED");
-}
diff --git a/tests/libqos/pci.c b/tests/libqos/pci.c
index 2dcdeade2a..9f36ec73ef 100644
--- a/tests/libqos/pci.c
+++ b/tests/libqos/pci.c
@@ -16,6 +16,9 @@
 #include "hw/pci/pci_regs.h"
 #include "qemu/host-utils.h"

+#define ACPI_PCIHP_ADDR         0xae00
+#define PCI_EJ_BASE             0x0008
+
 void qpci_device_foreach(QPCIBus *bus, int vendor_id, int device_id,
                          void (*func)(QPCIDevice *dev, int devfn, void *data),
                          void *data)
@@ -412,3 +415,23 @@ void qpci_plug_device_test(const char *driver, const char *id,
     g_assert(!qdict_haskey(response, "error"));
     QDECREF(response);
 }
+
+void qpci_unplug_device_test(const char *id, uint8_t slot)
+{
+    QDict *response;
+    char *cmd;
+
+    cmd = g_strdup_printf("{'execute': 'device_del',"
+                          " 'arguments': {"
+                          "   'id': '%s'"
+                          "}}", id);
+    response = qmp(cmd);
+    g_free(cmd);
+    g_assert(response);
+    g_assert(!qdict_haskey(response, "error"));
+    QDECREF(response);
+
+    outb(ACPI_PCIHP_ADDR + PCI_EJ_BASE, 1 << slot);
+
+    qmp_eventwait("DEVICE_DELETED");
+}
diff --git a/tests/virtio-blk-test.c b/tests/virtio-blk-test.c
index ca4ee645b7..8441ab91ed 100644
--- a/tests/virtio-blk-test.c
+++ b/tests/virtio-blk-test.c
@@ -672,7 +672,7 @@ static void pci_hotplug(void)

     /* unplug secondary disk */
     if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
-        qpci_unplug_acpi_device_test("drv1", PCI_SLOT_HP);
+        qpci_unplug_device_test("drv1", PCI_SLOT_HP);
     }
     qtest_shutdown(qs);
 }
diff --git a/tests/virtio-net-test.c b/tests/virtio-net-test.c
index 635b942c36..24e0f774f0 100644
--- a/tests/virtio-net-test.c
+++ b/tests/virtio-net-test.c
@@ -248,7 +248,7 @@ static void hotplug(void)
     qpci_plug_device_test("virtio-net-pci", "net1", PCI_SLOT_HP, NULL);

     if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
-        qpci_unplug_acpi_device_test("net1", PCI_SLOT_HP);
+        qpci_unplug_device_test("net1", PCI_SLOT_HP);
     }

     test_end();
diff --git a/tests/virtio-rng-test.c b/tests/virtio-rng-test.c
index dcecf77463..fc4a36474b 100644
--- a/tests/virtio-rng-test.c
+++ b/tests/virtio-rng-test.c
@@ -25,7 +25,7 @@ static void hotplug(void)
     qpci_plug_device_test("virtio-rng-pci", "rng1", PCI_SLOT_HP, NULL);

     if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
-        qpci_unplug_acpi_device_test("rng1", PCI_SLOT_HP);
+        qpci_unplug_device_test("rng1", PCI_SLOT_HP);
     }
 }

-- 
2.13.5

^ permalink raw reply related	[flat|nested] 75+ messages in thread

* [Qemu-devel] [PATCH v7 11/38] libqos: Use explicit QTestState for pci operations
  2017-09-11 17:19 [Qemu-devel] [PATCH v7 00/38] Preliminary libqtest cleanups Eric Blake
                   ` (9 preceding siblings ...)
  2017-09-11 17:19 ` [Qemu-devel] [PATCH v7 10/38] libqos: Move/rename qpci_unplug_acpi_device_test() to pci.c Eric Blake
@ 2017-09-11 17:19 ` Eric Blake
  2017-09-11 17:19 ` [Qemu-devel] [PATCH v7 12/38] libqos: Use explicit QTestState for virtio operations Eric Blake
                   ` (26 subsequent siblings)
  37 siblings, 0 replies; 75+ messages in thread
From: Eric Blake @ 2017-09-11 17:19 UTC (permalink / raw)
  To: qemu-devel
  Cc: armbru, pbonzini, thuth, David Gibson, Alexander Graf,
	Stefan Hajnoczi, Jason Wang, Amit Shah, open list:sPAPR,
	open list:virtio-blk

Now that the QPCIBus tracks QTestState, use that state instead
of an implicit reliance on global_qtest.

Pass an explicit QTestState to the remaining few functions in
the same file that don't have direct access to QPCIBus, and
update all callers.

Signed-off-by: Eric Blake <eblake@redhat.com>

---
v7: more sites with explicit state, drop R-b
---
 tests/libqos/pci.h       |  4 ++--
 tests/e1000e-test.c      |  4 ++--
 tests/ivshmem-test.c     |  4 ++--
 tests/libqos/pci-pc.c    | 47 ++++++++++++++++++++++++-----------------------
 tests/libqos/pci-spapr.c | 20 ++++++++++----------
 tests/libqos/pci.c       | 19 +++++++------------
 tests/virtio-blk-test.c  |  4 ++--
 tests/virtio-net-test.c  |  5 +++--
 tests/virtio-rng-test.c  |  5 +++--
 9 files changed, 55 insertions(+), 57 deletions(-)

diff --git a/tests/libqos/pci.h b/tests/libqos/pci.h
index fdda7eca6e..2943186819 100644
--- a/tests/libqos/pci.h
+++ b/tests/libqos/pci.h
@@ -109,7 +109,7 @@ QPCIBar qpci_iomap(QPCIDevice *dev, int barno, uint64_t *sizeptr);
 void qpci_iounmap(QPCIDevice *dev, QPCIBar addr);
 QPCIBar qpci_legacy_iomap(QPCIDevice *dev, uint16_t addr);

-void qpci_plug_device_test(const char *driver, const char *id,
+void qpci_plug_device_test(QTestState *qts, const char *driver, const char *id,
                            uint8_t slot, const char *opts);
-void qpci_unplug_device_test(const char *id, uint8_t slot);
+void qpci_unplug_device_test(QTestState *qts, const char *id, uint8_t slot);
 #endif
diff --git a/tests/e1000e-test.c b/tests/e1000e-test.c
index 4c663a3019..3cc081941e 100644
--- a/tests/e1000e-test.c
+++ b/tests/e1000e-test.c
@@ -460,8 +460,8 @@ static void test_e1000e_hotplug(gconstpointer data)

     qtest_start("-device e1000e");

-    qpci_plug_device_test("e1000e", "e1000e_net", slot, NULL);
-    qpci_unplug_device_test("e1000e_net", slot);
+    qpci_plug_device_test(global_qtest, "e1000e", "e1000e_net", slot, NULL);
+    qpci_unplug_device_test(global_qtest, "e1000e_net", slot);

     qtest_end();
 }
diff --git a/tests/ivshmem-test.c b/tests/ivshmem-test.c
index 8c9ed6a568..d35c922254 100644
--- a/tests/ivshmem-test.c
+++ b/tests/ivshmem-test.c
@@ -425,9 +425,9 @@ static void test_ivshmem_hotplug(void)

     opts = g_strdup_printf("'shm': '%s', 'size': '1M'", tmpshm);

-    qpci_plug_device_test("ivshmem", "iv1", PCI_SLOT_HP, opts);
+    qpci_plug_device_test(global_qtest, "ivshmem", "iv1", PCI_SLOT_HP, opts);
     if (strcmp(arch, "ppc64") != 0) {
-        qpci_unplug_device_test("iv1", PCI_SLOT_HP);
+        qpci_unplug_device_test(global_qtest, "iv1", PCI_SLOT_HP);
     }

     qtest_end();
diff --git a/tests/libqos/pci-pc.c b/tests/libqos/pci-pc.c
index 6305d142a5..e5af083f0c 100644
--- a/tests/libqos/pci-pc.c
+++ b/tests/libqos/pci-pc.c
@@ -26,90 +26,91 @@ typedef struct QPCIBusPC

 static uint8_t qpci_pc_pio_readb(QPCIBus *bus, uint32_t addr)
 {
-    return inb(addr);
+    return qtest_inb(bus->qts, addr);
 }

 static void qpci_pc_pio_writeb(QPCIBus *bus, uint32_t addr, uint8_t val)
 {
-    outb(addr, val);
+    qtest_outb(bus->qts, addr, val);
 }

 static uint16_t qpci_pc_pio_readw(QPCIBus *bus, uint32_t addr)
 {
-    return inw(addr);
+    return qtest_inw(bus->qts, addr);
 }

 static void qpci_pc_pio_writew(QPCIBus *bus, uint32_t addr, uint16_t val)
 {
-    outw(addr, val);
+    qtest_outw(bus->qts, addr, val);
 }

 static uint32_t qpci_pc_pio_readl(QPCIBus *bus, uint32_t addr)
 {
-    return inl(addr);
+    return qtest_inl(bus->qts, addr);
 }

 static void qpci_pc_pio_writel(QPCIBus *bus, uint32_t addr, uint32_t val)
 {
-    outl(addr, val);
+    qtest_outl(bus->qts, addr, val);
 }

 static uint64_t qpci_pc_pio_readq(QPCIBus *bus, uint32_t addr)
 {
-    return (uint64_t)inl(addr) + ((uint64_t)inl(addr + 4) << 32);
+    return (uint64_t)qtest_inl(bus->qts, addr) +
+        ((uint64_t)qtest_inl(bus->qts, addr + 4) << 32);
 }

 static void qpci_pc_pio_writeq(QPCIBus *bus, uint32_t addr, uint64_t val)
 {
-    outl(addr, val & 0xffffffff);
-    outl(addr + 4, val >> 32);
+    qtest_outl(bus->qts, addr, val & 0xffffffff);
+    qtest_outl(bus->qts, addr + 4, val >> 32);
 }

 static void qpci_pc_memread(QPCIBus *bus, uint32_t addr, void *buf, size_t len)
 {
-    memread(addr, buf, len);
+    qtest_memread(bus->qts, addr, buf, len);
 }

 static void qpci_pc_memwrite(QPCIBus *bus, uint32_t addr,
                              const void *buf, size_t len)
 {
-    memwrite(addr, buf, len);
+    qtest_memwrite(bus->qts, addr, buf, len);
 }

 static uint8_t qpci_pc_config_readb(QPCIBus *bus, int devfn, uint8_t offset)
 {
-    outl(0xcf8, (1U << 31) | (devfn << 8) | offset);
-    return inb(0xcfc);
+    qtest_outl(bus->qts, 0xcf8, (1U << 31) | (devfn << 8) | offset);
+    return qtest_inb(bus->qts, 0xcfc);
 }

 static uint16_t qpci_pc_config_readw(QPCIBus *bus, int devfn, uint8_t offset)
 {
-    outl(0xcf8, (1U << 31) | (devfn << 8) | offset);
-    return inw(0xcfc);
+    qtest_outl(bus->qts, 0xcf8, (1U << 31) | (devfn << 8) | offset);
+    return qtest_inw(bus->qts, 0xcfc);
 }

 static uint32_t qpci_pc_config_readl(QPCIBus *bus, int devfn, uint8_t offset)
 {
-    outl(0xcf8, (1U << 31) | (devfn << 8) | offset);
-    return inl(0xcfc);
+    qtest_outl(bus->qts, 0xcf8, (1U << 31) | (devfn << 8) | offset);
+    return qtest_inl(bus->qts, 0xcfc);
 }

 static void qpci_pc_config_writeb(QPCIBus *bus, int devfn, uint8_t offset, uint8_t value)
 {
-    outl(0xcf8, (1U << 31) | (devfn << 8) | offset);
-    outb(0xcfc, value);
+    qtest_outl(bus->qts, 0xcf8, (1U << 31) | (devfn << 8) | offset);
+    qtest_outb(bus->qts, 0xcfc, value);
 }

 static void qpci_pc_config_writew(QPCIBus *bus, int devfn, uint8_t offset, uint16_t value)
 {
-    outl(0xcf8, (1U << 31) | (devfn << 8) | offset);
-    outw(0xcfc, value);
+    qtest_outl(bus->qts, 0xcf8, (1U << 31) | (devfn << 8) | offset);
+    qtest_outw(bus->qts, 0xcfc, value);
 }

 static void qpci_pc_config_writel(QPCIBus *bus, int devfn, uint8_t offset, uint32_t value)
 {
-    outl(0xcf8, (1U << 31) | (devfn << 8) | offset);
-    outl(0xcfc, value);
+    qtest_outl(bus->qts, 0xcf8, (1U << 31) | (devfn << 8) | offset);
+    qtest_outl(bus->qts, 0xcfc, value);
 }

 QPCIBus *qpci_init_pc(QTestState *qts, QGuestAllocator *alloc)
diff --git a/tests/libqos/pci-spapr.c b/tests/libqos/pci-spapr.c
index ef52fcbe2b..9121913af6 100644
--- a/tests/libqos/pci-spapr.c
+++ b/tests/libqos/pci-spapr.c
@@ -45,63 +45,63 @@ typedef struct QPCIBusSPAPR {
 static uint8_t qpci_spapr_pio_readb(QPCIBus *bus, uint32_t addr)
 {
     QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
-    return readb(s->pio_cpu_base + addr);
+    return qtest_readb(bus->qts, s->pio_cpu_base + addr);
 }

 static void qpci_spapr_pio_writeb(QPCIBus *bus, uint32_t addr, uint8_t val)
 {
     QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
-    writeb(s->pio_cpu_base + addr, val);
+    qtest_writeb(bus->qts, s->pio_cpu_base + addr, val);
 }

 static uint16_t qpci_spapr_pio_readw(QPCIBus *bus, uint32_t addr)
 {
     QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
-    return bswap16(readw(s->pio_cpu_base + addr));
+    return bswap16(qtest_readw(bus->qts, s->pio_cpu_base + addr));
 }

 static void qpci_spapr_pio_writew(QPCIBus *bus, uint32_t addr, uint16_t val)
 {
     QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
-    writew(s->pio_cpu_base + addr, bswap16(val));
+    qtest_writew(bus->qts, s->pio_cpu_base + addr, bswap16(val));
 }

 static uint32_t qpci_spapr_pio_readl(QPCIBus *bus, uint32_t addr)
 {
     QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
-    return bswap32(readl(s->pio_cpu_base + addr));
+    return bswap32(qtest_readl(bus->qts, s->pio_cpu_base + addr));
 }

 static void qpci_spapr_pio_writel(QPCIBus *bus, uint32_t addr, uint32_t val)
 {
     QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
-    writel(s->pio_cpu_base + addr, bswap32(val));
+    qtest_writel(bus->qts, s->pio_cpu_base + addr, bswap32(val));
 }

 static uint64_t qpci_spapr_pio_readq(QPCIBus *bus, uint32_t addr)
 {
     QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
-    return bswap64(readq(s->pio_cpu_base + addr));
+    return bswap64(qtest_readq(bus->qts, s->pio_cpu_base + addr));
 }

 static void qpci_spapr_pio_writeq(QPCIBus *bus, uint32_t addr, uint64_t val)
 {
     QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
-    writeq(s->pio_cpu_base + addr, bswap64(val));
+    qtest_writeq(bus->qts, s->pio_cpu_base + addr, bswap64(val));
 }

 static void qpci_spapr_memread(QPCIBus *bus, uint32_t addr,
                                void *buf, size_t len)
 {
     QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
-    memread(s->mmio32_cpu_base + addr, buf, len);
+    qtest_memread(bus->qts, s->mmio32_cpu_base + addr, buf, len);
 }

 static void qpci_spapr_memwrite(QPCIBus *bus, uint32_t addr,
                                 const void *buf, size_t len)
 {
     QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
-    memwrite(s->mmio32_cpu_base + addr, buf, len);
+    qtest_memwrite(bus->qts, s->mmio32_cpu_base + addr, buf, len);
 }

 static uint8_t qpci_spapr_config_readb(QPCIBus *bus, int devfn, uint8_t offset)
diff --git a/tests/libqos/pci.c b/tests/libqos/pci.c
index 9f36ec73ef..d3ee21fdfd 100644
--- a/tests/libqos/pci.c
+++ b/tests/libqos/pci.c
@@ -394,7 +394,7 @@ QPCIBar qpci_legacy_iomap(QPCIDevice *dev, uint16_t addr)
     return bar;
 }

-void qpci_plug_device_test(const char *driver, const char *id,
+void qpci_plug_device_test(QTestState *qts, const char *driver, const char *id,
                            uint8_t slot, const char *opts)
 {
     QDict *response;
@@ -409,29 +409,24 @@ void qpci_plug_device_test(const char *driver, const char *id,
                           "}}", driver, slot,
                           opts ? opts : "", opts ? "," : "",
                           id);
-    response = qmp(cmd);
+    response = qtest_qmp(qts, cmd);
     g_free(cmd);
     g_assert(response);
     g_assert(!qdict_haskey(response, "error"));
     QDECREF(response);
 }

-void qpci_unplug_device_test(const char *id, uint8_t slot)
+void qpci_unplug_device_test(QTestState *qts, const char *id, uint8_t slot)
 {
     QDict *response;
-    char *cmd;

-    cmd = g_strdup_printf("{'execute': 'device_del',"
-                          " 'arguments': {"
-                          "   'id': '%s'"
-                          "}}", id);
-    response = qmp(cmd);
-    g_free(cmd);
+    response = qtest_qmp(qts, "{'execute': 'device_del',"
+                         " 'arguments': { 'id': %s }}", id);
     g_assert(response);
     g_assert(!qdict_haskey(response, "error"));
     QDECREF(response);

-    outb(ACPI_PCIHP_ADDR + PCI_EJ_BASE, 1 << slot);
+    qtest_outb(qts, ACPI_PCIHP_ADDR + PCI_EJ_BASE, 1 << slot);

-    qmp_eventwait("DEVICE_DELETED");
+    qtest_qmp_eventwait(qts, "DEVICE_DELETED");
 }
diff --git a/tests/virtio-blk-test.c b/tests/virtio-blk-test.c
index 8441ab91ed..2d38c49bfe 100644
--- a/tests/virtio-blk-test.c
+++ b/tests/virtio-blk-test.c
@@ -662,7 +662,7 @@ static void pci_hotplug(void)
     qs = pci_test_start();

     /* plug secondary disk */
-    qpci_plug_device_test("virtio-blk-pci", "drv1", PCI_SLOT_HP,
+    qpci_plug_device_test(global_qtest, "virtio-blk-pci", "drv1", PCI_SLOT_HP,
                           "'drive': 'drive1'");

     dev = virtio_blk_pci_init(qs->pcibus, PCI_SLOT_HP);
@@ -672,7 +672,7 @@ static void pci_hotplug(void)

     /* unplug secondary disk */
     if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
-        qpci_unplug_device_test("drv1", PCI_SLOT_HP);
+        qpci_unplug_device_test(global_qtest, "drv1", PCI_SLOT_HP);
     }
     qtest_shutdown(qs);
 }
diff --git a/tests/virtio-net-test.c b/tests/virtio-net-test.c
index 24e0f774f0..8b43d3dd65 100644
--- a/tests/virtio-net-test.c
+++ b/tests/virtio-net-test.c
@@ -245,10 +245,11 @@ static void hotplug(void)

     qtest_start("-device virtio-net-pci");

-    qpci_plug_device_test("virtio-net-pci", "net1", PCI_SLOT_HP, NULL);
+    qpci_plug_device_test(global_qtest, "virtio-net-pci", "net1",
+                          PCI_SLOT_HP, NULL);

     if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
-        qpci_unplug_device_test("net1", PCI_SLOT_HP);
+        qpci_unplug_device_test(global_qtest, "net1", PCI_SLOT_HP);
     }

     test_end();
diff --git a/tests/virtio-rng-test.c b/tests/virtio-rng-test.c
index fc4a36474b..0e21125cb4 100644
--- a/tests/virtio-rng-test.c
+++ b/tests/virtio-rng-test.c
@@ -22,10 +22,11 @@ static void hotplug(void)
 {
     const char *arch = qtest_get_arch();

-    qpci_plug_device_test("virtio-rng-pci", "rng1", PCI_SLOT_HP, NULL);
+    qpci_plug_device_test(global_qtest, "virtio-rng-pci", "rng1",
+                          PCI_SLOT_HP, NULL);

     if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
-        qpci_unplug_device_test("rng1", PCI_SLOT_HP);
+        qpci_unplug_device_test(global_qtest, "rng1", PCI_SLOT_HP);
     }
 }

-- 
2.13.5

^ permalink raw reply related	[flat|nested] 75+ messages in thread

* [Qemu-devel] [PATCH v7 12/38] libqos: Use explicit QTestState for virtio operations
  2017-09-11 17:19 [Qemu-devel] [PATCH v7 00/38] Preliminary libqtest cleanups Eric Blake
                   ` (10 preceding siblings ...)
  2017-09-11 17:19 ` [Qemu-devel] [PATCH v7 11/38] libqos: Use explicit QTestState for pci operations Eric Blake
@ 2017-09-11 17:19 ` Eric Blake
  2017-09-12  7:38   ` Thomas Huth
  2017-09-11 17:19 ` [Qemu-devel] [PATCH v7 13/38] libqos: Use explicit QTestState for fw_cfg operations Eric Blake
                   ` (25 subsequent siblings)
  37 siblings, 1 reply; 75+ messages in thread
From: Eric Blake @ 2017-09-11 17:19 UTC (permalink / raw)
  To: qemu-devel; +Cc: armbru, pbonzini, thuth, Stefan Hajnoczi, open list:virtio-blk

Now that QVirtioDevice and QVirtQueue point back to QVirtioBus,
we can reuse the explicit QTestState stored there rather than
relying on implicit global_qtest.  We also have to pass QTestState
through a few functions that can't trace back through
QVirtioDevice, and update those callers.

Drop some useless casts while touching things.

Signed-off-by: Eric Blake <eblake@redhat.com>
---
 tests/libqos/virtio.h      |  6 ++--
 tests/libqos/virtio-mmio.c | 57 ++++++++++++++++++-------------
 tests/libqos/virtio-pci.c  |  8 ++---
 tests/libqos/virtio.c      | 84 ++++++++++++++++++++++++++--------------------
 tests/virtio-blk-test.c    | 11 +++---
 5 files changed, 94 insertions(+), 72 deletions(-)

diff --git a/tests/libqos/virtio.h b/tests/libqos/virtio.h
index def46ad9bb..cc57b77de8 100644
--- a/tests/libqos/virtio.h
+++ b/tests/libqos/virtio.h
@@ -96,7 +96,7 @@ struct QVirtioBus {
 static inline bool qvirtio_is_big_endian(QVirtioDevice *d)
 {
     /* FIXME: virtio 1.0 is always little-endian */
-    return qtest_big_endian(global_qtest);
+    return qtest_big_endian(d->bus->qts);
 }

 static inline uint32_t qvring_size(uint32_t num, uint32_t align)
@@ -139,8 +139,8 @@ void qvirtqueue_cleanup(const QVirtioBus *bus, QVirtQueue *vq,
 void qvring_init(const QGuestAllocator *alloc, QVirtQueue *vq, uint64_t addr);
 QVRingIndirectDesc *qvring_indirect_desc_setup(QVirtioDevice *d,
                                         QGuestAllocator *alloc, uint16_t elem);
-void qvring_indirect_desc_add(QVRingIndirectDesc *indirect, uint64_t data,
-                                                    uint32_t len, bool write);
+void qvring_indirect_desc_add(QTestState *qts, QVRingIndirectDesc *indirect,
+                              uint64_t data, uint32_t len, bool write);
 uint32_t qvirtqueue_add(QVirtQueue *vq, uint64_t data, uint32_t len, bool write,
                                                                     bool next);
 uint32_t qvirtqueue_add_indirect(QVirtQueue *vq, QVRingIndirectDesc *indirect);
diff --git a/tests/libqos/virtio-mmio.c b/tests/libqos/virtio-mmio.c
index 12770319cd..8d256f6ac9 100644
--- a/tests/libqos/virtio-mmio.c
+++ b/tests/libqos/virtio-mmio.c
@@ -18,40 +18,45 @@
 static uint8_t qvirtio_mmio_config_readb(QVirtioDevice *d, uint64_t off)
 {
     QVirtioMMIODevice *dev = (QVirtioMMIODevice *)d;
-    return readb(dev->addr + QVIRTIO_MMIO_DEVICE_SPECIFIC + off);
+    return qtest_readb(d->bus->qts,
+                       dev->addr + QVIRTIO_MMIO_DEVICE_SPECIFIC + off);
 }

 static uint16_t qvirtio_mmio_config_readw(QVirtioDevice *d, uint64_t off)
 {
     QVirtioMMIODevice *dev = (QVirtioMMIODevice *)d;
-    return readw(dev->addr + QVIRTIO_MMIO_DEVICE_SPECIFIC + off);
+    return qtest_readw(d->bus->qts,
+                       dev->addr + QVIRTIO_MMIO_DEVICE_SPECIFIC + off);
 }

 static uint32_t qvirtio_mmio_config_readl(QVirtioDevice *d, uint64_t off)
 {
     QVirtioMMIODevice *dev = (QVirtioMMIODevice *)d;
-    return readl(dev->addr + QVIRTIO_MMIO_DEVICE_SPECIFIC + off);
+    return qtest_readl(d->bus->qts,
+                       dev->addr + QVIRTIO_MMIO_DEVICE_SPECIFIC + off);
 }

 static uint64_t qvirtio_mmio_config_readq(QVirtioDevice *d, uint64_t off)
 {
     QVirtioMMIODevice *dev = (QVirtioMMIODevice *)d;
-    return readq(dev->addr + QVIRTIO_MMIO_DEVICE_SPECIFIC + off);
+    return qtest_readq(d->bus->qts,
+                       dev->addr + QVIRTIO_MMIO_DEVICE_SPECIFIC + off);
 }

 static uint32_t qvirtio_mmio_get_features(QVirtioDevice *d)
 {
     QVirtioMMIODevice *dev = (QVirtioMMIODevice *)d;
-    writel(dev->addr + QVIRTIO_MMIO_HOST_FEATURES_SEL, 0);
-    return readl(dev->addr + QVIRTIO_MMIO_HOST_FEATURES);
+    qtest_writel(d->bus->qts, dev->addr + QVIRTIO_MMIO_HOST_FEATURES_SEL, 0);
+    return qtest_readl(d->bus->qts, dev->addr + QVIRTIO_MMIO_HOST_FEATURES);
 }

 static void qvirtio_mmio_set_features(QVirtioDevice *d, uint32_t features)
 {
     QVirtioMMIODevice *dev = (QVirtioMMIODevice *)d;
     dev->features = features;
-    writel(dev->addr + QVIRTIO_MMIO_GUEST_FEATURES_SEL, 0);
-    writel(dev->addr + QVIRTIO_MMIO_GUEST_FEATURES, features);
+    qtest_writel(d->bus->qts, dev->addr + QVIRTIO_MMIO_GUEST_FEATURES_SEL, 0);
+    qtest_writel(d->bus->qts, dev->addr + QVIRTIO_MMIO_GUEST_FEATURES,
+                 features);
 }

 static uint32_t qvirtio_mmio_get_guest_features(QVirtioDevice *d)
@@ -63,13 +68,13 @@ static uint32_t qvirtio_mmio_get_guest_features(QVirtioDevice *d)
 static uint8_t qvirtio_mmio_get_status(QVirtioDevice *d)
 {
     QVirtioMMIODevice *dev = (QVirtioMMIODevice *)d;
-    return (uint8_t)readl(dev->addr + QVIRTIO_MMIO_DEVICE_STATUS);
+    return qtest_readl(d->bus->qts, dev->addr + QVIRTIO_MMIO_DEVICE_STATUS);
 }

 static void qvirtio_mmio_set_status(QVirtioDevice *d, uint8_t status)
 {
     QVirtioMMIODevice *dev = (QVirtioMMIODevice *)d;
-    writel(dev->addr + QVIRTIO_MMIO_DEVICE_STATUS, (uint32_t)status);
+    qtest_writel(d->bus->qts, dev->addr + QVIRTIO_MMIO_DEVICE_STATUS, status);
 }

 static bool qvirtio_mmio_get_queue_isr_status(QVirtioDevice *d, QVirtQueue *vq)
@@ -77,9 +82,10 @@ static bool qvirtio_mmio_get_queue_isr_status(QVirtioDevice *d, QVirtQueue *vq)
     QVirtioMMIODevice *dev = (QVirtioMMIODevice *)d;
     uint32_t isr;

-    isr = readl(dev->addr + QVIRTIO_MMIO_INTERRUPT_STATUS) & 1;
+    isr = qtest_readl(d->bus->qts,
+                      dev->addr + QVIRTIO_MMIO_INTERRUPT_STATUS) & 1;
     if (isr != 0) {
-        writel(dev->addr + QVIRTIO_MMIO_INTERRUPT_ACK, 1);
+        qtest_writel(d->bus->qts, dev->addr + QVIRTIO_MMIO_INTERRUPT_ACK, 1);
         return true;
     }

@@ -91,9 +97,10 @@ static bool qvirtio_mmio_get_config_isr_status(QVirtioDevice *d)
     QVirtioMMIODevice *dev = (QVirtioMMIODevice *)d;
     uint32_t isr;

-    isr = readl(dev->addr + QVIRTIO_MMIO_INTERRUPT_STATUS) & 2;
+    isr = qtest_readl(d->bus->qts,
+                      dev->addr + QVIRTIO_MMIO_INTERRUPT_STATUS) & 2;
     if (isr != 0) {
-        writel(dev->addr + QVIRTIO_MMIO_INTERRUPT_ACK, 2);
+        qtest_writel(d->bus->qts, dev->addr + QVIRTIO_MMIO_INTERRUPT_ACK, 2);
         return true;
     }

@@ -103,21 +110,22 @@ static bool qvirtio_mmio_get_config_isr_status(QVirtioDevice *d)
 static void qvirtio_mmio_queue_select(QVirtioDevice *d, uint16_t index)
 {
     QVirtioMMIODevice *dev = (QVirtioMMIODevice *)d;
-    writel(dev->addr + QVIRTIO_MMIO_QUEUE_SEL, (uint32_t)index);
+    qtest_writel(d->bus->qts, dev->addr + QVIRTIO_MMIO_QUEUE_SEL, index);

-    g_assert_cmphex(readl(dev->addr + QVIRTIO_MMIO_QUEUE_PFN), ==, 0);
+    g_assert_cmphex(qtest_readl(d->bus->qts,
+                                dev->addr + QVIRTIO_MMIO_QUEUE_PFN), ==, 0);
 }

 static uint16_t qvirtio_mmio_get_queue_size(QVirtioDevice *d)
 {
     QVirtioMMIODevice *dev = (QVirtioMMIODevice *)d;
-    return (uint16_t)readl(dev->addr + QVIRTIO_MMIO_QUEUE_NUM_MAX);
+    return qtest_readl(d->bus->qts, dev->addr + QVIRTIO_MMIO_QUEUE_NUM_MAX);
 }

 static void qvirtio_mmio_set_queue_address(QVirtioDevice *d, uint32_t pfn)
 {
     QVirtioMMIODevice *dev = (QVirtioMMIODevice *)d;
-    writel(dev->addr + QVIRTIO_MMIO_QUEUE_PFN, pfn);
+    qtest_writel(d->bus->qts, dev->addr + QVIRTIO_MMIO_QUEUE_PFN, pfn);
 }

 static QVirtQueue *qvirtio_mmio_virtqueue_setup(QVirtioDevice *d,
@@ -129,7 +137,8 @@ static QVirtQueue *qvirtio_mmio_virtqueue_setup(QVirtioDevice *d,

     vq = g_malloc0(sizeof(*vq));
     qvirtio_mmio_queue_select(d, index);
-    writel(dev->addr + QVIRTIO_MMIO_QUEUE_ALIGN, dev->page_size);
+    qtest_writel(d->bus->qts, dev->addr + QVIRTIO_MMIO_QUEUE_ALIGN,
+                 dev->page_size);

     vq->dev = d;
     vq->index = index;
@@ -140,7 +149,7 @@ static QVirtQueue *qvirtio_mmio_virtqueue_setup(QVirtioDevice *d,
     vq->indirect = (dev->features & (1u << VIRTIO_RING_F_INDIRECT_DESC)) != 0;
     vq->event = (dev->features & (1u << VIRTIO_RING_F_EVENT_IDX)) != 0;

-    writel(dev->addr + QVIRTIO_MMIO_QUEUE_NUM, vq->size);
+    qtest_writel(d->bus->qts, dev->addr + QVIRTIO_MMIO_QUEUE_NUM, vq->size);

     /* Check different than 0 */
     g_assert_cmpint(vq->size, !=, 0);
@@ -165,7 +174,7 @@ static void qvirtio_mmio_virtqueue_cleanup(QVirtQueue *vq,
 static void qvirtio_mmio_virtqueue_kick(QVirtioDevice *d, QVirtQueue *vq)
 {
     QVirtioMMIODevice *dev = (QVirtioMMIODevice *)d;
-    writel(dev->addr + QVIRTIO_MMIO_QUEUE_NOTIFY, vq->index);
+    qtest_writel(d->bus->qts, dev->addr + QVIRTIO_MMIO_QUEUE_NOTIFY, vq->index);
 }

 const QVirtioBus qvirtio_mmio = {
@@ -195,15 +204,15 @@ QVirtioMMIODevice *qvirtio_mmio_init_device(QTestState *qts, uint64_t addr,
     uint32_t magic;
     dev = g_malloc0(sizeof(*dev));

-    magic = readl(addr + QVIRTIO_MMIO_MAGIC_VALUE);
+    magic = qtest_readl(qts, addr + QVIRTIO_MMIO_MAGIC_VALUE);
     g_assert(magic == ('v' | 'i' << 8 | 'r' << 16 | 't' << 24));

     dev->addr = addr;
     dev->page_size = page_size;
-    dev->vdev.device_type = readl(addr + QVIRTIO_MMIO_DEVICE_ID);
+    dev->vdev.device_type = qtest_readl(qts, addr + QVIRTIO_MMIO_DEVICE_ID);
     dev->vdev.bus = qvirtio_init_bus(qts, &qvirtio_mmio);

-    writel(addr + QVIRTIO_MMIO_GUEST_PAGE_SIZE, page_size);
+    qtest_writel(qts, addr + QVIRTIO_MMIO_GUEST_PAGE_SIZE, page_size);

     return dev;
 }
diff --git a/tests/libqos/virtio-pci.c b/tests/libqos/virtio-pci.c
index 6225e6df9b..a7b17b3ba2 100644
--- a/tests/libqos/virtio-pci.c
+++ b/tests/libqos/virtio-pci.c
@@ -168,9 +168,9 @@ static bool qvirtio_pci_get_queue_isr_status(QVirtioDevice *d, QVirtQueue *vq)
             /* No ISR checking should be done if masked, but read anyway */
             return qpci_msix_pending(dev->pdev, vqpci->msix_entry);
         } else {
-            data = readl(vqpci->msix_addr);
+            data = qtest_readl(d->bus->qts, vqpci->msix_addr);
             if (data == vqpci->msix_data) {
-                writel(vqpci->msix_addr, 0);
+                qtest_writel(d->bus->qts, vqpci->msix_addr, 0);
                 return true;
             } else {
                 return false;
@@ -192,9 +192,9 @@ static bool qvirtio_pci_get_config_isr_status(QVirtioDevice *d)
             /* No ISR checking should be done if masked, but read anyway */
             return qpci_msix_pending(dev->pdev, dev->config_msix_entry);
         } else {
-            data = readl(dev->config_msix_addr);
+            data = qtest_readl(d->bus->qts, dev->config_msix_addr);
             if (data == dev->config_msix_data) {
-                writel(dev->config_msix_addr, 0);
+                qtest_writel(d->bus->qts, dev->config_msix_addr, 0);
                 return true;
             } else {
                 return false;
diff --git a/tests/libqos/virtio.c b/tests/libqos/virtio.c
index 2b7d9e62c4..2212830742 100644
--- a/tests/libqos/virtio.c
+++ b/tests/libqos/virtio.c
@@ -97,7 +97,7 @@ void qvirtio_wait_queue_isr(QVirtioDevice *d,
     gint64 start_time = g_get_monotonic_time();

     for (;;) {
-        clock_step(100);
+        qtest_clock_step(d->bus->qts, 100);
         if (d->bus->get_queue_isr_status(d, vq)) {
             return;
         }
@@ -118,8 +118,8 @@ uint8_t qvirtio_wait_status_byte_no_isr(QVirtioDevice *d,
     gint64 start_time = g_get_monotonic_time();
     uint8_t val;

-    while ((val = readb(addr)) == 0xff) {
-        clock_step(100);
+    while ((val = qtest_readb(d->bus->qts, addr)) == 0xff) {
+        qtest_clock_step(d->bus->qts, 100);
         g_assert(!d->bus->get_queue_isr_status(d, vq));
         g_assert(g_get_monotonic_time() - start_time <= timeout_us);
     }
@@ -143,7 +143,7 @@ void qvirtio_wait_used_elem(QVirtioDevice *d,
     for (;;) {
         uint32_t got_desc_idx;

-        clock_step(100);
+        qtest_clock_step(d->bus->qts, 100);

         if (d->bus->get_queue_isr_status(d, vq) &&
             qvirtqueue_get_buf(vq, &got_desc_idx)) {
@@ -160,7 +160,7 @@ void qvirtio_wait_config_isr(QVirtioDevice *d, gint64 timeout_us)
     gint64 start_time = g_get_monotonic_time();

     for (;;) {
-        clock_step(100);
+        qtest_clock_step(d->bus->qts, 100);
         if (d->bus->get_config_isr_status(d)) {
             return;
         }
@@ -179,22 +179,23 @@ void qvring_init(const QGuestAllocator *alloc, QVirtQueue *vq, uint64_t addr)

     for (i = 0; i < vq->size - 1; i++) {
         /* vq->desc[i].addr */
-        writeq(vq->desc + (16 * i), 0);
+        qtest_writeq(vq->dev->bus->qts, vq->desc + (16 * i), 0);
         /* vq->desc[i].next */
-        writew(vq->desc + (16 * i) + 14, i + 1);
+        qtest_writew(vq->dev->bus->qts, vq->desc + (16 * i) + 14, i + 1);
     }

     /* vq->avail->flags */
-    writew(vq->avail, 0);
+    qtest_writew(vq->dev->bus->qts, vq->avail, 0);
     /* vq->avail->idx */
-    writew(vq->avail + 2, 0);
+    qtest_writew(vq->dev->bus->qts, vq->avail + 2, 0);
     /* vq->avail->used_event */
-    writew(vq->avail + 4 + (2 * vq->size), 0);
+    qtest_writew(vq->dev->bus->qts, vq->avail + 4 + (2 * vq->size), 0);

     /* vq->used->flags */
-    writew(vq->used, 0);
+    qtest_writew(vq->dev->bus->qts, vq->used, 0);
     /* vq->used->avail_event */
-    writew(vq->used + 2 + sizeof(struct vring_used_elem) * vq->size, 0);
+    qtest_writew(vq->dev->bus->qts,
+                 vq->used + 2 + sizeof(struct vring_used_elem) * vq->size, 0);
 }

 QVRingIndirectDesc *qvring_indirect_desc_setup(QVirtioDevice *d,
@@ -209,35 +210,36 @@ QVRingIndirectDesc *qvring_indirect_desc_setup(QVirtioDevice *d,

     for (i = 0; i < elem - 1; ++i) {
         /* indirect->desc[i].addr */
-        writeq(indirect->desc + (16 * i), 0);
+        qtest_writeq(d->bus->qts, indirect->desc + (16 * i), 0);
         /* indirect->desc[i].flags */
-        writew(indirect->desc + (16 * i) + 12, VRING_DESC_F_NEXT);
+        qtest_writew(d->bus->qts, indirect->desc + (16 * i) + 12,
+                     VRING_DESC_F_NEXT);
         /* indirect->desc[i].next */
-        writew(indirect->desc + (16 * i) + 14, i + 1);
+        qtest_writew(d->bus->qts, indirect->desc + (16 * i) + 14, i + 1);
     }

     return indirect;
 }

-void qvring_indirect_desc_add(QVRingIndirectDesc *indirect, uint64_t data,
-                                                    uint32_t len, bool write)
+void qvring_indirect_desc_add(QTestState *qts, QVRingIndirectDesc *indirect,
+                              uint64_t data, uint32_t len, bool write)
 {
     uint16_t flags;

     g_assert_cmpint(indirect->index, <, indirect->elem);

-    flags = readw(indirect->desc + (16 * indirect->index) + 12);
+    flags = qtest_readw(qts, indirect->desc + (16 * indirect->index) + 12);

     if (write) {
         flags |= VRING_DESC_F_WRITE;
     }

     /* indirect->desc[indirect->index].addr */
-    writeq(indirect->desc + (16 * indirect->index), data);
+    qtest_writeq(qts, indirect->desc + (16 * indirect->index), data);
     /* indirect->desc[indirect->index].len */
-    writel(indirect->desc + (16 * indirect->index) + 8, len);
+    qtest_writel(qts, indirect->desc + (16 * indirect->index) + 8, len);
     /* indirect->desc[indirect->index].flags */
-    writew(indirect->desc + (16 * indirect->index) + 12, flags);
+    qtest_writew(qts, indirect->desc + (16 * indirect->index) + 12, flags);

     indirect->index++;
 }
@@ -257,11 +259,12 @@ uint32_t qvirtqueue_add(QVirtQueue *vq, uint64_t data, uint32_t len, bool write,
     }

     /* vq->desc[vq->free_head].addr */
-    writeq(vq->desc + (16 * vq->free_head), data);
+    qtest_writeq(vq->dev->bus->qts, vq->desc + (16 * vq->free_head), data);
     /* vq->desc[vq->free_head].len */
-    writel(vq->desc + (16 * vq->free_head) + 8, len);
+    qtest_writel(vq->dev->bus->qts, vq->desc + (16 * vq->free_head) + 8, len);
     /* vq->desc[vq->free_head].flags */
-    writew(vq->desc + (16 * vq->free_head) + 12, flags);
+    qtest_writew(vq->dev->bus->qts, vq->desc + (16 * vq->free_head) + 12,
+                 flags);

     return vq->free_head++; /* Return and increase, in this order */
 }
@@ -275,12 +278,14 @@ uint32_t qvirtqueue_add_indirect(QVirtQueue *vq, QVRingIndirectDesc *indirect)
     vq->num_free--;

     /* vq->desc[vq->free_head].addr */
-    writeq(vq->desc + (16 * vq->free_head), indirect->desc);
+    qtest_writeq(vq->dev->bus->qts, vq->desc + (16 * vq->free_head),
+                 indirect->desc);
     /* vq->desc[vq->free_head].len */
-    writel(vq->desc + (16 * vq->free_head) + 8,
-           sizeof(struct vring_desc) * indirect->elem);
+    qtest_writel(vq->dev->bus->qts, vq->desc + (16 * vq->free_head) + 8,
+                 sizeof(struct vring_desc) * indirect->elem);
     /* vq->desc[vq->free_head].flags */
-    writew(vq->desc + (16 * vq->free_head) + 12, VRING_DESC_F_INDIRECT);
+    qtest_writew(vq->dev->bus->qts,
+                 vq->desc + (16 * vq->free_head) + 12, VRING_DESC_F_INDIRECT);

     return vq->free_head++; /* Return and increase, in this order */
 }
@@ -288,21 +293,24 @@ uint32_t qvirtqueue_add_indirect(QVirtQueue *vq, QVRingIndirectDesc *indirect)
 void qvirtqueue_kick(QVirtioDevice *d, QVirtQueue *vq, uint32_t free_head)
 {
     /* vq->avail->idx */
-    uint16_t idx = readw(vq->avail + 2);
+    uint16_t idx = qtest_readw(d->bus->qts, vq->avail + 2);
     /* vq->used->flags */
     uint16_t flags;
     /* vq->used->avail_event */
     uint16_t avail_event;

+    assert(vq->dev == d);
+
     /* vq->avail->ring[idx % vq->size] */
-    writew(vq->avail + 4 + (2 * (idx % vq->size)), free_head);
+    qtest_writew(d->bus->qts, vq->avail + 4 + (2 * (idx % vq->size)),
+                 free_head);
     /* vq->avail->idx */
-    writew(vq->avail + 2, idx + 1);
+    qtest_writew(d->bus->qts, vq->avail + 2, idx + 1);

     /* Must read after idx is updated */
-    flags = readw(vq->avail);
-    avail_event = readw(vq->used + 4 +
-                                sizeof(struct vring_used_elem) * vq->size);
+    flags = qtest_readw(d->bus->qts, vq->avail);
+    avail_event = qtest_readw(d->bus->qts, vq->used + 4 +
+                              sizeof(struct vring_used_elem) * vq->size);

     /* < 1 because we add elements to avail queue one by one */
     if ((flags & VRING_USED_F_NO_NOTIFY) == 0 &&
@@ -323,7 +331,8 @@ bool qvirtqueue_get_buf(QVirtQueue *vq, uint32_t *desc_idx)
 {
     uint16_t idx;

-    idx = readw(vq->used + offsetof(struct vring_used, idx));
+    idx = qtest_readw(vq->dev->bus->qts,
+                      vq->used + offsetof(struct vring_used, idx));
     if (idx == vq->last_used_idx) {
         return false;
     }
@@ -335,7 +344,8 @@ bool qvirtqueue_get_buf(QVirtQueue *vq, uint32_t *desc_idx)
                     offsetof(struct vring_used, ring) +
                     (vq->last_used_idx % vq->size) *
                     sizeof(struct vring_used_elem);
-        *desc_idx = readl(elem_addr + offsetof(struct vring_used_elem, id));
+        *desc_idx = qtest_readl(vq->dev->bus->qts, elem_addr +
+                                offsetof(struct vring_used_elem, id));
     }

     vq->last_used_idx++;
@@ -347,5 +357,5 @@ void qvirtqueue_set_used_event(QVirtQueue *vq, uint16_t idx)
     g_assert(vq->event);

     /* vq->avail->used_event */
-    writew(vq->avail + 4 + (2 * vq->size), idx);
+    qtest_writew(vq->dev->bus->qts, vq->avail + 4 + (2 * vq->size), idx);
 }
diff --git a/tests/virtio-blk-test.c b/tests/virtio-blk-test.c
index 2d38c49bfe..99bb6f26cd 100644
--- a/tests/virtio-blk-test.c
+++ b/tests/virtio-blk-test.c
@@ -343,8 +343,10 @@ static void pci_indirect(void)
     g_free(req.data);

     indirect = qvring_indirect_desc_setup(&dev->vdev, qs->alloc, 2);
-    qvring_indirect_desc_add(indirect, req_addr, 528, false);
-    qvring_indirect_desc_add(indirect, req_addr + 528, 1, true);
+    qvring_indirect_desc_add(dev->vdev.bus->qts, indirect, req_addr, 528,
+                             false);
+    qvring_indirect_desc_add(dev->vdev.bus->qts, indirect, req_addr + 528, 1,
+                             true);
     free_head = qvirtqueue_add_indirect(&vqpci->vq, indirect);
     qvirtqueue_kick(&dev->vdev, &vqpci->vq, free_head);

@@ -368,8 +370,9 @@ static void pci_indirect(void)
     g_free(req.data);

     indirect = qvring_indirect_desc_setup(&dev->vdev, qs->alloc, 2);
-    qvring_indirect_desc_add(indirect, req_addr, 16, false);
-    qvring_indirect_desc_add(indirect, req_addr + 16, 513, true);
+    qvring_indirect_desc_add(dev->vdev.bus->qts, indirect, req_addr, 16, false);
+    qvring_indirect_desc_add(dev->vdev.bus->qts, indirect, req_addr + 16, 513,
+                             true);
     free_head = qvirtqueue_add_indirect(&vqpci->vq, indirect);
     qvirtqueue_kick(&dev->vdev, &vqpci->vq, free_head);

-- 
2.13.5

^ permalink raw reply related	[flat|nested] 75+ messages in thread

* [Qemu-devel] [PATCH v7 13/38] libqos: Use explicit QTestState for fw_cfg operations
  2017-09-11 17:19 [Qemu-devel] [PATCH v7 00/38] Preliminary libqtest cleanups Eric Blake
                   ` (11 preceding siblings ...)
  2017-09-11 17:19 ` [Qemu-devel] [PATCH v7 12/38] libqos: Use explicit QTestState for virtio operations Eric Blake
@ 2017-09-11 17:19 ` Eric Blake
  2017-09-11 23:49   ` John Snow
  2017-09-12  8:55   ` Thomas Huth
  2017-09-11 17:19 ` [Qemu-devel] [PATCH v7 14/38] libqos: Use explicit QTestState for rtas operations Eric Blake
                   ` (24 subsequent siblings)
  37 siblings, 2 replies; 75+ messages in thread
From: Eric Blake @ 2017-09-11 17:19 UTC (permalink / raw)
  To: qemu-devel
  Cc: armbru, pbonzini, thuth, John Snow, David Gibson, Alexander Graf,
	open list:IDE, open list:sPAPR

Drop one more client of global_qtest by teaching all fw_cfg test
functionality (invoked through alloc-pc) to pass in an explicit
QTestState, adjusting all callers.  In particular, fw_cfg-test
had to reorder things to create the test state prior to creating
the fw_cfg (and drop a pointless strdup in the meantime), but that
test now no longer depends on global_qtest.

Signed-off-by: Eric Blake <eblake@redhat.com>

---
v7: squash in typedef fixup as well as fw_cfg-test cleanup
---
 tests/libqos/fw_cfg.h       | 10 ++++++----
 tests/libqos/libqos.h       |  2 +-
 tests/libqos/malloc-pc.h    |  4 ++--
 tests/libqos/malloc-spapr.h |  2 +-
 tests/libqos/malloc.h       |  1 +
 tests/boot-order-test.c     |  6 +++---
 tests/e1000e-test.c         |  2 +-
 tests/fw_cfg-test.c         | 13 ++++---------
 tests/ide-test.c            |  2 +-
 tests/libqos/fw_cfg.c       | 14 ++++++++------
 tests/libqos/libqos.c       |  2 +-
 tests/libqos/malloc-pc.c    |  8 ++++----
 tests/libqos/malloc-spapr.c |  4 ++--
 tests/vhost-user-test.c     |  2 +-
 14 files changed, 36 insertions(+), 36 deletions(-)

diff --git a/tests/libqos/fw_cfg.h b/tests/libqos/fw_cfg.h
index e8371b2317..0353416af0 100644
--- a/tests/libqos/fw_cfg.h
+++ b/tests/libqos/fw_cfg.h
@@ -13,12 +13,14 @@
 #ifndef LIBQOS_FW_CFG_H
 #define LIBQOS_FW_CFG_H

+#include "libqtest.h"

 typedef struct QFWCFG QFWCFG;

 struct QFWCFG
 {
     uint64_t base;
+    QTestState *qts;
     void (*select)(QFWCFG *fw_cfg, uint16_t key);
     void (*read)(QFWCFG *fw_cfg, void *data, size_t len);
 };
@@ -30,12 +32,12 @@ uint16_t qfw_cfg_get_u16(QFWCFG *fw_cfg, uint16_t key);
 uint32_t qfw_cfg_get_u32(QFWCFG *fw_cfg, uint16_t key);
 uint64_t qfw_cfg_get_u64(QFWCFG *fw_cfg, uint16_t key);

-QFWCFG *mm_fw_cfg_init(uint64_t base);
-QFWCFG *io_fw_cfg_init(uint16_t base);
+QFWCFG *mm_fw_cfg_init(QTestState *qts, uint64_t base);
+QFWCFG *io_fw_cfg_init(QTestState *qts, uint16_t base);

-static inline QFWCFG *pc_fw_cfg_init(void)
+static inline QFWCFG *pc_fw_cfg_init(QTestState *qts)
 {
-    return io_fw_cfg_init(0x510);
+    return io_fw_cfg_init(qts, 0x510);
 }

 #endif
diff --git a/tests/libqos/libqos.h b/tests/libqos/libqos.h
index 78e5c044a0..07d4b93d1d 100644
--- a/tests/libqos/libqos.h
+++ b/tests/libqos/libqos.h
@@ -8,7 +8,7 @@
 typedef struct QOSState QOSState;

 typedef struct QOSOps {
-    QGuestAllocator *(*init_allocator)(QAllocOpts);
+    QGuestAllocator *(*init_allocator)(QTestState *qts, QAllocOpts);
     void (*uninit_allocator)(QGuestAllocator *);
     QPCIBus *(*qpci_init)(QTestState *qts, QGuestAllocator *alloc);
     void (*qpci_free)(QPCIBus *bus);
diff --git a/tests/libqos/malloc-pc.h b/tests/libqos/malloc-pc.h
index 86ab9f0429..10f3da6cf2 100644
--- a/tests/libqos/malloc-pc.h
+++ b/tests/libqos/malloc-pc.h
@@ -15,8 +15,8 @@

 #include "libqos/malloc.h"

-QGuestAllocator *pc_alloc_init(void);
-QGuestAllocator *pc_alloc_init_flags(QAllocOpts flags);
+QGuestAllocator *pc_alloc_init(QTestState *qts);
+QGuestAllocator *pc_alloc_init_flags(QTestState *qts, QAllocOpts flags);
 void pc_alloc_uninit(QGuestAllocator *allocator);

 #endif
diff --git a/tests/libqos/malloc-spapr.h b/tests/libqos/malloc-spapr.h
index 64d0e770d1..52a9346a26 100644
--- a/tests/libqos/malloc-spapr.h
+++ b/tests/libqos/malloc-spapr.h
@@ -11,7 +11,7 @@
 #include "libqos/malloc.h"

 QGuestAllocator *spapr_alloc_init(void);
-QGuestAllocator *spapr_alloc_init_flags(QAllocOpts flags);
+QGuestAllocator *spapr_alloc_init_flags(QTestState *qts, QAllocOpts flags);
 void spapr_alloc_uninit(QGuestAllocator *allocator);

 #endif
diff --git a/tests/libqos/malloc.h b/tests/libqos/malloc.h
index ae9dac8f61..828fddabdb 100644
--- a/tests/libqos/malloc.h
+++ b/tests/libqos/malloc.h
@@ -14,6 +14,7 @@
 #define LIBQOS_MALLOC_H

 #include "qemu/queue.h"
+#include "libqtest.h"

 typedef enum {
     ALLOC_NO_FLAGS    = 0x00,
diff --git a/tests/boot-order-test.c b/tests/boot-order-test.c
index 9d516830dd..5fc2ca8e9e 100644
--- a/tests/boot-order-test.c
+++ b/tests/boot-order-test.c
@@ -135,7 +135,7 @@ static void test_prep_boot_order(void)

 static uint64_t read_boot_order_pmac(void)
 {
-    QFWCFG *fw_cfg = mm_fw_cfg_init(0xf0000510);
+    QFWCFG *fw_cfg = mm_fw_cfg_init(global_qtest, 0xf0000510);

     return qfw_cfg_get_u16(fw_cfg, FW_CFG_BOOT_DEVICE);
 }
@@ -160,7 +160,7 @@ static void test_pmac_newworld_boot_order(void)

 static uint64_t read_boot_order_sun4m(void)
 {
-    QFWCFG *fw_cfg = mm_fw_cfg_init(0xd00000510ULL);
+    QFWCFG *fw_cfg = mm_fw_cfg_init(global_qtest, 0xd00000510ULL);

     return qfw_cfg_get_u16(fw_cfg, FW_CFG_BOOT_DEVICE);
 }
@@ -172,7 +172,7 @@ static void test_sun4m_boot_order(void)

 static uint64_t read_boot_order_sun4u(void)
 {
-    QFWCFG *fw_cfg = io_fw_cfg_init(0x510);
+    QFWCFG *fw_cfg = io_fw_cfg_init(global_qtest, 0x510);

     return qfw_cfg_get_u16(fw_cfg, FW_CFG_BOOT_DEVICE);
 }
diff --git a/tests/e1000e-test.c b/tests/e1000e-test.c
index 3cc081941e..8645485c19 100644
--- a/tests/e1000e-test.c
+++ b/tests/e1000e-test.c
@@ -392,7 +392,7 @@ static void data_test_init(e1000e_device *d)
     qtest_start(cmdline);
     g_free(cmdline);

-    test_alloc = pc_alloc_init();
+    test_alloc = pc_alloc_init(global_qtest);
     g_assert_nonnull(test_alloc);

     test_bus = qpci_init_pc(global_qtest, test_alloc);
diff --git a/tests/fw_cfg-test.c b/tests/fw_cfg-test.c
index 688342bed5..3a8dc7a2cd 100644
--- a/tests/fw_cfg-test.c
+++ b/tests/fw_cfg-test.c
@@ -102,12 +102,13 @@ static void test_fw_cfg_boot_menu(void)
 int main(int argc, char **argv)
 {
     QTestState *s;
-    char *cmdline;
     int ret;

     g_test_init(&argc, &argv, NULL);

-    fw_cfg = pc_fw_cfg_init();
+    s = qtest_init("-uuid 4600cb32-38ec-4b2f-8acb-81c6ea54f2d8");
+
+    fw_cfg = pc_fw_cfg_init(s);

     qtest_add_func("fw_cfg/signature", test_fw_cfg_signature);
     qtest_add_func("fw_cfg/id", test_fw_cfg_id);
@@ -125,15 +126,9 @@ int main(int argc, char **argv)
     qtest_add_func("fw_cfg/numa", test_fw_cfg_numa);
     qtest_add_func("fw_cfg/boot_menu", test_fw_cfg_boot_menu);

-    cmdline = g_strdup_printf("-uuid 4600cb32-38ec-4b2f-8acb-81c6ea54f2d8 ");
-    s = qtest_start(cmdline);
-    g_free(cmdline);
-
     ret = g_test_run();

-    if (s) {
-        qtest_quit(s);
-    }
+    qtest_quit(s);

     return ret;
 }
diff --git a/tests/ide-test.c b/tests/ide-test.c
index b2237b6158..084f6a5f96 100644
--- a/tests/ide-test.c
+++ b/tests/ide-test.c
@@ -125,7 +125,7 @@ static void ide_test_start(const char *cmdline_fmt, ...)
     va_end(ap);

     qtest_start(cmdline);
-    guest_malloc = pc_alloc_init();
+    guest_malloc = pc_alloc_init(global_qtest);

     g_free(cmdline);
 }
diff --git a/tests/libqos/fw_cfg.c b/tests/libqos/fw_cfg.c
index 4d9dc3fd0b..d0889d1e22 100644
--- a/tests/libqos/fw_cfg.c
+++ b/tests/libqos/fw_cfg.c
@@ -56,7 +56,7 @@ uint64_t qfw_cfg_get_u64(QFWCFG *fw_cfg, uint16_t key)

 static void mm_fw_cfg_select(QFWCFG *fw_cfg, uint16_t key)
 {
-    writew(fw_cfg->base, key);
+    qtest_writew(fw_cfg->qts, fw_cfg->base, key);
 }

 static void mm_fw_cfg_read(QFWCFG *fw_cfg, void *data, size_t len)
@@ -65,15 +65,16 @@ static void mm_fw_cfg_read(QFWCFG *fw_cfg, void *data, size_t len)
     int i;

     for (i = 0; i < len; i++) {
-        ptr[i] = readb(fw_cfg->base + 2);
+        ptr[i] = qtest_readb(fw_cfg->qts, fw_cfg->base + 2);
     }
 }

-QFWCFG *mm_fw_cfg_init(uint64_t base)
+QFWCFG *mm_fw_cfg_init(QTestState *qts, uint64_t base)
 {
     QFWCFG *fw_cfg = g_malloc0(sizeof(*fw_cfg));

     fw_cfg->base = base;
+    fw_cfg->qts = qts;
     fw_cfg->select = mm_fw_cfg_select;
     fw_cfg->read = mm_fw_cfg_read;

@@ -82,7 +83,7 @@ QFWCFG *mm_fw_cfg_init(uint64_t base)

 static void io_fw_cfg_select(QFWCFG *fw_cfg, uint16_t key)
 {
-    outw(fw_cfg->base, key);
+    qtest_outw(fw_cfg->qts, fw_cfg->base, key);
 }

 static void io_fw_cfg_read(QFWCFG *fw_cfg, void *data, size_t len)
@@ -91,15 +92,16 @@ static void io_fw_cfg_read(QFWCFG *fw_cfg, void *data, size_t len)
     int i;

     for (i = 0; i < len; i++) {
-        ptr[i] = inb(fw_cfg->base + 1);
+        ptr[i] = qtest_inb(fw_cfg->qts, fw_cfg->base + 1);
     }
 }

-QFWCFG *io_fw_cfg_init(uint16_t base)
+QFWCFG *io_fw_cfg_init(QTestState *qts, uint16_t base)
 {
     QFWCFG *fw_cfg = g_malloc0(sizeof(*fw_cfg));

     fw_cfg->base = base;
+    fw_cfg->qts = qts;
     fw_cfg->select = io_fw_cfg_select;
     fw_cfg->read = io_fw_cfg_read;

diff --git a/tests/libqos/libqos.c b/tests/libqos/libqos.c
index 7a76c1ef34..40018b9c07 100644
--- a/tests/libqos/libqos.c
+++ b/tests/libqos/libqos.c
@@ -23,7 +23,7 @@ QOSState *qtest_vboot(QOSOps *ops, const char *cmdline_fmt, va_list ap)
     qs->qts = qtest_start(cmdline);
     qs->ops = ops;
     if (ops) {
-        qs->alloc = ops->init_allocator(ALLOC_NO_FLAGS);
+        qs->alloc = ops->init_allocator(qs->qts, ALLOC_NO_FLAGS);
         qs->pcibus = ops->qpci_init(qs->qts, qs->alloc);
     }

diff --git a/tests/libqos/malloc-pc.c b/tests/libqos/malloc-pc.c
index dd2b900c5f..634b9c288a 100644
--- a/tests/libqos/malloc-pc.c
+++ b/tests/libqos/malloc-pc.c
@@ -29,11 +29,11 @@ void pc_alloc_uninit(QGuestAllocator *allocator)
     alloc_uninit(allocator);
 }

-QGuestAllocator *pc_alloc_init_flags(QAllocOpts flags)
+QGuestAllocator *pc_alloc_init_flags(QTestState *qts, QAllocOpts flags)
 {
     QGuestAllocator *s;
     uint64_t ram_size;
-    QFWCFG *fw_cfg = pc_fw_cfg_init();
+    QFWCFG *fw_cfg = pc_fw_cfg_init(qts);

     ram_size = qfw_cfg_get_u64(fw_cfg, FW_CFG_RAM_SIZE);
     s = alloc_init_flags(flags, 1 << 20, MIN(ram_size, 0xE0000000));
@@ -45,7 +45,7 @@ QGuestAllocator *pc_alloc_init_flags(QAllocOpts flags)
     return s;
 }

-inline QGuestAllocator *pc_alloc_init(void)
+inline QGuestAllocator *pc_alloc_init(QTestState *qts)
 {
-    return pc_alloc_init_flags(ALLOC_NO_FLAGS);
+    return pc_alloc_init_flags(qts, ALLOC_NO_FLAGS);
 }
diff --git a/tests/libqos/malloc-spapr.c b/tests/libqos/malloc-spapr.c
index 006404af33..1c359cea6c 100644
--- a/tests/libqos/malloc-spapr.c
+++ b/tests/libqos/malloc-spapr.c
@@ -22,7 +22,7 @@ void spapr_alloc_uninit(QGuestAllocator *allocator)
     alloc_uninit(allocator);
 }

-QGuestAllocator *spapr_alloc_init_flags(QAllocOpts flags)
+QGuestAllocator *spapr_alloc_init_flags(QTestState *qts, QAllocOpts flags)
 {
     QGuestAllocator *s;

@@ -34,5 +34,5 @@ QGuestAllocator *spapr_alloc_init_flags(QAllocOpts flags)

 QGuestAllocator *spapr_alloc_init(void)
 {
-    return spapr_alloc_init_flags(ALLOC_NO_FLAGS);
+    return spapr_alloc_init_flags(NULL, ALLOC_NO_FLAGS);
 }
diff --git a/tests/vhost-user-test.c b/tests/vhost-user-test.c
index f762b2ebc1..b041d2c6a2 100644
--- a/tests/vhost-user-test.c
+++ b/tests/vhost-user-test.c
@@ -896,7 +896,7 @@ static void test_multiqueue(void)
     bus = qpci_init_pc(global_qtest, NULL);
     dev = virtio_net_pci_init(bus, PCI_SLOT);

-    alloc = pc_alloc_init();
+    alloc = pc_alloc_init(global_qtest);
     for (i = 0; i < queues * 2; i++) {
         vq[i] = (QVirtQueuePCI *)qvirtqueue_setup(&dev->vdev, alloc, i);
     }
-- 
2.13.5

^ permalink raw reply related	[flat|nested] 75+ messages in thread

* [Qemu-devel] [PATCH v7 14/38] libqos: Use explicit QTestState for rtas operations
  2017-09-11 17:19 [Qemu-devel] [PATCH v7 00/38] Preliminary libqtest cleanups Eric Blake
                   ` (12 preceding siblings ...)
  2017-09-11 17:19 ` [Qemu-devel] [PATCH v7 13/38] libqos: Use explicit QTestState for fw_cfg operations Eric Blake
@ 2017-09-11 17:19 ` Eric Blake
  2017-09-12  9:01   ` Thomas Huth
  2017-09-11 17:19 ` [Qemu-devel] [PATCH v7 15/38] libqos: Use explicit QTestState for i2c operations Eric Blake
                   ` (23 subsequent siblings)
  37 siblings, 1 reply; 75+ messages in thread
From: Eric Blake @ 2017-09-11 17:19 UTC (permalink / raw)
  To: qemu-devel
  Cc: armbru, pbonzini, thuth, David Gibson, Alexander Graf, open list:sPAPR

Drop one more client of global_qtest by teaching all rtas test
functionality to pass in an explicit QTestState, adjusting all
callers.

Signed-off-by: Eric Blake <eblake@redhat.com>

---
v7: catch a couple more operations, drop R-b
---
 tests/libqos/rtas.h      |  9 ++++++---
 tests/libqos/pci-spapr.c | 18 ++++++++++++------
 tests/libqos/rtas.c      | 36 ++++++++++++++++++++----------------
 tests/rtas-test.c        |  2 +-
 4 files changed, 39 insertions(+), 26 deletions(-)

diff --git a/tests/libqos/rtas.h b/tests/libqos/rtas.h
index 498eb19230..e3d0f850a7 100644
--- a/tests/libqos/rtas.h
+++ b/tests/libqos/rtas.h
@@ -7,9 +7,12 @@
 #define LIBQOS_RTAS_H
 #include "libqos/malloc.h"

-int qrtas_get_time_of_day(QGuestAllocator *alloc, struct tm *tm, uint32_t *ns);
-uint32_t qrtas_ibm_read_pci_config(QGuestAllocator *alloc, uint64_t buid,
+int qrtas_get_time_of_day(QTestState *qts, QGuestAllocator *alloc,
+                          struct tm *tm, uint32_t *ns);
+uint32_t qrtas_ibm_read_pci_config(QTestState *qts, QGuestAllocator *alloc,
+                                   uint64_t buid,
                                    uint32_t addr, uint32_t size);
-int qrtas_ibm_write_pci_config(QGuestAllocator *alloc, uint64_t buid,
+int qrtas_ibm_write_pci_config(QTestState *qts, QGuestAllocator *alloc,
+                               uint64_t buid,
                                uint32_t addr, uint32_t size, uint32_t val);
 #endif /* LIBQOS_RTAS_H */
diff --git a/tests/libqos/pci-spapr.c b/tests/libqos/pci-spapr.c
index 9121913af6..4c29889b0b 100644
--- a/tests/libqos/pci-spapr.c
+++ b/tests/libqos/pci-spapr.c
@@ -108,21 +108,24 @@ static uint8_t qpci_spapr_config_readb(QPCIBus *bus, int devfn, uint8_t offset)
 {
     QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
     uint32_t config_addr = (devfn << 8) | offset;
-    return qrtas_ibm_read_pci_config(s->alloc, s->buid, config_addr, 1);
+    return qrtas_ibm_read_pci_config(bus->qts, s->alloc, s->buid,
+                                     config_addr, 1);
 }

 static uint16_t qpci_spapr_config_readw(QPCIBus *bus, int devfn, uint8_t offset)
 {
     QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
     uint32_t config_addr = (devfn << 8) | offset;
-    return qrtas_ibm_read_pci_config(s->alloc, s->buid, config_addr, 2);
+    return qrtas_ibm_read_pci_config(bus->qts, s->alloc, s->buid,
+                                     config_addr, 2);
 }

 static uint32_t qpci_spapr_config_readl(QPCIBus *bus, int devfn, uint8_t offset)
 {
     QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
     uint32_t config_addr = (devfn << 8) | offset;
-    return qrtas_ibm_read_pci_config(s->alloc, s->buid, config_addr, 4);
+    return qrtas_ibm_read_pci_config(bus->qts, s->alloc, s->buid,
+                                     config_addr, 4);
 }

 static void qpci_spapr_config_writeb(QPCIBus *bus, int devfn, uint8_t offset,
@@ -130,7 +133,8 @@ static void qpci_spapr_config_writeb(QPCIBus *bus, int devfn, uint8_t offset,
 {
     QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
     uint32_t config_addr = (devfn << 8) | offset;
-    qrtas_ibm_write_pci_config(s->alloc, s->buid, config_addr, 1, value);
+    qrtas_ibm_write_pci_config(bus->qts, s->alloc, s->buid,
+                               config_addr, 1, value);
 }

 static void qpci_spapr_config_writew(QPCIBus *bus, int devfn, uint8_t offset,
@@ -138,7 +142,8 @@ static void qpci_spapr_config_writew(QPCIBus *bus, int devfn, uint8_t offset,
 {
     QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
     uint32_t config_addr = (devfn << 8) | offset;
-    qrtas_ibm_write_pci_config(s->alloc, s->buid, config_addr, 2, value);
+    qrtas_ibm_write_pci_config(bus->qts, s->alloc, s->buid,
+                               config_addr, 2, value);
 }

 static void qpci_spapr_config_writel(QPCIBus *bus, int devfn, uint8_t offset,
@@ -146,7 +151,8 @@ static void qpci_spapr_config_writel(QPCIBus *bus, int devfn, uint8_t offset,
 {
     QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
     uint32_t config_addr = (devfn << 8) | offset;
-    qrtas_ibm_write_pci_config(s->alloc, s->buid, config_addr, 4, value);
+    qrtas_ibm_write_pci_config(bus->qts, s->alloc, s->buid,
+                               config_addr, 4, value);
 }

 #define SPAPR_PCI_BASE               (1ULL << 45)
diff --git a/tests/libqos/rtas.c b/tests/libqos/rtas.c
index 0269803ce0..d81ff4274d 100644
--- a/tests/libqos/rtas.c
+++ b/tests/libqos/rtas.c
@@ -7,26 +7,28 @@
 #include "libqtest.h"
 #include "libqos/rtas.h"

-static void qrtas_copy_args(uint64_t target_args, uint32_t nargs,
-                            uint32_t *args)
+static void qrtas_copy_args(QTestState *qts, uint64_t target_args,
+                            uint32_t nargs, uint32_t *args)
 {
     int i;

     for (i = 0; i < nargs; i++) {
-        writel(target_args + i * sizeof(uint32_t), args[i]);
+        qtest_writel(qts, target_args + i * sizeof(uint32_t), args[i]);
     }
 }

-static void qrtas_copy_ret(uint64_t target_ret, uint32_t nret, uint32_t *ret)
+static void qrtas_copy_ret(QTestState *qts, uint64_t target_ret,
+                           uint32_t nret, uint32_t *ret)
 {
     int i;

     for (i = 0; i < nret; i++) {
-        ret[i] = readl(target_ret + i * sizeof(uint32_t));
+        ret[i] = qtest_readl(qts, target_ret + i * sizeof(uint32_t));
     }
 }

-static uint64_t qrtas_call(QGuestAllocator *alloc, const char *name,
+static uint64_t qrtas_call(QTestState *qts, QGuestAllocator *alloc,
+                           const char *name,
                            uint32_t nargs, uint32_t *args,
                            uint32_t nret, uint32_t *ret)
 {
@@ -36,10 +38,9 @@ static uint64_t qrtas_call(QGuestAllocator *alloc, const char *name,
     target_args = guest_alloc(alloc, nargs * sizeof(uint32_t));
     target_ret = guest_alloc(alloc, nret * sizeof(uint32_t));

-    qrtas_copy_args(target_args, nargs, args);
-    res = qtest_rtas_call(global_qtest, name,
-                          nargs, target_args, nret, target_ret);
-    qrtas_copy_ret(target_ret, nret, ret);
+    qrtas_copy_args(qts, target_args, nargs, args);
+    res = qtest_rtas_call(qts, name, nargs, target_args, nret, target_ret);
+    qrtas_copy_ret(qts, target_ret, nret, ret);

     guest_free(alloc, target_ret);
     guest_free(alloc, target_args);
@@ -47,12 +48,13 @@ static uint64_t qrtas_call(QGuestAllocator *alloc, const char *name,
     return res;
 }

-int qrtas_get_time_of_day(QGuestAllocator *alloc, struct tm *tm, uint32_t *ns)
+int qrtas_get_time_of_day(QTestState *qts, QGuestAllocator *alloc,
+                          struct tm *tm, uint32_t *ns)
 {
     int res;
     uint32_t ret[8];

-    res = qrtas_call(alloc, "get-time-of-day", 0, NULL, 8, ret);
+    res = qrtas_call(qts, alloc, "get-time-of-day", 0, NULL, 8, ret);
     if (res != 0) {
         return res;
     }
@@ -70,7 +72,8 @@ int qrtas_get_time_of_day(QGuestAllocator *alloc, struct tm *tm, uint32_t *ns)
     return res;
 }

-uint32_t qrtas_ibm_read_pci_config(QGuestAllocator *alloc, uint64_t buid,
+uint32_t qrtas_ibm_read_pci_config(QTestState *qts, QGuestAllocator *alloc,
+                                   uint64_t buid,
                                    uint32_t addr, uint32_t size)
 {
     int res;
@@ -80,7 +83,7 @@ uint32_t qrtas_ibm_read_pci_config(QGuestAllocator *alloc, uint64_t buid,
     args[1] = buid >> 32;
     args[2] = buid & 0xffffffff;
     args[3] = size;
-    res = qrtas_call(alloc, "ibm,read-pci-config", 4, args, 2, ret);
+    res = qrtas_call(qts, alloc, "ibm,read-pci-config", 4, args, 2, ret);
     if (res != 0) {
         return -1;
     }
@@ -92,7 +95,8 @@ uint32_t qrtas_ibm_read_pci_config(QGuestAllocator *alloc, uint64_t buid,
     return ret[1];
 }

-int qrtas_ibm_write_pci_config(QGuestAllocator *alloc, uint64_t buid,
+int qrtas_ibm_write_pci_config(QTestState *qts, QGuestAllocator *alloc,
+                               uint64_t buid,
                                uint32_t addr, uint32_t size, uint32_t val)
 {
     int res;
@@ -103,7 +107,7 @@ int qrtas_ibm_write_pci_config(QGuestAllocator *alloc, uint64_t buid,
     args[2] = buid & 0xffffffff;
     args[3] = size;
     args[4] = val;
-    res = qrtas_call(alloc, "ibm,write-pci-config", 5, args, 1, ret);
+    res = qrtas_call(qts, alloc, "ibm,write-pci-config", 5, args, 1, ret);
     if (res != 0) {
         return -1;
     }
diff --git a/tests/rtas-test.c b/tests/rtas-test.c
index 276c87ef84..34a692163f 100644
--- a/tests/rtas-test.c
+++ b/tests/rtas-test.c
@@ -16,7 +16,7 @@ static void test_rtas_get_time_of_day(void)
     qs = qtest_spapr_boot("-machine pseries");

     t1 = time(NULL);
-    ret = qrtas_get_time_of_day(qs->alloc, &tm, &ns);
+    ret = qrtas_get_time_of_day(qs->qts, qs->alloc, &tm, &ns);
     g_assert_cmpint(ret, ==, 0);
     t2 = mktimegm(&tm);
     g_assert(t2 - t1 < 5); /* 5 sec max to run the test */
-- 
2.13.5

^ permalink raw reply related	[flat|nested] 75+ messages in thread

* [Qemu-devel] [PATCH v7 15/38] libqos: Use explicit QTestState for i2c operations
  2017-09-11 17:19 [Qemu-devel] [PATCH v7 00/38] Preliminary libqtest cleanups Eric Blake
                   ` (13 preceding siblings ...)
  2017-09-11 17:19 ` [Qemu-devel] [PATCH v7 14/38] libqos: Use explicit QTestState for rtas operations Eric Blake
@ 2017-09-11 17:19 ` Eric Blake
  2017-09-12  9:04   ` Thomas Huth
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 16/38] libqos: Use explicit QTestState for ahci operations Eric Blake
                   ` (22 subsequent siblings)
  37 siblings, 1 reply; 75+ messages in thread
From: Eric Blake @ 2017-09-11 17:19 UTC (permalink / raw)
  To: qemu-devel; +Cc: armbru, pbonzini, thuth

Drop one more client of global_qtest by teaching all i2c test
functionality to pass in an explicit QTestState, adjusting all
callers.

Signed-off-by: Eric Blake <eblake@redhat.com>
---
 tests/libqos/i2c.h      |  7 ++++--
 tests/ds1338-test.c     |  6 ++---
 tests/libqos/i2c-imx.c  | 67 +++++++++++++++++++++++++------------------------
 tests/libqos/i2c-omap.c | 45 +++++++++++++++++----------------
 tests/tmp105-test.c     |  6 ++---
 5 files changed, 66 insertions(+), 65 deletions(-)

diff --git a/tests/libqos/i2c.h b/tests/libqos/i2c.h
index 6e648f922a..eb40b808bd 100644
--- a/tests/libqos/i2c.h
+++ b/tests/libqos/i2c.h
@@ -9,6 +9,7 @@
 #ifndef LIBQOS_I2C_H
 #define LIBQOS_I2C_H

+#include "libqtest.h"

 typedef struct I2CAdapter I2CAdapter;
 struct I2CAdapter {
@@ -16,6 +17,8 @@ struct I2CAdapter {
                  const uint8_t *buf, uint16_t len);
     void (*recv)(I2CAdapter *adapter, uint8_t addr,
                  uint8_t *buf, uint16_t len);
+
+    QTestState *qts;
 };

 void i2c_send(I2CAdapter *i2c, uint8_t addr,
@@ -24,9 +27,9 @@ void i2c_recv(I2CAdapter *i2c, uint8_t addr,
               uint8_t *buf, uint16_t len);

 /* libi2c-omap.c */
-I2CAdapter *omap_i2c_create(uint64_t addr);
+I2CAdapter *omap_i2c_create(QTestState *qts, uint64_t addr);

 /* libi2c-imx.c */
-I2CAdapter *imx_i2c_create(uint64_t addr);
+I2CAdapter *imx_i2c_create(QTestState *qts, uint64_t addr);

 #endif
diff --git a/tests/ds1338-test.c b/tests/ds1338-test.c
index 26968bc82a..742dad9113 100644
--- a/tests/ds1338-test.c
+++ b/tests/ds1338-test.c
@@ -61,16 +61,14 @@ int main(int argc, char **argv)
     g_test_init(&argc, &argv, NULL);

     s = qtest_start("-display none -machine imx25-pdk");
-    i2c = imx_i2c_create(IMX25_I2C_0_BASE);
+    i2c = imx_i2c_create(s, IMX25_I2C_0_BASE);
     addr = DS1338_ADDR;

     qtest_add_func("/ds1338/tx-rx", send_and_receive);

     ret = g_test_run();

-    if (s) {
-        qtest_quit(s);
-    }
+    qtest_quit(s);
     g_free(i2c);

     return ret;
diff --git a/tests/libqos/i2c-imx.c b/tests/libqos/i2c-imx.c
index 1c4b4314ba..0945f2ecdc 100644
--- a/tests/libqos/i2c-imx.c
+++ b/tests/libqos/i2c-imx.c
@@ -40,8 +40,8 @@ typedef struct IMXI2C {
 static void imx_i2c_set_slave_addr(IMXI2C *s, uint8_t addr,
                                    enum IMXI2CDirection direction)
 {
-    writeb(s->addr + I2DR_ADDR, (addr << 1) |
-           (direction == IMX_I2C_READ ? 1 : 0));
+    qtest_writeb(s->parent.qts, s->addr + I2DR_ADDR,
+                 (addr << 1) | (direction == IMX_I2C_READ ? 1 : 0));
 }

 static void imx_i2c_send(I2CAdapter *i2c, uint8_t addr,
@@ -63,35 +63,35 @@ static void imx_i2c_send(I2CAdapter *i2c, uint8_t addr,
            I2CR_MTX |
            I2CR_TXAK;

-    writeb(s->addr + I2CR_ADDR, data);
-    status = readb(s->addr + I2SR_ADDR);
+    qtest_writeb(i2c->qts, s->addr + I2CR_ADDR, data);
+    status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
     g_assert((status & I2SR_IBB) != 0);

     /* set the slave address */
     imx_i2c_set_slave_addr(s, addr, IMX_I2C_WRITE);
-    status = readb(s->addr + I2SR_ADDR);
+    status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
     g_assert((status & I2SR_IIF) != 0);
     g_assert((status & I2SR_RXAK) == 0);

     /* ack the interrupt */
-    writeb(s->addr + I2SR_ADDR, 0);
-    status = readb(s->addr + I2SR_ADDR);
+    qtest_writeb(i2c->qts, s->addr + I2SR_ADDR, 0);
+    status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
     g_assert((status & I2SR_IIF) == 0);

     while (size < len) {
         /* check we are still busy */
-        status = readb(s->addr + I2SR_ADDR);
+        status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
         g_assert((status & I2SR_IBB) != 0);

         /* write the data */
-        writeb(s->addr + I2DR_ADDR, buf[size]);
-        status = readb(s->addr + I2SR_ADDR);
+        qtest_writeb(i2c->qts, s->addr + I2DR_ADDR, buf[size]);
+        status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
         g_assert((status & I2SR_IIF) != 0);
         g_assert((status & I2SR_RXAK) == 0);

         /* ack the interrupt */
-        writeb(s->addr + I2SR_ADDR, 0);
-        status = readb(s->addr + I2SR_ADDR);
+        qtest_writeb(i2c->qts, s->addr + I2SR_ADDR, 0);
+        status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
         g_assert((status & I2SR_IIF) == 0);

         size++;
@@ -99,8 +99,8 @@ static void imx_i2c_send(I2CAdapter *i2c, uint8_t addr,

     /* release the bus */
     data &= ~(I2CR_MSTA | I2CR_MTX);
-    writeb(s->addr + I2CR_ADDR, data);
-    status = readb(s->addr + I2SR_ADDR);
+    qtest_writeb(i2c->qts, s->addr + I2CR_ADDR, data);
+    status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
     g_assert((status & I2SR_IBB) == 0);
 }

@@ -123,19 +123,19 @@ static void imx_i2c_recv(I2CAdapter *i2c, uint8_t addr,
            I2CR_MTX |
            I2CR_TXAK;

-    writeb(s->addr + I2CR_ADDR, data);
-    status = readb(s->addr + I2SR_ADDR);
+    qtest_writeb(i2c->qts, s->addr + I2CR_ADDR, data);
+    status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
     g_assert((status & I2SR_IBB) != 0);

     /* set the slave address */
     imx_i2c_set_slave_addr(s, addr, IMX_I2C_READ);
-    status = readb(s->addr + I2SR_ADDR);
+    status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
     g_assert((status & I2SR_IIF) != 0);
     g_assert((status & I2SR_RXAK) == 0);

     /* ack the interrupt */
-    writeb(s->addr + I2SR_ADDR, 0);
-    status = readb(s->addr + I2SR_ADDR);
+    qtest_writeb(i2c->qts, s->addr + I2SR_ADDR, 0);
+    status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
     g_assert((status & I2SR_IIF) == 0);

     /* set the bus for read */
@@ -144,23 +144,23 @@ static void imx_i2c_recv(I2CAdapter *i2c, uint8_t addr,
     if (len != 1) {
         data &= ~I2CR_TXAK;
     }
-    writeb(s->addr + I2CR_ADDR, data);
-    status = readb(s->addr + I2SR_ADDR);
+    qtest_writeb(i2c->qts, s->addr + I2CR_ADDR, data);
+    status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
     g_assert((status & I2SR_IBB) != 0);

     /* dummy read */
-    readb(s->addr + I2DR_ADDR);
-    status = readb(s->addr + I2SR_ADDR);
+    qtest_readb(i2c->qts, s->addr + I2DR_ADDR);
+    status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
     g_assert((status & I2SR_IIF) != 0);

     /* ack the interrupt */
-    writeb(s->addr + I2SR_ADDR, 0);
-    status = readb(s->addr + I2SR_ADDR);
+    qtest_writeb(i2c->qts, s->addr + I2SR_ADDR, 0);
+    status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
     g_assert((status & I2SR_IIF) == 0);

     while (size < len) {
         /* check we are still busy */
-        status = readb(s->addr + I2SR_ADDR);
+        status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
         g_assert((status & I2SR_IBB) != 0);

         if (size == (len - 1)) {
@@ -170,30 +170,30 @@ static void imx_i2c_recv(I2CAdapter *i2c, uint8_t addr,
             /* ack the data read */
             data |= I2CR_TXAK;
         }
-        writeb(s->addr + I2CR_ADDR, data);
+        qtest_writeb(i2c->qts, s->addr + I2CR_ADDR, data);

         /* read the data */
-        buf[size] = readb(s->addr + I2DR_ADDR);
+        buf[size] = qtest_readb(i2c->qts, s->addr + I2DR_ADDR);

         if (size != (len - 1)) {
-            status = readb(s->addr + I2SR_ADDR);
+            status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
             g_assert((status & I2SR_IIF) != 0);

             /* ack the interrupt */
-            writeb(s->addr + I2SR_ADDR, 0);
+            qtest_writeb(i2c->qts, s->addr + I2SR_ADDR, 0);
         }

-        status = readb(s->addr + I2SR_ADDR);
+        status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
         g_assert((status & I2SR_IIF) == 0);

         size++;
     }

-    status = readb(s->addr + I2SR_ADDR);
+    status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
     g_assert((status & I2SR_IBB) == 0);
 }

-I2CAdapter *imx_i2c_create(uint64_t addr)
+I2CAdapter *imx_i2c_create(QTestState *qts, uint64_t addr)
 {
     IMXI2C *s = g_malloc0(sizeof(*s));
     I2CAdapter *i2c = (I2CAdapter *)s;
@@ -202,6 +202,7 @@ I2CAdapter *imx_i2c_create(uint64_t addr)

     i2c->send = imx_i2c_send;
     i2c->recv = imx_i2c_recv;
+    i2c->qts = qts;

     return i2c;
 }
diff --git a/tests/libqos/i2c-omap.c b/tests/libqos/i2c-omap.c
index f603fdf43c..1ef6e7b200 100644
--- a/tests/libqos/i2c-omap.c
+++ b/tests/libqos/i2c-omap.c
@@ -51,8 +51,8 @@ static void omap_i2c_set_slave_addr(OMAPI2C *s, uint8_t addr)
 {
     uint16_t data = addr;

-    writew(s->addr + OMAP_I2C_SA, data);
-    data = readw(s->addr + OMAP_I2C_SA);
+    qtest_writew(s->parent.qts, s->addr + OMAP_I2C_SA, data);
+    data = qtest_readw(s->parent.qts, s->addr + OMAP_I2C_SA);
     g_assert_cmphex(data, ==, addr);
 }

@@ -65,38 +65,38 @@ static void omap_i2c_send(I2CAdapter *i2c, uint8_t addr,
     omap_i2c_set_slave_addr(s, addr);

     data = len;
-    writew(s->addr + OMAP_I2C_CNT, data);
+    qtest_writew(i2c->qts, s->addr + OMAP_I2C_CNT, data);

     data = OMAP_I2C_CON_I2C_EN |
            OMAP_I2C_CON_TRX |
            OMAP_I2C_CON_MST |
            OMAP_I2C_CON_STT |
            OMAP_I2C_CON_STP;
-    writew(s->addr + OMAP_I2C_CON, data);
-    data = readw(s->addr + OMAP_I2C_CON);
+    qtest_writew(i2c->qts, s->addr + OMAP_I2C_CON, data);
+    data = qtest_readw(i2c->qts, s->addr + OMAP_I2C_CON);
     g_assert((data & OMAP_I2C_CON_STP) != 0);

-    data = readw(s->addr + OMAP_I2C_STAT);
+    data = qtest_readw(i2c->qts, s->addr + OMAP_I2C_STAT);
     g_assert((data & OMAP_I2C_STAT_NACK) == 0);

     while (len > 1) {
-        data = readw(s->addr + OMAP_I2C_STAT);
+        data = qtest_readw(i2c->qts, s->addr + OMAP_I2C_STAT);
         g_assert((data & OMAP_I2C_STAT_XRDY) != 0);

         data = buf[0] | ((uint16_t)buf[1] << 8);
-        writew(s->addr + OMAP_I2C_DATA, data);
+        qtest_writew(i2c->qts, s->addr + OMAP_I2C_DATA, data);
         buf = (uint8_t *)buf + 2;
         len -= 2;
     }
     if (len == 1) {
-        data = readw(s->addr + OMAP_I2C_STAT);
+        data = qtest_readw(i2c->qts, s->addr + OMAP_I2C_STAT);
         g_assert((data & OMAP_I2C_STAT_XRDY) != 0);

         data = buf[0];
-        writew(s->addr + OMAP_I2C_DATA, data);
+        qtest_writew(i2c->qts, s->addr + OMAP_I2C_DATA, data);
     }

-    data = readw(s->addr + OMAP_I2C_CON);
+    data = qtest_readw(i2c->qts, s->addr + OMAP_I2C_CON);
     g_assert((data & OMAP_I2C_CON_STP) == 0);
 }

@@ -109,30 +109,30 @@ static void omap_i2c_recv(I2CAdapter *i2c, uint8_t addr,
     omap_i2c_set_slave_addr(s, addr);

     data = len;
-    writew(s->addr + OMAP_I2C_CNT, data);
+    qtest_writew(i2c->qts, s->addr + OMAP_I2C_CNT, data);

     data = OMAP_I2C_CON_I2C_EN |
            OMAP_I2C_CON_MST |
            OMAP_I2C_CON_STT |
            OMAP_I2C_CON_STP;
-    writew(s->addr + OMAP_I2C_CON, data);
-    data = readw(s->addr + OMAP_I2C_CON);
+    qtest_writew(i2c->qts, s->addr + OMAP_I2C_CON, data);
+    data = qtest_readw(i2c->qts, s->addr + OMAP_I2C_CON);
     g_assert((data & OMAP_I2C_CON_STP) == 0);

-    data = readw(s->addr + OMAP_I2C_STAT);
+    data = qtest_readw(i2c->qts, s->addr + OMAP_I2C_STAT);
     g_assert((data & OMAP_I2C_STAT_NACK) == 0);

-    data = readw(s->addr + OMAP_I2C_CNT);
+    data = qtest_readw(i2c->qts, s->addr + OMAP_I2C_CNT);
     g_assert_cmpuint(data, ==, len);

     while (len > 0) {
-        data = readw(s->addr + OMAP_I2C_STAT);
+        data = qtest_readw(i2c->qts, s->addr + OMAP_I2C_STAT);
         g_assert((data & OMAP_I2C_STAT_RRDY) != 0);
         g_assert((data & OMAP_I2C_STAT_ROVR) == 0);

-        data = readw(s->addr + OMAP_I2C_DATA);
+        data = qtest_readw(i2c->qts, s->addr + OMAP_I2C_DATA);

-        stat = readw(s->addr + OMAP_I2C_STAT);
+        stat = qtest_readw(i2c->qts, s->addr + OMAP_I2C_STAT);

         if (unlikely(len == 1)) {
             g_assert((stat & OMAP_I2C_STAT_SBD) != 0);
@@ -148,11 +148,11 @@ static void omap_i2c_recv(I2CAdapter *i2c, uint8_t addr,
         }
     }

-    data = readw(s->addr + OMAP_I2C_CON);
+    data = qtest_readw(i2c->qts, s->addr + OMAP_I2C_CON);
     g_assert((data & OMAP_I2C_CON_STP) == 0);
 }

-I2CAdapter *omap_i2c_create(uint64_t addr)
+I2CAdapter *omap_i2c_create(QTestState *qts, uint64_t addr)
 {
     OMAPI2C *s = g_malloc0(sizeof(*s));
     I2CAdapter *i2c = (I2CAdapter *)s;
@@ -162,9 +162,10 @@ I2CAdapter *omap_i2c_create(uint64_t addr)

     i2c->send = omap_i2c_send;
     i2c->recv = omap_i2c_recv;
+    i2c->qts = qts;

     /* verify the mmio address by looking for a known signature */
-    data = readw(addr + OMAP_I2C_REV);
+    data = qtest_readw(qts, addr + OMAP_I2C_REV);
     g_assert_cmphex(data, ==, 0x34);

     return i2c;
diff --git a/tests/tmp105-test.c b/tests/tmp105-test.c
index a7940a4639..382f88ba23 100644
--- a/tests/tmp105-test.c
+++ b/tests/tmp105-test.c
@@ -154,15 +154,13 @@ int main(int argc, char **argv)
     s = qtest_start("-machine n800 "
                     "-device tmp105,bus=i2c-bus.0,id=" TMP105_TEST_ID
                     ",address=0x49");
-    i2c = omap_i2c_create(OMAP2_I2C_1_BASE);
+    i2c = omap_i2c_create(s, OMAP2_I2C_1_BASE);

     qtest_add_func("/tmp105/tx-rx", send_and_receive);

     ret = g_test_run();

-    if (s) {
-        qtest_quit(s);
-    }
+    qtest_quit(s);
     g_free(i2c);

     return ret;
-- 
2.13.5

^ permalink raw reply related	[flat|nested] 75+ messages in thread

* [Qemu-devel] [PATCH v7 16/38] libqos: Use explicit QTestState for ahci operations
  2017-09-11 17:19 [Qemu-devel] [PATCH v7 00/38] Preliminary libqtest cleanups Eric Blake
                   ` (14 preceding siblings ...)
  2017-09-11 17:19 ` [Qemu-devel] [PATCH v7 15/38] libqos: Use explicit QTestState for i2c operations Eric Blake
@ 2017-09-11 17:20 ` Eric Blake
  2017-09-11 23:54   ` John Snow
  2017-09-12  9:09   ` Thomas Huth
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 17/38] libqos: Use explicit QTestState for remaining libqos operations Eric Blake
                   ` (21 subsequent siblings)
  37 siblings, 2 replies; 75+ messages in thread
From: Eric Blake @ 2017-09-11 17:20 UTC (permalink / raw)
  To: qemu-devel; +Cc: armbru, pbonzini, thuth, John Snow, open list:IDE

Drop one more client of global_qtest by teaching all ahci test
functionality to pass in an explicit QTestState.  The state was
already available, so no callers had to be adjusted.

Signed-off-by: Eric Blake <eblake@redhat.com>

---
v7: split libqos changes from test-ahci
---
 tests/libqos/ahci.c | 45 +++++++++++++++++++++++----------------------
 1 file changed, 23 insertions(+), 22 deletions(-)

diff --git a/tests/libqos/ahci.c b/tests/libqos/ahci.c
index 790ef991b3..ba79cd77a0 100644
--- a/tests/libqos/ahci.c
+++ b/tests/libqos/ahci.c
@@ -283,7 +283,8 @@ void ahci_hba_enable(AHCIQState *ahci)
         /* Allocate Memory for the Command List Buffer & FIS Buffer */
         /* PxCLB space ... 0x20 per command, as in 4.2.2 p 36 */
         ahci->port[i].clb = ahci_alloc(ahci, num_cmd_slots * 0x20);
-        qmemset(ahci->port[i].clb, 0x00, num_cmd_slots * 0x20);
+        qtest_memset(ahci->parent->qts, ahci->port[i].clb, 0x00,
+                     num_cmd_slots * 0x20);
         g_test_message("CLB: 0x%08" PRIx64, ahci->port[i].clb);
         ahci_px_wreg(ahci, i, AHCI_PX_CLB, ahci->port[i].clb);
         g_assert_cmphex(ahci->port[i].clb, ==,
@@ -291,7 +292,7 @@ void ahci_hba_enable(AHCIQState *ahci)

         /* PxFB space ... 0x100, as in 4.2.1 p 35 */
         ahci->port[i].fb = ahci_alloc(ahci, 0x100);
-        qmemset(ahci->port[i].fb, 0x00, 0x100);
+        qtest_memset(ahci->parent->qts, ahci->port[i].fb, 0x00, 0x100);
         g_test_message("FB: 0x%08" PRIx64, ahci->port[i].fb);
         ahci_px_wreg(ahci, i, AHCI_PX_FB, ahci->port[i].fb);
         g_assert_cmphex(ahci->port[i].fb, ==,
@@ -397,7 +398,7 @@ void ahci_port_clear(AHCIQState *ahci, uint8_t port)
     g_assert_cmphex(ahci_px_rreg(ahci, port, AHCI_PX_IS), ==, 0);

     /* Wipe the FIS-Receive Buffer */
-    qmemset(ahci->port[port].fb, 0x00, 0x100);
+    qtest_memset(ahci->parent->qts, ahci->port[port].fb, 0x00, 0x100);
 }

 /**
@@ -466,7 +467,7 @@ void ahci_port_check_d2h_sanity(AHCIQState *ahci, uint8_t port, uint8_t slot)
     RegD2HFIS *d2h = g_malloc0(0x20);
     uint32_t reg;

-    memread(ahci->port[port].fb + 0x40, d2h, 0x20);
+    qtest_memread(ahci->parent->qts, ahci->port[port].fb + 0x40, d2h, 0x20);
     g_assert_cmphex(d2h->fis_type, ==, 0x34);

     reg = ahci_px_rreg(ahci, port, AHCI_PX_TFD);
@@ -484,7 +485,7 @@ void ahci_port_check_pio_sanity(AHCIQState *ahci, uint8_t port,
     /* We cannot check the Status or E_Status registers, because
      * the status may have again changed between the PIO Setup FIS
      * and the conclusion of the command with the D2H Register FIS. */
-    memread(ahci->port[port].fb + 0x20, pio, 0x20);
+    qtest_memread(ahci->parent->qts, ahci->port[port].fb + 0x20, pio, 0x20);
     g_assert_cmphex(pio->fis_type, ==, 0x5f);

     /* BUG: PIO Setup FIS as utilized by QEMU tries to fit the entire
@@ -516,7 +517,7 @@ void ahci_get_command_header(AHCIQState *ahci, uint8_t port,
 {
     uint64_t ba = ahci->port[port].clb;
     ba += slot * sizeof(AHCICommandHeader);
-    memread(ba, cmd, sizeof(AHCICommandHeader));
+    qtest_memread(ahci->parent->qts, ba, cmd, sizeof(AHCICommandHeader));

     cmd->flags = le16_to_cpu(cmd->flags);
     cmd->prdtl = le16_to_cpu(cmd->prdtl);
@@ -537,7 +538,7 @@ void ahci_set_command_header(AHCIQState *ahci, uint8_t port,
     tmp.prdbc = cpu_to_le32(cmd->prdbc);
     tmp.ctba = cpu_to_le64(cmd->ctba);

-    memwrite(ba, &tmp, sizeof(AHCICommandHeader));
+    qtest_memwrite(ahci->parent->qts, ba, &tmp, sizeof(AHCICommandHeader));
 }

 void ahci_destroy_command(AHCIQState *ahci, uint8_t port, uint8_t slot)
@@ -575,7 +576,7 @@ void ahci_write_fis(AHCIQState *ahci, AHCICommand *cmd)
         tmp.count = cpu_to_le16(tmp.count);
     }

-    memwrite(addr, &tmp, sizeof(tmp));
+    qtest_memwrite(ahci->parent->qts, addr, &tmp, sizeof(tmp));
 }

 unsigned ahci_pick_cmd(AHCIQState *ahci, uint8_t port)
@@ -636,7 +637,7 @@ void ahci_exec(AHCIQState *ahci, uint8_t port,
     if (opts->size && !opts->buffer) {
         opts->buffer = ahci_alloc(ahci, opts->size);
         g_assert(opts->buffer);
-        qmemset(opts->buffer, 0x00, opts->size);
+        qtest_memset(ahci->parent->qts, opts->buffer, 0x00, opts->size);
     }

     /* Command creation */
@@ -661,15 +662,15 @@ void ahci_exec(AHCIQState *ahci, uint8_t port,
     ahci_command_commit(ahci, cmd, port);
     ahci_command_issue_async(ahci, cmd);
     if (opts->error) {
-        qmp_eventwait("STOP");
+        qtest_qmp_eventwait(ahci->parent->qts, "STOP");
     }
     if (opts->mid_cb) {
         rc = opts->mid_cb(ahci, cmd, opts);
         g_assert_cmpint(rc, ==, 0);
     }
     if (opts->error) {
-        qmp_async("{'execute':'cont' }");
-        qmp_eventwait("RESUME");
+        qtest_async_qmp(ahci->parent->qts, "{'execute':'cont' }");
+        qtest_qmp_eventwait(ahci->parent->qts, "RESUME");
     }

     /* Wait for command to complete and verify sanity */
@@ -697,7 +698,7 @@ AHCICommand *ahci_guest_io_halt(AHCIQState *ahci, uint8_t port,
     ahci_command_adjust(cmd, sector, buffer, bufsize, 0);
     ahci_command_commit(ahci, cmd, port);
     ahci_command_issue_async(ahci, cmd);
-    qmp_eventwait("STOP");
+    qtest_qmp_eventwait(ahci->parent->qts, "STOP");

     return cmd;
 }
@@ -706,8 +707,8 @@ AHCICommand *ahci_guest_io_halt(AHCIQState *ahci, uint8_t port,
 void ahci_guest_io_resume(AHCIQState *ahci, AHCICommand *cmd)
 {
     /* Complete the command */
-    qmp_async("{'execute':'cont' }");
-    qmp_eventwait("RESUME");
+    qtest_async_qmp(ahci->parent->qts, "{'execute':'cont' }");
+    qtest_qmp_eventwait(ahci->parent->qts, "RESUME");
     ahci_command_wait(ahci, cmd);
     ahci_command_verify(ahci, cmd);
     ahci_command_free(cmd);
@@ -754,16 +755,16 @@ void ahci_io(AHCIQState *ahci, uint8_t port, uint8_t ide_cmd,
     g_assert(props);
     ptr = ahci_alloc(ahci, bufsize);
     g_assert(!bufsize || ptr);
-    qmemset(ptr, 0x00, bufsize);
+    qtest_memset(ahci->parent->qts, ptr, 0x00, bufsize);

     if (bufsize && props->write) {
-        bufwrite(ptr, buffer, bufsize);
+        qtest_bufwrite(ahci->parent->qts, ptr, buffer, bufsize);
     }

     ahci_guest_io(ahci, port, ide_cmd, ptr, bufsize, sector);

     if (bufsize && props->read) {
-        bufread(ptr, buffer, bufsize);
+        qtest_bufread(ahci->parent->qts, ptr, buffer, bufsize);
     }

     ahci_free(ahci, ptr);
@@ -901,7 +902,7 @@ static int copy_buffer(AHCIQState *ahci, AHCICommand *cmd,
                         const AHCIOpts *opts)
 {
     unsigned char *rx = opts->opaque;
-    bufread(opts->buffer, rx, opts->size);
+    qtest_bufread(ahci->parent->qts, opts->buffer, rx, opts->size);
     return 0;
 }

@@ -1141,7 +1142,7 @@ void ahci_command_commit(AHCIQState *ahci, AHCICommand *cmd, uint8_t port)
     ahci_write_fis(ahci, cmd);
     /* Then ATAPI CMD, if needed */
     if (cmd->props->atapi) {
-        memwrite(table_ptr + 0x40, cmd->atapi_cmd, 16);
+        qtest_memwrite(ahci->parent->qts, table_ptr + 0x40, cmd->atapi_cmd, 16);
     }

     /* Construct and write the PRDs to the command table */
@@ -1162,8 +1163,8 @@ void ahci_command_commit(AHCIQState *ahci, AHCICommand *cmd, uint8_t port)
         prd.dbc |= cpu_to_le32(0x80000000); /* Request DPS Interrupt */

         /* Commit the PRD entry to the Command Table */
-        memwrite(table_ptr + 0x80 + (i * sizeof(PRD)),
-                 &prd, sizeof(PRD));
+        qtest_memwrite(ahci->parent->qts, table_ptr + 0x80 + (i * sizeof(PRD)),
+                       &prd, sizeof(PRD));
     }

     /* Bookmark the PRDTL and CTBA values */
-- 
2.13.5

^ permalink raw reply related	[flat|nested] 75+ messages in thread

* [Qemu-devel] [PATCH v7 17/38] libqos: Use explicit QTestState for remaining libqos operations
  2017-09-11 17:19 [Qemu-devel] [PATCH v7 00/38] Preliminary libqtest cleanups Eric Blake
                   ` (15 preceding siblings ...)
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 16/38] libqos: Use explicit QTestState for ahci operations Eric Blake
@ 2017-09-11 17:20 ` Eric Blake
  2017-09-11 21:30   ` Greg Kurz
  2017-09-12  0:01   ` John Snow
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 18/38] ahci-test: Drop dependence on global_qtest Eric Blake
                   ` (20 subsequent siblings)
  37 siblings, 2 replies; 75+ messages in thread
From: Eric Blake @ 2017-09-11 17:20 UTC (permalink / raw)
  To: qemu-devel
  Cc: armbru, pbonzini, thuth, John Snow, David Gibson, Alexander Graf,
	Gerd Hoffmann, Aneesh Kumar K.V, Greg Kurz, Stefan Hajnoczi,
	Jason Wang, open list:IDE, open list:sPAPR

Drop one more client of global_qtest by teaching all remaining
libqos stragglers to pass in an explicit QTestState.  Change the
setting of global_qtest from being implicit in libqos' call to
qtest_start() to instead be explicit in all clients that are
still relying on global_qtest.

Note that qmp_execute() can be greatly simplified in the process,
and that we also get rid of interpolation of a JSON string into a
temporary variable when qtest_qmp() can do it more reliably.

Signed-off-by: Eric Blake <eblake@redhat.com>

---
v7: split libqos changes from test-ahci, catch more spots
---
 tests/ahci-test.c         |  1 +
 tests/ivshmem-test.c      |  1 +
 tests/libqos/libqos-pc.c  |  2 +-
 tests/libqos/libqos.c     | 30 ++++++++++--------------------
 tests/megasas-test.c      |  5 ++++-
 tests/rtas-test.c         |  1 +
 tests/usb-hcd-uhci-test.c |  1 +
 tests/virtio-9p-test.c    |  1 +
 tests/virtio-blk-test.c   |  1 +
 tests/virtio-net-test.c   | 15 +++++++++------
 tests/virtio-scsi-test.c  | 16 +++++++++-------
 11 files changed, 39 insertions(+), 35 deletions(-)

diff --git a/tests/ahci-test.c b/tests/ahci-test.c
index c94d1bd712..d6696cc370 100644
--- a/tests/ahci-test.c
+++ b/tests/ahci-test.c
@@ -157,6 +157,7 @@ static AHCIQState *ahci_vboot(const char *cli, va_list ap)

     s = g_malloc0(sizeof(AHCIQState));
     s->parent = qtest_pc_vboot(cli, ap);
+    global_qtest = s->parent->qts;
     alloc_set_flags(s->parent->alloc, ALLOC_LEAK_ASSERT);

     /* Verify that we have an AHCI device present. */
diff --git a/tests/ivshmem-test.c b/tests/ivshmem-test.c
index d35c922254..d8f8530a4d 100644
--- a/tests/ivshmem-test.c
+++ b/tests/ivshmem-test.c
@@ -131,6 +131,7 @@ static void setup_vm_cmd(IVState *s, const char *cmd, bool msix)
         g_printerr("ivshmem-test tests are only available on x86 or ppc64\n");
         exit(EXIT_FAILURE);
     }
+    global_qtest = s->qs->qts;
     s->dev = get_device(s->qs->pcibus);

     s->reg_bar = qpci_iomap(s->dev, 0, &barsize);
diff --git a/tests/libqos/libqos-pc.c b/tests/libqos/libqos-pc.c
index b554758802..a9c1aceaa7 100644
--- a/tests/libqos/libqos-pc.c
+++ b/tests/libqos/libqos-pc.c
@@ -25,7 +25,7 @@ QOSState *qtest_pc_boot(const char *cmdline_fmt, ...)
     qs = qtest_vboot(&qos_ops, cmdline_fmt, ap);
     va_end(ap);

-    qtest_irq_intercept_in(global_qtest, "ioapic");
+    qtest_irq_intercept_in(qs->qts, "ioapic");

     return qs;
 }
diff --git a/tests/libqos/libqos.c b/tests/libqos/libqos.c
index 40018b9c07..9798e1d027 100644
--- a/tests/libqos/libqos.c
+++ b/tests/libqos/libqos.c
@@ -20,7 +20,7 @@ QOSState *qtest_vboot(QOSOps *ops, const char *cmdline_fmt, va_list ap)
     QOSState *qs = g_new0(QOSState, 1);

     cmdline = g_strdup_vprintf(cmdline_fmt, ap);
-    qs->qts = qtest_start(cmdline);
+    qs->qts = qtest_init(cmdline);
     qs->ops = ops;
     if (ops) {
         qs->alloc = ops->init_allocator(qs->qts, ALLOC_NO_FLAGS);
@@ -80,29 +80,21 @@ void set_context(QOSState *s)
     global_qtest = s->qts;
 }

-static QDict *qmp_execute(const char *command)
+static QDict *qmp_execute(QTestState *qts, const char *command)
 {
-    char *fmt;
-    QDict *rsp;
-
-    fmt = g_strdup_printf("{ 'execute': '%s' }", command);
-    rsp = qmp(fmt);
-    g_free(fmt);
-
-    return rsp;
+    return qtest_qmp(qts, "{ 'execute': %s }", command);
 }

 void migrate(QOSState *from, QOSState *to, const char *uri)
 {
     const char *st;
-    char *s;
     QDict *rsp, *sub;
     bool running;

     set_context(from);

     /* Is the machine currently running? */
-    rsp = qmp_execute("query-status");
+    rsp = qmp_execute(from->qts, "query-status");
     g_assert(qdict_haskey(rsp, "return"));
     sub = qdict_get_qdict(rsp, "return");
     g_assert(qdict_haskey(sub, "running"));
@@ -110,30 +102,28 @@ void migrate(QOSState *from, QOSState *to, const char *uri)
     QDECREF(rsp);

     /* Issue the migrate command. */
-    s = g_strdup_printf("{ 'execute': 'migrate',"
-                        "'arguments': { 'uri': '%s' } }",
-                        uri);
-    rsp = qmp(s);
-    g_free(s);
+    rsp = qtest_qmp(from->qts,
+                    "{ 'execute': 'migrate', 'arguments': { 'uri': %s }}",
+                    uri);
     g_assert(qdict_haskey(rsp, "return"));
     QDECREF(rsp);

     /* Wait for STOP event, but only if we were running: */
     if (running) {
-        qmp_eventwait("STOP");
+        qtest_qmp_eventwait(from->qts, "STOP");
     }

     /* If we were running, we can wait for an event. */
     if (running) {
         migrate_allocator(from->alloc, to->alloc);
         set_context(to);
-        qmp_eventwait("RESUME");
+        qtest_qmp_eventwait(to->qts, "RESUME");
         return;
     }

     /* Otherwise, we need to wait: poll until migration is completed. */
     while (1) {
-        rsp = qmp_execute("query-migrate");
+        rsp = qmp_execute(from->qts, "query-migrate");
         g_assert(qdict_haskey(rsp, "return"));
         sub = qdict_get_qdict(rsp, "return");
         g_assert(qdict_haskey(sub, "status"));
diff --git a/tests/megasas-test.c b/tests/megasas-test.c
index ce960e7f81..81837e14af 100644
--- a/tests/megasas-test.c
+++ b/tests/megasas-test.c
@@ -15,13 +15,16 @@

 static QOSState *qmegasas_start(const char *extra_opts)
 {
+    QOSState *qs;
     const char *arch = qtest_get_arch();
     const char *cmd = "-drive id=hd0,if=none,file=null-co://,format=raw "
                       "-device megasas,id=scsi0,addr=04.0 "
                       "-device scsi-hd,bus=scsi0.0,drive=hd0 %s";

     if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
-        return qtest_pc_boot(cmd, extra_opts ? : "");
+        qs = qtest_pc_boot(cmd, extra_opts ? : "");
+        global_qtest = qs->qts;
+        return qs;
     }

     g_printerr("virtio-scsi tests are only available on x86 or ppc64\n");
diff --git a/tests/rtas-test.c b/tests/rtas-test.c
index 34a692163f..009bda6d23 100644
--- a/tests/rtas-test.c
+++ b/tests/rtas-test.c
@@ -14,6 +14,7 @@ static void test_rtas_get_time_of_day(void)
     time_t t1, t2;

     qs = qtest_spapr_boot("-machine pseries");
+    global_qtest = qs->qts;

     t1 = time(NULL);
     ret = qrtas_get_time_of_day(qs->qts, qs->alloc, &tm, &ns);
diff --git a/tests/usb-hcd-uhci-test.c b/tests/usb-hcd-uhci-test.c
index 0fb7f8d223..290416d845 100644
--- a/tests/usb-hcd-uhci-test.c
+++ b/tests/usb-hcd-uhci-test.c
@@ -95,6 +95,7 @@ int main(int argc, char **argv)
                    "available on x86 or ppc64\n");
         exit(EXIT_FAILURE);
     }
+    global_qtest = qs->qts;
     ret = g_test_run();
     qtest_shutdown(qs);

diff --git a/tests/virtio-9p-test.c b/tests/virtio-9p-test.c
index ad33d96387..07cbb7f37c 100644
--- a/tests/virtio-9p-test.c
+++ b/tests/virtio-9p-test.c
@@ -46,6 +46,7 @@ static QVirtIO9P *qvirtio_9p_start(const char *driver)
         g_printerr("virtio-9p tests are only available on x86 or ppc64\n");
         exit(EXIT_FAILURE);
     }
+    global_qtest = v9p->qs->qts;

     return v9p;
 }
diff --git a/tests/virtio-blk-test.c b/tests/virtio-blk-test.c
index 99bb6f26cd..0cf33b6810 100644
--- a/tests/virtio-blk-test.c
+++ b/tests/virtio-blk-test.c
@@ -77,6 +77,7 @@ static QOSState *pci_test_start(void)
         g_printerr("virtio-blk tests are only available on x86 or ppc64\n");
         exit(EXIT_FAILURE);
     }
+    global_qtest = qs->qts;
     unlink(tmp_path);
     g_free(tmp_path);
     return qs;
diff --git a/tests/virtio-net-test.c b/tests/virtio-net-test.c
index 8b43d3dd65..acecef0a3e 100644
--- a/tests/virtio-net-test.c
+++ b/tests/virtio-net-test.c
@@ -53,18 +53,21 @@ static QVirtioPCIDevice *virtio_net_pci_init(QPCIBus *bus, int slot)

 static QOSState *pci_test_start(int socket)
 {
+    QOSState *qs;
     const char *arch = qtest_get_arch();
     const char *cmd = "-netdev socket,fd=%d,id=hs0 -device "
                       "virtio-net-pci,netdev=hs0";

     if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
-        return qtest_pc_boot(cmd, socket);
+        qs = qtest_pc_boot(cmd, socket);
+    } else if (strcmp(arch, "ppc64") == 0) {
+        qs = qtest_spapr_boot(cmd, socket);
+    } else {
+        g_printerr("virtio-net tests are only available on x86 or ppc64\n");
+        exit(EXIT_FAILURE);
     }
-    if (strcmp(arch, "ppc64") == 0) {
-        return qtest_spapr_boot(cmd, socket);
-    }
-    g_printerr("virtio-net tests are only available on x86 or ppc64\n");
-    exit(EXIT_FAILURE);
+    global_qtest = qs->qts;
+    return qs;
 }

 static void driver_init(QVirtioDevice *dev)
diff --git a/tests/virtio-scsi-test.c b/tests/virtio-scsi-test.c
index 87a3b6e81a..4d544ddbc3 100644
--- a/tests/virtio-scsi-test.c
+++ b/tests/virtio-scsi-test.c
@@ -34,20 +34,22 @@ typedef struct {

 static QOSState *qvirtio_scsi_start(const char *extra_opts)
 {
+    QOSState *qs;
     const char *arch = qtest_get_arch();
     const char *cmd = "-drive id=drv0,if=none,file=null-co://,format=raw "
                       "-device virtio-scsi-pci,id=vs0 "
                       "-device scsi-hd,bus=vs0.0,drive=drv0 %s";

     if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
-        return qtest_pc_boot(cmd, extra_opts ? : "");
+        qs = qtest_pc_boot(cmd, extra_opts ? : "");
+    } else if (strcmp(arch, "ppc64") == 0) {
+        qs = qtest_spapr_boot(cmd, extra_opts ? : "");
+    } else {
+        g_printerr("virtio-scsi tests are only available on x86 or ppc64\n");
+        exit(EXIT_FAILURE);
     }
-    if (strcmp(arch, "ppc64") == 0) {
-        return qtest_spapr_boot(cmd, extra_opts ? : "");
-    }
-
-    g_printerr("virtio-scsi tests are only available on x86 or ppc64\n");
-    exit(EXIT_FAILURE);
+    global_qtest = qs->qts;
+    return qs;
 }

 static void qvirtio_scsi_stop(QOSState *qs)
-- 
2.13.5

^ permalink raw reply related	[flat|nested] 75+ messages in thread

* [Qemu-devel] [PATCH v7 18/38] ahci-test: Drop dependence on global_qtest
  2017-09-11 17:19 [Qemu-devel] [PATCH v7 00/38] Preliminary libqtest cleanups Eric Blake
                   ` (16 preceding siblings ...)
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 17/38] libqos: Use explicit QTestState for remaining libqos operations Eric Blake
@ 2017-09-11 17:20 ` Eric Blake
  2017-09-12  0:20   ` John Snow
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 19/38] ivshmem-test: " Eric Blake
                   ` (19 subsequent siblings)
  37 siblings, 1 reply; 75+ messages in thread
From: Eric Blake @ 2017-09-11 17:20 UTC (permalink / raw)
  To: qemu-devel; +Cc: armbru, pbonzini, thuth, John Snow, open list:IDE

Managing parallel connections to two different monitors via
the implicit global_qtest makes it hard to copy-and-paste code
to tests that are not aware of the implicit state; the
management of global_qtest is even harder to follow because
it was masked behind set_context().

Instead, explicitly pass QTestState* around (generally, by
reusing the member already present in ahci->parent QOSState),
and call explicit qtest_* functions on all places that
interact with a monitor.

We can assert that the conversion is correct by checking that
global_qtest remains NULL throughout the test (a later patch
that changes global_qtest to not be a public global variable
will drop the assertions).

Bonus: there was one spots that was creating a needless temporary
variable to execute the 'cont' command, rather than just directly
passing the literal command through qtest_qmp().  Fixing that
gets us one step closer to enabling -Wformat checking on
constructed JSON.

Signed-off-by: Eric Blake <eblake@redhat.com>

---
v7: split bulk of libqos changes into earlier patch
---
 tests/libqos/libqos.h |  1 -
 tests/ahci-test.c     | 83 ++++++++++++++++++++++++---------------------------
 tests/libqos/libqos.c |  9 ------
 3 files changed, 39 insertions(+), 54 deletions(-)

diff --git a/tests/libqos/libqos.h b/tests/libqos/libqos.h
index 07d4b93d1d..9e020efb10 100644
--- a/tests/libqos/libqos.h
+++ b/tests/libqos/libqos.h
@@ -29,7 +29,6 @@ void qtest_shutdown(QOSState *qs);
 bool have_qemu_img(void);
 void mkimg(const char *file, const char *fmt, unsigned size_mb);
 void mkqcow2(const char *file, unsigned size_mb);
-void set_context(QOSState *s);
 void migrate(QOSState *from, QOSState *to, const char *uri);
 void prepare_blkdebug_script(const char *debug_fn, const char *event);
 void generate_pattern(void *buffer, size_t len, size_t cycle_len);
diff --git a/tests/ahci-test.c b/tests/ahci-test.c
index d6696cc370..7c94c4920b 100644
--- a/tests/ahci-test.c
+++ b/tests/ahci-test.c
@@ -157,7 +157,6 @@ static AHCIQState *ahci_vboot(const char *cli, va_list ap)

     s = g_malloc0(sizeof(AHCIQState));
     s->parent = qtest_pc_vboot(cli, ap);
-    global_qtest = s->parent->qts;
     alloc_set_flags(s->parent->alloc, ALLOC_LEAK_ASSERT);

     /* Verify that we have an AHCI device present. */
@@ -197,7 +196,7 @@ static void ahci_shutdown(AHCIQState *ahci)
 {
     QOSState *qs = ahci->parent;

-    set_context(qs);
+    assert(!global_qtest);
     ahci_clean_mem(ahci);
     free_ahci_device(ahci->dev);
     g_free(ahci);
@@ -870,15 +869,15 @@ static void ahci_test_io_rw_simple(AHCIQState *ahci, unsigned bufsize,

     /* Write some indicative pattern to our buffer. */
     generate_pattern(tx, bufsize, AHCI_SECTOR_SIZE);
-    bufwrite(ptr, tx, bufsize);
+    qtest_bufwrite(ahci->parent->qts, ptr, tx, bufsize);

     /* Write this buffer to disk, then read it back to the DMA buffer. */
     ahci_guest_io(ahci, port, write_cmd, ptr, bufsize, sector);
-    qmemset(ptr, 0x00, bufsize);
+    qtest_memset(ahci->parent->qts, ptr, 0x00, bufsize);
     ahci_guest_io(ahci, port, read_cmd, ptr, bufsize, sector);

     /*** Read back the Data ***/
-    bufread(ptr, rx, bufsize);
+    qtest_bufread(ahci->parent->qts, ptr, rx, bufsize);
     g_assert_cmphex(memcmp(tx, rx, bufsize), ==, 0);

     ahci_free(ahci, ptr);
@@ -919,7 +918,7 @@ static void ahci_test_max(AHCIQState *ahci)
     }

     port = ahci_test_nondata(ahci, cmd);
-    memread(ahci->port[port].fb + 0x40, d2h, 0x20);
+    qtest_memread(ahci->parent->qts, ahci->port[port].fb + 0x40, d2h, 0x20);
     nsect = (uint64_t)d2h->lba_hi[2] << 40 |
         (uint64_t)d2h->lba_hi[1] << 32 |
         (uint64_t)d2h->lba_hi[0] << 24 |
@@ -1037,7 +1036,7 @@ static void test_dma_fragmented(void)
     /* Create a DMA buffer in guest memory, and write our pattern to it. */
     ptr = guest_alloc(ahci->parent->alloc, bufsize);
     g_assert(ptr);
-    bufwrite(ptr, tx, bufsize);
+    qtest_bufwrite(ahci->parent->qts, ptr, tx, bufsize);

     cmd = ahci_command_create(CMD_WRITE_DMA);
     ahci_command_adjust(cmd, 0, ptr, bufsize, 32);
@@ -1054,7 +1053,7 @@ static void test_dma_fragmented(void)
     ahci_command_free(cmd);

     /* Read back the guest's receive buffer into local memory */
-    bufread(ptr, rx, bufsize);
+    qtest_bufread(ahci->parent->qts, ptr, rx, bufsize);
     guest_free(ahci->parent->alloc, ptr);

     g_assert_cmphex(memcmp(tx, rx, bufsize), ==, 0);
@@ -1165,8 +1164,6 @@ static void ahci_migrate_simple(uint8_t cmd_read, uint8_t cmd_write)
                     "-drive if=ide,format=%s,file=%s "
                     "-incoming %s", imgfmt, tmp_path, uri);

-    set_context(src->parent);
-
     /* initialize */
     px = ahci_port_select(src);
     ahci_port_clear(src, px);
@@ -1234,7 +1231,7 @@ static void ahci_halted_io_test(uint8_t cmd_read, uint8_t cmd_write)
     generate_pattern(tx, bufsize, AHCI_SECTOR_SIZE);
     ptr = ahci_alloc(ahci, bufsize);
     g_assert(ptr);
-    memwrite(ptr, tx, bufsize);
+    qtest_memwrite(ahci->parent->qts, ptr, tx, bufsize);

     /* Attempt to write (and fail) */
     cmd = ahci_guest_io_halt(ahci, port, cmd_write,
@@ -1300,8 +1297,6 @@ static void ahci_migrate_halted_io(uint8_t cmd_read, uint8_t cmd_write)
                     "-incoming %s",
                     tmp_path, imgfmt, uri);

-    set_context(src->parent);
-
     /* Initialize and prepare */
     port = ahci_port_select(src);
     ahci_port_clear(src, port);
@@ -1310,7 +1305,7 @@ static void ahci_migrate_halted_io(uint8_t cmd_read, uint8_t cmd_write)
     /* create DMA source buffer and write pattern */
     ptr = ahci_alloc(src, bufsize);
     g_assert(ptr);
-    memwrite(ptr, tx, bufsize);
+    qtest_memwrite(src->parent->qts, ptr, tx, bufsize);

     /* Write, trigger the VM to stop, migrate, then resume. */
     cmd = ahci_guest_io_halt(src, port, cmd_write,
@@ -1351,7 +1346,6 @@ static void test_flush_migrate(void)
     AHCIQState *src, *dst;
     AHCICommand *cmd;
     uint8_t px;
-    const char *s;
     char *uri = g_strdup_printf("unix:%s", mig_socket);

     prepare_blkdebug_script(debug_path, "flush_to_disk");
@@ -1369,8 +1363,6 @@ static void test_flush_migrate(void)
                     "-device ide-hd,drive=drive0 "
                     "-incoming %s", tmp_path, imgfmt, uri);

-    set_context(src->parent);
-
     px = ahci_port_select(src);
     ahci_port_clear(src, px);

@@ -1381,15 +1373,14 @@ static void test_flush_migrate(void)
     cmd = ahci_command_create(CMD_FLUSH_CACHE);
     ahci_command_commit(src, cmd, px);
     ahci_command_issue_async(src, cmd);
-    qmp_eventwait("STOP");
+    qtest_qmp_eventwait(src->parent->qts, "STOP");

     /* Migrate over */
     ahci_migrate(src, dst, uri);

     /* Complete the command */
-    s = "{'execute':'cont' }";
-    qmp_async(s);
-    qmp_eventwait("RESUME");
+    qtest_async_qmp(dst->parent->qts, "{'execute':'cont'}");
+    qtest_qmp_eventwait(dst->parent->qts, "RESUME");
     ahci_command_wait(dst, cmd);
     ahci_command_verify(dst, cmd);

@@ -1481,7 +1472,7 @@ static int ahci_cb_cmp_buff(AHCIQState *ahci, AHCICommand *cmd,
     }

     rx = g_malloc0(opts->size);
-    bufread(opts->buffer, rx, opts->size);
+    qtest_bufread(ahci->parent->qts, opts->buffer, rx, opts->size);
     g_assert_cmphex(memcmp(tx, rx, opts->size), ==, 0);
     g_free(rx);

@@ -1556,9 +1547,10 @@ static void test_atapi_bcl(void)
 }


-static void atapi_wait_tray(bool open)
+static void atapi_wait_tray(AHCIQState *ahci, bool open)
 {
-    QDict *rsp = qmp_eventwait_ref("DEVICE_TRAY_MOVED");
+    QDict *rsp = qtest_qmp_eventwait_ref(ahci->parent->qts,
+                                         "DEVICE_TRAY_MOVED");
     QDict *data = qdict_get_qdict(rsp, "data");
     if (open) {
         g_assert(qdict_get_bool(data, "tray-open"));
@@ -1585,43 +1577,46 @@ static void test_atapi_tray(void)
     port = ahci_port_select(ahci);

     ahci_atapi_eject(ahci, port);
-    atapi_wait_tray(true);
+    atapi_wait_tray(ahci, true);

     ahci_atapi_load(ahci, port);
-    atapi_wait_tray(false);
+    atapi_wait_tray(ahci, false);

     /* Remove media */
-    qmp_async("{'execute': 'blockdev-open-tray', "
-               "'arguments': {'device': 'drive0'}}");
-    atapi_wait_tray(true);
-    rsp = qmp_receive();
+    qtest_async_qmp(ahci->parent->qts, "{'execute': 'blockdev-open-tray', "
+                    "'arguments': {'device': 'drive0'}}");
+    atapi_wait_tray(ahci, true);
+    rsp = qtest_qmp_receive(ahci->parent->qts);
     QDECREF(rsp);

-    qmp_discard_response("{'execute': 'x-blockdev-remove-medium', "
-                         "'arguments': {'device': 'drive0'}}");
+    qtest_qmp_discard_response(ahci->parent->qts,
+                               "{'execute': 'x-blockdev-remove-medium', "
+                               "'arguments': {'device': 'drive0'}}");

     /* Test the tray without a medium */
     ahci_atapi_load(ahci, port);
-    atapi_wait_tray(false);
+    atapi_wait_tray(ahci, false);

     ahci_atapi_eject(ahci, port);
-    atapi_wait_tray(true);
+    atapi_wait_tray(ahci, true);

     /* Re-insert media */
-    qmp_discard_response("{'execute': 'blockdev-add', "
-                          "'arguments': {'node-name': 'node0', "
+    qtest_qmp_discard_response(ahci->parent->qts,
+                               "{'execute': 'blockdev-add', "
+                               "'arguments': {'node-name': 'node0', "
                                         "'driver': 'raw', "
                                         "'file': { 'driver': 'file', "
                                                   "'filename': %s }}}", iso);
-    qmp_discard_response("{'execute': 'x-blockdev-insert-medium',"
-                          "'arguments': { 'device': 'drive0', "
+    qtest_qmp_discard_response(ahci->parent->qts,
+                               "{'execute': 'x-blockdev-insert-medium',"
+                               "'arguments': { 'device': 'drive0', "
                                          "'node-name': 'node0' }}");

     /* Again, the event shows up first */
-    qmp_async("{'execute': 'blockdev-close-tray', "
-               "'arguments': {'device': 'drive0'}}");
-    atapi_wait_tray(false);
-    rsp = qmp_receive();
+    qtest_async_qmp(ahci->parent->qts, "{'execute': 'blockdev-close-tray', "
+                    "'arguments': {'device': 'drive0'}}");
+    atapi_wait_tray(ahci, false);
+    rsp = qtest_qmp_receive(ahci->parent->qts);
     QDECREF(rsp);

     /* Now, to convince ATAPI we understand the media has changed... */
@@ -1641,10 +1636,10 @@ static void test_atapi_tray(void)

     /* Final tray test. */
     ahci_atapi_eject(ahci, port);
-    atapi_wait_tray(true);
+    atapi_wait_tray(ahci, true);

     ahci_atapi_load(ahci, port);
-    atapi_wait_tray(false);
+    atapi_wait_tray(ahci, false);

     /* Cleanup */
     g_free(tx);
diff --git a/tests/libqos/libqos.c b/tests/libqos/libqos.c
index 9798e1d027..3ff61246a4 100644
--- a/tests/libqos/libqos.c
+++ b/tests/libqos/libqos.c
@@ -75,11 +75,6 @@ void qtest_shutdown(QOSState *qs)
     }
 }

-void set_context(QOSState *s)
-{
-    global_qtest = s->qts;
-}
-
 static QDict *qmp_execute(QTestState *qts, const char *command)
 {
     return qtest_qmp(qts, "{ 'execute': %s }", command);
@@ -91,8 +86,6 @@ void migrate(QOSState *from, QOSState *to, const char *uri)
     QDict *rsp, *sub;
     bool running;

-    set_context(from);
-
     /* Is the machine currently running? */
     rsp = qmp_execute(from->qts, "query-status");
     g_assert(qdict_haskey(rsp, "return"));
@@ -116,7 +109,6 @@ void migrate(QOSState *from, QOSState *to, const char *uri)
     /* If we were running, we can wait for an event. */
     if (running) {
         migrate_allocator(from->alloc, to->alloc);
-        set_context(to);
         qtest_qmp_eventwait(to->qts, "RESUME");
         return;
     }
@@ -146,7 +138,6 @@ void migrate(QOSState *from, QOSState *to, const char *uri)
     }

     migrate_allocator(from->alloc, to->alloc);
-    set_context(to);
 }

 bool have_qemu_img(void)
-- 
2.13.5

^ permalink raw reply related	[flat|nested] 75+ messages in thread

* [Qemu-devel] [PATCH v7 19/38] ivshmem-test: Drop dependence on global_qtest
  2017-09-11 17:19 [Qemu-devel] [PATCH v7 00/38] Preliminary libqtest cleanups Eric Blake
                   ` (17 preceding siblings ...)
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 18/38] ahci-test: Drop dependence on global_qtest Eric Blake
@ 2017-09-11 17:20 ` Eric Blake
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 20/38] postcopy-test: " Eric Blake
                   ` (18 subsequent siblings)
  37 siblings, 0 replies; 75+ messages in thread
From: Eric Blake @ 2017-09-11 17:20 UTC (permalink / raw)
  To: qemu-devel; +Cc: armbru, pbonzini, thuth

Managing parallel connections to two different monitors via
the implicit global_qtest makes it hard to copy-and-paste code
to tests that are not aware of the implicit state.  Since we
have already fixed qpci to avoid global_qtest, we can now
simplify by not using global_qtest anywhere in ivshmem-test.

We can assert that the conversion is correct by checking that
global_qtest remains NULL throughout the test (a later patch
that changes global_qtest to not be a public global variable
will drop the assertions).

Signed-off-by: Eric Blake <eblake@redhat.com>

---
v7: fix a couple more spots
---
 tests/ivshmem-test.c | 28 +++++++---------------------
 1 file changed, 7 insertions(+), 21 deletions(-)

diff --git a/tests/ivshmem-test.c b/tests/ivshmem-test.c
index d8f8530a4d..58eaf0cafb 100644
--- a/tests/ivshmem-test.c
+++ b/tests/ivshmem-test.c
@@ -71,13 +71,10 @@ static const char* reg2str(enum Reg reg) {
 static inline unsigned in_reg(IVState *s, enum Reg reg)
 {
     const char *name = reg2str(reg);
-    QTestState *qtest = global_qtest;
     unsigned res;

-    global_qtest = s->qs->qts;
     res = qpci_io_readl(s->dev, s->reg_bar, reg);
     g_test_message("*%s -> %x\n", name, res);
-    global_qtest = qtest;

     return res;
 }
@@ -85,35 +82,25 @@ static inline unsigned in_reg(IVState *s, enum Reg reg)
 static inline void out_reg(IVState *s, enum Reg reg, unsigned v)
 {
     const char *name = reg2str(reg);
-    QTestState *qtest = global_qtest;

-    global_qtest = s->qs->qts;
     g_test_message("%x -> *%s\n", v, name);
     qpci_io_writel(s->dev, s->reg_bar, reg, v);
-    global_qtest = qtest;
 }

 static inline void read_mem(IVState *s, uint64_t off, void *buf, size_t len)
 {
-    QTestState *qtest = global_qtest;
-
-    global_qtest = s->qs->qts;
     qpci_memread(s->dev, s->mem_bar, off, buf, len);
-    global_qtest = qtest;
 }

 static inline void write_mem(IVState *s, uint64_t off,
                              const void *buf, size_t len)
 {
-    QTestState *qtest = global_qtest;
-
-    global_qtest = s->qs->qts;
     qpci_memwrite(s->dev, s->mem_bar, off, buf, len);
-    global_qtest = qtest;
 }

 static void cleanup_vm(IVState *s)
 {
+    assert(!global_qtest);
     g_free(s->dev);
     qtest_shutdown(s->qs);
 }
@@ -131,7 +118,6 @@ static void setup_vm_cmd(IVState *s, const char *cmd, bool msix)
         g_printerr("ivshmem-test tests are only available on x86 or ppc64\n");
         exit(EXIT_FAILURE);
     }
-    global_qtest = s->qs->qts;
     s->dev = get_device(s->qs->pcibus);

     s->reg_bar = qpci_iomap(s->dev, 0, &barsize);
@@ -354,7 +340,6 @@ static void test_ivshmem_server(bool msi)
     g_assert_cmpint(vm1, !=, vm2);

     /* check number of MSI-X vectors */
-    global_qtest = s1->qs->qts;
     if (msi) {
         ret = qpci_msix_table_size(s1->dev);
         g_assert_cmpuint(ret, ==, nvectors);
@@ -377,7 +362,6 @@ static void test_ivshmem_server(bool msi)
     g_assert_cmpuint(ret, !=, 0);

     /* ping vm1 -> vm2 on vector 1 */
-    global_qtest = s2->qs->qts;
     if (msi) {
         ret = qpci_msix_pending(s2->dev, 1);
         g_assert_cmpuint(ret, ==, 0);
@@ -421,17 +405,19 @@ static void test_ivshmem_hotplug(void)
 {
     const char *arch = qtest_get_arch();
     gchar *opts;
+    QTestState *qts;

-    qtest_start("");
+    qts = qtest_init("");

     opts = g_strdup_printf("'shm': '%s', 'size': '1M'", tmpshm);

-    qpci_plug_device_test(global_qtest, "ivshmem", "iv1", PCI_SLOT_HP, opts);
+    qpci_plug_device_test(qts, "ivshmem", "iv1", PCI_SLOT_HP, opts);
     if (strcmp(arch, "ppc64") != 0) {
-        qpci_unplug_device_test(global_qtest, "iv1", PCI_SLOT_HP);
+        qpci_unplug_device_test(qts, "iv1", PCI_SLOT_HP);
     }

-    qtest_end();
+    assert(!global_qtest);
+    qtest_quit(qts);
     g_free(opts);
 }

-- 
2.13.5

^ permalink raw reply related	[flat|nested] 75+ messages in thread

* [Qemu-devel] [PATCH v7 20/38] postcopy-test: Drop dependence on global_qtest
  2017-09-11 17:19 [Qemu-devel] [PATCH v7 00/38] Preliminary libqtest cleanups Eric Blake
                   ` (18 preceding siblings ...)
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 19/38] ivshmem-test: " Eric Blake
@ 2017-09-11 17:20 ` Eric Blake
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 21/38] vhost-user-test: " Eric Blake
                   ` (17 subsequent siblings)
  37 siblings, 0 replies; 75+ messages in thread
From: Eric Blake @ 2017-09-11 17:20 UTC (permalink / raw)
  To: qemu-devel; +Cc: armbru, pbonzini, thuth

Managing parallel connections to two different monitors via
the implicit global_qtest makes it hard to copy-and-paste code
to tests that are not aware of the implicit state.  We are
already half-using the explicit APIs, so fix the remaining
spots to also be explicit about which connection is in use,
and avoid global_qtest.

We can assert that the conversion is correct by checking that
global_qtest remains NULL throughout the test (a later patch
that changes global_qtest to not be a public global variable
will drop the assertions).

Signed-off-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Thomas Huth <thuth@redhat.com>
---
 tests/postcopy-test.c | 84 ++++++++++++++++++++++++---------------------------
 1 file changed, 40 insertions(+), 44 deletions(-)

diff --git a/tests/postcopy-test.c b/tests/postcopy-test.c
index 8142f2ab90..0e5fe20a83 100644
--- a/tests/postcopy-test.c
+++ b/tests/postcopy-test.c
@@ -223,7 +223,7 @@ static void wait_for_serial(const char *side)
 /*
  * Events can get in the way of responses we are actually waiting for.
  */
-static QDict *return_or_event(QDict *response)
+static QDict *return_or_event(QTestState *qts, QDict *response)
 {
     const char *event_string;
     if (!qdict_haskey(response, "event")) {
@@ -236,7 +236,7 @@ static QDict *return_or_event(QDict *response)
         got_stop = true;
     }
     QDECREF(response);
-    return return_or_event(qtest_qmp_receive(global_qtest));
+    return return_or_event(qts, qtest_qmp_receive(qts));
 }


@@ -247,12 +247,13 @@ static QDict *return_or_event(QDict *response)
  * going postcopy.
  */

-static uint64_t get_migration_pass(void)
+static uint64_t get_migration_pass(QTestState *qts)
 {
     QDict *rsp, *rsp_return, *rsp_ram;
     uint64_t result;

-    rsp = return_or_event(qmp("{ 'execute': 'query-migrate' }"));
+    rsp = return_or_event(qts,
+                          qtest_qmp(qts, "{ 'execute': 'query-migrate' }"));
     rsp_return = qdict_get_qdict(rsp, "return");
     if (!qdict_haskey(rsp_return, "ram")) {
         /* Still in setup */
@@ -265,7 +266,7 @@ static uint64_t get_migration_pass(void)
     return result;
 }

-static void wait_for_migration_complete(void)
+static void wait_for_migration_complete(QTestState *qts)
 {
     QDict *rsp, *rsp_return;
     bool completed;
@@ -273,7 +274,8 @@ static void wait_for_migration_complete(void)
     do {
         const char *status;

-        rsp = return_or_event(qmp("{ 'execute': 'query-migrate' }"));
+        rsp = return_or_event(qts,
+                              qtest_qmp(qts, "{ 'execute': 'query-migrate' }"));
         rsp_return = qdict_get_qdict(rsp, "return");
         status = qdict_get_str(rsp_return, "status");
         completed = strcmp(status, "completed") == 0;
@@ -283,14 +285,14 @@ static void wait_for_migration_complete(void)
     } while (!completed);
 }

-static void wait_for_migration_pass(void)
+static void wait_for_migration_pass(QTestState *qts)
 {
-    uint64_t initial_pass = get_migration_pass();
+    uint64_t initial_pass = get_migration_pass(qts);
     uint64_t pass;

     /* Wait for the 1st sync */
     do {
-        initial_pass = get_migration_pass();
+        initial_pass = get_migration_pass(qts);
         if (got_stop || initial_pass) {
             break;
         }
@@ -299,11 +301,11 @@ static void wait_for_migration_pass(void)

     do {
         usleep(1000 * 100);
-        pass = get_migration_pass();
+        pass = get_migration_pass(qts);
     } while (pass == initial_pass && !got_stop);
 }

-static void check_guests_ram(void)
+static void check_guests_ram(QTestState *qts)
 {
     /* Our ASM test will have been incrementing one byte from each page from
      * 1MB to <100MB in order.
@@ -318,13 +320,13 @@ static void check_guests_ram(void)
     bool hit_edge = false;
     bool bad = false;

-    qtest_memread(global_qtest, start_address, &first_byte, 1);
+    qtest_memread(qts, start_address, &first_byte, 1);
     last_byte = first_byte;

     for (address = start_address + 4096; address < end_address; address += 4096)
     {
         uint8_t b;
-        qtest_memread(global_qtest, address, &b, 1);
+        qtest_memread(qts, address, &b, 1);
         if (b != last_byte) {
             if (((b + 1) % 256) == last_byte && !hit_edge) {
                 /* This is OK, the guest stopped at the point of
@@ -356,9 +358,9 @@ static void cleanup(const char *filename)
 static void test_migrate(void)
 {
     char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
-    QTestState *global = global_qtest, *from, *to;
+    QTestState *from, *to;
     unsigned char dest_byte_a, dest_byte_b, dest_byte_c, dest_byte_d;
-    gchar *cmd, *cmd_src, *cmd_dst;
+    gchar *cmd_src, *cmd_dst;
     QDict *rsp;

     char *bootpath = g_strdup_printf("%s/bootsect", tmpfs);
@@ -401,24 +403,24 @@ static void test_migrate(void)

     g_free(bootpath);

-    from = qtest_start(cmd_src);
+    from = qtest_init(cmd_src);
     g_free(cmd_src);

     to = qtest_init(cmd_dst);
     g_free(cmd_dst);

-    global_qtest = from;
-    rsp = qmp("{ 'execute': 'migrate-set-capabilities',"
-                  "'arguments': { "
+    assert(!global_qtest);
+
+    rsp = qtest_qmp(from, "{ 'execute': 'migrate-set-capabilities',"
+                    "'arguments': { "
                       "'capabilities': [ {"
                           "'capability': 'postcopy-ram',"
                           "'state': true } ] } }");
     g_assert(qdict_haskey(rsp, "return"));
     QDECREF(rsp);

-    global_qtest = to;
-    rsp = qmp("{ 'execute': 'migrate-set-capabilities',"
-                  "'arguments': { "
+    rsp = qtest_qmp(to, "{ 'execute': 'migrate-set-capabilities',"
+                    "'arguments': { "
                       "'capabilities': [ {"
                           "'capability': 'postcopy-ram',"
                           "'state': true } ] } }");
@@ -429,15 +431,14 @@ static void test_migrate(void)
      * quickly, but that it doesn't complete precopy even on a slow
      * machine, so also set the downtime.
      */
-    global_qtest = from;
-    rsp = qmp("{ 'execute': 'migrate_set_speed',"
-              "'arguments': { 'value': 100000000 } }");
+    rsp = qtest_qmp(from, "{ 'execute': 'migrate_set_speed',"
+                    "'arguments': { 'value': 100000000 } }");
     g_assert(qdict_haskey(rsp, "return"));
     QDECREF(rsp);

     /* 1ms downtime - it should never finish precopy */
-    rsp = qmp("{ 'execute': 'migrate_set_downtime',"
-              "'arguments': { 'value': 0.001 } }");
+    rsp = qtest_qmp(from, "{ 'execute': 'migrate_set_downtime',"
+                    "'arguments': { 'value': 0.001 } }");
     g_assert(qdict_haskey(rsp, "return"));
     QDECREF(rsp);

@@ -445,35 +446,30 @@ static void test_migrate(void)
     /* Wait for the first serial output from the source */
     wait_for_serial("src_serial");

-    cmd = g_strdup_printf("{ 'execute': 'migrate',"
-                          "'arguments': { 'uri': '%s' } }",
-                          uri);
-    rsp = qmp(cmd);
-    g_free(cmd);
+    rsp = qtest_qmp(from,
+                    "{'execute': 'migrate', 'arguments': { 'uri': %s}}", uri);
     g_assert(qdict_haskey(rsp, "return"));
     QDECREF(rsp);

-    wait_for_migration_pass();
+    wait_for_migration_pass(from);

-    rsp = return_or_event(qmp("{ 'execute': 'migrate-start-postcopy' }"));
+    rsp = return_or_event(from,
+                          qtest_qmp(from,
+                                    "{ 'execute': 'migrate-start-postcopy' }"));
     g_assert(qdict_haskey(rsp, "return"));
     QDECREF(rsp);

     if (!got_stop) {
-        qmp_eventwait("STOP");
+        qtest_qmp_eventwait(from, "STOP");
     }

-    global_qtest = to;
-    qmp_eventwait("RESUME");
+    qtest_qmp_eventwait(to, "RESUME");

     wait_for_serial("dest_serial");
-    global_qtest = from;
-    wait_for_migration_complete();
+    wait_for_migration_complete(from);

     qtest_quit(from);

-    global_qtest = to;
-
     qtest_memread(to, start_address, &dest_byte_a, 1);

     /* Destination still running, wait for a byte to change */
@@ -482,19 +478,19 @@ static void test_migrate(void)
         usleep(10 * 1000);
     } while (dest_byte_a == dest_byte_b);

-    qmp_discard_response("{ 'execute' : 'stop'}");
+    qtest_qmp_discard_response(to, "{ 'execute' : 'stop'}");
     /* With it stopped, check nothing changes */
     qtest_memread(to, start_address, &dest_byte_c, 1);
     sleep(1);
     qtest_memread(to, start_address, &dest_byte_d, 1);
     g_assert_cmpint(dest_byte_c, ==, dest_byte_d);

-    check_guests_ram();
+    check_guests_ram(to);

     qtest_quit(to);
     g_free(uri);

-    global_qtest = global;
+    assert(!global_qtest);

     cleanup("bootsect");
     cleanup("migsocket");
-- 
2.13.5

^ permalink raw reply related	[flat|nested] 75+ messages in thread

* [Qemu-devel] [PATCH v7 21/38] vhost-user-test: Drop dependence on global_qtest
  2017-09-11 17:19 [Qemu-devel] [PATCH v7 00/38] Preliminary libqtest cleanups Eric Blake
                   ` (19 preceding siblings ...)
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 20/38] postcopy-test: " Eric Blake
@ 2017-09-11 17:20 ` Eric Blake
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 22/38] qmp-test: " Eric Blake
                   ` (16 subsequent siblings)
  37 siblings, 0 replies; 75+ messages in thread
From: Eric Blake @ 2017-09-11 17:20 UTC (permalink / raw)
  To: qemu-devel; +Cc: armbru, pbonzini, thuth

Managing parallel connections to two different monitors via
the implicit global_qtest makes it hard to copy-and-paste code
to tests that are not aware of the implicit state.  Now that
libqos is fully-explicit, it is easy to expand the rest of
vhost-user-test to also use explicit state everywhere.

We can assert that the conversion is correct by checking that
global_qtest remains NULL throughout the test (a later patch
that changes global_qtest to not be a public global variable
will drop the assertions).

Signed-off-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Thomas Huth <thuth@redhat.com>
---
 tests/vhost-user-test.c | 68 ++++++++++++++++++++++---------------------------
 1 file changed, 30 insertions(+), 38 deletions(-)

diff --git a/tests/vhost-user-test.c b/tests/vhost-user-test.c
index b041d2c6a2..d5afc5f9f6 100644
--- a/tests/vhost-user-test.c
+++ b/tests/vhost-user-test.c
@@ -141,6 +141,7 @@ enum {
 };

 typedef struct TestServer {
+    QTestState *qts;
     QPCIBus *bus;
     gchar *socket_path;
     gchar *mig_path;
@@ -166,7 +167,7 @@ static void init_virtio_dev(TestServer *s)
     QVirtioPCIDevice *dev;
     uint32_t features;

-    s->bus = qpci_init_pc(global_qtest, NULL);
+    s->bus = qpci_init_pc(s->qts, NULL);
     g_assert_nonnull(s->bus);

     dev = qvirtio_pci_device_find(s->bus, VIRTIO_ID_NET);
@@ -238,8 +239,11 @@ static void read_guest_mem(const void *data)
         guest_mem += (s->memory.regions[i].mmap_offset / sizeof(*guest_mem));

         for (j = 0; j < 256; j++) {
-            uint32_t a = readl(s->memory.regions[i].guest_phys_addr + j*4);
-            uint32_t b = guest_mem[j];
+            uint32_t a, b;
+
+            a = qtest_readl(s->qts,
+                            s->memory.regions[i].guest_phys_addr + j * 4);
+            b = guest_mem[j];

             g_assert_cmpint(a, ==, b);
         }
@@ -512,6 +516,12 @@ static gboolean _test_server_free(TestServer *server)

 static void test_server_free(TestServer *server)
 {
+    assert(!global_qtest);
+    if (server->qts) {
+        qtest_quit(server->qts);
+        server->qts = NULL;
+    }
+
     g_idle_add((GSourceFunc)_test_server_free, server);
 }

@@ -623,7 +633,6 @@ static void test_migrate(void)
     TestServer *s = test_server_new("src");
     TestServer *dest = test_server_new("dest");
     char *uri = g_strdup_printf("%s%s", "unix:", dest->mig_path);
-    QTestState *global = global_qtest, *from, *to;
     GSource *source;
     gchar *cmd;
     QDict *rsp;
@@ -634,7 +643,7 @@ static void test_migrate(void)
     test_server_listen(dest);

     cmd = GET_QEMU_CMDE(s, 2, "", "");
-    from = qtest_start(cmd);
+    s->qts = qtest_init(cmd);
     g_free(cmd);

     init_virtio_dev(s);
@@ -643,7 +652,7 @@ static void test_migrate(void)
     g_assert_cmpint(size, ==, (2 * 1024 * 1024) / (VHOST_LOG_PAGE * 8));

     cmd = GET_QEMU_CMDE(dest, 2, "", " -incoming %s", uri);
-    to = qtest_init(cmd);
+    dest->qts = qtest_init(cmd);
     g_free(cmd);

     source = g_source_new(&test_migrate_source_funcs,
@@ -654,16 +663,13 @@ static void test_migrate(void)

     /* slow down migration to have time to fiddle with log */
     /* TODO: qtest could learn to break on some places */
-    rsp = qmp("{ 'execute': 'migrate_set_speed',"
-              "'arguments': { 'value': 10 } }");
+    rsp = qtest_qmp(s->qts, "{ 'execute': 'migrate_set_speed',"
+                    "'arguments': { 'value': 10 } }");
     g_assert(qdict_haskey(rsp, "return"));
     QDECREF(rsp);

-    cmd = g_strdup_printf("{ 'execute': 'migrate',"
-                          "'arguments': { 'uri': '%s' } }",
-                          uri);
-    rsp = qmp(cmd);
-    g_free(cmd);
+    rsp = qtest_qmp(s->qts,
+                    "{'execute': 'migrate', 'arguments': { 'uri': %s}}", uri);
     g_assert(qdict_haskey(rsp, "return"));
     QDECREF(rsp);

@@ -678,28 +684,23 @@ static void test_migrate(void)
     munmap(log, size);

     /* speed things up */
-    rsp = qmp("{ 'execute': 'migrate_set_speed',"
-              "'arguments': { 'value': 0 } }");
+    rsp = qtest_qmp(s->qts, "{ 'execute': 'migrate_set_speed',"
+                    "'arguments': { 'value': 0 } }");
     g_assert(qdict_haskey(rsp, "return"));
     QDECREF(rsp);

-    qmp_eventwait("STOP");
+    qtest_qmp_eventwait(s->qts, "STOP");

-    global_qtest = to;
-    qmp_eventwait("RESUME");
+    qtest_qmp_eventwait(dest->qts, "RESUME");

     read_guest_mem(dest);

     g_source_destroy(source);
     g_source_unref(source);

-    qtest_quit(to);
     test_server_free(dest);
-    qtest_quit(from);
     test_server_free(s);
     g_free(uri);
-
-    global_qtest = global;
 }

 static void wait_for_rings_started(TestServer *s, size_t count)
@@ -754,7 +755,7 @@ static void test_reconnect_subprocess(void)

     g_thread_new("connect", connect_thread, s);
     cmd = GET_QEMU_CMDE(s, 2, ",server", "");
-    qtest_start(cmd);
+    s->qts = qtest_init(cmd);
     g_free(cmd);

     init_virtio_dev(s);
@@ -768,7 +769,6 @@ static void test_reconnect_subprocess(void)
     wait_for_fds(s);
     wait_for_rings_started(s, 2);

-    qtest_end();
     test_server_free(s);
     return;
 }
@@ -790,14 +790,13 @@ static void test_connect_fail_subprocess(void)
     s->test_fail = true;
     g_thread_new("connect", connect_thread, s);
     cmd = GET_QEMU_CMDE(s, 2, ",server", "");
-    qtest_start(cmd);
+    s->qts = qtest_init(cmd);
     g_free(cmd);

     init_virtio_dev(s);
     wait_for_fds(s);
     wait_for_rings_started(s, 2);

-    qtest_end();
     test_server_free(s);
 }

@@ -818,14 +817,13 @@ static void test_flags_mismatch_subprocess(void)
     s->test_flags = TEST_FLAGS_DISCONNECT;
     g_thread_new("connect", connect_thread, s);
     cmd = GET_QEMU_CMDE(s, 2, ",server", "");
-    qtest_start(cmd);
+    s->qts = qtest_init(cmd);
     g_free(cmd);

     init_virtio_dev(s);
     wait_for_fds(s);
     wait_for_rings_started(s, 2);

-    qtest_end();
     test_server_free(s);
 }

@@ -890,13 +888,13 @@ static void test_multiqueue(void)
                           512, 512, root, s->chr_name,
                           s->socket_path, "", s->chr_name,
                           queues, queues * 2 + 2);
-    qtest_start(cmd);
+    s->qts = qtest_init(cmd);
     g_free(cmd);

-    bus = qpci_init_pc(global_qtest, NULL);
+    bus = qpci_init_pc(s->qts, NULL);
     dev = virtio_net_pci_init(bus, PCI_SLOT);

-    alloc = pc_alloc_init(global_qtest);
+    alloc = pc_alloc_init(s->qts);
     for (i = 0; i < queues * 2; i++) {
         vq[i] = (QVirtQueuePCI *)qvirtqueue_setup(&dev->vdev, alloc, i);
     }
@@ -913,14 +911,12 @@ static void test_multiqueue(void)
     g_free(dev->pdev);
     g_free(dev);
     qpci_free_pc(bus);
-    qtest_end();

     test_server_free(s);
 }

 int main(int argc, char **argv)
 {
-    QTestState *s = NULL;
     TestServer *server = NULL;
     const char *hugefs;
     char *qemu_cmd = NULL;
@@ -957,7 +953,7 @@ int main(int argc, char **argv)

     qemu_cmd = GET_QEMU_CMD(server);

-    s = qtest_start(qemu_cmd);
+    server->qts = qtest_init(qemu_cmd);
     g_free(qemu_cmd);
     init_virtio_dev(server);

@@ -979,10 +975,6 @@ int main(int argc, char **argv)

     ret = g_test_run();

-    if (s) {
-        qtest_quit(s);
-    }
-
     /* cleanup */
     test_server_free(server);

-- 
2.13.5

^ permalink raw reply related	[flat|nested] 75+ messages in thread

* [Qemu-devel] [PATCH v7 22/38] qmp-test: Drop dependence on global_qtest
  2017-09-11 17:19 [Qemu-devel] [PATCH v7 00/38] Preliminary libqtest cleanups Eric Blake
                   ` (20 preceding siblings ...)
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 21/38] vhost-user-test: " Eric Blake
@ 2017-09-11 17:20 ` Eric Blake
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 23/38] tests/boot-sector: " Eric Blake
                   ` (15 subsequent siblings)
  37 siblings, 0 replies; 75+ messages in thread
From: Eric Blake @ 2017-09-11 17:20 UTC (permalink / raw)
  To: qemu-devel; +Cc: armbru, pbonzini, thuth

As a general rule, we prefer avoiding implicit global state
because it makes code harder to safely copy and paste without
thinking about the global state.  Although qmp-test does not
maintain parallel qtest connections, it was the last test
assigning to global_qtest.  It's just as easy to be explicit
about the state; once all tests have been cleaned up, a later
patch can then get rid of global_qtest and a layer of wrappers
in libqtest.

Signed-off-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Thomas Huth <thuth@redhat.com>
---
 tests/qmp-test.c | 35 ++++++++++++++++++-----------------
 1 file changed, 18 insertions(+), 17 deletions(-)

diff --git a/tests/qmp-test.c b/tests/qmp-test.c
index c5a5c10b41..4e6198c9fa 100644
--- a/tests/qmp-test.c
+++ b/tests/qmp-test.c
@@ -41,32 +41,32 @@ static void test_version(QObject *version)
     visit_free(v);
 }

-static void test_malformed(void)
+static void test_malformed(QTestState *qts)
 {
     QDict *resp;

     /* Not even a dictionary */
-    resp = qmp("null");
+    resp = qtest_qmp(qts, "null");
     g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
     QDECREF(resp);

     /* No "execute" key */
-    resp = qmp("{}");
+    resp = qtest_qmp(qts, "{}");
     g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
     QDECREF(resp);

     /* "execute" isn't a string */
-    resp = qmp("{ 'execute': true }");
+    resp = qtest_qmp(qts, "{ 'execute': true }");
     g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
     QDECREF(resp);

     /* "arguments" isn't a dictionary */
-    resp = qmp("{ 'execute': 'no-such-cmd', 'arguments': [] }");
+    resp = qtest_qmp(qts, "{ 'execute': 'no-such-cmd', 'arguments': [] }");
     g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
     QDECREF(resp);

     /* extra key */
-    resp = qmp("{ 'execute': 'no-such-cmd', 'extra': true }");
+    resp = qtest_qmp(qts, "{ 'execute': 'no-such-cmd', 'extra': true }");
     g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
     QDECREF(resp);
 }
@@ -75,11 +75,12 @@ static void test_qmp_protocol(void)
 {
     QDict *resp, *q, *ret;
     QList *capabilities;
+    QTestState *qts;

-    global_qtest = qtest_init_without_qmp_handshake(common_args);
+    qts = qtest_init_without_qmp_handshake(common_args);

     /* Test greeting */
-    resp = qmp_receive();
+    resp = qtest_qmp_receive(qts);
     q = qdict_get_qdict(resp, "QMP");
     g_assert(q);
     test_version(qdict_get(q, "version"));
@@ -88,46 +89,46 @@ static void test_qmp_protocol(void)
     QDECREF(resp);

     /* Test valid command before handshake */
-    resp = qmp("{ 'execute': 'query-version' }");
+    resp = qtest_qmp(qts, "{ 'execute': 'query-version' }");
     g_assert_cmpstr(get_error_class(resp), ==, "CommandNotFound");
     QDECREF(resp);

     /* Test malformed commands before handshake */
-    test_malformed();
+    test_malformed(qts);

     /* Test handshake */
-    resp = qmp("{ 'execute': 'qmp_capabilities' }");
+    resp = qtest_qmp(qts, "{ 'execute': 'qmp_capabilities' }");
     ret = qdict_get_qdict(resp, "return");
     g_assert(ret && !qdict_size(ret));
     QDECREF(resp);

     /* Test repeated handshake */
-    resp = qmp("{ 'execute': 'qmp_capabilities' }");
+    resp = qtest_qmp(qts, "{ 'execute': 'qmp_capabilities' }");
     g_assert_cmpstr(get_error_class(resp), ==, "CommandNotFound");
     QDECREF(resp);

     /* Test valid command */
-    resp = qmp("{ 'execute': 'query-version' }");
+    resp = qtest_qmp(qts, "{ 'execute': 'query-version' }");
     test_version(qdict_get(resp, "return"));
     QDECREF(resp);

     /* Test malformed commands */
-    test_malformed();
+    test_malformed(qts);

     /* Test 'id' */
-    resp = qmp("{ 'execute': 'query-name', 'id': 'cookie#1' }");
+    resp = qtest_qmp(qts, "{ 'execute': 'query-name', 'id': 'cookie#1' }");
     ret = qdict_get_qdict(resp, "return");
     g_assert(ret);
     g_assert_cmpstr(qdict_get_try_str(resp, "id"), ==, "cookie#1");
     QDECREF(resp);

     /* Test command failure with 'id' */
-    resp = qmp("{ 'execute': 'human-monitor-command', 'id': 2 }");
+    resp = qtest_qmp(qts, "{ 'execute': 'human-monitor-command', 'id': 2 }");
     g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
     g_assert_cmpint(qdict_get_int(resp, "id"), ==, 2);
     QDECREF(resp);

-    qtest_end();
+    qtest_quit(qts);
 }

 static int query_error_class(const char *cmd)
-- 
2.13.5

^ permalink raw reply related	[flat|nested] 75+ messages in thread

* [Qemu-devel] [PATCH v7 23/38] tests/boot-sector: Drop dependence on global_qtest
  2017-09-11 17:19 [Qemu-devel] [PATCH v7 00/38] Preliminary libqtest cleanups Eric Blake
                   ` (21 preceding siblings ...)
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 22/38] qmp-test: " Eric Blake
@ 2017-09-11 17:20 ` Eric Blake
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 24/38] tests/acpi-utils: " Eric Blake
                   ` (14 subsequent siblings)
  37 siblings, 0 replies; 75+ messages in thread
From: Eric Blake @ 2017-09-11 17:20 UTC (permalink / raw)
  To: qemu-devel
  Cc: armbru, pbonzini, thuth, Michael S. Tsirkin, Igor Mammedov, Ben Warren

As a general rule, we prefer avoiding implicit global state
because it makes code harder to safely copy and paste without
thinking about the global state.  Adjust the helper code to
use explicit state instead, and update all callers.

Fix some trailing whitespace while touching the file.

Signed-off-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Thomas Huth <thuth@redhat.com>
Acked-by: Michael S. Tsirkin <mst@redhat.com>
---
 tests/boot-sector.h      | 6 ++++--
 tests/bios-tables-test.c | 2 +-
 tests/boot-sector.c      | 8 ++++----
 tests/pxe-test.c         | 2 +-
 tests/vmgenid-test.c     | 2 +-
 5 files changed, 11 insertions(+), 9 deletions(-)

diff --git a/tests/boot-sector.h b/tests/boot-sector.h
index 35d61c7e2b..6ee6bb4d97 100644
--- a/tests/boot-sector.h
+++ b/tests/boot-sector.h
@@ -5,7 +5,7 @@
  *
  * Authors:
  *  Michael S. Tsirkin <mst@redhat.com>
- *  Victor Kaplansky <victork@redhat.com>    
+ *  Victor Kaplansky <victork@redhat.com>
  *
  * This work is licensed under the terms of the GNU GPL, version 2 or later.
  * See the COPYING file in the top-level directory.
@@ -14,11 +14,13 @@
 #ifndef TEST_BOOT_SECTOR_H
 #define TEST_BOOT_SECTOR_H

+#include "libqtest.h"
+
 /* Create boot disk file. fname must be a suitable string for mkstemp() */
 int boot_sector_init(char *fname);

 /* Loop until signature in memory is OK.  */
-void boot_sector_test(void);
+void boot_sector_test(QTestState *qts);

 /* unlink boot disk file.  */
 void boot_sector_cleanup(const char *fname);
diff --git a/tests/bios-tables-test.c b/tests/bios-tables-test.c
index 564da45f65..bde62bf1cf 100644
--- a/tests/bios-tables-test.c
+++ b/tests/bios-tables-test.c
@@ -632,7 +632,7 @@ static void test_acpi_one(const char *params, test_data *data)

     qtest_start(args);

-    boot_sector_test();
+    boot_sector_test(global_qtest);

     test_acpi_rsdp_address(data);
     test_acpi_rsdp_table(data);
diff --git a/tests/boot-sector.c b/tests/boot-sector.c
index 9ee85370b0..8c8ac7f124 100644
--- a/tests/boot-sector.c
+++ b/tests/boot-sector.c
@@ -5,7 +5,7 @@
  *
  * Authors:
  *  Michael S. Tsirkin <mst@redhat.com>
- *  Victor Kaplansky <victork@redhat.com>    
+ *  Victor Kaplansky <victork@redhat.com>
  *
  * This work is licensed under the terms of the GNU GPL, version 2 or later.
  * See the COPYING file in the top-level directory.
@@ -130,7 +130,7 @@ int boot_sector_init(char *fname)
 }

 /* Loop until signature in memory is OK.  */
-void boot_sector_test(void)
+void boot_sector_test(QTestState *qts)
 {
     uint8_t signature_low;
     uint8_t signature_high;
@@ -146,8 +146,8 @@ void boot_sector_test(void)
      * instruction.
      */
     for (i = 0; i < TEST_CYCLES; ++i) {
-        signature_low = readb(SIGNATURE_ADDR);
-        signature_high = readb(SIGNATURE_ADDR + 1);
+        signature_low = qtest_readb(qts, SIGNATURE_ADDR);
+        signature_high = qtest_readb(qts, SIGNATURE_ADDR + 1);
         signature = (signature_high << 8) | signature_low;
         if (signature == SIGNATURE) {
             break;
diff --git a/tests/pxe-test.c b/tests/pxe-test.c
index 0d70afccd6..e2d3853534 100644
--- a/tests/pxe-test.c
+++ b/tests/pxe-test.c
@@ -31,7 +31,7 @@ static void test_pxe_one(const char *params, bool ipv6)
                            ipv6 ? "on" : "off", params);

     qtest_start(args);
-    boot_sector_test();
+    boot_sector_test(global_qtest);
     qtest_quit(global_qtest);
     g_free(args);
 }
diff --git a/tests/vmgenid-test.c b/tests/vmgenid-test.c
index 918c82c82a..0ea98b86d7 100644
--- a/tests/vmgenid-test.c
+++ b/tests/vmgenid-test.c
@@ -47,7 +47,7 @@ static uint32_t acpi_find_vgia(void)
     int i;

     /* Wait for guest firmware to finish and start the payload. */
-    boot_sector_test();
+    boot_sector_test(global_qtest);

     /* Tables should be initialized now. */
     rsdp_offset = acpi_find_rsdp_address();
-- 
2.13.5

^ permalink raw reply related	[flat|nested] 75+ messages in thread

* [Qemu-devel] [PATCH v7 24/38] tests/acpi-utils: Drop dependence on global_qtest
  2017-09-11 17:19 [Qemu-devel] [PATCH v7 00/38] Preliminary libqtest cleanups Eric Blake
                   ` (22 preceding siblings ...)
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 23/38] tests/boot-sector: " Eric Blake
@ 2017-09-11 17:20 ` Eric Blake
  2017-09-12  9:26   ` Thomas Huth
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 25/38] wdt_ib700-test: " Eric Blake
                   ` (13 subsequent siblings)
  37 siblings, 1 reply; 75+ messages in thread
From: Eric Blake @ 2017-09-11 17:20 UTC (permalink / raw)
  To: qemu-devel
  Cc: armbru, pbonzini, thuth, Michael S. Tsirkin, Igor Mammedov, Ben Warren

As a general rule, we prefer avoiding implicit global state
because it makes code harder to safely copy and paste without
thinking about the global state.  Adjust the helper code to
use explicit state instead, and update all callers.

bios-tables-test no longer depends on global_qtest, now that it
passes explicit state through the testsuite data; an assert
proves this fact (although we will get rid of it later, once
global_qtest is gone).

Signed-off-by: Eric Blake <eblake@redhat.com>

---
v7: merge two patches for less churn on bios-tables-test
---
 tests/acpi-utils.h       |  65 ++++++++---------
 tests/acpi-utils.c       |  19 ++---
 tests/bios-tables-test.c | 184 ++++++++++++++++++++++++-----------------------
 tests/vmgenid-test.c     |  18 ++---
 4 files changed, 146 insertions(+), 140 deletions(-)

diff --git a/tests/acpi-utils.h b/tests/acpi-utils.h
index f8d87236c6..8ec83f71b1 100644
--- a/tests/acpi-utils.h
+++ b/tests/acpi-utils.h
@@ -28,20 +28,20 @@ typedef struct {
     bool tmp_files_retain;   /* do not delete the temp asl/aml */
 } AcpiSdtTable;

-#define ACPI_READ_FIELD(field, addr)           \
+#define ACPI_READ_FIELD(qts, field, addr)      \
     do {                                       \
         switch (sizeof(field)) {               \
         case 1:                                \
-            field = readb(addr);               \
+            field = qtest_readb(qts, addr);    \
             break;                             \
         case 2:                                \
-            field = readw(addr);               \
+            field = qtest_readw(qts, addr);    \
             break;                             \
         case 4:                                \
-            field = readl(addr);               \
+            field = qtest_readl(qts, addr);    \
             break;                             \
         case 8:                                \
-            field = readq(addr);               \
+            field = qtest_readq(qts, addr);    \
             break;                             \
         default:                               \
             g_assert(false);                   \
@@ -49,28 +49,28 @@ typedef struct {
         addr += sizeof(field);                  \
     } while (0);

-#define ACPI_READ_ARRAY_PTR(arr, length, addr)  \
-    do {                                        \
-        int idx;                                \
-        for (idx = 0; idx < length; ++idx) {    \
-            ACPI_READ_FIELD(arr[idx], addr);    \
-        }                                       \
+#define ACPI_READ_ARRAY_PTR(qts, arr, length, addr)      \
+    do {                                                 \
+        int idx;                                         \
+        for (idx = 0; idx < length; ++idx) {             \
+            ACPI_READ_FIELD(qts, arr[idx], addr);        \
+        }                                                \
     } while (0);

-#define ACPI_READ_ARRAY(arr, addr)                               \
-    ACPI_READ_ARRAY_PTR(arr, sizeof(arr) / sizeof(arr[0]), addr)
+#define ACPI_READ_ARRAY(qts, arr, addr)                                 \
+    ACPI_READ_ARRAY_PTR(qts, arr, sizeof(arr) / sizeof(arr[0]), addr)

-#define ACPI_READ_TABLE_HEADER(table, addr)                      \
+#define ACPI_READ_TABLE_HEADER(qts, table, addr)                 \
     do {                                                         \
-        ACPI_READ_FIELD((table)->signature, addr);               \
-        ACPI_READ_FIELD((table)->length, addr);                  \
-        ACPI_READ_FIELD((table)->revision, addr);                \
-        ACPI_READ_FIELD((table)->checksum, addr);                \
-        ACPI_READ_ARRAY((table)->oem_id, addr);                  \
-        ACPI_READ_ARRAY((table)->oem_table_id, addr);            \
-        ACPI_READ_FIELD((table)->oem_revision, addr);            \
-        ACPI_READ_ARRAY((table)->asl_compiler_id, addr);         \
-        ACPI_READ_FIELD((table)->asl_compiler_revision, addr);   \
+        ACPI_READ_FIELD(qts, (table)->signature, addr);          \
+        ACPI_READ_FIELD(qts, (table)->length, addr);             \
+        ACPI_READ_FIELD(qts, (table)->revision, addr);           \
+        ACPI_READ_FIELD(qts, (table)->checksum, addr);           \
+        ACPI_READ_ARRAY(qts, (table)->oem_id, addr);             \
+        ACPI_READ_ARRAY(qts, (table)->oem_table_id, addr);       \
+        ACPI_READ_FIELD(qts, (table)->oem_revision, addr);       \
+        ACPI_READ_ARRAY(qts, (table)->asl_compiler_id, addr);    \
+        ACPI_READ_FIELD(qts, (table)->asl_compiler_revision, addr);     \
     } while (0);

 #define ACPI_ASSERT_CMP(actual, expected) do { \
@@ -87,18 +87,19 @@ typedef struct {
     g_assert_cmpstr(ACPI_ASSERT_CMP_str, ==, expected); \
 } while (0)

-#define ACPI_READ_GENERIC_ADDRESS(field, addr)       \
-    do {                                             \
-        ACPI_READ_FIELD((field).space_id, addr);     \
-        ACPI_READ_FIELD((field).bit_width, addr);    \
-        ACPI_READ_FIELD((field).bit_offset, addr);   \
-        ACPI_READ_FIELD((field).access_width, addr); \
-        ACPI_READ_FIELD((field).address, addr);      \
+#define ACPI_READ_GENERIC_ADDRESS(qts, field, addr)       \
+    do {                                                  \
+        ACPI_READ_FIELD(qts, (field).space_id, addr);     \
+        ACPI_READ_FIELD(qts, (field).bit_width, addr);    \
+        ACPI_READ_FIELD(qts, (field).bit_offset, addr);   \
+        ACPI_READ_FIELD(qts, (field).access_width, addr); \
+        ACPI_READ_FIELD(qts, (field).address, addr);      \
     } while (0);


 uint8_t acpi_calc_checksum(const uint8_t *data, int len);
-uint32_t acpi_find_rsdp_address(void);
-void acpi_parse_rsdp_table(uint32_t addr, AcpiRsdpDescriptor *rsdp_table);
+uint32_t acpi_find_rsdp_address(QTestState *qts);
+void acpi_parse_rsdp_table(QTestState *qts, uint32_t addr,
+                           AcpiRsdpDescriptor *rsdp_table);

 #endif  /* TEST_ACPI_UTILS_H */
diff --git a/tests/acpi-utils.c b/tests/acpi-utils.c
index 41dc1ea9b4..6dc8ca1a8c 100644
--- a/tests/acpi-utils.c
+++ b/tests/acpi-utils.c
@@ -32,7 +32,7 @@ uint8_t acpi_calc_checksum(const uint8_t *data, int len)
     return sum;
 }

-uint32_t acpi_find_rsdp_address(void)
+uint32_t acpi_find_rsdp_address(QTestState *qts)
 {
     uint32_t off;

@@ -42,7 +42,7 @@ uint32_t acpi_find_rsdp_address(void)
         int i;

         for (i = 0; i < sizeof sig - 1; ++i) {
-            sig[i] = readb(off + i);
+            sig[i] = qtest_readb(qts, off + i);
         }

         if (!memcmp(sig, "RSD PTR ", sizeof sig)) {
@@ -52,14 +52,15 @@ uint32_t acpi_find_rsdp_address(void)
     return off;
 }

-void acpi_parse_rsdp_table(uint32_t addr, AcpiRsdpDescriptor *rsdp_table)
+void acpi_parse_rsdp_table(QTestState *qts, uint32_t addr,
+                           AcpiRsdpDescriptor *rsdp_table)
 {
-    ACPI_READ_FIELD(rsdp_table->signature, addr);
+    ACPI_READ_FIELD(qts, rsdp_table->signature, addr);
     ACPI_ASSERT_CMP64(rsdp_table->signature, "RSD PTR ");

-    ACPI_READ_FIELD(rsdp_table->checksum, addr);
-    ACPI_READ_ARRAY(rsdp_table->oem_id, addr);
-    ACPI_READ_FIELD(rsdp_table->revision, addr);
-    ACPI_READ_FIELD(rsdp_table->rsdt_physical_address, addr);
-    ACPI_READ_FIELD(rsdp_table->length, addr);
+    ACPI_READ_FIELD(qts, rsdp_table->checksum, addr);
+    ACPI_READ_ARRAY(qts, rsdp_table->oem_id, addr);
+    ACPI_READ_FIELD(qts, rsdp_table->revision, addr);
+    ACPI_READ_FIELD(qts, rsdp_table->rsdt_physical_address, addr);
+    ACPI_READ_FIELD(qts, rsdp_table->length, addr);
 }
diff --git a/tests/bios-tables-test.c b/tests/bios-tables-test.c
index bde62bf1cf..4272ea1496 100644
--- a/tests/bios-tables-test.c
+++ b/tests/bios-tables-test.c
@@ -38,6 +38,7 @@ typedef struct {
     struct smbios_21_entry_point smbios_ep_table;
     uint8_t *required_struct_types;
     int required_struct_types_len;
+    QTestState *qts;
 } test_data;

 static char disk[] = "tests/acpi-test-disk-XXXXXX";
@@ -77,7 +78,7 @@ static void free_test_data(test_data *data)

 static void test_acpi_rsdp_address(test_data *data)
 {
-    uint32_t off = acpi_find_rsdp_address();
+    uint32_t off = acpi_find_rsdp_address(data->qts);
     g_assert_cmphex(off, <, 0x100000);
     data->rsdp_addr = off;
 }
@@ -87,7 +88,7 @@ static void test_acpi_rsdp_table(test_data *data)
     AcpiRsdpDescriptor *rsdp_table = &data->rsdp_table;
     uint32_t addr = data->rsdp_addr;

-    acpi_parse_rsdp_table(addr, rsdp_table);
+    acpi_parse_rsdp_table(data->qts, addr, rsdp_table);

     /* rsdp checksum is not for the whole table, but for the first 20 bytes */
     g_assert(!acpi_calc_checksum((uint8_t *)rsdp_table, 20));
@@ -102,7 +103,7 @@ static void test_acpi_rsdt_table(test_data *data)
     uint8_t checksum;

     /* read the header */
-    ACPI_READ_TABLE_HEADER(rsdt_table, addr);
+    ACPI_READ_TABLE_HEADER(data->qts, rsdt_table, addr);
     ACPI_ASSERT_CMP(rsdt_table->signature, "RSDT");

     /* compute the table entries in rsdt */
@@ -112,7 +113,7 @@ static void test_acpi_rsdt_table(test_data *data)

     /* get the addresses of the tables pointed by rsdt */
     tables = g_new0(uint32_t, tables_nr);
-    ACPI_READ_ARRAY_PTR(tables, tables_nr, addr);
+    ACPI_READ_ARRAY_PTR(data->qts, tables, tables_nr, addr);

     checksum = acpi_calc_checksum((uint8_t *)rsdt_table, rsdt_table->length) +
                acpi_calc_checksum((uint8_t *)tables,
@@ -128,63 +129,64 @@ static void test_acpi_fadt_table(test_data *data)
 {
     AcpiFadtDescriptorRev3 *fadt_table = &data->fadt_table;
     uint32_t addr;
+    QTestState *qts = data->qts;

     /* FADT table comes first */
     addr = data->rsdt_tables_addr[0];
-    ACPI_READ_TABLE_HEADER(fadt_table, addr);
+    ACPI_READ_TABLE_HEADER(qts, fadt_table, addr);

-    ACPI_READ_FIELD(fadt_table->firmware_ctrl, addr);
-    ACPI_READ_FIELD(fadt_table->dsdt, addr);
-    ACPI_READ_FIELD(fadt_table->model, addr);
-    ACPI_READ_FIELD(fadt_table->reserved1, addr);
-    ACPI_READ_FIELD(fadt_table->sci_int, addr);
-    ACPI_READ_FIELD(fadt_table->smi_cmd, addr);
-    ACPI_READ_FIELD(fadt_table->acpi_enable, addr);
-    ACPI_READ_FIELD(fadt_table->acpi_disable, addr);
-    ACPI_READ_FIELD(fadt_table->S4bios_req, addr);
-    ACPI_READ_FIELD(fadt_table->reserved2, addr);
-    ACPI_READ_FIELD(fadt_table->pm1a_evt_blk, addr);
-    ACPI_READ_FIELD(fadt_table->pm1b_evt_blk, addr);
-    ACPI_READ_FIELD(fadt_table->pm1a_cnt_blk, addr);
-    ACPI_READ_FIELD(fadt_table->pm1b_cnt_blk, addr);
-    ACPI_READ_FIELD(fadt_table->pm2_cnt_blk, addr);
-    ACPI_READ_FIELD(fadt_table->pm_tmr_blk, addr);
-    ACPI_READ_FIELD(fadt_table->gpe0_blk, addr);
-    ACPI_READ_FIELD(fadt_table->gpe1_blk, addr);
-    ACPI_READ_FIELD(fadt_table->pm1_evt_len, addr);
-    ACPI_READ_FIELD(fadt_table->pm1_cnt_len, addr);
-    ACPI_READ_FIELD(fadt_table->pm2_cnt_len, addr);
-    ACPI_READ_FIELD(fadt_table->pm_tmr_len, addr);
-    ACPI_READ_FIELD(fadt_table->gpe0_blk_len, addr);
-    ACPI_READ_FIELD(fadt_table->gpe1_blk_len, addr);
-    ACPI_READ_FIELD(fadt_table->gpe1_base, addr);
-    ACPI_READ_FIELD(fadt_table->reserved3, addr);
-    ACPI_READ_FIELD(fadt_table->plvl2_lat, addr);
-    ACPI_READ_FIELD(fadt_table->plvl3_lat, addr);
-    ACPI_READ_FIELD(fadt_table->flush_size, addr);
-    ACPI_READ_FIELD(fadt_table->flush_stride, addr);
-    ACPI_READ_FIELD(fadt_table->duty_offset, addr);
-    ACPI_READ_FIELD(fadt_table->duty_width, addr);
-    ACPI_READ_FIELD(fadt_table->day_alrm, addr);
-    ACPI_READ_FIELD(fadt_table->mon_alrm, addr);
-    ACPI_READ_FIELD(fadt_table->century, addr);
-    ACPI_READ_FIELD(fadt_table->boot_flags, addr);
-    ACPI_READ_FIELD(fadt_table->reserved, addr);
-    ACPI_READ_FIELD(fadt_table->flags, addr);
-    ACPI_READ_GENERIC_ADDRESS(fadt_table->reset_register, addr);
-    ACPI_READ_FIELD(fadt_table->reset_value, addr);
-    ACPI_READ_FIELD(fadt_table->arm_boot_flags, addr);
-    ACPI_READ_FIELD(fadt_table->minor_revision, addr);
-    ACPI_READ_FIELD(fadt_table->x_facs, addr);
-    ACPI_READ_FIELD(fadt_table->x_dsdt, addr);
-    ACPI_READ_GENERIC_ADDRESS(fadt_table->xpm1a_event_block, addr);
-    ACPI_READ_GENERIC_ADDRESS(fadt_table->xpm1b_event_block, addr);
-    ACPI_READ_GENERIC_ADDRESS(fadt_table->xpm1a_control_block, addr);
-    ACPI_READ_GENERIC_ADDRESS(fadt_table->xpm1b_control_block, addr);
-    ACPI_READ_GENERIC_ADDRESS(fadt_table->xpm2_control_block, addr);
-    ACPI_READ_GENERIC_ADDRESS(fadt_table->xpm_timer_block, addr);
-    ACPI_READ_GENERIC_ADDRESS(fadt_table->xgpe0_block, addr);
-    ACPI_READ_GENERIC_ADDRESS(fadt_table->xgpe1_block, addr);
+    ACPI_READ_FIELD(qts, fadt_table->firmware_ctrl, addr);
+    ACPI_READ_FIELD(qts, fadt_table->dsdt, addr);
+    ACPI_READ_FIELD(qts, fadt_table->model, addr);
+    ACPI_READ_FIELD(qts, fadt_table->reserved1, addr);
+    ACPI_READ_FIELD(qts, fadt_table->sci_int, addr);
+    ACPI_READ_FIELD(qts, fadt_table->smi_cmd, addr);
+    ACPI_READ_FIELD(qts, fadt_table->acpi_enable, addr);
+    ACPI_READ_FIELD(qts, fadt_table->acpi_disable, addr);
+    ACPI_READ_FIELD(qts, fadt_table->S4bios_req, addr);
+    ACPI_READ_FIELD(qts, fadt_table->reserved2, addr);
+    ACPI_READ_FIELD(qts, fadt_table->pm1a_evt_blk, addr);
+    ACPI_READ_FIELD(qts, fadt_table->pm1b_evt_blk, addr);
+    ACPI_READ_FIELD(qts, fadt_table->pm1a_cnt_blk, addr);
+    ACPI_READ_FIELD(qts, fadt_table->pm1b_cnt_blk, addr);
+    ACPI_READ_FIELD(qts, fadt_table->pm2_cnt_blk, addr);
+    ACPI_READ_FIELD(qts, fadt_table->pm_tmr_blk, addr);
+    ACPI_READ_FIELD(qts, fadt_table->gpe0_blk, addr);
+    ACPI_READ_FIELD(qts, fadt_table->gpe1_blk, addr);
+    ACPI_READ_FIELD(qts, fadt_table->pm1_evt_len, addr);
+    ACPI_READ_FIELD(qts, fadt_table->pm1_cnt_len, addr);
+    ACPI_READ_FIELD(qts, fadt_table->pm2_cnt_len, addr);
+    ACPI_READ_FIELD(qts, fadt_table->pm_tmr_len, addr);
+    ACPI_READ_FIELD(qts, fadt_table->gpe0_blk_len, addr);
+    ACPI_READ_FIELD(qts, fadt_table->gpe1_blk_len, addr);
+    ACPI_READ_FIELD(qts, fadt_table->gpe1_base, addr);
+    ACPI_READ_FIELD(qts, fadt_table->reserved3, addr);
+    ACPI_READ_FIELD(qts, fadt_table->plvl2_lat, addr);
+    ACPI_READ_FIELD(qts, fadt_table->plvl3_lat, addr);
+    ACPI_READ_FIELD(qts, fadt_table->flush_size, addr);
+    ACPI_READ_FIELD(qts, fadt_table->flush_stride, addr);
+    ACPI_READ_FIELD(qts, fadt_table->duty_offset, addr);
+    ACPI_READ_FIELD(qts, fadt_table->duty_width, addr);
+    ACPI_READ_FIELD(qts, fadt_table->day_alrm, addr);
+    ACPI_READ_FIELD(qts, fadt_table->mon_alrm, addr);
+    ACPI_READ_FIELD(qts, fadt_table->century, addr);
+    ACPI_READ_FIELD(qts, fadt_table->boot_flags, addr);
+    ACPI_READ_FIELD(qts, fadt_table->reserved, addr);
+    ACPI_READ_FIELD(qts, fadt_table->flags, addr);
+    ACPI_READ_GENERIC_ADDRESS(qts, fadt_table->reset_register, addr);
+    ACPI_READ_FIELD(qts, fadt_table->reset_value, addr);
+    ACPI_READ_FIELD(qts, fadt_table->arm_boot_flags, addr);
+    ACPI_READ_FIELD(qts, fadt_table->minor_revision, addr);
+    ACPI_READ_FIELD(qts, fadt_table->x_facs, addr);
+    ACPI_READ_FIELD(qts, fadt_table->x_dsdt, addr);
+    ACPI_READ_GENERIC_ADDRESS(qts, fadt_table->xpm1a_event_block, addr);
+    ACPI_READ_GENERIC_ADDRESS(qts, fadt_table->xpm1b_event_block, addr);
+    ACPI_READ_GENERIC_ADDRESS(qts, fadt_table->xpm1a_control_block, addr);
+    ACPI_READ_GENERIC_ADDRESS(qts, fadt_table->xpm1b_control_block, addr);
+    ACPI_READ_GENERIC_ADDRESS(qts, fadt_table->xpm2_control_block, addr);
+    ACPI_READ_GENERIC_ADDRESS(qts, fadt_table->xpm_timer_block, addr);
+    ACPI_READ_GENERIC_ADDRESS(qts, fadt_table->xgpe0_block, addr);
+    ACPI_READ_GENERIC_ADDRESS(qts, fadt_table->xgpe1_block, addr);

     ACPI_ASSERT_CMP(fadt_table->signature, "FACP");
     g_assert(!acpi_calc_checksum((uint8_t *)fadt_table, fadt_table->length));
@@ -195,26 +197,27 @@ static void test_acpi_facs_table(test_data *data)
     AcpiFacsDescriptorRev1 *facs_table = &data->facs_table;
     uint32_t addr = data->fadt_table.firmware_ctrl;

-    ACPI_READ_FIELD(facs_table->signature, addr);
-    ACPI_READ_FIELD(facs_table->length, addr);
-    ACPI_READ_FIELD(facs_table->hardware_signature, addr);
-    ACPI_READ_FIELD(facs_table->firmware_waking_vector, addr);
-    ACPI_READ_FIELD(facs_table->global_lock, addr);
-    ACPI_READ_FIELD(facs_table->flags, addr);
-    ACPI_READ_ARRAY(facs_table->resverved3, addr);
+    ACPI_READ_FIELD(data->qts, facs_table->signature, addr);
+    ACPI_READ_FIELD(data->qts, facs_table->length, addr);
+    ACPI_READ_FIELD(data->qts, facs_table->hardware_signature, addr);
+    ACPI_READ_FIELD(data->qts, facs_table->firmware_waking_vector, addr);
+    ACPI_READ_FIELD(data->qts, facs_table->global_lock, addr);
+    ACPI_READ_FIELD(data->qts, facs_table->flags, addr);
+    ACPI_READ_ARRAY(data->qts, facs_table->resverved3, addr);

     ACPI_ASSERT_CMP(facs_table->signature, "FACS");
 }

-static void test_dst_table(AcpiSdtTable *sdt_table, uint32_t addr)
+static void test_dst_table(test_data *data, AcpiSdtTable *sdt_table,
+                           uint32_t addr)
 {
     uint8_t checksum;

-    ACPI_READ_TABLE_HEADER(&sdt_table->header, addr);
+    ACPI_READ_TABLE_HEADER(data->qts, &sdt_table->header, addr);

     sdt_table->aml_len = sdt_table->header.length - sizeof(AcpiTableHeader);
     sdt_table->aml = g_malloc0(sdt_table->aml_len);
-    ACPI_READ_ARRAY_PTR(sdt_table->aml, sdt_table->aml_len, addr);
+    ACPI_READ_ARRAY_PTR(data->qts, sdt_table->aml, sdt_table->aml_len, addr);

     checksum = acpi_calc_checksum((uint8_t *)sdt_table,
                                   sizeof(AcpiTableHeader)) +
@@ -231,7 +234,7 @@ static void test_acpi_dsdt_table(test_data *data)
     memset(&dsdt_table, 0, sizeof(dsdt_table));
     data->tables = g_array_new(false, true, sizeof(AcpiSdtTable));

-    test_dst_table(&dsdt_table, addr);
+    test_dst_table(data, &dsdt_table, addr);
     ACPI_ASSERT_CMP(dsdt_table.header.signature, "DSDT");

     /* Place DSDT first */
@@ -248,7 +251,7 @@ static void test_acpi_tables(test_data *data)

         memset(&ssdt_table, 0, sizeof(ssdt_table));
         uint32_t addr = data->rsdt_tables_addr[i + 1]; /* fadt is first */
-        test_dst_table(&ssdt_table, addr);
+        test_dst_table(data, &ssdt_table, addr);
         g_array_append_val(data->tables, ssdt_table);
     }
 }
@@ -492,32 +495,32 @@ static bool smbios_ep_table_ok(test_data *data)
     struct smbios_21_entry_point *ep_table = &data->smbios_ep_table;
     uint32_t addr = data->smbios_ep_addr;

-    ACPI_READ_ARRAY(ep_table->anchor_string, addr);
+    ACPI_READ_ARRAY(data->qts, ep_table->anchor_string, addr);
     if (memcmp(ep_table->anchor_string, "_SM_", 4)) {
         return false;
     }
-    ACPI_READ_FIELD(ep_table->checksum, addr);
-    ACPI_READ_FIELD(ep_table->length, addr);
-    ACPI_READ_FIELD(ep_table->smbios_major_version, addr);
-    ACPI_READ_FIELD(ep_table->smbios_minor_version, addr);
-    ACPI_READ_FIELD(ep_table->max_structure_size, addr);
-    ACPI_READ_FIELD(ep_table->entry_point_revision, addr);
-    ACPI_READ_ARRAY(ep_table->formatted_area, addr);
-    ACPI_READ_ARRAY(ep_table->intermediate_anchor_string, addr);
+    ACPI_READ_FIELD(data->qts, ep_table->checksum, addr);
+    ACPI_READ_FIELD(data->qts, ep_table->length, addr);
+    ACPI_READ_FIELD(data->qts, ep_table->smbios_major_version, addr);
+    ACPI_READ_FIELD(data->qts, ep_table->smbios_minor_version, addr);
+    ACPI_READ_FIELD(data->qts, ep_table->max_structure_size, addr);
+    ACPI_READ_FIELD(data->qts, ep_table->entry_point_revision, addr);
+    ACPI_READ_ARRAY(data->qts, ep_table->formatted_area, addr);
+    ACPI_READ_ARRAY(data->qts, ep_table->intermediate_anchor_string, addr);
     if (memcmp(ep_table->intermediate_anchor_string, "_DMI_", 5)) {
         return false;
     }
-    ACPI_READ_FIELD(ep_table->intermediate_checksum, addr);
-    ACPI_READ_FIELD(ep_table->structure_table_length, addr);
+    ACPI_READ_FIELD(data->qts, ep_table->intermediate_checksum, addr);
+    ACPI_READ_FIELD(data->qts, ep_table->structure_table_length, addr);
     if (ep_table->structure_table_length == 0) {
         return false;
     }
-    ACPI_READ_FIELD(ep_table->structure_table_address, addr);
-    ACPI_READ_FIELD(ep_table->number_of_structures, addr);
+    ACPI_READ_FIELD(data->qts, ep_table->structure_table_address, addr);
+    ACPI_READ_FIELD(data->qts, ep_table->number_of_structures, addr);
     if (ep_table->number_of_structures == 0) {
         return false;
     }
-    ACPI_READ_FIELD(ep_table->smbios_bcd_revision, addr);
+    ACPI_READ_FIELD(data->qts, ep_table->smbios_bcd_revision, addr);
     if (acpi_calc_checksum((uint8_t *)ep_table, sizeof *ep_table) ||
         acpi_calc_checksum((uint8_t *)ep_table + 0x10,
                            sizeof *ep_table - 0x10)) {
@@ -536,7 +539,7 @@ static void test_smbios_entry_point(test_data *data)
         int i;

         for (i = 0; i < sizeof sig - 1; ++i) {
-            sig[i] = readb(off + i);
+            sig[i] = qtest_readb(data->qts, off + i);
         }

         if (!memcmp(sig, "_SM_", sizeof sig)) {
@@ -579,9 +582,9 @@ static void test_smbios_structs(test_data *data)
     for (i = 0; i < ep_table->number_of_structures; i++) {

         /* grab type and formatted area length from struct header */
-        type = readb(addr);
+        type = qtest_readb(data->qts, addr);
         g_assert_cmpuint(type, <=, SMBIOS_MAX_TYPE);
-        len = readb(addr + 1);
+        len = qtest_readb(data->qts, addr + 1);

         /* single-instance structs must not have been encountered before */
         if (smbios_single_instance(type)) {
@@ -593,7 +596,7 @@ static void test_smbios_structs(test_data *data)
         prv = crt = 1;
         while (prv || crt) {
             prv = crt;
-            crt = readb(addr + len);
+            crt = qtest_readb(data->qts, addr + len);
             len++;
         }

@@ -630,9 +633,9 @@ static void test_acpi_one(const char *params, test_data *data)
                            data->machine, "kvm:tcg",
                            params ? params : "", disk);

-    qtest_start(args);
+    data->qts = qtest_init(args);

-    boot_sector_test(global_qtest);
+    boot_sector_test(data->qts);

     test_acpi_rsdp_address(data);
     test_acpi_rsdp_table(data);
@@ -653,7 +656,8 @@ static void test_acpi_one(const char *params, test_data *data)
     test_smbios_entry_point(data);
     test_smbios_structs(data);

-    qtest_quit(global_qtest);
+    assert(!global_qtest);
+    qtest_quit(data->qts);
     g_free(args);
 }

diff --git a/tests/vmgenid-test.c b/tests/vmgenid-test.c
index 0ea98b86d7..970c6274fd 100644
--- a/tests/vmgenid-test.c
+++ b/tests/vmgenid-test.c
@@ -50,15 +50,15 @@ static uint32_t acpi_find_vgia(void)
     boot_sector_test(global_qtest);

     /* Tables should be initialized now. */
-    rsdp_offset = acpi_find_rsdp_address();
+    rsdp_offset = acpi_find_rsdp_address(global_qtest);

     g_assert_cmphex(rsdp_offset, <, RSDP_ADDR_INVALID);

-    acpi_parse_rsdp_table(rsdp_offset, &rsdp_table);
+    acpi_parse_rsdp_table(global_qtest, rsdp_offset, &rsdp_table);

     rsdt = rsdp_table.rsdt_physical_address;
     /* read the header */
-    ACPI_READ_TABLE_HEADER(&rsdt_table, rsdt);
+    ACPI_READ_TABLE_HEADER(global_qtest, &rsdt_table, rsdt);
     ACPI_ASSERT_CMP(rsdt_table.signature, "RSDT");

     /* compute the table entries in rsdt */
@@ -68,21 +68,21 @@ static uint32_t acpi_find_vgia(void)

     /* get the addresses of the tables pointed by rsdt */
     tables = g_new0(uint32_t, tables_nr);
-    ACPI_READ_ARRAY_PTR(tables, tables_nr, rsdt);
+    ACPI_READ_ARRAY_PTR(global_qtest, tables, tables_nr, rsdt);

     for (i = 0; i < tables_nr; i++) {
-        ACPI_READ_TABLE_HEADER(&ssdt_table, tables[i]);
+        ACPI_READ_TABLE_HEADER(global_qtest, &ssdt_table, tables[i]);
         if (!strncmp((char *)ssdt_table.oem_table_id, "VMGENID", 7)) {
             /* the first entry in the table should be VGIA
              * That's all we need
              */
-            ACPI_READ_FIELD(vgid_table.name_op, tables[i]);
+            ACPI_READ_FIELD(global_qtest, vgid_table.name_op, tables[i]);
             g_assert(vgid_table.name_op == 0x08);  /* name */
-            ACPI_READ_ARRAY(vgid_table.vgia, tables[i]);
+            ACPI_READ_ARRAY(global_qtest, vgid_table.vgia, tables[i]);
             g_assert(memcmp(vgid_table.vgia, "VGIA", 4) == 0);
-            ACPI_READ_FIELD(vgid_table.val_op, tables[i]);
+            ACPI_READ_FIELD(global_qtest, vgid_table.val_op, tables[i]);
             g_assert(vgid_table.val_op == 0x0C);  /* dword */
-            ACPI_READ_FIELD(vgid_table.vgia_val, tables[i]);
+            ACPI_READ_FIELD(global_qtest, vgid_table.vgia_val, tables[i]);
             /* The GUID is written at a fixed offset into the fw_cfg file
              * in order to implement the "OVMF SDT Header probe suppressor"
              * see docs/specs/vmgenid.txt for more details
-- 
2.13.5

^ permalink raw reply related	[flat|nested] 75+ messages in thread

* [Qemu-devel] [PATCH v7 25/38] wdt_ib700-test: Drop dependence on global_qtest
  2017-09-11 17:19 [Qemu-devel] [PATCH v7 00/38] Preliminary libqtest cleanups Eric Blake
                   ` (23 preceding siblings ...)
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 24/38] tests/acpi-utils: " Eric Blake
@ 2017-09-11 17:20 ` Eric Blake
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 26/38] libqtest: Merge qtest_end() into qtest_quit() Eric Blake
                   ` (12 subsequent siblings)
  37 siblings, 0 replies; 75+ messages in thread
From: Eric Blake @ 2017-09-11 17:20 UTC (permalink / raw)
  To: qemu-devel; +Cc: armbru, pbonzini, thuth

As a general rule, we prefer avoiding implicit global state
because it makes code harder to safely copy and paste without
thinking about the global state.  Improve this test to be
explicit about the state.

Signed-off-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Thomas Huth <thuth@redhat.com>
---
 tests/wdt_ib700-test.c | 63 +++++++++++++++++++++++++++-----------------------
 1 file changed, 34 insertions(+), 29 deletions(-)

diff --git a/tests/wdt_ib700-test.c b/tests/wdt_ib700-test.c
index 4fc8eeae86..4bb8374011 100644
--- a/tests/wdt_ib700-test.c
+++ b/tests/wdt_ib700-test.c
@@ -11,9 +11,9 @@
 #include "libqtest.h"
 #include "qemu/timer.h"

-static void qmp_check_no_event(void)
+static void qmp_check_no_event(QTestState *s)
 {
-    QDict *resp = qmp("{'execute':'query-status'}");
+    QDict *resp = qtest_qmp(s, "{'execute':'query-status'}");
     g_assert(qdict_haskey(resp, "return"));
     QDECREF(resp);
 }
@@ -22,30 +22,30 @@ static QDict *ib700_program_and_wait(QTestState *s)
 {
     QDict *event, *data;

-    clock_step(NANOSECONDS_PER_SECOND * 40);
-    qmp_check_no_event();
+    qtest_clock_step(s, NANOSECONDS_PER_SECOND * 40);
+    qmp_check_no_event(s);

     /* 2 second limit */
-    outb(0x443, 14);
+    qtest_outb(s, 0x443, 14);

     /* Ping */
-    clock_step(NANOSECONDS_PER_SECOND);
-    qmp_check_no_event();
-    outb(0x443, 14);
+    qtest_clock_step(s, NANOSECONDS_PER_SECOND);
+    qmp_check_no_event(s);
+    qtest_outb(s, 0x443, 14);

     /* Disable */
-    clock_step(NANOSECONDS_PER_SECOND);
-    qmp_check_no_event();
-    outb(0x441, 1);
-    clock_step(3 * NANOSECONDS_PER_SECOND);
-    qmp_check_no_event();
+    qtest_clock_step(s, NANOSECONDS_PER_SECOND);
+    qmp_check_no_event(s);
+    qtest_outb(s, 0x441, 1);
+    qtest_clock_step(s, 3 * NANOSECONDS_PER_SECOND);
+    qmp_check_no_event(s);

     /* Enable and let it fire */
-    outb(0x443, 13);
-    clock_step(3 * NANOSECONDS_PER_SECOND);
-    qmp_check_no_event();
-    clock_step(2 * NANOSECONDS_PER_SECOND);
-    event = qmp_eventwait_ref("WATCHDOG");
+    qtest_outb(s, 0x443, 13);
+    qtest_clock_step(s, 3 * NANOSECONDS_PER_SECOND);
+    qmp_check_no_event(s);
+    qtest_clock_step(s, 2 * NANOSECONDS_PER_SECOND);
+    event = qtest_qmp_eventwait_ref(s, "WATCHDOG");
     data = qdict_get_qdict(event, "data");
     QINCREF(data);
     QDECREF(event);
@@ -56,48 +56,53 @@ static QDict *ib700_program_and_wait(QTestState *s)
 static void ib700_pause(void)
 {
     QDict *d;
-    QTestState *s = qtest_start("-watchdog-action pause -device ib700");
+    QTestState *s = qtest_init("-watchdog-action pause -device ib700");
+
     qtest_irq_intercept_in(s, "ioapic");
     d = ib700_program_and_wait(s);
     g_assert(!strcmp(qdict_get_str(d, "action"), "pause"));
     QDECREF(d);
-    qmp_eventwait("STOP");
-    qtest_end();
+    qtest_qmp_eventwait(s, "STOP");
+    qtest_quit(s);
 }

 static void ib700_reset(void)
 {
     QDict *d;
-    QTestState *s = qtest_start("-watchdog-action reset -device ib700");
+    QTestState *s = qtest_init("-watchdog-action reset -device ib700");
+
     qtest_irq_intercept_in(s, "ioapic");
     d = ib700_program_and_wait(s);
     g_assert(!strcmp(qdict_get_str(d, "action"), "reset"));
     QDECREF(d);
-    qmp_eventwait("RESET");
-    qtest_end();
+    qtest_qmp_eventwait(s, "RESET");
+    qtest_quit(s);
 }

 static void ib700_shutdown(void)
 {
     QDict *d;
-    QTestState *s = qtest_start("-watchdog-action reset -no-reboot -device ib700");
+    QTestState *s;
+
+    s = qtest_init("-watchdog-action reset -no-reboot -device ib700");
     qtest_irq_intercept_in(s, "ioapic");
     d = ib700_program_and_wait(s);
     g_assert(!strcmp(qdict_get_str(d, "action"), "reset"));
     QDECREF(d);
-    qmp_eventwait("SHUTDOWN");
-    qtest_end();
+    qtest_qmp_eventwait(s, "SHUTDOWN");
+    qtest_quit(s);
 }

 static void ib700_none(void)
 {
     QDict *d;
-    QTestState *s = qtest_start("-watchdog-action none -device ib700");
+    QTestState *s = qtest_init("-watchdog-action none -device ib700");
+
     qtest_irq_intercept_in(s, "ioapic");
     d = ib700_program_and_wait(s);
     g_assert(!strcmp(qdict_get_str(d, "action"), "none"));
     QDECREF(d);
-    qtest_end();
+    qtest_quit(s);
 }

 int main(int argc, char **argv)
-- 
2.13.5

^ permalink raw reply related	[flat|nested] 75+ messages in thread

* [Qemu-devel] [PATCH v7 26/38] libqtest: Merge qtest_end() into qtest_quit()
  2017-09-11 17:19 [Qemu-devel] [PATCH v7 00/38] Preliminary libqtest cleanups Eric Blake
                   ` (24 preceding siblings ...)
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 25/38] wdt_ib700-test: " Eric Blake
@ 2017-09-11 17:20 ` Eric Blake
  2017-09-12  0:31   ` John Snow
  2017-09-12  9:30   ` Thomas Huth
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 27/38] libqtest: Swap order of qtest_init() and qtest_start() Eric Blake
                   ` (11 subsequent siblings)
  37 siblings, 2 replies; 75+ messages in thread
From: Eric Blake @ 2017-09-11 17:20 UTC (permalink / raw)
  To: qemu-devel
  Cc: armbru, pbonzini, thuth, Gerd Hoffmann, John Snow, Keith Busch,
	Andreas Färber, David Gibson, Alexander Graf,
	Dr. David Alan Gilbert, Michael S. Tsirkin, Stefan Hajnoczi,
	Amit Shah, Jason Wang, open list:Floppy, open list:sPAPR

Rather than have two similar shutdown functions, where one requires
the use of global_qtest in the header, it is better to have a single
shutdown function that still takes care of cleaning up global_qtest
if it is set.  All callers are updated.

Signed-off-by: Eric Blake <eblake@redhat.com>
---
 tests/libqtest.h               | 11 -----------
 tests/libqtest.c               |  6 +++++-
 tests/ac97-test.c              |  2 +-
 tests/device-introspect-test.c | 12 ++++++------
 tests/display-vga-test.c       | 12 ++++++------
 tests/drive_del-test.c         |  6 +++---
 tests/e1000e-test.c            |  4 ++--
 tests/es1370-test.c            |  2 +-
 tests/fdc-test.c               |  2 +-
 tests/hd-geo-test.c            |  8 ++++----
 tests/i440fx-test.c            |  6 +++---
 tests/i82801b11-test.c         |  2 +-
 tests/ide-test.c               |  2 +-
 tests/intel-hda-test.c         |  4 ++--
 tests/ioh3420-test.c           |  2 +-
 tests/ipoctal232-test.c        |  2 +-
 tests/ne2000-test.c            |  2 +-
 tests/numa-test.c              | 14 +++++++-------
 tests/nvme-test.c              |  2 +-
 tests/pc-cpu-test.c            |  4 ++--
 tests/pcnet-test.c             |  2 +-
 tests/pvpanic-test.c           |  2 +-
 tests/q35-test.c               |  4 ++--
 tests/qmp-test.c               |  4 ++--
 tests/qom-test.c               |  2 +-
 tests/rtl8139-test.c           |  2 +-
 tests/spapr-phb-test.c         |  2 +-
 tests/tco-test.c               |  2 +-
 tests/test-arm-mptimer.c       |  2 +-
 tests/test-filter-mirror.c     |  2 +-
 tests/test-filter-redirector.c |  4 ++--
 tests/test-hmp.c               |  2 +-
 tests/test-netfilter.c         |  2 +-
 tests/test-x86-cpuid-compat.c  |  6 +++---
 tests/tpci200-test.c           |  2 +-
 tests/usb-hcd-ehci-test.c      |  2 +-
 tests/usb-hcd-ohci-test.c      |  2 +-
 tests/usb-hcd-xhci-test.c      |  2 +-
 tests/virtio-balloon-test.c    |  2 +-
 tests/virtio-blk-test.c        |  2 +-
 tests/virtio-console-test.c    |  4 ++--
 tests/virtio-net-test.c        |  2 +-
 tests/virtio-rng-test.c        |  2 +-
 tests/virtio-serial-test.c     |  2 +-
 tests/vmxnet3-test.c           |  2 +-
 45 files changed, 80 insertions(+), 87 deletions(-)

diff --git a/tests/libqtest.h b/tests/libqtest.h
index 3ae570927a..d976a542b8 100644
--- a/tests/libqtest.h
+++ b/tests/libqtest.h
@@ -523,17 +523,6 @@ static inline QTestState *qtest_start(const char *args)
 }

 /**
- * qtest_end:
- *
- * Shut down the QEMU process started by qtest_start().
- */
-static inline void qtest_end(void)
-{
-    qtest_quit(global_qtest);
-    global_qtest = NULL;
-}
-
-/**
  * qmp:
  * @fmt...: QMP message to send to qemu
  *
diff --git a/tests/libqtest.c b/tests/libqtest.c
index 1710d63276..94c157ce02 100644
--- a/tests/libqtest.c
+++ b/tests/libqtest.c
@@ -246,6 +246,10 @@ QTestState *qtest_init(const char *extra_args)

 void qtest_quit(QTestState *s)
 {
+    if (global_qtest) {
+        assert(s == global_qtest);
+        global_qtest = NULL;
+    }
     g_hook_destroy_link(&abrt_hooks, g_hook_find_data(&abrt_hooks, TRUE, s));

     /* Uninstall SIGABRT handler on last instance */
@@ -979,6 +983,6 @@ void qtest_cb_for_every_machine(void (*cb)(const char *machine))
         cb(mname);
     }

-    qtest_end();
+    qtest_quit(global_qtest);
     QDECREF(response);
 }
diff --git a/tests/ac97-test.c b/tests/ac97-test.c
index e0d177bd9c..ca9b3dce88 100644
--- a/tests/ac97-test.c
+++ b/tests/ac97-test.c
@@ -25,7 +25,7 @@ int main(int argc, char **argv)
     qtest_start("-device AC97");
     ret = g_test_run();

-    qtest_end();
+    qtest_quit(global_qtest);

     return ret;
 }
diff --git a/tests/device-introspect-test.c b/tests/device-introspect-test.c
index f7162c023f..b3227bd950 100644
--- a/tests/device-introspect-test.c
+++ b/tests/device-introspect-test.c
@@ -134,7 +134,7 @@ static void test_device_intro_list(void)
     help = hmp("device_add help");
     g_free(help);

-    qtest_end();
+    qtest_quit(global_qtest);
 }

 /*
@@ -189,21 +189,21 @@ static void test_qom_list_fields(void)

     QDECREF(all_types);
     QDECREF(non_abstract);
-    qtest_end();
+    qtest_quit(global_qtest);
 }

 static void test_device_intro_none(void)
 {
     qtest_start(common_args);
     test_one_device("nonexistent");
-    qtest_end();
+    qtest_quit(global_qtest);
 }

 static void test_device_intro_abstract(void)
 {
     qtest_start(common_args);
     test_one_device("device");
-    qtest_end();
+    qtest_quit(global_qtest);
 }

 static void test_device_intro_concrete(void)
@@ -223,7 +223,7 @@ static void test_device_intro_concrete(void)
     }

     QDECREF(types);
-    qtest_end();
+    qtest_quit(global_qtest);
 }

 static void test_abstract_interfaces(void)
@@ -257,7 +257,7 @@ static void test_abstract_interfaces(void)

     QDECREF(all_types);
     QDECREF(index);
-    qtest_end();
+    qtest_quit(global_qtest);
 }

 int main(int argc, char **argv)
diff --git a/tests/display-vga-test.c b/tests/display-vga-test.c
index 2d7d24eee0..8667330e3c 100644
--- a/tests/display-vga-test.c
+++ b/tests/display-vga-test.c
@@ -13,38 +13,38 @@
 static void pci_cirrus(void)
 {
     qtest_start("-vga none -device cirrus-vga");
-    qtest_end();
+    qtest_quit(global_qtest);
 }

 static void pci_stdvga(void)
 {
     qtest_start("-vga none -device VGA");
-    qtest_end();
+    qtest_quit(global_qtest);
 }

 static void pci_secondary(void)
 {
     qtest_start("-vga none -device secondary-vga");
-    qtest_end();
+    qtest_quit(global_qtest);
 }

 static void pci_multihead(void)
 {
     qtest_start("-vga none -device VGA -device secondary-vga");
-    qtest_end();
+    qtest_quit(global_qtest);
 }

 static void pci_virtio_gpu(void)
 {
     qtest_start("-vga none -device virtio-gpu-pci");
-    qtest_end();
+    qtest_quit(global_qtest);
 }

 #ifdef CONFIG_VIRTIO_VGA
 static void pci_virtio_vga(void)
 {
     qtest_start("-vga none -device virtio-vga");
-    qtest_end();
+    qtest_quit(global_qtest);
 }
 #endif

diff --git a/tests/drive_del-test.c b/tests/drive_del-test.c
index 2175139abb..16d256fe64 100644
--- a/tests/drive_del-test.c
+++ b/tests/drive_del-test.c
@@ -55,7 +55,7 @@ static void test_drive_without_dev(void)
      */
     drive_add();

-    qtest_end();
+    qtest_quit(global_qtest);
 }

 static void test_after_failed_device_add(void)
@@ -86,7 +86,7 @@ static void test_after_failed_device_add(void)
      */
     drive_add();

-    qtest_end();
+    qtest_quit(global_qtest);
 }

 static void test_drive_del_device_del(void)
@@ -103,7 +103,7 @@ static void test_drive_del_device_del(void)
     drive_del();
     device_del();

-    qtest_end();
+    qtest_quit(global_qtest);
 }

 int main(int argc, char **argv)
diff --git a/tests/e1000e-test.c b/tests/e1000e-test.c
index 8645485c19..98f821fa7b 100644
--- a/tests/e1000e-test.c
+++ b/tests/e1000e-test.c
@@ -408,7 +408,7 @@ static void data_test_clear(e1000e_device *d)
     pc_alloc_uninit(test_alloc);
     g_free(d->pci_dev);
     qpci_free_pc(test_bus);
-    qtest_end();
+    qtest_quit(global_qtest);
 }

 static void test_e1000e_init(gconstpointer data)
@@ -463,7 +463,7 @@ static void test_e1000e_hotplug(gconstpointer data)
     qpci_plug_device_test(global_qtest, "e1000e", "e1000e_net", slot, NULL);
     qpci_unplug_device_test(global_qtest, "e1000e_net", slot);

-    qtest_end();
+    qtest_quit(global_qtest);
 }

 int main(int argc, char **argv)
diff --git a/tests/es1370-test.c b/tests/es1370-test.c
index 199fe193ce..5578ff94fa 100644
--- a/tests/es1370-test.c
+++ b/tests/es1370-test.c
@@ -25,7 +25,7 @@ int main(int argc, char **argv)
     qtest_start("-device ES1370");
     ret = g_test_run();

-    qtest_end();
+    qtest_quit(global_qtest);

     return ret;
 }
diff --git a/tests/fdc-test.c b/tests/fdc-test.c
index 325712e0f2..e63e93179a 100644
--- a/tests/fdc-test.c
+++ b/tests/fdc-test.c
@@ -583,7 +583,7 @@ int main(int argc, char **argv)
     ret = g_test_run();

     /* Cleanup */
-    qtest_end();
+    qtest_quit(global_qtest);
     unlink(test_image);

     return ret;
diff --git a/tests/hd-geo-test.c b/tests/hd-geo-test.c
index 24870b38f4..67d571ef13 100644
--- a/tests/hd-geo-test.c
+++ b/tests/hd-geo-test.c
@@ -245,7 +245,7 @@ static void test_ide_none(void)
     g_strfreev(argv);
     g_free(args);
     test_cmos();
-    qtest_end();
+    qtest_quit(global_qtest);
 }

 static void test_ide_mbr(bool use_device, MBRcontents mbr)
@@ -267,7 +267,7 @@ static void test_ide_mbr(bool use_device, MBRcontents mbr)
     g_strfreev(argv);
     g_free(args);
     test_cmos();
-    qtest_end();
+    qtest_quit(global_qtest);
 }

 /*
@@ -343,7 +343,7 @@ static void test_ide_drive_user(const char *dev, bool trans)
     g_strfreev(argv);
     g_free(args);
     test_cmos();
-    qtest_end();
+    qtest_quit(global_qtest);
 }

 /*
@@ -400,7 +400,7 @@ static void test_ide_drive_cd_0(void)
     g_strfreev(argv);
     g_free(args);
     test_cmos();
-    qtest_end();
+    qtest_quit(global_qtest);
 }

 int main(int argc, char **argv)
diff --git a/tests/i440fx-test.c b/tests/i440fx-test.c
index 4390e5591e..c847e4dbbb 100644
--- a/tests/i440fx-test.c
+++ b/tests/i440fx-test.c
@@ -136,7 +136,7 @@ static void test_i440fx_defaults(gconstpointer opaque)

     g_free(dev);
     qpci_free_pc(bus);
-    qtest_end();
+    qtest_quit(global_qtest);
 }

 #define PAM_RE 1
@@ -275,7 +275,7 @@ static void test_i440fx_pam(gconstpointer opaque)

     g_free(dev);
     qpci_free_pc(bus);
-    qtest_end();
+    qtest_quit(global_qtest);
 }

 #define BLOB_SIZE ((size_t)65536)
@@ -373,7 +373,7 @@ static void test_i440fx_firmware(FirmwareTestFixture *fixture,
     }

     g_free(buf);
-    qtest_end();
+    qtest_quit(global_qtest);
 }

 static void add_firmware_test(const char *testpath,
diff --git a/tests/i82801b11-test.c b/tests/i82801b11-test.c
index a6e31594c9..0c94876626 100644
--- a/tests/i82801b11-test.c
+++ b/tests/i82801b11-test.c
@@ -25,7 +25,7 @@ int main(int argc, char **argv)
     qtest_start("-machine q35 -device i82801b11-bridge,bus=pcie.0,addr=1e.0");
     ret = g_test_run();

-    qtest_end();
+    qtest_quit(global_qtest);

     return ret;
 }
diff --git a/tests/ide-test.c b/tests/ide-test.c
index 084f6a5f96..16b73e0101 100644
--- a/tests/ide-test.c
+++ b/tests/ide-test.c
@@ -134,7 +134,7 @@ static void ide_test_quit(void)
 {
     pc_alloc_uninit(guest_malloc);
     guest_malloc = NULL;
-    qtest_end();
+    qtest_quit(global_qtest);
 }

 static QPCIDevice *get_pci_device(QPCIBar *bmdma_bar, QPCIBar *ide_bar)
diff --git a/tests/intel-hda-test.c b/tests/intel-hda-test.c
index b782b2e944..ce24fbac1f 100644
--- a/tests/intel-hda-test.c
+++ b/tests/intel-hda-test.c
@@ -19,14 +19,14 @@
 static void ich6_test(void)
 {
     qtest_start("-device intel-hda,id=" HDA_ID CODEC_DEVICES);
-    qtest_end();
+    qtest_quit(global_qtest);
 }

 static void ich9_test(void)
 {
     qtest_start("-machine q35 -device ich9-intel-hda,bus=pcie.0,addr=1b.0,id="
                 HDA_ID CODEC_DEVICES);
-    qtest_end();
+    qtest_quit(global_qtest);
 }

 int main(int argc, char **argv)
diff --git a/tests/ioh3420-test.c b/tests/ioh3420-test.c
index b54c4b9f11..f2ca373e87 100644
--- a/tests/ioh3420-test.c
+++ b/tests/ioh3420-test.c
@@ -26,7 +26,7 @@ int main(int argc, char **argv)
                 "chassis=1,multifunction=on");
     ret = g_test_run();

-    qtest_end();
+    qtest_quit(global_qtest);

     return ret;
 }
diff --git a/tests/ipoctal232-test.c b/tests/ipoctal232-test.c
index 684914164d..d17a93449f 100644
--- a/tests/ipoctal232-test.c
+++ b/tests/ipoctal232-test.c
@@ -25,7 +25,7 @@ int main(int argc, char **argv)
     qtest_start("-device tpci200,id=ipack0 -device ipoctal232,bus=ipack0.0");
     ret = g_test_run();

-    qtest_end();
+    qtest_quit(global_qtest);

     return ret;
 }
diff --git a/tests/ne2000-test.c b/tests/ne2000-test.c
index b7cf3dd2f5..cae83c5c4c 100644
--- a/tests/ne2000-test.c
+++ b/tests/ne2000-test.c
@@ -25,7 +25,7 @@ int main(int argc, char **argv)
     qtest_start("-device ne2k_pci");
     ret = g_test_run();

-    qtest_end();
+    qtest_quit(global_qtest);

     return ret;
 }
diff --git a/tests/numa-test.c b/tests/numa-test.c
index e1b6152244..fa21d26935 100644
--- a/tests/numa-test.c
+++ b/tests/numa-test.c
@@ -32,7 +32,7 @@ static void test_mon_explicit(const void *data)
     g_assert(strstr(s, "node 1 cpus: 4 5 6 7"));
     g_free(s);

-    qtest_end();
+    qtest_quit(global_qtest);
     g_free(cli);
 }

@@ -49,7 +49,7 @@ static void test_mon_default(const void *data)
     g_assert(strstr(s, "node 1 cpus: 1 3 5 7"));
     g_free(s);

-    qtest_end();
+    qtest_quit(global_qtest);
     g_free(cli);
 }

@@ -68,7 +68,7 @@ static void test_mon_partial(const void *data)
     g_assert(strstr(s, "node 1 cpus: 4 5"));
     g_free(s);

-    qtest_end();
+    qtest_quit(global_qtest);
     g_free(cli);
 }

@@ -113,7 +113,7 @@ static void test_query_cpus(const void *data)
     }

     QDECREF(resp);
-    qtest_end();
+    qtest_quit(global_qtest);
     g_free(cli);
 }

@@ -166,7 +166,7 @@ static void pc_numa_cpu(const void *data)
     }

     QDECREF(resp);
-    qtest_end();
+    qtest_quit(global_qtest);
     g_free(cli);
 }

@@ -211,7 +211,7 @@ static void spapr_numa_cpu(const void *data)
     }

     QDECREF(resp);
-    qtest_end();
+    qtest_quit(global_qtest);
     g_free(cli);
 }

@@ -254,7 +254,7 @@ static void aarch64_numa_cpu(const void *data)
     }

     QDECREF(resp);
-    qtest_end();
+    qtest_quit(global_qtest);
     g_free(cli);
 }

diff --git a/tests/nvme-test.c b/tests/nvme-test.c
index 7674a446e4..3d6c0f39cf 100644
--- a/tests/nvme-test.c
+++ b/tests/nvme-test.c
@@ -26,7 +26,7 @@ int main(int argc, char **argv)
                 "-device nvme,drive=drv0,serial=foo");
     ret = g_test_run();

-    qtest_end();
+    qtest_quit(global_qtest);

     return ret;
 }
diff --git a/tests/pc-cpu-test.c b/tests/pc-cpu-test.c
index c4211a4e85..3ff7115625 100644
--- a/tests/pc-cpu-test.c
+++ b/tests/pc-cpu-test.c
@@ -44,7 +44,7 @@ static void test_pc_with_cpu_add(gconstpointer data)
         QDECREF(response);
     }

-    qtest_end();
+    qtest_quit(global_qtest);
     g_free(args);
 }

@@ -67,7 +67,7 @@ static void test_pc_without_cpu_add(gconstpointer data)
     g_assert(qdict_haskey(response, "error"));
     QDECREF(response);

-    qtest_end();
+    qtest_quit(global_qtest);
     g_free(args);
 }

diff --git a/tests/pcnet-test.c b/tests/pcnet-test.c
index efb1ef44e9..98246d3504 100644
--- a/tests/pcnet-test.c
+++ b/tests/pcnet-test.c
@@ -25,7 +25,7 @@ int main(int argc, char **argv)
     qtest_start("-device pcnet");
     ret = g_test_run();

-    qtest_end();
+    qtest_quit(global_qtest);

     return ret;
 }
diff --git a/tests/pvpanic-test.c b/tests/pvpanic-test.c
index 71ebb5c02c..5d99b3d9e2 100644
--- a/tests/pvpanic-test.c
+++ b/tests/pvpanic-test.c
@@ -40,7 +40,7 @@ int main(int argc, char **argv)
     qtest_start("-device pvpanic");
     ret = g_test_run();

-    qtest_end();
+    qtest_quit(global_qtest);

     return ret;
 }
diff --git a/tests/q35-test.c b/tests/q35-test.c
index e149c4c51d..a8e24e5b71 100644
--- a/tests/q35-test.c
+++ b/tests/q35-test.c
@@ -119,7 +119,7 @@ static void test_smram_lock(void)
     g_free(pcidev);
     qpci_free_pc(pcibus);

-    qtest_end();
+    qtest_quit(global_qtest);
 }

 static void test_tseg_size(const void *data)
@@ -185,7 +185,7 @@ static void test_tseg_size(const void *data)

     g_free(pcidev);
     qpci_free_pc(pcibus);
-    qtest_end();
+    qtest_quit(global_qtest);
 }

 int main(int argc, char **argv)
diff --git a/tests/qmp-test.c b/tests/qmp-test.c
index 4e6198c9fa..a31c0f7de1 100644
--- a/tests/qmp-test.c
+++ b/tests/qmp-test.c
@@ -188,7 +188,7 @@ static void test_query(const void *data)
     }
     QDECREF(resp);

-    qtest_end();
+    qtest_quit(global_qtest);
 }

 static bool query_is_blacklisted(const char *cmd)
@@ -232,7 +232,7 @@ static void qmp_schema_init(QmpSchema *schema)
     visit_free(qiv);

     QDECREF(resp);
-    qtest_end();
+    qtest_quit(global_qtest);

     schema->hash = g_hash_table_new(g_str_hash, g_str_equal);

diff --git a/tests/qom-test.c b/tests/qom-test.c
index ab0595dc75..661a6edefd 100644
--- a/tests/qom-test.c
+++ b/tests/qom-test.c
@@ -102,7 +102,7 @@ static void test_machine(gconstpointer data)
     g_assert(qdict_haskey(response, "return"));
     QDECREF(response);

-    qtest_end();
+    qtest_quit(global_qtest);
     g_free(args);
     g_free((void *)machine);
 }
diff --git a/tests/rtl8139-test.c b/tests/rtl8139-test.c
index 68bfc42178..317eb586b5 100644
--- a/tests/rtl8139-test.c
+++ b/tests/rtl8139-test.c
@@ -205,7 +205,7 @@ int main(int argc, char **argv)

     ret = g_test_run();

-    qtest_end();
+    qtest_quit(global_qtest);

     return ret;
 }
diff --git a/tests/spapr-phb-test.c b/tests/spapr-phb-test.c
index d3522ea093..e76987ace9 100644
--- a/tests/spapr-phb-test.c
+++ b/tests/spapr-phb-test.c
@@ -29,7 +29,7 @@ int main(int argc, char **argv)

     ret = g_test_run();

-    qtest_end();
+    qtest_quit(global_qtest);

     return ret;
 }
diff --git a/tests/tco-test.c b/tests/tco-test.c
index 0387971953..3d4b3db308 100644
--- a/tests/tco-test.c
+++ b/tests/tco-test.c
@@ -49,7 +49,7 @@ static void test_end(TestData *d)
 {
     g_free(d->dev);
     qpci_free_pc(d->bus);
-    qtest_end();
+    qtest_quit(global_qtest);
 }

 static void test_init(TestData *d)
diff --git a/tests/test-arm-mptimer.c b/tests/test-arm-mptimer.c
index cb8f2df914..823db9ebc9 100644
--- a/tests/test-arm-mptimer.c
+++ b/tests/test-arm-mptimer.c
@@ -1099,7 +1099,7 @@ tests_with_prescaler_arg:

     qtest_start("-machine vexpress-a9");
     ret = g_test_run();
-    qtest_end();
+    qtest_quit(global_qtest);

     return ret;
 }
diff --git a/tests/test-filter-mirror.c b/tests/test-filter-mirror.c
index d569d27657..85fb38976d 100644
--- a/tests/test-filter-mirror.c
+++ b/tests/test-filter-mirror.c
@@ -86,7 +86,7 @@ int main(int argc, char **argv)

     qtest_add_func("/netfilter/mirror", test_mirror);
     ret = g_test_run();
-    qtest_end();
+    qtest_quit(global_qtest);

     return ret;
 }
diff --git a/tests/test-filter-redirector.c b/tests/test-filter-redirector.c
index 3afd41110d..d3a0b1cdf4 100644
--- a/tests/test-filter-redirector.c
+++ b/tests/test-filter-redirector.c
@@ -135,7 +135,7 @@ static void test_redirector_tx(void)
     close(recv_sock);
     unlink(sock_path0);
     unlink(sock_path1);
-    qtest_end();
+    qtest_quit(global_qtest);
 }

 static void test_redirector_rx(void)
@@ -204,7 +204,7 @@ static void test_redirector_rx(void)
     g_free(recv_buf);
     unlink(sock_path0);
     unlink(sock_path1);
-    qtest_end();
+    qtest_quit(global_qtest);
 }

 int main(int argc, char **argv)
diff --git a/tests/test-hmp.c b/tests/test-hmp.c
index 729c0339f7..752f4b63bd 100644
--- a/tests/test-hmp.c
+++ b/tests/test-hmp.c
@@ -127,7 +127,7 @@ static void test_machine(gconstpointer data)
     test_info_commands();
     test_commands();

-    qtest_end();
+    qtest_quit(global_qtest);
     g_free(args);
     g_free((void *)data);
 }
diff --git a/tests/test-netfilter.c b/tests/test-netfilter.c
index 2506473365..ffb11dbfb7 100644
--- a/tests/test-netfilter.c
+++ b/tests/test-netfilter.c
@@ -202,7 +202,7 @@ int main(int argc, char **argv)
     qtest_start(args);
     ret = g_test_run();

-    qtest_end();
+    qtest_quit(global_qtest);
     g_free(args);

     return ret;
diff --git a/tests/test-x86-cpuid-compat.c b/tests/test-x86-cpuid-compat.c
index 58a2dd9fe8..4e88f9e4e2 100644
--- a/tests/test-x86-cpuid-compat.c
+++ b/tests/test-x86-cpuid-compat.c
@@ -64,7 +64,7 @@ static void test_cpuid_prop(const void *data)
     value = qobject_to_qnum(qom_get(path, args->property));
     g_assert(qnum_get_try_int(value, &val));
     g_assert_cmpint(val, ==, args->expected_value);
-    qtest_end();
+    qtest_quit(global_qtest);

     QDECREF(value);
     g_free(path);
@@ -137,7 +137,7 @@ static void test_feature_flag(const void *data)
     filtered = qobject_to_qlist(qom_get(path, "filtered-features"));
     value = get_feature_word(present, args->in_eax, args->in_ecx, args->reg);
     value |= get_feature_word(filtered, args->in_eax, args->in_ecx, args->reg);
-    qtest_end();
+    qtest_quit(global_qtest);

     g_assert(!!(value & (1U << args->bitnr)) == args->expected_value);

@@ -195,7 +195,7 @@ static void test_plus_minus_subprocess(void)
     g_assert_true(qom_get_bool(path, "sse4-2"));
     g_assert_true(qom_get_bool(path, "sse4.2"));

-    qtest_end();
+    qtest_quit(global_qtest);
     g_free(path);
 }

diff --git a/tests/tpci200-test.c b/tests/tpci200-test.c
index 0321ec27ec..3b756d10f0 100644
--- a/tests/tpci200-test.c
+++ b/tests/tpci200-test.c
@@ -25,7 +25,7 @@ int main(int argc, char **argv)
     qtest_start("-device tpci200");
     ret = g_test_run();

-    qtest_end();
+    qtest_quit(global_qtest);

     return ret;
 }
diff --git a/tests/usb-hcd-ehci-test.c b/tests/usb-hcd-ehci-test.c
index 55d4743a2a..16d74512cc 100644
--- a/tests/usb-hcd-ehci-test.c
+++ b/tests/usb-hcd-ehci-test.c
@@ -172,7 +172,7 @@ int main(int argc, char **argv)
     ret = g_test_run();
     test_deinit();

-    qtest_end();
+    qtest_quit(global_qtest);

     return ret;
 }
diff --git a/tests/usb-hcd-ohci-test.c b/tests/usb-hcd-ohci-test.c
index 4758813d78..aa38e44d03 100644
--- a/tests/usb-hcd-ohci-test.c
+++ b/tests/usb-hcd-ohci-test.c
@@ -33,7 +33,7 @@ int main(int argc, char **argv)

     qtest_start("-device pci-ohci,id=ohci");
     ret = g_test_run();
-    qtest_end();
+    qtest_quit(global_qtest);

     return ret;
 }
diff --git a/tests/usb-hcd-xhci-test.c b/tests/usb-hcd-xhci-test.c
index c05a339894..e45509741c 100644
--- a/tests/usb-hcd-xhci-test.c
+++ b/tests/usb-hcd-xhci-test.c
@@ -83,7 +83,7 @@ int main(int argc, char **argv)
     qtest_start("-device nec-usb-xhci,id=xhci"
                 " -drive id=drive0,if=none,file=null-co://,format=raw");
     ret = g_test_run();
-    qtest_end();
+    qtest_quit(global_qtest);

     return ret;
 }
diff --git a/tests/virtio-balloon-test.c b/tests/virtio-balloon-test.c
index 0d0046bf25..34ad718601 100644
--- a/tests/virtio-balloon-test.c
+++ b/tests/virtio-balloon-test.c
@@ -25,7 +25,7 @@ int main(int argc, char **argv)
     qtest_start("-device virtio-balloon-pci");
     ret = g_test_run();

-    qtest_end();
+    qtest_quit(global_qtest);

     return ret;
 }
diff --git a/tests/virtio-blk-test.c b/tests/virtio-blk-test.c
index 0cf33b6810..ce316e753e 100644
--- a/tests/virtio-blk-test.c
+++ b/tests/virtio-blk-test.c
@@ -102,7 +102,7 @@ static void arm_test_start(void)

 static void test_end(void)
 {
-    qtest_end();
+    qtest_quit(global_qtest);
 }

 static QVirtioPCIDevice *virtio_blk_pci_init(QPCIBus *bus, int slot)
diff --git a/tests/virtio-console-test.c b/tests/virtio-console-test.c
index 1c3de072f4..5c036ce02a 100644
--- a/tests/virtio-console-test.c
+++ b/tests/virtio-console-test.c
@@ -15,14 +15,14 @@ static void console_pci_nop(void)
 {
     qtest_start("-device virtio-serial-pci,id=vser0 "
                 "-device virtconsole,bus=vser0.0");
-    qtest_end();
+    qtest_quit(global_qtest);
 }

 static void serialport_pci_nop(void)
 {
     qtest_start("-device virtio-serial-pci,id=vser0 "
                 "-device virtserialport,bus=vser0.0");
-    qtest_end();
+    qtest_quit(global_qtest);
 }

 int main(int argc, char **argv)
diff --git a/tests/virtio-net-test.c b/tests/virtio-net-test.c
index acecef0a3e..e1085c7e77 100644
--- a/tests/virtio-net-test.c
+++ b/tests/virtio-net-test.c
@@ -30,7 +30,7 @@

 static void test_end(void)
 {
-    qtest_end();
+    qtest_quit(global_qtest);
 }

 #ifndef _WIN32
diff --git a/tests/virtio-rng-test.c b/tests/virtio-rng-test.c
index 0e21125cb4..a3a8bde491 100644
--- a/tests/virtio-rng-test.c
+++ b/tests/virtio-rng-test.c
@@ -41,7 +41,7 @@ int main(int argc, char **argv)
     qtest_start("-device virtio-rng-pci");
     ret = g_test_run();

-    qtest_end();
+    qtest_quit(global_qtest);

     return ret;
 }
diff --git a/tests/virtio-serial-test.c b/tests/virtio-serial-test.c
index b14d943ada..d0bbc2a8c9 100644
--- a/tests/virtio-serial-test.c
+++ b/tests/virtio-serial-test.c
@@ -52,7 +52,7 @@ int main(int argc, char **argv)
     qtest_start("-device virtio-serial-pci");
     ret = g_test_run();

-    qtest_end();
+    qtest_quit(global_qtest);

     return ret;
 }
diff --git a/tests/vmxnet3-test.c b/tests/vmxnet3-test.c
index 159c0ad728..631630b4d0 100644
--- a/tests/vmxnet3-test.c
+++ b/tests/vmxnet3-test.c
@@ -25,7 +25,7 @@ int main(int argc, char **argv)
     qtest_start("-device vmxnet3");
     ret = g_test_run();

-    qtest_end();
+    qtest_quit(global_qtest);

     return ret;
 }
-- 
2.13.5

^ permalink raw reply related	[flat|nested] 75+ messages in thread

* [Qemu-devel] [PATCH v7 27/38] libqtest: Swap order of qtest_init() and qtest_start()
  2017-09-11 17:19 [Qemu-devel] [PATCH v7 00/38] Preliminary libqtest cleanups Eric Blake
                   ` (25 preceding siblings ...)
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 26/38] libqtest: Merge qtest_end() into qtest_quit() Eric Blake
@ 2017-09-11 17:20 ` Eric Blake
  2017-09-12  9:57   ` Thomas Huth
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 28/38] libqtest: Add qtest_[v]startf() Eric Blake
                   ` (10 subsequent siblings)
  37 siblings, 1 reply; 75+ messages in thread
From: Eric Blake @ 2017-09-11 17:20 UTC (permalink / raw)
  To: qemu-devel; +Cc: armbru, pbonzini, thuth

We already have another qtest_init() in the tree (namely, as part
of the device initialization of the qtest device at the top level
qtest.c), with a different signature; having two different
qtest_init() is confusing, so an upcoming patch will consolidate
all testsuite callers onto a unified spelling.  But the
consolidation is easier if qtest_start() is further down the call
chain, rather than an intermediate wrapper, so swap the call chain
around.  This includes renaming qtest_init_without_qmp_handshake()
into qtest_start_without_qmp_handshake(), as it remains the lowest
point in the call stack.

Note that qtest_init() now asserts that global_qtest was not set
on entry, and clears it on exit, to preserve the behavior of
existing tests that assert the same (and also proving that we
fixed all tests that had parallel connections); but later patches
will eventually simplify things by getting rid of qtest_init()
and global_qtest altogether.

Signed-off-by: Eric Blake <eblake@redhat.com>
---
 tests/libqtest.h | 52 ++++++++++++++++++++++++++++------------------------
 tests/libqtest.c |  9 +++++----
 tests/qmp-test.c |  2 +-
 3 files changed, 34 insertions(+), 29 deletions(-)

diff --git a/tests/libqtest.h b/tests/libqtest.h
index d976a542b8..9ef0fbefea 100644
--- a/tests/libqtest.h
+++ b/tests/libqtest.h
@@ -24,20 +24,39 @@ typedef struct QTestState QTestState;
 extern QTestState *global_qtest;

 /**
+ * qtest_start_without_qmp_handshake:
+ * @extra_args: other arguments to pass to QEMU.
+ *
+ * Returns: #QTestState instance.  Does not affect #global_qtest.
+ */
+QTestState *qtest_start_without_qmp_handshake(const char *extra_args);
+
+/**
+ * qtest_start:
+ * @args: other arguments to pass to QEMU
+ *
+ * Start QEMU and assign the resulting #QTestState to #global_qtest.
+ * The global variable is used by "shortcut" functions documented below.
+ *
+ * Returns: #QTestState instance.
+ */
+QTestState *qtest_start(const char *args);
+
+/**
  * qtest_init:
  * @extra_args: other arguments to pass to QEMU.
  *
- * Returns: #QTestState instance.
+ * Returns: #QTestState instance.  Does not affect #global_qtest.
  */
-QTestState *qtest_init(const char *extra_args);
+static inline QTestState *qtest_init(const char *extra_args)
+{
+    QTestState *s;

-/**
- * qtest_init_without_qmp_handshake:
- * @extra_args: other arguments to pass to QEMU.
- *
- * Returns: #QTestState instance.
- */
-QTestState *qtest_init_without_qmp_handshake(const char *extra_args);
+    assert(!global_qtest);
+    s = qtest_start(extra_args);
+    global_qtest = NULL;
+    return s;
+}

 /**
  * qtest_quit:
@@ -508,21 +527,6 @@ void qtest_add_data_func_full(const char *str, void *data,
 void qtest_add_abrt_handler(GHookFunc fn, const void *data);

 /**
- * qtest_start:
- * @args: other arguments to pass to QEMU
- *
- * Start QEMU and assign the resulting #QTestState to a global variable.
- * The global variable is used by "shortcut" functions documented below.
- *
- * Returns: #QTestState instance.
- */
-static inline QTestState *qtest_start(const char *args)
-{
-    global_qtest = qtest_init(args);
-    return global_qtest;
-}
-
-/**
  * qmp:
  * @fmt...: QMP message to send to qemu
  *
diff --git a/tests/libqtest.c b/tests/libqtest.c
index 94c157ce02..e8c2e11817 100644
--- a/tests/libqtest.c
+++ b/tests/libqtest.c
@@ -152,7 +152,7 @@ void qtest_add_abrt_handler(GHookFunc fn, const void *data)
     g_hook_prepend(&abrt_hooks, hook);
 }

-QTestState *qtest_init_without_qmp_handshake(const char *extra_args)
+QTestState *qtest_start_without_qmp_handshake(const char *extra_args)
 {
     QTestState *s;
     int sock, qmpsock, i;
@@ -233,15 +233,16 @@ QTestState *qtest_init_without_qmp_handshake(const char *extra_args)
     return s;
 }

-QTestState *qtest_init(const char *extra_args)
+QTestState *qtest_start(const char *extra_args)
 {
-    QTestState *s = qtest_init_without_qmp_handshake(extra_args);
+    QTestState *s = qtest_start_without_qmp_handshake(extra_args);

     /* Read the QMP greeting and then do the handshake */
     qtest_qmp_discard_response(s, "");
     qtest_qmp_discard_response(s, "{ 'execute': 'qmp_capabilities' }");

-    return s;
+    assert(!global_qtest);
+    return global_qtest = s;
 }

 void qtest_quit(QTestState *s)
diff --git a/tests/qmp-test.c b/tests/qmp-test.c
index a31c0f7de1..8985e7f9ec 100644
--- a/tests/qmp-test.c
+++ b/tests/qmp-test.c
@@ -77,7 +77,7 @@ static void test_qmp_protocol(void)
     QList *capabilities;
     QTestState *qts;

-    qts = qtest_init_without_qmp_handshake(common_args);
+    qts = qtest_start_without_qmp_handshake(common_args);

     /* Test greeting */
     resp = qtest_qmp_receive(qts);
-- 
2.13.5

^ permalink raw reply related	[flat|nested] 75+ messages in thread

* [Qemu-devel] [PATCH v7 28/38] libqtest: Add qtest_[v]startf()
  2017-09-11 17:19 [Qemu-devel] [PATCH v7 00/38] Preliminary libqtest cleanups Eric Blake
                   ` (26 preceding siblings ...)
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 27/38] libqtest: Swap order of qtest_init() and qtest_start() Eric Blake
@ 2017-09-11 17:20 ` Eric Blake
  2017-09-12 10:14   ` Thomas Huth
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 29/38] libqtest: Merge qtest_init() into qtest_start() Eric Blake
                   ` (9 subsequent siblings)
  37 siblings, 1 reply; 75+ messages in thread
From: Eric Blake @ 2017-09-11 17:20 UTC (permalink / raw)
  To: qemu-devel
  Cc: armbru, pbonzini, thuth, Michael S. Tsirkin, Igor Mammedov,
	John Snow, Andreas Färber, Dr. David Alan Gilbert,
	Stefan Hajnoczi, Ben Warren, open list:IDE

We have several callers that were formatting the argument strings
themselves; consolidate this effort by adding new convenience
functions directly in libqtest, and update all call-sites that
can benefit from it.

Note that the new functions qtest_startf() and qtest_vstartf()
behave more like qtest_init() (the caller must assign global_qtest
after the fact, rather than getting it implicitly set).  This helps
us prepare for future patches that get rid of the global variable,
by explicitly highlighting which tests still depend on it now.

In ide-test, we now have to pass a space to keep -Wformat from
complaining about a zero-length format string (one of gcc's more
pointless warnings, oh well).

Signed-off-by: Eric Blake <eblake@redhat.com>
---
 tests/libqtest.h               | 25 ++++++++++++++++
 tests/libqtest.c               | 21 +++++++++++++
 tests/bios-tables-test.c       | 17 ++++-------
 tests/boot-order-test.c        | 11 +++----
 tests/boot-serial-test.c       | 12 +++-----
 tests/e1000-test.c             | 10 +------
 tests/e1000e-test.c            | 12 ++------
 tests/eepro100-test.c          | 11 +------
 tests/endianness-test.c        | 33 ++++++++-------------
 tests/i440fx-test.c            |  6 +---
 tests/ide-test.c               | 10 ++-----
 tests/ipmi-bt-test.c           | 11 +++----
 tests/libqos/libqos.c          |  5 +---
 tests/m25p80-test.c            |  9 ++----
 tests/numa-test.c              | 67 ++++++++++++++++--------------------------
 tests/pc-cpu-test.c            | 20 ++++---------
 tests/pnv-xscom-test.c         | 16 +++-------
 tests/prom-env-test.c          | 13 ++++----
 tests/pxe-test.c               | 14 ++++-----
 tests/qom-test.c               |  5 +---
 tests/tco-test.c               | 10 +++----
 tests/test-filter-mirror.c     | 14 ++++-----
 tests/test-filter-redirector.c | 56 ++++++++++++++++-------------------
 tests/test-hmp.c               |  5 +---
 tests/test-netfilter.c         |  7 ++---
 tests/vhost-user-test.c        | 51 ++++++++++----------------------
 tests/virtio-blk-test.c        |  5 +---
 tests/vmgenid-test.c           | 29 +++++-------------
 28 files changed, 199 insertions(+), 306 deletions(-)

diff --git a/tests/libqtest.h b/tests/libqtest.h
index 9ef0fbefea..817e3a5580 100644
--- a/tests/libqtest.h
+++ b/tests/libqtest.h
@@ -43,6 +43,31 @@ QTestState *qtest_start_without_qmp_handshake(const char *extra_args);
 QTestState *qtest_start(const char *args);

 /**
+ * qtest_startf:
+ * @fmt...: Format for creating other arguments to pass to QEMU, formatted
+ * like sprintf().
+ *
+ * Start QEMU and return the resulting #QTestState (but unlike qtest_start(),
+ * #global_qtest is left at NULL).
+ *
+ * Returns: #QTestState instance.
+ */
+QTestState *qtest_startf(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
+
+/**
+ * qtest_vstartf:
+ * @fmt: Format for creating other arguments to pass to QEMU, formatted
+ * like vsprintf().
+ * @ap: Format arguments.
+ *
+ * Start QEMU and return the resulting #QTestState (but unlike qtest_start(),
+ * #global_qtest is left at NULL).
+ *
+ * Returns: #QTestState instance.
+ */
+QTestState *qtest_vstartf(const char *fmt, va_list ap) GCC_FMT_ATTR(1, 0);
+
+/**
  * qtest_init:
  * @extra_args: other arguments to pass to QEMU.
  *
diff --git a/tests/libqtest.c b/tests/libqtest.c
index e8c2e11817..b535d7768f 100644
--- a/tests/libqtest.c
+++ b/tests/libqtest.c
@@ -245,6 +245,27 @@ QTestState *qtest_start(const char *extra_args)
     return global_qtest = s;
 }

+QTestState *qtest_vstartf(const char *fmt, va_list ap)
+{
+    char *args = g_strdup_vprintf(fmt, ap);
+    QTestState *s;
+
+    s = qtest_start(args);
+    global_qtest = NULL;
+    return s;
+}
+
+QTestState *qtest_startf(const char *fmt, ...)
+{
+    va_list ap;
+    QTestState *s;
+
+    va_start(ap, fmt);
+    s = qtest_vstartf(fmt, ap);
+    va_end(ap);
+    return s;
+}
+
 void qtest_quit(QTestState *s)
 {
     if (global_qtest) {
diff --git a/tests/bios-tables-test.c b/tests/bios-tables-test.c
index 4272ea1496..c17cd8e1a3 100644
--- a/tests/bios-tables-test.c
+++ b/tests/bios-tables-test.c
@@ -623,17 +623,13 @@ static void test_smbios_structs(test_data *data)

 static void test_acpi_one(const char *params, test_data *data)
 {
-    char *args;
-
     /* Disable kernel irqchip to be able to override apic irq0. */
-    args = g_strdup_printf("-machine %s,accel=%s,kernel-irqchip=off "
-                           "-net none -display none %s "
-                           "-drive id=hd0,if=none,file=%s,format=raw "
-                           "-device ide-hd,drive=hd0 ",
-                           data->machine, "kvm:tcg",
-                           params ? params : "", disk);
-
-    data->qts = qtest_init(args);
+    data->qts = qtest_startf("-machine %s,accel=%s,kernel-irqchip=off "
+                             "-net none -display none %s "
+                             "-drive id=hd0,if=none,file=%s,format=raw "
+                             "-device ide-hd,drive=hd0 ",
+                             data->machine, "kvm:tcg",
+                             params ? params : "", disk);

     boot_sector_test(data->qts);

@@ -658,7 +654,6 @@ static void test_acpi_one(const char *params, test_data *data)

     assert(!global_qtest);
     qtest_quit(data->qts);
-    g_free(args);
 }

 static uint8_t base_required_struct_types[] = {
diff --git a/tests/boot-order-test.c b/tests/boot-order-test.c
index 5fc2ca8e9e..e70f5dedba 100644
--- a/tests/boot-order-test.c
+++ b/tests/boot-order-test.c
@@ -28,14 +28,12 @@ static void test_a_boot_order(const char *machine,
                               uint64_t expected_boot,
                               uint64_t expected_reboot)
 {
-    char *args;
     uint64_t actual;

-    args = g_strdup_printf("-nodefaults%s%s %s",
-                           machine ? " -M " : "",
-                           machine ?: "",
-                           test_args);
-    qtest_start(args);
+    global_qtest = qtest_startf("-nodefaults%s%s %s",
+                                machine ? " -M " : "",
+                                machine ?: "",
+                                test_args);
     actual = read_boot_order();
     g_assert_cmphex(actual, ==, expected_boot);
     qmp_discard_response("{ 'execute': 'system_reset' }");
@@ -47,7 +45,6 @@ static void test_a_boot_order(const char *machine,
     actual = read_boot_order();
     g_assert_cmphex(actual, ==, expected_reboot);
     qtest_quit(global_qtest);
-    g_free(args);
 }

 static void test_boot_orders(const char *machine,
diff --git a/tests/boot-serial-test.c b/tests/boot-serial-test.c
index b95c5e74ea..c935d69824 100644
--- a/tests/boot-serial-test.c
+++ b/tests/boot-serial-test.c
@@ -71,7 +71,6 @@ done:
 static void test_machine(const void *data)
 {
     const testdef_t *test = data;
-    char *args;
     char tmpname[] = "/tmp/qtest-boot-serial-XXXXXX";
     int fd;

@@ -82,18 +81,15 @@ static void test_machine(const void *data)
      * Make sure that this test uses tcg if available: It is used as a
      * fast-enough smoketest for that.
      */
-    args = g_strdup_printf("-M %s,accel=tcg:kvm "
-                           "-chardev file,id=serial0,path=%s "
-                           "-no-shutdown -serial chardev:serial0 %s",
-                           test->machine, tmpname, test->extra);
-
-    qtest_start(args);
+    global_qtest = qtest_startf("-M %s,accel=tcg:kvm "
+                                "-chardev file,id=serial0,path=%s "
+                                "-no-shutdown -serial chardev:serial0 %s",
+                                test->machine, tmpname, test->extra);
     unlink(tmpname);

     check_guest_output(test, fd);
     qtest_quit(global_qtest);

-    g_free(args);
     close(fd);
 }

diff --git a/tests/e1000-test.c b/tests/e1000-test.c
index 0c5fcdcc44..12bc526ad6 100644
--- a/tests/e1000-test.c
+++ b/tests/e1000-test.c
@@ -14,16 +14,8 @@
 static void test_device(gconstpointer data)
 {
     const char *model = data;
-    QTestState *s;
-    char *args;

-    args = g_strdup_printf("-device %s", model);
-    s = qtest_start(args);
-
-    if (s) {
-        qtest_quit(s);
-    }
-    g_free(args);
+    qtest_quit(qtest_startf("-device %s", model));
 }

 static const char *models[] = {
diff --git a/tests/e1000e-test.c b/tests/e1000e-test.c
index 98f821fa7b..323aabb454 100644
--- a/tests/e1000e-test.c
+++ b/tests/e1000e-test.c
@@ -380,17 +380,11 @@ static void e1000e_device_clear(QPCIBus *bus, e1000e_device *d)

 static void data_test_init(e1000e_device *d)
 {
-    char *cmdline;
-
     int ret = socketpair(PF_UNIX, SOCK_STREAM, 0, test_sockets);
     g_assert_cmpint(ret, != , -1);

-    cmdline = g_strdup_printf("-netdev socket,fd=%d,id=hs0 "
-                              "-device e1000e,netdev=hs0", test_sockets[1]);
-    g_assert_nonnull(cmdline);
-
-    qtest_start(cmdline);
-    g_free(cmdline);
+    global_qtest = qtest_startf("-netdev socket,fd=%d,id=hs0 "
+                                "-device e1000e,netdev=hs0", test_sockets[1]);

     test_alloc = pc_alloc_init(global_qtest);
     g_assert_nonnull(test_alloc);
@@ -458,7 +452,7 @@ static void test_e1000e_hotplug(gconstpointer data)
 {
     static const uint8_t slot = 0x06;

-    qtest_start("-device e1000e");
+    global_qtest = qtest_start("-device e1000e");

     qpci_plug_device_test(global_qtest, "e1000e", "e1000e_net", slot, NULL);
     qpci_unplug_device_test(global_qtest, "e1000e_net", slot);
diff --git a/tests/eepro100-test.c b/tests/eepro100-test.c
index bdc8a67d57..fc9ea84d66 100644
--- a/tests/eepro100-test.c
+++ b/tests/eepro100-test.c
@@ -13,18 +13,9 @@
 static void test_device(gconstpointer data)
 {
     const char *model = data;
-    QTestState *s;
-    char *args;
-
-    args = g_strdup_printf("-device %s", model);
-    s = qtest_start(args);

     /* Tests only initialization so far. TODO: Implement functional tests */
-
-    if (s) {
-        qtest_quit(s);
-    }
-    g_free(args);
+    qtest_quit(qtest_startf("-device %s", model));
 }

 static const char *models[] = {
diff --git a/tests/endianness-test.c b/tests/endianness-test.c
index ed0bf52019..546e0969e4 100644
--- a/tests/endianness-test.c
+++ b/tests/endianness-test.c
@@ -114,13 +114,11 @@ static void isa_outl(const TestCase *test, uint16_t addr, uint32_t value)
 static void test_endianness(gconstpointer data)
 {
     const TestCase *test = data;
-    char *args;

-    args = g_strdup_printf("-M %s%s%s -device pc-testdev",
-                           test->machine,
-                           test->superio ? " -device " : "",
-                           test->superio ?: "");
-    qtest_start(args);
+    global_qtest = qtest_startf("-M %s%s%s -device pc-testdev",
+                                test->machine,
+                                test->superio ? " -device " : "",
+                                test->superio ?: "");
     isa_outl(test, 0xe0, 0x87654321);
     g_assert_cmphex(isa_inl(test, 0xe0), ==, 0x87654321);
     g_assert_cmphex(isa_inw(test, 0xe2), ==, 0x8765);
@@ -183,19 +181,16 @@ static void test_endianness(gconstpointer data)
     g_assert_cmphex(isa_inb(test, 0xe1), ==, 0x43);
     g_assert_cmphex(isa_inb(test, 0xe0), ==, 0x21);
     qtest_quit(global_qtest);
-    g_free(args);
 }

 static void test_endianness_split(gconstpointer data)
 {
     const TestCase *test = data;
-    char *args;

-    args = g_strdup_printf("-M %s%s%s -device pc-testdev",
-                           test->machine,
-                           test->superio ? " -device " : "",
-                           test->superio ?: "");
-    qtest_start(args);
+    global_qtest = qtest_startf("-M %s%s%s -device pc-testdev",
+                                test->machine,
+                                test->superio ? " -device " : "",
+                                test->superio ?: "");
     isa_outl(test, 0xe8, 0x87654321);
     g_assert_cmphex(isa_inl(test, 0xe0), ==, 0x87654321);
     g_assert_cmphex(isa_inw(test, 0xe2), ==, 0x8765);
@@ -230,19 +225,16 @@ static void test_endianness_split(gconstpointer data)
     g_assert_cmphex(isa_inw(test, 0xe2), ==, 0x8765);
     g_assert_cmphex(isa_inw(test, 0xe0), ==, 0x4321);
     qtest_quit(global_qtest);
-    g_free(args);
 }

 static void test_endianness_combine(gconstpointer data)
 {
     const TestCase *test = data;
-    char *args;

-    args = g_strdup_printf("-M %s%s%s -device pc-testdev",
-                           test->machine,
-                           test->superio ? " -device " : "",
-                           test->superio ?: "");
-    qtest_start(args);
+    global_qtest = qtest_startf("-M %s%s%s -device pc-testdev",
+                                test->machine,
+                                test->superio ? " -device " : "",
+                                test->superio ?: "");
     isa_outl(test, 0xe0, 0x87654321);
     g_assert_cmphex(isa_inl(test, 0xe8), ==, 0x87654321);
     g_assert_cmphex(isa_inw(test, 0xea), ==, 0x8765);
@@ -277,7 +269,6 @@ static void test_endianness_combine(gconstpointer data)
     g_assert_cmphex(isa_inw(test, 0xea), ==, 0x8765);
     g_assert_cmphex(isa_inw(test, 0xe8), ==, 0x4321);
     qtest_quit(global_qtest);
-    g_free(args);
 }

 int main(int argc, char **argv)
diff --git a/tests/i440fx-test.c b/tests/i440fx-test.c
index c847e4dbbb..c282ded4ca 100644
--- a/tests/i440fx-test.c
+++ b/tests/i440fx-test.c
@@ -33,11 +33,7 @@ typedef struct FirmwareTestFixture {

 static QPCIBus *test_start_get_bus(const TestData *s)
 {
-    char *cmdline;
-
-    cmdline = g_strdup_printf("-smp %d", s->num_cpus);
-    qtest_start(cmdline);
-    g_free(cmdline);
+    global_qtest = qtest_startf("-smp %d", s->num_cpus);
     return qpci_init_pc(global_qtest, NULL);
 }

diff --git a/tests/ide-test.c b/tests/ide-test.c
index 16b73e0101..e1650c0132 100644
--- a/tests/ide-test.c
+++ b/tests/ide-test.c
@@ -115,19 +115,15 @@ static QGuestAllocator *guest_malloc;
 static char tmp_path[] = "/tmp/qtest.XXXXXX";
 static char debug_path[] = "/tmp/qtest-blkdebug.XXXXXX";

-static void ide_test_start(const char *cmdline_fmt, ...)
+static void GCC_FMT_ATTR(1, 2) ide_test_start(const char *cmdline_fmt, ...)
 {
     va_list ap;
-    char *cmdline;

     va_start(ap, cmdline_fmt);
-    cmdline = g_strdup_vprintf(cmdline_fmt, ap);
+    global_qtest = qtest_vstartf(cmdline_fmt, ap);
     va_end(ap);

-    qtest_start(cmdline);
     guest_malloc = pc_alloc_init(global_qtest);
-
-    g_free(cmdline);
 }

 static void ide_test_quit(void)
@@ -675,7 +671,7 @@ static void test_flush_nodev(void)
     QPCIDevice *dev;
     QPCIBar bmdma_bar, ide_bar;

-    ide_test_start("");
+    ide_test_start(" ");

     dev = get_pci_device(&bmdma_bar, &ide_bar);

diff --git a/tests/ipmi-bt-test.c b/tests/ipmi-bt-test.c
index 7e21a9bbcb..8be18e3f42 100644
--- a/tests/ipmi-bt-test.c
+++ b/tests/ipmi-bt-test.c
@@ -401,7 +401,6 @@ static void open_socket(void)
 int main(int argc, char **argv)
 {
     const char *arch = qtest_get_arch();
-    char *cmdline;
     int ret;

     /* Check architecture */
@@ -415,12 +414,10 @@ int main(int argc, char **argv)
     /* Run the tests */
     g_test_init(&argc, &argv, NULL);

-    cmdline = g_strdup_printf(
-          " -chardev socket,id=ipmi0,host=localhost,port=%d,reconnect=10"
-          " -device ipmi-bmc-extern,chardev=ipmi0,id=bmc0"
-          " -device isa-ipmi-bt,bmc=bmc0", emu_port);
-    qtest_start(cmdline);
-    g_free(cmdline);
+    global_qtest = qtest_startf(
+        " -chardev socket,id=ipmi0,host=localhost,port=%d,reconnect=10"
+        " -device ipmi-bmc-extern,chardev=ipmi0,id=bmc0"
+        " -device isa-ipmi-bt,bmc=bmc0", emu_port);
     qtest_irq_intercept_in(global_qtest, "ioapic");
     qtest_add_func("/ipmi/extern/connect", test_connect);
     qtest_add_func("/ipmi/extern/bt_base", test_bt_base);
diff --git a/tests/libqos/libqos.c b/tests/libqos/libqos.c
index 3ff61246a4..559fae36bd 100644
--- a/tests/libqos/libqos.c
+++ b/tests/libqos/libqos.c
@@ -15,19 +15,16 @@
  */
 QOSState *qtest_vboot(QOSOps *ops, const char *cmdline_fmt, va_list ap)
 {
-    char *cmdline;

     QOSState *qs = g_new0(QOSState, 1);

-    cmdline = g_strdup_vprintf(cmdline_fmt, ap);
-    qs->qts = qtest_init(cmdline);
+    qs->qts = qtest_vstartf(cmdline_fmt, ap);
     qs->ops = ops;
     if (ops) {
         qs->alloc = ops->init_allocator(qs->qts, ALLOC_NO_FLAGS);
         qs->pcibus = ops->qpci_init(qs->qts, qs->alloc);
     }

-    g_free(cmdline);
     return qs;
 }

diff --git a/tests/m25p80-test.c b/tests/m25p80-test.c
index 244aa33dd9..c276e738e9 100644
--- a/tests/m25p80-test.c
+++ b/tests/m25p80-test.c
@@ -354,7 +354,6 @@ int main(int argc, char **argv)
 {
     int ret;
     int fd;
-    char *args;

     g_test_init(&argc, &argv, NULL);

@@ -364,10 +363,9 @@ int main(int argc, char **argv)
     g_assert(ret == 0);
     close(fd);

-    args = g_strdup_printf("-m 256 -machine palmetto-bmc "
-                           "-drive file=%s,format=raw,if=mtd",
-                           tmp_path);
-    qtest_start(args);
+    global_qtest = qtest_startf("-m 256 -machine palmetto-bmc "
+                                "-drive file=%s,format=raw,if=mtd",
+                                tmp_path);

     qtest_add_func("/m25p80/read_jedec", test_read_jedec);
     qtest_add_func("/m25p80/erase_sector", test_erase_sector);
@@ -380,6 +378,5 @@ int main(int argc, char **argv)

     qtest_quit(global_qtest);
     unlink(tmp_path);
-    g_free(args);
     return ret;
 }
diff --git a/tests/numa-test.c b/tests/numa-test.c
index fa21d26935..e2f6c68be8 100644
--- a/tests/numa-test.c
+++ b/tests/numa-test.c
@@ -12,20 +12,14 @@
 #include "qemu/osdep.h"
 #include "libqtest.h"

-static char *make_cli(const char *generic_cli, const char *test_cli)
-{
-    return g_strdup_printf("%s %s", generic_cli ? generic_cli : "", test_cli);
-}
-
 static void test_mon_explicit(const void *data)
 {
     char *s;
-    char *cli;
+    const char *args = data;

-    cli = make_cli(data, "-smp 8 "
-                   "-numa node,nodeid=0,cpus=0-3 "
-                   "-numa node,nodeid=1,cpus=4-7 ");
-    qtest_start(cli);
+    global_qtest = qtest_startf("%s -smp 8 "
+                                "-numa node,nodeid=0,cpus=0-3 "
+                                "-numa node,nodeid=1,cpus=4-7 ", args);

     s = hmp("info numa");
     g_assert(strstr(s, "node 0 cpus: 0 1 2 3"));
@@ -33,16 +27,14 @@ static void test_mon_explicit(const void *data)
     g_free(s);

     qtest_quit(global_qtest);
-    g_free(cli);
 }

 static void test_mon_default(const void *data)
 {
     char *s;
-    char *cli;
+    const char *args = data;

-    cli = make_cli(data, "-smp 8 -numa node -numa node");
-    qtest_start(cli);
+    global_qtest = qtest_startf("%s -smp 8 -numa node -numa node", args);

     s = hmp("info numa");
     g_assert(strstr(s, "node 0 cpus: 0 2 4 6"));
@@ -50,18 +42,16 @@ static void test_mon_default(const void *data)
     g_free(s);

     qtest_quit(global_qtest);
-    g_free(cli);
 }

 static void test_mon_partial(const void *data)
 {
     char *s;
-    char *cli;
+    const char *args = data;

-    cli = make_cli(data, "-smp 8 "
-                   "-numa node,nodeid=0,cpus=0-1 "
-                   "-numa node,nodeid=1,cpus=4-5 ");
-    qtest_start(cli);
+    global_qtest = qtest_startf("%s -smp 8 "
+                                "-numa node,nodeid=0,cpus=0-1 "
+                                "-numa node,nodeid=1,cpus=4-5 ", args);

     s = hmp("info numa");
     g_assert(strstr(s, "node 0 cpus: 0 1 2 3 6 7"));
@@ -69,7 +59,6 @@ static void test_mon_partial(const void *data)
     g_free(s);

     qtest_quit(global_qtest);
-    g_free(cli);
 }

 static QList *get_cpus(QDict **resp)
@@ -82,13 +71,13 @@ static QList *get_cpus(QDict **resp)

 static void test_query_cpus(const void *data)
 {
-    char *cli;
+    const char *args = data;
     QDict *resp;
     QList *cpus;
     QObject *e;

-    cli = make_cli(data, "-smp 8 -numa node,cpus=0-3 -numa node,cpus=4-7");
-    qtest_start(cli);
+    global_qtest = qtest_startf("%s -smp 8 -numa node,cpus=0-3 "
+                                "-numa node,cpus=4-7", args);
     cpus = get_cpus(&resp);
     g_assert(cpus);

@@ -114,23 +103,22 @@ static void test_query_cpus(const void *data)

     QDECREF(resp);
     qtest_quit(global_qtest);
-    g_free(cli);
 }

 static void pc_numa_cpu(const void *data)
 {
-    char *cli;
+    const char *args = data;
     QDict *resp;
     QList *cpus;
     QObject *e;

-    cli = make_cli(data, "-cpu pentium -smp 8,sockets=2,cores=2,threads=2 "
+    global_qtest = qtest_startf(
+        "%s -cpu pentium -smp 8,sockets=2,cores=2,threads=2 "
         "-numa node,nodeid=0 -numa node,nodeid=1 "
         "-numa cpu,node-id=1,socket-id=0 "
         "-numa cpu,node-id=0,socket-id=1,core-id=0 "
         "-numa cpu,node-id=0,socket-id=1,core-id=1,thread-id=0 "
-        "-numa cpu,node-id=1,socket-id=1,core-id=1,thread-id=1");
-    qtest_start(cli);
+        "-numa cpu,node-id=1,socket-id=1,core-id=1,thread-id=1", args);
     cpus = get_cpus(&resp);
     g_assert(cpus);

@@ -167,23 +155,22 @@ static void pc_numa_cpu(const void *data)

     QDECREF(resp);
     qtest_quit(global_qtest);
-    g_free(cli);
 }

 static void spapr_numa_cpu(const void *data)
 {
-    char *cli;
+    const char *args = data;
     QDict *resp;
     QList *cpus;
     QObject *e;

-    cli = make_cli(data, "-smp 4,cores=4 "
+    global_qtest = qtest_startf(
+        "%s -smp 4,cores=4 "
         "-numa node,nodeid=0 -numa node,nodeid=1 "
         "-numa cpu,node-id=0,core-id=0 "
         "-numa cpu,node-id=0,core-id=1 "
         "-numa cpu,node-id=0,core-id=2 "
-        "-numa cpu,node-id=1,core-id=3");
-    qtest_start(cli);
+        "-numa cpu,node-id=1,core-id=3", args);
     cpus = get_cpus(&resp);
     g_assert(cpus);

@@ -212,21 +199,20 @@ static void spapr_numa_cpu(const void *data)

     QDECREF(resp);
     qtest_quit(global_qtest);
-    g_free(cli);
 }

 static void aarch64_numa_cpu(const void *data)
 {
-    char *cli;
+    const char *args = data;
     QDict *resp;
     QList *cpus;
     QObject *e;

-    cli = make_cli(data, "-smp 2 "
+    global_qtest = qtest_startf(
+        "%s -smp 2 "
         "-numa node,nodeid=0 -numa node,nodeid=1 "
         "-numa cpu,node-id=1,thread-id=0 "
-        "-numa cpu,node-id=0,thread-id=1");
-    qtest_start(cli);
+        "-numa cpu,node-id=0,thread-id=1", args);
     cpus = get_cpus(&resp);
     g_assert(cpus);

@@ -255,12 +241,11 @@ static void aarch64_numa_cpu(const void *data)

     QDECREF(resp);
     qtest_quit(global_qtest);
-    g_free(cli);
 }

 int main(int argc, char **argv)
 {
-    const char *args = NULL;
+    const char *args = "";
     const char *arch = qtest_get_arch();

     if (strcmp(arch, "aarch64") == 0) {
diff --git a/tests/pc-cpu-test.c b/tests/pc-cpu-test.c
index 3ff7115625..08510a44bf 100644
--- a/tests/pc-cpu-test.c
+++ b/tests/pc-cpu-test.c
@@ -26,15 +26,12 @@ typedef struct PCTestData PCTestData;
 static void test_pc_with_cpu_add(gconstpointer data)
 {
     const PCTestData *s = data;
-    char *args;
     QDict *response;
     unsigned int i;

-    args = g_strdup_printf("-machine %s -cpu %s "
-                           "-smp sockets=%u,cores=%u,threads=%u,maxcpus=%u",
-                           s->machine, s->cpu_model,
-                           s->sockets, s->cores, s->threads, s->maxcpus);
-    qtest_start(args);
+    global_qtest = qtest_startf(
+        "-machine %s -cpu %s -smp sockets=%u,cores=%u,threads=%u,maxcpus=%u",
+        s->machine, s->cpu_model, s->sockets, s->cores, s->threads, s->maxcpus);

     for (i = s->sockets * s->cores * s->threads; i < s->maxcpus; i++) {
         response = qmp("{ 'execute': 'cpu-add',"
@@ -45,20 +42,16 @@ static void test_pc_with_cpu_add(gconstpointer data)
     }

     qtest_quit(global_qtest);
-    g_free(args);
 }

 static void test_pc_without_cpu_add(gconstpointer data)
 {
     const PCTestData *s = data;
-    char *args;
     QDict *response;

-    args = g_strdup_printf("-machine %s -cpu %s "
-                           "-smp sockets=%u,cores=%u,threads=%u,maxcpus=%u",
-                           s->machine, s->cpu_model,
-                           s->sockets, s->cores, s->threads, s->maxcpus);
-    qtest_start(args);
+    global_qtest = qtest_startf(
+        "-machine %s -cpu %s -smp sockets=%u,cores=%u,threads=%u,maxcpus=%u",
+        s->machine, s->cpu_model, s->sockets, s->cores, s->threads, s->maxcpus);

     response = qmp("{ 'execute': 'cpu-add',"
                    "  'arguments': { 'id': %d } }",
@@ -68,7 +61,6 @@ static void test_pc_without_cpu_add(gconstpointer data)
     QDECREF(response);

     qtest_quit(global_qtest);
-    g_free(args);
 }

 static void test_data_free(gpointer data)
diff --git a/tests/pnv-xscom-test.c b/tests/pnv-xscom-test.c
index 5adc3fd3a9..89fa6282d3 100644
--- a/tests/pnv-xscom-test.c
+++ b/tests/pnv-xscom-test.c
@@ -81,16 +81,12 @@ static void test_xscom_cfam_id(const PnvChip *chip)

 static void test_cfam_id(const void *data)
 {
-    char *args;
     const PnvChip *chip = data;

-    args = g_strdup_printf("-M powernv,accel=tcg -cpu %s", chip->cpu_model);
-
-    qtest_start(args);
+    global_qtest = qtest_startf("-M powernv,accel=tcg -cpu %s",
+                                chip->cpu_model);
     test_xscom_cfam_id(chip);
     qtest_quit(global_qtest);
-
-    g_free(args);
 }

 #define PNV_XSCOM_EX_CORE_BASE(chip, i)                 \
@@ -109,16 +105,12 @@ static void test_xscom_core(const PnvChip *chip)

 static void test_core(const void *data)
 {
-    char *args;
     const PnvChip *chip = data;

-    args = g_strdup_printf("-M powernv,accel=tcg -cpu %s", chip->cpu_model);
-
-    qtest_start(args);
+    global_qtest = qtest_startf("-M powernv,accel=tcg -cpu %s",
+                                chip->cpu_model);
     test_xscom_core(chip);
     qtest_quit(global_qtest);
-
-    g_free(args);
 }

 static void add_test(const char *name, void (*test)(const void *data))
diff --git a/tests/prom-env-test.c b/tests/prom-env-test.c
index eac207b30e..0f8e6950fd 100644
--- a/tests/prom-env-test.c
+++ b/tests/prom-env-test.c
@@ -44,21 +44,18 @@ static void check_guest_memory(void)

 static void test_machine(const void *machine)
 {
-    char *args;
     const char *extra_args;

     /* The pseries firmware boots much faster without the default devices */
     extra_args = strcmp(machine, "pseries") == 0 ? "-nodefaults" : "";

-    args = g_strdup_printf("-M %s,accel=tcg %s -prom-env 'use-nvramrc?=true' "
-                           "-prom-env 'nvramrc=%x %x l!' ",
-                           (const char *)machine, extra_args, MAGIC, ADDRESS);
-
-    qtest_start(args);
+    global_qtest = qtest_startf("-M %s,accel=tcg %s "
+                                "-prom-env 'use-nvramrc?=true' "
+                                "-prom-env 'nvramrc=%x %x l!' ",
+                                (const char *)machine, extra_args,
+                                MAGIC, ADDRESS);
     check_guest_memory();
     qtest_quit(global_qtest);
-
-    g_free(args);
 }

 static void add_tests(const char *machines[])
diff --git a/tests/pxe-test.c b/tests/pxe-test.c
index e2d3853534..5f3516e87f 100644
--- a/tests/pxe-test.c
+++ b/tests/pxe-test.c
@@ -23,17 +23,13 @@ static char disk[] = "tests/pxe-test-disk-XXXXXX";

 static void test_pxe_one(const char *params, bool ipv6)
 {
-    char *args;
-
-    args = g_strdup_printf("-machine accel=kvm:tcg -nodefaults -boot order=n "
-                           "-netdev user,id=" NETNAME ",tftp=./,bootfile=%s,"
-                           "ipv4=%s,ipv6=%s %s", disk, ipv6 ? "off" : "on",
-                           ipv6 ? "on" : "off", params);
-
-    qtest_start(args);
+    global_qtest = qtest_startf(
+        "-machine accel=kvm:tcg -nodefaults -boot order=n "
+        "-netdev user,id=" NETNAME ",tftp=./,bootfile=%s,"
+        "ipv4=%s,ipv6=%s %s", disk, ipv6 ? "off" : "on",
+        ipv6 ? "on" : "off", params);
     boot_sector_test(global_qtest);
     qtest_quit(global_qtest);
-    g_free(args);
 }

 static void test_pxe_e1000(void)
diff --git a/tests/qom-test.c b/tests/qom-test.c
index 661a6edefd..8ed8d7b528 100644
--- a/tests/qom-test.c
+++ b/tests/qom-test.c
@@ -90,11 +90,9 @@ static void test_properties(const char *path, bool recurse)
 static void test_machine(gconstpointer data)
 {
     const char *machine = data;
-    char *args;
     QDict *response;

-    args = g_strdup_printf("-machine %s", machine);
-    qtest_start(args);
+    global_qtest = qtest_startf("-machine %s", machine);

     test_properties("/machine", true);

@@ -103,7 +101,6 @@ static void test_machine(gconstpointer data)
     QDECREF(response);

     qtest_quit(global_qtest);
-    g_free(args);
     g_free((void *)machine);
 }

diff --git a/tests/tco-test.c b/tests/tco-test.c
index 3d4b3db308..b84a50927a 100644
--- a/tests/tco-test.c
+++ b/tests/tco-test.c
@@ -55,14 +55,12 @@ static void test_end(TestData *d)
 static void test_init(TestData *d)
 {
     QTestState *qs;
-    char *s;

-    s = g_strdup_printf("-machine q35 %s %s",
-                        d->noreboot ? "" : "-global ICH9-LPC.noreboot=false",
-                        !d->args ? "" : d->args);
-    qs = qtest_start(s);
+    qs = qtest_startf("-machine q35 %s %s",
+                      d->noreboot ? "" : "-global ICH9-LPC.noreboot=false",
+                      !d->args ? "" : d->args);
+    global_qtest = qs;
     qtest_irq_intercept_in(qs, "ioapic");
-    g_free(s);

     d->bus = qpci_init_pc(qs, NULL);
     d->dev = qpci_device_find(d->bus, QPCI_DEVFN(0x1f, 0x00));
diff --git a/tests/test-filter-mirror.c b/tests/test-filter-mirror.c
index 85fb38976d..c590698991 100644
--- a/tests/test-filter-mirror.c
+++ b/tests/test-filter-mirror.c
@@ -18,7 +18,6 @@
 static void test_mirror(void)
 {
     int send_sock[2], recv_sock;
-    char *cmdline;
     uint32_t ret = 0, len = 0;
     char send_buf[] = "Hello! filter-mirror~";
     char sock_path[] = "filter-mirror.XXXXXX";
@@ -37,13 +36,12 @@ static void test_mirror(void)
     ret = mkstemp(sock_path);
     g_assert_cmpint(ret, !=, -1);

-    cmdline = g_strdup_printf("-netdev socket,id=qtest-bn0,fd=%d "
-                 "-device %s,netdev=qtest-bn0,id=qtest-e0 "
-                 "-chardev socket,id=mirror0,path=%s,server,nowait "
-                 "-object filter-mirror,id=qtest-f0,netdev=qtest-bn0,queue=tx,outdev=mirror0 "
-                 , send_sock[1], devstr, sock_path);
-    qtest_start(cmdline);
-    g_free(cmdline);
+    global_qtest = qtest_startf(
+        "-netdev socket,id=qtest-bn0,fd=%d "
+        "-device %s,netdev=qtest-bn0,id=qtest-e0 "
+        "-chardev socket,id=mirror0,path=%s,server,nowait "
+        "-object filter-mirror,id=qtest-f0,netdev=qtest-bn0,queue=tx,outdev=mirror0 "
+        , send_sock[1], devstr, sock_path);

     recv_sock = unix_connect(sock_path, NULL);
     g_assert_cmpint(recv_sock, !=, -1);
diff --git a/tests/test-filter-redirector.c b/tests/test-filter-redirector.c
index d3a0b1cdf4..bcb338e7c1 100644
--- a/tests/test-filter-redirector.c
+++ b/tests/test-filter-redirector.c
@@ -70,7 +70,6 @@ static const char *get_devstr(void)
 static void test_redirector_tx(void)
 {
     int backend_sock[2], recv_sock;
-    char *cmdline;
     uint32_t ret = 0, len = 0;
     char send_buf[] = "Hello!!";
     char sock_path0[] = "filter-redirector0.XXXXXX";
@@ -87,20 +86,19 @@ static void test_redirector_tx(void)
     ret = mkstemp(sock_path1);
     g_assert_cmpint(ret, !=, -1);

-    cmdline = g_strdup_printf("-netdev socket,id=qtest-bn0,fd=%d "
-                "-device %s,netdev=qtest-bn0,id=qtest-e0 "
-                "-chardev socket,id=redirector0,path=%s,server,nowait "
-                "-chardev socket,id=redirector1,path=%s,server,nowait "
-                "-chardev socket,id=redirector2,path=%s,nowait "
-                "-object filter-redirector,id=qtest-f0,netdev=qtest-bn0,"
-                "queue=tx,outdev=redirector0 "
-                "-object filter-redirector,id=qtest-f1,netdev=qtest-bn0,"
-                "queue=tx,indev=redirector2 "
-                "-object filter-redirector,id=qtest-f2,netdev=qtest-bn0,"
-                "queue=tx,outdev=redirector1 ", backend_sock[1], get_devstr(),
-                sock_path0, sock_path1, sock_path0);
-    qtest_start(cmdline);
-    g_free(cmdline);
+    global_qtest = qtest_startf(
+        "-netdev socket,id=qtest-bn0,fd=%d "
+        "-device %s,netdev=qtest-bn0,id=qtest-e0 "
+        "-chardev socket,id=redirector0,path=%s,server,nowait "
+        "-chardev socket,id=redirector1,path=%s,server,nowait "
+        "-chardev socket,id=redirector2,path=%s,nowait "
+        "-object filter-redirector,id=qtest-f0,netdev=qtest-bn0,"
+        "queue=tx,outdev=redirector0 "
+        "-object filter-redirector,id=qtest-f1,netdev=qtest-bn0,"
+        "queue=tx,indev=redirector2 "
+        "-object filter-redirector,id=qtest-f2,netdev=qtest-bn0,"
+        "queue=tx,outdev=redirector1 ", backend_sock[1], get_devstr(),
+        sock_path0, sock_path1, sock_path0);

     recv_sock = unix_connect(sock_path1, NULL);
     g_assert_cmpint(recv_sock, !=, -1);
@@ -141,7 +139,6 @@ static void test_redirector_tx(void)
 static void test_redirector_rx(void)
 {
     int backend_sock[2], send_sock;
-    char *cmdline;
     uint32_t ret = 0, len = 0;
     char send_buf[] = "Hello!!";
     char sock_path0[] = "filter-redirector0.XXXXXX";
@@ -158,20 +155,19 @@ static void test_redirector_rx(void)
     ret = mkstemp(sock_path1);
     g_assert_cmpint(ret, !=, -1);

-    cmdline = g_strdup_printf("-netdev socket,id=qtest-bn0,fd=%d "
-                "-device %s,netdev=qtest-bn0,id=qtest-e0 "
-                "-chardev socket,id=redirector0,path=%s,server,nowait "
-                "-chardev socket,id=redirector1,path=%s,server,nowait "
-                "-chardev socket,id=redirector2,path=%s,nowait "
-                "-object filter-redirector,id=qtest-f0,netdev=qtest-bn0,"
-                "queue=rx,indev=redirector0 "
-                "-object filter-redirector,id=qtest-f1,netdev=qtest-bn0,"
-                "queue=rx,outdev=redirector2 "
-                "-object filter-redirector,id=qtest-f2,netdev=qtest-bn0,"
-                "queue=rx,indev=redirector1 ", backend_sock[1], get_devstr(),
-                sock_path0, sock_path1, sock_path0);
-    qtest_start(cmdline);
-    g_free(cmdline);
+    global_qtest = qtest_startf(
+        "-netdev socket,id=qtest-bn0,fd=%d "
+        "-device %s,netdev=qtest-bn0,id=qtest-e0 "
+        "-chardev socket,id=redirector0,path=%s,server,nowait "
+        "-chardev socket,id=redirector1,path=%s,server,nowait "
+        "-chardev socket,id=redirector2,path=%s,nowait "
+        "-object filter-redirector,id=qtest-f0,netdev=qtest-bn0,"
+        "queue=rx,indev=redirector0 "
+        "-object filter-redirector,id=qtest-f1,netdev=qtest-bn0,"
+        "queue=rx,outdev=redirector2 "
+        "-object filter-redirector,id=qtest-f2,netdev=qtest-bn0,"
+        "queue=rx,indev=redirector1 ", backend_sock[1], get_devstr(),
+        sock_path0, sock_path1, sock_path0);

     struct iovec iov[] = {
         {
diff --git a/tests/test-hmp.c b/tests/test-hmp.c
index 752f4b63bd..7ff47eda13 100644
--- a/tests/test-hmp.c
+++ b/tests/test-hmp.c
@@ -119,16 +119,13 @@ static void test_info_commands(void)
 static void test_machine(gconstpointer data)
 {
     const char *machine = data;
-    char *args;

-    args = g_strdup_printf("-S -M %s", machine);
-    qtest_start(args);
+    global_qtest = qtest_startf("-S -M %s", machine);

     test_info_commands();
     test_commands();

     qtest_quit(global_qtest);
-    g_free(args);
     g_free((void *)data);
 }

diff --git a/tests/test-netfilter.c b/tests/test-netfilter.c
index ffb11dbfb7..bf9163e79f 100644
--- a/tests/test-netfilter.c
+++ b/tests/test-netfilter.c
@@ -182,7 +182,6 @@ static void remove_netdev_with_multi_netfilter(void)
 int main(int argc, char **argv)
 {
     int ret;
-    char *args;
     const char *devstr = "e1000";

     if (g_str_equal(qtest_get_arch(), "s390x")) {
@@ -197,13 +196,11 @@ int main(int argc, char **argv)
     qtest_add_func("/netfilter/remove_netdev_multi",
                    remove_netdev_with_multi_netfilter);

-    args = g_strdup_printf("-netdev user,id=qtest-bn0 "
-                           "-device %s,netdev=qtest-bn0", devstr);
-    qtest_start(args);
+    global_qtest = qtest_startf("-netdev user,id=qtest-bn0 "
+                                "-device %s,netdev=qtest-bn0", devstr);
     ret = g_test_run();

     qtest_quit(global_qtest);
-    g_free(args);

     return ret;
 }
diff --git a/tests/vhost-user-test.c b/tests/vhost-user-test.c
index d5afc5f9f6..c205a9100a 100644
--- a/tests/vhost-user-test.c
+++ b/tests/vhost-user-test.c
@@ -479,12 +479,12 @@ static void test_server_listen(TestServer *server)
 }

 #define GET_QEMU_CMD(s)                                         \
-    g_strdup_printf(QEMU_CMD, 512, 512, (root), (s)->chr_name,  \
-                    (s)->socket_path, "", (s)->chr_name)
+    QEMU_CMD, 512, 512, (root), (s)->chr_name,                  \
+        (s)->socket_path, "", (s)->chr_name

 #define GET_QEMU_CMDE(s, mem, chr_opts, extra, ...)                     \
-    g_strdup_printf(QEMU_CMD extra, (mem), (mem), (root), (s)->chr_name, \
-                    (s)->socket_path, (chr_opts), (s)->chr_name, ##__VA_ARGS__)
+    QEMU_CMD extra, (mem), (mem), (root), (s)->chr_name,                \
+        (s)->socket_path, (chr_opts), (s)->chr_name, ##__VA_ARGS__

 static gboolean _test_server_free(TestServer *server)
 {
@@ -634,7 +634,6 @@ static void test_migrate(void)
     TestServer *dest = test_server_new("dest");
     char *uri = g_strdup_printf("%s%s", "unix:", dest->mig_path);
     GSource *source;
-    gchar *cmd;
     QDict *rsp;
     guint8 *log;
     guint64 size;
@@ -642,18 +641,14 @@ static void test_migrate(void)
     test_server_listen(s);
     test_server_listen(dest);

-    cmd = GET_QEMU_CMDE(s, 2, "", "");
-    s->qts = qtest_init(cmd);
-    g_free(cmd);
+    s->qts = qtest_startf(GET_QEMU_CMDE(s, 2, "", ""));

     init_virtio_dev(s);
     wait_for_fds(s);
     size = get_log_size(s);
     g_assert_cmpint(size, ==, (2 * 1024 * 1024) / (VHOST_LOG_PAGE * 8));

-    cmd = GET_QEMU_CMDE(dest, 2, "", " -incoming %s", uri);
-    dest->qts = qtest_init(cmd);
-    g_free(cmd);
+    dest->qts = qtest_startf(GET_QEMU_CMDE(dest, 2, "", " -incoming %s", uri));

     source = g_source_new(&test_migrate_source_funcs,
                           sizeof(TestMigrateSource));
@@ -751,12 +746,9 @@ connect_thread(gpointer data)
 static void test_reconnect_subprocess(void)
 {
     TestServer *s = test_server_new("reconnect");
-    char *cmd;

     g_thread_new("connect", connect_thread, s);
-    cmd = GET_QEMU_CMDE(s, 2, ",server", "");
-    s->qts = qtest_init(cmd);
-    g_free(cmd);
+    s->qts = qtest_startf(GET_QEMU_CMDE(s, 2, ",server", ""));

     init_virtio_dev(s);
     wait_for_fds(s);
@@ -785,13 +777,10 @@ static void test_reconnect(void)
 static void test_connect_fail_subprocess(void)
 {
     TestServer *s = test_server_new("connect-fail");
-    char *cmd;

     s->test_fail = true;
     g_thread_new("connect", connect_thread, s);
-    cmd = GET_QEMU_CMDE(s, 2, ",server", "");
-    s->qts = qtest_init(cmd);
-    g_free(cmd);
+    s->qts = qtest_startf(GET_QEMU_CMDE(s, 2, ",server", ""));

     init_virtio_dev(s);
     wait_for_fds(s);
@@ -812,13 +801,10 @@ static void test_connect_fail(void)
 static void test_flags_mismatch_subprocess(void)
 {
     TestServer *s = test_server_new("flags-mismatch");
-    char *cmd;

     s->test_flags = TEST_FLAGS_DISCONNECT;
     g_thread_new("connect", connect_thread, s);
-    cmd = GET_QEMU_CMDE(s, 2, ",server", "");
-    s->qts = qtest_init(cmd);
-    g_free(cmd);
+    s->qts = qtest_startf(GET_QEMU_CMDE(s, 2, ",server", ""));

     init_virtio_dev(s);
     wait_for_fds(s);
@@ -877,19 +863,16 @@ static void test_multiqueue(void)
     QPCIBus *bus;
     QVirtQueuePCI *vq[queues * 2];
     QGuestAllocator *alloc;
-    char *cmd;
     int i;

     s->queues = queues;
     test_server_listen(s);

-    cmd = g_strdup_printf(QEMU_CMD_MEM QEMU_CMD_CHR QEMU_CMD_NETDEV ",queues=%d "
-                          "-device virtio-net-pci,netdev=net0,mq=on,vectors=%d",
-                          512, 512, root, s->chr_name,
-                          s->socket_path, "", s->chr_name,
-                          queues, queues * 2 + 2);
-    s->qts = qtest_init(cmd);
-    g_free(cmd);
+    s->qts = qtest_startf(
+        QEMU_CMD_MEM QEMU_CMD_CHR QEMU_CMD_NETDEV ",queues=%d "
+        "-device virtio-net-pci,netdev=net0,mq=on,vectors=%d",
+        512, 512, root, s->chr_name, s->socket_path, "", s->chr_name,
+        queues, queues * 2 + 2);

     bus = qpci_init_pc(s->qts, NULL);
     dev = virtio_net_pci_init(bus, PCI_SLOT);
@@ -919,7 +902,6 @@ int main(int argc, char **argv)
 {
     TestServer *server = NULL;
     const char *hugefs;
-    char *qemu_cmd = NULL;
     int ret;
     char template[] = "/tmp/vhost-test-XXXXXX";
     GMainLoop *loop;
@@ -951,10 +933,7 @@ int main(int argc, char **argv)
     /* run the main loop thread so the chardev may operate */
     thread = g_thread_new(NULL, thread_function, loop);

-    qemu_cmd = GET_QEMU_CMD(server);
-
-    server->qts = qtest_init(qemu_cmd);
-    g_free(qemu_cmd);
+    server->qts = qtest_startf(GET_QEMU_CMD(server));
     init_virtio_dev(server);

     qtest_add_data_func("/vhost-user/read-guest-mem", server, read_guest_mem);
diff --git a/tests/virtio-blk-test.c b/tests/virtio-blk-test.c
index ce316e753e..9594fbf437 100644
--- a/tests/virtio-blk-test.c
+++ b/tests/virtio-blk-test.c
@@ -85,19 +85,16 @@ static QOSState *pci_test_start(void)

 static void arm_test_start(void)
 {
-    char *cmdline;
     char *tmp_path;

     tmp_path = drive_create();

-    cmdline = g_strdup_printf("-machine virt "
+    global_qtest = qtest_startf("-machine virt "
                                 "-drive if=none,id=drive0,file=%s,format=raw "
                                 "-device virtio-blk-device,drive=drive0",
                                 tmp_path);
-    qtest_start(cmdline);
     unlink(tmp_path);
     g_free(tmp_path);
-    g_free(cmdline);
 }

 static void test_end(void)
diff --git a/tests/vmgenid-test.c b/tests/vmgenid-test.c
index 970c6274fd..108a5d9a6e 100644
--- a/tests/vmgenid-test.c
+++ b/tests/vmgenid-test.c
@@ -130,41 +130,32 @@ static void read_guid_from_monitor(QemuUUID *guid)

 static char disk[] = "tests/vmgenid-test-disk-XXXXXX";

-static char *guid_cmd_strdup(const char *guid)
-{
-    return g_strdup_printf("-machine accel=kvm:tcg "
-                           "-device vmgenid,id=testvgid,guid=%s "
-                           "-drive id=hd0,if=none,file=%s,format=raw "
-                           "-device ide-hd,drive=hd0 ",
-                           guid, disk);
-}
-
+#define GUID_CMD(guid)                          \
+    "-machine accel=kvm:tcg "                   \
+    "-device vmgenid,id=testvgid,guid=%s "      \
+    "-drive id=hd0,if=none,file=%s,format=raw " \
+    "-device ide-hd,drive=hd0 ", guid, disk

 static void vmgenid_set_guid_test(void)
 {
     QemuUUID expected, measured;
-    gchar *cmd;

     g_assert(qemu_uuid_parse(VGID_GUID, &expected) == 0);

-    cmd = guid_cmd_strdup(VGID_GUID);
-    qtest_start(cmd);
+    global_qtest = qtest_startf(GUID_CMD(VGID_GUID));

     /* Read the GUID from accessing guest memory */
     read_guid_from_memory(&measured);
     g_assert(memcmp(measured.data, expected.data, sizeof(measured.data)) == 0);

     qtest_quit(global_qtest);
-    g_free(cmd);
 }

 static void vmgenid_set_guid_auto_test(void)
 {
-    char *cmd;
     QemuUUID measured;

-    cmd = guid_cmd_strdup("auto");
-    qtest_start(cmd);
+    global_qtest = qtest_startf(GUID_CMD("auto"));

     read_guid_from_memory(&measured);

@@ -172,25 +163,21 @@ static void vmgenid_set_guid_auto_test(void)
     g_assert(!qemu_uuid_is_null(&measured));

     qtest_quit(global_qtest);
-    g_free(cmd);
 }

 static void vmgenid_query_monitor_test(void)
 {
     QemuUUID expected, measured;
-    gchar *cmd;

     g_assert(qemu_uuid_parse(VGID_GUID, &expected) == 0);

-    cmd = guid_cmd_strdup(VGID_GUID);
-    qtest_start(cmd);
+    global_qtest = qtest_startf(GUID_CMD(VGID_GUID));

     /* Read the GUID via the monitor */
     read_guid_from_monitor(&measured);
     g_assert(memcmp(measured.data, expected.data, sizeof(measured.data)) == 0);

     qtest_quit(global_qtest);
-    g_free(cmd);
 }

 int main(int argc, char **argv)
-- 
2.13.5

^ permalink raw reply related	[flat|nested] 75+ messages in thread

* [Qemu-devel] [PATCH v7 29/38] libqtest: Merge qtest_init() into qtest_start()
  2017-09-11 17:19 [Qemu-devel] [PATCH v7 00/38] Preliminary libqtest cleanups Eric Blake
                   ` (27 preceding siblings ...)
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 28/38] libqtest: Add qtest_[v]startf() Eric Blake
@ 2017-09-11 17:20 ` Eric Blake
  2017-09-12 10:37   ` Thomas Huth
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 30/38] qtest: Avoid passing raw strings through hmp() Eric Blake
                   ` (8 subsequent siblings)
  37 siblings, 1 reply; 75+ messages in thread
From: Eric Blake @ 2017-09-11 17:20 UTC (permalink / raw)
  To: qemu-devel
  Cc: armbru, pbonzini, thuth, Gerd Hoffmann, John Snow, Keith Busch,
	David Gibson, Alexander Graf, Michael S. Tsirkin, Amit Shah,
	Jason Wang, open list:Floppy, open list:sPAPR

Remove the trivial wrapper qtest_init(), and change qtest_start()
to no longer implicitly set global_qtest, to make it obvious in the
rest of the testsuite where we are still relying on global_qtest.
Everything now uses qtest_start() (and friends) and qtest_quit(),
and explicitly tracks the QTestState between the two (although in
many cases, this tracking is still done through global_qtest).
Doing this makes it easier to see what remaining cleanups will be
needed if we don't want an implicit dependency on global state.

A few more places that were needlessly strdup'ing a temporary
variable are cleaned up in the process.

As mentioned in an earlier commit, the choice to get rid of
qtest_init() also reduces confusion with the device initializer
of the non-testsuite qtest.c device.

Signed-off-by: Eric Blake <eblake@redhat.com>
---
 tests/libqtest.h               | 33 ++++-----------------------------
 tests/libqtest.c               |  4 ++--
 tests/ac97-test.c              |  2 +-
 tests/device-introspect-test.c | 12 ++++++------
 tests/display-vga-test.c       | 18 ++++++------------
 tests/drive_del-test.c         | 11 ++++++-----
 tests/es1370-test.c            |  2 +-
 tests/fdc-test.c               |  2 +-
 tests/fw_cfg-test.c            |  2 +-
 tests/hd-geo-test.c            |  8 ++++----
 tests/i440fx-test.c            |  2 +-
 tests/i82801b11-test.c         |  3 ++-
 tests/intel-hda-test.c         |  9 ++++-----
 tests/ioh3420-test.c           |  5 +++--
 tests/ipmi-kcs-test.c          |  7 ++-----
 tests/ipoctal232-test.c        |  3 ++-
 tests/ivshmem-test.c           |  2 +-
 tests/m48t59-test.c            |  2 +-
 tests/ne2000-test.c            |  2 +-
 tests/nvme-test.c              |  5 +++--
 tests/pcnet-test.c             |  2 +-
 tests/postcopy-test.c          |  4 ++--
 tests/pvpanic-test.c           |  2 +-
 tests/q35-test.c               |  4 ++--
 tests/qmp-test.c               |  4 ++--
 tests/rtc-test.c               |  2 +-
 tests/rtl8139-test.c           |  2 +-
 tests/spapr-phb-test.c         |  5 +++--
 tests/test-arm-mptimer.c       |  2 +-
 tests/test-x86-cpuid-compat.c  |  7 ++++---
 tests/tmp105-test.c            |  1 +
 tests/tpci200-test.c           |  2 +-
 tests/usb-hcd-ehci-test.c      | 23 ++++++++++++-----------
 tests/usb-hcd-ohci-test.c      |  2 +-
 tests/usb-hcd-xhci-test.c      |  5 +++--
 tests/virtio-balloon-test.c    |  2 +-
 tests/virtio-console-test.c    | 10 ++++------
 tests/virtio-net-test.c        |  2 +-
 tests/virtio-rng-test.c        |  2 +-
 tests/virtio-serial-test.c     |  2 +-
 tests/vmxnet3-test.c           |  2 +-
 tests/wdt_ib700-test.c         |  8 ++++----
 42 files changed, 101 insertions(+), 128 deletions(-)

diff --git a/tests/libqtest.h b/tests/libqtest.h
index 817e3a5580..2a21bf4605 100644
--- a/tests/libqtest.h
+++ b/tests/libqtest.h
@@ -27,7 +27,7 @@ extern QTestState *global_qtest;
  * qtest_start_without_qmp_handshake:
  * @extra_args: other arguments to pass to QEMU.
  *
- * Returns: #QTestState instance.  Does not affect #global_qtest.
+ * Returns: #QTestState instance, handshaking not yet completed.
  */
 QTestState *qtest_start_without_qmp_handshake(const char *extra_args);

@@ -35,10 +35,7 @@ QTestState *qtest_start_without_qmp_handshake(const char *extra_args);
  * qtest_start:
  * @args: other arguments to pass to QEMU
  *
- * Start QEMU and assign the resulting #QTestState to #global_qtest.
- * The global variable is used by "shortcut" functions documented below.
- *
- * Returns: #QTestState instance.
+ * Returns: #QTestState instance, handshaking completed.
  */
 QTestState *qtest_start(const char *args);

@@ -47,10 +44,7 @@ QTestState *qtest_start(const char *args);
  * @fmt...: Format for creating other arguments to pass to QEMU, formatted
  * like sprintf().
  *
- * Start QEMU and return the resulting #QTestState (but unlike qtest_start(),
- * #global_qtest is left at NULL).
- *
- * Returns: #QTestState instance.
+ * Returns: #QTestState instance, handshaking completed.
  */
 QTestState *qtest_startf(const char *fmt, ...) GCC_FMT_ATTR(1, 2);

@@ -60,30 +54,11 @@ QTestState *qtest_startf(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
  * like vsprintf().
  * @ap: Format arguments.
  *
- * Start QEMU and return the resulting #QTestState (but unlike qtest_start(),
- * #global_qtest is left at NULL).
- *
- * Returns: #QTestState instance.
+ * Returns: #QTestState instance, handshaking completed.
  */
 QTestState *qtest_vstartf(const char *fmt, va_list ap) GCC_FMT_ATTR(1, 0);

 /**
- * qtest_init:
- * @extra_args: other arguments to pass to QEMU.
- *
- * Returns: #QTestState instance.  Does not affect #global_qtest.
- */
-static inline QTestState *qtest_init(const char *extra_args)
-{
-    QTestState *s;
-
-    assert(!global_qtest);
-    s = qtest_start(extra_args);
-    global_qtest = NULL;
-    return s;
-}
-
-/**
  * qtest_quit:
  * @s: #QTestState instance to operate on.
  *
diff --git a/tests/libqtest.c b/tests/libqtest.c
index b535d7768f..44c89813ff 100644
--- a/tests/libqtest.c
+++ b/tests/libqtest.c
@@ -242,7 +242,7 @@ QTestState *qtest_start(const char *extra_args)
     qtest_qmp_discard_response(s, "{ 'execute': 'qmp_capabilities' }");

     assert(!global_qtest);
-    return global_qtest = s;
+    return s;
 }

 QTestState *qtest_vstartf(const char *fmt, va_list ap)
@@ -988,7 +988,7 @@ void qtest_cb_for_every_machine(void (*cb)(const char *machine))
     QString *qstr;
     const char *mname;

-    qtest_start("-machine none");
+    global_qtest = qtest_start("-machine none");
     response = qmp("{ 'execute': 'query-machines' }");
     g_assert(response);
     list = qdict_get_qlist(response, "return");
diff --git a/tests/ac97-test.c b/tests/ac97-test.c
index ca9b3dce88..2f5fb10632 100644
--- a/tests/ac97-test.c
+++ b/tests/ac97-test.c
@@ -22,7 +22,7 @@ int main(int argc, char **argv)
     g_test_init(&argc, &argv, NULL);
     qtest_add_func("/ac97/nop", nop);

-    qtest_start("-device AC97");
+    global_qtest = qtest_start("-device AC97");
     ret = g_test_run();

     qtest_quit(global_qtest);
diff --git a/tests/device-introspect-test.c b/tests/device-introspect-test.c
index b3227bd950..ed4f5f2eed 100644
--- a/tests/device-introspect-test.c
+++ b/tests/device-introspect-test.c
@@ -126,7 +126,7 @@ static void test_device_intro_list(void)
     QList *types;
     char *help;

-    qtest_start(common_args);
+    global_qtest = qtest_start(common_args);

     types = device_type_list(true);
     QDECREF(types);
@@ -167,7 +167,7 @@ static void test_qom_list_fields(void)
     QList *non_abstract;
     QListEntry *e;

-    qtest_start(common_args);
+    global_qtest = qtest_start(common_args);

     all_types = qom_list_types(NULL, true);
     non_abstract = qom_list_types(NULL, false);
@@ -194,14 +194,14 @@ static void test_qom_list_fields(void)

 static void test_device_intro_none(void)
 {
-    qtest_start(common_args);
+    global_qtest = qtest_start(common_args);
     test_one_device("nonexistent");
     qtest_quit(global_qtest);
 }

 static void test_device_intro_abstract(void)
 {
-    qtest_start(common_args);
+    global_qtest = qtest_start(common_args);
     test_one_device("device");
     qtest_quit(global_qtest);
 }
@@ -212,7 +212,7 @@ static void test_device_intro_concrete(void)
     QListEntry *entry;
     const char *type;

-    qtest_start(common_args);
+    global_qtest = qtest_start(common_args);
     types = device_type_list(false);

     QLIST_FOREACH_ENTRY(types, entry) {
@@ -232,7 +232,7 @@ static void test_abstract_interfaces(void)
     QListEntry *e;
     QDict *index;

-    qtest_start(common_args);
+    global_qtest = qtest_start(common_args);

     all_types = qom_list_types("interface", true);
     index = qom_type_index(all_types);
diff --git a/tests/display-vga-test.c b/tests/display-vga-test.c
index 8667330e3c..d638f15ec3 100644
--- a/tests/display-vga-test.c
+++ b/tests/display-vga-test.c
@@ -12,39 +12,33 @@

 static void pci_cirrus(void)
 {
-    qtest_start("-vga none -device cirrus-vga");
-    qtest_quit(global_qtest);
+    qtest_quit(qtest_start("-vga none -device cirrus-vga"));
 }

 static void pci_stdvga(void)
 {
-    qtest_start("-vga none -device VGA");
-    qtest_quit(global_qtest);
+    qtest_quit(qtest_start("-vga none -device VGA"));
 }

 static void pci_secondary(void)
 {
-    qtest_start("-vga none -device secondary-vga");
-    qtest_quit(global_qtest);
+    qtest_quit(qtest_start("-vga none -device secondary-vga"));
 }

 static void pci_multihead(void)
 {
-    qtest_start("-vga none -device VGA -device secondary-vga");
-    qtest_quit(global_qtest);
+    qtest_quit(qtest_start("-vga none -device VGA -device secondary-vga"));
 }

 static void pci_virtio_gpu(void)
 {
-    qtest_start("-vga none -device virtio-gpu-pci");
-    qtest_quit(global_qtest);
+    qtest_quit(qtest_start("-vga none -device virtio-gpu-pci"));
 }

 #ifdef CONFIG_VIRTIO_VGA
 static void pci_virtio_vga(void)
 {
-    qtest_start("-vga none -device virtio-vga");
-    qtest_quit(global_qtest);
+    qtest_quit(qtest_start("-vga none -device virtio-vga"));
 }
 #endif

diff --git a/tests/drive_del-test.c b/tests/drive_del-test.c
index 16d256fe64..f68c4bc176 100644
--- a/tests/drive_del-test.c
+++ b/tests/drive_del-test.c
@@ -45,7 +45,7 @@ static void device_del(void)
 static void test_drive_without_dev(void)
 {
     /* Start with an empty drive */
-    qtest_start("-drive if=none,id=drive0");
+    global_qtest = qtest_start("-drive if=none,id=drive0");

     /* Delete the drive */
     drive_del();
@@ -63,7 +63,7 @@ static void test_after_failed_device_add(void)
     QDict *response;
     QDict *error;

-    qtest_start("-drive if=none,id=drive0");
+    global_qtest = qtest_start("-drive if=none,id=drive0");

     /* Make device_add fail.  If this leaks the virtio-blk-pci device then a
      * reference to drive0 will also be held (via qdev properties).
@@ -92,9 +92,10 @@ static void test_after_failed_device_add(void)
 static void test_drive_del_device_del(void)
 {
     /* Start with a drive used by a device that unplugs instantaneously */
-    qtest_start("-drive if=none,id=drive0,file=null-co://,format=raw"
-                " -device virtio-scsi-pci"
-                " -device scsi-hd,drive=drive0,id=dev0");
+    global_qtest = qtest_start(
+        "-drive if=none,id=drive0,file=null-co://,format=raw"
+        " -device virtio-scsi-pci"
+        " -device scsi-hd,drive=drive0,id=dev0");

     /*
      * Delete the drive, and then the device
diff --git a/tests/es1370-test.c b/tests/es1370-test.c
index 5578ff94fa..ff6fdbfe57 100644
--- a/tests/es1370-test.c
+++ b/tests/es1370-test.c
@@ -22,7 +22,7 @@ int main(int argc, char **argv)
     g_test_init(&argc, &argv, NULL);
     qtest_add_func("/es1370/nop", nop);

-    qtest_start("-device ES1370");
+    global_qtest = qtest_start("-device ES1370");
     ret = g_test_run();

     qtest_quit(global_qtest);
diff --git a/tests/fdc-test.c b/tests/fdc-test.c
index e63e93179a..ec83625db2 100644
--- a/tests/fdc-test.c
+++ b/tests/fdc-test.c
@@ -564,7 +564,7 @@ int main(int argc, char **argv)
     /* Run the tests */
     g_test_init(&argc, &argv, NULL);

-    qtest_start("-device floppy,id=floppy0");
+    global_qtest = qtest_start("-device floppy,id=floppy0");
     qtest_irq_intercept_in(global_qtest, "ioapic");
     qtest_add_func("/fdc/cmos", test_cmos);
     qtest_add_func("/fdc/no_media_on_start", test_no_media_on_start);
diff --git a/tests/fw_cfg-test.c b/tests/fw_cfg-test.c
index 3a8dc7a2cd..37214fdf10 100644
--- a/tests/fw_cfg-test.c
+++ b/tests/fw_cfg-test.c
@@ -106,7 +106,7 @@ int main(int argc, char **argv)

     g_test_init(&argc, &argv, NULL);

-    s = qtest_init("-uuid 4600cb32-38ec-4b2f-8acb-81c6ea54f2d8");
+    s = qtest_start("-uuid 4600cb32-38ec-4b2f-8acb-81c6ea54f2d8");

     fw_cfg = pc_fw_cfg_init(s);

diff --git a/tests/hd-geo-test.c b/tests/hd-geo-test.c
index 67d571ef13..406eea3c56 100644
--- a/tests/hd-geo-test.c
+++ b/tests/hd-geo-test.c
@@ -241,7 +241,7 @@ static void test_ide_none(void)

     setup_common(argv, ARGV_SIZE);
     args = g_strjoinv(" ", argv);
-    qtest_start(args);
+    global_qtest = qtest_start(args);
     g_strfreev(argv);
     g_free(args);
     test_cmos();
@@ -263,7 +263,7 @@ static void test_ide_mbr(bool use_device, MBRcontents mbr)
         argc = setup_ide(argc, argv, ARGV_SIZE, i, dev, i, mbr, "");
     }
     args = g_strjoinv(" ", argv);
-    qtest_start(args);
+    global_qtest = qtest_start(args);
     g_strfreev(argv);
     g_free(args);
     test_cmos();
@@ -339,7 +339,7 @@ static void test_ide_drive_user(const char *dev, bool trans)
                      dev ? "" : opts);
     g_free(opts);
     args = g_strjoinv(" ", argv);
-    qtest_start(args);
+    global_qtest = qtest_start(args);
     g_strfreev(argv);
     g_free(args);
     test_cmos();
@@ -396,7 +396,7 @@ static void test_ide_drive_cd_0(void)
                          ide_idx, NULL, i, mbr_blank, "");
     }
     args = g_strjoinv(" ", argv);
-    qtest_start(args);
+    global_qtest = qtest_start(args);
     g_strfreev(argv);
     g_free(args);
     test_cmos();
diff --git a/tests/i440fx-test.c b/tests/i440fx-test.c
index c282ded4ca..1d78089f8e 100644
--- a/tests/i440fx-test.c
+++ b/tests/i440fx-test.c
@@ -340,7 +340,7 @@ static void test_i440fx_firmware(FirmwareTestFixture *fixture,
                                          : "-drive if=pflash,format=raw,file=",
                               fw_pathname);
     g_test_message("qemu cmdline: %s", cmdline);
-    qtest_start(cmdline);
+    global_qtest = qtest_start(cmdline);
     g_free(cmdline);

     /* QEMU has loaded the firmware (because qtest_start() only returns after
diff --git a/tests/i82801b11-test.c b/tests/i82801b11-test.c
index 0c94876626..b76714ae16 100644
--- a/tests/i82801b11-test.c
+++ b/tests/i82801b11-test.c
@@ -22,7 +22,8 @@ int main(int argc, char **argv)
     g_test_init(&argc, &argv, NULL);
     qtest_add_func("/i82801b11/nop", nop);

-    qtest_start("-machine q35 -device i82801b11-bridge,bus=pcie.0,addr=1e.0");
+    global_qtest = qtest_start("-machine q35 "
+                               "-device i82801b11-bridge,bus=pcie.0,addr=1e.0");
     ret = g_test_run();

     qtest_quit(global_qtest);
diff --git a/tests/intel-hda-test.c b/tests/intel-hda-test.c
index ce24fbac1f..c97a7635ed 100644
--- a/tests/intel-hda-test.c
+++ b/tests/intel-hda-test.c
@@ -18,15 +18,14 @@
 /* Tests only initialization so far. TODO: Replace with functional tests */
 static void ich6_test(void)
 {
-    qtest_start("-device intel-hda,id=" HDA_ID CODEC_DEVICES);
-    qtest_quit(global_qtest);
+    qtest_quit(qtest_start("-device intel-hda,id=" HDA_ID CODEC_DEVICES));
 }

 static void ich9_test(void)
 {
-    qtest_start("-machine q35 -device ich9-intel-hda,bus=pcie.0,addr=1b.0,id="
-                HDA_ID CODEC_DEVICES);
-    qtest_quit(global_qtest);
+    qtest_quit(qtest_start(
+        "-machine q35 -device ich9-intel-hda,bus=pcie.0,addr=1b.0,id="
+        HDA_ID CODEC_DEVICES));
 }

 int main(int argc, char **argv)
diff --git a/tests/ioh3420-test.c b/tests/ioh3420-test.c
index f2ca373e87..aff569b99e 100644
--- a/tests/ioh3420-test.c
+++ b/tests/ioh3420-test.c
@@ -22,8 +22,9 @@ int main(int argc, char **argv)
     g_test_init(&argc, &argv, NULL);
     qtest_add_func("/ioh3420/nop", nop);

-    qtest_start("-machine q35 -device ioh3420,bus=pcie.0,addr=1c.0,port=1,"
-                "chassis=1,multifunction=on");
+    global_qtest = qtest_start(
+        "-machine q35 -device ioh3420,bus=pcie.0,addr=1c.0,port=1,"
+        "chassis=1,multifunction=on");
     ret = g_test_run();

     qtest_quit(global_qtest);
diff --git a/tests/ipmi-kcs-test.c b/tests/ipmi-kcs-test.c
index 178ffc1797..5acc0d9e35 100644
--- a/tests/ipmi-kcs-test.c
+++ b/tests/ipmi-kcs-test.c
@@ -264,7 +264,6 @@ static void test_enable_irq(void)
 int main(int argc, char **argv)
 {
     const char *arch = qtest_get_arch();
-    char *cmdline;
     int ret;

     /* Check architecture */
@@ -276,10 +275,8 @@ int main(int argc, char **argv)
     /* Run the tests */
     g_test_init(&argc, &argv, NULL);

-    cmdline = g_strdup_printf("-device ipmi-bmc-sim,id=bmc0"
-                              " -device isa-ipmi-kcs,bmc=bmc0");
-    qtest_start(cmdline);
-    g_free(cmdline);
+    global_qtest = qtest_start("-device ipmi-bmc-sim,id=bmc0"
+                               " -device isa-ipmi-kcs,bmc=bmc0");
     qtest_irq_intercept_in(global_qtest, "ioapic");
     qtest_add_func("/ipmi/local/kcs_base", test_kcs_base);
     qtest_add_func("/ipmi/local/kcs_abort", test_kcs_abort);
diff --git a/tests/ipoctal232-test.c b/tests/ipoctal232-test.c
index d17a93449f..90b683284a 100644
--- a/tests/ipoctal232-test.c
+++ b/tests/ipoctal232-test.c
@@ -22,7 +22,8 @@ int main(int argc, char **argv)
     g_test_init(&argc, &argv, NULL);
     qtest_add_func("/ipoctal232/tpci200/nop", nop);

-    qtest_start("-device tpci200,id=ipack0 -device ipoctal232,bus=ipack0.0");
+    global_qtest = qtest_start("-device tpci200,id=ipack0 "
+                               "-device ipoctal232,bus=ipack0.0");
     ret = g_test_run();

     qtest_quit(global_qtest);
diff --git a/tests/ivshmem-test.c b/tests/ivshmem-test.c
index 58eaf0cafb..aa5619c62c 100644
--- a/tests/ivshmem-test.c
+++ b/tests/ivshmem-test.c
@@ -407,7 +407,7 @@ static void test_ivshmem_hotplug(void)
     gchar *opts;
     QTestState *qts;

-    qts = qtest_init("");
+    qts = qtest_start("");

     opts = g_strdup_printf("'shm': '%s', 'size': '1M'", tmpshm);

diff --git a/tests/m48t59-test.c b/tests/m48t59-test.c
index 0f921ef38a..aadd770f4f 100644
--- a/tests/m48t59-test.c
+++ b/tests/m48t59-test.c
@@ -246,7 +246,7 @@ int main(int argc, char **argv)

     g_test_init(&argc, &argv, NULL);

-    s = qtest_start("-rtc clock=vm");
+    s = global_qtest = qtest_start("-rtc clock=vm");

     qtest_add_func("/rtc/bcd/check-time", bcd_check_time);
     qtest_add_func("/rtc/fuzz-registers", fuzz_registers);
diff --git a/tests/ne2000-test.c b/tests/ne2000-test.c
index cae83c5c4c..8e6f7b07c6 100644
--- a/tests/ne2000-test.c
+++ b/tests/ne2000-test.c
@@ -22,7 +22,7 @@ int main(int argc, char **argv)
     g_test_init(&argc, &argv, NULL);
     qtest_add_func("/ne2000/pci/nop", pci_nop);

-    qtest_start("-device ne2k_pci");
+    global_qtest = qtest_start("-device ne2k_pci");
     ret = g_test_run();

     qtest_quit(global_qtest);
diff --git a/tests/nvme-test.c b/tests/nvme-test.c
index 3d6c0f39cf..b054ad6fcd 100644
--- a/tests/nvme-test.c
+++ b/tests/nvme-test.c
@@ -22,8 +22,9 @@ int main(int argc, char **argv)
     g_test_init(&argc, &argv, NULL);
     qtest_add_func("/nvme/nop", nop);

-    qtest_start("-drive id=drv0,if=none,file=null-co://,format=raw "
-                "-device nvme,drive=drv0,serial=foo");
+    global_qtest = qtest_start(
+        "-drive id=drv0,if=none,file=null-co://,format=raw "
+        "-device nvme,drive=drv0,serial=foo");
     ret = g_test_run();

     qtest_quit(global_qtest);
diff --git a/tests/pcnet-test.c b/tests/pcnet-test.c
index 98246d3504..a58a5fd7bf 100644
--- a/tests/pcnet-test.c
+++ b/tests/pcnet-test.c
@@ -22,7 +22,7 @@ int main(int argc, char **argv)
     g_test_init(&argc, &argv, NULL);
     qtest_add_func("/pcnet/pci/nop", pci_nop);

-    qtest_start("-device pcnet");
+    global_qtest = qtest_start("-device pcnet");
     ret = g_test_run();

     qtest_quit(global_qtest);
diff --git a/tests/postcopy-test.c b/tests/postcopy-test.c
index 0e5fe20a83..efee20a099 100644
--- a/tests/postcopy-test.c
+++ b/tests/postcopy-test.c
@@ -403,10 +403,10 @@ static void test_migrate(void)

     g_free(bootpath);

-    from = qtest_init(cmd_src);
+    from = qtest_start(cmd_src);
     g_free(cmd_src);

-    to = qtest_init(cmd_dst);
+    to = qtest_start(cmd_dst);
     g_free(cmd_dst);

     assert(!global_qtest);
diff --git a/tests/pvpanic-test.c b/tests/pvpanic-test.c
index 5d99b3d9e2..c09b3ddac6 100644
--- a/tests/pvpanic-test.c
+++ b/tests/pvpanic-test.c
@@ -37,7 +37,7 @@ int main(int argc, char **argv)
     g_test_init(&argc, &argv, NULL);
     qtest_add_func("/pvpanic/panic", test_panic);

-    qtest_start("-device pvpanic");
+    global_qtest = qtest_start("-device pvpanic");
     ret = g_test_run();

     qtest_quit(global_qtest);
diff --git a/tests/q35-test.c b/tests/q35-test.c
index a8e24e5b71..4d90b9174e 100644
--- a/tests/q35-test.c
+++ b/tests/q35-test.c
@@ -84,7 +84,7 @@ static void test_smram_lock(void)
     QPCIDevice *pcidev;
     QDict *response;

-    qtest_start("-M q35");
+    global_qtest = qtest_start("-M q35");

     pcibus = qpci_init_pc(global_qtest, NULL);
     g_assert(pcibus != NULL);
@@ -141,7 +141,7 @@ static void test_tseg_size(const void *data)
         cmdline = g_strdup_printf("-M q35 -m %uM",
                                   TSEG_SIZE_TEST_GUEST_RAM_MBYTES);
     }
-    qtest_start(cmdline);
+    global_qtest = qtest_start(cmdline);
     g_free(cmdline);

     /* locate the DRAM controller */
diff --git a/tests/qmp-test.c b/tests/qmp-test.c
index 8985e7f9ec..6ea66211d4 100644
--- a/tests/qmp-test.c
+++ b/tests/qmp-test.c
@@ -172,7 +172,7 @@ static void test_query(const void *data)
     QDict *resp, *error;
     const char *error_class;

-    qtest_start(common_args);
+    global_qtest = qtest_start(common_args);

     resp = qmp("{ 'execute': %s }", cmd);
     error = qdict_get_qdict(resp, "error");
@@ -224,7 +224,7 @@ static void qmp_schema_init(QmpSchema *schema)
     Visitor *qiv;
     SchemaInfoList *tail;

-    qtest_start(common_args);
+    global_qtest = qtest_start(common_args);
     resp = qmp("{ 'execute': 'query-qmp-schema' }");

     qiv = qobject_input_visitor_new(qdict_get(resp, "return"));
diff --git a/tests/rtc-test.c b/tests/rtc-test.c
index d7a96cbd79..dd5430dd2d 100644
--- a/tests/rtc-test.c
+++ b/tests/rtc-test.c
@@ -690,7 +690,7 @@ int main(int argc, char **argv)

     g_test_init(&argc, &argv, NULL);

-    s = qtest_start("-rtc clock=vm");
+    s = global_qtest = qtest_start("-rtc clock=vm");
     qtest_irq_intercept_in(s, "ioapic");

     qtest_add_func("/rtc/check-time/bcd", bcd_check_time);
diff --git a/tests/rtl8139-test.c b/tests/rtl8139-test.c
index 317eb586b5..1741f0ec47 100644
--- a/tests/rtl8139-test.c
+++ b/tests/rtl8139-test.c
@@ -197,7 +197,7 @@ int main(int argc, char **argv)
 {
     int ret;

-    qtest_start("-device rtl8139");
+    global_qtest = qtest_start("-device rtl8139");

     g_test_init(&argc, &argv, NULL);
     qtest_add_func("/rtl8139/nop", nop);
diff --git a/tests/spapr-phb-test.c b/tests/spapr-phb-test.c
index e76987ace9..5ab22f3ba6 100644
--- a/tests/spapr-phb-test.c
+++ b/tests/spapr-phb-test.c
@@ -21,15 +21,16 @@ static void test_phb_device(void)
 int main(int argc, char **argv)
 {
     int ret;
+    QTestState *qts;

     g_test_init(&argc, &argv, NULL);
     qtest_add_func("/spapr-phb/device", test_phb_device);

-    qtest_start("-device " TYPE_SPAPR_PCI_HOST_BRIDGE ",index=30");
+    qts = qtest_start("-device " TYPE_SPAPR_PCI_HOST_BRIDGE ",index=30");

     ret = g_test_run();

-    qtest_quit(global_qtest);
+    qtest_quit(qts);

     return ret;
 }
diff --git a/tests/test-arm-mptimer.c b/tests/test-arm-mptimer.c
index 823db9ebc9..9631c55fe3 100644
--- a/tests/test-arm-mptimer.c
+++ b/tests/test-arm-mptimer.c
@@ -1097,7 +1097,7 @@ tests_with_prescaler_arg:
         goto tests_with_prescaler_arg;
     }

-    qtest_start("-machine vexpress-a9");
+    global_qtest = qtest_start("-machine vexpress-a9");
     ret = g_test_run();
     qtest_quit(global_qtest);

diff --git a/tests/test-x86-cpuid-compat.c b/tests/test-x86-cpuid-compat.c
index 4e88f9e4e2..84410c63b9 100644
--- a/tests/test-x86-cpuid-compat.c
+++ b/tests/test-x86-cpuid-compat.c
@@ -59,7 +59,7 @@ static void test_cpuid_prop(const void *data)
     QNum *value;
     int64_t val;

-    qtest_start(args->cmdline);
+    global_qtest = qtest_start(args->cmdline);
     path = get_cpu0_qom_path();
     value = qobject_to_qnum(qom_get(path, args->property));
     g_assert(qnum_get_try_int(value, &val));
@@ -131,7 +131,7 @@ static void test_feature_flag(const void *data)
     QList *present, *filtered;
     uint32_t value;

-    qtest_start(args->cmdline);
+    global_qtest = qtest_start(args->cmdline);
     path = get_cpu0_qom_path();
     present = qobject_to_qlist(qom_get(path, "feature-words"));
     filtered = qobject_to_qlist(qom_get(path, "filtered-features"));
@@ -181,7 +181,8 @@ static void test_plus_minus_subprocess(void)
      * Note: rules 1 and 2 are planned to be removed soon, and
      * should generate a warning.
      */
-    qtest_start("-cpu pentium,-fpu,+fpu,-mce,mce=on,+cx8,cx8=off,+sse4_1,sse4_2=on");
+    global_qtest = qtest_start("-cpu pentium,-fpu,+fpu,-mce,mce=on,+cx8,"
+                               "cx8=off,+sse4_1,sse4_2=on");
     path = get_cpu0_qom_path();

     g_assert_false(qom_get_bool(path, "fpu"));
diff --git a/tests/tmp105-test.c b/tests/tmp105-test.c
index 382f88ba23..3f2cb3a864 100644
--- a/tests/tmp105-test.c
+++ b/tests/tmp105-test.c
@@ -154,6 +154,7 @@ int main(int argc, char **argv)
     s = qtest_start("-machine n800 "
                     "-device tmp105,bus=i2c-bus.0,id=" TMP105_TEST_ID
                     ",address=0x49");
+    global_qtest = s;
     i2c = omap_i2c_create(s, OMAP2_I2C_1_BASE);

     qtest_add_func("/tmp105/tx-rx", send_and_receive);
diff --git a/tests/tpci200-test.c b/tests/tpci200-test.c
index 3b756d10f0..223d44da35 100644
--- a/tests/tpci200-test.c
+++ b/tests/tpci200-test.c
@@ -22,7 +22,7 @@ int main(int argc, char **argv)
     g_test_init(&argc, &argv, NULL);
     qtest_add_func("/tpci200/nop", nop);

-    qtest_start("-device tpci200");
+    global_qtest = qtest_start("-device tpci200");
     ret = g_test_run();

     qtest_quit(global_qtest);
diff --git a/tests/usb-hcd-ehci-test.c b/tests/usb-hcd-ehci-test.c
index 16d74512cc..c3568a1782 100644
--- a/tests/usb-hcd-ehci-test.c
+++ b/tests/usb-hcd-ehci-test.c
@@ -156,17 +156,18 @@ int main(int argc, char **argv)
     qtest_add_func("/ehci/pci/ehci-port-2", pci_ehci_port_2);
     qtest_add_func("/ehci/pci/ehci-port-3-hotplug", pci_ehci_port_hotplug);

-    qtest_start("-machine q35 -device ich9-usb-ehci1,bus=pcie.0,addr=1d.7,"
-                "multifunction=on,id=ich9-ehci-1 "
-                "-device ich9-usb-uhci1,bus=pcie.0,addr=1d.0,"
-                "multifunction=on,masterbus=ich9-ehci-1.0,firstport=0 "
-                "-device ich9-usb-uhci2,bus=pcie.0,addr=1d.1,"
-                "multifunction=on,masterbus=ich9-ehci-1.0,firstport=2 "
-                "-device ich9-usb-uhci3,bus=pcie.0,addr=1d.2,"
-                "multifunction=on,masterbus=ich9-ehci-1.0,firstport=4 "
-                "-drive if=none,id=usbcdrom,media=cdrom "
-                "-device usb-tablet,bus=ich9-ehci-1.0,port=1,usb_version=1 "
-                "-device usb-storage,bus=ich9-ehci-1.0,port=2,drive=usbcdrom ");
+    global_qtest = qtest_start(
+        "-machine q35 -device ich9-usb-ehci1,bus=pcie.0,addr=1d.7,"
+        "multifunction=on,id=ich9-ehci-1 "
+        "-device ich9-usb-uhci1,bus=pcie.0,addr=1d.0,"
+        "multifunction=on,masterbus=ich9-ehci-1.0,firstport=0 "
+        "-device ich9-usb-uhci2,bus=pcie.0,addr=1d.1,"
+        "multifunction=on,masterbus=ich9-ehci-1.0,firstport=2 "
+        "-device ich9-usb-uhci3,bus=pcie.0,addr=1d.2,"
+        "multifunction=on,masterbus=ich9-ehci-1.0,firstport=4 "
+        "-drive if=none,id=usbcdrom,media=cdrom "
+        "-device usb-tablet,bus=ich9-ehci-1.0,port=1,usb_version=1 "
+        "-device usb-storage,bus=ich9-ehci-1.0,port=2,drive=usbcdrom ");

     test_init();
     ret = g_test_run();
diff --git a/tests/usb-hcd-ohci-test.c b/tests/usb-hcd-ohci-test.c
index aa38e44d03..62fd35f69a 100644
--- a/tests/usb-hcd-ohci-test.c
+++ b/tests/usb-hcd-ohci-test.c
@@ -31,7 +31,7 @@ int main(int argc, char **argv)
     qtest_add_func("/ohci/pci/init", test_ohci_init);
     qtest_add_func("/ohci/pci/hotplug", test_ohci_hotplug);

-    qtest_start("-device pci-ohci,id=ohci");
+    global_qtest = qtest_start("-device pci-ohci,id=ohci");
     ret = g_test_run();
     qtest_quit(global_qtest);

diff --git a/tests/usb-hcd-xhci-test.c b/tests/usb-hcd-xhci-test.c
index e45509741c..bd3c65ae85 100644
--- a/tests/usb-hcd-xhci-test.c
+++ b/tests/usb-hcd-xhci-test.c
@@ -80,8 +80,9 @@ int main(int argc, char **argv)
     qtest_add_func("/xhci/pci/hotplug", test_xhci_hotplug);
     qtest_add_func("/xhci/pci/hotplug/usb-uas", test_usb_uas_hotplug);

-    qtest_start("-device nec-usb-xhci,id=xhci"
-                " -drive id=drive0,if=none,file=null-co://,format=raw");
+    global_qtest = qtest_start(
+        "-device nec-usb-xhci,id=xhci"
+        " -drive id=drive0,if=none,file=null-co://,format=raw");
     ret = g_test_run();
     qtest_quit(global_qtest);

diff --git a/tests/virtio-balloon-test.c b/tests/virtio-balloon-test.c
index 34ad718601..cca7b0e8fb 100644
--- a/tests/virtio-balloon-test.c
+++ b/tests/virtio-balloon-test.c
@@ -22,7 +22,7 @@ int main(int argc, char **argv)
     g_test_init(&argc, &argv, NULL);
     qtest_add_func("/virtio/balloon/pci/nop", pci_nop);

-    qtest_start("-device virtio-balloon-pci");
+    global_qtest = qtest_start("-device virtio-balloon-pci");
     ret = g_test_run();

     qtest_quit(global_qtest);
diff --git a/tests/virtio-console-test.c b/tests/virtio-console-test.c
index 5c036ce02a..07a5f26880 100644
--- a/tests/virtio-console-test.c
+++ b/tests/virtio-console-test.c
@@ -13,16 +13,14 @@
 /* Tests only initialization so far. TODO: Replace with functional tests */
 static void console_pci_nop(void)
 {
-    qtest_start("-device virtio-serial-pci,id=vser0 "
-                "-device virtconsole,bus=vser0.0");
-    qtest_quit(global_qtest);
+    qtest_quit(qtest_start("-device virtio-serial-pci,id=vser0 "
+                           "-device virtconsole,bus=vser0.0"));
 }

 static void serialport_pci_nop(void)
 {
-    qtest_start("-device virtio-serial-pci,id=vser0 "
-                "-device virtserialport,bus=vser0.0");
-    qtest_quit(global_qtest);
+    qtest_quit(qtest_start("-device virtio-serial-pci,id=vser0 "
+                           "-device virtserialport,bus=vser0.0"));
 }

 int main(int argc, char **argv)
diff --git a/tests/virtio-net-test.c b/tests/virtio-net-test.c
index e1085c7e77..9250c417a9 100644
--- a/tests/virtio-net-test.c
+++ b/tests/virtio-net-test.c
@@ -246,7 +246,7 @@ static void hotplug(void)
 {
     const char *arch = qtest_get_arch();

-    qtest_start("-device virtio-net-pci");
+    global_qtest = qtest_start("-device virtio-net-pci");

     qpci_plug_device_test(global_qtest, "virtio-net-pci", "net1",
                           PCI_SLOT_HP, NULL);
diff --git a/tests/virtio-rng-test.c b/tests/virtio-rng-test.c
index a3a8bde491..e632281fa4 100644
--- a/tests/virtio-rng-test.c
+++ b/tests/virtio-rng-test.c
@@ -38,7 +38,7 @@ int main(int argc, char **argv)
     qtest_add_func("/virtio/rng/pci/nop", pci_nop);
     qtest_add_func("/virtio/rng/pci/hotplug", hotplug);

-    qtest_start("-device virtio-rng-pci");
+    global_qtest = qtest_start("-device virtio-rng-pci");
     ret = g_test_run();

     qtest_quit(global_qtest);
diff --git a/tests/virtio-serial-test.c b/tests/virtio-serial-test.c
index d0bbc2a8c9..1cc43465e6 100644
--- a/tests/virtio-serial-test.c
+++ b/tests/virtio-serial-test.c
@@ -49,7 +49,7 @@ int main(int argc, char **argv)
     qtest_add_func("/virtio/serial/pci/nop", pci_nop);
     qtest_add_func("/virtio/serial/pci/hotplug", hotplug);

-    qtest_start("-device virtio-serial-pci");
+    global_qtest = qtest_start("-device virtio-serial-pci");
     ret = g_test_run();

     qtest_quit(global_qtest);
diff --git a/tests/vmxnet3-test.c b/tests/vmxnet3-test.c
index 631630b4d0..0b4b807616 100644
--- a/tests/vmxnet3-test.c
+++ b/tests/vmxnet3-test.c
@@ -22,7 +22,7 @@ int main(int argc, char **argv)
     g_test_init(&argc, &argv, NULL);
     qtest_add_func("/vmxnet3/nop", nop);

-    qtest_start("-device vmxnet3");
+    global_qtest = qtest_start("-device vmxnet3");
     ret = g_test_run();

     qtest_quit(global_qtest);
diff --git a/tests/wdt_ib700-test.c b/tests/wdt_ib700-test.c
index 4bb8374011..a169265e16 100644
--- a/tests/wdt_ib700-test.c
+++ b/tests/wdt_ib700-test.c
@@ -56,7 +56,7 @@ static QDict *ib700_program_and_wait(QTestState *s)
 static void ib700_pause(void)
 {
     QDict *d;
-    QTestState *s = qtest_init("-watchdog-action pause -device ib700");
+    QTestState *s = qtest_start("-watchdog-action pause -device ib700");

     qtest_irq_intercept_in(s, "ioapic");
     d = ib700_program_and_wait(s);
@@ -69,7 +69,7 @@ static void ib700_pause(void)
 static void ib700_reset(void)
 {
     QDict *d;
-    QTestState *s = qtest_init("-watchdog-action reset -device ib700");
+    QTestState *s = qtest_start("-watchdog-action reset -device ib700");

     qtest_irq_intercept_in(s, "ioapic");
     d = ib700_program_and_wait(s);
@@ -84,7 +84,7 @@ static void ib700_shutdown(void)
     QDict *d;
     QTestState *s;

-    s = qtest_init("-watchdog-action reset -no-reboot -device ib700");
+    s = qtest_start("-watchdog-action reset -no-reboot -device ib700");
     qtest_irq_intercept_in(s, "ioapic");
     d = ib700_program_and_wait(s);
     g_assert(!strcmp(qdict_get_str(d, "action"), "reset"));
@@ -96,7 +96,7 @@ static void ib700_shutdown(void)
 static void ib700_none(void)
 {
     QDict *d;
-    QTestState *s = qtest_init("-watchdog-action none -device ib700");
+    QTestState *s = qtest_start("-watchdog-action none -device ib700");

     qtest_irq_intercept_in(s, "ioapic");
     d = ib700_program_and_wait(s);
-- 
2.13.5

^ permalink raw reply related	[flat|nested] 75+ messages in thread

* [Qemu-devel] [PATCH v7 30/38] qtest: Avoid passing raw strings through hmp()
  2017-09-11 17:19 [Qemu-devel] [PATCH v7 00/38] Preliminary libqtest cleanups Eric Blake
                   ` (28 preceding siblings ...)
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 29/38] libqtest: Merge qtest_init() into qtest_start() Eric Blake
@ 2017-09-11 17:20 ` Eric Blake
  2017-09-11 17:42   ` Dr. David Alan Gilbert
  2017-09-12 10:40   ` Thomas Huth
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 31/38] libqtest: Merge qtest_clock_*() with clock_*() Eric Blake
                   ` (7 subsequent siblings)
  37 siblings, 2 replies; 75+ messages in thread
From: Eric Blake @ 2017-09-11 17:20 UTC (permalink / raw)
  To: qemu-devel; +Cc: armbru, pbonzini, thuth, Dr. David Alan Gilbert

hmp() passes its string argument through the sprintf() family;
with a proper attribute, gcc -Wformat warns us when we do something
dangerous like passing a non-constant format string.  Fortunately,
all our strings were safe, but checking whether the string can
contain an unintended % is easy to avoid and therefore worth doing.

Signed-off-by: Eric Blake <eblake@redhat.com>

---
v7: add GCC_FMT_ATTR here, drop R-b
---
 tests/libqtest.h | 8 ++++----
 tests/test-hmp.c | 4 ++--
 2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/tests/libqtest.h b/tests/libqtest.h
index 2a21bf4605..5651b77d2f 100644
--- a/tests/libqtest.h
+++ b/tests/libqtest.h
@@ -153,14 +153,14 @@ QDict *qtest_qmp_eventwait_ref(QTestState *s, const char *event);
 /**
  * qtest_hmp:
  * @s: #QTestState instance to operate on.
- * @fmt...: HMP command to send to QEMU
+ * @fmt...: HMP command to send to QEMU, formats arguments like sprintf().
  *
  * Send HMP command to QEMU via QMP's human-monitor-command.
  * QMP events are discarded.
  *
  * Returns: the command's output.  The caller should g_free() it.
  */
-char *qtest_hmp(QTestState *s, const char *fmt, ...);
+char *qtest_hmp(QTestState *s, const char *fmt, ...) GCC_FMT_ATTR(2, 3);

 /**
  * qtest_hmpv:
@@ -585,13 +585,13 @@ static inline QDict *qmp_eventwait_ref(const char *event)

 /**
  * hmp:
- * @fmt...: HMP command to send to QEMU
+ * @fmt...: HMP command to send to QEMU, formats arguments like sprintf().
  *
  * Send HMP command to QEMU via QMP's human-monitor-command.
  *
  * Returns: the command's output.  The caller should g_free() it.
  */
-char *hmp(const char *fmt, ...);
+char *hmp(const char *fmt, ...) GCC_FMT_ATTR(1, 2);

 /**
  * get_irq:
diff --git a/tests/test-hmp.c b/tests/test-hmp.c
index 7ff47eda13..b3102daea1 100644
--- a/tests/test-hmp.c
+++ b/tests/test-hmp.c
@@ -80,7 +80,7 @@ static void test_commands(void)
         if (verbose) {
             fprintf(stderr, "\t%s\n", hmp_cmds[i]);
         }
-        response = hmp(hmp_cmds[i]);
+        response = hmp("%s", hmp_cmds[i]);
         g_free(response);
     }

@@ -103,7 +103,7 @@ static void test_info_commands(void)
         if (verbose) {
             fprintf(stderr, "\t%s\n", info);
         }
-        resp = hmp(info);
+        resp = hmp("%s", info);
         g_free(resp);
         /* And move forward to the next line */
         info = strchr(endp + 1, '\n');
-- 
2.13.5

^ permalink raw reply related	[flat|nested] 75+ messages in thread

* [Qemu-devel] [PATCH v7 31/38] libqtest: Merge qtest_clock_*() with clock_*()
  2017-09-11 17:19 [Qemu-devel] [PATCH v7 00/38] Preliminary libqtest cleanups Eric Blake
                   ` (29 preceding siblings ...)
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 30/38] qtest: Avoid passing raw strings through hmp() Eric Blake
@ 2017-09-11 17:20 ` Eric Blake
  2017-09-12 10:45   ` Thomas Huth
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 32/38] libqtest: Merge qtest_irq*() with irq*() Eric Blake
                   ` (6 subsequent siblings)
  37 siblings, 1 reply; 75+ messages in thread
From: Eric Blake @ 2017-09-11 17:20 UTC (permalink / raw)
  To: qemu-devel; +Cc: armbru, pbonzini, thuth, John Snow, open list:Floppy

Maintaining two layers of libqtest APIs, one that takes an explicit
QTestState object, and the other that uses the implicit global_qtest,
is annoying.  In the interest of getting rid of global implicit
state and having less code to maintain, merge:
 qtest_clock_set()
 qtest_clock_step()
 qtest_clock_step_next()
with their short counterparts.  All callers that previously
used the short form now make it explicit that they are relying on
global_qtest, and later patches can then clean things up to remove
the global variable.

Signed-off-by: Eric Blake <eblake@redhat.com>
---
 tests/libqtest.h         | 50 ++++----------------------------
 tests/libqtest.c         |  6 ++--
 tests/e1000e-test.c      |  2 +-
 tests/fdc-test.c         |  4 +--
 tests/ide-test.c         |  2 +-
 tests/libqos/virtio.c    |  8 +++---
 tests/rtc-test.c         | 74 ++++++++++++++++++++++++------------------------
 tests/rtl8139-test.c     | 10 +++----
 tests/tco-test.c         | 22 +++++++-------
 tests/test-arm-mptimer.c | 25 +++++++++-------
 tests/wdt_ib700-test.c   | 12 ++++----
 11 files changed, 90 insertions(+), 125 deletions(-)

diff --git a/tests/libqtest.h b/tests/libqtest.h
index 5651b77d2f..26d5f37bc9 100644
--- a/tests/libqtest.h
+++ b/tests/libqtest.h
@@ -417,17 +417,17 @@ void qtest_bufwrite(QTestState *s, uint64_t addr,
 void qtest_memset(QTestState *s, uint64_t addr, uint8_t patt, size_t size);

 /**
- * qtest_clock_step_next:
+ * clock_step_next:
  * @s: #QTestState instance to operate on.
  *
  * Advance the QEMU_CLOCK_VIRTUAL to the next deadline.
  *
  * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
  */
-int64_t qtest_clock_step_next(QTestState *s);
+int64_t clock_step_next(QTestState *s);

 /**
- * qtest_clock_step:
+ * clock_step:
  * @s: QTestState instance to operate on.
  * @step: Number of nanoseconds to advance the clock by.
  *
@@ -435,10 +435,10 @@ int64_t qtest_clock_step_next(QTestState *s);
  *
  * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
  */
-int64_t qtest_clock_step(QTestState *s, int64_t step);
+int64_t clock_step(QTestState *s, int64_t step);

 /**
- * qtest_clock_set:
+ * clock_set:
  * @s: QTestState instance to operate on.
  * @val: Nanoseconds value to advance the clock to.
  *
@@ -446,7 +446,7 @@ int64_t qtest_clock_step(QTestState *s, int64_t step);
  *
  * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
  */
-int64_t qtest_clock_set(QTestState *s, int64_t val);
+int64_t clock_set(QTestState *s, int64_t val);

 /**
  * qtest_big_endian:
@@ -868,44 +868,6 @@ static inline void qmemset(uint64_t addr, uint8_t patt, size_t size)
     qtest_memset(global_qtest, addr, patt, size);
 }

-/**
- * clock_step_next:
- *
- * Advance the QEMU_CLOCK_VIRTUAL to the next deadline.
- *
- * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
- */
-static inline int64_t clock_step_next(void)
-{
-    return qtest_clock_step_next(global_qtest);
-}
-
-/**
- * clock_step:
- * @step: Number of nanoseconds to advance the clock by.
- *
- * Advance the QEMU_CLOCK_VIRTUAL by @step nanoseconds.
- *
- * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
- */
-static inline int64_t clock_step(int64_t step)
-{
-    return qtest_clock_step(global_qtest, step);
-}
-
-/**
- * clock_set:
- * @val: Nanoseconds value to advance the clock to.
- *
- * Advance the QEMU_CLOCK_VIRTUAL to @val nanoseconds since the VM was launched.
- *
- * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
- */
-static inline int64_t clock_set(int64_t val)
-{
-    return qtest_clock_set(global_qtest, val);
-}
-
 QDict *qmp_fd_receive(int fd);
 void qmp_fd_sendv(int fd, const char *fmt, va_list ap);
 void qmp_fd_send(int fd, const char *fmt, ...);
diff --git a/tests/libqtest.c b/tests/libqtest.c
index 44c89813ff..9f5f2cb933 100644
--- a/tests/libqtest.c
+++ b/tests/libqtest.c
@@ -666,19 +666,19 @@ static int64_t qtest_clock_rsp(QTestState *s)
     return clock;
 }

-int64_t qtest_clock_step_next(QTestState *s)
+int64_t clock_step_next(QTestState *s)
 {
     qtest_sendf(s, "clock_step\n");
     return qtest_clock_rsp(s);
 }

-int64_t qtest_clock_step(QTestState *s, int64_t step)
+int64_t clock_step(QTestState *s, int64_t step)
 {
     qtest_sendf(s, "clock_step %"PRIi64"\n", step);
     return qtest_clock_rsp(s);
 }

-int64_t qtest_clock_set(QTestState *s, int64_t val)
+int64_t clock_set(QTestState *s, int64_t val)
 {
     qtest_sendf(s, "clock_set %"PRIi64"\n", val);
     return qtest_clock_rsp(s);
diff --git a/tests/e1000e-test.c b/tests/e1000e-test.c
index 323aabb454..f4ead74f96 100644
--- a/tests/e1000e-test.c
+++ b/tests/e1000e-test.c
@@ -229,7 +229,7 @@ static void e1000e_wait_isr(e1000e_device *d, uint16_t msg_id)
         if (qpci_msix_pending(d->pci_dev, msg_id)) {
             return;
         }
-        clock_step(10000);
+        clock_step(global_qtest, 10000);
     } while (g_get_monotonic_time() < end_time);

     g_error("Timeout expired");
diff --git a/tests/fdc-test.c b/tests/fdc-test.c
index ec83625db2..e79cd0f3b5 100644
--- a/tests/fdc-test.c
+++ b/tests/fdc-test.c
@@ -424,7 +424,7 @@ static void test_read_id(void)

     while (!get_irq(FLOPPY_IRQ)) {
         /* qemu involves a timer with READ ID... */
-        clock_step(1000000000LL / 50);
+        clock_step(global_qtest, 1000000000LL / 50);
     }

     msr = inb(FLOPPY_BASE + reg_msr);
@@ -467,7 +467,7 @@ static void test_read_id(void)

     while (!get_irq(FLOPPY_IRQ)) {
         /* qemu involves a timer with READ ID... */
-        clock_step(1000000000LL / 50);
+        clock_step(global_qtest, 1000000000LL / 50);
     }

     msr = inb(FLOPPY_BASE + reg_msr);
diff --git a/tests/ide-test.c b/tests/ide-test.c
index e1650c0132..32c8c40294 100644
--- a/tests/ide-test.c
+++ b/tests/ide-test.c
@@ -749,7 +749,7 @@ static void nsleep(int64_t nsecs)
 {
     const struct timespec val = { .tv_nsec = nsecs };
     nanosleep(&val, NULL);
-    clock_set(nsecs);
+    clock_set(global_qtest, nsecs);
 }

 static uint8_t ide_wait_clear(uint8_t flag)
diff --git a/tests/libqos/virtio.c b/tests/libqos/virtio.c
index 2212830742..92e47e78f2 100644
--- a/tests/libqos/virtio.c
+++ b/tests/libqos/virtio.c
@@ -97,7 +97,7 @@ void qvirtio_wait_queue_isr(QVirtioDevice *d,
     gint64 start_time = g_get_monotonic_time();

     for (;;) {
-        qtest_clock_step(d->bus->qts, 100);
+        clock_step(d->bus->qts, 100);
         if (d->bus->get_queue_isr_status(d, vq)) {
             return;
         }
@@ -119,7 +119,7 @@ uint8_t qvirtio_wait_status_byte_no_isr(QVirtioDevice *d,
     uint8_t val;

     while ((val = qtest_readb(d->bus->qts, addr)) == 0xff) {
-        qtest_clock_step(d->bus->qts, 100);
+        clock_step(d->bus->qts, 100);
         g_assert(!d->bus->get_queue_isr_status(d, vq));
         g_assert(g_get_monotonic_time() - start_time <= timeout_us);
     }
@@ -143,7 +143,7 @@ void qvirtio_wait_used_elem(QVirtioDevice *d,
     for (;;) {
         uint32_t got_desc_idx;

-        qtest_clock_step(d->bus->qts, 100);
+        clock_step(d->bus->qts, 100);

         if (d->bus->get_queue_isr_status(d, vq) &&
             qvirtqueue_get_buf(vq, &got_desc_idx)) {
@@ -160,7 +160,7 @@ void qvirtio_wait_config_isr(QVirtioDevice *d, gint64 timeout_us)
     gint64 start_time = g_get_monotonic_time();

     for (;;) {
-        qtest_clock_step(d->bus->qts, 100);
+        clock_step(d->bus->qts, 100);
         if (d->bus->get_config_isr_status(d)) {
             return;
         }
diff --git a/tests/rtc-test.c b/tests/rtc-test.c
index dd5430dd2d..9b98e2d494 100644
--- a/tests/rtc-test.c
+++ b/tests/rtc-test.c
@@ -291,7 +291,7 @@ static void alarm_time(void)
             break;
         }

-        clock_step(1000000000);
+        clock_step(global_qtest, 1000000000);
     }

     g_assert(get_irq(RTC_ISA_IRQ));
@@ -371,35 +371,35 @@ static void basic_12h_bcd(void)
 {
     /* set BCD 12 hour mode */
     set_time(0, 0x81, 0x59, 0x00);
-    clock_step(1000000000LL);
+    clock_step(global_qtest, 1000000000LL);
     assert_time(0x81, 0x59, 0x01);
-    clock_step(59000000000LL);
+    clock_step(global_qtest, 59000000000LL);
     assert_time(0x82, 0x00, 0x00);

     /* test BCD wraparound */
     set_time(0, 0x09, 0x59, 0x59);
-    clock_step(60000000000LL);
+    clock_step(global_qtest, 60000000000LL);
     assert_time(0x10, 0x00, 0x59);

     /* 12 AM -> 1 AM */
     set_time(0, 0x12, 0x59, 0x59);
-    clock_step(1000000000LL);
+    clock_step(global_qtest, 1000000000LL);
     assert_time(0x01, 0x00, 0x00);

     /* 12 PM -> 1 PM */
     set_time(0, 0x92, 0x59, 0x59);
-    clock_step(1000000000LL);
+    clock_step(global_qtest, 1000000000LL);
     assert_time(0x81, 0x00, 0x00);

     /* 11 AM -> 12 PM */
     set_time(0, 0x11, 0x59, 0x59);
-    clock_step(1000000000LL);
+    clock_step(global_qtest, 1000000000LL);
     assert_time(0x92, 0x00, 0x00);
     /* TODO: test day wraparound */

     /* 11 PM -> 12 AM */
     set_time(0, 0x91, 0x59, 0x59);
-    clock_step(1000000000LL);
+    clock_step(global_qtest, 1000000000LL);
     assert_time(0x12, 0x00, 0x00);
     /* TODO: test day wraparound */
 }
@@ -408,29 +408,29 @@ static void basic_12h_dec(void)
 {
     /* set decimal 12 hour mode */
     set_time(REG_B_DM, 0x81, 59, 0);
-    clock_step(1000000000LL);
+    clock_step(global_qtest, 1000000000LL);
     assert_time(0x81, 59, 1);
-    clock_step(59000000000LL);
+    clock_step(global_qtest, 59000000000LL);
     assert_time(0x82, 0, 0);

     /* 12 PM -> 1 PM */
     set_time(REG_B_DM, 0x8c, 59, 59);
-    clock_step(1000000000LL);
+    clock_step(global_qtest, 1000000000LL);
     assert_time(0x81, 0, 0);

     /* 12 AM -> 1 AM */
     set_time(REG_B_DM, 0x0c, 59, 59);
-    clock_step(1000000000LL);
+    clock_step(global_qtest, 1000000000LL);
     assert_time(0x01, 0, 0);

     /* 11 AM -> 12 PM */
     set_time(REG_B_DM, 0x0b, 59, 59);
-    clock_step(1000000000LL);
+    clock_step(global_qtest, 1000000000LL);
     assert_time(0x8c, 0, 0);

     /* 11 PM -> 12 AM */
     set_time(REG_B_DM, 0x8b, 59, 59);
-    clock_step(1000000000LL);
+    clock_step(global_qtest, 1000000000LL);
     assert_time(0x0c, 0, 0);
     /* TODO: test day wraparound */
 }
@@ -439,19 +439,19 @@ static void basic_24h_bcd(void)
 {
     /* set BCD 24 hour mode */
     set_time(REG_B_24H, 0x09, 0x59, 0x00);
-    clock_step(1000000000LL);
+    clock_step(global_qtest, 1000000000LL);
     assert_time(0x09, 0x59, 0x01);
-    clock_step(59000000000LL);
+    clock_step(global_qtest, 59000000000LL);
     assert_time(0x10, 0x00, 0x00);

     /* test BCD wraparound */
     set_time(REG_B_24H, 0x09, 0x59, 0x00);
-    clock_step(60000000000LL);
+    clock_step(global_qtest, 60000000000LL);
     assert_time(0x10, 0x00, 0x00);

     /* TODO: test day wraparound */
     set_time(REG_B_24H, 0x23, 0x59, 0x00);
-    clock_step(60000000000LL);
+    clock_step(global_qtest, 60000000000LL);
     assert_time(0x00, 0x00, 0x00);
 }

@@ -459,19 +459,19 @@ static void basic_24h_dec(void)
 {
     /* set decimal 24 hour mode */
     set_time(REG_B_24H | REG_B_DM, 9, 59, 0);
-    clock_step(1000000000LL);
+    clock_step(global_qtest, 1000000000LL);
     assert_time(9, 59, 1);
-    clock_step(59000000000LL);
+    clock_step(global_qtest, 59000000000LL);
     assert_time(10, 0, 0);

     /* test BCD wraparound */
     set_time(REG_B_24H | REG_B_DM, 9, 59, 0);
-    clock_step(60000000000LL);
+    clock_step(global_qtest, 60000000000LL);
     assert_time(10, 0, 0);

     /* TODO: test day wraparound */
     set_time(REG_B_24H | REG_B_DM, 23, 59, 0);
-    clock_step(60000000000LL);
+    clock_step(global_qtest, 60000000000LL);
     assert_time(0, 0, 0);
 }

@@ -493,7 +493,7 @@ static void am_pm_alarm(void)
     cmos_write(RTC_REG_A, 0x26);

     /* Check that alarm triggers when AM/PM is set.  */
-    clock_step(60000000000LL);
+    clock_step(global_qtest, 60000000000LL);
     g_assert(cmos_read(RTC_HOURS) == 0x82);
     g_assert((cmos_read(RTC_REG_C) & REG_C_AF) != 0);

@@ -520,7 +520,7 @@ static void am_pm_alarm(void)
     cmos_write(RTC_REG_A, 0x26);

     /* Check that alarm triggers.  */
-    clock_step(3600 * 11 * 1000000000LL);
+    clock_step(global_qtest, 3600 * 11 * 1000000000LL);
     g_assert(cmos_read(RTC_HOURS) == 0x82);
     g_assert((cmos_read(RTC_REG_C) & REG_C_AF) != 0);

@@ -534,7 +534,7 @@ static void am_pm_alarm(void)
     cmos_write(RTC_REG_A, 0x26);

     /* Check that alarm does not trigger if hours differ only by AM/PM.  */
-    clock_step(3600 * 11 * 1000000000LL);
+    clock_step(global_qtest, 3600 * 11 * 1000000000LL);
     g_assert(cmos_read(RTC_HOURS) == 0x82);
     g_assert((cmos_read(RTC_REG_C) & REG_C_AF) == 0);
 }
@@ -558,7 +558,7 @@ static void fuzz_registers(void)
 static void register_b_set_flag(void)
 {
     if (cmos_read(RTC_REG_A) & REG_A_UIP) {
-        clock_step(UIP_HOLD_LENGTH + NANOSECONDS_PER_SECOND / 5);
+        clock_step(global_qtest, UIP_HOLD_LENGTH + NANOSECONDS_PER_SECOND / 5);
     }
     g_assert_cmpint(cmos_read(RTC_REG_A) & REG_A_UIP, ==, 0);

@@ -570,7 +570,7 @@ static void register_b_set_flag(void)
     assert_datetime_bcd(0x02, 0x04, 0x58, 0x02, 0x02, 0x2011);

     /* Since SET flag is still enabled, time does not advance. */
-    clock_step(1000000000LL);
+    clock_step(global_qtest, 1000000000LL);
     assert_datetime_bcd(0x02, 0x04, 0x58, 0x02, 0x02, 0x2011);

     /* Disable SET flag in Register B */
@@ -579,7 +579,7 @@ static void register_b_set_flag(void)
     assert_datetime_bcd(0x02, 0x04, 0x58, 0x02, 0x02, 0x2011);

     /* Since SET flag is disabled, the clock now advances.  */
-    clock_step(1000000000LL);
+    clock_step(global_qtest, 1000000000LL);
     assert_datetime_bcd(0x02, 0x04, 0x59, 0x02, 0x02, 0x2011);
 }

@@ -595,18 +595,18 @@ static void divider_reset(void)
     assert_datetime_bcd(0x02, 0x04, 0x58, 0x02, 0x02, 0x2011);

     /* Since divider reset flag is still enabled, these are equality checks. */
-    clock_step(1000000000LL);
+    clock_step(global_qtest, 1000000000LL);
     assert_datetime_bcd(0x02, 0x04, 0x58, 0x02, 0x02, 0x2011);

     /* The first update ends 500 ms after divider reset */
     cmos_write(RTC_REG_A, 0x26);
-    clock_step(500000000LL - UIP_HOLD_LENGTH - 1);
+    clock_step(global_qtest, 500000000LL - UIP_HOLD_LENGTH - 1);
     g_assert_cmpint(cmos_read(RTC_REG_A) & REG_A_UIP, ==, 0);
     assert_datetime_bcd(0x02, 0x04, 0x58, 0x02, 0x02, 0x2011);

-    clock_step(1);
+    clock_step(global_qtest, 1);
     g_assert_cmpint(cmos_read(RTC_REG_A) & REG_A_UIP, !=, 0);
-    clock_step(UIP_HOLD_LENGTH);
+    clock_step(global_qtest, UIP_HOLD_LENGTH);
     g_assert_cmpint(cmos_read(RTC_REG_A) & REG_A_UIP, ==, 0);

     assert_datetime_bcd(0x02, 0x04, 0x59, 0x02, 0x02, 0x2011);
@@ -618,7 +618,7 @@ static void uip_stuck(void)

     /* The first update ends 500 ms after divider reset */
     (void)cmos_read(RTC_REG_C);
-    clock_step(500000000LL);
+    clock_step(global_qtest, 500000000LL);
     g_assert_cmpint(cmos_read(RTC_REG_A) & REG_A_UIP, ==, 0);
     assert_datetime_bcd(0x02, 0x04, 0x59, 0x02, 0x02, 0x2011);

@@ -628,12 +628,12 @@ static void uip_stuck(void)
     cmos_write(RTC_SECONDS_ALARM, 0xC0);

     /* Because the alarm will fire soon, reading register A will latch UIP.  */
-    clock_step(1000000000LL - UIP_HOLD_LENGTH / 2);
+    clock_step(global_qtest, 1000000000LL - UIP_HOLD_LENGTH / 2);
     g_assert_cmpint(cmos_read(RTC_REG_A) & REG_A_UIP, !=, 0);

     /* Move the alarm far away.  This must not cause UIP to remain stuck!  */
     cmos_write(RTC_HOURS_ALARM, 0x03);
-    clock_step(UIP_HOLD_LENGTH);
+    clock_step(global_qtest, UIP_HOLD_LENGTH);
     g_assert_cmpint(cmos_read(RTC_REG_A) & REG_A_UIP, ==, 0);
 }

@@ -645,7 +645,7 @@ static void uip_stuck(void)
 static uint64_t wait_periodic_interrupt(uint64_t real_time)
 {
     while (!get_irq(RTC_ISA_IRQ)) {
-        real_time = clock_step_next();
+        real_time = clock_step_next(global_qtest);
     }

     g_assert((cmos_read(RTC_REG_C) & REG_C_PF) != 0);
@@ -664,7 +664,7 @@ static void periodic_timer(void)
     /* enable periodic interrupt after properly configure the period. */
     cmos_write(RTC_REG_B, cmos_read(RTC_REG_B) | REG_B_PIE);

-    start_time = real_time = clock_step_next();
+    start_time = real_time = clock_step_next(global_qtest);

     for (i = 0; i < RTC_PERIOD_TEST_NR; i++) {
         cmos_write(RTC_REG_A, RTC_PERIOD_CODE1);
diff --git a/tests/rtl8139-test.c b/tests/rtl8139-test.c
index 1741f0ec47..d123245108 100644
--- a/tests/rtl8139-test.c
+++ b/tests/rtl8139-test.c
@@ -84,7 +84,7 @@ static void test_timer(void)
         fatal("time too big %u\n", curr);
     }
     for (cnt = 0; ; ) {
-        clock_step(1 * NANOSECONDS_PER_SECOND);
+        clock_step(global_qtest, 1 * NANOSECONDS_PER_SECOND);
         prev = curr;
         curr = in_Timer();

@@ -106,7 +106,7 @@ static void test_timer(void)
     /* Test 3. Setting TimerInt to 1 and Timer to 0 get interrupt */
     out_TimerInt(1);
     out_Timer(0);
-    clock_step(40);
+    clock_step(global_qtest, 40);
     if ((in_IntrStatus() & 0x4000) == 0) {
         fatal("we should have an interrupt here!\n");
     }
@@ -123,7 +123,7 @@ static void test_timer(void)
     out_IntrStatus(0x4000);
     curr = in_Timer();
     out_TimerInt(curr + 0.5 * CLK);
-    clock_step(1 * NANOSECONDS_PER_SECOND);
+    clock_step(global_qtest, 1 * NANOSECONDS_PER_SECOND);
     out_Timer(0);
     if ((in_IntrStatus() & 0x4000) == 0) {
         fatal("we should have an interrupt here!\n");
@@ -135,7 +135,7 @@ static void test_timer(void)
     out_IntrStatus(0x4000);
     curr = in_Timer();
     out_TimerInt(curr + 0.5 * CLK);
-    clock_step(1 * NANOSECONDS_PER_SECOND);
+    clock_step(global_qtest, 1 * NANOSECONDS_PER_SECOND);
     out_TimerInt(0);
     if ((in_IntrStatus() & 0x4000) == 0) {
         fatal("we should have an interrupt here!\n");
@@ -146,7 +146,7 @@ static void test_timer(void)
     next = curr + 5.0 * CLK;
     out_TimerInt(next);
     for (cnt = 0; ; ) {
-        clock_step(1 * NANOSECONDS_PER_SECOND);
+        clock_step(global_qtest, 1 * NANOSECONDS_PER_SECOND);
         prev = curr;
         curr = in_Timer();
         diff = (curr-prev) & 0xffffffffu;
diff --git a/tests/tco-test.c b/tests/tco-test.c
index b84a50927a..27e982d8ac 100644
--- a/tests/tco-test.c
+++ b/tests/tco-test.c
@@ -173,7 +173,7 @@ static void test_tco_timeout(void)
     set_tco_timeout(&d, ticks);
     load_tco(&d);
     start_tco(&d);
-    clock_step(ticks * TCO_TICK_NSEC);
+    clock_step(global_qtest, ticks * TCO_TICK_NSEC);

     /* test first timeout */
     val = qpci_io_readw(d.dev, d.tco_io_bar, TCO1_STS);
@@ -188,7 +188,7 @@ static void test_tco_timeout(void)
     g_assert(ret == 0);

     /* test second timeout */
-    clock_step(ticks * TCO_TICK_NSEC);
+    clock_step(global_qtest, ticks * TCO_TICK_NSEC);
     val = qpci_io_readw(d.dev, d.tco_io_bar, TCO1_STS);
     ret = val & TCO_TIMEOUT ? 1 : 0;
     g_assert(ret == 1);
@@ -217,14 +217,14 @@ static void test_tco_max_timeout(void)
     set_tco_timeout(&d, ticks);
     load_tco(&d);
     start_tco(&d);
-    clock_step(((ticks & TCO_TMR_MASK) - 1) * TCO_TICK_NSEC);
+    clock_step(global_qtest, ((ticks & TCO_TMR_MASK) - 1) * TCO_TICK_NSEC);

     val = qpci_io_readw(d.dev, d.tco_io_bar, TCO_RLD);
     g_assert_cmpint(val & TCO_RLD_MASK, ==, 1);
     val = qpci_io_readw(d.dev, d.tco_io_bar, TCO1_STS);
     ret = val & TCO_TIMEOUT ? 1 : 0;
     g_assert(ret == 0);
-    clock_step(TCO_TICK_NSEC);
+    clock_step(global_qtest, TCO_TICK_NSEC);
     val = qpci_io_readw(d.dev, d.tco_io_bar, TCO1_STS);
     ret = val & TCO_TIMEOUT ? 1 : 0;
     g_assert(ret == 1);
@@ -260,7 +260,7 @@ static void test_tco_second_timeout_pause(void)
     set_tco_timeout(&td, TCO_SECS_TO_TICKS(16));
     load_tco(&td);
     start_tco(&td);
-    clock_step(ticks * TCO_TICK_NSEC * 2);
+    clock_step(global_qtest, ticks * TCO_TICK_NSEC * 2);
     ad = get_watchdog_action();
     g_assert(!strcmp(qdict_get_str(ad, "action"), "pause"));
     QDECREF(ad);
@@ -285,7 +285,7 @@ static void test_tco_second_timeout_reset(void)
     set_tco_timeout(&td, TCO_SECS_TO_TICKS(16));
     load_tco(&td);
     start_tco(&td);
-    clock_step(ticks * TCO_TICK_NSEC * 2);
+    clock_step(global_qtest, ticks * TCO_TICK_NSEC * 2);
     ad = get_watchdog_action();
     g_assert(!strcmp(qdict_get_str(ad, "action"), "reset"));
     QDECREF(ad);
@@ -310,7 +310,7 @@ static void test_tco_second_timeout_shutdown(void)
     set_tco_timeout(&td, ticks);
     load_tco(&td);
     start_tco(&td);
-    clock_step(ticks * TCO_TICK_NSEC * 2);
+    clock_step(global_qtest, ticks * TCO_TICK_NSEC * 2);
     ad = get_watchdog_action();
     g_assert(!strcmp(qdict_get_str(ad, "action"), "shutdown"));
     QDECREF(ad);
@@ -335,7 +335,7 @@ static void test_tco_second_timeout_none(void)
     set_tco_timeout(&td, ticks);
     load_tco(&td);
     start_tco(&td);
-    clock_step(ticks * TCO_TICK_NSEC * 2);
+    clock_step(global_qtest, ticks * TCO_TICK_NSEC * 2);
     ad = get_watchdog_action();
     g_assert(!strcmp(qdict_get_str(ad, "action"), "none"));
     QDECREF(ad);
@@ -364,7 +364,7 @@ static void test_tco_ticks_counter(void)
     do {
         rld = qpci_io_readw(d.dev, d.tco_io_bar, TCO_RLD) & TCO_RLD_MASK;
         g_assert_cmpint(rld, ==, ticks);
-        clock_step(TCO_TICK_NSEC);
+        clock_step(global_qtest, TCO_TICK_NSEC);
         ticks--;
     } while (!(qpci_io_readw(d.dev, d.tco_io_bar, TCO1_STS) & TCO_TIMEOUT));

@@ -407,7 +407,7 @@ static void test_tco1_status_bits(void)
     set_tco_timeout(&d, ticks);
     load_tco(&d);
     start_tco(&d);
-    clock_step(ticks * TCO_TICK_NSEC);
+    clock_step(global_qtest, ticks * TCO_TICK_NSEC);

     qpci_io_writeb(d.dev, d.tco_io_bar, TCO_DAT_IN, 0);
     qpci_io_writeb(d.dev, d.tco_io_bar, TCO_DAT_OUT, 0);
@@ -436,7 +436,7 @@ static void test_tco2_status_bits(void)
     set_tco_timeout(&d, ticks);
     load_tco(&d);
     start_tco(&d);
-    clock_step(ticks * TCO_TICK_NSEC * 2);
+    clock_step(global_qtest, ticks * TCO_TICK_NSEC * 2);

     val = qpci_io_readw(d.dev, d.tco_io_bar, TCO2_STS);
     ret = val & (TCO_SECOND_TO_STS | TCO_BOOT_STS) ? 1 : 0;
diff --git a/tests/test-arm-mptimer.c b/tests/test-arm-mptimer.c
index 9631c55fe3..0e6484a4a8 100644
--- a/tests/test-arm-mptimer.c
+++ b/tests/test-arm-mptimer.c
@@ -14,7 +14,8 @@
 #define TIMER_BLOCK_SCALE(s)    ((((s) & 0xff) + 1) * 10)

 #define TIMER_BLOCK_STEP(scaler, steps_nb) \
-    clock_step(TIMER_BLOCK_SCALE(scaler) * (int64_t)(steps_nb) + 1)
+    clock_step(global_qtest, \
+               TIMER_BLOCK_SCALE(scaler) * (int64_t)(steps_nb) + 1)

 #define TIMER_BASE_PHYS 0x1e000600

@@ -185,12 +186,14 @@ static void test_timer_periodic(gconstpointer arg)
     timer_start(PERIODIC, scaler);

     while (repeat--) {
-        clock_step(TIMER_BLOCK_SCALE(scaler) * (101 + repeat) + 1);
+        clock_step(global_qtest,
+                   TIMER_BLOCK_SCALE(scaler) * (101 + repeat) + 1);

         g_assert_cmpuint(timer_counter(), ==, 100 - repeat);
         g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 1);

-        clock_step(TIMER_BLOCK_SCALE(scaler) * (101 - repeat) - 1);
+        clock_step(global_qtest,
+                   TIMER_BLOCK_SCALE(scaler) * (101 - repeat) - 1);
     }
 }

@@ -686,10 +689,10 @@ static void test_periodic_counter(gconstpointer arg)
     timer_load(test_load);
     timer_start(PERIODIC, scaler);

-    clock_step(1);
+    clock_step(global_qtest, 1);

     for (test_val = 0; test_val <= test_load; test_val++) {
-        clock_step(TIMER_BLOCK_SCALE(scaler) * test_load);
+        clock_step(global_qtest, TIMER_BLOCK_SCALE(scaler) * test_load);
         g_assert_cmpint(timer_counter(), ==, test_val);
     }
 }
@@ -783,7 +786,7 @@ again:
     timer_reset();
     timer_start(mode, 255);

-    clock_step(100);
+    clock_step(global_qtest, 100);

     g_assert_cmpuint(timer_counter(), ==, 0);

@@ -795,7 +798,7 @@ again:
     timer_load(2);
     timer_start(mode, 255);

-    clock_step(100);
+    clock_step(global_qtest, 100);

     g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);

@@ -811,13 +814,13 @@ again:
     timer_load(UINT32_MAX);
     timer_start(mode, 255);

-    clock_step(100);
+    clock_step(global_qtest, 100);

     g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);

     timer_set_counter(0);

-    clock_step(100);
+    clock_step(global_qtest, 100);

     g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);

@@ -829,13 +832,13 @@ again:
     timer_load(UINT32_MAX);
     timer_start(mode, 255);

-    clock_step(100);
+    clock_step(global_qtest, 100);

     g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);

     timer_load(0);

-    clock_step(100);
+    clock_step(global_qtest, 100);

     g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);

diff --git a/tests/wdt_ib700-test.c b/tests/wdt_ib700-test.c
index a169265e16..05690e5159 100644
--- a/tests/wdt_ib700-test.c
+++ b/tests/wdt_ib700-test.c
@@ -22,29 +22,29 @@ static QDict *ib700_program_and_wait(QTestState *s)
 {
     QDict *event, *data;

-    qtest_clock_step(s, NANOSECONDS_PER_SECOND * 40);
+    clock_step(s, NANOSECONDS_PER_SECOND * 40);
     qmp_check_no_event(s);

     /* 2 second limit */
     qtest_outb(s, 0x443, 14);

     /* Ping */
-    qtest_clock_step(s, NANOSECONDS_PER_SECOND);
+    clock_step(s, NANOSECONDS_PER_SECOND);
     qmp_check_no_event(s);
     qtest_outb(s, 0x443, 14);

     /* Disable */
-    qtest_clock_step(s, NANOSECONDS_PER_SECOND);
+    clock_step(s, NANOSECONDS_PER_SECOND);
     qmp_check_no_event(s);
     qtest_outb(s, 0x441, 1);
-    qtest_clock_step(s, 3 * NANOSECONDS_PER_SECOND);
+    clock_step(s, 3 * NANOSECONDS_PER_SECOND);
     qmp_check_no_event(s);

     /* Enable and let it fire */
     qtest_outb(s, 0x443, 13);
-    qtest_clock_step(s, 3 * NANOSECONDS_PER_SECOND);
+    clock_step(s, 3 * NANOSECONDS_PER_SECOND);
     qmp_check_no_event(s);
-    qtest_clock_step(s, 2 * NANOSECONDS_PER_SECOND);
+    clock_step(s, 2 * NANOSECONDS_PER_SECOND);
     event = qtest_qmp_eventwait_ref(s, "WATCHDOG");
     data = qdict_get_qdict(event, "data");
     QINCREF(data);
-- 
2.13.5

^ permalink raw reply related	[flat|nested] 75+ messages in thread

* [Qemu-devel] [PATCH v7 32/38] libqtest: Merge qtest_irq*() with irq*()
  2017-09-11 17:19 [Qemu-devel] [PATCH v7 00/38] Preliminary libqtest cleanups Eric Blake
                   ` (30 preceding siblings ...)
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 31/38] libqtest: Merge qtest_clock_*() with clock_*() Eric Blake
@ 2017-09-11 17:20 ` Eric Blake
  2017-09-12 10:47   ` Thomas Huth
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 33/38] libqtest: Merge qtest_{in, out}[bwl]() with {in, out}[bwl]() Eric Blake
                   ` (5 subsequent siblings)
  37 siblings, 1 reply; 75+ messages in thread
From: Eric Blake @ 2017-09-11 17:20 UTC (permalink / raw)
  To: qemu-devel; +Cc: armbru, pbonzini, thuth, John Snow, open list:Floppy

Maintaining two layers of libqtest APIs, one that takes an explicit
QTestState object, and the other that uses the implicit global_qtest,
is annoying.  In the interest of getting rid of global implicit
state and having less code to maintain, merge:
 qtest_get_irq()
 qtest_irq_intercept_in()
 qtest_irq_intercept_out()
with their short counterparts.  All callers that previously
used the short form now make it explicit that they are relying on
global_qtest, and later patches can then clean things up to remove
the global variable.

Signed-off-by: Eric Blake <eblake@redhat.com>
---
 tests/libqtest.h         | 47 ++++++-----------------------------------------
 tests/libqtest.c         |  6 +++---
 tests/fdc-test.c         | 48 ++++++++++++++++++++++++------------------------
 tests/ide-test.c         | 17 +++++++++--------
 tests/ipmi-bt-test.c     |  6 +++---
 tests/ipmi-kcs-test.c    |  8 ++++----
 tests/libqos/libqos-pc.c |  2 +-
 tests/rtc-test.c         | 10 +++++-----
 tests/tco-test.c         |  2 +-
 tests/wdt_ib700-test.c   |  8 ++++----
 10 files changed, 60 insertions(+), 94 deletions(-)

diff --git a/tests/libqtest.h b/tests/libqtest.h
index 26d5f37bc9..8398c0fd07 100644
--- a/tests/libqtest.h
+++ b/tests/libqtest.h
@@ -176,33 +176,33 @@ char *qtest_hmp(QTestState *s, const char *fmt, ...) GCC_FMT_ATTR(2, 3);
 char *qtest_hmpv(QTestState *s, const char *fmt, va_list ap);

 /**
- * qtest_get_irq:
+ * get_irq:
  * @s: #QTestState instance to operate on.
  * @num: Interrupt to observe.
  *
  * Returns: The level of the @num interrupt.
  */
-bool qtest_get_irq(QTestState *s, int num);
+bool get_irq(QTestState *s, int num);

 /**
- * qtest_irq_intercept_in:
+ * irq_intercept_in:
  * @s: #QTestState instance to operate on.
  * @string: QOM path of a device.
  *
  * Associate qtest irqs with the GPIO-in pins of the device
  * whose path is specified by @string.
  */
-void qtest_irq_intercept_in(QTestState *s, const char *string);
+void irq_intercept_in(QTestState *s, const char *string);

 /**
- * qtest_irq_intercept_out:
+ * irq_intercept_out:
  * @s: #QTestState instance to operate on.
  * @string: QOM path of a device.
  *
  * Associate qtest irqs with the GPIO-out pins of the device
  * whose path is specified by @string.
  */
-void qtest_irq_intercept_out(QTestState *s, const char *string);
+void irq_intercept_out(QTestState *s, const char *string);

 /**
  * qtest_outb:
@@ -594,41 +594,6 @@ static inline QDict *qmp_eventwait_ref(const char *event)
 char *hmp(const char *fmt, ...) GCC_FMT_ATTR(1, 2);

 /**
- * get_irq:
- * @num: Interrupt to observe.
- *
- * Returns: The level of the @num interrupt.
- */
-static inline bool get_irq(int num)
-{
-    return qtest_get_irq(global_qtest, num);
-}
-
-/**
- * irq_intercept_in:
- * @string: QOM path of a device.
- *
- * Associate qtest irqs with the GPIO-in pins of the device
- * whose path is specified by @string.
- */
-static inline void irq_intercept_in(const char *string)
-{
-    qtest_irq_intercept_in(global_qtest, string);
-}
-
-/**
- * qtest_irq_intercept_out:
- * @string: QOM path of a device.
- *
- * Associate qtest irqs with the GPIO-out pins of the device
- * whose path is specified by @string.
- */
-static inline void irq_intercept_out(const char *string)
-{
-    qtest_irq_intercept_out(global_qtest, string);
-}
-
-/**
  * outb:
  * @addr: I/O port to write to.
  * @value: Value being written.
diff --git a/tests/libqtest.c b/tests/libqtest.c
index 9f5f2cb933..962432a27a 100644
--- a/tests/libqtest.c
+++ b/tests/libqtest.c
@@ -648,7 +648,7 @@ const char *qtest_get_arch(void)
     return end + strlen("/qemu-system-");
 }

-bool qtest_get_irq(QTestState *s, int num)
+bool get_irq(QTestState *s, int num)
 {
     /* dummy operation in order to make sure irq is up to date */
     qtest_inb(s, 0);
@@ -684,13 +684,13 @@ int64_t clock_set(QTestState *s, int64_t val)
     return qtest_clock_rsp(s);
 }

-void qtest_irq_intercept_out(QTestState *s, const char *qom_path)
+void irq_intercept_out(QTestState *s, const char *qom_path)
 {
     qtest_sendf(s, "irq_intercept_out %s\n", qom_path);
     qtest_rsp(s, 0);
 }

-void qtest_irq_intercept_in(QTestState *s, const char *qom_path)
+void irq_intercept_in(QTestState *s, const char *qom_path)
 {
     qtest_sendf(s, "irq_intercept_in %s\n", qom_path);
     qtest_rsp(s, 0);
diff --git a/tests/fdc-test.c b/tests/fdc-test.c
index e79cd0f3b5..584ad746ed 100644
--- a/tests/fdc-test.c
+++ b/tests/fdc-test.c
@@ -99,7 +99,7 @@ static void ack_irq(uint8_t *pcn)
 {
     uint8_t ret;

-    g_assert(get_irq(FLOPPY_IRQ));
+    g_assert(get_irq(global_qtest, FLOPPY_IRQ));
     floppy_send(CMD_SENSE_INT);
     floppy_recv();

@@ -108,7 +108,7 @@ static void ack_irq(uint8_t *pcn)
         *pcn = ret;
     }

-    g_assert(!get_irq(FLOPPY_IRQ));
+    g_assert(!get_irq(global_qtest, FLOPPY_IRQ));
 }

 static uint8_t send_read_command(uint8_t cmd)
@@ -129,7 +129,7 @@ static uint8_t send_read_command(uint8_t cmd)

     floppy_send(cmd);
     floppy_send(head << 2 | drive);
-    g_assert(!get_irq(FLOPPY_IRQ));
+    g_assert(!get_irq(global_qtest, FLOPPY_IRQ));
     floppy_send(cyl);
     floppy_send(head);
     floppy_send(sect_addr);
@@ -185,7 +185,7 @@ static uint8_t send_read_no_dma_command(int nb_sect, uint8_t expected_st0)

     floppy_send(CMD_READ);
     floppy_send(head << 2 | drive);
-    g_assert(!get_irq(FLOPPY_IRQ));
+    g_assert(!get_irq(global_qtest, FLOPPY_IRQ));
     floppy_send(cyl);
     floppy_send(head);
     floppy_send(sect_addr);
@@ -217,7 +217,7 @@ static uint8_t send_read_no_dma_command(int nb_sect, uint8_t expected_st0)

     msr = inb(FLOPPY_BASE + reg_msr);
     assert_bit_set(msr, BUSY | RQM | DIO);
-    g_assert(get_irq(FLOPPY_IRQ));
+    g_assert(get_irq(global_qtest, FLOPPY_IRQ));

     st0 = floppy_recv();
     if (st0 != expected_st0) {
@@ -229,14 +229,14 @@ static uint8_t send_read_no_dma_command(int nb_sect, uint8_t expected_st0)
     floppy_recv();
     floppy_recv();
     floppy_recv();
-    g_assert(get_irq(FLOPPY_IRQ));
+    g_assert(get_irq(global_qtest, FLOPPY_IRQ));
     floppy_recv();

     /* Check that we're back in command phase */
     msr = inb(FLOPPY_BASE + reg_msr);
     assert_bit_clear(msr, BUSY | DIO);
     assert_bit_set(msr, RQM);
-    g_assert(!get_irq(FLOPPY_IRQ));
+    g_assert(!get_irq(global_qtest, FLOPPY_IRQ));

     return ret;
 }
@@ -248,7 +248,7 @@ static void send_seek(int cyl)

     floppy_send(CMD_SEEK);
     floppy_send(head << 2 | drive);
-    g_assert(!get_irq(FLOPPY_IRQ));
+    g_assert(!get_irq(global_qtest, FLOPPY_IRQ));
     floppy_send(cyl);
     ack_irq(NULL);
 }
@@ -363,7 +363,7 @@ static void test_sense_interrupt(void)

     floppy_send(CMD_SEEK);
     floppy_send(head << 2 | drive);
-    g_assert(!get_irq(FLOPPY_IRQ));
+    g_assert(!get_irq(global_qtest, FLOPPY_IRQ));
     floppy_send(cyl);

     floppy_send(CMD_SENSE_INT);
@@ -385,7 +385,7 @@ static void test_relative_seek(void)
     /* Send relative seek to increase track by 1 */
     floppy_send(CMD_RELATIVE_SEEK_IN);
     floppy_send(head << 2 | drive);
-    g_assert(!get_irq(FLOPPY_IRQ));
+    g_assert(!get_irq(global_qtest, FLOPPY_IRQ));
     floppy_send(cyl);

     ack_irq(&pcn);
@@ -394,7 +394,7 @@ static void test_relative_seek(void)
     /* Send relative seek to decrease track by 1 */
     floppy_send(CMD_RELATIVE_SEEK_OUT);
     floppy_send(head << 2 | drive);
-    g_assert(!get_irq(FLOPPY_IRQ));
+    g_assert(!get_irq(global_qtest, FLOPPY_IRQ));
     floppy_send(cyl);

     ack_irq(&pcn);
@@ -413,16 +413,16 @@ static void test_read_id(void)
     send_seek(0);

     floppy_send(CMD_READ_ID);
-    g_assert(!get_irq(FLOPPY_IRQ));
+    g_assert(!get_irq(global_qtest, FLOPPY_IRQ));
     floppy_send(head << 2 | drive);

     msr = inb(FLOPPY_BASE + reg_msr);
-    if (!get_irq(FLOPPY_IRQ)) {
+    if (!get_irq(global_qtest, FLOPPY_IRQ)) {
         assert_bit_set(msr, BUSY);
         assert_bit_clear(msr, RQM);
     }

-    while (!get_irq(FLOPPY_IRQ)) {
+    while (!get_irq(global_qtest, FLOPPY_IRQ)) {
         /* qemu involves a timer with READ ID... */
         clock_step(global_qtest, 1000000000LL / 50);
     }
@@ -436,9 +436,9 @@ static void test_read_id(void)
     cyl = floppy_recv();
     head = floppy_recv();
     floppy_recv();
-    g_assert(get_irq(FLOPPY_IRQ));
+    g_assert(get_irq(global_qtest, FLOPPY_IRQ));
     floppy_recv();
-    g_assert(!get_irq(FLOPPY_IRQ));
+    g_assert(!get_irq(global_qtest, FLOPPY_IRQ));

     g_assert_cmpint(cyl, ==, 0);
     g_assert_cmpint(head, ==, 0);
@@ -450,22 +450,22 @@ static void test_read_id(void)

     floppy_send(CMD_SEEK);
     floppy_send(head << 2 | drive);
-    g_assert(!get_irq(FLOPPY_IRQ));
+    g_assert(!get_irq(global_qtest, FLOPPY_IRQ));
     floppy_send(cyl);
-    g_assert(get_irq(FLOPPY_IRQ));
+    g_assert(get_irq(global_qtest, FLOPPY_IRQ));
     ack_irq(NULL);

     floppy_send(CMD_READ_ID);
-    g_assert(!get_irq(FLOPPY_IRQ));
+    g_assert(!get_irq(global_qtest, FLOPPY_IRQ));
     floppy_send(head << 2 | drive);

     msr = inb(FLOPPY_BASE + reg_msr);
-    if (!get_irq(FLOPPY_IRQ)) {
+    if (!get_irq(global_qtest, FLOPPY_IRQ)) {
         assert_bit_set(msr, BUSY);
         assert_bit_clear(msr, RQM);
     }

-    while (!get_irq(FLOPPY_IRQ)) {
+    while (!get_irq(global_qtest, FLOPPY_IRQ)) {
         /* qemu involves a timer with READ ID... */
         clock_step(global_qtest, 1000000000LL / 50);
     }
@@ -479,9 +479,9 @@ static void test_read_id(void)
     cyl = floppy_recv();
     head = floppy_recv();
     floppy_recv();
-    g_assert(get_irq(FLOPPY_IRQ));
+    g_assert(get_irq(global_qtest, FLOPPY_IRQ));
     floppy_recv();
-    g_assert(!get_irq(FLOPPY_IRQ));
+    g_assert(!get_irq(global_qtest, FLOPPY_IRQ));

     g_assert_cmpint(cyl, ==, 8);
     g_assert_cmpint(head, ==, 1);
@@ -565,7 +565,7 @@ int main(int argc, char **argv)
     g_test_init(&argc, &argv, NULL);

     global_qtest = qtest_start("-device floppy,id=floppy0");
-    qtest_irq_intercept_in(global_qtest, "ioapic");
+    irq_intercept_in(global_qtest, "ioapic");
     qtest_add_func("/fdc/cmos", test_cmos);
     qtest_add_func("/fdc/no_media_on_start", test_no_media_on_start);
     qtest_add_func("/fdc/read_without_media", test_read_without_media);
diff --git a/tests/ide-test.c b/tests/ide-test.c
index 32c8c40294..c22dde55c1 100644
--- a/tests/ide-test.c
+++ b/tests/ide-test.c
@@ -255,14 +255,15 @@ static int send_dma_request(int cmd, uint64_t sector, int nb_sectors,
         status = qpci_io_readb(dev, bmdma_bar, bmreg_status);
     } while ((status & (BM_STS_ACTIVE | BM_STS_INTR)) == BM_STS_ACTIVE);

-    g_assert_cmpint(get_irq(IDE_PRIMARY_IRQ), ==, !!(status & BM_STS_INTR));
+    g_assert_cmpint(get_irq(global_qtest, IDE_PRIMARY_IRQ), ==,
+                    !!(status & BM_STS_INTR));

     /* Check IDE status code */
     assert_bit_set(qpci_io_readb(dev, ide_bar, reg_status), DRDY);
     assert_bit_clear(qpci_io_readb(dev, ide_bar, reg_status), BSY | DRQ);

     /* Reading the status register clears the IRQ */
-    g_assert(!get_irq(IDE_PRIMARY_IRQ));
+    g_assert(!get_irq(global_qtest, IDE_PRIMARY_IRQ));

     /* Stop DMA transfer if still active */
     if (status & BM_STS_ACTIVE) {
@@ -458,7 +459,7 @@ static void test_bmdma_setup(void)
         "-drive file=%s,if=ide,serial=%s,cache=writeback,format=raw "
         "-global ide-hd.ver=%s",
         tmp_path, "testdisk", "version");
-    qtest_irq_intercept_in(global_qtest, "ioapic");
+    irq_intercept_in(global_qtest, "ioapic");
 }

 static void test_bmdma_teardown(void)
@@ -580,7 +581,7 @@ static void test_flush(void)

     dev = get_pci_device(&bmdma_bar, &ide_bar);

-    qtest_irq_intercept_in(global_qtest, "ioapic");
+    irq_intercept_in(global_qtest, "ioapic");

     /* Dirty media so that CMD_FLUSH_CACHE will actually go to disk */
     make_dirty(0);
@@ -631,7 +632,7 @@ static void test_retry_flush(const char *machine)

     dev = get_pci_device(&bmdma_bar, &ide_bar);

-    qtest_irq_intercept_in(global_qtest, "ioapic");
+    irq_intercept_in(global_qtest, "ioapic");

     /* Dirty media so that CMD_FLUSH_CACHE will actually go to disk */
     make_dirty(0);
@@ -784,7 +785,7 @@ static void ide_wait_intr(int irq)

     time(&st);
     while (true) {
-        intr = get_irq(irq);
+        intr = get_irq(global_qtest, irq);
         if (intr) {
             return;
         }
@@ -822,7 +823,7 @@ static void cdrom_pio_impl(int nblocks)
     ide_test_start("-drive if=none,file=%s,media=cdrom,format=raw,id=sr0,index=0 "
                    "-device ide-cd,drive=sr0,bus=ide.0", tmp_path);
     dev = get_pci_device(&bmdma_bar, &ide_bar);
-    qtest_irq_intercept_in(global_qtest, "ioapic");
+    irq_intercept_in(global_qtest, "ioapic");

     /* PACKET command on device 0 */
     qpci_io_writeb(dev, ide_bar, reg_device, 0);
@@ -905,7 +906,7 @@ static void test_cdrom_dma(void)

     ide_test_start("-drive if=none,file=%s,media=cdrom,format=raw,id=sr0,index=0 "
                    "-device ide-cd,drive=sr0,bus=ide.0", tmp_path);
-    qtest_irq_intercept_in(global_qtest, "ioapic");
+    irq_intercept_in(global_qtest, "ioapic");

     guest_buf = guest_alloc(guest_malloc, len);
     prdt[0].addr = cpu_to_le32(guest_buf);
diff --git a/tests/ipmi-bt-test.c b/tests/ipmi-bt-test.c
index 8be18e3f42..f80a9a83c9 100644
--- a/tests/ipmi-bt-test.c
+++ b/tests/ipmi-bt-test.c
@@ -276,10 +276,10 @@ static void bt_cmd(uint8_t *cmd, unsigned int cmd_len,
     bt_wait_b2h_atn();
     if (bt_ints_enabled) {
         g_assert((bt_get_irqreg() & 0x02) == 0x02);
-        g_assert(get_irq(IPMI_IRQ));
+        g_assert(get_irq(global_qtest, IPMI_IRQ));
         bt_write_irqreg(0x03);
     } else {
-        g_assert(!get_irq(IPMI_IRQ));
+        g_assert(!get_irq(global_qtest, IPMI_IRQ));
     }
     IPMI_BT_CTLREG_SET_H_BUSY();
     IPMI_BT_CTLREG_SET_B2H_ATN();
@@ -418,7 +418,7 @@ int main(int argc, char **argv)
         " -chardev socket,id=ipmi0,host=localhost,port=%d,reconnect=10"
         " -device ipmi-bmc-extern,chardev=ipmi0,id=bmc0"
         " -device isa-ipmi-bt,bmc=bmc0", emu_port);
-    qtest_irq_intercept_in(global_qtest, "ioapic");
+    irq_intercept_in(global_qtest, "ioapic");
     qtest_add_func("/ipmi/extern/connect", test_connect);
     qtest_add_func("/ipmi/extern/bt_base", test_bt_base);
     qtest_add_func("/ipmi/extern/bt_enable_irq", test_enable_irq);
diff --git a/tests/ipmi-kcs-test.c b/tests/ipmi-kcs-test.c
index 5acc0d9e35..ddad8dd22f 100644
--- a/tests/ipmi-kcs-test.c
+++ b/tests/ipmi-kcs-test.c
@@ -89,14 +89,14 @@ static void kcs_wait_obf(void)
 static void kcs_clear_obf(void)
 {
     if (kcs_ints_enabled) {
-        g_assert(get_irq(IPMI_IRQ));
+        g_assert(get_irq(global_qtest, IPMI_IRQ));
     } else {
-        g_assert(!get_irq(IPMI_IRQ));
+        g_assert(!get_irq(global_qtest, IPMI_IRQ));
     }
     g_assert(IPMI_KCS_CMDREG_GET_OBF() == 1);
     kcs_get_datareg();
     g_assert(IPMI_KCS_CMDREG_GET_OBF() == 0);
-    g_assert(!get_irq(IPMI_IRQ));
+    g_assert(!get_irq(global_qtest, IPMI_IRQ));
 }

 static void kcs_check_state(uint8_t state)
@@ -277,7 +277,7 @@ int main(int argc, char **argv)

     global_qtest = qtest_start("-device ipmi-bmc-sim,id=bmc0"
                                " -device isa-ipmi-kcs,bmc=bmc0");
-    qtest_irq_intercept_in(global_qtest, "ioapic");
+    irq_intercept_in(global_qtest, "ioapic");
     qtest_add_func("/ipmi/local/kcs_base", test_kcs_base);
     qtest_add_func("/ipmi/local/kcs_abort", test_kcs_abort);
     qtest_add_func("/ipmi/local/kcs_enable_irq", test_enable_irq);
diff --git a/tests/libqos/libqos-pc.c b/tests/libqos/libqos-pc.c
index a9c1aceaa7..df97f0cd03 100644
--- a/tests/libqos/libqos-pc.c
+++ b/tests/libqos/libqos-pc.c
@@ -25,7 +25,7 @@ QOSState *qtest_pc_boot(const char *cmdline_fmt, ...)
     qs = qtest_vboot(&qos_ops, cmdline_fmt, ap);
     va_end(ap);

-    qtest_irq_intercept_in(qs->qts, "ioapic");
+    irq_intercept_in(qs->qts, "ioapic");

     return qs;
 }
diff --git a/tests/rtc-test.c b/tests/rtc-test.c
index 9b98e2d494..8ff201993e 100644
--- a/tests/rtc-test.c
+++ b/tests/rtc-test.c
@@ -277,7 +277,7 @@ static void alarm_time(void)
     /* set DEC mode */
     cmos_write(RTC_REG_B, REG_B_24H | REG_B_DM);

-    g_assert(!get_irq(RTC_ISA_IRQ));
+    g_assert(!get_irq(global_qtest, RTC_ISA_IRQ));
     cmos_read(RTC_REG_C);

     now.tm_sec = (now.tm_sec + 2) % 60;
@@ -287,14 +287,14 @@ static void alarm_time(void)
     cmos_write(RTC_REG_B, cmos_read(RTC_REG_B) | REG_B_AIE);

     for (i = 0; i < 2 + wiggle; i++) {
-        if (get_irq(RTC_ISA_IRQ)) {
+        if (get_irq(global_qtest, RTC_ISA_IRQ)) {
             break;
         }

         clock_step(global_qtest, 1000000000);
     }

-    g_assert(get_irq(RTC_ISA_IRQ));
+    g_assert(get_irq(global_qtest, RTC_ISA_IRQ));
     g_assert((cmos_read(RTC_REG_C) & REG_C_AF) != 0);
     g_assert(cmos_read(RTC_REG_C) == 0);
 }
@@ -644,7 +644,7 @@ static void uip_stuck(void)

 static uint64_t wait_periodic_interrupt(uint64_t real_time)
 {
-    while (!get_irq(RTC_ISA_IRQ)) {
+    while (!get_irq(global_qtest, RTC_ISA_IRQ)) {
         real_time = clock_step_next(global_qtest);
     }

@@ -691,7 +691,7 @@ int main(int argc, char **argv)
     g_test_init(&argc, &argv, NULL);

     s = global_qtest = qtest_start("-rtc clock=vm");
-    qtest_irq_intercept_in(s, "ioapic");
+    irq_intercept_in(s, "ioapic");

     qtest_add_func("/rtc/check-time/bcd", bcd_check_time);
     qtest_add_func("/rtc/check-time/dec", dec_check_time);
diff --git a/tests/tco-test.c b/tests/tco-test.c
index 27e982d8ac..c2abbeeaac 100644
--- a/tests/tco-test.c
+++ b/tests/tco-test.c
@@ -60,7 +60,7 @@ static void test_init(TestData *d)
                       d->noreboot ? "" : "-global ICH9-LPC.noreboot=false",
                       !d->args ? "" : d->args);
     global_qtest = qs;
-    qtest_irq_intercept_in(qs, "ioapic");
+    irq_intercept_in(qs, "ioapic");

     d->bus = qpci_init_pc(qs, NULL);
     d->dev = qpci_device_find(d->bus, QPCI_DEVFN(0x1f, 0x00));
diff --git a/tests/wdt_ib700-test.c b/tests/wdt_ib700-test.c
index 05690e5159..5ce8f4a08a 100644
--- a/tests/wdt_ib700-test.c
+++ b/tests/wdt_ib700-test.c
@@ -58,7 +58,7 @@ static void ib700_pause(void)
     QDict *d;
     QTestState *s = qtest_start("-watchdog-action pause -device ib700");

-    qtest_irq_intercept_in(s, "ioapic");
+    irq_intercept_in(s, "ioapic");
     d = ib700_program_and_wait(s);
     g_assert(!strcmp(qdict_get_str(d, "action"), "pause"));
     QDECREF(d);
@@ -71,7 +71,7 @@ static void ib700_reset(void)
     QDict *d;
     QTestState *s = qtest_start("-watchdog-action reset -device ib700");

-    qtest_irq_intercept_in(s, "ioapic");
+    irq_intercept_in(s, "ioapic");
     d = ib700_program_and_wait(s);
     g_assert(!strcmp(qdict_get_str(d, "action"), "reset"));
     QDECREF(d);
@@ -85,7 +85,7 @@ static void ib700_shutdown(void)
     QTestState *s;

     s = qtest_start("-watchdog-action reset -no-reboot -device ib700");
-    qtest_irq_intercept_in(s, "ioapic");
+    irq_intercept_in(s, "ioapic");
     d = ib700_program_and_wait(s);
     g_assert(!strcmp(qdict_get_str(d, "action"), "reset"));
     QDECREF(d);
@@ -98,7 +98,7 @@ static void ib700_none(void)
     QDict *d;
     QTestState *s = qtest_start("-watchdog-action none -device ib700");

-    qtest_irq_intercept_in(s, "ioapic");
+    irq_intercept_in(s, "ioapic");
     d = ib700_program_and_wait(s);
     g_assert(!strcmp(qdict_get_str(d, "action"), "none"));
     QDECREF(d);
-- 
2.13.5

^ permalink raw reply related	[flat|nested] 75+ messages in thread

* [Qemu-devel] [PATCH v7 33/38] libqtest: Merge qtest_{in, out}[bwl]() with {in, out}[bwl]()
  2017-09-11 17:19 [Qemu-devel] [PATCH v7 00/38] Preliminary libqtest cleanups Eric Blake
                   ` (31 preceding siblings ...)
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 32/38] libqtest: Merge qtest_irq*() with irq*() Eric Blake
@ 2017-09-11 17:20 ` Eric Blake
  2017-09-12 10:49   ` Thomas Huth
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 34/38] libqtest: Merge qtest_{read, write}[bwlq]() with {read, write}[bwlq]() Eric Blake
                   ` (4 subsequent siblings)
  37 siblings, 1 reply; 75+ messages in thread
From: Eric Blake @ 2017-09-11 17:20 UTC (permalink / raw)
  To: qemu-devel; +Cc: armbru, pbonzini, thuth, John Snow, open list:Floppy

Maintaining two layers of libqtest APIs, one that takes an explicit
QTestState object, and the other that uses the implicit global_qtest,
is annoying.  In the interest of getting rid of global implicit
state and having less code to maintain, merge:
 qtest_outb()
 qtest_outw()
 qtest_outl()
 qtest_inb()
 qtest_inw()
 qtest_inl()
with their short counterparts.  All callers that previously
used the short form now make it explicit that they are relying on
global_qtest, and later patches can then clean things up to remove
the global variable.

Signed-off-by: Eric Blake <eblake@redhat.com>
---
 tests/libqtest.h        | 99 ++++++-------------------------------------------
 tests/multiboot/libc.h  |  2 +-
 tests/libqtest.c        | 14 +++----
 tests/boot-order-test.c |  4 +-
 tests/endianness-test.c | 12 +++---
 tests/fdc-test.c        | 77 ++++++++++++++++++++------------------
 tests/hd-geo-test.c     |  4 +-
 tests/ipmi-bt-test.c    | 12 +++---
 tests/ipmi-kcs-test.c   |  8 ++--
 tests/libqos/fw_cfg.c   |  4 +-
 tests/libqos/pci-pc.c   | 44 +++++++++++-----------
 tests/libqos/pci.c      |  2 +-
 tests/m48t59-test.c     |  8 ++--
 tests/multiboot/libc.c  |  2 +-
 tests/pvpanic-test.c    |  4 +-
 tests/rtc-test.c        |  8 ++--
 tests/wdt_ib700-test.c  |  8 ++--
 17 files changed, 120 insertions(+), 192 deletions(-)

diff --git a/tests/libqtest.h b/tests/libqtest.h
index 8398c0fd07..520f745e7b 100644
--- a/tests/libqtest.h
+++ b/tests/libqtest.h
@@ -205,61 +205,61 @@ void irq_intercept_in(QTestState *s, const char *string);
 void irq_intercept_out(QTestState *s, const char *string);

 /**
- * qtest_outb:
+ * outb:
  * @s: #QTestState instance to operate on.
  * @addr: I/O port to write to.
  * @value: Value being written.
  *
  * Write an 8-bit value to an I/O port.
  */
-void qtest_outb(QTestState *s, uint16_t addr, uint8_t value);
+void outb(QTestState *s, uint16_t addr, uint8_t value);

 /**
- * qtest_outw:
+ * outw:
  * @s: #QTestState instance to operate on.
  * @addr: I/O port to write to.
  * @value: Value being written.
  *
  * Write a 16-bit value to an I/O port.
  */
-void qtest_outw(QTestState *s, uint16_t addr, uint16_t value);
+void outw(QTestState *s, uint16_t addr, uint16_t value);

 /**
- * qtest_outl:
+ * outl:
  * @s: #QTestState instance to operate on.
  * @addr: I/O port to write to.
  * @value: Value being written.
  *
  * Write a 32-bit value to an I/O port.
  */
-void qtest_outl(QTestState *s, uint16_t addr, uint32_t value);
+void outl(QTestState *s, uint16_t addr, uint32_t value);

 /**
- * qtest_inb:
+ * inb:
  * @s: #QTestState instance to operate on.
  * @addr: I/O port to read from.
  *
  * Returns an 8-bit value from an I/O port.
  */
-uint8_t qtest_inb(QTestState *s, uint16_t addr);
+uint8_t inb(QTestState *s, uint16_t addr);

 /**
- * qtest_inw:
+ * inw:
  * @s: #QTestState instance to operate on.
  * @addr: I/O port to read from.
  *
  * Returns a 16-bit value from an I/O port.
  */
-uint16_t qtest_inw(QTestState *s, uint16_t addr);
+uint16_t inw(QTestState *s, uint16_t addr);

 /**
- * qtest_inl:
+ * inl:
  * @s: #QTestState instance to operate on.
  * @addr: I/O port to read from.
  *
  * Returns a 32-bit value from an I/O port.
  */
-uint32_t qtest_inl(QTestState *s, uint16_t addr);
+uint32_t inl(QTestState *s, uint16_t addr);

 /**
  * qtest_writeb:
@@ -594,81 +594,6 @@ static inline QDict *qmp_eventwait_ref(const char *event)
 char *hmp(const char *fmt, ...) GCC_FMT_ATTR(1, 2);

 /**
- * outb:
- * @addr: I/O port to write to.
- * @value: Value being written.
- *
- * Write an 8-bit value to an I/O port.
- */
-static inline void outb(uint16_t addr, uint8_t value)
-{
-    qtest_outb(global_qtest, addr, value);
-}
-
-/**
- * outw:
- * @addr: I/O port to write to.
- * @value: Value being written.
- *
- * Write a 16-bit value to an I/O port.
- */
-static inline void outw(uint16_t addr, uint16_t value)
-{
-    qtest_outw(global_qtest, addr, value);
-}
-
-/**
- * outl:
- * @addr: I/O port to write to.
- * @value: Value being written.
- *
- * Write a 32-bit value to an I/O port.
- */
-static inline void outl(uint16_t addr, uint32_t value)
-{
-    qtest_outl(global_qtest, addr, value);
-}
-
-/**
- * inb:
- * @addr: I/O port to read from.
- *
- * Reads an 8-bit value from an I/O port.
- *
- * Returns: Value read.
- */
-static inline uint8_t inb(uint16_t addr)
-{
-    return qtest_inb(global_qtest, addr);
-}
-
-/**
- * inw:
- * @addr: I/O port to read from.
- *
- * Reads a 16-bit value from an I/O port.
- *
- * Returns: Value read.
- */
-static inline uint16_t inw(uint16_t addr)
-{
-    return qtest_inw(global_qtest, addr);
-}
-
-/**
- * inl:
- * @addr: I/O port to read from.
- *
- * Reads a 32-bit value from an I/O port.
- *
- * Returns: Value read.
- */
-static inline uint32_t inl(uint16_t addr)
-{
-    return qtest_inl(global_qtest, addr);
-}
-
-/**
  * writeb:
  * @addr: Guest address to write to.
  * @value: Value being written.
diff --git a/tests/multiboot/libc.h b/tests/multiboot/libc.h
index 04c9922c27..05eb7eafd6 100644
--- a/tests/multiboot/libc.h
+++ b/tests/multiboot/libc.h
@@ -48,7 +48,7 @@ typedef __builtin_va_list       va_list;

 /* Port I/O functions */

-static inline void outb(uint16_t port, uint8_t data)
+static inline void outb(global_qtest, uint16_t port, uint8_t data)
 {
     asm volatile ("outb %0, %1" : : "a" (data), "Nd" (port));
 }
diff --git a/tests/libqtest.c b/tests/libqtest.c
index 962432a27a..1db86b39f1 100644
--- a/tests/libqtest.c
+++ b/tests/libqtest.c
@@ -651,7 +651,7 @@ const char *qtest_get_arch(void)
 bool get_irq(QTestState *s, int num)
 {
     /* dummy operation in order to make sure irq is up to date */
-    qtest_inb(s, 0);
+    inb(s, 0);

     return s->irq_level[num];
 }
@@ -702,17 +702,17 @@ static void qtest_out(QTestState *s, const char *cmd, uint16_t addr, uint32_t va
     qtest_rsp(s, 0);
 }

-void qtest_outb(QTestState *s, uint16_t addr, uint8_t value)
+void outb(QTestState *s, uint16_t addr, uint8_t value)
 {
     qtest_out(s, "outb", addr, value);
 }

-void qtest_outw(QTestState *s, uint16_t addr, uint16_t value)
+void outw(QTestState *s, uint16_t addr, uint16_t value)
 {
     qtest_out(s, "outw", addr, value);
 }

-void qtest_outl(QTestState *s, uint16_t addr, uint32_t value)
+void outl(QTestState *s, uint16_t addr, uint32_t value)
 {
     qtest_out(s, "outl", addr, value);
 }
@@ -732,17 +732,17 @@ static uint32_t qtest_in(QTestState *s, const char *cmd, uint16_t addr)
     return value;
 }

-uint8_t qtest_inb(QTestState *s, uint16_t addr)
+uint8_t inb(QTestState *s, uint16_t addr)
 {
     return qtest_in(s, "inb", addr);
 }

-uint16_t qtest_inw(QTestState *s, uint16_t addr)
+uint16_t inw(QTestState *s, uint16_t addr)
 {
     return qtest_in(s, "inw", addr);
 }

-uint32_t qtest_inl(QTestState *s, uint16_t addr)
+uint32_t inl(QTestState *s, uint16_t addr)
 {
     return qtest_in(s, "inl", addr);
 }
diff --git a/tests/boot-order-test.c b/tests/boot-order-test.c
index e70f5dedba..177aac95ad 100644
--- a/tests/boot-order-test.c
+++ b/tests/boot-order-test.c
@@ -63,8 +63,8 @@ static void test_boot_orders(const char *machine,

 static uint8_t read_mc146818(uint16_t port, uint8_t reg)
 {
-    outb(port, reg);
-    return inb(port + 1);
+    outb(global_qtest, port, reg);
+    return inb(global_qtest, port + 1);
 }

 static uint64_t read_boot_order_pc(void)
diff --git a/tests/endianness-test.c b/tests/endianness-test.c
index 546e0969e4..16b303525e 100644
--- a/tests/endianness-test.c
+++ b/tests/endianness-test.c
@@ -52,7 +52,7 @@ static uint8_t isa_inb(const TestCase *test, uint16_t addr)
 {
     uint8_t value;
     if (test->isa_base == -1) {
-        value = inb(addr);
+        value = inb(global_qtest, addr);
     } else {
         value = readb(test->isa_base + addr);
     }
@@ -63,7 +63,7 @@ static uint16_t isa_inw(const TestCase *test, uint16_t addr)
 {
     uint16_t value;
     if (test->isa_base == -1) {
-        value = inw(addr);
+        value = inw(global_qtest, addr);
     } else {
         value = readw(test->isa_base + addr);
     }
@@ -74,7 +74,7 @@ static uint32_t isa_inl(const TestCase *test, uint16_t addr)
 {
     uint32_t value;
     if (test->isa_base == -1) {
-        value = inl(addr);
+        value = inl(global_qtest, addr);
     } else {
         value = readl(test->isa_base + addr);
     }
@@ -84,7 +84,7 @@ static uint32_t isa_inl(const TestCase *test, uint16_t addr)
 static void isa_outb(const TestCase *test, uint16_t addr, uint8_t value)
 {
     if (test->isa_base == -1) {
-        outb(addr, value);
+        outb(global_qtest, addr, value);
     } else {
         writeb(test->isa_base + addr, value);
     }
@@ -94,7 +94,7 @@ static void isa_outw(const TestCase *test, uint16_t addr, uint16_t value)
 {
     value = test->bswap ? bswap16(value) : value;
     if (test->isa_base == -1) {
-        outw(addr, value);
+        outw(global_qtest, addr, value);
     } else {
         writew(test->isa_base + addr, value);
     }
@@ -104,7 +104,7 @@ static void isa_outl(const TestCase *test, uint16_t addr, uint32_t value)
 {
     value = test->bswap ? bswap32(value) : value;
     if (test->isa_base == -1) {
-        outl(addr, value);
+        outl(global_qtest, addr, value);
     } else {
         writel(test->isa_base + addr, value);
     }
diff --git a/tests/fdc-test.c b/tests/fdc-test.c
index 584ad746ed..7803ff2789 100644
--- a/tests/fdc-test.c
+++ b/tests/fdc-test.c
@@ -77,21 +77,21 @@ static void floppy_send(uint8_t byte)
 {
     uint8_t msr;

-    msr = inb(FLOPPY_BASE + reg_msr);
+    msr = inb(global_qtest, FLOPPY_BASE + reg_msr);
     assert_bit_set(msr, RQM);
     assert_bit_clear(msr, DIO);

-    outb(FLOPPY_BASE + reg_fifo, byte);
+    outb(global_qtest, FLOPPY_BASE + reg_fifo, byte);
 }

 static uint8_t floppy_recv(void)
 {
     uint8_t msr;

-    msr = inb(FLOPPY_BASE + reg_msr);
+    msr = inb(global_qtest, FLOPPY_BASE + reg_msr);
     assert_bit_set(msr, RQM | DIO);

-    return inb(FLOPPY_BASE + reg_fifo);
+    return inb(global_qtest, FLOPPY_BASE + reg_fifo);
 }

 /* pcn: Present Cylinder Number */
@@ -141,7 +141,7 @@ static uint8_t send_read_command(uint8_t cmd)
     uint8_t i = 0;
     uint8_t n = 2;
     for (; i < n; i++) {
-        msr = inb(FLOPPY_BASE + reg_msr);
+        msr = inb(global_qtest, FLOPPY_BASE + reg_msr);
         if (msr == 0xd0) {
             break;
         }
@@ -197,7 +197,7 @@ static uint8_t send_read_no_dma_command(int nb_sect, uint8_t expected_st0)
     uint16_t i = 0;
     uint8_t n = 2;
     for (; i < n; i++) {
-        msr = inb(FLOPPY_BASE + reg_msr);
+        msr = inb(global_qtest, FLOPPY_BASE + reg_msr);
         if (msr == (BUSY | NONDMA | DIO | RQM)) {
             break;
         }
@@ -210,12 +210,12 @@ static uint8_t send_read_no_dma_command(int nb_sect, uint8_t expected_st0)

     /* Non-DMA mode */
     for (i = 0; i < 512 * 2 * nb_sect; i++) {
-        msr = inb(FLOPPY_BASE + reg_msr);
+        msr = inb(global_qtest, FLOPPY_BASE + reg_msr);
         assert_bit_set(msr, BUSY | RQM | DIO);
-        inb(FLOPPY_BASE + reg_fifo);
+        inb(global_qtest, FLOPPY_BASE + reg_fifo);
     }

-    msr = inb(FLOPPY_BASE + reg_msr);
+    msr = inb(global_qtest, FLOPPY_BASE + reg_msr);
     assert_bit_set(msr, BUSY | RQM | DIO);
     g_assert(get_irq(global_qtest, FLOPPY_IRQ));

@@ -233,7 +233,7 @@ static uint8_t send_read_no_dma_command(int nb_sect, uint8_t expected_st0)
     floppy_recv();

     /* Check that we're back in command phase */
-    msr = inb(FLOPPY_BASE + reg_msr);
+    msr = inb(global_qtest, FLOPPY_BASE + reg_msr);
     assert_bit_clear(msr, BUSY | DIO);
     assert_bit_set(msr, RQM);
     g_assert(!get_irq(global_qtest, FLOPPY_IRQ));
@@ -255,8 +255,8 @@ static void send_seek(int cyl)

 static uint8_t cmos_read(uint8_t reg)
 {
-    outb(base + 0, reg);
-    return inb(base + 1);
+    outb(global_qtest, base + 0, reg);
+    return inb(global_qtest, base + 1);
 }

 static void test_cmos(void)
@@ -273,14 +273,14 @@ static void test_no_media_on_start(void)

     /* Media changed bit must be set all time after start if there is
      * no media in drive. */
-    dir = inb(FLOPPY_BASE + reg_dir);
+    dir = inb(global_qtest, FLOPPY_BASE + reg_dir);
     assert_bit_set(dir, DSKCHG);
-    dir = inb(FLOPPY_BASE + reg_dir);
+    dir = inb(global_qtest, FLOPPY_BASE + reg_dir);
     assert_bit_set(dir, DSKCHG);
     send_seek(1);
-    dir = inb(FLOPPY_BASE + reg_dir);
+    dir = inb(global_qtest, FLOPPY_BASE + reg_dir);
     assert_bit_set(dir, DSKCHG);
-    dir = inb(FLOPPY_BASE + reg_dir);
+    dir = inb(global_qtest, FLOPPY_BASE + reg_dir);
     assert_bit_set(dir, DSKCHG);
 }

@@ -302,22 +302,22 @@ static void test_media_insert(void)
                          " 'id':'floppy0', 'filename': %s, 'format': 'raw' }}",
                          test_image);

-    dir = inb(FLOPPY_BASE + reg_dir);
+    dir = inb(global_qtest, FLOPPY_BASE + reg_dir);
     assert_bit_set(dir, DSKCHG);
-    dir = inb(FLOPPY_BASE + reg_dir);
+    dir = inb(global_qtest, FLOPPY_BASE + reg_dir);
     assert_bit_set(dir, DSKCHG);

     send_seek(0);
-    dir = inb(FLOPPY_BASE + reg_dir);
+    dir = inb(global_qtest, FLOPPY_BASE + reg_dir);
     assert_bit_set(dir, DSKCHG);
-    dir = inb(FLOPPY_BASE + reg_dir);
+    dir = inb(global_qtest, FLOPPY_BASE + reg_dir);
     assert_bit_set(dir, DSKCHG);

     /* Step to next track should clear DSKCHG bit. */
     send_seek(1);
-    dir = inb(FLOPPY_BASE + reg_dir);
+    dir = inb(global_qtest, FLOPPY_BASE + reg_dir);
     assert_bit_clear(dir, DSKCHG);
-    dir = inb(FLOPPY_BASE + reg_dir);
+    dir = inb(global_qtest, FLOPPY_BASE + reg_dir);
     assert_bit_clear(dir, DSKCHG);
 }

@@ -332,21 +332,21 @@ static void test_media_change(void)
     qmp_discard_response("{'execute':'eject', 'arguments':{"
                          " 'id':'floppy0' }}");

-    dir = inb(FLOPPY_BASE + reg_dir);
+    dir = inb(global_qtest, FLOPPY_BASE + reg_dir);
     assert_bit_set(dir, DSKCHG);
-    dir = inb(FLOPPY_BASE + reg_dir);
+    dir = inb(global_qtest, FLOPPY_BASE + reg_dir);
     assert_bit_set(dir, DSKCHG);

     send_seek(0);
-    dir = inb(FLOPPY_BASE + reg_dir);
+    dir = inb(global_qtest, FLOPPY_BASE + reg_dir);
     assert_bit_set(dir, DSKCHG);
-    dir = inb(FLOPPY_BASE + reg_dir);
+    dir = inb(global_qtest, FLOPPY_BASE + reg_dir);
     assert_bit_set(dir, DSKCHG);

     send_seek(1);
-    dir = inb(FLOPPY_BASE + reg_dir);
+    dir = inb(global_qtest, FLOPPY_BASE + reg_dir);
     assert_bit_set(dir, DSKCHG);
-    dir = inb(FLOPPY_BASE + reg_dir);
+    dir = inb(global_qtest, FLOPPY_BASE + reg_dir);
     assert_bit_set(dir, DSKCHG);
 }

@@ -416,7 +416,7 @@ static void test_read_id(void)
     g_assert(!get_irq(global_qtest, FLOPPY_IRQ));
     floppy_send(head << 2 | drive);

-    msr = inb(FLOPPY_BASE + reg_msr);
+    msr = inb(global_qtest, FLOPPY_BASE + reg_msr);
     if (!get_irq(global_qtest, FLOPPY_IRQ)) {
         assert_bit_set(msr, BUSY);
         assert_bit_clear(msr, RQM);
@@ -427,7 +427,7 @@ static void test_read_id(void)
         clock_step(global_qtest, 1000000000LL / 50);
     }

-    msr = inb(FLOPPY_BASE + reg_msr);
+    msr = inb(global_qtest, FLOPPY_BASE + reg_msr);
     assert_bit_set(msr, BUSY | RQM | DIO);

     st0 = floppy_recv();
@@ -459,7 +459,7 @@ static void test_read_id(void)
     g_assert(!get_irq(global_qtest, FLOPPY_IRQ));
     floppy_send(head << 2 | drive);

-    msr = inb(FLOPPY_BASE + reg_msr);
+    msr = inb(global_qtest, FLOPPY_BASE + reg_msr);
     if (!get_irq(global_qtest, FLOPPY_IRQ)) {
         assert_bit_set(msr, BUSY);
         assert_bit_clear(msr, RQM);
@@ -470,7 +470,7 @@ static void test_read_id(void)
         clock_step(global_qtest, 1000000000LL / 50);
     }

-    msr = inb(FLOPPY_BASE + reg_msr);
+    msr = inb(global_qtest, FLOPPY_BASE + reg_msr);
     assert_bit_set(msr, BUSY | RQM | DIO);

     st0 = floppy_recv();
@@ -492,7 +492,8 @@ static void test_read_no_dma_1(void)
 {
     uint8_t ret;

-    outb(FLOPPY_BASE + reg_dor, inb(FLOPPY_BASE + reg_dor) & ~0x08);
+    outb(global_qtest, FLOPPY_BASE + reg_dor,
+         inb(global_qtest, FLOPPY_BASE + reg_dor) & ~0x08);
     send_seek(0);
     ret = send_read_no_dma_command(1, 0x04);
     g_assert(ret == 0);
@@ -502,7 +503,8 @@ static void test_read_no_dma_18(void)
 {
     uint8_t ret;

-    outb(FLOPPY_BASE + reg_dor, inb(FLOPPY_BASE + reg_dor) & ~0x08);
+    outb(global_qtest, FLOPPY_BASE + reg_dor,
+         inb(global_qtest, FLOPPY_BASE + reg_dor) & ~0x08);
     send_seek(0);
     ret = send_read_no_dma_command(18, 0x04);
     g_assert(ret == 0);
@@ -512,7 +514,8 @@ static void test_read_no_dma_19(void)
 {
     uint8_t ret;

-    outb(FLOPPY_BASE + reg_dor, inb(FLOPPY_BASE + reg_dor) & ~0x08);
+    outb(global_qtest, FLOPPY_BASE + reg_dor,
+         inb(global_qtest, FLOPPY_BASE + reg_dor) & ~0x08);
     send_seek(0);
     ret = send_read_no_dma_command(19, 0x20);
     g_assert(ret == 0);
@@ -537,8 +540,8 @@ static void fuzz_registers(void)
         reg = (uint8_t)g_test_rand_int_range(0, 8);
         val = (uint8_t)g_test_rand_int_range(0, 256);

-        outb(FLOPPY_BASE + reg, val);
-        inb(FLOPPY_BASE + reg);
+        outb(global_qtest, FLOPPY_BASE + reg, val);
+        inb(global_qtest, FLOPPY_BASE + reg);
     }
 }

diff --git a/tests/hd-geo-test.c b/tests/hd-geo-test.c
index 406eea3c56..98e6621e6c 100644
--- a/tests/hd-geo-test.c
+++ b/tests/hd-geo-test.c
@@ -82,8 +82,8 @@ static void test_cmos_byte(int reg, int expected)
     enum { cmos_base = 0x70 };
     int actual;

-    outb(cmos_base + 0, reg);
-    actual = inb(cmos_base + 1);
+    outb(global_qtest, cmos_base + 0, reg);
+    actual = inb(global_qtest, cmos_base + 1);
     g_assert(actual == expected);
 }

diff --git a/tests/ipmi-bt-test.c b/tests/ipmi-bt-test.c
index f80a9a83c9..e25819fd4f 100644
--- a/tests/ipmi-bt-test.c
+++ b/tests/ipmi-bt-test.c
@@ -66,32 +66,32 @@ static int bt_ints_enabled;

 static uint8_t bt_get_ctrlreg(void)
 {
-    return inb(IPMI_BT_BASE);
+    return inb(global_qtest, IPMI_BT_BASE);
 }

 static void bt_write_ctrlreg(uint8_t val)
 {
-    outb(IPMI_BT_BASE, val);
+    outb(global_qtest, IPMI_BT_BASE, val);
 }

 static uint8_t bt_get_buf(void)
 {
-    return inb(IPMI_BT_BASE + 1);
+    return inb(global_qtest, IPMI_BT_BASE + 1);
 }

 static void bt_write_buf(uint8_t val)
 {
-    outb(IPMI_BT_BASE + 1, val);
+    outb(global_qtest, IPMI_BT_BASE + 1, val);
 }

 static uint8_t bt_get_irqreg(void)
 {
-    return inb(IPMI_BT_BASE + 2);
+    return inb(global_qtest, IPMI_BT_BASE + 2);
 }

 static void bt_write_irqreg(uint8_t val)
 {
-    outb(IPMI_BT_BASE + 2, val);
+    outb(global_qtest, IPMI_BT_BASE + 2, val);
 }

 static void bt_wait_b_busy(void)
diff --git a/tests/ipmi-kcs-test.c b/tests/ipmi-kcs-test.c
index ddad8dd22f..d27b9de185 100644
--- a/tests/ipmi-kcs-test.c
+++ b/tests/ipmi-kcs-test.c
@@ -52,22 +52,22 @@ static int kcs_ints_enabled;

 static uint8_t kcs_get_cmdreg(void)
 {
-    return inb(IPMI_KCS_BASE + 1);
+    return inb(global_qtest, IPMI_KCS_BASE + 1);
 }

 static void kcs_write_cmdreg(uint8_t val)
 {
-    outb(IPMI_KCS_BASE + 1, val);
+    outb(global_qtest, IPMI_KCS_BASE + 1, val);
 }

 static uint8_t kcs_get_datareg(void)
 {
-    return inb(IPMI_KCS_BASE);
+    return inb(global_qtest, IPMI_KCS_BASE);
 }

 static void kcs_write_datareg(uint8_t val)
 {
-    outb(IPMI_KCS_BASE, val);
+    outb(global_qtest, IPMI_KCS_BASE, val);
 }

 static void kcs_wait_ibf(void)
diff --git a/tests/libqos/fw_cfg.c b/tests/libqos/fw_cfg.c
index d0889d1e22..157d5190c6 100644
--- a/tests/libqos/fw_cfg.c
+++ b/tests/libqos/fw_cfg.c
@@ -83,7 +83,7 @@ QFWCFG *mm_fw_cfg_init(QTestState *qts, uint64_t base)

 static void io_fw_cfg_select(QFWCFG *fw_cfg, uint16_t key)
 {
-    qtest_outw(fw_cfg->qts, fw_cfg->base, key);
+    outw(fw_cfg->qts, fw_cfg->base, key);
 }

 static void io_fw_cfg_read(QFWCFG *fw_cfg, void *data, size_t len)
@@ -92,7 +92,7 @@ static void io_fw_cfg_read(QFWCFG *fw_cfg, void *data, size_t len)
     int i;

     for (i = 0; i < len; i++) {
-        ptr[i] = qtest_inb(fw_cfg->qts, fw_cfg->base + 1);
+        ptr[i] = inb(fw_cfg->qts, fw_cfg->base + 1);
     }
 }

diff --git a/tests/libqos/pci-pc.c b/tests/libqos/pci-pc.c
index e5af083f0c..4d8329bef0 100644
--- a/tests/libqos/pci-pc.c
+++ b/tests/libqos/pci-pc.c
@@ -26,44 +26,44 @@ typedef struct QPCIBusPC

 static uint8_t qpci_pc_pio_readb(QPCIBus *bus, uint32_t addr)
 {
-    return qtest_inb(bus->qts, addr);
+    return inb(bus->qts, addr);
 }

 static void qpci_pc_pio_writeb(QPCIBus *bus, uint32_t addr, uint8_t val)
 {
-    qtest_outb(bus->qts, addr, val);
+    outb(bus->qts, addr, val);
 }

 static uint16_t qpci_pc_pio_readw(QPCIBus *bus, uint32_t addr)
 {
-    return qtest_inw(bus->qts, addr);
+    return inw(bus->qts, addr);
 }

 static void qpci_pc_pio_writew(QPCIBus *bus, uint32_t addr, uint16_t val)
 {
-    qtest_outw(bus->qts, addr, val);
+    outw(bus->qts, addr, val);
 }

 static uint32_t qpci_pc_pio_readl(QPCIBus *bus, uint32_t addr)
 {
-    return qtest_inl(bus->qts, addr);
+    return inl(bus->qts, addr);
 }

 static void qpci_pc_pio_writel(QPCIBus *bus, uint32_t addr, uint32_t val)
 {
-    qtest_outl(bus->qts, addr, val);
+    outl(bus->qts, addr, val);
 }

 static uint64_t qpci_pc_pio_readq(QPCIBus *bus, uint32_t addr)
 {
-    return (uint64_t)qtest_inl(bus->qts, addr) +
-        ((uint64_t)qtest_inl(bus->qts, addr + 4) << 32);
+    return (uint64_t)inl(bus->qts, addr) +
+        ((uint64_t)inl(bus->qts, addr + 4) << 32);
 }

 static void qpci_pc_pio_writeq(QPCIBus *bus, uint32_t addr, uint64_t val)
 {
-    qtest_outl(bus->qts, addr, val & 0xffffffff);
-    qtest_outl(bus->qts, addr + 4, val >> 32);
+    outl(bus->qts, addr, val & 0xffffffff);
+    outl(bus->qts, addr + 4, val >> 32);
 }

 static void qpci_pc_memread(QPCIBus *bus, uint32_t addr, void *buf, size_t len)
@@ -79,38 +79,38 @@ static void qpci_pc_memwrite(QPCIBus *bus, uint32_t addr,

 static uint8_t qpci_pc_config_readb(QPCIBus *bus, int devfn, uint8_t offset)
 {
-    qtest_outl(bus->qts, 0xcf8, (1U << 31) | (devfn << 8) | offset);
-    return qtest_inb(bus->qts, 0xcfc);
+    outl(bus->qts, 0xcf8, (1U << 31) | (devfn << 8) | offset);
+    return inb(bus->qts, 0xcfc);
 }

 static uint16_t qpci_pc_config_readw(QPCIBus *bus, int devfn, uint8_t offset)
 {
-    qtest_outl(bus->qts, 0xcf8, (1U << 31) | (devfn << 8) | offset);
-    return qtest_inw(bus->qts, 0xcfc);
+    outl(bus->qts, 0xcf8, (1U << 31) | (devfn << 8) | offset);
+    return inw(bus->qts, 0xcfc);
 }

 static uint32_t qpci_pc_config_readl(QPCIBus *bus, int devfn, uint8_t offset)
 {
-    qtest_outl(bus->qts, 0xcf8, (1U << 31) | (devfn << 8) | offset);
-    return qtest_inl(bus->qts, 0xcfc);
+    outl(bus->qts, 0xcf8, (1U << 31) | (devfn << 8) | offset);
+    return inl(bus->qts, 0xcfc);
 }

 static void qpci_pc_config_writeb(QPCIBus *bus, int devfn, uint8_t offset, uint8_t value)
 {
-    qtest_outl(bus->qts, 0xcf8, (1U << 31) | (devfn << 8) | offset);
-    qtest_outb(bus->qts, 0xcfc, value);
+    outl(bus->qts, 0xcf8, (1U << 31) | (devfn << 8) | offset);
+    outb(bus->qts, 0xcfc, value);
 }

 static void qpci_pc_config_writew(QPCIBus *bus, int devfn, uint8_t offset, uint16_t value)
 {
-    qtest_outl(bus->qts, 0xcf8, (1U << 31) | (devfn << 8) | offset);
-    qtest_outw(bus->qts, 0xcfc, value);
+    outl(bus->qts, 0xcf8, (1U << 31) | (devfn << 8) | offset);
+    outw(bus->qts, 0xcfc, value);
 }

 static void qpci_pc_config_writel(QPCIBus *bus, int devfn, uint8_t offset, uint32_t value)
 {
-    qtest_outl(bus->qts, 0xcf8, (1U << 31) | (devfn << 8) | offset);
-    qtest_outl(bus->qts, 0xcfc, value);
+    outl(bus->qts, 0xcf8, (1U << 31) | (devfn << 8) | offset);
+    outl(bus->qts, 0xcfc, value);
 }

 QPCIBus *qpci_init_pc(QTestState *qts, QGuestAllocator *alloc)
diff --git a/tests/libqos/pci.c b/tests/libqos/pci.c
index d3ee21fdfd..aac42dc3d9 100644
--- a/tests/libqos/pci.c
+++ b/tests/libqos/pci.c
@@ -426,7 +426,7 @@ void qpci_unplug_device_test(QTestState *qts, const char *id, uint8_t slot)
     g_assert(!qdict_haskey(response, "error"));
     QDECREF(response);

-    qtest_outb(qts, ACPI_PCIHP_ADDR + PCI_EJ_BASE, 1 << slot);
+    outb(qts, ACPI_PCIHP_ADDR + PCI_EJ_BASE, 1 << slot);

     qtest_qmp_eventwait(qts, "DEVICE_DELETED");
 }
diff --git a/tests/m48t59-test.c b/tests/m48t59-test.c
index aadd770f4f..7ef3e2d26a 100644
--- a/tests/m48t59-test.c
+++ b/tests/m48t59-test.c
@@ -44,14 +44,14 @@ static void cmos_write_mmio(uint8_t reg, uint8_t val)

 static uint8_t cmos_read_ioio(uint8_t reg)
 {
-    outw(base + 0, reg_base + (uint16_t)reg);
-    return inb(base + 3);
+    outw(global_qtest, base + 0, reg_base + (uint16_t)reg);
+    return inb(global_qtest, base + 3);
 }

 static void cmos_write_ioio(uint8_t reg, uint8_t val)
 {
-    outw(base + 0, reg_base + (uint16_t)reg);
-    outb(base + 3, val);
+    outw(global_qtest, base + 0, reg_base + (uint16_t)reg);
+    outb(global_qtest, base + 3, val);
 }

 static uint8_t cmos_read(uint8_t reg)
diff --git a/tests/multiboot/libc.c b/tests/multiboot/libc.c
index 6df9bda96d..bf02987c5e 100644
--- a/tests/multiboot/libc.c
+++ b/tests/multiboot/libc.c
@@ -36,7 +36,7 @@ void* memcpy(void *dest, const void *src, int n)

 static void print_char(char c)
 {
-    outb(0xe9, c);
+    outb(global_qtest, 0xe9, c);
 }

 static void print_str(char *s)
diff --git a/tests/pvpanic-test.c b/tests/pvpanic-test.c
index c09b3ddac6..6dce9e368a 100644
--- a/tests/pvpanic-test.c
+++ b/tests/pvpanic-test.c
@@ -15,10 +15,10 @@ static void test_panic(void)
     uint8_t val;
     QDict *response, *data;

-    val = inb(0x505);
+    val = inb(global_qtest, 0x505);
     g_assert_cmpuint(val, ==, 1);

-    outb(0x505, 0x1);
+    outb(global_qtest, 0x505, 0x1);

     response = qmp_receive();
     g_assert(qdict_haskey(response, "event"));
diff --git a/tests/rtc-test.c b/tests/rtc-test.c
index 8ff201993e..aea7eaec9f 100644
--- a/tests/rtc-test.c
+++ b/tests/rtc-test.c
@@ -28,14 +28,14 @@ static int bcd2dec(int value)

 static uint8_t cmos_read(uint8_t reg)
 {
-    outb(base + 0, reg);
-    return inb(base + 1);
+    outb(global_qtest, base + 0, reg);
+    return inb(global_qtest, base + 1);
 }

 static void cmos_write(uint8_t reg, uint8_t val)
 {
-    outb(base + 0, reg);
-    outb(base + 1, val);
+    outb(global_qtest, base + 0, reg);
+    outb(global_qtest, base + 1, val);
 }

 static int tm_cmp(struct tm *lhs, struct tm *rhs)
diff --git a/tests/wdt_ib700-test.c b/tests/wdt_ib700-test.c
index 5ce8f4a08a..b2ff95bfd7 100644
--- a/tests/wdt_ib700-test.c
+++ b/tests/wdt_ib700-test.c
@@ -26,22 +26,22 @@ static QDict *ib700_program_and_wait(QTestState *s)
     qmp_check_no_event(s);

     /* 2 second limit */
-    qtest_outb(s, 0x443, 14);
+    outb(s, 0x443, 14);

     /* Ping */
     clock_step(s, NANOSECONDS_PER_SECOND);
     qmp_check_no_event(s);
-    qtest_outb(s, 0x443, 14);
+    outb(s, 0x443, 14);

     /* Disable */
     clock_step(s, NANOSECONDS_PER_SECOND);
     qmp_check_no_event(s);
-    qtest_outb(s, 0x441, 1);
+    outb(s, 0x441, 1);
     clock_step(s, 3 * NANOSECONDS_PER_SECOND);
     qmp_check_no_event(s);

     /* Enable and let it fire */
-    qtest_outb(s, 0x443, 13);
+    outb(s, 0x443, 13);
     clock_step(s, 3 * NANOSECONDS_PER_SECOND);
     qmp_check_no_event(s);
     clock_step(s, 2 * NANOSECONDS_PER_SECOND);
-- 
2.13.5

^ permalink raw reply related	[flat|nested] 75+ messages in thread

* [Qemu-devel] [PATCH v7 34/38] libqtest: Merge qtest_{read, write}[bwlq]() with {read, write}[bwlq]()
  2017-09-11 17:19 [Qemu-devel] [PATCH v7 00/38] Preliminary libqtest cleanups Eric Blake
                   ` (32 preceding siblings ...)
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 33/38] libqtest: Merge qtest_{in, out}[bwl]() with {in, out}[bwl]() Eric Blake
@ 2017-09-11 17:20 ` Eric Blake
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 35/38] libqtest: Merge qtest_{mem, buf}{read, write}() with {mem, buf}{read, write}() Eric Blake
                   ` (3 subsequent siblings)
  37 siblings, 0 replies; 75+ messages in thread
From: Eric Blake @ 2017-09-11 17:20 UTC (permalink / raw)
  To: qemu-devel
  Cc: armbru, pbonzini, thuth, Michael S. Tsirkin, Igor Mammedov,
	David Gibson, Alexander Graf, Stefan Hajnoczi, Ben Warren,
	open list:sPAPR, open list:virtio-blk

Maintaining two layers of libqtest APIs, one that takes an explicit
QTestState object, and the other that uses the implicit global_qtest,
is annoying.  In the interest of getting rid of global implicit
state and having less code to maintain, merge:
 qtest_readb()
 qtest_readw()
 qtest_readl()
 qtest_readq()
 qtest_writeb()
 qtest_writew()
 qtest_writel()
 qtest_writeq()
with their short counterparts.  All callers that previously
used the short form now make it explicit that they are relying on
global_qtest, and later patches can then clean things up to remove
the global variable.

Signed-off-by: Eric Blake <eblake@redhat.com>
---
 tests/libqtest.h           | 132 ++++++---------------------------------------
 tests/acpi-utils.h         |  10 ++--
 tests/libqtest.c           |  16 +++---
 tests/acpi-utils.c         |   2 +-
 tests/bios-tables-test.c   |   8 +--
 tests/boot-order-test.c    |   6 +--
 tests/boot-sector.c        |   4 +-
 tests/endianness-test.c    |  12 ++---
 tests/libqos/fw_cfg.c      |   4 +-
 tests/libqos/i2c-imx.c     |  64 +++++++++++-----------
 tests/libqos/i2c-omap.c    |  42 +++++++--------
 tests/libqos/pci-spapr.c   |  16 +++---
 tests/libqos/rtas.c        |   4 +-
 tests/libqos/virtio-mmio.c |  58 +++++++++-----------
 tests/libqos/virtio-pci.c  |   8 +--
 tests/libqos/virtio.c      |  73 ++++++++++++-------------
 tests/m25p80-test.c        |  80 +++++++++++++--------------
 tests/m48t59-test.c        |   4 +-
 tests/pnv-xscom-test.c     |   2 +-
 tests/prom-env-test.c      |   2 +-
 tests/q35-test.c           |  12 ++---
 tests/tco-test.c           |   4 +-
 tests/test-arm-mptimer.c   |  14 ++---
 tests/vhost-user-test.c    |   2 +-
 tests/virtio-blk-test.c    |  18 +++----
 tests/virtio-scsi-test.c   |   2 +-
 tests/vmgenid-test.c       |   2 +-
 27 files changed, 245 insertions(+), 356 deletions(-)

diff --git a/tests/libqtest.h b/tests/libqtest.h
index 520f745e7b..d0c487cf79 100644
--- a/tests/libqtest.h
+++ b/tests/libqtest.h
@@ -262,47 +262,47 @@ uint16_t inw(QTestState *s, uint16_t addr);
 uint32_t inl(QTestState *s, uint16_t addr);

 /**
- * qtest_writeb:
+ * writeb:
  * @s: #QTestState instance to operate on.
  * @addr: Guest address to write to.
  * @value: Value being written.
  *
  * Writes an 8-bit value to memory.
  */
-void qtest_writeb(QTestState *s, uint64_t addr, uint8_t value);
+void writeb(QTestState *s, uint64_t addr, uint8_t value);

 /**
- * qtest_writew:
+ * writew:
  * @s: #QTestState instance to operate on.
  * @addr: Guest address to write to.
  * @value: Value being written.
  *
  * Writes a 16-bit value to memory.
  */
-void qtest_writew(QTestState *s, uint64_t addr, uint16_t value);
+void writew(QTestState *s, uint64_t addr, uint16_t value);

 /**
- * qtest_writel:
+ * writel:
  * @s: #QTestState instance to operate on.
  * @addr: Guest address to write to.
  * @value: Value being written.
  *
  * Writes a 32-bit value to memory.
  */
-void qtest_writel(QTestState *s, uint64_t addr, uint32_t value);
+void writel(QTestState *s, uint64_t addr, uint32_t value);

 /**
- * qtest_writeq:
+ * writeq:
  * @s: #QTestState instance to operate on.
  * @addr: Guest address to write to.
  * @value: Value being written.
  *
  * Writes a 64-bit value to memory.
  */
-void qtest_writeq(QTestState *s, uint64_t addr, uint64_t value);
+void writeq(QTestState *s, uint64_t addr, uint64_t value);

 /**
- * qtest_readb:
+ * readb:
  * @s: #QTestState instance to operate on.
  * @addr: Guest address to read from.
  *
@@ -310,10 +310,10 @@ void qtest_writeq(QTestState *s, uint64_t addr, uint64_t value);
  *
  * Returns: Value read.
  */
-uint8_t qtest_readb(QTestState *s, uint64_t addr);
+uint8_t readb(QTestState *s, uint64_t addr);

 /**
- * qtest_readw:
+ * readw:
  * @s: #QTestState instance to operate on.
  * @addr: Guest address to read from.
  *
@@ -321,10 +321,10 @@ uint8_t qtest_readb(QTestState *s, uint64_t addr);
  *
  * Returns: Value read.
  */
-uint16_t qtest_readw(QTestState *s, uint64_t addr);
+uint16_t readw(QTestState *s, uint64_t addr);

 /**
- * qtest_readl:
+ * readl:
  * @s: #QTestState instance to operate on.
  * @addr: Guest address to read from.
  *
@@ -332,10 +332,10 @@ uint16_t qtest_readw(QTestState *s, uint64_t addr);
  *
  * Returns: Value read.
  */
-uint32_t qtest_readl(QTestState *s, uint64_t addr);
+uint32_t readl(QTestState *s, uint64_t addr);

 /**
- * qtest_readq:
+ * readq:
  * @s: #QTestState instance to operate on.
  * @addr: Guest address to read from.
  *
@@ -343,7 +343,7 @@ uint32_t qtest_readl(QTestState *s, uint64_t addr);
  *
  * Returns: Value read.
  */
-uint64_t qtest_readq(QTestState *s, uint64_t addr);
+uint64_t readq(QTestState *s, uint64_t addr);

 /**
  * qtest_memread:
@@ -594,106 +594,6 @@ static inline QDict *qmp_eventwait_ref(const char *event)
 char *hmp(const char *fmt, ...) GCC_FMT_ATTR(1, 2);

 /**
- * writeb:
- * @addr: Guest address to write to.
- * @value: Value being written.
- *
- * Writes an 8-bit value to guest memory.
- */
-static inline void writeb(uint64_t addr, uint8_t value)
-{
-    qtest_writeb(global_qtest, addr, value);
-}
-
-/**
- * writew:
- * @addr: Guest address to write to.
- * @value: Value being written.
- *
- * Writes a 16-bit value to guest memory.
- */
-static inline void writew(uint64_t addr, uint16_t value)
-{
-    qtest_writew(global_qtest, addr, value);
-}
-
-/**
- * writel:
- * @addr: Guest address to write to.
- * @value: Value being written.
- *
- * Writes a 32-bit value to guest memory.
- */
-static inline void writel(uint64_t addr, uint32_t value)
-{
-    qtest_writel(global_qtest, addr, value);
-}
-
-/**
- * writeq:
- * @addr: Guest address to write to.
- * @value: Value being written.
- *
- * Writes a 64-bit value to guest memory.
- */
-static inline void writeq(uint64_t addr, uint64_t value)
-{
-    qtest_writeq(global_qtest, addr, value);
-}
-
-/**
- * readb:
- * @addr: Guest address to read from.
- *
- * Reads an 8-bit value from guest memory.
- *
- * Returns: Value read.
- */
-static inline uint8_t readb(uint64_t addr)
-{
-    return qtest_readb(global_qtest, addr);
-}
-
-/**
- * readw:
- * @addr: Guest address to read from.
- *
- * Reads a 16-bit value from guest memory.
- *
- * Returns: Value read.
- */
-static inline uint16_t readw(uint64_t addr)
-{
-    return qtest_readw(global_qtest, addr);
-}
-
-/**
- * readl:
- * @addr: Guest address to read from.
- *
- * Reads a 32-bit value from guest memory.
- *
- * Returns: Value read.
- */
-static inline uint32_t readl(uint64_t addr)
-{
-    return qtest_readl(global_qtest, addr);
-}
-
-/**
- * readq:
- * @addr: Guest address to read from.
- *
- * Reads a 64-bit value from guest memory.
- *
- * Returns: Value read.
- */
-static inline uint64_t readq(uint64_t addr)
-{
-    return qtest_readq(global_qtest, addr);
-}
-
-/**
  * memread:
  * @addr: Guest address to read from.
  * @data: Pointer to where memory contents will be stored.
diff --git a/tests/acpi-utils.h b/tests/acpi-utils.h
index 8ec83f71b1..31eb59837d 100644
--- a/tests/acpi-utils.h
+++ b/tests/acpi-utils.h
@@ -32,21 +32,21 @@ typedef struct {
     do {                                       \
         switch (sizeof(field)) {               \
         case 1:                                \
-            field = qtest_readb(qts, addr);    \
+            field = readb(qts, addr);          \
             break;                             \
         case 2:                                \
-            field = qtest_readw(qts, addr);    \
+            field = readw(qts, addr);          \
             break;                             \
         case 4:                                \
-            field = qtest_readl(qts, addr);    \
+            field = readl(qts, addr);          \
             break;                             \
         case 8:                                \
-            field = qtest_readq(qts, addr);    \
+            field = readq(qts, addr);          \
             break;                             \
         default:                               \
             g_assert(false);                   \
         }                                      \
-        addr += sizeof(field);                  \
+        addr += sizeof(field);                 \
     } while (0);

 #define ACPI_READ_ARRAY_PTR(qts, arr, length, addr)      \
diff --git a/tests/libqtest.c b/tests/libqtest.c
index 1db86b39f1..d9d0402287 100644
--- a/tests/libqtest.c
+++ b/tests/libqtest.c
@@ -754,22 +754,22 @@ static void qtest_write(QTestState *s, const char *cmd, uint64_t addr,
     qtest_rsp(s, 0);
 }

-void qtest_writeb(QTestState *s, uint64_t addr, uint8_t value)
+void writeb(QTestState *s, uint64_t addr, uint8_t value)
 {
     qtest_write(s, "writeb", addr, value);
 }

-void qtest_writew(QTestState *s, uint64_t addr, uint16_t value)
+void writew(QTestState *s, uint64_t addr, uint16_t value)
 {
     qtest_write(s, "writew", addr, value);
 }

-void qtest_writel(QTestState *s, uint64_t addr, uint32_t value)
+void writel(QTestState *s, uint64_t addr, uint32_t value)
 {
     qtest_write(s, "writel", addr, value);
 }

-void qtest_writeq(QTestState *s, uint64_t addr, uint64_t value)
+void writeq(QTestState *s, uint64_t addr, uint64_t value)
 {
     qtest_write(s, "writeq", addr, value);
 }
@@ -789,22 +789,22 @@ static uint64_t qtest_read(QTestState *s, const char *cmd, uint64_t addr)
     return value;
 }

-uint8_t qtest_readb(QTestState *s, uint64_t addr)
+uint8_t readb(QTestState *s, uint64_t addr)
 {
     return qtest_read(s, "readb", addr);
 }

-uint16_t qtest_readw(QTestState *s, uint64_t addr)
+uint16_t readw(QTestState *s, uint64_t addr)
 {
     return qtest_read(s, "readw", addr);
 }

-uint32_t qtest_readl(QTestState *s, uint64_t addr)
+uint32_t readl(QTestState *s, uint64_t addr)
 {
     return qtest_read(s, "readl", addr);
 }

-uint64_t qtest_readq(QTestState *s, uint64_t addr)
+uint64_t readq(QTestState *s, uint64_t addr)
 {
     return qtest_read(s, "readq", addr);
 }
diff --git a/tests/acpi-utils.c b/tests/acpi-utils.c
index 6dc8ca1a8c..e915cc8d57 100644
--- a/tests/acpi-utils.c
+++ b/tests/acpi-utils.c
@@ -42,7 +42,7 @@ uint32_t acpi_find_rsdp_address(QTestState *qts)
         int i;

         for (i = 0; i < sizeof sig - 1; ++i) {
-            sig[i] = qtest_readb(qts, off + i);
+            sig[i] = readb(qts, off + i);
         }

         if (!memcmp(sig, "RSD PTR ", sizeof sig)) {
diff --git a/tests/bios-tables-test.c b/tests/bios-tables-test.c
index c17cd8e1a3..f0a2e94ff1 100644
--- a/tests/bios-tables-test.c
+++ b/tests/bios-tables-test.c
@@ -539,7 +539,7 @@ static void test_smbios_entry_point(test_data *data)
         int i;

         for (i = 0; i < sizeof sig - 1; ++i) {
-            sig[i] = qtest_readb(data->qts, off + i);
+            sig[i] = readb(data->qts, off + i);
         }

         if (!memcmp(sig, "_SM_", sizeof sig)) {
@@ -582,9 +582,9 @@ static void test_smbios_structs(test_data *data)
     for (i = 0; i < ep_table->number_of_structures; i++) {

         /* grab type and formatted area length from struct header */
-        type = qtest_readb(data->qts, addr);
+        type = readb(data->qts, addr);
         g_assert_cmpuint(type, <=, SMBIOS_MAX_TYPE);
-        len = qtest_readb(data->qts, addr + 1);
+        len = readb(data->qts, addr + 1);

         /* single-instance structs must not have been encountered before */
         if (smbios_single_instance(type)) {
@@ -596,7 +596,7 @@ static void test_smbios_structs(test_data *data)
         prv = crt = 1;
         while (prv || crt) {
             prv = crt;
-            crt = qtest_readb(data->qts, addr + len);
+            crt = readb(data->qts, addr + len);
             len++;
         }

diff --git a/tests/boot-order-test.c b/tests/boot-order-test.c
index 177aac95ad..048c8721c6 100644
--- a/tests/boot-order-test.c
+++ b/tests/boot-order-test.c
@@ -108,9 +108,9 @@ static void test_pc_boot_order(void)

 static uint8_t read_m48t59(uint64_t addr, uint16_t reg)
 {
-    writeb(addr, reg & 0xff);
-    writeb(addr + 1, reg >> 8);
-    return readb(addr + 3);
+    writeb(global_qtest, addr, reg & 0xff);
+    writeb(global_qtest, addr + 1, reg >> 8);
+    return readb(global_qtest, addr + 3);
 }

 static uint64_t read_boot_order_prep(void)
diff --git a/tests/boot-sector.c b/tests/boot-sector.c
index 8c8ac7f124..7b69e1d706 100644
--- a/tests/boot-sector.c
+++ b/tests/boot-sector.c
@@ -146,8 +146,8 @@ void boot_sector_test(QTestState *qts)
      * instruction.
      */
     for (i = 0; i < TEST_CYCLES; ++i) {
-        signature_low = qtest_readb(qts, SIGNATURE_ADDR);
-        signature_high = qtest_readb(qts, SIGNATURE_ADDR + 1);
+        signature_low = readb(qts, SIGNATURE_ADDR);
+        signature_high = readb(qts, SIGNATURE_ADDR + 1);
         signature = (signature_high << 8) | signature_low;
         if (signature == SIGNATURE) {
             break;
diff --git a/tests/endianness-test.c b/tests/endianness-test.c
index 16b303525e..bb1fb025a6 100644
--- a/tests/endianness-test.c
+++ b/tests/endianness-test.c
@@ -54,7 +54,7 @@ static uint8_t isa_inb(const TestCase *test, uint16_t addr)
     if (test->isa_base == -1) {
         value = inb(global_qtest, addr);
     } else {
-        value = readb(test->isa_base + addr);
+        value = readb(global_qtest, test->isa_base + addr);
     }
     return value;
 }
@@ -65,7 +65,7 @@ static uint16_t isa_inw(const TestCase *test, uint16_t addr)
     if (test->isa_base == -1) {
         value = inw(global_qtest, addr);
     } else {
-        value = readw(test->isa_base + addr);
+        value = readw(global_qtest, test->isa_base + addr);
     }
     return test->bswap ? bswap16(value) : value;
 }
@@ -76,7 +76,7 @@ static uint32_t isa_inl(const TestCase *test, uint16_t addr)
     if (test->isa_base == -1) {
         value = inl(global_qtest, addr);
     } else {
-        value = readl(test->isa_base + addr);
+        value = readl(global_qtest, test->isa_base + addr);
     }
     return test->bswap ? bswap32(value) : value;
 }
@@ -86,7 +86,7 @@ static void isa_outb(const TestCase *test, uint16_t addr, uint8_t value)
     if (test->isa_base == -1) {
         outb(global_qtest, addr, value);
     } else {
-        writeb(test->isa_base + addr, value);
+        writeb(global_qtest, test->isa_base + addr, value);
     }
 }

@@ -96,7 +96,7 @@ static void isa_outw(const TestCase *test, uint16_t addr, uint16_t value)
     if (test->isa_base == -1) {
         outw(global_qtest, addr, value);
     } else {
-        writew(test->isa_base + addr, value);
+        writew(global_qtest, test->isa_base + addr, value);
     }
 }

@@ -106,7 +106,7 @@ static void isa_outl(const TestCase *test, uint16_t addr, uint32_t value)
     if (test->isa_base == -1) {
         outl(global_qtest, addr, value);
     } else {
-        writel(test->isa_base + addr, value);
+        writel(global_qtest, test->isa_base + addr, value);
     }
 }

diff --git a/tests/libqos/fw_cfg.c b/tests/libqos/fw_cfg.c
index 157d5190c6..667e2ee93a 100644
--- a/tests/libqos/fw_cfg.c
+++ b/tests/libqos/fw_cfg.c
@@ -56,7 +56,7 @@ uint64_t qfw_cfg_get_u64(QFWCFG *fw_cfg, uint16_t key)

 static void mm_fw_cfg_select(QFWCFG *fw_cfg, uint16_t key)
 {
-    qtest_writew(fw_cfg->qts, fw_cfg->base, key);
+    writew(fw_cfg->qts, fw_cfg->base, key);
 }

 static void mm_fw_cfg_read(QFWCFG *fw_cfg, void *data, size_t len)
@@ -65,7 +65,7 @@ static void mm_fw_cfg_read(QFWCFG *fw_cfg, void *data, size_t len)
     int i;

     for (i = 0; i < len; i++) {
-        ptr[i] = qtest_readb(fw_cfg->qts, fw_cfg->base + 2);
+        ptr[i] = readb(fw_cfg->qts, fw_cfg->base + 2);
     }
 }

diff --git a/tests/libqos/i2c-imx.c b/tests/libqos/i2c-imx.c
index 0945f2ecdc..cb5cce701f 100644
--- a/tests/libqos/i2c-imx.c
+++ b/tests/libqos/i2c-imx.c
@@ -40,8 +40,8 @@ typedef struct IMXI2C {
 static void imx_i2c_set_slave_addr(IMXI2C *s, uint8_t addr,
                                    enum IMXI2CDirection direction)
 {
-    qtest_writeb(s->parent.qts, s->addr + I2DR_ADDR,
-                 (addr << 1) | (direction == IMX_I2C_READ ? 1 : 0));
+    writeb(s->parent.qts, s->addr + I2DR_ADDR,
+           (addr << 1) | (direction == IMX_I2C_READ ? 1 : 0));
 }

 static void imx_i2c_send(I2CAdapter *i2c, uint8_t addr,
@@ -63,35 +63,35 @@ static void imx_i2c_send(I2CAdapter *i2c, uint8_t addr,
            I2CR_MTX |
            I2CR_TXAK;

-    qtest_writeb(i2c->qts, s->addr + I2CR_ADDR, data);
-    status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
+    writeb(i2c->qts, s->addr + I2CR_ADDR, data);
+    status = readb(i2c->qts, s->addr + I2SR_ADDR);
     g_assert((status & I2SR_IBB) != 0);

     /* set the slave address */
     imx_i2c_set_slave_addr(s, addr, IMX_I2C_WRITE);
-    status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
+    status = readb(i2c->qts, s->addr + I2SR_ADDR);
     g_assert((status & I2SR_IIF) != 0);
     g_assert((status & I2SR_RXAK) == 0);

     /* ack the interrupt */
-    qtest_writeb(i2c->qts, s->addr + I2SR_ADDR, 0);
-    status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
+    writeb(i2c->qts, s->addr + I2SR_ADDR, 0);
+    status = readb(i2c->qts, s->addr + I2SR_ADDR);
     g_assert((status & I2SR_IIF) == 0);

     while (size < len) {
         /* check we are still busy */
-        status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
+        status = readb(i2c->qts, s->addr + I2SR_ADDR);
         g_assert((status & I2SR_IBB) != 0);

         /* write the data */
-        qtest_writeb(i2c->qts, s->addr + I2DR_ADDR, buf[size]);
-        status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
+        writeb(i2c->qts, s->addr + I2DR_ADDR, buf[size]);
+        status = readb(i2c->qts, s->addr + I2SR_ADDR);
         g_assert((status & I2SR_IIF) != 0);
         g_assert((status & I2SR_RXAK) == 0);

         /* ack the interrupt */
-        qtest_writeb(i2c->qts, s->addr + I2SR_ADDR, 0);
-        status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
+        writeb(i2c->qts, s->addr + I2SR_ADDR, 0);
+        status = readb(i2c->qts, s->addr + I2SR_ADDR);
         g_assert((status & I2SR_IIF) == 0);

         size++;
@@ -99,8 +99,8 @@ static void imx_i2c_send(I2CAdapter *i2c, uint8_t addr,

     /* release the bus */
     data &= ~(I2CR_MSTA | I2CR_MTX);
-    qtest_writeb(i2c->qts, s->addr + I2CR_ADDR, data);
-    status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
+    writeb(i2c->qts, s->addr + I2CR_ADDR, data);
+    status = readb(i2c->qts, s->addr + I2SR_ADDR);
     g_assert((status & I2SR_IBB) == 0);
 }

@@ -123,19 +123,19 @@ static void imx_i2c_recv(I2CAdapter *i2c, uint8_t addr,
            I2CR_MTX |
            I2CR_TXAK;

-    qtest_writeb(i2c->qts, s->addr + I2CR_ADDR, data);
-    status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
+    writeb(i2c->qts, s->addr + I2CR_ADDR, data);
+    status = readb(i2c->qts, s->addr + I2SR_ADDR);
     g_assert((status & I2SR_IBB) != 0);

     /* set the slave address */
     imx_i2c_set_slave_addr(s, addr, IMX_I2C_READ);
-    status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
+    status = readb(i2c->qts, s->addr + I2SR_ADDR);
     g_assert((status & I2SR_IIF) != 0);
     g_assert((status & I2SR_RXAK) == 0);

     /* ack the interrupt */
-    qtest_writeb(i2c->qts, s->addr + I2SR_ADDR, 0);
-    status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
+    writeb(i2c->qts, s->addr + I2SR_ADDR, 0);
+    status = readb(i2c->qts, s->addr + I2SR_ADDR);
     g_assert((status & I2SR_IIF) == 0);

     /* set the bus for read */
@@ -144,23 +144,23 @@ static void imx_i2c_recv(I2CAdapter *i2c, uint8_t addr,
     if (len != 1) {
         data &= ~I2CR_TXAK;
     }
-    qtest_writeb(i2c->qts, s->addr + I2CR_ADDR, data);
-    status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
+    writeb(i2c->qts, s->addr + I2CR_ADDR, data);
+    status = readb(i2c->qts, s->addr + I2SR_ADDR);
     g_assert((status & I2SR_IBB) != 0);

     /* dummy read */
-    qtest_readb(i2c->qts, s->addr + I2DR_ADDR);
-    status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
+    readb(i2c->qts, s->addr + I2DR_ADDR);
+    status = readb(i2c->qts, s->addr + I2SR_ADDR);
     g_assert((status & I2SR_IIF) != 0);

     /* ack the interrupt */
-    qtest_writeb(i2c->qts, s->addr + I2SR_ADDR, 0);
-    status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
+    writeb(i2c->qts, s->addr + I2SR_ADDR, 0);
+    status = readb(i2c->qts, s->addr + I2SR_ADDR);
     g_assert((status & I2SR_IIF) == 0);

     while (size < len) {
         /* check we are still busy */
-        status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
+        status = readb(i2c->qts, s->addr + I2SR_ADDR);
         g_assert((status & I2SR_IBB) != 0);

         if (size == (len - 1)) {
@@ -170,26 +170,26 @@ static void imx_i2c_recv(I2CAdapter *i2c, uint8_t addr,
             /* ack the data read */
             data |= I2CR_TXAK;
         }
-        qtest_writeb(i2c->qts, s->addr + I2CR_ADDR, data);
+        writeb(i2c->qts, s->addr + I2CR_ADDR, data);

         /* read the data */
-        buf[size] = qtest_readb(i2c->qts, s->addr + I2DR_ADDR);
+        buf[size] = readb(i2c->qts, s->addr + I2DR_ADDR);

         if (size != (len - 1)) {
-            status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
+            status = readb(i2c->qts, s->addr + I2SR_ADDR);
             g_assert((status & I2SR_IIF) != 0);

             /* ack the interrupt */
-            qtest_writeb(i2c->qts, s->addr + I2SR_ADDR, 0);
+            writeb(i2c->qts, s->addr + I2SR_ADDR, 0);
         }

-        status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
+        status = readb(i2c->qts, s->addr + I2SR_ADDR);
         g_assert((status & I2SR_IIF) == 0);

         size++;
     }

-    status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
+    status = readb(i2c->qts, s->addr + I2SR_ADDR);
     g_assert((status & I2SR_IBB) == 0);
 }

diff --git a/tests/libqos/i2c-omap.c b/tests/libqos/i2c-omap.c
index 1ef6e7b200..c4fefb64a8 100644
--- a/tests/libqos/i2c-omap.c
+++ b/tests/libqos/i2c-omap.c
@@ -51,8 +51,8 @@ static void omap_i2c_set_slave_addr(OMAPI2C *s, uint8_t addr)
 {
     uint16_t data = addr;

-    qtest_writew(s->parent.qts, s->addr + OMAP_I2C_SA, data);
-    data = qtest_readw(s->parent.qts, s->addr + OMAP_I2C_SA);
+    writew(s->parent.qts, s->addr + OMAP_I2C_SA, data);
+    data = readw(s->parent.qts, s->addr + OMAP_I2C_SA);
     g_assert_cmphex(data, ==, addr);
 }

@@ -65,38 +65,38 @@ static void omap_i2c_send(I2CAdapter *i2c, uint8_t addr,
     omap_i2c_set_slave_addr(s, addr);

     data = len;
-    qtest_writew(i2c->qts, s->addr + OMAP_I2C_CNT, data);
+    writew(i2c->qts, s->addr + OMAP_I2C_CNT, data);

     data = OMAP_I2C_CON_I2C_EN |
            OMAP_I2C_CON_TRX |
            OMAP_I2C_CON_MST |
            OMAP_I2C_CON_STT |
            OMAP_I2C_CON_STP;
-    qtest_writew(i2c->qts, s->addr + OMAP_I2C_CON, data);
-    data = qtest_readw(i2c->qts, s->addr + OMAP_I2C_CON);
+    writew(i2c->qts, s->addr + OMAP_I2C_CON, data);
+    data = readw(i2c->qts, s->addr + OMAP_I2C_CON);
     g_assert((data & OMAP_I2C_CON_STP) != 0);

-    data = qtest_readw(i2c->qts, s->addr + OMAP_I2C_STAT);
+    data = readw(i2c->qts, s->addr + OMAP_I2C_STAT);
     g_assert((data & OMAP_I2C_STAT_NACK) == 0);

     while (len > 1) {
-        data = qtest_readw(i2c->qts, s->addr + OMAP_I2C_STAT);
+        data = readw(i2c->qts, s->addr + OMAP_I2C_STAT);
         g_assert((data & OMAP_I2C_STAT_XRDY) != 0);

         data = buf[0] | ((uint16_t)buf[1] << 8);
-        qtest_writew(i2c->qts, s->addr + OMAP_I2C_DATA, data);
+        writew(i2c->qts, s->addr + OMAP_I2C_DATA, data);
         buf = (uint8_t *)buf + 2;
         len -= 2;
     }
     if (len == 1) {
-        data = qtest_readw(i2c->qts, s->addr + OMAP_I2C_STAT);
+        data = readw(i2c->qts, s->addr + OMAP_I2C_STAT);
         g_assert((data & OMAP_I2C_STAT_XRDY) != 0);

         data = buf[0];
-        qtest_writew(i2c->qts, s->addr + OMAP_I2C_DATA, data);
+        writew(i2c->qts, s->addr + OMAP_I2C_DATA, data);
     }

-    data = qtest_readw(i2c->qts, s->addr + OMAP_I2C_CON);
+    data = readw(i2c->qts, s->addr + OMAP_I2C_CON);
     g_assert((data & OMAP_I2C_CON_STP) == 0);
 }

@@ -109,30 +109,30 @@ static void omap_i2c_recv(I2CAdapter *i2c, uint8_t addr,
     omap_i2c_set_slave_addr(s, addr);

     data = len;
-    qtest_writew(i2c->qts, s->addr + OMAP_I2C_CNT, data);
+    writew(i2c->qts, s->addr + OMAP_I2C_CNT, data);

     data = OMAP_I2C_CON_I2C_EN |
            OMAP_I2C_CON_MST |
            OMAP_I2C_CON_STT |
            OMAP_I2C_CON_STP;
-    qtest_writew(i2c->qts, s->addr + OMAP_I2C_CON, data);
-    data = qtest_readw(i2c->qts, s->addr + OMAP_I2C_CON);
+    writew(i2c->qts, s->addr + OMAP_I2C_CON, data);
+    data = readw(i2c->qts, s->addr + OMAP_I2C_CON);
     g_assert((data & OMAP_I2C_CON_STP) == 0);

-    data = qtest_readw(i2c->qts, s->addr + OMAP_I2C_STAT);
+    data = readw(i2c->qts, s->addr + OMAP_I2C_STAT);
     g_assert((data & OMAP_I2C_STAT_NACK) == 0);

-    data = qtest_readw(i2c->qts, s->addr + OMAP_I2C_CNT);
+    data = readw(i2c->qts, s->addr + OMAP_I2C_CNT);
     g_assert_cmpuint(data, ==, len);

     while (len > 0) {
-        data = qtest_readw(i2c->qts, s->addr + OMAP_I2C_STAT);
+        data = readw(i2c->qts, s->addr + OMAP_I2C_STAT);
         g_assert((data & OMAP_I2C_STAT_RRDY) != 0);
         g_assert((data & OMAP_I2C_STAT_ROVR) == 0);

-        data = qtest_readw(i2c->qts, s->addr + OMAP_I2C_DATA);
+        data = readw(i2c->qts, s->addr + OMAP_I2C_DATA);

-        stat = qtest_readw(i2c->qts, s->addr + OMAP_I2C_STAT);
+        stat = readw(i2c->qts, s->addr + OMAP_I2C_STAT);

         if (unlikely(len == 1)) {
             g_assert((stat & OMAP_I2C_STAT_SBD) != 0);
@@ -148,7 +148,7 @@ static void omap_i2c_recv(I2CAdapter *i2c, uint8_t addr,
         }
     }

-    data = qtest_readw(i2c->qts, s->addr + OMAP_I2C_CON);
+    data = readw(i2c->qts, s->addr + OMAP_I2C_CON);
     g_assert((data & OMAP_I2C_CON_STP) == 0);
 }

@@ -165,7 +165,7 @@ I2CAdapter *omap_i2c_create(QTestState *qts, uint64_t addr)
     i2c->qts = qts;

     /* verify the mmio address by looking for a known signature */
-    data = qtest_readw(qts, addr + OMAP_I2C_REV);
+    data = readw(qts, addr + OMAP_I2C_REV);
     g_assert_cmphex(data, ==, 0x34);

     return i2c;
diff --git a/tests/libqos/pci-spapr.c b/tests/libqos/pci-spapr.c
index 4c29889b0b..7bae94e9bd 100644
--- a/tests/libqos/pci-spapr.c
+++ b/tests/libqos/pci-spapr.c
@@ -45,49 +45,49 @@ typedef struct QPCIBusSPAPR {
 static uint8_t qpci_spapr_pio_readb(QPCIBus *bus, uint32_t addr)
 {
     QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
-    return qtest_readb(bus->qts, s->pio_cpu_base + addr);
+    return readb(bus->qts, s->pio_cpu_base + addr);
 }

 static void qpci_spapr_pio_writeb(QPCIBus *bus, uint32_t addr, uint8_t val)
 {
     QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
-    qtest_writeb(bus->qts, s->pio_cpu_base + addr, val);
+    writeb(bus->qts, s->pio_cpu_base + addr, val);
 }

 static uint16_t qpci_spapr_pio_readw(QPCIBus *bus, uint32_t addr)
 {
     QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
-    return bswap16(qtest_readw(bus->qts, s->pio_cpu_base + addr));
+    return bswap16(readw(bus->qts, s->pio_cpu_base + addr));
 }

 static void qpci_spapr_pio_writew(QPCIBus *bus, uint32_t addr, uint16_t val)
 {
     QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
-    qtest_writew(bus->qts, s->pio_cpu_base + addr, bswap16(val));
+    writew(bus->qts, s->pio_cpu_base + addr, bswap16(val));
 }

 static uint32_t qpci_spapr_pio_readl(QPCIBus *bus, uint32_t addr)
 {
     QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
-    return bswap32(qtest_readl(bus->qts, s->pio_cpu_base + addr));
+    return bswap32(readl(bus->qts, s->pio_cpu_base + addr));
 }

 static void qpci_spapr_pio_writel(QPCIBus *bus, uint32_t addr, uint32_t val)
 {
     QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
-    qtest_writel(bus->qts, s->pio_cpu_base + addr, bswap32(val));
+    writel(bus->qts, s->pio_cpu_base + addr, bswap32(val));
 }

 static uint64_t qpci_spapr_pio_readq(QPCIBus *bus, uint32_t addr)
 {
     QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
-    return bswap64(qtest_readq(bus->qts, s->pio_cpu_base + addr));
+    return bswap64(readq(bus->qts, s->pio_cpu_base + addr));
 }

 static void qpci_spapr_pio_writeq(QPCIBus *bus, uint32_t addr, uint64_t val)
 {
     QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
-    qtest_writeq(bus->qts, s->pio_cpu_base + addr, bswap64(val));
+    writeq(bus->qts, s->pio_cpu_base + addr, bswap64(val));
 }

 static void qpci_spapr_memread(QPCIBus *bus, uint32_t addr,
diff --git a/tests/libqos/rtas.c b/tests/libqos/rtas.c
index d81ff4274d..fd5d34364e 100644
--- a/tests/libqos/rtas.c
+++ b/tests/libqos/rtas.c
@@ -13,7 +13,7 @@ static void qrtas_copy_args(QTestState *qts, uint64_t target_args,
     int i;

     for (i = 0; i < nargs; i++) {
-        qtest_writel(qts, target_args + i * sizeof(uint32_t), args[i]);
+        writel(qts, target_args + i * sizeof(uint32_t), args[i]);
     }
 }

@@ -23,7 +23,7 @@ static void qrtas_copy_ret(QTestState *qts, uint64_t target_ret,
     int i;

     for (i = 0; i < nret; i++) {
-        ret[i] = qtest_readl(qts, target_ret + i * sizeof(uint32_t));
+        ret[i] = readl(qts, target_ret + i * sizeof(uint32_t));
     }
 }

diff --git a/tests/libqos/virtio-mmio.c b/tests/libqos/virtio-mmio.c
index 8d256f6ac9..b2a5b63494 100644
--- a/tests/libqos/virtio-mmio.c
+++ b/tests/libqos/virtio-mmio.c
@@ -18,45 +18,40 @@
 static uint8_t qvirtio_mmio_config_readb(QVirtioDevice *d, uint64_t off)
 {
     QVirtioMMIODevice *dev = (QVirtioMMIODevice *)d;
-    return qtest_readb(d->bus->qts,
-                       dev->addr + QVIRTIO_MMIO_DEVICE_SPECIFIC + off);
+    return readb(d->bus->qts, dev->addr + QVIRTIO_MMIO_DEVICE_SPECIFIC + off);
 }

 static uint16_t qvirtio_mmio_config_readw(QVirtioDevice *d, uint64_t off)
 {
     QVirtioMMIODevice *dev = (QVirtioMMIODevice *)d;
-    return qtest_readw(d->bus->qts,
-                       dev->addr + QVIRTIO_MMIO_DEVICE_SPECIFIC + off);
+    return readw(d->bus->qts, dev->addr + QVIRTIO_MMIO_DEVICE_SPECIFIC + off);
 }

 static uint32_t qvirtio_mmio_config_readl(QVirtioDevice *d, uint64_t off)
 {
     QVirtioMMIODevice *dev = (QVirtioMMIODevice *)d;
-    return qtest_readl(d->bus->qts,
-                       dev->addr + QVIRTIO_MMIO_DEVICE_SPECIFIC + off);
+    return readl(d->bus->qts, dev->addr + QVIRTIO_MMIO_DEVICE_SPECIFIC + off);
 }

 static uint64_t qvirtio_mmio_config_readq(QVirtioDevice *d, uint64_t off)
 {
     QVirtioMMIODevice *dev = (QVirtioMMIODevice *)d;
-    return qtest_readq(d->bus->qts,
-                       dev->addr + QVIRTIO_MMIO_DEVICE_SPECIFIC + off);
+    return readq(d->bus->qts, dev->addr + QVIRTIO_MMIO_DEVICE_SPECIFIC + off);
 }

 static uint32_t qvirtio_mmio_get_features(QVirtioDevice *d)
 {
     QVirtioMMIODevice *dev = (QVirtioMMIODevice *)d;
-    qtest_writel(d->bus->qts, dev->addr + QVIRTIO_MMIO_HOST_FEATURES_SEL, 0);
-    return qtest_readl(d->bus->qts, dev->addr + QVIRTIO_MMIO_HOST_FEATURES);
+    writel(d->bus->qts, dev->addr + QVIRTIO_MMIO_HOST_FEATURES_SEL, 0);
+    return readl(d->bus->qts, dev->addr + QVIRTIO_MMIO_HOST_FEATURES);
 }

 static void qvirtio_mmio_set_features(QVirtioDevice *d, uint32_t features)
 {
     QVirtioMMIODevice *dev = (QVirtioMMIODevice *)d;
     dev->features = features;
-    qtest_writel(d->bus->qts, dev->addr + QVIRTIO_MMIO_GUEST_FEATURES_SEL, 0);
-    qtest_writel(d->bus->qts, dev->addr + QVIRTIO_MMIO_GUEST_FEATURES,
-                 features);
+    writel(d->bus->qts, dev->addr + QVIRTIO_MMIO_GUEST_FEATURES_SEL, 0);
+    writel(d->bus->qts, dev->addr + QVIRTIO_MMIO_GUEST_FEATURES, features);
 }

 static uint32_t qvirtio_mmio_get_guest_features(QVirtioDevice *d)
@@ -68,13 +63,13 @@ static uint32_t qvirtio_mmio_get_guest_features(QVirtioDevice *d)
 static uint8_t qvirtio_mmio_get_status(QVirtioDevice *d)
 {
     QVirtioMMIODevice *dev = (QVirtioMMIODevice *)d;
-    return qtest_readl(d->bus->qts, dev->addr + QVIRTIO_MMIO_DEVICE_STATUS);
+    return readl(d->bus->qts, dev->addr + QVIRTIO_MMIO_DEVICE_STATUS);
 }

 static void qvirtio_mmio_set_status(QVirtioDevice *d, uint8_t status)
 {
     QVirtioMMIODevice *dev = (QVirtioMMIODevice *)d;
-    qtest_writel(d->bus->qts, dev->addr + QVIRTIO_MMIO_DEVICE_STATUS, status);
+    writel(d->bus->qts, dev->addr + QVIRTIO_MMIO_DEVICE_STATUS, status);
 }

 static bool qvirtio_mmio_get_queue_isr_status(QVirtioDevice *d, QVirtQueue *vq)
@@ -82,10 +77,9 @@ static bool qvirtio_mmio_get_queue_isr_status(QVirtioDevice *d, QVirtQueue *vq)
     QVirtioMMIODevice *dev = (QVirtioMMIODevice *)d;
     uint32_t isr;

-    isr = qtest_readl(d->bus->qts,
-                      dev->addr + QVIRTIO_MMIO_INTERRUPT_STATUS) & 1;
+    isr = readl(d->bus->qts, dev->addr + QVIRTIO_MMIO_INTERRUPT_STATUS) & 1;
     if (isr != 0) {
-        qtest_writel(d->bus->qts, dev->addr + QVIRTIO_MMIO_INTERRUPT_ACK, 1);
+        writel(d->bus->qts, dev->addr + QVIRTIO_MMIO_INTERRUPT_ACK, 1);
         return true;
     }

@@ -97,10 +91,9 @@ static bool qvirtio_mmio_get_config_isr_status(QVirtioDevice *d)
     QVirtioMMIODevice *dev = (QVirtioMMIODevice *)d;
     uint32_t isr;

-    isr = qtest_readl(d->bus->qts,
-                      dev->addr + QVIRTIO_MMIO_INTERRUPT_STATUS) & 2;
+    isr = readl(d->bus->qts, dev->addr + QVIRTIO_MMIO_INTERRUPT_STATUS) & 2;
     if (isr != 0) {
-        qtest_writel(d->bus->qts, dev->addr + QVIRTIO_MMIO_INTERRUPT_ACK, 2);
+        writel(d->bus->qts, dev->addr + QVIRTIO_MMIO_INTERRUPT_ACK, 2);
         return true;
     }

@@ -110,22 +103,22 @@ static bool qvirtio_mmio_get_config_isr_status(QVirtioDevice *d)
 static void qvirtio_mmio_queue_select(QVirtioDevice *d, uint16_t index)
 {
     QVirtioMMIODevice *dev = (QVirtioMMIODevice *)d;
-    qtest_writel(d->bus->qts, dev->addr + QVIRTIO_MMIO_QUEUE_SEL, index);
+    writel(d->bus->qts, dev->addr + QVIRTIO_MMIO_QUEUE_SEL, index);

-    g_assert_cmphex(qtest_readl(d->bus->qts,
-                                dev->addr + QVIRTIO_MMIO_QUEUE_PFN), ==, 0);
+    g_assert_cmphex(readl(d->bus->qts,
+                          dev->addr + QVIRTIO_MMIO_QUEUE_PFN), ==, 0);
 }

 static uint16_t qvirtio_mmio_get_queue_size(QVirtioDevice *d)
 {
     QVirtioMMIODevice *dev = (QVirtioMMIODevice *)d;
-    return qtest_readl(d->bus->qts, dev->addr + QVIRTIO_MMIO_QUEUE_NUM_MAX);
+    return readl(d->bus->qts, dev->addr + QVIRTIO_MMIO_QUEUE_NUM_MAX);
 }

 static void qvirtio_mmio_set_queue_address(QVirtioDevice *d, uint32_t pfn)
 {
     QVirtioMMIODevice *dev = (QVirtioMMIODevice *)d;
-    qtest_writel(d->bus->qts, dev->addr + QVIRTIO_MMIO_QUEUE_PFN, pfn);
+    writel(d->bus->qts, dev->addr + QVIRTIO_MMIO_QUEUE_PFN, pfn);
 }

 static QVirtQueue *qvirtio_mmio_virtqueue_setup(QVirtioDevice *d,
@@ -137,8 +130,7 @@ static QVirtQueue *qvirtio_mmio_virtqueue_setup(QVirtioDevice *d,

     vq = g_malloc0(sizeof(*vq));
     qvirtio_mmio_queue_select(d, index);
-    qtest_writel(d->bus->qts, dev->addr + QVIRTIO_MMIO_QUEUE_ALIGN,
-                 dev->page_size);
+    writel(d->bus->qts, dev->addr + QVIRTIO_MMIO_QUEUE_ALIGN, dev->page_size);

     vq->dev = d;
     vq->index = index;
@@ -149,7 +141,7 @@ static QVirtQueue *qvirtio_mmio_virtqueue_setup(QVirtioDevice *d,
     vq->indirect = (dev->features & (1u << VIRTIO_RING_F_INDIRECT_DESC)) != 0;
     vq->event = (dev->features & (1u << VIRTIO_RING_F_EVENT_IDX)) != 0;

-    qtest_writel(d->bus->qts, dev->addr + QVIRTIO_MMIO_QUEUE_NUM, vq->size);
+    writel(d->bus->qts, dev->addr + QVIRTIO_MMIO_QUEUE_NUM, vq->size);

     /* Check different than 0 */
     g_assert_cmpint(vq->size, !=, 0);
@@ -174,7 +166,7 @@ static void qvirtio_mmio_virtqueue_cleanup(QVirtQueue *vq,
 static void qvirtio_mmio_virtqueue_kick(QVirtioDevice *d, QVirtQueue *vq)
 {
     QVirtioMMIODevice *dev = (QVirtioMMIODevice *)d;
-    qtest_writel(d->bus->qts, dev->addr + QVIRTIO_MMIO_QUEUE_NOTIFY, vq->index);
+    writel(d->bus->qts, dev->addr + QVIRTIO_MMIO_QUEUE_NOTIFY, vq->index);
 }

 const QVirtioBus qvirtio_mmio = {
@@ -204,15 +196,15 @@ QVirtioMMIODevice *qvirtio_mmio_init_device(QTestState *qts, uint64_t addr,
     uint32_t magic;
     dev = g_malloc0(sizeof(*dev));

-    magic = qtest_readl(qts, addr + QVIRTIO_MMIO_MAGIC_VALUE);
+    magic = readl(qts, addr + QVIRTIO_MMIO_MAGIC_VALUE);
     g_assert(magic == ('v' | 'i' << 8 | 'r' << 16 | 't' << 24));

     dev->addr = addr;
     dev->page_size = page_size;
-    dev->vdev.device_type = qtest_readl(qts, addr + QVIRTIO_MMIO_DEVICE_ID);
+    dev->vdev.device_type = readl(qts, addr + QVIRTIO_MMIO_DEVICE_ID);
     dev->vdev.bus = qvirtio_init_bus(qts, &qvirtio_mmio);

-    qtest_writel(qts, addr + QVIRTIO_MMIO_GUEST_PAGE_SIZE, page_size);
+    writel(qts, addr + QVIRTIO_MMIO_GUEST_PAGE_SIZE, page_size);

     return dev;
 }
diff --git a/tests/libqos/virtio-pci.c b/tests/libqos/virtio-pci.c
index a7b17b3ba2..ef7d6da153 100644
--- a/tests/libqos/virtio-pci.c
+++ b/tests/libqos/virtio-pci.c
@@ -168,9 +168,9 @@ static bool qvirtio_pci_get_queue_isr_status(QVirtioDevice *d, QVirtQueue *vq)
             /* No ISR checking should be done if masked, but read anyway */
             return qpci_msix_pending(dev->pdev, vqpci->msix_entry);
         } else {
-            data = qtest_readl(d->bus->qts, vqpci->msix_addr);
+            data = readl(d->bus->qts, vqpci->msix_addr);
             if (data == vqpci->msix_data) {
-                qtest_writel(d->bus->qts, vqpci->msix_addr, 0);
+                writel(d->bus->qts, vqpci->msix_addr, 0);
                 return true;
             } else {
                 return false;
@@ -192,9 +192,9 @@ static bool qvirtio_pci_get_config_isr_status(QVirtioDevice *d)
             /* No ISR checking should be done if masked, but read anyway */
             return qpci_msix_pending(dev->pdev, dev->config_msix_entry);
         } else {
-            data = qtest_readl(d->bus->qts, dev->config_msix_addr);
+            data = readl(d->bus->qts, dev->config_msix_addr);
             if (data == dev->config_msix_data) {
-                qtest_writel(d->bus->qts, dev->config_msix_addr, 0);
+                writel(d->bus->qts, dev->config_msix_addr, 0);
                 return true;
             } else {
                 return false;
diff --git a/tests/libqos/virtio.c b/tests/libqos/virtio.c
index 92e47e78f2..e5252052d9 100644
--- a/tests/libqos/virtio.c
+++ b/tests/libqos/virtio.c
@@ -118,7 +118,7 @@ uint8_t qvirtio_wait_status_byte_no_isr(QVirtioDevice *d,
     gint64 start_time = g_get_monotonic_time();
     uint8_t val;

-    while ((val = qtest_readb(d->bus->qts, addr)) == 0xff) {
+    while ((val = readb(d->bus->qts, addr)) == 0xff) {
         clock_step(d->bus->qts, 100);
         g_assert(!d->bus->get_queue_isr_status(d, vq));
         g_assert(g_get_monotonic_time() - start_time <= timeout_us);
@@ -179,23 +179,23 @@ void qvring_init(const QGuestAllocator *alloc, QVirtQueue *vq, uint64_t addr)

     for (i = 0; i < vq->size - 1; i++) {
         /* vq->desc[i].addr */
-        qtest_writeq(vq->dev->bus->qts, vq->desc + (16 * i), 0);
+        writeq(vq->dev->bus->qts, vq->desc + (16 * i), 0);
         /* vq->desc[i].next */
-        qtest_writew(vq->dev->bus->qts, vq->desc + (16 * i) + 14, i + 1);
+        writew(vq->dev->bus->qts, vq->desc + (16 * i) + 14, i + 1);
     }

     /* vq->avail->flags */
-    qtest_writew(vq->dev->bus->qts, vq->avail, 0);
+    writew(vq->dev->bus->qts, vq->avail, 0);
     /* vq->avail->idx */
-    qtest_writew(vq->dev->bus->qts, vq->avail + 2, 0);
+    writew(vq->dev->bus->qts, vq->avail + 2, 0);
     /* vq->avail->used_event */
-    qtest_writew(vq->dev->bus->qts, vq->avail + 4 + (2 * vq->size), 0);
+    writew(vq->dev->bus->qts, vq->avail + 4 + (2 * vq->size), 0);

     /* vq->used->flags */
-    qtest_writew(vq->dev->bus->qts, vq->used, 0);
+    writew(vq->dev->bus->qts, vq->used, 0);
     /* vq->used->avail_event */
-    qtest_writew(vq->dev->bus->qts,
-                 vq->used + 2 + sizeof(struct vring_used_elem) * vq->size, 0);
+    writew(vq->dev->bus->qts,
+           vq->used + 2 + sizeof(struct vring_used_elem) * vq->size, 0);
 }

 QVRingIndirectDesc *qvring_indirect_desc_setup(QVirtioDevice *d,
@@ -210,12 +210,11 @@ QVRingIndirectDesc *qvring_indirect_desc_setup(QVirtioDevice *d,

     for (i = 0; i < elem - 1; ++i) {
         /* indirect->desc[i].addr */
-        qtest_writeq(d->bus->qts, indirect->desc + (16 * i), 0);
+        writeq(d->bus->qts, indirect->desc + (16 * i), 0);
         /* indirect->desc[i].flags */
-        qtest_writew(d->bus->qts, indirect->desc + (16 * i) + 12,
-                     VRING_DESC_F_NEXT);
+        writew(d->bus->qts, indirect->desc + (16 * i) + 12, VRING_DESC_F_NEXT);
         /* indirect->desc[i].next */
-        qtest_writew(d->bus->qts, indirect->desc + (16 * i) + 14, i + 1);
+        writew(d->bus->qts, indirect->desc + (16 * i) + 14, i + 1);
     }

     return indirect;
@@ -228,18 +227,18 @@ void qvring_indirect_desc_add(QTestState *qts, QVRingIndirectDesc *indirect,

     g_assert_cmpint(indirect->index, <, indirect->elem);

-    flags = qtest_readw(qts, indirect->desc + (16 * indirect->index) + 12);
+    flags = readw(qts, indirect->desc + (16 * indirect->index) + 12);

     if (write) {
         flags |= VRING_DESC_F_WRITE;
     }

     /* indirect->desc[indirect->index].addr */
-    qtest_writeq(qts, indirect->desc + (16 * indirect->index), data);
+    writeq(qts, indirect->desc + (16 * indirect->index), data);
     /* indirect->desc[indirect->index].len */
-    qtest_writel(qts, indirect->desc + (16 * indirect->index) + 8, len);
+    writel(qts, indirect->desc + (16 * indirect->index) + 8, len);
     /* indirect->desc[indirect->index].flags */
-    qtest_writew(qts, indirect->desc + (16 * indirect->index) + 12, flags);
+    writew(qts, indirect->desc + (16 * indirect->index) + 12, flags);

     indirect->index++;
 }
@@ -259,12 +258,11 @@ uint32_t qvirtqueue_add(QVirtQueue *vq, uint64_t data, uint32_t len, bool write,
     }

     /* vq->desc[vq->free_head].addr */
-    qtest_writeq(vq->dev->bus->qts, vq->desc + (16 * vq->free_head), data);
+    writeq(vq->dev->bus->qts, vq->desc + (16 * vq->free_head), data);
     /* vq->desc[vq->free_head].len */
-    qtest_writel(vq->dev->bus->qts, vq->desc + (16 * vq->free_head) + 8, len);
+    writel(vq->dev->bus->qts, vq->desc + (16 * vq->free_head) + 8, len);
     /* vq->desc[vq->free_head].flags */
-    qtest_writew(vq->dev->bus->qts, vq->desc + (16 * vq->free_head) + 12,
-                 flags);
+    writew(vq->dev->bus->qts, vq->desc + (16 * vq->free_head) + 12, flags);

     return vq->free_head++; /* Return and increase, in this order */
 }
@@ -278,14 +276,13 @@ uint32_t qvirtqueue_add_indirect(QVirtQueue *vq, QVRingIndirectDesc *indirect)
     vq->num_free--;

     /* vq->desc[vq->free_head].addr */
-    qtest_writeq(vq->dev->bus->qts, vq->desc + (16 * vq->free_head),
-                 indirect->desc);
+    writeq(vq->dev->bus->qts, vq->desc + (16 * vq->free_head), indirect->desc);
     /* vq->desc[vq->free_head].len */
-    qtest_writel(vq->dev->bus->qts, vq->desc + (16 * vq->free_head) + 8,
-                 sizeof(struct vring_desc) * indirect->elem);
+    writel(vq->dev->bus->qts, vq->desc + (16 * vq->free_head) + 8,
+           sizeof(struct vring_desc) * indirect->elem);
     /* vq->desc[vq->free_head].flags */
-    qtest_writew(vq->dev->bus->qts,
-                 vq->desc + (16 * vq->free_head) + 12, VRING_DESC_F_INDIRECT);
+    writew(vq->dev->bus->qts,
+           vq->desc + (16 * vq->free_head) + 12, VRING_DESC_F_INDIRECT);

     return vq->free_head++; /* Return and increase, in this order */
 }
@@ -293,7 +290,7 @@ uint32_t qvirtqueue_add_indirect(QVirtQueue *vq, QVRingIndirectDesc *indirect)
 void qvirtqueue_kick(QVirtioDevice *d, QVirtQueue *vq, uint32_t free_head)
 {
     /* vq->avail->idx */
-    uint16_t idx = qtest_readw(d->bus->qts, vq->avail + 2);
+    uint16_t idx = readw(d->bus->qts, vq->avail + 2);
     /* vq->used->flags */
     uint16_t flags;
     /* vq->used->avail_event */
@@ -302,15 +299,14 @@ void qvirtqueue_kick(QVirtioDevice *d, QVirtQueue *vq, uint32_t free_head)
     assert(vq->dev == d);

     /* vq->avail->ring[idx % vq->size] */
-    qtest_writew(d->bus->qts, vq->avail + 4 + (2 * (idx % vq->size)),
-                 free_head);
+    writew(d->bus->qts, vq->avail + 4 + (2 * (idx % vq->size)), free_head);
     /* vq->avail->idx */
-    qtest_writew(d->bus->qts, vq->avail + 2, idx + 1);
+    writew(d->bus->qts, vq->avail + 2, idx + 1);

     /* Must read after idx is updated */
-    flags = qtest_readw(d->bus->qts, vq->avail);
-    avail_event = qtest_readw(d->bus->qts, vq->used + 4 +
-                              sizeof(struct vring_used_elem) * vq->size);
+    flags = readw(d->bus->qts, vq->avail);
+    avail_event = readw(d->bus->qts, vq->used + 4 +
+                        sizeof(struct vring_used_elem) * vq->size);

     /* < 1 because we add elements to avail queue one by one */
     if ((flags & VRING_USED_F_NO_NOTIFY) == 0 &&
@@ -331,8 +327,7 @@ bool qvirtqueue_get_buf(QVirtQueue *vq, uint32_t *desc_idx)
 {
     uint16_t idx;

-    idx = qtest_readw(vq->dev->bus->qts,
-                      vq->used + offsetof(struct vring_used, idx));
+    idx = readw(vq->dev->bus->qts, vq->used + offsetof(struct vring_used, idx));
     if (idx == vq->last_used_idx) {
         return false;
     }
@@ -344,8 +339,8 @@ bool qvirtqueue_get_buf(QVirtQueue *vq, uint32_t *desc_idx)
                     offsetof(struct vring_used, ring) +
                     (vq->last_used_idx % vq->size) *
                     sizeof(struct vring_used_elem);
-        *desc_idx = qtest_readl(vq->dev->bus->qts, elem_addr +
-                                offsetof(struct vring_used_elem, id));
+        *desc_idx = readl(vq->dev->bus->qts, elem_addr +
+                          offsetof(struct vring_used_elem, id));
     }

     vq->last_used_idx++;
@@ -357,5 +352,5 @@ void qvirtqueue_set_used_event(QVirtQueue *vq, uint16_t idx)
     g_assert(vq->event);

     /* vq->avail->used_event */
-    qtest_writew(vq->dev->bus->qts, vq->avail + 4 + (2 * vq->size), idx);
+    writew(vq->dev->bus->qts, vq->avail + 4 + (2 * vq->size), idx);
 }
diff --git a/tests/m25p80-test.c b/tests/m25p80-test.c
index c276e738e9..a480776bb5 100644
--- a/tests/m25p80-test.c
+++ b/tests/m25p80-test.c
@@ -75,53 +75,53 @@ static inline uint32_t make_be32(uint32_t data)

 static void spi_conf(uint32_t value)
 {
-    uint32_t conf = readl(ASPEED_FMC_BASE + R_CONF);
+    uint32_t conf = readl(global_qtest, ASPEED_FMC_BASE + R_CONF);

     conf |= value;
-    writel(ASPEED_FMC_BASE + R_CONF, conf);
+    writel(global_qtest, ASPEED_FMC_BASE + R_CONF, conf);
 }

 static void spi_conf_remove(uint32_t value)
 {
-    uint32_t conf = readl(ASPEED_FMC_BASE + R_CONF);
+    uint32_t conf = readl(global_qtest, ASPEED_FMC_BASE + R_CONF);

     conf &= ~value;
-    writel(ASPEED_FMC_BASE + R_CONF, conf);
+    writel(global_qtest, ASPEED_FMC_BASE + R_CONF, conf);
 }

 static void spi_ce_ctrl(uint32_t value)
 {
-    uint32_t conf = readl(ASPEED_FMC_BASE + R_CE_CTRL);
+    uint32_t conf = readl(global_qtest, ASPEED_FMC_BASE + R_CE_CTRL);

     conf |= value;
-    writel(ASPEED_FMC_BASE + R_CE_CTRL, conf);
+    writel(global_qtest, ASPEED_FMC_BASE + R_CE_CTRL, conf);
 }

 static void spi_ctrl_setmode(uint8_t mode, uint8_t cmd)
 {
-    uint32_t ctrl = readl(ASPEED_FMC_BASE + R_CTRL0);
+    uint32_t ctrl = readl(global_qtest, ASPEED_FMC_BASE + R_CTRL0);
     ctrl &= ~(CTRL_USERMODE | 0xff << 16);
     ctrl |= mode | (cmd << 16);
-    writel(ASPEED_FMC_BASE + R_CTRL0, ctrl);
+    writel(global_qtest, ASPEED_FMC_BASE + R_CTRL0, ctrl);
 }

 static void spi_ctrl_start_user(void)
 {
-    uint32_t ctrl = readl(ASPEED_FMC_BASE + R_CTRL0);
+    uint32_t ctrl = readl(global_qtest, ASPEED_FMC_BASE + R_CTRL0);

     ctrl |= CTRL_USERMODE | CTRL_CE_STOP_ACTIVE;
-    writel(ASPEED_FMC_BASE + R_CTRL0, ctrl);
+    writel(global_qtest, ASPEED_FMC_BASE + R_CTRL0, ctrl);

     ctrl &= ~CTRL_CE_STOP_ACTIVE;
-    writel(ASPEED_FMC_BASE + R_CTRL0, ctrl);
+    writel(global_qtest, ASPEED_FMC_BASE + R_CTRL0, ctrl);
 }

 static void spi_ctrl_stop_user(void)
 {
-    uint32_t ctrl = readl(ASPEED_FMC_BASE + R_CTRL0);
+    uint32_t ctrl = readl(global_qtest, ASPEED_FMC_BASE + R_CTRL0);

     ctrl |= CTRL_USERMODE | CTRL_CE_STOP_ACTIVE;
-    writel(ASPEED_FMC_BASE + R_CTRL0, ctrl);
+    writel(global_qtest, ASPEED_FMC_BASE + R_CTRL0, ctrl);
 }

 static void flash_reset(void)
@@ -129,8 +129,8 @@ static void flash_reset(void)
     spi_conf(CONF_ENABLE_W0);

     spi_ctrl_start_user();
-    writeb(ASPEED_FLASH_BASE, RESET_ENABLE);
-    writeb(ASPEED_FLASH_BASE, RESET_MEMORY);
+    writeb(global_qtest, ASPEED_FLASH_BASE, RESET_ENABLE);
+    writeb(global_qtest, ASPEED_FLASH_BASE, RESET_MEMORY);
     spi_ctrl_stop_user();

     spi_conf_remove(CONF_ENABLE_W0);
@@ -143,10 +143,10 @@ static void test_read_jedec(void)
     spi_conf(CONF_ENABLE_W0);

     spi_ctrl_start_user();
-    writeb(ASPEED_FLASH_BASE, JEDEC_READ);
-    jedec |= readb(ASPEED_FLASH_BASE) << 16;
-    jedec |= readb(ASPEED_FLASH_BASE) << 8;
-    jedec |= readb(ASPEED_FLASH_BASE);
+    writeb(global_qtest, ASPEED_FLASH_BASE, JEDEC_READ);
+    jedec |= readb(global_qtest, ASPEED_FLASH_BASE) << 16;
+    jedec |= readb(global_qtest, ASPEED_FLASH_BASE) << 8;
+    jedec |= readb(global_qtest, ASPEED_FLASH_BASE);
     spi_ctrl_stop_user();

     flash_reset();
@@ -160,13 +160,13 @@ static void read_page(uint32_t addr, uint32_t *page)

     spi_ctrl_start_user();

-    writeb(ASPEED_FLASH_BASE, EN_4BYTE_ADDR);
-    writeb(ASPEED_FLASH_BASE, READ);
-    writel(ASPEED_FLASH_BASE, make_be32(addr));
+    writeb(global_qtest, ASPEED_FLASH_BASE, EN_4BYTE_ADDR);
+    writeb(global_qtest, ASPEED_FLASH_BASE, READ);
+    writel(global_qtest, ASPEED_FLASH_BASE, make_be32(addr));

     /* Continuous read are supported */
     for (i = 0; i < PAGE_SIZE / 4; i++) {
-        page[i] = make_be32(readl(ASPEED_FLASH_BASE));
+        page[i] = make_be32(readl(global_qtest, ASPEED_FLASH_BASE));
     }
     spi_ctrl_stop_user();
 }
@@ -179,7 +179,8 @@ static void read_page_mem(uint32_t addr, uint32_t *page)
     spi_ctrl_setmode(CTRL_READMODE, READ);

     for (i = 0; i < PAGE_SIZE / 4; i++) {
-        page[i] = make_be32(readl(ASPEED_FLASH_BASE + addr + i * 4));
+        page[i] = make_be32(readl(global_qtest,
+                                  ASPEED_FLASH_BASE + addr + i * 4));
     }
 }

@@ -192,10 +193,10 @@ static void test_erase_sector(void)
     spi_conf(CONF_ENABLE_W0);

     spi_ctrl_start_user();
-    writeb(ASPEED_FLASH_BASE, WREN);
-    writeb(ASPEED_FLASH_BASE, EN_4BYTE_ADDR);
-    writeb(ASPEED_FLASH_BASE, ERASE_SECTOR);
-    writel(ASPEED_FLASH_BASE, make_be32(some_page_addr));
+    writeb(global_qtest, ASPEED_FLASH_BASE, WREN);
+    writeb(global_qtest, ASPEED_FLASH_BASE, EN_4BYTE_ADDR);
+    writeb(global_qtest, ASPEED_FLASH_BASE, ERASE_SECTOR);
+    writel(global_qtest, ASPEED_FLASH_BASE, make_be32(some_page_addr));
     spi_ctrl_stop_user();

     /* Previous page should be full of zeroes as backend is not
@@ -230,8 +231,8 @@ static void test_erase_all(void)
     }

     spi_ctrl_start_user();
-    writeb(ASPEED_FLASH_BASE, WREN);
-    writeb(ASPEED_FLASH_BASE, BULK_ERASE);
+    writeb(global_qtest, ASPEED_FLASH_BASE, WREN);
+    writeb(global_qtest, ASPEED_FLASH_BASE, BULK_ERASE);
     spi_ctrl_stop_user();

     /* Recheck that some random page */
@@ -253,14 +254,15 @@ static void test_write_page(void)
     spi_conf(CONF_ENABLE_W0);

     spi_ctrl_start_user();
-    writeb(ASPEED_FLASH_BASE, EN_4BYTE_ADDR);
-    writeb(ASPEED_FLASH_BASE, WREN);
-    writeb(ASPEED_FLASH_BASE, PP);
-    writel(ASPEED_FLASH_BASE, make_be32(my_page_addr));
+    writeb(global_qtest, ASPEED_FLASH_BASE, EN_4BYTE_ADDR);
+    writeb(global_qtest, ASPEED_FLASH_BASE, WREN);
+    writeb(global_qtest, ASPEED_FLASH_BASE, PP);
+    writel(global_qtest, ASPEED_FLASH_BASE, make_be32(my_page_addr));

     /* Fill the page with its own addresses */
     for (i = 0; i < PAGE_SIZE / 4; i++) {
-        writel(ASPEED_FLASH_BASE, make_be32(my_page_addr + i * 4));
+        writel(global_qtest, ASPEED_FLASH_BASE,
+               make_be32(my_page_addr + i * 4));
     }
     spi_ctrl_stop_user();

@@ -294,7 +296,7 @@ static void test_read_page_mem(void)
     /* Enable 4BYTE mode for flash. */
     spi_conf(CONF_ENABLE_W0);
     spi_ctrl_start_user();
-    writeb(ASPEED_FLASH_BASE, EN_4BYTE_ADDR);
+    writeb(global_qtest, ASPEED_FLASH_BASE, EN_4BYTE_ADDR);
     spi_ctrl_stop_user();
     spi_conf_remove(CONF_ENABLE_W0);

@@ -327,15 +329,15 @@ static void test_write_page_mem(void)
     /* Enable 4BYTE mode for flash. */
     spi_conf(CONF_ENABLE_W0);
     spi_ctrl_start_user();
-    writeb(ASPEED_FLASH_BASE, EN_4BYTE_ADDR);
-    writeb(ASPEED_FLASH_BASE, WREN);
+    writeb(global_qtest, ASPEED_FLASH_BASE, EN_4BYTE_ADDR);
+    writeb(global_qtest, ASPEED_FLASH_BASE, WREN);
     spi_ctrl_stop_user();

     /* move out USER mode to use direct writes to the AHB bus */
     spi_ctrl_setmode(CTRL_WRITEMODE, PP);

     for (i = 0; i < PAGE_SIZE / 4; i++) {
-        writel(ASPEED_FLASH_BASE + my_page_addr + i * 4,
+        writel(global_qtest, ASPEED_FLASH_BASE + my_page_addr + i * 4,
                make_be32(my_page_addr + i * 4));
     }

diff --git a/tests/m48t59-test.c b/tests/m48t59-test.c
index 7ef3e2d26a..6218995d62 100644
--- a/tests/m48t59-test.c
+++ b/tests/m48t59-test.c
@@ -32,14 +32,14 @@ static bool use_mmio;

 static uint8_t cmos_read_mmio(uint8_t reg)
 {
-    return readb(base + (uint32_t)reg_base + (uint32_t)reg);
+    return readb(global_qtest, base + (uint32_t)reg_base + (uint32_t)reg);
 }

 static void cmos_write_mmio(uint8_t reg, uint8_t val)
 {
     uint8_t data = val;

-    writeb(base + (uint32_t)reg_base + (uint32_t)reg, data);
+    writeb(global_qtest, base + (uint32_t)reg_base + (uint32_t)reg, data);
 }

 static uint8_t cmos_read_ioio(uint8_t reg)
diff --git a/tests/pnv-xscom-test.c b/tests/pnv-xscom-test.c
index 89fa6282d3..52a2cc2f82 100644
--- a/tests/pnv-xscom-test.c
+++ b/tests/pnv-xscom-test.c
@@ -69,7 +69,7 @@ static uint64_t pnv_xscom_addr(const PnvChip *chip, uint32_t pcba)

 static uint64_t pnv_xscom_read(const PnvChip *chip, uint32_t pcba)
 {
-    return readq(pnv_xscom_addr(chip, pcba));
+    return readq(global_qtest, pnv_xscom_addr(chip, pcba));
 }

 static void test_xscom_cfam_id(const PnvChip *chip)
diff --git a/tests/prom-env-test.c b/tests/prom-env-test.c
index 0f8e6950fd..567d5a241d 100644
--- a/tests/prom-env-test.c
+++ b/tests/prom-env-test.c
@@ -32,7 +32,7 @@ static void check_guest_memory(void)

     /* Poll until code has run and modified memory. Wait at most 120 seconds */
     for (i = 0; i < 12000; ++i) {
-        signature = readl(ADDRESS);
+        signature = readl(global_qtest, ADDRESS);
         if (signature == MAGIC) {
             break;
         }
diff --git a/tests/q35-test.c b/tests/q35-test.c
index 4d90b9174e..fc2ecd43c9 100644
--- a/tests/q35-test.c
+++ b/tests/q35-test.c
@@ -174,14 +174,14 @@ static void test_tseg_size(const void *data)
      */
     ram_offs = (TSEG_SIZE_TEST_GUEST_RAM_MBYTES - args->expected_tseg_mbytes) *
                1024 * 1024 - 1;
-    g_assert_cmpint(readb(ram_offs), ==, 0);
-    writeb(ram_offs, 1);
-    g_assert_cmpint(readb(ram_offs), ==, 1);
+    g_assert_cmpint(readb(global_qtest, ram_offs), ==, 0);
+    writeb(global_qtest, ram_offs, 1);
+    g_assert_cmpint(readb(global_qtest, ram_offs), ==, 1);

     ram_offs++;
-    g_assert_cmpint(readb(ram_offs), ==, 0xff);
-    writeb(ram_offs, 1);
-    g_assert_cmpint(readb(ram_offs), ==, 0xff);
+    g_assert_cmpint(readb(global_qtest, ram_offs), ==, 0xff);
+    writeb(global_qtest, ram_offs, 1);
+    g_assert_cmpint(readb(global_qtest, ram_offs), ==, 0xff);

     g_free(pcidev);
     qpci_free_pc(pcibus);
diff --git a/tests/tco-test.c b/tests/tco-test.c
index c2abbeeaac..1cf7362d83 100644
--- a/tests/tco-test.c
+++ b/tests/tco-test.c
@@ -117,13 +117,13 @@ static void reset_on_second_timeout(bool enable)
 {
     uint32_t val;

-    val = readl(RCBA_BASE_ADDR + ICH9_CC_GCS);
+    val = readl(global_qtest, RCBA_BASE_ADDR + ICH9_CC_GCS);
     if (enable) {
         val &= ~ICH9_CC_GCS_NO_REBOOT;
     } else {
         val |= ICH9_CC_GCS_NO_REBOOT;
     }
-    writel(RCBA_BASE_ADDR + ICH9_CC_GCS, val);
+    writel(global_qtest, RCBA_BASE_ADDR + ICH9_CC_GCS, val);
 }

 static void test_tco_defaults(void)
diff --git a/tests/test-arm-mptimer.c b/tests/test-arm-mptimer.c
index 0e6484a4a8..0b6b514271 100644
--- a/tests/test-arm-mptimer.c
+++ b/tests/test-arm-mptimer.c
@@ -38,7 +38,7 @@ static int scaled = 122;

 static void timer_load(uint32_t load)
 {
-    writel(TIMER_BASE_PHYS + TIMER_LOAD, load);
+    writel(global_qtest, TIMER_BASE_PHYS + TIMER_LOAD, load);
 }

 static void timer_start(int periodic, uint32_t scale)
@@ -49,17 +49,17 @@ static void timer_start(int periodic, uint32_t scale)
         ctl |= TIMER_CONTROL_PERIODIC;
     }

-    writel(TIMER_BASE_PHYS + TIMER_CONTROL, ctl);
+    writel(global_qtest, TIMER_BASE_PHYS + TIMER_CONTROL, ctl);
 }

 static void timer_stop(void)
 {
-    writel(TIMER_BASE_PHYS + TIMER_CONTROL, 0);
+    writel(global_qtest, TIMER_BASE_PHYS + TIMER_CONTROL, 0);
 }

 static void timer_int_clr(void)
 {
-    writel(TIMER_BASE_PHYS + TIMER_INTSTAT, 1);
+    writel(global_qtest, TIMER_BASE_PHYS + TIMER_INTSTAT, 1);
 }

 static void timer_reset(void)
@@ -71,7 +71,7 @@ static void timer_reset(void)

 static uint32_t timer_get_and_clr_int_sts(void)
 {
-    uint32_t int_sts = readl(TIMER_BASE_PHYS + TIMER_INTSTAT);
+    uint32_t int_sts = readl(global_qtest, TIMER_BASE_PHYS + TIMER_INTSTAT);

     if (int_sts) {
         timer_int_clr();
@@ -82,12 +82,12 @@ static uint32_t timer_get_and_clr_int_sts(void)

 static uint32_t timer_counter(void)
 {
-    return readl(TIMER_BASE_PHYS + TIMER_COUNTER);
+    return readl(global_qtest, TIMER_BASE_PHYS + TIMER_COUNTER);
 }

 static void timer_set_counter(uint32_t value)
 {
-    writel(TIMER_BASE_PHYS + TIMER_COUNTER, value);
+    writel(global_qtest, TIMER_BASE_PHYS + TIMER_COUNTER, value);
 }

 static void test_timer_oneshot(gconstpointer arg)
diff --git a/tests/vhost-user-test.c b/tests/vhost-user-test.c
index c205a9100a..580ad43fad 100644
--- a/tests/vhost-user-test.c
+++ b/tests/vhost-user-test.c
@@ -241,7 +241,7 @@ static void read_guest_mem(const void *data)
         for (j = 0; j < 256; j++) {
             uint32_t a, b;

-            a = qtest_readl(s->qts,
+            a = readl(s->qts,
                             s->memory.regions[i].guest_phys_addr + j * 4);
             b = guest_mem[j];

diff --git a/tests/virtio-blk-test.c b/tests/virtio-blk-test.c
index 9594fbf437..5f8606ea80 100644
--- a/tests/virtio-blk-test.c
+++ b/tests/virtio-blk-test.c
@@ -195,7 +195,7 @@ static void test_basic(QVirtioDevice *dev, QGuestAllocator *alloc,
     qvirtqueue_kick(dev, vq, free_head);

     qvirtio_wait_used_elem(dev, vq, free_head, QVIRTIO_BLK_TIMEOUT_US);
-    status = readb(req_addr + 528);
+    status = readb(global_qtest, req_addr + 528);
     g_assert_cmpint(status, ==, 0);

     guest_free(alloc, req_addr);
@@ -217,7 +217,7 @@ static void test_basic(QVirtioDevice *dev, QGuestAllocator *alloc,
     qvirtqueue_kick(dev, vq, free_head);

     qvirtio_wait_used_elem(dev, vq, free_head, QVIRTIO_BLK_TIMEOUT_US);
-    status = readb(req_addr + 528);
+    status = readb(global_qtest, req_addr + 528);
     g_assert_cmpint(status, ==, 0);

     data = g_malloc0(512);
@@ -245,7 +245,7 @@ static void test_basic(QVirtioDevice *dev, QGuestAllocator *alloc,
         qvirtqueue_kick(dev, vq, free_head);

         qvirtio_wait_used_elem(dev, vq, free_head, QVIRTIO_BLK_TIMEOUT_US);
-        status = readb(req_addr + 528);
+        status = readb(global_qtest, req_addr + 528);
         g_assert_cmpint(status, ==, 0);

         guest_free(alloc, req_addr);
@@ -266,7 +266,7 @@ static void test_basic(QVirtioDevice *dev, QGuestAllocator *alloc,
         qvirtqueue_kick(dev, vq, free_head);

         qvirtio_wait_used_elem(dev, vq, free_head, QVIRTIO_BLK_TIMEOUT_US);
-        status = readb(req_addr + 528);
+        status = readb(global_qtest, req_addr + 528);
         g_assert_cmpint(status, ==, 0);

         data = g_malloc0(512);
@@ -350,7 +350,7 @@ static void pci_indirect(void)

     qvirtio_wait_used_elem(&dev->vdev, &vqpci->vq, free_head,
                            QVIRTIO_BLK_TIMEOUT_US);
-    status = readb(req_addr + 528);
+    status = readb(global_qtest, req_addr + 528);
     g_assert_cmpint(status, ==, 0);

     g_free(indirect);
@@ -376,7 +376,7 @@ static void pci_indirect(void)

     qvirtio_wait_used_elem(&dev->vdev, &vqpci->vq, free_head,
                            QVIRTIO_BLK_TIMEOUT_US);
-    status = readb(req_addr + 528);
+    status = readb(global_qtest, req_addr + 528);
     g_assert_cmpint(status, ==, 0);

     data = g_malloc0(512);
@@ -488,7 +488,7 @@ static void pci_msix(void)
     qvirtio_wait_used_elem(&dev->vdev, &vqpci->vq, free_head,
                            QVIRTIO_BLK_TIMEOUT_US);

-    status = readb(req_addr + 528);
+    status = readb(global_qtest, req_addr + 528);
     g_assert_cmpint(status, ==, 0);

     guest_free(qs->alloc, req_addr);
@@ -513,7 +513,7 @@ static void pci_msix(void)
     qvirtio_wait_used_elem(&dev->vdev, &vqpci->vq, free_head,
                            QVIRTIO_BLK_TIMEOUT_US);

-    status = readb(req_addr + 528);
+    status = readb(global_qtest, req_addr + 528);
     g_assert_cmpint(status, ==, 0);

     data = g_malloc0(512);
@@ -636,7 +636,7 @@ static void pci_idx(void)
     g_assert(qvirtqueue_get_buf(&vqpci->vq, &desc_idx));
     g_assert_cmpint(desc_idx, ==, free_head);

-    status = readb(req_addr + 528);
+    status = readb(global_qtest, req_addr + 528);
     g_assert_cmpint(status, ==, 0);

     data = g_malloc0(512);
diff --git a/tests/virtio-scsi-test.c b/tests/virtio-scsi-test.c
index 4d544ddbc3..30a341df2f 100644
--- a/tests/virtio-scsi-test.c
+++ b/tests/virtio-scsi-test.c
@@ -125,7 +125,7 @@ static uint8_t virtio_scsi_do_command(QVirtIOSCSI *vs, const uint8_t *cdb,
     qvirtqueue_kick(vs->dev, vq, free_head);
     qvirtio_wait_used_elem(vs->dev, vq, free_head, QVIRTIO_SCSI_TIMEOUT_US);

-    response = readb(resp_addr +
+    response = readb(global_qtest, resp_addr +
                      offsetof(struct virtio_scsi_cmd_resp, response));

     if (resp_out) {
diff --git a/tests/vmgenid-test.c b/tests/vmgenid-test.c
index 108a5d9a6e..aab7760bee 100644
--- a/tests/vmgenid-test.c
+++ b/tests/vmgenid-test.c
@@ -105,7 +105,7 @@ static void read_guid_from_memory(QemuUUID *guid)

     /* Read the GUID directly from guest memory */
     for (i = 0; i < 16; i++) {
-        guid->data[i] = readb(vmgenid_addr + i);
+        guid->data[i] = readb(global_qtest, vmgenid_addr + i);
     }
     /* The GUID is in little-endian format in the guest, while QEMU
      * uses big-endian.  Swap after reading.
-- 
2.13.5

^ permalink raw reply related	[flat|nested] 75+ messages in thread

* [Qemu-devel] [PATCH v7 35/38] libqtest: Merge qtest_{mem, buf}{read, write}() with {mem, buf}{read, write}()
  2017-09-11 17:19 [Qemu-devel] [PATCH v7 00/38] Preliminary libqtest cleanups Eric Blake
                   ` (33 preceding siblings ...)
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 34/38] libqtest: Merge qtest_{read, write}[bwlq]() with {read, write}[bwlq]() Eric Blake
@ 2017-09-11 17:20 ` Eric Blake
  2017-09-11 21:35   ` Greg Kurz
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 36/38] libqtest: Merge qtest_memset() with qmemset() Eric Blake
                   ` (2 subsequent siblings)
  37 siblings, 1 reply; 75+ messages in thread
From: Eric Blake @ 2017-09-11 17:20 UTC (permalink / raw)
  To: qemu-devel
  Cc: armbru, pbonzini, thuth, John Snow, David Gibson, Alexander Graf,
	Aneesh Kumar K.V, Greg Kurz, Stefan Hajnoczi, Jason Wang,
	open list:IDE, open list:sPAPR

Maintaining two layers of libqtest APIs, one that takes an explicit
QTestState object, and the other that uses the implicit global_qtest,
is annoying.  In the interest of getting rid of global implicit
state and having less code to maintain, merge:
 qtest_memread()
 qtest_bufread()
 qtest_memwrite()
 qtest_bufwrite()
with their short counterparts.  All callers that previously
used the short form now make it explicit that they are relying on
global_qtest, and later patches can then clean things up to remove
the global variable.

Signed-off-by: Eric Blake <eblake@redhat.com>
---
 tests/libqtest.h         | 69 ++++++------------------------------------------
 tests/libqtest.c         |  8 +++---
 tests/ahci-test.c        | 16 +++++------
 tests/e1000e-test.c      | 16 ++++++-----
 tests/i440fx-test.c      |  8 +++---
 tests/ide-test.c         | 14 +++++-----
 tests/libqos/ahci.c      | 22 +++++++--------
 tests/libqos/pci-pc.c    |  4 +--
 tests/libqos/pci-spapr.c |  4 +--
 tests/megasas-test.c     |  2 +-
 tests/postcopy-test.c    | 12 ++++-----
 tests/virtio-9p-test.c   |  4 +--
 tests/virtio-blk-test.c  | 16 +++++------
 tests/virtio-net-test.c  |  6 ++---
 tests/virtio-scsi-test.c |  4 +--
 15 files changed, 78 insertions(+), 127 deletions(-)

diff --git a/tests/libqtest.h b/tests/libqtest.h
index d0c487cf79..adf04327b7 100644
--- a/tests/libqtest.h
+++ b/tests/libqtest.h
@@ -346,7 +346,7 @@ uint32_t readl(QTestState *s, uint64_t addr);
 uint64_t readq(QTestState *s, uint64_t addr);

 /**
- * qtest_memread:
+ * memread:
  * @s: #QTestState instance to operate on.
  * @addr: Guest address to read from.
  * @data: Pointer to where memory contents will be stored.
@@ -354,7 +354,7 @@ uint64_t readq(QTestState *s, uint64_t addr);
  *
  * Read guest memory into a buffer.
  */
-void qtest_memread(QTestState *s, uint64_t addr, void *data, size_t size);
+void memread(QTestState *s, uint64_t addr, void *data, size_t size);

 /**
  * qtest_rtas_call:
@@ -372,7 +372,7 @@ uint64_t qtest_rtas_call(QTestState *s, const char *name,
                          uint32_t nret, uint64_t ret);

 /**
- * qtest_bufread:
+ * bufread:
  * @s: #QTestState instance to operate on.
  * @addr: Guest address to read from.
  * @data: Pointer to where memory contents will be stored.
@@ -380,10 +380,10 @@ uint64_t qtest_rtas_call(QTestState *s, const char *name,
  *
  * Read guest memory into a buffer and receive using a base64 encoding.
  */
-void qtest_bufread(QTestState *s, uint64_t addr, void *data, size_t size);
+void bufread(QTestState *s, uint64_t addr, void *data, size_t size);

 /**
- * qtest_memwrite:
+ * memwrite:
  * @s: #QTestState instance to operate on.
  * @addr: Guest address to write to.
  * @data: Pointer to the bytes that will be written to guest memory.
@@ -391,10 +391,10 @@ void qtest_bufread(QTestState *s, uint64_t addr, void *data, size_t size);
  *
  * Write a buffer to guest memory.
  */
-void qtest_memwrite(QTestState *s, uint64_t addr, const void *data, size_t size);
+void memwrite(QTestState *s, uint64_t addr, const void *data, size_t size);

 /**
- * qtest_bufwrite:
+ * bufwrite:
  * @s: #QTestState instance to operate on.
  * @addr: Guest address to write to.
  * @data: Pointer to the bytes that will be written to guest memory.
@@ -402,8 +402,7 @@ void qtest_memwrite(QTestState *s, uint64_t addr, const void *data, size_t size)
  *
  * Write a buffer to guest memory and transmit using a base64 encoding.
  */
-void qtest_bufwrite(QTestState *s, uint64_t addr,
-                    const void *data, size_t size);
+void bufwrite(QTestState *s, uint64_t addr, const void *data, size_t size);

 /**
  * qtest_memset:
@@ -594,58 +593,6 @@ static inline QDict *qmp_eventwait_ref(const char *event)
 char *hmp(const char *fmt, ...) GCC_FMT_ATTR(1, 2);

 /**
- * memread:
- * @addr: Guest address to read from.
- * @data: Pointer to where memory contents will be stored.
- * @size: Number of bytes to read.
- *
- * Read guest memory into a buffer.
- */
-static inline void memread(uint64_t addr, void *data, size_t size)
-{
-    qtest_memread(global_qtest, addr, data, size);
-}
-
-/**
- * bufread:
- * @addr: Guest address to read from.
- * @data: Pointer to where memory contents will be stored.
- * @size: Number of bytes to read.
- *
- * Read guest memory into a buffer, receive using a base64 encoding.
- */
-static inline void bufread(uint64_t addr, void *data, size_t size)
-{
-    qtest_bufread(global_qtest, addr, data, size);
-}
-
-/**
- * memwrite:
- * @addr: Guest address to write to.
- * @data: Pointer to the bytes that will be written to guest memory.
- * @size: Number of bytes to write.
- *
- * Write a buffer to guest memory.
- */
-static inline void memwrite(uint64_t addr, const void *data, size_t size)
-{
-    qtest_memwrite(global_qtest, addr, data, size);
-}
-
-/**
- * bufwrite:
- * @addr: Guest address to write to.
- * @data: Pointer to the bytes that will be written to guest memory.
- * @size: Number of bytes to write.
- *
- * Write a buffer to guest memory, transmit using a base64 encoding.
- */
-static inline void bufwrite(uint64_t addr, const void *data, size_t size)
-{
-    qtest_bufwrite(global_qtest, addr, data, size);
-}
-
-/**
  * qmemset:
  * @addr: Guest address to write to.
  * @patt: Byte pattern to fill the guest memory region with.
diff --git a/tests/libqtest.c b/tests/libqtest.c
index d9d0402287..a52dc635f4 100644
--- a/tests/libqtest.c
+++ b/tests/libqtest.c
@@ -822,7 +822,7 @@ static int hex2nib(char ch)
     }
 }

-void qtest_memread(QTestState *s, uint64_t addr, void *data, size_t size)
+void memread(QTestState *s, uint64_t addr, void *data, size_t size)
 {
     uint8_t *ptr = data;
     gchar **args;
@@ -877,7 +877,7 @@ void qtest_add_data_func(const char *str, const void *data,
     g_free(path);
 }

-void qtest_bufwrite(QTestState *s, uint64_t addr, const void *data, size_t size)
+void bufwrite(QTestState *s, uint64_t addr, const void *data, size_t size)
 {
     gchar *bdata;

@@ -889,7 +889,7 @@ void qtest_bufwrite(QTestState *s, uint64_t addr, const void *data, size_t size)
     g_free(bdata);
 }

-void qtest_bufread(QTestState *s, uint64_t addr, void *data, size_t size)
+void bufread(QTestState *s, uint64_t addr, void *data, size_t size)
 {
     gchar **args;
     size_t len;
@@ -908,7 +908,7 @@ void qtest_bufread(QTestState *s, uint64_t addr, void *data, size_t size)
     g_strfreev(args);
 }

-void qtest_memwrite(QTestState *s, uint64_t addr, const void *data, size_t size)
+void memwrite(QTestState *s, uint64_t addr, const void *data, size_t size)
 {
     const uint8_t *ptr = data;
     size_t i;
diff --git a/tests/ahci-test.c b/tests/ahci-test.c
index 7c94c4920b..4b5f0b23fb 100644
--- a/tests/ahci-test.c
+++ b/tests/ahci-test.c
@@ -869,7 +869,7 @@ static void ahci_test_io_rw_simple(AHCIQState *ahci, unsigned bufsize,

     /* Write some indicative pattern to our buffer. */
     generate_pattern(tx, bufsize, AHCI_SECTOR_SIZE);
-    qtest_bufwrite(ahci->parent->qts, ptr, tx, bufsize);
+    bufwrite(ahci->parent->qts, ptr, tx, bufsize);

     /* Write this buffer to disk, then read it back to the DMA buffer. */
     ahci_guest_io(ahci, port, write_cmd, ptr, bufsize, sector);
@@ -877,7 +877,7 @@ static void ahci_test_io_rw_simple(AHCIQState *ahci, unsigned bufsize,
     ahci_guest_io(ahci, port, read_cmd, ptr, bufsize, sector);

     /*** Read back the Data ***/
-    qtest_bufread(ahci->parent->qts, ptr, rx, bufsize);
+    bufread(ahci->parent->qts, ptr, rx, bufsize);
     g_assert_cmphex(memcmp(tx, rx, bufsize), ==, 0);

     ahci_free(ahci, ptr);
@@ -918,7 +918,7 @@ static void ahci_test_max(AHCIQState *ahci)
     }

     port = ahci_test_nondata(ahci, cmd);
-    qtest_memread(ahci->parent->qts, ahci->port[port].fb + 0x40, d2h, 0x20);
+    memread(ahci->parent->qts, ahci->port[port].fb + 0x40, d2h, 0x20);
     nsect = (uint64_t)d2h->lba_hi[2] << 40 |
         (uint64_t)d2h->lba_hi[1] << 32 |
         (uint64_t)d2h->lba_hi[0] << 24 |
@@ -1036,7 +1036,7 @@ static void test_dma_fragmented(void)
     /* Create a DMA buffer in guest memory, and write our pattern to it. */
     ptr = guest_alloc(ahci->parent->alloc, bufsize);
     g_assert(ptr);
-    qtest_bufwrite(ahci->parent->qts, ptr, tx, bufsize);
+    bufwrite(ahci->parent->qts, ptr, tx, bufsize);

     cmd = ahci_command_create(CMD_WRITE_DMA);
     ahci_command_adjust(cmd, 0, ptr, bufsize, 32);
@@ -1053,7 +1053,7 @@ static void test_dma_fragmented(void)
     ahci_command_free(cmd);

     /* Read back the guest's receive buffer into local memory */
-    qtest_bufread(ahci->parent->qts, ptr, rx, bufsize);
+    bufread(ahci->parent->qts, ptr, rx, bufsize);
     guest_free(ahci->parent->alloc, ptr);

     g_assert_cmphex(memcmp(tx, rx, bufsize), ==, 0);
@@ -1231,7 +1231,7 @@ static void ahci_halted_io_test(uint8_t cmd_read, uint8_t cmd_write)
     generate_pattern(tx, bufsize, AHCI_SECTOR_SIZE);
     ptr = ahci_alloc(ahci, bufsize);
     g_assert(ptr);
-    qtest_memwrite(ahci->parent->qts, ptr, tx, bufsize);
+    memwrite(ahci->parent->qts, ptr, tx, bufsize);

     /* Attempt to write (and fail) */
     cmd = ahci_guest_io_halt(ahci, port, cmd_write,
@@ -1305,7 +1305,7 @@ static void ahci_migrate_halted_io(uint8_t cmd_read, uint8_t cmd_write)
     /* create DMA source buffer and write pattern */
     ptr = ahci_alloc(src, bufsize);
     g_assert(ptr);
-    qtest_memwrite(src->parent->qts, ptr, tx, bufsize);
+    memwrite(src->parent->qts, ptr, tx, bufsize);

     /* Write, trigger the VM to stop, migrate, then resume. */
     cmd = ahci_guest_io_halt(src, port, cmd_write,
@@ -1472,7 +1472,7 @@ static int ahci_cb_cmp_buff(AHCIQState *ahci, AHCICommand *cmd,
     }

     rx = g_malloc0(opts->size);
-    qtest_bufread(ahci->parent->qts, opts->buffer, rx, opts->size);
+    bufread(ahci->parent->qts, opts->buffer, rx, opts->size);
     g_assert_cmphex(memcmp(tx, rx, opts->size), ==, 0);
     g_free(rx);

diff --git a/tests/e1000e-test.c b/tests/e1000e-test.c
index f4ead74f96..1e39d98e4a 100644
--- a/tests/e1000e-test.c
+++ b/tests/e1000e-test.c
@@ -202,11 +202,13 @@ static void e1000e_tx_ring_push(e1000e_device *d, void *descr)
     uint32_t tail = e1000e_macreg_read(d, E1000E_TDT);
     uint32_t len = e1000e_macreg_read(d, E1000E_TDLEN) / E1000E_TXD_LEN;

-    memwrite(d->tx_ring + tail * E1000E_TXD_LEN, descr, E1000E_TXD_LEN);
+    memwrite(global_qtest, d->tx_ring + tail * E1000E_TXD_LEN, descr,
+             E1000E_TXD_LEN);
     e1000e_macreg_write(d, E1000E_TDT, (tail + 1) % len);

     /* Read WB data for the packet transmitted */
-    memread(d->tx_ring + tail * E1000E_TXD_LEN, descr, E1000E_TXD_LEN);
+    memread(global_qtest, d->tx_ring + tail * E1000E_TXD_LEN, descr,
+            E1000E_TXD_LEN);
 }

 static void e1000e_rx_ring_push(e1000e_device *d, void *descr)
@@ -214,11 +216,13 @@ static void e1000e_rx_ring_push(e1000e_device *d, void *descr)
     uint32_t tail = e1000e_macreg_read(d, E1000E_RDT);
     uint32_t len = e1000e_macreg_read(d, E1000E_RDLEN) / E1000E_RXD_LEN;

-    memwrite(d->rx_ring + tail * E1000E_RXD_LEN, descr, E1000E_RXD_LEN);
+    memwrite(global_qtest, d->rx_ring + tail * E1000E_RXD_LEN, descr,
+             E1000E_RXD_LEN);
     e1000e_macreg_write(d, E1000E_RDT, (tail + 1) % len);

     /* Read WB data for the packet received */
-    memread(d->rx_ring + tail * E1000E_RXD_LEN, descr, E1000E_RXD_LEN);
+    memread(global_qtest, d->rx_ring + tail * E1000E_RXD_LEN, descr,
+            E1000E_RXD_LEN);
 }

 static void e1000e_wait_isr(e1000e_device *d, uint16_t msg_id)
@@ -269,7 +273,7 @@ static void e1000e_send_verify(e1000e_device *d)

     /* Prepare test data buffer */
     uint64_t data = guest_alloc(test_alloc, data_len);
-    memwrite(data, "TEST", 5);
+    memwrite(global_qtest, data, "TEST", 5);

     /* Prepare TX descriptor */
     memset(&descr, 0, sizeof(descr));
@@ -365,7 +369,7 @@ static void e1000e_receive_verify(e1000e_device *d)
         esta_dd, ==, esta_dd);

     /* Check data sent to the backend */
-    memread(data, buffer, sizeof(buffer));
+    memread(global_qtest, data, buffer, sizeof(buffer));
     g_assert_cmpstr(buffer, == , "TEST");

     /* Free test data buffer */
diff --git a/tests/i440fx-test.c b/tests/i440fx-test.c
index 1d78089f8e..804c8ca9a8 100644
--- a/tests/i440fx-test.c
+++ b/tests/i440fx-test.c
@@ -160,7 +160,7 @@ static gboolean verify_area(uint32_t start, uint32_t end, uint8_t value)
     int i;

     data = g_malloc0(size);
-    memread(start, data, size);
+    memread(global_qtest, start, data, size);

     g_test_message("verify_area: data[0] = 0x%x", data[0]);

@@ -183,7 +183,7 @@ static void write_area(uint32_t start, uint32_t end, uint8_t value)

     data = g_malloc(size);
     memset(data, value, size);
-    memwrite(start, data, size);
+    memwrite(global_qtest, start, data, size);

     g_free(data);
 }
@@ -354,7 +354,7 @@ static void test_i440fx_firmware(FirmwareTestFixture *fixture,

     /* check below 4G */
     buf = g_malloc0(BLOB_SIZE);
-    memread(0x100000000ULL - BLOB_SIZE, buf, BLOB_SIZE);
+    memread(global_qtest, 0x100000000ULL - BLOB_SIZE, buf, BLOB_SIZE);
     for (i = 0; i < BLOB_SIZE; ++i) {
         g_assert_cmphex(buf[i], ==, (uint8_t)i);
     }
@@ -362,7 +362,7 @@ static void test_i440fx_firmware(FirmwareTestFixture *fixture,
     /* check in ISA space too */
     memset(buf, 0, BLOB_SIZE);
     isa_bios_size = ISA_BIOS_MAXSZ < BLOB_SIZE ? ISA_BIOS_MAXSZ : BLOB_SIZE;
-    memread(0x100000 - isa_bios_size, buf, isa_bios_size);
+    memread(global_qtest, 0x100000 - isa_bios_size, buf, isa_bios_size);
     for (i = 0; i < isa_bios_size; ++i) {
         g_assert_cmphex(buf[i], ==,
                         (uint8_t)((BLOB_SIZE - isa_bios_size) + i));
diff --git a/tests/ide-test.c b/tests/ide-test.c
index c22dde55c1..c47cbb1949 100644
--- a/tests/ide-test.c
+++ b/tests/ide-test.c
@@ -222,7 +222,7 @@ static int send_dma_request(int cmd, uint64_t sector, int nb_sectors,
     /* Setup PRDT */
     len = sizeof(*prdt) * prdt_entries;
     guest_prdt = guest_alloc(guest_malloc, len);
-    memwrite(guest_prdt, prdt, len);
+    memwrite(global_qtest, guest_prdt, prdt, len);
     qpci_io_writel(dev, bmdma_bar, bmreg_prdt, guest_prdt);

     /* ATA DMA command */
@@ -299,7 +299,7 @@ static void test_bmdma_simple_rw(void)

     /* Write 0x55 pattern to sector 0 */
     memset(buf, 0x55, len);
-    memwrite(guest_buf, buf, len);
+    memwrite(global_qtest, guest_buf, buf, len);

     status = send_dma_request(CMD_WRITE_DMA, 0, 1, prdt,
                               ARRAY_SIZE(prdt), NULL);
@@ -308,7 +308,7 @@ static void test_bmdma_simple_rw(void)

     /* Write 0xaa pattern to sector 1 */
     memset(buf, 0xaa, len);
-    memwrite(guest_buf, buf, len);
+    memwrite(global_qtest, guest_buf, buf, len);

     status = send_dma_request(CMD_WRITE_DMA, 1, 1, prdt,
                               ARRAY_SIZE(prdt), NULL);
@@ -322,7 +322,7 @@ static void test_bmdma_simple_rw(void)
     g_assert_cmphex(status, ==, BM_STS_INTR);
     assert_bit_clear(qpci_io_readb(dev, ide_bar, reg_status), DF | ERR);

-    memread(guest_buf, buf, len);
+    memread(global_qtest, guest_buf, buf, len);
     g_assert(memcmp(buf, cmpbuf, len) == 0);

     /* Read and verify 0xaa pattern in sector 1 */
@@ -332,7 +332,7 @@ static void test_bmdma_simple_rw(void)
     g_assert_cmphex(status, ==, BM_STS_INTR);
     assert_bit_clear(qpci_io_readb(dev, ide_bar, reg_status), DF | ERR);

-    memread(guest_buf, buf, len);
+    memread(global_qtest, guest_buf, buf, len);
     g_assert(memcmp(buf, cmpbuf, len) == 0);


@@ -551,7 +551,7 @@ static void make_dirty(uint8_t device)
     g_assert(guest_buf);
     g_assert(buf);

-    memwrite(guest_buf, buf, len);
+    memwrite(global_qtest, guest_buf, buf, len);

     PrdtEntry prdt[] = {
         {
@@ -921,7 +921,7 @@ static void test_cdrom_dma(void)
     send_dma_request(CMD_PACKET, 0, 1, prdt, 1, send_scsi_cdb_read10);

     /* Read back data from guest memory into local qtest memory */
-    memread(guest_buf, rx, len);
+    memread(global_qtest, guest_buf, rx, len);
     g_assert_cmpint(memcmp(pattern, rx, len), ==, 0);

     g_free(pattern);
diff --git a/tests/libqos/ahci.c b/tests/libqos/ahci.c
index ba79cd77a0..433952c11a 100644
--- a/tests/libqos/ahci.c
+++ b/tests/libqos/ahci.c
@@ -467,7 +467,7 @@ void ahci_port_check_d2h_sanity(AHCIQState *ahci, uint8_t port, uint8_t slot)
     RegD2HFIS *d2h = g_malloc0(0x20);
     uint32_t reg;

-    qtest_memread(ahci->parent->qts, ahci->port[port].fb + 0x40, d2h, 0x20);
+    memread(ahci->parent->qts, ahci->port[port].fb + 0x40, d2h, 0x20);
     g_assert_cmphex(d2h->fis_type, ==, 0x34);

     reg = ahci_px_rreg(ahci, port, AHCI_PX_TFD);
@@ -485,7 +485,7 @@ void ahci_port_check_pio_sanity(AHCIQState *ahci, uint8_t port,
     /* We cannot check the Status or E_Status registers, because
      * the status may have again changed between the PIO Setup FIS
      * and the conclusion of the command with the D2H Register FIS. */
-    qtest_memread(ahci->parent->qts, ahci->port[port].fb + 0x20, pio, 0x20);
+    memread(ahci->parent->qts, ahci->port[port].fb + 0x20, pio, 0x20);
     g_assert_cmphex(pio->fis_type, ==, 0x5f);

     /* BUG: PIO Setup FIS as utilized by QEMU tries to fit the entire
@@ -517,7 +517,7 @@ void ahci_get_command_header(AHCIQState *ahci, uint8_t port,
 {
     uint64_t ba = ahci->port[port].clb;
     ba += slot * sizeof(AHCICommandHeader);
-    qtest_memread(ahci->parent->qts, ba, cmd, sizeof(AHCICommandHeader));
+    memread(ahci->parent->qts, ba, cmd, sizeof(AHCICommandHeader));

     cmd->flags = le16_to_cpu(cmd->flags);
     cmd->prdtl = le16_to_cpu(cmd->prdtl);
@@ -538,7 +538,7 @@ void ahci_set_command_header(AHCIQState *ahci, uint8_t port,
     tmp.prdbc = cpu_to_le32(cmd->prdbc);
     tmp.ctba = cpu_to_le64(cmd->ctba);

-    qtest_memwrite(ahci->parent->qts, ba, &tmp, sizeof(AHCICommandHeader));
+    memwrite(ahci->parent->qts, ba, &tmp, sizeof(AHCICommandHeader));
 }

 void ahci_destroy_command(AHCIQState *ahci, uint8_t port, uint8_t slot)
@@ -576,7 +576,7 @@ void ahci_write_fis(AHCIQState *ahci, AHCICommand *cmd)
         tmp.count = cpu_to_le16(tmp.count);
     }

-    qtest_memwrite(ahci->parent->qts, addr, &tmp, sizeof(tmp));
+    memwrite(ahci->parent->qts, addr, &tmp, sizeof(tmp));
 }

 unsigned ahci_pick_cmd(AHCIQState *ahci, uint8_t port)
@@ -758,13 +758,13 @@ void ahci_io(AHCIQState *ahci, uint8_t port, uint8_t ide_cmd,
     qtest_memset(ahci->parent->qts, ptr, 0x00, bufsize);

     if (bufsize && props->write) {
-        qtest_bufwrite(ahci->parent->qts, ptr, buffer, bufsize);
+        bufwrite(ahci->parent->qts, ptr, buffer, bufsize);
     }

     ahci_guest_io(ahci, port, ide_cmd, ptr, bufsize, sector);

     if (bufsize && props->read) {
-        qtest_bufread(ahci->parent->qts, ptr, buffer, bufsize);
+        bufread(ahci->parent->qts, ptr, buffer, bufsize);
     }

     ahci_free(ahci, ptr);
@@ -902,7 +902,7 @@ static int copy_buffer(AHCIQState *ahci, AHCICommand *cmd,
                         const AHCIOpts *opts)
 {
     unsigned char *rx = opts->opaque;
-    qtest_bufread(ahci->parent->qts, opts->buffer, rx, opts->size);
+    bufread(ahci->parent->qts, opts->buffer, rx, opts->size);
     return 0;
 }

@@ -1142,7 +1142,7 @@ void ahci_command_commit(AHCIQState *ahci, AHCICommand *cmd, uint8_t port)
     ahci_write_fis(ahci, cmd);
     /* Then ATAPI CMD, if needed */
     if (cmd->props->atapi) {
-        qtest_memwrite(ahci->parent->qts, table_ptr + 0x40, cmd->atapi_cmd, 16);
+        memwrite(ahci->parent->qts, table_ptr + 0x40, cmd->atapi_cmd, 16);
     }

     /* Construct and write the PRDs to the command table */
@@ -1163,8 +1163,8 @@ void ahci_command_commit(AHCIQState *ahci, AHCICommand *cmd, uint8_t port)
         prd.dbc |= cpu_to_le32(0x80000000); /* Request DPS Interrupt */

         /* Commit the PRD entry to the Command Table */
-        qtest_memwrite(ahci->parent->qts, table_ptr + 0x80 + (i * sizeof(PRD)),
-                       &prd, sizeof(PRD));
+        memwrite(ahci->parent->qts, table_ptr + 0x80 + (i * sizeof(PRD)),
+                 &prd, sizeof(PRD));
     }

     /* Bookmark the PRDTL and CTBA values */
diff --git a/tests/libqos/pci-pc.c b/tests/libqos/pci-pc.c
index 4d8329bef0..aa4d5b3c43 100644
--- a/tests/libqos/pci-pc.c
+++ b/tests/libqos/pci-pc.c
@@ -68,13 +68,13 @@ static void qpci_pc_pio_writeq(QPCIBus *bus, uint32_t addr, uint64_t val)

 static void qpci_pc_memread(QPCIBus *bus, uint32_t addr, void *buf, size_t len)
 {
-    qtest_memread(bus->qts, addr, buf, len);
+    memread(bus->qts, addr, buf, len);
 }

 static void qpci_pc_memwrite(QPCIBus *bus, uint32_t addr,
                              const void *buf, size_t len)
 {
-    qtest_memwrite(bus->qts, addr, buf, len);
+    memwrite(bus->qts, addr, buf, len);
 }

 static uint8_t qpci_pc_config_readb(QPCIBus *bus, int devfn, uint8_t offset)
diff --git a/tests/libqos/pci-spapr.c b/tests/libqos/pci-spapr.c
index 7bae94e9bd..5d9f7c3494 100644
--- a/tests/libqos/pci-spapr.c
+++ b/tests/libqos/pci-spapr.c
@@ -94,14 +94,14 @@ static void qpci_spapr_memread(QPCIBus *bus, uint32_t addr,
                                void *buf, size_t len)
 {
     QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
-    qtest_memread(bus->qts, s->mmio32_cpu_base + addr, buf, len);
+    memread(bus->qts, s->mmio32_cpu_base + addr, buf, len);
 }

 static void qpci_spapr_memwrite(QPCIBus *bus, uint32_t addr,
                                 const void *buf, size_t len)
 {
     QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
-    qtest_memwrite(bus->qts, s->mmio32_cpu_base + addr, buf, len);
+    memwrite(bus->qts, s->mmio32_cpu_base + addr, buf, len);
 }

 static uint8_t qpci_spapr_config_readb(QPCIBus *bus, int devfn, uint8_t offset)
diff --git a/tests/megasas-test.c b/tests/megasas-test.c
index 81837e14af..7f16630e52 100644
--- a/tests/megasas-test.c
+++ b/tests/megasas-test.c
@@ -72,7 +72,7 @@ static void megasas_pd_get_info_fuzz(void)
     context[7] = cpu_to_le32(0);

     context_pa = qmalloc(qs, sizeof(context));
-    memwrite(context_pa, context, sizeof(context));
+    memwrite(global_qtest, context_pa, context, sizeof(context));
     qpci_io_writel(dev, bar, 0x40, context_pa);

     g_free(dev);
diff --git a/tests/postcopy-test.c b/tests/postcopy-test.c
index efee20a099..04889b8078 100644
--- a/tests/postcopy-test.c
+++ b/tests/postcopy-test.c
@@ -320,13 +320,13 @@ static void check_guests_ram(QTestState *qts)
     bool hit_edge = false;
     bool bad = false;

-    qtest_memread(qts, start_address, &first_byte, 1);
+    memread(qts, start_address, &first_byte, 1);
     last_byte = first_byte;

     for (address = start_address + 4096; address < end_address; address += 4096)
     {
         uint8_t b;
-        qtest_memread(qts, address, &b, 1);
+        memread(qts, address, &b, 1);
         if (b != last_byte) {
             if (((b + 1) % 256) == last_byte && !hit_edge) {
                 /* This is OK, the guest stopped at the point of
@@ -470,19 +470,19 @@ static void test_migrate(void)

     qtest_quit(from);

-    qtest_memread(to, start_address, &dest_byte_a, 1);
+    memread(to, start_address, &dest_byte_a, 1);

     /* Destination still running, wait for a byte to change */
     do {
-        qtest_memread(to, start_address, &dest_byte_b, 1);
+        memread(to, start_address, &dest_byte_b, 1);
         usleep(10 * 1000);
     } while (dest_byte_a == dest_byte_b);

     qtest_qmp_discard_response(to, "{ 'execute' : 'stop'}");
     /* With it stopped, check nothing changes */
-    qtest_memread(to, start_address, &dest_byte_c, 1);
+    memread(to, start_address, &dest_byte_c, 1);
     sleep(1);
-    qtest_memread(to, start_address, &dest_byte_d, 1);
+    memread(to, start_address, &dest_byte_d, 1);
     g_assert_cmpint(dest_byte_c, ==, dest_byte_d);

     check_guests_ram(to);
diff --git a/tests/virtio-9p-test.c b/tests/virtio-9p-test.c
index 07cbb7f37c..e2a14bc2ff 100644
--- a/tests/virtio-9p-test.c
+++ b/tests/virtio-9p-test.c
@@ -116,7 +116,7 @@ typedef struct {

 static void v9fs_memwrite(P9Req *req, const void *addr, size_t len)
 {
-    memwrite(req->t_msg + req->t_off, addr, len);
+    memwrite(global_qtest, req->t_msg + req->t_off, addr, len);
     req->t_off += len;
 }

@@ -132,7 +132,7 @@ static void v9fs_memrewind(P9Req *req, size_t len)

 static void v9fs_memread(P9Req *req, void *addr, size_t len)
 {
-    memread(req->r_msg + req->r_off, addr, len);
+    memread(global_qtest, req->r_msg + req->r_off, addr, len);
     req->r_off += len;
 }

diff --git a/tests/virtio-blk-test.c b/tests/virtio-blk-test.c
index 5f8606ea80..ec582c8e1c 100644
--- a/tests/virtio-blk-test.c
+++ b/tests/virtio-blk-test.c
@@ -145,9 +145,9 @@ static uint64_t virtio_blk_request(QGuestAllocator *alloc, QVirtioDevice *d,

     virtio_blk_fix_request(d, req);

-    memwrite(addr, req, 16);
-    memwrite(addr + 16, req->data, data_size);
-    memwrite(addr + 16 + data_size, &status, sizeof(status));
+    memwrite(global_qtest, addr, req, 16);
+    memwrite(global_qtest, addr + 16, req->data, data_size);
+    memwrite(global_qtest, addr + 16 + data_size, &status, sizeof(status));

     return addr;
 }
@@ -221,7 +221,7 @@ static void test_basic(QVirtioDevice *dev, QGuestAllocator *alloc,
     g_assert_cmpint(status, ==, 0);

     data = g_malloc0(512);
-    memread(req_addr + 16, data, 512);
+    memread(global_qtest, req_addr + 16, data, 512);
     g_assert_cmpstr(data, ==, "TEST");
     g_free(data);

@@ -270,7 +270,7 @@ static void test_basic(QVirtioDevice *dev, QGuestAllocator *alloc,
         g_assert_cmpint(status, ==, 0);

         data = g_malloc0(512);
-        memread(req_addr + 16, data, 512);
+        memread(global_qtest, req_addr + 16, data, 512);
         g_assert_cmpstr(data, ==, "TEST");
         g_free(data);

@@ -380,7 +380,7 @@ static void pci_indirect(void)
     g_assert_cmpint(status, ==, 0);

     data = g_malloc0(512);
-    memread(req_addr + 16, data, 512);
+    memread(global_qtest, req_addr + 16, data, 512);
     g_assert_cmpstr(data, ==, "TEST");
     g_free(data);

@@ -517,7 +517,7 @@ static void pci_msix(void)
     g_assert_cmpint(status, ==, 0);

     data = g_malloc0(512);
-    memread(req_addr + 16, data, 512);
+    memread(global_qtest, req_addr + 16, data, 512);
     g_assert_cmpstr(data, ==, "TEST");
     g_free(data);

@@ -640,7 +640,7 @@ static void pci_idx(void)
     g_assert_cmpint(status, ==, 0);

     data = g_malloc0(512);
-    memread(req_addr + 16, data, 512);
+    memread(global_qtest, req_addr + 16, data, 512);
     g_assert_cmpstr(data, ==, "TEST");
     g_free(data);

diff --git a/tests/virtio-net-test.c b/tests/virtio-net-test.c
index 9250c417a9..7ae3b57d37 100644
--- a/tests/virtio-net-test.c
+++ b/tests/virtio-net-test.c
@@ -112,7 +112,7 @@ static void rx_test(QVirtioDevice *dev,
     g_assert_cmpint(ret, ==, sizeof(test) + sizeof(len));

     qvirtio_wait_used_elem(dev, vq, free_head, QVIRTIO_NET_TIMEOUT_US);
-    memread(req_addr + VNET_HDR_SIZE, buffer, sizeof(test));
+    memread(global_qtest, req_addr + VNET_HDR_SIZE, buffer, sizeof(test));
     g_assert_cmpstr(buffer, ==, "TEST");

     guest_free(alloc, req_addr);
@@ -129,7 +129,7 @@ static void tx_test(QVirtioDevice *dev,
     int ret;

     req_addr = guest_alloc(alloc, 64);
-    memwrite(req_addr + VNET_HDR_SIZE, "TEST", 4);
+    memwrite(global_qtest, req_addr + VNET_HDR_SIZE, "TEST", 4);

     free_head = qvirtqueue_add(vq, req_addr, 64, false, false);
     qvirtqueue_kick(dev, vq, free_head);
@@ -186,7 +186,7 @@ static void rx_stop_cont_test(QVirtioDevice *dev,
     QDECREF(rsp);

     qvirtio_wait_used_elem(dev, vq, free_head, QVIRTIO_NET_TIMEOUT_US);
-    memread(req_addr + VNET_HDR_SIZE, buffer, sizeof(test));
+    memread(global_qtest, req_addr + VNET_HDR_SIZE, buffer, sizeof(test));
     g_assert_cmpstr(buffer, ==, "TEST");

     guest_free(alloc, req_addr);
diff --git a/tests/virtio-scsi-test.c b/tests/virtio-scsi-test.c
index 30a341df2f..2cd8db9a9a 100644
--- a/tests/virtio-scsi-test.c
+++ b/tests/virtio-scsi-test.c
@@ -77,7 +77,7 @@ static uint64_t qvirtio_scsi_alloc(QVirtIOSCSI *vs, size_t alloc_size,

     addr = guest_alloc(vs->qs->alloc, alloc_size);
     if (data) {
-        memwrite(addr, data, alloc_size);
+        memwrite(global_qtest, addr, data, alloc_size);
     }

     return addr;
@@ -129,7 +129,7 @@ static uint8_t virtio_scsi_do_command(QVirtIOSCSI *vs, const uint8_t *cdb,
                      offsetof(struct virtio_scsi_cmd_resp, response));

     if (resp_out) {
-        memread(resp_addr, resp_out, sizeof(*resp_out));
+        memread(global_qtest, resp_addr, resp_out, sizeof(*resp_out));
     }

     guest_free(vs->qs->alloc, req_addr);
-- 
2.13.5

^ permalink raw reply related	[flat|nested] 75+ messages in thread

* [Qemu-devel] [PATCH v7 36/38] libqtest: Merge qtest_memset() with qmemset()
  2017-09-11 17:19 [Qemu-devel] [PATCH v7 00/38] Preliminary libqtest cleanups Eric Blake
                   ` (34 preceding siblings ...)
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 35/38] libqtest: Merge qtest_{mem, buf}{read, write}() with {mem, buf}{read, write}() Eric Blake
@ 2017-09-11 17:20 ` Eric Blake
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 37/38] libqtest: Separate qmp_discard_response() from command Eric Blake
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 38/38] libqtest: Merge qtest_hmp() with hmp() Eric Blake
  37 siblings, 0 replies; 75+ messages in thread
From: Eric Blake @ 2017-09-11 17:20 UTC (permalink / raw)
  To: qemu-devel; +Cc: armbru, pbonzini, thuth, John Snow, open list:IDE

Maintaining two layers of libqtest APIs, one that takes an explicit
QTestState object, and the other that uses the implicit global_qtest,
is annoying.  In the interest of getting rid of global implicit
state and having less code to maintain, merge:
 qtest_memset()
with its short counterpart.  All callers that previously used the
short form now make it explicit that they are relying on
global_qtest, and later patches can then clean things up to remove
the global variable.

Signed-off-by: Eric Blake <eblake@redhat.com>
---
 tests/libqtest.h    | 17 ++---------------
 tests/libqtest.c    |  2 +-
 tests/ahci-test.c   |  2 +-
 tests/libqos/ahci.c | 12 ++++++------
 4 files changed, 10 insertions(+), 23 deletions(-)

diff --git a/tests/libqtest.h b/tests/libqtest.h
index adf04327b7..8f2946ff5e 100644
--- a/tests/libqtest.h
+++ b/tests/libqtest.h
@@ -405,7 +405,7 @@ void memwrite(QTestState *s, uint64_t addr, const void *data, size_t size);
 void bufwrite(QTestState *s, uint64_t addr, const void *data, size_t size);

 /**
- * qtest_memset:
+ * qmemset:
  * @s: #QTestState instance to operate on.
  * @addr: Guest address to write to.
  * @patt: Byte pattern to fill the guest memory region with.
@@ -413,7 +413,7 @@ void bufwrite(QTestState *s, uint64_t addr, const void *data, size_t size);
  *
  * Write a pattern to guest memory.
  */
-void qtest_memset(QTestState *s, uint64_t addr, uint8_t patt, size_t size);
+void qmemset(QTestState *s, uint64_t addr, uint8_t patt, size_t size);

 /**
  * clock_step_next:
@@ -592,19 +592,6 @@ static inline QDict *qmp_eventwait_ref(const char *event)
  */
 char *hmp(const char *fmt, ...) GCC_FMT_ATTR(1, 2);

-/**
- * qmemset:
- * @addr: Guest address to write to.
- * @patt: Byte pattern to fill the guest memory region with.
- * @size: Number of bytes to write.
- *
- * Write a pattern to guest memory.
- */
-static inline void qmemset(uint64_t addr, uint8_t patt, size_t size)
-{
-    qtest_memset(global_qtest, addr, patt, size);
-}
-
 QDict *qmp_fd_receive(int fd);
 void qmp_fd_sendv(int fd, const char *fmt, va_list ap);
 void qmp_fd_send(int fd, const char *fmt, ...);
diff --git a/tests/libqtest.c b/tests/libqtest.c
index a52dc635f4..ac38f6d4bb 100644
--- a/tests/libqtest.c
+++ b/tests/libqtest.c
@@ -929,7 +929,7 @@ void memwrite(QTestState *s, uint64_t addr, const void *data, size_t size)
     g_free(enc);
 }

-void qtest_memset(QTestState *s, uint64_t addr, uint8_t pattern, size_t size)
+void qmemset(QTestState *s, uint64_t addr, uint8_t pattern, size_t size)
 {
     qtest_sendf(s, "memset 0x%" PRIx64 " 0x%zx 0x%02x\n", addr, size, pattern);
     qtest_rsp(s, 0);
diff --git a/tests/ahci-test.c b/tests/ahci-test.c
index 4b5f0b23fb..18af01501f 100644
--- a/tests/ahci-test.c
+++ b/tests/ahci-test.c
@@ -873,7 +873,7 @@ static void ahci_test_io_rw_simple(AHCIQState *ahci, unsigned bufsize,

     /* Write this buffer to disk, then read it back to the DMA buffer. */
     ahci_guest_io(ahci, port, write_cmd, ptr, bufsize, sector);
-    qtest_memset(ahci->parent->qts, ptr, 0x00, bufsize);
+    qmemset(ahci->parent->qts, ptr, 0x00, bufsize);
     ahci_guest_io(ahci, port, read_cmd, ptr, bufsize, sector);

     /*** Read back the Data ***/
diff --git a/tests/libqos/ahci.c b/tests/libqos/ahci.c
index 433952c11a..f60f60fff5 100644
--- a/tests/libqos/ahci.c
+++ b/tests/libqos/ahci.c
@@ -283,8 +283,8 @@ void ahci_hba_enable(AHCIQState *ahci)
         /* Allocate Memory for the Command List Buffer & FIS Buffer */
         /* PxCLB space ... 0x20 per command, as in 4.2.2 p 36 */
         ahci->port[i].clb = ahci_alloc(ahci, num_cmd_slots * 0x20);
-        qtest_memset(ahci->parent->qts, ahci->port[i].clb, 0x00,
-                     num_cmd_slots * 0x20);
+        qmemset(ahci->parent->qts, ahci->port[i].clb, 0x00,
+                num_cmd_slots * 0x20);
         g_test_message("CLB: 0x%08" PRIx64, ahci->port[i].clb);
         ahci_px_wreg(ahci, i, AHCI_PX_CLB, ahci->port[i].clb);
         g_assert_cmphex(ahci->port[i].clb, ==,
@@ -292,7 +292,7 @@ void ahci_hba_enable(AHCIQState *ahci)

         /* PxFB space ... 0x100, as in 4.2.1 p 35 */
         ahci->port[i].fb = ahci_alloc(ahci, 0x100);
-        qtest_memset(ahci->parent->qts, ahci->port[i].fb, 0x00, 0x100);
+        qmemset(ahci->parent->qts, ahci->port[i].fb, 0x00, 0x100);
         g_test_message("FB: 0x%08" PRIx64, ahci->port[i].fb);
         ahci_px_wreg(ahci, i, AHCI_PX_FB, ahci->port[i].fb);
         g_assert_cmphex(ahci->port[i].fb, ==,
@@ -398,7 +398,7 @@ void ahci_port_clear(AHCIQState *ahci, uint8_t port)
     g_assert_cmphex(ahci_px_rreg(ahci, port, AHCI_PX_IS), ==, 0);

     /* Wipe the FIS-Receive Buffer */
-    qtest_memset(ahci->parent->qts, ahci->port[port].fb, 0x00, 0x100);
+    qmemset(ahci->parent->qts, ahci->port[port].fb, 0x00, 0x100);
 }

 /**
@@ -637,7 +637,7 @@ void ahci_exec(AHCIQState *ahci, uint8_t port,
     if (opts->size && !opts->buffer) {
         opts->buffer = ahci_alloc(ahci, opts->size);
         g_assert(opts->buffer);
-        qtest_memset(ahci->parent->qts, opts->buffer, 0x00, opts->size);
+        qmemset(ahci->parent->qts, opts->buffer, 0x00, opts->size);
     }

     /* Command creation */
@@ -755,7 +755,7 @@ void ahci_io(AHCIQState *ahci, uint8_t port, uint8_t ide_cmd,
     g_assert(props);
     ptr = ahci_alloc(ahci, bufsize);
     g_assert(!bufsize || ptr);
-    qtest_memset(ahci->parent->qts, ptr, 0x00, bufsize);
+    qmemset(ahci->parent->qts, ptr, 0x00, bufsize);

     if (bufsize && props->write) {
         bufwrite(ahci->parent->qts, ptr, buffer, bufsize);
-- 
2.13.5

^ permalink raw reply related	[flat|nested] 75+ messages in thread

* [Qemu-devel] [PATCH v7 37/38] libqtest: Separate qmp_discard_response() from command
  2017-09-11 17:19 [Qemu-devel] [PATCH v7 00/38] Preliminary libqtest cleanups Eric Blake
                   ` (35 preceding siblings ...)
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 36/38] libqtest: Merge qtest_memset() with qmemset() Eric Blake
@ 2017-09-11 17:20 ` Eric Blake
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 38/38] libqtest: Merge qtest_hmp() with hmp() Eric Blake
  37 siblings, 0 replies; 75+ messages in thread
From: Eric Blake @ 2017-09-11 17:20 UTC (permalink / raw)
  To: qemu-devel
  Cc: armbru, pbonzini, thuth, John Snow, Stefan Hajnoczi, open list:IDE

Upcoming patches will be adding new convenience methods for
constructing QMP commands.  But making every variation of sending
support every variation of response handling becomes unwieldy;
it's easier to specify that discarding a JSON response is
unassociated with sending the command, where qmp_async() already
fits the bill for sending a command without tying up a reference
to the response.

Doing this renders qtest_qmpv_discard_response() unused.

Bonus: gets rid of a non-literal format string in ide-test, which
is a step towards compile-time format string checking without
triggering -Wformat-nonliteral.

Signed-off-by: Eric Blake <eblake@redhat.com>
---
 tests/libqtest.h               | 20 ++++----------------
 tests/libqtest.c               | 27 ++++++++-------------------
 tests/ahci-test.c              | 26 +++++++++++++-------------
 tests/boot-order-test.c        |  2 +-
 tests/drive_del-test.c         |  5 +++--
 tests/fdc-test.c               | 11 ++++++-----
 tests/ide-test.c               |  5 ++---
 tests/postcopy-test.c          |  3 ++-
 tests/test-filter-mirror.c     |  3 ++-
 tests/test-filter-redirector.c |  6 ++++--
 tests/virtio-blk-test.c        | 21 ++++++++++++---------
 11 files changed, 57 insertions(+), 72 deletions(-)

diff --git a/tests/libqtest.h b/tests/libqtest.h
index 8f2946ff5e..b811394d4d 100644
--- a/tests/libqtest.h
+++ b/tests/libqtest.h
@@ -69,11 +69,10 @@ void qtest_quit(QTestState *s);
 /**
  * qtest_qmp_discard_response:
  * @s: #QTestState instance to operate on.
- * @fmt...: QMP message to send to qemu
  *
- * Sends a QMP message to QEMU and consumes the response.
+ * Read and discard a QMP response, typically after qtest_async_qmp().
  */
-void qtest_qmp_discard_response(QTestState *s, const char *fmt, ...);
+void qtest_qmp_discard_response(QTestState *s);

 /**
  * qtest_qmp:
@@ -94,16 +93,6 @@ QDict *qtest_qmp(QTestState *s, const char *fmt, ...);
 void qtest_async_qmp(QTestState *s, const char *fmt, ...);

 /**
- * qtest_qmpv_discard_response:
- * @s: #QTestState instance to operate on.
- * @fmt: QMP message to send to QEMU
- * @ap: QMP message arguments
- *
- * Sends a QMP message to QEMU and consumes the response.
- */
-void qtest_qmpv_discard_response(QTestState *s, const char *fmt, va_list ap);
-
-/**
  * qtest_qmpv:
  * @s: #QTestState instance to operate on.
  * @fmt: QMP message to send to QEMU
@@ -543,11 +532,10 @@ void qmp_async(const char *fmt, ...);

 /**
  * qmp_discard_response:
- * @fmt...: QMP message to send to qemu
  *
- * Sends a QMP message to QEMU and consumes the response.
+ * Read and discard a QMP response, typically after qmp_async().
  */
-void qmp_discard_response(const char *fmt, ...);
+void qmp_discard_response(void);

 /**
  * qmp_receive:
diff --git a/tests/libqtest.c b/tests/libqtest.c
index ac38f6d4bb..747411b5b5 100644
--- a/tests/libqtest.c
+++ b/tests/libqtest.c
@@ -238,8 +238,9 @@ QTestState *qtest_start(const char *extra_args)
     QTestState *s = qtest_start_without_qmp_handshake(extra_args);

     /* Read the QMP greeting and then do the handshake */
-    qtest_qmp_discard_response(s, "");
-    qtest_qmp_discard_response(s, "{ 'execute': 'qmp_capabilities' }");
+    qtest_qmp_discard_response(s);
+    qtest_async_qmp(s, "{ 'execute': 'qmp_capabilities' }");
+    qtest_qmp_discard_response(s);

     assert(!global_qtest);
     return s;
@@ -566,20 +567,10 @@ void qtest_async_qmp(QTestState *s, const char *fmt, ...)
     va_end(ap);
 }

-void qtest_qmpv_discard_response(QTestState *s, const char *fmt, va_list ap)
+void qtest_qmp_discard_response(QTestState *s)
 {
-    QDict *response = qtest_qmpv(s, fmt, ap);
-    QDECREF(response);
-}
+    QDict *response = qtest_qmp_receive(s);

-void qtest_qmp_discard_response(QTestState *s, const char *fmt, ...)
-{
-    va_list ap;
-    QDict *response;
-
-    va_start(ap, fmt);
-    response = qtest_qmpv(s, fmt, ap);
-    va_end(ap);
     QDECREF(response);
 }

@@ -955,13 +946,11 @@ void qmp_async(const char *fmt, ...)
     va_end(ap);
 }

-void qmp_discard_response(const char *fmt, ...)
+void qmp_discard_response(void)
 {
-    va_list ap;
+    QDict *response = qmp_receive();

-    va_start(ap, fmt);
-    qtest_qmpv_discard_response(global_qtest, fmt, ap);
-    va_end(ap);
+    QDECREF(response);
 }
 char *hmp(const char *fmt, ...)
 {
diff --git a/tests/ahci-test.c b/tests/ahci-test.c
index 18af01501f..a0ed334604 100644
--- a/tests/ahci-test.c
+++ b/tests/ahci-test.c
@@ -1589,9 +1589,10 @@ static void test_atapi_tray(void)
     rsp = qtest_qmp_receive(ahci->parent->qts);
     QDECREF(rsp);

-    qtest_qmp_discard_response(ahci->parent->qts,
-                               "{'execute': 'x-blockdev-remove-medium', "
-                               "'arguments': {'device': 'drive0'}}");
+    qtest_async_qmp(ahci->parent->qts,
+                    "{'execute': 'x-blockdev-remove-medium', "
+                    "'arguments': {'device': 'drive0'}}");
+    qtest_qmp_discard_response(ahci->parent->qts);

     /* Test the tray without a medium */
     ahci_atapi_load(ahci, port);
@@ -1601,16 +1602,15 @@ static void test_atapi_tray(void)
     atapi_wait_tray(ahci, true);

     /* Re-insert media */
-    qtest_qmp_discard_response(ahci->parent->qts,
-                               "{'execute': 'blockdev-add', "
-                               "'arguments': {'node-name': 'node0', "
-                                        "'driver': 'raw', "
-                                        "'file': { 'driver': 'file', "
-                                                  "'filename': %s }}}", iso);
-    qtest_qmp_discard_response(ahci->parent->qts,
-                               "{'execute': 'x-blockdev-insert-medium',"
-                               "'arguments': { 'device': 'drive0', "
-                                         "'node-name': 'node0' }}");
+    qtest_async_qmp(ahci->parent->qts,
+                    "{'execute': 'blockdev-add', 'arguments': {"
+                    "  'node-name': 'node0', 'driver': 'raw', "
+                    "  'file': { 'driver': 'file', 'filename': %s }}}", iso);
+    qtest_qmp_discard_response(ahci->parent->qts);
+    qtest_async_qmp(ahci->parent->qts,
+                    "{'execute': 'x-blockdev-insert-medium',"
+                    "'arguments': {'device': 'drive0', 'node-name': 'node0'}}");
+    qtest_qmp_discard_response(ahci->parent->qts);

     /* Again, the event shows up first */
     qtest_async_qmp(ahci->parent->qts, "{'execute': 'blockdev-close-tray', "
diff --git a/tests/boot-order-test.c b/tests/boot-order-test.c
index 048c8721c6..5cd793e075 100644
--- a/tests/boot-order-test.c
+++ b/tests/boot-order-test.c
@@ -36,7 +36,7 @@ static void test_a_boot_order(const char *machine,
                                 test_args);
     actual = read_boot_order();
     g_assert_cmphex(actual, ==, expected_boot);
-    qmp_discard_response("{ 'execute': 'system_reset' }");
+    qmp_async("{ 'execute': 'system_reset' }");
     /*
      * system_reset only requests reset.  We get a RESET event after
      * the actual reset completes.  Need to wait for that.
diff --git a/tests/drive_del-test.c b/tests/drive_del-test.c
index f68c4bc176..0fde0c2040 100644
--- a/tests/drive_del-test.c
+++ b/tests/drive_del-test.c
@@ -34,8 +34,9 @@ static void device_del(void)
     QDict *response;

     /* Complication: ignore DEVICE_DELETED event */
-    qmp_discard_response("{'execute': 'device_del',"
-                         " 'arguments': { 'id': 'dev0' } }");
+    qmp_async("{'execute': 'device_del',"
+              " 'arguments': { 'id': 'dev0' } }");
+    qmp_discard_response();
     response = qmp_receive();
     g_assert(response);
     g_assert(qdict_haskey(response, "return"));
diff --git a/tests/fdc-test.c b/tests/fdc-test.c
index 7803ff2789..241b13e4de 100644
--- a/tests/fdc-test.c
+++ b/tests/fdc-test.c
@@ -298,9 +298,10 @@ static void test_media_insert(void)

     /* Insert media in drive. DSKCHK should not be reset until a step pulse
      * is sent. */
-    qmp_discard_response("{'execute':'blockdev-change-medium', 'arguments':{"
-                         " 'id':'floppy0', 'filename': %s, 'format': 'raw' }}",
-                         test_image);
+    qmp_async("{'execute':'blockdev-change-medium', 'arguments':{"
+              " 'id':'floppy0', 'filename': %s, 'format': 'raw' }}",
+              test_image);
+    qmp_discard_response();

     dir = inb(global_qtest, FLOPPY_BASE + reg_dir);
     assert_bit_set(dir, DSKCHG);
@@ -329,8 +330,8 @@ static void test_media_change(void)

     /* Eject the floppy and check that DSKCHG is set. Reading it out doesn't
      * reset the bit. */
-    qmp_discard_response("{'execute':'eject', 'arguments':{"
-                         " 'id':'floppy0' }}");
+    qmp_async("{'execute':'eject', 'arguments':{ 'id':'floppy0' }}");
+    qmp_discard_response();

     dir = inb(global_qtest, FLOPPY_BASE + reg_dir);
     assert_bit_set(dir, DSKCHG);
diff --git a/tests/ide-test.c b/tests/ide-test.c
index c47cbb1949..6ba3a8589d 100644
--- a/tests/ide-test.c
+++ b/tests/ide-test.c
@@ -621,7 +621,6 @@ static void test_retry_flush(const char *machine)
     QPCIDevice *dev;
     QPCIBar bmdma_bar, ide_bar;
     uint8_t data;
-    const char *s;

     prepare_blkdebug_script(debug_path, "flush_to_disk");

@@ -649,8 +648,8 @@ static void test_retry_flush(const char *machine)
     qmp_eventwait("STOP");

     /* Complete the command */
-    s = "{'execute':'cont' }";
-    qmp_discard_response(s);
+    qmp_async("{'execute':'cont' }");
+    qmp_discard_response();

     /* Check registers */
     data = qpci_io_readb(dev, ide_bar, reg_device);
diff --git a/tests/postcopy-test.c b/tests/postcopy-test.c
index 04889b8078..9c995ee3e5 100644
--- a/tests/postcopy-test.c
+++ b/tests/postcopy-test.c
@@ -478,7 +478,8 @@ static void test_migrate(void)
         usleep(10 * 1000);
     } while (dest_byte_a == dest_byte_b);

-    qtest_qmp_discard_response(to, "{ 'execute' : 'stop'}");
+    qtest_async_qmp(to, "{ 'execute' : 'stop'}");
+    qtest_qmp_discard_response(to);
     /* With it stopped, check nothing changes */
     memread(to, start_address, &dest_byte_c, 1);
     sleep(1);
diff --git a/tests/test-filter-mirror.c b/tests/test-filter-mirror.c
index c590698991..3fc1211681 100644
--- a/tests/test-filter-mirror.c
+++ b/tests/test-filter-mirror.c
@@ -57,7 +57,8 @@ static void test_mirror(void)
     };

     /* send a qmp command to guarantee that 'connected' is setting to true. */
-    qmp_discard_response("{ 'execute' : 'query-status'}");
+    qmp_async("{ 'execute' : 'query-status'}");
+    qmp_discard_response();
     ret = iov_send(send_sock[0], iov, 2, 0, sizeof(size) + sizeof(send_buf));
     g_assert_cmpint(ret, ==, sizeof(send_buf) + sizeof(size));
     close(send_sock[0]);
diff --git a/tests/test-filter-redirector.c b/tests/test-filter-redirector.c
index bcb338e7c1..a10649d4fc 100644
--- a/tests/test-filter-redirector.c
+++ b/tests/test-filter-redirector.c
@@ -104,7 +104,8 @@ static void test_redirector_tx(void)
     g_assert_cmpint(recv_sock, !=, -1);

     /* send a qmp command to guarantee that 'connected' is setting to true. */
-    qmp_discard_response("{ 'execute' : 'query-status'}");
+    qmp_async("{ 'execute' : 'query-status'}");
+    qmp_discard_response();

     struct iovec iov[] = {
         {
@@ -182,7 +183,8 @@ static void test_redirector_rx(void)
     send_sock = unix_connect(sock_path1, NULL);
     g_assert_cmpint(send_sock, !=, -1);
     /* send a qmp command to guarantee that 'connected' is setting to true. */
-    qmp_discard_response("{ 'execute' : 'query-status'}");
+    qmp_async("{ 'execute' : 'query-status'}");
+    qmp_discard_response();

     ret = iov_send(send_sock, iov, 2, 0, sizeof(size) + sizeof(send_buf));
     g_assert_cmpint(ret, ==, sizeof(send_buf) + sizeof(size));
diff --git a/tests/virtio-blk-test.c b/tests/virtio-blk-test.c
index ec582c8e1c..78ca44b520 100644
--- a/tests/virtio-blk-test.c
+++ b/tests/virtio-blk-test.c
@@ -410,9 +410,10 @@ static void pci_config(void)

     qvirtio_set_driver_ok(&dev->vdev);

-    qmp_discard_response("{ 'execute': 'block_resize', "
-                         " 'arguments': { 'device': 'drive0', "
-                         " 'size': %d } }", n_size);
+    qmp_async("{ 'execute': 'block_resize', "
+              " 'arguments': { 'device': 'drive0', "
+              " 'size': %d } }", n_size);
+    qmp_discard_response();
     qvirtio_wait_config_isr(&dev->vdev, QVIRTIO_BLK_TIMEOUT_US);

     capacity = qvirtio_config_readq(&dev->vdev, 0);
@@ -460,9 +461,10 @@ static void pci_msix(void)

     qvirtio_set_driver_ok(&dev->vdev);

-    qmp_discard_response("{ 'execute': 'block_resize', "
-                         " 'arguments': { 'device': 'drive0', "
-                         " 'size': %d } }", n_size);
+    qmp_async("{ 'execute': 'block_resize', "
+              " 'arguments': { 'device': 'drive0', "
+              " 'size': %d } }", n_size);
+    qmp_discard_response();

     qvirtio_wait_config_isr(&dev->vdev, QVIRTIO_BLK_TIMEOUT_US);

@@ -702,9 +704,10 @@ static void mmio_basic(void)

     test_basic(&dev->vdev, alloc, vq);

-    qmp_discard_response("{ 'execute': 'block_resize', "
-                         " 'arguments': { 'device': 'drive0', "
-                         " 'size': %d } }", n_size);
+    qmp_async("{ 'execute': 'block_resize', "
+              " 'arguments': { 'device': 'drive0', "
+              " 'size': %d } }", n_size);
+    qmp_discard_response();

     qvirtio_wait_queue_isr(&dev->vdev, vq, QVIRTIO_BLK_TIMEOUT_US);

-- 
2.13.5

^ permalink raw reply related	[flat|nested] 75+ messages in thread

* [Qemu-devel] [PATCH v7 38/38] libqtest: Merge qtest_hmp() with hmp()
  2017-09-11 17:19 [Qemu-devel] [PATCH v7 00/38] Preliminary libqtest cleanups Eric Blake
                   ` (36 preceding siblings ...)
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 37/38] libqtest: Separate qmp_discard_response() from command Eric Blake
@ 2017-09-11 17:20 ` Eric Blake
  37 siblings, 0 replies; 75+ messages in thread
From: Eric Blake @ 2017-09-11 17:20 UTC (permalink / raw)
  To: qemu-devel
  Cc: armbru, pbonzini, thuth, John Snow, Dr. David Alan Gilbert,
	open list:IDE

Maintaining two layers of libqtest APIs, one that takes an explicit
QTestState object, and the other that uses the implicit global_qtest,
is annoying.  In the interest of getting rid of global implicit
state and having less code to maintain, merge:
 qtest_hmp()
with its short counterpart, and delete qtest_hmpv() as unused (not
to mention that a 'v' suffix is unusual, when compared to the
printf family or even our new qtest_vstartf() where the va_list
counterpart uses 'v' as a prefix).  All callers that previously
used the short form now make it explicit that they are relying on
global_qtest, and later patches can then clean things up to remove
the global variable.

Signed-off-by: Eric Blake <eblake@redhat.com>
---
 tests/libqtest.h               | 27 ++-------------------------
 tests/libqtest.c               | 26 ++++----------------------
 tests/device-introspect-test.c |  6 +++---
 tests/drive_del-test.c         |  4 ++--
 tests/ide-test.c               |  4 ++--
 tests/numa-test.c              |  6 +++---
 tests/test-hmp.c               |  6 +++---
 7 files changed, 19 insertions(+), 60 deletions(-)

diff --git a/tests/libqtest.h b/tests/libqtest.h
index b811394d4d..34986955d3 100644
--- a/tests/libqtest.h
+++ b/tests/libqtest.h
@@ -140,7 +140,7 @@ void qtest_qmp_eventwait(QTestState *s, const char *event);
 QDict *qtest_qmp_eventwait_ref(QTestState *s, const char *event);

 /**
- * qtest_hmp:
+ * hmp:
  * @s: #QTestState instance to operate on.
  * @fmt...: HMP command to send to QEMU, formats arguments like sprintf().
  *
@@ -149,20 +149,7 @@ QDict *qtest_qmp_eventwait_ref(QTestState *s, const char *event);
  *
  * Returns: the command's output.  The caller should g_free() it.
  */
-char *qtest_hmp(QTestState *s, const char *fmt, ...) GCC_FMT_ATTR(2, 3);
-
-/**
- * qtest_hmpv:
- * @s: #QTestState instance to operate on.
- * @fmt: HMP command to send to QEMU
- * @ap: HMP command arguments
- *
- * Send HMP command to QEMU via QMP's human-monitor-command.
- * QMP events are discarded.
- *
- * Returns: the command's output.  The caller should g_free() it.
- */
-char *qtest_hmpv(QTestState *s, const char *fmt, va_list ap);
+char *hmp(QTestState *s, const char *fmt, ...) GCC_FMT_ATTR(2, 3);

 /**
  * get_irq:
@@ -570,16 +557,6 @@ static inline QDict *qmp_eventwait_ref(const char *event)
     return qtest_qmp_eventwait_ref(global_qtest, event);
 }

-/**
- * hmp:
- * @fmt...: HMP command to send to QEMU, formats arguments like sprintf().
- *
- * Send HMP command to QEMU via QMP's human-monitor-command.
- *
- * Returns: the command's output.  The caller should g_free() it.
- */
-char *hmp(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
-
 QDict *qmp_fd_receive(int fd);
 void qmp_fd_sendv(int fd, const char *fmt, va_list ap);
 void qmp_fd_send(int fd, const char *fmt, ...);
diff --git a/tests/libqtest.c b/tests/libqtest.c
index 747411b5b5..1641c3ae4c 100644
--- a/tests/libqtest.c
+++ b/tests/libqtest.c
@@ -596,13 +596,16 @@ void qtest_qmp_eventwait(QTestState *s, const char *event)
     QDECREF(response);
 }

-char *qtest_hmpv(QTestState *s, const char *fmt, va_list ap)
+char *hmp(QTestState *s, const char *fmt, ...)
 {
+    va_list ap;
     char *cmd;
     QDict *resp;
     char *ret;

+    va_start(ap, fmt);
     cmd = g_strdup_vprintf(fmt, ap);
+    va_end(ap);
     resp = qtest_qmp(s, "{'execute': 'human-monitor-command',"
                      " 'arguments': {'command-line': %s}}",
                      cmd);
@@ -619,17 +622,6 @@ char *qtest_hmpv(QTestState *s, const char *fmt, va_list ap)
     return ret;
 }

-char *qtest_hmp(QTestState *s, const char *fmt, ...)
-{
-    va_list ap;
-    char *ret;
-
-    va_start(ap, fmt);
-    ret = qtest_hmpv(s, fmt, ap);
-    va_end(ap);
-    return ret;
-}
-
 const char *qtest_get_arch(void)
 {
     const char *qemu = getenv("QTEST_QEMU_BINARY");
@@ -952,16 +944,6 @@ void qmp_discard_response(void)

     QDECREF(response);
 }
-char *hmp(const char *fmt, ...)
-{
-    va_list ap;
-    char *ret;
-
-    va_start(ap, fmt);
-    ret = qtest_hmpv(global_qtest, fmt, ap);
-    va_end(ap);
-    return ret;
-}

 bool qtest_big_endian(QTestState *s)
 {
diff --git a/tests/device-introspect-test.c b/tests/device-introspect-test.c
index ed4f5f2eed..7dbd27330a 100644
--- a/tests/device-introspect-test.c
+++ b/tests/device-introspect-test.c
@@ -110,14 +110,14 @@ static void test_one_device(const char *type)
                type);
     QDECREF(resp);

-    help = hmp("device_add \"%s,help\"", type);
+    help = hmp(global_qtest, "device_add \"%s,help\"", type);
     g_free(help);

     /*
      * Some devices leave dangling pointers in QOM behind.
      * "info qom-tree" has a good chance at crashing then
      */
-    qom_tree = hmp("info qom-tree");
+    qom_tree = hmp(global_qtest, "info qom-tree");
     g_free(qom_tree);
 }

@@ -131,7 +131,7 @@ static void test_device_intro_list(void)
     types = device_type_list(true);
     QDECREF(types);

-    help = hmp("device_add help");
+    help = hmp(global_qtest, "device_add help");
     g_free(help);

     qtest_quit(global_qtest);
diff --git a/tests/drive_del-test.c b/tests/drive_del-test.c
index 0fde0c2040..6b53ac5a1a 100644
--- a/tests/drive_del-test.c
+++ b/tests/drive_del-test.c
@@ -15,7 +15,7 @@

 static void drive_add(void)
 {
-    char *resp = hmp("drive_add 0 if=none,id=drive0");
+    char *resp = hmp(global_qtest, "drive_add 0 if=none,id=drive0");

     g_assert_cmpstr(resp, ==, "OK\r\n");
     g_free(resp);
@@ -23,7 +23,7 @@ static void drive_add(void)

 static void drive_del(void)
 {
-    char *resp = hmp("drive_del drive0");
+    char *resp = hmp(global_qtest, "drive_del drive0");

     g_assert_cmpstr(resp, ==, "");
     g_free(resp);
diff --git a/tests/ide-test.c b/tests/ide-test.c
index 6ba3a8589d..5e7fa6e461 100644
--- a/tests/ide-test.c
+++ b/tests/ide-test.c
@@ -587,7 +587,7 @@ static void test_flush(void)
     make_dirty(0);

     /* Delay the completion of the flush request until we explicitly do it */
-    g_free(hmp("qemu-io ide0-hd0 \"break flush_to_os A\""));
+    g_free(hmp(global_qtest, "qemu-io ide0-hd0 \"break flush_to_os A\""));

     /* FLUSH CACHE command on device 0*/
     qpci_io_writeb(dev, ide_bar, reg_device, 0);
@@ -599,7 +599,7 @@ static void test_flush(void)
     assert_bit_clear(data, DF | ERR | DRQ);

     /* Complete the command */
-    g_free(hmp("qemu-io ide0-hd0 \"resume A\""));
+    g_free(hmp(global_qtest, "qemu-io ide0-hd0 \"resume A\""));

     /* Check registers */
     data = qpci_io_readb(dev, ide_bar, reg_device);
diff --git a/tests/numa-test.c b/tests/numa-test.c
index e2f6c68be8..a7e2b183f7 100644
--- a/tests/numa-test.c
+++ b/tests/numa-test.c
@@ -21,7 +21,7 @@ static void test_mon_explicit(const void *data)
                                 "-numa node,nodeid=0,cpus=0-3 "
                                 "-numa node,nodeid=1,cpus=4-7 ", args);

-    s = hmp("info numa");
+    s = hmp(global_qtest, "info numa");
     g_assert(strstr(s, "node 0 cpus: 0 1 2 3"));
     g_assert(strstr(s, "node 1 cpus: 4 5 6 7"));
     g_free(s);
@@ -36,7 +36,7 @@ static void test_mon_default(const void *data)

     global_qtest = qtest_startf("%s -smp 8 -numa node -numa node", args);

-    s = hmp("info numa");
+    s = hmp(global_qtest, "info numa");
     g_assert(strstr(s, "node 0 cpus: 0 2 4 6"));
     g_assert(strstr(s, "node 1 cpus: 1 3 5 7"));
     g_free(s);
@@ -53,7 +53,7 @@ static void test_mon_partial(const void *data)
                                 "-numa node,nodeid=0,cpus=0-1 "
                                 "-numa node,nodeid=1,cpus=4-5 ", args);

-    s = hmp("info numa");
+    s = hmp(global_qtest, "info numa");
     g_assert(strstr(s, "node 0 cpus: 0 1 2 3 6 7"));
     g_assert(strstr(s, "node 1 cpus: 4 5"));
     g_free(s);
diff --git a/tests/test-hmp.c b/tests/test-hmp.c
index b3102daea1..e670a5ee6c 100644
--- a/tests/test-hmp.c
+++ b/tests/test-hmp.c
@@ -80,7 +80,7 @@ static void test_commands(void)
         if (verbose) {
             fprintf(stderr, "\t%s\n", hmp_cmds[i]);
         }
-        response = hmp("%s", hmp_cmds[i]);
+        response = hmp(global_qtest, "%s", hmp_cmds[i]);
         g_free(response);
     }

@@ -91,7 +91,7 @@ static void test_info_commands(void)
 {
     char *resp, *info, *info_buf, *endp;

-    info_buf = info = hmp("help info");
+    info_buf = info = hmp(global_qtest, "help info");

     while (*info) {
         /* Extract the info command, ignore parameters and description */
@@ -103,7 +103,7 @@ static void test_info_commands(void)
         if (verbose) {
             fprintf(stderr, "\t%s\n", info);
         }
-        resp = hmp("%s", info);
+        resp = hmp(global_qtest, "%s", info);
         g_free(resp);
         /* And move forward to the next line */
         info = strchr(endp + 1, '\n');
-- 
2.13.5

^ permalink raw reply related	[flat|nested] 75+ messages in thread

* Re: [Qemu-devel] [PATCH v7 30/38] qtest: Avoid passing raw strings through hmp()
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 30/38] qtest: Avoid passing raw strings through hmp() Eric Blake
@ 2017-09-11 17:42   ` Dr. David Alan Gilbert
  2017-09-12 10:40   ` Thomas Huth
  1 sibling, 0 replies; 75+ messages in thread
From: Dr. David Alan Gilbert @ 2017-09-11 17:42 UTC (permalink / raw)
  To: Eric Blake; +Cc: qemu-devel, armbru, pbonzini, thuth

* Eric Blake (eblake@redhat.com) wrote:
> hmp() passes its string argument through the sprintf() family;
> with a proper attribute, gcc -Wformat warns us when we do something
> dangerous like passing a non-constant format string.  Fortunately,
> all our strings were safe, but checking whether the string can
> contain an unintended % is easy to avoid and therefore worth doing.
> 
> Signed-off-by: Eric Blake <eblake@redhat.com>
> 

Reviewed-by: Dr. David Alan Gilbert <dgilbert@redhat.com>

> ---
> v7: add GCC_FMT_ATTR here, drop R-b
> ---
>  tests/libqtest.h | 8 ++++----
>  tests/test-hmp.c | 4 ++--
>  2 files changed, 6 insertions(+), 6 deletions(-)
> 
> diff --git a/tests/libqtest.h b/tests/libqtest.h
> index 2a21bf4605..5651b77d2f 100644
> --- a/tests/libqtest.h
> +++ b/tests/libqtest.h
> @@ -153,14 +153,14 @@ QDict *qtest_qmp_eventwait_ref(QTestState *s, const char *event);
>  /**
>   * qtest_hmp:
>   * @s: #QTestState instance to operate on.
> - * @fmt...: HMP command to send to QEMU
> + * @fmt...: HMP command to send to QEMU, formats arguments like sprintf().
>   *
>   * Send HMP command to QEMU via QMP's human-monitor-command.
>   * QMP events are discarded.
>   *
>   * Returns: the command's output.  The caller should g_free() it.
>   */
> -char *qtest_hmp(QTestState *s, const char *fmt, ...);
> +char *qtest_hmp(QTestState *s, const char *fmt, ...) GCC_FMT_ATTR(2, 3);
> 
>  /**
>   * qtest_hmpv:
> @@ -585,13 +585,13 @@ static inline QDict *qmp_eventwait_ref(const char *event)
> 
>  /**
>   * hmp:
> - * @fmt...: HMP command to send to QEMU
> + * @fmt...: HMP command to send to QEMU, formats arguments like sprintf().
>   *
>   * Send HMP command to QEMU via QMP's human-monitor-command.
>   *
>   * Returns: the command's output.  The caller should g_free() it.
>   */
> -char *hmp(const char *fmt, ...);
> +char *hmp(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
> 
>  /**
>   * get_irq:
> diff --git a/tests/test-hmp.c b/tests/test-hmp.c
> index 7ff47eda13..b3102daea1 100644
> --- a/tests/test-hmp.c
> +++ b/tests/test-hmp.c
> @@ -80,7 +80,7 @@ static void test_commands(void)
>          if (verbose) {
>              fprintf(stderr, "\t%s\n", hmp_cmds[i]);
>          }
> -        response = hmp(hmp_cmds[i]);
> +        response = hmp("%s", hmp_cmds[i]);
>          g_free(response);
>      }
> 
> @@ -103,7 +103,7 @@ static void test_info_commands(void)
>          if (verbose) {
>              fprintf(stderr, "\t%s\n", info);
>          }
> -        resp = hmp(info);
> +        resp = hmp("%s", info);
>          g_free(resp);
>          /* And move forward to the next line */
>          info = strchr(endp + 1, '\n');
> -- 
> 2.13.5
> 
--
Dr. David Alan Gilbert / dgilbert@redhat.com / Manchester, UK

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [Qemu-devel] [PATCH v7 17/38] libqos: Use explicit QTestState for remaining libqos operations
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 17/38] libqos: Use explicit QTestState for remaining libqos operations Eric Blake
@ 2017-09-11 21:30   ` Greg Kurz
  2017-09-12  0:01   ` John Snow
  1 sibling, 0 replies; 75+ messages in thread
From: Greg Kurz @ 2017-09-11 21:30 UTC (permalink / raw)
  To: Eric Blake
  Cc: qemu-devel, armbru, pbonzini, thuth, John Snow, David Gibson,
	Alexander Graf, Gerd Hoffmann, Aneesh Kumar K.V, Stefan Hajnoczi,
	Jason Wang, open list:IDE, open list:sPAPR

[-- Attachment #1: Type: text/plain, Size: 10806 bytes --]

On Mon, 11 Sep 2017 12:20:01 -0500
Eric Blake <eblake@redhat.com> wrote:

> Drop one more client of global_qtest by teaching all remaining
> libqos stragglers to pass in an explicit QTestState.  Change the
> setting of global_qtest from being implicit in libqos' call to
> qtest_start() to instead be explicit in all clients that are
> still relying on global_qtest.
> 
> Note that qmp_execute() can be greatly simplified in the process,
> and that we also get rid of interpolation of a JSON string into a
> temporary variable when qtest_qmp() can do it more reliably.
> 
> Signed-off-by: Eric Blake <eblake@redhat.com>
> 
> ---
> v7: split libqos changes from test-ahci, catch more spots
> ---

For tests/virtio-9p-test.c.

Acked-by: Greg Kurz <groug@kaod.org>

>  tests/ahci-test.c         |  1 +
>  tests/ivshmem-test.c      |  1 +
>  tests/libqos/libqos-pc.c  |  2 +-
>  tests/libqos/libqos.c     | 30 ++++++++++--------------------
>  tests/megasas-test.c      |  5 ++++-
>  tests/rtas-test.c         |  1 +
>  tests/usb-hcd-uhci-test.c |  1 +
>  C    |  1 +
>  tests/virtio-blk-test.c   |  1 +
>  tests/virtio-net-test.c   | 15 +++++++++------
>  tests/virtio-scsi-test.c  | 16 +++++++++-------
>  11 files changed, 39 insertions(+), 35 deletions(-)
> 
> diff --git a/tests/ahci-test.c b/tests/ahci-test.c
> index c94d1bd712..d6696cc370 100644
> --- a/tests/ahci-test.c
> +++ b/tests/ahci-test.c
> @@ -157,6 +157,7 @@ static AHCIQState *ahci_vboot(const char *cli, va_list ap)
> 
>      s = g_malloc0(sizeof(AHCIQState));
>      s->parent = qtest_pc_vboot(cli, ap);
> +    global_qtest = s->parent->qts;
>      alloc_set_flags(s->parent->alloc, ALLOC_LEAK_ASSERT);
> 
>      /* Verify that we have an AHCI device present. */
> diff --git a/tests/ivshmem-test.c b/tests/ivshmem-test.c
> index d35c922254..d8f8530a4d 100644
> --- a/tests/ivshmem-test.c
> +++ b/tests/ivshmem-test.c
> @@ -131,6 +131,7 @@ static void setup_vm_cmd(IVState *s, const char *cmd, bool msix)
>          g_printerr("ivshmem-test tests are only available on x86 or ppc64\n");
>          exit(EXIT_FAILURE);
>      }
> +    global_qtest = s->qs->qts;
>      s->dev = get_device(s->qs->pcibus);
> 
>      s->reg_bar = qpci_iomap(s->dev, 0, &barsize);
> diff --git a/tests/libqos/libqos-pc.c b/tests/libqos/libqos-pc.c
> index b554758802..a9c1aceaa7 100644
> --- a/tests/libqos/libqos-pc.c
> +++ b/tests/libqos/libqos-pc.c
> @@ -25,7 +25,7 @@ QOSState *qtest_pc_boot(const char *cmdline_fmt, ...)
>      qs = qtest_vboot(&qos_ops, cmdline_fmt, ap);
>      va_end(ap);
> 
> -    qtest_irq_intercept_in(global_qtest, "ioapic");
> +    qtest_irq_intercept_in(qs->qts, "ioapic");
> 
>      return qs;
>  }
> diff --git a/tests/libqos/libqos.c b/tests/libqos/libqos.c
> index 40018b9c07..9798e1d027 100644
> --- a/tests/libqos/libqos.c
> +++ b/tests/libqos/libqos.c
> @@ -20,7 +20,7 @@ QOSState *qtest_vboot(QOSOps *ops, const char *cmdline_fmt, va_list ap)
>      QOSState *qs = g_new0(QOSState, 1);
> 
>      cmdline = g_strdup_vprintf(cmdline_fmt, ap);
> -    qs->qts = qtest_start(cmdline);
> +    qs->qts = qtest_init(cmdline);
>      qs->ops = ops;
>      if (ops) {
>          qs->alloc = ops->init_allocator(qs->qts, ALLOC_NO_FLAGS);
> @@ -80,29 +80,21 @@ void set_context(QOSState *s)
>      global_qtest = s->qts;
>  }
> 
> -static QDict *qmp_execute(const char *command)
> +static QDict *qmp_execute(QTestState *qts, const char *command)
>  {
> -    char *fmt;
> -    QDict *rsp;
> -
> -    fmt = g_strdup_printf("{ 'execute': '%s' }", command);
> -    rsp = qmp(fmt);
> -    g_free(fmt);
> -
> -    return rsp;
> +    return qtest_qmp(qts, "{ 'execute': %s }", command);
>  }
> 
>  void migrate(QOSState *from, QOSState *to, const char *uri)
>  {
>      const char *st;
> -    char *s;
>      QDict *rsp, *sub;
>      bool running;
> 
>      set_context(from);
> 
>      /* Is the machine currently running? */
> -    rsp = qmp_execute("query-status");
> +    rsp = qmp_execute(from->qts, "query-status");
>      g_assert(qdict_haskey(rsp, "return"));
>      sub = qdict_get_qdict(rsp, "return");
>      g_assert(qdict_haskey(sub, "running"));
> @@ -110,30 +102,28 @@ void migrate(QOSState *from, QOSState *to, const char *uri)
>      QDECREF(rsp);
> 
>      /* Issue the migrate command. */
> -    s = g_strdup_printf("{ 'execute': 'migrate',"
> -                        "'arguments': { 'uri': '%s' } }",
> -                        uri);
> -    rsp = qmp(s);
> -    g_free(s);
> +    rsp = qtest_qmp(from->qts,
> +                    "{ 'execute': 'migrate', 'arguments': { 'uri': %s }}",
> +                    uri);
>      g_assert(qdict_haskey(rsp, "return"));
>      QDECREF(rsp);
> 
>      /* Wait for STOP event, but only if we were running: */
>      if (running) {
> -        qmp_eventwait("STOP");
> +        qtest_qmp_eventwait(from->qts, "STOP");
>      }
> 
>      /* If we were running, we can wait for an event. */
>      if (running) {
>          migrate_allocator(from->alloc, to->alloc);
>          set_context(to);
> -        qmp_eventwait("RESUME");
> +        qtest_qmp_eventwait(to->qts, "RESUME");
>          return;
>      }
> 
>      /* Otherwise, we need to wait: poll until migration is completed. */
>      while (1) {
> -        rsp = qmp_execute("query-migrate");
> +        rsp = qmp_execute(from->qts, "query-migrate");
>          g_assert(qdict_haskey(rsp, "return"));
>          sub = qdict_get_qdict(rsp, "return");
>          g_assert(qdict_haskey(sub, "status"));
> diff --git a/tests/megasas-test.c b/tests/megasas-test.c
> index ce960e7f81..81837e14af 100644
> --- a/tests/megasas-test.c
> +++ b/tests/megasas-test.c
> @@ -15,13 +15,16 @@
> 
>  static QOSState *qmegasas_start(const char *extra_opts)
>  {
> +    QOSState *qs;
>      const char *arch = qtest_get_arch();
>      const char *cmd = "-drive id=hd0,if=none,file=null-co://,format=raw "
>                        "-device megasas,id=scsi0,addr=04.0 "
>                        "-device scsi-hd,bus=scsi0.0,drive=hd0 %s";
> 
>      if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
> -        return qtest_pc_boot(cmd, extra_opts ? : "");
> +        qs = qtest_pc_boot(cmd, extra_opts ? : "");
> +        global_qtest = qs->qts;
> +        return qs;
>      }
> 
>      g_printerr("virtio-scsi tests are only available on x86 or ppc64\n");
> diff --git a/tests/rtas-test.c b/tests/rtas-test.c
> index 34a692163f..009bda6d23 100644
> --- a/tests/rtas-test.c
> +++ b/tests/rtas-test.c
> @@ -14,6 +14,7 @@ static void test_rtas_get_time_of_day(void)
>      time_t t1, t2;
> 
>      qs = qtest_spapr_boot("-machine pseries");
> +    global_qtest = qs->qts;
> 
>      t1 = time(NULL);
>      ret = qrtas_get_time_of_day(qs->qts, qs->alloc, &tm, &ns);
> diff --git a/tests/usb-hcd-uhci-test.c b/tests/usb-hcd-uhci-test.c
> index 0fb7f8d223..290416d845 100644
> --- a/tests/usb-hcd-uhci-test.c
> +++ b/tests/usb-hcd-uhci-test.c
> @@ -95,6 +95,7 @@ int main(int argc, char **argv)
>                     "available on x86 or ppc64\n");
>          exit(EXIT_FAILURE);
>      }
> +    global_qtest = qs->qts;
>      ret = g_test_run();
>      qtest_shutdown(qs);
> 
> diff --git a/tests/virtio-9p-test.c b/tests/virtio-9p-test.c
> index ad33d96387..07cbb7f37c 100644
> --- a/tests/virtio-9p-test.c
> +++ b/tests/virtio-9p-test.c
> @@ -46,6 +46,7 @@ static QVirtIO9P *qvirtio_9p_start(const char *driver)
>          g_printerr("virtio-9p tests are only available on x86 or ppc64\n");
>          exit(EXIT_FAILURE);
>      }
> +    global_qtest = v9p->qs->qts;
> 
>      return v9p;
>  }
> diff --git a/tests/virtio-blk-test.c b/tests/virtio-blk-test.c
> index 99bb6f26cd..0cf33b6810 100644
> --- a/tests/virtio-blk-test.c
> +++ b/tests/virtio-blk-test.c
> @@ -77,6 +77,7 @@ static QOSState *pci_test_start(void)
>          g_printerr("virtio-blk tests are only available on x86 or ppc64\n");
>          exit(EXIT_FAILURE);
>      }
> +    global_qtest = qs->qts;
>      unlink(tmp_path);
>      g_free(tmp_path);
>      return qs;
> diff --git a/tests/virtio-net-test.c b/tests/virtio-net-test.c
> index 8b43d3dd65..acecef0a3e 100644
> --- a/tests/virtio-net-test.c
> +++ b/tests/virtio-net-test.c
> @@ -53,18 +53,21 @@ static QVirtioPCIDevice *virtio_net_pci_init(QPCIBus *bus, int slot)
> 
>  static QOSState *pci_test_start(int socket)
>  {
> +    QOSState *qs;
>      const char *arch = qtest_get_arch();
>      const char *cmd = "-netdev socket,fd=%d,id=hs0 -device "
>                        "virtio-net-pci,netdev=hs0";
> 
>      if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
> -        return qtest_pc_boot(cmd, socket);
> +        qs = qtest_pc_boot(cmd, socket);
> +    } else if (strcmp(arch, "ppc64") == 0) {
> +        qs = qtest_spapr_boot(cmd, socket);
> +    } else {
> +        g_printerr("virtio-net tests are only available on x86 or ppc64\n");
> +        exit(EXIT_FAILURE);
>      }
> -    if (strcmp(arch, "ppc64") == 0) {
> -        return qtest_spapr_boot(cmd, socket);
> -    }
> -    g_printerr("virtio-net tests are only available on x86 or ppc64\n");
> -    exit(EXIT_FAILURE);
> +    global_qtest = qs->qts;
> +    return qs;
>  }
> 
>  static void driver_init(QVirtioDevice *dev)
> diff --git a/tests/virtio-scsi-test.c b/tests/virtio-scsi-test.c
> index 87a3b6e81a..4d544ddbc3 100644
> --- a/tests/virtio-scsi-test.c
> +++ b/tests/virtio-scsi-test.c
> @@ -34,20 +34,22 @@ typedef struct {
> 
>  static QOSState *qvirtio_scsi_start(const char *extra_opts)
>  {
> +    QOSState *qs;
>      const char *arch = qtest_get_arch();
>      const char *cmd = "-drive id=drv0,if=none,file=null-co://,format=raw "
>                        "-device virtio-scsi-pci,id=vs0 "
>                        "-device scsi-hd,bus=vs0.0,drive=drv0 %s";
> 
>      if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
> -        return qtest_pc_boot(cmd, extra_opts ? : "");
> +        qs = qtest_pc_boot(cmd, extra_opts ? : "");
> +    } else if (strcmp(arch, "ppc64") == 0) {
> +        qs = qtest_spapr_boot(cmd, extra_opts ? : "");
> +    } else {
> +        g_printerr("virtio-scsi tests are only available on x86 or ppc64\n");
> +        exit(EXIT_FAILURE);
>      }
> -    if (strcmp(arch, "ppc64") == 0) {
> -        return qtest_spapr_boot(cmd, extra_opts ? : "");
> -    }
> -
> -    g_printerr("virtio-scsi tests are only available on x86 or ppc64\n");
> -    exit(EXIT_FAILURE);
> +    global_qtest = qs->qts;
> +    return qs;
>  }
> 
>  static void qvirtio_scsi_stop(QOSState *qs)


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 195 bytes --]

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [Qemu-devel] [PATCH v7 35/38] libqtest: Merge qtest_{mem, buf}{read, write}() with {mem, buf}{read, write}()
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 35/38] libqtest: Merge qtest_{mem, buf}{read, write}() with {mem, buf}{read, write}() Eric Blake
@ 2017-09-11 21:35   ` Greg Kurz
  0 siblings, 0 replies; 75+ messages in thread
From: Greg Kurz @ 2017-09-11 21:35 UTC (permalink / raw)
  To: Eric Blake
  Cc: qemu-devel, armbru, pbonzini, thuth, John Snow, David Gibson,
	Alexander Graf, Aneesh Kumar K.V, Stefan Hajnoczi, Jason Wang,
	open list:IDE, open list:sPAPR

[-- Attachment #1: Type: text/plain, Size: 2376 bytes --]

On Mon, 11 Sep 2017 12:20:19 -0500
Eric Blake <eblake@redhat.com> wrote:

> Maintaining two layers of libqtest APIs, one that takes an explicit
> QTestState object, and the other that uses the implicit global_qtest,
> is annoying.  In the interest of getting rid of global implicit
> state and having less code to maintain, merge:
>  qtest_memread()
>  qtest_bufread()
>  qtest_memwrite()
>  qtest_bufwrite()
> with their short counterparts.  All callers that previously
> used the short form now make it explicit that they are relying on
> global_qtest, and later patches can then clean things up to remove
> the global variable.
> 
> Signed-off-by: Eric Blake <eblake@redhat.com>
> ---
>  tests/libqtest.h         | 69 ++++++------------------------------------------
>  tests/libqtest.c         |  8 +++---
>  tests/ahci-test.c        | 16 +++++------
>  tests/e1000e-test.c      | 16 ++++++-----
>  tests/i440fx-test.c      |  8 +++---
>  tests/ide-test.c         | 14 +++++-----
>  tests/libqos/ahci.c      | 22 +++++++--------
>  tests/libqos/pci-pc.c    |  4 +--
>  tests/libqos/pci-spapr.c |  4 +--
>  tests/megasas-test.c     |  2 +-
>  tests/postcopy-test.c    | 12 ++++-----
>  tests/virtio-9p-test.c   |  4 +--
>  tests/virtio-blk-test.c  | 16 +++++------
>  tests/virtio-net-test.c  |  6 ++---
>  tests/virtio-scsi-test.c |  4 +--
>  15 files changed, 78 insertions(+), 127 deletions(-)
> 
[...]
> diff --git a/tests/virtio-9p-test.c b/tests/virtio-9p-test.c
> index 07cbb7f37c..e2a14bc2ff 100644
> --- a/tests/virtio-9p-test.c
> +++ b/tests/virtio-9p-test.c
> @@ -116,7 +116,7 @@ typedef struct {
> 
>  static void v9fs_memwrite(P9Req *req, const void *addr, size_t len)
>  {
> -    memwrite(req->t_msg + req->t_off, addr, len);
> +    memwrite(global_qtest, req->t_msg + req->t_off, addr, len);

Maybe use req->v9p->qs->qts instead of global_qtest ?

>      req->t_off += len;
>  }
> 
> @@ -132,7 +132,7 @@ static void v9fs_memrewind(P9Req *req, size_t len)
> 
>  static void v9fs_memread(P9Req *req, void *addr, size_t len)
>  {
> -    memread(req->r_msg + req->r_off, addr, len);
> +    memread(global_qtest, req->r_msg + req->r_off, addr, len);

Same here.

But this can be done in a follow-up patch. In case you don't respin, for
tests/virtio-9p-test.c:

Acked-by: Greg Kurz <groug@kaod.org>

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 195 bytes --]

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [Qemu-devel] [PATCH v7 08/38] libqos: Track QTestState with QPCIBus
  2017-09-11 17:19 ` [Qemu-devel] [PATCH v7 08/38] libqos: Track QTestState with QPCIBus Eric Blake
@ 2017-09-11 23:46   ` John Snow
  2017-09-12  7:05   ` Thomas Huth
  1 sibling, 0 replies; 75+ messages in thread
From: John Snow @ 2017-09-11 23:46 UTC (permalink / raw)
  To: Eric Blake, qemu-devel
  Cc: thuth, open list:IDE, armbru, Alexander Graf, open list:sPAPR,
	Gerd Hoffmann, pbonzini, David Gibson



On 09/11/2017 01:19 PM, Eric Blake wrote:
> When initializing a QPCIBus, track which QTestState the bus is
> associated with (so that a later patch can then explicitly use
> that test state for all communication on the bus, rather than
> blindly relying on global_qtest).  Update the initialization
> functions to take another parameter, and update all callers to
> pass in state (for now, most callers get away with passing the
> current global_qtest as the current state, although this required
> fixing the order of initialization to ensure qtest_start() is
> called before qpci_init*() in rtl8139-test, and provided an
> opportunity to pass in the allocator in e1000e-test).
> 
> Touch up some allocations to use g_new0() rather than g_malloc()
> while in the area, and simplify some code (all implementations
> of QOSOps provide a .init_allocator() that never fails).
> 
> Signed-off-by: Eric Blake <eblake@redhat.com>

Reviewed-by: John Snow <jsnow@redhat.com>

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [Qemu-devel] [PATCH v7 13/38] libqos: Use explicit QTestState for fw_cfg operations
  2017-09-11 17:19 ` [Qemu-devel] [PATCH v7 13/38] libqos: Use explicit QTestState for fw_cfg operations Eric Blake
@ 2017-09-11 23:49   ` John Snow
  2017-09-12  8:55   ` Thomas Huth
  1 sibling, 0 replies; 75+ messages in thread
From: John Snow @ 2017-09-11 23:49 UTC (permalink / raw)
  To: Eric Blake, qemu-devel
  Cc: thuth, open list:IDE, armbru, Alexander Graf, open list:sPAPR,
	pbonzini, David Gibson



On 09/11/2017 01:19 PM, Eric Blake wrote:
> Drop one more client of global_qtest by teaching all fw_cfg test
> functionality (invoked through alloc-pc) to pass in an explicit
> QTestState, adjusting all callers.  In particular, fw_cfg-test
> had to reorder things to create the test state prior to creating
> the fw_cfg (and drop a pointless strdup in the meantime), but that
> test now no longer depends on global_qtest.
> 
> Signed-off-by: Eric Blake <eblake@redhat.com>
> 
Reviewed-by: John Snow <jsnow@redhat.com>

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [Qemu-devel] [PATCH v7 16/38] libqos: Use explicit QTestState for ahci operations
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 16/38] libqos: Use explicit QTestState for ahci operations Eric Blake
@ 2017-09-11 23:54   ` John Snow
  2017-09-12  9:09   ` Thomas Huth
  1 sibling, 0 replies; 75+ messages in thread
From: John Snow @ 2017-09-11 23:54 UTC (permalink / raw)
  To: Eric Blake, qemu-devel; +Cc: pbonzini, thuth, armbru, open list:IDE



On 09/11/2017 01:20 PM, Eric Blake wrote:
> Drop one more client of global_qtest by teaching all ahci test
> functionality to pass in an explicit QTestState.  The state was
> already available, so no callers had to be adjusted.
> 
> Signed-off-by: Eric Blake <eblake@redhat.com>

I should admit that this makes my urge to write a wrapper/macro increase
just a little bit, but ... admitting you have a problem is the first
step to recovery.

Reviewed-by: John Snow <jsnow@redhat.com>

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [Qemu-devel] [PATCH v7 17/38] libqos: Use explicit QTestState for remaining libqos operations
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 17/38] libqos: Use explicit QTestState for remaining libqos operations Eric Blake
  2017-09-11 21:30   ` Greg Kurz
@ 2017-09-12  0:01   ` John Snow
  1 sibling, 0 replies; 75+ messages in thread
From: John Snow @ 2017-09-12  0:01 UTC (permalink / raw)
  To: Eric Blake, qemu-devel
  Cc: thuth, open list:IDE, Aneesh Kumar K.V, Jason Wang, armbru,
	Alexander Graf, Greg Kurz, open list:sPAPR, Gerd Hoffmann,
	Stefan Hajnoczi, pbonzini, David Gibson



On 09/11/2017 01:20 PM, Eric Blake wrote:
> Drop one more client of global_qtest by teaching all remaining
> libqos stragglers to pass in an explicit QTestState.  Change the
> setting of global_qtest from being implicit in libqos' call to
> qtest_start() to instead be explicit in all clients that are
> still relying on global_qtest.
> 
> Note that qmp_execute() can be greatly simplified in the process,
> and that we also get rid of interpolation of a JSON string into a
> temporary variable when qtest_qmp() can do it more reliably.
> 
> Signed-off-by: Eric Blake <eblake@redhat.com>
> 

Reviewed-by: John Snow <jsnow@redhat.com>

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [Qemu-devel] [PATCH v7 18/38] ahci-test: Drop dependence on global_qtest
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 18/38] ahci-test: Drop dependence on global_qtest Eric Blake
@ 2017-09-12  0:20   ` John Snow
  2017-09-12  0:21     ` John Snow
  0 siblings, 1 reply; 75+ messages in thread
From: John Snow @ 2017-09-12  0:20 UTC (permalink / raw)
  To: Eric Blake, qemu-devel; +Cc: pbonzini, thuth, armbru, open list:IDE



On 09/11/2017 01:20 PM, Eric Blake wrote:
> Managing parallel connections to two different monitors via
> the implicit global_qtest makes it hard to copy-and-paste code
> to tests that are not aware of the implicit state; the
> management of global_qtest is even harder to follow because
> it was masked behind set_context().
> 
> Instead, explicitly pass QTestState* around (generally, by
> reusing the member already present in ahci->parent QOSState),
> and call explicit qtest_* functions on all places that
> interact with a monitor.
> 
> We can assert that the conversion is correct by checking that
> global_qtest remains NULL throughout the test (a later patch
> that changes global_qtest to not be a public global variable
> will drop the assertions).
> 
> Bonus: there was one spots that was creating a needless temporary
> variable to execute the 'cont' command, rather than just directly
> passing the literal command through qtest_qmp().  Fixing that
> gets us one step closer to enabling -Wformat checking on
> constructed JSON.
> 
> Signed-off-by: Eric Blake <eblake@redhat.com>

still LGTM, you can probably still see why I was a little iffy about
making the global qstate ubiquitous instead of doing it this way.

(Though it does make the calls a little more verbose, they are IMO a lot
easier to reason about in tests that deal with mixed-state and
migrations and so on, which -- most of my qtest experience was from AHCI
-- there is a lot of here. (Sorry Markus, I'm a

Reviewed-by: John Snow <jsnow@redhat.com>

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [Qemu-devel] [PATCH v7 18/38] ahci-test: Drop dependence on global_qtest
  2017-09-12  0:20   ` John Snow
@ 2017-09-12  0:21     ` John Snow
  0 siblings, 0 replies; 75+ messages in thread
From: John Snow @ 2017-09-12  0:21 UTC (permalink / raw)
  To: Eric Blake, qemu-devel; +Cc: pbonzini, thuth, armbru, open list:IDE



On 09/11/2017 08:20 PM, John Snow wrote:
> 
> 
> On 09/11/2017 01:20 PM, Eric Blake wrote:
>> Managing parallel connections to two different monitors via
>> the implicit global_qtest makes it hard to copy-and-paste code
>> to tests that are not aware of the implicit state; the
>> management of global_qtest is even harder to follow because
>> it was masked behind set_context().
>>
>> Instead, explicitly pass QTestState* around (generally, by
>> reusing the member already present in ahci->parent QOSState),
>> and call explicit qtest_* functions on all places that
>> interact with a monitor.
>>
>> We can assert that the conversion is correct by checking that
>> global_qtest remains NULL throughout the test (a later patch
>> that changes global_qtest to not be a public global variable
>> will drop the assertions).
>>
>> Bonus: there was one spots that was creating a needless temporary
>> variable to execute the 'cont' command, rather than just directly
>> passing the literal command through qtest_qmp().  Fixing that
>> gets us one step closer to enabling -Wformat checking on
>> constructed JSON.
>>
>> Signed-off-by: Eric Blake <eblake@redhat.com>
> 
> still LGTM, you can probably still see why I was a little iffy about
> making the global qstate ubiquitous instead of doing it this way.
> 
> (Though it does make the calls a little more verbose, they are IMO a lot
> easier to reason about in tests that deal with mixed-state and
> migrations and so on, which -- most of my qtest experience was from AHCI
> -- there is a lot of here. (Sorry Markus, I'm a
lol! I hit send too soon, I tabbed away to look for Markus' previous
email which used some German term to refer to people who preferred
explicit state.

I wound up not finding it and then getting distracted.

Enjoy.

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [Qemu-devel] [PATCH v7 26/38] libqtest: Merge qtest_end() into qtest_quit()
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 26/38] libqtest: Merge qtest_end() into qtest_quit() Eric Blake
@ 2017-09-12  0:31   ` John Snow
  2017-09-12  9:30   ` Thomas Huth
  1 sibling, 0 replies; 75+ messages in thread
From: John Snow @ 2017-09-12  0:31 UTC (permalink / raw)
  To: Eric Blake, qemu-devel
  Cc: thuth, open list:Floppy, Michael S. Tsirkin, Jason Wang,
	Amit Shah, armbru, Alexander Graf, Keith Busch, open list:sPAPR,
	Gerd Hoffmann, Stefan Hajnoczi, pbonzini, Andreas Färber,
	Dr. David Alan Gilbert, David Gibson



On 09/11/2017 01:20 PM, Eric Blake wrote:
> Rather than have two similar shutdown functions, where one requires
> the use of global_qtest in the header, it is better to have a single
> shutdown function that still takes care of cleaning up global_qtest
> if it is set.  All callers are updated.
> 
> Signed-off-by: Eric Blake <eblake@redhat.com>
> ---
>  tests/libqtest.h               | 11 -----------
>  tests/libqtest.c               |  6 +++++-
[...]
>  tests/fdc-test.c               |  2 +-
[...]
>  tests/ide-test.c               |  2 +-

Reviewed-by: John Snow <jsnow@redhat.com>

The rest: "ACK." Change looks sane and I assume the rest of the patch is
fairly straightforward mechanical changes. Everything still compiles and
runs successfully.

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [Qemu-devel] [PATCH v7 07/38] libqtest: Inline qtest_query_target_endianness()
  2017-09-11 17:19 ` [Qemu-devel] [PATCH v7 07/38] libqtest: Inline qtest_query_target_endianness() Eric Blake
@ 2017-09-12  6:32   ` Thomas Huth
  0 siblings, 0 replies; 75+ messages in thread
From: Thomas Huth @ 2017-09-12  6:32 UTC (permalink / raw)
  To: Eric Blake, qemu-devel; +Cc: armbru, pbonzini

On 11.09.2017 19:19, Eric Blake wrote:
> There was only one caller; it's easier to inline things.  (It
> trades one forward declaration for two others, but it's not
> worth the additional churn of topological sorting, even though
> that would be possible.)
> 
> Signed-off-by: Eric Blake <eblake@redhat.com>
> 
> ---
> v7: Drop the topological sorting
> ---
>  tests/libqtest.c | 26 +++++++++-----------------
>  1 file changed, 9 insertions(+), 17 deletions(-)
> 
> diff --git a/tests/libqtest.c b/tests/libqtest.c
> index 95d4159b38..1710d63276 100644
> --- a/tests/libqtest.c
> +++ b/tests/libqtest.c
> @@ -49,7 +49,9 @@ static struct sigaction sigact_old;
>      g_assert_cmpint(ret, !=, -1); \
>  } while (0)
> 
> -static int qtest_query_target_endianness(QTestState *s);
> +static void qtest_sendf(QTestState *s, const char *fmt, ...)
> +    GCC_FMT_ATTR(2, 3);
> +static gchar **qtest_rsp(QTestState *s, int expected_args);
> 
>  static int init_socket(const char *socket_path)
>  {
> @@ -158,6 +160,7 @@ QTestState *qtest_init_without_qmp_handshake(const char *extra_args)
>      gchar *qmp_socket_path;
>      gchar *command;
>      const char *qemu_binary;
> +    gchar **args;
> 
>      qemu_binary = getenv("QTEST_QEMU_BINARY");
>      if (!qemu_binary) {
> @@ -221,8 +224,11 @@ QTestState *qtest_init_without_qmp_handshake(const char *extra_args)
>      }
> 
>      /* ask endianness of the target */
> -
> -    s->big_endian = qtest_query_target_endianness(s);
> +    qtest_sendf(s, "endianness\n");
> +    args = qtest_rsp(s, 1);
> +    g_assert(strcmp(args[1], "big") == 0 || strcmp(args[1], "little") == 0);
> +    s->big_endian = strcmp(args[1], "big") == 0;
> +    g_strfreev(args);
> 
>      return s;
>  }
> @@ -367,20 +373,6 @@ redo:
>      return words;
>  }
> 
> -static int qtest_query_target_endianness(QTestState *s)
> -{
> -    gchar **args;
> -    int big_endian;
> -
> -    qtest_sendf(s, "endianness\n");
> -    args = qtest_rsp(s, 1);
> -    g_assert(strcmp(args[1], "big") == 0 || strcmp(args[1], "little") == 0);
> -    big_endian = strcmp(args[1], "big") == 0;
> -    g_strfreev(args);
> -
> -    return big_endian;
> -}
> -
>  typedef struct {
>      JSONMessageParser parser;
>      QDict *response;

Looking at this patch again, I'm not sure anymore whether this is really
worth the effort. Keeping code that belongs together in a separate
function also makes sense ... Not sure, but I'd maybe rather drop this
patch. But in case you want to keep it:

Reviewed-by: Thomas Huth <thuth@redhat.com>

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [Qemu-devel] [PATCH v7 08/38] libqos: Track QTestState with QPCIBus
  2017-09-11 17:19 ` [Qemu-devel] [PATCH v7 08/38] libqos: Track QTestState with QPCIBus Eric Blake
  2017-09-11 23:46   ` John Snow
@ 2017-09-12  7:05   ` Thomas Huth
  1 sibling, 0 replies; 75+ messages in thread
From: Thomas Huth @ 2017-09-12  7:05 UTC (permalink / raw)
  To: Eric Blake, qemu-devel
  Cc: armbru, pbonzini, John Snow, David Gibson, Alexander Graf,
	Gerd Hoffmann, open list:IDE, open list:sPAPR

On 11.09.2017 19:19, Eric Blake wrote:
> When initializing a QPCIBus, track which QTestState the bus is
> associated with (so that a later patch can then explicitly use
> that test state for all communication on the bus, rather than
> blindly relying on global_qtest).  Update the initialization
> functions to take another parameter, and update all callers to
> pass in state (for now, most callers get away with passing the
> current global_qtest as the current state, although this required
> fixing the order of initialization to ensure qtest_start() is
> called before qpci_init*() in rtl8139-test, and provided an
> opportunity to pass in the allocator in e1000e-test).
> 
> Touch up some allocations to use g_new0() rather than g_malloc()
> while in the area, and simplify some code (all implementations
> of QOSOps provide a .init_allocator() that never fails).
> 
> Signed-off-by: Eric Blake <eblake@redhat.com>

Reviewed-by: Thomas Huth <thuth@redhat.com>

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [Qemu-devel] [PATCH v7 09/38] libqos: Track QTestState with QVirtioBus
  2017-09-11 17:19 ` [Qemu-devel] [PATCH v7 09/38] libqos: Track QTestState with QVirtioBus Eric Blake
@ 2017-09-12  7:21   ` Thomas Huth
  2017-09-12 13:28     ` Eric Blake
  0 siblings, 1 reply; 75+ messages in thread
From: Thomas Huth @ 2017-09-12  7:21 UTC (permalink / raw)
  To: Eric Blake, qemu-devel
  Cc: armbru, pbonzini, Stefan Hajnoczi, open list:virtio-blk

On 11.09.2017 19:19, Eric Blake wrote:
> When initializing a QVirtioDevice (which always has an associated
> QVirtioBus), we want to track which QTestState to use for all
> I/O processed through that bus and device.  Copy the paradigm
> used for QPCIBus, and track the test state at the bus level; this
> in turn requires a separate bus object per device (and associated
> cleanup) rather than just sharing a const version of the dispatch
> table.
I fail to see why we need a separate bus object here for each device.
The bus is only available one time, not multiple times, isn't it? So
there should also only be one bus object floating around, not multiple
ones... or do I miss something?

 Thomas

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [Qemu-devel] [PATCH v7 10/38] libqos: Move/rename qpci_unplug_acpi_device_test() to pci.c
  2017-09-11 17:19 ` [Qemu-devel] [PATCH v7 10/38] libqos: Move/rename qpci_unplug_acpi_device_test() to pci.c Eric Blake
@ 2017-09-12  7:29   ` Thomas Huth
  2017-09-12 13:28     ` Eric Blake
  0 siblings, 1 reply; 75+ messages in thread
From: Thomas Huth @ 2017-09-12  7:29 UTC (permalink / raw)
  To: Eric Blake, qemu-devel
  Cc: armbru, pbonzini, Stefan Hajnoczi, Jason Wang, Amit Shah,
	open list:virtio-blk, Laurent Vivier

On 11.09.2017 19:19, Eric Blake wrote:
> Commit 2f8b2767 originally added qpci_plug_device_test() and
> qpci_unplug_acpi_device_test() as a pair, both in pci-pc.c.
> Later, commit cf716b31 moved one half of the pair to pci.c
> when adding PPC64 support.  Keep the implementations of the
> two functions together, and shorten the name to
> qpci_unplug_device_test(), since all callers use the two
> functions in tandem.
> 
> Signed-off-by: Eric Blake <eblake@redhat.com>
> ---
>  tests/libqos/pci.h      |  2 +-
>  tests/e1000e-test.c     |  2 +-
>  tests/ivshmem-test.c    |  2 +-
>  tests/libqos/pci-pc.c   | 23 -----------------------
>  tests/libqos/pci.c      | 23 +++++++++++++++++++++++
>  tests/virtio-blk-test.c |  2 +-
>  tests/virtio-net-test.c |  2 +-
>  tests/virtio-rng-test.c |  2 +-
>  8 files changed, 29 insertions(+), 29 deletions(-)
> 
> diff --git a/tests/libqos/pci.h b/tests/libqos/pci.h
> index 429c382282..fdda7eca6e 100644
> --- a/tests/libqos/pci.h
> +++ b/tests/libqos/pci.h
> @@ -111,5 +111,5 @@ QPCIBar qpci_legacy_iomap(QPCIDevice *dev, uint16_t addr);
> 
>  void qpci_plug_device_test(const char *driver, const char *id,
>                             uint8_t slot, const char *opts);
> -void qpci_unplug_acpi_device_test(const char *id, uint8_t slot);
> +void qpci_unplug_device_test(const char *id, uint8_t slot);
>  #endif
> diff --git a/tests/e1000e-test.c b/tests/e1000e-test.c
> index d8085d944e..4c663a3019 100644
> --- a/tests/e1000e-test.c
> +++ b/tests/e1000e-test.c
> @@ -461,7 +461,7 @@ static void test_e1000e_hotplug(gconstpointer data)
>      qtest_start("-device e1000e");
> 
>      qpci_plug_device_test("e1000e", "e1000e_net", slot, NULL);
> -    qpci_unplug_acpi_device_test("e1000e_net", slot);
> +    qpci_unplug_device_test("e1000e_net", slot);
> 
>      qtest_end();
>  }
> diff --git a/tests/ivshmem-test.c b/tests/ivshmem-test.c
> index 37763425ee..8c9ed6a568 100644
> --- a/tests/ivshmem-test.c
> +++ b/tests/ivshmem-test.c
> @@ -427,7 +427,7 @@ static void test_ivshmem_hotplug(void)
> 
>      qpci_plug_device_test("ivshmem", "iv1", PCI_SLOT_HP, opts);
>      if (strcmp(arch, "ppc64") != 0) {
> -        qpci_unplug_acpi_device_test("iv1", PCI_SLOT_HP);
> +        qpci_unplug_device_test("iv1", PCI_SLOT_HP);
>      }
> 
>      qtest_end();
> diff --git a/tests/libqos/pci-pc.c b/tests/libqos/pci-pc.c
> index e267fd1a44..6305d142a5 100644
> --- a/tests/libqos/pci-pc.c
> +++ b/tests/libqos/pci-pc.c
> @@ -19,9 +19,6 @@
>  #include "qemu-common.h"
> 
> 
> -#define ACPI_PCIHP_ADDR         0xae00
> -#define PCI_EJ_BASE             0x0008
> -
>  typedef struct QPCIBusPC
>  {
>      QPCIBus bus;
> @@ -156,23 +153,3 @@ void qpci_free_pc(QPCIBus *bus)
> 
>      g_free(s);
>  }
> -
> -void qpci_unplug_acpi_device_test(const char *id, uint8_t slot)
> -{
> -    QDict *response;
> -    char *cmd;
> -
> -    cmd = g_strdup_printf("{'execute': 'device_del',"
> -                          " 'arguments': {"
> -                          "   'id': '%s'"
> -                          "}}", id);
> -    response = qmp(cmd);
> -    g_free(cmd);
> -    g_assert(response);
> -    g_assert(!qdict_haskey(response, "error"));
> -    QDECREF(response);
> -
> -    outb(ACPI_PCIHP_ADDR + PCI_EJ_BASE, 1 << slot);
> -
> -    qmp_eventwait("DEVICE_DELETED");
> -}
> diff --git a/tests/libqos/pci.c b/tests/libqos/pci.c
> index 2dcdeade2a..9f36ec73ef 100644
> --- a/tests/libqos/pci.c
> +++ b/tests/libqos/pci.c
> @@ -16,6 +16,9 @@
>  #include "hw/pci/pci_regs.h"
>  #include "qemu/host-utils.h"
> 
> +#define ACPI_PCIHP_ADDR         0xae00
> +#define PCI_EJ_BASE             0x0008
> +
>  void qpci_device_foreach(QPCIBus *bus, int vendor_id, int device_id,
>                           void (*func)(QPCIDevice *dev, int devfn, void *data),
>                           void *data)
> @@ -412,3 +415,23 @@ void qpci_plug_device_test(const char *driver, const char *id,
>      g_assert(!qdict_haskey(response, "error"));
>      QDECREF(response);
>  }
> +
> +void qpci_unplug_device_test(const char *id, uint8_t slot)
> +{
> +    QDict *response;
> +    char *cmd;
> +
> +    cmd = g_strdup_printf("{'execute': 'device_del',"
> +                          " 'arguments': {"
> +                          "   'id': '%s'"
> +                          "}}", id);
> +    response = qmp(cmd);
> +    g_free(cmd);
> +    g_assert(response);
> +    g_assert(!qdict_haskey(response, "error"));
> +    QDECREF(response);
> +
> +    outb(ACPI_PCIHP_ADDR + PCI_EJ_BASE, 1 << slot);
> +
> +    qmp_eventwait("DEVICE_DELETED");
> +}

No, that's a bad idea. ACPI and that outb() is clearly something
specific to x86, so this should not reside in pci.c but in pci-pc.c

We might be able to unify this - I've had a similar patch here:

 https://patchwork.kernel.org/patch/9905031/

... but I think this needs some more careful thinking and discussion, so
I'd suggest that you remove this from your already huge patch series for
now and we fix it later instead.

 Thomas

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [Qemu-devel] [PATCH v7 12/38] libqos: Use explicit QTestState for virtio operations
  2017-09-11 17:19 ` [Qemu-devel] [PATCH v7 12/38] libqos: Use explicit QTestState for virtio operations Eric Blake
@ 2017-09-12  7:38   ` Thomas Huth
  0 siblings, 0 replies; 75+ messages in thread
From: Thomas Huth @ 2017-09-12  7:38 UTC (permalink / raw)
  To: Eric Blake, qemu-devel
  Cc: armbru, pbonzini, Stefan Hajnoczi, open list:virtio-blk

On 11.09.2017 19:19, Eric Blake wrote:
> Now that QVirtioDevice and QVirtQueue point back to QVirtioBus,
> we can reuse the explicit QTestState stored there rather than
> relying on implicit global_qtest.  We also have to pass QTestState
> through a few functions that can't trace back through
> QVirtioDevice, and update those callers.
> 
> Drop some useless casts while touching things.
> 
> Signed-off-by: Eric Blake <eblake@redhat.com>
> ---
>  tests/libqos/virtio.h      |  6 ++--
>  tests/libqos/virtio-mmio.c | 57 ++++++++++++++++++-------------
>  tests/libqos/virtio-pci.c  |  8 ++---
>  tests/libqos/virtio.c      | 84 ++++++++++++++++++++++++++--------------------
>  tests/virtio-blk-test.c    | 11 +++---
>  5 files changed, 94 insertions(+), 72 deletions(-)

Reviewed-by: Thomas Huth <thuth@redhat.com>

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [Qemu-devel] [PATCH v7 13/38] libqos: Use explicit QTestState for fw_cfg operations
  2017-09-11 17:19 ` [Qemu-devel] [PATCH v7 13/38] libqos: Use explicit QTestState for fw_cfg operations Eric Blake
  2017-09-11 23:49   ` John Snow
@ 2017-09-12  8:55   ` Thomas Huth
  1 sibling, 0 replies; 75+ messages in thread
From: Thomas Huth @ 2017-09-12  8:55 UTC (permalink / raw)
  To: Eric Blake, qemu-devel
  Cc: armbru, pbonzini, John Snow, David Gibson, Alexander Graf,
	open list:IDE, open list:sPAPR

On 11.09.2017 19:19, Eric Blake wrote:
> Drop one more client of global_qtest by teaching all fw_cfg test
> functionality (invoked through alloc-pc) to pass in an explicit
> QTestState, adjusting all callers.  In particular, fw_cfg-test
> had to reorder things to create the test state prior to creating
> the fw_cfg (and drop a pointless strdup in the meantime), but that
> test now no longer depends on global_qtest.
> 
> Signed-off-by: Eric Blake <eblake@redhat.com>

Reviewed-by: Thomas Huth <thuth@redhat.com>

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [Qemu-devel] [PATCH v7 14/38] libqos: Use explicit QTestState for rtas operations
  2017-09-11 17:19 ` [Qemu-devel] [PATCH v7 14/38] libqos: Use explicit QTestState for rtas operations Eric Blake
@ 2017-09-12  9:01   ` Thomas Huth
  0 siblings, 0 replies; 75+ messages in thread
From: Thomas Huth @ 2017-09-12  9:01 UTC (permalink / raw)
  To: Eric Blake, qemu-devel
  Cc: armbru, pbonzini, David Gibson, Alexander Graf, open list:sPAPR

On 11.09.2017 19:19, Eric Blake wrote:
> Drop one more client of global_qtest by teaching all rtas test
> functionality to pass in an explicit QTestState, adjusting all
> callers.
> 
> Signed-off-by: Eric Blake <eblake@redhat.com>
> 
> ---
> v7: catch a couple more operations, drop R-b
> ---
>  tests/libqos/rtas.h      |  9 ++++++---
>  tests/libqos/pci-spapr.c | 18 ++++++++++++------
>  tests/libqos/rtas.c      | 36 ++++++++++++++++++++----------------
>  tests/rtas-test.c        |  2 +-
>  4 files changed, 39 insertions(+), 26 deletions(-)
> 
> diff --git a/tests/libqos/rtas.h b/tests/libqos/rtas.h
> index 498eb19230..e3d0f850a7 100644
> --- a/tests/libqos/rtas.h
> +++ b/tests/libqos/rtas.h
> @@ -7,9 +7,12 @@
>  #define LIBQOS_RTAS_H
>  #include "libqos/malloc.h"
> 
> -int qrtas_get_time_of_day(QGuestAllocator *alloc, struct tm *tm, uint32_t *ns);
> -uint32_t qrtas_ibm_read_pci_config(QGuestAllocator *alloc, uint64_t buid,
> +int qrtas_get_time_of_day(QTestState *qts, QGuestAllocator *alloc,
> +                          struct tm *tm, uint32_t *ns);
> +uint32_t qrtas_ibm_read_pci_config(QTestState *qts, QGuestAllocator *alloc,
> +                                   uint64_t buid,
>                                     uint32_t addr, uint32_t size);
> -int qrtas_ibm_write_pci_config(QGuestAllocator *alloc, uint64_t buid,
> +int qrtas_ibm_write_pci_config(QTestState *qts, QGuestAllocator *alloc,
> +                               uint64_t buid,
>                                 uint32_t addr, uint32_t size, uint32_t val);

Nit: I'd prefer if you rather put hte "uint32_t val" on a separate line
instead of the "buid".

[...]
> @@ -92,7 +95,8 @@ uint32_t qrtas_ibm_read_pci_config(QGuestAllocator *alloc, uint64_t buid,
>      return ret[1];
>  }
> 
> -int qrtas_ibm_write_pci_config(QGuestAllocator *alloc, uint64_t buid,
> +int qrtas_ibm_write_pci_config(QTestState *qts, QGuestAllocator *alloc,
> +                               uint64_t buid,
>                                 uint32_t addr, uint32_t size, uint32_t val)

dito

>  {
>      int res;
> @@ -103,7 +107,7 @@ int qrtas_ibm_write_pci_config(QGuestAllocator *alloc, uint64_t buid,
>      args[2] = buid & 0xffffffff;
>      args[3] = size;
>      args[4] = val;
> -    res = qrtas_call(alloc, "ibm,write-pci-config", 5, args, 1, ret);
> +    res = qrtas_call(qts, alloc, "ibm,write-pci-config", 5, args, 1, ret);
>      if (res != 0) {
>          return -1;
>      }
> diff --git a/tests/rtas-test.c b/tests/rtas-test.c
> index 276c87ef84..34a692163f 100644
> --- a/tests/rtas-test.c
> +++ b/tests/rtas-test.c
> @@ -16,7 +16,7 @@ static void test_rtas_get_time_of_day(void)
>      qs = qtest_spapr_boot("-machine pseries");
> 
>      t1 = time(NULL);
> -    ret = qrtas_get_time_of_day(qs->alloc, &tm, &ns);
> +    ret = qrtas_get_time_of_day(qs->qts, qs->alloc, &tm, &ns);
>      g_assert_cmpint(ret, ==, 0);
>      t2 = mktimegm(&tm);
>      g_assert(t2 - t1 < 5); /* 5 sec max to run the test */
> 

Reviewed-by: Thomas Huth <thuth@redhat.com>

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [Qemu-devel] [PATCH v7 15/38] libqos: Use explicit QTestState for i2c operations
  2017-09-11 17:19 ` [Qemu-devel] [PATCH v7 15/38] libqos: Use explicit QTestState for i2c operations Eric Blake
@ 2017-09-12  9:04   ` Thomas Huth
  0 siblings, 0 replies; 75+ messages in thread
From: Thomas Huth @ 2017-09-12  9:04 UTC (permalink / raw)
  To: Eric Blake, qemu-devel; +Cc: armbru, pbonzini

On 11.09.2017 19:19, Eric Blake wrote:
> Drop one more client of global_qtest by teaching all i2c test
> functionality to pass in an explicit QTestState, adjusting all
> callers.
> 
> Signed-off-by: Eric Blake <eblake@redhat.com>
> ---
>  tests/libqos/i2c.h      |  7 ++++--
>  tests/ds1338-test.c     |  6 ++---
>  tests/libqos/i2c-imx.c  | 67 +++++++++++++++++++++++++------------------------
>  tests/libqos/i2c-omap.c | 45 +++++++++++++++++----------------
>  tests/tmp105-test.c     |  6 ++---
>  5 files changed, 66 insertions(+), 65 deletions(-)

Reviewed-by: Thomas Huth <thuth@redhat.com>

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [Qemu-devel] [PATCH v7 16/38] libqos: Use explicit QTestState for ahci operations
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 16/38] libqos: Use explicit QTestState for ahci operations Eric Blake
  2017-09-11 23:54   ` John Snow
@ 2017-09-12  9:09   ` Thomas Huth
  1 sibling, 0 replies; 75+ messages in thread
From: Thomas Huth @ 2017-09-12  9:09 UTC (permalink / raw)
  To: Eric Blake, qemu-devel; +Cc: armbru, pbonzini, John Snow, open list:IDE

On 11.09.2017 19:20, Eric Blake wrote:
> Drop one more client of global_qtest by teaching all ahci test
> functionality to pass in an explicit QTestState.  The state was
> already available, so no callers had to be adjusted.
> 
> Signed-off-by: Eric Blake <eblake@redhat.com>

Reviewed-by: Thomas Huth <thuth@redhat.com>

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [Qemu-devel] [PATCH v7 24/38] tests/acpi-utils: Drop dependence on global_qtest
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 24/38] tests/acpi-utils: " Eric Blake
@ 2017-09-12  9:26   ` Thomas Huth
  0 siblings, 0 replies; 75+ messages in thread
From: Thomas Huth @ 2017-09-12  9:26 UTC (permalink / raw)
  To: Eric Blake, qemu-devel
  Cc: armbru, pbonzini, Michael S. Tsirkin, Igor Mammedov, Ben Warren

On 11.09.2017 19:20, Eric Blake wrote:
> As a general rule, we prefer avoiding implicit global state
> because it makes code harder to safely copy and paste without
> thinking about the global state.  Adjust the helper code to
> use explicit state instead, and update all callers.
> 
> bios-tables-test no longer depends on global_qtest, now that it
> passes explicit state through the testsuite data; an assert
> proves this fact (although we will get rid of it later, once
> global_qtest is gone).
> 
> Signed-off-by: Eric Blake <eblake@redhat.com>
[...]
> diff --git a/tests/vmgenid-test.c b/tests/vmgenid-test.c
> index 0ea98b86d7..970c6274fd 100644
> --- a/tests/vmgenid-test.c
> +++ b/tests/vmgenid-test.c
> @@ -50,15 +50,15 @@ static uint32_t acpi_find_vgia(void)
>      boot_sector_test(global_qtest);
> 
>      /* Tables should be initialized now. */
> -    rsdp_offset = acpi_find_rsdp_address();
> +    rsdp_offset = acpi_find_rsdp_address(global_qtest);
> 
>      g_assert_cmphex(rsdp_offset, <, RSDP_ADDR_INVALID);
> 
> -    acpi_parse_rsdp_table(rsdp_offset, &rsdp_table);
> +    acpi_parse_rsdp_table(global_qtest, rsdp_offset, &rsdp_table);
> 
>      rsdt = rsdp_table.rsdt_physical_address;
>      /* read the header */
> -    ACPI_READ_TABLE_HEADER(&rsdt_table, rsdt);
> +    ACPI_READ_TABLE_HEADER(global_qtest, &rsdt_table, rsdt);
>      ACPI_ASSERT_CMP(rsdt_table.signature, "RSDT");
> 
>      /* compute the table entries in rsdt */
> @@ -68,21 +68,21 @@ static uint32_t acpi_find_vgia(void)
> 
>      /* get the addresses of the tables pointed by rsdt */
>      tables = g_new0(uint32_t, tables_nr);
> -    ACPI_READ_ARRAY_PTR(tables, tables_nr, rsdt);
> +    ACPI_READ_ARRAY_PTR(global_qtest, tables, tables_nr, rsdt);
> 
>      for (i = 0; i < tables_nr; i++) {
> -        ACPI_READ_TABLE_HEADER(&ssdt_table, tables[i]);
> +        ACPI_READ_TABLE_HEADER(global_qtest, &ssdt_table, tables[i]);
>          if (!strncmp((char *)ssdt_table.oem_table_id, "VMGENID", 7)) {
>              /* the first entry in the table should be VGIA
>               * That's all we need
>               */
> -            ACPI_READ_FIELD(vgid_table.name_op, tables[i]);
> +            ACPI_READ_FIELD(global_qtest, vgid_table.name_op, tables[i]);
>              g_assert(vgid_table.name_op == 0x08);  /* name */
> -            ACPI_READ_ARRAY(vgid_table.vgia, tables[i]);
> +            ACPI_READ_ARRAY(global_qtest, vgid_table.vgia, tables[i]);
>              g_assert(memcmp(vgid_table.vgia, "VGIA", 4) == 0);
> -            ACPI_READ_FIELD(vgid_table.val_op, tables[i]);
> +            ACPI_READ_FIELD(global_qtest, vgid_table.val_op, tables[i]);
>              g_assert(vgid_table.val_op == 0x0C);  /* dword */
> -            ACPI_READ_FIELD(vgid_table.vgia_val, tables[i]);
> +            ACPI_READ_FIELD(global_qtest, vgid_table.vgia_val, tables[i]);
>              /* The GUID is written at a fixed offset into the fw_cfg file
>               * in order to implement the "OVMF SDT Header probe suppressor"
>               * see docs/specs/vmgenid.txt for more details
> 

I'd maybe use a local "QTestState *qts = global_qtest;" at the beginning
of this function instead, and then use "qts" instead of "global_qtest in
this function ... then we have to only touch one line later instead of
changing all lines with "global_qtest" again.

Any way:
Reviewed-by: Thomas Huth <thuth@redhat.com>

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [Qemu-devel] [PATCH v7 26/38] libqtest: Merge qtest_end() into qtest_quit()
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 26/38] libqtest: Merge qtest_end() into qtest_quit() Eric Blake
  2017-09-12  0:31   ` John Snow
@ 2017-09-12  9:30   ` Thomas Huth
  1 sibling, 0 replies; 75+ messages in thread
From: Thomas Huth @ 2017-09-12  9:30 UTC (permalink / raw)
  To: Eric Blake, qemu-devel
  Cc: armbru, pbonzini, Gerd Hoffmann, John Snow, Keith Busch,
	Andreas Färber, David Gibson, Alexander Graf,
	Dr. David Alan Gilbert, Michael S. Tsirkin, Stefan Hajnoczi,
	Amit Shah, Jason Wang, open list:Floppy, open list:sPAPR

On 11.09.2017 19:20, Eric Blake wrote:
> Rather than have two similar shutdown functions, where one requires
> the use of global_qtest in the header, it is better to have a single
> shutdown function that still takes care of cleaning up global_qtest
> if it is set.  All callers are updated.
> 
> Signed-off-by: Eric Blake <eblake@redhat.com>

Reviewed-by: Thomas Huth <thuth@redhat.com>

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [Qemu-devel] [PATCH v7 27/38] libqtest: Swap order of qtest_init() and qtest_start()
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 27/38] libqtest: Swap order of qtest_init() and qtest_start() Eric Blake
@ 2017-09-12  9:57   ` Thomas Huth
  0 siblings, 0 replies; 75+ messages in thread
From: Thomas Huth @ 2017-09-12  9:57 UTC (permalink / raw)
  To: Eric Blake, qemu-devel; +Cc: armbru, pbonzini

On 11.09.2017 19:20, Eric Blake wrote:
> We already have another qtest_init() in the tree (namely, as part
> of the device initialization of the qtest device at the top level
> qtest.c), with a different signature; having two different
> qtest_init() is confusing, so an upcoming patch will consolidate
> all testsuite callers onto a unified spelling.  But the
> consolidation is easier if qtest_start() is further down the call
> chain, rather than an intermediate wrapper, so swap the call chain
> around.  This includes renaming qtest_init_without_qmp_handshake()
> into qtest_start_without_qmp_handshake(), as it remains the lowest
> point in the call stack.
> 
> Note that qtest_init() now asserts that global_qtest was not set
> on entry, and clears it on exit, to preserve the behavior of
> existing tests that assert the same (and also proving that we
> fixed all tests that had parallel connections); but later patches
> will eventually simplify things by getting rid of qtest_init()
> and global_qtest altogether.
> 
> Signed-off-by: Eric Blake <eblake@redhat.com>

Reviewed-by: Thomas Huth <thuth@redhat.com>

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [Qemu-devel] [PATCH v7 28/38] libqtest: Add qtest_[v]startf()
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 28/38] libqtest: Add qtest_[v]startf() Eric Blake
@ 2017-09-12 10:14   ` Thomas Huth
  2017-09-12 13:32     ` Eric Blake
  0 siblings, 1 reply; 75+ messages in thread
From: Thomas Huth @ 2017-09-12 10:14 UTC (permalink / raw)
  To: Eric Blake, qemu-devel
  Cc: armbru, pbonzini, Michael S. Tsirkin, Igor Mammedov, John Snow,
	Andreas Färber, Dr. David Alan Gilbert, Stefan Hajnoczi,
	Ben Warren, open list:IDE

On 11.09.2017 19:20, Eric Blake wrote:
> We have several callers that were formatting the argument strings
> themselves; consolidate this effort by adding new convenience
> functions directly in libqtest, and update all call-sites that
> can benefit from it.
[...]
> diff --git a/tests/libqtest.c b/tests/libqtest.c
> index e8c2e11817..b535d7768f 100644
> --- a/tests/libqtest.c
> +++ b/tests/libqtest.c
> @@ -245,6 +245,27 @@ QTestState *qtest_start(const char *extra_args)
>      return global_qtest = s;
>  }
> 
> +QTestState *qtest_vstartf(const char *fmt, va_list ap)
> +{
> +    char *args = g_strdup_vprintf(fmt, ap);
> +    QTestState *s;
> +
> +    s = qtest_start(args);
> +    global_qtest = NULL;

Don't you need a g_free(args) here?

> +    return s;
> +}
> +
> +QTestState *qtest_startf(const char *fmt, ...)
> +{
> +    va_list ap;
> +    QTestState *s;
> +
> +    va_start(ap, fmt);
> +    s = qtest_vstartf(fmt, ap);
> +    va_end(ap);
> +    return s;
> +}
[...]
> diff --git a/tests/e1000-test.c b/tests/e1000-test.c
> index 0c5fcdcc44..12bc526ad6 100644
> --- a/tests/e1000-test.c
> +++ b/tests/e1000-test.c
> @@ -14,16 +14,8 @@
>  static void test_device(gconstpointer data)
>  {
>      const char *model = data;
> -    QTestState *s;
> -    char *args;
> 
> -    args = g_strdup_printf("-device %s", model);
> -    s = qtest_start(args);
> -
> -    if (s) {
> -        qtest_quit(s);
> -    }
> -    g_free(args);
> +    qtest_quit(qtest_startf("-device %s", model));

Just my personal taste, but I think I'd be nicer to keep this on
separate lines:

    QTestState *s;

    s = qtest_startf("-device %s", model);
    qtest_quit(s);

>  }
[...]
> diff --git a/tests/eepro100-test.c b/tests/eepro100-test.c
> index bdc8a67d57..fc9ea84d66 100644
> --- a/tests/eepro100-test.c
> +++ b/tests/eepro100-test.c
> @@ -13,18 +13,9 @@
>  static void test_device(gconstpointer data)
>  {
>      const char *model = data;
> -    QTestState *s;
> -    char *args;
> -
> -    args = g_strdup_printf("-device %s", model);
> -    s = qtest_start(args);
> 
>      /* Tests only initialization so far. TODO: Implement functional tests */
> -
> -    if (s) {
> -        qtest_quit(s);
> -    }
> -    g_free(args);
> +    qtest_quit(qtest_startf("-device %s", model));
>  }

dito

[...]
> diff --git a/tests/numa-test.c b/tests/numa-test.c
> index fa21d26935..e2f6c68be8 100644
> --- a/tests/numa-test.c
> +++ b/tests/numa-test.c
> @@ -12,20 +12,14 @@
>  #include "qemu/osdep.h"
>  #include "libqtest.h"
> 
> -static char *make_cli(const char *generic_cli, const char *test_cli)
> -{
> -    return g_strdup_printf("%s %s", generic_cli ? generic_cli : "", test_cli);
> -}
> -
>  static void test_mon_explicit(const void *data)
>  {
>      char *s;
> -    char *cli;
> +    const char *args = data;
> 
> -    cli = make_cli(data, "-smp 8 "
> -                   "-numa node,nodeid=0,cpus=0-3 "
> -                   "-numa node,nodeid=1,cpus=4-7 ");
> -    qtest_start(cli);
> +    global_qtest = qtest_startf("%s -smp 8 "
> +                                "-numa node,nodeid=0,cpus=0-3 "
> +                                "-numa node,nodeid=1,cpus=4-7 ", args);
> 
>      s = hmp("info numa");
>      g_assert(strstr(s, "node 0 cpus: 0 1 2 3"));
> @@ -33,16 +27,14 @@ static void test_mon_explicit(const void *data)
>      g_free(s);
> 
>      qtest_quit(global_qtest);
> -    g_free(cli);
>  }
> 
>  static void test_mon_default(const void *data)
>  {
>      char *s;
> -    char *cli;
> +    const char *args = data;
> 
> -    cli = make_cli(data, "-smp 8 -numa node -numa node");
> -    qtest_start(cli);
> +    global_qtest = qtest_startf("%s -smp 8 -numa node -numa node", args);
> 
>      s = hmp("info numa");
>      g_assert(strstr(s, "node 0 cpus: 0 2 4 6"));
> @@ -50,18 +42,16 @@ static void test_mon_default(const void *data)
>      g_free(s);
> 
>      qtest_quit(global_qtest);
> -    g_free(cli);
>  }
> 
>  static void test_mon_partial(const void *data)
>  {
>      char *s;
> -    char *cli;
> +    const char *args = data;
> 
> -    cli = make_cli(data, "-smp 8 "
> -                   "-numa node,nodeid=0,cpus=0-1 "
> -                   "-numa node,nodeid=1,cpus=4-5 ");
> -    qtest_start(cli);
> +    global_qtest = qtest_startf("%s -smp 8 "
> +                                "-numa node,nodeid=0,cpus=0-1 "
> +                                "-numa node,nodeid=1,cpus=4-5 ", args);

Does GCC emit a warning if you'd used data here directly? Otherwise I
think you could simply do this without the local args variable...

 Thomas

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [Qemu-devel] [PATCH v7 29/38] libqtest: Merge qtest_init() into qtest_start()
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 29/38] libqtest: Merge qtest_init() into qtest_start() Eric Blake
@ 2017-09-12 10:37   ` Thomas Huth
  0 siblings, 0 replies; 75+ messages in thread
From: Thomas Huth @ 2017-09-12 10:37 UTC (permalink / raw)
  To: Eric Blake, qemu-devel
  Cc: armbru, pbonzini, Gerd Hoffmann, John Snow, Keith Busch,
	David Gibson, Alexander Graf, Michael S. Tsirkin, Amit Shah,
	Jason Wang, open list:Floppy, open list:sPAPR

On 11.09.2017 19:20, Eric Blake wrote:
> Remove the trivial wrapper qtest_init(), and change qtest_start()
> to no longer implicitly set global_qtest, to make it obvious in the
> rest of the testsuite where we are still relying on global_qtest.
> Everything now uses qtest_start() (and friends) and qtest_quit(),
> and explicitly tracks the QTestState between the two (although in
> many cases, this tracking is still done through global_qtest).
> Doing this makes it easier to see what remaining cleanups will be
> needed if we don't want an implicit dependency on global state.
[...]
> diff --git a/tests/libqtest.h b/tests/libqtest.h
> index 817e3a5580..2a21bf4605 100644
> --- a/tests/libqtest.h
> +++ b/tests/libqtest.h
> @@ -27,7 +27,7 @@ extern QTestState *global_qtest;
>   * qtest_start_without_qmp_handshake:
>   * @extra_args: other arguments to pass to QEMU.
>   *
> - * Returns: #QTestState instance.  Does not affect #global_qtest.
> + * Returns: #QTestState instance, handshaking not yet completed.

I'd rather add a description a la:

  * Start QEMU, but do not execute the QMP handshake yet.
  *
  * Returns: #QTestState instance

>   */
>  QTestState *qtest_start_without_qmp_handshake(const char *extra_args);
> 
> @@ -35,10 +35,7 @@ QTestState *qtest_start_without_qmp_handshake(const char *extra_args);
>   * qtest_start:
>   * @args: other arguments to pass to QEMU
>   *
> - * Start QEMU and assign the resulting #QTestState to #global_qtest.
> - * The global variable is used by "shortcut" functions documented below.
>   *
> - * Returns: #QTestState instance.
> + * Returns: #QTestState instance, handshaking completed.

I'd rather change the description instead of removing it:

  * Start QEMU and execute the initial QMP handshake
  *
  * Returns: #QTestState instance.

>   */
>  QTestState *qtest_start(const char *args);
> 
> @@ -47,10 +44,7 @@ QTestState *qtest_start(const char *args);
>   * @fmt...: Format for creating other arguments to pass to QEMU, formatted
>   * like sprintf().
>   *
> - * Start QEMU and return the resulting #QTestState (but unlike qtest_start(),
> - * #global_qtest is left at NULL).
> - *
> - * Returns: #QTestState instance.
> + * Returns: #QTestState instance, handshaking completed.

dito

>   */
>  QTestState *qtest_startf(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
> 
> @@ -60,30 +54,11 @@ QTestState *qtest_startf(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
>   * like vsprintf().
>   * @ap: Format arguments.
>   *
> - * Start QEMU and return the resulting #QTestState (but unlike qtest_start(),
> - * #global_qtest is left at NULL).
> - *
> - * Returns: #QTestState instance.
> + * Returns: #QTestState instance, handshaking completed.

dito

>   */
>  QTestState *qtest_vstartf(const char *fmt, va_list ap) GCC_FMT_ATTR(1, 0);
> 
>  /**
> - * qtest_init:
> - * @extra_args: other arguments to pass to QEMU.
> - *
> - * Returns: #QTestState instance.  Does not affect #global_qtest.
> - */
> -static inline QTestState *qtest_init(const char *extra_args)
> -{
> -    QTestState *s;
> -
> -    assert(!global_qtest);
> -    s = qtest_start(extra_args);
> -    global_qtest = NULL;
> -    return s;
> -}
[...]
> diff --git a/tests/display-vga-test.c b/tests/display-vga-test.c
> index 8667330e3c..d638f15ec3 100644
> --- a/tests/display-vga-test.c
> +++ b/tests/display-vga-test.c
> @@ -12,39 +12,33 @@
> 
>  static void pci_cirrus(void)
>  {
> -    qtest_start("-vga none -device cirrus-vga");
> -    qtest_quit(global_qtest);
> +    qtest_quit(qtest_start("-vga none -device cirrus-vga"));

I'd prefer to keep this on separate lines ... but that's again just my
personal taste. (also for the othe changes below)

>  }
> 
>  static void pci_stdvga(void)
>  {
> -    qtest_start("-vga none -device VGA");
> -    qtest_quit(global_qtest);
> +    qtest_quit(qtest_start("-vga none -device VGA"));
>  }
> 
>  static void pci_secondary(void)
>  {
> -    qtest_start("-vga none -device secondary-vga");
> -    qtest_quit(global_qtest);
> +    qtest_quit(qtest_start("-vga none -device secondary-vga"));
>  }
> 
>  static void pci_multihead(void)
>  {
> -    qtest_start("-vga none -device VGA -device secondary-vga");
> -    qtest_quit(global_qtest);
> +    qtest_quit(qtest_start("-vga none -device VGA -device secondary-vga"));
>  }
> 
>  static void pci_virtio_gpu(void)
>  {
> -    qtest_start("-vga none -device virtio-gpu-pci");
> -    qtest_quit(global_qtest);
> +    qtest_quit(qtest_start("-vga none -device virtio-gpu-pci"));
>  }
> 
>  #ifdef CONFIG_VIRTIO_VGA
>  static void pci_virtio_vga(void)
>  {
> -    qtest_start("-vga none -device virtio-vga");
> -    qtest_quit(global_qtest);
> +    qtest_quit(qtest_start("-vga none -device virtio-vga"));
>  }
>  #endif
[...]
> diff --git a/tests/ne2000-test.c b/tests/ne2000-test.c
> index cae83c5c4c..8e6f7b07c6 100644
> --- a/tests/ne2000-test.c
> +++ b/tests/ne2000-test.c
> @@ -22,7 +22,7 @@ int main(int argc, char **argv)
>      g_test_init(&argc, &argv, NULL);
>      qtest_add_func("/ne2000/pci/nop", pci_nop);
> 
> -    qtest_start("-device ne2k_pci");
> +    global_qtest = qtest_start("-device ne2k_pci");
>      ret = g_test_run();
> 
>      qtest_quit(global_qtest);

For such very trivial tests, it maybe makes sense to use a local
"QTestState *qts" variable here already, so we don't have to touch this
code again later?

> diff --git a/tests/nvme-test.c b/tests/nvme-test.c
> index 3d6c0f39cf..b054ad6fcd 100644
> --- a/tests/nvme-test.c
> +++ b/tests/nvme-test.c
> @@ -22,8 +22,9 @@ int main(int argc, char **argv)
>      g_test_init(&argc, &argv, NULL);
>      qtest_add_func("/nvme/nop", nop);
> 
> -    qtest_start("-drive id=drv0,if=none,file=null-co://,format=raw "
> -                "-device nvme,drive=drv0,serial=foo");
> +    global_qtest = qtest_start(
> +        "-drive id=drv0,if=none,file=null-co://,format=raw "
> +        "-device nvme,drive=drv0,serial=foo");
>      ret = g_test_run();
> 
>      qtest_quit(global_qtest);

dito

> diff --git a/tests/pcnet-test.c b/tests/pcnet-test.c
> index 98246d3504..a58a5fd7bf 100644
> --- a/tests/pcnet-test.c
> +++ b/tests/pcnet-test.c
> @@ -22,7 +22,7 @@ int main(int argc, char **argv)
>      g_test_init(&argc, &argv, NULL);
>      qtest_add_func("/pcnet/pci/nop", pci_nop);
> 
> -    qtest_start("-device pcnet");
> +    global_qtest = qtest_start("-device pcnet");
>      ret = g_test_run();
> 
>      qtest_quit(global_qtest);

dito

[...]
> diff --git a/tests/virtio-balloon-test.c b/tests/virtio-balloon-test.c
> index 34ad718601..cca7b0e8fb 100644
> --- a/tests/virtio-balloon-test.c
> +++ b/tests/virtio-balloon-test.c
> @@ -22,7 +22,7 @@ int main(int argc, char **argv)
>      g_test_init(&argc, &argv, NULL);
>      qtest_add_func("/virtio/balloon/pci/nop", pci_nop);
> 
> -    qtest_start("-device virtio-balloon-pci");
> +    global_qtest = qtest_start("-device virtio-balloon-pci");
>      ret = g_test_run();
> 
>      qtest_quit(global_qtest);

dito

[...]
> diff --git a/tests/vmxnet3-test.c b/tests/vmxnet3-test.c
> index 631630b4d0..0b4b807616 100644
> --- a/tests/vmxnet3-test.c
> +++ b/tests/vmxnet3-test.c
> @@ -22,7 +22,7 @@ int main(int argc, char **argv)
>      g_test_init(&argc, &argv, NULL);
>      qtest_add_func("/vmxnet3/nop", nop);
> 
> -    qtest_start("-device vmxnet3");
> +    global_qtest = qtest_start("-device vmxnet3");
>      ret = g_test_run();
> 
>      qtest_quit(global_qtest);

dito

 Thomas

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [Qemu-devel] [PATCH v7 30/38] qtest: Avoid passing raw strings through hmp()
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 30/38] qtest: Avoid passing raw strings through hmp() Eric Blake
  2017-09-11 17:42   ` Dr. David Alan Gilbert
@ 2017-09-12 10:40   ` Thomas Huth
  1 sibling, 0 replies; 75+ messages in thread
From: Thomas Huth @ 2017-09-12 10:40 UTC (permalink / raw)
  To: Eric Blake, qemu-devel; +Cc: armbru, pbonzini, Dr. David Alan Gilbert

On 11.09.2017 19:20, Eric Blake wrote:
> hmp() passes its string argument through the sprintf() family;
> with a proper attribute, gcc -Wformat warns us when we do something
> dangerous like passing a non-constant format string.  Fortunately,
> all our strings were safe, but checking whether the string can
> contain an unintended % is easy to avoid and therefore worth doing.
> 
> Signed-off-by: Eric Blake <eblake@redhat.com>
> 
> ---
> v7: add GCC_FMT_ATTR here, drop R-b
> ---
>  tests/libqtest.h | 8 ++++----
>  tests/test-hmp.c | 4 ++--
>  2 files changed, 6 insertions(+), 6 deletions(-)

Reviewed-by: Thomas Huth <thuth@redhat.com>

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [Qemu-devel] [PATCH v7 31/38] libqtest: Merge qtest_clock_*() with clock_*()
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 31/38] libqtest: Merge qtest_clock_*() with clock_*() Eric Blake
@ 2017-09-12 10:45   ` Thomas Huth
  2017-09-12 13:35     ` Eric Blake
  0 siblings, 1 reply; 75+ messages in thread
From: Thomas Huth @ 2017-09-12 10:45 UTC (permalink / raw)
  To: Eric Blake, qemu-devel; +Cc: armbru, pbonzini, John Snow, open list:Floppy

On 11.09.2017 19:20, Eric Blake wrote:
> Maintaining two layers of libqtest APIs, one that takes an explicit
> QTestState object, and the other that uses the implicit global_qtest,
> is annoying.  In the interest of getting rid of global implicit
> state and having less code to maintain, merge:
>  qtest_clock_set()
>  qtest_clock_step()
>  qtest_clock_step_next()
> with their short counterparts.  All callers that previously
> used the short form now make it explicit that they are relying on
> global_qtest, and later patches can then clean things up to remove
> the global variable.
> 
> Signed-off-by: Eric Blake <eblake@redhat.com>
> ---
>  tests/libqtest.h         | 50 ++++----------------------------
>  tests/libqtest.c         |  6 ++--
>  tests/e1000e-test.c      |  2 +-
>  tests/fdc-test.c         |  4 +--
>  tests/ide-test.c         |  2 +-
>  tests/libqos/virtio.c    |  8 +++---
>  tests/rtc-test.c         | 74 ++++++++++++++++++++++++------------------------
>  tests/rtl8139-test.c     | 10 +++----
>  tests/tco-test.c         | 22 +++++++-------
>  tests/test-arm-mptimer.c | 25 +++++++++-------
>  tests/wdt_ib700-test.c   | 12 ++++----
>  11 files changed, 90 insertions(+), 125 deletions(-)
> 
> diff --git a/tests/libqtest.h b/tests/libqtest.h
> index 5651b77d2f..26d5f37bc9 100644
> --- a/tests/libqtest.h
> +++ b/tests/libqtest.h
> @@ -417,17 +417,17 @@ void qtest_bufwrite(QTestState *s, uint64_t addr,
>  void qtest_memset(QTestState *s, uint64_t addr, uint8_t patt, size_t size);
> 
>  /**
> - * qtest_clock_step_next:
> + * clock_step_next:
>   * @s: #QTestState instance to operate on.
>   *
>   * Advance the QEMU_CLOCK_VIRTUAL to the next deadline.
>   *
>   * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
>   */
> -int64_t qtest_clock_step_next(QTestState *s);
> +int64_t clock_step_next(QTestState *s);
> 
>  /**
> - * qtest_clock_step:
> + * clock_step:
>   * @s: QTestState instance to operate on.
>   * @step: Number of nanoseconds to advance the clock by.
>   *
> @@ -435,10 +435,10 @@ int64_t qtest_clock_step_next(QTestState *s);
>   *
>   * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
>   */
> -int64_t qtest_clock_step(QTestState *s, int64_t step);
> +int64_t clock_step(QTestState *s, int64_t step);
> 
>  /**
> - * qtest_clock_set:
> + * clock_set:
>   * @s: QTestState instance to operate on.
>   * @val: Nanoseconds value to advance the clock to.
>   *
> @@ -446,7 +446,7 @@ int64_t qtest_clock_step(QTestState *s, int64_t step);
>   *
>   * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
>   */
> -int64_t qtest_clock_set(QTestState *s, int64_t val);
> +int64_t clock_set(QTestState *s, int64_t val);
 Could we please keep the "qtest" prefix here and rather get rid of the
other ones? Even if it's more to type, I prefer to have a proper prefix
here so that it is clear at the first sight that the functions belong to
the qtest framework.

 Thomas

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [Qemu-devel] [PATCH v7 32/38] libqtest: Merge qtest_irq*() with irq*()
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 32/38] libqtest: Merge qtest_irq*() with irq*() Eric Blake
@ 2017-09-12 10:47   ` Thomas Huth
  0 siblings, 0 replies; 75+ messages in thread
From: Thomas Huth @ 2017-09-12 10:47 UTC (permalink / raw)
  To: Eric Blake, qemu-devel; +Cc: armbru, pbonzini, John Snow, open list:Floppy

On 11.09.2017 19:20, Eric Blake wrote:
> Maintaining two layers of libqtest APIs, one that takes an explicit
> QTestState object, and the other that uses the implicit global_qtest,
> is annoying.  In the interest of getting rid of global implicit
> state and having less code to maintain, merge:
>  qtest_get_irq()
>  qtest_irq_intercept_in()
>  qtest_irq_intercept_out()
> with their short counterparts.  All callers that previously
> used the short form now make it explicit that they are relying on
> global_qtest, and later patches can then clean things up to remove
> the global variable.
> 
> Signed-off-by: Eric Blake <eblake@redhat.com>
> ---
>  tests/libqtest.h         | 47 ++++++-----------------------------------------
>  tests/libqtest.c         |  6 +++---
>  tests/fdc-test.c         | 48 ++++++++++++++++++++++++------------------------
>  tests/ide-test.c         | 17 +++++++++--------
>  tests/ipmi-bt-test.c     |  6 +++---
>  tests/ipmi-kcs-test.c    |  8 ++++----
>  tests/libqos/libqos-pc.c |  2 +-
>  tests/rtc-test.c         | 10 +++++-----
>  tests/tco-test.c         |  2 +-
>  tests/wdt_ib700-test.c   |  8 ++++----
>  10 files changed, 60 insertions(+), 94 deletions(-)
> 
> diff --git a/tests/libqtest.h b/tests/libqtest.h
> index 26d5f37bc9..8398c0fd07 100644
> --- a/tests/libqtest.h
> +++ b/tests/libqtest.h
> @@ -176,33 +176,33 @@ char *qtest_hmp(QTestState *s, const char *fmt, ...) GCC_FMT_ATTR(2, 3);
>  char *qtest_hmpv(QTestState *s, const char *fmt, va_list ap);
> 
>  /**
> - * qtest_get_irq:
> + * get_irq:
>   * @s: #QTestState instance to operate on.
>   * @num: Interrupt to observe.
>   *
>   * Returns: The level of the @num interrupt.
>   */
> -bool qtest_get_irq(QTestState *s, int num);
> +bool get_irq(QTestState *s, int num);
> 
>  /**
> - * qtest_irq_intercept_in:
> + * irq_intercept_in:
>   * @s: #QTestState instance to operate on.
>   * @string: QOM path of a device.
>   *
>   * Associate qtest irqs with the GPIO-in pins of the device
>   * whose path is specified by @string.
>   */
> -void qtest_irq_intercept_in(QTestState *s, const char *string);
> +void irq_intercept_in(QTestState *s, const char *string);
> 
>  /**
> - * qtest_irq_intercept_out:
> + * irq_intercept_out:
>   * @s: #QTestState instance to operate on.
>   * @string: QOM path of a device.
>   *
>   * Associate qtest irqs with the GPIO-out pins of the device
>   * whose path is specified by @string.
>   */
> -void qtest_irq_intercept_out(QTestState *s, const char *string);
> +void irq_intercept_out(QTestState *s, const char *string);

Could we please also keep the qtest prefix here?

 Thomas

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [Qemu-devel] [PATCH v7 33/38] libqtest: Merge qtest_{in, out}[bwl]() with {in, out}[bwl]()
  2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 33/38] libqtest: Merge qtest_{in, out}[bwl]() with {in, out}[bwl]() Eric Blake
@ 2017-09-12 10:49   ` Thomas Huth
  0 siblings, 0 replies; 75+ messages in thread
From: Thomas Huth @ 2017-09-12 10:49 UTC (permalink / raw)
  To: Eric Blake, qemu-devel; +Cc: armbru, pbonzini, John Snow, open list:Floppy

On 11.09.2017 19:20, Eric Blake wrote:
> Maintaining two layers of libqtest APIs, one that takes an explicit
> QTestState object, and the other that uses the implicit global_qtest,
> is annoying.  In the interest of getting rid of global implicit
> state and having less code to maintain, merge:
>  qtest_outb()
>  qtest_outw()
>  qtest_outl()
>  qtest_inb()
>  qtest_inw()
>  qtest_inl()
> with their short counterparts.  All callers that previously
> used the short form now make it explicit that they are relying on
> global_qtest, and later patches can then clean things up to remove
> the global variable.
> 
> Signed-off-by: Eric Blake <eblake@redhat.com>
> ---
>  tests/libqtest.h        | 99 ++++++-------------------------------------------
>  tests/multiboot/libc.h  |  2 +-
>  tests/libqtest.c        | 14 +++----
>  tests/boot-order-test.c |  4 +-
>  tests/endianness-test.c | 12 +++---
>  tests/fdc-test.c        | 77 ++++++++++++++++++++------------------
>  tests/hd-geo-test.c     |  4 +-
>  tests/ipmi-bt-test.c    | 12 +++---
>  tests/ipmi-kcs-test.c   |  8 ++--
>  tests/libqos/fw_cfg.c   |  4 +-
>  tests/libqos/pci-pc.c   | 44 +++++++++++-----------
>  tests/libqos/pci.c      |  2 +-
>  tests/m48t59-test.c     |  8 ++--
>  tests/multiboot/libc.c  |  2 +-
>  tests/pvpanic-test.c    |  4 +-
>  tests/rtc-test.c        |  8 ++--
>  tests/wdt_ib700-test.c  |  8 ++--
>  17 files changed, 120 insertions(+), 192 deletions(-)
> 
> diff --git a/tests/libqtest.h b/tests/libqtest.h
> index 8398c0fd07..520f745e7b 100644
> --- a/tests/libqtest.h
> +++ b/tests/libqtest.h
> @@ -205,61 +205,61 @@ void irq_intercept_in(QTestState *s, const char *string);
>  void irq_intercept_out(QTestState *s, const char *string);
> 
>  /**
> - * qtest_outb:
> + * outb:
>   * @s: #QTestState instance to operate on.
>   * @addr: I/O port to write to.
>   * @value: Value being written.
>   *
>   * Write an 8-bit value to an I/O port.
>   */
> -void qtest_outb(QTestState *s, uint16_t addr, uint8_t value);
> +void outb(QTestState *s, uint16_t addr, uint8_t value);

Could we please also keep the qtest prefix here? ... same applies for
all your other following "Merge ..." patches in this series...

 Thomas

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [Qemu-devel] [PATCH v7 09/38] libqos: Track QTestState with QVirtioBus
  2017-09-12  7:21   ` Thomas Huth
@ 2017-09-12 13:28     ` Eric Blake
  2017-09-13  7:10       ` Thomas Huth
  0 siblings, 1 reply; 75+ messages in thread
From: Eric Blake @ 2017-09-12 13:28 UTC (permalink / raw)
  To: Thomas Huth, qemu-devel
  Cc: armbru, pbonzini, Stefan Hajnoczi, open list:virtio-blk

[-- Attachment #1: Type: text/plain, Size: 1894 bytes --]

On 09/12/2017 02:21 AM, Thomas Huth wrote:
> On 11.09.2017 19:19, Eric Blake wrote:
>> When initializing a QVirtioDevice (which always has an associated
>> QVirtioBus), we want to track which QTestState to use for all
>> I/O processed through that bus and device.  Copy the paradigm
>> used for QPCIBus, and track the test state at the bus level; this
>> in turn requires a separate bus object per device (and associated
>> cleanup) rather than just sharing a const version of the dispatch
>> table.
> I fail to see why we need a separate bus object here for each device.
> The bus is only available one time, not multiple times, isn't it? So
> there should also only be one bus object floating around, not multiple
> ones... or do I miss something?

You are right that there is only one bus for the machine - the problem
is that the object representing the bus is dynamically created on the
fly at the point where the device is first grabbed, rather than up front
as part of creating the machine (in other words, the device search is
not performed on a pre-existing bus object).

Contrast qpci_device_find() (lookup based on a pre-existing bus) with
qvirtio_mmio_init_device() (lookup that did not use a pre-existing bus);
also note that qvirtio_pci_device_find() takes a pre-existing QPCIBus
(since on that architecture, the QVirtioBus is itself a device on the
QPCIBus).

I suppose that we could indeed refactor the code to require callers to
create the QVirtioBus object up front, and then make the device lookup
(both qvirtio_mmio_init_device() and qvirtio_pci_device_find()) take a
QVirtioBus parameter instead of QTestState or QPCIBus.  That's slightly
more work, but I'm happy to attempt it if you think it will be better.

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 619 bytes --]

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [Qemu-devel] [PATCH v7 10/38] libqos: Move/rename qpci_unplug_acpi_device_test() to pci.c
  2017-09-12  7:29   ` Thomas Huth
@ 2017-09-12 13:28     ` Eric Blake
  2017-09-13  7:15       ` Thomas Huth
  0 siblings, 1 reply; 75+ messages in thread
From: Eric Blake @ 2017-09-12 13:28 UTC (permalink / raw)
  To: Thomas Huth, qemu-devel
  Cc: armbru, pbonzini, Stefan Hajnoczi, Jason Wang, Amit Shah,
	open list:virtio-blk, Laurent Vivier

[-- Attachment #1: Type: text/plain, Size: 1171 bytes --]

On 09/12/2017 02:29 AM, Thomas Huth wrote:
> On 11.09.2017 19:19, Eric Blake wrote:
>> Commit 2f8b2767 originally added qpci_plug_device_test() and
>> qpci_unplug_acpi_device_test() as a pair, both in pci-pc.c.
>> Later, commit cf716b31 moved one half of the pair to pci.c
>> when adding PPC64 support.  Keep the implementations of the
>> two functions together, and shorten the name to
>> qpci_unplug_device_test(), since all callers use the two
>> functions in tandem.
>>

> 
> No, that's a bad idea. ACPI and that outb() is clearly something
> specific to x86, so this should not reside in pci.c but in pci-pc.c
> 
> We might be able to unify this - I've had a similar patch here:
> 
>  https://patchwork.kernel.org/patch/9905031/
> 
> ... but I think this needs some more careful thinking and discussion, so
> I'd suggest that you remove this from your already huge patch series for
> now and we fix it later instead.

Okay, I'm fine dropping this patch, and can base my respin on top of
your cleanup instead.

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 619 bytes --]

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [Qemu-devel] [PATCH v7 28/38] libqtest: Add qtest_[v]startf()
  2017-09-12 10:14   ` Thomas Huth
@ 2017-09-12 13:32     ` Eric Blake
  2017-09-13  7:19       ` Thomas Huth
  0 siblings, 1 reply; 75+ messages in thread
From: Eric Blake @ 2017-09-12 13:32 UTC (permalink / raw)
  To: Thomas Huth, qemu-devel
  Cc: armbru, pbonzini, Michael S. Tsirkin, Igor Mammedov, John Snow,
	Andreas Färber, Dr. David Alan Gilbert, Stefan Hajnoczi,
	Ben Warren, open list:IDE

[-- Attachment #1: Type: text/plain, Size: 2459 bytes --]

On 09/12/2017 05:14 AM, Thomas Huth wrote:
> On 11.09.2017 19:20, Eric Blake wrote:
>> We have several callers that were formatting the argument strings
>> themselves; consolidate this effort by adding new convenience
>> functions directly in libqtest, and update all call-sites that
>> can benefit from it.
> [...]
>> diff --git a/tests/libqtest.c b/tests/libqtest.c
>> index e8c2e11817..b535d7768f 100644
>> --- a/tests/libqtest.c
>> +++ b/tests/libqtest.c
>> @@ -245,6 +245,27 @@ QTestState *qtest_start(const char *extra_args)
>>      return global_qtest = s;
>>  }
>>
>> +QTestState *qtest_vstartf(const char *fmt, va_list ap)
>> +{
>> +    char *args = g_strdup_vprintf(fmt, ap);
>> +    QTestState *s;
>> +
>> +    s = qtest_start(args);
>> +    global_qtest = NULL;
> 
> Don't you need a g_free(args) here?

D'oh.  Yes, I do.

>> +    qtest_quit(qtest_startf("-device %s", model));
> 
> Just my personal taste, but I think I'd be nicer to keep this on
> separate lines:
> 
>     QTestState *s;
> 
>     s = qtest_startf("-device %s", model);
>     qtest_quit(s);

Sure.  I debated about it.  If we ever do more than just create/destroy,
then having the separate lines makes it easier to stick the actual test
in between the two lines, so I'll avoid my abbreviation and go with the
longer form on respin.

>>  static void test_mon_partial(const void *data)
>>  {
>>      char *s;
>> -    char *cli;
>> +    const char *args = data;
>>
>> -    cli = make_cli(data, "-smp 8 "
>> -                   "-numa node,nodeid=0,cpus=0-1 "
>> -                   "-numa node,nodeid=1,cpus=4-5 ");
>> -    qtest_start(cli);
>> +    global_qtest = qtest_startf("%s -smp 8 "
>> +                                "-numa node,nodeid=0,cpus=0-1 "
>> +                                "-numa node,nodeid=1,cpus=4-5 ", args);
> 
> Does GCC emit a warning if you'd used data here directly? Otherwise I
> think you could simply do this without the local args variable...

Passing void* through varargs, with the intent of the receiver parsing
it as char*, is technically undefined in C.  I don't know if gcc warns,
but I'm also worried that clang might warn.  I prefer to err on the side
of defined behavior in this case, even though it annoyingly requires a
temporary variable.

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 619 bytes --]

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [Qemu-devel] [PATCH v7 31/38] libqtest: Merge qtest_clock_*() with clock_*()
  2017-09-12 10:45   ` Thomas Huth
@ 2017-09-12 13:35     ` Eric Blake
  2017-09-14  4:35       ` Thomas Huth
  0 siblings, 1 reply; 75+ messages in thread
From: Eric Blake @ 2017-09-12 13:35 UTC (permalink / raw)
  To: Thomas Huth, qemu-devel; +Cc: armbru, pbonzini, John Snow, open list:Floppy

[-- Attachment #1: Type: text/plain, Size: 1697 bytes --]

On 09/12/2017 05:45 AM, Thomas Huth wrote:
> On 11.09.2017 19:20, Eric Blake wrote:
>> Maintaining two layers of libqtest APIs, one that takes an explicit
>> QTestState object, and the other that uses the implicit global_qtest,
>> is annoying.  In the interest of getting rid of global implicit
>> state and having less code to maintain, merge:
>>  qtest_clock_set()
>>  qtest_clock_step()
>>  qtest_clock_step_next()
>> with their short counterparts.  All callers that previously
>> used the short form now make it explicit that they are relying on
>> global_qtest, and later patches can then clean things up to remove
>> the global variable.
>>

>> @@ -446,7 +446,7 @@ int64_t qtest_clock_step(QTestState *s, int64_t step);
>>   *
>>   * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
>>   */
>> -int64_t qtest_clock_set(QTestState *s, int64_t val);
>> +int64_t clock_set(QTestState *s, int64_t val);
>  Could we please keep the "qtest" prefix here and rather get rid of the
> other ones? Even if it's more to type, I prefer to have a proper prefix
> here so that it is clear at the first sight that the functions belong to
> the qtest framework.

I suppose we can, although it makes more lines that are likely to bump
up against 80 columns, and thus slightly more churn to reformat things
to keep checkpatch happy.  I like the shorter name, because less typing
is easier to remember.  I'd prefer a second opinion on naming before
doing anything about it though - Markus or Paolo, do you have any
preference?

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 619 bytes --]

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [Qemu-devel] [PATCH v7 09/38] libqos: Track QTestState with QVirtioBus
  2017-09-12 13:28     ` Eric Blake
@ 2017-09-13  7:10       ` Thomas Huth
  0 siblings, 0 replies; 75+ messages in thread
From: Thomas Huth @ 2017-09-13  7:10 UTC (permalink / raw)
  To: Eric Blake, qemu-devel
  Cc: pbonzini, open list:virtio-blk, armbru, Stefan Hajnoczi

[-- Attachment #1: Type: text/plain, Size: 1697 bytes --]

On 12.09.2017 15:28, Eric Blake wrote:
> On 09/12/2017 02:21 AM, Thomas Huth wrote:
[...]
>> I fail to see why we need a separate bus object here for each device.
>> The bus is only available one time, not multiple times, isn't it? So
>> there should also only be one bus object floating around, not multiple
>> ones... or do I miss something?
> 
> You are right that there is only one bus for the machine - the problem
> is that the object representing the bus is dynamically created on the
> fly at the point where the device is first grabbed, rather than up front
> as part of creating the machine (in other words, the device search is
> not performed on a pre-existing bus object).
> 
> Contrast qpci_device_find() (lookup based on a pre-existing bus) with
> qvirtio_mmio_init_device() (lookup that did not use a pre-existing bus);
> also note that qvirtio_pci_device_find() takes a pre-existing QPCIBus
> (since on that architecture, the QVirtioBus is itself a device on the
> QPCIBus).
> 
> I suppose that we could indeed refactor the code to require callers to
> create the QVirtioBus object up front, and then make the device lookup
> (both qvirtio_mmio_init_device() and qvirtio_pci_device_find()) take a
> QVirtioBus parameter instead of QTestState or QPCIBus.  That's slightly
> more work, but I'm happy to attempt it if you think it will be better.

At least to me, that sounds like the right way to go. I guess we might
run into other problems later if we have multiple instances of the bus
object floating around ... so sorry if this is extra work, but I'd say
let's better do it properly now instead of having to rework this again
later.

 Thomas


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 836 bytes --]

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [Qemu-devel] [PATCH v7 10/38] libqos: Move/rename qpci_unplug_acpi_device_test() to pci.c
  2017-09-12 13:28     ` Eric Blake
@ 2017-09-13  7:15       ` Thomas Huth
  0 siblings, 0 replies; 75+ messages in thread
From: Thomas Huth @ 2017-09-13  7:15 UTC (permalink / raw)
  To: Eric Blake, qemu-devel
  Cc: Laurent Vivier, open list:virtio-blk, Amit Shah, Jason Wang,
	armbru, Stefan Hajnoczi, pbonzini

[-- Attachment #1: Type: text/plain, Size: 1333 bytes --]

On 12.09.2017 15:28, Eric Blake wrote:
> On 09/12/2017 02:29 AM, Thomas Huth wrote:
>> On 11.09.2017 19:19, Eric Blake wrote:
>>> Commit 2f8b2767 originally added qpci_plug_device_test() and
>>> qpci_unplug_acpi_device_test() as a pair, both in pci-pc.c.
>>> Later, commit cf716b31 moved one half of the pair to pci.c
>>> when adding PPC64 support.  Keep the implementations of the
>>> two functions together, and shorten the name to
>>> qpci_unplug_device_test(), since all callers use the two
>>> functions in tandem.
>>>
> 
>>
>> No, that's a bad idea. ACPI and that outb() is clearly something
>> specific to x86, so this should not reside in pci.c but in pci-pc.c
>>
>> We might be able to unify this - I've had a similar patch here:
>>
>>  https://patchwork.kernel.org/patch/9905031/
>>
>> ... but I think this needs some more careful thinking and discussion, so
>> I'd suggest that you remove this from your already huge patch series for
>> now and we fix it later instead.
> 
> Okay, I'm fine dropping this patch, and can base my respin on top of
> your cleanup instead.

Note that my patches are currently on halt since I'm waiting for your
qemu_startf() reworks to get included first :-) ... so feel free to pick
up ideas or patches from my series into your series if that helps.

 Thomas


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 836 bytes --]

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [Qemu-devel] [PATCH v7 28/38] libqtest: Add qtest_[v]startf()
  2017-09-12 13:32     ` Eric Blake
@ 2017-09-13  7:19       ` Thomas Huth
  0 siblings, 0 replies; 75+ messages in thread
From: Thomas Huth @ 2017-09-13  7:19 UTC (permalink / raw)
  To: Eric Blake, qemu-devel
  Cc: open list:IDE, Ben Warren, Michael S. Tsirkin, armbru,
	Dr. David Alan Gilbert, Stefan Hajnoczi, Igor Mammedov, pbonzini,
	John Snow, Andreas Färber

[-- Attachment #1: Type: text/plain, Size: 1424 bytes --]

On 12.09.2017 15:32, Eric Blake wrote:
> On 09/12/2017 05:14 AM, Thomas Huth wrote:
>> On 11.09.2017 19:20, Eric Blake wrote:
>>> We have several callers that were formatting the argument strings
>>> themselves; consolidate this effort by adding new convenience
>>> functions directly in libqtest, and update all call-sites that
>>> can benefit from it.
[...]
>>>  static void test_mon_partial(const void *data)
>>>  {
>>>      char *s;
>>> -    char *cli;
>>> +    const char *args = data;
>>>
>>> -    cli = make_cli(data, "-smp 8 "
>>> -                   "-numa node,nodeid=0,cpus=0-1 "
>>> -                   "-numa node,nodeid=1,cpus=4-5 ");
>>> -    qtest_start(cli);
>>> +    global_qtest = qtest_startf("%s -smp 8 "
>>> +                                "-numa node,nodeid=0,cpus=0-1 "
>>> +                                "-numa node,nodeid=1,cpus=4-5 ", args);
>>
>> Does GCC emit a warning if you'd used data here directly? Otherwise I
>> think you could simply do this without the local args variable...
> 
> Passing void* through varargs, with the intent of the receiver parsing
> it as char*, is technically undefined in C.  I don't know if gcc warns,
> but I'm also worried that clang might warn.  I prefer to err on the side
> of defined behavior in this case, even though it annoyingly requires a
> temporary variable.

OK, sounds reasonable, so let's keep it!

 Thomas


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 836 bytes --]

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [Qemu-devel] [PATCH v7 31/38] libqtest: Merge qtest_clock_*() with clock_*()
  2017-09-12 13:35     ` Eric Blake
@ 2017-09-14  4:35       ` Thomas Huth
  0 siblings, 0 replies; 75+ messages in thread
From: Thomas Huth @ 2017-09-14  4:35 UTC (permalink / raw)
  To: Eric Blake, qemu-devel; +Cc: pbonzini, John Snow, armbru, open list:Floppy

[-- Attachment #1: Type: text/plain, Size: 1830 bytes --]

On 12.09.2017 15:35, Eric Blake wrote:
> On 09/12/2017 05:45 AM, Thomas Huth wrote:
>> On 11.09.2017 19:20, Eric Blake wrote:
>>> Maintaining two layers of libqtest APIs, one that takes an explicit
>>> QTestState object, and the other that uses the implicit global_qtest,
>>> is annoying.  In the interest of getting rid of global implicit
>>> state and having less code to maintain, merge:
>>>  qtest_clock_set()
>>>  qtest_clock_step()
>>>  qtest_clock_step_next()
>>> with their short counterparts.  All callers that previously
>>> used the short form now make it explicit that they are relying on
>>> global_qtest, and later patches can then clean things up to remove
>>> the global variable.
>>>
> 
>>> @@ -446,7 +446,7 @@ int64_t qtest_clock_step(QTestState *s, int64_t step);
>>>   *
>>>   * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
>>>   */
>>> -int64_t qtest_clock_set(QTestState *s, int64_t val);
>>> +int64_t clock_set(QTestState *s, int64_t val);
>>  Could we please keep the "qtest" prefix here and rather get rid of the
>> other ones? Even if it's more to type, I prefer to have a proper prefix
>> here so that it is clear at the first sight that the functions belong to
>> the qtest framework.
> 
> I suppose we can, although it makes more lines that are likely to bump
> up against 80 columns, and thus slightly more churn to reformat things
> to keep checkpatch happy.  I like the shorter name, because less typing
> is easier to remember.  I'd prefer a second opinion on naming before
> doing anything about it though - Markus or Paolo, do you have any
> preference?

IMHO you should at least keep the qtest prefix in patch 33/38 to avoid
confusion with the system definitions that have the same names (see "man
2 outb" for example).

 Thomas


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 836 bytes --]

^ permalink raw reply	[flat|nested] 75+ messages in thread

end of thread, other threads:[~2017-09-14  4:36 UTC | newest]

Thread overview: 75+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-09-11 17:19 [Qemu-devel] [PATCH v7 00/38] Preliminary libqtest cleanups Eric Blake
2017-09-11 17:19 ` [Qemu-devel] [PATCH v7 01/38] test-qga: Kill broken and dead QGA_TEST_SIDE_EFFECTING code Eric Blake
2017-09-11 17:19 ` [Qemu-devel] [PATCH v7 02/38] qtest: Don't perform side effects inside assertion Eric Blake
2017-09-11 17:19 ` [Qemu-devel] [PATCH v7 03/38] numa-test: Use hmp() Eric Blake
2017-09-11 17:19 ` [Qemu-devel] [PATCH v7 04/38] tests: Clean up wait for event Eric Blake
2017-09-11 17:19 ` [Qemu-devel] [PATCH v7 05/38] libqtest: Remove dead qtest_instances variable Eric Blake
2017-09-11 17:19 ` [Qemu-devel] [PATCH v7 06/38] libqtest: Use qemu_strtoul() Eric Blake
2017-09-11 17:19 ` [Qemu-devel] [PATCH v7 07/38] libqtest: Inline qtest_query_target_endianness() Eric Blake
2017-09-12  6:32   ` Thomas Huth
2017-09-11 17:19 ` [Qemu-devel] [PATCH v7 08/38] libqos: Track QTestState with QPCIBus Eric Blake
2017-09-11 23:46   ` John Snow
2017-09-12  7:05   ` Thomas Huth
2017-09-11 17:19 ` [Qemu-devel] [PATCH v7 09/38] libqos: Track QTestState with QVirtioBus Eric Blake
2017-09-12  7:21   ` Thomas Huth
2017-09-12 13:28     ` Eric Blake
2017-09-13  7:10       ` Thomas Huth
2017-09-11 17:19 ` [Qemu-devel] [PATCH v7 10/38] libqos: Move/rename qpci_unplug_acpi_device_test() to pci.c Eric Blake
2017-09-12  7:29   ` Thomas Huth
2017-09-12 13:28     ` Eric Blake
2017-09-13  7:15       ` Thomas Huth
2017-09-11 17:19 ` [Qemu-devel] [PATCH v7 11/38] libqos: Use explicit QTestState for pci operations Eric Blake
2017-09-11 17:19 ` [Qemu-devel] [PATCH v7 12/38] libqos: Use explicit QTestState for virtio operations Eric Blake
2017-09-12  7:38   ` Thomas Huth
2017-09-11 17:19 ` [Qemu-devel] [PATCH v7 13/38] libqos: Use explicit QTestState for fw_cfg operations Eric Blake
2017-09-11 23:49   ` John Snow
2017-09-12  8:55   ` Thomas Huth
2017-09-11 17:19 ` [Qemu-devel] [PATCH v7 14/38] libqos: Use explicit QTestState for rtas operations Eric Blake
2017-09-12  9:01   ` Thomas Huth
2017-09-11 17:19 ` [Qemu-devel] [PATCH v7 15/38] libqos: Use explicit QTestState for i2c operations Eric Blake
2017-09-12  9:04   ` Thomas Huth
2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 16/38] libqos: Use explicit QTestState for ahci operations Eric Blake
2017-09-11 23:54   ` John Snow
2017-09-12  9:09   ` Thomas Huth
2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 17/38] libqos: Use explicit QTestState for remaining libqos operations Eric Blake
2017-09-11 21:30   ` Greg Kurz
2017-09-12  0:01   ` John Snow
2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 18/38] ahci-test: Drop dependence on global_qtest Eric Blake
2017-09-12  0:20   ` John Snow
2017-09-12  0:21     ` John Snow
2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 19/38] ivshmem-test: " Eric Blake
2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 20/38] postcopy-test: " Eric Blake
2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 21/38] vhost-user-test: " Eric Blake
2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 22/38] qmp-test: " Eric Blake
2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 23/38] tests/boot-sector: " Eric Blake
2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 24/38] tests/acpi-utils: " Eric Blake
2017-09-12  9:26   ` Thomas Huth
2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 25/38] wdt_ib700-test: " Eric Blake
2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 26/38] libqtest: Merge qtest_end() into qtest_quit() Eric Blake
2017-09-12  0:31   ` John Snow
2017-09-12  9:30   ` Thomas Huth
2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 27/38] libqtest: Swap order of qtest_init() and qtest_start() Eric Blake
2017-09-12  9:57   ` Thomas Huth
2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 28/38] libqtest: Add qtest_[v]startf() Eric Blake
2017-09-12 10:14   ` Thomas Huth
2017-09-12 13:32     ` Eric Blake
2017-09-13  7:19       ` Thomas Huth
2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 29/38] libqtest: Merge qtest_init() into qtest_start() Eric Blake
2017-09-12 10:37   ` Thomas Huth
2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 30/38] qtest: Avoid passing raw strings through hmp() Eric Blake
2017-09-11 17:42   ` Dr. David Alan Gilbert
2017-09-12 10:40   ` Thomas Huth
2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 31/38] libqtest: Merge qtest_clock_*() with clock_*() Eric Blake
2017-09-12 10:45   ` Thomas Huth
2017-09-12 13:35     ` Eric Blake
2017-09-14  4:35       ` Thomas Huth
2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 32/38] libqtest: Merge qtest_irq*() with irq*() Eric Blake
2017-09-12 10:47   ` Thomas Huth
2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 33/38] libqtest: Merge qtest_{in, out}[bwl]() with {in, out}[bwl]() Eric Blake
2017-09-12 10:49   ` Thomas Huth
2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 34/38] libqtest: Merge qtest_{read, write}[bwlq]() with {read, write}[bwlq]() Eric Blake
2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 35/38] libqtest: Merge qtest_{mem, buf}{read, write}() with {mem, buf}{read, write}() Eric Blake
2017-09-11 21:35   ` Greg Kurz
2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 36/38] libqtest: Merge qtest_memset() with qmemset() Eric Blake
2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 37/38] libqtest: Separate qmp_discard_response() from command Eric Blake
2017-09-11 17:20 ` [Qemu-devel] [PATCH v7 38/38] libqtest: Merge qtest_hmp() with hmp() Eric Blake

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.