use Getopt::Long qw(:config bundling);
# Command-line options.
-our ($sim); # Simulator: bochs, qemu, or gsx.
+our ($start_time) = time ();
+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.
our (@kernel_args); # Arguments to pass to kernel.
-our (%disks) = (OS => {FILENAME => 'os.dsk'}, # Disks to give VM.
+our (%disks) = (OS => {DEF_FN => 'os.dsk'}, # Disks to give VM.
FS => {DEF_FN => 'fs.dsk'},
SCRATCH => {DEF_FN => 'scratch.dsk'},
SWAP => {DEF_FN => 'swap.dsk'});
GetOptions ("sim=s" => sub { set_sim (@_) },
"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 (@_) },
"no-debug" => sub { set_debug ("none") },
"m|memory=i" => \$mem,
"j|jitter=i" => sub { set_jitter (@_) },
"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]); },
"h|help" => sub { usage (0); },
- "os-disk=s" => \$disks{OS}{FILENAME},
- "fs-disk=s" => \$disks{FS}{FILENAME},
- "scratch-disk=s" => \$disks{SCRATCH}{FILENAME},
- "swap-disk=s" => \$disks{SWAP}{FILENAME},
+ "os-disk=s" => \$disks{OS}{FILE_NAME},
+ "fs-disk=s" => \$disks{FS}{FILE_NAME},
+ "scratch-disk=s" => \$disks{SCRATCH}{FILE_NAME},
+ "swap-disk=s" => \$disks{SWAP}{FILE_NAME},
- "0|disk-0|hda=s" => \$disks_by_iface[0]{FILENAME},
- "1|disk-1|hdb=s" => \$disks_by_iface[1]{FILENAME},
- "2|disk-2|hdc=s" => \$disks_by_iface[2]{FILENAME},
- "3|disk-3|hdd=s" => \$disks_by_iface[3]{FILENAME})
+ "0|disk-0|hda=s" => \$disks_by_iface[0]{FILE_NAME},
+ "1|disk-1|hdb=s" => \$disks_by_iface[1]{FILE_NAME},
+ "2|disk-2|hdc=s" => \$disks_by_iface[2]{FILE_NAME},
+ "3|disk-3|hdd=s" => \$disks_by_iface[3]{FILE_NAME})
or exit 1;
}
$sim = "bochs" if !defined $sim;
$debug = "none" if !defined $debug;
$vga = "window" if !defined $vga;
+
+ 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).
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
- -T, --timeout=N Time out and kill Pintos after N seconds
+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):
sub find_disks {
for my $disk (values %disks) {
# If there's no assigned file name but the default file exists,
- # assign the default file.
- $disk->{FILENAME} = $disk->{DEF_FN}
- if !defined ($disk->{FILENAME}) && -e $disk->{DEF_FN};
+ # try to assign a default file name.
+ if (!defined ($disk->{FILE_NAME})) {
+ for my $try_fn ($disk->{DEF_FN}, "build/" . $disk->{DEF_FN}) {
+ $disk->{FILE_NAME} = $try_fn, last
+ if -e $try_fn;
+ }
+ }
# If there's no file name, we're done.
- next if !defined ($disk->{FILENAME});
+ next if !defined ($disk->{FILE_NAME});
- if ($disk->{FILENAME} =~ /^\d+(\.\d+)?|\.\d+$/) {
+ if ($disk->{FILE_NAME} =~ /^\d+(\.\d+)?|\.\d+$/) {
# Create a temporary disk of approximately the specified
# size in megabytes.
die "OS disk can't be temporary\n" if $disk == $disks{OS};
- my ($mb) = $disk->{FILENAME};
- undef $disk->{FILENAME};
+ my ($mb) = $disk->{FILE_NAME};
+ undef $disk->{FILE_NAME};
- my ($cylinder) = 1024 * 504;
- my ($bytes) = $mb * ($cylinder * 2);
- $bytes = int (($bytes + $cylinder - 1) / $cylinder) * $cylinder;
- extend_disk ($disk, $bytes);
+ my ($cyl_size) = 512 * 16 * 63;
+ extend_disk ($disk, ceil ($mb * 2) * $cyl_size);
} else {
# The file must exist and have nonzero size.
- -e $disk->{FILENAME} or die "$disk->{FILENAME}: stat: $!\n";
- -s _ or die "$disk->{FILENAME}: disk has zero size\n";
+ -e $disk->{FILE_NAME} or die "$disk->{FILE_NAME}: stat: $!\n";
+ -s _ or die "$disk->{FILE_NAME}: disk has zero size\n";
}
}
# Warn about (potentially) missing disks.
+ die "Cannot find OS disk\n" if !defined $disks{OS}{FILE_NAME};
if (my ($project) = `pwd` =~ /\b(threads|userprog|vm|filesys)\b/) {
if ((grep ($project eq $_, qw (userprog vm filesys)))
- && !defined ($disks{FS}{FILENAME})) {
+ && !defined ($disks{FS}{FILE_NAME})) {
print STDERR "warning: it looks like you're running the $project ";
print STDERR "project, but no file system disk is present\n";
}
- if ($project eq 'vm' && !defined $disks{SWAP}{FILENAME}) {
+ if ($project eq 'vm' && !defined $disks{SWAP}{FILE_NAME}) {
print STDERR "warning: it looks like you're running the $project ";
print STDERR "project, but no swap disk is present\n";
}
#
# Copies $file into the scratch disk.
sub put_scratch_file {
- my ($put_filename) = @_;
- my ($disk_handle, $disk_filename) = open_disk ($disks{SCRATCH});
+ my ($put_file_name) = @_;
+ my ($disk_handle, $disk_file_name) = open_disk ($disks{SCRATCH});
- print "Copying $put_filename into $disk_filename...\n";
+ print "Copying $put_file_name into $disk_file_name...\n";
# Write metadata sector, which consists of a 4-byte signature
# followed by the file size.
- stat $put_filename or die "$put_filename: stat: $!\n";
+ stat $put_file_name or die "$put_file_name: stat: $!\n";
my ($size) = -s _;
my ($metadata) = pack ("a4 V x504", "PUT\0", $size);
- write_fully ($disk_handle, $disk_filename, $metadata);
+ write_fully ($disk_handle, $disk_file_name, $metadata);
# Copy file data.
my ($put_handle);
- sysopen ($put_handle, $put_filename, O_RDONLY)
- or die "$put_filename: open: $!\n";
- copy_file ($put_handle, $put_filename, $disk_handle, $disk_filename,
+ 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,
$size);
close ($put_handle);
# Round up disk data to beginning of next sector.
- write_fully ($disk_handle, $disk_filename, "\0" x (512 - $size % 512))
+ write_fully ($disk_handle, $disk_file_name, "\0" x (512 - $size % 512))
if $size % 512;
}
#
# Copies from the scratch disk to $file.
sub get_scratch_file {
- my ($get_filename) = @_;
- my ($disk_handle, $disk_filename) = open_disk ($disks{SCRATCH});
+ my ($get_file_name) = @_;
+ my ($disk_handle, $disk_file_name) = open_disk ($disks{SCRATCH});
- print "Copying $get_filename out of $disk_filename...\n";
+ 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_filename, 512);
+ 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";
# Copy file data.
my ($get_handle);
- sysopen ($get_handle, $get_filename, O_WRONLY | O_CREAT | O_EXCL, 0666)
- or die "$get_filename: create: $!\n";
- copy_file ($disk_handle, $disk_filename, $get_handle, $get_filename,
+ sysopen ($get_handle, $get_file_name, O_WRONLY | O_CREAT | O_EXCL, 0666)
+ or die "$get_file_name: create: $!\n";
+ copy_file ($disk_handle, $disk_file_name, $get_handle, $get_file_name,
$size);
close ($get_handle);
# Skip forward in disk up to beginning of next sector.
- read_fully ($disk_handle, $disk_filename, 512 - $size % 512)
+ read_fully ($disk_handle, $disk_file_name, 512 - $size % 512)
if $size % 512;
}
\f
$args .= "\0" x (128 - length ($args));
# Write command line.
- my ($handle, $filename) = open_disk_copy ($disk);
- print "Writing command line to $filename...\n";
- sysseek ($handle, 0x17a, 0) == 0x17a or die "$filename: seek: $!\n";
- syswrite ($handle, "$arg_cnt$args") or die "$filename: write: $!\n";
+ my ($handle, $file_name) = open_disk_copy ($disk);
+ print "Writing command line to $file_name...\n";
+ sysseek ($handle, 0x17a, 0) == 0x17a or die "$file_name: seek: $!\n";
+ syswrite ($handle, "$arg_cnt$args") or die "$file_name: write: $!\n";
}
\f
# Running simulators.
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";
}
# Runs Bochs.
sub run_bochs {
# Select Bochs binary based on the chosen debugger.
- my ($bin);
- if ($debug eq 'none') {
- $bin = 'bochs';
- } elsif ($debug eq 'monitor') {
- $bin = 'bochs-dbg';
- } elsif ($debug eq 'gdb') {
- $bin = 'bochs-gdb';
+ 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;
romimage: file=\$BXSHARE/BIOS-bochs-latest, address=0xf0000
-vgaromimage: \$BXSHARE/VGABIOS-lgpl-latest
-boot: c
-ips: 1000000
+vgaromimage: file=\$BXSHARE/VGABIOS-lgpl-latest
+boot: disk
+cpu: ips=1000000
megs: $mem
log: bochsout.txt
+panic: action=fatal
EOF
- print BOCHSRC "clock: sync=", $realtime ? 'realtime' : 'none', "time0=0\n";
+ print BOCHSRC "gdbstub: enabled=1\n" if $debug eq 'gdb';
+ print BOCHSRC "clock: sync=", $realtime ? 'realtime' : 'none',
+ " time0=0\n";
print_bochs_disk_line ("ata0-master", 0);
print_bochs_disk_line ("ata0-slave", 1);
- if (defined ($disks_by_iface[2]{FILENAME})
- || defined ($disks_by_iface[3]{FILENAME})) {
+ 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') {
- print BOCHSRC "com1: enabled=1, 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";
# 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.
# line for attaching it to $device.
sub print_bochs_disk_line {
my ($device, $iface) = @_;
- my ($file) = $disks_by_iface[$iface]{FILENAME};
+ my ($disk) = $disks_by_iface[$iface];
+ my ($file) = $disk->{FILE_NAME};
if (defined $file) {
- my (%geom) = disk_geometry ($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.
+# Runs QEMU.
sub run_qemu {
print "warning: qemu doesn't support --terminal\n"
if $vga eq 'terminal';
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, $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_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;
+# 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++) {
- my ($dsk) = $disks_by_iface[$i]{FILENAME};
+ my ($disk) = $disks_by_iface[$i];
+ 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
$device.fileName = "$pln"
EOF
- my (%geom) = disk_geometry ($dsk);
+ 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.
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";
+ if ($disk->{FILE_NAME}) {
+ sysopen ($disk->{HANDLE}, $disk->{FILE_NAME}, O_RDWR)
+ or die "$disk->{FILE_NAME}: open: $!\n";
} else {
- ($disk->{HANDLE}, $disk->{FILENAME}) = tempfile (UNLINK => 1,
+ ($disk->{HANDLE}, $disk->{FILE_NAME}) = tempfile (UNLINK => 1,
SUFFIX => '.dsk');
}
}
- return ($disk->{HANDLE}, $disk->{FILENAME});
+ return ($disk->{HANDLE}, $disk->{FILE_NAME});
}
# 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};
+ die if !$disk->{FILE_NAME};
- 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,
+ my ($orig_handle, $orig_file_name) = open_disk ($disk);
+ my ($cp_handle, $cp_file_name) = tempfile (UNLINK => 1, SUFFIX => '.dsk');
+ copy_file ($orig_handle, $orig_file_name, $cp_handle, $cp_file_name,
-s $orig_handle);
- return ($disk->{HANDLE}, $disk->{FILENAME}) = ($cp_handle, $cp_filename);
+ return ($disk->{HANDLE}, $disk->{FILE_NAME}) = ($cp_handle, $cp_file_name);
}
# extend_disk($disk, $size)
# long.
sub extend_disk {
my ($disk, $size) = @_;
- my ($handle, $filename) = open_disk ($disk);
+ my ($handle, $file_name) = open_disk ($disk);
if (-s ($handle) < $size) {
sysseek ($handle, $size - 1, 0) == $size - 1
- or die "$filename: seek: $!\n";
+ or die "$file_name: seek: $!\n";
syswrite ($handle, "\0") == 1
- or die "$filename: write: $!\n";
+ or die "$file_name: write: $!\n";
}
}
# Examines $file and returns a valid IDE disk geometry for it, as a
# hash.
sub disk_geometry {
- my ($file) = @_;
+ my ($disk) = @_;
+ my ($file) = $disk->{FILE_NAME};
my ($size) = -s $file;
die "$file: stat: $!\n" if !defined $size;
die "$file: size not a multiple of 512 bytes\n" if $size % 512;
- my ($cylinders) = int ($size / (512 * 16 * 63));
- $cylinders++ if $size % (512 * 16 * 63);
+ 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,
S => 63);
}
-# copy_file($from_handle, $from_filename, $to_handle, $to_filename, $size)
+# copy_file($from_handle, $from_file_name, $to_handle, $to_file_name, $size)
#
# Copies $size bytes from $from_handle to $to_handle.
-# $from_filename and $to_filename are used in error messages.
+# $from_file_name and $to_file_name are used in error messages.
sub copy_file {
- my ($from_handle, $from_filename, $to_handle, $to_filename, $size) = @_;
+ 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_filename, $chunk_size);
- write_fully ($to_handle, $to_filename, $data);
+ my ($data) = read_fully ($from_handle, $from_file_name, $chunk_size);
+ write_fully ($to_handle, $to_file_name, $data);
}
}
-# read_fully($handle, $filename, $bytes)
+# read_fully($handle, $file_name, $bytes)
#
# Reads exactly $bytes bytes from $handle and returns the data read.
-# $filename is used in error messages.
+# $file_name is used in error messages.
sub read_fully {
- my ($handle, $filename, $bytes) = @_;
+ my ($handle, $file_name, $bytes) = @_;
my ($data);
my ($read_bytes) = sysread ($handle, $data, $bytes);
- die "$filename: read: $!\n" if !defined $read_bytes;
- die "$filename: unexpected end of file\n" if $read_bytes != $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, $filename, $data)
+# write_fully($handle, $file_name, $data)
#
# Write $data to $handle.
-# $filename is used in error messages.
+# $file_name is used in error messages.
sub write_fully {
- my ($handle, $filename, $data) = @_;
+ my ($handle, $file_name, $data) = @_;
my ($written_bytes) = syswrite ($handle, $data);
- die "$filename: write: $!\n" if !defined $written_bytes;
- die "$filename: short write\n" if $written_bytes != length $data;
+ die "$file_name: write: $!\n" if !defined $written_bytes;
+ die "$file_name: short write\n" if $written_bytes != length $data;
}
\f
# Subprocess utilities.
# 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.
- exec (@_);
- exit (1);
+ 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"); };
- alarm ($timeout) if defined ($timeout);
- waitpid ($pid, 0);
+ 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)
+# relay_signal($pid, $signal, &$cleanup)
#
# Relays $signal to $pid and then reinvokes it for us with the default
-# handler.
+# handler. Also cleans up temporary files and invokes $cleanup.
sub relay_signal {
- my ($pid, $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)
+# 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 $timeout seconds";
- 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;
}
+
+# 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;
+}