From 07f338ed70df2b57414d4bc688a00b06afc14d0a Mon Sep 17 00:00:00 2001
From: Ekaitz Zarraga <ekaitz@elenq.tech>
Date: Thu, 21 Dec 2023 18:12:19 +0100
Subject: qemu: add patches

---
 qemu-build-info-manual.patch              | 62 +++++++++++++++++++++++++++++
 qemu-disable-aarch64-migration-test.patch | 13 +++++++
 qemu-fix-agent-paths.patch                | 65 +++++++++++++++++++++++++++++++
 qemu-glibc-2.27.patch                     | 55 ++++++++++++++++++++++++++
 qemu-glibc-2.30.patch                     | 57 +++++++++++++++++++++++++++
 5 files changed, 252 insertions(+)
 create mode 100644 qemu-build-info-manual.patch
 create mode 100644 qemu-disable-aarch64-migration-test.patch
 create mode 100644 qemu-fix-agent-paths.patch
 create mode 100644 qemu-glibc-2.27.patch
 create mode 100644 qemu-glibc-2.30.patch

diff --git a/qemu-build-info-manual.patch b/qemu-build-info-manual.patch
new file mode 100644
index 0000000..ff28266
--- /dev/null
+++ b/qemu-build-info-manual.patch
@@ -0,0 +1,62 @@
+From 2793f47c066ed396b38893c10533202fceb1a05f Mon Sep 17 00:00:00 2001
+From: Maxim Cournoyer <maxim.cournoyer@gmail.com>
+Date: Thu, 17 Sep 2020 13:28:19 -0400
+Subject: [PATCH] build: Build and install a Texinfo version of the manual.
+
+Take advantage of the Sphinx texinfo backend to generate a QEMU info
+manual.  The Texinfo format allows for more structure and info readers
+provide more advanced navigation capabilities compared to manpages
+readers.
+
+* docs/meson.build (texi, info): New targets.
+
+Signed-off-by: Maxim Cournoyer <maxim.cournoyer@gmail.com>
+
+diff --git a/docs/meson.build b/docs/meson.build
+--- a/docs/meson.build
++++ b/docs/meson.build
+@@ -92,4 +92,25 @@ if build_docs
+   alias_target('sphinxdocs', sphinxdocs)
+   alias_target('html', sphinxdocs)
+   alias_target('man', sphinxmans)
++
++  # Generate a Texinfo version of the QEMU manual.
++  makeinfo = find_program(['texi2any', 'makeinfo'])
++  if makeinfo.found()
++    sphinxtexi = custom_target(
++      'QEMU manual generated texinfo source',
++      output: ['QEMU.texi', 'sphinxtexi.stamp'],
++      depfile: 'sphinxtexi.d',
++      command: [SPHINX_ARGS, '-Ddepfile=@DEPFILE@',
++               '-Ddepfile_stamp=@OUTPUT1@', '-b', 'texinfo',
++               meson.current_source_dir(), meson.current_build_dir()])
++    sphinxinfo = custom_target(
++      'QEMU info manual',
++      input: sphinxtexi,
++      output: 'QEMU.info',
++      install: true,
++      install_dir: get_option('infodir'),
++      command: [makeinfo, '--no-split', '@INPUT0@', '--output=@OUTPUT@'])
++    alias_target('texi', sphinxtexi)
++    alias_target('info', sphinxinfo)
++  endif
+ endif
+diff --git a/meson.build b/meson.build
+--- a/meson.build
++++ b/meson.build
+@@ -37,6 +37,7 @@ endif
+ qemu_confdir = get_option('sysconfdir') / get_option('qemu_suffix')
+ qemu_datadir = get_option('datadir') / get_option('qemu_suffix')
+ qemu_docdir = get_option('docdir') / get_option('qemu_suffix')
++qemu_infodir = get_option('infodir') / get_option('qemu_suffix')
+ qemu_moddir = get_option('libdir') / get_option('qemu_suffix')
+ 
+ qemu_desktopdir = get_option('datadir') / 'applications'
+@@ -3698,6 +3699,7 @@ else
+   summary_info += {'local state directory': 'queried at runtime'}
+ endif
+ summary_info += {'Doc directory':     get_option('prefix') / get_option('docdir')}
++summary_info += {'Info directory':    get_option('prefix') / get_option('infodir')}
+ summary_info += {'Build directory':   meson.current_build_dir()}
+ summary_info += {'Source path':       meson.current_source_dir()}
+ summary_info += {'GIT submodules':    config_host['GIT_SUBMODULES']}
diff --git a/qemu-disable-aarch64-migration-test.patch b/qemu-disable-aarch64-migration-test.patch
new file mode 100644
index 0000000..bf60ed2
--- /dev/null
+++ b/qemu-disable-aarch64-migration-test.patch
@@ -0,0 +1,13 @@
+Disable the qtest-aarch64/migration-test, which sometimes fail non-deterministically.
+See: https://gitlab.com/qemu-project/qemu/-/issues/1230.
+
+--- qemu-7.1.0/tests/qtest/meson.build.old	1969-12-31 19:00:01.000000000 -0500
++++ qemu-7.1.0/tests/qtest/meson.build	2022-09-26 11:11:05.434209797 -0400
+@@ -219,7 +219,6 @@
+   ['arm-cpu-features',
+    'numa-test',
+    'boot-serial-test',
+-   'migration-test',
+    'bcm2835-dma-test']
+ 
+ qtests_s390x = \
diff --git a/qemu-fix-agent-paths.patch b/qemu-fix-agent-paths.patch
new file mode 100644
index 0000000..0c29890
--- /dev/null
+++ b/qemu-fix-agent-paths.patch
@@ -0,0 +1,65 @@
+Allow a QEMU host to set the time and shutdown Guix guests.  Styled
+after the patch from the Nix package:
+
+https://github.com/NixOS/nixpkgs/blob/master/pkgs/applications/virtualization/qemu/fix-qemu-ga.patch
+
+diff --git a/qga/commands-posix.c b/qga/commands-posix.c
+--- a/qga/commands-posix.c
++++ b/qga/commands-posix.c
+@@ -84,6 +84,7 @@ static void ga_wait_child(pid_t pid, int *status, Error **errp)
+ void qmp_guest_shutdown(bool has_mode, const char *mode, Error **errp)
+ {
+     const char *shutdown_flag;
++    const char *command;
+     Error *local_err = NULL;
+     pid_t pid;
+     int status;
+@@ -101,10 +102,13 @@ void qmp_guest_shutdown(bool has_mode, const char *mode, Error **errp)
+     slog("guest-shutdown called, mode: %s", mode);
+     if (!has_mode || strcmp(mode, "powerdown") == 0) {
+         shutdown_flag = powerdown_flag;
++        command = "shutdown";
+     } else if (strcmp(mode, "halt") == 0) {
+         shutdown_flag = halt_flag;
++        command = "halt";
+     } else if (strcmp(mode, "reboot") == 0) {
+         shutdown_flag = reboot_flag;
++        command = "reboot";
+     } else {
+         error_setg(errp,
+                    "mode is invalid (valid values are: halt|powerdown|reboot");
+@@ -123,6 +127,11 @@ void qmp_guest_shutdown(bool has_mode, const char *mode, Error **errp)
+         execl("/sbin/shutdown", "shutdown", shutdown_flag, "-g0", "-y",
+               "hypervisor initiated shutdown", (char *)NULL);
+ #else
++        /* try Guix’s shutdown/halt/reboot first */
++        char *path = g_strdup_printf("/run/current-system/profile/sbin/%s", command);
++        execl(path, command, (char *)NULL);
++        g_free(path);
++
+         execl("/sbin/shutdown", "shutdown", "-h", shutdown_flag, "+0",
+                "hypervisor initiated shutdown", (char *)NULL);
+ #endif
+@@ -159,10 +168,12 @@ void qmp_guest_set_time(bool has_time, int64_t time_ns, Error **errp)
+     Error *local_err = NULL;
+     struct timeval tv;
+     static const char hwclock_path[] = "/sbin/hwclock";
++    static const char hwclock_path_guix[] = "/run/current-system/profile/sbin/hwclock";
+     static int hwclock_available = -1;
+ 
+     if (hwclock_available < 0) {
+-        hwclock_available = (access(hwclock_path, X_OK) == 0);
++        hwclock_available = (access(hwclock_path_guix, X_OK) == 0) ||
++                            (access(hwclock_path, X_OK) == 0);
+     }
+ 
+     if (!hwclock_available) {
+@@ -208,6 +219,8 @@ void qmp_guest_set_time(bool has_time, int64_t time_ns, Error **errp)
+ 
+         /* Use '/sbin/hwclock -w' to set RTC from the system time,
+          * or '/sbin/hwclock -s' to set the system time from RTC. */
++        execl(hwclock_path_guix, "hwclock", has_time ? "-w" : "-s",
++               NULL);
+         execl(hwclock_path, "hwclock", has_time ? "-w" : "-s", NULL);
+         _exit(EXIT_FAILURE);
+     } else if (pid < 0) {
diff --git a/qemu-glibc-2.27.patch b/qemu-glibc-2.27.patch
new file mode 100644
index 0000000..b1f354a
--- /dev/null
+++ b/qemu-glibc-2.27.patch
@@ -0,0 +1,55 @@
+From 75e5b70e6b5dcc4f2219992d7cffa462aa406af0 Mon Sep 17 00:00:00 2001
+From: Paolo Bonzini <pbonzini@redhat.com>
+Date: Tue, 28 Nov 2017 11:51:27 +0100
+Subject: [PATCH] memfd: fix configure test
+MIME-Version: 1.0
+Content-Type: text/plain; charset=utf8
+Content-Transfer-Encoding: 8bit
+
+Recent glibc added memfd_create in sys/mman.h.  This conflicts with
+the definition in util/memfd.c:
+
+    /builddir/build/BUILD/qemu-2.11.0-rc1/util/memfd.c:40:12: error: static declaration of memfd_create follows non-static declaration
+
+Fix the configure test, and remove the sys/memfd.h inclusion since the
+file actually does not exist---it is a typo in the memfd_create(2) man
+page.
+
+Cc: Marc-André Lureau <marcandre.lureau@redhat.com>
+Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
+---
+ configure    | 2 +-
+ util/memfd.c | 4 +---
+ 2 files changed, 2 insertions(+), 4 deletions(-)
+
+diff --git a/configure b/configure
+index 9c8aa5a..99ccc17 100755
+--- a/configure
++++ b/configure
+@@ -3923,7 +3923,7 @@ fi
+ # check if memfd is supported
+ memfd=no
+ cat > $TMPC << EOF
+-#include <sys/memfd.h>
++#include <sys/mman.h>
+ 
+ int main(void)
+ {
+diff --git a/util/memfd.c b/util/memfd.c
+index 4571d1a..412e94a 100644
+--- a/util/memfd.c
++++ b/util/memfd.c
+@@ -31,9 +31,7 @@
+ 
+ #include "qemu/memfd.h"
+ 
+-#ifdef CONFIG_MEMFD
+-#include <sys/memfd.h>
+-#elif defined CONFIG_LINUX
++#if defined CONFIG_LINUX && !defined CONFIG_MEMFD
+ #include <sys/syscall.h>
+ #include <asm/unistd.h>
+ 
+-- 
+1.8.3.1
+
diff --git a/qemu-glibc-2.30.patch b/qemu-glibc-2.30.patch
new file mode 100644
index 0000000..1b74dee
--- /dev/null
+++ b/qemu-glibc-2.30.patch
@@ -0,0 +1,57 @@
+This patch was taken from NixOS
+https://raw.githubusercontent.com/Mindavi/nixpkgs/1a737743a829746e48f4869ac517ff29c23c9d09/pkgs/tools/security/afl/qemu-patches/syscall-glibc2_30.diff
+It is based on an unmerged patch against american-fuzzy-lop and was
+never merged upstream because the author was unable to sign Google's CLA.
+Based on https://github.com/google/AFL/commit/6c917e3d63a2a0685d58c3518524f9615b001893.patch
+
+--- qemu-2.10.0-clean/linux-user/syscall.c	2020-03-12 18:47:47.898592169 +0100
++++ qemu-2.10.0/linux-user/syscall.c	2020-03-13 09:13:42.461809699 +0100
+@@ -34,6 +34,7 @@
+ #include <sys/resource.h>
+ #include <sys/swap.h>
+ #include <linux/capability.h>
++#include <linux/sockios.h> // https://lkml.org/lkml/2019/6/3/988
+ #include <sched.h>
+ #include <sys/timex.h>
+ #ifdef __ia64__
+@@ -256,7 +257,9 @@ static type name (type1 arg1,type2 arg2,
+ #endif
+ 
+ #ifdef __NR_gettid
+-_syscall0(int, gettid)
++// taken from https://patchwork.kernel.org/patch/10862231/
++#define __NR_sys_gettid __NR_gettid
++_syscall0(int, sys_gettid)
+ #else
+ /* This is a replacement for the host gettid() and must return a host
+    errno. */
+@@ -6219,7 +6222,7 @@ static void *clone_func(void *arg)
+     cpu = ENV_GET_CPU(env);
+     thread_cpu = cpu;
+     ts = (TaskState *)cpu->opaque;
+-    info->tid = gettid();
++    info->tid = sys_gettid();
+     task_settid(ts);
+     if (info->child_tidptr)
+         put_user_u32(info->tid, info->child_tidptr);
+@@ -6363,9 +6366,9 @@ static int do_fork(CPUArchState *env, un
+                mapping.  We can't repeat the spinlock hack used above because
+                the child process gets its own copy of the lock.  */
+             if (flags & CLONE_CHILD_SETTID)
+-                put_user_u32(gettid(), child_tidptr);
++                put_user_u32(sys_gettid(), child_tidptr);
+             if (flags & CLONE_PARENT_SETTID)
+-                put_user_u32(gettid(), parent_tidptr);
++                put_user_u32(sys_gettid(), parent_tidptr);
+             ts = (TaskState *)cpu->opaque;
+             if (flags & CLONE_SETTLS)
+                 cpu_set_tls (env, newtls);
+@@ -11402,7 +11405,7 @@ abi_long do_syscall(void *cpu_env, int n
+         break;
+ #endif
+     case TARGET_NR_gettid:
+-        ret = get_errno(gettid());
++        ret = get_errno(sys_gettid());
+         break;
+ #ifdef TARGET_NR_readahead
+     case TARGET_NR_readahead:
-- 
cgit v1.2.3