-
Notifications
You must be signed in to change notification settings - Fork 0
/
thread.c
1362 lines (1151 loc) · 35.1 KB
/
thread.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
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#include <errno.h>
#include <ccan/likely/likely.h>
#include <ccan/compiler/compiler.h>
#include <l4/types.h>
#include <l4/thread.h>
#include <l4/vregs.h>
#include <l4/kip.h>
#include <ukernel/x86.h>
#include <ukernel/interrupt.h>
#include <ukernel/mm.h>
#include <ukernel/slab.h>
#include <ukernel/rangealloc.h>
#include <ukernel/ipc.h>
#include <ukernel/misc.h>
#include <ukernel/space.h>
#include <ukernel/gdt.h>
#include <ukernel/kip.h>
#include <ukernel/sched.h>
#include <ukernel/trace.h>
#include <ukernel/bug.h>
#include <ukernel/kip.h>
#include <ukernel/ktest.h>
#include <ukernel/thread.h>
/* for exregs, threadctl, threadswitch prints */
#define TRACE(fmt, ...) TRACE_MSG(TRID_THREAD, fmt, __VA_ARGS__)
/* the exception message, rounded up to cacheline size */
#define NUM_SAVED_REGS 14
/* stored TCRs for an exception originator */
struct saved_regs
{
L4_ThreadId_t va_sender, ir;
L4_Word_t mrs[NUM_SAVED_REGS];
};
/* interrupt routing state. */
struct interrupt
{
struct thread *pager; /* set in ThreadControl */
bool pending, delivered;
};
static struct thread *thread_find(thread_id tid);
static struct thread *int_trigger(int irqn);
/* only accessible with interrupts disabled. */
static short num_ints = 0;
static struct interrupt *int_table = NULL;
static struct kmem_cache *saved_regs_slab = NULL;
struct rangealloc *thread_ra = NULL;
/* burner threads for tno=0 .. num_ints */
static int num_static;
static struct thread **static_threads;
#ifdef DEBUG_ME_HARDER
#include <ukernel/invariant.h>
static bool check_thread(int opt, struct thread *t)
{
INV_CTX;
inv_push("thread %lu:%lu (%p)", TID_THREADNUM(t->id),
TID_VERSION(t->id), t);
inv_ok1(t->space != NULL);
/* TODO: verify presence in utcb_page slot. */
inv_iff1(t->utcb_page != NULL, t->utcb_pos >= 0);
inv_iff1(t->utcb_pos >= 0, t->utcb_ptr_seg > 0);
/* the halt bit */
inv_imply1(CHECK_FLAG(t->flags, TF_HALT) && t->status != TS_STOPPED,
IS_IPC_WAIT(t->status));
/* rangealloc interactions */
inv_ok1(ra_alloc(thread_ra, TID_THREADNUM(t->id)) == NULL);
inv_ok1(ra_id2ptr(thread_ra, TID_THREADNUM(t->id)) == t);
inv_ok1(ra_ptr2id(thread_ra, t) == TID_THREADNUM(t->id));
inv_pop();
return true;
inv_fail:
return false;
}
static bool check_thread_module(int opt)
{
struct ra_iter it;
for(struct thread *t = ra_first(thread_ra, &it);
t != NULL;
t = ra_next(thread_ra, &it))
{
if(t->space == NULL) continue; /* burners etc. */
if(!check_thread(opt, t)) return false;
}
return true;
}
#else
static bool check_thread(int a, struct thread *b) { return true; }
static bool check_thread_module(int a) { return true; }
#endif
COLD void init_threading(void)
{
assert(thread_ra == NULL);
num_ints = last_int_threadno() + 1;
int_table = malloc(num_ints * sizeof(struct interrupt));
for(int i=0; i < num_ints; i++) {
int_table[i] = (struct interrupt){
.pager = NULL, .pending = false,
};
}
saved_regs_slab = KMEM_CACHE_NEW("saved_regs_slab", struct saved_regs);
/* room for the full 256k threads of 32-bit L4.X2. */
assert(thread_ra == NULL);
thread_ra = RA_NEW(struct thread, 1 << 18);
num_static = first_user_threadno() - 1;
if(num_static == 0) {
static_threads = NULL;
} else {
static_threads = malloc(num_static * sizeof(struct thread *));
for(int i=0; i < num_static; i++) {
struct thread *t = ra_zalloc(thread_ra, i);
BUG_ON(t == NULL, "static_threads[%d]", i);
static_threads[i] = t;
}
#ifndef NDEBUG
printf("thread_ra: allocated %d burners.\n", num_static);
#endif
}
assert(check_thread_module(0));
}
static void restore_saved_regs(
struct hook *hook,
void *param, uintptr_t code,
struct saved_regs *sr)
{
struct thread *t = container_of(hook, struct thread, post_exn_call);
assert(t->utcb_pos >= 0);
void *utcb = thread_get_utcb(t);
L4_VREG(utcb, L4_TCR_VA_SENDER) = sr->va_sender.raw;
L4_VREG(utcb, L4_TCR_INTENDEDRECEIVER) = sr->ir.raw;
memcpy(&L4_VREG(utcb, L4_TCR_MR(0)), sr->mrs,
sizeof(L4_Word_t) * NUM_SAVED_REGS);
kmem_cache_free(saved_regs_slab, sr);
hook_detach(hook);
}
/* FIXME: handle OOM from kmem_cache_alloc(), hook_push_back()! */
void save_ipc_regs(struct thread *t, void *utcb, int n_regs)
{
assert(n_regs >= 0 && n_regs <= NUM_SAVED_REGS);
assert(t->utcb_pos >= 0);
struct saved_regs *sr = kmem_cache_alloc(saved_regs_slab);
sr->va_sender.raw = L4_VREG(utcb, L4_TCR_VA_SENDER);
sr->ir.raw = L4_VREG(utcb, L4_TCR_INTENDEDRECEIVER);
memcpy(sr->mrs, &L4_VREG(utcb, L4_TCR_MR(0)),
sizeof(L4_Word_t) * NUM_SAVED_REGS);
hook_push_back(&t->post_exn_call, &restore_saved_regs, sr);
}
bool post_exn_ok(struct thread *t, struct thread *sender)
{
int num = hook_call_front(&t->post_exn_call, sender, 0);
/* kernel IPC chains either keep shitting, or get off the pot. */
assert(num == 0 || t->ipc != NULL || IS_READY(t->status)
|| (CHECK_FLAG(t->flags, TF_HALT) && t->status == TS_STOPPED));
return num > 0;
}
bool post_exn_fail(struct thread *t)
{
int num = hook_call_front(&t->post_exn_call, NULL, 1);
return num > 0;
}
struct thread *thread_new(thread_id tid)
{
assert(thread_ra != NULL); /* must have been initialized */
assert(thread_find(tid) == NULL);
assert(TID_THREADNUM(tid) > last_int_threadno());
struct thread *t = ra_alloc(thread_ra, TID_THREADNUM(tid));
if(unlikely(t == NULL)) return NULL;
assert(ra_ptr2id(thread_ra, t) == TID_THREADNUM(tid));
*t = (struct thread){
.id = tid,
.status = TS_STOPPED,
.utcb_pos = -1,
.pri = 100, .sens_pri = 100,
.max_delay = 0,
.ts_len = L4_TimePeriod(10000), /* 10 ms */
.quantum = 0,
.total_quantum = ~(uint64_t)0, /* useful default */
/* x86 malarkey for non-kernel threads. */
.ctx = {
/* IOPL 0 (noblman swerve) */
.eflags = (0 << 12) | X86_DEFAULT_USER_EFLAGS,
},
};
hook_init(&t->post_exn_call, NULL);
return t;
}
static void thread_destroy(struct thread *t)
{
assert(t->status == TS_STOPPED);
assert(hook_empty(&t->post_exn_call));
if(unlikely(t == get_current_thread())) leaving_thread(t);
struct space *sp = t->space;
if(t->utcb_ptr_seg != 0) {
release_gdt_ptr_seg(L4_Address(sp->utcb_area)
+ t->utcb_pos * UTCB_SIZE + 256 + TCR_UTCB_PTR * 4,
t->utcb_ptr_seg);
t->utcb_ptr_seg = 0;
}
if(t->utcb_pos >= 0) {
assert(t->space != NULL);
space_remove_thread(sp, t);
}
cop_killa(t);
t->space = NULL;
ra_free(thread_ra, t);
}
void thread_set_spip(struct thread *t, L4_Word_t sp, L4_Word_t ip)
{
assert(t->status != TS_RUNNING);
t->ctx.r.esp = sp;
t->ctx.eip = ip;
}
static void copy_tcrs(void *dst, const void *src)
{
/* all the user-writable TCRs. */
static const int vregs[] = {
L4_TCR_VA_SENDER,
L4_TCR_XFERTIMEOUTS,
L4_TCR_COP_PREEMPT,
L4_TCR_EXCEPTIONHANDLER,
L4_TCR_PAGER,
L4_TCR_USERDEFINEDHANDLE,
L4_TCR_THREADWORD0,
L4_TCR_THREADWORD1,
};
for(int i=0; i < sizeof(vregs) / sizeof(vregs[0]); i++) {
L4_VREG(dst, vregs[i]) = L4_VREG(src, vregs[i]);
}
}
/* FIXME: this isn't atomic on reserved_gdt_ptr_seg() failure. */
int thread_set_utcb(struct thread *t, L4_Word_t start)
{
assert(t->space != NULL);
assert(t->utcb_pos < 0 || t->utcb_page != NULL);
assert(!L4_IsNilFpage(t->space->utcb_area));
assert((start & (UTCB_SIZE - 1)) == 0);
struct space *sp = t->space;
int new_pos = (start - L4_Address(sp->utcb_area)) / UTCB_SIZE;
if(new_pos == t->utcb_pos) return 0; /* no effect */
int page = new_pos / UTCB_PER_PAGE, slot = new_pos % UTCB_PER_PAGE;
assert(page < NUM_UTCB_PAGES(sp->utcb_area));
struct utcb_page *up = space_get_utcb_page(sp, page);
if(up == NULL) return -ENOMEM;
if(CHECK_FLAG(up->occmap, 1 << slot)) {
/* invalid destination: thread present. */
return -EEXIST;
}
const uint16_t hold_mask = 1 << UTCB_PER_PAGE;
assert(!CHECK_FLAG(up->occmap, hold_mask));
up->occmap |= hold_mask; /* hold despite space_remove_thread() */
if(t->utcb_ptr_seg != 0) {
release_gdt_ptr_seg(L4_Address(sp->utcb_area)
+ t->utcb_pos * UTCB_SIZE + 256 + TCR_UTCB_PTR * 4,
t->utcb_ptr_seg);
t->utcb_ptr_seg = 0;
}
assert(t->utcb_ptr_seg == 0);
/* TODO: allocate old_utcb in the heap & copy TCRs via it to work around
* the old one vanishing when it's the last in the old UTCB page. then
* remove the hold_mask trickery, which leaves empty utcb pages in the
* tree. (very minor.)
*/
void *old_utcb = NULL;
if(t->utcb_pos >= 0) {
old_utcb = thread_get_utcb(t);
space_remove_thread(sp, t);
}
t->utcb_page = up;
t->utcb_pos = new_pos;
space_add_thread(sp, t);
assert(CHECK_FLAG(up->occmap, hold_mask));
up->occmap &= ~hold_mask;
int offset = new_pos - (page * UTCB_PER_PAGE);
assert(up->pg->vm_addr != NULL);
void *utcb_mem = up->pg->vm_addr + offset * UTCB_SIZE;
memset(utcb_mem, 0, UTCB_SIZE);
if(old_utcb != NULL) {
copy_tcrs(utcb_mem + 256, old_utcb);
}
L4_VREG(utcb_mem + 256, L4_TCR_MYGLOBALID) = t->id;
*(L4_Word_t *)(utcb_mem + 256 + TCR_UTCB_PTR * 4) = start + 256;
assert(thread_get_utcb(t) == utcb_mem + 256);
assert(start == L4_Address(sp->utcb_area) + UTCB_SIZE * t->utcb_pos);
if(likely(sp != kernel_space)) {
assert(t->utcb_ptr_seg == 0);
t->utcb_ptr_seg = reserve_gdt_ptr_seg(start + 256 + TCR_UTCB_PTR * 4);
if(unlikely(t->utcb_ptr_seg < 0)) {
t->utcb_ptr_seg = 0;
return -ENOMEM;
}
}
return 0;
}
void thread_start(struct thread *t)
{
/* thread_start() is for freshly-created threads only. unhalting goes
* through thread_resume().
*/
assert(t->status == TS_STOPPED && !CHECK_FLAG(t->flags, TF_HALT));
t->status = TS_READY;
t->wakeup_time = 0;
sq_insert_thread(t);
}
void thread_halt(struct thread *t)
{
TRACE("%s: called for %lu:%lu from %p\n", __func__,
TID_THREADNUM(t->id), TID_VERSION(t->id), __builtin_return_address(0));
assert(!CHECK_FLAG_ANY(t->flags, TF_HALT | TF_PRE_RECV));
t->flags |= TF_HALT;
if(t->status == TS_READY
|| t->status == TS_RUNNING
|| t->status == TS_R_RECV)
{
if(t->status == TS_R_RECV) t->flags |= TF_PRE_RECV;
int old_status = t->status;
t->status = TS_STOPPED;
if(old_status != TS_READY || t->total_quantum > 0) {
sq_remove_thread(t);
}
} else if(t->status == TS_SEND_WAIT) {
/* render @t ineligible for active receive */
/* TODO: should redirect-blocked sendwait also put the redir_wait
* entry on ice? or is it enough to have redirectors not pick up for
* TF_HALTed redir_wait entries?
*/
remove_send_wait(t);
}
}
void thread_resume(struct thread *t)
{
assert(CHECK_FLAG(t->flags, TF_HALT));
int old = t->flags;
t->flags &= ~(TF_HALT | TF_PRE_RECV);
if(t->status == TS_STOPPED) {
t->status = CHECK_FLAG(old, TF_PRE_RECV) ? TS_R_RECV : TS_READY;
if(t->status != TS_R_RECV) {
/* it shouldn't be lost in R_RECV. */
t->wakeup_time = 0;
}
if(t->total_quantum > 0) sq_insert_thread(t);
} else if(t->status == TS_SEND_WAIT) {
/* FIXME: check return value and pass it on. it'll be tough to change
* all callsites of thread_resume(), but maybe it can be done.
*/
insert_send_wait(t);
}
}
uint64_t wakeup_at(L4_Time_t t)
{
if(t.raw == L4_ZeroTime.raw) return 0;
else if(t.raw == L4_Never.raw) return ~(uint64_t)0;
else {
L4_Clock_t base = { .raw = ksystemclock() };
if(L4_IsTimePoint_NP(t)) {
if(pt_is_valid(base, t)) {
return L4_PointClock_NP(base, t).raw;
} else {
/* like it was ZeroTime. */
return 0;
}
} else {
return base.raw + time_in_us(t);
}
}
}
void thread_sleep(struct thread *t, L4_Time_t period)
{
/* NOTE: this function was merged with thread_wake(), which asserted
* against {STOPPED, DEAD} rather than for the IPC wait states. callers
* should handle R_RECV separately as it doesn't instantly promote to
* RECV_WAIT.
*/
assert(L4_IsTimePeriod_NP(period));
#ifndef NDEBUG
if(period.raw != L4_ZeroTime.raw && period.raw != L4_Never.raw) {
TRACE("%s: sleeping thread %lu:%lu for %llu microseconds\n", __func__,
TID_THREADNUM(t->id), TID_VERSION(t->id), time_in_us(period));
} else if(period.raw == L4_Never.raw) {
TRACE("%s: sleeping thread %lu:%lu for good\n", __func__,
TID_THREADNUM(t->id), TID_VERSION(t->id));
}
#endif
if(period.raw == L4_ZeroTime.raw) {
if(CHECK_FLAG(t->flags, TF_HALT)) {
t->status = TS_STOPPED;
sq_remove_thread(t);
} else {
/* extreme napping */
if(t->status != TS_XFER) t->status = TS_READY;
t->wakeup_time = 0;
sq_update_thread(t);
if(t->status == TS_READY) {
might_preempt(t);
}
}
} else {
assert(IS_IPC_WAIT(t->status) || t->status == TS_XFER);
t->wakeup_time = wakeup_at(period);
sq_update_thread(t);
}
}
/* postcond: @t->status == TS_READY || @t->status == TS_STOPPED
* (depending on TF_HALT)
*/
void thread_ipc_fail(struct thread *t)
{
assert(t->status == TS_RECV_WAIT
|| t->status == TS_SEND_WAIT
|| t->status == TS_R_RECV
|| t->status == TS_XFER);
assert(t->ipc == NULL);
if(t->status == TS_SEND_WAIT || t->status == TS_RECV_WAIT) {
/* (NOTE: also, $t->ipc != NULL \implies \ Myself \notin \dom
* sendwait\_hash$ (and the same for recvwait_hash), so this isn't
* actually required. cancel_ipc_from() accepts that, though.)
*/
cancel_ipc_from(t);
}
if(CHECK_FLAG(t->flags, TF_HALT)) {
t->status = TS_STOPPED;
sq_remove_thread(t);
} else {
if(CHECK_FLAG(t->flags, TF_REDIR_WAIT)) {
remove_redir_wait(t);
}
t->status = TS_READY;
if(t->wakeup_time > 0) {
t->wakeup_time = 0;
sq_update_thread(t);
}
}
t->flags &= ~TF_REDIR_WAIT;
}
void *thread_get_utcb(struct thread *t)
{
assert(t != NULL);
assert(t->space != NULL);
assert(t->utcb_pos >= 0);
assert(t->utcb_pos < NUM_UTCB_PAGES(t->space->utcb_area) * UTCB_PER_PAGE);
int page_ix = t->utcb_pos / UTCB_PER_PAGE,
offset = t->utcb_pos & (UTCB_PER_PAGE - 1);
struct utcb_page *up = t->utcb_page;
assert(up->pos == page_ix);
assert(CHECK_FLAG(up->occmap, 1 << offset));
struct page *p = up->pg;
assert(p != NULL);
assert(p->vm_addr != NULL);
/* TODO: change the "256" that appears all over UTCB-related code to
* UTCB_SIZE / 2, which it properly is.
*/
return p->vm_addr + offset * UTCB_SIZE + 256;
}
/* look for a thread number, ignoring the version number. used from
* sys_threadcontrol(), thread_new(), and thread_get().
*
* this is possibly not as fast as it could be, however, it's quicker than the
* hashtable search of ra_id2ptr_safe() and accesses fewer cachelines over
* time. if at some point pt_get_pgid() loses its htable lookup (i.e. the
* kernel gets some way of quickly accessing its own pagetables), that method
* should be used instead of catch_pf().
*/
static struct thread *thread_find(thread_id tid)
{
if(unlikely(catch_pf() != 0)) return NULL;
uintptr_t ptr = tid >> (14 - thread_ra->id_shift);
ptr &= thread_ra->and_mask;
ptr |= thread_ra->or_mask;
struct thread *t = (struct thread *)ptr;
assert(t == ra_id2ptr(thread_ra, TID_THREADNUM(tid)));
if(unlikely(*(struct space *volatile *)&t->space == NULL)) t = NULL;
assert(t == NULL
|| TID_THREADNUM(t->id) == TID_THREADNUM(tid));
uncatch_pf();
return t;
}
struct thread *thread_get(L4_ThreadId_t tid)
{
assert(L4_IsGlobalId(tid));
struct thread *t = thread_find(tid.raw);
if(t != NULL && t->id != tid.raw) t = NULL;
return t;
}
struct thread *resolve_tid_spec(struct space *ref_space, L4_ThreadId_t tid)
{
if(L4_IsNilThread(tid)) return NULL;
else if(L4_IsLocalId(tid)) {
return space_find_local_thread(ref_space, tid.local);
} else {
return thread_get(tid);
}
}
bool thread_is_valid(const struct thread *t)
{
L4_ThreadId_t tid = L4_GlobalId(ra_ptr2id(thread_ra, t), 1);
struct thread *cand = thread_find(tid.raw);
assert(cand == NULL || cand == t);
return cand != NULL;
}
static void receive_breath_of_life(
struct hook *hook, void *sender, uintptr_t code, struct thread *t)
{
assert(t == container_of(hook, struct thread, post_exn_call));
hook_detach(hook);
if(code == 0) {
void *utcb = thread_get_utcb(sender);
L4_MsgTag_t tag = { .raw = L4_VREG(utcb, L4_TCR_MR(0)) };
TRACE("%s: in thread %lu:%lu, tag %#lx\n", __func__,
TID_THREADNUM(t->id), TID_VERSION(t->id),
tag.raw);
if(tag.X.u != 2 || tag.X.t != 0) return;
L4_Word_t ip = L4_VREG(utcb, L4_TCR_MR(1)),
sp = L4_VREG(utcb, L4_TCR_MR(2));
TRACE("%s: setting sp %#lx, ip %#lx\n", __func__, sp, ip);
thread_set_spip(t, sp, ip);
thread_wake(t);
}
}
L4_ThreadId_t get_local_id(struct thread *t)
{
assert(t->space != NULL);
assert(t->utcb_pos >= 0);
L4_ThreadId_t result = {
.raw = L4_Address(t->space->utcb_area)
+ t->utcb_pos * UTCB_SIZE + 256,
};
assert(L4_IsLocalId(result));
return result;
}
static inline bool selftest_enabled(void) {
#ifdef ENABLE_SELFTEST
return true;
#else
return false;
#endif
}
/* system calls */
/* exregs control bitmasks (W RCdh pufi sSRH) */
#define CTL_H 0x001
#define CTL_R 0x002
#define CTL_S 0x004
#define CTL_s 0x008
#define CTL_i 0x010
#define CTL_f 0x020
#define CTL_u 0x040
#define CTL_p 0x080
#define CTL_h 0x100
#define CTL_d 0x200
#define CTL_XFER_MASK (0x1c00)
SYSCALL L4_Word_t sys_exregs(
L4_ThreadId_t dest,
L4_Word_t *control_p,
L4_Word_t *sp_p, L4_Word_t *ip_p, L4_Word_t *flags_p,
L4_Word_t *udh_p,
L4_ThreadId_t *pager_p)
{
assert(check_thread_module(0)); /* NB: adds 15k cycles to benchmark */
struct thread *current = get_current_thread();
#ifndef NDEBUG
if(trace_is_enabled(TRID_THREAD)) {
char ctl_buf[16];
int cp = 0;
const char *ctl_chars = "HRSsifuphd";
for(int i=0; ctl_chars[i] != '\0'; i++) {
if(CHECK_FLAG(*control_p, 1 << i)) ctl_buf[cp++] = ctl_chars[i];
}
ctl_buf[cp] = '\0';
/* NOTE: this doesn't handle local TIDs at all well. */
TRACE("%s: called from %lu:%lu on %lu:%lu; control %#lx (%s)\n",
__func__,
TID_THREADNUM(current->id), TID_VERSION(current->id),
TID_THREADNUM(dest.raw), TID_VERSION(dest.raw),
*control_p, ctl_buf);
}
#endif
struct thread *dest_thread;
if(L4_IsNilThread(dest)
|| (dest_thread = resolve_tid_spec(current->space, dest)) == NULL
|| dest_thread->space != current->space
|| dest_thread->utcb_pos < 0)
{
if(unlikely(selftest_enabled()
&& !L4_IsNilThread(dest) && dest_thread != NULL
&& L4_ThreadNo(dest) == first_user_threadno()
&& CHECK_FLAG(current->space->flags, SF_PRIVILEGE)))
{
/* succeed, but return only pager. do nothing else. */
pager_p->raw = L4_VREG(thread_get_utcb(dest_thread),
L4_TCR_PAGER);
assert(dest_thread->space != current->space);
return dest_thread->id;
} else {
/* threadspec was invalid, or not present, or in a different
* space, or not active.
*/
L4_VREG(thread_get_utcb(current), L4_TCR_ERRORCODE) = 2;
return L4_nilthread.raw;
}
}
L4_ThreadId_t result;
if(L4_IsGlobalId(dest)) {
result = get_local_id(dest_thread);
assert(result.local.X.zeros == 0);
} else {
result.global.raw = dest_thread->id;
assert(result.local.X.zeros != 0);
}
L4_Word_t ctl_out = 0;
if(CHECK_FLAG(dest_thread->flags, TF_HALT)) ctl_out |= 1; /* H bit */
switch(dest_thread->status) {
case TS_RECV_WAIT:
case TS_R_RECV:
ctl_out |= 2; /* R bit */
break;
case TS_SEND_WAIT:
ctl_out |= 4; /* S bit */
break;
case TS_XFER:
assert(dest_thread->ipc != NULL);
ctl_out |= dest_thread == dest_thread->ipc->from ? 4 : 2;
break;
}
/* fast exit for L4_{Local,Global}IdOf() */
if(*control_p == 0) goto end;
if(unlikely(CHECK_FLAG_ANY(*control_p, CTL_XFER_MASK))) {
TRACE("%s: control transfer items are not supported by this microkernel\n",
__func__);
goto fail;
}
void *dest_utcb;
if(CHECK_FLAG_ANY(*control_p, CTL_R | CTL_S | CTL_p | CTL_u | CTL_d)) {
dest_utcb = thread_get_utcb(dest_thread);
} else {
dest_utcb = NULL;
}
L4_Word_t ctl_in = *control_p, sp_in = *sp_p, ip_in = *ip_p,
flags_in = *flags_p, udh_in = *udh_p;
L4_ThreadId_t pager_in = *pager_p;
if(CHECK_FLAG(ctl_in, CTL_d)) {
/* readout */
ctl_in &= ~CTL_d;
*sp_p = dest_thread->ctx.r.esp;
*ip_p = dest_thread->ctx.eip;
*flags_p = dest_thread->ctx.eflags;
*udh_p = L4_VREG(dest_utcb, L4_TCR_USERDEFINEDHANDLE);
pager_p->raw = L4_VREG(dest_utcb, L4_TCR_PAGER);
TRACE("%s: delivered ExchangeRegisters values\n", __func__);
}
if(CHECK_FLAG(ctl_in, CTL_R)) {
/* abort receive. */
/* TODO: check for the "currently receiving" state */
/* ... and move this into ipc.c, & also the one for the send side */
int state = dest_thread->status;
if(state == TS_R_RECV || state == TS_RECV_WAIT) {
thread_ipc_fail(dest_thread);
dest_thread->ipc_from = L4_nilthread;
/* "canceled in receive phase", but only communicate it for
* non-exception IPC.
*/
if(!post_exn_fail(dest_thread)) {
assert(dest_utcb != NULL);
set_ipc_error_thread(dest_thread, 7);
}
TRACE("%s: aborted receive\n", __func__);
}
ctl_in &= ~CTL_R;
}
if(CHECK_FLAG(ctl_in, CTL_S)) {
/* abort send. */
/* TODO: check for the "currently sending" state */
if(dest_thread->status == TS_SEND_WAIT) {
thread_ipc_fail(dest_thread);
dest_thread->ipc_from = L4_nilthread;
/* "canceled in send phase", but see comment above */
if(!post_exn_fail(dest_thread)) {
assert(dest_utcb != NULL);
set_ipc_error_thread(dest_thread, 6);
}
TRACE("%s: aborted send\n", __func__);
}
ctl_in &= ~CTL_S;
}
if(CHECK_FLAG(ctl_in, CTL_h)) {
int state = dest_thread->status;
if(!CHECK_FLAG(ctl_in, CTL_H)) {
if(!CHECK_FLAG(dest_thread->flags, TF_HALT)
&& state == TS_STOPPED)
{
TRACE("%s: starting fresh thread\n", __func__);
thread_start(dest_thread);
} else if(CHECK_FLAG(dest_thread->flags, TF_HALT)) {
TRACE("%s: calling thread_resume() on state %d\n",
__func__, state);
thread_resume(dest_thread);
}
} else {
if(!CHECK_FLAG(dest_thread->flags, TF_HALT)) {
thread_halt(dest_thread);
assert(dest_thread->status == TS_STOPPED
|| dest_thread->status == TS_RECV_WAIT
|| dest_thread->status == TS_SEND_WAIT);
TRACE("%s: halted thread\n", __func__);
}
}
ctl_in &= ~(CTL_H | CTL_h);
}
/* register-setting control bits. */
const L4_Word_t regset_mask = CTL_p | CTL_u | CTL_f | CTL_i | CTL_s;
if(CHECK_FLAG_ANY(ctl_in, regset_mask)) {
if(CHECK_FLAG(ctl_in, CTL_p)) {
assert(dest_utcb != NULL);
L4_VREG(dest_utcb, L4_TCR_PAGER) = pager_in.raw;
}
if(CHECK_FLAG(ctl_in, CTL_u)) {
assert(dest_utcb != NULL);
L4_VREG(dest_utcb, L4_TCR_USERDEFINEDHANDLE) = udh_in;
}
if(CHECK_FLAG(ctl_in, CTL_f)) {
dest_thread->ctx.eflags = x86_clean_eflags(
dest_thread->ctx.eflags, flags_in);
}
if(CHECK_FLAG(ctl_in, CTL_i)) {
/* no longer returning from a simple system call, so must use full
* context reload.
*/
dest_thread->flags &= ~TF_SYSCALL;
dest_thread->ctx.eip = ip_in;
}
if(CHECK_FLAG(ctl_in, CTL_s)) dest_thread->ctx.r.esp = sp_in;
ctl_in &= ~regset_mask;
}
if(unlikely(ctl_in != 0)) {
printf("%s: unhandled ExchangeRegister control bits %#lx\n",
__func__, ctl_in);
goto fail;
}
end:
*control_p = ctl_out;
TRACE("%s: returning control'=%#lx\n", __func__, *control_p);
assert(dest_thread == NULL || check_thread(0, dest_thread));
assert(check_thread_module(0));
return result.raw;
fail:
result = L4_nilthread;
/* HAIL SATAN errday */
L4_VREG(thread_get_utcb(current), L4_TCR_ERRORCODE) = 666;
goto end;
}
/* NOTE: "active high", "edge sensitive" is the default. there should be some
* ACPI shenanigans for recognizing legacy devices that instead do active-low
* signaling.
*/
static void int_enable(int irq)
{
x86_irq_disable();
set_irq_handler(irq, &int_trigger, IHF_AUTOMASK);
unmask_irq(irq, 0);
x86_irq_enable();
}
static void int_disable(int irq)
{
x86_irq_disable();
mask_irq(irq);
set_irq_handler(irq, NULL, 0);
x86_irq_enable();
}
static void int_unmask(int irq)
{
x86_irq_disable();
unmask_irq(irq, 0);
x86_irq_enable();
}
static L4_Word_t interrupt_ctl(
L4_Word_t *ec_p,
struct thread *current, L4_ThreadId_t dest_tid, L4_ThreadId_t pager)
{
int intnum = L4_ThreadNo(dest_tid);
assert(L4_Version(dest_tid) == 1);
assert(intnum < num_ints);
struct thread *pgt = thread_get(pager);
if(pager.raw != dest_tid.raw && pgt == NULL) {
*ec_p = 2; /* "unavailable thread", when pager isn't valid */
return 0;
}
assert(pgt != NULL || pager.raw == dest_tid.raw);
/* TODO: add mechanism for clearing TF_INTR also */
if(pgt == NULL) int_disable(intnum);
struct interrupt *it = &int_table[intnum];
it->pager = pgt;
it->pending = false;
it->delivered = false;
if(pgt != NULL) {
pgt->flags |= TF_INTR;
int_enable(intnum);
}
*ec_p = 0;
return 1;
}
/* returns true if t->status was changed. caller should set
* int_table[@intnum].delivered in that case to avoid double delivery.
*/
static bool send_int_ipc(int intnum, struct thread *t)
{
assert(t != NULL);
if(!CHECK_FLAG(t->flags, TF_INTR)) return false; /* nope.jpg */
if((t->status != TS_RECV_WAIT && t->status != TS_R_RECV)
|| (t->ipc_from.raw != L4_anythread.raw
&& t->ipc_from.raw != L4_GlobalId(intnum, 1).raw))
{
/* active signaling can't happen because the recipient isn't waiting.
* this one will be received with int_poll().
*/
return false;
} else {
void *utcb = thread_get_utcb(t);
L4_VREG(utcb, L4_TCR_MR(0)) = (L4_MsgTag_t){ .X.label = 0xfff0 }.raw;
t->ipc_from = L4_GlobalId(intnum, 1);
set_ipc_return_regs(&t->ctx.r, t, utcb);
thread_wake(t);
return true;
}
}
/* called from ThreadControl in the deletion & version stomp cases */
static void int_kick(struct thread *t)
{
assert(CHECK_FLAG(t->flags, TF_INTR));
/* brute force, but acceptable because interrupts are usually few. */
for(int i=0; i < num_ints; i++) {
struct interrupt *it = &int_table[i];
if(it->pager == t) {