stack_machdep.c revision 209975
1219820Sjeff/*-
2219820Sjeff * Copyright (c) 2005 Antoine Brodin
3219820Sjeff * All rights reserved.
4219820Sjeff *
5219820Sjeff * Redistribution and use in source and binary forms, with or without
6219820Sjeff * modification, are permitted provided that the following conditions
7219820Sjeff * are met:
8219820Sjeff * 1. Redistributions of source code must retain the above copyright
9219820Sjeff *    notice, this list of conditions and the following disclaimer.
10219820Sjeff * 2. Redistributions in binary form must reproduce the above copyright
11219820Sjeff *    notice, this list of conditions and the following disclaimer in the
12219820Sjeff *    documentation and/or other materials provided with the distribution.
13219820Sjeff *
14219820Sjeff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15219820Sjeff * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16219820Sjeff * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17219820Sjeff * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18219820Sjeff * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19219820Sjeff * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20219820Sjeff * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21219820Sjeff * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22219820Sjeff * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23219820Sjeff * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24219820Sjeff * SUCH DAMAGE.
25219820Sjeff */
26219820Sjeff
27219820Sjeff#include <sys/cdefs.h>
28219820Sjeff__FBSDID("$FreeBSD: head/sys/powerpc/powerpc/stack_machdep.c 209975 2010-07-13 05:32:19Z nwhitehorn $");
29219820Sjeff
30219820Sjeff#include <sys/param.h>
31219820Sjeff#include <sys/proc.h>
32219820Sjeff#include <sys/stack.h>
33219820Sjeff#include <sys/systm.h>
34219820Sjeff
35219820Sjeff#include <vm/vm.h>
36219820Sjeff#include <vm/pmap.h>
37219820Sjeff#include <vm/vm_extern.h>
38219820Sjeff
39219820Sjeff#include <machine/db_machdep.h>
40219820Sjeff#include <machine/pcb.h>
41219820Sjeff#include <machine/spr.h>
42219820Sjeff#include <machine/stack.h>
43219820Sjeff#include <machine/trap.h>
44219820Sjeff
45219820Sjeff#ifdef __powerpc64__
46219820Sjeff#define CALLOFFSET 8 /* Account for the TOC reload slot */
47219820Sjeff#else
48219820Sjeff#define CALLOFFSET 4
49219820Sjeff#endif
50219820Sjeff
51219820Sjeffstatic void
52219820Sjeffstack_capture(struct stack *st, vm_offset_t frame)
53219820Sjeff{
54219820Sjeff	vm_offset_t callpc;
55219820Sjeff
56219820Sjeff	stack_zero(st);
57219820Sjeff	if (frame < PAGE_SIZE)
58		return;
59	while (1) {
60		frame = *(vm_offset_t *)frame;
61		if (frame < PAGE_SIZE)
62			break;
63
64	    #ifdef __powerpc64__
65		callpc = *(vm_offset_t *)(frame + 16) - 4;
66	    #else
67		callpc = *(vm_offset_t *)(frame + 4) - 4;
68	    #endif
69		if ((callpc & 3) || (callpc < 0x100))
70			break;
71
72		/*
73		 * Don't bother traversing trap-frames - there should
74		 * be enough info down to the frame to work out where
75		 * things are going wrong. Plus, prevents this shortened
76		 * version of code from accessing user-space frames
77		 */
78		if (callpc + CALLOFFSET == (vm_offset_t) &trapexit ||
79		    callpc + CALLOFFSET == (vm_offset_t) &asttrapexit)
80			break;
81
82		if (stack_put(st, callpc) == -1)
83			break;
84	}
85}
86
87void
88stack_save_td(struct stack *st, struct thread *td)
89{
90	vm_offset_t frame;
91
92	if (TD_IS_SWAPPED(td))
93		panic("stack_save_td: swapped");
94	if (TD_IS_RUNNING(td))
95		panic("stack_save_td: running");
96
97	frame = td->td_pcb->pcb_sp;
98	stack_capture(st, frame);
99}
100
101void
102stack_save(struct stack *st)
103{
104	register_t frame;
105
106	frame = (register_t)__builtin_frame_address(1);
107	stack_capture(st, frame);
108}
109