7 our (@disks) = ("os.dsk", "fs.dsk", "scratch.dsk", "swap.dsk");
13 use Getopt::Long qw(:config require_order bundling);
14 GetOptions ("bochs|qemu|gsx" => \&set_sim,
15 "no-debug|monitor|gdb" => \&set_debug,
16 "run|get|put|make-disk" => \&cmd_option,
17 "m|memory=i" => \$mem,
18 "j|jitter=i" => \$jitter,
19 "v|no-vga" => sub { set_vga ('none'); },
20 "s|no-serial" => sub { $serial_out = 0; },
21 "t|terminal" => sub { set_vga ('terminal'); },
22 "h|help" => sub { usage (0); },
23 "0|os-disk|disk-0|hda=s" => \$disks[0],
24 "1|fs-disk|disk-1|hdb=s" => \$disks[1],
25 "2|scratch-disk|disk-2|hdc=s" => \$disks[2],
26 "3|swap-disk|disk-3|hdd=s" => \$disks[3])
29 $sim = "bochs" if !defined $sim;
30 $debug = "no-debug" if !defined $debug;
31 $vga = "window" if !defined $vga;
35 die "--$option conflicts with --$sim\n" if defined $sim;
41 die "--$option conflicts with --$debug\n" if defined $debug;
42 our ($debug) = $option;
47 if (defined ($vga) && $vga ne $new_vga) {
48 print "warning: conflicting vga display options\n";
50 our ($vga) = $new_vga;
56 # Force an end to option processing, as with --.
60 die "no command specified; use --help for help\n"
62 my ($cmd) = shift @ARGV;
65 } elsif ($cmd eq 'make-disk') {
66 usage () if @ARGV != 2;
67 my ($file, $mb) = @ARGV;
68 usage () if $mb !~ /^\d+(\.\d+)?|\.\d+$/;
69 die "$file: already exists\n" if -e $file;
71 create_disk ($file, int ($mb * 1008));
72 } elsif ($cmd eq 'put') {
73 usage () if @ARGV != 1 && @ARGV != 2;
74 my ($hostfn, $guestfn) = @ARGV;
75 $guestfn = $hostfn if !defined $guestfn;
77 # Create scratch disk from file.
78 die "$hostfn: $!\n" if ! -e $hostfn;
80 copy_pad ($hostfn, "scratch.dsk", 512);
83 run_vm ("-ci", $guestfn, $size, "-q");
84 } elsif ($cmd eq 'get') {
85 usage () if @ARGV != 1 && @ARGV != 2;
86 my ($guestfn, $hostfn) = @ARGV;
87 $hostfn = $guestfn if !defined $hostfn;
88 die "$hostfn: already exists\n" if -e $hostfn;
90 # Create scratch disk big enough for any file in the filesystem
91 # (modulo sparse files).
92 die "fs.dsk: $!\n" if ! -e "fs.dsk";
94 my ($scratch_size) = -s "scratch.dsk";
95 $scratch_size = 0 if !defined $scratch_size;
96 create_disk ("scratch.dsk", $fs_size / 1024 + 16)
97 if $scratch_size < $fs_size + 16384;
100 run_vm ("-co", $guestfn, "-q");
102 # Read out scratch disk.
103 print "copying $guestfn from scratch.dsk to $hostfn...\n";
104 open (SRC, "<scratch.dsk") or die "scratch.dsk: open: $!\n";
105 open (DST, ">$hostfn") or die "$hostfn: create: $!\n";
107 read (SRC, $input, 512) == 512 or die "scratch.dsk: read error\n";
108 my ($size) = unpack ("%V", $input);
109 $size != 0xffffffff or die "$guestfn: too big for scratch.dsk?";
111 read (SRC, $src, $size) == $size or die "scratch.dsk: read error\n";
112 print DST $src or die "$hostfn: write error\n";
115 } elsif ($cmd eq 'help') {
118 die "unknown command `$cmd'; use --help for help\n";
124 $exitcode = 1 unless defined $exitcode;
125 print "pintos, a utility for invoking Pintos in a simulator\n";
126 print "Usage: pintos [OPTION...] COMMAND [ARG...]\n";
127 print "where COMMAND is one of the following:\n";
128 print " run [CMDLINE...] run a VM in the simulator\n";
129 print " make-disk FILE.DSK SIZE create FILE.DSK as empty SIZE MB disk\n";
130 print " put HOSTFN [GUESTFN] copy HOSTFN into VM (as GUESTFN)\n";
131 print " get GUESTFN [HOSTFN] copy GUESTFN out of VM (to HOSTFN)\n";
132 print " help print this help message and exit\n";
133 print "Simulator options:\n";
134 print " --bochs (default) Use Bochs as simulator\n";
135 print " --qemu Use qemu as simulator\n";
136 print " --gsx Use VMware GSX Server 3.x as simulator\n";
137 print "Debugger options:\n";
138 print " --no-debug (default) No debugger\n";
139 print " --monitor Debug with simulator's monitor\n";
140 print " --gdb Debug with gdb\n";
141 print "Display options: (default is VGA + serial)\n";
142 print " -v, --no-vga No VGA display\n";
143 print " -s, --no-serial No serial output\n";
144 print " -t, --terminal Display VGA in terminal (Bochs only)\n";
145 print "VM options:\n";
146 print " -j SEED Randomize timer interrupts (Bochs only)\n";
147 print " -m, --mem=MB Run VM with MB megabytes of physical memory\n";
148 print "Disk options:\n";
149 print " --os-disk=DISK Set OS disk file (default: os.dsk)\n";
150 print " --fs-disk=DISK Set FS disk file (default: fs.dsk)\n";
151 print " --scratch-disk=DISK Set scratch disk (default: scratch.dsk)\n";
152 print " --swap-disk=DISK Set swap disk file (default: swap.dsk)\n";
157 my ($src, $dst, $blocksize) = @_;
158 run_command ("dd", "if=$src", "of=$dst", "bs=$blocksize", "conv=sync");
162 my ($disk, $kb) = @_;
163 run_command ("dd", "if=/dev/zero", "of=$disk", "bs=1024", "count=$kb");
169 die "$disks[0]: can't find OS disk\n" if ! -e $disks[0];
171 undef $disks[$i] if ! -e $disks[$i];
174 write_cmd_line ($disks[0], @_);
176 if ($sim eq 'bochs') {
178 if ($debug eq 'no-debug') {
180 } elsif ($debug eq 'monitor') {
182 } elsif ($debug eq 'gdb') {
186 my ($bochsbin) = search_path ($bin);
187 my ($bochsshare) = "$bochsbin/../share/bochs";
189 open (BOCHSRC, ">bochsrc.txt") or die "bochsrc.txt: create: $!\n";
190 print BOCHSRC "romimage: file=$bochsshare/BIOS-bochs-latest, "
191 . "address=0xf0000\n";
192 print BOCHSRC "vgaromimage: $bochsshare/VGABIOS-lgpl-latest\n";
193 print BOCHSRC bochs_disk_line ("ata0-master", $disks[0]);
194 print BOCHSRC bochs_disk_line ("ata0-slave", $disks[1]);
195 print BOCHSRC "ata1: enabled=1, ioaddr1=0x170, ioaddr2=0x370, irq=15\n"
196 if defined ($disks[2]) || defined ($disks[3]);
197 print BOCHSRC bochs_disk_line ("ata1-master", $disks[2]);
198 print BOCHSRC bochs_disk_line ("ata1-slave", $disks[3]);
199 print BOCHSRC "boot: c\n";
200 print BOCHSRC "ips: 1000000\n";
201 print BOCHSRC "clock: sync=none, time0=0\n";
202 print BOCHSRC "megs: $mem\n";
203 print BOCHSRC "log: bochsout.txt\n";
204 if ($vga ne 'terminal') {
205 print BOCHSRC "com1: enabled=1, dev=/dev/stdout\n"
207 print BOCHSRC "display_library: nogui\n"
210 print BOCHSRC "display_library: term\n";
214 my (@cmd) = ($bin, '-q');
215 push (@cmd, '-j', $jitter) if defined $jitter;
216 run_command_no_die (@cmd);
217 } elsif ($sim eq 'qemu') {
218 print "warning: qemu doesn't support --terminal\n"
219 if $vga eq 'terminal';
220 print "warning: qemu doesn't support jitter\n"
222 my (@cmd) = ('qemu');
223 push (@cmd, '-hda', $disks[0]) if defined $disks[0];
224 push (@cmd, '-hdb', $disks[1]) if defined $disks[1];
225 push (@cmd, '-hdc', $disks[2]) if defined $disks[2];
226 push (@cmd, '-hdd', $disks[3]) if defined $disks[3];
227 push (@cmd, '-m', $mem);
228 push (@cmd, '-nographic') if $vga eq 'none';
229 push (@cmd, '-serial', 'stdio') if $serial_out && $vga ne 'none';
230 push (@cmd, '-S') if $debug eq 'monitor';
231 push (@cmd, '-s') if $debug eq 'gdb';
233 } elsif ($sim eq 'gsx') {
234 print "warning: VMware GSX Server doesn't support --$debug\n"
235 if $debug ne 'no-debug';
236 print "warning: VMware GSX Server doesn't support --no-vga\n"
238 print "warning: VMware GSX Server doesn't support --terminal\n"
239 if $vga eq 'terminal';
240 print "warning: VMware GSX Server doesn't support jitter\n"
243 open (VMX, ">pintos.vmx") or die "pintos.vmx: create: $!\n";
244 chmod 0777 & ~umask, "pintos.vmx";
245 print VMX "#! /usr/bin/vmware -G\n";
246 print VMX "config.version = 6\n";
247 print VMX "guestOS = \"linux\"\n";
248 print VMX "floppy0.present = FALSE\n";
250 if (! -e 'null.bin') {
251 open (NULL, ">null.bin") or die "null.bin: create: $!\n";
255 for (my ($i) = 0; $i < 4; $i++) {
256 my ($dsk) = $disks[$i];
257 next if !defined $dsk;
263 my ($device) = "ide" . int ($i / 2) . ":" . ($i % 2);
264 print VMX "\n$device.present = TRUE\n";
265 print VMX "$device.deviceType = \"plainDisk\"\n";
266 print VMX "$device.fileName = \"$pln\"\n";
268 my (%geom) = disk_geometry ($dsk);
269 open (PLN, ">$pln") or die "$pln: create: $!\n";
270 print PLN "DRIVETYPE ide\n";
271 print PLN "#vm|VERSION 2\n";
272 print PLN "#vm|TOOLSVERSION 2\n";
273 print PLN "CYLINDERS $geom{C}\n";
274 print PLN "HEADS $geom{H}\n";
275 print PLN "SECTORS $geom{S}\n";
276 print PLN "#vm|CAPACITY $geom{CAPACITY}\n";
277 print PLN "ACCESS \"$dsk\" 0 $geom{CAPACITY}\n";
283 my ($vmx) = getcwd () . "/pintos.vmx";
284 system ("vmware-cmd -s register $vmx >&/dev/null");
285 system ("vmware-cmd $vmx stop hard >&/dev/null");
286 system ("vmware -l -G -x -q $vmx");
287 system ("vmware-cmd $vmx stop hard >&/dev/null");
292 my ($disk, @args) = @_;
294 die "command line includes empty string" if grep (/^$/, @args);
295 my ($args) = join ("\0", @args) . "\0\0";
296 die "command line exceeds 128 bytes" if length ($args) > 128;
297 $args .= "\0" x (128 - length ($args));
299 print "writing command line to $disk...\n";
300 open (DISK, "+<$disk") or die "$disk: open: $!\n";
301 seek (DISK, 0x17e, 0) or die "$disk: seek: $!\n";
302 syswrite (DISK, $args) or die "$disk: write: $!\n";
303 close (DISK) or die "$disk: close: $!\n";
307 print join (' ', @_), "\n";
308 die "command failed\n" if system (@_);
311 sub run_command_no_die {
312 print join (' ', @_), "\n";
318 for my $dir (split (':', $ENV{PATH})) {
319 return $dir if -e "$dir/$target";
321 die "$target not in PATH\n";
324 sub bochs_disk_line {
325 my ($device, $file) = @_;
326 return "" if !defined $file;
327 my (%geom) = disk_geometry ($file);
328 return "$device: type=disk, path=$file, mode=flat, "
329 . "cylinders=$geom{C}, heads=$geom{H}, spt=$geom{S}, "
330 . "translation=none\n";
335 my ($size) = -s $file;
336 die "$file: stat: $!\n" if !defined $size;
337 die "$file: size not a multiple of 512 bytes\n" if $size % 512;
338 my ($cylinders) = int ($size / (512 * 16 * 63));
339 $cylinders++ if $size % (512 * 16 * 63);
341 return (CAPACITY => $size / 512,