helper.c 4.3 KB
Newer Older
A
Anthony Green 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 *  Moxie helper routines.
 *
 *  Copyright (c) 2008, 2009, 2010, 2013 Anthony Green
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <string.h>
#include <assert.h>

#include "config.h"
#include "cpu.h"
#include "mmu.h"
#include "exec/exec-all.h"
28
#include "exec/softmmu_exec.h"
A
Anthony Green 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#include "qemu/host-utils.h"
#include "helper.h"

#define MMUSUFFIX _mmu

#define SHIFT 0
#include "exec/softmmu_template.h"

#define SHIFT 1
#include "exec/softmmu_template.h"

#define SHIFT 2
#include "exec/softmmu_template.h"

#define SHIFT 3
#include "exec/softmmu_template.h"

/* Try to fill the TLB and return an exception if error. If retaddr is
   NULL, it means that the function was called in C code (i.e. not
   from generated code or from helper.c) */
49
void tlb_fill(CPUState *cs, target_ulong addr, int is_write, int mmu_idx,
A
Anthony Green 已提交
50 51 52 53
              uintptr_t retaddr)
{
    int ret;

54
    ret = moxie_cpu_handle_mmu_fault(cs, addr, is_write, mmu_idx);
A
Anthony Green 已提交
55 56
    if (unlikely(ret)) {
        if (retaddr) {
57
            cpu_restore_state(cs, retaddr);
A
Anthony Green 已提交
58 59
        }
    }
60
    cpu_loop_exit(cs);
A
Anthony Green 已提交
61 62 63 64
}

void helper_raise_exception(CPUMoxieState *env, int ex)
{
65 66 67
    CPUState *cs = CPU(moxie_env_get_cpu(env));

    cs->exception_index = ex;
A
Anthony Green 已提交
68 69 70
    /* Stash the exception type.  */
    env->sregs[2] = ex;
    /* Stash the address where the exception occurred.  */
71
    cpu_restore_state(cs, GETPC());
A
Anthony Green 已提交
72 73 74
    env->sregs[5] = env->pc;
    /* Jump the the exception handline routine.  */
    env->pc = env->sregs[1];
75
    cpu_loop_exit(cs);
A
Anthony Green 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
}

uint32_t helper_div(CPUMoxieState *env, uint32_t a, uint32_t b)
{
    if (unlikely(b == 0)) {
        helper_raise_exception(env, MOXIE_EX_DIV0);
        return 0;
    }
    if (unlikely(a == INT_MIN && b == -1)) {
        return INT_MIN;
    }

    return (int32_t)a / (int32_t)b;
}

uint32_t helper_udiv(CPUMoxieState *env, uint32_t a, uint32_t b)
{
    if (unlikely(b == 0)) {
        helper_raise_exception(env, MOXIE_EX_DIV0);
        return 0;
    }
    return a / b;
}

void helper_debug(CPUMoxieState *env)
{
102 103 104
    CPUState *cs = CPU(moxie_env_get_cpu(env));

    cs->exception_index = EXCP_DEBUG;
105
    cpu_loop_exit(cs);
A
Anthony Green 已提交
106 107 108 109
}

#if defined(CONFIG_USER_ONLY)

110
void moxie_cpu_do_interrupt(CPUState *cs)
A
Anthony Green 已提交
111
{
112 113 114
    CPUState *cs = CPU(moxie_env_get_cpu(env));

    cs->exception_index = -1;
A
Anthony Green 已提交
115 116
}

117
int moxie_cpu_handle_mmu_fault(CPUState *cs, vaddr address,
A
Anthony Green 已提交
118 119
                               int rw, int mmu_idx)
{
120
    MoxieCPU *cpu = MOXIE_CPU(cs);
121

122
    cs->exception_index = 0xaa;
123 124
    cpu->env.debug1 = address;
    cpu_dump_state(cs, stderr, fprintf, 0);
A
Anthony Green 已提交
125 126 127 128 129
    return 1;
}

#else /* !CONFIG_USER_ONLY */

130
int moxie_cpu_handle_mmu_fault(CPUState *cs, vaddr address,
A
Anthony Green 已提交
131 132
                               int rw, int mmu_idx)
{
133 134
    MoxieCPU *cpu = MOXIE_CPU(cs);
    CPUMoxieState *env = &cpu->env;
A
Anthony Green 已提交
135 136 137 138 139 140 141 142 143 144 145
    MoxieMMUResult res;
    int prot, miss;
    target_ulong phy;
    int r = 1;

    address &= TARGET_PAGE_MASK;
    prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
    miss = moxie_mmu_translate(&res, env, address, rw, mmu_idx);
    if (miss) {
        /* handle the miss.  */
        phy = 0;
146
        cs->exception_index = MOXIE_EX_MMU_MISS;
A
Anthony Green 已提交
147 148 149 150 151 152 153 154 155
    } else {
        phy = res.phy;
        r = 0;
    }
    tlb_set_page(env, address, phy, prot, mmu_idx, TARGET_PAGE_SIZE);
    return r;
}


156
void moxie_cpu_do_interrupt(CPUState *cs)
A
Anthony Green 已提交
157
{
158
    switch (cs->exception_index) {
A
Anthony Green 已提交
159 160 161 162 163 164 165
    case MOXIE_EX_BREAK:
        break;
    default:
        break;
    }
}

166
hwaddr moxie_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
A
Anthony Green 已提交
167
{
168
    MoxieCPU *cpu = MOXIE_CPU(cs);
A
Anthony Green 已提交
169 170 171
    uint32_t phy = addr;
    MoxieMMUResult res;
    int miss;
172 173

    miss = moxie_mmu_translate(&res, &cpu->env, addr, 0, 0);
A
Anthony Green 已提交
174 175 176 177 178 179
    if (!miss) {
        phy = res.phy;
    }
    return phy;
}
#endif