1174195Srwatson/*- 2174195Srwatson * Copyright (c) 2005 Antoine Brodin 3174195Srwatson * All rights reserved. 4174195Srwatson * 5174195Srwatson * Redistribution and use in source and binary forms, with or without 6174195Srwatson * modification, are permitted provided that the following conditions 7174195Srwatson * are met: 8174195Srwatson * 1. Redistributions of source code must retain the above copyright 9174195Srwatson * notice, this list of conditions and the following disclaimer. 10174195Srwatson * 2. Redistributions in binary form must reproduce the above copyright 11174195Srwatson * notice, this list of conditions and the following disclaimer in the 12174195Srwatson * documentation and/or other materials provided with the distribution. 13174195Srwatson * 14174195Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15174195Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16174195Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17174195Srwatson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18174195Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19174195Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20174195Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21174195Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22174195Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23174195Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24174195Srwatson * SUCH DAMAGE. 25174195Srwatson */ 26174195Srwatson 27174195Srwatson#include <sys/cdefs.h> 28174195Srwatson__FBSDID("$FreeBSD$"); 29174195Srwatson 30174195Srwatson#include <sys/param.h> 31174195Srwatson#include <sys/proc.h> 32174195Srwatson#include <sys/stack.h> 33174195Srwatson#include <sys/systm.h> 34174195Srwatson 35174195Srwatson#include <vm/vm.h> 36174195Srwatson#include <vm/pmap.h> 37174195Srwatson#include <vm/vm_extern.h> 38174195Srwatson 39174195Srwatson#include <machine/db_machdep.h> 40174195Srwatson#include <machine/pcb.h> 41174195Srwatson#include <machine/spr.h> 42174195Srwatson#include <machine/stack.h> 43174195Srwatson#include <machine/trap.h> 44174195Srwatson 45209975Snwhitehorn#ifdef __powerpc64__ 46209975Snwhitehorn#define CALLOFFSET 8 /* Account for the TOC reload slot */ 47209975Snwhitehorn#else 48209975Snwhitehorn#define CALLOFFSET 4 49209975Snwhitehorn#endif 50209975Snwhitehorn 51174195Srwatsonstatic void 52198678Snwhitehornstack_capture(struct stack *st, vm_offset_t frame) 53174195Srwatson{ 54174195Srwatson vm_offset_t callpc; 55174195Srwatson 56174195Srwatson stack_zero(st); 57174195Srwatson if (frame < PAGE_SIZE) 58174195Srwatson return; 59174195Srwatson while (1) { 60209975Snwhitehorn frame = *(vm_offset_t *)frame; 61174195Srwatson if (frame < PAGE_SIZE) 62174195Srwatson break; 63209975Snwhitehorn 64209975Snwhitehorn #ifdef __powerpc64__ 65209975Snwhitehorn callpc = *(vm_offset_t *)(frame + 16) - 4; 66209975Snwhitehorn #else 67174195Srwatson callpc = *(vm_offset_t *)(frame + 4) - 4; 68209975Snwhitehorn #endif 69174195Srwatson if ((callpc & 3) || (callpc < 0x100)) 70174195Srwatson break; 71174195Srwatson 72174195Srwatson /* 73174195Srwatson * Don't bother traversing trap-frames - there should 74174195Srwatson * be enough info down to the frame to work out where 75174195Srwatson * things are going wrong. Plus, prevents this shortened 76174195Srwatson * version of code from accessing user-space frames 77174195Srwatson */ 78209975Snwhitehorn if (callpc + CALLOFFSET == (vm_offset_t) &trapexit || 79209975Snwhitehorn callpc + CALLOFFSET == (vm_offset_t) &asttrapexit) 80174195Srwatson break; 81174195Srwatson 82174195Srwatson if (stack_put(st, callpc) == -1) 83174195Srwatson break; 84174195Srwatson } 85174195Srwatson} 86174195Srwatson 87174195Srwatsonvoid 88174195Srwatsonstack_save_td(struct stack *st, struct thread *td) 89174195Srwatson{ 90198678Snwhitehorn vm_offset_t frame; 91174195Srwatson 92174195Srwatson if (TD_IS_SWAPPED(td)) 93174195Srwatson panic("stack_save_td: swapped"); 94174195Srwatson if (TD_IS_RUNNING(td)) 95174195Srwatson panic("stack_save_td: running"); 96174195Srwatson 97174195Srwatson frame = td->td_pcb->pcb_sp; 98174195Srwatson stack_capture(st, frame); 99174195Srwatson} 100174195Srwatson 101174195Srwatsonvoid 102174195Srwatsonstack_save(struct stack *st) 103174195Srwatson{ 104174195Srwatson register_t frame; 105174195Srwatson 106174195Srwatson frame = (register_t)__builtin_frame_address(1); 107174195Srwatson stack_capture(st, frame); 108174195Srwatson} 109