1 /*-
2 * Copyright (c) 1992 Terrence R. Lambert.
3 * Copyright (c) 1982, 1987, 1990 The Regents of the University of California.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * William Jolitz.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
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.
20 *
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
31 * SUCH DAMAGE.
32 *
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
36 */
37
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD: releng/8.1/sys/mips/mips/pm_machdep.c 200722 2009-12-19 11:31:28Z kib $");
40
41 #include "opt_compat.h"
42 #include <sys/types.h>
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/sysent.h>
46 #include <sys/proc.h>
47 #include <sys/signalvar.h>
48 #include <sys/exec.h>
49 #include <sys/imgact.h>
50 #include <sys/ucontext.h>
51 #include <sys/lock.h>
52 #include <sys/sysproto.h>
53 #include <sys/ptrace.h>
54 #include <sys/syslog.h>
55 #include <vm/vm.h>
56 #include <vm/pmap.h>
57 #include <vm/vm_map.h>
58 #include <vm/vm_extern.h>
59 #include <sys/user.h>
60 #include <sys/uio.h>
61 #include <machine/reg.h>
62 #include <machine/md_var.h>
63 #include <machine/sigframe.h>
64 #include <machine/vmparam.h>
65 #include <sys/vnode.h>
66 #include <fs/pseudofs/pseudofs.h>
67 #include <fs/procfs/procfs.h>
68
69 #define UCONTEXT_MAGIC 0xACEDBADE
70
71 /*
72 * Send an interrupt to process.
73 *
74 * Stack is set up to allow sigcode stored
75 * at top to call routine, followed by kcall
76 * to sigreturn routine below. After sigreturn
77 * resets the signal mask, the stack, and the
78 * frame pointer, it returns to the user
79 * specified pc, psl.
80 */
81 void
82 sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
83 {
84 struct proc *p;
85 struct thread *td;
86 struct trapframe *regs;
87 struct sigacts *psp;
88 struct sigframe sf, *sfp;
89 int sig;
90 int oonstack;
91
92 td = curthread;
93 p = td->td_proc;
94 PROC_LOCK_ASSERT(p, MA_OWNED);
95 sig = ksi->ksi_signo;
96 psp = p->p_sigacts;
97 mtx_assert(&psp->ps_mtx, MA_OWNED);
98
99 regs = td->td_frame;
100 oonstack = sigonstack(regs->sp);
101
102 /* save user context */
103 bzero(&sf, sizeof(struct sigframe));
104 sf.sf_uc.uc_sigmask = *mask;
105 sf.sf_uc.uc_stack = td->td_sigstk;
106 sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
107 sf.sf_uc.uc_mcontext.mc_pc = regs->pc;
108 sf.sf_uc.uc_mcontext.mullo = regs->mullo;
109 sf.sf_uc.uc_mcontext.mulhi = regs->mulhi;
110 sf.sf_uc.uc_mcontext.mc_regs[0] = UCONTEXT_MAGIC; /* magic number */
111 bcopy((void *)®s->ast, (void *)&sf.sf_uc.uc_mcontext.mc_regs[1],
112 sizeof(sf.sf_uc.uc_mcontext.mc_regs) - sizeof(register_t));
113 sf.sf_uc.uc_mcontext.mc_fpused = td->td_md.md_flags & MDTD_FPUSED;
114 if (sf.sf_uc.uc_mcontext.mc_fpused) {
115 /* if FPU has current state, save it first */
116 if (td == PCPU_GET(fpcurthread))
117 MipsSaveCurFPState(td);
118 bcopy((void *)&td->td_frame->f0,
119 (void *)sf.sf_uc.uc_mcontext.mc_fpregs,
120 sizeof(sf.sf_uc.uc_mcontext.mc_fpregs));
121 }
122
123 /* Allocate and validate space for the signal handler context. */
124 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
125 SIGISMEMBER(psp->ps_sigonstack, sig)) {
126 sfp = (struct sigframe *)((vm_offset_t)(td->td_sigstk.ss_sp +
127 td->td_sigstk.ss_size - sizeof(struct sigframe))
128 & ~(sizeof(__int64_t) - 1));
129 } else
130 sfp = (struct sigframe *)((vm_offset_t)(regs->sp -
131 sizeof(struct sigframe)) & ~(sizeof(__int64_t) - 1));
132
133 /* Translate the signal if appropriate */
134 if (p->p_sysent->sv_sigtbl) {
135 if (sig <= p->p_sysent->sv_sigsize)
136 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
137 }
138
139 /* Build the argument list for the signal handler. */
140 regs->a0 = sig;
141 regs->a2 = (register_t)&sfp->sf_uc;
142 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
143 /* Signal handler installed with SA_SIGINFO. */
144 regs->a1 = (register_t)&sfp->sf_si;
145 /* sf.sf_ahu.sf_action = (__siginfohandler_t *)catcher; */
146
147 /* fill siginfo structure */
148 sf.sf_si.si_signo = sig;
149 sf.sf_si.si_code = ksi->ksi_code;
150 sf.sf_si.si_addr = (void*)regs->badvaddr;
151 } else {
152 /* Old FreeBSD-style arguments. */
153 regs->a1 = ksi->ksi_code;
154 regs->a3 = regs->badvaddr;
155 /* sf.sf_ahu.sf_handler = catcher; */
156 }
157
158 mtx_unlock(&psp->ps_mtx);
159 PROC_UNLOCK(p);
160
161 /*
162 * Copy the sigframe out to the user's stack.
163 */
164 if (copyout(&sf, sfp, sizeof(struct sigframe)) != 0) {
165 /*
166 * Something is wrong with the stack pointer.
167 * ...Kill the process.
168 */
169 PROC_LOCK(p);
170 sigexit(td, SIGILL);
171 }
172
173 regs->pc = (register_t) catcher;
174 regs->t9 = (register_t) catcher;
175 regs->sp = (register_t) sfp;
176 /*
177 * Signal trampoline code is at base of user stack.
178 */
179 regs->ra = (register_t) PS_STRINGS - *(p->p_sysent->sv_szsigcode);
180 PROC_LOCK(p);
181 mtx_lock(&psp->ps_mtx);
182 }
183
184 #ifdef GONE_IN_7
185 /*
186 * Build siginfo_t for SA thread
187 */
188 void
189 cpu_thread_siginfo(int sig, u_long code, siginfo_t *si)
190 {
191 struct proc *p;
192 struct thread *td;
193
194 td = curthread;
195 p = td->td_proc;
196 PROC_LOCK_ASSERT(p, MA_OWNED);
197
198 bzero(si, sizeof(*si));
199 si->si_signo = sig;
200 si->si_code = code;
201 /* XXXKSE fill other fields */
202 }
203 #endif
204
205 /*
206 * System call to cleanup state after a signal
207 * has been taken. Reset signal mask and
208 * stack state from context left by sendsig (above).
209 * Return to previous pc as specified by
210 * context left by sendsig.
211 */
212 int
213 sigreturn(struct thread *td, struct sigreturn_args *uap)
214 {
215 struct trapframe *regs;
216 ucontext_t *ucp;
217 ucontext_t uc;
218 int error;
219
220 ucp = &uc;
221
222 error = copyin(uap->sigcntxp, &uc, sizeof(uc));
223 if (error != 0)
224 return (error);
225
226 regs = td->td_frame;
227
228 /* #ifdef DEBUG */
229 if (ucp->uc_mcontext.mc_regs[ZERO] != UCONTEXT_MAGIC) {
230 printf("sigreturn: pid %d, ucp %p\n", td->td_proc->p_pid, ucp);
231 printf(" old sp %x ra %x pc %x\n",
232 regs->sp, regs->ra, regs->pc);
233 printf(" new sp %x ra %x pc %x z %x\n",
234 ucp->uc_mcontext.mc_regs[SP],
235 ucp->uc_mcontext.mc_regs[RA],
236 ucp->uc_mcontext.mc_regs[PC],
237 ucp->uc_mcontext.mc_regs[ZERO]);
238 return EINVAL;
239 }
240 /* #endif */
241
242 bcopy((const void *)&ucp->uc_mcontext.mc_regs[1], (void *)®s->ast,
243 sizeof(ucp->uc_mcontext.mc_regs) - sizeof(register_t));
244
245 if (ucp->uc_mcontext.mc_fpused)
246 bcopy((const void *)ucp->uc_mcontext.mc_fpregs,
247 (void *)&td->td_frame->f0,
248 sizeof(ucp->uc_mcontext.mc_fpregs));
249
250 regs->pc = ucp->uc_mcontext.mc_pc;
251 regs->mullo = ucp->uc_mcontext.mullo;
252 regs->mulhi = ucp->uc_mcontext.mulhi;
253
254 kern_sigprocmask(td, SIG_SETMASK, &ucp->uc_sigmask, NULL, 0);
255
256 return(EJUSTRETURN);
257 }
258
259
260 int
261 ptrace_set_pc(struct thread *td, unsigned long addr)
262 {
263 td->td_frame->pc = (register_t) addr;
264 return 0;
265 }
266
267 static int
268 ptrace_read_int(struct thread *td, off_t addr, int *v)
269 {
270 struct iovec iov;
271 struct uio uio;
272
273 PROC_LOCK_ASSERT(td->td_proc, MA_NOTOWNED);
274 iov.iov_base = (caddr_t) v;
275 iov.iov_len = sizeof(int);
276 uio.uio_iov = &iov;
277 uio.uio_iovcnt = 1;
278 uio.uio_offset = (off_t)addr;
279 uio.uio_resid = sizeof(int);
280 uio.uio_segflg = UIO_SYSSPACE;
281 uio.uio_rw = UIO_READ;
282 uio.uio_td = td;
283 return proc_rwmem(td->td_proc, &uio);
284 }
285
286 static int
287 ptrace_write_int(struct thread *td, off_t addr, int v)
288 {
289 struct iovec iov;
290 struct uio uio;
291
292 PROC_LOCK_ASSERT(td->td_proc, MA_NOTOWNED);
293 iov.iov_base = (caddr_t) &v;
294 iov.iov_len = sizeof(int);
295 uio.uio_iov = &iov;
296 uio.uio_iovcnt = 1;
297 uio.uio_offset = (off_t)addr;
298 uio.uio_resid = sizeof(int);
299 uio.uio_segflg = UIO_SYSSPACE;
300 uio.uio_rw = UIO_WRITE;
301 uio.uio_td = td;
302 return proc_rwmem(td->td_proc, &uio);
303 }
304
305 int
306 ptrace_single_step(struct thread *td)
307 {
308 unsigned va;
309 struct trapframe *locr0 = td->td_frame;
310 int i;
311 int bpinstr = BREAK_SSTEP;
312 int curinstr;
313 struct proc *p;
314
315 p = td->td_proc;
316 PROC_UNLOCK(p);
317 /*
318 * Fetch what's at the current location.
319 */
320 ptrace_read_int(td, (off_t)locr0->pc, &curinstr);
321
322 /* compute next address after current location */
323 if(curinstr != 0) {
324 va = MipsEmulateBranch(locr0, locr0->pc, locr0->fsr,
325 (u_int)&curinstr);
326 } else {
327 va = locr0->pc + 4;
328 }
329 if (td->td_md.md_ss_addr) {
330 printf("SS %s (%d): breakpoint already set at %x (va %x)\n",
331 p->p_comm, p->p_pid, td->td_md.md_ss_addr, va); /* XXX */
332 return (EFAULT);
333 }
334 td->td_md.md_ss_addr = va;
335 /*
336 * Fetch what's at the current location.
337 */
338 ptrace_read_int(td, (off_t)va, &td->td_md.md_ss_instr);
339
340 /*
341 * Store breakpoint instruction at the "next" location now.
342 */
343 i = ptrace_write_int (td, va, bpinstr);
344
345 /*
346 * The sync'ing of I & D caches is done by procfs_domem()
347 * through procfs_rwmem().
348 */
349
350 PROC_LOCK(p);
351 if (i < 0)
352 return (EFAULT);
353 #if 0
354 printf("SS %s (%d): breakpoint set at %x: %x (pc %x) br %x\n",
355 p->p_comm, p->p_pid, p->p_md.md_ss_addr,
356 p->p_md.md_ss_instr, locr0->pc, curinstr); /* XXX */
357 #endif
358 return (0);
359 }
360
361
362 void
363 makectx(struct trapframe *tf, struct pcb *pcb)
364 {
365
366 pcb->pcb_regs.ra = tf->ra;
367 pcb->pcb_regs.pc = tf->pc;
368 pcb->pcb_regs.sp = tf->sp;
369 }
370
371 int
372 fill_regs(struct thread *td, struct reg *regs)
373 {
374 memcpy(regs, td->td_frame, sizeof(struct reg));
375 return (0);
376 }
377
378 int
379 set_regs(struct thread *td, struct reg *regs)
380 {
381 struct trapframe *f;
382 register_t sr;
383
384 f = (struct trapframe *) td->td_frame;
385 /*
386 * Don't allow the user to change SR
387 */
388 sr = f->sr;
389 memcpy(td->td_frame, regs, sizeof(struct reg));
390 f->sr = sr;
391 return (0);
392 }
393
394 int
395 get_mcontext(struct thread *td, mcontext_t *mcp, int flags)
396 {
397 struct trapframe *tp;
398
399 tp = td->td_frame;
400 PROC_LOCK(curthread->td_proc);
401 mcp->mc_onstack = sigonstack(tp->sp);
402 PROC_UNLOCK(curthread->td_proc);
403 bcopy((void *)&td->td_frame->zero, (void *)&mcp->mc_regs,
404 sizeof(mcp->mc_regs));
405
406 mcp->mc_fpused = td->td_md.md_flags & MDTD_FPUSED;
407 if (mcp->mc_fpused) {
408 bcopy((void *)&td->td_frame->f0, (void *)&mcp->mc_fpregs,
409 sizeof(mcp->mc_fpregs));
410 }
411 mcp->mc_pc = td->td_frame->pc;
412 mcp->mullo = td->td_frame->mullo;
413 mcp->mulhi = td->td_frame->mulhi;
414 return (0);
415 }
416
417 int
418 set_mcontext(struct thread *td, const mcontext_t *mcp)
419 {
420 struct trapframe *tp;
421
422 tp = td->td_frame;
423 bcopy((void *)&mcp->mc_regs, (void *)&td->td_frame->zero,
424 sizeof(mcp->mc_regs));
425
426 td->td_md.md_flags = mcp->mc_fpused & MDTD_FPUSED;
427 if (mcp->mc_fpused) {
428 bcopy((void *)&mcp->mc_fpregs, (void *)&td->td_frame->f0,
429 sizeof(mcp->mc_fpregs));
430 }
431 td->td_frame->pc = mcp->mc_pc;
432 td->td_frame->mullo = mcp->mullo;
433 td->td_frame->mulhi = mcp->mulhi;
434 /* Dont let user to set any bits in Status and casue registers */
435
436 return (0);
437 }
438
439 int
440 fill_fpregs(struct thread *td, struct fpreg *fpregs)
441 {
442 if (td == PCPU_GET(fpcurthread))
443 MipsSaveCurFPState(td);
444 memcpy(fpregs, &td->td_frame->f0, sizeof(struct fpreg));
445 return 0;
446 }
447
448 int
449 set_fpregs(struct thread *td, struct fpreg *fpregs)
450 {
451 if (PCPU_GET(fpcurthread) == td)
452 PCPU_SET(fpcurthread, (struct thread *)0);
453 memcpy(&td->td_frame->f0, fpregs, sizeof(struct fpreg));
454 return 0;
455 }
456
457
458 /*
459 * Clear registers on exec
460 * $sp is set to the stack pointer passed in. $pc is set to the entry
461 * point given by the exec_package passed in, as is $t9 (used for PIC
462 * code by the MIPS elf abi).
463 */
464 void
465 exec_setregs(struct thread *td, u_long entry, u_long stack, u_long ps_strings)
466 {
467
468 bzero((caddr_t)td->td_frame, sizeof(struct trapframe));
469
470 /*
471 * Make sp 64-bit aligned.
472 */
473 td->td_frame->sp = ((register_t) stack) & ~(sizeof(__int64_t) - 1);
474 td->td_frame->pc = entry & ~3;
475 td->td_frame->t9 = entry & ~3; /* abicall req */
476 #if 0
477 // td->td_frame->sr = SR_KSU_USER | SR_EXL | SR_INT_ENAB;
478 //? td->td_frame->sr |= idle_mask & ALL_INT_MASK;
479 #else
480 td->td_frame->sr = SR_KSU_USER | SR_EXL;// mips2 also did COP_0_BIT
481 #endif
482 #ifdef TARGET_OCTEON
483 td->td_frame->sr |= MIPS_SR_COP_2_BIT | MIPS32_SR_PX | MIPS_SR_UX |
484 MIPS_SR_KX | MIPS_SR_SX;
485 #endif
486 /*
487 * FREEBSD_DEVELOPERS_FIXME:
488 * Setup any other CPU-Specific registers (Not MIPS Standard)
489 * and/or bits in other standard MIPS registers (if CPU-Specific)
490 * that are needed.
491 */
492
493 /*
494 * Set up arguments for the rtld-capable crt0:
495 * a0 stack pointer
496 * a1 rtld cleanup (filled in by dynamic loader)
497 * a2 rtld object (filled in by dynamic loader)
498 * a3 ps_strings
499 */
500 td->td_frame->a0 = (register_t) stack;
501 td->td_frame->a1 = 0;
502 td->td_frame->a2 = 0;
503 td->td_frame->a3 = (register_t)ps_strings;
504
505 td->td_md.md_flags &= ~MDTD_FPUSED;
506 if (PCPU_GET(fpcurthread) == td)
507 PCPU_SET(fpcurthread, (struct thread *)0);
508 td->td_md.md_ss_addr = 0;
509 }
510
511 int
512 ptrace_clear_single_step(struct thread *td)
513 {
514 int i;
515 struct proc *p;
516
517 p = td->td_proc;
518 PROC_LOCK_ASSERT(p, MA_OWNED);
519 if (!td->td_md.md_ss_addr)
520 return EINVAL;
521
522 /*
523 * Restore original instruction and clear BP
524 */
525 i = ptrace_write_int (td, td->td_md.md_ss_addr, td->td_md.md_ss_instr);
526
527 /* The sync'ing of I & D caches is done by procfs_domem(). */
528
529 if (i < 0) {
530 log(LOG_ERR, "SS %s %d: can't restore instruction at %x: %x\n",
531 p->p_comm, p->p_pid, td->td_md.md_ss_addr,
532 td->td_md.md_ss_instr);
533 }
534 td->td_md.md_ss_addr = 0;
535 return 0;
536 }
Cache object: 085e7f61d1d3fe345c87bc9c14d46dd7
|