X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?p=pintos-anon;a=blobdiff_plain;f=src%2Futils%2Fpintos;h=fca610a15c83d6c4cdd1e80062d04b1812f5d1f8;hp=19ef70f650954680a8507bad26e5364c2e8ec226;hb=8349a4cbeec56e4b497d98064ce51aba80c1f258;hpb=c0206643b024b6f0e6cde1cbb5e7d37abbc84c69 diff --git a/src/utils/pintos b/src/utils/pintos index 19ef70f..fca610a 100755 --- a/src/utils/pintos +++ b/src/utils/pintos @@ -8,14 +8,15 @@ use Getopt::Long qw(:config bundling); # Command-line options. our ($start_time) = time (); -our ($sim) = $ENV{PINTOSSIM}; # 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? our ($timeout); # Maximum runtime in seconds, if set. +our ($kill_on_failure); # Abort quickly on test failure? our (@puts); # Files to copy into the VM. our (@gets); # Files to copy out of the VM. our ($as_ref); # Reference to last addition to @gets or @puts. @@ -45,23 +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]); }, @@ -86,8 +89,11 @@ sub parse_command_line { $debug = "none" if !defined $debug; $vga = "window" if !defined $vga; - print "warning: -T or --timeout should not be used with --$debug\n" + undef $timeout, print "warning: disabling timeout with --$debug\n" if defined ($timeout) && $debug ne 'none'; + + print "warning: enabling serial port for -k or --kill-on-failure\n" + if $kill_on_failure && !$serial; } # usage($exitcode). @@ -102,21 +108,24 @@ 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 -r, --realtime Use realistic, not reproducible, timings +Testing options: -T, --timeout=N Kill Pintos after N seconds CPU time or N*load_avg seconds wall-clock time (whichever comes first) + -k, --kill-on-failure Kill Pintos a few seconds after a kernel or user + panic, test failure, or triple fault Configuration options: -m, --mem=N Give Pintos N MB physical RAM (default: 4) File system commands (for `run' command): @@ -130,8 +139,6 @@ Disk options: (name an existing FILE or specify SIZE in MB for a temp disk) --swap-disk=FILE|SIZE Set swap disk file (default: swap.dsk) Other options: -h, --help Display this help message. -Environment variables: - PINTOSSIM Select default simulator. EOF exit $exitcode; } @@ -245,8 +252,16 @@ sub find_disks { # Prepare the scratch disk for gets and puts. sub prepare_scratch_disk { - # Copy the files to put onto the scratch disk. - put_scratch_file ($_->[0]) foreach @puts; + if (@puts) { + # Write ustar header and data for each file. + put_scratch_file ($_->[0], + defined $_->[1] ? $_->[1] : $_->[0]) + foreach @puts; + + # Write end-of-archive marker. + write_fully ($disks{SCRATCH}{HANDLE}, $disks{SCRATCH}{FILE_NAME}, + "\0" x 1024); + } # Make sure the scratch disk is big enough to get big files. extend_disk ($disks{SCRATCH}, @gets * 1024 * 1024) if @gets; @@ -261,31 +276,90 @@ 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]; + my ($error) = get_scratch_file ($name); + if ($error) { + print STDERR "getting $name failed ($error)\n"; + die "$name: unlink: $!\n" if !unlink ($name) && !$!{ENOENT}; + $ok = 0; + } + } } -# put_scratch_file($file). +# mk_ustar_field($number, $size) +# +# Returns $number in a $size-byte numeric field in the format used by +# the standard ustar archive header. +sub mk_ustar_field { + my ($number, $size) = @_; + my ($len) = $size - 1; + my ($out) = sprintf ("%0${len}o", $number) . "\0"; + die "$number: too large for $size-byte octal ustar field\n" + if length ($out) != $size; + return $out; +} + +# calc_ustar_chksum($s) # -# Copies $file into the scratch disk. +# Calculates and returns the ustar checksum of 512-byte ustar archive +# header $s. +sub calc_ustar_chksum { + my ($s) = @_; + die if length ($s) != 512; + substr ($s, 148, 8, ' ' x 8); + return unpack ("%32a*", $s); +} + +# put_scratch_file($src_file_name, $dst_file_name). +# +# Copies $src_file_name into the scratch disk for extraction as +# $dst_file_name. sub put_scratch_file { - my ($put_file_name) = @_; + my ($src_file_name, $dst_file_name) = @_; my ($disk_handle, $disk_file_name) = open_disk ($disks{SCRATCH}); - print "Copying $put_file_name into $disk_file_name...\n"; + print "Copying $src_file_name to scratch partition...\n"; - # Write metadata sector, which consists of a 4-byte signature - # followed by the file size. - stat $put_file_name or die "$put_file_name: stat: $!\n"; + # ustar format supports up to 100 characters for a file name, and + # even longer names given some common properties, but our code in + # the Pintos kernel only supports at most 99 characters. + die "$dst_file_name: name too long (max 99 characters)\n" + if length ($dst_file_name) > 99; + + # Compose and write ustar header. + stat $src_file_name or die "$src_file_name: stat: $!\n"; my ($size) = -s _; - my ($metadata) = pack ("a4 V x504", "PUT\0", $size); - write_fully ($disk_handle, $disk_file_name, $metadata); + my ($header) = (pack ("a100", $dst_file_name) # name + . mk_ustar_field (0644, 8) # mode + . mk_ustar_field (0, 8) # uid + . mk_ustar_field (0, 8) # gid + . mk_ustar_field ($size, 12) # size + . mk_ustar_field (1136102400, 12) # mtime + . (' ' x 8) # chksum + . '0' # typeflag + . ("\0" x 100) # linkname + . "ustar\0" # magic + . "00" # version + . "root" . ("\0" x 28) # uname + . "root" . ("\0" x 28) # gname + . "\0" x 8 # devmajor + . "\0" x 8 # devminor + . ("\0" x 155)) # prefix + . "\0" x 12; # pad to 512 bytes + substr ($header, 148, 8) = mk_ustar_field (calc_ustar_chksum ($header), 8); + write_fully ($disk_handle, $disk_file_name, $header); # Copy file data. my ($put_handle); - sysopen ($put_handle, $put_file_name, O_RDONLY) - or die "$put_file_name: open: $!\n"; - copy_file ($put_handle, $put_file_name, $disk_handle, $disk_file_name, + sysopen ($put_handle, $src_file_name, O_RDONLY) + or die "$src_file_name: open: $!\n"; + copy_file ($put_handle, $src_file_name, $disk_handle, $disk_file_name, $size); + die "$src_file_name: changed size while being read\n" + if $size != -s $put_handle; close ($put_handle); # Round up disk data to beginning of next sector. @@ -296,22 +370,38 @@ 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}); print "Copying $get_file_name out of $disk_file_name...\n"; - # Read metadata sector, which has a 4-byte signature followed by - # 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"; + # Read ustar header sector. + my ($header) = read_fully ($disk_handle, $disk_file_name, 512); + return "scratch disk tar archive ends unexpectedly" + if $header eq ("\0" x 512); + + # Verify magic numbers. + return "corrupt ustar signature" if substr ($header, 257, 6) ne "ustar\0"; + return "invalid ustar version" if substr ($header, 263, 2) ne '00'; + + # Verify checksum. + my ($chksum) = oct (unpack ("Z*", substr ($header, 148, 8))); + my ($correct_chksum) = calc_ustar_chksum ($header); + return "checksum mismatch" if $chksum != $correct_chksum; + + # Get type. + my ($typeflag) = substr ($header, 156, 1); + return "not a regular file" if $typeflag ne '0' && $typeflag ne "\0"; + + # Get size. + my ($size) = oct (unpack ("Z*", substr ($header, 124, 12))); + return "bad size $size\n" if $size < 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); @@ -320,6 +410,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 0; } # Prepares the arguments to pass to the Pintos kernel, @@ -328,9 +420,9 @@ sub prepare_arguments { my (@args); push (@args, shift (@kernel_args)) while @kernel_args && $kernel_args[0] =~ /^-/; - push (@args, 'put', defined $_->[1] ? $_->[1] : $_->[0]) foreach @puts; + push (@args, 'extract') if @puts; push (@args, @kernel_args); - push (@args, 'get', $_->[0]) foreach @gets; + push (@args, 'append', $_->[0]) foreach @gets; write_cmd_line ($disks{OS}, @args); } @@ -359,8 +451,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"; } @@ -371,10 +463,17 @@ 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 <", "pintos.vmx") or die "pintos.vmx: create: $!\n"; chmod 0777 & ~umask, "pintos.vmx"; print VMX <{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 <", $pln) or die "$pln: create: $!\n"; print PLN <&/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); } # Disk utilities. @@ -657,21 +795,76 @@ 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. + # We compensate by restoring it ourselves. + my $cleanup = sub {}; + if (isatty (0)) { + my $termios = POSIX::Termios->new; + $termios->getattr (0); + $cleanup = sub { $termios->setattr (0, &POSIX::TCSANOW); } + } + + # Create pipe for filtering output. + pipe (my $in, my $out) or die "pipe: $!\n" if $kill_on_failure; + my ($pid) = fork; if (!defined ($pid)) { # Fork failed. die "fork: $!\n"; } elsif (!$pid) { # Running in child process. + dup2 (fileno ($out), STDOUT_FILENO) or die "dup2: $!\n" + if $kill_on_failure; exec_setitimer (@_); } else { # Running in parent process. - local $SIG{ALRM} = sub { timeout ($pid); }; - local $SIG{INT} = sub { relay_signal ($pid, "INT"); }; - local $SIG{TERM} = sub { relay_signal ($pid, "TERM"); }; + close $out if $kill_on_failure; + + my ($cause); + local $SIG{ALRM} = sub { timeout ($pid, $cause, $cleanup); }; + local $SIG{INT} = sub { relay_signal ($pid, "INT", $cleanup); }; + local $SIG{TERM} = sub { relay_signal ($pid, "TERM", $cleanup); }; alarm ($timeout * get_load_average () + 1) if defined ($timeout); - waitpid ($pid, 0); + + if ($kill_on_failure) { + # Filter output. + my ($buf) = ""; + my ($boots) = 0; + local ($|) = 1; + for (;;) { + if (waitpid ($pid, WNOHANG) != 0) { + # Subprocess died. Pass through any remaining data. + print $buf while sysread ($in, $buf, 4096) > 0; + last; + } + + # Read and print out pipe data. + my ($len) = length ($buf); + waitpid ($pid, 0), last + if sysread ($in, $buf, 4096, $len) <= 0; + print substr ($buf, $len); + + # Remove full lines from $buf and scan them for keywords. + while ((my $idx = index ($buf, "\n")) >= 0) { + local $_ = substr ($buf, 0, $idx + 1, ''); + next if defined ($cause); + if (/(Kernel PANIC|User process ABORT)/ ) { + $cause = "\L$1\E"; + alarm (5); + } elsif (/Pintos booting/ && ++$boots > 1) { + $cause = "triple fault"; + alarm (5); + } elsif (/FAILED/) { + $cause = "test failure"; + alarm (5); + } + } + } + } else { + waitpid ($pid, 0); + } alarm (0); + &$cleanup (); if (WIFSIGNALED ($?) && WTERMSIG ($?) == SIGVTALRM ()) { seek (STDOUT, 0, 2); @@ -683,31 +876,38 @@ sub xsystem { } } -# relay_signal($pid, $signal) +# relay_signal($pid, $signal, &$cleanup) # # Relays $signal to $pid and then reinvokes it for us with the default -# handler. Also cleans up temporary files. +# handler. Also cleans up temporary files and invokes $cleanup. sub relay_signal { - my ($pid, $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 (); } -# timeout($pid) +# timeout($pid, $cause, &$cleanup) # -# Interrupts $pid and dies with a timeout error message. +# Interrupts $pid and dies with a timeout error message, +# after invoking $cleanup. sub timeout { - my ($pid) = @_; + my ($pid, $cause, $cleanup) = @_; kill "INT", $pid; waitpid ($pid, 0); + &$cleanup (); seek (STDOUT, 0, 2); - my ($load_avg) = `uptime` =~ /(load average:.*)$/i; - print "\nTIMEOUT after ", time () - $start_time, - " seconds of wall-clock time"; - print " - $load_avg" if defined $load_avg; - print "\n"; + if (!defined ($cause)) { + my ($load_avg) = `uptime` =~ /(load average:.*)$/i; + print "\nTIMEOUT after ", time () - $start_time, + " seconds of wall-clock time"; + print " - $load_avg" if defined $load_avg; + print "\n"; + } else { + print "Simulation terminated due to $cause.\n"; + } exit 0; } @@ -746,3 +946,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; +}