]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/mips/mips/pm_machdep.c
Rework printouts and logging level in ENA driver
[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  * 3. 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/cpuinfo.h>
64 #include <machine/reg.h>
65 #include <machine/md_var.h>
66 #include <machine/sigframe.h>
67 #include <machine/tls.h>
68 #include <machine/vmparam.h>
69 #include <sys/vnode.h>
70 #include <fs/pseudofs/pseudofs.h>
71 #include <fs/procfs/procfs.h>
72
73 #define UCONTEXT_MAGIC  0xACEDBADE
74
75 /*
76  * Send an interrupt to process.
77  *
78  * Stack is set up to allow sigcode stored
79  * at top to call routine, followed by kcall
80  * to sigreturn routine below.  After sigreturn
81  * resets the signal mask, the stack, and the
82  * frame pointer, it returns to the user
83  * specified pc, psl.
84  */
85 void
86 sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
87 {
88         struct proc *p;
89         struct thread *td;
90         struct trapframe *regs;
91         struct sigacts *psp;
92         struct sigframe sf, *sfp;
93         int sig;
94         int oonstack;
95
96         td = curthread;
97         p = td->td_proc;
98         PROC_LOCK_ASSERT(p, MA_OWNED);
99         sig = ksi->ksi_signo;
100         psp = p->p_sigacts;
101         mtx_assert(&psp->ps_mtx, MA_OWNED);
102
103         regs = td->td_frame;
104         oonstack = sigonstack(regs->sp);
105
106         /* save user context */
107         bzero(&sf, sizeof(struct sigframe));
108         sf.sf_uc.uc_sigmask = *mask;
109         sf.sf_uc.uc_stack = td->td_sigstk;
110         sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
111         sf.sf_uc.uc_mcontext.mc_pc = regs->pc;
112         sf.sf_uc.uc_mcontext.mullo = regs->mullo;
113         sf.sf_uc.uc_mcontext.mulhi = regs->mulhi;
114         sf.sf_uc.uc_mcontext.mc_tls = td->td_md.md_tls;
115         sf.sf_uc.uc_mcontext.mc_regs[0] = UCONTEXT_MAGIC;  /* magic number */
116         bcopy((void *)&regs->ast, (void *)&sf.sf_uc.uc_mcontext.mc_regs[1],
117             sizeof(sf.sf_uc.uc_mcontext.mc_regs) - sizeof(register_t));
118         sf.sf_uc.uc_mcontext.mc_fpused = td->td_md.md_flags & MDTD_FPUSED;
119         if (sf.sf_uc.uc_mcontext.mc_fpused) {
120                 /* if FPU has current state, save it first */
121                 if (td == PCPU_GET(fpcurthread))
122                         MipsSaveCurFPState(td);
123                 bcopy((void *)&td->td_frame->f0,
124                     (void *)sf.sf_uc.uc_mcontext.mc_fpregs,
125                     sizeof(sf.sf_uc.uc_mcontext.mc_fpregs));
126         }
127
128         /* Allocate and validate space for the signal handler context. */
129         if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
130             SIGISMEMBER(psp->ps_sigonstack, sig)) {
131                 sfp = (struct sigframe *)(((uintptr_t)td->td_sigstk.ss_sp +
132                     td->td_sigstk.ss_size - sizeof(struct sigframe))
133                     & ~(sizeof(__int64_t) - 1));
134         } else
135                 sfp = (struct sigframe *)((vm_offset_t)(regs->sp - 
136                     sizeof(struct sigframe)) & ~(sizeof(__int64_t) - 1));
137
138         /* Build the argument list for the signal handler. */
139         regs->a0 = sig;
140         regs->a2 = (register_t)(intptr_t)&sfp->sf_uc;
141         if (SIGISMEMBER(psp->ps_siginfo, sig)) {
142                 /* Signal handler installed with SA_SIGINFO. */
143                 regs->a1 = (register_t)(intptr_t)&sfp->sf_si;
144                 /* sf.sf_ahu.sf_action = (__siginfohandler_t *)catcher; */
145
146                 /* fill siginfo structure */
147                 sf.sf_si = ksi->ksi_info;
148                 sf.sf_si.si_signo = sig;
149                 sf.sf_si.si_code = ksi->ksi_code;
150                 sf.sf_si.si_addr = (void*)(intptr_t)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)(intptr_t)catcher;
174         regs->t9 = (register_t)(intptr_t)catcher;
175         regs->sp = (register_t)(intptr_t)sfp;
176         /*
177          * Signal trampoline code is at base of user stack.
178          */
179         regs->ra = (register_t)(intptr_t)PS_STRINGS - *(p->p_sysent->sv_szsigcode);
180         PROC_LOCK(p);
181         mtx_lock(&psp->ps_mtx);
182 }
183
184 /*
185  * System call to cleanup state after a signal
186  * has been taken.  Reset signal mask and
187  * stack state from context left by sendsig (above).
188  * Return to previous pc as specified by
189  * context left by sendsig.
190  */
191 int
192 sys_sigreturn(struct thread *td, struct sigreturn_args *uap)
193 {
194         ucontext_t uc;
195         int error;
196
197         error = copyin(uap->sigcntxp, &uc, sizeof(uc));
198         if (error != 0)
199             return (error);
200
201         error = set_mcontext(td, &uc.uc_mcontext);
202         if (error != 0)
203                 return (error);
204
205         kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask, NULL, 0);
206
207         return (EJUSTRETURN);
208 }
209
210 int
211 ptrace_set_pc(struct thread *td, unsigned long addr)
212 {
213         td->td_frame->pc = (register_t) addr;
214         return 0;
215 }
216
217 static int
218 ptrace_read_int(struct thread *td, off_t addr, int *v)
219 {
220
221         if (proc_readmem(td, td->td_proc, addr, v, sizeof(*v)) != sizeof(*v))
222                 return (ENOMEM);
223         return (0);
224 }
225
226 static int
227 ptrace_write_int(struct thread *td, off_t addr, int v)
228 {
229
230         if (proc_writemem(td, td->td_proc, addr, &v, sizeof(v)) != sizeof(v))
231                 return (ENOMEM);
232         return (0);
233 }
234
235 int
236 ptrace_single_step(struct thread *td)
237 {
238         unsigned va;
239         struct trapframe *locr0 = td->td_frame;
240         int i;
241         int bpinstr = MIPS_BREAK_SSTEP;
242         int curinstr;
243         struct proc *p;
244
245         p = td->td_proc;
246         PROC_UNLOCK(p);
247         /*
248          * Fetch what's at the current location.
249          */
250         ptrace_read_int(td,  (off_t)locr0->pc, &curinstr);
251
252         /* compute next address after current location */
253         if(curinstr != 0) {
254                 va = MipsEmulateBranch(locr0, locr0->pc, locr0->fsr,
255                     (uintptr_t)&curinstr);
256         } else {
257                 va = locr0->pc + 4;
258         }
259         if (td->td_md.md_ss_addr) {
260                 printf("SS %s (%d): breakpoint already set at %x (va %x)\n",
261                     p->p_comm, p->p_pid, td->td_md.md_ss_addr, va); /* XXX */
262                 return (EFAULT);
263         }
264         td->td_md.md_ss_addr = va;
265         /*
266          * Fetch what's at the current location.
267          */
268         ptrace_read_int(td, (off_t)va, &td->td_md.md_ss_instr);
269
270         /*
271          * Store breakpoint instruction at the "next" location now.
272          */
273         i = ptrace_write_int (td, va, bpinstr);
274
275         /*
276          * The sync'ing of I & D caches is done by procfs_domem()
277          * through procfs_rwmem().
278          */
279
280         PROC_LOCK(p);
281         if (i < 0)
282                 return (EFAULT);
283 #if 0
284         printf("SS %s (%d): breakpoint set at %x: %x (pc %x) br %x\n",
285             p->p_comm, p->p_pid, p->p_md.md_ss_addr,
286             p->p_md.md_ss_instr, locr0->pc, curinstr); /* XXX */
287 #endif
288         return (0);
289 }
290
291
292 void
293 makectx(struct trapframe *tf, struct pcb *pcb)
294 {
295
296         pcb->pcb_context[PCB_REG_RA] = tf->ra;
297         pcb->pcb_context[PCB_REG_PC] = tf->pc;
298         pcb->pcb_context[PCB_REG_SP] = tf->sp;
299 }
300
301 int
302 fill_regs(struct thread *td, struct reg *regs)
303 {
304         memcpy(regs, td->td_frame, sizeof(struct reg));
305         return (0);
306 }
307
308 int
309 set_regs(struct thread *td, struct reg *regs)
310 {
311         struct trapframe *f;
312         register_t sr;
313
314         f = (struct trapframe *) td->td_frame;
315         /*
316          * Don't allow the user to change SR
317          */
318         sr = f->sr;
319         memcpy(td->td_frame, regs, sizeof(struct reg));
320         f->sr = sr;
321         return (0);
322 }
323
324 int
325 get_mcontext(struct thread *td, mcontext_t *mcp, int flags)
326 {
327         struct trapframe *tp;
328
329         tp = td->td_frame;
330         PROC_LOCK(curthread->td_proc);
331         mcp->mc_onstack = sigonstack(tp->sp);
332         PROC_UNLOCK(curthread->td_proc);
333         bcopy((void *)&td->td_frame->zero, (void *)&mcp->mc_regs,
334             sizeof(mcp->mc_regs));
335
336         mcp->mc_fpused = td->td_md.md_flags & MDTD_FPUSED;
337         if (mcp->mc_fpused) {
338                 bcopy((void *)&td->td_frame->f0, (void *)&mcp->mc_fpregs,
339                     sizeof(mcp->mc_fpregs));
340         }
341         if (flags & GET_MC_CLEAR_RET) {
342                 mcp->mc_regs[V0] = 0;
343                 mcp->mc_regs[V1] = 0;
344                 mcp->mc_regs[A3] = 0;
345         }
346
347         mcp->mc_pc = td->td_frame->pc;
348         mcp->mullo = td->td_frame->mullo;
349         mcp->mulhi = td->td_frame->mulhi;
350         mcp->mc_tls = td->td_md.md_tls;
351         return (0);
352 }
353
354 int
355 set_mcontext(struct thread *td, mcontext_t *mcp)
356 {
357         struct trapframe *tp;
358
359         tp = td->td_frame;
360         bcopy((void *)&mcp->mc_regs, (void *)&td->td_frame->zero,
361             sizeof(mcp->mc_regs));
362
363         td->td_md.md_flags = mcp->mc_fpused & MDTD_FPUSED;
364         if (mcp->mc_fpused) {
365                 bcopy((void *)&mcp->mc_fpregs, (void *)&td->td_frame->f0,
366                     sizeof(mcp->mc_fpregs));
367         }
368         td->td_frame->pc = mcp->mc_pc;
369         td->td_frame->mullo = mcp->mullo;
370         td->td_frame->mulhi = mcp->mulhi;
371         td->td_md.md_tls = mcp->mc_tls;
372         /* Dont let user to set any bits in status and cause registers. */
373
374         return (0);
375 }
376
377 int
378 fill_fpregs(struct thread *td, struct fpreg *fpregs)
379 {
380         if (td == PCPU_GET(fpcurthread))
381                 MipsSaveCurFPState(td);
382         memcpy(fpregs, &td->td_frame->f0, sizeof(struct fpreg));
383         fpregs->r_regs[FIR_NUM] = cpuinfo.fpu_id;
384         return 0;
385 }
386
387 int
388 set_fpregs(struct thread *td, struct fpreg *fpregs)
389 {
390         if (PCPU_GET(fpcurthread) == td)
391                 PCPU_SET(fpcurthread, (struct thread *)0);
392         memcpy(&td->td_frame->f0, fpregs, sizeof(struct fpreg));
393         return 0;
394 }
395
396
397 /*
398  * Clear registers on exec
399  * $sp is set to the stack pointer passed in.  $pc is set to the entry
400  * point given by the exec_package passed in, as is $t9 (used for PIC
401  * code by the MIPS elf abi).
402  */
403 void
404 exec_setregs(struct thread *td, struct image_params *imgp, u_long stack)
405 {
406
407         bzero((caddr_t)td->td_frame, sizeof(struct trapframe));
408
409         /*
410          * The stack pointer has to be aligned to accommodate the largest
411          * datatype at minimum.  This probably means it should be 16-byte
412          * aligned, but for now we're 8-byte aligning it.
413          */
414         td->td_frame->sp = ((register_t) stack) & ~(sizeof(__int64_t) - 1);
415
416         /*
417          * If we're running o32 or n32 programs but have 64-bit registers,
418          * GCC may use stack-relative addressing near the top of user
419          * address space that, due to sign extension, will yield an
420          * invalid address.  For instance, if sp is 0x7fffff00 then GCC
421          * might do something like this to load a word from 0x7ffffff0:
422          *
423          *      addu    sp, sp, 32768
424          *      lw      t0, -32528(sp)
425          *
426          * On systems with 64-bit registers, sp is sign-extended to
427          * 0xffffffff80007f00 and the load is instead done from
428          * 0xffffffff7ffffff0.
429          *
430          * To prevent this, we subtract 64K from the stack pointer here
431          * for processes with 32-bit pointers.
432          */
433 #if defined(__mips_n32) || defined(__mips_n64)
434         if (!SV_PROC_FLAG(td->td_proc, SV_LP64))
435                 td->td_frame->sp -= 65536;
436 #endif
437
438         td->td_frame->pc = imgp->entry_addr & ~3;
439         td->td_frame->t9 = imgp->entry_addr & ~3; /* abicall req */
440         td->td_frame->sr = MIPS_SR_KSU_USER | MIPS_SR_EXL | MIPS_SR_INT_IE |
441             (mips_rd_status() & MIPS_SR_INT_MASK);
442 #if defined(__mips_n32) 
443         td->td_frame->sr |= MIPS_SR_PX;
444 #elif  defined(__mips_n64)
445         td->td_frame->sr |= MIPS_SR_PX | MIPS_SR_UX | MIPS_SR_KX;
446 #endif
447         /*
448          * FREEBSD_DEVELOPERS_FIXME:
449          * Setup any other CPU-Specific registers (Not MIPS Standard)
450          * and/or bits in other standard MIPS registers (if CPU-Specific)
451          *  that are needed.
452          */
453
454         /*
455          * Set up arguments for the rtld-capable crt0:
456          *      a0      stack pointer
457          *      a1      rtld cleanup (filled in by dynamic loader)
458          *      a2      rtld object (filled in by dynamic loader)
459          *      a3      ps_strings
460          */
461         td->td_frame->a0 = (register_t) stack;
462         td->td_frame->a1 = 0;
463         td->td_frame->a2 = 0;
464         td->td_frame->a3 = (register_t)imgp->ps_strings;
465
466         td->td_md.md_flags &= ~MDTD_FPUSED;
467         if (PCPU_GET(fpcurthread) == td)
468             PCPU_SET(fpcurthread, (struct thread *)0);
469         td->td_md.md_ss_addr = 0;
470
471         td->td_md.md_tls_tcb_offset = TLS_TP_OFFSET + TLS_TCB_SIZE;
472 }
473
474 int
475 ptrace_clear_single_step(struct thread *td)
476 {
477         int i;
478         struct proc *p;
479
480         p = td->td_proc;
481         PROC_LOCK_ASSERT(p, MA_OWNED);
482         if (!td->td_md.md_ss_addr)
483                 return EINVAL;
484
485         /*
486          * Restore original instruction and clear BP
487          */
488         i = ptrace_write_int (td, td->td_md.md_ss_addr, td->td_md.md_ss_instr);
489
490         /* The sync'ing of I & D caches is done by procfs_domem(). */
491
492         if (i < 0) {
493                 log(LOG_ERR, "SS %s %d: can't restore instruction at %x: %x\n",
494                     p->p_comm, p->p_pid, td->td_md.md_ss_addr,
495                     td->td_md.md_ss_instr);
496         }
497         td->td_md.md_ss_addr = 0;
498         return 0;
499 }