-
Notifications
You must be signed in to change notification settings - Fork 0
/
lazy.patch
255 lines (245 loc) · 6.48 KB
/
lazy.patch
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
diff --git a/Makefile b/Makefile
index 09d790c..a1694cd 100644
--- a/Makefile
+++ b/Makefile
@@ -181,6 +181,8 @@ UPROGS=\
_usertests\
_wc\
_zombie\
+ _testprog\
+ _testprog2\
fs.img: mkfs README $(UPROGS)
./mkfs fs.img README $(UPROGS)
@@ -248,9 +250,10 @@ qemu-nox-gdb: fs.img xv6.img .gdbinit
# check in that version.
EXTRA=\
- mkfs.c ulib.c user.h cat.c echo.c forktest.c grep.c kill.c\
+ mkfs.c ulib.c user.h cat.c echo.c forktest.c grep.c kill.c testprog.c\
ln.c ls.c mkdir.c rm.c stressfs.c usertests.c wc.c zombie.c\
printf.c umalloc.c\
+ testprog.c\
README dot-bochsrc *.pl toc.* runoff runoff1 runoff.list\
.gdbinit.tmpl gdbutil\
diff --git a/defs.h b/defs.h
index 82fb982..9b850a3 100644
--- a/defs.h
+++ b/defs.h
@@ -172,6 +172,7 @@ void uartputc(int);
// vm.c
void seginit(void);
+int mappages(pde_t*,void*,uint,uint,int);
void kvmalloc(void);
pde_t* setupkvm(void);
char* uva2ka(pde_t*, char*);
diff --git a/kernel.ld b/kernel.ld
index 4e12e14..e24c860 100644
--- a/kernel.ld
+++ b/kernel.ld
@@ -26,12 +26,16 @@ SECTIONS
PROVIDE(__STAB_BEGIN__ = .);
*(.stab);
PROVIDE(__STAB_END__ = .);
+ BYTE(0) /* Force the linker to allocate space
+ for this section */
}
.stabstr : {
PROVIDE(__STABSTR_BEGIN__ = .);
*(.stabstr);
PROVIDE(__STABSTR_END__ = .);
+ BYTE(0) /* Force the linker to allocate space
+ for this section */
}
/* Adjust the address for the data segment to the next page */
diff --git a/syscall.c b/syscall.c
index ee85261..13db545 100644
--- a/syscall.c
+++ b/syscall.c
@@ -103,6 +103,8 @@ extern int sys_unlink(void);
extern int sys_wait(void);
extern int sys_write(void);
extern int sys_uptime(void);
+extern int sys_count_virtual_pages(void);
+extern int sys_count_physical_pages(void);
static int (*syscalls[])(void) = {
[SYS_fork] sys_fork,
@@ -126,6 +128,8 @@ static int (*syscalls[])(void) = {
[SYS_link] sys_link,
[SYS_mkdir] sys_mkdir,
[SYS_close] sys_close,
+[SYS_count_virtual_pages] sys_count_virtual_pages,
+[SYS_count_physical_pages] sys_count_physical_pages,
};
void
@@ -143,3 +147,4 @@ syscall(void)
curproc->tf->eax = -1;
}
}
+
diff --git a/syscall.h b/syscall.h
index bc5f356..5bee4bc 100644
--- a/syscall.h
+++ b/syscall.h
@@ -20,3 +20,5 @@
#define SYS_link 19
#define SYS_mkdir 20
#define SYS_close 21
+#define SYS_count_virtual_pages 22
+#define SYS_count_physical_pages 23
diff --git a/sysproc.c b/sysproc.c
index 0686d29..82fa341 100644
--- a/sysproc.c
+++ b/sysproc.c
@@ -47,12 +47,15 @@ sys_sbrk(void)
{
int addr;
int n;
-
+ //Have to increase proc size by n bytes
if(argint(0, &n) < 0)
return -1;
addr = myproc()->sz;
- if(growproc(n) < 0)
- return -1;
+ myproc()->sz = addr + n;
+ //cprintf("Proc size was %d and now is %d\n",addr-n,myproc()->sz);
+ //if n<0 then simply deallocate
+ //if(growproc(n) < 0)
+ // return -1;
return addr;
}
@@ -89,3 +92,34 @@ sys_uptime(void)
release(&tickslock);
return xticks;
}
+
+int
+sys_count_virtual_pages(void)
+{
+ uint vsize;
+ vsize = myproc()->sz;
+ vsize = vsize/PGSIZE;
+ return vsize;
+}
+
+int sys_count_physical_pages(void)
+{
+ struct proc *curproc = myproc();
+ pde_t *pgdir = curproc->pgdir;
+ pde_t *pde;
+ pte_t *pgtab;
+ int i;int j;int cnt;cnt=0;
+ for(i=0;i<NPDENTRIES;++i){
+ pde = &pgdir[i];
+ if(*pde & PTE_P){
+ //cprintf("Directory %d is present :contents are = %p\n ",i,*pde);
+ pgtab = (pte_t*)P2V(PTE_ADDR(*pde));
+ for(j=0;j<NPTENTRIES;++j){
+ if(pgtab[j] & PTE_P){
+ cnt++;
+ }
+ }
+ }
+ }
+ return cnt;
+}
diff --git a/trap.c b/trap.c
index 41c66eb..b4a2f08 100644
--- a/trap.c
+++ b/trap.c
@@ -36,6 +36,8 @@ idtinit(void)
void
trap(struct trapframe *tf)
{
+ int va;
+ char* mem;
if(tf->trapno == T_SYSCALL){
if(myproc()->killed)
exit();
@@ -77,7 +79,40 @@ trap(struct trapframe *tf)
cpuid(), tf->cs, tf->eip);
lapiceoi();
break;
-
+
+ case T_PGFLT:
+ //PAGEFAULT: 14 - allocate a new page
+ va = rcr2();
+ struct proc *curproc = myproc();
+ va = PGROUNDDOWN(va);
+ if(va <= curproc->sz){
+ //allocate page corresponding to va
+ //cprintf("Page not allocated at virtual address %d\n",va);
+ mem = kalloc();
+ if(mem == 0){
+ cprintf("allocuvm out of memory\n");
+ //deallocuvm(pgdir, newsz, oldsz);
+ //return 0;
+ myproc()->killed = 1;
+ }else{
+ memset(mem, 0, PGSIZE);
+ if(mappages(curproc->pgdir, (char*)va, PGSIZE, V2P(mem), PTE_W|PTE_U) < 0){
+ cprintf("allocuvm out of memory (2)\n");
+ //deallocuvm(pgdir, newsz, oldsz);
+ //kfree(mem);
+ myproc()->killed = 1;
+ }
+ }
+ }else{
+ //cprintf("Process size is %d\n",myproc()->sz);
+ //cprintf("Page fault at virtual address %d\n",va);
+ cprintf("pid %d %s: trap %d err %d on cpu %d "
+ "eip 0x%x addr 0x%x--kill proc\n",
+ myproc()->pid, myproc()->name, tf->trapno,
+ tf->err, cpuid(), tf->eip, rcr2());
+ myproc()->killed = 1;
+ }
+ break;
//PAGEBREAK: 13
default:
if(myproc() == 0 || (tf->cs&3) == 0){
diff --git a/user.h b/user.h
index 4f99c52..ccc3c34 100644
--- a/user.h
+++ b/user.h
@@ -23,6 +23,8 @@ int getpid(void);
char* sbrk(int);
int sleep(int);
int uptime(void);
+int count_virtual_pages(void);
+int count_physical_pages(void);
// ulib.c
int stat(const char*, struct stat*);
diff --git a/usys.S b/usys.S
index 8bfd8a1..24c9b81 100644
--- a/usys.S
+++ b/usys.S
@@ -29,3 +29,5 @@ SYSCALL(getpid)
SYSCALL(sbrk)
SYSCALL(sleep)
SYSCALL(uptime)
+SYSCALL(count_virtual_pages)
+SYSCALL(count_physical_pages)
diff --git a/vm.c b/vm.c
index 7134cff..b6c31e0 100644
--- a/vm.c
+++ b/vm.c
@@ -57,7 +57,7 @@ walkpgdir(pde_t *pgdir, const void *va, int alloc)
// 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
+int
mappages(pde_t *pgdir, void *va, uint size, uint pa, int perm)
{
char *a, *last;
@@ -325,8 +325,8 @@ copyuvm(pde_t *pgdir, uint sz)
for(i = 0; i < sz; i += PGSIZE){
if((pte = walkpgdir(pgdir, (void *) i, 0)) == 0)
panic("copyuvm: pte should exist");
- if(!(*pte & PTE_P))
- panic("copyuvm: page not present");
+ //if(!(*pte & PTE_P))
+ // panic("copyuvm: page not present");
pa = PTE_ADDR(*pte);
flags = PTE_FLAGS(*pte);
if((mem = kalloc()) == 0)