xref: /linux/arch/arm64/kvm/hyp/nvhe/stacktrace.c (revision 7d1f84f1b94e79cb95d974ab3ec54cb3a4d64f5b)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * KVM nVHE hypervisor stack tracing support.
4  *
5  * Copyright (C) 2022 Google LLC
6  */
7 #include <asm/kvm_asm.h>
8 #include <asm/kvm_hyp.h>
9 #include <asm/memory.h>
10 #include <asm/percpu.h>
11 #include <asm/stacktrace/nvhe.h>
12 
13 DEFINE_PER_CPU(unsigned long [OVERFLOW_STACK_SIZE/sizeof(long)], overflow_stack)
14 	__aligned(16);
15 
16 DEFINE_PER_CPU(struct kvm_nvhe_stacktrace_info, kvm_stacktrace_info);
17 
18 /*
19  * hyp_prepare_backtrace - Prepare non-protected nVHE backtrace.
20  *
21  * @fp : frame pointer at which to start the unwinding.
22  * @pc : program counter at which to start the unwinding.
23  *
24  * Save the information needed by the host to unwind the non-protected
25  * nVHE hypervisor stack in EL1.
26  */
27 static void hyp_prepare_backtrace(unsigned long fp, unsigned long pc)
28 {
29 	struct kvm_nvhe_stacktrace_info *stacktrace_info = this_cpu_ptr(&kvm_stacktrace_info);
30 	struct kvm_nvhe_init_params *params = this_cpu_ptr(&kvm_init_params);
31 
32 	stacktrace_info->stack_base = (unsigned long)(params->stack_hyp_va - NVHE_STACK_SIZE);
33 	stacktrace_info->overflow_stack_base = (unsigned long)this_cpu_ptr(overflow_stack);
34 	stacktrace_info->fp = fp;
35 	stacktrace_info->pc = pc;
36 }
37 
38 #ifdef CONFIG_PKVM_STACKTRACE
39 DEFINE_PER_CPU(unsigned long [NVHE_STACKTRACE_SIZE/sizeof(long)], pkvm_stacktrace);
40 
41 static struct stack_info stackinfo_get_overflow(void)
42 {
43 	unsigned long low = (unsigned long)this_cpu_ptr(overflow_stack);
44 	unsigned long high = low + OVERFLOW_STACK_SIZE;
45 
46 	return (struct stack_info) {
47 		.low = low,
48 		.high = high,
49 	};
50 }
51 
52 static struct stack_info stackinfo_get_hyp(void)
53 {
54 	struct kvm_nvhe_init_params *params = this_cpu_ptr(&kvm_init_params);
55 	unsigned long high = params->stack_hyp_va;
56 	unsigned long low = high - NVHE_STACK_SIZE;
57 
58 	return (struct stack_info) {
59 		.low = low,
60 		.high = high,
61 	};
62 }
63 
64 static int unwind_next(struct unwind_state *state)
65 {
66 	return unwind_next_frame_record(state);
67 }
68 
69 static void notrace unwind(struct unwind_state *state,
70 			   stack_trace_consume_fn consume_entry,
71 			   void *cookie)
72 {
73 	while (1) {
74 		int ret;
75 
76 		if (!consume_entry(cookie, state->pc))
77 			break;
78 		ret = unwind_next(state);
79 		if (ret < 0)
80 			break;
81 	}
82 }
83 
84 /*
85  * pkvm_save_backtrace_entry - Saves a protected nVHE HYP stacktrace entry
86  *
87  * @arg    : index of the entry in the stacktrace buffer
88  * @where  : the program counter corresponding to the stack frame
89  *
90  * Save the return address of a stack frame to the shared stacktrace buffer.
91  * The host can access this shared buffer from EL1 to dump the backtrace.
92  */
93 static bool pkvm_save_backtrace_entry(void *arg, unsigned long where)
94 {
95 	unsigned long *stacktrace = this_cpu_ptr(pkvm_stacktrace);
96 	int *idx = (int *)arg;
97 
98 	/*
99 	 * Need 2 free slots: 1 for current entry and 1 for the
100 	 * delimiter.
101 	 */
102 	if (*idx > ARRAY_SIZE(pkvm_stacktrace) - 2)
103 		return false;
104 
105 	stacktrace[*idx] = where;
106 	stacktrace[++*idx] = 0UL;
107 
108 	return true;
109 }
110 
111 /*
112  * pkvm_save_backtrace - Saves the protected nVHE HYP stacktrace
113  *
114  * @fp : frame pointer at which to start the unwinding.
115  * @pc : program counter at which to start the unwinding.
116  *
117  * Save the unwinded stack addresses to the shared stacktrace buffer.
118  * The host can access this shared buffer from EL1 to dump the backtrace.
119  */
120 static void pkvm_save_backtrace(unsigned long fp, unsigned long pc)
121 {
122 	struct stack_info stacks[] = {
123 		stackinfo_get_overflow(),
124 		stackinfo_get_hyp(),
125 	};
126 	struct unwind_state state = {
127 		.stacks = stacks,
128 		.nr_stacks = ARRAY_SIZE(stacks),
129 	};
130 	int idx = 0;
131 
132 	kvm_nvhe_unwind_init(&state, fp, pc);
133 
134 	unwind(&state, pkvm_save_backtrace_entry, &idx);
135 }
136 #else /* !CONFIG_PKVM_STACKTRACE */
137 static void pkvm_save_backtrace(unsigned long fp, unsigned long pc)
138 {
139 }
140 #endif /* CONFIG_PKVM_STACKTRACE */
141 
142 /*
143  * kvm_nvhe_prepare_backtrace - prepare to dump the nVHE backtrace
144  *
145  * @fp : frame pointer at which to start the unwinding.
146  * @pc : program counter at which to start the unwinding.
147  *
148  * Saves the information needed by the host to dump the nVHE hypervisor
149  * backtrace.
150  */
151 void kvm_nvhe_prepare_backtrace(unsigned long fp, unsigned long pc)
152 {
153 	if (is_protected_kvm_enabled())
154 		pkvm_save_backtrace(fp, pc);
155 	else
156 		hyp_prepare_backtrace(fp, pc);
157 }
158