* same with xv6
[mascara-docs.git] / i386 / ucla / src / lab4 / kern / env.c
bloba6533797e5c5047a980ddae5d63f1d14f3115189
1 /* See COPYRIGHT for copyright information. */
3 #include <inc/x86.h>
4 #include <inc/mmu.h>
5 #include <inc/error.h>
6 #include <inc/string.h>
7 #include <inc/assert.h>
8 #include <inc/elf.h>
10 #include <kern/env.h>
11 #include <kern/pmap.h>
12 #include <kern/trap.h>
13 #include <kern/monitor.h>
14 #include <kern/sched.h>
16 struct Env *envs = NULL; // All environments
17 struct Env *curenv = NULL; // The current env
18 static Env *env_free_list = NULL; // Free list
20 #define ENVGENSHIFT 12 // >= LOGNENV
23 // Converts an envid to an env pointer.
24 // If checkperm is set, the specified environment must be either the
25 // current environment or an immediate child of the current environment.
27 // RETURNS
28 // 0 on success, -E_BAD_ENV on error.
29 // On success, sets *env_store to the environment.
30 // On error, sets *env_store to NULL.
32 int
33 envid2env(envid_t envid, struct Env **env_store, bool checkperm)
35 struct Env *e;
37 // If envid is zero, return the current environment.
38 if (envid == 0) {
39 *env_store = curenv;
40 return 0;
43 // Look up the Env structure via the index part of the envid,
44 // then check the env_id field in that struct Env
45 // to ensure that the envid is not stale
46 // (i.e., does not refer to a _previous_ environment
47 // that used the same slot in the envs[] array).
48 e = &envs[ENVX(envid)];
49 if (e->env_status == ENV_FREE || e->env_id != envid) {
50 *env_store = 0;
51 return -E_BAD_ENV;
54 // Check that the calling environment has legitimate permission
55 // to manipulate the specified environment.
56 // If checkperm is set, the specified environment
57 // must be either the current environment
58 // or an immediate child of the current environment.
59 if (checkperm && e != curenv && e->env_parent_id != curenv->env_id) {
60 *env_store = 0;
61 return -E_BAD_ENV;
64 *env_store = e;
65 return 0;
69 // Mark all environments in 'envs' as free, set their env_ids to 0,
70 // and insert them into the env_free_list.
71 // Insert in reverse order, so that the first call to env_alloc()
72 // returns envs[0]. (This is important for grading.)
74 void
75 env_init(void)
77 // LAB 3: Your code here.
81 // Initialize the kernel virtual memory layout for environment e.
82 // Allocate a page directory, set e->env_pgdir accordingly,
83 // and initialize the kernel portion of the new environment's address space.
84 // Do NOT (yet) map anything into the user portion
85 // of the environment's virtual address space.
87 // Returns 0 on success, < 0 on error. Errors include:
88 // -E_NO_MEM if page directory or table could not be allocated.
90 static int
91 env_mem_init(struct Env *e)
93 pte_t i;
94 int r;
95 struct Page *p;
97 // Allocate a page for the page directory
98 p = page_alloc();
99 if (!p)
100 return -E_NO_MEM;
102 // Now, set e->env_pgdir and initialize the page directory.
104 // Hint:
105 // - Remember that page_alloc doesn't zero the page.
106 // - The VA space of all envs is identical above UTOP
107 // (except at UVPT, which we've set below).
108 // See inc/memlayout.h for permissions and layout.
109 // Can you use kern_pgdir as a template? Hint: Yes.
110 // (Make sure you got the permissions right in Lab 2.)
111 // - The initial VA below UTOP is empty.
112 // - You do not need to make any more calls to page_alloc.
113 // - Note: In general, pp_ref is not maintained for
114 // physical pages mapped only above UTOP, but env_pgdir
115 // is an exception -- you need to increment env_pgdir's
116 // pp_ref for env_free to work correctly.
117 // - The functions in kern/pmap.h are handy.
119 // LAB 3: Your code here.
121 // UVPT maps the env's own page table read-only.
122 // Permissions: kernel R, user R
123 e->env_pgdir[PDX(UVPT)] = PADDR(e->env_pgdir) | PTE_P | PTE_U;
125 return 0;
129 // Allocates and initializes a new environment.
130 // On success, the new environment is stored in *newenv_store.
132 // Returns 0 on success, < 0 on failure. Errors include:
133 // -E_NO_FREE_ENV if all NENVS environments are allocated
134 // -E_NO_MEM on memory exhaustion
137 env_alloc(struct Env **newenv_store, envid_t parent_id)
139 int32_t generation;
140 int r;
141 struct Env *e = env_free_list;
142 if (!e)
143 return -E_NO_FREE_ENV;
145 // Allocate and set up the page directory for this environment.
146 if ((r = env_mem_init(e)) < 0)
147 return r;
149 // Generate an env_id for this environment.
150 generation = (e->env_id + (1 << ENVGENSHIFT)) & ~(NENV - 1);
151 if (generation <= 0) // Don't create a negative env_id.
152 generation = 1 << ENVGENSHIFT;
153 e->env_id = generation | (e - envs);
155 // Set the basic status variables.
156 e->env_parent_id = parent_id;
157 e->env_status = ENV_RUNNABLE;
158 e->env_runs = 0;
160 // Clear out all the saved register state,
161 // to prevent the register values
162 // of a prior environment inhabiting this Env structure
163 // from "leaking" into our new environment.
164 memset(&e->env_tf, 0, sizeof(e->env_tf));
166 // Set up appropriate initial values for the segment registers.
167 // GD_UD is the user data segment selector in the GDT, and
168 // GD_UT is the user text segment selector (see inc/memlayout.h).
169 // The low 2 bits of each segment register contains the
170 // Requestor Privilege Level (RPL); 3 means user mode.
171 e->env_tf.tf_ds = GD_UD | 3;
172 e->env_tf.tf_es = GD_UD | 3;
173 e->env_tf.tf_ss = GD_UD | 3;
174 e->env_tf.tf_esp = USTACKTOP;
175 e->env_tf.tf_cs = GD_UT | 3;
176 // You will set e->env_tf.tf_eip later.
178 // Enable interrupts while in user mode.
179 // LAB 4: Your code here.
181 // Clear the page fault handler until user installs one.
182 e->env_pgfault_upcall = 0;
184 // Also clear the IPC receiving flag.
185 e->env_ipc_recving = 0;
187 // commit the allocation
188 env_free_list = e->env_next;
189 *newenv_store = e;
191 cprintf("[%08x] new env %08x\n", curenv ? curenv->env_id : 0, e->env_id);
192 return 0;
196 // Allocate at least len bytes of physical memory for environment env,
197 // and map it at virtual address va in the environment's address space.
198 // Does not zero or otherwise initialize the mapped pages in any way.
199 // Pages should be writable by user and kernel.
200 // Panic if any allocation attempt fails.
202 static void
203 segment_alloc(struct Env *e, uintptr_t va, size_t len)
205 // LAB 3: Your code here.
206 // (But only if you need it for load_elf.)
208 // Hint: It is easier to use segment_alloc if the caller can pass
209 // 'va' and 'len' values that are not page-aligned.
210 // You should round 'va' down, and round 'va + len' up.
214 // Set up the initial program binary, stack, and processor flags
215 // for a user process.
217 // This function loads all loadable segments from the ELF binary image
218 // into the environment's user memory, starting at the appropriate
219 // virtual addresses indicated in the ELF program header.
220 // It also clears to zero any portions of these segments
221 // that are marked in the program header as being mapped
222 // but not actually present in the ELF file -- i.e., the program's bss section.
224 // Finally, this function maps one page for the program's initial stack.
226 // load_elf panics if it encounters problems.
227 // - How might load_elf fail? What might be wrong with the given input?
229 static void
230 load_elf(struct Env *e, uint8_t *binary, size_t size)
232 struct Elf *elf = (struct Elf *) binary;
233 // Load each program segment into environment 'e's virtual memory
234 // at the address specified in the ELF section header.
235 // Only load segments with ph->p_type == ELF_PROG_LOAD.
236 // Each segment's virtual address can be found in ph->p_va
237 // and its size in memory can be found in ph->p_memsz.
238 // The ph->p_filesz bytes from the ELF binary, starting at
239 // 'binary + ph->p_offset', should be copied to virtual address
240 // ph->p_va. Any remaining memory bytes should be cleared to zero.
241 // (The ELF header should have ph->p_filesz <= ph->p_memsz.)
242 // Use functions from the previous lab to allocate and map pages.
244 // All page protection bits should be user read/write for now.
245 // ELF segments are not necessarily page-aligned, but you can
246 // assume for this function that no two segments will touch
247 // the same virtual page.
249 // You may find a function like segment_alloc useful.
251 // Loading the segments is much simpler if you can move data
252 // directly into the virtual addresses stored in the ELF binary.
253 // So which page directory should be in force during
254 // this function?
256 // All this is very similar to what our boot loader does, except the
257 // boot loader reads the code from disk and doesn't check whether
258 // segments are loadable. Take a look at boot/main.c to get ideas.
260 // You must also store the program's entry point somewhere,
261 // to make sure that the environment starts executing at that point.
262 // See env_run() and env_iret() below.
264 // LAB 3: Your code here.
267 // Now map one page for the program's initial stack
268 // at virtual address USTACKTOP - PGSIZE.
269 // (What should the permissions be?)
271 // LAB 3: Your code here.
275 // Creates a new env running a specific binary.
276 // The new env's parent ID is set to 0.
277 // The implementation is a simple wrapper around env_alloc and load_elf.
279 void
280 env_create(uint8_t *binary, size_t size)
282 // LAB 3: Your code here.
286 // Frees env e and all memory it uses.
288 void
289 env_free(struct Env *e)
291 pde_t *pgdir;
292 pte_t *pt;
293 uint32_t pdeno, pteno;
294 physaddr_t pa;
296 // If freeing the current environment, switch to kern_pgdir
297 // before freeing the page directory, just in case the page
298 // gets reused.
299 if (e == curenv)
300 lcr3(PADDR(kern_pgdir));
302 // Note the environment's demise.
303 cprintf("[%08x] free env %08x\n", curenv ? curenv->env_id : 0, e->env_id);
305 // Flush all mapped pages in the user portion of the address space
306 static_assert(UTOP % PTSIZE == 0);
307 pgdir = e->env_pgdir;
308 for (pdeno = 0; pdeno < PDX(UTOP); pdeno++) {
309 // only look at mapped page tables
310 if (!(pgdir[pdeno] & PTE_P))
311 continue;
313 // find the pa and va of the page table
314 pt = (pte_t *) KADDR(PTE_ADDR(pgdir[pdeno]));
316 // unmap all PTEs in this page table
317 for (pteno = 0; pteno <= PTX(~0); pteno++) {
318 if (pt[pteno] & PTE_P)
319 page_remove(pgdir, PGADDR(pdeno, pteno, 0));
322 // free the page table itself
323 pgdir[pdeno] = 0;
324 page_decref(kva2page(pt));
327 // free the page directory
328 e->env_pgdir = 0;
329 page_decref(kva2page(pgdir));
331 // return the environment to the free list
332 e->env_status = ENV_FREE;
333 e->env_next = env_free_list;
334 env_free_list = e;
338 // Frees environment e.
339 // If e was the current env, then runs a new environment (and does not return
340 // to the caller).
342 void
343 env_destroy(struct Env *e)
345 env_free(e);
347 if (curenv == e) {
348 curenv = NULL;
349 sched_yield();
355 // Restores the register values in the Trapframe with the 'iret' instruction.
356 // This exits the kernel and starts executing some environment's code
357 // at the location and registers specified in the Trapframe.
359 // This function does not return.
361 void
362 env_iret(struct Trapframe *tf)
364 __asm __volatile("movl %0,%%esp\n"
365 "\tpopal\n"
366 "\tpopl %%es\n"
367 "\tpopl %%ds\n"
368 "\taddl $0x8,%%esp\n" /* skip tf_trapno and tf_errcode */
369 "\tiret"
370 : : "g" (tf) : "memory");
371 panic("iret failed"); /* mostly to placate the compiler */
375 // Context switch from curenv to env e.
376 // Note: if this is the first call to env_run, curenv is NULL.
378 // This function does not return.
380 void
381 env_run(struct Env *e)
383 // Step 1: If this is a context switch (a new environment is running),
384 // then set 'curenv' to the new environment,
385 // update its 'env_runs' counter, and
386 // and use lcr3() to switch to its address space.
387 // Step 2: Use env_iret() to restore the environment's
388 // registers and drop into user mode in the
389 // environment.
391 // Hint: This function loads the new environment's state from
392 // e->env_tf. Go back through the code you wrote above
393 // and make sure you have set the relevant parts of
394 // e->env_tf to sensible values.
396 // LAB 3: Your code here.
398 panic("env_run not yet implemented");