9fc540ae67745390c0e5f7000024f129572c9c08
[urcu.git] / formal-model / ooomem-double-update-minimal / mem.spin
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_READ_ONE (1 << 1)
50 #define P1_RMB (1 << 2)
51 #define P1_READ_TWO (1 << 3)
52
53 int proc_one_produced;
54
55 #define P2_PROD_NONE (1 << 0)
56
57 #define P2_WRITE_ONE (1 << 1)
58 #define P2_WMB (1 << 2)
59 #define P2_WRITE_TWO (1 << 3)
60
61 int proc_two_produced;
62
63 #define NR_PROCS 2
64
65 #define get_pid() (_pid)
66
67 /*
68 * Each process have its own data in cache. Caches are randomly updated.
69 * smp_wmb and smp_rmb forces cache updates (write and read), wmb_mb forces
70 * both.
71 */
72
73 #define DECLARE_CACHED_VAR(type, x, v) \
74 type mem_##x = v; \
75 type cached_##x[NR_PROCS] = v; \
76 bit cache_dirty_##x[NR_PROCS] = 0;
77
78 #define IS_CACHE_DIRTY(x, id) (cache_dirty_##x[id])
79
80 #define READ_CACHED_VAR(x) \
81 (cached_##x[get_pid()])
82
83 #define WRITE_CACHED_VAR(x, v) \
84 atomic { \
85 cached_##x[get_pid()] = v; \
86 cache_dirty_##x[get_pid()] = 1; \
87 }
88
89 #define CACHE_WRITE_TO_MEM(x, id) \
90 if \
91 :: IS_CACHE_DIRTY(x, id) -> \
92 mem_##x = cached_##x[id]; \
93 cache_dirty_##x[id] = 0; \
94 :: else -> \
95 skip \
96 fi;
97
98 #define CACHE_READ_FROM_MEM(x, id) \
99 if \
100 :: !IS_CACHE_DIRTY(x, id) -> \
101 cached_##x[id] = mem_##x; \
102 :: else -> \
103 skip \
104 fi;
105
106 /*
107 * May update other caches if cache is dirty, or not.
108 */
109 #define RANDOM_CACHE_WRITE_TO_MEM(x, id) \
110 if \
111 :: 1 -> CACHE_WRITE_TO_MEM(x, id); \
112 :: 1 -> skip \
113 fi;
114
115 #define RANDOM_CACHE_READ_FROM_MEM(x, id)\
116 if \
117 :: 1 -> CACHE_READ_FROM_MEM(x, id); \
118 :: 1 -> skip \
119 fi;
120
121 inline ooo_mem()
122 {
123 atomic {
124 RANDOM_CACHE_WRITE_TO_MEM(alpha, get_pid());
125 RANDOM_CACHE_WRITE_TO_MEM(beta, get_pid());
126 RANDOM_CACHE_READ_FROM_MEM(alpha, get_pid());
127 RANDOM_CACHE_READ_FROM_MEM(beta, get_pid());
128 }
129 }
130
131 /* must consume all prior read tokens */
132 inline smp_rmb()
133 {
134 atomic {
135 /* todo : consume all read tokens .. ? */
136 CACHE_READ_FROM_MEM(alpha, get_pid());
137 CACHE_READ_FROM_MEM(beta, get_pid());
138 }
139 }
140
141 /* must consume all prior write tokens */
142 inline smp_wmb()
143 {
144 atomic {
145 CACHE_WRITE_TO_MEM(alpha, get_pid());
146 CACHE_WRITE_TO_MEM(beta, get_pid());
147 }
148 }
149
150 /* sync_core() must consume all prior read and write tokens, including rmb/wmb
151 * tokens */
152
153 /* must consume all prior read and write tokens */
154 inline smp_mb()
155 {
156 atomic {
157 smp_wmb();
158 /* sync_core() */
159 smp_rmb();
160 }
161 }
162
163 /* Keep in sync manually with smp_rmb, wmp_wmb and ooo_mem */
164 DECLARE_CACHED_VAR(byte, alpha, 0);
165 DECLARE_CACHED_VAR(byte, beta, 0);
166
167 /* value 2 is uninitialized */
168 byte read_one = 2;
169 byte read_two = 2;
170
171 active proctype test_proc_one()
172 {
173 assert(get_pid() < NR_PROCS);
174
175 PRODUCE_TOKENS(proc_one_produced, P1_PROD_NONE);
176 #ifdef NO_RMB
177 PRODUCE_TOKENS(proc_one_produced, P1_RMB);
178 #endif
179
180 do
181 :: CONSUME_TOKENS(proc_one_produced, P1_PROD_NONE, P1_READ_ONE) ->
182 ooo_mem();
183 read_one = READ_CACHED_VAR(beta);
184 ooo_mem();
185 PRODUCE_TOKENS(proc_one_produced, P1_READ_ONE);
186 :: CONSUME_TOKENS(proc_one_produced, P1_READ_ONE, P1_RMB) ->
187 smp_rmb();
188 PRODUCE_TOKENS(proc_one_produced, P1_RMB);
189 :: CONSUME_TOKENS(proc_one_produced, P1_RMB, P1_READ_TWO) ->
190 ooo_mem();
191 read_two = READ_CACHED_VAR(alpha);
192 ooo_mem();
193 PRODUCE_TOKENS(proc_one_produced, P1_READ_TWO);
194 :: CONSUME_TOKENS(proc_one_produced, P1_PROD_NONE | P1_READ_ONE
195 | P1_RMB | P1_READ_TWO, 0) ->
196 break;
197 od;
198
199 //CLEAR_TOKENS(proc_one_produced,
200 // P1_PROD_NONE | P1_READ_ONE | P1_RMB | P2_READ_TWO);
201
202 // test : [] (read_one == 1 -> read_two == 1)
203 assert(read_one != 1 || read_two == 1);
204 }
205
206 active proctype test_proc_two()
207 {
208 assert(get_pid() < NR_PROCS);
209
210 PRODUCE_TOKENS(proc_two_produced, P2_PROD_NONE);
211 #ifdef NO_WMB
212 PRODUCE_TOKENS(proc_two_produced, P2_WMB);
213 #endif
214
215 do
216 :: CONSUME_TOKENS(proc_two_produced, P2_PROD_NONE, P2_WRITE_ONE) ->
217 ooo_mem();
218 WRITE_CACHED_VAR(alpha, 1);
219 ooo_mem();
220 PRODUCE_TOKENS(proc_two_produced, P2_WRITE_ONE);
221 :: CONSUME_TOKENS(proc_two_produced, P2_WRITE_ONE, P2_WMB) ->
222 smp_wmb();
223 PRODUCE_TOKENS(proc_two_produced, P2_WMB);
224 :: CONSUME_TOKENS(proc_two_produced, P2_WMB, P2_WRITE_TWO) ->
225 ooo_mem();
226 WRITE_CACHED_VAR(beta, 1);
227 ooo_mem();
228 PRODUCE_TOKENS(proc_two_produced, P2_WRITE_TWO);
229 :: CONSUME_TOKENS(proc_two_produced, P2_PROD_NONE | P2_WRITE_ONE
230 | P2_WMB | P2_WRITE_TWO, 0) ->
231 break;
232 od;
233
234 //CLEAR_TOKENS(proc_two_produced,
235 // P2_PROD_NONE | P2_WRITE_ONE | P2_WMB | P2_WRITE_TWO);
236 }
This page took 0.033553 seconds and 3 git commands to generate.