2 * Copyright (c) 1992 Terrence R. Lambert.
3 * Copyright (c) 1982, 1987, 1990 The Regents of the University of California.
6 * This code is derived from software contributed to Berkeley by
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * from: @(#)machdep.c 7.4 (Berkeley) 6/3/91
34 * from: src/sys/i386/i386/machdep.c,v 1.385.2.3 2000/05/10 02:04:46 obrien
35 * JNPR: pm_machdep.c,v 1.9.2.1 2007/08/16 15:59:10 girish
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
41 #include "opt_compat.h"
43 #include <sys/types.h>
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/sysent.h>
48 #include <sys/signalvar.h>
50 #include <sys/imgact.h>
51 #include <sys/ucontext.h>
53 #include <sys/syscallsubr.h>
54 #include <sys/sysproto.h>
55 #include <sys/ptrace.h>
56 #include <sys/syslog.h>
59 #include <vm/vm_map.h>
60 #include <vm/vm_extern.h>
63 #include <machine/reg.h>
64 #include <machine/md_var.h>
65 #include <machine/sigframe.h>
66 #include <machine/vmparam.h>
67 #include <sys/vnode.h>
68 #include <fs/pseudofs/pseudofs.h>
69 #include <fs/procfs/procfs.h>
71 #define UCONTEXT_MAGIC 0xACEDBADE
74 * Send an interrupt to process.
76 * Stack is set up to allow sigcode stored
77 * at top to call routine, followed by kcall
78 * to sigreturn routine below. After sigreturn
79 * resets the signal mask, the stack, and the
80 * frame pointer, it returns to the user
84 sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
88 struct trapframe *regs;
90 struct sigframe sf, *sfp;
96 PROC_LOCK_ASSERT(p, MA_OWNED);
99 mtx_assert(&psp->ps_mtx, MA_OWNED);
102 oonstack = sigonstack(regs->sp);
104 /* save user context */
105 bzero(&sf, sizeof(struct sigframe));
106 sf.sf_uc.uc_sigmask = *mask;
107 sf.sf_uc.uc_stack = td->td_sigstk;
108 sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
109 sf.sf_uc.uc_mcontext.mc_pc = regs->pc;
110 sf.sf_uc.uc_mcontext.mullo = regs->mullo;
111 sf.sf_uc.uc_mcontext.mulhi = regs->mulhi;
112 sf.sf_uc.uc_mcontext.mc_tls = td->td_md.md_tls;
113 sf.sf_uc.uc_mcontext.mc_regs[0] = UCONTEXT_MAGIC; /* magic number */
114 bcopy((void *)®s->ast, (void *)&sf.sf_uc.uc_mcontext.mc_regs[1],
115 sizeof(sf.sf_uc.uc_mcontext.mc_regs) - sizeof(register_t));
116 sf.sf_uc.uc_mcontext.mc_fpused = td->td_md.md_flags & MDTD_FPUSED;
117 if (sf.sf_uc.uc_mcontext.mc_fpused) {
118 /* if FPU has current state, save it first */
119 if (td == PCPU_GET(fpcurthread))
120 MipsSaveCurFPState(td);
121 bcopy((void *)&td->td_frame->f0,
122 (void *)sf.sf_uc.uc_mcontext.mc_fpregs,
123 sizeof(sf.sf_uc.uc_mcontext.mc_fpregs));
126 /* Allocate and validate space for the signal handler context. */
127 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
128 SIGISMEMBER(psp->ps_sigonstack, sig)) {
129 sfp = (struct sigframe *)((vm_offset_t)(td->td_sigstk.ss_sp +
130 td->td_sigstk.ss_size - sizeof(struct sigframe))
131 & ~(sizeof(__int64_t) - 1));
133 sfp = (struct sigframe *)((vm_offset_t)(regs->sp -
134 sizeof(struct sigframe)) & ~(sizeof(__int64_t) - 1));
136 /* Translate the signal if appropriate */
137 if (p->p_sysent->sv_sigtbl) {
138 if (sig <= p->p_sysent->sv_sigsize)
139 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
142 /* Build the argument list for the signal handler. */
144 regs->a2 = (register_t)(intptr_t)&sfp->sf_uc;
145 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
146 /* Signal handler installed with SA_SIGINFO. */
147 regs->a1 = (register_t)(intptr_t)&sfp->sf_si;
148 /* sf.sf_ahu.sf_action = (__siginfohandler_t *)catcher; */
150 /* fill siginfo structure */
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;
155 /* Old FreeBSD-style arguments. */
156 regs->a1 = ksi->ksi_code;
157 regs->a3 = regs->badvaddr;
158 /* sf.sf_ahu.sf_handler = catcher; */
161 mtx_unlock(&psp->ps_mtx);
165 * Copy the sigframe out to the user's stack.
167 if (copyout(&sf, sfp, sizeof(struct sigframe)) != 0) {
169 * Something is wrong with the stack pointer.
170 * ...Kill the process.
176 regs->pc = (register_t)(intptr_t)catcher;
177 regs->t9 = (register_t)(intptr_t)catcher;
178 regs->sp = (register_t)(intptr_t)sfp;
180 * Signal trampoline code is at base of user stack.
182 regs->ra = (register_t)(intptr_t)PS_STRINGS - *(p->p_sysent->sv_szsigcode);
184 mtx_lock(&psp->ps_mtx);
189 * Build siginfo_t for SA thread
192 cpu_thread_siginfo(int sig, u_long code, siginfo_t *si)
199 PROC_LOCK_ASSERT(p, MA_OWNED);
201 bzero(si, sizeof(*si));
204 /* XXXKSE fill other fields */
209 * System call to cleanup state after a signal
210 * has been taken. Reset signal mask and
211 * stack state from context left by sendsig (above).
212 * Return to previous pc as specified by
213 * context left by sendsig.
216 sys_sigreturn(struct thread *td, struct sigreturn_args *uap)
221 error = copyin(uap->sigcntxp, &uc, sizeof(uc));
225 error = set_mcontext(td, &uc.uc_mcontext);
229 kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask, NULL, 0);
231 return (EJUSTRETURN);
235 ptrace_set_pc(struct thread *td, unsigned long addr)
237 td->td_frame->pc = (register_t) addr;
242 ptrace_read_int(struct thread *td, off_t addr, int *v)
247 PROC_LOCK_ASSERT(td->td_proc, MA_NOTOWNED);
248 iov.iov_base = (caddr_t) v;
249 iov.iov_len = sizeof(int);
252 uio.uio_offset = (off_t)addr;
253 uio.uio_resid = sizeof(int);
254 uio.uio_segflg = UIO_SYSSPACE;
255 uio.uio_rw = UIO_READ;
257 return proc_rwmem(td->td_proc, &uio);
261 ptrace_write_int(struct thread *td, off_t addr, int v)
266 PROC_LOCK_ASSERT(td->td_proc, MA_NOTOWNED);
267 iov.iov_base = (caddr_t) &v;
268 iov.iov_len = sizeof(int);
271 uio.uio_offset = (off_t)addr;
272 uio.uio_resid = sizeof(int);
273 uio.uio_segflg = UIO_SYSSPACE;
274 uio.uio_rw = UIO_WRITE;
276 return proc_rwmem(td->td_proc, &uio);
280 ptrace_single_step(struct thread *td)
283 struct trapframe *locr0 = td->td_frame;
285 int bpinstr = MIPS_BREAK_SSTEP;
292 * Fetch what's at the current location.
294 ptrace_read_int(td, (off_t)locr0->pc, &curinstr);
296 /* compute next address after current location */
298 va = MipsEmulateBranch(locr0, locr0->pc, locr0->fsr,
299 (uintptr_t)&curinstr);
303 if (td->td_md.md_ss_addr) {
304 printf("SS %s (%d): breakpoint already set at %x (va %x)\n",
305 p->p_comm, p->p_pid, td->td_md.md_ss_addr, va); /* XXX */
308 td->td_md.md_ss_addr = va;
310 * Fetch what's at the current location.
312 ptrace_read_int(td, (off_t)va, &td->td_md.md_ss_instr);
315 * Store breakpoint instruction at the "next" location now.
317 i = ptrace_write_int (td, va, bpinstr);
320 * The sync'ing of I & D caches is done by procfs_domem()
321 * through procfs_rwmem().
328 printf("SS %s (%d): breakpoint set at %x: %x (pc %x) br %x\n",
329 p->p_comm, p->p_pid, p->p_md.md_ss_addr,
330 p->p_md.md_ss_instr, locr0->pc, curinstr); /* XXX */
337 makectx(struct trapframe *tf, struct pcb *pcb)
340 pcb->pcb_regs.ra = tf->ra;
341 pcb->pcb_regs.pc = tf->pc;
342 pcb->pcb_regs.sp = tf->sp;
346 fill_regs(struct thread *td, struct reg *regs)
348 memcpy(regs, td->td_frame, sizeof(struct reg));
353 set_regs(struct thread *td, struct reg *regs)
358 f = (struct trapframe *) td->td_frame;
360 * Don't allow the user to change SR
363 memcpy(td->td_frame, regs, sizeof(struct reg));
369 get_mcontext(struct thread *td, mcontext_t *mcp, int flags)
371 struct trapframe *tp;
374 PROC_LOCK(curthread->td_proc);
375 mcp->mc_onstack = sigonstack(tp->sp);
376 PROC_UNLOCK(curthread->td_proc);
377 bcopy((void *)&td->td_frame->zero, (void *)&mcp->mc_regs,
378 sizeof(mcp->mc_regs));
380 mcp->mc_fpused = td->td_md.md_flags & MDTD_FPUSED;
381 if (mcp->mc_fpused) {
382 bcopy((void *)&td->td_frame->f0, (void *)&mcp->mc_fpregs,
383 sizeof(mcp->mc_fpregs));
385 if (flags & GET_MC_CLEAR_RET) {
386 mcp->mc_regs[V0] = 0;
387 mcp->mc_regs[V1] = 0;
388 mcp->mc_regs[A3] = 0;
391 mcp->mc_pc = td->td_frame->pc;
392 mcp->mullo = td->td_frame->mullo;
393 mcp->mulhi = td->td_frame->mulhi;
394 mcp->mc_tls = td->td_md.md_tls;
399 set_mcontext(struct thread *td, const mcontext_t *mcp)
401 struct trapframe *tp;
404 bcopy((void *)&mcp->mc_regs, (void *)&td->td_frame->zero,
405 sizeof(mcp->mc_regs));
407 td->td_md.md_flags = mcp->mc_fpused & MDTD_FPUSED;
408 if (mcp->mc_fpused) {
409 bcopy((void *)&mcp->mc_fpregs, (void *)&td->td_frame->f0,
410 sizeof(mcp->mc_fpregs));
412 td->td_frame->pc = mcp->mc_pc;
413 td->td_frame->mullo = mcp->mullo;
414 td->td_frame->mulhi = mcp->mulhi;
415 td->td_md.md_tls = mcp->mc_tls;
416 /* Dont let user to set any bits in Status and casue registers */
422 fill_fpregs(struct thread *td, struct fpreg *fpregs)
424 if (td == PCPU_GET(fpcurthread))
425 MipsSaveCurFPState(td);
426 memcpy(fpregs, &td->td_frame->f0, sizeof(struct fpreg));
431 set_fpregs(struct thread *td, struct fpreg *fpregs)
433 if (PCPU_GET(fpcurthread) == td)
434 PCPU_SET(fpcurthread, (struct thread *)0);
435 memcpy(&td->td_frame->f0, fpregs, sizeof(struct fpreg));
441 * Clear registers on exec
442 * $sp is set to the stack pointer passed in. $pc is set to the entry
443 * point given by the exec_package passed in, as is $t9 (used for PIC
444 * code by the MIPS elf abi).
447 exec_setregs(struct thread *td, struct image_params *imgp, u_long stack)
450 bzero((caddr_t)td->td_frame, sizeof(struct trapframe));
453 * The stack pointer has to be aligned to accommodate the largest
454 * datatype at minimum. This probably means it should be 16-byte
455 * aligned, but for now we're 8-byte aligning it.
457 td->td_frame->sp = ((register_t) stack) & ~(sizeof(__int64_t) - 1);
460 * If we're running o32 or n32 programs but have 64-bit registers,
461 * GCC may use stack-relative addressing near the top of user
462 * address space that, due to sign extension, will yield an
463 * invalid address. For instance, if sp is 0x7fffff00 then GCC
464 * might do something like this to load a word from 0x7ffffff0:
469 * On systems with 64-bit registers, sp is sign-extended to
470 * 0xffffffff80007f00 and the load is instead done from
471 * 0xffffffff7ffffff0.
473 * To prevent this, we subtract 64K from the stack pointer here.
475 * For consistency, we should just always do this unless we're
476 * running n64 programs. For now, since we don't support
477 * COMPAT_FREEBSD32 on n64 kernels, we just do it unless we're
478 * running n64 kernels.
480 #if !defined(__mips_n64)
481 td->td_frame->sp -= 65536;
484 td->td_frame->pc = imgp->entry_addr & ~3;
485 td->td_frame->t9 = imgp->entry_addr & ~3; /* abicall req */
486 td->td_frame->sr = MIPS_SR_KSU_USER | MIPS_SR_EXL | MIPS_SR_INT_IE |
487 (mips_rd_status() & MIPS_SR_INT_MASK);
488 #if defined(__mips_n32)
489 td->td_frame->sr |= MIPS_SR_PX;
490 #elif defined(__mips_n64)
491 td->td_frame->sr |= MIPS_SR_PX | MIPS_SR_UX | MIPS_SR_KX;
494 * FREEBSD_DEVELOPERS_FIXME:
495 * Setup any other CPU-Specific registers (Not MIPS Standard)
496 * and/or bits in other standard MIPS registers (if CPU-Specific)
501 * Set up arguments for the rtld-capable crt0:
503 * a1 rtld cleanup (filled in by dynamic loader)
504 * a2 rtld object (filled in by dynamic loader)
507 td->td_frame->a0 = (register_t) stack;
508 td->td_frame->a1 = 0;
509 td->td_frame->a2 = 0;
510 td->td_frame->a3 = (register_t)imgp->ps_strings;
512 td->td_md.md_flags &= ~MDTD_FPUSED;
513 if (PCPU_GET(fpcurthread) == td)
514 PCPU_SET(fpcurthread, (struct thread *)0);
515 td->td_md.md_ss_addr = 0;
519 ptrace_clear_single_step(struct thread *td)
525 PROC_LOCK_ASSERT(p, MA_OWNED);
526 if (!td->td_md.md_ss_addr)
530 * Restore original instruction and clear BP
532 i = ptrace_write_int (td, td->td_md.md_ss_addr, td->td_md.md_ss_instr);
534 /* The sync'ing of I & D caches is done by procfs_domem(). */
537 log(LOG_ERR, "SS %s %d: can't restore instruction at %x: %x\n",
538 p->p_comm, p->p_pid, td->td_md.md_ss_addr,
539 td->td_md.md_ss_instr);
541 td->td_md.md_ss_addr = 0;