2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2005 Antoine Brodin
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/types.h>
33 #include <sys/systm.h>
34 #include <sys/param.h>
36 #include <sys/stack.h>
38 #include <machine/mips_opcode.h>
40 #include <machine/pcb.h>
41 #include <machine/regnum.h>
44 stack_register_fetch(u_register_t sp, u_register_t stack_pos)
46 u_register_t * stack =
47 ((u_register_t *)(intptr_t)sp + (size_t)stack_pos/sizeof(u_register_t));
53 stack_capture(struct stack *st, u_register_t pc, u_register_t sp)
55 u_register_t ra = 0, i, stacksize;
56 short ra_stack_pos = 0;
63 if (pc <= (u_register_t)(intptr_t)btext)
65 for (i = pc; i >= (u_register_t)(intptr_t)btext; i -= sizeof (insn)) {
66 bcopy((void *)(intptr_t)i, &insn, sizeof insn);
67 switch (insn.IType.op) {
72 if (insn.IType.rs != SP || insn.IType.rt != SP)
74 stacksize = -(short)insn.IType.imm;
79 if (insn.IType.rs != SP || insn.IType.rt != RA)
81 ra_stack_pos = (short)insn.IType.imm;
91 if (stack_put(st, pc) == -1)
94 for (i = pc; !ra; i += sizeof (insn)) {
95 bcopy((void *)(intptr_t)i, &insn, sizeof insn);
97 switch (insn.IType.op) {
99 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_td_running(struct stack *st, struct thread *td)
153 stack_save(struct stack *st)
157 if (curthread == NULL)
158 panic("stack_save: curthread == NULL");
160 pc = curthread->td_pcb->pcb_regs.pc;
161 sp = curthread->td_pcb->pcb_regs.sp;
162 stack_capture(st, pc, sp);