/[gxemul]/trunk/src/emul_parse.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/src/emul_parse.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 24 - (show annotations)
Mon Oct 8 16:19:56 2007 UTC (16 years, 6 months ago) by dpavlin
File MIME type: text/plain
File size: 20773 byte(s)
++ trunk/HISTORY	(local)
$Id: HISTORY,v 1.1256 2006/06/23 20:43:44 debug Exp $
20060219	Various minor updates. Removing the old MIPS16 skeleton code,
		because it will need to be rewritten for dyntrans anyway.
20060220-22	Removing the non-working dyntrans backend support.
		Continuing on the 64-bit dyntrans virtual memory generalization.
20060223	More work on the 64-bit vm generalization.
20060225	Beginning on MIPS dyntrans load/store instructions.
		Minor PPC updates (64-bit load/store, etc).
		Fixes for the variable-instruction-length framework, some
		minor AVR updates (a simple Hello World program works!).
		Beginning on a skeleton for automatically generating documen-
		tation (for devices etc.).
20060226	PPC updates (adding some more 64-bit instructions, etc).
		AVR updates (more instructions).
		FINALLY found and fixed the zs bug, making NetBSD/macppc
		accept the serial console.
20060301	Adding more AVR instructions.
20060304	Continuing on AVR-related stuff. Beginning on a framework for
		cycle-accurate device emulation. Adding an experimental "PAL
		TV" device (just a dummy so far).
20060305	Adding more AVR instructions.
		Adding a dummy epcom serial controller (for TS7200 emulation).
20060310	Removing the emul() command from configuration files, so only
		net() and machine() are supported.
		Minor progress on the MIPS dyntrans rewrite.
20060311	Continuing on the MIPS dyntrans rewrite (adding more
		instructions, etc).
20060315	Adding more instructions (sllv, srav, srlv, bgtz[l], blez[l],
		beql, bnel, slti[u], various loads and stores).
20060316	Removing the ALWAYS_SIGNEXTEND_32 option, since it was rarely
		used.
		Adding more MIPS dyntrans instructions, and fixing bugs.
20060318	Implementing fast loads/stores for MIPS dyntrans (big/little
		endian, 32-bit and 64-bit modes).
20060320	Making MIPS dyntrans the default configure option; use
		"--enable-oldmips" to use the old bintrans system.
		Adding MIPS dyntrans dmult[u]; minor updates.
20060322	Continuing... adding some more instructions.
		Adding a simple skeleton for demangling C++ "_ZN" symbols.
20060323	Moving src/debugger.c into a new directory (src/debugger/).
20060324	Fixing the hack used to load PPC ELFs (useful for relocated
		Linux/ppc kernels), and adding a dummy G3 machine mode.
20060325-26	Beginning to experiment with GDB remote serial protocol
		connections; adding a -G command line option for selecting
		which TCP port to listen to.
20060330	Beginning a major cleanup to replace things like "0x%016llx"
		with more correct "0x%016"PRIx64, etc.
		Continuing on the GDB remote serial protocol support.
20060331	More cleanup, and some minor GDB remote progress.
20060402	Adding a hack to the configure script, to allow compilation
		on systems that lack PRIx64 etc.
20060406	Removing the temporary FreeBSD/arm hack in dev_ns16550.c and
		replacing it with a better fix from Olivier Houchard.
20060407	A remote debugger (gdb or ddd) can now start and stop the
		emulator using the GDB remote serial protocol, and registers
		and memory can be read. MIPS only for now.
20060408	More GDB progress: single-stepping also works, and also adding
		support for ARM, PowerPC, and Alpha targets.
		Continuing on the delay-slot-across-page-boundary issue.
20060412	Minor update: beginning to add support for the SPARC target
		to the remote GDB functionality.
20060414	Various MIPS updates: adding more instructions for dyntrans
		(eret, add), and making some exceptions work. Fixing a bug
		in dmult[u].
		Implementing the first SPARC instructions (sethi, or).
20060415	Adding "magic trap" instructions so that PROM calls can be
		software emulated in MIPS dyntrans.
		Adding more MIPS dyntrans instructions (ddiv, dadd) and
		fixing another bug in dmult.
20060416	More MIPS dyntrans progress: adding [d]addi, movn, movz, dsllv,
		rfi, an ugly hack for supporting R2000/R3000 style faked caches,
		preliminary interrupt support, and various other updates and
		bugfixes.
20060417	Adding more SPARC instructions (add, sub, sll[x], sra[x],
		srl[x]), and useful SPARC header definitions.
		Adding the first (trivial) x86/AMD64 dyntrans instructions (nop,
		cli/sti, stc/clc, std/cld, simple mov, inc ax). Various other
		x86 updates related to variable instruction length stuff.
		Adding unaligned loads/stores to the MIPS dyntrans mode (but
		still using the pre-dyntrans (slow) imlementation).
20060419	Fixing a MIPS dyntrans exception-in-delay-slot bug.
		Removing the old "show opcode statistics" functionality, since
		it wasn't really useful and isn't implemented for dyntrans.
		Single-stepping (or running with instruction trace) now looks
		ok with dyntrans with delay-slot architectures.
