[PATCH v3 09/54] tests/qtest: fdc-test: Avoid using hardcoded /tmp

2022-09-25 Thread Bin Meng
From: Bin Meng 

This case was written to use hardcoded /tmp directory for temporary
files. Update to use g_file_open_tmp() for a portable implementation.

Signed-off-by: Bin Meng 
---

Changes in v3:
- Split to a separate patch

 tests/qtest/fdc-test.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/tests/qtest/fdc-test.c b/tests/qtest/fdc-test.c
index 52ade90a7d..1f9b99ad6d 100644
--- a/tests/qtest/fdc-test.c
+++ b/tests/qtest/fdc-test.c
@@ -68,7 +68,7 @@ enum {
 DSKCHG  = 0x80,
 };
 
-static char test_image[] = "/tmp/qtest.XX";
+static char *test_image;
 
 #define assert_bit_set(data, mask) g_assert_cmphex((data) & (mask), ==, (mask))
 #define assert_bit_clear(data, mask) g_assert_cmphex((data) & (mask), ==, 0)
@@ -608,7 +608,7 @@ int main(int argc, char **argv)
 int ret;
 
 /* Create a temporary raw image */
-fd = mkstemp(test_image);
+fd = g_file_open_tmp("qtest.XX", &test_image, NULL);
 g_assert(fd >= 0);
 ret = ftruncate(fd, TEST_IMAGE_SIZE);
 g_assert(ret == 0);
@@ -640,6 +640,7 @@ int main(int argc, char **argv)
 /* Cleanup */
 qtest_end();
 unlink(test_image);
+g_free(test_image);
 
 return ret;
 }
-- 
2.34.1




[PATCH v3 00/54] tests/qtest: Enable running qtest on Windows

2022-09-25 Thread Bin Meng
In preparation to adding virtio-9p support on Windows, this series
enables running qtest on Windows, so that we can run the virtio-9p
tests on Windows to make sure it does not break accidently.

Changes in v3:
- Remove unnecessary "error = NULL" statements
- Split patch "tests: Avoid using hardcoded /tmp in test cases" to separate 
patches
- Ensure g_autofree variable is initialized
- Use g_steal_pointer() in create_test_img()
- Add another case "boot-serial-test" to justify the change
- Add a usleep(1) in the busy wait loop
- Drop the host test
- Drop patch: "tests: Change to use g_mkdir()"
- Drop patch: "block: Unify the get_tmp_filename() implementation",
  and send it as a separate patch
- Drop patch: "chardev/char-file: Add FILE_SHARE_WRITE when openning the file 
for win32"

Changes in v2:
- new patch: "tests/qtest: i440fx-test: Rewrite create_blob_file() to be 
portable"
- Use g_autofree to declare the variable
- Change to use g_mkdir()
- Change to use g_mkdir()
- Change to use g_mkdir()
- Change to skip only part of the virtio-net-test cases that require
  socketpair() intead of disabling all of them
- Introduce a new variable qtests_filter and add that to the
  qtests_ARCH variables
- Add a comment in the code to explain why test_qmp_oob test case
  is skipped on win32
- Replace signal by the semaphore on posix too
- Use __declspec(selectany) for the common weak symbol on Windows
- Introduce qemu_send_full() and use it
- Move the enabling of building qtests on Windows to a separate
  patch to keep bisectablity
- Call socket_init() unconditionally
- Add a missing CloseHandle() call
- Change the place that sets IO redirection in the command line
- Drop ahci-test.c changes that are no longer needed
- Update commit message to include the use case why we should set
  FILE_SHARE_WRITE when opening the file for win32
- Change to a busy wait after migration is canceled
- new patch: "io/channel-watch: Drop the unnecessary cast"
- Change the timeout limit to 90 minutes
- new patch: Display meson test logs in the Windows CI
- new patch: "tests/qtest: Enable qtest build on Windows"
- Minor wording changes
- Drop patches that were already applied in the mainline
- Drop patch: "qga/commands-posix-ssh: Use g_mkdir_with_parents()"
- Drop patch: "tests: Skip iotests and qtest when '--without-default-devices'"
- Drop patch: "tests/qtest: Fix ERROR_SHARING_VIOLATION for win32"

