summaryrefslogtreecommitdiffstats
path: root/llvm/atomic/coremu-atomic.h
blob: 998232b6b19bfee6c7ba55bc3e343407f5c7d443 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
 * COREMU Parallel Emulator Framework
 *
 * Atomic support for COREMU system.
 * XXX: Now only support x86-64 architecture.
 *
 * Copyright (C) 2010 Parallel Processing Institute (PPI), Fudan Univ.
 *  <http://ppi.fudan.edu.cn/system_research_group>
 *
 * Authors:
 *  Zhaoguo Wang    <zgwang@fudan.edu.cn>
 *  Yufei Chen      <chenyufei@fudan.edu.cn>
 *  Ran Liu         <naruilone@gmail.com>
 *  Xi Wu           <wuxi@fudan.edu.cn>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
 */

#ifndef _COREMU_ATOMIC_H
#define _COREMU_ATOMIC_H

#include <stdint.h>
#include <stdlib.h>
#include <assert.h>
#include "config-target.h"
#include "hqemu.h"

/* Given the guest virtual address, get the corresponding host address.
 * This macro resembles ldxxx in softmmu_template.h
 * NOTE: This must be inlined since the use of GETPC needs to get the
 * return address. Using always inline also works, we use macro here to be more
 * explicit. */
#if defined(CONFIG_USER_ONLY)
#define CM_GET_QEMU_ADDR(__env1, q_addr, v_addr) \
do {					         \
    q_addr = v_addr + GUEST_BASE;	         \
} while (0)

#else
#define CM_GET_QEMU_ADDR(__env1, q_addr, v_addr) \
do {                                                                        \
    CPUState *cpu = ENV_GET_CPU(__env1);                                    \
    int __mmu_idx, __index;                                                 \
    uintptr_t __retaddr;                                                    \
    __index = (v_addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);            \
    /* get the CPL, hence determine the MMU mode */                         \
    __mmu_idx = cpu_mmu_index(__env1, false);                               \
    /* We use this function in the implementation of atomic instructions */ \
    /* and we are going to modify these memory. So we use addr_write. */    \
    if (unlikely(__env1->tlb_table[__mmu_idx][__index].addr_write           \
                != ((v_addr & TARGET_PAGE_MASK) | tlb_version(__env1)))) {  \
        __retaddr = GETPC();                                                \
        tlb_fill(cpu, v_addr, 1, __mmu_idx, __retaddr);                     \
    }                                                                       \
    q_addr = v_addr + __env1->tlb_table[__mmu_idx][__index].addend;         \
} while(0)
#endif

/* XXX These are also used by atomic instruction handling.
 * Put these defines in some other files? */
#define DATA_b uint8_t
#define DATA_w uint16_t
#define DATA_l uint32_t
#define DATA_q uint64_t

#define __inline__ inline __attribute__((always_inline))

#if defined(__i386__) || defined(__x86_64__)
// Is this the correct way to detect 64 system?
#if defined(_LP64)
static __inline__ uint8_t
atomic_compare_exchange16b(uint64_t *memp,
                           uint64_t rax, uint64_t rdx,
                           uint64_t rbx, uint64_t rcx)
{
    uint8_t z;
    __asm __volatile__ ( "lock; cmpxchg16b %3\n\t"
                         "setz %2\n\t"
                         : "=a" (rax), "=d" (rdx), "=r" (z), "+m" (*memp)
                         : "a" (rax), "d" (rdx), "b" (rbx), "c" (rcx)
                         : "memory", "cc" );
    return z;
}
#else
static __inline__ uint8_t
atomic_compare_exchange16b(uint64_t *memp,
                           uint64_t rax, uint64_t rdx,
                           uint64_t rbx, uint64_t rcx)
{
    assert("atomic_compare_exchange16b: not supported.\n");
    exit(0);
}

static __inline__ uint8_t
atomic_compare_exchangeq(uint64_t *addr,
		uint64_t oldval, uint64_t newval)
{
    assert("atomic_compare_exchangeq: not supported.\n");
    exit(0);
}

#endif

/* Memory Barriers: x86-64 ONLY now */
#define mb()    asm volatile("mfence":::"memory")
#define rmb()   asm volatile("lfence":::"memory")
#define wmb()   asm volatile("sfence" ::: "memory")

#define LOCK_PREFIX "lock; "

#define coremu_xglue(a, b) a ## b
// If a/b is macro, it will expand first, then pass to coremu_xglue
#define coremu_glue(a, b) coremu_xglue(a, b)

#define coremu_xstr(s) # s
#define coremu_str(s) coremu_xstr(s)

#define DATA_BITS 8
#include "coremu-template.h"

#define DATA_BITS 16
#include "coremu-template.h"

#define DATA_BITS 32
#include "coremu-template.h"

