From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1CpySy-0004fP-Hl for qemu-devel@nongnu.org; Sat, 15 Jan 2005 19:40:28 -0500 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1CpySt-0004eo-NE for qemu-devel@nongnu.org; Sat, 15 Jan 2005 19:40:27 -0500 Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1CpyOM-0002nw-Qz for qemu-devel@nongnu.org; Sat, 15 Jan 2005 19:35:42 -0500 Received: from [64.233.184.207] (helo=wproxy.gmail.com) by monty-python.gnu.org with esmtp (Exim 4.34) id 1Cpxhe-0004Tg-Tn for qemu-devel@nongnu.org; Sat, 15 Jan 2005 18:51:35 -0500 Received: by wproxy.gmail.com with SMTP id 40so999023wri for ; Sat, 15 Jan 2005 15:51:34 -0800 (PST) Message-ID: Date: Sun, 16 Jan 2005 00:51:34 +0100 From: Magnus Damm Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_Part_236_28613074.1105833094185" Subject: [Qemu-devel] [PATCH][RFC] system emulator modularization Reply-To: Magnus Damm , qemu-devel@nongnu.org List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org ------=_Part_236_28613074.1105833094185 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Content-Disposition: inline Heya, I have recently been scratching one of my favourite itches: modularization. QEMU has some well defined interfaces, but one of the bottlenecks when it comes to add and test new code is IMHO the command line options. The current way to do it is pretty straight forward - just add your new command line options to vl.c and off you go. However, this leads to some kind of option space pollution. With other words, I do not think the current solution scales wery well and it would be a shame to make it harder to add code to QEMU than it has to be. So, what I would like to add to QEMU is some kind of module support. My idea consists of two basic building blocks: modules and labels. A module is some kind of well defined function of the emulator, maybe code that emulates one chip, or perhaps some core emulator code. You could call it a class. A label is a named instance of a module. Each label is associated with a private data area that contains all state about the label. Pretty much an object. So, to be able to use any module you have to create an instance of it by creating a label. When a label is created it is possible to pass parameters to the instance. Each module has a set of operations, I suggest the following: check(): Called when the label is created. Used to validate module parameters. Should not allocate any data, private data belonging to label is already allocated by global module code. Must make sure that the module parameters are saved in a standardized endian-independent format - I suggest network byte order. start(): Called when the label is enabled. The private data is still in network byte order, now it is time for the module to convert the data to native byte order, allocate data and register itself or whatever. This start operation is used both for restore from a saved state and for a clean start. stop(): Called when the label is disabled, ie when the emulator is shut down or when state is saved. The module should unregister itself and convert the private data into network byte order. After stop() it should be possible to use the same private data and perform start(). >>From the command line it should be possible to create/change labels and to list availible modules. The attached patch lists modules after the help text, and the temporary option -x is currently used to create a label. From the help text: -x label=module[:args] create one instance of module called label The grand plan (not finished yet if ever) is that labels should depend on each other and something like this could be used to start the emulator with 2 vga cards and one nic on the pci bus: -x vga0=cirrus-vga -x vga1=std-vga -x nic0=ne2000:mac=0x0080deadbe -x pcibus0=pcibus:vga0,vga1,nic0 -x cpu0=x86:nommx -x mypc=pc:cpu0,pcibus0 This idea together with predefined profiles (collection of predefined labels) and that it should be possible to override predefined labels with user-defined ones would make QEMU pretty flexible IMHO. The attached patch does not do anything useful, it just contains some module code and a module skeleton. The code is pretty much a big hack, but I included it just to let you see. The module code is "inspired" from the Linux kernel, but improved to support instances. Module information is stored in a special section, just link in the code between module.o and module_last.o and everything should work by itself. / magnus ------=_Part_236_28613074.1105833094185 Content-Type: text/x-patch; name="qemu-cvs_20050115-modules.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="qemu-cvs_20050115-modules.patch" diff -urN qemu/Makefile.target qemu-modules/Makefile.target --- qemu/Makefile.target=092005-01-15 17:53:22.000000000 +0100 +++ qemu-modules/Makefile.target=092005-01-15 21:48:22.603025920 +0100 @@ -290,6 +290,7 @@ # must use static linking to avoid leaving stuff in virtual address space VL_OBJS=3Dvl.o osdep.o block.o readline.o monitor.o pci.o console.o=20 VL_OBJS+=3Dblock-cow.o block-qcow.o aes.o block-vmdk.o block-cloop.o block= -dmg.o +VL_OBJS+=3Dmodule.o module_skeleton.o module_last.o =20 SOUND_HW =3D sb16.o AUDIODRV =3D audio.o noaudio.o wavaudio.o diff -urN qemu/module.c qemu-modules/module.c --- qemu/module.c=091970-01-01 01:00:00.000000000 +0100 +++ qemu-modules/module.c=092005-01-15 23:10:31.273754080 +0100 @@ -0,0 +1,178 @@ +#include +#include +#include +#include + +#include "module.h" + +static void *__param_first __param_section =3D NULL; + +void module_list_all(void) +{ + void *first_module =3D &__param_first + 1; + + struct module *curr; + + for (curr =3D first_module; curr->name; curr++) { + + printf("%-15s %s\n", curr->name, curr->descr);=20 + } +} + +static struct module *module_find(char *name) +{ + void *first_module =3D &__param_first + 1; + + struct module *curr; + + for (curr =3D first_module; curr->name; curr++) { + +=09if (strcmp(name, curr->name) =3D=3D 0) { +=09=09return curr; +=09} + } + + return NULL; +} + +struct label { + struct label *next; + + char *name; + char *args; + struct module *module; + void *private; +}; + +struct label *module_label_first =3D NULL; + +static struct label *module_label_find(char *name) +{ + struct label *x; + + for (x =3D module_label_first; x; x =3D x->next) { + if (strcmp(x->name, name) =3D=3D 0) { + return x; +=09} + } + + return NULL; +} + +void module_label_remove(const char *name) +{ + struct label *x; + struct label *p =3D NULL; + + for (x =3D module_label_first; x; x =3D x->next) { + if (strcmp(x->name, name) =3D=3D 0) { + if (p) { + p->next =3D x->next; +=09 } + else { + module_label_first =3D x->next; +=09 } + +=09 free(x->name); +=09 free(x); + return; +=09} + + p =3D x; + } +} + + +int module_label_create(const char *str) +{ + char *s; + struct module *module; + struct label *label; + char *m, *a; + + s =3D strdup(str); + + if (!s) { +=09goto bad; + } + + a =3D index(s, ':'); + if (a) { +=09*a =3D '\0'; + a++; + } + else { + a =3D ""; + } + + m =3D index(s, '=3D'); + if (m) { +=09*m =3D '\0'; + } + else { +=09goto bad1; + } + m++; + + module_label_remove(s); + + module =3D module_find(m); + + if (!module) { +=09goto bad1; + } + + label =3D malloc(sizeof(*label) + module->size_of_priv); + + if (!label) { +=09goto bad1; + } + + label->name =3D s; + label->args =3D a; + label->module =3D module; + label->private =3D label + 1; + =20 + if (module->ops->check(label->private) < 0) { +=09goto bad2; + } + + label->next =3D module_label_first; + module_label_first =3D label; + + return 0; + + bad2: + free(label); + + bad1: + free(s); + + bad: + return -1; +} + +void module_printf(void *priv, const char *format, ...) +{ + va_list args; + struct label *label =3D priv; + + label--; + + printf("%s[%s]:", label->module->name, label->name); + + va_start(args, format); + + vprintf(format, args); + + va_end(args); +} + +char *module_args(void *priv) +{ + struct label *label =3D priv; + + label--; + + return label->args; +} diff -urN qemu/module.h qemu-modules/module.h --- qemu/module.h=091970-01-01 01:00:00.000000000 +0100 +++ qemu-modules/module.h=092005-01-15 23:00:45.006880192 +0100 @@ -0,0 +1,78 @@ + +#ifndef __MODULE_H__ +#define __MODULE_H__ + +struct module_ops { + int (*check)(void *); + int (*start)(void *); + void (*stop)(void *); +}; + +#define MODULE_OP_TYPECHECK(func, ret_ptr_type, arg) \ +static inline ret_ptr_type(*MODULE_OP_TYPECHECK_##func(void))(arg) \ +{ return(func); } + +#define MODULE_OPS(check, start, stop, priv) \ + { (void *)check, (void *)start, (void *)stop }; \ + MODULE_OP_TYPECHECK(check, int, priv *); \ + MODULE_OP_TYPECHECK(start, int, priv *); \ + MODULE_OP_TYPECHECK(stop, void, priv *) + +struct module_param { + char *name; + char *type; + char *descr; + char *deflt; + unsigned int offset; +}; + +#define MODULE_PARAM_TYPECHECK(symbol, type) \ +static inline (type) MODULE_PARAM_TYPECHECK_##symbol(void) { return(symbol= ); } + +#define offset_of(type,member) ((unsigned int)(&((type *)0)->member)) + +#define MODULE_PARAM(name, type, descr, deflt, priv) \ + { #name, type, descr, #deflt, offset_of(priv, name) } + +struct module { + char *name; + char *type; + char *descr; + struct module_ops *ops; + struct module_param *params; + unsigned int no_params; + unsigned int size_of_priv; +}; + +#if __GNUC__ > 3 +#define __attribute__used__ __attribute__((__used__)) +#elif __GNUC__ =3D=3D 3 +#if __GNUC_MINOR__ > 3 +#define __attribute__used__ __attribute__((__used__)) +#else +#define __attribute__used__ __attribute__((__unused__)) +#endif +#elif __GNUC__ =3D=3D 2 +#define __attribute__used__ __attribute__((__unused__)) +#else +#define __attribute__used__ +#endif + +#define MODULE(name, type, descr, ops, params, priv) \ + static struct module const module \ + __attribute__used__ \ + __attribute__ ((unused,__section__ ("__param"),aligned(sizeof(void *))))= \ + =3D { \ + #name, type, descr, &ops, params, \ + (sizeof(params) / sizeof(struct module_param)), sizeof(priv) \ + } + +#define __param_section __attribute__ ((unused,__section__ ("__param"),ali= gned(sizeof(void *)))) + +void module_printf(void *priv, const char *format, ...); +char *module_args(void *priv); + +void module_list_all(void); +int module_label_create(const char *str); + +#endif /* __MODULE_H__ */ diff -urN qemu/module_last.c qemu-modules/module_last.c --- qemu/module_last.c=091970-01-01 01:00:00.000000000 +0100 +++ qemu-modules/module_last.c=092005-01-15 21:25:16.484747928 +0100 @@ -0,0 +1,7 @@ +#include + +#include "module.h" + +/* the object file generated from this file must be placed at the end */ + +static void *__param_last __param_section =3D NULL; diff -urN qemu/module_skeleton.c qemu-modules/module_skeleton.c --- qemu/module_skeleton.c=091970-01-01 01:00:00.000000000 +0100 +++ qemu-modules/module_skeleton.c=092005-01-15 23:02:36.227972016 +0100 @@ -0,0 +1,33 @@ +#include "module.h" + +struct private { + unsigned int foo; + char buf[100]; + int my_bool; +}; + +static int check(struct private *p) +{ + module_printf(p, " check! my_bool =3D %d, args are \"%s\"\n",=20 +=09=09=09=09p->my_bool, module_args(p)); + + return 0; +} + +static int start(struct private *p) +{ + return 0; +} + +static void stop(struct private *p) +{ +} + +static struct module_ops ops =3D MODULE_OPS(check, start, stop, struct pri= vate); + +static struct module_param params[] =3D { + MODULE_PARAM(my_bool, "bool", "a bool parameter called my_bool",=20 +=09 xx, struct private), +}; + +MODULE(skel, "", "a simple module skeleton", ops, params, struct private); diff -urN qemu/vl.c qemu-modules/vl.c --- qemu/vl.c=092005-01-16 00:27:05.974253432 +0100 +++ qemu-modules/vl.c=092005-01-15 23:24:54.956454352 +0100 @@ -76,6 +76,8 @@ =20 #include "exec-all.h" =20 +#include "module.h" + //#define DO_TB_FLUSH =20 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup" @@ -2769,6 +2771,7 @@ " (default is CL-GD5446 PCI VGA)\n" #endif "-loadvm file start right away with a saved state (loadvm in= monitor)\n" + "-x label=3Dmodule[:args] create one instance of module called= label\n" "\n" "During emulation, the following keys are useful:\n" "ctrl-alt-f toggle full screen\n" @@ -2792,6 +2795,11 @@ "work. Please use the 'qemu' executable to have a more accurate= (but slower)\n" "PC emulation.\n"); #endif + +=09printf("\nAvailable modules:\n"); +=09module_list_all(); + + exit(1); } =20 @@ -2848,6 +2856,7 @@ QEMU_OPTION_loadvm, QEMU_OPTION_full_screen, QEMU_OPTION_pidfile, +=09QEMU_OPTION_set_module, }; =20 typedef struct QEMUOption { @@ -2915,6 +2924,7 @@ /* temporary options */ { "pci", 0, QEMU_OPTION_pci }, { "cirrusvga", 0, QEMU_OPTION_cirrusvga }, + { "x", HAS_ARG, QEMU_OPTION_set_module }, { NULL }, }; =20 @@ -3328,6 +3338,12 @@ graphic_depth =3D depth; } break; + case QEMU_OPTION_set_module: +=09=09=09 if (module_label_create(optarg) < 0) { + fprintf(stderr, "qemu: unable to create label \"%s\"\n= ", optarg); + exit(1); +=09=09=09 } + break; case QEMU_OPTION_monitor: pstrcpy(monitor_device, sizeof(monitor_device), optarg); break; ------=_Part_236_28613074.1105833094185--