Bin Meng (47):
  tests/qtest: i440fx-test: Rewrite create_blob_file() to be portable
  semihosting/arm-compat-semi: Avoid using hardcoded /tmp
  tcg: Avoid using hardcoded /tmp
  util/qemu-sockets: Use g_get_tmp_dir() to get the directory for
temporary files
  tests/qtest: ahci-test: Avoid using hardcoded /tmp
  tests/qtest: aspeed_smc-test: Avoid using hardcoded /tmp
  tests/qtest: boot-serial-test: Avoid using hardcoded /tmp
  tests/qtest: cxl-test: Avoid using hardcoded /tmp
  tests/qtest: fdc-test: Avoid using hardcoded /tmp
  tests/qtest: generic_fuzz: Avoid using hardcoded /tmp
  tests/qtest: virtio_blk_fuzz: Avoid using hardcoded /tmp
  tests/qtest: hd-geo-test: Avoid using hardcoded /tmp
  tests/qtest: ide-test: Avoid using hardcoded /tmp
  tests/qtest: migration-test: Avoid using hardcoded /tmp
  tests/qtest: pflash-cfi02-test: Avoid using hardcoded /tmp
  tests/qtest: qmp-test: Avoid using hardcoded /tmp
  tests/qtest: vhost-user-blk-test: Avoid using hardcoded /tmp
  tests/qtest: vhost-user-test: Avoid using hardcoded /tmp
  tests/qtest: virtio-blk-test: Avoid using hardcoded /tmp
  tests/qtest: virtio-scsi-test: Avoid using hardcoded /tmp
  tests/qtest: libqtest: Avoid using hardcoded /tmp
  tests/unit: test-image-locking: Avoid using hardcoded /tmp
  tests/unit: test-qga: Avoid using hardcoded /tmp
  tests: vhost-user-bridge: Avoid using hardcoded /tmp
  block/vvfat: Unify the mkdir() call
  fsdev/virtfs-proxy-helper: Use g_mkdir()
  hw/usb: dev-mtp: Use g_mkdir()
  tests/qtest: Skip running virtio-net-test cases that require
socketpair() for win32
  tests/qtest: Build test-filter-{mirror,redirector} cases for posix
only
  tests/qtest: qmp-test: Skip running test_qmp_oob for win32
  tests/qtest: libqtest: Exclude the *_fds APIs for win32
  tests/qtest: libqtest: Install signal handler via signal()
  tests/qtest: Support libqtest to build and run on Windows
  tests/qtest: {ahci,ide}-test: Use relative path for temporary files
for win32
  tests/qtest: bios-tables-test: Adapt the case for win32
  tests/qtest: migration-test: Disable IO redirection for win32
  tests/qtest: microbit-test: Fix socket access for win32
  tests/qtest: libqtest: Replace the call to close a socket with
closesocket()
  tests/qtest: libqtest: Correct the timeout unit of blocking receive
calls for win32
  io/channel-watch: Drop a superfluous '#ifdef WIN32'
  io/channel-watch: Drop the unnecessary cast
  io/channel-watch: Fix socket watch on Windows
  tests/qtest: migration-test: Skip running some TLS cases for win32
  .gitlab-ci.d/windo

[PATCH v3 05/54] tests/qtest: ahci-test: Avoid using hardcoded /tmp

2022-09-25 Thread Bin Meng
From: Bin Meng 

This case was written to use hardcoded /tmp directory for temporary
files. Update to use g_file_open_tmp() for a portable implementation.

Signed-off-by: Bin Meng 
---

Changes in v3:
- Split to a separate patch
- Ensure g_autofree variable is initialized

 tests/qtest/ahci-test.c | 19 +++
 1 file changed, 11 insertions(+), 8 deletions(-)

diff --git a/tests/qtest/ahci-test.c b/tests/qtest/ahci-test.c
index f1e510b0ac..1d5929d8c3 100644
--- a/tests/qtest/ahci-test.c
+++ b/tests/qtest/ahci-test.c
@@ -44,9 +44,9 @@
 #define TEST_IMAGE_SIZE_MB_SMALL 64
 
 /*** Globals ***/
