debuggers.hg

view xen/arch/x86/x86_32/mm.c @ 3336:2711f7eb364c

bitkeeper revision 1.1159.1.490 (41c1bb05aOZv3pnPk-NIbxvGZzv5BQ)

page.h, mm.c:
More cleaning.
author kaf24@pb001.cl.cam.ac.uk
date Thu Dec 16 16:42:45 2004 +0000 (2004-12-16)
parents ddf5b350364f
children d263200ffdc3
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 <asm/page.h>
26 #include <asm/flushtlb.h>
27 #include <asm/fixmap.h>
28 #include <asm/domain_page.h>
30 static inline void set_pte_phys(unsigned long vaddr,
31 l1_pgentry_t entry)
32 {
33 l2_pgentry_t *l2ent;
34 l1_pgentry_t *l1ent;
36 l2ent = &idle_pg_table[l2_table_offset(vaddr)];
37 l1ent = l2_pgentry_to_l1(*l2ent) + l1_table_offset(vaddr);
38 *l1ent = entry;
40 /* It's enough to flush this one mapping. */
41 __flush_tlb_one(vaddr);
42 }
45 void __set_fixmap(enum fixed_addresses idx,
46 l1_pgentry_t entry)
47 {
48 unsigned long address = fix_to_virt(idx);
50 if ( likely(idx < __end_of_fixed_addresses) )
51 set_pte_phys(address, entry);
52 else
53 printk("Invalid __set_fixmap\n");
54 }
57 void __init paging_init(void)
58 {
59 void *ioremap_pt;
60 int i;
62 /* Idle page table 1:1 maps the first part of physical memory. */
63 for ( i = 0; i < DOMAIN_ENTRIES_PER_L2_PAGETABLE; i++ )
64 idle_pg_table[i] =
65 mk_l2_pgentry((i << L2_PAGETABLE_SHIFT) |
66 __PAGE_HYPERVISOR | _PAGE_PSE);
68 /* Create page table for ioremap(). */
69 ioremap_pt = (void *)alloc_xenheap_page();
70 clear_page(ioremap_pt);
71 idle_pg_table[IOREMAP_VIRT_START >> L2_PAGETABLE_SHIFT] =
72 mk_l2_pgentry(__pa(ioremap_pt) | __PAGE_HYPERVISOR);
74 /* Create read-only mapping of MPT for guest-OS use. */
75 idle_pg_table[RO_MPT_VIRT_START >> L2_PAGETABLE_SHIFT] =
76 mk_l2_pgentry(l2_pgentry_val(
77 idle_pg_table[RDWR_MPT_VIRT_START >> L2_PAGETABLE_SHIFT]) &
78 ~_PAGE_RW);
80 /* Set up mapping cache for domain pages. */
81 mapcache = (unsigned long *)alloc_xenheap_page();
82 clear_page(mapcache);
83 idle_pg_table[MAPCACHE_VIRT_START >> L2_PAGETABLE_SHIFT] =
84 mk_l2_pgentry(__pa(mapcache) | __PAGE_HYPERVISOR);
86 /* Set up linear page table mapping. */
87 idle_pg_table[LINEAR_PT_VIRT_START >> L2_PAGETABLE_SHIFT] =
88 mk_l2_pgentry(__pa(idle_pg_table) | __PAGE_HYPERVISOR);
90 }
92 void __init zap_low_mappings(void)
93 {
94 int i;
95 for ( i = 0; i < DOMAIN_ENTRIES_PER_L2_PAGETABLE; i++ )
96 idle_pg_table[i] = mk_l2_pgentry(0);
97 flush_tlb_all_pge();
98 }
101 /*
102 * Allows shooting down of borrowed page-table use on specific CPUs.
103 * Specifically, we borrow page tables when running the idle domain.
104 */
105 static void __synchronise_pagetables(void *mask)
106 {
107 struct exec_domain *ed = current;
108 if ( ((unsigned long)mask & (1 << ed->processor)) &&
109 is_idle_task(ed->domain) )
110 write_ptbase(&ed->mm);
111 }
112 void synchronise_pagetables(unsigned long cpu_mask)
113 {
114 __synchronise_pagetables((void *)cpu_mask);
115 smp_call_function(__synchronise_pagetables, (void *)cpu_mask, 1, 1);
116 }
118 long do_stack_switch(unsigned long ss, unsigned long esp)
119 {
120 int nr = smp_processor_id();
121 struct tss_struct *t = &init_tss[nr];
123 /* We need to do this check as we load and use SS on guest's behalf. */
124 if ( (ss & 3) == 0 )
125 return -EPERM;
127 current->thread.guestos_ss = ss;
128 current->thread.guestos_sp = esp;
129 t->ss1 = ss;
130 t->esp1 = esp;
132 return 0;
133 }
136 /* Returns TRUE if given descriptor is valid for GDT or LDT. */
137 int check_descriptor(unsigned long *d)
138 {
139 unsigned long base, limit, a = d[0], b = d[1];
141 /* A not-present descriptor will always fault, so is safe. */
142 if ( !(b & _SEGMENT_P) )
143 goto good;
145 /*
146 * We don't allow a DPL of zero. There is no legitimate reason for
147 * specifying DPL==0, and it gets rather dangerous if we also accept call
148 * gates (consider a call gate pointing at another guestos descriptor with
149 * DPL 0 -- this would get the OS ring-0 privileges).
150 */
151 if ( (b & _SEGMENT_DPL) == 0 )
152 goto bad;
154 if ( !(b & _SEGMENT_S) )
155 {
156 /*
157 * System segment:
158 * 1. Don't allow interrupt or trap gates as they belong in the IDT.
159 * 2. Don't allow TSS descriptors or task gates as we don't
160 * virtualise x86 tasks.
161 * 3. Don't allow LDT descriptors because they're unnecessary and
162 * I'm uneasy about allowing an LDT page to contain LDT
163 * descriptors. In any case, Xen automatically creates the
164 * required descriptor when reloading the LDT register.
165 * 4. We allow call gates but they must not jump to a private segment.
166 */
168 /* Disallow everything but call gates. */
169 if ( (b & _SEGMENT_TYPE) != 0xc00 )
170 goto bad;
172 /* Can't allow far jump to a Xen-private segment. */
173 if ( !VALID_CODESEL(a>>16) )
174 goto bad;
176 /* Reserved bits must be zero. */
177 if ( (b & 0xe0) != 0 )
178 goto bad;
180 /* No base/limit check is needed for a call gate. */
181 goto good;
182 }
184 /* Check that base is at least a page away from Xen-private area. */
185 base = (b&(0xff<<24)) | ((b&0xff)<<16) | (a>>16);
186 if ( base >= (PAGE_OFFSET - PAGE_SIZE) )
187 goto bad;
189 /* Check and truncate the limit if necessary. */
190 limit = (b&0xf0000) | (a&0xffff);
191 limit++; /* We add one because limit is inclusive. */
192 if ( (b & _SEGMENT_G) )
193 limit <<= 12;
195 if ( (b & (_SEGMENT_CODE | _SEGMENT_EC)) == _SEGMENT_EC )
196 {
197 /*
198 * Grows-down limit check.
199 * NB. limit == 0xFFFFF provides no access (if G=1).
200 * limit == 0x00000 provides 4GB-4kB access (if G=1).
201 */
202 if ( (base + limit) > base )
203 {
204 limit = -(base & PAGE_MASK);
205 goto truncate;
206 }
207 }
208 else
209 {
210 /*
211 * Grows-up limit check.
212 * NB. limit == 0xFFFFF provides 4GB access (if G=1).
213 * limit == 0x00000 provides 4kB access (if G=1).
214 */
215 if ( ((base + limit) <= base) ||
216 ((base + limit) > PAGE_OFFSET) )
217 {
218 limit = PAGE_OFFSET - base;
219 truncate:
220 if ( !(b & _SEGMENT_G) )
221 goto bad; /* too dangerous; too hard to work out... */
222 limit = (limit >> 12) - 1;
223 d[0] &= ~0x0ffff; d[0] |= limit & 0x0ffff;
224 d[1] &= ~0xf0000; d[1] |= limit & 0xf0000;
225 }
226 }
228 good:
229 return 1;
230 bad:
231 return 0;
232 }
235 void destroy_gdt(struct exec_domain *ed)
236 {
237 int i;
238 unsigned long pfn;
240 for ( i = 0; i < 16; i++ )
241 {
242 if ( (pfn = l1_pgentry_to_pagenr(ed->mm.perdomain_ptes[i])) != 0 )
243 put_page_and_type(&frame_table[pfn]);
244 ed->mm.perdomain_ptes[i] = mk_l1_pgentry(0);
245 }
246 }
249 long set_gdt(struct exec_domain *ed,
250 unsigned long *frames,
251 unsigned int entries)
252 {
253 struct domain *d = ed->domain;
254 /* NB. There are 512 8-byte entries per GDT page. */
255 int i = 0, nr_pages = (entries + 511) / 512;
256 struct desc_struct *vgdt;
257 unsigned long pfn;
259 /* Check the first page in the new GDT. */
260 if ( (pfn = frames[0]) >= max_page )
261 goto fail;
263 /* The first page is special because Xen owns a range of entries in it. */
264 if ( !get_page_and_type(&frame_table[pfn], d, PGT_gdt_page) )
265 {
266 /* GDT checks failed: try zapping the Xen reserved entries. */
267 if ( !get_page_and_type(&frame_table[pfn], d, PGT_writable_page) )
268 goto fail;
269 vgdt = map_domain_mem(pfn << PAGE_SHIFT);
270 memset(vgdt + FIRST_RESERVED_GDT_ENTRY, 0,
271 NR_RESERVED_GDT_ENTRIES*8);
272 unmap_domain_mem(vgdt);
273 put_page_and_type(&frame_table[pfn]);
275 /* Okay, we zapped the entries. Now try the GDT checks again. */
276 if ( !get_page_and_type(&frame_table[pfn], d, PGT_gdt_page) )
277 goto fail;
278 }
280 /* Check the remaining pages in the new GDT. */
281 for ( i = 1; i < nr_pages; i++ )
282 if ( ((pfn = frames[i]) >= max_page) ||
283 !get_page_and_type(&frame_table[pfn], d, PGT_gdt_page) )
284 goto fail;
286 /* Copy reserved GDT entries to the new GDT. */
287 vgdt = map_domain_mem(frames[0] << PAGE_SHIFT);
288 memcpy(vgdt + FIRST_RESERVED_GDT_ENTRY,
289 gdt_table + FIRST_RESERVED_GDT_ENTRY,
290 NR_RESERVED_GDT_ENTRIES*8);
291 unmap_domain_mem(vgdt);
293 /* Tear down the old GDT. */
294 destroy_gdt(ed);
296 /* Install the new GDT. */
297 for ( i = 0; i < nr_pages; i++ )
298 ed->mm.perdomain_ptes[i] =
299 mk_l1_pgentry((frames[i] << PAGE_SHIFT) | __PAGE_HYPERVISOR);
301 SET_GDT_ADDRESS(ed, GDT_VIRT_START(ed));
302 SET_GDT_ENTRIES(ed, entries);
304 return 0;
306 fail:
307 while ( i-- > 0 )
308 put_page_and_type(&frame_table[frames[i]]);
309 return -EINVAL;
310 }
313 long do_set_gdt(unsigned long *frame_list, unsigned int entries)
314 {
315 int nr_pages = (entries + 511) / 512;
316 unsigned long frames[16];
317 long ret;
319 if ( (entries <= LAST_RESERVED_GDT_ENTRY) || (entries > 8192) )
320 return -EINVAL;
322 if ( copy_from_user(frames, frame_list, nr_pages * sizeof(unsigned long)) )
323 return -EFAULT;
325 LOCK_BIGLOCK(current->domain);
327 if ( (ret = set_gdt(current, frames, entries)) == 0 )
328 {
329 local_flush_tlb();
330 __asm__ __volatile__ ("lgdt %0" : "=m" (*current->mm.gdt));
331 }
333 UNLOCK_BIGLOCK(current->domain);
335 return ret;
336 }
339 long do_update_descriptor(
340 unsigned long pa, unsigned long word1, unsigned long word2)
341 {
342 unsigned long *gdt_pent, pfn = pa >> PAGE_SHIFT, d[2];
343 struct pfn_info *page;
344 struct exec_domain *ed;
345 long ret = -EINVAL;
347 d[0] = word1;
348 d[1] = word2;
350 LOCK_BIGLOCK(current->domain);
352 if ( (pa & 7) || (pfn >= max_page) || !check_descriptor(d) ) {
353 UNLOCK_BIGLOCK(current->domain);
354 return -EINVAL;
355 }
357 page = &frame_table[pfn];
358 if ( unlikely(!get_page(page, current->domain)) ) {
359 UNLOCK_BIGLOCK(current->domain);
360 return -EINVAL;
361 }
363 /* Check if the given frame is in use in an unsafe context. */
364 switch ( page->u.inuse.type_info & PGT_type_mask )
365 {
366 case PGT_gdt_page:
367 /* Disallow updates of Xen-reserved descriptors in the current GDT. */
368 for_each_exec_domain(current->domain, ed) {
369 if ( (l1_pgentry_to_pagenr(ed->mm.perdomain_ptes[0]) == pfn) &&
370 (((pa&(PAGE_SIZE-1))>>3) >= FIRST_RESERVED_GDT_ENTRY) &&
371 (((pa&(PAGE_SIZE-1))>>3) <= LAST_RESERVED_GDT_ENTRY) )
372 goto out;
373 }
374 if ( unlikely(!get_page_type(page, PGT_gdt_page)) )
375 goto out;
376 break;
377 case PGT_ldt_page:
378 if ( unlikely(!get_page_type(page, PGT_ldt_page)) )
379 goto out;
380 break;
381 default:
382 if ( unlikely(!get_page_type(page, PGT_writable_page)) )
383 goto out;
384 break;
385 }
387 /* All is good so make the update. */
388 gdt_pent = map_domain_mem(pa);
389 memcpy(gdt_pent, d, 8);
390 unmap_domain_mem(gdt_pent);
392 put_page_type(page);
394 ret = 0; /* success */
396 out:
397 put_page(page);
399 UNLOCK_BIGLOCK(current->domain);
401 return ret;
402 }
404 #ifdef MEMORY_GUARD
406 void *memguard_init(void *heap_start)
407 {
408 l1_pgentry_t *l1;
409 int i, j;
411 /* Round the allocation pointer up to a page boundary. */
412 heap_start = (void *)(((unsigned long)heap_start + (PAGE_SIZE-1)) &
413 PAGE_MASK);
415 /* Memory guarding is incompatible with super pages. */
416 for ( i = 0; i < (xenheap_phys_end >> L2_PAGETABLE_SHIFT); i++ )
417 {
418 l1 = (l1_pgentry_t *)heap_start;
419 heap_start = (void *)((unsigned long)heap_start + PAGE_SIZE);
420 for ( j = 0; j < ENTRIES_PER_L1_PAGETABLE; j++ )
421 l1[j] = mk_l1_pgentry((i << L2_PAGETABLE_SHIFT) |
422 (j << L1_PAGETABLE_SHIFT) |
423 __PAGE_HYPERVISOR);
424 idle_pg_table[i] = idle_pg_table[i + l2_table_offset(PAGE_OFFSET)] =
425 mk_l2_pgentry(virt_to_phys(l1) | __PAGE_HYPERVISOR);
426 }
428 return heap_start;
429 }
431 static void __memguard_change_range(void *p, unsigned long l, int guard)
432 {
433 l1_pgentry_t *l1;
434 l2_pgentry_t *l2;
435 unsigned long _p = (unsigned long)p;
436 unsigned long _l = (unsigned long)l;
438 /* Ensure we are dealing with a page-aligned whole number of pages. */
439 ASSERT((_p&PAGE_MASK) != 0);
440 ASSERT((_l&PAGE_MASK) != 0);
441 ASSERT((_p&~PAGE_MASK) == 0);
442 ASSERT((_l&~PAGE_MASK) == 0);
444 while ( _l != 0 )
445 {
446 l2 = &idle_pg_table[l2_table_offset(_p)];
447 l1 = l2_pgentry_to_l1(*l2) + l1_table_offset(_p);
448 if ( guard )
449 *l1 = mk_l1_pgentry(l1_pgentry_val(*l1) & ~_PAGE_PRESENT);
450 else
451 *l1 = mk_l1_pgentry(l1_pgentry_val(*l1) | _PAGE_PRESENT);
452 _p += PAGE_SIZE;
453 _l -= PAGE_SIZE;
454 }
455 }
457 void memguard_guard_range(void *p, unsigned long l)
458 {
459 __memguard_change_range(p, l, 1);
460 local_flush_tlb();
461 }
463 void memguard_unguard_range(void *p, unsigned long l)
464 {
465 __memguard_change_range(p, l, 0);
466 }
468 int memguard_is_guarded(void *p)
469 {
470 l1_pgentry_t *l1;
471 l2_pgentry_t *l2;
472 unsigned long _p = (unsigned long)p;
473 l2 = &idle_pg_table[l2_table_offset(_p)];
474 l1 = l2_pgentry_to_l1(*l2) + l1_table_offset(_p);
475 return !(l1_pgentry_val(*l1) & _PAGE_PRESENT);
476 }
478 #endif