OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_61/] [or1ksim/] [cpu/] [common/] [parse.c] - Blame information for rev 167

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 138 markom
/* parce.c -- Architecture independent load
2 2 cvs
   Copyright (C) 1999 Damjan Lampret, lampret@opencores.org
3
 
4
This file is part of OpenRISC 1000 Architectural Simulator.
5
 
6
This program is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2 of the License, or
9
(at your option) any later version.
10
 
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
GNU General Public License for more details.
15
 
16
You should have received a copy of the GNU General Public License
17
along with this program; if not, write to the Free Software
18
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
 
20
#include <stdio.h>
21
#include <ctype.h>
22
#include <string.h>
23
#include <stdlib.h>
24
 
25
#include "abstract.h"
26 18 lampret
#include "arch.h"
27 67 lampret
#include "dmmu.h"
28 28 lampret
#include "coff.h"
29 138 markom
#include "debug_unit.h"
30 134 markom
#include "opcode/or32.h"
31 138 markom
#include "parse.h"
32 2 cvs
 
33
#define MAXLINE_LEN     18000
34
 
35 28 lampret
extern char *disassembled;
36
 
37 2 cvs
/* Unused mem memory marker. It is used when allocating program and data memory
38
   during parsing */
39
unsigned int freemem;
40
 
41 67 lampret
/* Translation table provided by microkernel. Only used if simulating microkernel. */
42
static unsigned long transl_table;
43
 
44
/* Used to signal whether during loading of programs a translation fault occured. */
45
static unsigned long transl_error;
46
 
47
 
48 2 cvs
int nonempty(char *line)
49
{
50
        int i;
51
 
52
        for(i = 0; i < strlen(line); i++)
53
                if (!isspace(line[i]))
54
                        return(1);
55
        return(0);
56
}
57
 
58
int nondigit(char *line)
59
{
60
        int i;
61
 
62
        for(i = 0; i < strlen(line); i++)
63
                if (!isdigit(line[i]))
64
                        return(1);
65
        return(0);
66
}
67
 
68
char *strtoken(char *in, char *out, int which)
69
{
70
        char    *super;
71
        char    *sub;
72
        char    *newline;
73
 
74
        super = strdup(in);
75
        sub = strtok(super, " \t");
76
        while (sub && --which)
77
                sub = strtok(NULL, " \t");
78
        if (sub && !which) {
79
                if ((newline = strchr(sub, '\n')))
80
                        newline[0] = '\0';
81
                strcpy(out, sub);
82
        } else
83
                out[0] = '\0';
84
        free(super);
85
        if ((newline = strchr(out, '\r')))      /* get rid of CR */
86
                newline[0] = '\0';
87
        return(out);
88
}
89
 
90 67 lampret
/* Used only by the simulator loader to translate logical addresses int ophysical.
91
   If loadcode() is called with valid virtphy_transl pointer to a table of
92
   translations then translate() performs translation otherwise phy address is
93
   equal to logical. */
