-
Notifications
You must be signed in to change notification settings - Fork 108
/
Ipc_A.thy
561 lines (510 loc) · 24.2 KB
/
Ipc_A.thy
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
(*
* Copyright 2014, General Dynamics C4 Systems
*
* SPDX-License-Identifier: GPL-2.0-only
*)
(*
Specification of Inter-Process Communication.
*)
chapter "IPC"
theory Ipc_A
imports Tcb_A ArchFault_A
begin
arch_requalify_consts (A)
lookup_ipc_buffer
make_arch_fault_msg
handle_arch_fault_reply
section \<open>Getting and setting the message info register.\<close>
definition
get_message_info :: "obj_ref \<Rightarrow> (message_info,'z::state_ext) s_monad"
where
"get_message_info thread \<equiv> do
x \<leftarrow> as_user thread $ getRegister msg_info_register;
return $ data_to_message_info x
od"
section \<open>IPC Capability Transfers\<close>
definition
remove_rights :: "cap_rights \<Rightarrow> cap \<Rightarrow> cap"
where
"remove_rights rights cap \<equiv> cap_rights_update (cap_rights cap - rights) cap"
text \<open>In addition to the data payload a message may also contain capabilities.
When a thread requests additional capabilities be transferred the identities of
those capabilities are retreived from the thread's IPC buffer.\<close>
definition
buffer_cptr_index :: nat
where
"buffer_cptr_index \<equiv> (msg_max_length + 2)"
primrec
get_extra_cptrs :: "obj_ref option \<Rightarrow> message_info \<Rightarrow> (cap_ref list,'z::state_ext) s_monad"
where
"get_extra_cptrs (Some buf) mi =
(liftM (map data_to_cptr) $ mapM (load_word_offs buf)
[buffer_cptr_index ..< buffer_cptr_index + (unat (mi_extra_caps mi))])"
| "get_extra_cptrs None mi = return []"
definition
get_extra_cptr :: "obj_ref \<Rightarrow> nat \<Rightarrow> (cap_ref,'z::state_ext) s_monad"
where
"get_extra_cptr buffer n \<equiv> liftM data_to_cptr
(load_word_offs buffer (n + buffer_cptr_index))"
text \<open>This function both looks up the addresses of the additional capabilities
and retreives them from the sender's CSpace.\<close>
definition
lookup_extra_caps :: "obj_ref \<Rightarrow> data option \<Rightarrow> message_info \<Rightarrow> ((cap \<times> cslot_ptr) list,'z::state_ext) f_monad" where
"lookup_extra_caps thread buffer mi \<equiv> doE
cptrs \<leftarrow> liftE $ get_extra_cptrs buffer mi;
mapME (\<lambda>cptr. cap_fault_on_failure (of_bl cptr) False $ lookup_cap_and_slot thread cptr) cptrs
odE"
text \<open>Capability transfers. Capabilities passed along with a message are split
into two groups. Capabilities to the same endpoint as the message is passed
through are not copied. Their badges are unwrapped and stored in the receiver's
message buffer instead. Other capabilities are copied into the given slots.
Capability unwrapping allows a client to efficiently demonstrate to a server
that it possesses authority to two or more services that server provides.
\<close>
definition
set_extra_badge :: "obj_ref \<Rightarrow> machine_word \<Rightarrow> nat \<Rightarrow> (unit,'z::state_ext) s_monad"
where
"set_extra_badge buffer badge n \<equiv>
store_word_offs buffer (buffer_cptr_index + n) badge"
type_synonym transfer_caps_data = "(cap \<times> cslot_ptr) list \<times> cslot_ptr list"
fun
transfer_caps_loop :: "obj_ref option \<Rightarrow> obj_ref \<Rightarrow> nat
\<Rightarrow> (cap \<times> cslot_ptr) list \<Rightarrow> cslot_ptr list
\<Rightarrow> message_info \<Rightarrow> (message_info,'z::state_ext) s_monad"
where
"transfer_caps_loop ep rcv_buffer n [] slots
mi = return (MI (mi_length mi) (of_nat n) (mi_caps_unwrapped mi)
(mi_label mi))"
| "transfer_caps_loop ep rcv_buffer n ((cap, slot) # morecaps)
slots mi =
const_on_failure (MI (mi_length mi) (of_nat n) (mi_caps_unwrapped mi)
(mi_label mi)) (doE
transfer_rest \<leftarrow> returnOk $ transfer_caps_loop ep
rcv_buffer (n + 1) morecaps;
if (is_ep_cap cap \<and> ep = Some (obj_ref_of cap))
then doE
liftE $ set_extra_badge rcv_buffer (cap_ep_badge cap) n;
liftE $ transfer_rest slots (MI (mi_length mi) (mi_extra_caps mi)
(mi_caps_unwrapped mi || (1 << n)) (mi_label mi))
odE
else if slots \<noteq> []
then doE
cap' \<leftarrow> derive_cap slot cap;
whenE (cap' = NullCap) $ throwError undefined;
liftE $ cap_insert cap' slot (hd slots);
liftE $ transfer_rest (tl slots) mi
odE
else returnOk (MI (mi_length mi) (of_nat n) (mi_caps_unwrapped mi)
(mi_label mi))
odE)"
definition
transfer_caps :: "message_info \<Rightarrow> (cap \<times> cslot_ptr) list \<Rightarrow>
obj_ref option \<Rightarrow> obj_ref \<Rightarrow> obj_ref option \<Rightarrow>
(message_info,'z::state_ext) s_monad"
where
"transfer_caps info caps endpoint receiver recv_buffer \<equiv> do
dest_slots \<leftarrow> get_receive_slots receiver recv_buffer;
mi' \<leftarrow> return $ MI (mi_length info) 0 0 (mi_label info);
case recv_buffer of
None \<Rightarrow> return mi'
| Some receive_buffer \<Rightarrow>
transfer_caps_loop endpoint receive_buffer 0 caps dest_slots mi'
od"
section \<open>Fault Handling\<close>
text \<open>Threads fault when they attempt to access services that are not backed
by any resources. Such a thread is then blocked and a fault messages is sent to
its supervisor. When a reply to that message is sent the thread is reactivated.
\<close>
text \<open>Format a message for a given fault type.\<close>
fun
make_fault_msg :: "fault \<Rightarrow> obj_ref \<Rightarrow> (data \<times> data list,'z::state_ext) s_monad"
where
"make_fault_msg (CapFault cptr rp lf) thread = (do
pc \<leftarrow> as_user thread getRestartPC;
return (1, pc # cptr # (if rp then 1 else 0) # msg_from_lookup_failure lf)
od)"
| "make_fault_msg (UnknownSyscallException n) thread = (do
msg \<leftarrow> as_user thread $ mapM getRegister syscallMessage;
return (2, msg @ [n])
od)"
| "make_fault_msg (UserException exception code) thread = (do
msg \<leftarrow> as_user thread $ mapM getRegister exceptionMessage;
return (3, msg @ [exception, code])
od)"
| "make_fault_msg (ArchFault af) thread = make_arch_fault_msg af thread " (* arch_fault *)
text \<open>React to a fault reply. The reply message is interpreted in a manner
that depends on the type of the original fault. For some fault types a thread
reconfiguration is performed. This is done entirely to save the fault message
recipient an additional system call. This function returns a boolean indicating
whether the thread should now be restarted.\<close>
fun
handle_fault_reply :: "fault \<Rightarrow> obj_ref \<Rightarrow>
data \<Rightarrow> data list \<Rightarrow> (bool,'z::state_ext) s_monad"
where
"handle_fault_reply (CapFault cptr rp lf) thread x y = return True"
| "handle_fault_reply (UnknownSyscallException n) thread label msg = do
t \<leftarrow> arch_get_sanitise_register_info thread;
as_user thread $ zipWithM_x
(\<lambda>r v. setRegister r $ sanitise_register t r v)
syscallMessage msg;
return (label = 0)
od"
| "handle_fault_reply (UserException exception code) thread label msg = do
t \<leftarrow> arch_get_sanitise_register_info thread;
as_user thread $ zipWithM_x
(\<lambda>r v. setRegister r $ sanitise_register t r v)
exceptionMessage msg;
return (label = 0)
od"
| " handle_fault_reply (ArchFault af) thread label msg =
handle_arch_fault_reply af thread label msg" (* arch_fault *)
text \<open>Transfer a fault message from a faulting thread to its supervisor.\<close>
definition
do_fault_transfer :: "data \<Rightarrow> obj_ref \<Rightarrow> obj_ref
\<Rightarrow> obj_ref option \<Rightarrow> (unit,'z::state_ext) s_monad"
where
"do_fault_transfer badge sender receiver buf \<equiv> do
fault \<leftarrow> thread_get tcb_fault sender;
f \<leftarrow> (case fault of
Some f \<Rightarrow> return f
| None \<Rightarrow> fail);
(label, msg) \<leftarrow> make_fault_msg f sender;
sent \<leftarrow> set_mrs receiver buf msg;
set_message_info receiver $ MI sent 0 0 label;
as_user receiver $ setRegister badge_register badge
od"
section \<open>Synchronous Message Transfers\<close>
text \<open>Transfer a non-fault message.\<close>
definition
do_normal_transfer :: "obj_ref \<Rightarrow> obj_ref option \<Rightarrow> obj_ref option
\<Rightarrow> data \<Rightarrow> bool \<Rightarrow> obj_ref
\<Rightarrow> obj_ref option
\<Rightarrow> (unit,'z::state_ext) s_monad"
where
"do_normal_transfer sender sbuf endpoint badge grant
receiver rbuf \<equiv>
do
mi \<leftarrow> get_message_info sender;
caps \<leftarrow> if grant then lookup_extra_caps sender sbuf mi <catch> K (return [])
else return [];
mrs_transferred \<leftarrow> copy_mrs sender sbuf receiver rbuf (mi_length mi);
mi' \<leftarrow> transfer_caps mi caps endpoint receiver rbuf;
set_message_info receiver $ MI mrs_transferred (mi_extra_caps mi')
(mi_caps_unwrapped mi') (mi_label mi);
as_user receiver $ setRegister badge_register badge
od"
text \<open>Transfer a message either involving a fault or not.\<close>
definition
do_ipc_transfer :: "obj_ref \<Rightarrow> obj_ref option \<Rightarrow>
badge \<Rightarrow> bool \<Rightarrow> obj_ref \<Rightarrow> (unit,'z::state_ext) s_monad"
where
"do_ipc_transfer sender ep badge grant
receiver \<equiv> do
recv_buffer \<leftarrow> lookup_ipc_buffer True receiver;
fault \<leftarrow> thread_get tcb_fault sender;
case fault
of None \<Rightarrow> do
send_buffer \<leftarrow> lookup_ipc_buffer False sender;
do_normal_transfer sender send_buffer ep badge grant
receiver recv_buffer
od
| Some f \<Rightarrow> do_fault_transfer badge sender receiver recv_buffer
od"
text \<open>Transfer a reply message and delete the one-use Reply capability.\<close>
definition
do_reply_transfer :: "obj_ref \<Rightarrow> obj_ref \<Rightarrow> cslot_ptr \<Rightarrow> bool \<Rightarrow> (unit,'z::state_ext) s_monad"
where
"do_reply_transfer sender receiver slot grant \<equiv> do
state \<leftarrow> get_thread_state receiver;
assert (state = BlockedOnReply);
fault \<leftarrow> thread_get tcb_fault receiver;
case fault of
None \<Rightarrow> do
do_ipc_transfer sender None 0 grant receiver;
cap_delete_one slot;
set_thread_state receiver Running;
do_extended_op (possible_switch_to receiver)
od
| Some f \<Rightarrow> do
cap_delete_one slot;
mi \<leftarrow> get_message_info sender;
buf \<leftarrow> lookup_ipc_buffer False sender;
mrs \<leftarrow> get_mrs sender buf mi;
restart \<leftarrow> handle_fault_reply f receiver (mi_label mi) mrs;
thread_set (\<lambda>tcb. tcb \<lparr> tcb_fault := None \<rparr>) receiver;
set_thread_state receiver (if restart then Restart else Inactive);
when restart $ do_extended_op (possible_switch_to receiver);
return ()
od
od"
text \<open>This function transfers a reply message to a thread when that message
is generated by a kernel service.\<close>
definition
reply_from_kernel :: "obj_ref \<Rightarrow> (data \<times> data list) \<Rightarrow> (unit,'z::state_ext) s_monad"
where
"reply_from_kernel thread x \<equiv> do
(label, msg) \<leftarrow> return x;
buf \<leftarrow> lookup_ipc_buffer True thread;
as_user thread $ setRegister badge_register 0;
len \<leftarrow> set_mrs thread buf msg;
set_message_info thread $ MI len 0 0 label
od"
text \<open>Install a one-use Reply capability.\<close>
definition
setup_caller_cap :: "obj_ref \<Rightarrow> obj_ref \<Rightarrow> bool \<Rightarrow> (unit,'z::state_ext) s_monad"
where
"setup_caller_cap sender receiver grant \<equiv> do
set_thread_state sender BlockedOnReply;
cap_insert (ReplyCap sender False (if grant then {AllowGrant, AllowWrite} else {AllowWrite}))
(sender, tcb_cnode_index 2)
(receiver, tcb_cnode_index 3)
od"
text \<open>Handle a message send operation performed on an endpoint by a thread.
If a receiver is waiting then transfer the message. If no receiver is available
and the thread is willing to block waiting to send then put it in the endpoint
sending queue.\<close>
definition
send_ipc :: "bool \<Rightarrow> bool \<Rightarrow> badge \<Rightarrow> bool \<Rightarrow> bool
\<Rightarrow> obj_ref \<Rightarrow> obj_ref \<Rightarrow> (unit,'z::state_ext) s_monad"
where
"send_ipc block call badge can_grant can_grant_reply thread epptr \<equiv> do
ep \<leftarrow> get_endpoint epptr;
case (ep, block) of
(IdleEP, True) \<Rightarrow> do
set_thread_state thread (BlockedOnSend epptr
\<lparr> sender_badge = badge,
sender_can_grant = can_grant,
sender_can_grant_reply = can_grant_reply,
sender_is_call = call \<rparr>);
set_endpoint epptr $ SendEP [thread]
od
| (SendEP queue, True) \<Rightarrow> do
set_thread_state thread (BlockedOnSend epptr
\<lparr> sender_badge = badge,
sender_can_grant = can_grant,
sender_can_grant_reply = can_grant_reply,
sender_is_call = call\<rparr>);
set_endpoint epptr $ SendEP (queue @ [thread])
od
| (IdleEP, False) \<Rightarrow> return ()
| (SendEP queue, False) \<Rightarrow> return ()
| (RecvEP (dest # queue), _) \<Rightarrow> do
set_endpoint epptr $ (case queue of [] \<Rightarrow> IdleEP
| _ \<Rightarrow> RecvEP queue);
recv_state \<leftarrow> get_thread_state dest;
reply_can_grant \<leftarrow> case recv_state
of (BlockedOnReceive x data) \<Rightarrow> do
do_ipc_transfer thread (Some epptr) badge can_grant dest;
return (receiver_can_grant data)
od
| _ \<Rightarrow> fail;
set_thread_state dest Running;
do_extended_op (possible_switch_to dest);
when call $
if (can_grant \<or> can_grant_reply)
then setup_caller_cap thread dest reply_can_grant
else set_thread_state thread Inactive
od
| (RecvEP [], _) \<Rightarrow> fail
od"
text \<open>Handle a message receive operation performed on an endpoint by a thread.
If a sender is waiting then transfer the message, otherwise put the thread in
the endpoint receiving queue.\<close>
definition
isActive :: "notification \<Rightarrow> bool"
where
"isActive ntfn \<equiv> case ntfn_obj ntfn
of ActiveNtfn _ \<Rightarrow> True
| _ \<Rightarrow> False"
text\<open>Helper function for performing \emph{signal} when receiving on a normal
endpoint\<close>
definition
complete_signal :: "obj_ref \<Rightarrow> obj_ref \<Rightarrow> (unit,'z::state_ext) s_monad"
where
"complete_signal ntfnptr tcb \<equiv> do
ntfn \<leftarrow> get_notification ntfnptr;
case ntfn_obj ntfn of
ActiveNtfn badge \<Rightarrow> do
as_user tcb $ setRegister badge_register badge;
set_notification ntfnptr $ ntfn_set_obj ntfn IdleNtfn
od
| _ \<Rightarrow> fail
od"
definition
do_nbrecv_failed_transfer :: "obj_ref \<Rightarrow> (unit,'z::state_ext) s_monad"
where
"do_nbrecv_failed_transfer thread = do as_user thread $ setRegister badge_register 0; return () od"
definition
receive_ipc :: "obj_ref \<Rightarrow> cap \<Rightarrow> bool \<Rightarrow> (unit,'z::state_ext) s_monad"
where
"receive_ipc thread cap is_blocking \<equiv> do
(epptr,rights) \<leftarrow> (case cap
of EndpointCap ref badge rights \<Rightarrow> return (ref,rights)
| _ \<Rightarrow> fail);
ep \<leftarrow> get_endpoint epptr;
ntfnptr \<leftarrow> get_bound_notification thread;
ntfn \<leftarrow> case_option (return default_notification) get_notification ntfnptr;
if (ntfnptr \<noteq> None \<and> isActive ntfn)
then
complete_signal (the ntfnptr) thread
else
case ep
of IdleEP \<Rightarrow> (case is_blocking of
True \<Rightarrow> do
set_thread_state thread (BlockedOnReceive epptr
\<lparr>receiver_can_grant = (AllowGrant \<in> rights)\<rparr>);
set_endpoint epptr (RecvEP [thread])
od
| False \<Rightarrow> do_nbrecv_failed_transfer thread)
| RecvEP queue \<Rightarrow> (case is_blocking of
True \<Rightarrow> do
set_thread_state thread (BlockedOnReceive epptr
\<lparr>receiver_can_grant = (AllowGrant \<in> rights)\<rparr>);
set_endpoint epptr (RecvEP (queue @ [thread]))
od
| False \<Rightarrow> do_nbrecv_failed_transfer thread)
| SendEP q \<Rightarrow> do
assert (q \<noteq> []);
queue \<leftarrow> return $ tl q;
sender \<leftarrow> return $ hd q;
set_endpoint epptr $
(case queue of [] \<Rightarrow> IdleEP | _ \<Rightarrow> SendEP queue);
sender_state \<leftarrow> get_thread_state sender;
data \<leftarrow> (case sender_state
of BlockedOnSend ref data \<Rightarrow> return data
| _ \<Rightarrow> fail);
do_ipc_transfer sender (Some epptr)
(sender_badge data) (sender_can_grant data)
thread;
if (sender_is_call data)
then
if (sender_can_grant data \<or> sender_can_grant_reply data)
then setup_caller_cap sender thread (AllowGrant \<in> rights)
else set_thread_state sender Inactive
else do
set_thread_state sender Running;
do_extended_op (possible_switch_to sender)
od
od
od"
section \<open>Asynchronous Message Transfers\<close>
text \<open>Helper function to handle a signal operation in the case
where a receiver is waiting.\<close>
definition
update_waiting_ntfn :: "obj_ref \<Rightarrow> obj_ref list \<Rightarrow> obj_ref option \<Rightarrow> badge \<Rightarrow>
(unit,'z::state_ext) s_monad"
where
"update_waiting_ntfn ntfnptr queue bound_tcb badge \<equiv> do
assert (queue \<noteq> []);
(dest,rest) \<leftarrow> return $ (hd queue, tl queue);
set_notification ntfnptr $ \<lparr>
ntfn_obj = (case rest of [] \<Rightarrow> IdleNtfn | _ \<Rightarrow> WaitingNtfn rest),
ntfn_bound_tcb = bound_tcb \<rparr>;
set_thread_state dest Running;
as_user dest $ setRegister badge_register badge;
do_extended_op (possible_switch_to dest)
od"
text \<open>Handle a message send operation performed on a notification object.
If a receiver is waiting then transfer the message, otherwise combine the new
message with whatever message is currently waiting.\<close>
(* helper function for checking if thread is blocked *)
definition
receive_blocked :: "thread_state \<Rightarrow> bool"
where
"receive_blocked st \<equiv> case st of
BlockedOnReceive _ _ \<Rightarrow> True
| _ \<Rightarrow> False"
definition
send_signal :: "obj_ref \<Rightarrow> badge \<Rightarrow> (unit,'z::state_ext) s_monad"
where
"send_signal ntfnptr badge \<equiv> do
ntfn \<leftarrow> get_notification ntfnptr;
case (ntfn_obj ntfn, ntfn_bound_tcb ntfn) of
(IdleNtfn, Some tcb) \<Rightarrow> do
st \<leftarrow> get_thread_state tcb;
if (receive_blocked st)
then do
cancel_ipc tcb;
set_thread_state tcb Running;
as_user tcb $ setRegister badge_register badge;
do_extended_op (possible_switch_to tcb)
od
else set_notification ntfnptr $ ntfn_set_obj ntfn (ActiveNtfn badge)
od
| (IdleNtfn, None) \<Rightarrow> set_notification ntfnptr $ ntfn_set_obj ntfn (ActiveNtfn badge)
| (WaitingNtfn queue, bound_tcb) \<Rightarrow> update_waiting_ntfn ntfnptr queue bound_tcb badge
| (ActiveNtfn badge', _) \<Rightarrow>
set_notification ntfnptr $ ntfn_set_obj ntfn $
ActiveNtfn (combine_ntfn_badges badge badge')
od"
text \<open>Handle a receive operation performed on a notification object by a
thread. If a message is waiting then perform the transfer, otherwise put the
thread in the endpoint's receiving queue.\<close>
definition
receive_signal :: "obj_ref \<Rightarrow> cap \<Rightarrow> bool \<Rightarrow> (unit,'z::state_ext) s_monad"
where
"receive_signal thread cap is_blocking \<equiv> do
ntfnptr \<leftarrow>
case cap
of NotificationCap ntfnptr badge rights \<Rightarrow> return ntfnptr
| _ \<Rightarrow> fail;
ntfn \<leftarrow> get_notification ntfnptr;
case ntfn_obj ntfn
of IdleNtfn \<Rightarrow>
(case is_blocking of
True \<Rightarrow> do
set_thread_state thread (BlockedOnNotification ntfnptr);
set_notification ntfnptr $ ntfn_set_obj ntfn $ WaitingNtfn [thread]
od
| False \<Rightarrow> do_nbrecv_failed_transfer thread)
| WaitingNtfn queue \<Rightarrow>
(case is_blocking of
True \<Rightarrow> do
set_thread_state thread (BlockedOnNotification ntfnptr);
set_notification ntfnptr $ ntfn_set_obj ntfn $ WaitingNtfn (queue @ [thread])
od
| False \<Rightarrow> do_nbrecv_failed_transfer thread)
| ActiveNtfn badge \<Rightarrow> do
as_user thread $ setRegister badge_register badge;
set_notification ntfnptr $ ntfn_set_obj ntfn IdleNtfn
od
od"
section \<open>Sending Fault Messages\<close>
text \<open>When a thread encounters a fault, retreive its fault handler capability
and send a fault message.\<close>
definition
send_fault_ipc :: "obj_ref \<Rightarrow> fault \<Rightarrow> (unit,'z::state_ext) f_monad"
where
"send_fault_ipc tptr fault \<equiv> doE
handler_cptr \<leftarrow> liftE $ thread_get tcb_fault_handler tptr;
handler_cap \<leftarrow> cap_fault_on_failure (of_bl handler_cptr) False $
lookup_cap tptr handler_cptr;
let f = CapFault (of_bl handler_cptr) False (MissingCapability 0)
in
(case handler_cap
of EndpointCap ref badge rights \<Rightarrow>
if AllowSend \<in> rights \<and> (AllowGrant \<in> rights \<or> AllowGrantReply \<in> rights)
then liftE $ (do
thread_set (\<lambda>tcb. tcb \<lparr> tcb_fault := Some fault \<rparr>) tptr;
send_ipc True True (cap_ep_badge handler_cap)
(AllowGrant \<in> rights) True tptr (cap_ep_ptr handler_cap)
od)
else throwError f
| _ \<Rightarrow> throwError f)
odE"
text \<open>If a fault message cannot be sent then leave the thread inactive.\<close>
definition
handle_double_fault :: "obj_ref \<Rightarrow> fault \<Rightarrow> fault \<Rightarrow> (unit,'z::state_ext) s_monad"
where
"handle_double_fault tptr ex1 ex2 \<equiv> set_thread_state tptr Inactive"
text \<open>Handle a thread fault by sending a fault message if possible.\<close>
definition
handle_fault :: "obj_ref \<Rightarrow> fault \<Rightarrow> (unit,'z::state_ext) s_monad"
where
"handle_fault thread ex \<equiv> do
_ \<leftarrow> gets_the $ get_tcb thread;
send_fault_ipc thread ex
<catch> handle_double_fault thread ex;
return ()
od"
end