xref: /openbmc/qemu/target/ppc/mem_helper.c (revision 2a17583082f5350edb17207d76252603ec960afa)
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"
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 {
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 
1065a2c8b9eSDavid Gibson /*
1075a2c8b9eSDavid Gibson  * PPC32 specification says we must generate an exception if rA is in
1085a2c8b9eSDavid Gibson  * the range of registers to be loaded.  In an other hand, IBM says
1095a2c8b9eSDavid Gibson  * this is valid, but rA won't be loaded.  For now, I'll follow the
1105a2c8b9eSDavid Gibson  * spec...
111fcf5ef2aSThomas Huth  */
112fcf5ef2aSThomas Huth void helper_lswx(CPUPPCState *env, target_ulong addr, uint32_t reg,
113fcf5ef2aSThomas Huth                  uint32_t ra, uint32_t rb)
114fcf5ef2aSThomas Huth {
115fcf5ef2aSThomas Huth     if (likely(xer_bc != 0)) {
116f0704d78SMarc-André Lureau         int num_used_regs = DIV_ROUND_UP(xer_bc, 4);
117fcf5ef2aSThomas Huth         if (unlikely((ra != 0 && lsw_reg_in_range(reg, num_used_regs, ra)) ||
118fcf5ef2aSThomas Huth                      lsw_reg_in_range(reg, num_used_regs, rb))) {
119fcf5ef2aSThomas Huth             raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
120fcf5ef2aSThomas Huth                                    POWERPC_EXCP_INVAL |
121fcf5ef2aSThomas Huth                                    POWERPC_EXCP_INVAL_LSWX, GETPC());
122fcf5ef2aSThomas Huth         } else {
123fcf5ef2aSThomas Huth             do_lsw(env, addr, xer_bc, reg, GETPC());
124fcf5ef2aSThomas Huth         }
125fcf5ef2aSThomas Huth     }
126fcf5ef2aSThomas Huth }
127fcf5ef2aSThomas Huth 
128fcf5ef2aSThomas Huth void helper_stsw(CPUPPCState *env, target_ulong addr, uint32_t nb,
129fcf5ef2aSThomas Huth                  uint32_t reg)
130fcf5ef2aSThomas Huth {
131fcf5ef2aSThomas Huth     int sh;
132fcf5ef2aSThomas Huth 
133fcf5ef2aSThomas Huth     for (; nb > 3; nb -= 4) {
134fcf5ef2aSThomas Huth         cpu_stl_data_ra(env, addr, env->gpr[reg], GETPC());
135fcf5ef2aSThomas Huth         reg = (reg + 1) % 32;
136fcf5ef2aSThomas Huth         addr = addr_add(env, addr, 4);
137fcf5ef2aSThomas Huth     }
138fcf5ef2aSThomas Huth     if (unlikely(nb > 0)) {
139fcf5ef2aSThomas Huth         for (sh = 24; nb > 0; nb--, sh -= 8) {
140fcf5ef2aSThomas Huth             cpu_stb_data_ra(env, addr, (env->gpr[reg] >> sh) & 0xFF, GETPC());
141fcf5ef2aSThomas Huth             addr = addr_add(env, addr, 1);
142fcf5ef2aSThomas Huth         }
143fcf5ef2aSThomas Huth     }
144fcf5ef2aSThomas Huth }
145fcf5ef2aSThomas Huth 
14650728199SRoman Kapl static void dcbz_common(CPUPPCState *env, target_ulong addr,
14750728199SRoman Kapl                         uint32_t opcode, bool epid, uintptr_t retaddr)
148fcf5ef2aSThomas Huth {
149fcf5ef2aSThomas Huth     target_ulong mask, dcbz_size = env->dcache_line_size;
150fcf5ef2aSThomas Huth     uint32_t i;
151fcf5ef2aSThomas Huth     void *haddr;
15250728199SRoman Kapl     int mmu_idx = epid ? PPC_TLB_EPID_STORE : env->dmmu_idx;
153fcf5ef2aSThomas Huth 
154fcf5ef2aSThomas Huth #if defined(TARGET_PPC64)
155fcf5ef2aSThomas Huth     /* Check for dcbz vs dcbzl on 970 */
156fcf5ef2aSThomas Huth     if (env->excp_model == POWERPC_EXCP_970 &&
157fcf5ef2aSThomas Huth         !(opcode & 0x00200000) && ((env->spr[SPR_970_HID5] >> 7) & 0x3) == 1) {
158fcf5ef2aSThomas Huth         dcbz_size = 32;
159fcf5ef2aSThomas Huth     }
160fcf5ef2aSThomas Huth #endif
161fcf5ef2aSThomas Huth 
162fcf5ef2aSThomas Huth     /* Align address */
163fcf5ef2aSThomas Huth     mask = ~(dcbz_size - 1);
164fcf5ef2aSThomas Huth     addr &= mask;
165fcf5ef2aSThomas Huth 
166fcf5ef2aSThomas Huth     /* Check reservation */
167fcf5ef2aSThomas Huth     if ((env->reserve_addr & mask) == (addr & mask))  {
168fcf5ef2aSThomas Huth         env->reserve_addr = (target_ulong)-1ULL;
169fcf5ef2aSThomas Huth     }
170fcf5ef2aSThomas Huth 
171fcf5ef2aSThomas Huth     /* Try fast path translate */
17250728199SRoman Kapl     haddr = tlb_vaddr_to_host(env, addr, MMU_DATA_STORE, mmu_idx);
173fcf5ef2aSThomas Huth     if (haddr) {
174fcf5ef2aSThomas Huth         memset(haddr, 0, dcbz_size);
175fcf5ef2aSThomas Huth     } else {
176fcf5ef2aSThomas Huth         /* Slow path */
177fcf5ef2aSThomas Huth         for (i = 0; i < dcbz_size; i += 8) {
17850728199SRoman Kapl             if (epid) {
17950728199SRoman Kapl #if !defined(CONFIG_USER_ONLY)
18050728199SRoman Kapl                 /* Does not make sense on USER_ONLY config */
18150728199SRoman Kapl                 cpu_stq_eps_ra(env, addr + i, 0, retaddr);
18250728199SRoman Kapl #endif
18350728199SRoman Kapl             } else {
18450728199SRoman Kapl                 cpu_stq_data_ra(env, addr + i, 0, retaddr);
185fcf5ef2aSThomas Huth             }
186fcf5ef2aSThomas Huth         }
187fcf5ef2aSThomas Huth     }
18850728199SRoman Kapl }
18950728199SRoman Kapl 
19050728199SRoman Kapl void helper_dcbz(CPUPPCState *env, target_ulong addr, uint32_t opcode)
19150728199SRoman Kapl {
19250728199SRoman Kapl     dcbz_common(env, addr, opcode, false, GETPC());
19350728199SRoman Kapl }
19450728199SRoman Kapl 
19550728199SRoman Kapl void helper_dcbzep(CPUPPCState *env, target_ulong addr, uint32_t opcode)
19650728199SRoman Kapl {
19750728199SRoman Kapl     dcbz_common(env, addr, opcode, true, GETPC());
19850728199SRoman Kapl }
199fcf5ef2aSThomas Huth 
200fcf5ef2aSThomas Huth void helper_icbi(CPUPPCState *env, target_ulong addr)
201fcf5ef2aSThomas Huth {
202fcf5ef2aSThomas Huth     addr &= ~(env->dcache_line_size - 1);
2035a2c8b9eSDavid Gibson     /*
2045a2c8b9eSDavid Gibson      * Invalidate one cache line :
205fcf5ef2aSThomas Huth      * PowerPC specification says this is to be treated like a load
206fcf5ef2aSThomas Huth      * (not a fetch) by the MMU. To be sure it will be so,
207fcf5ef2aSThomas Huth      * do the load "by hand".
208fcf5ef2aSThomas Huth      */
209fcf5ef2aSThomas Huth     cpu_ldl_data_ra(env, addr, GETPC());
210fcf5ef2aSThomas Huth }
211fcf5ef2aSThomas Huth 
21250728199SRoman Kapl void helper_icbiep(CPUPPCState *env, target_ulong addr)
21350728199SRoman Kapl {
21450728199SRoman Kapl #if !defined(CONFIG_USER_ONLY)
21550728199SRoman Kapl     /* See comments above */
21650728199SRoman Kapl     addr &= ~(env->dcache_line_size - 1);
21750728199SRoman Kapl     cpu_ldl_epl_ra(env, addr, GETPC());
21850728199SRoman Kapl #endif
21950728199SRoman Kapl }
22050728199SRoman Kapl 
221fcf5ef2aSThomas Huth /* XXX: to be tested */
222fcf5ef2aSThomas Huth target_ulong helper_lscbx(CPUPPCState *env, target_ulong addr, uint32_t reg,
223fcf5ef2aSThomas Huth                           uint32_t ra, uint32_t rb)
224fcf5ef2aSThomas Huth {
225fcf5ef2aSThomas Huth     int i, c, d;
226fcf5ef2aSThomas Huth 
227fcf5ef2aSThomas Huth     d = 24;
228fcf5ef2aSThomas Huth     for (i = 0; i < xer_bc; i++) {
229fcf5ef2aSThomas Huth         c = cpu_ldub_data_ra(env, addr, GETPC());
230fcf5ef2aSThomas Huth         addr = addr_add(env, addr, 1);
231fcf5ef2aSThomas Huth         /* ra (if not 0) and rb are never modified */
232fcf5ef2aSThomas Huth         if (likely(reg != rb && (ra == 0 || reg != ra))) {
233fcf5ef2aSThomas Huth             env->gpr[reg] = (env->gpr[reg] & ~(0xFF << d)) | (c << d);
234fcf5ef2aSThomas Huth         }
235fcf5ef2aSThomas Huth         if (unlikely(c == xer_cmp)) {
236fcf5ef2aSThomas Huth             break;
237fcf5ef2aSThomas Huth         }
238fcf5ef2aSThomas Huth         if (likely(d != 0)) {
239fcf5ef2aSThomas Huth             d -= 8;
240fcf5ef2aSThomas Huth         } else {
241fcf5ef2aSThomas Huth             d = 24;
242fcf5ef2aSThomas Huth             reg++;
243fcf5ef2aSThomas Huth             reg = reg & 0x1F;
244fcf5ef2aSThomas Huth         }
245fcf5ef2aSThomas Huth     }
246fcf5ef2aSThomas Huth     return i;
247fcf5ef2aSThomas Huth }
248fcf5ef2aSThomas Huth 
249f34ec0f6SRichard Henderson #ifdef TARGET_PPC64
25094bf2658SRichard Henderson uint64_t helper_lq_le_parallel(CPUPPCState *env, target_ulong addr,
25194bf2658SRichard Henderson                                uint32_t opidx)
25294bf2658SRichard Henderson {
253f34ec0f6SRichard Henderson     Int128 ret;
254f34ec0f6SRichard Henderson 
255f34ec0f6SRichard Henderson     /* We will have raised EXCP_ATOMIC from the translator.  */
256f34ec0f6SRichard Henderson     assert(HAVE_ATOMIC128);
257f34ec0f6SRichard Henderson     ret = helper_atomic_ldo_le_mmu(env, addr, opidx, GETPC());
25894bf2658SRichard Henderson     env->retxh = int128_gethi(ret);
25994bf2658SRichard Henderson     return int128_getlo(ret);
26094bf2658SRichard Henderson }
26194bf2658SRichard Henderson 
26294bf2658SRichard Henderson uint64_t helper_lq_be_parallel(CPUPPCState *env, target_ulong addr,
26394bf2658SRichard Henderson                                uint32_t opidx)
26494bf2658SRichard Henderson {
265f34ec0f6SRichard Henderson     Int128 ret;
266f34ec0f6SRichard Henderson 
267f34ec0f6SRichard Henderson     /* We will have raised EXCP_ATOMIC from the translator.  */
268f34ec0f6SRichard Henderson     assert(HAVE_ATOMIC128);
269f34ec0f6SRichard Henderson     ret = helper_atomic_ldo_be_mmu(env, addr, opidx, GETPC());
27094bf2658SRichard Henderson     env->retxh = int128_gethi(ret);
27194bf2658SRichard Henderson     return int128_getlo(ret);
27294bf2658SRichard Henderson }
273f89ced5fSRichard Henderson 
274f89ced5fSRichard Henderson void helper_stq_le_parallel(CPUPPCState *env, target_ulong addr,
275f89ced5fSRichard Henderson                             uint64_t lo, uint64_t hi, uint32_t opidx)
276f89ced5fSRichard Henderson {
277f34ec0f6SRichard Henderson     Int128 val;
278f34ec0f6SRichard Henderson 
279f34ec0f6SRichard Henderson     /* We will have raised EXCP_ATOMIC from the translator.  */
280f34ec0f6SRichard Henderson     assert(HAVE_ATOMIC128);
281f34ec0f6SRichard Henderson     val = int128_make128(lo, hi);
282f89ced5fSRichard Henderson     helper_atomic_sto_le_mmu(env, addr, val, opidx, GETPC());
283f89ced5fSRichard Henderson }
284f89ced5fSRichard Henderson 
285f89ced5fSRichard Henderson void helper_stq_be_parallel(CPUPPCState *env, target_ulong addr,
286f89ced5fSRichard Henderson                             uint64_t lo, uint64_t hi, uint32_t opidx)
287f89ced5fSRichard Henderson {
288f34ec0f6SRichard Henderson     Int128 val;
289f34ec0f6SRichard Henderson 
290f34ec0f6SRichard Henderson     /* We will have raised EXCP_ATOMIC from the translator.  */
291f34ec0f6SRichard Henderson     assert(HAVE_ATOMIC128);
292f34ec0f6SRichard Henderson     val = int128_make128(lo, hi);
293f89ced5fSRichard Henderson     helper_atomic_sto_be_mmu(env, addr, val, opidx, GETPC());
294f89ced5fSRichard Henderson }
2954a9b3c5dSRichard Henderson 
2964a9b3c5dSRichard Henderson uint32_t helper_stqcx_le_parallel(CPUPPCState *env, target_ulong addr,
2974a9b3c5dSRichard Henderson                                   uint64_t new_lo, uint64_t new_hi,
2984a9b3c5dSRichard Henderson                                   uint32_t opidx)
2994a9b3c5dSRichard Henderson {
3004a9b3c5dSRichard Henderson     bool success = false;
3014a9b3c5dSRichard Henderson 
302f34ec0f6SRichard Henderson     /* We will have raised EXCP_ATOMIC from the translator.  */
303f34ec0f6SRichard Henderson     assert(HAVE_CMPXCHG128);
304f34ec0f6SRichard Henderson 
3054a9b3c5dSRichard Henderson     if (likely(addr == env->reserve_addr)) {
3064a9b3c5dSRichard Henderson         Int128 oldv, cmpv, newv;
3074a9b3c5dSRichard Henderson 
3084a9b3c5dSRichard Henderson         cmpv = int128_make128(env->reserve_val2, env->reserve_val);
3094a9b3c5dSRichard Henderson         newv = int128_make128(new_lo, new_hi);
3104a9b3c5dSRichard Henderson         oldv = helper_atomic_cmpxchgo_le_mmu(env, addr, cmpv, newv,
3114a9b3c5dSRichard Henderson                                              opidx, GETPC());
3124a9b3c5dSRichard Henderson         success = int128_eq(oldv, cmpv);
3134a9b3c5dSRichard Henderson     }
3144a9b3c5dSRichard Henderson     env->reserve_addr = -1;
3154a9b3c5dSRichard Henderson     return env->so + success * CRF_EQ_BIT;
3164a9b3c5dSRichard Henderson }
3174a9b3c5dSRichard Henderson 
3184a9b3c5dSRichard Henderson uint32_t helper_stqcx_be_parallel(CPUPPCState *env, target_ulong addr,
3194a9b3c5dSRichard Henderson                                   uint64_t new_lo, uint64_t new_hi,
3204a9b3c5dSRichard Henderson                                   uint32_t opidx)
3214a9b3c5dSRichard Henderson {
3224a9b3c5dSRichard Henderson     bool success = false;
3234a9b3c5dSRichard Henderson 
324f34ec0f6SRichard Henderson     /* We will have raised EXCP_ATOMIC from the translator.  */
325f34ec0f6SRichard Henderson     assert(HAVE_CMPXCHG128);
326f34ec0f6SRichard Henderson 
3274a9b3c5dSRichard Henderson     if (likely(addr == env->reserve_addr)) {
3284a9b3c5dSRichard Henderson         Int128 oldv, cmpv, newv;
3294a9b3c5dSRichard Henderson 
3304a9b3c5dSRichard Henderson         cmpv = int128_make128(env->reserve_val2, env->reserve_val);
3314a9b3c5dSRichard Henderson         newv = int128_make128(new_lo, new_hi);
3324a9b3c5dSRichard Henderson         oldv = helper_atomic_cmpxchgo_be_mmu(env, addr, cmpv, newv,
3334a9b3c5dSRichard Henderson                                              opidx, GETPC());
3344a9b3c5dSRichard Henderson         success = int128_eq(oldv, cmpv);
3354a9b3c5dSRichard Henderson     }
3364a9b3c5dSRichard Henderson     env->reserve_addr = -1;
3374a9b3c5dSRichard Henderson     return env->so + success * CRF_EQ_BIT;
3384a9b3c5dSRichard Henderson }
33994bf2658SRichard Henderson #endif
34094bf2658SRichard Henderson 
341fcf5ef2aSThomas Huth /*****************************************************************************/
342fcf5ef2aSThomas Huth /* Altivec extension helpers */
343fcf5ef2aSThomas Huth #if defined(HOST_WORDS_BIGENDIAN)
344fcf5ef2aSThomas Huth #define HI_IDX 0
345fcf5ef2aSThomas Huth #define LO_IDX 1
346fcf5ef2aSThomas Huth #else
347fcf5ef2aSThomas Huth #define HI_IDX 1
348fcf5ef2aSThomas Huth #define LO_IDX 0
349fcf5ef2aSThomas Huth #endif
350fcf5ef2aSThomas Huth 
3515a2c8b9eSDavid Gibson /*
3525a2c8b9eSDavid Gibson  * We use msr_le to determine index ordering in a vector.  However,
3535a2c8b9eSDavid Gibson  * byteswapping is not simply controlled by msr_le.  We also need to
3545a2c8b9eSDavid Gibson  * take into account endianness of the target.  This is done for the
3555a2c8b9eSDavid Gibson  * little-endian PPC64 user-mode target.
3565a2c8b9eSDavid Gibson  */
357fcf5ef2aSThomas Huth 
358fcf5ef2aSThomas Huth #define LVE(name, access, swap, element)                        \
359fcf5ef2aSThomas Huth     void helper_##name(CPUPPCState *env, ppc_avr_t *r,          \
360fcf5ef2aSThomas Huth                        target_ulong addr)                       \
361fcf5ef2aSThomas Huth     {                                                           \
362fcf5ef2aSThomas Huth         size_t n_elems = ARRAY_SIZE(r->element);                \
363fcf5ef2aSThomas Huth         int adjust = HI_IDX * (n_elems - 1);                    \
364fcf5ef2aSThomas Huth         int sh = sizeof(r->element[0]) >> 1;                    \
365fcf5ef2aSThomas Huth         int index = (addr & 0xf) >> sh;                         \
366fcf5ef2aSThomas Huth         if (msr_le) {                                           \
367fcf5ef2aSThomas Huth             index = n_elems - index - 1;                        \
368fcf5ef2aSThomas Huth         }                                                       \
369fcf5ef2aSThomas Huth                                                                 \
370fcf5ef2aSThomas Huth         if (needs_byteswap(env)) {                              \
371fcf5ef2aSThomas Huth             r->element[LO_IDX ? index : (adjust - index)] =     \
372fcf5ef2aSThomas Huth                 swap(access(env, addr, GETPC()));               \
373fcf5ef2aSThomas Huth         } else {                                                \
374fcf5ef2aSThomas Huth             r->element[LO_IDX ? index : (adjust - index)] =     \
375fcf5ef2aSThomas Huth                 access(env, addr, GETPC());                     \
376fcf5ef2aSThomas Huth         }                                                       \
377fcf5ef2aSThomas Huth     }
378fcf5ef2aSThomas Huth #define I(x) (x)
379fcf5ef2aSThomas Huth LVE(lvebx, cpu_ldub_data_ra, I, u8)
380fcf5ef2aSThomas Huth LVE(lvehx, cpu_lduw_data_ra, bswap16, u16)
381fcf5ef2aSThomas Huth LVE(lvewx, cpu_ldl_data_ra, bswap32, u32)
382fcf5ef2aSThomas Huth #undef I
383fcf5ef2aSThomas Huth #undef LVE
384fcf5ef2aSThomas Huth 
385fcf5ef2aSThomas Huth #define STVE(name, access, swap, element)                               \
386fcf5ef2aSThomas Huth     void helper_##name(CPUPPCState *env, ppc_avr_t *r,                  \
387fcf5ef2aSThomas Huth                        target_ulong addr)                               \
388fcf5ef2aSThomas Huth     {                                                                   \
389fcf5ef2aSThomas Huth         size_t n_elems = ARRAY_SIZE(r->element);                        \
390fcf5ef2aSThomas Huth         int adjust = HI_IDX * (n_elems - 1);                            \
391fcf5ef2aSThomas Huth         int sh = sizeof(r->element[0]) >> 1;                            \
392fcf5ef2aSThomas Huth         int index = (addr & 0xf) >> sh;                                 \
393fcf5ef2aSThomas Huth         if (msr_le) {                                                   \
394fcf5ef2aSThomas Huth             index = n_elems - index - 1;                                \
395fcf5ef2aSThomas Huth         }                                                               \
396fcf5ef2aSThomas Huth                                                                         \
397fcf5ef2aSThomas Huth         if (needs_byteswap(env)) {                                      \
398fcf5ef2aSThomas Huth             access(env, addr, swap(r->element[LO_IDX ? index :          \
399fcf5ef2aSThomas Huth                                               (adjust - index)]),       \
400fcf5ef2aSThomas Huth                         GETPC());                                       \
401fcf5ef2aSThomas Huth         } else {                                                        \
402fcf5ef2aSThomas Huth             access(env, addr, r->element[LO_IDX ? index :               \
403fcf5ef2aSThomas Huth                                          (adjust - index)], GETPC());   \
404fcf5ef2aSThomas Huth         }                                                               \
405fcf5ef2aSThomas Huth     }
406fcf5ef2aSThomas Huth #define I(x) (x)
407fcf5ef2aSThomas Huth STVE(stvebx, cpu_stb_data_ra, I, u8)
408fcf5ef2aSThomas Huth STVE(stvehx, cpu_stw_data_ra, bswap16, u16)
409fcf5ef2aSThomas Huth STVE(stvewx, cpu_stl_data_ra, bswap32, u32)
410fcf5ef2aSThomas Huth #undef I
411fcf5ef2aSThomas Huth #undef LVE
412fcf5ef2aSThomas Huth 
4136914bc4fSNikunj A Dadhania #ifdef TARGET_PPC64
4146914bc4fSNikunj A Dadhania #define GET_NB(rb) ((rb >> 56) & 0xFF)
4156914bc4fSNikunj A Dadhania 
4166914bc4fSNikunj A Dadhania #define VSX_LXVL(name, lj)                                              \
4176914bc4fSNikunj A Dadhania void helper_##name(CPUPPCState *env, target_ulong addr,                 \
4186914bc4fSNikunj A Dadhania                    target_ulong xt_num, target_ulong rb)                \
4196914bc4fSNikunj A Dadhania {                                                                       \
420*2a175830SMark Cave-Ayland     ppc_vsr_t *xt = &env->vsr[xt_num];                                  \
421*2a175830SMark Cave-Ayland     ppc_vsr_t t;                                                        \
4226914bc4fSNikunj A Dadhania     uint64_t nb = GET_NB(rb);                                           \
423*2a175830SMark Cave-Ayland     int i;                                                              \
4246914bc4fSNikunj A Dadhania                                                                         \
425*2a175830SMark Cave-Ayland     t.s128 = int128_zero();                                             \
4266914bc4fSNikunj A Dadhania     if (nb) {                                                           \
4276914bc4fSNikunj A Dadhania         nb = (nb >= 16) ? 16 : nb;                                      \
4286914bc4fSNikunj A Dadhania         if (msr_le && !lj) {                                            \
4296914bc4fSNikunj A Dadhania             for (i = 16; i > 16 - nb; i--) {                            \
430*2a175830SMark Cave-Ayland                 t.VsrB(i - 1) = cpu_ldub_data_ra(env, addr, GETPC());   \
4316914bc4fSNikunj A Dadhania                 addr = addr_add(env, addr, 1);                          \
4326914bc4fSNikunj A Dadhania             }                                                           \
4336914bc4fSNikunj A Dadhania         } else {                                                        \
4346914bc4fSNikunj A Dadhania             for (i = 0; i < nb; i++) {                                  \
435*2a175830SMark Cave-Ayland                 t.VsrB(i) = cpu_ldub_data_ra(env, addr, GETPC());       \
4366914bc4fSNikunj A Dadhania                 addr = addr_add(env, addr, 1);                          \
4376914bc4fSNikunj A Dadhania             }                                                           \
4386914bc4fSNikunj A Dadhania         }                                                               \
4396914bc4fSNikunj A Dadhania     }                                                                   \
440*2a175830SMark Cave-Ayland     *xt = t;                                                            \
4416914bc4fSNikunj A Dadhania }
4426914bc4fSNikunj A Dadhania 
4436914bc4fSNikunj A Dadhania VSX_LXVL(lxvl, 0)
444176e44e7SNikunj A Dadhania VSX_LXVL(lxvll, 1)
4456914bc4fSNikunj A Dadhania #undef VSX_LXVL
446681c2478SNikunj A Dadhania 
447681c2478SNikunj A Dadhania #define VSX_STXVL(name, lj)                                       \
448681c2478SNikunj A Dadhania void helper_##name(CPUPPCState *env, target_ulong addr,           \
449681c2478SNikunj A Dadhania                    target_ulong xt_num, target_ulong rb)          \
450681c2478SNikunj A Dadhania {                                                                 \
451*2a175830SMark Cave-Ayland     ppc_vsr_t *xt = &env->vsr[xt_num];                            \
452681c2478SNikunj A Dadhania     target_ulong nb = GET_NB(rb);                                 \
453*2a175830SMark Cave-Ayland     int i;                                                        \
454681c2478SNikunj A Dadhania                                                                   \
455681c2478SNikunj A Dadhania     if (!nb) {                                                    \
456681c2478SNikunj A Dadhania         return;                                                   \
457681c2478SNikunj A Dadhania     }                                                             \
458*2a175830SMark Cave-Ayland                                                                   \
459681c2478SNikunj A Dadhania     nb = (nb >= 16) ? 16 : nb;                                    \
460681c2478SNikunj A Dadhania     if (msr_le && !lj) {                                          \
461681c2478SNikunj A Dadhania         for (i = 16; i > 16 - nb; i--) {                          \
462*2a175830SMark Cave-Ayland             cpu_stb_data_ra(env, addr, xt->VsrB(i - 1), GETPC()); \
463681c2478SNikunj A Dadhania             addr = addr_add(env, addr, 1);                        \
464681c2478SNikunj A Dadhania         }                                                         \
465681c2478SNikunj A Dadhania     } else {                                                      \
466681c2478SNikunj A Dadhania         for (i = 0; i < nb; i++) {                                \
467*2a175830SMark Cave-Ayland             cpu_stb_data_ra(env, addr, xt->VsrB(i), GETPC());     \
468681c2478SNikunj A Dadhania             addr = addr_add(env, addr, 1);                        \
469681c2478SNikunj A Dadhania         }                                                         \
470681c2478SNikunj A Dadhania     }                                                             \
471681c2478SNikunj A Dadhania }
472681c2478SNikunj A Dadhania 
473681c2478SNikunj A Dadhania VSX_STXVL(stxvl, 0)
474e122090dSNikunj A Dadhania VSX_STXVL(stxvll, 1)
475681c2478SNikunj A Dadhania #undef VSX_STXVL
4766914bc4fSNikunj A Dadhania #undef GET_NB
4776914bc4fSNikunj A Dadhania #endif /* TARGET_PPC64 */
4786914bc4fSNikunj A Dadhania 
479fcf5ef2aSThomas Huth #undef HI_IDX
480fcf5ef2aSThomas Huth #undef LO_IDX
481fcf5ef2aSThomas Huth 
482fcf5ef2aSThomas Huth void helper_tbegin(CPUPPCState *env)
483fcf5ef2aSThomas Huth {
4845a2c8b9eSDavid Gibson     /*
4855a2c8b9eSDavid Gibson      * As a degenerate implementation, always fail tbegin.  The reason
486fcf5ef2aSThomas Huth      * given is "Nesting overflow".  The "persistent" bit is set,
487fcf5ef2aSThomas Huth      * providing a hint to the error handler to not retry.  The TFIAR
488fcf5ef2aSThomas Huth      * captures the address of the failure, which is this tbegin
4895a2c8b9eSDavid Gibson      * instruction.  Instruction execution will continue with the next
4905a2c8b9eSDavid Gibson      * instruction in memory, which is precisely what we want.
491fcf5ef2aSThomas Huth      */
492fcf5ef2aSThomas Huth 
493fcf5ef2aSThomas Huth     env->spr[SPR_TEXASR] =
494fcf5ef2aSThomas Huth         (1ULL << TEXASR_FAILURE_PERSISTENT) |
495fcf5ef2aSThomas Huth         (1ULL << TEXASR_NESTING_OVERFLOW) |
496fcf5ef2aSThomas Huth         (msr_hv << TEXASR_PRIVILEGE_HV) |
497fcf5ef2aSThomas Huth         (msr_pr << TEXASR_PRIVILEGE_PR) |
498fcf5ef2aSThomas Huth         (1ULL << TEXASR_FAILURE_SUMMARY) |
499fcf5ef2aSThomas Huth         (1ULL << TEXASR_TFIAR_EXACT);
500fcf5ef2aSThomas Huth     env->spr[SPR_TFIAR] = env->nip | (msr_hv << 1) | msr_pr;
501fcf5ef2aSThomas Huth     env->spr[SPR_TFHAR] = env->nip + 4;
502fcf5ef2aSThomas Huth     env->crf[0] = 0xB; /* 0b1010 = transaction failure */
503fcf5ef2aSThomas Huth }
504