-static char tmp_path[] = "/tmp/qtest.XX";
-static char debug_path[] = "/tmp/qtest-blkdebug.XX";
-static char mig_socket[] = "/tmp/qtest-migration.XX";
+static char *tmp_path;
+static char *debug_path;
+static char *mig_socket;
 static bool ahci_pedantic;
 static const char *imgfmt;
 static unsigned test_image_size_mb;
@@ -1437,10 +1437,10 @@ static void test_ncq_simple(void)
 
 static int prepare_iso(size_t size, unsigned char **buf, char **name)
 {
-char cdrom_path[] = "/tmp/qtest.iso.XX";
+g_autofree char *cdrom_path = NULL;
 unsigned char *patt;
 ssize_t ret;
-int fd = mkstemp(cdrom_path);
+int fd = g_file_open_tmp("qtest.iso.XX", &cdrom_path, NULL);
 
 g_assert(fd != -1);
 g_assert(buf);
@@ -1872,7 +1872,7 @@ int main(int argc, char **argv)
 }
 
 /* Create a temporary image */
-fd = mkstemp(tmp_path);
+fd = g_file_open_tmp("qtest.XX", &tmp_path, NULL);
 g_assert(fd >= 0);
 if (have_qemu_img()) {
 imgfmt = "qcow2";
@@ -1889,12 +1889,12 @@ int main(int argc, char **argv)
 close(fd);
 
 /* Create temporary blkdebug instructions */
-fd = mkstemp(debug_path);
+fd = g_file_open_tmp("qtest-blkdebug.XX", &debug_path, NULL);
 g_assert(fd >= 0);
 close(fd);
 
 /* Reserve a hollow file to use as a socket for migration tests */
-fd = mkstemp(mig_socket);
+fd = g_file_open_tmp("qtest-migration.XX", &mig_socket, NULL);
 g_assert(fd >= 0);
 close(fd);
 
@@ -1947,8 +1947,11 @@ int main(int argc, char **argv)
 
 /* Cleanup */
 unlink(tmp_path);
+g_free(tmp_path);
 unlink(debug_path);
+g_free(debug_path);
 unlink(mig_socket);
+g_free(mig_socket);
 
 return ret;
 }
-- 
2.34.1




[PATCH v3 13/54] tests/qtest: ide-test: Avoid using hardcoded /tmp

2022-09-25 Thread Bin Meng
From: Bin Meng 

This case was written to use hardcoded /tmp directory for temporary
files. Update to use g_file_open_tmp() for a portable implementation.

Signed-off-by: Bin Meng 
---

