1 /////////////////////////////////////////////////////////////////////////
2 // $Id: main.cc,v 1.386 2008/12/11 21:19:37 sshwarts Exp $
3 /////////////////////////////////////////////////////////////////////////
5 // Copyright (C) 2002 MandrakeSoft S.A.
9 // 75002 Paris - France
10 // http://www.linux-mandrake.com/
11 // http://www.mandrakesoft.com/
13 // This library is free software; you can redistribute it and/or
14 // modify it under the terms of the GNU Lesser General Public
15 // License as published by the Free Software Foundation; either
16 // version 2 of the License, or (at your option) any later version.
18 // This library is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 // Lesser General Public License for more details.
23 // You should have received a copy of the GNU Lesser General Public
24 // License along with this library; if not, write to the Free Software
25 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include "iodev/iodev.h"
36 // since SDL redefines main() to SDL_main(), we must include SDL.h so that the
37 // C language prototype is found. Otherwise SDL_main() will get its name
38 // mangled and not match what the SDL library is expecting.
41 #if defined(macintosh)
42 // Work around a bug in SDL 1.2.4 on MacOS X, which redefines getenv to
43 // SDL_getenv, but then neglects to provide SDL_getenv. It happens
44 // because we are defining -Dmacintosh.
50 #define Float32 KLUDGE_Float32
51 #define Float64 KLUDGE_Float64
52 #include <Carbon/Carbon.h>
62 #if defined(__MINGW32__) || defined(_MSC_VER)
67 bx_bool bx_gui_sighandler
= 0;
70 #if BX_PROVIDE_DEVICE_MODELS==1
71 // some prototypes from iodev/
72 // I want to stay away from including iodev/iodev.h here
73 Bit32u
bx_unmapped_io_read_handler(Bit32u address
, unsigned io_len
);
74 void bx_unmapped_io_write_handler(Bit32u address
, Bit32u value
,
78 void bx_init_hardware(void);
79 void bx_init_options(void);
80 void bx_init_bx_dbg(void);
82 static const char *divider
= "========================================================================";
83 static logfunctions thePluginLog
;
84 logfunctions
*pluginlog
= &thePluginLog
;
86 bx_startup_flags_t bx_startup_flags
;
92 #define LOG_THIS genlog->
94 #if BX_PROVIDE_DEVICE_MODELS
95 bx_pc_system_c bx_pc_system
;
100 typedef BX_CPU_C
*BX_CPU_C_PTR
;
103 // multiprocessor simulation, we need an array of cpus
104 BOCHSAPI BX_CPU_C_PTR
*bx_cpu_array
= NULL
;
106 // single processor simulation, so there's one of everything
107 BOCHSAPI BX_CPU_C bx_cpu
;
110 #if BX_PROVIDE_CPU_MEMORY==1
111 BOCHSAPI BX_MEM_C bx_mem
;
114 char *bochsrc_filename
= NULL
;
116 void bx_print_header ()
118 fprintf (stderr
, "%s\n", divider
);
120 sprintf (buffer
, "Bochs x86 Emulator %s\n", VER_STRING
);
121 bx_center_print (stderr
, buffer
, 72);
123 sprintf (buffer
, "%s\n", REL_STRING
);
124 bx_center_print (stderr
, buffer
, 72);
126 fprintf (stderr
, "%s\n", divider
);
130 /* Original code by Darrell Walisser - dwaliss1@purdue.edu */
132 static void setupWorkingDirectory(char *path
)
134 char parentdir
[MAXPATHLEN
];
137 strncpy (parentdir
, path
, MAXPATHLEN
);
138 c
= (char*) parentdir
;
140 while (*c
!= '\0') /* go to end */
143 while (*c
!= '/') /* back up to parent */
146 *c
= '\0'; /* cut off last part (binary name) */
148 /* chdir to the binary app's parent */
150 n
= chdir (parentdir
);
151 if (n
) BX_PANIC (("failed to change dir to parent"));
152 /* chdir to the .app's parent */
153 n
= chdir ("../../../");
154 if (n
) BX_PANIC (("failed to change to ../../.."));
157 /* Panic button to display fatal errors.
158 Completely self contained, can't rely on carbon.cc being available */
159 static void carbonFatalDialog(const char *error
, const char *exposition
)
161 DialogRef alertDialog
;
163 CFStringRef cfExposition
;
164 DialogItemIndex index
;
165 AlertStdCFStringAlertParamRec alertParam
= {0};
166 fprintf(stderr
, "Entering carbonFatalDialog: %s\n", error
);
171 cfError
= CFStringCreateWithCString(NULL
, error
, kCFStringEncodingASCII
);
172 if(exposition
!= NULL
)
174 cfExposition
= CFStringCreateWithCString(NULL
, exposition
, kCFStringEncodingASCII
);
176 else { cfExposition
= NULL
; }
177 alertParam
.version
= kStdCFStringAlertVersionOne
;
178 alertParam
.defaultText
= CFSTR("Quit");
179 alertParam
.position
= kWindowDefaultPosition
;
180 alertParam
.defaultButton
= kAlertStdAlertOKButton
;
185 cfExposition
, /* can be NULL */
186 &alertParam
, /* can be NULL */
188 RunStandardAlert(alertDialog
, NULL
, &index
);
191 if(cfExposition
!= NULL
) { CFRelease(cfExposition
); }
196 void print_tree(bx_param_c
*node
, int level
)
199 char tmpstr
[BX_PATHNAME_LEN
], tmpbyte
[4];
201 for (i
=0; i
<level
; i
++)
204 dbg_printf("NULL pointer\n");
207 switch (node
->get_type()) {
209 if (((bx_param_num_c
*)node
)->get_base() == BASE_DEC
) {
210 dbg_printf("%s = " FMT_LL
"d (number)\n", node
->get_name(), ((bx_param_num_c
*)node
)->get64());
212 dbg_printf("%s = 0x" FMT_LL
"x (hex number)\n", node
->get_name(), ((bx_param_num_c
*)node
)->get64());
216 dbg_printf("%s = %s (boolean)\n", node
->get_name(), ((bx_param_bool_c
*)node
)->get()?"true":"false");
219 dbg_printf("%s = '%s' (enum)\n", node
->get_name(), ((bx_param_enum_c
*)node
)->get_selected());
221 case BXT_PARAM_STRING
:
222 if (((bx_param_string_c
*)node
)->get_options()->get() & bx_param_string_c::RAW_BYTES
) {
224 for (i
= 0; i
< ((bx_param_string_c
*)node
)->get_maxsize(); i
++) {
226 tmpbyte
[0] = ((bx_param_string_c
*)node
)->get_separator();
228 strcat(tmpstr
, tmpbyte
);
230 sprintf(tmpbyte
, "%02x", (Bit8u
)((bx_param_string_c
*)node
)->getptr()[i
]);
231 strcat(tmpstr
, tmpbyte
);
233 dbg_printf("%s = '%s' (raw byte string)\n", node
->get_name(), tmpstr
);
235 dbg_printf("%s = '%s' (string)\n", node
->get_name(), ((bx_param_string_c
*)node
)->getptr());
240 dbg_printf("%s = \n", node
->get_name());
241 bx_list_c
*list
= (bx_list_c
*)node
;
242 for (i
=0; i
< list
->get_size(); i
++) {
243 print_tree(list
->get(i
), level
+1);
248 dbg_printf("%s = 'size=%d' (binary data)\n", node
->get_name(), ((bx_shadow_data_c
*)node
)->get_size());
251 dbg_printf("%s (unknown parameter type)\n", node
->get_name());
258 // Initialize locale (for isprint() and other functions)
259 setlocale (LC_ALL
, "");
262 bx_init_siminterface(); // create the SIM object
263 static jmp_buf context
;
264 if (setjmp (context
) == 0) {
265 SIM
->set_quit_context (&context
);
267 if (bx_init_main(bx_startup_flags
.argc
, bx_startup_flags
.argv
) < 0)
268 { BX_INSTR_EXIT_ENV();
271 // read a param to decide which config interface to start.
272 // If one exists, start it. If not, just begin.
273 bx_param_enum_c
*ci_param
= SIM
->get_param_enum(BXPN_SEL_CONFIG_INTERFACE
);
274 const char *ci_name
= ci_param
->get_selected();
275 if (!strcmp(ci_name
, "textconfig")) {
276 #if BX_USE_TEXTCONFIG
277 init_text_config_interface(); // in textconfig.h
279 BX_PANIC(("configuration interface 'textconfig' not present"));
283 else if (!strcmp(ci_name
, "wx")) {
284 PLUG_load_plugin(wx
, PLUGTYPE_CORE
);
288 BX_PANIC(("unsupported configuration interface '%s'", ci_name
));
290 ci_param
->set_enabled(0);
291 int status
= SIM
->configuration_interface(ci_name
, CI_START
);
292 if (status
== CI_ERR_NO_TEXT_CONSOLE
)
293 BX_PANIC(("Bochs needed the text console, but it was not usable"));
294 // user quit the config interface, so just quit
298 SIM
->set_quit_context(NULL
);
301 // ask user to press ENTER before exiting, so that they can read messages
302 // before the console window is closed. This isn't necessary after pressing
304 fprintf(stderr
, "\nBochs is exiting. Press ENTER when you're ready to close this window.\n");
306 fgets(buf
, sizeof(buf
), stdin
);
310 return SIM
->get_exit_code();
313 #if defined(__WXMSW__)
315 // win32 applications get the whole command line in one long string.
316 // This function is used to split up the string into argc and argv,
317 // so that the command line can be used on win32 just like on every
320 // I'm sure other people have written this same function, and they may have
321 // done it better, but I don't know where to find it. -BBD
323 #define MAX_ARGLEN 80
325 int split_string_into_argv (
331 char *buf0
= new char[strlen(string
)+1];
332 strcpy (buf0
, string
);
334 int in_double_quote
= 0, in_single_quote
= 0;
335 for (int i
=0; i
<max_argv
; i
++)
337 argv
[0] = new char[6];
338 strcpy (argv
[0], "bochs");
340 argv
[argc
] = new char[MAX_ARGLEN
];
341 char *outp
= &argv
[argc
][0];
342 // trim leading and trailing spaces
343 while (*buf
==' ') buf
++;
345 char *last_nonspace
= buf
;
346 for (p
=buf
; *p
; p
++) {
347 if (*p
!=' ') last_nonspace
= p
;
349 if (last_nonspace
!= buf
) *(last_nonspace
+1) = 0;
351 bx_bool done
= false;
353 //fprintf (stderr, "parsing '%c' with singlequote=%d, dblquote=%d\n", *p, in_single_quote, in_double_quote);
357 // fall through into behavior for space
359 if (in_double_quote
|| in_single_quote
)
362 //fprintf (stderr, "completed arg %d = '%s'\n", argc, argv[argc]);
364 if (argc
>= max_argv
) {
365 fprintf (stderr
, "too many arguments. Increase MAX_ARGUMENTS\n");
368 argv
[argc
] = new char[MAX_ARGLEN
];
369 outp
= &argv
[argc
][0];
373 if (in_single_quote
) goto do_default
;
374 in_double_quote
= !in_double_quote
;
378 if (in_double_quote
) goto do_default
;
379 in_single_quote
= !in_single_quote
;
384 if (outp
-&argv
[argc
][0] >= MAX_ARGLEN
) {
385 //fprintf (stderr, "command line arg %d exceeded max size %d\n", argc, MAX_ARGLEN);
391 if (in_single_quote
) {
392 fprintf (stderr
, "end of string with mismatched single quote (')\n");
395 if (in_double_quote
) {
396 fprintf (stderr
, "end of string with mismatched double quote (\")\n");
402 #endif /* if defined(__WXMSW__) */
404 #if defined(__WXMSW__) || (BX_WITH_SDL && defined(WIN32))
405 // The RedirectIOToConsole() function is copied from an article called "Adding
406 // Console I/O to a Win32 GUI App" in Windows Developer Journal, December 1997.
407 // It creates a console window.
409 // NOTE: It could probably be written so that it can safely be called for all
411 int RedirectIOToConsole()
416 // allocate a console for this app
417 if (!AllocConsole()) {
418 MessageBox(NULL
, "Failed to create text console", "Error", MB_ICONERROR
);
421 // redirect unbuffered STDOUT to the console
422 lStdHandle
= (long)GetStdHandle(STD_OUTPUT_HANDLE
);
423 hConHandle
= _open_osfhandle(lStdHandle
, _O_TEXT
);
424 fp
= _fdopen(hConHandle
, "w");
426 setvbuf(stdout
, NULL
, _IONBF
, 0);
427 // redirect unbuffered STDIN to the console
428 lStdHandle
= (long)GetStdHandle(STD_INPUT_HANDLE
);
429 hConHandle
= _open_osfhandle(lStdHandle
, _O_TEXT
);
430 fp
= _fdopen(hConHandle
, "r");
432 setvbuf(stdin
, NULL
, _IONBF
, 0);
433 // redirect unbuffered STDERR to the console
434 lStdHandle
= (long)GetStdHandle(STD_ERROR_HANDLE
);
435 hConHandle
= _open_osfhandle(lStdHandle
, _O_TEXT
);
436 fp
= _fdopen(hConHandle
, "w");
438 setvbuf(stderr
, NULL
, _IONBF
, 0);
441 #endif /* if defined(__WXMSW__) || (BX_WITH_SDL && defined(WIN32)) */
443 #if defined(__WXMSW__)
444 // only used for wxWidgets/win32.
445 // This works ok in Cygwin with a standard wxWidgets compile. In
446 // VC++ wxWidgets must be compiled with -DNOMAIN=1.
449 HINSTANCE hPrevInstance
,
450 LPSTR m_lpCmdLine
, int nCmdShow
)
452 bx_startup_flags
.hInstance
= hInstance
;
453 bx_startup_flags
.hPrevInstance
= hPrevInstance
;
454 bx_startup_flags
.m_lpCmdLine
= m_lpCmdLine
;
455 bx_startup_flags
.nCmdShow
= nCmdShow
;
456 if (!RedirectIOToConsole()) {
459 SetConsoleTitle("Bochs for Windows (wxWidgets port) - Console");
461 bx_startup_flags
.argv
= (char**) malloc (max_argv
* sizeof (char*));
462 split_string_into_argv(m_lpCmdLine
, &bx_startup_flags
.argc
, bx_startup_flags
.argv
, max_argv
);
467 #if !defined(__WXMSW__)
468 // normal main function, presently in for all cases except for
469 // wxWidgets under win32.
470 int CDECL
main(int argc
, char *argv
[])
472 bx_startup_flags
.argc
= argc
;
473 bx_startup_flags
.argv
= argv
;
474 #if BX_WITH_SDL && defined(WIN32)
475 // if SDL/win32, try to create a console window.
476 if (!RedirectIOToConsole()) {
481 SetConsoleTitle("Bochs for Windows - Console");
487 void print_usage(void)
490 "Usage: bochs [flags] [bochsrc options]\n\n"
491 " -n no configuration file\n"
492 " -f configfile specify configuration file\n"
493 " -q quick start (skip configuration interface)\n"
494 " -benchmark n run bochs in benchmark mode for millions of emulated ticks\n"
495 " -r path restore the Bochs state from path\n"
496 " -log filename specify Bochs log file name\n"
498 " -rc filename execute debugger commands stored in file\n"
499 " -dbglog filename specify Bochs internal debugger log file name\n"
501 " --help display this help and exit\n\n"
502 "For information on Bochs configuration file arguments, see the\n"
503 #if (!defined(WIN32)) && !BX_WITH_MACOS
504 "bochsrc section in the user documentation or the man page of bochsrc.\n");
506 "bochsrc section in the user documentation.\n");
510 int bx_init_main(int argc
, char *argv
[])
512 // To deal with initialization order problems inherent in C++, use the macros
513 // SAFE_GET_IOFUNC and SAFE_GET_GENLOG to retrieve "io" and "genlog" in all
514 // constructors or functions called by constructors. The macros test for
515 // NULL and create the object if necessary, then return it. Ensure that io
516 // and genlog get created, by making one reference to each macro right here.
517 // All other code can reference io and genlog directly. Because these
518 // objects are required for logging, and logging is so fundamental to
519 // knowing what the program is doing, they are never free()d.
520 SAFE_GET_IOFUNC(); // never freed
521 SAFE_GET_GENLOG(); // never freed
523 // initalization must be done early because some destructors expect
524 // the bochs config options to exist by the time they are called.
530 SIM
->get_param_enum(BXPN_BOCHS_START
)->set(BX_RUN_START
);
532 // interpret the args that start with -, like -q, -f, etc.
533 int arg
= 1, load_rcfile
=1;
536 if (!strcmp("--help", argv
[arg
]) || !strncmp("-h", argv
[arg
], 2)
538 || !strncmp("/?", argv
[arg
], 2)
544 else if (!strcmp("-n", argv
[arg
])) {
547 else if (!strcmp("-q", argv
[arg
])) {
548 SIM
->get_param_enum(BXPN_BOCHS_START
)->set(BX_QUICK_START
);
550 else if (!strcmp("-log", argv
[arg
])) {
551 if (++arg
>= argc
) BX_PANIC(("-log must be followed by a filename"));
552 else SIM
->get_param_string(BXPN_LOG_FILENAME
)->set(argv
[arg
]);
555 else if (!strcmp("-dbglog", argv
[arg
])) {
556 if (++arg
>= argc
) BX_PANIC(("-dbglog must be followed by a filename"));
557 else SIM
->get_param_string(BXPN_DEBUGGER_LOG_FILENAME
)->set(argv
[arg
]);
560 else if (!strcmp("-f", argv
[arg
])) {
561 if (++arg
>= argc
) BX_PANIC(("-f must be followed by a filename"));
562 else bochsrc_filename
= argv
[arg
];
564 else if (!strcmp("-qf", argv
[arg
])) {
565 SIM
->get_param_enum(BXPN_BOCHS_START
)->set(BX_QUICK_START
);
566 if (++arg
>= argc
) BX_PANIC(("-qf must be followed by a filename"));
567 else bochsrc_filename
= argv
[arg
];
569 else if (!strcmp("-benchmark", argv
[arg
])) {
570 SIM
->get_param_enum(BXPN_BOCHS_START
)->set(BX_QUICK_START
);
571 if (++arg
>= argc
) BX_PANIC(("-benchmark must be followed by a number"));
572 else SIM
->get_param_num(BXPN_BOCHS_BENCHMARK
)->set(atoi(argv
[arg
]));
574 else if (!strcmp("-r", argv
[arg
])) {
575 if (++arg
>= argc
) BX_PANIC(("-r must be followed by a path"));
577 SIM
->get_param_enum(BXPN_BOCHS_START
)->set(BX_QUICK_START
);
578 SIM
->get_param_bool(BXPN_RESTORE_FLAG
)->set(1);
579 SIM
->get_param_string(BXPN_RESTORE_PATH
)->set(argv
[arg
]);
583 else if (!strncmp("-psn", argv
[arg
], 4)) {
584 // "-psn" is passed if we are launched by double-clicking
585 // ugly hack. I don't know how to open a window to print messages in,
586 // so put them in /tmp/early-bochs-out.txt. Sorry. -bbd
587 io
->init_log("/tmp/early-bochs-out.txt");
588 BX_INFO (("I was launched by double clicking. Fixing home directory."));
589 arg
= argc
; // ignore all other args.
590 setupWorkingDirectory (argv
[0]);
591 // there is no stdin/stdout so disable the text-based config interface.
592 SIM
->get_param_enum(BXPN_BOCHS_START
)->set(BX_QUICK_START
);
593 char cwd
[MAXPATHLEN
];
595 BX_INFO (("Now my working directory is %s", cwd
));
596 // if it was started from command line, there could be some args still.
597 for (int a
=0; a
<argc
; a
++) {
598 BX_INFO (("argument %d is %s", a
, argv
[a
]));
603 else if (!strcmp("-rc", argv
[arg
])) {
604 // process "-rc filename" option, if it exists
605 if (++arg
>= argc
) BX_PANIC(("-rc must be followed by a filename"));
606 else bx_dbg_set_rcfile(argv
[arg
]);
609 else if (argv
[arg
][0] == '-') {
611 BX_PANIC (("command line arg '%s' was not understood", argv
[arg
]));
614 // the arg did not start with -, so stop interpreting flags
620 if(!getenv("BXSHARE"))
622 CFBundleRef mainBundle
;
624 char bxshareDirPath
[MAXPATHLEN
];
625 BX_INFO (("fixing default bxshare location ..."));
626 // set bxshare to the directory that contains our application
627 mainBundle
= CFBundleGetMainBundle();
628 BX_ASSERT(mainBundle
!= NULL
);
629 bxshareDir
= CFBundleCopyBundleURL(mainBundle
);
630 BX_ASSERT(bxshareDir
!= NULL
);
631 // translate this to a unix style full path
632 if(!CFURLGetFileSystemRepresentation(bxshareDir
, true, (UInt8
*)bxshareDirPath
, MAXPATHLEN
))
634 BX_PANIC(("Unable to work out bxshare path! (Most likely path too long!)"));
638 c
= (char*) bxshareDirPath
;
639 while (*c
!= '\0') /* go to end */
641 while (*c
!= '/') /* back up to parent */
643 *c
= '\0'; /* cut off last part (binary name) */
644 setenv("BXSHARE", bxshareDirPath
, 1);
645 BX_INFO (("now my BXSHARE is %s", getenv("BXSHARE")));
646 CFRelease(bxshareDir
);
650 // set a default plugin path, in case the user did not specify one
652 // if there is no stdin, then we must create our own LTDL_LIBRARY_PATH.
653 // also if there is no LTDL_LIBRARY_PATH, but we have a bundle since we're here
654 // This is here so that it is available whenever --with-carbon is defined but
655 // the above code might be skipped, as in --with-sdl --with-carbon
656 if(!isatty(STDIN_FILENO
) || !getenv("LTDL_LIBRARY_PATH"))
658 CFBundleRef mainBundle
;
660 char libDirPath
[MAXPATHLEN
];
661 if(!isatty(STDIN_FILENO
))
663 // there is no stdin/stdout so disable the text-based config interface.
664 SIM
->get_param_enum(BXPN_BOCHS_START
)->set(BX_QUICK_START
);
666 BX_INFO (("fixing default lib location ..."));
667 // locate the lib directory within the application bundle.
668 // our libs have been placed in bochs.app/Contents/(current platform aka MacOS)/lib
669 // This isn't quite right, but they are platform specific and we haven't put
670 // our plugins into true frameworks and bundles either
671 mainBundle
= CFBundleGetMainBundle();
672 BX_ASSERT(mainBundle
!= NULL
);
673 libDir
= CFBundleCopyAuxiliaryExecutableURL(mainBundle
, CFSTR("lib"));
674 BX_ASSERT(libDir
!= NULL
);
675 // translate this to a unix style full path
676 if(!CFURLGetFileSystemRepresentation(libDir
, true, (UInt8
*)libDirPath
, MAXPATHLEN
))
678 BX_PANIC(("Unable to work out ltdl library path within bochs bundle! (Most likely path too long!)"));
681 setenv("LTDL_LIBRARY_PATH", libDirPath
, 1);
682 BX_INFO (("now my LTDL_LIBRARY_PATH is %s", getenv("LTDL_LIBRARY_PATH")));
685 #elif BX_HAVE_GETENV && BX_HAVE_SETENV
686 if (getenv("LTDL_LIBRARY_PATH") != NULL
) {
687 BX_INFO (("LTDL_LIBRARY_PATH is set to '%s'", getenv("LTDL_LIBRARY_PATH")));
689 BX_INFO (("LTDL_LIBRARY_PATH not set. using compile time default '%s'",
691 setenv("LTDL_LIBRARY_PATH", BX_PLUGIN_PATH
, 1);
693 if (getenv("BXSHARE") != NULL
) {
694 BX_INFO (("BXSHARE is set to '%s'", getenv("BXSHARE")));
696 BX_INFO (("BXSHARE not set. using compile time default '%s'",
698 setenv("BXSHARE", BX_SHARE_PATH
, 1);
701 // we don't have getenv or setenv. Do nothing.
703 #endif /* if BX_PLUGINS */
707 if (SIM
->get_param_bool(BXPN_RESTORE_FLAG
)->get()) {
712 /* parse configuration file and command line arguments */
715 if (bochsrc_filename
!= NULL
) {
716 lstrcpy(bx_startup_flags
.initial_dir
, bochsrc_filename
);
717 length
= lstrlen(bx_startup_flags
.initial_dir
);
718 while ((length
> 1) && (bx_startup_flags
.initial_dir
[length
-1] != 92)) length
--;
719 bx_startup_flags
.initial_dir
[length
] = 0;
721 bx_startup_flags
.initial_dir
[0] = 0;
724 if (bochsrc_filename
== NULL
) bochsrc_filename
= bx_find_bochsrc ();
725 if (bochsrc_filename
)
726 norcfile
= bx_read_configuration(bochsrc_filename
);
730 // No configuration was loaded, so the current settings are unusable.
731 // Switch off quick start so that we will drop into the configuration
733 if (SIM
->get_param_enum(BXPN_BOCHS_START
)->get() == BX_QUICK_START
) {
734 if (!SIM
->test_for_text_console())
735 BX_PANIC(("Unable to start Bochs without a bochsrc.txt and without a text console"));
737 BX_ERROR(("Switching off quick start, because no configuration file was found."));
739 SIM
->get_param_enum(BXPN_BOCHS_START
)->set(BX_LOAD_START
);
742 if (SIM
->get_param_bool(BXPN_RESTORE_FLAG
)->get()) {
744 BX_ERROR(("WARNING: bochsrc options are ignored in restore mode!"));
748 // parse the rest of the command line. This is done after reading the
749 // configuration file so that the command line arguments can override
750 // the settings from the file.
751 if (bx_parse_cmdline(arg
, argc
, argv
)) {
752 BX_PANIC(("There were errors while parsing the command line"));
756 // initialize plugin system. This must happen before we attempt to
762 bx_bool
load_and_init_display_lib(void)
764 if (bx_gui
!= NULL
) {
765 // bx_gui has already been filled in. This happens when you start
766 // the simulation for the second time.
767 // Also, if you load wxWidgets as the configuration interface. Its
768 // plugin_init will install wxWidgets as the bx_gui.
771 BX_ASSERT(bx_gui
== NULL
);
772 bx_param_enum_c
*ci_param
= SIM
->get_param_enum(BXPN_SEL_CONFIG_INTERFACE
);
773 const char *ci_name
= ci_param
->get_selected();
774 bx_param_enum_c
*gui_param
= SIM
->get_param_enum(BXPN_SEL_DISPLAY_LIBRARY
);
775 const char *gui_name
= gui_param
->get_selected();
776 if (!strcmp(ci_name
, "wx")) {
777 BX_ERROR(("change of the config interface to wx not implemented yet"));
779 if (!strcmp(gui_name
, "wx")) {
780 // they must not have used wx as the configuration interface, or bx_gui
781 // would already be initialized. Sorry, it doesn't work that way.
782 BX_ERROR(("wxWidgets was not used as the configuration interface, so it cannot be used as the display library"));
783 // choose another, hopefully different!
785 gui_name
= gui_param
->get_selected();
786 if (!strcmp (gui_name
, "wx")) {
787 BX_PANIC(("no alternative display libraries are available"));
790 BX_ERROR(("changing display library to '%s' instead", gui_name
));
793 if (!strcmp(gui_name
, "amigaos"))
794 PLUG_load_plugin (amigaos
, PLUGTYPE_OPTIONAL
);
797 if (!strcmp(gui_name
, "beos"))
798 PLUG_load_plugin (beos
, PLUGTYPE_OPTIONAL
);
801 if (!strcmp(gui_name
, "carbon"))
802 PLUG_load_plugin (carbon
, PLUGTYPE_OPTIONAL
);
805 if (!strcmp(gui_name
, "macos"))
806 PLUG_load_plugin (macintosh
, PLUGTYPE_OPTIONAL
);
809 if (!strcmp(gui_name
, "nogui"))
810 PLUG_load_plugin (nogui
, PLUGTYPE_OPTIONAL
);
813 if (!strcmp(gui_name
, "rfb"))
814 PLUG_load_plugin (rfb
, PLUGTYPE_OPTIONAL
);
817 if (!strcmp(gui_name
, "sdl"))
818 PLUG_load_plugin (sdl
, PLUGTYPE_OPTIONAL
);
821 if (!strcmp(gui_name
, "svga"))
822 PLUG_load_plugin (svga
, PLUGTYPE_OPTIONAL
);
825 if (!strcmp(gui_name
, "term"))
826 PLUG_load_plugin (term
, PLUGTYPE_OPTIONAL
);
829 if (!strcmp(gui_name
, "win32"))
830 PLUG_load_plugin (win32
, PLUGTYPE_OPTIONAL
);
833 if (!strcmp(gui_name
, "x"))
834 PLUG_load_plugin (x
, PLUGTYPE_OPTIONAL
);
837 #if BX_GUI_SIGHANDLER
838 // set the flag for guis requiring a GUI sighandler.
839 // useful when guis are compiled as plugins
841 if (!strcmp(gui_name
, "term")) {
842 bx_gui_sighandler
= 1;
846 BX_ASSERT(bx_gui
!= NULL
);
850 int bx_begin_simulation (int argc
, char *argv
[])
852 SIM
->init_save_restore();
853 if (SIM
->get_param_bool(BXPN_RESTORE_FLAG
)->get()) {
854 if (!SIM
->restore_config()) {
855 BX_PANIC(("cannot restore configuration"));
856 SIM
->get_param_bool(BXPN_RESTORE_FLAG
)->set(0);
860 // deal with gui selection
861 if (!load_and_init_display_lib ()) {
862 BX_PANIC (("no gui module was loaded"));
866 bx_cpu_count
= SIM
->get_param_num(BXPN_CPU_NPROCESSORS
)->get() *
867 SIM
->get_param_num(BXPN_CPU_NCORES
)->get() *
868 SIM
->get_param_num(BXPN_CPU_NTHREADS
)->get();
870 BX_ASSERT(bx_cpu_count
<= BX_MAX_SMP_THREADS_SUPPORTED
);
871 BX_ASSERT(bx_cpu_count
> 0);
875 if (SIM
->get_param_enum(BXPN_LOAD32BITOS_WHICH
)->get()) {
876 void bx_load32bitOSimagehack(void);
877 bx_load32bitOSimagehack();
880 SIM
->set_init_done(1);
882 // update headerbar buttons since drive status can change during init
883 bx_gui
->update_drive_status_buttons();
885 // iniialize statusbar and set all items inactive
886 if (!SIM
->get_param_bool(BXPN_RESTORE_FLAG
)->get())
888 bx_gui
->statusbar_setitem(-1, 0);
891 // The set handler for mouse_enabled does not actually update the gui
892 // until init_done is set. This forces the set handler to be called,
893 // which sets up the mouse enabled GUI-specific stuff correctly.
894 // Not a great solution but it works. BBD
895 SIM
->get_param_bool(BXPN_MOUSE_ENABLED
)->set(SIM
->get_param_bool(BXPN_MOUSE_ENABLED
)->get());
898 // If using the debugger, it will take control and call
899 // bx_init_hardware() and cpu_loop()
903 // If using gdbstub, it will take control and call
904 // bx_init_hardware() and cpu_loop()
905 if (bx_dbg
.gdbstub_enabled
) bx_gdbstub_init();
909 if (BX_SMP_PROCESSORS
== 1) {
910 // only one processor, run as fast as possible by not messing with
911 // quantums and loops.
913 BX_CPU(0)->cpu_loop(0);
914 if (bx_pc_system
.kill_bochs_request
)
917 // for one processor, the only reason for cpu_loop to return is
918 // that kill_bochs_request was set by the GUI interface.
921 // SMP simulation: do a few instructions on each processor, then switch
922 // to another. Increasing quantum speeds up overall performance, but
923 // reduces granularity of synchronization between processors.
925 int quantum
= SIM
->get_param_num(BXPN_SMP_QUANTUM
)->get();
927 // do some instructions in each processor
928 BX_CPU(processor
)->cpu_loop(quantum
);
929 processor
= (processor
+1) % BX_SMP_PROCESSORS
;
930 if (bx_pc_system
.kill_bochs_request
)
937 #endif /* BX_DEBUGGER == 0 */
938 BX_INFO(("cpu loop quit, shutting down simulator"));
943 void bx_stop_simulation(void)
945 // in wxWidgets, the whole simulator is running in a separate thread.
946 // our only job is to end the thread as soon as possible, NOT to shut
947 // down the whole application with an exit.
948 BX_CPU(0)->async_event
= 1;
949 bx_pc_system
.kill_bochs_request
= 1;
950 // the cpu loop will exit very soon after this condition is set.
953 void bx_sr_after_restore_state(void)
955 #if BX_SUPPORT_SMP == 0
956 BX_CPU(0)->after_restore_state();
958 for (unsigned i
=0; i
<BX_SMP_PROCESSORS
; i
++) {
959 BX_CPU(i
)->after_restore_state();
962 DEV_after_restore_state();
965 void bx_init_hardware()
967 // all configuration has been read, now initialize everything.
969 if (SIM
->get_param_enum(BXPN_BOCHS_START
)->get()==BX_QUICK_START
) {
970 for (int level
=0; level
<N_LOGLEV
; level
++) {
971 int action
= SIM
->get_default_log_action(level
);
972 io
->set_log_action(level
, action
);
976 bx_pc_system
.initialize(SIM
->get_param_num(BXPN_IPS
)->get());
978 if (SIM
->get_param_string(BXPN_LOG_FILENAME
)->getptr()[0]!='-') {
979 BX_INFO (("using log file %s", SIM
->get_param_string(BXPN_LOG_FILENAME
)->getptr()));
980 io
->init_log(SIM
->get_param_string(BXPN_LOG_FILENAME
)->getptr());
983 io
->set_log_prefix(SIM
->get_param_string(BXPN_LOG_PREFIX
)->getptr());
985 // Output to the log file the cpu and device settings
986 // This will by handy for bug reports
987 BX_INFO(("Bochs x86 Emulator %s", VER_STRING
));
988 BX_INFO((" %s", REL_STRING
));
989 BX_INFO(("System configuration"));
990 BX_INFO((" processors: %d (cores=%u, HT threads=%u)", BX_SMP_PROCESSORS
,
991 SIM
->get_param_num(BXPN_CPU_NCORES
)->get(), SIM
->get_param_num(BXPN_CPU_NTHREADS
)->get()));
992 BX_INFO((" A20 line support: %s",BX_SUPPORT_A20
?"yes":"no"));
993 BX_INFO((" APIC support: %s",BX_SUPPORT_APIC
?"yes":"no"));
994 BX_INFO(("CPU configuration"));
995 BX_INFO((" level: %d",BX_CPU_LEVEL
));
997 BX_INFO((" SMP support: yes, quantum=%d", SIM
->get_param_num(BXPN_SMP_QUANTUM
)->get()));
999 BX_INFO((" SMP support: no"));
1001 BX_INFO((" FPU support: %s",BX_SUPPORT_FPU
?"yes":"no"));
1002 BX_INFO((" MMX support: %s",BX_SUPPORT_MMX
?"yes":"no"));
1003 if (BX_SUPPORT_SSE
== 0)
1004 BX_INFO((" SSE support: no"));
1006 BX_INFO((" SSE support: %d%s",BX_SUPPORT_SSE
,BX_SUPPORT_SSE_EXTENSION
?"E":""));
1007 BX_INFO((" CLFLUSH support: %s",BX_SUPPORT_CLFLUSH
?"yes":"no"));
1008 BX_INFO((" VME support: %s",BX_SUPPORT_VME
?"yes":"no"));
1009 BX_INFO((" 3dnow! support: %s",BX_SUPPORT_3DNOW
?"yes":"no"));
1010 BX_INFO((" PAE support: %s",BX_SUPPORT_PAE
?"yes":"no"));
1011 BX_INFO((" PGE support: %s",BX_SUPPORT_GLOBAL_PAGES
?"yes":"no"));
1012 BX_INFO((" PSE support: %s",BX_SUPPORT_LARGE_PAGES
?"yes":"no"));
1013 BX_INFO((" x86-64 support: %s",BX_SUPPORT_X86_64
?"yes":"no"));
1014 BX_INFO((" SEP support: %s",BX_SUPPORT_SEP
?"yes":"no"));
1015 BX_INFO((" MWAIT support: %s",BX_SUPPORT_MONITOR_MWAIT
?"yes":"no"));
1016 BX_INFO((" XSAVE support: %s",BX_SUPPORT_XSAVE
?"yes":"no"));
1017 BX_INFO((" AES support: %s",BX_SUPPORT_AES
?"yes":"no"));
1018 BX_INFO(("Optimization configuration"));
1019 BX_INFO((" RepeatSpeedups support: %s",BX_SupportRepeatSpeedups
?"yes":"no"));
1020 BX_INFO((" Icache support: %s",BX_SUPPORT_ICACHE
?"yes":"no"));
1021 BX_INFO((" Trace cache support: %s",BX_SUPPORT_TRACE_CACHE
?"yes":"no"));
1022 BX_INFO((" Fast function calls: %s",BX_FAST_FUNC_CALL
?"yes":"no"));
1023 BX_INFO(("Devices configuration"));
1024 BX_INFO((" ACPI support: %s",BX_SUPPORT_ACPI
?"yes":"no"));
1025 BX_INFO((" NE2000 support: %s",BX_SUPPORT_NE2K
?"yes":"no"));
1026 BX_INFO((" PCI support: %s",BX_SUPPORT_PCI
?"yes":"no"));
1027 BX_INFO((" SB16 support: %s",BX_SUPPORT_SB16
?"yes":"no"));
1028 BX_INFO((" USB support: %s",BX_SUPPORT_PCIUSB
?"yes":"no"));
1029 BX_INFO((" VGA extension support: %s %s",BX_SUPPORT_VBE
?"vbe":"",
1030 BX_SUPPORT_CLGD54XX
?"cirrus":""));
1032 // Check if there is a romimage
1033 if (strcmp(SIM
->get_param_string(BXPN_ROM_PATH
)->getptr(),"") == 0) {
1034 BX_ERROR(("No romimage to load. Is your bochsrc file loaded/valid ?"));
1037 // set one shot timer for benchmark mode if needed, the timer will fire
1038 // once and kill Bochs simulation after predefined amount of emulated
1040 int benchmark_mode
= SIM
->get_param_num(BXPN_BOCHS_BENCHMARK
)->get();
1041 if (benchmark_mode
) {
1042 BX_INFO(("Bochs benchmark mode is ON (~%d millions of ticks)", benchmark_mode
));
1043 bx_pc_system
.register_timer_ticks(&bx_pc_system
, bx_pc_system_c::benchmarkTimer
,
1044 (Bit64u
) benchmark_mode
* 1000000, 0, 1, "benchmark.timer");
1047 // set up memory and CPU objects
1048 bx_param_num_c
*bxp_memsize
= SIM
->get_param_num(BXPN_MEM_SIZE
);
1049 Bit32u memSize
= bxp_memsize
->get() * 1024*1024;
1051 BX_MEM(0)->init_memory(memSize
);
1053 // First load the BIOS and VGABIOS
1054 BX_MEM(0)->load_ROM(SIM
->get_param_string(BXPN_ROM_PATH
)->getptr(),
1055 SIM
->get_param_num(BXPN_ROM_ADDRESS
)->get(), 0);
1056 BX_MEM(0)->load_ROM(SIM
->get_param_string(BXPN_VGA_ROM_PATH
)->getptr(), 0xc0000, 1);
1058 // Then load the optional ROM images
1059 if (strcmp(SIM
->get_param_string(BXPN_OPTROM1_PATH
)->getptr(), "") !=0)
1060 BX_MEM(0)->load_ROM(SIM
->get_param_string(BXPN_OPTROM1_PATH
)->getptr(), SIM
->get_param_num(BXPN_OPTROM1_ADDRESS
)->get(), 2);
1061 if (strcmp(SIM
->get_param_string(BXPN_OPTROM2_PATH
)->getptr(), "") !=0)
1062 BX_MEM(0)->load_ROM(SIM
->get_param_string(BXPN_OPTROM2_PATH
)->getptr(), SIM
->get_param_num(BXPN_OPTROM2_ADDRESS
)->get(), 2);
1063 if (strcmp(SIM
->get_param_string(BXPN_OPTROM3_PATH
)->getptr(), "") !=0)
1064 BX_MEM(0)->load_ROM(SIM
->get_param_string(BXPN_OPTROM3_PATH
)->getptr(), SIM
->get_param_num(BXPN_OPTROM3_ADDRESS
)->get(), 2);
1065 if (strcmp(SIM
->get_param_string(BXPN_OPTROM4_PATH
)->getptr(), "") !=0)
1066 BX_MEM(0)->load_ROM(SIM
->get_param_string(BXPN_OPTROM4_PATH
)->getptr(), SIM
->get_param_num(BXPN_OPTROM4_ADDRESS
)->get(), 2);
1068 // Then load the optional RAM images
1069 if (strcmp(SIM
->get_param_string(BXPN_OPTRAM1_PATH
)->getptr(), "") !=0)
1070 BX_MEM(0)->load_RAM(SIM
->get_param_string(BXPN_OPTRAM1_PATH
)->getptr(), SIM
->get_param_num(BXPN_OPTRAM1_ADDRESS
)->get(), 2);
1071 if (strcmp(SIM
->get_param_string(BXPN_OPTRAM2_PATH
)->getptr(), "") !=0)
1072 BX_MEM(0)->load_RAM(SIM
->get_param_string(BXPN_OPTRAM2_PATH
)->getptr(), SIM
->get_param_num(BXPN_OPTRAM2_ADDRESS
)->get(), 2);
1073 if (strcmp(SIM
->get_param_string(BXPN_OPTRAM3_PATH
)->getptr(), "") !=0)
1074 BX_MEM(0)->load_RAM(SIM
->get_param_string(BXPN_OPTRAM3_PATH
)->getptr(), SIM
->get_param_num(BXPN_OPTRAM3_ADDRESS
)->get(), 2);
1075 if (strcmp(SIM
->get_param_string(BXPN_OPTRAM4_PATH
)->getptr(), "") !=0)
1076 BX_MEM(0)->load_RAM(SIM
->get_param_string(BXPN_OPTRAM4_PATH
)->getptr(), SIM
->get_param_num(BXPN_OPTRAM4_ADDRESS
)->get(), 2);
1078 #if BX_SUPPORT_SMP == 0
1079 BX_CPU(0)->initialize();
1080 BX_CPU(0)->sanity_checks();
1081 BX_CPU(0)->register_state();
1082 BX_INSTR_INITIALIZE(0);
1084 bx_cpu_array
= new BX_CPU_C_PTR
[BX_SMP_PROCESSORS
];
1086 for (unsigned i
=0; i
<BX_SMP_PROCESSORS
; i
++) {
1087 BX_CPU(i
) = new BX_CPU_C(i
);
1088 BX_CPU(i
)->initialize(); // assign local apic id in 'initialize' method
1089 BX_CPU(i
)->sanity_checks();
1090 BX_CPU(i
)->register_state();
1091 BX_INSTR_INITIALIZE(i
);
1096 bx_pc_system
.register_state();
1097 DEV_register_state();
1098 if (SIM
->get_param_bool(BXPN_RESTORE_FLAG
)->get()) {
1099 if (!SIM
->restore_logopts()) {
1100 BX_PANIC(("cannot restore log options"));
1101 SIM
->get_param_bool(BXPN_RESTORE_FLAG
)->set(0);
1105 // will enable A20 line and reset CPU and devices
1106 bx_pc_system
.Reset(BX_RESET_HARDWARE
);
1108 if (SIM
->get_param_bool(BXPN_RESTORE_FLAG
)->get()) {
1109 if (SIM
->restore_hardware()) {
1110 bx_sr_after_restore_state();
1112 BX_PANIC(("cannot restore hardware state"));
1113 SIM
->get_param_bool(BXPN_RESTORE_FLAG
)->set(0);
1117 bx_gui
->init_signal_handlers();
1118 bx_pc_system
.start_timers();
1120 BX_DEBUG(("bx_init_hardware is setting signal handlers"));
1121 // if not using debugger, then we can take control of SIGINT.
1123 signal(SIGINT
, bx_signal_handler
);
1127 #if !defined(__MINGW32__) && !defined(_MSC_VER)
1128 signal(SIGALRM
, bx_signal_handler
);
1134 void bx_init_bx_dbg(void)
1137 bx_dbg_init_infile();
1140 bx_dbg
.keyboard
= 0;
1148 bx_dbg
.interrupts
= 0;
1149 bx_dbg
.exceptions
= 0;
1152 bx_dbg
.debugger
= 0;
1154 bx_dbg
.unsupported_io
= 0;
1155 bx_dbg
.record_io
= 0;
1159 bx_dbg
.magic_break_enabled
= 0;
1162 bx_dbg
.gdbstub_enabled
= 0;
1168 if (!SIM
->get_init_done()) return 1; // protect from reentry
1170 // in case we ended up in simulation mode, change back to config mode
1171 // so that the user can see any messages left behind on the console.
1172 SIM
->set_display_mode(DISP_MODE_CONFIG
);
1174 #if BX_DEBUGGER == 0
1175 if (SIM
&& SIM
->get_init_done()) {
1176 for (int cpu
=0; cpu
<BX_SMP_PROCESSORS
; cpu
++)
1180 BX_CPU(cpu
)->atexit();
1184 BX_MEM(0)->cleanup_memory();
1186 #if BX_PROVIDE_DEVICE_MODELS
1187 bx_pc_system
.exit();
1190 // restore signal handling to defaults
1191 #if BX_DEBUGGER == 0
1192 BX_INFO(("restoring default signal behavior"));
1193 signal(SIGINT
, SIG_DFL
);
1197 #if !defined(__MINGW32__) && !defined(_MSC_VER)
1198 signal(SIGALRM
, SIG_DFL
);
1202 SIM
->set_init_done(0);
1207 void CDECL
bx_signal_handler(int signum
)
1209 // in a multithreaded environment, a signal such as SIGINT can be sent to all
1210 // threads. This function is only intended to handle signals in the
1211 // simulator thread. It will simply return if called from any other thread.
1212 // Otherwise the BX_PANIC() below can be called in multiple threads at
1213 // once, leading to multiple threads trying to display a dialog box,
1214 // leading to GUI deadlock.
1215 if (!SIM
->is_sim_thread()) {
1216 BX_INFO (("bx_signal_handler: ignored sig %d because it wasn't called from the simulator thread", signum
));
1219 #if BX_GUI_SIGHANDLER
1220 if (bx_gui_sighandler
) {
1221 // GUI signal handler gets first priority, if the mask says it's wanted
1222 if ((1<<signum
) & bx_gui
->get_sighandler_mask()) {
1223 bx_gui
->sighandler(signum
);
1230 static Bit64u ticks_count
= 0;
1231 static Bit64u counts
= 0;
1233 if (signum
== SIGALRM
)
1235 // amount of system ticks passed from last time the handler was called
1236 Bit64u ips_count
= bx_pc_system
.time_ticks() - ticks_count
;
1238 bx_gui
->show_ips((Bit32u
) ips_count
);
1239 ticks_count
= bx_pc_system
.time_ticks();
1241 if (bx_dbg
.print_timestamps
) {
1242 printf("IPS: %u\taverage = %u\t\t(%us)\n",
1243 (unsigned) ips_count
, (unsigned) (ticks_count
/counts
), (unsigned) counts
);
1246 #if !defined(__MINGW32__) && !defined(_MSC_VER)
1247 signal(SIGALRM
, bx_signal_handler
);
1254 #if BX_GUI_SIGHANDLER
1255 if (bx_gui_sighandler
) {
1256 if ((1<<signum
) & bx_gui
->get_sighandler_mask ()) {
1257 bx_gui
->sighandler(signum
);
1263 BX_PANIC(("SIGNAL %u caught", signum
));