1129198Scognet/*-
2129198Scognet * Copyright (c) 1992 Terrence R. Lambert.
3139735Simp * Copyright (c) 1982, 1987, 1990 The Regents of the University of California.
4129198Scognet * All rights reserved.
5129198Scognet *
6129198Scognet * This code is derived from software contributed to Berkeley by
7129198Scognet * William Jolitz.
8129198Scognet *
9129198Scognet * Redistribution and use in source and binary forms, with or without
10129198Scognet * modification, are permitted provided that the following conditions
11129198Scognet * are met:
12129198Scognet * 1. Redistributions of source code must retain the above copyright
13129198Scognet *    notice, this list of conditions and the following disclaimer.
14129198Scognet * 2. Redistributions in binary form must reproduce the above copyright
15129198Scognet *    notice, this list of conditions and the following disclaimer in the
16129198Scognet *    documentation and/or other materials provided with the distribution.
17129198Scognet * 4. Neither the name of the University nor the names of its contributors
18129198Scognet *    may be used to endorse or promote products derived from this software
19129198Scognet *    without specific prior written permission.
20129198Scognet *
21129198Scognet * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22129198Scognet * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23129198Scognet * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24129198Scognet * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25129198Scognet * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26129198Scognet * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27129198Scognet * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28129198Scognet * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29129198Scognet * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30129198Scognet * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31129198Scognet * SUCH DAMAGE.
32129198Scognet *
33129198Scognet *	from: @(#)machdep.c	7.4 (Berkeley) 6/3/91
34129198Scognet *	from: src/sys/i386/i386/machdep.c,v 1.385.2.3 2000/05/10 02:04:46 obrien
35129198Scognet *	JNPR: pm_machdep.c,v 1.9.2.1 2007/08/16 15:59:10 girish
36129198Scognet */
37129198Scognet
38129198Scognet#include <sys/cdefs.h>
39129198Scognet__FBSDID("$FreeBSD: releng/10.3/sys/mips/mips/pm_machdep.c 293581 2016-01-09 17:39:41Z dchagin $");
40129198Scognet
41129198Scognet#include "opt_compat.h"
42129198Scognet
43129198Scognet#include <sys/types.h>
44129198Scognet#include <sys/param.h>
45129198Scognet#include <sys/systm.h>
46129198Scognet#include <sys/sysent.h>
47129198Scognet#include <sys/proc.h>
48129198Scognet#include <sys/signalvar.h>
49129198Scognet#include <sys/exec.h>
50129198Scognet#include <sys/imgact.h>
51129198Scognet#include <sys/ucontext.h>
52129198Scognet#include <sys/lock.h>
53129198Scognet#include <sys/syscallsubr.h>
54129198Scognet#include <sys/sysproto.h>
55129198Scognet#include <sys/ptrace.h>
56129198Scognet#include <sys/syslog.h>
57129198Scognet#include <vm/vm.h>
58129198Scognet#include <vm/pmap.h>
59129198Scognet#include <vm/vm_map.h>
60129198Scognet#include <vm/vm_extern.h>
61129198Scognet#include <sys/user.h>
62129198Scognet#include <sys/uio.h>
63129198Scognet#include <machine/reg.h>
64129198Scognet#include <machine/md_var.h>
65129198Scognet#include <machine/sigframe.h>
66129198Scognet#include <machine/vmparam.h>
67129198Scognet#include <sys/vnode.h>
68129198Scognet#include <fs/pseudofs/pseudofs.h>
69129198Scognet#include <fs/procfs/procfs.h>
70129198Scognet
71129198Scognet#define	UCONTEXT_MAGIC	0xACEDBADE
72129198Scognet
73129198Scognet/*
74129198Scognet * Send an interrupt to process.
75129198Scognet *
76129198Scognet * Stack is set up to allow sigcode stored
77129198Scognet * at top to call routine, followed by kcall
78129198Scognet * to sigreturn routine below.	After sigreturn
79129198Scognet * resets the signal mask, the stack, and the
80129198Scognet * frame pointer, it returns to the user
81129198Scognet * specified pc, psl.
82129198Scognet */
83129198Scognetvoid
84129198Scognetsendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
85129198Scognet{
86129198Scognet	struct proc *p;
87129198Scognet	struct thread *td;
88129198Scognet	struct trapframe *regs;
89129198Scognet	struct sigacts *psp;
90129198Scognet	struct sigframe sf, *sfp;
91129198Scognet	int sig;
92129198Scognet	int oonstack;
93129198Scognet
94129198Scognet	td = curthread;
95137463Scognet	p = td->td_proc;
96129198Scognet	PROC_LOCK_ASSERT(p, MA_OWNED);
97129198Scognet	sig = ksi->ksi_signo;
98129198Scognet	psp = p->p_sigacts;
99129198Scognet	mtx_assert(&psp->ps_mtx, MA_OWNED);
100129198Scognet
101129198Scognet	regs = td->td_frame;
102129198Scognet	oonstack = sigonstack(regs->sp);
103129198Scognet
104248361Sandrew	/* save user context */
105129198Scognet	bzero(&sf, sizeof(struct sigframe));
106129198Scognet	sf.sf_uc.uc_sigmask = *mask;
107129198Scognet	sf.sf_uc.uc_stack = td->td_sigstk;
108129198Scognet	sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
109129198Scognet	sf.sf_uc.uc_mcontext.mc_pc = regs->pc;
110129198Scognet	sf.sf_uc.uc_mcontext.mullo = regs->mullo;
111129198Scognet	sf.sf_uc.uc_mcontext.mulhi = regs->mulhi;
112129198Scognet	sf.sf_uc.uc_mcontext.mc_tls = td->td_md.md_tls;
113129198Scognet	sf.sf_uc.uc_mcontext.mc_regs[0] = UCONTEXT_MAGIC;  /* magic number */
114129198Scognet	bcopy((void *)&regs->ast, (void *)&sf.sf_uc.uc_mcontext.mc_regs[1],
115129198Scognet	    sizeof(sf.sf_uc.uc_mcontext.mc_regs) - sizeof(register_t));
116129198Scognet	sf.sf_uc.uc_mcontext.mc_fpused = td->td_md.md_flags & MDTD_FPUSED;
117129198Scognet	if (sf.sf_uc.uc_mcontext.mc_fpused) {
118129198Scognet		/* if FPU has current state, save it first */
119129198Scognet		if (td == PCPU_GET(fpcurthread))
120129198Scognet			MipsSaveCurFPState(td);
121129198Scognet		bcopy((void *)&td->td_frame->f0,
122129198Scognet		    (void *)sf.sf_uc.uc_mcontext.mc_fpregs,
123129198Scognet		    sizeof(sf.sf_uc.uc_mcontext.mc_fpregs));
124129198Scognet	}
125129198Scognet
126129198Scognet	/* Allocate and validate space for the signal handler context. */
127129198Scognet	if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
128129198Scognet	    SIGISMEMBER(psp->ps_sigonstack, sig)) {
129129198Scognet		sfp = (struct sigframe *)((vm_offset_t)(td->td_sigstk.ss_sp +
130129198Scognet		    td->td_sigstk.ss_size - sizeof(struct sigframe))
131129198Scognet		    & ~(sizeof(__int64_t) - 1));
132129198Scognet	} else
133129198Scognet		sfp = (struct sigframe *)((vm_offset_t)(regs->sp -
134129198Scognet		    sizeof(struct sigframe)) & ~(sizeof(__int64_t) - 1));
135129198Scognet
136129198Scognet	/* Build the argument list for the signal handler. */
137129198Scognet	regs->a0 = sig;
138129198Scognet	regs->a2 = (register_t)(intptr_t)&sfp->sf_uc;
139129198Scognet	if (SIGISMEMBER(psp->ps_siginfo, sig)) {
140129198Scognet		/* Signal handler installed with SA_SIGINFO. */
141129198Scognet		regs->a1 = (register_t)(intptr_t)&sfp->sf_si;
142129198Scognet		/* sf.sf_ahu.sf_action = (__siginfohandler_t *)catcher; */
143129198Scognet
144129198Scognet		/* fill siginfo structure */
145129198Scognet		sf.sf_si.si_signo = sig;
146129198Scognet		sf.sf_si.si_code = ksi->ksi_code;
147129198Scognet		sf.sf_si.si_addr = (void*)(intptr_t)regs->badvaddr;
148129198Scognet	} else {
149129198Scognet		/* Old FreeBSD-style arguments. */
150129198Scognet		regs->a1 = ksi->ksi_code;
151129198Scognet		regs->a3 = regs->badvaddr;
152129198Scognet		/* sf.sf_ahu.sf_handler = catcher; */
153129198Scognet	}
154129198Scognet
155129198Scognet	mtx_unlock(&psp->ps_mtx);
156248361Sandrew	PROC_UNLOCK(p);
157129198Scognet
158129198Scognet	/*
159129198Scognet	 * Copy the sigframe out to the user's stack.
160129198Scognet	 */
161129198Scognet	if (copyout(&sf, sfp, sizeof(struct sigframe)) != 0) {
162129198Scognet		/*
163129198Scognet		 * Something is wrong with the stack pointer.
164129198Scognet		 * ...Kill the process.
165129198Scognet		 */
166129198Scognet		PROC_LOCK(p);
167129198Scognet		sigexit(td, SIGILL);
168129198Scognet	}
169129198Scognet
170129198Scognet	regs->pc = (register_t)(intptr_t)catcher;
171129198Scognet	regs->t9 = (register_t)(intptr_t)catcher;
172129198Scognet	regs->sp = (register_t)(intptr_t)sfp;
173129198Scognet	/*
174129198Scognet	 * Signal trampoline code is at base of user stack.
175129198Scognet	 */
176129198Scognet	regs->ra = (register_t)(intptr_t)PS_STRINGS - *(p->p_sysent->sv_szsigcode);
177129198Scognet	PROC_LOCK(p);
178129198Scognet	mtx_lock(&psp->ps_mtx);
179129198Scognet}
180129198Scognet
181129198Scognet#ifdef GONE_IN_7
182129198Scognet/*
183129198Scognet * Build siginfo_t for SA thread
184129198Scognet */
185129198Scognetvoid
186129198Scognetcpu_thread_siginfo(int sig, u_long code, siginfo_t *si)
187137463Scognet{
188129198Scognet	struct proc *p;
189129198Scognet	struct thread *td;
190129198Scognet
191129198Scognet	td = curthread;
192129198Scognet	p = td->td_proc;
193129198Scognet	PROC_LOCK_ASSERT(p, MA_OWNED);
194129198Scognet
195129198Scognet	bzero(si, sizeof(*si));
196129198Scognet	si->si_signo = sig;
197129198Scognet	si->si_code = code;
198129198Scognet	/* XXXKSE fill other fields */
199129198Scognet}
200129198Scognet#endif
201129198Scognet
202129198Scognet/*
203137463Scognet * System call to cleanup state after a signal
204248361Sandrew * has been taken.  Reset signal mask and
205129198Scognet * stack state from context left by sendsig (above).
206129198Scognet * Return to previous pc as specified by
207129198Scognet * context left by sendsig.
208129198Scognet */
209129198Scognetint
210129198Scognetsys_sigreturn(struct thread *td, struct sigreturn_args *uap)
211129198Scognet{
212129198Scognet	ucontext_t uc;
213129198Scognet	int error;
214129198Scognet
215129198Scognet	error = copyin(uap->sigcntxp, &uc, sizeof(uc));
216129198Scognet	if (error != 0)
217129198Scognet	    return (error);
218129198Scognet
219129198Scognet	error = set_mcontext(td, &uc.uc_mcontext);
220129198Scognet	if (error != 0)
221129198Scognet		return (error);
222129198Scognet
223129198Scognet	kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask, NULL, 0);
224129198Scognet
225129198Scognet	return (EJUSTRETURN);
226129198Scognet}
227129198Scognet
228129198Scognetint
229129198Scognetptrace_set_pc(struct thread *td, unsigned long addr)
230129198Scognet{
231129198Scognet	td->td_frame->pc = (register_t) addr;
232129198Scognet	return 0;
233129198Scognet}
234129198Scognet
235129198Scognetstatic int
236137463Scognetptrace_read_int(struct thread *td, off_t addr, int *v)
237129198Scognet{
238129198Scognet	struct iovec iov;
239129198Scognet	struct uio uio;
240129198Scognet
241129198Scognet	PROC_LOCK_ASSERT(td->td_proc, MA_NOTOWNED);
242129198Scognet	iov.iov_base = (caddr_t) v;
243129198Scognet	iov.iov_len = sizeof(int);
244129198Scognet	uio.uio_iov = &iov;
245129198Scognet	uio.uio_iovcnt = 1;
246129198Scognet	uio.uio_offset = (off_t)addr;
247129198Scognet	uio.uio_resid = sizeof(int);
248129198Scognet	uio.uio_segflg = UIO_SYSSPACE;
249129198Scognet	uio.uio_rw = UIO_READ;
250129198Scognet	uio.uio_td = td;
251129198Scognet	return proc_rwmem(td->td_proc, &uio);
252129198Scognet}
253129198Scognet
254129198Scognetstatic int
255129198Scognetptrace_write_int(struct thread *td, off_t addr, int v)
256129198Scognet{
257129198Scognet	struct iovec iov;
258129198Scognet	struct uio uio;
259129198Scognet
260129198Scognet	PROC_LOCK_ASSERT(td->td_proc, MA_NOTOWNED);
261129198Scognet	iov.iov_base = (caddr_t) &v;
262129198Scognet	iov.iov_len = sizeof(int);
263129198Scognet	uio.uio_iov = &iov;
264137463Scognet	uio.uio_iovcnt = 1;
265248361Sandrew	uio.uio_offset = (off_t)addr;
266129198Scognet	uio.uio_resid = sizeof(int);
267129198Scognet	uio.uio_segflg = UIO_SYSSPACE;
268129198Scognet	uio.uio_rw = UIO_WRITE;
269129198Scognet	uio.uio_td = td;
270129198Scognet	return proc_rwmem(td->td_proc, &uio);
271129198Scognet}
272129198Scognet
273129198Scognetint
274129198Scognetptrace_single_step(struct thread *td)
275129198Scognet{
276129198Scognet	unsigned va;
277129198Scognet	struct trapframe *locr0 = td->td_frame;
278129198Scognet	int i;
279129198Scognet	int bpinstr = MIPS_BREAK_SSTEP;
280129198Scognet	int curinstr;
281129198Scognet	struct proc *p;
282129198Scognet
283129198Scognet	p = td->td_proc;
284129198Scognet	PROC_UNLOCK(p);
285129198Scognet	/*
286129198Scognet	 * Fetch what's at the current location.
287129198Scognet	 */
288129198Scognet	ptrace_read_int(td,  (off_t)locr0->pc, &curinstr);
289129198Scognet
290129198Scognet	/* compute next address after current location */
291129198Scognet	if(curinstr != 0) {
292129198Scognet		va = MipsEmulateBranch(locr0, locr0->pc, locr0->fsr,
293129198Scognet		    (uintptr_t)&curinstr);
294129198Scognet	} else {
295129198Scognet		va = locr0->pc + 4;
296129198Scognet	}
297129198Scognet	if (td->td_md.md_ss_addr) {
298129198Scognet		printf("SS %s (%d): breakpoint already set at %x (va %x)\n",
299129198Scognet		    p->p_comm, p->p_pid, td->td_md.md_ss_addr, va); /* XXX */
300129198Scognet		return (EFAULT);
301129198Scognet	}
302129198Scognet	td->td_md.md_ss_addr = va;
303129198Scognet	/*
304129198Scognet	 * Fetch what's at the current location.
305129198Scognet	 */
306129198Scognet	ptrace_read_int(td, (off_t)va, &td->td_md.md_ss_instr);
307129198Scognet
308129198Scognet	/*
309129198Scognet	 * Store breakpoint instruction at the "next" location now.
310129198Scognet	 */
311129198Scognet	i = ptrace_write_int (td, va, bpinstr);
312129198Scognet
313129198Scognet	/*
314129198Scognet	 * The sync'ing of I & D caches is done by procfs_domem()
315129198Scognet	 * through procfs_rwmem().
316129198Scognet	 */
317129198Scognet
318129198Scognet	PROC_LOCK(p);
319129198Scognet	if (i < 0)
320129198Scognet		return (EFAULT);
321129198Scognet#if 0
322129198Scognet	printf("SS %s (%d): breakpoint set at %x: %x (pc %x) br %x\n",
323129198Scognet	    p->p_comm, p->p_pid, p->p_md.md_ss_addr,
324248361Sandrew	    p->p_md.md_ss_instr, locr0->pc, curinstr); /* XXX */
325129198Scognet#endif
326129198Scognet	return (0);
327129198Scognet}
328129198Scognet
329129198Scognet
330129198Scognetvoid
331129198Scognetmakectx(struct trapframe *tf, struct pcb *pcb)
332129198Scognet{
333129198Scognet
334129198Scognet	pcb->pcb_regs.ra = tf->ra;
335129198Scognet	pcb->pcb_regs.pc = tf->pc;
336129198Scognet	pcb->pcb_regs.sp = tf->sp;
337129198Scognet}
338129198Scognet
339129198Scognetint
340129198Scognetfill_regs(struct thread *td, struct reg *regs)
341129198Scognet{
342129198Scognet	memcpy(regs, td->td_frame, sizeof(struct reg));
343129198Scognet	return (0);
344129198Scognet}
345129198Scognet
346129198Scognetint
347129198Scognetset_regs(struct thread *td, struct reg *regs)
348129198Scognet{
349129198Scognet	struct trapframe *f;
350129198Scognet	register_t sr;
351129198Scognet
352129198Scognet	f = (struct trapframe *) td->td_frame;
353129198Scognet	/*
354129198Scognet	 * Don't allow the user to change SR
355129198Scognet	 */
356129198Scognet	sr = f->sr;
357129198Scognet	memcpy(td->td_frame, regs, sizeof(struct reg));
358129198Scognet	f->sr = sr;
359129198Scognet	return (0);
360129198Scognet}
361129198Scognet
362129198Scognetint
363129198Scognetget_mcontext(struct thread *td, mcontext_t *mcp, int flags)
364129198Scognet{
365129198Scognet	struct trapframe *tp;
366129198Scognet
367129198Scognet	tp = td->td_frame;
368129198Scognet	PROC_LOCK(curthread->td_proc);
369129198Scognet	mcp->mc_onstack = sigonstack(tp->sp);
370129198Scognet	PROC_UNLOCK(curthread->td_proc);
371129198Scognet	bcopy((void *)&td->td_frame->zero, (void *)&mcp->mc_regs,
372129198Scognet	    sizeof(mcp->mc_regs));
373129198Scognet
374129198Scognet	mcp->mc_fpused = td->td_md.md_flags & MDTD_FPUSED;
375129198Scognet	if (mcp->mc_fpused) {
376129198Scognet		bcopy((void *)&td->td_frame->f0, (void *)&mcp->mc_fpregs,
377129198Scognet		    sizeof(mcp->mc_fpregs));
378129198Scognet	}
379129198Scognet	if (flags & GET_MC_CLEAR_RET) {
380129198Scognet		mcp->mc_regs[V0] = 0;
381129198Scognet		mcp->mc_regs[V1] = 0;
382129198Scognet		mcp->mc_regs[A3] = 0;
383129198Scognet	}
384129198Scognet
385129198Scognet	mcp->mc_pc = td->td_frame->pc;
386129198Scognet	mcp->mullo = td->td_frame->mullo;
387129198Scognet	mcp->mulhi = td->td_frame->mulhi;
388129198Scognet	mcp->mc_tls = td->td_md.md_tls;
389129198Scognet	return (0);
390129198Scognet}
391248361Sandrew
392129198Scognetint
393129198Scognetset_mcontext(struct thread *td, mcontext_t *mcp)
394129198Scognet{
395129198Scognet	struct trapframe *tp;
396129198Scognet
397129198Scognet	tp = td->td_frame;
398129198Scognet	bcopy((void *)&mcp->mc_regs, (void *)&td->td_frame->zero,
399129198Scognet	    sizeof(mcp->mc_regs));
400129198Scognet
401129198Scognet	td->td_md.md_flags = mcp->mc_fpused & MDTD_FPUSED;
402129198Scognet	if (mcp->mc_fpused) {
403129198Scognet		bcopy((void *)&mcp->mc_fpregs, (void *)&td->td_frame->f0,
404129198Scognet		    sizeof(mcp->mc_fpregs));
405129198Scognet	}
406129198Scognet	td->td_frame->pc = mcp->mc_pc;
407129198Scognet	td->td_frame->mullo = mcp->mullo;
408129198Scognet	td->td_frame->mulhi = mcp->mulhi;
409129198Scognet	td->td_md.md_tls = mcp->mc_tls;
410129198Scognet	/* Dont let user to set any bits in Status and casue registers */
411129198Scognet
412129198Scognet	return (0);
413129198Scognet}
414129198Scognet
415129198Scognetint
416129198Scognetfill_fpregs(struct thread *td, struct fpreg *fpregs)
417129198Scognet{
418129198Scognet	if (td == PCPU_GET(fpcurthread))
419129198Scognet		MipsSaveCurFPState(td);
420129198Scognet	memcpy(fpregs, &td->td_frame->f0, sizeof(struct fpreg));
421129198Scognet	return 0;
422129198Scognet}
423129198Scognet
424129198Scognetint
425129198Scognetset_fpregs(struct thread *td, struct fpreg *fpregs)
426129198Scognet{
427129198Scognet	if (PCPU_GET(fpcurthread) == td)
428129198Scognet		PCPU_SET(fpcurthread, (struct thread *)0);
429129198Scognet	memcpy(&td->td_frame->f0, fpregs, sizeof(struct fpreg));
430129198Scognet	return 0;
431129198Scognet}
432129198Scognet
433129198Scognet
434129198Scognet/*
435129198Scognet * Clear registers on exec
436129198Scognet * $sp is set to the stack pointer passed in.  $pc is set to the entry
437129198Scognet * point given by the exec_package passed in, as is $t9 (used for PIC
438129198Scognet * code by the MIPS elf abi).
439129198Scognet */
440129198Scognetvoid
441129198Scognetexec_setregs(struct thread *td, struct image_params *imgp, u_long stack)
442129198Scognet{
443129198Scognet
444129198Scognet	bzero((caddr_t)td->td_frame, sizeof(struct trapframe));
445129198Scognet
446129198Scognet	/*
447129198Scognet	 * The stack pointer has to be aligned to accommodate the largest
448129198Scognet	 * datatype at minimum.  This probably means it should be 16-byte
449129198Scognet	 * aligned, but for now we're 8-byte aligning it.
450129198Scognet	 */
451129198Scognet	td->td_frame->sp = ((register_t) stack) & ~(sizeof(__int64_t) - 1);
452129198Scognet
453129198Scognet	/*
454129198Scognet	 * If we're running o32 or n32 programs but have 64-bit registers,
455129198Scognet	 * GCC may use stack-relative addressing near the top of user
456129198Scognet	 * address space that, due to sign extension, will yield an
457129198Scognet	 * invalid address.  For instance, if sp is 0x7fffff00 then GCC
458129198Scognet	 * might do something like this to load a word from 0x7ffffff0:
459129198Scognet	 *
460129198Scognet	 * 	addu	sp, sp, 32768
461129198Scognet	 * 	lw	t0, -32528(sp)
462129198Scognet	 *
463129198Scognet	 * On systems with 64-bit registers, sp is sign-extended to
464129198Scognet	 * 0xffffffff80007f00 and the load is instead done from
465129198Scognet	 * 0xffffffff7ffffff0.
466129198Scognet	 *
467129198Scognet	 * To prevent this, we subtract 64K from the stack pointer here.
468129198Scognet	 *
469129198Scognet	 * For consistency, we should just always do this unless we're
470129198Scognet	 * running n64 programs.  For now, since we don't support
471129198Scognet	 * COMPAT_FREEBSD32 on n64 kernels, we just do it unless we're
472129198Scognet	 * running n64 kernels.
473129198Scognet	 */
474129198Scognet#if !defined(__mips_n64)
475129198Scognet	td->td_frame->sp -= 65536;
476129198Scognet#endif
477129198Scognet
478129198Scognet	td->td_frame->pc = imgp->entry_addr & ~3;
479129198Scognet	td->td_frame->t9 = imgp->entry_addr & ~3; /* abicall req */
480129198Scognet	td->td_frame->sr = MIPS_SR_KSU_USER | MIPS_SR_EXL | MIPS_SR_INT_IE |
481129198Scognet	    (mips_rd_status() & MIPS_SR_INT_MASK);
482129198Scognet#if defined(__mips_n32)
483129198Scognet	td->td_frame->sr |= MIPS_SR_PX;
484129198Scognet#elif  defined(__mips_n64)
485129198Scognet	td->td_frame->sr |= MIPS_SR_PX | MIPS_SR_UX | MIPS_SR_KX;
486129198Scognet#endif
487129198Scognet	/*
488248361Sandrew	 * FREEBSD_DEVELOPERS_FIXME:
489129198Scognet	 * Setup any other CPU-Specific registers (Not MIPS Standard)
490129198Scognet	 * and/or bits in other standard MIPS registers (if CPU-Specific)
491129198Scognet	 *  that are needed.
492129198Scognet	 */
493129198Scognet
494129198Scognet	/*
495129198Scognet	 * Set up arguments for the rtld-capable crt0:
496129198Scognet	 *	a0	stack pointer
497129198Scognet	 *	a1	rtld cleanup (filled in by dynamic loader)
498129198Scognet	 *	a2	rtld object (filled in by dynamic loader)
499129198Scognet	 *	a3	ps_strings
500129198Scognet	 */
501129198Scognet	td->td_frame->a0 = (register_t) stack;
502129198Scognet	td->td_frame->a1 = 0;
503129198Scognet	td->td_frame->a2 = 0;
504129198Scognet	td->td_frame->a3 = (register_t)imgp->ps_strings;
505129198Scognet
506129198Scognet	td->td_md.md_flags &= ~MDTD_FPUSED;
507129198Scognet	if (PCPU_GET(fpcurthread) == td)
508129198Scognet	    PCPU_SET(fpcurthread, (struct thread *)0);
509129198Scognet	td->td_md.md_ss_addr = 0;
510129198Scognet}
511129198Scognet
512129198Scognetint
513129198Scognetptrace_clear_single_step(struct thread *td)
514129198Scognet{
515129198Scognet	int i;
516129198Scognet	struct proc *p;
517129198Scognet
518129198Scognet	p = td->td_proc;
519129198Scognet	PROC_LOCK_ASSERT(p, MA_OWNED);
520129198Scognet	if (!td->td_md.md_ss_addr)
521129198Scognet		return EINVAL;
522129198Scognet
523129198Scognet	/*
524129198Scognet	 * Restore original instruction and clear BP
525129198Scognet	 */
526129198Scognet	i = ptrace_write_int (td, td->td_md.md_ss_addr, td->td_md.md_ss_instr);
527129198Scognet
528129198Scognet	/* The sync'ing of I & D caches is done by procfs_domem(). */
529129198Scognet
530129198Scognet	if (i < 0) {
531129198Scognet		log(LOG_ERR, "SS %s %d: can't restore instruction at %x: %x\n",
532129198Scognet		    p->p_comm, p->p_pid, td->td_md.md_ss_addr,
533129198Scognet		    td->td_md.md_ss_instr);
534129198Scognet	}
535129198Scognet	td->td_md.md_ss_addr = 0;
536129198Scognet	return 0;
537129198Scognet}
538129198Scognet