Changes in v3:
- Split to a separate patch

 tests/qtest/ide-test.c | 10 ++
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/tests/qtest/ide-test.c b/tests/qtest/ide-test.c
index 5bcb75a7e5..25302be6dc 100644
--- a/tests/qtest/ide-test.c
+++ b/tests/qtest/ide-test.c
@@ -121,8 +121,8 @@ enum {
 static QPCIBus *pcibus = NULL;
 static QGuestAllocator guest_malloc;
 
-static char tmp_path[] = "/tmp/qtest.XX";
-static char debug_path[] = "/tmp/qtest-blkdebug.XX";
+static char *tmp_path;
+static char *debug_path;
 
 static QTestState *ide_test_start(const char *cmdline_fmt, ...)
 {
@@ -1015,12 +1015,12 @@ int main(int argc, char **argv)
 int ret;
 
 /* Create temporary blkdebug instructions */
-fd = mkstemp(debug_path);
+fd = g_file_open_tmp("qtest-blkdebug.XX", &debug_path, NULL);
 g_assert(fd >= 0);
 close(fd);
 
 /* Create a temporary raw image */
-fd = mkstemp(tmp_path);
+fd = g_file_open_tmp("qtest.XX", &tmp_path, NULL);
 g_assert(fd >= 0);
 ret = ftruncate(fd, TEST_IMAGE_SIZE);
 g_assert(ret == 0);
@@ -1049,7 +1049,9 @@ int main(int argc, char **argv)
 
 /* Cleanup */
 unlink(tmp_path);
+g_free(tmp_path);
 unlink(debug_path);
+g_free(debug_path);
 
 return ret;
 }
-- 
2.34.1




[PATCH v3 19/54] tests/qtest: virtio-blk-test: Avoid using hardcoded /tmp

2022-09-25 Thread Bin Meng
From: Bin Meng 

This case was written to use hardcoded /tmp directory for temporary
files. Update to use g_file_open_tmp() for a portable implementation.

Signed-off-by: Bin Meng 
---

Changes in v3:
- Split to a separate patch

 tests/qtest/virtio-blk-test.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/tests/qtest/virtio-blk-test.c b/tests/qtest/virtio-blk-test.c
index dc5eed31c8..19c01f808b 100644
--- a/tests/qtest/virtio-blk-test.c
+++ b/tests/qtest/virtio-blk-test.c
@@ -49,10 +49,10 @@ static void drive_destroy(void *path)
 static char *drive_create(void)
 {
 int fd, ret;
-char *t_path = g_strdup("/tmp/qtest.XX");
+char *t_path;
 
 /* Create a temporary raw image */
-fd = mkstemp(t_path);
+fd = g_file_open_tmp("qtest.XX", &t_path, NULL);
 g_assert_cmpint(fd, >=, 0);
 ret = ftruncate(fd, TEST_IMAGE_SIZE);
 g_assert_cmpint(ret, ==, 0);
-- 
2.34.1




[PATCH v3 25/54] block/vvfat: Unify the mkdir() call

2022-09-25 Thread Bin Meng
From: Bin Meng 

There is a difference in the mkdir() call for win32 and non-win32
platforms, and currently is handled in the codes with #ifdefs.

glib provides a portable g_mkdir() API and we can use it to unify
the codes without #ifdefs.

Signed-off-by: Bin Meng 
Reviewed-by: Marc-André Lureau 
---

(no changes since v2)

Changes in v2:
- Change to use g_mkdir()

 block/vvfat.c | 9 +++--
 1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/block/vvfat.c b/block/vvfat.c
index d6dd919683..723beef025 100644
--- a/block/vvfat.c
+++ b/block/vvfat.c
@@ -25,6 +25,7 @@
 
 #include "qemu/osdep.h"
 #include 
+#include 
 #include "qapi/error.h"
 #include "block/block_int.h"
 #include "block/qdict.h"
@@ -2726,13 +2727,9 @@ static int handle_renames_and_mkdirs(BDRVVVFATState* s)
 mapping_t* mapping;
 int j, parent_path_len;
 
-#ifdef __MINGW32__
-if (mkdir(commit->path))
+if (g_mkdir(commit->path, 0755)) {
 return -5;
-#else
-if (mkdir(commit->path, 0755))
-return -5;
-#endif
+}
 
 mapping = insert_mapping(s, commit->param.mkdir.cluster,
 commit->param.mkdir.cluster + 1);
-- 
2.34.1




[PATCH v3 37/54] tests/qtest: {ahci, ide}-test: Use relative path for temporary files for win32

2022-09-25 Thread Bin Meng
From: Bin Meng 

These test cases uses "blkdebug:path/to/config:path/to/image" for
testing. On Windows, absolute file paths contain the delimiter ':'
which causes the blkdebug filename parser fail to parse filenames.

Signed-off-by: Bin Meng 
Reviewed-by: Marc-André Lureau 
---

(no changes since v1)

 tests/qtest/ahci-test.c | 21 ++---
 tests/qtest/ide-test.c  | 20 ++--
 2 files changed, 36 insertions(+), 5 deletions(-)

diff --git a/tests/qtest/ahci-test.c b/tests/qtest/ahci-test.c
index 1d5929d8c3..66652fed04 100644
--- a/tests/qtest/ahci-test.c
+++ b/tests/qtest/ahci-test.c
@@ -1833,7 +1833,7 @@ static void create_ahci_io_test(enum IOMode type, enum 
AddrMode addr,
 
 int main(int argc, char **argv)
 {
-const char *arch;
+const char *arch, *base;
 int ret;
 int fd;
 int c;
@@ -1871,8 +1871,22 @@ int main(int argc, char **argv)
 return 0;
 }
 
+/*
+ * "base" stores the starting point where we create temporary files.
+ *
+ * On Windows, this is set to the relative path of current working
+ * directory, because the absolute path causes the blkdebug filename
+ * parser fail to parse "blkdebug:path/to/config:path/to/image".
+ */
+#ifndef _WIN32
+base = g_get_tmp_dir();
+#else
+base = ".";
+#endif
+
 /* Create a temporary image */
-fd = g_file_open_tmp("qtest.XX", &tmp_path, NULL);
+tmp_path = g_strdup_printf("%s/qtest.XX", base);
+fd = g_mkstemp(tmp_path);
 g_assert(fd >= 0);
 if (have_qemu_img()) {
 imgfmt = "qcow2";
@@ -1889,7 +1903,8 @@ int main(int argc, char **argv)
 close(fd);
 
 /* Create temporary blkdebug instructions */
-fd = g_file_open_tmp("qtest-blkdebug.XX", &debug_path, NULL);
+debug_path = g_strdup_printf("%s/qtest-blkdebug.XX", base);
+fd = g_mkstemp(debug_path);
 g_assert(fd >= 0);
 close(fd);
 
diff --git a/tests/qtest/ide-test.c b/tests/qtest/ide-test.c
index 25302be6dc..5e3e28aea2 100644
--- a/tests/qtest/ide-test.c
+++ b/tests/qtest/ide-test.c
@@ -1011,16 +1011,32 @@ static void test_cdrom_dma(void)
 
 int main(int argc, char **argv)
 {
+const char *base;
 int fd;
 int ret;
 
+/*
+ * "base" stores the starting point where we create temporary files.
+ *
+ * On Windows, this is set to the relative path of current working
+ * directory, because the absolute path causes the blkdebug filename
+ * parser fail to parse "blkdebug:path/to/config:path/to/image".
+ */
+#ifndef _WIN32
+base = g_get_tmp_dir();
+#else
+base = ".";
+#endif
+
 /* Create temporary blkdebug instructions */
-fd = g_file_open_tmp("qtest-blkdebug.XX", &debug_path, NULL);
+debug_path = g_strdup_printf("%s/qtest-blkdebug.XX", base);
+fd = g_mkstemp(debug_path);
 g_assert(fd >= 0);
 close(fd);
 
 /* Create a temporary raw image */
-fd = g_file_open_tmp("qtest.XX", &tmp_path, NULL);
+tmp_path = g_strdup_printf("%s/qtest.XX", base);
+fd = g_mkstemp(tmp_path);
 g_assert(fd >= 0);
 ret = ftruncate(fd, TEST_IMAGE_SIZE);
 g_assert(ret == 0);
-- 
2.34.1




[PATCH v3 40/54] tests/qtest: ide-test: Open file in binary mode

2022-09-25 Thread Bin Meng
From: Xuzhou Cheng 

By default Windows opens file in text mode, while a POSIX compliant
implementation treats text files and binary files the same.

The fopen() 'mode' string can include the letter 'b' to indicate
binary mode shall be used. POSIX spec says the character 'b' shall
have no effect, but is allowed for ISO C standard conformance.
Let's add the letter 'b' which works on both POSIX and Windows.

Signed-off-by: Xuzhou Cheng 
Signed-off-by: Bin Meng 
Reviewed-by: Marc-André Lureau 
---

(no changes since v2)

Changes in v2:
- Drop ahci-test.c changes that are no longer needed

 tests/qtest/ide-test.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/tests/qtest/ide-test.c b/tests/qtest/ide-test.c
index 5e3e28aea2..4ea89c26c9 100644
--- a/tests/qtest/ide-test.c
+++ b/tests/qtest/ide-test.c
@@ -892,7 +892,7 @@ static void cdrom_pio_impl(int nblocks)
 
 /* Prepopulate the CDROM with an interesting pattern */
 generate_pattern(pattern, patt_len, ATAPI_BLOCK_SIZE);
-fh = fopen(tmp_path, "w+");
+fh = fopen(tmp_path, "wb+");
 ret = fwrite(pattern, ATAPI_BLOCK_SIZE, patt_blocks, fh);
 g_assert_cmpint(ret, ==, patt_blocks);
 fclose(fh);
@@ -993,7 +993,7 @@ static void test_cdrom_dma(void)
 prdt[0].size = cpu_to_le32(len | PRDT_EOT);
 
 generate_pattern(pattern, ATAPI_BLOCK_SIZE * 16, ATAPI_BLOCK_SIZE);
-fh = fopen(tmp_path, "w+");
+fh = fopen(tmp_path, "wb+");
 ret = fwrite(pattern, ATAPI_BLOCK_SIZE, 16, fh);
 g_assert_cmpint(ret, ==, 16);
 fclose(fh);
-- 
2.34.1




[PATCH v3 2/2] hw/ide/piix: Ignore writes of hardwired PCI command register bits

2022-09-25 Thread Lev Kujawski
One method to enable PCI bus mastering for IDE controllers, often used
by x86 firmware, is to write 0x7 to the PCI command register.  Neither
the PIIX3 specification nor actual hardware (a Tyan S1686D system)
permit modification of the Memory Space Enable (MSE) bit, 1, and thus
the command register would be left in an unspecified state without
this patch.

* hw/ide/pci.c
  Call post_load if provided by derived IDE controller.
* hw/ide/piix.c
  a) Add references to the PIIX data sheets.
  b) Mask the MSE bit using the QEMU PCI device wmask field.
  c) Add a post_load function to mask bits from saved machine states.
  d) Specify post_load for both the PIIX3/4 IDE controllers.
* include/hw/ide/pci.h
  Switch from SIMPLE_TYPE to TYPE, explicitly create a PCIIDEClass
  that includes the post_load function pointer.
* tests/qtest/ide-test.c
  Use the command_disabled field of the QPCIDevice testing model to
  indicate that PCI_COMMAND_MEMORY is hardwired in the PIIX3/4 IDE
  controller.

Signed-off-by: Lev Kujawski 
---
(v2) Use QEMU's built-in PCI bit-masking support rather than attempting
 to manually filter writes.  Thanks to Philippe Mathieu-Daude and
 Michael S. Tsirkin for review and the pointer.
(v3) Handle migration of older machine states, which may have set bits
 masked by this patch, via a new post_load method of PCIIDEClass.
 Thanks to Michael S. Tsirkin for catching this via review.

 hw/ide/pci.c   |  5 +
 hw/ide/piix.c  | 39 +++
 include/hw/ide/pci.h   |  7 ++-
 tests/qtest/ide-test.c |  1 +
 4 files changed, 51 insertions(+), 1 deletion(-)

diff --git a/hw/ide/pci.c b/hw/ide/pci.c
index 84ba733548..e42c7b9415 100644
--- a/hw/ide/pci.c
+++ b/hw/ide/pci.c
@@ -447,6 +447,7 @@ static const VMStateDescription vmstate_bmdma = {
 
 static int ide_pci_post_load(void *opaque, int version_id)
 {
+PCIIDEClass *dc = PCI_IDE_GET_CLASS(opaque);
 PCIIDEState *d = opaque;
 int i;
 
@@ -457,6 +458,10 @@ static int ide_pci_post_load(void *opaque, int version_id)
 ide_bmdma_post_load(&d->bmdma[i], -1);
 }
 
+if (dc->post_load) {
+dc->post_load(d, version_id);
+}
+
 return 0;
 }
 
diff --git a/hw/ide/piix.c b/hw/ide/piix.c
index 9a9b28078e..fd55ecbd36 100644
--- a/hw/ide/piix.c
+++ b/hw/ide/piix.c
@@ -21,6 +21,12 @@
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
FROM,
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  * THE SOFTWARE.
+ *
+ * References:
+ *  [1] 82371FB (PIIX) AND 82371SB (PIIX3) PCI ISA IDE XCELERATOR,
+ *  290550-002, Intel Corporation, April 1997.
+ *  [2] 82371AB PCI-TO-ISA / IDE XCELERATOR (PIIX4), 290562-001,
+ *  Intel Corporation, April 1997.
  */
 
 #include "qemu/osdep.h"
@@ -159,6 +165,19 @@ static void pci_piix_ide_realize(PCIDevice *dev, Error 
**errp)
 uint8_t *pci_conf = dev->config;
 int rc;
 
+/*
+ * Mask all IDE PCI command register bits except for Bus Master
+ * Function Enable (bit 2) and I/O Space Enable (bit 0), as the
+ * remainder are hardwired to 0 [1, p.48] [2, p.89-90].
+ *
+ * NOTE: According to the PIIX3 datasheet [1], the Memory Space
+ * Enable (MSE, bit 1) is hardwired to 1, but this is contradicted
+ * by actual PIIX3 hardware, the datasheet itself (viz., Default
+ * Value: h), and the PIIX4 datasheet [2].
+ */
+pci_set_word(dev->wmask + PCI_COMMAND,
+ PCI_COMMAND_MASTER | PCI_COMMAND_IO);
+
 pci_conf[PCI_CLASS_PROG] = 0x80; // legacy ATA mode
 
 bmdma_setup_bar(d);
@@ -184,11 +203,28 @@ static void pci_piix_ide_exitfn(PCIDevice *dev)
 }
 }
 
