6 use File::Temp 'tempfile';
7 use Getopt::Long qw(:config bundling);
9 # Command-line options.
10 our ($sim); # Simulator: bochs, qemu, or gsx.
11 our ($debug) = "none"; # Debugger: none, monitor, or gdb.
12 our ($mem) = 4; # Physical RAM in MB.
13 our ($serial_out) = 1; # Send output to serial port?
14 our ($vga); # VGA output: window, terminal, or none.
15 our ($jitter); # Seed for random timer interrupts, if set.
16 our ($realtime); # Synchronize timer interrupts with real time?
17 our ($timeout); # Maximum runtime in seconds, if set.
18 our (@puts); # Files to copy into the VM.
19 our (@gets); # Files to copy out of the VM.
20 our ($as_ref); # Reference to last addition to @gets or @puts.
21 our (@kernel_args); # Arguments to pass to kernel.
22 our (%disks) = (OS => {DEF_FN => 'os.dsk'}, # Disks to give VM.
23 FS => {DEF_FN => 'fs.dsk'},
24 SCRATCH => {DEF_FN => 'scratch.dsk'},
25 SWAP => {DEF_FN => 'swap.dsk'});
26 our (@disks_by_iface) = @disks{qw (OS FS SCRATCH SWAP)};
28 parse_command_line ();
30 prepare_scratch_disk ();
33 finish_scratch_disk ();
37 # Parses the command line.
38 sub parse_command_line {
39 usage (0) if @ARGV == 0 || (@ARGV == 1 && $ARGV[0] eq '--help');
42 if (grep ($_ eq '--', @kernel_args)) {
44 while ((my $arg = shift (@kernel_args)) ne '--') {
47 GetOptions ("sim=s" => sub { set_sim (@_) },
48 "bochs" => sub { set_sim ("bochs") },
49 "qemu" => sub { set_sim ("qemu") },
50 "gsx" => sub { set_sim ("gsx") },
52 "debug=s" => sub { set_debug (@_) },
53 "no-debug" => sub { set_debug ("none") },
54 "monitor" => sub { set_debug ("monitor") },
55 "gdb" => sub { set_debug ("gdb") },
57 "m|memory=i" => \$mem,
58 "j|jitter=i" => sub { set_jitter (@_) },
59 "r|realtime" => sub { set_realtime () },
60 "T|timeout=i" => \$timeout,
62 "v|no-vga" => sub { set_vga ('none'); },
63 "s|no-serial" => sub { $serial_out = 0; },
64 "t|terminal" => sub { set_vga ('terminal'); },
66 "p|put-file=s" => sub { add_file (\@puts, $_[1]); },
67 "g|get-file=s" => sub { add_file (\@gets, $_[1]); },
68 "a|as=s" => sub { set_as ($_[1]); },
70 "h|help" => sub { usage (0); },
72 "os-disk=s" => \$disks{OS}{FILENAME},
73 "fs-disk=s" => \$disks{FS}{FILENAME},
74 "scratch-disk=s" => \$disks{SCRATCH}{FILENAME},
75 "swap-disk=s" => \$disks{SWAP}{FILENAME},
77 "0|disk-0|hda=s" => \$disks_by_iface[0]{FILENAME},
78 "1|disk-1|hdb=s" => \$disks_by_iface[1]{FILENAME},
79 "2|disk-2|hdc=s" => \$disks_by_iface[2]{FILENAME},
80 "3|disk-3|hdd=s" => \$disks_by_iface[3]{FILENAME})
84 $sim = "bochs" if !defined $sim;
85 $debug = "none" if !defined $debug;
86 $vga = "window" if !defined $vga;
90 # Prints a usage message and exits with $exitcode.
93 $exitcode = 1 unless defined $exitcode;
95 pintos, a utility for running Pintos in a simulator
96 Usage: pintos [OPTION...] -- [ARGUMENT...]
97 where each OPTION is one of the following options
98 and each ARGUMENT is passed to Pintos kernel verbatim.
100 --bochs (default) Use Bochs as simulator
101 --qemu Use qemu as simulator
102 --gsx Use VMware GSX Server 3.x as simulator
104 --no-debug (default) No debugger
105 --monitor Debug with simulator's monitor
107 Display options: (default is both VGA and serial)
108 -v, --no-vga No VGA display
109 -s, --no-serial No serial output
110 -t, --terminal Display VGA in terminal (Bochs only)
111 Timing options: (Bochs only)
112 -j SEED Randomize timer interrupts
113 -r, --realtime Use realistic, not reproducible, timings
114 -T, --timeout=N Time out and kill Pintos after N seconds
115 Configuration options:
116 -m, --mem=N Give Pintos N MB physical RAM (default: 4)
117 File system commands (for `run' command):
118 -p, --put-file=HOSTFN Copy HOSTFN into VM, by default under same name
119 -g, --get-file=GUESTFN Copy GUESTFN out of VM, by default under same name
120 -a, --as=FILENAME Specifies guest (for -p) or host (for -g) file name
121 Disk options: (name an existing FILE or specify SIZE in MB for a temp disk)
122 --os-disk=FILE Set OS disk file (default: os.dsk)
123 --fs-disk=FILE|SIZE Set FS disk file (default: fs.dsk)
124 --scratch-disk=FILE|SIZE Set scratch disk (default: scratch.dsk)
125 --swap-disk=FILE|SIZE Set swap disk file (default: swap.dsk)
127 -h, --help Display this help message.
132 # Sets the simulator.
135 die "--$new_sim conflicts with --$sim\n"
136 if defined ($sim) && $sim ne $new_sim;
142 my ($new_debug) = @_;
143 die "--$new_debug conflicts with --$debug\n"
144 if $debug ne 'none' && $new_debug ne 'none' && $debug ne $new_debug;
148 # Sets VGA output destination.
151 if (defined ($vga) && $vga ne $new_vga) {
152 print "warning: conflicting vga display options\n";
157 # Sets randomized timer interrupts.
159 my ($new_jitter) = @_;
160 die "--realtime conflicts with --jitter\n" if defined $realtime;
161 die "different --jitter already defined\n"
162 if defined $jitter && $jitter != $new_jitter;
163 $jitter = $new_jitter;
166 # Sets real-time timer interrupts.
168 die "--realtime conflicts with --jitter\n" if defined $jitter;
172 # add_file(\@list, $file)
174 # Adds [$file] to @list, which should be @puts or @gets.
175 # Sets $as_ref to point to the added element.
177 my ($list, $file) = @_;
179 push (@$list, $as_ref);
182 # Sets the guest/host name for the previous put/get.
185 die "-a (or --as) is only allowed after -p or -g\n" if !defined $as_ref;
186 die "Only one -a (or --as) is allowed after -p or -g\n"
187 if defined $as_ref->[1];
191 # Locates the files used to back each of the virtual disks,
192 # and creates temporary disks.
194 for my $disk (values %disks) {
195 # If there's no assigned file name but the default file exists,
196 # try to assign a default file name.
197 if (!defined ($disk->{FILENAME})) {
198 for my $try_fn ($disk->{DEF_FN}, "build/" . $disk->{DEF_FN}) {
199 $disk->{FILENAME} = $try_fn, last
204 # If there's no file name, we're done.
205 next if !defined ($disk->{FILENAME});
207 if ($disk->{FILENAME} =~ /^\d+(\.\d+)?|\.\d+$/) {
208 # Create a temporary disk of approximately the specified
210 die "OS disk can't be temporary\n" if $disk == $disks{OS};
212 my ($mb) = $disk->{FILENAME};
213 undef $disk->{FILENAME};
215 my ($cylinder) = 1024 * 504;
216 my ($bytes) = $mb * ($cylinder * 2);
217 $bytes = int (($bytes + $cylinder - 1) / $cylinder) * $cylinder;
218 extend_disk ($disk, $bytes);
220 # The file must exist and have nonzero size.
221 -e $disk->{FILENAME} or die "$disk->{FILENAME}: stat: $!\n";
222 -s _ or die "$disk->{FILENAME}: disk has zero size\n";
226 # Warn about (potentially) missing disks.
227 die "Cannot find OS disk\n" if !defined $disks{OS}{FILENAME};
228 if (my ($project) = `pwd` =~ /\b(threads|userprog|vm|filesys)\b/) {
229 if ((grep ($project eq $_, qw (userprog vm filesys)))
230 && !defined ($disks{FS}{FILENAME})) {
231 print STDERR "warning: it looks like you're running the $project ";
232 print STDERR "project, but no file system disk is present\n";
234 if ($project eq 'vm' && !defined $disks{SWAP}{FILENAME}) {
235 print STDERR "warning: it looks like you're running the $project ";
236 print STDERR "project, but no swap disk is present\n";
241 # Prepare the scratch disk for gets and puts.
242 sub prepare_scratch_disk {
243 # Copy the files to put onto the scratch disk.
244 put_scratch_file ($_->[0]) foreach @puts;
246 # Make sure the scratch disk is big enough to get big files.
247 extend_disk ($disks{SCRATCH}, @gets * 1024 * 1024) if @gets;
250 # Read "get" files from the scratch disk.
251 sub finish_scratch_disk {
252 # We need to start reading the scratch disk from the beginning again.
254 close ($disks{SCRATCH}{HANDLE});
255 undef ($disks{SCRATCH}{HANDLE});
259 get_scratch_file (defined ($_->[1]) ? $_->[1] : $_->[0]) foreach @gets;
262 # put_scratch_file($file).
264 # Copies $file into the scratch disk.
265 sub put_scratch_file {
266 my ($put_filename) = @_;
267 my ($disk_handle, $disk_filename) = open_disk ($disks{SCRATCH});
269 print "Copying $put_filename into $disk_filename...\n";
271 # Write metadata sector, which consists of a 4-byte signature
272 # followed by the file size.
273 stat $put_filename or die "$put_filename: stat: $!\n";
275 my ($metadata) = pack ("a4 V x504", "PUT\0", $size);
276 write_fully ($disk_handle, $disk_filename, $metadata);
280 sysopen ($put_handle, $put_filename, O_RDONLY)
281 or die "$put_filename: open: $!\n";
282 copy_file ($put_handle, $put_filename, $disk_handle, $disk_filename,
286 # Round up disk data to beginning of next sector.
287 write_fully ($disk_handle, $disk_filename, "\0" x (512 - $size % 512))
291 # get_scratch_file($file).
293 # Copies from the scratch disk to $file.
294 sub get_scratch_file {
295 my ($get_filename) = @_;
296 my ($disk_handle, $disk_filename) = open_disk ($disks{SCRATCH});
298 print "Copying $get_filename out of $disk_filename...\n";
300 # Read metadata sector, which has a 4-byte signature followed by
302 my ($metadata) = read_fully ($disk_handle, $disk_filename, 512);
303 my ($signature, $size) = unpack ("a4 V", $metadata);
304 die "bad signature reading scratch disk--did Pintos run correctly?\n"
305 if $signature ne "GET\0";
309 sysopen ($get_handle, $get_filename, O_WRONLY | O_CREAT | O_EXCL, 0666)
310 or die "$get_filename: create: $!\n";
311 copy_file ($disk_handle, $disk_filename, $get_handle, $get_filename,
315 # Skip forward in disk up to beginning of next sector.
316 read_fully ($disk_handle, $disk_filename, 512 - $size % 512)
320 # Prepares the arguments to pass to the Pintos kernel,
321 # and then write them into Pintos bootloader.
322 sub prepare_arguments {
324 push (@args, shift (@kernel_args))
325 while @kernel_args && $kernel_args[0] =~ /^-/;
326 push (@args, 'put', defined $_->[1] ? $_->[1] : $_->[0]) foreach @puts;
327 push (@args, @kernel_args);
328 push (@args, 'get', $_->[0]) foreach @gets;
329 write_cmd_line ($disks{OS}, @args);
332 # Writes @args into the Pintos bootloader at the beginning of $disk.
334 my ($disk, @args) = @_;
336 # Figure out command line to write.
337 my ($arg_cnt) = pack ("V", scalar (@args));
338 my ($args) = join ('', map ("$_\0", @args));
339 die "command line exceeds 128 bytes" if length ($args) > 128;
340 $args .= "\0" x (128 - length ($args));
342 # Write command line.
343 my ($handle, $filename) = open_disk_copy ($disk);
344 print "Writing command line to $filename...\n";
345 sysseek ($handle, 0x17a, 0) == 0x17a or die "$filename: seek: $!\n";
346 syswrite ($handle, "$arg_cnt$args") or die "$filename: write: $!\n";
349 # Running simulators.
351 # Runs the selected simulator.
353 if ($sim eq 'bochs') {
355 } elsif ($sim eq 'qemu') {
357 } elsif ($sim eq 'gsx') {
360 die "unknown simulator `$sim'\n";
366 # Select Bochs binary based on the chosen debugger.
368 if ($debug eq 'none') {
370 } elsif ($debug eq 'monitor') {
372 } elsif ($debug eq 'gdb') {
376 # Write bochsrc.txt configuration file.
377 open (BOCHSRC, ">", "bochsrc.txt") or die "bochsrc.txt: create: $!\n";
379 romimage: file=\$BXSHARE/BIOS-bochs-latest, address=0xf0000
380 vgaromimage: \$BXSHARE/VGABIOS-lgpl-latest
386 print BOCHSRC "clock: sync=", $realtime ? 'realtime' : 'none', "time0=0\n";
387 print_bochs_disk_line ("ata0-master", 0);
388 print_bochs_disk_line ("ata0-slave", 1);
389 if (defined ($disks_by_iface[2]{FILENAME})
390 || defined ($disks_by_iface[3]{FILENAME})) {
391 print BOCHSRC "ata1: enabled=1, ioaddr1=0x170, ",
392 "ioaddr2=0x370, irq=15\n";
393 print_bochs_disk_line ("ata1-master", 2);
394 print_bochs_disk_line ("ata1-slave", 3);
396 if ($vga ne 'terminal') {
397 print BOCHSRC "com1: enabled=1, dev=/dev/stdout\n" if $serial_out;
398 print BOCHSRC "display_library: nogui\n" if $vga eq 'none';
400 print BOCHSRC "display_library: term\n";
404 # Compose Bochs command line.
405 my (@cmd) = ($bin, '-q');
406 push (@cmd, '-j', $jitter) if defined $jitter;
409 print join (' ', @cmd), "\n";
410 my ($exit) = xsystem (@cmd);
411 if (WIFEXITED ($exit)) {
412 # Bochs exited normally.
413 # Ignore the exit code; Bochs normally exits with status 1,
415 } elsif (WIFSIGNALED ($exit)) {
416 die "Bochs died with signal ", WTERMSIG ($exit), "\n";
418 die "Bochs died: code $exit\n";
422 # print_bochs_disk_line($device, $iface)
424 # If IDE interface $iface has a disk attached, prints a bochsrc.txt
425 # line for attaching it to $device.
426 sub print_bochs_disk_line {
427 my ($device, $iface) = @_;
428 my ($file) = $disks_by_iface[$iface]{FILENAME};
430 my (%geom) = disk_geometry ($file);
431 print BOCHSRC "$device: type=disk, path=$file, mode=flat, ";
432 print BOCHSRC "cylinders=$geom{C}, heads=$geom{H}, spt=$geom{S}, ";
433 print BOCHSRC "translation=none\n";
439 print "warning: qemu doesn't support --terminal\n"
440 if $vga eq 'terminal';
441 print "warning: qemu doesn't support jitter\n"
443 my (@cmd) = ('qemu');
444 for my $iface (0...3) {
445 my ($option) = ('-hda', '-hdb', '-hdc', '-hdd')[$iface];
446 push (@cmd, $option, $disks_by_iface[$iface]{FILENAME})
447 if defined $disks_by_iface[$iface]{FILENAME};
449 push (@cmd, '-m', $mem, '-nics', '0');
450 push (@cmd, '-nographic') if $vga eq 'none';
451 push (@cmd, '-serial', 'stdio') if $serial_out && $vga ne 'none';
452 push (@cmd, '-S') if $debug eq 'monitor';
453 push (@cmd, '-s', '-S') if $debug eq 'gdb';
454 push (@cmd, '-monitor', 'null') if $vga eq 'none' && $debug eq 'none';
460 # Prints a message that $flag is unsupported by GSX Server.
463 print "warning: no support for $flag with VMware GSX Server\n";
466 # Runs VMware GSX Server.
468 gsx_unsup ("--$debug") if $debug ne 'none';
469 gsx_unsup ("--no-vga") if $vga eq 'none';
470 gsx_unsup ("--terminal") if $vga eq 'terminal';
471 gsx_unsup ("--jitter") if defined $jitter;
473 unlink ("pintos.out");
475 open (VMX, ">", "pintos.vmx") or die "pintos.vmx: create: $!\n";
476 chmod 0777 & ~umask, "pintos.vmx";
478 #! /usr/bin/vmware -G
481 floppy0.present = FALSE
484 serial0.present = TRUE
485 serial0.fileType = "file"
486 serial0.fileName = "pintos.out"
489 for (my ($i) = 0; $i < 4; $i++) {
490 my ($dsk) = $disks_by_iface[$i]{FILENAME};
491 next if !defined $dsk;
497 my ($device) = "ide" . int ($i / 2) . ":" . ($i % 2);
500 $device.present = TRUE
501 $device.deviceType = "plainDisk"
502 $device.fileName = "$pln"
505 my (%geom) = disk_geometry ($dsk);
506 open (PLN, ">", $pln) or die "$pln: create: $!\n";
514 #vm|CAPACITY $geom{CAPACITY}
515 ACCESS "$dsk" 0 $geom{CAPACITY}
521 my ($vmx) = getcwd () . "/pintos.vmx";
522 system ("vmware-cmd -s register $vmx >&/dev/null");
523 system ("vmware-cmd $vmx stop hard >&/dev/null");
524 system ("vmware -l -G -x -q $vmx");
525 system ("vmware-cmd $vmx stop hard >&/dev/null");
526 system ("vmware-cmd -s unregister $vmx >&/dev/null");
533 # Opens $disk, if it is not already open, and returns its file handle
537 if (!defined ($disk->{HANDLE})) {
538 if ($disk->{FILENAME}) {
539 sysopen ($disk->{HANDLE}, $disk->{FILENAME}, O_RDWR)
540 or die "$disk->{FILENAME}: open: $!\n";
542 ($disk->{HANDLE}, $disk->{FILENAME}) = tempfile (UNLINK => 1,
546 return ($disk->{HANDLE}, $disk->{FILENAME});
549 # open_disk_copy($disk)
551 # Makes a temporary copy of $disk and returns its file handle and file name.
554 die if !$disk->{FILENAME};
556 my ($orig_handle, $orig_filename) = open_disk ($disk);
557 my ($cp_handle, $cp_filename) = tempfile (UNLINK => 1, SUFFIX => '.dsk');
558 copy_file ($orig_handle, $orig_filename, $cp_handle, $cp_filename,
560 return ($disk->{HANDLE}, $disk->{FILENAME}) = ($cp_handle, $cp_filename);
563 # extend_disk($disk, $size)
565 # Extends $disk, if necessary, so that it is at least $size bytes
568 my ($disk, $size) = @_;
569 my ($handle, $filename) = open_disk ($disk);
570 if (-s ($handle) < $size) {
571 sysseek ($handle, $size - 1, 0) == $size - 1
572 or die "$filename: seek: $!\n";
573 syswrite ($handle, "\0") == 1
574 or die "$filename: write: $!\n";
578 # disk_geometry($file)
580 # Examines $file and returns a valid IDE disk geometry for it, as a
584 my ($size) = -s $file;
585 die "$file: stat: $!\n" if !defined $size;
586 die "$file: size not a multiple of 512 bytes\n" if $size % 512;
587 my ($cylinders) = int ($size / (512 * 16 * 63));
588 $cylinders++ if $size % (512 * 16 * 63);
590 return (CAPACITY => $size / 512,
596 # copy_file($from_handle, $from_filename, $to_handle, $to_filename, $size)
598 # Copies $size bytes from $from_handle to $to_handle.
599 # $from_filename and $to_filename are used in error messages.
601 my ($from_handle, $from_filename, $to_handle, $to_filename, $size) = @_;
604 my ($chunk_size) = 4096;
605 $chunk_size = $size if $chunk_size > $size;
606 $size -= $chunk_size;
608 my ($data) = read_fully ($from_handle, $from_filename, $chunk_size);
609 write_fully ($to_handle, $to_filename, $data);
613 # read_fully($handle, $filename, $bytes)
615 # Reads exactly $bytes bytes from $handle and returns the data read.
616 # $filename is used in error messages.
618 my ($handle, $filename, $bytes) = @_;
620 my ($read_bytes) = sysread ($handle, $data, $bytes);
621 die "$filename: read: $!\n" if !defined $read_bytes;
622 die "$filename: unexpected end of file\n" if $read_bytes != $bytes;
626 # write_fully($handle, $filename, $data)
628 # Write $data to $handle.
629 # $filename is used in error messages.
631 my ($handle, $filename, $data) = @_;
632 my ($written_bytes) = syswrite ($handle, $data);
633 die "$filename: write: $!\n" if !defined $written_bytes;
634 die "$filename: short write\n" if $written_bytes != length $data;
637 # Subprocess utilities.
641 # Runs xsystem(@args).
642 # Also prints the command it's running and checks that it succeeded.
644 print join (' ', @_), "\n";
645 die "command failed\n" if xsystem (@_);
650 # Creates a subprocess via exec(@args) and waits for it to complete.
651 # Relays common signals to the subprocess.
652 # If $timeout is set then the subprocess will be killed after that long.
655 if (!defined ($pid)) {
659 # Running in child process.
663 # Running in parent process.
664 local $SIG{ALRM} = sub { timeout ($pid); };
665 local $SIG{INT} = sub { relay_signal ($pid, "INT"); };
666 local $SIG{TERM} = sub { relay_signal ($pid, "TERM"); };
667 alarm ($timeout) if defined ($timeout);
674 # relay_signal($pid, $signal)
676 # Relays $signal to $pid and then reinvokes it for us with the default
679 my ($pid, $signal) = @_;
681 $SIG{$signal} = 'DEFAULT';
682 kill $signal, getpid ();
687 # Interrupts $pid and dies with a timeout error message.
693 my ($load_avg) = `uptime` =~ /(load average:.*)$/i;
694 print "\nTIMEOUT after $timeout seconds";
695 print " - $load_avg" if defined $load_avg;