sim-se: don't wake up SE futex syscalls on ARM events
[gem5.git] / src / sim / futex_map.cc
1 /*
2 * Copyright (c) 2017 Advanced Micro Devices, Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sim/futex_map.hh>
30
31 FutexKey::FutexKey(uint64_t addr_in, uint64_t tgid_in)
32 : addr(addr_in), tgid(tgid_in) {}
33
34 bool
35 FutexKey::operator==(const FutexKey &in) const
36 {
37 return addr == in.addr && tgid == in.tgid;
38 }
39
40 namespace std {
41 size_t hash<FutexKey>::operator()(const FutexKey& in) const
42 {
43 size_t hash = 65521;
44 for (int i = 0; i < sizeof(uint64_t) / sizeof(size_t); i++) {
45 hash ^= (size_t)(in.addr >> sizeof(size_t) * i) ^
46 (size_t)(in.tgid >> sizeof(size_t) * i);
47 }
48 return hash;
49 }
50 }
51
52 WaiterState::WaiterState(ThreadContext* _tc, int _bitmask)
53 : tc(_tc), bitmask(_bitmask) { }
54
55 bool
56 WaiterState::checkMask(int wakeup_bitmask) const
57 {
58 return bitmask & wakeup_bitmask;
59 }
60
61 void
62 FutexMap::suspend(Addr addr, uint64_t tgid, ThreadContext *tc)
63 {
64 suspend_bitset(addr, tgid, tc, 0xffffffff);
65 }
66
67 int
68 FutexMap::wakeup(Addr addr, uint64_t tgid, int count)
69 {
70 FutexKey key(addr, tgid);
71 auto it = find(key);
72
73 if (it == end())
74 return 0;
75
76 int woken_up = 0;
77 auto &waiterList = it->second;
78
79 while (!waiterList.empty() && woken_up < count) {
80 // Threads may be woken up by access to locked
81 // memory addresses outside of syscalls, so we
82 // must only count threads that were actually
83 // woken up by this syscall.
84 auto& tc = waiterList.front().tc;
85 tc->activate();
86 woken_up++;
87 waiterList.pop_front();
88 waitingTcs.erase(tc);
89 }
90
91 if (waiterList.empty())
92 erase(it);
93
94 return woken_up;
95 }
96
97 void
98 FutexMap::suspend_bitset(Addr addr, uint64_t tgid, ThreadContext *tc,
99 int bitmask)
100 {
101 FutexKey key(addr, tgid);
102 auto it = find(key);
103
104 if (it == end()) {
105 WaiterList waiterList {WaiterState(tc, bitmask)};
106 insert({key, waiterList});
107 } else {
108 it->second.push_back(WaiterState(tc, bitmask));
109 }
110 waitingTcs.emplace(tc);
111
112 /** Suspend the thread context */
113 tc->suspend();
114 }
115
116 int
117 FutexMap::wakeup_bitset(Addr addr, uint64_t tgid, int bitmask)
118 {
119 FutexKey key(addr, tgid);
120 auto it = find(key);
121
122 if (it == end())
123 return 0;
124
125 int woken_up = 0;
126
127 auto &waiterList = it->second;
128 auto iter = waiterList.begin();
129
130 while (iter != waiterList.end()) {
131 WaiterState& waiter = *iter;
132
133 if (waiter.checkMask(bitmask)) {
134 waiter.tc->activate();
135 iter = waiterList.erase(iter);
136 waitingTcs.erase(waiter.tc);
137 woken_up++;
138 } else {
139 ++iter;
140 }
141 }
142
143 if (waiterList.empty())
144 erase(it);
145
146 return woken_up;
147 }
148
149 int
150 FutexMap::requeue(Addr addr1, uint64_t tgid, int count, int count2, Addr addr2)
151 {
152 FutexKey key1(addr1, tgid);
153 auto it1 = find(key1);
154
155 if (it1 == end())
156 return 0;
157
158 int woken_up = 0;
159 auto &waiterList1 = it1->second;
160
161 while (!waiterList1.empty() && woken_up < count) {
162 waiterList1.front().tc->activate();
163 waiterList1.pop_front();
164 woken_up++;
165 }
166
167 WaiterList tmpList;
168 int requeued = 0;
169
170 while (!waiterList1.empty() && requeued < count2) {
171 auto w = waiterList1.front();
172 waiterList1.pop_front();
173 tmpList.push_back(w);
174 requeued++;
175 }
176
177 FutexKey key2(addr2, tgid);
178 auto it2 = find(key2);
179
180 if (it2 == end() && requeued > 0) {
181 insert({key2, tmpList});
182 } else {
183 it2->second.insert(it2->second.end(),
184 tmpList.begin(), tmpList.end());
185 }
186
187 if (waiterList1.empty())
188 erase(it1);
189
190 return woken_up + requeued;
191 }
192
193 bool
194 FutexMap::is_waiting(ThreadContext *tc)
195 {
196 return waitingTcs.find(tc) != waitingTcs.end();
197 }