-
Notifications
You must be signed in to change notification settings - Fork 31
/
main.c
224 lines (183 loc) · 6.25 KB
/
main.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
/* -*- Mode: C; Tab-Width: 4 -*- */
#include "std.h"
#include "VLM_configuration.h"
#include "life_prototypes.h"
#include "world_tools.h"
#include "utilities.h"
#include "SystemComm.h"
#ifdef _C_EMULATOR_
#include "emulator.h"
#include "memory.h"
#else
#include "ivoryrep.h"
#endif
#include "spy.h"
#ifdef OS_OSF
#include <machine/fpu.h>
#else
#include <fenv.h>
#endif
#define MBToWords(MB) ((MB * 1024 * 1024) + 4)/5
#define WordsToMB(words) ((5 * words) + (1024 * 1024) - 1)/(1024 * 1024)
Boolean Trace = FALSE;
Boolean EnableIDS = FALSE;
Boolean TestFunction = FALSE;
static pthread_key_t mainThread;
static void MaybeTerminateVLM (int signal)
{
#ifdef OS_LINUX
char *answer = NULL;
size_t answerSize = 0, *answerSize_p = &answerSize;
ssize_t nRead;
#else
char answer[BUFSIZ];
#endif
if (NULL == pthread_getspecific (mainThread))
return;
if (EmbCommAreaPtr->guestStatus > StartedGuestStatus)
{
if (RunningGuestStatus == EmbCommAreaPtr->guestStatus)
fprintf (stderr, "\nLisp is running!\n\n");
else
fprintf (stderr, "\nLisp was running!\n\n");
fprintf (stderr, "If you exit, the current state of Lisp will be lost.\n");
fprintf (stderr, "All information in its memory image (e.g., any modified editor\n");
fprintf (stderr, "buffers) will be irretrievably lost. Further, Lisp will abandon\n");
fprintf (stderr, "any tasks it is performing for its clients.\n\n");
fprintf (stderr, "Do you still wish to exit? (yes or no) ");
fflush (stderr);
while (TRUE)
{
#ifdef OS_LINUX
nRead = getline (&answer, answerSize_p, stdin);
if (nRead < 0)
vpunt (NULL, "Unexpected EOF on standard input");
answer[nRead - 1] = '\0';
#else
if (NULL == gets (answer))
vpunt (NULL, "Unexpected EOF on standard input");
#endif
if (0 == strcmp (answer, "yes"))
break;
else if (0 == strcmp (answer, "no"))
return;
else
{
fprintf (stderr, "Please answer 'yes' or 'no'. ");
fflush (stderr);
}
}
}
TerminateTracing ();
TerminateSpy ();
TerminateLifeSupport ();
_exit (EXIT_SUCCESS);
}
int main (int argc, char** argv)
{
VLMConfig config;
struct sigaction sigAction;
Integer worldImageSize, worldImageMB;
char* message;
int reason;
BuildConfiguration (&config, argc, argv);
#ifdef GENERA
EnableIDS = config.enableIDS;
#endif
TestFunction = config.testFunction;
Trace = config.tracing.tracePOST;
InitializeIvoryProcessor (MapVirtualAddressData (0), MapVirtualAddressTag (0));
Trace = config.tracing.traceP;
if (Trace) InitializeTracing (config.tracing.bufferSize, config.tracing.startPC,
config.tracing.stopPC, config.tracing.outputFile);
InitializeLifeSupport (&config);
#if defined(OS_OSF)
ieee_set_fp_control(IEEE_TRAP_ENABLE_INV +
IEEE_TRAP_ENABLE_DZE +
IEEE_TRAP_ENABLE_OVF +
IEEE_TRAP_ENABLE_UNF +
IEEE_TRAP_ENABLE_INE);
#elif defined(OS_LINUX)
#ifdef FE_NOMASK_ENV
fesetenv (FE_NOMASK_ENV);
#else
feenableexcept (FE_INEXACT | FE_DIVBYZERO | FE_UNDERFLOW | FE_OVERFLOW | FE_INVALID);
#endif
#elif defined(OS_DARWIN)
/* TBD: -- Need an equivalent to: fesetenv (FE_NOMASK_ENV) */
#endif
if (pthread_key_create (&mainThread, NULL))
vpunt (NULL, "Unable to establish per-thread data.");
pthread_setspecific (mainThread, (void*) TRUE);
sigAction.sa_handler = (sa_handler_t)MaybeTerminateVLM;
sigemptyset (&sigAction.sa_mask);
sigAction.sa_flags = 0;
if (sigaction (SIGINT, &sigAction, NULL))
vpunt (NULL, "Unable to establish SIGINT handler.");
if (sigaction (SIGTERM, &sigAction, NULL))
vpunt (NULL, "Unable to establish SIGTERM handler.");
if (sigaction (SIGHUP, &sigAction, NULL))
vpunt (NULL, "Unable to establish SIGHUP handler.");
if (sigaction (SIGQUIT, &sigAction, NULL))
vpunt (NULL, "Unable to establish SIGQUIT handler.");
#ifdef IVERIFY
EnsureVirtualAddressRange (0xF8000000L, 0x00100000L, FALSE);
#else
worldImageSize = LoadWorld (&config);
#ifdef GENERA
LoadVLMDebugger (&config);
worldImageMB = WordsToMB (worldImageSize);
if (worldImageMB > config.virtualMemory)
vpunt (NULL, "World file %s won't fit within the requested virtual memory (%dMB)",
config.worldPath, config.virtualMemory);
if ((2 * worldImageMB) > config.virtualMemory)
vwarn (NULL, "Only %dMB of virtual memory unused after loading world file %s\n",
(config.virtualMemory - worldImageMB), config.worldPath);
VirtualMemoryWrite (SystemCommSlotAddress (enableSysoutAtColdBoot),
EnableIDS ? processor->taddress : processor->niladdress);
EmbCommAreaPtr->virtualMemorySize = MBToWords (config.virtualMemory);
EmbCommAreaPtr->worldImageSize = worldImageSize;
#endif
#endif
if (config.enableSpy) InitializeSpy (TRUE, config.diagnosticIPAddress.s_addr);
#ifdef AUTOSTART
if (!IvoryProcessorSystemStartup (TRUE))
vpunt (NULL, "Unable to start the VLM.");
#endif
if (config.enableSpy) ReleaseSpyLock ();
while (config.enableSpy ? TRUE : Runningp())
{
reason = InstructionSequencer ();
if (reason)
{
switch (reason)
{
case HaltReason_IllInstn:
message = "Unimplemented instruction";
break;
case HaltReason_Halted:
message = NULL;
break;
case HaltReason_SpyCalled:
message = NULL;
break;
case HaltReason_FatalStackOverflow:
message = "Stack overflow while not in emulator mode";
break;
case HaltReason_IllegalTrapVector:
message = "Illegal trap vector contents";
break;
default:
message = "Halted for unknown reason";
}
if (message != NULL)
vwarn (NULL, "%s at PC %08x (%s)", message, processor->epc >> 1,
(processor->epc & 1) ? "Odd" : "Even");
}
#ifndef IVERIFY
if (HaltReason_Halted == reason)
break;
#endif
}
exit (EXIT_SUCCESS);
}