]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/mips/mips/pm_machdep.c
MFV r307315:
[FreeBSD/FreeBSD.git] / sys / mips / mips / pm_machdep.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1992 Terrence R. Lambert.
5  * Copyright (c) 1982, 1987, 1990 The Regents of the University of California.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * William Jolitz.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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  *      from: @(#)machdep.c     7.4 (Berkeley) 6/3/91
36  *      from: src/sys/i386/i386/machdep.c,v 1.385.2.3 2000/05/10 02:04:46 obrien
37  *      JNPR: pm_machdep.c,v 1.9.2.1 2007/08/16 15:59:10 girish
38  */
39
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
42
43 #include "opt_compat.h"
44
45 #include <sys/types.h>
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/sysent.h>
49 #include <sys/proc.h>
50 #include <sys/signalvar.h>
51 #include <sys/exec.h>
52 #include <sys/imgact.h>
53 #include <sys/ucontext.h>
54 #include <sys/lock.h>
55 #include <sys/syscallsubr.h>
56 #include <sys/sysproto.h>
57 #include <sys/ptrace.h>
58 #include <sys/syslog.h>
59 #include <vm/vm.h>
60 #include <vm/pmap.h>
61 #include <vm/vm_map.h>
62 #include <vm/vm_extern.h>
63 #include <sys/user.h>
64 #include <sys/uio.h>
65 #include <machine/abi.h>
66 #include <machine/cpuinfo.h>
67 #include <machine/reg.h>
68 #include <machine/md_var.h>
69 #include <machine/sigframe.h>
70 #include <machine/tls.h>
71 #include <machine/vmparam.h>
72 #include <sys/vnode.h>
73 #include <fs/pseudofs/pseudofs.h>
74 #include <fs/procfs/procfs.h>
75
76 #define UCONTEXT_MAGIC  0xACEDBADE
77
78 /*
79  * Send an interrupt to process.
80  *
81  * Stack is set up to allow sigcode stored
82  * at top to call routine, followed by kcall
83  * to sigreturn routine below.  After sigreturn
84  * resets the signal mask, the stack, and the
85  * frame pointer, it returns to the user
86  * specified pc, psl.
87  */
88 void
89 sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
90 {
91         struct proc *p;
92         struct thread *td;
93         struct trapframe *regs;
94         struct sigacts *psp;
95         struct sigframe sf, *sfp;
96         int sig;
97         int oonstack;
98
99         td = curthread;
100         p = td->td_proc;
101         PROC_LOCK_ASSERT(p, MA_OWNED);
102         sig = ksi->ksi_signo;
103         psp = p->p_sigacts;
104         mtx_assert(&psp->ps_mtx, MA_OWNED);
105
106         regs = td->td_frame;
107         oonstack = sigonstack(regs->sp);
108
109         /* save user context */
110         bzero(&sf, sizeof(struct sigframe));
111         sf.sf_uc.uc_sigmask = *mask;
112         sf.sf_uc.uc_stack = td->td_sigstk;
113         sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
114         sf.sf_uc.uc_mcontext.mc_pc = regs->pc;
115         sf.sf_uc.uc_mcontext.mullo = regs->mullo;
116         sf.sf_uc.uc_mcontext.mulhi = regs->mulhi;
117         sf.sf_uc.uc_mcontext.mc_tls = td->td_md.md_tls;
118         sf.sf_uc.uc_mcontext.mc_regs[0] = UCONTEXT_MAGIC;  /* magic number */
119         bcopy((void *)&regs->ast, (void *)&sf.sf_uc.uc_mcontext.mc_regs[1],
120             sizeof(sf.sf_uc.uc_mcontext.mc_regs) - sizeof(register_t));
121         sf.sf_uc.uc_mcontext.mc_fpused = td->td_md.md_flags & MDTD_FPUSED;
122         if (sf.sf_uc.uc_mcontext.mc_fpused) {
123                 /* if FPU has current state, save it first */
124                 if (td == PCPU_GET(fpcurthread))
125                         MipsSaveCurFPState(td);
126                 bcopy((void *)&td->td_frame->f0,
127                     (void *)sf.sf_uc.uc_mcontext.mc_fpregs,
128                     sizeof(sf.sf_uc.uc_mcontext.mc_fpregs));
129         }
130
131         /* Allocate and validate space for the signal handler context. */
132         if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
133             SIGISMEMBER(psp->ps_sigonstack, sig)) {
134                 sfp = (struct sigframe *)(((uintptr_t)td->td_sigstk.ss_sp +
135                     td->td_sigstk.ss_size - sizeof(struct sigframe))
136                     & ~(STACK_ALIGN - 1));
137         } else
138                 sfp = (struct sigframe *)((vm_offset_t)(regs->sp - 
139                     sizeof(struct sigframe)) & ~(STACK_ALIGN - 1));
140
141         /* Build the argument list for the signal handler. */
142         regs->a0 = sig;
143         regs->a2 = (register_t)(intptr_t)&sfp->sf_uc;
144         if (SIGISMEMBER(psp->ps_siginfo, sig)) {
145                 /* Signal handler installed with SA_SIGINFO. */
146                 regs->a1 = (register_t)(intptr_t)&sfp->sf_si;
147                 /* sf.sf_ahu.sf_action = (__siginfohandler_t *)catcher; */
148
149                 /* fill siginfo structure */
150                 sf.sf_si = ksi->ksi_info;
151                 sf.sf_si.si_signo = sig;
152                 sf.sf_si.si_code = ksi->ksi_code;
153                 sf.sf_si.si_addr = (void*)(intptr_t)regs->badvaddr;
154         } else {
155                 /* Old FreeBSD-style arguments. */
156                 regs->a1 = ksi->ksi_code;
157                 regs->a3 = regs->badvaddr;
158                 /* sf.sf_ahu.sf_handler = catcher; */
159         }
160
161         mtx_unlock(&psp->ps_mtx);
162         PROC_UNLOCK(p);
163
164         /*
165          * Copy the sigframe out to the user's stack.
166          */
167         if (copyout(&sf, sfp, sizeof(struct sigframe)) != 0) {
168                 /*
169                  * Something is wrong with the stack pointer.
170                  * ...Kill the process.
171                  */
172                 PROC_LOCK(p);
173                 sigexit(td, SIGILL);
174         }
175
176         regs->pc = (register_t)(intptr_t)catcher;
177         regs->t9 = (register_t)(intptr_t)catcher;
178         regs->sp = (register_t)(intptr_t)sfp;
179         /*
180          * Signal trampoline code is at base of user stack.
181          */
182         regs->ra = (register_t)(intptr_t)PS_STRINGS - *(p->p_sysent->sv_szsigcode);
183         PROC_LOCK(p);
184         mtx_lock(&psp->ps_mtx);
185 }
186
187 /*
188  * System call to cleanup state after a signal
189  * has been taken.  Reset signal mask and
190  * stack state from context left by sendsig (above).
191  * Return to previous pc as specified by
192  * context left by sendsig.
193  */
194 int
195 sys_sigreturn(struct thread *td, struct sigreturn_args *uap)
196 {
197         ucontext_t uc;
198         int error;
199
200         error = copyin(uap->sigcntxp, &uc, sizeof(uc));
201         if (error != 0)
202             return (error);
203
204         error = set_mcontext(td, &uc.uc_mcontext);
205         if (error != 0)
206                 return (error);
207
208         kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask, NULL, 0);
209
210         return (EJUSTRETURN);
211 }
212
213 int
214 ptrace_set_pc(struct thread *td, unsigned long addr)
215 {
216         td->td_frame->pc = (register_t) addr;
217         return 0;
218 }
219
220 static int
221 ptrace_read_int(struct thread *td, off_t addr, int *v)
222 {
223
224         if (proc_readmem(td, td->td_proc, addr, v, sizeof(*v)) != sizeof(*v))
225                 return (ENOMEM);
226         return (0);
227 }
228
229 static int
230 ptrace_write_int(struct thread *td, off_t addr, int v)
231 {
232
233         if (proc_writemem(td, td->td_proc, addr, &v, sizeof(v)) != sizeof(v))
234                 return (ENOMEM);
235         return (0);
236 }
237
238 int
239 ptrace_single_step(struct thread *td)
240 {
241         unsigned va;
242         struct trapframe *locr0 = td->td_frame;
243         int i;
244         int bpinstr = MIPS_BREAK_SSTEP;
245         int curinstr;
246         struct proc *p;
247
248         p = td->td_proc;
249         PROC_UNLOCK(p);
250         /*
251          * Fetch what's at the current location.
252          */
253         ptrace_read_int(td,  (off_t)locr0->pc, &curinstr);
254
255         /* compute next address after current location */
256         if(curinstr != 0) {
257                 va = MipsEmulateBranch(locr0, locr0->pc, locr0->fsr,
258                     (uintptr_t)&curinstr);
259         } else {
260                 va = locr0->pc + 4;
261         }
262         if (td->td_md.md_ss_addr) {
263                 printf("SS %s (%d): breakpoint already set at %x (va %x)\n",
264                     p->p_comm, p->p_pid, td->td_md.md_ss_addr, va); /* XXX */
265                 return (EFAULT);
266         }
267         td->td_md.md_ss_addr = va;
268         /*
269          * Fetch what's at the current location.
270          */
271         ptrace_read_int(td, (off_t)va, &td->td_md.md_ss_instr);
272
273         /*
274          * Store breakpoint instruction at the "next" location now.
275          */
276         i = ptrace_write_int (td, va, bpinstr);
277
278         /*
279          * The sync'ing of I & D caches is done by procfs_domem()
280          * through procfs_rwmem().
281          */
282
283         PROC_LOCK(p);
284         if (i < 0)
285                 return (EFAULT);
286 #if 0
287         printf("SS %s (%d): breakpoint set at %x: %x (pc %x) br %x\n",
288             p->p_comm, p->p_pid, p->p_md.md_ss_addr,
289             p->p_md.md_ss_instr, locr0->pc, curinstr); /* XXX */
290 #endif
291         return (0);
292 }
293
294
295 void
296 makectx(struct trapframe *tf, struct pcb *pcb)
297 {
298
299         pcb->pcb_context[PCB_REG_RA] = tf->ra;
300         pcb->pcb_context[PCB_REG_PC] = tf->pc;
301         pcb->pcb_context[PCB_REG_SP] = tf->sp;
302 }
303
304 int
305 fill_regs(struct thread *td, struct reg *regs)
306 {
307         memcpy(regs, td->td_frame, sizeof(struct reg));
308         return (0);
309 }
310
311 int
312 set_regs(struct thread *td, struct reg *regs)
313 {
314         struct trapframe *f;
315         register_t sr;
316
317         f = (struct trapframe *) td->td_frame;
318         /*
319          * Don't allow the user to change SR
320          */
321         sr = f->sr;
322         memcpy(td->td_frame, regs, sizeof(struct reg));
323         f->sr = sr;
324         return (0);
325 }
326
327 int
328 get_mcontext(struct thread *td, mcontext_t *mcp, int flags)
329 {
330         struct trapframe *tp;
331
332         tp = td->td_frame;
333         PROC_LOCK(curthread->td_proc);
334         mcp->mc_onstack = sigonstack(tp->sp);
335         PROC_UNLOCK(curthread->td_proc);
336         bcopy((void *)&td->td_frame->zero, (void *)&mcp->mc_regs,
337             sizeof(mcp->mc_regs));
338
339         mcp->mc_fpused = td->td_md.md_flags & MDTD_FPUSED;
340         if (mcp->mc_fpused) {
341                 bcopy((void *)&td->td_frame->f0, (void *)&mcp->mc_fpregs,
342                     sizeof(mcp->mc_fpregs));
343         }
344         if (flags & GET_MC_CLEAR_RET) {
345                 mcp->mc_regs[V0] = 0;
346                 mcp->mc_regs[V1] = 0;
347                 mcp->mc_regs[A3] = 0;
348         }
349
350         mcp->mc_pc = td->td_frame->pc;
351         mcp->mullo = td->td_frame->mullo;
352         mcp->mulhi = td->td_frame->mulhi;
353         mcp->mc_tls = td->td_md.md_tls;
354         return (0);
355 }
356
357 int
358 set_mcontext(struct thread *td, mcontext_t *mcp)
359 {
360         struct trapframe *tp;
361
362         tp = td->td_frame;
363         bcopy((void *)&mcp->mc_regs, (void *)&td->td_frame->zero,
364             sizeof(mcp->mc_regs));
365
366         td->td_md.md_flags = mcp->mc_fpused & MDTD_FPUSED;
367         if (mcp->mc_fpused) {
368                 bcopy((void *)&mcp->mc_fpregs, (void *)&td->td_frame->f0,
369                     sizeof(mcp->mc_fpregs));
370         }
371         td->td_frame->pc = mcp->mc_pc;
372         td->td_frame->mullo = mcp->mullo;
373         td->td_frame->mulhi = mcp->mulhi;
374         td->td_md.md_tls = mcp->mc_tls;
375         /* Dont let user to set any bits in status and cause registers. */
376
377         return (0);
378 }
379
380 int
381 fill_fpregs(struct thread *td, struct fpreg *fpregs)
382 {
383         if (td == PCPU_GET(fpcurthread))
384                 MipsSaveCurFPState(td);
385         memcpy(fpregs, &td->td_frame->f0, sizeof(struct fpreg));
386         fpregs->r_regs[FIR_NUM] = cpuinfo.fpu_id;
387         return 0;
388 }
389
390 int
391 set_fpregs(struct thread *td, struct fpreg *fpregs)
392 {
393         if (PCPU_GET(fpcurthread) == td)
394                 PCPU_SET(fpcurthread, (struct thread *)0);
395         memcpy(&td->td_frame->f0, fpregs, sizeof(struct fpreg));
396         return 0;
397 }
398
399
400 /*
401  * Clear registers on exec
402  * $sp is set to the stack pointer passed in.  $pc is set to the entry
403  * point given by the exec_package passed in, as is $t9 (used for PIC
404  * code by the MIPS elf abi).
405  */
406 void
407 exec_setregs(struct thread *td, struct image_params *imgp, u_long stack)
408 {
409
410         bzero((caddr_t)td->td_frame, sizeof(struct trapframe));
411
412         td->td_frame->sp = ((register_t)stack) & ~(STACK_ALIGN - 1);
413
414         /*
415          * If we're running o32 or n32 programs but have 64-bit registers,
416          * GCC may use stack-relative addressing near the top of user
417          * address space that, due to sign extension, will yield an
418          * invalid address.  For instance, if sp is 0x7fffff00 then GCC
419          * might do something like this to load a word from 0x7ffffff0:
420          *
421          *      addu    sp, sp, 32768
422          *      lw      t0, -32528(sp)
423          *
424          * On systems with 64-bit registers, sp is sign-extended to
425          * 0xffffffff80007f00 and the load is instead done from
426          * 0xffffffff7ffffff0.
427          *
428          * To prevent this, we subtract 64K from the stack pointer here
429          * for processes with 32-bit pointers.
430          */
431 #if defined(__mips_n32) || defined(__mips_n64)
432         if (!SV_PROC_FLAG(td->td_proc, SV_LP64))
433                 td->td_frame->sp -= 65536;
434 #endif
435
436         td->td_frame->pc = imgp->entry_addr & ~3;
437         td->td_frame->t9 = imgp->entry_addr & ~3; /* abicall req */
438         td->td_frame->sr = MIPS_SR_KSU_USER | MIPS_SR_EXL | MIPS_SR_INT_IE |
439             (mips_rd_status() & MIPS_SR_INT_MASK);
440 #if defined(__mips_n32) 
441         td->td_frame->sr |= MIPS_SR_PX;
442 #elif  defined(__mips_n64)
443         td->td_frame->sr |= MIPS_SR_PX | MIPS_SR_UX | MIPS_SR_KX;
444 #endif
445         /*
446          * FREEBSD_DEVELOPERS_FIXME:
447          * Setup any other CPU-Specific registers (Not MIPS Standard)
448          * and/or bits in other standard MIPS registers (if CPU-Specific)
449          *  that are needed.
450          */
451
452         /*
453          * Set up arguments for the rtld-capable crt0:
454          *      a0      stack pointer
455          *      a1      rtld cleanup (filled in by dynamic loader)
456          *      a2      rtld object (filled in by dynamic loader)
457          *      a3      ps_strings
458          */
459         td->td_frame->a0 = (register_t) stack;
460         td->td_frame->a1 = 0;
461         td->td_frame->a2 = 0;
462         td->td_frame->a3 = (register_t)imgp->ps_strings;
463
464         td->td_md.md_flags &= ~MDTD_FPUSED;
465         if (PCPU_GET(fpcurthread) == td)
466             PCPU_SET(fpcurthread, (struct thread *)0);
467         td->td_md.md_ss_addr = 0;
468
469         td->td_md.md_tls_tcb_offset = TLS_TP_OFFSET + TLS_TCB_SIZE;
470 }
471
472 int
473 ptrace_clear_single_step(struct thread *td)
474 {
475         int i;
476         struct proc *p;
477
478         p = td->td_proc;
479         PROC_LOCK_ASSERT(p, MA_OWNED);
480         if (!td->td_md.md_ss_addr)
481                 return EINVAL;
482
483         /*
484          * Restore original instruction and clear BP
485          */
486         i = ptrace_write_int (td, td->td_md.md_ss_addr, td->td_md.md_ss_instr);
487
488         /* The sync'ing of I & D caches is done by procfs_domem(). */
489
490         if (i < 0) {
491                 log(LOG_ERR, "SS %s %d: can't restore instruction at %x: %x\n",
492                     p->p_comm, p->p_pid, td->td_md.md_ss_addr,
493                     td->td_md.md_ss_instr);
494         }
495         td->td_md.md_ss_addr = 0;
496         return 0;
497 }