Don't consider zero-size disks to exist.
[pintos-anon] / src / utils / pintos
1 #! /usr/bin/perl -w
2
3 use strict;
4 use POSIX;
5
6 our ($mem) = 4;
7 our ($serial_out) = 1;
8 our (@disks) = ("os.dsk", "fs.dsk", "scratch.dsk", "swap.dsk");
9 our ($sim);
10 our ($debug);
11 our ($vga);
12 our ($jitter, $realtime);
13
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") },
21
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") },
26
27             "run|get|put|make-disk" => \&cmd_option,
28
29             "m|memory=i" => \$mem,
30             "j|jitter=i" => sub { set_jitter (@_) },
31             "r|realtime" => sub { set_realtime () },
32
33             "v|no-vga" => sub { set_vga ('none'); },
34             "s|no-serial" => sub { $serial_out = 0; },
35             "t|terminal" => sub { set_vga ('terminal'); },
36
37             "h|help" => sub { usage (0); },
38
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])
43     or exit 1;
44
45 $sim = "bochs" if !defined $sim;
46 $debug = "no-debug" if !defined $debug;
47 $vga = "window" if !defined $vga;
48
49 sub set_sim {
50     my ($new_sim) = @_;
51     die "--$new_sim conflicts with --$sim\n"
52         if defined ($sim) && $sim ne $new_sim;
53     $sim = $new_sim;
54 }
55
56 sub set_debug {
57     my ($new_debug) = @_;
58     die "--$new_debug conflicts with --$debug\n"
59         if defined ($debug) && $debug ne $new_debug;
60     $debug = $new_debug;
61 }
62
63 sub set_vga {
64     my ($new_vga) = @_;
65     if (defined ($vga) && $vga ne $new_vga) {
66         print "warning: conflicting vga display options\n";
67     }
68     $vga = $new_vga;
69 }
70
71 sub set_jitter {
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;
77 }
78
79 sub set_realtime {
80     die "--realtime conflicts with --jitter\n" if defined $jitter;
81     $realtime = 1;
82 }
83
84 sub cmd_option {
85     # Force an end to option processing, as with --.
86     die ("!FINISH");
87 }
88
89 die "no command specified; use --help for help\n"
90     if @ARGV < 1;
91 my ($cmd) = shift @ARGV;
92 if ($cmd eq 'run') {
93     run_vm (@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;
99
100     create_disk ($file, int ($mb * 1008));
101 } elsif ($cmd eq 'put') {
102     # Take a -f option to combine formatting with putting.
103     my ($format) = 0;
104     if (@ARGV > 0 && $ARGV[0] eq '-f') {
105         shift @ARGV;
106         $format = 1;
107     }
108
109     usage () if @ARGV != 1 && @ARGV != 2;
110     my ($hostfn, $guestfn) = @ARGV;
111     $guestfn = $hostfn if !defined $guestfn;
112
113     # Create scratch disk from file.
114     die "$hostfn: $!\n" if ! -e $hostfn;
115     my ($size) = -s _;
116     if ($size) {
117         copy_pad ($hostfn, "scratch.dsk", 512);
118     } else {
119         open (SCRATCH, ">scratch.dsk") or die "scratch.dsk: create: $!\n";
120         syswrite (SCRATCH, "\0" x 512);
121         close (SCRATCH);
122     }
123
124     # Do copy.
125     my (@cmd) = ("-ci", $guestfn, $size, "-q");
126     unshift (@cmd, "-f") if $format;
127     run_vm (@cmd);
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;
133
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;
142
143     # Do copy.
144     run_vm ("-co", $guestfn, "-q");
145
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";
150     my ($input);
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]?";
154     my ($src);
155     read (SRC, $src, $size) == $size or die "$disks[2]: read error\n";
156     print DST $src or die "$hostfn: write error\n";
157     close (DST);
158     close (SRC);
159 } elsif ($cmd eq 'help') {
160     usage (0);
161 } else {
162     die "unknown command `$cmd'; use --help for help\n";
163 }
164 exit 0;
165
166 sub usage {
167     my ($exitcode) = @_;
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";
198     exit $exitcode;
199 }
200
201 sub copy_pad {
202     my ($src, $dst, $blocksize) = @_;
203     run_command ("dd", "if=$src", "of=$dst", "bs=$blocksize", "conv=sync");
204 }
205
206 sub create_disk {
207     my ($disk, $kb) = @_;
208     run_command ("dd", "if=/dev/zero", "of=$disk", "bs=1024", "count=$kb");
209 }
210
211 sub run_vm {
212     my (@args) = @_;
213
214     our (@disks);
215
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];
218     for my $i (1...3) {
219         undef $disks[$i] if ! -s $disks[$i];
220     }
221
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";
227         }
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";
231         }
232     }
233
234     write_cmd_line ($disks[0], @args);
235
236     if ($sim eq 'bochs') {
237         my ($bin);
238         if ($debug eq 'no-debug') {
239             $bin = 'bochs';
240         } elsif ($debug eq 'monitor') {
241             $bin = 'bochs-dbg';
242         } elsif ($debug eq 'gdb') {
243             $bin = 'bochs-gdb';
244         }
245
246         my ($bochsbin) = search_path ($bin);
247         my ($bochsshare) = "$bochsbin/../share/bochs";
248
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";
261         if (!$realtime) {
262             print BOCHSRC "clock: sync=none, time0=0\n";
263         } else {
264             print BOCHSRC "clock: sync=realtime, time0=0\n";
265         }
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"
270                 if $serial_out;
271             print BOCHSRC "display_library: nogui\n"
272                 if $vga eq 'none';
273         } else {
274             print BOCHSRC "display_library: term\n";
275         }
276         close (BOCHSRC);
277
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,
285             # which is weird.
286         } elsif (WIFSIGNALED ($exit)) {
287             die "Bochs died with signal ", WTERMSIG ($exit), "\n";
288         } else {
289             die "Bochs died: code $exit\n";
290         }
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"
295             if defined $jitter;
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';
306         run_command (@cmd);
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"
311             if $vga eq 'none';
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"
315             if defined $jitter;
316
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";
323
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";
328
329         if (! -e 'null.bin') {
330             open (NULL, ">null.bin") or die "null.bin: create: $!\n";
331             close (NULL);
332         }
333
334         for (my ($i) = 0; $i < 4; $i++) {
335             my ($dsk) = $disks[$i];
336             next if !defined $dsk;
337
338             my ($pln) = $dsk;
339             $pln =~ s/\.dsk//;
340             $pln .= ".pln";
341
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";
346
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";
357             close (PLN);
358         }
359         close (VMX);
360
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");
366     }
367 }
368
369 sub relay_signal {
370     my ($pid, $signal) = @_;
371     kill $signal, $pid;
372     $SIG{$signal} = 'DEFAULT';
373     kill $signal, getpid ();
374 }
375
376 sub xsystem {
377     my ($pid) = fork;
378     if (!defined ($pid)) {
379         # Fork failed.
380         die "fork: $!\n";
381     } elsif (!$pid) {
382         # Running in child process.
383         exec (@_);
384         exit (1);
385     } else {
386         # Running in parent process.
387         local $SIG{INT} = sub { relay_signal ($pid, "INT"); };
388         local $SIG{TERM} = sub { relay_signal ($pid, "TERM"); };
389         waitpid ($pid, 0);
390         return $?;
391     }
392 }
393
394 sub write_cmd_line {
395     my ($disk, @args) = @_;
396
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));
401
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";
407 }
408
409 sub run_command {
410     print join (' ', @_), "\n";
411     die "command failed\n" if xsystem (@_);
412 }
413
414 sub search_path {
415     my ($target) = @_;
416     for my $dir (split (':', $ENV{PATH})) {
417         return $dir if -e "$dir/$target";
418     }
419     die "$target not in PATH\n";
420 }
421
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";
429 }
430
431 sub disk_geometry {
432     my ($file) = @_;
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);
438
439     return (CAPACITY => $size / 512,
440             C => $cylinders,
441             H => 16,
442             S => 63);
443 }