]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/mips/mips/pm_machdep.c
Merge clang 3.5.0 release from ^/vendor/clang/dist, resolve conflicts,
[FreeBSD/FreeBSD.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
43 #include <sys/types.h>
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/sysent.h>
47 #include <sys/proc.h>
48 #include <sys/signalvar.h>
49 #include <sys/exec.h>
50 #include <sys/imgact.h>
51 #include <sys/ucontext.h>
52 #include <sys/lock.h>
53 #include <sys/syscallsubr.h>
54 #include <sys/sysproto.h>
55 #include <sys/ptrace.h>
56 #include <sys/syslog.h>
57 #include <vm/vm.h>
58 #include <vm/pmap.h>
59 #include <vm/vm_map.h>
60 #include <vm/vm_extern.h>
61 #include <sys/user.h>
62 #include <sys/uio.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>
70
71 #define UCONTEXT_MAGIC  0xACEDBADE
72
73 /*
74  * Send an interrupt to process.
75  *
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
81  * specified pc, psl.
82  */
83 void
84 sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
85 {
86         struct proc *p;
87         struct thread *td;
88         struct trapframe *regs;
89         struct sigacts *psp;
90         struct sigframe sf, *sfp;
91         int sig;
92         int oonstack;
93
94         td = curthread;
95         p = td->td_proc;
96         PROC_LOCK_ASSERT(p, MA_OWNED);
97         sig = ksi->ksi_signo;
98         psp = p->p_sigacts;
99         mtx_assert(&psp->ps_mtx, MA_OWNED);
100
101         regs = td->td_frame;
102         oonstack = sigonstack(regs->sp);
103
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 *)&regs->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));
124         }
125
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));
132         } else
133                 sfp = (struct sigframe *)((vm_offset_t)(regs->sp - 
134                     sizeof(struct sigframe)) & ~(sizeof(__int64_t) - 1));
135
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)];
140         }
141
142         /* Build the argument list for the signal handler. */
143         regs->a0 = sig;
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; */
149
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;
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         struct iovec iov;
224         struct uio uio;
225
226         PROC_LOCK_ASSERT(td->td_proc, MA_NOTOWNED);
227         iov.iov_base = (caddr_t) v;
228         iov.iov_len = sizeof(int);
229         uio.uio_iov = &iov;
230         uio.uio_iovcnt = 1;
231         uio.uio_offset = (off_t)addr;
232         uio.uio_resid = sizeof(int);
233         uio.uio_segflg = UIO_SYSSPACE;
234         uio.uio_rw = UIO_READ;
235         uio.uio_td = td;
236         return proc_rwmem(td->td_proc, &uio);
237 }
238
239 static int
240 ptrace_write_int(struct thread *td, off_t addr, int v)
241 {
242         struct iovec iov;
243         struct uio uio;
244
245         PROC_LOCK_ASSERT(td->td_proc, MA_NOTOWNED);
246         iov.iov_base = (caddr_t) &v;
247         iov.iov_len = sizeof(int);
248         uio.uio_iov = &iov;
249         uio.uio_iovcnt = 1;
250         uio.uio_offset = (off_t)addr;
251         uio.uio_resid = sizeof(int);
252         uio.uio_segflg = UIO_SYSSPACE;
253         uio.uio_rw = UIO_WRITE;
254         uio.uio_td = td;
255         return proc_rwmem(td->td_proc, &uio);
256 }
257
258 int
259 ptrace_single_step(struct thread *td)
260 {
261         unsigned va;
262         struct trapframe *locr0 = td->td_frame;
263         int i;
264         int bpinstr = MIPS_BREAK_SSTEP;
265         int curinstr;
266         struct proc *p;
267
268         p = td->td_proc;
269         PROC_UNLOCK(p);
270         /*
271          * Fetch what's at the current location.
272          */
273         ptrace_read_int(td,  (off_t)locr0->pc, &curinstr);
274
275         /* compute next address after current location */
276         if(curinstr != 0) {
277                 va = MipsEmulateBranch(locr0, locr0->pc, locr0->fsr,
278                     (uintptr_t)&curinstr);
279         } else {
280                 va = locr0->pc + 4;
281         }
282         if (td->td_md.md_ss_addr) {
283                 printf("SS %s (%d): breakpoint already set at %x (va %x)\n",
284                     p->p_comm, p->p_pid, td->td_md.md_ss_addr, va); /* XXX */
285                 return (EFAULT);
286         }
287         td->td_md.md_ss_addr = va;
288         /*
289          * Fetch what's at the current location.
290          */
291         ptrace_read_int(td, (off_t)va, &td->td_md.md_ss_instr);
292
293         /*
294          * Store breakpoint instruction at the "next" location now.
295          */
296         i = ptrace_write_int (td, va, bpinstr);
297
298         /*
299          * The sync'ing of I & D caches is done by procfs_domem()
300          * through procfs_rwmem().
301          */
302
303         PROC_LOCK(p);
304         if (i < 0)
305                 return (EFAULT);
306 #if 0
307         printf("SS %s (%d): breakpoint set at %x: %x (pc %x) br %x\n",
308             p->p_comm, p->p_pid, p->p_md.md_ss_addr,
309             p->p_md.md_ss_instr, locr0->pc, curinstr); /* XXX */
310 #endif
311         return (0);
312 }
313
314
315 void
316 makectx(struct trapframe *tf, struct pcb *pcb)
317 {
318
319         pcb->pcb_regs.ra = tf->ra;
320         pcb->pcb_regs.pc = tf->pc;
321         pcb->pcb_regs.sp = tf->sp;
322 }
323
324 int
325 fill_regs(struct thread *td, struct reg *regs)
326 {
327         memcpy(regs, td->td_frame, sizeof(struct reg));
328         return (0);
329 }
330
331 int
332 set_regs(struct thread *td, struct reg *regs)
333 {
334         struct trapframe *f;
335         register_t sr;
336
337         f = (struct trapframe *) td->td_frame;
338         /*
339          * Don't allow the user to change SR
340          */
341         sr = f->sr;
342         memcpy(td->td_frame, regs, sizeof(struct reg));
343         f->sr = sr;
344         return (0);
345 }
346
347 int
348 get_mcontext(struct thread *td, mcontext_t *mcp, int flags)
349 {
350         struct trapframe *tp;
351
352         tp = td->td_frame;
353         PROC_LOCK(curthread->td_proc);
354         mcp->mc_onstack = sigonstack(tp->sp);
355         PROC_UNLOCK(curthread->td_proc);
356         bcopy((void *)&td->td_frame->zero, (void *)&mcp->mc_regs,
357             sizeof(mcp->mc_regs));
358
359         mcp->mc_fpused = td->td_md.md_flags & MDTD_FPUSED;
360         if (mcp->mc_fpused) {
361                 bcopy((void *)&td->td_frame->f0, (void *)&mcp->mc_fpregs,
362                     sizeof(mcp->mc_fpregs));
363         }
364         if (flags & GET_MC_CLEAR_RET) {
365                 mcp->mc_regs[V0] = 0;
366                 mcp->mc_regs[V1] = 0;
367                 mcp->mc_regs[A3] = 0;
368         }
369
370         mcp->mc_pc = td->td_frame->pc;
371         mcp->mullo = td->td_frame->mullo;
372         mcp->mulhi = td->td_frame->mulhi;
373         mcp->mc_tls = td->td_md.md_tls;
374         return (0);
375 }
376
377 int
378 set_mcontext(struct thread *td, const mcontext_t *mcp)
379 {
380         struct trapframe *tp;
381
382         tp = td->td_frame;
383         bcopy((void *)&mcp->mc_regs, (void *)&td->td_frame->zero,
384             sizeof(mcp->mc_regs));
385
386         td->td_md.md_flags = mcp->mc_fpused & MDTD_FPUSED;
387         if (mcp->mc_fpused) {
388                 bcopy((void *)&mcp->mc_fpregs, (void *)&td->td_frame->f0,
389                     sizeof(mcp->mc_fpregs));
390         }
391         td->td_frame->pc = mcp->mc_pc;
392         td->td_frame->mullo = mcp->mullo;
393         td->td_frame->mulhi = mcp->mulhi;
394         td->td_md.md_tls = mcp->mc_tls;
395         /* Dont let user to set any bits in status and cause registers. */
396
397         return (0);
398 }
399
400 int
401 fill_fpregs(struct thread *td, struct fpreg *fpregs)
402 {
403         if (td == PCPU_GET(fpcurthread))
404                 MipsSaveCurFPState(td);
405         memcpy(fpregs, &td->td_frame->f0, sizeof(struct fpreg)); 
406         return 0;
407 }
408
409 int
410 set_fpregs(struct thread *td, struct fpreg *fpregs)
411 {
412         if (PCPU_GET(fpcurthread) == td)
413                 PCPU_SET(fpcurthread, (struct thread *)0);
414         memcpy(&td->td_frame->f0, fpregs, sizeof(struct fpreg));
415         return 0;
416 }
417
418
419 /*
420  * Clear registers on exec
421  * $sp is set to the stack pointer passed in.  $pc is set to the entry
422  * point given by the exec_package passed in, as is $t9 (used for PIC
423  * code by the MIPS elf abi).
424  */
425 void
426 exec_setregs(struct thread *td, struct image_params *imgp, u_long stack)
427 {
428
429         bzero((caddr_t)td->td_frame, sizeof(struct trapframe));
430
431         /*
432          * The stack pointer has to be aligned to accommodate the largest
433          * datatype at minimum.  This probably means it should be 16-byte
434          * aligned, but for now we're 8-byte aligning it.
435          */
436         td->td_frame->sp = ((register_t) stack) & ~(sizeof(__int64_t) - 1);
437
438         /*
439          * If we're running o32 or n32 programs but have 64-bit registers,
440          * GCC may use stack-relative addressing near the top of user
441          * address space that, due to sign extension, will yield an
442          * invalid address.  For instance, if sp is 0x7fffff00 then GCC
443          * might do something like this to load a word from 0x7ffffff0:
444          *
445          *      addu    sp, sp, 32768
446          *      lw      t0, -32528(sp)
447          *
448          * On systems with 64-bit registers, sp is sign-extended to
449          * 0xffffffff80007f00 and the load is instead done from
450          * 0xffffffff7ffffff0.
451          *
452          * To prevent this, we subtract 64K from the stack pointer here.
453          *
454          * For consistency, we should just always do this unless we're
455          * running n64 programs.  For now, since we don't support
456          * COMPAT_FREEBSD32 on n64 kernels, we just do it unless we're
457          * running n64 kernels.
458          */
459 #if !defined(__mips_n64)
460         td->td_frame->sp -= 65536;
461 #endif
462
463         td->td_frame->pc = imgp->entry_addr & ~3;
464         td->td_frame->t9 = imgp->entry_addr & ~3; /* abicall req */
465         td->td_frame->sr = MIPS_SR_KSU_USER | MIPS_SR_EXL | MIPS_SR_INT_IE |
466             (mips_rd_status() & MIPS_SR_INT_MASK);
467 #if defined(__mips_n32) 
468         td->td_frame->sr |= MIPS_SR_PX;
469 #elif  defined(__mips_n64)
470         td->td_frame->sr |= MIPS_SR_PX | MIPS_SR_UX | MIPS_SR_KX;
471 #endif
472         /*
473          * FREEBSD_DEVELOPERS_FIXME:
474          * Setup any other CPU-Specific registers (Not MIPS Standard)
475          * and/or bits in other standard MIPS registers (if CPU-Specific)
476          *  that are needed.
477          */
478
479         /*
480          * Set up arguments for the rtld-capable crt0:
481          *      a0      stack pointer
482          *      a1      rtld cleanup (filled in by dynamic loader)
483          *      a2      rtld object (filled in by dynamic loader)
484          *      a3      ps_strings
485          */
486         td->td_frame->a0 = (register_t) stack;
487         td->td_frame->a1 = 0;
488         td->td_frame->a2 = 0;
489         td->td_frame->a3 = (register_t)imgp->ps_strings;
490
491         td->td_md.md_flags &= ~MDTD_FPUSED;
492         if (PCPU_GET(fpcurthread) == td)
493             PCPU_SET(fpcurthread, (struct thread *)0);
494         td->td_md.md_ss_addr = 0;
495 }
496
497 int
498 ptrace_clear_single_step(struct thread *td)
499 {
500         int i;
501         struct proc *p;
502
503         p = td->td_proc;
504         PROC_LOCK_ASSERT(p, MA_OWNED);
505         if (!td->td_md.md_ss_addr)
506                 return EINVAL;
507
508         /*
509          * Restore original instruction and clear BP
510          */
511         i = ptrace_write_int (td, td->td_md.md_ss_addr, td->td_md.md_ss_instr);
512
513         /* The sync'ing of I & D caches is done by procfs_domem(). */
514
515         if (i < 0) {
516                 log(LOG_ERR, "SS %s %d: can't restore instruction at %x: %x\n",
517                     p->p_comm, p->p_pid, td->td_md.md_ss_addr,
518                     td->td_md.md_ss_instr);
519         }
520         td->td_md.md_ss_addr = 0;
521         return 0;
522 }