xref: /openbmc/qemu/accel/tcg/atomic_common.c.inc (revision b14df228)
1/*
2 * Common Atomic Helper Functions
3 *
4 * This file should be included before the various instantiations of
5 * the atomic_template.h helpers.
6 *
7 * Copyright (c) 2019 Linaro
8 * Written by Alex Bennée <alex.bennee@linaro.org>
9 *
10 * SPDX-License-Identifier: GPL-2.0-or-later
11 *
12 * This work is licensed under the terms of the GNU GPL, version 2 or later.
13 * See the COPYING file in the top-level directory.
14 */
15
16static void atomic_trace_rmw_post(CPUArchState *env, target_ulong addr,
17                                  MemOpIdx oi)
18{
19    qemu_plugin_vcpu_mem_cb(env_cpu(env), addr, oi, QEMU_PLUGIN_MEM_RW);
20}
21
22#if HAVE_ATOMIC128
23static void atomic_trace_ld_post(CPUArchState *env, target_ulong addr,
24                                 MemOpIdx oi)
25{
26    qemu_plugin_vcpu_mem_cb(env_cpu(env), addr, oi, QEMU_PLUGIN_MEM_R);
27}
28
29static void atomic_trace_st_post(CPUArchState *env, target_ulong addr,
30                                 MemOpIdx oi)
31{
32    qemu_plugin_vcpu_mem_cb(env_cpu(env), addr, oi, QEMU_PLUGIN_MEM_W);
33}
34#endif
35
36/*
37 * Atomic helpers callable from TCG.
38 * These have a common interface and all defer to cpu_atomic_*
39 * using the host return address from GETPC().
40 */
41
42#define CMPXCHG_HELPER(OP, TYPE) \
43    TYPE HELPER(atomic_##OP)(CPUArchState *env, target_ulong addr,  \
44                             TYPE oldv, TYPE newv, uint32_t oi)     \
45    { return cpu_atomic_##OP##_mmu(env, addr, oldv, newv, oi, GETPC()); }
46
47CMPXCHG_HELPER(cmpxchgb, uint32_t)
48CMPXCHG_HELPER(cmpxchgw_be, uint32_t)
49CMPXCHG_HELPER(cmpxchgw_le, uint32_t)
50CMPXCHG_HELPER(cmpxchgl_be, uint32_t)
51CMPXCHG_HELPER(cmpxchgl_le, uint32_t)
52
53#ifdef CONFIG_ATOMIC64
54CMPXCHG_HELPER(cmpxchgq_be, uint64_t)
55CMPXCHG_HELPER(cmpxchgq_le, uint64_t)
56#endif
57
58#undef CMPXCHG_HELPER
59
60#define ATOMIC_HELPER(OP, TYPE) \
61    TYPE HELPER(glue(atomic_,OP))(CPUArchState *env, target_ulong addr,  \
62                                  TYPE val, uint32_t oi)                 \
63    { return glue(glue(cpu_atomic_,OP),_mmu)(env, addr, val, oi, GETPC()); }
64
65#ifdef CONFIG_ATOMIC64
66#define GEN_ATOMIC_HELPERS(OP)              \
67    ATOMIC_HELPER(glue(OP,b), uint32_t)     \
68    ATOMIC_HELPER(glue(OP,w_be), uint32_t)  \
69    ATOMIC_HELPER(glue(OP,w_le), uint32_t)  \
70    ATOMIC_HELPER(glue(OP,l_be), uint32_t)  \
71    ATOMIC_HELPER(glue(OP,l_le), uint32_t)  \
72    ATOMIC_HELPER(glue(OP,q_be), uint64_t)  \
73    ATOMIC_HELPER(glue(OP,q_le), uint64_t)
74#else
75#define GEN_ATOMIC_HELPERS(OP)              \
76    ATOMIC_HELPER(glue(OP,b), uint32_t)     \
77    ATOMIC_HELPER(glue(OP,w_be), uint32_t)  \
78    ATOMIC_HELPER(glue(OP,w_le), uint32_t)  \
79    ATOMIC_HELPER(glue(OP,l_be), uint32_t)  \
80    ATOMIC_HELPER(glue(OP,l_le), uint32_t)
81#endif
82
83GEN_ATOMIC_HELPERS(fetch_add)
84GEN_ATOMIC_HELPERS(fetch_and)
85GEN_ATOMIC_HELPERS(fetch_or)
86GEN_ATOMIC_HELPERS(fetch_xor)
87GEN_ATOMIC_HELPERS(fetch_smin)
88GEN_ATOMIC_HELPERS(fetch_umin)
89GEN_ATOMIC_HELPERS(fetch_smax)
90GEN_ATOMIC_HELPERS(fetch_umax)
91
92GEN_ATOMIC_HELPERS(add_fetch)
93GEN_ATOMIC_HELPERS(and_fetch)
94GEN_ATOMIC_HELPERS(or_fetch)
95GEN_ATOMIC_HELPERS(xor_fetch)
96GEN_ATOMIC_HELPERS(smin_fetch)
97GEN_ATOMIC_HELPERS(umin_fetch)
98GEN_ATOMIC_HELPERS(smax_fetch)
99GEN_ATOMIC_HELPERS(umax_fetch)
100
101GEN_ATOMIC_HELPERS(xchg)
102
103#undef ATOMIC_HELPER
104#undef GEN_ATOMIC_HELPERS
105