+# 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]{FILENAME})
+ if defined $disks_by_iface[$iface]{FILENAME};
+ }
+ push (@cmd, '-m', $mem);
+ push (@cmd, '-nographic') if $vga eq 'none';
+ push (@cmd, '-serial', 'stdio') if $serial_out && $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);
+}
+
+# gsx_unsup($flag)
+#
+# Prints a message that $flag is unsupported by GSX Server.
+sub gsx_unsup {
+ my ($flag) = @_;
+ print "warning: no support for $flag with VMware GSX Server\n";
+}
+
+# Runs VMware GSX Server.
+sub run_gsx {
+ gsx_unsup ("--$debug") if $debug ne 'none';
+ gsx_unsup ("--no-vga") if $vga eq 'none';
+ gsx_unsup ("--terminal") if $vga eq 'terminal';
+ gsx_unsup ("--jitter") if defined $jitter;
+
+ unlink ("pintos.out");
+
+ open (VMX, ">", "pintos.vmx") or die "pintos.vmx: create: $!\n";
+ chmod 0777 & ~umask, "pintos.vmx";
+ print VMX <<EOF;
+#! /usr/bin/vmware -G
+config.version = 6
+guestOS = "linux"
+floppy0.present = FALSE
+memsize = $mem
+
+serial0.present = TRUE
+serial0.fileType = "file"
+serial0.fileName = "pintos.out"
+EOF
+
+ for (my ($i) = 0; $i < 4; $i++) {
+ my ($dsk) = $disks_by_iface[$i]{FILENAME};
+ next if !defined $dsk;
+
+ my ($pln) = $dsk;
+ $pln =~ s/\.dsk//;
+ $pln .= ".pln";
+
+ my ($device) = "ide" . int ($i / 2) . ":" . ($i % 2);
+ print VMX <<EOF;
+
+$device.present = TRUE
+$device.deviceType = "plainDisk"
+$device.fileName = "$pln"
+EOF
+
+ my (%geom) = disk_geometry ($dsk);
+ open (PLN, ">", $pln) or die "$pln: create: $!\n";
+ print PLN <<EOF;
+DRIVETYPE ide
+#vm|VERSION 2
+#vm|TOOLSVERSION 2
+CYLINDERS $geom{C}
+HEADS $geom{H}
+SECTORS $geom{S}
+#vm|CAPACITY $geom{CAPACITY}
+ACCESS "$dsk" 0 $geom{CAPACITY}
+EOF
+ close (PLN);
+ }
+ close (VMX);
+
+ my ($vmx) = getcwd () . "/pintos.vmx";
+ system ("vmware-cmd -s register $vmx >&/dev/null");
+ system ("vmware-cmd $vmx stop hard >&/dev/null");
+ system ("vmware -l -G -x -q $vmx");
+ system ("vmware-cmd $vmx stop hard >&/dev/null");
+ system ("vmware-cmd -s unregister $vmx >&/dev/null");
+}
+\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->{FILENAME}) {
+ sysopen ($disk->{HANDLE}, $disk->{FILENAME}, O_RDWR)
+ or die "$disk->{FILENAME}: open: $!\n";
+ } else {
+ ($disk->{HANDLE}, $disk->{FILENAME}) = tempfile (UNLINK => 1,
+ SUFFIX => '.dsk');
+ }
+ }
+ return ($disk->{HANDLE}, $disk->{FILENAME});
+}
+
+# open_disk_copy($disk)
+#
+# Makes a temporary copy of $disk and returns its file handle and file name.
+sub open_disk_copy {
+ my ($disk) = @_;
+ die if !$disk->{FILENAME};
+
+ my ($orig_handle, $orig_filename) = open_disk ($disk);
+ my ($cp_handle, $cp_filename) = tempfile (UNLINK => 1, SUFFIX => '.dsk');
+ copy_file ($orig_handle, $orig_filename, $cp_handle, $cp_filename,
+ -s $orig_handle);
+ return ($disk->{HANDLE}, $disk->{FILENAME}) = ($cp_handle, $cp_filename);
+}
+
+# extend_disk($disk, $size)
+#
+# Extends $disk, if necessary, so that it is at least $size bytes
+# long.
+sub extend_disk {
+ my ($disk, $size) = @_;
+ my ($handle, $filename) = open_disk ($disk);
+ if (-s ($handle) < $size) {
+ sysseek ($handle, $size - 1, 0) == $size - 1
+ or die "$filename: seek: $!\n";
+ syswrite ($handle, "\0") == 1
+ or die "$filename: write: $!\n";
+ }
+}
+
+# disk_geometry($file)
+#
+# Examines $file and returns a valid IDE disk geometry for it, as a
+# hash.