PING: [PATCH 0/4] Add a new backend for cryptodev

2022-09-25 Thread Lei He

On 2022/9/19 11:53, Lei He wrote:

This patch adds a new backend called LKCF to cryptodev, LKCF stands
for Linux Kernel Cryptography Framework. If a cryptographic
accelerator that supports LKCF is installed on the the host (you can
see which algorithms are supported in host's LKCF by executing
'cat /proc/crypto'), then RSA operations can be offloaded.
More background info can refer to: https://lwn.net/Articles/895399/,
'keyctl[5]' in the picture.

This patch:
1. Modified some interfaces of cryptodev and cryptodev-backend to
support asynchronous requests.
2. Extended the DER encoder in crypto, so that we can export the
RSA private key into PKCS#8 format and upload it to host kernel.
3. Added a new backend for cryptodev.

I tested the backend with a QAT card, the qps of RSA-2048-decryption
is about 25k/s, and the main-loop becomes the bottleneck. The qps
using OpenSSL directly is about 6k/s (with 6 vCPUs). We will support
IO-thread for cryptodev in another series later.


PING, sorry if it made noise, can anyone help take a look at this patch,
thanks.

Best regards,
Lei He
--
helei.si...@bytedance.com




Re: [PATCH v2] scsi-disk: support setting CD-ROM block size via device options

2022-09-25 Thread John Millikin
Gentle ping again.

I'd really like to get this ~6 line patch into mainline so I can pop it
off my TODO list.

On Mon, Sep 12, 2022 at 04:54:52PM +0900, John Millikin wrote:
> Gentle ping -- is there anything blocking this patch from going in?
> 
> It's been about a month now since I sent it, and it's not very big, so I
> was hoping it would merge without much difficulty.
> 
> If it's breaking a test or something then I'd be happy to take a look
> and send a new revision.
> 
> On Mon, Sep 05, 2022 at 09:43:20AM +0200, Paolo Bonzini wrote:
> > Probably just my screw up, or it broke something when testing... let me
> > check.
> > 
> > Paolo
> > 
> > Il lun 5 set 2022, 05:30 John Millikin  ha scritto:
> > 
> > > I notice this patch wasn't included in the [PULL] series last week, and
> > > it isn't present in the `master` branch.
> > >
> > > Is there anything else I should be doing to get it merged in? Sorry if
> > > this isn't a good question, I'm not used to QEMU's email-based change
> > > management workflow.
> > >
> > > On Sat, Aug 20, 2022 at 09:42:25AM +0200, Paolo Bonzini wrote:
> > > > No, I had not seen it indeed. Queued now, thanks.
> > > >
> > > > Paolo
> > > >



Re: [PATCH 2/2] target/riscv: rvv-1.0: vf[w]redsum distinguish between ordered/unordered

2022-09-25 Thread Alistair Francis
On Thu, Aug 18, 2022 at 1:43 AM Yang Liu  wrote:
>
> Starting with RVV1.0, the original vf[w]redsum_vs instruction was renamed
> to vf[w]redusum_vs. The distinction between ordered and unordered is also
> more consistent with other instructions, although there is no difference
> in implementation between the two for QEMU.
>
> Signed-off-by: Yang Liu 

Thanks!

Applied to riscv-to-apply.next

Alistair

