Statistics
| Branch: | Revision:

root / target-m68k / op_helper.c @ c48c6522

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 20503968 Blue Swirl
              uintptr_t retaddr)
60 0633879f pbrook
{
61 0633879f pbrook
    TranslationBlock *tb;
62 2b3e3cfe Andreas Färber
    CPUM68KState *saved_env;
63 0633879f pbrook
    int ret;
64 0633879f pbrook
65 0633879f pbrook
    saved_env = env;
66 bccd9ec5 Blue Swirl
    env = env1;
67 97b348e7 Blue Swirl
    ret = cpu_m68k_handle_mmu_fault(env, addr, is_write, mmu_idx);
68 551bd27f ths
    if (unlikely(ret)) {
69 0633879f pbrook
        if (retaddr) {
70 0633879f pbrook
            /* now we have a real cpu fault */
71 20503968 Blue Swirl
            tb = tb_find_pc(retaddr);
72 0633879f pbrook
            if (tb) {
73 0633879f pbrook
                /* the PC is inside the translated code. It means that we have
74 0633879f pbrook
                   a virtual CPU fault */
75 20503968 Blue Swirl
                cpu_restore_state(tb, env, retaddr);
76 0633879f pbrook
            }
77 0633879f pbrook
        }
78 1162c041 Blue Swirl
        cpu_loop_exit(env);
79 0633879f pbrook
    }
80 0633879f pbrook
    env = saved_env;
81 0633879f pbrook
}
82 0633879f pbrook
83 0633879f pbrook
static void do_rte(void)
84 0633879f pbrook
{
85 0633879f pbrook
    uint32_t sp;
86 0633879f pbrook
    uint32_t fmt;
87 0633879f pbrook
88 0633879f pbrook
    sp = env->aregs[7];
89 0633879f pbrook
    fmt = ldl_kernel(sp);
90 0633879f pbrook
    env->pc = ldl_kernel(sp + 4);
91 0633879f pbrook
    sp |= (fmt >> 28) & 3;
92 0633879f pbrook
    env->sr = fmt & 0xffff;
93 20dcee94 pbrook
    m68k_switch_sp(env);
94 0633879f pbrook
    env->aregs[7] = sp + 8;
95 0633879f pbrook
}
96 0633879f pbrook
97 3c688828 Blue Swirl
static void do_interrupt_all(int is_hw)
98 0633879f pbrook
{
99 0633879f pbrook
    uint32_t sp;
100 0633879f pbrook
    uint32_t fmt;
101 0633879f pbrook
    uint32_t retaddr;
102 0633879f pbrook
    uint32_t vector;
103 0633879f pbrook
104 0633879f pbrook
    fmt = 0;
105 0633879f pbrook
    retaddr = env->pc;
106 0633879f pbrook
107 0633879f pbrook
    if (!is_hw) {
108 0633879f pbrook
        switch (env->exception_index) {
109 0633879f pbrook
        case EXCP_RTE:
110 0633879f pbrook
            /* Return from an exception.  */
111 0633879f pbrook
            do_rte();
112 0633879f pbrook
            return;
113 a87295e8 pbrook
        case EXCP_HALT_INSN:
114 a87295e8 pbrook
            if (semihosting_enabled
115 a87295e8 pbrook
                    && (env->sr & SR_S) != 0
116 a87295e8 pbrook
                    && (env->pc & 3) == 0
117 a87295e8 pbrook
                    && lduw_code(env->pc - 4) == 0x4e71
118 a87295e8 pbrook
                    && ldl_code(env->pc) == 0x4e7bf000) {
119 a87295e8 pbrook
                env->pc += 4;
120 a87295e8 pbrook
                do_m68k_semihosting(env, env->dregs[0]);
121 a87295e8 pbrook
                return;
122 a87295e8 pbrook
            }
123 a87295e8 pbrook
            env->halted = 1;
124 a87295e8 pbrook
            env->exception_index = EXCP_HLT;
125 1162c041 Blue Swirl
            cpu_loop_exit(env);
126 a87295e8 pbrook
            return;
127 0633879f pbrook
        }
128 0633879f pbrook
        if (env->exception_index >= EXCP_TRAP0
129 0633879f pbrook
            && env->exception_index <= EXCP_TRAP15) {
130 0633879f pbrook
            /* Move the PC after the trap instruction.  */
131 0633879f pbrook
            retaddr += 2;
132 0633879f pbrook
        }
133 0633879f pbrook
    }
134 0633879f pbrook
135 0633879f pbrook
    vector = env->exception_index << 2;
136 0633879f pbrook
137 0cf5c677 pbrook
    sp = env->aregs[7];
138 0cf5c677 pbrook
139 0633879f pbrook
    fmt |= 0x40000000;
140 0633879f pbrook
    fmt |= (sp & 3) << 28;
141 0633879f pbrook
    fmt |= vector << 16;
142 0633879f pbrook
    fmt |= env->sr;
143 0633879f pbrook
144 20dcee94 pbrook
    env->sr |= SR_S;
145 20dcee94 pbrook
    if (is_hw) {
146 20dcee94 pbrook
        env->sr = (env->sr & ~SR_I) | (env->pending_level << SR_I_SHIFT);
147 20dcee94 pbrook
        env->sr &= ~SR_M;
148 20dcee94 pbrook
    }
149 20dcee94 pbrook
    m68k_switch_sp(env);
150 20dcee94 pbrook
151 0633879f pbrook
    /* ??? This could cause MMU faults.  */
152 0633879f pbrook
    sp &= ~3;
153 0633879f pbrook
    sp -= 4;
154 0633879f pbrook
    stl_kernel(sp, retaddr);
155 0633879f pbrook
    sp -= 4;
156 0633879f pbrook
    stl_kernel(sp, fmt);
157 0633879f pbrook
    env->aregs[7] = sp;
158 0633879f pbrook
    /* Jump to vector.  */
159 0633879f pbrook
    env->pc = ldl_kernel(env->vbr + vector);
160 0633879f pbrook
}
161 0633879f pbrook
162 2b3e3cfe Andreas Färber
void do_interrupt(CPUM68KState *env1)
163 3c688828 Blue Swirl
{
164 2b3e3cfe Andreas Färber
    CPUM68KState *saved_env;
165 3c688828 Blue Swirl
166 3c688828 Blue Swirl
    saved_env = env;
167 3c688828 Blue Swirl
    env = env1;
168 3c688828 Blue Swirl
    do_interrupt_all(0);
169 3c688828 Blue Swirl
    env = saved_env;
170 3c688828 Blue Swirl
}
171 3c688828 Blue Swirl
172 2b3e3cfe Andreas Färber
void do_interrupt_m68k_hardirq(CPUM68KState *env1)
173 3c688828 Blue Swirl
{
174 2b3e3cfe Andreas Färber
    CPUM68KState *saved_env;
175 3c688828 Blue Swirl
176 3c688828 Blue Swirl
    saved_env = env;
177 3c688828 Blue Swirl
    env = env1;
178 3c688828 Blue Swirl
    do_interrupt_all(1);
179 3c688828 Blue Swirl
    env = saved_env;
180 3c688828 Blue Swirl
}
181 0633879f pbrook
#endif
182 e1f3808e pbrook
183 e1f3808e pbrook
static void raise_exception(int tt)
184 e1f3808e pbrook
{
185 e1f3808e pbrook
    env->exception_index = tt;
186 1162c041 Blue Swirl
    cpu_loop_exit(env);
187 e1f3808e pbrook
}
188 e1f3808e pbrook
189 e1f3808e pbrook
void HELPER(raise_exception)(uint32_t tt)
190 e1f3808e pbrook
{
191 e1f3808e pbrook
    raise_exception(tt);
192 e1f3808e pbrook
}
193 e1f3808e pbrook
194 2b3e3cfe Andreas Färber
void HELPER(divu)(CPUM68KState *env, uint32_t word)
195 e1f3808e pbrook
{
196 e1f3808e pbrook
    uint32_t num;
197 e1f3808e pbrook
    uint32_t den;
198 e1f3808e pbrook
    uint32_t quot;
199 e1f3808e pbrook
    uint32_t rem;
200 e1f3808e pbrook
    uint32_t flags;
201 e1f3808e pbrook
202 e1f3808e pbrook
    num = env->div1;
203 e1f3808e pbrook
    den = env->div2;
204 e1f3808e pbrook
    /* ??? This needs to make sure the throwing location is accurate.  */
205 e1f3808e pbrook
    if (den == 0)
206 e1f3808e pbrook
        raise_exception(EXCP_DIV0);
207 e1f3808e pbrook
    quot = num / den;
208 e1f3808e pbrook
    rem = num % den;
209 e1f3808e pbrook
    flags = 0;
210 e1f3808e pbrook
    /* Avoid using a PARAM1 of zero.  This breaks dyngen because it uses
211 e1f3808e pbrook
       the address of a symbol, and gcc knows symbols can't have address
212 e1f3808e pbrook
       zero.  */
213 e1f3808e pbrook
    if (word && quot > 0xffff)
214 e1f3808e pbrook
        flags |= CCF_V;
215 e1f3808e pbrook
    if (quot == 0)
216 e1f3808e pbrook
        flags |= CCF_Z;
217 e1f3808e pbrook
    else if ((int32_t)quot < 0)
218 e1f3808e pbrook
        flags |= CCF_N;
219 e1f3808e pbrook
    env->div1 = quot;
220 e1f3808e pbrook
    env->div2 = rem;
221 e1f3808e pbrook
    env->cc_dest = flags;
222 e1f3808e pbrook
}
223 e1f3808e pbrook
224 2b3e3cfe Andreas Färber
void HELPER(divs)(CPUM68KState *env, uint32_t word)
225 e1f3808e pbrook
{
226 e1f3808e pbrook
    int32_t num;
227 e1f3808e pbrook
    int32_t den;
228 e1f3808e pbrook
    int32_t quot;
229 e1f3808e pbrook
    int32_t rem;
230 e1f3808e pbrook
    int32_t flags;
231 e1f3808e pbrook
232 e1f3808e pbrook
    num = env->div1;
233 e1f3808e pbrook
    den = env->div2;
234 e1f3808e pbrook
    if (den == 0)
235 e1f3808e pbrook
        raise_exception(EXCP_DIV0);
236 e1f3808e pbrook
    quot = num / den;
237 e1f3808e pbrook
    rem = num % den;
238 e1f3808e pbrook
    flags = 0;
239 e1f3808e pbrook
    if (word && quot != (int16_t)quot)
240 e1f3808e pbrook
        flags |= CCF_V;
241 e1f3808e pbrook
    if (quot == 0)
242 e1f3808e pbrook
        flags |= CCF_Z;
243 e1f3808e pbrook
    else if (quot < 0)
244 e1f3808e pbrook
        flags |= CCF_N;
245 e1f3808e pbrook
    env->div1 = quot;
246 e1f3808e pbrook
    env->div2 = rem;
247 e1f3808e pbrook
    env->cc_dest = flags;
248 e1f3808e pbrook
}