qemu/linux-user/alpha/cpu_loop.c
<<
>>
Prefs
   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-common.h"
  22#include "qemu.h"
  23#include "user-internals.h"
  24#include "cpu_loop-common.h"
  25#include "signal-common.h"
  26
  27void cpu_loop(CPUAlphaState *env)
  28{
  29    CPUState *cs = env_cpu(env);
  30    int trapnr, si_code;
  31    abi_long sysret;
  32
  33    while (1) {
  34        bool arch_interrupt = true;
  35
  36        cpu_exec_start(cs);
  37        trapnr = cpu_exec(cs);
  38        cpu_exec_end(cs);
  39        process_queued_cpu_work(cs);
  40
  41        switch (trapnr) {
  42        case EXCP_RESET:
  43            fprintf(stderr, "Reset requested. Exit\n");
  44            exit(EXIT_FAILURE);
  45            break;
  46        case EXCP_MCHK:
  47            fprintf(stderr, "Machine check exception. Exit\n");
  48            exit(EXIT_FAILURE);
  49            break;
  50        case EXCP_SMP_INTERRUPT:
  51        case EXCP_CLK_INTERRUPT:
  52        case EXCP_DEV_INTERRUPT:
  53            fprintf(stderr, "External interrupt. Exit\n");
  54            exit(EXIT_FAILURE);
  55            break;
  56        case EXCP_OPCDEC:
  57        do_sigill:
  58            force_sig_fault(TARGET_SIGILL, TARGET_ILL_ILLOPC, env->pc);
  59            break;
  60        case EXCP_ARITH:
  61            force_sig_fault(TARGET_SIGFPE, TARGET_FPE_FLTINV, env->pc);
  62            break;
  63        case EXCP_FEN:
  64            /* No-op.  Linux simply re-enables the FPU.  */
  65            break;
  66        case EXCP_CALL_PAL:
  67            switch (env->error_code) {
  68            case 0x80:
  69                /* BPT */
  70                goto do_sigtrap_brkpt;
  71            case 0x81:
  72                /* BUGCHK */
  73                goto do_sigtrap_unk;
  74            case 0x83:
  75                /* CALLSYS */
  76                trapnr = env->ir[IR_V0];
  77                sysret = do_syscall(env, trapnr,
  78                                    env->ir[IR_A0], env->ir[IR_A1],
  79                                    env->ir[IR_A2], env->ir[IR_A3],
  80                                    env->ir[IR_A4], env->ir[IR_A5],
  81                                    0, 0);
  82                if (sysret == -QEMU_ERESTARTSYS) {
  83                    env->pc -= 4;
  84                    break;
  85                }
  86                if (sysret == -QEMU_ESIGRETURN) {
  87                    break;
  88                }
  89                /* Syscall writes 0 to V0 to bypass error check, similar
  90                   to how this is handled internal to Linux kernel.
  91                   (Ab)use trapnr temporarily as boolean indicating error.  */
  92                trapnr = (env->ir[IR_V0] != 0 && sysret < 0);
  93                env->ir[IR_V0] = (trapnr ? -sysret : sysret);
  94                env->ir[IR_A3] = trapnr;
  95                break;
  96            case 0x86:
  97                /* IMB */
  98                /* ??? We can probably elide the code using page_unprotect
  99                   that is checking for self-modifying code.  Instead we
 100                   could simply call tb_flush here.  Until we work out the
 101                   changes required to turn off the extra write protection,
 102                   this can be a no-op.  */
 103                break;
 104            case 0x9E:
 105                /* RDUNIQUE */
 106                /* Handled in the translator for usermode.  */
 107                abort();
 108            case 0x9F:
 109                /* WRUNIQUE */
 110                /* Handled in the translator for usermode.  */
 111                abort();
 112            case 0xAA:
 113                /* GENTRAP */
 114                switch (env->ir[IR_A0]) {
 115                case TARGET_GEN_INTOVF:
 116                    si_code = TARGET_FPE_INTOVF;
 117                    break;
 118                case TARGET_GEN_INTDIV:
 119                    si_code = TARGET_FPE_INTDIV;
 120                    break;
 121                case TARGET_GEN_FLTOVF:
 122                    si_code = TARGET_FPE_FLTOVF;
 123                    break;
 124                case TARGET_GEN_FLTUND:
 125                    si_code = TARGET_FPE_FLTUND;
 126                    break;
 127                case TARGET_GEN_FLTINV:
 128                    si_code = TARGET_FPE_FLTINV;
 129                    break;
 130                case TARGET_GEN_FLTINE:
 131                    si_code = TARGET_FPE_FLTRES;
 132                    break;
 133                case TARGET_GEN_ROPRAND:
 134                    si_code = TARGET_FPE_FLTUNK;
 135                    break;
 136                default:
 137                    goto do_sigtrap_unk;
 138                }
 139                force_sig_fault(TARGET_SIGFPE, si_code, env->pc);
 140                break;
 141            default:
 142                goto do_sigill;
 143            }
 144            break;
 145        case EXCP_DEBUG:
 146        do_sigtrap_brkpt:
 147            force_sig_fault(TARGET_SIGTRAP, TARGET_TRAP_BRKPT, env->pc);
 148            break;
 149        do_sigtrap_unk:
 150            force_sig_fault(TARGET_SIGTRAP, TARGET_TRAP_UNK, env->pc);
 151            break;
 152        case EXCP_INTERRUPT:
 153            /* Just indicate that signals should be handled asap.  */
 154            break;
 155        case EXCP_ATOMIC:
 156            cpu_exec_step_atomic(cs);
 157            arch_interrupt = false;
 158            break;
 159        default:
 160            fprintf(stderr, "Unhandled trap: 0x%x\n", trapnr);
 161            cpu_dump_state(cs, stderr, 0);
 162            exit(EXIT_FAILURE);
 163        }
 164        process_pending_signals (env);
 165
 166        /* Most of the traps imply a transition through PALcode, which
 167           implies an REI instruction has been executed.  Which means
 168           that RX and LOCK_ADDR should be cleared.  But there are a
 169           few exceptions for traps internal to QEMU.  */
 170        if (arch_interrupt) {
 171            env->flags &= ~ENV_FLAG_RX_FLAG;
 172            env->lock_addr = -1;
 173        }
 174    }
 175}
 176
 177void target_cpu_copy_regs(CPUArchState *env, struct target_pt_regs *regs)
 178{
 179    int i;
 180
 181    for(i = 0; i < 28; i++) {
 182        env->ir[i] = ((abi_ulong *)regs)[i];
 183    }
 184    env->ir[IR_SP] = regs->usp;
 185    env->pc = regs->pc;
 186}
 187