Statistics
| Branch: | Revision:

root / target-m68k / op_helper.c @ 6f06f178

History | View | Annotate | Download (5.7 kB)

1 0633879f pbrook
/*
2 0633879f pbrook
 *  M68K helper routines
3 5fafdf24 ths
 *
4 0633879f pbrook
 *  Copyright (c) 2007 CodeSourcery
5 0633879f pbrook
 *
6 0633879f pbrook
 * This library is free software; you can redistribute it and/or
7 0633879f pbrook
 * modify it under the terms of the GNU Lesser General Public
8 0633879f pbrook
 * License as published by the Free Software Foundation; either
9 0633879f pbrook
 * version 2 of the License, or (at your option) any later version.
10 0633879f pbrook
 *
11 0633879f pbrook
 * This library is distributed in the hope that it will be useful,
12 0633879f pbrook
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 0633879f pbrook
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 0633879f pbrook
 * Lesser General Public License for more details.
15 0633879f pbrook
 *
16 0633879f pbrook
 * You should have received a copy of the GNU Lesser General Public
17 8167ee88 Blue Swirl
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 0633879f pbrook
 */
19 3e457172 Blue Swirl
#include "cpu.h"
20 3e457172 Blue Swirl
#include "dyngen-exec.h"
21 e1f3808e pbrook
#include "helpers.h"
22 0633879f pbrook
23 0633879f pbrook
#if defined(CONFIG_USER_ONLY)
24 0633879f pbrook
25 2b3e3cfe Andreas Färber
void do_interrupt(CPUM68KState *env1)
26 3c688828 Blue Swirl
{
27 3c688828 Blue Swirl
    env1->exception_index = -1;
28 3c688828 Blue Swirl
}
29 3c688828 Blue Swirl
30 2b3e3cfe Andreas Färber
void do_interrupt_m68k_hardirq(CPUM68KState *env1)
31 0633879f pbrook
{
32 0633879f pbrook
}
33 0633879f pbrook
34 0633879f pbrook
#else
35 0633879f pbrook
36 a87295e8 pbrook
extern int semihosting_enabled;
37 a87295e8 pbrook
38 3e457172 Blue Swirl
#include "softmmu_exec.h"
39 3e457172 Blue Swirl
40 0633879f pbrook
#define MMUSUFFIX _mmu
41 0633879f pbrook
42 0633879f pbrook
#define SHIFT 0
43 0633879f pbrook
#include "softmmu_template.h"
44 0633879f pbrook
45 0633879f pbrook
#define SHIFT 1
46 0633879f pbrook
#include "softmmu_template.h"
47 0633879f pbrook
48 0633879f pbrook
#define SHIFT 2
49 0633879f pbrook
#include "softmmu_template.h"
50 0633879f pbrook
51 0633879f pbrook
#define SHIFT 3
52 0633879f pbrook
#include "softmmu_template.h"
53 0633879f pbrook
54 0633879f pbrook
/* Try to fill the TLB and return an exception if error. If retaddr is
55 0633879f pbrook
   NULL, it means that the function was called in C code (i.e. not
56 0633879f pbrook
   from generated code or from helper.c) */
57 0633879f pbrook
/* XXX: fix it to restore all registers */
58 2b3e3cfe Andreas Färber
void tlb_fill(CPUM68KState *env1, target_ulong addr, int is_write, int mmu_idx,
59 bccd9ec5 Blue Swirl
              void *retaddr)
