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;
113 copy_pad ($hostfn, "scratch.dsk", 512);
116 my (@cmd) = ("-ci", $guestfn, $size, "-q");
117 unshift (@cmd, "-f") if $format;
118 run_vm ('EXEC', @cmd);
119 } elsif ($cmd eq 'get') {
120 usage () if @ARGV != 1 && @ARGV != 2;
121 my ($guestfn, $hostfn) = @ARGV;
122 $hostfn = $guestfn if !defined $hostfn;
123 die "$hostfn: already exists\n" if -e $hostfn;
125 # Create scratch disk big enough for any file in the filesystem
126 # (modulo sparse files).
127 die "$disks[1]: $!\n" if ! -e $disks[1];
128 my ($fs_size) = -s _;
129 my ($scratch_size) = -s $disks[2];
130 $scratch_size = 0 if !defined $scratch_size;
131 create_disk ($disks[2], $fs_size / 1024 + 16)
132 if $scratch_size < $fs_size + 16384;
135 run_vm ('FORK', "-co", $guestfn, "-q");
137 # Read out scratch disk.
138 print "copying $guestfn from $disks[2] to $hostfn...\n";
139 open (SRC, "<$disks[2]") or die "$disks[2]: open: $!\n";
140 open (DST, ">$hostfn") or die "$hostfn: create: $!\n";
142 read (SRC, $input, 512) == 512 or die "$disks[2]: read error\n";
143 my ($size) = unpack ("V", $input);
144 $size != 0xffffffff or die "$guestfn: too big for $disks[2]?";
146 read (SRC, $src, $size) == $size or die "$disks[2]: read error\n";
147 print DST $src or die "$hostfn: write error\n";
150 } elsif ($cmd eq 'help') {
153 die "unknown command `$cmd'; use --help for help\n";
159 $exitcode = 1 unless defined $exitcode;
160 print "pintos, a utility for invoking Pintos in a simulator\n";
161 print "Usage: pintos [OPTION...] COMMAND [ARG...]\n";
162 print "where COMMAND is one of the following:\n";
163 print " run [CMDLINE...] run a VM in the simulator\n";
164 print " make-disk FILE.DSK SIZE create FILE.DSK as empty SIZE MB disk\n";
165 print " put HOSTFN [GUESTFN] copy HOSTFN into VM (as GUESTFN)\n";
166 print " get GUESTFN [HOSTFN] copy GUESTFN out of VM (to HOSTFN)\n";
167 print " help print this help message and exit\n";
168 print "Simulator options:\n";
169 print " --bochs (default) Use Bochs as simulator\n";
170 print " --qemu Use qemu as simulator\n";
171 print " --gsx Use VMware GSX Server 3.x as simulator\n";
172 print "Debugger options:\n";
173 print " --no-debug (default) No debugger\n";
174 print " --monitor Debug with simulator's monitor\n";
175 print " --gdb Debug with gdb\n";
176 print "Display options: (default is VGA + serial)\n";
177 print " -v, --no-vga No VGA display\n";
178 print " -s, --no-serial No serial output\n";
179 print " -t, --terminal Display VGA in terminal (Bochs only)\n";
180 print "VM options:\n";
181 print " -j SEED Randomize timer interrupts (Bochs only)\n";
182 print " -r, --realtime Use realistic, but not reproducible, timings\n";
183 print " -m, --mem=MB Run VM with MB megabytes of physical memory\n";
184 print "Disk options:\n";
185 print " --os-disk=DISK Set OS disk file (default: os.dsk)\n";
186 print " --fs-disk=DISK Set FS disk file (default: fs.dsk)\n";
187 print " --scratch-disk=DISK Set scratch disk (default: scratch.dsk)\n";
188 print " --swap-disk=DISK Set swap disk file (default: swap.dsk)\n";
193 my ($src, $dst, $blocksize) = @_;
194 run_command ("dd", "if=$src", "of=$dst", "bs=$blocksize", "conv=sync");
198 my ($disk, $kb) = @_;
199 run_command ("dd", "if=/dev/zero", "of=$disk", "bs=1024", "count=$kb");
204 $fork eq 'FORK' || $fork eq 'EXEC' or die;
208 die "$disks[0]: can't find OS disk\n" if ! -e $disks[0];
210 undef $disks[$i] if ! -e $disks[$i];
213 write_cmd_line ($disks[0], @_);
215 if ($sim eq 'bochs') {
217 if ($debug eq 'no-debug') {
219 } elsif ($debug eq 'monitor') {
221 } elsif ($debug eq 'gdb') {
225 my ($bochsbin) = search_path ($bin);
226 my ($bochsshare) = "$bochsbin/../share/bochs";
228 open (BOCHSRC, ">bochsrc.txt") or die "bochsrc.txt: create: $!\n";
229 print BOCHSRC "romimage: file=$bochsshare/BIOS-bochs-latest, "
230 . "address=0xf0000\n";
231 print BOCHSRC "vgaromimage: $bochsshare/VGABIOS-lgpl-latest\n";
232 print BOCHSRC bochs_disk_line ("ata0-master", $disks[0]);
233 print BOCHSRC bochs_disk_line ("ata0-slave", $disks[1]);
234 print BOCHSRC "ata1: enabled=1, ioaddr1=0x170, ioaddr2=0x370, irq=15\n"
235 if defined ($disks[2]) || defined ($disks[3]);
236 print BOCHSRC bochs_disk_line ("ata1-master", $disks[2]);
237 print BOCHSRC bochs_disk_line ("ata1-slave", $disks[3]);
238 print BOCHSRC "boot: c\n";
239 print BOCHSRC "ips: 1000000\n";
241 print BOCHSRC "clock: sync=none, time0=0\n";
243 print BOCHSRC "clock: sync=realtime, time0=0\n";
245 print BOCHSRC "megs: $mem\n";
246 print BOCHSRC "log: bochsout.txt\n";
247 if ($vga ne 'terminal') {
248 print BOCHSRC "com1: enabled=1, dev=/dev/stdout\n"
250 print BOCHSRC "display_library: nogui\n"
253 print BOCHSRC "display_library: term\n";
257 my (@cmd) = ($bin, '-q');
258 push (@cmd, '-j', $jitter) if defined $jitter;
259 print join (' ', @cmd), "\n";
260 $fork eq 'EXEC' ? exec (@cmd) : system (@cmd);
261 } elsif ($sim eq 'qemu') {
262 print "warning: qemu doesn't support --terminal\n"
263 if $vga eq 'terminal';
264 print "warning: qemu doesn't support jitter\n"
266 my (@cmd) = ('qemu');
267 push (@cmd, '-hda', $disks[0]) if defined $disks[0];
268 push (@cmd, '-hdb', $disks[1]) if defined $disks[1];
269 push (@cmd, '-hdc', $disks[2]) if defined $disks[2];
270 push (@cmd, '-hdd', $disks[3]) if defined $disks[3];
271 push (@cmd, '-m', $mem);
272 push (@cmd, '-nographic') if $vga eq 'none';
273 push (@cmd, '-serial', 'stdio') if $serial_out && $vga ne 'none';
274 push (@cmd, '-S') if $debug eq 'monitor';
275 push (@cmd, '-s') if $debug eq 'gdb';
277 } elsif ($sim eq 'gsx') {
278 print "warning: VMware GSX Server doesn't support --$debug\n"
279 if $debug ne 'no-debug';
280 print "warning: VMware GSX Server doesn't support --no-vga\n"
282 print "warning: VMware GSX Server doesn't support --terminal\n"
283 if $vga eq 'terminal';
284 print "warning: VMware GSX Server doesn't support jitter\n"
287 open (VMX, ">pintos.vmx") or die "pintos.vmx: create: $!\n";
288 chmod 0777 & ~umask, "pintos.vmx";
289 print VMX "#! /usr/bin/vmware -G\n";
290 print VMX "config.version = 6\n";
291 print VMX "guestOS = \"linux\"\n";
292 print VMX "floppy0.present = FALSE\n";
294 if (! -e 'null.bin') {
295 open (NULL, ">null.bin") or die "null.bin: create: $!\n";
299 for (my ($i) = 0; $i < 4; $i++) {
300 my ($dsk) = $disks[$i];
301 next if !defined $dsk;
307 my ($device) = "ide" . int ($i / 2) . ":" . ($i % 2);
308 print VMX "\n$device.present = TRUE\n";
309 print VMX "$device.deviceType = \"plainDisk\"\n";
310 print VMX "$device.fileName = \"$pln\"\n";
312 my (%geom) = disk_geometry ($dsk);
313 open (PLN, ">$pln") or die "$pln: create: $!\n";
314 print PLN "DRIVETYPE ide\n";
315 print PLN "#vm|VERSION 2\n";
316 print PLN "#vm|TOOLSVERSION 2\n";
317 print PLN "CYLINDERS $geom{C}\n";
318 print PLN "HEADS $geom{H}\n";
319 print PLN "SECTORS $geom{S}\n";
320 print PLN "#vm|CAPACITY $geom{CAPACITY}\n";
321 print PLN "ACCESS \"$dsk\" 0 $geom{CAPACITY}\n";
327 my ($vmx) = getcwd () . "/pintos.vmx";
328 system ("vmware-cmd -s register $vmx >&/dev/null");
329 system ("vmware-cmd $vmx stop hard >&/dev/null");
330 system ("vmware -l -G -x -q $vmx");
331 system ("vmware-cmd $vmx stop hard >&/dev/null");
336 my ($disk, @args) = @_;
338 die "command line includes empty string" if grep (/^$/, @args);
339 my ($args) = join ("\0", @args) . "\0\0";
340 die "command line exceeds 128 bytes" if length ($args) > 128;
341 $args .= "\0" x (128 - length ($args));
343 print "writing command line to $disk...\n";
344 open (DISK, "+<$disk") or die "$disk: open: $!\n";
345 seek (DISK, 0x17e, 0) or die "$disk: seek: $!\n";
346 syswrite (DISK, $args) or die "$disk: write: $!\n";
347 close (DISK) or die "$disk: close: $!\n";
351 print join (' ', @_), "\n";
352 die "command failed\n" if system (@_);
357 for my $dir (split (':', $ENV{PATH})) {
358 return $dir if -e "$dir/$target";
360 die "$target not in PATH\n";
363 sub bochs_disk_line {
364 my ($device, $file) = @_;
365 return "" if !defined $file;
366 my (%geom) = disk_geometry ($file);
367 return "$device: type=disk, path=$file, mode=flat, "
368 . "cylinders=$geom{C}, heads=$geom{H}, spt=$geom{S}, "
369 . "translation=none\n";
374 my ($size) = -s $file;
375 die "$file: stat: $!\n" if !defined $size;
376 die "$file: size not a multiple of 512 bytes\n" if $size % 512;
377 my ($cylinders) = int ($size / (512 * 16 * 63));
378 $cylinders++ if $size % (512 * 16 * 63);
380 return (CAPACITY => $size / 512,