ia64/xen-unstable

view xen/arch/x86/x86_32/mm.c @ 5593:384059d92d3b

bitkeeper revision 1.1760.1.5 (42c105a0IGE_9KbvmOfox8TMahmQVw)

Only enable PGE in CR4 if the CPU supports the feature.
Signed-off-by: Keir Fraser <keir@xensource.com>
author kaf24@firebug.cl.cam.ac.uk
date Tue Jun 28 08:09:04 2005 +0000 (2005-06-28)
parents 849b58da37b7
children 63c1bb85fcea
line source
1 /******************************************************************************
2 * arch/x86/x86_32/mm.c
3 *
4 * Modifications to Linux original are copyright (c) 2004, K A Fraser
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
21 #include <xen/config.h>
22 #include <xen/lib.h>
23 #include <xen/init.h>
24 #include <xen/mm.h>
25 #include <xen/sched.h>
26 #include <asm/current.h>
27 #include <asm/page.h>
28 #include <asm/flushtlb.h>
29 #include <asm/fixmap.h>
31 extern l1_pgentry_t *mapcache;
33 unsigned int PAGE_HYPERVISOR = __PAGE_HYPERVISOR;
34 unsigned int PAGE_HYPERVISOR_NOCACHE = __PAGE_HYPERVISOR_NOCACHE;
36 static unsigned long mpt_size;
38 struct pfn_info *alloc_xen_pagetable(void)
39 {
40 extern int early_boot;
41 extern unsigned long xenheap_phys_start;
42 struct pfn_info *pg;
44 if ( !early_boot )
45 {
46 void *v = alloc_xenheap_page();
47 return ((v == NULL) ? NULL : virt_to_page(v));
48 }
50 pg = phys_to_page(xenheap_phys_start);
51 xenheap_phys_start += PAGE_SIZE;
52 return pg;
53 }
55 void free_xen_pagetable(struct pfn_info *pg)
56 {
57 free_xenheap_page(page_to_virt(pg));
58 }
60 l2_pgentry_t *virt_to_xen_l2e(unsigned long v)
61 {
62 return &idle_pg_table_l2[l2_linear_offset(v)];
63 }
65 void __init paging_init(void)
66 {
67 void *ioremap_pt;
68 unsigned long v;
69 struct pfn_info *pg;
70 int i, mapcache_order;
72 #ifdef CONFIG_X86_PAE
73 printk("PAE enabled, limit: %d GB\n", MACHPHYS_MBYTES);
74 #else
75 printk("PAE disabled.\n");
76 #endif
78 idle0_vcpu.arch.monitor_table = mk_pagetable(__pa(idle_pg_table));
80 if ( cpu_has_pge )
81 {
82 /* Suitable Xen mapping can be GLOBAL. */
83 set_in_cr4(X86_CR4_PGE);
84 PAGE_HYPERVISOR |= _PAGE_GLOBAL;
85 PAGE_HYPERVISOR_NOCACHE |= _PAGE_GLOBAL;
86 /* Transform early mappings (e.g., the frametable). */
87 for ( v = HYPERVISOR_VIRT_START; v; v += (1 << L2_PAGETABLE_SHIFT) )
88 if ( (l2e_get_flags(idle_pg_table_l2[l2_linear_offset(v)]) &
89 (_PAGE_PSE|_PAGE_PRESENT)) == (_PAGE_PSE|_PAGE_PRESENT) )
90 l2e_add_flags(idle_pg_table_l2[l2_linear_offset(v)],
91 _PAGE_GLOBAL);
92 }
94 /*
95 * Allocate and map the machine-to-phys table and create read-only mapping
96 * of MPT for guest-OS use. Without PAE we'll end up with one 4MB page,
97 * with PAE we'll allocate 2MB pages depending on the amount of memory
98 * installed, but at least 4MB to cover 4GB address space. This is needed
99 * to make PCI I/O memory address lookups work in guests.
100 */
101 if ( (mpt_size = max_page * 4) < (4*1024*1024) )
102 mpt_size = 4*1024*1024;
103 for ( i = 0; i < (mpt_size >> L2_PAGETABLE_SHIFT); i++ )
104 {
105 if ( (pg = alloc_domheap_pages(NULL, PAGETABLE_ORDER)) == NULL )
106 panic("Not enough memory to bootstrap Xen.\n");
107 idle_pg_table_l2[l2_linear_offset(RDWR_MPT_VIRT_START) + i] =
108 l2e_from_page(pg, PAGE_HYPERVISOR | _PAGE_PSE);
109 idle_pg_table_l2[l2_linear_offset(RO_MPT_VIRT_START) + i] =
110 l2e_from_page(pg, (__PAGE_HYPERVISOR | _PAGE_PSE) & ~_PAGE_RW);
111 }
112 memset((void *)RDWR_MPT_VIRT_START, 0x55, mpt_size);
114 /* Create page tables for ioremap(). */
115 for ( i = 0; i < (IOREMAP_MBYTES >> (L2_PAGETABLE_SHIFT - 20)); i++ )
116 {
117 ioremap_pt = alloc_xenheap_page();
118 clear_page(ioremap_pt);
119 idle_pg_table_l2[l2_linear_offset(IOREMAP_VIRT_START) + i] =
120 l2e_from_page(virt_to_page(ioremap_pt), __PAGE_HYPERVISOR);
121 }
123 /* Set up mapping cache for domain pages. */
124 mapcache_order = get_order(MAPCACHE_MBYTES << (20 - PAGETABLE_ORDER));
125 mapcache = alloc_xenheap_pages(mapcache_order);
126 memset(mapcache, 0, PAGE_SIZE << mapcache_order);
127 for ( i = 0; i < (MAPCACHE_MBYTES >> (L2_PAGETABLE_SHIFT - 20)); i++ )
128 idle_pg_table_l2[l2_linear_offset(MAPCACHE_VIRT_START) + i] =
129 l2e_from_page(virt_to_page(mapcache) + i, __PAGE_HYPERVISOR);
130 }
132 void __init zap_low_mappings(l2_pgentry_t *base)
133 {
134 int i;
135 u32 addr;
137 for (i = 0; ; i++) {
138 addr = (i << L2_PAGETABLE_SHIFT);
139 if (addr >= HYPERVISOR_VIRT_START)
140 break;
141 if (l2e_get_paddr(base[i]) != addr)
142 continue;
143 base[i] = l2e_empty();
144 }
145 flush_tlb_all_pge();
146 }
148 void subarch_init_memory(struct domain *dom_xen)
149 {
150 unsigned long m2p_start_mfn;
151 int i;
153 /*
154 * We are rather picky about the layout of 'struct pfn_info'. The
155 * count_info and domain fields must be adjacent, as we perform atomic
156 * 64-bit operations on them. Also, just for sanity, we assert the size
157 * of the structure here.
158 */
159 if ( (offsetof(struct pfn_info, u.inuse._domain) !=
160 (offsetof(struct pfn_info, count_info) + sizeof(u32))) ||
161 (sizeof(struct pfn_info) != 24) )
162 {
163 printk("Weird pfn_info layout (%ld,%ld,%d)\n",
164 offsetof(struct pfn_info, count_info),
165 offsetof(struct pfn_info, u.inuse._domain),
166 sizeof(struct pfn_info));
167 BUG();
168 }
170 /* M2P table is mappable read-only by privileged domains. */
171 for ( i = 0; i < (mpt_size >> L2_PAGETABLE_SHIFT); i++ )
172 {
173 m2p_start_mfn = l2e_get_pfn(
174 idle_pg_table_l2[l2_linear_offset(RDWR_MPT_VIRT_START) + i]);
175 for ( i = 0; i < L2_PAGETABLE_ENTRIES; i++ )
176 {
177 frame_table[m2p_start_mfn+i].count_info = PGC_allocated | 1;
178 /* Ensure it's only mapped read-only by domains. */
179 frame_table[m2p_start_mfn+i].u.inuse.type_info = PGT_gdt_page | 1;
180 page_set_owner(&frame_table[m2p_start_mfn+i], dom_xen);
181 }
182 }
183 }
186 long do_stack_switch(unsigned long ss, unsigned long esp)
187 {
188 int nr = smp_processor_id();
189 struct tss_struct *t = &init_tss[nr];
191 if ( (ss & 3) != 1 )
192 return -EPERM;
194 current->arch.guest_context.kernel_ss = ss;
195 current->arch.guest_context.kernel_sp = esp;
196 t->ss1 = ss;
197 t->esp1 = esp;
199 return 0;
200 }
202 /* Returns TRUE if given descriptor is valid for GDT or LDT. */
203 int check_descriptor(struct desc_struct *d)
204 {
205 unsigned long base, limit;
206 u32 a = d->a, b = d->b;
208 /* A not-present descriptor will always fault, so is safe. */
209 if ( !(b & _SEGMENT_P) )
210 goto good;
212 /*
213 * We don't allow a DPL of zero. There is no legitimate reason for
214 * specifying DPL==0, and it gets rather dangerous if we also accept call
215 * gates (consider a call gate pointing at another kernel descriptor with
216 * DPL 0 -- this would get the OS ring-0 privileges).
217 */
218 if ( (b & _SEGMENT_DPL) == 0 )
219 goto bad;
221 if ( !(b & _SEGMENT_S) )
222 {
223 /*
224 * System segment:
225 * 1. Don't allow interrupt or trap gates as they belong in the IDT.
226 * 2. Don't allow TSS descriptors or task gates as we don't
227 * virtualise x86 tasks.
228 * 3. Don't allow LDT descriptors because they're unnecessary and
229 * I'm uneasy about allowing an LDT page to contain LDT
230 * descriptors. In any case, Xen automatically creates the
231 * required descriptor when reloading the LDT register.
232 * 4. We allow call gates but they must not jump to a private segment.
233 */
235 /* Disallow everything but call gates. */
236 if ( (b & _SEGMENT_TYPE) != 0xc00 )
237 goto bad;
239 /* Can't allow far jump to a Xen-private segment. */
240 if ( !VALID_CODESEL(a>>16) )
241 goto bad;
243 /* Reserved bits must be zero. */
244 if ( (b & 0xe0) != 0 )
245 goto bad;
247 /* No base/limit check is needed for a call gate. */
248 goto good;
249 }
251 /* Check that base is at least a page away from Xen-private area. */
252 base = (b&(0xff<<24)) | ((b&0xff)<<16) | (a>>16);
253 if ( base >= (GUEST_SEGMENT_MAX_ADDR - PAGE_SIZE) )
254 goto bad;
256 /* Check and truncate the limit if necessary. */
257 limit = (b&0xf0000) | (a&0xffff);
258 limit++; /* We add one because limit is inclusive. */
259 if ( (b & _SEGMENT_G) )
260 limit <<= 12;
262 if ( (b & (_SEGMENT_CODE | _SEGMENT_EC)) == _SEGMENT_EC )
263 {
264 /*
265 * DATA, GROWS-DOWN.
266 * Grows-down limit check.
267 * NB. limit == 0xFFFFF provides no access (if G=1).
268 * limit == 0x00000 provides 4GB-4kB access (if G=1).
269 */
270 if ( (base + limit) > base )
271 {
272 limit = -(base & PAGE_MASK);
273 goto truncate;
274 }
275 }
276 else
277 {
278 /*
279 * DATA, GROWS-UP.
280 * CODE (CONFORMING AND NON-CONFORMING).
281 * Grows-up limit check.
282 * NB. limit == 0xFFFFF provides 4GB access (if G=1).
283 * limit == 0x00000 provides 4kB access (if G=1).
284 */
285 if ( ((base + limit) <= base) ||
286 ((base + limit) > GUEST_SEGMENT_MAX_ADDR) )
287 {
288 limit = GUEST_SEGMENT_MAX_ADDR - base;
289 truncate:
290 if ( !(b & _SEGMENT_G) )
291 goto bad; /* too dangerous; too hard to work out... */
292 limit = (limit >> 12) - 1;
293 d->a &= ~0x0ffff; d->a |= limit & 0x0ffff;
294 d->b &= ~0xf0000; d->b |= limit & 0xf0000;
295 }
296 }
298 good:
299 return 1;
300 bad:
301 return 0;
302 }
304 void memguard_guard_stack(void *p)
305 {
306 memguard_guard_range(p, PAGE_SIZE);
307 }
309 /*
310 * Local variables:
311 * mode: C
312 * c-set-style: "BSD"
313 * c-basic-offset: 4
314 * tab-width: 4
315 * indent-tabs-mode: nil
316 * End:
317 */