forked from hengyin/cs153_lab3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiff.txt
183 lines (171 loc) · 5.22 KB
/
diff.txt
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
diff --git a/Makefile b/Makefile
index 2d2b9e4..d47164b 100644
--- a/Makefile
+++ b/Makefile
@@ -29,6 +29,7 @@ OBJS = \
vm.o\
shm.o\
+
# Cross-compiling (e.g., on Mac OS X)
# TOOLPREFIX = i386-jos-elf
@@ -177,6 +178,8 @@ UPROGS=\
_zombie\
_shm_cnt\
_null\
+ _lab3_test1\
+ _lab3_test2\
fs.img: mkfs README $(UPROGS)
./mkfs fs.img README $(UPROGS)
diff --git a/exec.c b/exec.c
index b40134f..edf97f3 100644
--- a/exec.c
+++ b/exec.c
@@ -63,10 +63,12 @@ exec(char *path, char **argv)
// Allocate two pages at the next page boundary.
// Make the first inaccessible. Use the second as the user stack.
sz = PGROUNDUP(sz);
- if((sz = allocuvm(pgdir, sz, sz + 2*PGSIZE)) == 0)
+ sp = KERNBASE-1; // put sp at 0x8
+ if((allocuvm(pgdir, sp - PGSIZE, sp )) == 0) // userstack + page guard
goto bad;
- clearpteu(pgdir, (char*)(sz - 2*PGSIZE));
- sp = sz;
+ clearpteu(pgdir, (char*)(KERNBASE - 2*PGSIZE)); // create page guard starting from 2 page sizes below kernbase
+ sp = KERNBASE-4;
+ //curproc->topOfStack = sp - PGSIZE; // where user stack starts
// Push argument strings, prepare rest of stack in ustack.
for(argc = 0; argv[argc]; argc++) {
@@ -99,6 +101,7 @@ exec(char *path, char **argv)
curproc->sz = sz;
curproc->tf->eip = elf.entry; // main
curproc->tf->esp = sp;
+ curproc->stackNum = 1; // initialize stacknum
switchuvm(curproc);
freevm(oldpgdir);
return 0;
diff --git a/proc.h b/proc.h
index 1647114..c89bd38 100644
--- a/proc.h
+++ b/proc.h
@@ -49,6 +49,7 @@ struct proc {
struct file *ofile[NOFILE]; // Open files
struct inode *cwd; // Current directory
char name[16]; // Process name (debugging)
+ uint stackNum;
};
// Process memory is laid out contiguously, low addresses first:
diff --git a/syscall.c b/syscall.c
index 94ef3c9..c888335 100644
--- a/syscall.c
+++ b/syscall.c
@@ -17,9 +17,9 @@
int
fetchint(uint addr, int *ip)
{
- struct proc *curproc = myproc();
+// struct proc *curproc = myproc();
- if(addr >= curproc->sz || addr+4 > curproc->sz)
+ if(addr >= KERNBASE-1 || addr+4 > KERNBASE-1) // these changes to access whole changes we made
return -1;
*ip = *(int*)(addr);
return 0;
@@ -32,12 +32,12 @@ int
fetchstr(uint addr, char **pp)
{
char *s, *ep;
- struct proc *curproc = myproc();
+// struct proc *curproc = myproc();
- if(addr >= curproc->sz)
+ if(addr >= KERNBASE-1)
return -1;
*pp = (char*)addr;
- ep = (char*)curproc->sz;
+ ep = (char*)KERNBASE-1;
for(s = *pp; s < ep; s++){
if(*s == 0)
return s - *pp;
@@ -59,11 +59,11 @@ int
argptr(int n, char **pp, int size)
{
int i;
- struct proc *curproc = myproc();
+// struct proc *curproc = myproc();
if(argint(n, &i) < 0)
return -1;
- if(size < 0 || (uint)i >= curproc->sz || (uint)i+size > curproc->sz)
+ if(size < 0 || (uint)i >= KERNBASE-1 || (uint)i+size > KERNBASE-1)
return -1;
*pp = (char*)i;
return 0;
diff --git a/trap.c b/trap.c
index 41c66eb..2474cdf 100644
--- a/trap.c
+++ b/trap.c
@@ -78,6 +78,25 @@ trap(struct trapframe *tf)
lapiceoi();
break;
+ case T_PGFLT:
+ if (KERNBASE > rcr2()) { //rcr2 address causing pagefault, checking if from userspace
+ if (tf->esp >= rcr2()) {
+ uint topOfStack = KERNBASE - myproc()->stackNum * PGSIZE; // right above page guard, where to start allocating
+ if (allocuvm(myproc()->pgdir, PGROUNDDOWN(topOfStack - 1), topOfStack - 1) == 0) { // check if we can allocate
+ cprintf("No new page can be allocated. \n");
+ exit();
+ }
+ myproc()->stackNum++;
+ cprintf("%d pages currently allocated. \n", myproc()->stackNum);
+ }
+ }
+ else {
+ cprintf("Address is out of bounds. \n");
+ exit();
+ }
+ break;
+
+
//PAGEBREAK: 13
default:
if(myproc() == 0 || (tf->cs&3) == 0){
diff --git a/vm.c b/vm.c
index f623aa3..05c7b1a 100644
--- a/vm.c
+++ b/vm.c
@@ -224,7 +224,7 @@ allocuvm(pde_t *pgdir, uint oldsz, uint newsz)
char *mem;
uint a;
- if(newsz >= KERNBASE)
+ if(newsz > KERNBASE)
return 0;
if(newsz < oldsz)
return oldsz;
@@ -320,9 +320,11 @@ copyuvm(pde_t *pgdir, uint sz)
uint pa, i, flags;
char *mem;
+ struct proc *curproc = myproc();
+
if((d = setupkvm()) == 0)
return 0;
- for(i = 0; i < sz; i += PGSIZE){
+ for(i = 0; i < curproc->sz ; i += PGSIZE){
if((pte = walkpgdir(pgdir, (void *) i, 0)) == 0)
panic("copyuvm: pte should exist");
if(!(*pte & PTE_P))
@@ -335,6 +337,19 @@ copyuvm(pde_t *pgdir, uint sz)
if(mappages(d, (void*)i, PGSIZE, V2P(mem), flags) < 0)
goto bad;
}
+ for(i = PGROUNDUP(KERNBASE - 1 - myproc()->stackNum * PGSIZE); i < KERNBASE ; i += PGSIZE){ // basically go through userstack
+ if((pte = walkpgdir(pgdir, (void *) i, 0)) == 0)
+ panic("copyuvm: pte should exist");
+ if(!(*pte & PTE_P))
+ panic("copyuvm: page not present");
+ pa = PTE_ADDR(*pte);
+ flags = PTE_FLAGS(*pte);
+ if((mem = kalloc()) == 0)
+ goto bad;
+ memmove(mem, (char*)P2V(pa), PGSIZE);
+ if(mappages(d, (void*)i, PGSIZE, V2P(mem), flags) < 0)
+ goto bad;
+ }
return d;
bad: