* same with xv6
[mascara-docs.git] / i386 / MIT / course / src / git.lab / lib / fork.c
blobe7df9eb366509e20b5a4af2f2c5059b45bcc8c42
1 // implement fork from user space
3 #include <inc/string.h>
4 #include <inc/lib.h>
6 // PTE_COW marks copy-on-write page table entries.
7 // It is one of the bits explicitly allocated to user processes (PTE_AVAIL).
8 #define PTE_COW 0x800
11 // Custom page fault handler - if faulting page is copy-on-write,
12 // map in our own private writable copy.
14 static void
15 pgfault(struct UTrapframe *utf)
17 void *addr = (void *) utf->utf_fault_va;
18 uint32_t err = utf->utf_err;
19 int r;
21 // Check that the faulting access was (1) a write, and (2) to a
22 // copy-on-write page. If not, panic.
23 // Hint:
24 // Use the read-only page table mappings at vpt
25 // (see <inc/memlayout.h>).
27 // LAB 4: Your code here.
29 // Allocate a new page, map it at a temporary location (PFTEMP),
30 // copy the data from the old page to the new page, then move the new
31 // page to the old page's address.
32 // Hint:
33 // You should make three system calls.
34 // No need to explicitly delete the old page's mapping.
36 // LAB 4: Your code here.
38 panic("pgfault not implemented");
42 // Map our virtual page pn (address pn*PGSIZE) into the target envid
43 // at the same virtual address. If the page is writable or copy-on-write,
44 // the new mapping must be created copy-on-write, and then our mapping must be
45 // marked copy-on-write as well. (Exercise: Why do we need to mark ours
46 // copy-on-write again if it was already copy-on-write at the beginning of
47 // this function?)
49 // Returns: 0 on success, < 0 on error.
50 // It is also OK to panic on error.
52 static int
53 duppage(envid_t envid, unsigned pn)
55 int r;
57 // LAB 4: Your code here.
58 panic("duppage not implemented");
59 return 0;
63 // User-level fork with copy-on-write.
64 // Set up our page fault handler appropriately.
65 // Create a child.
66 // Copy our address space and page fault handler setup to the child.
67 // Then mark the child as runnable and return.
69 // Returns: child's envid to the parent, 0 to the child, < 0 on error.
70 // It is also OK to panic on error.
72 // Hint:
73 // Use vpd, vpt, and duppage.
74 // Remember to fix "thisenv" in the child process.
75 // Neither user exception stack should ever be marked copy-on-write,
76 // so you must allocate a new page for the child's user exception stack.
78 envid_t
79 fork(void)
81 // LAB 4: Your code here.
82 panic("fork not implemented");
85 // Challenge!
86 int
87 sfork(void)
89 panic("sfork not implemented");
90 return -E_INVAL;