]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/riscv/riscv/exec_machdep.c
Import device-tree files from Linux 6.4
[FreeBSD/FreeBSD.git] / sys / riscv / riscv / exec_machdep.c
1 /*-
2  * Copyright (c) 2014 Andrew Turner
3  * Copyright (c) 2015-2017 Ruslan Bukin <br@bsdpad.com>
4  * All rights reserved.
5  *
6  * Portions of this software were developed by SRI International and the
7  * University of Cambridge Computer Laboratory under DARPA/AFRL contract
8  * FA8750-10-C-0237 ("CTSRD"), as part of the DARPA CRASH research programme.
9  *
10  * Portions of this software were developed by the University of Cambridge
11  * Computer Laboratory as part of the CTSRD Project, with support from the
12  * UK Higher Education Innovation Fund (HEIF).
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/exec.h>
42 #include <sys/imgact.h>
43 #include <sys/kdb.h>
44 #include <sys/kernel.h>
45 #include <sys/ktr.h>
46 #include <sys/limits.h>
47 #include <sys/lock.h>
48 #include <sys/mutex.h>
49 #include <sys/proc.h>
50 #include <sys/ptrace.h>
51 #include <sys/reg.h>
52 #include <sys/rwlock.h>
53 #include <sys/sched.h>
54 #include <sys/signalvar.h>
55 #include <sys/syscallsubr.h>
56 #include <sys/sysent.h>
57 #include <sys/sysproto.h>
58 #include <sys/ucontext.h>
59
60 #include <machine/cpu.h>
61 #include <machine/fpe.h>
62 #include <machine/kdb.h>
63 #include <machine/pcb.h>
64 #include <machine/pte.h>
65 #include <machine/riscvreg.h>
66 #include <machine/sbi.h>
67 #include <machine/trap.h>
68
69 #include <vm/vm.h>
70 #include <vm/vm_param.h>
71 #include <vm/pmap.h>
72 #include <vm/vm_map.h>
73
74 static void get_fpcontext(struct thread *td, mcontext_t *mcp);
75 static void set_fpcontext(struct thread *td, mcontext_t *mcp);
76
77 _Static_assert(sizeof(mcontext_t) == 864, "mcontext_t size incorrect");
78 _Static_assert(sizeof(ucontext_t) == 936, "ucontext_t size incorrect");
79 _Static_assert(sizeof(siginfo_t) == 80, "siginfo_t size incorrect");
80
81 int
82 fill_regs(struct thread *td, struct reg *regs)
83 {
84         struct trapframe *frame;
85
86         frame = td->td_frame;
87         regs->sepc = frame->tf_sepc;
88         regs->sstatus = frame->tf_sstatus;
89         regs->ra = frame->tf_ra;
90         regs->sp = frame->tf_sp;
91         regs->gp = frame->tf_gp;
92         regs->tp = frame->tf_tp;
93
94         memcpy(regs->t, frame->tf_t, sizeof(regs->t));
95         memcpy(regs->s, frame->tf_s, sizeof(regs->s));
96         memcpy(regs->a, frame->tf_a, sizeof(regs->a));
97
98         return (0);
99 }
100
101 int
102 set_regs(struct thread *td, struct reg *regs)
103 {
104         struct trapframe *frame;
105
106         frame = td->td_frame;
107         frame->tf_sepc = regs->sepc;
108         frame->tf_ra = regs->ra;
109         frame->tf_sp = regs->sp;
110         frame->tf_gp = regs->gp;
111         frame->tf_tp = regs->tp;
112
113         memcpy(frame->tf_t, regs->t, sizeof(frame->tf_t));
114         memcpy(frame->tf_s, regs->s, sizeof(frame->tf_s));
115         memcpy(frame->tf_a, regs->a, sizeof(frame->tf_a));
116
117         return (0);
118 }
119
120 int
121 fill_fpregs(struct thread *td, struct fpreg *regs)
122 {
123         struct pcb *pcb;
124
125         pcb = td->td_pcb;
126
127         if ((pcb->pcb_fpflags & PCB_FP_STARTED) != 0) {
128                 /*
129                  * If we have just been running FPE instructions we will
130                  * need to save the state to memcpy it below.
131                  */
132                 if (td == curthread)
133                         fpe_state_save(td);
134
135                 memcpy(regs->fp_x, pcb->pcb_x, sizeof(regs->fp_x));
136                 regs->fp_fcsr = pcb->pcb_fcsr;
137         } else
138                 memset(regs, 0, sizeof(*regs));
139
140         return (0);
141 }
142
143 int
144 set_fpregs(struct thread *td, struct fpreg *regs)
145 {
146         struct trapframe *frame;
147         struct pcb *pcb;
148
149         frame = td->td_frame;
150         pcb = td->td_pcb;
151
152         memcpy(pcb->pcb_x, regs->fp_x, sizeof(regs->fp_x));
153         pcb->pcb_fcsr = regs->fp_fcsr;
154         pcb->pcb_fpflags |= PCB_FP_STARTED;
155         frame->tf_sstatus &= ~SSTATUS_FS_MASK;
156         frame->tf_sstatus |= SSTATUS_FS_CLEAN;
157
158         return (0);
159 }
160
161 int
162 fill_dbregs(struct thread *td, struct dbreg *regs)
163 {
164
165         panic("fill_dbregs");
166 }
167
168 int
169 set_dbregs(struct thread *td, struct dbreg *regs)
170 {
171
172         panic("set_dbregs");
173 }
174
175 void
176 exec_setregs(struct thread *td, struct image_params *imgp, uintptr_t stack)
177 {
178         struct trapframe *tf;
179         struct pcb *pcb;
180
181         tf = td->td_frame;
182         pcb = td->td_pcb;
183
184         memset(tf, 0, sizeof(struct trapframe));
185
186         tf->tf_a[0] = stack;
187         tf->tf_sp = STACKALIGN(stack);
188         tf->tf_ra = imgp->entry_addr;
189         tf->tf_sepc = imgp->entry_addr;
190
191         pcb->pcb_fpflags &= ~PCB_FP_STARTED;
192 }
193
194 /* Sanity check these are the same size, they will be memcpy'd to and from */
195 CTASSERT(sizeof(((struct trapframe *)0)->tf_a) ==
196     sizeof((struct gpregs *)0)->gp_a);
197 CTASSERT(sizeof(((struct trapframe *)0)->tf_s) ==
198     sizeof((struct gpregs *)0)->gp_s);
199 CTASSERT(sizeof(((struct trapframe *)0)->tf_t) ==
200     sizeof((struct gpregs *)0)->gp_t);
201 CTASSERT(sizeof(((struct trapframe *)0)->tf_a) ==
202     sizeof((struct reg *)0)->a);
203 CTASSERT(sizeof(((struct trapframe *)0)->tf_s) ==
204     sizeof((struct reg *)0)->s);
205 CTASSERT(sizeof(((struct trapframe *)0)->tf_t) ==
206     sizeof((struct reg *)0)->t);
207
208 int
209 get_mcontext(struct thread *td, mcontext_t *mcp, int clear_ret)
210 {
211         struct trapframe *tf = td->td_frame;
212
213         memcpy(mcp->mc_gpregs.gp_t, tf->tf_t, sizeof(mcp->mc_gpregs.gp_t));
214         memcpy(mcp->mc_gpregs.gp_s, tf->tf_s, sizeof(mcp->mc_gpregs.gp_s));
215         memcpy(mcp->mc_gpregs.gp_a, tf->tf_a, sizeof(mcp->mc_gpregs.gp_a));
216
217         if (clear_ret & GET_MC_CLEAR_RET) {
218                 mcp->mc_gpregs.gp_a[0] = 0;
219                 mcp->mc_gpregs.gp_t[0] = 0; /* clear syscall error */
220         }
221
222         mcp->mc_gpregs.gp_ra = tf->tf_ra;
223         mcp->mc_gpregs.gp_sp = tf->tf_sp;
224         mcp->mc_gpregs.gp_gp = tf->tf_gp;
225         mcp->mc_gpregs.gp_tp = tf->tf_tp;
226         mcp->mc_gpregs.gp_sepc = tf->tf_sepc;
227         mcp->mc_gpregs.gp_sstatus = tf->tf_sstatus;
228         get_fpcontext(td, mcp);
229
230         return (0);
231 }
232
233 int
234 set_mcontext(struct thread *td, mcontext_t *mcp)
235 {
236         struct trapframe *tf;
237
238         tf = td->td_frame;
239
240         /*
241          * Permit changes to the USTATUS bits of SSTATUS.
242          *
243          * Ignore writes to read-only bits (SD, XS).
244          *
245          * Ignore writes to the FS field as set_fpcontext() will set
246          * it explicitly.
247          */
248         if (((mcp->mc_gpregs.gp_sstatus ^ tf->tf_sstatus) &
249             ~(SSTATUS_SD | SSTATUS_XS_MASK | SSTATUS_FS_MASK | SSTATUS_UPIE |
250             SSTATUS_UIE)) != 0)
251                 return (EINVAL);
252
253         memcpy(tf->tf_t, mcp->mc_gpregs.gp_t, sizeof(tf->tf_t));
254         memcpy(tf->tf_s, mcp->mc_gpregs.gp_s, sizeof(tf->tf_s));
255         memcpy(tf->tf_a, mcp->mc_gpregs.gp_a, sizeof(tf->tf_a));
256
257         tf->tf_ra = mcp->mc_gpregs.gp_ra;
258         tf->tf_sp = mcp->mc_gpregs.gp_sp;
259         tf->tf_gp = mcp->mc_gpregs.gp_gp;
260         tf->tf_sepc = mcp->mc_gpregs.gp_sepc;
261         tf->tf_sstatus = mcp->mc_gpregs.gp_sstatus;
262         set_fpcontext(td, mcp);
263
264         return (0);
265 }
266
267 static void
268 get_fpcontext(struct thread *td, mcontext_t *mcp)
269 {
270         struct pcb *curpcb;
271
272         critical_enter();
273
274         curpcb = curthread->td_pcb;
275
276         KASSERT(td->td_pcb == curpcb, ("Invalid fpe pcb"));
277
278         if ((curpcb->pcb_fpflags & PCB_FP_STARTED) != 0) {
279                 /*
280                  * If we have just been running FPE instructions we will
281                  * need to save the state to memcpy it below.
282                  */
283                 fpe_state_save(td);
284
285                 KASSERT((curpcb->pcb_fpflags & ~PCB_FP_USERMASK) == 0,
286                     ("Non-userspace FPE flags set in get_fpcontext"));
287                 memcpy(mcp->mc_fpregs.fp_x, curpcb->pcb_x,
288                     sizeof(mcp->mc_fpregs.fp_x));
289                 mcp->mc_fpregs.fp_fcsr = curpcb->pcb_fcsr;
290                 mcp->mc_fpregs.fp_flags = curpcb->pcb_fpflags;
291                 mcp->mc_flags |= _MC_FP_VALID;
292         }
293
294         critical_exit();
295 }
296
297 static void
298 set_fpcontext(struct thread *td, mcontext_t *mcp)
299 {
300         struct pcb *curpcb;
301
302         td->td_frame->tf_sstatus &= ~SSTATUS_FS_MASK;
303         td->td_frame->tf_sstatus |= SSTATUS_FS_OFF;
304
305         critical_enter();
306
307         if ((mcp->mc_flags & _MC_FP_VALID) != 0) {
308                 curpcb = curthread->td_pcb;
309                 /* FPE usage is enabled, override registers. */
310                 memcpy(curpcb->pcb_x, mcp->mc_fpregs.fp_x,
311                     sizeof(mcp->mc_fpregs.fp_x));
312                 curpcb->pcb_fcsr = mcp->mc_fpregs.fp_fcsr;
313                 curpcb->pcb_fpflags = mcp->mc_fpregs.fp_flags & PCB_FP_USERMASK;
314                 td->td_frame->tf_sstatus |= SSTATUS_FS_CLEAN;
315         }
316
317         critical_exit();
318 }
319
320 int
321 sys_sigreturn(struct thread *td, struct sigreturn_args *uap)
322 {
323         ucontext_t uc;
324         int error;
325
326         if (copyin(uap->sigcntxp, &uc, sizeof(uc)))
327                 return (EFAULT);
328
329         error = set_mcontext(td, &uc.uc_mcontext);
330         if (error != 0)
331                 return (error);
332
333         /* Restore signal mask. */
334         kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask, NULL, 0);
335
336         return (EJUSTRETURN);
337 }
338
339 void
340 sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
341 {
342         struct sigframe *fp, frame;
343         struct sysentvec *sysent;
344         struct trapframe *tf;
345         struct sigacts *psp;
346         struct thread *td;
347         struct proc *p;
348         int onstack;
349         int sig;
350
351         td = curthread;
352         p = td->td_proc;
353         PROC_LOCK_ASSERT(p, MA_OWNED);
354
355         sig = ksi->ksi_signo;
356         psp = p->p_sigacts;
357         mtx_assert(&psp->ps_mtx, MA_OWNED);
358
359         tf = td->td_frame;
360         onstack = sigonstack(tf->tf_sp);
361
362         CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm,
363             catcher, sig);
364
365         /* Allocate and validate space for the signal handler context. */
366         if ((td->td_pflags & TDP_ALTSTACK) != 0 && !onstack &&
367             SIGISMEMBER(psp->ps_sigonstack, sig)) {
368                 fp = (struct sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
369                     td->td_sigstk.ss_size);
370         } else {
371                 fp = (struct sigframe *)td->td_frame->tf_sp;
372         }
373
374         /* Make room, keeping the stack aligned */
375         fp--;
376         fp = (struct sigframe *)STACKALIGN(fp);
377
378         /* Fill in the frame to copy out */
379         bzero(&frame, sizeof(frame));
380         get_mcontext(td, &frame.sf_uc.uc_mcontext, 0);
381         frame.sf_si = ksi->ksi_info;
382         frame.sf_uc.uc_sigmask = *mask;
383         frame.sf_uc.uc_stack = td->td_sigstk;
384         frame.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK) != 0 ?
385             (onstack ? SS_ONSTACK : 0) : SS_DISABLE;
386         mtx_unlock(&psp->ps_mtx);
387         PROC_UNLOCK(td->td_proc);
388
389         /* Copy the sigframe out to the user's stack. */
390         if (copyout(&frame, fp, sizeof(*fp)) != 0) {
391                 /* Process has trashed its stack. Kill it. */
392                 CTR2(KTR_SIG, "sendsig: sigexit td=%p fp=%p", td, fp);
393                 PROC_LOCK(p);
394                 sigexit(td, SIGILL);
395         }
396
397         tf->tf_a[0] = sig;
398         tf->tf_a[1] = (register_t)&fp->sf_si;
399         tf->tf_a[2] = (register_t)&fp->sf_uc;
400
401         tf->tf_sepc = (register_t)catcher;
402         tf->tf_sp = (register_t)fp;
403
404         sysent = p->p_sysent;
405         if (PROC_HAS_SHP(p))
406                 tf->tf_ra = (register_t)PROC_SIGCODE(p);
407         else
408                 tf->tf_ra = (register_t)(PROC_PS_STRINGS(p) -
409                     *(sysent->sv_szsigcode));
410
411         CTR3(KTR_SIG, "sendsig: return td=%p pc=%#x sp=%#x", td, tf->tf_sepc,
412             tf->tf_sp);
413
414         PROC_LOCK(p);
415         mtx_lock(&psp->ps_mtx);
416 }