xref: /openbmc/qemu/tcg/tcg-op-ldst.c (revision 259ebed4)
1 /*
2  * Tiny Code Generator for QEMU
3  *
4  * Copyright (c) 2008 Fabrice Bellard
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
25 #include "qemu/osdep.h"
26 #include "tcg/tcg.h"
27 #include "tcg/tcg-temp-internal.h"
28 #include "tcg/tcg-op-common.h"
29 #include "tcg/tcg-mo.h"
30 #include "exec/translation-block.h"
31 #include "exec/plugin-gen.h"
32 #include "tcg-internal.h"
33 
34 
35 static void check_max_alignment(unsigned a_bits)
36 {
37     /*
38      * The requested alignment cannot overlap the TLB flags.
39      * FIXME: Must keep the count up-to-date with "exec/cpu-all.h".
40      */
41     if (tcg_use_softmmu) {
42         tcg_debug_assert(a_bits + 5 <= tcg_ctx->page_bits);
43     }
44 }
45 
46 static MemOp tcg_canonicalize_memop(MemOp op, bool is64, bool st)
47 {
48     unsigned a_bits = get_alignment_bits(op);
49 
50     check_max_alignment(a_bits);
51 
52     /* Prefer MO_ALIGN+MO_XX over MO_ALIGN_XX+MO_XX */
53     if (a_bits == (op & MO_SIZE)) {
54         op = (op & ~MO_AMASK) | MO_ALIGN;
55     }
56 
57     switch (op & MO_SIZE) {
58     case MO_8:
59         op &= ~MO_BSWAP;
60         break;
61     case MO_16:
62         break;
63     case MO_32:
64         if (!is64) {
65             op &= ~MO_SIGN;
66         }
67         break;
68     case MO_64:
69         if (is64) {
70             op &= ~MO_SIGN;
71             break;
72         }
73         /* fall through */
74     default:
75         g_assert_not_reached();
76     }
77     if (st) {
78         op &= ~MO_SIGN;
79     }
80     return op;
81 }
82 
83 static void gen_ldst(TCGOpcode opc, TCGTemp *vl, TCGTemp *vh,
84                      TCGTemp *addr, MemOpIdx oi)
85 {
86     if (TCG_TARGET_REG_BITS == 64 || tcg_ctx->addr_type == TCG_TYPE_I32) {
87         if (vh) {
88             tcg_gen_op4(opc, temp_arg(vl), temp_arg(vh), temp_arg(addr), oi);
89         } else {
90             tcg_gen_op3(opc, temp_arg(vl), temp_arg(addr), oi);
91         }
92     } else {
93         /* See TCGV_LOW/HIGH. */
94         TCGTemp *al = addr + HOST_BIG_ENDIAN;
95         TCGTemp *ah = addr + !HOST_BIG_ENDIAN;
96 
97         if (vh) {
98             tcg_gen_op5(opc, temp_arg(vl), temp_arg(vh),
99                         temp_arg(al), temp_arg(ah), oi);
100         } else {
101             tcg_gen_op4(opc, temp_arg(vl), temp_arg(al), temp_arg(ah), oi);
102         }
103     }
104 }
105 
106 static void gen_ldst_i64(TCGOpcode opc, TCGv_i64 v, TCGTemp *addr, MemOpIdx oi)
107 {
108     if (TCG_TARGET_REG_BITS == 32) {
109         TCGTemp *vl = tcgv_i32_temp(TCGV_LOW(v));
110         TCGTemp *vh = tcgv_i32_temp(TCGV_HIGH(v));
111         gen_ldst(opc, vl, vh, addr, oi);
112     } else {
113         gen_ldst(opc, tcgv_i64_temp(v), NULL, addr, oi);
114     }
115 }
116 
117 static void tcg_gen_req_mo(TCGBar type)
118 {
119     type &= tcg_ctx->guest_mo;
120     type &= ~TCG_TARGET_DEFAULT_MO;
121     if (type) {
122         tcg_gen_mb(type | TCG_BAR_SC);
123     }
124 }
125 
126 /* Only required for loads, where value might overlap addr. */
127 static TCGv_i64 plugin_maybe_preserve_addr(TCGTemp *addr)
128 {
129 #ifdef CONFIG_PLUGIN
130     if (tcg_ctx->plugin_insn != NULL) {
131         /* Save a copy of the vaddr for use after a load.  */
132         TCGv_i64 temp = tcg_temp_ebb_new_i64();
133         if (tcg_ctx->addr_type == TCG_TYPE_I32) {
134             tcg_gen_extu_i32_i64(temp, temp_tcgv_i32(addr));
135         } else {
136             tcg_gen_mov_i64(temp, temp_tcgv_i64(addr));
137         }
138         return temp;
139     }
140 #endif
141     return NULL;
142 }
143 
144 static void
145 plugin_gen_mem_callbacks(TCGv_i64 copy_addr, TCGTemp *orig_addr, MemOpIdx oi,
146                          enum qemu_plugin_mem_rw rw)
147 {
148 #ifdef CONFIG_PLUGIN
149     if (tcg_ctx->plugin_insn != NULL) {
150         qemu_plugin_meminfo_t info = make_plugin_meminfo(oi, rw);
151 
152         if (tcg_ctx->addr_type == TCG_TYPE_I32) {
153             if (!copy_addr) {
154                 copy_addr = tcg_temp_ebb_new_i64();
155                 tcg_gen_extu_i32_i64(copy_addr, temp_tcgv_i32(orig_addr));
156             }
157             plugin_gen_empty_mem_callback(copy_addr, info);
158             tcg_temp_free_i64(copy_addr);
159         } else {
160             if (copy_addr) {
161                 plugin_gen_empty_mem_callback(copy_addr, info);
162                 tcg_temp_free_i64(copy_addr);
163             } else {
164                 plugin_gen_empty_mem_callback(temp_tcgv_i64(orig_addr), info);
165             }
166         }
167     }
168 #endif
169 }
170 
171 static void tcg_gen_qemu_ld_i32_int(TCGv_i32 val, TCGTemp *addr,
172                                     TCGArg idx, MemOp memop)
173 {
174     MemOp orig_memop;
175     MemOpIdx orig_oi, oi;
176     TCGv_i64 copy_addr;
177     TCGOpcode opc;
178 
179     tcg_gen_req_mo(TCG_MO_LD_LD | TCG_MO_ST_LD);
180     orig_memop = memop = tcg_canonicalize_memop(memop, 0, 0);
181     orig_oi = oi = make_memop_idx(memop, idx);
182 
183     if ((memop & MO_BSWAP) && !tcg_target_has_memory_bswap(memop)) {
184         memop &= ~MO_BSWAP;
185         /* The bswap primitive benefits from zero-extended input.  */
186         if ((memop & MO_SSIZE) == MO_SW) {
187             memop &= ~MO_SIGN;
188         }
189         oi = make_memop_idx(memop, idx);
190     }
191 
192     copy_addr = plugin_maybe_preserve_addr(addr);
193     if (tcg_ctx->addr_type == TCG_TYPE_I32) {
194         opc = INDEX_op_qemu_ld_a32_i32;
195     } else {
196         opc = INDEX_op_qemu_ld_a64_i32;
197     }
198     gen_ldst(opc, tcgv_i32_temp(val), NULL, addr, oi);
199     plugin_gen_mem_callbacks(copy_addr, addr, orig_oi, QEMU_PLUGIN_MEM_R);
200 
201     if ((orig_memop ^ memop) & MO_BSWAP) {
202         switch (orig_memop & MO_SIZE) {
203         case MO_16:
204             tcg_gen_bswap16_i32(val, val, (orig_memop & MO_SIGN
205                                            ? TCG_BSWAP_IZ | TCG_BSWAP_OS
206                                            : TCG_BSWAP_IZ | TCG_BSWAP_OZ));
207             break;
208         case MO_32:
209             tcg_gen_bswap32_i32(val, val);
210             break;
211         default:
212             g_assert_not_reached();
213         }
214     }
215 }
216 
217 void tcg_gen_qemu_ld_i32_chk(TCGv_i32 val, TCGTemp *addr, TCGArg idx,
218                              MemOp memop, TCGType addr_type)
219 {
220     tcg_debug_assert(addr_type == tcg_ctx->addr_type);
221     tcg_debug_assert((memop & MO_SIZE) <= MO_32);
222     tcg_gen_qemu_ld_i32_int(val, addr, idx, memop);
223 }
224 
225 static void tcg_gen_qemu_st_i32_int(TCGv_i32 val, TCGTemp *addr,
226                                     TCGArg idx, MemOp memop)
227 {
228     TCGv_i32 swap = NULL;
229     MemOpIdx orig_oi, oi;
230     TCGOpcode opc;
231 
232     tcg_gen_req_mo(TCG_MO_LD_ST | TCG_MO_ST_ST);
233     memop = tcg_canonicalize_memop(memop, 0, 1);
234     orig_oi = oi = make_memop_idx(memop, idx);
235 
236     if ((memop & MO_BSWAP) && !tcg_target_has_memory_bswap(memop)) {
237         swap = tcg_temp_ebb_new_i32();
238         switch (memop & MO_SIZE) {
239         case MO_16:
240             tcg_gen_bswap16_i32(swap, val, 0);
241             break;
242         case MO_32:
243             tcg_gen_bswap32_i32(swap, val);
244             break;
245         default:
246             g_assert_not_reached();
247         }
248         val = swap;
249         memop &= ~MO_BSWAP;
250         oi = make_memop_idx(memop, idx);
251     }
252 
253     if (TCG_TARGET_HAS_qemu_st8_i32 && (memop & MO_SIZE) == MO_8) {
254         if (tcg_ctx->addr_type == TCG_TYPE_I32) {
255             opc = INDEX_op_qemu_st8_a32_i32;
256         } else {
257             opc = INDEX_op_qemu_st8_a64_i32;
258         }
259     } else {
260         if (tcg_ctx->addr_type == TCG_TYPE_I32) {
261             opc = INDEX_op_qemu_st_a32_i32;
262         } else {
263             opc = INDEX_op_qemu_st_a64_i32;
264         }
265     }
266     gen_ldst(opc, tcgv_i32_temp(val), NULL, addr, oi);
267     plugin_gen_mem_callbacks(NULL, addr, orig_oi, QEMU_PLUGIN_MEM_W);
268 
269     if (swap) {
270         tcg_temp_free_i32(swap);
271     }
272 }
273 
274 void tcg_gen_qemu_st_i32_chk(TCGv_i32 val, TCGTemp *addr, TCGArg idx,
275                              MemOp memop, TCGType addr_type)
276 {
277     tcg_debug_assert(addr_type == tcg_ctx->addr_type);
278     tcg_debug_assert((memop & MO_SIZE) <= MO_32);
279     tcg_gen_qemu_st_i32_int(val, addr, idx, memop);
280 }
281 
282 static void tcg_gen_qemu_ld_i64_int(TCGv_i64 val, TCGTemp *addr,
283                                     TCGArg idx, MemOp memop)
284 {
285     MemOp orig_memop;
286     MemOpIdx orig_oi, oi;
287     TCGv_i64 copy_addr;
288     TCGOpcode opc;
289 
290     if (TCG_TARGET_REG_BITS == 32 && (memop & MO_SIZE) < MO_64) {
291         tcg_gen_qemu_ld_i32_int(TCGV_LOW(val), addr, idx, memop);
292         if (memop & MO_SIGN) {
293             tcg_gen_sari_i32(TCGV_HIGH(val), TCGV_LOW(val), 31);
294         } else {
295             tcg_gen_movi_i32(TCGV_HIGH(val), 0);
296         }
297         return;
298     }
299 
300     tcg_gen_req_mo(TCG_MO_LD_LD | TCG_MO_ST_LD);
301     orig_memop = memop = tcg_canonicalize_memop(memop, 1, 0);
302     orig_oi = oi = make_memop_idx(memop, idx);
303 
304     if ((memop & MO_BSWAP) && !tcg_target_has_memory_bswap(memop)) {
305         memop &= ~MO_BSWAP;
306         /* The bswap primitive benefits from zero-extended input.  */
307         if ((memop & MO_SIGN) && (memop & MO_SIZE) < MO_64) {
308             memop &= ~MO_SIGN;
309         }
310         oi = make_memop_idx(memop, idx);
311     }
312 
313     copy_addr = plugin_maybe_preserve_addr(addr);
314     if (tcg_ctx->addr_type == TCG_TYPE_I32) {
315         opc = INDEX_op_qemu_ld_a32_i64;
316     } else {
317         opc = INDEX_op_qemu_ld_a64_i64;
318     }
319     gen_ldst_i64(opc, val, addr, oi);
320     plugin_gen_mem_callbacks(copy_addr, addr, orig_oi, QEMU_PLUGIN_MEM_R);
321 
322     if ((orig_memop ^ memop) & MO_BSWAP) {
323         int flags = (orig_memop & MO_SIGN
324                      ? TCG_BSWAP_IZ | TCG_BSWAP_OS
325                      : TCG_BSWAP_IZ | TCG_BSWAP_OZ);
326         switch (orig_memop & MO_SIZE) {
327         case MO_16:
328             tcg_gen_bswap16_i64(val, val, flags);
329             break;
330         case MO_32:
331             tcg_gen_bswap32_i64(val, val, flags);
332             break;
333         case MO_64:
334             tcg_gen_bswap64_i64(val, val);
335             break;
336         default:
337             g_assert_not_reached();
338         }
339     }
340 }
341 
342 void tcg_gen_qemu_ld_i64_chk(TCGv_i64 val, TCGTemp *addr, TCGArg idx,
343                              MemOp memop, TCGType addr_type)
344 {
345     tcg_debug_assert(addr_type == tcg_ctx->addr_type);
346     tcg_debug_assert((memop & MO_SIZE) <= MO_64);
347     tcg_gen_qemu_ld_i64_int(val, addr, idx, memop);
348 }
349 
350 static void tcg_gen_qemu_st_i64_int(TCGv_i64 val, TCGTemp *addr,
351                                     TCGArg idx, MemOp memop)
352 {
353     TCGv_i64 swap = NULL;
354     MemOpIdx orig_oi, oi;
355     TCGOpcode opc;
356 
357     if (TCG_TARGET_REG_BITS == 32 && (memop & MO_SIZE) < MO_64) {
358         tcg_gen_qemu_st_i32_int(TCGV_LOW(val), addr, idx, memop);
359         return;
360     }
361 
362     tcg_gen_req_mo(TCG_MO_LD_ST | TCG_MO_ST_ST);
363     memop = tcg_canonicalize_memop(memop, 1, 1);
364     orig_oi = oi = make_memop_idx(memop, idx);
365 
366     if ((memop & MO_BSWAP) && !tcg_target_has_memory_bswap(memop)) {
367         swap = tcg_temp_ebb_new_i64();
368         switch (memop & MO_SIZE) {
369         case MO_16:
370             tcg_gen_bswap16_i64(swap, val, 0);
371             break;
372         case MO_32:
373             tcg_gen_bswap32_i64(swap, val, 0);
374             break;
375         case MO_64:
376             tcg_gen_bswap64_i64(swap, val);
377             break;
378         default:
379             g_assert_not_reached();
380         }
381         val = swap;
382         memop &= ~MO_BSWAP;
383         oi = make_memop_idx(memop, idx);
384     }
385 
386     if (tcg_ctx->addr_type == TCG_TYPE_I32) {
387         opc = INDEX_op_qemu_st_a32_i64;
388     } else {
389         opc = INDEX_op_qemu_st_a64_i64;
390     }
391     gen_ldst_i64(opc, val, addr, oi);
392     plugin_gen_mem_callbacks(NULL, addr, orig_oi, QEMU_PLUGIN_MEM_W);
393 
394     if (swap) {
395         tcg_temp_free_i64(swap);
396     }
397 }
398 
399 void tcg_gen_qemu_st_i64_chk(TCGv_i64 val, TCGTemp *addr, TCGArg idx,
400                              MemOp memop, TCGType addr_type)
401 {
402     tcg_debug_assert(addr_type == tcg_ctx->addr_type);
403     tcg_debug_assert((memop & MO_SIZE) <= MO_64);
404     tcg_gen_qemu_st_i64_int(val, addr, idx, memop);
405 }
406 
407 /*
408  * Return true if @mop, without knowledge of the pointer alignment,
409  * does not require 16-byte atomicity, and it would be adventagous
410  * to avoid a call to a helper function.
411  */
412 static bool use_two_i64_for_i128(MemOp mop)
413 {
414     /* Two softmmu tlb lookups is larger than one function call. */
415     if (tcg_use_softmmu) {
416         return false;
417     }
418 
419     /*
420      * For user-only, two 64-bit operations may well be smaller than a call.
421      * Determine if that would be legal for the requested atomicity.
422      */
423     switch (mop & MO_ATOM_MASK) {
424     case MO_ATOM_NONE:
425     case MO_ATOM_IFALIGN_PAIR:
426         return true;
427     case MO_ATOM_IFALIGN:
428     case MO_ATOM_SUBALIGN:
429     case MO_ATOM_WITHIN16:
430     case MO_ATOM_WITHIN16_PAIR:
431         /* In a serialized context, no atomicity is required. */
432         return !(tcg_ctx->gen_tb->cflags & CF_PARALLEL);
433     default:
434         g_assert_not_reached();
435     }
436 }
437 
438 static void canonicalize_memop_i128_as_i64(MemOp ret[2], MemOp orig)
439 {
440     MemOp mop_1 = orig, mop_2;
441 
442     /* Reduce the size to 64-bit. */
443     mop_1 = (mop_1 & ~MO_SIZE) | MO_64;
444 
445     /* Retain the alignment constraints of the original. */
446     switch (orig & MO_AMASK) {
447     case MO_UNALN:
448     case MO_ALIGN_2:
449     case MO_ALIGN_4:
450         mop_2 = mop_1;
451         break;
452     case MO_ALIGN_8:
453         /* Prefer MO_ALIGN+MO_64 to MO_ALIGN_8+MO_64. */
454         mop_1 = (mop_1 & ~MO_AMASK) | MO_ALIGN;
455         mop_2 = mop_1;
456         break;
457     case MO_ALIGN:
458         /* Second has 8-byte alignment; first has 16-byte alignment. */
459         mop_2 = mop_1;
460         mop_1 = (mop_1 & ~MO_AMASK) | MO_ALIGN_16;
461         break;
462     case MO_ALIGN_16:
463     case MO_ALIGN_32:
464     case MO_ALIGN_64:
465         /* Second has 8-byte alignment; first retains original. */
466         mop_2 = (mop_1 & ~MO_AMASK) | MO_ALIGN;
467         break;
468     default:
469         g_assert_not_reached();
470     }
471 
472     /* Use a memory ordering implemented by the host. */
473     if ((orig & MO_BSWAP) && !tcg_target_has_memory_bswap(mop_1)) {
474         mop_1 &= ~MO_BSWAP;
475         mop_2 &= ~MO_BSWAP;
476     }
477 
478     ret[0] = mop_1;
479     ret[1] = mop_2;
480 }
481 
482 static TCGv_i64 maybe_extend_addr64(TCGTemp *addr)
483 {
484     if (tcg_ctx->addr_type == TCG_TYPE_I32) {
485         TCGv_i64 a64 = tcg_temp_ebb_new_i64();
486         tcg_gen_extu_i32_i64(a64, temp_tcgv_i32(addr));
487         return a64;
488     }
489     return temp_tcgv_i64(addr);
490 }
491 
492 static void maybe_free_addr64(TCGv_i64 a64)
493 {
494     if (tcg_ctx->addr_type == TCG_TYPE_I32) {
495         tcg_temp_free_i64(a64);
496     }
497 }
498 
499 static void tcg_gen_qemu_ld_i128_int(TCGv_i128 val, TCGTemp *addr,
500                                      TCGArg idx, MemOp memop)
501 {
502     const MemOpIdx orig_oi = make_memop_idx(memop, idx);
503     TCGv_i64 ext_addr = NULL;
504     TCGOpcode opc;
505 
506     check_max_alignment(get_alignment_bits(memop));
507     tcg_gen_req_mo(TCG_MO_LD_LD | TCG_MO_ST_LD);
508 
509     /* TODO: For now, force 32-bit hosts to use the helper. */
510     if (TCG_TARGET_HAS_qemu_ldst_i128 && TCG_TARGET_REG_BITS == 64) {
511         TCGv_i64 lo, hi;
512         bool need_bswap = false;
513         MemOpIdx oi = orig_oi;
514 
515         if ((memop & MO_BSWAP) && !tcg_target_has_memory_bswap(memop)) {
516             lo = TCGV128_HIGH(val);
517             hi = TCGV128_LOW(val);
518             oi = make_memop_idx(memop & ~MO_BSWAP, idx);
519             need_bswap = true;
520         } else {
521             lo = TCGV128_LOW(val);
522             hi = TCGV128_HIGH(val);
523         }
524 
525         if (tcg_ctx->addr_type == TCG_TYPE_I32) {
526             opc = INDEX_op_qemu_ld_a32_i128;
527         } else {
528             opc = INDEX_op_qemu_ld_a64_i128;
529         }
530         gen_ldst(opc, tcgv_i64_temp(lo), tcgv_i64_temp(hi), addr, oi);
531 
532         if (need_bswap) {
533             tcg_gen_bswap64_i64(lo, lo);
534             tcg_gen_bswap64_i64(hi, hi);
535         }
536     } else if (use_two_i64_for_i128(memop)) {
537         MemOp mop[2];
538         TCGTemp *addr_p8;
539         TCGv_i64 x, y;
540         bool need_bswap;
541 
542         canonicalize_memop_i128_as_i64(mop, memop);
543         need_bswap = (mop[0] ^ memop) & MO_BSWAP;
544 
545         if (tcg_ctx->addr_type == TCG_TYPE_I32) {
546             opc = INDEX_op_qemu_ld_a32_i64;
547         } else {
548             opc = INDEX_op_qemu_ld_a64_i64;
549         }
550 
551         /*
552          * Since there are no global TCGv_i128, there is no visible state
553          * changed if the second load faults.  Load directly into the two
554          * subwords.
555          */
556         if ((memop & MO_BSWAP) == MO_LE) {
557             x = TCGV128_LOW(val);
558             y = TCGV128_HIGH(val);
559         } else {
560             x = TCGV128_HIGH(val);
561             y = TCGV128_LOW(val);
562         }
563 
564         gen_ldst_i64(opc, x, addr, make_memop_idx(mop[0], idx));
565 
566         if (need_bswap) {
567             tcg_gen_bswap64_i64(x, x);
568         }
569 
570         if (tcg_ctx->addr_type == TCG_TYPE_I32) {
571             TCGv_i32 t = tcg_temp_ebb_new_i32();
572             tcg_gen_addi_i32(t, temp_tcgv_i32(addr), 8);
573             addr_p8 = tcgv_i32_temp(t);
574         } else {
575             TCGv_i64 t = tcg_temp_ebb_new_i64();
576             tcg_gen_addi_i64(t, temp_tcgv_i64(addr), 8);
577             addr_p8 = tcgv_i64_temp(t);
578         }
579 
580         gen_ldst_i64(opc, y, addr_p8, make_memop_idx(mop[1], idx));
581         tcg_temp_free_internal(addr_p8);
582 
583         if (need_bswap) {
584             tcg_gen_bswap64_i64(y, y);
585         }
586     } else {
587         if (tcg_ctx->addr_type == TCG_TYPE_I32) {
588             ext_addr = tcg_temp_ebb_new_i64();
589             tcg_gen_extu_i32_i64(ext_addr, temp_tcgv_i32(addr));
590             addr = tcgv_i64_temp(ext_addr);
591         }
592         gen_helper_ld_i128(val, tcg_env, temp_tcgv_i64(addr),
593                            tcg_constant_i32(orig_oi));
594     }
595 
596     plugin_gen_mem_callbacks(ext_addr, addr, orig_oi, QEMU_PLUGIN_MEM_R);
597 }
598 
599 void tcg_gen_qemu_ld_i128_chk(TCGv_i128 val, TCGTemp *addr, TCGArg idx,
600                               MemOp memop, TCGType addr_type)
601 {
602     tcg_debug_assert(addr_type == tcg_ctx->addr_type);
603     tcg_debug_assert((memop & MO_SIZE) == MO_128);
604     tcg_debug_assert((memop & MO_SIGN) == 0);
605     tcg_gen_qemu_ld_i128_int(val, addr, idx, memop);
606 }
607 
608 static void tcg_gen_qemu_st_i128_int(TCGv_i128 val, TCGTemp *addr,
609                                      TCGArg idx, MemOp memop)
610 {
611     const MemOpIdx orig_oi = make_memop_idx(memop, idx);
612     TCGv_i64 ext_addr = NULL;
613     TCGOpcode opc;
614 
615     check_max_alignment(get_alignment_bits(memop));
616     tcg_gen_req_mo(TCG_MO_ST_LD | TCG_MO_ST_ST);
617 
618     /* TODO: For now, force 32-bit hosts to use the helper. */
619 
620     if (TCG_TARGET_HAS_qemu_ldst_i128 && TCG_TARGET_REG_BITS == 64) {
621         TCGv_i64 lo, hi;
622         MemOpIdx oi = orig_oi;
623         bool need_bswap = false;
624 
625         if ((memop & MO_BSWAP) && !tcg_target_has_memory_bswap(memop)) {
626             lo = tcg_temp_ebb_new_i64();
627             hi = tcg_temp_ebb_new_i64();
628             tcg_gen_bswap64_i64(lo, TCGV128_HIGH(val));
629             tcg_gen_bswap64_i64(hi, TCGV128_LOW(val));
630             oi = make_memop_idx(memop & ~MO_BSWAP, idx);
631             need_bswap = true;
632         } else {
633             lo = TCGV128_LOW(val);
634             hi = TCGV128_HIGH(val);
635         }
636 
637         if (tcg_ctx->addr_type == TCG_TYPE_I32) {
638             opc = INDEX_op_qemu_st_a32_i128;
639         } else {
640             opc = INDEX_op_qemu_st_a64_i128;
641         }
642         gen_ldst(opc, tcgv_i64_temp(lo), tcgv_i64_temp(hi), addr, oi);
643 
644         if (need_bswap) {
645             tcg_temp_free_i64(lo);
646             tcg_temp_free_i64(hi);
647         }
648     } else if (use_two_i64_for_i128(memop)) {
649         MemOp mop[2];
650         TCGTemp *addr_p8;
651         TCGv_i64 x, y, b = NULL;
652 
653         canonicalize_memop_i128_as_i64(mop, memop);
654 
655         if (tcg_ctx->addr_type == TCG_TYPE_I32) {
656             opc = INDEX_op_qemu_st_a32_i64;
657         } else {
658             opc = INDEX_op_qemu_st_a64_i64;
659         }
660 
661         if ((memop & MO_BSWAP) == MO_LE) {
662             x = TCGV128_LOW(val);
663             y = TCGV128_HIGH(val);
664         } else {
665             x = TCGV128_HIGH(val);
666             y = TCGV128_LOW(val);
667         }
668 
669         if ((mop[0] ^ memop) & MO_BSWAP) {
670             b = tcg_temp_ebb_new_i64();
671             tcg_gen_bswap64_i64(b, x);
672             x = b;
673         }
674 
675         gen_ldst_i64(opc, x, addr, make_memop_idx(mop[0], idx));
676 
677         if (tcg_ctx->addr_type == TCG_TYPE_I32) {
678             TCGv_i32 t = tcg_temp_ebb_new_i32();
679             tcg_gen_addi_i32(t, temp_tcgv_i32(addr), 8);
680             addr_p8 = tcgv_i32_temp(t);
681         } else {
682             TCGv_i64 t = tcg_temp_ebb_new_i64();
683             tcg_gen_addi_i64(t, temp_tcgv_i64(addr), 8);
684             addr_p8 = tcgv_i64_temp(t);
685         }
686 
687         if (b) {
688             tcg_gen_bswap64_i64(b, y);
689             gen_ldst_i64(opc, b, addr_p8, make_memop_idx(mop[1], idx));
690             tcg_temp_free_i64(b);
691         } else {
692             gen_ldst_i64(opc, y, addr_p8, make_memop_idx(mop[1], idx));
693         }
694         tcg_temp_free_internal(addr_p8);
695     } else {
696         if (tcg_ctx->addr_type == TCG_TYPE_I32) {
697             ext_addr = tcg_temp_ebb_new_i64();
698             tcg_gen_extu_i32_i64(ext_addr, temp_tcgv_i32(addr));
699             addr = tcgv_i64_temp(ext_addr);
700         }
701         gen_helper_st_i128(tcg_env, temp_tcgv_i64(addr), val,
702                            tcg_constant_i32(orig_oi));
703     }
704 
705     plugin_gen_mem_callbacks(ext_addr, addr, orig_oi, QEMU_PLUGIN_MEM_W);
706 }
707 
708 void tcg_gen_qemu_st_i128_chk(TCGv_i128 val, TCGTemp *addr, TCGArg idx,
709                               MemOp memop, TCGType addr_type)
710 {
711     tcg_debug_assert(addr_type == tcg_ctx->addr_type);
712     tcg_debug_assert((memop & MO_SIZE) == MO_128);
713     tcg_debug_assert((memop & MO_SIGN) == 0);
714     tcg_gen_qemu_st_i128_int(val, addr, idx, memop);
715 }
716 
717 void tcg_gen_ext_i32(TCGv_i32 ret, TCGv_i32 val, MemOp opc)
718 {
719     switch (opc & MO_SSIZE) {
720     case MO_SB:
721         tcg_gen_ext8s_i32(ret, val);
722         break;
723     case MO_UB:
724         tcg_gen_ext8u_i32(ret, val);
725         break;
726     case MO_SW:
727         tcg_gen_ext16s_i32(ret, val);
728         break;
729     case MO_UW:
730         tcg_gen_ext16u_i32(ret, val);
731         break;
732     case MO_UL:
733     case MO_SL:
734         tcg_gen_mov_i32(ret, val);
735         break;
736     default:
737         g_assert_not_reached();
738     }
739 }
740 
741 void tcg_gen_ext_i64(TCGv_i64 ret, TCGv_i64 val, MemOp opc)
742 {
743     switch (opc & MO_SSIZE) {
744     case MO_SB:
745         tcg_gen_ext8s_i64(ret, val);
746         break;
747     case MO_UB:
748         tcg_gen_ext8u_i64(ret, val);
749         break;
750     case MO_SW:
751         tcg_gen_ext16s_i64(ret, val);
752         break;
753     case MO_UW:
754         tcg_gen_ext16u_i64(ret, val);
755         break;
756     case MO_SL:
757         tcg_gen_ext32s_i64(ret, val);
758         break;
759     case MO_UL:
760         tcg_gen_ext32u_i64(ret, val);
761         break;
762     case MO_UQ:
763     case MO_SQ:
764         tcg_gen_mov_i64(ret, val);
765         break;
766     default:
767         g_assert_not_reached();
768     }
769 }
770 
771 typedef void (*gen_atomic_cx_i32)(TCGv_i32, TCGv_env, TCGv_i64,
772                                   TCGv_i32, TCGv_i32, TCGv_i32);
773 typedef void (*gen_atomic_cx_i64)(TCGv_i64, TCGv_env, TCGv_i64,
774                                   TCGv_i64, TCGv_i64, TCGv_i32);
775 typedef void (*gen_atomic_cx_i128)(TCGv_i128, TCGv_env, TCGv_i64,
776                                    TCGv_i128, TCGv_i128, TCGv_i32);
777 typedef void (*gen_atomic_op_i32)(TCGv_i32, TCGv_env, TCGv_i64,
778                                   TCGv_i32, TCGv_i32);
779 typedef void (*gen_atomic_op_i64)(TCGv_i64, TCGv_env, TCGv_i64,
780                                   TCGv_i64, TCGv_i32);
781 
782 #ifdef CONFIG_ATOMIC64
783 # define WITH_ATOMIC64(X) X,
784 #else
785 # define WITH_ATOMIC64(X)
786 #endif
787 #if HAVE_CMPXCHG128
788 # define WITH_ATOMIC128(X) X,
789 #else
790 # define WITH_ATOMIC128(X)
791 #endif
792 
793 static void * const table_cmpxchg[(MO_SIZE | MO_BSWAP) + 1] = {
794     [MO_8] = gen_helper_atomic_cmpxchgb,
795     [MO_16 | MO_LE] = gen_helper_atomic_cmpxchgw_le,
796     [MO_16 | MO_BE] = gen_helper_atomic_cmpxchgw_be,
797     [MO_32 | MO_LE] = gen_helper_atomic_cmpxchgl_le,
798     [MO_32 | MO_BE] = gen_helper_atomic_cmpxchgl_be,
799     WITH_ATOMIC64([MO_64 | MO_LE] = gen_helper_atomic_cmpxchgq_le)
800     WITH_ATOMIC64([MO_64 | MO_BE] = gen_helper_atomic_cmpxchgq_be)
801     WITH_ATOMIC128([MO_128 | MO_LE] = gen_helper_atomic_cmpxchgo_le)
802     WITH_ATOMIC128([MO_128 | MO_BE] = gen_helper_atomic_cmpxchgo_be)
803 };
804 
805 static void tcg_gen_nonatomic_cmpxchg_i32_int(TCGv_i32 retv, TCGTemp *addr,
806                                               TCGv_i32 cmpv, TCGv_i32 newv,
807                                               TCGArg idx, MemOp memop)
808 {
809     TCGv_i32 t1 = tcg_temp_ebb_new_i32();
810     TCGv_i32 t2 = tcg_temp_ebb_new_i32();
811 
812     tcg_gen_ext_i32(t2, cmpv, memop & MO_SIZE);
813 
814     tcg_gen_qemu_ld_i32_int(t1, addr, idx, memop & ~MO_SIGN);
815     tcg_gen_movcond_i32(TCG_COND_EQ, t2, t1, t2, newv, t1);
816     tcg_gen_qemu_st_i32_int(t2, addr, idx, memop);
817     tcg_temp_free_i32(t2);
818 
819     if (memop & MO_SIGN) {
820         tcg_gen_ext_i32(retv, t1, memop);
821     } else {
822         tcg_gen_mov_i32(retv, t1);
823     }
824     tcg_temp_free_i32(t1);
825 }
826 
827 void tcg_gen_nonatomic_cmpxchg_i32_chk(TCGv_i32 retv, TCGTemp *addr,
828                                        TCGv_i32 cmpv, TCGv_i32 newv,
829                                        TCGArg idx, MemOp memop,
830                                        TCGType addr_type)
831 {
832     tcg_debug_assert(addr_type == tcg_ctx->addr_type);
833     tcg_debug_assert((memop & MO_SIZE) <= MO_32);
834     tcg_gen_nonatomic_cmpxchg_i32_int(retv, addr, cmpv, newv, idx, memop);
835 }
836 
837 static void tcg_gen_atomic_cmpxchg_i32_int(TCGv_i32 retv, TCGTemp *addr,
838                                            TCGv_i32 cmpv, TCGv_i32 newv,
839                                            TCGArg idx, MemOp memop)
840 {
841     gen_atomic_cx_i32 gen;
842     TCGv_i64 a64;
843     MemOpIdx oi;
844 
845     if (!(tcg_ctx->gen_tb->cflags & CF_PARALLEL)) {
846         tcg_gen_nonatomic_cmpxchg_i32_int(retv, addr, cmpv, newv, idx, memop);
847         return;
848     }
849 
850     memop = tcg_canonicalize_memop(memop, 0, 0);
851     gen = table_cmpxchg[memop & (MO_SIZE | MO_BSWAP)];
852     tcg_debug_assert(gen != NULL);
853 
854     oi = make_memop_idx(memop & ~MO_SIGN, idx);
855     a64 = maybe_extend_addr64(addr);
856     gen(retv, tcg_env, a64, cmpv, newv, tcg_constant_i32(oi));
857     maybe_free_addr64(a64);
858 
859     if (memop & MO_SIGN) {
860         tcg_gen_ext_i32(retv, retv, memop);
861     }
862 }
863 
864 void tcg_gen_atomic_cmpxchg_i32_chk(TCGv_i32 retv, TCGTemp *addr,
865                                     TCGv_i32 cmpv, TCGv_i32 newv,
866                                     TCGArg idx, MemOp memop,
867                                     TCGType addr_type)
868 {
869     tcg_debug_assert(addr_type == tcg_ctx->addr_type);
870     tcg_debug_assert((memop & MO_SIZE) <= MO_32);
871     tcg_gen_atomic_cmpxchg_i32_int(retv, addr, cmpv, newv, idx, memop);
872 }
873 
874 static void tcg_gen_nonatomic_cmpxchg_i64_int(TCGv_i64 retv, TCGTemp *addr,
875                                               TCGv_i64 cmpv, TCGv_i64 newv,
876                                               TCGArg idx, MemOp memop)
877 {
878     TCGv_i64 t1, t2;
879 
880     if (TCG_TARGET_REG_BITS == 32 && (memop & MO_SIZE) < MO_64) {
881         tcg_gen_nonatomic_cmpxchg_i32_int(TCGV_LOW(retv), addr, TCGV_LOW(cmpv),
882                                           TCGV_LOW(newv), idx, memop);
883         if (memop & MO_SIGN) {
884             tcg_gen_sari_i32(TCGV_HIGH(retv), TCGV_LOW(retv), 31);
885         } else {
886             tcg_gen_movi_i32(TCGV_HIGH(retv), 0);
887         }
888         return;
889     }
890 
891     t1 = tcg_temp_ebb_new_i64();
892     t2 = tcg_temp_ebb_new_i64();
893 
894     tcg_gen_ext_i64(t2, cmpv, memop & MO_SIZE);
895 
896     tcg_gen_qemu_ld_i64_int(t1, addr, idx, memop & ~MO_SIGN);
897     tcg_gen_movcond_i64(TCG_COND_EQ, t2, t1, t2, newv, t1);
898     tcg_gen_qemu_st_i64_int(t2, addr, idx, memop);
899     tcg_temp_free_i64(t2);
900 
901     if (memop & MO_SIGN) {
902         tcg_gen_ext_i64(retv, t1, memop);
903     } else {
904         tcg_gen_mov_i64(retv, t1);
905     }
906     tcg_temp_free_i64(t1);
907 }
908 
909 void tcg_gen_nonatomic_cmpxchg_i64_chk(TCGv_i64 retv, TCGTemp *addr,
910                                        TCGv_i64 cmpv, TCGv_i64 newv,
911                                        TCGArg idx, MemOp memop,
912                                        TCGType addr_type)
913 {
914     tcg_debug_assert(addr_type == tcg_ctx->addr_type);
915     tcg_debug_assert((memop & MO_SIZE) <= MO_64);
916     tcg_gen_nonatomic_cmpxchg_i64_int(retv, addr, cmpv, newv, idx, memop);
917 }
918 
919 static void tcg_gen_atomic_cmpxchg_i64_int(TCGv_i64 retv, TCGTemp *addr,
920                                            TCGv_i64 cmpv, TCGv_i64 newv,
921                                            TCGArg idx, MemOp memop)
922 {
923     if (!(tcg_ctx->gen_tb->cflags & CF_PARALLEL)) {
924         tcg_gen_nonatomic_cmpxchg_i64_int(retv, addr, cmpv, newv, idx, memop);
925         return;
926     }
927 
928     if ((memop & MO_SIZE) == MO_64) {
929         gen_atomic_cx_i64 gen;
930 
931         memop = tcg_canonicalize_memop(memop, 1, 0);
932         gen = table_cmpxchg[memop & (MO_SIZE | MO_BSWAP)];
933         if (gen) {
934             MemOpIdx oi = make_memop_idx(memop, idx);
935             TCGv_i64 a64 = maybe_extend_addr64(addr);
936             gen(retv, tcg_env, a64, cmpv, newv, tcg_constant_i32(oi));
937             maybe_free_addr64(a64);
938             return;
939         }
940 
941         gen_helper_exit_atomic(tcg_env);
942 
943         /*
944          * Produce a result for a well-formed opcode stream.  This satisfies
945          * liveness for set before used, which happens before this dead code
946          * is removed.
947          */
948         tcg_gen_movi_i64(retv, 0);
949         return;
950     }
951 
952     if (TCG_TARGET_REG_BITS == 32) {
953         tcg_gen_atomic_cmpxchg_i32_int(TCGV_LOW(retv), addr, TCGV_LOW(cmpv),
954                                        TCGV_LOW(newv), idx, memop);
955         if (memop & MO_SIGN) {
956             tcg_gen_sari_i32(TCGV_HIGH(retv), TCGV_LOW(retv), 31);
957         } else {
958             tcg_gen_movi_i32(TCGV_HIGH(retv), 0);
959         }
960     } else {
961         TCGv_i32 c32 = tcg_temp_ebb_new_i32();
962         TCGv_i32 n32 = tcg_temp_ebb_new_i32();
963         TCGv_i32 r32 = tcg_temp_ebb_new_i32();
964 
965         tcg_gen_extrl_i64_i32(c32, cmpv);
966         tcg_gen_extrl_i64_i32(n32, newv);
967         tcg_gen_atomic_cmpxchg_i32_int(r32, addr, c32, n32,
968                                        idx, memop & ~MO_SIGN);
969         tcg_temp_free_i32(c32);
970         tcg_temp_free_i32(n32);
971 
972         tcg_gen_extu_i32_i64(retv, r32);
973         tcg_temp_free_i32(r32);
974 
975         if (memop & MO_SIGN) {
976             tcg_gen_ext_i64(retv, retv, memop);
977         }
978     }
979 }
980 
981 void tcg_gen_atomic_cmpxchg_i64_chk(TCGv_i64 retv, TCGTemp *addr,
982                                     TCGv_i64 cmpv, TCGv_i64 newv,
983                                     TCGArg idx, MemOp memop, TCGType addr_type)
984 {
985     tcg_debug_assert(addr_type == tcg_ctx->addr_type);
986     tcg_debug_assert((memop & MO_SIZE) <= MO_64);
987     tcg_gen_atomic_cmpxchg_i64_int(retv, addr, cmpv, newv, idx, memop);
988 }
989 
990 static void tcg_gen_nonatomic_cmpxchg_i128_int(TCGv_i128 retv, TCGTemp *addr,
991                                                TCGv_i128 cmpv, TCGv_i128 newv,
992                                                TCGArg idx, MemOp memop)
993 {
994     if (TCG_TARGET_REG_BITS == 32) {
995         /* Inline expansion below is simply too large for 32-bit hosts. */
996         MemOpIdx oi = make_memop_idx(memop, idx);
997         TCGv_i64 a64 = maybe_extend_addr64(addr);
998 
999         gen_helper_nonatomic_cmpxchgo(retv, tcg_env, a64, cmpv, newv,
1000                                       tcg_constant_i32(oi));
1001         maybe_free_addr64(a64);
1002     } else {
1003         TCGv_i128 oldv = tcg_temp_ebb_new_i128();
1004         TCGv_i128 tmpv = tcg_temp_ebb_new_i128();
1005         TCGv_i64 t0 = tcg_temp_ebb_new_i64();
1006         TCGv_i64 t1 = tcg_temp_ebb_new_i64();
1007         TCGv_i64 z = tcg_constant_i64(0);
1008 
1009         tcg_gen_qemu_ld_i128_int(oldv, addr, idx, memop);
1010 
1011         /* Compare i128 */
1012         tcg_gen_xor_i64(t0, TCGV128_LOW(oldv), TCGV128_LOW(cmpv));
1013         tcg_gen_xor_i64(t1, TCGV128_HIGH(oldv), TCGV128_HIGH(cmpv));
1014         tcg_gen_or_i64(t0, t0, t1);
1015 
1016         /* tmpv = equal ? newv : oldv */
1017         tcg_gen_movcond_i64(TCG_COND_EQ, TCGV128_LOW(tmpv), t0, z,
1018                             TCGV128_LOW(newv), TCGV128_LOW(oldv));
1019         tcg_gen_movcond_i64(TCG_COND_EQ, TCGV128_HIGH(tmpv), t0, z,
1020                             TCGV128_HIGH(newv), TCGV128_HIGH(oldv));
1021 
1022         /* Unconditional writeback. */
1023         tcg_gen_qemu_st_i128_int(tmpv, addr, idx, memop);
1024         tcg_gen_mov_i128(retv, oldv);
1025 
1026         tcg_temp_free_i64(t0);
1027         tcg_temp_free_i64(t1);
1028         tcg_temp_free_i128(tmpv);
1029         tcg_temp_free_i128(oldv);
1030     }
1031 }
1032 
1033 void tcg_gen_nonatomic_cmpxchg_i128_chk(TCGv_i128 retv, TCGTemp *addr,
1034                                         TCGv_i128 cmpv, TCGv_i128 newv,
1035                                         TCGArg idx, MemOp memop,
1036                                         TCGType addr_type)
1037 {
1038     tcg_debug_assert(addr_type == tcg_ctx->addr_type);
1039     tcg_debug_assert((memop & (MO_SIZE | MO_SIGN)) == MO_128);
1040     tcg_gen_nonatomic_cmpxchg_i128_int(retv, addr, cmpv, newv, idx, memop);
1041 }
1042 
1043 static void tcg_gen_atomic_cmpxchg_i128_int(TCGv_i128 retv, TCGTemp *addr,
1044                                             TCGv_i128 cmpv, TCGv_i128 newv,
1045                                             TCGArg idx, MemOp memop)
1046 {
1047     gen_atomic_cx_i128 gen;
1048 
1049     if (!(tcg_ctx->gen_tb->cflags & CF_PARALLEL)) {
1050         tcg_gen_nonatomic_cmpxchg_i128_int(retv, addr, cmpv, newv, idx, memop);
1051         return;
1052     }
1053 
1054     gen = table_cmpxchg[memop & (MO_SIZE | MO_BSWAP)];
1055     if (gen) {
1056         MemOpIdx oi = make_memop_idx(memop, idx);
1057         TCGv_i64 a64 = maybe_extend_addr64(addr);
1058         gen(retv, tcg_env, a64, cmpv, newv, tcg_constant_i32(oi));
1059         maybe_free_addr64(a64);
1060         return;
1061     }
1062 
1063     gen_helper_exit_atomic(tcg_env);
1064 
1065     /*
1066      * Produce a result for a well-formed opcode stream.  This satisfies
1067      * liveness for set before used, which happens before this dead code
1068      * is removed.
1069      */
1070     tcg_gen_movi_i64(TCGV128_LOW(retv), 0);
1071     tcg_gen_movi_i64(TCGV128_HIGH(retv), 0);
1072 }
1073 
1074 void tcg_gen_atomic_cmpxchg_i128_chk(TCGv_i128 retv, TCGTemp *addr,
1075                                      TCGv_i128 cmpv, TCGv_i128 newv,
1076                                      TCGArg idx, MemOp memop,
1077                                      TCGType addr_type)
1078 {
1079     tcg_debug_assert(addr_type == tcg_ctx->addr_type);
1080     tcg_debug_assert((memop & (MO_SIZE | MO_SIGN)) == MO_128);
1081     tcg_gen_atomic_cmpxchg_i128_int(retv, addr, cmpv, newv, idx, memop);
1082 }
1083 
1084 static void do_nonatomic_op_i32(TCGv_i32 ret, TCGTemp *addr, TCGv_i32 val,
1085                                 TCGArg idx, MemOp memop, bool new_val,
1086                                 void (*gen)(TCGv_i32, TCGv_i32, TCGv_i32))
1087 {
1088     TCGv_i32 t1 = tcg_temp_ebb_new_i32();
1089     TCGv_i32 t2 = tcg_temp_ebb_new_i32();
1090 
1091     memop = tcg_canonicalize_memop(memop, 0, 0);
1092 
1093     tcg_gen_qemu_ld_i32_int(t1, addr, idx, memop);
1094     tcg_gen_ext_i32(t2, val, memop);
1095     gen(t2, t1, t2);
1096     tcg_gen_qemu_st_i32_int(t2, addr, idx, memop);
1097 
1098     tcg_gen_ext_i32(ret, (new_val ? t2 : t1), memop);
1099     tcg_temp_free_i32(t1);
1100     tcg_temp_free_i32(t2);
1101 }
1102 
1103 static void do_atomic_op_i32(TCGv_i32 ret, TCGTemp *addr, TCGv_i32 val,
1104                              TCGArg idx, MemOp memop, void * const table[])
1105 {
1106     gen_atomic_op_i32 gen;
1107     TCGv_i64 a64;
1108     MemOpIdx oi;
1109 
1110     memop = tcg_canonicalize_memop(memop, 0, 0);
1111 
1112     gen = table[memop & (MO_SIZE | MO_BSWAP)];
1113     tcg_debug_assert(gen != NULL);
1114 
1115     oi = make_memop_idx(memop & ~MO_SIGN, idx);
1116     a64 = maybe_extend_addr64(addr);
1117     gen(ret, tcg_env, a64, val, tcg_constant_i32(oi));
1118     maybe_free_addr64(a64);
1119 
1120     if (memop & MO_SIGN) {
1121         tcg_gen_ext_i32(ret, ret, memop);
1122     }
1123 }
1124 
1125 static void do_nonatomic_op_i64(TCGv_i64 ret, TCGTemp *addr, TCGv_i64 val,
1126                                 TCGArg idx, MemOp memop, bool new_val,
1127                                 void (*gen)(TCGv_i64, TCGv_i64, TCGv_i64))
1128 {
1129     TCGv_i64 t1 = tcg_temp_ebb_new_i64();
1130     TCGv_i64 t2 = tcg_temp_ebb_new_i64();
1131 
1132     memop = tcg_canonicalize_memop(memop, 1, 0);
1133 
1134     tcg_gen_qemu_ld_i64_int(t1, addr, idx, memop);
1135     tcg_gen_ext_i64(t2, val, memop);
1136     gen(t2, t1, t2);
1137     tcg_gen_qemu_st_i64_int(t2, addr, idx, memop);
1138 
1139     tcg_gen_ext_i64(ret, (new_val ? t2 : t1), memop);
1140     tcg_temp_free_i64(t1);
1141     tcg_temp_free_i64(t2);
1142 }
1143 
1144 static void do_atomic_op_i64(TCGv_i64 ret, TCGTemp *addr, TCGv_i64 val,
1145                              TCGArg idx, MemOp memop, void * const table[])
1146 {
1147     memop = tcg_canonicalize_memop(memop, 1, 0);
1148 
1149     if ((memop & MO_SIZE) == MO_64) {
1150         gen_atomic_op_i64 gen = table[memop & (MO_SIZE | MO_BSWAP)];
1151 
1152         if (gen) {
1153             MemOpIdx oi = make_memop_idx(memop & ~MO_SIGN, idx);
1154             TCGv_i64 a64 = maybe_extend_addr64(addr);
1155             gen(ret, tcg_env, a64, val, tcg_constant_i32(oi));
1156             maybe_free_addr64(a64);
1157             return;
1158         }
1159 
1160         gen_helper_exit_atomic(tcg_env);
1161         /* Produce a result, so that we have a well-formed opcode stream
1162            with respect to uses of the result in the (dead) code following.  */
1163         tcg_gen_movi_i64(ret, 0);
1164     } else {
1165         TCGv_i32 v32 = tcg_temp_ebb_new_i32();
1166         TCGv_i32 r32 = tcg_temp_ebb_new_i32();
1167 
1168         tcg_gen_extrl_i64_i32(v32, val);
1169         do_atomic_op_i32(r32, addr, v32, idx, memop & ~MO_SIGN, table);
1170         tcg_temp_free_i32(v32);
1171 
1172         tcg_gen_extu_i32_i64(ret, r32);
1173         tcg_temp_free_i32(r32);
1174 
1175         if (memop & MO_SIGN) {
1176             tcg_gen_ext_i64(ret, ret, memop);
1177         }
1178     }
1179 }
1180 
1181 #define GEN_ATOMIC_HELPER(NAME, OP, NEW)                                \
1182 static void * const table_##NAME[(MO_SIZE | MO_BSWAP) + 1] = {          \
1183     [MO_8] = gen_helper_atomic_##NAME##b,                               \
1184     [MO_16 | MO_LE] = gen_helper_atomic_##NAME##w_le,                   \
1185     [MO_16 | MO_BE] = gen_helper_atomic_##NAME##w_be,                   \
1186     [MO_32 | MO_LE] = gen_helper_atomic_##NAME##l_le,                   \
1187     [MO_32 | MO_BE] = gen_helper_atomic_##NAME##l_be,                   \
1188     WITH_ATOMIC64([MO_64 | MO_LE] = gen_helper_atomic_##NAME##q_le)     \
1189     WITH_ATOMIC64([MO_64 | MO_BE] = gen_helper_atomic_##NAME##q_be)     \
1190 };                                                                      \
1191 void tcg_gen_atomic_##NAME##_i32_chk(TCGv_i32 ret, TCGTemp *addr,       \
1192                                      TCGv_i32 val, TCGArg idx,          \
1193                                      MemOp memop, TCGType addr_type)    \
1194 {                                                                       \
1195     tcg_debug_assert(addr_type == tcg_ctx->addr_type);                  \
1196     tcg_debug_assert((memop & MO_SIZE) <= MO_32);                       \
1197     if (tcg_ctx->gen_tb->cflags & CF_PARALLEL) {                        \
1198         do_atomic_op_i32(ret, addr, val, idx, memop, table_##NAME);     \
1199     } else {                                                            \
1200         do_nonatomic_op_i32(ret, addr, val, idx, memop, NEW,            \
1201                             tcg_gen_##OP##_i32);                        \
1202     }                                                                   \
1203 }                                                                       \
1204 void tcg_gen_atomic_##NAME##_i64_chk(TCGv_i64 ret, TCGTemp *addr,       \
1205                                      TCGv_i64 val, TCGArg idx,          \
1206                                      MemOp memop, TCGType addr_type)    \
1207 {                                                                       \
1208     tcg_debug_assert(addr_type == tcg_ctx->addr_type);                  \
1209     tcg_debug_assert((memop & MO_SIZE) <= MO_64);                       \
1210     if (tcg_ctx->gen_tb->cflags & CF_PARALLEL) {                        \
1211         do_atomic_op_i64(ret, addr, val, idx, memop, table_##NAME);     \
1212     } else {                                                            \
1213         do_nonatomic_op_i64(ret, addr, val, idx, memop, NEW,            \
1214                             tcg_gen_##OP##_i64);                        \
1215     }                                                                   \
1216 }
1217 
1218 GEN_ATOMIC_HELPER(fetch_add, add, 0)
1219 GEN_ATOMIC_HELPER(fetch_and, and, 0)
1220 GEN_ATOMIC_HELPER(fetch_or, or, 0)
1221 GEN_ATOMIC_HELPER(fetch_xor, xor, 0)
1222 GEN_ATOMIC_HELPER(fetch_smin, smin, 0)
1223 GEN_ATOMIC_HELPER(fetch_umin, umin, 0)
1224 GEN_ATOMIC_HELPER(fetch_smax, smax, 0)
1225 GEN_ATOMIC_HELPER(fetch_umax, umax, 0)
1226 
1227 GEN_ATOMIC_HELPER(add_fetch, add, 1)
1228 GEN_ATOMIC_HELPER(and_fetch, and, 1)
1229 GEN_ATOMIC_HELPER(or_fetch, or, 1)
1230 GEN_ATOMIC_HELPER(xor_fetch, xor, 1)
1231 GEN_ATOMIC_HELPER(smin_fetch, smin, 1)
1232 GEN_ATOMIC_HELPER(umin_fetch, umin, 1)
1233 GEN_ATOMIC_HELPER(smax_fetch, smax, 1)
1234 GEN_ATOMIC_HELPER(umax_fetch, umax, 1)
1235 
1236 static void tcg_gen_mov2_i32(TCGv_i32 r, TCGv_i32 a, TCGv_i32 b)
1237 {
1238     tcg_gen_mov_i32(r, b);
1239 }
1240 
1241 static void tcg_gen_mov2_i64(TCGv_i64 r, TCGv_i64 a, TCGv_i64 b)
1242 {
1243     tcg_gen_mov_i64(r, b);
1244 }
1245 
1246 GEN_ATOMIC_HELPER(xchg, mov2, 0)
1247 
1248 #undef GEN_ATOMIC_HELPER
1249