diff mbox series

[v16,2/9] linux-user: Add LoongArch signal support

Message ID 20220614090536.1103616-3-gaosong@loongson.cn
State New
Headers show
Series Add LoongArch linux-user emulation support | expand

Commit Message

gaosong June 14, 2022, 9:05 a.m. UTC
Signed-off-by: Song Gao <gaosong@loongson.cn>
Signed-off-by: Xiaojuan Yang <yangxiaojuan@loongson.cn>
---
 linux-user/loongarch64/signal.c        | 283 +++++++++++++++++++++++++
 linux-user/loongarch64/target_signal.h |  13 ++
 2 files changed, 296 insertions(+)
 create mode 100644 linux-user/loongarch64/signal.c
 create mode 100644 linux-user/loongarch64/target_signal.h

Comments

Richard Henderson June 14, 2022, 4:15 p.m. UTC | #1
On 6/14/22 02:05, Song Gao wrote:
> Signed-off-by: Song Gao <gaosong@loongson.cn>
> Signed-off-by: Xiaojuan Yang <yangxiaojuan@loongson.cn>
> ---
>   linux-user/loongarch64/signal.c        | 283 +++++++++++++++++++++++++
>   linux-user/loongarch64/target_signal.h |  13 ++
>   2 files changed, 296 insertions(+)
>   create mode 100644 linux-user/loongarch64/signal.c
>   create mode 100644 linux-user/loongarch64/target_signal.h

You copied too much directly from the kernel, without changing to match the host/guest 
split that is present in qemu.

