2 * Copyright (c) 2005 Antoine Brodin
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/types.h>
31 #include <sys/systm.h>
32 #include <sys/param.h>
34 #include <sys/stack.h>
36 #include <machine/mips_opcode.h>
38 #include <machine/param.h>
39 #include <machine/pcb.h>
40 #include <machine/regnum.h>
43 stack_register_fetch(u_register_t sp, u_register_t stack_pos)
45 u_register_t * stack =
46 ((u_register_t *)(intptr_t)sp + (size_t)stack_pos/sizeof(u_register_t));
52 stack_capture(struct stack *st, u_register_t pc, u_register_t sp)
54 u_register_t ra = 0, i, stacksize;
55 short ra_stack_pos = 0;
62 if (pc <= (u_register_t)(intptr_t)btext)
64 for (i = pc; i >= (u_register_t)(intptr_t)btext; i -= sizeof (insn)) {
65 bcopy((void *)(intptr_t)i, &insn, sizeof insn);
66 switch (insn.IType.op) {
71 if (insn.IType.rs != SP || insn.IType.rt != SP)
73 stacksize = -(short)insn.IType.imm;
78 if (insn.IType.rs != SP || insn.IType.rt != RA)
80 ra_stack_pos = (short)insn.IType.imm;
90 if (stack_put(st, pc) == -1)
93 for (i = pc; !ra; i += sizeof (insn)) {
94 bcopy((void *)(intptr_t)i, &insn, sizeof insn);
96 switch (insn.IType.op) {
98 if((insn.RType.func == OP_JR))
100 if (ra >= (u_register_t)(intptr_t)btext)
102 if (insn.RType.rs != RA)
104 ra = stack_register_fetch(sp,
115 if (insn.word == 0x42000018)
119 if (pc == ra && stacksize == 0)
131 stack_save_td(struct stack *st, struct thread *td)
135 if (TD_IS_SWAPPED(td))
136 panic("stack_save_td: swapped");
137 if (TD_IS_RUNNING(td))
138 panic("stack_save_td: running");
140 pc = td->td_pcb->pcb_regs.pc;
141 sp = td->td_pcb->pcb_regs.sp;
142 stack_capture(st, pc, sp);
146 stack_save(struct stack *st)
150 if (curthread == NULL)
151 panic("stack_save: curthread == NULL)");
153 pc = curthread->td_pcb->pcb_regs.pc;
154 sp = curthread->td_pcb->pcb_regs.sp;
155 stack_capture(st, pc, sp);