debuggers.hg

view xen/include/asm-x86/mm.h @ 20688:0cb22e32795c

x86_32: Fix build after RDTSCP and memory hotplug changes.

Signed-off-by: Yunhong Jiang <yunhong.jiang@intel.com>
Signed-off-by: Dongxiao Xu <dongxiao.xu@intel.com>
author Keir Fraser <keir.fraser@citrix.com>
date Mon Dec 14 09:48:47 2009 +0000 (2009-12-14)
parents a50c1cbf08ec
children 3a3be3938b2c
line source
2 #ifndef __ASM_X86_MM_H__
3 #define __ASM_X86_MM_H__
5 #include <xen/config.h>
6 #include <xen/list.h>
7 #include <asm/io.h>
8 #include <asm/uaccess.h>
10 /*
11 * Per-page-frame information.
12 *
13 * Every architecture must ensure the following:
14 * 1. 'struct page_info' contains a 'struct page_list_entry list'.
15 * 2. Provide a PFN_ORDER() macro for accessing the order of a free page.
16 */
17 #define PFN_ORDER(_pfn) ((_pfn)->v.free.order)
19 /*
20 * This definition is solely for the use in struct page_info (and
21 * struct page_list_head), intended to allow easy adjustment once x86-64
22 * wants to support more than 16TB.
23 * 'unsigned long' should be used for MFNs everywhere else.
24 */
25 #define __pdx_t unsigned int
27 #undef page_list_entry
28 struct page_list_entry
29 {
30 __pdx_t next, prev;
31 };
33 struct page_info
34 {
35 union {
36 /* Each frame can be threaded onto a doubly-linked list.
37 *
38 * For unused shadow pages, a list of pages of this order; for
39 * pinnable shadows, if pinned, a list of other pinned shadows
40 * (see sh_type_is_pinnable() below for the definition of
41 * "pinnable" shadow types).
42 */
43 struct page_list_entry list;
44 /* For non-pinnable shadows, a higher entry that points at us. */
45 paddr_t up;
46 };
48 /* Reference count and various PGC_xxx flags and fields. */
49 unsigned long count_info;
51 /* Context-dependent fields follow... */
52 union {
54 /* Page is in use: ((count_info & PGC_count_mask) != 0). */
55 struct {
56 /* Type reference count and various PGT_xxx flags and fields. */
57 unsigned long type_info;
58 } inuse;
60 /* Page is in use as a shadow: count_info == 0. */
61 struct {
62 unsigned long type:5; /* What kind of shadow is this? */
63 unsigned long pinned:1; /* Is the shadow pinned? */
64 unsigned long count:26; /* Reference count */
65 } sh;
67 /* Page is on a free list: ((count_info & PGC_count_mask) == 0). */
68 struct {
69 /* Do TLBs need flushing for safety before next page use? */
70 bool_t need_tlbflush;
71 } free;
73 } u;
75 union {
77 /* Page is in use, but not as a shadow. */
78 struct {
79 /* Owner of this page (zero if page is anonymous). */
80 __pdx_t _domain;
81 } inuse;
83 /* Page is in use as a shadow. */
84 struct {
85 /* GMFN of guest page we're a shadow of. */
86 __pdx_t back;
87 } sh;
89 /* Page is on a free list (including shadow code free lists). */
90 struct {
91 /* Order-size of the free chunk this page is the head of. */
92 unsigned int order;
93 } free;
95 } v;
97 union {
98 /*
99 * Timestamp from 'TLB clock', used to avoid extra safety flushes.
100 * Only valid for: a) free pages, and b) pages with zero type count
101 * (except page table pages when the guest is in shadow mode).
102 */
103 u32 tlbflush_timestamp;
105 /*
106 * When PGT_partial is true then this field is valid and indicates
107 * that PTEs in the range [0, @nr_validated_ptes) have been validated.
108 * An extra page reference must be acquired (or not dropped) whenever
109 * PGT_partial gets set, and it must be dropped when the flag gets
110 * cleared. This is so that a get() leaving a page in partially
111 * validated state (where the caller would drop the reference acquired
112 * due to the getting of the type [apparently] failing [-EAGAIN])
113 * would not accidentally result in a page left with zero general
114 * reference count, but non-zero type reference count (possible when
115 * the partial get() is followed immediately by domain destruction).
116 * Likewise, the ownership of the single type reference for partially
117 * (in-)validated pages is tied to this flag, i.e. the instance
118 * setting the flag must not drop that reference, whereas the instance
119 * clearing it will have to.
120 *
121 * If @partial_pte is positive then PTE at @nr_validated_ptes+1 has
122 * been partially validated. This implies that the general reference
123 * to the page (acquired from get_page_from_lNe()) would be dropped
124 * (again due to the apparent failure) and hence must be re-acquired
125 * when resuming the validation, but must not be dropped when picking
126 * up the page for invalidation.
127 *
128 * If @partial_pte is negative then PTE at @nr_validated_ptes+1 has
129 * been partially invalidated. This is basically the opposite case of
130 * above, i.e. the general reference to the page was not dropped in
131 * put_page_from_lNe() (due to the apparent failure), and hence it
132 * must be dropped when the put operation is resumed (and completes),
133 * but it must not be acquired if picking up the page for validation.
134 */
135 struct {
136 u16 nr_validated_ptes;
137 s8 partial_pte;
138 };
140 /*
141 * Guest pages with a shadow. This does not conflict with
142 * tlbflush_timestamp since page table pages are explicitly not
143 * tracked for TLB-flush avoidance when a guest runs in shadow mode.
144 */
145 u32 shadow_flags;
147 /* When in use as a shadow, next shadow in this hash chain. */
148 __pdx_t next_shadow;
149 };
150 };
152 #undef __pdx_t
154 #define PG_shift(idx) (BITS_PER_LONG - (idx))
155 #define PG_mask(x, idx) (x ## UL << PG_shift(idx))
157 /* The following page types are MUTUALLY EXCLUSIVE. */
158 #define PGT_none PG_mask(0, 3) /* no special uses of this page */
159 #define PGT_l1_page_table PG_mask(1, 3) /* using as an L1 page table? */
160 #define PGT_l2_page_table PG_mask(2, 3) /* using as an L2 page table? */
161 #define PGT_l3_page_table PG_mask(3, 3) /* using as an L3 page table? */
162 #define PGT_l4_page_table PG_mask(4, 3) /* using as an L4 page table? */
163 #define PGT_seg_desc_page PG_mask(5, 3) /* using this page in a GDT/LDT? */
164 #define PGT_writable_page PG_mask(7, 3) /* has writable mappings? */
165 #define PGT_type_mask PG_mask(7, 3) /* Bits 29-31. */
167 /* Owning guest has pinned this page to its current type? */
168 #define _PGT_pinned PG_shift(4)
169 #define PGT_pinned PG_mask(1, 4)
170 /* Has this page been validated for use as its current type? */
171 #define _PGT_validated PG_shift(5)
172 #define PGT_validated PG_mask(1, 5)
173 /* PAE only: is this an L2 page directory containing Xen-private mappings? */
174 #define _PGT_pae_xen_l2 PG_shift(6)
175 #define PGT_pae_xen_l2 PG_mask(1, 6)
176 /* Has this page been *partially* validated for use as its current type? */
177 #define _PGT_partial PG_shift(7)
178 #define PGT_partial PG_mask(1, 7)
179 /* Page is locked? */
180 #define _PGT_locked PG_shift(8)
181 #define PGT_locked PG_mask(1, 8)
183 /* Count of uses of this frame as its current type. */
184 #define PGT_count_width PG_shift(8)
185 #define PGT_count_mask ((1UL<<PGT_count_width)-1)
187 /* Cleared when the owning guest 'frees' this page. */
188 #define _PGC_allocated PG_shift(1)
189 #define PGC_allocated PG_mask(1, 1)
190 /* Page is Xen heap? */
191 #define _PGC_xen_heap PG_shift(2)
192 #define PGC_xen_heap PG_mask(1, 2)
193 /* Set when is using a page as a page table */
194 #define _PGC_page_table PG_shift(3)
195 #define PGC_page_table PG_mask(1, 3)
196 /* 3-bit PAT/PCD/PWT cache-attribute hint. */
197 #define PGC_cacheattr_base PG_shift(6)
198 #define PGC_cacheattr_mask PG_mask(7, 6)
199 /* Page is broken? */
200 #define _PGC_broken PG_shift(7)
201 #define PGC_broken PG_mask(1, 7)
202 /* Mutually-exclusive page states: { inuse, offlining, offlined, free }. */
203 #define PGC_state PG_mask(3, 9)
204 #define PGC_state_inuse PG_mask(0, 9)
205 #define PGC_state_offlining PG_mask(1, 9)
206 #define PGC_state_offlined PG_mask(2, 9)
207 #define PGC_state_free PG_mask(3, 9)
208 #define page_state_is(pg, st) (((pg)->count_info&PGC_state) == PGC_state_##st)
210 /* Count of references to this frame. */
211 #define PGC_count_width PG_shift(9)
212 #define PGC_count_mask ((1UL<<PGC_count_width)-1)
214 #if defined(__i386__)
215 #define is_xen_heap_page(page) is_xen_heap_mfn(page_to_mfn(page))
216 #define is_xen_heap_mfn(mfn) ({ \
217 unsigned long _mfn = (mfn); \
218 (_mfn < paddr_to_pfn(xenheap_phys_end)); \
219 })
220 #define is_xen_fixed_mfn(mfn) is_xen_heap_mfn(mfn)
221 #else
222 #define is_xen_heap_page(page) ((page)->count_info & PGC_xen_heap)
223 #define is_xen_heap_mfn(mfn) \
224 (__mfn_valid(mfn) && is_xen_heap_page(__mfn_to_page(mfn)))
225 #define is_xen_fixed_mfn(mfn) \
226 ((((mfn) << PAGE_SHIFT) >= __pa(&_start)) && \
227 (((mfn) << PAGE_SHIFT) <= __pa(&_end)))
228 #endif
230 #if defined(__i386__)
231 #define PRtype_info "08lx" /* should only be used for printk's */
232 #elif defined(__x86_64__)
233 #define PRtype_info "016lx"/* should only be used for printk's */
234 #endif
236 /* The order of the largest allocation unit we use for shadow pages */
237 #define SHADOW_MAX_ORDER 2 /* Need up to 16k allocs for 32-bit on PAE/64 */
239 /* The number of out-of-sync shadows we allow per vcpu (prime, please) */
240 #define SHADOW_OOS_PAGES 3
242 /* OOS fixup entries */
243 #define SHADOW_OOS_FIXUPS 2
245 #define page_get_owner(_p) \
246 ((struct domain *)((_p)->v.inuse._domain ? \
247 pdx_to_virt((_p)->v.inuse._domain) : NULL))
248 #define page_set_owner(_p,_d) \
249 ((_p)->v.inuse._domain = (_d) ? virt_to_pdx(_d) : 0)
251 #define maddr_get_owner(ma) (page_get_owner(maddr_to_page((ma))))
252 #define vaddr_get_owner(va) (page_get_owner(virt_to_page((va))))
254 #define XENSHARE_writable 0
255 #define XENSHARE_readonly 1
256 extern void share_xen_page_with_guest(
257 struct page_info *page, struct domain *d, int readonly);
258 extern void share_xen_page_with_privileged_guests(
259 struct page_info *page, int readonly);
261 #define frame_table ((struct page_info *)FRAMETABLE_VIRT_START)
262 extern unsigned long max_page;
263 extern unsigned long total_pages;
264 void init_frametable(void);
266 #define PDX_GROUP_COUNT ((1 << L2_PAGETABLE_SHIFT) / \
267 (sizeof(*frame_table) & -sizeof(*frame_table)))
268 extern unsigned long pdx_group_valid[];
270 /* Convert between Xen-heap virtual addresses and page-info structures. */
271 static inline struct page_info *__virt_to_page(const void *v)
272 {
273 unsigned long va = (unsigned long)v;
275 #ifdef __x86_64__
276 ASSERT(va >= XEN_VIRT_START);
277 ASSERT(va < DIRECTMAP_VIRT_END);
278 if ( va < XEN_VIRT_END )
279 va += DIRECTMAP_VIRT_START - XEN_VIRT_START + xen_phys_start;
280 else
281 ASSERT(va >= DIRECTMAP_VIRT_START);
282 #else
283 ASSERT(va - DIRECTMAP_VIRT_START < DIRECTMAP_VIRT_END);
284 #endif
285 return frame_table + ((va - DIRECTMAP_VIRT_START) >> PAGE_SHIFT);
286 }
288 static inline void *__page_to_virt(const struct page_info *pg)
289 {
290 ASSERT((unsigned long)pg - FRAMETABLE_VIRT_START < FRAMETABLE_VIRT_END);
291 return (void *)(DIRECTMAP_VIRT_START +
292 ((unsigned long)pg - FRAMETABLE_VIRT_START) /
293 (sizeof(*pg) / (sizeof(*pg) & -sizeof(*pg))) *
294 (PAGE_SIZE / (sizeof(*pg) & -sizeof(*pg))));
295 }
297 int free_page_type(struct page_info *page, unsigned long type,
298 int preemptible);
299 int _shadow_mode_refcounts(struct domain *d);
301 void cleanup_page_cacheattr(struct page_info *page);
303 int is_iomem_page(unsigned long mfn);
305 struct domain *page_get_owner_and_reference(struct page_info *page);
306 void put_page(struct page_info *page);
307 int get_page(struct page_info *page, struct domain *domain);
308 void put_page_type(struct page_info *page);
309 int get_page_type(struct page_info *page, unsigned long type);
310 int put_page_type_preemptible(struct page_info *page);
311 int get_page_type_preemptible(struct page_info *page, unsigned long type);
312 int get_page_from_l1e(
313 l1_pgentry_t l1e, struct domain *l1e_owner, struct domain *pg_owner);
314 void put_page_from_l1e(l1_pgentry_t l1e, struct domain *l1e_owner);
316 static inline void put_page_and_type(struct page_info *page)
317 {
318 put_page_type(page);
319 put_page(page);
320 }
322 static inline int put_page_and_type_preemptible(struct page_info *page,
323 int preemptible)
324 {
325 int rc = 0;
327 if ( preemptible )
328 rc = put_page_type_preemptible(page);
329 else
330 put_page_type(page);
331 if ( likely(rc == 0) )
332 put_page(page);
333 return rc;
334 }
336 static inline int get_page_and_type(struct page_info *page,
337 struct domain *domain,
338 unsigned long type)
339 {
340 int rc = get_page(page, domain);
342 if ( likely(rc) && unlikely(!get_page_type(page, type)) )
343 {
344 put_page(page);
345 rc = 0;
346 }
348 return rc;
349 }
351 #define ASSERT_PAGE_IS_TYPE(_p, _t) \
352 ASSERT(((_p)->u.inuse.type_info & PGT_type_mask) == (_t)); \
353 ASSERT(((_p)->u.inuse.type_info & PGT_count_mask) != 0)
354 #define ASSERT_PAGE_IS_DOMAIN(_p, _d) \
355 ASSERT(((_p)->count_info & PGC_count_mask) != 0); \
356 ASSERT(page_get_owner(_p) == (_d))
358 // Quick test for whether a given page can be represented directly in CR3.
359 //
360 #if CONFIG_PAGING_LEVELS == 3
361 #define MFN_FITS_IN_CR3(_MFN) !(mfn_x(_MFN) >> 20)
363 /* returns a lowmem machine address of the copied L3 root table */
364 unsigned long
365 pae_copy_root(struct vcpu *v, l3_pgentry_t *l3tab);
366 #endif /* CONFIG_PAGING_LEVELS == 3 */
368 int check_descriptor(const struct domain *, struct desc_struct *d);
370 extern int opt_allow_hugepage;
371 extern int mem_hotplug;
373 /******************************************************************************
374 * With shadow pagetables, the different kinds of address start
375 * to get get confusing.
376 *
377 * Virtual addresses are what they usually are: the addresses that are used
378 * to accessing memory while the guest is running. The MMU translates from
379 * virtual addresses to machine addresses.
380 *
381 * (Pseudo-)physical addresses are the abstraction of physical memory the
382 * guest uses for allocation and so forth. For the purposes of this code,
383 * we can largely ignore them.
384 *
385 * Guest frame numbers (gfns) are the entries that the guest puts in its
386 * pagetables. For normal paravirtual guests, they are actual frame numbers,
387 * with the translation done by the guest.
388 *
389 * Machine frame numbers (mfns) are the entries that the hypervisor puts
390 * in the shadow page tables.
391 *
392 * Elsewhere in the xen code base, the name "gmfn" is generally used to refer
393 * to a "machine frame number, from the guest's perspective", or in other
394 * words, pseudo-physical frame numbers. However, in the shadow code, the
395 * term "gmfn" means "the mfn of a guest page"; this combines naturally with
396 * other terms such as "smfn" (the mfn of a shadow page), gl2mfn (the mfn of a
397 * guest L2 page), etc...
398 */
400 /* With this defined, we do some ugly things to force the compiler to
401 * give us type safety between mfns and gfns and other integers.
402 * TYPE_SAFE(int foo) defines a foo_t, and _foo() and foo_x() functions
403 * that translate beween int and foo_t.
404 *
405 * It does have some performance cost because the types now have
406 * a different storage attribute, so may not want it on all the time. */
408 #ifndef NDEBUG
409 #define TYPE_SAFETY 1
410 #endif
412 #ifdef TYPE_SAFETY
413 #define TYPE_SAFE(_type,_name) \
414 typedef struct { _type _name; } _name##_t; \
415 static inline _name##_t _##_name(_type n) { return (_name##_t) { n }; } \
416 static inline _type _name##_x(_name##_t n) { return n._name; }
417 #else
418 #define TYPE_SAFE(_type,_name) \
419 typedef _type _name##_t; \
420 static inline _name##_t _##_name(_type n) { return n; } \
421 static inline _type _name##_x(_name##_t n) { return n; }
422 #endif
424 TYPE_SAFE(unsigned long,mfn);
426 /* Macro for printk formats: use as printk("%"PRI_mfn"\n", mfn_x(foo)); */
427 #define PRI_mfn "05lx"
430 /*
431 * The MPT (machine->physical mapping table) is an array of word-sized
432 * values, indexed on machine frame number. It is expected that guest OSes
433 * will use it to store a "physical" frame number to give the appearance of
434 * contiguous (or near contiguous) physical memory.
435 */
436 #undef machine_to_phys_mapping
437 #define machine_to_phys_mapping ((unsigned long *)RDWR_MPT_VIRT_START)
438 #define INVALID_M2P_ENTRY (~0UL)
439 #define VALID_M2P(_e) (!((_e) & (1UL<<(BITS_PER_LONG-1))))
441 #ifdef CONFIG_COMPAT
442 #define compat_machine_to_phys_mapping ((unsigned int *)RDWR_COMPAT_MPT_VIRT_START)
443 #define set_gpfn_from_mfn(mfn, pfn) \
444 ((void)((mfn) >= (RDWR_COMPAT_MPT_VIRT_END - RDWR_COMPAT_MPT_VIRT_START) / 4 || \
445 (compat_machine_to_phys_mapping[(mfn)] = (unsigned int)(pfn))), \
446 machine_to_phys_mapping[(mfn)] = (pfn))
447 #else
448 #define set_gpfn_from_mfn(mfn, pfn) (machine_to_phys_mapping[(mfn)] = (pfn))
449 #endif
450 #define get_gpfn_from_mfn(mfn) (machine_to_phys_mapping[(mfn)])
452 #define mfn_to_gmfn(_d, mfn) \
453 ( (paging_mode_translate(_d)) \
454 ? get_gpfn_from_mfn(mfn) \
455 : (mfn) )
457 #define INVALID_MFN (~0UL)
459 #define compat_pfn_to_cr3(pfn) (((unsigned)(pfn) << 12) | ((unsigned)(pfn) >> 20))
460 #define compat_cr3_to_pfn(cr3) (((unsigned)(cr3) >> 12) | ((unsigned)(cr3) << 20))
462 #ifdef MEMORY_GUARD
463 void memguard_init(void);
464 void memguard_guard_range(void *p, unsigned long l);
465 void memguard_unguard_range(void *p, unsigned long l);
466 #else
467 #define memguard_init() ((void)0)
468 #define memguard_guard_range(_p,_l) ((void)0)
469 #define memguard_unguard_range(_p,_l) ((void)0)
470 #endif
472 void memguard_guard_stack(void *p);
474 int ptwr_do_page_fault(struct vcpu *, unsigned long,
475 struct cpu_user_regs *);
477 int audit_adjust_pgtables(struct domain *d, int dir, int noisy);
479 #ifdef CONFIG_X86_64
480 extern int pagefault_by_memadd(unsigned long addr, struct cpu_user_regs *regs);
481 extern int handle_memadd_fault(unsigned long addr, struct cpu_user_regs *regs);
482 #else
483 static inline int pagefault_by_memadd(unsigned long addr,
484 struct cpu_user_regs *regs)
485 {
486 return 0;
487 }
489 static inline int handle_memadd_fault(unsigned long addr,
490 struct cpu_user_regs *regs)
491 {
492 return 0;
493 }
494 #endif
496 #ifndef NDEBUG
498 #define AUDIT_SHADOW_ALREADY_LOCKED ( 1u << 0 )
499 #define AUDIT_ERRORS_OK ( 1u << 1 )
500 #define AUDIT_QUIET ( 1u << 2 )
502 void _audit_domain(struct domain *d, int flags);
503 #define audit_domain(_d) _audit_domain((_d), AUDIT_ERRORS_OK)
504 void audit_domains(void);
506 #else
508 #define _audit_domain(_d, _f) ((void)0)
509 #define audit_domain(_d) ((void)0)
510 #define audit_domains() ((void)0)
512 #endif
514 int new_guest_cr3(unsigned long pfn);
515 void make_cr3(struct vcpu *v, unsigned long mfn);
516 void update_cr3(struct vcpu *v);
517 void propagate_page_fault(unsigned long addr, u16 error_code);
518 void *do_page_walk(struct vcpu *v, unsigned long addr);
520 int __sync_lazy_execstate(void);
522 /* Arch-specific portion of memory_op hypercall. */
523 long arch_memory_op(int op, XEN_GUEST_HANDLE(void) arg);
524 long subarch_memory_op(int op, XEN_GUEST_HANDLE(void) arg);
525 int compat_arch_memory_op(int op, XEN_GUEST_HANDLE(void));
526 int compat_subarch_memory_op(int op, XEN_GUEST_HANDLE(void));
528 int steal_page(
529 struct domain *d, struct page_info *page, unsigned int memflags);
530 int donate_page(
531 struct domain *d, struct page_info *page, unsigned int memflags);
533 int map_ldt_shadow_page(unsigned int);
535 #ifdef CONFIG_X86_64
536 extern int memory_add(unsigned long spfn, unsigned long epfn, unsigned int pxm);
537 #else
538 static inline int memory_add(uint64_t spfn, uint64_t epfn, uint32_t pxm)
539 {
540 return -ENOSYS;
541 }
542 #endif
544 #ifdef CONFIG_COMPAT
545 void domain_set_alloc_bitsize(struct domain *d);
546 unsigned int domain_clamp_alloc_bitsize(struct domain *d, unsigned int bits);
547 #else
548 # define domain_set_alloc_bitsize(d) ((void)0)
549 # define domain_clamp_alloc_bitsize(d, b) (b)
550 #endif
552 unsigned long domain_get_maximum_gpfn(struct domain *d);
554 extern struct domain *dom_xen, *dom_io; /* for vmcoreinfo */
556 #endif /* __ASM_X86_MM_H__ */