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

cpu_loop.c (5267B)


      1/*
      2 *  qemu user cpu loop
      3 *
      4 *  Copyright (c) 2003-2008 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
     20#include "qemu/osdep.h"
     21#include "qemu.h"
     22#include "user-internals.h"
     23#include "cpu_loop-common.h"
     24#include "signal-common.h"
     25
     26void cpu_loop(CPUNios2State *env)
     27{
     28    CPUState *cs = env_cpu(env);
     29    Nios2CPU *cpu = NIOS2_CPU(cs);
     30    target_siginfo_t info;
     31    int trapnr, ret;
     32
     33    for (;;) {
     34        cpu_exec_start(cs);
     35        trapnr = cpu_exec(cs);
     36        cpu_exec_end(cs);
     37
     38        switch (trapnr) {
     39        case EXCP_INTERRUPT:
     40            /* just indicate that signals should be handled asap */
     41            break;
     42        case EXCP_TRAP:
     43            if (env->regs[R_AT] == 0) {
     44                abi_long ret;
     45                qemu_log_mask(CPU_LOG_INT, "\nSyscall\n");
     46
     47                ret = do_syscall(env, env->regs[2],
     48                                 env->regs[4], env->regs[5], env->regs[6],
     49                                 env->regs[7], env->regs[8], env->regs[9],
     50                                 0, 0);
     51
     52                if (env->regs[2] == 0) {    /* FIXME: syscall 0 workaround */
     53                    ret = 0;
     54                }
     55
     56                env->regs[2] = abs(ret);
     57                /* Return value is 0..4096 */
     58                env->regs[7] = (ret > 0xfffffffffffff000ULL);
     59                env->regs[CR_ESTATUS] = env->regs[CR_STATUS];
     60                env->regs[CR_STATUS] &= ~0x3;
     61                env->regs[R_EA] = env->regs[R_PC] + 4;
     62                env->regs[R_PC] += 4;
     63                break;
     64            } else {
     65                qemu_log_mask(CPU_LOG_INT, "\nTrap\n");
     66
     67                env->regs[CR_ESTATUS] = env->regs[CR_STATUS];
     68                env->regs[CR_STATUS] &= ~0x3;
     69                env->regs[R_EA] = env->regs[R_PC] + 4;
     70                env->regs[R_PC] = cpu->exception_addr;
     71
     72                info.si_signo = TARGET_SIGTRAP;
     73                info.si_errno = 0;
     74                info.si_code = TARGET_TRAP_BRKPT;
     75                queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
     76                break;
     77            }
     78        case EXCP_DEBUG:
     79            info.si_signo = TARGET_SIGTRAP;
     80            info.si_errno = 0;
     81            info.si_code = TARGET_TRAP_BRKPT;
     82            queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
     83            break;
     84        case 0xaa:
     85            switch (env->regs[R_PC]) {
     86            /*case 0x1000:*/  /* TODO:__kuser_helper_version */
     87            case 0x1004:      /* __kuser_cmpxchg */
     88                start_exclusive();
     89                if (env->regs[4] & 0x3) {
     90                    goto kuser_fail;
     91                }
     92                ret = get_user_u32(env->regs[2], env->regs[4]);
     93                if (ret) {
     94                    end_exclusive();
     95                    goto kuser_fail;
     96                }
     97                env->regs[2] -= env->regs[5];
     98                if (env->regs[2] == 0) {
     99                    put_user_u32(env->regs[6], env->regs[4]);
    100                }
    101                end_exclusive();
    102                env->regs[R_PC] = env->regs[R_RA];
    103                break;
    104            /*case 0x1040:*/  /* TODO:__kuser_sigtramp */
    105            default:
    106                ;
    107kuser_fail:
    108                info.si_signo = TARGET_SIGSEGV;
    109                info.si_errno = 0;
    110                /* TODO: check env->error_code */
    111                info.si_code = TARGET_SEGV_MAPERR;
    112                info._sifields._sigfault._addr = env->regs[R_PC];
    113                queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
    114            }
    115            break;
    116        default:
    117            EXCP_DUMP(env, "\nqemu: unhandled CPU exception %#x - aborting\n",
    118                     trapnr);
    119            abort();
    120        }
    121
    122        process_pending_signals(env);
    123    }
    124}
    125
    126void target_cpu_copy_regs(CPUArchState *env, struct target_pt_regs *regs)
    127{
    128    env->regs[0] = 0;
    129    env->regs[1] = regs->r1;
    130    env->regs[2] = regs->r2;
    131    env->regs[3] = regs->r3;
    132    env->regs[4] = regs->r4;
    133    env->regs[5] = regs->r5;
    134    env->regs[6] = regs->r6;
    135    env->regs[7] = regs->r7;
    136    env->regs[8] = regs->r8;
    137    env->regs[9] = regs->r9;
    138    env->regs[10] = regs->r10;
    139    env->regs[11] = regs->r11;
    140    env->regs[12] = regs->r12;
    141    env->regs[13] = regs->r13;
    142    env->regs[14] = regs->r14;
    143    env->regs[15] = regs->r15;
    144    /* TODO: unsigned long  orig_r2; */
    145    env->regs[R_RA] = regs->ra;
    146    env->regs[R_FP] = regs->fp;
    147    env->regs[R_SP] = regs->sp;
    148    env->regs[R_GP] = regs->gp;
    149    env->regs[CR_ESTATUS] = regs->estatus;
    150    env->regs[R_EA] = regs->ea;
    151    /* TODO: unsigned long  orig_r7; */
    152
    153    /* Emulate eret when starting thread. */
    154    env->regs[R_PC] = regs->ea;
    155}