Fix bug in --sim and --debug options reported by Bob Lantz.
[pintos-anon] / src / utils / pintos
index 13d7e5436fbe77a79d18ac1d81b02083214fa8de..94132052b30e4b22feba396b0f58b8b57b928a2c 100755 (executable)
@@ -8,10 +8,10 @@ use Getopt::Long qw(:config bundling);
 
 # Command-line options.
 our ($start_time) = time ();
-our ($sim);                    # Simulator: bochs, qemu, or gsx.
+our ($sim);                    # Simulator: bochs, qemu, or player.
 our ($debug) = "none";         # Debugger: none, monitor, or gdb.
 our ($mem) = 4;                        # Physical RAM in MB.
-our ($serial_out) = 1;         # Send output to serial port?
+our ($serial) = 1;             # Use serial port for input and output?
 our ($vga);                    # VGA output: window, terminal, or none.
 our ($jitter);                 # Seed for random timer interrupts, if set.
 our ($realtime);               # Synchronize timer interrupts with real time?
@@ -46,25 +46,25 @@ sub parse_command_line {
        while ((my $arg = shift (@kernel_args)) ne '--') {
            push (@ARGV, $arg);
        }
-       GetOptions ("sim=s" => sub { set_sim (@_) },
+       GetOptions ("sim=s" => sub { set_sim ($_[1]) },
                    "bochs" => sub { set_sim ("bochs") },
                    "qemu" => sub { set_sim ("qemu") },
-                   "gsx" => sub { set_sim ("gsx") },
+                   "player" => sub { set_sim ("player") },
 
-                   "debug=s" => sub { set_debug (@_) },
+                   "debug=s" => sub { set_debug ($_[1]) },
                    "no-debug" => sub { set_debug ("none") },
                    "monitor" => sub { set_debug ("monitor") },
                    "gdb" => sub { set_debug ("gdb") },
 
                    "m|memory=i" => \$mem,
-                   "j|jitter=i" => sub { set_jitter (@_) },
+                   "j|jitter=i" => sub { set_jitter ($_[1]) },
                    "r|realtime" => sub { set_realtime () },
 
                    "T|timeout=i" => \$timeout,
                    "k|kill-on-failure" => \$kill_on_failure,
 
                    "v|no-vga" => sub { set_vga ('none'); },
-                   "s|no-serial" => sub { $serial_out = 0; },
+                   "s|no-serial" => sub { $serial = 0; },
                    "t|terminal" => sub { set_vga ('terminal'); },
 
                    "p|put-file=s" => sub { add_file (\@puts, $_[1]); },
@@ -92,8 +92,8 @@ sub parse_command_line {
     undef $timeout, print "warning: disabling timeout with --$debug\n"
       if defined ($timeout) && $debug ne 'none';
 
-    print "warning: enabling serial output for -k or --kill-on-failure\n"
-      if $kill_on_failure && !$serial_out;
+    print "warning: enabling serial port for -k or --kill-on-failure\n"
+      if $kill_on_failure && !$serial;
 }
 
 # usage($exitcode).
@@ -108,15 +108,15 @@ where each OPTION is one of the following options
   and each ARGUMENT is passed to Pintos kernel verbatim.
 Simulator selection:
   --bochs                  (default) Use Bochs as simulator
-  --qemu                   Use qemu as simulator
-  --gsx                    Use VMware GSX Server 3.x as simulator
+  --qemu                   Use QEMU as simulator
+  --player                 Use VMware Player as simulator
 Debugger selection:
   --no-debug               (default) No debugger
   --monitor                Debug with simulator's monitor
   --gdb                    Debug with gdb
 Display options: (default is both VGA and serial)
-  -v, --no-vga             No VGA display
-  -s, --no-serial          No serial output
+  -v, --no-vga             No VGA display or keyboard
+  -s, --no-serial          No serial input or output
   -t, --terminal           Display VGA in terminal (Bochs only)
 Timing options: (Bochs only)
   -j SEED                  Randomize timer interrupts
@@ -268,7 +268,15 @@ sub finish_scratch_disk {
     }
 
     # Read each file.
-    get_scratch_file (defined ($_->[1]) ? $_->[1] : $_->[0]) foreach @gets;
+    # If reading fails, delete that file and all subsequent files.
+    my ($ok) = 1;
+    foreach my $get (@gets) {
+       my ($name) = defined ($get->[1]) ? $get->[1] : $get->[0];
+       $ok &&= get_scratch_file ($name);
+       if (!$ok) {
+           die "$name: unlink: $!\n" if !unlink ($name) && !$!{ENOENT};
+       }
+    }
 }
 
 # put_scratch_file($file).
@@ -303,6 +311,7 @@ sub put_scratch_file {
 # get_scratch_file($file).
 #
 # Copies from the scratch disk to $file.
+# Returns 1 if successful, 0 on failure.
 sub get_scratch_file {
     my ($get_file_name) = @_;
     my ($disk_handle, $disk_file_name) = open_disk ($disks{SCRATCH});
@@ -313,12 +322,13 @@ sub get_scratch_file {
     # the file size.
     my ($metadata) = read_fully ($disk_handle, $disk_file_name, 512);
     my ($signature, $size) = unpack ("a4 V", $metadata);
-    die "bad signature reading scratch disk--did Pintos run correctly?\n"
-      if $signature ne "GET\0";
+    (print STDERR "bad signature on scratch disk--did Pintos run fail?\n"),
+      return 0
+       if $signature ne "GET\0";
 
     # Copy file data.
     my ($get_handle);
-    sysopen ($get_handle, $get_file_name, O_WRONLY | O_CREAT | O_EXCL, 0666)
+    sysopen ($get_handle, $get_file_name, O_WRONLY | O_CREAT, 0666)
       or die "$get_file_name: create: $!\n";
     copy_file ($disk_handle, $disk_file_name, $get_handle, $get_file_name,
               $size);
@@ -327,6 +337,8 @@ sub get_scratch_file {
     # Skip forward in disk up to beginning of next sector.
     read_fully ($disk_handle, $disk_file_name, 512 - $size % 512)
       if $size % 512;
+
+    return 1;
 }
 \f
 # Prepares the arguments to pass to the Pintos kernel,
@@ -366,8 +378,8 @@ sub run_vm {
        run_bochs ();
     } elsif ($sim eq 'qemu') {
        run_qemu ();
-    } elsif ($sim eq 'gsx') {
-       run_gsx ();
+    } elsif ($sim eq 'player') {
+       run_player ();
     } else {
        die "unknown simulator `$sim'\n";
     }
@@ -378,6 +390,13 @@ 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;
@@ -391,7 +410,7 @@ panic: action=fatal
 EOF
     print BOCHSRC "gdbstub: enabled=1\n" if $debug eq 'gdb';
     print BOCHSRC "clock: sync=", $realtime ? 'realtime' : 'none',
-      " time0=0\n";
+      ", time0=0\n";
     print_bochs_disk_line ("ata0-master", 0);
     print_bochs_disk_line ("ata0-slave", 1);
     if (defined ($disks_by_iface[2]{FILE_NAME})
@@ -402,8 +421,10 @@ EOF
        print_bochs_disk_line ("ata1-slave", 3);
     }
     if ($vga ne 'terminal') {
-       print BOCHSRC "com1: enabled=1, mode=file, dev=/dev/stdout\n"
-         if $serial_out;
+       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";
@@ -412,6 +433,7 @@ EOF
 
     # 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.
@@ -444,7 +466,7 @@ sub print_bochs_disk_line {
     }
 }
 
-# Runs qemu.
+# Runs QEMU.
 sub run_qemu {
     print "warning: qemu doesn't support --terminal\n"
       if $vga eq 'terminal';
@@ -459,43 +481,57 @@ sub run_qemu {
     push (@cmd, '-m', $mem);
     push (@cmd, '-net', 'none');
     push (@cmd, '-nographic') if $vga eq 'none';
-    push (@cmd, '-serial', 'stdio') if $serial_out && $vga ne '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);
 }
 
-# gsx_unsup($flag)
+# player_unsup($flag)
 #
-# Prints a message that $flag is unsupported by GSX Server.
-sub gsx_unsup {
+# Prints a message that $flag is unsupported by VMware Player.
+sub player_unsup {
     my ($flag) = @_;
-    print "warning: no support for $flag with VMware GSX Server\n";
+    print "warning: no support for $flag with VMware Player\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;
-    gsx_unsup ("--kill-on-failure") if defined $kill_on_failure;
+# 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;
 
-    unlink ("pintos.out");
+    # 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 = 6
+config.version = 8
 guestOS = "linux"
-floppy0.present = FALSE
 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 = "file"
-serial0.fileName = "pintos.out"
+serial0.fileType = "pipe"
+serial0.fileName = "pintos.socket"
+serial0.pipe.endPoint = "client"
+serial0.tryNoRxLoss = "TRUE"
 EOF
 
     for (my ($i) = 0; $i < 4; $i++) {
@@ -503,11 +539,8 @@ EOF
        my ($dsk) = $disk->{FILE_NAME};
        next if !defined $dsk;
 
-       my ($pln) = $dsk;
-       $pln =~ s/\.dsk//;
-       $pln .= ".pln";
-
        my ($device) = "ide" . int ($i / 2) . ":" . ($i % 2);
+       my ($pln) = "$device.pln";
        print VMX <<EOF;
 
 $device.present = TRUE
@@ -515,28 +548,48 @@ $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;
-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}
+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";
-    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");
+    my (@cmd) = ("vmplayer", $vmx);
+    unshift (@cmd, $squish_unix, "pintos.socket") if $squish_unix;
+    print join (' ', @cmd), "\n";
+    xsystem (@cmd);
 }
 \f
 # Disk utilities.
@@ -666,7 +719,7 @@ sub run_command {
 # Relays common signals to the subprocess.
 # If $timeout is set then the subprocess will be killed after that long.
 sub xsystem {
-    # qemu turns off local echo and does not restore it if killed by a signal.
+    # QEMU turns off local echo and does not restore it if killed by a signal.
     # We compensate by restoring it ourselves.
     my $cleanup = sub {};
     if (isatty (0)) {
@@ -754,7 +807,7 @@ sub xsystem {
 sub relay_signal {
     my ($pid, $signal, $cleanup) = @_;
     kill $signal, $pid;
-    File::Temp::cleanup();
+    eval { File::Temp::cleanup() };    # Not defined in old File::Temp.
     &$cleanup ();
     $SIG{$signal} = 'DEFAULT';
     kill $signal, getpid ();
@@ -817,3 +870,13 @@ sub SIGVTALRM {
     }
     return 0;
 }
+
+# find_in_path ($program)
+#
+# Searches for $program in $ENV{PATH}.
+# Returns $program if found, otherwise undef.
+sub find_in_path {
+    my ($program) = @_;
+    -x "$_/$program" and return $program foreach split (':', $ENV{PATH});
+    return;
+}