8 my ($arg) = shift (@ARGV);
9 if ($arg =~ /--(bochs|qemu|gsx)$/) {
11 } elsif ($arg =~ /--(monitor|gdb)$/) {
13 } elsif ($arg eq 'run') {
16 } elsif ($arg =~ /^mem(?:ory)?=(\d+)/) {
18 } elsif ($arg eq '--headless') {
20 } elsif ($arg eq 'make-disk') {
21 my ($force) = @ARGV > 0 && $ARGV[0] eq '--force';
22 shift @ARGV if $force;
23 usage () if @ARGV != 2;
24 my ($file, $mb) = @ARGV;
25 usage () if $mb !~ /^\d+$/;
26 die "$file: already exists\n" if -e $file;
28 if ($mb != .5 && $mb != 1 && $mb != 1.5 && $mb != 2) {
29 print "$file: recommended sizes are .5, 1, 1.5, or 2 MB\n";
30 die "use --force to override\n" if !$force;
33 create_disk ($file, $mb * 1008);
35 } elsif ($arg eq 'put') {
36 usage () if @ARGV != 1 && @ARGV != 2;
37 my ($hostfn, $guestfn) = @ARGV;
38 $guestfn = $hostfn if !defined $guestfn;
40 # Create scratch disk from file.
41 die "$hostfn: $!\n" if ! -e $hostfn;
43 copy_pad ($hostfn, "scratch.dsk", 512);
46 run_vm ("-ci", $hostfn, $size, "-q");
48 } elsif ($arg eq 'get') {
49 usage () if @ARGV != 1 && @ARGV != 2;
50 my ($guestfn, $hostfn) = @ARGV;
51 $hostfn = $guestfn if !defined $hostfn;
52 die "$hostfn: already exists\n" if -e $file;
54 # Create scratch disk big enough for any file in the filesystem
55 # (modulo sparse files).
56 die "fs.dsk: $!\n" if ! -e "fs.dsk";
58 my ($scratch_size) = -s "scratch.dsk";
59 $scratch_size = 0 if !defined $scratch_size;
60 create_disk ("scratch.dsk", $fs_size / 1024 + 16)
61 if $scratch_size < $fs_size + 16384;
64 run_vm ("-co", $guestfn, "-q");
66 # Read out scratch disk.
67 print "copying $guestfn from scratch.dsk to $hostfn...\n";
68 open (SRC, "<scratch.dsk") or die "scratch.dsk: open: $!\n";
69 open (DST, ">$hostfn") or die "$hostfn: create: $!\n";
71 read (SRC, $input, 512) == 512 or die "scratch.dsk: read error\n";
72 my ($size) = unpack ("%V", $input);
73 $size != 0xffffffff or die "$guestfn: too big for scratch.dsk?";
74 read (SRC, $src, $size) == $size or die "scratch.dsk: read error\n";
75 print DST $src or die "$hostfn: write error\n";
81 die "unknown option `$arg'\n";
88 $exitcode = 1 unless defined $exitcode;
89 print "pintos, a utility for invoking Pintos in a simulator\n";
90 print "Usage: pintos [OPTION...] COMMAND [ARG...]\n";
91 print "where COMMAND is one of the following:\n";
92 print " run [CMDLINE...] run a VM in the simulator\n";
93 print " make-disk FILE.DSK SIZE create FILE.DSK as empty SIZE MB disk\n";
94 print " put HOSTFN [GUESTFN] copy HOSTFN into VM (as GUESTFN)\n";
95 print " get GUESTFN [HOSTFN] copy GUESTFN out of VM (to HOSTFN)\n";
96 print " help print this help message and exit\n";
97 print "Available options:\n";
98 print " --bochs Use Bochs as simulator (default)\n";
99 print " --qemu Use qemu as simulator\n";
100 print " --gsx Use VMware GSX Server 3.x as simulator\n";
101 print " --monitor Debug with simulator's monitor\n";
102 print " --gdb Debug with gdb\n";
103 print " --mem=MB Run VM with MB megabytes of physical memory\n";
104 print " --headless No VGA display. Send VM output to stdout\n";
109 my ($src, $dst, $blocksize) = @_;
110 run_command ("dd", "if=$src", "of=$dst", "bs=$blocksize", "conv=sync");
114 my ($disk, $kb) = @_;
115 run_command ("dd", "if=/dev/zero", "of=$disk", "bs=1024", "count=$kb");
119 my (@disks) = (undef, undef, undef, undef);
121 $disks[0] = "os.dsk";
122 $disks[1] = "fs.dsk" if -e "fs.dsk";
123 $disks[2] = "scratch.dsk" if -e "scratch.dsk";
124 $disks[3] = "swap.dsk" if -e "swap.dsk";
126 die "$disks[0]: can't find OS disk\n" if ! -e $disks[0];
127 write_cmd_line ($disks[0], @_);
129 if ($sim eq 'bochs') {
132 } elsif ($debug eq 'monitor') {
134 } elsif ($debug eq 'gdb') {
137 $bochsbin = search_path ($bin);
138 $bochsshare = "$bochsbin/../share/bochs";
139 $romimage = "$bochsshare/BIOS-bochs-latest";
140 $vgaromimage = "$bochsshare/VGABIOS-lgpl-latest";
142 open (BOCHSRC, ">bochsrc.txt") or die "bochsrc.txt: create: $!\n";
143 print BOCHSRC "romimage: file=$romimage, address=0xf0000\n";
144 print BOCHSRC "vgaromimage: $vgaromimage\n";
145 print BOCHSRC bochs_disk_line ("ata0-master", $disks[0]);
146 print BOCHSRC bochs_disk_line ("ata0-slave", $disks[1]);
147 print BOCHSRC bochs_disk_line ("ata1-master", $disks[2]);
148 print BOCHSRC bochs_disk_line ("ata1-slave", $disks[3]);
149 print BOCHSRC "boot: c\n";
150 print BOCHSRC "ips: 1000000\n";
151 print BOCHSRC "clock: sync=none, time0=0\n";
152 print BOCHSRC "megs: $mem\n";
153 print BOCHSRC "com1: dev=/dev/tty\n" if $headless;
155 run_command ($bin, '-q');
156 } elsif ($sim eq 'qemu') {
157 my (@cmd) = ('qemu');
158 push (@cmd, '-hda', $disks[0]) if defined $disks[0];
159 push (@cmd, '-hdb', $disks[1]) if defined $disks[1];
160 push (@cmd, '-hdc', $disks[2]) if defined $disks[2];
161 push (@cmd, '-hdd', $disks[3]) if defined $disks[3];
162 push (@cmd, '-m', $mem);
163 push (@cmd, '-nographic') if $headless;
164 push (@cmd, '-S') if $debug eq 'monitor';
165 push (@cmd, '-s') if $debug eq 'gdb';
167 } elsif ($sim eq 'gsx') {
168 print "warning: --$debug not supported by VMware GSX Server, ignoring"
171 open (VMX, ">pintos.vmx") or die "pintos.vmx: create: $!\n";
172 chmod 0777 & ~umask, "pintos.vmx";
173 print VMX "#! /usr/bin/vmware -G\n";
174 print VMX "config.version = 6\n";
175 print VMX "guestOS = \"linux\"\n";
176 print VMX "floppy0.present = FALSE\n";
178 if (! -e 'null.bin') {
179 open (NULL, ">null.bin") or die "null.bin: create: $!\n";
183 for (my ($i) = 0; $i < 4; $i++) {
184 my ($dsk) = $disks[$i];
185 next if !defined $dsk;
186 $device = "ide" . int ($i / 2) . ":" . ($i % 2);
192 print VMX "\n$device.present = TRUE\n";
193 print VMX "$device.deviceType = \"plainDisk\"\n";
194 print VMX "$device.fileName = \"$pln\"\n";
196 my (%geom) = disk_geometry ($dsk);
197 open (PLN, ">$pln") or die "$pln: create: $!\n";
198 print PLN "DRIVETYPE ide\n";
199 print PLN "#vm|VERSION 2\n";
200 print PLN "#vm|TOOLSVERSION 2\n";
201 print PLN "CYLINDERS $geom{C}\n";
202 print PLN "HEADS $geom{H}\n";
203 print PLN "SECTORS $geom{S}\n";
204 print PLN "#vm|CAPACITY $geom{CAPACITY}\n";
205 print PLN "ACCESS \"$dsk\" 0 $geom{CAPACITY}\n";
211 $vmx = getcwd () . "/pintos.vmx";
212 system ("vmware-cmd -s register $vmx >&/dev/null");
213 system ("vmware-cmd $vmx stop hard >&/dev/null");
214 system ("vmware -l -G -x -q $vmx");
215 system ("vmware-cmd $vmx stop hard >&/dev/null");
220 my ($disk, @args) = @_;
222 die "command line includes empty string" if grep (/^$/, @args);
223 $args = join ("\0", @args) . "\0\0";
224 die "command line exceeds 128 bytes" if length ($args) > 128;
225 $args .= "\0" x (128 - length ($args));
227 print "writing command line to $disk...\n";
228 open (DISK, "+<$disk") or die "$disk: open: $!\n";
229 seek (DISK, 0x17e, 0) or die "$disk: seek: $!\n";
230 syswrite (DISK, $args) or die "$disk: write: $!\n";
231 close (DISK) or die "$disk: close: $!\n";
235 print join (' ', @_), "\n";
236 die "command failed\n" if system (@_);
241 for $dir (split (':', $ENV{PATH})) {
242 return $dir if -e "$dir/$target";
244 die "$target not in PATH\n";
247 sub bochs_disk_line {
248 my ($device, $file) = @_;
249 return "" if !defined $file;
250 my (%geom) = disk_geometry ($file);
251 return "$device: type=disk, path=$file, mode=flat, "
252 . " cylinders=$geom{C}, heads=$geom{H}, spt=$geom{S}, "
253 . "translation=none\n";
258 my ($size) = -s $file;
259 die "$file: stat: $!\n" if !defined $size;
260 die "$file: size not a multiple of 512 bytes\n" if $size % 512;
261 $cylinders = int ($size / (512 * 16 * 63));
262 $cylinders++ if $size % (512 * 16 * 63);
264 return (CAPACITY => $size / 512,