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/param.h>
33 #include <sys/systm.h>
35 #include <sys/mutex.h>
37 #include <sys/stack.h>
39 #include <machine/mips_opcode.h>
41 #include <machine/pcb.h>
42 #include <machine/regnum.h>
44 #define VALID_PC(addr) ((addr) >= (uintptr_t)btext && (addr) % 4 == 0)
47 stack_capture(struct stack *st, struct thread *td, uintptr_t pc, uintptr_t sp)
51 int ra_stack_pos, stacksize;
61 * Walk backward from the PC looking for the function
62 * start. Assume a subtraction from SP is the start
63 * of a function. Hope that we find the store of RA
64 * into the stack frame along the way and save the
65 * offset of the saved RA relative to SP.
69 for (i = pc; VALID_PC(i); i -= sizeof(insn)) {
70 bcopy((void *)i, &insn, sizeof(insn));
71 switch (insn.IType.op) {
76 if (insn.IType.rs != SP || insn.IType.rt != SP)
80 * Ignore stack fixups in "early"
81 * returns in a function, or if the
82 * call was from an unlikely branch
83 * moved after the end of the normal
86 if ((short)insn.IType.imm > 0)
89 stacksize = -(short)insn.IType.imm;
94 if (insn.IType.rs != SP || insn.IType.rt != RA)
96 ra_stack_pos = (short)insn.IType.imm;
106 if (stack_put(st, pc) == -1)
109 if (ra_stack_pos == -1)
113 * Walk forward from the PC to find the function end
114 * (jr RA). If eret is hit instead, stop unwinding.
116 ra_addr = sp + ra_stack_pos;
118 for (i = pc; VALID_PC(i); i += sizeof(insn)) {
119 bcopy((void *)i, &insn, sizeof(insn));
121 switch (insn.IType.op) {
123 if (insn.RType.func == OP_JR) {
124 if (insn.RType.rs != RA)
126 if (!kstack_contains(td, ra_addr,
129 ra = *(u_register_t *)ra_addr;
140 if (insn.word == 0x42000018)
147 if (pc == ra && stacksize == 0)
158 stack_save_td(struct stack *st, struct thread *td)
162 THREAD_LOCK_ASSERT(td, MA_OWNED);
163 KASSERT(!TD_IS_SWAPPED(td),
164 ("stack_save_td: thread %p is swapped", td));
166 if (TD_IS_RUNNING(td))
169 pc = td->td_pcb->pcb_context[PCB_REG_RA];
170 sp = td->td_pcb->pcb_context[PCB_REG_SP];
171 stack_capture(st, td, pc, sp);
176 stack_save(struct stack *st)
180 pc = (uintptr_t)&&here;
181 sp = (uintptr_t)__builtin_frame_address(0);
183 stack_capture(st, curthread, pc, sp);