]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/riscv/riscv/exec_machdep.c
ddb: print the actual syscall name
[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/kdb.h>
62 #include <machine/pcb.h>
63 #include <machine/pte.h>
64 #include <machine/riscvreg.h>
65 #include <machine/sbi.h>
66 #include <machine/trap.h>
67
68 #include <vm/vm.h>
69 #include <vm/vm_param.h>
70 #include <vm/pmap.h>
71 #include <vm/vm_map.h>
72
73 #ifdef FPE
74 #include <machine/fpe.h>
75 #endif
76
77 static void get_fpcontext(struct thread *td, mcontext_t *mcp);
78 static void set_fpcontext(struct thread *td, mcontext_t *mcp);
79
80 int
81 fill_regs(struct thread *td, struct reg *regs)
82 {
83         struct trapframe *frame;
84
85         frame = td->td_frame;
86         regs->sepc = frame->tf_sepc;
87         regs->sstatus = frame->tf_sstatus;
88         regs->ra = frame->tf_ra;
89         regs->sp = frame->tf_sp;
90         regs->gp = frame->tf_gp;
91         regs->tp = frame->tf_tp;
92
93         memcpy(regs->t, frame->tf_t, sizeof(regs->t));
94         memcpy(regs->s, frame->tf_s, sizeof(regs->s));
95         memcpy(regs->a, frame->tf_a, sizeof(regs->a));
96
97         return (0);
98 }
99
100 int
101 set_regs(struct thread *td, struct reg *regs)
102 {
103         struct trapframe *frame;
104
105         frame = td->td_frame;
106         frame->tf_sepc = regs->sepc;
107         frame->tf_ra = regs->ra;
108         frame->tf_sp = regs->sp;
109         frame->tf_gp = regs->gp;
110         frame->tf_tp = regs->tp;
111
112         memcpy(frame->tf_t, regs->t, sizeof(frame->tf_t));
113         memcpy(frame->tf_s, regs->s, sizeof(frame->tf_s));
114         memcpy(frame->tf_a, regs->a, sizeof(frame->tf_a));
115
116         return (0);
117 }
118
119 int
120 fill_fpregs(struct thread *td, struct fpreg *regs)
121 {
122 #ifdef FPE
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 #endif
139                 memset(regs, 0, sizeof(*regs));
140
141         return (0);
142 }
143
144 int
145 set_fpregs(struct thread *td, struct fpreg *regs)
146 {
147 #ifdef FPE
148         struct trapframe *frame;
149         struct pcb *pcb;
150
151         frame = td->td_frame;
152         pcb = td->td_pcb;
153
154         memcpy(pcb->pcb_x, regs->fp_x, sizeof(regs->fp_x));
155         pcb->pcb_fcsr = regs->fp_fcsr;
156         pcb->pcb_fpflags |= PCB_FP_STARTED;
157         frame->tf_sstatus &= ~SSTATUS_FS_MASK;
158         frame->tf_sstatus |= SSTATUS_FS_CLEAN;
159 #endif
160
161         return (0);
162 }
163
164 int
165 fill_dbregs(struct thread *td, struct dbreg *regs)
166 {
167
168         panic("fill_dbregs");
169 }
170
171 int
172 set_dbregs(struct thread *td, struct dbreg *regs)
173 {
174
175         panic("set_dbregs");
176 }
177
178 void
179 exec_setregs(struct thread *td, struct image_params *imgp, uintptr_t stack)
180 {
181         struct trapframe *tf;
182         struct pcb *pcb;
183
184         tf = td->td_frame;
185         pcb = td->td_pcb;
186
187         memset(tf, 0, sizeof(struct trapframe));
188
189         tf->tf_a[0] = stack;
190         tf->tf_sp = STACKALIGN(stack);
191         tf->tf_ra = imgp->entry_addr;
192         tf->tf_sepc = imgp->entry_addr;
193
194         pcb->pcb_fpflags &= ~PCB_FP_STARTED;
195 }
196
197 /* Sanity check these are the same size, they will be memcpy'd to and from */
198 CTASSERT(sizeof(((struct trapframe *)0)->tf_a) ==
199     sizeof((struct gpregs *)0)->gp_a);
200 CTASSERT(sizeof(((struct trapframe *)0)->tf_s) ==
201     sizeof((struct gpregs *)0)->gp_s);
202 CTASSERT(sizeof(((struct trapframe *)0)->tf_t) ==
203     sizeof((struct gpregs *)0)->gp_t);
204 CTASSERT(sizeof(((struct trapframe *)0)->tf_a) ==
205     sizeof((struct reg *)0)->a);
206 CTASSERT(sizeof(((struct trapframe *)0)->tf_s) ==
207     sizeof((struct reg *)0)->s);
208 CTASSERT(sizeof(((struct trapframe *)0)->tf_t) ==
209     sizeof((struct reg *)0)->t);
210
211 int
212 get_mcontext(struct thread *td, mcontext_t *mcp, int clear_ret)
213 {
214         struct trapframe *tf = td->td_frame;
215
216         memcpy(mcp->mc_gpregs.gp_t, tf->tf_t, sizeof(mcp->mc_gpregs.gp_t));
217         memcpy(mcp->mc_gpregs.gp_s, tf->tf_s, sizeof(mcp->mc_gpregs.gp_s));
218         memcpy(mcp->mc_gpregs.gp_a, tf->tf_a, sizeof(mcp->mc_gpregs.gp_a));
219
220         if (clear_ret & GET_MC_CLEAR_RET) {
221                 mcp->mc_gpregs.gp_a[0] = 0;
222                 mcp->mc_gpregs.gp_t[0] = 0; /* clear syscall error */
223         }
224
225         mcp->mc_gpregs.gp_ra = tf->tf_ra;
226         mcp->mc_gpregs.gp_sp = tf->tf_sp;
227         mcp->mc_gpregs.gp_gp = tf->tf_gp;
228         mcp->mc_gpregs.gp_tp = tf->tf_tp;
229         mcp->mc_gpregs.gp_sepc = tf->tf_sepc;
230         mcp->mc_gpregs.gp_sstatus = tf->tf_sstatus;
231         get_fpcontext(td, mcp);
232
233         return (0);
234 }
235
236 int
237 set_mcontext(struct thread *td, mcontext_t *mcp)
238 {
239         struct trapframe *tf;
240
241         tf = td->td_frame;
242
243         /*
244          * Permit changes to the USTATUS bits of SSTATUS.
245          *
246          * Ignore writes to read-only bits (SD, XS).
247          *
248          * Ignore writes to the FS field as set_fpcontext() will set
249          * it explicitly.
250          */
251         if (((mcp->mc_gpregs.gp_sstatus ^ tf->tf_sstatus) &
252             ~(SSTATUS_SD | SSTATUS_XS_MASK | SSTATUS_FS_MASK | SSTATUS_UPIE |
253             SSTATUS_UIE)) != 0)
254                 return (EINVAL);
255
256         memcpy(tf->tf_t, mcp->mc_gpregs.gp_t, sizeof(tf->tf_t));
257         memcpy(tf->tf_s, mcp->mc_gpregs.gp_s, sizeof(tf->tf_s));
258         memcpy(tf->tf_a, mcp->mc_gpregs.gp_a, sizeof(tf->tf_a));
259
260         tf->tf_ra = mcp->mc_gpregs.gp_ra;
261         tf->tf_sp = mcp->mc_gpregs.gp_sp;
262         tf->tf_gp = mcp->mc_gpregs.gp_gp;
263         tf->tf_sepc = mcp->mc_gpregs.gp_sepc;
264         tf->tf_sstatus = mcp->mc_gpregs.gp_sstatus;
265         set_fpcontext(td, mcp);
266
267         return (0);
268 }
269
270 static void
271 get_fpcontext(struct thread *td, mcontext_t *mcp)
272 {
273 #ifdef FPE
274         struct pcb *curpcb;
275
276         critical_enter();
277
278         curpcb = curthread->td_pcb;
279
280         KASSERT(td->td_pcb == curpcb, ("Invalid fpe pcb"));
281
282         if ((curpcb->pcb_fpflags & PCB_FP_STARTED) != 0) {
283                 /*
284                  * If we have just been running FPE instructions we will
285                  * need to save the state to memcpy it below.
286                  */
287                 fpe_state_save(td);
288
289                 KASSERT((curpcb->pcb_fpflags & ~PCB_FP_USERMASK) == 0,
290                     ("Non-userspace FPE flags set in get_fpcontext"));
291                 memcpy(mcp->mc_fpregs.fp_x, curpcb->pcb_x,
292                     sizeof(mcp->mc_fpregs.fp_x));
293                 mcp->mc_fpregs.fp_fcsr = curpcb->pcb_fcsr;
294                 mcp->mc_fpregs.fp_flags = curpcb->pcb_fpflags;
295                 mcp->mc_flags |= _MC_FP_VALID;
296         }
297
298         critical_exit();
299 #endif
300 }
301
302 static void
303 set_fpcontext(struct thread *td, mcontext_t *mcp)
304 {
305 #ifdef FPE
306         struct pcb *curpcb;
307 #endif
308
309         td->td_frame->tf_sstatus &= ~SSTATUS_FS_MASK;
310         td->td_frame->tf_sstatus |= SSTATUS_FS_OFF;
311
312 #ifdef FPE
313         critical_enter();
314
315         if ((mcp->mc_flags & _MC_FP_VALID) != 0) {
316                 curpcb = curthread->td_pcb;
317                 /* FPE usage is enabled, override registers. */
318                 memcpy(curpcb->pcb_x, mcp->mc_fpregs.fp_x,
319                     sizeof(mcp->mc_fpregs.fp_x));
320                 curpcb->pcb_fcsr = mcp->mc_fpregs.fp_fcsr;
321                 curpcb->pcb_fpflags = mcp->mc_fpregs.fp_flags & PCB_FP_USERMASK;
322                 td->td_frame->tf_sstatus |= SSTATUS_FS_CLEAN;
323         }
324
325         critical_exit();
326 #endif
327 }
328
329 int
330 sys_sigreturn(struct thread *td, struct sigreturn_args *uap)
331 {
332         ucontext_t uc;
333         int error;
334
335         if (copyin(uap->sigcntxp, &uc, sizeof(uc)))
336                 return (EFAULT);
337
338         error = set_mcontext(td, &uc.uc_mcontext);
339         if (error != 0)
340                 return (error);
341
342         /* Restore signal mask. */
343         kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask, NULL, 0);
344
345         return (EJUSTRETURN);
346 }
347
348 void
349 sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
350 {
351         struct sigframe *fp, frame;
352         struct sysentvec *sysent;
353         struct trapframe *tf;
354         struct sigacts *psp;
355         struct thread *td;
356         struct proc *p;
357         int onstack;
358         int sig;
359
360         td = curthread;
361         p = td->td_proc;
362         PROC_LOCK_ASSERT(p, MA_OWNED);
363
364         sig = ksi->ksi_signo;
365         psp = p->p_sigacts;
366         mtx_assert(&psp->ps_mtx, MA_OWNED);
367
368         tf = td->td_frame;
369         onstack = sigonstack(tf->tf_sp);
370
371         CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm,
372             catcher, sig);
373
374         /* Allocate and validate space for the signal handler context. */
375         if ((td->td_pflags & TDP_ALTSTACK) != 0 && !onstack &&
376             SIGISMEMBER(psp->ps_sigonstack, sig)) {
377                 fp = (struct sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
378                     td->td_sigstk.ss_size);
379         } else {
380                 fp = (struct sigframe *)td->td_frame->tf_sp;
381         }
382
383         /* Make room, keeping the stack aligned */
384         fp--;
385         fp = (struct sigframe *)STACKALIGN(fp);
386
387         /* Fill in the frame to copy out */
388         bzero(&frame, sizeof(frame));
389         get_mcontext(td, &frame.sf_uc.uc_mcontext, 0);
390         frame.sf_si = ksi->ksi_info;
391         frame.sf_uc.uc_sigmask = *mask;
392         frame.sf_uc.uc_stack = td->td_sigstk;
393         frame.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK) != 0 ?
394             (onstack ? SS_ONSTACK : 0) : SS_DISABLE;
395         mtx_unlock(&psp->ps_mtx);
396         PROC_UNLOCK(td->td_proc);
397
398         /* Copy the sigframe out to the user's stack. */
399         if (copyout(&frame, fp, sizeof(*fp)) != 0) {
400                 /* Process has trashed its stack. Kill it. */
401                 CTR2(KTR_SIG, "sendsig: sigexit td=%p fp=%p", td, fp);
402                 PROC_LOCK(p);
403                 sigexit(td, SIGILL);
404         }
405
406         tf->tf_a[0] = sig;
407         tf->tf_a[1] = (register_t)&fp->sf_si;
408         tf->tf_a[2] = (register_t)&fp->sf_uc;
409
410         tf->tf_sepc = (register_t)catcher;
411         tf->tf_sp = (register_t)fp;
412
413         sysent = p->p_sysent;
414         if (sysent->sv_sigcode_base != 0)
415                 tf->tf_ra = (register_t)sysent->sv_sigcode_base;
416         else
417                 tf->tf_ra = (register_t)(PROC_PS_STRINGS(p) -
418                     *(sysent->sv_szsigcode));
419
420         CTR3(KTR_SIG, "sendsig: return td=%p pc=%#x sp=%#x", td, tf->tf_sepc,
421             tf->tf_sp);
422
423         PROC_LOCK(p);
424         mtx_lock(&psp->ps_mtx);
425 }