sim-se: don't wake up SE futex syscalls on ARM events
[gem5.git] / src / sim / futex_map.hh
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 #ifndef __FUTEX_MAP_HH__
30 #define __FUTEX_MAP_HH__
31
32 #include <unordered_map>
33 #include <unordered_set>
34
35 #include <cpu/thread_context.hh>
36
37 /**
38 * FutexKey class defines an unique identifier for a particular futex in the
39 * system. The tgid and an address are the unique values needed as the key.
40 */
41 class FutexKey {
42 public:
43 uint64_t addr;
44 uint64_t tgid;
45
46 FutexKey(uint64_t addr_in, uint64_t tgid_in);
47
48 bool operator==(const FutexKey &in) const;
49 };
50
51 namespace std {
52 /**
53 * The unordered_map structure needs the parenthesis operator defined for
54 * std::hash if a user defined key is used. Our key is is user defined
55 * so we need to provide the hash functor.
56 */
57 template <>
58 struct hash<FutexKey>
59 {
60 size_t operator()(const FutexKey& in) const;
61 };
62 }
63
64 /**
65 * WaiterState defines internal state of a waiter thread. The state
66 * includes a pointer to the thread's context and its associated bitmask.
67 */
68 class WaiterState {
69 public:
70 ThreadContext* tc;
71 int bitmask;
72
73 /**
74 * this constructor is used if futex ops with bitset are used
75 */
76 WaiterState(ThreadContext* _tc, int _bitmask);
77
78 /**
79 * return true if the bit-wise AND of the wakeup_bitmask given by
80 * a waking thread and this thread's internal bitmask is non-zero
81 */
82 bool checkMask(int wakeup_bitmask) const;
83 };
84
85 typedef std::list<WaiterState> WaiterList;
86
87 /**
88 * FutexMap class holds a map of all futexes used in the system
89 */
90 class FutexMap : public std::unordered_map<FutexKey, WaiterList>
91 {
92 public:
93 /** Inserts a futex into the map with one waiting TC */
94 void suspend(Addr addr, uint64_t tgid, ThreadContext *tc);
95
96 /** Wakes up at most count waiting threads on a futex */
97 int wakeup(Addr addr, uint64_t tgid, int count);
98
99 void suspend_bitset(Addr addr, uint64_t tgid, ThreadContext *tc,
100 int bitmask);
101
102 int wakeup_bitset(Addr addr, uint64_t tgid, int bitmask);
103
104 /**
105 * This operation wakes a given number (val) of waiters. If there are
106 * more threads waiting than woken, they are removed from the wait
107 * queue of the futex pointed to by addr1 and added to the wait queue
108 * of the futex pointed to by addr2. The number of waiter moved is
109 * capped by count2 (misused timeout parameter).
110 *
111 * The return value is the number of waiters that are woken or
112 * requeued.
113 */
114 int requeue(Addr addr1, uint64_t tgid, int count, int count2, Addr addr2);
115
116 /**
117 * Determine if the given thread context is currently waiting on a
118 * futex wait operation on any of the futexes tracked by this FutexMap.
119 */
120 bool is_waiting(ThreadContext *tc);
121
122 private:
123
124 std::unordered_set<ThreadContext *> waitingTcs;
125 };
126
127 #endif // __FUTEX_MAP_HH__