1fcf5ef2aSThomas Huth /* 2fcf5ef2aSThomas Huth * PowerPC memory access emulation helpers for QEMU. 3fcf5ef2aSThomas Huth * 4fcf5ef2aSThomas Huth * Copyright (c) 2003-2007 Jocelyn Mayer 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 96bd039cdSChetan Pant * version 2.1 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 */ 19db725815SMarkus Armbruster 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 "helper_regs.h" 26fcf5ef2aSThomas Huth #include "exec/cpu_ldst.h" 276914bc4fSNikunj A Dadhania #include "internal.h" 28f34ec0f6SRichard Henderson #include "qemu/atomic128.h" 29fcf5ef2aSThomas Huth 305a2c8b9eSDavid Gibson /* #define DEBUG_OP */ 31fcf5ef2aSThomas Huth 32fcf5ef2aSThomas Huth static inline bool needs_byteswap(const CPUPPCState *env) 33fcf5ef2aSThomas Huth { 34ee3eb3a7SMarc-André Lureau #if TARGET_BIG_ENDIAN 351922322cSVíctor Colombo return FIELD_EX64(env->msr, MSR, LE); 36fcf5ef2aSThomas Huth #else 371922322cSVíctor Colombo return !FIELD_EX64(env->msr, MSR, LE); 38fcf5ef2aSThomas Huth #endif 39fcf5ef2aSThomas Huth } 40fcf5ef2aSThomas Huth 41fcf5ef2aSThomas Huth /*****************************************************************************/ 42fcf5ef2aSThomas Huth /* Memory load and stores */ 43fcf5ef2aSThomas Huth 44fcf5ef2aSThomas Huth static inline target_ulong addr_add(CPUPPCState *env, target_ulong addr, 45fcf5ef2aSThomas Huth target_long arg) 46fcf5ef2aSThomas Huth { 47fcf5ef2aSThomas Huth #if defined(TARGET_PPC64) 48fcf5ef2aSThomas Huth if (!msr_is_64bit(env, env->msr)) { 49fcf5ef2aSThomas Huth return (uint32_t)(addr + arg); 50fcf5ef2aSThomas Huth } else 51fcf5ef2aSThomas Huth #endif 52fcf5ef2aSThomas Huth { 53fcf5ef2aSThomas Huth return addr + arg; 54fcf5ef2aSThomas Huth } 55fcf5ef2aSThomas Huth } 56fcf5ef2aSThomas Huth 57bb99b391SRichard Henderson static void *probe_contiguous(CPUPPCState *env, target_ulong addr, uint32_t nb, 58bb99b391SRichard Henderson MMUAccessType access_type, int mmu_idx, 59bb99b391SRichard Henderson uintptr_t raddr) 60bb99b391SRichard Henderson { 61bb99b391SRichard Henderson void *host1, *host2; 62bb99b391SRichard Henderson uint32_t nb_pg1, nb_pg2; 63bb99b391SRichard Henderson 64bb99b391SRichard Henderson nb_pg1 = -(addr | TARGET_PAGE_MASK); 65bb99b391SRichard Henderson if (likely(nb <= nb_pg1)) { 66bb99b391SRichard Henderson /* The entire operation is on a single page. */ 67bb99b391SRichard Henderson return probe_access(env, addr, nb, access_type, mmu_idx, raddr); 68bb99b391SRichard Henderson } 69bb99b391SRichard Henderson 70bb99b391SRichard Henderson /* The operation spans two pages. */ 71bb99b391SRichard Henderson nb_pg2 = nb - nb_pg1; 72bb99b391SRichard Henderson host1 = probe_access(env, addr, nb_pg1, access_type, mmu_idx, raddr); 73bb99b391SRichard Henderson addr = addr_add(env, addr, nb_pg1); 74bb99b391SRichard Henderson host2 = probe_access(env, addr, nb_pg2, access_type, mmu_idx, raddr); 75bb99b391SRichard Henderson 76bb99b391SRichard Henderson /* If the two host pages are contiguous, optimize. */ 77bb99b391SRichard Henderson if (host2 == host1 + nb_pg1) { 78bb99b391SRichard Henderson return host1; 79bb99b391SRichard Henderson } 80bb99b391SRichard Henderson return NULL; 81bb99b391SRichard Henderson } 82bb99b391SRichard Henderson 83fcf5ef2aSThomas Huth void helper_lmw(CPUPPCState *env, target_ulong addr, uint32_t reg) 84fcf5ef2aSThomas Huth { 852ca2ef49SRichard Henderson uintptr_t raddr = GETPC(); 86*fb00f730SRichard Henderson int mmu_idx = ppc_env_mmu_index(env, false); 872ca2ef49SRichard Henderson void *host = probe_contiguous(env, addr, (32 - reg) * 4, 882ca2ef49SRichard Henderson MMU_DATA_LOAD, mmu_idx, raddr); 892ca2ef49SRichard Henderson 902ca2ef49SRichard Henderson if (likely(host)) { 912ca2ef49SRichard Henderson /* Fast path -- the entire operation is in RAM at host. */ 92fcf5ef2aSThomas Huth for (; reg < 32; reg++) { 932ca2ef49SRichard Henderson env->gpr[reg] = (uint32_t)ldl_be_p(host); 942ca2ef49SRichard Henderson host += 4; 95fcf5ef2aSThomas Huth } 962ca2ef49SRichard Henderson } else { 972ca2ef49SRichard Henderson /* Slow path -- at least some of the operation requires i/o. */ 982ca2ef49SRichard Henderson for (; reg < 32; reg++) { 992ca2ef49SRichard Henderson env->gpr[reg] = cpu_ldl_mmuidx_ra(env, addr, mmu_idx, raddr); 100fcf5ef2aSThomas Huth addr = addr_add(env, addr, 4); 101fcf5ef2aSThomas Huth } 102fcf5ef2aSThomas Huth } 1032ca2ef49SRichard Henderson } 104fcf5ef2aSThomas Huth 105fcf5ef2aSThomas Huth void helper_stmw(CPUPPCState *env, target_ulong addr, uint32_t reg) 106fcf5ef2aSThomas Huth { 1072ca2ef49SRichard Henderson uintptr_t raddr = GETPC(); 108*fb00f730SRichard Henderson int mmu_idx = ppc_env_mmu_index(env, false); 1092ca2ef49SRichard Henderson void *host = probe_contiguous(env, addr, (32 - reg) * 4, 1102ca2ef49SRichard Henderson MMU_DATA_STORE, mmu_idx, raddr); 1112ca2ef49SRichard Henderson 1122ca2ef49SRichard Henderson if (likely(host)) { 1132ca2ef49SRichard Henderson /* Fast path -- the entire operation is in RAM at host. */ 114fcf5ef2aSThomas Huth for (; reg < 32; reg++) { 1152ca2ef49SRichard Henderson stl_be_p(host, env->gpr[reg]); 1162ca2ef49SRichard Henderson host += 4; 117fcf5ef2aSThomas Huth } 1182ca2ef49SRichard Henderson } else { 1192ca2ef49SRichard Henderson /* Slow path -- at least some of the operation requires i/o. */ 1202ca2ef49SRichard Henderson for (; reg < 32; reg++) { 1212ca2ef49SRichard Henderson cpu_stl_mmuidx_ra(env, addr, env->gpr[reg], mmu_idx, raddr); 122fcf5ef2aSThomas Huth addr = addr_add(env, addr, 4); 123fcf5ef2aSThomas Huth } 124fcf5ef2aSThomas Huth } 1252ca2ef49SRichard Henderson } 126fcf5ef2aSThomas Huth 127fcf5ef2aSThomas Huth static void do_lsw(CPUPPCState *env, target_ulong addr, uint32_t nb, 128fcf5ef2aSThomas Huth uint32_t reg, uintptr_t raddr) 129fcf5ef2aSThomas Huth { 130bb99b391SRichard Henderson int mmu_idx; 131bb99b391SRichard Henderson void *host; 132bb99b391SRichard Henderson uint32_t val; 133fcf5ef2aSThomas Huth 134bb99b391SRichard Henderson if (unlikely(nb == 0)) { 135bb99b391SRichard Henderson return; 136bb99b391SRichard Henderson } 137bb99b391SRichard Henderson 138*fb00f730SRichard Henderson mmu_idx = ppc_env_mmu_index(env, false); 139bb99b391SRichard Henderson host = probe_contiguous(env, addr, nb, MMU_DATA_LOAD, mmu_idx, raddr); 140bb99b391SRichard Henderson 141bb99b391SRichard Henderson if (likely(host)) { 142bb99b391SRichard Henderson /* Fast path -- the entire operation is in RAM at host. */ 143fcf5ef2aSThomas Huth for (; nb > 3; nb -= 4) { 144bb99b391SRichard Henderson env->gpr[reg] = (uint32_t)ldl_be_p(host); 145bb99b391SRichard Henderson reg = (reg + 1) % 32; 146bb99b391SRichard Henderson host += 4; 147bb99b391SRichard Henderson } 148bb99b391SRichard Henderson switch (nb) { 149bb99b391SRichard Henderson default: 150bb99b391SRichard Henderson return; 151bb99b391SRichard Henderson case 1: 152bb99b391SRichard Henderson val = ldub_p(host) << 24; 153bb99b391SRichard Henderson break; 154bb99b391SRichard Henderson case 2: 155bb99b391SRichard Henderson val = lduw_be_p(host) << 16; 156bb99b391SRichard Henderson break; 157bb99b391SRichard Henderson case 3: 158bb99b391SRichard Henderson val = (lduw_be_p(host) << 16) | (ldub_p(host + 2) << 8); 159bb99b391SRichard Henderson break; 160bb99b391SRichard Henderson } 161bb99b391SRichard Henderson } else { 162bb99b391SRichard Henderson /* Slow path -- at least some of the operation requires i/o. */ 163bb99b391SRichard Henderson for (; nb > 3; nb -= 4) { 164bb99b391SRichard Henderson env->gpr[reg] = cpu_ldl_mmuidx_ra(env, addr, mmu_idx, raddr); 165fcf5ef2aSThomas Huth reg = (reg + 1) % 32; 166fcf5ef2aSThomas Huth addr = addr_add(env, addr, 4); 167fcf5ef2aSThomas Huth } 168bb99b391SRichard Henderson switch (nb) { 169bb99b391SRichard Henderson default: 170bb99b391SRichard Henderson return; 171bb99b391SRichard Henderson case 1: 172bb99b391SRichard Henderson val = cpu_ldub_mmuidx_ra(env, addr, mmu_idx, raddr) << 24; 173bb99b391SRichard Henderson break; 174bb99b391SRichard Henderson case 2: 175bb99b391SRichard Henderson val = cpu_lduw_mmuidx_ra(env, addr, mmu_idx, raddr) << 16; 176bb99b391SRichard Henderson break; 177bb99b391SRichard Henderson case 3: 178bb99b391SRichard Henderson val = cpu_lduw_mmuidx_ra(env, addr, mmu_idx, raddr) << 16; 179bb99b391SRichard Henderson addr = addr_add(env, addr, 2); 180bb99b391SRichard Henderson val |= cpu_ldub_mmuidx_ra(env, addr, mmu_idx, raddr) << 8; 181bb99b391SRichard Henderson break; 182fcf5ef2aSThomas Huth } 183fcf5ef2aSThomas Huth } 184bb99b391SRichard Henderson env->gpr[reg] = val; 185fcf5ef2aSThomas Huth } 186fcf5ef2aSThomas Huth 187bb99b391SRichard Henderson void helper_lsw(CPUPPCState *env, target_ulong addr, 188bb99b391SRichard Henderson uint32_t nb, uint32_t reg) 189fcf5ef2aSThomas Huth { 190fcf5ef2aSThomas Huth do_lsw(env, addr, nb, reg, GETPC()); 191fcf5ef2aSThomas Huth } 192fcf5ef2aSThomas Huth 1935a2c8b9eSDavid Gibson /* 1945a2c8b9eSDavid Gibson * PPC32 specification says we must generate an exception if rA is in 1955a2c8b9eSDavid Gibson * the range of registers to be loaded. In an other hand, IBM says 1965a2c8b9eSDavid Gibson * this is valid, but rA won't be loaded. For now, I'll follow the 1975a2c8b9eSDavid Gibson * spec... 198fcf5ef2aSThomas Huth */ 199fcf5ef2aSThomas Huth void helper_lswx(CPUPPCState *env, target_ulong addr, uint32_t reg, 200fcf5ef2aSThomas Huth uint32_t ra, uint32_t rb) 201fcf5ef2aSThomas Huth { 202fcf5ef2aSThomas Huth if (likely(xer_bc != 0)) { 203f0704d78SMarc-André Lureau int num_used_regs = DIV_ROUND_UP(xer_bc, 4); 204fcf5ef2aSThomas Huth if (unlikely((ra != 0 && lsw_reg_in_range(reg, num_used_regs, ra)) || 205fcf5ef2aSThomas Huth lsw_reg_in_range(reg, num_used_regs, rb))) { 206fcf5ef2aSThomas Huth raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM, 207fcf5ef2aSThomas Huth POWERPC_EXCP_INVAL | 208fcf5ef2aSThomas Huth POWERPC_EXCP_INVAL_LSWX, GETPC()); 209fcf5ef2aSThomas Huth } else { 210fcf5ef2aSThomas Huth do_lsw(env, addr, xer_bc, reg, GETPC()); 211fcf5ef2aSThomas Huth } 212fcf5ef2aSThomas Huth } 213fcf5ef2aSThomas Huth } 214fcf5ef2aSThomas Huth 215fcf5ef2aSThomas Huth void helper_stsw(CPUPPCState *env, target_ulong addr, uint32_t nb, 216fcf5ef2aSThomas Huth uint32_t reg) 217fcf5ef2aSThomas Huth { 218bb99b391SRichard Henderson uintptr_t raddr = GETPC(); 219bb99b391SRichard Henderson int mmu_idx; 220bb99b391SRichard Henderson void *host; 221bb99b391SRichard Henderson uint32_t val; 222fcf5ef2aSThomas Huth 223bb99b391SRichard Henderson if (unlikely(nb == 0)) { 224bb99b391SRichard Henderson return; 225bb99b391SRichard Henderson } 226bb99b391SRichard Henderson 227*fb00f730SRichard Henderson mmu_idx = ppc_env_mmu_index(env, false); 228bb99b391SRichard Henderson host = probe_contiguous(env, addr, nb, MMU_DATA_STORE, mmu_idx, raddr); 229bb99b391SRichard Henderson 230bb99b391SRichard Henderson if (likely(host)) { 231bb99b391SRichard Henderson /* Fast path -- the entire operation is in RAM at host. */ 232fcf5ef2aSThomas Huth for (; nb > 3; nb -= 4) { 233bb99b391SRichard Henderson stl_be_p(host, env->gpr[reg]); 234bb99b391SRichard Henderson reg = (reg + 1) % 32; 235bb99b391SRichard Henderson host += 4; 236bb99b391SRichard Henderson } 237bb99b391SRichard Henderson val = env->gpr[reg]; 238bb99b391SRichard Henderson switch (nb) { 239bb99b391SRichard Henderson case 1: 240bb99b391SRichard Henderson stb_p(host, val >> 24); 241bb99b391SRichard Henderson break; 242bb99b391SRichard Henderson case 2: 243bb99b391SRichard Henderson stw_be_p(host, val >> 16); 244bb99b391SRichard Henderson break; 245bb99b391SRichard Henderson case 3: 246bb99b391SRichard Henderson stw_be_p(host, val >> 16); 247bb99b391SRichard Henderson stb_p(host + 2, val >> 8); 248bb99b391SRichard Henderson break; 249bb99b391SRichard Henderson } 250bb99b391SRichard Henderson } else { 251bb99b391SRichard Henderson for (; nb > 3; nb -= 4) { 252bb99b391SRichard Henderson cpu_stl_mmuidx_ra(env, addr, env->gpr[reg], mmu_idx, raddr); 253fcf5ef2aSThomas Huth reg = (reg + 1) % 32; 254fcf5ef2aSThomas Huth addr = addr_add(env, addr, 4); 255fcf5ef2aSThomas Huth } 256bb99b391SRichard Henderson val = env->gpr[reg]; 257bb99b391SRichard Henderson switch (nb) { 258bb99b391SRichard Henderson case 1: 259bb99b391SRichard Henderson cpu_stb_mmuidx_ra(env, addr, val >> 24, mmu_idx, raddr); 260bb99b391SRichard Henderson break; 261bb99b391SRichard Henderson case 2: 262bb99b391SRichard Henderson cpu_stw_mmuidx_ra(env, addr, val >> 16, mmu_idx, raddr); 263bb99b391SRichard Henderson break; 264bb99b391SRichard Henderson case 3: 265bb99b391SRichard Henderson cpu_stw_mmuidx_ra(env, addr, val >> 16, mmu_idx, raddr); 266bb99b391SRichard Henderson addr = addr_add(env, addr, 2); 267bb99b391SRichard Henderson cpu_stb_mmuidx_ra(env, addr, val >> 8, mmu_idx, raddr); 268bb99b391SRichard Henderson break; 269fcf5ef2aSThomas Huth } 270fcf5ef2aSThomas Huth } 271fcf5ef2aSThomas Huth } 272fcf5ef2aSThomas Huth 27350728199SRoman Kapl static void dcbz_common(CPUPPCState *env, target_ulong addr, 27450728199SRoman Kapl uint32_t opcode, bool epid, uintptr_t retaddr) 275fcf5ef2aSThomas Huth { 276fcf5ef2aSThomas Huth target_ulong mask, dcbz_size = env->dcache_line_size; 277fcf5ef2aSThomas Huth uint32_t i; 278fcf5ef2aSThomas Huth void *haddr; 279*fb00f730SRichard Henderson int mmu_idx = epid ? PPC_TLB_EPID_STORE : ppc_env_mmu_index(env, false); 280fcf5ef2aSThomas Huth 281fcf5ef2aSThomas Huth #if defined(TARGET_PPC64) 282fcf5ef2aSThomas Huth /* Check for dcbz vs dcbzl on 970 */ 283fcf5ef2aSThomas Huth if (env->excp_model == POWERPC_EXCP_970 && 284fcf5ef2aSThomas Huth !(opcode & 0x00200000) && ((env->spr[SPR_970_HID5] >> 7) & 0x3) == 1) { 285fcf5ef2aSThomas Huth dcbz_size = 32; 286fcf5ef2aSThomas Huth } 287fcf5ef2aSThomas Huth #endif 288fcf5ef2aSThomas Huth 289fcf5ef2aSThomas Huth /* Align address */ 290fcf5ef2aSThomas Huth mask = ~(dcbz_size - 1); 291fcf5ef2aSThomas Huth addr &= mask; 292fcf5ef2aSThomas Huth 293fcf5ef2aSThomas Huth /* Check reservation */ 2941cbddf6dSRichard Henderson if ((env->reserve_addr & mask) == addr) { 295fcf5ef2aSThomas Huth env->reserve_addr = (target_ulong)-1ULL; 296fcf5ef2aSThomas Huth } 297fcf5ef2aSThomas Huth 298fcf5ef2aSThomas Huth /* Try fast path translate */ 2994dcf078fSRichard Henderson haddr = probe_write(env, addr, dcbz_size, mmu_idx, retaddr); 300fcf5ef2aSThomas Huth if (haddr) { 301fcf5ef2aSThomas Huth memset(haddr, 0, dcbz_size); 302fcf5ef2aSThomas Huth } else { 303fcf5ef2aSThomas Huth /* Slow path */ 304fcf5ef2aSThomas Huth for (i = 0; i < dcbz_size; i += 8) { 3055a376e4fSRichard Henderson cpu_stq_mmuidx_ra(env, addr + i, 0, mmu_idx, retaddr); 306fcf5ef2aSThomas Huth } 307fcf5ef2aSThomas Huth } 30850728199SRoman Kapl } 30950728199SRoman Kapl 31050728199SRoman Kapl void helper_dcbz(CPUPPCState *env, target_ulong addr, uint32_t opcode) 31150728199SRoman Kapl { 31250728199SRoman Kapl dcbz_common(env, addr, opcode, false, GETPC()); 31350728199SRoman Kapl } 31450728199SRoman Kapl 31550728199SRoman Kapl void helper_dcbzep(CPUPPCState *env, target_ulong addr, uint32_t opcode) 31650728199SRoman Kapl { 31750728199SRoman Kapl dcbz_common(env, addr, opcode, true, GETPC()); 31850728199SRoman Kapl } 319fcf5ef2aSThomas Huth 320fcf5ef2aSThomas Huth void helper_icbi(CPUPPCState *env, target_ulong addr) 321fcf5ef2aSThomas Huth { 322fcf5ef2aSThomas Huth addr &= ~(env->dcache_line_size - 1); 3235a2c8b9eSDavid Gibson /* 3245a2c8b9eSDavid Gibson * Invalidate one cache line : 325fcf5ef2aSThomas Huth * PowerPC specification says this is to be treated like a load 326fcf5ef2aSThomas Huth * (not a fetch) by the MMU. To be sure it will be so, 327fcf5ef2aSThomas Huth * do the load "by hand". 328fcf5ef2aSThomas Huth */ 329fcf5ef2aSThomas Huth cpu_ldl_data_ra(env, addr, GETPC()); 330fcf5ef2aSThomas Huth } 331fcf5ef2aSThomas Huth 33250728199SRoman Kapl void helper_icbiep(CPUPPCState *env, target_ulong addr) 33350728199SRoman Kapl { 33450728199SRoman Kapl #if !defined(CONFIG_USER_ONLY) 33550728199SRoman Kapl /* See comments above */ 33650728199SRoman Kapl addr &= ~(env->dcache_line_size - 1); 3375a376e4fSRichard Henderson cpu_ldl_mmuidx_ra(env, addr, PPC_TLB_EPID_LOAD, GETPC()); 33850728199SRoman Kapl #endif 33950728199SRoman Kapl } 34050728199SRoman Kapl 341fcf5ef2aSThomas Huth /* XXX: to be tested */ 342fcf5ef2aSThomas Huth target_ulong helper_lscbx(CPUPPCState *env, target_ulong addr, uint32_t reg, 343fcf5ef2aSThomas Huth uint32_t ra, uint32_t rb) 344fcf5ef2aSThomas Huth { 345fcf5ef2aSThomas Huth int i, c, d; 346fcf5ef2aSThomas Huth 347fcf5ef2aSThomas Huth d = 24; 348fcf5ef2aSThomas Huth for (i = 0; i < xer_bc; i++) { 349fcf5ef2aSThomas Huth c = cpu_ldub_data_ra(env, addr, GETPC()); 350fcf5ef2aSThomas Huth addr = addr_add(env, addr, 1); 351fcf5ef2aSThomas Huth /* ra (if not 0) and rb are never modified */ 352fcf5ef2aSThomas Huth if (likely(reg != rb && (ra == 0 || reg != ra))) { 353fcf5ef2aSThomas Huth env->gpr[reg] = (env->gpr[reg] & ~(0xFF << d)) | (c << d); 354fcf5ef2aSThomas Huth } 355fcf5ef2aSThomas Huth if (unlikely(c == xer_cmp)) { 356fcf5ef2aSThomas Huth break; 357fcf5ef2aSThomas Huth } 358fcf5ef2aSThomas Huth if (likely(d != 0)) { 359fcf5ef2aSThomas Huth d -= 8; 360fcf5ef2aSThomas Huth } else { 361fcf5ef2aSThomas Huth d = 24; 362fcf5ef2aSThomas Huth reg++; 363fcf5ef2aSThomas Huth reg = reg & 0x1F; 364fcf5ef2aSThomas Huth } 365fcf5ef2aSThomas Huth } 366fcf5ef2aSThomas Huth return i; 367fcf5ef2aSThomas Huth } 368fcf5ef2aSThomas Huth 369fcf5ef2aSThomas Huth /*****************************************************************************/ 370fcf5ef2aSThomas Huth /* Altivec extension helpers */ 371e03b5686SMarc-André Lureau #if HOST_BIG_ENDIAN 372fcf5ef2aSThomas Huth #define HI_IDX 0 373fcf5ef2aSThomas Huth #define LO_IDX 1 374fcf5ef2aSThomas Huth #else 375fcf5ef2aSThomas Huth #define HI_IDX 1 376fcf5ef2aSThomas Huth #define LO_IDX 0 377fcf5ef2aSThomas Huth #endif 378fcf5ef2aSThomas Huth 3795a2c8b9eSDavid Gibson /* 3801922322cSVíctor Colombo * We use MSR_LE to determine index ordering in a vector. However, 3811922322cSVíctor Colombo * byteswapping is not simply controlled by MSR_LE. We also need to 3825a2c8b9eSDavid Gibson * take into account endianness of the target. This is done for the 3835a2c8b9eSDavid Gibson * little-endian PPC64 user-mode target. 3845a2c8b9eSDavid Gibson */ 385fcf5ef2aSThomas Huth 386fcf5ef2aSThomas Huth #define LVE(name, access, swap, element) \ 387fcf5ef2aSThomas Huth void helper_##name(CPUPPCState *env, ppc_avr_t *r, \ 388fcf5ef2aSThomas Huth target_ulong addr) \ 389fcf5ef2aSThomas Huth { \ 390fcf5ef2aSThomas Huth size_t n_elems = ARRAY_SIZE(r->element); \ 391fcf5ef2aSThomas Huth int adjust = HI_IDX * (n_elems - 1); \ 392fcf5ef2aSThomas Huth int sh = sizeof(r->element[0]) >> 1; \ 393fcf5ef2aSThomas Huth int index = (addr & 0xf) >> sh; \ 3941922322cSVíctor Colombo if (FIELD_EX64(env->msr, MSR, LE)) { \ 395fcf5ef2aSThomas Huth index = n_elems - index - 1; \ 396fcf5ef2aSThomas Huth } \ 397fcf5ef2aSThomas Huth \ 398fcf5ef2aSThomas Huth if (needs_byteswap(env)) { \ 399fcf5ef2aSThomas Huth r->element[LO_IDX ? index : (adjust - index)] = \ 400fcf5ef2aSThomas Huth swap(access(env, addr, GETPC())); \ 401fcf5ef2aSThomas Huth } else { \ 402fcf5ef2aSThomas Huth r->element[LO_IDX ? index : (adjust - index)] = \ 403fcf5ef2aSThomas Huth access(env, addr, GETPC()); \ 404fcf5ef2aSThomas Huth } \ 405fcf5ef2aSThomas Huth } 406fcf5ef2aSThomas Huth #define I(x) (x) 407fcf5ef2aSThomas Huth LVE(lvebx, cpu_ldub_data_ra, I, u8) 408fcf5ef2aSThomas Huth LVE(lvehx, cpu_lduw_data_ra, bswap16, u16) 409fcf5ef2aSThomas Huth LVE(lvewx, cpu_ldl_data_ra, bswap32, u32) 410fcf5ef2aSThomas Huth #undef I 411fcf5ef2aSThomas Huth #undef LVE 412fcf5ef2aSThomas Huth 413fcf5ef2aSThomas Huth #define STVE(name, access, swap, element) \ 414fcf5ef2aSThomas Huth void helper_##name(CPUPPCState *env, ppc_avr_t *r, \ 415fcf5ef2aSThomas Huth target_ulong addr) \ 416fcf5ef2aSThomas Huth { \ 417fcf5ef2aSThomas Huth size_t n_elems = ARRAY_SIZE(r->element); \ 418fcf5ef2aSThomas Huth int adjust = HI_IDX * (n_elems - 1); \ 419fcf5ef2aSThomas Huth int sh = sizeof(r->element[0]) >> 1; \ 420fcf5ef2aSThomas Huth int index = (addr & 0xf) >> sh; \ 4211922322cSVíctor Colombo if (FIELD_EX64(env->msr, MSR, LE)) { \ 422fcf5ef2aSThomas Huth index = n_elems - index - 1; \ 423fcf5ef2aSThomas Huth } \ 424fcf5ef2aSThomas Huth \ 425fcf5ef2aSThomas Huth if (needs_byteswap(env)) { \ 426fcf5ef2aSThomas Huth access(env, addr, swap(r->element[LO_IDX ? index : \ 427fcf5ef2aSThomas Huth (adjust - index)]), \ 428fcf5ef2aSThomas Huth GETPC()); \ 429fcf5ef2aSThomas Huth } else { \ 430fcf5ef2aSThomas Huth access(env, addr, r->element[LO_IDX ? index : \ 431fcf5ef2aSThomas Huth (adjust - index)], GETPC()); \ 432fcf5ef2aSThomas Huth } \ 433fcf5ef2aSThomas Huth } 434fcf5ef2aSThomas Huth #define I(x) (x) 435fcf5ef2aSThomas Huth STVE(stvebx, cpu_stb_data_ra, I, u8) 436fcf5ef2aSThomas Huth STVE(stvehx, cpu_stw_data_ra, bswap16, u16) 437fcf5ef2aSThomas Huth STVE(stvewx, cpu_stl_data_ra, bswap32, u32) 438fcf5ef2aSThomas Huth #undef I 439fcf5ef2aSThomas Huth #undef LVE 440fcf5ef2aSThomas Huth 4416914bc4fSNikunj A Dadhania #ifdef TARGET_PPC64 4426914bc4fSNikunj A Dadhania #define GET_NB(rb) ((rb >> 56) & 0xFF) 4436914bc4fSNikunj A Dadhania 4446914bc4fSNikunj A Dadhania #define VSX_LXVL(name, lj) \ 4456914bc4fSNikunj A Dadhania void helper_##name(CPUPPCState *env, target_ulong addr, \ 4462aba168eSMark Cave-Ayland ppc_vsr_t *xt, target_ulong rb) \ 4476914bc4fSNikunj A Dadhania { \ 4482a175830SMark Cave-Ayland ppc_vsr_t t; \ 4496914bc4fSNikunj A Dadhania uint64_t nb = GET_NB(rb); \ 4502a175830SMark Cave-Ayland int i; \ 4516914bc4fSNikunj A Dadhania \ 4522a175830SMark Cave-Ayland t.s128 = int128_zero(); \ 4536914bc4fSNikunj A Dadhania if (nb) { \ 4546914bc4fSNikunj A Dadhania nb = (nb >= 16) ? 16 : nb; \ 4551922322cSVíctor Colombo if (FIELD_EX64(env->msr, MSR, LE) && !lj) { \ 4566914bc4fSNikunj A Dadhania for (i = 16; i > 16 - nb; i--) { \ 4572a175830SMark Cave-Ayland t.VsrB(i - 1) = cpu_ldub_data_ra(env, addr, GETPC()); \ 4586914bc4fSNikunj A Dadhania addr = addr_add(env, addr, 1); \ 4596914bc4fSNikunj A Dadhania } \ 4606914bc4fSNikunj A Dadhania } else { \ 4616914bc4fSNikunj A Dadhania for (i = 0; i < nb; i++) { \ 4622a175830SMark Cave-Ayland t.VsrB(i) = cpu_ldub_data_ra(env, addr, GETPC()); \ 4636914bc4fSNikunj A Dadhania addr = addr_add(env, addr, 1); \ 4646914bc4fSNikunj A Dadhania } \ 4656914bc4fSNikunj A Dadhania } \ 4666914bc4fSNikunj A Dadhania } \ 4672a175830SMark Cave-Ayland *xt = t; \ 4686914bc4fSNikunj A Dadhania } 4696914bc4fSNikunj A Dadhania 4706914bc4fSNikunj A Dadhania VSX_LXVL(lxvl, 0) 471176e44e7SNikunj A Dadhania VSX_LXVL(lxvll, 1) 4726914bc4fSNikunj A Dadhania #undef VSX_LXVL 473681c2478SNikunj A Dadhania 474681c2478SNikunj A Dadhania #define VSX_STXVL(name, lj) \ 475681c2478SNikunj A Dadhania void helper_##name(CPUPPCState *env, target_ulong addr, \ 4762aba168eSMark Cave-Ayland ppc_vsr_t *xt, target_ulong rb) \ 477681c2478SNikunj A Dadhania { \ 478681c2478SNikunj A Dadhania target_ulong nb = GET_NB(rb); \ 4792a175830SMark Cave-Ayland int i; \ 480681c2478SNikunj A Dadhania \ 481681c2478SNikunj A Dadhania if (!nb) { \ 482681c2478SNikunj A Dadhania return; \ 483681c2478SNikunj A Dadhania } \ 4842a175830SMark Cave-Ayland \ 485681c2478SNikunj A Dadhania nb = (nb >= 16) ? 16 : nb; \ 4861922322cSVíctor Colombo if (FIELD_EX64(env->msr, MSR, LE) && !lj) { \ 487681c2478SNikunj A Dadhania for (i = 16; i > 16 - nb; i--) { \ 4882a175830SMark Cave-Ayland cpu_stb_data_ra(env, addr, xt->VsrB(i - 1), GETPC()); \ 489681c2478SNikunj A Dadhania addr = addr_add(env, addr, 1); \ 490681c2478SNikunj A Dadhania } \ 491681c2478SNikunj A Dadhania } else { \ 492681c2478SNikunj A Dadhania for (i = 0; i < nb; i++) { \ 4932a175830SMark Cave-Ayland cpu_stb_data_ra(env, addr, xt->VsrB(i), GETPC()); \ 494681c2478SNikunj A Dadhania addr = addr_add(env, addr, 1); \ 495681c2478SNikunj A Dadhania } \ 496681c2478SNikunj A Dadhania } \ 497681c2478SNikunj A Dadhania } 498681c2478SNikunj A Dadhania 499681c2478SNikunj A Dadhania VSX_STXVL(stxvl, 0) 500e122090dSNikunj A Dadhania VSX_STXVL(stxvll, 1) 501681c2478SNikunj A Dadhania #undef VSX_STXVL 5026914bc4fSNikunj A Dadhania #undef GET_NB 5036914bc4fSNikunj A Dadhania #endif /* TARGET_PPC64 */ 5046914bc4fSNikunj A Dadhania 505fcf5ef2aSThomas Huth #undef HI_IDX 506fcf5ef2aSThomas Huth #undef LO_IDX 507fcf5ef2aSThomas Huth 508fcf5ef2aSThomas Huth void helper_tbegin(CPUPPCState *env) 509fcf5ef2aSThomas Huth { 5105a2c8b9eSDavid Gibson /* 5115a2c8b9eSDavid Gibson * As a degenerate implementation, always fail tbegin. The reason 512fcf5ef2aSThomas Huth * given is "Nesting overflow". The "persistent" bit is set, 513fcf5ef2aSThomas Huth * providing a hint to the error handler to not retry. The TFIAR 514fcf5ef2aSThomas Huth * captures the address of the failure, which is this tbegin 5155a2c8b9eSDavid Gibson * instruction. Instruction execution will continue with the next 5165a2c8b9eSDavid Gibson * instruction in memory, which is precisely what we want. 517fcf5ef2aSThomas Huth */ 518fcf5ef2aSThomas Huth 519fcf5ef2aSThomas Huth env->spr[SPR_TEXASR] = 520fcf5ef2aSThomas Huth (1ULL << TEXASR_FAILURE_PERSISTENT) | 521fcf5ef2aSThomas Huth (1ULL << TEXASR_NESTING_OVERFLOW) | 5229de754d3SVíctor Colombo (FIELD_EX64_HV(env->msr) << TEXASR_PRIVILEGE_HV) | 523d41ccf6eSVíctor Colombo (FIELD_EX64(env->msr, MSR, PR) << TEXASR_PRIVILEGE_PR) | 524fcf5ef2aSThomas Huth (1ULL << TEXASR_FAILURE_SUMMARY) | 525fcf5ef2aSThomas Huth (1ULL << TEXASR_TFIAR_EXACT); 5269de754d3SVíctor Colombo env->spr[SPR_TFIAR] = env->nip | (FIELD_EX64_HV(env->msr) << 1) | 527d41ccf6eSVíctor Colombo FIELD_EX64(env->msr, MSR, PR); 528fcf5ef2aSThomas Huth env->spr[SPR_TFHAR] = env->nip + 4; 529fcf5ef2aSThomas Huth env->crf[0] = 0xB; /* 0b1010 = transaction failure */ 530fcf5ef2aSThomas Huth } 531