]> git.proxmox.com Git - qemu.git/blame - target-i386/arch_memory_mapping.c
Merge remote-tracking branch 'jliu/or32' into staging
[qemu.git] / target-i386 / arch_memory_mapping.c
CommitLineData
fae001f5
WC
1/*
2 * i386 memory mapping
3 *
4 * Copyright Fujitsu, Corp. 2011, 2012
5 *
6 * Authors:
7 * Wen Congyang <wency@cn.fujitsu.com>
8 *
fc0608ac
SW
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
fae001f5
WC
11 *
12 */
13
14#include "cpu.h"
022c62cb 15#include "exec/cpu-all.h"
9c17d615 16#include "sysemu/memory_mapping.h"
fae001f5
WC
17
18/* PAE Paging or IA-32e Paging */
a8170e5e 19static void walk_pte(MemoryMappingList *list, hwaddr pte_start_addr,
fae001f5
WC
20 int32_t a20_mask, target_ulong start_line_addr)
21{
a8170e5e 22 hwaddr pte_addr, start_paddr;
fae001f5
WC
23 uint64_t pte;
24 target_ulong start_vaddr;
25 int i;
26
27 for (i = 0; i < 512; i++) {
28 pte_addr = (pte_start_addr + i * 8) & a20_mask;
29 pte = ldq_phys(pte_addr);
30 if (!(pte & PG_PRESENT_MASK)) {
31 /* not present */
32 continue;
33 }
34
35 start_paddr = (pte & ~0xfff) & ~(0x1ULL << 63);
36 if (cpu_physical_memory_is_io(start_paddr)) {
37 /* I/O region */
38 continue;
39 }
40
bff63471 41 start_vaddr = start_line_addr | ((i & 0x1ff) << 12);
fae001f5
WC
42 memory_mapping_list_add_merge_sorted(list, start_paddr,
43 start_vaddr, 1 << 12);
44 }
45}
46
47/* 32-bit Paging */
48static void walk_pte2(MemoryMappingList *list,
a8170e5e 49 hwaddr pte_start_addr, int32_t a20_mask,
fae001f5
WC
50 target_ulong start_line_addr)
51{
a8170e5e 52 hwaddr pte_addr, start_paddr;
fae001f5
WC
53 uint32_t pte;
54 target_ulong start_vaddr;
55 int i;
56
57 for (i = 0; i < 1024; i++) {
58 pte_addr = (pte_start_addr + i * 4) & a20_mask;
59 pte = ldl_phys(pte_addr);
60 if (!(pte & PG_PRESENT_MASK)) {
61 /* not present */
62 continue;
63 }
64
65 start_paddr = pte & ~0xfff;
66 if (cpu_physical_memory_is_io(start_paddr)) {
67 /* I/O region */
68 continue;
69 }
70
71 start_vaddr = start_line_addr | ((i & 0x3ff) << 12);
72 memory_mapping_list_add_merge_sorted(list, start_paddr,
73 start_vaddr, 1 << 12);
74 }
75}
76
77/* PAE Paging or IA-32e Paging */
fbc2ed95
LC
78#define PLM4_ADDR_MASK 0xffffffffff000 /* selects bits 51:12 */
79
a8170e5e 80static void walk_pde(MemoryMappingList *list, hwaddr pde_start_addr,
fae001f5
WC
81 int32_t a20_mask, target_ulong start_line_addr)
82{
a8170e5e 83 hwaddr pde_addr, pte_start_addr, start_paddr;
fae001f5
WC
84 uint64_t pde;
85 target_ulong line_addr, start_vaddr;
86 int i;
87
88 for (i = 0; i < 512; i++) {
89 pde_addr = (pde_start_addr + i * 8) & a20_mask;
90 pde = ldq_phys(pde_addr);
91 if (!(pde & PG_PRESENT_MASK)) {
92 /* not present */
93 continue;
94 }
95
96 line_addr = start_line_addr | ((i & 0x1ff) << 21);
97 if (pde & PG_PSE_MASK) {
98 /* 2 MB page */
99 start_paddr = (pde & ~0x1fffff) & ~(0x1ULL << 63);
100 if (cpu_physical_memory_is_io(start_paddr)) {
101 /* I/O region */
102 continue;
103 }
104 start_vaddr = line_addr;
105 memory_mapping_list_add_merge_sorted(list, start_paddr,
106 start_vaddr, 1 << 21);
107 continue;
108 }
109
fbc2ed95 110 pte_start_addr = (pde & PLM4_ADDR_MASK) & a20_mask;
fae001f5
WC
111 walk_pte(list, pte_start_addr, a20_mask, line_addr);
112 }
113}
114
115/* 32-bit Paging */
116static void walk_pde2(MemoryMappingList *list,
a8170e5e 117 hwaddr pde_start_addr, int32_t a20_mask,
fae001f5
WC
118 bool pse)
119{
6ad53bdf 120 hwaddr pde_addr, pte_start_addr, start_paddr, high_paddr;
fae001f5
WC
121 uint32_t pde;
122 target_ulong line_addr, start_vaddr;
123 int i;
124
125 for (i = 0; i < 1024; i++) {
126 pde_addr = (pde_start_addr + i * 4) & a20_mask;
127 pde = ldl_phys(pde_addr);
128 if (!(pde & PG_PRESENT_MASK)) {
129 /* not present */
130 continue;
131 }
132
133 line_addr = (((unsigned int)i & 0x3ff) << 22);
134 if ((pde & PG_PSE_MASK) && pse) {
6ad53bdf
WC
135 /*
136 * 4 MB page:
137 * bits 39:32 are bits 20:13 of the PDE
138 * bit3 31:22 are bits 31:22 of the PDE
139 */
140 high_paddr = ((hwaddr)(pde & 0x1fe000) << 19);
141 start_paddr = (pde & ~0x3fffff) | high_paddr;
fae001f5
WC
142 if (cpu_physical_memory_is_io(start_paddr)) {
143 /* I/O region */
144 continue;
145 }
146 start_vaddr = line_addr;
147 memory_mapping_list_add_merge_sorted(list, start_paddr,
148 start_vaddr, 1 << 22);
149 continue;
150 }
151
152 pte_start_addr = (pde & ~0xfff) & a20_mask;
153 walk_pte2(list, pte_start_addr, a20_mask, line_addr);
154 }
155}
156
157/* PAE Paging */
158static void walk_pdpe2(MemoryMappingList *list,
a8170e5e 159 hwaddr pdpe_start_addr, int32_t a20_mask)
fae001f5 160{
a8170e5e 161 hwaddr pdpe_addr, pde_start_addr;
fae001f5
WC
162 uint64_t pdpe;
163 target_ulong line_addr;
164 int i;
165
166 for (i = 0; i < 4; i++) {
167 pdpe_addr = (pdpe_start_addr + i * 8) & a20_mask;
168 pdpe = ldq_phys(pdpe_addr);
169 if (!(pdpe & PG_PRESENT_MASK)) {
170 /* not present */
171 continue;
172 }
173
174 line_addr = (((unsigned int)i & 0x3) << 30);
175 pde_start_addr = (pdpe & ~0xfff) & a20_mask;
176 walk_pde(list, pde_start_addr, a20_mask, line_addr);
177 }
178}
179
180#ifdef TARGET_X86_64
181/* IA-32e Paging */
182static void walk_pdpe(MemoryMappingList *list,
a8170e5e 183 hwaddr pdpe_start_addr, int32_t a20_mask,
fae001f5
WC
184 target_ulong start_line_addr)
185{
a8170e5e 186 hwaddr pdpe_addr, pde_start_addr, start_paddr;
fae001f5
WC
187 uint64_t pdpe;
188 target_ulong line_addr, start_vaddr;
189 int i;
190
191 for (i = 0; i < 512; i++) {
192 pdpe_addr = (pdpe_start_addr + i * 8) & a20_mask;
193 pdpe = ldq_phys(pdpe_addr);
194 if (!(pdpe & PG_PRESENT_MASK)) {
195 /* not present */
196 continue;
197 }
198
199 line_addr = start_line_addr | ((i & 0x1ffULL) << 30);
200 if (pdpe & PG_PSE_MASK) {
201 /* 1 GB page */
202 start_paddr = (pdpe & ~0x3fffffff) & ~(0x1ULL << 63);
203 if (cpu_physical_memory_is_io(start_paddr)) {
204 /* I/O region */
205 continue;
206 }
207 start_vaddr = line_addr;
208 memory_mapping_list_add_merge_sorted(list, start_paddr,
209 start_vaddr, 1 << 30);
210 continue;
211 }
212
fbc2ed95 213 pde_start_addr = (pdpe & PLM4_ADDR_MASK) & a20_mask;
fae001f5
WC
214 walk_pde(list, pde_start_addr, a20_mask, line_addr);
215 }
216}
217
218/* IA-32e Paging */
219static void walk_pml4e(MemoryMappingList *list,
a8170e5e 220 hwaddr pml4e_start_addr, int32_t a20_mask)
fae001f5 221{
a8170e5e 222 hwaddr pml4e_addr, pdpe_start_addr;
fae001f5
WC
223 uint64_t pml4e;
224 target_ulong line_addr;
225 int i;
226
227 for (i = 0; i < 512; i++) {
228 pml4e_addr = (pml4e_start_addr + i * 8) & a20_mask;
229 pml4e = ldq_phys(pml4e_addr);
230 if (!(pml4e & PG_PRESENT_MASK)) {
231 /* not present */
232 continue;
233 }
234
235 line_addr = ((i & 0x1ffULL) << 39) | (0xffffULL << 48);
fbc2ed95 236 pdpe_start_addr = (pml4e & PLM4_ADDR_MASK) & a20_mask;
fae001f5
WC
237 walk_pdpe(list, pdpe_start_addr, a20_mask, line_addr);
238 }
239}
240#endif
241
a23bbfda
AF
242void x86_cpu_get_memory_mapping(CPUState *cs, MemoryMappingList *list,
243 Error **errp)
fae001f5 244{
a23bbfda
AF
245 X86CPU *cpu = X86_CPU(cs);
246 CPUX86State *env = &cpu->env;
247
248 if (!cpu_paging_enabled(cs)) {
fae001f5 249 /* paging is disabled */
a23bbfda 250 return;
fae001f5
WC
251 }
252
253 if (env->cr[4] & CR4_PAE_MASK) {
254#ifdef TARGET_X86_64
255 if (env->hflags & HF_LMA_MASK) {
a8170e5e 256 hwaddr pml4e_addr;
fae001f5 257
fbc2ed95 258 pml4e_addr = (env->cr[3] & PLM4_ADDR_MASK) & env->a20_mask;
fae001f5
WC
259 walk_pml4e(list, pml4e_addr, env->a20_mask);
260 } else
261#endif
262 {
a8170e5e 263 hwaddr pdpe_addr;
fae001f5
WC
264
265 pdpe_addr = (env->cr[3] & ~0x1f) & env->a20_mask;
266 walk_pdpe2(list, pdpe_addr, env->a20_mask);
267 }
268 } else {
a8170e5e 269 hwaddr pde_addr;
fae001f5
WC
270 bool pse;
271
272 pde_addr = (env->cr[3] & ~0xfff) & env->a20_mask;
273 pse = !!(env->cr[4] & CR4_PSE_MASK);
274 walk_pde2(list, pde_addr, env->a20_mask, pse);
275 }
fae001f5 276}
31a2207a 277