20060420	Minor hacks (removing the -B command line option when compiled
		for non-bintrans, and some other very minor updates).
		Adding (slow) MIPS dyntrans load-linked/store-conditional.
20060422	Applying fixes for bugs discovered by Nils Weller's nwcc
		(static DEC memmap => now per machine, and adding an extern
		keyword in cpu_arm_instr.c).
		Finally found one of the MIPS dyntrans bugs that I've been
		looking for (copy/paste spelling error BIG vs LITTLE endian in
		cpu_mips_instr_loadstore.c for 16-bit fast stores).
		FINALLY found the major MIPS dyntrans bug: slti vs sltiu
		signed/unsigned code in cpu_mips_instr.c. :-)
		Adding more MIPS dyntrans instructions (lwc1, swc1, bgezal[l],
		ctc1, tlt[u], tge[u], tne, beginning on rdhwr).
		NetBSD/hpcmips can now reach userland when using dyntrans :-)
		Adding some more x86 dyntrans instructions.
		Finally removed the old Alpha-specific virtual memory code,
		and replaced it with the generic 64-bit version.
		Beginning to add disassembly support for SPECIAL3 MIPS opcodes.
20060423	Continuing on the delay-slot-across-page-boundary issue;
		adding an end_of_page2 ic slot (like I had planned before, but
		had removed for some reason).
		Adding a quick-and-dirty fallback to legacy coprocessor 1
		code (i.e. skipping dyntrans implementation for now).
		NetBSD/hpcmips and NetBSD/pmax (when running on an emulated
		R4400) can now be installed and run. :-)  (Many bugs left
		to fix, though.)
		Adding more MIPS dyntrans instructions: madd[u], msub[u].
		Cleaning up the SPECIAL2 vs R5900/TX79/C790 "MMI" opcode
		maps somewhat (disassembly and dyntrans instruction decoding).
20060424	Adding an isa_revision field to mips_cpu_types.h, and making
		sure that SPECIAL3 opcodes cause Reserved Instruction
		exceptions on MIPS32/64 revisions lower than 2.
		Adding the SPARC 'ba', 'call', 'jmpl/retl', 'and', and 'xor'
		instructions.
20060425	Removing the -m command line option ("run at most x 
		instructions") and -T ("single_step_on_bad_addr"), because
		they never worked correctly with dyntrans anyway.
		Freshening up the man page.
20060428	Adding more MIPS dyntrans instructions: bltzal[l], idle.
		Enabling MIPS dyntrans compare interrupts.
20060429	FINALLY found the weird dyntrans bug, causing NetBSD etc. to
		behave strangely: some floating point code (conditional
		coprocessor branches) could not be reused from the old
		non-dyntrans code. The "quick-and-dirty fallback" only appeared
		to work. Fixing by implementing bc1* for MIPS dyntrans.
		More MIPS instructions: [d]sub, sdc1, ldc1, dmtc1, dmfc1, cfc0.
		Freshening up MIPS floating point disassembly appearance.
20060430	Continuing on C790/R5900/TX79 disassembly; implementing 128-bit
		"por" and "pextlw".
20060504	Disabling -u (userland emulation) unless compiled as unstable
		development version.
		Beginning on freshening up the testmachine include files,
		to make it easier to reuse those files (placing them in
		src/include/testmachine/), and beginning on a set of "demos"
		or "tutorials" for the testmachine functionality.
		Minor updates to the MIPS GDB remote protocol stub.
		Refreshing doc/experiments.html and gdb_remote.html.
		Enabling Alpha emulation in the stable release configuration,
		even though no guest OSes for Alpha can run yet.
20060505	Adding a generic 'settings' object, which will contain
		references to settable variables (which will later be possible
		to access using the debugger).
20060506	Updating dev_disk and corresponding demo/documentation (and
		switching from SCSI to IDE disk types, so it actually works
		with current test machines :-).
20060510	Adding a -D_LARGEFILE_SOURCE hack for 64-bit Linux hosts,
		so that fseeko() doesn't give a warning.
		Updating the section about how dyntrans works (the "runnable
		IR") in doc/intro.html.
		Instruction updates (some x64=1 checks, some more R5900
		dyntrans stuff: better mul/mult separation from MIPS32/64,
		adding ei and di).
		Updating MIPS cpuregs.h to a newer one (from NetBSD).
		Adding more MIPS dyntrans instructions: deret, ehb.
20060514	Adding disassembly and beginning implementation of SPARC wr
		and wrpr instructions.
20060515	Adding a SUN SPARC machine mode, with dummy SS20 and Ultra1
		machines. Adding the 32-bit "rd psr" instruction.
20060517	Disassembly support for the general SPARC rd instruction.
		Partial implementation of the cmp (subcc) instruction.
		Some other minor updates (making sure that R5900 processors
		start up with the EIE bit enabled, otherwise Linux/playstation2
		receives no interrupts).
20060519	Minor MIPS updates/cleanups.
20060521	Moving the MeshCube machine into evbmips; this seems to work
		reasonably well with a snapshot of a NetBSD MeshCube kernel.
		Cleanup/fix of MIPS config0 register initialization.
20060529	Minor MIPS fixes, including a sign-extension fix to the
		unaligned load/store code, which makes NetBSD/pmax on R3000
		work better with dyntrans. (Ultrix and Linux/DECstation still
		don't work, though.)
20060530	Minor updates to the Alpha machine mode: adding an AlphaBook
		mode, an LCA bus (forwarding accesses to an ISA bus), etc.
20060531	Applying a bugfix for the MIPS dyntrans sc[d] instruction from
		Ondrej Palkovsky. (Many thanks.)
20060601	Minifix to allow ARM immediate msr instruction to not give
		an error for some valid values.
		More Alpha updates.
20060602	Some minor Alpha updates.
20060603	Adding the Alpha cmpbge instruction. NetBSD/alpha prints its
		first boot messages :-) on an emulated Alphabook 1.
20060612	Minor updates; adding a dev_ether.h include file for the
		testmachine ether device. Continuing the hunt for the dyntrans
		bug which makes Linux and Ultrix on DECstation behave
		strangely... FINALLY found it! It seems to be related to
		invalidation of the translation cache, on tlbw{r,i}. There
		also seems to be some remaining interrupt-related problems.
20060614	Correcting the implementation of ldc1/sdc1 for MIPS dyntrans
		(so that it uses 16 32-bit registers if the FR bit in the
		status register is not set).
20060616	REMOVING BINTRANS COMPLETELY!
		Removing the old MIPS interpretation mode.
		Removing the MFHILO_DELAY and instruction delay stuff, because
		they wouldn't work with dyntrans anyway.
20060617	Some documentation updates (adding "NetBSD-archive" to some
		URLs, and new Debian/DECstation installation screenshots).
		Removing the "tracenull" and "enable-caches" configure options.
		Improving MIPS dyntrans performance somewhat (only invalidate
		translations if necessary, on writes to the entryhi register,
		instead of doing it for all cop0 writes).
20060618	More cleanup after the removal of the old MIPS emulation.
		Trying to fix the MIPS dyntrans performance bugs/bottlenecks;
		only semi-successful so far (for R3000).
20060620	Minor update to allow clean compilation again on Tru64/Alpha.
20060622	MIPS cleanup and fixes (removing the pc_last stuff, which
		doesn't make sense with dyntrans anyway, and fixing a cross-
		page-delay-slot-with-exception case in end_of_page).
		Removing the old max_random_cycles_per_chunk stuff, and the
		concept of cycles vs instructions for MIPS emulation.
		FINALLY found and fixed the bug which caused NetBSD/pmax
		clocks to behave strangely (it was a load to the zero register,
		which was treated as a NOP; now it is treated as a load to a
		dummy scratch register).
20060623	Increasing the dyntrans chunk size back to
		N_SAFE_DYNTRANS_LIMIT, instead of N_SAFE_DYNTRANS_LIMIT/2.
		Preparing for a quick release, even though there are known
		bugs, and performance for non-R3000 MIPS emulation is very
		poor. :-/
		Reverting to half the dyntrans chunk size again, because
		NetBSD/cats seemed less stable with full size chunks. :(
		NetBSD/sgimips 3.0 can now run :-)  (With release 0.3.8, only
		NetBSD/sgimips 2.1 worked, not 3.0.)

==============  RELEASE 0.4.0  ==============


1 /*
2 * Copyright (C) 2005-2006 Anders Gavare. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 *
28 * $Id: emul_parse.c,v 1.42 2006/06/22 13:22:41 debug Exp $
29 *
30 * Set up an emulation by parsing a config file.
31 *
32 * TODO: REWRITE THIS FROM SCRATCH! :-)
33 */
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38
39 #include "diskimage.h"
40 #include "emul.h"
41 #include "machine.h"
42 #include "misc.h"
43 #include "net.h"
44
45
46 #define is_word_char(ch) ( \
47 (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || \
48 ch == '_' || ch == '$' || (ch >= '0' && ch <= '9') )
49
50 #define MAX_WORD_LEN 200
51
52 #define EXPECT_WORD 1
53 #define EXPECT_LEFT_PARENTHESIS 2
54 #define EXPECT_RIGHT_PARENTHESIS 4
55
56 static int parenthesis_level = 0;
57
58
59 /*
60 * read_one_word():
61 *
62 * Reads the file f until it has read a complete word. Whitespace is ignored,
63 * and also any exclamation mark ('!') and anything following an exclamation
64 * mark on a line.
65 *
66 * Used internally by emul_parse_config().
67 */
68 static void read_one_word(FILE *f, char *buf, int buflen, int *line,
69 int expect)
70 {
71 int ch;
72 int done = 0;
73 int curlen = 0;
74 int in_word = 0;
75
76 while (!done) {
77 if (curlen >= buflen - 1)
78 break;
79
80 ch = fgetc(f);
81 if (ch == EOF)
82 break;
83
84 if (in_word) {
85 if (is_word_char(ch)) {
86 buf[curlen++] = ch;
87 if (curlen == buflen - 1)
88 done = 1;
89 continue;
90 } else {
91 if (ungetc(ch, f) == EOF) {
92 fatal("ungetc() failed?\n");
93 exit(1);
94 }
95 break;
96 }
97 }
98
99 if (ch == '\n') {
100 (*line) ++;
101 continue;
102 }
103
104 if (ch == '\r')
105 continue;
106
107 if (ch == '!') {
108 /* Skip until newline: */
109 while (ch != '\n' && ch != EOF)
110 ch = fgetc(f);
111 if (ch == '\n')
112 (*line) ++;
113 continue;
114 }
115
116 if (ch == '{') {
117 int depth = 1;
118
119 /* Skip until '}': */
120 while (depth > 0) {
121 ch = fgetc(f);
122 if (ch == '\n')
123 (*line) ++;
124 if (ch == '{')
125 depth ++;
126 if (ch == '}')
127 depth --;
128 if (ch == EOF) {
129 fatal("line %i: unexpected EOF inside"
130 " a nested comment\n", *line);
131 exit(1);
132 }
133 }
134 continue;
135 }
136
137 /* Whitespace? */
138 if (ch <= ' ')
139 continue;
140
141 if (ch == '"' || ch == '\'') {
142 /* This is a quoted word. */
143 int start_ch = ch;
144
145 if (expect & EXPECT_LEFT_PARENTHESIS) {
146 fatal("unexpected character '%c', line %i\n",
147 ch, *line);
148 exit(1);
149 }
150
151 while (curlen < buflen - 1) {
152 ch = fgetc(f);
153 if (ch == '\n') {
154 fatal("line %i: newline inside"
155 " quotes?\n", *line);
156 exit(1);
157 }
158 if (ch == EOF) {
159 fatal("line %i: EOF inside a quoted"
160 " string?\n", *line);
161 exit(1);
162 }
163 if (ch == start_ch)
164 break;
165 buf[curlen++] = ch;
166 }
167 break;
168 }
169
170 if ((expect & EXPECT_WORD) && is_word_char(ch)) {
171 buf[curlen++] = ch;
172 in_word = 1;
173 if (curlen == buflen - 1)
174 done = 1;
175 } else {
176 if ((expect & EXPECT_LEFT_PARENTHESIS) && ch == '(') {
177 parenthesis_level ++;
178 buf[curlen++] = ch;
179 break;
180 }
181 if ((expect & EXPECT_RIGHT_PARENTHESIS) && ch == ')') {
182 parenthesis_level --;
183 buf[curlen++] = ch;
184 break;
185 }
186
187 fatal("unexpected character '%c', line %i\n",
188 ch, *line);
189 exit(1);
190 }
191 }
192
193 buf[curlen] = '\0';
194 }
195
196
197 #define PARSESTATE_NONE 0
198 #define PARSESTATE_EMUL 1
199 #define PARSESTATE_NET 2
200 #define PARSESTATE_MACHINE 3
201
202 static char cur_net_ipv4net[50];
203 static char cur_net_ipv4len[50];
204 static char cur_net_local_port[10];
205 #define MAX_N_REMOTE 20
206 #define MAX_REMOTE_LEN 100
207 static char *cur_net_remote[MAX_N_REMOTE];
208 static int cur_net_n_remote;
209
210 static char cur_machine_name[50];
211 static char cur_machine_cpu[50];
212 static char cur_machine_type[50];
213 static char cur_machine_subtype[50];
214 static char cur_machine_bootname[150];
215 static char cur_machine_bootarg[250];
216 static char cur_machine_slowsi[10];
217 static char cur_machine_prom_emulation[10];
218 static char cur_machine_use_x11[10];
219 static char cur_machine_x11_scaledown[10];
220 static char cur_machine_byte_order[20];
221 static char cur_machine_random_mem[10];
222 static char cur_machine_random_cpu[10];
223 static char cur_machine_force_netboot[10];
224 static char cur_machine_start_paused[10];
225 static char cur_machine_ncpus[10];
226 static char cur_machine_n_gfx_cards[10];
227 static char cur_machine_serial_nr[10];
228 static char cur_machine_emulated_hz[10];
229 static char cur_machine_memory[10];
230 #define MAX_N_LOAD 15
231 #define MAX_LOAD_LEN 2000
232 static char *cur_machine_load[MAX_N_LOAD];
233 static int cur_machine_n_load;
234 #define MAX_N_DISK 10
235 #define MAX_DISK_LEN 2000
236 static char *cur_machine_disk[MAX_N_DISK];
237 static int cur_machine_n_disk;
238 #define MAX_N_DEVICE 20
239 #define MAX_DEVICE_LEN 400
240 static char *cur_machine_device[MAX_N_DISK];
241 static int cur_machine_n_device;
242 #define MAX_N_X11_DISP 5
243 #define MAX_X11_DISP_LEN 1000
244 static char *cur_machine_x11_disp[MAX_N_X11_DISP];
245 static int cur_machine_n_x11_disp;
246
247 #define WORD(w,var) { \
248 if (strcmp(word, w) == 0) { \
249 read_one_word(f, word, maxbuflen, \
250 line, EXPECT_LEFT_PARENTHESIS); \
251 read_one_word(f, var, sizeof(var), \
252 line, EXPECT_WORD); \
253 read_one_word(f, word, maxbuflen, \
254 line, EXPECT_RIGHT_PARENTHESIS); \
255 return; \
256 } \
257 }
258
259 static void parse__machine(struct emul *e, FILE *f, int *in_emul, int *line,
260 int *parsestate, char *word, size_t maxbuflen);
261
262
263 /*
264 * parse_on_off():
265 *
266 * Returns 1 for "on", "yes", "enable", or "1".
267 * Returns 0 for "off", "no", "disable", or "0".
268 * Prints a fatal warning and exit()s for other values.
269 */
270 int parse_on_off(char *s)
271 {
272 if (strcasecmp(s, "on") == 0 || strcasecmp(s, "yes") == 0 ||
273 strcasecmp(s, "enable") == 0 || strcasecmp(s, "1") == 0)
274 return 1;
275 if (strcasecmp(s, "off") == 0 || strcasecmp(s, "no") == 0 ||
276 strcasecmp(s, "disable") == 0 || strcasecmp(s, "0") == 0)
277 return 0;
278
279 fprintf(stderr, "parse_on_off(): WARNING: unknown value '%s'\n", s);
280
281 return 0;
282 }
283
284
285 /*
286 * parse__emul():
287 *
288 * name, net, machine
289 */
290 static void parse__emul(struct emul *e, FILE *f, int *in_emul, int *line,
291 int *parsestate, char *word, size_t maxbuflen)
292 {
293 if (word[0] == ')') {
294 *parsestate = PARSESTATE_NONE;
295 return;
296 }
297
298 if (strcmp(word, "name") == 0) {
299 char tmp[200];
300 read_one_word(f, word, maxbuflen,
301 line, EXPECT_LEFT_PARENTHESIS);
302 read_one_word(f, tmp, sizeof(tmp), line, EXPECT_WORD);
303 read_one_word(f, word, maxbuflen,
304 line, EXPECT_RIGHT_PARENTHESIS);
305 if (e->name != NULL) {
306 free(e->name);
307 e->name = NULL;
308 }
309 e->name = strdup(tmp);
310 if (e->name == NULL) {
311 fprintf(stderr, "out of memory in parse__emul()\n");
312 exit(1);
313 }
314 debug("name: \"%s\"\n", e->name);
315 return;
316 }
317
318 if (strcmp(word, "net") == 0) {
319 *parsestate = PARSESTATE_NET;
320 read_one_word(f, word, maxbuflen,
321 line, EXPECT_LEFT_PARENTHESIS);
322
323 /* Default net: */
324 strlcpy(cur_net_ipv4net, "10.0.0.0", sizeof(cur_net_ipv4net));
325 strlcpy(cur_net_ipv4len, "8", sizeof(cur_net_ipv4len));
326 strlcpy(cur_net_local_port, "", sizeof(cur_net_local_port));
327 cur_net_n_remote = 0;
328 return;
329 }
330
331 if (strcmp(word, "machine") == 0) {
332 *parsestate = PARSESTATE_MACHINE;
333 read_one_word(f, word, maxbuflen,
334 line, EXPECT_LEFT_PARENTHESIS);
335
336 /* A "zero state": */
337 cur_machine_name[0] = '\0';
338 cur_machine_cpu[0] = '\0';
339 cur_machine_type[0] = '\0';
340 cur_machine_subtype[0] = '\0';
341 cur_machine_bootname[0] = '\0';
342 cur_machine_bootarg[0] = '\0';
343 cur_machine_n_load = 0;
344 cur_machine_n_disk = 0;
345 cur_machine_n_device = 0;
346 cur_machine_n_x11_disp = 0;
347 cur_machine_slowsi[0] = '\0';
348 cur_machine_prom_emulation[0] = '\0';
349 cur_machine_use_x11[0] = '\0';
350 cur_machine_x11_scaledown[0] = '\0';
351 cur_machine_byte_order[0] = '\0';
352 cur_machine_random_mem[0] = '\0';
353 cur_machine_random_cpu[0] = '\0';
354 cur_machine_force_netboot[0] = '\0';
355 cur_machine_start_paused[0] = '\0';
356 cur_machine_ncpus[0] = '\0';
357 cur_machine_n_gfx_cards[0] = '\0';
358 cur_machine_serial_nr[0] = '\0';
359 cur_machine_emulated_hz[0] = '\0';
360 cur_machine_memory[0] = '\0';
361 return;
362 }
363
364 fatal("line %i: not expecting '%s' in an 'emul' section\n",
365 *line, word);
366 exit(1);
367 }
368
369
370 /*
371 * parse__net():
372 *
373 * Simple words: ipv4net, ipv4len, local_port
374 *
375 * Complex: add_remote
376 *
377 * TODO: more words? for example an option to disable the gateway? that would
378 * have to be implemented correctly in src/net.c first.
379 */
380 static void parse__net(struct emul *e, FILE *f, int *in_emul, int *line,
381 int *parsestate, char *word, size_t maxbuflen)
382 {
383 int i;
384
385 if (word[0] == ')') {
386 /* Finished with the 'net' section. Let's create the net: */
387 if (e->net != NULL) {
388 fatal("line %i: more than one net isn't really "
389 "supported yet\n", *line);
390 exit(1);
391 }
392
393 if (!cur_net_local_port[0])
394 strlcpy(cur_net_local_port, "0",
395 sizeof(cur_net_local_port));
396
397 e->net = net_init(e, NET_INIT_FLAG_GATEWAY,
398 cur_net_ipv4net, atoi(cur_net_ipv4len),
399 cur_net_remote, cur_net_n_remote,
400 atoi(cur_net_local_port));
401
402 if (e->net == NULL) {
403 fatal("line %i: fatal error: could not create"
404 " the net (?)\n", *line);
405 exit(1);
406 }
407
408 for (i=0; i<cur_net_n_remote; i++) {
409 free(cur_net_remote[i]);
410 cur_net_remote[i] = NULL;
411 }
412
413 *parsestate = PARSESTATE_EMUL;
414 return;
415 }
416
417 WORD("ipv4net", cur_net_ipv4net);
418 WORD("ipv4len", cur_net_ipv4len);
419 WORD("local_port", cur_net_local_port);
420
421 if (strcmp(word, "add_remote") == 0) {
422 read_one_word(f, word, maxbuflen,
423 line, EXPECT_LEFT_PARENTHESIS);
424 if (cur_net_n_remote >= MAX_N_REMOTE) {
425 fprintf(stderr, "too many remote networks\n");
426 exit(1);
427 }
428 cur_net_remote[cur_net_n_remote] = malloc(MAX_REMOTE_LEN);
429 if (cur_net_remote[cur_net_n_remote] == NULL) {
430 fprintf(stderr, "out of memory\n");
431 exit(1);
432 }
433 read_one_word(f, cur_net_remote[cur_net_n_remote],
434 MAX_REMOTE_LEN, line, EXPECT_WORD);
435 cur_net_n_remote ++;
436 read_one_word(f, word, maxbuflen, line,
437 EXPECT_RIGHT_PARENTHESIS);
438 return;
439 }
440
441 fatal("line %i: not expecting '%s' in a 'net' section\n", *line, word);
442 exit(1);
443 }
444
445
446 /*
447 * parse__machine():
448 */
449 static void parse__machine(struct emul *e, FILE *f, int *in_emul, int *line,
450 int *parsestate, char *word, size_t maxbuflen)
451 {
452 int r, i;
453
454 if (word[0] == ')') {
455 /* Finished with the 'machine' section. */
456 struct machine *m;
457
458 if (!cur_machine_name[0])
459 strlcpy(cur_machine_name, "no_name",
460 sizeof(cur_machine_name));
461
462 m = emul_add_machine(e, cur_machine_name);
463
464 r = machine_name_to_type(cur_machine_type, cur_machine_subtype,
465 &m->machine_type, &m->machine_subtype, &m->arch);
466 if (!r)
467 exit(1);
468
469 if (cur_machine_cpu[0])
470 m->cpu_name = strdup(cur_machine_cpu);
471
472 if (!cur_machine_use_x11[0])
473 strlcpy(cur_machine_use_x11, "no",
474 sizeof(cur_machine_use_x11));
475 m->use_x11 = parse_on_off(cur_machine_use_x11);
476
477 if (!cur_machine_slowsi[0])
478 strlcpy(cur_machine_slowsi, "no",
479 sizeof(cur_machine_slowsi));
480 m->slow_serial_interrupts_hack_for_linux =
481 parse_on_off(cur_machine_slowsi);
482
483 if (!cur_machine_prom_emulation[0])
484 strlcpy(cur_machine_prom_emulation, "yes",
485 sizeof(cur_machine_prom_emulation));
486 m->prom_emulation = parse_on_off(cur_machine_prom_emulation);
487
488 if (!cur_machine_random_mem[0])
489 strlcpy(cur_machine_random_mem, "no",
490 sizeof(cur_machine_random_mem));
491 m->random_mem_contents =
492 parse_on_off(cur_machine_random_mem);
493
494 if (!cur_machine_random_cpu[0])
495 strlcpy(cur_machine_random_cpu, "no",
496 sizeof(cur_machine_random_cpu));
497 m->use_random_bootstrap_cpu =
498 parse_on_off(cur_machine_random_cpu);
499
500 m->byte_order_override = NO_BYTE_ORDER_OVERRIDE;
501 if (cur_machine_byte_order[0]) {
502 if (strncasecmp(cur_machine_byte_order, "big", 3) == 0)
503 m->byte_order_override = EMUL_BIG_ENDIAN;
504 else if (strncasecmp(cur_machine_byte_order, "little",
505 6) == 0)
506 m->byte_order_override = EMUL_LITTLE_ENDIAN;
507 else {
508 fatal("Byte order must be big-endian or"
509 " little-endian\n");
510 exit(1);
511 }
512 }
513
514 if (!cur_machine_force_netboot[0])
515 strlcpy(cur_machine_force_netboot, "no",
516 sizeof(cur_machine_force_netboot));
517 m->force_netboot = parse_on_off(cur_machine_force_netboot);
518
519 if (!cur_machine_start_paused[0])
520 strlcpy(cur_machine_start_paused, "no",
521 sizeof(cur_machine_start_paused));
522 m->start_paused = parse_on_off(cur_machine_start_paused);
523
524 /* NOTE: Default nr of CPUs is 0: */
525 if (!cur_machine_ncpus[0])
526 strlcpy(cur_machine_ncpus, "0",
527 sizeof(cur_machine_ncpus));
528 m->ncpus = atoi(cur_machine_ncpus);
529
530 if (cur_machine_n_gfx_cards[0])
531 m->n_gfx_cards = atoi(cur_machine_n_gfx_cards);
532
533 if (cur_machine_serial_nr[0]) {
534 m->serial_nr = atoi(cur_machine_serial_nr);
535 e->next_serial_nr = m->serial_nr+1;
536 }
537
538 if (cur_machine_emulated_hz[0]) {
539 m->emulated_hz = mystrtoull(cur_machine_emulated_hz,
540 NULL, 0);
541 m->automatic_clock_adjustment = 0;
542 }
543
544 /* NOTE: Default nr of CPUs is 0: */
545 if (!cur_machine_memory[0])
546 strlcpy(cur_machine_memory, "0",
547 sizeof(cur_machine_memory));
548 m->physical_ram_in_mb = atoi(cur_machine_memory);
549
550 if (!cur_machine_x11_scaledown[0])
551 m->x11_scaledown = 1;
552 else {
553 m->x11_scaledown = atoi(cur_machine_x11_scaledown);
554 if (m->x11_scaledown < 0) {
555 m->x11_scaleup = 0 - m->x11_scaledown;
556 m->x11_scaledown = 1;
557 }
558 if (m->x11_scaledown < 1) {
559 fprintf(stderr, "Invalid scaledown value"
560 " (%i)\n", m->x11_scaledown);
561 exit(1);
562 }
563 }
564
565 for (i=0; i<cur_machine_n_disk; i++) {
566 diskimage_add(m, cur_machine_disk[i]);
567 free(cur_machine_disk[i]);
568 cur_machine_disk[i] = NULL;
569 }
570
571 m->boot_kernel_filename = strdup(cur_machine_bootname);
572
573 if (cur_machine_bootarg[0])
574 m->boot_string_argument = strdup(cur_machine_bootarg);
575
576 for (i=0; i<cur_machine_n_x11_disp; i++) {
577 m->x11_n_display_names ++;
578 m->x11_display_names = realloc(
579 m->x11_display_names, m->x11_n_display_names
580 * sizeof(char *));
581 if (m->x11_display_names == NULL) {
582 printf("out of memory\n");
583 exit(1);
584 }
585 m->x11_display_names[m->x11_n_display_names-1] =
586 strdup(cur_machine_x11_disp[i]);
587 if (m->x11_display_names
588 [m->x11_n_display_names-1] == NULL) {
589 printf("out of memory\n");
590 exit(1);
591 }
592 free(cur_machine_x11_disp[i]);
593 cur_machine_x11_disp[i] = NULL;
594 }
595
596 emul_machine_setup(m,
597 cur_machine_n_load, cur_machine_load,
598 cur_machine_n_device, cur_machine_device);
599
600 for (i=0; i<cur_machine_n_device; i++) {
601 free(cur_machine_device[i]);
602 cur_machine_device[i] = NULL;
603 }
604
605 for (i=0; i<cur_machine_n_load; i++) {
606 free(cur_machine_load[i]);
607 cur_machine_load[i] = NULL;
608 }
609
610 *parsestate = PARSESTATE_EMUL;
611 return;
612 }
613
614 WORD("name", cur_machine_name);
615 WORD("cpu", cur_machine_cpu);
616 WORD("type", cur_machine_type);
617 WORD("subtype", cur_machine_subtype);
618 WORD("bootname", cur_machine_bootname);
619 WORD("bootarg", cur_machine_bootarg);
620 WORD("slow_serial_interrupts_hack_for_linux", cur_machine_slowsi);
621 WORD("prom_emulation", cur_machine_prom_emulation);
622 WORD("use_x11", cur_machine_use_x11);
623 WORD("x11_scaledown", cur_machine_x11_scaledown);
624 WORD("byte_order", cur_machine_byte_order);
625 WORD("random_mem_contents", cur_machine_random_mem);
626 WORD("use_random_bootstrap_cpu", cur_machine_random_cpu);
627 WORD("force_netboot", cur_machine_force_netboot);
628 WORD("ncpus", cur_machine_ncpus);
629 WORD("serial_nr", cur_machine_serial_nr);
630 WORD("n_gfx_cards", cur_machine_n_gfx_cards);
631 WORD("emulated_hz", cur_machine_emulated_hz);
632 WORD("memory", cur_machine_memory);
633 WORD("start_paused", cur_machine_start_paused);
634
635 if (strcmp(word, "load") == 0) {
636 read_one_word(f, word, maxbuflen,
637 line, EXPECT_LEFT_PARENTHESIS);
638 if (cur_machine_n_load >= MAX_N_LOAD) {
639 fprintf(stderr, "too many loads\n");
640 exit(1);
641 }
642 cur_machine_load[cur_machine_n_load] = malloc(MAX_LOAD_LEN);
643 if (cur_machine_load[cur_machine_n_load] == NULL) {
644 fprintf(stderr, "out of memory\n");
645 exit(1);
646 }
647 read_one_word(f, cur_machine_load[cur_machine_n_load],
648 MAX_LOAD_LEN, line, EXPECT_WORD);
649 cur_machine_n_load ++;
650 read_one_word(f, word, maxbuflen,
651 line, EXPECT_RIGHT_PARENTHESIS);
652 return;
653 }
654
655 if (strcmp(word, "disk") == 0) {
656 read_one_word(f, word, maxbuflen,
657 line, EXPECT_LEFT_PARENTHESIS);
658 if (cur_machine_n_disk >= MAX_N_DISK) {
659 fprintf(stderr, "too many disks\n");
660 exit(1);
661 }
662 cur_machine_disk[cur_machine_n_disk] = malloc(MAX_DISK_LEN);
663 if (cur_machine_disk[cur_machine_n_disk] == NULL) {
664 fprintf(stderr, "out of memory\n");
665 exit(1);
666 }
667 read_one_word(f, cur_machine_disk[cur_machine_n_disk],
668 MAX_DISK_LEN, line, EXPECT_WORD);
669 cur_machine_n_disk ++;
670 read_one_word(f, word, maxbuflen,
671 line, EXPECT_RIGHT_PARENTHESIS);
672 return;
673 }
674
675 if (strcmp(word, "device") == 0) {
676 read_one_word(f, word, maxbuflen,
677 line, EXPECT_LEFT_PARENTHESIS);
678 if (cur_machine_n_device >= MAX_N_DEVICE) {
679 fprintf(stderr, "too many devices\n");
680 exit(1);
681 }
682 cur_machine_device[cur_machine_n_device] =
683 malloc(MAX_DEVICE_LEN);
684 if (cur_machine_device[cur_machine_n_device] == NULL) {
685 fprintf(stderr, "out of memory\n");
686 exit(1);
687 }
688 read_one_word(f, cur_machine_device[cur_machine_n_device],
689 MAX_DEVICE_LEN, line, EXPECT_WORD);
690 cur_machine_n_device ++;
691 read_one_word(f, word, maxbuflen,
692 line, EXPECT_RIGHT_PARENTHESIS);
693 return;
694 }
695
696 if (strcmp(word, "add_x11_display") == 0) {
697 read_one_word(f, word, maxbuflen,
698 line, EXPECT_LEFT_PARENTHESIS);
699 if (cur_machine_n_x11_disp >= MAX_N_X11_DISP) {
700 fprintf(stderr, "too many x11 displays\n");
701 exit(1);
702 }
703 cur_machine_x11_disp[cur_machine_n_x11_disp] =
704 malloc(MAX_X11_DISP_LEN);
705 if (cur_machine_x11_disp[cur_machine_n_x11_disp] == NULL) {
706 fprintf(stderr, "out of memory\n");
707 exit(1);
708 }
709 read_one_word(f, cur_machine_x11_disp[cur_machine_n_x11_disp],
710 MAX_X11_DISP_LEN, line, EXPECT_WORD);
711 cur_machine_n_x11_disp ++;
712 read_one_word(f, word, maxbuflen,
713 line, EXPECT_RIGHT_PARENTHESIS);
714 return;
715 }
716
717 fatal("line %i: not expecting '%s' in a 'machine' section\n",
718 *line, word);
719 exit(1);
720 }
721
722
723 /*
724 * emul_parse_config():
725 *
726 * Set up an emulation by parsing a config file.
727 */
728 void emul_parse_config(struct emul *e, char *fname)
729 {
730 FILE *f = fopen(fname, "r");
731 char word[MAX_WORD_LEN];
732 int in_emul = 0;
733 int line = 1;
734 int parsestate = PARSESTATE_EMUL;
735
736 /* debug("emul_parse_config()\n"); */
737 if (f == NULL) {
738 perror(fname);
739 exit(1);
740 }
741
742 while (!feof(f)) {
743 read_one_word(f, word, sizeof(word), &line,
744 EXPECT_WORD | EXPECT_RIGHT_PARENTHESIS);
745 if (!word[0])
746 break;
747
748 /* debug("word = '%s'\n", word); */
749
750 switch (parsestate) {
751 case PARSESTATE_EMUL:
752 parse__emul(e, f, &in_emul, &line, &parsestate,
753 word, sizeof(word));
754 break;
755 case PARSESTATE_NET:
756 parse__net(e, f, &in_emul, &line, &parsestate,
757 word, sizeof(word));
758 break;
759 case PARSESTATE_MACHINE:
760 parse__machine(e, f, &in_emul, &line, &parsestate,
761 word, sizeof(word));
762 break;
763 case PARSESTATE_NONE:
764 break;
765 default:
766 fatal("INTERNAL ERROR in emul_parse.c ("
767 "parsestate %i is not imlemented yet?)\n",
768 parsestate);
769 exit(1);
770 }
771 }
772
773 if (parenthesis_level != 0) {
774 fatal("EOF but not enough right parentheses?\n");
775 exit(1);
776 }
777
778 fclose(f);
779 }
780

  ViewVC Help
Powered by ViewVC 1.1.26