Fix `get' behavior.
[pintos-anon] / src / utils / pintos
1 #! /usr/bin/perl -w
2
3 use strict;
4
5 our ($mem) = 4;
6 our ($serial_out) = 1;
7 our (@disks) = ("os.dsk", "fs.dsk", "scratch.dsk", "swap.dsk");
8 our ($sim);
9 our ($debug);
10 our ($vga);
11 our ($jitter);
12
13 use Getopt::Long qw(:config require_order bundling);
14 GetOptions ("sim=s" => sub { set_sim (@_) },
15             "bochs" => sub { set_sim ("bochs") },
16             "qemu" => sub { set_sim ("qemu") },
17             "gsx" => sub { set_sim ("gsx") },
18
19             "debug=s" => sub { set_debug (@_) },
20             "no-debug" => sub { set_debug ("no-debug") },
21             "monitor" => sub { set_debug ("monitor") },
22             "gdb" => sub { set_debug ("gdb") },
23             
24             "run|get|put|make-disk" => \&cmd_option,
25             
26             "m|memory=i" => \$mem,
27             "j|jitter=i" => \$jitter,
28             
29             "v|no-vga" => sub { set_vga ('none'); },
30             "s|no-serial" => sub { $serial_out = 0; },
31             "t|terminal" => sub { set_vga ('terminal'); },
32             
33             "h|help" => sub { usage (0); },
34
35             "0|os-disk|disk-0|hda=s" => \$disks[0],
36             "1|fs-disk|disk-1|hdb=s" => \$disks[1],
37             "2|scratch-disk|disk-2|hdc=s" => \$disks[2],
38             "3|swap-disk|disk-3|hdd=s" => \$disks[3])
39     or exit 1;
40
41 $sim = "bochs" if !defined $sim;
42 $debug = "no-debug" if !defined $debug;
43 $vga = "window" if !defined $vga;
44
45 sub set_sim {
46     my ($new_sim) = @_;
47     die "--$new_sim conflicts with --$sim\n"
48         if defined ($sim) && $sim ne $new_sim;
49     $sim = $new_sim;
50 }
51
52 sub set_debug {
53     my ($new_debug) = @_;
54     die "--$new_debug conflicts with --$debug\n"
55         if defined ($debug) && $debug ne $new_debug;
56     $debug = $new_debug;
57 }
58
59 sub set_vga {
60     my ($new_vga) = @_;
61     if (defined ($vga) && $vga ne $new_vga) {
62         print "warning: conflicting vga display options\n";
63     }
64     $vga = $new_vga;
65 }
66
67 sub cmd_option {
68     # Force an end to option processing, as with --.
69     die ("!FINISH");
70 }
71
72 die "no command specified; use --help for help\n"
73     if @ARGV < 1;
74 my ($cmd) = shift @ARGV;
75 if ($cmd eq 'run') {
76     run_vm ('EXEC', @ARGV);
77 } elsif ($cmd eq 'make-disk') {
78     usage () if @ARGV != 2;
79     my ($file, $mb) = @ARGV;
80     usage () if $mb !~ /^\d+(\.\d+)?|\.\d+$/;
81     die "$file: already exists\n" if -e $file;
82
83     create_disk ($file, int ($mb * 1008));
84 } elsif ($cmd eq 'put') {
85     # Take a -f option to combine formatting with putting.
86     my ($format) = 0;
87     if (@ARGV > 0 && $ARGV[0] eq '-f') {
88         shift @ARGV;
89         $format = 1;
90     }
91
92     usage () if @ARGV != 1 && @ARGV != 2;
93     my ($hostfn, $guestfn) = @ARGV;
94     $guestfn = $hostfn if !defined $guestfn;
95
96     # Create scratch disk from file.
97     die "$hostfn: $!\n" if ! -e $hostfn;
98     my ($size) = -s _;
99     copy_pad ($hostfn, "scratch.dsk", 512);
100
101     # Do copy.
102     my (@cmd) = ("-ci", $guestfn, $size, "-q");
103     unshift (@cmd, "-f") if $format;
104     run_vm ('EXEC', @cmd);
105 } elsif ($cmd eq 'get') {
106     usage () if @ARGV != 1 && @ARGV != 2;
107     my ($guestfn, $hostfn) = @ARGV;
108     $hostfn = $guestfn if !defined $hostfn;
109     die "$hostfn: already exists\n" if -e $hostfn;
110
111     # Create scratch disk big enough for any file in the filesystem
112     # (modulo sparse files).
113     die "$disks[1]: $!\n" if ! -e $disks[1];
114     my ($fs_size) = -s _;
115     my ($scratch_size) = -s $disks[2];
116     $scratch_size = 0 if !defined $scratch_size;
117     create_disk ($disks[2], $fs_size / 1024 + 16)
118         if $scratch_size < $fs_size + 16384;
119
120     # Do copy.
121     run_vm ('FORK', "-co", $guestfn, "-q");
122
123     # Read out scratch disk.
124     print "copying $guestfn from $disks[2] to $hostfn...\n";
125     open (SRC, "<$disks[2]") or die "$disks[2]: open: $!\n";
126     open (DST, ">$hostfn") or die "$hostfn: create: $!\n";
127     my ($input);
128     read (SRC, $input, 512) == 512 or die "$disks[2]: read error\n";
129     my ($size) = unpack ("V", $input);
130     $size != 0xffffffff or die "$guestfn: too big for $disks[2]?";
131     my ($src);
132     read (SRC, $src, $size) == $size or die "$disks[2]: read error\n";
133     print DST $src or die "$hostfn: write error\n";
134     close (DST);
135     close (SRC);
136 } elsif ($cmd eq 'help') {
137     usage (0);
138 } else {
139     die "unknown command `$cmd'; use --help for help\n";
140 }
141 exit 0;
142
143 sub usage {
144     my ($exitcode) = @_;
145     $exitcode = 1 unless defined $exitcode;
146     print "pintos, a utility for invoking Pintos in a simulator\n";
147     print "Usage: pintos [OPTION...] COMMAND [ARG...]\n";
148     print "where COMMAND is one of the following:\n";
149     print "  run [CMDLINE...]        run a VM in the simulator\n";
150     print "  make-disk FILE.DSK SIZE create FILE.DSK as empty SIZE MB disk\n";
151     print "  put HOSTFN [GUESTFN]    copy HOSTFN into VM (as GUESTFN)\n";
152     print "  get GUESTFN [HOSTFN]    copy GUESTFN out of VM (to HOSTFN)\n";
153     print "  help                    print this help message and exit\n";
154     print "Simulator options:\n";
155     print "  --bochs          (default) Use Bochs as simulator\n";
156     print "  --qemu           Use qemu as simulator\n";
157     print "  --gsx            Use VMware GSX Server 3.x as simulator\n";
158     print "Debugger options:\n";
159     print "  --no-debug       (default) No debugger\n";
160     print "  --monitor        Debug with simulator's monitor\n";
161     print "  --gdb            Debug with gdb\n";
162     print "Display options: (default is VGA + serial)\n";
163     print "  -v, --no-vga     No VGA display\n";
164     print "  -s, --no-serial  No serial output\n";
165     print "  -t, --terminal   Display VGA in terminal (Bochs only)\n";
166     print "VM options:\n";
167     print "  -j SEED          Randomize timer interrupts (Bochs only)\n";
168     print "  -m, --mem=MB     Run VM with MB megabytes of physical memory\n";
169     print "Disk options:\n";
170     print "  --os-disk=DISK    Set OS disk file (default: os.dsk)\n";
171     print "  --fs-disk=DISK    Set FS disk file (default: fs.dsk)\n";
172     print "  --scratch-disk=DISK   Set scratch disk (default: scratch.dsk)\n";
173     print "  --swap-disk=DISK  Set swap disk file (default: swap.dsk)\n";
174     exit $exitcode;
175 }
176
177 sub copy_pad {
178     my ($src, $dst, $blocksize) = @_;
179     run_command ("dd", "if=$src", "of=$dst", "bs=$blocksize", "conv=sync");
180 }
181
182 sub create_disk {
183     my ($disk, $kb) = @_;
184     run_command ("dd", "if=/dev/zero", "of=$disk", "bs=1024", "count=$kb");
185 }
186
187 sub run_vm {
188     my ($fork) = shift;
189     $fork eq 'FORK' || $fork eq 'EXEC' or die;
190
191     our (@disks);
192
193     die "$disks[0]: can't find OS disk\n" if ! -e $disks[0];
194     for my $i (1...3) {
195         undef $disks[$i] if ! -e $disks[$i];
196     }
197
198     write_cmd_line ($disks[0], @_);
199
200     if ($sim eq 'bochs') {
201         my ($bin);
202         if ($debug eq 'no-debug') {
203             $bin = 'bochs';
204         } elsif ($debug eq 'monitor') {
205             $bin = 'bochs-dbg';
206         } elsif ($debug eq 'gdb') {
207             $bin = 'bochs-gdb';
208         }
209
210         my ($bochsbin) = search_path ($bin);
211         my ($bochsshare) = "$bochsbin/../share/bochs";
212
213         open (BOCHSRC, ">bochsrc.txt") or die "bochsrc.txt: create: $!\n";
214         print BOCHSRC "romimage: file=$bochsshare/BIOS-bochs-latest, "
215             . "address=0xf0000\n";
216         print BOCHSRC "vgaromimage: $bochsshare/VGABIOS-lgpl-latest\n";
217         print BOCHSRC bochs_disk_line ("ata0-master", $disks[0]);
218         print BOCHSRC bochs_disk_line ("ata0-slave", $disks[1]);
219         print BOCHSRC "ata1: enabled=1, ioaddr1=0x170, ioaddr2=0x370, irq=15\n"
220             if defined ($disks[2]) || defined ($disks[3]);
221         print BOCHSRC bochs_disk_line ("ata1-master", $disks[2]);
222         print BOCHSRC bochs_disk_line ("ata1-slave", $disks[3]);
223         print BOCHSRC "boot: c\n";
224         print BOCHSRC "ips: 1000000\n";
225         print BOCHSRC "clock: sync=none, time0=0\n";
226         print BOCHSRC "megs: $mem\n";
227         print BOCHSRC "log: bochsout.txt\n";
228         if ($vga ne 'terminal') {
229             print BOCHSRC "com1: enabled=1, dev=/dev/stdout\n"
230                 if $serial_out;
231             print BOCHSRC "display_library: nogui\n"
232                 if $vga eq 'none';
233         } else {
234             print BOCHSRC "display_library: term\n";
235         }
236         close (BOCHSRC);
237
238         my (@cmd) = ($bin, '-q');
239         push (@cmd, '-j', $jitter) if defined $jitter;
240         print join (' ', @cmd), "\n";
241         $fork eq 'EXEC' ? exec (@cmd) : system (@cmd);
242     } elsif ($sim eq 'qemu') {
243         print "warning: qemu doesn't support --terminal\n"
244             if $vga eq 'terminal';
245         print "warning: qemu doesn't support jitter\n"
246             if defined $jitter;
247         my (@cmd) = ('qemu');
248         push (@cmd, '-hda', $disks[0]) if defined $disks[0];
249         push (@cmd, '-hdb', $disks[1]) if defined $disks[1];
250         push (@cmd, '-hdc', $disks[2]) if defined $disks[2];
251         push (@cmd, '-hdd', $disks[3]) if defined $disks[3];
252         push (@cmd, '-m', $mem);
253         push (@cmd, '-nographic') if $vga eq 'none';
254         push (@cmd, '-serial', 'stdio') if $serial_out && $vga ne 'none';
255         push (@cmd, '-S') if $debug eq 'monitor';
256         push (@cmd, '-s') if $debug eq 'gdb';
257         run_command (@cmd);
258     } elsif ($sim eq 'gsx') {
259         print "warning: VMware GSX Server doesn't support --$debug\n"
260             if $debug ne 'no-debug';
261         print "warning: VMware GSX Server doesn't support --no-vga\n"
262             if $vga eq 'none';
263         print "warning: VMware GSX Server doesn't support --terminal\n"
264             if $vga eq 'terminal';
265         print "warning: VMware GSX Server doesn't support jitter\n"
266             if defined $jitter;
267
268         open (VMX, ">pintos.vmx") or die "pintos.vmx: create: $!\n";
269         chmod 0777 & ~umask, "pintos.vmx";
270         print VMX "#! /usr/bin/vmware -G\n";
271         print VMX "config.version = 6\n";
272         print VMX "guestOS = \"linux\"\n";
273         print VMX "floppy0.present = FALSE\n";
274
275         if (! -e 'null.bin') {
276             open (NULL, ">null.bin") or die "null.bin: create: $!\n";
277             close (NULL);
278         }
279
280         for (my ($i) = 0; $i < 4; $i++) {
281             my ($dsk) = $disks[$i];
282             next if !defined $dsk;
283
284             my ($pln) = $dsk;
285             $pln =~ s/\.dsk//;
286             $pln .= ".pln";
287
288             my ($device) = "ide" . int ($i / 2) . ":" . ($i % 2);
289             print VMX "\n$device.present = TRUE\n";
290             print VMX "$device.deviceType = \"plainDisk\"\n";
291             print VMX "$device.fileName = \"$pln\"\n";
292
293             my (%geom) = disk_geometry ($dsk);
294             open (PLN, ">$pln") or die "$pln: create: $!\n";
295             print PLN "DRIVETYPE        ide\n";
296             print PLN "#vm|VERSION      2\n";
297             print PLN "#vm|TOOLSVERSION 2\n";
298             print PLN "CYLINDERS        $geom{C}\n";
299             print PLN "HEADS            $geom{H}\n";
300             print PLN "SECTORS          $geom{S}\n";
301             print PLN "#vm|CAPACITY     $geom{CAPACITY}\n";
302             print PLN "ACCESS \"$dsk\" 0 $geom{CAPACITY}\n";
303             close (PLN);
304         }
305         close (VMX);
306
307         use Cwd;
308         my ($vmx) = getcwd () . "/pintos.vmx";
309         system ("vmware-cmd -s register $vmx >&/dev/null");
310         system ("vmware-cmd $vmx stop hard >&/dev/null");
311         system ("vmware -l -G -x -q $vmx");
312         system ("vmware-cmd $vmx stop hard >&/dev/null");
313     }
314 }
315
316 sub write_cmd_line {
317     my ($disk, @args) = @_;
318
319     die "command line includes empty string" if grep (/^$/, @args);
320     my ($args) = join ("\0", @args) . "\0\0";
321     die "command line exceeds 128 bytes" if length ($args) > 128;
322     $args .= "\0" x (128 - length ($args));
323
324     print "writing command line to $disk...\n";
325     open (DISK, "+<$disk") or die "$disk: open: $!\n";
326     seek (DISK, 0x17e, 0) or die "$disk: seek: $!\n";
327     syswrite (DISK, $args) or die "$disk: write: $!\n";
328     close (DISK) or die "$disk: close: $!\n";
329 }
330
331 sub run_command {
332     print join (' ', @_), "\n";
333     die "command failed\n" if system (@_);
334 }
335
336 sub search_path {
337     my ($target) = @_;
338     for my $dir (split (':', $ENV{PATH})) {
339         return $dir if -e "$dir/$target";
340     }
341     die "$target not in PATH\n";
342 }
343
344 sub bochs_disk_line {
345     my ($device, $file) = @_;
346     return "" if !defined $file;
347     my (%geom) = disk_geometry ($file);
348     return "$device: type=disk, path=$file, mode=flat, "
349         . "cylinders=$geom{C}, heads=$geom{H}, spt=$geom{S}, "
350         . "translation=none\n";
351 }
352
353 sub disk_geometry {
354     my ($file) = @_;
355     my ($size) = -s $file;
356     die "$file: stat: $!\n" if !defined $size;
357     die "$file: size not a multiple of 512 bytes\n" if $size % 512;
358     my ($cylinders) = int ($size / (512 * 16 * 63));
359     $cylinders++ if $size % (512 * 16 * 63);
360
361     return (CAPACITY => $size / 512,
362             C => $cylinders,
363             H => 16,
364             S => 63);
365 }