94 123 markom
static unsigned int translate(unsigned int laddr,int* breakpoint)
95 67 lampret
{
96
        int i;
97
 
98
        /* No translation (i.e. when loading kernel into simulator)
99
/*      printf("transl_table=%x  laddr=%x\n", transl_table, laddr);
100
        printf("laddr=%x\n", laddr);*/
101
        if (transl_table == 0)
102
                return laddr;
103
 
104
        /* Try to find our translation in the table. */
105
        for(i = 0; i < (MEMORY_LEN / PAGE_SIZE) * 16; i += 16)
106 123 markom
                if ((laddr & ~(PAGE_SIZE - 1)) == evalsim_mem32(transl_table + i,breakpoint)) {
107 67 lampret
                        setsim_mem32(transl_table + i + 8, -2); /* Page modified */
108 123 markom
                        printf("found paddr=%x\n", evalsim_mem32(transl_table + i + 4,breakpoint) | (laddr & (PAGE_SIZE - 1)));
109
                        return (unsigned long)evalsim_mem32(transl_table + i + 4,breakpoint) | (laddr & (unsigned long)(PAGE_SIZE - 1));
110 67 lampret
                }
111
 
112
        /* Allocate new phy page for us. */
113
        for(i = 0; i < (MEMORY_LEN / PAGE_SIZE) * 16; i += 16)
114 123 markom
                if (evalsim_mem32(transl_table + i + 8,breakpoint) == 0) {
115 67 lampret
                        setsim_mem32(transl_table + i, laddr & ~(PAGE_SIZE - 1)); /* VPN */
116
                        setsim_mem32(transl_table + i + 4, (i/16) * PAGE_SIZE); /* PPN */
117
                        setsim_mem32(transl_table + i + 8, -2); /* Page modified */
118 123 markom
                        printf("newly allocated ppn=%x\n", (unsigned long)evalsim_mem32(transl_table + i + 4,breakpoint));
119 67 lampret
                        printf("newly allocated .ppn=%x\n", (unsigned long)transl_table + i + 4);
120
                        printf("newly allocated ofs=%x\n", (unsigned long)(laddr & (PAGE_SIZE - 1)));
121 123 markom
                        printf("newly allocated paddr=%x\n", (unsigned long)evalsim_mem32(transl_table + i + 4,breakpoint) | (laddr & (PAGE_SIZE - 1)));
122
                        return (unsigned long)evalsim_mem32(transl_table + i + 4,breakpoint) | (laddr & (unsigned long)(PAGE_SIZE - 1));
123 67 lampret
                }
124
        /* If we come this far then all phy memory is used and we can't find our page
125
           nor allocate new page. */
126
        transl_error = 1;
127
 
128
        printf("can't translate\n", laddr);
129
        exit(1);
130
        return -1;
131
}
132
 
133 123 markom
void adddatastr(char *str,int* breakpoint)
134 2 cvs
{
135
        if (str)
136
                str++;
137
        else
138
                return;
139
 
140 123 markom
        for(; *str && *str != '\"'; str++, translate(freemem++,breakpoint))
141 2 cvs
                if (*str == '\\')
142
                        switch (*++str) {
143 123 markom
                                case 'n': mem[translate(freemem,breakpoint)].data = '\n';
144 2 cvs
                                        break;
145 123 markom
                                case 't': mem[translate(freemem,breakpoint)].data = '\t';
146 2 cvs
                                        break;
147 123 markom
                                case 'r': mem[translate(freemem,breakpoint)].data = '\r';
148 2 cvs
                                        break;
149 123 markom
                                case '0': mem[translate(freemem,breakpoint)].data = '\0';
150 2 cvs
                                        break;
151
                                default: break;
152
                        }
153
                else
154 123 markom
                        mem[translate(freemem,breakpoint)].data = *str;
155 2 cvs
}
156
 
157 123 markom
/* Modified by CZ 26/05/01 */
158
/* Added code for new mode operation */
159
void adddataword(char *item,int* breakpoint)
160 2 cvs
{
161 26 lampret
        unsigned long num;
162 2 cvs
 
163 26 lampret
        if (isdigit(*item))
164 36 lampret
                num = strtoul(item, NULL, 0);
165 26 lampret
        else
166
                num = eval_label(item);
167
 
168 123 markom
        debug("adddataword: [0x%x] <= %x\n", translate(freemem,breakpoint), num);
169
        mem[translate(freemem,breakpoint)].data = (char) (num >> 24);
170
        mem[translate(freemem + 1,breakpoint)].data = (char) (num >> 16);
171
        mem[translate(freemem + 2,breakpoint)].data = (char) (num >> 8);
172
        mem[translate(freemem + 3,breakpoint)].data = (char) (num);
173 67 lampret
 
174 123 markom
        if(!GlobalMode)
175
          freemem += 4;
176 2 cvs
}
177
 
178 123 markom
void adddatahalf(char *item,int* breakpoint)
179 2 cvs
{
180 26 lampret
        unsigned long num;
181 2 cvs
 
182 26 lampret
        if (isdigit(*item))
183 36 lampret
                num = strtoul(item, NULL, 0);
184 26 lampret
        else
185
                num = eval_label(item);
186
 
187 123 markom
        mem[translate(freemem,breakpoint)].data = (char) (num >> 8);
188
        mem[translate(freemem + 1,breakpoint)].data = (char) (num);
189 26 lampret
 
190 2 cvs
        freemem += 2;
191
}
192
 
