qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Peter Maydell <peter.maydell@linaro.org>
To: Richard Henderson <richard.henderson@linaro.org>
Cc: QEMU Developers <qemu-devel@nongnu.org>,
	Laurent Vivier <laurent@vivier.eu>
Subject: Re: [PATCH v3 05/27] linux-user/arm: Implement setup_sigtramp
Date: Mon, 27 Sep 2021 12:06:17 +0100	[thread overview]
Message-ID: <CAFEAcA85RoJO5he8pLw9bcDnav84yz2RiRcuzdu9dJF-QKZaeA@mail.gmail.com> (raw)
In-Reply-To: <20210924165926.752809-6-richard.henderson@linaro.org>

On Fri, 24 Sept 2021 at 17:59, Richard Henderson
<richard.henderson@linaro.org> wrote:
>
> Update the trampoline code to match the kernel: this uses
> sp-relative accesses rather than pc-relative.
>
> Signed-off-by: Richard Henderson <richard.henderson@linaro.org>

These functions must write at most 8 bytes:

> +static void write_arm_sigreturn(uint32_t *rc, int syscall)
> +{
> +    __put_user(ARM_MOV_R7_IMM(syscall), rc);
> +    __put_user(ARM_SWI_SYS(syscall), rc + 1);
> +}
> +
> +static void write_thumb_sigreturn(uint32_t *rc, int syscall)
> +{
> +    __put_user(THUMB_SWI_SYS << 16 | THUMB_MOVS_R7_IMM(syscall), rc);
> +}
>
>  /*
> - * Stub needed to make sure the FD register (r9) contains the right
> - * value.
> + * Stub needed to make sure the FD register (r9) contains the right value.
> + * Use the same instruction sequence as the kernel.
>   */
> -static const unsigned long sigreturn_fdpic_codes[3] = {
> -    0xe59fc004, /* ldr r12, [pc, #4] to read function descriptor */
> -    0xe59c9004, /* ldr r9, [r12, #4] to setup GOT */
> -    0xe59cf000  /* ldr pc, [r12] to jump into restorer */
> -};

...and these must write at most 12 bytes. But nothing states
or asserts that.

> +static void write_arm_fdpic_sigreturn(uint32_t *rc, int ofs)
> +{
> +    assert(ofs <= 0xfff);
> +    __put_user(0xe59d3000 | ofs, rc + 0);   /* ldr r3, [sp, #ofs] */
> +    __put_user(0xe8930908, rc + 1);         /* ldm r3, { r3, r9 } */
> +    __put_user(0xe12fff13, rc + 2);         /* bx  r3 */
> +}
>
> -static const unsigned long sigreturn_fdpic_thumb_codes[3] = {
> -    0xc008f8df, /* ldr r12, [pc, #8] to read function descriptor */
> -    0x9004f8dc, /* ldr r9, [r12, #4] to setup GOT */
> -    0xf000f8dc  /* ldr pc, [r12] to jump into restorer */
> -};
> +static void write_thumb_fdpic_sigreturn(void *vrc, int ofs)
> +{
> +    uint16_t *rc = vrc;
> +
> +    assert((ofs & ~0x3fc) == 0);
> +    __put_user(0x9b00 | (ofs >> 2), rc + 0);      /* ldr r3, [sp, #ofs] */
> +    __put_user(0xcb0c, rc + 1);                   /* ldm r3, { r2, r3 } */
> +    __put_user(0x4699, rc + 2);                   /* mov r9, r3 */
> +    __put_user(0x4710, rc + 3);                   /* bx  r2 */
> +}
>

