-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpikoRT.pml
396 lines (362 loc) · 10 KB
/
pikoRT.pml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#include "variables.pml"
#include "helper.pml"
#include "ti.pml"
#include "sched.pml"
#include "softirq.pml"
#include "mutex.pml"
#include "cond.pml"
#ifdef LTL
#include "specifications.pml"
#endif
bit data_ready, cs_c, cs_p;
#define get_pending(irq, pending) get_bit(irq - 2, pending)
inline set_pending(irq, pending)
{
assert(PendSV < irq && irq < USER0);
set_bit(irq - 2, pending)
}
inline clear_pending(irq, pending)
{
assert(PendSV < irq && irq < USER0);
clear_bit(irq - 2, pending)
}
/* return maxima exception priority from pending status. If the exception
* has same priority, return the smaller exception number first.
* http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0552a/BABICDEB.html
*/
inline get_max_pending(ret)
{
assert(ret == UNKNOWN);
/* SVC will not be pending, and pending of PendSV has no effect here */
for (idx: 1 .. (USER0 - 1)) {
if
:: idx == 1 ->
if
:: PendSV_pending && ret == UNKNOWN && ATStack[1] != PendSV ->
ret = idx
:: else
fi
:: get_pending(idx, irq_pending) && ret == UNKNOWN ->
ret = idx
:: get_pending(idx, irq_pending) && (irq_prio[idx] < irq_prio[ret]) ->
ret = idx
:: else
fi
}
idx = 0;
/* always has a max priority exception */
assert(ret != UNKNOWN)
}
/* similar to tail-chaining */
inline change_AT_directly(proc)
{
assert(PendSV <= proc && proc < USER0);
//assert(ghost_direct_AT < (1 << (proc - 2)));
if
:: proc == PendSV ->
PendSV_ghost_direct_AT = 1
:: else ->
set_pending(proc, ghost_direct_AT);
fi
AT = proc
}
inline push_and_change_AT(proc)
{
/* XXX: this might be false if SOFTIRQ is greater than 7 */
ATTop = ATTop + 1;
assert(ATTop < NBATSTACK);
ATStack[ATTop] = AT;
AT = proc
}
inline pop_ATStack_to_AT()
{
AT = ATStack[ATTop];
ATStack[ATTop] = UNKNOWN;
assert(ATTop >= 0);
ATTop = ATTop - 1
}
// XXX: We can not simulate the exception preempts itself on Spin.
// If the coming task had been executed (in ATStack), it will be wrong.
inline inATStack(proc)
{
FOR_ATTOP_IDX {
if
:: ATStack[idx] == proc -> assert(false)
:: else
fi
}
idx = 0
}
inline ITake(proc)
{
do
:: atomic { proc == AT ->
/* XXX: The irq is triggered by the running of process. It will not
* trigger again while the related interrupt process are running. */
assert(PendSV < proc && proc < USER0);
assert(get_pending(proc, ghost_direct_AT));
/* the proc can not be self */
/* change AT directly from IRet or irq_pending from
* interrupt_policy, similar to tail-chaining */
inATStack(proc);
clear_pending(proc, ghost_direct_AT);
clear_pending(proc, irq_pending);
break
}
:: atomic { AT >= USER0 ->
/* the exception always takes while user task is running
* and there remain nothing in ATStack */
assert(ATTop <= 0 && proc != SVC);
/* if PendSV proc user task, setting the pending bit of PendSV
* has no side-effect */
if
:: irq_pending == 0 && PendSV_pending == 0 ->
inATStack(proc);
push_and_change_AT(proc);
break
:: else
fi
}
:: atomic { else ->
/* nested interrupt: running < USER0
* compare the priority of pending and preemtive exception */
assert(!get_pending(proc, ghost_direct_AT));
if
:: irq_prio[proc] < irq_prio[AT] ->
/* proc directly, and not from irq_pending */
inATStack(proc);
/* late arrival
* current process AT is changed by change_AT_directly and been
* proc by higher priority exception, however, AT has not
* process the ITake to clear the ghost bit. Thus, can not push
* to the ATStack. */
if
:: PendSV < AT && AT < USER0 && get_pending(AT, ghost_direct_AT) ->
clear_pending(AT, ghost_direct_AT);
AT = proc
:: else ->
push_and_change_AT(proc)
fi;
break
:: else ->
set_pending(proc, irq_pending);
fi
}
od
}
inline PendSVTake()
{
do
:: atomic { PendSV_pending && (AT == PendSV) ->
assert(PendSV_ghost_direct_AT);
inATStack(PendSV);
PendSV_pending = 0;
PendSV_ghost_direct_AT = 0;
break
}
:: atomic { PendSV_pending && (AT >= USER0) ->
assert(ATTop <= 0 && AT != PendSV);
if
:: irq_pending == 0 ->
inATStack(PendSV);
push_and_change_AT(PendSV);
PendSV_pending = 0;
:: else
fi
break
}
:: else
od
}
inline IRet()
{
assert(retPolicy == false);
if
:: irq_pending != 0 ->
/* ignore SVC and PendSV */
get_max_pending(max_prio);
/* interrupt_policy(max_prio, ATStack[ATTop], retPolicy) */
if
:: max_prio == ATStack[ATTop] -> assert(false)
:: ATStack[ATTop] >= USER0 -> retPolicy = true
:: irq_prio[max_prio] < irq_prio[ATStack[ATTop]] ->
assert(!get_pending(max_prio, ghost_direct_AT)); retPolicy = true
:: else
fi
:: else -> assert(max_prio == UNKNOWN)
fi;
if
:: retPolicy ->
change_AT_directly(max_prio); retPolicy = false
:: else ->
pop_ATStack_to_AT()
fi;
max_prio = UNKNOWN;
/**
* reset local monitor: 14.1.7. Exclusive monitor system location
* B1.5.8 Exception return behavior
*/
local_monitor = 0
}
inline sys_call(__svc_type, __svc_chan)
{
d_step {
assert(ATTop < 0 && irq_pending == 0 && ghost_direct_AT == 0);
push_and_change_AT(SVC)
};
/* rendezvous chan will block the process, need to place outside d_step */
__svc_chan ! __svc_type;
(evalPID == AT)
}
/* -------------
* all processes
* ------------ */
proctype svc(chan svc_chan)
{
byte idx, max_prio = UNKNOWN, nextUser = UNKNOWN;
bool retPolicy, del_queue_check;
mutex_head mutex_list;
cond_head cond_list;
mtype:svc_t svc_type;
assert(evalPID == SVC);
loop:
svc_chan ? svc_type;
if
:: SELE(evalPID, svc_type == SYS_MUTEX_LOCK) ->
sys_pthread_mutex_lock(evalPID)
:: SELE(evalPID, svc_type == SYS_MUTEX_UNLOCK) ->
sys_pthread_mutex_unlock(evalPID)
:: SELE(evalPID, svc_type == SYS_COND_WAIT) ->
sys_pthread_cond_wait(evalPID)
:: SELE(evalPID, svc_type == SYS_COND_SIGNAL) ->
sys_pthread_cond_signal(evalPID)
:: SELE(evalPID, svc_type == SYS_PTHREAD_YIELD) ->
sched_enqueue(curUser, evalPID);
sched_elect(SCHED_OPT_NONE, evalPID)
fi;
AWAITS(evalPID, IRet());
goto loop
}
proctype pendsv()
{
byte idx, max_prio = UNKNOWN, nextUser = UNKNOWN;
bool retPolicy, del_queue_check;
assert(evalPID == PendSV);
loop:
PendSVTake();
sched_elect(SCHED_OPT_TICK, evalPID);
AWAITS(evalPID, IRet());
goto loop
}
proctype systick()
{
byte idx, max_prio = UNKNOWN;
bool retPolicy, softirq_run;
assert(PendSV < evalPID && evalPID < USER0);
loop:
ITake(evalPID);
tasklet_schedule(BH_SYSTICK, TIMER_SOFTIRQ_PRIO, evalPID);
AWAITS(evalPID, PendSV_pending = 1);
iret:
AWAITS(evalPID, IRet());
goto loop
}
proctype interrupts()
{
byte idx, max_prio = UNKNOWN;
bool retPolicy;
assert(PendSV < evalPID && evalPID < USER0);
loop:
ITake(evalPID);
/* using stm32_uartx_isr() as interrupt example
* this isr will not influence the scheduling behavior
* only updates charactor buffer and calls an empty callback func */
skip;
iret:
AWAITS(evalPID, IRet());
goto loop
}
proctype consumer(chan svc_chan)
{
bit ne;
assert(USER0 <= evalPID && evalPID < SOFTIRQ);
loop:
mutex_lock(mutex, cs_c, svc_chan, evalPID);
do
:: A_AWAITS(evalPID,
if
:: !data_ready ->
cs_c = 0; sys_call(SYS_COND_WAIT, svc_chan);
sys_call(SYS_MUTEX_LOCK, svc_chan); cs_c = 1
:: else -> break
fi )
od;
cs:
A_AWAITS(evalPID, d_step { assert(!cs_p); data_ready = 0 } );
A_AWAITS(evalPID, assert(!cs_p); sys_call(SYS_COND_SIGNAL, svc_chan));
mutex_unlock(mutex, cs_c, svc_chan, evalPID);
goto loop
}
proctype producer(chan svc_chan)
{
bit ne;
assert(USER0 <= evalPID && evalPID < SOFTIRQ);
loop:
mutex_lock(mutex, cs_p, svc_chan, evalPID);
do
:: A_AWAITS(evalPID,
if
:: data_ready ->
cs_p = 0; sys_call(SYS_COND_WAIT, svc_chan);
sys_call(SYS_MUTEX_LOCK, svc_chan); cs_p = 1
:: else -> break
fi )
od;
cs:
A_AWAITS(evalPID, d_step { assert(!cs_c); data_ready = 1 } );
A_AWAITS(evalPID, assert(!cs_c); sys_call(SYS_COND_SIGNAL, svc_chan));
mutex_unlock(mutex, cs_p, svc_chan, evalPID);
goto loop
}
/* softirq is in non-privileged mode */
proctype softirq(chan svc_chan)
{
byte idx, max_prio;
bool del_queue_check;
mtype:tasklet_t next_tasklet = NO_BH_TASK;
assert(evalPID == SOFTIRQ);
loop:
tasklet_action(next_tasklet, evalPID);
/* softirqd thread should not return */
/* sched yield */
A_AWAITS(evalPID, assert(next_tasklet == NO_BH_TASK); sys_call(SYS_PTHREAD_YIELD, svc_chan));
goto loop
}
init
{
byte idx, idx2;
chan svc_chan = [0] of { mtype:svc_t };
d_step {
system_initialize();
thread_info_initialize()
};
atomic {
run svc(svc_chan);
run pendsv();
for (idx: 2 .. (USER0 - 1)) {
if
:: idx == (PendSV + 1) -> run systick()
:: else -> run interrupts()
fi
}
idx = 0;
for (idx: USER0 .. (USER0 + NBUSERS - 1)) {
if
:: idx == USER0 -> run consumer(svc_chan)
:: idx == (USER0 + 1) -> run producer(svc_chan)
fi
}
idx = 0;
run softirq(svc_chan)
}
}