+static int pci_piix_ide_post_load(PCIIDEState *s, int version_id)
+{
+PCIDevice *dev = PCI_DEVICE(s);
+uint8_t *pci_conf = dev->config;
+
+/*
+ * To preserve backward compatibility, handle saved machine states
+ * with reserved bits set (see comment in pci_piix_ide_realize()).
+ */
+pci_set_word(pci_conf + PCI_COMMAND,
+ pci_get_word(pci_conf + PCI_COMMAND) &
+ (PCI_COMMAND_MASTER | PCI_COMMAND_IO));
+
+return 0;
+}
+
 /* NOTE: for the PIIX3, the IRQs and IOports are hardcoded */
 static void piix3_ide_class_init(ObjectClass *klass, void *data)
 {
 DeviceClass *dc = DEVICE_CLASS(klass);
 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
+PCIIDEClass *ic = PCI_IDE_CLASS(klass);
 
 dc->reset = piix_ide_reset;
 k->realize = pci_piix_ide_realize;
@@ -196,6 +232,7 @@ static void piix3_ide_class_init(ObjectClass *klass, void 
*data)
 k->vendor_id = PCI_VENDOR_ID_INTEL;
 k->device_id = PCI_DEVICE_ID_INTEL_82371SB_1;
 k->class_id = PCI_CLASS_STORAGE_IDE;
+ic->post_load = pci_piix_ide_post_load;
 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
 dc->hotpluggable = false;
 }
