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

Subversion Repositories or1k

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

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
                if (++firstthree == 1 && strcmp(coffscnhdr.s_name, ".text") != 0) {
314
                        printf("First section should be .text (%s instead)\n", coffscnhdr.s_name);
315
                        exit(1);
316
                }
317
                if (firstthree == 2 && strcmp(coffscnhdr.s_name, ".data") != 0) {
318
                        printf("Second section should be .data (%s instead)\n", coffscnhdr.s_name);
319
                        exit(1);
320
                }
321
                if (firstthree == 3 && strcmp(coffscnhdr.s_name, ".bss") != 0) {
322
                        printf("Third section should be .bss (%s instead)\n", coffscnhdr.s_name);
323
                        exit(1);
324
                }
325 28 lampret
 
326 41 lampret
                /* loading section */
327
                freemem = COFF_LONG_H(coffscnhdr.s_vaddr);
328 67 lampret
                debug("Starting to load at 0x%x", freemem);
329 41 lampret
                if (fseek(inputfs, COFF_LONG_H(coffscnhdr.s_scnptr), SEEK_SET) == -1) {
330
                        fclose(inputfs);
331
                        perror("readfile_coff");
332
                        exit(1);
333 28 lampret
                }
334 41 lampret
                while (sectsize > 0 && (len = fread(&inputbuf, sizeof(inputbuf), 1, inputfs))) {
335
                        insn = COFF_LONG_H(inputbuf);
336 138 markom
                        len = insn_len (insn_decode (insn));
337
                        if (len == 2)
338
                          {
339
                            fseek(inputfs, -2, SEEK_CUR);
340
                            debug("readfile_coff: %x 0x%x   ", sectsize, insn >> 16);
341
                          }
342 41 lampret
                        else
343 138 markom
                          debug("readfile_coff: %x 0x%x   ", sectsize, insn);
344
                        addprogram (freemem, insn, &breakpoint);
345 41 lampret
                        sectsize -= len;
346 138 markom
                }
347 28 lampret
        }
348 41 lampret
        if (firstthree < 3) {
349
                printf("One or more missing sections. At least");
350
                printf(" three sections expected (.text, .data, .bss).\n");
351
                exit(1);
352
        }
353
        if (firstthree > 3) {
354
                printf("Warning: one or more extra sections. These");
355
                printf(" sections were handled as .data sections.\n");
356
        }
357
 
358 28 lampret
        fclose(inputfs);
359
        printf("Finished loading COFF.\n");
360
        return;
361
}
362
 
363
/* Load symbols from big-endian COFF file. */
364
 
365
void readsyms_coff(char *filename, unsigned long symptr, long syms)
366
{
367
        FILE *inputfs;
368
        struct COFF_syment coffsymhdr;
369 123 markom
        int breakpoint = 0;
370 28 lampret
 
371
        if (!(inputfs = fopen(filename, "r"))) {
372
                perror("readsyms_coff");
373
                exit(1);
374
        }
375
 
376
        if (fseek(inputfs, symptr, SEEK_SET) == -1) {
377
                fclose(inputfs);
378
                perror("readsyms_coff");
379
                exit(1);
380
        }
381
 
382
        while(syms--) {
383
                if (fread(&coffsymhdr, COFF_SYMESZ, 1, inputfs) != 1) {
384
                        fclose(inputfs);
385
                        perror("readsyms_coff");
386
                        exit(1);
387
                }
388 41 lampret
                debug("Symbol: %s,", coffsymhdr.e.e_name);
389
                debug(" val: 0x%.8x,", COFF_LONG_H(coffsymhdr.e_value));
390
                debug(" auxs: %c\n", coffsymhdr.e_numaux);
391 42 lampret
                if (strlen(coffsymhdr.e.e_name) && strlen(coffsymhdr.e.e_name) < 9)
392 123 markom
                        addlabel(coffsymhdr.e.e_name, COFF_LONG_H(coffsymhdr.e_value),&breakpoint);
393 28 lampret
        }
394
 
395
        fclose(inputfs);
396
        printf("Finished loading symbols.\n");
397
        return;
398
}
399
 
400
/* Identify file type and call appropriate readfile_X routine. It only
401
handles orX-coff-big executables at the moment. */
402
 
403
void identifyfile(char *filename)
404
{
405
        FILE *inputfs;
406
        struct COFF_filehdr coffhdr;
407
        size_t len;
408
 
409
        if (!(inputfs = fopen(filename, "r"))) {
410 67 lampret
                fprintf(stderr, "xx %s", filename);
411
                perror("identifyfile1");
412
                fflush(stdout);
413
                fflush(stderr);
414 28 lampret
                exit(1);
415
        }
416
 
417
        if (fread(&coffhdr, sizeof(coffhdr), 1, inputfs) == 1) {
418
                if (COFF_SHORT_H(coffhdr.f_magic) == 0x17a) {
419
                        unsigned long opthdr_size;
420
                        printf("COFF magic: 0x%.4x\n", COFF_SHORT_H(coffhdr.f_magic));
421
                        printf("COFF flags: 0x%.4x\n", COFF_SHORT_H(coffhdr.f_flags));
422
                        printf("COFF symptr: 0x%.8x\n", COFF_LONG_H(coffhdr.f_symptr));
423
                        if ((COFF_SHORT_H(coffhdr.f_flags) & COFF_F_EXEC) != COFF_F_EXEC) {
424
                                printf("This COFF is not an executable.\n");
425
                                exit(1);
426
                        }
427
                        opthdr_size = COFF_SHORT_H(coffhdr.f_opthdr);
428
                        if (opthdr_size != sizeof(COFF_AOUTHDR)) {
429
                                printf("COFF optional header is missing or not recognized.\n");
430
                                printf("COFF f_opthdr: 0x%.2x\n", opthdr_size);
431
                                exit(1);
432
                        }
433
                        fclose(inputfs);
434
                        readfile_coff(filename, COFF_SHORT_H(coffhdr.f_nscns));
435
                        readsyms_coff(filename, COFF_LONG_H(coffhdr.f_symptr), COFF_LONG_H(coffhdr.f_nsyms));
436
                        return;
437 2 cvs
                }
438 28 lampret
                else {
439 138 markom
                        printf("Not COFF, quiting.\n");
440 28 lampret
                        fclose(inputfs);
441 138 markom
                        exit (1);
442 28 lampret
                }
443 2 cvs
        }
444 67 lampret
        else {
445
                printf("yy %s", filename);
446
                perror("identifyfile2");
447
        }
448
 
449 28 lampret
        fclose(inputfs);
450
 
451 2 cvs
        return;
452
}
453
 
454 67 lampret
 
455
/* Loads file to memory starting at address startaddr and returns freemem. */
456
unsigned long loadcode(char *filename, unsigned long startaddr, unsigned long virtphy_transl)
457 2 cvs
{
458 123 markom
  int breakpoint = 0;
459
 
460 67 lampret
        transl_error = 0;
461
        transl_table = virtphy_transl;
462
        freemem = startaddr;
463
        printf("loadcode: filename %s  startaddr=%x  virtphy_transl=%x", filename, startaddr, virtphy_transl);
464 28 lampret
        identifyfile(filename);
465 67 lampret
        if (transl_error)
466
                return -1;
467
        else
468 123 markom
                return translate(freemem,&breakpoint);
469 2 cvs
}

powered by: WebSVN 2.1.0

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