vme.c 6.2 KB
Newer Older
1
#include <nemu.h>
Z
Zihao Yu 已提交
2 3
#include <klib.h>

4 5
#define CLINT_MMIO (RTC_ADDR - 0xbff8)

G
Guokai Chen 已提交
6
_AddressSpace kas; // Kernel address space
7 8
static void* (*pgalloc_usr)(size_t) = NULL;
static void (*pgfree_usr)(void*) = NULL;
9
static int vme_enable = 0;
Z
Zihao Yu 已提交
10

11 12
#define RANGE_LEN(start, len) RANGE((start), (start + len))

13
static const _Area segments[] = {      // Kernel memory mappings
G
Guokai Chen 已提交
14
#if defined(__ARCH_RISCV64_NOOP) || defined(__ARCH_RISCV64_XS)
G
Guokai Chen 已提交
15
  RANGE_LEN(0x80000000, 0x8000000), // PMEM
16
  RANGE_LEN(0x40600000, 0x1000),    // uart
17
  RANGE_LEN(CLINT_MMIO, 0x10000),   // clint/timer
G
Guokai Chen 已提交
18 19
  RANGE_LEN(FB_ADDR,    0x400000),  // vmem
  RANGE_LEN(SCREEN_ADDR,0x1000),    // vmem
20 21 22
  RANGE_LEN(0x3c000000, 0x4000000), // PLIC
  RANGE_LEN(0xc0000000, 0x100000),  // page table test allocates from this position
#elif defined(__ARCH_RISCV64_XS_SOUTHLAKE) || defined(__ARCH_RISCV64_XS_SOUTHLAKE_FLASH)
W
William Wang 已提交
23
  RANGE_LEN(0x2000000000, 0x8000000), // PMEM
24
  RANGE_LEN(0x1f00050000, 0x1000),    // uart
W
William Wang 已提交
25 26
  // RANGE_LEN(CLINT_MMIO, 0x10000),     // clint/timer
  // RANGE_LEN(0x1f0c000000, 0x4000000), // PLIC
27
  RANGE_LEN(0x2040000000, 0x100000),  // page table test allocates from this position
28
#else
29
  NEMU_PADDR_SPACE,
30
#if __riscv_xlen == 64
31 32
  RANGE_LEN(0xa2000000, 0x10000), // clint
#endif
33
#endif
Z
Zihao Yu 已提交
34 35
};

36 37 38 39 40
#if __riscv_xlen == 64
#define USER_SPACE RANGE(0xc0000000, 0xf0000000)
#define SATP_MODE (8ull << 60)
#define PTW_CONFIG PTW_SV39
#else
41
#define USER_SPACE RANGE(0x40000000, 0x80000000)
42 43 44
#define SATP_MODE 0x80000000
#define PTW_CONFIG PTW_SV32
#endif
45

Z
Zihao Yu 已提交
46
static inline void set_satp(void *pdir) {
47
  asm volatile("csrw satp, %0" : : "r"(SATP_MODE | PN(pdir)));
48 49 50
#if __riscv_xlen == 64
  asm volatile("sfence.vma");
#endif
Z
Zihao Yu 已提交
51 52
}

53 54 55
static inline uintptr_t get_satp() {
  uintptr_t satp;
  asm volatile("csrr %0, satp" : "=r"(satp));
56
  return satp << 12;  // the mode bits will be shifted out
57 58
}

59 60 61 62 63
static inline void *new_page() {
  void *p = pgalloc_usr(PGSIZE);
  memset(p, 0, PGSIZE);
  return p;
}
64

65 66 67 68 69 70
/*
 * Virtual Memory initialize
 * pgalloc_f: pointer of page table memory allocater, must return page-aligned address
 * pgfree_f: pointer page table memory free function
 * return 0 if success
 */
Z
Zihao Yu 已提交
71 72 73 74
int _vme_init(void* (*pgalloc_f)(size_t), void (*pgfree_f)(void*)) {
  pgalloc_usr = pgalloc_f;
  pgfree_usr = pgfree_f;

75
  kas.ptr = new_page();
76
  int i;
Z
Zihao Yu 已提交
77
  for (i = 0; i < LENGTH(segments); i ++) {
78
    void *va = segments[i].start;
G
Guokai Chen 已提交
79
    printf("va start %llx, end %llx\n", segments[i].start, segments[i].end);
80
    for (; va < segments[i].end; va += PGSIZE) {
G
Guokai Chen 已提交
81
      _map(&kas, va, va, PTE_R | PTE_W | PTE_X | PTE_A | PTE_D);
Z
Zihao Yu 已提交
82 83 84
    }
  }

85
  set_satp(kas.ptr);
86
  vme_enable = 1;
Z
Zihao Yu 已提交
87

Z
Zihao Yu 已提交
88 89 90
  return 0;
}

L
lixin 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
/*
 * Virtual Memory initialize with custom segments
 * pgalloc_f: pointer of page table memory allocater, must return page-aligned address
 * pgfree_f: pointer page table memory free function
 * custom_segments: pointer of self-defined segments
 * len: length of custom_segments
 * return 0 if success
 */
int _vme_init_custom(void* (*pgalloc_f)(size_t), void (*pgfree_f)(void*), _Area custom_segments[], int len) {
  pgalloc_usr = pgalloc_f;
  pgfree_usr = pgfree_f;

  kas.ptr = new_page();
  int i;
  for (i = 0; i < len; i ++) {
    void *va = custom_segments[i].start;
    printf("va start %llx, end %llx\n", custom_segments[i].start, custom_segments[i].end);
    for (; va < custom_segments[i].end; va += PGSIZE) {
      _map(&kas, va, va, PTE_R | PTE_W | PTE_X | PTE_A | PTE_D);
    }
  }

  set_satp(kas.ptr);
  vme_enable = 1;

  return 0;
}

119 120 121
/*
 * copy page table
 */
122
void _protect(_AddressSpace *as) {
123
  PTE *updir = new_page();
Z
Zihao Yu 已提交
124
  as->ptr = updir;
125 126
  as->area = USER_SPACE;
  as->pgsize = PGSIZE;
Z
Zihao Yu 已提交
127
  // map kernel space
128
  memcpy(updir, kas.ptr, PGSIZE);
Z
Zihao Yu 已提交
129 130
}

Z
Zihao Yu 已提交
131
void _unprotect(_AddressSpace *as) {
Z
Zihao Yu 已提交
132
}
133

134 135 136
/*
 * get current satp
 */
137
void __am_get_cur_as(_Context *c) {
138
  c->pdir = (vme_enable ? (void *)get_satp() : NULL);
Z
Zihao Yu 已提交
139
}
140

141 142 143
/*
 * switch page table to the given context
 */
144
void __am_switch(_Context *c) {
145 146
  if (vme_enable && c->pdir != NULL) {
    set_satp(c->pdir);
147
  }
Z
Zihao Yu 已提交
148
}
149

150 151
/*
 * map va to pa with prot permission with page table root as
G
Guokai Chen 已提交
152
 * Note that RISC-V allow hardware to fault when A and D bit is not set
153
 */
154
void _map(_AddressSpace *as, void *va, void *pa, int prot) {
155 156
  assert((uintptr_t)va % PGSIZE == 0);
  assert((uintptr_t)pa % PGSIZE == 0);
157 158 159
  PTE *pg_base = as->ptr;
  PTE *pte;
  int level;
160
  for (level = PTW_CONFIG.ptw_level - 1; ; level --) {
161 162
    pte = &pg_base[VPNi(PTW_CONFIG, (uintptr_t)va, level)];
    pg_base = (PTE *)PTE_ADDR(*pte);
163 164
    if (level == 0) break;
    if (!(*pte & PTE_V)) {
165
      pg_base = new_page();
166 167
      *pte = PTE_V | (PN(pg_base) << 10);
    }
Z
Zihao Yu 已提交
168
  }
169

Z
Zihao Yu 已提交
170
  if (!(*pte & PTE_V)) {
G
Guokai Chen 已提交
171
    *pte = PTE_V | prot | (PN(pa) << 10);
Z
Zihao Yu 已提交
172 173 174
  }
}

175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
/*
 * map va to pa with prot permission with page table root as
 * pagetable_level indicates page table level to be used
 * 0: basic 4KiB page
 * 1: 2MiB megapage
 * 2: 1GiB gigapage
 * Note that RISC-V allow hardware to fault when A and D bit is not set
 */
void _map_rv_hugepage(_AddressSpace *as, void *va, void *pa, int prot, int pagetable_level) {
  int hugepage_size;
  switch (pagetable_level) {
    case 0: hugepage_size = PGSIZE; break; // 4KiB
    case 1: hugepage_size = PGSIZE * 512; break;  // 2MiB
    case 2: hugepage_size = PGSIZE * 512 * 512; break;  // 1GiB
    default: assert(0);
  }
  assert((uintptr_t)va % hugepage_size == 0);
  // printf("pa %lx sz %lx\n", pa, hugepage_size);
  assert((uintptr_t)pa % hugepage_size == 0);
  PTE *pg_base = as->ptr;
  PTE *pte;
  int level;
  for (level = PTW_CONFIG.ptw_level - 1; ; level --) {
    pte = &pg_base[VPNi(PTW_CONFIG, (uintptr_t)va, level)];
    pg_base = (PTE *)PTE_ADDR(*pte);
    if (level == pagetable_level) break;
    if (!(*pte & PTE_V)) {
      pg_base = new_page();
      *pte = PTE_V | (PN(pg_base) << 10);
    }
  }

  int hugepage_pn_shift = pagetable_level * 9;
  if (!(*pte & PTE_V)) {
    *pte = PTE_V | prot | (PN(pa) >> hugepage_pn_shift << hugepage_pn_shift << 10);
  }
  printf("map huge page level %d, pte value %lx\n", pagetable_level, *pte);
}

214 215
_Context *_ucontext(_AddressSpace *as, _Area kstack, void *entry) {
  _Context *c = (_Context*)kstack.end - 1;
Z
Zihao Yu 已提交
216

217
  c->pdir = as->ptr;
218 219
  c->sepc = (uintptr_t)entry;
  c->sstatus = MSTATUS_SPP(MODE_S) | MSTATUS_PIE(MODE_S);
220
  c->gpr[2] = 1; // sp slot, used as usp, non-zero is ok
Z
Zihao Yu 已提交
221 222
  return c;
}