1 /*
2 * Definitions and utilities for save / restore.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation;
7 * version 2.1 of the License.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "xc_private.h"
19
20 #include <xen/foreign/x86_32.h>
21 #include <xen/foreign/x86_64.h>
22
23 /*
24 ** We process save/restore/migrate in batches of pages; the below
25 ** determines how many pages we (at maximum) deal with in each batch.
26 */
27 #define MAX_BATCH_SIZE 1024 /* up to 1024 pages (4MB) at a time */
28
29 /* When pinning page tables at the end of restore, we also use batching. */
30 #define MAX_PIN_BATCH 1024
31
32 /*
33 ** Determine various platform information required for save/restore, in
34 ** particular:
35 **
36 ** - the maximum MFN on this machine, used to compute the size of
37 ** the M2P table;
38 **
39 ** - the starting virtual address of the the hypervisor; we use this
40 ** to determine which parts of guest address space(s) do and don't
41 ** require canonicalization during save/restore; and
42 **
43 ** - the number of page-table levels for save/ restore. This should
44 ** be a property of the domain, but for the moment we just read it
45 ** from the hypervisor.
46 **
47 ** - The width of a guest word (unsigned long), in bytes.
48 **
49 ** Returns 1 on success, 0 on failure.
50 */
get_platform_info(xc_interface * xch,uint32_t dom,unsigned long * max_mfn,unsigned long * hvirt_start,unsigned int * pt_levels,unsigned int * guest_width)51 static inline int get_platform_info(xc_interface *xch, uint32_t dom,
52 /* OUT */ unsigned long *max_mfn,
53 /* OUT */ unsigned long *hvirt_start,
54 /* OUT */ unsigned int *pt_levels,
55 /* OUT */ unsigned int *guest_width)
56 {
57 xen_capabilities_info_t xen_caps = "";
58 xen_platform_parameters_t xen_params;
59
60 if (xc_version(xch, XENVER_platform_parameters, &xen_params) != 0)
61 return 0;
62
63 if (xc_version(xch, XENVER_capabilities, &xen_caps) != 0)
64 return 0;
65
66 if (xc_maximum_ram_page(xch, max_mfn))
67 return 0;
68
69 *hvirt_start = xen_params.virt_start;
70
71 if ( xc_domain_get_guest_width(xch, dom, guest_width) != 0)
72 return 0;
73
74 /* 64-bit tools will see the 64-bit hvirt_start, but 32-bit guests
75 * will be using the compat one. */
76 if ( *guest_width < sizeof (unsigned long) )
77 /* XXX need to fix up a way of extracting this value from Xen if
78 * XXX it becomes variable for domU */
79 *hvirt_start = 0xf5800000;
80
81 if (strstr(xen_caps, "xen-3.0-x86_64"))
82 /* Depends on whether it's a compat 32-on-64 guest */
83 *pt_levels = ( (*guest_width == 8) ? 4 : 3 );
84 else if (strstr(xen_caps, "xen-3.0-x86_32p"))
85 *pt_levels = 3;
86 else
87 return 0;
88
89 return 1;
90 }
91
92
93 /*
94 ** Save/restore deal with the mfn_to_pfn (M2P) and pfn_to_mfn (P2M) tables.
95 ** The M2P simply holds the corresponding PFN, while the top bit of a P2M
96 ** entry tell us whether or not the the PFN is currently mapped.
97 */
98
99 #define PFN_TO_KB(_pfn) ((_pfn) << (PAGE_SHIFT - 10))
100
101
102 /*
103 ** The M2P is made up of some number of 'chunks' of at least 2MB in size.
104 ** The below definitions and utility function(s) deal with mapping the M2P
105 ** regarldess of the underlying machine memory size or architecture.
106 */
107 #define M2P_SHIFT L2_PAGETABLE_SHIFT_PAE
108 #define M2P_CHUNK_SIZE (1 << M2P_SHIFT)
109 #define M2P_SIZE(_m) ROUNDUP(((_m) * sizeof(xen_pfn_t)), M2P_SHIFT)
110 #define M2P_CHUNKS(_m) (M2P_SIZE((_m)) >> M2P_SHIFT)
111
112 /* Returns TRUE if the PFN is currently mapped */
113 #define is_mapped(pfn_type) (!((pfn_type) & 0x80000000UL))
114
115
116 #define GET_FIELD(_p, _f, _w) (((_w) == 8) ? ((_p)->x64._f) : ((_p)->x32._f))
117
118 #define SET_FIELD(_p, _f, _v, _w) do { \
119 if ((_w) == 8) \
120 (_p)->x64._f = (_v); \
121 else \
122 (_p)->x32._f = (_v); \
123 } while (0)
124
125 #define UNFOLD_CR3(_c) \
126 ((uint64_t)((dinfo->guest_width == 8) \
127 ? ((_c) >> 12) \
128 : (((uint32_t)(_c) >> 12) | ((uint32_t)(_c) << 20))))
129
130 #define FOLD_CR3(_c) \
131 ((uint64_t)((dinfo->guest_width == 8) \
132 ? ((uint64_t)(_c)) << 12 \
133 : (((uint32_t)(_c) << 12) | ((uint32_t)(_c) >> 20))))
134
135 #define MEMCPY_FIELD(_d, _s, _f, _w) do { \
136 if ((_w) == 8) \
137 memcpy(&(_d)->x64._f, &(_s)->x64._f,sizeof((_d)->x64._f)); \
138 else \
139 memcpy(&(_d)->x32._f, &(_s)->x32._f,sizeof((_d)->x32._f)); \
140 } while (0)
141
142 #define MEMSET_ARRAY_FIELD(_p, _f, _v, _w) do { \
143 if ((_w) == 8) \
144 memset(&(_p)->x64._f[0], (_v), sizeof((_p)->x64._f)); \
145 else \
146 memset(&(_p)->x32._f[0], (_v), sizeof((_p)->x32._f)); \
147 } while (0)
148