ia64/xen-unstable

view xen/include/asm-x86/mm.h @ 19092:beba88f6f90d

x86: Clean up pickle_domptr().
Signed-off-by: Keir Fraser <keir.fraser@citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Mon Jan 26 16:34:16 2009 +0000 (2009-01-26)
parents 39517e863cc8
children 6e623569455c
line source
2 #ifndef __ASM_X86_MM_H__
3 #define __ASM_X86_MM_H__
5 #include <xen/config.h>
6 #include <xen/cpumask.h>
7 #include <xen/list.h>
8 #include <asm/io.h>
9 #include <asm/uaccess.h>
11 /*
12 * Per-page-frame information.
13 *
14 * Every architecture must ensure the following:
15 * 1. 'struct page_info' contains a 'struct list_head list'.
16 * 2. Provide a PFN_ORDER() macro for accessing the order of a free page.
17 */
18 #define PFN_ORDER(_pfn) ((_pfn)->u.free.order)
20 struct page_info
21 {
22 /* Each frame can be threaded onto a doubly-linked list. */
23 struct list_head list;
25 /* Reference count and various PGC_xxx flags and fields. */
26 unsigned long count_info;
28 /* Context-dependent fields follow... */
29 union {
31 /* Page is in use: ((count_info & PGC_count_mask) != 0). */
32 struct {
33 /* Owner of this page (NULL if page is anonymous). */
34 unsigned long _domain; /* pickled format */
35 /* Type reference count and various PGT_xxx flags and fields. */
36 unsigned long type_info;
37 } __attribute__ ((packed)) inuse;
39 /* Page is on a free list: ((count_info & PGC_count_mask) == 0). */
40 struct {
41 /* Order-size of the free chunk this page is the head of. */
42 u32 order;
43 /* Mask of possibly-tainted TLBs. */
44 cpumask_t cpumask;
45 } __attribute__ ((packed)) free;
47 } u;
49 #if defined(__x86_64__)
50 spinlock_t lock;
51 #endif
53 union {
54 /*
55 * Timestamp from 'TLB clock', used to avoid extra safety flushes.
56 * Only valid for: a) free pages, and b) pages with zero type count
57 * (except page table pages when the guest is in shadow mode).
58 */
59 u32 tlbflush_timestamp;
61 /*
62 * When PGT_partial is true then this field is valid and indicates
63 * that PTEs in the range [0, @nr_validated_ptes) have been validated.
64 * An extra page reference must be acquired (or not dropped) whenever
65 * PGT_partial gets set, and it must be dropped when the flag gets
66 * cleared. This is so that a get() leaving a page in partially
67 * validated state (where the caller would drop the reference acquired
68 * due to the getting of the type [apparently] failing [-EAGAIN])
69 * would not accidentally result in a page left with zero general
70 * reference count, but non-zero type reference count (possible when
71 * the partial get() is followed immediately by domain destruction).
72 * Likewise, the ownership of the single type reference for partially
73 * (in-)validated pages is tied to this flag, i.e. the instance
74 * setting the flag must not drop that reference, whereas the instance
75 * clearing it will have to.
76 *
77 * If @partial_pte is positive then PTE at @nr_validated_ptes+1 has
78 * been partially validated. This implies that the general reference
79 * to the page (acquired from get_page_from_lNe()) would be dropped
80 * (again due to the apparent failure) and hence must be re-acquired
81 * when resuming the validation, but must not be dropped when picking
82 * up the page for invalidation.
83 *
84 * If @partial_pte is negative then PTE at @nr_validated_ptes+1 has
85 * been partially invalidated. This is basically the opposite case of
86 * above, i.e. the general reference to the page was not dropped in
87 * put_page_from_lNe() (due to the apparent failure), and hence it
88 * must be dropped when the put operation is resumed (and completes),
89 * but it must not be acquired if picking up the page for validation.
90 */
91 struct {
92 u16 nr_validated_ptes;
93 s8 partial_pte;
94 };
96 /*
97 * Guest pages with a shadow. This does not conflict with
98 * tlbflush_timestamp since page table pages are explicitly not
99 * tracked for TLB-flush avoidance when a guest runs in shadow mode.
100 */
101 u32 shadow_flags;
102 };
103 };
105 #define PG_shift(idx) (BITS_PER_LONG - (idx))
106 #define PG_mask(x, idx) (x ## UL << PG_shift(idx))
108 /* The following page types are MUTUALLY EXCLUSIVE. */
109 #define PGT_none PG_mask(0, 3) /* no special uses of this page */
110 #define PGT_l1_page_table PG_mask(1, 3) /* using as an L1 page table? */
111 #define PGT_l2_page_table PG_mask(2, 3) /* using as an L2 page table? */
112 #define PGT_l3_page_table PG_mask(3, 3) /* using as an L3 page table? */
113 #define PGT_l4_page_table PG_mask(4, 3) /* using as an L4 page table? */
114 #define PGT_seg_desc_page PG_mask(5, 3) /* using this page in a GDT/LDT? */
115 #define PGT_writable_page PG_mask(7, 3) /* has writable mappings? */
116 #define PGT_type_mask PG_mask(7, 3) /* Bits 29-31. */
118 /* Owning guest has pinned this page to its current type? */
119 #define _PGT_pinned PG_shift(4)
120 #define PGT_pinned PG_mask(1, 4)
121 /* Has this page been validated for use as its current type? */
122 #define _PGT_validated PG_shift(5)
123 #define PGT_validated PG_mask(1, 5)
124 /* PAE only: is this an L2 page directory containing Xen-private mappings? */
125 #define _PGT_pae_xen_l2 PG_shift(6)
126 #define PGT_pae_xen_l2 PG_mask(1, 6)
127 /* Has this page been *partially* validated for use as its current type? */
128 #define _PGT_partial PG_shift(7)
129 #define PGT_partial PG_mask(1, 7)
131 /* Count of uses of this frame as its current type. */
132 #define PGT_count_width PG_shift(7)
133 #define PGT_count_mask ((1UL<<PGT_count_width)-1)
135 /* Cleared when the owning guest 'frees' this page. */
136 #define _PGC_allocated PG_shift(1)
137 #define PGC_allocated PG_mask(1, 1)
138 #if defined(__i386__)
139 /* Page is locked? */
140 # define _PGC_locked PG_shift(2)
141 # define PGC_locked PG_mask(1, 2)
142 #else
143 /* Page is Xen heap? */
144 # define _PGC_xen_heap PG_shift(2)
145 # define PGC_xen_heap PG_mask(1, 2)
146 #endif
147 /* Set when is using a page as a page table */
148 #define _PGC_page_table PG_shift(3)
149 #define PGC_page_table PG_mask(1, 3)
150 /* 3-bit PAT/PCD/PWT cache-attribute hint. */
151 #define PGC_cacheattr_base PG_shift(6)
152 #define PGC_cacheattr_mask PG_mask(7, 6)
153 /* Count of references to this frame. */
154 #define PGC_count_width PG_shift(6)
155 #define PGC_count_mask ((1UL<<PGC_count_width)-1)
157 #if defined(__i386__)
158 #define is_xen_heap_page(page) is_xen_heap_mfn(page_to_mfn(page))
159 #define is_xen_heap_mfn(mfn) ({ \
160 unsigned long _mfn = (mfn); \
161 (_mfn < paddr_to_pfn(xenheap_phys_end)); \
162 })
163 #else
164 #define is_xen_heap_page(page) ((page)->count_info & PGC_xen_heap)
165 #define is_xen_heap_mfn(mfn) is_xen_heap_page(&frame_table[mfn])
166 #endif
168 #if defined(__i386__)
169 #define PRtype_info "08lx" /* should only be used for printk's */
170 #elif defined(__x86_64__)
171 #define PRtype_info "016lx"/* should only be used for printk's */
172 #endif
174 /* The order of the largest allocation unit we use for shadow pages */
175 #define SHADOW_MAX_ORDER 2 /* Need up to 16k allocs for 32-bit on PAE/64 */
177 /* The number of out-of-sync shadows we allow per vcpu (prime, please) */
178 #define SHADOW_OOS_PAGES 3
180 /* OOS fixup entries */
181 #define SHADOW_OOS_FIXUPS 2
183 #define page_get_owner(_p) ((struct domain *)(_p)->u.inuse._domain)
184 #define page_set_owner(_p,_d) ((_p)->u.inuse._domain = (unsigned long)(_d))
186 #define maddr_get_owner(ma) (page_get_owner(maddr_to_page((ma))))
187 #define vaddr_get_owner(va) (page_get_owner(virt_to_page((va))))
189 #define XENSHARE_writable 0
190 #define XENSHARE_readonly 1
191 extern void share_xen_page_with_guest(
192 struct page_info *page, struct domain *d, int readonly);
193 extern void share_xen_page_with_privileged_guests(
194 struct page_info *page, int readonly);
196 extern struct page_info *frame_table;
197 extern unsigned long max_page;
198 extern unsigned long total_pages;
199 void init_frametable(void);
201 int free_page_type(struct page_info *page, unsigned long type,
202 int preemptible);
203 int _shadow_mode_refcounts(struct domain *d);
205 void cleanup_page_cacheattr(struct page_info *page);
207 int is_iomem_page(unsigned long mfn);
209 void put_page(struct page_info *page);
210 int get_page(struct page_info *page, struct domain *domain);
211 void put_page_type(struct page_info *page);
212 int get_page_type(struct page_info *page, unsigned long type);
213 int put_page_type_preemptible(struct page_info *page);
214 int get_page_type_preemptible(struct page_info *page, unsigned long type);
215 int get_page_from_l1e(l1_pgentry_t l1e, struct domain *d);
216 void put_page_from_l1e(l1_pgentry_t l1e, struct domain *d);
218 static inline void put_page_and_type(struct page_info *page)
219 {
220 put_page_type(page);
221 put_page(page);
222 }
224 static inline int put_page_and_type_preemptible(struct page_info *page,
225 int preemptible)
226 {
227 int rc = 0;
229 if ( preemptible )
230 rc = put_page_type_preemptible(page);
231 else
232 put_page_type(page);
233 if ( likely(rc == 0) )
234 put_page(page);
235 return rc;
236 }
238 static inline int get_page_and_type(struct page_info *page,
239 struct domain *domain,
240 unsigned long type)
241 {
242 int rc = get_page(page, domain);
244 if ( likely(rc) && unlikely(!get_page_type(page, type)) )
245 {
246 put_page(page);
247 rc = 0;
248 }
250 return rc;
251 }
253 #define ASSERT_PAGE_IS_TYPE(_p, _t) \
254 ASSERT(((_p)->u.inuse.type_info & PGT_type_mask) == (_t)); \
255 ASSERT(((_p)->u.inuse.type_info & PGT_count_mask) != 0)
256 #define ASSERT_PAGE_IS_DOMAIN(_p, _d) \
257 ASSERT(((_p)->count_info & PGC_count_mask) != 0); \
258 ASSERT(page_get_owner(_p) == (_d))
260 // Quick test for whether a given page can be represented directly in CR3.
261 //
262 #if CONFIG_PAGING_LEVELS == 3
263 #define MFN_FITS_IN_CR3(_MFN) !(mfn_x(_MFN) >> 20)
265 /* returns a lowmem machine address of the copied L3 root table */
266 unsigned long
267 pae_copy_root(struct vcpu *v, l3_pgentry_t *l3tab);
268 #endif /* CONFIG_PAGING_LEVELS == 3 */
270 int check_descriptor(const struct domain *, struct desc_struct *d);
272 extern int opt_allow_hugepage;
274 /******************************************************************************
275 * With shadow pagetables, the different kinds of address start
276 * to get get confusing.
277 *
278 * Virtual addresses are what they usually are: the addresses that are used
279 * to accessing memory while the guest is running. The MMU translates from
280 * virtual addresses to machine addresses.
281 *
282 * (Pseudo-)physical addresses are the abstraction of physical memory the
283 * guest uses for allocation and so forth. For the purposes of this code,
284 * we can largely ignore them.
285 *
286 * Guest frame numbers (gfns) are the entries that the guest puts in its
287 * pagetables. For normal paravirtual guests, they are actual frame numbers,
288 * with the translation done by the guest.
289 *
290 * Machine frame numbers (mfns) are the entries that the hypervisor puts
291 * in the shadow page tables.
292 *
293 * Elsewhere in the xen code base, the name "gmfn" is generally used to refer
294 * to a "machine frame number, from the guest's perspective", or in other
295 * words, pseudo-physical frame numbers. However, in the shadow code, the
296 * term "gmfn" means "the mfn of a guest page"; this combines naturally with
297 * other terms such as "smfn" (the mfn of a shadow page), gl2mfn (the mfn of a
298 * guest L2 page), etc...
299 */
301 /* With this defined, we do some ugly things to force the compiler to
302 * give us type safety between mfns and gfns and other integers.
303 * TYPE_SAFE(int foo) defines a foo_t, and _foo() and foo_x() functions
304 * that translate beween int and foo_t.
305 *
306 * It does have some performance cost because the types now have
307 * a different storage attribute, so may not want it on all the time. */
309 #ifndef NDEBUG
310 #define TYPE_SAFETY 1
311 #endif
313 #ifdef TYPE_SAFETY
314 #define TYPE_SAFE(_type,_name) \
315 typedef struct { _type _name; } _name##_t; \
316 static inline _name##_t _##_name(_type n) { return (_name##_t) { n }; } \
317 static inline _type _name##_x(_name##_t n) { return n._name; }
318 #else
319 #define TYPE_SAFE(_type,_name) \
320 typedef _type _name##_t; \
321 static inline _name##_t _##_name(_type n) { return n; } \
322 static inline _type _name##_x(_name##_t n) { return n; }
323 #endif
325 TYPE_SAFE(unsigned long,mfn);
327 /* Macro for printk formats: use as printk("%"PRI_mfn"\n", mfn_x(foo)); */
328 #define PRI_mfn "05lx"
331 /*
332 * The MPT (machine->physical mapping table) is an array of word-sized
333 * values, indexed on machine frame number. It is expected that guest OSes
334 * will use it to store a "physical" frame number to give the appearance of
335 * contiguous (or near contiguous) physical memory.
336 */
337 #undef machine_to_phys_mapping
338 #define machine_to_phys_mapping ((unsigned long *)RDWR_MPT_VIRT_START)
339 #define INVALID_M2P_ENTRY (~0UL)
340 #define VALID_M2P(_e) (!((_e) & (1UL<<(BITS_PER_LONG-1))))
342 #ifdef CONFIG_COMPAT
343 #define compat_machine_to_phys_mapping ((unsigned int *)RDWR_COMPAT_MPT_VIRT_START)
344 #define set_gpfn_from_mfn(mfn, pfn) \
345 ((void)((mfn) >= (RDWR_COMPAT_MPT_VIRT_END - RDWR_COMPAT_MPT_VIRT_START) / 4 || \
346 (compat_machine_to_phys_mapping[(mfn)] = (unsigned int)(pfn))), \
347 machine_to_phys_mapping[(mfn)] = (pfn))
348 #else
349 #define set_gpfn_from_mfn(mfn, pfn) (machine_to_phys_mapping[(mfn)] = (pfn))
350 #endif
351 #define get_gpfn_from_mfn(mfn) (machine_to_phys_mapping[(mfn)])
353 #define mfn_to_gmfn(_d, mfn) \
354 ( (paging_mode_translate(_d)) \
355 ? get_gpfn_from_mfn(mfn) \
356 : (mfn) )
358 #define INVALID_MFN (~0UL)
360 #ifdef CONFIG_COMPAT
361 #define compat_pfn_to_cr3(pfn) (((unsigned)(pfn) << 12) | ((unsigned)(pfn) >> 20))
362 #define compat_cr3_to_pfn(cr3) (((unsigned)(cr3) >> 12) | ((unsigned)(cr3) << 20))
363 #endif
365 #ifdef MEMORY_GUARD
366 void memguard_init(void);
367 void memguard_guard_range(void *p, unsigned long l);
368 void memguard_unguard_range(void *p, unsigned long l);
369 #else
370 #define memguard_init() ((void)0)
371 #define memguard_guard_range(_p,_l) ((void)0)
372 #define memguard_unguard_range(_p,_l) ((void)0)
373 #endif
375 void memguard_guard_stack(void *p);
377 int ptwr_do_page_fault(struct vcpu *, unsigned long,
378 struct cpu_user_regs *);
380 int audit_adjust_pgtables(struct domain *d, int dir, int noisy);
382 #ifndef NDEBUG
384 #define AUDIT_SHADOW_ALREADY_LOCKED ( 1u << 0 )
385 #define AUDIT_ERRORS_OK ( 1u << 1 )
386 #define AUDIT_QUIET ( 1u << 2 )
388 void _audit_domain(struct domain *d, int flags);
389 #define audit_domain(_d) _audit_domain((_d), AUDIT_ERRORS_OK)
390 void audit_domains(void);
392 #else
394 #define _audit_domain(_d, _f) ((void)0)
395 #define audit_domain(_d) ((void)0)
396 #define audit_domains() ((void)0)
398 #endif
400 int new_guest_cr3(unsigned long pfn);
401 void make_cr3(struct vcpu *v, unsigned long mfn);
402 void update_cr3(struct vcpu *v);
403 void propagate_page_fault(unsigned long addr, u16 error_code);
405 int __sync_lazy_execstate(void);
407 /* Arch-specific portion of memory_op hypercall. */
408 long arch_memory_op(int op, XEN_GUEST_HANDLE(void) arg);
409 long subarch_memory_op(int op, XEN_GUEST_HANDLE(void) arg);
410 #ifdef CONFIG_COMPAT
411 int compat_arch_memory_op(int op, XEN_GUEST_HANDLE(void));
412 int compat_subarch_memory_op(int op, XEN_GUEST_HANDLE(void));
413 #endif
415 int steal_page(
416 struct domain *d, struct page_info *page, unsigned int memflags);
418 int map_ldt_shadow_page(unsigned int);
420 #ifdef CONFIG_COMPAT
421 void domain_set_alloc_bitsize(struct domain *d);
422 unsigned int domain_clamp_alloc_bitsize(struct domain *d, unsigned int bits);
423 #else
424 # define domain_set_alloc_bitsize(d) ((void)0)
425 # define domain_clamp_alloc_bitsize(d, b) (b)
426 #endif
428 unsigned long domain_get_maximum_gpfn(struct domain *d);
430 extern struct domain *dom_xen, *dom_io; /* for vmcoreinfo */
432 #endif /* __ASM_X86_MM_H__ */