]>
Commit | Line | Data |
---|---|---|
48e06fe0 BK |
1 | /* |
2 | * Copyright (c) 2012-2014 Bastian Koppelmann C-Lab/University Paderborn | |
3 | * | |
4 | * This library is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU Lesser General Public | |
6 | * License as published by the Free Software Foundation; either | |
7 | * version 2 of the License, or (at your option) any later version. | |
8 | * | |
9 | * This library is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
12 | * Lesser General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU Lesser General Public | |
15 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. | |
16 | */ | |
17 | #include <stdlib.h> | |
18 | #include "cpu.h" | |
19 | #include "qemu/host-utils.h" | |
20 | #include "exec/helper-proto.h" | |
21 | #include "exec/cpu_ldst.h" | |
22 | ||
2692802a BK |
23 | #define SSOV(env, ret, arg, len) do { \ |
24 | int64_t max_pos = INT##len ##_MAX; \ | |
25 | int64_t max_neg = INT##len ##_MIN; \ | |
26 | if (arg > max_pos) { \ | |
27 | env->PSW_USB_V = (1 << 31); \ | |
28 | env->PSW_USB_SV = (1 << 31); \ | |
29 | ret = (target_ulong)max_pos; \ | |
30 | } else { \ | |
31 | if (arg < max_neg) { \ | |
32 | env->PSW_USB_V = (1 << 31); \ | |
33 | env->PSW_USB_SV = (1 << 31); \ | |
34 | ret = (target_ulong)max_neg; \ | |
35 | } else { \ | |
36 | env->PSW_USB_V = 0; \ | |
37 | ret = (target_ulong)arg; \ | |
38 | } \ | |
39 | } \ | |
40 | env->PSW_USB_AV = arg ^ arg * 2u; \ | |
41 | env->PSW_USB_SAV |= env->PSW_USB_AV; \ | |
42 | } while (0) | |
43 | ||
44 | target_ulong helper_add_ssov(CPUTriCoreState *env, target_ulong r1, | |
45 | target_ulong r2) | |
46 | { | |
47 | target_ulong ret; | |
48 | int64_t t1 = sextract64(r1, 0, 32); | |
49 | int64_t t2 = sextract64(r2, 0, 32); | |
50 | int64_t result = t1 + t2; | |
51 | SSOV(env, ret, result, 32); | |
52 | return ret; | |
53 | } | |
54 | ||
55 | target_ulong helper_sub_ssov(CPUTriCoreState *env, target_ulong r1, | |
56 | target_ulong r2) | |
57 | { | |
58 | target_ulong ret; | |
59 | int64_t t1 = sextract64(r1, 0, 32); | |
60 | int64_t t2 = sextract64(r2, 0, 32); | |
61 | int64_t result = t1 - t2; | |
62 | SSOV(env, ret, result, 32); | |
63 | return ret; | |
64 | } | |
65 | ||
9a31922b BK |
66 | /* context save area (CSA) related helpers */ |
67 | ||
68 | static int cdc_increment(target_ulong *psw) | |
69 | { | |
70 | if ((*psw & MASK_PSW_CDC) == 0x7f) { | |
71 | return 0; | |
72 | } | |
73 | ||
74 | (*psw)++; | |
75 | /* check for overflow */ | |
76 | int lo = clo32((*psw & MASK_PSW_CDC) << (32 - 7)); | |
77 | int mask = (1u << (7 - lo)) - 1; | |
78 | int count = *psw & mask; | |
79 | if (count == 0) { | |
80 | (*psw)--; | |
81 | return 1; | |
82 | } | |
83 | return 0; | |
84 | } | |
85 | ||
86 | static int cdc_decrement(target_ulong *psw) | |
87 | { | |
88 | if ((*psw & MASK_PSW_CDC) == 0x7f) { | |
89 | return 0; | |
90 | } | |
91 | /* check for underflow */ | |
92 | int lo = clo32((*psw & MASK_PSW_CDC) << (32 - 7)); | |
93 | int mask = (1u << (7 - lo)) - 1; | |
94 | int count = *psw & mask; | |
95 | if (count == 0) { | |
96 | return 1; | |
97 | } | |
98 | (*psw)--; | |
99 | return 0; | |
100 | } | |
101 | ||
102 | static void save_context_upper(CPUTriCoreState *env, int ea, | |
103 | target_ulong *new_FCX) | |
104 | { | |
105 | *new_FCX = cpu_ldl_data(env, ea); | |
106 | cpu_stl_data(env, ea, env->PCXI); | |
107 | cpu_stl_data(env, ea+4, env->PSW); | |
108 | cpu_stl_data(env, ea+8, env->gpr_a[10]); | |
109 | cpu_stl_data(env, ea+12, env->gpr_a[11]); | |
110 | cpu_stl_data(env, ea+16, env->gpr_d[8]); | |
111 | cpu_stl_data(env, ea+20, env->gpr_d[9]); | |
112 | cpu_stl_data(env, ea+24, env->gpr_d[10]); | |
113 | cpu_stl_data(env, ea+28, env->gpr_d[11]); | |
114 | cpu_stl_data(env, ea+32, env->gpr_a[12]); | |
115 | cpu_stl_data(env, ea+36, env->gpr_a[13]); | |
116 | cpu_stl_data(env, ea+40, env->gpr_a[14]); | |
117 | cpu_stl_data(env, ea+44, env->gpr_a[15]); | |
118 | cpu_stl_data(env, ea+48, env->gpr_d[12]); | |
119 | cpu_stl_data(env, ea+52, env->gpr_d[13]); | |
120 | cpu_stl_data(env, ea+56, env->gpr_d[14]); | |
121 | cpu_stl_data(env, ea+60, env->gpr_d[15]); | |
122 | ||
123 | } | |
124 | ||
125 | static void restore_context_upper(CPUTriCoreState *env, int ea, | |
126 | target_ulong *new_PCXI, target_ulong *new_PSW) | |
127 | { | |
128 | *new_PCXI = cpu_ldl_data(env, ea); | |
129 | *new_PSW = cpu_ldl_data(env, ea+4); | |
130 | env->gpr_a[10] = cpu_ldl_data(env, ea+8); | |
131 | env->gpr_a[11] = cpu_ldl_data(env, ea+12); | |
132 | env->gpr_d[8] = cpu_ldl_data(env, ea+16); | |
133 | env->gpr_d[9] = cpu_ldl_data(env, ea+20); | |
134 | env->gpr_d[10] = cpu_ldl_data(env, ea+24); | |
135 | env->gpr_d[11] = cpu_ldl_data(env, ea+28); | |
136 | env->gpr_a[12] = cpu_ldl_data(env, ea+32); | |
137 | env->gpr_a[13] = cpu_ldl_data(env, ea+36); | |
138 | env->gpr_a[14] = cpu_ldl_data(env, ea+40); | |
139 | env->gpr_a[15] = cpu_ldl_data(env, ea+44); | |
140 | env->gpr_d[12] = cpu_ldl_data(env, ea+48); | |
141 | env->gpr_d[13] = cpu_ldl_data(env, ea+52); | |
142 | env->gpr_d[14] = cpu_ldl_data(env, ea+56); | |
143 | env->gpr_d[15] = cpu_ldl_data(env, ea+60); | |
144 | cpu_stl_data(env, ea, env->FCX); | |
145 | } | |
146 | ||
147 | void helper_call(CPUTriCoreState *env, uint32_t next_pc) | |
148 | { | |
149 | target_ulong tmp_FCX; | |
150 | target_ulong ea; | |
151 | target_ulong new_FCX; | |
152 | target_ulong psw; | |
153 | ||
154 | psw = psw_read(env); | |
155 | /* if (FCX == 0) trap(FCU); */ | |
156 | if (env->FCX == 0) { | |
157 | /* FCU trap */ | |
158 | } | |
159 | /* if (PSW.CDE) then if (cdc_increment()) then trap(CDO); */ | |
160 | if (psw & MASK_PSW_CDE) { | |
161 | if (cdc_increment(&psw)) { | |
162 | /* CDO trap */ | |
163 | } | |
164 | } | |
165 | /* PSW.CDE = 1;*/ | |
166 | psw |= MASK_PSW_CDE; | |
167 | /* tmp_FCX = FCX; */ | |
168 | tmp_FCX = env->FCX; | |
169 | /* EA = {FCX.FCXS, 6'b0, FCX.FCXO, 6'b0}; */ | |
170 | ea = ((env->FCX & MASK_FCX_FCXS) << 12) + | |
171 | ((env->FCX & MASK_FCX_FCXO) << 6); | |
172 | /* new_FCX = M(EA, word); | |
173 | M(EA, 16 * word) = {PCXI, PSW, A[10], A[11], D[8], D[9], D[10], D[11], | |
174 | A[12], A[13], A[14], A[15], D[12], D[13], D[14], | |
175 | D[15]}; */ | |
176 | save_context_upper(env, ea, &new_FCX); | |
177 | ||
178 | /* PCXI.PCPN = ICR.CCPN; */ | |
179 | env->PCXI = (env->PCXI & 0xffffff) + | |
180 | ((env->ICR & MASK_ICR_CCPN) << 24); | |
181 | /* PCXI.PIE = ICR.IE; */ | |
182 | env->PCXI = ((env->PCXI & ~MASK_PCXI_PIE) + | |
183 | ((env->ICR & MASK_ICR_IE) << 15)); | |
184 | /* PCXI.UL = 1; */ | |
185 | env->PCXI |= MASK_PCXI_UL; | |
186 | ||
187 | /* PCXI[19: 0] = FCX[19: 0]; */ | |
188 | env->PCXI = (env->PCXI & 0xfff00000) + (env->FCX & 0xfffff); | |
189 | /* FCX[19: 0] = new_FCX[19: 0]; */ | |
190 | env->FCX = (env->FCX & 0xfff00000) + (new_FCX & 0xfffff); | |
191 | /* A[11] = next_pc[31: 0]; */ | |
192 | env->gpr_a[11] = next_pc; | |
193 | ||
194 | /* if (tmp_FCX == LCX) trap(FCD);*/ | |
195 | if (tmp_FCX == env->LCX) { | |
196 | /* FCD trap */ | |
197 | } | |
198 | psw_write(env, psw); | |
199 | } | |
200 | ||
201 | void helper_ret(CPUTriCoreState *env) | |
202 | { | |
203 | target_ulong ea; | |
204 | target_ulong new_PCXI; | |
205 | target_ulong new_PSW, psw; | |
206 | ||
207 | psw = psw_read(env); | |
208 | /* if (PSW.CDE) then if (cdc_decrement()) then trap(CDU);*/ | |
209 | if (env->PSW & MASK_PSW_CDE) { | |
210 | if (cdc_decrement(&(env->PSW))) { | |
211 | /* CDU trap */ | |
212 | } | |
213 | } | |
214 | /* if (PCXI[19: 0] == 0) then trap(CSU); */ | |
215 | if ((env->PCXI & 0xfffff) == 0) { | |
216 | /* CSU trap */ | |
217 | } | |
218 | /* if (PCXI.UL == 0) then trap(CTYP); */ | |
219 | if ((env->PCXI & MASK_PCXI_UL) == 0) { | |
220 | /* CTYP trap */ | |
221 | } | |
222 | /* PC = {A11 [31: 1], 1’b0}; */ | |
223 | env->PC = env->gpr_a[11] & 0xfffffffe; | |
224 | ||
225 | /* EA = {PCXI.PCXS, 6'b0, PCXI.PCXO, 6'b0}; */ | |
226 | ea = ((env->PCXI & MASK_PCXI_PCXS) << 12) + | |
227 | ((env->PCXI & MASK_PCXI_PCXO) << 6); | |
228 | /* {new_PCXI, new_PSW, A[10], A[11], D[8], D[9], D[10], D[11], A[12], | |
229 | A[13], A[14], A[15], D[12], D[13], D[14], D[15]} = M(EA, 16 * word); | |
230 | M(EA, word) = FCX; */ | |
231 | restore_context_upper(env, ea, &new_PCXI, &new_PSW); | |
232 | /* FCX[19: 0] = PCXI[19: 0]; */ | |
233 | env->FCX = (env->FCX & 0xfff00000) + (env->PCXI & 0x000fffff); | |
234 | /* PCXI = new_PCXI; */ | |
235 | env->PCXI = new_PCXI; | |
236 | ||
237 | if (tricore_feature(env, TRICORE_FEATURE_13)) { | |
238 | /* PSW = new_PSW */ | |
239 | psw_write(env, new_PSW); | |
240 | } else { | |
241 | /* PSW = {new_PSW[31:26], PSW[25:24], new_PSW[23:0]}; */ | |
242 | psw_write(env, (new_PSW & ~(0x3000000)) + (psw & (0x3000000))); | |
243 | } | |
244 | } | |
245 | ||
2d30267e BK |
246 | static inline void QEMU_NORETURN do_raise_exception_err(CPUTriCoreState *env, |
247 | uint32_t exception, | |
248 | int error_code, | |
249 | uintptr_t pc) | |
250 | { | |
251 | CPUState *cs = CPU(tricore_env_get_cpu(env)); | |
252 | cs->exception_index = exception; | |
253 | env->error_code = error_code; | |
254 | ||
255 | if (pc) { | |
256 | /* now we have a real cpu fault */ | |
257 | cpu_restore_state(cs, pc); | |
258 | } | |
259 | ||
260 | cpu_loop_exit(cs); | |
261 | } | |
262 | ||
263 | static inline void QEMU_NORETURN do_raise_exception(CPUTriCoreState *env, | |
264 | uint32_t exception, | |
265 | uintptr_t pc) | |
266 | { | |
267 | do_raise_exception_err(env, exception, 0, pc); | |
268 | } | |
269 | ||
48e06fe0 BK |
270 | void tlb_fill(CPUState *cs, target_ulong addr, int is_write, int mmu_idx, |
271 | uintptr_t retaddr) | |
272 | { | |
2d30267e BK |
273 | int ret; |
274 | ret = cpu_tricore_handle_mmu_fault(cs, addr, is_write, mmu_idx); | |
275 | if (ret) { | |
276 | TriCoreCPU *cpu = TRICORE_CPU(cs); | |
277 | CPUTriCoreState *env = &cpu->env; | |
278 | do_raise_exception_err(env, cs->exception_index, | |
279 | env->error_code, retaddr); | |
280 | } | |
48e06fe0 | 281 | } |