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