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
/
checkpoint.c
95 lines (86 loc) · 2.26 KB
/
checkpoint.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
/*
* In The Name Of God
* ========================================
* [] File Name : checkpoint.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)];
}
static int checkpoint_mem(struct checkpoint_t *ch)
{
pte_t *pte;
uint pa, i, flags;
for (i = 0; i < ch->p.sz; i += PGSIZE){
if ((pte = walkpgdir(ch->p.pgdir, (void *) i, 0)) == 0)
panic("checkpoint_mem: pte should exist");
if (!(*pte & PTE_P))
panic("checkpoint_mem: page not present");
pa = PTE_ADDR(*pte);
flags = PTE_FLAGS(*pte);
memmove((ch->pages) + i, (char*)p2v(pa), PGSIZE);
(ch->flags)[i / PGSIZE] = flags;
}
return 0;
}
static int checkpoint_proc(struct checkpoint_t *ch)
{
if ((ch->p.pgdir = copyuvm(proc->pgdir, proc->sz)) == 0) {
return -1;
}
ch->p.sz = PGROUNDUP(proc->sz);
ch->tf = *proc->tf;
safestrcpy(ch->p.name, proc->name, sizeof(proc->name));
return 0;
}
int sys_checkpoint_proc(void)
{
char *p = 0;
if (argptr(0, &p, sizeof(struct checkpoint_t)) < 0)
return -1;
return checkpoint_proc((struct checkpoint_t *) p);
}
int sys_checkpoint_mem(void)
{
char *p = 0;
if (argptr(0, &p, sizeof(struct checkpoint_t)) < 0)
return -1;
return checkpoint_mem((void *) p);
}