* same with xv6
[mascara-docs.git] / i386 / standford / 2004 / src / lab5 / kern / syscall.c
blob9ea80caff6c158135bf480efe8cb9febaa6093e1
1 /* See COPYRIGHT for copyright information. */
3 #include <inc/x86.h>
4 #include <inc/error.h>
5 #include <inc/string.h>
6 #include <inc/assert.h>
8 #include <kern/env.h>
9 #include <kern/pmap.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.
16 static void
17 sys_cputs(char *s)
19 printf("%s", s);
22 // read a character from the system console
23 static int
24 sys_cgetc(void)
26 int c;
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)
31 ; /* spin */
33 return c;
36 // return the current environment's envid
37 static u_int
38 sys_getenvid(void)
40 return curenv->env_id;
43 // destroy a given environment
44 // (possibly the currently running environment)
45 static int
46 sys_env_destroy(u_int envid)
48 int r;
49 struct Env *e;
51 if ((r=envid2env(envid, &e, 1)) < 0)
52 return r;
53 if (e == curenv)
54 printf("[%08x] exiting gracefully\n", curenv->env_id);
55 else
56 printf("[%08x] destroying %08x\n", curenv->env_id, e->env_id);
57 env_destroy(e);
58 return 0;
61 // Deschedule current environment and pick a different one to run.
62 static void
63 sys_yield(void)
65 sched_yield();
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
73 // side-effect.
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
84 static int
85 sys_mem_alloc(u_int envid, u_int va, u_int perm)
87 // Your code here.
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
95 // page.
97 // Return 0 on success, < 0 on error.
99 // Cannot access pages above UTOP.
100 static int
101 sys_mem_map(u_int srcid, u_int srcva, u_int dstid, u_int dstva, u_int perm)
103 // Your code here.
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.
113 static int
114 sys_mem_unmap(u_int envid, u_int va)
116 // Your code here.
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.
128 static int
129 sys_env_alloc(void)
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.
140 static int
141 sys_set_trapframe(u_int envid, struct Trapframe *tf)
143 // Your code here (in lab 4).
145 // HINT:
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.
158 static int
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.
169 static int
170 sys_set_pgfault_entry(u_int envid, u_int func)
172 // Your code here.
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
196 // in sys_mem_map.
198 // Hint: you will find envid2env() useful.
199 static int
200 sys_ipc_can_send(u_int envid, u_int value, u_int srcva, u_int perm)
202 // Your code here
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
211 // zero.
212 static void
213 sys_ipc_recv(u_int dstva)
215 // Your code here
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);
226 // Your code here
227 panic("syscall not implemented");