8 our (@disks) = ("os.dsk", "fs.dsk", "scratch.dsk", "swap.dsk");
12 our ($jitter, $realtime);
14 use Getopt::Long qw(:config require_order bundling);
15 unshift (@ARGV, split (' ', $ENV{PINTOSOPTS}))
16 if defined $ENV{PINTOSOPTS};
17 GetOptions ("sim=s" => sub { set_sim (@_) },
18 "bochs" => sub { set_sim ("bochs") },
19 "qemu" => sub { set_sim ("qemu") },
20 "gsx" => sub { set_sim ("gsx") },
22 "debug=s" => sub { set_debug (@_) },
23 "no-debug" => sub { set_debug ("no-debug") },
24 "monitor" => sub { set_debug ("monitor") },
25 "gdb" => sub { set_debug ("gdb") },
27 "run|get|put|make-disk" => \&cmd_option,
29 "m|memory=i" => \$mem,
30 "j|jitter=i" => sub { set_jitter (@_) },
31 "r|realtime" => sub { set_realtime () },
33 "v|no-vga" => sub { set_vga ('none'); },
34 "s|no-serial" => sub { $serial_out = 0; },
35 "t|terminal" => sub { set_vga ('terminal'); },
37 "h|help" => sub { usage (0); },
39 "0|os-disk|disk-0|hda=s" => \$disks[0],
40 "1|fs-disk|disk-1|hdb=s" => \$disks[1],
41 "2|scratch-disk|disk-2|hdc=s" => \$disks[2],
42 "3|swap-disk|disk-3|hdd=s" => \$disks[3])
45 $sim = "bochs" if !defined $sim;
46 $debug = "no-debug" if !defined $debug;
47 $vga = "window" if !defined $vga;
51 die "--$new_sim conflicts with --$sim\n"
52 if defined ($sim) && $sim ne $new_sim;
58 die "--$new_debug conflicts with --$debug\n"
59 if defined ($debug) && $debug ne $new_debug;
65 if (defined ($vga) && $vga ne $new_vga) {
66 print "warning: conflicting vga display options\n";
72 my ($new_jitter) = @_;
73 die "--realtime conflicts with --jitter\n" if defined $realtime;
74 die "different --jitter already defined\n"
75 if defined $jitter && $jitter != $new_jitter;
76 $jitter = $new_jitter;
80 die "--realtime conflicts with --jitter\n" if defined $jitter;
85 # Force an end to option processing, as with --.
89 die "no command specified; use --help for help\n"
91 my ($cmd) = shift @ARGV;
94 } elsif ($cmd eq 'make-disk') {
95 usage () if @ARGV != 2;
96 my ($file, $mb) = @ARGV;
97 usage () if $mb !~ /^\d+(\.\d+)?|\.\d+$/;
98 die "$file: already exists\n" if -e $file;
100 create_disk ($file, int ($mb * 1008));
101 } elsif ($cmd eq 'put') {
102 # Take a -f option to combine formatting with putting.
104 if (@ARGV > 0 && $ARGV[0] eq '-f') {
109 usage () if @ARGV != 1 && @ARGV != 2;
110 my ($hostfn, $guestfn) = @ARGV;
111 $guestfn = $hostfn if !defined $guestfn;
113 # Create scratch disk from file.
114 die "$hostfn: $!\n" if ! -e $hostfn;
117 copy_pad ($hostfn, "scratch.dsk", 512);
119 open (SCRATCH, ">scratch.dsk") or die "scratch.dsk: create: $!\n";
120 syswrite (SCRATCH, "\0" x 512);
125 my (@cmd) = ("-ci", $guestfn, $size, "-q");
126 unshift (@cmd, "-f") if $format;
128 } elsif ($cmd eq 'get') {
129 usage () if @ARGV != 1 && @ARGV != 2;
130 my ($guestfn, $hostfn) = @ARGV;
131 $hostfn = $guestfn if !defined $hostfn;
132 die "$hostfn: already exists\n" if -e $hostfn;
134 # Create scratch disk big enough for any file in the filesystem
135 # (modulo sparse files).
136 die "$disks[1]: $!\n" if ! -e $disks[1];
137 my ($fs_size) = -s _;
138 my ($scratch_size) = -s $disks[2];
139 $scratch_size = 0 if !defined $scratch_size;
140 create_disk ($disks[2], $fs_size / 1024 + 16)
141 if $scratch_size < $fs_size + 16384;
144 run_vm ("-co", $guestfn, "-q");
146 # Read out scratch disk.
147 print "copying $guestfn from $disks[2] to $hostfn...\n";
148 open (SRC, "<$disks[2]") or die "$disks[2]: open: $!\n";
149 open (DST, ">$hostfn") or die "$hostfn: create: $!\n";
151 read (SRC, $input, 512) == 512 or die "$disks[2]: read error\n";
152 my ($size) = unpack ("V", $input);
153 $size != 0xffffffff or die "$guestfn: too big for $disks[2]?";
155 read (SRC, $src, $size) == $size or die "$disks[2]: read error\n";
156 print DST $src or die "$hostfn: write error\n";
159 } elsif ($cmd eq 'help') {
162 die "unknown command `$cmd'; use --help for help\n";
168 $exitcode = 1 unless defined $exitcode;
169 print "pintos, a utility for invoking Pintos in a simulator\n";
170 print "Usage: pintos [OPTION...] COMMAND [ARG...]\n";
171 print "where COMMAND is one of the following:\n";
172 print " run [CMDLINE...] run a VM in the simulator\n";
173 print " make-disk FILE.DSK SIZE create FILE.DSK as empty SIZE MB disk\n";
174 print " put HOSTFN [GUESTFN] copy HOSTFN into VM (as GUESTFN)\n";
175 print " get GUESTFN [HOSTFN] copy GUESTFN out of VM (to HOSTFN)\n";
176 print " help print this help message and exit\n";
177 print "Simulator options:\n";
178 print " --bochs (default) Use Bochs as simulator\n";
179 print " --qemu Use qemu as simulator\n";
180 print " --gsx Use VMware GSX Server 3.x as simulator\n";
181 print "Debugger options:\n";
182 print " --no-debug (default) No debugger\n";
183 print " --monitor Debug with simulator's monitor\n";
184 print " --gdb Debug with gdb\n";
185 print "Display options: (default is VGA + serial)\n";
186 print " -v, --no-vga No VGA display\n";
187 print " -s, --no-serial No serial output\n";
188 print " -t, --terminal Display VGA in terminal (Bochs only)\n";
189 print "VM options:\n";
190 print " -j SEED Randomize timer interrupts (Bochs only)\n";
191 print " -r, --realtime Use realistic, but not reproducible, timings\n";
192 print " -m, --mem=MB Run VM with MB megabytes of physical memory\n";
193 print "Disk options:\n";
194 print " --os-disk=DISK Set OS disk file (default: os.dsk)\n";
195 print " --fs-disk=DISK Set FS disk file (default: fs.dsk)\n";
196 print " --scratch-disk=DISK Set scratch disk (default: scratch.dsk)\n";
197 print " --swap-disk=DISK Set swap disk file (default: swap.dsk)\n";
202 my ($src, $dst, $blocksize) = @_;
203 run_command ("dd", "if=$src", "of=$dst", "bs=$blocksize", "conv=sync");
207 my ($disk, $kb) = @_;
208 run_command ("dd", "if=/dev/zero", "of=$disk", "bs=1024", "count=$kb");
216 die "$disks[0]: can't find OS disk\n" if ! -e $disks[0];
217 die "$disks[0]: OS disk cannot have zero size\n" if ! -s $disks[0];
219 undef $disks[$i] if ! -s $disks[$i];
222 if (my ($project) = `pwd` =~ /\b(threads|userprog|vm|filesys)\b/) {
223 if ((grep ($project eq $_, qw (userprog vm filesys)))
224 && !defined ($disks[1])) {
225 print STDERR "warning: it looks like you're running the $project ";
226 print STDERR "project, but no file system disk is present\n";
228 if ($project eq 'vm' && !defined $disks[3]) {
229 print STDERR "warning: it looks like you're running the $project ";
230 print STDERR "project, but no swap disk is present\n";
234 write_cmd_line ($disks[0], @args);
236 if ($sim eq 'bochs') {
238 if ($debug eq 'no-debug') {
240 } elsif ($debug eq 'monitor') {
242 } elsif ($debug eq 'gdb') {
246 my ($bochsbin) = search_path ($bin);
247 my ($bochsshare) = "$bochsbin/../share/bochs";
249 open (BOCHSRC, ">bochsrc.txt") or die "bochsrc.txt: create: $!\n";
250 print BOCHSRC "romimage: file=$bochsshare/BIOS-bochs-latest, "
251 . "address=0xf0000\n";
252 print BOCHSRC "vgaromimage: $bochsshare/VGABIOS-lgpl-latest\n";
253 print BOCHSRC bochs_disk_line ("ata0-master", $disks[0]);
254 print BOCHSRC bochs_disk_line ("ata0-slave", $disks[1]);
255 print BOCHSRC "ata1: enabled=1, ioaddr1=0x170, ioaddr2=0x370, irq=15\n"
256 if defined ($disks[2]) || defined ($disks[3]);
257 print BOCHSRC bochs_disk_line ("ata1-master", $disks[2]);
258 print BOCHSRC bochs_disk_line ("ata1-slave", $disks[3]);
259 print BOCHSRC "boot: c\n";
260 print BOCHSRC "ips: 1000000\n";
262 print BOCHSRC "clock: sync=none, time0=0\n";
264 print BOCHSRC "clock: sync=realtime, time0=0\n";
266 print BOCHSRC "megs: $mem\n";
267 print BOCHSRC "log: bochsout.txt\n";
268 if ($vga ne 'terminal') {
269 print BOCHSRC "com1: enabled=1, dev=/dev/stdout\n"
271 print BOCHSRC "display_library: nogui\n"
274 print BOCHSRC "display_library: term\n";
278 my (@cmd) = ($bin, '-q');
279 push (@cmd, '-j', $jitter) if defined $jitter;
280 print join (' ', @cmd), "\n";
281 my ($exit) = xsystem (@cmd);
282 if (WIFEXITED ($exit)) {
283 # Bochs exited normally.
284 # Ignore the exit code; Bochs normally exits with status 1,
286 } elsif (WIFSIGNALED ($exit)) {
287 die "Bochs died with signal ", WTERMSIG ($exit), "\n";
289 die "Bochs died: code $exit\n";
291 } elsif ($sim eq 'qemu') {
292 print "warning: qemu doesn't support --terminal\n"
293 if $vga eq 'terminal';
294 print "warning: qemu doesn't support jitter\n"
296 my (@cmd) = ('qemu');
297 push (@cmd, '-hda', $disks[0]) if defined $disks[0];
298 push (@cmd, '-hdb', $disks[1]) if defined $disks[1];
299 push (@cmd, '-hdc', $disks[2]) if defined $disks[2];
300 push (@cmd, '-hdd', $disks[3]) if defined $disks[3];
301 push (@cmd, '-m', $mem);
302 push (@cmd, '-nographic') if $vga eq 'none';
303 push (@cmd, '-serial', 'stdio') if $serial_out && $vga ne 'none';
304 push (@cmd, '-S') if $debug eq 'monitor';
305 push (@cmd, '-s') if $debug eq 'gdb';
307 } elsif ($sim eq 'gsx') {
308 print "warning: VMware GSX Server doesn't support --$debug\n"
309 if $debug ne 'no-debug';
310 print "warning: VMware GSX Server doesn't support --no-vga\n"
312 print "warning: VMware GSX Server doesn't support --terminal\n"
313 if $vga eq 'terminal';
314 print "warning: VMware GSX Server doesn't support jitter\n"
317 open (VMX, ">pintos.vmx") or die "pintos.vmx: create: $!\n";
318 chmod 0777 & ~umask, "pintos.vmx";
319 print VMX "#! /usr/bin/vmware -G\n";
320 print VMX "config.version = 6\n";
321 print VMX "guestOS = \"linux\"\n";
322 print VMX "floppy0.present = FALSE\n";
324 unlink ("pintos.out");
325 print VMX "serial0.present = TRUE\n";
326 print VMX "serial0.fileType = \"file\"\n";
327 print VMX "serial0.fileName = \"pintos.out\"\n";
329 if (! -e 'null.bin') {
330 open (NULL, ">null.bin") or die "null.bin: create: $!\n";
334 for (my ($i) = 0; $i < 4; $i++) {
335 my ($dsk) = $disks[$i];
336 next if !defined $dsk;
342 my ($device) = "ide" . int ($i / 2) . ":" . ($i % 2);
343 print VMX "\n$device.present = TRUE\n";
344 print VMX "$device.deviceType = \"plainDisk\"\n";
345 print VMX "$device.fileName = \"$pln\"\n";
347 my (%geom) = disk_geometry ($dsk);
348 open (PLN, ">$pln") or die "$pln: create: $!\n";
349 print PLN "DRIVETYPE ide\n";
350 print PLN "#vm|VERSION 2\n";
351 print PLN "#vm|TOOLSVERSION 2\n";
352 print PLN "CYLINDERS $geom{C}\n";
353 print PLN "HEADS $geom{H}\n";
354 print PLN "SECTORS $geom{S}\n";
355 print PLN "#vm|CAPACITY $geom{CAPACITY}\n";
356 print PLN "ACCESS \"$dsk\" 0 $geom{CAPACITY}\n";
361 my ($vmx) = getcwd () . "/pintos.vmx";
362 system ("vmware-cmd -s register $vmx >&/dev/null");
363 system ("vmware-cmd $vmx stop hard >&/dev/null");
364 system ("vmware -l -G -x -q $vmx");
365 system ("vmware-cmd $vmx stop hard >&/dev/null");
370 my ($pid, $signal) = @_;
372 $SIG{$signal} = 'DEFAULT';
373 kill $signal, getpid ();
378 if (!defined ($pid)) {
382 # Running in child process.
386 # Running in parent process.
387 local $SIG{INT} = sub { relay_signal ($pid, "INT"); };
388 local $SIG{TERM} = sub { relay_signal ($pid, "TERM"); };
395 my ($disk, @args) = @_;
397 die "command line includes empty string" if grep (/^$/, @args);
398 my ($args) = join ("\0", @args) . "\0\0";
399 die "command line exceeds 128 bytes" if length ($args) > 128;
400 $args .= "\0" x (128 - length ($args));
402 print "writing command line to $disk...\n";
403 open (DISK, "+<$disk") or die "$disk: open: $!\n";
404 seek (DISK, 0x17e, 0) or die "$disk: seek: $!\n";
405 syswrite (DISK, $args) or die "$disk: write: $!\n";
406 close (DISK) or die "$disk: close: $!\n";
410 print join (' ', @_), "\n";
411 die "command failed\n" if xsystem (@_);
416 for my $dir (split (':', $ENV{PATH})) {
417 return $dir if -e "$dir/$target";
419 die "$target not in PATH\n";
422 sub bochs_disk_line {
423 my ($device, $file) = @_;
424 return "" if !defined $file;
425 my (%geom) = disk_geometry ($file);
426 return "$device: type=disk, path=$file, mode=flat, "
427 . "cylinders=$geom{C}, heads=$geom{H}, spt=$geom{S}, "
428 . "translation=none\n";
433 my ($size) = -s $file;
434 die "$file: stat: $!\n" if !defined $size;
435 die "$file: size not a multiple of 512 bytes\n" if $size % 512;
436 my ($cylinders) = int ($size / (512 * 16 * 63));
437 $cylinders++ if $size % (512 * 16 * 63);
439 return (CAPACITY => $size / 512,