* same with xv6
[mascara-docs.git] / i386 / ucla / src / lab5 / lib / fork.c
blob90e75173480726c1269c38729f8d5bab01bfc935
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.
13 // Return 0 if the fault was not handled, 1 if it was handled.
15 static int
16 pgfault(struct UTrapframe *utf)
18 void *addr = (void *) utf->utf_fault_va;
19 uint32_t err = utf->utf_err;
20 int r;
22 // Check that the faulting access was (1) a write, and (2) to a
23 // copy-on-write page. If not, return 0.
24 // Hint:
25 // Use the read-only page table mappings at vpt
26 // (see <inc/memlayout.h>).
28 // LAB 4: Your code here.
30 // Allocate a new page, map it at a temporary location (PFTEMP),
31 // copy the data from the old page to the new page, then move the new
32 // page to the old page's address.
33 // Hint:
34 // You should make three system calls.
35 // No need to explicitly delete the old page's mapping.
37 // LAB 4: Your code here.
39 panic("pgfault not implemented");
41 return 1;
45 // Map our virtual page pn (address pn*PGSIZE) into the target envid
46 // at the same virtual address. If the page is writable or copy-on-write,
47 // the new mapping must be created copy-on-write, and then our mapping must be
48 // marked copy-on-write as well. (Exercise: Why do we need to mark ours
49 // copy-on-write again if it was already copy-on-write at the beginning of
50 // this function?)
52 // Returns: 0 on success, < 0 on error.
53 // It is also OK to panic on error.
55 static int
56 duppage(envid_t envid, unsigned pn)
58 int r;
60 // LAB 4: Your code here.
61 panic("duppage not implemented");
62 return 0;
66 // User-level fork with copy-on-write.
67 // Set up our page fault handler appropriately.
68 // Create a child.
69 // Copy our address space and page fault handler setup to the child.
70 // Then mark the child as runnable and return.
72 // Returns: child's envid to the parent, 0 to the child, < 0 on error.
73 // It is also OK to panic on error.
75 // Hint:
76 // Use vpd, vpt, and duppage.
77 // Remember to fix "thisenv" in the child process.
78 // Neither user exception stack should ever be marked copy-on-write,
79 // so you must allocate a new page for the child's user exception stack.
81 envid_t
82 fork(void)
84 // LAB 4: Your code here.
85 cprintf("fork not implemented, falling back to dumbfork\n");
86 return dumbfork();
89 // Challenge!
90 int
91 sfork(void)
93 panic("sfork not implemented");
94 return -E_INVAL;