console.c
19.4 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
/*
* Copyright (C) 1996-1998 by the Board of Trustees
* of Leland Stanford Junior University.
*
* This file is part of the SimOS distribution.
* See LICENSE file for terms of the license.
*
*/
/*****************************************************************
* simconsole.c
*
* Very simple console device emulation.
* Dan Teodosiu, 07/96
*
*****************************************************************/
#include <sys/types.h>
#include <sys/termios.h>
#include <sys/time.h>
#include <sys/signal.h>
#ifndef linux
#include <sys/sysinfo.h>
#include <poll.h>
#endif
#include <sys/times.h>
#include <sys/file.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/mman.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <setjmp.h>
#include <string.h>
#include <memory.h>
#include <ctype.h>
#include <math.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <netdb.h>
#include "syslimits.h"
#include "simtypes.h"
#include "sim_error.h"
#include "sim.h"
#include "expect.h"
#include "checkpoint.h"
#include "console.h"
#include "machine_params.h"
/*
* General remarks:
* For every console, we have two (logical) interrupt sources: tx and rx
* interrupts. These two are OR'ed together inside the device.
*/
typedef struct Sim_Console {
int inited; /* keeps track of whether this console was inited */
/* interrupt status/enable */
int intr_status;
int intr_enable;
void (*int_f)(int n, int on); /* interrupt function */
/* sconsole connection management */
int in_fd; /* note: iface 0 uses stdin/stdout */
int out_fd;
int accepter_fd; /* socket on which we do the accept call */
int accepter_port;
int has_connected;
int warned; /* failed connection has been reported to error log */
} Sim_Console;
/* check whether an int is pending */
#define INT_PENDING(sts, enb) (((sts) & (enb)) != 0)
/* check whether the intr line has transitioned */
#define INT_TRANSITION(old_sts, old_enb, new_sts, new_enb) \
(INT_PENDING(old_sts, old_enb) != INT_PENDING(new_sts, new_enb))
static Sim_Console cs[MAX_CONSOLES]; /* console data */
static int ncs; /* number of consoles */
int ConsolePort = 3456; /* some old port */
int SlaveConsoleTimeOut = 600; /* 10 minutes */
extern CptVersion cptVersion;
/*****************************************************************
* initialization and sconsole session management
*****************************************************************/
/* Make a socket for a slave console (so someone can use 'sconsole' to
* connect to it).
*/
static void
CreateConsoleSocket(int n)
{
struct sockaddr_in sockaddr;
int tmp;
int sfd;
sfd = socket (PF_INET, SOCK_STREAM, 0);
if (sfd < 0) {
perror("Can't open console socket\n");
cs[n].accepter_fd = -1;
cs[n].accepter_port = -1;
return;
}
/* Allow rapid reuse of this port. */
tmp = 1;
if (setsockopt (sfd, SOL_SOCKET, SO_REUSEADDR, (char *)&tmp, sizeof(tmp))
< 0) {
perror("CreateConsoleSocket setsockopt SO_REUSEADDR");
/* Not fatal */
}
sockaddr.sin_family = PF_INET;
sockaddr.sin_port = htons(ConsolePort + n);
sockaddr.sin_addr.s_addr = INADDR_ANY;
while (bind (sfd, (struct sockaddr *)&sockaddr, sizeof (sockaddr)) || listen (sfd, 1)) {
perror("CreateConsoleSocket - Can't bind address");
Sim_Warning("Console port %d inuse ID %d PID %d\n",
ConsolePort+n, n, getpid());
ConsolePort++;
sockaddr.sin_port = htons(ConsolePort + n);
}
CPUWarning("Console num %d tcp port=%d\n", n, sockaddr.sin_port);
cs[n].accepter_fd = sfd;
cs[n].accepter_port = sockaddr.sin_port;
}
/* Open a connection. This is synchronous and blocks until the sconsole
* connection arrives.
*/
static void
ConnectSlaveConsole(int n)
{
struct sockaddr_in sockaddr;
int tmp;
int sock_fd;
int retval;
char buf[256];
tmp = sizeof (sockaddr);
sock_fd = accept (cs[n].accepter_fd, (struct sockaddr *)&sockaddr, &tmp);
if (sock_fd == -1) {
perror( "ConnectSlaveConsole - Accept failed");
return;
}
/* Tell TCP not to delay small packets. This greatly speeds up
interactive response. */
tmp = 1;
setsockopt (sock_fd, IPPROTO_TCP, TCP_NODELAY, (char *)&tmp, sizeof(tmp));
cs[n].in_fd = sock_fd;
cs[n].out_fd = sock_fd;
cs[n].has_connected = 1;
sprintf(buf, "\n==== SimOS slave console: Console %d ====\r\n", n);
retval = write(cs[n].out_fd, buf, strlen(buf));
if (retval < 0) {
perror( "ConnectSlaveConsole - Initial output write failed\n");
} else {
Sim_Warning("ConnectSlaveConsole: console %d connected\n", n);
}
}
/*****************************************************************
* ConfigureTerm turns off all character processing by the host OS
* so the launched OS can control it.
*
* We ignore anything except stdin; the sconsole program runs this
* same code on the ttys for the slave consoles before connecting.
*****************************************************************/
static void
ConfigureTerm(int fd)
{
struct termios ios;
if ((fd == 0) && isatty(0)) {
if (tcgetattr(fd, &ios) < 0) {
perror( "tcgetattr\n");
}
ios.c_iflag &= ~(ISTRIP|ICRNL|IGNCR|ICRNL|IXOFF|IXON);
ios.c_oflag &= ~OPOST;
ios.c_lflag &= ~(ISIG|ICANON|ECHO);
ios.c_cc[VMIN] = 1;
ios.c_cc[VTIME] = 0;
if (tcsetattr(fd, TCSANOW, &ios) < 0) {
perror( "tcsetattr\n");
}
}
}
/*****************************************************************
* Initialize console.
*****************************************************************/
static void
InitConsole(int n, void (*int_f)(int n, int on))
{
SIMASSERT(!cs[n].inited);
cs[n].inited = 1;
/* note: intr_status and intr_enable will normally be 0, since
* cs is statically allocated. When restoring from a checkpoint,
* these fields will be set, so don't touch them here.
*/
cs[n].int_f = int_f; /* interrupt function */
if (n == 0) {
/* console 0 gets special treatment since it uses stdin/stdout */
cs[n].in_fd = 0;
cs[n].out_fd = 1;
cs[n].has_connected = 1;
cs[n].accepter_fd = -1;
cs[n].accepter_port = 0;
} else {
CreateConsoleSocket(n);
cs[n].has_connected = 0;
cs[n].in_fd = cs[n].accepter_fd;
cs[n].out_fd = -1;
}
cs[n].warned = 0;
}
/*****************************************************************
* Wait for all slave consoles to connect or to time out.
*****************************************************************/
static void
ConsoleWaitLoop(int n)
{
#ifdef linux
/* Only have one console for linux runs for now */
return;
#else
int delay = SlaveConsoleTimeOut;
int connected = 0;
int needed = n-1;
int i;
struct pollfd fds[MAX_CONSOLES];
Sim_Warning("This configuration calls for %d slave consoles.\n", needed);
Sim_Warning("Use 'sconsole' to connect. All sessions must start before simulation begins.\n");
Sim_Warning("Any slave consoles not connected within %d seconds will not be usable during this run.\n",
delay);
Sim_Warning("Waiting for connections...\n");
fds[0].fd = -1;
for (i=1; i<n; i++) {
fds[i].fd = cs[i].accepter_fd;
fds[i].events = POLLIN;
fds[i].revents = 0;
}
for (; connected < needed && delay > 0; delay--) {
sleep(1);
if (poll(fds, n, 0) > 0) {
for (i=1; i<n; i++) {
if (fds[i].revents & POLLIN) {
ConnectSlaveConsole(i);
fds[i].fd = -1; /* ignore on future polls */
fds[i].revents = 0;
connected++;
}
}
}
}
if (connected < needed) {
Sim_Warning("\nConsole wait timed out! %d consoles bitbucketed.\n",
needed - connected);
} else {
Sim_Warning("\nAll slave consoles established.\n");
}
#endif
}
/*****************************************************************
* Initialize all consoles
*****************************************************************/
void
sim_console_init(int n, void (*int_f)(int n, int on))
{
int i;
SIMASSERT(n > 0 && n <= MAX_CONSOLES);
ncs = n;
for (i = 0; i < n; i++) {
InitConsole(i, int_f);
}
if (n > 1) {
ConsoleWaitLoop(n);
}
ConfigureTerm(0); /* need to set line discipline for console 0 */
}
/*****************************************************************
* Poll all console inputs. Should be called periodically.
*****************************************************************/
static struct {
int hasChar;
char c;
} simconsoleSavedChar[MAX_CONSOLES];
#ifdef linux
/* Linux version of console polling */
void
sim_console_poll(void)
{
fd_set readfds;
int i;
int n;
int old_status;
int max_fd;
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 0;
FD_ZERO(&readfds);
max_fd = 0;
for (i = 0; i < ncs; i++) {
if (cs[i].has_connected != 0) {
FD_SET(cs[i].in_fd, &readfds);
if (cs[i].in_fd >= max_fd)
max_fd = cs[i].in_fd;
}
}
n = select(max_fd+1, &readfds, NULL, NULL, &tv);
if (n) {
for(i = 0; i < ncs; i++) {
if (FD_ISSET(cs[i].in_fd, &readfds)) {
/* post interrupt */
old_status = cs[i].intr_status;
cs[i].intr_status |= CONS_INT_RX;
if (INT_TRANSITION(old_status, cs[i].intr_enable,
cs[i].intr_status, cs[i].intr_enable) &&
cs[i].int_f)
cs[i].int_f(i, 1); /* 0->1 */
}
}
}
}
#else
void
sim_console_poll(void)
{
static struct pollfd fds[MAX_CONSOLES]; /* must be 0-inited */
int i;
int old_status;
for (i = 0; i < ncs; i++) {
if (cs[i].has_connected != 0) {
fds[i].fd = cs[i].in_fd;
fds[i].events = POLLRDNORM;
fds[i].revents = 0;
}
}
poll(fds, ncs, 0);
for (i = 0; i < ncs; i++) {
SIM_DEBUG(('i', "sim_console_poll console %d fd: 0x%x events: 0x%x revents: 0x%x\n",
i, fds[i].fd, fds[i].events, fds[i].revents));
}
for(i = 0; i < ncs; i++)
if (fds[i].revents & POLLRDNORM) {
#if defined(__alpha)
if (!simconsoleSavedChar[i].hasChar) {
char c;
int ch = read(cs[i].in_fd, &c, 1);
if (ch <= 0) {
continue;
} else {
simconsoleSavedChar[i].hasChar = 1;
simconsoleSavedChar[i].c = c;
}
}
#endif
/* post interrupt */
old_status = cs[i].intr_status;
cs[i].intr_status |= CONS_INT_RX;
if (INT_TRANSITION(old_status, cs[i].intr_enable,
cs[i].intr_status, cs[i].intr_enable) &&
cs[i].int_f)
cs[i].int_f(i, 1); /* 0->1 */
}
}
#endif /* non-linux version of console polling */
/* Character is available on this console -- raise RX interrupt.
* This functionality is used by expect.
*/
void
sim_console_has_char(int n)
{
int old_status;
old_status = cs[n].intr_status;
cs[n].intr_status |= CONS_INT_RX;
if (INT_TRANSITION(old_status, cs[n].intr_enable,
cs[n].intr_status, cs[n].intr_enable) &&
cs[n].int_f)
cs[n].int_f(n, 1); /* 0->1 */
}
/*
* Checkpointing support.
*/
void
sim_console_ckpt(CptDescriptor *cptd)
{
int i, machNo, console;
int intr_cpu;
if (cptVersion.ver == 4 && cptVersion.sub == 0) {
for (machNo = 0, console = 0; machNo < NUM_MACHINES; machNo++) {
for (i = 0; i < NUM_CONSOLES(machNo); i++, console++) {
Simcpt_CptInt(cptd, "IntrStatus", machNo, i,
&(cs[console].intr_status));
Simcpt_CptInt(cptd, "IntrState", machNo, i,
&(cs[console].intr_enable));
/* note: intr_cpu is not used, but for compatibility we'll checkpoint it.
*/
intr_cpu = 0;
Simcpt_CptInt(cptd, "IntrCPU", machNo, i, &intr_cpu);
}
}
} else if (cptVersion.ver == 3) {
Simcpt_CptParamInt(cptd, "NumConsoles", NO_INDEX, NO_INDEX,
&(NUM_CONSOLES(0)));
for (i = 0; i < ncs; i++) {
Simcpt_CptInt(cptd, "IntrStatus", i, NO_INDEX, &(cs[i].intr_status));
Simcpt_CptInt(cptd, "IntrState", i, NO_INDEX, &(cs[i].intr_enable));
intr_cpu = 0;
Simcpt_CptInt(cptd, "IntrCPU", i, NO_INDEX, &intr_cpu);
}
} else {
ASSERT(0);
}
}
/*****************************************************************
* console i/o
*****************************************************************/
/* Console output.
* NOTE: this very rudimentary device generates a TX int as soon as
* a character is output, since it has unlimited TX buffer capacity.
*/
void
sim_console_out(int n, char c)
{
int oldval;
ASSERT(n >= 0 && n < ncs);
if (cs[n].out_fd >= 0) {
if (write(cs[n].out_fd, &c, 1) < 0) {
perror( "SimpromPutc: Can't write console\n");
}
} else {
/* if connection is not open, drop the character on the floor */
}
/* Expect hooks */
ExpectPutChar(n, c);
/* Make sure console output goes to the log! */
CPUPrint(":%d:%c\n", n, c);
oldval = cs[n].intr_status;
cs[n].intr_status |= CONS_INT_TX;
if (INT_TRANSITION(oldval, cs[n].intr_enable,
cs[n].intr_status, cs[n].intr_enable)
&& cs[n].int_f) {
cs[n].int_f(n, 1); /* 0->1 */
}
SIM_DEBUG(('i', "sim_console_out console %d char: 0x%x (%c) status: 0x%x\n",
n, c, c, cs[n].intr_status));
}
/* Simple console output used by Alpha firmware (not by the OS)
* - outputs the character to console n, and doesn't raise any interrupts
*/
void
sim_console_simpleIO(int n, char c)
{
int oldval;
ASSERT(n >= 0 && n < ncs);
if (cs[n].out_fd >= 0) {
if (write(cs[n].out_fd, &c, 1) < 0) {
perror( "SimpromPutc: Can't write console\n");
}
} else {
/* if connection is not open, drop the character on the floor */
}
/* Make sure console output goes to the log! */
CPUPrint(":%d:%c\n", n, c);
SIM_DEBUG(('i', "sim_console_simpleIO console %d char: 0x%x (%c)\n", n, c, c));
}
/* Console output.
* Uses sim_console_out to perform functionality similar to 'write'
*/
void
sim_console_write(int console, char *buf, unsigned int nbytes)
{
unsigned int count;
ASSERT(buf);
for (count = 0; count < nbytes; count++) {
sim_console_out(console, buf[count]);
}
}
/*****************************************************************
* Console input.
* Returns -1 if there is no character pending, otherwise returns
* the char. Calling this function clears the input int (if no further
* chars are pending).
*****************************************************************/
#ifdef linux
int
sim_console_in(int n)
{
int ch, num;
char c;
fd_set readfds;
int old_status;
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 0;
SIMASSERT(n >= 0 && n < ncs);
/* Expect hooks: expect characters take priority over user typed chars */
ch = ExpectGetChar(n);
if (ch) goto out;
if (simconsoleSavedChar[n].hasChar) {
simconsoleSavedChar[n].hasChar = 0;
return simconsoleSavedChar[n].c;
}
ch = -1; /* by default: no char */
FD_ZERO(&readfds);
FD_SET(cs[n].in_fd, &readfds);
num = select(cs[n].in_fd+1, &readfds, NULL, NULL, &tv);
if (num) {
/* read 1 character from console */
ch = read(cs[n].in_fd, &c, 1);
if (ch == 0) return -1;
FD_CLR(cs[n].in_fd, &readfds);
if (ch < 0) {
perror( "SimpromGetc: Can't read console\n");
return -1;
}
ch = c; /* will return the character */
}
out:
/* note: for efficiency's sake, if we have a char now, leave the
* interrupt line asserted in order to force a poll ASAP. The first
* unsuccessful poll then clears the receive interrupt.
*/
if (ch == -1) {
old_status = cs[n].intr_status;
cs[n].intr_status &= ~CONS_INT_RX;
if (INT_TRANSITION(old_status, cs[n].intr_enable,
cs[n].intr_status, cs[n].intr_enable) &&
cs[n].int_f)
cs[n].int_f(n, 0); /* 1->0 */
}
SIM_DEBUG(('i', "sim_console_in %d char: 0x%x status: 0x%x\n",
n, ch, cs[n].intr_status));
/* xxx there is a race here if a character was pending (we should raise
* the interrupt, actually). However, this is fixed when the poll process
* goes through its next polling round.
*/
return ch;
}
#else
int
sim_console_in(int n)
{
int ch;
char c;
struct pollfd fds[1];
int old_status;
SIMASSERT(n >= 0 && n < ncs);
/* Expect hooks: expect characters take priority over user typed chars */
ch = ExpectGetChar(n);
if (ch) goto out;
if (simconsoleSavedChar[n].hasChar) {
simconsoleSavedChar[n].hasChar = 0;
return simconsoleSavedChar[n].c;
}
ch = -1; /* by default: no char */
/* Poll for input char */
fds[0].fd = cs[n].in_fd;
fds[0].events = POLLRDNORM;
fds[0].revents = 0;
poll(fds, 1, 0);
if (!(fds[0].revents & POLLRDNORM)) goto out; /* no char */
/* read 1 character from console */
ch = read(cs[n].in_fd, &c, 1);
#if defined(__alpha)
if (!ch) {
/*
* this happens with /dev/null piped in
*/
ch = -1;
goto out;
}
#endif
if (ch < 0) {
perror( "SimpromGetc: Can't read console\n");
return -1;
}
ch = c; /* will return the character */
out:
/* note: for efficiency's sake, if we have a char now, leave the
* interrupt line asserted in order to force a poll ASAP. The first
* unsuccessful poll then clears the receive interrupt.
*/
if (ch == -1) {
old_status = cs[n].intr_status;
cs[n].intr_status &= ~CONS_INT_RX;
if (INT_TRANSITION(old_status, cs[n].intr_enable,
cs[n].intr_status, cs[n].intr_enable) &&
cs[n].int_f)
cs[n].int_f(n, 0); /* 1->0 */
}
SIM_DEBUG(('i', "sim_console_in %d char: 0x%x status: 0x%x\n",
n, ch, cs[n].intr_status));
/* xxx there is a race here if a character was pending (we should raise
* the interrupt, actually). However, this is fixed when the poll process
* goes through its next polling round.
*/
return ch;
}
#endif
/* Read the current interrupt status of this console.
* NOTE: calling this routine has the side effect of clearing any pending
* TX interrupt.
*/
int
sim_console_int_status(int n)
{
int old_status;
SIMASSERT(n >= 0 && n < ncs);
old_status = cs[n].intr_status;
SIM_DEBUG(('i', "sim_console_int_status %d status: 0x%x\n", n, old_status));
cs[n].intr_status &= ~CONS_INT_TX;
if (INT_TRANSITION(old_status, cs[n].intr_enable,
cs[n].intr_status, cs[n].intr_enable) &&
cs[n].int_f)
cs[n].int_f(n, 0); /* 1->0 */
return old_status;
}
/* ALPHA only. Check if it is obsolete now */
int sim_console_int_status_noSE(int n)
{
return cs[n].intr_status;
}
/* ALPHA only. Check if it is obsolete now */
int sim_console_clear_int(int n)
{
int old_status = cs[n].intr_status;
cs[n].intr_status = 0;
if (INT_TRANSITION(old_status, cs[n].intr_enable,
cs[n].intr_status, cs[n].intr_enable) &&
cs[n].int_f)
cs[n].int_f(n, 0); /* 1->0 */
return old_status;
}
/* Set the interrupt enable bits.
*/
void
sim_console_int_set(int n, int bits)
{
int old_enable;
ASSERT(n >= 0 && n < ncs);
ASSERT((bits & ~(CONS_INT_TX|CONS_INT_RX)) == 0);
old_enable = cs[n].intr_enable;
cs[n].intr_enable |= bits;
if (INT_TRANSITION(cs[n].intr_status, old_enable,
cs[n].intr_status, cs[n].intr_enable) &&
cs[n].int_f) {
if (INT_PENDING(cs[n].intr_status, cs[n].intr_enable))
cs[n].int_f(n, 1); /* 0->1 */
else
cs[n].int_f(n, 0); /* 1->0 */
}
}