blob: 5bf03c796e1bc28dcced7121c48ce543310aae8f [file] [log] [blame]
Thaminda Edirisooriyab0945832015-08-26 15:28:04 -07001/*
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>
33
34#define SUPERPAGE_SIZE ((uintptr_t)(RISCV_PGSIZE << RISCV_PGLEVEL_BITS))
35#define VM_CHOICE VM_SV39
36#define VA_BITS 39
37#define MEGAPAGE_SIZE (SUPERPAGE_SIZE << RISCV_PGLEVEL_BITS)
38
39#define PROT_READ 1
40#define PROT_WRITE 2
41#define PROT_EXEC 4
42
43#define MAP_PRIVATE 0x2
44#define MAP_FIXED 0x10
45#define MAP_ANONYMOUS 0x20
46#define MAP_POPULATE 0x8000
47#define MREMAP_FIXED 0x2
48
49#define EXTRACT_FIELD(val, which) (((val) & (which)) / ((which) & ~((which)-1)))
50#define INSERT_FIELD(val, which, fieldval) (((val) & ~(which)) | ((fieldval) * ((which) & ~((which)-1))))
51
52#define supervisor_paddr_valid(start, length) \
53 ((uintptr_t)(start) >= current.first_user_vaddr + current.bias \
54 && (uintptr_t)(start) + (length) < mem_size \
55 && (uintptr_t)(start) + (length) >= (uintptr_t)(start))
56
57typedef uintptr_t pte_t;
58extern pte_t* root_page_table;
59
Thaminda Edirisooriyab0945832015-08-26 15:28:04 -070060void initVirtualMemory(void);
61
62size_t pte_ppn(pte_t pte);
63pte_t ptd_create(uintptr_t ppn);
64pte_t pte_create(uintptr_t ppn, int prot, int user);
65
66void walk_page_table(void);
67
68void init_vm(uintptr_t virtMemStart, uintptr_t physMemStart, uintptr_t pageTableStart);
69void mstatus_init(void); // need to setup mstatus so we know we have virtual memory
70
71void flush_tlb(void);
72
73#endif