Skip to contentSkip to conversation

linux-user fixes for hppa target - part 2

PATCH In review Helge Deller 2 participants 11 messages Sep 24, 2022 – Sep 27, 2022

lore
Share & tools
lore
#0 lore raw

cover linux-user fixes for hppa target - part 2

Some additional patches for linux-user, which mostly target the hppa platform.
This series is on top of my previous posted patch series.

Please review.

Helge

Helge Deller (7):
linux-user: Fix TARGET_PROT_SEM for XTENSA
linux-user: Add proper strace format strings for

    getdents()/getdents64()

linux-user/hppa: Add signal trampoline for hppa target linux-user/hppa: Drop stack guard page on hppa target linux-user/hppa: Increase guest stack size to 80MB for hppa target linux-user/hppa: Allow PROT_GROWSUP and PROT_GROWSDOWN in mprotect() linux-user/hppa: Fix setup_sigcontext()

 linux-user/elfload.c             | 13 +++++---
 linux-user/hppa/signal.c         | 57 +++++++++++++++++++-------------
 linux-user/hppa/target_signal.h  | 14 +-------
 linux-user/hppa/target_syscall.h |  2 ++
 linux-user/main.c                |  9 +++--
 linux-user/mmap.c                |  2 ++
 linux-user/strace.list           |  4 +--
 linux-user/syscall.c             |  4 +++
 linux-user/syscall_defs.h        |  2 +-
 9 files changed, 62 insertions(+), 45 deletions(-)

--
2.37.3

#1 lore raw

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

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

---
 linux-user/syscall_defs.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
linux-user/syscall_defs.h +1 -1
index 85b0f33e91..1e3577bfa5 100644
@@ -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 on v1 1/7
#2 lore raw

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

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

Quoted 21 lines

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

---
  linux-user/syscall_defs.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
linux-user/syscall_defs.h +1 -2
index 85b0f33e91..1e3577bfa5 100644
@@ -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
--
Quoted 3 lines

2.37.3

#3 lore raw

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

In reply to Helge Deller
---
 linux-user/strace.list | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
linux-user/strace.list +2 -2
index da8c1bf34e..bfef568d18 100644
@@ -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 },
 #endif
 #ifdef TARGET_NR_getdomainname
 { TARGET_NR_getdomainname, "getdomainname" , NULL, NULL, NULL },

--
2.37.3

#4 lore raw

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

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

Quoted 10 lines
---
  linux-user/strace.list | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
linux-user/strace.list +0 -0
index da8c1bf34e..bfef568d18 100644

linux-user/strace.list line 291

Quoted 9 lines
linux-user/strace.list +2 -2
@@ -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

Quoted 7 lines
linux-user/strace.list +0 -1
  #endif
  #ifdef TARGET_NR_getdomainname
  { TARGET_NR_getdomainname, "getdomainname" , NULL, NULL, NULL },
--

2.37.3

#5 lore raw

PATCH 3/7 linux-user/hppa: Add signal trampoline for hppa target

In reply to Helge Deller

In Linux kernel v5.18 the vDSO for signal trampoline was added. This code mimiks the bare minimum of this vDSO and thus avoids that the parisc emulation needs executable stacks.

---
 linux-user/hppa/signal.c        | 41 +++++++++++++++++++++++++--------
 linux-user/hppa/target_signal.h | 14 +----------
 2 files changed, 32 insertions(+), 23 deletions(-)
linux-user/hppa/signal.c +31 -10
index 962f551c04..396e310dc9 100644
@@ -41,7 +41,7 @@ struct target_ucontext {
 };
 
 struct target_rt_sigframe {
-    abi_uint tramp[9];
+    abi_uint tramp[2];  /* syscall restart return address */
     target_siginfo_t info;
     struct target_ucontext uc;
     /* hidden location of upper halves of pa2.0 64-bit gregs */
@@ -101,9 +101,15 @@ static void restore_sigcontext(CPUArchState *env, struct target_sigcontext *sc)
     __get_user(env->cr[CR_SAR], &sc->sc_sar);
 }
 
-/* No, this doesn't look right, but it's copied straight from the kernel.  */
+#if TARGET_ABI_BITS == 32
+#define SIGFRAME                64
+#define FUNCTIONCALLFRAME       48
+#else
+#define SIGFRAME                128
+#define FUNCTIONCALLFRAME       96
+#endif
 #define PARISC_RT_SIGFRAME_SIZE32 \
