1 #include <assert.h>
2 #include <arpa/inet.h>
3 
4 #include "xc_sr_common_x86.h"
5 
6 /*
7  * Process an HVM_CONTEXT record from the stream.
8  */
handle_hvm_context(struct xc_sr_context * ctx,struct xc_sr_record * rec)9 static int handle_hvm_context(struct xc_sr_context *ctx,
10                               struct xc_sr_record *rec)
11 {
12     xc_interface *xch = ctx->xch;
13     int rc = update_blob(&ctx->x86.hvm.restore.context, rec->data, rec->length);
14 
15     if ( rc )
16         ERROR("Unable to allocate %u bytes for hvm context", rec->length);
17 
18     return rc;
19 }
20 
21 /*
22  * Process an HVM_PARAMS record from the stream.
23  */
handle_hvm_params(struct xc_sr_context * ctx,struct xc_sr_record * rec)24 static int handle_hvm_params(struct xc_sr_context *ctx,
25                              struct xc_sr_record *rec)
26 {
27     xc_interface *xch = ctx->xch;
28     struct xc_sr_rec_hvm_params *hdr = rec->data;
29     struct xc_sr_rec_hvm_params_entry *entry = hdr->param;
30     unsigned int i;
31     int rc;
32 
33     if ( rec->length < sizeof(*hdr) )
34     {
35         ERROR("HVM_PARAMS record truncated: length %u, header size %zu",
36               rec->length, sizeof(*hdr));
37         return -1;
38     }
39 
40     if ( rec->length != (sizeof(*hdr) + hdr->count * sizeof(*entry)) )
41     {
42         ERROR("HVM_PARAMS record truncated: header %zu, count %u, "
43               "expected len %zu, got %u",
44               sizeof(*hdr), hdr->count, hdr->count * sizeof(*entry),
45               rec->length);
46         return -1;
47     }
48 
49     /*
50      * Tolerate empty records.  Older sending sides used to accidentally
51      * generate them.
52      */
53     if ( hdr->count == 0 )
54     {
55         DBGPRINTF("Skipping empty HVM_PARAMS record\n");
56         return 0;
57     }
58 
59     for ( i = 0; i < hdr->count; i++, entry++ )
60     {
61         switch ( entry->index )
62         {
63         case HVM_PARAM_CONSOLE_PFN:
64             ctx->restore.console_gfn = entry->value;
65             xc_clear_domain_page(xch, ctx->domid, entry->value);
66             break;
67         case HVM_PARAM_STORE_PFN:
68             ctx->restore.xenstore_gfn = entry->value;
69             xc_clear_domain_page(xch, ctx->domid, entry->value);
70             break;
71         case HVM_PARAM_IOREQ_PFN:
72         case HVM_PARAM_BUFIOREQ_PFN:
73             xc_clear_domain_page(xch, ctx->domid, entry->value);
74             break;
75 
76         case HVM_PARAM_PAE_ENABLED:
77             /*
78              * This HVM_PARAM only ever existed to pass data into
79              * xc_cpuid_apply_policy().  The function has now been updated to
80              * use a normal calling convention, making the param obsolete.
81              *
82              * Discard if we find it in an old migration stream.
83              */
84             continue;
85         }
86 
87         rc = xc_hvm_param_set(xch, ctx->domid, entry->index, entry->value);
88         if ( rc < 0 )
89         {
90             PERROR("set HVM param %"PRId64" = 0x%016"PRIx64,
91                    entry->index, entry->value);
92             return rc;
93         }
94     }
95     return 0;
96 }
97 
98 /* restore_ops function. */
x86_hvm_pfn_is_valid(const struct xc_sr_context * ctx,xen_pfn_t pfn)99 static bool x86_hvm_pfn_is_valid(const struct xc_sr_context *ctx, xen_pfn_t pfn)
100 {
101     return true;
102 }
103 
104 /* restore_ops function. */
x86_hvm_pfn_to_gfn(const struct xc_sr_context * ctx,xen_pfn_t pfn)105 static xen_pfn_t x86_hvm_pfn_to_gfn(const struct xc_sr_context *ctx,
106                                     xen_pfn_t pfn)
107 {
108     return pfn;
109 }
110 
111 /* restore_ops function. */
x86_hvm_set_gfn(struct xc_sr_context * ctx,xen_pfn_t pfn,xen_pfn_t gfn)112 static void x86_hvm_set_gfn(struct xc_sr_context *ctx, xen_pfn_t pfn,
113                             xen_pfn_t gfn)
114 {
115     /* no op */
116 }
117 
118 /* restore_ops function. */
x86_hvm_set_page_type(struct xc_sr_context * ctx,xen_pfn_t pfn,xen_pfn_t type)119 static void x86_hvm_set_page_type(struct xc_sr_context *ctx,
120                                   xen_pfn_t pfn, xen_pfn_t type)
121 {
122     /* no-op */
123 }
124 
125 /* restore_ops function. */
x86_hvm_localise_page(struct xc_sr_context * ctx,uint32_t type,void * page)126 static int x86_hvm_localise_page(struct xc_sr_context *ctx,
127                                  uint32_t type, void *page)
128 {
129     /* no-op */
130     return 0;
131 }
132 
133 /*
134  * restore_ops function. Confirms the stream matches the domain.
135  */
x86_hvm_setup(struct xc_sr_context * ctx)136 static int x86_hvm_setup(struct xc_sr_context *ctx)
137 {
138     xc_interface *xch = ctx->xch;
139 
140     if ( ctx->restore.guest_type != DHDR_TYPE_X86_HVM )
141     {
142         ERROR("Unable to restore %s domain into an x86 HVM domain",
143               dhdr_type_to_str(ctx->restore.guest_type));
144         return -1;
145     }
146 
147     if ( ctx->restore.guest_page_size != PAGE_SIZE )
148     {
149         ERROR("Invalid page size %u for x86 HVM domains",
150               ctx->restore.guest_page_size);
151         return -1;
152     }
153 
154 #ifdef __i386__
155     /* Very large domains (> 1TB) will exhaust virtual address space. */
156     if ( ctx->restore.p2m_size > 0x0fffffff )
157     {
158         errno = E2BIG;
159         PERROR("Cannot restore this big a guest");
160         return -1;
161     }
162 #endif
163 
164     return 0;
165 }
166 
167 /*
168  * restore_ops function.
169  */
x86_hvm_process_record(struct xc_sr_context * ctx,struct xc_sr_record * rec)170 static int x86_hvm_process_record(struct xc_sr_context *ctx,
171                                   struct xc_sr_record *rec)
172 {
173     switch ( rec->type )
174     {
175     case REC_TYPE_X86_TSC_INFO:
176         return handle_x86_tsc_info(ctx, rec);
177 
178     case REC_TYPE_HVM_CONTEXT:
179         return handle_hvm_context(ctx, rec);
180 
181     case REC_TYPE_HVM_PARAMS:
182         return handle_hvm_params(ctx, rec);
183 
184     case REC_TYPE_X86_CPUID_POLICY:
185         return handle_x86_cpuid_policy(ctx, rec);
186 
187     case REC_TYPE_X86_MSR_POLICY:
188         return handle_x86_msr_policy(ctx, rec);
189 
190     default:
191         return RECORD_NOT_PROCESSED;
192     }
193 }
194 
195 /*
196  * restore_ops function.  Sets extra hvm parameters and seeds the grant table.
197  */
x86_hvm_stream_complete(struct xc_sr_context * ctx)198 static int x86_hvm_stream_complete(struct xc_sr_context *ctx)
199 {
200     xc_interface *xch = ctx->xch;
201     int rc;
202 
203     rc = xc_hvm_param_set(xch, ctx->domid, HVM_PARAM_STORE_EVTCHN,
204                           ctx->restore.xenstore_evtchn);
205     if ( rc )
206     {
207         PERROR("Failed to set HVM_PARAM_STORE_EVTCHN");
208         return rc;
209     }
210 
211     rc = xc_hvm_param_set(xch, ctx->domid, HVM_PARAM_CONSOLE_EVTCHN,
212                           ctx->restore.console_evtchn);
213     if ( rc )
214     {
215         PERROR("Failed to set HVM_PARAM_CONSOLE_EVTCHN");
216         return rc;
217     }
218 
219     rc = xc_domain_hvm_setcontext(xch, ctx->domid,
220                                   ctx->x86.hvm.restore.context.ptr,
221                                   ctx->x86.hvm.restore.context.size);
222     if ( rc < 0 )
223     {
224         PERROR("Unable to restore HVM context");
225         return rc;
226     }
227 
228     rc = xc_dom_gnttab_seed(xch, ctx->domid, true,
229                             ctx->restore.console_gfn,
230                             ctx->restore.xenstore_gfn,
231                             ctx->restore.console_domid,
232                             ctx->restore.xenstore_domid);
233     if ( rc )
234     {
235         PERROR("Failed to seed grant table");
236         return rc;
237     }
238 
239     return rc;
240 }
241 
x86_hvm_cleanup(struct xc_sr_context * ctx)242 static int x86_hvm_cleanup(struct xc_sr_context *ctx)
243 {
244     free(ctx->x86.hvm.restore.context.ptr);
245 
246     free(ctx->x86.restore.cpuid.ptr);
247     free(ctx->x86.restore.msr.ptr);
248 
249     return 0;
250 }
251 
252 struct xc_sr_restore_ops restore_ops_x86_hvm =
253 {
254     .pfn_is_valid    = x86_hvm_pfn_is_valid,
255     .pfn_to_gfn      = x86_hvm_pfn_to_gfn,
256     .set_gfn         = x86_hvm_set_gfn,
257     .set_page_type   = x86_hvm_set_page_type,
258     .localise_page   = x86_hvm_localise_page,
259     .setup           = x86_hvm_setup,
260     .process_record  = x86_hvm_process_record,
261     .static_data_complete = x86_static_data_complete,
262     .stream_complete = x86_hvm_stream_complete,
263     .cleanup         = x86_hvm_cleanup,
264 };
265 
266 /*
267  * Local variables:
268  * mode: C
269  * c-file-style: "BSD"
270  * c-basic-offset: 4
271  * tab-width: 4
272  * indent-tabs-mode: nil
273  * End:
274  */
275