source: mainline/kernel/arch/amd64/include/mm/page.h@ 68f57e1

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 68f57e1 was ee04c28, checked in by Jakub Jermar <jakub@…>, 14 years ago

Remove obsolete comment.

  • Property mode set to 100644
File size: 7.3 KB
RevLine 
[1141c1a]1/*
[df4ed85]2 * Copyright (c) 2005 Ondrej Palkovsky
[1141c1a]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
[7f1c620]29/** @addtogroup amd64mm
[b45c443]30 * @{
31 */
32/** @file
33 */
34
[06e1e95]35#ifndef KERN_amd64_PAGE_H_
36#define KERN_amd64_PAGE_H_
[1141c1a]37
[fa2d382]38#include <arch/mm/frame.h>
[7a0359b]39#include <trace.h>
[fa2d382]40
[a1f60f3]41#define PAGE_WIDTH FRAME_WIDTH
42#define PAGE_SIZE FRAME_SIZE
[d1f8a87]43
[8fc0d455]44#ifndef __ASM__
[1141c1a]45
[dc0b964]46#define KA2PA(x) (((uintptr_t) (x)) - UINT64_C(0xffff800000000000))
47#define PA2KA(x) (((uintptr_t) (x)) + UINT64_C(0xffff800000000000))
[b3f8fb7]48
[a1f60f3]49#else /* __ASM__ */
50
51#define KA2PA(x) ((x) - 0xffff800000000000)
52#define PA2KA(x) ((x) + 0xffff800000000000)
53
54#endif /* __ASM__ */
[8fc0d455]55
[c03ee1c]56/* Number of entries in each level. */
[a1f60f3]57#define PTL0_ENTRIES_ARCH 512
58#define PTL1_ENTRIES_ARCH 512
59#define PTL2_ENTRIES_ARCH 512
60#define PTL3_ENTRIES_ARCH 512
[ecbdc724]61
[c03ee1c]62/* Page table sizes for each level. */
[a1f60f3]63#define PTL0_SIZE_ARCH ONE_FRAME
64#define PTL1_SIZE_ARCH ONE_FRAME
65#define PTL2_SIZE_ARCH ONE_FRAME
66#define PTL3_SIZE_ARCH ONE_FRAME
[c03ee1c]67
68/* Macros calculating indices into page tables in each level. */
[dc0b964]69#define PTL0_INDEX_ARCH(vaddr) (((vaddr) >> 39) & 0x1ffU)
70#define PTL1_INDEX_ARCH(vaddr) (((vaddr) >> 30) & 0x1ffU)
71#define PTL2_INDEX_ARCH(vaddr) (((vaddr) >> 21) & 0x1ffU)
72#define PTL3_INDEX_ARCH(vaddr) (((vaddr) >> 12) & 0x1ffU)
[c03ee1c]73
74/* Get PTE address accessors for each level. */
75#define GET_PTL1_ADDRESS_ARCH(ptl0, i) \
76 ((pte_t *) ((((uint64_t) ((pte_t *) (ptl0))[(i)].addr_12_31) << 12) | \
77 (((uint64_t) ((pte_t *) (ptl0))[(i)].addr_32_51) << 32)))
78#define GET_PTL2_ADDRESS_ARCH(ptl1, i) \
79 ((pte_t *) ((((uint64_t) ((pte_t *) (ptl1))[(i)].addr_12_31) << 12) | \
80 (((uint64_t) ((pte_t *) (ptl1))[(i)].addr_32_51) << 32)))
81#define GET_PTL3_ADDRESS_ARCH(ptl2, i) \
82 ((pte_t *) ((((uint64_t) ((pte_t *) (ptl2))[(i)].addr_12_31) << 12) | \
83 (((uint64_t) ((pte_t *) (ptl2))[(i)].addr_32_51) << 32)))
84#define GET_FRAME_ADDRESS_ARCH(ptl3, i) \
85 ((uintptr_t *) \
86 ((((uint64_t) ((pte_t *) (ptl3))[(i)].addr_12_31) << 12) | \
87 (((uint64_t) ((pte_t *) (ptl3))[(i)].addr_32_51) << 32)))
88
89/* Set PTE address accessors for each level. */
90#define SET_PTL0_ADDRESS_ARCH(ptl0) \
91 (write_cr3((uintptr_t) (ptl0)))
92#define SET_PTL1_ADDRESS_ARCH(ptl0, i, a) \
[98000fb]93 set_pt_addr((pte_t *) (ptl0), (size_t) (i), a)
[c03ee1c]94#define SET_PTL2_ADDRESS_ARCH(ptl1, i, a) \
[98000fb]95 set_pt_addr((pte_t *) (ptl1), (size_t) (i), a)
[c03ee1c]96#define SET_PTL3_ADDRESS_ARCH(ptl2, i, a) \
[98000fb]97 set_pt_addr((pte_t *) (ptl2), (size_t) (i), a)
[c03ee1c]98#define SET_FRAME_ADDRESS_ARCH(ptl3, i, a) \
[98000fb]99 set_pt_addr((pte_t *) (ptl3), (size_t) (i), a)
[c03ee1c]100
101/* Get PTE flags accessors for each level. */
102#define GET_PTL1_FLAGS_ARCH(ptl0, i) \
[98000fb]103 get_pt_flags((pte_t *) (ptl0), (size_t) (i))
[c03ee1c]104#define GET_PTL2_FLAGS_ARCH(ptl1, i) \
[98000fb]105 get_pt_flags((pte_t *) (ptl1), (size_t) (i))
[c03ee1c]106#define GET_PTL3_FLAGS_ARCH(ptl2, i) \
[98000fb]107 get_pt_flags((pte_t *) (ptl2), (size_t) (i))
[c03ee1c]108#define GET_FRAME_FLAGS_ARCH(ptl3, i) \
[98000fb]109 get_pt_flags((pte_t *) (ptl3), (size_t) (i))
[c03ee1c]110
111/* Set PTE flags accessors for each level. */
112#define SET_PTL1_FLAGS_ARCH(ptl0, i, x) \
[98000fb]113 set_pt_flags((pte_t *) (ptl0), (size_t) (i), (x))
[c03ee1c]114#define SET_PTL2_FLAGS_ARCH(ptl1, i, x) \
[98000fb]115 set_pt_flags((pte_t *) (ptl1), (size_t) (i), (x))
[c03ee1c]116#define SET_PTL3_FLAGS_ARCH(ptl2, i, x) \
[98000fb]117 set_pt_flags((pte_t *) (ptl2), (size_t) (i), (x))
[c03ee1c]118#define SET_FRAME_FLAGS_ARCH(ptl3, i, x) \
[98000fb]119 set_pt_flags((pte_t *) (ptl3), (size_t) (i), (x))
[c03ee1c]120
121/* Macros for querying the last-level PTE entries. */
122#define PTE_VALID_ARCH(p) \
123 (*((uint64_t *) (p)) != 0)
124#define PTE_PRESENT_ARCH(p) \
125 ((p)->present != 0)
126#define PTE_GET_FRAME_ARCH(p) \
127 ((((uintptr_t) (p)->addr_12_31) << 12) | \
128 ((uintptr_t) (p)->addr_32_51 << 32))
129#define PTE_WRITABLE_ARCH(p) \
130 ((p)->writeable != 0)
131#define PTE_EXECUTABLE_ARCH(p) \
132 ((p)->no_execute == 0)
[ecbdc724]133
[8fc0d455]134#ifndef __ASM__
[db3341e]135
[a1f60f3]136#include <mm/mm.h>
137#include <arch/interrupt.h>
138#include <typedefs.h>
139
[567807b1]140/* Page fault error codes. */
141
[c03ee1c]142/** When bit on this position is 0, the page fault was caused by a not-present
143 * page.
144 */
[a1f60f3]145#define PFERR_CODE_P (1 << 0)
[567807b1]146
147/** When bit on this position is 1, the page fault was caused by a write. */
[a1f60f3]148#define PFERR_CODE_RW (1 << 1)
[567807b1]149
150/** When bit on this position is 1, the page fault was caused in user mode. */
[a1f60f3]151#define PFERR_CODE_US (1 << 2)
[567807b1]152
153/** When bit on this position is 1, a reserved bit was set in page directory. */
[a1f60f3]154#define PFERR_CODE_RSVD (1 << 3)
[567807b1]155
[c03ee1c]156/** When bit on this position os 1, the page fault was caused during instruction
157 * fecth.
158 */
[a1f60f3]159#define PFERR_CODE_ID (1 << 4)
[567807b1]160
[33be9ac]161/** Page Table Entry. */
162typedef struct {
[a1f60f3]163 unsigned int present : 1;
164 unsigned int writeable : 1;
165 unsigned int uaccessible : 1;
166 unsigned int page_write_through : 1;
167 unsigned int page_cache_disable : 1;
168 unsigned int accessed : 1;
169 unsigned int dirty : 1;
170 unsigned int unused: 1;
171 unsigned int global : 1;
172 unsigned int soft_valid : 1; /**< Valid content even if present bit is cleared. */
173 unsigned int avl : 2;
174 unsigned int addr_12_31 : 30;
175 unsigned int addr_32_51 : 21;
176 unsigned int no_execute : 1;
[33be9ac]177} __attribute__ ((packed)) pte_t;
178
[7a0359b]179NO_TRACE static inline unsigned int get_pt_flags(pte_t *pt, size_t i)
[db3341e]180{
181 pte_t *p = &pt[i];
182
[c03ee1c]183 return ((!p->page_cache_disable) << PAGE_CACHEABLE_SHIFT |
184 (!p->present) << PAGE_PRESENT_SHIFT |
185 p->uaccessible << PAGE_USER_SHIFT |
186 1 << PAGE_READ_SHIFT |
187 p->writeable << PAGE_WRITE_SHIFT |
188 (!p->no_execute) << PAGE_EXEC_SHIFT |
189 p->global << PAGE_GLOBAL_SHIFT);
[db3341e]190}
191
[7a0359b]192NO_TRACE static inline void set_pt_addr(pte_t *pt, size_t i, uintptr_t a)
[db3341e]193{
194 pte_t *p = &pt[i];
[a1f60f3]195
[dc0b964]196 p->addr_12_31 = (a >> 12) & UINT32_C(0xfffff);
[db3341e]197 p->addr_32_51 = a >> 32;
198}
199
[7a0359b]200NO_TRACE static inline void set_pt_flags(pte_t *pt, size_t i, int flags)
[db3341e]201{
202 pte_t *p = &pt[i];
203
204 p->page_cache_disable = !(flags & PAGE_CACHEABLE);
205 p->present = !(flags & PAGE_NOT_PRESENT);
206 p->uaccessible = (flags & PAGE_USER) != 0;
207 p->writeable = (flags & PAGE_WRITE) != 0;
208 p->no_execute = (flags & PAGE_EXEC) == 0;
[bfb87df]209 p->global = (flags & PAGE_GLOBAL) != 0;
[0882a9a]210
211 /*
212 * Ensure that there is at least one bit set even if the present bit is cleared.
213 */
214 p->soft_valid = 1;
[db3341e]215}
216
[1141c1a]217extern void page_arch_init(void);
[214ec25c]218extern void page_fault(unsigned int, istate_t *);
[1141c1a]219
[d1f8a87]220#endif /* __ASM__ */
221
[1141c1a]222#endif
[b45c443]223
[7f1c620]224/** @}
[b45c443]225 */
Note: See TracBrowser for help on using the repository browser.