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 (7230B)


      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 MCONTEXT_VERSION 2
     26
     27struct target_sigcontext {
     28    int version;
     29    unsigned long gregs[32];
     30};
     31
     32struct target_ucontext {
     33    abi_ulong tuc_flags;
     34    abi_ulong tuc_link;
     35    target_stack_t tuc_stack;
     36    struct target_sigcontext tuc_mcontext;
     37    target_sigset_t tuc_sigmask;   /* mask last for extensibility */
     38};
     39
     40struct target_rt_sigframe {
     41    struct target_siginfo info;
     42    struct target_ucontext uc;
     43};
     44
     45static int rt_setup_ucontext(struct target_ucontext *uc, CPUNios2State *env)
     46{
     47    unsigned long *gregs = uc->tuc_mcontext.gregs;
     48
     49    __put_user(MCONTEXT_VERSION, &uc->tuc_mcontext.version);
     50    __put_user(env->regs[1], &gregs[0]);
     51    __put_user(env->regs[2], &gregs[1]);
     52    __put_user(env->regs[3], &gregs[2]);
     53    __put_user(env->regs[4], &gregs[3]);
     54    __put_user(env->regs[5], &gregs[4]);
     55    __put_user(env->regs[6], &gregs[5]);
     56    __put_user(env->regs[7], &gregs[6]);
     57    __put_user(env->regs[8], &gregs[7]);
     58    __put_user(env->regs[9], &gregs[8]);
     59    __put_user(env->regs[10], &gregs[9]);
     60    __put_user(env->regs[11], &gregs[10]);
     61    __put_user(env->regs[12], &gregs[11]);
     62    __put_user(env->regs[13], &gregs[12]);
     63    __put_user(env->regs[14], &gregs[13]);
     64    __put_user(env->regs[15], &gregs[14]);
     65    __put_user(env->regs[16], &gregs[15]);
     66    __put_user(env->regs[17], &gregs[16]);
     67    __put_user(env->regs[18], &gregs[17]);
     68    __put_user(env->regs[19], &gregs[18]);
     69    __put_user(env->regs[20], &gregs[19]);
     70    __put_user(env->regs[21], &gregs[20]);
     71    __put_user(env->regs[22], &gregs[21]);
     72    __put_user(env->regs[23], &gregs[22]);
     73    __put_user(env->regs[R_RA], &gregs[23]);
     74    __put_user(env->regs[R_FP], &gregs[24]);
     75    __put_user(env->regs[R_GP], &gregs[25]);
     76    __put_user(env->regs[R_EA], &gregs[27]);
     77    __put_user(env->regs[R_SP], &gregs[28]);
     78
     79    return 0;
     80}
     81
     82static int rt_restore_ucontext(CPUNios2State *env, struct target_ucontext *uc,
     83                               int *pr2)
     84{
     85    int temp;
     86    unsigned long *gregs = uc->tuc_mcontext.gregs;
     87
     88    /* Always make any pending restarted system calls return -EINTR */
     89    /* current->restart_block.fn = do_no_restart_syscall; */
     90
     91    __get_user(temp, &uc->tuc_mcontext.version);
     92    if (temp != MCONTEXT_VERSION) {
     93        return 1;
     94    }
     95
     96    /* restore passed registers */
     97    __get_user(env->regs[1], &gregs[0]);
     98    __get_user(env->regs[2], &gregs[1]);
     99    __get_user(env->regs[3], &gregs[2]);
    100    __get_user(env->regs[4], &gregs[3]);
    101    __get_user(env->regs[5], &gregs[4]);
    102    __get_user(env->regs[6], &gregs[5]);
    103    __get_user(env->regs[7], &gregs[6]);
    104    __get_user(env->regs[8], &gregs[7]);
    105    __get_user(env->regs[9], &gregs[8]);
    106    __get_user(env->regs[10], &gregs[9]);
    107    __get_user(env->regs[11], &gregs[10]);
    108    __get_user(env->regs[12], &gregs[11]);
    109    __get_user(env->regs[13], &gregs[12]);
    110    __get_user(env->regs[14], &gregs[13]);
    111    __get_user(env->regs[15], &gregs[14]);
    112    __get_user(env->regs[16], &gregs[15]);
    113    __get_user(env->regs[17], &gregs[16]);
    114    __get_user(env->regs[18], &gregs[17]);
    115    __get_user(env->regs[19], &gregs[18]);
    116    __get_user(env->regs[20], &gregs[19]);
    117    __get_user(env->regs[21], &gregs[20]);
    118    __get_user(env->regs[22], &gregs[21]);
    119    __get_user(env->regs[23], &gregs[22]);
    120    /* gregs[23] is handled below */
    121    /* Verify, should this be settable */
    122    __get_user(env->regs[R_FP], &gregs[24]);
    123    /* Verify, should this be settable */
    124    __get_user(env->regs[R_GP], &gregs[25]);
    125    /* Not really necessary no user settable bits */
    126    __get_user(temp, &gregs[26]);
    127    __get_user(env->regs[R_EA], &gregs[27]);
    128
    129    __get_user(env->regs[R_RA], &gregs[23]);
    130    __get_user(env->regs[R_SP], &gregs[28]);
    131
    132    target_restore_altstack(&uc->tuc_stack, env);
    133
    134    *pr2 = env->regs[2];
    135    return 0;
    136}
    137
    138static void *get_sigframe(struct target_sigaction *ka, CPUNios2State *env,
    139                          size_t frame_size)
    140{
    141    unsigned long usp;
    142
    143    /* This is the X/Open sanctioned signal stack switching.  */
    144    usp = target_sigsp(get_sp_from_cpustate(env), ka);
    145
    146    /* Verify, is it 32 or 64 bit aligned */
    147    return (void *)((usp - frame_size) & -8UL);
    148}
    149
    150void setup_rt_frame(int sig, struct target_sigaction *ka,
    151                    target_siginfo_t *info,
    152                    target_sigset_t *set,
    153                    CPUNios2State *env)
    154{
    155    struct target_rt_sigframe *frame;
    156    int i, err = 0;
    157
    158    frame = get_sigframe(ka, env, sizeof(*frame));
    159
    160    if (ka->sa_flags & SA_SIGINFO) {
    161        tswap_siginfo(&frame->info, info);
    162    }
    163
    164    /* Create the ucontext.  */
    165    __put_user(0, &frame->uc.tuc_flags);
    166    __put_user(0, &frame->uc.tuc_link);
    167    target_save_altstack(&frame->uc.tuc_stack, env);
    168    err |= rt_setup_ucontext(&frame->uc, env);
    169    for (i = 0; i < TARGET_NSIG_WORDS; i++) {
    170        __put_user((abi_ulong)set->sig[i],
    171            (abi_ulong *)&frame->uc.tuc_sigmask.sig[i]);
    172    }
    173
    174    if (err) {
    175        goto give_sigsegv;
    176    }
    177
    178    /* Set up to return from userspace; jump to fixed address sigreturn
    179       trampoline on kuser page.  */
    180    env->regs[R_RA] = (unsigned long) (0x1044);
    181
    182    /* Set up registers for signal handler */
    183    env->regs[R_SP] = (unsigned long) frame;
    184    env->regs[4] = (unsigned long) sig;
    185    env->regs[5] = (unsigned long) &frame->info;
    186    env->regs[6] = (unsigned long) &frame->uc;
    187    env->regs[R_EA] = (unsigned long) ka->_sa_handler;
    188    return;
    189
    190give_sigsegv:
    191    if (sig == TARGET_SIGSEGV) {
    192        ka->_sa_handler = TARGET_SIG_DFL;
    193    }
    194    force_sigsegv(sig);
    195    return;
    196}
    197
    198long do_sigreturn(CPUNios2State *env)
    199{
    200    trace_user_do_sigreturn(env, 0);
    201    qemu_log_mask(LOG_UNIMP, "do_sigreturn: not implemented\n");
    202    return -TARGET_ENOSYS;
    203}
    204
    205long do_rt_sigreturn(CPUNios2State *env)
    206{
    207    /* Verify, can we follow the stack back */
    208    abi_ulong frame_addr = env->regs[R_SP];
    209    struct target_rt_sigframe *frame;
    210    sigset_t set;
    211    int rval;
    212
    213    if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
    214        goto badframe;
    215    }
    216
    217    target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
    218    do_sigprocmask(SIG_SETMASK, &set, NULL);
    219
    220    if (rt_restore_ucontext(env, &frame->uc, &rval)) {
    221        goto badframe;
    222    }
    223
    224    unlock_user_struct(frame, frame_addr, 0);
    225    return rval;
    226
    227badframe:
    228    unlock_user_struct(frame, frame_addr, 0);
    229    force_sig(TARGET_SIGSEGV);
    230    return 0;
    231}