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