Update out of order memory models to include instruction scheduling
[urcu.git] / formal-model / ooomem-double-update / mem.spin
... / ...
CommitLineData
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 control and data flow. Updated after each instruction to
25 * show which variables are ready. Using one-hot bit encoding per variable to
26 * save state space. Used as triggers to execute the instructions having those
27 * variables as input. Leaving bits active to inhibit instruction execution.
28 * Scheme used to make instruction disabling and automatic dependency fall-back
29 * automatic.
30 */
31
32#define CONSUME_TOKENS(state, bits, notbits) \
33 ((!(state & (notbits))) && (state & (bits)) == (bits))
34
35#define PRODUCE_TOKENS(state, bits) \
36 state = state | (bits);
37
38#define CLEAR_TOKENS(state, bits) \
39 state = state & ~(bits)
40
41#define NR_PROCS 2
42
43#define get_pid() (_pid)
44
45/*
46 * Each process have its own data in cache. Caches are randomly updated.
47 * smp_wmb and smp_rmb forces cache updates (write and read), wmb_mb forces
48 * both.
49 */
50
51#define DECLARE_CACHED_VAR(type, x, v) \
52 type mem_##x = v; \
53 type cached_##x[NR_PROCS] = v; \
54 bit cache_dirty_##x[NR_PROCS] = 0;
55
56#define IS_CACHE_DIRTY(x, id) (cache_dirty_##x[id])
57
58#define READ_CACHED_VAR(x) \
59 (cached_##x[get_pid()])
60
61#define WRITE_CACHED_VAR(x, v) \
62 atomic { \
63 cached_##x[get_pid()] = v; \
64 cache_dirty_##x[get_pid()] = 1; \
65 }
66
67#define CACHE_WRITE_TO_MEM(x, id) \
68 if \
69 :: IS_CACHE_DIRTY(x, id) -> \
70 mem_##x = cached_##x[id]; \
71 cache_dirty_##x[id] = 0; \
72 :: else -> \
73 skip \
74 fi;
75
76#define CACHE_READ_FROM_MEM(x, id) \
77 if \
78 :: !IS_CACHE_DIRTY(x, id) -> \
79 cached_##x[id] = mem_##x; \
80 :: else -> \
81 skip \
82 fi;
83
84/*
85 * May update other caches if cache is dirty, or not.
86 */
87#define RANDOM_CACHE_WRITE_TO_MEM(x, id) \
88 if \
89 :: 1 -> CACHE_WRITE_TO_MEM(x, id); \
90 :: 1 -> skip \
91 fi;
92
93#define RANDOM_CACHE_READ_FROM_MEM(x, id)\
94 if \
95 :: 1 -> CACHE_READ_FROM_MEM(x, id); \
96 :: 1 -> skip \
97 fi;
98
99inline ooo_mem()
100{
101 atomic {
102 RANDOM_CACHE_WRITE_TO_MEM(alpha, get_pid());
103 RANDOM_CACHE_WRITE_TO_MEM(beta, get_pid());
104 RANDOM_CACHE_READ_FROM_MEM(alpha, get_pid());
105 RANDOM_CACHE_READ_FROM_MEM(beta, get_pid());
106 }
107}
108
109/* must consume all prior read tokens */
110inline smp_rmb()
111{
112 atomic {
113 /* todo : consume all read tokens .. ? */
114 CACHE_READ_FROM_MEM(alpha, get_pid());
115 CACHE_READ_FROM_MEM(beta, get_pid());
116 }
117}
118
119/* must consume all prior write tokens */
120inline smp_wmb()
121{
122 atomic {
123 CACHE_WRITE_TO_MEM(alpha, get_pid());
124 CACHE_WRITE_TO_MEM(beta, get_pid());
125 }
126}
127
128/* sync_core() must consume all prior read and write tokens, including rmb/wmb
129 * tokens */
130
131/* must consume all prior read and write tokens */
132inline smp_mb()
133{
134 atomic {
135 smp_wmb();
136 /* sync_core() */
137 smp_rmb();
138 }
139}
140
141/* Keep in sync manually with smp_rmb, wmp_wmb and ooo_mem */
142DECLARE_CACHED_VAR(byte, alpha, 0);
143DECLARE_CACHED_VAR(byte, beta, 0);
144
145/* value 2 is uninitialized */
146byte read_one = 2;
147byte read_two = 2;
148
149/*
150 * Bit encoding, proc_one_produced :
151 */
152
153#define P1_PROD_NONE (1 << 0)
154
155#define P1_READ_ONE (1 << 1)
156#define P1_RMB (1 << 2)
157#define P1_READ_TWO (1 << 3)
158
159/* Only need a single color. */
160byte proc_one_produced;
161
162active proctype test_proc_one()
163{
164 assert(get_pid() < NR_PROCS);
165
166 PRODUCE_TOKENS(proc_one_produced, P1_PROD_NONE);
167#ifdef NO_RMB
168 PRODUCE_TOKENS(proc_one_produced, P1_RMB);
169#endif
170
171 do
172 :: CONSUME_TOKENS(proc_one_produced,
173 P1_PROD_NONE, P1_READ_ONE) ->
174 ooo_mem();
175 read_one = READ_CACHED_VAR(beta);
176 ooo_mem();
177 PRODUCE_TOKENS(proc_one_produced, P1_READ_ONE);
178 :: CONSUME_TOKENS(proc_one_produced,
179 P1_READ_ONE, P1_RMB) ->
180 smp_rmb();
181 PRODUCE_TOKENS(proc_one_produced, P1_RMB);
182 :: CONSUME_TOKENS(proc_one_produced,
183 P1_RMB, P1_READ_TWO) ->
184 ooo_mem();
185 read_two = READ_CACHED_VAR(alpha);
186 ooo_mem();
187 PRODUCE_TOKENS(proc_one_produced, P1_READ_TWO);
188 :: CONSUME_TOKENS(proc_one_produced,
189 P1_PROD_NONE | P1_READ_ONE | P1_RMB
190 | P1_READ_TWO, 0) ->
191 break;
192 od;
193
194 //CLEAR_TOKENS(proc_one_produced,
195 // P1_PROD_NONE | P1_READ_ONE | P1_RMB | P1_READ_TWO);
196
197 // test : [] (read_one == 1 -> read_two == 1)
198 assert(read_one != 1 || read_two == 1);
199}
200
201
202/*
203 * Bit encoding, proc_two_produced :
204 */
205
206#define P2_PROD_NONE (1 << 0)
207
208#define P2_WRITE_ONE (1 << 1)
209#define P2_WMB (1 << 2)
210#define P2_WRITE_TWO (1 << 3)
211
212/* Only need a single color. */
213byte proc_two_produced;
214
215active proctype test_proc_two()
216{
217 assert(get_pid() < NR_PROCS);
218
219 PRODUCE_TOKENS(proc_two_produced, P2_PROD_NONE);
220#ifdef NO_WMB
221 PRODUCE_TOKENS(proc_two_produced, P2_WMB);
222#endif
223
224 do
225 :: CONSUME_TOKENS(proc_two_produced,
226 P2_PROD_NONE, P2_WRITE_ONE) ->
227 ooo_mem();
228 WRITE_CACHED_VAR(alpha, 1);
229 ooo_mem();
230 PRODUCE_TOKENS(proc_two_produced, P2_WRITE_ONE);
231 :: CONSUME_TOKENS(proc_two_produced,
232 P2_WRITE_ONE, P2_WMB) ->
233 smp_wmb();
234 PRODUCE_TOKENS(proc_two_produced, P2_WMB);
235 :: CONSUME_TOKENS(proc_two_produced,
236 P2_WMB, P2_WRITE_TWO) ->
237 ooo_mem();
238 WRITE_CACHED_VAR(beta, 1);
239 ooo_mem();
240 PRODUCE_TOKENS(proc_two_produced, P2_WRITE_TWO);
241 :: CONSUME_TOKENS(proc_two_produced,
242 P2_PROD_NONE | P2_WRITE_ONE
243 | P2_WMB | P2_WRITE_TWO, 0) ->
244 break;
245 od;
246
247 //CLEAR_TOKENS(proc_two_produced,
248 // P2_PROD_NONE | P2_WRITE_ONE | P2_WMB | P2_WRITE_TWO);
249}
This page took 0.022861 seconds and 4 git commands to generate.