#if defined(_LP64)
#define DATA_BITS 64
#include "coremu-template.h"
#else
static inline uint64_t atomic_exchangeq(uint64_t *p, uint64_t val)
{
    assert("atomic_exchangeq: not supported.\n");
    exit(0);
}

#endif

#elif defined(__arm__)

#if defined(__ARM_ARCH_7__)   || \
    defined(__ARM_ARCH_7A__)  || \
    defined(__ARM_ARCH_7EM__) || \
    defined(__ARM_ARCH_7M__)  || \
    defined(__ARM_ARCH_7R__)  || \
    defined(__ARM_ARCH_6J__)  || \
    defined(__ARM_ARCH_6K__)  || \
    defined(__ARM_ARCH_6T2__) || \
    defined(__ARM_ARCH_6Z__)  || \
    defined(__ARM_ARCH_6ZK__)
#define USE_ARMV6_INSTRUCTIONS
#endif

#ifdef USE_ARMV6_INSTRUCTIONS
#define mb()	__asm__ __volatile__("dmb" : : : "memory")
#define raw_local_irq_save(x)                                   \
        ({                                                      \
        __asm__ __volatile__(                                   \
        "mrs    %0, cpsr                @ local_irq_save\n"     \
        "cpsid  i"                                              \
        : "=r" (x) : : "memory", "cc");                         \
        })
#else
#define mb()    __asm__ __volatile__("":::"memory")
#define raw_local_irq_save(x)                                   \
        ({                                                      \
                unsigned long temp;                             \
                (void) (&temp == &x);                           \
        __asm__ __volatile__(                                   \
        "mrs    %0, cpsr                @ local_irq_save\n"     \
"       orr     %1, %0, #128\n"                                 \
"       msr     cpsr_c, %1"                                     \
        : "=r" (x), "=r" (temp)                                 \
        :                                                       \
        : "memory", "cc");                                      \
        })
#endif

#define raw_local_irq_restore(x)                                \
	__asm__ __volatile(                                     \
	"msr    cpsr_c, %0              @ local_irq_restore\n"  \
	:                                                       \
	: "r" (x)                                               \
	: "memory", "cc")

static __inline__ uint8_t atomic_compare_exchangeb(uint8_t *addr,
        uint8_t oldval, uint8_t newval)
{
    uint8_t ret;
#ifdef USE_ARMV6_INSTRUCTIONS
    unsigned long tmp;
    __asm__ __volatile__("@ atomic_cmpxchgl\n"
    "1:     ldrexb  %1, [%3]\n"
    "       mov    %0, #0\n"
    "       teq    %1, %4\n"
    "       strexbeq %0, %5, [%3]\n"
    "       teq    %0, #0\n"
    "       bne    1b\n"
            : "=&r" (tmp), "=&r" (ret), "+Qo" (*addr)
            : "r" (addr), "Ir" (oldval), "r" (newval)
            : "cc");
#else
    unsigned long flags;
    raw_local_irq_save(flags);
    ret = *addr;
    if (likely(ret == oldval))
        *addr = newval;
    raw_local_irq_restore(flags);
#endif
    return ret;
}

static __inline__ uint16_t atomic_compare_exchangew(uint16_t *addr,
        uint16_t oldval, uint16_t newval)
{
    uint16_t ret;
#ifdef USE_ARMV6_INSTRUCTIONS
    unsigned long tmp;
    __asm__ __volatile__("@ atomic_cmpxchgl\n"
    "1:     ldrexh  %1, [%3]\n"
    "       mov    %0, #0\n"
    "       teq    %1, %4\n"
    "       strexheq %0, %5, [%3]\n"
    "       teq    %0, #0\n"
    "       bne    1b\n"
            : "=&r" (tmp), "=&r" (ret), "+Qo" (*addr)
            : "r" (addr), "Ir" (oldval), "r" (newval)
            : "cc");
#else
    unsigned long flags;
    raw_local_irq_save(flags);
    ret = *addr;
    if (likely(ret == oldval))
        *addr = newval;
    raw_local_irq_restore(flags);
#endif
    return ret;
}

static __inline__ uint32_t atomic_compare_exchangel(uint32_t *addr,
        uint32_t oldval, uint32_t newval)
{
    uint32_t ret;
#ifdef USE_ARMV6_INSTRUCTIONS
    unsigned long tmp;
    __asm__ __volatile__("@ atomic_cmpxchgl\n"
    "1:     ldrex  %1, [%3]\n"
    "       mov    %0, #0\n"
    "       teq    %1, %4\n"
    "       strexeq %0, %5, [%3]\n"
    "       teq    %0, #0\n"
    "       bne    1b\n"
            : "=&r" (tmp), "=&r" (ret), "+Qo" (*addr)
            : "r" (addr), "Ir" (oldval), "r" (newval)
            : "cc");
#else
    unsigned long flags;
    raw_local_irq_save(flags);
    ret = *addr;
    if (likely(ret == oldval))
        *addr = newval;
    raw_local_irq_restore(flags);
#endif
    return ret;
}

