1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2005 Antoine Brodin
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
15 *
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
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD: releng/12.0/sys/mips/mips/stack_machdep.c 326259 2017-11-27 15:07:26Z pfg $");
31
32 #include <sys/types.h>
33 #include <sys/systm.h>
34 #include <sys/param.h>
35 #include <sys/proc.h>
36 #include <sys/stack.h>
37
38 #include <machine/mips_opcode.h>
39
40 #include <machine/pcb.h>
41 #include <machine/regnum.h>
42
43 static u_register_t
44 stack_register_fetch(u_register_t sp, u_register_t stack_pos)
45 {
46 u_register_t * stack =
47 ((u_register_t *)(intptr_t)sp + (size_t)stack_pos/sizeof(u_register_t));
48
49 return *stack;
50 }
51
52 static void
53 stack_capture(struct stack *st, u_register_t pc, u_register_t sp)
54 {
55 u_register_t ra = 0, i, stacksize;
56 short ra_stack_pos = 0;
57 InstFmt insn;
58
59 stack_zero(st);
60
61 for (;;) {
62 stacksize = 0;
63 if (pc <= (u_register_t)(intptr_t)btext)
64 break;
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) {
68 case OP_ADDI:
69 case OP_ADDIU:
70 case OP_DADDI:
71 case OP_DADDIU:
72 if (insn.IType.rs != SP || insn.IType.rt != SP)
73 break;
74 stacksize = -(short)insn.IType.imm;
75 break;
76
77 case OP_SW:
78 case OP_SD:
79 if (insn.IType.rs != SP || insn.IType.rt != RA)
80 break;
81 ra_stack_pos = (short)insn.IType.imm;
82 break;
83 default:
84 break;
85 }
86
87 if (stacksize)
88 break;
89 }
90
91 if (stack_put(st, pc) == -1)
92 break;
93
94 for (i = pc; !ra; i += sizeof (insn)) {
95 bcopy((void *)(intptr_t)i, &insn, sizeof insn);
96
97 switch (insn.IType.op) {
98 case OP_SPECIAL:
99 if (insn.RType.func == OP_JR) {
100 if (ra >= (u_register_t)(intptr_t)btext)
101 break;
102 if (insn.RType.rs != RA)
103 break;
104 ra = stack_register_fetch(sp,
105 ra_stack_pos);
106 if (!ra)
107 goto done;
108 ra -= 8;
109 }
110 break;
111 default:
112 break;
113 }
114 /* eret */
115 if (insn.word == 0x42000018)
116 goto done;
117 }
118
119 if (pc == ra && stacksize == 0)
120 break;
121
122 sp += stacksize;
123 pc = ra;
124 ra = 0;
125 }
126 done:
127 return;
128 }
129
130 void
131 stack_save_td(struct stack *st, struct thread *td)
132 {
133 u_register_t pc, sp;
134
135 if (TD_IS_SWAPPED(td))
136 panic("stack_save_td: swapped");
137 if (TD_IS_RUNNING(td))
138 panic("stack_save_td: running");
139
140 pc = td->td_pcb->pcb_regs.pc;
141 sp = td->td_pcb->pcb_regs.sp;
142 stack_capture(st, pc, sp);
143 }
144
145 int
146 stack_save_td_running(struct stack *st, struct thread *td)
147 {
148
149 return (EOPNOTSUPP);
150 }
151
152 void
153 stack_save(struct stack *st)
154 {
155 u_register_t pc, sp;
156
157 if (curthread == NULL)
158 panic("stack_save: curthread == NULL");
159
160 pc = curthread->td_pcb->pcb_regs.pc;
161 sp = curthread->td_pcb->pcb_regs.sp;
162 stack_capture(st, pc, sp);
163 }
Cache object: 09c8210753a2d2ab3540d2c12dbaa592
|