1e8af50a3Sbellard /* 2163fa5caSBlue Swirl * Misc Sparc helpers 3e8af50a3Sbellard * 483469015Sbellard * Copyright (c) 2003-2005 Fabrice Bellard 5e8af50a3Sbellard * 6e8af50a3Sbellard * This library is free software; you can redistribute it and/or 7e8af50a3Sbellard * modify it under the terms of the GNU Lesser General Public 8e8af50a3Sbellard * License as published by the Free Software Foundation; either 9e8af50a3Sbellard * version 2 of the License, or (at your option) any later version. 10e8af50a3Sbellard * 11e8af50a3Sbellard * This library is distributed in the hope that it will be useful, 12e8af50a3Sbellard * but WITHOUT ANY WARRANTY; without even the implied warranty of 13e8af50a3Sbellard * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14e8af50a3Sbellard * Lesser General Public License for more details. 15e8af50a3Sbellard * 16e8af50a3Sbellard * You should have received a copy of the GNU Lesser General Public 178167ee88SBlue Swirl * License along with this library; if not, see <http://www.gnu.org/licenses/>. 18e8af50a3Sbellard */ 19ee5bbe38Sbellard 20ee5bbe38Sbellard #include "cpu.h" 212336c1f1SBlue Swirl #include "host-utils.h" 222336c1f1SBlue Swirl #include "helper.h" 232336c1f1SBlue Swirl #include "sysemu.h" 24e8af50a3Sbellard 25c5f9864eSAndreas Färber void helper_raise_exception(CPUSPARCState *env, int tt) 26bc265319SBlue Swirl { 27bc265319SBlue Swirl env->exception_index = tt; 28bc265319SBlue Swirl cpu_loop_exit(env); 29bc265319SBlue Swirl } 30bc265319SBlue Swirl 31c5f9864eSAndreas Färber void helper_debug(CPUSPARCState *env) 32bc265319SBlue Swirl { 33bc265319SBlue Swirl env->exception_index = EXCP_DEBUG; 34bc265319SBlue Swirl cpu_loop_exit(env); 35bc265319SBlue Swirl } 36bc265319SBlue Swirl 372336c1f1SBlue Swirl #ifdef TARGET_SPARC64 382336c1f1SBlue Swirl target_ulong helper_popc(target_ulong val) 392336c1f1SBlue Swirl { 402336c1f1SBlue Swirl return ctpop64(val); 412336c1f1SBlue Swirl } 422336c1f1SBlue Swirl 432336c1f1SBlue Swirl void helper_tick_set_count(void *opaque, uint64_t count) 442336c1f1SBlue Swirl { 452336c1f1SBlue Swirl #if !defined(CONFIG_USER_ONLY) 462336c1f1SBlue Swirl cpu_tick_set_count(opaque, count); 472336c1f1SBlue Swirl #endif 482336c1f1SBlue Swirl } 492336c1f1SBlue Swirl 502336c1f1SBlue Swirl uint64_t helper_tick_get_count(void *opaque) 512336c1f1SBlue Swirl { 522336c1f1SBlue Swirl #if !defined(CONFIG_USER_ONLY) 532336c1f1SBlue Swirl return cpu_tick_get_count(opaque); 542336c1f1SBlue Swirl #else 552336c1f1SBlue Swirl return 0; 562336c1f1SBlue Swirl #endif 572336c1f1SBlue Swirl } 582336c1f1SBlue Swirl 592336c1f1SBlue Swirl void helper_tick_set_limit(void *opaque, uint64_t limit) 602336c1f1SBlue Swirl { 612336c1f1SBlue Swirl #if !defined(CONFIG_USER_ONLY) 622336c1f1SBlue Swirl cpu_tick_set_limit(opaque, limit); 632336c1f1SBlue Swirl #endif 642336c1f1SBlue Swirl } 652336c1f1SBlue Swirl #endif 667a5e4488SBlue Swirl 67c5f9864eSAndreas Färber static target_ulong helper_udiv_common(CPUSPARCState *env, target_ulong a, 687a5e4488SBlue Swirl target_ulong b, int cc) 697a5e4488SBlue Swirl { 707a5e4488SBlue Swirl int overflow = 0; 717a5e4488SBlue Swirl uint64_t x0; 727a5e4488SBlue Swirl uint32_t x1; 737a5e4488SBlue Swirl 747a5e4488SBlue Swirl x0 = (a & 0xffffffff) | ((int64_t) (env->y) << 32); 757a5e4488SBlue Swirl x1 = (b & 0xffffffff); 767a5e4488SBlue Swirl 777a5e4488SBlue Swirl if (x1 == 0) { 78c28ae41eSRichard Henderson cpu_restore_state2(env, GETPC()); 797a5e4488SBlue Swirl helper_raise_exception(env, TT_DIV_ZERO); 807a5e4488SBlue Swirl } 817a5e4488SBlue Swirl 827a5e4488SBlue Swirl x0 = x0 / x1; 837a5e4488SBlue Swirl if (x0 > 0xffffffff) { 847a5e4488SBlue Swirl x0 = 0xffffffff; 857a5e4488SBlue Swirl overflow = 1; 867a5e4488SBlue Swirl } 877a5e4488SBlue Swirl 887a5e4488SBlue Swirl if (cc) { 897a5e4488SBlue Swirl env->cc_dst = x0; 907a5e4488SBlue Swirl env->cc_src2 = overflow; 917a5e4488SBlue Swirl env->cc_op = CC_OP_DIV; 927a5e4488SBlue Swirl } 937a5e4488SBlue Swirl return x0; 947a5e4488SBlue Swirl } 957a5e4488SBlue Swirl 96c5f9864eSAndreas Färber target_ulong helper_udiv(CPUSPARCState *env, target_ulong a, target_ulong b) 977a5e4488SBlue Swirl { 987a5e4488SBlue Swirl return helper_udiv_common(env, a, b, 0); 997a5e4488SBlue Swirl } 1007a5e4488SBlue Swirl 101c5f9864eSAndreas Färber target_ulong helper_udiv_cc(CPUSPARCState *env, target_ulong a, target_ulong b) 1027a5e4488SBlue Swirl { 1037a5e4488SBlue Swirl return helper_udiv_common(env, a, b, 1); 1047a5e4488SBlue Swirl } 1057a5e4488SBlue Swirl 106c5f9864eSAndreas Färber static target_ulong helper_sdiv_common(CPUSPARCState *env, target_ulong a, 1077a5e4488SBlue Swirl target_ulong b, int cc) 1087a5e4488SBlue Swirl { 1097a5e4488SBlue Swirl int overflow = 0; 1107a5e4488SBlue Swirl int64_t x0; 1117a5e4488SBlue Swirl int32_t x1; 1127a5e4488SBlue Swirl 1137a5e4488SBlue Swirl x0 = (a & 0xffffffff) | ((int64_t) (env->y) << 32); 1147a5e4488SBlue Swirl x1 = (b & 0xffffffff); 1157a5e4488SBlue Swirl 1167a5e4488SBlue Swirl if (x1 == 0) { 117c28ae41eSRichard Henderson cpu_restore_state2(env, GETPC()); 1187a5e4488SBlue Swirl helper_raise_exception(env, TT_DIV_ZERO); 1197a5e4488SBlue Swirl } 1207a5e4488SBlue Swirl 1217a5e4488SBlue Swirl x0 = x0 / x1; 1227a5e4488SBlue Swirl if ((int32_t) x0 != x0) { 1237a5e4488SBlue Swirl x0 = x0 < 0 ? 0x80000000 : 0x7fffffff; 1247a5e4488SBlue Swirl overflow = 1; 1257a5e4488SBlue Swirl } 1267a5e4488SBlue Swirl 1277a5e4488SBlue Swirl if (cc) { 1287a5e4488SBlue Swirl env->cc_dst = x0; 1297a5e4488SBlue Swirl env->cc_src2 = overflow; 1307a5e4488SBlue Swirl env->cc_op = CC_OP_DIV; 1317a5e4488SBlue Swirl } 1327a5e4488SBlue Swirl return x0; 1337a5e4488SBlue Swirl } 1347a5e4488SBlue Swirl 135c5f9864eSAndreas Färber target_ulong helper_sdiv(CPUSPARCState *env, target_ulong a, target_ulong b) 1367a5e4488SBlue Swirl { 1377a5e4488SBlue Swirl return helper_sdiv_common(env, a, b, 0); 1387a5e4488SBlue Swirl } 1397a5e4488SBlue Swirl 140c5f9864eSAndreas Färber target_ulong helper_sdiv_cc(CPUSPARCState *env, target_ulong a, target_ulong b) 1417a5e4488SBlue Swirl { 1427a5e4488SBlue Swirl return helper_sdiv_common(env, a, b, 1); 1437a5e4488SBlue Swirl } 144c28ae41eSRichard Henderson 145c28ae41eSRichard Henderson #ifdef TARGET_SPARC64 146c28ae41eSRichard Henderson int64_t helper_sdivx(CPUSPARCState *env, int64_t a, int64_t b) 147c28ae41eSRichard Henderson { 148c28ae41eSRichard Henderson if (b == 0) { 149c28ae41eSRichard Henderson /* Raise divide by zero trap. */ 150c28ae41eSRichard Henderson cpu_restore_state2(env, GETPC()); 151c28ae41eSRichard Henderson helper_raise_exception(env, TT_DIV_ZERO); 152c28ae41eSRichard Henderson } else if (b == -1) { 153c28ae41eSRichard Henderson /* Avoid overflow trap with i386 divide insn. */ 154c28ae41eSRichard Henderson return -a; 155c28ae41eSRichard Henderson } else { 156c28ae41eSRichard Henderson return a / b; 157c28ae41eSRichard Henderson } 158c28ae41eSRichard Henderson } 159c28ae41eSRichard Henderson 160c28ae41eSRichard Henderson uint64_t helper_udivx(CPUSPARCState *env, uint64_t a, uint64_t b) 161c28ae41eSRichard Henderson { 162c28ae41eSRichard Henderson if (b == 0) { 163c28ae41eSRichard Henderson /* Raise divide by zero trap. */ 164c28ae41eSRichard Henderson cpu_restore_state2(env, GETPC()); 165c28ae41eSRichard Henderson helper_raise_exception(env, TT_DIV_ZERO); 166c28ae41eSRichard Henderson } 167c28ae41eSRichard Henderson return a / b; 168c28ae41eSRichard Henderson } 169c28ae41eSRichard Henderson #endif 170a2ea4aa9SRichard Henderson 171a2ea4aa9SRichard Henderson target_ulong helper_taddcctv(CPUSPARCState *env, target_ulong src1, 172a2ea4aa9SRichard Henderson target_ulong src2) 173a2ea4aa9SRichard Henderson { 174a2ea4aa9SRichard Henderson target_ulong dst; 175a2ea4aa9SRichard Henderson 176a2ea4aa9SRichard Henderson /* Tag overflow occurs if either input has bits 0 or 1 set. */ 177a2ea4aa9SRichard Henderson if ((src1 | src2) & 3) { 178a2ea4aa9SRichard Henderson goto tag_overflow; 179a2ea4aa9SRichard Henderson } 180a2ea4aa9SRichard Henderson 181a2ea4aa9SRichard Henderson dst = src1 + src2; 182a2ea4aa9SRichard Henderson 183a2ea4aa9SRichard Henderson /* Tag overflow occurs if the addition overflows. */ 184a2ea4aa9SRichard Henderson if (~(src1 ^ src2) & (src1 ^ dst) & (1u << 31)) { 185a2ea4aa9SRichard Henderson goto tag_overflow; 186a2ea4aa9SRichard Henderson } 187a2ea4aa9SRichard Henderson 188a2ea4aa9SRichard Henderson /* Only modify the CC after any exceptions have been generated. */ 189a2ea4aa9SRichard Henderson env->cc_op = CC_OP_TADDTV; 190a2ea4aa9SRichard Henderson env->cc_src = src1; 191a2ea4aa9SRichard Henderson env->cc_src2 = src2; 192a2ea4aa9SRichard Henderson env->cc_dst = dst; 193a2ea4aa9SRichard Henderson return dst; 194a2ea4aa9SRichard Henderson 195a2ea4aa9SRichard Henderson tag_overflow: 196a2ea4aa9SRichard Henderson cpu_restore_state2(env, GETPC()); 197a2ea4aa9SRichard Henderson helper_raise_exception(env, TT_TOVF); 198a2ea4aa9SRichard Henderson } 199a2ea4aa9SRichard Henderson 200a2ea4aa9SRichard Henderson target_ulong helper_tsubcctv(CPUSPARCState *env, target_ulong src1, 201a2ea4aa9SRichard Henderson target_ulong src2) 202a2ea4aa9SRichard Henderson { 203a2ea4aa9SRichard Henderson target_ulong dst; 204a2ea4aa9SRichard Henderson 205a2ea4aa9SRichard Henderson /* Tag overflow occurs if either input has bits 0 or 1 set. */ 206a2ea4aa9SRichard Henderson if ((src1 | src2) & 3) { 207a2ea4aa9SRichard Henderson goto tag_overflow; 208a2ea4aa9SRichard Henderson } 209a2ea4aa9SRichard Henderson 210a2ea4aa9SRichard Henderson dst = src1 - src2; 211a2ea4aa9SRichard Henderson 212a2ea4aa9SRichard Henderson /* Tag overflow occurs if the subtraction overflows. */ 213a2ea4aa9SRichard Henderson if ((src1 ^ src2) & (src1 ^ dst) & (1u << 31)) { 214a2ea4aa9SRichard Henderson goto tag_overflow; 215a2ea4aa9SRichard Henderson } 216a2ea4aa9SRichard Henderson 217a2ea4aa9SRichard Henderson /* Only modify the CC after any exceptions have been generated. */ 218a2ea4aa9SRichard Henderson env->cc_op = CC_OP_TSUBTV; 219a2ea4aa9SRichard Henderson env->cc_src = src1; 220a2ea4aa9SRichard Henderson env->cc_src2 = src2; 221a2ea4aa9SRichard Henderson env->cc_dst = dst; 222a2ea4aa9SRichard Henderson return dst; 223a2ea4aa9SRichard Henderson 224a2ea4aa9SRichard Henderson tag_overflow: 225a2ea4aa9SRichard Henderson cpu_restore_state2(env, GETPC()); 226a2ea4aa9SRichard Henderson helper_raise_exception(env, TT_TOVF); 227a2ea4aa9SRichard Henderson } 228