-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cc
1537 lines (1414 loc) · 48.1 KB
/
main.cc
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
/////////////////////////////////////////////////////////////////////////
// $Id: main.cc 14204 2021-03-27 17:23:31Z vruppert $
/////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2001-2021 The Bochs Project
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#include "bochs.h"
#include "bxversion.h"
#include "param_names.h"
#include "cpu/cpu.h"
#include "iodev/iodev.h"
#include "iodev/hdimage/hdimage.h"
#if BX_NETWORKING
#include "iodev/network/netmod.h"
#endif
#if BX_SUPPORT_SOUNDLOW
#include "iodev/sound/soundmod.h"
#endif
#if BX_SUPPORT_PCIUSB
#include "iodev/usb/usb_common.h"
#endif
#ifdef HAVE_LOCALE_H
#include <locale.h>
#endif
#if BX_WITH_SDL || BX_WITH_SDL2
// since SDL redefines main() to SDL_main(), we must include SDL.h so that the
// C language prototype is found. Otherwise SDL_main() will get its name
// mangled and not match what the SDL library is expecting.
#include <SDL.h>
#if defined(macintosh)
// Work around a bug in SDL 1.2.4 on MacOS X, which redefines getenv to
// SDL_getenv, but then neglects to provide SDL_getenv. It happens
// because we are defining -Dmacintosh.
#undef getenv
#endif
#endif
#if BX_WITH_CARBON
#include <Carbon/Carbon.h>
#endif
extern "C" {
#include <signal.h>
}
#if BX_GUI_SIGHANDLER
bool bx_gui_sighandler = 0;
#endif
int bx_init_main(int argc, char *argv[]);
void bx_init_hardware(void);
void bx_plugin_ctrl_reset(bool init_done);
void bx_init_options(void);
void bx_init_bx_dbg(void);
static const char *divider = "========================================================================";
bx_startup_flags_t bx_startup_flags;
bool bx_user_quit;
Bit8u bx_cpu_count;
#if BX_SUPPORT_APIC
Bit32u apic_id_mask; // determinted by XAPIC option
bool simulate_xapic;
#endif
/* typedefs */
#define LOG_THIS genlog->
bx_pc_system_c bx_pc_system;
bx_debug_t bx_dbg;
typedef BX_CPU_C *BX_CPU_C_PTR;
#if BX_SUPPORT_SMP
// multiprocessor simulation, we need an array of cpus
BOCHSAPI BX_CPU_C_PTR *bx_cpu_array = NULL;
#else
// single processor simulation, so there's one of everything
BOCHSAPI BX_CPU_C bx_cpu;
#endif
BOCHSAPI BX_MEM_C bx_mem;
char *bochsrc_filename = NULL;
size_t bx_get_timestamp(char *buffer)
{
#if VER_SVNFLAG == 1
#ifdef __DATE__
#ifdef __TIME__
sprintf(buffer, "Compiled on %s at %s", __DATE__, __TIME__);
#else
sprintf(buffer, "Compiled on %s", __DATE__);
#endif
#else
buffer[0] = 0;
#endif
#else
// Releases use the timestamp from README file
sprintf(buffer, "Timestamp: %s", REL_TIMESTAMP);
#endif
return strlen(buffer);
}
void bx_print_header()
{
char buffer[128];
printf("%s\n", divider);
sprintf (buffer, "Bochs x86 Emulator %s\n", VERSION);
bx_center_print(stdout, buffer, 72);
if (REL_STRING[0]) {
sprintf(buffer, "%s\n", REL_STRING);
bx_center_print(stdout, buffer, 72);
if (bx_get_timestamp(buffer) > 0) {
bx_center_print(stdout, buffer, 72);
printf("\n");
}
}
printf("%s\n", divider);
}
#if BX_WITH_CARBON
/* Original code by Darrell Walisser - [email protected] */
static void setupWorkingDirectory(char *path)
{
char parentdir[MAXPATHLEN];
char *c;
strncpy (parentdir, path, MAXPATHLEN);
c = (char*) parentdir;
while (*c != '\0') /* go to end */
c++;
while (*c != '/') /* back up to parent */
c--;
*c = '\0'; /* cut off last part (binary name) */
/* chdir to the binary app's parent */
int n;
n = chdir (parentdir);
if (n) BX_PANIC(("failed to change dir to parent"));
/* chdir to the .app's parent */
n = chdir ("../../../");
if (n) BX_PANIC(("failed to change to ../../.."));
}
/* Panic button to display fatal errors.
Completely self contained, can't rely on carbon.cc being available */
static void carbonFatalDialog(const char *error, const char *exposition)
{
DialogRef alertDialog;
CFStringRef cfError;
CFStringRef cfExposition;
DialogItemIndex index;
AlertStdCFStringAlertParamRec alertParam = {0};
fprintf(stderr, "Entering carbonFatalDialog: %s\n", error);
// Init libraries
InitCursor();
// Assemble dialog
cfError = CFStringCreateWithCString(NULL, error, kCFStringEncodingASCII);
if(exposition != NULL)
{
cfExposition = CFStringCreateWithCString(NULL, exposition, kCFStringEncodingASCII);
}
else { cfExposition = NULL; }
alertParam.version = kStdCFStringAlertVersionOne;
alertParam.defaultText = CFSTR("Quit");
alertParam.position = kWindowDefaultPosition;
alertParam.defaultButton = kAlertStdAlertOKButton;
// Display Dialog
CreateStandardAlert(
kAlertStopAlert,
cfError,
cfExposition, /* can be NULL */
&alertParam, /* can be NULL */
&alertDialog);
RunStandardAlert(alertDialog, NULL, &index);
// Cleanup
CFRelease(cfError);
if(cfExposition != NULL) { CFRelease(cfExposition); }
}
#endif
#if BX_DEBUGGER
void print_tree(bx_param_c *node, int level, bool xml)
{
int i;
char tmpstr[BX_PATHNAME_LEN];
for (i=0; i<level; i++)
dbg_printf(" ");
if (node == NULL) {
dbg_printf("NULL pointer\n");
return;
}
if (xml)
dbg_printf("<%s>", node->get_name());
else
dbg_printf("%s = ", node->get_name());
switch (node->get_type()) {
case BXT_PARAM_NUM:
case BXT_PARAM_BOOL:
case BXT_PARAM_ENUM:
case BXT_PARAM_STRING:
node->dump_param(tmpstr, BX_PATHNAME_LEN, 1);
dbg_printf("%s", tmpstr);
break;
case BXT_LIST:
{
if (!xml) dbg_printf("{");
dbg_printf("\n");
bx_list_c *list = (bx_list_c*)node;
for (i=0; i < list->get_size(); i++) {
print_tree(list->get(i), level+1, xml);
}
for (i=0; i<level; i++)
dbg_printf(" ");
if (!xml) dbg_printf("}");
break;
}
case BXT_PARAM_DATA:
dbg_printf("'binary data size=%d'", ((bx_shadow_data_c*)node)->get_size());
break;
default:
dbg_printf("(unknown parameter type)");
}
if (xml) dbg_printf("</%s>", node->get_name());
dbg_printf("\n");
}
#endif
#if BX_ENABLE_STATISTICS
void print_statistics_tree(bx_param_c *node, int level)
{
for (int i=0; i<level; i++)
printf(" ");
if (node == NULL) {
printf("NULL pointer\n");
return;
}
switch (node->get_type()) {
case BXT_PARAM_NUM:
{
bx_param_num_c* param = (bx_param_num_c*) node;
printf("%s = " FMT_LL "d\n", node->get_name(), param->get64());
param->set(0); // clear the statistic
}
break;
case BXT_PARAM_BOOL:
BX_PANIC(("boolean statistics are not supported !"));
break;
case BXT_PARAM_ENUM:
BX_PANIC(("enum statistics are not supported !"));
break;
case BXT_PARAM_STRING:
BX_PANIC(("string statistics are not supported !"));
break;
case BXT_LIST:
{
bx_list_c *list = (bx_list_c*)node;
if (list->get_size() > 0) {
printf("%s = \n", node->get_name());
for (int i=0; i < list->get_size(); i++) {
print_statistics_tree(list->get(i), level+1);
}
}
break;
}
case BXT_PARAM_DATA:
BX_PANIC(("binary data statistics are not supported !"));
break;
default:
BX_PANIC(("%s (unknown parameter type)\n", node->get_name()));
break;
}
}
#endif
int bxmain(void)
{
#ifdef HAVE_LOCALE_H
// Initialize locale (for isprint() and other functions)
setlocale (LC_ALL, "");
#endif
bx_init_siminterface(); // create the SIM object
static jmp_buf context;
if (setjmp (context) == 0) {
SIM->set_quit_context (&context);
BX_INSTR_INIT_ENV();
if (bx_init_main(bx_startup_flags.argc, bx_startup_flags.argv) < 0) {
BX_INSTR_EXIT_ENV();
return 0;
}
// read a param to decide which config interface to start.
// If one exists, start it. If not, just begin.
bx_param_enum_c *ci_param = SIM->get_param_enum(BXPN_SEL_CONFIG_INTERFACE);
const char *ci_name = ci_param->get_selected();
#if BX_WITH_WX
if (!strcmp(ci_name, "wx")) {
PLUG_load_plugin_var("wx", PLUGTYPE_GUI);
}
else
#endif
{
PLUG_load_plugin_var(ci_name, PLUGTYPE_CI);
}
ci_param->set_enabled(0);
int status = SIM->configuration_interface(ci_name, CI_START);
if (status == CI_ERR_NO_TEXT_CONSOLE)
BX_PANIC(("Bochs needed the text console, but it was not usable"));
// user quit the config interface, so just quit
} else {
// quit via longjmp
}
SIM->set_quit_context(NULL);
#if defined(WIN32)
if (!bx_user_quit) {
// ask user to press ENTER before exiting, so that they can read messages
// before the console window is closed. This isn't necessary after pressing
// the power button.
fprintf(stderr, "\nBochs is exiting. Press ENTER when you're ready to close this window.\n");
char buf[16];
fgets(buf, sizeof(buf), stdin);
}
#endif
plugin_cleanup();
BX_INSTR_EXIT_ENV();
return SIM->get_exit_code();
}
#if defined(__WXMSW__)
// win32 applications get the whole command line in one long string.
// This function is used to split up the string into argc and argv,
// so that the command line can be used on win32 just like on every
// other platform.
//
// I'm sure other people have written this same function, and they may have
// done it better, but I don't know where to find it. -BBD
#ifndef MAX_ARGLEN
#define MAX_ARGLEN 80
#endif
int split_string_into_argv(char *string, int *argc_out, char **argv, int max_argv)
{
char *buf0 = new char[strlen(string)+1];
strcpy (buf0, string);
char *buf = buf0;
int in_double_quote = 0, in_single_quote = 0;
for (int i=0; i<max_argv; i++)
argv[i] = NULL;
argv[0] = new char[6];
strcpy (argv[0], "bochs");
int argc = 1;
argv[argc] = new char[MAX_ARGLEN];
char *outp = &argv[argc][0];
// trim leading and trailing spaces
while (*buf==' ') buf++;
char *p;
char *last_nonspace = buf;
for (p=buf; *p; p++) {
if (*p!=' ') last_nonspace = p;
}
if (last_nonspace != buf) *(last_nonspace+1) = 0;
p = buf;
bool done = false;
while (!done) {
//fprintf (stderr, "parsing '%c' with singlequote=%d, dblquote=%d\n", *p, in_single_quote, in_double_quote);
switch (*p) {
case '\0':
done = true;
// fall through into behavior for space
case ' ':
if (in_double_quote || in_single_quote)
goto do_default;
*outp = 0;
//fprintf (stderr, "completed arg %d = '%s'\n", argc, argv[argc]);
argc++;
if (argc >= max_argv) {
fprintf (stderr, "too many arguments. Increase MAX_ARGUMENTS\n");
return -1;
}
argv[argc] = new char[MAX_ARGLEN];
outp = &argv[argc][0];
while (*p==' ') p++;
break;
case '"':
if (in_single_quote) goto do_default;
in_double_quote = !in_double_quote;
p++;
break;
case '\'':
if (in_double_quote) goto do_default;
in_single_quote = !in_single_quote;
p++;
break;
do_default:
default:
if (outp-&argv[argc][0] >= MAX_ARGLEN) {
//fprintf (stderr, "command line arg %d exceeded max size %d\n", argc, MAX_ARGLEN);
return -1;
}
*(outp++) = *(p++);
}
}
if (in_single_quote) {
fprintf (stderr, "end of string with mismatched single quote (')\n");
return -1;
}
if (in_double_quote) {
fprintf (stderr, "end of string with mismatched double quote (\")\n");
return -1;
}
*argc_out = argc;
return 0;
}
#endif /* if defined(__WXMSW__) */
#if defined(__WXMSW__) || ((BX_WITH_SDL || BX_WITH_SDL2) && defined(WIN32))
// The RedirectIOToConsole() function is copied from an article called "Adding
// Console I/O to a Win32 GUI App" in Windows Developer Journal, December 1997.
// It creates a console window.
//
// NOTE: It could probably be written so that it can safely be called for all
// win32 builds.
int RedirectIOToConsole()
{
int hConHandle;
Bit64s lStdHandle;
FILE *fp;
// allocate a console for this app
FreeConsole();
if (!AllocConsole()) {
MessageBox(NULL, "Failed to create text console", "Error", MB_ICONERROR);
return 0;
}
// redirect unbuffered STDOUT to the console
lStdHandle = (Bit64s)GetStdHandle(STD_OUTPUT_HANDLE);
hConHandle = _open_osfhandle((long)lStdHandle, _O_TEXT);
fp = _fdopen(hConHandle, "w");
*stdout = *fp;
setvbuf(stdout, NULL, _IONBF, 0);
// redirect unbuffered STDIN to the console
lStdHandle = (Bit64s)GetStdHandle(STD_INPUT_HANDLE);
hConHandle = _open_osfhandle((long)lStdHandle, _O_TEXT);
fp = _fdopen(hConHandle, "r");
*stdin = *fp;
setvbuf(stdin, NULL, _IONBF, 0);
// redirect unbuffered STDERR to the console
lStdHandle = (Bit64s)GetStdHandle(STD_ERROR_HANDLE);
hConHandle = _open_osfhandle((long)lStdHandle, _O_TEXT);
fp = _fdopen(hConHandle, "w");
*stderr = *fp;
setvbuf(stderr, NULL, _IONBF, 0);
return 1;
}
#endif /* if defined(__WXMSW__) || ((BX_WITH_SDL || BX_WITH_SDL2) && defined(WIN32)) */
#if defined(__WXMSW__)
// only used for wxWidgets/win32.
// This works ok in Cygwin with a standard wxWidgets compile. In
// VC++ wxWidgets must be compiled with -DNOMAIN=1.
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR m_lpCmdLine, int nCmdShow)
{
bx_startup_flags.hInstance = hInstance;
bx_startup_flags.hPrevInstance = hPrevInstance;
bx_startup_flags.m_lpCmdLine = m_lpCmdLine;
bx_startup_flags.nCmdShow = nCmdShow;
int max_argv = 20;
bx_startup_flags.argv = (char**) malloc (max_argv * sizeof (char*));
split_string_into_argv(m_lpCmdLine, &bx_startup_flags.argc, bx_startup_flags.argv, max_argv);
int arg = 1;
bool bx_noconsole = 0;
while (arg < bx_startup_flags.argc) {
if (!strcmp("-noconsole", bx_startup_flags.argv[arg])) {
bx_noconsole = 1;
break;
}
arg++;
}
if (!bx_noconsole) {
if (!RedirectIOToConsole()) {
return 1;
}
SetConsoleTitle("Bochs for Windows (wxWidgets port) - Console");
}
return bxmain();
}
#endif
#if !defined(__WXMSW__)
// normal main function, presently in for all cases except for
// wxWidgets under win32.
int CDECL main(int argc, char *argv[])
{
bx_startup_flags.argc = argc;
bx_startup_flags.argv = argv;
#ifdef WIN32
int arg = 1;
bool bx_noconsole = 0;
while (arg < argc) {
if (!strcmp("-noconsole", argv[arg])) {
bx_noconsole = 1;
break;
}
arg++;
}
if (bx_noconsole) {
FreeConsole();
} else {
#if BX_WITH_SDL || BX_WITH_SDL2
// if SDL/win32, try to create a console window.
if (!RedirectIOToConsole()) {
return 1;
}
#endif
SetConsoleTitle("Bochs for Windows - Console");
}
#endif
return bxmain();
}
#endif
void print_usage(void)
{
fprintf(stderr,
"Usage: bochs [flags] [bochsrc options]\n\n"
" -n no configuration file\n"
" -f configfile specify configuration file\n"
" -q quick start (skip configuration interface)\n"
" -benchmark N run Bochs in benchmark mode for N millions of emulated ticks\n"
#if BX_ENABLE_STATISTICS
" -dumpstats N dump Bochs stats every N millions of emulated ticks\n"
#endif
" -r path restore the Bochs state from path\n"
" -log filename specify Bochs log file name\n"
" -unlock unlock Bochs images leftover from previous session\n"
#if BX_DEBUGGER
" -rc filename execute debugger commands stored in file\n"
" -dbglog filename specify Bochs internal debugger log file name\n"
#endif
#ifdef WIN32
" -noconsole disable console window\n"
#endif
" --help display this help and exit\n"
" --help features display available features / devices and exit\n"
#if BX_CPU_LEVEL > 4
" --help cpu display supported CPU models and exit\n"
#endif
"\nFor information on Bochs configuration file arguments, see the\n"
#if (!defined(WIN32)) && !BX_WITH_MACOS
"bochsrc section in the user documentation or the man page of bochsrc.\n");
#else
"bochsrc section in the user documentation.\n");
#endif
}
int bx_init_main(int argc, char *argv[])
{
// To deal with initialization order problems inherent in C++, use the macros
// SAFE_GET_IOFUNC and SAFE_GET_GENLOG to retrieve "io" and "genlog" in all
// constructors or functions called by constructors. The macros test for
// NULL and create the object if necessary, then return it. Ensure that io
// and genlog get created, by making one reference to each macro right here.
// All other code can reference io and genlog directly. Because these
// objects are required for logging, and logging is so fundamental to
// knowing what the program is doing, they are never free()d.
SAFE_GET_IOFUNC(); // never freed
SAFE_GET_GENLOG(); // never freed
// initalization must be done early because some destructors expect
// the bochs config options to exist by the time they are called.
bx_init_bx_dbg();
#if BX_PLUGINS && BX_HAVE_GETENV && BX_HAVE_SETENV
// set a default plugin path, in case the user did not specify one
if (getenv("LTDL_LIBRARY_PATH") != NULL) {
BX_INFO(("LTDL_LIBRARY_PATH is set to '%s'", getenv("LTDL_LIBRARY_PATH")));
} else {
BX_INFO(("LTDL_LIBRARY_PATH not set. using compile time default '%s'",
BX_PLUGIN_PATH));
setenv("LTDL_LIBRARY_PATH", BX_PLUGIN_PATH, 1);
}
#endif
// initialize plugin system. This must happen before we attempt to
// load any modules.
plugin_startup();
bx_init_options();
bx_print_header();
SIM->get_param_enum(BXPN_BOCHS_START)->set(BX_RUN_START);
// interpret the args that start with -, like -q, -f, etc.
int arg = 1, load_rcfile=1;
while (arg < argc) {
// parse next arg
if (!strcmp("--help", argv[arg]) || !strncmp("-h", argv[arg], 2)
#if defined(WIN32)
|| !strncmp("/?", argv[arg], 2)
#endif
) {
if ((arg+1) < argc) {
if (!strcmp("features", argv[arg+1])) {
fprintf(stderr, "Supported features:\n\n");
#if BX_SUPPORT_CLGD54XX
fprintf(stderr, "cirrus\n");
#endif
#if BX_SUPPORT_VOODOO
fprintf(stderr, "voodoo\n");
#endif
#if BX_SUPPORT_PCI
fprintf(stderr, "pci\n");
#endif
#if BX_SUPPORT_PCIDEV
fprintf(stderr, "pcidev\n");
#endif
#if BX_SUPPORT_NE2K
fprintf(stderr, "ne2k\n");
#endif
#if BX_SUPPORT_PCIPNIC
fprintf(stderr, "pcipnic\n");
#endif
#if BX_SUPPORT_E1000
fprintf(stderr, "e1000\n");
#endif
#if BX_SUPPORT_SB16
fprintf(stderr, "sb16\n");
#endif
#if BX_SUPPORT_ES1370
fprintf(stderr, "es1370\n");
#endif
#if BX_SUPPORT_USB_OHCI
fprintf(stderr, "usb_ohci\n");
#endif
#if BX_SUPPORT_USB_UHCI
fprintf(stderr, "usb_uhci\n");
#endif
#if BX_SUPPORT_USB_EHCI
fprintf(stderr, "usb_ehci\n");
#endif
#if BX_SUPPORT_USB_XHCI
fprintf(stderr, "usb_xhci\n");
#endif
#if BX_GDBSTUB
fprintf(stderr, "gdbstub\n");
#endif
fprintf(stderr, "\n");
arg++;
}
#if BX_CPU_LEVEL > 4
else if (!strcmp("cpu", argv[arg+1])) {
int i = 0;
fprintf(stderr, "Supported CPU models:\n\n");
do {
fprintf(stderr, "%s\n", SIM->get_param_enum(BXPN_CPU_MODEL)->get_choice(i));
} while (i++ < SIM->get_param_enum(BXPN_CPU_MODEL)->get_max());
fprintf(stderr, "\n");
arg++;
}
#endif
} else {
print_usage();
}
SIM->quit_sim(0);
}
else if (!strcmp("-n", argv[arg])) {
load_rcfile = 0;
}
else if (!strcmp("-q", argv[arg])) {
SIM->get_param_enum(BXPN_BOCHS_START)->set(BX_QUICK_START);
}
else if (!strcmp("-log", argv[arg])) {
if (++arg >= argc) BX_PANIC(("-log must be followed by a filename"));
else SIM->get_param_string(BXPN_LOG_FILENAME)->set(argv[arg]);
}
else if (!strcmp("-unlock", argv[arg])) {
SIM->get_param_bool(BXPN_UNLOCK_IMAGES)->set(1);
}
#if BX_DEBUGGER
else if (!strcmp("-dbglog", argv[arg])) {
if (++arg >= argc) BX_PANIC(("-dbglog must be followed by a filename"));
else SIM->get_param_string(BXPN_DEBUGGER_LOG_FILENAME)->set(argv[arg]);
}
#endif
else if (!strcmp("-f", argv[arg])) {
if (++arg >= argc) BX_PANIC(("-f must be followed by a filename"));
else bochsrc_filename = argv[arg];
}
else if (!strcmp("-qf", argv[arg])) {
SIM->get_param_enum(BXPN_BOCHS_START)->set(BX_QUICK_START);
if (++arg >= argc) BX_PANIC(("-qf must be followed by a filename"));
else bochsrc_filename = argv[arg];
}
else if (!strcmp("-benchmark", argv[arg])) {
SIM->get_param_enum(BXPN_BOCHS_START)->set(BX_QUICK_START);
if (++arg >= argc) BX_PANIC(("-benchmark must be followed by a number"));
else SIM->get_param_num(BXPN_BOCHS_BENCHMARK)->set(atoi(argv[arg]));
}
#if BX_ENABLE_STATISTICS
else if (!strcmp("-dumpstats", argv[arg])) {
if (++arg >= argc) BX_PANIC(("-dumpstats must be followed by a number"));
else SIM->get_param_num(BXPN_DUMP_STATS)->set(atoi(argv[arg]));
}
#endif
else if (!strcmp("-r", argv[arg])) {
if (++arg >= argc) BX_PANIC(("-r must be followed by a path"));
else {
SIM->get_param_enum(BXPN_BOCHS_START)->set(BX_QUICK_START);
SIM->get_param_bool(BXPN_RESTORE_FLAG)->set(1);
SIM->get_param_string(BXPN_RESTORE_PATH)->set(argv[arg]);
}
}
#ifdef WIN32
else if (!strcmp("-noconsole", argv[arg])) {
// already handled in main() / WinMain()
}
#endif
#if BX_WITH_CARBON
else if (!strncmp("-psn", argv[arg], 4)) {
// "-psn" is passed if we are launched by double-clicking
// ugly hack. I don't know how to open a window to print messages in,
// so put them in /tmp/early-bochs-out.txt. Sorry. -bbd
io->init_log("/tmp/early-bochs-out.txt");
BX_INFO(("I was launched by double clicking. Fixing home directory."));
arg = argc; // ignore all other args.
setupWorkingDirectory (argv[0]);
// there is no stdin/stdout so disable the text-based config interface.
SIM->get_param_enum(BXPN_BOCHS_START)->set(BX_QUICK_START);
char cwd[MAXPATHLEN];
getwd (cwd);
BX_INFO(("Now my working directory is %s", cwd));
// if it was started from command line, there could be some args still.
for (int a=0; a<argc; a++) {
BX_INFO(("argument %d is %s", a, argv[a]));
}
}
#endif
#if BX_DEBUGGER
else if (!strcmp("-rc", argv[arg])) {
// process "-rc filename" option, if it exists
if (++arg >= argc) BX_PANIC(("-rc must be followed by a filename"));
else bx_dbg_set_rcfile(argv[arg]);
}
#endif
else if (argv[arg][0] == '-') {
print_usage();
BX_PANIC(("command line arg '%s' was not understood", argv[arg]));
}
else {
// the arg did not start with -, so stop interpreting flags
break;
}
arg++;
}
#if BX_WITH_CARBON
if(!getenv("BXSHARE"))
{
CFBundleRef mainBundle;
CFURLRef bxshareDir;
char bxshareDirPath[MAXPATHLEN];
BX_INFO(("fixing default bxshare location ..."));
// set bxshare to the directory that contains our application
mainBundle = CFBundleGetMainBundle();
BX_ASSERT(mainBundle != NULL);
bxshareDir = CFBundleCopyBundleURL(mainBundle);
BX_ASSERT(bxshareDir != NULL);
// translate this to a unix style full path
if(!CFURLGetFileSystemRepresentation(bxshareDir, true, (UInt8 *)bxshareDirPath, MAXPATHLEN))
{
BX_PANIC(("Unable to work out bxshare path! (Most likely path too long!)"));
return -1;
}
char *c;
c = (char*) bxshareDirPath;
while (*c != '\0') /* go to end */
c++;
while (*c != '/') /* back up to parent */
c--;
*c = '\0'; /* cut off last part (binary name) */
setenv("BXSHARE", bxshareDirPath, 1);
BX_INFO(("now my BXSHARE is %s", getenv("BXSHARE")));
CFRelease(bxshareDir);
}
#endif
#if BX_PLUGINS && BX_WITH_CARBON
// if there is no stdin, then we must create our own LTDL_LIBRARY_PATH.
// also if there is no LTDL_LIBRARY_PATH, but we have a bundle since we're here
// This is here so that it is available whenever --with-carbon is defined but
// the above code might be skipped, as in --with-sdl --with-carbon
if(!isatty(STDIN_FILENO) || !getenv("LTDL_LIBRARY_PATH"))
{
CFBundleRef mainBundle;
CFURLRef libDir;
char libDirPath[MAXPATHLEN];
if(!isatty(STDIN_FILENO))
{
// there is no stdin/stdout so disable the text-based config interface.
SIM->get_param_enum(BXPN_BOCHS_START)->set(BX_QUICK_START);
}
BX_INFO(("fixing default lib location ..."));
// locate the lib directory within the application bundle.
// our libs have been placed in bochs.app/Contents/(current platform aka MacOS)/lib
// This isn't quite right, but they are platform specific and we haven't put
// our plugins into true frameworks and bundles either
mainBundle = CFBundleGetMainBundle();
BX_ASSERT(mainBundle != NULL);
libDir = CFBundleCopyAuxiliaryExecutableURL(mainBundle, CFSTR("lib"));
BX_ASSERT(libDir != NULL);
// translate this to a unix style full path
if(!CFURLGetFileSystemRepresentation(libDir, true, (UInt8 *)libDirPath, MAXPATHLEN))
{
BX_PANIC(("Unable to work out ltdl library path within bochs bundle! (Most likely path too long!)"));
return -1;
}
setenv("LTDL_LIBRARY_PATH", libDirPath, 1);
BX_INFO(("now my LTDL_LIBRARY_PATH is %s", getenv("LTDL_LIBRARY_PATH")));
CFRelease(libDir);
}
#endif /* if BX_PLUGINS && BX_WITH_CARBON */
#if BX_HAVE_GETENV && BX_HAVE_SETENV
if (getenv("BXSHARE") != NULL) {
BX_INFO(("BXSHARE is set to '%s'", getenv("BXSHARE")));
} else {
#ifdef WIN32
BX_INFO(("BXSHARE not set. using system default '%s'",
get_builtin_variable("BXSHARE")));
setenv("BXSHARE", get_builtin_variable("BXSHARE"), 1);
#else
BX_INFO(("BXSHARE not set. using compile time default '%s'",
BX_SHARE_PATH));
setenv("BXSHARE", BX_SHARE_PATH, 1);
#endif
}
#else
// we don't have getenv or setenv. Do nothing.
#endif
int norcfile = 1;
if (SIM->get_param_bool(BXPN_RESTORE_FLAG)->get()) {
load_rcfile = 0;
norcfile = 0;
} else {
// set up and load pre-defined optional plugins before parsing configuration
bx_plugin_ctrl_reset(0);
}
SIM->init_save_restore();
SIM->init_statistics();
if (load_rcfile) {
// parse configuration file and command line arguments
#ifdef WIN32
int length;
if (bochsrc_filename != NULL) {
lstrcpy(bx_startup_flags.initial_dir, bochsrc_filename);
length = lstrlen(bx_startup_flags.initial_dir);
while ((length > 1) && (bx_startup_flags.initial_dir[length-1] != 92)) length--;
bx_startup_flags.initial_dir[length] = 0;
} else {
bx_startup_flags.initial_dir[0] = 0;
}
#endif
if (bochsrc_filename == NULL) bochsrc_filename = bx_find_bochsrc ();
if (bochsrc_filename)
norcfile = bx_read_configuration(bochsrc_filename);
}
if (norcfile) {
// No configuration was loaded, so the current settings are unusable.
// Switch off quick start so that we will drop into the configuration
// interface.
if (SIM->get_param_enum(BXPN_BOCHS_START)->get() == BX_QUICK_START) {
if (!SIM->test_for_text_console())
BX_PANIC(("Unable to start Bochs without a bochsrc.txt and without a text console"));
else
BX_ERROR(("Switching off quick start, because no configuration file was found."));
}
SIM->get_param_enum(BXPN_BOCHS_START)->set(BX_LOAD_START);
}
if (SIM->get_param_bool(BXPN_RESTORE_FLAG)->get()) {
if (arg < argc) {
BX_ERROR(("WARNING: bochsrc options are ignored in restore mode!"));
}
}
else {
// parse the rest of the command line. This is done after reading the
// configuration file so that the command line arguments can override
// the settings from the file.
if (bx_parse_cmdline(arg, argc, argv)) {
BX_PANIC(("There were errors while parsing the command line"));
return -1;
}
}
return 0;
}
bool load_and_init_display_lib(void)
{
if (bx_gui != NULL) {
// bx_gui has already been filled in. This happens when you start
// the simulation for the second time.
// Also, if you load wxWidgets as the configuration interface. Its
// plugin_entry() will install wxWidgets as the bx_gui.
return 1;
}
BX_ASSERT(bx_gui == NULL);
bx_param_enum_c *ci_param = SIM->get_param_enum(BXPN_SEL_CONFIG_INTERFACE);
const char *ci_name = ci_param->get_selected();
bx_param_enum_c *gui_param = SIM->get_param_enum(BXPN_SEL_DISPLAY_LIBRARY);
const char *gui_name = gui_param->get_selected();
if (!strcmp(ci_name, "wx")) {
BX_ERROR(("change of the config interface to wx not implemented yet"));
}
if (!strcmp(gui_name, "wx")) {
// they must not have used wx as the configuration interface, or bx_gui
// would already be initialized. Sorry, it doesn't work that way.
BX_ERROR(("wxWidgets was not used as the configuration interface, so it cannot be used as the display library"));
// choose another, hopefully different!
gui_param->set(0);
gui_name = gui_param->get_selected();
if (!strcmp (gui_name, "wx")) {
BX_PANIC(("no alternative display libraries are available"));
return 0;
}
BX_ERROR(("changing display library to '%s' instead", gui_name));
}
PLUG_load_plugin_var(gui_name, PLUGTYPE_GUI);
#if BX_GUI_SIGHANDLER
// set the flag for guis requiring a GUI sighandler.
// useful when guis are compiled as plugins
// only term for now
if (!strcmp(gui_name, "term")) {
bx_gui_sighandler = 1;
}
#endif
return (bx_gui != NULL);
}
int bx_begin_simulation(int argc, char *argv[])
{
bx_user_quit = 0;
if (SIM->get_param_bool(BXPN_RESTORE_FLAG)->get()) {
if (!SIM->restore_config()) {
BX_PANIC(("cannot restore configuration"));
SIM->get_param_bool(BXPN_RESTORE_FLAG)->set(0);
}
} else {
// make sure all optional plugins have been loaded
SIM->opt_plugin_ctrl("*", 1);
}
// deal with gui selection
if (!load_and_init_display_lib()) {
BX_PANIC(("no gui module was loaded"));
return 0;
}
bx_cpu_count = SIM->get_param_num(BXPN_CPU_NPROCESSORS)->get() *
SIM->get_param_num(BXPN_CPU_NCORES)->get() *
SIM->get_param_num(BXPN_CPU_NTHREADS)->get();
#if BX_SUPPORT_APIC
simulate_xapic = (SIM->get_param_enum(BXPN_CPUID_APIC)->get() >= BX_CPUID_SUPPORT_XAPIC);
// For P6 and Pentium family processors the local APIC ID feild is 4 bits