> +struct target_ctx_layout {
> +    struct target_sctx_info *addr;

abi_ulong.

> +    unsigned int size;
> +};
> +
> +struct target_extctx_layout {
> +    unsigned long size;

unsigned int -- it only needs to hold sizeof(target_fpu_context) + 
sizeof(target_sctx_info).  Use of "unsigned long" in qemu is generally incorrect.

Both of these two structures should drop the "target_" prefix from the name, because they 
do not appear in target memory.  They are implementation details of this file.

> +static void *get_ctx(struct target_sctx_info *info)
> +{
> +    return (void *)((char *)info + sizeof(struct target_sctx_info));
> +}

Return type should be struct target_sctx_info *.

> +static unsigned long extframe_alloc(struct target_extctx_layout *extctx,
> +                                    struct target_ctx_layout *layout,
> +                                    size_t size, unsigned long base)
> +{
> +    unsigned long new_base = base - size;
> +
> +    new_base -= sizeof(struct target_sctx_info);
> +    layout->addr = (void *)new_base;
> +    layout->size = (unsigned int)(base - new_base);
> +    extctx->size += layout->size;

All of these unsigned long should be abi_ulong.
The cast into layout->addr is wrong.

> +static unsigned long setup_extcontext(struct target_extctx_layout *extctx,
> +                                      unsigned long sp)
> +{
> +    unsigned long new_sp = sp;
> +
> +    memset(extctx, 0, sizeof(struct target_extctx_layout));
> +    new_sp -= sizeof(struct target_sctx_info);
> +
> +    extctx->end.addr = (void *) new_sp;
> +    extctx->end.size = (unsigned int)sizeof(struct target_sctx_info);
> +    extctx->size += extctx->end.size;
> +    extctx->flags = SC_USED_FP;
> +
> +    new_sp = extframe_alloc(extctx, &extctx->fpu,
> +                            sizeof(struct target_fpu_context), new_sp);
> +
> +    return new_sp;
> +}

More unsigned long and casting errors.


> +static void restore_sigcontext(CPULoongArchState *env,
> +                               struct target_sigcontext *sc)
> +{
> +    int i;
> +    struct target_extctx_layout extctx;
> +
> +    memset(&extctx, 0, sizeof(struct target_extctx_layout));
> +
> +    __get_user(extctx.flags, &sc->sc_flags);
> +
> +    extctx.fpu.addr = (struct target_sctx_info *)&sc->sc_extcontext;

This is wrong.  You're missing all of the code from parse_extcontext().


r~
gaosong June 15, 2022, 9:57 a.m. UTC | #2
On 2022/6/15 上午12:15, Richard Henderson wrote:
>> +static void *get_ctx(struct target_sctx_info *info)
>> +{
>> +    return (void *)((char *)info + sizeof(struct target_sctx_info));
>> +}
>
> Return type should be struct target_sctx_info *. 

I wonder that if we return target_fpu_context * and rename get_ctx to 
get_fpu_context() would be better.

So we need't  cast  like this:

     struct target_fpu_context *fpu_ctx = (struct target_fpu_context *)
                                          get_ctx(info);


Thanks.
Song Gao
Richard Henderson June 15, 2022, 3 p.m. UTC | #3
On 6/15/22 02:57, gaosong wrote:
> 
> On 2022/6/15 上午12:15, Richard Henderson wrote:
>>> +static void *get_ctx(struct target_sctx_info *info)
>>> +{
>>> +    return (void *)((char *)info + sizeof(struct target_sctx_info));
>>> +}
>>
>> Return type should be struct target_sctx_info *. 
> 
> I wonder that if we return target_fpu_context * and rename get_ctx to get_fpu_context() 
> would be better.
> 
> So we need't  cast  like this:
> 
>      struct target_fpu_context *fpu_ctx = (struct target_fpu_context *)
>                                           get_ctx(info);

Oh, hmm.  I think I mis-read the kernel code before -- retain the return type of void* so 
that you do not need the cast at all in the caller.

The computation in the function is weirdly over-complicated -- it's really just "return 
info + 1".


r~
diff mbox series

Patch

diff --git a/linux-user/loongarch64/signal.c b/linux-user/loongarch64/signal.c
new file mode 100644
index 0000000000..ff7c5d611b
--- /dev/null
+++ b/linux-user/loongarch64/signal.c
@@ -0,0 +1,283 @@ 
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * LoongArch emulation of Linux signals
+ *
+ * Copyright (c) 2021 Loongson Technology Corporation Limited
+ */
+
+#include "qemu/osdep.h"
+#include "qemu.h"
+#include "user-internals.h"
+#include "signal-common.h"
+#include "linux-user/trace.h"
+
+#include "target/loongarch/internals.h"
+
+/* FP context was used */
+#define SC_USED_FP              (1 << 0)
+
+struct target_sigcontext {
+    uint64_t sc_pc;
+    uint64_t sc_regs[32];
+    uint32_t sc_flags;
+    uint64_t sc_extcontext[0] __attribute__((aligned(16)));
+};
+
+struct target_fpu_context {
+    uint64_t regs[32];
+    uint64_t fcc;
+    uint32_t fcsr;
+};
+
+struct target_sctx_info {
+    uint32_t magic;
+    uint32_t size;
+    uint64_t padding;  /* padding to 16 bytes */
+};
+
+struct target_ucontext {
+    target_ulong tuc_flags;
+    struct target_ucontext *tuc_link;
+    target_stack_t tuc_stack;
+    target_sigset_t tuc_sigmask;
+    uint8_t __unused[1024 / 8 - sizeof(target_sigset_t)];
+    struct target_sigcontext tuc_mcontext;
+};
+
+struct target_rt_sigframe {
+    struct target_siginfo rs_info;
+    struct target_ucontext rs_uc;
+};
+
+struct target_ctx_layout {
+    struct target_sctx_info *addr;
+    unsigned int size;
+};
+
+struct target_extctx_layout {
+    unsigned long size;
+    unsigned int flags;
+    struct target_ctx_layout fpu;
+    struct target_ctx_layout end;
+};
+
+static uint64_t read_all_fcc(CPULoongArchState *env)
+{
+    uint64_t ret = 0;
+
+    for (int i = 0; i < 8; ++i) {
+        ret |= (uint64_t)env->cf[i] << (i * 8);
+    }
+
+    return ret;
+}
+
+static void write_all_fcc(CPULoongArchState *env, uint64_t val)
+{
+    for (int i = 0; i < 8; ++i) {
+        env->cf[i] = (val >> (i * 8)) & 1;
+    }
+}
+
+static void *get_ctx(struct target_sctx_info *info)
+{
+    return (void *)((char *)info + sizeof(struct target_sctx_info));
+}
+
+static void copy_fpu_to_sigcontext(CPULoongArchState *env,
+                                   struct target_extctx_layout *extctx)
+{
+    int i;
+    struct target_sctx_info *info = extctx->fpu.addr;
+    struct target_fpu_context *fpu_ctx = (struct target_fpu_context *)
+                                         get_ctx(info);
+
+    for (i = 1; i < 32; ++i) {
+        __put_user(env->fpr[i], &fpu_ctx->regs[i]);
+    }
+
+    fpu_ctx->fcc = read_all_fcc(env);
+    __put_user(env->fcsr0, &fpu_ctx->fcsr);
+    __put_user(extctx->fpu.size, &info->size);
+}
+
+static unsigned long extframe_alloc(struct target_extctx_layout *extctx,
+                                    struct target_ctx_layout *layout,
+                                    size_t size, unsigned long base)
+{
+    unsigned long new_base = base - size;
+
+    new_base -= sizeof(struct target_sctx_info);
+    layout->addr = (void *)new_base;
+    layout->size = (unsigned int)(base - new_base);
+    extctx->size += layout->size;
+
+    return new_base;
+}
+
+static unsigned long setup_extcontext(struct target_extctx_layout *extctx,
+                                      unsigned long sp)
+{
+    unsigned long new_sp = sp;
+
+    memset(extctx, 0, sizeof(struct target_extctx_layout));
+    new_sp -= sizeof(struct target_sctx_info);
+
+    extctx->end.addr = (void *) new_sp;
+    extctx->end.size = (unsigned int)sizeof(struct target_sctx_info);
+    extctx->size += extctx->end.size;
+    extctx->flags = SC_USED_FP;
+
+    new_sp = extframe_alloc(extctx, &extctx->fpu,
+                            sizeof(struct target_fpu_context), new_sp);
+
+    return new_sp;
+}
+
+static void setup_sigcontext(CPULoongArchState *env,
+                             struct target_sigcontext *sc,
+                             struct target_extctx_layout *extctx)
+{
+    int i;
+
+    __put_user(extctx->flags, &sc->sc_flags);
+    __put_user(env->pc, &sc->sc_pc);
+
+    for (i = 1; i < 32; ++i) {
+        __put_user(env->gpr[i], &sc->sc_regs[i]);
+    }
+
+    copy_fpu_to_sigcontext(env, extctx);
+}
+
+static void copy_fpu_from_sigcontext(CPULoongArchState *env,
+                                     struct target_extctx_layout *extctx)
+{
+    int i;
+    struct target_sctx_info *info = extctx->fpu.addr;
+    struct target_fpu_context *fpu_ctx = (struct target_fpu_context *)
+                                         get_ctx(info);
+
+    for (i = 1; i < 32; ++i) {
+        __get_user(env->fpr[i], &fpu_ctx->regs[i]);
+    }
+    write_all_fcc(env, fpu_ctx->fcc);
+    __get_user(env->fcsr0, &fpu_ctx->fcsr);
+}
+
+static void restore_sigcontext(CPULoongArchState *env,
+                               struct target_sigcontext *sc)
+{
+    int i;
+    struct target_extctx_layout extctx;
+
+    memset(&extctx, 0, sizeof(struct target_extctx_layout));
+
+    __get_user(extctx.flags, &sc->sc_flags);
+
+    extctx.fpu.addr = (struct target_sctx_info *)&sc->sc_extcontext;
+
+    __get_user(env->pc, &sc->sc_pc);
+    for (i = 1; i < 32; ++i) {
+        __get_user(env->gpr[i], &sc->sc_regs[i]);
+    }
+
+    copy_fpu_from_sigcontext(env, &extctx);
+    restore_fp_status(env);
+}
+
+/*
+ * Determine which stack to use.
+ */
+static abi_ulong get_sigframe(struct target_sigaction *ka,
+                              CPULoongArchState *env, size_t frame_size,
+                              struct target_extctx_layout *extctx)
+{
+    unsigned long sp;
+
+    sp = target_sigsp(get_sp_from_cpustate(env) - 32, ka);
+    sp = setup_extcontext(extctx, sp);
+
+    return (sp - frame_size) & ~15;
+}
+
+void setup_rt_frame(int sig, struct target_sigaction *ka,
+                    target_siginfo_t *info,
+                    target_sigset_t *set, CPULoongArchState *env)
+{
+    struct target_rt_sigframe *frame;
+    struct target_extctx_layout extctx;
+    abi_ulong frame_addr;
+    int i;
+
+    frame_addr = get_sigframe(ka, env, sizeof(*frame), &extctx);
+    trace_user_setup_rt_frame(env, frame_addr);
+    if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
+        goto give_sigsegv;
+    }
+
+    tswap_siginfo(&frame->rs_info, info);
+
+    __put_user(0, &frame->rs_uc.tuc_flags);
+    __put_user(0, &frame->rs_uc.tuc_link);
+    target_save_altstack(&frame->rs_uc.tuc_stack, env);
+
+    setup_sigcontext(env, &frame->rs_uc.tuc_mcontext, &extctx);
+
+    for (i = 0; i < TARGET_NSIG_WORDS; i++) {
+        __put_user(set->sig[i], &frame->rs_uc.tuc_sigmask.sig[i]);
+    }
+
+    env->gpr[4] = sig;
+    env->gpr[5] = frame_addr + offsetof(struct target_rt_sigframe, rs_info);
+    env->gpr[6] = frame_addr + offsetof(struct target_rt_sigframe, rs_uc);
+    env->gpr[3] = frame_addr;
+    env->gpr[1] = default_rt_sigreturn;
+
+    env->pc = ka->_sa_handler;
+    unlock_user_struct(frame, frame_addr, 1);
+    return;
+
+give_sigsegv:
+    unlock_user_struct(frame, frame_addr, 1);
+    force_sigsegv(sig);
+}
+
+long do_rt_sigreturn(CPULoongArchState *env)
+{
+    struct target_rt_sigframe *frame;
+    abi_ulong frame_addr;
+    sigset_t blocked;
+
+    frame_addr = env->gpr[3];
+    trace_user_do_rt_sigreturn(env, frame_addr);
+    if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
+        goto badframe;
+    }
+
+    target_to_host_sigset(&blocked, &frame->rs_uc.tuc_sigmask);
+    set_sigmask(&blocked);
+
+    restore_sigcontext(env, &frame->rs_uc.tuc_mcontext);
+    target_restore_altstack(&frame->rs_uc.tuc_stack, env);
+
+    unlock_user_struct(frame, frame_addr, 0);
+    return -QEMU_ESIGRETURN;
+
+badframe:
+    unlock_user_struct(frame, frame_addr, 0);
+    force_sig(TARGET_SIGSEGV);
+    return -QEMU_ESIGRETURN;
+}
+
+void setup_sigtramp(abi_ulong sigtramp_page)
+{
+    uint32_t *tramp = lock_user(VERIFY_WRITE, sigtramp_page, 8, 0);
+    assert(tramp != NULL);
+
+    __put_user(0x03822c0b, tramp + 0);  /* ori     a7, zero, 0x8b */
+    __put_user(0x002b0000, tramp + 1);  /* syscall 0 */
+
+    default_rt_sigreturn = sigtramp_page;
+    unlock_user(tramp, sigtramp_page, 8);
+}
diff --git a/linux-user/loongarch64/target_signal.h b/linux-user/loongarch64/target_signal.h
new file mode 100644
index 0000000000..ad3aaffcb4
--- /dev/null
+++ b/linux-user/loongarch64/target_signal.h
@@ -0,0 +1,13 @@ 
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Copyright (c) 2021 Loongson Technology Corporation Limited
+ */
+
+#ifndef LOONGARCH_TARGET_SIGNAL_H
+#define LOONGARCH_TARGET_SIGNAL_H
+
+#include "../generic/signal.h"
+
+#define TARGET_ARCH_HAS_SIGTRAMP_PAGE 1
+
+#endif /* LOONGARCH_TARGET_SIGNAL_H */