-
Notifications
You must be signed in to change notification settings - Fork 51
/
bios.c
1225 lines (901 loc) · 26.4 KB
/
bios.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 <stdlib.h>
#include <assert.h>
#include <stdint.h>
#include <pthread.h>
#include <signal.h>
#include <time.h>
#include <sys/select.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/select.h>
#include <sys/signalfd.h>
#include <sys/sysinfo.h>
#include <unistd.h>
#include <fcntl.h>
#include <poll.h>
#include "util.h"
#include "bios.h"
/*
Implementation of bios.h API
Basic idea:
- Each core is simulated by a pthread
- One POSIX timer per core thread
- Core threads mask all signals except for USR1.
- The PIC thread receives all signals and dispatches them to
the right core thread by raising SIGUSR1.
*/
#if 0
#define CORE_STATISTICS
#endif
/*
Per-core data.
*/
typedef struct core
{
uint id;
interrupt_handler* bootfunc;
pthread_t thread;
struct sigevent timer_sigevent;
timer_t timer_id;
volatile uint32_t intr_pending;
interrupt_handler* intvec[maximum_interrupt_no];
#if defined(CORE_STATISTICS)
/* Statistics */
volatile uintptr_t irq_count;
volatile uintptr_t irq_raised[maximum_interrupt_no];
volatile uintptr_t irq_delivered[maximum_interrupt_no];
volatile uintptr_t hlt_count;
volatile uintptr_t rst_count;
volatile TimerDuration hlt_time;
volatile TimerDuration run_time;
#endif
} Core;
/* Used to store the set of core threads' signal mask */
static sigset_t core_signal_set;
/* Uset to store the singleton set containing SIGUSR1 */
static sigset_t sigusr1_set;
/* Uset to store the singleton set containing SIGALRM */
static sigset_t sigalrm_set;
/* Used to create the signalfd */
static sigset_t signalfd_set;
/* Array of Core objects, one per core */
static Core CORE[MAX_CORES];
/* Number of cores */
static unsigned int ncores = 0;
/* Core barrier */
static pthread_barrier_t system_barrier, core_barrier;
/* Flag that signals that PIC daemon should be active */
static volatile sig_atomic_t PIC_active;
/* Bit vector denoting halted cores */
static _Atomic uint32_t halt_vector;
/* PIC thread id */
static pthread_t PIC_thread;
/* Save the sigaction for SIGUSR1 */
static struct sigaction USR1_saved_sigaction;
/* The sigaction for SIGUSR1 (core interrupts) */
static struct sigaction USR1_sigaction;
/* This gives a rough serial port timeout of 300 msec */
#define SERIAL_TIMEOUT 300000
/* Forward decl. of per-core signal handler */
static void sigusr1_handler(int signo, siginfo_t* si, void* ctx);
/* PIC daemon statistics */
static unsigned long PIC_loops;
/* Physical cores (needed for some heuristics) */
static unsigned int physical_cores;
/* Initialize static vars. This is called via pthread_once() */
static pthread_once_t init_control = PTHREAD_ONCE_INIT;
static void initialize()
{
physical_cores = get_nprocs();
USR1_sigaction.sa_sigaction = sigusr1_handler;
USR1_sigaction.sa_flags = SA_SIGINFO;
sigemptyset(& USR1_sigaction.sa_mask);
/* Create the sigmask to block all signals, except USR1 */
CHECK(sigfillset(&core_signal_set));
CHECK(sigdelset(&core_signal_set, SIGUSR1));
/* Create the mask for blocking SIGUSR1 */
CHECK(sigemptyset(&sigusr1_set));
CHECK(sigaddset(&sigusr1_set, SIGUSR1));
/* Create the mask for SIGALRM */
CHECK(sigemptyset(&sigalrm_set));
CHECK(sigaddset(&sigalrm_set, SIGALRM));
/* Create signaldf_set */
CHECK(sigemptyset(&signalfd_set));
CHECK(sigaddset(&signalfd_set, SIGUSR1));
CHECK(sigaddset(&signalfd_set, SIGALRM));
}
/*
Static func to access the thread-local Core.
*/
_Thread_local uint cpu_core_id;
static inline Core* curr_core() {
return CORE+cpu_core_id;
}
/*
Cause PIC daemon to loop. This needs to happen when we wish
the PIC daemon to refresh the list of fds it is polling.
*/
static inline void interrupt_pic_thread()
{
union sigval coreval;
coreval.sival_ptr = NULL; /* This is silly, but silences valgrind */
coreval.sival_int = -1;
CHECKRC(pthread_sigqueue(PIC_thread, SIGUSR1, coreval));
}
/*
Helper pthread-startable function to launch a core thread.
*/
static void* core_thread(void* _core)
{
Core* core = (Core*)_core;
/* Clear pending bitvec */
core->intr_pending = 0;
/* Default interrupt handlers */
for(int i=0; i<maximum_interrupt_no; i++)
core->intvec[i] = NULL;
cpu_core_id = core->id;
/* Set core signal mask */
CHECKRC(pthread_sigmask(SIG_BLOCK, &core_signal_set, NULL));
/* create a thread-specific timer */
core->timer_sigevent.sigev_notify = SIGEV_SIGNAL;
core->timer_sigevent.sigev_signo = SIGALRM;
core->timer_sigevent.sigev_value.sival_int = core->id;
// Could also be CLOCK_REALTIME
CHECK(timer_create(CLOCK_MONOTONIC, & core->timer_sigevent, & core->timer_id));
/* sync with all cores */
pthread_barrier_wait(& system_barrier);
/* execute the boot code */
core->bootfunc();
/* Reset interrupt handlers to null, to stop processing interrupts. */
for(int i=0; i<maximum_interrupt_no; i++) {
core->intvec[i] = NULL;
}
/* Delete the core timer */
CHECK(timer_delete(core->timer_id));
pthread_barrier_wait(& core_barrier);
/* Stop PIC daemon */
if(core->id==0) {
PIC_active = 0;
interrupt_pic_thread();
}
/* sync with all cores */
pthread_barrier_wait(& system_barrier);
return _core;
}
/*
Set pending interrupt, return previous value
*/
static inline int intr_fetch_set(Core* core, Interrupt intno)
{
uint32_t sel = 1<<intno;
uint32_t old = __atomic_fetch_or(& core->intr_pending, sel, __ATOMIC_ACQ_REL);
return (old & sel) != 0;
}
/*
Clear pending interrupt for core, return previous value
*/
static inline int intr_fetch_clear(Core* core, Interrupt intno)
{
uint32_t sel = 1<<intno;
uint32_t old = __atomic_fetch_and(& core->intr_pending, ~sel, __ATOMIC_ACQ_REL);
return (old & sel) != 0;
}
/*
If there are pending interrupts for core, clear lowest pending interrupt,
store in intp and return 1, else return 0.
*/
static inline int intr_fetch_lowest(Core* core, Interrupt* intp)
{
Interrupt irq;
uint32_t ipnvec;
uint32_t ipvec = core->intr_pending;
do {
if(! ipvec) return 0;
irq = __builtin_ctz(ipvec);
assert(irq < maximum_interrupt_no);
ipnvec = ipvec & ~(1 << irq);
} while(! __atomic_compare_exchange_n(& core->intr_pending, &ipvec, ipnvec, 0,
__ATOMIC_ACQ_REL, __ATOMIC_RELAXED));
*intp = irq;
return 1;
}
/*
Cause the given core to be interrupted in the future.
This function does not add a pending interrupt, but
causes a signal to be sent to the core.
*/
static inline void interrupt_core(Core* core)
{
union sigval coreval;
coreval.sival_ptr = NULL; /* This is to silence valgrind */
coreval.sival_int = core->id;
CHECKRC(pthread_sigqueue(core->thread, SIGUSR1, coreval));
}
/*
Raise an interrupt to a core.
Adds intno as pending for the core and causes a signal to
be delivered.
*/
static inline void raise_interrupt(Core* core, Interrupt intno)
{
if(! intr_fetch_set(core, intno) ) {
#if defined(CORE_STATISTICS)
core->irq_raised[intno] ++;
#endif
interrupt_core(core);
}
}
/*
Dispatch any pending interrupts, lowest first.
Cease if an interrupt causes core change.
*/
static inline void dispatch_interrupts(Core* core)
{
assert(cpu_core_id==core->id);
while(1) {
Interrupt irq;
if(! intr_fetch_lowest(core, &irq)) break;
assert(0 <= irq && irq < maximum_interrupt_no);
#if defined(CORE_STATISTICS)
core->irq_delivered[irq]++;
#endif
interrupt_handler* handler = core->intvec[irq];
if(handler != NULL) handler();
/*
Note: after a successful dispatch, we may not
be running on the core any more (!), if the
dispatch action has been scheduled...
*/
if(cpu_core_id != core->id) {
//if(core->intr_pending) interrupt_core(core);
break;
}
}
}
/*
This is the signal handler for core threads, to handle interrupts.
*/
static void sigusr1_handler(int signo, siginfo_t* si, void* ctx)
{
Core* core = & CORE[si->si_value.sival_int];
#if defined(CORE_STATISTICS)
core->irq_count++;
#endif
dispatch_interrupts(core);
}
/*
Peripherals
*/
/* Coarse clock */
static TimerDuration get_coarse_time()
{
struct timespec curtime;
CHECK(clock_gettime(CLOCK_REALTIME_COARSE, &curtime));
return curtime.tv_nsec / 1000ul + curtime.tv_sec*1000000ull;
}
/*
An io_device handles a file descriptor that is connected to some
'peripheral' in stream (byte-oriented) mode. The file descriptor must be
'select-able' (i.e. not a disk file) and support non-blocking mode.
Model outline:
A fd (e.g., a FIFO) is operated at two ends: the cores, via I/O *transfers*,
and the 'actual device' (e.g., a terminal), via I/O *operations*.
Each io_device is unidirectional: the fd is either only read or only written to,
by this program (bidirectional fds, such as sockets, can be handled by a pair of
io_device objects).
An io_device is ready if I/O operations may succeed (as reported by select()).
A not-ready device is made ready when select() returns it as such.
A ready device is made not-ready on each failed attempt to do an I/O transfer.
When a not-ready device becomes ready, an interrupt is raised.
*/
typedef enum io_direction
{
IODIR_RX = 0,
IODIR_TX = 1
} io_direction;
/*
An io_device is a file descriptor from which we either read or write bytes.
*/
typedef struct io_device
{
int fd; /* file descriptor */
io_direction iodir; /* device direction */
Core* volatile int_core; /* core to receive interrupts */
volatile int ready; /* ready flag */
TimerDuration last_int; /* used by PIC for timeouts */
} io_device;
/*
Determine device readiness without blocking
*/
static int io_device_ready(int fd, io_direction dir) {
struct pollfd pfd;
pfd.fd = fd;
int evt = (dir==IODIR_RX) ? POLLIN : POLLOUT;
pfd.events = evt;
CHECK(poll(&pfd, 1, 0));
return (pfd.revents & evt) ? 1 : 0;
}
/*
Check that the a device is connected and in a good state.
*/
static int io_device_check(io_device* dev)
{
struct pollfd fds = { .fd=dev->fd, .events=POLLIN };
int rc;
do {
rc=poll(&fds, 1, 0);
} while( rc == -1 && errno==EINTR );
CHECK(rc);
rc = (fds.revents & (POLLHUP|POLLERR))==0;
assert(rc);
return rc;
}
/*
Initialize device
*/
static void io_device_init(io_device* this, int fd, io_direction iodir)
{
this->fd = fd;
this->iodir = iodir;
this->int_core = &CORE[0];
this->ready = io_device_ready(fd, iodir);
this->last_int = get_coarse_time();
/* Set file descriptor to non-blocking */
CHECK(fcntl(fd, F_SETFL, O_NONBLOCK));
}
/*
Destroy device
*/
static int io_device_destroy(io_device* this)
{
int rc;
while((rc = close(this->fd))==-1 && errno==EINTR);
if(rc==-1) perror("io_device_destroy: ");
return rc;
}
static int io_device_read(io_device* this, char* ptr)
{
assert(this->iodir == IODIR_RX);
int rc;
while((rc=read(this->fd, ptr, 1))==-1 && errno == EINTR);
int ok = rc==0 || rc==1 || (rc==-1 && (errno==EAGAIN || errno==EWOULDBLOCK));
if(!ok) perror("io_device_read:");
assert(ok);
if(rc!=1 && this->ready) {
this->ready = 0;
interrupt_pic_thread();
}
return rc==1;
}
static int io_device_write(io_device* this, char value)
{
assert(this->iodir == IODIR_TX);
/* Try to write */
int rc;
while((rc = write(this->fd, &value, 1))==-1 && errno == EINTR);
int ok = rc==1 || (rc==-1 && (errno == EAGAIN || errno==EWOULDBLOCK || errno == EPIPE));
if(! ok) perror("io_device_write:");
assert(ok);
if(rc!=1 && this->ready) {
this->ready = 0;
interrupt_pic_thread();
}
return rc==1;
}
/*
A terminal encapsulates two io_devices: a console and a keyboard
*/
typedef struct terminal
{
io_device con, kbd; /* fds for terminal fifos */
} terminal;
/* The terminal table */
static terminal TERM[MAX_TERMINALS];
/* Current number of terminals */
static uint nterm = 0;
/*
Init the devices for this terminal
*/
static void terminal_init(terminal* this, int fdin, int fdout)
{
io_device_init(& this->kbd, fdin, IODIR_RX);
io_device_init(& this->con, fdout, IODIR_TX);
}
/*
Destroy the terminal devices
*/
static int terminal_destroy(terminal* this)
{
return io_device_destroy(& this->con)
| io_device_destroy(& this->kbd);
}
/*
The PIC daemon dispatches interrupts to core threads,
by calling raise_interrupt().
Interrupts sent include
(a) ALARM, when the core timer expires
(b) SERIAL_RX_READY & SERIAL_TX_READY, when some
io_device becomes ready.
Implementation:
- Use Linux signal file descriptors to receive signals. Currently,
two signals are used:
* SIGUSR1 is sent by io_device to signify that some io_device is NOT READY.
Otherwise it is discarded. The signal simply wakes up the PIC_daemon thread.
This however causes the PIC loop to include the devices to the ones monitored.
* SIGALRM is sent to indicate that some core timer has expired. This
results to an interrupt on the core.
- Monitor these fds together with the fds of the terminals.
- At each loop dispatch interrupts as needed:
* ALARM interrupts to those cores whose timer has expired
* SERIAL_RX/TX_READY to those cores handling the interrupts of
an io_device which is now READY.
*/
/******************************
Helpers for signal fds
******************************/
static inline int read_signalfd(int sfd, struct signalfd_siginfo* sfdinfo)
{
int rc = read(sfd, sfdinfo, sizeof(struct signalfd_siginfo));
assert( (rc==-1 || rc==sizeof(struct signalfd_siginfo))
&& (rc!=-1 || errno==EAGAIN || errno==EWOULDBLOCK) );
return rc;
}
static inline void drain_signalfd(int sfd)
{
struct signalfd_siginfo sfdinfo;
while(read_signalfd(sfd, &sfdinfo)!=-1);
}
static int open_signalfd(sigset_t* set)
{
int fd = signalfd(-1, set, SFD_NONBLOCK);
CHECK(fd);
return fd;
}
static void close_signalfd(int sfd)
{
drain_signalfd(sfd);
CHECK(close(sfd));
}
/********************************
PIC loop state and helpers
********************************/
typedef struct pic_selector
{
fd_set fds[2];
int maxfd;
TimerDuration system_clock;
} pic_selector;
static void pic_selector_reset(pic_selector* ps)
{
FD_ZERO(& ps->fds[0]);
FD_ZERO(& ps->fds[1]);
ps->maxfd = 0;
}
static inline void pic_add_fd(pic_selector* ps, io_direction dir, int fd)
{
FD_SET(fd, ps->fds + dir);
if(fd >= ps->maxfd) ps->maxfd = fd+1;
}
static int pic_select(pic_selector* ps)
{
/* select will sleep for about SLOW_HZ usec (half the system_clock res.) */
struct timeval sleeptime = { .tv_sec=0, .tv_usec = SERIAL_TIMEOUT };
int selcode = select(ps->maxfd, &ps->fds[IODIR_RX], &ps->fds[IODIR_TX], NULL, &sleeptime);
if(selcode == -1) {
/* An error is likely EINTR */
if(errno != EINTR) perror("PIC_loops: "); else perror("PIC_select:");
} else {
/* update system clock */
ps->system_clock = get_coarse_time();
}
return selcode;
}
static inline int pic_is_ready(pic_selector* ps, io_direction dir, int fd)
{
return FD_ISSET(fd, & ps->fds[dir]);
}
static inline void pic_add_io_device(pic_selector* ps, io_device* dev)
{
if(! dev->ready) pic_add_fd(ps, dev->iodir, dev->fd);
}
static inline void pic_add_terminal(pic_selector* ps, terminal* term)
{
/* First check that terminal is connected, without blocking.
This is done by polling for errors on the kbd device. */
if(io_device_check(& term->kbd)) {
pic_add_io_device(ps, & term->kbd);
pic_add_io_device(ps, & term->con);
}
}
static void term_dev_raise_if_ready(io_device* dev, pic_selector* ps)
{
if( pic_is_ready(ps, dev->iodir, dev->fd)
|| (ps->system_clock - dev->last_int) > SERIAL_TIMEOUT
)
{
dev->ready = 1;
dev->last_int = ps->system_clock;
Core* core = (Core*) dev->int_core;
switch(dev->iodir) {
case IODIR_RX:
raise_interrupt(core, SERIAL_RX_READY); break;
case IODIR_TX:
raise_interrupt(core, SERIAL_TX_READY); break;
}
}
}
static void PIC_daemon(void)
{
/* Change the thread name */
char oldname[16];
CHECKRC(pthread_getname_np(pthread_self(), oldname, 16));
CHECKRC(pthread_setname_np(pthread_self(), "tinyos_vm"));
/* Open signal queues */
int sigusr1fd = open_signalfd(&sigusr1_set);
int sigalrmfd = open_signalfd(&sigalrm_set);
/* Set signal mask to block the signals monitored by signalfd */
sigset_t saved_mask;
CHECKRC(pthread_sigmask(SIG_BLOCK, &signalfd_set, &saved_mask));
/* sync with all cores */
pthread_barrier_wait(& system_barrier);
/* The PIC multiplexing loop */
while(PIC_active) {
pic_selector ps;
pic_selector_reset(&ps);
for(uint i=0; i<nterm; i++)
pic_add_terminal(&ps, & TERM[i]);
pic_add_fd(&ps, IODIR_RX, sigalrmfd);
pic_add_fd(&ps, IODIR_RX, sigusr1fd);
if(pic_select(&ps) == -1)
continue;
PIC_loops++ ;
if( pic_is_ready(&ps, IODIR_RX, sigalrmfd)!=-1 ) {
struct signalfd_siginfo sfdinfo;
while(read_signalfd(sigalrmfd, &sfdinfo) != -1) {
Core* core = & CORE[sfdinfo.ssi_int];
raise_interrupt(core, ALARM);
}
}
if( pic_is_ready(&ps, IODIR_RX, sigusr1fd)!=-1 ) {
drain_signalfd(sigusr1fd);
}
for(uint i=0; i<nterm; i++) {
terminal* term = & TERM[i];
term_dev_raise_if_ready(& term->con, &ps);
term_dev_raise_if_ready(& term->kbd, &ps);
}
}
/* sync with all cores */
pthread_barrier_wait(& system_barrier);
/* Close signal fds */
close_signalfd(sigusr1fd);
close_signalfd(sigalrmfd);
/* Restore sigmask */
CHECKRC(pthread_sigmask(SIG_SETMASK, &saved_mask, NULL));
/* Reset name */
CHECKRC(pthread_setname_np(pthread_self(), oldname));
}
/*****************************************
Public API
*****************************************/
/*
VM boot functions.
*/
int vm_config_terminals(vm_config* vmc, uint serialno, int nowait)
{
if(serialno>MAX_TERMINALS) return -1;
/* If nowait is requested, we will open fifos with O_NONBLOCK.
This will fail (for serial_out) if the fifos are not already open
on the terminal emulator side */
//int BLOCK = nowait ? O_NONBLOCK : 0;
int BLOCK = O_NONBLOCK;
/* Used to store the fifo fds temporarily */
unsigned int fdno = 0;
int fds[2*MAX_TERMINALS];
/* Helper to open a FIFO */
int open_fifo(const char* name, uint no, int flags) {
char fname[16];
snprintf(fname,16,"%s%u", name, no);
int fd = open(fname, flags);
if(fd==-1) {
for(uint i=0; i<fdno; i++) close(fds[i]);
return 0;
} else {
fds[fdno++] = fd;
return 1;
}
}
for(uint i=0; i<serialno; i++) {
/* Open serial port. Order is important!!! */
//if(! open_fifo("con", i, O_WRONLY | BLOCK)) return -1;
//if(! open_fifo("kbd", i, O_RDONLY | BLOCK)) return -1;
if(! open_fifo("con", i, O_RDWR | BLOCK)) return -1;
if(! open_fifo("kbd", i, O_RDWR | BLOCK)) return -1;
}
/* Everything was successful, initialize vmc */
vmc->serialno = serialno;
for(uint i=0; i<serialno; i++) {
vmc->serial_out[i] = fds[2*i];
vmc->serial_in[i] = fds[2*i+1];
}
return 0;
}
void vm_configure(vm_config* vmc, interrupt_handler bootfunc, uint cores, uint serialno)
{
vmc->bootfunc = bootfunc;
vmc->cores = cores;
CHECK(vm_config_terminals(vmc, serialno, 0));
}
void vm_boot(interrupt_handler bootfunc, uint cores, uint serialno)
{
vm_config VMC;
vm_configure(&VMC, bootfunc, cores, serialno);
vm_run(&VMC);
}
void vm_run(vm_config* vmc)
{
CHECK_CONDITION(vmc->cores > 0 && vmc->cores <= MAX_CORES);
CHECK_CONDITION(ncores==0);
CHECK_CONDITION(vmc->serialno <= MAX_TERMINALS);
/* This is called only once in the life of the process. */
CHECKRC(pthread_once(&init_control, initialize));
/* Install signal handler for SIGUSR1 */
CHECK(sigaction(SIGUSR1, &USR1_sigaction, &USR1_saved_sigaction));
/* Set pic_active to 1 */
PIC_thread = pthread_self();
PIC_active = 1;
/* Initialize terminals */
nterm = vmc->serialno;
for(uint i=0; i<nterm; i++)
terminal_init(& TERM[i], vmc->serial_in[i], vmc->serial_out[i]);
/* Init the cores */
ncores = vmc->cores;
/* Initialize the barriers */
pthread_barrier_init(& system_barrier, NULL, ncores+1);
pthread_barrier_init(& core_barrier, NULL, ncores);
/* Initialize the halted vector */
halt_vector = 0;
/* Launch the core threads */
for(uint c=0; c < ncores; c++) {
/* Initialize Core */
CORE[c].bootfunc = vmc->bootfunc;
CORE[c].id = c;
#if defined(CORE_STATISTICS)
/* Initialize Core statistics */
CORE[c].irq_count = 0;
for(uint intno=0; intno<maximum_interrupt_no;intno++) {
CORE[c].irq_delivered[intno] = 0;
CORE[c].irq_raised[intno] = 0;
CORE[c].hlt_count = 0;
CORE[c].rst_count = 0;
CORE[c].hlt_time = 0;
CORE[c].run_time = get_coarse_time();
}
#endif
/* Create the core thread */
CHECKRC(pthread_create(& CORE[c].thread, NULL, core_thread, &CORE[c]));
char thread_name[16];
CHECK(snprintf(thread_name,16,"core-%d",c));
CHECKRC(pthread_setname_np(CORE[c].thread, thread_name));
}
/* Initialize PIC statistics */
PIC_loops = 0;
/* Run the interrupt controller daemon on this thread */
PIC_daemon();
/* Wait for core threads to finish */
for(uint c=0; c<ncores; c++) {
CHECKRC(pthread_join(CORE[c].thread, NULL));
#if defined(CORE_STATISTICS)
CORE[c].run_time = get_coarse_time() - CORE[c].run_time;
#endif
}
/* Delete the Core table */
ncores = 0;
/* Destroy the core barrier */
pthread_barrier_destroy(& system_barrier);
pthread_barrier_destroy(& core_barrier);
/* Finalize terminals */
for(uint i=0; i<nterm; i++)
CHECK(terminal_destroy(& TERM[i]));
nterm = 0;
/* Restore signal mask before VM execution */
CHECK(sigaction(SIGUSR1, &USR1_saved_sigaction, NULL));
/* print statistics */
#if defined(CORE_STATISTICS)
fprintf(stderr,"PIC loops: %lu \n", PIC_loops);
double total_util = 0.0;
for(uint c=0; c < vmc->cores; c++) {
fprintf(stderr,"Core %3d: irq_count=%6tu. deliv(raised): ",
c, CORE[c].irq_count);
for(uint i=0;i<maximum_interrupt_no;i++)
fprintf(stderr," %tu(%tu)",CORE[c].irq_delivered[i], CORE[c].irq_raised[i]);
fprintf(stderr, " hlt(rst): %tu(%tu)", CORE[c].hlt_count, CORE[c].rst_count);
fprintf(stderr, " hltt: %2.3lf", 1E-6*CORE[c].hlt_time);
double util = 100.0 - 100.0 * CORE[c].hlt_time / (double)CORE[c].run_time ;
total_util += util;
fprintf(stderr, " util %%: %3.2lf", util);
fprintf(stderr,"\n");
}
fprintf(stderr,"Avg(util)=%6.2lf\n", total_util);
#endif
}
/*
CPU functions.
*/
uint cpu_cores()
{
return ncores;
}
void cpu_core_halt()
{
CHECKRC(pthread_sigmask(SIG_BLOCK, &sigusr1_set, NULL));