formal verif : move bits produced declarations closer to processes
[urcu.git] / formal-model / ooomem-two-writes / mem.spin
CommitLineData
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
03c9e0f3
MD
43#define NR_PROCS 2
44
45#define get_pid() (_pid)
46
47/*
48 * Each process have its own data in cache. Caches are randomly updated.
49 * smp_wmb and smp_rmb forces cache updates (write and read), wmb_mb forces
50 * both.
51 */
52
53#define DECLARE_CACHED_VAR(type, x, v) \
54 type mem_##x = v; \
55 type cached_##x[NR_PROCS] = v; \
56 bit cache_dirty_##x[NR_PROCS] = 0;
57
58#define IS_CACHE_DIRTY(x, id) (cache_dirty_##x[id])
59
60#define READ_CACHED_VAR(x) \
61 (cached_##x[get_pid()])
62
63#define WRITE_CACHED_VAR(x, v) \
64 atomic { \
65 cached_##x[get_pid()] = v; \
66 cache_dirty_##x[get_pid()] = 1; \
67 }
68
69#define CACHE_WRITE_TO_MEM(x, id) \
70 if \
71 :: IS_CACHE_DIRTY(x, id) -> \
72 mem_##x = cached_##x[id]; \
73 cache_dirty_##x[id] = 0; \
74 :: else -> \
75 skip \
76 fi;
77
78#define CACHE_READ_FROM_MEM(x, id) \
79 if \
80 :: !IS_CACHE_DIRTY(x, id) -> \
81 cached_##x[id] = mem_##x; \
82 :: else -> \
83 skip \
84 fi;
85
86/*
87 * May update other caches if cache is dirty, or not.
88 */
89#define RANDOM_CACHE_WRITE_TO_MEM(x, id) \
90 if \
91 :: 1 -> CACHE_WRITE_TO_MEM(x, id); \
92 :: 1 -> skip \
93 fi;
94
95#define RANDOM_CACHE_READ_FROM_MEM(x, id)\
96 if \
97 :: 1 -> CACHE_READ_FROM_MEM(x, id); \
98 :: 1 -> skip \
99 fi;
100
101inline ooo_mem()
102{
103 atomic {
104 RANDOM_CACHE_WRITE_TO_MEM(alpha, get_pid());
105 RANDOM_CACHE_WRITE_TO_MEM(beta, get_pid());
106 RANDOM_CACHE_READ_FROM_MEM(alpha, get_pid());
107 RANDOM_CACHE_READ_FROM_MEM(beta, get_pid());
108 }
109}
110
111/* must consume all prior read tokens */
112inline smp_rmb()
113{
114 atomic {
115 /* todo : consume all read tokens .. ? */
116 CACHE_READ_FROM_MEM(alpha, get_pid());
117 CACHE_READ_FROM_MEM(beta, get_pid());
118 }
119}
120
121/* must consume all prior write tokens */
122inline smp_wmb()
123{
124 atomic {
125 CACHE_WRITE_TO_MEM(alpha, get_pid());
126 CACHE_WRITE_TO_MEM(beta, get_pid());
127 }
128}
129
130/* sync_core() must consume all prior read and write tokens, including rmb/wmb
131 * tokens */
132
133/* must consume all prior read and write tokens */
134inline smp_mb()
135{
136 atomic {
137 smp_wmb();
138 /* sync_core() */
139 smp_rmb();
140 }
141}
142
143/* Keep in sync manually with smp_rmb, wmp_wmb and ooo_mem */
144DECLARE_CACHED_VAR(byte, alpha, 0);
145DECLARE_CACHED_VAR(byte, beta, 0);
146
147/* value 2 is uninitialized */
148byte read_one = 2;
149byte read_two = 2;
150
3db2d75b
MD
151/*
152 * Bit encoding, proc_one_produced :
153 */
154
155#define P1_PROD_NONE (1 << 0)
156
157#define P1_WRITE (1 << 1)
158#define P1_WMB (1 << 2)
159#define P1_SYNC_CORE (1 << 3)
160#define P1_RMB (1 << 4)
161#define P1_READ (1 << 5)
162
163int proc_one_produced;
164
03c9e0f3
MD
165active proctype test_proc_one()
166{
167 assert(get_pid() < NR_PROCS);
168
169 PRODUCE_TOKENS(proc_one_produced, P1_PROD_NONE);
170
171#ifdef NO_WMB
172 PRODUCE_TOKENS(proc_one_produced, P1_WMB);
173#endif
174#ifdef NO_RMB
175 PRODUCE_TOKENS(proc_one_produced, P1_RMB);
176#endif
4b8839f1
MD
177#ifdef NO_SYNC
178 PRODUCE_TOKENS(proc_one_produced, P1_SYNC_CORE);
179#endif
03c9e0f3
MD
180
181 do
182 :: CONSUME_TOKENS(proc_one_produced, P1_PROD_NONE, P1_WRITE) ->
183 ooo_mem();
184 WRITE_CACHED_VAR(alpha, 1);
185 ooo_mem();
186 PRODUCE_TOKENS(proc_one_produced, P1_WRITE);
187 :: CONSUME_TOKENS(proc_one_produced, P1_WRITE, P1_WMB) ->
188 smp_wmb();
189 PRODUCE_TOKENS(proc_one_produced, P1_WMB);
190 :: CONSUME_TOKENS(proc_one_produced, P1_WRITE | P1_WMB, P1_SYNC_CORE) ->
191 /* sync_core(); */
192 PRODUCE_TOKENS(proc_one_produced, P1_SYNC_CORE);
193 :: CONSUME_TOKENS(proc_one_produced, P1_SYNC_CORE, P1_RMB) ->
194 smp_rmb();
195 PRODUCE_TOKENS(proc_one_produced, P1_RMB);
196 :: CONSUME_TOKENS(proc_one_produced, P1_RMB | P1_SYNC_CORE, P1_READ) ->
197 ooo_mem();
198 read_one = READ_CACHED_VAR(beta);
199 ooo_mem();
200 PRODUCE_TOKENS(proc_one_produced, P1_READ);
201 :: CONSUME_TOKENS(proc_one_produced, P1_PROD_NONE | P1_WRITE
202 | P1_WMB | P1_SYNC_CORE | P1_RMB | P1_READ, 0) ->
203 break;
204 od;
205
206 //CLEAR_TOKENS(proc_one_produced,
207 // P1_PROD_NONE | P1_WRITE | P1_WMB | P1_SYNC_CORE | P1_RMB |
208 // P2_READ);
209
210 // test : [] (read_one == 0 -> read_two != 0)
211 // test : [] (read_two == 0 -> read_one != 0)
212 assert(!(read_one == 0 && read_two == 0));
213}
214
3db2d75b
MD
215
216/*
217 * Bit encoding, proc_two_produced :
218 */
219
220#define P2_PROD_NONE (1 << 0)
221
222#define P2_WRITE (1 << 1)
223#define P2_WMB (1 << 2)
224#define P2_SYNC_CORE (1 << 3)
225#define P2_RMB (1 << 4)
226#define P2_READ (1 << 5)
227
228int proc_two_produced;
229
03c9e0f3
MD
230active proctype test_proc_two()
231{
232 assert(get_pid() < NR_PROCS);
233
234 PRODUCE_TOKENS(proc_two_produced, P2_PROD_NONE);
235
236#ifdef NO_WMB
237 PRODUCE_TOKENS(proc_two_produced, P2_WMB);
238#endif
239#ifdef NO_RMB
240 PRODUCE_TOKENS(proc_two_produced, P2_RMB);
241#endif
4b8839f1
MD
242#ifdef NO_SYNC
243 PRODUCE_TOKENS(proc_two_produced, P2_SYNC_CORE);
244#endif
03c9e0f3
MD
245
246 do
247 :: CONSUME_TOKENS(proc_two_produced, P2_PROD_NONE, P2_WRITE) ->
248 ooo_mem();
249 WRITE_CACHED_VAR(beta, 1);
250 ooo_mem();
251 PRODUCE_TOKENS(proc_two_produced, P2_WRITE);
252 :: CONSUME_TOKENS(proc_two_produced, P2_WRITE, P2_WMB) ->
253 smp_wmb();
254 PRODUCE_TOKENS(proc_two_produced, P2_WMB);
255 :: CONSUME_TOKENS(proc_two_produced, P2_WRITE | P2_WMB, P2_SYNC_CORE) ->
256 /* sync_core(); */
257 PRODUCE_TOKENS(proc_two_produced, P2_SYNC_CORE);
258 :: CONSUME_TOKENS(proc_two_produced, P2_SYNC_CORE, P2_RMB) ->
259 smp_rmb();
260 PRODUCE_TOKENS(proc_two_produced, P2_RMB);
261 :: CONSUME_TOKENS(proc_two_produced, P2_SYNC_CORE | P2_RMB, P2_READ) ->
262 ooo_mem();
263 read_two = READ_CACHED_VAR(alpha);
264 ooo_mem();
265 PRODUCE_TOKENS(proc_two_produced, P2_READ);
266 :: CONSUME_TOKENS(proc_two_produced, P2_PROD_NONE | P2_WRITE
267 | P2_WMB | P2_SYNC_CORE | P2_RMB | P2_READ, 0) ->
268 break;
269 od;
270
271 //CLEAR_TOKENS(proc_two_produced,
272 // P2_PROD_NONE | P2_WRITE | P2_WMB | P2_SYNC_CORE | P2_RMB |
273 // P2_READ);
274
275 // test : [] (read_one == 0 -> read_two != 0)
276 // test : [] (read_two == 0 -> read_one != 0)
277 assert(!(read_one == 0 && read_two == 0));
278}
This page took 0.032981 seconds and 4 git commands to generate.