xref: /freebsd/sys/riscv/riscv/vm_machdep.c (revision 5d3e7166)
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/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/limits.h>
41 #include <sys/proc.h>
42 #include <sys/sf_buf.h>
43 #include <sys/signal.h>
44 #include <sys/unistd.h>
45 
46 #include <vm/vm.h>
47 #include <vm/vm_page.h>
48 #include <vm/vm_map.h>
49 #include <vm/uma.h>
50 #include <vm/uma_int.h>
51 
52 #include <machine/riscvreg.h>
53 #include <machine/cpu.h>
54 #include <machine/pcb.h>
55 #include <machine/frame.h>
56 #include <machine/sbi.h>
57 
58 #if __riscv_xlen == 64
59 #define	TP_OFFSET	16	/* sizeof(struct tcb) */
60 #endif
61 
62 /*
63  * Finish a fork operation, with process p2 nearly set up.
64  * Copy and update the pcb, set up the stack so that the child
65  * ready to run and return to user mode.
66  */
67 void
68 cpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags)
69 {
70 	struct pcb *pcb2;
71 	struct trapframe *tf;
72 
73 	if ((flags & RFPROC) == 0)
74 		return;
75 
76 	/* RISCVTODO: save the FPU state here */
77 
78 	pcb2 = (struct pcb *)(td2->td_kstack +
79 	    td2->td_kstack_pages * PAGE_SIZE) - 1;
80 
81 	td2->td_pcb = pcb2;
82 	bcopy(td1->td_pcb, pcb2, sizeof(*pcb2));
83 
84 	tf = (struct trapframe *)STACKALIGN((struct trapframe *)pcb2 - 1);
85 	bcopy(td1->td_frame, tf, sizeof(*tf));
86 
87 	/* Clear syscall error flag */
88 	tf->tf_t[0] = 0;
89 
90 	/* Arguments for child */
91 	tf->tf_a[0] = 0;
92 	tf->tf_a[1] = 0;
93 	tf->tf_sstatus |= (SSTATUS_SPIE); /* Enable interrupts. */
94 	tf->tf_sstatus &= ~(SSTATUS_SPP); /* User mode. */
95 
96 	td2->td_frame = tf;
97 
98 	/* Set the return value registers for fork() */
99 	td2->td_pcb->pcb_s[0] = (uintptr_t)fork_return;
100 	td2->td_pcb->pcb_s[1] = (uintptr_t)td2;
101 	td2->td_pcb->pcb_ra = (uintptr_t)fork_trampoline;
102 	td2->td_pcb->pcb_sp = (uintptr_t)td2->td_frame;
103 
104 	/* Setup to release spin count in fork_exit(). */
105 	td2->td_md.md_spinlock_count = 1;
106 	td2->td_md.md_saved_sstatus_ie = (SSTATUS_SIE);
107 }
108 
109 void
110 cpu_reset(void)
111 {
112 
113 	sbi_system_reset(SBI_SRST_TYPE_COLD_REBOOT, SBI_SRST_REASON_NONE);
114 
115 	while(1);
116 }
117 
118 void
119 cpu_thread_swapin(struct thread *td)
120 {
121 }
122 
123 void
124 cpu_thread_swapout(struct thread *td)
125 {
126 }
127 
128 void
129 cpu_set_syscall_retval(struct thread *td, int error)
130 {
131 	struct trapframe *frame;
132 
133 	frame = td->td_frame;
134 
135 	if (__predict_true(error == 0)) {
136 		frame->tf_a[0] = td->td_retval[0];
137 		frame->tf_a[1] = td->td_retval[1];
138 		frame->tf_t[0] = 0;		/* syscall succeeded */
139 		return;
140 	}
141 
142 	switch (error) {
143 	case ERESTART:
144 		frame->tf_sepc -= 4;		/* prev instruction */
145 		break;
146 	case EJUSTRETURN:
147 		break;
148 	default:
149 		frame->tf_a[0] = error;
150 		frame->tf_t[0] = 1;		/* syscall error */
151 		break;
152 	}
153 }
154 
155 /*
156  * Initialize machine state, mostly pcb and trap frame for a new
157  * thread, about to return to userspace.  Put enough state in the new
158  * thread's PCB to get it to go back to the fork_return(), which
159  * finalizes the thread state and handles peculiarities of the first
160  * return to userspace for the new thread.
161  */
162 void
163 cpu_copy_thread(struct thread *td, struct thread *td0)
164 {
165 
166 	bcopy(td0->td_frame, td->td_frame, sizeof(struct trapframe));
167 	bcopy(td0->td_pcb, td->td_pcb, sizeof(struct pcb));
168 
169 	td->td_pcb->pcb_s[0] = (uintptr_t)fork_return;
170 	td->td_pcb->pcb_s[1] = (uintptr_t)td;
171 	td->td_pcb->pcb_ra = (uintptr_t)fork_trampoline;
172 	td->td_pcb->pcb_sp = (uintptr_t)td->td_frame;
173 
174 	/* Setup to release spin count in fork_exit(). */
175 	td->td_md.md_spinlock_count = 1;
176 	td->td_md.md_saved_sstatus_ie = (SSTATUS_SIE);
177 }
178 
179 /*
180  * Set that machine state for performing an upcall that starts
181  * the entry function with the given argument.
182  */
183 void
184 cpu_set_upcall(struct thread *td, void (*entry)(void *), void *arg,
185 	stack_t *stack)
186 {
187 	struct trapframe *tf;
188 
189 	tf = td->td_frame;
190 
191 	tf->tf_sp = STACKALIGN((uintptr_t)stack->ss_sp + stack->ss_size);
192 	tf->tf_sepc = (register_t)entry;
193 	tf->tf_a[0] = (register_t)arg;
194 }
195 
196 int
197 cpu_set_user_tls(struct thread *td, void *tls_base)
198 {
199 
200 	if ((uintptr_t)tls_base >= VM_MAXUSER_ADDRESS)
201 		return (EINVAL);
202 
203 	/*
204 	 * The user TLS is set by modifying the trapframe's tp value, which
205 	 * will be restored when returning to userspace.
206 	 */
207 	td->td_frame->tf_tp = (register_t)tls_base + TP_OFFSET;
208 
209 	return (0);
210 }
211 
212 void
213 cpu_thread_exit(struct thread *td)
214 {
215 }
216 
217 void
218 cpu_thread_alloc(struct thread *td)
219 {
220 
221 	td->td_pcb = (struct pcb *)(td->td_kstack +
222 	    td->td_kstack_pages * PAGE_SIZE) - 1;
223 	td->td_frame = (struct trapframe *)STACKALIGN(
224 	    (caddr_t)td->td_pcb - 8 - sizeof(struct trapframe));
225 }
226 
227 void
228 cpu_thread_free(struct thread *td)
229 {
230 }
231 
232 void
233 cpu_thread_clean(struct thread *td)
234 {
235 }
236 
237 /*
238  * Intercept the return address from a freshly forked process that has NOT
239  * been scheduled yet.
240  *
241  * This is needed to make kernel threads stay in kernel mode.
242  */
243 void
244 cpu_fork_kthread_handler(struct thread *td, void (*func)(void *), void *arg)
245 {
246 
247 	td->td_pcb->pcb_s[0] = (uintptr_t)func;
248 	td->td_pcb->pcb_s[1] = (uintptr_t)arg;
249 	td->td_pcb->pcb_ra = (uintptr_t)fork_trampoline;
250 	td->td_pcb->pcb_sp = (uintptr_t)td->td_frame;
251 }
252 
253 void
254 cpu_exit(struct thread *td)
255 {
256 }
257 
258 bool
259 cpu_exec_vmspace_reuse(struct proc *p __unused, vm_map_t map __unused)
260 {
261 
262 	return (true);
263 }
264 
265 int
266 cpu_procctl(struct thread *td __unused, int idtype __unused, id_t id __unused,
267     int com __unused, void *data __unused)
268 {
269 
270 	return (EINVAL);
271 }
272