summaryrefslogtreecommitdiffstats
path: root/llvm/atomic/atomic-arm.c
blob: 4176caaecfd8b063229f4a070a61ff6481a89228 (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
/*
 * 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>
 *
 * 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/>.
 */

/* We include this file in op_helper.c */

#include <stdlib.h>
#include <pthread.h>
#include "coremu-atomic.h"

__thread uint64_t cm_exclusive_val;
__thread uint32_t cm_exclusive_addr = -1;

#define GEN_LOAD_EXCLUSIVE(type, TYPE) \
void HELPER(load_exclusive##type)(CPUArchState *env, uint32_t reg,    \
                uint32_t addr)                                        \
{                                                                     \
    unsigned long q_addr = 0;                                         \
    DATA_##type val = 0;                                              \
                                                                      \
    cm_exclusive_addr = addr;                                         \
    CM_GET_QEMU_ADDR(env, q_addr,addr);                               \
    val = *(DATA_##type *)q_addr;                                     \
    cm_exclusive_val = val;                                           \
    env->regs[reg] = val;                                             \
}

GEN_LOAD_EXCLUSIVE(b, B);
GEN_LOAD_EXCLUSIVE(w, W);
GEN_LOAD_EXCLUSIVE(l, L);
//GEN_LOAD_EXCLUSIVE(q, Q);

#define GEN_STORE_EXCLUSIVE(type, TYPE) \
void HELPER(store_exclusive##type)(CPUArchState *env, uint32_t res,           \
                uint32_t reg, uint32_t addr)                                  \
{                                                                             \
    unsigned long q_addr = 0;                                                 \
    DATA_##type val = 0;                                                      \
    DATA_##type r = 0;                                                        \
                                                                              \
    if(addr != cm_exclusive_addr)                                             \
        goto fail;                                                            \
                                                                              \
    CM_GET_QEMU_ADDR(env, q_addr,addr);                                       \
    val = (DATA_##type)env->regs[reg];                                        \
                                                                              \
    r = atomic_compare_exchange##type((DATA_##type *)q_addr,                  \
                                    (DATA_##type)cm_exclusive_val, val);      \
                                                                              \
    if(r == (DATA_##type)cm_exclusive_val) {                                  \
        env->regs[res] = 0;                                                   \
        goto done;                                                            \
    } else {                                                                  \
        goto fail;                                                            \
    }                                                                         \
                                                                              \
fail:                                                                         \
    env->regs[res] = 1;                                                       \
                                                                              \
done:                                                                         \
    cm_exclusive_addr = -1;                                                   \
    return;                                                                   \
}

GEN_STORE_EXCLUSIVE(b, B);
GEN_STORE_EXCLUSIVE(w, W);
GEN_STORE_EXCLUSIVE(l, L);
//GEN_STORE_EXCLUSIVE(q, Q);

void HELPER(load_exclusiveq)(CPUArchState *env, uint32_t reg, uint32_t addr)
{
   unsigned long q_addr = 0;
   uint64_t val = 0;

   cm_exclusive_addr = addr;
   CM_GET_QEMU_ADDR(env, q_addr,addr);
   val = *(uint64_t *)q_addr;
   cm_exclusive_val = val;
   env->regs[reg] = (uint32_t)val;
   env->regs[reg + 1] = (uint32_t)(val>>32);
}

void HELPER(store_exclusiveq)(CPUArchState *env, uint32_t res, uint32_t reg, uint32_t addr)
{
   unsigned long q_addr = 0;
   uint64_t val = 0;
   uint64_t r = 0;

   if(addr != cm_exclusive_addr)
        goto fail;

   CM_GET_QEMU_ADDR(env, q_addr,addr);
   val = (uint32_t)env->regs[reg];
   val |= ((uint64_t)env->regs[reg + 1]) << 32;

   r = atomic_compare_exchangeq((uint64_t *)q_addr,
                                    (uint64_t)cm_exclusive_val, val);

   if(r == (uint64_t)cm_exclusive_val) {
        env->regs[res] = 0;
        goto done;
   } else {
        goto fail;
   }

fail:
    env->regs[res] = 1;

done:
    cm_exclusive_addr = -1;
    return;
}

void HELPER(clear_exclusive)(CPUArchState *env)
{
    cm_exclusive_addr = -1;
}

void HELPER(swpb)(CPUArchState *env, uint32_t dst, uint32_t src, uint32_t addr)
{
    uint8_t old, val;
    unsigned long q_addr;
    CM_GET_QEMU_ADDR(env, q_addr,env->regs[addr]);
    val = (uint8_t)env->regs[src];
    old = atomic_exchangeb((uint8_t *)q_addr, (uint8_t)val);
    env->regs[dst] = old;
    //printf("SWPB\n");
}

void HELPER(swp)(CPUArchState *env, uint32_t dst, uint32_t src, uint32_t addr)
{
    uint32_t old, val;
    unsigned long q_addr;
    CM_GET_QEMU_ADDR(env, q_addr,env->regs[addr]);
    val = env->regs[src];
    old = atomic_exchangel((uint32_t *)q_addr, val);
    env->regs[dst] = old;
    //printf("SWP\n");
}
OpenPOWER on IntegriCloud