Skip to contentSkip to conversation

bsd-user: Misc changes for 9.1 (I hope)

PATCH In review Warner Losh 4 participants 34 messages Jul 22, 2024 – Jul 23, 2024

lore
Share & tools
lore
#2 lore raw

PATCH 2/14 bsd-user:Add AArch64 register handling and related functions

In reply to Warner Losh

From: Stacey Son <[email protected]>

Added header file for managing CPU register states in FreeBSD user mode.
Introduced prototypes for setting and getting thread-local storage (TLS).
Implemented AArch64 sysarch() system call emulation and a printing function.
Added function for setting up thread upcall to add thread support to BSD-USER. Initialized thread's register state during thread setup.
Updated ARM AArch64 VM parameter definitions for bsd-user, including address spaces for FreeBSD/arm64 and a function for getting the stack pointer from CPU and setting a return value.

Message-Id: <[email protected]>

---
 bsd-user/aarch64/target_arch.h         | 28 +++++++++++
 bsd-user/aarch64/target_arch_reg.h     | 56 +++++++++++++++++++++
 bsd-user/aarch64/target_arch_sysarch.h | 42 ++++++++++++++++
 bsd-user/aarch64/target_arch_thread.h  | 61 +++++++++++++++++++++++
 bsd-user/aarch64/target_arch_vmparam.h | 68 ++++++++++++++++++++++++++
 5 files changed, 255 insertions(+)
 create mode 100644 bsd-user/aarch64/target_arch.h
 create mode 100644 bsd-user/aarch64/target_arch_reg.h
 create mode 100644 bsd-user/aarch64/target_arch_sysarch.h
 create mode 100644 bsd-user/aarch64/target_arch_thread.h
 create mode 100644 bsd-user/aarch64/target_arch_vmparam.h
bsd-user/aarch64/target_arch.h new file +28 -0
new file mode 100644
index 00000000000..27f47de8eb3
@@ -0,0 +1,28 @@
+/*
+ * ARM AArch64 specific prototypes for bsd-user
+ *
+ * Copyright (c) 2015 Stacey D. Son <sson at FreeBSD>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef TARGET_ARCH_H
+#define TARGET_ARCH_H
+
+#include "qemu.h"
+
+void target_cpu_set_tls(CPUARMState *env, target_ulong newtls);
+target_ulong target_cpu_get_tls(CPUARMState *env);
+
+#endif /* TARGET_ARCH_H */
bsd-user/aarch64/target_arch_reg.h new file +56 -0

56 lines not on this page · raw message

bsd-user/aarch64/target_arch_sysarch.h new file +42 -0

42 lines not on this page · raw message

bsd-user/aarch64/target_arch_thread.h new file +61 -0

61 lines not on this page · raw message

bsd-user/aarch64/target_arch_vmparam.h new file +68 -0

68 lines not on this page · raw message

Signature · 1 line
2.45.1
#7 lore raw

PATCH 7/14 bsd-user:Add set_mcontext function for ARM AArch64

In reply to Warner Losh

From: Stacey Son <[email protected]>

The function copies register values from the provided target_mcontext_t structure to the CPUARMState registers.
Note:FP is unfinished upstream but will be a separate commit coming soon.

Message-Id: <[email protected]>

---
 bsd-user/aarch64/signal.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
bsd-user/aarch64/signal.c +22 -0
index 43c886e6036..13faac8ce60 100644
@@ -95,3 +95,25 @@ abi_long setup_sigframe_arch(CPUARMState *env, abi_ulong frame_addr,
     return 0;
 }
 
+/*
+ * Compare to set_mcontext() in arm64/arm64/machdep.c
+ * Assumes that the memory is locked if frame points to user memory.
+ */
+abi_long set_mcontext(CPUARMState *regs, target_mcontext_t *mcp, int srflag)
+{
+    int err = 0, i;
+    const uint64_t *gr = mcp->mc_gpregs.gp_x;
+
+    for (i = 0; i < 30; i++) {
+        regs->xregs[i] = tswap64(gr[i]);
+    }
+
+    regs->xregs[TARGET_REG_SP] = tswap64(mcp->mc_gpregs.gp_sp);
+    regs->xregs[TARGET_REG_LR] = tswap64(mcp->mc_gpregs.gp_lr);
+    regs->pc = mcp->mc_gpregs.gp_elr;
+    pstate_write(regs, mcp->mc_gpregs.gp_spsr);
+
+    /* XXX FP? */
+
+    return err;
+}
Signature · 1 line
2.45.1
#8 lore raw

PATCH 8/14 bsd-user:Add AArch64 improvements and signal handling functions

In reply to Warner Losh

From: Stacey Son <[email protected]>

Added get_ucontext_sigreturn function to check processor state ensuring current execution mode is EL0 and no flags indicating interrupts or exceptions are set.
Updated AArch64 code to use CF directly without reading/writing the entire processor state, improving efficiency. Changed FP data structures to use Int128 instead of __uint128_t, leveraging QEMU's generic mechanism for referencing this type.

Message-Id: <[email protected]>

---
 bsd-user/aarch64/signal.c             | 20 +++++++++++++++++++-
 bsd-user/aarch64/target_arch_cpu.h    |  7 ++-----
 bsd-user/aarch64/target_arch_reg.h    |  2 +-
 bsd-user/aarch64/target_arch_signal.h |  2 +-
 bsd-user/qemu.h                       |  3 +++
 5 files changed, 26 insertions(+), 8 deletions(-)
bsd-user/aarch64/signal.c +19 -1
index 13faac8ce60..6bc73a798f3 100644
@@ -21,7 +21,7 @@
 #include "qemu.h"
 
 /*
- * Compare to sendsig() in sys/arm64/arm64/machdep.c
+ * Compare to sendsig() in sys/arm64/arm64/exec_machdep.c
  * Assumes that target stack frame memory is locked.
  */
 abi_long set_sigtramp_args(CPUARMState *regs, int sig,
@@ -117,3 +117,21 @@ abi_long set_mcontext(CPUARMState *regs, target_mcontext_t *mcp, int srflag)
 
     return err;
 }
+
+/* Compare to sys_sigreturn() in  arm64/arm64/machdep.c */
+abi_long get_ucontext_sigreturn(CPUARMState *regs, abi_ulong target_sf,
+                                abi_ulong *target_uc)
+{
+    uint32_t pstate = pstate_read(regs);
+
+    *target_uc = 0;
+
+    if ((pstate & PSTATE_M) != PSTATE_MODE_EL0t  ||
+        (pstate & (PSTATE_F | PSTATE_I | PSTATE_A | PSTATE_D)) != 0) {
+        return -TARGET_EINVAL;
+    }
+
+    *target_uc = target_sf;
+
+    return 0;
+}
bsd-user/aarch64/target_arch_cpu.h +2 -5
index 5c150bb7e9c..b288e0d069b 100644
@@ -48,7 +48,6 @@ static inline void target_cpu_loop(CPUARMState *env)
     CPUState *cs = env_cpu(env);
     int trapnr, ec, fsc, si_code, si_signo;
     uint64_t code, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8;
