| 1 | /*
|
|---|
| 2 | * Copyright (c) 2010 Martin Decky
|
|---|
| 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
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /** @addtogroup abs32lemm
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | #ifndef KERN_abs32le_PAGE_H_
|
|---|
| 36 | #define KERN_abs32le_PAGE_H_
|
|---|
| 37 |
|
|---|
| 38 | #include <arch/mm/frame.h>
|
|---|
| 39 | #include <trace.h>
|
|---|
| 40 |
|
|---|
| 41 | #define PAGE_WIDTH FRAME_WIDTH
|
|---|
| 42 | #define PAGE_SIZE FRAME_SIZE
|
|---|
| 43 |
|
|---|
| 44 | #define KA2PA(x) (((uintptr_t) (x)) - UINT32_C(0x80000000))
|
|---|
| 45 | #define PA2KA(x) (((uintptr_t) (x)) + UINT32_C(0x80000000))
|
|---|
| 46 |
|
|---|
| 47 | /*
|
|---|
| 48 | * This is an example of 2-level page tables (PTL1 and PTL2 are left out)
|
|---|
| 49 | * on top of the generic 4-level page table interface.
|
|---|
| 50 | */
|
|---|
| 51 |
|
|---|
| 52 | /* Number of entries in each level. */
|
|---|
| 53 | #define PTL0_ENTRIES_ARCH 1024
|
|---|
| 54 | #define PTL1_ENTRIES_ARCH 0
|
|---|
| 55 | #define PTL2_ENTRIES_ARCH 0
|
|---|
| 56 | #define PTL3_ENTRIES_ARCH 1024
|
|---|
| 57 |
|
|---|
| 58 | /* Page table sizes for each level. */
|
|---|
| 59 | #define PTL0_SIZE_ARCH ONE_FRAME
|
|---|
| 60 | #define PTL1_SIZE_ARCH 0
|
|---|
| 61 | #define PTL2_SIZE_ARCH 0
|
|---|
| 62 | #define PTL3_SIZE_ARCH ONE_FRAME
|
|---|
| 63 |
|
|---|
| 64 | /* Macros calculating indices for each level. */
|
|---|
| 65 | #define PTL0_INDEX_ARCH(vaddr) (((vaddr) >> 22) & 0x3ffU)
|
|---|
| 66 | #define PTL1_INDEX_ARCH(vaddr) 0
|
|---|
| 67 | #define PTL2_INDEX_ARCH(vaddr) 0
|
|---|
| 68 | #define PTL3_INDEX_ARCH(vaddr) (((vaddr) >> 12) & 0x3ffU)
|
|---|
| 69 |
|
|---|
| 70 | /* Get PTE address accessors for each level. */
|
|---|
| 71 | #define GET_PTL1_ADDRESS_ARCH(ptl0, i) \
|
|---|
| 72 | ((pte_t *) ((((pte_t *) (ptl0))[(i)].frame_address) << 12))
|
|---|
| 73 | #define GET_PTL2_ADDRESS_ARCH(ptl1, i) \
|
|---|
| 74 | (ptl1)
|
|---|
| 75 | #define GET_PTL3_ADDRESS_ARCH(ptl2, i) \
|
|---|
| 76 | (ptl2)
|
|---|
| 77 | #define GET_FRAME_ADDRESS_ARCH(ptl3, i) \
|
|---|
| 78 | ((uintptr_t) ((((pte_t *) (ptl3))[(i)].frame_address) << 12))
|
|---|
| 79 |
|
|---|
| 80 | /* Set PTE address accessors for each level. */
|
|---|
| 81 | #define SET_PTL0_ADDRESS_ARCH(ptl0)
|
|---|
| 82 | #define SET_PTL1_ADDRESS_ARCH(ptl0, i, a) \
|
|---|
| 83 | (((pte_t *) (ptl0))[(i)].frame_address = (a) >> 12)
|
|---|
| 84 | #define SET_PTL2_ADDRESS_ARCH(ptl1, i, a)
|
|---|
| 85 | #define SET_PTL3_ADDRESS_ARCH(ptl2, i, a)
|
|---|
| 86 | #define SET_FRAME_ADDRESS_ARCH(ptl3, i, a) \
|
|---|
| 87 | (((pte_t *) (ptl3))[(i)].frame_address = (a) >> 12)
|
|---|
| 88 |
|
|---|
| 89 | /* Get PTE flags accessors for each level. */
|
|---|
| 90 | #define GET_PTL1_FLAGS_ARCH(ptl0, i) \
|
|---|
| 91 | get_pt_flags((pte_t *) (ptl0), (size_t) (i))
|
|---|
| 92 | #define GET_PTL2_FLAGS_ARCH(ptl1, i) \
|
|---|
| 93 | PAGE_PRESENT
|
|---|
| 94 | #define GET_PTL3_FLAGS_ARCH(ptl2, i) \
|
|---|
| 95 | PAGE_PRESENT
|
|---|
| 96 | #define GET_FRAME_FLAGS_ARCH(ptl3, i) \
|
|---|
| 97 | get_pt_flags((pte_t *) (ptl3), (size_t) (i))
|
|---|
| 98 |
|
|---|
| 99 | /* Set PTE flags accessors for each level. */
|
|---|
| 100 | #define SET_PTL1_FLAGS_ARCH(ptl0, i, x) \
|
|---|
| 101 | set_pt_flags((pte_t *) (ptl0), (size_t) (i), (x))
|
|---|
| 102 | #define SET_PTL2_FLAGS_ARCH(ptl1, i, x)
|
|---|
| 103 | #define SET_PTL3_FLAGS_ARCH(ptl2, i, x)
|
|---|
| 104 | #define SET_FRAME_FLAGS_ARCH(ptl3, i, x) \
|
|---|
| 105 | set_pt_flags((pte_t *) (ptl3), (size_t) (i), (x))
|
|---|
| 106 |
|
|---|
| 107 | /* Set PTE present bit accessors for each level. */
|
|---|
| 108 | #define SET_PTL1_PRESENT_ARCH(ptl0, i) \
|
|---|
| 109 | set_pt_present((pte_t *) (ptl0), (size_t) (i))
|
|---|
| 110 | #define SET_PTL2_PRESENT_ARCH(ptl1, i)
|
|---|
| 111 | #define SET_PTL3_PRESENT_ARCH(ptl2, i)
|
|---|
| 112 | #define SET_FRAME_PRESENT_ARCH(ptl3, i) \
|
|---|
| 113 | set_pt_present((pte_t *) (ptl3), (size_t) (i))
|
|---|
| 114 |
|
|---|
| 115 | /* Macros for querying the last level entries. */
|
|---|
| 116 | #define PTE_VALID_ARCH(p) \
|
|---|
| 117 | (*((uint32_t *) (p)) != 0)
|
|---|
| 118 | #define PTE_PRESENT_ARCH(p) \
|
|---|
| 119 | ((p)->present != 0)
|
|---|
| 120 | #define PTE_GET_FRAME_ARCH(p) \
|
|---|
| 121 | ((p)->frame_address << FRAME_WIDTH)
|
|---|
| 122 | #define PTE_WRITABLE_ARCH(p) \
|
|---|
| 123 | ((p)->writeable != 0)
|
|---|
| 124 | #define PTE_EXECUTABLE_ARCH(p) 1
|
|---|
| 125 |
|
|---|
| 126 | #include <mm/mm.h>
|
|---|
| 127 | #include <arch/interrupt.h>
|
|---|
| 128 | #include <typedefs.h>
|
|---|
| 129 |
|
|---|
| 130 | /** Page Table Entry. */
|
|---|
| 131 | typedef struct {
|
|---|
| 132 | unsigned int present : 1;
|
|---|
| 133 | unsigned int writeable : 1;
|
|---|
| 134 | unsigned int uaccessible : 1;
|
|---|
| 135 | unsigned int page_write_through : 1;
|
|---|
| 136 | unsigned int page_cache_disable : 1;
|
|---|
| 137 | unsigned int accessed : 1;
|
|---|
| 138 | unsigned int dirty : 1;
|
|---|
| 139 | unsigned int pat : 1;
|
|---|
| 140 | unsigned int global : 1;
|
|---|
| 141 |
|
|---|
| 142 | /** Valid content even if the present bit is not set. */
|
|---|
| 143 | unsigned int soft_valid : 1;
|
|---|
| 144 | unsigned int avl : 2;
|
|---|
| 145 | unsigned int frame_address : 20;
|
|---|
| 146 | } __attribute__((packed)) pte_t;
|
|---|
| 147 |
|
|---|
| 148 | NO_TRACE static inline unsigned int get_pt_flags(pte_t *pt, size_t i)
|
|---|
| 149 | REQUIRES_ARRAY_MUTABLE(pt, PTL0_ENTRIES_ARCH)
|
|---|
| 150 | {
|
|---|
| 151 | pte_t *p = &pt[i];
|
|---|
| 152 |
|
|---|
| 153 | return (
|
|---|
| 154 | ((unsigned int) (!p->page_cache_disable) << PAGE_CACHEABLE_SHIFT) |
|
|---|
| 155 | ((unsigned int) (!p->present) << PAGE_PRESENT_SHIFT) |
|
|---|
| 156 | ((unsigned int) p->uaccessible << PAGE_USER_SHIFT) |
|
|---|
| 157 | (1 << PAGE_READ_SHIFT) |
|
|---|
| 158 | ((unsigned int) p->writeable << PAGE_WRITE_SHIFT) |
|
|---|
| 159 | (1 << PAGE_EXEC_SHIFT) |
|
|---|
| 160 | ((unsigned int) p->global << PAGE_GLOBAL_SHIFT)
|
|---|
| 161 | );
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | NO_TRACE static inline void set_pt_flags(pte_t *pt, size_t i, int flags)
|
|---|
| 165 | WRITES(ARRAY_RANGE(pt, PTL0_ENTRIES_ARCH))
|
|---|
| 166 | REQUIRES_ARRAY_MUTABLE(pt, PTL0_ENTRIES_ARCH)
|
|---|
| 167 | {
|
|---|
| 168 | pte_t *p = &pt[i];
|
|---|
| 169 |
|
|---|
| 170 | p->page_cache_disable = !(flags & PAGE_CACHEABLE);
|
|---|
| 171 | p->present = !(flags & PAGE_NOT_PRESENT);
|
|---|
| 172 | p->uaccessible = (flags & PAGE_USER) != 0;
|
|---|
| 173 | p->writeable = (flags & PAGE_WRITE) != 0;
|
|---|
| 174 | p->global = (flags & PAGE_GLOBAL) != 0;
|
|---|
| 175 |
|
|---|
| 176 | /*
|
|---|
| 177 | * Ensure that there is at least one bit set even if the present bit is
|
|---|
| 178 | * cleared.
|
|---|
| 179 | */
|
|---|
| 180 | p->soft_valid = true;
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | NO_TRACE static inline void set_pt_present(pte_t *pt, size_t i)
|
|---|
| 184 | WRITES(ARRAY_RANGE(pt, PTL0_ENTRIES_ARCH))
|
|---|
| 185 | REQUIRES_ARRAY_MUTABLE(pt, PTL0_ENTRIES_ARCH)
|
|---|
| 186 | {
|
|---|
| 187 | pte_t *p = &pt[i];
|
|---|
| 188 |
|
|---|
| 189 | p->present = 1;
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | extern void page_arch_init(void);
|
|---|
| 193 | extern void page_fault(unsigned int, istate_t *);
|
|---|
| 194 |
|
|---|
| 195 | #endif
|
|---|
| 196 |
|
|---|
| 197 | /** @}
|
|---|
| 198 | */
|
|---|