]> rtime.felk.cvut.cz Git - lisovros/qemu_apohw.git/blob - target-sparc/helper.c
cpu-exec: Change cpu_loop_exit() argument to CPUState
[lisovros/qemu_apohw.git] / target-sparc / helper.c
1 /*
2  *  Misc Sparc helpers
3  *
4  *  Copyright (c) 2003-2005 Fabrice Bellard
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
20 #include "cpu.h"
21 #include "qemu/host-utils.h"
22 #include "helper.h"
23 #include "sysemu/sysemu.h"
24
25 void helper_raise_exception(CPUSPARCState *env, int tt)
26 {
27     CPUState *cs = CPU(sparc_env_get_cpu(env));
28
29     cs->exception_index = tt;
30     cpu_loop_exit(cs);
31 }
32
33 void helper_debug(CPUSPARCState *env)
34 {
35     CPUState *cs = CPU(sparc_env_get_cpu(env));
36
37     cs->exception_index = EXCP_DEBUG;
38     cpu_loop_exit(cs);
39 }
40
41 #ifdef TARGET_SPARC64
42 target_ulong helper_popc(target_ulong val)
43 {
44     return ctpop64(val);
45 }
46
47 void helper_tick_set_count(void *opaque, uint64_t count)
48 {
49 #if !defined(CONFIG_USER_ONLY)
50     cpu_tick_set_count(opaque, count);
51 #endif
52 }
53
54 uint64_t helper_tick_get_count(void *opaque)
55 {
56 #if !defined(CONFIG_USER_ONLY)
57     return cpu_tick_get_count(opaque);
58 #else
59     return 0;
60 #endif
61 }
62
63 void helper_tick_set_limit(void *opaque, uint64_t limit)
64 {
65 #if !defined(CONFIG_USER_ONLY)
66     cpu_tick_set_limit(opaque, limit);
67 #endif
68 }
69 #endif
70
71 static target_ulong helper_udiv_common(CPUSPARCState *env, target_ulong a,
72                                        target_ulong b, int cc)
73 {
74     int overflow = 0;
75     uint64_t x0;
76     uint32_t x1;
77
78     x0 = (a & 0xffffffff) | ((int64_t) (env->y) << 32);
79     x1 = (b & 0xffffffff);
80
81     if (x1 == 0) {
82         cpu_restore_state(env, GETPC());
83         helper_raise_exception(env, TT_DIV_ZERO);
84     }
85
86     x0 = x0 / x1;
87     if (x0 > 0xffffffff) {
88         x0 = 0xffffffff;
89         overflow = 1;
90     }
91
92     if (cc) {
93         env->cc_dst = x0;
94         env->cc_src2 = overflow;
95         env->cc_op = CC_OP_DIV;
96     }
97     return x0;
98 }
99
100 target_ulong helper_udiv(CPUSPARCState *env, target_ulong a, target_ulong b)
101 {
102     return helper_udiv_common(env, a, b, 0);
103 }
104
105 target_ulong helper_udiv_cc(CPUSPARCState *env, target_ulong a, target_ulong b)
106 {
107     return helper_udiv_common(env, a, b, 1);
108 }
109
110 static target_ulong helper_sdiv_common(CPUSPARCState *env, target_ulong a,
111                                        target_ulong b, int cc)
112 {
113     int overflow = 0;
114     int64_t x0;
115     int32_t x1;
116
117     x0 = (a & 0xffffffff) | ((int64_t) (env->y) << 32);
118     x1 = (b & 0xffffffff);
119
120     if (x1 == 0) {
121         cpu_restore_state(env, GETPC());
122         helper_raise_exception(env, TT_DIV_ZERO);
123     }
124
125     x0 = x0 / x1;
126     if ((int32_t) x0 != x0) {
127         x0 = x0 < 0 ? 0x80000000 : 0x7fffffff;
128         overflow = 1;
129     }
130
131     if (cc) {
132         env->cc_dst = x0;
133         env->cc_src2 = overflow;
134         env->cc_op = CC_OP_DIV;
135     }
136     return x0;
137 }
138
139 target_ulong helper_sdiv(CPUSPARCState *env, target_ulong a, target_ulong b)
140 {
141     return helper_sdiv_common(env, a, b, 0);
142 }
143
144 target_ulong helper_sdiv_cc(CPUSPARCState *env, target_ulong a, target_ulong b)
145 {
146     return helper_sdiv_common(env, a, b, 1);
147 }
148
149 #ifdef TARGET_SPARC64
150 int64_t helper_sdivx(CPUSPARCState *env, int64_t a, int64_t b)
151 {
152     if (b == 0) {
153         /* Raise divide by zero trap.  */
154         cpu_restore_state(env, GETPC());
155         helper_raise_exception(env, TT_DIV_ZERO);
156     } else if (b == -1) {
157         /* Avoid overflow trap with i386 divide insn.  */
158         return -a;
159     } else {
160         return a / b;
161     }
162 }
163
164 uint64_t helper_udivx(CPUSPARCState *env, uint64_t a, uint64_t b)
165 {
166     if (b == 0) {
167         /* Raise divide by zero trap.  */
168         cpu_restore_state(env, GETPC());
169         helper_raise_exception(env, TT_DIV_ZERO);
170     }
171     return a / b;
172 }
173 #endif
174
175 target_ulong helper_taddcctv(CPUSPARCState *env, target_ulong src1,
176                              target_ulong src2)
177 {
178     target_ulong dst;
179
180     /* Tag overflow occurs if either input has bits 0 or 1 set.  */
181     if ((src1 | src2) & 3) {
182         goto tag_overflow;
183     }
184
185     dst = src1 + src2;
186
187     /* Tag overflow occurs if the addition overflows.  */
188     if (~(src1 ^ src2) & (src1 ^ dst) & (1u << 31)) {
189         goto tag_overflow;
190     }
191
192     /* Only modify the CC after any exceptions have been generated.  */
193     env->cc_op = CC_OP_TADDTV;
194     env->cc_src = src1;
195     env->cc_src2 = src2;
196     env->cc_dst = dst;
197     return dst;
198
199  tag_overflow:
200     cpu_restore_state(env, GETPC());
201     helper_raise_exception(env, TT_TOVF);
202 }
203
204 target_ulong helper_tsubcctv(CPUSPARCState *env, target_ulong src1,
205                              target_ulong src2)
206 {
207     target_ulong dst;
208
209     /* Tag overflow occurs if either input has bits 0 or 1 set.  */
210     if ((src1 | src2) & 3) {
211         goto tag_overflow;
212     }
213
214     dst = src1 - src2;
215
216     /* Tag overflow occurs if the subtraction overflows.  */
217     if ((src1 ^ src2) & (src1 ^ dst) & (1u << 31)) {
218         goto tag_overflow;
219     }
220
221     /* Only modify the CC after any exceptions have been generated.  */
222     env->cc_op = CC_OP_TSUBTV;
223     env->cc_src = src1;
224     env->cc_src2 = src2;
225     env->cc_dst = dst;
226     return dst;
227
228  tag_overflow:
229     cpu_restore_state(env, GETPC());
230     helper_raise_exception(env, TT_TOVF);
231 }
232
233 #ifndef TARGET_SPARC64
234 void helper_power_down(CPUSPARCState *env)
235 {
236     CPUState *cs = CPU(sparc_env_get_cpu(env));
237
238     cs->halted = 1;
239     cs->exception_index = EXCP_HLT;
240     env->pc = env->npc;
241     env->npc = env->pc + 4;
242     cpu_loop_exit(cs);
243 }
244 #endif