xref: /openbmc/qemu/target/sparc/helper.c (revision 5a59fbce)
1fcf5ef2aSThomas Huth /*
2fcf5ef2aSThomas Huth  *  Misc Sparc helpers
3fcf5ef2aSThomas Huth  *
4fcf5ef2aSThomas Huth  *  Copyright (c) 2003-2005 Fabrice Bellard
5fcf5ef2aSThomas Huth  *
6fcf5ef2aSThomas Huth  * This library is free software; you can redistribute it and/or
7fcf5ef2aSThomas Huth  * modify it under the terms of the GNU Lesser General Public
8fcf5ef2aSThomas Huth  * License as published by the Free Software Foundation; either
9fcf5ef2aSThomas Huth  * version 2 of the License, or (at your option) any later version.
10fcf5ef2aSThomas Huth  *
11fcf5ef2aSThomas Huth  * This library is distributed in the hope that it will be useful,
12fcf5ef2aSThomas Huth  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13fcf5ef2aSThomas Huth  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14fcf5ef2aSThomas Huth  * Lesser General Public License for more details.
15fcf5ef2aSThomas Huth  *
16fcf5ef2aSThomas Huth  * You should have received a copy of the GNU Lesser General Public
17fcf5ef2aSThomas Huth  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18fcf5ef2aSThomas Huth  */
19fcf5ef2aSThomas Huth 
20fcf5ef2aSThomas Huth #include "qemu/osdep.h"
21fcf5ef2aSThomas Huth #include "cpu.h"
22fcf5ef2aSThomas Huth #include "exec/exec-all.h"
23fcf5ef2aSThomas Huth #include "qemu/host-utils.h"
24fcf5ef2aSThomas Huth #include "exec/helper-proto.h"
25fcf5ef2aSThomas Huth #include "sysemu/sysemu.h"
26fcf5ef2aSThomas Huth 
27fcf5ef2aSThomas Huth void cpu_raise_exception_ra(CPUSPARCState *env, int tt, uintptr_t ra)
28fcf5ef2aSThomas Huth {
29*5a59fbceSRichard Henderson     CPUState *cs = env_cpu(env);
30fcf5ef2aSThomas Huth 
31fcf5ef2aSThomas Huth     cs->exception_index = tt;
32fcf5ef2aSThomas Huth     cpu_loop_exit_restore(cs, ra);
33fcf5ef2aSThomas Huth }
34fcf5ef2aSThomas Huth 
35fcf5ef2aSThomas Huth void helper_raise_exception(CPUSPARCState *env, int tt)
36fcf5ef2aSThomas Huth {
37*5a59fbceSRichard Henderson     CPUState *cs = env_cpu(env);
38fcf5ef2aSThomas Huth 
39fcf5ef2aSThomas Huth     cs->exception_index = tt;
40fcf5ef2aSThomas Huth     cpu_loop_exit(cs);
41fcf5ef2aSThomas Huth }
42fcf5ef2aSThomas Huth 
43fcf5ef2aSThomas Huth void helper_debug(CPUSPARCState *env)
44fcf5ef2aSThomas Huth {
45*5a59fbceSRichard Henderson     CPUState *cs = env_cpu(env);
46fcf5ef2aSThomas Huth 
47fcf5ef2aSThomas Huth     cs->exception_index = EXCP_DEBUG;
48fcf5ef2aSThomas Huth     cpu_loop_exit(cs);
49fcf5ef2aSThomas Huth }
50fcf5ef2aSThomas Huth 
51fcf5ef2aSThomas Huth #ifdef TARGET_SPARC64
52fcf5ef2aSThomas Huth void helper_tick_set_count(void *opaque, uint64_t count)
53fcf5ef2aSThomas Huth {
54fcf5ef2aSThomas Huth #if !defined(CONFIG_USER_ONLY)
55fcf5ef2aSThomas Huth     cpu_tick_set_count(opaque, count);
56fcf5ef2aSThomas Huth #endif
57fcf5ef2aSThomas Huth }
58fcf5ef2aSThomas Huth 
59fcf5ef2aSThomas Huth uint64_t helper_tick_get_count(CPUSPARCState *env, void *opaque, int mem_idx)
60fcf5ef2aSThomas Huth {
61fcf5ef2aSThomas Huth #if !defined(CONFIG_USER_ONLY)
62fcf5ef2aSThomas Huth     CPUTimer *timer = opaque;
63fcf5ef2aSThomas Huth 
64fcf5ef2aSThomas Huth     if (timer->npt && mem_idx < MMU_KERNEL_IDX) {
65fcf5ef2aSThomas Huth         cpu_raise_exception_ra(env, TT_PRIV_INSN, GETPC());
66fcf5ef2aSThomas Huth     }
67fcf5ef2aSThomas Huth 
68fcf5ef2aSThomas Huth     return cpu_tick_get_count(timer);
69fcf5ef2aSThomas Huth #else
70b8e13ba9SLaurent Vivier     /* In user-mode, QEMU_CLOCK_VIRTUAL doesn't exist.
71b8e13ba9SLaurent Vivier        Just pass through the host cpu clock ticks.  */
72b8e13ba9SLaurent Vivier     return cpu_get_host_ticks();
73fcf5ef2aSThomas Huth #endif
74fcf5ef2aSThomas Huth }
75fcf5ef2aSThomas Huth 
76fcf5ef2aSThomas Huth void helper_tick_set_limit(void *opaque, uint64_t limit)
77fcf5ef2aSThomas Huth {
78fcf5ef2aSThomas Huth #if !defined(CONFIG_USER_ONLY)
79fcf5ef2aSThomas Huth     cpu_tick_set_limit(opaque, limit);
80fcf5ef2aSThomas Huth #endif
81fcf5ef2aSThomas Huth }
82fcf5ef2aSThomas Huth #endif
83fcf5ef2aSThomas Huth 
84fcf5ef2aSThomas Huth static target_ulong do_udiv(CPUSPARCState *env, target_ulong a,
85fcf5ef2aSThomas Huth                             target_ulong b, int cc, uintptr_t ra)
86fcf5ef2aSThomas Huth {
87fcf5ef2aSThomas Huth     int overflow = 0;
88fcf5ef2aSThomas Huth     uint64_t x0;
89fcf5ef2aSThomas Huth     uint32_t x1;
90fcf5ef2aSThomas Huth 
91fcf5ef2aSThomas Huth     x0 = (a & 0xffffffff) | ((int64_t) (env->y) << 32);
92fcf5ef2aSThomas Huth     x1 = (b & 0xffffffff);
93fcf5ef2aSThomas Huth 
94fcf5ef2aSThomas Huth     if (x1 == 0) {
95fcf5ef2aSThomas Huth         cpu_raise_exception_ra(env, TT_DIV_ZERO, ra);
96fcf5ef2aSThomas Huth     }
97fcf5ef2aSThomas Huth 
98fcf5ef2aSThomas Huth     x0 = x0 / x1;
99fcf5ef2aSThomas Huth     if (x0 > UINT32_MAX) {
100fcf5ef2aSThomas Huth         x0 = UINT32_MAX;
101fcf5ef2aSThomas Huth         overflow = 1;
102fcf5ef2aSThomas Huth     }
103fcf5ef2aSThomas Huth 
104fcf5ef2aSThomas Huth     if (cc) {
105fcf5ef2aSThomas Huth         env->cc_dst = x0;
106fcf5ef2aSThomas Huth         env->cc_src2 = overflow;
107fcf5ef2aSThomas Huth         env->cc_op = CC_OP_DIV;
108fcf5ef2aSThomas Huth     }
109fcf5ef2aSThomas Huth     return x0;
110fcf5ef2aSThomas Huth }
111fcf5ef2aSThomas Huth 
112fcf5ef2aSThomas Huth target_ulong helper_udiv(CPUSPARCState *env, target_ulong a, target_ulong b)
113fcf5ef2aSThomas Huth {
114fcf5ef2aSThomas Huth     return do_udiv(env, a, b, 0, GETPC());
115fcf5ef2aSThomas Huth }
116fcf5ef2aSThomas Huth 
117fcf5ef2aSThomas Huth target_ulong helper_udiv_cc(CPUSPARCState *env, target_ulong a, target_ulong b)
118fcf5ef2aSThomas Huth {
119fcf5ef2aSThomas Huth     return do_udiv(env, a, b, 1, GETPC());
120fcf5ef2aSThomas Huth }
121fcf5ef2aSThomas Huth 
122fcf5ef2aSThomas Huth static target_ulong do_sdiv(CPUSPARCState *env, target_ulong a,
123fcf5ef2aSThomas Huth                             target_ulong b, int cc, uintptr_t ra)
124fcf5ef2aSThomas Huth {
125fcf5ef2aSThomas Huth     int overflow = 0;
126fcf5ef2aSThomas Huth     int64_t x0;
127fcf5ef2aSThomas Huth     int32_t x1;
128fcf5ef2aSThomas Huth 
129fcf5ef2aSThomas Huth     x0 = (a & 0xffffffff) | ((int64_t) (env->y) << 32);
130fcf5ef2aSThomas Huth     x1 = (b & 0xffffffff);
131fcf5ef2aSThomas Huth 
132fcf5ef2aSThomas Huth     if (x1 == 0) {
133fcf5ef2aSThomas Huth         cpu_raise_exception_ra(env, TT_DIV_ZERO, ra);
134fcf5ef2aSThomas Huth     } else if (x1 == -1 && x0 == INT64_MIN) {
135fcf5ef2aSThomas Huth         x0 = INT32_MAX;
136fcf5ef2aSThomas Huth         overflow = 1;
137fcf5ef2aSThomas Huth     } else {
138fcf5ef2aSThomas Huth         x0 = x0 / x1;
139fcf5ef2aSThomas Huth         if ((int32_t) x0 != x0) {
140fcf5ef2aSThomas Huth             x0 = x0 < 0 ? INT32_MIN : INT32_MAX;
141fcf5ef2aSThomas Huth             overflow = 1;
142fcf5ef2aSThomas Huth         }
143fcf5ef2aSThomas Huth     }
144fcf5ef2aSThomas Huth 
145fcf5ef2aSThomas Huth     if (cc) {
146fcf5ef2aSThomas Huth         env->cc_dst = x0;
147fcf5ef2aSThomas Huth         env->cc_src2 = overflow;
148fcf5ef2aSThomas Huth         env->cc_op = CC_OP_DIV;
149fcf5ef2aSThomas Huth     }
150fcf5ef2aSThomas Huth     return x0;
151fcf5ef2aSThomas Huth }
152fcf5ef2aSThomas Huth 
153fcf5ef2aSThomas Huth target_ulong helper_sdiv(CPUSPARCState *env, target_ulong a, target_ulong b)
154fcf5ef2aSThomas Huth {
155fcf5ef2aSThomas Huth     return do_sdiv(env, a, b, 0, GETPC());
156fcf5ef2aSThomas Huth }
157fcf5ef2aSThomas Huth 
158fcf5ef2aSThomas Huth target_ulong helper_sdiv_cc(CPUSPARCState *env, target_ulong a, target_ulong b)
159fcf5ef2aSThomas Huth {
160fcf5ef2aSThomas Huth     return do_sdiv(env, a, b, 1, GETPC());
161fcf5ef2aSThomas Huth }
162fcf5ef2aSThomas Huth 
163fcf5ef2aSThomas Huth #ifdef TARGET_SPARC64
164fcf5ef2aSThomas Huth int64_t helper_sdivx(CPUSPARCState *env, int64_t a, int64_t b)
165fcf5ef2aSThomas Huth {
166fcf5ef2aSThomas Huth     if (b == 0) {
167fcf5ef2aSThomas Huth         /* Raise divide by zero trap.  */
168fcf5ef2aSThomas Huth         cpu_raise_exception_ra(env, TT_DIV_ZERO, GETPC());
169fcf5ef2aSThomas Huth     } else if (b == -1) {
170fcf5ef2aSThomas Huth         /* Avoid overflow trap with i386 divide insn.  */
171fcf5ef2aSThomas Huth         return -a;
172fcf5ef2aSThomas Huth     } else {
173fcf5ef2aSThomas Huth         return a / b;
174fcf5ef2aSThomas Huth     }
175fcf5ef2aSThomas Huth }
176fcf5ef2aSThomas Huth 
177fcf5ef2aSThomas Huth uint64_t helper_udivx(CPUSPARCState *env, uint64_t a, uint64_t b)
178fcf5ef2aSThomas Huth {
179fcf5ef2aSThomas Huth     if (b == 0) {
180fcf5ef2aSThomas Huth         /* Raise divide by zero trap.  */
181fcf5ef2aSThomas Huth         cpu_raise_exception_ra(env, TT_DIV_ZERO, GETPC());
182fcf5ef2aSThomas Huth     }
183fcf5ef2aSThomas Huth     return a / b;
184fcf5ef2aSThomas Huth }
185fcf5ef2aSThomas Huth #endif
186fcf5ef2aSThomas Huth 
187fcf5ef2aSThomas Huth target_ulong helper_taddcctv(CPUSPARCState *env, target_ulong src1,
188fcf5ef2aSThomas Huth                              target_ulong src2)
189fcf5ef2aSThomas Huth {
190fcf5ef2aSThomas Huth     target_ulong dst;
191fcf5ef2aSThomas Huth 
192fcf5ef2aSThomas Huth     /* Tag overflow occurs if either input has bits 0 or 1 set.  */
193fcf5ef2aSThomas Huth     if ((src1 | src2) & 3) {
194fcf5ef2aSThomas Huth         goto tag_overflow;
195fcf5ef2aSThomas Huth     }
196fcf5ef2aSThomas Huth 
197fcf5ef2aSThomas Huth     dst = src1 + src2;
198fcf5ef2aSThomas Huth 
199fcf5ef2aSThomas Huth     /* Tag overflow occurs if the addition overflows.  */
200fcf5ef2aSThomas Huth     if (~(src1 ^ src2) & (src1 ^ dst) & (1u << 31)) {
201fcf5ef2aSThomas Huth         goto tag_overflow;
202fcf5ef2aSThomas Huth     }
203fcf5ef2aSThomas Huth 
204fcf5ef2aSThomas Huth     /* Only modify the CC after any exceptions have been generated.  */
205fcf5ef2aSThomas Huth     env->cc_op = CC_OP_TADDTV;
206fcf5ef2aSThomas Huth     env->cc_src = src1;
207fcf5ef2aSThomas Huth     env->cc_src2 = src2;
208fcf5ef2aSThomas Huth     env->cc_dst = dst;
209fcf5ef2aSThomas Huth     return dst;
210fcf5ef2aSThomas Huth 
211fcf5ef2aSThomas Huth  tag_overflow:
212fcf5ef2aSThomas Huth     cpu_raise_exception_ra(env, TT_TOVF, GETPC());
213fcf5ef2aSThomas Huth }
214fcf5ef2aSThomas Huth 
215fcf5ef2aSThomas Huth target_ulong helper_tsubcctv(CPUSPARCState *env, target_ulong src1,
216fcf5ef2aSThomas Huth                              target_ulong src2)
217fcf5ef2aSThomas Huth {
218fcf5ef2aSThomas Huth     target_ulong dst;
219fcf5ef2aSThomas Huth 
220fcf5ef2aSThomas Huth     /* Tag overflow occurs if either input has bits 0 or 1 set.  */
221fcf5ef2aSThomas Huth     if ((src1 | src2) & 3) {
222fcf5ef2aSThomas Huth         goto tag_overflow;
223fcf5ef2aSThomas Huth     }
224fcf5ef2aSThomas Huth 
225fcf5ef2aSThomas Huth     dst = src1 - src2;
226fcf5ef2aSThomas Huth 
227fcf5ef2aSThomas Huth     /* Tag overflow occurs if the subtraction overflows.  */
228fcf5ef2aSThomas Huth     if ((src1 ^ src2) & (src1 ^ dst) & (1u << 31)) {
229fcf5ef2aSThomas Huth         goto tag_overflow;
230fcf5ef2aSThomas Huth     }
231fcf5ef2aSThomas Huth 
232fcf5ef2aSThomas Huth     /* Only modify the CC after any exceptions have been generated.  */
233fcf5ef2aSThomas Huth     env->cc_op = CC_OP_TSUBTV;
234fcf5ef2aSThomas Huth     env->cc_src = src1;
235fcf5ef2aSThomas Huth     env->cc_src2 = src2;
236fcf5ef2aSThomas Huth     env->cc_dst = dst;
237fcf5ef2aSThomas Huth     return dst;
238fcf5ef2aSThomas Huth 
239fcf5ef2aSThomas Huth  tag_overflow:
240fcf5ef2aSThomas Huth     cpu_raise_exception_ra(env, TT_TOVF, GETPC());
241fcf5ef2aSThomas Huth }
242fcf5ef2aSThomas Huth 
243fcf5ef2aSThomas Huth #ifndef TARGET_SPARC64
244fcf5ef2aSThomas Huth void helper_power_down(CPUSPARCState *env)
245fcf5ef2aSThomas Huth {
246*5a59fbceSRichard Henderson     CPUState *cs = env_cpu(env);
247fcf5ef2aSThomas Huth 
248fcf5ef2aSThomas Huth     cs->halted = 1;
249fcf5ef2aSThomas Huth     cs->exception_index = EXCP_HLT;
250fcf5ef2aSThomas Huth     env->pc = env->npc;
251fcf5ef2aSThomas Huth     env->npc = env->pc + 4;
252fcf5ef2aSThomas Huth     cpu_loop_exit(cs);
253fcf5ef2aSThomas Huth }
254fcf5ef2aSThomas Huth #endif
255