]> rtime.felk.cvut.cz Git - lisovros/qemu_apohw.git/blob - target-m68k/op_helper.c
exec: Change tlb_fill() argument to CPUState
[lisovros/qemu_apohw.git] / target-m68k / op_helper.c
1 /*
2  *  M68K helper routines
3  *
4  *  Copyright (c) 2007 CodeSourcery
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library 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 GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18  */
19 #include "cpu.h"
20 #include "helper.h"
21
22 #if defined(CONFIG_USER_ONLY)
23
24 void m68k_cpu_do_interrupt(CPUState *cs)
25 {
26     cs->exception_index = -1;
27 }
28
29 void do_interrupt_m68k_hardirq(CPUM68KState *env)
30 {
31 }
32
33 #else
34
35 extern int semihosting_enabled;
36
37 #include "exec/softmmu_exec.h"
38
39 #define MMUSUFFIX _mmu
40
41 #define SHIFT 0
42 #include "exec/softmmu_template.h"
43
44 #define SHIFT 1
45 #include "exec/softmmu_template.h"
46
47 #define SHIFT 2
48 #include "exec/softmmu_template.h"
49
50 #define SHIFT 3
51 #include "exec/softmmu_template.h"
52
53 /* Try to fill the TLB and return an exception if error. If retaddr is
54    NULL, it means that the function was called in C code (i.e. not
55    from generated code or from helper.c) */
56 void tlb_fill(CPUState *cs, target_ulong addr, int is_write, int mmu_idx,
57               uintptr_t retaddr)
58 {
59     int ret;
60
61     ret = m68k_cpu_handle_mmu_fault(cs, addr, is_write, mmu_idx);
62     if (unlikely(ret)) {
63         M68kCPU *cpu = M68K_CPU(cs);
64         CPUM68KState *env = &cpu->env;
65
66         if (retaddr) {
67             /* now we have a real cpu fault */
68             cpu_restore_state(env, retaddr);
69         }
70         cpu_loop_exit(env);
71     }
72 }
73
74 static void do_rte(CPUM68KState *env)
75 {
76     uint32_t sp;
77     uint32_t fmt;
78
79     sp = env->aregs[7];
80     fmt = cpu_ldl_kernel(env, sp);
81     env->pc = cpu_ldl_kernel(env, sp + 4);
82     sp |= (fmt >> 28) & 3;
83     env->sr = fmt & 0xffff;
84     m68k_switch_sp(env);
85     env->aregs[7] = sp + 8;
86 }
87
88 static void do_interrupt_all(CPUM68KState *env, int is_hw)
89 {
90     CPUState *cs = CPU(m68k_env_get_cpu(env));
91     uint32_t sp;
92     uint32_t fmt;
93     uint32_t retaddr;
94     uint32_t vector;
95
96     fmt = 0;
97     retaddr = env->pc;
98
99     if (!is_hw) {
100         switch (cs->exception_index) {
101         case EXCP_RTE:
102             /* Return from an exception.  */
103             do_rte(env);
104             return;
105         case EXCP_HALT_INSN:
106             if (semihosting_enabled
107                     && (env->sr & SR_S) != 0
108                     && (env->pc & 3) == 0
109                     && cpu_lduw_code(env, env->pc - 4) == 0x4e71
110                     && cpu_ldl_code(env, env->pc) == 0x4e7bf000) {
111                 env->pc += 4;
112                 do_m68k_semihosting(env, env->dregs[0]);
113                 return;
114             }
115             cs->halted = 1;
116             cs->exception_index = EXCP_HLT;
117             cpu_loop_exit(env);
118             return;
119         }
120         if (cs->exception_index >= EXCP_TRAP0
121             && cs->exception_index <= EXCP_TRAP15) {
122             /* Move the PC after the trap instruction.  */
123             retaddr += 2;
124         }
125     }
126
127     vector = cs->exception_index << 2;
128
129     sp = env->aregs[7];
130
131     fmt |= 0x40000000;
132     fmt |= (sp & 3) << 28;
133     fmt |= vector << 16;
134     fmt |= env->sr;
135
136     env->sr |= SR_S;
137     if (is_hw) {
138         env->sr = (env->sr & ~SR_I) | (env->pending_level << SR_I_SHIFT);
139         env->sr &= ~SR_M;
140     }
141     m68k_switch_sp(env);
142
143     /* ??? This could cause MMU faults.  */
144     sp &= ~3;
145     sp -= 4;
146     cpu_stl_kernel(env, sp, retaddr);
147     sp -= 4;
148     cpu_stl_kernel(env, sp, fmt);
149     env->aregs[7] = sp;
150     /* Jump to vector.  */
151     env->pc = cpu_ldl_kernel(env, env->vbr + vector);
152 }
153
154 void m68k_cpu_do_interrupt(CPUState *cs)
155 {
156     M68kCPU *cpu = M68K_CPU(cs);
157     CPUM68KState *env = &cpu->env;
158
159     do_interrupt_all(env, 0);
160 }
161
162 void do_interrupt_m68k_hardirq(CPUM68KState *env)
163 {
164     do_interrupt_all(env, 1);
165 }
166 #endif
167
168 static void raise_exception(CPUM68KState *env, int tt)
169 {
170     CPUState *cs = CPU(m68k_env_get_cpu(env));
171
172     cs->exception_index = tt;
173     cpu_loop_exit(env);
174 }
175
176 void HELPER(raise_exception)(CPUM68KState *env, uint32_t tt)
177 {
178     raise_exception(env, tt);
179 }
180
181 void HELPER(divu)(CPUM68KState *env, uint32_t word)
182 {
183     uint32_t num;
184     uint32_t den;
185     uint32_t quot;
186     uint32_t rem;
187     uint32_t flags;
188
189     num = env->div1;
190     den = env->div2;
191     /* ??? This needs to make sure the throwing location is accurate.  */
192     if (den == 0) {
193         raise_exception(env, EXCP_DIV0);
194     }
195     quot = num / den;
196     rem = num % den;
197     flags = 0;
198     if (word && quot > 0xffff)
199         flags |= CCF_V;
200     if (quot == 0)
201         flags |= CCF_Z;
202     else if ((int32_t)quot < 0)
203         flags |= CCF_N;
204     env->div1 = quot;
205     env->div2 = rem;
206     env->cc_dest = flags;
207 }
208
209 void HELPER(divs)(CPUM68KState *env, uint32_t word)
210 {
211     int32_t num;
212     int32_t den;
213     int32_t quot;
214     int32_t rem;
215     int32_t flags;
216
217     num = env->div1;
218     den = env->div2;
219     if (den == 0) {
220         raise_exception(env, EXCP_DIV0);
221     }
222     quot = num / den;
223     rem = num % den;
224     flags = 0;
225     if (word && quot != (int16_t)quot)
226         flags |= CCF_V;
227     if (quot == 0)
228         flags |= CCF_Z;
229     else if (quot < 0)
230         flags |= CCF_N;
231     env->div1 = quot;
232     env->div2 = rem;
233     env->cc_dest = flags;
234 }