193 123 markom
void adddatabyte(char *item,int* breakpoint)
194 2 cvs
{
195 26 lampret
        unsigned long num;
196 2 cvs
 
197 26 lampret
        if (isdigit(*item))
198 36 lampret
                num = strtoul(item, NULL, 0);
199 26 lampret
        else
200
                num = eval_label(item);
201
 
202 123 markom
        mem[translate(freemem,breakpoint)].data = (char) (num);
203 26 lampret
 
204 2 cvs
        freemem++;
205
}
206
 
207
void adddataspace(char *num)
208
{
209
        freemem += atol(num);
210
}
211
 
212 123 markom
void addlabel(char *label, unsigned long freemem,int* breakpoint)
213 2 cvs
{
214 28 lampret
        struct label_entry **tmp;
215 2 cvs
 
216 123 markom
        debug("Adding label %s at 0x%x\n", label, translate(freemem,breakpoint));
217
        tmp = &mem[translate(freemem,breakpoint)].label;
218 28 lampret
        for (; *tmp; tmp = &((*tmp)->next));
219
        *tmp = malloc(sizeof(**tmp));
220
        (*tmp)->name = malloc(strlen(label)+1);
221
        strcpy((*tmp)->name, label);
222
        (*tmp)->next = NULL;
223
 
224 2 cvs
        return;
225
}
226
 
227 28 lampret
char null_str[1] = "\0";
228
 
229 123 markom
/* Modified by CZ 26/05/01 */
230
/* Replaced several calls to translate(freemem) with vaddr */
231
/* Added new mode execution code */
232
/* Changed parameters so address can be passed as argument */
233 138 markom
void addprogram(unsigned long address, unsigned long insn, int* breakpoint)
234 2 cvs
{
235 138 markom
  char insn_first2_char[3];
236
  int vaddr = GlobalMode ? translate(address,breakpoint) : translate(freemem,breakpoint);
237
 
238
  debug("addprogram 1\n");
239 18 lampret
 
240 138 markom
  *breakpoint += CheckDebugUnit(DebugStoreAddress,vaddr);  /* 22/06/01 MM*/
241
  *breakpoint += CheckDebugUnit(DebugStoreData,insn);
242 28 lampret
 
243 138 markom
  setsim_mem32 (vaddr, insn);
244 2 cvs
 
245 138 markom
  if(!GlobalMode)
246
    freemem += insn_len (insn_decode (insn));
247 2 cvs
}
248
 
249 28 lampret
/* Load big-endian COFF file. At the moment it doesn't load symbols yet. */
250
 
