1 /*- 2 * Copyright (c) 2015 EMC Corporation 3 * Copyright (c) 2005 Antoine Brodin 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 #include "opt_stack.h" 30 31 #include <sys/param.h> 32 #include <sys/systm.h> 33 #include <sys/kernel.h> 34 #include <sys/lock.h> 35 #include <sys/mutex.h> 36 #include <sys/proc.h> 37 #include <sys/stack.h> 38 39 #include <machine/pcb.h> 40 #include <machine/smp.h> 41 42 #include <vm/vm.h> 43 #include <vm/vm_param.h> 44 #include <vm/pmap.h> 45 46 #include <machine/stack.h> 47 48 #ifdef __i386__ 49 #define PCB_FP(pcb) ((pcb)->pcb_ebp) 50 #define TF_FLAGS(tf) ((tf)->tf_eflags) 51 #define TF_FP(tf) ((tf)->tf_ebp) 52 #define TF_PC(tf) ((tf)->tf_eip) 53 54 typedef struct i386_frame *x86_frame_t; 55 #else 56 #define PCB_FP(pcb) ((pcb)->pcb_rbp) 57 #define TF_FLAGS(tf) ((tf)->tf_rflags) 58 #define TF_FP(tf) ((tf)->tf_rbp) 59 #define TF_PC(tf) ((tf)->tf_rip) 60 61 typedef struct amd64_frame *x86_frame_t; 62 #endif 63 64 #ifdef SMP 65 static struct stack *stack_intr_stack; 66 static struct thread *stack_intr_td; 67 static struct mtx intr_lock; 68 MTX_SYSINIT(intr_lock, &intr_lock, "stack intr", MTX_DEF); 69 #endif 70 71 static void __nosanitizeaddress __nosanitizememory 72 stack_capture(struct thread *td, struct stack *st, register_t fp) 73 { 74 x86_frame_t frame; 75 vm_offset_t callpc; 76 77 stack_zero(st); 78 frame = (x86_frame_t)fp; 79 while (1) { 80 if (!kstack_contains(td, (vm_offset_t)frame, sizeof(*frame))) 81 break; 82 callpc = frame->f_retaddr; 83 if (!INKERNEL(callpc)) 84 break; 85 if (stack_put(st, callpc) == -1) 86 break; 87 if (frame->f_frame <= frame) 88 break; 89 frame = frame->f_frame; 90 } 91 } 92 93 #ifdef SMP 94 void 95 stack_capture_intr(void) 96 { 97 struct thread *td; 98 99 td = curthread; 100 stack_capture(td, stack_intr_stack, TF_FP(td->td_intr_frame)); 101 atomic_store_rel_ptr((void *)&stack_intr_td, (uintptr_t)td); 102 } 103 #endif 104 105 int 106 stack_save_td(struct stack *st, struct thread *td) 107 { 108 int cpuid, error; 109 bool done; 110 111 THREAD_LOCK_ASSERT(td, MA_OWNED); 112 if (TD_IS_RUNNING(td) && td != curthread) 113 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED); 114 115 if (td == curthread) { 116 stack_save(st); 117 return (0); 118 } 119 120 for (done = false, error = 0; !done;) { 121 if (!TD_IS_RUNNING(td)) { 122 /* 123 * The thread will not start running so long as we hold 124 * its lock. 125 */ 126 stack_capture(td, st, PCB_FP(td->td_pcb)); 127 error = 0; 128 break; 129 } 130 131 #ifdef SMP 132 thread_unlock(td); 133 cpuid = atomic_load_int(&td->td_oncpu); 134 if (cpuid == NOCPU) { 135 cpu_spinwait(); 136 } else { 137 mtx_lock(&intr_lock); 138 stack_intr_td = NULL; 139 stack_intr_stack = st; 140 ipi_cpu(cpuid, IPI_TRACE); 141 while (atomic_load_acq_ptr((void *)&stack_intr_td) == 142 (uintptr_t)NULL) 143 cpu_spinwait(); 144 if (stack_intr_td == td) { 145 done = true; 146 error = st->depth > 0 ? 0 : EBUSY; 147 } 148 stack_intr_td = NULL; 149 mtx_unlock(&intr_lock); 150 } 151 thread_lock(td); 152 #else 153 (void)cpuid; 154 KASSERT(0, ("%s: multiple running threads", __func__)); 155 #endif 156 } 157 158 return (error); 159 } 160 161 void 162 stack_save(struct stack *st) 163 { 164 register_t fp; 165 166 #ifdef __i386__ 167 __asm __volatile("movl %%ebp,%0" : "=g" (fp)); 168 #else 169 __asm __volatile("movq %%rbp,%0" : "=g" (fp)); 170 #endif 171 stack_capture(curthread, st, fp); 172 } 173