1 /*-
2 * Copyright (c) 2015-2018 Ruslan Bukin <br@bsdpad.com>
3 * All rights reserved.
4 *
5 * Portions of this software were developed by SRI International and the
6 * University of Cambridge Computer Laboratory under DARPA/AFRL contract
7 * FA8750-10-C-0237 ("CTSRD"), as part of the DARPA CRASH research programme.
8 *
9 * Portions of this software were developed by the University of Cambridge
10 * Computer Laboratory as part of the CTSRD Project, with support from the
11 * UK Higher Education Innovation Fund (HEIF).
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/limits.h>
38 #include <sys/proc.h>
39 #include <sys/sf_buf.h>
40 #include <sys/signal.h>
41 #include <sys/unistd.h>
42
43 #include <vm/vm.h>
44 #include <vm/vm_page.h>
45 #include <vm/vm_map.h>
46 #include <vm/uma.h>
47 #include <vm/uma_int.h>
48
49 #include <machine/riscvreg.h>
50 #include <machine/cpu.h>
51 #include <machine/cpufunc.h>
52 #include <machine/pcb.h>
53 #include <machine/frame.h>
54 #include <machine/sbi.h>
55
56 #if __riscv_xlen == 64
57 #define TP_OFFSET 16 /* sizeof(struct tcb) */
58 #endif
59
60 /*
61 * Finish a fork operation, with process p2 nearly set up.
62 * Copy and update the pcb, set up the stack so that the child
63 * ready to run and return to user mode.
64 */
65 void
cpu_fork(struct thread * td1,struct proc * p2,struct thread * td2,int flags)66 cpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags)
67 {
68 struct pcb *pcb2;
69 struct trapframe *tf;
70
71 if ((flags & RFPROC) == 0)
72 return;
73
74 /* RISCVTODO: save the FPU state here */
75
76 pcb2 = (struct pcb *)(td2->td_kstack +
77 td2->td_kstack_pages * PAGE_SIZE) - 1;
78
79 td2->td_pcb = pcb2;
80 bcopy(td1->td_pcb, pcb2, sizeof(*pcb2));
81
82 tf = (struct trapframe *)STACKALIGN((struct trapframe *)pcb2 - 1);
83 bcopy(td1->td_frame, tf, sizeof(*tf));
84
85 /* Clear syscall error flag */
86 tf->tf_t[0] = 0;
87
88 /* Arguments for child */
89 tf->tf_a[0] = 0;
90 tf->tf_a[1] = 0;
91 tf->tf_sstatus |= (SSTATUS_SPIE); /* Enable interrupts. */
92 tf->tf_sstatus &= ~(SSTATUS_SPP); /* User mode. */
93
94 td2->td_frame = tf;
95
96 /* Set the return value registers for fork() */
97 td2->td_pcb->pcb_s[0] = (uintptr_t)fork_return;
98 td2->td_pcb->pcb_s[1] = (uintptr_t)td2;
99 td2->td_pcb->pcb_ra = (uintptr_t)fork_trampoline;
100 td2->td_pcb->pcb_sp = (uintptr_t)td2->td_frame;
101
102 /* Setup to release spin count in fork_exit(). */
103 td2->td_md.md_spinlock_count = 1;
104 td2->td_md.md_saved_sstatus_ie = (SSTATUS_SIE);
105 }
106
107 void
cpu_reset(void)108 cpu_reset(void)
109 {
110
111 sbi_system_reset(SBI_SRST_TYPE_COLD_REBOOT, SBI_SRST_REASON_NONE);
112
113 while(1);
114 }
115
116 void
cpu_thread_swapin(struct thread * td)117 cpu_thread_swapin(struct thread *td)
118 {
119 }
120
121 void
cpu_thread_swapout(struct thread * td)122 cpu_thread_swapout(struct thread *td)
123 {
124 }
125
126 void
cpu_set_syscall_retval(struct thread * td,int error)127 cpu_set_syscall_retval(struct thread *td, int error)
128 {
129 struct trapframe *frame;
130
131 frame = td->td_frame;
132
133 if (__predict_true(error == 0)) {
134 frame->tf_a[0] = td->td_retval[0];
135 frame->tf_a[1] = td->td_retval[1];
136 frame->tf_t[0] = 0; /* syscall succeeded */
137 return;
138 }
139
140 switch (error) {
141 case ERESTART:
142 frame->tf_sepc -= 4; /* prev instruction */
143 break;
144 case EJUSTRETURN:
145 break;
146 default:
147 frame->tf_a[0] = error;
148 frame->tf_t[0] = 1; /* syscall error */
149 break;
150 }
151 }
152
153 /*
154 * Initialize machine state, mostly pcb and trap frame for a new
155 * thread, about to return to userspace. Put enough state in the new
156 * thread's PCB to get it to go back to the fork_return(), which
157 * finalizes the thread state and handles peculiarities of the first
158 * return to userspace for the new thread.
159 */
160 void
cpu_copy_thread(struct thread * td,struct thread * td0)161 cpu_copy_thread(struct thread *td, struct thread *td0)
162 {
163
164 bcopy(td0->td_frame, td->td_frame, sizeof(struct trapframe));
165 bcopy(td0->td_pcb, td->td_pcb, sizeof(struct pcb));
166
167 td->td_pcb->pcb_s[0] = (uintptr_t)fork_return;
168 td->td_pcb->pcb_s[1] = (uintptr_t)td;
169 td->td_pcb->pcb_ra = (uintptr_t)fork_trampoline;
170 td->td_pcb->pcb_sp = (uintptr_t)td->td_frame;
171
172 /* Setup to release spin count in fork_exit(). */
173 td->td_md.md_spinlock_count = 1;
174 td->td_md.md_saved_sstatus_ie = (SSTATUS_SIE);
175 }
176
177 /*
178 * Set that machine state for performing an upcall that starts
179 * the entry function with the given argument.
180 */
181 int
cpu_set_upcall(struct thread * td,void (* entry)(void *),void * arg,stack_t * stack)182 cpu_set_upcall(struct thread *td, void (*entry)(void *), void *arg,
183 stack_t *stack)
184 {
185 struct trapframe *tf;
186
187 tf = td->td_frame;
188
189 tf->tf_sp = STACKALIGN((uintptr_t)stack->ss_sp + stack->ss_size);
190 tf->tf_sepc = (register_t)entry;
191 tf->tf_a[0] = (register_t)arg;
192 return (0);
193 }
194
195 int
cpu_set_user_tls(struct thread * td,void * tls_base)196 cpu_set_user_tls(struct thread *td, void *tls_base)
197 {
198
199 if ((uintptr_t)tls_base >= VM_MAXUSER_ADDRESS)
200 return (EINVAL);
201
202 /*
203 * The user TLS is set by modifying the trapframe's tp value, which
204 * will be restored when returning to userspace.
205 */
206 td->td_frame->tf_tp = (register_t)tls_base + TP_OFFSET;
207
208 return (0);
209 }
210
211 void
cpu_thread_exit(struct thread * td)212 cpu_thread_exit(struct thread *td)
213 {
214 }
215
216 void
cpu_thread_alloc(struct thread * td)217 cpu_thread_alloc(struct thread *td)
218 {
219
220 td->td_pcb = (struct pcb *)(td->td_kstack +
221 td->td_kstack_pages * PAGE_SIZE) - 1;
222 td->td_frame = (struct trapframe *)STACKALIGN(
223 (caddr_t)td->td_pcb - 8 - sizeof(struct trapframe));
224 }
225
226 void
cpu_thread_free(struct thread * td)227 cpu_thread_free(struct thread *td)
228 {
229 }
230
231 void
cpu_thread_clean(struct thread * td)232 cpu_thread_clean(struct thread *td)
233 {
234 }
235
236 /*
237 * Intercept the return address from a freshly forked process that has NOT
238 * been scheduled yet.
239 *
240 * This is needed to make kernel threads stay in kernel mode.
241 */
242 void
cpu_fork_kthread_handler(struct thread * td,void (* func)(void *),void * arg)243 cpu_fork_kthread_handler(struct thread *td, void (*func)(void *), void *arg)
244 {
245
246 td->td_pcb->pcb_s[0] = (uintptr_t)func;
247 td->td_pcb->pcb_s[1] = (uintptr_t)arg;
248 td->td_pcb->pcb_ra = (uintptr_t)fork_trampoline;
249 td->td_pcb->pcb_sp = (uintptr_t)td->td_frame;
250 }
251
252 void
cpu_exit(struct thread * td)253 cpu_exit(struct thread *td)
254 {
255 }
256
257 bool
cpu_exec_vmspace_reuse(struct proc * p __unused,vm_map_t map __unused)258 cpu_exec_vmspace_reuse(struct proc *p __unused, vm_map_t map __unused)
259 {
260
261 return (true);
262 }
263
264 int
cpu_procctl(struct thread * td __unused,int idtype __unused,id_t id __unused,int com __unused,void * data __unused)265 cpu_procctl(struct thread *td __unused, int idtype __unused, id_t id __unused,
266 int com __unused, void *data __unused)
267 {
268
269 return (EINVAL);
270 }
271
272 void
cpu_sync_core(void)273 cpu_sync_core(void)
274 {
275 fence_i();
276 }
277