xref: /openbmc/qemu/target/ppc/mem_helper.c (revision f34ec0f6d79f1b9f52e41cd89bf7f0e7c853b124)
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  */
19fcf5ef2aSThomas Huth #include "qemu/osdep.h"
20fcf5ef2aSThomas Huth #include "cpu.h"
21fcf5ef2aSThomas Huth #include "exec/exec-all.h"
22fcf5ef2aSThomas Huth #include "qemu/host-utils.h"
23fcf5ef2aSThomas Huth #include "exec/helper-proto.h"
24fcf5ef2aSThomas Huth #include "helper_regs.h"
25fcf5ef2aSThomas Huth #include "exec/cpu_ldst.h"
2694bf2658SRichard Henderson #include "tcg.h"
276914bc4fSNikunj A Dadhania #include "internal.h"
28*f34ec0f6SRichard Henderson #include "qemu/atomic128.h"
29fcf5ef2aSThomas Huth 
30fcf5ef2aSThomas Huth //#define DEBUG_OP
31fcf5ef2aSThomas Huth 
32fcf5ef2aSThomas Huth static inline bool needs_byteswap(const CPUPPCState *env)
33fcf5ef2aSThomas Huth {
34fcf5ef2aSThomas Huth #if defined(TARGET_WORDS_BIGENDIAN)
35fcf5ef2aSThomas Huth   return msr_le;
36fcf5ef2aSThomas Huth #else
37fcf5ef2aSThomas Huth   return !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 
57fcf5ef2aSThomas Huth void helper_lmw(CPUPPCState *env, target_ulong addr, uint32_t reg)
58fcf5ef2aSThomas Huth {
59fcf5ef2aSThomas Huth     for (; reg < 32; reg++) {
60fcf5ef2aSThomas Huth         if (needs_byteswap(env)) {
61fcf5ef2aSThomas Huth             env->gpr[reg] = bswap32(cpu_ldl_data_ra(env, addr, GETPC()));
62fcf5ef2aSThomas Huth         } else {
63fcf5ef2aSThomas Huth             env->gpr[reg] = cpu_ldl_data_ra(env, addr, GETPC());
64fcf5ef2aSThomas Huth         }
65fcf5ef2aSThomas Huth         addr = addr_add(env, addr, 4);
66fcf5ef2aSThomas Huth     }
67fcf5ef2aSThomas Huth }
68fcf5ef2aSThomas Huth 
69fcf5ef2aSThomas Huth void helper_stmw(CPUPPCState *env, target_ulong addr, uint32_t reg)
70fcf5ef2aSThomas Huth {
71fcf5ef2aSThomas Huth     for (; reg < 32; reg++) {
72fcf5ef2aSThomas Huth         if (needs_byteswap(env)) {
73fcf5ef2aSThomas Huth             cpu_stl_data_ra(env, addr, bswap32((uint32_t)env->gpr[reg]),
74fcf5ef2aSThomas Huth                                                    GETPC());
75fcf5ef2aSThomas Huth         } else {
76fcf5ef2aSThomas Huth             cpu_stl_data_ra(env, addr, (uint32_t)env->gpr[reg], GETPC());
77fcf5ef2aSThomas Huth         }
78fcf5ef2aSThomas Huth         addr = addr_add(env, addr, 4);
79fcf5ef2aSThomas Huth     }
80fcf5ef2aSThomas Huth }
81fcf5ef2aSThomas Huth 
82fcf5ef2aSThomas Huth static void do_lsw(CPUPPCState *env, target_ulong addr, uint32_t nb,
83fcf5ef2aSThomas Huth                    uint32_t reg, uintptr_t raddr)
84fcf5ef2aSThomas Huth {
85fcf5ef2aSThomas Huth     int sh;
86fcf5ef2aSThomas Huth 
87fcf5ef2aSThomas Huth     for (; nb > 3; nb -= 4) {
88fcf5ef2aSThomas Huth         env->gpr[reg] = cpu_ldl_data_ra(env, addr, raddr);
89fcf5ef2aSThomas Huth         reg = (reg + 1) % 32;
90fcf5ef2aSThomas Huth         addr = addr_add(env, addr, 4);
91fcf5ef2aSThomas Huth     }
92fcf5ef2aSThomas Huth     if (unlikely(nb > 0)) {
93fcf5ef2aSThomas Huth         env->gpr[reg] = 0;
94fcf5ef2aSThomas Huth         for (sh = 24; nb > 0; nb--, sh -= 8) {
95fcf5ef2aSThomas Huth             env->gpr[reg] |= cpu_ldub_data_ra(env, addr, raddr) << sh;
96fcf5ef2aSThomas Huth             addr = addr_add(env, addr, 1);
97fcf5ef2aSThomas Huth         }
98fcf5ef2aSThomas Huth     }
99fcf5ef2aSThomas Huth }
100fcf5ef2aSThomas Huth 
101fcf5ef2aSThomas Huth void helper_lsw(CPUPPCState *env, target_ulong addr, uint32_t nb, uint32_t reg)
102fcf5ef2aSThomas Huth {
103fcf5ef2aSThomas Huth     do_lsw(env, addr, nb, reg, GETPC());
104fcf5ef2aSThomas Huth }
105fcf5ef2aSThomas Huth 
106fcf5ef2aSThomas Huth /* PPC32 specification says we must generate an exception if
107fcf5ef2aSThomas Huth  * rA is in the range of registers to be loaded.
108fcf5ef2aSThomas Huth  * In an other hand, IBM says this is valid, but rA won't be loaded.
109fcf5ef2aSThomas Huth  * For now, I'll follow the spec...
110fcf5ef2aSThomas Huth  */
111fcf5ef2aSThomas Huth void helper_lswx(CPUPPCState *env, target_ulong addr, uint32_t reg,
112fcf5ef2aSThomas Huth                  uint32_t ra, uint32_t rb)
113fcf5ef2aSThomas Huth {
114fcf5ef2aSThomas Huth     if (likely(xer_bc != 0)) {
115f0704d78SMarc-André Lureau         int num_used_regs = DIV_ROUND_UP(xer_bc, 4);
116fcf5ef2aSThomas Huth         if (unlikely((ra != 0 && lsw_reg_in_range(reg, num_used_regs, ra)) ||
117fcf5ef2aSThomas Huth                      lsw_reg_in_range(reg, num_used_regs, rb))) {
118fcf5ef2aSThomas Huth             raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
119fcf5ef2aSThomas Huth                                    POWERPC_EXCP_INVAL |
120fcf5ef2aSThomas Huth                                    POWERPC_EXCP_INVAL_LSWX, GETPC());
121fcf5ef2aSThomas Huth         } else {
122fcf5ef2aSThomas Huth             do_lsw(env, addr, xer_bc, reg, GETPC());
123fcf5ef2aSThomas Huth         }
124fcf5ef2aSThomas Huth     }
125fcf5ef2aSThomas Huth }
126fcf5ef2aSThomas Huth 
127fcf5ef2aSThomas Huth void helper_stsw(CPUPPCState *env, target_ulong addr, uint32_t nb,
128fcf5ef2aSThomas Huth                  uint32_t reg)
129fcf5ef2aSThomas Huth {
130fcf5ef2aSThomas Huth     int sh;
131fcf5ef2aSThomas Huth 
132fcf5ef2aSThomas Huth     for (; nb > 3; nb -= 4) {
133fcf5ef2aSThomas Huth         cpu_stl_data_ra(env, addr, env->gpr[reg], GETPC());
134fcf5ef2aSThomas Huth         reg = (reg + 1) % 32;
135fcf5ef2aSThomas Huth         addr = addr_add(env, addr, 4);
136fcf5ef2aSThomas Huth     }
137fcf5ef2aSThomas Huth     if (unlikely(nb > 0)) {
138fcf5ef2aSThomas Huth         for (sh = 24; nb > 0; nb--, sh -= 8) {
139fcf5ef2aSThomas Huth             cpu_stb_data_ra(env, addr, (env->gpr[reg] >> sh) & 0xFF, GETPC());
140fcf5ef2aSThomas Huth             addr = addr_add(env, addr, 1);
141fcf5ef2aSThomas Huth         }
142fcf5ef2aSThomas Huth     }
143fcf5ef2aSThomas Huth }
144fcf5ef2aSThomas Huth 
145fcf5ef2aSThomas Huth void helper_dcbz(CPUPPCState *env, target_ulong addr, uint32_t opcode)
146fcf5ef2aSThomas Huth {
147fcf5ef2aSThomas Huth     target_ulong mask, dcbz_size = env->dcache_line_size;
148fcf5ef2aSThomas Huth     uint32_t i;
149fcf5ef2aSThomas Huth     void *haddr;
150fcf5ef2aSThomas Huth 
151fcf5ef2aSThomas Huth #if defined(TARGET_PPC64)
152fcf5ef2aSThomas Huth     /* Check for dcbz vs dcbzl on 970 */
153fcf5ef2aSThomas Huth     if (env->excp_model == POWERPC_EXCP_970 &&
154fcf5ef2aSThomas Huth         !(opcode & 0x00200000) && ((env->spr[SPR_970_HID5] >> 7) & 0x3) == 1) {
155fcf5ef2aSThomas Huth         dcbz_size = 32;
156fcf5ef2aSThomas Huth     }
157fcf5ef2aSThomas Huth #endif
158fcf5ef2aSThomas Huth 
159fcf5ef2aSThomas Huth     /* Align address */
160fcf5ef2aSThomas Huth     mask = ~(dcbz_size - 1);
161fcf5ef2aSThomas Huth     addr &= mask;
162fcf5ef2aSThomas Huth 
163fcf5ef2aSThomas Huth     /* Check reservation */
164fcf5ef2aSThomas Huth     if ((env->reserve_addr & mask) == (addr & mask))  {
165fcf5ef2aSThomas Huth         env->reserve_addr = (target_ulong)-1ULL;
166fcf5ef2aSThomas Huth     }
167fcf5ef2aSThomas Huth 
168fcf5ef2aSThomas Huth     /* Try fast path translate */
169fcf5ef2aSThomas Huth     haddr = tlb_vaddr_to_host(env, addr, MMU_DATA_STORE, env->dmmu_idx);
170fcf5ef2aSThomas Huth     if (haddr) {
171fcf5ef2aSThomas Huth         memset(haddr, 0, dcbz_size);
172fcf5ef2aSThomas Huth     } else {
173fcf5ef2aSThomas Huth         /* Slow path */
174fcf5ef2aSThomas Huth         for (i = 0; i < dcbz_size; i += 8) {
175fcf5ef2aSThomas Huth             cpu_stq_data_ra(env, addr + i, 0, GETPC());
176fcf5ef2aSThomas Huth         }
177fcf5ef2aSThomas Huth     }
178fcf5ef2aSThomas Huth }
179fcf5ef2aSThomas Huth 
180fcf5ef2aSThomas Huth void helper_icbi(CPUPPCState *env, target_ulong addr)
181fcf5ef2aSThomas Huth {
182fcf5ef2aSThomas Huth     addr &= ~(env->dcache_line_size - 1);
183fcf5ef2aSThomas Huth     /* Invalidate one cache line :
184fcf5ef2aSThomas Huth      * PowerPC specification says this is to be treated like a load
185fcf5ef2aSThomas Huth      * (not a fetch) by the MMU. To be sure it will be so,
186fcf5ef2aSThomas Huth      * do the load "by hand".
187fcf5ef2aSThomas Huth      */
188fcf5ef2aSThomas Huth     cpu_ldl_data_ra(env, addr, GETPC());
189fcf5ef2aSThomas Huth }
190fcf5ef2aSThomas Huth 
191fcf5ef2aSThomas Huth /* XXX: to be tested */
192fcf5ef2aSThomas Huth target_ulong helper_lscbx(CPUPPCState *env, target_ulong addr, uint32_t reg,
193fcf5ef2aSThomas Huth                           uint32_t ra, uint32_t rb)
194fcf5ef2aSThomas Huth {
195fcf5ef2aSThomas Huth     int i, c, d;
196fcf5ef2aSThomas Huth 
197fcf5ef2aSThomas Huth     d = 24;
198fcf5ef2aSThomas Huth     for (i = 0; i < xer_bc; i++) {
199fcf5ef2aSThomas Huth         c = cpu_ldub_data_ra(env, addr, GETPC());
200fcf5ef2aSThomas Huth         addr = addr_add(env, addr, 1);
201fcf5ef2aSThomas Huth         /* ra (if not 0) and rb are never modified */
202fcf5ef2aSThomas Huth         if (likely(reg != rb && (ra == 0 || reg != ra))) {
203fcf5ef2aSThomas Huth             env->gpr[reg] = (env->gpr[reg] & ~(0xFF << d)) | (c << d);
204fcf5ef2aSThomas Huth         }
205fcf5ef2aSThomas Huth         if (unlikely(c == xer_cmp)) {
206fcf5ef2aSThomas Huth             break;
207fcf5ef2aSThomas Huth         }
208fcf5ef2aSThomas Huth         if (likely(d != 0)) {
209fcf5ef2aSThomas Huth             d -= 8;
210fcf5ef2aSThomas Huth         } else {
211fcf5ef2aSThomas Huth             d = 24;
212fcf5ef2aSThomas Huth             reg++;
213fcf5ef2aSThomas Huth             reg = reg & 0x1F;
214fcf5ef2aSThomas Huth         }
215fcf5ef2aSThomas Huth     }
216fcf5ef2aSThomas Huth     return i;
217fcf5ef2aSThomas Huth }
218fcf5ef2aSThomas Huth 
219*f34ec0f6SRichard Henderson #ifdef TARGET_PPC64
22094bf2658SRichard Henderson uint64_t helper_lq_le_parallel(CPUPPCState *env, target_ulong addr,
22194bf2658SRichard Henderson                                uint32_t opidx)
22294bf2658SRichard Henderson {
223*f34ec0f6SRichard Henderson     Int128 ret;
224*f34ec0f6SRichard Henderson 
225*f34ec0f6SRichard Henderson     /* We will have raised EXCP_ATOMIC from the translator.  */
226*f34ec0f6SRichard Henderson     assert(HAVE_ATOMIC128);
227*f34ec0f6SRichard Henderson     ret = helper_atomic_ldo_le_mmu(env, addr, opidx, GETPC());
22894bf2658SRichard Henderson     env->retxh = int128_gethi(ret);
22994bf2658SRichard Henderson     return int128_getlo(ret);
23094bf2658SRichard Henderson }
23194bf2658SRichard Henderson 
23294bf2658SRichard Henderson uint64_t helper_lq_be_parallel(CPUPPCState *env, target_ulong addr,
23394bf2658SRichard Henderson                                uint32_t opidx)
23494bf2658SRichard Henderson {
235*f34ec0f6SRichard Henderson     Int128 ret;
236*f34ec0f6SRichard Henderson 
237*f34ec0f6SRichard Henderson     /* We will have raised EXCP_ATOMIC from the translator.  */
238*f34ec0f6SRichard Henderson     assert(HAVE_ATOMIC128);
239*f34ec0f6SRichard Henderson     ret = helper_atomic_ldo_be_mmu(env, addr, opidx, GETPC());
24094bf2658SRichard Henderson     env->retxh = int128_gethi(ret);
24194bf2658SRichard Henderson     return int128_getlo(ret);
24294bf2658SRichard Henderson }
243f89ced5fSRichard Henderson 
244f89ced5fSRichard Henderson void helper_stq_le_parallel(CPUPPCState *env, target_ulong addr,
245f89ced5fSRichard Henderson                             uint64_t lo, uint64_t hi, uint32_t opidx)
246f89ced5fSRichard Henderson {
247*f34ec0f6SRichard Henderson     Int128 val;
248*f34ec0f6SRichard Henderson 
249*f34ec0f6SRichard Henderson     /* We will have raised EXCP_ATOMIC from the translator.  */
250*f34ec0f6SRichard Henderson     assert(HAVE_ATOMIC128);
251*f34ec0f6SRichard Henderson     val = int128_make128(lo, hi);
252f89ced5fSRichard Henderson     helper_atomic_sto_le_mmu(env, addr, val, opidx, GETPC());
253f89ced5fSRichard Henderson }
254f89ced5fSRichard Henderson 
255f89ced5fSRichard Henderson void helper_stq_be_parallel(CPUPPCState *env, target_ulong addr,
256f89ced5fSRichard Henderson                             uint64_t lo, uint64_t hi, uint32_t opidx)
257f89ced5fSRichard Henderson {
258*f34ec0f6SRichard Henderson     Int128 val;
259*f34ec0f6SRichard Henderson 
260*f34ec0f6SRichard Henderson     /* We will have raised EXCP_ATOMIC from the translator.  */
261*f34ec0f6SRichard Henderson     assert(HAVE_ATOMIC128);
262*f34ec0f6SRichard Henderson     val = int128_make128(lo, hi);
263f89ced5fSRichard Henderson     helper_atomic_sto_be_mmu(env, addr, val, opidx, GETPC());
264f89ced5fSRichard Henderson }
2654a9b3c5dSRichard Henderson 
2664a9b3c5dSRichard Henderson uint32_t helper_stqcx_le_parallel(CPUPPCState *env, target_ulong addr,
2674a9b3c5dSRichard Henderson                                   uint64_t new_lo, uint64_t new_hi,
2684a9b3c5dSRichard Henderson                                   uint32_t opidx)
2694a9b3c5dSRichard Henderson {
2704a9b3c5dSRichard Henderson     bool success = false;
2714a9b3c5dSRichard Henderson 
272*f34ec0f6SRichard Henderson     /* We will have raised EXCP_ATOMIC from the translator.  */
273*f34ec0f6SRichard Henderson     assert(HAVE_CMPXCHG128);
274*f34ec0f6SRichard Henderson 
2754a9b3c5dSRichard Henderson     if (likely(addr == env->reserve_addr)) {
2764a9b3c5dSRichard Henderson         Int128 oldv, cmpv, newv;
2774a9b3c5dSRichard Henderson 
2784a9b3c5dSRichard Henderson         cmpv = int128_make128(env->reserve_val2, env->reserve_val);
2794a9b3c5dSRichard Henderson         newv = int128_make128(new_lo, new_hi);
2804a9b3c5dSRichard Henderson         oldv = helper_atomic_cmpxchgo_le_mmu(env, addr, cmpv, newv,
2814a9b3c5dSRichard Henderson                                              opidx, GETPC());
2824a9b3c5dSRichard Henderson         success = int128_eq(oldv, cmpv);
2834a9b3c5dSRichard Henderson     }
2844a9b3c5dSRichard Henderson     env->reserve_addr = -1;
2854a9b3c5dSRichard Henderson     return env->so + success * CRF_EQ_BIT;
2864a9b3c5dSRichard Henderson }
2874a9b3c5dSRichard Henderson 
2884a9b3c5dSRichard Henderson uint32_t helper_stqcx_be_parallel(CPUPPCState *env, target_ulong addr,
2894a9b3c5dSRichard Henderson                                   uint64_t new_lo, uint64_t new_hi,
2904a9b3c5dSRichard Henderson                                   uint32_t opidx)
2914a9b3c5dSRichard Henderson {
2924a9b3c5dSRichard Henderson     bool success = false;
2934a9b3c5dSRichard Henderson 
294*f34ec0f6SRichard Henderson     /* We will have raised EXCP_ATOMIC from the translator.  */
295*f34ec0f6SRichard Henderson     assert(HAVE_CMPXCHG128);
296*f34ec0f6SRichard Henderson 
2974a9b3c5dSRichard Henderson     if (likely(addr == env->reserve_addr)) {
2984a9b3c5dSRichard Henderson         Int128 oldv, cmpv, newv;
2994a9b3c5dSRichard Henderson 
3004a9b3c5dSRichard Henderson         cmpv = int128_make128(env->reserve_val2, env->reserve_val);
3014a9b3c5dSRichard Henderson         newv = int128_make128(new_lo, new_hi);
3024a9b3c5dSRichard Henderson         oldv = helper_atomic_cmpxchgo_be_mmu(env, addr, cmpv, newv,
3034a9b3c5dSRichard Henderson                                              opidx, GETPC());
3044a9b3c5dSRichard Henderson         success = int128_eq(oldv, cmpv);
3054a9b3c5dSRichard Henderson     }
3064a9b3c5dSRichard Henderson     env->reserve_addr = -1;
3074a9b3c5dSRichard Henderson     return env->so + success * CRF_EQ_BIT;
3084a9b3c5dSRichard Henderson }
30994bf2658SRichard Henderson #endif
31094bf2658SRichard Henderson 
311fcf5ef2aSThomas Huth /*****************************************************************************/
312fcf5ef2aSThomas Huth /* Altivec extension helpers */
313fcf5ef2aSThomas Huth #if defined(HOST_WORDS_BIGENDIAN)
314fcf5ef2aSThomas Huth #define HI_IDX 0
315fcf5ef2aSThomas Huth #define LO_IDX 1
316fcf5ef2aSThomas Huth #else
317fcf5ef2aSThomas Huth #define HI_IDX 1
318fcf5ef2aSThomas Huth #define LO_IDX 0
319fcf5ef2aSThomas Huth #endif
320fcf5ef2aSThomas Huth 
321fcf5ef2aSThomas Huth /* We use msr_le to determine index ordering in a vector.  However,
322fcf5ef2aSThomas Huth    byteswapping is not simply controlled by msr_le.  We also need to take
323fcf5ef2aSThomas Huth    into account endianness of the target.  This is done for the little-endian
324fcf5ef2aSThomas Huth    PPC64 user-mode target. */
325fcf5ef2aSThomas Huth 
326fcf5ef2aSThomas Huth #define LVE(name, access, swap, element)                        \
327fcf5ef2aSThomas Huth     void helper_##name(CPUPPCState *env, ppc_avr_t *r,          \
328fcf5ef2aSThomas Huth                        target_ulong addr)                       \
329fcf5ef2aSThomas Huth     {                                                           \
330fcf5ef2aSThomas Huth         size_t n_elems = ARRAY_SIZE(r->element);                \
331fcf5ef2aSThomas Huth         int adjust = HI_IDX*(n_elems - 1);                      \
332fcf5ef2aSThomas Huth         int sh = sizeof(r->element[0]) >> 1;                    \
333fcf5ef2aSThomas Huth         int index = (addr & 0xf) >> sh;                         \
334fcf5ef2aSThomas Huth         if (msr_le) {                                           \
335fcf5ef2aSThomas Huth             index = n_elems - index - 1;                        \
336fcf5ef2aSThomas Huth         }                                                       \
337fcf5ef2aSThomas Huth                                                                 \
338fcf5ef2aSThomas Huth         if (needs_byteswap(env)) {                              \
339fcf5ef2aSThomas Huth             r->element[LO_IDX ? index : (adjust - index)] =     \
340fcf5ef2aSThomas Huth                 swap(access(env, addr, GETPC()));               \
341fcf5ef2aSThomas Huth         } else {                                                \
342fcf5ef2aSThomas Huth             r->element[LO_IDX ? index : (adjust - index)] =     \
343fcf5ef2aSThomas Huth                 access(env, addr, GETPC());                     \
344fcf5ef2aSThomas Huth         }                                                       \
345fcf5ef2aSThomas Huth     }
346fcf5ef2aSThomas Huth #define I(x) (x)
347fcf5ef2aSThomas Huth LVE(lvebx, cpu_ldub_data_ra, I, u8)
348fcf5ef2aSThomas Huth LVE(lvehx, cpu_lduw_data_ra, bswap16, u16)
349fcf5ef2aSThomas Huth LVE(lvewx, cpu_ldl_data_ra, bswap32, u32)
350fcf5ef2aSThomas Huth #undef I
351fcf5ef2aSThomas Huth #undef LVE
352fcf5ef2aSThomas Huth 
353fcf5ef2aSThomas Huth #define STVE(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             access(env, addr, swap(r->element[LO_IDX ? index :          \
367fcf5ef2aSThomas Huth                                               (adjust - index)]),       \
368fcf5ef2aSThomas Huth                         GETPC());                                       \
369fcf5ef2aSThomas Huth         } else {                                                        \
370fcf5ef2aSThomas Huth             access(env, addr, r->element[LO_IDX ? index :               \
371fcf5ef2aSThomas Huth                                          (adjust - index)], GETPC());   \
372fcf5ef2aSThomas Huth         }                                                               \
373fcf5ef2aSThomas Huth     }
374fcf5ef2aSThomas Huth #define I(x) (x)
375fcf5ef2aSThomas Huth STVE(stvebx, cpu_stb_data_ra, I, u8)
376fcf5ef2aSThomas Huth STVE(stvehx, cpu_stw_data_ra, bswap16, u16)
377fcf5ef2aSThomas Huth STVE(stvewx, cpu_stl_data_ra, bswap32, u32)
378fcf5ef2aSThomas Huth #undef I
379fcf5ef2aSThomas Huth #undef LVE
380fcf5ef2aSThomas Huth 
3816914bc4fSNikunj A Dadhania #ifdef TARGET_PPC64
3826914bc4fSNikunj A Dadhania #define GET_NB(rb) ((rb >> 56) & 0xFF)
3836914bc4fSNikunj A Dadhania 
3846914bc4fSNikunj A Dadhania #define VSX_LXVL(name, lj)                                              \
3856914bc4fSNikunj A Dadhania void helper_##name(CPUPPCState *env, target_ulong addr,                 \
3866914bc4fSNikunj A Dadhania                    target_ulong xt_num, target_ulong rb)                \
3876914bc4fSNikunj A Dadhania {                                                                       \
3886914bc4fSNikunj A Dadhania     int i;                                                              \
3896914bc4fSNikunj A Dadhania     ppc_vsr_t xt;                                                       \
3906914bc4fSNikunj A Dadhania     uint64_t nb = GET_NB(rb);                                           \
3916914bc4fSNikunj A Dadhania                                                                         \
3926914bc4fSNikunj A Dadhania     xt.s128 = int128_zero();                                            \
3936914bc4fSNikunj A Dadhania     if (nb) {                                                           \
3946914bc4fSNikunj A Dadhania         nb = (nb >= 16) ? 16 : nb;                                      \
3956914bc4fSNikunj A Dadhania         if (msr_le && !lj) {                                            \
3966914bc4fSNikunj A Dadhania             for (i = 16; i > 16 - nb; i--) {                            \
3976914bc4fSNikunj A Dadhania                 xt.VsrB(i - 1) = cpu_ldub_data_ra(env, addr, GETPC());  \
3986914bc4fSNikunj A Dadhania                 addr = addr_add(env, addr, 1);                          \
3996914bc4fSNikunj A Dadhania             }                                                           \
4006914bc4fSNikunj A Dadhania         } else {                                                        \
4016914bc4fSNikunj A Dadhania             for (i = 0; i < nb; i++) {                                  \
4026914bc4fSNikunj A Dadhania                 xt.VsrB(i) = cpu_ldub_data_ra(env, addr, GETPC());      \
4036914bc4fSNikunj A Dadhania                 addr = addr_add(env, addr, 1);                          \
4046914bc4fSNikunj A Dadhania             }                                                           \
4056914bc4fSNikunj A Dadhania         }                                                               \
4066914bc4fSNikunj A Dadhania     }                                                                   \
4076914bc4fSNikunj A Dadhania     putVSR(xt_num, &xt, env);                                           \
4086914bc4fSNikunj A Dadhania }
4096914bc4fSNikunj A Dadhania 
4106914bc4fSNikunj A Dadhania VSX_LXVL(lxvl, 0)
411176e44e7SNikunj A Dadhania VSX_LXVL(lxvll, 1)
4126914bc4fSNikunj A Dadhania #undef VSX_LXVL
413681c2478SNikunj A Dadhania 
414681c2478SNikunj A Dadhania #define VSX_STXVL(name, lj)                                       \
415681c2478SNikunj A Dadhania void helper_##name(CPUPPCState *env, target_ulong addr,           \
416681c2478SNikunj A Dadhania                    target_ulong xt_num, target_ulong rb)          \
417681c2478SNikunj A Dadhania {                                                                 \
418681c2478SNikunj A Dadhania     int i;                                                        \
419681c2478SNikunj A Dadhania     ppc_vsr_t xt;                                                 \
420681c2478SNikunj A Dadhania     target_ulong nb = GET_NB(rb);                                 \
421681c2478SNikunj A Dadhania                                                                   \
422681c2478SNikunj A Dadhania     if (!nb) {                                                    \
423681c2478SNikunj A Dadhania         return;                                                   \
424681c2478SNikunj A Dadhania     }                                                             \
425681c2478SNikunj A Dadhania     getVSR(xt_num, &xt, env);                                     \
426681c2478SNikunj A Dadhania     nb = (nb >= 16) ? 16 : nb;                                    \
427681c2478SNikunj A Dadhania     if (msr_le && !lj) {                                          \
428681c2478SNikunj A Dadhania         for (i = 16; i > 16 - nb; i--) {                          \
429681c2478SNikunj A Dadhania             cpu_stb_data_ra(env, addr, xt.VsrB(i - 1), GETPC());  \
430681c2478SNikunj A Dadhania             addr = addr_add(env, addr, 1);                        \
431681c2478SNikunj A Dadhania         }                                                         \
432681c2478SNikunj A Dadhania     } else {                                                      \
433681c2478SNikunj A Dadhania         for (i = 0; i < nb; i++) {                                \
434681c2478SNikunj A Dadhania             cpu_stb_data_ra(env, addr, xt.VsrB(i), GETPC());      \
435681c2478SNikunj A Dadhania             addr = addr_add(env, addr, 1);                        \
436681c2478SNikunj A Dadhania         }                                                         \
437681c2478SNikunj A Dadhania     }                                                             \
438681c2478SNikunj A Dadhania }
439681c2478SNikunj A Dadhania 
440681c2478SNikunj A Dadhania VSX_STXVL(stxvl, 0)
441e122090dSNikunj A Dadhania VSX_STXVL(stxvll, 1)
442681c2478SNikunj A Dadhania #undef VSX_STXVL
4436914bc4fSNikunj A Dadhania #undef GET_NB
4446914bc4fSNikunj A Dadhania #endif /* TARGET_PPC64 */
4456914bc4fSNikunj A Dadhania 
446fcf5ef2aSThomas Huth #undef HI_IDX
447fcf5ef2aSThomas Huth #undef LO_IDX
448fcf5ef2aSThomas Huth 
449fcf5ef2aSThomas Huth void helper_tbegin(CPUPPCState *env)
450fcf5ef2aSThomas Huth {
451fcf5ef2aSThomas Huth     /* As a degenerate implementation, always fail tbegin.  The reason
452fcf5ef2aSThomas Huth      * given is "Nesting overflow".  The "persistent" bit is set,
453fcf5ef2aSThomas Huth      * providing a hint to the error handler to not retry.  The TFIAR
454fcf5ef2aSThomas Huth      * captures the address of the failure, which is this tbegin
455fcf5ef2aSThomas Huth      * instruction.  Instruction execution will continue with the
456fcf5ef2aSThomas Huth      * next instruction in memory, which is precisely what we want.
457fcf5ef2aSThomas Huth      */
458fcf5ef2aSThomas Huth 
459fcf5ef2aSThomas Huth     env->spr[SPR_TEXASR] =
460fcf5ef2aSThomas Huth         (1ULL << TEXASR_FAILURE_PERSISTENT) |
461fcf5ef2aSThomas Huth         (1ULL << TEXASR_NESTING_OVERFLOW) |
462fcf5ef2aSThomas Huth         (msr_hv << TEXASR_PRIVILEGE_HV) |
463fcf5ef2aSThomas Huth         (msr_pr << TEXASR_PRIVILEGE_PR) |
464fcf5ef2aSThomas Huth         (1ULL << TEXASR_FAILURE_SUMMARY) |
465fcf5ef2aSThomas Huth         (1ULL << TEXASR_TFIAR_EXACT);
466fcf5ef2aSThomas Huth     env->spr[SPR_TFIAR] = env->nip | (msr_hv << 1) | msr_pr;
467fcf5ef2aSThomas Huth     env->spr[SPR_TFHAR] = env->nip + 4;
468fcf5ef2aSThomas Huth     env->crf[0] = 0xB; /* 0b1010 = transaction failure */
469fcf5ef2aSThomas Huth }
470