+ print_bochs_disk_line ("ata0-master", 0);
+ print_bochs_disk_line ("ata0-slave", 1);
+ if (defined ($disks_by_iface[2]{FILE_NAME})
+ || defined ($disks_by_iface[3]{FILE_NAME})) {
+ print BOCHSRC "ata1: enabled=1, ioaddr1=0x170, ",
+ "ioaddr2=0x370, irq=15\n";
+ print_bochs_disk_line ("ata1-master", 2);
+ print_bochs_disk_line ("ata1-slave", 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";
+ }
+}
+
+# print_bochs_disk_line($device, $iface)
+#
+# If IDE interface $iface has a disk attached, prints a bochsrc.txt
+# line for attaching it to $device.
+sub print_bochs_disk_line {
+ my ($device, $iface) = @_;
+ my ($disk) = $disks_by_iface[$iface];
+ my ($file) = $disk->{FILE_NAME};
+ if (defined $file) {
+ my (%geom) = disk_geometry ($disk);
+ print BOCHSRC "$device: type=disk, path=$file, 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');
+ for my $iface (0...3) {
+ my ($option) = ('-hda', '-hdb', '-hdc', '-hdd')[$iface];
+ push (@cmd, $option, $disks_by_iface[$iface]{FILE_NAME})
+ if defined $disks_by_iface[$iface]{FILE_NAME};
+ }
+ 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, '-s', '-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;
+
+ # Memory size must be multiple of 4.
+ $mem = int (($mem + 3) / 4) * 4;
+
+ 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 ($disk) = $disks_by_iface[$i];
+ my ($dsk) = $disk->{FILE_NAME};
+ next 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 ($disk);
+ 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);
+}
+\f
+# Disk utilities.
+
+# open_disk($disk)
+#
+# Opens $disk, if it is not already open, and returns its file handle
+# and file name.
+sub open_disk {
+ my ($disk) = @_;
+ if (!defined ($disk->{HANDLE})) {
+ if ($disk->{FILE_NAME}) {
+ sysopen ($disk->{HANDLE}, $disk->{FILE_NAME}, O_RDWR)
+ or die "$disk->{FILE_NAME}: open: $!\n";
+ } else {
+ ($disk->{HANDLE}, $disk->{FILE_NAME}) = tempfile (UNLINK => 1,
+ SUFFIX => '.dsk');
+ }
+ }
+ return ($disk->{HANDLE}, $disk->{FILE_NAME});