1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
|
/******************************************************************************
* xc_linux_restore.c
*
* Restore the state of a Xenolinux session.
*
* Copyright (c) 2003, K A Fraser.
*/
#include "xc_private.h"
#include <asm-xeno/suspend.h>
#include <zlib.h>
/* This may allow us to create a 'quiet' command-line option, if necessary. */
#define verbose_printf(_f, _a...) \
do { \
if ( !verbose ) break; \
printf( _f , ## _a ); \
fflush(stdout); \
} while ( 0 )
static int get_pfn_list(int xc_handle,
int domain_id,
unsigned long *pfn_buf,
unsigned long max_pfns)
{
dom0_op_t op;
int ret;
op.cmd = DOM0_GETMEMLIST;
op.u.getmemlist.domain = domain_id;
op.u.getmemlist.max_pfns = max_pfns;
op.u.getmemlist.buffer = pfn_buf;
if ( mlock(pfn_buf, max_pfns * sizeof(unsigned long)) != 0 )
{
PERROR("Could not lock pfn list buffer");
return -1;
}
ret = do_dom0_op(xc_handle, &op);
(void)munlock(pfn_buf, max_pfns * sizeof(unsigned long));
return (ret < 0) ? -1 : op.u.getmemlist.num_pfns;
}
#define MAX_MMU_UPDATES 1024
static int flush_mmu_updates(int xc_handle,
mmu_update_t *mmu_updates,
int *mmu_update_idx)
{
int err = 0;
privcmd_hypercall_t hypercall;
if ( *mmu_update_idx == 0 )
return 0;
hypercall.op = __HYPERVISOR_mmu_update;
hypercall.arg[0] = (unsigned long)mmu_updates;
hypercall.arg[1] = (unsigned long)*mmu_update_idx;
if ( mlock(mmu_updates, sizeof(mmu_updates)) != 0 )
{
PERROR("Could not lock pagetable update array");
err = 1;
goto out;
}
if ( do_xen_hypercall(xc_handle, &hypercall) < 0 )
{
ERROR("Failure when submitting mmu updates");
err = 1;
}
*mmu_update_idx = 0;
(void)munlock(mmu_updates, sizeof(mmu_updates));
out:
return err;
}
static int add_mmu_update(int xc_handle,
mmu_update_t *mmu_updates,
int *mmu_update_idx,
unsigned long ptr,
unsigned long val)
{
mmu_updates[*mmu_update_idx].ptr = ptr;
mmu_updates[*mmu_update_idx].val = val;
if ( ++*mmu_update_idx == MAX_MMU_UPDATES )
return flush_mmu_updates(xc_handle, mmu_updates, mmu_update_idx);
return 0;
}
static int checked_read(gzFile fd, void *buf, size_t count)
{
int rc;
while ( ((rc = gzread(fd, buf, count)) == -1) && (errno == EINTR) )
continue;
return rc == count;
}
int xc_linux_restore(int xc_handle,
const char *state_file,
int verbose)
{
dom0_op_t op;
int rc = 1, i, j;
unsigned long mfn, pfn, dom = 0;
unsigned int prev_pc, this_pc;
/* Number of page frames in use by this XenoLinux session. */
unsigned long nr_pfns;
/* The new domain's shared-info frame number. */
unsigned long shared_info_frame;
unsigned char shared_info[PAGE_SIZE]; /* saved contents from file */
/* A copy of the CPU context of the guest. */
full_execution_context_t ctxt;
/* First 16 bytes of the state file must contain 'XenoLinuxSuspend'. */
char signature[16];
/* A copy of the domain's name. */
char name[MAX_DOMAIN_NAME];
/* A table containg the type of each PFN (/not/ MFN!). */
unsigned long *pfn_type = NULL;
/* A temporary mapping, and a copy, of one frame of guest memory. */
unsigned long *ppage, page[1024];
/* A copy of the pfn-to-mfn table frame list. */
unsigned long pfn_to_mfn_frame_list[1024];
/* A table mapping each PFN to its new MFN. */
unsigned long *pfn_to_mfn_table = NULL;
/* A temporary mapping of the guest's suspend record. */
suspend_record_t *p_srec;
/* The name and descriptor of the file that we are reading from. */
int fd;
gzFile gfd;
mmu_update_t mmu_updates[MAX_MMU_UPDATES];
int mmu_update_idx = 0;
int pm_handle = -1;
if ( (fd = open(state_file, O_RDONLY)) == -1 )
{
PERROR("Could not open state file for reading");
return 1;
}
if ( (gfd = gzdopen(fd, "rb")) == NULL )
{
ERROR("Could not allocate decompression state for state file");
close(fd);
return 1;
}
/* Start writing out the saved-domain record. */
if ( !checked_read(gfd, signature, 16) ||
(memcmp(signature, "XenoLinuxSuspend", 16) != 0) )
{
ERROR("Unrecognised state format -- no signature found");
goto out;
}
if ( !checked_read(gfd, name, sizeof(name)) ||
!checked_read(gfd, &nr_pfns, sizeof(unsigned long)) ||
!checked_read(gfd, &ctxt, sizeof(ctxt)) ||
!checked_read(gfd, shared_info, PAGE_SIZE) ||
!checked_read(gfd, pfn_to_mfn_frame_list, PAGE_SIZE) )
{
ERROR("Error when reading from state file");
goto out;
}
for ( i = 0; i < MAX_DOMAIN_NAME; i++ )
{
if ( name[i] == '\0' ) break;
if ( name[i] & 0x80 )
{
ERROR("Random characters in domain name");
goto out;
}
}
name[MAX_DOMAIN_NAME-1] = '\0';
if ( nr_pfns > 1024*1024 )
{
ERROR("Invalid state file -- pfn count out of range");
goto out;
}
/* We want zeroed memory so use calloc rather than malloc. */
pfn_to_mfn_table = calloc(1, 4 * nr_pfns);
pfn_type = calloc(1, 4 * nr_pfns);
if ( (pfn_to_mfn_table == NULL) || (pfn_type == NULL) )
{
errno = ENOMEM;
goto out;
}
if ( !checked_read(gfd, pfn_type, 4 * nr_pfns) )
{
ERROR("Error when reading from state file");
goto out;
}
/* Create a new domain of the appropriate size, and find it's dom_id. */
op.cmd = DOM0_CREATEDOMAIN;
op.u.createdomain.memory_kb = nr_pfns * (PAGE_SIZE / 1024);
memcpy(op.u.createdomain.name, name, MAX_DOMAIN_NAME);
if ( do_dom0_op(xc_handle, &op) < 0 )
{
ERROR("Could not create new domain");
goto out;
}
dom = op.u.createdomain.domain;
/* Get the domain's shared-info frame. */
op.cmd = DOM0_GETDOMAININFO;
op.u.getdomaininfo.domain = dom;
if ( do_dom0_op(xc_handle, &op) < 0 )
{
ERROR("Could not get information on new domain");
goto out;
}
shared_info_frame = op.u.getdomaininfo.shared_info_frame;
if ( (pm_handle = init_pfn_mapper()) < 0 )
goto out;
/* Copy saved contents of shared-info page. No checking needed. */
ppage = map_pfn(pm_handle, shared_info_frame);
memcpy(ppage, shared_info, PAGE_SIZE);
unmap_pfn(pm_handle, ppage);
/* Build the pfn-to-mfn table. We choose MFN ordering returned by Xen. */
if ( get_pfn_list(xc_handle, dom, pfn_to_mfn_table, nr_pfns) != nr_pfns )
{
ERROR("Did not read correct number of frame numbers for new dom");
goto out;
}
verbose_printf("Reloading memory pages: 0%%");
/*
* Now simply read each saved frame into its new machine frame.
* We uncanonicalise page tables as we go.
*/
prev_pc = 0;
for ( i = 0; i < nr_pfns; i++ )
{
this_pc = (i * 100) / nr_pfns;
if ( (this_pc - prev_pc) >= 5 )
{
verbose_printf("\b\b\b\b%3d%%", this_pc);
prev_pc = this_pc;
}
mfn = pfn_to_mfn_table[i];
if ( !checked_read(gfd, page, PAGE_SIZE) )
{
ERROR("Error when reading from state file");
goto out;
}
ppage = map_pfn(pm_handle, mfn);
switch ( pfn_type[i] )
{
case L1TAB:
memset(ppage, 0, PAGE_SIZE);
if ( add_mmu_update(xc_handle, mmu_updates, &mmu_update_idx,
(mfn<<PAGE_SHIFT) | MMU_EXTENDED_COMMAND,
MMUEXT_PIN_L1_TABLE) )
goto out;
for ( j = 0; j < 1024; j++ )
{
if ( page[j] & _PAGE_PRESENT )
{
if ( (pfn = page[j] >> PAGE_SHIFT) >= nr_pfns )
{
ERROR("Frame number in page table is out of range");
goto out;
}
if ( (pfn_type[pfn] != NONE) && (page[j] & _PAGE_RW) )
|