/[gxemul]/trunk/src/devices/device.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/devices/device.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 44 - (show annotations)
Mon Oct 8 16:22:56 2007 UTC (16 years, 6 months ago) by dpavlin
File MIME type: text/plain
File size: 11421 byte(s)
++ trunk/HISTORY	(local)
$Id: HISTORY,v 1.1632 2007/09/11 21:46:35 debug Exp $
20070616	Implementing the MIPS32/64 revision 2 "ror" instruction.
20070617	Adding a struct for each physpage which keeps track of which
		ranges within that page (base offset, length) that are
		continuously translatable. When running with native code
		generation enabled (-b), a range is added after each read-
		ahead loop.
		Experimenting with using the physical program counter sample
		data (implemented 20070608) together with the "translatable
		range" information, to figure out which physical address ranges
		would be worth translating to native code (if the number of
		samples falling within a range is above a certain threshold).
20070618	Adding automagic building of .index comment files for
		src/file/, src/promemul/, src src/useremul/ as well.
		Adding a "has been translated" bit to the ranges, so that only
		not-yet-translated ranges will be sampled.
20070619	Moving src/cpu.c and src/memory_rw.c into src/cpus/,
		src/device.c into src/devices/, and src/machine.c into
		src/machines/.
		Creating a skeleton cc/ld native backend module; beginning on
		the function which will detect cc command line, etc.
20070620	Continuing on the native code generation infrastructure.
20070621	Moving src/x11.c and src/console.c into a new src/console/
		subdir (for everything that is console or framebuffer related).
		Moving src/symbol*.c into a new src/symbol/, which should
		contain anything that is symbol handling related.
20070624	Making the program counter sampling threshold a "settings
		variable" (sampling_threshold), i.e. it can now be changed
		during runtime.
		Switching the RELEASE notes format from plain text to HTML.
		If the TMPDIR environment variable is set, it is used instead
		of "/tmp" for temporary files.
		Continuing on the cc/ld backend: simple .c code is generated,
		the compiler and linker are called, etc.
		Adding detection of host architecture to the configure script
		(again), and adding icache invalidation support (only
		implemented for Alpha hosts so far).
20070625	Simplifying the program counter sampling mechanism.
20070626	Removing the cc/ld native code generation stuff, program
		counter sampling, etc; it would not have worked well in the
		general case.
20070627	Removing everything related to native code generation.
20070629	Removing the (practically unusable) support for multiple
		emulations. (The single emulation allowed now still supports
		multiple simultaneous machines, as before.)
		Beginning on PCCTWO and M88K interrupts.
20070723	Adding a dummy skeleton for emulation of M32R processors.
20070901	Fixing a warning found by "gcc version 4.3.0 20070817
		(experimental)" on amd64.
20070905	Removing some more traces of the old "multiple emulations"
		code.
		Also looking in /usr/local/include and /usr/local/lib for
		X11 libs, when running configure.
20070909	Minor updates to the guest OS install instructions, in
		preparation for the NetBSD 4.0 release.
20070918	More testing of NetBSD 4.0 RC1.

