source: soft/giet_vm/xml/xml_parser.c @ 242

Last change on this file since 242 was 238, checked in by alain, 11 years ago

Major evolution to support physical addresses larger than 32 bits.
The map.xml format has been modified: the vsegs associated to schedulers
are now explicitely defined and mapped in the page tables.

File size: 70.3 KB
Line 
1///////////////////////////////////////////////////////////////////////////////////////
2// File     : xml_parser.c
3// Date     : 14/04/2012
4// Author   : alain greiner
5// Copyright (c) UPMC-LIP6
6///////////////////////////////////////////////////////////////////////////////////////
7// This program translate a "map.xml" source file to a binary file "map.bin" that
8// can be directly loaded in the boot ROM and used by the GIET-VM operating system.
9//
10// This map.xml file contains :
11// 1) the multi-cluster/multi-processors hardware architecture description
12// 2) the various multi-threaded software applications
13// 3) the mapping directives bor both the tasks and the virtual segments.
14// The corresponding C structures are defined in the "mapping_info.h" file.
15//
16// This program also generates the "hard_config.h" and the "giet_vsegs.ld" files,
17// required  to compile the GIET-VM code.
18///////////////////////////////////////////////////////////////////////////////////////
19
20#include  <stdlib.h>
21#include  <fcntl.h>
22#include  <sys/types.h>
23#include  <sys/stat.h>
24#include  <unistd.h>
25#include  <stdio.h>
26#include  <string.h>
27#include  <assert.h>
28#include  <libxml/xmlreader.h>
29#include  <mapping_info.h>
30#include  <irq_handler.h>
31
32#define MAX_CLUSTERS   1024
33#define MAX_PSEGS      4096
34#define MAX_VSPACES    1024
35#define MAX_TASKS      4096
36#define MAX_MWMRS      4096
37#define MAX_VSEGS      4096
38#define MAX_VOBJS      8192
39#define MAX_PROCS      1024
40#define MAX_IRQS       8192
41#define MAX_COPROCS    4096
42#define MAX_CP_PORTS   8192
43#define MAX_PERIPHS    8192
44
45#define XML_PARSER_DEBUG  0
46
47///////////////////////////////////////////////////////////////////////////////////
48//     global variables used to store and index the data structures
49///////////////////////////////////////////////////////////////////////////////////
50
51mapping_header_t *   header;
52mapping_cluster_t *  cluster[MAX_CLUSTERS];  // cluster array
53mapping_pseg_t *     pseg[MAX_PSEGS];        // pseg array
54mapping_vspace_t *   vspace[MAX_VSPACES];    // vspace array
55mapping_vseg_t *     vseg[MAX_VSEGS];        // vseg array
56mapping_vobj_t *     vobj[MAX_VOBJS];        // vobj array
57mapping_task_t *     task[MAX_TASKS];        // task array
58mapping_proc_t *     proc[MAX_PROCS];        // proc array
59mapping_irq_t *      irq[MAX_IRQS];          // irq array
60mapping_coproc_t *   coproc[MAX_COPROCS];    // coproc array
61mapping_cp_port_t *  cp_port[MAX_CP_PORTS];  // coproc port array
62mapping_periph_t *   periph[MAX_PERIPHS];    // peripheral array
63
64// Index for the various arrays
65
66unsigned int cluster_index  = 0;
67unsigned int vspace_index = 0;
68unsigned int global_index = 0;
69unsigned int pseg_index = 0;       
70
71unsigned int proc_index = 0;
72unsigned int proc_loc_index = 0;
73
74unsigned int irq_index = 0;
75unsigned int irq_loc_index  = 0;
76
77unsigned int coproc_index = 0;
78unsigned int coproc_loc_index = 0;
79
80unsigned int cp_port_index = 0;
81unsigned int cp_port_loc_index = 0;
82
83unsigned int periph_index = 0;
84unsigned int periph_loc_index = 0;
85
86unsigned int vseg_index = 0;
87unsigned int vseg_loc_index = 0;
88
89unsigned int task_index = 0;
90unsigned int task_loc_index = 0;
91
92unsigned int vobj_index = 0;
93unsigned int vobj_loc_index = 0;
94unsigned int vobj_count = 0;
95
96
97//////////////////////////////
98// for replicated peripheral
99//////////////////////////////
100char found_timer = 0;
101char found_icu = 0;
102char found_dma = 0;
103
104
105////////////////////////////////////////////////////////////////////
106// These variables are used to generate the hard_config.h file
107////////////////////////////////////////////////////////////////////
108
109unsigned int cluster_y        = 0; // number of clusters in a column
110unsigned int cluster_x        = 0; // number of clusters in a row
111unsigned int nb_proc_max      = 0; // max number of processors per cluster
112unsigned int nb_tasks_max     = 0; // max number of tasks (for all vspaces)
113
114unsigned int tim_channels     = 0; // max number of user timers per cluster
115unsigned int dma_channels     = 0; // max number of DMA channels per cluster
116
117unsigned int tty_channels     = 0; // total number of terminals in first TTY
118unsigned int ioc_channels     = 0; // total number of channels in first IOC
119unsigned int nic_channels     = 0; // total number of channels in first NIC
120unsigned int cma_channels     = 0; // total number of channels in first CMA
121
122unsigned int io_mmu_active    = 0; // using IOB component
123unsigned int use_xicu         = 0; // using XICU (not ICU)
124
125
126////////////////////////////////////////////////////////////////
127// These variables are used to generate the giet_vseg.ld file
128////////////////////////////////////////////////////////////////
129
130unsigned int periph_vbase_array[PERIPH_TYPE_MAX_VALUE] 
131         = { [0 ... (PERIPH_TYPE_MAX_VALUE - 1)] = 0xFFFFFFFF };
132
133//////////////////////////////////////////////////////////////////////
134// This arrray is useful to build a temporary list of vobj references.
135// The struct vobj_ref_s is formed by a vspace_name and a vobj_name.
136// This array is used to set the attribute vobj_id of a cp_port
137// once all the vspace have been parsed.
138/////////////////////////////////////////////////////////////////////
139typedef struct vobj_ref_s
140{
141    char vspace_name[32];
142    char vobj_name[32];
143} vobj_ref_t;
144
145vobj_ref_t * cp_port_vobj_ref[MAX_CP_PORTS];   
146
147
148//////////////////////////////////////////////////
149unsigned int getIntValue( xmlTextReaderPtr reader, 
150                          const char * attributeName, 
151                          unsigned int * ok) 
152{
153    unsigned int value = 0;
154    unsigned int i;
155    char c;
156
157    char * string = (char *) xmlTextReaderGetAttribute(reader, 
158                                (const xmlChar *) attributeName);
159
160    if (string == NULL) 
161    {
162        // missing argument
163        *ok = 0;
164        return 0;
165    }
166    else 
167    {
168        if ((string[0] == '0') && ((string[1] == 'x') || (string[1] == 'X'))) 
169        {
170            // Hexa
171            for (i = 2 ; (string[i] != 0) && (i < 10) ; i++) 
172            {
173                c = string[i];
174                if      ((c >= '0') && (c <= '9')) { value = (value << 4) + string[i] - 48; }
175                else if ((c >= 'a') && (c <= 'f')) { value = (value << 4) + string[i] - 87; }
176                else if ((c >= 'A') && (c <= 'F')) { value = (value << 4) + string[i] - 55; }
177                else 
178                {
179                    *ok = 0;
180                    return 0;
181                }
182            }
183        }
184        else 
185        {
186            // Decimal
187            for (i = 0; (string[i] != 0) && (i < 9); i++) 
188            {
189                c = string[i];
190                if ((c >= '0') && (c <= '9')) value = (value * 10) + string[i] - 48;
191                else 
192                {
193                    *ok = 0;
194                    return 0;
195                }
196            }
197        }
198        *ok = 1;
199        return value; 
200    }
201} // end getIntValue()
202
203////////////////////////////////////////////////
204paddr_t getPaddrValue( xmlTextReaderPtr reader, 
205                       const char * attributeName, 
206                       unsigned int * ok) 
207{
208    paddr_t value = 0;
209    unsigned int i;
210    char c;
211
212    char * string = (char *) xmlTextReaderGetAttribute(reader, 
213                                (const xmlChar *) attributeName);
214
215    if (string == NULL) 
216    {
217        // missing argument
218        *ok = 0;
219        return 0;
220    }
221    else 
222    {
223        if ((string[0] == '0') && ((string[1] == 'x') || (string[1] == 'X'))) 
224        {
225            // Hexa
226            for (i = 2 ; (string[i] != 0) && (i < 18) ; i++) 
227            {
228                c = string[i];
229                if      ((c >= '0') && (c <= '9')) { value = (value << 4) + string[i] - 48; }
230                else if ((c >= 'a') && (c <= 'f')) { value = (value << 4) + string[i] - 87; }
231                else if ((c >= 'A') && (c <= 'F')) { value = (value << 4) + string[i] - 55; }
232                else 
233                {
234                    *ok = 0;
235                    return 0;
236                }
237            }
238        }
239        else 
240        {
241            // Decimal not supported for paddr_t
242            *ok = 0;
243            return 0;
244        }
245        *ok = 1;
246        return value; 
247    }
248} // end getPaddrValue()
249
250////////////////////////////////////////////////
251char * getStringValue( xmlTextReaderPtr reader, 
252                       const char * attributeName, 
253                       unsigned int * ok ) 
254{
255    char * string = (char *) xmlTextReaderGetAttribute(reader, 
256                               (const xmlChar *) attributeName);
257
258
259    if (string == NULL) 
260    {
261        // missing argument
262        *ok = 0;
263        return NULL;
264    }
265    else 
266    {
267        //we read only string smaller than 32 byte
268        if (strlen(string) > 32) 
269        {
270            printf("[XML ERROR] all strings must be less than 32 bytes\n");
271            exit(1);
272        }
273
274        *ok = 1;
275        return string;
276    }
277} // end getStringValue()
278
279///////////////////////////////////////////////////////
280void set_periph_vbase_array( unsigned int vseg_index )
281{
282    unsigned int vbase  = vseg[vseg_index]->vbase;  // peripheral vbase address
283    unsigned int psegid = vseg[vseg_index]->psegid; // pseg global index
284    unsigned int type;                              // peripheral type
285    unsigned int periph_id; 
286
287    for ( periph_id = 0 ; periph_id < header->periphs ; periph_id++)
288    {
289        if( periph[periph_id]->psegid == psegid )
290        {
291            type = periph[periph_id]->type;
292            if ( periph_vbase_array[type] == 0xFFFFFFFF ) 
293                 periph_vbase_array[type] = vbase;   
294        }
295    }
296}  // end set_periph_vbase_array()
297
298////////////////////////////////////////////////////////
299int getPsegId(unsigned int cluster_id, char * pseg_name) 
300{
301    unsigned int pseg_id;
302    unsigned int pseg_min = cluster[cluster_id]->pseg_offset;
303    unsigned int pseg_max = pseg_min + cluster[cluster_id]->psegs;
304
305    for (pseg_id = pseg_min; pseg_id < pseg_max; pseg_id++) 
306    {
307        if (strcmp(pseg[pseg_id]->name, pseg_name) == 0) 
308        {
309            return pseg_id;
310        }
311    }
312    return -1;
313}
314
315///////////////////////////////////
316int getVspaceId(char * vspace_name) 
317{
318    unsigned int vspace_id;
319
320    for (vspace_id = 0; vspace_id < vspace_index; vspace_id++) 
321    {
322        if (strcmp(vspace[vspace_id]->name, vspace_name) == 0) 
323        {
324            return vspace_id;
325        }
326    }
327    return -1;
328}
329
330///////////////////////////////////////////////////////////////////////////////////
331int getVobjLocId(unsigned int vspace_id, char * vobj_name, unsigned int vspace_max) 
332{
333    unsigned int vobj_id;
334    unsigned int vobj_min = vspace[vspace_id]->vobj_offset;
335    unsigned int vobj_max = vobj_min + vspace_max;
336
337    for (vobj_id = vobj_min; vobj_id < vobj_max; vobj_id++) {
338        if (strcmp(vobj[vobj_id]->name, vobj_name) == 0) {
339            return (vobj_id - vobj_min);
340        }
341    }
342    return -1;
343}
344
345
346/////////////////////////////////////////
347void taskNode(xmlTextReaderPtr reader) 
348{
349    unsigned int ok;
350    unsigned int value;
351    char * str;
352
353    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) { return; }
354
355    if (task_index >= MAX_TASKS) 
356    {
357        printf("[XML ERROR] The number of tasks is larger than %d\n", MAX_TASKS);
358    }
359
360#if XML_PARSER_DEBUG
361printf("   task %d\n", task_loc_index);
362#endif
363
364    task[task_index] = (mapping_task_t *) malloc(sizeof(mapping_task_t));
365
366    ////////// get name attribute
367    str = getStringValue(reader, "name", &ok);
368    if (ok) 
369    {
370#if XML_PARSER_DEBUG
371printf("      name = %s\n", str);
372#endif
373        strncpy( task[task_index]->name, str, 31 );
374    }
375    else 
376    {
377        printf("[XML ERROR] illegal or missing <name> attribute for task (%d,%d)\n", 
378                vspace_index, task_loc_index);
379        exit(1);
380    }
381
382    ///////// get clusterid attribute
383    value = getIntValue(reader, "clusterid", &ok);
384    if (ok) 
385    {
386#if XML_PARSER_DEBUG
387printf("      clusterid = %x\n", value);
388#endif
389        if (value >= header->clusters) 
390        {
391            printf("[XML ERROR] <clusterid> too large for task (%d,%d)\n",
392                    vspace_index, task_loc_index);
393            exit(1);
394        }
395        task[task_index]->clusterid = value;
396    } 
397    else 
398    {
399        printf("[XML ERROR] illegal or missing <clusterid> attribute for task (%d,%d)\n",
400                vspace_index, task_loc_index);
401        exit(1);
402    }
403
404    ////////// get proclocid attribute
405    value = getIntValue(reader, "proclocid", &ok);
406    if (ok) 
407    {
408#if XML_PARSER_DEBUG
409printf("      proclocid = %x\n", value);
410#endif
411        if (value >= cluster[task[task_index]->clusterid]->procs) 
412        {
413            printf("[XML ERROR] <proclocid> too large for task (%d,%d)\n",
414                    vspace_index, task_loc_index);
415            exit(1);
416        }
417        task[task_index]->proclocid = value;
418    } 
419    else 
420    {
421        printf("[XML ERROR] illegal or missing <locprocid> attribute for task (%d,%d)\n", 
422                vspace_index, task_loc_index);
423        exit(1);
424    }
425
426    ////////// get stackname attribute
427    str = getStringValue(reader, "stackname" , &ok);
428    if (ok) 
429    {
430        int index = getVobjLocId(vspace_index, str , vobj_loc_index);
431        if (index >= 0) 
432        {
433#if XML_PARSER_DEBUG
434printf("      stackname = %s\n", str);
435printf("      stackid   = %d\n", index);
436#endif
437            task[task_index]->stack_vobjid = index;
438        }
439        else 
440        {
441            printf("[XML ERROR] illegal or missing <stackname> for task (%d,%d)\n", 
442                    vspace_index, task_loc_index);
443            exit(1);
444        }
445    } 
446    else 
447    {
448        printf("[XML ERROR] illegal or missing <stackname> for task (%d,%d)\n", 
449                vspace_index, task_loc_index);
450        exit(1);
451    }
452
453    ////////// get heap attribute
454    str = getStringValue(reader, "heapname", &ok);
455    if (ok) 
456    {
457        int index = getVobjLocId(vspace_index, str, vobj_loc_index);
458        if (index >= 0) 
459        {
460#if XML_PARSER_DEBUG
461printf("      heapname = %s\n", str);
462printf("      heapid   = %d\n", index);
463#endif
464            task[task_index]->heap_vobjid = index;
465        }
466        else 
467        {
468            printf("[XML ERROR] illegal or missing <heapname> for task (%d,%d)\n", 
469                   vspace_index, task_loc_index);
470            exit(1);
471        }
472    } 
473    else 
474    {
475        task[task_index]->heap_vobjid = -1;
476    }
477
478
479    ////////// get startid  attribute
480    value = getIntValue(reader, "startid", &ok);
481    if (ok) 
482    {
483#if XML_PARSER_DEBUG
484printf("      startid = %x\n", value);
485#endif
486        task[task_index]->startid = value;
487    } 
488    else 
489    {
490        printf("[XML ERROR] illegal or missing <startid> attribute for task (%d,%d)\n", 
491                vspace_index, task_loc_index);
492        exit(1);
493    }
494
495    /////////// get usetty  attribute (optionnal : 0 if missing)
496    value = getIntValue(reader, "usetty", &ok);
497#if XML_PARSER_DEBUG
498printf("      usetty = %x\n", value);
499#endif
500    task[task_index]->use_tty = (ok)? value : 0;
501
502    /////////// get usenic  attribute (optionnal : 0 if missing)
503    value = getIntValue(reader, "usenic", &ok);
504#if XML_PARSER_DEBUG
505printf("      usenic = %x\n", value);
506#endif
507    task[task_index]->use_nic = (ok)? value : 0;
508
509    /////////// get usetim attribute (optionnal : 0 if missing)
510    value = getIntValue(reader, "usetim", &ok);
511#if XML_PARSER_DEBUG
512printf("      usetim = %x\n", value);
513#endif
514    task[task_index]->use_tim = (ok)? value : 0;
515
516    /////////// get usedma  attribute (optionnal : 0 if missing)
517    value = getIntValue(reader, "usedma", &ok);
518#if XML_PARSER_DEBUG
519printf("      usedma = %x\n", value);
520#endif
521    task[task_index]->use_dma = (ok)? value : 0;
522
523    /////////// get useioc  attribute (optionnal : 0 if missing)
524    value = getIntValue(reader, "useioc", &ok);
525#if XML_PARSER_DEBUG
526printf("      useioc = %x\n", value);
527#endif
528    task[task_index]->use_ioc = (ok)? value : 0;
529
530    /////////// get usecma  attribute (optionnal : 0 if missing)
531    value = getIntValue(reader, "usecma", &ok);
532#if XML_PARSER_DEBUG
533printf("      usecma = %x\n", value);
534#endif
535    task[task_index]->use_cma = (ok)? value : 0;
536
537    task_index++;
538    task_loc_index++;
539} // end taskNode()
540
541
542//////////////////////////////////////
543void vobjNode(xmlTextReaderPtr reader) 
544{
545    unsigned int ok;
546    unsigned int value;
547    char * str;
548
549    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) { return; }
550
551    if (vobj_index >= MAX_VOBJS) 
552    {
553        printf("[XML ERROR] The number of vobjs is larger than %d\n", MAX_VOBJS);
554        exit(1);
555    }
556
557#if XML_PARSER_DEBUG
558printf("      vobj %d\n", vobj_loc_index);
559#endif
560
561    vobj[vobj_index] = (mapping_vobj_t *) malloc(sizeof(mapping_vobj_t));
562
563    ///////// get name attribute
564    str = getStringValue(reader, "name", &ok);
565    if (ok) 
566    {
567#if XML_PARSER_DEBUG
568printf("        name = %s\n", str);
569#endif
570        strncpy(vobj[vobj_index]->name, str, 31);
571    }
572    else 
573    {
574        printf("[XML ERROR] illegal or missing <name> attribute for vobj (%d,%d)\n", 
575                vseg_index, vobj_loc_index);
576        exit(1);
577    }
578
579    //////// get type attribute
580    str = getStringValue(reader, "type", &ok);
581#if XML_PARSER_DEBUG
582printf("        type = %s\n", str);
583#endif
584    if (ok && (strcmp(str, "ELF") == 0)) 
585    {
586        vobj[vobj_index]->type = VOBJ_TYPE_ELF;
587
588        assert( (vobj_count == 0) && "[XML ERROR] an ELF vobj must be alone in a vseg");
589    }
590    else if (ok && (strcmp(str, "PERI")     == 0)) 
591    { 
592        vobj[vobj_index]->type = VOBJ_TYPE_PERI;     
593
594        assert( (vobj_count == 0) && "[XML ERROR] a PERI vobj must be alone in a vseg");
595
596        // fill the peripheral base address array
597        set_periph_vbase_array( vseg_index );
598    }
599    else if (ok && (strcmp(str, "BLOB")     == 0)) { vobj[vobj_index]->type = VOBJ_TYPE_BLOB;     }
600    else if (ok && (strcmp(str, "PTAB")     == 0)) { vobj[vobj_index]->type = VOBJ_TYPE_PTAB;     }
601    else if (ok && (strcmp(str, "MWMR")     == 0)) { vobj[vobj_index]->type = VOBJ_TYPE_MWMR;     }
602    else if (ok && (strcmp(str, "LOCK")     == 0)) { vobj[vobj_index]->type = VOBJ_TYPE_LOCK;     }
603    else if (ok && (strcmp(str, "BUFFER")   == 0)) { vobj[vobj_index]->type = VOBJ_TYPE_BUFFER;   }
604    else if (ok && (strcmp(str, "BARRIER")  == 0)) { vobj[vobj_index]->type = VOBJ_TYPE_BARRIER;  }
605    else if (ok && (strcmp(str, "CONST")    == 0)) { vobj[vobj_index]->type = VOBJ_TYPE_CONST;    }
606    else if (ok && (strcmp(str, "MEMSPACE") == 0)) { vobj[vobj_index]->type = VOBJ_TYPE_MEMSPACE; }
607    else if (ok && (strcmp(str, "SCHED")    == 0)) { vobj[vobj_index]->type = VOBJ_TYPE_SCHED;    }
608    else 
609    {
610        printf("[XML ERROR] illegal or missing <type> attribute for vobj (%d,%d)\n", 
611                vspace_index, vobj_loc_index);
612        exit(1);
613    }
614
615    ////////// get length attribute
616    value = getIntValue(reader, "length", &ok);
617    if (ok) {
618#if XML_PARSER_DEBUG
619printf("        length = %d\n", value);
620#endif
621        vobj[vobj_index]->length = value;
622    } 
623    else {
624        printf("[XML ERROR] illegal or missing <length> attribute for vobj (%d,%d)\n", 
625                vspace_index, vobj_loc_index);
626        exit(1);
627    }
628
629    ////////// get align attribute (optional : 0 if missing)
630    value = getIntValue(reader, "align", &ok);
631    if (ok) {
632#if XML_PARSER_DEBUG
633printf("        align = %d\n", value);
634#endif
635        vobj[vobj_index]->align = value;
636    } 
637    else {
638        vobj[vobj_index]->align = 0;
639    }
640
641    ////////// get binpath attribute (optional : '\0' if missing)
642    str = getStringValue(reader, "binpath", &ok);
643    if (ok) {
644#if XML_PARSER_DEBUG
645printf("        binpath = %s\n", str);
646#endif
647        strncpy(vobj[vobj_index]->binpath, str, 63);
648    } 
649    else {
650        vobj[vobj_index]->binpath[0] = '\0';
651    }
652
653    ////////// get init attribute (mandatory for mwmr and barrier)
654    value = getIntValue(reader, "init", &ok);
655    if (ok) 
656    {
657#if XML_PARSER_DEBUG
658printf("        init  = %d\n", value);
659#endif
660        vobj[vobj_index]->init = value;
661    } 
662    else 
663    {
664        if ((vobj[vobj_index]->type == VOBJ_TYPE_MWMR) || 
665            (vobj[vobj_index]->type == VOBJ_TYPE_BARRIER) ||
666            (vobj[vobj_index]->type == VOBJ_TYPE_CONST)) 
667        {
668            printf("[XML ERROR] illegal or missing <value> attribute for vobj (%d,%d). \
669                    All MWMR or BARRIER or CONST vobj must have a init value \n", 
670                    vspace_index, vobj_loc_index);
671            exit(1);
672        }
673        vobj[vobj_index]->init = 0;
674    }
675
676    vobj_index++;
677    vobj_count++;
678    vobj_loc_index++;
679} // end vobjNode()
680
681
682//////////////////////////////////////
683void vsegNode(xmlTextReaderPtr reader) 
684{
685    unsigned int ok;
686    unsigned int value;
687    char * str;
688
689    vobj_count = 0;
690
691    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) { return; }
692
693    if (vseg_index >= MAX_VSEGS) 
694    {
695        printf("[XML ERROR] The number of vsegs is larger than %d\n", MAX_VSEGS);
696        exit(1);
697    }
698
699#if XML_PARSER_DEBUG
700printf("    vseg %d\n", vseg_loc_index);
701#endif
702
703    vseg[vseg_index] = (mapping_vseg_t *) malloc(sizeof(mapping_vseg_t));
704
705    ////////// set vobj_offset attributes
706    vseg[vseg_index]->vobj_offset = vobj_index;
707
708#if XML_PARSER_DEBUG
709printf("      vobj_offset = %d\n", vobj_index);
710#endif
711
712    ///////// get name attribute
713    str = getStringValue(reader, "name", &ok);
714    if (ok) 
715    {
716#if XML_PARSER_DEBUG
717printf("      name = %s\n", str);
718#endif
719        strncpy( vseg[vseg_index]->name, str, 31);
720    }
721    else 
722    {
723        printf("[XML ERROR] illegal or missing <name> attribute for vseg (%d,%d)\n", 
724                vspace_index, vseg_loc_index);
725        exit(1);
726    }
727
728    ////////// get ident attribute (optional : 0 if missing)
729    value = getIntValue(reader, "ident", &ok);
730    if (ok) 
731    {
732#if XML_PARSER_DEBUG
733printf("      ident = %d\n", value);
734#endif
735        vseg[vseg_index]->ident = value;
736    } 
737    else 
738    {
739        vseg[vseg_index]->ident = 0;
740    }
741
742    /////////// get vbase attribute
743    value = getIntValue(reader, "vbase", &ok);
744    if (ok) 
745    {
746#if XML_PARSER_DEBUG
747printf("      vbase = 0x%x\n", value);
748#endif
749        vseg[vseg_index]->vbase = value;
750    }
751    else 
752    {
753        printf("[XML ERROR] illegal or missing <vbase> attribute for vseg (%d,%d)\n", 
754                vspace_index, vseg_loc_index);
755        exit(1);
756    }
757
758    ////////// get clusterid and psegname attributes
759    value = getIntValue(reader, "clusterid", &ok);
760    if (ok == 0) 
761    {
762        printf("[XML ERROR] illegal or missing <clusterid> for vseg %d\n", 
763                vseg_loc_index);
764        exit(1);
765    }
766    str = getStringValue(reader, "psegname", &ok);
767    if (ok == 0) 
768    {
769        printf("[XML ERROR] illegal or missing <psegname> for vseg %d\n", 
770                vseg_loc_index);
771        exit(1);
772    }
773
774    /////////// set psegid field
775    int index = getPsegId(value, str);
776    if (index >= 0) 
777    {
778#if XML_PARSER_DEBUG
779printf("      clusterid = %d\n", value);
780printf("      psegname  = %s\n", str);
781printf("      psegid    = %d\n", index);
782#endif
783        vseg[vseg_index]->psegid = index;
784    }
785    else 
786    {
787        printf("[XML ERROR] pseg not found for vseg %d / clusterid = %d / psegname = %s\n", 
788                vseg_loc_index, value, str );
789        exit(1);
790    } 
791
792    //////// get mode attribute
793    str = getStringValue(reader, "mode", &ok);
794#if XML_PARSER_DEBUG
795printf("      mode = %s\n", str);
796#endif
797    if      (ok && (strcmp(str, "CXWU") == 0)) { vseg[vseg_index]->mode = 0xF; }
798    else if (ok && (strcmp(str, "CXW_") == 0)) { vseg[vseg_index]->mode = 0xE; }
799    else if (ok && (strcmp(str, "CX_U") == 0)) { vseg[vseg_index]->mode = 0xD; }
800    else if (ok && (strcmp(str, "CX__") == 0)) { vseg[vseg_index]->mode = 0xC; }
801    else if (ok && (strcmp(str, "C_WU") == 0)) { vseg[vseg_index]->mode = 0xB; }
802    else if (ok && (strcmp(str, "C_W_") == 0)) { vseg[vseg_index]->mode = 0xA; }
803    else if (ok && (strcmp(str, "C__U") == 0)) { vseg[vseg_index]->mode = 0x9; }
804    else if (ok && (strcmp(str, "C___") == 0)) { vseg[vseg_index]->mode = 0x8; }
805    else if (ok && (strcmp(str, "_XWU") == 0)) { vseg[vseg_index]->mode = 0x7; }
806    else if (ok && (strcmp(str, "_XW_") == 0)) { vseg[vseg_index]->mode = 0x6; }
807    else if (ok && (strcmp(str, "_X_U") == 0)) { vseg[vseg_index]->mode = 0x5; }
808    else if (ok && (strcmp(str, "_X__") == 0)) { vseg[vseg_index]->mode = 0x4; }
809    else if (ok && (strcmp(str, "__WU") == 0)) { vseg[vseg_index]->mode = 0x3; }
810    else if (ok && (strcmp(str, "__W_") == 0)) { vseg[vseg_index]->mode = 0x2; }
811    else if (ok && (strcmp(str, "___U") == 0)) { vseg[vseg_index]->mode = 0x1; }
812    else if (ok && (strcmp(str, "____") == 0)) { vseg[vseg_index]->mode = 0x0; }
813    else {
814        printf("[XML ERROR] illegal or missing <mode> attribute for vseg (%d,%d)\n", 
815                vspace_index, vseg_loc_index);
816        exit(1);
817    }
818
819    ////////// get vobjs in vseg
820    int status = xmlTextReaderRead(reader);
821    while (status == 1) {
822        const char * tag = (const char *) xmlTextReaderConstName(reader);
823
824        if      (strcmp(tag, "vobj")     == 0 ) { vobjNode(reader); }
825        else if (strcmp(tag, "#text"  )  == 0 ) { }
826        else if (strcmp(tag, "#comment") == 0 ) { }
827        else if (strcmp(tag, "vseg")     == 0 ) 
828        {
829            vseg[vseg_index]->vobjs = vobj_count;
830            vseg_index++;
831            vseg_loc_index++;
832            return;
833        }
834        else {
835            printf("[XML ERROR] Unknown tag %s", tag);
836            exit(1);
837        }
838        status = xmlTextReaderRead (reader);
839    }
840} // end vsegNode()
841
842
843//////////////////////////////////////////
844void vspaceNode(xmlTextReaderPtr reader) {
845    char * str;
846    unsigned int ok;
847    unsigned int nb_task_vspace = 0;
848
849    vobj_loc_index = 0;
850    vseg_loc_index = 0;
851    task_loc_index = 0;
852
853    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) {
854        return;
855    }
856
857    // checking source file consistency
858    if (vspace_index >= header->vspaces) {
859        printf("[XML ERROR] The vspace index is too large : %d\n", 
860                vspace_index);
861        exit(1);
862    }
863
864#if XML_PARSER_DEBUG
865printf("\n  vspace %d\n", vspace_index);
866#endif
867
868    vspace[vspace_index] = (mapping_vspace_t *) malloc(sizeof(mapping_vspace_t));
869
870    ////////// get name attribute
871    str = getStringValue(reader, "name", &ok);
872    if (ok) {
873#if XML_PARSER_DEBUG
874printf("  name = %s\n", str);
875#endif
876        strncpy(vspace[vspace_index]->name, str, 31);
877    }
878    else {
879        printf("[XML ERROR] illegal or missing <name> attribute for vspace %d\n", 
880                vspace_index);
881        exit(1);
882    }
883
884    ////////// set vseg_offset and task_offset attributes
885    vspace[vspace_index]->vseg_offset = vseg_index;
886    vspace[vspace_index]->vobj_offset = vobj_index;
887    vspace[vspace_index]->task_offset = task_index;
888
889#if XML_PARSER_DEBUG
890    printf("  vseg_offset = %d\n", vseg_index);
891    printf("  vobj_offset = %d\n", vobj_index);
892    printf("  task_offset = %d\n", task_index);
893#endif
894
895    ////////// get startname attribute
896    str = getStringValue(reader, "startname", &ok);
897    if (ok) {
898        //used after parsing the vobjs
899    }
900    else {
901        printf("[XML ERROR] illegal or missing <startname> attribute for vspace %s\n", 
902                vspace[vspace_index]->name);
903        exit(1);
904    }
905
906    int status = xmlTextReaderRead(reader);
907    while (status == 1) {
908        const char * tag = (const char *) xmlTextReaderConstName(reader);
909
910        if (strcmp(tag, "vseg") == 0) {
911            vsegNode(reader);
912        }
913        else if (strcmp(tag, "task") == 0) {
914            taskNode(reader);
915            nb_task_vspace++;
916        }
917        else if (strcmp(tag, "#text")    == 0) { }
918        else if (strcmp(tag, "#comment") == 0) { }
919        else if (strcmp(tag, "vspace")   == 0) {
920            vspace[vspace_index]->vobjs = vobj_loc_index; 
921            vspace[vspace_index]->tasks = task_loc_index ;
922            vspace[vspace_index]->vsegs = vseg_loc_index ;
923
924            // get index of the vobj containing the start vector
925            int index = getVobjLocId(vspace_index, str , vobj_loc_index);
926            if (index == -1) {
927                printf("[XML ERROR] vobj containing start vector not found in vspace %s\n",
928                        vspace[vspace_index]->name);
929                exit(-1);
930            }
931            else {
932                vspace[vspace_index]->start_offset = index;
933#if XML_PARSER_DEBUG
934                printf("      startname = %s\n", str);
935                printf("      startid   = %d\n", index);
936                printf("  end vspace %d\n\n", vspace_index);
937#endif
938            }
939
940            // checking startid values for all tasks in vspace
941            int task_id;
942            int task_min = vspace[vspace_index]->task_offset;
943            int task_max = task_min + vspace[vspace_index]->tasks; 
944            for (task_id = task_min; task_id < task_max; task_id++) {
945                if (task[task_id]->startid >= vspace[vspace_index]->tasks) {
946                    printf("[XML ERROR] <startid> too large for task (%d,%d)\n", 
947                            vspace_index, task_id );
948                    exit(1);
949                }
950            }
951
952            nb_tasks_max += nb_task_vspace;
953            vspace_index++;
954            return;
955        }
956        else {
957            printf("[XML ERROR] Unknown tag %s", tag);
958            exit(1);
959        }
960        status = xmlTextReaderRead(reader);
961    }
962} // end vspaceNode()
963
964
965///////////////////////////////////////////
966void cpPortNode(xmlTextReaderPtr reader) {
967    char * str;
968    unsigned int ok;
969
970    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) {
971        return;
972    }
973
974    if (cp_port_index >= MAX_CP_PORTS) {
975        printf("[XML ERROR] The number of ports (for coprocs) is larger than %d\n", MAX_CP_PORTS);
976    }
977
978#if XML_PARSER_DEBUG
979    printf("\n  port %d\n", cp_port_index);
980#endif
981
982    cp_port[cp_port_index] = (mapping_cp_port_t *) malloc(sizeof(mapping_cp_port_t));
983    cp_port_vobj_ref[cp_port_index] = (vobj_ref_t *) malloc(sizeof(vobj_ref_t));
984
985
986
987    ///////// get direction attribute
988    str = getStringValue(reader, "direction", &ok);
989    if (ok) {
990#if XML_PARSER_DEBUG
991        printf("      direction = %s\n", str);
992#endif
993        if (strcmp(str, "TO_COPROC")   ==  0) {
994            cp_port[cp_port_index]->direction = PORT_TO_COPROC;
995        }
996        else if (strcmp(str, "FROM_COPROC") ==  0) {
997            cp_port[cp_port_index]->direction = PORT_FROM_COPROC;
998        }
999        else {
1000            printf("[XML ERROR] illegal <direction> for cp_port %d in cluster %d\n",
1001                    cp_port_index, cluster_index);
1002            exit(1);
1003        }
1004    } 
1005    else {
1006        printf("[XML ERROR] missing <direction> for cp_port %d in cluster %d\n",
1007                cp_port_index, cluster_index);
1008        exit(1);
1009    }
1010
1011    /////////// get vspacename attribute
1012    str = getStringValue(reader, "vspacename", &ok);
1013#if XML_PARSER_DEBUG
1014    printf("      vspacename = %s\n", str);
1015#endif
1016    if (ok) {
1017        strncpy(cp_port_vobj_ref[cp_port_index]->vspace_name, str, 31);
1018    }
1019    else {
1020        printf("[XML ERROR] missing <vspacename> for cp_port %d in cluster %d\n",
1021                cp_port_index, cluster_index);
1022        exit(1);
1023    }
1024
1025    /////////// get vobjname attribute
1026    str = getStringValue(reader, "vobjname", &ok);
1027#if XML_PARSER_DEBUG
1028    printf("      vobjname = %s\n", str);
1029#endif
1030    if (ok) {
1031        strncpy(cp_port_vobj_ref[cp_port_index]->vobj_name, str, 31);
1032    }
1033    else {
1034        printf("[XML ERROR] missing <vobjname> for cp_port %d in cluster %d\n",
1035                cp_port_index, cluster_index);
1036        exit(1);
1037    }
1038
1039    cp_port_index++;
1040    cp_port_loc_index++;
1041
1042} // end cpPortNode()
1043
1044
1045////////////////////////////////////////
1046void periphNode(xmlTextReaderPtr reader) 
1047{
1048    char * str;
1049    unsigned int value;
1050    unsigned int ok;
1051
1052    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) { return; }
1053
1054    if (periph_index >= MAX_PERIPHS) 
1055    {
1056        printf("[XML ERROR] The number of periphs is larger than %d\n", MAX_PERIPHS);
1057    }
1058
1059#if XML_PARSER_DEBUG
1060    printf("\n  periph %d\n", periph_index);
1061#endif
1062
1063    periph[periph_index] = (mapping_periph_t *) malloc(sizeof(mapping_periph_t));
1064
1065    ///////// get channels attribute (optionnal : 1 if missing)
1066    value = getIntValue(reader, "channels", &ok);
1067    if (ok) 
1068    {
1069#if XML_PARSER_DEBUG
1070        printf("      channels = %d\n", value);
1071#endif
1072        periph[periph_index]->channels = value;
1073    }
1074    else 
1075    {
1076        periph[periph_index]->channels = 1;
1077    }
1078
1079    /////////// get psegname attribute
1080    str = getStringValue(reader, "psegname", &ok);
1081    if (ok == 0) 
1082    {
1083        printf("[XML ERROR] illegal or missing <psegname> for coproc %d in cluster %d\n", 
1084                coproc_index, cluster_index);
1085        exit(1);
1086    }
1087
1088    /////////// set psegid attribute
1089    int index = getPsegId(cluster_index, str);
1090    if (index >= 0) 
1091    {
1092#if XML_PARSER_DEBUG
1093        printf("      clusterid = %d\n", cluster_index);
1094        printf("      psegname  = %s\n", str);
1095        printf("      psegid    = %d\n", index);
1096#endif
1097        periph[periph_index]->psegid = index;
1098        assert(pseg[index]->type == PSEG_TYPE_PERI && 
1099                "peripheral psegname attribute must refer to a pseg of type PERI" );
1100    }
1101    else 
1102    {
1103        printf("[XML ERROR] pseg not found for periph %d / clusterid = %d / psegname = %s\n", 
1104                periph_loc_index, cluster_index, str );
1105        exit(1);
1106    } 
1107
1108    /////////// get type attribute
1109    str = getStringValue(reader, "type", &ok);
1110    if (ok) 
1111    {
1112#if XML_PARSER_DEBUG
1113        printf("      type     = %s\n", str);
1114#endif
1115        unsigned int error = 0;
1116
1117        // The TTY, IOC, NIC, FBF, CMA and IOB peripherals are not replicated in all clusters
1118        // but can be replicated in two different clusters for fault tolerance
1119        // In case of replication, the number of channels must be the same
1120        if (strcmp(str, "IOC") == 0) 
1121        {
1122            periph[periph_index]->type = PERIPH_TYPE_IOC;
1123            if (header->ioc_cluster == 0xFFFFFFFF)
1124            {
1125                header->ioc_cluster  = cluster_index; 
1126                ioc_channels = periph[periph_index]->channels;
1127            }
1128            else if (header->ioc_cluster_bis == 0xFFFFFFFF) 
1129            {
1130                header->ioc_cluster_bis = cluster_index;
1131            }
1132            else
1133            {
1134                error = 1;
1135            }
1136        } 
1137        else if (strcmp(str, "TTY") == 0) 
1138        {
1139            periph[periph_index]->type = PERIPH_TYPE_TTY;
1140            if (header->tty_cluster == 0xFFFFFFFF) 
1141            {
1142                header->tty_cluster = cluster_index;
1143                tty_channels = periph[periph_index]->channels;
1144            }
1145            else if (header->tty_cluster_bis == 0xFFFFFFFF) 
1146            {
1147                header->tty_cluster_bis = cluster_index;
1148            }
1149            else 
1150            {
1151                error = 1;
1152            }
1153        }
1154        else if (strcmp(str, "FBF") == 0) 
1155        {
1156            periph[periph_index]->type = PERIPH_TYPE_FBF;
1157            if (header->fbf_cluster == 0xFFFFFFFF) 
1158            {
1159                header->fbf_cluster = cluster_index; 
1160            }
1161            else if (header->fbf_cluster_bis == 0xFFFFFFFF) 
1162            {
1163                header->fbf_cluster_bis = cluster_index; 
1164            }
1165            else 
1166            {
1167                error = 1;
1168            }
1169        }
1170        else if (strcmp(str, "NIC") == 0) 
1171        {
1172            periph[periph_index]->type = PERIPH_TYPE_NIC;
1173            if (header->nic_cluster == 0xFFFFFFFF) 
1174            {
1175                header->nic_cluster = cluster_index;
1176                nic_channels = periph[periph_index]->channels;
1177            }
1178            else if (header->nic_cluster_bis == 0xFFFFFFFF) 
1179            {
1180                header->nic_cluster_bis = cluster_index;
1181            }
1182            else 
1183            {
1184                error = 1;
1185            }
1186        }
1187        else if (strcmp(str, "CMA") == 0) 
1188        {
1189            periph[periph_index]->type = PERIPH_TYPE_CMA;
1190            if (header->cma_cluster == 0xFFFFFFFF) 
1191            {
1192                header->cma_cluster = cluster_index;
1193                cma_channels = periph[periph_index]->channels;
1194            }
1195            else if (header->nic_cluster_bis == 0xFFFFFFFF) 
1196            {
1197                header->cma_cluster_bis = cluster_index;
1198            }
1199            else 
1200            {
1201                error = 1;
1202            }
1203        }
1204        else if (strcmp(str, "IOB") == 0) 
1205        {
1206            periph[periph_index]->type = PERIPH_TYPE_IOB;
1207            io_mmu_active = 1;
1208            if (header->iob_cluster == 0xFFFFFFFF) 
1209            {
1210                header->iob_cluster = cluster_index; 
1211            }
1212            else if (header->iob_cluster_bis == 0xFFFFFFFF) 
1213            {
1214                header->iob_cluster_bis = cluster_index; 
1215            }
1216            else 
1217            {
1218                error = 1;
1219            }
1220        }
1221        // The TIM, ICU, XICU, DMA peripherals are replicated in all clusters
1222        // but only one component per cluster
1223        else if (strcmp(str, "TIM") == 0 ) 
1224        {
1225            periph[periph_index]->type = PERIPH_TYPE_TIM;
1226            if (found_timer || use_xicu)  error = 1; 
1227            found_timer = 1;
1228            if (tim_channels < periph[periph_index]->channels) 
1229            {
1230                tim_channels = periph[periph_index]->channels;
1231            }
1232        }
1233        else if (strcmp(str, "ICU") == 0) 
1234        {
1235            periph[periph_index]->type = PERIPH_TYPE_ICU;
1236            if (found_icu)  error = 1; 
1237            found_icu = 1;
1238        }
1239        else if (strcmp(str, "XICU") == 0) 
1240        {
1241            periph[periph_index]->type = PERIPH_TYPE_ICU;
1242            if (found_icu || found_timer)  error = 1; 
1243            found_icu = 1;
1244            if (tim_channels == 0) 
1245            { 
1246                tim_channels = 32; 
1247            }
1248            use_xicu = 1;
1249        }
1250        else if (strcmp(str, "DMA") == 0) 
1251        {
1252            periph[periph_index]->type = PERIPH_TYPE_DMA;
1253            if (found_dma)  error = 1; 
1254            found_dma = 1;
1255            if (dma_channels < periph[periph_index]->channels)
1256                dma_channels = periph[periph_index]->channels;
1257        }
1258        else 
1259        {
1260            printf("[XML ERROR] illegal <type> for peripheral %d in cluster %d\n",
1261                    periph_loc_index, cluster_index);
1262            exit(1);
1263        }
1264
1265        if (error) 
1266        {
1267            printf("[XML ERROR] illegal <type> for peripheral %d in cluster %d\n",
1268                    periph_loc_index, cluster_index);
1269            exit(1);
1270        }
1271    }
1272    else 
1273    {
1274        printf("[XML ERROR] missing <type> for peripheral  %d in cluster %d\n",
1275                periph_loc_index, cluster_index);
1276        exit(1);
1277    }
1278
1279    periph_index++;
1280    periph_loc_index++;
1281    cluster[cluster_index]->periphs++;
1282
1283} // end periphNode
1284
1285
1286/////////////////////////////////////////
1287void coprocNode(xmlTextReaderPtr reader) {
1288    char * str;
1289    unsigned int ok;
1290
1291    cp_port_loc_index = 0;
1292
1293    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) {
1294        return;
1295    }
1296
1297    if (coproc_index >= MAX_COPROCS) {
1298        printf("[XML ERROR] The number of coprocs is larger than %d\n", MAX_COPROCS);
1299    }
1300
1301#if XML_PARSER_DEBUG
1302    printf("\n  coproc %d\n", coproc_index);
1303#endif
1304
1305    coproc[coproc_index] = (mapping_coproc_t *) malloc(sizeof(mapping_coproc_t));
1306
1307    /////////// get name attribute
1308    str = getStringValue(reader, "name", &ok);
1309    if (ok) {
1310#if XML_PARSER_DEBUG
1311        printf("      name = %s\n", str);
1312#endif
1313        strncpy(coproc[coproc_index]->name, str, 31);
1314    }
1315    else {
1316        printf("[XML ERROR] illegal or missing <name> for coproc %d in cluster %d\n",
1317                coproc_index, cluster_index);
1318        exit(1);
1319    }
1320
1321    /////////// get psegname attribute
1322    str = getStringValue(reader, "psegname", &ok);
1323    if (ok == 0) {
1324        printf("[XML ERROR] illegal or missing <psegname> for coproc %d in cluster %d\n", 
1325                coproc_index, cluster_index);
1326        exit(1);
1327    }
1328
1329    /////////// set psegid attribute
1330    int index = getPsegId(cluster_index, str);
1331    if (index >= 0) {
1332#if XML_PARSER_DEBUG
1333        printf("      clusterid = %d\n", cluster_index);
1334        printf("      psegname  = %s\n", str);
1335        printf("      psegid    = %d\n", index);
1336#endif
1337        coproc[coproc_index]->psegid = index;
1338        assert(pseg[index]->type == PSEG_TYPE_PERI && "coproc psegname attribute must refer to a pseg of type PERI" );
1339    }
1340    else {
1341        printf("[XML ERROR] pseg not found for coproc %d / clusterid = %d / psegname = %s\n", 
1342                coproc_index, cluster_index, str );
1343        exit(1);
1344    } 
1345
1346    ////////// set port_offset
1347    coproc[coproc_index]->port_offset = cp_port_index;
1348
1349#if XML_PARSER_DEBUG
1350    printf("      port_offset = %d\n", cp_port_index);
1351#endif
1352
1353    int status = xmlTextReaderRead(reader);
1354    while (status == 1) {
1355        const char * tag = (const char *) xmlTextReaderConstName(reader);
1356
1357        if (strcmp(tag, "port") == 0 ) {
1358            cpPortNode(reader);
1359        }
1360        else if (strcmp(tag, "#text")    == 0 ) { }
1361        else if (strcmp(tag, "#comment") == 0 ) { }
1362        else if (strcmp(tag, "coproc") == 0 ) {
1363            coproc[coproc_index]->ports = cp_port_loc_index;
1364            cluster[cluster_index]->coprocs++;
1365            coproc_loc_index++;
1366            coproc_index++;
1367            return;
1368        }
1369        else {
1370            printf("[XML ERROR] Unknown tag %s", tag);
1371            exit(1);
1372        }
1373        status = xmlTextReaderRead(reader);
1374    }
1375} // end coprocNode()
1376
1377
1378///////////////////////////////////////
1379void irqNode(xmlTextReaderPtr reader) {
1380    unsigned int ok;
1381    unsigned int value;
1382    char * str;
1383
1384    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) {
1385        return;
1386    }
1387
1388    if (irq_index >= MAX_IRQS) {
1389        printf("[XML ERROR] The number of irqs is larger than %d\n", MAX_IRQS);
1390    }
1391
1392#if XML_PARSER_DEBUG
1393    printf("     irq %d\n", irq_loc_index);
1394#endif
1395
1396    irq[irq_index] = (mapping_irq_t *) malloc(sizeof(mapping_irq_t));
1397
1398    ///////// get type attribute
1399    str = getStringValue(reader, "type", &ok);
1400    if (ok) {
1401#if XML_PARSER_DEBUG
1402        printf("        type    = %s\n", str);
1403#endif
1404        if (strcmp(str, "HARD") == 0 ) {
1405            irq[irq_index]->type = 0;
1406        }
1407        else {
1408            irq[irq_index]->type = 1;
1409        }
1410    }
1411    else {
1412        printf("[XML ERROR] missing IRQ <type> for processor %d in cluster %d\n",
1413                cluster_index, proc_loc_index);
1414        exit(1);
1415    }
1416
1417    ///////// get icuid attribute
1418    value = getIntValue(reader, "icuid", &ok);
1419    if (ok) {
1420#if XML_PARSER_DEBUG
1421        printf("        icuid   = %d\n", value);
1422#endif
1423        irq[irq_index]->icuid = value;
1424        if (value >= 32) {
1425            printf("[XML ERROR] IRQ <icuid> too large for processor %d in cluster %d\n",
1426                    cluster_index, proc_loc_index);
1427            exit(1);
1428        }
1429    }
1430    else {
1431        printf("[XML ERROR] missing IRQ <icuid> for processor %d in cluster %d\n",
1432                cluster_index, proc_loc_index);
1433        exit(1);
1434    }
1435
1436    ///////// get isr attribute
1437    str = getStringValue(reader, "isr", &ok);
1438    if (ok) {
1439#if XML_PARSER_DEBUG
1440        printf("        isr     = %s\n", str);
1441#endif
1442        if      (strcmp(str, "ISR_SWITCH" ) == 0) { irq[irq_index]->isr = ISR_SWITCH; }
1443        else if (strcmp(str, "ISR_IOC"    ) == 0) { irq[irq_index]->isr = ISR_IOC; }
1444        else if (strcmp(str, "ISR_DMA"    ) == 0) { irq[irq_index]->isr = ISR_DMA; }
1445        else if (strcmp(str, "ISR_TTY"    ) == 0) { irq[irq_index]->isr = ISR_TTY; }
1446        else if (strcmp(str, "ISR_TIMER"  ) == 0) { irq[irq_index]->isr = ISR_TIMER; }
1447        else {
1448            printf("[XML ERROR] illegal IRQ <isr> for processor %d in cluster %d\n",
1449                    cluster_index, proc_loc_index);
1450            exit(1);
1451        }
1452#if XML_PARSER_DEBUG
1453        printf("        isrnum  = %d\n", irq[irq_index]->isr);
1454#endif
1455    } 
1456    else {
1457        printf("[XML ERROR] missing IRQ <isr> for processor %d in cluster %d\n",
1458                cluster_index, proc_loc_index);
1459        exit(1);
1460    }
1461
1462    ///////// get channel attribute (optionnal : 0 if missing)
1463    value = getIntValue(reader, "channel", &ok);
1464    if (ok) {
1465#if XML_PARSER_DEBUG
1466        printf("        channel = %d\n", value);
1467#endif
1468        irq[irq_index]->channel = value;
1469    }
1470    else {
1471        irq[irq_index]->channel = 0;
1472    }
1473
1474    irq_index++;
1475    irq_loc_index++;
1476
1477} // end irqNode
1478
1479
1480/////////////////////////////////////////
1481void procNode(xmlTextReaderPtr reader) {
1482    unsigned int ok;
1483    unsigned int value;
1484
1485    irq_loc_index = 0;
1486
1487    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) {
1488        return;
1489    }
1490
1491    if (proc_index >= MAX_PROCS) {
1492        printf("[XML ERROR] The number of procs is larger than %d\n", MAX_PROCS);
1493    }
1494
1495#if XML_PARSER_DEBUG
1496    printf("\n  proc %d\n", proc_index);
1497#endif
1498
1499    proc[proc_index] = (mapping_proc_t *) malloc(sizeof(mapping_proc_t));
1500
1501
1502    /////////// get index attribute (optional)
1503    value = getIntValue(reader, "index", &ok);
1504    if (ok && (value != proc_loc_index)) {
1505        printf("[XML ERROR] wrong proc index / expected value is %d", 
1506                proc_loc_index);
1507        exit(1);
1508    }
1509
1510    ////////// set irq_offset attribute
1511    proc[proc_index]->irq_offset = irq_index;
1512
1513#if XML_PARSER_DEBUG
1514    printf("    irq_offset = %d\n", irq_index);
1515#endif
1516
1517    int status = xmlTextReaderRead(reader);
1518    while (status == 1) {
1519        const char * tag = (const char *) xmlTextReaderConstName(reader);
1520
1521        if (strcmp(tag, "irq") == 0) {
1522            irqNode(reader);
1523        }
1524        else if (strcmp(tag, "#text")    == 0) { }
1525        else if (strcmp(tag, "#comment") == 0) { }
1526        else if (strcmp(tag, "proc")     == 0) {
1527            proc[proc_index]->irqs = irq_loc_index;
1528            cluster[cluster_index]->procs++;
1529            proc_loc_index++;
1530            proc_index++;
1531            return;
1532        }
1533        else {
1534            printf("[XML ERROR] Unknown tag %s", tag);
1535            exit(1);
1536        }
1537        status = xmlTextReaderRead(reader);
1538    }
1539} // end procNode()
1540
1541
1542//////////////////////////////////////////
1543void psegNode(xmlTextReaderPtr reader) {
1544    unsigned int ok;
1545    paddr_t      ll_value;
1546    char * str;
1547
1548    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) {
1549        return;
1550    }
1551
1552    if (pseg_index >= MAX_PSEGS) {
1553        printf("[XML ERROR] The number of psegs is larger than %d\n", MAX_PSEGS);
1554        exit(1);
1555    }
1556
1557#if XML_PARSER_DEBUG
1558    printf("    pseg %d\n", pseg_index);
1559#endif
1560
1561    pseg[pseg_index] = (mapping_pseg_t *) malloc(sizeof(mapping_pseg_t));
1562
1563    /////// get name attribute
1564    str = getStringValue(reader, "name", &ok);
1565#if XML_PARSER_DEBUG
1566    printf("      name = %s\n", str);
1567#endif
1568    if (ok) {
1569        strncpy(pseg[pseg_index]->name, str, 31);
1570    }
1571    else {
1572        printf("[XML ERROR] illegal or missing <name> for pseg %d in cluster %d\n",
1573                pseg_index, cluster_index);
1574        exit(1);
1575    }
1576
1577    //////// get type attribute
1578    str = getStringValue(reader, "type", &ok);
1579#if XML_PARSER_DEBUG
1580    printf("      type = %s\n", str);
1581#endif
1582    if      (ok && (strcmp(str, "RAM" ) == 0)) { pseg[pseg_index]->type = PSEG_TYPE_RAM; }
1583    else if (ok && (strcmp(str, "ROM" ) == 0)) { pseg[pseg_index]->type = PSEG_TYPE_ROM; }
1584    else if (ok && (strcmp(str, "PERI") == 0)) { pseg[pseg_index]->type = PSEG_TYPE_PERI; }
1585    else {
1586        printf("[XML ERROR] illegal or missing <type> for pseg %s in cluster %d\n",
1587                pseg[pseg_index]->name, cluster_index);
1588        exit(1);
1589    }
1590
1591    //////// get base attribute
1592    ll_value = getPaddrValue(reader, "base", &ok);
1593#if XML_PARSER_DEBUG
1594    printf("      base = 0x%llx\n", ll_value);
1595#endif
1596    if (ok) {
1597        pseg[pseg_index]->base = ll_value;
1598    }
1599    else {
1600        printf("[XML ERROR] illegal or missing <base> for pseg %s in cluster %d\n",
1601                pseg[pseg_index]->name, cluster_index);
1602        exit(1);
1603    }
1604
1605    //////// get length attribute
1606    ll_value = getPaddrValue(reader, "length", &ok);
1607#if XML_PARSER_DEBUG
1608    printf("      length = 0x%llx\n", ll_value);
1609#endif
1610    if (ok) {
1611        pseg[pseg_index]->length = ll_value;
1612    } 
1613    else {
1614        printf("[XML ERROR] illegal or missing <length> for pseg %s in cluster %d\n",
1615                pseg[pseg_index]->name, cluster_index);
1616        exit(1);
1617    }
1618
1619    //////// set cluster attribute
1620    pseg[pseg_index]->cluster = cluster_index;
1621
1622    pseg_index++;
1623    cluster[cluster_index]->psegs++;
1624} // end psegNode()
1625
1626
1627/////////////////////////////////////////
1628void clusterNode(xmlTextReaderPtr reader) 
1629{
1630    unsigned int ok;
1631    unsigned int value;
1632
1633    cluster[cluster_index] = (mapping_cluster_t *) malloc(sizeof(mapping_cluster_t));
1634
1635    //initialise all variables
1636    //they will be incremented by *Node() functions
1637    //FIXME: calloc?
1638    cluster[cluster_index]->psegs = 0;
1639    cluster[cluster_index]->procs = 0;
1640    cluster[cluster_index]->coprocs = 0;
1641    cluster[cluster_index]->periphs = 0;
1642
1643
1644    //initialise global variables
1645    //TODO: delete those three
1646    proc_loc_index = 0;
1647    coproc_loc_index = 0;
1648    periph_loc_index = 0;
1649
1650    // for replicated periph
1651    found_timer = 0;
1652    found_icu = 0;
1653    found_dma = 0;
1654
1655    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) {
1656        return;
1657    }
1658
1659    // checking source file consistency
1660    if (cluster_index >= header->clusters) {
1661        printf("[XML ERROR] The cluster index is too large : %d\n", cluster_index);
1662        exit(1);
1663    }
1664
1665#if XML_PARSER_DEBUG
1666    printf("  cluster %d\n", cluster_index);
1667#endif
1668
1669
1670    /////////// check cluster index attribute (optional)
1671    value = getIntValue(reader, "index", &ok);
1672    if (ok && (value != cluster_index)) {
1673        printf("[XML ERROR] wrong cluster index / expected value is %d", 
1674                cluster_index);
1675        exit(1);
1676    }
1677
1678    ////////// set offsets
1679    cluster[cluster_index]->pseg_offset = pseg_index;
1680    cluster[cluster_index]->proc_offset = proc_index;
1681    cluster[cluster_index]->coproc_offset = coproc_index;
1682    cluster[cluster_index]->periph_offset = periph_index;
1683
1684#if XML_PARSER_DEBUG
1685    printf("    pseg_offset   = %d\n", pseg_index);
1686    printf("    proc_offset   = %d\n", proc_index);
1687    printf("    coproc_offset = %d\n", coproc_index);
1688    printf("    periph_offset = %d\n", coproc_index);
1689#endif
1690
1691    ////////// get psegs, procs, coprocs and periphs
1692    int status = xmlTextReaderRead(reader);
1693
1694    while (status == 1) 
1695    {
1696        const char * tag = (const char *) xmlTextReaderConstName(reader);
1697
1698        if      (strcmp(tag, "pseg")     == 0) psegNode(reader);
1699        else if (strcmp(tag, "proc")     == 0) procNode(reader);
1700        else if (strcmp(tag, "coproc")   == 0) coprocNode(reader);
1701        else if (strcmp(tag, "periph")   == 0) periphNode(reader);
1702        else if (strcmp(tag, "#text")    == 0) { }
1703        else if (strcmp(tag, "#comment") == 0) { }
1704        else if (strcmp(tag, "cluster")  == 0) 
1705        {
1706
1707            ////////////////// peripherals checks /////////////////////////
1708            if ((found_timer  && use_xicu) || (!found_timer  && !use_xicu)) 
1709            {
1710                printf("[XML ERROR] illegal or missing timer peripheral in cluster %d\n", cluster_index);
1711                exit(1);
1712            }
1713
1714            if (!found_icu) 
1715            {
1716                printf("[XML ERROR] illegal or missing icu peripheral in cluster %d\n", cluster_index);
1717                exit(1);
1718            }
1719
1720            if (!found_dma) 
1721            {
1722                printf("[XML ERROR] illegal or missing dma peripheral in cluster %d\n", cluster_index);
1723                exit(1);
1724            }
1725
1726
1727            if (nb_proc_max < cluster[cluster_index]->procs) 
1728            {
1729                nb_proc_max = cluster[cluster_index]->procs;
1730            }
1731
1732#if XML_PARSER_DEBUG
1733            printf("    psegs   = %d\n", cluster[cluster_index]->psegs);
1734            printf("    procs   = %d\n", cluster[cluster_index]->procs);
1735            printf("    coprocs = %d\n", cluster[cluster_index]->coprocs);
1736            printf("    periphs = %d\n", cluster[cluster_index]->periphs);
1737            printf("    end cluster %d\n", cluster_index);
1738#endif
1739            cluster_index++;
1740            return;
1741        }
1742        status = xmlTextReaderRead(reader);
1743    }
1744} // end clusterNode()
1745
1746
1747//////////////////////////////////////////////
1748void clusterSetNode(xmlTextReaderPtr reader) 
1749{
1750    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) { return; }
1751
1752#if XML_PARSER_DEBUG
1753    printf("\n  clusters set\n");
1754#endif
1755
1756    int status = xmlTextReaderRead(reader);
1757    while (status == 1) 
1758    {
1759        const char * tag = (const char *) xmlTextReaderConstName(reader);
1760
1761        if      (strcmp(tag, "cluster")    == 0) { clusterNode(reader); }
1762        else if (strcmp(tag, "#text")      == 0) { }
1763        else if (strcmp(tag, "#comment")   == 0) { }
1764        else if (strcmp(tag, "clusterset") == 0) 
1765        {
1766            // checking source file consistency
1767            if (cluster_index != header->clusters) 
1768            {
1769                printf("[XML ERROR] Wrong number of clusters\n");
1770                exit(1);
1771            }
1772
1773            // At least one TTY terminal for system boot
1774            if (header->tty_cluster == 0xFFFFFFFF) 
1775            {
1776                printf("[XML ERROR] illegal or missing tty peripheral");
1777                exit(1);
1778            }
1779
1780#if XML_PARSER_DEBUG
1781            printf("  end cluster set\n\n");
1782#endif
1783            header->psegs = pseg_index;
1784            header->procs = proc_index;
1785            header->irqs = irq_index;
1786            header->coprocs = coproc_index;
1787            header->cp_ports = cp_port_index;
1788            header->periphs = periph_index;
1789            return;
1790        }
1791        else {
1792            printf("[XML ERROR] Unknown tag in clusterset node : %s",tag);
1793            exit(1);
1794        }
1795        status = xmlTextReaderRead(reader);
1796    }
1797} // end clusterSetNode()
1798
1799
1800/////////////////////////////////////////////
1801void globalSetNode(xmlTextReaderPtr reader) {
1802    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) {
1803        return;
1804    }
1805
1806#if XML_PARSER_DEBUG
1807    printf("  globals set\n");
1808#endif
1809
1810    int status = xmlTextReaderRead(reader);
1811    while (status == 1) 
1812    {
1813        const char * tag = (const char *) xmlTextReaderConstName(reader);
1814
1815        if      (strcmp(tag, "vseg")      == 0) { vsegNode(reader); }
1816        else if (strcmp(tag, "#text")     == 0) { }
1817        else if (strcmp(tag, "#comment")  == 0) { }
1818        else if (strcmp(tag, "globalset") == 0) {
1819#if XML_PARSER_DEBUG
1820            printf("  end global set\n\n");
1821#endif
1822            header->globals = vseg_index;
1823            vseg_loc_index = 0;
1824            return;
1825        }
1826        else {
1827            printf("[XML ERROR] Unknown tag in globalset node : %s",tag);
1828            exit(1);
1829        }
1830        status = xmlTextReaderRead(reader);
1831    }
1832} // end globalSetNode()
1833
1834
1835/////////////////////////////////////////////
1836void vspaceSetNode(xmlTextReaderPtr reader) {
1837    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) {
1838        return;
1839    }
1840
1841#if XML_PARSER_DEBUG
1842    printf("\n  vspaces set\n");
1843#endif
1844
1845    int status = xmlTextReaderRead ( reader );
1846    while (status == 1) {
1847        const char * tag = (const char *) xmlTextReaderConstName(reader);
1848
1849        if (strcmp(tag, "vspace") == 0) {
1850            vspaceNode(reader);
1851        }
1852        else if (strcmp(tag, "#text"    ) == 0 ) { }
1853        else if (strcmp(tag, "#comment" ) == 0 ) { }
1854        else if (strcmp(tag, "vspaceset") == 0 ) {
1855            // checking source file consistency
1856            if (vspace_index != header->vspaces) {
1857                printf("[XML ERROR] Wrong number of vspaces\n");
1858                exit(1);
1859            }
1860            else {
1861                header->vsegs = vseg_index;
1862                header->vobjs = vobj_index;
1863                header->tasks = task_index;
1864                return;
1865            }
1866        }
1867        else {
1868            printf("[XML ERROR] Unknown tag in vspaceset node : %s",tag);
1869            exit(1);
1870        }
1871        status = xmlTextReaderRead(reader);
1872    }
1873} // end globalSetNode()
1874
1875
1876//////////////////////////////////////////
1877void headerNode(xmlTextReaderPtr reader) 
1878{
1879    char * name;
1880    unsigned int value;
1881    unsigned int ok;
1882
1883    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) { return; }
1884
1885#if XML_PARSER_DEBUG
1886    printf("mapping_info\n");
1887#endif
1888
1889    header = (mapping_header_t *) malloc(sizeof(mapping_header_t));
1890
1891    ////////// get name attribute
1892    name = getStringValue(reader, "name", &ok);
1893    if (ok) {
1894#if XML_PARSER_DEBUG
1895        printf("  name = %s\n", name);
1896#endif
1897        strncpy( header->name, name, 31);
1898    }
1899    else {
1900        printf("[XML ERROR] illegal or missing <name> attribute in header\n");
1901        exit(1);
1902    }
1903
1904    /////////// get cluster_x attribute
1905    cluster_x = getIntValue(reader, "cluster_x", &ok);
1906    if (ok) {
1907#if XML_PARSER_DEBUG
1908        printf("  cluster_x = %d\n", cluster_x);
1909#endif
1910        header->cluster_x = cluster_x;
1911    }
1912    else {
1913        printf("[XML ERROR] illegal or missing <cluster_x> attribute in header\n");
1914        exit(1);
1915    }
1916
1917    /////////// get cluster_y attribute
1918    cluster_y = getIntValue(reader, "cluster_y", &ok);
1919    if (ok) {
1920#if XML_PARSER_DEBUG
1921        printf("  cluster_y = %d\n", cluster_y);
1922#endif
1923        header->cluster_y = cluster_y;
1924    }
1925    else {
1926        printf("[XML ERROR] illegal or missing <cluster_y> attribute in header\n");
1927        exit(1);
1928    }
1929
1930    //check the number of cluster
1931    value = cluster_x * cluster_y;
1932    if (value >= MAX_CLUSTERS) {
1933        printf("[XML ERROR] The number of clusters is larger than %d\n", MAX_CLUSTERS);
1934        exit(1);
1935    }
1936
1937    header->clusters  = value;
1938
1939#if XML_PARSER_DEBUG
1940    printf("  clusters = %d\n", value);
1941#endif
1942
1943    ///////// get vspaces attribute
1944    value = getIntValue(reader, "vspaces", &ok);
1945    if (ok) {
1946        if (value >= MAX_VSPACES) {
1947            printf("[XML ERROR] The number of vspaces is larger than %d\n", MAX_VSPACES);
1948            exit(1);
1949        }
1950#if XML_PARSER_DEBUG
1951        printf("  vspaces = %d\n", value);
1952#endif
1953        header->vspaces  = value;
1954    }
1955    else {
1956        printf("[XML ERROR] illegal or missing <vspaces> attribute in mapping\n");
1957        exit(1);
1958    }
1959
1960    //////// initialise non replicated peripherals cluster index
1961    header->tty_cluster = 0xFFFFFFFF;
1962    header->nic_cluster = 0xFFFFFFFF;
1963    header->ioc_cluster = 0xFFFFFFFF;
1964    header->fbf_cluster = 0xFFFFFFFF;
1965    header->cma_cluster = 0xFFFFFFFF;
1966    header->iob_cluster = 0xFFFFFFFF;
1967    header->tty_cluster_bis = 0xFFFFFFFF;
1968    header->nic_cluster_bis = 0xFFFFFFFF;
1969    header->ioc_cluster_bis = 0xFFFFFFFF;
1970    header->fbf_cluster_bis = 0xFFFFFFFF;
1971    header->cma_cluster_bis = 0xFFFFFFFF;
1972    header->iob_cluster_bis = 0xFFFFFFFF;
1973
1974    ///////// set signature
1975    header->signature = IN_MAPPING_SIGNATURE;
1976
1977    int status = xmlTextReaderRead(reader);
1978    while (status == 1) {
1979        const char * tag = (const char *) xmlTextReaderConstName(reader);
1980
1981        if (strcmp(tag, "clusterset") == 0) {
1982            clusterSetNode(reader);
1983        }
1984        else if (strcmp(tag, "globalset")    == 0) { globalSetNode(reader); }
1985        else if (strcmp(tag, "vspaceset")    == 0) { vspaceSetNode(reader); }
1986        else if (strcmp(tag, "#text")        == 0) { }
1987        else if (strcmp(tag, "#comment")     == 0) { }
1988        else if (strcmp(tag, "mapping_info") == 0) {
1989#if XML_PARSER_DEBUG
1990            printf("end mapping_info\n");
1991#endif
1992            return;
1993        }
1994        else {
1995            printf("[XML ERROR] Unknown tag in header node : %s\n",tag);
1996            exit(1);
1997        }
1998        status = xmlTextReaderRead(reader);
1999    }
2000} // end headerNode()
2001
2002
2003///////////////////////////////////////
2004void BuildTable(int fdout, const char * type, unsigned int nb_elem,
2005                unsigned int elem_size, char ** table) {
2006    unsigned int i;
2007    // write element
2008    for (i = 0; i < nb_elem; i++) {
2009        if (elem_size != write(fdout, table[i], elem_size)) {
2010            printf("function %s: %s(%d) write  error \n", __FUNCTION__, type, i);
2011            exit(1);
2012        }
2013
2014#if XML_PARSER_DEBUG
2015        printf("Building binary: writing %s %d\n", type, i);
2016#endif
2017    }
2018}
2019
2020
2021int open_file(const char * file_path) {
2022
2023    //open file
2024    int fdout = open( file_path, (O_CREAT | O_RDWR), (S_IWUSR | S_IRUSR) );
2025    if (fdout < 0) {
2026        perror("open");
2027        exit(1);
2028    }
2029
2030    //reinitialise the file
2031    if (ftruncate(fdout, 0)) {
2032        perror("truncate");
2033        exit(1);
2034    }
2035
2036    //#if XML_PARSER_DEBUG
2037    printf("%s\n", file_path);
2038    //#endif
2039
2040    return fdout;
2041}
2042
2043
2044///////////////////////////
2045void buildBin(const char * file_path) {
2046    unsigned int length;
2047
2048    int fdout = open_file(file_path);
2049
2050    // write header to binary file
2051    length = write(fdout, (char *) header, sizeof(mapping_header_t));
2052    if (length != sizeof(mapping_header_t)) {
2053        printf("write header error : length = %d \n", length);
2054        exit(1);
2055    }
2056
2057    // write clusters
2058    BuildTable(fdout, "cluster", cluster_index, sizeof(mapping_cluster_t), (char **) cluster);
2059    // write psegs
2060    BuildTable(fdout, "pseg", pseg_index, sizeof(mapping_pseg_t), (char **) pseg);
2061    // write vspaces
2062    BuildTable(fdout, "vspace", vspace_index, sizeof(mapping_vspace_t), (char **) vspace);
2063    // write vsegs
2064    BuildTable(fdout, "vseg", vseg_index, sizeof(mapping_vseg_t), (char **) vseg);
2065    // write vobjs
2066    BuildTable(fdout, "vobj", vobj_index, sizeof(mapping_vobj_t), (char **) vobj);
2067    // write tasks array
2068    BuildTable(fdout, "task", task_index, sizeof(mapping_task_t), (char **) task);
2069    //building procs array
2070    BuildTable(fdout, "proc", proc_index, sizeof(mapping_proc_t), (char **) proc);
2071    //building irqs array
2072    BuildTable(fdout, "irq", irq_index, sizeof(mapping_irq_t), (char **) irq);
2073    //building coprocs array
2074    BuildTable(fdout, "coproc", coproc_index, sizeof(mapping_coproc_t), (char **) coproc);
2075    //building cp_ports array
2076    BuildTable(fdout, "cp_port", cp_port_index, sizeof(mapping_cp_port_t),(char **) cp_port);
2077    //building periphs array
2078    BuildTable(fdout, "periph", periph_index, sizeof(mapping_periph_t), (char **) periph);
2079
2080    close(fdout);
2081
2082} // end buildBin()
2083
2084
2085///////////////////////////////////////////////////////////////////////
2086// this function set the value the vobj_id fiels of all cp_ports
2087///////////////////////////////////////////////////////////////////////
2088void prepareBuild() 
2089{
2090    unsigned int i;
2091    //asign for all cp_ports the correct vspaceid and vobjid
2092    for (i = 0; i < cp_port_index; i++) {
2093        int vspace_id = getVspaceId(cp_port_vobj_ref[i]->vspace_name);
2094        if (vspace_id < 0) {
2095            printf("[XML ERROR] illegal  <vspacename> for cp_port %d,\n", i);
2096            exit(1);
2097        }
2098        cp_port[i]->vspaceid = vspace_id;
2099
2100        int vobj_id = getVobjLocId(vspace_id, cp_port_vobj_ref[i]->vobj_name, vspace[vspace_id]->vobjs);
2101        if (vobj_id >= 0) {
2102
2103#if XML_PARSER_DEBUG
2104            printf("\ncp_port = %d\n", i);
2105            printf("      vspace_name  = %s\n", cp_port_vobj_ref[i]->vspace_name);
2106            printf("      vobj_name    = %s\n", cp_port_vobj_ref[i]->vobj_name);
2107            printf("      vobj_index   = %d\n", vobj_id);
2108#endif
2109            cp_port[i]->mwmr_vobjid = vobj_id;
2110
2111            assert((vobj[ vspace[vspace_id]->vobj_offset + vobj_id]->type == VOBJ_TYPE_MWMR)
2112                    && "coproc ports have to refer to a vobj of type MWMR");
2113        }
2114        else {
2115            printf("[XML ERROR] illegal  <vobjname> for cp_port %d,\n", i);
2116            exit(1);
2117        }
2118    }
2119}
2120
2121//////////////////////////////////////////
2122void file_write(int fdout, char * towrite) 
2123{
2124    unsigned int size = strlen(towrite);
2125    if (size != write(fdout, towrite, size)) 
2126    {
2127        printf("file_write error");
2128        exit(1);
2129    }
2130}
2131
2132//////////////////////////////////////////////////
2133void def_int_write(int fdout, char * def, int num) 
2134{
2135    char  buf[64];
2136    sprintf(buf, "#define\t %s  %d\n", def, num);
2137    file_write(fdout, buf);
2138}
2139
2140//////////////////////////////////////////////////
2141void def_hex_write(int fdout, char * def, int num) 
2142{
2143    char  buf[64];
2144    sprintf(buf, "#define\t %s  0x%x\n", def, num);
2145    file_write(fdout, buf);
2146}
2147
2148///////////////////////////////////
2149void  genHd(const char * file_path) 
2150{
2151    int fdout = open_file(file_path);
2152
2153    char * prol   = "/* Generated from the mapping_info file */\n\n";
2154    char * ifdef  = "#ifndef _HD_CONFIG_H\n#define _HD_CONFIG_H\n\n";
2155    char * epil   = "\n#endif //_HD_CONFIG_H";
2156
2157    file_write(fdout, prol);
2158    file_write(fdout, ifdef);
2159
2160    def_int_write(fdout, "CLUSTER_X         ", cluster_x);
2161    def_int_write(fdout, "CLUSTER_Y         ", cluster_y);
2162    def_int_write(fdout, "NB_CLUSTERS       ", cluster_index);
2163    def_int_write(fdout, "NB_PROCS_MAX      ", nb_proc_max);
2164    def_int_write(fdout, "NB_TASKS_MAX      ", nb_tasks_max);
2165
2166    file_write(fdout, "\n");
2167
2168    def_int_write(fdout, "NB_TIM_CHANNELS   ", tim_channels);
2169    def_int_write(fdout, "NB_DMA_CHANNELS   ", dma_channels);
2170
2171    file_write(fdout, "\n");
2172
2173    def_int_write(fdout, "NB_TTY_CHANNELS   ", tty_channels);
2174    def_int_write(fdout, "NB_IOC_CHANNELS   ", ioc_channels);
2175    def_int_write(fdout, "NB_NIC_CHANNELS   ", nic_channels);
2176    def_int_write(fdout, "NB_CMA_CHANNELS   ", cma_channels);
2177
2178    file_write(fdout, "\n");
2179
2180    def_int_write(fdout, "USE_XICU          ", use_xicu);
2181    def_int_write(fdout, "IOMMU_ACTIVE      ", io_mmu_active);
2182
2183    file_write(fdout, epil);
2184
2185    close(fdout);
2186}
2187
2188////////////////////////////////////////////////////////
2189void ld_write(int fdout, char * seg, unsigned int addr) 
2190{
2191    char buf[64];
2192    sprintf(buf, "%s = 0x%x;\n", seg, addr);
2193    file_write(fdout, buf);
2194}
2195
2196//////////////////////////////////
2197void genLd(const char * file_path) 
2198{
2199    int          fdout = open_file(file_path);
2200    unsigned int count = 0;
2201    unsigned int vseg_id;
2202
2203    char * prol = "/* Generated from the mapping_info file */\n\n";
2204    file_write(fdout, prol);
2205
2206    // boot and kernel segments
2207    for (vseg_id = 0 ; vseg_id < header->vsegs ; vseg_id++)
2208    {
2209        if ( strcmp(vseg[vseg_id]->name, "seg_boot_code") == 0 )
2210        {
2211            ld_write(fdout, "seg_boot_code_base      ",  vseg[vseg_id]->vbase);
2212            count++;
2213        }
2214        else if ( strcmp(vseg[vseg_id]->name, "seg_boot_stack") == 0 )
2215        {
2216            ld_write(fdout, "seg_boot_stack_base     ",  vseg[vseg_id]->vbase);
2217            count++;
2218        }
2219        else if ( strcmp(vseg[vseg_id]->name, "seg_boot_mapping") == 0 )
2220        {
2221            ld_write(fdout, "seg_mapping_base        ",  vseg[vseg_id]->vbase);
2222            count++;
2223        }
2224        else if ( strcmp(vseg[vseg_id]->name, "seg_kernel_code") == 0 )
2225        {
2226            ld_write(fdout, "seg_kernel_code_base    ",  vseg[vseg_id]->vbase);
2227            count++;
2228        }
2229        else if ( strcmp(vseg[vseg_id]->name, "seg_kernel_data") == 0 )
2230        {
2231            ld_write(fdout, "seg_kernel_data_base    ",  vseg[vseg_id]->vbase);
2232            count++;
2233        }
2234        else if ( strcmp(vseg[vseg_id]->name, "seg_kernel_uncdata") == 0 )
2235        {
2236            ld_write(fdout, "seg_kernel_uncdata_base ",  vseg[vseg_id]->vbase);
2237            count++;
2238        }
2239        else if ( strcmp(vseg[vseg_id]->name, "seg_kernel_init") == 0 )
2240        {
2241            ld_write(fdout, "seg_kernel_init_base    ",  vseg[vseg_id]->vbase);
2242            count++;
2243        }
2244    }
2245    if ( count != 7 )
2246    { 
2247        printf ("[XML ERROR] Missing Boot or Kernel vseg : only %d\n", count);
2248        printf ("Mandatory segments are :\n");
2249        printf (" - seg_boot_code\n");
2250        printf (" - seg_boot_stack\n");
2251        printf (" - seg_boot_mapping\n");
2252        printf (" - seg_kernel_code\n");
2253        printf (" - seg_kernel_data\n");
2254        printf (" - seg_kernel_uncdata\n");
2255        printf (" - seg_kernel_init\n");
2256    }
2257
2258    file_write(fdout, "\n");
2259
2260    // non replicated peripherals
2261    ld_write(fdout, "\nseg_fbf_base            ", periph_vbase_array[PERIPH_TYPE_FBF]);
2262    ld_write(fdout, "seg_ioc_base            ",   periph_vbase_array[PERIPH_TYPE_IOC]);
2263    ld_write(fdout, "seg_nic_base            ",   periph_vbase_array[PERIPH_TYPE_NIC]);
2264    ld_write(fdout, "seg_tty_base            ",   periph_vbase_array[PERIPH_TYPE_TTY]);
2265    ld_write(fdout, "seg_gcd_base            ",   periph_vbase_array[PERIPH_TYPE_GCD]);
2266    ld_write(fdout, "seg_iob_base            ",   periph_vbase_array[PERIPH_TYPE_IOB]);
2267
2268    file_write(fdout, "\n");
2269
2270    // replicated peripherals
2271    ld_write(fdout, "seg_icu_base            ",   periph_vbase_array[PERIPH_TYPE_ICU]);
2272    ld_write(fdout, "seg_dma_base            ",   periph_vbase_array[PERIPH_TYPE_DMA]);
2273    ld_write(fdout, "seg_tim_base            ",   periph_vbase_array[PERIPH_TYPE_TIM]);
2274
2275    close(fdout);
2276}
2277
2278//////////////////////////////////////////////////////
2279char * buildPath(const char * path, const char * name) 
2280{
2281    char * res = calloc(strlen(path) + strlen(name) + 1, 1);
2282    strcat(res, path);
2283    strcat(res, "/");
2284    strcat(res, name);
2285    return res; 
2286}
2287
2288
2289//////////////////////////////////
2290int main(int argc, char * argv[]) 
2291{
2292    if (argc < 3) {
2293        printf("Usage: xml2bin <input_file_path> <output_path>\n");
2294        return 1;
2295    }
2296
2297    struct stat dir_st;
2298    if (stat( argv[2], &dir_st)) {
2299        perror("bad path");
2300        exit(1);
2301    }
2302
2303    if ((dir_st.st_mode & S_IFDIR) == 0) {
2304        printf("path is not a dir: %s", argv[2] );
2305        exit(1);
2306    }
2307
2308
2309    char * map_path = buildPath(argv[2], "map.bin"); 
2310    char * ld_path = buildPath(argv[2], "giet_vsegs.ld"); 
2311    char * hd_path = buildPath(argv[2], "hard_config.h"); 
2312
2313    LIBXML_TEST_VERSION;
2314
2315    int status;
2316    xmlTextReaderPtr reader = xmlReaderForFile(argv[1], NULL, 0);
2317
2318    if (reader != NULL) {
2319        status = xmlTextReaderRead (reader);
2320        while (status == 1) {
2321            const char * tag = (const char *) xmlTextReaderConstName(reader);
2322
2323            if (strcmp(tag, "mapping_info") == 0) { 
2324                headerNode(reader);
2325                prepareBuild();
2326                buildBin(map_path);
2327                genHd(hd_path);
2328                genLd(ld_path);
2329            }
2330            else {
2331                printf("[XML ERROR] Wrong file type: \"%s\"\n", argv[1]);
2332                return 1;
2333            }
2334            status = xmlTextReaderRead(reader);
2335        }
2336        xmlFreeTextReader(reader);
2337
2338        if (status != 0) {
2339            printf("[XML ERROR] Wrong Syntax in \"%s\" file\n", argv[1]);
2340            return 1;
2341        }
2342    }
2343    return 0;
2344} // end main()
2345
2346
2347// Local Variables:
2348// tab-width: 4
2349// c-basic-offset: 4
2350// c-file-offsets:((innamespace . 0)(inline-open . 0))
2351// indent-tabs-mode: nil
2352// End:
2353// vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4
2354
Note: See TracBrowser for help on using the repository browser.