]> CyberLeo.Net >> Repos - FreeBSD/releng/8.0.git/blob - sys/mips/mips/pm_machdep.c
Adjust to reflect 8.0-RELEASE.
[FreeBSD/releng/8.0.git] / sys / mips / mips / pm_machdep.c
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$");
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 *)&regs->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         const ucontext_t *ucp;
217         struct proc *p;
218         ucontext_t uc;
219         int error;
220
221         ucp = &uc;
222         p = td->td_proc;
223
224         error = copyin(uap->sigcntxp, &uc, sizeof(uc));
225         if (error != 0)
226             return (error);
227
228         regs = td->td_frame;
229
230 /* #ifdef DEBUG */
231         if (ucp->uc_mcontext.mc_regs[ZERO] != UCONTEXT_MAGIC) {
232                 printf("sigreturn: pid %d, ucp %p\n", p->p_pid, ucp);
233                 printf("  old sp %x ra %x pc %x\n",
234                     regs->sp, regs->ra, regs->pc);
235                 printf("  new sp %x ra %x pc %x z %x\n",
236                     ucp->uc_mcontext.mc_regs[SP],
237                     ucp->uc_mcontext.mc_regs[RA],
238                     ucp->uc_mcontext.mc_regs[PC],
239                     ucp->uc_mcontext.mc_regs[ZERO]);
240                 return EINVAL;
241         }
242 /* #endif */
243
244         bcopy((const void *)&ucp->uc_mcontext.mc_regs[1], (void *)&regs->ast,
245             sizeof(ucp->uc_mcontext.mc_regs) - sizeof(register_t));
246
247         if (ucp->uc_mcontext.mc_fpused)
248                 bcopy((const void *)ucp->uc_mcontext.mc_fpregs,
249                     (void *)&td->td_frame->f0,
250                     sizeof(ucp->uc_mcontext.mc_fpregs));
251
252         regs->pc = ucp->uc_mcontext.mc_pc;
253         regs->mullo = ucp->uc_mcontext.mullo;
254         regs->mulhi = ucp->uc_mcontext.mulhi;
255
256         PROC_LOCK(p);
257         td->td_sigmask = ucp->uc_sigmask;
258         SIG_CANTMASK(td->td_sigmask);
259         signotify(td);
260         PROC_UNLOCK(p);
261         return(EJUSTRETURN);
262 }
263
264
265 int
266 ptrace_set_pc(struct thread *td, unsigned long addr)
267 {
268         td->td_frame->pc = (register_t) addr;
269         return 0;
270 }
271
272 static int
273 ptrace_read_int(struct thread *td, off_t addr, int *v)
274 {
275         struct iovec iov;
276         struct uio uio;
277
278         PROC_LOCK_ASSERT(td->td_proc, MA_NOTOWNED);
279         iov.iov_base = (caddr_t) v;
280         iov.iov_len = sizeof(int);
281         uio.uio_iov = &iov;
282         uio.uio_iovcnt = 1;
283         uio.uio_offset = (off_t)addr;
284         uio.uio_resid = sizeof(int);
285         uio.uio_segflg = UIO_SYSSPACE;
286         uio.uio_rw = UIO_READ;
287         uio.uio_td = td;
288         return proc_rwmem(td->td_proc, &uio);
289 }
290
291 static int
292 ptrace_write_int(struct thread *td, off_t addr, int v)
293 {
294         struct iovec iov;
295         struct uio uio;
296
297         PROC_LOCK_ASSERT(td->td_proc, MA_NOTOWNED);
298         iov.iov_base = (caddr_t) &v;
299         iov.iov_len = sizeof(int);
300         uio.uio_iov = &iov;
301         uio.uio_iovcnt = 1;
302         uio.uio_offset = (off_t)addr;
303         uio.uio_resid = sizeof(int);
304         uio.uio_segflg = UIO_SYSSPACE;
305         uio.uio_rw = UIO_WRITE;
306         uio.uio_td = td;
307         return proc_rwmem(td->td_proc, &uio);
308 }
309
310 int
311 ptrace_single_step(struct thread *td)
312 {
313         unsigned va;
314         struct trapframe *locr0 = td->td_frame;
315         int i;
316         int bpinstr = BREAK_SSTEP;
317         int curinstr;
318         struct proc *p;
319
320         p = td->td_proc;
321         PROC_UNLOCK(p);
322         /*
323          * Fetch what's at the current location.
324          */
325         ptrace_read_int(td,  (off_t)locr0->pc, &curinstr);
326
327         /* compute next address after current location */
328         if(curinstr != 0) {
329                 va = MipsEmulateBranch(locr0, locr0->pc, locr0->fsr,
330                     (u_int)&curinstr);
331         } else {
332                 va = locr0->pc + 4;
333         }
334         if (td->td_md.md_ss_addr) {
335                 printf("SS %s (%d): breakpoint already set at %x (va %x)\n",
336                     p->p_comm, p->p_pid, td->td_md.md_ss_addr, va); /* XXX */
337                 return (EFAULT);
338         }
339         td->td_md.md_ss_addr = va;
340         /*
341          * Fetch what's at the current location.
342          */
343         ptrace_read_int(td, (off_t)va, &td->td_md.md_ss_instr);
344
345         /*
346          * Store breakpoint instruction at the "next" location now.
347          */
348         i = ptrace_write_int (td, va, bpinstr);
349
350         /*
351          * The sync'ing of I & D caches is done by procfs_domem()
352          * through procfs_rwmem().
353          */
354
355         PROC_LOCK(p);
356         if (i < 0)
357                 return (EFAULT);
358 #if 0
359         printf("SS %s (%d): breakpoint set at %x: %x (pc %x) br %x\n",
360             p->p_comm, p->p_pid, p->p_md.md_ss_addr,
361             p->p_md.md_ss_instr, locr0->pc, curinstr); /* XXX */
362 #endif
363         return (0);
364 }
365
366
367 void
368 makectx(struct trapframe *tf, struct pcb *pcb)
369 {
370
371         pcb->pcb_regs.ra = tf->ra;
372         pcb->pcb_regs.pc = tf->pc;
373         pcb->pcb_regs.sp = tf->sp;
374 }
375
376 int
377 fill_regs(struct thread *td, struct reg *regs)
378 {
379         memcpy(regs, td->td_frame, sizeof(struct reg));
380         return (0);
381 }
382
383 int
384 set_regs(struct thread *td, struct reg *regs)
385 {
386         struct trapframe *f;
387         register_t sr;
388
389         f = (struct trapframe *) td->td_frame;
390         /*
391          * Don't allow the user to change SR
392          */
393         sr = f->sr;
394         memcpy(td->td_frame, regs, sizeof(struct reg));
395         f->sr = sr;
396         return (0);
397 }
398
399 int
400 get_mcontext(struct thread *td, mcontext_t *mcp, int flags)
401 {
402         struct trapframe *tp;
403
404         tp = td->td_frame;
405         PROC_LOCK(curthread->td_proc);
406         mcp->mc_onstack = sigonstack(tp->sp);
407         PROC_UNLOCK(curthread->td_proc);
408         bcopy((void *)&td->td_frame->zero, (void *)&mcp->mc_regs,
409             sizeof(mcp->mc_regs));
410
411         mcp->mc_fpused = td->td_md.md_flags & MDTD_FPUSED;
412         if (mcp->mc_fpused) {
413                 bcopy((void *)&td->td_frame->f0, (void *)&mcp->mc_fpregs,
414                     sizeof(mcp->mc_fpregs));
415         }
416         mcp->mc_pc = td->td_frame->pc;
417         mcp->mullo = td->td_frame->mullo;
418         mcp->mulhi = td->td_frame->mulhi;
419         return (0);
420 }
421
422 int
423 set_mcontext(struct thread *td, const mcontext_t *mcp)
424 {
425         struct trapframe *tp;
426
427         tp = td->td_frame;
428         bcopy((void *)&mcp->mc_regs, (void *)&td->td_frame->zero,
429             sizeof(mcp->mc_regs));
430
431         td->td_md.md_flags = mcp->mc_fpused & MDTD_FPUSED;
432         if (mcp->mc_fpused) {
433                 bcopy((void *)&mcp->mc_fpregs, (void *)&td->td_frame->f0,
434                     sizeof(mcp->mc_fpregs));
435         }
436         td->td_frame->pc = mcp->mc_pc;
437         td->td_frame->mullo = mcp->mullo;
438         td->td_frame->mulhi = mcp->mulhi;
439         /* Dont let user to set any bits in Status and casue registers */
440
441         return (0);
442 }
443
444 int
445 fill_fpregs(struct thread *td, struct fpreg *fpregs)
446 {
447         if (td == PCPU_GET(fpcurthread))
448                 MipsSaveCurFPState(td);
449         memcpy(fpregs, &td->td_frame->f0, sizeof(struct fpreg)); 
450         return 0;
451 }
452
453 int
454 set_fpregs(struct thread *td, struct fpreg *fpregs)
455 {
456         if (PCPU_GET(fpcurthread) == td)
457                 PCPU_SET(fpcurthread, (struct thread *)0);
458         memcpy(&td->td_frame->f0, fpregs, sizeof(struct fpreg));
459         return 0;
460 }
461
462
463 /*
464  * Clear registers on exec
465  * $sp is set to the stack pointer passed in.  $pc is set to the entry
466  * point given by the exec_package passed in, as is $t9 (used for PIC
467  * code by the MIPS elf abi).
468  */
469 void
470 exec_setregs(struct thread *td, u_long entry, u_long stack, u_long ps_strings)
471 {
472
473         bzero((caddr_t)td->td_frame, sizeof(struct trapframe));
474
475         /*
476          * Make sp 64-bit aligned.
477          */
478         td->td_frame->sp = ((register_t) stack) & ~(sizeof(__int64_t) - 1);
479         td->td_frame->pc = entry & ~3;
480         td->td_frame->t9 = entry & ~3; /* abicall req */
481 #if 0
482 //      td->td_frame->sr = SR_KSU_USER | SR_EXL | SR_INT_ENAB;
483 //?     td->td_frame->sr |=  idle_mask & ALL_INT_MASK;
484 #else
485         td->td_frame->sr = SR_KSU_USER | SR_EXL;// mips2 also did COP_0_BIT
486 #endif
487 #ifdef TARGET_OCTEON
488         td->td_frame->sr |= MIPS_SR_COP_2_BIT | MIPS32_SR_PX | MIPS_SR_UX |
489             MIPS_SR_KX | MIPS_SR_SX;
490 #endif
491         /*
492          * FREEBSD_DEVELOPERS_FIXME:
493          * Setup any other CPU-Specific registers (Not MIPS Standard)
494          * and/or bits in other standard MIPS registers (if CPU-Specific)
495          *  that are needed.
496          */
497
498         /*
499          * Set up arguments for the rtld-capable crt0:
500          *      a0      stack pointer
501          *      a1      rtld cleanup (filled in by dynamic loader)
502          *      a2      rtld object (filled in by dynamic loader)
503          *      a3      ps_strings
504          */
505         td->td_frame->a0 = (register_t) stack;
506         td->td_frame->a1 = 0;
507         td->td_frame->a2 = 0;
508         td->td_frame->a3 = (register_t)ps_strings;
509
510         td->td_md.md_flags &= ~MDTD_FPUSED;
511         if (PCPU_GET(fpcurthread) == td)
512             PCPU_SET(fpcurthread, (struct thread *)0);
513         td->td_md.md_ss_addr = 0;
514 }
515
516 int
517 ptrace_clear_single_step(struct thread *td)
518 {
519         int i;
520         struct proc *p;
521
522         p = td->td_proc;
523         PROC_LOCK_ASSERT(p, MA_OWNED);
524         if (!td->td_md.md_ss_addr)
525                 return EINVAL;
526
527         /*
528          * Restore original instruction and clear BP
529          */
530         i = ptrace_write_int (td, td->td_md.md_ss_addr, td->td_md.md_ss_instr);
531
532         /* The sync'ing of I & D caches is done by procfs_domem(). */
533
534         if (i < 0) {
535                 log(LOG_ERR, "SS %s %d: can't restore instruction at %x: %x\n",
536                     p->p_comm, p->p_pid, td->td_md.md_ss_addr,
537                     td->td_md.md_ss_instr);
538         }
539         td->td_md.md_ss_addr = 0;
540         return 0;
541 }