ia64/xen-unstable

view xen/arch/x86/x86_32/mm.c @ 6435:b4b3f6be5226

merge?
author cl349@firebug.cl.cam.ac.uk
date Thu Aug 25 17:27:49 2005 +0000 (2005-08-25)
parents 5ec5f6960507 f7cdd99d1106
children 8799d14bef77 9312a3e8a6f8 112d44270733
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.
97 */
98 mpt_size = (max_page * 4) + (1UL << L2_PAGETABLE_SHIFT) - 1UL;
99 mpt_size &= ~((1UL << L2_PAGETABLE_SHIFT) - 1UL);
100 for ( i = 0; i < (mpt_size >> L2_PAGETABLE_SHIFT); i++ )
101 {
102 if ( (pg = alloc_domheap_pages(NULL, PAGETABLE_ORDER, 0)) == NULL )
103 panic("Not enough memory to bootstrap Xen.\n");
104 idle_pg_table_l2[l2_linear_offset(RDWR_MPT_VIRT_START) + i] =
105 l2e_from_page(pg, PAGE_HYPERVISOR | _PAGE_PSE);
106 idle_pg_table_l2[l2_linear_offset(RO_MPT_VIRT_START) + i] =
107 l2e_from_page(pg, (__PAGE_HYPERVISOR | _PAGE_PSE) & ~_PAGE_RW);
108 }
109 memset((void *)RDWR_MPT_VIRT_START, 0x55, mpt_size);
111 /* Create page tables for ioremap(). */
112 for ( i = 0; i < (IOREMAP_MBYTES >> (L2_PAGETABLE_SHIFT - 20)); i++ )
113 {
114 ioremap_pt = alloc_xenheap_page();
115 clear_page(ioremap_pt);
116 idle_pg_table_l2[l2_linear_offset(IOREMAP_VIRT_START) + i] =
117 l2e_from_page(virt_to_page(ioremap_pt), __PAGE_HYPERVISOR);
118 }
120 /* Set up mapping cache for domain pages. */
121 mapcache_order = get_order(MAPCACHE_MBYTES << (20 - PAGETABLE_ORDER));
122 mapcache = alloc_xenheap_pages(mapcache_order);
123 memset(mapcache, 0, PAGE_SIZE << mapcache_order);
124 for ( i = 0; i < (MAPCACHE_MBYTES >> (L2_PAGETABLE_SHIFT - 20)); i++ )
125 idle_pg_table_l2[l2_linear_offset(MAPCACHE_VIRT_START) + i] =
126 l2e_from_page(virt_to_page(mapcache) + i, __PAGE_HYPERVISOR);
127 }
129 void __init zap_low_mappings(l2_pgentry_t *base)
130 {
131 int i;
132 u32 addr;
134 for (i = 0; ; i++) {
135 addr = (i << L2_PAGETABLE_SHIFT);
136 if (addr >= HYPERVISOR_VIRT_START)
137 break;
138 if (l2e_get_paddr(base[i]) != addr)
139 continue;
140 base[i] = l2e_empty();
141 }
142 flush_tlb_all_pge();
143 }
145 void subarch_init_memory(struct domain *dom_xen)
146 {
147 unsigned long m2p_start_mfn;
148 unsigned int i, j;
150 /*
151 * We are rather picky about the layout of 'struct pfn_info'. The
152 * count_info and domain fields must be adjacent, as we perform atomic
153 * 64-bit operations on them. Also, just for sanity, we assert the size
154 * of the structure here.
155 */
156 if ( (offsetof(struct pfn_info, u.inuse._domain) !=
157 (offsetof(struct pfn_info, count_info) + sizeof(u32))) ||
158 (sizeof(struct pfn_info) != 24) )
159 {
160 printk("Weird pfn_info layout (%ld,%ld,%d)\n",
161 offsetof(struct pfn_info, count_info),
162 offsetof(struct pfn_info, u.inuse._domain),
163 sizeof(struct pfn_info));
164 BUG();
165 }
167 /* M2P table is mappable read-only by privileged domains. */
168 for ( i = 0; i < (mpt_size >> L2_PAGETABLE_SHIFT); i++ )
169 {
170 m2p_start_mfn = l2e_get_pfn(
171 idle_pg_table_l2[l2_linear_offset(RDWR_MPT_VIRT_START) + i]);
172 for ( j = 0; j < L2_PAGETABLE_ENTRIES; j++ )
173 {
174 frame_table[m2p_start_mfn+j].count_info = PGC_allocated | 1;
175 /* Ensure it's only mapped read-only by domains. */
176 frame_table[m2p_start_mfn+j].u.inuse.type_info = PGT_gdt_page | 1;
177 page_set_owner(&frame_table[m2p_start_mfn+j], dom_xen);
178 }
179 }
180 }
183 long do_stack_switch(unsigned long ss, unsigned long esp)
184 {
185 int nr = smp_processor_id();
186 struct tss_struct *t = &init_tss[nr];
188 if ( (ss & 3) != 1 )
189 return -EPERM;
191 current->arch.guest_context.kernel_ss = ss;
192 current->arch.guest_context.kernel_sp = esp;
193 t->ss1 = ss;
194 t->esp1 = esp;
196 return 0;
197 }
199 /* Returns TRUE if given descriptor is valid for GDT or LDT. */
200 int check_descriptor(struct desc_struct *d)
201 {
202 unsigned long base, limit;
203 u32 a = d->a, b = d->b;
205 /* A not-present descriptor will always fault, so is safe. */
206 if ( !(b & _SEGMENT_P) )
207 goto good;
209 /*
210 * We don't allow a DPL of zero. There is no legitimate reason for
211 * specifying DPL==0, and it gets rather dangerous if we also accept call
212 * gates (consider a call gate pointing at another kernel descriptor with
213 * DPL 0 -- this would get the OS ring-0 privileges).
214 */
215 if ( (b & _SEGMENT_DPL) == 0 )
216 goto bad;
218 if ( !(b & _SEGMENT_S) )
219 {
220 /*
221 * System segment:
222 * 1. Don't allow interrupt or trap gates as they belong in the IDT.
223 * 2. Don't allow TSS descriptors or task gates as we don't
224 * virtualise x86 tasks.
225 * 3. Don't allow LDT descriptors because they're unnecessary and
226 * I'm uneasy about allowing an LDT page to contain LDT
227 * descriptors. In any case, Xen automatically creates the
228 * required descriptor when reloading the LDT register.
229 * 4. We allow call gates but they must not jump to a private segment.
230 */
232 /* Disallow everything but call gates. */
233 if ( (b & _SEGMENT_TYPE) != 0xc00 )
234 goto bad;
236 /* Can't allow far jump to a Xen-private segment. */
237 if ( !VALID_CODESEL(a>>16) )
238 goto bad;
240 /* Reserved bits must be zero. */
241 if ( (b & 0xe0) != 0 )
242 goto bad;
244 /* No base/limit check is needed for a call gate. */
245 goto good;
246 }
248 /* Check that base is at least a page away from Xen-private area. */
249 base = (b&(0xff<<24)) | ((b&0xff)<<16) | (a>>16);
250 if ( base >= (GUEST_SEGMENT_MAX_ADDR - PAGE_SIZE) )
251 goto bad;
253 /* Check and truncate the limit if necessary. */
254 limit = (b&0xf0000) | (a&0xffff);
255 limit++; /* We add one because limit is inclusive. */
256 if ( (b & _SEGMENT_G) )
257 limit <<= 12;
259 if ( (b & (_SEGMENT_CODE | _SEGMENT_EC)) == _SEGMENT_EC )
260 {
261 /*
262 * DATA, GROWS-DOWN.
263 * Grows-down limit check.
264 * NB. limit == 0xFFFFF provides no access (if G=1).
265 * limit == 0x00000 provides 4GB-4kB access (if G=1).
266 */
267 if ( (base + limit) > base )
268 {
269 limit = -(base & PAGE_MASK);
270 goto truncate;
271 }
272 }
273 else
274 {
275 /*
276 * DATA, GROWS-UP.
277 * CODE (CONFORMING AND NON-CONFORMING).
278 * Grows-up limit check.
279 * NB. limit == 0xFFFFF provides 4GB access (if G=1).
280 * limit == 0x00000 provides 4kB access (if G=1).
281 */
282 if ( ((base + limit) <= base) ||
283 ((base + limit) > GUEST_SEGMENT_MAX_ADDR) )
284 {
285 limit = GUEST_SEGMENT_MAX_ADDR - base;
286 truncate:
287 if ( !(b & _SEGMENT_G) )
288 goto bad; /* too dangerous; too hard to work out... */
289 limit = (limit >> 12) - 1;
290 d->a &= ~0x0ffff; d->a |= limit & 0x0ffff;
291 d->b &= ~0xf0000; d->b |= limit & 0xf0000;
292 }
293 }
295 good:
296 return 1;
297 bad:
298 return 0;
299 }
301 void memguard_guard_stack(void *p)
302 {
303 memguard_guard_range(p, PAGE_SIZE);
304 }
306 /*
307 * Local variables:
308 * mode: C
309 * c-set-style: "BSD"
310 * c-basic-offset: 4
311 * tab-width: 4
312 * indent-tabs-mode: nil
313 * End:
314 */