2 * mem.spin: Promela code to validate memory barriers with OOO memory.
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.
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.
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.
18 * Copyright (c) 2009 Mathieu Desnoyers
21 /* Promela validation variables. */
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
32 #define PRODUCE_TOKENS(state, bits) \
33 state = (state) | (bits)
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))
40 #define CLEAR_TOKENS(state, bits) \
41 state = (state) & ~(bits)
45 #define get_pid() (_pid)
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
53 #define DECLARE_CACHED_VAR(type, x, v) \
55 type cached_##x[NR_PROCS] = v; \
56 bit cache_dirty_##x[NR_PROCS] = 0;
58 #define IS_CACHE_DIRTY(x, id) (cache_dirty_##x[id])
60 #define READ_CACHED_VAR(x) \
61 (cached_##x[get_pid()])
63 #define WRITE_CACHED_VAR(x, v) \
65 cached_##x[get_pid()] = v; \
66 cache_dirty_##x[get_pid()] = 1; \
69 #define CACHE_WRITE_TO_MEM(x, id) \
71 :: IS_CACHE_DIRTY(x, id) -> \
72 mem_##x = cached_##x[id]; \
73 cache_dirty_##x[id] = 0; \
78 #define CACHE_READ_FROM_MEM(x, id) \
80 :: !IS_CACHE_DIRTY(x, id) -> \
81 cached_##x[id] = mem_##x; \
87 * May update other caches if cache is dirty, or not.
89 #define RANDOM_CACHE_WRITE_TO_MEM(x, id) \
91 :: 1 -> CACHE_WRITE_TO_MEM(x, id); \
95 #define RANDOM_CACHE_READ_FROM_MEM(x, id)\
97 :: 1 -> CACHE_READ_FROM_MEM(x, id); \
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());
111 /* must consume all prior read tokens */
115 /* todo : consume all read tokens .. ? */
116 CACHE_READ_FROM_MEM(alpha, get_pid());
117 CACHE_READ_FROM_MEM(beta, get_pid());
121 /* must consume all prior write tokens */
125 CACHE_WRITE_TO_MEM(alpha, get_pid());
126 CACHE_WRITE_TO_MEM(beta, get_pid());
130 /* sync_core() must consume all prior read and write tokens, including rmb/wmb
133 /* must consume all prior read and write tokens */
143 /* Keep in sync manually with smp_rmb, wmp_wmb and ooo_mem */
144 DECLARE_CACHED_VAR(byte, alpha, 0);
145 DECLARE_CACHED_VAR(byte, beta, 0);
147 /* value 2 is uninitialized */
152 * Bit encoding, proc_one_produced :
155 #define P1_PROD_NONE (1 << 0)
157 #define P1_READ_ONE (1 << 1)
158 #define P1_RMB (1 << 2)
159 #define P1_READ_TWO (1 << 3)
161 int proc_one_produced;
163 active proctype test_proc_one()
165 assert(get_pid() < NR_PROCS);
167 PRODUCE_TOKENS(proc_one_produced, P1_PROD_NONE);
169 PRODUCE_TOKENS(proc_one_produced, P1_RMB);
173 :: CONSUME_TOKENS(proc_one_produced, P1_PROD_NONE, P1_READ_ONE) ->
175 read_one = READ_CACHED_VAR(beta);
177 PRODUCE_TOKENS(proc_one_produced, P1_READ_ONE);
178 :: CONSUME_TOKENS(proc_one_produced, P1_READ_ONE, P1_RMB) ->
180 PRODUCE_TOKENS(proc_one_produced, P1_RMB);
181 :: CONSUME_TOKENS(proc_one_produced, P1_RMB, P1_READ_TWO) ->
183 read_two = READ_CACHED_VAR(alpha);
185 PRODUCE_TOKENS(proc_one_produced, P1_READ_TWO);
186 :: CONSUME_TOKENS(proc_one_produced, P1_PROD_NONE | P1_READ_ONE
187 | P1_RMB | P1_READ_TWO, 0) ->
191 //CLEAR_TOKENS(proc_one_produced,
192 // P1_PROD_NONE | P1_READ_ONE | P1_RMB | P2_READ_TWO);
194 // test : [] (read_one == 1 -> read_two == 1)
195 assert(read_one != 1 || read_two == 1);
200 * Bit encoding, proc_two_produced :
203 #define P2_PROD_NONE (1 << 0)
205 #define P2_WRITE_ONE (1 << 1)
206 #define P2_WMB (1 << 2)
207 #define P2_WRITE_TWO (1 << 3)
209 int proc_two_produced;
211 active proctype test_proc_two()
213 assert(get_pid() < NR_PROCS);
215 PRODUCE_TOKENS(proc_two_produced, P2_PROD_NONE);
217 PRODUCE_TOKENS(proc_two_produced, P2_WMB);
221 :: CONSUME_TOKENS(proc_two_produced, P2_PROD_NONE, P2_WRITE_ONE) ->
223 WRITE_CACHED_VAR(alpha, 1);
225 PRODUCE_TOKENS(proc_two_produced, P2_WRITE_ONE);
226 :: CONSUME_TOKENS(proc_two_produced, P2_WRITE_ONE, P2_WMB) ->
228 PRODUCE_TOKENS(proc_two_produced, P2_WMB);
229 :: CONSUME_TOKENS(proc_two_produced, P2_WMB, P2_WRITE_TWO) ->
231 WRITE_CACHED_VAR(beta, 1);
233 PRODUCE_TOKENS(proc_two_produced, P2_WRITE_TWO);
234 :: CONSUME_TOKENS(proc_two_produced, P2_PROD_NONE | P2_WRITE_ONE
235 | P2_WMB | P2_WRITE_TWO, 0) ->
239 //CLEAR_TOKENS(proc_two_produced,
240 // P2_PROD_NONE | P2_WRITE_ONE | P2_WMB | P2_WRITE_TWO);