@@ -211,6 +248,7 @@ static void piix4_ide_class_init(ObjectClass *klass, void 
*data)
 {
 Dev

[PATCH v3 1/2] qpci_device_enable: Allow for command bits hardwired to 0

2022-09-25 Thread Lev Kujawski
Devices like the PIIX3/4 IDE controller do not support certain modes
of operation, such as memory space accesses, and indicate this lack of
support by hardwiring the applicable bits to zero.  Extend the QEMU
PCI device testing framework to accommodate such devices.

* tests/qtest/libqos/pci.h: Add the command_disabled word to indicate
  bits hardwired to 0.
* tests/qtest/libqos/pci.c: Verify that hardwired bits are actually
  hardwired.

Signed-off-by: Lev Kujawski 
---
 tests/qtest/libqos/pci.c | 13 +++--
 tests/qtest/libqos/pci.h |  1 +
 2 files changed, 8 insertions(+), 6 deletions(-)

diff --git a/tests/qtest/libqos/pci.c b/tests/qtest/libqos/pci.c
index b23d72346b..4f3d28d8d9 100644
--- a/tests/qtest/libqos/pci.c
+++ b/tests/qtest/libqos/pci.c
@@ -220,18 +220,19 @@ int qpci_secondary_buses_init(QPCIBus *bus)
 
 void qpci_device_enable(QPCIDevice *dev)
 {
-uint16_t cmd;
+const uint16_t enable_bits =
+PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
+uint16_t cmd, new_cmd;
 
 /* FIXME -- does this need to be a bus callout? */
 cmd = qpci_config_readw(dev, PCI_COMMAND);
-cmd |= PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
+cmd |= enable_bits;
 qpci_config_writew(dev, PCI_COMMAND, cmd);
 
 /* Verify the bits are now set. */
-cmd = qpci_config_readw(dev, PCI_COMMAND);
-g_assert_cmphex(cmd & PCI_COMMAND_IO, ==, PCI_COMMAND_IO);
-g_assert_cmphex(cmd & PCI_COMMAND_MEMORY, ==, PCI_COMMAND_MEMORY);
-g_assert_cmphex(cmd & PCI_COMMAND_MASTER, ==, PCI_COMMAND_MASTER);
+new_cmd = qpci_config_readw(dev, PCI_COMMAND);
+new_cmd &= enable_bits;
+g_assert_cmphex(new_cmd, ==, enable_bits & ~dev->command_disabled);
 }
 
 /**
diff --git a/tests/qtest/libqos/pci.h b/tests/qtest/libqos/pci.h
index 8389614523..eaedb98588 100644
--- a/tests/qtest/libqos/pci.h
+++ b/tests/qtest/libqos/pci.h
@@ -68,6 +68,7 @@ struct QPCIDevice
 bool msix_enabled;
 QPCIBar msix_table_bar, msix_pba_bar;
 uint64_t msix_table_off, msix_pba_off;
+uint16_t command_disabled;
 };
 
 struct QPCIAddress {
-- 
2.34.1




[PATCH v3 0/2] Re: hw/ide/piix: Ignore writes of hardwired PCI command register bits

2022-09-25 Thread Lev Kujawski
> On Tue, Sep 06, 2022 at 10:23:57AM -0400, Michael S. Tsirkin wrote:
> > On Thu, Jun 02, 2022 at 08:47:31PM +, Lev Kujawski wrote:
> > > ---
> > > This revised patch uses QEMU's built-in PCI bit-masking support rather
> > > than attempting to manually filter writes.  Thanks to Philippe Mathieu-
> > > Daude and Michael S. Tsirkin for review and the pointer.
> >
> > But pls note I wrote:
> >
> > Might need machine compat machinery
> > for this.
> >
> > without said machinery, if guest set one of the other
> > bits, migration will fail.
>
> I assume v3 will be forthcoming, right?

Thanks for your review and my apologies for the delay.  I hope this revised
patch appropriately handles the machine state migration case.

Kind regards,
Lev Kujawski

Lev Kujawski (2):
  qpci_device_enable: Allow for command bits hardwired to 0
  hw/ide/piix: Ignore writes of hardwired PCI command register bits

 hw/ide/pci.c |  5 +
 hw/ide/piix.c| 39 +++
 include/hw/ide/pci.h |  7 ++-
 tests/qtest/ide-test.c   |  1 +
 tests/qtest/libqos/pci.c | 13 +++--
 tests/qtest/libqos/pci.h |  1 +
 6 files changed, 59 insertions(+), 7 deletions(-)

-- 
2.34.1




[PATCH] qemu-nbd: set timeout to qemu-nbd socket

2022-09-25 Thread luzhipeng
From: lu zhipeng 

Prevent the NBD socket stuck all the time, So
set timeout.

Signed-off-by: lu zhipeng 
---
 nbd/client.c | 8 
 1 file changed, 8 insertions(+)

diff --git a/nbd/client.c b/nbd/client.c
index 30d5383cb1..89dde53a0f 100644
--- a/nbd/client.c
+++ b/nbd/client.c
@@ -24,6 +24,8 @@
 #include "nbd-internal.h"
 #include "qemu/cutils.h"
 
+#define NBD_DEFAULT_TIMEOUT 30
+
 /* Definitions for opaque data types */
 
 static QTAILQ_HEAD(, NBDExport) exports = QTAILQ_HEAD_INITIALIZER(exports);
@@ -1301,6 +1303,12 @@ int nbd_init(int fd, QIOChannelSocket *sioc, 
NBDExportInfo *info,
 }
 }
 
+if (ioctl(fd, NBD_SET_TIMEOUT, NBD_DEFAULT_TIMEOUT) < 0) {
+int serrno = errno;
+error_setg(errp, "Failed setting timeout");
+return -serrno;
+}
+
 trace_nbd_init_finish();
 
 return 0;
-- 
2.31.1