> ---
>  target/riscv/helper.h   | 15 ++-
>  target/riscv/insn32.decode  |  6 --
>  target/riscv/insn_trans/trans_rvv.c.inc |  6 --
>  target/riscv/vector_helper.c| 19 +--
>  4 files changed, 31 insertions(+), 15 deletions(-)
>
> diff --git a/target/riscv/helper.h b/target/riscv/helper.h
> index 4ef3b2251d..a03014fe67 100644
> --- a/target/riscv/helper.h
> +++ b/target/riscv/helper.h
> @@ -1009,9 +1009,12 @@ DEF_HELPER_6(vwredsum_vs_b, void, ptr, ptr, ptr, ptr, 
> env, i32)
>  DEF_HELPER_6(vwredsum_vs_h, void, ptr, ptr, ptr, ptr, env, i32)
>  DEF_HELPER_6(vwredsum_vs_w, void, ptr, ptr, ptr, ptr, env, i32)
>
> -DEF_HELPER_6(vfredsum_vs_h, void, ptr, ptr, ptr, ptr, env, i32)
> -DEF_HELPER_6(vfredsum_vs_w, void, ptr, ptr, ptr, ptr, env, i32)
> -DEF_HELPER_6(vfredsum_vs_d, void, ptr, ptr, ptr, ptr, env, i32)
> +DEF_HELPER_6(vfredusum_vs_h, void, ptr, ptr, ptr, ptr, env, i32)
> +DEF_HELPER_6(vfredusum_vs_w, void, ptr, ptr, ptr, ptr, env, i32)
> +DEF_HELPER_6(vfredusum_vs_d, void, ptr, ptr, ptr, ptr, env, i32)
> +DEF_HELPER_6(vfredosum_vs_h, void, ptr, ptr, ptr, ptr, env, i32)
> +DEF_HELPER_6(vfredosum_vs_w, void, ptr, ptr, ptr, ptr, env, i32)
> +DEF_HELPER_6(vfredosum_vs_d, void, ptr, ptr, ptr, ptr, env, i32)
>  DEF_HELPER_6(vfredmax_vs_h, void, ptr, ptr, ptr, ptr, env, i32)
>  DEF_HELPER_6(vfredmax_vs_w, void, ptr, ptr, ptr, ptr, env, i32)
>  DEF_HELPER_6(vfredmax_vs_d, void, ptr, ptr, ptr, ptr, env, i32)
> @@ -1019,8 +1022,10 @@ DEF_HELPER_6(vfredmin_vs_h, void, ptr, ptr, ptr, ptr, 
> env, i32)
>  DEF_HELPER_6(vfredmin_vs_w, void, ptr, ptr, ptr, ptr, env, i32)
>  DEF_HELPER_6(vfredmin_vs_d, void, ptr, ptr, ptr, ptr, env, i32)
>
> -DEF_HELPER_6(vfwredsum_vs_h, void, ptr, ptr, ptr, ptr, env, i32)
> -DEF_HELPER_6(vfwredsum_vs_w, void, ptr, ptr, ptr, ptr, env, i32)
> +DEF_HELPER_6(vfwredusum_vs_h, void, ptr, ptr, ptr, ptr, env, i32)
> +DEF_HELPER_6(vfwredusum_vs_w, void, ptr, ptr, ptr, ptr, env, i32)
> +DEF_HELPER_6(vfwredosum_vs_h, void, ptr, ptr, ptr, ptr, env, i32)
> +DEF_HELPER_6(vfwredosum_vs_w, void, ptr, ptr, ptr, ptr, env, i32)
>
>  DEF_HELPER_6(vmand_mm, void, ptr, ptr, ptr, ptr, env, i32)
>  DEF_HELPER_6(vmnand_mm, void, ptr, ptr, ptr, ptr, env, i32)
> diff --git a/target/riscv/insn32.decode b/target/riscv/insn32.decode
> index 4033565393..2873a7ae04 100644
> --- a/target/riscv/insn32.decode
> +++ b/target/riscv/insn32.decode
> @@ -659,11 +659,13 @@ vredmax_vs  000111 . . . 010 . 1010111 
> @r_vm
>  vwredsumu_vs11 . . . 000 . 1010111 @r_vm
>  vwredsum_vs 110001 . . . 000 . 1010111 @r_vm
>  # Vector ordered and unordered reduction sum
> -vfredsum_vs -1 . . . 001 . 1010111 @r_vm
> +vfredusum_vs01 . . . 001 . 1010111 @r_vm
> +vfredosum_vs11 . . . 001 . 1010111 @r_vm
>  vfredmin_vs 000101 . . . 001 . 1010111 @r_vm
>  vfredmax_vs 000111 . . . 001 . 1010111 @r_vm
>  # Vector widening ordered and unordered float reduction sum
> -vfwredsum_vs1100-1 . . . 001 . 1010111 @r_vm
> +vfwredusum_vs   110001 . . . 001 . 1010111 @r_vm
> +vfwredosum_vs   110011 . . . 001 . 1010111 @r_vm
>  vmand_mm011001 - . . 010 . 1010111 @r
>  vmnand_mm   011101 - . . 010 . 1010111 @r
>  vmandn_mm   011000 - . . 010 . 1010111 @r
> diff --git a/target/riscv/insn_trans/trans_rvv.c.inc 
> b/target/riscv/insn_trans/trans_rvv.c.inc
> index 6c091824b6..9c9de17f8a 100644
> --- a/target/riscv/insn_trans/trans_rvv.c.inc
> +++ b/target/riscv/insn_trans/trans_rvv.c.inc
> @@ -3112,7 +3112,8 @@ static bool freduction_check(DisasContext *s, arg_rmrr 
> *a)
> require_zve64f(s);
>  }
>
> -GEN_OPFVV_TRANS(vfredsum_vs, freduction_check)
> +GEN_OPFVV_TRANS(vfredusum_vs, freduction_check)
> +GEN_OPFVV_TRANS(vfredosum_vs, freduction_check)
>  GEN_OPFVV_TRANS(vfredmax_vs, freduction_check)
>  GEN_OPFVV_TRANS(vfredmin_vs, freduction_check)
>
> @@ -3124,7 +3125,8 @@ static bool freduction_widen_check(DisasContext *s, 
> arg_rmrr *a)
> (s->sew != MO_8);
>  }
>
> -GEN_OPFVV_WIDEN_TRANS(vfwredsum_vs, freduction_widen_check)
> +GEN_OPFVV_WIDEN_TRANS(vfwredusum_vs, freduction_widen_check)
> +GEN_OPFVV_WIDEN_TRANS(vfwredosum_vs, freduction_widen_check)
>
>  /*
>   *** Vector Mask Operations
> diff --git a/target/riscv/vector_helper.c b/target/riscv/vector_helper.c
> index fd83c0b20b..d87f79ad82 100644
> --- a/target/riscv/vector_helper.c

[PATCH] ui/vnc-clipboard: fix integer underflow in vnc_client_cut_text_ext

2022-09-25 Thread Mauro Matteo Cascella
Extended ClientCutText messages start with a 4-byte header. If len < 4,
an integer underflow occurs in vnc_client_cut_text_ext. The result is
used to decompress data in a while loop in inflate_buffer, leading to
CPU consumption and denial of service. Prevent this by checking dlen in
protocol_client_msg.

Fixes: CVE-2022-3165
Fixes: 0bf41cab93e5 ("ui/vnc: clipboard support")
Reported-by: TangPeng 
Signed-off-by: Mauro Matteo Cascella 
---
Extended Clipboard Pseudo-Encoding:
https://github.com/rfbproto/rfbproto/blob/master/rfbproto.rst#extended-clipboard-pseudo-encoding

 ui/vnc.c | 11 ---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/ui/vnc.c b/ui/vnc.c
index 6a05d06147..acb3629cd8 100644
--- a/ui/vnc.c
+++ b/ui/vnc.c
@@ -2442,8 +2442,8 @@ static int protocol_client_msg(VncState *vs, uint8_t 
*data, size_t len)
 if (len == 1) {
 return 8;
 }
+uint32_t dlen = abs(read_s32(data, 4));
 if (len == 8) {
-uint32_t dlen = abs(read_s32(data, 4));
 if (dlen > (1 << 20)) {
 error_report("vnc: client_cut_text msg payload has %u bytes"
  " which exceeds our limit of 1MB.", dlen);
@@ -2456,8 +2456,13 @@ static int protocol_client_msg(VncState *vs, uint8_t 
*data, size_t len)
 }
 
 if (read_s32(data, 4) < 0) {
-vnc_client_cut_text_ext(vs, abs(read_s32(data, 4)),
-read_u32(data, 8), data + 12);
+if (dlen < 4) {
+error_report("vnc: malformed payload (header less than 4 
bytes)"
+ " in extended clipboard pseudo-encoding.");
+vnc_client_error(vs);
+break;
+}
+vnc_client_cut_text_ext(vs, dlen, read_u32(data, 8), data + 12);
 break;
 }
 vnc_client_cut_text(vs, read_u32(data, 4), data + 8);
-- 
2.37.3




Re: [PATCH v3 06/54] tests/qtest: aspeed_smc-test: Avoid using hardcoded /tmp

2022-09-25 Thread Cédric Le Goater

On 9/25/22 13:29, Bin Meng wrote:

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 


Reviewed-by: Cédric Le Goater 

Thanks,

C.


---

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

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

diff --git a/tests/qtest/aspeed_smc-test.c b/tests/qtest/aspeed_smc-test.c
index 05ce941566..c713a3700b 100644
--- a/tests/qtest/aspeed_smc-test.c
+++ b/tests/qtest/aspeed_smc-test.c
@@ -608,16 +608,15 @@ static void test_write_block_protect_bottom_bit(void)
  flash_reset();
  }
  
-static char tmp_path[] = "/tmp/qtest.m25p80.XX";

-
  int main(int argc, char **argv)
  {
+g_autofree char *tmp_path = NULL;
  int ret;
  int fd;
  
  g_test_init(, , NULL);
  
-fd = mkstemp(tmp_path);

+fd = g_file_open_tmp("qtest.m25p80.XX", _path, NULL);
  g_assert(fd >= 0);
  ret = ftruncate(fd, FLASH_SIZE);
  g_assert(ret == 0);





[PATCH 1/2] linux-user: handle /proc/self/exe with execve() syscall

2022-09-25 Thread Laurent Vivier
If path is /proc/self/exe, use the executable file descriptor
provided by binfmt_misc (or opened by main()) with execveat().

Signed-off-by: Laurent Vivier 
---
 linux-user/main.c   | 9 +++--
 linux-user/syscall.c| 9 -
 linux-user/user-internals.h | 1 +
 3 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/linux-user/main.c b/linux-user/main.c
index e44bdb17b853..f915bdd7cef7 100644
--- a/linux-user/main.c
+++ b/linux-user/main.c
@@ -64,6 +64,7 @@
 #endif
 
 char *exec_path;
+int execfd;
 
 int singlestep;
 static const char *argv0;
@@ -646,7 +647,6 @@ int main(int argc, char **argv, char **envp)
 int target_argc;
 int i;
 int ret;
-int execfd;
 unsigned long max_reserved_va;
 bool preserve_argv0;
 
@@ -845,7 +845,12 @@ int main(int argc, char **argv, char **envp)
 
 fd_trans_init();
 
-ret = loader_exec(execfd, exec_path, target_argv, target_environ, regs,
+/*
+ * loader_exec() closes the file descriptor provided by the caller.
+ * As we need to keep it available for execve("/proc/self/exe")
+ * we provide a copy to loader_exec().
+ */
+ret = loader_exec(dup(execfd), exec_path, target_argv, target_environ, 
regs,
 info, );
 if (ret != 0) {
 printf("Error while loading %s: %s\n", exec_path, strerror(-ret));
diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index f4091212027c..6642652b7644 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -649,6 +649,8 @@ safe_syscall4(pid_t, wait4, pid_t, pid, int *, status, int, 
options, \
 safe_syscall5(int, waitid, idtype_t, idtype, id_t, id, siginfo_t *, infop, \
   int, options, struct rusage *, rusage)
 safe_syscall3(int, execve, const char *, filename, char **, argv, char **, 
envp)
+safe_syscall5(int, execveat, int, dirfd, const char *, pathname, char **, \
+  argv, char **, envp, int, flags)
 #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) || \
 defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64)
 safe_syscall6(int, pselect6, int, nfds, fd_set *, readfds, fd_set *, writefds, 
\
@@ -8843,7 +8845,12 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int 
num, abi_long arg1,
  * before the execve completes and makes it the other
  * program's problem.
  */
-ret = get_errno(safe_execve(p, argp, envp));
+if (is_proc_myself(p, "exe")) {
+ret = get_errno(safe_execveat(execfd, "", argp, envp,
+AT_EMPTY_PATH));
+} else {
+ret = get_errno(safe_execve(p, argp, envp));
+}
 unlock_user(p, arg1, 0);
 
 goto execve_end;
diff --git a/linux-user/user-internals.h b/linux-user/user-internals.h
index 0280e76addda..84f29a1e2990 100644
--- a/linux-user/user-internals.h
+++ b/linux-user/user-internals.h
@@ -23,6 +23,7 @@
 #include "qemu/log.h"
 
 extern char *exec_path;
+extern int execfd;
 void init_task_state(TaskState *ts);
 void task_settid(TaskState *);
 void stop_all_tasks(void);
-- 
2.37.3




[PATCH 0/2] linux-user: handle /proc/self/exe with execve() syscall

2022-09-25 Thread Laurent Vivier
Use execfd to re-exectute the binary from /proc/self/exe

Fix do_openat() that should not use execfd.

Laurent Vivier (2):
  linux-user: handle /proc/self/exe with execve() syscall
  linux-user: don't use AT_EXECFD in do_openat()

 linux-user/main.c   |  9 +++--
 linux-user/syscall.c| 12 +---
 linux-user/user-internals.h |  1 +
 3 files changed, 17 insertions(+), 5 deletions(-)

-- 
2.37.3




[PATCH 2/2] linux-user: don't use AT_EXECFD in do_openat()

2022-09-25 Thread Laurent Vivier
AT_EXECFD gives access to the binary file even if
it is not readable (only executable).

Moreover it can be opened with flags and mode that are not the ones
provided by do_openat() caller.

And finally the caller can close the file descriptor whereas
we can need it with execveat().

To avoid that, use only safe_openat() with the exec_path.

Signed-off-by: Laurent Vivier 
---
 linux-user/syscall.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index 6642652b7644..01f03535fe64 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -8265,8 +8265,7 @@ static int do_openat(CPUArchState *cpu_env, int dirfd, 
const char *pathname, int
 };
 
 if (is_proc_myself(pathname, "exe")) {
-int execfd = qemu_getauxval(AT_EXECFD);
-return execfd ? execfd : safe_openat(dirfd, exec_path, flags, mode);
+return safe_openat(dirfd, exec_path, flags, mode);
 }
 
 for (fake_open = fakes; fake_open->filename; fake_open++) {
-- 
2.37.3




Re: [PATCH v3 02/12] linux-user: Add missing clock_gettime64() syscall strace

2022-09-25 Thread Helge Deller

On 9/25/22 17:58, Laurent Vivier wrote:

Le 25/09/2022 à 17:53, Helge Deller a écrit :

On 9/25/22 17:47, Laurent Vivier wrote:

Le 25/09/2022 à 17:27, Helge Deller a écrit :

On 9/25/22 17:09, Laurent Vivier wrote:

Le 18/09/2022 à 21:45, Helge Deller a écrit :

Allow linux-user to strace the clock_gettime64() syscall.
This syscall is used a lot on 32-bit guest architectures which use newer
glibc versions.

Signed-off-by: Helge Deller 
---
  linux-user/strace.c    | 53 ++
  linux-user/strace.list |  4 
  2 files changed, 57 insertions(+)

diff --git a/linux-user/strace.c b/linux-user/strace.c
index a4eeef7ae1..816e679995 100644
--- a/linux-user/strace.c
+++ b/linux-user/strace.c
@@ -82,6 +82,7 @@ UNUSED static void print_buf(abi_long addr, abi_long len, int 
last);
  UNUSED static void print_raw_param(const char *, abi_long, int);
  UNUSED static void print_timeval(abi_ulong, int);
  UNUSED static void print_timespec(abi_ulong, int);
+UNUSED static void print_timespec64(abi_ulong, int);
  UNUSED static void print_timezone(abi_ulong, int);
  UNUSED static void print_itimerval(abi_ulong, int);
  UNUSED static void print_number(abi_long, int);
@@ -795,6 +796,24 @@ print_syscall_ret_clock_gettime(CPUArchState *cpu_env, 
const struct syscallname
  #define print_syscall_ret_clock_getres print_syscall_ret_clock_gettime
  #endif

+#if defined(TARGET_NR_clock_gettime64)
+static void
+print_syscall_ret_clock_gettime64(CPUArchState *cpu_env, const struct 
syscallname *name,
+    abi_long ret, abi_long arg0, abi_long arg1,
+    abi_long arg2, abi_long arg3, abi_long arg4,
+    abi_long arg5)
+{
+    if (!print_syscall_err(ret)) {
+    qemu_log(TARGET_ABI_FMT_ld, ret);
+    qemu_log(" (");
+    print_timespec64(arg1, 1);
+    qemu_log(")");
+    }
+
+    qemu_log("\n");
+}
+#endif
+
  #ifdef TARGET_NR_gettimeofday
  static void
  print_syscall_ret_gettimeofday(CPUArchState *cpu_env, const struct 
syscallname *name,
@@ -1652,6 +1671,27 @@ print_timespec(abi_ulong ts_addr, int last)
  }
  }

+static void
+print_timespec64(abi_ulong ts_addr, int last)
+{
+    if (ts_addr) {
+    struct target__kernel_timespec *ts;
+
+    ts = lock_user(VERIFY_READ, ts_addr, sizeof(*ts), 1);
+    if (!ts) {
+    print_pointer(ts_addr, last);
+    return;
+    }
+    qemu_log("{tv_sec = %lld"
+ ",tv_nsec = %lld}%s",
+ (long long)tswap64(ts->tv_sec), (long 
long)tswap64(ts->tv_nsec),
+ get_comma(last));
+    unlock_user(ts, ts_addr, 0);
+    } else {
+    qemu_log("NULL%s", get_comma(last));
+    }
+}
+
  static void
  print_timezone(abi_ulong tz_addr, int last)
  {
@@ -2267,6 +2307,19 @@ print_clock_gettime(CPUArchState *cpu_env, const struct 
syscallname *name,
  #define print_clock_getres print_clock_gettime
  #endif

+#if defined(TARGET_NR_clock_gettime64)
+static void
+print_clock_gettime64(CPUArchState *cpu_env, const struct syscallname *name,
+    abi_long arg0, abi_long arg1, abi_long arg2,
+    abi_long arg3, abi_long arg4, abi_long arg5)
+{
+    print_syscall_prologue(name);
+    print_enums(clockids, arg0, 0);
+    print_pointer(arg1, 1);
+    print_syscall_epilogue(name);
+}
+#endif


I think it could be simply:

#define print_clock_gettime64 print_clock_gettime where print_clock_gettime() 
is defined.


Unfortunately not, because one uses print_timespec() while the other uses 
print_timespec64().


The syscall_ret part cannot be shared, but the prefix function can, they are 
identical.


Ah.. right. I don't know any longer why I didn't that.
Maybe because of too much #ifdeffery or to keep the patch simple.
Will we leave as-is, will you clean up, or shall I resend that patch?


As you prefer...


I'd suggest to leave as-is for now.

Helge



Re: [PATCH v3 02/12] linux-user: Add missing clock_gettime64() syscall strace

2022-09-25 Thread Laurent Vivier

Le 25/09/2022 à 17:53, Helge Deller a écrit :

On 9/25/22 17:47, Laurent Vivier wrote:

Le 25/09/2022 à 17:27, Helge Deller a écrit :

On 9/25/22 17:09, Laurent Vivier wrote:

Le 18/09/2022 à 21:45, Helge Deller a écrit :

Allow linux-user to strace the clock_gettime64() syscall.
This syscall is used a lot on 32-bit guest architectures which use newer
glibc versions.

Signed-off-by: Helge Deller 
---
  linux-user/strace.c    | 53 ++
  linux-user/strace.list |  4 
  2 files changed, 57 insertions(+)

diff --git a/linux-user/strace.c b/linux-user/strace.c
index a4eeef7ae1..816e679995 100644
--- a/linux-user/strace.c
+++ b/linux-user/strace.c
@@ -82,6 +82,7 @@ UNUSED static void print_buf(abi_long addr, abi_long len, int 
last);
  UNUSED static void print_raw_param(const char *, abi_long, int);
  UNUSED static void print_timeval(abi_ulong, int);
  UNUSED static void print_timespec(abi_ulong, int);
+UNUSED static void print_timespec64(abi_ulong, int);
  UNUSED static void print_timezone(abi_ulong, int);
  UNUSED static void print_itimerval(abi_ulong, int);
  UNUSED static void print_number(abi_long, int);
@@ -795,6 +796,24 @@ print_syscall_ret_clock_gettime(CPUArchState *cpu_env, const struct 
syscallname

  #define print_syscall_ret_clock_getres print_syscall_ret_clock_gettime
  #endif

+#if defined(TARGET_NR_clock_gettime64)
+static void
+print_syscall_ret_clock_gettime64(CPUArchState *cpu_env, const struct 
syscallname *name,
+    abi_long ret, abi_long arg0, abi_long arg1,
+    abi_long arg2, abi_long arg3, abi_long arg4,
+    abi_long arg5)
+{
+    if (!print_syscall_err(ret)) {
+    qemu_log(TARGET_ABI_FMT_ld, ret);
+    qemu_log(" (");
+    print_timespec64(arg1, 1);
+    qemu_log(")");
+    }
+
+    qemu_log("\n");
+}
+#endif
+
  #ifdef TARGET_NR_gettimeofday
  static void
  print_syscall_ret_gettimeofday(CPUArchState *cpu_env, const struct 
syscallname *name,
@@ -1652,6 +1671,27 @@ print_timespec(abi_ulong ts_addr, int last)
  }
  }

+static void
+print_timespec64(abi_ulong ts_addr, int last)
+{
+    if (ts_addr) {
+    struct target__kernel_timespec *ts;
+
+    ts = lock_user(VERIFY_READ, ts_addr, sizeof(*ts), 1);
+    if (!ts) {
+    print_pointer(ts_addr, last);
+    return;
+    }
+    qemu_log("{tv_sec = %lld"
+ ",tv_nsec = %lld}%s",
+ (long long)tswap64(ts->tv_sec), (long 
long)tswap64(ts->tv_nsec),
+ get_comma(last));
+    unlock_user(ts, ts_addr, 0);
+    } else {
+    qemu_log("NULL%s", get_comma(last));
+    }
+}
+
  static void
  print_timezone(abi_ulong tz_addr, int last)
  {
@@ -2267,6 +2307,19 @@ print_clock_gettime(CPUArchState *cpu_env, const struct 
syscallname *name,
  #define print_clock_getres print_clock_gettime
  #endif

+#if defined(TARGET_NR_clock_gettime64)
+static void
+print_clock_gettime64(CPUArchState *cpu_env, const struct syscallname *name,
+    abi_long arg0, abi_long arg1, abi_long arg2,
+    abi_long arg3, abi_long arg4, abi_long arg5)
+{
+    print_syscall_prologue(name);
+    print_enums(clockids, arg0, 0);
+    print_pointer(arg1, 1);
+    print_syscall_epilogue(name);
+}
+#endif


I think it could be simply:

#define print_clock_gettime64 print_clock_gettime where print_clock_gettime() 
is defined.


Unfortunately not, because one uses print_timespec() while the other uses 
print_timespec64().


The syscall_ret part cannot be shared, but the prefix function can, they are 
identical.


Ah.. right. I don't know any longer why I didn't that.
Maybe because of too much #ifdeffery or to keep the patch simple.
Will we leave as-is, will you clean up, or shall I resend that patch?


As you prefer...

Thanks,
Laurent




Re: [PATCH 2/7] linux-user: Add proper strace format strings for getdents()/getdents64()

2022-09-25 Thread Laurent Vivier

Le 24/09/2022 à 13:44, Helge Deller a écrit :

Signed-off-by: Helge Deller 
---
  linux-user/strace.list | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/linux-user/strace.list b/linux-user/strace.list
index da8c1bf34e..bfef568d18 100644
--- a/linux-user/strace.list
+++ b/linux-user/strace.list
@@ -285,10 +285,10 @@
  { TARGET_NR_getcwd, "getcwd" , "%s(%p,%d)", NULL, NULL },
  #endif
  #ifdef TARGET_NR_getdents
-{ TARGET_NR_getdents, "getdents" , NULL, NULL, NULL },
+{ TARGET_NR_getdents, "getdents" , "%s(%d,%p,%u)", NULL, NULL },
  #endif
  #ifdef TARGET_NR_getdents64
-{ TARGET_NR_getdents64, "getdents64" , NULL, NULL, NULL },
+{ TARGET_NR_getdents64, "getdents64" , "%s(%d,%p,%u)", NULL, NULL },


3rd argument is a size_t.
I think it should be TARGET_ABI_FMT_lu.

Thanks,
Laurent

  #endif
  #ifdef TARGET_NR_getdomainname
  { TARGET_NR_getdomainname, "getdomainname" , NULL, NULL, NULL },
--
2.37.3







Re: [PATCH v3 02/12] linux-user: Add missing clock_gettime64() syscall strace

2022-09-25 Thread Helge Deller

On 9/25/22 17:47, Laurent Vivier wrote:

Le 25/09/2022 à 17:27, Helge Deller a écrit :

On 9/25/22 17:09, Laurent Vivier wrote:

Le 18/09/2022 à 21:45, Helge Deller a écrit :

Allow linux-user to strace the clock_gettime64() syscall.
This syscall is used a lot on 32-bit guest architectures which use newer
glibc versions.

Signed-off-by: Helge Deller 
---
  linux-user/strace.c    | 53 ++
  linux-user/strace.list |  4 
  2 files changed, 57 insertions(+)

diff --git a/linux-user/strace.c b/linux-user/strace.c
index a4eeef7ae1..816e679995 100644
--- a/linux-user/strace.c
+++ b/linux-user/strace.c
@@ -82,6 +82,7 @@ UNUSED static void print_buf(abi_long addr, abi_long len, int 
last);
  UNUSED static void print_raw_param(const char *, abi_long, int);
  UNUSED static void print_timeval(abi_ulong, int);
  UNUSED static void print_timespec(abi_ulong, int);
+UNUSED static void print_timespec64(abi_ulong, int);
  UNUSED static void print_timezone(abi_ulong, int);
  UNUSED static void print_itimerval(abi_ulong, int);
  UNUSED static void print_number(abi_long, int);
@@ -795,6 +796,24 @@ print_syscall_ret_clock_gettime(CPUArchState *cpu_env, 
const struct syscallname
  #define print_syscall_ret_clock_getres print_syscall_ret_clock_gettime
  #endif

+#if defined(TARGET_NR_clock_gettime64)
+static void
+print_syscall_ret_clock_gettime64(CPUArchState *cpu_env, const struct 
syscallname *name,
+    abi_long ret, abi_long arg0, abi_long arg1,
+    abi_long arg2, abi_long arg3, abi_long arg4,
+    abi_long arg5)
+{
+    if (!print_syscall_err(ret)) {
+    qemu_log(TARGET_ABI_FMT_ld, ret);
+    qemu_log(" (");
+    print_timespec64(arg1, 1);
+    qemu_log(")");
+    }
+
+    qemu_log("\n");
+}
+#endif
+
  #ifdef TARGET_NR_gettimeofday
  static void
  print_syscall_ret_gettimeofday(CPUArchState *cpu_env, const struct 
syscallname *name,
@@ -1652,6 +1671,27 @@ print_timespec(abi_ulong ts_addr, int last)
  }
  }

+static void
+print_timespec64(abi_ulong ts_addr, int last)
+{
+    if (ts_addr) {
+    struct target__kernel_timespec *ts;
+
+    ts = lock_user(VERIFY_READ, ts_addr, sizeof(*ts), 1);
+    if (!ts) {
+    print_pointer(ts_addr, last);
+    return;
+    }
+    qemu_log("{tv_sec = %lld"
+ ",tv_nsec = %lld}%s",
+ (long long)tswap64(ts->tv_sec), (long 
long)tswap64(ts->tv_nsec),
+ get_comma(last));
+    unlock_user(ts, ts_addr, 0);
+    } else {
+    qemu_log("NULL%s", get_comma(last));
+    }
+}
+
  static void
  print_timezone(abi_ulong tz_addr, int last)
  {
@@ -2267,6 +2307,19 @@ print_clock_gettime(CPUArchState *cpu_env, const struct 
syscallname *name,
  #define print_clock_getres print_clock_gettime
  #endif

+#if defined(TARGET_NR_clock_gettime64)
+static void
+print_clock_gettime64(CPUArchState *cpu_env, const struct syscallname *name,
+    abi_long arg0, abi_long arg1, abi_long arg2,
+    abi_long arg3, abi_long arg4, abi_long arg5)
+{
+    print_syscall_prologue(name);
+    print_enums(clockids, arg0, 0);
+    print_pointer(arg1, 1);
+    print_syscall_epilogue(name);
+}
+#endif


I think it could be simply:

#define print_clock_gettime64 print_clock_gettime where print_clock_gettime() 
is defined.


Unfortunately not, because one uses print_timespec() while the other uses 
print_timespec64().


The syscall_ret part cannot be shared, but the prefix function can, they are 
identical.


Ah.. right. I don't know any longer why I didn't that.
Maybe because of too much #ifdeffery or to keep the patch simple.
Will we leave as-is, will you clean up, or shall I resend that patch?

Helge



Re: [PATCH 1/7] linux-user: Fix TARGET_PROT_SEM for XTENSA

2022-09-25 Thread Laurent Vivier

Le 24/09/2022 à 13:44, Helge Deller a écrit :

The xtensa platform has a value of 0x10 for PROT_SEM.

Signed-off-by: Helge Deller 
---
  linux-user/syscall_defs.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/linux-user/syscall_defs.h b/linux-user/syscall_defs.h
index 85b0f33e91..1e3577bfa5 100644
--- a/linux-user/syscall_defs.h
+++ b/linux-user/syscall_defs.h
@@ -1246,7 +1246,7 @@ struct target_winsize {

  #include "termbits.h"

-#if defined(TARGET_MIPS)
+#if defined(TARGET_MIPS) || defined(TARGET_XTENSA)
  #define TARGET_PROT_SEM 0x10
  #else
  #define TARGET_PROT_SEM 0x08
--
2.37.3




Reviewed-by: Laurent Vivier 




Re: [PATCH v3 02/12] linux-user: Add missing clock_gettime64() syscall strace

2022-09-25 Thread Laurent Vivier

Le 25/09/2022 à 17:27, Helge Deller a écrit :

On 9/25/22 17:09, Laurent Vivier wrote:

Le 18/09/2022 à 21:45, Helge Deller a écrit :

Allow linux-user to strace the clock_gettime64() syscall.
This syscall is used a lot on 32-bit guest architectures which use newer
glibc versions.

Signed-off-by: Helge Deller 
---
  linux-user/strace.c    | 53 ++
  linux-user/strace.list |  4 
  2 files changed, 57 insertions(+)

diff --git a/linux-user/strace.c b/linux-user/strace.c
index a4eeef7ae1..816e679995 100644
--- a/linux-user/strace.c
+++ b/linux-user/strace.c
@@ -82,6 +82,7 @@ UNUSED static void print_buf(abi_long addr, abi_long len, int 
last);
  UNUSED static void print_raw_param(const char *, abi_long, int);
  UNUSED static void print_timeval(abi_ulong, int);
  UNUSED static void print_timespec(abi_ulong, int);
+UNUSED static void print_timespec64(abi_ulong, int);
  UNUSED static void print_timezone(abi_ulong, int);
  UNUSED static void print_itimerval(abi_ulong, int);
  UNUSED static void print_number(abi_long, int);
@@ -795,6 +796,24 @@ print_syscall_ret_clock_gettime(CPUArchState *cpu_env, 
const struct syscallname
  #define print_syscall_ret_clock_getres print_syscall_ret_clock_gettime
  #endif

+#if defined(TARGET_NR_clock_gettime64)
+static void
+print_syscall_ret_clock_gettime64(CPUArchState *cpu_env, const struct 
syscallname *name,
+    abi_long ret, abi_long arg0, abi_long arg1,
+    abi_long arg2, abi_long arg3, abi_long arg4,
+    abi_long arg5)
+{
+    if (!print_syscall_err(ret)) {
+    qemu_log(TARGET_ABI_FMT_ld, ret);
+    qemu_log(" (");
+    print_timespec64(arg1, 1);
+    qemu_log(")");
+    }
+
+    qemu_log("\n");
+}
+#endif
+
  #ifdef TARGET_NR_gettimeofday
  static void
  print_syscall_ret_gettimeofday(CPUArchState *cpu_env, const struct 
syscallname *name,
@@ -1652,6 +1671,27 @@ print_timespec(abi_ulong ts_addr, int last)
  }
  }

+static void
+print_timespec64(abi_ulong ts_addr, int last)
+{
+    if (ts_addr) {
+    struct target__kernel_timespec *ts;
+
+    ts = lock_user(VERIFY_READ, ts_addr, sizeof(*ts), 1);
+    if (!ts) {
+    print_pointer(ts_addr, last);
+    return;
+    }
+    qemu_log("{tv_sec = %lld"
+ ",tv_nsec = %lld}%s",
+ (long long)tswap64(ts->tv_sec), (long 
long)tswap64(ts->tv_nsec),
+ get_comma(last));
+    unlock_user(ts, ts_addr, 0);
+    } else {
+    qemu_log("NULL%s", get_comma(last));
+    }
+}
+
  static void
  print_timezone(abi_ulong tz_addr, int last)
  {
@@ -2267,6 +2307,19 @@ print_clock_gettime(CPUArchState *cpu_env, const struct 
syscallname *name,
  #define print_clock_getres print_clock_gettime
  #endif

+#if defined(TARGET_NR_clock_gettime64)
+static void
+print_clock_gettime64(CPUArchState *cpu_env, const struct syscallname *name,
+    abi_long arg0, abi_long arg1, abi_long arg2,
+    abi_long arg3, abi_long arg4, abi_long arg5)
+{
+    print_syscall_prologue(name);
+    print_enums(clockids, arg0, 0);
+    print_pointer(arg1, 1);
+    print_syscall_epilogue(name);
+}
+#endif


I think it could be simply:

#define print_clock_gettime64 print_clock_gettime where print_clock_gettime() 
is defined.


Unfortunately not, because one uses print_timespec() while the other uses 
print_timespec64().



The syscall_ret part cannot be shared, but the prefix function can, they are 
identical.

Thanks,
Laurent




Re: [PATCH v3 11/12] linux-user: Add close_range() syscall

2022-09-25 Thread Laurent Vivier

Le 18/09/2022 à 21:45, Helge Deller a écrit :

Signed-off-by: Helge Deller 
---
  linux-user/strace.list |  3 +++
  linux-user/syscall.c   | 16 
  2 files changed, 19 insertions(+)

diff --git a/linux-user/strace.list b/linux-user/strace.list
index 215d971b2a..ad9ef94689 100644
--- a/linux-user/strace.list
+++ b/linux-user/strace.list
@@ -103,6 +103,9 @@
  #ifdef TARGET_NR_close
  { TARGET_NR_close, "close" , "%s(%d)", NULL, NULL },
  #endif
+#ifdef TARGET_NR_close_range
+{ TARGET_NR_close_range, "close_range" , "%s(%d,%d,%d)", NULL, NULL },
+#endif
  #ifdef TARGET_NR_connect
  { TARGET_NR_connect, "connect" , "%s(%d,%#x,%d)", NULL, NULL },
  #endif
diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index ca39acfceb..2e0e974562 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -338,6 +338,10 @@ _syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
  #ifdef __NR_exit_group
  _syscall1(int,exit_group,int,error_code)
  #endif
+#if defined(__NR_close_range) && defined(TARGET_NR_close_range)
+#define __NR_sys_close_range __NR_close_range
+_syscall3(int,sys_close_range,int,first,int,last,int,flags)
+#endif
  #if defined(__NR_futex)
  _syscall6(int,sys_futex,int *,uaddr,int,op,int,val,
const struct timespec *,timeout,int *,uaddr2,int,val3)
@@ -8721,6 +8725,18 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int 
num, abi_long arg1,
  case TARGET_NR_close:
  fd_trans_unregister(arg1);
  return get_errno(close(arg1));
+#if defined(__NR_close_range) && defined(TARGET_NR_close_range)
+case TARGET_NR_close_range:
+{
+abi_long fd;
+abi_long maxfd = (arg2 == (abi_long)-1) ? target_fd_max : arg2;
+
+for (fd = arg1; fd <= maxfd; fd++) {
+fd_trans_unregister(fd);
+}
+}
+return get_errno(sys_close_range(arg1, arg2, arg3));


if flags is CLOSE_RANGE_CLOEXEC, the fd is not closed so you don't want to call 
fd_trans_unregister() in this case.


It would be better to call fd_trans_unregister() loop only if sys_close_range() 
doesn't fail.

Thanks,
Laurent


+#endif

  case TARGET_NR_brk:
  return do_brk(arg1);
--
2.37.3







Re: [PATCH v3 10/12] linux-user: Show timespec on strace for futex()

2022-09-25 Thread Laurent Vivier

Le 18/09/2022 à 21:45, Helge Deller a écrit :

Signed-off-by: Helge Deller 
---
  linux-user/strace.c | 11 ++-
  1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/linux-user/strace.c b/linux-user/strace.c
index 6f818212d5..b6b9abaea4 100644
--- a/linux-user/strace.c
+++ b/linux-user/strace.c
@@ -3714,11 +3714,20 @@ print_futex(CPUArchState *cpu_env, const struct 
syscallname *name,
  abi_long arg0, abi_long arg1, abi_long arg2,
  abi_long arg3, abi_long arg4, abi_long arg5)
  {
+abi_long op = arg1 & FUTEX_CMD_MASK;
  print_syscall_prologue(name);
  print_pointer(arg0, 0);
  print_futex_op(arg1, 0);
  print_raw_param(",%d", arg2, 0);
-print_pointer(arg3, 0); /* struct timespec */
+switch (op) {
+case FUTEX_WAIT:
+case FUTEX_WAIT_BITSET:
+print_timespec(arg3, 0);
+break;
+default:
+print_pointer(arg3, 0);
+break;
+}
  print_pointer(arg4, 0);
  print_raw_param("%d", arg4, 1);
  print_syscall_epilogue(name);
--
2.37.3



Reviewed-by: Laurent Vivier 








Re: [PATCH v3 09/12] linux-user: Add strace for clock_nanosleep()

2022-09-25 Thread Laurent Vivier

Le 18/09/2022 à 21:45, Helge Deller a écrit :

Signed-off-by: Helge Deller 
---
  linux-user/strace.c| 15 +++
  linux-user/strace.list |  3 ++-
  2 files changed, 17 insertions(+), 1 deletion(-)



Reviewed-by: Laurent Vivier 




Re: [PATCH v3 07/12] linux-user: Fix strace of chmod() if mode == 0

2022-09-25 Thread Laurent Vivier

Le 18/09/2022 à 21:45, Helge Deller a écrit :

If the mode parameter of chmod() is zero, this value isn't shown
when stracing a program:
 chmod("filename",)
This patch fixes it up to show the zero-value as well:
 chmod("filename",000)

Signed-off-by: Helge Deller 
---
  linux-user/strace.c | 5 +
  1 file changed, 5 insertions(+)

diff --git a/linux-user/strace.c b/linux-user/strace.c
index 5ac64df02b..2f539845bb 100644
--- a/linux-user/strace.c
+++ b/linux-user/strace.c
@@ -1505,6 +1505,11 @@ print_file_mode(abi_long mode, int last)
  const char *sep = "";
  const struct flags *m;

+if (mode == 0) {
+qemu_log("000%s", get_comma(last));
+return;
+}
+
  for (m = _flags[0]; m->f_string != NULL; m++) {
  if ((m->f_value & mode) == m->f_value) {
  qemu_log("%s%s", m->f_string, sep);
--
2.37.3



Reviewed-by: Laurent Vivier 







Re: [PATCH v3 06/12] linux-user/hppa: Dump IIR on register dump

2022-09-25 Thread Laurent Vivier

Le 18/09/2022 à 21:45, Helge Deller a écrit :

Include the IIR register (which holds the opcode of the failing
instruction) when dumping the hppa registers.

Signed-off-by: Helge Deller 
---
  target/hppa/helper.c | 6 --
  1 file changed, 4 insertions(+), 2 deletions(-)



Reviewed-by: Laurent Vivier 





Re: [PATCH v3 02/12] linux-user: Add missing clock_gettime64() syscall strace

2022-09-25 Thread Helge Deller

On 9/25/22 17:09, Laurent Vivier wrote:

Le 18/09/2022 à 21:45, Helge Deller a écrit :

Allow linux-user to strace the clock_gettime64() syscall.
This syscall is used a lot on 32-bit guest architectures which use newer
glibc versions.

Signed-off-by: Helge Deller 
---
  linux-user/strace.c    | 53 ++
  linux-user/strace.list |  4 
  2 files changed, 57 insertions(+)

diff --git a/linux-user/strace.c b/linux-user/strace.c
index a4eeef7ae1..816e679995 100644
--- a/linux-user/strace.c
+++ b/linux-user/strace.c
@@ -82,6 +82,7 @@ UNUSED static void print_buf(abi_long addr, abi_long len, int 
last);
  UNUSED static void print_raw_param(const char *, abi_long, int);
  UNUSED static void print_timeval(abi_ulong, int);
  UNUSED static void print_timespec(abi_ulong, int);
+UNUSED static void print_timespec64(abi_ulong, int);
  UNUSED static void print_timezone(abi_ulong, int);
  UNUSED static void print_itimerval(abi_ulong, int);
  UNUSED static void print_number(abi_long, int);
@@ -795,6 +796,24 @@ print_syscall_ret_clock_gettime(CPUArchState *cpu_env, 
const struct syscallname
  #define print_syscall_ret_clock_getres print_syscall_ret_clock_gettime
  #endif

+#if defined(TARGET_NR_clock_gettime64)
+static void
+print_syscall_ret_clock_gettime64(CPUArchState *cpu_env, const struct 
syscallname *name,
+    abi_long ret, abi_long arg0, abi_long arg1,
+    abi_long arg2, abi_long arg3, abi_long arg4,
+    abi_long arg5)
+{
+    if (!print_syscall_err(ret)) {
+    qemu_log(TARGET_ABI_FMT_ld, ret);
+    qemu_log(" (");
+    print_timespec64(arg1, 1);
+    qemu_log(")");
+    }
+
+    qemu_log("\n");
+}
+#endif
+
  #ifdef TARGET_NR_gettimeofday
  static void
  print_syscall_ret_gettimeofday(CPUArchState *cpu_env, const struct 
syscallname *name,
@@ -1652,6 +1671,27 @@ print_timespec(abi_ulong ts_addr, int last)
  }
  }

+static void
+print_timespec64(abi_ulong ts_addr, int last)
+{
+    if (ts_addr) {
+    struct target__kernel_timespec *ts;
+
+    ts = lock_user(VERIFY_READ, ts_addr, sizeof(*ts), 1);
+    if (!ts) {
+    print_pointer(ts_addr, last);
+    return;
+    }
+    qemu_log("{tv_sec = %lld"
+ ",tv_nsec = %lld}%s",
+ (long long)tswap64(ts->tv_sec), (long 
long)tswap64(ts->tv_nsec),
+ get_comma(last));
+    unlock_user(ts, ts_addr, 0);
+    } else {
+    qemu_log("NULL%s", get_comma(last));
+    }
+}
+
  static void
  print_timezone(abi_ulong tz_addr, int last)
  {
@@ -2267,6 +2307,19 @@ print_clock_gettime(CPUArchState *cpu_env, const struct 
syscallname *name,
  #define print_clock_getres print_clock_gettime
  #endif

+#if defined(TARGET_NR_clock_gettime64)
+static void
+print_clock_gettime64(CPUArchState *cpu_env, const struct syscallname *name,
+    abi_long arg0, abi_long arg1, abi_long arg2,
+    abi_long arg3, abi_long arg4, abi_long arg5)
+{
+    print_syscall_prologue(name);
+    print_enums(clockids, arg0, 0);
+    print_pointer(arg1, 1);
+    print_syscall_epilogue(name);
+}
+#endif


I think it could be simply:

#define print_clock_gettime64 print_clock_gettime where print_clock_gettime() 
is defined.


Unfortunately not, because one uses print_timespec() while the other uses 
print_timespec64().


except that:

Reviewed-by: Laurent Vivier 


Thanks!
Helge



Re: [PATCH v3 05/12] linux-user/hppa: Use EXCP_DUMP() to show enhanced debug info

2022-09-25 Thread Laurent Vivier

Le 18/09/2022 à 21:45, Helge Deller a écrit :

Enhance the hppa linux-user cpu_loop() to show more debugging info
on hard errors.

Signed-off-by: Helge Deller 
---
  linux-user/hppa/cpu_loop.c | 6 +-
  1 file changed, 5 insertions(+), 1 deletion(-)




Reviewed-by: Laurent Vivier 





Re: [PATCH v3 04/12] linux-user: Log failing executable in EXCP_DUMP()

2022-09-25 Thread Laurent Vivier

Le 18/09/2022 à 21:45, Helge Deller a écrit :

Enhance the EXCP_DUMP() macro to print out the failing program too.
During debugging it's sometimes hard to track down the actual failing
program if you are e.g. building a whole debian package.

Signed-off-by: Helge Deller 
---
  linux-user/cpu_loop-common.h | 2 ++
  1 file changed, 2 insertions(+)



Reviewed-by: Laurent Vivier 





Re: [PATCH v3 03/12] linux-user: Add pidfd_open(), pidfd_send_signal() and pidfd_getfd() syscalls

2022-09-25 Thread Laurent Vivier

Le 18/09/2022 à 21:45, Helge Deller a écrit :

I noticed those were missing when running the glib2.0 testsuite.
Add the syscalls including the strace output.

Signed-off-by: Helge Deller 
---
  linux-user/strace.c| 28 
  linux-user/strace.list |  9 +
  linux-user/syscall.c   | 34 ++
  3 files changed, 71 insertions(+)



Reviewed-by: Laurent Vivier 





Re: [PATCH v3 02/12] linux-user: Add missing clock_gettime64() syscall strace

2022-09-25 Thread Laurent Vivier

Le 18/09/2022 à 21:45, Helge Deller a écrit :

Allow linux-user to strace the clock_gettime64() syscall.
This syscall is used a lot on 32-bit guest architectures which use newer
glibc versions.

Signed-off-by: Helge Deller 
---
  linux-user/strace.c| 53 ++
  linux-user/strace.list |  4 
  2 files changed, 57 insertions(+)

diff --git a/linux-user/strace.c b/linux-user/strace.c
index a4eeef7ae1..816e679995 100644
--- a/linux-user/strace.c
+++ b/linux-user/strace.c
@@ -82,6 +82,7 @@ UNUSED static void print_buf(abi_long addr, abi_long len, int 
last);
  UNUSED static void print_raw_param(const char *, abi_long, int);
  UNUSED static void print_timeval(abi_ulong, int);
  UNUSED static void print_timespec(abi_ulong, int);
+UNUSED static void print_timespec64(abi_ulong, int);
  UNUSED static void print_timezone(abi_ulong, int);
  UNUSED static void print_itimerval(abi_ulong, int);
  UNUSED static void print_number(abi_long, int);
@@ -795,6 +796,24 @@ print_syscall_ret_clock_gettime(CPUArchState *cpu_env, 
const struct syscallname
  #define print_syscall_ret_clock_getres print_syscall_ret_clock_gettime
  #endif

+#if defined(TARGET_NR_clock_gettime64)
+static void
+print_syscall_ret_clock_gettime64(CPUArchState *cpu_env, const struct 
syscallname *name,
+abi_long ret, abi_long arg0, abi_long arg1,
+abi_long arg2, abi_long arg3, abi_long arg4,
+abi_long arg5)
+{
+if (!print_syscall_err(ret)) {
+qemu_log(TARGET_ABI_FMT_ld, ret);
+qemu_log(" (");
+print_timespec64(arg1, 1);
+qemu_log(")");
+}
+
+qemu_log("\n");
+}
+#endif
+
  #ifdef TARGET_NR_gettimeofday
  static void
  print_syscall_ret_gettimeofday(CPUArchState *cpu_env, const struct 
syscallname *name,
@@ -1652,6 +1671,27 @@ print_timespec(abi_ulong ts_addr, int last)
  }
  }

+static void
+print_timespec64(abi_ulong ts_addr, int last)
+{
+if (ts_addr) {
+struct target__kernel_timespec *ts;
+
+ts = lock_user(VERIFY_READ, ts_addr, sizeof(*ts), 1);
+if (!ts) {
+print_pointer(ts_addr, last);
+return;
+}
+qemu_log("{tv_sec = %lld"
+ ",tv_nsec = %lld}%s",
+ (long long)tswap64(ts->tv_sec), (long 
long)tswap64(ts->tv_nsec),
+ get_comma(last));
+unlock_user(ts, ts_addr, 0);
+} else {
+qemu_log("NULL%s", get_comma(last));
+}
+}
+
  static void
  print_timezone(abi_ulong tz_addr, int last)
  {
@@ -2267,6 +2307,19 @@ print_clock_gettime(CPUArchState *cpu_env, const struct 
syscallname *name,
  #define print_clock_getres print_clock_gettime
  #endif

+#if defined(TARGET_NR_clock_gettime64)
+static void
+print_clock_gettime64(CPUArchState *cpu_env, const struct syscallname *name,
+abi_long arg0, abi_long arg1, abi_long arg2,
+abi_long arg3, abi_long arg4, abi_long arg5)
+{
+print_syscall_prologue(name);
+print_enums(clockids, arg0, 0);
+print_pointer(arg1, 1);
+print_syscall_epilogue(name);
+}
+#endif


I think it could be simply:

#define print_clock_gettime64 print_clock_gettime where print_clock_gettime() 
is defined.

except that:

Reviewed-by: Laurent Vivier 




Re: [PATCH v3 01/12] linux-user: Add missing signals in strace output

2022-09-25 Thread Laurent Vivier

Le 18/09/2022 à 21:45, Helge Deller a écrit :

Some of the guest signal numbers are currently not converted to
their representative names in the strace output, e.g. SIGVTALRM.

This patch introduces a smart way to generate and keep in sync the
host-to-guest and guest-to-host signal conversion tables for usage in
the qemu signal and strace code. This ensures that any signals
will now show up in both tables.

There is no functional change in this patch - with the exception that yet
missing signal names now show up in the strace code too.

Signed-off-by: Helge Deller 
---
  linux-user/signal-common.h | 46 ++
  linux-user/signal.c| 37 +++---
  linux-user/strace.c| 30 +
  3 files changed, 60 insertions(+), 53 deletions(-)



Reviewed-by: Laurent Vivier 




Re: [PATCH 1/2] target/m68k: increase size of m68k CPU features from uint32_t to uint64_t

2022-09-25 Thread Philippe Mathieu-Daudé via
On Sun, Sep 25, 2022 at 3:53 PM Mark Cave-Ayland
 wrote:
>
> There are already 32 feature bits in use, so change the size of the m68k
> CPU features to uint64_t (along with the associated m68k_feature()
> functions) to allow up to 64 feature bits to be used.
>
> At the same time make use of the BIT_ULL() macro when reading/writing
> the CPU feature bits to improve readability, and also update m68k_feature()
> to return a bool rather than an int.
>
> Signed-off-by: Mark Cave-Ayland 
> ---
>  target/m68k/cpu.c | 4 ++--
>  target/m68k/cpu.h | 6 +++---
>  2 files changed, 5 insertions(+), 5 deletions(-)

Reviewed-by: Philippe Mathieu-Daudé 



[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






[PATCH 2/2] target/m68k: use M68K_FEATURE_MOVEFROMSR_PRIV feature for move_from_sr privilege check

2022-09-25 Thread Mark Cave-Ayland
Now that M68K_FEATURE_M68000 has been renamed to M68K_FEATURE_M68K it is easier
to see that the privilege exception check is wrong: it is currently only 
generated
for ColdFire CPUs when in fact it should also be generated for Motorola CPUs 
from
the 68010 onwards.

Introduce a new M68K_FEATURE_MOVEFROMSR_PRIV feature which is set for all non-
Motorola CPUs, and for all Motorola CPUs from the 68010 onwards and use it to
determine whether a privilege exception should be generated for the MOVE-from-SR
instruction.

Signed-off-by: Mark Cave-Ayland 
Reviewed-by: Richard Henderson 
---
 target/m68k/cpu.c   | 5 +
 target/m68k/cpu.h   | 2 ++
 target/m68k/translate.c | 2 +-
 3 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/target/m68k/cpu.c b/target/m68k/cpu.c
index 8d23c72056..25d610db21 100644
--- a/target/m68k/cpu.c
+++ b/target/m68k/cpu.c
@@ -102,6 +102,7 @@ static void m5206_cpu_initfn(Object *obj)
 CPUM68KState *env = >env;
 
 m68k_set_feature(env, M68K_FEATURE_CF_ISA_A);
+m68k_set_feature(env, M68K_FEATURE_MOVEFROMSR_PRIV);
 }
 
 /* Base feature set, including isns. for m68k family */
@@ -129,6 +130,7 @@ static void m68010_cpu_initfn(Object *obj)
 m68k_set_feature(env, M68K_FEATURE_RTD);
 m68k_set_feature(env, M68K_FEATURE_BKPT);
 m68k_set_feature(env, M68K_FEATURE_MOVEC);
+m68k_set_feature(env, M68K_FEATURE_MOVEFROMSR_PRIV);
 }
 
 /*
@@ -241,6 +243,7 @@ static void m5208_cpu_initfn(Object *obj)
 m68k_set_feature(env, M68K_FEATURE_BRAL);
 m68k_set_feature(env, M68K_FEATURE_CF_EMAC);
 m68k_set_feature(env, M68K_FEATURE_USP);
+m68k_set_feature(env, M68K_FEATURE_MOVEFROMSR_PRIV);
 }
 
 static void cfv4e_cpu_initfn(Object *obj)
@@ -254,6 +257,7 @@ static void cfv4e_cpu_initfn(Object *obj)
 m68k_set_feature(env, M68K_FEATURE_CF_FPU);
 m68k_set_feature(env, M68K_FEATURE_CF_EMAC);
 m68k_set_feature(env, M68K_FEATURE_USP);
+m68k_set_feature(env, M68K_FEATURE_MOVEFROMSR_PRIV);
 }
 
 static void any_cpu_initfn(Object *obj)
@@ -275,6 +279,7 @@ static void any_cpu_initfn(Object *obj)
 m68k_set_feature(env, M68K_FEATURE_USP);
 m68k_set_feature(env, M68K_FEATURE_EXT_FULL);
 m68k_set_feature(env, M68K_FEATURE_WORD_INDEX);
+m68k_set_feature(env, M68K_FEATURE_MOVEFROMSR_PRIV);
 }
 
 static void m68k_cpu_realizefn(DeviceState *dev, Error **errp)
diff --git a/target/m68k/cpu.h b/target/m68k/cpu.h
index f5c6e95cb4..3a9cfe2f33 100644
--- a/target/m68k/cpu.h
+++ b/target/m68k/cpu.h
@@ -537,6 +537,8 @@ enum m68k_features {
 M68K_FEATURE_UNALIGNED_DATA,
 /* TRAPcc insn. (680[2346]0, and CPU32) */
 M68K_FEATURE_TRAPCC,
+/* MOVE from SR privileged (from 68010) */
+M68K_FEATURE_MOVEFROMSR_PRIV,
 };
 
 static inline bool m68k_feature(CPUM68KState *env, int feature)
diff --git a/target/m68k/translate.c b/target/m68k/translate.c
index 233b9d8e57..9df17aa4b2 100644
--- a/target/m68k/translate.c
+++ b/target/m68k/translate.c
@@ -4624,7 +4624,7 @@ DISAS_INSN(move_from_sr)
 {
 TCGv sr;
 
-if (IS_USER(s) && !m68k_feature(env, M68K_FEATURE_M68K)) {
+if (IS_USER(s) && m68k_feature(env, M68K_FEATURE_MOVEFROMSR_PRIV)) {
 gen_exception(s, s->base.pc_next, EXCP_PRIVILEGE);
 return;
 }
-- 
2.30.2




[PATCH 1/2] target/m68k: increase size of m68k CPU features from uint32_t to uint64_t

2022-09-25 Thread Mark Cave-Ayland
There are already 32 feature bits in use, so change the size of the m68k
CPU features to uint64_t (along with the associated m68k_feature()
functions) to allow up to 64 feature bits to be used.

At the same time make use of the BIT_ULL() macro when reading/writing
the CPU feature bits to improve readability, and also update m68k_feature()
to return a bool rather than an int.

Signed-off-by: Mark Cave-Ayland 
---
 target/m68k/cpu.c | 4 ++--
 target/m68k/cpu.h | 6 +++---
 2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/target/m68k/cpu.c b/target/m68k/cpu.c
index f681be3a2a..8d23c72056 100644
--- a/target/m68k/cpu.c
+++ b/target/m68k/cpu.c
@@ -38,12 +38,12 @@ static bool m68k_cpu_has_work(CPUState *cs)
 
 static void m68k_set_feature(CPUM68KState *env, int feature)
 {
-env->features |= (1u << feature);
+env->features |= BIT_ULL(feature);
 }
 
 static void m68k_unset_feature(CPUM68KState *env, int feature)
 {
-env->features &= (-1u - (1u << feature));
+env->features &= ~BIT_ULL(feature);
 }
 
 static void m68k_cpu_reset(DeviceState *dev)
diff --git a/target/m68k/cpu.h b/target/m68k/cpu.h
index 67b6c12c28..f5c6e95cb4 100644
--- a/target/m68k/cpu.h
+++ b/target/m68k/cpu.h
@@ -154,7 +154,7 @@ typedef struct CPUArchState {
 struct {} end_reset_fields;
 
 /* Fields from here on are preserved across CPU reset. */
-uint32_t features;
+uint64_t features;
 } CPUM68KState;
 
 /*
@@ -539,9 +539,9 @@ enum m68k_features {
 M68K_FEATURE_TRAPCC,
 };
 
-static inline int m68k_feature(CPUM68KState *env, int feature)
+static inline bool m68k_feature(CPUM68KState *env, int feature)
 {
-return (env->features & (1u << feature)) != 0;
+return (env->features & BIT_ULL(feature)) != 0;
 }
 
 void m68k_cpu_list(void);
-- 
2.30.2




Re: [PATCH 2/4] hw/intc: sifive_plic.c: Fix interrupt priority index.

2022-09-25 Thread Jim Shu
Hi Tyler,

This fix is incorrect.

In PLIC spec, Interrupt Source Priority Memory Map is
0x00: Reserved (interrupt source 0 does not exist)
0x04: Interrupt source 1 priority
0x08: Interrupt source 2 priority

Current RISC-V machines (virt, sifive_u) use 0x4 as priority_base, so
current formula "irq = ((addr - plic->priority_base) >> 2) + 1" will
take offset 0x4 as IRQ source 1, which is correct.
Your fix will cause the bug in existing machines.

Thanks,
Jim Shu




On Tue, Sep 6, 2022 at 11:21 PM Tyler Ng  wrote:
>
> Here's the patch SHA that introduced the offset: 
> 0feb4a7129eb4f120c75849ddc9e50495c50cb63
>
> -Tyler
>
> On Mon, Sep 5, 2022 at 6:15 AM Andrew Jones  wrote:
>>
>> On Thu, Sep 01, 2022 at 03:50:06PM -0700, Tyler Ng wrote:
>> > Fixes a bug in which the index of the interrupt priority is off by 1.
>> > For example, using an IRQ number of 3 with a priority of 1 is supposed to 
>> > set
>> > plic->source_priority[2] = 1, but instead it sets
>> > plic->source_priority[3] = 1. When an interrupt is claimed to be
>> > serviced, it checks the index 2 instead of 3.
>> >
>> > Signed-off-by: Tyler Ng 
>>
>> Fixes tag?
>>
>> Thanks,
>> drew
>>
>> > ---
>> >  hw/intc/sifive_plic.c | 2 +-
>> >  1 file changed, 1 insertion(+), 1 deletion(-)
>> >
>> > diff --git a/hw/intc/sifive_plic.c b/hw/intc/sifive_plic.c
>> > index af4ae3630e..e75c47300a 100644
>> > --- a/hw/intc/sifive_plic.c
>> > +++ b/hw/intc/sifive_plic.c
>> > @@ -178,7 +178,7 @@ static void sifive_plic_write(void *opaque, hwaddr
>> > addr, uint64_t value,
>> >  SiFivePLICState *plic = opaque;
>> >
>> >  if (addr_between(addr, plic->priority_base, plic->num_sources << 2)) {
>> > -uint32_t irq = ((addr - plic->priority_base) >> 2) + 1;
>> > +uint32_t irq = ((addr - plic->priority_base) >> 2) + 0;
>> >
>> >  plic->source_priority[irq] = value & 7;
>> >  sifive_plic_update(plic);
>> > --
>> > 2.30.2
>> >



[PATCH 0/2] target/m68k: MacOS supervisor/user mode switch fixes - part 2

2022-09-25 Thread Mark Cave-Ayland
Here are the two outstanding patches from
https://lists.gnu.org/archive/html/qemu-devel/2022-09/msg02340.html updated and
rebased onto git master.

Patch 1 is equivalent to patch 2 in the original series but using BIT_ULL() to
set and clear the feature bits, and updating m68k_feature() to return a bool
instead of int.

Patch 2 is the same as the previous patch 3 but with Richard's R-B tag added.

Signed-off-by: Mark Cave-Ayland 


Mark Cave-Ayland (2):
  target/m68k: increase size of m68k CPU features from uint32_t to
uint64_t
  target/m68k: use M68K_FEATURE_MOVEFROMSR_PRIV feature for move_from_sr
privilege check

 target/m68k/cpu.c   | 9 +++--
 target/m68k/cpu.h   | 8 +---
 target/m68k/translate.c | 2 +-
 3 files changed, 13 insertions(+), 6 deletions(-)

-- 
2.30.2




[PATCH] hw/intc: sifive_plic: fix hard-coded max priority level

2022-09-25 Thread Jim Shu
The maximum priority level is hard-coded when writing to interrupt
priority register. However, when writing to priority threshold register,
the maximum priority level is from num_priorities Property which is
configured by platform.

Also change interrupt priority register to use num_priorities Property
in maximum priority level.

Signed-off-by: Emmanuel Blot 
Signed-off-by: Jim Shu 
Reviewed-by: Frank Chang 
---
 hw/intc/sifive_plic.c | 6 --
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/hw/intc/sifive_plic.c b/hw/intc/sifive_plic.c
index af4ae3630e..f864efa761 100644
--- a/hw/intc/sifive_plic.c
+++ b/hw/intc/sifive_plic.c
@@ -180,8 +180,10 @@ static void sifive_plic_write(void *opaque, hwaddr addr, 
uint64_t value,
 if (addr_between(addr, plic->priority_base, plic->num_sources << 2)) {
 uint32_t irq = ((addr - plic->priority_base) >> 2) + 1;
 
-plic->source_priority[irq] = value & 7;
-sifive_plic_update(plic);
+if (value <= plic->num_priorities) {
+plic->source_priority[irq] = value;
+sifive_plic_update(plic);
+}
 } else if (addr_between(addr, plic->pending_base,
 plic->num_sources >> 3)) {
 qemu_log_mask(LOG_GUEST_ERROR,
-- 
2.17.1




Re: [PATCH v1 1/9] hw: encode accessing CPU index in MemTxAttrs

2022-09-25 Thread Alex Bennée


Richard Henderson  writes:

> On 9/22/22 14:58, Alex Bennée wrote:
>> We currently have hacks across the hw/ to reference current_cpu to
>> work out what the current accessing CPU is. This breaks in some cases
>> including using gdbstub to access HW state. As we have MemTxAttrs to
>> describe details about the access lets extend it to mention if this is
>> a CPU access and which one it is.
>> There are a number of places we need to fix up including:
>>CPU helpers directly calling address_space_*() fns
>>models in hw/ fishing the data out of current_cpu
>> I'll start addressing some of these in following patches.
>> Signed-off-by: Alex Bennée 
>> ---
>> v2
>>- use separate field cpu_index
>>- bool for requester_is_cpu
>> ---
>>   include/exec/memattrs.h |  4 
>>   accel/tcg/cputlb.c  | 22 --
>>   hw/core/cpu-sysemu.c| 17 +
>>   3 files changed, 33 insertions(+), 10 deletions(-)
>> diff --git a/include/exec/memattrs.h b/include/exec/memattrs.h
>> index 9fb98bc1ef..e83a993c21 100644
>> --- a/include/exec/memattrs.h
>> +++ b/include/exec/memattrs.h
>> @@ -43,6 +43,10 @@ typedef struct MemTxAttrs {
>>* (see MEMTX_ACCESS_ERROR).
>>*/
>>   unsigned int memory:1;
>> +/* Requester is CPU (or as CPU, e.g. debug) */
>> +bool requester_is_cpu:1;
>> +/* cpu_index (if requester_is_cpu) */
>> +unsigned int cpu_index:16;
>>   /* Requester ID (for MSI for example) */
>>   unsigned int requester_id:16;
>
> I'm not keen on adding another field like this.

Hmm I thought it was unavoidable from Edgar's comment:

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

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

Of course we don't have to care about external use cases but it seemed
to indicate we might need both.

> I don't think it addresses Peter's point about unique identifiers on
> e.g. the MSI bus. But addressing that is surely an problem for any
> host/pci bridge that supports PCI. Because we're already talking about
> two different busses -- PCI, and the one between the cpu and the
> bridge.

We can return to overloading requester_id with a enum to indicate the
type of bus.

> What bounds our max number of cpus at the moment?  We use "int" in
> struct CPUCore, but that's almost certainly for convenience.
>
> target/s390x/cpu.h:#define S390_MAX_CPUS 248
> hw/i386/pc_piix.c:m->max_cpus = HVM_MAX_VCPUS;
>
> hw/i386/pc_q35.c:m->max_cpus = 288;
>
> hw/arm/virt.c:mc->max_cpus = 512;
>
> hw/arm/sbsa-ref.c:mc->max_cpus = 512;
>
> hw/i386/microvm.c:mc->max_cpus = 288;
>
> hw/ppc/spapr.c:mc->max_cpus = INT32_MAX;
>
>
> Most of these are nicely bounded, but HVM_MAX_VCPUS is a magic number
> from Xen, and ppc appears to be prepared for 31 bits worth of cpus.

>From 5642e4513e (spapr.c: do not use MachineClass::max_cpus to limit
CPUs) I think it is being a little optimistic. Even with the beefiest
hosts you start to see diminishing returns by ~12 vCPUs and it won't
take long before each extra vCPU just slows you down.

>
>
>> @@ -1340,8 +1340,13 @@ static uint64_t io_readx(CPUArchState *env, 
>> CPUIOTLBEntry *iotlbentry,
>>   uint64_t val;
>>   bool locked = false;
>>   MemTxResult r;
>> +MemTxAttrs attrs = iotlbentry->attrs;
>>   -section = iotlb_to_section(cpu, iotlbentry->addr,
>> iotlbentry->attrs);
>> +/* encode the accessing CPU */
>> +attrs.requester_is_cpu = 1;
>> +attrs.cpu_index = cpu->cpu_index;
>
>
> As I said before, we cannot set these generically, or
> MEMTXATTRS_UNSPECIFIED means nothing.  Furthermore, they should be set
> at the point we create the tlb entry, not while we're reading it.
> Thus this must be done by each target's tlb_fill function.

I was confused by the last comment because I forgot the TLBs are not
shared between cores. So I can just bang:

MemTxAttrs attrs = { .cpu_index = cs->cpu_index };

into arm_cpu_tlb_fill and be done with it? Or only when we know it is an
IOTLB being filled?

>
>> @@ -51,13 +51,22 @@ hwaddr cpu_get_phys_page_attrs_debug(CPUState *cpu, 
>> vaddr addr,
>>MemTxAttrs *attrs)
>>   {
>>   CPUClass *cc = CPU_GET_CLASS(cpu);
>> +MemTxAttrs local = { };
>> +hwaddr res;
>> if (cc->sysemu_ops->get_phys_page_attrs_debug) {
>> -return cc->sysemu_ops->get_phys_page_attrs_debug(cpu, addr, attrs);
>> +res = cc->sysemu_ops->get_phys_page_attrs_debug(cpu, addr, );
>> +} else {
>> +/* Fallback for CPUs which don't implement the _attrs_ hook */
>> +local = MEMTXATTRS_UNSPECIFIED;
>> +res = 

[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 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 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(>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)
 {
 

[PATCH v2 12/13] hw/ppc/mac.h: Rename to include/hw/nvram/mac_nvram.h

2022-09-25 Thread BALATON Zoltan
All that is left in mac.h now belongs to the nvram emulation so rename
it accordingly and only include it where it is really used.

Signed-off-by: BALATON Zoltan 
Reviewed-by: Mark Cave-Ayland 
---
 MAINTAINERS  |  1 +
 hw/ide/macio.c   |  1 -
 hw/intc/heathrow_pic.c   |  1 -
 hw/intc/openpic.c|  1 -
 hw/misc/macio/cuda.c |  1 -
 hw/misc/macio/gpio.c |  1 -
 hw/misc/macio/macio.c|  1 -
 hw/misc/macio/pmu.c  |  1 -
 hw/nvram/mac_nvram.c |  2 +-
 hw/pci-host/grackle.c|  1 -
 hw/pci-host/uninorth.c   |  1 -
 hw/ppc/mac_newworld.c|  2 +-
 hw/ppc/mac_oldworld.c|  1 -
 include/hw/misc/macio/macio.h|  2 +-
 hw/ppc/mac.h => include/hw/nvram/mac_nvram.h | 11 ++-
 15 files changed, 10 insertions(+), 18 deletions(-)
 rename hw/ppc/mac.h => include/hw/nvram/mac_nvram.h (89%)

diff --git a/MAINTAINERS b/MAINTAINERS
index 738c4eb647..41a4700979 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -1328,6 +1328,7 @@ F: hw/nvram/mac_nvram.c
 F: hw/input/adb*
 F: include/hw/misc/macio/
 F: include/hw/misc/mos6522.h
+F: include/hw/nvram/mac_nvram.h
 F: include/hw/ppc/mac_dbdma.h
 F: include/hw/pci-host/uninorth.h
 F: include/hw/input/adb*
diff --git a/hw/ide/macio.c b/hw/ide/macio.c
index 1c15c37ec5..e604466acb 100644
--- a/hw/ide/macio.c
+++ b/hw/ide/macio.c
@@ -24,7 +24,6 @@
  */
 
 #include "qemu/osdep.h"
-#include "hw/ppc/mac.h"
 #include "hw/ppc/mac_dbdma.h"
 #include "hw/qdev-properties.h"
 #include "migration/vmstate.h"
diff --git a/hw/intc/heathrow_pic.c b/hw/intc/heathrow_pic.c
index cb97c315da..13048a2735 100644
--- a/hw/intc/heathrow_pic.c
+++ b/hw/intc/heathrow_pic.c
@@ -24,7 +24,6 @@
  */
 
 #include "qemu/osdep.h"
-#include "hw/ppc/mac.h"
 #include "migration/vmstate.h"
 #include "qemu/module.h"
 #include "hw/intc/heathrow_pic.h"
diff --git a/hw/intc/openpic.c b/hw/intc/openpic.c
index b0787e8ee7..c757adbe53 100644
--- a/hw/intc/openpic.c
+++ b/hw/intc/openpic.c
@@ -32,7 +32,6 @@
 
 #include "qemu/osdep.h"
 #include "hw/irq.h"
-#include "hw/ppc/mac.h"
 #include "hw/pci/pci.h"
 #include "hw/ppc/openpic.h"
 #include "hw/ppc/ppc_e500.h"
diff --git a/hw/misc/macio/cuda.c b/hw/misc/macio/cuda.c
index 1498113cfc..0d4c13319a 100644
--- a/hw/misc/macio/cuda.c
+++ b/hw/misc/macio/cuda.c
@@ -25,7 +25,6 @@
 
 #include "qemu/osdep.h"
 #include "hw/irq.h"
-#include "hw/ppc/mac.h"
 #include "hw/qdev-properties.h"
 #include "migration/vmstate.h"
 #include "hw/input/adb.h"
diff --git a/hw/misc/macio/gpio.c b/hw/misc/macio/gpio.c
index b1bcf830c3..c8ac5633b2 100644
--- a/hw/misc/macio/gpio.c
+++ b/hw/misc/macio/gpio.c
@@ -24,7 +24,6 @@
  */
 
 #include "qemu/osdep.h"
-#include "hw/ppc/mac.h"
 #include "hw/qdev-properties.h"
 #include "migration/vmstate.h"
 #include "hw/misc/macio/macio.h"
diff --git a/hw/misc/macio/macio.c b/hw/misc/macio/macio.c
index f9f0758b03..93a7c7bbc8 100644
--- a/hw/misc/macio/macio.c
+++ b/hw/misc/macio/macio.c
@@ -26,7 +26,6 @@
 #include "qemu/osdep.h"
 #include "qapi/error.h"
 #include "qemu/module.h"
-#include "hw/ppc/mac.h"
 #include "hw/misc/macio/cuda.h"
 #include "hw/pci/pci.h"
 #include "hw/ppc/mac_dbdma.h"
diff --git a/hw/misc/macio/pmu.c b/hw/misc/macio/pmu.c
index 336502a84b..70562ed8d0 100644
--- a/hw/misc/macio/pmu.c
+++ b/hw/misc/macio/pmu.c
@@ -29,7 +29,6 @@
  */
 
 #include "qemu/osdep.h"
-#include "hw/ppc/mac.h"
 #include "hw/qdev-properties.h"
 #include "migration/vmstate.h"
 #include "hw/input/adb.h"
diff --git a/hw/nvram/mac_nvram.c b/hw/nvram/mac_nvram.c
index 11f2d31cdb..3d9ddda217 100644
--- a/hw/nvram/mac_nvram.c
+++ b/hw/nvram/mac_nvram.c
@@ -25,7 +25,7 @@
 
 #include "qemu/osdep.h"
 #include "hw/nvram/chrp_nvram.h"
-#include "hw/ppc/mac.h"
+#include "hw/nvram/mac_nvram.h"
 #include "hw/qdev-properties.h"
 #include "migration/vmstate.h"
 #include "qemu/cutils.h"
diff --git a/hw/pci-host/grackle.c b/hw/pci-host/grackle.c
index 5282123004..d9c11d22e0 100644
--- a/hw/pci-host/grackle.c
+++ b/hw/pci-host/grackle.c
@@ -25,7 +25,6 @@
 
 #include "qemu/osdep.h"
 #include "hw/pci/pci_host.h"
-#include "hw/ppc/mac.h"
 #include "hw/qdev-properties.h"
 #include "hw/pci/pci.h"
 #include "hw/irq.h"
diff --git a/hw/pci-host/uninorth.c b/hw/pci-host/uninorth.c
index d25b62d6a5..aebd44d265 100644
--- a/hw/pci-host/uninorth.c
+++ b/hw/pci-host/uninorth.c
@@ -24,7 +24,6 @@
 
 #include "qemu/osdep.h"
 #include "hw/irq.h"
-#include "hw/ppc/mac.h"
 #include "hw/qdev-properties.h"
 #include "qemu/module.h"
 #include "hw/pci/pci.h"
diff --git a/hw/ppc/mac_newworld.c b/hw/ppc/mac_newworld.c
index 94a8b72fb1..c0b2173cbd 100644
--- a/hw/ppc/mac_newworld.c
+++ b/hw/ppc/mac_newworld.c
@@ -52,7 +52,7 @@
 #include "qapi/error.h"
 #include "hw/ppc/ppc.h"
 #include 

[PATCH v2 09/13] hw/ppc/mac.h: Move macio specific parts out from shared header

2022-09-25 Thread BALATON Zoltan
Move the parts specific to and only used by macio out from the shared
mac.h into macio.c where they better belong.

Signed-off-by: BALATON Zoltan 
Reviewed-by: Philippe Mathieu-Daudé 
---
 hw/misc/macio/macio.c |  5 +++--
 hw/ppc/mac.h  | 23 ---
 include/hw/misc/macio/macio.h | 21 +
 3 files changed, 24 insertions(+), 25 deletions(-)

diff --git a/hw/misc/macio/macio.c b/hw/misc/macio/macio.c
index c1fad43f6c..f9f0758b03 100644
--- a/hw/misc/macio/macio.c
+++ b/hw/misc/macio/macio.c
@@ -37,8 +37,9 @@
 #include "hw/intc/heathrow_pic.h"
 #include "trace.h"
 
-/* Note: this code is strongly inspirated from the corresponding code
- * in PearPC */
+#define ESCC_CLOCK 3686400
+
+/* Note: this code is strongly inspired by the corresponding code in PearPC */
 
 /*
  * The mac-io has two interfaces to the ESCC. One is called "escc-legacy",
diff --git a/hw/ppc/mac.h b/hw/ppc/mac.h
index e97087c7e7..55cb02c990 100644
--- a/hw/ppc/mac.h
+++ b/hw/ppc/mac.h
@@ -35,29 +35,6 @@
 #define KERNEL_LOAD_ADDR 0x0100
 #define KERNEL_GAP   0x0010
 
-#define ESCC_CLOCK 3686400
-
-/* Old World IRQs */
-#define OLDWORLD_CUDA_IRQ  0x12
-#define OLDWORLD_ESCCB_IRQ 0x10
-#define OLDWORLD_ESCCA_IRQ 0xf
-#define OLDWORLD_IDE0_IRQ  0xd
-#define OLDWORLD_IDE0_DMA_IRQ  0x2
-#define OLDWORLD_IDE1_IRQ  0xe
-#define OLDWORLD_IDE1_DMA_IRQ  0x3
-
-/* New World IRQs */
-#define NEWWORLD_CUDA_IRQ  0x19
-#define NEWWORLD_PMU_IRQ   0x19
-#define NEWWORLD_ESCCB_IRQ 0x24
-#define NEWWORLD_ESCCA_IRQ 0x25
-#define NEWWORLD_IDE0_IRQ  0xd
-#define NEWWORLD_IDE0_DMA_IRQ  0x2
-#define NEWWORLD_IDE1_IRQ  0xe
-#define NEWWORLD_IDE1_DMA_IRQ  0x3
-#define NEWWORLD_EXTING_GPIO1  0x2f
-#define NEWWORLD_EXTING_GPIO9  0x37
-
 /* Grackle PCI */
 #define TYPE_GRACKLE_PCI_HOST_BRIDGE "grackle-pcihost"
 
diff --git a/include/hw/misc/macio/macio.h b/include/hw/misc/macio/macio.h
index 6c05f3bfd2..26cf15b1ce 100644
--- a/include/hw/misc/macio/macio.h
+++ b/include/hw/misc/macio/macio.h
@@ -38,6 +38,27 @@
 #include "hw/ppc/openpic.h"
 #include "qom/object.h"
 
+/* Old World IRQs */
+#define OLDWORLD_CUDA_IRQ  0x12
+#define OLDWORLD_ESCCB_IRQ 0x10
+#define OLDWORLD_ESCCA_IRQ 0xf
+#define OLDWORLD_IDE0_IRQ  0xd
+#define OLDWORLD_IDE0_DMA_IRQ  0x2
+#define OLDWORLD_IDE1_IRQ  0xe
+#define OLDWORLD_IDE1_DMA_IRQ  0x3
+
+/* New World IRQs */
+#define NEWWORLD_CUDA_IRQ  0x19
+#define NEWWORLD_PMU_IRQ   0x19
+#define NEWWORLD_ESCCB_IRQ 0x24
+#define NEWWORLD_ESCCA_IRQ 0x25
+#define NEWWORLD_IDE0_IRQ  0xd
+#define NEWWORLD_IDE0_DMA_IRQ  0x2
+#define NEWWORLD_IDE1_IRQ  0xe
+#define NEWWORLD_IDE1_DMA_IRQ  0x3
+#define NEWWORLD_EXTING_GPIO1  0x2f
+#define NEWWORLD_EXTING_GPIO9  0x37
+
 /* MacIO virtual bus */
 #define TYPE_MACIO_BUS "macio-bus"
 OBJECT_DECLARE_SIMPLE_TYPE(MacIOBusState, MACIO_BUS)
-- 
2.30.4




[PATCH v2 06/13] mac_newworld: Simplify creation of Uninorth devices

2022-09-25 Thread BALATON Zoltan
Avoid open coding sysbus_mmio_map() and map regions in ascending
otder. Reorganise code a bit to avoid some casts.

Signed-off-by: BALATON Zoltan 
---
 hw/ppc/mac_newworld.c | 42 +-
 1 file changed, 17 insertions(+), 25 deletions(-)

diff --git a/hw/ppc/mac_newworld.c b/hw/ppc/mac_newworld.c
index 6bc3bd19be..b4ad43cc05 100644
--- a/hw/ppc/mac_newworld.c
+++ b/hw/ppc/mac_newworld.c
@@ -228,13 +228,6 @@ static void ppc_core99_init(MachineState *machine)
 }
 }
 
-/* UniN init */
-dev = qdev_new(TYPE_UNI_NORTH);
-s = SYS_BUS_DEVICE(dev);
-sysbus_realize_and_unref(s, _fatal);
-memory_region_add_subregion(get_system_memory(), 0xf800,
-sysbus_mmio_get_region(s, 0));
-
 openpic_irqs = g_new0(IrqLines, machine->smp.cpus);
 for (i = 0; i < machine->smp.cpus; i++) {
 /* Mac99 IRQ connection between OpenPIC outputs pins
@@ -275,24 +268,27 @@ static void ppc_core99_init(MachineState *machine)
 }
 }
 
+/* UniN init */
+s = SYS_BUS_DEVICE(qdev_new(TYPE_UNI_NORTH));
+sysbus_realize_and_unref(s, _fatal);
+sysbus_mmio_map(s, 0, 0xf800);
+
 if (PPC_INPUT(env) == PPC_FLAGS_INPUT_970) {
+machine_arch = ARCH_MAC99_U3;
 /* 970 gets a U3 bus */
 /* Uninorth AGP bus */
 dev = qdev_new(TYPE_U3_AGP_HOST_BRIDGE);
-sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), _fatal);
 uninorth_pci = U3_AGP_HOST_BRIDGE(dev);
 s = SYS_BUS_DEVICE(dev);
-/* PCI hole */
-memory_region_add_subregion(get_system_memory(), 0x8000ULL,
-sysbus_mmio_get_region(s, 2));
-/* Register 8 MB of ISA IO space */
-memory_region_add_subregion(get_system_memory(), 0xf200,
-sysbus_mmio_get_region(s, 3));
+sysbus_realize_and_unref(s, _fatal);
 sysbus_mmio_map(s, 0, 0xf080);
 sysbus_mmio_map(s, 1, 0xf0c0);
-
-machine_arch = ARCH_MAC99_U3;
+/* PCI hole */
+sysbus_mmio_map(s, 2, 0x8000);
+/* Register 8 MB of ISA IO space */
+sysbus_mmio_map(s, 3, 0xf200);
 } else {
+machine_arch = ARCH_MAC99;
 /* Use values found on a real PowerMac */
 /* Uninorth AGP bus */
 uninorth_agp_dev = qdev_new(TYPE_UNI_NORTH_AGP_HOST_BRIDGE);
@@ -312,19 +308,15 @@ static void ppc_core99_init(MachineState *machine)
 /* Uninorth main bus */
 dev = qdev_new(TYPE_UNI_NORTH_PCI_HOST_BRIDGE);
 qdev_prop_set_uint32(dev, "ofw-addr", 0xf200);
-sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), _fatal);
 uninorth_pci = UNI_NORTH_PCI_HOST_BRIDGE(dev);
 s = SYS_BUS_DEVICE(dev);
-/* PCI hole */
-memory_region_add_subregion(get_system_memory(), 0x8000ULL,
-sysbus_mmio_get_region(s, 2));
-/* Register 8 MB of ISA IO space */
-memory_region_add_subregion(get_system_memory(), 0xf200,
-sysbus_mmio_get_region(s, 3));
+sysbus_realize_and_unref(s, _fatal);
 sysbus_mmio_map(s, 0, 0xf280);
 sysbus_mmio_map(s, 1, 0xf2c0);
-
-machine_arch = ARCH_MAC99;
+/* PCI hole */
+sysbus_mmio_map(s, 2, 0x8000);
+/* Register 8 MB of ISA IO space */
+sysbus_mmio_map(s, 3, 0xf200);
 }
 
 machine->usb |= defaults_enabled() && !machine->usb_disabled;
-- 
2.30.4




[PATCH v2 08/13] hw/ppc/mac.h: Move newworld specific parts out from shared header

2022-09-25 Thread BALATON Zoltan
Move the parts specific to and only used by mac99 out from the shared
mac.h into mac_newworld.c where they better belong.

Signed-off-by: BALATON Zoltan 
Reviewed-by: Mark Cave-Ayland 
---
 hw/ppc/mac.h  | 24 
 hw/ppc/mac_newworld.c | 19 +++
 hw/ppc/mac_oldworld.c |  1 +
 3 files changed, 20 insertions(+), 24 deletions(-)

diff --git a/hw/ppc/mac.h b/hw/ppc/mac.h
index a1fa8f8e41..e97087c7e7 100644
--- a/hw/ppc/mac.h
+++ b/hw/ppc/mac.h
@@ -26,15 +26,8 @@
 #ifndef PPC_MAC_H
 #define PPC_MAC_H
 
-#include "qemu/units.h"
 #include "exec/memory.h"
-#include "hw/boards.h"
 #include "hw/sysbus.h"
-#include "hw/input/adb.h"
-#include "hw/misc/mos6522.h"
-#include "hw/pci/pci_host.h"
-#include "hw/pci-host/uninorth.h"
-#include "qom/object.h"
 
 #define NVRAM_SIZE0x2000
 #define PROM_FILENAME"openbios-ppc"
@@ -65,23 +58,6 @@
 #define NEWWORLD_EXTING_GPIO1  0x2f
 #define NEWWORLD_EXTING_GPIO9  0x37
 
-/* Core99 machine */
-#define TYPE_CORE99_MACHINE MACHINE_TYPE_NAME("mac99")
-typedef struct Core99MachineState Core99MachineState;
-DECLARE_INSTANCE_CHECKER(Core99MachineState, CORE99_MACHINE,
- TYPE_CORE99_MACHINE)
-
-#define CORE99_VIA_CONFIG_CUDA 0x0
-#define CORE99_VIA_CONFIG_PMU  0x1
-#define CORE99_VIA_CONFIG_PMU_ADB  0x2
-
-struct Core99MachineState {
-/*< private >*/
-MachineState parent;
-
-uint8_t via_config;
-};
-
 /* Grackle PCI */
 #define TYPE_GRACKLE_PCI_HOST_BRIDGE "grackle-pcihost"
 
diff --git a/hw/ppc/mac_newworld.c b/hw/ppc/mac_newworld.c
index 2acd103dd3..32ab730600 100644
--- a/hw/ppc/mac_newworld.c
+++ b/hw/ppc/mac_newworld.c
@@ -48,10 +48,13 @@
 
 #include "qemu/osdep.h"
 #include "qemu/datadir.h"
+#include "qemu/units.h"
 #include "qapi/error.h"
 #include "hw/ppc/ppc.h"
 #include "hw/qdev-properties.h"
 #include "hw/ppc/mac.h"
+#include "hw/boards.h"
+#include "hw/pci-host/uninorth.h"
 #include "hw/input/adb.h"
 #include "hw/ppc/mac_dbdma.h"
 #include "hw/pci/pci.h"
@@ -83,6 +86,22 @@
 #define PROM_BASE 0xfff0
 #define PROM_SIZE (1 * MiB)
 
+#define TYPE_CORE99_MACHINE MACHINE_TYPE_NAME("mac99")
+typedef struct Core99MachineState Core99MachineState;
+DECLARE_INSTANCE_CHECKER(Core99MachineState, CORE99_MACHINE,
+ TYPE_CORE99_MACHINE)
+
+#define CORE99_VIA_CONFIG_CUDA 0x0
+#define CORE99_VIA_CONFIG_PMU  0x1
+#define CORE99_VIA_CONFIG_PMU_ADB  0x2
+
+struct Core99MachineState {
+/*< private >*/
+MachineState parent;
+
+uint8_t via_config;
+};
+
 static void fw_cfg_boot_set(void *opaque, const char *boot_device,
 Error **errp)
 {
diff --git a/hw/ppc/mac_oldworld.c b/hw/ppc/mac_oldworld.c
index 9d651cf482..1fa7b770b7 100644
--- a/hw/ppc/mac_oldworld.c
+++ b/hw/ppc/mac_oldworld.c
@@ -31,6 +31,7 @@
 #include "hw/ppc/ppc.h"
 #include "hw/qdev-properties.h"
 #include "mac.h"
+#include "hw/boards.h"
 #include "hw/input/adb.h"
 #include "sysemu/sysemu.h"
 #include "net/net.h"
-- 
2.30.4




Re: [PATCH v3 38/54] tests/qtest: bios-tables-test: Adapt the case for win32

2022-09-25 Thread Michael S. Tsirkin
On Sun, Sep 25, 2022 at 07:30:16PM +0800, Bin Meng wrote:
> From: Bin Meng 
> 
> Single quotes in the arguments (oem_id='CRASH ') are not removed in
> the Windows environment before it is passed to the QEMU executable.
> The space in the argument causes the "-acpitable" option parser to
> think that all of its parameters are done, hence it complains:
> 
>   '-acpitable' requires one of 'data' or 'file'
> 
> Change to use double quotes which works fine on all platforms.
> 
> Also /dev/null does not work on win32, and nul should be used.
> 
> Signed-off-by: Bin Meng 
> Reviewed-by: Marc-André Lureau 

Acked-by: Michael S. Tsirkin 

Feel free to merge with rest of the patchset.

> ---
> 
> (no changes since v1)
> 
>  tests/qtest/bios-tables-test.c | 12 +---
>  1 file changed, 9 insertions(+), 3 deletions(-)
> 
> diff --git a/tests/qtest/bios-tables-test.c b/tests/qtest/bios-tables-test.c
> index 46a46fe0aa..2ebeb530b2 100644
> --- a/tests/qtest/bios-tables-test.c
> +++ b/tests/qtest/bios-tables-test.c
> @@ -1615,6 +1615,12 @@ static void test_acpi_virt_viot(void)
>  free_test_data();
>  }
>  
> +#ifndef _WIN32
> +# define DEV_NULL "/dev/null"
> +#else
> +# define DEV_NULL "nul"
> +#endif
> +
>  static void test_acpi_q35_slic(void)
>  {
>  test_data data = {
> @@ -1622,9 +1628,9 @@ static void test_acpi_q35_slic(void)
>  .variant = ".slic",
>  };
>  
> -test_acpi_one("-acpitable sig=SLIC,oem_id='CRASH ',oem_table_id='ME',"
> -  "oem_rev=2210,asl_compiler_id='qemu',"
> -  "asl_compiler_rev=,data=/dev/null",
> +test_acpi_one("-acpitable sig=SLIC,oem_id=\"CRASH \",oem_table_id=ME,"
> +  "oem_rev=2210,asl_compiler_id=qemu,"
> +  "asl_compiler_rev=,data=" DEV_NULL,
>);
>  free_test_data();
>  }
> -- 
> 2.34.1




[PATCH v2 13/13] mac_nvram: Use NVRAM_SIZE constant

2022-09-25 Thread BALATON Zoltan
The NVRAM_SIZE constant was defined but not used. Rename it to
MACIO_NVRAM_SIZE to match the device model and use it where appropriate.

Signed-off-by: BALATON Zoltan 
---
 hw/misc/macio/macio.c| 2 +-
 hw/ppc/mac_newworld.c| 4 ++--
 include/hw/nvram/mac_nvram.h | 3 +--
 3 files changed, 4 insertions(+), 5 deletions(-)

diff --git a/hw/misc/macio/macio.c b/hw/misc/macio/macio.c
index 93a7c7bbc8..08dbdd7fc0 100644
--- a/hw/misc/macio/macio.c
+++ b/hw/misc/macio/macio.c
@@ -226,7 +226,7 @@ static void macio_oldworld_init(Object *obj)
 
 object_initialize_child(OBJECT(s), "nvram", >nvram, TYPE_MACIO_NVRAM);
 dev = DEVICE(>nvram);
-qdev_prop_set_uint32(dev, "size", 0x2000);
+qdev_prop_set_uint32(dev, "size", MACIO_NVRAM_SIZE);
 qdev_prop_set_uint32(dev, "it_shift", 4);
 
 for (i = 0; i < 2; i++) {
diff --git a/hw/ppc/mac_newworld.c b/hw/ppc/mac_newworld.c
index c0b2173cbd..37fb7845f1 100644
--- a/hw/ppc/mac_newworld.c
+++ b/hw/ppc/mac_newworld.c
@@ -445,12 +445,12 @@ static void ppc_core99_init(MachineState *machine)
 nvram_addr = 0xFFE0;
 }
 dev = qdev_new(TYPE_MACIO_NVRAM);
-qdev_prop_set_uint32(dev, "size", 0x2000);
+qdev_prop_set_uint32(dev, "size", MACIO_NVRAM_SIZE);
 qdev_prop_set_uint32(dev, "it_shift", 1);
 sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), _fatal);
 sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, nvram_addr);
 nvr = MACIO_NVRAM(dev);
-pmac_format_nvram_partition(nvr, 0x2000);
+pmac_format_nvram_partition(nvr, MACIO_NVRAM_SIZE);
 /* No PCI init: the BIOS will do it */
 
 dev = qdev_new(TYPE_FW_CFG_MEM);
diff --git a/include/hw/nvram/mac_nvram.h b/include/hw/nvram/mac_nvram.h
index baa9f6a5a6..b780aca470 100644
--- a/include/hw/nvram/mac_nvram.h
+++ b/include/hw/nvram/mac_nvram.h
@@ -29,9 +29,8 @@
 #include "exec/memory.h"
 #include "hw/sysbus.h"
 
-#define NVRAM_SIZE0x2000
+#define MACIO_NVRAM_SIZE 0x2000
 
-/* Mac NVRAM */
 #define TYPE_MACIO_NVRAM "macio-nvram"
 OBJECT_DECLARE_SIMPLE_TYPE(MacIONVRAMState, MACIO_NVRAM)
 
-- 
2.30.4




[PATCH v2 11/13] hw/ppc/mac.h: Move PROM and KERNEL defines to board code

2022-09-25 Thread BALATON Zoltan
The PROM_FILENAME and KERNEL_* defines are used by mac_oldworld and
mac_newworld but they don't have to be identical so these could be
moved to the individual boards.

Signed-off-by: BALATON Zoltan 
Reviewed-by: Philippe Mathieu-Daudé 
Reviewed-by: Mark Cave-Ayland 
---
 hw/ppc/mac.h  | 4 
 hw/ppc/mac_newworld.c | 4 
 hw/ppc/mac_oldworld.c | 7 ++-
 3 files changed, 10 insertions(+), 5 deletions(-)

diff --git a/hw/ppc/mac.h b/hw/ppc/mac.h
index fe77a6c6db..3e2df262ee 100644
--- a/hw/ppc/mac.h
+++ b/hw/ppc/mac.h
@@ -30,10 +30,6 @@
 #include "hw/sysbus.h"
 
 #define NVRAM_SIZE0x2000
-#define PROM_FILENAME"openbios-ppc"
-
-#define KERNEL_LOAD_ADDR 0x0100
-#define KERNEL_GAP   0x0010
 
 /* Mac NVRAM */
 #define TYPE_MACIO_NVRAM "macio-nvram"
diff --git a/hw/ppc/mac_newworld.c b/hw/ppc/mac_newworld.c
index 32ab730600..94a8b72fb1 100644
--- a/hw/ppc/mac_newworld.c
+++ b/hw/ppc/mac_newworld.c
@@ -83,9 +83,13 @@
 
 #define NDRV_VGA_FILENAME "qemu_vga.ndrv"
 
+#define PROM_FILENAME "openbios-ppc"
 #define PROM_BASE 0xfff0
 #define PROM_SIZE (1 * MiB)
 
+#define KERNEL_LOAD_ADDR 0x0100
+#define KERNEL_GAP   0x0010
+
 #define TYPE_CORE99_MACHINE MACHINE_TYPE_NAME("mac99")
 typedef struct Core99MachineState Core99MachineState;
 DECLARE_INSTANCE_CHECKER(Core99MachineState, CORE99_MACHINE,
diff --git a/hw/ppc/mac_oldworld.c b/hw/ppc/mac_oldworld.c
index 1355d032ff..cfdee21cd2 100644
--- a/hw/ppc/mac_oldworld.c
+++ b/hw/ppc/mac_oldworld.c
@@ -57,10 +57,15 @@
 
 #define NDRV_VGA_FILENAME "qemu_vga.ndrv"
 
-#define GRACKLE_BASE 0xfec0
+#define PROM_FILENAME "openbios-ppc"
 #define PROM_BASE 0xffc0
 #define PROM_SIZE (4 * MiB)
 
+#define KERNEL_LOAD_ADDR 0x0100
+#define KERNEL_GAP   0x0010
+
+#define GRACKLE_BASE 0xfec0
+
 static void fw_cfg_boot_set(void *opaque, const char *boot_device,
 Error **errp)
 {
-- 
2.30.4




[PATCH v2 03/13] mac_{old|new}world: Set tbfreq at declaration

2022-09-25 Thread BALATON Zoltan
The tbfreq variable is only set once in an if-else which can be done
at the variable declaration saving some lines of code and making it
simpler.

Signed-off-by: BALATON Zoltan 
---
 hw/ppc/mac_newworld.c | 9 +
 hw/ppc/mac_oldworld.c | 9 +
 2 files changed, 2 insertions(+), 16 deletions(-)

diff --git a/hw/ppc/mac_newworld.c b/hw/ppc/mac_newworld.c
index 27e4e8d136..6327694f85 100644
--- a/hw/ppc/mac_newworld.c
+++ b/hw/ppc/mac_newworld.c
@@ -130,7 +130,7 @@ static void ppc_core99_init(MachineState *machine)
 DeviceState *dev, *pic_dev;
 DeviceState *uninorth_internal_dev = NULL, *uninorth_agp_dev = NULL;
 hwaddr nvram_addr = 0xFFF04000;
-uint64_t tbfreq;
+uint64_t tbfreq = kvm_enabled() ? kvmppc_get_tbfreq() : TBFREQ;
 
 /* init CPUs */
 for (i = 0; i < machine->smp.cpus; i++) {
@@ -343,13 +343,6 @@ static void ppc_core99_init(MachineState *machine)
 has_adb = (core99_machine->via_config == CORE99_VIA_CONFIG_CUDA ||
core99_machine->via_config == CORE99_VIA_CONFIG_PMU_ADB);
 
-/* Timebase Frequency */
-if (kvm_enabled()) {
-tbfreq = kvmppc_get_tbfreq();
-} else {
-tbfreq = TBFREQ;
-}
-
 /* init basic PC hardware */
 pci_bus = PCI_HOST_BRIDGE(uninorth_pci)->bus;
 
diff --git a/hw/ppc/mac_oldworld.c b/hw/ppc/mac_oldworld.c
index 86512d31ad..5cabc410e7 100644
--- a/hw/ppc/mac_oldworld.c
+++ b/hw/ppc/mac_oldworld.c
@@ -99,7 +99,7 @@ static void ppc_heathrow_init(MachineState *machine)
 uint16_t ppc_boot_device;
 DriveInfo *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
 void *fw_cfg;
-uint64_t tbfreq;
+uint64_t tbfreq = kvm_enabled() ? kvmppc_get_tbfreq() : TBFREQ;
 
 /* init CPUs */
 for (i = 0; i < machine->smp.cpus; i++) {
@@ -223,13 +223,6 @@ static void ppc_heathrow_init(MachineState *machine)
 }
 }
 
-/* Timebase Frequency */
-if (kvm_enabled()) {
-tbfreq = kvmppc_get_tbfreq();
-} else {
-tbfreq = TBFREQ;
-}
-
 /* Grackle PCI host bridge */
 grackle_dev = qdev_new(TYPE_GRACKLE_PCI_HOST_BRIDGE);
 qdev_prop_set_uint32(grackle_dev, "ofw-addr", 0x8000);
-- 
2.30.4




[PATCH v2 10/13] hw/ppc/mac.h: Move grackle-pcihost declaration out from shared header

2022-09-25 Thread BALATON Zoltan
It is only used by mac_oldworld anyway and it already instantiates
a few devices by name so this allows reducing the shared header further.

Signed-off-by: BALATON Zoltan 
---
 hw/pci-host/grackle.c | 1 +
 hw/ppc/mac.h  | 3 ---
 hw/ppc/mac_oldworld.c | 2 +-
 3 files changed, 2 insertions(+), 4 deletions(-)

diff --git a/hw/pci-host/grackle.c b/hw/pci-host/grackle.c
index b05facf463..5282123004 100644
--- a/hw/pci-host/grackle.c
+++ b/hw/pci-host/grackle.c
@@ -34,6 +34,7 @@
 #include "trace.h"
 #include "qom/object.h"
 
+#define TYPE_GRACKLE_PCI_HOST_BRIDGE "grackle-pcihost"
 OBJECT_DECLARE_SIMPLE_TYPE(GrackleState, GRACKLE_PCI_HOST_BRIDGE)
 
 struct GrackleState {
diff --git a/hw/ppc/mac.h b/hw/ppc/mac.h
index 55cb02c990..fe77a6c6db 100644
--- a/hw/ppc/mac.h
+++ b/hw/ppc/mac.h
@@ -35,9 +35,6 @@
 #define KERNEL_LOAD_ADDR 0x0100
 #define KERNEL_GAP   0x0010
 
-/* Grackle PCI */
-#define TYPE_GRACKLE_PCI_HOST_BRIDGE "grackle-pcihost"
-
 /* Mac NVRAM */
 #define TYPE_MACIO_NVRAM "macio-nvram"
 OBJECT_DECLARE_SIMPLE_TYPE(MacIONVRAMState, MACIO_NVRAM)
diff --git a/hw/ppc/mac_oldworld.c b/hw/ppc/mac_oldworld.c
index 1fa7b770b7..1355d032ff 100644
--- a/hw/ppc/mac_oldworld.c
+++ b/hw/ppc/mac_oldworld.c
@@ -214,7 +214,7 @@ static void ppc_heathrow_init(MachineState *machine)
 }
 
 /* Grackle PCI host bridge */
-grackle_dev = qdev_new(TYPE_GRACKLE_PCI_HOST_BRIDGE);
+grackle_dev = qdev_new("grackle-pcihost");
 qdev_prop_set_uint32(grackle_dev, "ofw-addr", 0x8000);
 s = SYS_BUS_DEVICE(grackle_dev);
 sysbus_realize_and_unref(s, _fatal);
-- 
2.30.4




[PATCH v2 01/13] mac_newworld: Drop some variables

2022-09-25 Thread BALATON Zoltan
Values not used frequently enough may not worth putting in a local
variable, especially with names almost as long as the original value
because that does not improve readability, to the contrary it makes it
harder to see what value is used. Drop a few such variables. This is
the same clean up that was done for mac_oldworld in commit b8df32555ce5.

Signed-off-by: BALATON Zoltan 
Reviewed-by: Philippe Mathieu-Daudé 
Reviewed-by: Mark Cave-Ayland 
---
 hw/ppc/mac_newworld.c | 65 +++
 1 file changed, 29 insertions(+), 36 deletions(-)

diff --git a/hw/ppc/mac_newworld.c b/hw/ppc/mac_newworld.c
index cf7eb72391..27e4e8d136 100644
--- a/hw/ppc/mac_newworld.c
+++ b/hw/ppc/mac_newworld.c
@@ -106,18 +106,13 @@ static void ppc_core99_reset(void *opaque)
 /* PowerPC Mac99 hardware initialisation */
 static void ppc_core99_init(MachineState *machine)
 {
-ram_addr_t ram_size = machine->ram_size;
-const char *bios_name = machine->firmware ?: PROM_FILENAME;
-const char *kernel_filename = machine->kernel_filename;
-const char *kernel_cmdline = machine->kernel_cmdline;
-const char *initrd_filename = machine->initrd_filename;
-const char *boot_device = machine->boot_config.order;
 Core99MachineState *core99_machine = CORE99_MACHINE(machine);
 PowerPCCPU *cpu = NULL;
 CPUPPCState *env = NULL;
 char *filename;
 IrqLines *openpic_irqs;
-int linux_boot, i, j, k;
+int i, j, k, ppc_boot_device, machine_arch, bios_size;
+const char *bios_name = machine->firmware ?: PROM_FILENAME;
 MemoryRegion *bios = g_new(MemoryRegion, 1);
 hwaddr kernel_base, initrd_base, cmdline_base = 0;
 long kernel_size, initrd_size;
@@ -129,22 +124,16 @@ static void ppc_core99_init(MachineState *machine)
 MACIOIDEState *macio_ide;
 BusState *adb_bus;
 MacIONVRAMState *nvr;
-int bios_size;
-int ppc_boot_device;
 DriveInfo *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
 void *fw_cfg;
-int machine_arch;
 SysBusDevice *s;
 DeviceState *dev, *pic_dev;
 DeviceState *uninorth_internal_dev = NULL, *uninorth_agp_dev = NULL;
 hwaddr nvram_addr = 0xFFF04000;
 uint64_t tbfreq;
-unsigned int smp_cpus = machine->smp.cpus;
-
-linux_boot = (kernel_filename != NULL);
 
 /* init CPUs */
-for (i = 0; i < smp_cpus; i++) {
+for (i = 0; i < machine->smp.cpus; i++) {
 cpu = POWERPC_CPU(cpu_create(machine->cpu_type));
 env = >env;
 
@@ -184,7 +173,7 @@ static void ppc_core99_init(MachineState *machine)
 exit(1);
 }
 
-if (linux_boot) {
+if (machine->kernel_filename) {
 int bswap_needed;
 
 #ifdef BSWAP_NEEDED
@@ -194,29 +183,31 @@ static void ppc_core99_init(MachineState *machine)
 #endif
 kernel_base = KERNEL_LOAD_ADDR;
 
-kernel_size = load_elf(kernel_filename, NULL,
+kernel_size = load_elf(machine->kernel_filename, NULL,
translate_kernel_address, NULL, NULL, NULL,
NULL, NULL, 1, PPC_ELF_MACHINE, 0, 0);
 if (kernel_size < 0)
-kernel_size = load_aout(kernel_filename, kernel_base,
-ram_size - kernel_base, bswap_needed,
-TARGET_PAGE_SIZE);
+kernel_size = load_aout(machine->kernel_filename, kernel_base,
+machine->ram_size - kernel_base,
+bswap_needed, TARGET_PAGE_SIZE);
 if (kernel_size < 0)
-kernel_size = load_image_targphys(kernel_filename,
+kernel_size = load_image_targphys(machine->kernel_filename,
   kernel_base,
-  ram_size - kernel_base);
+  machine->ram_size - kernel_base);
 if (kernel_size < 0) {
-error_report("could not load kernel '%s'", kernel_filename);
+error_report("could not load kernel '%s'",
+ machine->kernel_filename);
 exit(1);
 }
 /* load initrd */
-if (initrd_filename) {
+if (machine->initrd_filename) {
 initrd_base = TARGET_PAGE_ALIGN(kernel_base + kernel_size + 
KERNEL_GAP);
-initrd_size = load_image_targphys(initrd_filename, initrd_base,
-  ram_size - initrd_base);
+initrd_size = load_image_targphys(machine->initrd_filename,
+  initrd_base,
+  machine->ram_size - initrd_base);
 if (initrd_size < 0) {
 error_report("could not load initial ram disk '%s'",
- initrd_filename);
+ machine->initrd_filename);
 exit(1);
 }
 cmdline_base = 

[PATCH v2 02/13] mac_oldworld: Drop some more variables

2022-09-25 Thread BALATON Zoltan
Drop some more local variables additionally to commit b8df32555ce5 to
match clean ups done to mac_newwold in previous patch.

Signed-off-by: BALATON Zoltan 
Reviewed-by: Philippe Mathieu-Daudé 
Reviewed-by: Mark Cave-Ayland 
---
 hw/ppc/mac_oldworld.c | 43 +--
 1 file changed, 21 insertions(+), 22 deletions(-)

diff --git a/hw/ppc/mac_oldworld.c b/hw/ppc/mac_oldworld.c
index 03732ca7ed..86512d31ad 100644
--- a/hw/ppc/mac_oldworld.c
+++ b/hw/ppc/mac_oldworld.c
@@ -80,14 +80,13 @@ static void ppc_heathrow_reset(void *opaque)
 
 static void ppc_heathrow_init(MachineState *machine)
 {
-ram_addr_t ram_size = machine->ram_size;
 const char *bios_name = machine->firmware ?: PROM_FILENAME;
-const char *boot_device = machine->boot_config.order;
 PowerPCCPU *cpu = NULL;
 CPUPPCState *env = NULL;
 char *filename;
-int i;
+int i, bios_size;
 MemoryRegion *bios = g_new(MemoryRegion, 1);
+uint64_t bios_addr;
 uint32_t kernel_base, initrd_base, cmdline_base = 0;
 int32_t kernel_size, initrd_size;
 PCIBus *pci_bus;
@@ -97,16 +96,13 @@ static void ppc_heathrow_init(MachineState *machine)
 SysBusDevice *s;
 DeviceState *dev, *pic_dev, *grackle_dev;
 BusState *adb_bus;
-uint64_t bios_addr;
-int bios_size;
-unsigned int smp_cpus = machine->smp.cpus;
 uint16_t ppc_boot_device;
 DriveInfo *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
 void *fw_cfg;
 uint64_t tbfreq;
 
 /* init CPUs */
-for (i = 0; i < smp_cpus; i++) {
+for (i = 0; i < machine->smp.cpus; i++) {
 cpu = POWERPC_CPU(cpu_create(machine->cpu_type));
 env = >env;
 
@@ -116,9 +112,9 @@ static void ppc_heathrow_init(MachineState *machine)
 }
 
 /* allocate RAM */
-if (ram_size > 2047 * MiB) {
+if (machine->ram_size > 2047 * MiB) {
 error_report("Too much memory for this machine: %" PRId64 " MB, "
- "maximum 2047 MB", ram_size / MiB);
+ "maximum 2047 MB", machine->ram_size / MiB);
 exit(1);
 }
 
@@ -165,12 +161,12 @@ static void ppc_heathrow_init(MachineState *machine)
NULL, NULL, 1, PPC_ELF_MACHINE, 0, 0);
 if (kernel_size < 0)
 kernel_size = load_aout(machine->kernel_filename, kernel_base,
-ram_size - kernel_base, bswap_needed,
-TARGET_PAGE_SIZE);
+machine->ram_size - kernel_base,
+bswap_needed, TARGET_PAGE_SIZE);
 if (kernel_size < 0)
 kernel_size = load_image_targphys(machine->kernel_filename,
   kernel_base,
-  ram_size - kernel_base);
+  machine->ram_size - kernel_base);
 if (kernel_size < 0) {
 error_report("could not load kernel '%s'",
  machine->kernel_filename);
@@ -182,7 +178,7 @@ static void ppc_heathrow_init(MachineState *machine)
 KERNEL_GAP);
 initrd_size = load_image_targphys(machine->initrd_filename,
   initrd_base,
-  ram_size - initrd_base);
+  machine->ram_size - initrd_base);
 if (initrd_size < 0) {
 error_report("could not load initial ram disk '%s'",
  machine->initrd_filename);
@@ -201,19 +197,22 @@ static void ppc_heathrow_init(MachineState *machine)
 initrd_base = 0;
 initrd_size = 0;
 ppc_boot_device = '\0';
-for (i = 0; boot_device[i] != '\0'; i++) {
-/* TOFIX: for now, the second IDE channel is not properly
+for (i = 0; machine->boot_config.order[i] != '\0'; i++) {
+/*
+ * TOFIX: for now, the second IDE channel is not properly
  *used by OHW. The Mac floppy disk are not emulated.
  *For now, OHW cannot boot from the network.
  */
 #if 0
-if (boot_device[i] >= 'a' && boot_device[i] <= 'f') {
-ppc_boot_device = boot_device[i];
+if (machine->boot_config.order[i] >= 'a' &&
+machine->boot_config.order[i] <= 'f') {
+ppc_boot_device = machine->boot_config.order[i];
 break;
 }
 #else
-if (boot_device[i] >= 'c' && boot_device[i] <= 'd') {
-ppc_boot_device = boot_device[i];
+if (machine->boot_config.order[i] >= 'c' &&
+machine->boot_config.order[i] <= 'd') {
+ppc_boot_device = machine->boot_config.order[i];
 break;
 }
 #endif
@@ -266,7 +265,7 @@ static void 

[PATCH v2 07/13] mac_{old|new}world: Reduce number of QOM casts

2022-09-25 Thread BALATON Zoltan
By storing the device pointers in a variable with the right type the
number of QOM casts can be reduced which also makes the code more
readable.

Signed-off-by: BALATON Zoltan 
Reviewed-by: Philippe Mathieu-Daudé 
Reviewed-by: Mark Cave-Ayland 
---
 hw/ppc/mac_newworld.c | 61 ---
 hw/ppc/mac_oldworld.c | 26 --
 2 files changed, 39 insertions(+), 48 deletions(-)

diff --git a/hw/ppc/mac_newworld.c b/hw/ppc/mac_newworld.c
index b4ad43cc05..2acd103dd3 100644
--- a/hw/ppc/mac_newworld.c
+++ b/hw/ppc/mac_newworld.c
@@ -116,18 +116,16 @@ static void ppc_core99_init(MachineState *machine)
 MemoryRegion *bios = g_new(MemoryRegion, 1);
 hwaddr kernel_base = 0, initrd_base = 0, cmdline_base = 0;
 long kernel_size = 0, initrd_size = 0;
-UNINHostState *uninorth_pci;
 PCIBus *pci_bus;
-PCIDevice *macio;
-ESCCState *escc;
 bool has_pmu, has_adb;
+Object *macio;
 MACIOIDEState *macio_ide;
 BusState *adb_bus;
 MacIONVRAMState *nvr;
 DriveInfo *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
 void *fw_cfg;
 SysBusDevice *s;
-DeviceState *dev, *pic_dev;
+DeviceState *dev, *pic_dev, *uninorth_pci_dev;
 DeviceState *uninorth_internal_dev = NULL, *uninorth_agp_dev = NULL;
 hwaddr nvram_addr = 0xFFF04000;
 uint64_t tbfreq = kvm_enabled() ? kvmppc_get_tbfreq() : TBFREQ;
@@ -229,6 +227,7 @@ static void ppc_core99_init(MachineState *machine)
 }
 
 openpic_irqs = g_new0(IrqLines, machine->smp.cpus);
+dev = DEVICE(cpu);
 for (i = 0; i < machine->smp.cpus; i++) {
 /* Mac99 IRQ connection between OpenPIC outputs pins
  * and PowerPC input pins
@@ -236,30 +235,30 @@ static void ppc_core99_init(MachineState *machine)
 switch (PPC_INPUT(env)) {
 case PPC_FLAGS_INPUT_6xx:
 openpic_irqs[i].irq[OPENPIC_OUTPUT_INT] =
-qdev_get_gpio_in(DEVICE(cpu), PPC6xx_INPUT_INT);
+qdev_get_gpio_in(dev, PPC6xx_INPUT_INT);
 openpic_irqs[i].irq[OPENPIC_OUTPUT_CINT] =
- qdev_get_gpio_in(DEVICE(cpu), PPC6xx_INPUT_INT);
+ qdev_get_gpio_in(dev, PPC6xx_INPUT_INT);
 openpic_irqs[i].irq[OPENPIC_OUTPUT_MCK] =
-qdev_get_gpio_in(DEVICE(cpu), PPC6xx_INPUT_MCP);
+qdev_get_gpio_in(dev, PPC6xx_INPUT_MCP);
 /* Not connected ? */
 openpic_irqs[i].irq[OPENPIC_OUTPUT_DEBUG] = NULL;
 /* Check this */
 openpic_irqs[i].irq[OPENPIC_OUTPUT_RESET] =
-qdev_get_gpio_in(DEVICE(cpu), PPC6xx_INPUT_HRESET);
+qdev_get_gpio_in(dev, PPC6xx_INPUT_HRESET);
 break;
 #if defined(TARGET_PPC64)
 case PPC_FLAGS_INPUT_970:
 openpic_irqs[i].irq[OPENPIC_OUTPUT_INT] =
-qdev_get_gpio_in(DEVICE(cpu), PPC970_INPUT_INT);
+qdev_get_gpio_in(dev, PPC970_INPUT_INT);
 openpic_irqs[i].irq[OPENPIC_OUTPUT_CINT] =
-qdev_get_gpio_in(DEVICE(cpu), PPC970_INPUT_INT);
+qdev_get_gpio_in(dev, PPC970_INPUT_INT);
 openpic_irqs[i].irq[OPENPIC_OUTPUT_MCK] =
-qdev_get_gpio_in(DEVICE(cpu), PPC970_INPUT_MCP);
+qdev_get_gpio_in(dev, PPC970_INPUT_MCP);
 /* Not connected ? */
 openpic_irqs[i].irq[OPENPIC_OUTPUT_DEBUG] = NULL;
 /* Check this */
 openpic_irqs[i].irq[OPENPIC_OUTPUT_RESET] =
-qdev_get_gpio_in(DEVICE(cpu), PPC970_INPUT_HRESET);
+qdev_get_gpio_in(dev, PPC970_INPUT_HRESET);
 break;
 #endif /* defined(TARGET_PPC64) */
 default:
@@ -277,9 +276,8 @@ static void ppc_core99_init(MachineState *machine)
 machine_arch = ARCH_MAC99_U3;
 /* 970 gets a U3 bus */
 /* Uninorth AGP bus */
-dev = qdev_new(TYPE_U3_AGP_HOST_BRIDGE);
-uninorth_pci = U3_AGP_HOST_BRIDGE(dev);
-s = SYS_BUS_DEVICE(dev);
+uninorth_pci_dev = qdev_new(TYPE_U3_AGP_HOST_BRIDGE);
+s = SYS_BUS_DEVICE(uninorth_pci_dev);
 sysbus_realize_and_unref(s, _fatal);
 sysbus_mmio_map(s, 0, 0xf080);
 sysbus_mmio_map(s, 1, 0xf0c0);
@@ -306,10 +304,9 @@ static void ppc_core99_init(MachineState *machine)
 sysbus_mmio_map(s, 1, 0xf4c0);
 
 /* Uninorth main bus */
-dev = qdev_new(TYPE_UNI_NORTH_PCI_HOST_BRIDGE);
-qdev_prop_set_uint32(dev, "ofw-addr", 0xf200);
-uninorth_pci = UNI_NORTH_PCI_HOST_BRIDGE(dev);
-s = SYS_BUS_DEVICE(dev);
+uninorth_pci_dev = qdev_new(TYPE_UNI_NORTH_PCI_HOST_BRIDGE);
+qdev_prop_set_uint32(uninorth_pci_dev, "ofw-addr", 0xf200);
+s = SYS_BUS_DEVICE(uninorth_pci_dev);
 sysbus_realize_and_unref(s, _fatal);
 sysbus_mmio_map(s, 0, 0xf280);
 sysbus_mmio_map(s, 1, 0xf2c0);
@@ -325,24 +322,24 @@ static void ppc_core99_init(MachineState 

[PATCH v2 00/13] Misc ppc/mac machines clean up

2022-09-25 Thread BALATON Zoltan
This series includes some clean ups to mac_newworld and mac_oldworld
to make them a bit simpler and more readable, It also removes the
shared mac.h file that turns out was more of a random collection of
unrelated things. Getting rid of this mac.h improves the locality of
device models and reduces unnecessary interdependency.

v2: Split some patches and add a few more I've noticed now and address
review comments

BALATON Zoltan (13):
  mac_newworld: Drop some variables
  mac_oldworld: Drop some more variables
  mac_{old|new}world: Set tbfreq at declaration
  mac_{old|new}world: Avoid else branch by setting default value
  mac_oldworld: Do not open code sysbus_mmio_map()
  mac_newworld: Simplify creation of Uninorth devices
  mac_{old|new}world: Reduce number of QOM casts
  hw/ppc/mac.h: Move newworld specific parts out from shared header
  hw/ppc/mac.h: Move macio specific parts out from shared header
  hw/ppc/mac.h: Move grackle-pcihost declaration out from shared header
  hw/ppc/mac.h: Move PROM and KERNEL defines to board code
  hw/ppc/mac.h: Rename to include/hw/nvram/mac_nvram.h
  mac_nvram: Use NVRAM_SIZE constant

 MAINTAINERS   |   1 +
 hw/ide/macio.c|   1 -
 hw/intc/heathrow_pic.c|   1 -
 hw/intc/openpic.c |   1 -
 hw/misc/macio/cuda.c  |   1 -
 hw/misc/macio/gpio.c  |   1 -
 hw/misc/macio/macio.c |   8 +-
 hw/misc/macio/pmu.c   |   1 -
 hw/nvram/mac_nvram.c  |   2 +-
 hw/pci-host/grackle.c |   2 +-
 hw/pci-host/uninorth.c|   1 -
 hw/ppc/mac.h  | 105 
 hw/ppc/mac_newworld.c | 223 --
 hw/ppc/mac_oldworld.c | 113 +++--
 include/hw/misc/macio/macio.h |  23 +++-
 include/hw/nvram/mac_nvram.h  |  51 
 16 files changed, 230 insertions(+), 305 deletions(-)
 delete mode 100644 hw/ppc/mac.h
 create mode 100644 include/hw/nvram/mac_nvram.h

-- 
2.30.4




[PATCH v3 45/54] tests/qtest: libqtest: Replace the call to close a socket with closesocket()

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

close() is a *nix function. It works on any file descriptor, and
sockets in *nix are an example of a file descriptor.

closesocket() is a Windows-specific function, which works only
specifically with sockets. Sockets on Windows do not use *nix-style
file descriptors, and socket() returns a handle to a kernel object
instead, so it must be closed with closesocket().

In QEMU there is already a logic to handle such platform difference
in os-posix.h and os-win32.h, that:

  * closesocket maps to close on POSIX
  * closesocket maps to a wrapper that calls the real closesocket()
on Windows

Replace the call to close a socket with closesocket() instead.

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

(no changes since v1)

 tests/qtest/libqtest.c | 8 
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/tests/qtest/libqtest.c b/tests/qtest/libqtest.c
index 12b1e85b51..1ae58624d9 100644
--- a/tests/qtest/libqtest.c
+++ b/tests/qtest/libqtest.c
@@ -113,7 +113,7 @@ static int socket_accept(int sock)
(void *), sizeof(timeout))) {
 fprintf(stderr, "%s failed to set SO_RCVTIMEO: %s\n",
 __func__, strerror(errno));
-close(sock);
+closesocket(sock);
 return -1;
 }
 
@@ -124,7 +124,7 @@ static int socket_accept(int sock)
 if (ret == -1) {
 fprintf(stderr, "%s failed: %s\n", __func__, strerror(errno));
 }
-close(sock);
+closesocket(sock);
 
 return ret;
 }
@@ -507,8 +507,8 @@ void qtest_quit(QTestState *s)
 qtest_remove_abrt_handler(s);
 
 qtest_kill_qemu(s);
-close(s->fd);
-close(s->qmp_fd);
+closesocket(s->fd);
+closesocket(s->qmp_fd);
 g_string_free(s->rx, true);
 
 for (GList *it = s->pending_events; it != NULL; it = it->next) {
-- 
2.34.1




[PATCH v2 05/13] mac_oldworld: Do not open code sysbus_mmio_map()

2022-09-25 Thread BALATON Zoltan
Signed-off-by: BALATON Zoltan 
---
 hw/ppc/mac_oldworld.c | 8 ++--
 1 file changed, 2 insertions(+), 6 deletions(-)

diff --git a/hw/ppc/mac_oldworld.c b/hw/ppc/mac_oldworld.c
index cb67e44081..75fbd2a7df 100644
--- a/hw/ppc/mac_oldworld.c
+++ b/hw/ppc/mac_oldworld.c
@@ -218,16 +218,12 @@ static void ppc_heathrow_init(MachineState *machine)
 qdev_prop_set_uint32(grackle_dev, "ofw-addr", 0x8000);
 s = SYS_BUS_DEVICE(grackle_dev);
 sysbus_realize_and_unref(s, _fatal);
-
 sysbus_mmio_map(s, 0, GRACKLE_BASE);
 sysbus_mmio_map(s, 1, GRACKLE_BASE + 0x20);
 /* PCI hole */
-memory_region_add_subregion(get_system_memory(), 0x8000ULL,
-sysbus_mmio_get_region(s, 2));
+sysbus_mmio_map(s, 2, 0x8000);
 /* Register 2 MB of ISA IO space */
-memory_region_add_subregion(get_system_memory(), 0xfe00,
-sysbus_mmio_get_region(s, 3));
-
+sysbus_mmio_map(s, 3, 0xfe00);
 pci_bus = PCI_HOST_BRIDGE(grackle_dev)->bus;
 
 /* MacIO */
-- 
2.30.4




[PATCH v3 54/54] docs/devel: testing: Document writing portable test cases

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

Update the best practices of how to write portable test cases that
can be built and run successfully on both Linux and Windows hosts.

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

(no changes since v3)

Changes in v2:
- 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"

 docs/devel/testing.rst | 30 ++
 1 file changed, 30 insertions(+)

diff --git a/docs/devel/testing.rst b/docs/devel/testing.rst
index aea5b42356..0eae9c4654 100644
--- a/docs/devel/testing.rst
+++ b/docs/devel/testing.rst
@@ -115,6 +115,36 @@ check-block
 are in the "auto" group).
 See the "QEMU iotests" section below for more information.
 
+Writing portable test cases
+~~~
+Both unit tests and qtests can run on POSIX hosts as well as a Windows host.
+Care must be taken when writing portable test cases that can be built and run
+successfully on various hosts. The following are some best practices:
+
+* Use portable APIs from glib whenever necessary, e.g.: g_setenv(),
+  g_mkdtemp(), g_mkdir().
+* Avoid using hardcoded /tmp for temporary file directory.
+  Use g_get_tmp_dir() instead.
+* Bear in mind that Windows has different special string representation for
+  stdin/stdout/stderr and null devices. For example if your test case uses
+  "/dev/fd/2" and "/dev/null" on Linux, remember to use "2" and "nul" on
+  Windows instead. Also IO redirection does not work on Windows, so avoid
+  using "2>nul" whenever necessary.
+* If your test cases uses the blkdebug feature, use relative path to pass
+  the config and image file paths in the command line as Windows absolute
+  path contains the delimeter ":" which will confuse the blkdebug parser.
+* Use double quotes in your extra QEMU commmand line in your test cases
+  instead of single quotes, as Windows does not drop single quotes when
+  passing the command line to QEMU.
+* Windows opens a file in text mode by default, while a POSIX compliant
+  implementation treats text files and binary files the same. So if your
+  test cases opens a file to write some data and later wants to compare the
+  written data with the original one, be sure to pass the letter 'b' as
+  part of the mode string to fopen(), or O_BINARY flag for the open() call.
+* If a certain test case can only run on POSIX or Linux hosts, use a proper
+  #ifdef in the codes. If the whole test suite cannot run on Windows, disable
+  the build in the meson.build file.
+
 QEMU iotests
 
 
-- 
2.34.1




[PATCH v3 44/54] tests/qtest: microbit-test: Fix socket access for win32

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

Sockets on Windows do not use *nix-style file descriptors, so
write()/read()/close() do not work on Windows.

Switch over to use send()/recv()/closesocket() which work with
sockets on all platforms.

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

(no changes since v1)

 tests/qtest/microbit-test.c | 10 +-
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/tests/qtest/microbit-test.c b/tests/qtest/microbit-test.c
index b71daae9a9..4bc267020b 100644
--- a/tests/qtest/microbit-test.c
+++ b/tests/qtest/microbit-test.c
@@ -51,7 +51,7 @@ static void uart_rw_to_rxd(QTestState *qts, int sock_fd, 
const char *in,
 {
 int i, in_len = strlen(in);
 
-g_assert_true(write(sock_fd, in, in_len) == in_len);
+g_assert_true(send(sock_fd, in, in_len, 0) == in_len);
 for (i = 0; i < in_len; i++) {
 g_assert_true(uart_wait_for_event(qts, NRF51_UART_BASE +
A_UART_RXDRDY));
@@ -77,7 +77,7 @@ static void test_nrf51_uart(void)
 char s[10];
 QTestState *qts = qtest_init_with_serial("-M microbit", _fd);
 
-g_assert_true(write(sock_fd, "c", 1) == 1);
+g_assert_true(send(sock_fd, "c", 1, 0) == 1);
 g_assert_cmphex(qtest_readl(qts, NRF51_UART_BASE + A_UART_RXD), ==, 0x00);
 
 qtest_writel(qts, NRF51_UART_BASE + A_UART_ENABLE, 0x04);
@@ -97,17 +97,17 @@ static void test_nrf51_uart(void)
 
 qtest_writel(qts, NRF51_UART_BASE + A_UART_STARTTX, 0x01);
 uart_w_to_txd(qts, "d");
-g_assert_true(read(sock_fd, s, 10) == 1);
+g_assert_true(recv(sock_fd, s, 10, 0) == 1);
 g_assert_cmphex(s[0], ==, 'd');
 
 qtest_writel(qts, NRF51_UART_BASE + A_UART_SUSPEND, 0x01);
 qtest_writel(qts, NRF51_UART_BASE + A_UART_TXD, 'h');
 qtest_writel(qts, NRF51_UART_BASE + A_UART_STARTTX, 0x01);
 uart_w_to_txd(qts, "world");
-g_assert_true(read(sock_fd, s, 10) == 5);
+g_assert_true(recv(sock_fd, s, 10, 0) == 5);
 g_assert_true(memcmp(s, "world", 5) == 0);
 
-close(sock_fd);
+closesocket(sock_fd);
 
 qtest_quit(qts);
 }
-- 
2.34.1




[PATCH v2 04/13] mac_{old|new}world: Avoid else branch by setting default value

2022-09-25 Thread BALATON Zoltan
Several variables are set in if-else branches where the else branch
can be removed by setting a default value at the variable declaration
which leads to simlpler code that is easier to follow.

Signed-off-by: BALATON Zoltan 
---
 hw/ppc/mac_newworld.c | 19 ---
 hw/ppc/mac_oldworld.c | 18 --
 2 files changed, 8 insertions(+), 29 deletions(-)

diff --git a/hw/ppc/mac_newworld.c b/hw/ppc/mac_newworld.c
index 6327694f85..6bc3bd19be 100644
--- a/hw/ppc/mac_newworld.c
+++ b/hw/ppc/mac_newworld.c
@@ -111,11 +111,11 @@ static void ppc_core99_init(MachineState *machine)
 CPUPPCState *env = NULL;
 char *filename;
 IrqLines *openpic_irqs;
-int i, j, k, ppc_boot_device, machine_arch, bios_size;
+int i, j, k, ppc_boot_device, machine_arch, bios_size = -1;
 const char *bios_name = machine->firmware ?: PROM_FILENAME;
 MemoryRegion *bios = g_new(MemoryRegion, 1);
-hwaddr kernel_base, initrd_base, cmdline_base = 0;
-long kernel_size, initrd_size;
+hwaddr kernel_base = 0, initrd_base = 0, cmdline_base = 0;
+long kernel_size = 0, initrd_size = 0;
 UNINHostState *uninorth_pci;
 PCIBus *pci_bus;
 PCIDevice *macio;
@@ -165,8 +165,6 @@ static void ppc_core99_init(MachineState *machine)
 bios_size = load_image_targphys(filename, PROM_BASE, PROM_SIZE);
 }
 g_free(filename);
-} else {
-bios_size = -1;
 }
 if (bios_size < 0 || bios_size > PROM_SIZE) {
 error_report("could not load PowerPC bios '%s'", bios_name);
@@ -174,15 +172,12 @@ static void ppc_core99_init(MachineState *machine)
 }
 
 if (machine->kernel_filename) {
-int bswap_needed;
+int bswap_needed = 0;
 
 #ifdef BSWAP_NEEDED
 bswap_needed = 1;
-#else
-bswap_needed = 0;
 #endif
 kernel_base = KERNEL_LOAD_ADDR;
-
 kernel_size = load_elf(machine->kernel_filename, NULL,
translate_kernel_address, NULL, NULL, NULL,
NULL, NULL, 1, PPC_ELF_MACHINE, 0, 0);
@@ -212,16 +207,10 @@ static void ppc_core99_init(MachineState *machine)
 }
 cmdline_base = TARGET_PAGE_ALIGN(initrd_base + initrd_size);
 } else {
-initrd_base = 0;
-initrd_size = 0;
 cmdline_base = TARGET_PAGE_ALIGN(kernel_base + kernel_size + 
KERNEL_GAP);
 }
 ppc_boot_device = 'm';
 } else {
-kernel_base = 0;
-kernel_size = 0;
-initrd_base = 0;
-initrd_size = 0;
 ppc_boot_device = '\0';
 /* We consider that NewWorld PowerMac never have any floppy drive
  * For now, OHW cannot boot from the network.
diff --git a/hw/ppc/mac_oldworld.c b/hw/ppc/mac_oldworld.c
index 5cabc410e7..cb67e44081 100644
--- a/hw/ppc/mac_oldworld.c
+++ b/hw/ppc/mac_oldworld.c
@@ -84,11 +84,11 @@ static void ppc_heathrow_init(MachineState *machine)
 PowerPCCPU *cpu = NULL;
 CPUPPCState *env = NULL;
 char *filename;
-int i, bios_size;
+int i, bios_size = -1;
 MemoryRegion *bios = g_new(MemoryRegion, 1);
 uint64_t bios_addr;
-uint32_t kernel_base, initrd_base, cmdline_base = 0;
-int32_t kernel_size, initrd_size;
+uint32_t kernel_base = 0, initrd_base = 0, cmdline_base = 0;
+int32_t kernel_size = 0, initrd_size = 0;
 PCIBus *pci_bus;
 PCIDevice *macio;
 MACIOIDEState *macio_ide;
@@ -139,8 +139,6 @@ static void ppc_heathrow_init(MachineState *machine)
 bios_addr = PROM_BASE;
 }
 g_free(filename);
-} else {
-bios_size = -1;
 }
 if (bios_size < 0 || bios_addr - PROM_BASE + bios_size > PROM_SIZE) {
 error_report("could not load PowerPC bios '%s'", bios_name);
@@ -148,12 +146,10 @@ static void ppc_heathrow_init(MachineState *machine)
 }
 
 if (machine->kernel_filename) {
-int bswap_needed;
+int bswap_needed = 0;
 
 #ifdef BSWAP_NEEDED
 bswap_needed = 1;
-#else
-bswap_needed = 0;
 #endif
 kernel_base = KERNEL_LOAD_ADDR;
 kernel_size = load_elf(machine->kernel_filename, NULL,
@@ -186,16 +182,10 @@ static void ppc_heathrow_init(MachineState *machine)
 }
 cmdline_base = TARGET_PAGE_ALIGN(initrd_base + initrd_size);
 } else {
-initrd_base = 0;
-initrd_size = 0;
 cmdline_base = TARGET_PAGE_ALIGN(kernel_base + kernel_size + 
KERNEL_GAP);
 }
 ppc_boot_device = 'm';
 } else {
-kernel_base = 0;
-kernel_size = 0;
-initrd_base = 0;
-initrd_size = 0;
 ppc_boot_device = '\0';
 for (i = 0; machine->boot_config.order[i] != '\0'; i++) {
 /*
-- 
2.30.4




[PATCH v3 52/54] .gitlab-ci.d/windows.yml: Display meson test logs

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

When CI fails we don't know what causes the failure. Displaying the
meson test logs can be helpful.

Signed-off-by: Bin Meng 
---

(no changes since v2)

Changes in v2:
- new patch: Display meson test logs in the Windows CI

 .gitlab-ci.d/windows.yml | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/.gitlab-ci.d/windows.yml b/.gitlab-ci.d/windows.yml
index 9ef4667317..29a3ba04a4 100644
--- a/.gitlab-ci.d/windows.yml
+++ b/.gitlab-ci.d/windows.yml
@@ -61,7 +61,7 @@ msys2-64bit:
   - .\msys64\usr\bin\bash -lc './configure --target-list=x86_64-softmmu
   --enable-capstone'
   - .\msys64\usr\bin\bash -lc 'make'
-  - .\msys64\usr\bin\bash -lc 'make check'
+  - .\msys64\usr\bin\bash -lc 'make check || { cat 
build/meson-logs/testlog.txt; exit 1; } ;'
 
 msys2-32bit:
   extends: .shared_msys2_builder
@@ -94,4 +94,4 @@ msys2-32bit:
   - cd output
   - ..\msys64\usr\bin\bash -lc "../configure --target-list=ppc64-softmmu"
   - ..\msys64\usr\bin\bash -lc 'make'
-  - ..\msys64\usr\bin\bash -lc 'make check'
+  - ..\msys64\usr\bin\bash -lc 'make check || { cat meson-logs/testlog.txt; 
exit 1; } ;'
-- 
2.34.1




[PATCH v3 42/54] chardev/char-file: Add FILE_SHARE_WRITE when opening the file for win32

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

The combination of GENERIC_WRITE and FILE_SHARE_READ options does not
allow the same file to be opened again by CreateFile() from another
QEMU process with the same options when the previous QEMU process
still holds the file handle opened.

This was triggered by running the test_multifd_tcp_cancel() case on
Windows, which cancels the migration, and launches another QEMU
process to migrate with the same file opened for write. Chances are
that the previous QEMU process does not quit before the new QEMU
process runs hence the old one still holds the file handle that does
not allow shared write permission then the new QEMU process will fail.

There is another test case boot-serial-test that triggers the same
issue. The qtest executable created a serial chardev file to be
passed to the QEMU executable. The serial file was created by
g_file_open_tmp(), which internally opens the file with
FILE_SHARE_WRITE security attribute, and based on [1], there is
only one case that allows the first call to CreateFile() with
GENERIC_READ & FILE_SHARE_WRITE, and second call to CreateFile()
with GENERIC_WRITE & FILE_SHARE_READ. All other combinations
require FILE_SHARE_WRITE in the second call. But there is no way
for the second call (in this case the QEMU executable) to know
what combination was passed to the first call, so we will have to
add FILE_SHARE_WRITE to the second call.

For both scenarios we should add FILE_SHARE_WRITE in the chardev
file backend driver. This change also makes the behavior to be
consistent with the POSIX platforms.

[1] 
https://docs.microsoft.com/en-us/windows/win32/fileio/creating-and-opening-files

Signed-off-by: Xuzhou Cheng 
Signed-off-by: Bin Meng 
---

Changes in v3:
- Add another case "boot-serial-test" to justify the change

Changes in v2:
- Update commit message to include the use case why we should set
  FILE_SHARE_WRITE when opening the file for win32

 chardev/char-file.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/chardev/char-file.c b/chardev/char-file.c
index 2fd80707e5..66385211eb 100644
--- a/chardev/char-file.c
+++ b/chardev/char-file.c
@@ -60,8 +60,8 @@ static void qmp_chardev_open_file(Chardev *chr,
 flags = CREATE_ALWAYS;
 }
 
-out = CreateFile(file->out, accessmode, FILE_SHARE_READ, NULL, flags,
- FILE_ATTRIBUTE_NORMAL, NULL);
+out = CreateFile(file->out, accessmode, FILE_SHARE_READ | FILE_SHARE_WRITE,
+ NULL, flags, FILE_ATTRIBUTE_NORMAL, NULL);
 if (out == INVALID_HANDLE_VALUE) {
 error_setg(errp, "open %s failed", file->out);
 return;
-- 
2.34.1




[PATCH v3 53/54] tests/qtest: Enable qtest build on Windows

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

Now that we have fixed various test case issues as seen when running
on Windows, let's enable the qtest build on Windows.

Signed-off-by: Bin Meng 
---

Changes in v3:
- Drop the host test

Changes in v2:
- new patch: "tests/qtest: Enable qtest build on Windows"

 tests/qtest/meson.build | 6 --
 1 file changed, 6 deletions(-)

diff --git a/tests/qtest/meson.build b/tests/qtest/meson.build
index 455f1bbb7e..8701c3a8e7 100644
--- a/tests/qtest/meson.build
+++ b/tests/qtest/meson.build
@@ -1,9 +1,3 @@
-# All QTests for now are POSIX-only, but the dependencies are
-# really in libqtest, not in the testcases themselves.
-if not config_host.has_key('CONFIG_POSIX')
-  subdir_done()
-endif
-
 slow_qtests = {
   'ahci-test' : 60,
   'bios-tables-test' : 120,
-- 
2.34.1




[PATCH v3 51/54] .gitlab-ci.d/windows.yml: Increase the timeout to 90 minutes

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

commit 9f8e6cad65a6 ("gitlab-ci: Speed up the msys2-64bit job by using 
--without-default-devices"
changed to compile QEMU with the --without-default-devices switch for
the msys2-64bit job, due to the build could not complete within the
project timeout (1h), and also mentioned that a bigger timeout was
getting ignored on the shared Gitlab-CI Windows runners.

However as of today it seems the shared Gitlab-CI Windows runners does
honor the job timeout, and the runner has the timeout limit of 2h, so
let's increase the timeout to 90 minutes and drop the configure switch
"--without-default-devices" to get a larger build coverage.

Signed-off-by: Bin Meng 
---

(no changes since v2)

Changes in v2:
- Change the timeout limit to 90 minutes

 .gitlab-ci.d/windows.yml | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/.gitlab-ci.d/windows.yml b/.gitlab-ci.d/windows.yml
index 86a4339c48..9ef4667317 100644
--- a/.gitlab-ci.d/windows.yml
+++ b/.gitlab-ci.d/windows.yml
@@ -10,7 +10,7 @@
   - ${CI_PROJECT_DIR}/msys64/var/cache
   needs: []
   stage: build
-  timeout: 70m
+  timeout: 90m
   before_script:
   - If ( !(Test-Path -Path msys64\var\cache ) ) {
   mkdir msys64\var\cache
@@ -59,7 +59,7 @@ msys2-64bit:
   - $env:MSYSTEM = 'MINGW64' # Start a 64 bit Mingw environment
   - $env:MSYS = 'winsymlinks:native' # Enable native Windows symlink
   - .\msys64\usr\bin\bash -lc './configure --target-list=x86_64-softmmu
-  --enable-capstone --without-default-devices'
+  --enable-capstone'
   - .\msys64\usr\bin\bash -lc 'make'
   - .\msys64\usr\bin\bash -lc 'make check'
 
-- 
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", _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", _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", _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", _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 48/54] io/channel-watch: Drop the unnecessary cast

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

There is no need to do a type cast on ssource->socket as it is
already declared as a SOCKET.

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

(no changes since v2)

Changes in v2:
- new patch: "io/channel-watch: Drop the unnecessary cast"

 io/channel-watch.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/io/channel-watch.c b/io/channel-watch.c
index 89f3c8a88a..43d38494f7 100644
--- a/io/channel-watch.c
+++ b/io/channel-watch.c
@@ -130,13 +130,13 @@ qio_channel_socket_source_check(GSource *source)
 FD_ZERO();
 FD_ZERO();
 if (ssource->condition & G_IO_IN) {
-FD_SET((SOCKET)ssource->socket, );
+FD_SET(ssource->socket, );
 }
 if (ssource->condition & G_IO_OUT) {
-FD_SET((SOCKET)ssource->socket, );
+FD_SET(ssource->socket, );
 }
 if (ssource->condition & G_IO_PRI) {
-FD_SET((SOCKET)ssource->socket, );
+FD_SET(ssource->socket, );
 }
 ssource->revents = 0;
 if (select(0, , , , ) == 0) {
-- 
2.34.1




[PATCH v3 50/54] tests/qtest: migration-test: Skip running some TLS cases for win32

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

Some migration test cases use TLS to communicate, but they fail on
Windows with the following error messages:

  qemu-system-x86_64: TLS handshake failed: Insufficient credentials for that 
request.
  qemu-system-x86_64: TLS handshake failed: Error in the pull function.
  query-migrate shows failed migration: TLS handshake failed: Error in the pull 
function.

Disable them temporarily.

Signed-off-by: Bin Meng 
---
I am not familar with the gnutls and simply enabling the gnutls debug
output does not give me an immedidate hint on why it's failing on
Windows. Disable these cases for now until someone or maintainers
who may want to test this on Windows.

(no changes since v1)

 tests/qtest/migration-test.c | 14 ++
 1 file changed, 14 insertions(+)

diff --git a/tests/qtest/migration-test.c b/tests/qtest/migration-test.c
index 9ec501d1eb..e22637a4e3 100644
--- a/tests/qtest/migration-test.c
+++ b/tests/qtest/migration-test.c
@@ -1402,6 +1402,7 @@ static void test_precopy_unix_dirty_ring(void)
 }
 
 #ifdef CONFIG_GNUTLS
+#ifndef _WIN32
 static void test_precopy_unix_tls_psk(void)
 {
 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
@@ -1414,6 +1415,7 @@ static void test_precopy_unix_tls_psk(void)
 
 test_precopy_common();
 }
+#endif /* _WIN32 */
 
 #ifdef CONFIG_TASN1
 static void test_precopy_unix_tls_x509_default_host(void)
@@ -1522,6 +1524,7 @@ static void test_precopy_tcp_plain(void)
 }
 
 #ifdef CONFIG_GNUTLS
+#ifndef _WIN32
 static void test_precopy_tcp_tls_psk_match(void)
 {
 MigrateCommon args = {
@@ -1532,6 +1535,7 @@ static void test_precopy_tcp_tls_psk_match(void)
 
 test_precopy_common();
 }
+#endif /* _WIN32 */
 
 static void test_precopy_tcp_tls_psk_mismatch(void)
 {
@@ -1929,6 +1933,7 @@ static void test_multifd_tcp_zstd(void)
 #endif
 
 #ifdef CONFIG_GNUTLS
+#ifndef _WIN32
 static void *
 test_migrate_multifd_tcp_tls_psk_start_match(QTestState *from,
  QTestState *to)
@@ -1936,6 +1941,7 @@ test_migrate_multifd_tcp_tls_psk_start_match(QTestState 
*from,
 test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
 return test_migrate_tls_psk_start_match(from, to);
 }
+#endif /* _WIN32 */
 
 static void *
 test_migrate_multifd_tcp_tls_psk_start_mismatch(QTestState *from,
@@ -1987,6 +1993,7 @@ 
test_migrate_multifd_tls_x509_start_reject_anon_client(QTestState *from,
 }
 #endif /* CONFIG_TASN1 */
 
+#ifndef _WIN32
 static void test_multifd_tcp_tls_psk_match(void)
 {
 MigrateCommon args = {
@@ -1996,6 +2003,7 @@ static void test_multifd_tcp_tls_psk_match(void)
 };
 test_precopy_common();
 }
+#endif /* _WIN32 */
 
 static void test_multifd_tcp_tls_psk_mismatch(void)
 {
@@ -2496,8 +2504,10 @@ int main(int argc, char **argv)
 qtest_add_func("/migration/precopy/unix/plain", test_precopy_unix_plain);
 qtest_add_func("/migration/precopy/unix/xbzrle", test_precopy_unix_xbzrle);
 #ifdef CONFIG_GNUTLS
+#ifndef _WIN32
 qtest_add_func("/migration/precopy/unix/tls/psk",
test_precopy_unix_tls_psk);
+#endif
 
 if (has_uffd) {
 /*
@@ -2523,8 +2533,10 @@ int main(int argc, char **argv)
 
 qtest_add_func("/migration/precopy/tcp/plain", test_precopy_tcp_plain);
 #ifdef CONFIG_GNUTLS
+#ifndef _WIN32
 qtest_add_func("/migration/precopy/tcp/tls/psk/match",
test_precopy_tcp_tls_psk_match);
+#endif
 qtest_add_func("/migration/precopy/tcp/tls/psk/mismatch",
test_precopy_tcp_tls_psk_mismatch);
 #ifdef CONFIG_TASN1
@@ -2568,8 +2580,10 @@ int main(int argc, char **argv)
test_multifd_tcp_zstd);
 #endif
 #ifdef CONFIG_GNUTLS
+#ifndef _WIN32
 qtest_add_func("/migration/multifd/tcp/tls/psk/match",
test_multifd_tcp_tls_psk_match);
+#endif
 qtest_add_func("/migration/multifd/tcp/tls/psk/mismatch",
test_multifd_tcp_tls_psk_mismatch);
 #ifdef CONFIG_TASN1
-- 
2.34.1




[PATCH v3 30/54] tests/qtest: qmp-test: Skip running test_qmp_oob for win32

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

The test_qmp_oob test case calls mkfifo() which does not exist on
win32. Exclude it.

Signed-off-by: Bin Meng 
Reviewed-by: Thomas Huth 
---

(no changes since v2)

Changes in v2:
- Add a comment in the code to explain why test_qmp_oob test case
  is skipped on win32

 tests/qtest/qmp-test.c | 7 +++
 1 file changed, 7 insertions(+)

diff --git a/tests/qtest/qmp-test.c b/tests/qtest/qmp-test.c
index 0fa00c12dc..074953fdf0 100644
--- a/tests/qtest/qmp-test.c
+++ b/tests/qtest/qmp-test.c
@@ -159,6 +159,8 @@ static void test_qmp_protocol(void)
 qtest_quit(qts);
 }
 
+#ifndef _WIN32
+
 /* Out-of-band tests */
 
 char *tmpdir;
@@ -279,6 +281,8 @@ static void test_qmp_oob(void)
 qtest_quit(qts);
 }
 
+#endif /* _WIN32 */
+
 /* Preconfig tests */
 
 static void test_qmp_preconfig(void)
@@ -338,7 +342,10 @@ int main(int argc, char *argv[])
 g_test_init(, , NULL);
 
 qtest_add_func("qmp/protocol", test_qmp_protocol);
+#ifndef _WIN32
+/* This case calls mkfifo() which does not exist on win32 */
 qtest_add_func("qmp/oob", test_qmp_oob);
+#endif
 qtest_add_func("qmp/preconfig", test_qmp_preconfig);
 qtest_add_func("qmp/missing-any-arg", test_qmp_missing_any_arg);
 
-- 
2.34.1




[PATCH v3 47/54] io/channel-watch: Drop a superfluous '#ifdef WIN32'

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

In the win32 version qio_channel_create_socket_watch() body there is
no need to do a '#ifdef WIN32'.

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

(no changes since v1)

 io/channel-watch.c | 2 --
 1 file changed, 2 deletions(-)

diff --git a/io/channel-watch.c b/io/channel-watch.c
index 0289b3647c..89f3c8a88a 100644
--- a/io/channel-watch.c
+++ b/io/channel-watch.c
@@ -285,11 +285,9 @@ GSource *qio_channel_create_socket_watch(QIOChannel *ioc,
 GSource *source;
 QIOChannelSocketSource *ssource;
 
-#ifdef WIN32
 WSAEventSelect(socket, ioc->event,
FD_READ | FD_ACCEPT | FD_CLOSE |
FD_CONNECT | FD_WRITE | FD_OOB);
-#endif
 
 source = g_source_new(_channel_socket_source_funcs,
   sizeof(QIOChannelSocketSource));
-- 
2.34.1




[PATCH v3 41/54] tests/qtest: virtio-net-failover: Disable migration tests for win32

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

These tests use the exec migration protocol, which is unsupported
on Windows as of today. Disable these tests for now.

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

(no changes since v1)

 tests/qtest/virtio-net-failover.c | 9 -
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/tests/qtest/virtio-net-failover.c 
b/tests/qtest/virtio-net-failover.c
index 443ee56de9..4a809590bf 100644
--- a/tests/qtest/virtio-net-failover.c
+++ b/tests/qtest/virtio-net-failover.c
@@ -588,6 +588,7 @@ static void test_hotplug_2_reverse(void)
 machine_stop(qts);
 }
 
+#ifndef _WIN32
 static QDict *migrate_status(QTestState *qts)
 {
 QDict *resp, *ret;
@@ -1827,6 +1828,7 @@ static void test_multi_in(gconstpointer opaque)
 
 machine_stop(qts);
 }
+#endif /* _WIN32 */
 
 int main(int argc, char **argv)
 {
@@ -1857,7 +1859,11 @@ int main(int argc, char **argv)
 qtest_add_func("failover-virtio-net/hotplug/2_reverse",
test_hotplug_2_reverse);
 
-/* migration tests */
+#ifndef _WIN32
+/*
+ * These migration tests cases use the exec migration protocol,
+ * which is unsupported on Windows.
+ */
 qtest_add_data_func("failover-virtio-net/migrate/on/out", tmpfile,
 test_migrate_out);
 qtest_add_data_func("failover-virtio-net/migrate/on/in", tmpfile,
@@ -1886,6 +1892,7 @@ int main(int argc, char **argv)
 tmpfile, test_multi_out);
 qtest_add_data_func("failover-virtio-net/migrate/multi/in",
tmpfile, test_multi_in);
+#endif /* _WIN32 */
 
 ret = g_test_run();
 
-- 
2.34.1




[PATCH v3 26/54] fsdev/virtfs-proxy-helper: Use g_mkdir()

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

Use g_mkdir() to create a directory on all platforms.

Signed-off-by: Bin Meng 
Reviewed-by: Christian Schoenebeck 
---

(no changes since v2)

Changes in v2:
- Change to use g_mkdir()

 fsdev/virtfs-proxy-helper.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/fsdev/virtfs-proxy-helper.c b/fsdev/virtfs-proxy-helper.c
index 2dde27922f..5cafcd7703 100644
--- a/fsdev/virtfs-proxy-helper.c
+++ b/fsdev/virtfs-proxy-helper.c
@@ -10,6 +10,7 @@
  */
 
 #include "qemu/osdep.h"
+#include 
 #include 
 #include 
 #include 
@@ -639,7 +640,7 @@ static int do_create_others(int type, struct iovec *iovec)
 if (retval < 0) {
 goto err_out;
 }
-retval = mkdir(path.data, mode);
+retval = g_mkdir(path.data, mode);
 break;
 case T_SYMLINK:
 retval = proxy_unmarshal(iovec, offset, "ss", , );
-- 
2.34.1




[PATCH v3 46/54] tests/qtest: libqtest: Correct the timeout unit of blocking receive calls for win32

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

Some qtest cases don't get response from the QEMU executable under
test in time on Windows. It turns out that the socket receive call
got timeout before it receive the complete response.

The timeout value is supposed to be set to 50 seconds via the
setsockopt() call, but there is a difference among platforms.
The timeout unit of blocking receive calls is measured in
seconds on non-Windows platforms but milliseconds on Windows.

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

(no changes since v1)

 tests/qtest/libqtest.c | 11 ++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/tests/qtest/libqtest.c b/tests/qtest/libqtest.c
index 1ae58624d9..e3360338de 100644
--- a/tests/qtest/libqtest.c
+++ b/tests/qtest/libqtest.c
@@ -36,13 +36,14 @@
 #include "qapi/qmp/qstring.h"
 
 #define MAX_IRQ 256
-#define SOCKET_TIMEOUT 50
 
 #ifndef _WIN32
+# define SOCKET_TIMEOUT 50
 # define CMD_EXEC   "exec "
 # define DEV_STDERR "/dev/fd/2"
 # define DEV_NULL   "/dev/null"
 #else
+# define SOCKET_TIMEOUT 5
 # define CMD_EXEC   ""
 # define DEV_STDERR "2"
 # define DEV_NULL   "nul"
@@ -106,8 +107,16 @@ static int socket_accept(int sock)
 struct sockaddr_un addr;
 socklen_t addrlen;
 int ret;
+/*
+ * timeout unit of blocking receive calls is different among platfoms.
+ * It's in seconds on non-Windows platforms but milliseconds on Windows.
+ */
+#ifndef _WIN32
 struct timeval timeout = { .tv_sec = SOCKET_TIMEOUT,
.tv_usec = 0 };
+#else
+DWORD timeout = SOCKET_TIMEOUT;
+#endif
 
 if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,
(void *), sizeof(timeout))) {
-- 
2.34.1




[PATCH v3 38/54] tests/qtest: bios-tables-test: Adapt the case for win32

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

Single quotes in the arguments (oem_id='CRASH ') are not removed in
the Windows environment before it is passed to the QEMU executable.
The space in the argument causes the "-acpitable" option parser to
think that all of its parameters are done, hence it complains:

  '-acpitable' requires one of 'data' or 'file'

Change to use double quotes which works fine on all platforms.

Also /dev/null does not work on win32, and nul should be used.

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

(no changes since v1)

 tests/qtest/bios-tables-test.c | 12 +---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/tests/qtest/bios-tables-test.c b/tests/qtest/bios-tables-test.c
index 46a46fe0aa..2ebeb530b2 100644
--- a/tests/qtest/bios-tables-test.c
+++ b/tests/qtest/bios-tables-test.c
@@ -1615,6 +1615,12 @@ static void test_acpi_virt_viot(void)
 free_test_data();
 }
 
+#ifndef _WIN32
+# define DEV_NULL "/dev/null"
+#else
+# define DEV_NULL "nul"
+#endif
+
 static void test_acpi_q35_slic(void)
 {
 test_data data = {
@@ -1622,9 +1628,9 @@ static void test_acpi_q35_slic(void)
 .variant = ".slic",
 };
 
-test_acpi_one("-acpitable sig=SLIC,oem_id='CRASH ',oem_table_id='ME',"
-  "oem_rev=2210,asl_compiler_id='qemu',"
-  "asl_compiler_rev=,data=/dev/null",
+test_acpi_one("-acpitable sig=SLIC,oem_id=\"CRASH \",oem_table_id=ME,"
+  "oem_rev=2210,asl_compiler_id=qemu,"
+  "asl_compiler_rev=,data=" DEV_NULL,
   );
 free_test_data();
 }
-- 
2.34.1




[PATCH v3 49/54] io/channel-watch: Fix socket watch on Windows

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

Random failure was observed when running qtests on Windows due to
"Broken pipe" detected by qmp_fd_receive(). What happened is that
the qtest executable sends testing data over a socket to the QEMU
under test but no response is received. The errno of the recv()
call from the qtest executable indicates ETIMEOUT, due to the qmp
chardev's tcp_chr_read() is never called to receive testing data
hence no response is sent to the other side.

tcp_chr_read() is registered as the callback of the socket watch
GSource. The reason of the callback not being called by glib, is
that the source check fails to indicate the source is ready. There
are two socket watch sources created to monitor the same socket
event object from the char-socket backend in update_ioc_handlers().
During the source check phase, qio_channel_socket_source_check()
calls WSAEnumNetworkEvents() to discover occurrences of network
events for the indicated socket, clear internal network event records,
and reset the event object. Testing shows that if we don't reset the
event object by not passing the event handle to WSAEnumNetworkEvents()
the symptom goes away and qtest runs very stably.

It seems we don't need to call WSAEnumNetworkEvents() at all, as we
don't parse the result of WSANETWORKEVENTS returned from this API.
We use select() to poll the socket status. Fix this instability by
dropping the WSAEnumNetworkEvents() call.

Some side notes:

During the testing, I removed the following codes in update_ioc_handlers():

  remove_hup_source(s);
  s->hup_source = qio_channel_create_watch(s->ioc, G_IO_HUP);
  g_source_set_callback(s->hup_source, (GSourceFunc)tcp_chr_hup,
chr, NULL);
  g_source_attach(s->hup_source, chr->gcontext);

and such change also makes the symptom go away.

And if I moved the above codes to the beginning, before the call to
io_add_watch_poll(), the symptom also goes away.

It seems two sources watching on the same socket event object is
the key that leads to the instability. The order of adding a source
watch seems to also play a role but I can't explain why.
Hopefully a Windows and glib expert could explain this behavior.

Signed-off-by: Bin Meng 
---

(no changes since v1)

 io/channel-watch.c | 4 
 1 file changed, 4 deletions(-)

diff --git a/io/channel-watch.c b/io/channel-watch.c
index 43d38494f7..ad7c568a84 100644
--- a/io/channel-watch.c
+++ b/io/channel-watch.c
@@ -115,17 +115,13 @@ static gboolean
 qio_channel_socket_source_check(GSource *source)
 {
 static struct timeval tv0;
-
 QIOChannelSocketSource *ssource = (QIOChannelSocketSource *)source;
-WSANETWORKEVENTS ev;
 fd_set rfds, wfds, xfds;
 
 if (!ssource->condition) {
 return 0;
 }
 
-WSAEnumNetworkEvents(ssource->socket, ssource->ioc->event, );
-
 FD_ZERO();
 FD_ZERO();
 FD_ZERO();
-- 
2.34.1




[PATCH v3 18/54] tests/qtest: vhost-user-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_dir_make_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/vhost-user-test.c | 8 
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/tests/qtest/vhost-user-test.c b/tests/qtest/vhost-user-test.c
index d7d6cfc9bd..448fda3e7f 100644
--- a/tests/qtest/vhost-user-test.c
+++ b/tests/qtest/vhost-user-test.c
@@ -482,8 +482,7 @@ static TestServer *test_server_new(const gchar *name,
 struct vhost_user_ops *ops)
 {
 TestServer *server = g_new0(TestServer, 1);
-char template[] = "/tmp/vhost-test-XX";
-const char *tmpfs;
+g_autofree const char *tmpfs = NULL;
 
 server->context = g_main_context_new();
 server->loop = g_main_loop_new(server->context, FALSE);
@@ -491,9 +490,10 @@ static TestServer *test_server_new(const gchar *name,
 /* run the main loop thread so the chardev may operate */
 server->thread = g_thread_new(NULL, thread_function, server->loop);
 
-tmpfs = g_mkdtemp(template);
+tmpfs = g_dir_make_tmp("vhost-test-XX", NULL);
 if (!tmpfs) {
-g_test_message("g_mkdtemp on path (%s): %s", template, 
strerror(errno));
+g_test_message("g_dir_make_tmp on path (%s): %s", tmpfs,
+   strerror(errno));
 }
 g_assert(tmpfs);
 
-- 
2.34.1




[PATCH v3 36/54] tests/qtest: Support libqtest to build and run on Windows

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

At present the libqtest codes were written to depend on several
POSIX APIs, including fork(), kill() and waitpid(). Unfortunately
these APIs are not available on Windows.

This commit implements the corresponding functionalities using
win32 native APIs. With this change, all qtest cases can build
successfully on a Windows host, and we can start qtest testing
on Windows now.

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

(no changes since v2)

Changes in v2:
- Move the enabling of building qtests on Windows to a separate
  patch to keep bisectablity
- Call socket_init() unconditionally
- Add a missing CloseHandle() call

 tests/qtest/libqtest.c | 98 +-
 1 file changed, 96 insertions(+), 2 deletions(-)

diff --git a/tests/qtest/libqtest.c b/tests/qtest/libqtest.c
index f0ac467903..12b1e85b51 100644
--- a/tests/qtest/libqtest.c
+++ b/tests/qtest/libqtest.c
@@ -16,9 +16,11 @@
 
 #include "qemu/osdep.h"
 
+#ifndef _WIN32
 #include 
 #include 
 #include 
+#endif /* _WIN32 */
 #ifdef __linux__
 #include 
 #endif /* __linux__ */
@@ -27,6 +29,7 @@
 #include "libqmp.h"
 #include "qemu/ctype.h"
 #include "qemu/cutils.h"
+#include "qemu/sockets.h"
 #include "qapi/qmp/qdict.h"
 #include "qapi/qmp/qjson.h"
 #include "qapi/qmp/qlist.h"
@@ -35,6 +38,16 @@
 #define MAX_IRQ 256
 #define SOCKET_TIMEOUT 50
 
+#ifndef _WIN32
+# define CMD_EXEC   "exec "
+# define DEV_STDERR "/dev/fd/2"
+# define DEV_NULL   "/dev/null"
+#else
+# define CMD_EXEC   ""
+# define DEV_STDERR "2"
+# define DEV_NULL   "nul"
+#endif
+
 typedef void (*QTestSendFn)(QTestState *s, const char *buf);
 typedef void (*ExternalSendFn)(void *s, const char *buf);
 typedef GString* (*QTestRecvFn)(QTestState *);
@@ -66,6 +79,9 @@ struct QTestState
 };
 
 static GHookList abrt_hooks;
+#ifdef _WIN32
+typedef void (*sighandler_t)(int);
+#endif
 static sighandler_t sighandler_old;
 
 static int qtest_query_target_endianness(QTestState *s);
@@ -118,10 +134,19 @@ bool qtest_probe_child(QTestState *s)
 pid_t pid = s->qemu_pid;
 
 if (pid != -1) {
+#ifndef _WIN32
 pid = waitpid(pid, >wstatus, WNOHANG);
 if (pid == 0) {
 return true;
 }
+#else
+DWORD exit_code;
+GetExitCodeProcess((HANDLE)pid, _code);
+if (exit_code == STILL_ACTIVE) {
+return true;
+}
+CloseHandle((HANDLE)pid);
+#endif
 s->qemu_pid = -1;
 }
 return false;
@@ -135,13 +160,23 @@ void qtest_set_expected_status(QTestState *s, int status)
 void qtest_kill_qemu(QTestState *s)
 {
 pid_t pid = s->qemu_pid;
+#ifndef _WIN32
 int wstatus;
+#else
+DWORD ret, exit_code;
+#endif
 
 /* Skip wait if qtest_probe_child already reaped.  */
 if (pid != -1) {
+#ifndef _WIN32
 kill(pid, SIGTERM);
 TFR(pid = waitpid(s->qemu_pid, >wstatus, 0));
 assert(pid == s->qemu_pid);
+#else
+TerminateProcess((HANDLE)pid, s->expected_status);
+ret = WaitForSingleObject((HANDLE)pid, INFINITE);
+assert(ret == WAIT_OBJECT_0);
+#endif
 s->qemu_pid = -1;
 }
 
@@ -149,6 +184,7 @@ void qtest_kill_qemu(QTestState *s)
  * Check whether qemu exited with expected exit status; anything else is
  * fishy and should be logged with as much detail as possible.
  */
+#ifndef _WIN32
 wstatus = s->wstatus;
 if (WIFEXITED(wstatus) && WEXITSTATUS(wstatus) != s->expected_status) {
 fprintf(stderr, "%s:%d: kill_qemu() tried to terminate QEMU "
@@ -165,6 +201,16 @@ void qtest_kill_qemu(QTestState *s)
 __FILE__, __LINE__, sig, signame, dump);
 abort();
 }
+#else
+GetExitCodeProcess((HANDLE)pid, _code);
+CloseHandle((HANDLE)pid);
+if (exit_code != s->expected_status) {
+fprintf(stderr, "%s:%d: kill_qemu() tried to terminate QEMU "
+"process but encountered exit status %ld (expected %d)\n",
+__FILE__, __LINE__, exit_code, s->expected_status);
+abort();
+}
+#endif
 }
 
 static void kill_qemu_hook_func(void *s)
@@ -243,6 +289,38 @@ static const char *qtest_qemu_binary(void)
 return qemu_bin;
 }
 
+#ifdef _WIN32
+static pid_t qtest_create_process(char *cmd)
+{
+STARTUPINFO si;
+PROCESS_INFORMATION pi;
+BOOL ret;
+
+ZeroMemory(, sizeof(si));
+si.cb = sizeof(si);
+ZeroMemory(, sizeof(pi));
+
+ret = CreateProcess(NULL,   /* module name */
+cmd,/* command line */
+NULL,   /* process handle not inheritable */
+NULL,   /* thread handle not inheritable */
+FALSE,  /* set handle inheritance to FALSE */
+0,  /* No creation flags */
+NULL,   /* use parent's environment block */
+NULL,   /* use parent's starting directory */
+,/* pointer to 

[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 34/54] tests/qtest: libqtest: Exclude the *_fds APIs for win32

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

libqmp.c::qmp_fd_vsend_fds() is not available on Windows, hence any
APIs in libqtest that call libqmp.c::qmp_fd_vsend_fds() should be
excluded for win32 too. This includes the following:

  * qtest_qmp_vsend_fds()
  * qtest_vqmp_fds()
  * qtest_qmp_fds()
  * qtest_qmp_add_client()

Note qtest_qmp_vsend() was wrongly written to call qmp_fd_vsend_fds()
previously, but it should call the non fds version API qmp_fd_vsend().

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

(no changes since v1)

 tests/qtest/libqtest.h |  8 
 tests/qtest/libqtest.c | 10 +-
 2 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/tests/qtest/libqtest.h b/tests/qtest/libqtest.h
index 94b187837d..3abc75964d 100644
--- a/tests/qtest/libqtest.h
+++ b/tests/qtest/libqtest.h
@@ -94,6 +94,7 @@ void qtest_kill_qemu(QTestState *s);
  */
 void qtest_quit(QTestState *s);
 
+#ifndef _WIN32
 /**
  * qtest_qmp_fds:
  * @s: #QTestState instance to operate on.
@@ -108,6 +109,7 @@ void qtest_quit(QTestState *s);
 QDict *qtest_qmp_fds(QTestState *s, int *fds, size_t fds_num,
  const char *fmt, ...)
 G_GNUC_PRINTF(4, 5);
+#endif /* _WIN32 */
 
 /**
  * qtest_qmp:
@@ -152,6 +154,7 @@ void qtest_qmp_send_raw(QTestState *s, const char *fmt, ...)
  */
 int qtest_socket_server(const char *socket_path);
 
+#ifndef _WIN32
 /**
  * qtest_vqmp_fds:
  * @s: #QTestState instance to operate on.
@@ -167,6 +170,7 @@ int qtest_socket_server(const char *socket_path);
 QDict *qtest_vqmp_fds(QTestState *s, int *fds, size_t fds_num,
   const char *fmt, va_list ap)
 G_GNUC_PRINTF(4, 0);
+#endif /* _WIN32 */
 
 /**
  * qtest_vqmp:
@@ -181,6 +185,7 @@ QDict *qtest_vqmp_fds(QTestState *s, int *fds, size_t 
fds_num,
 QDict *qtest_vqmp(QTestState *s, const char *fmt, va_list ap)
 G_GNUC_PRINTF(2, 0);
 
+#ifndef _WIN32
 /**
  * qtest_qmp_vsend_fds:
  * @s: #QTestState instance to operate on.
@@ -196,6 +201,7 @@ QDict *qtest_vqmp(QTestState *s, const char *fmt, va_list 
ap)
 void qtest_qmp_vsend_fds(QTestState *s, int *fds, size_t fds_num,
  const char *fmt, va_list ap)
 G_GNUC_PRINTF(4, 0);
+#endif /* _WIN32 */
 
 /**
  * qtest_qmp_vsend:
@@ -743,6 +749,7 @@ void qtest_qmp_device_add_qdict(QTestState *qts, const char 
*drv,
 void qtest_qmp_device_add(QTestState *qts, const char *driver, const char *id,
   const char *fmt, ...) G_GNUC_PRINTF(4, 5);
 
+#ifndef _WIN32
 /**
  * qtest_qmp_add_client:
  * @qts: QTestState instance to operate on
@@ -752,6 +759,7 @@ void qtest_qmp_device_add(QTestState *qts, const char 
*driver, const char *id,
  * Call QMP ``getfd`` followed by ``add_client`` with the given @fd.
  */
 void qtest_qmp_add_client(QTestState *qts, const char *protocol, int fd);
+#endif /* _WIN32 */
 
 /**
  * qtest_qmp_device_del:
diff --git a/tests/qtest/libqtest.c b/tests/qtest/libqtest.c
index 0dfe630373..269d622fe3 100644
--- a/tests/qtest/libqtest.c
+++ b/tests/qtest/libqtest.c
@@ -592,17 +592,20 @@ int qtest_socket_server(const char *socket_path)
 return sock;
 }
 
+#ifndef _WIN32
 void qtest_qmp_vsend_fds(QTestState *s, int *fds, size_t fds_num,
  const char *fmt, va_list ap)
 {
 qmp_fd_vsend_fds(s->qmp_fd, fds, fds_num, fmt, ap);
 }
+#endif
 
 void qtest_qmp_vsend(QTestState *s, const char *fmt, va_list ap)
 {
-qmp_fd_vsend_fds(s->qmp_fd, NULL, 0, fmt, ap);
+qmp_fd_vsend(s->qmp_fd, fmt, ap);
 }
 
+#ifndef _WIN32
 QDict *qtest_vqmp_fds(QTestState *s, int *fds, size_t fds_num,
   const char *fmt, va_list ap)
 {
@@ -611,6 +614,7 @@ QDict *qtest_vqmp_fds(QTestState *s, int *fds, size_t 
fds_num,
 /* Receive reply */
 return qtest_qmp_receive(s);
 }
+#endif
 
 QDict *qtest_vqmp(QTestState *s, const char *fmt, va_list ap)
 {
@@ -620,6 +624,7 @@ QDict *qtest_vqmp(QTestState *s, const char *fmt, va_list 
ap)
 return qtest_qmp_receive(s);
 }
 
+#ifndef _WIN32
 QDict *qtest_qmp_fds(QTestState *s, int *fds, size_t fds_num,
  const char *fmt, ...)
 {
@@ -631,6 +636,7 @@ QDict *qtest_qmp_fds(QTestState *s, int *fds, size_t 
fds_num,
 va_end(ap);
 return response;
 }
+#endif
 
 QDict *qtest_qmp(QTestState *s, const char *fmt, ...)
 {
@@ -1327,6 +1333,7 @@ void qtest_qmp_device_add(QTestState *qts, const char 
*driver, const char *id,
 qobject_unref(args);
 }
 
+#ifndef _WIN32
 void qtest_qmp_add_client(QTestState *qts, const char *protocol, int fd)
 {
 QDict *resp;
@@ -1346,6 +1353,7 @@ void qtest_qmp_add_client(QTestState *qts, const char 
*protocol, int fd)
 g_assert(!qdict_haskey(resp, "error"));
 qobject_unref(resp);
 }
+#endif
 
 /*
  * Generic hot-unplugging test via the device_del QMP command.
-- 
2.34.1




[PATCH v3 39/54] tests/qtest: migration-test: Disable IO redirection for win32

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

On Windows the QEMU executable is created via CreateProcess() and
IO redirection does not work, so don't bother adding IO redirection
to the command line.

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

(no changes since v2)

Changes in v2:
- Change the place that sets IO redirection in the command line

 tests/qtest/migration-test.c | 9 +
 1 file changed, 9 insertions(+)

diff --git a/tests/qtest/migration-test.c b/tests/qtest/migration-test.c
index 029c4f3a6a..a1dd342739 100644
--- a/tests/qtest/migration-test.c
+++ b/tests/qtest/migration-test.c
@@ -647,7 +647,16 @@ static int test_migrate_start(QTestState **from, 
QTestState **to,
 }
 
 if (!getenv("QTEST_LOG") && args->hide_stderr) {
+#ifndef _WIN32
 ignore_stderr = "2>/dev/null";
+#else
+/*
+ * On Windows the QEMU executable is created via CreateProcess() and
+ * IO redirection does not work, so don't bother adding IO redirection
+ * to the command line.
+ */
+ignore_stderr = "";
+#endif
 } else {
 ignore_stderr = "";
 }
-- 
2.34.1




[PATCH v3 20/54] tests/qtest: virtio-scsi-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/virtio-scsi-test.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/tests/qtest/virtio-scsi-test.c b/tests/qtest/virtio-scsi-test.c
index 8ceb12aacd..ceaa7f2415 100644
--- a/tests/qtest/virtio-scsi-test.c
+++ b/tests/qtest/virtio-scsi-test.c
@@ -268,7 +268,7 @@ static void test_iothread_attach_node(void *obj, void *data,
 QVirtioSCSIPCI *scsi_pci = obj;
 QVirtioSCSI *scsi = _pci->scsi;
 QVirtioSCSIQueues *vs;
-char tmp_path[] = "/tmp/qtest.XX";
+g_autofree char *tmp_path = NULL;
 int fd;
 int ret;
 
@@ -282,7 +282,7 @@ static void test_iothread_attach_node(void *obj, void *data,
 vs = qvirtio_scsi_init(scsi->vdev);
 
 /* Create a temporary qcow2 overlay*/
-fd = mkstemp(tmp_path);
+fd = g_file_open_tmp("qtest.XX", _path, NULL);
 g_assert(fd >= 0);
 close(fd);
 
-- 
2.34.1




[PATCH v3 43/54] tests/qtest: migration-test: Make sure QEMU process "to" exited after migration is canceled

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

Make sure QEMU process "to" exited before launching another target
for migration in the test_multifd_tcp_cancel case.

Signed-off-by: Xuzhou Cheng 
Signed-off-by: Bin Meng 
---

Changes in v3:
- Add a usleep(1) in the busy wait loop

Changes in v2:
- Change to a busy wait after migration is canceled

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

diff --git a/tests/qtest/migration-test.c b/tests/qtest/migration-test.c
index a1dd342739..9ec501d1eb 100644
--- a/tests/qtest/migration-test.c
+++ b/tests/qtest/migration-test.c
@@ -2132,6 +2132,10 @@ static void test_multifd_tcp_cancel(void)
 wait_for_migration_pass(from);
 
 migrate_cancel(from);
+/* Make sure QEMU process "to" exited */
+while (qtest_probe_child(to)) {
+usleep(1);
+}
 
 args = (MigrateStart){
 .only_target = true,
-- 
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", _path, NULL);
 g_assert_cmpint(fd, >=, 0);
 ret = ftruncate(fd, TEST_IMAGE_SIZE);
 g_assert_cmpint(ret, ==, 0);
-- 
2.34.1




[PATCH v3 33/54] tests/qtest: Use send/recv for socket communication

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

Socket communication in the libqtest and libqmp codes uses read()
and write() which work on any file descriptor on *nix, and sockets
in *nix are an example of a file descriptor.

However sockets on Windows do not use *nix-style file descriptors,
so read() and write() cannot be used on sockets on Windows.
Switch over to use send() and recv() instead which work on both
Windows and *nix.

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

(no changes since v2)

Changes in v2:
- Introduce qemu_send_full() and use it

 include/qemu/sockets.h |  2 ++
 tests/qtest/libqmp.c   |  5 +++--
 tests/qtest/libqtest.c |  4 ++--
 util/osdep.c   | 33 +
 4 files changed, 40 insertions(+), 4 deletions(-)

diff --git a/include/qemu/sockets.h b/include/qemu/sockets.h
index 038faa157f..8ff7832eba 100644
--- a/include/qemu/sockets.h
+++ b/include/qemu/sockets.h
@@ -15,6 +15,8 @@ int inet_aton(const char *cp, struct in_addr *ia);
 bool fd_is_socket(int fd);
 int qemu_socket(int domain, int type, int protocol);
 int qemu_accept(int s, struct sockaddr *addr, socklen_t *addrlen);
+ssize_t qemu_send_full(int s, const void *buf, size_t count)
+G_GNUC_WARN_UNUSED_RESULT;
 int socket_set_cork(int fd, int v);
 int socket_set_nodelay(int fd);
 void qemu_socket_set_block(int fd);
diff --git a/tests/qtest/libqmp.c b/tests/qtest/libqmp.c
index ade26c15f0..2b08382e5d 100644
--- a/tests/qtest/libqmp.c
+++ b/tests/qtest/libqmp.c
@@ -23,6 +23,7 @@
 #endif
 
 #include "qemu/cutils.h"
+#include "qemu/sockets.h"
 #include "qapi/error.h"
 #include "qapi/qmp/json-parser.h"
 #include "qapi/qmp/qjson.h"
@@ -36,7 +37,7 @@ typedef struct {
 
 static void socket_send(int fd, const char *buf, size_t size)
 {
-size_t res = qemu_write_full(fd, buf, size);
+ssize_t res = qemu_send_full(fd, buf, size);
 
 assert(res == size);
 }
@@ -69,7 +70,7 @@ QDict *qmp_fd_receive(int fd)
 ssize_t len;
 char c;
 
-len = read(fd, , 1);
+len = recv(fd, , 1, 0);
 if (len == -1 && errno == EINTR) {
 continue;
 }
diff --git a/tests/qtest/libqtest.c b/tests/qtest/libqtest.c
index d8ffa0e7b1..0dfe630373 100644
--- a/tests/qtest/libqtest.c
+++ b/tests/qtest/libqtest.c
@@ -436,7 +436,7 @@ void qtest_quit(QTestState *s)
 
 static void socket_send(int fd, const char *buf, size_t size)
 {
-size_t res = qemu_write_full(fd, buf, size);
+ssize_t res = qemu_send_full(fd, buf, size);
 
 assert(res == size);
 }
@@ -468,7 +468,7 @@ static GString *qtest_client_socket_recv_line(QTestState *s)
 ssize_t len;
 char buffer[1024];
 
-len = read(s->fd, buffer, sizeof(buffer));
+len = recv(s->fd, buffer, sizeof(buffer), 0);
 if (len == -1 && errno == EINTR) {
 continue;
 }
diff --git a/util/osdep.c b/util/osdep.c
index 60fcbbaebe..0342e754e1 100644
--- a/util/osdep.c
+++ b/util/osdep.c
@@ -502,6 +502,39 @@ int qemu_accept(int s, struct sockaddr *addr, socklen_t 
*addrlen)
 return ret;
 }
 
+/*
+ * A variant of send(2) which handles partial send.
+ *
+ * Return the number of bytes transferred over the socket.
+ * Set errno if fewer than `count' bytes are sent.
+ *
+ * This function don't work with non-blocking socket's.
+ * Any of the possibilities with non-blocking socket's is bad:
+ *   - return a short write (then name is wrong)
+ *   - busy wait adding (errno == EAGAIN) to the loop
+ */
+ssize_t qemu_send_full(int s, const void *buf, size_t count)
+{
+ssize_t ret = 0;
+ssize_t total = 0;
+
+while (count) {
+ret = send(s, buf, count, 0);
+if (ret < 0) {
+if (errno == EINTR) {
+continue;
+}
+break;
+}
+
+count -= ret;
+buf += ret;
+total += ret;
+}
+
+return total;
+}
+
 void qemu_set_hw_version(const char *version)
 {
 hw_version = version;
-- 
2.34.1




[PATCH v3 31/54] accel/qtest: Implement a portable qtest accelerator

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

Currently signal SIGIPI [=SIGUSR1] is used to kick the dummy CPU
when qtest accelerator is used. However SIGUSR1 is unsupported on
Windows. To support Windows, we add a QemuSemaphore CPUState::sem
to kick the dummy CPU instead.

As a result of this, the POSIX implementation via signal is no
longer needed and can use the same path as Windows.

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

(no changes since v2)

Changes in v2:
- Replace signal by the semaphore on posix too

 include/hw/core/cpu.h   |  1 +
 accel/dummy-cpus.c  | 15 ++-
 softmmu/cpus.c  | 10 +-
 accel/meson.build   |  1 +
 accel/qtest/meson.build |  1 +
 5 files changed, 6 insertions(+), 22 deletions(-)

diff --git a/include/hw/core/cpu.h b/include/hw/core/cpu.h
index 500503da13..2f46c37dc1 100644
--- a/include/hw/core/cpu.h
+++ b/include/hw/core/cpu.h
@@ -326,6 +326,7 @@ struct CPUState {
 #ifdef _WIN32
 HANDLE hThread;
 #endif
+QemuSemaphore sem;
 int thread_id;
 bool running, has_waiter;
 struct QemuCond *halt_cond;
diff --git a/accel/dummy-cpus.c b/accel/dummy-cpus.c
index 10429fdfb2..3769d3db0a 100644
--- a/accel/dummy-cpus.c
+++ b/accel/dummy-cpus.c
@@ -21,8 +21,6 @@
 static void *dummy_cpu_thread_fn(void *arg)
 {
 CPUState *cpu = arg;
-sigset_t waitset;
-int r;
 
 rcu_register_thread();
 
@@ -32,23 +30,13 @@ static void *dummy_cpu_thread_fn(void *arg)
 cpu->can_do_io = 1;
 current_cpu = cpu;
 
-sigemptyset();
-sigaddset(, SIG_IPI);
-
 /* signal CPU creation */
 cpu_thread_signal_created(cpu);
 qemu_guest_random_seed_thread_part2(cpu->random_seed);
 
 do {
 qemu_mutex_unlock_iothread();
-do {
-int sig;
-r = sigwait(, );
-} while (r == -1 && (errno == EAGAIN || errno == EINTR));
-if (r == -1) {
-perror("sigwait");
-exit(1);
-}
+qemu_sem_wait(>sem);
 qemu_mutex_lock_iothread();
 qemu_wait_io_event(cpu);
 } while (!cpu->unplug);
@@ -67,6 +55,7 @@ void dummy_start_vcpu_thread(CPUState *cpu)
 qemu_cond_init(cpu->halt_cond);
 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/DUMMY",
  cpu->cpu_index);
+qemu_sem_init(>sem, 0);
 qemu_thread_create(cpu->thread, thread_name, dummy_cpu_thread_fn, cpu,
QEMU_THREAD_JOINABLE);
 }
diff --git a/softmmu/cpus.c b/softmmu/cpus.c
index 23b30484b2..2a992d0d5f 100644
--- a/softmmu/cpus.c
+++ b/softmmu/cpus.c
@@ -437,19 +437,11 @@ void qemu_wait_io_event(CPUState *cpu)
 
 void cpus_kick_thread(CPUState *cpu)
 {
-#ifndef _WIN32
-int err;
-
 if (cpu->thread_kicked) {
 return;
 }
 cpu->thread_kicked = true;
-err = pthread_kill(cpu->thread->thread, SIG_IPI);
-if (err && err != ESRCH) {
-fprintf(stderr, "qemu:%s: %s", __func__, strerror(err));
-exit(1);
-}
-#endif
+qemu_sem_post(>sem);
 }
 
 void qemu_cpu_kick(CPUState *cpu)
diff --git a/accel/meson.build b/accel/meson.build
index b9a963cf80..b21c85dc0a 100644
--- a/accel/meson.build
+++ b/accel/meson.build
@@ -17,4 +17,5 @@ dummy_ss.add(files(
 ))
 
 specific_ss.add_all(when: ['CONFIG_SOFTMMU', 'CONFIG_POSIX'], if_true: 
dummy_ss)
+specific_ss.add_all(when: ['CONFIG_WIN32'], if_true: dummy_ss)
 specific_ss.add_all(when: ['CONFIG_XEN'], if_true: dummy_ss)
diff --git a/accel/qtest/meson.build b/accel/qtest/meson.build
index 4c65600293..a4876fc0f2 100644
--- a/accel/qtest/meson.build
+++ b/accel/qtest/meson.build
@@ -1,2 +1,3 @@
 qtest_module_ss.add(when: ['CONFIG_SOFTMMU', 'CONFIG_POSIX'],
 if_true: files('qtest.c'))
+qtest_module_ss.add(when: ['CONFIG_WIN32'], if_true: files('qtest.c'))
-- 
2.34.1




[PATCH v3 15/54] tests/qtest: pflash-cfi02-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/pflash-cfi02-test.c | 8 +---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/tests/qtest/pflash-cfi02-test.c b/tests/qtest/pflash-cfi02-test.c
index 7fce614b64..55890b7d95 100644
--- a/tests/qtest/pflash-cfi02-test.c
+++ b/tests/qtest/pflash-cfi02-test.c
@@ -56,7 +56,7 @@ typedef struct {
 QTestState *qtest;
 } FlashConfig;
 
-static char image_path[] = "/tmp/qtest.XX";
+static char *image_path;
 
 /*
  * The pflash implementation allows some parameters to be unspecified. We want
@@ -608,6 +608,7 @@ static void test_cfi_in_autoselect(const void *opaque)
 static void cleanup(void *opaque)
 {
 unlink(image_path);
+g_free(image_path);
 }
 
 /*
@@ -635,16 +636,17 @@ static const FlashConfig configuration[] = {
 
 int main(int argc, char **argv)
 {
-int fd = mkstemp(image_path);
+int fd = g_file_open_tmp("qtest.XX", _path, NULL);
 if (fd == -1) {
 g_printerr("Failed to create temporary file %s: %s\n", image_path,
strerror(errno));
+g_free(image_path);
 exit(EXIT_FAILURE);
 }
 if (ftruncate(fd, UNIFORM_FLASH_SIZE) < 0) {
 int error_code = errno;
 close(fd);
-unlink(image_path);
+cleanup(NULL);
 g_printerr("Failed to truncate file %s to %u MB: %s\n", image_path,
UNIFORM_FLASH_SIZE, strerror(error_code));
 exit(EXIT_FAILURE);
-- 
2.34.1




[PATCH v3 35/54] tests/qtest: libqtest: Install signal handler via signal()

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

At present the codes uses sigaction() to install signal handler with
a flag SA_RESETHAND. Such usage can be covered by the signal() API
that is a simplified interface to the general sigaction() facility.

Update to use signal() to install the signal handler, as it is
available on Windows which we are going to support.

Signed-off-by: Bin Meng 
---

(no changes since v1)

 tests/qtest/libqtest.c | 14 +++---
 1 file changed, 3 insertions(+), 11 deletions(-)

diff --git a/tests/qtest/libqtest.c b/tests/qtest/libqtest.c
index 269d622fe3..f0ac467903 100644
--- a/tests/qtest/libqtest.c
+++ b/tests/qtest/libqtest.c
@@ -66,7 +66,7 @@ struct QTestState
 };
 
 static GHookList abrt_hooks;
-static struct sigaction sigact_old;
+static sighandler_t sighandler_old;
 
 static int qtest_query_target_endianness(QTestState *s);
 
@@ -179,20 +179,12 @@ static void sigabrt_handler(int signo)
 
 static void setup_sigabrt_handler(void)
 {
-struct sigaction sigact;
-
-/* Catch SIGABRT to clean up on g_assert() failure */
-sigact = (struct sigaction){
-.sa_handler = sigabrt_handler,
-.sa_flags = SA_RESETHAND,
-};
-sigemptyset(_mask);
-sigaction(SIGABRT, , _old);
+sighandler_old = signal(SIGABRT, sigabrt_handler);
 }
 
 static void cleanup_sigabrt_handler(void)
 {
-sigaction(SIGABRT, _old, NULL);
+signal(SIGABRT, sighandler_old);
 }
 
 static bool hook_list_is_empty(GHookList *hook_list)
-- 
2.34.1




[PATCH v3 27/54] hw/usb: dev-mtp: Use g_mkdir()

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

Use g_mkdir() to create a directory on all platforms.

Signed-off-by: Bin Meng 
Acked-by: Gerd Hoffmann 
---

(no changes since v2)

Changes in v2:
- Change to use g_mkdir()

 hw/usb/dev-mtp.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/hw/usb/dev-mtp.c b/hw/usb/dev-mtp.c
index 5831395cef..1cac1cd435 100644
--- a/hw/usb/dev-mtp.c
+++ b/hw/usb/dev-mtp.c
@@ -14,7 +14,7 @@
 #include "qemu/error-report.h"
 #include 
 #include 
-
+#include 
 #include 
 
 
@@ -1622,7 +1622,7 @@ static void usb_mtp_write_data(MTPState *s, uint32_t 
handle)
 if (s->dataset.filename) {
 path = g_strdup_printf("%s/%s", parent->path, s->dataset.filename);
 if (s->dataset.format == FMT_ASSOCIATION) {
-ret = mkdir(path, mask);
+ret = g_mkdir(path, mask);
 if (!ret) {
 usb_mtp_queue_result(s, RES_OK, d->trans, 3,
  QEMU_STORAGE_ID,
-- 
2.34.1




[PATCH v3 29/54] tests/qtest: Build test-filter-{mirror, redirector} cases for posix only

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

The test-filter-{mirror,redirector} cases use socketpair() API that
is only available on POSIX and should only be built for POSIX.

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

(no changes since v2)

Changes in v2:
- Introduce a new variable qtests_filter and add that to the
  qtests_ARCH variables

 tests/qtest/meson.build | 36 
 1 file changed, 16 insertions(+), 20 deletions(-)

diff --git a/tests/qtest/meson.build b/tests/qtest/meson.build
index e910cb32ca..455f1bbb7e 100644
--- a/tests/qtest/meson.build
+++ b/tests/qtest/meson.build
@@ -39,9 +39,14 @@ qtests_pci = \
 qtests_cxl = \
   (config_all_devices.has_key('CONFIG_CXL') ? ['cxl-test'] : [])
 
+qtests_filter = \
+  (slirp.found() ? ['test-netfilter'] : []) + \
+  (config_host.has_key('CONFIG_POSIX') ? ['test-filter-mirror'] : []) + \
+  (config_host.has_key('CONFIG_POSIX') ? ['test-filter-redirector'] : [])
+
 qtests_i386 = \
-  (slirp.found() ? ['pxe-test', 'test-netfilter'] : []) + \
-  (config_host.has_key('CONFIG_POSIX') ? ['test-filter-mirror'] : []) +
 \
+  (slirp.found() ? ['pxe-test'] : []) + \
+  qtests_filter + \
   (have_tools ? ['ahci-test'] : []) +  
 \
   (config_all_devices.has_key('CONFIG_ISA_TESTDEV') ? ['endianness-test'] : 
[]) +   \
   (config_all_devices.has_key('CONFIG_SGA') ? ['boot-serial-test'] : []) + 
 \
@@ -95,8 +100,7 @@ qtests_i386 = \
'vmgenid-test',
'migration-test',
'test-x86-cpuid-compat',
-   'numa-test',
-   'test-filter-redirector'
+   'numa-test'
   ]
 
 if dbus_display
@@ -120,30 +124,25 @@ endif
 qtests_x86_64 = qtests_i386
 
 qtests_alpha = ['boot-serial-test'] + \
-  ['test-filter-mirror', 'test-filter-redirector'] + \
-  (slirp.found() ? ['test-netfilter'] : []) + \
+  qtests_filter + \
   (config_all_devices.has_key('CONFIG_VGA') ? ['display-vga-test'] : [])
 
 qtests_avr = [ 'boot-serial-test' ]
 
 qtests_hppa = ['boot-serial-test'] + \
-  ['test-filter-mirror', 'test-filter-redirector'] + \
-  (slirp.found() ? ['test-netfilter'] : []) + \
+  qtests_filter + \
   (config_all_devices.has_key('CONFIG_VGA') ? ['display-vga-test'] : [])
 
 qtests_m68k = ['boot-serial-test'] + \
-  ['test-filter-mirror', 'test-filter-redirector'] + \
-  (slirp.found() ? ['test-netfilter'] : [])
+  qtests_filter
 
 qtests_microblaze = ['boot-serial-test'] + \
-  ['test-filter-mirror', 'test-filter-redirector'] + \
-  (slirp.found() ? ['test-netfilter'] : [])
+  qtests_filter
 
 qtests_microblazeel = qtests_microblaze
 
 qtests_mips = \
-  ['test-filter-mirror', 'test-filter-redirector'] + \
-  (slirp.found() ? ['test-netfilter'] : []) + \
+  qtests_filter + \
   (config_all_devices.has_key('CONFIG_ISA_TESTDEV') ? ['endianness-test'] : 
[]) +\
   (config_all_devices.has_key('CONFIG_VGA') ? ['display-vga-test'] : [])
 
@@ -152,8 +151,7 @@ qtests_mips64 = qtests_mips
 qtests_mips64el = qtests_mips
 
 qtests_ppc = \
-  ['test-filter-mirror', 'test-filter-redirector'] + \
-  (slirp.found() ? ['test-netfilter'] : []) + \
+  qtests_filter + \
   (config_all_devices.has_key('CONFIG_ISA_TESTDEV') ? ['endianness-test'] : 
[]) +\
   (config_all_devices.has_key('CONFIG_M48T59') ? ['m48t59-test'] : []) +   
  \
   (config_all_devices.has_key('CONFIG_TCG') ? ['prom-env-test'] : []) +
  \
@@ -174,13 +172,11 @@ qtests_sh4 = 
(config_all_devices.has_key('CONFIG_ISA_TESTDEV') ? ['endianness-te
 qtests_sh4eb = (config_all_devices.has_key('CONFIG_ISA_TESTDEV') ? 
['endianness-test'] : [])
 
 qtests_sparc = ['prom-env-test', 'm48t59-test', 'boot-serial-test'] + \
-  ['test-filter-mirror', 'test-filter-redirector'] + \
-  (slirp.found() ? ['test-netfilter'] : [])
+  qtests_filter
 
 qtests_sparc64 = \
   (config_all_devices.has_key('CONFIG_ISA_TESTDEV') ? ['endianness-test'] : 
[]) +\
-  (slirp.found() ? ['test-netfilter'] : []) + \
-  ['test-filter-mirror', 'test-filter-redirector'] + \
+  qtests_filter + \
   ['prom-env-test', 'boot-serial-test']
 
 qtests_npcm7xx = \
-- 
2.34.1




[PATCH v3 32/54] tests/qtest: libqtest: Adapt global_qtest declaration for win32

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

Commit dd2107497275 ("tests/libqtest: Use libqtest-single.h in tests that 
require global_qtest")
moved global_qtest to libqtest-single.h, by declaring global_qtest
attribute to be common and weak.

This trick unfortunately does not work on Windows, and building
qtest test cases results in multiple definition errors of the weak
symbol global_qtest, as Windows PE does not have the concept of
the so-called weak symbol like ELF in the *nix world.

However Windows does provide a trick to declare a variable to be
a common symbol, via __declspec(selectany) [1]. It does not provide
the "strong override weak" effect but we don't need it in our use
case anyway. So let's use it for win32.

[1] https://docs.microsoft.com/en-us/cpp/cpp/selectany

Signed-off-by: Xuzhou Cheng 
Signed-off-by: Bin Meng 
---

(no changes since v2)

Changes in v2:
- Use __declspec(selectany) for the common weak symbol on Windows

 tests/qtest/libqtest-single.h | 4 
 1 file changed, 4 insertions(+)

diff --git a/tests/qtest/libqtest-single.h b/tests/qtest/libqtest-single.h
index 4e7d0ae1dc..851724cbcb 100644
--- a/tests/qtest/libqtest-single.h
+++ b/tests/qtest/libqtest-single.h
@@ -13,7 +13,11 @@
 
 #include "libqtest.h"
 
+#ifndef _WIN32
 QTestState *global_qtest __attribute__((common, weak));
+#else
+__declspec(selectany) QTestState *global_qtest;
+#endif
 
 /**
  * qtest_start:
-- 
2.34.1




[PATCH v3 17/54] tests/qtest: vhost-user-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_get_tmp_dir() for a portable implementation.

Signed-off-by: Bin Meng 
---

Changes in v3:
- Split to a separate patch

 tests/qtest/vhost-user-blk-test.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/tests/qtest/vhost-user-blk-test.c 
b/tests/qtest/vhost-user-blk-test.c
index a81c2a2715..07a4c2d500 100644
--- a/tests/qtest/vhost-user-blk-test.c
+++ b/tests/qtest/vhost-user-blk-test.c
@@ -841,7 +841,8 @@ static char *create_listen_socket(int *fd)
 char *path;
 
 /* No race because our pid makes the path unique */
-path = g_strdup_printf("/tmp/qtest-%d-sock.XX", getpid());
+path = g_strdup_printf("%s/qtest-%d-sock.XX",
+   g_get_tmp_dir(), getpid());
 tmp_fd = mkstemp(path);
 g_assert_cmpint(tmp_fd, >=, 0);
 close(tmp_fd);
-- 
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 24/54] tests: vhost-user-bridge: 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/vhost-user-bridge.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/tests/vhost-user-bridge.c b/tests/vhost-user-bridge.c
index 9b1dab2f28..fecdf915e7 100644
--- a/tests/vhost-user-bridge.c
+++ b/tests/vhost-user-bridge.c
@@ -631,7 +631,6 @@ static void *notifier_thread(void *arg)
 static void
 vubr_host_notifier_setup(VubrDev *dev)
 {
-char template[] = "/tmp/vubr-XX";
 pthread_t thread;
 size_t length;
 void *addr;
@@ -639,7 +638,7 @@ vubr_host_notifier_setup(VubrDev *dev)
 
 length = qemu_real_host_page_size() * VHOST_USER_BRIDGE_MAX_QUEUES;
 
-fd = mkstemp(template);
+fd = g_file_open_tmp("vubr-XX", NULL, NULL);
 if (fd < 0) {
 vubr_die("mkstemp()");
 }
-- 
2.34.1




[PATCH v3 12/54] tests/qtest: hd-geo-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
- Use g_steal_pointer() in create_test_img()

 tests/qtest/hd-geo-test.c | 25 +++--
 1 file changed, 11 insertions(+), 14 deletions(-)

diff --git a/tests/qtest/hd-geo-test.c b/tests/qtest/hd-geo-test.c
index 413cf964c0..455bc5db5c 100644
--- a/tests/qtest/hd-geo-test.c
+++ b/tests/qtest/hd-geo-test.c
@@ -27,20 +27,19 @@
 
 static char *create_test_img(int secs)
 {
-char *template = strdup("/tmp/qtest.XX");
+g_autofree char *template = NULL;
 int fd, ret;
 
-fd = mkstemp(template);
+fd = g_file_open_tmp("qtest.XX", , NULL);
 g_assert(fd >= 0);
 ret = ftruncate(fd, (off_t)secs * 512);
 close(fd);
 
 if (ret) {
-free(template);
 template = NULL;
 }
 
-return template;
+return g_steal_pointer();
 }
 
 typedef struct {
@@ -422,9 +421,8 @@ static MBRpartitions empty_mbr = { {false, 0, 0, 0, 0, 0, 
0, 0, 0},
 
 static char *create_qcow2_with_mbr(MBRpartitions mbr, uint64_t sectors)
 {
-const char *template = "/tmp/qtest.XX";
-char *raw_path = strdup(template);
-char *qcow2_path = strdup(template);
+g_autofree char *raw_path = NULL;
+char *qcow2_path;
 char cmd[100 + 2 * PATH_MAX];
 uint8_t buf[512] = {};
 int i, ret, fd, offset;
@@ -468,7 +466,7 @@ static char *create_qcow2_with_mbr(MBRpartitions mbr, 
uint64_t sectors)
 offset += 0x10;
 }
 
-fd = mkstemp(raw_path);
+fd = g_file_open_tmp("qtest.XX", _path, NULL);
 g_assert(fd >= 0);
 close(fd);
 
@@ -478,7 +476,7 @@ static char *create_qcow2_with_mbr(MBRpartitions mbr, 
uint64_t sectors)
 g_assert(ret == sizeof(buf));
 close(fd);
 
-fd = mkstemp(qcow2_path);
+fd = g_file_open_tmp("qtest.XX", _path, NULL);
 g_assert(fd >= 0);
 close(fd);
 
@@ -506,7 +504,6 @@ static char *create_qcow2_with_mbr(MBRpartitions mbr, 
uint64_t sectors)
 free(qemu_img_abs_path);
 
 unlink(raw_path);
-free(raw_path);
 
 return qcow2_path;
 }
@@ -714,7 +711,7 @@ static void test_override(TestArgs *args, CHSResult 
expected[])
 
 for (i = 0; i < args->n_drives; i++) {
 unlink(args->drives[i]);
-free(args->drives[i]);
+g_free(args->drives[i]);
 }
 g_free(args->drives);
 g_strfreev(args->argv);
@@ -867,7 +864,7 @@ static void test_override_scsi_hot_unplug(void)
 
 for (i = 0; i < args->n_drives; i++) {
 unlink(args->drives[i]);
-free(args->drives[i]);
+g_free(args->drives[i]);
 }
 g_free(args->drives);
 g_strfreev(args->argv);
@@ -927,7 +924,7 @@ static void test_override_virtio_hot_unplug(void)
 
 for (i = 0; i < args->n_drives; i++) {
 unlink(args->drives[i]);
-free(args->drives[i]);
+g_free(args->drives[i]);
 }
 g_free(args->drives);
 g_strfreev(args->argv);
@@ -987,7 +984,7 @@ test_add_done:
 for (i = 0; i < backend_last; i++) {
 if (img_file_name[i]) {
 unlink(img_file_name[i]);
-free(img_file_name[i]);
+g_free(img_file_name[i]);
 }
 }
 
-- 
2.34.1




[PATCH v3 23/54] tests/unit: test-qga: 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_get_tmp_dir() for a portable implementation.

Signed-off-by: Bin Meng 
---

Changes in v3:
- Split to a separate patch

 tests/unit/test-qga.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tests/unit/test-qga.c b/tests/unit/test-qga.c
index b6ea7c7304..b4e0a14573 100644
--- a/tests/unit/test-qga.c
+++ b/tests/unit/test-qga.c
@@ -60,7 +60,7 @@ fixture_setup(TestFixture *fixture, gconstpointer data, gchar 
**envp)
 
 fixture->loop = g_main_loop_new(NULL, FALSE);
 
-fixture->test_dir = g_strdup("/tmp/qgatest.XX");
+fixture->test_dir = g_strdup_printf("%s/qgatest.XX", g_get_tmp_dir());
 g_assert_nonnull(g_mkdtemp(fixture->test_dir));
 
 path = g_build_filename(fixture->test_dir, "sock", NULL);
-- 
2.34.1




[PATCH v3 28/54] tests/qtest: Skip running virtio-net-test cases that require socketpair() for win32

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

Some of the virtio-net-test test cases require socketpair() to do the
test setup. Skip them for win32.

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

(no changes since v2)

Changes in v2:
- Change to skip only part of the virtio-net-test cases that require
  socketpair() intead of disabling all of them

 tests/qtest/virtio-net-test.c | 13 ++---
 1 file changed, 6 insertions(+), 7 deletions(-)

diff --git a/tests/qtest/virtio-net-test.c b/tests/qtest/virtio-net-test.c
index 6ded252901..dff43f0f60 100644
--- a/tests/qtest/virtio-net-test.c
+++ b/tests/qtest/virtio-net-test.c
@@ -165,8 +165,6 @@ static void stop_cont_test(void *obj, void *data, 
QGuestAllocator *t_alloc)
 rx_stop_cont_test(dev, t_alloc, rx, sv[0]);
 }
 
-#endif
-
 static void hotplug(void *obj, void *data, QGuestAllocator *t_alloc)
 {
 QVirtioPCIDevice *dev = obj;
@@ -286,6 +284,8 @@ static void *virtio_net_test_setup(GString *cmd_line, void 
*arg)
 return sv;
 }
 
+#endif /* _WIN32 */
+
 static void large_tx(void *obj, void *data, QGuestAllocator *t_alloc)
 {
 QVirtioNet *dev = obj;
@@ -319,16 +319,15 @@ static void *virtio_net_test_setup_nosocket(GString 
*cmd_line, void *arg)
 
 static void register_virtio_net_test(void)
 {
-QOSGraphTestOptions opts = {
-.before = virtio_net_test_setup,
-};
+QOSGraphTestOptions opts = { 0 };
 
-qos_add_test("hotplug", "virtio-net-pci", hotplug, );
 #ifndef _WIN32
+opts.before = virtio_net_test_setup;
+qos_add_test("hotplug", "virtio-net-pci", hotplug, );
 qos_add_test("basic", "virtio-net", send_recv_test, );
 qos_add_test("rx_stop_cont", "virtio-net", stop_cont_test, );
-#endif
 qos_add_test("announce-self", "virtio-net", announce_self, );
+#endif
 
 /* These tests do not need a loopback backend.  */
 opts.before = virtio_net_test_setup_nosocket;
-- 
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", _path, NULL);
 g_assert(fd >= 0);
 close(fd);
 
 /* Create a temporary raw image */
-fd = mkstemp(tmp_path);
+fd = g_file_open_tmp("qtest.XX", _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 08/54] tests/qtest: cxl-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_dir_make_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/cxl-test.c | 15 ++-
 1 file changed, 6 insertions(+), 9 deletions(-)

diff --git a/tests/qtest/cxl-test.c b/tests/qtest/cxl-test.c
index 2e14da7dee..cbe0fb549b 100644
--- a/tests/qtest/cxl-test.c
+++ b/tests/qtest/cxl-test.c
@@ -93,10 +93,9 @@ static void cxl_2root_port(void)
 static void cxl_t3d(void)
 {
 g_autoptr(GString) cmdline = g_string_new(NULL);
-char template[] = "/tmp/cxl-test-XX";
-const char *tmpfs;
+g_autofree const char *tmpfs = NULL;
 
-tmpfs = g_mkdtemp(template);
+tmpfs = g_dir_make_tmp("cxl-test-XX", NULL);
 
 g_string_printf(cmdline, QEMU_PXB_CMD QEMU_RP QEMU_T3D, tmpfs, tmpfs);
 
@@ -107,10 +106,9 @@ static void cxl_t3d(void)
 static void cxl_1pxb_2rp_2t3d(void)
 {
 g_autoptr(GString) cmdline = g_string_new(NULL);
-char template[] = "/tmp/cxl-test-XX";
-const char *tmpfs;
+g_autofree const char *tmpfs = NULL;
 
-tmpfs = g_mkdtemp(template);
+tmpfs = g_dir_make_tmp("cxl-test-XX", NULL);
 
 g_string_printf(cmdline, QEMU_PXB_CMD QEMU_2RP QEMU_2T3D,
 tmpfs, tmpfs, tmpfs, tmpfs);
@@ -122,10 +120,9 @@ static void cxl_1pxb_2rp_2t3d(void)
 static void cxl_2pxb_4rp_4t3d(void)
 {
 g_autoptr(GString) cmdline = g_string_new(NULL);
-char template[] = "/tmp/cxl-test-XX";
-const char *tmpfs;
+g_autofree const char *tmpfs = NULL;
 
-tmpfs = g_mkdtemp(template);
+tmpfs = g_dir_make_tmp("cxl-test-XX", NULL);
 
 g_string_printf(cmdline, QEMU_2PXB_CMD QEMU_4RP QEMU_4T3D,
 tmpfs, tmpfs, tmpfs, tmpfs, tmpfs, tmpfs,
-- 
2.34.1




[PATCH v3 21/54] tests/qtest: libqtest: Avoid using hardcoded /tmp

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

The qtest library was written to use hardcoded /tmp directory for
temporary files. Update to use g_get_tmp_dir() and g_dir_make_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/libqtest.c | 12 
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/tests/qtest/libqtest.c b/tests/qtest/libqtest.c
index 7c9fc07de4..d8ffa0e7b1 100644
--- a/tests/qtest/libqtest.c
+++ b/tests/qtest/libqtest.c
@@ -265,8 +265,10 @@ QTestState *qtest_init_without_qmp_handshake(const char 
*extra_args)
 
 s = g_new(QTestState, 1);
 
-socket_path = g_strdup_printf("/tmp/qtest-%d.sock", getpid());
-qmp_socket_path = g_strdup_printf("/tmp/qtest-%d.qmp", getpid());
+socket_path = g_strdup_printf("%s/qtest-%d.sock",
+  g_get_tmp_dir(), getpid());
+qmp_socket_path = g_strdup_printf("%s/qtest-%d.qmp",
+  g_get_tmp_dir(), getpid());
 
 /* It's possible that if an earlier test run crashed it might
  * have left a stale unix socket lying around. Delete any
@@ -390,10 +392,12 @@ QTestState *qtest_initf(const char *fmt, ...)
 QTestState *qtest_init_with_serial(const char *extra_args, int *sock_fd)
 {
 int sock_fd_init;
-char *sock_path, sock_dir[] = "/tmp/qtest-serial-XX";
+g_autofree char *sock_dir = NULL;
+char *sock_path;
 QTestState *qts;
 
-g_assert_true(g_mkdtemp(sock_dir) != NULL);
+sock_dir = g_dir_make_tmp("qtest-serial-XX", NULL);
+g_assert_true(sock_dir != NULL);
 sock_path = g_strdup_printf("%s/sock", sock_dir);
 
 sock_fd_init = init_socket(sock_path);
-- 
2.34.1




[PATCH v3 14/54] tests/qtest: migration-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_dir_make_tmp() for a portable implementation.

Signed-off-by: Bin Meng 
---

Changes in v3:
- Split to a separate patch

 tests/qtest/migration-test.c | 7 ---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/tests/qtest/migration-test.c b/tests/qtest/migration-test.c
index 4728d528bb..029c4f3a6a 100644
--- a/tests/qtest/migration-test.c
+++ b/tests/qtest/migration-test.c
@@ -2434,7 +2434,6 @@ static bool kvm_dirty_ring_supported(void)
 
 int main(int argc, char **argv)
 {
-char template[] = "/tmp/migration-test-XX";
 const bool has_kvm = qtest_has_accel("kvm");
 const bool has_uffd = ufd_version_check();
 const char *arch = qtest_get_arch();
@@ -2462,9 +2461,10 @@ int main(int argc, char **argv)
 return g_test_run();
 }
 
-tmpfs = g_mkdtemp(template);
+tmpfs = g_dir_make_tmp("migration-test-XX", NULL);
 if (!tmpfs) {
-g_test_message("g_mkdtemp on path (%s): %s", template, 
strerror(errno));
+g_test_message("g_dir_make_tmp on path (%s): %s", tmpfs,
+   strerror(errno));
 }
 g_assert(tmpfs);
 
@@ -2589,6 +2589,7 @@ int main(int argc, char **argv)
 g_test_message("unable to rmdir: path (%s): %s",
tmpfs, strerror(errno));
 }
+g_free((gpointer)tmpfs);
 
 return ret;
 }
-- 
2.34.1




[PATCH v3 16/54] tests/qtest: qmp-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_dir_make_tmp() for a portable implementation.

Signed-off-by: Bin Meng 
---

Changes in v3:
- Split to a separate patch

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

diff --git a/tests/qtest/qmp-test.c b/tests/qtest/qmp-test.c
index bf7304c7dc..0fa00c12dc 100644
--- a/tests/qtest/qmp-test.c
+++ b/tests/qtest/qmp-test.c
@@ -161,12 +161,13 @@ static void test_qmp_protocol(void)
 
 /* Out-of-band tests */
 
-char tmpdir[] = "/tmp/qmp-test-XX";
+char *tmpdir;
 char *fifo_name;
 
 static void setup_blocking_cmd(void)
 {
-if (!g_mkdtemp(tmpdir)) {
+tmpdir = g_dir_make_tmp("qmp-test-XX", NULL);
+if (!tmpdir) {
 g_error("g_mkdtemp: %s", strerror(errno));
 }
 fifo_name = g_strdup_printf("%s/fifo", tmpdir);
@@ -179,6 +180,7 @@ static void cleanup_blocking_cmd(void)
 {
 unlink(fifo_name);
 rmdir(tmpdir);
+g_free(tmpdir);
 }
 
 static void send_cmd_that_blocks(QTestState *s, const char *id)
-- 
2.34.1




[PATCH v3 06/54] tests/qtest: aspeed_smc-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/aspeed_smc-test.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/tests/qtest/aspeed_smc-test.c b/tests/qtest/aspeed_smc-test.c
index 05ce941566..c713a3700b 100644
--- a/tests/qtest/aspeed_smc-test.c
+++ b/tests/qtest/aspeed_smc-test.c
@@ -608,16 +608,15 @@ static void test_write_block_protect_bottom_bit(void)
 flash_reset();
 }
 
-static char tmp_path[] = "/tmp/qtest.m25p80.XX";
-
 int main(int argc, char **argv)
 {
+g_autofree char *tmp_path = NULL;
 int ret;
 int fd;
 
 g_test_init(, , NULL);
 
-fd = mkstemp(tmp_path);
+fd = g_file_open_tmp("qtest.m25p80.XX", _path, NULL);
 g_assert(fd >= 0);
 ret = ftruncate(fd, FLASH_SIZE);
 g_assert(ret == 0);
-- 
2.34.1




[PATCH v3 22/54] tests/unit: test-image-locking: 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/unit/test-image-locking.c | 8 
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/tests/unit/test-image-locking.c b/tests/unit/test-image-locking.c
index ba057bd66c..a47299c247 100644
--- a/tests/unit/test-image-locking.c
+++ b/tests/unit/test-image-locking.c
@@ -76,10 +76,10 @@ static void check_locked_bytes(int fd, uint64_t perm_locks,
 static void test_image_locking_basic(void)
 {
 BlockBackend *blk1, *blk2, *blk3;
-char img_path[] = "/tmp/qtest.XX";
+g_autofree char *img_path = NULL;
 uint64_t perm, shared_perm;
 
-int fd = mkstemp(img_path);
+int fd = g_file_open_tmp("qtest.XX", _path, NULL);
 assert(fd >= 0);
 
 perm = BLK_PERM_WRITE | BLK_PERM_CONSISTENT_READ;
@@ -117,10 +117,10 @@ static void test_image_locking_basic(void)
 static void test_set_perm_abort(void)
 {
 BlockBackend *blk1, *blk2;
-char img_path[] = "/tmp/qtest.XX";
+g_autofree char *img_path = NULL;
 uint64_t perm, shared_perm;
 int r;
-int fd = mkstemp(img_path);
+int fd = g_file_open_tmp("qtest.XX", _path, NULL);
 assert(fd >= 0);
 
 perm = BLK_PERM_WRITE | BLK_PERM_CONSISTENT_READ;
-- 
2.34.1




[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", _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 04/54] util/qemu-sockets: Use g_get_tmp_dir() to get the directory for temporary files

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

Replace the existing logic to get the directory for temporary files
with g_get_tmp_dir(), which works for win32 too.

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

(no changes since v1)

 util/qemu-sockets.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/util/qemu-sockets.c b/util/qemu-sockets.c
index 83f4bd6fd2..0c41ca9e42 100644
--- a/util/qemu-sockets.c
+++ b/util/qemu-sockets.c
@@ -919,9 +919,8 @@ static int unix_listen_saddr(UnixSocketAddress *saddr,
 if (saddr->path[0] || abstract) {
 path = saddr->path;
 } else {
-const char *tmpdir = getenv("TMPDIR");
-tmpdir = tmpdir ? tmpdir : "/tmp";
-path = pathbuf = g_strdup_printf("%s/qemu-socket-XX", tmpdir);
+path = pathbuf = g_strdup_printf("%s/qemu-socket-XX",
+ g_get_tmp_dir());
 }
 
 pathlen = strlen(path);
-- 
2.34.1




[PATCH v3 03/54] tcg: Avoid using hardcoded /tmp

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

Use g_get_tmp_dir() to get the directory to use for temporary files.

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

(no changes since v2)

Changes in v2:
- Use g_autofree to declare the variable

 tcg/tcg.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/tcg/tcg.c b/tcg/tcg.c
index 0f9cfe96f2..8847053176 100644
--- a/tcg/tcg.c
+++ b/tcg/tcg.c
@@ -4729,7 +4729,8 @@ static void tcg_register_jit_int(const void *buf_ptr, 
size_t buf_size,
 /* Enable this block to be able to debug the ELF image file creation.
One can use readelf, objdump, or other inspection utilities.  */
 {
-FILE *f = fopen("/tmp/qemu.jit", "w+b");
+g_autofree char *jit = g_strdup_printf("%s/qemu.jit", g_get_tmp_dir());
+FILE *f = fopen(jit, "w+b");
 if (f) {
 if (fwrite(img, img_size, 1, f) != img_size) {
 /* Avoid stupid unused return value warning for fwrite.  */
-- 
2.34.1




[PATCH v3 07/54] tests/qtest: boot-serial-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/boot-serial-test.c | 8 
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/tests/qtest/boot-serial-test.c b/tests/qtest/boot-serial-test.c
index 2f99d71cab..72310ba30e 100644
--- a/tests/qtest/boot-serial-test.c
+++ b/tests/qtest/boot-serial-test.c
@@ -224,14 +224,14 @@ static bool check_guest_output(QTestState *qts, const 
testdef_t *test, int fd)
 static void test_machine(const void *data)
 {
 const testdef_t *test = data;
-char serialtmp[] = "/tmp/qtest-boot-serial-sXX";
-char codetmp[] = "/tmp/qtest-boot-serial-cXX";
+g_autofree char *serialtmp = NULL;
+g_autofree char *codetmp = NULL;
 const char *codeparam = "";
 const uint8_t *code = NULL;
 QTestState *qts;
 int ser_fd;
 
-ser_fd = mkstemp(serialtmp);
+ser_fd = g_file_open_tmp("qtest-boot-serial-sXX", , NULL);
 g_assert(ser_fd != -1);
 
 if (test->kernel) {
@@ -246,7 +246,7 @@ static void test_machine(const void *data)
 ssize_t wlen;
 int code_fd;
 
-code_fd = mkstemp(codetmp);
+code_fd = g_file_open_tmp("qtest-boot-serial-cXX", , NULL);
 g_assert(code_fd != -1);
 wlen = write(code_fd, code, test->codesize);
 g_assert(wlen == test->codesize);
-- 
2.34.1




  1   2   >