cachepc-qemu

Fork of AMDESE/qemu with changes for cachepc side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-qemu
Log | Files | Refs | Submodules | LICENSE | sfeed.txt

signal.c (12601B)


      1/*
      2 *  Emulation of Linux signals
      3 *
      4 *  Copyright (c) 2003 Fabrice Bellard
      5 *
      6 *  This program is free software; you can redistribute it and/or modify
      7 *  it under the terms of the GNU General Public License as published by
      8 *  the Free Software Foundation; either version 2 of the License, or
      9 *  (at your option) any later version.
     10 *
     11 *  This program is distributed in the hope that it will be useful,
     12 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     13 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14 *  GNU General Public License for more details.
     15 *
     16 *  You should have received a copy of the GNU General Public License
     17 *  along with this program; if not, see <http://www.gnu.org/licenses/>.
     18 */
     19#include "qemu/osdep.h"
     20#include "qemu.h"
     21#include "user-internals.h"
     22#include "signal-common.h"
     23#include "linux-user/trace.h"
     24
     25#define __NUM_GPRS 16
     26#define __NUM_FPRS 16
     27#define __NUM_ACRS 16
     28
     29#define __SIGNAL_FRAMESIZE      160 /* FIXME: 31-bit mode -> 96 */
     30
     31#define _SIGCONTEXT_NSIG        64
     32#define _SIGCONTEXT_NSIG_BPW    64 /* FIXME: 31-bit mode -> 32 */
     33#define _SIGCONTEXT_NSIG_WORDS  (_SIGCONTEXT_NSIG / _SIGCONTEXT_NSIG_BPW)
     34#define _SIGMASK_COPY_SIZE    (sizeof(unsigned long)*_SIGCONTEXT_NSIG_WORDS)
     35#define S390_SYSCALL_OPCODE ((uint16_t)0x0a00)
     36
     37typedef struct {
     38    target_psw_t psw;
     39    abi_ulong gprs[__NUM_GPRS];
     40    abi_uint acrs[__NUM_ACRS];
     41} target_s390_regs_common;
     42
     43typedef struct {
     44    uint32_t fpc;
     45    uint32_t pad;
     46    uint64_t fprs[__NUM_FPRS];
     47} target_s390_fp_regs;
     48
     49typedef struct {
     50    target_s390_regs_common regs;
     51    target_s390_fp_regs     fpregs;
     52} target_sigregs;
     53
     54typedef struct {
     55    uint64_t vxrs_low[16];
     56    uint64_t vxrs_high[16][2];
     57    uint8_t reserved[128];
     58} target_sigregs_ext;
     59
     60typedef struct {
     61    abi_ulong oldmask[_SIGCONTEXT_NSIG_WORDS];
     62    abi_ulong sregs;
     63} target_sigcontext;
     64
     65typedef struct {
     66    uint8_t callee_used_stack[__SIGNAL_FRAMESIZE];
     67    target_sigcontext sc;
     68    target_sigregs sregs;
     69    int signo;
     70    target_sigregs_ext sregs_ext;
     71} sigframe;
     72
     73#define TARGET_UC_VXRS 2
     74
     75struct target_ucontext {
     76    abi_ulong tuc_flags;
     77    abi_ulong tuc_link;
     78    target_stack_t tuc_stack;
     79    target_sigregs tuc_mcontext;
     80    target_sigset_t tuc_sigmask;
     81    uint8_t reserved[128 - sizeof(target_sigset_t)];
     82    target_sigregs_ext tuc_mcontext_ext;
     83};
     84
     85typedef struct {
     86    uint8_t callee_used_stack[__SIGNAL_FRAMESIZE];
     87    struct target_siginfo info;
     88    struct target_ucontext uc;
     89} rt_sigframe;
     90
     91static inline abi_ulong
     92get_sigframe(struct target_sigaction *ka, CPUS390XState *env, size_t frame_size)
     93{
     94    abi_ulong sp;
     95
     96    /* Default to using normal stack */
     97    sp = get_sp_from_cpustate(env);
     98
     99    /* This is the X/Open sanctioned signal stack switching.  */
    100    if (ka->sa_flags & TARGET_SA_ONSTACK) {
    101        sp = target_sigsp(sp, ka);
    102    }
    103
    104    /* This is the legacy signal stack switching. */
    105    else if (/* FIXME !user_mode(regs) */ 0 &&
    106             !(ka->sa_flags & TARGET_SA_RESTORER) &&
    107             ka->sa_restorer) {
    108        sp = (abi_ulong) ka->sa_restorer;
    109    }
    110
    111    return (sp - frame_size) & -8ul;
    112}
    113
    114#define PSW_USER_BITS   (PSW_MASK_DAT | PSW_MASK_IO | PSW_MASK_EXT | \
    115                         PSW_MASK_MCHECK | PSW_MASK_PSTATE | PSW_ASC_PRIMARY)
    116#define PSW_MASK_USER   (PSW_MASK_ASC | PSW_MASK_CC | PSW_MASK_PM | \
    117                         PSW_MASK_64 | PSW_MASK_32)
    118
    119static void save_sigregs(CPUS390XState *env, target_sigregs *sregs)
    120{
    121    uint64_t psw_mask = s390_cpu_get_psw_mask(env);
    122    int i;
    123
    124    /*
    125     * Copy a 'clean' PSW mask to the user to avoid leaking
    126     * information about whether PER is currently on.
    127     * TODO: qemu does not support PSW_MASK_RI; it will never be set.
    128     */
    129    psw_mask = PSW_USER_BITS | (psw_mask & PSW_MASK_USER);
    130    __put_user(psw_mask, &sregs->regs.psw.mask);
    131    __put_user(env->psw.addr, &sregs->regs.psw.addr);
    132
    133    for (i = 0; i < 16; i++) {
    134        __put_user(env->regs[i], &sregs->regs.gprs[i]);
    135    }
    136    for (i = 0; i < 16; i++) {
    137        __put_user(env->aregs[i], &sregs->regs.acrs[i]);
    138    }
    139
    140    /*
    141     * We have to store the fp registers to current->thread.fp_regs
    142     * to merge them with the emulated registers.
    143     */
    144    for (i = 0; i < 16; i++) {
    145        __put_user(*get_freg(env, i), &sregs->fpregs.fprs[i]);
    146    }
    147}
    148
    149static void save_sigregs_ext(CPUS390XState *env, target_sigregs_ext *ext)
    150{
    151    int i;
    152
    153    /*
    154     * if (MACHINE_HAS_VX) ...
    155     * That said, we always allocate the stack storage and the
    156     * space is always available in env.
    157     */
    158    for (i = 0; i < 16; ++i) {
    159        __put_user(env->vregs[i][1], &ext->vxrs_low[i]);
    160    }
    161    for (i = 0; i < 16; ++i) {
    162        __put_user(env->vregs[i + 16][0], &ext->vxrs_high[i][0]);
    163        __put_user(env->vregs[i + 16][1], &ext->vxrs_high[i][1]);
    164    }
    165}
    166
    167void setup_frame(int sig, struct target_sigaction *ka,
    168                 target_sigset_t *set, CPUS390XState *env)
    169{
    170    sigframe *frame;
    171    abi_ulong frame_addr;
    172    abi_ulong restorer;
    173
    174    frame_addr = get_sigframe(ka, env, sizeof(*frame));
    175    trace_user_setup_frame(env, frame_addr);
    176    if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
    177        force_sigsegv(sig);
    178        return;
    179    }
    180
    181    /* Set up backchain. */
    182    __put_user(env->regs[15], (abi_ulong *) frame);
    183
    184    /* Create struct sigcontext on the signal stack. */
    185    /* Make sure that we're initializing all of oldmask. */
    186    QEMU_BUILD_BUG_ON(ARRAY_SIZE(frame->sc.oldmask) != 1);
    187    __put_user(set->sig[0], &frame->sc.oldmask[0]);
    188    __put_user(frame_addr + offsetof(sigframe, sregs), &frame->sc.sregs);
    189
    190    /* Create _sigregs on the signal stack */
    191    save_sigregs(env, &frame->sregs);
    192
    193    /*
    194     * ??? The kernel uses regs->gprs[2] here, which is not yet the signo.
    195     * Moreover the comment talks about allowing backtrace, which is really
    196     * done by the r15 copy above.
    197     */
    198    __put_user(sig, &frame->signo);
    199
    200    /* Create sigregs_ext on the signal stack. */
    201    save_sigregs_ext(env, &frame->sregs_ext);
    202
    203    /*
    204     * Set up to return from userspace.
    205     * If provided, use a stub already in userspace.
    206     */
    207    if (ka->sa_flags & TARGET_SA_RESTORER) {
    208        restorer = ka->sa_restorer;
    209    } else {
    210        restorer = default_sigreturn;
    211    }
    212
    213    /* Set up registers for signal handler */
    214    env->regs[14] = restorer;
    215    env->regs[15] = frame_addr;
    216    /* Force default amode and default user address space control. */
    217    env->psw.mask = PSW_MASK_64 | PSW_MASK_32 | PSW_ASC_PRIMARY
    218                  | (env->psw.mask & ~PSW_MASK_ASC);
    219    env->psw.addr = ka->_sa_handler;
    220
    221    env->regs[2] = sig;
    222    env->regs[3] = frame_addr + offsetof(typeof(*frame), sc);
    223
    224    /*
    225     * We forgot to include these in the sigcontext.
    226     * To avoid breaking binary compatibility, they are passed as args.
    227     */
    228    env->regs[4] = 0; /* FIXME: regs->int_code & 127 */
    229    env->regs[5] = 0; /* FIXME: regs->int_parm_long */
    230    env->regs[6] = 0; /* FIXME: current->thread.last_break */
    231
    232    unlock_user_struct(frame, frame_addr, 1);
    233}
    234
    235void setup_rt_frame(int sig, struct target_sigaction *ka,
    236                    target_siginfo_t *info,
    237                    target_sigset_t *set, CPUS390XState *env)
    238{
    239    rt_sigframe *frame;
    240    abi_ulong frame_addr;
    241    abi_ulong restorer;
    242    abi_ulong uc_flags;
    243
    244    frame_addr = get_sigframe(ka, env, sizeof *frame);
    245    trace_user_setup_rt_frame(env, frame_addr);
    246    if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
    247        force_sigsegv(sig);
    248        return;
    249    }
    250
    251    /* Set up backchain. */
    252    __put_user(env->regs[15], (abi_ulong *) frame);
    253
    254    /*
    255     * Set up to return from userspace.
    256     * If provided, use a stub already in userspace.
    257     */
    258    if (ka->sa_flags & TARGET_SA_RESTORER) {
    259        restorer = ka->sa_restorer;
    260    } else {
    261        restorer = default_rt_sigreturn;
    262    }
    263
    264    /* Create siginfo on the signal stack. */
    265    tswap_siginfo(&frame->info, info);
    266
    267    /* Create ucontext on the signal stack. */
    268    uc_flags = 0;
    269    if (s390_has_feat(S390_FEAT_VECTOR)) {
    270        uc_flags |= TARGET_UC_VXRS;
    271    }
    272    __put_user(uc_flags, &frame->uc.tuc_flags);
    273    __put_user(0, &frame->uc.tuc_link);
    274    target_save_altstack(&frame->uc.tuc_stack, env);
    275    save_sigregs(env, &frame->uc.tuc_mcontext);
    276    save_sigregs_ext(env, &frame->uc.tuc_mcontext_ext);
    277    tswap_sigset(&frame->uc.tuc_sigmask, set);
    278
    279    /* Set up registers for signal handler */
    280    env->regs[14] = restorer;
    281    env->regs[15] = frame_addr;
    282    /* Force default amode and default user address space control. */
    283    env->psw.mask = PSW_MASK_64 | PSW_MASK_32 | PSW_ASC_PRIMARY
    284                  | (env->psw.mask & ~PSW_MASK_ASC);
    285    env->psw.addr = ka->_sa_handler;
    286
    287    env->regs[2] = sig;
    288    env->regs[3] = frame_addr + offsetof(typeof(*frame), info);
    289    env->regs[4] = frame_addr + offsetof(typeof(*frame), uc);
    290    env->regs[5] = 0; /* FIXME: current->thread.last_break */
    291}
    292
    293static void restore_sigregs(CPUS390XState *env, target_sigregs *sc)
    294{
    295    uint64_t prev_addr, prev_mask, mask, addr;
    296    int i;
    297
    298    for (i = 0; i < 16; i++) {
    299        __get_user(env->regs[i], &sc->regs.gprs[i]);
    300    }
    301
    302    prev_addr = env->psw.addr;
    303    __get_user(mask, &sc->regs.psw.mask);
    304    __get_user(addr, &sc->regs.psw.addr);
    305    trace_user_s390x_restore_sigregs(env, addr, prev_addr);
    306
    307    /*
    308     * Use current psw.mask to preserve PER bit.
    309     * TODO:
    310     *  if (!is_ri_task(current) && (user_sregs.regs.psw.mask & PSW_MASK_RI))
    311     *          return -EINVAL;
    312     * Simply do not allow it to be set in mask.
    313     */
    314    prev_mask = s390_cpu_get_psw_mask(env);
    315    mask = (prev_mask & ~PSW_MASK_USER) | (mask & PSW_MASK_USER);
    316    /* Check for invalid user address space control. */
    317    if ((mask & PSW_MASK_ASC) == PSW_ASC_HOME) {
    318        mask = (mask & ~PSW_MASK_ASC) | PSW_ASC_PRIMARY;
    319    }
    320    /* Check for invalid amode. */
    321    if (mask & PSW_MASK_64) {
    322        mask |= PSW_MASK_32;
    323    }
    324    s390_cpu_set_psw(env, mask, addr);
    325
    326    for (i = 0; i < 16; i++) {
    327        __get_user(env->aregs[i], &sc->regs.acrs[i]);
    328    }
    329    for (i = 0; i < 16; i++) {
    330        __get_user(*get_freg(env, i), &sc->fpregs.fprs[i]);
    331    }
    332}
    333
    334static void restore_sigregs_ext(CPUS390XState *env, target_sigregs_ext *ext)
    335{
    336    int i;
    337
    338    /*
    339     * if (MACHINE_HAS_VX) ...
    340     * That said, we always allocate the stack storage and the
    341     * space is always available in env.
    342     */
    343    for (i = 0; i < 16; ++i) {
    344        __get_user(env->vregs[i][1], &ext->vxrs_low[i]);
    345    }
    346    for (i = 0; i < 16; ++i) {
    347        __get_user(env->vregs[i + 16][0], &ext->vxrs_high[i][0]);
    348        __get_user(env->vregs[i + 16][1], &ext->vxrs_high[i][1]);
    349    }
    350}
    351
    352long do_sigreturn(CPUS390XState *env)
    353{
    354    sigframe *frame;
    355    abi_ulong frame_addr = env->regs[15];
    356    target_sigset_t target_set;
    357    sigset_t set;
    358
    359    trace_user_do_sigreturn(env, frame_addr);
    360    if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
    361        force_sig(TARGET_SIGSEGV);
    362        return -TARGET_QEMU_ESIGRETURN;
    363    }
    364
    365    /* Make sure that we're initializing all of target_set. */
    366    QEMU_BUILD_BUG_ON(ARRAY_SIZE(target_set.sig) != 1);
    367    __get_user(target_set.sig[0], &frame->sc.oldmask[0]);
    368
    369    target_to_host_sigset_internal(&set, &target_set);
    370    set_sigmask(&set); /* ~_BLOCKABLE? */
    371
    372    restore_sigregs(env, &frame->sregs);
    373    restore_sigregs_ext(env, &frame->sregs_ext);
    374
    375    unlock_user_struct(frame, frame_addr, 0);
    376    return -TARGET_QEMU_ESIGRETURN;
    377}
    378
    379long do_rt_sigreturn(CPUS390XState *env)
    380{
    381    rt_sigframe *frame;
    382    abi_ulong frame_addr = env->regs[15];
    383    sigset_t set;
    384
    385    trace_user_do_rt_sigreturn(env, frame_addr);
    386    if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
    387        force_sig(TARGET_SIGSEGV);
    388        return -TARGET_QEMU_ESIGRETURN;
    389    }
    390    target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
    391
    392    set_sigmask(&set); /* ~_BLOCKABLE? */
    393
    394    restore_sigregs(env, &frame->uc.tuc_mcontext);
    395    restore_sigregs_ext(env, &frame->uc.tuc_mcontext_ext);
    396
    397    target_restore_altstack(&frame->uc.tuc_stack, env);
    398
    399    unlock_user_struct(frame, frame_addr, 0);
    400    return -TARGET_QEMU_ESIGRETURN;
    401}
    402
    403void setup_sigtramp(abi_ulong sigtramp_page)
    404{
    405    uint16_t *tramp = lock_user(VERIFY_WRITE, sigtramp_page, 2 + 2, 0);
    406    assert(tramp != NULL);
    407
    408    default_sigreturn = sigtramp_page;
    409    __put_user(S390_SYSCALL_OPCODE | TARGET_NR_sigreturn, &tramp[0]);
    410
    411    default_rt_sigreturn = sigtramp_page + 2;
    412    __put_user(S390_SYSCALL_OPCODE | TARGET_NR_rt_sigreturn, &tramp[1]);
    413
    414    unlock_user(tramp, sigtramp_page, 2 + 2);
    415}