-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathload.c
340 lines (303 loc) · 11.7 KB
/
load.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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#include "kernel.h"
// Created by Wenzhe Jiang & JUN LIUon 3/21/15.
// Copyright (c) 2015 Wenzhe Jiang & JUN LIU. All rights reserved.
/*
* Load a program into the current process's address space. The
* program comes from the Unix file identified by "name", and its
* arguments come from the array at "args", which is in standard
* argv format.
*
* Returns:
* 0 on success
* -1 on any error for which the current process is still runnable
* -2 on any error for which the current process is no longer runnable
*
* This function, after a series of initial checks, deletes the
* contents of Region 0, thus making the current process no longer
* runnable. Before this point, it is possible to return ERROR
* to an Exec() call that has called LoadProgram, and this function
* returns -1 for errors up to this point. After this point, the
* contents of Region 0 no longer exist, so the calling user process
* is no longer runnable, and this function returns -2 for errors
* in this case.
*/
int LoadProgram(char *name, char **args, ExceptionStackFrame *frame)
{
int fd;
int status;
struct loadinfo li;
char *cp;
char *cp2;
char **cpp;
char *argbuf;
int i;
unsigned long argcount;
int size;
int text_npg;
int data_bss_npg;
int stack_npg;
int usedpages;
unsigned long user_stack_limit=(USER_STACK_LIMIT>>PAGESHIFT)-1;
TracePrintf(9, "LoadProgram '%s', args %p\n", name, args);
if ((fd = open(name, O_RDONLY)) < 0) {
TracePrintf(9, "LoadProgram: can't open file '%s'\n", name);
return (-1);
}
status = LoadInfo(fd, &li);
TracePrintf(9, "LoadProgram: LoadInfo status %d\n", status);
switch (status) {
case LI_SUCCESS:
break;
case LI_FORMAT_ERROR:
TracePrintf(9, "LoadProgram: '%s' not in Yalnix format\n", name);
close(fd);
return (-1);
case LI_OTHER_ERROR:
TracePrintf(9, "LoadProgram: '%s' other error\n", name);
close(fd);
return (-1);
default:
TracePrintf(9, "LoadProgram: '%s' unknown error\n", name);
close(fd);
return (-1);
}
TracePrintf(9, "text_size 0x%lx, data_size 0x%lx, bss_size 0x%lx\n",
li.text_size, li.data_size, li.bss_size);
TracePrintf(9, "entry 0x%lx\n", li.entry);
/*
* Figure out how many bytes are needed to hold the arguments on
* the new stack that we are building. Also count the number of
* arguments, to become the argc that the new "main" gets called with.
*/
size = 0;
for (i = 0; args[i] != NULL; i++) {
size += strlen(args[i]) + 1;
}
argcount = i;
TracePrintf(9, "LoadProgram: size %d, argcount %d\n", size, argcount);
/*
* Now save the arguments in a separate buffer in Region 1, since
* we are about to delete all of Region 0.
*/
cp = argbuf = (char *)malloc(size);
for (i = 0; args[i] != NULL; i++) {
strcpy(cp, args[i]);
cp += strlen(cp) + 1;
}
/*
* The arguments will get copied starting at "cp" as set below,
* and the argv pointers to the arguments (and the argc value)
* will get built starting at "cpp" as set below. The value for
* "cpp" is computed by subtracting off space for the number of
* arguments plus 4 (for the argc value, a 0 (AT_NULL) to
* terminate the auxiliary vector, a NULL pointer terminating
* the argv pointers, and a NULL pointer terminating the envp
* pointers) times the size of each (sizeof(void *)). The
* value must also be aligned down to a multiple of 8 boundary.
*/
cp = ((char *)USER_STACK_LIMIT) - size;
cpp = (char **)((unsigned long)cp & (-1 << 4)); /* align cpp */
cpp = (char **)((unsigned long)cpp - ((argcount + 4) * sizeof(void *)));
text_npg = li.text_size >> PAGESHIFT;
data_bss_npg = UP_TO_PAGE(li.data_size + li.bss_size) >> PAGESHIFT;
stack_npg = (USER_STACK_LIMIT - DOWN_TO_PAGE(cpp)) >> PAGESHIFT;
/*
* Make sure we will leave at least one page between heap and stack
*/
if (MEM_INVALID_PAGES + text_npg + data_bss_npg + stack_npg + 1 + KERNEL_STACK_PAGES >= PAGE_TABLE_LEN) {
TracePrintf(9, "LoadProgram: program '%s' size too large for VM\n", name);
free(argbuf);
close(fd);
return (-1);
}
/*
* And make sure there will be enough physical memory to
* load the new program.
*/
/**
*>>>> The new program will require text_npg pages of text,
*>>>> data_bss_npg pages of data/bss, and stack_npg pages of
*>>>> stack. In checking that there is enough free physical
*>>>> memory for this, be sure to allow for the physical memory
*>>>> pages already allocated to this process that will be
*>>>> freed below before we allocate the needed pages for
*>>>> the new program being loaded.
*/
usedpages = 0;
for (i = 0; i < PAGE_TABLE_LEN - KERNEL_STACK_PAGES; i++) {
if (currentProc->pt_r0[i].valid == 1) {
currentProc->pt_r0[i].uprot = PROT_READ | PROT_WRITE;
currentProc->pt_r0[i].kprot = PROT_READ | PROT_WRITE;
usedpages++;
}
}
if (text_npg + data_bss_npg + stack_npg > free_frame_cnt + usedpages) {
TracePrintf(9, "LoadProgram: program '%s' size too large for physical memory\n", name);
free(argbuf);
close(fd);
return (-1);
}
/**
*>>>> Initialize sp for the current process to (char *)cpp.
*>>>> The value of cpp was initialized above.
*/
frame->sp = (char*)cpp;
/*
* Free all the old physical memory belonging to this process,
* but be sure to leave the kernel stack for this process (which
* is also in Region 0) alone.
*/
/**
*>>>> Loop over all PTEs for the current process's Region 0,
*>>>> except for those corresponding to the kernel stack (between
*>>>> address KERNEL_STACK_BASE and KERNEL_STACK_LIMIT). For
*>>>> any of these PTEs that are valid, free the physical memory
*>>>> memory page indicated by that PTE's pfn field. Set all
*>>>> of these PTEs to be no longer valid.
*/
WriteRegister(REG_TLB_FLUSH, TLB_FLUSH_0);
for (i=0;i<PAGE_TABLE_LEN-KERNEL_STACK_PAGES;i++) {
if (currentProc->pt_r0[i].valid) {
removeUsedPage(&(currentProc->pt_r0[i]));//XXX
currentProc->pt_r0[i].valid = 0;
WriteRegister(REG_TLB_FLUSH, (unsigned long)((currentProc->pt_r0)+i));
}
}
/*
* Fill in the page table with the right number of text,
* data+bss, and stack pages. We set all the text pages
* here to be read/write, just like the data+bss and
* stack pages, so that we can read the text into them
* from the file. We then change them read/execute.
*/
/**
*>>>> Leave the first MEM_INVALID_PAGES number of PTEs in the
*>>>> Region 0 page table unused (and thus invalid)
*/
for (i =0;i<MEM_INVALID_PAGES;i++) {
currentProc->pt_r0[i].valid = 0;
}
/* First, the text pages */
/**
*>>>> For the next text_npg number of PTEs in the Region 0
*>>>> page table, initialize each PTE:
*>>>> valid = 1
*>>>> kprot = PROT_READ | PROT_WRITE
*>>>> uprot = PROT_READ | PROT_EXEC
*>>>> pfn = a new page of physical memory
*/
for (;i<MEM_INVALID_PAGES+text_npg;i++) {
currentProc->pt_r0[i].uprot = PROT_READ | PROT_EXEC;
currentProc->pt_r0[i].kprot = PROT_READ | PROT_WRITE;
currentProc->pt_r0[i].valid = 1;
currentProc->pt_r0[i].pfn = getFreePage();//XXX
}
/* Then the data and bss pages */
/**
*>>>> For the next data_bss_npg number of PTEs in the Region 0
*>>>> page table, initialize each PTE:
*>>>> valid = 1
*>>>> kprot = PROT_READ | PROT_WRITE
*>>>> uprot = PROT_READ | PROT_WRITE
*>>>> pfn = a new page of physical memory
*/
for (; i<MEM_INVALID_PAGES+text_npg+data_bss_npg;i++) {
currentProc->pt_r0[i].uprot = PROT_READ | PROT_WRITE;
currentProc->pt_r0[i].kprot = PROT_READ | PROT_WRITE;
currentProc->pt_r0[i].valid = 1;
currentProc->pt_r0[i].pfn = getFreePage();//XXX
}
/* And finally the user stack pages */
/**
*>>>> For stack_npg number of PTEs in the Region 0 page table
*>>>> corresponding to the user stack (the last page of the
*>>>> user stack *ends* at virtual address USER_STACK_LIMIT),
*>>>> initialize each PTE:
*>>>> valid = 1
*>>>> kprot = PROT_READ | PROT_WRITE
*>>>> uprot = PROT_READ | PROT_WRITE
*>>>> pfn = a new page of physical memory
*/
/* Note that the stack is growing down, so the page is off by one */
for (i=0;i<stack_npg;i++) {
currentProc->pt_r0[user_stack_limit-i].uprot = PROT_READ | PROT_WRITE;
currentProc->pt_r0[user_stack_limit-i].kprot = PROT_READ | PROT_WRITE;
currentProc->pt_r0[user_stack_limit-i].valid = 1;
currentProc->pt_r0[user_stack_limit-i].pfn = getFreePage();//XXX
}
/*
* All pages for the new address space are now in place. Flush
* the TLB to get rid of all the old PTEs from this process, so
* we'll be able to do the read() into the new pages below.
*/
WriteRegister(REG_TLB_FLUSH, TLB_FLUSH_0);
/*
* Read the text and data from the file into memory.
*/
if (read(fd, (void *)MEM_INVALID_SIZE, li.text_size+li.data_size) != li.text_size+li.data_size) {
TracePrintf(9, "LoadProgram: couldn't read for '%s'\n", name);
free(argbuf);
close(fd);
/**
* >>>> Since we are returning -2 here, this should mean to
* >>>> the rest of the kernel that the current process should
* >>>> be terminated with an exit status of ERROR reported
* >>>> to its parent process.
*/
kernel_Exit(ERROR);
return (-2);
}
close(fd); /* we've read it all now */
/*
* Now set the page table entries for the program text to be readable
* and executable, but not writable.
*/
/**
*>>>> For text_npg number of PTEs corresponding to the user text
*>>>> pages, set each PTE's kprot to PROT_READ | PROT_EXEC.
*/
for (; i < MEM_INVALID_PAGES + text_npg; i++) {
currentProc->pt_r0[i].kprot = PROT_READ | PROT_EXEC;
}
WriteRegister(REG_TLB_FLUSH, TLB_FLUSH_0);
/*
* Zero out the bss
*/
memset((void *)(MEM_INVALID_SIZE + li.text_size + li.data_size), '\0', li.bss_size);
/*
* Set the entry point in the exception frame.
*/
/* >>>> Initialize pc for the current process to (void *)li.entry */
frame->pc = (void *)li.entry;
/*
* Now, finally, build the argument list on the new stack.
*/
*cpp++ = (char *)argcount; /* the first value at cpp is argc */
cp2 = argbuf;
for (i = 0; i < argcount; i++) { /* copy each argument and set argv */
*cpp++ = cp;
strcpy(cp, cp2);
cp += strlen(cp) + 1;
cp2 += strlen(cp2) + 1;
}
free(argbuf);
*cpp++ = NULL; /* the last argv is a NULL pointer */
*cpp++ = NULL; /* a NULL pointer for an empty envp */
*cpp++ = 0; /* and terminate the auxiliary vector */
/*
* Initialize all regs[] registers for the current process to 0,
* initialize the PSR for the current process also to 0. This
* value for the PSR will make the process run in user mode,
* since this PSR value of 0 does not have the PSR_MODE bit set.
*/
/**
*>>>> Initialize regs[0] through regs[NUM_REGS-1] for the
*>>>> current process to 0.
*>>>> Initialize psr for the current process to 0.
*/
frame->psr = 0;
for (i = 0; i < NUM_REGS; i++) {
frame->regs[i] = 0;
}
return (0);
}