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/pcb.h>
39 #include <machine/regnum.h>
42 stack_register_fetch(u_register_t sp, u_register_t stack_pos)
44 u_register_t * stack =
45 ((u_register_t *)(intptr_t)sp + (size_t)stack_pos/sizeof(u_register_t));
51 stack_capture(struct stack *st, u_register_t pc, u_register_t sp)
53 u_register_t ra = 0, i, stacksize;
54 short ra_stack_pos = 0;
61 if (pc <= (u_register_t)(intptr_t)btext)
63 for (i = pc; i >= (u_register_t)(intptr_t)btext; i -= sizeof (insn)) {
64 bcopy((void *)(intptr_t)i, &insn, sizeof insn);
65 switch (insn.IType.op) {
70 if (insn.IType.rs != SP || insn.IType.rt != SP)
72 stacksize = -(short)insn.IType.imm;
77 if (insn.IType.rs != SP || insn.IType.rt != RA)
79 ra_stack_pos = (short)insn.IType.imm;
89 if (stack_put(st, pc) == -1)
92 for (i = pc; !ra; i += sizeof (insn)) {
93 bcopy((void *)(intptr_t)i, &insn, sizeof insn);
95 switch (insn.IType.op) {
97 if((insn.RType.func == OP_JR))
99 if (ra >= (u_register_t)(intptr_t)btext)
101 if (insn.RType.rs != RA)
103 ra = stack_register_fetch(sp,
114 if (insn.word == 0x42000018)
118 if (pc == ra && stacksize == 0)
130 stack_save_td(struct stack *st, struct thread *td)
134 if (TD_IS_SWAPPED(td))
135 panic("stack_save_td: swapped");
136 if (TD_IS_RUNNING(td))
137 panic("stack_save_td: running");
139 pc = td->td_pcb->pcb_regs.pc;
140 sp = td->td_pcb->pcb_regs.sp;
141 stack_capture(st, pc, sp);
145 stack_save_td_running(struct stack *st, struct thread *td)
152 stack_save(struct stack *st)
156 if (curthread == NULL)
157 panic("stack_save: curthread == NULL");
159 pc = curthread->td_pcb->pcb_regs.pc;
160 sp = curthread->td_pcb->pcb_regs.sp;
161 stack_capture(st, pc, sp);