> -            retcode = rc_addr + thumb;
> +            /* Each trampoline variant consumes a 12-byte slot. */
> +            retcode = sigreturn_fdpic_tramp + retcode_idx * 12 + thumb;
>          } else {
>              retcode = ka->sa_restorer;
>          }
>      } else {

> -
> -        retcode = rc_addr + thumb;
> +        /* Each trampoline variant consumes 8-byte slot. */
> +        retcode = default_sigreturn + retcode_idx * 8 + thumb;

These 12 and 8 magic numbers correspond to the maximum sequence sizes
above...

> +void setup_sigtramp(abi_ulong sigtramp_page)
> +{
> +    enum {
> +        SIGFRAME_FDPIC_OFS = offsetof(struct sigframe, retcode[3]),
> +        RT_SIGFRAME_FDPIC_OFS = offsetof(struct rt_sigframe, retcode[3]),
> +    };
> +
> +    uint32_t total_size = 4 * 8 + 4 * 12;
> +    uint32_t *tramp = lock_user(VERIFY_WRITE, sigtramp_page, total_size, 0);
> +    uint32_t i = 0;
> +
> +    assert(tramp != NULL);
> +
> +    default_sigreturn = sigtramp_page;
> +    write_arm_sigreturn(&tramp[i], TARGET_NR_sigreturn);
> +    i += 2;
> +    write_thumb_sigreturn(&tramp[i], TARGET_NR_sigreturn);
> +    i += 2;
> +    write_arm_sigreturn(&tramp[i], TARGET_NR_rt_sigreturn);
> +    i += 2;
> +    write_thumb_sigreturn(&tramp[i], TARGET_NR_rt_sigreturn);
> +    i += 2;

...and these "+=2" and the "+=3" later do as well, but with
a count of 32-bit words rather than bytes. I think it would be
useful to at least have some defined constants for the lengths
rather than hard-coded 8,12,2,3, and comments that the write_
functions must not write more than however-many bytes.

> +
> +    /*
> +     * FDPIC require trampolines to call sa_restorer, and different
> +     * from the pc-relative versions we write to the stack.
> +     *
> +     * ARM versions use:
> +     *    ldr   r3, [sp, #ofs]
> +     *    ldr   r9, [r3, #4]
> +     *    ldr   pc, [r3, #0]

This comment doesn't match the code that write_arm_fdpic_sigreturn()
now generates. The "different from the pc-relative versions we
write from the stack" bit doesn't seem to be right either, given
we call the same functions in both places to write the code.

> +     *
> +     * Thumb versions use:
> +     *    ldr   r3, [sp, #ofs]
> +     *    ldmia r3, {r2, r3}
> +     *    mov   r9, r3
> +     *    bx    r2
> +     */
> +    sigreturn_fdpic_tramp = sigtramp_page + i * 4;
> +
> +    /* ARM sigframe */
> +    write_arm_fdpic_sigreturn(tramp + i,
> +                              offsetof(struct sigframe, retcode[3]));
> +    i += 3;
> +
> +    /* Thumb sigframe */
> +    write_thumb_fdpic_sigreturn(tramp + i,
> +                                offsetof(struct sigframe, retcode[3]));
> +    i += 3;
> +
> +    /* ARM rt_sigframe */
> +    write_arm_fdpic_sigreturn(tramp + i,
> +                              offsetof(struct rt_sigframe, retcode[3]));
> +    i += 3;
> +
> +    /* Thumb rt_sigframe */
> +    write_thumb_fdpic_sigreturn(tramp + i,
> +                                offsetof(struct rt_sigframe, retcode[3]));
> +    i += 3;
> +
> +    assert(i * 4 == total_size);
> +    unlock_user(tramp, sigtramp_page, total_size);
> +}
> --
> 2.25.1

thanks
-- PMM


  reply	other threads:[~2021-09-27 11:09 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-24 16:58 [PATCH v3 00/27] linux-user: Move signal trampolines to new page Richard Henderson
2021-09-24 16:59 ` [PATCH v3 01/27] linux-user: Add infrastructure for a signal trampoline page Richard Henderson
2021-09-24 16:59 ` [PATCH v3 02/27] linux-user/aarch64: Implement setup_sigtramp Richard Henderson
2021-09-24 16:59 ` [PATCH v3 03/27] linux-user/arm: Drop v1 signal frames Richard Henderson
2021-09-24 16:59 ` [PATCH v3 04/27] linux-user/arm: Drop "_v2" from symbols in signal.c Richard Henderson
2021-09-24 16:59 ` [PATCH v3 05/27] linux-user/arm: Implement setup_sigtramp Richard Henderson
2021-09-27 11:06   ` Peter Maydell [this message]
2021-09-24 16:59 ` [PATCH v3 06/27] linux-user/alpha: " Richard Henderson
2021-09-24 16:59 ` [PATCH v3 07/27] linux-user/cris: " Richard Henderson
2021-09-24 16:59 ` [PATCH v3 08/27] linux-user/hexagon: " Richard Henderson
2021-09-24 18:49   ` Taylor Simpson
2021-09-24 16:59 ` [PATCH v3 09/27] linux-user/hppa: Document non-use of setup_sigtramp Richard Henderson
2021-09-24 16:59 ` [PATCH v3 10/27] linux-user/i386: Implement setup_sigtramp Richard Henderson
2021-09-24 18:01   ` Philippe Mathieu-Daudé
2021-09-28  1:42     ` Richard Henderson
2021-09-28  6:50       ` Philippe Mathieu-Daudé
2021-09-24 16:59 ` [PATCH v3 11/27] linux-user/x86_64: Raise SIGSEGV if SA_RESTORER not set Richard Henderson
2021-09-27 13:01   ` Peter Maydell
2021-09-24 16:59 ` [PATCH v3 12/27] linux-user/m68k: Implement setup_sigtramp Richard Henderson
2021-09-24 16:59 ` [PATCH v3 13/27] linux-user/microblaze: " Richard Henderson
2021-09-24 16:59 ` [PATCH v3 14/27] linux-user/mips: Tidy install_sigtramp Richard Henderson
2021-09-24 16:59 ` [PATCH v3 15/27] linux-user/mips: Implement setup_sigtramp Richard Henderson
2021-09-24 16:59 ` [PATCH v3 16/27] linux-user/nios2: Properly emulate EXCP_TRAP Richard Henderson
2021-09-27 13:23   ` Peter Maydell
2021-09-27 14:30     ` Richard Henderson
2021-09-24 16:59 ` [PATCH v3 17/27] linux-user/nios2: Map a real kuser page Richard Henderson
2021-09-27 13:26   ` Peter Maydell
2021-09-27 13:59     ` Richard Henderson
2021-09-24 16:59 ` [PATCH v3 18/27] linux-user/nios2: Fixes for signal frame setup Richard Henderson
2021-09-27 13:28   ` Peter Maydell
2021-09-24 16:59 ` [PATCH v3 19/27] linux-user/openrisc: Implement setup_sigtramp Richard Henderson
2021-09-24 16:59 ` [PATCH v3 20/27] linux-user/ppc: Simplify encode_trampoline Richard Henderson
2021-09-24 17:55   ` Philippe Mathieu-Daudé
2021-09-24 16:59 ` [PATCH v3 21/27] linux-user/ppc: Implement setup_sigtramp Richard Henderson
2021-09-27 13:34   ` Peter Maydell
2021-09-24 16:59 ` [PATCH v3 22/27] linux-user/riscv: " Richard Henderson
2021-09-24 16:59 ` [PATCH v3 23/27] linux-user/s390x: " Richard Henderson
2021-09-24 16:59 ` [PATCH v3 24/27] linux-user/sh4: " Richard Henderson
2021-09-24 16:59 ` [PATCH v3 25/27] linux-user/sparc: " Richard Henderson
2021-09-27 13:30   ` Peter Maydell
2021-09-24 16:59 ` [PATCH v3 26/27] linux-user/xtensa: " Richard Henderson
2021-09-24 17:53   ` Philippe Mathieu-Daudé
2021-09-24 16:59 ` [PATCH v3 27/27] linux-user: Remove default for TARGET_ARCH_HAS_SIGTRAMP_PAGE Richard Henderson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAFEAcA85RoJO5he8pLw9bcDnav84yz2RiRcuzdu9dJF-QKZaeA@mail.gmail.com \
    --to=peter.maydell@linaro.org \
    --cc=laurent@vivier.eu \
    --cc=qemu-devel@nongnu.org \
    --cc=richard.henderson@linaro.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).