forked from Stichting-MINIX-Research-Foundation/minix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproc.c
More file actions
1980 lines (1708 loc) · 59.6 KB
/
Copy pathproc.c
File metadata and controls
1980 lines (1708 loc) · 59.6 KB
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
/* This file contains essentially all of the process and message handling.
* Together with "mpx.s" it forms the lowest layer of the MINIX kernel.
* There is one entry point from the outside:
*
* sys_call: a system call, i.e., the kernel is trapped with an INT
*
* Changes:
* Aug 19, 2005 rewrote scheduling code (Jorrit N. Herder)
* Jul 25, 2005 rewrote system call handling (Jorrit N. Herder)
* May 26, 2005 rewrote message passing functions (Jorrit N. Herder)
* May 24, 2005 new notification system call (Jorrit N. Herder)
* Oct 28, 2004 nonblocking send and receive calls (Jorrit N. Herder)
*
* The code here is critical to make everything work and is important for the
* overall performance of the system. A large fraction of the code deals with
* list manipulation. To make this both easy to understand and fast to execute
* pointer pointers are used throughout the code. Pointer pointers prevent
* exceptions for the head or tail of a linked list.
*
* node_t *queue, *new_node; // assume these as global variables
* node_t **xpp = &queue; // get pointer pointer to head of queue
* while (*xpp != NULL) // find last pointer of the linked list
* xpp = &(*xpp)->next; // get pointer to next pointer
* *xpp = new_node; // now replace the end (the NULL pointer)
* new_node->next = NULL; // and mark the new end of the list
*
* For example, when adding a new node to the end of the list, one normally
* makes an exception for an empty list and looks up the end of the list for
* nonempty lists. As shown above, this is not required with pointer pointers.
*/
#include <stddef.h>
#include <signal.h>
#include <assert.h>
#include <string.h>
#include "vm.h"
#include "clock.h"
#include "spinlock.h"
#include "arch_proto.h"
#include <minix/syslib.h>
/* Scheduling and message passing functions */
static void idle(void);
/**
* Made public for use in clock.c (for user-space scheduling)
static int mini_send(struct proc *caller_ptr, endpoint_t dst_e, message
*m_ptr, int flags);
*/
static int mini_receive(struct proc *caller_ptr, endpoint_t src,
message *m_buff_usr, int flags);
static int mini_senda(struct proc *caller_ptr, asynmsg_t *table, size_t
size);
static int deadlock(int function, register struct proc *caller,
endpoint_t src_dst_e);
static int try_async(struct proc *caller_ptr);
static int try_one(endpoint_t receive_e, struct proc *src_ptr,
struct proc *dst_ptr);
static struct proc * pick_proc(void);
static void enqueue_head(struct proc *rp);
/* all idles share the same idle_priv structure */
static struct priv idle_priv;
static void set_idle_name(char * name, int n)
{
int i, c;
int p_z = 0;
if (n > 999)
n = 999;
name[0] = 'i';
name[1] = 'd';
name[2] = 'l';
name[3] = 'e';
for (i = 4, c = 100; c > 0; c /= 10) {
int digit;
digit = n / c;
n -= digit * c;
if (p_z || digit != 0 || c == 1) {
p_z = 1;
name[i++] = '0' + digit;
}
}
name[i] = '\0';
}
#define PICK_ANY 1
#define PICK_HIGHERONLY 2
#define BuildNotifyMessage(m_ptr, src, dst_ptr) \
memset((m_ptr), 0, sizeof(*(m_ptr))); \
(m_ptr)->m_type = NOTIFY_MESSAGE; \
(m_ptr)->m_notify.timestamp = get_monotonic(); \
switch (src) { \
case HARDWARE: \
(m_ptr)->m_notify.interrupts = \
priv(dst_ptr)->s_int_pending; \
priv(dst_ptr)->s_int_pending = 0; \
break; \
case SYSTEM: \
memcpy(&(m_ptr)->m_notify.sigset, \
&priv(dst_ptr)->s_sig_pending, \
sizeof(sigset_t)); \
sigemptyset(&priv(dst_ptr)->s_sig_pending); \
break; \
}
static message m_notify_buff = { 0, NOTIFY_MESSAGE };
void proc_init(void)
{
struct proc * rp;
struct priv *sp;
int i;
/* Clear the process table. Announce each slot as empty and set up
* mappings for proc_addr() and proc_nr() macros. Do the same for the
* table with privilege structures for the system processes.
*/
for (rp = BEG_PROC_ADDR, i = -NR_TASKS; rp < END_PROC_ADDR; ++rp, ++i) {
rp->p_rts_flags = RTS_SLOT_FREE;/* initialize free slot */
rp->p_magic = PMAGIC;
rp->p_nr = i; /* proc number from ptr */
rp->p_endpoint = _ENDPOINT(0, rp->p_nr); /* generation no. 0 */
rp->p_scheduler = NULL; /* no user space scheduler */
rp->p_priority = 0; /* no priority */
rp->p_quantum_size_ms = 0; /* no quantum size */
/* arch-specific initialization */
arch_proc_reset(rp);
}
for (sp = BEG_PRIV_ADDR, i = 0; sp < END_PRIV_ADDR; ++sp, ++i) {
sp->s_proc_nr = NONE; /* initialize as free */
sp->s_id = (sys_id_t) i; /* priv structure index */
ppriv_addr[i] = sp; /* priv ptr from number */
sp->s_sig_mgr = NONE; /* clear signal managers */
sp->s_bak_sig_mgr = NONE;
}
idle_priv.s_flags = IDL_F;
/* initialize IDLE structures for every CPU */
for (i = 0; i < CONFIG_MAX_CPUS; i++) {
struct proc * ip = get_cpu_var_ptr(i, idle_proc);
ip->p_endpoint = IDLE;
ip->p_priv = &idle_priv;
/* must not let idle ever get scheduled */
ip->p_rts_flags |= RTS_PROC_STOP;
set_idle_name(ip->p_name, i);
}
}
static void switch_address_space_idle(void)
{
#ifdef CONFIG_SMP
/*
* currently we bet that VM is always alive and its pages available so
* when the CPU wakes up the kernel is mapped and no surprises happen.
* This is only a problem if more than 1 cpus are available
*/
switch_address_space(proc_addr(VM_PROC_NR));
#endif
}
/*===========================================================================*
* idle *
*===========================================================================*/
static void idle(void)
{
struct proc * p;
/* This function is called whenever there is no work to do.
* Halt the CPU, and measure how many timestamp counter ticks are
* spent not doing anything. This allows test setups to measure
* the CPU utilization of certain workloads with high precision.
*/
p = get_cpulocal_var(proc_ptr) = get_cpulocal_var_ptr(idle_proc);
if (priv(p)->s_flags & BILLABLE)
get_cpulocal_var(bill_ptr) = p;
switch_address_space_idle();
#ifdef CONFIG_SMP
get_cpulocal_var(cpu_is_idle) = 1;
/* we don't need to keep time on APs as it is handled on the BSP */
if (cpuid != bsp_cpu_id)
stop_local_timer();
else
#endif
{
/*
* If the timer has expired while in kernel we must
* rearm it before we go to sleep
*/
restart_local_timer();
}
/* start accounting for the idle time */
context_stop(proc_addr(KERNEL));
#if !SPROFILE
halt_cpu();
#else
if (!sprofiling)
halt_cpu();
else {
volatile int * v;
v = get_cpulocal_var_ptr(idle_interrupted);
interrupts_enable();
while (!*v)
arch_pause();
interrupts_disable();
*v = 0;
}
#endif
/*
* end of accounting for the idle task does not happen here, the kernel
* is handling stuff for quite a while before it gets back here!
*/
}
/*===========================================================================*
* vm_suspend *
*===========================================================================*/
void vm_suspend(struct proc *caller, const struct proc *target,
const vir_bytes linaddr, const vir_bytes len, const int type,
const int writeflag)
{
/* This range is not OK for this process. Set parameters
* of the request and notify VM about the pending request.
*/
assert(!RTS_ISSET(caller, RTS_VMREQUEST));
assert(!RTS_ISSET(target, RTS_VMREQUEST));
RTS_SET(caller, RTS_VMREQUEST);
caller->p_vmrequest.req_type = VMPTYPE_CHECK;
caller->p_vmrequest.target = target->p_endpoint;
caller->p_vmrequest.params.check.start = linaddr;
caller->p_vmrequest.params.check.length = len;
caller->p_vmrequest.params.check.writeflag = writeflag;
caller->p_vmrequest.type = type;
/* Connect caller on vmrequest wait queue. */
if(!(caller->p_vmrequest.nextrequestor = vmrequest))
if(OK != send_sig(VM_PROC_NR, SIGKMEM))
panic("send_sig failed");
vmrequest = caller;
}
/*===========================================================================*
* delivermsg *
*===========================================================================*/
static void delivermsg(struct proc *rp)
{
assert(!RTS_ISSET(rp, RTS_VMREQUEST));
assert(rp->p_misc_flags & MF_DELIVERMSG);
assert(rp->p_delivermsg.m_source != NONE);
if (copy_msg_to_user(&rp->p_delivermsg,
(message *) rp->p_delivermsg_vir)) {
if(rp->p_misc_flags & MF_MSGFAILED) {
/* 2nd consecutive failure means this won't succeed */
printf("WARNING wrong user pointer 0x%08lx from "
"process %s / %d\n",
rp->p_delivermsg_vir,
rp->p_name,
rp->p_endpoint);
cause_sig(rp->p_nr, SIGSEGV);
} else {
/* 1st failure means we have to ask VM to handle it */
vm_suspend(rp, rp, rp->p_delivermsg_vir,
sizeof(message), VMSTYPE_DELIVERMSG, 1);
rp->p_misc_flags |= MF_MSGFAILED;
}
} else {
/* Indicate message has been delivered; address is 'used'. */
rp->p_delivermsg.m_source = NONE;
rp->p_misc_flags &= ~(MF_DELIVERMSG|MF_MSGFAILED);
if(!(rp->p_misc_flags & MF_CONTEXT_SET)) {
rp->p_reg.retreg = OK;
}
}
}
/*===========================================================================*
* switch_to_user *
*===========================================================================*/
void switch_to_user(void)
{
/* This function is called an instant before proc_ptr is
* to be scheduled again.
*/
struct proc * p;
#ifdef CONFIG_SMP
int tlb_must_refresh = 0;
#endif
p = get_cpulocal_var(proc_ptr);
/*
* if the current process is still runnable check the misc flags and let
* it run unless it becomes not runnable in the meantime
*/
if (proc_is_runnable(p))
goto check_misc_flags;
/*
* if a process becomes not runnable while handling the misc flags, we
* need to pick a new one here and start from scratch. Also if the
* current process wasn't runnable, we pick a new one here
*/
not_runnable_pick_new:
if (proc_is_preempted(p)) {
p->p_rts_flags &= ~RTS_PREEMPTED;
if (proc_is_runnable(p)) {
if (p->p_cpu_time_left)
enqueue_head(p);
else
enqueue(p);
}
}
/*
* if we have no process to run, set IDLE as the current process for
* time accounting and put the cpu in an idle state. After the next
* timer interrupt the execution resumes here and we can pick another
* process. If there is still nothing runnable we "schedule" IDLE again
*/
while (!(p = pick_proc())) {
idle();
}
/* update the global variable */
get_cpulocal_var(proc_ptr) = p;
#ifdef CONFIG_SMP
if (p->p_misc_flags & MF_FLUSH_TLB && get_cpulocal_var(ptproc) == p)
tlb_must_refresh = 1;
#endif
switch_address_space(p);
check_misc_flags:
assert(p);
assert(proc_is_runnable(p));
while (p->p_misc_flags &
(MF_KCALL_RESUME | MF_DELIVERMSG |
MF_SC_DEFER | MF_SC_TRACE | MF_SC_ACTIVE)) {
assert(proc_is_runnable(p));
if (p->p_misc_flags & MF_KCALL_RESUME) {
kernel_call_resume(p);
}
else if (p->p_misc_flags & MF_DELIVERMSG) {
TRACE(VF_SCHEDULING, printf("delivering to %s / %d\n",
p->p_name, p->p_endpoint););
delivermsg(p);
}
else if (p->p_misc_flags & MF_SC_DEFER) {
/* Perform the system call that we deferred earlier. */
assert (!(p->p_misc_flags & MF_SC_ACTIVE));
arch_do_syscall(p);
/* If the process is stopped for signal delivery, and
* not blocked sending a message after the system call,
* inform PM.
*/
if ((p->p_misc_flags & MF_SIG_DELAY) &&
!RTS_ISSET(p, RTS_SENDING))
sig_delay_done(p);
}
else if (p->p_misc_flags & MF_SC_TRACE) {
/* Trigger a system call leave event if this was a
* system call. We must do this after processing the
* other flags above, both for tracing correctness and
* to be able to use 'break'.
*/
if (!(p->p_misc_flags & MF_SC_ACTIVE))
break;
p->p_misc_flags &=
~(MF_SC_TRACE | MF_SC_ACTIVE);
/* Signal the "leave system call" event.
* Block the process.
*/
cause_sig(proc_nr(p), SIGTRAP);
}
else if (p->p_misc_flags & MF_SC_ACTIVE) {
/* If MF_SC_ACTIVE was set, remove it now:
* we're leaving the system call.
*/
p->p_misc_flags &= ~MF_SC_ACTIVE;
break;
}
/*
* the selected process might not be runnable anymore. We have
* to checkit and schedule another one
*/
if (!proc_is_runnable(p))
goto not_runnable_pick_new;
}
/*
* check the quantum left before it runs again. We must do it only here
* as we are sure that a possible out-of-quantum message to the
* scheduler will not collide with the regular ipc
*/
if (!p->p_cpu_time_left)
proc_no_time(p);
/*
* After handling the misc flags the selected process might not be
* runnable anymore. We have to checkit and schedule another one
*/
if (!proc_is_runnable(p))
goto not_runnable_pick_new;
TRACE(VF_SCHEDULING, printf("cpu %d starting %s / %d "
"pc 0x%08x\n",
cpuid, p->p_name, p->p_endpoint, p->p_reg.pc););
#if DEBUG_TRACE
p->p_schedules++;
#endif
p = arch_finish_switch_to_user();
assert(p->p_cpu_time_left);
context_stop(proc_addr(KERNEL));
/* If the process isn't the owner of FPU, enable the FPU exception */
if (get_cpulocal_var(fpu_owner) != p)
enable_fpu_exception();
else
disable_fpu_exception();
/* If MF_CONTEXT_SET is set, don't clobber process state within
* the kernel. The next kernel entry is OK again though.
*/
p->p_misc_flags &= ~MF_CONTEXT_SET;
#if defined(__i386__)
assert(p->p_seg.p_cr3 != 0);
#elif defined(__arm__)
assert(p->p_seg.p_ttbr != 0);
#endif
#ifdef CONFIG_SMP
if (p->p_misc_flags & MF_FLUSH_TLB) {
if (tlb_must_refresh)
refresh_tlb();
p->p_misc_flags &= ~MF_FLUSH_TLB;
}
#endif
restart_local_timer();
/*
* restore_user_context() carries out the actual mode switch from kernel
* to userspace. This function does not return
*/
restore_user_context(p);
NOT_REACHABLE;
}
/*
* handler for all synchronous IPC calls
*/
static int do_sync_ipc(struct proc * caller_ptr, /* who made the call */
int call_nr, /* system call number and flags */
endpoint_t src_dst_e, /* src or dst of the call */
message *m_ptr) /* users pointer to a message */
{
int result; /* the system call's result */
int src_dst_p; /* Process slot number */
char *callname;
/* Check destination. RECEIVE is the only call that accepts ANY (in addition
* to a real endpoint). The other calls (SEND, SENDREC, and NOTIFY) require an
* endpoint to corresponds to a process. In addition, it is necessary to check
* whether a process is allowed to send to a given destination.
*/
assert(call_nr != SENDA);
/* Only allow non-negative call_nr values less than 32 */
if (call_nr < 0 || call_nr > IPCNO_HIGHEST || call_nr >= 32
|| !(callname = ipc_call_names[call_nr])) {
#if DEBUG_ENABLE_IPC_WARNINGS
printf("sys_call: trap %d not allowed, caller %d, src_dst %d\n",
call_nr, proc_nr(caller_ptr), src_dst_e);
#endif
return(ETRAPDENIED); /* trap denied by mask or kernel */
}
if (src_dst_e == ANY)
{
if (call_nr != RECEIVE)
{
#if 0
printf("sys_call: %s by %d with bad endpoint %d\n",
callname,
proc_nr(caller_ptr), src_dst_e);
#endif
return EINVAL;
}
src_dst_p = (int) src_dst_e;
}
else
{
/* Require a valid source and/or destination process. */
if(!isokendpt(src_dst_e, &src_dst_p)) {
#if 0
printf("sys_call: %s by %d with bad endpoint %d\n",
callname,
proc_nr(caller_ptr), src_dst_e);
#endif
return EDEADSRCDST;
}
/* If the call is to send to a process, i.e., for SEND, SENDNB,
* SENDREC or NOTIFY, verify that the caller is allowed to send to
* the given destination.
*/
if (call_nr != RECEIVE)
{
if (!may_send_to(caller_ptr, src_dst_p)) {
#if DEBUG_ENABLE_IPC_WARNINGS
printf(
"sys_call: ipc mask denied %s from %d to %d\n",
callname,
caller_ptr->p_endpoint, src_dst_e);
#endif
return(ECALLDENIED); /* call denied by ipc mask */
}
}
}
/* Check if the process has privileges for the requested call. Calls to the
* kernel may only be SENDREC, because tasks always reply and may not block
* if the caller doesn't do receive().
*/
if (!(priv(caller_ptr)->s_trap_mask & (1 << call_nr))) {
#if DEBUG_ENABLE_IPC_WARNINGS
printf("sys_call: %s not allowed, caller %d, src_dst %d\n",
callname, proc_nr(caller_ptr), src_dst_p);
#endif
return(ETRAPDENIED); /* trap denied by mask or kernel */
}
if (call_nr != SENDREC && call_nr != RECEIVE && iskerneln(src_dst_p)) {
#if DEBUG_ENABLE_IPC_WARNINGS
printf("sys_call: trap %s not allowed, caller %d, src_dst %d\n",
callname, proc_nr(caller_ptr), src_dst_e);
#endif
return(ETRAPDENIED); /* trap denied by mask or kernel */
}
switch(call_nr) {
case SENDREC:
/* A flag is set so that notifications cannot interrupt SENDREC. */
caller_ptr->p_misc_flags |= MF_REPLY_PEND;
/* fall through */
case SEND:
result = mini_send(caller_ptr, src_dst_e, m_ptr, 0);
if (call_nr == SEND || result != OK)
break; /* done, or SEND failed */
/* fall through for SENDREC */
case RECEIVE:
if (call_nr == RECEIVE) {
caller_ptr->p_misc_flags &= ~MF_REPLY_PEND;
IPC_STATUS_CLEAR(caller_ptr); /* clear IPC status code */
}
result = mini_receive(caller_ptr, src_dst_e, m_ptr, 0);
break;
case NOTIFY:
result = mini_notify(caller_ptr, src_dst_e);
break;
case SENDNB:
result = mini_send(caller_ptr, src_dst_e, m_ptr, NON_BLOCKING);
break;
default:
result = EBADCALL; /* illegal system call */
}
/* Now, return the result of the system call to the caller. */
return(result);
}
int do_ipc(reg_t r1, reg_t r2, reg_t r3)
{
struct proc *const caller_ptr = get_cpulocal_var(proc_ptr); /* get pointer to caller */
int call_nr = (int) r1;
assert(!RTS_ISSET(caller_ptr, RTS_SLOT_FREE));
/* bill kernel time to this process. */
kbill_ipc = caller_ptr;
/* If this process is subject to system call tracing, handle that first. */
if (caller_ptr->p_misc_flags & (MF_SC_TRACE | MF_SC_DEFER)) {
/* Are we tracing this process, and is it the first sys_call entry? */
if ((caller_ptr->p_misc_flags & (MF_SC_TRACE | MF_SC_DEFER)) ==
MF_SC_TRACE) {
/* We must notify the tracer before processing the actual
* system call. If we don't, the tracer could not obtain the
* input message. Postpone the entire system call.
*/
caller_ptr->p_misc_flags &= ~MF_SC_TRACE;
assert(!(caller_ptr->p_misc_flags & MF_SC_DEFER));
caller_ptr->p_misc_flags |= MF_SC_DEFER;
caller_ptr->p_defer.r1 = r1;
caller_ptr->p_defer.r2 = r2;
caller_ptr->p_defer.r3 = r3;
/* Signal the "enter system call" event. Block the process. */
cause_sig(proc_nr(caller_ptr), SIGTRAP);
/* Preserve the return register's value. */
return caller_ptr->p_reg.retreg;
}
/* If the MF_SC_DEFER flag is set, the syscall is now being resumed. */
caller_ptr->p_misc_flags &= ~MF_SC_DEFER;
assert (!(caller_ptr->p_misc_flags & MF_SC_ACTIVE));
/* Set a flag to allow reliable tracing of leaving the system call. */
caller_ptr->p_misc_flags |= MF_SC_ACTIVE;
}
if(caller_ptr->p_misc_flags & MF_DELIVERMSG) {
panic("sys_call: MF_DELIVERMSG on for %s / %d\n",
caller_ptr->p_name, caller_ptr->p_endpoint);
}
/* Now check if the call is known and try to perform the request. The only
* system calls that exist in MINIX are sending and receiving messages.
* - SENDREC: combines SEND and RECEIVE in a single system call
* - SEND: sender blocks until its message has been delivered
* - RECEIVE: receiver blocks until an acceptable message has arrived
* - NOTIFY: asynchronous call; deliver notification or mark pending
* - SENDA: list of asynchronous send requests
*/
switch(call_nr) {
case SENDREC:
case SEND:
case RECEIVE:
case NOTIFY:
case SENDNB:
{
/* Process accounting for scheduling */
caller_ptr->p_accounting.ipc_sync++;
return do_sync_ipc(caller_ptr, call_nr, (endpoint_t) r2,
(message *) r3);
}
case SENDA:
{
/*
* Get and check the size of the argument in bytes as it is a
* table
*/
size_t msg_size = (size_t) r2;
/* Process accounting for scheduling */
caller_ptr->p_accounting.ipc_async++;
/* Limit size to something reasonable. An arbitrary choice is 16
* times the number of process table entries.
*/
if (msg_size > 16*(NR_TASKS + NR_PROCS))
return EDOM;
return mini_senda(caller_ptr, (asynmsg_t *) r3, msg_size);
}
case MINIX_KERNINFO:
{
/* It might not be initialized yet. */
if(!minix_kerninfo_user) {
return EBADCALL;
}
arch_set_secondary_ipc_return(caller_ptr, minix_kerninfo_user);
return OK;
}
default:
return EBADCALL; /* illegal system call */
}
}
/*===========================================================================*
* deadlock *
*===========================================================================*/
static int deadlock(
int function, /* trap number */
register struct proc *cp, /* pointer to caller */
endpoint_t src_dst_e /* src or dst process */
)
{
/* Check for deadlock. This can happen if 'caller_ptr' and 'src_dst' have
* a cyclic dependency of blocking send and receive calls. The only cyclic
* dependency that is not fatal is if the caller and target directly SEND(REC)
* and RECEIVE to each other. If a deadlock is found, the group size is
* returned. Otherwise zero is returned.
*/
register struct proc *xp; /* process pointer */
int group_size = 1; /* start with only caller */
#if DEBUG_ENABLE_IPC_WARNINGS
static struct proc *processes[NR_PROCS + NR_TASKS];
processes[0] = cp;
#endif
while (src_dst_e != ANY) { /* check while process nr */
int src_dst_slot;
okendpt(src_dst_e, &src_dst_slot);
xp = proc_addr(src_dst_slot); /* follow chain of processes */
assert(proc_ptr_ok(xp));
assert(!RTS_ISSET(xp, RTS_SLOT_FREE));
#if DEBUG_ENABLE_IPC_WARNINGS
processes[group_size] = xp;
#endif
group_size ++; /* extra process in group */
/* Check whether the last process in the chain has a dependency. If it
* has not, the cycle cannot be closed and we are done.
*/
if((src_dst_e = P_BLOCKEDON(xp)) == NONE)
return 0;
/* Now check if there is a cyclic dependency. For group sizes of two,
* a combination of SEND(REC) and RECEIVE is not fatal. Larger groups
* or other combinations indicate a deadlock.
*/
if (src_dst_e == cp->p_endpoint) { /* possible deadlock */
if (group_size == 2) { /* caller and src_dst */
/* The function number is magically converted to flags. */
if ((xp->p_rts_flags ^ (function << 2)) & RTS_SENDING) {
return(0); /* not a deadlock */
}
}
#if DEBUG_ENABLE_IPC_WARNINGS
{
int i;
printf("deadlock between these processes:\n");
for(i = 0; i < group_size; i++) {
printf(" %10s ", processes[i]->p_name);
}
printf("\n\n");
for(i = 0; i < group_size; i++) {
print_proc(processes[i]);
proc_stacktrace(processes[i]);
}
}
#endif
return(group_size); /* deadlock found */
}
}
return(0); /* not a deadlock */
}
/*===========================================================================*
* has_pending *
*===========================================================================*/
static int has_pending(sys_map_t *map, int src_p, int asynm)
{
/* Check to see if there is a pending message from the desired source
* available.
*/
int src_id;
sys_id_t id = NULL_PRIV_ID;
#ifdef CONFIG_SMP
struct proc * p;
#endif
/* Either check a specific bit in the mask map, or find the first bit set in
* it (if any), depending on whether the receive was called on a specific
* source endpoint.
*/
if (src_p != ANY) {
src_id = nr_to_id(src_p);
if (get_sys_bit(*map, src_id)) {
#ifdef CONFIG_SMP
p = proc_addr(id_to_nr(src_id));
if (asynm && RTS_ISSET(p, RTS_VMINHIBIT))
p->p_misc_flags |= MF_SENDA_VM_MISS;
else
#endif
id = src_id;
}
} else {
/* Find a source with a pending message */
for (src_id = 0; src_id < NR_SYS_PROCS; src_id += BITCHUNK_BITS) {
if (get_sys_bits(*map, src_id) != 0) {
#ifdef CONFIG_SMP
while (src_id < NR_SYS_PROCS) {
while (!get_sys_bit(*map, src_id)) {
if (src_id == NR_SYS_PROCS)
goto quit_search;
src_id++;
}
p = proc_addr(id_to_nr(src_id));
/*
* We must not let kernel fiddle with pages of a
* process which are currently being changed by
* VM. It is dangerous! So do not report such a
* process as having pending async messages.
* Skip it.
*/
if (asynm && RTS_ISSET(p, RTS_VMINHIBIT)) {
p->p_misc_flags |= MF_SENDA_VM_MISS;
src_id++;
} else
goto quit_search;
}
#else
while (!get_sys_bit(*map, src_id)) src_id++;
goto quit_search;
#endif
}
}
quit_search:
if (src_id < NR_SYS_PROCS) /* Found one */
id = src_id;
}
return(id);
}
/*===========================================================================*
* has_pending_notify *
*===========================================================================*/
int has_pending_notify(struct proc * caller, int src_p)
{
sys_map_t * map = &priv(caller)->s_notify_pending;
return has_pending(map, src_p, 0);
}
/*===========================================================================*
* has_pending_asend *
*===========================================================================*/
int has_pending_asend(struct proc * caller, int src_p)
{
sys_map_t * map = &priv(caller)->s_asyn_pending;
return has_pending(map, src_p, 1);
}
/*===========================================================================*
* unset_notify_pending *
*===========================================================================*/
void unset_notify_pending(struct proc * caller, int src_p)
{
sys_map_t * map = &priv(caller)->s_notify_pending;
unset_sys_bit(*map, src_p);
}
/*===========================================================================*
* mini_send *
*===========================================================================*/
int mini_send(
register struct proc *caller_ptr, /* who is trying to send a message? */
endpoint_t dst_e, /* to whom is message being sent? */
message *m_ptr, /* pointer to message buffer */
const int flags
)
{
/* Send a message from 'caller_ptr' to 'dst'. If 'dst' is blocked waiting
* for this message, copy the message to it and unblock 'dst'. If 'dst' is
* not waiting at all, or is waiting for another source, queue 'caller_ptr'.
*/
register struct proc *dst_ptr;
register struct proc **xpp;
int dst_p;
dst_p = _ENDPOINT_P(dst_e);
dst_ptr = proc_addr(dst_p);
if (RTS_ISSET(dst_ptr, RTS_NO_ENDPOINT))
{
return EDEADSRCDST;
}
/* Check if 'dst' is blocked waiting for this message. The destination's
* RTS_SENDING flag may be set when its SENDREC call blocked while sending.
*/
if (WILLRECEIVE(caller_ptr->p_endpoint, dst_ptr, (vir_bytes)m_ptr, NULL)) {
int call;
/* Destination is indeed waiting for this message. */
assert(!(dst_ptr->p_misc_flags & MF_DELIVERMSG));
if (!(flags & FROM_KERNEL)) {
if(copy_msg_from_user(m_ptr, &dst_ptr->p_delivermsg))
return EFAULT;
} else {
dst_ptr->p_delivermsg = *m_ptr;
IPC_STATUS_ADD_FLAGS(dst_ptr, IPC_FLG_MSG_FROM_KERNEL);
}
dst_ptr->p_delivermsg.m_source = caller_ptr->p_endpoint;
dst_ptr->p_misc_flags |= MF_DELIVERMSG;
call = (caller_ptr->p_misc_flags & MF_REPLY_PEND ? SENDREC
: (flags & NON_BLOCKING ? SENDNB : SEND));
IPC_STATUS_ADD_CALL(dst_ptr, call);
if (dst_ptr->p_misc_flags & MF_REPLY_PEND)
dst_ptr->p_misc_flags &= ~MF_REPLY_PEND;
RTS_UNSET(dst_ptr, RTS_RECEIVING);
#if DEBUG_IPC_HOOK
hook_ipc_msgsend(&dst_ptr->p_delivermsg, caller_ptr, dst_ptr);
hook_ipc_msgrecv(&dst_ptr->p_delivermsg, caller_ptr, dst_ptr);
#endif
} else {
if(flags & NON_BLOCKING) {
return(ENOTREADY);
}
/* Check for a possible deadlock before actually blocking. */
if (deadlock(SEND, caller_ptr, dst_e)) {
return(ELOCKED);
}
/* Destination is not waiting. Block and dequeue caller. */
if (!(flags & FROM_KERNEL)) {
if(copy_msg_from_user(m_ptr, &caller_ptr->p_sendmsg))
return EFAULT;
} else {
caller_ptr->p_sendmsg = *m_ptr;
/*
* we need to remember that this message is from kernel so we
* can set the delivery status flags when the message is
* actually delivered
*/
caller_ptr->p_misc_flags |= MF_SENDING_FROM_KERNEL;
}
RTS_SET(caller_ptr, RTS_SENDING);
caller_ptr->p_sendto_e = dst_e;
/* Process is now blocked. Put in on the destination's queue. */
assert(caller_ptr->p_q_link == NULL);
xpp = &dst_ptr->p_caller_q; /* find end of list */
while (*xpp) xpp = &(*xpp)->p_q_link;
*xpp = caller_ptr; /* add caller to end */
#if DEBUG_IPC_HOOK
hook_ipc_msgsend(&caller_ptr->p_sendmsg, caller_ptr, dst_ptr);
#endif
}
return(OK);
}
/*===========================================================================*
* mini_receive *
*===========================================================================*/
static int mini_receive(struct proc * caller_ptr,
endpoint_t src_e, /* which message source is wanted */
message * m_buff_usr, /* pointer to message buffer */
const int flags)
{
/* A process or task wants to get a message. If a message is already queued,
* acquire it and deblock the sender. If no message from the desired source
* is available block the caller.
*/
register struct proc **xpp;
int r, src_id, found, src_proc_nr, src_p;
endpoint_t sender_e;
assert(!(caller_ptr->p_misc_flags & MF_DELIVERMSG));
/* This is where we want our message. */
caller_ptr->p_delivermsg_vir = (vir_bytes) m_buff_usr;
if(src_e == ANY) src_p = ANY;
else
{
okendpt(src_e, &src_p);
if (RTS_ISSET(proc_addr(src_p), RTS_NO_ENDPOINT))
{
return EDEADSRCDST;
}
}
/* Check to see if a message from desired source is already available. The
* caller's RTS_SENDING flag may be set if SENDREC couldn't send. If it is
* set, the process should be blocked.
*/
if (!RTS_ISSET(caller_ptr, RTS_SENDING)) {