ia64/xen-unstable

view extras/mini-os/include/x86/arch_mm.h @ 16838:945820bfedb6

minios: POSIX fixes
Fixes some functions which are POSIX. Also make them ifndef HAVE_LIBC.

Signed-off-by: Samuel Thibault <samuel.thibault@eu.citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Tue Jan 22 14:20:22 2008 +0000 (2008-01-22)
parents 420f98121491
children 898ce9341e6b
line source
1 /* -*- Mode:C; c-basic-offset:4; tab-width:4 -*-
2 *
3 * (C) 2003 - Rolf Neugebauer - Intel Research Cambridge
4 * Copyright (c) 2005, Keir A Fraser
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
25 #ifndef _ARCH_MM_H_
26 #define _ARCH_MM_H_
28 #ifndef __ASSEMBLY__
29 #include <xen/xen.h>
30 #if defined(__i386__)
31 #include <xen/arch-x86_32.h>
32 #elif defined(__x86_64__)
33 #include <xen/arch-x86_64.h>
34 #else
35 #error "Unsupported architecture"
36 #endif
37 #endif
39 #define L1_FRAME 1
40 #define L2_FRAME 2
41 #define L3_FRAME 3
43 #define L1_PAGETABLE_SHIFT 12
45 #if defined(__i386__)
47 #if !defined(CONFIG_X86_PAE)
49 #define L2_PAGETABLE_SHIFT 22
51 #define L1_PAGETABLE_ENTRIES 1024
52 #define L2_PAGETABLE_ENTRIES 1024
54 #define PADDR_BITS 32
55 #define PADDR_MASK (~0UL)
57 #define NOT_L1_FRAMES 1
58 #define PRIpte "08lx"
59 #ifndef __ASSEMBLY__
60 typedef unsigned long pgentry_t;
61 #endif
63 #else /* defined(CONFIG_X86_PAE) */
65 #define L2_PAGETABLE_SHIFT 21
66 #define L3_PAGETABLE_SHIFT 30
68 #define L1_PAGETABLE_ENTRIES 512
69 #define L2_PAGETABLE_ENTRIES 512
70 #define L3_PAGETABLE_ENTRIES 4
72 #define PADDR_BITS 44
73 #define PADDR_MASK ((1ULL << PADDR_BITS)-1)
75 #define L2_MASK ((1UL << L3_PAGETABLE_SHIFT) - 1)
77 /*
78 * If starting from virtual address greater than 0xc0000000,
79 * this value will be 2 to account for final mid-level page
80 * directory which is always mapped in at this location.
81 */
82 #define NOT_L1_FRAMES 3
83 #define PRIpte "016llx"
84 #ifndef __ASSEMBLY__
85 typedef uint64_t pgentry_t;
86 #endif
88 #endif /* !defined(CONFIG_X86_PAE) */
90 #elif defined(__x86_64__)
92 #define L2_PAGETABLE_SHIFT 21
93 #define L3_PAGETABLE_SHIFT 30
94 #define L4_PAGETABLE_SHIFT 39
96 #define L1_PAGETABLE_ENTRIES 512
97 #define L2_PAGETABLE_ENTRIES 512
98 #define L3_PAGETABLE_ENTRIES 512
99 #define L4_PAGETABLE_ENTRIES 512
101 /* These are page-table limitations. Current CPUs support only 40-bit phys. */
102 #define PADDR_BITS 52
103 #define VADDR_BITS 48
104 #define PADDR_MASK ((1UL << PADDR_BITS)-1)
105 #define VADDR_MASK ((1UL << VADDR_BITS)-1)
107 #define L2_MASK ((1UL << L3_PAGETABLE_SHIFT) - 1)
108 #define L3_MASK ((1UL << L4_PAGETABLE_SHIFT) - 1)
110 #define NOT_L1_FRAMES 3
111 #define PRIpte "016lx"
112 #ifndef __ASSEMBLY__
113 typedef unsigned long pgentry_t;
114 #endif
116 #endif
118 #define L1_MASK ((1UL << L2_PAGETABLE_SHIFT) - 1)
120 /* Given a virtual address, get an entry offset into a page table. */
121 #define l1_table_offset(_a) \
122 (((_a) >> L1_PAGETABLE_SHIFT) & (L1_PAGETABLE_ENTRIES - 1))
123 #define l2_table_offset(_a) \
124 (((_a) >> L2_PAGETABLE_SHIFT) & (L2_PAGETABLE_ENTRIES - 1))
125 #if defined(__x86_64__) || defined(CONFIG_X86_PAE)
126 #define l3_table_offset(_a) \
127 (((_a) >> L3_PAGETABLE_SHIFT) & (L3_PAGETABLE_ENTRIES - 1))
128 #endif
129 #if defined(__x86_64__)
130 #define l4_table_offset(_a) \
131 (((_a) >> L4_PAGETABLE_SHIFT) & (L4_PAGETABLE_ENTRIES - 1))
132 #endif
134 #define _PAGE_PRESENT 0x001UL
135 #define _PAGE_RW 0x002UL
136 #define _PAGE_USER 0x004UL
137 #define _PAGE_PWT 0x008UL
138 #define _PAGE_PCD 0x010UL
139 #define _PAGE_ACCESSED 0x020UL
140 #define _PAGE_DIRTY 0x040UL
141 #define _PAGE_PAT 0x080UL
142 #define _PAGE_PSE 0x080UL
143 #define _PAGE_GLOBAL 0x100UL
145 #if defined(__i386__)
146 #define L1_PROT (_PAGE_PRESENT|_PAGE_RW|_PAGE_ACCESSED)
147 #define L1_PROT_RO (_PAGE_PRESENT|_PAGE_ACCESSED)
148 #define L2_PROT (_PAGE_PRESENT|_PAGE_RW|_PAGE_ACCESSED|_PAGE_DIRTY |_PAGE_USER)
149 #if defined(CONFIG_X86_PAE)
150 #define L3_PROT (_PAGE_PRESENT)
151 #endif /* CONFIG_X86_PAE */
152 #elif defined(__x86_64__)
153 #define L1_PROT (_PAGE_PRESENT|_PAGE_RW|_PAGE_ACCESSED|_PAGE_USER)
154 #define L1_PROT_RO (_PAGE_PRESENT|_PAGE_ACCESSED|_PAGE_USER)
155 #define L2_PROT (_PAGE_PRESENT|_PAGE_RW|_PAGE_ACCESSED|_PAGE_DIRTY|_PAGE_USER)
156 #define L3_PROT (_PAGE_PRESENT|_PAGE_RW|_PAGE_ACCESSED|_PAGE_DIRTY|_PAGE_USER)
157 #define L4_PROT (_PAGE_PRESENT|_PAGE_RW|_PAGE_ACCESSED|_PAGE_DIRTY|_PAGE_USER)
158 #endif /* __i386__ || __x86_64__ */
160 #ifdef __ASSEMBLY__
161 #define PAGE_SIZE (1 << L1_PAGETABLE_SHIFT)
162 #else
163 #ifndef CONFIG_X86_PAE
164 #define PAGE_SIZE (1UL << L1_PAGETABLE_SHIFT)
165 #else
166 #define PAGE_SIZE (1ULL << L1_PAGETABLE_SHIFT)
167 #endif
168 #endif
169 #define PAGE_SHIFT L1_PAGETABLE_SHIFT
170 #define PAGE_MASK (~(PAGE_SIZE-1))
172 #define PFN_UP(x) (((x) + PAGE_SIZE-1) >> L1_PAGETABLE_SHIFT)
173 #define PFN_DOWN(x) ((x) >> L1_PAGETABLE_SHIFT)
174 #define PFN_PHYS(x) ((x) << L1_PAGETABLE_SHIFT)
175 #define PHYS_PFN(x) ((x) >> L1_PAGETABLE_SHIFT)
177 /* to align the pointer to the (next) page boundary */
178 #define PAGE_ALIGN(addr) (((addr)+PAGE_SIZE-1)&PAGE_MASK)
180 #define STACK_SIZE_PAGE_ORDER 1
181 #define STACK_SIZE (PAGE_SIZE * (1 << STACK_SIZE_PAGE_ORDER))
183 #ifndef __ASSEMBLY__
184 /* Definitions for machine and pseudophysical addresses. */
185 #ifdef CONFIG_X86_PAE
186 typedef unsigned long long paddr_t;
187 typedef unsigned long long maddr_t;
188 #else
189 typedef unsigned long paddr_t;
190 typedef unsigned long maddr_t;
191 #endif
193 extern unsigned long *phys_to_machine_mapping;
194 extern char _text, _etext, _erodata, _edata, _end;
195 extern unsigned long mfn_zero;
196 #define pfn_to_mfn(_pfn) (phys_to_machine_mapping[(_pfn)])
197 static __inline__ maddr_t phys_to_machine(paddr_t phys)
198 {
199 maddr_t machine = pfn_to_mfn(phys >> PAGE_SHIFT);
200 machine = (machine << PAGE_SHIFT) | (phys & ~PAGE_MASK);
201 return machine;
202 }
204 #define mfn_to_pfn(_mfn) (machine_to_phys_mapping[(_mfn)])
205 static __inline__ paddr_t machine_to_phys(maddr_t machine)
206 {
207 paddr_t phys = mfn_to_pfn(machine >> PAGE_SHIFT);
208 phys = (phys << PAGE_SHIFT) | (machine & ~PAGE_MASK);
209 return phys;
210 }
211 #endif
213 #define VIRT_START ((unsigned long)&_text)
215 #define to_phys(x) ((unsigned long)(x)-VIRT_START)
216 #define to_virt(x) ((void *)((unsigned long)(x)+VIRT_START))
218 #define virt_to_pfn(_virt) (PFN_DOWN(to_phys(_virt)))
219 #define virt_to_mfn(_virt) (pfn_to_mfn(virt_to_pfn(_virt)))
220 #define mach_to_virt(_mach) (to_virt(machine_to_phys(_mach)))
221 #define virt_to_mach(_virt) (phys_to_machine(to_phys(_virt)))
222 #define mfn_to_virt(_mfn) (to_virt(mfn_to_pfn(_mfn) << PAGE_SHIFT))
223 #define pfn_to_virt(_pfn) (to_virt((_pfn) << PAGE_SHIFT))
225 /* Pagetable walking. */
226 #define pte_to_mfn(_pte) (((_pte) & (PADDR_MASK&PAGE_MASK)) >> L1_PAGETABLE_SHIFT)
227 #define pte_to_virt(_pte) to_virt(mfn_to_pfn(pte_to_mfn(_pte)) << PAGE_SHIFT)
229 #define map_frames(f, n) map_frames_ex(f, n, 1, 0, 1, DOMID_SELF, 0, L1_PROT)
230 #define map_zero(n, a) map_frames_ex(&mfn_zero, n, 0, 0, a, DOMID_SELF, 0, L1_PROT_RO)
232 #endif /* _ARCH_MM_H_ */