Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1L5rdn-0000In-DK for qemu-devel@nongnu.org; Thu, 27 Nov 2008 19:55:27 -0500 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1L5rdl-0000Ia-GP for qemu-devel@nongnu.org; Thu, 27 Nov 2008 19:55:26 -0500 Received: from [199.232.76.173] (port=41271 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1L5rdl-0000IX-AG for qemu-devel@nongnu.org; Thu, 27 Nov 2008 19:55:25 -0500 Received: from web27206.mail.ukl.yahoo.com ([217.146.182.96]:31355) by monty-python.gnu.org with smtp (Exim 4.60) (envelope-from ) id 1L5rdk-0008Na-Kb for qemu-devel@nongnu.org; Thu, 27 Nov 2008 19:55:25 -0500 Date: Fri, 28 Nov 2008 00:55:22 +0000 (GMT) From: Salvatore Lionetti MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Message-ID: <833623.60336.qm@web27206.mail.ukl.yahoo.com> Subject: [Qemu-devel] [CONFIGURATION METHOD] A proposal Reply-To: salvatorelionetti@yahoo.it, 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 Hi, i'm working a little on ppc405 emulation and. Instead of - overload method with many parameter, - putting interface definition in the same implementation module, - duplicate lot of code between similar board, i've elaborated a first draft, compiled and working, of configuration compo= nent: see the comment on the head of file. Thanks for eventually feedback (information, comment, proposal) 1,528d0 < /* Configuration object < * =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D < *=20 < * Every object: < * - is identified by a string (es '/walnut/cpu/mal/txeobisr') < * - is tagged with a type (es 'INT' 'STRING' 'PTR' 'FILE' 'CALLBACK') < * - have a value, which can be {set,get}ted. < * Es "/walnut/cpu/mal/txeobisr INT 0" < * < * Collection of objects is readed from a char* variable,=20 < * so can be acquired both from configuration file and code. < * < * Every sw entity can intercept a value set over an object. < * Co-relation take place in the context of set operation. < * < * As special case, sharing a pointer ex toward a variable, < * no code change is required after registering it. < * < * Ascii representation has some constrain: < * - start a line with a number of space equal to level number, < * - every object name have to start with '/'. < * - multivalue have to follow strictly this format: last/{a INT 0, b INT= 1, ...} < *=20 < * Some example: < # Some comment < /cpu < # That can be inserted < /emac < # At any level < /txeobisr INT 0 < /iic/eeprom/{address INT 0x56, image FILE pid.bin} < /serial < /serial0/{base PTR 0x300, intr INT 0} < /serial1/{base PTR 0x400, intr INT 1, filename STRING "tcp:..."} < * < * Common interface or inheritance relation should be supported < * Not in this release < * Some example: < * < eeprom{address INT, image FILE} < =20 < /cpu_meta < /emac < /txeobisr INT 0 < /iic/eeprom <=20 < /cpu0(cpu_meta) < /cpu1(cpu_meta) < * < * Configuration is totally dinamic so assertion about missing definition= of an object (es no value during get()) < * take place only when needed, like pure virtual function call in C++. < */ <=20 < #include < #define TEST < #ifndef TEST < #include "qemu-common.h" < #include "hw.h" < #else < #define qemu_malloc malloc=20 < #include < #include < #include < #include < #include < /* Configuration object is a 3-ple: < * "/name/of/object", type, value < * Every function return <0 on error. < */ < enum hw_conf_basicT { < =09HW_CONF_NOTYPE, < =09HW_CONF_INT, < =09HW_CONF_STRING, < =09HW_CONF_PTR, < =09HW_CONF_FUNC, < }; < #define HW_CONF_GET(name, val) hw_conf_get(name, (void**)&val) < #define HW_CONF_SET(name, val) hw_conf_set(name, (void*)val) < /* 0 let change happen, 1 deny current change (a voting alg) */ < typedef int (HWConf_corelateFunc)(void *opaque, const char* name, void* n= ewv); <=20 < /* Return number of object successfully parsed. Value is only an opaque o= f type void* */ < int hw_conf_parsefrom(const char* str, int sz); < int hw_conf_get(const char* name, void** val); < int hw_conf_set(const char* name, void* val); < void hw_conf_corelatewith(const char* str, HWConf_corelateFunc f, void* o= paque); < #endif < struct hw_conf_corelateT { < =09HWConf_corelateFunc* f; < =09void* opaque; < }; < struct hw_conf_nodeT { < =09/* Three related */ < =09struct hw_conf_nodeT* nextH; < =09struct hw_conf_nodeT* nextV; < =09struct hw_conf_nodeT* prevV; <=20 < =09char* id; < =09/* Data contents */ < =09enum hw_conf_basicT type; < =09void* opaque; <=20 < =09/* Communique with the world */ < =09struct hw_conf_corelateT core[10]; < }; <=20 < static struct hw_conf_nodeT hw_conf_root; < static int hw_conf_haveroot =3D 0; <=20 < static void hw_conf_nodeinit(struct hw_conf_nodeT* n) { < =09int l; <=20 < =09assert(n); < =09n->nextH =3D n->nextV =3D n->prevV =3D NULL; < =09n->id =3D NULL; < =09for (l=3D0; lcore)/sizeof(n->core[0]); l++) { < =09=09n->core[l].f =3D NULL; < =09=09n->core[l].opaque =3D NULL; < =09} < } <=20 < static void hw_conf_skipword(const char** s0, int n) { < =09const char* s =3D *s0; < =09while (*s && (s-*s0)=3D'0' && *s<=3D'9') || (*s>=3D'a' && *s<=3D'z') || (*s>= =3D'A' && *s<=3D'Z')/*|| *s=3D=3D'\'' || *s=3D=3D'\"'*/)) { < =09=09=09break; < =09=09} < =09=09s++; < =09} < =09*s0 =3D s; < } <=20 < /* Return 0 =3D> root */ < static int hw_conf_getLevel(struct hw_conf_nodeT* n) { < =09int ret =3D 0; < =09while (n->prevV !=3D &hw_conf_root) { < =09=09n =3D n->prevV; < =09=09ret++; < =09} < =09return ret; < } < static struct hw_conf_nodeT* hw_conf_find(struct hw_conf_nodeT* n0, const= char** id, int create) { < =09char haveMultiValue; < =09struct hw_conf_nodeT* n, *nKm1; < =09struct hw_conf_nodeT* ret =3D NULL; < =09if (n0 =3D=3D NULL || id =3D=3D NULL || *id=3D=3DNULL) < =09=09return ret; <=20 < =09(*id)++; < =09if ((*id)[0]=3D=3D'{') < =09=09(*id)++; < =09/* Root initialize */ < =09if (hw_conf_haveroot =3D=3D 0) { < =09=09hw_conf_nodeinit(&hw_conf_root); < =09=09hw_conf_haveroot =3D 1; < =09} < =09 < =09haveMultiValue =3D 0; < =09/* Down one level, then loop trow node */ < =09nKm1 =3D n =3D n0->nextV; < =09while (n && !ret) { < =09=09if (strstr(*id, n->id)) { < =09=09=09*id+=3Dstrlen(n->id); < =09=09=09switch (*id[0]) { < =09=09=09=09case '/': < =09=09=09=09=09ret =3D hw_conf_find(n, id, create); < =09=09=09=09=09break; < =09=09=09=09case '\x0': < =09=09=09=09=09ret =3D n; < =09=09=09=09=09break; < =09=09=09=09default: < =09=09=09=09=09break; < =09=09=09} < =09=09} < =09=09nKm1 =3D n; < =09=09n =3D n->nextH; < =09} < =09if (!ret && create) { < =09=09struct hw_conf_nodeT* newn =3D (struct hw_conf_nodeT*) qemu_malloc(= sizeof(struct hw_conf_nodeT)); < =09=09const char* eon; < =09=09int len; <=20 < =09=09eon =3D *id; < =09=09hw_conf_skipword(&eon, 255); <=20 < =09=09len =3D eon-*id; < =09=09hw_conf_nodeinit(newn); < =09=09newn->id =3D qemu_malloc(len+1); < =09=09memcpy(newn->id, *id, len); < =09=09newn->id[len] =3D '\x0'; <=20 < =09=09/*=20 < =09=09 * on new node: < =09=09 * nextH: NULL (we add to the tail) < =09=09 * nextV: NULL (no child when create) < =09=09 * prevV: n0 (always have a node of level-1) < =09=09 */ < =09=09newn->prevV =3D n0; < =09=09if (n0->nextV) { < =09=09=09/* Link to last in this level */ < =09=09=09assert(nKm1->nextH=3D=3DNULL); < =09=09=09nKm1->nextH =3D newn; < =09=09} else { < =09=09=09/* First item on this level */ < =09=09=09n0->nextV =3D newn; < =09=09} <=20 < =09=09*id=3Deon; < =09=09ret =3D *eon!=3D'/' ? newn : hw_conf_find(newn, id, create); < =09} < =09if (ret) { < =09=09switch (ret->type) { < =09=09=09case HW_CONF_INT: < =09=09=09case HW_CONF_STRING: < =09=09=09case HW_CONF_PTR: < =09=09=09case HW_CONF_FUNC: < =09=09=09case HW_CONF_NOTYPE: < =09=09=09=09break; < =09=09=09default: < =09=09=09=09assert("Find a node with unmanaged type!"); < =09=09=09=09break; < =09=09} < =09} < =09return ret; < } <=20 < char* hw_conf_getTypeDes(enum hw_conf_basicT t) { < =09char* ret =3D NULL; < =09switch (t) { < =09=09case HW_CONF_INT:=09ret =3D "INT"; break; < =09=09case HW_CONF_STRING:=09ret =3D "STRING"; break; < =09=09case HW_CONF_PTR:=09ret =3D "PTR"; break; < =09=09case HW_CONF_FUNC:=09ret =3D "FUNC"; break; < =09=09case HW_CONF_NOTYPE:=09ret =3D "NOTYPE"; break; < =09=09default:=09=09ret =3D "UNKTYPE"; break; < =09} < =09return ret; < } <=20 < enum hw_conf_basicT hw_conf_parseType(const char** s, int n) { < =09enum hw_conf_basicT ret =3D HW_CONF_NOTYPE; < =09if (s=3D=3DNULL || *s=3D=3DNULL || n=3D=3D0) < =09=09return ret; <=20 < =09 if (strncmp(*s, "INT ", n>4?4:n)=3D=3D0) { < =09=09(*s)+=3D4; < =09=09ret =3D HW_CONF_INT; < =09} else if (strncmp(*s, "STRING ", n>7?7:n)=3D=3D0) { < =09=09(*s)+=3D7; < =09=09ret =3D HW_CONF_STRING; < =09} else if (strncmp(*s, "PTR ", n>4?4:n)=3D=3D0) { < =09=09(*s)+=3D4; < =09=09ret =3D HW_CONF_PTR; < =09} else if (strncmp(*s, "FUNC ", n>5?5:n)=3D=3D0) { < =09=09(*s)+=3D5; < =09=09ret =3D HW_CONF_FUNC; < =09} < =09return ret; < } < int hw_conf_parseValue(const char** s0, int n, enum hw_conf_basicT t, voi= d** o) { < =09int ret=3D0; < =09if (s0 && *s0 && n) { < =09=09const char* s =3D *s0; < =09=09switch (t) { < =09=09=09case HW_CONF_INT: < =09=09=09case HW_CONF_PTR: < =09=09=09=09hw_conf_skipword(&s, n); < =09=09=09=09if (sscanf(*s0, "0x%x", (int*)o)=3D=3D1 || sscanf(*s0, "%d", = (int*)o)=3D=3D1) < =09=09=09=09=09ret =3D 1; < =09=09=09=09break; < =09=09=09case HW_CONF_STRING: < =09=09=09case HW_CONF_FUNC: < =09=09=09{ < =09=09=09=09if (**s0=3D=3D'\'' || **s0=3D=3D'\"') { < =09=09=09=09=09char* tmp; < =09=09=09=09=09s++; < =09=09=09=09=09while (n-(*s0-s)>0 && (*s!=3D'\'' && *s!=3D'\"')) { < =09=09=09=09=09=09s++; < =09=09=09=09=09} < =09=09=09=09=09tmp =3D qemu_malloc(s-*s0); < =09=09=09=09=09memcpy(tmp, *s0+1, s-*s0-1); < =09=09=09=09=09tmp[s-*s0-1] =3D '\x0'; < =09=09=09=09=09*o =3D (void*) tmp; < =09=09=09=09=09=09 < =09=09=09=09=09ret =3D 1; < =09=09=09=09} < =09=09=09=09break; < =09=09=09} < =09=09} < =09=09if (ret=3D=3D1) { < =09=09=09*s0 =3D s; < =09=09} < =09} < =09return ret; < } <=20 < /* Elaborate until eof | eos */ < int hw_conf_parsefrom(const char* name, int sz) { < =09enum {HW_CONF_READING_LEVEL, HW_CONF_READING_NAME, HW_CONF_READED_NAME= , HW_CONF_READING_TYPE, HW_CONF_READING_VALUE, HW_CONF_READING_DONE} state; < =09enum hw_conf_basicT t; < =09void *o; < =09char nlev, nlevKm1, comment; < =09int nobj; < =09const char* n =3D name; < =09struct hw_conf_nodeT* node; <=20 < =09printf("len %d\n", sz); < =09state =3D HW_CONF_READING_LEVEL; < =09t =3D HW_CONF_NOTYPE; < =09nlev =3D nlevKm1 =3D comment =3D 0; < =09nobj =3D 0; < =09if (name=3D=3DNULL) < =09=09return nobj; < =09if (sz =3D=3D 0) < =09=09sz =3D strlen(name); < =09while ((n-name)prevV; < =09=09=09=09=09n++; /* Skip ',' */ < =09=09=09=09} else { < =09=09=09=09=09break; < =09=09=09=09} < =09=09=09case '/': < =09=09=09=09if (state <=3D HW_CONF_READING_NAME) { < =09=09=09=09=09if (nlev>nlevKm1) { < =09=09=09=09=09=09assert((nlev - nlevKm1) =3D=3D 1); < =09=09=09=09=09} < =09=09=09=09=09while (nlev && nlev<=3DnlevKm1) { < =09=09=09=09=09=09node =3D node->prevV; < =09=09=09=09=09=09nlevKm1--; < =09=09=09=09=09} < =09=09=09=09=09if (nlev =3D=3D 0) { < =09=09=09=09=09=09node =3D &hw_conf_root; < =09=09=09=09=09} < =09=09=09=09=09node =3D hw_conf_find(node, &n, 1); < =09=09=09=09=09nlev =3D hw_conf_getLevel(node); < =09=09=09=09=09state=3DHW_CONF_READED_NAME; < =09=09=09=09=09n--; < =09=09=09=09} < =09=09=09=09break; < =09=09=09case '}': < =09=09=09=09nlev--; < =09=09=09=09node =3D node->prevV; < =09=09=09=09break; < =09=09=09case 0xa: < =09=09=09=09if (comment) { < =09=09=09=09=09nlev =3D 0; < =09=09=09=09} < =09=09=09=09comment =3D 0; < =09=09=09=09if (state !=3D HW_CONF_READING_LEVEL) { < =09=09=09=09=09state =3D HW_CONF_READING_LEVEL; < =09=09=09=09=09nlevKm1 =3D nlev; < =09=09=09=09=09nlev =3D 0; < =09=09=09=09} < =09=09=09=09break; < =09=09=09case '#': < =09=09=09=09comment =3D 1; < =09=09=09=09break; < =09=09=09default: < =09=09=09=09switch (state) { < =09=09=09=09=09case HW_CONF_READING_TYPE: < =09=09=09=09=09=09if ((t =3D hw_conf_parseType(&n, sz-(n-name)))!=3DHW_CO= NF_NOTYPE) { < =09=09=09=09=09=09=09state =3D HW_CONF_READING_VALUE; < =09=09=09=09=09=09=09n--; < =09=09=09=09=09=09} < =09=09=09=09=09=09break; < =09=09=09=09=09case HW_CONF_READING_VALUE: < =09=09=09=09=09=09if (hw_conf_parseValue(&n, sz-(n-name), t, &o)) { < =09=09=09=09=09=09=09node->type =3D t; < =09=09=09=09=09=09=09node->opaque =3D o; < =09=09=09=09=09=09=09state =3D HW_CONF_READING_DONE; < =09=09=09=09=09=09=09nobj++; < =09=09=09=09=09=09=09n--; < =09=09=09=09=09=09} < =09=09=09=09=09default: < =09=09=09=09=09=09break; < =09=09=09=09} < =09=09=09=09break; < =09=09} /* switch */ < =09=09} /* if */ < =09=09n++; <=20 < =09} < =09return nobj; < } < int hw_conf_get(const char* name, void** val) { < =09struct hw_conf_nodeT *ret =3D hw_conf_find(&hw_conf_root, &name, 0); < =09if (ret) < =09=09*val =3D ret->opaque; < =09return ret!=3DNULL; < } < int hw_conf_set(const char* name, void* val) { < =09const char* orign =3D name; < =09struct hw_conf_nodeT *ret =3D hw_conf_find(&hw_conf_root, &name, 0); < =09if (ret) { < =09=09/* Inform the world */ < =09=09int i, l; < =09=09l =3D 0; < =09=09for (i=3D0; icore)/sizeof(ret->core[0]); i++) < =09=09=09if (ret->core[i].f) < =09=09=09=09l +=3D ret->core[i].f(ret->core[i].opaque, orign, val); <=20 < =09=09if (l=3D=3D0) < =09=09=09ret->opaque =3D val; < =09} < =09return ret!=3DNULL; < } < void hw_conf_corelatewith(const char* name, HWConf_corelateFunc f, void* = opaque) { < =09struct hw_conf_nodeT *ret =3D hw_conf_find(&hw_conf_root, &name, 0); < =09if (ret) { < =09=09int i; < =09=09for (i=3D0; icore)/sizeof(ret->core[0]); i++) < =09=09=09if (ret->core[i].f=3D=3DNULL && ret->core[i].opaque=3D=3DNULL) { < =09=09=09=09ret->core[i].f =3D f; < =09=09=09=09ret->core[i].opaque =3D opaque; < =09=09=09=09break; < =09=09=09} < =09=09if (i =3D=3D sizeof(ret->core)/sizeof(ret->core[0])) { < =09=09=09assert("Not enough space for store corelation information!"); < =09=09} < =09} else { < =09=09assert("Corelation with non existent object"); < =09} < } < /* scende & stampa con nextV < * sale con prevV e segue nextH < * sale con prevV < * fino ad n0 < */ < int hw_conf_printfrom(struct hw_conf_nodeT* n0) { < =09struct hw_conf_nodeT *n; < =09int nlev, nlevKm1, lastWasUp; < =09int nobj =3D 0; < =09if (n0=3D=3DNULL || n0->nextV=3D=3DNULL) < =09=09return nobj; < =09n =3D n0->nextV; < =09nlev =3D lastWasUp =3D 0; < =09while (n !=3D n0) { < =09=09if (!lastWasUp) { < =09=09int i; < =09=09for (i=3D0; iid); < =09=09if (n->type!=3DHW_CONF_NOTYPE) { < =09=09=09printf(" %s ", hw_conf_getTypeDes(n->type)); < =09=09=09if (n->type =3D=3D HW_CONF_STRING) { < =09=09=09=09printf("'%s'", (char*) n->opaque); < =09=09=09} else { < =09=09=09=09printf("0x%x", n->opaque); < =09=09=09} < =09=09} < =09=09printf("\n"); < =09=09} < =09=09if (n->nextV && !lastWasUp) { < =09=09=09nlev++; < =09=09=09n =3D n->nextV; < =09=09/* Are on leaf: 1) Try same level, then go up */ < =09=09} else { < =09=09=09if (n->nextH) { < =09=09=09=09n =3D n->nextH; < =09=09=09=09lastWasUp =3D 0; < =09=09=09} else { < =09=09=09=09nlev--; < =09=09=09=09n =3D n->prevV; < =09=09=09=09lastWasUp =3D 1; < =09=09=09} < =09=09} < =09} < =09return nobj; < } <=20 < char* open_mmap(char* fn, int* len) { < =09int fd; < =09char* ret =3D NULL; < =09fd =3D open(fn, O_RDONLY); < =09if (fd>0) { < =09=09*len =3D lseek(fd, 0, SEEK_END); < =09=09printf("File lungo %d bytes\n", *len); < =09=09ret=3Dmmap(NULL, *len, PROT_READ, MAP_SHARED, fd, 0); < =09=09if (ret=3D=3DMAP_FAILED) < =09=09=09ret =3D NULL; < =09} < =09return ret; < } < #ifdef TEST < int core(void *opaque, const char* name, void* newv) { < =09printf("Co-relation(this=3D0x%x) %s now %x\n", opaque, name, newv); < =09return 0; < } < int main(int argc, char* argv[]) { < =09char tmp[256]; < =09char* tmp1=3DNULL; < =09int len; < =09void* serial_base; < =09char* info =3D "\ < /cpu\n\ < /serial\n\ < /base PTR 0x500\n\ < /intr STRING '0'\n\ < /emac/txeobisr INT 0\n\ < /iic/eeprom\n\ < /address INT 0x56\n\ < /image STRING 'pid.bin'"; < =09len =3D 0; < =09tmp1 =3D open_mmap("hw_conf.txt", &len); < =09printf("parse =3D %d\n", hw_conf_parsefrom(tmp1, len)); < =09hw_conf_printfrom(&hw_conf_root); < =09hw_conf_get("/cpu/serial/base", &serial_base); < =09printf("get serial_base 0x%x\n", serial_base); < =09hw_conf_corelatewith("/cpu/serial/base", core, (void*)4); < =09serial_base++; < =09hw_conf_set("/cpu/serial/base", serial_base); < =09serial_base=3D0; < =09hw_conf_get("/cpu/serial/base", &serial_base); < =09printf("get serial_base 0x%x\n", serial_base); < } < #endif=0A=0A=0A