-    ((sizeof(struct target_rt_sigframe) + 48 + 64) & -64)
+    ((sizeof(struct target_rt_sigframe) + FUNCTIONCALLFRAME + SIGFRAME) & -SIGFRAME)
 
 void setup_rt_frame(int sig, struct target_sigaction *ka,
                     target_siginfo_t *info,
@@ -118,7 +124,7 @@ void setup_rt_frame(int sig, struct target_sigaction *ka,
     if ((ka->sa_flags & TARGET_SA_ONSTACK) && !sas_ss_flags(sp)) {
         sp = (ts->sigaltstack_used.ss_sp + 0x7f) & ~0x3f;
     }
-    frame_addr = QEMU_ALIGN_UP(sp, 64);
+    frame_addr = QEMU_ALIGN_UP(sp, SIGFRAME);
     sp = frame_addr + PARISC_RT_SIGFRAME_SIZE32;
 
     trace_user_setup_rt_frame(env, frame_addr);
@@ -139,14 +145,9 @@ void setup_rt_frame(int sig, struct target_sigaction *ka,
 
     setup_sigcontext(&frame->uc.tuc_mcontext, env);
 
-    __put_user(0x34190000, frame->tramp + 0); /* ldi 0,%r25 */
-    __put_user(0x3414015a, frame->tramp + 1); /* ldi __NR_rt_sigreturn,%r20 */
-    __put_user(0xe4008200, frame->tramp + 2); /* be,l 0x100(%sr2,%r0) */
-    __put_user(0x08000240, frame->tramp + 3); /* nop */
-
     unlock_user_struct(frame, frame_addr, 1);
 
-    env->gr[2] = h2g(frame->tramp);
+    env->gr[2] = default_rt_sigreturn;
     env->gr[30] = sp;
     env->gr[26] = sig;
     env->gr[25] = h2g(&frame->info);
@@ -197,3 +198,23 @@ long do_rt_sigreturn(CPUArchState *env)
     force_sig(TARGET_SIGSEGV);
     return -QEMU_ESIGRETURN;
 }
+
+void setup_sigtramp(abi_ulong sigtramp_page)
+{
+    uint32_t *tramp = lock_user(VERIFY_WRITE, sigtramp_page, 6*4, 0);
+    abi_ulong SIGFRAME_CONTEXT_REGS32;
+    assert(tramp != NULL);
+
+    SIGFRAME_CONTEXT_REGS32 = offsetof(struct target_rt_sigframe, uc.tuc_mcontext);
+    SIGFRAME_CONTEXT_REGS32 -= PARISC_RT_SIGFRAME_SIZE32;
+
+    __put_user(SIGFRAME_CONTEXT_REGS32, tramp + 0);
+    __put_user(0x08000240, tramp + 1);  /* nop - b/c dwarf2 unwind routines */
+    __put_user(0x34190000, tramp + 2);  /* ldi 0, %r25 (in_syscall=0) */
+    __put_user(0x3414015a, tramp + 3);  /* ldi __NR_rt_sigreturn, %r20 */
+    __put_user(0xe4008200, tramp + 4);  /* ble 0x100(%sr2, %r0) */
+    __put_user(0x08000240, tramp + 5);  /* nop */
+
+    default_rt_sigreturn = (sigtramp_page + 8) | 3;
+    unlock_user(tramp, sigtramp_page, 6*4);
+}
linux-user/hppa/target_signal.h +1 -13
index af6c2fce58..190bb3d653 100644
@@ -70,18 +70,6 @@ typedef struct target_sigaltstack {
 /* mask for all SS_xxx flags */
 #define TARGET_SS_FLAG_BITS  TARGET_SS_AUTODISARM
 
-/*
- * We cannot use a bare sigtramp page for hppa-linux.
- *
- * Unlike other guests where we use the instructions at PC to validate
- * an offset from SP, the hppa libgcc signal frame fallback unwinding uses
- * the PC address itself to find the frame.  This is due to the fact that
- * the hppa grows the stack upward, and the frame is of unknown size.
- *
- * TODO: We should be able to use a VDSO to address this, by providing
- * proper unwind info for the sigtramp code, at which point the fallback
- * unwinder will not be used.
- */
-#define TARGET_ARCH_HAS_SIGTRAMP_PAGE 0
+#define TARGET_ARCH_HAS_SIGTRAMP_PAGE 1
 
 #endif /* HPPA_TARGET_SIGNAL_H */

--
2.37.3

#6 lore raw

PATCH 4/7 linux-user/hppa: Drop stack guard page on hppa target

In reply to Helge Deller

The stack-overflow check when building the "grep" debian package fails on the debian hppa target. Reason is, that the guard page at the top of the stack (which is added by qemu) prevents the fault handler in the grep program to properly detect the stack overflow.

The Linux kernel on a physical machine doesn't install a guard page either, so drop it and as such fix the build of "grep".

---
 linux-user/elfload.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
linux-user/elfload.c +9 -4
index ce902dbd56..51ec5dd668 100644
@@ -2033,9 +2033,15 @@ static abi_ulong setup_arg_pages(struct linux_binprm *bprm,
     if (size < STACK_LOWER_LIMIT) {
         size = STACK_LOWER_LIMIT;
     }
-    guard = TARGET_PAGE_SIZE;
-    if (guard < qemu_real_host_page_size()) {
-        guard = qemu_real_host_page_size();
+
+    if (STACK_GROWS_DOWN) {
+        guard = TARGET_PAGE_SIZE;
+        if (guard < qemu_real_host_page_size()) {
+            guard = qemu_real_host_page_size();
+        }
+    } else {
+        /* no guard page for hppa target where stack grows upwards. */
+        guard = 0;
     }
 
     error = target_mmap(0, size + guard, PROT_READ | PROT_WRITE,
@@ -2051,7 +2057,6 @@ static abi_ulong setup_arg_pages(struct linux_binprm *bprm,
         info->stack_limit = error + guard;
         return info->stack_limit + size - sizeof(void *);
     } else {
-        target_mprotect(error + size, guard, PROT_NONE);
         info->stack_limit = error + size;
         return error;
     }

--
2.37.3

#7 lore raw

PATCH 5/7 linux-user/hppa: Increase guest stack size to 80MB for hppa target

In reply to Helge Deller

The hppa target requires a much bigger stack than many other targets, and the Linux kernel allocates 80 MB by default for it.

This patch increases the guest stack for hppa to 80MB, and prevents that this default stack size gets reduced by a lower stack limit on the host.

Since the stack grows upwards on hppa, the stack_limit value marks the upper boundary of the stack. Fix the output of /proc/self/maps (in the guest) to show the [stack] marker on the correct memory area.

---
 linux-user/hppa/target_syscall.h | 2 ++
 linux-user/main.c                | 9 +++++++--
 linux-user/syscall.c             | 4 ++++
 3 files changed, 13 insertions(+), 2 deletions(-)
linux-user/hppa/target_syscall.h +2 -0
index 4b382c1fcf..9a8f8ca628 100644
@@ -26,4 +26,6 @@ struct target_pt_regs {
 #define TARGET_MCL_FUTURE  2
 #define TARGET_MCL_ONFAULT 4
 
+#define TARGET_DEFAULT_STACK_SIZE	80 * 1024 * 1024UL
+
 #endif /* HPPA_TARGET_SYSCALL_H */
linux-user/main.c +7 -2
index 587bd02db2..3a86feb09a 100644
@@ -124,10 +124,14 @@ static void usage(int exitcode);
 static const char *interp_prefix = CONFIG_QEMU_INTERP_PREFIX;
 const char *qemu_uname_release;
 
+#if !defined(TARGET_DEFAULT_STACK_SIZE)
 /* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
    we allocate a bigger stack. Need a better solution, for example
    by remapping the process stack directly at the right place */
-unsigned long guest_stack_size = 8 * 1024 * 1024UL;
+#define TARGET_DEFAULT_STACK_SIZE	8 * 1024 * 1024UL
+#endif
+
+unsigned long guest_stack_size = TARGET_DEFAULT_STACK_SIZE;
 
 /***********************************************************/
 /* Helper routines for implementing atomic operations.  */
@@ -690,7 +694,8 @@ int main(int argc, char **argv, char **envp)
         struct rlimit lim;
         if (getrlimit(RLIMIT_STACK, &lim) == 0
             && lim.rlim_cur != RLIM_INFINITY
-            && lim.rlim_cur == (target_long)lim.rlim_cur) {
+            && lim.rlim_cur == (target_long)lim.rlim_cur
+            && lim.rlim_cur > guest_stack_size) {
             guest_stack_size = lim.rlim_cur;
         }
     }
linux-user/syscall.c +4 -0
index 4bbf46b622..d17f5d1c66 100644
@@ -8043,7 +8043,11 @@ static int open_self_maps(CPUArchState *cpu_env, int fd)
                 continue;
             }
 
+#ifdef TARGET_HPPA
+            if (h2g(max) == ts->info->stack_limit) {
+#else
             if (h2g(min) == ts->info->stack_limit) {
+#endif
                 path = "[stack]";
             } else {
                 path = e->path;

--
2.37.3

#8 lore raw

PATCH 6/7 linux-user/hppa: Allow PROT_GROWSUP and PROT_GROWSDOWN in mprotect()

In reply to Helge Deller

The hppa platform uses an upwards-growing stack and required in Linux kernels < 5.18 an executable stack for signal processing. For that some executables and libraries are marked to have an executable stack, for which glibc uses the mprotect() syscall to mark the stack like this:
mprotect(xfa000000,4096,PROT_EXEC|PROT_READ|PROT_WRITE|PROT_GROWSUP).

Currently qemu will return -TARGET_EINVAL for this syscall because of the
checks in validate_prot_to_pageflags(), which doesn't allow the
PROT_GROWSUP or PROT_GROWSDOWN flags and thus triggers this error in the
guest:
error while loading shared libraries: libc.so.6: cannot enable executable stack as shared object requires: Invalid argument

Allow mprotect() to handle both flags and thus fix the guest. The glibc tst-execstack testcase can be used to reproduce the issue.

---
 linux-user/mmap.c | 2 ++
 1 file changed, 2 insertions(+)
linux-user/mmap.c +2 -0
index dba6823668..b7478ad0fa 100644
@@ -105,6 +105,8 @@ static int validate_prot_to_pageflags(int *host_prot, int prot)
             page_flags |= PAGE_MTE;
         }
     }
+#elif TARGET_HPPA
+    valid |= PROT_GROWSDOWN | PROT_GROWSUP;
 #endif
 
     return prot & ~valid ? 0 : page_flags;

--
2.37.3

#9 lore raw

PATCH 7/7 linux-user/hppa: Fix setup_sigcontext()

In reply to Helge Deller

We don't emulate a preemptive kernel on this level, and the hppa architecture doesn't allow context switches on the gateway page. So we always have to return to sc_iaoq[] and not to gr[31].
This fixes the remaining random segfaults which still occured.

---
 linux-user/hppa/signal.c | 16 +++-------------
 1 file changed, 3 insertions(+), 13 deletions(-)
linux-user/hppa/signal.c +3 -13
index 396e310dc9..f253a15864 100644
@@ -49,23 +49,13 @@ struct target_rt_sigframe {
 
 static void setup_sigcontext(struct target_sigcontext *sc, CPUArchState *env)
 {
-    int flags = 0;
     int i;
 
-    /* ??? if on_sig_stack, flags |= 1 (PARISC_SC_FLAG_ONSTACK).  */
-
-    if (env->iaoq_f < TARGET_PAGE_SIZE) {
-        /* In the gateway page, executing a syscall.  */
-        flags |= 2; /* PARISC_SC_FLAG_IN_SYSCALL */
-        __put_user(env->gr[31], &sc->sc_iaoq[0]);
-        __put_user(env->gr[31] + 4, &sc->sc_iaoq[1]);
-    } else {
-        __put_user(env->iaoq_f, &sc->sc_iaoq[0]);
-        __put_user(env->iaoq_b, &sc->sc_iaoq[1]);
-    }
+    __put_user(env->iaoq_f, &sc->sc_iaoq[0]);
+    __put_user(env->iaoq_b, &sc->sc_iaoq[1]);
     __put_user(0, &sc->sc_iasq[0]);
     __put_user(0, &sc->sc_iasq[1]);
-    __put_user(flags, &sc->sc_flags);
+    __put_user(0, &sc->sc_flags);
 
     __put_user(cpu_hppa_get_psw(env), &sc->sc_gr[0]);
     for (i = 1; i < 32; ++i) {

--
2.37.3

#10 lore raw
In reply to Helge Deller

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

Quoted 29 lines

Some additional patches for linux-user, which mostly target the hppa platform.
This series is on top of my previous posted patch series.

Please review.

Helge

Helge Deller (7):
linux-user: Fix TARGET_PROT_SEM for XTENSA
linux-user: Add proper strace format strings for

     getdents()/getdents64()

linux-user/hppa: Add signal trampoline for hppa target linux-user/hppa: Drop stack guard page on hppa target linux-user/hppa: Increase guest stack size to 80MB for hppa target linux-user/hppa: Allow PROT_GROWSUP and PROT_GROWSDOWN in mprotect() linux-user/hppa: Fix setup_sigcontext()

  linux-user/elfload.c             | 13 +++++---
  linux-user/hppa/signal.c         | 57 +++++++++++++++++++-------------
  linux-user/hppa/target_signal.h  | 14 +-------
  linux-user/hppa/target_syscall.h |  2 ++
  linux-user/main.c                |  9 +++--
  linux-user/mmap.c                |  2 ++
  linux-user/strace.list           |  4 +--
  linux-user/syscall.c             |  4 +++
  linux-user/syscall_defs.h        |  2 +-
  9 files changed, 62 insertions(+), 45 deletions(-)
Quoted 4 lines

--
2.37.3

Series applied to linux-user-for-7.2 branch.

Thanks,
Laurent