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__FBSDID("$FreeBSD: stable/11/sys/x86/x86/stack_machdep.c 337976 2018-08-17 16:04:59Z markj $");
30
31#include "opt_stack.h"
32
33#include <sys/param.h>
34#include <sys/systm.h>
35#include <sys/kernel.h>
36#include <sys/lock.h>
37#include <sys/mutex.h>
38#include <sys/proc.h>
39#include <sys/stack.h>
40
41#include <machine/pcb.h>
42#include <machine/smp.h>
43
44#include <vm/vm.h>
45#include <vm/vm_param.h>
46#include <vm/pmap.h>
47
48#include <x86/stack.h>
49
50#ifdef __i386__
51#define	PCB_FP(pcb)	((pcb)->pcb_ebp)
52#define	TF_FLAGS(tf)	((tf)->tf_eflags)
53#define	TF_FP(tf)	((tf)->tf_ebp)
54#define	TF_PC(tf)	((tf)->tf_eip)
55
56typedef struct i386_frame *x86_frame_t;
57#else
58#define	PCB_FP(pcb)	((pcb)->pcb_rbp)
59#define	TF_FLAGS(tf)	((tf)->tf_rflags)
60#define	TF_FP(tf)	((tf)->tf_rbp)
61#define	TF_PC(tf)	((tf)->tf_rip)
62
63typedef struct amd64_frame *x86_frame_t;
64#endif
65
66#ifdef STACK
67static struct stack *nmi_stack;
68static volatile struct thread *nmi_pending;
69
70#ifdef SMP
71static struct mtx nmi_lock;
72MTX_SYSINIT(nmi_lock, &nmi_lock, "stack_nmi", MTX_SPIN);
73#endif
74#endif
75
76static void
77stack_capture(struct thread *td, struct stack *st, register_t fp)
78{
79	x86_frame_t frame;
80	vm_offset_t callpc;
81
82	stack_zero(st);
83	frame = (x86_frame_t)fp;
84	while (1) {
85		if ((vm_offset_t)frame < td->td_kstack ||
86		    (vm_offset_t)frame >= td->td_kstack +
87		    td->td_kstack_pages * PAGE_SIZE)
88			break;
89		callpc = frame->f_retaddr;
90		if (!INKERNEL(callpc))
91			break;
92		if (stack_put(st, callpc) == -1)
93			break;
94		if (frame->f_frame <= frame)
95			break;
96		frame = frame->f_frame;
97	}
98}
99
100int
101stack_nmi_handler(struct trapframe *tf)
102{
103
104#ifdef STACK
105	/* Don't consume an NMI that wasn't meant for us. */
106	if (nmi_stack == NULL || curthread != nmi_pending)
107		return (0);
108
109	if (!TRAPF_USERMODE(tf) && (TF_FLAGS(tf) & PSL_I) != 0)
110		stack_capture(curthread, nmi_stack, TF_FP(tf));
111	else
112		/* We were running in usermode or had interrupts disabled. */
113		nmi_stack->depth = 0;
114
115	atomic_store_rel_ptr((long *)&nmi_pending, (long)NULL);
116	return (1);
117#else
118	return (0);
119#endif
120}
121
122void
123stack_save_td(struct stack *st, struct thread *td)
124{
125
126	if (TD_IS_SWAPPED(td))
127		panic("stack_save_td: swapped");
128	if (TD_IS_RUNNING(td))
129		panic("stack_save_td: running");
130
131	stack_capture(td, st, PCB_FP(td->td_pcb));
132}
133
134int
135stack_save_td_running(struct stack *st, struct thread *td)
136{
137
138#ifdef STACK
139	THREAD_LOCK_ASSERT(td, MA_OWNED);
140	MPASS(TD_IS_RUNNING(td));
141
142	if (td == curthread) {
143		stack_save(st);
144		return (0);
145	}
146
147#ifdef SMP
148	mtx_lock_spin(&nmi_lock);
149
150	nmi_stack = st;
151	nmi_pending = td;
152	ipi_cpu(td->td_oncpu, IPI_TRACE);
153	while ((void *)atomic_load_acq_ptr((long *)&nmi_pending) != NULL)
154		cpu_spinwait();
155	nmi_stack = NULL;
156
157	mtx_unlock_spin(&nmi_lock);
158
159	if (st->depth == 0)
160		return (EAGAIN);
161#else /* !SMP */
162	KASSERT(0, ("curthread isn't running"));
163#endif /* SMP */
164	return (0);
165#else /* !STACK */
166	return (EOPNOTSUPP);
167#endif /* STACK */
168}
169
170void
171stack_save(struct stack *st)
172{
173	register_t fp;
174
175#ifdef __i386__
176	__asm __volatile("movl %%ebp,%0" : "=g" (fp));
177#else
178	__asm __volatile("movq %%rbp,%0" : "=g" (fp));
179#endif
180	stack_capture(curthread, st, fp);
181}
182