static __inline__ uint64_t atomic_compare_exchangeq(uint64_t *addr,
        uint64_t oldval, uint64_t newval)
{
    uint64_t ret;
#ifdef USE_ARMV6_INSTRUCTIONS
    unsigned long tmp;
    __asm__ __volatile__("@ atomic_cmpxchgl\n"
    "1:     ldrexd  %1, %H1, [%3]\n"
    "       mov    %0, #0\n"
    "       teq    %1, %4\n"
    "       teqeq  %H1, %H4\n"
    "       strexdeq %0, %5, %H5, [%3]\n"
    "       teq    %0, #0\n"
    "       bne    1b\n"
            : "=&r" (tmp), "=&r" (ret), "+Qo" (*addr)
            : "r" (addr), "Ir" (oldval), "r" (newval)
            : "cc");
#else
    unsigned long flags;
    raw_local_irq_save(flags);
    ret = *addr;
    if (likely(ret == oldval))
        *addr = newval;
    raw_local_irq_restore(flags);
#endif
    return ret;
}

static __inline__ uint8_t
atomic_compare_exchange16b(uint64_t *memp,
                           uint64_t old_less, uint64_t old_most,
                           uint64_t new_less, uint64_t new_most)
{
    uint8_t ret = 0;
    unsigned long flags;
    raw_local_irq_save(flags);
    ret = *memp;
    if (likely(*memp == old_less && *(memp+1) == old_most))
    {
        *memp = new_less;
	*(memp+1) = new_most;
	ret = 1;
    }
    raw_local_irq_restore(flags);
    return ret;
}

static __inline__ unsigned long __xchg(unsigned long x, volatile void *ptr, int size)
{
    unsigned long ret;
#ifdef USE_ARMV6_INSTRUCTIONS
    unsigned int tmp;
#endif

    mb();

    switch (size) {
#ifdef USE_ARMV6_INSTRUCTIONS
        case 1:
		__asm __volatile("@	__xchg1\n"
		"1:	ldrexb	%0, [%3]\n"
		"	strexb	%1, %2, [%3]\n"
		"	teq	%1, #0\n"
		"	bne	1b"
			: "=&r" (ret), "=&r" (tmp)
			: "r" (x), "r" (ptr)
			: "memory", "cc");
		break;
        case 2:
		__asm __volatile("@	__xchg1\n"
		"1:	ldrexh	%0, [%3]\n"
		"	strexh	%1, %2, [%3]\n"
		"	teq	%1, #0\n"
		"	bne	1b"
			: "=&r" (ret), "=&r" (tmp)
			: "r" (x), "r" (ptr)
			: "memory", "cc");
		break;
	case 4:
		__asm __volatile("@	__xchg4\n"
		"1:	ldrex	%0, [%3]\n"
		"	strex	%1, %2, [%3]\n"
		"	teq	%1, #0\n"
		"	bne	1b"
			: "=&r" (ret), "=&r" (tmp)
			: "r" (x), "r" (ptr)
			: "memory", "cc");
		break;
#else
	case 1:
		__asm __volatile("@	__xchg1\n"
		"	swpb	%0, %1, [%2]"
			: "=&r" (ret)
			: "r" (x), "r" (ptr)
			: "memory", "cc");
		break;

	case 4:
		__asm __volatile("@	__xchg4\n"
		"	swp	%0, %1, [%2]"
			: "=&r" (ret)
			: "r" (x), "r" (ptr)
			: "memory", "cc");
		break;
	case 2:
		{
    		unsigned long flags = 0;
		raw_local_irq_save(flags);
		ret = *(volatile uint16_t *)ptr;
		*(volatile uint16_t *)ptr = x;
		raw_local_irq_restore(flags);
		break;
		}

#endif
	default:
		exit(0);
    }
    mb();

    return ret;
}

#define xchg(ptr,x) ((__typeof__(*(ptr)))__xchg((unsigned long)(x),(ptr),sizeof(*(ptr))))
#define GEN_ATOMIC_XCHG_HELPER(TYPE) \
static __inline__ DATA_##TYPE atomic_exchange##TYPE(DATA_##TYPE *p, DATA_##TYPE val) { return xchg(p, val); }

GEN_ATOMIC_XCHG_HELPER(b);
GEN_ATOMIC_XCHG_HELPER(w);
GEN_ATOMIC_XCHG_HELPER(l);

#endif

#endif /* _COREMU_ATOMIC_H */

OpenPOWER on IntegriCloud