251
void readfile_coff(char *filename, short sections)
252
{
253
        FILE *inputfs;
254
        char inputbuf[4];
255
        unsigned long insn;
256 41 lampret
        signed long sectsize;
257 28 lampret
        COFF_AOUTHDR coffaouthdr;
258
        struct COFF_scnhdr coffscnhdr;
259
        int  len;
260
        char item[MAXLINE_LEN];
261
        char item2[MAXLINE_LEN];
262 41 lampret
        int  firstthree = 0;
263 123 markom
        int breakpoint = 0;
264
 
265 28 lampret
        if (!(inputfs = fopen(filename, "r"))) {
266
                perror("readfile_coff");
267
                exit(1);
268
        }
269
 
270
        if (fseek(inputfs, sizeof(struct COFF_filehdr), SEEK_SET) == -1) {
271
                fclose(inputfs);
272
                perror("readfile_coff");
273
                exit(1);
274
        }
275
 
276
        if (fread(&coffaouthdr, sizeof(coffaouthdr), 1, inputfs) != 1) {
277
                fclose(inputfs);
278
                perror("readfile_coff");
279
                exit(1);
280
        }
281
 
282
        while(sections--) {
283 41 lampret
                long scnhdr_pos = sizeof(struct COFF_filehdr) + sizeof(coffaouthdr)
284
                                + sizeof(struct COFF_scnhdr) * firstthree;
285
                if (fseek(inputfs, scnhdr_pos, SEEK_SET) == -1) {
286
                        fclose(inputfs);
287
                        perror("readfile_coff");
288
                        exit(1);
289
                }
290 28 lampret
                if (fread(&coffscnhdr, sizeof(struct COFF_scnhdr), 1, inputfs) != 1) {
291
                        fclose(inputfs);
292
                        perror("readfile_coff");
293
                        exit(1);
294
                }
295
                printf("Section: %s,", coffscnhdr.s_name);
296 41 lampret
                printf(" vaddr: 0x%.8x,", COFF_LONG_H(coffscnhdr.s_vaddr));
297
                printf(" size: 0x%.8x,", COFF_LONG_H(coffscnhdr.s_size));
298
                printf(" scnptr: 0x%.8x\n", COFF_LONG_H(coffscnhdr.s_scnptr));
299
 
300
                sectsize = COFF_LONG_H(coffscnhdr.s_size);
301
                /* A couple of sanity checks. */
302 123 markom
                if (translate(COFF_LONG_H(coffscnhdr.s_vaddr),&breakpoint) < MEMORY_START) {
303 41 lampret
                        printf("Section %s starts out of ", coffscnhdr.s_name);
304
                        printf("memory (at %x)\n", COFF_LONG_H(coffscnhdr.s_vaddr));
305
                        exit(1);
306
                }
307 123 markom
                if (translate(COFF_LONG_H(coffscnhdr.s_vaddr) + sectsize,&breakpoint) >
308 41 lampret
                    MEMORY_START + MEMORY_LEN) {
309
                        printf("Section %s ends out of ", coffscnhdr.s_name);
310
                        printf("memory.\n");
311
                        exit(1);
312
                }
313 167 markom
#if 0
314 41 lampret
                if (++firstthree == 1 && strcmp(coffscnhdr.s_name, ".text") != 0) {
315
                        printf("First section should be .text (%s instead)\n", coffscnhdr.s_name);
316
                        exit(1);
317
                }
318
                if (firstthree == 2 && strcmp(coffscnhdr.s_name, ".data") != 0) {
319
                        printf("Second section should be .data (%s instead)\n", coffscnhdr.s_name);
320
                        exit(1);
321
                }
322
                if (firstthree == 3 && strcmp(coffscnhdr.s_name, ".bss") != 0) {
323
                        printf("Third section should be .bss (%s instead)\n", coffscnhdr.s_name);
324
                        exit(1);
325
                }
326 167 markom
#else
327
                ++firstthree;
328
#endif
329 28 lampret
 
330 41 lampret
                /* loading section */
331
                freemem = COFF_LONG_H(coffscnhdr.s_vaddr);
332 67 lampret
                debug("Starting to load at 0x%x", freemem);
333 41 lampret
                if (fseek(inputfs, COFF_LONG_H(coffscnhdr.s_scnptr), SEEK_SET) == -1) {
334
                        fclose(inputfs);
335
                        perror("readfile_coff");
336
                        exit(1);
337 28 lampret
                }
338 41 lampret
                while (sectsize > 0 && (len = fread(&inputbuf, sizeof(inputbuf), 1, inputfs))) {
339
                        insn = COFF_LONG_H(inputbuf);
340 138 markom
                        len = insn_len (insn_decode (insn));
341
                        if (len == 2)
342
                          {
343
                            fseek(inputfs, -2, SEEK_CUR);
344
                            debug("readfile_coff: %x 0x%x   ", sectsize, insn >> 16);
345
                          }
346 41 lampret
                        else
347 138 markom
                          debug("readfile_coff: %x 0x%x   ", sectsize, insn);
348
                        addprogram (freemem, insn, &breakpoint);
349 41 lampret
                        sectsize -= len;
350 138 markom
                }
351 28 lampret
        }
352 41 lampret
        if (firstthree < 3) {
353
                printf("One or more missing sections. At least");
354
                printf(" three sections expected (.text, .data, .bss).\n");
355
                exit(1);
356
        }
357
        if (firstthree > 3) {
358
                printf("Warning: one or more extra sections. These");
359
                printf(" sections were handled as .data sections.\n");
360
        }
361
 
362 28 lampret
        fclose(inputfs);
363
        printf("Finished loading COFF.\n");
364
        return;
365
}
366
 
367
/* Load symbols from big-endian COFF file. */
368
 
