+# Runs Bochs.
+sub run_bochs {
+ # Select Bochs binary based on the chosen debugger.
+ my ($bin) = $debug eq 'monitor' ? 'bochs-dbg' : 'bochs';
+
+ my ($squish_pty);
+ if ($serial) {
+ $squish_pty = find_in_path ("squish-pty");
+ print "warning: can't find squish-pty, so terminal input will fail\n"
+ if !defined $squish_pty;
+ }
+
+ # Write bochsrc.txt configuration file.
+ open (BOCHSRC, ">", "bochsrc.txt") or die "bochsrc.txt: create: $!\n";
+ print BOCHSRC <<EOF;
+romimage: file=\$BXSHARE/BIOS-bochs-latest
+vgaromimage: file=\$BXSHARE/VGABIOS-lgpl-latest
+boot: disk
+cpu: ips=1000000
+megs: $mem
+log: bochsout.txt
+panic: action=fatal
+# For older bochs:
+#user_shortcut: keys=ctrlaltdel
+# For more recent bochs:
+keyboard: user_shortcut=ctrl-alt-del
+EOF
+ print BOCHSRC "gdbstub: enabled=1, port=$gdb_port\n" if $debug eq 'gdb';
+ print BOCHSRC "clock: sync=", $realtime ? 'realtime' : 'none',
+ ", time0=0\n";
+ print BOCHSRC "ata1: enabled=1, ioaddr1=0x170, ioaddr2=0x370, irq=15\n"
+ if @disks > 2;
+ print_bochs_disk_line ("ata0-master", $disks[0]);
+ print_bochs_disk_line ("ata0-slave", $disks[1]);
+ print_bochs_disk_line ("ata1-master", $disks[2]);
+ print_bochs_disk_line ("ata1-slave", $disks[3]);
+ if ($vga ne 'terminal') {
+ if ($serial) {
+ my $mode = defined ($squish_pty) ? "term" : "file";
+ print BOCHSRC "com1: enabled=1, mode=$mode, dev=/dev/stdout\n";
+ }
+ print BOCHSRC "display_library: nogui\n" if $vga eq 'none';
+ } else {
+ print BOCHSRC "display_library: term\n";
+ }
+ close (BOCHSRC);
+
+ # Compose Bochs command line.
+ my (@cmd) = ($bin, '-q');
+ unshift (@cmd, $squish_pty) if defined $squish_pty;
+ push (@cmd, '-j', $jitter) if defined $jitter;
+
+ # Run Bochs.
+ print join (' ', @cmd), "\n";
+ my ($exit) = xsystem (@cmd);
+ if (WIFEXITED ($exit)) {
+ # Bochs exited normally.
+ # Ignore the exit code; Bochs normally exits with status 1,
+ # which is weird.
+ } elsif (WIFSIGNALED ($exit)) {
+ die "Bochs died with signal ", WTERMSIG ($exit), "\n";
+ } else {
+ die "Bochs died: code $exit\n";
+ }
+}
+
+sub print_bochs_disk_line {
+ my ($device, $disk) = @_;
+ if (defined $disk) {
+ my (%geom) = disk_geometry ($disk);
+ print BOCHSRC "$device: type=disk, path=$disk, mode=flat, ";
+ print BOCHSRC "cylinders=$geom{C}, heads=$geom{H}, spt=$geom{S}, ";
+ print BOCHSRC "translation=none\n";
+ }
+}
+
+# Runs QEMU.
+sub run_qemu {
+ print "warning: qemu doesn't support --terminal\n"
+ if $vga eq 'terminal';
+ print "warning: qemu doesn't support jitter\n"
+ if defined $jitter;
+ my (@cmd) = ('qemu-system-i386');
+ push (@cmd, '-device', 'isa-debug-exit');
+
+ my ($i);
+ for ($i = 0; $i < 4; $i++) {
+ if (defined $disks[$i]) {
+ push (@cmd, '-drive');
+ push (@cmd, "file=$disks[$i],format=raw,index=$i,media=disk");
+ }
+ }
+# push (@cmd, '-hda', $disks[0]) if defined $disks[0];
+# push (@cmd, '-hdb', $disks[1]) if defined $disks[1];
+# push (@cmd, '-hdc', $disks[2]) if defined $disks[2];
+# push (@cmd, '-hdd', $disks[3]) if defined $disks[3];
+ push (@cmd, '-m', $mem);
+ push (@cmd, '-net', 'none');
+ push (@cmd, '-nographic') if $vga eq 'none';
+ push (@cmd, '-serial', 'stdio') if $serial && $vga ne 'none';
+ push (@cmd, '-S') if $debug eq 'monitor';
+ push (@cmd, '-gdb', "tcp::$gdb_port", '-S') if $debug eq 'gdb';
+ push (@cmd, '-monitor', 'null') if $vga eq 'none' && $debug eq 'none';
+ run_command (@cmd);
+}
+
+# player_unsup($flag)
+#
+# Prints a message that $flag is unsupported by VMware Player.
+sub player_unsup {
+ my ($flag) = @_;
+ print "warning: no support for $flag with VMware Player\n";
+}
+
+# Runs VMware Player.
+sub run_player {
+ player_unsup ("--$debug") if $debug ne 'none';
+ player_unsup ("--no-vga") if $vga eq 'none';
+ player_unsup ("--terminal") if $vga eq 'terminal';
+ player_unsup ("--jitter") if defined $jitter;
+ player_unsup ("--timeout"), undef $timeout if defined $timeout;
+ player_unsup ("--kill-on-failure"), undef $kill_on_failure
+ if defined $kill_on_failure;
+
+ $mem = round_up ($mem, 4); # Memory must be multiple of 4 MB.
+
+ open (VMX, ">", "pintos.vmx") or die "pintos.vmx: create: $!\n";
+ chmod 0777 & ~umask, "pintos.vmx";
+ print VMX <<EOF;
+#! /usr/bin/vmware -G
+config.version = 8
+guestOS = "linux"
+memsize = $mem
+floppy0.present = FALSE
+usb.present = FALSE
+sound.present = FALSE
+gui.exitAtPowerOff = TRUE
+gui.exitOnCLIHLT = TRUE
+gui.powerOnAtStartUp = TRUE
+EOF
+
+ print VMX <<EOF if $serial;
+serial0.present = TRUE
+serial0.fileType = "pipe"
+serial0.fileName = "pintos.socket"
+serial0.pipe.endPoint = "client"
+serial0.tryNoRxLoss = "TRUE"
+EOF
+
+ for (my ($i) = 0; $i < 4; $i++) {
+ my ($dsk) = $disks[$i];
+ last if !defined $dsk;
+
+ my ($device) = "ide" . int ($i / 2) . ":" . ($i % 2);
+ my ($pln) = "$device.pln";
+ print VMX <<EOF;
+
+$device.present = TRUE
+$device.deviceType = "plainDisk"
+$device.fileName = "$pln"
+EOF
+
+ open (URANDOM, '<', '/dev/urandom') or die "/dev/urandom: open: $!\n";
+ my ($bytes);
+ sysread (URANDOM, $bytes, 4) == 4 or die "/dev/urandom: read: $!\n";
+ close (URANDOM);
+ my ($cid) = unpack ("L", $bytes);
+
+ my (%geom) = disk_geometry ($dsk);
+ open (PLN, ">", $pln) or die "$pln: create: $!\n";
+ print PLN <<EOF;
+version=1
+CID=$cid
+parentCID=ffffffff
+createType="monolithicFlat"
+
+RW $geom{CAPACITY} FLAT "$dsk" 0
+
+# The Disk Data Base
+#DDB
+
+ddb.adapterType = "ide"
+ddb.virtualHWVersion = "4"
+ddb.toolsVersion = "2"
+ddb.geometry.cylinders = "$geom{C}"
+ddb.geometry.heads = "$geom{H}"
+ddb.geometry.sectors = "$geom{S}"
+EOF
+ close (PLN);
+ }
+ close (VMX);
+
+ my ($squish_unix);
+ if ($serial) {
+ $squish_unix = find_in_path ("squish-unix");
+ print "warning: can't find squish-unix, so terminal input ",
+ "and output will fail\n" if !defined $squish_unix;
+ }
+
+ my ($vmx) = getcwd () . "/pintos.vmx";
+ my (@cmd) = ("vmplayer", $vmx);
+ unshift (@cmd, $squish_unix, "pintos.socket") if $squish_unix;
+ print join (' ', @cmd), "\n";
+ xsystem (@cmd);