Commit 0bfe3ca51ebddbf2cc099fa34f359bd1ac4f65e6
Committed by
Paul Brook
1 parent
70ec5dc0
Constructor support
Allow devices/drivers to register themselves via constructors. Destructors are not needed (can be registered from a constructor) and "priority" has been renamed and changed to an enum for clarity. Signed-off-by: Paul Brook <paul@codesourcery.com> Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
Showing
6 changed files
with
145 additions
and
17 deletions
Makefile
... | ... | @@ -63,7 +63,7 @@ recurse-all: $(SUBDIR_RULES) |
63 | 63 | ####################################################################### |
64 | 64 | # BLOCK_OBJS is code used by both qemu system emulation and qemu-img |
65 | 65 | |
66 | -BLOCK_OBJS=cutils.o cache-utils.o qemu-malloc.o | |
66 | +BLOCK_OBJS=cutils.o cache-utils.o qemu-malloc.o module.o | |
67 | 67 | BLOCK_OBJS+=block-cow.o block-qcow.o aes.o block-vmdk.o block-cloop.o |
68 | 68 | BLOCK_OBJS+=block-dmg.o block-bochs.o block-vpc.o block-vvfat.o |
69 | 69 | BLOCK_OBJS+=block-qcow2.o block-parallels.o block-nbd.o | ... | ... |
Makefile.target
... | ... | @@ -340,14 +340,13 @@ ifeq ($(TARGET_ARCH), m68k) |
340 | 340 | OBJS+= m68k-sim.o m68k-semi.o |
341 | 341 | endif |
342 | 342 | |
343 | -OBJS+= libqemu.a | |
344 | - | |
345 | 343 | # Note: this is a workaround. The real fix is to avoid compiling |
346 | 344 | # cpu_signal_handler() in cpu-exec.c. |
347 | 345 | signal.o: CFLAGS += $(HELPER_CFLAGS) |
348 | 346 | |
349 | -$(QEMU_PROG): $(OBJS) ../libqemu_user.a | |
350 | - $(LINK) | |
347 | +$(QEMU_PROG): ARLIBS=../libqemu_user.a libqemu.a | |
348 | +$(QEMU_PROG): $(OBJS) ../libqemu_user.a libqemu.a | |
349 | + $(call LINK,$(OBJS)) | |
351 | 350 | ifeq ($(ARCH),alpha) |
352 | 351 | # Mark as 32 bit binary, i. e. it will be mapped into the low 31 bit of |
353 | 352 | # the address space (31 bit so sign extending doesn't matter) |
... | ... | @@ -372,14 +371,13 @@ LIBS+=-lmx |
372 | 371 | OBJS= main.o commpage.o machload.o mmap.o signal.o syscall.o thunk.o \ |
373 | 372 | gdbstub.o gdbstub-xml.o |
374 | 373 | |
375 | -OBJS+= libqemu.a | |
376 | - | |
377 | 374 | # Note: this is a workaround. The real fix is to avoid compiling |
378 | 375 | # cpu_signal_handler() in cpu-exec.c. |
379 | 376 | signal.o: CFLAGS += $(HELPER_CFLAGS) |
380 | 377 | |
381 | -$(QEMU_PROG): $(OBJS) | |
382 | - $(LINK) | |
378 | +$(QEMU_PROG): ARLIBS=libqemu.a | |
379 | +$(QEMU_PROG): $(OBJS) libqemu.a | |
380 | + $(call LINK,$(OBJS)) | |
383 | 381 | |
384 | 382 | endif #CONFIG_DARWIN_USER |
385 | 383 | |
... | ... | @@ -473,14 +471,13 @@ OBJS= main.o bsdload.o elfload.o mmap.o path.o signal.o strace.o syscall.o \ |
473 | 471 | gdbstub.o gdbstub-xml.o |
474 | 472 | OBJS+= uaccess.o |
475 | 473 | |
476 | -OBJS+= libqemu.a | |
477 | - | |
478 | 474 | # Note: this is a workaround. The real fix is to avoid compiling |
479 | 475 | # cpu_signal_handler() in cpu-exec.c. |
480 | 476 | signal.o: CFLAGS += $(HELPER_CFLAGS) |
481 | 477 | |
482 | -$(QEMU_PROG): $(OBJS) ../libqemu_user.a | |
483 | - $(LINK) | |
478 | +$(QEMU_PROG): ARLIBS=libqemu.a ../libqemu_user.a | |
479 | +$(QEMU_PROG): $(OBJS) libqemu.a ../libqemu_user.a | |
480 | + $(call LINK,$(OBJS)) | |
484 | 481 | |
485 | 482 | endif #CONFIG_BSD_USER |
486 | 483 | |
... | ... | @@ -716,9 +713,9 @@ endif |
716 | 713 | vl.o: qemu-options.h |
717 | 714 | |
718 | 715 | $(QEMU_PROG): LIBS += $(SDL_LIBS) $(COCOA_LIBS) $(CURSES_LIBS) $(BRLAPI_LIBS) $(VDE_LIBS) |
719 | - | |
716 | +$(QEMU_PROG): ARLIBS=../libqemu_common.a libqemu.a | |
720 | 717 | $(QEMU_PROG): $(OBJS) ../libqemu_common.a libqemu.a |
721 | - $(LINK) | |
718 | + $(call LINK,$(OBJS)) | |
722 | 719 | |
723 | 720 | endif # !CONFIG_USER_ONLY |
724 | 721 | ... | ... |
module.c
0 → 100644
1 | +/* | |
2 | + * QEMU Module Infrastructure | |
3 | + * | |
4 | + * Copyright IBM, Corp. 2009 | |
5 | + * | |
6 | + * Authors: | |
7 | + * Anthony Liguori <aliguori@us.ibm.com> | |
8 | + * | |
9 | + * This work is licensed under the terms of the GNU GPL, version 2. See | |
10 | + * the COPYING file in the top-level directory. | |
11 | + * | |
12 | + */ | |
13 | + | |
14 | +#include "qemu-common.h" | |
15 | +#include "sys-queue.h" | |
16 | +#include "module.h" | |
17 | + | |
18 | +typedef struct ModuleEntry | |
19 | +{ | |
20 | + module_init_type type; | |
21 | + void (*init)(void); | |
22 | + TAILQ_ENTRY(ModuleEntry) node; | |
23 | +} ModuleEntry; | |
24 | + | |
25 | +typedef struct ModuleTypeList | |
26 | +{ | |
27 | + module_init_type type; | |
28 | + TAILQ_HEAD(, ModuleEntry) entry_list; | |
29 | + TAILQ_ENTRY(ModuleTypeList) node; | |
30 | +} ModuleTypeList; | |
31 | + | |
32 | +static TAILQ_HEAD(, ModuleTypeList) init_type_list; | |
33 | + | |
34 | +static ModuleTypeList *find_type_or_alloc(module_init_type type, int alloc) | |
35 | +{ | |
36 | + ModuleTypeList *n; | |
37 | + | |
38 | + TAILQ_FOREACH(n, &init_type_list, node) { | |
39 | + if (type >= n->type) | |
40 | + break; | |
41 | + } | |
42 | + | |
43 | + if (!n || n->type != type) { | |
44 | + ModuleTypeList *o; | |
45 | + | |
46 | + if (!alloc) | |
47 | + return NULL; | |
48 | + | |
49 | + o = qemu_mallocz(sizeof(*o)); | |
50 | + o->type = type; | |
51 | + TAILQ_INIT(&o->entry_list); | |
52 | + | |
53 | + if (n) { | |
54 | + TAILQ_INSERT_AFTER(&init_type_list, n, o, node); | |
55 | + } else { | |
56 | + TAILQ_INSERT_HEAD(&init_type_list, o, node); | |
57 | + } | |
58 | + | |
59 | + n = o; | |
60 | + } | |
61 | + | |
62 | + return n; | |
63 | +} | |
64 | + | |
65 | +void register_module_init(void (*fn)(void), module_init_type type) | |
66 | +{ | |
67 | + ModuleEntry *e; | |
68 | + ModuleTypeList *l; | |
69 | + | |
70 | + e = qemu_mallocz(sizeof(*e)); | |
71 | + e->init = fn; | |
72 | + | |
73 | + l = find_type_or_alloc(type, 1); | |
74 | + | |
75 | + TAILQ_INSERT_TAIL(&l->entry_list, e, node); | |
76 | +} | |
77 | + | |
78 | +void module_call_init(module_init_type type) | |
79 | +{ | |
80 | + ModuleTypeList *l; | |
81 | + ModuleEntry *e; | |
82 | + | |
83 | + l = find_type_or_alloc(type, 0); | |
84 | + if (!l) { | |
85 | + return; | |
86 | + } | |
87 | + | |
88 | + TAILQ_FOREACH(e, &l->entry_list, node) { | |
89 | + e->init(); | |
90 | + } | |
91 | +} | ... | ... |
module.h
0 → 100644
1 | +/* | |
2 | + * QEMU Module Infrastructure | |
3 | + * | |
4 | + * Copyright IBM, Corp. 2009 | |
5 | + * | |
6 | + * Authors: | |
7 | + * Anthony Liguori <aliguori@us.ibm.com> | |
8 | + * | |
9 | + * This work is licensed under the terms of the GNU GPL, version 2. See | |
10 | + * the COPYING file in the top-level directory. | |
11 | + * | |
12 | + */ | |
13 | + | |
14 | +#ifndef QEMU_MODULE_H | |
15 | +#define QEMU_MODULE_H | |
16 | + | |
17 | +/* This should not be used directly. Use block_init etc. instead. */ | |
18 | +#define module_init(function, type) \ | |
19 | +static void __attribute__((constructor)) do_qemu_init_ ## function(void) { \ | |
20 | + register_module_init(function, type); \ | |
21 | +} | |
22 | + | |
23 | +typedef enum { | |
24 | + MODULE_INIT_BLOCK, | |
25 | + MODULE_INIT_DEVICE | |
26 | +} module_init_type; | |
27 | + | |
28 | +#define block_init(function) module_init(function, MODULE_INIT_BLOCK) | |
29 | +#define device_init(function) module_init(function, MODULE_INIT_DEVICE) | |
30 | + | |
31 | +void register_module_init(void (*fn)(void), module_init_type type); | |
32 | + | |
33 | +void module_call_init(module_init_type type); | |
34 | + | |
35 | +#endif | ... | ... |
qemu-common.h
rules.mak
... | ... | @@ -8,10 +8,13 @@ |
8 | 8 | %.o: %.m |
9 | 9 | $(call quiet-command,$(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $<," OBJC $(TARGET_DIR)$@") |
10 | 10 | |
11 | -LINK = $(call quiet-command,$(CC) $(LDFLAGS) -o $@ $^ $(LIBS)," LINK $(TARGET_DIR)$@") | |
11 | +WAS=-Wl,--whole-archive | |
12 | +WAE=-Wl,--no-whole-archive | |
13 | + | |
14 | +LINK = $(call quiet-command,$(CC) $(LDFLAGS) -o $@ $(1) $(LIBS) $(WAS) $(ARLIBS) $(WAE)," LINK $(TARGET_DIR)$@") | |
12 | 15 | |
13 | 16 | %$(EXESUF): %.o |
14 | - $(LINK) | |
17 | + $(call LINK,$^) | |
15 | 18 | |
16 | 19 | %.a: |
17 | 20 | $(call quiet-command,rm -f $@ && $(AR) rcs $@ $^," AR $(TARGET_DIR)$@") | ... | ... |