369
void readsyms_coff(char *filename, unsigned long symptr, long syms)
370
{
371
        FILE *inputfs;
372
        struct COFF_syment coffsymhdr;
373 123 markom
        int breakpoint = 0;
374 28 lampret
 
375
        if (!(inputfs = fopen(filename, "r"))) {
376
                perror("readsyms_coff");
377
                exit(1);
378
        }
379
 
380
        if (fseek(inputfs, symptr, SEEK_SET) == -1) {
381
                fclose(inputfs);
382
                perror("readsyms_coff");
383
                exit(1);
384
        }
385
 
386
        while(syms--) {
387
                if (fread(&coffsymhdr, COFF_SYMESZ, 1, inputfs) != 1) {
388
                        fclose(inputfs);
389
                        perror("readsyms_coff");
390
                        exit(1);
391
                }
392 41 lampret
                debug("Symbol: %s,", coffsymhdr.e.e_name);
393
                debug(" val: 0x%.8x,", COFF_LONG_H(coffsymhdr.e_value));
394
                debug(" auxs: %c\n", coffsymhdr.e_numaux);
395 42 lampret
                if (strlen(coffsymhdr.e.e_name) && strlen(coffsymhdr.e.e_name) < 9)
396 123 markom
                        addlabel(coffsymhdr.e.e_name, COFF_LONG_H(coffsymhdr.e_value),&breakpoint);
397 28 lampret
        }
398
 
399
        fclose(inputfs);
400
        printf("Finished loading symbols.\n");
401
        return;
402
}
403
 
404
/* Identify file type and call appropriate readfile_X routine. It only
405
handles orX-coff-big executables at the moment. */
406
 
407
void identifyfile(char *filename)
408
{
409
        FILE *inputfs;
410
        struct COFF_filehdr coffhdr;
411
        size_t len;
412
 
413
        if (!(inputfs = fopen(filename, "r"))) {
414 67 lampret
                fprintf(stderr, "xx %s", filename);
415
                perror("identifyfile1");
416
                fflush(stdout);
417
                fflush(stderr);
418 28 lampret
                exit(1);
419
        }
420
 
421
        if (fread(&coffhdr, sizeof(coffhdr), 1, inputfs) == 1) {
422
                if (COFF_SHORT_H(coffhdr.f_magic) == 0x17a) {
423
                        unsigned long opthdr_size;
424
                        printf("COFF magic: 0x%.4x\n", COFF_SHORT_H(coffhdr.f_magic));
425
                        printf("COFF flags: 0x%.4x\n", COFF_SHORT_H(coffhdr.f_flags));
426
                        printf("COFF symptr: 0x%.8x\n", COFF_LONG_H(coffhdr.f_symptr));
427
                        if ((COFF_SHORT_H(coffhdr.f_flags) & COFF_F_EXEC) != COFF_F_EXEC) {
428
                                printf("This COFF is not an executable.\n");
429
                                exit(1);
430
                        }
431
                        opthdr_size = COFF_SHORT_H(coffhdr.f_opthdr);
432
                        if (opthdr_size != sizeof(COFF_AOUTHDR)) {
433
                                printf("COFF optional header is missing or not recognized.\n");
434
                                printf("COFF f_opthdr: 0x%.2x\n", opthdr_size);
435
                                exit(1);
436
                        }
437
                        fclose(inputfs);
438
                        readfile_coff(filename, COFF_SHORT_H(coffhdr.f_nscns));
439
                        readsyms_coff(filename, COFF_LONG_H(coffhdr.f_symptr), COFF_LONG_H(coffhdr.f_nsyms));
440
                        return;
441 2 cvs
                }
442 28 lampret
                else {
443 138 markom
                        printf("Not COFF, quiting.\n");
444 28 lampret
                        fclose(inputfs);
445 138 markom
                        exit (1);
446 28 lampret
                }
447 2 cvs
        }
448 67 lampret
        else {
449
                printf("yy %s", filename);
450
                perror("identifyfile2");
451
        }
452
 
453 28 lampret
        fclose(inputfs);
454
 
455 2 cvs
        return;
456
}
457
 
458 67 lampret
 
459
/* Loads file to memory starting at address startaddr and returns freemem. */
460
unsigned long loadcode(char *filename, unsigned long startaddr, unsigned long virtphy_transl)
461 2 cvs
{
462 123 markom
  int breakpoint = 0;
463
 
464 67 lampret
        transl_error = 0;
465
        transl_table = virtphy_transl;
466
        freemem = startaddr;
467
        printf("loadcode: filename %s  startaddr=%x  virtphy_transl=%x", filename, startaddr, virtphy_transl);
468 28 lampret
        identifyfile(filename);
469 67 lampret
        if (transl_error)
470
                return -1;
471
        else
472 123 markom
                return translate(freemem,&breakpoint);
473 2 cvs
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.