-    uint32_t pstate;
     abi_long ret;
 
     for (;;) {
@@ -88,18 +87,16 @@ static inline void target_cpu_loop(CPUARMState *env)
              * The carry bit is cleared for no error; set for error.
              * See arm64/arm64/vm_machdep.c cpu_set_syscall_retval()
              */
-            pstate = pstate_read(env);
             if (ret >= 0) {
-                pstate &= ~PSTATE_C;
+                env->CF = 0;
                 env->xregs[0] = ret;
             } else if (ret == -TARGET_ERESTART) {
                 env->pc -= 4;
                 break;
             } else if (ret != -TARGET_EJUSTRETURN) {
-                pstate |= PSTATE_C;
+                env->CF = 1;
                 env->xregs[0] = -ret;
             }
-            pstate_write(env, pstate);
             break;
 
         case EXCP_INTERRUPT:
bsd-user/aarch64/target_arch_reg.h +1 -1
index 5c7154f0c18..b53302e7f7a 100644
@@ -31,7 +31,7 @@ typedef struct target_reg {
 } target_reg_t;
 
 typedef struct target_fpreg {
-    __uint128_t     fp_q[32];
+    Int128          fp_q[32];
     uint32_t        fp_sr;
     uint32_t        fp_cr;
 } target_fpreg_t;
bsd-user/aarch64/target_arch_signal.h +1 -1
index df171733166..bff752a67ab 100644
@@ -49,7 +49,7 @@ struct target_gpregs {
 };
 
 struct target_fpregs {
-    __uint128_t fp_q[32];
+    Int128      fp_q[32];
     uint32_t    fp_sr;
     uint32_t    fp_cr;
     uint32_t    fp_flags;
bsd-user/qemu.h +3 -0
index 9d2fc7148eb..3736c417860 100644
@@ -17,6 +17,9 @@
 #ifndef QEMU_H
 #define QEMU_H
 
+#include <sys/param.h>
+
+#include "qemu/int128.h"
 #include "cpu.h"
 #include "qemu/units.h"
 #include "exec/cpu_ldst.h"
Signature · 1 line
2.45.1
#1 lore raw

PATCH 1/14 bsd-user:Add CPU initialization and management functions

In reply to Warner Losh

From: Stacey Son <[email protected]>

Added function to initialize ARM CPU and check if it supports 64-bit mode. Implemented CPU loop function to handle exceptions and emulate execution of instructions. Added function to clone CPU state to create a new thread.
Included AArch64 specific CPU functions for bsd-user to set and receive thread-local-storage value from the tpidr_el0 register.
Introduced structure for storing CPU register states for BSD-USER.

Message-Id: <[email protected]>

---
 bsd-user/aarch64/target_arch_cpu.c |  31 +++++
 bsd-user/aarch64/target_arch_cpu.h | 192 +++++++++++++++++++++++++++++
 bsd-user/aarch64/target_syscall.h  |  51 ++++++++
 3 files changed, 274 insertions(+)
 create mode 100644 bsd-user/aarch64/target_arch_cpu.c
 create mode 100644 bsd-user/aarch64/target_arch_cpu.h
 create mode 100644 bsd-user/aarch64/target_syscall.h
bsd-user/aarch64/target_arch_cpu.c new file +31 -0
new file mode 100644
index 00000000000..b2fa59efaf6
@@ -0,0 +1,31 @@
+/*
+ * ARM AArch64 specific CPU for bsd-user
+ *
+ * Copyright (c) 2015 Stacey Son
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+#include "qemu/osdep.h"
+#include "target_arch.h"
+
+/* See cpu_set_user_tls() in arm64/arm64/vm_machdep.c */
+void target_cpu_set_tls(CPUARMState *env, target_ulong newtls)
+{
+    env->cp15.tpidr_el[0] = newtls;
+}
+
+target_ulong target_cpu_get_tls(CPUARMState *env)
+{
+    return env->cp15.tpidr_el[0];
+}
bsd-user/aarch64/target_arch_cpu.h new file +192 -0

192 lines not on this page · raw message

bsd-user/aarch64/target_syscall.h new file +51 -0

51 lines not on this page · raw message

Signature · 1 line
2.45.1
#5 lore raw

PATCH 5/14 bsd-user:Add get_mcontext function for ARM AArch64

In reply to Warner Losh

From: Stacey Son <[email protected]>

function to retrieve machine context,it populates the provided target_mcontext_t structure with information from the CPUARMState registers.

Message-Id: <[email protected]>

---
 bsd-user/aarch64/signal.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
bsd-user/aarch64/signal.c +30 -0
index 98861f9ab3b..ab3bf8558ab 100644
@@ -51,3 +51,33 @@ abi_long set_sigtramp_args(CPUARMState *regs, int sig,
 
     return 0;
 }
+
+/*
+ * Compare to get_mcontext() in arm64/arm64/machdep.c
+ * Assumes that the memory is locked if mcp points to user memory.
+ */
+abi_long get_mcontext(CPUARMState *regs, target_mcontext_t *mcp, int flags)
+{
+    int err = 0, i;
+    uint64_t *gr = mcp->mc_gpregs.gp_x;
+
+    mcp->mc_gpregs.gp_spsr = pstate_read(regs);
+    if (flags & TARGET_MC_GET_CLEAR_RET) {
+        gr[0] = 0UL;
+        mcp->mc_gpregs.gp_spsr &= ~CPSR_C;
+    } else {
+        gr[0] = tswap64(regs->xregs[0]);
+    }
+
+    for (i = 1; i < 30; i++) {
+        gr[i] = tswap64(regs->xregs[i]);
+    }
+
+    mcp->mc_gpregs.gp_sp = tswap64(regs->xregs[TARGET_REG_SP]);
+    mcp->mc_gpregs.gp_lr = tswap64(regs->xregs[TARGET_REG_LR]);
+    mcp->mc_gpregs.gp_elr = tswap64(regs->pc);
+
+    /* XXX FP? */
+
+    return err;
+}
Signature · 1 line
2.45.1
#0 lore raw

cover bsd-user: Misc changes for 9.1 (I hope)

This patch series includes two main sets of patches. To make it simple to review, I've included the changes from my student which the later changes depend on. I've included a change from Jessica and Doug as well. I've reviewed them, but more eyes never hurt.

I've also included a number of 'touch up' patches needed either to get the aarch64 building, or to implmement suggestions from prior review cycles. The main one is what's charitably described as a kludge: force aarch64 to use 4k pages. The qemu-project (and blitz branch) hasn't had the necessary changes to bsd-user needed to support variable page size.

Sorry this is so late... Live has conspired to delay me.

Doug Rabson (1):
bsd-user: Simplify the implementation of execve

Jessica Clarke (1):
bsd-user: Sync fork_start/fork_end with linux-user

Stacey Son (6):
bsd-user:Add CPU initialization and management functions bsd-user:Add AArch64 register handling and related functions bsd-user:Add ARM AArch64 signal handling support bsd-user:Add get_mcontext function for ARM AArch64 bsd-user:Add set_mcontext function for ARM AArch64 bsd-user:Add AArch64 improvements and signal handling functions

Warner Losh (6):
bsd-user:Add ARM AArch64 support and capabilities bsd-user:Add setup_sigframe_arch function for ARM AArch64
bsd-user: Hard wire aarch64 to be 4k pages only
bsd-user: Define TARGET_SIGSTACK_ALIGN and use it to round stack
bsd-user: Make compile for non-linux user-mode stuff
bsd-user: Add aarch64 build to tree

 bsd-user/aarch64/signal.c               | 137 +++++++++++++++++
 bsd-user/aarch64/target_arch.h          |  29 ++++
 bsd-user/aarch64/target_arch_cpu.c      |  31 ++++
 bsd-user/aarch64/target_arch_cpu.h      | 189 ++++++++++++++++++++++++
 bsd-user/aarch64/target_arch_elf.h      | 163 ++++++++++++++++++++
 bsd-user/aarch64/target_arch_reg.h      |  56 +++++++
 bsd-user/aarch64/target_arch_signal.h   |  82 ++++++++++
 bsd-user/aarch64/target_arch_sigtramp.h |  48 ++++++
 bsd-user/aarch64/target_arch_sysarch.h  |  42 ++++++
 bsd-user/aarch64/target_arch_thread.h   |  61 ++++++++
 bsd-user/aarch64/target_arch_vmparam.h  |  74 ++++++++++
 bsd-user/aarch64/target_syscall.h       |  51 +++++++
 bsd-user/arm/target_arch_signal.h       |   2 +
 bsd-user/freebsd/os-proc.c              | 118 +--------------
 bsd-user/i386/target_arch_signal.h      |   2 +
 bsd-user/main.c                         |  46 ++----
 bsd-user/qemu.h                         |   3 +
 bsd-user/signal.c                       |   9 +-
 bsd-user/x86_64/target_arch_signal.h    |   2 +
 configs/targets/aarch64-bsd-user.mak    |   3 +
 target/arm/cpu-param.h                  |   4 +
 target/arm/gdbstub64.c                  |   4 +
 22 files changed, 1001 insertions(+), 155 deletions(-)
 create mode 100644 bsd-user/aarch64/signal.c
 create mode 100644 bsd-user/aarch64/target_arch.h
 create mode 100644 bsd-user/aarch64/target_arch_cpu.c
 create mode 100644 bsd-user/aarch64/target_arch_cpu.h
 create mode 100644 bsd-user/aarch64/target_arch_elf.h
 create mode 100644 bsd-user/aarch64/target_arch_reg.h
 create mode 100644 bsd-user/aarch64/target_arch_signal.h
 create mode 100644 bsd-user/aarch64/target_arch_sigtramp.h
 create mode 100644 bsd-user/aarch64/target_arch_sysarch.h
 create mode 100644 bsd-user/aarch64/target_arch_thread.h
 create mode 100644 bsd-user/aarch64/target_arch_vmparam.h
 create mode 100644 bsd-user/aarch64/target_syscall.h
 create mode 100644 configs/targets/aarch64-bsd-user.mak
Signature · 1 line
2.45.1
#3 lore raw

PATCH 3/14 bsd-user:Add ARM AArch64 support and capabilities

Added function to access rval2 by accessing the x1 register.
Defined ARM AArch64 ELF parameters including mmap and dynamic load addresses.
Introduced extensive hardware capability definitions and macros for retrieving hardware capability (hwcap) flags. Implemented function to retrieve ARM AArch64 hardware capabilities using the GET_FEATURE_ID macro. Added function to retrieve extended ARM AArch64 hardware capability flags.

Message-Id: <[email protected]>

---
 bsd-user/aarch64/target_arch.h         |   1 +
 bsd-user/aarch64/target_arch_elf.h     | 163 +++++++++++++++++++++++++
 bsd-user/aarch64/target_arch_vmparam.h |   6 +
 3 files changed, 170 insertions(+)
 create mode 100644 bsd-user/aarch64/target_arch_elf.h
bsd-user/aarch64/target_arch.h +1 -0
index 27f47de8eb3..4815a56ae3c 100644
@@ -21,6 +21,7 @@
 #define TARGET_ARCH_H
 
 #include "qemu.h"
+#include "target/arm/cpu-features.h"
 
 void target_cpu_set_tls(CPUARMState *env, target_ulong newtls);
 target_ulong target_cpu_get_tls(CPUARMState *env);
bsd-user/aarch64/target_arch_elf.h new file +163 -0

163 lines not on this page · raw message

bsd-user/aarch64/target_arch_vmparam.h +6 -0
index dc66e1289b5..0c354919708 100644
@@ -65,4 +65,10 @@ static inline void set_second_rval(CPUARMState *state, abi_ulong retval2)
 {
     state->xregs[1] = retval2; /* XXX not really used on 64-bit arch */
 }
+
+static inline abi_ulong get_second_rval(CPUARMState *state)
+{
+    return state->xregs[1];
+}
+
 #endif /* TARGET_ARCH_VMPARAM_H */
Signature · 1 line
2.45.1
#19 lore raw

PATCH 13/14 bsd-user: Make compile for non-linux user-mode stuff

In reply to Warner Losh

We include the files that define PR_MTE_TCF_SHIFT only on Linux, but use them unconditionally. Restrict its use to Linux-only.

---
 target/arm/gdbstub64.c | 4 ++++
 1 file changed, 4 insertions(+)
target/arm/gdbstub64.c +4 -0
index 2e2bc2700b8..6dc81aecb2a 100644
@@ -404,6 +404,7 @@ int aarch64_gdb_get_tag_ctl_reg(CPUState *cs, GByteArray *buf, int reg)
 
 int aarch64_gdb_set_tag_ctl_reg(CPUState *cs, uint8_t *buf, int reg)
 {
+#if defined(CONFIG_LINUX)
     ARMCPU *cpu = ARM_CPU(cs);
     CPUARMState *env = &cpu->env;
 
@@ -425,6 +426,9 @@ int aarch64_gdb_set_tag_ctl_reg(CPUState *cs, uint8_t *buf, int reg)
     arm_set_mte_tcf0(env, tcf);
 
     return 1;
+#else
+    return 0;
+#endif
 }
 
 static void handle_q_memtag(GArray *params, void *user_ctx)
Signature · 1 line
2.45.1
#17 lore raw

PATCH 12/14 bsd-user: Define TARGET_SIGSTACK_ALIGN and use it to round stack

In reply to Warner Losh

Most (all?) targets require stacks to be properly aligned. Rather than a series of ifdefs in bsd-user/signal.h, instead use a manditory #define for all architectures.

---
 bsd-user/aarch64/target_arch_signal.h | 2 ++
 bsd-user/arm/target_arch_signal.h     | 2 ++
 bsd-user/i386/target_arch_signal.h    | 2 ++
 bsd-user/signal.c                     | 9 +--------
 bsd-user/x86_64/target_arch_signal.h  | 2 ++
 5 files changed, 9 insertions(+), 8 deletions(-)
bsd-user/aarch64/target_arch_signal.h +2 -0
index bff752a67ab..b72ba7aa504 100644
@@ -77,4 +77,6 @@ struct target_sigframe {
     target_ucontext_t   sf_uc;  /* saved ucontext */
 };
 
+#define TARGET_SIGSTACK_ALIGN 16
+
 #endif /* TARGET_ARCH_SIGNAL_H */
bsd-user/arm/target_arch_signal.h +2 -0
index 02b2b33e07a..10f96b8bfc9 100644
@@ -86,4 +86,6 @@ struct target_sigframe {
     target_mcontext_vfp_t sf_vfp; /* actual saved VFP context */
 };
 
+#define TARGET_SIGSTACK_ALIGN 8
+
 #endif /* TARGET_ARCH_SIGNAL_H */
bsd-user/i386/target_arch_signal.h +2 -0
index 279dadc22c7..2c14153ab6b 100644
@@ -88,4 +88,6 @@ struct target_sigframe {
     uint32_t    __spare__[2];
 };
 
+#define TARGET_SIGSTACK_ALIGN 8
+
 #endif /* TARGET_ARCH_SIGNAL_H */
bsd-user/signal.c +1 -8
index 8b6654b91da..da49b9bffc1 100644
@@ -728,14 +728,7 @@ static inline abi_ulong get_sigframe(struct target_sigaction *ka,
         sp = ts->sigaltstack_used.ss_sp + ts->sigaltstack_used.ss_size;
     }
 
-/* TODO: make this a target_arch function / define */
-#if defined(TARGET_ARM)
-    return (sp - frame_size) & ~7;
-#elif defined(TARGET_AARCH64)
-    return (sp - frame_size) & ~15;
-#else
-    return sp - frame_size;
-#endif
+    return ROUND_DOWN(sp - frame_size, TARGET_SIGSTACK_ALIGN);
 }
 
 /* compare to $M/$M/exec_machdep.c sendsig and sys/kern/kern_sig.c sigexit */
bsd-user/x86_64/target_arch_signal.h +2 -0
index ca24bf1e7f7..f833ee66cef 100644
@@ -97,4 +97,6 @@ struct target_sigframe {
     uint32_t    __spare__[2];
 };
 
+#define TARGET_SIGSTACK_ALIGN 16
+
 #endif /* TARGET_ARCH_SIGNAL_H */
Signature · 1 line
2.45.1
#9 lore raw

PATCH 9/14 bsd-user: Simplify the implementation of execve

In reply to Warner Losh

From: Doug Rabson <[email protected]>

This removes the logic which prepends the emulator to each call to execve and fexecve. This is not necessary with the existing imgact_binmisc support and it avoids the need to install the emulator binary into jail environments when using 'binmiscctl --pre-open'.

---
 bsd-user/freebsd/os-proc.c | 118 +------------------------------------
 bsd-user/main.c            |  18 ------
 2 files changed, 3 insertions(+), 133 deletions(-)
bsd-user/freebsd/os-proc.c +3 -115
index e0203e259b0..bf993f1b662 100644
@@ -26,65 +26,13 @@ struct kinfo_proc;
 
 #include "qemu.h"
 
-/*
- * Get the filename for the given file descriptor.
- * Note that this may return NULL (fail) if no longer cached in the kernel.
- */
-static char *
-get_filename_from_fd(pid_t pid, int fd, char *filename, size_t len)
-{
-    char *ret = NULL;
-    unsigned int cnt;
-    struct procstat *procstat = NULL;
-    struct kinfo_proc *kp = NULL;
-    struct filestat_list *head = NULL;
-    struct filestat *fst;
-
-    procstat = procstat_open_sysctl();
-    if (procstat == NULL) {
-        goto out;
-    }
-
-    kp = procstat_getprocs(procstat, KERN_PROC_PID, pid, &cnt);
-    if (kp == NULL) {
-        goto out;
-    }
-
-    head = procstat_getfiles(procstat, kp, 0);
-    if (head == NULL) {
-        goto out;
-    }
-
-    STAILQ_FOREACH(fst, head, next) {
-        if (fd == fst->fs_fd) {
-            if (fst->fs_path != NULL) {
-                (void)strlcpy(filename, fst->fs_path, len);
-                ret = filename;
-            }
-            break;
-        }
-    }
-
-out:
-    if (head != NULL) {
-        procstat_freefiles(procstat, head);
-    }
-    if (kp != NULL) {
-        procstat_freeprocs(procstat, kp);
-    }
-    if (procstat != NULL) {
-        procstat_close(procstat);
-    }
-    return ret;
-}
-
 /*
  * execve/fexecve
  */
 abi_long freebsd_exec_common(abi_ulong path_or_fd, abi_ulong guest_argp,
         abi_ulong guest_envp, int do_fexec)
 {
-    char **argp, **envp, **qargp, **qarg1, **qarg0, **qargend;
+    char **argp, **envp, **qarg0;
     int argc, envc;
     abi_ulong gp;
     abi_ulong addr;
@@ -117,9 +65,7 @@ abi_long freebsd_exec_common(abi_ulong path_or_fd, abi_ulong guest_argp,
     qarg0 = argp = g_new0(char *, argc + 9);
     /* save the first argument for the emulator */
     *argp++ = (char *)getprogname();
-    qargp = argp;
     *argp++ = (char *)getprogname();
-    qarg1 = argp;
     envp = g_new0(char *, envc + 1);
     for (gp = guest_argp, q = argp; gp; gp += sizeof(abi_ulong), q++) {
         if (get_user_ual(addr, gp)) {
@@ -137,7 +83,6 @@ abi_long freebsd_exec_common(abi_ulong path_or_fd, abi_ulong guest_argp,
         total_size += strlen(*q) + 1;
     }
     *q++ = NULL;
-    qargend = q;
 
     for (gp = guest_envp, q = envp; gp; gp += sizeof(abi_ulong), q++) {
         if (get_user_ual(addr, gp)) {
@@ -166,71 +111,14 @@ abi_long freebsd_exec_common(abi_ulong path_or_fd, abi_ulong guest_argp,
     }
 
     if (do_fexec) {
-        if (((int)path_or_fd > 0 &&
-            is_target_elf_binary((int)path_or_fd)) == 1) {
-            char execpath[PATH_MAX];
-
-            /*
-             * The executable is an elf binary for the target
-             * arch.  execve() it using the emulator if we can
-             * determine the filename path from the fd.
-             */
-            if (get_filename_from_fd(getpid(), (int)path_or_fd, execpath,
-                        sizeof(execpath)) != NULL) {
-                memmove(qarg1 + 2, qarg1, (qargend - qarg1) * sizeof(*qarg1));
-                qarg1[1] = qarg1[0];
-                qarg1[0] = (char *)"-0";
-                qarg1 += 2;
-                qargend += 2;
-                *qarg1 = execpath;
-#ifndef DONT_INHERIT_INTERP_PREFIX
-                memmove(qarg1 + 2, qarg1, (qargend - qarg1) * sizeof(*qarg1));
-                *qarg1++ = (char *)"-L";
-                *qarg1++ = (char *)interp_prefix;
-#endif
-                ret = get_errno(execve(qemu_proc_pathname, qargp, envp));
-            } else {
-                /* Getting the filename path failed. */
-                ret = -TARGET_EBADF;
-                goto execve_end;
-            }
-        } else {
-            ret = get_errno(fexecve((int)path_or_fd, argp, envp));
-        }
+        ret = get_errno(fexecve((int)path_or_fd, argp, envp));
     } else {
-        int fd;
-
         p = lock_user_string(path_or_fd);
         if (p == NULL) {
             ret = -TARGET_EFAULT;
             goto execve_end;
         }
-
-        /*
-         * Check the header and see if it a target elf binary.  If so
-         * then execute using qemu user mode emulator.
-         */
-        fd = open(p, O_RDONLY | O_CLOEXEC);
-        if (fd > 0 && is_target_elf_binary(fd) == 1) {
-            close(fd);
-            /* execve() as a target binary using emulator. */
-            memmove(qarg1 + 2, qarg1, (qargend - qarg1) * sizeof(*qarg1));
-            qarg1[1] = qarg1[0];
-            qarg1[0] = (char *)"-0";
-            qarg1 += 2;
-            qargend += 2;
-            *qarg1 = (char *)p;
-#ifndef DONT_INHERIT_INTERP_PREFIX
-            memmove(qarg1 + 2, qarg1, (qargend - qarg1) * sizeof(*qarg1));
-            *qarg1++ = (char *)"-L";
-            *qarg1++ = (char *)interp_prefix;
-#endif
-            ret = get_errno(execve(qemu_proc_pathname, qargp, envp));
-        } else {
-            close(fd);
-            /* Execve() as a host native binary. */
-            ret = get_errno(execve(p, argp, envp));
-        }
+        ret = get_errno(execve(p, argp, envp));
         unlock_user(p, path_or_fd, 0);
     }
 
bsd-user/main.c +0 -18
index dcad266c2c9..82e94a03160 100644
@@ -90,7 +90,6 @@ unsigned long reserved_va;
 
 const char *interp_prefix = CONFIG_QEMU_INTERP_PREFIX;
 const char *qemu_uname_release;
-char qemu_proc_pathname[PATH_MAX];  /* full path to exeutable */
 
 unsigned long target_maxtsiz = TARGET_MAXTSIZ;   /* max text size */
 unsigned long target_dfldsiz = TARGET_DFLDSIZ;   /* initial data size limit */
@@ -247,22 +246,6 @@ adjust_ssize(void)
     setrlimit(RLIMIT_STACK, &rl);
 }
 
-static void save_proc_pathname(char *argv0)
-{
-    int mib[4];
-    size_t len;
-
-    mib[0] = CTL_KERN;
-    mib[1] = KERN_PROC;
-    mib[2] = KERN_PROC_PATHNAME;
-    mib[3] = -1;
-
-    len = sizeof(qemu_proc_pathname);
-    if (sysctl(mib, 4, qemu_proc_pathname, &len, NULL, 0)) {
-        perror("sysctl");
-    }
-}
-
 int main(int argc, char **argv)
 {
     const char *filename;
@@ -292,7 +275,6 @@ int main(int argc, char **argv)
         usage();
     }
 
-    save_proc_pathname(argv[0]);
 
     error_init(argv[0]);
     module_call_init(MODULE_INIT_TRACE);
Signature · 1 line
2.45.1
#6 lore raw

PATCH 6/14 bsd-user:Add setup_sigframe_arch function for ARM AArch64

In reply to Warner Losh

The function utilizes the get_mcontext function to retrieve the machine context for the current CPUARMState

Message-Id: <[email protected]>

---
 bsd-user/aarch64/signal.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
bsd-user/aarch64/signal.c +14 -0
index ab3bf8558ab..43c886e6036 100644
@@ -81,3 +81,17 @@ abi_long get_mcontext(CPUARMState *regs, target_mcontext_t *mcp, int flags)
 
     return err;
 }
+
+/*
+ * Compare to arm64/arm64/exec_machdep.c sendsig()
+ * Assumes that the memory is locked if frame points to user memory.
+ */
+abi_long setup_sigframe_arch(CPUARMState *env, abi_ulong frame_addr,
+                             struct target_sigframe *frame, int flags)
+{
+    target_mcontext_t *mcp = &frame->sf_uc.uc_mcontext;
+
+    get_mcontext(env, mcp, flags);
+    return 0;
+}
+
Signature · 1 line
2.45.1
#15 lore raw

PATCH 11/14 bsd-user: Sync fork_start/fork_end with linux-user

In reply to Warner Losh

From: Jessica Clarke <[email protected]>

This reorders some of the calls, deduplicates code between branches and, most importantly, fixes a double end_exclusive call in the parent that will cause exclusive_context_count to go negative.

Pull-Request: https://github.com/qemu-bsd-user/qemu-bsd-user/pull/52

---
 bsd-user/main.c | 28 ++++++++++++++--------------
 1 file changed, 14 insertions(+), 14 deletions(-)
bsd-user/main.c +14 -14
index 82e94a03160..6d81452e51a 100644
@@ -35,6 +35,7 @@
 #include "qemu/path.h"
 #include "qemu/help_option.h"
 #include "qemu/module.h"
+#include "qemu/plugin.h"
 #include "exec/exec-all.h"
 #include "user/guest-base.h"
 #include "tcg/startup.h"
@@ -103,8 +104,9 @@ unsigned long target_sgrowsiz = TARGET_SGROWSIZ; /* amount to grow stack */
 void fork_start(void)
 {
     start_exclusive();
-    cpu_list_lock();
     mmap_fork_start();
+    cpu_list_lock();
+    qemu_plugin_user_prefork_lock();
     gdbserver_fork_start();
 }
 
@@ -112,31 +114,29 @@ void fork_end(pid_t pid)
 {
     bool child = pid == 0;
 
+    qemu_plugin_user_postfork(child);
+    mmap_fork_end(child);
     if (child) {
         CPUState *cpu, *next_cpu;
-        /*
-         * Child processes created by fork() only have a single thread.  Discard
-         * information about the parent threads.
-         */
+        /* Child processes created by fork() only have a single thread.
+           Discard information about the parent threads.  */
         CPU_FOREACH_SAFE(cpu, next_cpu) {
             if (cpu != thread_cpu) {
                 QTAILQ_REMOVE_RCU(&cpus_queue, cpu, node);
             }
         }
-        mmap_fork_end(child);
-        /*
-         * qemu_init_cpu_list() takes care of reinitializing the exclusive
-         * state, so we don't need to end_exclusive() here.
-         */
         qemu_init_cpu_list();
         get_task_state(thread_cpu)->ts_tid = qemu_get_thread_id();
-        gdbserver_fork_end(thread_cpu, pid);
     } else {
-        mmap_fork_end(child);
         cpu_list_unlock();
-        gdbserver_fork_end(thread_cpu, pid);
-        end_exclusive();
     }
+    gdbserver_fork_end(thread_cpu, pid);
+    /*
+     * qemu_init_cpu_list() reinitialized the child exclusive state, but we
+     * also need to keep current_cpu consistent, so call end_exclusive() for
+     * both child and parent.
+     */
+    end_exclusive();
 }
 
 void cpu_loop(CPUArchState *env)
Signature · 1 line
2.45.1
#11 lore raw

PATCH 10/14 bsd-user: Hard wire aarch64 to be 4k pages only

In reply to Warner Losh

Only support 4k pages for aarch64 binaries. The variable page size stuff isn't working just yet, so put in this lessor-of-evils kludge until that is complete.

---
 target/arm/cpu-param.h | 4 ++++
 1 file changed, 4 insertions(+)
target/arm/cpu-param.h +4 -0
index 2d5f3aa312c..fa6cae0e3aa 100644
@@ -21,9 +21,13 @@
 #ifdef CONFIG_USER_ONLY
 # ifdef TARGET_AARCH64
 #  define TARGET_TAGGED_ADDRESSES
+# ifdef __FreeBSD__
+#  define TARGET_PAGE_BITS 12
+# else
 /* Allow user-only to vary page size from 4k */
 #  define TARGET_PAGE_BITS_VARY
 #  define TARGET_PAGE_BITS_MIN  12
+# endif
 # else
 #  define TARGET_PAGE_BITS 12
 # endif
Signature · 1 line
2.45.1
#24 lore raw

PATCH 14/14 bsd-user: Add aarch64 build to tree

In reply to Warner Losh

Add the aarch64 bsd-user fragments needed to build the new aarch64 code.

---
 configs/targets/aarch64-bsd-user.mak | 3 +++
 1 file changed, 3 insertions(+)
 create mode 100644 configs/targets/aarch64-bsd-user.mak
configs/targets/aarch64-bsd-user.mak new file +3 -0
new file mode 100644
index 00000000000..8aaa5d8c802
@@ -0,0 +1,3 @@
+TARGET_ARCH=aarch64
+TARGET_BASE_ARCH=arm
+TARGET_XML_FILES= gdb-xml/aarch64-core.xml gdb-xml/aarch64-fpu.xml gdb-xml/aarch64-pauth.xml
Signature · 1 line
2.45.1
#4 lore raw

PATCH 4/14 bsd-user:Add ARM AArch64 signal handling support

In reply to Warner Losh

From: Stacey Son <[email protected]>

Added sigcode setup function for signal trampoline which initializes a sequence of instructions to handle signal returns and exits, copying this code to the target offset.
Defined ARM AArch64 specific signal definitions including register indices and sizes,
and introduced structures to represent general purpose registers, floating point registers, and machine context. Added function to set up signal handler arguments, populating register values in CPUARMState based on the provided signal, signal frame, signal action, and frame address.

Message-Id: <[email protected]>

---
 bsd-user/aarch64/signal.c               | 53 ++++++++++++++++
 bsd-user/aarch64/target_arch_signal.h   | 80 +++++++++++++++++++++++++
 bsd-user/aarch64/target_arch_sigtramp.h | 48 +++++++++++++++
 3 files changed, 181 insertions(+)
 create mode 100644 bsd-user/aarch64/signal.c
 create mode 100644 bsd-user/aarch64/target_arch_signal.h
 create mode 100644 bsd-user/aarch64/target_arch_sigtramp.h
bsd-user/aarch64/signal.c new file +53 -0
new file mode 100644
index 00000000000..98861f9ab3b
@@ -0,0 +1,53 @@
+/*
+ * ARM AArch64 specific signal definitions for bsd-user
+ *
+ * Copyright (c) 2015 Stacey D. Son <sson at FreeBSD>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+#include "qemu/osdep.h"
+
+#include "qemu.h"
+
+/*
+ * Compare to sendsig() in sys/arm64/arm64/machdep.c
+ * Assumes that target stack frame memory is locked.
+ */
+abi_long set_sigtramp_args(CPUARMState *regs, int sig,
+                           struct target_sigframe *frame,
+                           abi_ulong frame_addr,
+                           struct target_sigaction *ka)
+{
+    /*
+     * Arguments to signal handler:
+     *  x0 = signal number
+     *  x1 = siginfo pointer
+     *  x2 = ucontext pointer
+     *  pc/elr = signal handler pointer
+     *  sp = sigframe struct pointer
+     *  lr = sigtramp at base of user stack
+     */
+
+    regs->xregs[0] = sig;
+    regs->xregs[1] = frame_addr +
+        offsetof(struct target_sigframe, sf_si);
+    regs->xregs[2] = frame_addr +
+        offsetof(struct target_sigframe, sf_uc);
+
+    regs->pc = ka->_sa_handler;
+    regs->xregs[TARGET_REG_SP] = frame_addr;
+    regs->xregs[TARGET_REG_LR] = TARGET_PS_STRINGS - TARGET_SZSIGCODE;
+
+    return 0;
+}
bsd-user/aarch64/target_arch_signal.h new file +80 -0

80 lines not on this page · raw message

bsd-user/aarch64/target_arch_sigtramp.h new file +48 -0

48 lines not on this page · raw message

Signature · 1 line
2.45.1
#25 lore raw

Re: [PATCH 14/14] bsd-user: Add aarch64 build to tree

In reply to Warner Losh

Hi Warner,

Quoted 7 lines · Warner Losh

Add the aarch64 bsd-user fragments needed to build the new aarch64 code.

---
  configs/targets/aarch64-bsd-user.mak | 3 +++
  1 file changed, 3 insertions(+)

create mode 100644 configs/targets/aarch64-bsd-user.mak

Can we build aarch64 on Cirrus-CI? (not clear on
https://cirrus-ci.org/guide/FreeBSD/). If so, could you add a follow-up patch to build that on our CI, patching .gitlab-ci.d/cirrus.yml?

Regards,

Phil.

Reviewed-by on v1 13/14
#20 lore raw

Re: [PATCH 13/14] bsd-user: Make compile for non-linux user-mode stuff

In reply to Warner Losh

Hi Warner,

Quoted 2 lines · Warner Losh

We include the files that define PR_MTE_TCF_SHIFT only on Linux, but use them unconditionally. Restrict its use to Linux-only.

We should check that in meson, i.e.:

config_host_data.set('CONFIG_PRCTL_PR_MTE_TCF_SHIFT',

                     cc.has_header_symbol('sys/prctl.h', 

'PR_MTE_TCF_SHIFT'))

(like we do for CONFIG_PRCTL_PR_SET_TIMERSLACK), then rework linux-user/aarch64/mte_user_helper.h (and possibly tests/tcg/aarch64/mte.h), moving in a common directory.

That said, your patch matches this file header:

  #if defined(CONFIG_USER_ONLY) && defined(CONFIG_LINUX)
  #include <sys/prctl.h>
  #include "mte_user_helper.h"
  #endif

Even if this #ifdef'ry is ugly, it isn't wrong, so:

¯\_(ツ)_/¯

Quoted 27 lines
---
  target/arm/gdbstub64.c | 4 ++++
  1 file changed, 4 insertions(+)
target/arm/gdbstub64.c +4 -0
index 2e2bc2700b8..6dc81aecb2a 100644
@@ -404,6 +404,7 @@ int aarch64_gdb_get_tag_ctl_reg(CPUState *cs, GByteArray *buf, int reg)
 
  int aarch64_gdb_set_tag_ctl_reg(CPUState *cs, uint8_t *buf, int reg)
  {
+#if defined(CONFIG_LINUX)
      ARMCPU *cpu = ARM_CPU(cs);
      CPUARMState *env = &cpu->env;
 
@@ -425,6 +426,9 @@ int aarch64_gdb_set_tag_ctl_reg(CPUState *cs, uint8_t *buf, int reg)
      arm_set_mte_tcf0(env, tcf);
 
      return 1;
+#else
+    return 0;
+#endif
  }
 
  static void handle_q_memtag(GArray *params, void *user_ctx)
#26 lore raw

Re: [PATCH 14/14] bsd-user: Add aarch64 build to tree

In reply to Philippe Mathieu-Daudé
Quoted 11 lines · Philippe Mathieu-Daudé

Hi Warner,

Quoted 7 lines · Warner Losh

Add the aarch64 bsd-user fragments needed to build the new aarch64 code.

---
  configs/targets/aarch64-bsd-user.mak | 3 +++
  1 file changed, 3 insertions(+)

create mode 100644 configs/targets/aarch64-bsd-user.mak

Quoted 5 lines

Can we build aarch64 on Cirrus-CI? (not clear on
https://cirrus-ci.org/guide/FreeBSD/). If so, could you add a follow-up patch to build that on our CI, patching .gitlab-ci.d/cirrus.yml?

We can build aarch64 host for bsd-user for sure. I'll see if we can do it in cirrus CI.
If so, I'll try to add it to cirrus.yml.

This patch series adds aarch64 guest...

Warner

#27 lore raw

Re: [PATCH 14/14] bsd-user: Add aarch64 build to tree

Quoted 23 lines · Warner Losh

On Mon, Jul 22, 2024 at 3:54 PM Philippe Mathieu-Daudé <[email protected] <mailto:[email protected]>> wrote:

Hi Warner,

On 22/7/24 23:43, Warner Losh wrote:
> Add the aarch64 bsd-user fragments needed to build the new
aarch64 code.
>
> Signed-off-by: Warner Losh <[email protected] <mailto:[email protected]>> > ---
>   configs/targets/aarch64-bsd-user.mak | 3 +++
>   1 file changed, 3 insertions(+)
>   create mode 100644 configs/targets/aarch64-bsd-user.mak

Can we build aarch64 on Cirrus-CI? (not clear on
https://cirrus-ci.org/guide/FreeBSD/
<https://cirrus-ci.org/guide/FreeBSD/>). If so, could you add a follow-up patch to build that on our CI, patching .gitlab-ci.d/cirrus.yml?

Quoted 4 lines

We can build aarch64 host for bsd-user for sure. I'll see if we can do it in cirrus CI. If so, I'll try to add it to cirrus.yml.

This patch series adds aarch64 guest...

Yes, we want to use a aarch64 FreeBSD host to build your FreeBSD aarch64 bsd-user guest and test it. Am I wrong?

#21 lore raw

Re: [PATCH 13/14] bsd-user: Make compile for non-linux user-mode stuff

In reply to Philippe Mathieu-Daudé
Quoted 11 lines · Philippe Mathieu-Daudé

Hi Warner,

Quoted 2 lines · Warner Losh

We include the files that define PR_MTE_TCF_SHIFT only on Linux, but use them unconditionally. Restrict its use to Linux-only.

We should check that in meson, i.e.:

config_host_data.set('CONFIG_PRCTL_PR_MTE_TCF_SHIFT',

                     cc.has_header_symbol('sys/prctl.h',
                     'PR_MTE_TCF_SHIFT'))

Surely this fails for non-aarch64 hosts?

Quoted 40 lines

(like we do for CONFIG_PRCTL_PR_SET_TIMERSLACK), then rework linux-user/aarch64/mte_user_helper.h (and possibly tests/tcg/aarch64/mte.h), moving in a common directory.

That said, your patch matches this file header:

  #if defined(CONFIG_USER_ONLY) && defined(CONFIG_LINUX)
  #include <sys/prctl.h>
  #include "mte_user_helper.h"
  #endif

Even if this #ifdef'ry is ugly, it isn't wrong, so:

¯\_(ツ)_/¯

Quoted 24 lines
---
  target/arm/gdbstub64.c | 4 ++++
  1 file changed, 4 insertions(+)
target/arm/gdbstub64.c +1 -0
index 2e2bc2700b8..6dc81aecb2a 100644
@@ -404,6 +404,7 @@ int aarch64_gdb_get_tag_ctl_reg(CPUState *cs, GByteArray *buf, int reg)
    int aarch64_gdb_set_tag_ctl_reg(CPUState *cs, uint8_t *buf, int
 reg)
  {
+#if defined(CONFIG_LINUX)
      ARMCPU *cpu = ARM_CPU(cs);
      CPUARMState *env = &cpu->env;
  @@ -425,6 +426,9 @@ int aarch64_gdb_set_tag_ctl_reg(CPUState *cs,

uint8_t *buf, int reg)

      arm_set_mte_tcf0(env, tcf);
        return 1;
target/arm/gdbstub64.c +3 -0
+#else
+    return 0;
+#endif
  }
    static void handle_q_memtag(GArray *params, void *user_ctx)
Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
Acked-by on v1 9/14
#10 lore raw

Re: [PATCH 09/14] bsd-user: Simplify the implementation of execve

In reply to Warner Losh
Quoted 7 lines · Warner Losh

From: Doug Rabson<[email protected]>

This removes the logic which prepends the emulator to each call to execve and fexecve. This is not necessary with the existing imgact_binmisc support and it avoids the need to install the emulator binary into jail environments when using 'binmiscctl --pre-open'.

Quoted 7 lines
---
  bsd-user/freebsd/os-proc.c | 118 +------------------------------------
  bsd-user/main.c            |  18 ------
  2 files changed, 3 insertions(+), 133 deletions(-)

r~

#12 lore raw

Re: [PATCH 10/14] bsd-user: Hard wire aarch64 to be 4k pages only

In reply to Warner Losh
Quoted 3 lines · Warner Losh

Only support 4k pages for aarch64 binaries. The variable page size stuff isn't working just yet, so put in this lessor-of-evils kludge until that is complete.

Hmm. In what way is it not working?

What if we limit this to bsd-user/main.c instead?

  set_preferred_target_page_bits(TARGET_PAGE_BITS_MIN);

r~

Reviewed-by on v1 11/14
#16 lore raw

Re: [PATCH 11/14] bsd-user: Sync fork_start/fork_end with linux-user

In reply to Warner Losh

bsd-user/main.c void fork_end(pid_t pid) · line 122

Quoted 6 lines · Warner Losh
bsd-user/main.c +2 -4
-        /*
-         * Child processes created by fork() only have a single thread.  Discard
-         * information about the parent threads.
-         */
+        /* Child processes created by fork() only have a single thread.
+           Discard information about the parent threads.  */

This comment should stay formatted the way it is, per coding style.

Otherwise,

r~

Reviewed-by on v1 12/14
#18 lore raw

Re: [PATCH 12/14] bsd-user: Define TARGET_SIGSTACK_ALIGN and use it to round stack

In reply to Warner Losh
Quoted 4 lines · Warner Losh

Most (all?) targets require stacks to be properly aligned. Rather than a series of ifdefs in bsd-user/signal.h, instead use a manditory #define for all architectures.

Quoted 8 lines
---
  bsd-user/aarch64/target_arch_signal.h | 2 ++
  bsd-user/arm/target_arch_signal.h     | 2 ++
  bsd-user/i386/target_arch_signal.h    | 2 ++
  bsd-user/signal.c                     | 9 +--------
  bsd-user/x86_64/target_arch_signal.h  | 2 ++
  5 files changed, 9 insertions(+), 8 deletions(-)

r~

#28 lore raw

Re: [PATCH 14/14] bsd-user: Add aarch64 build to tree

In reply to Philippe Mathieu-Daudé
Quoted 24 lines · Philippe Mathieu-Daudé
Quoted 23 lines · Warner Losh

On Mon, Jul 22, 2024 at 3:54 PM Philippe Mathieu-Daudé <[email protected] <mailto:[email protected]>> wrote:

    Hi Warner,

    On 22/7/24 23:43, Warner Losh wrote:
     > Add the aarch64 bsd-user fragments needed to build the new aarch64 code.
     >
     > Signed-off-by: Warner Losh <[email protected] <mailto:[email protected]>> > ---
     >   configs/targets/aarch64-bsd-user.mak | 3 +++
     >   1 file changed, 3 insertions(+)
     >   create mode 100644 configs/targets/aarch64-bsd-user.mak

    Can we build aarch64 on Cirrus-CI? (not clear on
    https://cirrus-ci.org/guide/FreeBSD/
    <https://cirrus-ci.org/guide/FreeBSD/>). If so, could you add a follow-up patch to build that on our CI, patching .gitlab-ci.d/cirrus.yml?

Quoted 8 lines
Quoted 4 lines

We can build aarch64 host for bsd-user for sure. I'll see if we can do it in cirrus CI. If so, I'll try to add it to cirrus.yml.

This patch series adds aarch64 guest...

Yes, we want to use a aarch64 FreeBSD host to build your FreeBSD aarch64 bsd-user guest and test it. Am I wrong?

This is adding guest support, so your suggestion is orthogonal.

r~

#13 lore raw

Re: [PATCH 10/14] bsd-user: Hard wire aarch64 to be 4k pages only

In reply to Richard Henderson
Quoted 11 lines · Richard Henderson
Quoted 3 lines · Warner Losh

Only support 4k pages for aarch64 binaries. The variable page size stuff isn't working just yet, so put in this lessor-of-evils kludge until that is complete.

Hmm. In what way is it not working?

What if we limit this to bsd-user/main.c instead?

   set_preferred_target_page_bits(TARGET_PAGE_BITS_MIN);

It's a compile issue that this won't solve:
In file included from ../bsd-user/aarch64/signal.c:21:
../bsd-user/qemu.h:146:20: warning: variable length array used [-Wvla]

  146 |         void *page[MAX_ARG_PAGES];
      |                    ^~~~~~~~~~~~~
../bsd-user/qemu.h:138:23: note: expanded from macro 'MAX_ARG_PAGES'
  138 | #define MAX_ARG_PAGES (TARGET_ARG_MAX / TARGET_PAGE_SIZE)
      |                       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

../bsd-user/qemu.h:146:20: note: initializer of 'target_page' is unknown ../bsd-user/qemu.h:138:41: note: expanded from macro 'MAX_ARG_PAGES'
138 | #define MAX_ARG_PAGES (TARGET_ARG_MAX / TARGET_PAGE_SIZE)
| ^
/dune/imp/git/qemu/include/exec/cpu-all.h:152:37: note: expanded from macro 'TARGET_PAGE_SIZE'
152 | # define TARGET_PAGE_SIZE (-(int)TARGET_PAGE_MASK)
| ^
/dune/imp/git/qemu/include/exec/cpu-all.h:150:44: note: expanded from macro 'TARGET_PAGE_MASK'
150 | # define TARGET_PAGE_MASK ((target_long)target_page.mask)
| ^
/dune/imp/git/qemu/include/exec/cpu-all.h:142:29: note: declared here
142 | extern const TargetPageBits target_page;
| ^
In file included from ../bsd-user/aarch64/signal.c:21:
../bsd-user/qemu.h:146:15: error: fields must have a constant size:
'variable length array in structure' extension will never be supported
146 | void *page[MAX_ARG_PAGES];
| ^
1 warning and 1 error generated.
ninja: build stopped: subcommand failed.
gmake: *** [Makefile:167: run-ninja] Error 1

that have a couple of additional levels after those issues are fixed... It's fixable, but will take a fair amount more time that I have before the deadline...

Warner

#29 lore raw

Re: [PATCH 14/14] bsd-user: Add aarch64 build to tree

In reply to Richard Henderson
Quoted 32 lines · Richard Henderson
Quoted 4 lines · Philippe Mathieu-Daudé
Quoted 3 lines · Warner Losh

On Mon, Jul 22, 2024 at 3:54 PM Philippe Mathieu-Daudé <

[email protected]

Quoted 9 lines
Quoted 9 lines

<mailto:[email protected]>> wrote:

Hi Warner,

On 22/7/24 23:43, Warner Losh wrote:
> Add the aarch64 bsd-user fragments needed to build the new
aarch64 code.
>
> Signed-off-by: Warner Losh <[email protected] <mailto:

[email protected]>>

Quoted 5 lines
Quoted 5 lines

> ---
> configs/targets/aarch64-bsd-user.mak | 3 +++
> 1 file changed, 3 insertions(+)
> create mode 100644 configs/targets/aarch64-bsd-user.mak

Quoted 8 lines
Quoted 8 lines

Can we build aarch64 on Cirrus-CI? (not clear on
https://cirrus-ci.org/guide/FreeBSD/
<https://cirrus-ci.org/guide/FreeBSD/>). If so, could you add a follow-up patch to build that on our CI, patching .gitlab-ci.d/cirrus.yml?

We can build aarch64 host for bsd-user for sure. I'll see if we can do

it in cirrus CI.

Quoted 2 lines
Quoted 2 lines

If so, I'll try to add it to cirrus.yml.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

This patch series adds aarch64 guest...

Yes, we want to use a aarch64 FreeBSD host to build your FreeBSD aarch64 bsd-user guest and test it. Am I wrong?

This is adding guest support, so your suggestion is orthogonal.

Yea... It's a good suggestion, but not what I'm working on right now...

Warner

#30 lore raw

Re: [PATCH 14/14] bsd-user: Add aarch64 build to tree

Oh, also, can I get a reviewed-by?

Warner

Quoted 50 lines · Warner Losh
Quoted 32 lines · Richard Henderson
Quoted 4 lines · Philippe Mathieu-Daudé
Quoted 3 lines · Warner Losh

On Mon, Jul 22, 2024 at 3:54 PM Philippe Mathieu-Daudé <

[email protected]

Quoted 9 lines
Quoted 9 lines

<mailto:[email protected]>> wrote:

Hi Warner,

On 22/7/24 23:43, Warner Losh wrote:
> Add the aarch64 bsd-user fragments needed to build the new
aarch64 code.
>
> Signed-off-by: Warner Losh <[email protected] <mailto:

[email protected]>>

Quoted 5 lines
Quoted 5 lines

> ---
> configs/targets/aarch64-bsd-user.mak | 3 +++
> 1 file changed, 3 insertions(+)
> create mode 100644 configs/targets/aarch64-bsd-user.mak

Quoted 8 lines
Quoted 8 lines

Can we build aarch64 on Cirrus-CI? (not clear on
https://cirrus-ci.org/guide/FreeBSD/
<https://cirrus-ci.org/guide/FreeBSD/>). If so, could you add a follow-up patch to build that on our CI, patching .gitlab-ci.d/cirrus.yml?

We can build aarch64 host for bsd-user for sure. I'll see if we can do

it in cirrus CI.

Quoted 2 lines
Quoted 2 lines

If so, I'll try to add it to cirrus.yml.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

This patch series adds aarch64 guest...

Yes, we want to use a aarch64 FreeBSD host to build your FreeBSD aarch64 bsd-user guest and test it. Am I wrong?

This is adding guest support, so your suggestion is orthogonal.

Yea... It's a good suggestion, but not what I'm working on right now...

Warner

#22 lore raw

Re: [PATCH 13/14] bsd-user: Make compile for non-linux user-mode stuff

In reply to Philippe Mathieu-Daudé
Quoted 8 lines · Philippe Mathieu-Daudé

Hi Warner,

Quoted 2 lines · Warner Losh

We include the files that define PR_MTE_TCF_SHIFT only on Linux, but use them unconditionally. Restrict its use to Linux-only.

We should check that in meson, i.e.:

Quoted 8 lines

config_host_data.set('CONFIG_PRCTL_PR_MTE_TCF_SHIFT',

                      cc.has_header_symbol('sys/prctl.h',

'PR_MTE_TCF_SHIFT'))

(like we do for CONFIG_PRCTL_PR_SET_TIMERSLACK), then rework linux-user/aarch64/mte_user_helper.h (and possibly tests/tcg/aarch64/mte.h), moving in a common directory.

Hmmm... I'll have to check, but since I didn't add the incompatibility, it will likely be on my backlog for a while...

Quoted 7 lines

That said, your patch matches this file header:

   #if defined(CONFIG_USER_ONLY) && defined(CONFIG_LINUX)
   #include <sys/prctl.h>
   #include "mte_user_helper.h"
   #endif
Quoted 5 lines

Even if this #ifdef'ry is ugly, it isn't wrong, so:

¯\_(ツ)_/¯

Thanks!

Warner

Quoted 31 lines

target/arm/gdbstub64.c int aarch64_gdb_get_tag_ctl_reg(CPUState *cs,

Quoted 10 lines
---
  target/arm/gdbstub64.c | 4 ++++
  1 file changed, 4 insertions(+)
target/arm/gdbstub64.c +0 -0
index 2e2bc2700b8..6dc81aecb2a 100644
@@ -404,6 +404,7 @@ int aarch64_gdb_get_tag_ctl_reg(CPUState *cs,

GByteArray *buf, int reg)

target/arm/gdbstub64.c int aarch64_gdb_set_tag_ctl_reg(CPUState *cs,

Quoted 8 lines
target/arm/gdbstub64.c +1 -0
  int aarch64_gdb_set_tag_ctl_reg(CPUState *cs, uint8_t *buf, int reg)
  {
+#if defined(CONFIG_LINUX)
      ARMCPU *cpu = ARM_CPU(cs);
      CPUARMState *env = &cpu->env;
 
@@ -425,6 +426,9 @@ int aarch64_gdb_set_tag_ctl_reg(CPUState *cs,

uint8_t *buf, int reg)

Quoted 9 lines
target/arm/gdbstub64.c +3 -0
      arm_set_mte_tcf0(env, tcf);
 
      return 1;
+#else
+    return 0;
+#endif
  }
 
  static void handle_q_memtag(GArray *params, void *user_ctx)
#31 lore raw

Re: [PATCH 14/14] bsd-user: Add aarch64 build to tree

In reply to Richard Henderson
Quoted 28 lines · Richard Henderson
Quoted 27 lines · Philippe Mathieu-Daudé
Quoted 26 lines · Warner Losh

On Mon, Jul 22, 2024 at 3:54 PM Philippe Mathieu-Daudé <[email protected] <mailto:[email protected]>> wrote:

    Hi Warner,

    On 22/7/24 23:43, Warner Losh wrote:
     > Add the aarch64 bsd-user fragments needed to build the new aarch64 code.
     >
     > Signed-off-by: Warner Losh <[email protected] <mailto:[email protected]>> > ---
     >   configs/targets/aarch64-bsd-user.mak | 3 +++
     >   1 file changed, 3 insertions(+)
     >   create mode 100644 configs/targets/aarch64-bsd-user.mak

    Can we build aarch64 on Cirrus-CI? (not clear on
    https://cirrus-ci.org/guide/FreeBSD/
    <https://cirrus-ci.org/guide/FreeBSD/>). If so, could you add a follow-up patch to build that on our CI, patching .gitlab-ci.d/cirrus.yml?

We can build aarch64 host for bsd-user for sure. I'll see if we can do it in cirrus CI. If so, I'll try to add it to cirrus.yml.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

This patch series adds aarch64 guest...

Yes, we want to use a aarch64 FreeBSD host to build your FreeBSD aarch64 bsd-user guest and test it. Am I wrong?

This is adding guest support, so your suggestion is orthogonal.

Ah, got it I guess, this series adds support for aarch64 user-mode emulation on any FreeBSD kernels, so IIUC we can test it on the current FreeBSD x86 job we have. Currently the x64-freebsd-13-build job uses:

CONFIGURE_ARGS:
--target-list-exclude=arm-softmmu,i386-softmmu,microblaze-softmmu,mips64el-softmmu,mipsel-softmmu,mips-softmmu,ppc-softmmu,sh4eb-softmmu,xtensa-softmmu
TEST_TARGETS: check

So the aarch64-bsd-user target should be selected automatically, is that correct?

(got it, my request to test aarch64 FreeBSD *host* is orthogonal).

#32 lore raw

Re: [PATCH 14/14] bsd-user: Add aarch64 build to tree

Quoted 44 lines · Philippe Mathieu-Daudé
Quoted 31 lines · Richard Henderson
Quoted 30 lines · Philippe Mathieu-Daudé
Quoted 29 lines · Warner Losh

On Mon, Jul 22, 2024 at 3:54 PM Philippe Mathieu-Daudé <[email protected] <mailto:[email protected]>> wrote:

Hi Warner,

On 22/7/24 23:43, Warner Losh wrote:
> Add the aarch64 bsd-user fragments needed to build the new
aarch64 code.
>
> Signed-off-by: Warner Losh <[email protected]
<mailto:[email protected]>>
> ---
> configs/targets/aarch64-bsd-user.mak | 3 +++
> 1 file changed, 3 insertions(+)
> create mode 100644 configs/targets/aarch64-bsd-user.mak

Can we build aarch64 on Cirrus-CI? (not clear on
https://cirrus-ci.org/guide/FreeBSD/
<https://cirrus-ci.org/guide/FreeBSD/>). If so, could you add a follow-up patch to build that on our CI, patching .gitlab-ci.d/cirrus.yml?

We can build aarch64 host for bsd-user for sure. I'll see if we can do it in cirrus CI.
If so, I'll try to add it to cirrus.yml.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

This patch series adds aarch64 guest...

Yes, we want to use a aarch64 FreeBSD host to build your FreeBSD aarch64 bsd-user guest and test it. Am I wrong?

This is adding guest support, so your suggestion is orthogonal.

Ah, got it I guess, this series adds support for aarch64 user-mode emulation on any FreeBSD kernels, so IIUC we can test it on the current FreeBSD x86 job we have. Currently the x64-freebsd-13-build job uses:

Quoted 8 lines

CONFIGURE_ARGS:

--target-list-exclude=arm-softmmu,i386-softmmu,microblaze-softmmu,mips64el-softmmu,mipsel-softmmu,mips-softmmu,ppc-softmmu,sh4eb-softmmu,xtensa-softmmu
TEST_TARGETS: check

So the aarch64-bsd-user target should be selected automatically, is that correct?

Yes. It's automatically added to the build just by this file existing.

So it should at least be compile tested by everything.

Warner

Quoted 2 lines

(got it, my request to test aarch64 FreeBSD *host* is orthogonal).