linux-user: fix emulation of /proc/self/maps
[qemu/ar7.git] / target-unicore32 / helper.c
blob9fe4a375e40685caa19d4173e0497fe872abaf75
1 /*
2 * Copyright (C) 2010-2011 GUAN Xue-tao
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * Contributions from 2012-04-01 on are considered under GPL version 2,
9 * or (at your option) any later version.
12 #include "cpu.h"
13 #include "gdbstub.h"
14 #include "helper.h"
15 #include "host-utils.h"
17 CPUUniCore32State *uc32_cpu_init(const char *cpu_model)
19 UniCore32CPU *cpu;
20 CPUUniCore32State *env;
21 static int inited = 1;
23 if (object_class_by_name(cpu_model) == NULL) {
24 return NULL;
26 cpu = UNICORE32_CPU(object_new(cpu_model));
27 env = &cpu->env;
29 if (inited) {
30 inited = 0;
31 uc32_translate_init();
34 qemu_init_vcpu(env);
35 return env;
38 uint32_t HELPER(clo)(uint32_t x)
40 return clo32(x);
43 uint32_t HELPER(clz)(uint32_t x)
45 return clz32(x);
48 void do_interrupt(CPUUniCore32State *env)
50 env->exception_index = -1;
53 int uc32_cpu_handle_mmu_fault(CPUUniCore32State *env, target_ulong address, int rw,
54 int mmu_idx)
56 env->exception_index = UC32_EXCP_TRAP;
57 env->cp0.c4_faultaddr = address;
58 return 1;
61 /* These should probably raise undefined insn exceptions. */
62 void HELPER(set_cp)(CPUUniCore32State *env, uint32_t insn, uint32_t val)
64 int op1 = (insn >> 8) & 0xf;
65 cpu_abort(env, "cp%i insn %08x\n", op1, insn);
66 return;
69 uint32_t HELPER(get_cp)(CPUUniCore32State *env, uint32_t insn)
71 int op1 = (insn >> 8) & 0xf;
72 cpu_abort(env, "cp%i insn %08x\n", op1, insn);
73 return 0;
76 void HELPER(set_cp0)(CPUUniCore32State *env, uint32_t insn, uint32_t val)
78 cpu_abort(env, "cp0 insn %08x\n", insn);
81 uint32_t HELPER(get_cp0)(CPUUniCore32State *env, uint32_t insn)
83 cpu_abort(env, "cp0 insn %08x\n", insn);
84 return 0;
87 void switch_mode(CPUUniCore32State *env, int mode)
89 if (mode != ASR_MODE_USER) {
90 cpu_abort(env, "Tried to switch out of user mode\n");
94 void HELPER(set_r29_banked)(CPUUniCore32State *env, uint32_t mode, uint32_t val)
96 cpu_abort(env, "banked r29 write\n");
99 uint32_t HELPER(get_r29_banked)(CPUUniCore32State *env, uint32_t mode)
101 cpu_abort(env, "banked r29 read\n");
102 return 0;
105 /* UniCore-F64 support. We follow the convention used for F64 instrunctions:
106 Single precition routines have a "s" suffix, double precision a
107 "d" suffix. */
109 /* Convert host exception flags to f64 form. */
110 static inline int ucf64_exceptbits_from_host(int host_bits)
112 int target_bits = 0;
114 if (host_bits & float_flag_invalid) {
115 target_bits |= UCF64_FPSCR_FLAG_INVALID;
117 if (host_bits & float_flag_divbyzero) {
118 target_bits |= UCF64_FPSCR_FLAG_DIVZERO;
120 if (host_bits & float_flag_overflow) {
121 target_bits |= UCF64_FPSCR_FLAG_OVERFLOW;
123 if (host_bits & float_flag_underflow) {
124 target_bits |= UCF64_FPSCR_FLAG_UNDERFLOW;
126 if (host_bits & float_flag_inexact) {
127 target_bits |= UCF64_FPSCR_FLAG_INEXACT;
129 return target_bits;
132 uint32_t HELPER(ucf64_get_fpscr)(CPUUniCore32State *env)
134 int i;
135 uint32_t fpscr;
137 fpscr = (env->ucf64.xregs[UC32_UCF64_FPSCR] & UCF64_FPSCR_MASK);
138 i = get_float_exception_flags(&env->ucf64.fp_status);
139 fpscr |= ucf64_exceptbits_from_host(i);
140 return fpscr;
143 /* Convert ucf64 exception flags to target form. */
144 static inline int ucf64_exceptbits_to_host(int target_bits)
146 int host_bits = 0;
148 if (target_bits & UCF64_FPSCR_FLAG_INVALID) {
149 host_bits |= float_flag_invalid;
151 if (target_bits & UCF64_FPSCR_FLAG_DIVZERO) {
152 host_bits |= float_flag_divbyzero;
154 if (target_bits & UCF64_FPSCR_FLAG_OVERFLOW) {
155 host_bits |= float_flag_overflow;
157 if (target_bits & UCF64_FPSCR_FLAG_UNDERFLOW) {
158 host_bits |= float_flag_underflow;
160 if (target_bits & UCF64_FPSCR_FLAG_INEXACT) {
161 host_bits |= float_flag_inexact;
163 return host_bits;
166 void HELPER(ucf64_set_fpscr)(CPUUniCore32State *env, uint32_t val)
168 int i;
169 uint32_t changed;
171 changed = env->ucf64.xregs[UC32_UCF64_FPSCR];
172 env->ucf64.xregs[UC32_UCF64_FPSCR] = (val & UCF64_FPSCR_MASK);
174 changed ^= val;
175 if (changed & (UCF64_FPSCR_RND_MASK)) {
176 i = UCF64_FPSCR_RND(val);
177 switch (i) {
178 case 0:
179 i = float_round_nearest_even;
180 break;
181 case 1:
182 i = float_round_to_zero;
183 break;
184 case 2:
185 i = float_round_up;
186 break;
187 case 3:
188 i = float_round_down;
189 break;
190 default: /* 100 and 101 not implement */
191 cpu_abort(env, "Unsupported UniCore-F64 round mode");
193 set_float_rounding_mode(i, &env->ucf64.fp_status);
196 i = ucf64_exceptbits_to_host(UCF64_FPSCR_TRAPEN(val));
197 set_float_exception_flags(i, &env->ucf64.fp_status);
200 float32 HELPER(ucf64_adds)(float32 a, float32 b, CPUUniCore32State *env)
202 return float32_add(a, b, &env->ucf64.fp_status);
205 float64 HELPER(ucf64_addd)(float64 a, float64 b, CPUUniCore32State *env)
207 return float64_add(a, b, &env->ucf64.fp_status);
210 float32 HELPER(ucf64_subs)(float32 a, float32 b, CPUUniCore32State *env)
212 return float32_sub(a, b, &env->ucf64.fp_status);
215 float64 HELPER(ucf64_subd)(float64 a, float64 b, CPUUniCore32State *env)
217 return float64_sub(a, b, &env->ucf64.fp_status);
220 float32 HELPER(ucf64_muls)(float32 a, float32 b, CPUUniCore32State *env)
222 return float32_mul(a, b, &env->ucf64.fp_status);
225 float64 HELPER(ucf64_muld)(float64 a, float64 b, CPUUniCore32State *env)
227 return float64_mul(a, b, &env->ucf64.fp_status);
230 float32 HELPER(ucf64_divs)(float32 a, float32 b, CPUUniCore32State *env)
232 return float32_div(a, b, &env->ucf64.fp_status);
235 float64 HELPER(ucf64_divd)(float64 a, float64 b, CPUUniCore32State *env)
237 return float64_div(a, b, &env->ucf64.fp_status);
240 float32 HELPER(ucf64_negs)(float32 a)
242 return float32_chs(a);
245 float64 HELPER(ucf64_negd)(float64 a)
247 return float64_chs(a);
250 float32 HELPER(ucf64_abss)(float32 a)
252 return float32_abs(a);
255 float64 HELPER(ucf64_absd)(float64 a)
257 return float64_abs(a);
260 /* XXX: check quiet/signaling case */
261 void HELPER(ucf64_cmps)(float32 a, float32 b, uint32_t c, CPUUniCore32State *env)
263 int flag;
264 flag = float32_compare_quiet(a, b, &env->ucf64.fp_status);
265 env->CF = 0;
266 switch (c & 0x7) {
267 case 0: /* F */
268 break;
269 case 1: /* UN */
270 if (flag == 2) {
271 env->CF = 1;
273 break;
274 case 2: /* EQ */
275 if (flag == 0) {
276 env->CF = 1;
278 break;
279 case 3: /* UEQ */
280 if ((flag == 0) || (flag == 2)) {
281 env->CF = 1;
283 break;
284 case 4: /* OLT */
285 if (flag == -1) {
286 env->CF = 1;
288 break;
289 case 5: /* ULT */
290 if ((flag == -1) || (flag == 2)) {
291 env->CF = 1;
293 break;
294 case 6: /* OLE */
295 if ((flag == -1) || (flag == 0)) {
296 env->CF = 1;
298 break;
299 case 7: /* ULE */
300 if (flag != 1) {
301 env->CF = 1;
303 break;
305 env->ucf64.xregs[UC32_UCF64_FPSCR] = (env->CF << 29)
306 | (env->ucf64.xregs[UC32_UCF64_FPSCR] & 0x0fffffff);
309 void HELPER(ucf64_cmpd)(float64 a, float64 b, uint32_t c, CPUUniCore32State *env)
311 int flag;
312 flag = float64_compare_quiet(a, b, &env->ucf64.fp_status);
313 env->CF = 0;
314 switch (c & 0x7) {
315 case 0: /* F */
316 break;
317 case 1: /* UN */
318 if (flag == 2) {
319 env->CF = 1;
321 break;
322 case 2: /* EQ */
323 if (flag == 0) {
324 env->CF = 1;
326 break;
327 case 3: /* UEQ */
328 if ((flag == 0) || (flag == 2)) {
329 env->CF = 1;
331 break;
332 case 4: /* OLT */
333 if (flag == -1) {
334 env->CF = 1;
336 break;
337 case 5: /* ULT */
338 if ((flag == -1) || (flag == 2)) {
339 env->CF = 1;
341 break;
342 case 6: /* OLE */
343 if ((flag == -1) || (flag == 0)) {
344 env->CF = 1;
346 break;
347 case 7: /* ULE */
348 if (flag != 1) {
349 env->CF = 1;
351 break;
353 env->ucf64.xregs[UC32_UCF64_FPSCR] = (env->CF << 29)
354 | (env->ucf64.xregs[UC32_UCF64_FPSCR] & 0x0fffffff);
357 /* Helper routines to perform bitwise copies between float and int. */
358 static inline float32 ucf64_itos(uint32_t i)
360 union {
361 uint32_t i;
362 float32 s;
363 } v;
365 v.i = i;
366 return v.s;
369 static inline uint32_t ucf64_stoi(float32 s)
371 union {
372 uint32_t i;
373 float32 s;
374 } v;
376 v.s = s;
377 return v.i;
380 static inline float64 ucf64_itod(uint64_t i)
382 union {
383 uint64_t i;
384 float64 d;
385 } v;
387 v.i = i;
388 return v.d;
391 static inline uint64_t ucf64_dtoi(float64 d)
393 union {
394 uint64_t i;
395 float64 d;
396 } v;
398 v.d = d;
399 return v.i;
402 /* Integer to float conversion. */
403 float32 HELPER(ucf64_si2sf)(float32 x, CPUUniCore32State *env)
405 return int32_to_float32(ucf64_stoi(x), &env->ucf64.fp_status);
408 float64 HELPER(ucf64_si2df)(float32 x, CPUUniCore32State *env)
410 return int32_to_float64(ucf64_stoi(x), &env->ucf64.fp_status);
413 /* Float to integer conversion. */
414 float32 HELPER(ucf64_sf2si)(float32 x, CPUUniCore32State *env)
416 return ucf64_itos(float32_to_int32(x, &env->ucf64.fp_status));
419 float32 HELPER(ucf64_df2si)(float64 x, CPUUniCore32State *env)
421 return ucf64_itos(float64_to_int32(x, &env->ucf64.fp_status));
424 /* floating point conversion */
425 float64 HELPER(ucf64_sf2df)(float32 x, CPUUniCore32State *env)
427 return float32_to_float64(x, &env->ucf64.fp_status);
430 float32 HELPER(ucf64_df2sf)(float64 x, CPUUniCore32State *env)
432 return float64_to_float32(x, &env->ucf64.fp_status);