8 my ($arg) = shift (@ARGV);
9 if ($arg =~ /--(bochs|qemu|vmware)$/) {
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";
86 $exitcode = 1 unless defined $exitcode;
87 print "pintos, a utility for invoking Pintos in a simulator\n";
88 print "Usage: pintos [OPTION...] COMMAND [ARG...]\n";
89 print "where COMMAND is one of the following:\n";
90 print " run [CMDLINE...] run a VM in the simulator\n";
91 print " make-disk FILE.DSK SIZE create FILE.DSK as empty SIZE MB disk\n";
92 print " put HOSTFN [GUESTFN] copy HOSTFN into VM (as GUESTFN)\n";
93 print " get GUESTFN [HOSTFN] copy GUESTFN out of VM (to HOSTFN)\n";
94 print " help print this help message and exit\n";
95 print "Available options:\n";
96 print " --bochs Use Bochs as simulator (default)\n";
97 print " --qemu Use qemu as simulator\n";
98 print " --vmware Use VMware Workstation as simulator\n";
99 print " --monitor Debug with simulator's monitor\n";
100 print " --gdb Debug with gdb\n";
101 print " --mem=MB Run VM with MB megabytes of physical memory\n";
102 print " --headless No VGA display. Send VM output to stdout\n";
107 my ($src, $dst, $blocksize) = @_;
108 run_command ("dd", "if=$src", "of=$dst", "bs=$blocksize", "conv=sync");
112 my ($disk, $kb) = @_;
113 run_command ("dd", "if=/dev/zero", "of=$disk", "bs=1024", "count=$kb");
117 my (@disks) = (undef, undef, undef, undef);
119 $disks[0] = "os.dsk";
120 $disks[1] = "fs.dsk" if -e "fs.dsk";
121 $disks[2] = "scratch.dsk" if -e "scratch.dsk";
122 $disks[3] = "swap.dsk" if -e "swap.dsk";
124 die "$disks[0]: can't find OS disk\n" if ! -e $disks[0];
125 write_cmd_line ($disks[0], @_);
127 if ($sim eq 'bochs') {
130 } elsif ($debug eq 'monitor') {
132 } elsif ($debug eq 'gdb') {
135 $bochsbin = search_path ($bin);
136 $bochsshare = "$bochsbin/../share/bochs";
137 $romimage = "$bochsshare/BIOS-bochs-latest";
138 $vgaromimage = "$bochsshare/VGABIOS-lgpl-latest";
140 open (BOCHSRC, ">bochsrc.txt") or die "bochsrc.txt: create: $!\n";
141 print BOCHSRC "romimage: file=$romimage, address=0xf0000\n";
142 print BOCHSRC "vgaromimage: $vgaromimage\n";
143 print BOCHSRC bochs_disk_line ("ata0-master", $disks[0]);
144 print BOCHSRC bochs_disk_line ("ata0-slave", $disks[1]);
145 print BOCHSRC bochs_disk_line ("ata1-master", $disks[2]);
146 print BOCHSRC bochs_disk_line ("ata1-slave", $disks[3]);
147 print BOCHSRC "boot: c\n";
148 print BOCHSRC "ips: 1000000\n";
149 print BOCHSRC "clock: sync=none, time0=0\n";
150 print BOCHSRC "megs: $mem\n";
151 print BOCHSRC "com1: dev=/dev/tty\n" if $headless;
153 run_command ($bin, '-q');
154 } elsif ($sim eq 'qemu') {
155 my (@cmd) = ('qemu');
156 push (@cmd, '-hda', $disks[0]) if defined $disks[0];
157 push (@cmd, '-hdb', $disks[1]) if defined $disks[1];
158 push (@cmd, '-hdc', $disks[2]) if defined $disks[2];
159 push (@cmd, '-hdd', $disks[3]) if defined $disks[3];
160 push (@cmd, '-m', $mem);
161 push (@cmd, '-nographic') if $headless;
162 push (@cmd, '-S') if $debug eq 'monitor';
163 push (@cmd, '-s') if $debug eq 'gdb';
165 } elsif ($sim eq 'vmware') {
166 print "warning: --$debug not supported by VMware Workstation, ignoring"
169 open (VMX, ">pintos.vmx") or die "pintos.vmx: create: $!\n";
170 chmod 0777 & ~umask, "pintos.vmx";
171 print VMX "#! /usr/bin/vmware -G\n";
172 print VMX "config.version = 6\n";
173 print VMX "gui.powerOnAtStartUp = TRUE\n";
174 print VMX "gui.exitAtPowerOff = TRUE\n";
175 print VMX "guestOS = \"linux\"\n";
176 print VMX "floppy0.fileName = \"null.bin\"\n";
177 print VMX "floppy0.fileType = \"file\"\n";
179 if (! -e 'null.bin') {
180 open (NULL, ">null.bin") or die "null.bin: create: $!\n";
184 for (my ($i) = 0; $i < 4; $i++) {
185 my ($dsk) = $disks[$i];
186 next if !defined $dsk;
187 $device = "ide" . int ($i / 2) . ":" . ($i % 2);
193 print VMX "\n$device.present = TRUE\n";
194 print VMX "$device.deviceType = \"plainDisk\"\n";
195 print VMX "$device.fileName = \"$pln\"\n";
197 my (%geom) = disk_geometry ($dsk);
198 open (PLN, ">$pln") or die "$pln: create: $!\n";
199 print PLN "DRIVETYPE ide\n";
200 print PLN "#vm|VERSION 2\n";
201 print PLN "#vm|TOOLSVERSION 2\n";
202 print PLN "CYLINDERS $geom{C}\n";
203 print PLN "HEADS $geom{H}\n";
204 print PLN "SECTORS $geom{S}\n";
205 print PLN "#vm|CAPACITY $geom{CAPACITY}\n";
206 print PLN "ACCESS \"$dsk\" 0 $geom{CAPACITY}\n";
211 run_command ("./pintos.vmx");
216 my ($disk, @args) = @_;
218 die "command line includes empty string" if grep (/^$/, @args);
219 $args = join ("\0", @args) . "\0\0";
220 die "command line exceeds 128 bytes" if length ($args) > 128;
221 $args .= "\0" x (128 - length ($args));
223 print "writing command line to $disk...\n";
224 open (DISK, "+<$disk") or die "$disk: open: $!\n";
225 seek (DISK, 0x17e, 0) or die "$disk: seek: $!\n";
226 syswrite (DISK, $args) or die "$disk: write: $!\n";
227 close (DISK) or die "$disk: close: $!\n";
231 print join (' ', @_), "\n";
232 die "command failed\n" if system (@_);
237 for $dir (split (':', $ENV{PATH})) {
238 return $dir if -e "$dir/$target";
240 die "$target not in PATH\n";
243 sub bochs_disk_line {
244 my ($device, $file) = @_;
245 return "" if !defined $file;
246 my (%geom) = disk_geometry ($file);
247 return "$device: type=disk, path=$file, mode=flat, "
248 . " cylinders=$geom{C}, heads=$geom{H}, spt=$geom{S}, "
249 . "translation=none\n";
254 my ($size) = -s $file;
255 die "$file: stat: $!\n" if !defined $size;
256 die "$file: size not a multiple of 512 bytes\n" if $size % 512;
257 $cylinders = int ($size / (512 * 16 * 63));
258 $cylinders++ if $size % (512 * 16 * 63);
260 return (CAPACITY => $size / 512,