8 our ($disk) = "pintos.dsk";
9 our (@part_files) = ("boot.part", "filesys.part", "scratch.part", "swap.part");
13 our ($jitter, $realtime);
15 our (%role2type) = (0 => 0x20, 1 => 0x21, 2 => 0x22, 3 => 0x23);
17 use Getopt::Long qw(:config require_order bundling);
18 unshift (@ARGV, split (' ', $ENV{PINTOSOPTS}))
19 if defined $ENV{PINTOSOPTS};
20 GetOptions ("sim=s" => sub { set_sim (@_) },
21 "bochs" => sub { set_sim ("bochs") },
22 "qemu" => sub { set_sim ("qemu") },
23 "gsx" => sub { set_sim ("gsx") },
25 "debug=s" => sub { set_debug (@_) },
26 "no-debug" => sub { set_debug ("no-debug") },
27 "monitor" => sub { set_debug ("monitor") },
28 "gdb" => sub { set_debug ("gdb") },
30 "run|get|put|assemble|disassemble" => \&cmd_option,
32 "m|memory=i" => \$mem,
33 "j|jitter=i" => sub { set_jitter (@_) },
34 "r|realtime" => sub { set_realtime () },
36 "v|no-vga" => sub { set_vga ('none'); },
37 "s|no-serial" => sub { $serial_out = 0; },
38 "t|terminal" => sub { set_vga ('terminal'); },
40 "h|help" => sub { usage (0); },
44 "boot-partition=s" => \$part_files[0],
45 "fs-partition|filesys-partition=s" => \$part_files[1],
46 "scratch-partition=s" => \$part_files[2],
47 "swap-partition=s" => \$part_files[3]
51 $sim = "bochs" if !defined $sim;
52 $debug = "no-debug" if !defined $debug;
53 $vga = "window" if !defined $vga;
57 die "--$new_sim conflicts with --$sim\n"
58 if defined ($sim) && $sim ne $new_sim;
64 die "--$new_debug conflicts with --$debug\n"
65 if defined ($debug) && $debug ne $new_debug;
71 if (defined ($vga) && $vga ne $new_vga) {
72 print "warning: conflicting vga display options\n";
78 my ($new_jitter) = @_;
79 die "--realtime conflicts with --jitter\n" if defined $realtime;
80 die "different --jitter already defined\n"
81 if defined $jitter && $jitter != $new_jitter;
82 $jitter = $new_jitter;
86 die "--realtime conflicts with --jitter\n" if defined $jitter;
91 # Force an end to option processing, as with --.
95 die "no command specified; use --help for help\n"
97 my ($cmd) = shift @ARGV;
100 } elsif ($cmd eq 'put') {
101 # Take a -f option to combine formatting with putting.
103 if (@ARGV > 0 && $ARGV[0] eq '-f') {
108 usage () if @ARGV != 1 && @ARGV != 2;
109 my ($hostfn, $guestfn) = @ARGV;
110 $guestfn = $hostfn if !defined $guestfn;
113 @part_files = ("part0.tmp", "part1.tmp", "part2.tmp", "part3.tmp");
114 disassemble ($part_files[0], $part_files[1], undef, $part_files[3]);
115 die "missing file system partition\n" if ! -e $part_files[1];
117 # Create scratch disk from file.
118 open (FILE, "<$hostfn") or die "$hostfn: open: $!\n";
119 my ($scratchfn) = $part_files[1];
120 open (SCRATCH, ">$scratchfn") or die "$scratchfn: create: $!\n";
124 my ($amt) = sysread (FILE, $buf, 65536);
125 die "$hostfn: read: $!\n" if $amt < 0;
127 syswrite (FILE, $buf, $amt) == $amt or die "$scratchfn: write: $!\n";
130 my ($zeros) = 512 - $size % 512;
131 syswrite (FILE, "\0" x $zeros) == $zeros or die "$scratchfn: write: $!\n";
137 unlink (@part_files);
140 my (@cmd) = ("-ci", $guestfn, $size, "-q");
141 unshift (@cmd, "-f") if $format;
143 } elsif ($cmd eq 'get') {
144 usage () if @ARGV != 1 && @ARGV != 2;
145 my ($guestfn, $hostfn) = @ARGV;
146 $hostfn = $guestfn if !defined $hostfn;
147 die "$hostfn: already exists\n" if -e $hostfn;
150 @part_files = ("part0.tmp", "part1.tmp", undef, "part3.tmp");
151 disassemble (@part_files);
153 # Create scratch disk big enough for any file in the file system
154 # (modulo sparse files).
155 die "missing file system partition\n" if ! -e $part_files[1];
156 my ($fs_size) = -s _;
157 my ($approx_mb) = (16 * 63 * 512) * 2;
158 $part_files[2] = sprintf ("%d",
159 int (($fs_size + $approx_mb - 1) / $approx_mb));
160 assemble (@part_files);
163 run_vm ("-co", $guestfn, "-q");
165 # FIXME: we could just read the parttbl, then copy directly.
167 my ($scratchfn) = "part2.tmp";
168 disassemble (undef, undef, $scratchfn, undef);
170 # Read out scratch disk.
171 print "copying $guestfn from $scratchfn to $hostfn...\n";
172 open (SRC, "<$scratchfn") or die "$scratchfn: open: $!\n";
173 open (DST, ">$hostfn") or die "$hostfn: create: $!\n";
175 read (SRC, $input, 512) == 512 or die "$scratchfn: read error\n";
176 my ($size) = unpack ("V", $input);
177 $size != 0xffffffff or die "$guestfn: too big for $scratchfn?";
179 read (SRC, $src, $size) == $size or die "$scratchfn: read error\n";
180 print DST $src or die "$hostfn: write error\n";
183 } elsif ($cmd eq 'assemble') {
184 die "$part_files[0] not found ($!), but a boot partition is required\n"
185 if ! -e $part_files[0];
186 do { $_ = undef if ! -e $_ } foreach @part_files[1...3];
188 } elsif ($cmd eq 'help') {
191 die "unknown command `$cmd'; use --help for help\n";
197 $exitcode = 1 unless defined $exitcode;
198 print "pintos, a utility for invoking Pintos in a simulator\n";
199 print "Usage: pintos [OPTION...] COMMAND [ARG...]\n";
200 print "where COMMAND is one of the following:\n";
201 print " run [CMDLINE...] run a VM in the simulator\n";
202 print " put HOSTFN [GUESTFN] copy HOSTFN into VM (as GUESTFN)\n";
203 print " get GUESTFN [HOSTFN] copy GUESTFN out of VM (to HOSTFN)\n";
204 print " assemble assemble a VM disk from partitions\n";
205 print " disassemble disassemble a VM disk into partitions\n";
206 print " help print this help message and exit\n";
207 print "Simulator options:\n";
208 print " --bochs (default) Use Bochs as simulator\n";
209 print " --qemu Use qemu as simulator\n";
210 print " --gsx Use VMware GSX Server 3.x as simulator\n";
211 print "Debugger options:\n";
212 print " --no-debug (default) No debugger\n";
213 print " --monitor Debug with simulator's monitor\n";
214 print " --gdb Debug with gdb\n";
215 print "Display options: (default is VGA + serial)\n";
216 print " -v, --no-vga No VGA display\n";
217 print " -s, --no-serial No serial output\n";
218 print " -t, --terminal Display VGA in terminal (Bochs only)\n";
219 print "VM options:\n";
220 print " -d, --disk=DISK File holding VM's disk (default: pintos.dsk)\n";
221 print " -j SEED Randomize timer interrupts (Bochs only)\n";
222 print " -r, --realtime Use realistic, but not reproducible, timings\n";
223 print " -m, --mem=MB Run VM with MB megabytes of physical memory\n";
224 print "Assemble/disassemble partitions (default names in parentheses):\n";
225 print "(SOURCE is a file or a size in MB, for a blank partition.)\n";
226 print " --boot=FILE Boot partition (boot.part)\n";
227 print " --filesys=SOURCE File system partition (filesys.part)\n";
228 print " --scratch=SOURCE Scratch partition (scratch.part)\n";
229 print " --swap=SOURCE Swap partition (swap.part)\n";
234 my ($src, $dst, $blocksize) = @_;
235 run_command ("dd", "if=$src", "of=$dst", "bs=$blocksize", "conv=sync");
239 my ($disk, $kb) = @_;
240 run_command ("dd", "if=/dev/zero", "of=$disk", "bs=1024", "count=$kb");
247 die "$disk: can't find OS disk\n" if ! -e $disk;
250 # if (my ($project) = `pwd` =~ /\b(threads|userprog|vm|filesys)\b/) {
251 # if ((grep ($project eq $_, qw (userprog vm filesys)))
252 # && !defined ($disks[1])) {
253 # print STDERR "warning: it looks like you're running the $project ";
254 # print STDERR "project, but no file system disk is present\n";
256 # if ($project eq 'vm' && !defined $disks[3]) {
257 # print STDERR "warning: it looks like you're running the $project ";
258 # print STDERR "project, but no swap disk is present\n";
262 write_cmd_line ($disks[0], @args);
264 if ($sim eq 'bochs') {
266 if ($debug eq 'no-debug') {
268 } elsif ($debug eq 'monitor') {
270 } elsif ($debug eq 'gdb') {
274 my ($bochsbin) = search_path ($bin);
275 my ($bochsshare) = "$bochsbin/../share/bochs";
277 open (BOCHSRC, ">bochsrc.txt") or die "bochsrc.txt: create: $!\n";
278 print BOCHSRC "romimage: file=$bochsshare/BIOS-bochs-latest, "
279 . "address=0xf0000\n";
280 print BOCHSRC "vgaromimage: $bochsshare/VGABIOS-lgpl-latest\n";
281 print BOCHSRC bochs_disk_line ("ata0-master", $disks[0]);
282 print BOCHSRC bochs_disk_line ("ata0-slave", $disks[1]);
283 print BOCHSRC "ata1: enabled=1, ioaddr1=0x170, ioaddr2=0x370, irq=15\n"
284 if defined ($disks[2]) || defined ($disks[3]);
285 print BOCHSRC bochs_disk_line ("ata1-master", $disks[2]);
286 print BOCHSRC bochs_disk_line ("ata1-slave", $disks[3]);
287 print BOCHSRC "boot: c\n";
288 print BOCHSRC "ips: 1000000\n";
290 print BOCHSRC "clock: sync=none, time0=0\n";
292 print BOCHSRC "clock: sync=realtime, time0=0\n";
294 print BOCHSRC "megs: $mem\n";
295 print BOCHSRC "log: bochsout.txt\n";
296 if ($vga ne 'terminal') {
297 print BOCHSRC "com1: enabled=1, dev=/dev/stdout\n"
299 print BOCHSRC "display_library: nogui\n"
302 print BOCHSRC "display_library: term\n";
306 my (@cmd) = ($bin, '-q');
307 push (@cmd, '-j', $jitter) if defined $jitter;
308 print join (' ', @cmd), "\n";
309 my ($exit) = xsystem (@cmd);
310 if (WIFEXITED ($exit)) {
311 # Bochs exited normally.
312 # Ignore the exit code; Bochs normally exits with status 1,
314 } elsif (WIFSIGNALED ($exit)) {
315 die "Bochs died with signal ", WTERMSIG ($exit), "\n";
317 die "Bochs died: code $exit\n";
319 } elsif ($sim eq 'qemu') {
320 print "warning: qemu doesn't support --terminal\n"
321 if $vga eq 'terminal';
322 print "warning: qemu doesn't support jitter\n"
324 my (@cmd) = ('qemu');
325 push (@cmd, '-hda', $disks[0]) if defined $disks[0];
326 push (@cmd, '-hdb', $disks[1]) if defined $disks[1];
327 push (@cmd, '-hdc', $disks[2]) if defined $disks[2];
328 push (@cmd, '-hdd', $disks[3]) if defined $disks[3];
329 push (@cmd, '-m', $mem);
330 push (@cmd, '-nographic') if $vga eq 'none';
331 push (@cmd, '-serial', 'stdio') if $serial_out && $vga ne 'none';
332 push (@cmd, '-S') if $debug eq 'monitor';
333 push (@cmd, '-s') if $debug eq 'gdb';
335 } elsif ($sim eq 'gsx') {
336 print "warning: VMware GSX Server doesn't support --$debug\n"
337 if $debug ne 'no-debug';
338 print "warning: VMware GSX Server doesn't support --no-vga\n"
340 print "warning: VMware GSX Server doesn't support --terminal\n"
341 if $vga eq 'terminal';
342 print "warning: VMware GSX Server doesn't support jitter\n"
345 open (VMX, ">pintos.vmx") or die "pintos.vmx: create: $!\n";
346 chmod 0777 & ~umask, "pintos.vmx";
347 print VMX "#! /usr/bin/vmware -G\n";
348 print VMX "config.version = 6\n";
349 print VMX "guestOS = \"linux\"\n";
350 print VMX "floppy0.present = FALSE\n";
352 unlink ("pintos.out");
353 print VMX "serial0.present = TRUE\n";
354 print VMX "serial0.fileType = \"file\"\n";
355 print VMX "serial0.fileName = \"pintos.out\"\n";
357 if (! -e 'null.bin') {
358 open (NULL, ">null.bin") or die "null.bin: create: $!\n";
362 for (my ($i) = 0; $i < 4; $i++) {
363 my ($dsk) = $disks[$i];
364 next if !defined $dsk;
370 my ($device) = "ide" . int ($i / 2) . ":" . ($i % 2);
371 print VMX "\n$device.present = TRUE\n";
372 print VMX "$device.deviceType = \"plainDisk\"\n";
373 print VMX "$device.fileName = \"$pln\"\n";
375 my (%geom) = disk_geometry ($dsk);
376 open (PLN, ">$pln") or die "$pln: create: $!\n";
377 print PLN "DRIVETYPE ide\n";
378 print PLN "#vm|VERSION 2\n";
379 print PLN "#vm|TOOLSVERSION 2\n";
380 print PLN "CYLINDERS $geom{C}\n";
381 print PLN "HEADS $geom{H}\n";
382 print PLN "SECTORS $geom{S}\n";
383 print PLN "#vm|CAPACITY $geom{CAPACITY}\n";
384 print PLN "ACCESS \"$dsk\" 0 $geom{CAPACITY}\n";
389 my ($vmx) = getcwd () . "/pintos.vmx";
390 system ("vmware-cmd -s register $vmx >&/dev/null");
391 system ("vmware-cmd $vmx stop hard >&/dev/null");
392 system ("vmware -l -G -x -q $vmx");
393 system ("vmware-cmd $vmx stop hard >&/dev/null");
398 my ($pid, $signal) = @_;
400 $SIG{$signal} = 'DEFAULT';
401 kill $signal, getpid ();
406 if (!defined ($pid)) {
410 # Running in child process.
414 # Running in parent process.
415 local $SIG{INT} = sub { relay_signal ($pid, "INT"); };
416 local $SIG{TERM} = sub { relay_signal ($pid, "TERM"); };
423 my ($disk, @args) = @_;
425 die "command line includes empty string" if grep (/^$/, @args);
426 my ($args) = join ("\0", @args) . "\0\0";
427 die "command line exceeds 128 bytes" if length ($args) > 128;
428 $args .= "\0" x (128 - length ($args));
430 print "writing command line to $disk...\n";
431 open (DISK, "+<$disk") or die "$disk: open: $!\n";
432 seek (DISK, 0x17e, 0) or die "$disk: seek: $!\n";
433 syswrite (DISK, $args) or die "$disk: write: $!\n";
434 close (DISK) or die "$disk: close: $!\n";
438 print join (' ', @_), "\n";
439 die "command failed\n" if xsystem (@_);
444 for my $dir (split (':', $ENV{PATH})) {
445 return $dir if -e "$dir/$target";
447 die "$target not in PATH\n";
450 sub bochs_disk_line {
451 my ($device, $file) = @_;
452 return "" if !defined $file;
453 my (%geom) = disk_geometry ($file);
454 return ("$device: type=disk, path=$file, mode=flat, "
455 . "cylinders=$geom{C}, heads=$geom{H}, spt=$geom{S}, "
456 . "translation=none\n");
461 my ($size) = -s $file;
462 die "$file: stat: $!\n" if !defined $size;
463 die "$file: size not a multiple of 512 bytes\n" if $size % 512;
464 my ($cylinders) = int ($size / (512 * 16 * 63));
465 $cylinders++ if $size % (512 * 16 * 63);
467 return (CAPACITY => $size / 512,
478 my (@part_names) = ("boot", "file system", "scratch", "swap");
479 my ($next_start) = 1;
483 my ($name) = $part_names[$i];
484 my ($file) = $files[$i];
489 if (($mb) = $file =~ /^\d+(\.\d+)?|\.\d+$/) {
490 $size = $mb * 63 * 16 * 512;
493 die ("$file: stat: $!\n");
497 die "$name: not a multiple of 512 bytes in size\n"
499 my ($sector_cnt) = $size / 512;
500 my ($start) = $next_start;
501 $next_start += $sector_cnt;
507 SECTORS => $sector_cnt});
509 die "Sorry, disk size (", ($sector_cnt * 512) / 1024 / 1024, " MB) "
510 . "exceeds limit (approx. 503 MB)\n"
511 if $sector_cnt > 1023 * 63 * 16;
513 my ($part_tbl) = "\0" x 446;
515 my ($bootable) = $p->{ROLE} == 0 ? 0x80 : 0x00;
516 my (@start_chs) = linear_to_chs ($p->{START});
517 my ($type) = $role2type{$p->{ROLE}};
518 my (@end_chs) = linear_to_chs ($p->{START} + $p->{SECTORS} - 1);
520 my ($part_tbl_entry) = pack ("C CCC C CCC V V",
522 pack_chs (@start_chs),
525 $p->{START}, $p->{SECTORS});
526 length ($part_tbl_entry) == 16 or die;
527 $part_tbl .= $part_tbl_entry;
529 $part_tbl .= "\0" x 16 while length ($part_tbl) < 510;
530 $part_tbl .= pack ("v", 0xaa55);
531 length ($part_tbl) == 512 or die;
534 open (DISK, ">$disk") or die "$disk: create: $!\n";
535 syswrite (DISK, $part_tbl) == 512 or die "$disk: write: $!\n";
538 $from_file = defined ($p->{FILE});
539 open (PART, "<$p->{FILE}") or die "$p->{FILE}: open: $!\n"
543 for (my ($ofs) = 0; $ofs < $p->{SECTORS}; $ofs += length ($buf)) {
544 my ($bytes_left) = ($p->{SECTORS} - $ofs) * 512;
545 my ($read_bytes) = $bytes_left > 16384 ? 16384 : $bytes_left;
548 my ($ret) = sysread (PART, $buf, $read_bytes);
549 die "$p->{FILE}: read: $!\n" if $ret < 0;
550 die "$p->{FILE}: unexpected end of file\n"
551 if $ret != $read_bytes;
553 $buf = "\0" x $read_bytes;
556 syswrite (DISK, $buf) == length ($buf)
557 or die "$p->{FILE}: write: $!\n"
560 close (PART) if $from_file;
563 close (DISK) or die "$disk: close: $!\n";
569 # We maintain these as constants.
572 my ($sectors_per_cylinder) = $heads * sectors;
575 my ($c) = int ($linear / $sectors_per_cylinder);
576 my ($cylinder_ofs) = $linear % $sectors_per_cylinder;
577 my ($h) = int ($cylinder_ofs / $sectors);
578 my ($s) = $cylinder_ofs % $sectors;
580 die if $c > 1023 || $h > 15 || $s > 63;
586 my ($c, $h, $s) = @_;
587 die if $c > 1023 || $h > 15 || $s > 63;
589 my ($pc, $ph, $ps) = ($h, $s | (($c & 0x300) >> 2), $c & 0xff);
590 die if $pc > 255 || $ph > 255 || ps > 255;
592 return ($pc, $ph, $ps);
597 open (DISK, "<$disk") or die "$disk: open: $!\n";
598 sysread (DISK, $part_tbl, 512) == 512 or die "$disk: read: $!\n";
601 my ($loader, @partitions, $signature);
602 ($loader, @partitions[0..3], $signature)
603 = unpack ("a446 (a16)4 v", $part_tbl);
605 die "$disk: invalid partition table signature\n" if $signature != 0xaa55;
608 for my $partition (@partitions) {
609 my ($bootable, @start_chs_packed, $type, @end_chs_packed,
610 $start, $sector_cnt);
611 ($bootable, $start_chs_packed[0...2], $type, @end_chs_packed[0...2],
613 = unpack ("C CCC C CCC V V", $partition) or die;
615 my ($role) = (reverse (%role2type{$type})){$type};
616 next if !defined ($role);
621 SECTORS => $sector_cnt});
630 open (DISK, "<$disk") or die "$disk: open: $!\n";
631 for my $p (read_part_tbl ()) {
632 use Fcntl 'SEEK_CUR';
634 my ($file) = $files[$p->{ROLE}];
635 next if !defined $file;
637 open (PART, ">$file") or die "$file: create: $!\n";
638 sysseek (DISK, $p->{START} * 512, SEEK_CUR) or die "$disk: seek: $!\n";
641 for (my ($ofs) = 0; $ofs < $p->{SECTORS}; $ofs += length ($buf)) {
642 my ($bytes_left) = ($p->{SECTORS} - $ofs) * 512;
643 my ($read_bytes) = $bytes_left > 16384 ? 16384 : $bytes_left;
645 my ($ret) = sysread (DISK, $buf, $read_bytes);
646 die "$p->{FILE}: read: $!\n" if $ret < 0;
647 die "$p->{FILE}: unexpected end of file\n"
648 if $ret != $read_bytes;
650 syswrite (PART, $buf) == length ($buf)
651 or die "$p->{FILE}: write: $!\n";
654 close (PART) or die "$file: close: $!\n";