Enable ata1 under Bochs if hdc or hdd is needed.
[pintos-anon] / src / utils / pintos
1 #! /usr/bin/perl
2
3 $mem = 4;
4 $serial_out = 1;
5 while (@ARGV) {
6     my ($arg) = shift (@ARGV);
7     if ($arg =~ /--(bochs|qemu|gsx)$/) {
8         die "$arg conflicts with --$sim\n" if defined $sim;
9         $sim = $1;
10     } elsif ($arg =~ /--(no-debug|monitor|gdb)$/) {
11         die "$debug conflicts with --$debug" if defined $debug;
12         $debug = $1;
13     } elsif ($arg eq 'run') {
14         run_vm (@ARGV);
15         exit 0;
16     } elsif ($arg =~ /^mem(?:ory)?=(\d+)/) {
17         $mem = $1;
18     } elsif ($arg eq '--no-vga' || $arg eq '-nv') {
19         print "warning: --no-vga conflicts with --terminal\n"
20             if $vga eq 'terminal';
21         $vga = 'none';
22     } elsif ($arg eq '--no-serial' || $arg eq '-ns') {
23         $serial_out = 0;
24     } elsif ($arg eq '--terminal' || $arg eq '-t') {
25         print "warning: --terminal conflicts with --no-vga\n"
26             if $vga eq 'none';
27         $vga = 'terminal';
28     } elsif ($arg eq 'make-disk') {
29         my ($force) = @ARGV > 0 && $ARGV[0] eq '--force';
30         shift @ARGV if $force;
31         usage () if @ARGV != 2;
32         my ($file, $mb) = @ARGV;
33         usage () if $mb !~ /^\d+$/;
34         die "$file: already exists\n" if -e $file;
35
36         if ($mb != .5 && $mb != 1 && $mb != 1.5 && $mb != 2) {
37             print "$file: recommended sizes are .5, 1, 1.5, or 2 MB\n";
38             die "use --force to override\n" if !$force;
39         }
40
41         create_disk ($file, $mb * 1008);
42         exit 0;
43     } elsif ($arg eq 'put') {
44         usage () if @ARGV != 1 && @ARGV != 2;
45         my ($hostfn, $guestfn) = @ARGV;
46         $guestfn = $hostfn if !defined $guestfn;
47
48         # Create scratch disk from file.
49         die "$hostfn: $!\n" if ! -e $hostfn;
50         my ($size) = -s _;
51         copy_pad ($hostfn, "scratch.dsk", 512);
52
53         # Do copy.
54         run_vm ("-ci", $guestfn, $size, "-q");
55         exit 0;
56     } elsif ($arg eq 'get') {
57         usage () if @ARGV != 1 && @ARGV != 2;
58         my ($guestfn, $hostfn) = @ARGV;
59         $hostfn = $guestfn if !defined $hostfn;
60         die "$hostfn: already exists\n" if -e $file;
61
62         # Create scratch disk big enough for any file in the filesystem
63         # (modulo sparse files).
64         die "fs.dsk: $!\n" if ! -e "fs.dsk";
65         my ($fs_size) = -s _;
66         my ($scratch_size) = -s "scratch.dsk";
67         $scratch_size = 0 if !defined $scratch_size;
68         create_disk ("scratch.dsk", $fs_size / 1024 + 16)
69             if $scratch_size < $fs_size + 16384;
70
71         # Do copy.
72         run_vm ("-co", $guestfn, "-q");
73
74         # Read out scratch disk.
75         print "copying $guestfn from scratch.dsk to $hostfn...\n";
76         open (SRC, "<scratch.dsk") or die "scratch.dsk: open: $!\n";
77         open (DST, ">$hostfn") or die "$hostfn: create: $!\n";
78         my ($input);
79         read (SRC, $input, 512) == 512 or die "scratch.dsk: read error\n";
80         my ($size) = unpack ("%V", $input);
81         $size != 0xffffffff or die "$guestfn: too big for scratch.dsk?";
82         read (SRC, $src, $size) == $size or die "scratch.dsk: read error\n";
83         print DST $src or die "$hostfn: write error\n";
84         close (DST);
85         close (SRC);
86
87         exit 0;
88     } elsif ($arg eq 'help' || $arg eq '--help') {
89         usage (0);
90     } else {
91         die "unknown option `$arg'\n";
92     }
93 }
94 usage ();
95
96 sub usage {
97     my ($exitcode) = @_;
98     $exitcode = 1 unless defined $exitcode;
99     print "pintos, a utility for invoking Pintos in a simulator\n";
100     print "Usage: pintos [OPTION...] COMMAND [ARG...]\n";
101     print "where COMMAND is one of the following:\n";
102     print "  run [CMDLINE...]        run a VM in the simulator\n";
103     print "  make-disk FILE.DSK SIZE create FILE.DSK as empty SIZE MB disk\n";
104     print "  put HOSTFN [GUESTFN]    copy HOSTFN into VM (as GUESTFN)\n";
105     print "  get GUESTFN [HOSTFN]    copy GUESTFN out of VM (to HOSTFN)\n";
106     print "  help                    print this help message and exit\n";
107     print "Simulator options:\n";
108     print "  --bochs          (default) Use Bochs as simulator\n";
109     print "  --qemu           Use qemu as simulator\n";
110     print "  --gsx            Use VMware GSX Server 3.x as simulator\n";
111     print "Debugger options:\n";
112     print "  --no-debug       (default) No debugger\n";
113     print "  --monitor        Debug with simulator's monitor\n";
114     print "  --gdb            Debug with gdb\n";
115     print "Display options: (default is VGA + serial)\n";
116     print "  -nv, --no-vga    No VGA display\n";
117     print "  -ns, --no-serial No serial output\n";
118     print "  -t, --terminal   Display VGA in terminal (Bochs only)\n";
119     print "VM options:\n";
120     print "  --mem=MB         Run VM with MB megabytes of physical memory\n";
121     exit $exitcode;
122 }
123
124 sub copy_pad {
125     my ($src, $dst, $blocksize) = @_;
126     run_command ("dd", "if=$src", "of=$dst", "bs=$blocksize", "conv=sync");
127 }
128
129 sub create_disk {
130     my ($disk, $kb) = @_;
131     run_command ("dd", "if=/dev/zero", "of=$disk", "bs=1024", "count=$kb");
132 }
133
134 sub run_vm {
135     my (@disks) = (undef, undef, undef, undef);
136
137     $sim = "bochs" if !defined $sim;
138     $debug = "no-debug" if !defined $debug;
139     $vga = "window" if !defined $vga;
140
141     $disks[0] = "os.dsk";
142     $disks[1] = "fs.dsk" if -e "fs.dsk";
143     $disks[2] = "scratch.dsk" if -e "scratch.dsk";
144     $disks[3] = "swap.dsk" if -e "swap.dsk";
145
146     die "$disks[0]: can't find OS disk\n" if ! -e $disks[0];
147     write_cmd_line ($disks[0], @_);
148
149     if ($sim eq 'bochs') {
150         if ($debug eq 'no-debug') {
151             $bin = 'bochs';
152         } elsif ($debug eq 'monitor') {
153             $bin = 'bochs-dbg';
154         } elsif ($debug eq 'gdb') {
155             $bin = 'bochs-gdb';
156         }
157         $bochsbin = search_path ($bin);
158         $bochsshare = "$bochsbin/../share/bochs";
159         $romimage = "$bochsshare/BIOS-bochs-latest";
160         $vgaromimage = "$bochsshare/VGABIOS-lgpl-latest";
161
162         open (BOCHSRC, ">bochsrc.txt") or die "bochsrc.txt: create: $!\n";
163         print BOCHSRC "romimage: file=$romimage, address=0xf0000\n";
164         print BOCHSRC "vgaromimage: $vgaromimage\n";
165         print BOCHSRC bochs_disk_line ("ata0-master", $disks[0]);
166         print BOCHSRC bochs_disk_line ("ata0-slave", $disks[1]);
167         print BOCHSRC "ata1: enabled=1, ioaddr1=0x170, ioaddr2=0x370, irq=15\n"
168             if defined ($disks[2]) || defined ($disks[3]);
169         print BOCHSRC bochs_disk_line ("ata1-master", $disks[2]);
170         print BOCHSRC bochs_disk_line ("ata1-slave", $disks[3]);
171         print BOCHSRC "boot: c\n";
172         print BOCHSRC "ips: 1000000\n";
173         print BOCHSRC "clock: sync=none, time0=0\n";
174         print BOCHSRC "megs: $mem\n";
175         print BOCHSRC "log: bochsout.txt\n";
176         if ($vga ne 'terminal') {
177             print BOCHSRC "com1: enabled=1, dev=/dev/stdout\n"
178                 if $serial_out;
179             print BOCHSRC "display_library: nogui\n"
180                 if $vga eq 'none';
181         } else {
182             print BOCHSRC "display_library: term\n";
183         }
184         close (BOCHSRC);
185         run_command ($bin, '-q');
186     } elsif ($sim eq 'qemu') {
187         print "warning: qemu doesn't support --terminal\n"
188             if $vga eq 'terminal';
189         my (@cmd) = ('qemu');
190         push (@cmd, '-hda', $disks[0]) if defined $disks[0];
191         push (@cmd, '-hdb', $disks[1]) if defined $disks[1];
192         push (@cmd, '-hdc', $disks[2]) if defined $disks[2];
193         push (@cmd, '-hdd', $disks[3]) if defined $disks[3];
194         push (@cmd, '-m', $mem);
195         push (@cmd, '-nographic') if $vga eq 'none';
196         push (@cmd, '-serial', 'stdio') if $serial_out && $vga ne 'none';
197         push (@cmd, '-S') if $debug eq 'monitor';
198         push (@cmd, '-s') if $debug eq 'gdb';
199         run_command (@cmd);
200     } elsif ($sim eq 'gsx') {
201         print "warning: VMware GSX Server doesn't support --$debug\n"
202             if $debug ne 'no-debug';
203         print "warning: VMware GSX Server doesn't support --no-vga\n"
204             if $vga eq 'none';
205         print "warning: VMware GSX Server doesn't support --terminal\n"
206             if $vga eq 'terminal';
207
208         open (VMX, ">pintos.vmx") or die "pintos.vmx: create: $!\n";
209         chmod 0777 & ~umask, "pintos.vmx";
210         print VMX "#! /usr/bin/vmware -G\n";
211         print VMX "config.version = 6\n";
212         print VMX "guestOS = \"linux\"\n";
213         print VMX "floppy0.present = FALSE\n";
214
215         if (! -e 'null.bin') {
216             open (NULL, ">null.bin") or die "null.bin: create: $!\n";
217             close (NULL);
218         }
219
220         for (my ($i) = 0; $i < 4; $i++) {
221             my ($dsk) = $disks[$i];
222             next if !defined $dsk;
223             $device = "ide" . int ($i / 2) . ":" . ($i % 2);
224
225             my ($pln) = $dsk;
226             $pln =~ s/\.dsk//;
227             $pln .= ".pln";
228
229             print VMX "\n$device.present = TRUE\n";
230             print VMX "$device.deviceType = \"plainDisk\"\n";
231             print VMX "$device.fileName = \"$pln\"\n";
232
233             my (%geom) = disk_geometry ($dsk);
234             open (PLN, ">$pln") or die "$pln: create: $!\n";
235             print PLN "DRIVETYPE        ide\n";
236             print PLN "#vm|VERSION      2\n";
237             print PLN "#vm|TOOLSVERSION 2\n";
238             print PLN "CYLINDERS        $geom{C}\n";
239             print PLN "HEADS            $geom{H}\n";
240             print PLN "SECTORS          $geom{S}\n";
241             print PLN "#vm|CAPACITY     $geom{CAPACITY}\n";
242             print PLN "ACCESS \"$dsk\" 0 $geom{CAPACITY}\n";
243             close (PLN);
244         }
245         close (VMX);
246
247         use Cwd;
248         $vmx = getcwd () . "/pintos.vmx";
249         system ("vmware-cmd -s register $vmx >&/dev/null");
250         system ("vmware-cmd $vmx stop hard >&/dev/null");
251         system ("vmware -l -G -x -q $vmx");
252         system ("vmware-cmd $vmx stop hard >&/dev/null");
253     }
254 }
255
256 sub write_cmd_line {
257     my ($disk, @args) = @_;
258
259     die "command line includes empty string" if grep (/^$/, @args);
260     $args = join ("\0", @args) . "\0\0";
261     die "command line exceeds 128 bytes" if length ($args) > 128;
262     $args .= "\0" x (128 - length ($args));
263
264     print "writing command line to $disk...\n";
265     open (DISK, "+<$disk") or die "$disk: open: $!\n";
266     seek (DISK, 0x17e, 0) or die "$disk: seek: $!\n";
267     syswrite (DISK, $args) or die "$disk: write: $!\n";
268     close (DISK) or die "$disk: close: $!\n";
269 }
270
271 sub run_command {
272     print join (' ', @_), "\n";
273     die "command failed\n" if system (@_);
274 }
275
276 sub search_path {
277     my ($target) = @_;
278     for $dir (split (':', $ENV{PATH})) {
279         return $dir if -e "$dir/$target";
280     }
281     die "$target not in PATH\n";
282 }
283
284 sub bochs_disk_line {
285     my ($device, $file) = @_;
286     return "" if !defined $file;
287     my (%geom) = disk_geometry ($file);
288     return "$device: type=disk, path=$file, mode=flat, "
289         . "cylinders=$geom{C}, heads=$geom{H}, spt=$geom{S}, "
290         . "translation=none\n";
291 }
292
293 sub disk_geometry {
294     my ($file) = @_;
295     my ($size) = -s $file;
296     die "$file: stat: $!\n" if !defined $size;
297     die "$file: size not a multiple of 512 bytes\n" if $size % 512;
298     $cylinders = int ($size / (512 * 16 * 63));
299     $cylinders++ if $size % (512 * 16 * 63);
300
301     return (CAPACITY => $size / 512,
302             C => $cylinders,
303             H => 16,
304             S => 63);
305 }