1 /*
2 * Copyright (C) 2005-2007 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: device.c,v 1.2 2007/06/19 04:14:59 debug Exp $
29 *
30 * COMMENT: Device registry framework
31 */
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36
37 #include "device.h"
38 #include "machine.h"
39 #include "memory.h"
40 #include "misc.h"
41
42
43 static struct device_entry *device_entries = NULL;
44 static int device_entries_sorted = 0;
45 static int n_device_entries = 0;
46 static int device_exit_on_error = 1;
47
48 static struct pci_entry *pci_entries = NULL;
49 static int n_pci_entries = 0;
50
51
52 /*
53 * device_entry_compar():
54 *
55 * Internal function, used by sort_entries().
56 */
57 static int device_entry_compar(const void *a, const void *b)
58 {
59 const struct device_entry *pa = a, *pb = b;
60 return strcmp(pa->name, pb->name);
61 }
62
63
64 /*
65 * sort_entries():
66 *
67 * Internal function. Sorts the device_entries array in alphabetic order.
68 */
69 static void sort_entries(void)
70 {
71 qsort(device_entries, n_device_entries, sizeof(struct device_entry),
72 device_entry_compar);
73
74 device_entries_sorted = 1;
75 }
76
77
78 /*
79 * device_register():
80 *
81 * Registers a device. The device is added to the end of the device_entries
82 * array, and the sorted flag is set to zero.
83 *
84 * NOTE: It would be a bad thing if two devices had the same name. However,
85 * that isn't checked here, it is up to the caller!
86 *
87 * Return value is 1 if the device was registered, 0 otherwise.
88 */
89 int device_register(char *name, int (*initf)(struct devinit *))
90 {
91 CHECK_ALLOCATION(device_entries = realloc(device_entries,
92 sizeof(struct device_entry) * (n_device_entries + 1)));
93
94 memset(&device_entries[n_device_entries], 0,
95 sizeof(struct device_entry));
96
97 CHECK_ALLOCATION(device_entries[n_device_entries].name = strdup(name));
98 device_entries[n_device_entries].initf = initf;
99
100 device_entries_sorted = 0;
101 n_device_entries ++;
102 return 1;
103 }
104
105
106 /*
107 * pci_register():
108 *
109 * Registers a pci device. The pci device is added to the pci_entries array.
110 *
111 * Return value is 1 if the pci device was registered. If it was not
112 * added, this function does not return.
113 */
114 int pci_register(char *name, void (*initf)(struct machine *, struct memory *,
115 struct pci_device *))
116 {
117 CHECK_ALLOCATION(pci_entries = realloc(pci_entries,
118 sizeof(struct pci_entry) * (n_pci_entries + 1)));
119
120 memset(&pci_entries[n_pci_entries], 0, sizeof(struct pci_entry));
121
122 CHECK_ALLOCATION(pci_entries[n_pci_entries].name = strdup(name));
123 pci_entries[n_pci_entries].initf = initf;
124 n_pci_entries ++;
125 return 1;
126 }
127
128
129 /*
130 * pci_lookup_initf():
131 *
132 * Find a pci device init function by scanning the pci_entries array.
133 *
134 * Return value is a function pointer, or NULL if the name was not found.
135 */
136 void (*pci_lookup_initf(const char *name))(struct machine *machine,
137 struct memory *mem, struct pci_device *pd)
138 {
139 int i;
140
141 if (name == NULL) {
142 fprintf(stderr, "pci_lookup_initf(): name = NULL\n");
143 exit(1);
144 }
145
146 for (i=0; i<n_pci_entries; i++)
147 if (strcmp(name, pci_entries[i].name) == 0)
148 return pci_entries[i].initf;
149 return NULL;
150 }
151
152
153 /*
154 * device_lookup():
155 *
156 * Lookup a device name by scanning the device_entries array (as a binary
157 * search tree).
158 *
159 * Return value is a pointer to the device_entry on success, or a NULL pointer
160 * if there was no such device.
161 */
162 struct device_entry *device_lookup(char *name)
163 {
164 int hi, lo;
165
166 if (name == NULL) {
167 fprintf(stderr, "device_lookup(): NULL ptr\n");
168 exit(1);
169 }
170
171 if (!device_entries_sorted)
172 sort_entries();
173
174 if (n_device_entries == 0)
175 return NULL;
176
177 lo = 0; hi = n_device_entries - 1;
178
179 while (lo <= hi) {
180 int r, i = (lo + hi) / 2;
181
182 /* printf("device_lookup(): i=%i (lo=%i hi=%i)\n", i, lo, hi);
183 printf(" name='%s', '%s'\n", name,
184 device_entries[i].name); */
185
186 r = strcmp(name, device_entries[i].name);
187 if (r == 0) {
188 /* Found it! */
189 return &device_entries[i];
190 }
191
192 /* Try left or right half: */
193 if (r < 0)
194 hi = i - 1;
195 if (r > 0)
196 lo = i + 1;
197 }
198
199 return NULL;
200 }
201
202
203 /*
204 * device_unregister():
205 *
206 * Unregisters a device.
207 *
208 * Return value is 1 if a device was unregistered, 0 otherwise.
209 */
210 int device_unregister(char *name)
211 {
212 ssize_t i;
213 struct device_entry *p = device_lookup(name);
214
215 if (p == NULL) {
216 fatal("device_unregister(): no such device (\"%s\")\n", name);
217 return 0;
218 }
219
220 i = (size_t)p - (size_t)device_entries;
221 i /= sizeof(struct device_entry);
222
223 free(device_entries[i].name);
224 device_entries[i].name = NULL;
225
226 if (i == n_device_entries-1) {
227 /* Do nothing if we're removing the last array element. */
228 } else {
229 /* Remove array element i by copying the last element
230 to i's position: */
231 device_entries[i] = device_entries[n_device_entries-1];
232
233 /* The array is not sorted anymore: */
234 device_entries_sorted = 0;
235 }
236
237 n_device_entries --;
238
239 /* TODO: realloc? */
240 return 1;
241 }
242
243
244 /*
245 * device_add():
246 *
247 * Add a device to a machine. For example: "kn210 addr=0x12340000" adds a
248 * device called "kn210" at a specific address.
249 *
250 * TODO: This function is quite ugly, and should be cleaned up.
251 * name_and_params should be const.
252 */
253 void *device_add(struct machine *machine, char *name_and_params)
254 {
255 struct device_entry *p;
256 struct devinit devinit;
257 char *s2, *s3;
258 size_t len, interrupt_path_len = strlen(machine->path) + 100;
259 int quoted;
260
261 memset(&devinit, 0, sizeof(struct devinit));
262 devinit.machine = machine;
263
264 /* Default values: */
265 devinit.addr_mult = 1;
266 devinit.in_use = 1;
267
268 /* Get the device name first: */
269 s2 = name_and_params;
270 while (s2[0] != ',' && s2[0] != ' ' && s2[0] != '\0')
271 s2 ++;
272
273 len = (size_t)s2 - (size_t)name_and_params;
274 CHECK_ALLOCATION(devinit.name = malloc(len + 1));
275 memcpy(devinit.name, name_and_params, len);
276 devinit.name[len] = '\0';
277
278 /* Allocate space for the default interrupt name: */
279 CHECK_ALLOCATION(devinit.interrupt_path =
280 malloc(interrupt_path_len + 1));
281 snprintf(devinit.interrupt_path, interrupt_path_len,
282 "%s.cpu[%i]", machine->path, machine->bootstrap_cpu);
283
284 p = device_lookup(devinit.name);
285 if (p == NULL) {
286 fatal("no such device (\"%s\")\n", devinit.name);
287 if (device_exit_on_error)
288 exit(1);
289 else
290 goto return_fail;
291 }
292
293 /* Get params from name_and_params: */
294 while (*s2 != '\0') {
295 /* Skip spaces, commas, and semicolons: */
296 while (*s2 == ' ' || *s2 == ',' || *s2 == ';')
297 s2 ++;
298
299 if (*s2 == '\0')
300 break;
301
302 /* s2 now points to the next param. eg "addr=1234" */
303
304 /* Get a word (until there is a '=' sign): */
305 s3 = s2;
306 while (*s3 != '=' && *s3 != '\0')
307 s3 ++;
308 if (s3 == s2) {
309 fatal("weird param: %s\n", s2);
310 if (device_exit_on_error)
311 exit(1);
312 else
313 goto return_fail;
314 }
315 s3 ++;
316 /* s3 now points to the parameter value ("1234") */
317
318 if (strncmp(s2, "addr=", 5) == 0) {
319 devinit.addr = mystrtoull(s3, NULL, 0);
320 } else if (strncmp(s2, "addr2=", 6) == 0) {
321 devinit.addr2 = mystrtoull(s3, NULL, 0);
322 } else if (strncmp(s2, "len=", 4) == 0) {
323 devinit.len = mystrtoull(s3, NULL, 0);
324 } else if (strncmp(s2, "addr_mult=", 10) == 0) {
325 devinit.addr_mult = mystrtoull(s3, NULL, 0);
326 } else if (strncmp(s2, "pci_little_endian=", 18) == 0) {
327 devinit.pci_little_endian = mystrtoull(s3, NULL, 0);
328 switch (devinit.pci_little_endian) {
329 case 0: break;
330 case 1: devinit.pci_little_endian =
331 MEM_PCI_LITTLE_ENDIAN;
332 break;
333 default:fatal("Bad pci_little_endian value.\n");
334 exit(1);
335 }
336 } else if (strncmp(s2, "irq=", 4) == 0) {
337 snprintf(devinit.interrupt_path, interrupt_path_len,s3);
338 if (strchr(devinit.interrupt_path, ' ') != NULL)
339 *strchr(devinit.interrupt_path, ' ') = '\0';
340 } else if (strncmp(s2, "in_use=", 7) == 0) {
341 devinit.in_use = mystrtoull(s3, NULL, 0);
342 } else if (strncmp(s2, "name2=", 6) == 0) {
343 char *h = s2 + 6;
344 size_t len = 0;
345 quoted = 0;
346 while (*h) {
347 if (*h == '\'')
348 quoted = !quoted;
349 h++, len++;
350 if (!quoted && *h == ' ')
351 break;
352 }
353 CHECK_ALLOCATION(devinit.name2 = malloc(len + 1));
354 h = s2 + 6;
355 if (*h == '\'')
356 len -= 2, h++;
357 snprintf(devinit.name2, len + 1, h);
358 } else {
359 fatal("unknown param: %s\n", s2);
360 if (device_exit_on_error)
361 exit(1);
362 else
363 goto return_fail;
364 }
365
366 /* skip to the next param: */
367 s2 = s3;
368 quoted = 0;
369 while (*s2 != '\0' && (*s2 != ' ' || quoted) &&
370 *s2 != ',' && *s2 != ';') {
371 if (*s2 == '\'')
372 quoted = !quoted;
373 s2 ++;
374 }
375 }
376
377
378 /*
379 * Call the init function for this device:
380 */
381
382 devinit.return_ptr = NULL;
383
384 if (!p->initf(&devinit)) {
385 fatal("error adding device (\"%s\")\n", name_and_params);
386 if (device_exit_on_error)
387 exit(1);
388 else
389 goto return_fail;
390 }
391
392 free(devinit.interrupt_path);
393 free(devinit.name);
394 return devinit.return_ptr;
395
396 return_fail:
397 free(devinit.name);
398 return NULL;
399 }
400
401
402 /*
403 * device_dumplist():
404 *
405 * Dump a list of all registered devices. (If the list is not sorted when
406 * this function is called, it is implicitly sorted.)
407 */
408 void device_dumplist(void)
409 {
410 int i;
411
412 if (!device_entries_sorted)
413 sort_entries();
414
415 for (i=0; i<n_device_entries; i++) {
416 debug(" %s", device_entries[i].name);
417
418 /* TODO: flags? */
419
420 debug("\n");
421 }
422 }
423
424
425 /*
426 * device_set_exit_on_error():
427 *
428 * This function selects the behaviour of the emulator when a device is not
429 * found. During startup, it is nicest to abort the whole emulator session,
430 * but if a device addition is attempted from within the debugger, then it is
431 * nicer to just print a warning and continue.
432 */
433 void device_set_exit_on_error(int exit_on_error)
434 {
435 device_exit_on_error = exit_on_error;
436 }
437
438
439 /*
440 * device_init():
441 *
442 * Initialize the device registry, and call autodev_init() to automatically
443 * add all normal devices (from the src/devices/ directory).
444 *
445 * This function should be called before any other device_*() function is used.
446 */
447 void device_init(void)
448 {
449 device_entries = NULL;
450 device_entries_sorted = 0;
451 n_device_entries = 0;
452
453 autodev_init();
454 }
455

  ViewVC Help
Powered by ViewVC 1.1.26