This repository has been archived by the owner on Jan 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
restore.c
110 lines (99 loc) · 2.39 KB
/
restore.c
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
/*
* In The Name Of God
* ========================================
* [] File Name : restore.c
*
* [] Creation Date : 13-01-2016
*
* [] Created By : Pooya Parsa ([email protected])
*
* [] Created By : Parham Alvani ([email protected])
* =======================================
*/
/*
* Copyright (c) 2016 Parham Alvani and Pooya Parsa.
*/
#include "types.h"
#include "defs.h"
#include "param.h"
#include "memlayout.h"
#include "mmu.h"
#include "x86.h"
#include "proc.h"
#include "spinlock.h"
#include "checkpoint.h"
// Return the address of the PTE in page table pgdir
// that corresponds to virtual address va. If alloc!=0,
// create any required page table pages.
static pte_t *walkpgdir(pde_t *pgdir, const void *va, int alloc)
{
pde_t *pde;
pte_t *pgtab;
pde = &pgdir[PDX(va)];
if(*pde & PTE_P){
pgtab = (pte_t*)p2v(PTE_ADDR(*pde));
} else {
if(!alloc || (pgtab = (pte_t*)kalloc()) == 0)
return 0;
// Make sure all those PTE_P bits are zero.
memset(pgtab, 0, PGSIZE);
// The permissions here are overly generous, but they can
// be further restricted by the permissions in the page table
// entries, if necessary.
*pde = v2p(pgtab) | PTE_P | PTE_W | PTE_U;
}
return &pgtab[PTX(va)];
}
// Create PTEs for virtual addresses starting at va that refer to
// physical addresses starting at pa. va and size might not
// be page-aligned.
static int mappages(pde_t *pgdir, void *va, uint size, uint pa, int perm)
{
char *a, *last;
pte_t *pte;
a = (char*)PGROUNDDOWN((uint)va);
last = (char*)PGROUNDDOWN(((uint)va) + size - 1);
for(;;){
if((pte = walkpgdir(pgdir, a, 1)) == 0)
return -1;
if(*pte & PTE_P)
panic("remap");
*pte = pa | perm | PTE_P;
if(a == last)
break;
a += PGSIZE;
pa += PGSIZE;
}
return 0;
}
static int restore(struct checkpoint_t *ch)
{
pde_t *d;
uint i;
char *mem;
if((d = setupkvm()) == 0)
return -1;
for(i = 0; i < ch->p.sz; i += PGSIZE){
if((mem = kalloc()) == 0)
goto bad;
memmove(mem, (ch->pages) + i, PGSIZE);
if(mappages(d, (char*)i, PGSIZE, v2p(mem), (ch->flags)[i / PGSIZE]) < 0)
goto bad;
}
struct proc np;
struct trapframe tf;
np.pgdir = d;
tf = ch->tf;
*np.tf = tf;
return procload(&np);
bad:
freevm(d);
return -1;
}
int sys_restore(void)
{
char *p = 0;
if (argptr(0, &p, sizeof(struct checkpoint_t)) < 0)
return -1;
return restore((struct checkpoint_t *)p);
}