Commit | Line | Data |
---|---|---|
03c9e0f3 MD |
1 | /* |
2 | * mem.spin: Promela code to validate memory barriers with OOO memory. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 2 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program; if not, write to the Free Software | |
16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
17 | * | |
18 | * Copyright (c) 2009 Mathieu Desnoyers | |
19 | */ | |
20 | ||
21 | /* Promela validation variables. */ | |
22 | ||
23 | /* | |
24 | * Produced process data flow. Updated after each instruction to show which | |
25 | * variables are ready. Assigned using SSA (static single assignment) (defuse | |
26 | * analysis must be done on the program to map "real" variables to single define | |
27 | * followed by use). Using one-hot bit encoding per variable to save state | |
28 | * space. Used as triggers to execute the instructions having those variables | |
29 | * as input. | |
30 | */ | |
31 | ||
32 | #define PRODUCE_TOKENS(state, bits) \ | |
33 | state = (state) | (bits) | |
34 | ||
35 | /* All bits must be active to consume. All notbits must be inactive. */ | |
36 | /* Consuming a token does not clear it, it just waits for it. */ | |
37 | #define CONSUME_TOKENS(state, bits, notbits) \ | |
38 | ((!((state) & (notbits))) && ((state) & (bits)) == (bits)) | |
39 | ||
40 | #define CLEAR_TOKENS(state, bits) \ | |
41 | state = (state) & ~(bits) | |
42 | ||
43 | /* | |
44 | * Bit encoding, proc_one_produced : | |
45 | */ | |
46 | ||
47 | #define P1_PROD_NONE (1 << 0) | |
48 | ||
49 | #define P1_WRITE (1 << 1) | |
50 | #define P1_WMB (1 << 2) | |
51 | #define P1_SYNC_CORE (1 << 3) | |
52 | #define P1_RMB (1 << 4) | |
53 | #define P1_READ (1 << 5) | |
54 | ||
55 | int proc_one_produced; | |
56 | ||
57 | #define P2_PROD_NONE (1 << 0) | |
58 | ||
59 | #define P2_WRITE (1 << 1) | |
60 | #define P2_WMB (1 << 2) | |
61 | #define P2_SYNC_CORE (1 << 3) | |
62 | #define P2_RMB (1 << 4) | |
63 | #define P2_READ (1 << 5) | |
64 | ||
65 | int proc_two_produced; | |
66 | ||
67 | #define NR_PROCS 2 | |
68 | ||
69 | #define get_pid() (_pid) | |
70 | ||
71 | /* | |
72 | * Each process have its own data in cache. Caches are randomly updated. | |
73 | * smp_wmb and smp_rmb forces cache updates (write and read), wmb_mb forces | |
74 | * both. | |
75 | */ | |
76 | ||
77 | #define DECLARE_CACHED_VAR(type, x, v) \ | |
78 | type mem_##x = v; \ | |
79 | type cached_##x[NR_PROCS] = v; \ | |
80 | bit cache_dirty_##x[NR_PROCS] = 0; | |
81 | ||
82 | #define IS_CACHE_DIRTY(x, id) (cache_dirty_##x[id]) | |
83 | ||
84 | #define READ_CACHED_VAR(x) \ | |
85 | (cached_##x[get_pid()]) | |
86 | ||
87 | #define WRITE_CACHED_VAR(x, v) \ | |
88 | atomic { \ | |
89 | cached_##x[get_pid()] = v; \ | |
90 | cache_dirty_##x[get_pid()] = 1; \ | |
91 | } | |
92 | ||
93 | #define CACHE_WRITE_TO_MEM(x, id) \ | |
94 | if \ | |
95 | :: IS_CACHE_DIRTY(x, id) -> \ | |
96 | mem_##x = cached_##x[id]; \ | |
97 | cache_dirty_##x[id] = 0; \ | |
98 | :: else -> \ | |
99 | skip \ | |
100 | fi; | |
101 | ||
102 | #define CACHE_READ_FROM_MEM(x, id) \ | |
103 | if \ | |
104 | :: !IS_CACHE_DIRTY(x, id) -> \ | |
105 | cached_##x[id] = mem_##x; \ | |
106 | :: else -> \ | |
107 | skip \ | |
108 | fi; | |
109 | ||
110 | /* | |
111 | * May update other caches if cache is dirty, or not. | |
112 | */ | |
113 | #define RANDOM_CACHE_WRITE_TO_MEM(x, id) \ | |
114 | if \ | |
115 | :: 1 -> CACHE_WRITE_TO_MEM(x, id); \ | |
116 | :: 1 -> skip \ | |
117 | fi; | |
118 | ||
119 | #define RANDOM_CACHE_READ_FROM_MEM(x, id)\ | |
120 | if \ | |
121 | :: 1 -> CACHE_READ_FROM_MEM(x, id); \ | |
122 | :: 1 -> skip \ | |
123 | fi; | |
124 | ||
125 | inline ooo_mem() | |
126 | { | |
127 | atomic { | |
128 | RANDOM_CACHE_WRITE_TO_MEM(alpha, get_pid()); | |
129 | RANDOM_CACHE_WRITE_TO_MEM(beta, get_pid()); | |
130 | RANDOM_CACHE_READ_FROM_MEM(alpha, get_pid()); | |
131 | RANDOM_CACHE_READ_FROM_MEM(beta, get_pid()); | |
132 | } | |
133 | } | |
134 | ||
135 | /* must consume all prior read tokens */ | |
136 | inline smp_rmb() | |
137 | { | |
138 | atomic { | |
139 | /* todo : consume all read tokens .. ? */ | |
140 | CACHE_READ_FROM_MEM(alpha, get_pid()); | |
141 | CACHE_READ_FROM_MEM(beta, get_pid()); | |
142 | } | |
143 | } | |
144 | ||
145 | /* must consume all prior write tokens */ | |
146 | inline smp_wmb() | |
147 | { | |
148 | atomic { | |
149 | CACHE_WRITE_TO_MEM(alpha, get_pid()); | |
150 | CACHE_WRITE_TO_MEM(beta, get_pid()); | |
151 | } | |
152 | } | |
153 | ||
154 | /* sync_core() must consume all prior read and write tokens, including rmb/wmb | |
155 | * tokens */ | |
156 | ||
157 | /* must consume all prior read and write tokens */ | |
158 | inline smp_mb() | |
159 | { | |
160 | atomic { | |
161 | smp_wmb(); | |
162 | /* sync_core() */ | |
163 | smp_rmb(); | |
164 | } | |
165 | } | |
166 | ||
167 | /* Keep in sync manually with smp_rmb, wmp_wmb and ooo_mem */ | |
168 | DECLARE_CACHED_VAR(byte, alpha, 0); | |
169 | DECLARE_CACHED_VAR(byte, beta, 0); | |
170 | ||
171 | /* value 2 is uninitialized */ | |
172 | byte read_one = 2; | |
173 | byte read_two = 2; | |
174 | ||
175 | active proctype test_proc_one() | |
176 | { | |
177 | assert(get_pid() < NR_PROCS); | |
178 | ||
179 | PRODUCE_TOKENS(proc_one_produced, P1_PROD_NONE); | |
180 | ||
181 | #ifdef NO_WMB | |
182 | PRODUCE_TOKENS(proc_one_produced, P1_WMB); | |
183 | #endif | |
184 | #ifdef NO_RMB | |
185 | PRODUCE_TOKENS(proc_one_produced, P1_RMB); | |
186 | #endif | |
187 | ||
188 | do | |
189 | :: CONSUME_TOKENS(proc_one_produced, P1_PROD_NONE, P1_WRITE) -> | |
190 | ooo_mem(); | |
191 | WRITE_CACHED_VAR(alpha, 1); | |
192 | ooo_mem(); | |
193 | PRODUCE_TOKENS(proc_one_produced, P1_WRITE); | |
194 | :: CONSUME_TOKENS(proc_one_produced, P1_WRITE, P1_WMB) -> | |
195 | smp_wmb(); | |
196 | PRODUCE_TOKENS(proc_one_produced, P1_WMB); | |
197 | :: CONSUME_TOKENS(proc_one_produced, P1_WRITE | P1_WMB, P1_SYNC_CORE) -> | |
198 | /* sync_core(); */ | |
199 | PRODUCE_TOKENS(proc_one_produced, P1_SYNC_CORE); | |
200 | :: CONSUME_TOKENS(proc_one_produced, P1_SYNC_CORE, P1_RMB) -> | |
201 | smp_rmb(); | |
202 | PRODUCE_TOKENS(proc_one_produced, P1_RMB); | |
203 | :: CONSUME_TOKENS(proc_one_produced, P1_RMB | P1_SYNC_CORE, P1_READ) -> | |
204 | ooo_mem(); | |
205 | read_one = READ_CACHED_VAR(beta); | |
206 | ooo_mem(); | |
207 | PRODUCE_TOKENS(proc_one_produced, P1_READ); | |
208 | :: CONSUME_TOKENS(proc_one_produced, P1_PROD_NONE | P1_WRITE | |
209 | | P1_WMB | P1_SYNC_CORE | P1_RMB | P1_READ, 0) -> | |
210 | break; | |
211 | od; | |
212 | ||
213 | //CLEAR_TOKENS(proc_one_produced, | |
214 | // P1_PROD_NONE | P1_WRITE | P1_WMB | P1_SYNC_CORE | P1_RMB | | |
215 | // P2_READ); | |
216 | ||
217 | // test : [] (read_one == 0 -> read_two != 0) | |
218 | // test : [] (read_two == 0 -> read_one != 0) | |
219 | assert(!(read_one == 0 && read_two == 0)); | |
220 | } | |
221 | ||
222 | active proctype test_proc_two() | |
223 | { | |
224 | assert(get_pid() < NR_PROCS); | |
225 | ||
226 | PRODUCE_TOKENS(proc_two_produced, P2_PROD_NONE); | |
227 | ||
228 | #ifdef NO_WMB | |
229 | PRODUCE_TOKENS(proc_two_produced, P2_WMB); | |
230 | #endif | |
231 | #ifdef NO_RMB | |
232 | PRODUCE_TOKENS(proc_two_produced, P2_RMB); | |
233 | #endif | |
234 | ||
235 | do | |
236 | :: CONSUME_TOKENS(proc_two_produced, P2_PROD_NONE, P2_WRITE) -> | |
237 | ooo_mem(); | |
238 | WRITE_CACHED_VAR(beta, 1); | |
239 | ooo_mem(); | |
240 | PRODUCE_TOKENS(proc_two_produced, P2_WRITE); | |
241 | :: CONSUME_TOKENS(proc_two_produced, P2_WRITE, P2_WMB) -> | |
242 | smp_wmb(); | |
243 | PRODUCE_TOKENS(proc_two_produced, P2_WMB); | |
244 | :: CONSUME_TOKENS(proc_two_produced, P2_WRITE | P2_WMB, P2_SYNC_CORE) -> | |
245 | /* sync_core(); */ | |
246 | PRODUCE_TOKENS(proc_two_produced, P2_SYNC_CORE); | |
247 | :: CONSUME_TOKENS(proc_two_produced, P2_SYNC_CORE, P2_RMB) -> | |
248 | smp_rmb(); | |
249 | PRODUCE_TOKENS(proc_two_produced, P2_RMB); | |
250 | :: CONSUME_TOKENS(proc_two_produced, P2_SYNC_CORE | P2_RMB, P2_READ) -> | |
251 | ooo_mem(); | |
252 | read_two = READ_CACHED_VAR(alpha); | |
253 | ooo_mem(); | |
254 | PRODUCE_TOKENS(proc_two_produced, P2_READ); | |
255 | :: CONSUME_TOKENS(proc_two_produced, P2_PROD_NONE | P2_WRITE | |
256 | | P2_WMB | P2_SYNC_CORE | P2_RMB | P2_READ, 0) -> | |
257 | break; | |
258 | od; | |
259 | ||
260 | //CLEAR_TOKENS(proc_two_produced, | |
261 | // P2_PROD_NONE | P2_WRITE | P2_WMB | P2_SYNC_CORE | P2_RMB | | |
262 | // P2_READ); | |
263 | ||
264 | // test : [] (read_one == 0 -> read_two != 0) | |
265 | // test : [] (read_two == 0 -> read_one != 0) | |
266 | assert(!(read_one == 0 && read_two == 0)); | |
267 | } |