ram.cpp 4.7 KB
Newer Older
Z
Zihao Yu 已提交
1 2
#include "common.h"

Z
Zihao Yu 已提交
3
#define RAMSIZE (128 * 1024 * 1024)
Z
Zihao Yu 已提交
4

5
static uint64_t ram[RAMSIZE / sizeof(uint64_t)];
Z
Zihao Yu 已提交
6
static long img_size = 0;
7
void* get_img_start() { return &ram[0]; }
Z
Zihao Yu 已提交
8
long get_img_size() { return img_size; }
Z
Zihao Yu 已提交
9 10
void* get_ram_start() { return &ram[0]; }
long get_ram_size() { return RAMSIZE; }
Z
Zihao Yu 已提交
11

12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
void addpageSv39() {
//three layers
//addr range: 0x0000000080000000 - 0x0000000088000000 for 128MB from 2GB - 2GB128MB
//the first layer: one entry for 1GB. (512GB in total by 512 entries). need the 2th entries
//the second layer: one entry for 2MB. (1GB in total by 512 entries). need the 0th-63rd entries
//the third layer: one entry for 4KB (2MB in total by 512 entries). need 64 with each one all  

#define PAGESIZE (4 * 1024)  // 4KB = 2^12B
#define ENTRYNUM (PAGESIZE / 8) //512 2^9
#define PTEVOLUME (PAGESIZE * ENTRYNUM) // 2MB
#define PTENUM (RAMSIZE / PTEVOLUME) // 128MB / 2MB = 64
#define PDDENUM 1
#define PDENUM 1
#define PDDEADDR (0x88000000 - (PAGESIZE * (PTENUM + 2))) //0x88000000 - 0x1000*66
#define PDEADDR (0x88000000 - (PAGESIZE * (PTENUM + 1))) //0x88000000 - 0x1000*65
#define PTEADDR(i) (0x88000000 - (PAGESIZE * PTENUM) + (PAGESIZE * i)) //0x88000000 - 0x100*64
28 29
#define PTEMMIONUM 128
#define PDEMMIONUM 1
30 31
#define PTEDEVNUM 128
#define PDEDEVNUM 1
32 33 34 35 36

  uint64_t pdde[ENTRYNUM];
  uint64_t pde[ENTRYNUM];
  uint64_t pte[PTENUM][ENTRYNUM];
  
37
  // special addr for mmio 0x40000000 - 0x4fffffff
38
  uint64_t pdemmio[ENTRYNUM];
39
  uint64_t ptemmio[PTEMMIONUM][ENTRYNUM];
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59

  // special addr for internal devices 0x30000000-0x3fffffff
  uint64_t pdedev[ENTRYNUM];
  uint64_t ptedev[PTEDEVNUM][ENTRYNUM];

  // dev: 0x30000000-0x3fffffff
  pdde[0] = (((PDDEADDR-PAGESIZE*(PDEMMIONUM+PTEMMIONUM+PDEDEVNUM)) & 0xfffff000) >> 2) | 0x1;

  for (int i = 0; i < PTEDEVNUM; i++) {
    pdedev[ENTRYNUM-PTEDEVNUM+i] = (((PDDEADDR-PAGESIZE*(PDEMMIONUM+PTEMMIONUM+PDEDEVNUM+PTEDEVNUM-i)) & 0xfffff000) >> 2) | 0x1;
  }

  for(int outidx = 0; outidx < PTEDEVNUM; outidx++) {
    for(int inidx = 0; inidx < ENTRYNUM; inidx++) {
      ptedev[outidx][inidx] = (((0x30000000 + outidx*PTEVOLUME + inidx*PAGESIZE) & 0xfffff000) >> 2) | 0xf;
    }
  }

  // mmio: 0x40000000 - 0x4fffffff
  pdde[1] = (((PDDEADDR-PAGESIZE*PDEMMIONUM) & 0xfffff000) >> 2) | 0x1;
60

61
  for(int i = 0; i < PTEMMIONUM; i++) {
Z
zhangzifei 已提交
62
    pdemmio[i] = (((PDDEADDR-PAGESIZE*(PTEMMIONUM+PDEMMIONUM-i)) & 0xfffff000) >> 2) | 0x1;
63 64 65 66
  }
  
  for(int outidx = 0; outidx < PTEMMIONUM; outidx++) {
    for(int inidx = 0; inidx < ENTRYNUM; inidx++) {
67
      ptemmio[outidx][inidx] = (((0x40000000 + outidx*PTEVOLUME + inidx*PAGESIZE) & 0xfffff000) >> 2) | 0xf;
68 69 70 71
    }
  }
  
  //0x800000000 - 0x87ffffff
Z
zhangzifei 已提交
72 73 74
  pdde[2] = ((PDEADDR & 0xfffff000) >> 2) | 0x1;
  //pdde[2] = ((0x80000000&0xc0000000) >> 2) | 0xf;

75
  for(int i = 0; i < PTENUM ;i++) {
Z
zhangzifei 已提交
76 77
    pde[i] = ((PTEADDR(i)&0xfffff000)>>2) | 0x1;
    //pde[i] = (((0x8000000+i*2*1024*1024)&0xffe00000)>>2) | 0xf;
78 79 80 81
  }

  for(int outidx = 0; outidx < PTENUM; outidx++ ) {
    for(int inidx = 0; inidx < ENTRYNUM; inidx++ ) {
82
      pte[outidx][inidx] = (((0x80000000 + outidx*PTEVOLUME + inidx*PAGESIZE) & 0xfffff000)>>2) | 0xf;
83 84
    }
  }
85

86 87
  memcpy((char *)ram+(RAMSIZE-PAGESIZE*(PTENUM+PDDENUM+PDENUM+PDEMMIONUM+PTEMMIONUM+PDEDEVNUM+PTEDEVNUM)),ptedev,PAGESIZE*PTEDEVNUM);
  memcpy((char *)ram+(RAMSIZE-PAGESIZE*(PTENUM+PDDENUM+PDENUM+PDEMMIONUM+PTEMMIONUM+PDEDEVNUM)),pdedev,PAGESIZE*PDEDEVNUM);
88 89
  memcpy((char *)ram+(RAMSIZE-PAGESIZE*(PTENUM+PDDENUM+PDENUM+PDEMMIONUM+PTEMMIONUM)),ptemmio, PAGESIZE*PTEMMIONUM);
  memcpy((char *)ram+(RAMSIZE-PAGESIZE*(PTENUM+PDDENUM+PDENUM+PDEMMIONUM)), pdemmio, PAGESIZE*PDEMMIONUM);
90 91 92 93
  memcpy((char *)ram+(RAMSIZE-PAGESIZE*(PTENUM+PDDENUM+PDENUM)), pdde, PAGESIZE*PDDENUM);
  memcpy((char *)ram+(RAMSIZE-PAGESIZE*(PTENUM+PDENUM)), pde, PAGESIZE*PDENUM);
  memcpy((char *)ram+(RAMSIZE-PAGESIZE*PTENUM), pte, PAGESIZE*PTENUM);
}
Z
zhangzifei 已提交
94

Z
Zihao Yu 已提交
95
void init_ram(const char *img) {
Z
Zihao Yu 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108
  assert(img != NULL);
  FILE *fp = fopen(img, "rb");
  if (fp == NULL) {
    printf("Can not open '%s'\n", img);
    assert(0);
  }

  printf("The image is %s\n", img);

  fseek(fp, 0, SEEK_END);
  img_size = ftell(fp);

  fseek(fp, 0, SEEK_SET);
109
  int ret = fread(ram, img_size, 1, fp);
Z
Zihao Yu 已提交
110 111
  assert(ret == 1);
  fclose(fp);
Z
Zihao Yu 已提交
112

Z
zhangzifei 已提交
113
  //new add
114
  addpageSv39();
Z
zhangzifei 已提交
115
  //new end
Z
Zihao Yu 已提交
116 117 118
}

extern "C" void ram_helper(
119
    uint64_t rIdx, uint64_t *rdata, uint64_t wIdx, uint64_t wdata, uint64_t wmask, uint8_t wen) {
120 121
  if (rIdx >= RAMSIZE / sizeof(uint64_t)) {
    printf("ERROR: ram idx = 0x%x out of bound!\n", rIdx);
122 123 124 125
    // TODO: don't allow out of bound when crossbar is ready
    //assert(rIdx < RAMSIZE / sizeof(uint64_t));
    *rdata = 0xabcd12345678dcbaUL;
    return;
126
  }
Z
Zihao Yu 已提交
127
  *rdata = ram[rIdx];
128 129 130 131
  if (wen) {
    assert(wIdx < RAMSIZE / sizeof(uint64_t));
    ram[wIdx] = (ram[wIdx] & ~wmask) | (wdata & wmask);
  }
Z
Zihao Yu 已提交
132
}