-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipc.c
2130 lines (1892 loc) · 64.1 KB
/
ipc.c
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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* the L4.X2 IPC state machine, the Ipc system call, untyped transfers, and
* kernel-to-user IPC operation.
*/
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <ccan/likely/likely.h>
#include <ccan/htable/htable.h>
#include <l4/types.h>
#include <l4/message.h>
#include <l4/vregs.h>
#include <l4/schedule.h>
#include <l4/ipc.h>
#include <ukernel/config.h>
#include <ukernel/misc.h>
#include <ukernel/trace.h>
#include <ukernel/slab.h>
#include <ukernel/thread.h>
#include <ukernel/sched.h>
#include <ukernel/space.h>
#include <ukernel/bug.h>
#include <ukernel/kip.h>
#include <ukernel/interrupt.h>
#include <ukernel/ipc.h>
#define TRACE(fmt, ...) TRACE_MSG(TRID_IPC, fmt, ##__VA_ARGS__)
#define TRACE_REDIR(fmt, ...) TRACE_MSG(TRID_IPC_REDIR, fmt, ##__VA_ARGS__)
static size_t hash_ipc_wait(const void *tid, void *priv);
static size_t hash_pasv_from(const void *tid, void *priv);
static size_t hash_waited_redir(const void *thread, void *priv);
/* sendwait_hash is a multiset keyed by thread->u2.ipc_wait.dest_tid, i.e. the
* recipient's gTID. active receive scans these to find a passive sender.
*
* recvwait_hash is much the same, but by a passive receiver's ipc_from, which
* is a global TID. cancel_ipc_to() scans this to generate "invalid thread ID"
* errors on deleting ThreadControl.
*
* redir_wait is similar for redirection waits.
*/
static struct htable sendwait_hash = HTABLE_INITIALIZER(
sendwait_hash, &hash_ipc_wait, NULL),
recvwait_hash = HTABLE_INITIALIZER(recvwait_hash, &hash_pasv_from, NULL),
redir_wait = HTABLE_INITIALIZER(redir_wait, &hash_waited_redir, NULL);
static size_t hash_ipc_wait(const void *threadptr, void *priv) {
const struct thread *t = threadptr;
return int_hash(t->u2.ipc_wait.dest_tid.raw);
}
static size_t hash_pasv_from(const void *threadptr, void *priv) {
const struct thread *t = threadptr;
assert(thread_is_valid(t));
assert(t->status == TS_RECV_WAIT);
assert(L4_IsGlobalId(t->ipc_from)); /* implies !IsNilThread */
assert(t->ipc_from.raw != L4_anythread.raw);
return int_hash(t->ipc_from.raw);
}
static size_t hash_waited_redir(const void *threadptr, void *priv) {
const struct thread *t = threadptr;
assert(!L4_IsNilThread(t->u1.waited_redir));
assert(t->space != kernel_space);
return int_hash(t->u1.waited_redir.raw);
}
/* TODO: move into <ukernel/util.h> or some such */
static inline bool is_wildcard(L4_ThreadId_t tid) {
return tid.raw == L4_anythread.raw
|| tid.raw == L4_anylocalthread.raw;
}
/* whether @t should be in recvwait_hash or not, by its ->status and
* ->ipc_from fields. this is repeated all over the place.
*/
static bool in_recv_wait(struct thread *t) {
return t->status == TS_RECV_WAIT
&& !L4_IsNilThread(t->ipc_from)
&& !is_wildcard(t->ipc_from);
}
static bool verify_recv_wait(struct thread *t)
{
#ifndef NDEBUG
size_t hash = int_hash(t->ipc_from.raw);
struct htable_iter it;
for(struct thread *cand = htable_firstval(&recvwait_hash, &it, hash);
cand != NULL;
cand = htable_nextval(&recvwait_hash, &it, hash))
{
if(cand == t) return true;
}
return false;
#else
return true;
#endif
}
/* module invariants. these mostly concern thread states within hash
* tables.
*/
#ifndef DEBUG_ME_HARDER
#define check_ipc_module() true
#else
#include <ukernel/invariant.h>
static unsigned ptr_count(struct htable *ht, void *ptr)
{
unsigned count = 0;
struct htable_iter it;
for(void *cand = htable_first(ht, &it);
cand != NULL; cand = htable_next(ht, &it))
{
if(cand == ptr) count++;
}
return count;
}
static bool check_ipc_module(void)
{
INV_CTX;
struct ra_iter rai;
for(struct thread *t = ra_first(thread_ra, &rai);
t != NULL; t = ra_next(thread_ra, &rai))
{
/* NOTE: this could live in a different function. certainly there's
* utility to this elsewhere also.
*/
static const struct {
unsigned bit;
const char *name;
} flag_names[] = {
{ TF_HALT, "halt" },
{ TF_SENDER, "sender" },
{ TF_INTR, "intr" },
{ TF_PRE_RECV, "pre_recv" },
{ TF_SYSCALL, "syscall" },
{ TF_REDIR, "redir" },
{ TF_REDIR_WAIT, "redir_wait" },
{ TF_PREEMPT, "preempt" },
{ TF_FROM_TURN, "from_turn" },
};
char flagstr[100] = "";
size_t flagstrlen = 0;
for(int i=0; i < ARRAY_SIZE(flag_names); i++) {
if(~t->flags & flag_names[i].bit) continue;
if(flagstrlen == 0) {
assert(sizeof flagstr > strlen(flag_names[i].name));
strcpy(flagstr, flag_names[i].name);
flagstrlen = strlen(flag_names[i].name);
} else {
flagstrlen += snprintf(
&flagstr[flagstrlen], sizeof flagstr - flagstrlen,
", %s", flag_names[i].name);
}
}
flagstr[sizeof flagstr - 1] = '\0';
inv_push("t=%lu:%lu (%p), ->ipc_from=%lu:%lu, ->ipc_to=%lu:%lu",
TID_THREADNUM(t->id), TID_VERSION(t->id), t,
L4_ThreadNo(t->ipc_from), L4_Version(t->ipc_from),
L4_ThreadNo(t->ipc_to), L4_Version(t->ipc_to));
inv_log(" ->flags={%s}, ->status=%s", flagstr, sched_status_str(t));
/* TF_FROM_TURN should only ever be set in receive-phase states. */
inv_imply1(t->flags & TF_FROM_TURN,
t->status == TS_RECV_WAIT || t->status == TS_R_RECV);
/* string transfers that're waiting for either the other thread's
* pagefaults, or for the transfer to proceed via scheduling.
*/
inv_push("[TS_XFER checks] t->ipc=%p", t->ipc);
inv_imply1(t->status == TS_XFER, t->ipc != NULL);
if(t->status == TS_XFER) {
inv_log("ipc->from->id=%lu:%lu, ->to->id=%lu:%lu",
TID_THREADNUM(t->ipc->from->id), TID_VERSION(t->ipc->from->id),
TID_THREADNUM(t->ipc->to->id), TID_VERSION(t->ipc->to->id));
}
inv_imply1(t->status == TS_XFER,
t->ipc->from == t || t->ipc->to == t);
inv_imply1(t->status == TS_XFER && t->ipc->from == t,
t->ipc->to != t);
inv_imply1(t->status == TS_XFER && t->ipc->to == t,
t->ipc->from != t);
inv_pop();
/* check number of occurrences in sendwait_hash, recvwait_hash, and
* redir_wait.
*/
unsigned sendwait_count = ptr_count(&sendwait_hash, t),
recvwait_count = ptr_count(&recvwait_hash, t),
redir_count = ptr_count(&redir_wait, t);
inv_log("in_recv_wait(t)=%s", btos(in_recv_wait(t)));
inv_log("sendwait_count=%u, recvwait_count=%u, redir_count=%u",
sendwait_count, recvwait_count, redir_count);
inv_ok1(sendwait_count <= 1);
inv_iff1(t->status == TS_SEND_WAIT && (~t->flags & TF_HALT)
&& (~t->flags & TF_REDIR_WAIT), sendwait_count == 1);
inv_ok1(recvwait_count <= 1);
inv_iff1(in_recv_wait(t), recvwait_count == 1);
inv_ok1(redir_count <= 1);
inv_iff1(t->status == TS_SEND_WAIT && (t->flags & TF_REDIR_WAIT)
&& !L4_IsNilThread(t->u1.waited_redir),
redir_count == 1);
inv_imply1(sendwait_count == 1, recvwait_count + redir_count == 0);
inv_imply1(recvwait_count == 1, sendwait_count + redir_count == 0);
inv_imply1(redir_count == 1, recvwait_count + sendwait_count == 0);
inv_pop();
}
/* recvwait_hash per member. simple enough. */
struct htable_iter it;
for(struct thread *t = htable_first(&recvwait_hash, &it);
t != NULL; t = htable_next(&recvwait_hash, &it))
{
inv_push("recvwait_hash: t=%lu:%lu (%p), ->status=%s, ->ipc_from=%lu:%lu",
TID_THREADNUM(t->id), TID_VERSION(t->id), t, sched_status_str(t),
L4_ThreadNo(t->ipc_from), L4_Version(t->ipc_from));
inv_ok1(thread_is_valid(t));
inv_ok1(t->status == TS_RECV_WAIT); /* no R_RECV plz. */
inv_ok1(!L4_IsNilThread(t->ipc_from));
inv_ok1(L4_IsGlobalId(t->ipc_from));
inv_ok1(resolve_tid_spec(t->space, t->ipc_from) != NULL);
inv_ok1(in_recv_wait(t)); /* duh */
inv_ok1(verify_recv_wait(t)); /* implied */
inv_pop();
}
for(struct thread *t = htable_first(&sendwait_hash, &it);
t != NULL; t = htable_next(&sendwait_hash, &it))
{
inv_push("sendwait_hash: t=%lu:%lu (%p), ->status=%s, ->ipc_from=%lu:%lu",
TID_THREADNUM(t->id), TID_VERSION(t->id), t, sched_status_str(t),
L4_ThreadNo(t->ipc_from), L4_Version(t->ipc_from));
inv_ok1(thread_is_valid(t));
/* TODO: other sendwait things */
inv_pop();
}
for(struct thread *t = htable_first(&redir_wait, &it);
t != NULL; t = htable_next(&redir_wait, &it))
{
inv_push("redir_wait: t=%lu:%lu (%p), ->status=%s, ->ipc_from=%lu:%lu",
TID_THREADNUM(t->id), TID_VERSION(t->id), t, sched_status_str(t),
L4_ThreadNo(t->ipc_from), L4_Version(t->ipc_from));
inv_ok1(thread_is_valid(t));
/* TODO: other redir things */
inv_pop();
}
return true;
inv_fail:
return false;
}
#endif
static inline void set_ipc_error(void *utcb, L4_Word_t ec)
{
L4_VREG(utcb, L4_TCR_ERRORCODE) = ec;
L4_VREG(utcb, L4_TCR_MR(0)) = ((L4_MsgTag_t){ .X.flags = 0x8 }).raw;
L4_VREG(utcb, L4_TCR_MR(1)) = 0;
L4_VREG(utcb, L4_TCR_MR(2)) = 0;
}
inline void set_ipc_return_regs(
struct x86_regs *regs,
struct thread *current,
void *utcb)
{
regs->eax = current->ipc_from.raw;
regs->esi = L4_VREG(utcb, L4_TCR_MR(0));
regs->ebx = L4_VREG(utcb, L4_TCR_MR(1));
regs->ebp = L4_VREG(utcb, L4_TCR_MR(2));
}
static inline void set_ipc_return_thread(struct thread *t, void *utcb)
{
set_ipc_return_regs(&t->ctx.r, t, utcb);
}
/* exported for sched.c and thread.c */
void set_ipc_error_thread(struct thread *t, L4_Word_t ec)
{
void *utcb = thread_get_utcb(t);
set_ipc_error(utcb, ec);
set_ipc_return_thread(t, utcb);
}
/* this is like resolve_tid_spec(), but in reverse. */
static L4_ThreadId_t tid_return(struct thread *self, struct thread *t)
{
if(self->space == t->space) {
return get_local_id(t);
} else {
return (L4_ThreadId_t){ .raw = t->id };
}
}
void remove_send_wait(struct thread *t) {
htable_del(&sendwait_hash, hash_ipc_wait(t, NULL), t);
}
bool insert_send_wait(struct thread *t) {
return htable_add(&sendwait_hash, hash_ipc_wait(t, NULL), t);
}
static void remove_recv_wait(struct thread *t)
{
assert(in_recv_wait(t));
assert(verify_recv_wait(t));
htable_del(&recvwait_hash, hash_pasv_from(t, NULL), t);
}
static bool insert_recv_wait(struct thread *t)
{
assert(L4_IsGlobalId(t->ipc_from)); /* implies !IsNilThread */
assert(!is_wildcard(t->ipc_from));
return htable_add(&recvwait_hash, hash_pasv_from(t, NULL), t);
}
void remove_redir_wait(struct thread *t)
{
if(likely(!L4_IsNilThread(t->u1.waited_redir))) {
htable_del(&redir_wait, hash_waited_redir(t, NULL), t);
}
}
void ipc_xfer_timeout(struct ipc_state *st)
{
assert(st->from->ipc == st);
assert(CHECK_FLAG(st->from->flags, TF_SENDER));
assert(st->to->ipc == st);
TRACE("%s: st->from{%lu:%lu} -> st->to{%lu:%lu}\n", __func__,
TID_THREADNUM(st->from->id), TID_VERSION(st->from->id),
TID_THREADNUM(st->to->id), TID_VERSION(st->to->id));
/* must determine which peer's fault caused the timeout. if they were both
* waiting, we'll blame the sender.
*/
bool f_in_src;
if(st->str_off < 0) {
/* pre-xfer mode. decided by whether the sender has unserviced faults,
* or not. this isn't quite even, but regardless if the sender's pager
* services them quicker than the receiver's, then the error code
* indicates the receiver.
*/
struct fault_peer *f_src = &st->xfer.fault[0];
assert(f_src->num > 0 || st->xfer.fault[1].num > 0);
f_in_src = f_src->num > 0;
} else {
/* in-transfer mode. decided by whether the sender is waiting for the
* receiver's fault handling.
*/
f_in_src = (st->from->status != TS_XFER);
}
const L4_Word_t ec_offs = st->tot_offset << 4;
set_ipc_error_thread(st->from, ec_offs | (f_in_src ? 5 : 6) << 1);
set_ipc_error_thread(st->to, ec_offs | (!f_in_src ? 5 : 6) << 1 | 1);
struct thread *from = st->from, *to = st->to;
/* (note the use of bitwise rather than short-circuit or. this is
* intended.)
*/
bool dead_ipc = post_exn_fail(from) | post_exn_fail(to);
if(!dead_ipc) {
/* can happen when both were waiting for transfer start due to
* scheduling. otherwise, free() is called from prexfer_ipc_hook().
*/
st->from->ipc = NULL;
st->to->ipc = NULL;
free(st);
}
from->flags &= ~TF_SENDER;
thread_ipc_fail(from);
thread_ipc_fail(to);
}
/* this function is called from the scheduler when it finds that both sides of
* a paused string transfer are ready to proceed. it should write both sides'
* IPC return registers.
*/
bool ipc_resume(struct thread *t)
{
assert(check_ipc_module());
assert(t->status == TS_XFER);
assert(t->ipc != NULL);
struct ipc_state *st = t->ipc;
struct thread *dest = st->to, *source = st->from;
TRACE("%s: called on %lu:%lu -> %lu:%lu\n",
__func__, TID_THREADNUM(source->id), TID_VERSION(source->id),
TID_THREADNUM(dest->id), TID_VERSION(dest->id));
void *s_utcb = thread_get_utcb(st->from),
*d_utcb = thread_get_utcb(st->to);
assert(source->ipc == st);
assert(dest->ipc == st);
/* resume mode. */
L4_MsgTag_t tag = { .raw = L4_VREG(s_utcb, L4_TCR_MR(0)) };
int n = do_typed_transfer(source, s_utcb, dest, d_utcb, tag);
if(n < 0) {
assert(n == -EFAULT);
assert(check_ipc_module());
return false;
}
assert(dest->ipc == NULL);
assert(source->ipc == NULL);
assert(!CHECK_FLAG(source->flags | dest->flags, TF_SENDER));
if(n == 0) {
/* success.
*
* TODO: merge this with the result stuff in ipc_send_half() and
* ipc_recv_half() to reduce duplication.
*
* TODO: tests about propagation, redirection, etc. should be changed
* to include a transfer fault so that they cover also this path.
*/
struct thread *sender = source;
if(L4_IpcPropagated(tag)) {
L4_VREG(d_utcb, L4_TCR_VA_SENDER) = tid_return(dest, source).raw;
struct thread *vs = get_tcr_thread(source, s_utcb, L4_TCR_VA_SENDER);
if(vs != NULL) sender = vs;
/* turn VirtualSender's closed-waiting FromSpec when applicable */
if((vs->status == TS_R_RECV || vs->status == TS_RECV_WAIT)
&& (vs->ipc_from.raw == source->id
|| (vs->space == source->space
&& vs->ipc_from.raw == get_local_id(source).raw)))
{
bool twiddle = in_recv_wait(vs);
if(twiddle) remove_recv_wait(vs);
vs->flags |= TF_FROM_TURN;
vs->ipc_from.raw = dest->id;
if(vs->u0.partner == source) vs->u0.partner = dest;
if(twiddle) insert_recv_wait(vs);
}
}
if(L4_IpcRedirected(tag)) {
/* FIXME!!!!! this should set dest's IntendedReceiver to the
* destination thread ID saved in ipc_send_half().
*/
}
if(dest->flags & TF_FROM_TURN) {
dest->flags &= ~TF_FROM_TURN;
L4_VREG(d_utcb, L4_TCR_VA_SENDER) = tid_return(dest, source).raw;
}
dest->ipc_from = tid_return(dest, sender);
} else {
/* overflows and xfer timeouts. */
assert(n > 0);
assert(((n >> 1) & 0xf) > 3);
/* TODO: translate xfer timeout by partner/caller! */
set_ipc_error(s_utcb, n & ~1u);
set_ipc_error(d_utcb, n | 1);
source->ipc_from = L4_nilthread;
}
if(L4_IsNilThread(source->ipc_from)) {
TRACE("%s: source returns to userspace\n", __func__);
source->status = TS_READY;
source->wakeup_time = 0;
set_ipc_return_thread(source, s_utcb);
} else {
source->status = TS_R_RECV;
source->wakeup_time = wakeup_at(L4_Never); /* FIXME: IPC timeout */
TRACE("%s: source receives from %lu:%lu\n", __func__,
L4_ThreadNo(source->ipc_from), L4_Version(source->ipc_from));
}
/* dest always wakes up. */
dest->status = TS_READY;
dest->wakeup_time = 0;
set_ipc_return_thread(dest, d_utcb);
sq_update_thread(source);
sq_update_thread(dest);
assert(check_ipc_module());
return true;
}
struct thread *ipc_partner(struct thread *t)
{
assert(t->status == TS_XFER || IS_IPC_WAIT(t->status));
assert(t->ipc != NULL);
assert(!CHECK_FLAG(t->flags, TF_SENDER) || t == t->ipc->from);
assert(CHECK_FLAG(t->flags, TF_SENDER) || t == t->ipc->to);
assert(check_ipc_module());
struct thread *partner = CHECK_FLAG(t->flags, TF_SENDER) ? t->ipc->to : t->ipc->from;
assert(IS_IPC(partner->status));
assert(partner->ipc == t->ipc);
return partner;
}
/* returns 0 on success, ErrorCode on error signal, or -EFAULT on ongoing
* typed transfer.
*/
static int do_ipc_transfer(
struct thread *source, void *s_utcb,
struct thread *dest, void *d_utcb)
{
L4_MsgTag_t tag = { .raw = L4_VREG(s_utcb, L4_TCR_MR(0)) };
if(hook_empty(&dest->post_exn_call)) {
L4_VREG(d_utcb, L4_TCR_MR(0)) = tag.raw;
for(int i=0; i < tag.X.u; i++) {
int reg = L4_TCR_MR(i + 1);
L4_VREG(d_utcb, reg) = L4_VREG(s_utcb, reg);
}
}
if(tag.X.t == 0) return 0;
else {
return do_typed_transfer(source, s_utcb, dest, d_utcb, tag);
}
}
/* used by the deleting and overwriting modes of ThreadControl.
*
* TODO: this should signal preemption when it occurs: one of the aborted
* would-be peers may have priority.
*/
void cancel_ipc_to(L4_ThreadId_t with_tid, L4_Word_t errcode)
{
assert(L4_IsGlobalId(with_tid));
/* invariant check only at bottom; this function fixes things up. */
/* fail passive senders to @with_tid. */
struct htable_iter it;
size_t hash = int_hash(with_tid.raw);
errcode &= ~(L4_Word_t)1; /* send-phase errors. */
for(struct thread *peer = htable_firstval(&sendwait_hash, &it, hash);
peer != NULL;
peer = htable_nextval(&sendwait_hash, &it, hash))
{
if(peer->u2.ipc_wait.dest_tid.raw != with_tid.raw) continue;
assert(!L4_IsGlobalId(peer->ipc_to)
|| L4_ThreadNo(peer->ipc_to) == L4_ThreadNo(with_tid));
assert(!L4_IsLocalId(peer->ipc_to)
|| resolve_tid_spec(peer->space, peer->ipc_to) == NULL
|| TID_THREADNUM(resolve_tid_spec(peer->space, peer->ipc_to)->id)
== L4_ThreadNo(with_tid));
assert(peer->status == TS_SEND_WAIT || peer->status == TS_XFER
|| peer->status == TS_STOPPED);
TRACE("%s: cancelling sendwait peer=%lu:%lu\n", __func__,
TID_THREADNUM(peer->id), TID_VERSION(peer->id));
if(!post_exn_fail(peer)) {
set_ipc_error_thread(peer, errcode);
thread_wake(peer);
}
htable_delval(&sendwait_hash, &it);
}
/* fail passive receivers from @with_tid. */
errcode |= 1; /* receive-phase errors. */
for(struct thread *peer = htable_firstval(&recvwait_hash, &it, hash);
peer != NULL;
peer = htable_nextval(&recvwait_hash, &it, hash))
{
if(peer->ipc_from.raw != with_tid.raw) continue;
TRACE("%s: cancelling recvwait peer=%lu:%lu\n", __func__,
TID_THREADNUM(peer->id), TID_VERSION(peer->id));
if(!post_exn_fail(peer)) {
set_ipc_error_thread(peer, errcode);
thread_wake(peer);
}
htable_delval(&recvwait_hash, &it);
}
assert(check_ipc_module());
}
/* TODO: this function scales poorly: it does a brute-force loop over all the
* passive sends in the system, not just those for the IPC peer.
*/
static void rewrite_passive_vs_from(struct thread *t)
{
L4_ThreadId_t ltid = L4_nilthread;
/* inactive threads may be propagated on behalf of. */
if(likely(t->utcb_pos >= 0)) ltid = get_local_id(t);
struct htable_iter it;
for(struct thread *from = htable_first(&sendwait_hash, &it);
from != NULL;
from = htable_next(&sendwait_hash, &it))
{
if(from->u2.ipc_wait.send_tid.raw != t->id
&& from->u2.ipc_wait.send_tid.raw != ltid.raw)
{
continue;
}
struct thread *dest = thread_get_fast(from->u2.ipc_wait.dest_tid);
from->u2.ipc_wait.send_tid = tid_return(dest, from);
L4_MsgTag_t *tag = (void *)&L4_VREG(
thread_get_utcb(from), L4_TCR_MR(0));
tag->X.flags &= ~0x1;
}
}
/* called from thread_ipc_fail() and from the deleting/modifying
* ThreadControl. takes care of the {send,recv}wait_hash entries and disables
* passive propagated sends' propagation. leaves errorcode setting to caller's
* caller.
*/
void cancel_ipc_from(struct thread *t)
{
assert(verify_recv_wait(t) || !in_recv_wait(t));
if(t->status == TS_SEND_WAIT) {
remove_send_wait(t);
sq_remove_thread(t);
t->status = TS_READY;
sq_insert_thread(t);
} else if(t->status == TS_RECV_WAIT || t->status == TS_R_RECV) {
struct thread *s = resolve_tid_spec(t->space, t->ipc_from);
if(s != NULL && s->id == t->ipc_from.raw && s->u0.partner == t) {
/* TODO: coördinate partnership breaking using a function of some
* kind, such as one that undoes scheduling effects.
*/
s->u0.partner = NULL;
}
if(in_recv_wait(t)) {
remove_recv_wait(t);
t->status = 0x42; /* a dummy */
t->flags &= ~TF_FROM_TURN;
assert(!in_recv_wait(t));
}
}
rewrite_passive_vs_from(t);
assert(check_ipc_module());
}
static inline bool is_interrupt(L4_ThreadId_t tid) {
return L4_Version(tid) == 1
&& L4_ThreadNo(tid) <= last_int_threadno();
}
static bool active_send_match(struct thread *sender, struct thread *dest)
{
return dest->ipc_from.raw == L4_anythread.raw
|| dest->ipc_from.raw == sender->id
|| (dest->space == sender->space
&& (dest->ipc_from.raw == L4_anylocalthread.raw
|| dest->ipc_from.raw == get_local_id(sender).raw));
}
/* whether "tip" ends up redirecting for "base". */
static bool has_redir_chain(struct thread *base, struct thread *tip)
{
assert(tip != NULL);
do {
if(!CHECK_FLAG(base->space->flags, SF_REDIRECT)) return false;
struct thread *r = base->space->redirector;
if(r == tip) return true;
base = r;
} while(base != NULL);
return false;
}
/* whether @t's redirector chain is ready. this covers the whole chain.
* returns the non-ready one in *holdup_p, or nilthread if there was an
* invalid redirector along the chain.
*/
static bool is_redir_ready(L4_ThreadId_t *holdup_p, struct thread *t)
{
assert(holdup_p != NULL);
struct thread *prev;
do {
if(!CHECK_FLAG(t->space->flags, SF_REDIRECT)) return true;
if(unlikely(t->space->redirector == NULL)) {
/* invalid redirector. happens when the redirecting thread's ID
* has been deleted or its version bits overwritten, and a new
* redirector hasn't been set.
*/
*holdup_p = L4_nilthread;
return false;
}
prev = t;
t = t->space->redirector;
} while((t->status == TS_R_RECV || t->status == TS_RECV_WAIT)
&& active_send_match(prev, t));
holdup_p->raw = t->id;
return false;
}
/* returns true iff @t's IPC to @dst will be redirected. */
static inline bool will_redirect(struct thread *t, struct thread *dst)
{
return CHECK_FLAG(t->space->flags, SF_REDIRECT)
&& t->space != dst->space
&& (unlikely(t->space->redirector == NULL)
|| t->space->redirector->space != dst->space);
}
/* returns true for instant success, and false for error condition, or IPC in
* progress (sleep, string transfer fault). afterward *@dest_p will point to
* the actual destination, i.e. a redirector if that applied.
*
* precond: @self->status != TS_STOPPED && !CHECK_FLAG(@self->flags, TF_HALT)
* postcond: !@retval -> @self->status \in {SEND_WAIT, XFER, READY, STOPPED}
*/
static bool ipc_send_half(
struct thread *self, void *self_utcb,
struct thread **dest_p)
{
/* must look this alive to attempt active send */
assert(!CHECK_FLAG(self->flags, TF_HALT));
assert(self->status != TS_STOPPED);
/* NOTE: this assert can blow under some curious timing circumstances.
* those are provoked by DEBUG_ME_HARDER, i.e. the super nasty invariant
* checks in mapdb.c .
*/
assert(!L4_IsNilThread(self->ipc_to));
assert(!is_wildcard(self->ipc_to));
int err_code = 0;
L4_MsgTag_t *tag = (void *)&L4_VREG(self_utcb, L4_TCR_MR(0));
tag->X.flags &= 0x1; /* keep the propagate flag */
assert(dest_p != NULL);
if(*dest_p == NULL) {
assert(CHECK_FLAG(self->flags, TF_INTR));
assert(is_interrupt(self->ipc_to));
assert(!CHECK_FLAG(self->space->flags, SF_REDIRECT));
/* eat an interrupt reply. */
err_code = int_clear(L4_ThreadNo(self->ipc_to), self);
if(err_code == 0) return true; else goto error;
}
struct thread *dest = *dest_p;
/* get matching variables, check propagation */
L4_ThreadId_t self_id = { .raw = self->id }, self_lid = get_local_id(self),
np_self_id = self_id, np_self_lid = self_lid;
bool propagated = false;
struct thread *vs = NULL, *sender = self;
if(L4_IpcPropagated(*tag)) {
/* propagation (sender fakery). */
vs = get_tcr_thread(self, self_utcb, L4_TCR_VA_SENDER);
/* FIXME: also check interrupt propagation */
if(vs != NULL && (self->space == vs->space
|| self->space == dest->space
|| (vs->ipc_from.raw == self_id.raw
&& (vs->status == TS_R_RECV || vs->status == TS_RECV_WAIT))
|| has_redir_chain(vs, self)))
{
sender = vs;
self_id.raw = vs->id;
self_lid = get_local_id(vs);
propagated = true;
} else {
tag->X.flags &= ~0x1; /* no propagation for you. */
}
assert(propagated == L4_IpcPropagated(*tag));
}
assert(!propagated || vs != NULL);
/* NOTE: due to the way propagation can alter self_lid, this condition
* can't be replaced with active_send_match().
*/
const bool match_cond = dest->ipc_from.raw == L4_anythread.raw
|| dest->ipc_from.raw == self_id.raw
|| (dest->space == sender->space
&& (dest->ipc_from.raw == L4_anylocalthread.raw
|| dest->ipc_from.raw == self_lid.raw));
uint64_t now_us = ksystemclock();
/* override TS_R_RECV? */
int status = dest->status;
bool status_cond;
if(match_cond && status == TS_R_RECV
&& !CHECK_FLAG(dest->flags, TF_HALT))
{
/* FIXME: this peer-timeouting thing shouldn't be here. such timeouts
* should be handled by the scheduler; this code should just fail to
* send to a thread under its receive timeout.
*
* and that test could bear to be much higher up. it would also call
* passive_send() explicitly.
*/
if(now_us >= dest->wakeup_time) {
/* nah, time the peer out instead. */
dest->status = TS_RECV_WAIT; /* required by thread_wake() */
set_ipc_error_thread(dest, (1 << 1) | 1);
thread_wake(dest);
status = dest->status; /* reload after thread_wake() */
TRACE("%s: r_recv override timeout\n", __func__);
status_cond = false;
} else {
/* yep */
TRACE("%s: override r_recv\n", __func__);
status = TS_RECV_WAIT;
status_cond = true;
}
} else {
status_cond = (status == TS_RECV_WAIT);
}
if(status_cond
&& match_cond
&& !CHECK_FLAG(dest->flags, TF_HALT)
&& (dest->wakeup_time == ~(uint64_t)0u
|| dest->wakeup_time > now_us))
{
/* active send */
TRACE("%s: active send to %lu:%lu (from %lu:%lu, actual %lu:%lu)\n", __func__,
TID_THREADNUM(dest->id), TID_VERSION(dest->id),
TID_THREADNUM(self_id.raw), TID_VERSION(self_id.raw),
TID_THREADNUM(self->id), TID_VERSION(self->id));
/* check and apply redirection. */
bool redirected = false;
struct thread *saved_dest = NULL;
if(self->space != dest->space
&& CHECK_FLAG(self->space->flags, SF_REDIRECT))
{
assert(!CHECK_FLAG(self->flags, TF_REDIR_WAIT));
if(!is_redir_ready(&self->u1.waited_redir, self)) {
/* a redirector in the chain was either invalid, or not ready
* to receive. hold this IPC until ready.
*/
TRACE_REDIR("IPC: send-side held by %lu:%lu\n",
L4_ThreadNo(self->u1.waited_redir),
L4_Version(self->u1.waited_redir));
if(self->send_timeout.raw == L4_ZeroTime.raw) {
TRACE_REDIR("IPC: immediate send timeout on redir wait\n");
goto send_timeout;
}
self->flags |= TF_REDIR_WAIT;
self->status = TS_SEND_WAIT;
thread_sleep(self, self->send_timeout);
if(!L4_IsNilThread(self->u1.waited_redir)) {
/* TODO: handle or conceal OOM */
htable_add(&redir_wait,
hash_waited_redir(self, NULL), self);
}
return false;
}
struct thread *red = self->space->redirector;
assert(red != NULL); /* ensured by is_redir_ready() */
if(dest->space != red->space) {
TRACE_REDIR("IPC: redirecting from=%lu:%lu, to=%lu:%lu -> red=%lu:%lu\n",
TID_THREADNUM(self->id), TID_VERSION(self->id),
TID_THREADNUM(dest->id), TID_VERSION(dest->id),
TID_THREADNUM(red->id), TID_VERSION(red->id));
tag->X.flags |= 0x2; /* set redirect bit */
redirected = true;
saved_dest = dest;
*dest_p = dest = red;
/* redirect a closed IPC's receive phase. this'll be
* re-redirected to saved_dest if the redirector passes the
* IPC as-is, or replied to if the redirector returns a
* rejection.
*
* NOTE: this is not explicitly specified by L4.X2, but the
* wording also doesn't forbid it; and this seems like the
* most reasonable approach in any case. (it also fits the
* "redirector chain" pattern; if not, those'd only work for
* chaining pass/don't policies together -- and that's
* bizarre.)
*
* rerererere.
*/
if(self->ipc_from.raw == saved_dest->id) {
self->ipc_from.raw = red->id;
TRACE_REDIR("IPC: closed wait on %lu:%lu changed to %lu:%lu\n",
TID_THREADNUM(saved_dest->id), TID_VERSION(saved_dest->id),
TID_THREADNUM(red->id), TID_VERSION(red->id));
}
}
}
assert(!redirected || saved_dest != NULL);
/* simplify the recv_wait/r_recv difference */
if(in_recv_wait(dest)) {
remove_recv_wait(dest);
dest->status = TS_R_RECV;
assert(!in_recv_wait(dest));
}
void *dest_utcb = thread_get_utcb(dest);
int n = do_ipc_transfer(self, self_utcb, dest, dest_utcb);
if(n > 0) {
TRACE("%s: do_ipc_transfer failed, n=%d\n", __func__, n);
const L4_Word_t error = n;
int code = (error & 0xe) >> 1;
if(code >= 4) {
/* mutual error; signal to partner also. */
if(likely(!post_exn_fail(dest))) {
set_ipc_error_thread(dest, error | 1);
}
thread_wake(dest);
}
set_ipc_error(self_utcb, error & ~1ul);
assert(self->status == TS_RUNNING);
self->status = TS_READY;
return false;
} else if(n < 0) {
assert(n == -EFAULT);
/* (may be in send_wait, to pager; recv_wait and r_recv, from
* pager; and xfer, waiting for partner's pager thing.)
*/
assert(IS_IPC(self->status));
return false;
}
/* wake the receiver up, joining with the overridden status. this
* satisfies thread_wake()'s precondition both in ordinary and
* kernel-originated IPC.
*/
assert(dest->status == TS_RECV_WAIT || dest->status == TS_R_RECV);
dest->status = status;
if(!post_exn_ok(dest, self)) {
/* receiver was in Ipc system call. set return values & wake it up
* from Ipc.
*/
if(propagated) {
assert(L4_IpcPropagated(*tag));
L4_VREG(dest_utcb, L4_TCR_VA_SENDER) = tid_return(dest,
self).raw;
/* turn VirtualSender's closed-waiting FromSpec when applicable */
if((vs->status == TS_R_RECV || vs->status == TS_RECV_WAIT)
&& (vs->ipc_from.raw == np_self_id.raw
|| (vs->ipc_from.raw == np_self_lid.raw
&& vs->space == self->space)))
{
bool twiddle = in_recv_wait(vs);
if(twiddle) remove_recv_wait(vs);