xref: /openbmc/qemu/target/ppc/mem_helper.c (revision 4dcf078f094d436866ef793aa25c96fba85ac8d0)
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"
28dcb32f1dSPhilippe Mathieu-Daudé #include "tcg/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 
59bb99b391SRichard Henderson static void *probe_contiguous(CPUPPCState *env, target_ulong addr, uint32_t nb,
60bb99b391SRichard Henderson                               MMUAccessType access_type, int mmu_idx,
61bb99b391SRichard Henderson                               uintptr_t raddr)
62bb99b391SRichard Henderson {
63bb99b391SRichard Henderson     void *host1, *host2;
64bb99b391SRichard Henderson     uint32_t nb_pg1, nb_pg2;
65bb99b391SRichard Henderson 
66bb99b391SRichard Henderson     nb_pg1 = -(addr | TARGET_PAGE_MASK);
67bb99b391SRichard Henderson     if (likely(nb <= nb_pg1)) {
68bb99b391SRichard Henderson         /* The entire operation is on a single page.  */
69bb99b391SRichard Henderson         return probe_access(env, addr, nb, access_type, mmu_idx, raddr);
70bb99b391SRichard Henderson     }
71bb99b391SRichard Henderson 
72bb99b391SRichard Henderson     /* The operation spans two pages.  */
73bb99b391SRichard Henderson     nb_pg2 = nb - nb_pg1;
74bb99b391SRichard Henderson     host1 = probe_access(env, addr, nb_pg1, access_type, mmu_idx, raddr);
75bb99b391SRichard Henderson     addr = addr_add(env, addr, nb_pg1);
76bb99b391SRichard Henderson     host2 = probe_access(env, addr, nb_pg2, access_type, mmu_idx, raddr);
77bb99b391SRichard Henderson 
78bb99b391SRichard Henderson     /* If the two host pages are contiguous, optimize.  */
79bb99b391SRichard Henderson     if (host2 == host1 + nb_pg1) {
80bb99b391SRichard Henderson         return host1;
81bb99b391SRichard Henderson     }
82bb99b391SRichard Henderson     return NULL;
83bb99b391SRichard Henderson }
84bb99b391SRichard Henderson 
85fcf5ef2aSThomas Huth void helper_lmw(CPUPPCState *env, target_ulong addr, uint32_t reg)
86fcf5ef2aSThomas Huth {
872ca2ef49SRichard Henderson     uintptr_t raddr = GETPC();
882ca2ef49SRichard Henderson     int mmu_idx = cpu_mmu_index(env, false);
892ca2ef49SRichard Henderson     void *host = probe_contiguous(env, addr, (32 - reg) * 4,
902ca2ef49SRichard Henderson                                   MMU_DATA_LOAD, mmu_idx, raddr);
912ca2ef49SRichard Henderson 
922ca2ef49SRichard Henderson     if (likely(host)) {
932ca2ef49SRichard Henderson         /* Fast path -- the entire operation is in RAM at host.  */
94fcf5ef2aSThomas Huth         for (; reg < 32; reg++) {
952ca2ef49SRichard Henderson             env->gpr[reg] = (uint32_t)ldl_be_p(host);
962ca2ef49SRichard Henderson             host += 4;
97fcf5ef2aSThomas Huth         }
982ca2ef49SRichard Henderson     } else {
992ca2ef49SRichard Henderson         /* Slow path -- at least some of the operation requires i/o.  */
1002ca2ef49SRichard Henderson         for (; reg < 32; reg++) {
1012ca2ef49SRichard Henderson             env->gpr[reg] = cpu_ldl_mmuidx_ra(env, addr, mmu_idx, raddr);
102fcf5ef2aSThomas Huth             addr = addr_add(env, addr, 4);
103fcf5ef2aSThomas Huth         }
104fcf5ef2aSThomas Huth     }
1052ca2ef49SRichard Henderson }
106fcf5ef2aSThomas Huth 
107fcf5ef2aSThomas Huth void helper_stmw(CPUPPCState *env, target_ulong addr, uint32_t reg)
108fcf5ef2aSThomas Huth {
1092ca2ef49SRichard Henderson     uintptr_t raddr = GETPC();
1102ca2ef49SRichard Henderson     int mmu_idx = cpu_mmu_index(env, false);
1112ca2ef49SRichard Henderson     void *host = probe_contiguous(env, addr, (32 - reg) * 4,
1122ca2ef49SRichard Henderson                                   MMU_DATA_STORE, mmu_idx, raddr);
1132ca2ef49SRichard Henderson 
1142ca2ef49SRichard Henderson     if (likely(host)) {
1152ca2ef49SRichard Henderson         /* Fast path -- the entire operation is in RAM at host.  */
116fcf5ef2aSThomas Huth         for (; reg < 32; reg++) {
1172ca2ef49SRichard Henderson             stl_be_p(host, env->gpr[reg]);
1182ca2ef49SRichard Henderson             host += 4;
119fcf5ef2aSThomas Huth         }
1202ca2ef49SRichard Henderson     } else {
1212ca2ef49SRichard Henderson         /* Slow path -- at least some of the operation requires i/o.  */
1222ca2ef49SRichard Henderson         for (; reg < 32; reg++) {
1232ca2ef49SRichard Henderson             cpu_stl_mmuidx_ra(env, addr, env->gpr[reg], mmu_idx, raddr);
124fcf5ef2aSThomas Huth             addr = addr_add(env, addr, 4);
125fcf5ef2aSThomas Huth         }
126fcf5ef2aSThomas Huth     }
1272ca2ef49SRichard Henderson }
128fcf5ef2aSThomas Huth 
129fcf5ef2aSThomas Huth static void do_lsw(CPUPPCState *env, target_ulong addr, uint32_t nb,
130fcf5ef2aSThomas Huth                    uint32_t reg, uintptr_t raddr)
131fcf5ef2aSThomas Huth {
132bb99b391SRichard Henderson     int mmu_idx;
133bb99b391SRichard Henderson     void *host;
134bb99b391SRichard Henderson     uint32_t val;
135fcf5ef2aSThomas Huth 
136bb99b391SRichard Henderson     if (unlikely(nb == 0)) {
137bb99b391SRichard Henderson         return;
138bb99b391SRichard Henderson     }
139bb99b391SRichard Henderson 
140bb99b391SRichard Henderson     mmu_idx = cpu_mmu_index(env, false);
141bb99b391SRichard Henderson     host = probe_contiguous(env, addr, nb, MMU_DATA_LOAD, mmu_idx, raddr);
142bb99b391SRichard Henderson 
143bb99b391SRichard Henderson     if (likely(host)) {
144bb99b391SRichard Henderson         /* Fast path -- the entire operation is in RAM at host.  */
145fcf5ef2aSThomas Huth         for (; nb > 3; nb -= 4) {
146bb99b391SRichard Henderson             env->gpr[reg] = (uint32_t)ldl_be_p(host);
147bb99b391SRichard Henderson             reg = (reg + 1) % 32;
148bb99b391SRichard Henderson             host += 4;
149bb99b391SRichard Henderson         }
150bb99b391SRichard Henderson         switch (nb) {
151bb99b391SRichard Henderson         default:
152bb99b391SRichard Henderson             return;
153bb99b391SRichard Henderson         case 1:
154bb99b391SRichard Henderson             val = ldub_p(host) << 24;
155bb99b391SRichard Henderson             break;
156bb99b391SRichard Henderson         case 2:
157bb99b391SRichard Henderson             val = lduw_be_p(host) << 16;
158bb99b391SRichard Henderson             break;
159bb99b391SRichard Henderson         case 3:
160bb99b391SRichard Henderson             val = (lduw_be_p(host) << 16) | (ldub_p(host + 2) << 8);
161bb99b391SRichard Henderson             break;
162bb99b391SRichard Henderson         }
163bb99b391SRichard Henderson     } else {
164bb99b391SRichard Henderson         /* Slow path -- at least some of the operation requires i/o.  */
165bb99b391SRichard Henderson         for (; nb > 3; nb -= 4) {
166bb99b391SRichard Henderson             env->gpr[reg] = cpu_ldl_mmuidx_ra(env, addr, mmu_idx, raddr);
167fcf5ef2aSThomas Huth             reg = (reg + 1) % 32;
168fcf5ef2aSThomas Huth             addr = addr_add(env, addr, 4);
169fcf5ef2aSThomas Huth         }
170bb99b391SRichard Henderson         switch (nb) {
171bb99b391SRichard Henderson         default:
172bb99b391SRichard Henderson             return;
173bb99b391SRichard Henderson         case 1:
174bb99b391SRichard Henderson             val = cpu_ldub_mmuidx_ra(env, addr, mmu_idx, raddr) << 24;
175bb99b391SRichard Henderson             break;
176bb99b391SRichard Henderson         case 2:
177bb99b391SRichard Henderson             val = cpu_lduw_mmuidx_ra(env, addr, mmu_idx, raddr) << 16;
178bb99b391SRichard Henderson             break;
179bb99b391SRichard Henderson         case 3:
180bb99b391SRichard Henderson             val = cpu_lduw_mmuidx_ra(env, addr, mmu_idx, raddr) << 16;
181bb99b391SRichard Henderson             addr = addr_add(env, addr, 2);
182bb99b391SRichard Henderson             val |= cpu_ldub_mmuidx_ra(env, addr, mmu_idx, raddr) << 8;
183bb99b391SRichard Henderson             break;
184fcf5ef2aSThomas Huth         }
185fcf5ef2aSThomas Huth     }
186bb99b391SRichard Henderson     env->gpr[reg] = val;
187fcf5ef2aSThomas Huth }
188fcf5ef2aSThomas Huth 
189bb99b391SRichard Henderson void helper_lsw(CPUPPCState *env, target_ulong addr,
190bb99b391SRichard Henderson                 uint32_t nb, uint32_t reg)
191fcf5ef2aSThomas Huth {
192fcf5ef2aSThomas Huth     do_lsw(env, addr, nb, reg, GETPC());
193fcf5ef2aSThomas Huth }
194fcf5ef2aSThomas Huth 
1955a2c8b9eSDavid Gibson /*
1965a2c8b9eSDavid Gibson  * PPC32 specification says we must generate an exception if rA is in
1975a2c8b9eSDavid Gibson  * the range of registers to be loaded.  In an other hand, IBM says
1985a2c8b9eSDavid Gibson  * this is valid, but rA won't be loaded.  For now, I'll follow the
1995a2c8b9eSDavid Gibson  * spec...
200fcf5ef2aSThomas Huth  */
201fcf5ef2aSThomas Huth void helper_lswx(CPUPPCState *env, target_ulong addr, uint32_t reg,
202fcf5ef2aSThomas Huth                  uint32_t ra, uint32_t rb)
203fcf5ef2aSThomas Huth {
204fcf5ef2aSThomas Huth     if (likely(xer_bc != 0)) {
205f0704d78SMarc-André Lureau         int num_used_regs = DIV_ROUND_UP(xer_bc, 4);
206fcf5ef2aSThomas Huth         if (unlikely((ra != 0 && lsw_reg_in_range(reg, num_used_regs, ra)) ||
207fcf5ef2aSThomas Huth                      lsw_reg_in_range(reg, num_used_regs, rb))) {
208fcf5ef2aSThomas Huth             raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
209fcf5ef2aSThomas Huth                                    POWERPC_EXCP_INVAL |
210fcf5ef2aSThomas Huth                                    POWERPC_EXCP_INVAL_LSWX, GETPC());
211fcf5ef2aSThomas Huth         } else {
212fcf5ef2aSThomas Huth             do_lsw(env, addr, xer_bc, reg, GETPC());
213fcf5ef2aSThomas Huth         }
214fcf5ef2aSThomas Huth     }
215fcf5ef2aSThomas Huth }
216fcf5ef2aSThomas Huth 
217fcf5ef2aSThomas Huth void helper_stsw(CPUPPCState *env, target_ulong addr, uint32_t nb,
218fcf5ef2aSThomas Huth                  uint32_t reg)
219fcf5ef2aSThomas Huth {
220bb99b391SRichard Henderson     uintptr_t raddr = GETPC();
221bb99b391SRichard Henderson     int mmu_idx;
222bb99b391SRichard Henderson     void *host;
223bb99b391SRichard Henderson     uint32_t val;
224fcf5ef2aSThomas Huth 
225bb99b391SRichard Henderson     if (unlikely(nb == 0)) {
226bb99b391SRichard Henderson         return;
227bb99b391SRichard Henderson     }
228bb99b391SRichard Henderson 
229bb99b391SRichard Henderson     mmu_idx = cpu_mmu_index(env, false);
230bb99b391SRichard Henderson     host = probe_contiguous(env, addr, nb, MMU_DATA_STORE, mmu_idx, raddr);
231bb99b391SRichard Henderson 
232bb99b391SRichard Henderson     if (likely(host)) {
233bb99b391SRichard Henderson         /* Fast path -- the entire operation is in RAM at host.  */
234fcf5ef2aSThomas Huth         for (; nb > 3; nb -= 4) {
235bb99b391SRichard Henderson             stl_be_p(host, env->gpr[reg]);
236bb99b391SRichard Henderson             reg = (reg + 1) % 32;
237bb99b391SRichard Henderson             host += 4;
238bb99b391SRichard Henderson         }
239bb99b391SRichard Henderson         val = env->gpr[reg];
240bb99b391SRichard Henderson         switch (nb) {
241bb99b391SRichard Henderson         case 1:
242bb99b391SRichard Henderson             stb_p(host, val >> 24);
243bb99b391SRichard Henderson             break;
244bb99b391SRichard Henderson         case 2:
245bb99b391SRichard Henderson             stw_be_p(host, val >> 16);
246bb99b391SRichard Henderson             break;
247bb99b391SRichard Henderson         case 3:
248bb99b391SRichard Henderson             stw_be_p(host, val >> 16);
249bb99b391SRichard Henderson             stb_p(host + 2, val >> 8);
250bb99b391SRichard Henderson             break;
251bb99b391SRichard Henderson         }
252bb99b391SRichard Henderson     } else {
253bb99b391SRichard Henderson         for (; nb > 3; nb -= 4) {
254bb99b391SRichard Henderson             cpu_stl_mmuidx_ra(env, addr, env->gpr[reg], mmu_idx, raddr);
255fcf5ef2aSThomas Huth             reg = (reg + 1) % 32;
256fcf5ef2aSThomas Huth             addr = addr_add(env, addr, 4);
257fcf5ef2aSThomas Huth         }
258bb99b391SRichard Henderson         val = env->gpr[reg];
259bb99b391SRichard Henderson         switch (nb) {
260bb99b391SRichard Henderson         case 1:
261bb99b391SRichard Henderson             cpu_stb_mmuidx_ra(env, addr, val >> 24, mmu_idx, raddr);
262bb99b391SRichard Henderson             break;
263bb99b391SRichard Henderson         case 2:
264bb99b391SRichard Henderson             cpu_stw_mmuidx_ra(env, addr, val >> 16, mmu_idx, raddr);
265bb99b391SRichard Henderson             break;
266bb99b391SRichard Henderson         case 3:
267bb99b391SRichard Henderson             cpu_stw_mmuidx_ra(env, addr, val >> 16, mmu_idx, raddr);
268bb99b391SRichard Henderson             addr = addr_add(env, addr, 2);
269bb99b391SRichard Henderson             cpu_stb_mmuidx_ra(env, addr, val >> 8, mmu_idx, raddr);
270bb99b391SRichard Henderson             break;
271fcf5ef2aSThomas Huth         }
272fcf5ef2aSThomas Huth     }
273fcf5ef2aSThomas Huth }
274fcf5ef2aSThomas Huth 
27550728199SRoman Kapl static void dcbz_common(CPUPPCState *env, target_ulong addr,
27650728199SRoman Kapl                         uint32_t opcode, bool epid, uintptr_t retaddr)
277fcf5ef2aSThomas Huth {
278fcf5ef2aSThomas Huth     target_ulong mask, dcbz_size = env->dcache_line_size;
279fcf5ef2aSThomas Huth     uint32_t i;
280fcf5ef2aSThomas Huth     void *haddr;
28150728199SRoman Kapl     int mmu_idx = epid ? PPC_TLB_EPID_STORE : env->dmmu_idx;
282fcf5ef2aSThomas Huth 
283fcf5ef2aSThomas Huth #if defined(TARGET_PPC64)
284fcf5ef2aSThomas Huth     /* Check for dcbz vs dcbzl on 970 */
285fcf5ef2aSThomas Huth     if (env->excp_model == POWERPC_EXCP_970 &&
286fcf5ef2aSThomas Huth         !(opcode & 0x00200000) && ((env->spr[SPR_970_HID5] >> 7) & 0x3) == 1) {
287fcf5ef2aSThomas Huth         dcbz_size = 32;
288fcf5ef2aSThomas Huth     }
289fcf5ef2aSThomas Huth #endif
290fcf5ef2aSThomas Huth 
291fcf5ef2aSThomas Huth     /* Align address */
292fcf5ef2aSThomas Huth     mask = ~(dcbz_size - 1);
293fcf5ef2aSThomas Huth     addr &= mask;
294fcf5ef2aSThomas Huth 
295fcf5ef2aSThomas Huth     /* Check reservation */
2961cbddf6dSRichard Henderson     if ((env->reserve_addr & mask) == addr)  {
297fcf5ef2aSThomas Huth         env->reserve_addr = (target_ulong)-1ULL;
298fcf5ef2aSThomas Huth     }
299fcf5ef2aSThomas Huth 
300fcf5ef2aSThomas Huth     /* Try fast path translate */
301*4dcf078fSRichard Henderson     haddr = probe_write(env, addr, dcbz_size, mmu_idx, retaddr);
302fcf5ef2aSThomas Huth     if (haddr) {
303fcf5ef2aSThomas Huth         memset(haddr, 0, dcbz_size);
304fcf5ef2aSThomas Huth     } else {
305fcf5ef2aSThomas Huth         /* Slow path */
306fcf5ef2aSThomas Huth         for (i = 0; i < dcbz_size; i += 8) {
3075a376e4fSRichard Henderson             cpu_stq_mmuidx_ra(env, addr + i, 0, mmu_idx, retaddr);
308fcf5ef2aSThomas Huth         }
309fcf5ef2aSThomas Huth     }
31050728199SRoman Kapl }
31150728199SRoman Kapl 
31250728199SRoman Kapl void helper_dcbz(CPUPPCState *env, target_ulong addr, uint32_t opcode)
31350728199SRoman Kapl {
31450728199SRoman Kapl     dcbz_common(env, addr, opcode, false, GETPC());
31550728199SRoman Kapl }
31650728199SRoman Kapl 
31750728199SRoman Kapl void helper_dcbzep(CPUPPCState *env, target_ulong addr, uint32_t opcode)
31850728199SRoman Kapl {
31950728199SRoman Kapl     dcbz_common(env, addr, opcode, true, GETPC());
32050728199SRoman Kapl }
321fcf5ef2aSThomas Huth 
322fcf5ef2aSThomas Huth void helper_icbi(CPUPPCState *env, target_ulong addr)
323fcf5ef2aSThomas Huth {
324fcf5ef2aSThomas Huth     addr &= ~(env->dcache_line_size - 1);
3255a2c8b9eSDavid Gibson     /*
3265a2c8b9eSDavid Gibson      * Invalidate one cache line :
327fcf5ef2aSThomas Huth      * PowerPC specification says this is to be treated like a load
328fcf5ef2aSThomas Huth      * (not a fetch) by the MMU. To be sure it will be so,
329fcf5ef2aSThomas Huth      * do the load "by hand".
330fcf5ef2aSThomas Huth      */
331fcf5ef2aSThomas Huth     cpu_ldl_data_ra(env, addr, GETPC());
332fcf5ef2aSThomas Huth }
333fcf5ef2aSThomas Huth 
33450728199SRoman Kapl void helper_icbiep(CPUPPCState *env, target_ulong addr)
33550728199SRoman Kapl {
33650728199SRoman Kapl #if !defined(CONFIG_USER_ONLY)
33750728199SRoman Kapl     /* See comments above */
33850728199SRoman Kapl     addr &= ~(env->dcache_line_size - 1);
3395a376e4fSRichard Henderson     cpu_ldl_mmuidx_ra(env, addr, PPC_TLB_EPID_LOAD, GETPC());
34050728199SRoman Kapl #endif
34150728199SRoman Kapl }
34250728199SRoman Kapl 
343fcf5ef2aSThomas Huth /* XXX: to be tested */
344fcf5ef2aSThomas Huth target_ulong helper_lscbx(CPUPPCState *env, target_ulong addr, uint32_t reg,
345fcf5ef2aSThomas Huth                           uint32_t ra, uint32_t rb)
346fcf5ef2aSThomas Huth {
347fcf5ef2aSThomas Huth     int i, c, d;
348fcf5ef2aSThomas Huth 
349fcf5ef2aSThomas Huth     d = 24;
350fcf5ef2aSThomas Huth     for (i = 0; i < xer_bc; i++) {
351fcf5ef2aSThomas Huth         c = cpu_ldub_data_ra(env, addr, GETPC());
352fcf5ef2aSThomas Huth         addr = addr_add(env, addr, 1);
353fcf5ef2aSThomas Huth         /* ra (if not 0) and rb are never modified */
354fcf5ef2aSThomas Huth         if (likely(reg != rb && (ra == 0 || reg != ra))) {
355fcf5ef2aSThomas Huth             env->gpr[reg] = (env->gpr[reg] & ~(0xFF << d)) | (c << d);
356fcf5ef2aSThomas Huth         }
357fcf5ef2aSThomas Huth         if (unlikely(c == xer_cmp)) {
358fcf5ef2aSThomas Huth             break;
359fcf5ef2aSThomas Huth         }
360fcf5ef2aSThomas Huth         if (likely(d != 0)) {
361fcf5ef2aSThomas Huth             d -= 8;
362fcf5ef2aSThomas Huth         } else {
363fcf5ef2aSThomas Huth             d = 24;
364fcf5ef2aSThomas Huth             reg++;
365fcf5ef2aSThomas Huth             reg = reg & 0x1F;
366fcf5ef2aSThomas Huth         }
367fcf5ef2aSThomas Huth     }
368fcf5ef2aSThomas Huth     return i;
369fcf5ef2aSThomas Huth }
370fcf5ef2aSThomas Huth 
371f34ec0f6SRichard Henderson #ifdef TARGET_PPC64
37294bf2658SRichard Henderson uint64_t helper_lq_le_parallel(CPUPPCState *env, target_ulong addr,
37394bf2658SRichard Henderson                                uint32_t opidx)
37494bf2658SRichard Henderson {
375f34ec0f6SRichard Henderson     Int128 ret;
376f34ec0f6SRichard Henderson 
377f34ec0f6SRichard Henderson     /* We will have raised EXCP_ATOMIC from the translator.  */
378f34ec0f6SRichard Henderson     assert(HAVE_ATOMIC128);
379f34ec0f6SRichard Henderson     ret = helper_atomic_ldo_le_mmu(env, addr, opidx, GETPC());
38094bf2658SRichard Henderson     env->retxh = int128_gethi(ret);
38194bf2658SRichard Henderson     return int128_getlo(ret);
38294bf2658SRichard Henderson }
38394bf2658SRichard Henderson 
38494bf2658SRichard Henderson uint64_t helper_lq_be_parallel(CPUPPCState *env, target_ulong addr,
38594bf2658SRichard Henderson                                uint32_t opidx)
38694bf2658SRichard Henderson {
387f34ec0f6SRichard Henderson     Int128 ret;
388f34ec0f6SRichard Henderson 
389f34ec0f6SRichard Henderson     /* We will have raised EXCP_ATOMIC from the translator.  */
390f34ec0f6SRichard Henderson     assert(HAVE_ATOMIC128);
391f34ec0f6SRichard Henderson     ret = helper_atomic_ldo_be_mmu(env, addr, opidx, GETPC());
39294bf2658SRichard Henderson     env->retxh = int128_gethi(ret);
39394bf2658SRichard Henderson     return int128_getlo(ret);
39494bf2658SRichard Henderson }
395f89ced5fSRichard Henderson 
396f89ced5fSRichard Henderson void helper_stq_le_parallel(CPUPPCState *env, target_ulong addr,
397f89ced5fSRichard Henderson                             uint64_t lo, uint64_t hi, uint32_t opidx)
398f89ced5fSRichard Henderson {
399f34ec0f6SRichard Henderson     Int128 val;
400f34ec0f6SRichard Henderson 
401f34ec0f6SRichard Henderson     /* We will have raised EXCP_ATOMIC from the translator.  */
402f34ec0f6SRichard Henderson     assert(HAVE_ATOMIC128);
403f34ec0f6SRichard Henderson     val = int128_make128(lo, hi);
404f89ced5fSRichard Henderson     helper_atomic_sto_le_mmu(env, addr, val, opidx, GETPC());
405f89ced5fSRichard Henderson }
406f89ced5fSRichard Henderson 
407f89ced5fSRichard Henderson void helper_stq_be_parallel(CPUPPCState *env, target_ulong addr,
408f89ced5fSRichard Henderson                             uint64_t lo, uint64_t hi, uint32_t opidx)
409f89ced5fSRichard Henderson {
410f34ec0f6SRichard Henderson     Int128 val;
411f34ec0f6SRichard Henderson 
412f34ec0f6SRichard Henderson     /* We will have raised EXCP_ATOMIC from the translator.  */
413f34ec0f6SRichard Henderson     assert(HAVE_ATOMIC128);
414f34ec0f6SRichard Henderson     val = int128_make128(lo, hi);
415f89ced5fSRichard Henderson     helper_atomic_sto_be_mmu(env, addr, val, opidx, GETPC());
416f89ced5fSRichard Henderson }
4174a9b3c5dSRichard Henderson 
4184a9b3c5dSRichard Henderson uint32_t helper_stqcx_le_parallel(CPUPPCState *env, target_ulong addr,
4194a9b3c5dSRichard Henderson                                   uint64_t new_lo, uint64_t new_hi,
4204a9b3c5dSRichard Henderson                                   uint32_t opidx)
4214a9b3c5dSRichard Henderson {
4224a9b3c5dSRichard Henderson     bool success = false;
4234a9b3c5dSRichard Henderson 
424f34ec0f6SRichard Henderson     /* We will have raised EXCP_ATOMIC from the translator.  */
425f34ec0f6SRichard Henderson     assert(HAVE_CMPXCHG128);
426f34ec0f6SRichard Henderson 
4274a9b3c5dSRichard Henderson     if (likely(addr == env->reserve_addr)) {
4284a9b3c5dSRichard Henderson         Int128 oldv, cmpv, newv;
4294a9b3c5dSRichard Henderson 
4304a9b3c5dSRichard Henderson         cmpv = int128_make128(env->reserve_val2, env->reserve_val);
4314a9b3c5dSRichard Henderson         newv = int128_make128(new_lo, new_hi);
4324a9b3c5dSRichard Henderson         oldv = helper_atomic_cmpxchgo_le_mmu(env, addr, cmpv, newv,
4334a9b3c5dSRichard Henderson                                              opidx, GETPC());
4344a9b3c5dSRichard Henderson         success = int128_eq(oldv, cmpv);
4354a9b3c5dSRichard Henderson     }
4364a9b3c5dSRichard Henderson     env->reserve_addr = -1;
4374a9b3c5dSRichard Henderson     return env->so + success * CRF_EQ_BIT;
4384a9b3c5dSRichard Henderson }
4394a9b3c5dSRichard Henderson 
4404a9b3c5dSRichard Henderson uint32_t helper_stqcx_be_parallel(CPUPPCState *env, target_ulong addr,
4414a9b3c5dSRichard Henderson                                   uint64_t new_lo, uint64_t new_hi,
4424a9b3c5dSRichard Henderson                                   uint32_t opidx)
4434a9b3c5dSRichard Henderson {
4444a9b3c5dSRichard Henderson     bool success = false;
4454a9b3c5dSRichard Henderson 
446f34ec0f6SRichard Henderson     /* We will have raised EXCP_ATOMIC from the translator.  */
447f34ec0f6SRichard Henderson     assert(HAVE_CMPXCHG128);
448f34ec0f6SRichard Henderson 
4494a9b3c5dSRichard Henderson     if (likely(addr == env->reserve_addr)) {
4504a9b3c5dSRichard Henderson         Int128 oldv, cmpv, newv;
4514a9b3c5dSRichard Henderson 
4524a9b3c5dSRichard Henderson         cmpv = int128_make128(env->reserve_val2, env->reserve_val);
4534a9b3c5dSRichard Henderson         newv = int128_make128(new_lo, new_hi);
4544a9b3c5dSRichard Henderson         oldv = helper_atomic_cmpxchgo_be_mmu(env, addr, cmpv, newv,
4554a9b3c5dSRichard Henderson                                              opidx, GETPC());
4564a9b3c5dSRichard Henderson         success = int128_eq(oldv, cmpv);
4574a9b3c5dSRichard Henderson     }
4584a9b3c5dSRichard Henderson     env->reserve_addr = -1;
4594a9b3c5dSRichard Henderson     return env->so + success * CRF_EQ_BIT;
4604a9b3c5dSRichard Henderson }
46194bf2658SRichard Henderson #endif
46294bf2658SRichard Henderson 
463fcf5ef2aSThomas Huth /*****************************************************************************/
464fcf5ef2aSThomas Huth /* Altivec extension helpers */
465fcf5ef2aSThomas Huth #if defined(HOST_WORDS_BIGENDIAN)
466fcf5ef2aSThomas Huth #define HI_IDX 0
467fcf5ef2aSThomas Huth #define LO_IDX 1
468fcf5ef2aSThomas Huth #else
469fcf5ef2aSThomas Huth #define HI_IDX 1
470fcf5ef2aSThomas Huth #define LO_IDX 0
471fcf5ef2aSThomas Huth #endif
472fcf5ef2aSThomas Huth 
4735a2c8b9eSDavid Gibson /*
4745a2c8b9eSDavid Gibson  * We use msr_le to determine index ordering in a vector.  However,
4755a2c8b9eSDavid Gibson  * byteswapping is not simply controlled by msr_le.  We also need to
4765a2c8b9eSDavid Gibson  * take into account endianness of the target.  This is done for the
4775a2c8b9eSDavid Gibson  * little-endian PPC64 user-mode target.
4785a2c8b9eSDavid Gibson  */
479fcf5ef2aSThomas Huth 
480fcf5ef2aSThomas Huth #define LVE(name, access, swap, element)                        \
481fcf5ef2aSThomas Huth     void helper_##name(CPUPPCState *env, ppc_avr_t *r,          \
482fcf5ef2aSThomas Huth                        target_ulong addr)                       \
483fcf5ef2aSThomas Huth     {                                                           \
484fcf5ef2aSThomas Huth         size_t n_elems = ARRAY_SIZE(r->element);                \
485fcf5ef2aSThomas Huth         int adjust = HI_IDX * (n_elems - 1);                    \
486fcf5ef2aSThomas Huth         int sh = sizeof(r->element[0]) >> 1;                    \
487fcf5ef2aSThomas Huth         int index = (addr & 0xf) >> sh;                         \
488fcf5ef2aSThomas Huth         if (msr_le) {                                           \
489fcf5ef2aSThomas Huth             index = n_elems - index - 1;                        \
490fcf5ef2aSThomas Huth         }                                                       \
491fcf5ef2aSThomas Huth                                                                 \
492fcf5ef2aSThomas Huth         if (needs_byteswap(env)) {                              \
493fcf5ef2aSThomas Huth             r->element[LO_IDX ? index : (adjust - index)] =     \
494fcf5ef2aSThomas Huth                 swap(access(env, addr, GETPC()));               \
495fcf5ef2aSThomas Huth         } else {                                                \
496fcf5ef2aSThomas Huth             r->element[LO_IDX ? index : (adjust - index)] =     \
497fcf5ef2aSThomas Huth                 access(env, addr, GETPC());                     \
498fcf5ef2aSThomas Huth         }                                                       \
499fcf5ef2aSThomas Huth     }
500fcf5ef2aSThomas Huth #define I(x) (x)
501fcf5ef2aSThomas Huth LVE(lvebx, cpu_ldub_data_ra, I, u8)
502fcf5ef2aSThomas Huth LVE(lvehx, cpu_lduw_data_ra, bswap16, u16)
503fcf5ef2aSThomas Huth LVE(lvewx, cpu_ldl_data_ra, bswap32, u32)
504fcf5ef2aSThomas Huth #undef I
505fcf5ef2aSThomas Huth #undef LVE
506fcf5ef2aSThomas Huth 
507fcf5ef2aSThomas Huth #define STVE(name, access, swap, element)                               \
508fcf5ef2aSThomas Huth     void helper_##name(CPUPPCState *env, ppc_avr_t *r,                  \
509fcf5ef2aSThomas Huth                        target_ulong addr)                               \
510fcf5ef2aSThomas Huth     {                                                                   \
511fcf5ef2aSThomas Huth         size_t n_elems = ARRAY_SIZE(r->element);                        \
512fcf5ef2aSThomas Huth         int adjust = HI_IDX * (n_elems - 1);                            \
513fcf5ef2aSThomas Huth         int sh = sizeof(r->element[0]) >> 1;                            \
514fcf5ef2aSThomas Huth         int index = (addr & 0xf) >> sh;                                 \
515fcf5ef2aSThomas Huth         if (msr_le) {                                                   \
516fcf5ef2aSThomas Huth             index = n_elems - index - 1;                                \
517fcf5ef2aSThomas Huth         }                                                               \
518fcf5ef2aSThomas Huth                                                                         \
519fcf5ef2aSThomas Huth         if (needs_byteswap(env)) {                                      \
520fcf5ef2aSThomas Huth             access(env, addr, swap(r->element[LO_IDX ? index :          \
521fcf5ef2aSThomas Huth                                               (adjust - index)]),       \
522fcf5ef2aSThomas Huth                         GETPC());                                       \
523fcf5ef2aSThomas Huth         } else {                                                        \
524fcf5ef2aSThomas Huth             access(env, addr, r->element[LO_IDX ? index :               \
525fcf5ef2aSThomas Huth                                          (adjust - index)], GETPC());   \
526fcf5ef2aSThomas Huth         }                                                               \
527fcf5ef2aSThomas Huth     }
528fcf5ef2aSThomas Huth #define I(x) (x)
529fcf5ef2aSThomas Huth STVE(stvebx, cpu_stb_data_ra, I, u8)
530fcf5ef2aSThomas Huth STVE(stvehx, cpu_stw_data_ra, bswap16, u16)
531fcf5ef2aSThomas Huth STVE(stvewx, cpu_stl_data_ra, bswap32, u32)
532fcf5ef2aSThomas Huth #undef I
533fcf5ef2aSThomas Huth #undef LVE
534fcf5ef2aSThomas Huth 
5356914bc4fSNikunj A Dadhania #ifdef TARGET_PPC64
5366914bc4fSNikunj A Dadhania #define GET_NB(rb) ((rb >> 56) & 0xFF)
5376914bc4fSNikunj A Dadhania 
5386914bc4fSNikunj A Dadhania #define VSX_LXVL(name, lj)                                              \
5396914bc4fSNikunj A Dadhania void helper_##name(CPUPPCState *env, target_ulong addr,                 \
5402aba168eSMark Cave-Ayland                    ppc_vsr_t *xt, target_ulong rb)                      \
5416914bc4fSNikunj A Dadhania {                                                                       \
5422a175830SMark Cave-Ayland     ppc_vsr_t t;                                                        \
5436914bc4fSNikunj A Dadhania     uint64_t nb = GET_NB(rb);                                           \
5442a175830SMark Cave-Ayland     int i;                                                              \
5456914bc4fSNikunj A Dadhania                                                                         \
5462a175830SMark Cave-Ayland     t.s128 = int128_zero();                                             \
5476914bc4fSNikunj A Dadhania     if (nb) {                                                           \
5486914bc4fSNikunj A Dadhania         nb = (nb >= 16) ? 16 : nb;                                      \
5496914bc4fSNikunj A Dadhania         if (msr_le && !lj) {                                            \
5506914bc4fSNikunj A Dadhania             for (i = 16; i > 16 - nb; i--) {                            \
5512a175830SMark Cave-Ayland                 t.VsrB(i - 1) = cpu_ldub_data_ra(env, addr, GETPC());   \
5526914bc4fSNikunj A Dadhania                 addr = addr_add(env, addr, 1);                          \
5536914bc4fSNikunj A Dadhania             }                                                           \
5546914bc4fSNikunj A Dadhania         } else {                                                        \
5556914bc4fSNikunj A Dadhania             for (i = 0; i < nb; i++) {                                  \
5562a175830SMark Cave-Ayland                 t.VsrB(i) = cpu_ldub_data_ra(env, addr, GETPC());       \
5576914bc4fSNikunj A Dadhania                 addr = addr_add(env, addr, 1);                          \
5586914bc4fSNikunj A Dadhania             }                                                           \
5596914bc4fSNikunj A Dadhania         }                                                               \
5606914bc4fSNikunj A Dadhania     }                                                                   \
5612a175830SMark Cave-Ayland     *xt = t;                                                            \
5626914bc4fSNikunj A Dadhania }
5636914bc4fSNikunj A Dadhania 
5646914bc4fSNikunj A Dadhania VSX_LXVL(lxvl, 0)
565176e44e7SNikunj A Dadhania VSX_LXVL(lxvll, 1)
5666914bc4fSNikunj A Dadhania #undef VSX_LXVL
567681c2478SNikunj A Dadhania 
568681c2478SNikunj A Dadhania #define VSX_STXVL(name, lj)                                       \
569681c2478SNikunj A Dadhania void helper_##name(CPUPPCState *env, target_ulong addr,           \
5702aba168eSMark Cave-Ayland                    ppc_vsr_t *xt, target_ulong rb)                \
571681c2478SNikunj A Dadhania {                                                                 \
572681c2478SNikunj A Dadhania     target_ulong nb = GET_NB(rb);                                 \
5732a175830SMark Cave-Ayland     int i;                                                        \
574681c2478SNikunj A Dadhania                                                                   \
575681c2478SNikunj A Dadhania     if (!nb) {                                                    \
576681c2478SNikunj A Dadhania         return;                                                   \
577681c2478SNikunj A Dadhania     }                                                             \
5782a175830SMark Cave-Ayland                                                                   \
579681c2478SNikunj A Dadhania     nb = (nb >= 16) ? 16 : nb;                                    \
580681c2478SNikunj A Dadhania     if (msr_le && !lj) {                                          \
581681c2478SNikunj A Dadhania         for (i = 16; i > 16 - nb; i--) {                          \
5822a175830SMark Cave-Ayland             cpu_stb_data_ra(env, addr, xt->VsrB(i - 1), GETPC()); \
583681c2478SNikunj A Dadhania             addr = addr_add(env, addr, 1);                        \
584681c2478SNikunj A Dadhania         }                                                         \
585681c2478SNikunj A Dadhania     } else {                                                      \
586681c2478SNikunj A Dadhania         for (i = 0; i < nb; i++) {                                \
5872a175830SMark Cave-Ayland             cpu_stb_data_ra(env, addr, xt->VsrB(i), GETPC());     \
588681c2478SNikunj A Dadhania             addr = addr_add(env, addr, 1);                        \
589681c2478SNikunj A Dadhania         }                                                         \
590681c2478SNikunj A Dadhania     }                                                             \
591681c2478SNikunj A Dadhania }
592681c2478SNikunj A Dadhania 
593681c2478SNikunj A Dadhania VSX_STXVL(stxvl, 0)
594e122090dSNikunj A Dadhania VSX_STXVL(stxvll, 1)
595681c2478SNikunj A Dadhania #undef VSX_STXVL
5966914bc4fSNikunj A Dadhania #undef GET_NB
5976914bc4fSNikunj A Dadhania #endif /* TARGET_PPC64 */
5986914bc4fSNikunj A Dadhania 
599fcf5ef2aSThomas Huth #undef HI_IDX
600fcf5ef2aSThomas Huth #undef LO_IDX
601fcf5ef2aSThomas Huth 
602fcf5ef2aSThomas Huth void helper_tbegin(CPUPPCState *env)
603fcf5ef2aSThomas Huth {
6045a2c8b9eSDavid Gibson     /*
6055a2c8b9eSDavid Gibson      * As a degenerate implementation, always fail tbegin.  The reason
606fcf5ef2aSThomas Huth      * given is "Nesting overflow".  The "persistent" bit is set,
607fcf5ef2aSThomas Huth      * providing a hint to the error handler to not retry.  The TFIAR
608fcf5ef2aSThomas Huth      * captures the address of the failure, which is this tbegin
6095a2c8b9eSDavid Gibson      * instruction.  Instruction execution will continue with the next
6105a2c8b9eSDavid Gibson      * instruction in memory, which is precisely what we want.
611fcf5ef2aSThomas Huth      */
612fcf5ef2aSThomas Huth 
613fcf5ef2aSThomas Huth     env->spr[SPR_TEXASR] =
614fcf5ef2aSThomas Huth         (1ULL << TEXASR_FAILURE_PERSISTENT) |
615fcf5ef2aSThomas Huth         (1ULL << TEXASR_NESTING_OVERFLOW) |
616fcf5ef2aSThomas Huth         (msr_hv << TEXASR_PRIVILEGE_HV) |
617fcf5ef2aSThomas Huth         (msr_pr << TEXASR_PRIVILEGE_PR) |
618fcf5ef2aSThomas Huth         (1ULL << TEXASR_FAILURE_SUMMARY) |
619fcf5ef2aSThomas Huth         (1ULL << TEXASR_TFIAR_EXACT);
620fcf5ef2aSThomas Huth     env->spr[SPR_TFIAR] = env->nip | (msr_hv << 1) | msr_pr;
621fcf5ef2aSThomas Huth     env->spr[SPR_TFHAR] = env->nip + 4;
622fcf5ef2aSThomas Huth     env->crf[0] = 0xB; /* 0b1010 = transaction failure */
623fcf5ef2aSThomas Huth }
624