+ my ($cyl_size) = 512 * 16 * 63;
+ my ($cylinders) = ceil ($size / $cyl_size);
+ extend_disk ($disk, $cylinders * $cyl_size) if $size % $cyl_size;
+
+ return (CAPACITY => $size / 512,
+ C => $cylinders,
+ H => 16,
+ S => 63);
+}
+
+# copy_file($from_handle, $from_file_name, $to_handle, $to_file_name, $size)
+#
+# Copies $size bytes from $from_handle to $to_handle.
+# $from_file_name and $to_file_name are used in error messages.
+sub copy_file {
+ my ($from_handle, $from_file_name, $to_handle, $to_file_name, $size) = @_;
+
+ while ($size > 0) {
+ my ($chunk_size) = 4096;
+ $chunk_size = $size if $chunk_size > $size;
+ $size -= $chunk_size;
+
+ my ($data) = read_fully ($from_handle, $from_file_name, $chunk_size);
+ write_fully ($to_handle, $to_file_name, $data);
+ }
+}
+
+# read_fully($handle, $file_name, $bytes)
+#
+# Reads exactly $bytes bytes from $handle and returns the data read.
+# $file_name is used in error messages.
+sub read_fully {
+ my ($handle, $file_name, $bytes) = @_;
+ my ($data);
+ my ($read_bytes) = sysread ($handle, $data, $bytes);
+ die "$file_name: read: $!\n" if !defined $read_bytes;
+ die "$file_name: unexpected end of file\n" if $read_bytes != $bytes;
+ return $data;
+}
+
+# write_fully($handle, $file_name, $data)
+#
+# Write $data to $handle.
+# $file_name is used in error messages.
+sub write_fully {
+ my ($handle, $file_name, $data) = @_;
+ my ($written_bytes) = syswrite ($handle, $data);
+ die "$file_name: write: $!\n" if !defined $written_bytes;
+ die "$file_name: short write\n" if $written_bytes != length $data;
+}
+\f
+# Subprocess utilities.
+
+# run_command(@args)
+#
+# Runs xsystem(@args).
+# Also prints the command it's running and checks that it succeeded.
+sub run_command {
+ print join (' ', @_), "\n";
+ die "command failed\n" if xsystem (@_);
+}
+
+# xsystem(@args)
+#
+# Creates a subprocess via exec(@args) and waits for it to complete.
+# 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.
+ 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);
+
+ 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);
+ print "\nTIMEOUT after $timeout seconds of host CPU time\n";
+ exit 0;
+ }
+
+ return $?;
+ }
+}
+
+# relay_signal($pid, $signal, &$cleanup)
+#
+# Relays $signal to $pid and then reinvokes it for us with the default
+# handler. Also cleans up temporary files and invokes $cleanup.
+sub relay_signal {
+ my ($pid, $signal, $cleanup) = @_;
+ kill $signal, $pid;
+ eval { File::Temp::cleanup() }; # Not defined in old File::Temp.
+ &$cleanup ();
+ $SIG{$signal} = 'DEFAULT';
+ kill $signal, getpid ();
+}
+
+# timeout($pid, $cause, &$cleanup)
+#
+# Interrupts $pid and dies with a timeout error message,
+# after invoking $cleanup.
+sub timeout {
+ my ($pid, $cause, $cleanup) = @_;
+ kill "INT", $pid;
+ waitpid ($pid, 0);
+ &$cleanup ();
+ seek (STDOUT, 0, 2);
+ 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;
+}
+
+# Returns the system load average over the last minute.
+# If the load average is less than 1.0 or cannot be determined, returns 1.0.
+sub get_load_average {
+ my ($avg) = `uptime` =~ /load average:\s*([^,]+),/;
+ return $avg >= 1.0 ? $avg : 1.0;
+}
+
+# Calls setitimer to set a timeout, then execs what was passed to us.
+sub exec_setitimer {
+ if (defined $timeout) {
+ if ($\16 ge 5.8.0) {
+ eval "
+ use Time::HiRes qw(setitimer ITIMER_VIRTUAL);
+ setitimer (ITIMER_VIRTUAL, $timeout, 0);
+ ";
+ } else {
+ { exec ("setitimer-helper", $timeout, @_); };
+ exit 1 if !$!{ENOENT};
+ print STDERR "warning: setitimer-helper is not installed, so ",
+ "CPU time limit will not be enforced\n";
+ }
+ }
+ exec (@_);
+ exit (1);
+}
+
+sub SIGVTALRM {
+ use Config;
+ my $i = 0;
+ foreach my $name (split(' ', $Config{sig_name})) {
+ return $i if $name eq 'VTALRM';
+ $i++;
+ }
+ 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;