7 our (@disks) = ("os.dsk", "fs.dsk", "scratch.dsk", "swap.dsk");
11 our ($jitter, $realtime);
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") },
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") },
24 "run|get|put|make-disk" => \&cmd_option,
26 "m|memory=i" => \$mem,
27 "j|jitter=i" => sub { set_jitter (@_) },
28 "r|realtime" => sub { set_realtime () },
30 "v|no-vga" => sub { set_vga ('none'); },
31 "s|no-serial" => sub { $serial_out = 0; },
32 "t|terminal" => sub { set_vga ('terminal'); },
34 "h|help" => sub { usage (0); },
36 "0|os-disk|disk-0|hda=s" => \$disks[0],
37 "1|fs-disk|disk-1|hdb=s" => \$disks[1],
38 "2|scratch-disk|disk-2|hdc=s" => \$disks[2],
39 "3|swap-disk|disk-3|hdd=s" => \$disks[3])
42 $sim = "bochs" if !defined $sim;
43 $debug = "no-debug" if !defined $debug;
44 $vga = "window" if !defined $vga;
48 die "--$new_sim conflicts with --$sim\n"
49 if defined ($sim) && $sim ne $new_sim;
55 die "--$new_debug conflicts with --$debug\n"
56 if defined ($debug) && $debug ne $new_debug;
62 if (defined ($vga) && $vga ne $new_vga) {
63 print "warning: conflicting vga display options\n";
69 my ($new_jitter) = @_;
70 die "--realtime conflicts with --jitter\n" if defined $realtime;
71 die "different --jitter already defined\n"
72 if defined $jitter && $jitter != $new_jitter;
73 $jitter = $new_jitter;
77 die "--realtime conflicts with --jitter\n" if defined $jitter;
82 # Force an end to option processing, as with --.
86 die "no command specified; use --help for help\n"
88 my ($cmd) = shift @ARGV;
90 run_vm ('EXEC', @ARGV);
91 } elsif ($cmd eq 'make-disk') {
92 usage () if @ARGV != 2;
93 my ($file, $mb) = @ARGV;
94 usage () if $mb !~ /^\d+(\.\d+)?|\.\d+$/;
95 die "$file: already exists\n" if -e $file;
97 create_disk ($file, int ($mb * 1008));
98 } elsif ($cmd eq 'put') {
99 # Take a -f option to combine formatting with putting.
101 if (@ARGV > 0 && $ARGV[0] eq '-f') {
106 usage () if @ARGV != 1 && @ARGV != 2;
107 my ($hostfn, $guestfn) = @ARGV;
108 $guestfn = $hostfn if !defined $guestfn;
110 # Create scratch disk from file.
111 die "$hostfn: $!\n" if ! -e $hostfn;
114 copy_pad ($hostfn, "scratch.dsk", 512);
116 open (SCRATCH, ">scratch.dsk") or die "scratch.dsk: create: $!\n";
117 syswrite (SCRATCH, "\0" x 512);
122 my (@cmd) = ("-ci", $guestfn, $size, "-q");
123 unshift (@cmd, "-f") if $format;
124 run_vm ('EXEC', @cmd);
125 } elsif ($cmd eq 'get') {
126 usage () if @ARGV != 1 && @ARGV != 2;
127 my ($guestfn, $hostfn) = @ARGV;
128 $hostfn = $guestfn if !defined $hostfn;
129 die "$hostfn: already exists\n" if -e $hostfn;
131 # Create scratch disk big enough for any file in the filesystem
132 # (modulo sparse files).
133 die "$disks[1]: $!\n" if ! -e $disks[1];
134 my ($fs_size) = -s _;
135 my ($scratch_size) = -s $disks[2];
136 $scratch_size = 0 if !defined $scratch_size;
137 create_disk ($disks[2], $fs_size / 1024 + 16)
138 if $scratch_size < $fs_size + 16384;
141 run_vm ('FORK', "-co", $guestfn, "-q");
143 # Read out scratch disk.
144 print "copying $guestfn from $disks[2] to $hostfn...\n";
145 open (SRC, "<$disks[2]") or die "$disks[2]: open: $!\n";
146 open (DST, ">$hostfn") or die "$hostfn: create: $!\n";
148 read (SRC, $input, 512) == 512 or die "$disks[2]: read error\n";
149 my ($size) = unpack ("V", $input);
150 $size != 0xffffffff or die "$guestfn: too big for $disks[2]?";
152 read (SRC, $src, $size) == $size or die "$disks[2]: read error\n";
153 print DST $src or die "$hostfn: write error\n";
156 } elsif ($cmd eq 'help') {
159 die "unknown command `$cmd'; use --help for help\n";
165 $exitcode = 1 unless defined $exitcode;
166 print "pintos, a utility for invoking Pintos in a simulator\n";
167 print "Usage: pintos [OPTION...] COMMAND [ARG...]\n";
168 print "where COMMAND is one of the following:\n";
169 print " run [CMDLINE...] run a VM in the simulator\n";
170 print " make-disk FILE.DSK SIZE create FILE.DSK as empty SIZE MB disk\n";
171 print " put HOSTFN [GUESTFN] copy HOSTFN into VM (as GUESTFN)\n";
172 print " get GUESTFN [HOSTFN] copy GUESTFN out of VM (to HOSTFN)\n";
173 print " help print this help message and exit\n";
174 print "Simulator options:\n";
175 print " --bochs (default) Use Bochs as simulator\n";
176 print " --qemu Use qemu as simulator\n";
177 print " --gsx Use VMware GSX Server 3.x as simulator\n";
178 print "Debugger options:\n";
179 print " --no-debug (default) No debugger\n";
180 print " --monitor Debug with simulator's monitor\n";
181 print " --gdb Debug with gdb\n";
182 print "Display options: (default is VGA + serial)\n";
183 print " -v, --no-vga No VGA display\n";
184 print " -s, --no-serial No serial output\n";
185 print " -t, --terminal Display VGA in terminal (Bochs only)\n";
186 print "VM options:\n";
187 print " -j SEED Randomize timer interrupts (Bochs only)\n";
188 print " -r, --realtime Use realistic, but not reproducible, timings\n";
189 print " -m, --mem=MB Run VM with MB megabytes of physical memory\n";
190 print "Disk options:\n";
191 print " --os-disk=DISK Set OS disk file (default: os.dsk)\n";
192 print " --fs-disk=DISK Set FS disk file (default: fs.dsk)\n";
193 print " --scratch-disk=DISK Set scratch disk (default: scratch.dsk)\n";
194 print " --swap-disk=DISK Set swap disk file (default: swap.dsk)\n";
199 my ($src, $dst, $blocksize) = @_;
200 run_command ("dd", "if=$src", "of=$dst", "bs=$blocksize", "conv=sync");
204 my ($disk, $kb) = @_;
205 run_command ("dd", "if=/dev/zero", "of=$disk", "bs=1024", "count=$kb");
210 $fork eq 'FORK' || $fork eq 'EXEC' or die;
214 die "$disks[0]: can't find OS disk\n" if ! -e $disks[0];
216 undef $disks[$i] if ! -e $disks[$i];
219 write_cmd_line ($disks[0], @_);
221 if ($sim eq 'bochs') {
223 if ($debug eq 'no-debug') {
225 } elsif ($debug eq 'monitor') {
227 } elsif ($debug eq 'gdb') {
231 my ($bochsbin) = search_path ($bin);
232 my ($bochsshare) = "$bochsbin/../share/bochs";
234 open (BOCHSRC, ">bochsrc.txt") or die "bochsrc.txt: create: $!\n";
235 print BOCHSRC "romimage: file=$bochsshare/BIOS-bochs-latest, "
236 . "address=0xf0000\n";
237 print BOCHSRC "vgaromimage: $bochsshare/VGABIOS-lgpl-latest\n";
238 print BOCHSRC bochs_disk_line ("ata0-master", $disks[0]);
239 print BOCHSRC bochs_disk_line ("ata0-slave", $disks[1]);
240 print BOCHSRC "ata1: enabled=1, ioaddr1=0x170, ioaddr2=0x370, irq=15\n"
241 if defined ($disks[2]) || defined ($disks[3]);
242 print BOCHSRC bochs_disk_line ("ata1-master", $disks[2]);
243 print BOCHSRC bochs_disk_line ("ata1-slave", $disks[3]);
244 print BOCHSRC "boot: c\n";
245 print BOCHSRC "ips: 1000000\n";
247 print BOCHSRC "clock: sync=none, time0=0\n";
249 print BOCHSRC "clock: sync=realtime, time0=0\n";
251 print BOCHSRC "megs: $mem\n";
252 print BOCHSRC "log: bochsout.txt\n";
253 if ($vga ne 'terminal') {
254 print BOCHSRC "com1: enabled=1, dev=/dev/stdout\n"
256 print BOCHSRC "display_library: nogui\n"
259 print BOCHSRC "display_library: term\n";
263 my (@cmd) = ($bin, '-q');
264 push (@cmd, '-j', $jitter) if defined $jitter;
265 print join (' ', @cmd), "\n";
266 $fork eq 'EXEC' ? exec (@cmd) : system (@cmd);
267 } elsif ($sim eq 'qemu') {
268 print "warning: qemu doesn't support --terminal\n"
269 if $vga eq 'terminal';
270 print "warning: qemu doesn't support jitter\n"
272 my (@cmd) = ('qemu');
273 push (@cmd, '-hda', $disks[0]) if defined $disks[0];
274 push (@cmd, '-hdb', $disks[1]) if defined $disks[1];
275 push (@cmd, '-hdc', $disks[2]) if defined $disks[2];
276 push (@cmd, '-hdd', $disks[3]) if defined $disks[3];
277 push (@cmd, '-m', $mem);
278 push (@cmd, '-nographic') if $vga eq 'none';
279 push (@cmd, '-serial', 'stdio') if $serial_out && $vga ne 'none';
280 push (@cmd, '-S') if $debug eq 'monitor';
281 push (@cmd, '-s') if $debug eq 'gdb';
283 } elsif ($sim eq 'gsx') {
284 print "warning: VMware GSX Server doesn't support --$debug\n"
285 if $debug ne 'no-debug';
286 print "warning: VMware GSX Server doesn't support --no-vga\n"
288 print "warning: VMware GSX Server doesn't support --terminal\n"
289 if $vga eq 'terminal';
290 print "warning: VMware GSX Server doesn't support jitter\n"
293 open (VMX, ">pintos.vmx") or die "pintos.vmx: create: $!\n";
294 chmod 0777 & ~umask, "pintos.vmx";
295 print VMX "#! /usr/bin/vmware -G\n";
296 print VMX "config.version = 6\n";
297 print VMX "guestOS = \"linux\"\n";
298 print VMX "floppy0.present = FALSE\n";
300 if (! -e 'null.bin') {
301 open (NULL, ">null.bin") or die "null.bin: create: $!\n";
305 for (my ($i) = 0; $i < 4; $i++) {
306 my ($dsk) = $disks[$i];
307 next if !defined $dsk;
313 my ($device) = "ide" . int ($i / 2) . ":" . ($i % 2);
314 print VMX "\n$device.present = TRUE\n";
315 print VMX "$device.deviceType = \"plainDisk\"\n";
316 print VMX "$device.fileName = \"$pln\"\n";
318 my (%geom) = disk_geometry ($dsk);
319 open (PLN, ">$pln") or die "$pln: create: $!\n";
320 print PLN "DRIVETYPE ide\n";
321 print PLN "#vm|VERSION 2\n";
322 print PLN "#vm|TOOLSVERSION 2\n";
323 print PLN "CYLINDERS $geom{C}\n";
324 print PLN "HEADS $geom{H}\n";
325 print PLN "SECTORS $geom{S}\n";
326 print PLN "#vm|CAPACITY $geom{CAPACITY}\n";
327 print PLN "ACCESS \"$dsk\" 0 $geom{CAPACITY}\n";
333 my ($vmx) = getcwd () . "/pintos.vmx";
334 system ("vmware-cmd -s register $vmx >&/dev/null");
335 system ("vmware-cmd $vmx stop hard >&/dev/null");
336 system ("vmware -l -G -x -q $vmx");
337 system ("vmware-cmd $vmx stop hard >&/dev/null");
342 my ($disk, @args) = @_;
344 die "command line includes empty string" if grep (/^$/, @args);
345 my ($args) = join ("\0", @args) . "\0\0";
346 die "command line exceeds 128 bytes" if length ($args) > 128;
347 $args .= "\0" x (128 - length ($args));
349 print "writing command line to $disk...\n";
350 open (DISK, "+<$disk") or die "$disk: open: $!\n";
351 seek (DISK, 0x17e, 0) or die "$disk: seek: $!\n";
352 syswrite (DISK, $args) or die "$disk: write: $!\n";
353 close (DISK) or die "$disk: close: $!\n";
357 print join (' ', @_), "\n";
358 die "command failed\n" if system (@_);
363 for my $dir (split (':', $ENV{PATH})) {
364 return $dir if -e "$dir/$target";
366 die "$target not in PATH\n";
369 sub bochs_disk_line {
370 my ($device, $file) = @_;
371 return "" if !defined $file;
372 my (%geom) = disk_geometry ($file);
373 return "$device: type=disk, path=$file, mode=flat, "
374 . "cylinders=$geom{C}, heads=$geom{H}, spt=$geom{S}, "
375 . "translation=none\n";
380 my ($size) = -s $file;
381 die "$file: stat: $!\n" if !defined $size;
382 die "$file: size not a multiple of 512 bytes\n" if $size % 512;
383 my ($cylinders) = int ($size / (512 * 16 * 63));
384 $cylinders++ if $size % (512 * 16 * 63);
386 return (CAPACITY => $size / 512,