xref: /freebsd/sys/x86/x86/stack_machdep.c (revision 685dc743)
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
stack_capture(struct thread * td,struct stack * st,register_t fp)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
stack_capture_intr(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
stack_save_td(struct stack * st,struct thread * td)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 	KASSERT(!TD_IS_SWAPPED(td),
113 	    ("stack_save_td: thread %p is swapped", td));
114 	if (TD_IS_RUNNING(td) && td != curthread)
115 		PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
116 
117 	if (td == curthread) {
118 		stack_save(st);
119 		return (0);
120 	}
121 
122 	for (done = false, error = 0; !done;) {
123 		if (!TD_IS_RUNNING(td)) {
124 			/*
125 			 * The thread will not start running so long as we hold
126 			 * its lock.
127 			 */
128 			stack_capture(td, st, PCB_FP(td->td_pcb));
129 			error = 0;
130 			break;
131 		}
132 
133 #ifdef SMP
134 		thread_unlock(td);
135 		cpuid = atomic_load_int(&td->td_oncpu);
136 		if (cpuid == NOCPU) {
137 			cpu_spinwait();
138 		} else {
139 			mtx_lock(&intr_lock);
140 			stack_intr_td = NULL;
141 			stack_intr_stack = st;
142 			ipi_cpu(cpuid, IPI_TRACE);
143 			while (atomic_load_acq_ptr((void *)&stack_intr_td) ==
144 			    (uintptr_t)NULL)
145 				cpu_spinwait();
146 			if (stack_intr_td == td) {
147 				done = true;
148 				error = st->depth > 0 ? 0 : EBUSY;
149 			}
150 			stack_intr_td = NULL;
151 			mtx_unlock(&intr_lock);
152 		}
153 		thread_lock(td);
154 #else
155 		(void)cpuid;
156 		KASSERT(0, ("%s: multiple running threads", __func__));
157 #endif
158 	}
159 
160 	return (error);
161 }
162 
163 void
stack_save(struct stack * st)164 stack_save(struct stack *st)
165 {
166 	register_t fp;
167 
168 #ifdef __i386__
169 	__asm __volatile("movl %%ebp,%0" : "=g" (fp));
170 #else
171 	__asm __volatile("movq %%rbp,%0" : "=g" (fp));
172 #endif
173 	stack_capture(curthread, st, fp);
174 }
175