-
Notifications
You must be signed in to change notification settings - Fork 3
/
paging.patch
225 lines (219 loc) · 4.88 KB
/
paging.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
diff --git a/defs.h b/defs.h
index 82fb982..f33f2cb 100644
--- a/defs.h
+++ b/defs.h
@@ -52,6 +52,11 @@ struct inode* nameiparent(char*, char*);
int readi(struct inode*, char*, uint, uint);
void stati(struct inode*, struct stat*);
int writei(struct inode*, char*, uint, uint);
+int createSwapFile(struct proc* p);
+int readFromSwapFile(struct proc * p, char* buffer, uint placeOnFile, uint size);
+int writeToSwapFile(struct proc* p, char* buffer, uint placeOnFile, uint size);
+int removeSwapFile(struct proc* p);
+
// ide.c
void ideinit(void);
@@ -124,6 +129,11 @@ void yield(void);
// swtch.S
void swtch(struct context**, struct context*);
+// sysfile
+struct inode* create(char *path, short type, short major, short minor);
+int isdirempty(struct inode *dp);
+
+
// spinlock.c
void acquire(struct spinlock*);
void getcallerpcs(void*, uint*);
diff --git a/fs.c b/fs.c
index feb59fe..f7c92cf 100644
--- a/fs.c
+++ b/fs.c
@@ -669,3 +669,144 @@ nameiparent(char *path, char *name)
{
return namex(path, 1, name);
}
+
+// NEW FOR PAGING
+
+#include "fcntl.h"
+#define DIGITS 14
+
+char* itoa(int i, char b[]){
+ char const digit[] = "0123456789";
+ char* p = b;
+ if(i<0){
+ *p++ = '-';
+ i *= -1;
+ }
+ int shifter = i;
+ do{ //Move to where representation ends
+ ++p;
+ shifter = shifter/10;
+ }while(shifter);
+ *p = '\0';
+ do{ //Move back, inserting digits as u go
+ *--p = digit[i%10];
+ i = i/10;
+ }while(i);
+ return b;
+}
+
+//remove swap file of proc p;
+int
+removeSwapFile(struct proc* p)
+{
+ //path of proccess
+ char path[DIGITS];
+ memmove(path,"/.swap", 6);
+ itoa(p->pid, path+ 6);
+
+ struct inode *ip, *dp;
+ struct dirent de;
+ char name[DIRSIZ];
+ uint off;
+
+ if(0 == p->swapFile)
+ {
+ return -1;
+ }
+ fileclose(p->swapFile);
+
+ begin_op();
+ if((dp = nameiparent(path, name)) == 0)
+ {
+ end_op();
+ return -1;
+ }
+
+ ilock(dp);
+
+ // Cannot unlink "." or "..".
+ if(namecmp(name, ".") == 0 || namecmp(name, "..") == 0)
+ goto bad;
+
+ if((ip = dirlookup(dp, name, &off)) == 0)
+ goto bad;
+ ilock(ip);
+
+ if(ip->nlink < 1)
+ panic("unlink: nlink < 1");
+ if(ip->type == T_DIR && !isdirempty(ip)){
+ iunlockput(ip);
+ goto bad;
+ }
+
+ memset(&de, 0, sizeof(de));
+ if(writei(dp, (char*)&de, off, sizeof(de)) != sizeof(de))
+ panic("unlink: writei");
+ if(ip->type == T_DIR){
+ dp->nlink--;
+ iupdate(dp);
+ }
+ iunlockput(dp);
+
+ ip->nlink--;
+ iupdate(ip);
+ iunlockput(ip);
+
+ end_op();
+
+ return 0;
+
+ bad:
+ iunlockput(dp);
+ end_op();
+ return -1;
+
+}
+
+
+//return 0 on success
+int
+createSwapFile(struct proc* p)
+{
+
+ char path[DIGITS];
+ memmove(path,"/.swap", 6);
+ itoa(p->pid, path+ 6);
+
+ begin_op();
+ struct inode * in = create(path, T_FILE, 0, 0);
+ iunlock(in);
+
+ p->swapFile = filealloc();
+ if (p->swapFile == 0)
+ panic("no slot for files on /store");
+
+ p->swapFile->ip = in;
+ p->swapFile->type = FD_INODE;
+ p->swapFile->off = 0;
+ p->swapFile->readable = O_WRONLY;
+ p->swapFile->writable = O_RDWR;
+ end_op();
+
+ return 0;
+}
+
+//return as sys_write (-1 when error)
+int
+writeToSwapFile(struct proc * p, char* buffer, uint placeOnFile, uint size)
+{
+ p->swapFile->off = placeOnFile;
+
+ return filewrite(p->swapFile, buffer, size);
+
+}
+
+//return as sys_read (-1 when error)
+int
+readFromSwapFile(struct proc * p, char* buffer, uint placeOnFile, uint size)
+{
+ p->swapFile->off = placeOnFile;
+
+ return fileread(p->swapFile, buffer, size);
+}
+
diff --git a/mmu.h b/mmu.h
index a82d8e2..aec4420 100644
--- a/mmu.h
+++ b/mmu.h
@@ -95,6 +95,8 @@ struct segdesc {
#define PTE_W 0x002 // Writeable
#define PTE_U 0x004 // User
#define PTE_PS 0x080 // Page Size
+#define PTE_A 0x020 // Accessed
+#define PTE_PG 0x200 // Paged out to secondary storage
// Address in page table or page directory entry
#define PTE_ADDR(pte) ((uint)(pte) & ~0xFFF)
diff --git a/proc.h b/proc.h
index 1647114..9f65fae 100644
--- a/proc.h
+++ b/proc.h
@@ -49,6 +49,8 @@ struct proc {
struct file *ofile[NOFILE]; // Open files
struct inode *cwd; // Current directory
char name[16]; // Process name (debugging)
+ //Swap file. must initiate with create swap file
+ struct file *swapFile; //page file
};
// Process memory is laid out contiguously, low addresses first:
diff --git a/sysfile.c b/sysfile.c
index 87e508b..22d3588 100644
--- a/sysfile.c
+++ b/sysfile.c
@@ -165,7 +165,7 @@ bad:
}
// Is the directory dp empty except for "." and ".." ?
-static int
+int
isdirempty(struct inode *dp)
{
int off;
@@ -238,7 +238,7 @@ bad:
return -1;
}
-static struct inode*
+struct inode*
create(char *path, short type, short major, short minor)
{
uint off;