1 /* See COPYRIGHT for copyright information. */
5 #include <inc/string.h>
6 #include <inc/assert.h>
10 #include <kern/trap.h>
11 #include <kern/syscall.h>
12 #include <kern/console.h>
13 #include <kern/sched.h>
15 // print a string to the system console.
22 // read a character from the system console
28 // The cons_getc() primitive doesn't wait for a character,
29 // but the sys_cgetc() system call does.
30 while ((c
= cons_getc()) == 0)
36 // return the current environment's envid
40 return curenv
->env_id
;
43 // destroy a given environment
44 // (possibly the currently running environment)
46 sys_env_destroy(u_int envid
)
51 if ((r
=envid2env(envid
, &e
, 1)) < 0)
54 printf("[%08x] exiting gracefully\n", curenv
->env_id
);
56 printf("[%08x] destroying %08x\n", curenv
->env_id
, e
->env_id
);
61 // Deschedule current environment and pick a different one to run.
69 // Allocate a page of memory and map it at 'va' with permission
70 // 'perm' in the address space of 'envid'.
72 // If a page is already mapped at 'va', that page is unmapped as a
75 // perm -- PTE_U|PTE_P are required,
76 // PTE_AVAIL|PTE_W are optional,
77 // but no other bits are allowed (return -E_INVAL)
79 // Return 0 on success, < 0 on error
80 // - va must be < UTOP
81 // - an environment may modify its own address space or the
82 // address space of its children
85 sys_mem_alloc(u_int envid
, u_int va
, u_int perm
)
88 panic("sys_mem_alloc not implemented");
91 // Map the page of memory at 'srcva' in srcid's address space
92 // at 'dstva' in dstid's address space with permission 'perm'.
93 // Perm has the same restrictions as in sys_mem_alloc, except
94 // that it also must not grant write access to a read-only
97 // Return 0 on success, < 0 on error.
99 // Cannot access pages above UTOP.
101 sys_mem_map(u_int srcid
, u_int srcva
, u_int dstid
, u_int dstva
, u_int perm
)
104 panic("sys_mem_map not implemented");
107 // Unmap the page of memory at 'va' in the address space of 'envid'
108 // (if no page is mapped, the function silently succeeds)
110 // Return 0 on success, < 0 on error.
112 // Cannot unmap pages above UTOP.
114 sys_mem_unmap(u_int envid
, u_int va
)
117 panic("sys_mem_unmap not implemented");
120 // Allocate a new environment.
122 // The new child is left as env_alloc created it, except that
123 // status is set to ENV_NOT_RUNNABLE and the register set is copied
124 // from the current environment. In the child, the register set is
125 // tweaked so sys_env_alloc returns 0.
127 // Returns envid of new environment, or < 0 on error.
131 // Your code here (in lab 4).
132 panic("sys_env_alloc not implemented");
135 // Set envid's trap frame to tf.
137 // Returns 0 on success, < 0 on error.
139 // Return -E_INVAL if the environment cannot be manipulated.
141 sys_set_trapframe(u_int envid
, struct Trapframe
*tf
)
143 // Your code here (in lab 4).
146 // Should enforce some limits on tf_eflags and tf_cs
147 // The case were envid is the current environment needs
148 // to be handled specially.
150 panic("sys_set_trapframe not implemented");
153 // Set envid's env_status to status.
155 // Returns 0 on success, < 0 on error.
157 // Return -E_INVAL if status is not a valid status for an environment.
159 sys_set_status(u_int envid
, u_int status
)
161 // Your code here (in lab 4).
162 panic("sys_set_status not implemented");
165 // Set envid's pagefault handler entry point and exception stack.
166 // (xstacktop points one byte past exception stack).
168 // Returns 0 on success, < 0 on error.
170 sys_set_pgfault_entry(u_int envid
, u_int func
)
173 panic("sys_set_pgfault_entry not implemented");
176 // Try to send 'value' to the target env 'envid'.
177 // If va != 0, then also send page currently mapped at va,
178 // so that receiver gets a duplicate mapping of the same page.
180 // The send fails with a return value of -E_IPC_NOT_RECV if the
181 // target has not requested IPC with sys_ipc_recv.
183 // Otherwise, the send succeeds, and the target's ipc fields are
184 // updated as follows:
185 // env_ipc_recving is set to 0 to block future sends
186 // env_ipc_from is set to the sending envid
187 // env_ipc_value is set to the 'value' parameter
188 // The target environment is marked runnable again.
190 // Return 0 on success, < 0 on error.
192 // If the sender sends a page but the receiver isn't asking for one,
193 // then no page mapping is transferred but no error occurs.
195 // srcva and perm should have the same restrictions as they had
198 // Hint: you will find envid2env() useful.
200 sys_ipc_can_send(u_int envid
, u_int value
, u_int srcva
, u_int perm
)
203 panic("sys_ipc_can_send not implemented");
206 // Block until a value is ready. Record that you want to receive,
207 // mark yourself not runnable, and then give up the CPU.
209 // Again, dstva should have the same restrictions as it had in
210 // sys_mem_map. If it violates these restrictions, assume that it is
213 sys_ipc_recv(u_int dstva
)
216 panic("sys_ipc_recv not implemented");
220 // Dispatches to the correct kernel function, passing the arguments.
222 syscall(u_int sn
, u_int a1
, u_int a2
, u_int a3
, u_int a4
, u_int a5
)
224 // printf("syscall %d %x %x %x from env %08x\n", sn, a1, a2, a3, curenv->env_id);
227 panic("syscall not implemented");