60 0633879f pbrook
{
61 0633879f pbrook
    TranslationBlock *tb;
62 2b3e3cfe Andreas Färber
    CPUM68KState *saved_env;
63 44f8625d bellard
    unsigned long pc;
64 0633879f pbrook
    int ret;
65 0633879f pbrook
66 0633879f pbrook
    saved_env = env;
67 bccd9ec5 Blue Swirl
    env = env1;
68 97b348e7 Blue Swirl
    ret = cpu_m68k_handle_mmu_fault(env, addr, is_write, mmu_idx);
69 551bd27f ths
    if (unlikely(ret)) {
70 0633879f pbrook
        if (retaddr) {
71 0633879f pbrook
            /* now we have a real cpu fault */
72 44f8625d bellard
            pc = (unsigned long)retaddr;
73 0633879f pbrook
            tb = tb_find_pc(pc);
74 0633879f pbrook
            if (tb) {
75 0633879f pbrook
                /* the PC is inside the translated code. It means that we have
76 0633879f pbrook
                   a virtual CPU fault */
77 618ba8e6 Stefan Weil
                cpu_restore_state(tb, env, pc);
78 0633879f pbrook
            }
79 0633879f pbrook
        }
80 1162c041 Blue Swirl
        cpu_loop_exit(env);
81 0633879f pbrook
    }
82 0633879f pbrook
    env = saved_env;
83 0633879f pbrook
}
84 0633879f pbrook
85 0633879f pbrook
static void do_rte(void)
86 0633879f pbrook
{
87 0633879f pbrook
    uint32_t sp;
88 0633879f pbrook
    uint32_t fmt;
89 0633879f pbrook
90 0633879f pbrook
    sp = env->aregs[7];
91 0633879f pbrook
    fmt = ldl_kernel(sp);
92 0633879f pbrook
    env->pc = ldl_kernel(sp + 4);
93 0633879f pbrook
    sp |= (fmt >> 28) & 3;
94 0633879f pbrook
    env->sr = fmt & 0xffff;
95 20dcee94 pbrook
    m68k_switch_sp(env);
96 0633879f pbrook
    env->aregs[7] = sp + 8;
97 0633879f pbrook
}
98 0633879f pbrook
99 3c688828 Blue Swirl
static void do_interrupt_all(int is_hw)
100 0633879f pbrook
{
101 0633879f pbrook
    uint32_t sp;
102 0633879f pbrook
    uint32_t fmt;
103 0633879f pbrook
    uint32_t retaddr;
104 0633879f pbrook
    uint32_t vector;
105 0633879f pbrook
106 0633879f pbrook
    fmt = 0;
107 0633879f pbrook
    retaddr = env->pc;
108 0633879f pbrook
109 0633879f pbrook
    if (!is_hw) {
110 0633879f pbrook
        switch (env->exception_index) {
111 0633879f pbrook
        case EXCP_RTE:
112 0633879f pbrook
            /* Return from an exception.  */
113 0633879f pbrook
            do_rte();
114 0633879f pbrook
            return;
115 a87295e8 pbrook
        case EXCP_HALT_INSN:
116 a87295e8 pbrook
            if (semihosting_enabled
117 a87295e8 pbrook
                    && (env->sr & SR_S) != 0
118 a87295e8 pbrook
                    && (env->pc & 3) == 0
119 a87295e8 pbrook
                    && lduw_code(env->pc - 4) == 0x4e71
120 a87295e8 pbrook
                    && ldl_code(env->pc) == 0x4e7bf000) {
121 a87295e8 pbrook
                env->pc += 4;
122 a87295e8 pbrook
                do_m68k_semihosting(env, env->dregs[0]);
123 a87295e8 pbrook
                return;
124 a87295e8 pbrook
            }
125 a87295e8 pbrook
            env->halted = 1;
126 a87295e8 pbrook
            env->exception_index = EXCP_HLT;
127 1162c041 Blue Swirl
            cpu_loop_exit(env);
128 a87295e8 pbrook
            return;
129 0633879f pbrook
        }
130 0633879f pbrook
        if (env->exception_index >= EXCP_TRAP0
131 0633879f pbrook
            && env->exception_index <= EXCP_TRAP15) {
132 0633879f pbrook
            /* Move the PC after the trap instruction.  */
133 0633879f pbrook
            retaddr += 2;
134 0633879f pbrook
        }
135 0633879f pbrook
    }
136 0633879f pbrook
137 0633879f pbrook
    vector = env->exception_index << 2;
138 0633879f pbrook
139 0cf5c677 pbrook
    sp = env->aregs[7];
140 0cf5c677 pbrook
141 0633879f pbrook
    fmt |= 0x40000000;
142 0633879f pbrook
    fmt |= (sp & 3) << 28;
143 0633879f pbrook
    fmt |= vector << 16;
144 0633879f pbrook
    fmt |= env->sr;
145 0633879f pbrook
146 20dcee94 pbrook
    env->sr |= SR_S;
147 20dcee94 pbrook
    if (is_hw) {
148 20dcee94 pbrook
        env->sr = (env->sr & ~SR_I) | (env->pending_level << SR_I_SHIFT);
149 20dcee94 pbrook
        env->sr &= ~SR_M;
150 20dcee94 pbrook
    }
151 20dcee94 pbrook
    m68k_switch_sp(env);
152 20dcee94 pbrook
153 0633879f pbrook
    /* ??? This could cause MMU faults.  */
154 0633879f pbrook
    sp &= ~3;
155 0633879f pbrook
    sp -= 4;
156 0633879f pbrook
    stl_kernel(sp, retaddr);
157 0633879f pbrook
    sp -= 4;
158 0633879f pbrook
    stl_kernel(sp, fmt);
159 0633879f pbrook
    env->aregs[7] = sp;
160 0633879f pbrook
    /* Jump to vector.  */
161 0633879f pbrook
    env->pc = ldl_kernel(env->vbr + vector);
162 0633879f pbrook
}
163 0633879f pbrook
164 2b3e3cfe Andreas Färber
void do_interrupt(CPUM68KState *env1)
165 3c688828 Blue Swirl
{
166 2b3e3cfe Andreas Färber
    CPUM68KState *saved_env;
167 3c688828 Blue Swirl
168 3c688828 Blue Swirl
    saved_env = env;
169 3c688828 Blue Swirl
    env = env1;
170 3c688828 Blue Swirl
    do_interrupt_all(0);
171 3c688828 Blue Swirl
    env = saved_env;
172 3c688828 Blue Swirl
}
173 3c688828 Blue Swirl
174 2b3e3cfe Andreas Färber
void do_interrupt_m68k_hardirq(CPUM68KState *env1)
175 3c688828 Blue Swirl
{
176 2b3e3cfe Andreas Färber
    CPUM68KState *saved_env;
177 3c688828 Blue Swirl
178 3c688828 Blue Swirl
    saved_env = env;
179 3c688828 Blue Swirl
    env = env1;
180 3c688828 Blue Swirl
    do_interrupt_all(1);
181 3c688828 Blue Swirl
    env = saved_env;
182 3c688828 Blue Swirl
}
183 0633879f pbrook
#endif
184 e1f3808e pbrook
185 e1f3808e pbrook
static void raise_exception(int tt)
186 e1f3808e pbrook
{
187 e1f3808e pbrook
    env->exception_index = tt;
188 1162c041 Blue Swirl
    cpu_loop_exit(env);
189 e1f3808e pbrook
}
190 e1f3808e pbrook
191 e1f3808e pbrook
void HELPER(raise_exception)(uint32_t tt)
192 e1f3808e pbrook
{
193 e1f3808e pbrook
    raise_exception(tt);
194 e1f3808e pbrook
}
195 e1f3808e pbrook
196 2b3e3cfe Andreas Färber
void HELPER(divu)(CPUM68KState *env, uint32_t word)
197 e1f3808e pbrook
{
198 e1f3808e pbrook
    uint32_t num;
199 e1f3808e pbrook
    uint32_t den;
200 e1f3808e pbrook
    uint32_t quot;
201 e1f3808e pbrook
    uint32_t rem;
202 e1f3808e pbrook
    uint32_t flags;
203 e1f3808e pbrook
204 e1f3808e pbrook
    num = env->div1;
205 e1f3808e pbrook
    den = env->div2;
206 e1f3808e pbrook
    /* ??? This needs to make sure the throwing location is accurate.  */
207 e1f3808e pbrook
    if (den == 0)
208 e1f3808e pbrook
        raise_exception(EXCP_DIV0);
209 e1f3808e pbrook
    quot = num / den;
210 e1f3808e pbrook
    rem = num % den;
211 e1f3808e pbrook
    flags = 0;
212 e1f3808e pbrook
    /* Avoid using a PARAM1 of zero.  This breaks dyngen because it uses
213 e1f3808e pbrook
       the address of a symbol, and gcc knows symbols can't have address
214 e1f3808e pbrook
       zero.  */
215 e1f3808e pbrook
    if (word && quot > 0xffff)
216 e1f3808e pbrook
        flags |= CCF_V;
217 e1f3808e pbrook
    if (quot == 0)
218 e1f3808e pbrook
        flags |= CCF_Z;
219 e1f3808e pbrook
    else if ((int32_t)quot < 0)
220 e1f3808e pbrook
        flags |= CCF_N;
221 e1f3808e pbrook
    env->div1 = quot;
222 e1f3808e pbrook
    env->div2 = rem;
223 e1f3808e pbrook
    env->cc_dest = flags;
224 e1f3808e pbrook
}
225 e1f3808e pbrook
226 2b3e3cfe Andreas Färber
void HELPER(divs)(CPUM68KState *env, uint32_t word)
227 e1f3808e pbrook
{
228 e1f3808e pbrook
    int32_t num;
229 e1f3808e pbrook
    int32_t den;
230 e1f3808e pbrook
    int32_t quot;
231 e1f3808e pbrook
    int32_t rem;
232 e1f3808e pbrook
    int32_t flags;
233 e1f3808e pbrook
234 e1f3808e pbrook
    num = env->div1;
235 e1f3808e pbrook
    den = env->div2;
236 e1f3808e pbrook
    if (den == 0)
237 e1f3808e pbrook
        raise_exception(EXCP_DIV0);
238 e1f3808e pbrook
    quot = num / den;
239 e1f3808e pbrook
    rem = num % den;
240 e1f3808e pbrook
    flags = 0;
241 e1f3808e pbrook
    if (word && quot != (int16_t)quot)
242 e1f3808e pbrook
        flags |= CCF_V;
243 e1f3808e pbrook
    if (quot == 0)
244 e1f3808e pbrook
        flags |= CCF_Z;
245 e1f3808e pbrook
    else if (quot < 0)
246 e1f3808e pbrook
        flags |= CCF_N;
247 e1f3808e pbrook
    env->div1 = quot;
248 e1f3808e pbrook
    env->div2 = rem;
249 e1f3808e pbrook
    env->cc_dest = flags;
250 e1f3808e pbrook
}