debuggers.hg

view xen/arch/x86/x86_32/mm.c @ 3766:89e86842952a

bitkeeper revision 1.1159.212.132 (4208e2acn2x2RLZsxZIR12IGEO1b3A)

Merge scramble.cl.cam.ac.uk:/auto/groups/xeno/BK/xeno.bk
into scramble.cl.cam.ac.uk:/local/scratch/kaf24/xen-unstable.bk
author kaf24@scramble.cl.cam.ac.uk
date Tue Feb 08 16:02:52 2005 +0000 (2005-02-08)
parents 23e7cf28ddb3 4dfebfdc7933
children f5f2757b3aa2
line source
1 /* -*- Mode:C; c-basic-offset:4; tab-width:4; indent-tabs-mode:nil -*- */
2 /******************************************************************************
3 * arch/x86/x86_32/mm.c
4 *
5 * Modifications to Linux original are copyright (c) 2004, K A Fraser
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
22 #include <xen/config.h>
23 #include <xen/lib.h>
24 #include <xen/init.h>
25 #include <xen/mm.h>
26 #include <asm/page.h>
27 #include <asm/flushtlb.h>
28 #include <asm/fixmap.h>
29 #include <asm/domain_page.h>
31 /* Map physical byte range (@p, @p+@s) at virt address @v in pagetable @pt. */
32 int map_pages(
33 pagetable_t *pt,
34 unsigned long v,
35 unsigned long p,
36 unsigned long s,
37 unsigned long flags)
38 {
39 l2_pgentry_t *pl2e;
40 l1_pgentry_t *pl1e;
41 void *newpg;
43 while ( s != 0 )
44 {
45 pl2e = &pt[l2_table_offset(v)];
47 if ( ((s|v|p) & ((1<<L2_PAGETABLE_SHIFT)-1)) == 0 )
48 {
49 /* Super-page mapping. */
50 if ( (l2_pgentry_val(*pl2e) & _PAGE_PRESENT) )
51 __flush_tlb_pge();
52 *pl2e = mk_l2_pgentry(p|flags|_PAGE_PSE);
54 v += 1 << L2_PAGETABLE_SHIFT;
55 p += 1 << L2_PAGETABLE_SHIFT;
56 s -= 1 << L2_PAGETABLE_SHIFT;
57 }
58 else
59 {
60 /* Normal page mapping. */
61 if ( !(l2_pgentry_val(*pl2e) & _PAGE_PRESENT) )
62 {
63 newpg = (void *)alloc_xenheap_page();
64 clear_page(newpg);
65 *pl2e = mk_l2_pgentry(__pa(newpg) | __PAGE_HYPERVISOR);
66 }
67 pl1e = l2_pgentry_to_l1(*pl2e) + l1_table_offset(v);
68 if ( (l1_pgentry_val(*pl1e) & _PAGE_PRESENT) )
69 __flush_tlb_one(v);
70 *pl1e = mk_l1_pgentry(p|flags);
72 v += 1 << L1_PAGETABLE_SHIFT;
73 p += 1 << L1_PAGETABLE_SHIFT;
74 s -= 1 << L1_PAGETABLE_SHIFT;
75 }
76 }
78 return 0;
79 }
81 void __set_fixmap(
82 enum fixed_addresses idx, unsigned long p, unsigned long flags)
83 {
84 if ( unlikely(idx >= __end_of_fixed_addresses) )
85 BUG();
86 map_pages(idle_pg_table, fix_to_virt(idx), p, PAGE_SIZE, flags);
87 }
90 void __init paging_init(void)
91 {
92 void *ioremap_pt;
93 unsigned long v, l2e;
94 struct pfn_info *pg;
96 /* Allocate and map the machine-to-phys table. */
97 if ( (pg = alloc_domheap_pages(NULL, 10)) == NULL )
98 panic("Not enough memory to bootstrap Xen.\n");
99 idle_pg_table[l2_table_offset(RDWR_MPT_VIRT_START)] =
100 mk_l2_pgentry(page_to_phys(pg) | __PAGE_HYPERVISOR | _PAGE_PSE);
101 memset((void *)RDWR_MPT_VIRT_START, 0x55, 4UL << 20);
103 /* Xen 4MB mappings can all be GLOBAL. */
104 if ( cpu_has_pge )
105 {
106 for ( v = HYPERVISOR_VIRT_START; v; v += (1 << L2_PAGETABLE_SHIFT) )
107 {
108 l2e = l2_pgentry_val(idle_pg_table[l2_table_offset(v)]);
109 if ( l2e & _PAGE_PSE )
110 l2e |= _PAGE_GLOBAL;
111 idle_pg_table[v >> L2_PAGETABLE_SHIFT] = mk_l2_pgentry(l2e);
112 }
113 }
115 /* Create page table for ioremap(). */
116 ioremap_pt = (void *)alloc_xenheap_page();
117 clear_page(ioremap_pt);
118 idle_pg_table[l2_table_offset(IOREMAP_VIRT_START)] =
119 mk_l2_pgentry(__pa(ioremap_pt) | __PAGE_HYPERVISOR);
121 /* Create read-only mapping of MPT for guest-OS use. */
122 idle_pg_table[l2_table_offset(RO_MPT_VIRT_START)] =
123 mk_l2_pgentry(l2_pgentry_val(
124 idle_pg_table[l2_table_offset(RDWR_MPT_VIRT_START)]) & ~_PAGE_RW);
126 /* Set up mapping cache for domain pages. */
127 mapcache = (unsigned long *)alloc_xenheap_page();
128 clear_page(mapcache);
129 idle_pg_table[l2_table_offset(MAPCACHE_VIRT_START)] =
130 mk_l2_pgentry(__pa(mapcache) | __PAGE_HYPERVISOR);
132 /* Set up linear page table mapping. */
133 idle_pg_table[l2_table_offset(LINEAR_PT_VIRT_START)] =
134 mk_l2_pgentry(__pa(idle_pg_table) | __PAGE_HYPERVISOR);
135 }
137 void __init zap_low_mappings(void)
138 {
139 int i;
140 for ( i = 0; i < DOMAIN_ENTRIES_PER_L2_PAGETABLE; i++ )
141 idle_pg_table[i] = mk_l2_pgentry(0);
142 flush_tlb_all_pge();
143 }
145 void subarch_init_memory(struct domain *dom_xen)
146 {
147 unsigned long i, m2p_start_mfn;
149 /*
150 * We are rather picky about the layout of 'struct pfn_info'. The
151 * count_info and domain fields must be adjacent, as we perform atomic
152 * 64-bit operations on them. Also, just for sanity, we assert the size
153 * of the structure here.
154 */
155 if ( (offsetof(struct pfn_info, u.inuse._domain) !=
156 (offsetof(struct pfn_info, count_info) + sizeof(u32))) ||
157 (sizeof(struct pfn_info) != 24) )
158 {
159 printk("Weird pfn_info layout (%ld,%ld,%d)\n",
160 offsetof(struct pfn_info, count_info),
161 offsetof(struct pfn_info, u.inuse._domain),
162 sizeof(struct pfn_info));
163 for ( ; ; ) ;
164 }
166 /* M2P table is mappable read-only by privileged domains. */
167 m2p_start_mfn = l2_pgentry_to_pfn(
168 idle_pg_table[l2_table_offset(RDWR_MPT_VIRT_START)]);
169 for ( i = 0; i < 1024; i++ )
170 {
171 frame_table[m2p_start_mfn+i].count_info = PGC_allocated | 1;
172 /* gdt to make sure it's only mapped read-only by non-privileged
173 domains. */
174 frame_table[m2p_start_mfn+i].u.inuse.type_info = PGT_gdt_page | 1;
175 page_set_owner(&frame_table[m2p_start_mfn+i], dom_xen);
176 }
177 }
179 /*
180 * Allows shooting down of borrowed page-table use on specific CPUs.
181 * Specifically, we borrow page tables when running the idle domain.
182 */
183 static void __synchronise_pagetables(void *mask)
184 {
185 struct exec_domain *ed = current;
186 if ( ((unsigned long)mask & (1 << ed->processor)) &&
187 is_idle_task(ed->domain) )
188 write_ptbase(ed);
189 }
190 void synchronise_pagetables(unsigned long cpu_mask)
191 {
192 __synchronise_pagetables((void *)cpu_mask);
193 smp_call_function(__synchronise_pagetables, (void *)cpu_mask, 1, 1);
194 }
196 long do_stack_switch(unsigned long ss, unsigned long esp)
197 {
198 int nr = smp_processor_id();
199 struct tss_struct *t = &init_tss[nr];
201 /* We need to do this check as we load and use SS on guest's behalf. */
202 if ( (ss & 3) == 0 )
203 return -EPERM;
205 current->arch.guestos_ss = ss;
206 current->arch.guestos_sp = esp;
207 t->ss1 = ss;
208 t->esp1 = esp;
210 return 0;
211 }
214 /* Returns TRUE if given descriptor is valid for GDT or LDT. */
215 int check_descriptor(struct desc_struct *d)
216 {
217 unsigned long base, limit;
218 u32 a = d->a, b = d->b;
220 /* A not-present descriptor will always fault, so is safe. */
221 if ( !(b & _SEGMENT_P) )
222 goto good;
224 /*
225 * We don't allow a DPL of zero. There is no legitimate reason for
226 * specifying DPL==0, and it gets rather dangerous if we also accept call
227 * gates (consider a call gate pointing at another guestos descriptor with
228 * DPL 0 -- this would get the OS ring-0 privileges).
229 */
230 if ( (b & _SEGMENT_DPL) == 0 )
231 goto bad;
233 if ( !(b & _SEGMENT_S) )
234 {
235 /*
236 * System segment:
237 * 1. Don't allow interrupt or trap gates as they belong in the IDT.
238 * 2. Don't allow TSS descriptors or task gates as we don't
239 * virtualise x86 tasks.
240 * 3. Don't allow LDT descriptors because they're unnecessary and
241 * I'm uneasy about allowing an LDT page to contain LDT
242 * descriptors. In any case, Xen automatically creates the
243 * required descriptor when reloading the LDT register.
244 * 4. We allow call gates but they must not jump to a private segment.
245 */
247 /* Disallow everything but call gates. */
248 if ( (b & _SEGMENT_TYPE) != 0xc00 )
249 goto bad;
251 /* Can't allow far jump to a Xen-private segment. */
252 if ( !VALID_CODESEL(a>>16) )
253 goto bad;
255 /* Reserved bits must be zero. */
256 if ( (b & 0xe0) != 0 )
257 goto bad;
259 /* No base/limit check is needed for a call gate. */
260 goto good;
261 }
263 /* Check that base is at least a page away from Xen-private area. */
264 base = (b&(0xff<<24)) | ((b&0xff)<<16) | (a>>16);
265 if ( base >= (PAGE_OFFSET - PAGE_SIZE) )
266 goto bad;
268 /* Check and truncate the limit if necessary. */
269 limit = (b&0xf0000) | (a&0xffff);
270 limit++; /* We add one because limit is inclusive. */
271 if ( (b & _SEGMENT_G) )
272 limit <<= 12;
274 if ( (b & (_SEGMENT_CODE | _SEGMENT_EC)) == _SEGMENT_EC )
275 {
276 /*
277 * Grows-down limit check.
278 * NB. limit == 0xFFFFF provides no access (if G=1).
279 * limit == 0x00000 provides 4GB-4kB access (if G=1).
280 */
281 if ( (base + limit) > base )
282 {
283 limit = -(base & PAGE_MASK);
284 goto truncate;
285 }
286 }
287 else
288 {
289 /*
290 * Grows-up limit check.
291 * NB. limit == 0xFFFFF provides 4GB access (if G=1).
292 * limit == 0x00000 provides 4kB access (if G=1).
293 */
294 if ( ((base + limit) <= base) ||
295 ((base + limit) > PAGE_OFFSET) )
296 {
297 limit = PAGE_OFFSET - base;
298 truncate:
299 if ( !(b & _SEGMENT_G) )
300 goto bad; /* too dangerous; too hard to work out... */
301 limit = (limit >> 12) - 1;
302 d->a &= ~0x0ffff; d->a |= limit & 0x0ffff;
303 d->b &= ~0xf0000; d->b |= limit & 0xf0000;
304 }
305 }
307 good:
308 return 1;
309 bad:
310 return 0;
311 }
314 #ifdef MEMORY_GUARD
316 void *memguard_init(void *heap_start)
317 {
318 l1_pgentry_t *l1;
319 int i, j;
321 /* Round the allocation pointer up to a page boundary. */
322 heap_start = (void *)(((unsigned long)heap_start + (PAGE_SIZE-1)) &
323 PAGE_MASK);
325 /* Memory guarding is incompatible with super pages. */
326 for ( i = 0; i < (xenheap_phys_end >> L2_PAGETABLE_SHIFT); i++ )
327 {
328 l1 = (l1_pgentry_t *)heap_start;
329 heap_start = (void *)((unsigned long)heap_start + PAGE_SIZE);
330 for ( j = 0; j < ENTRIES_PER_L1_PAGETABLE; j++ )
331 l1[j] = mk_l1_pgentry((i << L2_PAGETABLE_SHIFT) |
332 (j << L1_PAGETABLE_SHIFT) |
333 __PAGE_HYPERVISOR);
334 idle_pg_table[i + l2_table_offset(PAGE_OFFSET)] =
335 mk_l2_pgentry(virt_to_phys(l1) | __PAGE_HYPERVISOR);
336 }
338 return heap_start;
339 }
341 static void __memguard_change_range(void *p, unsigned long l, int guard)
342 {
343 l1_pgentry_t *l1;
344 l2_pgentry_t *l2;
345 unsigned long _p = (unsigned long)p;
346 unsigned long _l = (unsigned long)l;
348 /* Ensure we are dealing with a page-aligned whole number of pages. */
349 ASSERT((_p&PAGE_MASK) != 0);
350 ASSERT((_l&PAGE_MASK) != 0);
351 ASSERT((_p&~PAGE_MASK) == 0);
352 ASSERT((_l&~PAGE_MASK) == 0);
354 while ( _l != 0 )
355 {
356 l2 = &idle_pg_table[l2_table_offset(_p)];
357 l1 = l2_pgentry_to_l1(*l2) + l1_table_offset(_p);
358 if ( guard )
359 *l1 = mk_l1_pgentry(l1_pgentry_val(*l1) & ~_PAGE_PRESENT);
360 else
361 *l1 = mk_l1_pgentry(l1_pgentry_val(*l1) | _PAGE_PRESENT);
362 _p += PAGE_SIZE;
363 _l -= PAGE_SIZE;
364 }
365 }
367 void memguard_guard_stack(void *p)
368 {
369 memguard_guard_range(p, PAGE_SIZE);
370 }
372 void memguard_guard_range(void *p, unsigned long l)
373 {
374 __memguard_change_range(p, l, 1);
375 local_flush_tlb();
376 }
378 void memguard_unguard_range(void *p, unsigned long l)
379 {
380 __memguard_change_range(p, l, 0);
381 }
383 #endif