xref: /openbmc/qemu/target/ppc/mem_helper.c (revision 5a376e4f40a76d8bea5426cdc7ac16c7d0a7166b)
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
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  */
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"
24db725815SMarkus Armbruster #include "qemu/main-loop.h"
25fcf5ef2aSThomas Huth #include "exec/helper-proto.h"
26fcf5ef2aSThomas Huth #include "helper_regs.h"
27fcf5ef2aSThomas Huth #include "exec/cpu_ldst.h"
2894bf2658SRichard Henderson #include "tcg.h"
296914bc4fSNikunj A Dadhania #include "internal.h"
30f34ec0f6SRichard Henderson #include "qemu/atomic128.h"
31fcf5ef2aSThomas Huth 
325a2c8b9eSDavid Gibson /* #define DEBUG_OP */
33fcf5ef2aSThomas Huth 
34fcf5ef2aSThomas Huth static inline bool needs_byteswap(const CPUPPCState *env)
35fcf5ef2aSThomas Huth {
36fcf5ef2aSThomas Huth #if defined(TARGET_WORDS_BIGENDIAN)
37fcf5ef2aSThomas Huth   return msr_le;
38fcf5ef2aSThomas Huth #else
39fcf5ef2aSThomas Huth   return !msr_le;
40fcf5ef2aSThomas Huth #endif
41fcf5ef2aSThomas Huth }
42fcf5ef2aSThomas Huth 
43fcf5ef2aSThomas Huth /*****************************************************************************/
44fcf5ef2aSThomas Huth /* Memory load and stores */
45fcf5ef2aSThomas Huth 
46fcf5ef2aSThomas Huth static inline target_ulong addr_add(CPUPPCState *env, target_ulong addr,
47fcf5ef2aSThomas Huth                                     target_long arg)
48fcf5ef2aSThomas Huth {
49fcf5ef2aSThomas Huth #if defined(TARGET_PPC64)
50fcf5ef2aSThomas Huth     if (!msr_is_64bit(env, env->msr)) {
51fcf5ef2aSThomas Huth         return (uint32_t)(addr + arg);
52fcf5ef2aSThomas Huth     } else
53fcf5ef2aSThomas Huth #endif
54fcf5ef2aSThomas Huth     {
55fcf5ef2aSThomas Huth         return addr + arg;
56fcf5ef2aSThomas Huth     }
57fcf5ef2aSThomas Huth }
58fcf5ef2aSThomas Huth 
59fcf5ef2aSThomas Huth void helper_lmw(CPUPPCState *env, target_ulong addr, uint32_t reg)
60fcf5ef2aSThomas Huth {
61fcf5ef2aSThomas Huth     for (; reg < 32; reg++) {
62fcf5ef2aSThomas Huth         if (needs_byteswap(env)) {
63fcf5ef2aSThomas Huth             env->gpr[reg] = bswap32(cpu_ldl_data_ra(env, addr, GETPC()));
64fcf5ef2aSThomas Huth         } else {
65fcf5ef2aSThomas Huth             env->gpr[reg] = cpu_ldl_data_ra(env, addr, GETPC());
66fcf5ef2aSThomas Huth         }
67fcf5ef2aSThomas Huth         addr = addr_add(env, addr, 4);
68fcf5ef2aSThomas Huth     }
69fcf5ef2aSThomas Huth }
70fcf5ef2aSThomas Huth 
71fcf5ef2aSThomas Huth void helper_stmw(CPUPPCState *env, target_ulong addr, uint32_t reg)
72fcf5ef2aSThomas Huth {
73fcf5ef2aSThomas Huth     for (; reg < 32; reg++) {
74fcf5ef2aSThomas Huth         if (needs_byteswap(env)) {
75fcf5ef2aSThomas Huth             cpu_stl_data_ra(env, addr, bswap32((uint32_t)env->gpr[reg]),
76fcf5ef2aSThomas Huth                                                    GETPC());
77fcf5ef2aSThomas Huth         } else {
78fcf5ef2aSThomas Huth             cpu_stl_data_ra(env, addr, (uint32_t)env->gpr[reg], GETPC());
79fcf5ef2aSThomas Huth         }
80fcf5ef2aSThomas Huth         addr = addr_add(env, addr, 4);
81fcf5ef2aSThomas Huth     }
82fcf5ef2aSThomas Huth }
83fcf5ef2aSThomas Huth 
84fcf5ef2aSThomas Huth static void do_lsw(CPUPPCState *env, target_ulong addr, uint32_t nb,
85fcf5ef2aSThomas Huth                    uint32_t reg, uintptr_t raddr)
86fcf5ef2aSThomas Huth {
87fcf5ef2aSThomas Huth     int sh;
88fcf5ef2aSThomas Huth 
89fcf5ef2aSThomas Huth     for (; nb > 3; nb -= 4) {
90fcf5ef2aSThomas Huth         env->gpr[reg] = cpu_ldl_data_ra(env, addr, raddr);
91fcf5ef2aSThomas Huth         reg = (reg + 1) % 32;
92fcf5ef2aSThomas Huth         addr = addr_add(env, addr, 4);
93fcf5ef2aSThomas Huth     }
94fcf5ef2aSThomas Huth     if (unlikely(nb > 0)) {
95fcf5ef2aSThomas Huth         env->gpr[reg] = 0;
96fcf5ef2aSThomas Huth         for (sh = 24; nb > 0; nb--, sh -= 8) {
97fcf5ef2aSThomas Huth             env->gpr[reg] |= cpu_ldub_data_ra(env, addr, raddr) << sh;
98fcf5ef2aSThomas Huth             addr = addr_add(env, addr, 1);
99fcf5ef2aSThomas Huth         }
100fcf5ef2aSThomas Huth     }
101fcf5ef2aSThomas Huth }
102fcf5ef2aSThomas Huth 
103fcf5ef2aSThomas Huth void helper_lsw(CPUPPCState *env, target_ulong addr, uint32_t nb, uint32_t reg)
104fcf5ef2aSThomas Huth {
105fcf5ef2aSThomas Huth     do_lsw(env, addr, nb, reg, GETPC());
106fcf5ef2aSThomas Huth }
107fcf5ef2aSThomas Huth 
1085a2c8b9eSDavid Gibson /*
1095a2c8b9eSDavid Gibson  * PPC32 specification says we must generate an exception if rA is in
1105a2c8b9eSDavid Gibson  * the range of registers to be loaded.  In an other hand, IBM says
1115a2c8b9eSDavid Gibson  * this is valid, but rA won't be loaded.  For now, I'll follow the
1125a2c8b9eSDavid Gibson  * spec...
113fcf5ef2aSThomas Huth  */
114fcf5ef2aSThomas Huth void helper_lswx(CPUPPCState *env, target_ulong addr, uint32_t reg,
115fcf5ef2aSThomas Huth                  uint32_t ra, uint32_t rb)
116fcf5ef2aSThomas Huth {
117fcf5ef2aSThomas Huth     if (likely(xer_bc != 0)) {
118f0704d78SMarc-André Lureau         int num_used_regs = DIV_ROUND_UP(xer_bc, 4);
119fcf5ef2aSThomas Huth         if (unlikely((ra != 0 && lsw_reg_in_range(reg, num_used_regs, ra)) ||
120fcf5ef2aSThomas Huth                      lsw_reg_in_range(reg, num_used_regs, rb))) {
121fcf5ef2aSThomas Huth             raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
122fcf5ef2aSThomas Huth                                    POWERPC_EXCP_INVAL |
123fcf5ef2aSThomas Huth                                    POWERPC_EXCP_INVAL_LSWX, GETPC());
124fcf5ef2aSThomas Huth         } else {
125fcf5ef2aSThomas Huth             do_lsw(env, addr, xer_bc, reg, GETPC());
126fcf5ef2aSThomas Huth         }
127fcf5ef2aSThomas Huth     }
128fcf5ef2aSThomas Huth }
129fcf5ef2aSThomas Huth 
130fcf5ef2aSThomas Huth void helper_stsw(CPUPPCState *env, target_ulong addr, uint32_t nb,
131fcf5ef2aSThomas Huth                  uint32_t reg)
132fcf5ef2aSThomas Huth {
133fcf5ef2aSThomas Huth     int sh;
134fcf5ef2aSThomas Huth 
135fcf5ef2aSThomas Huth     for (; nb > 3; nb -= 4) {
136fcf5ef2aSThomas Huth         cpu_stl_data_ra(env, addr, env->gpr[reg], GETPC());
137fcf5ef2aSThomas Huth         reg = (reg + 1) % 32;
138fcf5ef2aSThomas Huth         addr = addr_add(env, addr, 4);
139fcf5ef2aSThomas Huth     }
140fcf5ef2aSThomas Huth     if (unlikely(nb > 0)) {
141fcf5ef2aSThomas Huth         for (sh = 24; nb > 0; nb--, sh -= 8) {
142fcf5ef2aSThomas Huth             cpu_stb_data_ra(env, addr, (env->gpr[reg] >> sh) & 0xFF, GETPC());
143fcf5ef2aSThomas Huth             addr = addr_add(env, addr, 1);
144fcf5ef2aSThomas Huth         }
145fcf5ef2aSThomas Huth     }
146fcf5ef2aSThomas Huth }
147fcf5ef2aSThomas Huth 
14850728199SRoman Kapl static void dcbz_common(CPUPPCState *env, target_ulong addr,
14950728199SRoman Kapl                         uint32_t opcode, bool epid, uintptr_t retaddr)
150fcf5ef2aSThomas Huth {
151fcf5ef2aSThomas Huth     target_ulong mask, dcbz_size = env->dcache_line_size;
152fcf5ef2aSThomas Huth     uint32_t i;
153fcf5ef2aSThomas Huth     void *haddr;
15450728199SRoman Kapl     int mmu_idx = epid ? PPC_TLB_EPID_STORE : env->dmmu_idx;
155fcf5ef2aSThomas Huth 
156fcf5ef2aSThomas Huth #if defined(TARGET_PPC64)
157fcf5ef2aSThomas Huth     /* Check for dcbz vs dcbzl on 970 */
158fcf5ef2aSThomas Huth     if (env->excp_model == POWERPC_EXCP_970 &&
159fcf5ef2aSThomas Huth         !(opcode & 0x00200000) && ((env->spr[SPR_970_HID5] >> 7) & 0x3) == 1) {
160fcf5ef2aSThomas Huth         dcbz_size = 32;
161fcf5ef2aSThomas Huth     }
162fcf5ef2aSThomas Huth #endif
163fcf5ef2aSThomas Huth 
164fcf5ef2aSThomas Huth     /* Align address */
165fcf5ef2aSThomas Huth     mask = ~(dcbz_size - 1);
166fcf5ef2aSThomas Huth     addr &= mask;
167fcf5ef2aSThomas Huth 
168fcf5ef2aSThomas Huth     /* Check reservation */
169fcf5ef2aSThomas Huth     if ((env->reserve_addr & mask) == (addr & mask))  {
170fcf5ef2aSThomas Huth         env->reserve_addr = (target_ulong)-1ULL;
171fcf5ef2aSThomas Huth     }
172fcf5ef2aSThomas Huth 
173fcf5ef2aSThomas Huth     /* Try fast path translate */
17450728199SRoman Kapl     haddr = tlb_vaddr_to_host(env, addr, MMU_DATA_STORE, mmu_idx);
175fcf5ef2aSThomas Huth     if (haddr) {
176fcf5ef2aSThomas Huth         memset(haddr, 0, dcbz_size);
177fcf5ef2aSThomas Huth     } else {
178fcf5ef2aSThomas Huth         /* Slow path */
179fcf5ef2aSThomas Huth         for (i = 0; i < dcbz_size; i += 8) {
180*5a376e4fSRichard Henderson             cpu_stq_mmuidx_ra(env, addr + i, 0, mmu_idx, retaddr);
181fcf5ef2aSThomas Huth         }
182fcf5ef2aSThomas Huth     }
18350728199SRoman Kapl }
18450728199SRoman Kapl 
18550728199SRoman Kapl void helper_dcbz(CPUPPCState *env, target_ulong addr, uint32_t opcode)
18650728199SRoman Kapl {
18750728199SRoman Kapl     dcbz_common(env, addr, opcode, false, GETPC());
18850728199SRoman Kapl }
18950728199SRoman Kapl 
19050728199SRoman Kapl void helper_dcbzep(CPUPPCState *env, target_ulong addr, uint32_t opcode)
19150728199SRoman Kapl {
19250728199SRoman Kapl     dcbz_common(env, addr, opcode, true, GETPC());
19350728199SRoman Kapl }
194fcf5ef2aSThomas Huth 
195fcf5ef2aSThomas Huth void helper_icbi(CPUPPCState *env, target_ulong addr)
196fcf5ef2aSThomas Huth {
197fcf5ef2aSThomas Huth     addr &= ~(env->dcache_line_size - 1);
1985a2c8b9eSDavid Gibson     /*
1995a2c8b9eSDavid Gibson      * Invalidate one cache line :
200fcf5ef2aSThomas Huth      * PowerPC specification says this is to be treated like a load
201fcf5ef2aSThomas Huth      * (not a fetch) by the MMU. To be sure it will be so,
202fcf5ef2aSThomas Huth      * do the load "by hand".
203fcf5ef2aSThomas Huth      */
204fcf5ef2aSThomas Huth     cpu_ldl_data_ra(env, addr, GETPC());
205fcf5ef2aSThomas Huth }
206fcf5ef2aSThomas Huth 
20750728199SRoman Kapl void helper_icbiep(CPUPPCState *env, target_ulong addr)
20850728199SRoman Kapl {
20950728199SRoman Kapl #if !defined(CONFIG_USER_ONLY)
21050728199SRoman Kapl     /* See comments above */
21150728199SRoman Kapl     addr &= ~(env->dcache_line_size - 1);
212*5a376e4fSRichard Henderson     cpu_ldl_mmuidx_ra(env, addr, PPC_TLB_EPID_LOAD, GETPC());
21350728199SRoman Kapl #endif
21450728199SRoman Kapl }
21550728199SRoman Kapl 
216fcf5ef2aSThomas Huth /* XXX: to be tested */
217fcf5ef2aSThomas Huth target_ulong helper_lscbx(CPUPPCState *env, target_ulong addr, uint32_t reg,
218fcf5ef2aSThomas Huth                           uint32_t ra, uint32_t rb)
219fcf5ef2aSThomas Huth {
220fcf5ef2aSThomas Huth     int i, c, d;
221fcf5ef2aSThomas Huth 
222fcf5ef2aSThomas Huth     d = 24;
223fcf5ef2aSThomas Huth     for (i = 0; i < xer_bc; i++) {
224fcf5ef2aSThomas Huth         c = cpu_ldub_data_ra(env, addr, GETPC());
225fcf5ef2aSThomas Huth         addr = addr_add(env, addr, 1);
226fcf5ef2aSThomas Huth         /* ra (if not 0) and rb are never modified */
227fcf5ef2aSThomas Huth         if (likely(reg != rb && (ra == 0 || reg != ra))) {
228fcf5ef2aSThomas Huth             env->gpr[reg] = (env->gpr[reg] & ~(0xFF << d)) | (c << d);
229fcf5ef2aSThomas Huth         }
230fcf5ef2aSThomas Huth         if (unlikely(c == xer_cmp)) {
231fcf5ef2aSThomas Huth             break;
232fcf5ef2aSThomas Huth         }
233fcf5ef2aSThomas Huth         if (likely(d != 0)) {
234fcf5ef2aSThomas Huth             d -= 8;
235fcf5ef2aSThomas Huth         } else {
236fcf5ef2aSThomas Huth             d = 24;
237fcf5ef2aSThomas Huth             reg++;
238fcf5ef2aSThomas Huth             reg = reg & 0x1F;
239fcf5ef2aSThomas Huth         }
240fcf5ef2aSThomas Huth     }
241fcf5ef2aSThomas Huth     return i;
242fcf5ef2aSThomas Huth }
243fcf5ef2aSThomas Huth 
244f34ec0f6SRichard Henderson #ifdef TARGET_PPC64
24594bf2658SRichard Henderson uint64_t helper_lq_le_parallel(CPUPPCState *env, target_ulong addr,
24694bf2658SRichard Henderson                                uint32_t opidx)
24794bf2658SRichard Henderson {
248f34ec0f6SRichard Henderson     Int128 ret;
249f34ec0f6SRichard Henderson 
250f34ec0f6SRichard Henderson     /* We will have raised EXCP_ATOMIC from the translator.  */
251f34ec0f6SRichard Henderson     assert(HAVE_ATOMIC128);
252f34ec0f6SRichard Henderson     ret = helper_atomic_ldo_le_mmu(env, addr, opidx, GETPC());
25394bf2658SRichard Henderson     env->retxh = int128_gethi(ret);
25494bf2658SRichard Henderson     return int128_getlo(ret);
25594bf2658SRichard Henderson }
25694bf2658SRichard Henderson 
25794bf2658SRichard Henderson uint64_t helper_lq_be_parallel(CPUPPCState *env, target_ulong addr,
25894bf2658SRichard Henderson                                uint32_t opidx)
25994bf2658SRichard Henderson {
260f34ec0f6SRichard Henderson     Int128 ret;
261f34ec0f6SRichard Henderson 
262f34ec0f6SRichard Henderson     /* We will have raised EXCP_ATOMIC from the translator.  */
263f34ec0f6SRichard Henderson     assert(HAVE_ATOMIC128);
264f34ec0f6SRichard Henderson     ret = helper_atomic_ldo_be_mmu(env, addr, opidx, GETPC());
26594bf2658SRichard Henderson     env->retxh = int128_gethi(ret);
26694bf2658SRichard Henderson     return int128_getlo(ret);
26794bf2658SRichard Henderson }
268f89ced5fSRichard Henderson 
269f89ced5fSRichard Henderson void helper_stq_le_parallel(CPUPPCState *env, target_ulong addr,
270f89ced5fSRichard Henderson                             uint64_t lo, uint64_t hi, uint32_t opidx)
271f89ced5fSRichard Henderson {
272f34ec0f6SRichard Henderson     Int128 val;
273f34ec0f6SRichard Henderson 
274f34ec0f6SRichard Henderson     /* We will have raised EXCP_ATOMIC from the translator.  */
275f34ec0f6SRichard Henderson     assert(HAVE_ATOMIC128);
276f34ec0f6SRichard Henderson     val = int128_make128(lo, hi);
277f89ced5fSRichard Henderson     helper_atomic_sto_le_mmu(env, addr, val, opidx, GETPC());
278f89ced5fSRichard Henderson }
279f89ced5fSRichard Henderson 
280f89ced5fSRichard Henderson void helper_stq_be_parallel(CPUPPCState *env, target_ulong addr,
281f89ced5fSRichard Henderson                             uint64_t lo, uint64_t hi, uint32_t opidx)
282f89ced5fSRichard Henderson {
283f34ec0f6SRichard Henderson     Int128 val;
284f34ec0f6SRichard Henderson 
285f34ec0f6SRichard Henderson     /* We will have raised EXCP_ATOMIC from the translator.  */
286f34ec0f6SRichard Henderson     assert(HAVE_ATOMIC128);
287f34ec0f6SRichard Henderson     val = int128_make128(lo, hi);
288f89ced5fSRichard Henderson     helper_atomic_sto_be_mmu(env, addr, val, opidx, GETPC());
289f89ced5fSRichard Henderson }
2904a9b3c5dSRichard Henderson 
2914a9b3c5dSRichard Henderson uint32_t helper_stqcx_le_parallel(CPUPPCState *env, target_ulong addr,
2924a9b3c5dSRichard Henderson                                   uint64_t new_lo, uint64_t new_hi,
2934a9b3c5dSRichard Henderson                                   uint32_t opidx)
2944a9b3c5dSRichard Henderson {
2954a9b3c5dSRichard Henderson     bool success = false;
2964a9b3c5dSRichard Henderson 
297f34ec0f6SRichard Henderson     /* We will have raised EXCP_ATOMIC from the translator.  */
298f34ec0f6SRichard Henderson     assert(HAVE_CMPXCHG128);
299f34ec0f6SRichard Henderson 
3004a9b3c5dSRichard Henderson     if (likely(addr == env->reserve_addr)) {
3014a9b3c5dSRichard Henderson         Int128 oldv, cmpv, newv;
3024a9b3c5dSRichard Henderson 
3034a9b3c5dSRichard Henderson         cmpv = int128_make128(env->reserve_val2, env->reserve_val);
3044a9b3c5dSRichard Henderson         newv = int128_make128(new_lo, new_hi);
3054a9b3c5dSRichard Henderson         oldv = helper_atomic_cmpxchgo_le_mmu(env, addr, cmpv, newv,
3064a9b3c5dSRichard Henderson                                              opidx, GETPC());
3074a9b3c5dSRichard Henderson         success = int128_eq(oldv, cmpv);
3084a9b3c5dSRichard Henderson     }
3094a9b3c5dSRichard Henderson     env->reserve_addr = -1;
3104a9b3c5dSRichard Henderson     return env->so + success * CRF_EQ_BIT;
3114a9b3c5dSRichard Henderson }
3124a9b3c5dSRichard Henderson 
3134a9b3c5dSRichard Henderson uint32_t helper_stqcx_be_parallel(CPUPPCState *env, target_ulong addr,
3144a9b3c5dSRichard Henderson                                   uint64_t new_lo, uint64_t new_hi,
3154a9b3c5dSRichard Henderson                                   uint32_t opidx)
3164a9b3c5dSRichard Henderson {
3174a9b3c5dSRichard Henderson     bool success = false;
3184a9b3c5dSRichard Henderson 
319f34ec0f6SRichard Henderson     /* We will have raised EXCP_ATOMIC from the translator.  */
320f34ec0f6SRichard Henderson     assert(HAVE_CMPXCHG128);
321f34ec0f6SRichard Henderson 
3224a9b3c5dSRichard Henderson     if (likely(addr == env->reserve_addr)) {
3234a9b3c5dSRichard Henderson         Int128 oldv, cmpv, newv;
3244a9b3c5dSRichard Henderson 
3254a9b3c5dSRichard Henderson         cmpv = int128_make128(env->reserve_val2, env->reserve_val);
3264a9b3c5dSRichard Henderson         newv = int128_make128(new_lo, new_hi);
3274a9b3c5dSRichard Henderson         oldv = helper_atomic_cmpxchgo_be_mmu(env, addr, cmpv, newv,
3284a9b3c5dSRichard Henderson                                              opidx, GETPC());
3294a9b3c5dSRichard Henderson         success = int128_eq(oldv, cmpv);
3304a9b3c5dSRichard Henderson     }
3314a9b3c5dSRichard Henderson     env->reserve_addr = -1;
3324a9b3c5dSRichard Henderson     return env->so + success * CRF_EQ_BIT;
3334a9b3c5dSRichard Henderson }
33494bf2658SRichard Henderson #endif
33594bf2658SRichard Henderson 
336fcf5ef2aSThomas Huth /*****************************************************************************/
337fcf5ef2aSThomas Huth /* Altivec extension helpers */
338fcf5ef2aSThomas Huth #if defined(HOST_WORDS_BIGENDIAN)
339fcf5ef2aSThomas Huth #define HI_IDX 0
340fcf5ef2aSThomas Huth #define LO_IDX 1
341fcf5ef2aSThomas Huth #else
342fcf5ef2aSThomas Huth #define HI_IDX 1
343fcf5ef2aSThomas Huth #define LO_IDX 0
344fcf5ef2aSThomas Huth #endif
345fcf5ef2aSThomas Huth 
3465a2c8b9eSDavid Gibson /*
3475a2c8b9eSDavid Gibson  * We use msr_le to determine index ordering in a vector.  However,
3485a2c8b9eSDavid Gibson  * byteswapping is not simply controlled by msr_le.  We also need to
3495a2c8b9eSDavid Gibson  * take into account endianness of the target.  This is done for the
3505a2c8b9eSDavid Gibson  * little-endian PPC64 user-mode target.
3515a2c8b9eSDavid Gibson  */
352fcf5ef2aSThomas Huth 
353fcf5ef2aSThomas Huth #define LVE(name, access, swap, element)                        \
354fcf5ef2aSThomas Huth     void helper_##name(CPUPPCState *env, ppc_avr_t *r,          \
355fcf5ef2aSThomas Huth                        target_ulong addr)                       \
356fcf5ef2aSThomas Huth     {                                                           \
357fcf5ef2aSThomas Huth         size_t n_elems = ARRAY_SIZE(r->element);                \
358fcf5ef2aSThomas Huth         int adjust = HI_IDX * (n_elems - 1);                    \
359fcf5ef2aSThomas Huth         int sh = sizeof(r->element[0]) >> 1;                    \
360fcf5ef2aSThomas Huth         int index = (addr & 0xf) >> sh;                         \
361fcf5ef2aSThomas Huth         if (msr_le) {                                           \
362fcf5ef2aSThomas Huth             index = n_elems - index - 1;                        \
363fcf5ef2aSThomas Huth         }                                                       \
364fcf5ef2aSThomas Huth                                                                 \
365fcf5ef2aSThomas Huth         if (needs_byteswap(env)) {                              \
366fcf5ef2aSThomas Huth             r->element[LO_IDX ? index : (adjust - index)] =     \
367fcf5ef2aSThomas Huth                 swap(access(env, addr, GETPC()));               \
368fcf5ef2aSThomas Huth         } else {                                                \
369fcf5ef2aSThomas Huth             r->element[LO_IDX ? index : (adjust - index)] =     \
370fcf5ef2aSThomas Huth                 access(env, addr, GETPC());                     \
371fcf5ef2aSThomas Huth         }                                                       \
372fcf5ef2aSThomas Huth     }
373fcf5ef2aSThomas Huth #define I(x) (x)
374fcf5ef2aSThomas Huth LVE(lvebx, cpu_ldub_data_ra, I, u8)
375fcf5ef2aSThomas Huth LVE(lvehx, cpu_lduw_data_ra, bswap16, u16)
376fcf5ef2aSThomas Huth LVE(lvewx, cpu_ldl_data_ra, bswap32, u32)
377fcf5ef2aSThomas Huth #undef I
378fcf5ef2aSThomas Huth #undef LVE
379fcf5ef2aSThomas Huth 
380fcf5ef2aSThomas Huth #define STVE(name, access, swap, element)                               \
381fcf5ef2aSThomas Huth     void helper_##name(CPUPPCState *env, ppc_avr_t *r,                  \
382fcf5ef2aSThomas Huth                        target_ulong addr)                               \
383fcf5ef2aSThomas Huth     {                                                                   \
384fcf5ef2aSThomas Huth         size_t n_elems = ARRAY_SIZE(r->element);                        \
385fcf5ef2aSThomas Huth         int adjust = HI_IDX * (n_elems - 1);                            \
386fcf5ef2aSThomas Huth         int sh = sizeof(r->element[0]) >> 1;                            \
387fcf5ef2aSThomas Huth         int index = (addr & 0xf) >> sh;                                 \
388fcf5ef2aSThomas Huth         if (msr_le) {                                                   \
389fcf5ef2aSThomas Huth             index = n_elems - index - 1;                                \
390fcf5ef2aSThomas Huth         }                                                               \
391fcf5ef2aSThomas Huth                                                                         \
392fcf5ef2aSThomas Huth         if (needs_byteswap(env)) {                                      \
393fcf5ef2aSThomas Huth             access(env, addr, swap(r->element[LO_IDX ? index :          \
394fcf5ef2aSThomas Huth                                               (adjust - index)]),       \
395fcf5ef2aSThomas Huth                         GETPC());                                       \
396fcf5ef2aSThomas Huth         } else {                                                        \
397fcf5ef2aSThomas Huth             access(env, addr, r->element[LO_IDX ? index :               \
398fcf5ef2aSThomas Huth                                          (adjust - index)], GETPC());   \
399fcf5ef2aSThomas Huth         }                                                               \
400fcf5ef2aSThomas Huth     }
401fcf5ef2aSThomas Huth #define I(x) (x)
402fcf5ef2aSThomas Huth STVE(stvebx, cpu_stb_data_ra, I, u8)
403fcf5ef2aSThomas Huth STVE(stvehx, cpu_stw_data_ra, bswap16, u16)
404fcf5ef2aSThomas Huth STVE(stvewx, cpu_stl_data_ra, bswap32, u32)
405fcf5ef2aSThomas Huth #undef I
406fcf5ef2aSThomas Huth #undef LVE
407fcf5ef2aSThomas Huth 
4086914bc4fSNikunj A Dadhania #ifdef TARGET_PPC64
4096914bc4fSNikunj A Dadhania #define GET_NB(rb) ((rb >> 56) & 0xFF)
4106914bc4fSNikunj A Dadhania 
4116914bc4fSNikunj A Dadhania #define VSX_LXVL(name, lj)                                              \
4126914bc4fSNikunj A Dadhania void helper_##name(CPUPPCState *env, target_ulong addr,                 \
4132aba168eSMark Cave-Ayland                    ppc_vsr_t *xt, target_ulong rb)                      \
4146914bc4fSNikunj A Dadhania {                                                                       \
4152a175830SMark Cave-Ayland     ppc_vsr_t t;                                                        \
4166914bc4fSNikunj A Dadhania     uint64_t nb = GET_NB(rb);                                           \
4172a175830SMark Cave-Ayland     int i;                                                              \
4186914bc4fSNikunj A Dadhania                                                                         \
4192a175830SMark Cave-Ayland     t.s128 = int128_zero();                                             \
4206914bc4fSNikunj A Dadhania     if (nb) {                                                           \
4216914bc4fSNikunj A Dadhania         nb = (nb >= 16) ? 16 : nb;                                      \
4226914bc4fSNikunj A Dadhania         if (msr_le && !lj) {                                            \
4236914bc4fSNikunj A Dadhania             for (i = 16; i > 16 - nb; i--) {                            \
4242a175830SMark Cave-Ayland                 t.VsrB(i - 1) = cpu_ldub_data_ra(env, addr, GETPC());   \
4256914bc4fSNikunj A Dadhania                 addr = addr_add(env, addr, 1);                          \
4266914bc4fSNikunj A Dadhania             }                                                           \
4276914bc4fSNikunj A Dadhania         } else {                                                        \
4286914bc4fSNikunj A Dadhania             for (i = 0; i < nb; i++) {                                  \
4292a175830SMark Cave-Ayland                 t.VsrB(i) = cpu_ldub_data_ra(env, addr, GETPC());       \
4306914bc4fSNikunj A Dadhania                 addr = addr_add(env, addr, 1);                          \
4316914bc4fSNikunj A Dadhania             }                                                           \
4326914bc4fSNikunj A Dadhania         }                                                               \
4336914bc4fSNikunj A Dadhania     }                                                                   \
4342a175830SMark Cave-Ayland     *xt = t;                                                            \
4356914bc4fSNikunj A Dadhania }
4366914bc4fSNikunj A Dadhania 
4376914bc4fSNikunj A Dadhania VSX_LXVL(lxvl, 0)
438176e44e7SNikunj A Dadhania VSX_LXVL(lxvll, 1)
4396914bc4fSNikunj A Dadhania #undef VSX_LXVL
440681c2478SNikunj A Dadhania 
441681c2478SNikunj A Dadhania #define VSX_STXVL(name, lj)                                       \
442681c2478SNikunj A Dadhania void helper_##name(CPUPPCState *env, target_ulong addr,           \
4432aba168eSMark Cave-Ayland                    ppc_vsr_t *xt, target_ulong rb)                \
444681c2478SNikunj A Dadhania {                                                                 \
445681c2478SNikunj A Dadhania     target_ulong nb = GET_NB(rb);                                 \
4462a175830SMark Cave-Ayland     int i;                                                        \
447681c2478SNikunj A Dadhania                                                                   \
448681c2478SNikunj A Dadhania     if (!nb) {                                                    \
449681c2478SNikunj A Dadhania         return;                                                   \
450681c2478SNikunj A Dadhania     }                                                             \
4512a175830SMark Cave-Ayland                                                                   \
452681c2478SNikunj A Dadhania     nb = (nb >= 16) ? 16 : nb;                                    \
453681c2478SNikunj A Dadhania     if (msr_le && !lj) {                                          \
454681c2478SNikunj A Dadhania         for (i = 16; i > 16 - nb; i--) {                          \
4552a175830SMark Cave-Ayland             cpu_stb_data_ra(env, addr, xt->VsrB(i - 1), GETPC()); \
456681c2478SNikunj A Dadhania             addr = addr_add(env, addr, 1);                        \
457681c2478SNikunj A Dadhania         }                                                         \
458681c2478SNikunj A Dadhania     } else {                                                      \
459681c2478SNikunj A Dadhania         for (i = 0; i < nb; i++) {                                \
4602a175830SMark Cave-Ayland             cpu_stb_data_ra(env, addr, xt->VsrB(i), GETPC());     \
461681c2478SNikunj A Dadhania             addr = addr_add(env, addr, 1);                        \
462681c2478SNikunj A Dadhania         }                                                         \
463681c2478SNikunj A Dadhania     }                                                             \
464681c2478SNikunj A Dadhania }
465681c2478SNikunj A Dadhania 
466681c2478SNikunj A Dadhania VSX_STXVL(stxvl, 0)
467e122090dSNikunj A Dadhania VSX_STXVL(stxvll, 1)
468681c2478SNikunj A Dadhania #undef VSX_STXVL
4696914bc4fSNikunj A Dadhania #undef GET_NB
4706914bc4fSNikunj A Dadhania #endif /* TARGET_PPC64 */
4716914bc4fSNikunj A Dadhania 
472fcf5ef2aSThomas Huth #undef HI_IDX
473fcf5ef2aSThomas Huth #undef LO_IDX
474fcf5ef2aSThomas Huth 
475fcf5ef2aSThomas Huth void helper_tbegin(CPUPPCState *env)
476fcf5ef2aSThomas Huth {
4775a2c8b9eSDavid Gibson     /*
4785a2c8b9eSDavid Gibson      * As a degenerate implementation, always fail tbegin.  The reason
479fcf5ef2aSThomas Huth      * given is "Nesting overflow".  The "persistent" bit is set,
480fcf5ef2aSThomas Huth      * providing a hint to the error handler to not retry.  The TFIAR
481fcf5ef2aSThomas Huth      * captures the address of the failure, which is this tbegin
4825a2c8b9eSDavid Gibson      * instruction.  Instruction execution will continue with the next
4835a2c8b9eSDavid Gibson      * instruction in memory, which is precisely what we want.
484fcf5ef2aSThomas Huth      */
485fcf5ef2aSThomas Huth 
486fcf5ef2aSThomas Huth     env->spr[SPR_TEXASR] =
487fcf5ef2aSThomas Huth         (1ULL << TEXASR_FAILURE_PERSISTENT) |
488fcf5ef2aSThomas Huth         (1ULL << TEXASR_NESTING_OVERFLOW) |
489fcf5ef2aSThomas Huth         (msr_hv << TEXASR_PRIVILEGE_HV) |
490fcf5ef2aSThomas Huth         (msr_pr << TEXASR_PRIVILEGE_PR) |
491fcf5ef2aSThomas Huth         (1ULL << TEXASR_FAILURE_SUMMARY) |
492fcf5ef2aSThomas Huth         (1ULL << TEXASR_TFIAR_EXACT);
493fcf5ef2aSThomas Huth     env->spr[SPR_TFIAR] = env->nip | (msr_hv << 1) | msr_pr;
494fcf5ef2aSThomas Huth     env->spr[SPR_TFHAR] = env->nip + 4;
495fcf5ef2aSThomas Huth     env->crf[0] = 0xB; /* 0b1010 = transaction failure */
496fcf5ef2aSThomas Huth }
497