Xen Test Framework
mm.h
Go to the documentation of this file.
1#ifndef XTF_X86_MM_H
2#define XTF_X86_MM_H
3
4#include <xtf/compiler.h>
5#include <xtf/types.h>
6
7#include <arch/page.h>
8#include <arch/traps.h>
9
10#include <xen/xen.h>
11
12/*
13 * Terminology (inherited from Xen):
14 *
15 * GFN - Guest Frame Number
16 * What a guest writes into its pagetables.
17 * MFN - Machine Frame Number
18 * What Xen writes into its pagetables.
19 * PFN - Pseudophysical Frame Number
20 * A linear idea of a guests physical address space.
21 *
22 * For HVM, PFN == GFN, and MFN is strictly irrelevent.
23 * For PV, MFN == GFN != PFN.
24 *
25 * XTF memory layout.
26 *
27 * Wherever possible, identity layout for simplicity. Flat segments are also
28 * used, so virtual address == linear address.
29 *
30 * PV guests: VIRT_OFFSET is 0 which causes all linked virtual addresses to be
31 * contiguous in the pagetables created by the domain builder. Therefore,
32 * virt == pfn << PAGE_SHIFT for any pfn constructed by the domain builder.
33 *
34 * HVM guests: All memory from 0 to 4GB is identity mapped.
35 */
36
37static inline void *pfn_to_virt(unsigned long pfn)
38{
39 return (void *)(pfn << PAGE_SHIFT);
40}
41
42static inline unsigned long virt_to_pfn(const void *va)
43{
44 return _u(va) >> PAGE_SHIFT;
45}
46
47#if defined(CONFIG_PV)
48
49#define m2p ((unsigned long *)MACH2PHYS_VIRT_START)
50
51static inline void *mfn_to_virt(unsigned long mfn)
52{
53 return pfn_to_virt(m2p[mfn]);
54}
55
56static inline void *maddr_to_virt(uint64_t maddr)
57{
58 return mfn_to_virt(maddr >> PAGE_SHIFT) + (maddr & ~PAGE_MASK);
59}
60
61static inline unsigned long pfn_to_mfn(unsigned long pfn)
62{
63 unsigned long *p2m = _p(pv_start_info->mfn_list);
64
65 return p2m[pfn];
66}
67
68static inline unsigned long virt_to_mfn(const void *va)
69{
70 return pfn_to_mfn(virt_to_pfn(va));
71}
72
73static inline uint64_t virt_to_maddr(const void *va)
74{
75 return ((uint64_t)virt_to_mfn(va) << PAGE_SHIFT) + (_u(va) & ~PAGE_MASK);
76}
77
78#undef m2p
79
80#else /* CONFIG_PV */
81
82/* Enough compatibility to compile in HVM environments. */
83extern void *mfn_to_virt(unsigned long mfn);
84extern void *maddr_to_virt(uint64_t maddr);
85extern unsigned long pfn_to_mfn(unsigned long pfn);
86extern unsigned long virt_to_mfn(const void *va);
87extern uint64_t virt_to_maddr(const void *va);
88
89#endif /* CONFIG_PV */
90
91static inline void *gfn_to_virt(unsigned long gfn)
92{
93#if defined(CONFIG_PV)
94 return mfn_to_virt(gfn);
95#else
96 return pfn_to_virt(gfn);
97#endif
98}
99
100static inline unsigned long virt_to_gfn(const void *va)
101{
102#if defined(CONFIG_PV)
103 return virt_to_mfn(va);
104#else
105 return virt_to_pfn(va);
106#endif
107}
108
109#endif /* XTF_X86_MM_H */
110
111/*
112 * Local variables:
113 * mode: C
114 * c-file-style: "BSD"
115 * c-basic-offset: 4
116 * tab-width: 4
117 * indent-tabs-mode: nil
118 * End:
119 */
xen_pv_start_info_t * pv_start_info
Definition: traps.c:14
void * maddr_to_virt(uint64_t maddr)
unsigned long pfn_to_mfn(unsigned long pfn)
unsigned long virt_to_mfn(const void *va)
void * mfn_to_virt(unsigned long mfn)
static void * gfn_to_virt(unsigned long gfn)
Definition: mm.h:91
uint64_t virt_to_maddr(const void *va)
static void * pfn_to_virt(unsigned long pfn)
Definition: mm.h:37
static unsigned long virt_to_pfn(const void *va)
Definition: mm.h:42
static unsigned long virt_to_gfn(const void *va)
Definition: mm.h:100
#define _p(v)
Express an abitrary integer v as void *.
Definition: numbers.h:48
#define _u(v)
Express an arbitrary value v as unsigned long.
Definition: numbers.h:53
#define PAGE_SHIFT
Definition: page.h:10
__UINT64_TYPE__ uint64_t
Definition: stdint.h:17
unsigned long mfn_list
Definition: xen.h:225
Common declarations for all tests.