VirtualBox

source: vbox/trunk/src/VBox/Frontends/VBoxBFE/VBoxBFE.cpp@ 2705

Last change on this file since 2705 was 2667, checked in by vboxsync, 18 years ago

FE/BFE: removed the APIC -> IOAPIC dependency again

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 58.8 KB
Line 
1/** @file
2 *
3 * VBox frontends: Basic Frontend (BFE):
4 * VBoxBFE main routines
5 *
6 * VBoxBFE is a limited frontend that sits directly on the Virtual Machine
7 * Manager (VMM) and does _not_ use COM to communicate.
8 * On Linux and Windows, VBoxBFE is based on SDL; on L4 it's based on the
9 * L4 console. Much of the code has been copied over from the other frontends
10 * in VBox/Main/ and src/Frontends/VBoxSDL/.
11 */
12
13/*
14 * Copyright (C) 2006 InnoTek Systemberatung GmbH
15 *
16 * This file is part of VirtualBox Open Source Edition (OSE), as
17 * available from http://www.virtualbox.org. This file is free software;
18 * you can redistribute it and/or modify it under the terms of the GNU
19 * General Public License as published by the Free Software Foundation,
20 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
21 * distribution. VirtualBox OSE is distributed in the hope that it will
22 * be useful, but WITHOUT ANY WARRANTY of any kind.
23 *
24 * If you received this file as part of a commercial VirtualBox
25 * distribution, then only the terms of your commercial VirtualBox
26 * license agreement apply instead of the previous paragraph.
27 */
28
29/*******************************************************************************
30* Header Files *
31*******************************************************************************/
32#define LOG_GROUP LOG_GROUP_GUI
33
34#ifndef VBOXBFE_WITHOUT_COM
35# include <VBox/com/Guid.h>
36# include <VBox/com/string.h>
37using namespace com;
38#endif
39
40#include <VBox/types.h>
41#include <VBox/err.h>
42#include <VBox/param.h>
43#include <VBox/pdm.h>
44#include <VBox/version.h>
45#ifdef VBOXBFE_WITH_USB
46# include <VBox/vusb.h>
47#endif
48#include <VBox/log.h>
49#include <iprt/path.h>
50#include <iprt/string.h>
51#include <iprt/runtime.h>
52#include <iprt/assert.h>
53#include <iprt/semaphore.h>
54#include <iprt/stream.h>
55#include <iprt/thread.h>
56#include <iprt/uuid.h>
57#include <iprt/file.h>
58#include <iprt/alloca.h>
59#include <iprt/ctype.h>
60
61#include "VBoxBFE.h"
62
63#include <stdio.h>
64#include <stdlib.h> /* putenv */
65#include <errno.h>
66
67#if defined(__LINUX__) || defined(__L4__)
68#include <fcntl.h>
69#include <net/if.h>
70#include <sys/ioctl.h>
71#include <linux/if_tun.h>
72#endif
73
74#ifndef __L4ENV__
75#include <vector>
76#endif
77
78#include "ConsoleImpl.h"
79#include "DisplayImpl.h"
80#include "MouseImpl.h"
81#include "KeyboardImpl.h"
82#include "VMMDevInterface.h"
83#include "StatusImpl.h"
84#include "Framebuffer.h"
85#include "MachineDebuggerImpl.h"
86#ifdef VBOXBFE_WITH_USB
87# include "HostUSBImpl.h"
88#endif
89
90#if defined(USE_SDL) && ! defined(__L4__)
91#include "SDLConsole.h"
92#include "SDLFramebuffer.h"
93#endif
94
95#ifdef __L4__
96#include "L4Console.h"
97#include "L4Framebuffer.h"
98#endif
99
100#ifdef __L4ENV__
101# ifndef L4API_l4v2onv4
102# include <l4/sys/ktrace.h>
103# endif
104# include <l4/vboxserver/file.h>
105#endif
106
107/*******************************************************************************
108* Defined Constants And Macros *
109*******************************************************************************/
110
111#define VBOXSDL_ADVANCED_OPTIONS
112
113
114/*******************************************************************************
115* Internal Functions *
116*******************************************************************************/
117static DECLCALLBACK(int) vboxbfeConfigConstructor(PVM pVM, void *pvUser);
118static DECLCALLBACK(void) vmstateChangeCallback(PVM pVM, VMSTATE enmState, VMSTATE enmOldState, void *pvUser);
119static DECLCALLBACK(void) setVMErrorCallback(PVM pVM, void *pvUser, int rc, RT_SRC_POS_DECL,
120 const char *pszFormat, va_list args);
121static DECLCALLBACK(int) VMPowerUpThread(RTTHREAD Thread, void *pvUser);
122
123
124/*******************************************************************************
125* Global Variables *
126*******************************************************************************/
127
128PVM pVM = NULL;
129Mouse *gMouse = NULL;
130VMDisplay *gDisplay = NULL;
131Keyboard *gKeyboard = NULL;
132VMMDev *gVMMDev = NULL;
133Framebuffer *gFramebuffer = NULL;
134MachineDebugger *gMachineDebugger = NULL;
135VMStatus *gStatus = NULL;
136Console *gConsole = NULL;
137#ifdef VBOXBFE_WITH_USB
138HostUSB *gHostUSB = NULL;
139#endif
140
141VMSTATE machineState = VMSTATE_CREATING;
142
143PPDMLED mapFDLeds[2] = {0};
144PPDMLED mapIDELeds[4] = {0};
145
146/** flag whether keyboard/mouse events are grabbed */
147#ifdef __L4__
148/** see <l4/input/macros.h> for key definitions */
149int gHostKey; /* not used */
150int gHostKeySym = KEY_RIGHTCTRL;
151#elif defined (DEBUG_dmik)
152// my mini kbd doesn't have RCTRL...
153int gHostKey = KMOD_RSHIFT;
154int gHostKeySym = SDLK_RSHIFT;
155#else
156int gHostKey = KMOD_RCTRL;
157int gHostKeySym = SDLK_RCTRL;
158#endif
159bool gfAllowFullscreenToggle = true;
160
161static bool g_fIOAPIC = false;
162static bool g_fACPI = true;
163static bool g_fAudio = false;
164#ifdef VBOXBFE_WITH_USB
165static bool g_fUSB = false;
166#endif
167//static bool fPacketSniffer = false;
168static char *hdaFile = NULL;
169static char *cdromFile = NULL;
170static char *fdaFile = NULL;
171static const char *pszBootDevice = "IDE";
172static uint32_t memorySize = 128;
173static uint32_t vramSize = 4;
174#ifdef VBOXSDL_ADVANCED_OPTIONS
175static unsigned fRawR0 = ~0U;
176static unsigned fRawR3 = ~0U;
177static unsigned fPATM = ~0U;
178static unsigned fCSAM = ~0U;
179#endif
180static bool g_fReleaseLog = true; /**< Set if we should open the release. */
181
182
183/**
184 * Network device config info.
185 */
186typedef struct BFENetworkDevice
187{
188 enum
189 {
190 NOT_CONFIGURED = 0,
191 NONE,
192 NAT,
193 HIF,
194 INTNET
195 } enmType; /**< The type of network driver. */
196 bool fSniff; /**< Set if the network sniffer should be installed. */
197 const char *pszSniff; /**< Output file for the network sniffer. */
198 PDMMAC Mac; /**< The mac address for the device. */
199 const char *pszName; /**< The device name of a HIF device. The name of the internal network. */
200#if 1//defined(__LINUX__)
201 bool fHaveFd; /**< Set if fd is valid. */
202 int32_t fd; /**< The file descriptor of a HIF device.*/
203#endif
204} BFENETDEV, *PBFENETDEV;
205
206/** Array of network device configurations. */
207static BFENETDEV g_aNetDevs[NetworkAdapterCount];
208
209
210/** @todo currently this is only set but never read. */
211static char szError[512];
212
213
214/**
215 * Converts the passed in network option
216 *
217 * @returns Index into g_aNetDevs on success. (positive)
218 * @returns VERR_INVALID_PARAMETER on failure. (negative)
219 * @param pszArg The argument.
220 * @param cchRoot The length of the argument root.
221 */
222static int networkArg2Index(const char *pszArg, int cchRoot)
223{
224 uint32_t n;
225 int rc = RTStrToUInt32Ex(&pszArg[cchRoot], NULL, 10, &n);
226 if (VBOX_FAILURE(rc))
227 {
228 RTPrintf("Error: invalid network device option (rc=%Vrc): %s\n", rc, pszArg);
229 return -1;
230 }
231 if (n < 1 || n > NetworkAdapterCount)
232 {
233 RTPrintf("Error: The network device number is out of range: %RU32 (1 <= 0 <= %u) (%s)\n",
234 n, NetworkAdapterCount, pszArg);
235 return -1;
236 }
237 return n;
238}
239
240
241/**
242 * Print a syntax error.
243 *
244 * @returns return value for main().
245 * @param pszMsg The message format string.
246 * @param ... Format arguments.
247 */
248static int SyntaxError(const char *pszMsg, ...)
249{
250 va_list va;
251 RTPrintf("error: ");
252 va_start(va, pszMsg);
253 RTPrintfV(pszMsg, va);
254 va_end(va);
255 return 1;
256}
257
258
259/**
260 * Print a fatal error.
261 *
262 * @returns return value for main().
263 * @param pszMsg The message format string.
264 * @param ... Format arguments.
265 */
266static int FatalError(const char *pszMsg, ...)
267{
268 va_list va;
269 RTPrintf("fatal error: ");
270 va_start(va, pszMsg);
271 RTPrintfV(pszMsg, va);
272 va_end(va);
273 return 1;
274}
275
276
277/**
278 * Print program usage.
279 */
280static void show_usage()
281{
282 RTPrintf("Usage:\n"
283 " -hda <file> Set first hard disk to file\n"
284 " -fda <file> Set first floppy disk to file\n"
285 " -cdrom <file> Set CDROM to file/device ('none' to unmount)\n"
286 " -boot <a|c|d> Set boot device (a = floppy, c = first hard disk, d = DVD)\n"
287 " -m <size> Set memory size in megabytes (default 128MB)\n"
288 " -vram <size> Set size of video memory in megabytes\n"
289 " -fullscreen Start VM in fullscreen mode\n"
290 " -nofstoggle Forbid switching to/from fullscreen mode\n"
291 " -nohostkey Disable hostkey\n"
292 " -[no]acpi Enable or disable ACPI (default: enabled)\n"
293 " -[no]ioapic Enable or disable the IO-APIC (default: disabled)\n"
294 " -audio Enable audio\n"
295 " -natdev<1-N> Configure NAT for network device N\n"
296 " -hifdev<1-N> <dev> <mac> Use existing Host Interface Network Device with the given name and MAC address\n"
297#if 0
298 " -netsniff<1-N> Enable packet sniffer\n"
299#endif
300#ifdef __LINUX__
301 " -tapfd<1-N> <fd> Use existing TAP device, don't allocate\n"
302#endif
303#ifdef VBOX_VRDP
304 " -vrdp [port] Listen for VRDP connections on port (default if not specified)\n"
305#endif
306#ifdef VBOX_SECURELABEL
307 " -securelabel Display a secure VM label at the top of the screen\n"
308 " -seclabelfnt TrueType (.ttf) font file for secure session label\n"
309 " -seclabelsiz Font point size for secure session label (default 12)\n"
310#endif
311 " -[no]rellog Enable or disable the release log './VBoxBFE.log' (default: enabled)\n"
312#ifdef VBOXSDL_ADVANCED_OPTIONS
313 " -[no]rawr0 Enable or disable raw ring 3\n"
314 " -[no]rawr3 Enable or disable raw ring 0\n"
315 " -[no]patm Enable or disable PATM\n"
316 " -[no]csam Enable or disable CSAM\n"
317#endif
318#ifdef __L4ENV__
319 " -env <var=value> Set the given environment variable to \"value\"\n"
320#endif
321 "\n");
322}
323
324
325/** entry point */
326int main(int argc, char **argv)
327{
328#ifdef __L4ENV__
329#ifndef L4API_l4v2onv4
330 /* clear Fiasco kernel trace buffer */
331 fiasco_tbuf_clear();
332#endif
333 /* set the environment. Must be done before the runtime is
334 initialised. Yes, it really must. */
335 for (int i = 0; i < argc; i++)
336 if (strcmp(argv[i], "-env") == 0)
337 {
338 if (++i >= argc)
339 return SyntaxError("missing argument to -env (format: var=value)!\n");
340 /* add it to the environment */
341 if (putenv(argv[i]) != 0)
342 return SyntaxError("Error setting environment string %s.\n", argv[i]);
343 }
344#endif /* __L4ENV__ */
345
346 /*
347 * Before we do *anything*, we initialize the runtime.
348 */
349 int rc = RTR3Init();
350 if (VBOX_FAILURE(rc))
351 return FatalError("RTR3Init failed rc=%Vrc\n", rc);
352
353
354 bool fFullscreen = false;
355#ifdef VBOX_VRDP
356 int32_t portVRDP = -1;
357#endif
358#ifdef VBOX_SECURELABEL
359 bool fSecureLabel = false;
360 uint32_t secureLabelPointSize = 12;
361 char *secureLabelFontFile = NULL;
362#endif
363 RTPrintf("VirtualBox Simple SDL GUI built %s %s\n", __DATE__, __TIME__);
364
365 // less than one parameter is not possible
366 if (argc < 2)
367 {
368 show_usage();
369 return 1;
370 }
371
372 /*
373 * Parse the command line arguments.
374 */
375 for (int curArg = 1; curArg < argc; curArg++)
376 {
377 const char * const pszArg = argv[curArg];
378 if (strcmp(pszArg, "-boot") == 0)
379 {
380 if (++curArg >= argc)
381 return SyntaxError("missing argument for boot drive!\n");
382 if (strlen(argv[curArg]) != 1)
383 return SyntaxError("invalid argument for boot drive! (%s)\n", argv[curArg]);
384 rc = VINF_SUCCESS;
385 switch (argv[curArg][0])
386 {
387 case 'a':
388 {
389 pszBootDevice = "FLOPPY";
390 break;
391 }
392
393 case 'c':
394 {
395 pszBootDevice = "IDE";
396 break;
397 }
398
399 case 'd':
400 {
401 pszBootDevice = "DVD";
402 break;
403 }
404
405 default:
406 return SyntaxError("wrong argument for boot drive! (%s)\n", argv[curArg]);
407 }
408 }
409 else if (strcmp(pszArg, "-m") == 0)
410 {
411 if (++curArg >= argc)
412 return SyntaxError("missing argument for memory size!\n");
413 rc = RTStrToUInt32Ex(argv[curArg], NULL, 0, &memorySize);
414 if (VBOX_FAILURE(rc))
415 return SyntaxError("cannot grok the memory size: %s (%Vrc)\n",
416 argv[curArg], rc);
417 }
418 else if (strcmp(pszArg, "-vram") == 0)
419 {
420 if (++curArg >= argc)
421 return SyntaxError("missing argument for vram size!\n");
422 rc = RTStrToUInt32Ex(argv[curArg], NULL, 0, &vramSize);
423 if (VBOX_FAILURE(rc))
424 return SyntaxError("cannot grok the vram size: %s (%Vrc)\n",
425 argv[curArg], rc);
426 }
427 else if (strcmp(pszArg, "-fullscreen") == 0)
428 fFullscreen = true;
429 else if (strcmp(pszArg, "-nofstoggle") == 0)
430 gfAllowFullscreenToggle = false;
431 else if (strcmp(pszArg, "-nohostkey") == 0)
432 {
433 gHostKey = 0;
434 gHostKeySym = 0;
435 }
436 else if (strcmp(pszArg, "-acpi") == 0)
437 g_fACPI = true;
438 else if (strcmp(pszArg, "-noacpi") == 0)
439 g_fACPI = false;
440 else if (strcmp(pszArg, "-ioapic") == 0)
441 g_fIOAPIC = true;
442 else if (strcmp(pszArg, "-noioapic") == 0)
443 g_fIOAPIC = false;
444 else if (strcmp(pszArg, "-audio") == 0)
445 g_fAudio = true;
446#ifdef VBOXBFE_WITH_USB
447 else if (strcmp(pszArg, "-usb") == 0)
448 g_fUSB = true;
449#endif
450 else if (strcmp(pszArg, "-hda") == 0)
451 {
452 if (++curArg >= argc)
453 return SyntaxError("missing file name for first hard disk!\n");
454
455 /* resolve it. */
456 if (RTPathExists(argv[curArg]))
457 hdaFile = RTPathRealDup(argv[curArg]);
458 if (!hdaFile)
459 return SyntaxError("The path to the specified harddisk, '%s', could not be resolved.\n", argv[curArg]);
460 }
461 else if (strcmp(pszArg, "-fda") == 0)
462 {
463 if (++curArg >= argc)
464 return SyntaxError("missing file/device name for first floppy disk!\n");
465
466 /* resolve it. */
467 if (RTPathExists(argv[curArg]))
468 fdaFile = RTPathRealDup(argv[curArg]);
469 if (!fdaFile)
470 return SyntaxError("The path to the specified floppy disk, '%s', could not be resolved.\n", argv[curArg]);
471 }
472 else if (strcmp(pszArg, "-cdrom") == 0)
473 {
474 if (++curArg >= argc)
475 return SyntaxError("missing file/device name for first hard disk!\n");
476
477 /* resolve it. */
478 if (RTPathExists(argv[curArg]))
479 cdromFile = RTPathRealDup(argv[curArg]);
480 if (!cdromFile)
481 return SyntaxError("The path to the specified cdrom, '%s', could not be resolved.\n", argv[curArg]);
482 }
483 else if ( strncmp(pszArg, "-natdev", 7) == 0
484 || strncmp(pszArg, "-hifdev", 7) == 0
485 || strncmp(pszArg, "-nonetd", 7) == 0
486 || strncmp(pszArg, "-intnet", 7) == 0)
487 {
488 int i = networkArg2Index(pszArg, 7);
489 if (i < 0)
490 return 1;
491 g_aNetDevs[i].enmType = !strncmp(pszArg, "-natdev", 7)
492 ? BFENETDEV::NAT
493 : !strncmp(pszArg, "-hifdev", 7)
494 ? BFENETDEV::HIF
495 : !strncmp(pszArg, "-intnet", 7)
496 ? BFENETDEV::INTNET
497 : BFENETDEV::NONE;
498
499 /* The HIF device name / The Internal Network name. */
500 g_aNetDevs[i].pszName = NULL;
501 if ( g_aNetDevs[i].enmType == BFENETDEV::HIF
502 || g_aNetDevs[i].enmType == BFENETDEV::INTNET)
503 {
504 if (curArg + 1 >= argc)
505 return SyntaxError(g_aNetDevs[i].enmType == BFENETDEV::HIF
506 ? "The TAP network device name is missing! (%s)\n"
507 : "The internal network name is missing! (%s)\n"
508 , pszArg);
509 g_aNetDevs[i].pszName = argv[++curArg];
510 }
511
512 /* The MAC address. */
513 if (++curArg >= argc)
514 return SyntaxError("The network MAC address is missing! (%s)\n", pszArg);
515 if (strlen(argv[curArg]) != 12)
516 return SyntaxError("The network MAC address has an invalid length: %s (%s)\n", argv[curArg], pszArg);
517 const char *pszMac = argv[curArg];
518 for (unsigned j = 0; j < RT_ELEMENTS(g_aNetDevs[i].Mac.au8); j++)
519 {
520 char c1 = toupper(*pszMac++) - '0';
521 if (c1 > 9)
522 c1 -= 7;
523 char c2 = toupper(*pszMac++) - '0';
524 if (c2 > 9)
525 c2 -= 7;
526 if (c2 > 16 || c1 > 16)
527 return SyntaxError("Invalid MAC address: %s\n", argv[curArg]);
528 g_aNetDevs[i].Mac.au8[j] = ((c1 & 0x0f) << 4) | (c2 & 0x0f);
529 }
530 }
531 else if (strncmp(pszArg, "-netsniff", 9) == 0)
532 {
533 int i = networkArg2Index(pszArg, 7);
534 if (rc < 0)
535 return 1;
536 g_aNetDevs[i].fSniff = true;
537 /** @todo filename */
538 }
539#ifdef __LINUX__
540 else if (strncmp(pszArg, "-tapfd", 6) == 0)
541 {
542 int i = networkArg2Index(pszArg, 7);
543 if (++curArg >= argc)
544 return SyntaxError("missing argument for %s!\n", pszArg);
545 rc = RTStrToInt32Ex(argv[curArg], NULL, 0, &g_aNetDevs[i].fd);
546 if (VBOX_FAILURE(rc))
547 return SyntaxError("cannot grok tap fd: %s (%VRc)\n", argv[curArg], rc);
548 g_aNetDevs[i].fHaveFd = true;
549 }
550#endif /* __LINUX__ */
551#ifdef VBOX_VRDP
552 else if (strcmp(pszArg, "-vrdp") == 0)
553 {
554 // -vrdp might take a port number (positive).
555 portVRDP = 0; // indicate that it was encountered.
556 if (curArg + 1 < argc && argv[curArg + 1][0] != '-')
557 {
558 rc = RTStrToInt32Ex(argv[curArg], NULL, 0, &portVRDP);
559 if (VBOX_FAILURE(rc))
560 return SyntaxError("cannot vrpd port: %s (%VRc)\n", argv[curArg], rc);
561 if (portVRDP < 0 || portVRDP >= 0x10000)
562 return SyntaxError("vrdp port number is out of range: %RI32\n", portVRDP);
563 }
564 }
565#endif /* VBOX_VRDP */
566#ifdef VBOX_SECURELABEL
567 else if (strcmp(pszArg, "-securelabel") == 0)
568 {
569 fSecureLabel = true;
570 LogFlow(("Secure labelling turned on\n"));
571 }
572 else if (strcmp(pszArg, "-seclabelfnt") == 0)
573 {
574 if (++curArg >= argc)
575 return SyntaxError("missing font file name for secure label!\n");
576 secureLabelFontFile = argv[curArg];
577 }
578 else if (strcmp(pszArg, "-seclabelsiz") == 0)
579 {
580 if (++curArg >= argc)
581 return SyntaxError("missing font point size for secure label!\n");
582 secureLabelPointSize = atoi(argv[curArg]);
583 }
584#endif
585 else if (strcmp(pszArg, "-rellog") == 0)
586 g_fReleaseLog = true;
587 else if (strcmp(pszArg, "-norellog") == 0)
588 g_fReleaseLog = false;
589#ifdef VBOXSDL_ADVANCED_OPTIONS
590 else if (strcmp(pszArg, "-rawr0") == 0)
591 fRawR0 = true;
592 else if (strcmp(pszArg, "-norawr0") == 0)
593 fRawR0 = false;
594 else if (strcmp(pszArg, "-rawr3") == 0)
595 fRawR3 = true;
596 else if (strcmp(pszArg, "-norawr3") == 0)
597 fRawR3 = false;
598 else if (strcmp(pszArg, "-patm") == 0)
599 fPATM = true;
600 else if (strcmp(pszArg, "-nopatm") == 0)
601 fPATM = false;
602 else if (strcmp(pszArg, "-csam") == 0)
603 fCSAM = true;
604 else if (strcmp(pszArg, "-nocsam") == 0)
605 fCSAM = false;
606#endif /* VBOXSDL_ADVANCED_OPTIONS */
607#ifdef __L4__
608 else if (strcmp(pszArg, "-env") == 0)
609 ++curArg;
610#endif /* __L4__ */
611 /* just show the help screen */
612 else
613 {
614 SyntaxError("unrecognized argument '%s'\n", pszArg);
615 show_usage();
616 return 1;
617 }
618 }
619
620 gMachineDebugger = new MachineDebugger();
621 gStatus = new VMStatus();
622 gKeyboard = new Keyboard();
623 gMouse = new Mouse();
624 gVMMDev = new VMMDev();
625 gDisplay = new VMDisplay();
626#if defined(USE_SDL)
627 /* First console, then framebuffer!! */
628 gConsole = new SDLConsole();
629 gFramebuffer = new SDLFramebuffer();
630#elif defined(__L4ENV__)
631 gConsole = new L4Console();
632 gFramebuffer = new L4Framebuffer();
633#else
634#error "todo"
635#endif
636 if (!gConsole->initialized())
637 goto leave;
638 gDisplay->RegisterExternalFramebuffer(gFramebuffer);
639
640 /* start with something in the titlebar */
641 gConsole->updateTitlebar();
642
643 /*
644 * Start the VM execution thread. This has to be done
645 * asynchronously as powering up can take some time
646 * (accessing devices such as the host DVD drive). In
647 * the meantime, we have to service the SDL event loop.
648 */
649
650 RTTHREAD thread;
651 rc = RTThreadCreate(&thread, VMPowerUpThread, 0, 0, RTTHREADTYPE_MAIN_WORKER, 0, "PowerUp");
652 if (VBOX_FAILURE(rc))
653 {
654 RTPrintf("Error: Thread creation failed with %d\n", rc);
655 return -1;
656 }
657
658 /* loop until the powerup processing is done */
659 do
660 {
661#if defined(__LINUX__) && defined(USE_SDL)
662 if ( machineState == VMSTATE_CREATING
663 || machineState == VMSTATE_LOADING)
664 {
665 int event = gConsole->eventWait();
666
667 switch (event)
668 {
669 case CONEVENT_USR_SCREENRESIZE:
670 LogFlow(("CONEVENT_USR_SCREENRESIZE\n"));
671 gFramebuffer->resize();
672 /* notify the display that the resize has been completed */
673 gDisplay->ResizeCompleted();
674 break;
675
676 case CONEVENT_USR_QUIT:
677 RTPrintf("Error: failed to power up VM! No error text available.\n");
678 goto leave;
679 }
680 }
681 else
682#endif
683 RTThreadSleep(1000);
684 }
685 while ( machineState == VMSTATE_CREATING
686 || machineState == VMSTATE_LOADING);
687
688 if (machineState == VMSTATE_TERMINATED)
689 goto leave;
690
691 /* did the power up succeed? */
692 if (machineState != VMSTATE_RUNNING)
693 {
694 RTPrintf("Error: failed to power up VM! No error text available (rc = 0x%x state = %d)\n", rc, machineState);
695 goto leave;
696 }
697
698 gConsole->updateTitlebar();
699
700 /*
701 * Main event loop
702 */
703 LogFlow(("VBoxSDL: Entering big event loop\n"));
704
705 while (1)
706 {
707 int event = gConsole->eventWait();
708
709 switch (event)
710 {
711 case CONEVENT_NONE:
712 /* Handled internally */
713 break;
714
715 case CONEVENT_QUIT:
716 case CONEVENT_USR_QUIT:
717 goto leave;
718
719 case CONEVENT_SCREENUPDATE:
720 /// @todo that somehow doesn't seem to work!
721 gFramebuffer->repaint();
722 break;
723
724 case CONEVENT_USR_TITLEBARUPDATE:
725 gConsole->updateTitlebar();
726 break;
727
728 case CONEVENT_USR_SCREENRESIZE:
729 {
730 LogFlow(("CONEVENT_USR_SCREENRESIZE\n"));
731 gFramebuffer->resize();
732 /* notify the display that the resize has been completed */
733 gDisplay->ResizeCompleted();
734 break;
735 }
736
737#ifdef VBOX_SECURELABEL
738 case CONEVENT_USR_SECURELABELUPDATE:
739 {
740 /*
741 * Query the new label text
742 */
743 Bstr key = VBOXSDL_SECURELABEL_EXTRADATA;
744 Bstr label;
745 gMachine->COMGETTER(ExtraData)(key, label.asOutParam());
746 Utf8Str labelUtf8 = label;
747 /*
748 * Now update the label
749 */
750 gFramebuffer->setSecureLabelText(labelUtf8.raw());
751 break;
752 }
753#endif /* VBOX_SECURELABEL */
754
755 }
756
757 }
758
759leave:
760 LogFlow(("Returning from main()!\n"));
761
762 if (pVM)
763 {
764 /*
765 * If get here because the guest terminated using ACPI off we don't have to
766 * switch off the VM because we were notified via vmstateChangeCallback()
767 * that this already happened. In any other case stop the VM before killing her.
768 */
769 if (machineState != VMSTATE_OFF)
770 {
771 /* Power off VM */
772 PVMREQ pReq;
773 rc = VMR3ReqCall(pVM, &pReq, RT_INDEFINITE_WAIT, (PFNRT)VMR3PowerOff, 1, pVM);
774 }
775
776 /* And destroy it */
777 rc = VMR3Destroy(pVM);
778 AssertRC(rc);
779 }
780
781 delete gFramebuffer;
782 delete gConsole;
783 delete gDisplay;
784 delete gKeyboard;
785 delete gMouse;
786 delete gStatus;
787 delete gMachineDebugger;
788
789 RTLogFlush(NULL);
790 return VBOX_FAILURE (rc) ? 1 : 0;
791}
792
793
794
795/**
796 * VM state callback function. Called by the VMM
797 * using its state machine states.
798 *
799 * Primarily used to handle VM initiated power off, suspend and state saving,
800 * but also for doing termination completed work (VMSTATE_TERMINATE).
801 *
802 * In general this function is called in the context of the EMT.
803 *
804 * @todo machineState is set to VMSTATE_RUNNING before all devices have received power on events
805 * this can prematurely allow the main thread to enter the event loop
806 *
807 * @param pVM The VM handle.
808 * @param enmState The new state.
809 * @param enmOldState The old state.
810 * @param pvUser The user argument.
811 */
812static DECLCALLBACK(void) vmstateChangeCallback(PVM pVM, VMSTATE enmState, VMSTATE enmOldState, void *pvUser)
813{
814 LogFlow(("vmstateChangeCallback: changing state from %d to %d\n", enmOldState, enmState));
815 machineState = enmState;
816
817 switch (enmState)
818 {
819 /*
820 * The VM has terminated
821 */
822 case VMSTATE_OFF:
823 {
824 gConsole->eventQuit();
825 break;
826 }
827
828 /*
829 * The VM has been completely destroyed.
830 *
831 * Note: This state change can happen at two points:
832 * 1) At the end of VMR3Destroy() if it was not called from EMT.
833 * 2) At the end of vmR3EmulationThread if VMR3Destroy() was called by EMT.
834 */
835 case VMSTATE_TERMINATED:
836 {
837 break;
838 }
839
840 default: /* shut up gcc */
841 break;
842 }
843}
844
845
846/**
847 * VM error callback function. Called by the various VM components.
848 *
849 * @param pVM The VM handle.
850 * @param pvUser The user argument.
851 * @param rc VBox status code.
852 * @param pszError Error message format string.
853 * @param args Error message arguments.
854 * @thread EMT.
855 */
856DECLCALLBACK(void) setVMErrorCallback(PVM pVM, void *pvUser, int rc, RT_SRC_POS_DECL,
857 const char *pszFormat, va_list args)
858{
859 /** @todo accessing shared resource without any kind of synchronization */
860 if (VBOX_SUCCESS(rc))
861 szError[0] = '\0';
862 else
863 RTStrPrintfV(szError, sizeof(szError), pszFormat, args);
864}
865
866
867/** VM asynchronous operations thread */
868DECLCALLBACK(int) VMPowerUpThread(RTTHREAD Thread, void *pvUser)
869{
870 int rc = VINF_SUCCESS;
871 int rc2;
872
873 /*
874 * Setup the release log instance in current directory.
875 */
876 if (g_fReleaseLog)
877 {
878 static const char * const s_apszGroups[] = VBOX_LOGGROUP_NAMES;
879 PRTLOGGER pLogger;
880 rc2 = RTLogCreate(&pLogger, RTLOGFLAGS_PREFIX_TIME_PROG, "all",
881 "VBOX_RELEASE_LOG", ELEMENTS(s_apszGroups), s_apszGroups,
882 RTLOGDEST_FILE, "./VBoxBFE.log");
883 if (VBOX_SUCCESS(rc2))
884 {
885 /* some introductory information */
886 RTTIMESPEC TimeSpec;
887 char szNowUct[64];
888 RTTimeSpecToString(RTTimeNow(&TimeSpec), szNowUct, sizeof(szNowUct));
889 RTLogRelLogger(pLogger, 0, ~0U,
890 "VBoxBFE %s (%s %s) release log\n"
891 "Log opened %s\n",
892 VBOX_VERSION_STRING, __DATE__, __TIME__,
893 szNowUct);
894
895 /* register this logger as the release logger */
896 RTLogRelSetDefaultInstance(pLogger);
897 }
898 }
899
900 /*
901 * Start VM (also from saved state) and track progress
902 */
903 LogFlow(("VMPowerUp\n"));
904
905 /*
906 * Create empty VM.
907 */
908 rc = VMR3Create(setVMErrorCallback, NULL, vboxbfeConfigConstructor, NULL, &pVM);
909 if (VBOX_FAILURE(rc))
910 {
911 RTPrintf("Error: VM creation failed with %Vrc.\n", rc);
912 goto failure;
913 }
914
915
916 /*
917 * Register VM state change handler
918 */
919 rc = VMR3AtStateRegister(pVM, vmstateChangeCallback, NULL);
920 if (VBOX_FAILURE(rc))
921 {
922 RTPrintf("Error: VMR3AtStateRegister failed with %Vrc.\n", rc);
923 goto failure;
924 }
925
926#ifdef VBOXBFE_WITH_USB
927 /*
928 * Capture USB devices.
929 */
930 if (g_fUSB)
931 {
932 gHostUSB = new HostUSB();
933 gHostUSB->init(pVM);
934 }
935#endif /* VBOXBFE_WITH_USB */
936
937#ifdef __L4ENV__
938 /* L4 console cannot draw a host cursor */
939 gMouse->setHostCursor(false);
940#else
941 gMouse->setHostCursor(true);
942#endif
943
944 /*
945 * Power on the VM (i.e. start executing).
946 */
947 if (VBOX_SUCCESS(rc))
948 {
949 PVMREQ pReq;
950 rc = VMR3ReqCall(pVM, &pReq, RT_INDEFINITE_WAIT, (PFNRT)VMR3PowerOn, 1, pVM);
951 if (VBOX_SUCCESS(rc))
952 {
953 rc = pReq->iStatus;
954 AssertRC(rc);
955 VMR3ReqFree(pReq);
956 }
957 else
958 AssertMsgFailed(("VMR3PowerOn failed, rc=%Vrc\n", rc));
959 }
960
961 /*
962 * On failure destroy the VM.
963 */
964 if (VBOX_FAILURE(rc))
965 {
966 goto failure;
967 }
968 return 0;
969
970
971failure:
972 if (pVM)
973 {
974 rc2 = VMR3Destroy(pVM);
975 AssertRC(rc2);
976 pVM = NULL;
977 }
978 machineState = VMSTATE_TERMINATED;
979 return 0;
980}
981
982/**
983 * Register the main drivers.
984 *
985 * @returns VBox status code.
986 * @param pCallbacks Pointer to the callback table.
987 * @param u32Version VBox version number.
988 */
989DECLCALLBACK(int) VBoxDriversRegister(PCPDMDRVREGCB pCallbacks, uint32_t u32Version)
990{
991 int rc;
992
993 LogFlow(("VBoxDriversRegister: u32Version=%#x\n", u32Version));
994 AssertReleaseMsg(u32Version == VBOX_VERSION, ("u32Version=%#x VBOX_VERSION=%#x\n", u32Version, VBOX_VERSION));
995
996 rc = pCallbacks->pfnRegister(pCallbacks, &Mouse::DrvReg);
997 AssertRC(rc);
998 if (VBOX_FAILURE(rc))
999 return rc;
1000 rc = pCallbacks->pfnRegister(pCallbacks, &Keyboard::DrvReg);
1001 AssertRC(rc);
1002 if (VBOX_FAILURE(rc))
1003 return rc;
1004
1005 rc = pCallbacks->pfnRegister(pCallbacks, &VMDisplay::DrvReg);
1006 AssertRC(rc);
1007 if (VBOX_FAILURE(rc))
1008 return rc;
1009 rc = pCallbacks->pfnRegister(pCallbacks, &VMMDev::DrvReg);
1010 AssertRC(rc);
1011 if (VBOX_FAILURE(rc))
1012 return rc;
1013
1014 rc = pCallbacks->pfnRegister(pCallbacks, &VMStatus::DrvReg);
1015 if (VBOX_FAILURE(rc))
1016 return rc;
1017
1018 return VINF_SUCCESS;
1019}
1020
1021
1022/**
1023 * Constructs the VMM configuration tree.
1024 *
1025 * @returns VBox status code.
1026 * @param pVM VM handle.
1027 */
1028static DECLCALLBACK(int) vboxbfeConfigConstructor(PVM pVM, void *pvUser)
1029{
1030 int rcAll = VINF_SUCCESS;
1031 int rc;
1032
1033#define UPDATERC() do { if (VBOX_FAILURE(rc) && VBOX_SUCCESS(rcAll)) rcAll = rc; } while (0)
1034#undef CHECK_RC /** @todo r=bird: clashes with VBox/com/Assert.h. */
1035#define CHECK_RC() UPDATERC()
1036
1037 /*
1038 * Root values.
1039 */
1040 PCFGMNODE pRoot = CFGMR3GetRoot(pVM);
1041 rc = CFGMR3InsertString(pRoot, "Name", "Default VM");
1042 UPDATERC();
1043 rc = CFGMR3InsertInteger(pRoot, "RamSize", memorySize * _1M);
1044 UPDATERC();
1045 rc = CFGMR3InsertInteger(pRoot, "TimerMillies", 10);
1046 UPDATERC();
1047#ifdef VBOXSDL_ADVANCED_OPTIONS
1048 rc = CFGMR3InsertInteger(pRoot, "RawR3Enabled", (fRawR3 != ~0U) ? fRawR3 : 1);
1049 UPDATERC();
1050 rc = CFGMR3InsertInteger(pRoot, "RawR0Enabled", (fRawR0 != ~0U) ? fRawR0 : 1);
1051 UPDATERC();
1052 rc = CFGMR3InsertInteger(pRoot, "PATMEnabled", (fPATM != ~0U) ? fPATM : 1);
1053 UPDATERC();
1054 rc = CFGMR3InsertInteger(pRoot, "CSAMEnabled", (fCSAM != ~0U) ? fCSAM : 1);
1055#else
1056 rc = CFGMR3InsertInteger(pRoot, "RawR3Enabled", 1);
1057 UPDATERC();
1058 rc = CFGMR3InsertInteger(pRoot, "RawR0Enabled", 1);
1059 UPDATERC();
1060 rc = CFGMR3InsertInteger(pRoot, "PATMEnabled", 1);
1061 UPDATERC();
1062 rc = CFGMR3InsertInteger(pRoot, "CSAMEnabled", 1);
1063#endif
1064 UPDATERC();
1065
1066 /*
1067 * PDM.
1068 */
1069 rc = PDMR3RegisterDrivers(pVM, VBoxDriversRegister);
1070 UPDATERC();
1071
1072 /*
1073 * Devices
1074 */
1075 PCFGMNODE pDevices = NULL;
1076 rc = CFGMR3InsertNode(pRoot, "Devices", &pDevices);
1077 UPDATERC();
1078 /* device */
1079 PCFGMNODE pDev = NULL;
1080 PCFGMNODE pInst = NULL;
1081 PCFGMNODE pCfg = NULL;
1082 PCFGMNODE pLunL0 = NULL;
1083 PCFGMNODE pLunL1 = NULL;
1084
1085 /*
1086 * PC Arch.
1087 */
1088 rc = CFGMR3InsertNode(pDevices, "pcarch", &pDev);
1089 UPDATERC();
1090 rc = CFGMR3InsertNode(pDev, "0", &pInst);
1091 UPDATERC();
1092 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */
1093 UPDATERC();
1094 rc = CFGMR3InsertNode(pInst, "Config", &pCfg);
1095 UPDATERC();
1096
1097 /*
1098 * PC Bios.
1099 */
1100 rc = CFGMR3InsertNode(pDevices, "pcbios", &pDev);
1101 UPDATERC();
1102 rc = CFGMR3InsertNode(pDev, "0", &pInst);
1103 UPDATERC();
1104 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */
1105 UPDATERC();
1106 rc = CFGMR3InsertNode(pInst, "Config", &pCfg);
1107 UPDATERC();
1108 rc = CFGMR3InsertInteger(pCfg, "RamSize", memorySize * _1M);
1109 UPDATERC();
1110 rc = CFGMR3InsertString(pCfg, "BootDevice0", pszBootDevice);
1111 UPDATERC();
1112 rc = CFGMR3InsertString(pCfg, "BootDevice1", "NONE");
1113 UPDATERC();
1114 rc = CFGMR3InsertString(pCfg, "BootDevice2", "NONE");
1115 UPDATERC();
1116 rc = CFGMR3InsertString(pCfg, "BootDevice3", "NONE");
1117 UPDATERC();
1118 rc = CFGMR3InsertString(pCfg, "HardDiskDevice", "piix3ide");
1119 UPDATERC();
1120 rc = CFGMR3InsertString(pCfg, "FloppyDevice", "i82078");
1121 UPDATERC();
1122
1123 /* Default: no bios logo. */
1124 rc = CFGMR3InsertInteger(pCfg, "FadeIn", 1);
1125 UPDATERC();
1126 rc = CFGMR3InsertInteger(pCfg, "FadeOut", 0);
1127 UPDATERC();
1128 rc = CFGMR3InsertInteger(pCfg, "LogoTime", 0);
1129 UPDATERC();
1130 rc = CFGMR3InsertString(pCfg, "LogoFile", "");
1131 UPDATERC();
1132
1133 /*
1134 * ACPI
1135 */
1136 if (g_fACPI)
1137 {
1138 rc = CFGMR3InsertNode(pDevices, "acpi", &pDev); CHECK_RC();
1139 rc = CFGMR3InsertNode(pDev, "0", &pInst); CHECK_RC();
1140 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ CHECK_RC();
1141 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); CHECK_RC();
1142 rc = CFGMR3InsertInteger(pCfg, "RamSize", memorySize * _1M); CHECK_RC();
1143 rc = CFGMR3InsertInteger(pCfg, "IOAPIC", g_fIOAPIC); CHECK_RC();
1144 rc = CFGMR3InsertInteger(pInst, "PCIDeviceNo", 7); CHECK_RC();
1145 rc = CFGMR3InsertInteger(pInst, "PCIFunctionNo", 0); CHECK_RC();
1146
1147 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); CHECK_RC();
1148 rc = CFGMR3InsertString(pLunL0, "Driver", "ACPIHost"); CHECK_RC();
1149 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); CHECK_RC();
1150 }
1151
1152 /*
1153 * PCI bus.
1154 */
1155 rc = CFGMR3InsertNode(pDevices, "pci", &pDev); /* piix3 */
1156 UPDATERC();
1157 rc = CFGMR3InsertNode(pDev, "0", &pInst);
1158 UPDATERC();
1159 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */
1160 UPDATERC();
1161 rc = CFGMR3InsertNode(pInst, "Config", &pCfg);
1162 UPDATERC();
1163 rc = CFGMR3InsertInteger(pCfg, "IOAPIC", g_fIOAPIC); CHECK_RC();
1164
1165 /*
1166 * DMA
1167 */
1168 rc = CFGMR3InsertNode(pDevices, "8237A", &pDev); CHECK_RC();
1169 rc = CFGMR3InsertNode(pDev, "0", &pInst); CHECK_RC();
1170 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ CHECK_RC();
1171
1172 /*
1173 * PCI bus.
1174 */
1175 rc = CFGMR3InsertNode(pDevices, "pci", &pDev); /* piix3 */ CHECK_RC();
1176 rc = CFGMR3InsertNode(pDev, "0", &pInst); CHECK_RC();
1177 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ CHECK_RC();
1178 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); CHECK_RC();
1179
1180 /*
1181 * PS/2 keyboard & mouse.
1182 */
1183 rc = CFGMR3InsertNode(pDevices, "pckbd", &pDev); CHECK_RC();
1184 rc = CFGMR3InsertNode(pDev, "0", &pInst); CHECK_RC();
1185 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ CHECK_RC();
1186 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); CHECK_RC();
1187
1188 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); CHECK_RC();
1189 rc = CFGMR3InsertString(pLunL0, "Driver", "KeyboardQueue"); CHECK_RC();
1190 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); CHECK_RC();
1191 rc = CFGMR3InsertInteger(pCfg, "QueueSize", 64); CHECK_RC();
1192
1193 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL1); CHECK_RC();
1194 rc = CFGMR3InsertString(pLunL1, "Driver", "MainKeyboard"); CHECK_RC();
1195 rc = CFGMR3InsertNode(pLunL1, "Config", &pCfg); CHECK_RC();
1196 rc = CFGMR3InsertInteger(pCfg, "Object", (uintptr_t)gKeyboard); CHECK_RC();
1197
1198 rc = CFGMR3InsertNode(pInst, "LUN#1", &pLunL0); CHECK_RC();
1199 rc = CFGMR3InsertString(pLunL0, "Driver", "MouseQueue"); CHECK_RC();
1200 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); CHECK_RC();
1201 rc = CFGMR3InsertInteger(pCfg, "QueueSize", 128); CHECK_RC();
1202
1203 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL1); CHECK_RC();
1204 rc = CFGMR3InsertString(pLunL1, "Driver", "MainMouse"); CHECK_RC();
1205 rc = CFGMR3InsertNode(pLunL1, "Config", &pCfg); CHECK_RC();
1206 rc = CFGMR3InsertInteger(pCfg, "Object", (uintptr_t)gMouse); CHECK_RC();
1207
1208
1209 /*
1210 * i82078 Floppy drive controller
1211 */
1212 rc = CFGMR3InsertNode(pDevices, "i82078", &pDev); CHECK_RC();
1213 rc = CFGMR3InsertNode(pDev, "0", &pInst); CHECK_RC();
1214 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); CHECK_RC();
1215 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); CHECK_RC();
1216 rc = CFGMR3InsertInteger(pCfg, "IRQ", 6); CHECK_RC();
1217 rc = CFGMR3InsertInteger(pCfg, "DMA", 2); CHECK_RC();
1218 rc = CFGMR3InsertInteger(pCfg, "MemMapped", 0 ); CHECK_RC();
1219 rc = CFGMR3InsertInteger(pCfg, "IOBase", 0x3f0); CHECK_RC();
1220
1221 /* Attach the status driver */
1222 rc = CFGMR3InsertNode(pInst, "LUN#999", &pLunL0); CHECK_RC();
1223 rc = CFGMR3InsertString(pLunL0, "Driver", "MainStatus"); CHECK_RC();
1224 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); CHECK_RC();
1225 rc = CFGMR3InsertInteger(pCfg, "papLeds", (uintptr_t)&mapFDLeds[0]); CHECK_RC();
1226 rc = CFGMR3InsertInteger(pCfg, "First", 0); CHECK_RC();
1227 rc = CFGMR3InsertInteger(pCfg, "Last", 0); CHECK_RC();
1228
1229 if (fdaFile)
1230 {
1231 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); CHECK_RC();
1232 rc = CFGMR3InsertString(pLunL0, "Driver", "Block"); CHECK_RC();
1233 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); CHECK_RC();
1234 rc = CFGMR3InsertString(pCfg, "Type", "Floppy 1.44"); CHECK_RC();
1235 rc = CFGMR3InsertInteger(pCfg, "Mountable", 1); CHECK_RC();
1236
1237 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL1); CHECK_RC();
1238 rc = CFGMR3InsertString(pLunL1, "Driver", "RawImage"); CHECK_RC();
1239 rc = CFGMR3InsertNode(pLunL1, "Config", &pCfg); CHECK_RC();
1240 rc = CFGMR3InsertString(pCfg, "Path", fdaFile); CHECK_RC();
1241 }
1242
1243 /*
1244 * i8254 Programmable Interval Timer And Dummy Speaker
1245 */
1246 rc = CFGMR3InsertNode(pDevices, "i8254", &pDev); CHECK_RC();
1247 rc = CFGMR3InsertNode(pDev, "0", &pInst); CHECK_RC();
1248 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); CHECK_RC();
1249#ifdef DEBUG
1250 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ CHECK_RC();
1251#endif
1252
1253 /*
1254 * i8259 Programmable Interrupt Controller.
1255 */
1256 rc = CFGMR3InsertNode(pDevices, "i8259", &pDev); CHECK_RC();
1257 rc = CFGMR3InsertNode(pDev, "0", &pInst); CHECK_RC();
1258 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ CHECK_RC();
1259 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); CHECK_RC();
1260
1261 /*
1262 * Advanced Programmable Interrupt Controller.
1263 */
1264 rc = CFGMR3InsertNode(pDevices, "apic", &pDev); CHECK_RC();
1265 rc = CFGMR3InsertNode(pDev, "0", &pInst); CHECK_RC();
1266 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ CHECK_RC();
1267 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); CHECK_RC();
1268 rc = CFGMR3InsertInteger(pCfg, "IOAPIC", g_fIOAPIC); CHECK_RC();
1269
1270 /*
1271 * I/O Advanced Programmable Interrupt Controller.
1272 */
1273 if (g_fIOAPIC)
1274 {
1275 rc = CFGMR3InsertNode(pDevices, "ioapic", &pDev); CHECK_RC();
1276 rc = CFGMR3InsertNode(pDev, "0", &pInst); CHECK_RC();
1277 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ CHECK_RC();
1278 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); CHECK_RC();
1279 }
1280
1281 /*
1282 * RTC MC146818.
1283 */
1284 rc = CFGMR3InsertNode(pDevices, "mc146818", &pDev); CHECK_RC();
1285 rc = CFGMR3InsertNode(pDev, "0", &pInst); CHECK_RC();
1286 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); CHECK_RC();
1287
1288 /*
1289 * Serial ports
1290 */
1291 rc = CFGMR3InsertNode(pDevices, "serial", &pDev); CHECK_RC();
1292 rc = CFGMR3InsertNode(pDev, "0", &pInst); CHECK_RC();
1293 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); CHECK_RC();
1294 rc = CFGMR3InsertInteger(pCfg, "IRQ", 4); CHECK_RC();
1295 rc = CFGMR3InsertInteger(pCfg, "IOBase", 0x3f8); CHECK_RC();
1296
1297 rc = CFGMR3InsertNode(pDev, "1", &pInst); CHECK_RC();
1298 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); CHECK_RC();
1299 rc = CFGMR3InsertInteger(pCfg, "IRQ", 3); CHECK_RC();
1300 rc = CFGMR3InsertInteger(pCfg, "IOBase", 0x2f8); CHECK_RC();
1301
1302 /*
1303 * VGA.
1304 */
1305 rc = CFGMR3InsertNode(pDevices, "vga", &pDev); CHECK_RC();
1306 rc = CFGMR3InsertNode(pDev, "0", &pInst); CHECK_RC();
1307 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ CHECK_RC();
1308 rc = CFGMR3InsertInteger(pInst, "PCIDeviceNo", 2); CHECK_RC();
1309 rc = CFGMR3InsertInteger(pInst, "PCIFunctionNo", 0); CHECK_RC();
1310 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); CHECK_RC();
1311 rc = CFGMR3InsertInteger(pCfg, "VRamSize", vramSize * _1M); CHECK_RC();
1312
1313#ifdef __L4ENV__
1314 /* XXX hard-coded */
1315 rc = CFGMR3InsertInteger(pCfg, "HeightReduction", 18); CHECK_RC();
1316 rc = CFGMR3InsertInteger(pCfg, "CustomVideoModes", 1); CHECK_RC();
1317 char szBuf[64];
1318 /* Tell the guest which is the ideal video mode to use */
1319 RTStrPrintf(szBuf, sizeof(szBuf), "%dx%dx%d",
1320 gFramebuffer->getHostXres(),
1321 gFramebuffer->getHostYres(),
1322 gFramebuffer->getHostBitsPerPixel());
1323 rc = CFGMR3InsertString(pCfg, "CustomVideoMode1", szBuf); CHECK_RC();
1324#endif
1325
1326 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); CHECK_RC();
1327 rc = CFGMR3InsertString(pLunL0, "Driver", "MainDisplay"); CHECK_RC();
1328 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); CHECK_RC();
1329 rc = CFGMR3InsertInteger(pCfg, "Object", (uintptr_t)gDisplay); CHECK_RC();
1330
1331 /*
1332 * IDE (update this when the main interface changes)
1333 */
1334 rc = CFGMR3InsertNode(pDevices, "piix3ide", &pDev); /* piix3 */ CHECK_RC();
1335 rc = CFGMR3InsertNode(pDev, "0", &pInst); CHECK_RC();
1336 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ CHECK_RC();
1337 rc = CFGMR3InsertInteger(pInst, "PCIDeviceNo", 1); CHECK_RC();
1338 rc = CFGMR3InsertInteger(pInst, "PCIFunctionNo", 1); CHECK_RC();
1339 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); CHECK_RC();
1340
1341 if (hdaFile)
1342 {
1343 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); CHECK_RC();
1344 rc = CFGMR3InsertString(pLunL0, "Driver", "Block"); CHECK_RC();
1345 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); CHECK_RC();
1346 rc = CFGMR3InsertString(pCfg, "Type", "HardDisk"); CHECK_RC();
1347 rc = CFGMR3InsertInteger(pCfg, "Mountable", 0); CHECK_RC();
1348
1349 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL1); CHECK_RC();
1350 rc = CFGMR3InsertString(pLunL1, "Driver", "VBoxHDD"); CHECK_RC();
1351 rc = CFGMR3InsertNode(pLunL1, "Config", &pCfg); CHECK_RC();
1352 rc = CFGMR3InsertString(pCfg, "Path", hdaFile); CHECK_RC();
1353 }
1354
1355 if (cdromFile)
1356 {
1357 // ASSUME: DVD drive is always attached to LUN#2 (i.e. secondary IDE master)
1358 rc = CFGMR3InsertNode(pInst, "LUN#2", &pLunL0); CHECK_RC();
1359 rc = CFGMR3InsertString(pLunL0, "Driver", "Block"); CHECK_RC();
1360 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); CHECK_RC();
1361 rc = CFGMR3InsertString(pCfg, "Type", "DVD"); CHECK_RC();
1362 rc = CFGMR3InsertInteger(pCfg, "Mountable", 1); CHECK_RC();
1363
1364 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL1); CHECK_RC();
1365 rc = CFGMR3InsertString(pLunL1, "Driver", "MediaISO"); CHECK_RC();
1366 rc = CFGMR3InsertNode(pLunL1, "Config", &pCfg); CHECK_RC();
1367 rc = CFGMR3InsertString(pCfg, "Path", cdromFile); CHECK_RC();
1368 }
1369
1370 /*
1371 * Network adapters
1372 */
1373 rc = CFGMR3InsertNode(pDevices, "pcnet", &pDev); CHECK_RC();
1374 for (ULONG ulInstance = 0; ulInstance < NetworkAdapterCount; ulInstance++)
1375 {
1376 if (g_aNetDevs[ulInstance].enmType != BFENETDEV::NOT_CONFIGURED)
1377 {
1378 char szInstance[4];
1379 RTStrPrintf(szInstance, sizeof(szInstance), "%lu", ulInstance);
1380 rc = CFGMR3InsertNode(pDev, szInstance, &pInst); CHECK_RC();
1381 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); CHECK_RC();
1382 rc = CFGMR3InsertInteger(pInst, "PCIDeviceNo",
1383 !ulInstance ? 3 : ulInstance - 1 + 8); CHECK_RC();
1384 rc = CFGMR3InsertInteger(pInst, "PCIFunctionNo", 0); CHECK_RC();
1385 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); CHECK_RC();
1386 rc = CFGMR3InsertBytes(pCfg, "MAC", &g_aNetDevs[ulInstance].Mac, sizeof(PDMMAC));
1387 CHECK_RC();
1388
1389 /*
1390 * Enable the packet sniffer if requested.
1391 */
1392 if (g_aNetDevs[ulInstance].fSniff)
1393 {
1394 /* insert the sniffer filter driver. */
1395 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); CHECK_RC();
1396 rc = CFGMR3InsertString(pLunL0, "Driver", "NetSniffer"); CHECK_RC();
1397 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); CHECK_RC();
1398 if (g_aNetDevs[ulInstance].pszSniff)
1399 {
1400 rc = CFGMR3InsertString(pCfg, "File", g_aNetDevs[ulInstance].pszSniff); CHECK_RC();
1401 }
1402 }
1403
1404 /*
1405 * Create the driver config (if any).
1406 */
1407 if (g_aNetDevs[ulInstance].enmType != BFENETDEV::NONE)
1408 {
1409 if (g_aNetDevs[ulInstance].fSniff)
1410 {
1411 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL0); CHECK_RC();
1412 }
1413 else
1414 {
1415 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); CHECK_RC();
1416 }
1417 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); CHECK_RC();
1418 }
1419
1420 /*
1421 * Configure the driver.
1422 */
1423 if (g_aNetDevs[ulInstance].enmType == BFENETDEV::NAT)
1424 {
1425 rc = CFGMR3InsertString(pLunL0, "Driver", "NAT"); CHECK_RC();
1426 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); CHECK_RC();
1427 /* (Port forwarding goes here.) */
1428 }
1429 else if (g_aNetDevs[ulInstance].enmType == BFENETDEV::HIF)
1430 {
1431 rc = CFGMR3InsertString(pLunL0, "Driver", "HostInterface"); CHECK_RC();
1432 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); CHECK_RC();
1433
1434#if defined(__LINUX__)
1435 if (g_aNetDevs[ulInstance].fHaveFd)
1436 {
1437 rc = CFGMR3InsertString(pCfg, "Device", g_aNetDevs[ulInstance].pszName); CHECK_RC();
1438 rc = CFGMR3InsertInteger(pCfg, "FileHandle", g_aNetDevs[ulInstance].fd); CHECK_RC();
1439 }
1440 else
1441#endif
1442 {
1443#if defined (__LINUX__) || defined (__L4__)
1444 /*
1445 * Create/Open the TAP the device.
1446 */
1447 RTFILE tapFD;
1448 rc = RTFileOpen(&tapFD, "/dev/net/tun",
1449 RTFILE_O_READWRITE | RTFILE_O_OPEN |
1450 RTFILE_O_DENY_NONE | RTFILE_O_INHERIT);
1451 if (VBOX_FAILURE(rc))
1452 {
1453 FatalError("Failed to open /dev/net/tun: %Vrc\n", rc);
1454 return rc;
1455 }
1456
1457 struct ifreq IfReq;
1458 memset(&IfReq, 0, sizeof(IfReq));
1459 if (g_aNetDevs[ulInstance].pszName && g_aNetDevs[ulInstance].pszName[0])
1460 {
1461 size_t cch = strlen(g_aNetDevs[ulInstance].pszName);
1462 if (cch >= sizeof(IfReq.ifr_name))
1463 {
1464 FatalError("HIF name too long for device #%d: %s\n",
1465 ulInstance + 1, g_aNetDevs[ulInstance].pszName);
1466 return VERR_BUFFER_OVERFLOW;
1467 }
1468 memcpy(IfReq.ifr_name, g_aNetDevs[ulInstance].pszName, cch + 1);
1469 }
1470 else
1471 strcpy(IfReq.ifr_name, "tun%d");
1472 IfReq.ifr_flags = IFF_TAP | IFF_NO_PI;
1473 rc = ioctl(tapFD, TUNSETIFF, &IfReq);
1474 if (rc)
1475 {
1476 int rc2 = RTErrConvertFromErrno(errno);
1477 FatalError("ioctl TUNSETIFF '%s' failed: errno=%d rc=%d (%Vrc)\n",
1478 IfReq.ifr_name, errno, rc, rc2);
1479 return rc2;
1480 }
1481
1482 rc = fcntl(tapFD, F_SETFL, O_NONBLOCK);
1483 if (rc)
1484 {
1485 int rc2 = RTErrConvertFromErrno(errno);
1486 FatalError("fcntl F_SETFL/O_NONBLOCK '%s' failed: errno=%d rc=%d (%Vrc)\n",
1487 IfReq.ifr_name, errno, rc, rc2);
1488 return rc2;
1489 }
1490
1491 rc = CFGMR3InsertString(pCfg, "Device", g_aNetDevs[ulInstance].pszName); CHECK_RC();
1492 rc = CFGMR3InsertInteger(pCfg, "FileHandle", (RTFILE)tapFD); CHECK_RC();
1493
1494#elif defined(__WIN__)
1495 /*
1496 * We need the GUID too here...
1497 */
1498 rc = CFGMR3InsertString(pCfg, "Device", g_aNetDevs[ulInstance].pszName); CHECK_RC();
1499 rc = CFGMR3InsertString(pCfg, "HostInterfaceName", g_aNetDevs[ulInstance].pszName); CHECK_RC();
1500 rc = CFGMR3InsertString(pCfg, "GUID", g_aNetDevs[ulInstance].pszName /*pszGUID*/); CHECK_RC();
1501
1502
1503#else /* !__LINUX__ && !__L4__ */
1504 FatalError("Name based HIF devices not implemented yet for this host platform\n");
1505 return VERR_NOT_IMPLEMENTED;
1506#endif
1507 }
1508 }
1509 else if (g_aNetDevs[ulInstance].enmType == BFENETDEV::INTNET)
1510 {
1511 /*
1512 * Internal networking.
1513 */
1514 rc = CFGMR3InsertString(pCfg, "Network", g_aNetDevs[ulInstance].pszName); CHECK_RC();
1515 }
1516 }
1517 }
1518
1519 /*
1520 * VMM Device
1521 */
1522 rc = CFGMR3InsertNode(pDevices, "VMMDev", &pDev); CHECK_RC();
1523 rc = CFGMR3InsertNode(pDev, "0", &pInst); CHECK_RC();
1524 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); CHECK_RC();
1525 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ CHECK_RC();
1526 rc = CFGMR3InsertInteger(pInst, "PCIDeviceNo", 4); CHECK_RC();
1527 rc = CFGMR3InsertInteger(pInst, "PCIFunctionNo", 0); CHECK_RC();
1528
1529 /* the VMM device's Main driver */
1530 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); CHECK_RC();
1531 rc = CFGMR3InsertString(pLunL0, "Driver", "MainVMMDev"); CHECK_RC();
1532 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); CHECK_RC();
1533 rc = CFGMR3InsertInteger(pCfg, "Object", (uintptr_t)gVMMDev); CHECK_RC();
1534
1535 /*
1536 * AC'97 ICH audio
1537 */
1538 if (g_fAudio)
1539 {
1540 rc = CFGMR3InsertNode(pDevices, "ichac97", &pDev);
1541 rc = CFGMR3InsertNode(pDev, "0", &pInst);
1542 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ CHECK_RC();
1543 rc = CFGMR3InsertInteger(pInst, "PCIDeviceNo", 5); CHECK_RC();
1544 rc = CFGMR3InsertInteger(pInst, "PCIFunctionNo", 0); CHECK_RC();
1545 rc = CFGMR3InsertNode(pInst, "Config", &pCfg);
1546
1547 /* the Audio driver */
1548 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); CHECK_RC();
1549 rc = CFGMR3InsertString(pLunL0, "Driver", "AUDIO"); CHECK_RC();
1550 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); CHECK_RC();
1551#ifdef __WIN__
1552 rc = CFGMR3InsertString(pCfg, "AudioDriver", "winmm"); CHECK_RC();
1553#elif defined(__DARWIN__)
1554 rc = CFGMR3InsertString(pCfg, "AudioDriver", "coreaudio"); CHECK_RC();
1555#elif defined(__LINUX__)
1556 rc = CFGMR3InsertString(pCfg, "AudioDriver", "oss"); CHECK_RC();
1557#else /* portme */
1558 rc = CFGMR3InsertString(pCfg, "AudioDriver", "none"); CHECK_RC();
1559#endif /* !__WIN__ */
1560 }
1561
1562#ifdef VBOXBFE_WITH_USB
1563 /*
1564 * The USB Controller.
1565 */
1566 if (g_fUSB)
1567 {
1568 rc = CFGMR3InsertNode(pDevices, "usb-ohci", &pDev); CHECK_RC();
1569 rc = CFGMR3InsertNode(pDev, "0", &pInst); CHECK_RC();
1570 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); CHECK_RC();
1571 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ CHECK_RC();
1572 rc = CFGMR3InsertInteger(pInst, "PCIDeviceNo", 6); CHECK_RC();
1573 rc = CFGMR3InsertInteger(pInst, "PCIFunctionNo", 0); CHECK_RC();
1574
1575 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); CHECK_RC();
1576 rc = CFGMR3InsertString(pLunL0, "Driver", "VUSBRootHub"); CHECK_RC();
1577 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); CHECK_RC();
1578 }
1579#endif /* VBOXBFE_WITH_USB */
1580
1581#undef UPDATERC
1582#undef CHECK_RC
1583
1584 return rc;
1585}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette