Thaminda Edirisooriya | b094583 | 2015-08-26 15:28:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013, The Regents of the University of California (Regents). |
| 3 | * All Rights Reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * 3. Neither the name of the Regents nor the |
| 13 | * names of its contributors may be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, |
| 17 | * SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING |
| 18 | * OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF REGENTS HAS |
| 19 | * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 20 | * |
| 21 | * REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 22 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 23 | * PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED |
| 24 | * HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE |
| 25 | * MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
| 26 | */ |
| 27 | |
| 28 | #ifndef _VM_H |
| 29 | #define _VM_H |
| 30 | |
| 31 | #include <string.h> |
| 32 | #include <stdint.h> |
Jonathan Neuschäfer | f643d66 | 2016-08-22 19:37:15 +0200 | [diff] [blame] | 33 | #include <arch/encoding.h> |
Thaminda Edirisooriya | b094583 | 2015-08-26 15:28:04 -0700 | [diff] [blame] | 34 | |
| 35 | #define SUPERPAGE_SIZE ((uintptr_t)(RISCV_PGSIZE << RISCV_PGLEVEL_BITS)) |
| 36 | #define VM_CHOICE VM_SV39 |
| 37 | #define VA_BITS 39 |
| 38 | #define MEGAPAGE_SIZE (SUPERPAGE_SIZE << RISCV_PGLEVEL_BITS) |
| 39 | |
Thaminda Edirisooriya | b094583 | 2015-08-26 15:28:04 -0700 | [diff] [blame] | 40 | #define EXTRACT_FIELD(val, which) (((val) & (which)) / ((which) & ~((which)-1))) |
| 41 | #define INSERT_FIELD(val, which, fieldval) (((val) & ~(which)) | ((fieldval) * ((which) & ~((which)-1)))) |
| 42 | |
| 43 | #define supervisor_paddr_valid(start, length) \ |
| 44 | ((uintptr_t)(start) >= current.first_user_vaddr + current.bias \ |
| 45 | && (uintptr_t)(start) + (length) < mem_size \ |
| 46 | && (uintptr_t)(start) + (length) >= (uintptr_t)(start)) |
| 47 | |
| 48 | typedef uintptr_t pte_t; |
| 49 | extern pte_t* root_page_table; |
| 50 | |
Thaminda Edirisooriya | b094583 | 2015-08-26 15:28:04 -0700 | [diff] [blame] | 51 | void initVirtualMemory(void); |
| 52 | |
| 53 | size_t pte_ppn(pte_t pte); |
| 54 | pte_t ptd_create(uintptr_t ppn); |
| 55 | pte_t pte_create(uintptr_t ppn, int prot, int user); |
| 56 | |
Jonathan Neuschäfer | 538e446 | 2016-08-22 19:37:15 +0200 | [diff] [blame] | 57 | void print_page_table(void); |
Thaminda Edirisooriya | b094583 | 2015-08-26 15:28:04 -0700 | [diff] [blame] | 58 | |
Ronald G. Minnich | 5965cba | 2016-10-19 08:07:13 -0700 | [diff] [blame] | 59 | void init_vm(uintptr_t virtMemStart, uintptr_t physMemStart, |
| 60 | pte_t *pageTableStart); |
Thaminda Edirisooriya | b094583 | 2015-08-26 15:28:04 -0700 | [diff] [blame] | 61 | void mstatus_init(void); // need to setup mstatus so we know we have virtual memory |
| 62 | |
| 63 | void flush_tlb(void); |
| 64 | |
Jonathan Neuschäfer | f643d66 | 2016-08-22 19:37:15 +0200 | [diff] [blame] | 65 | |
| 66 | #define DEFINE_MPRV_READ(name, type, insn) \ |
| 67 | static inline type name(type *p); \ |
| 68 | static inline type name(type *p) \ |
| 69 | { \ |
| 70 | int mprv = MSTATUS_MPRV; \ |
| 71 | type value; \ |
| 72 | asm ( \ |
| 73 | "csrs mstatus, %1\n" \ |
| 74 | STRINGIFY(insn) " %0, 0(%2)\n" \ |
| 75 | "csrc mstatus, %1\n" \ |
| 76 | : "=r"(value) : "r"(mprv), "r"(p) : "memory" \ |
| 77 | ); \ |
| 78 | return value; \ |
| 79 | } |
| 80 | |
| 81 | #define DEFINE_MPRV_WRITE(name, type, insn) \ |
| 82 | static inline void name(type *p, type value); \ |
| 83 | static inline void name(type *p, type value) \ |
| 84 | { \ |
| 85 | int mprv = MSTATUS_MPRV; \ |
| 86 | asm ( \ |
| 87 | "csrs mstatus, %0\n" \ |
| 88 | STRINGIFY(insn) " %1, 0(%2)\n" \ |
| 89 | "csrc mstatus, %0\n" \ |
| 90 | :: "r"(mprv), "r"(value), "r"(p) : "memory" \ |
| 91 | ); \ |
| 92 | } |
| 93 | |
| 94 | DEFINE_MPRV_READ(mprv_read_u8, uint8_t, lbu) |
| 95 | DEFINE_MPRV_READ(mprv_read_u16, uint16_t, lhu) |
| 96 | DEFINE_MPRV_READ(mprv_read_u32, uint32_t, lwu) |
| 97 | DEFINE_MPRV_READ(mprv_read_u64, uint32_t, ld) |
| 98 | DEFINE_MPRV_READ(mprv_read_long, long, ld) |
| 99 | DEFINE_MPRV_READ(mprv_read_ulong, unsigned long, ld) |
| 100 | DEFINE_MPRV_WRITE(mprv_write_u8, uint8_t, sb) |
| 101 | DEFINE_MPRV_WRITE(mprv_write_u16, uint16_t, sh) |
| 102 | DEFINE_MPRV_WRITE(mprv_write_u32, uint32_t, sw) |
| 103 | DEFINE_MPRV_WRITE(mprv_write_u64, uint64_t, sd) |
| 104 | DEFINE_MPRV_WRITE(mprv_write_long, long, sd) |
| 105 | DEFINE_MPRV_WRITE(mprv_write_ulong, unsigned long, sd) |
| 106 | |
| 107 | #undef DEFINE_MPRV_READ |
| 108 | #undef DEFINE_MPRV_WRITE |
| 109 | |
| 110 | |
Thaminda Edirisooriya | b094583 | 2015-08-26 15:28:04 -0700 | [diff] [blame] | 111 | #endif |