1 /*
2  * ARMv8 single-step debug support and mdscr context switching.
3  *
4  * Copyright (C) 2012 ARM Limited
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  * Author: Will Deacon <will.deacon@arm.com>
19  */
20 
21 #include <linux/cpu.h>
22 #include <linux/debugfs.h>
23 #include <linux/hardirq.h>
24 #include <linux/init.h>
25 #include <linux/ptrace.h>
26 #include <linux/kprobes.h>
27 #include <linux/stat.h>
28 #include <linux/uaccess.h>
29 #include <linux/sched/task_stack.h>
30 
31 #include <asm/cpufeature.h>
32 #include <asm/cputype.h>
33 #include <asm/daifflags.h>
34 #include <asm/debug-monitors.h>
35 #include <asm/system_misc.h>
36 #include <asm/traps.h>
37 
38 /* Determine debug architecture. */
39 u8 debug_monitors_arch(void)
40 {
41 	return cpuid_feature_extract_unsigned_field(read_sanitised_ftr_reg(SYS_ID_AA64DFR0_EL1),
42 						ID_AA64DFR0_DEBUGVER_SHIFT);
43 }
44 
45 /*
46  * MDSCR access routines.
47  */
48 static void mdscr_write(u32 mdscr)
49 {
50 	unsigned long flags;
51 	flags = local_daif_save();
52 	write_sysreg(mdscr, mdscr_el1);
53 	local_daif_restore(flags);
54 }
55 NOKPROBE_SYMBOL(mdscr_write);
56 
57 static u32 mdscr_read(void)
58 {
59 	return read_sysreg(mdscr_el1);
60 }
61 NOKPROBE_SYMBOL(mdscr_read);
62 
63 /*
64  * Allow root to disable self-hosted debug from userspace.
65  * This is useful if you want to connect an external JTAG debugger.
66  */
67 static bool debug_enabled = true;
68 
69 static int create_debug_debugfs_entry(void)
70 {
71 	debugfs_create_bool("debug_enabled", 0644, NULL, &debug_enabled);
72 	return 0;
73 }
74 fs_initcall(create_debug_debugfs_entry);
75 
76 static int __init early_debug_disable(char *buf)
77 {
78 	debug_enabled = false;
79 	return 0;
80 }
81 
82 early_param("nodebugmon", early_debug_disable);
83 
84 /*
85  * Keep track of debug users on each core.
86  * The ref counts are per-cpu so we use a local_t type.
87  */
88 static DEFINE_PER_CPU(int, mde_ref_count);
89 static DEFINE_PER_CPU(int, kde_ref_count);
90 
91 void enable_debug_monitors(enum dbg_active_el el)
92 {
93 	u32 mdscr, enable = 0;
94 
95 	WARN_ON(preemptible());
96 
97 	if (this_cpu_inc_return(mde_ref_count) == 1)
98 		enable = DBG_MDSCR_MDE;
99 
100 	if (el == DBG_ACTIVE_EL1 &&
101 	    this_cpu_inc_return(kde_ref_count) == 1)
102 		enable |= DBG_MDSCR_KDE;
103 
104 	if (enable && debug_enabled) {
105 		mdscr = mdscr_read();
106 		mdscr |= enable;
107 		mdscr_write(mdscr);
108 	}
109 }
110 NOKPROBE_SYMBOL(enable_debug_monitors);
111 
112 void disable_debug_monitors(enum dbg_active_el el)
113 {
114 	u32 mdscr, disable = 0;
115 
116 	WARN_ON(preemptible());
117 
118 	if (this_cpu_dec_return(mde_ref_count) == 0)
119 		disable = ~DBG_MDSCR_MDE;
120 
121 	if (el == DBG_ACTIVE_EL1 &&
122 	    this_cpu_dec_return(kde_ref_count) == 0)
123 		disable &= ~DBG_MDSCR_KDE;
124 
125 	if (disable) {
126 		mdscr = mdscr_read();
127 		mdscr &= disable;
128 		mdscr_write(mdscr);
129 	}
130 }
131 NOKPROBE_SYMBOL(disable_debug_monitors);
132 
133 /*
134  * OS lock clearing.
135  */
136 static int clear_os_lock(unsigned int cpu)
137 {
138 	write_sysreg(0, oslar_el1);
139 	isb();
140 	return 0;
141 }
142 
143 static int debug_monitors_init(void)
144 {
145 	return cpuhp_setup_state(CPUHP_AP_ARM64_DEBUG_MONITORS_STARTING,
146 				 "arm64/debug_monitors:starting",
147 				 clear_os_lock, NULL);
148 }
149 postcore_initcall(debug_monitors_init);
150 
151 /*
152  * Single step API and exception handling.
153  */
154 static void set_regs_spsr_ss(struct pt_regs *regs)
155 {
156 	regs->pstate |= DBG_SPSR_SS;
157 }
158 NOKPROBE_SYMBOL(set_regs_spsr_ss);
159 
160 static void clear_regs_spsr_ss(struct pt_regs *regs)
161 {
162 	regs->pstate &= ~DBG_SPSR_SS;
163 }
164 NOKPROBE_SYMBOL(clear_regs_spsr_ss);
165 
166 static DEFINE_SPINLOCK(debug_hook_lock);
167 static LIST_HEAD(user_step_hook);
168 static LIST_HEAD(kernel_step_hook);
169 
170 static void register_debug_hook(struct list_head *node, struct list_head *list)
171 {
172 	spin_lock(&debug_hook_lock);
173 	list_add_rcu(node, list);
174 	spin_unlock(&debug_hook_lock);
175 
176 }
177 
178 static void unregister_debug_hook(struct list_head *node)
179 {
180 	spin_lock(&debug_hook_lock);
181 	list_del_rcu(node);
182 	spin_unlock(&debug_hook_lock);
183 	synchronize_rcu();
184 }
185 
186 void register_user_step_hook(struct step_hook *hook)
187 {
188 	register_debug_hook(&hook->node, &user_step_hook);
189 }
190 
191 void unregister_user_step_hook(struct step_hook *hook)
192 {
193 	unregister_debug_hook(&hook->node);
194 }
195 
196 void register_kernel_step_hook(struct step_hook *hook)
197 {
198 	register_debug_hook(&hook->node, &kernel_step_hook);
199 }
200 
201 void unregister_kernel_step_hook(struct step_hook *hook)
202 {
203 	unregister_debug_hook(&hook->node);
204 }
205 
206 /*
207  * Call registered single step handlers
208  * There is no Syndrome info to check for determining the handler.
209  * So we call all the registered handlers, until the right handler is
210  * found which returns zero.
211  */
212 static int call_step_hook(struct pt_regs *regs, unsigned int esr)
213 {
214 	struct step_hook *hook;
215 	struct list_head *list;
216 	int retval = DBG_HOOK_ERROR;
217 
218 	list = user_mode(regs) ? &user_step_hook : &kernel_step_hook;
219 
220 	rcu_read_lock();
221 
222 	list_for_each_entry_rcu(hook, list, node)	{
223 		retval = hook->fn(regs, esr);
224 		if (retval == DBG_HOOK_HANDLED)
225 			break;
226 	}
227 
228 	rcu_read_unlock();
229 
230 	return retval;
231 }
232 NOKPROBE_SYMBOL(call_step_hook);
233 
234 static void send_user_sigtrap(int si_code)
235 {
236 	struct pt_regs *regs = current_pt_regs();
237 
238 	if (WARN_ON(!user_mode(regs)))
239 		return;
240 
241 	if (interrupts_enabled(regs))
242 		local_irq_enable();
243 
244 	arm64_force_sig_fault(SIGTRAP, si_code,
245 			     (void __user *)instruction_pointer(regs),
246 			     "User debug trap");
247 }
248 
249 static int single_step_handler(unsigned long unused, unsigned int esr,
250 			       struct pt_regs *regs)
251 {
252 	bool handler_found = false;
253 
254 	/*
255 	 * If we are stepping a pending breakpoint, call the hw_breakpoint
256 	 * handler first.
257 	 */
258 	if (!reinstall_suspended_bps(regs))
259 		return 0;
260 
261 	if (!handler_found && call_step_hook(regs, esr) == DBG_HOOK_HANDLED)
262 		handler_found = true;
263 
264 	if (!handler_found && user_mode(regs)) {
265 		send_user_sigtrap(TRAP_TRACE);
266 
267 		/*
268 		 * ptrace will disable single step unless explicitly
269 		 * asked to re-enable it. For other clients, it makes
270 		 * sense to leave it enabled (i.e. rewind the controls
271 		 * to the active-not-pending state).
272 		 */
273 		user_rewind_single_step(current);
274 	} else if (!handler_found) {
275 		pr_warn("Unexpected kernel single-step exception at EL1\n");
276 		/*
277 		 * Re-enable stepping since we know that we will be
278 		 * returning to regs.
279 		 */
280 		set_regs_spsr_ss(regs);
281 	}
282 
283 	return 0;
284 }
285 NOKPROBE_SYMBOL(single_step_handler);
286 
287 static LIST_HEAD(user_break_hook);
288 static LIST_HEAD(kernel_break_hook);
289 
290 void register_user_break_hook(struct break_hook *hook)
291 {
292 	register_debug_hook(&hook->node, &user_break_hook);
293 }
294 
295 void unregister_user_break_hook(struct break_hook *hook)
296 {
297 	unregister_debug_hook(&hook->node);
298 }
299 
300 void register_kernel_break_hook(struct break_hook *hook)
301 {
302 	register_debug_hook(&hook->node, &kernel_break_hook);
303 }
304 
305 void unregister_kernel_break_hook(struct break_hook *hook)
306 {
307 	unregister_debug_hook(&hook->node);
308 }
309 
310 static int call_break_hook(struct pt_regs *regs, unsigned int esr)
311 {
312 	struct break_hook *hook;
313 	struct list_head *list;
314 	int (*fn)(struct pt_regs *regs, unsigned int esr) = NULL;
315 
316 	list = user_mode(regs) ? &user_break_hook : &kernel_break_hook;
317 
318 	rcu_read_lock();
319 	list_for_each_entry_rcu(hook, list, node) {
320 		unsigned int comment = esr & BRK64_ESR_MASK;
321 
322 		if ((comment & ~hook->mask) == hook->imm)
323 			fn = hook->fn;
324 	}
325 	rcu_read_unlock();
326 
327 	return fn ? fn(regs, esr) : DBG_HOOK_ERROR;
328 }
329 NOKPROBE_SYMBOL(call_break_hook);
330 
331 static int brk_handler(unsigned long unused, unsigned int esr,
332 		       struct pt_regs *regs)
333 {
334 	bool handler_found = false;
335 
336 	if (!handler_found && call_break_hook(regs, esr) == DBG_HOOK_HANDLED)
337 		handler_found = true;
338 
339 	if (!handler_found && user_mode(regs)) {
340 		send_user_sigtrap(TRAP_BRKPT);
341 	} else if (!handler_found) {
342 		pr_warn("Unexpected kernel BRK exception at EL1\n");
343 		return -EFAULT;
344 	}
345 
346 	return 0;
347 }
348 NOKPROBE_SYMBOL(brk_handler);
349 
350 int aarch32_break_handler(struct pt_regs *regs)
351 {
352 	u32 arm_instr;
353 	u16 thumb_instr;
354 	bool bp = false;
355 	void __user *pc = (void __user *)instruction_pointer(regs);
356 
357 	if (!compat_user_mode(regs))
358 		return -EFAULT;
359 
360 	if (compat_thumb_mode(regs)) {
361 		/* get 16-bit Thumb instruction */
362 		__le16 instr;
363 		get_user(instr, (__le16 __user *)pc);
364 		thumb_instr = le16_to_cpu(instr);
365 		if (thumb_instr == AARCH32_BREAK_THUMB2_LO) {
366 			/* get second half of 32-bit Thumb-2 instruction */
367 			get_user(instr, (__le16 __user *)(pc + 2));
368 			thumb_instr = le16_to_cpu(instr);
369 			bp = thumb_instr == AARCH32_BREAK_THUMB2_HI;
370 		} else {
371 			bp = thumb_instr == AARCH32_BREAK_THUMB;
372 		}
373 	} else {
374 		/* 32-bit ARM instruction */
375 		__le32 instr;
376 		get_user(instr, (__le32 __user *)pc);
377 		arm_instr = le32_to_cpu(instr);
378 		bp = (arm_instr & ~0xf0000000) == AARCH32_BREAK_ARM;
379 	}
380 
381 	if (!bp)
382 		return -EFAULT;
383 
384 	send_user_sigtrap(TRAP_BRKPT);
385 	return 0;
386 }
387 NOKPROBE_SYMBOL(aarch32_break_handler);
388 
389 static int __init debug_traps_init(void)
390 {
391 	hook_debug_fault_code(DBG_ESR_EVT_HWSS, single_step_handler, SIGTRAP,
392 			      TRAP_TRACE, "single-step handler");
393 	hook_debug_fault_code(DBG_ESR_EVT_BRK, brk_handler, SIGTRAP,
394 			      TRAP_BRKPT, "ptrace BRK handler");
395 	return 0;
396 }
397 arch_initcall(debug_traps_init);
398 
399 /* Re-enable single step for syscall restarting. */
400 void user_rewind_single_step(struct task_struct *task)
401 {
402 	/*
403 	 * If single step is active for this thread, then set SPSR.SS
404 	 * to 1 to avoid returning to the active-pending state.
405 	 */
406 	if (test_ti_thread_flag(task_thread_info(task), TIF_SINGLESTEP))
407 		set_regs_spsr_ss(task_pt_regs(task));
408 }
409 NOKPROBE_SYMBOL(user_rewind_single_step);
410 
411 void user_fastforward_single_step(struct task_struct *task)
412 {
413 	if (test_ti_thread_flag(task_thread_info(task), TIF_SINGLESTEP))
414 		clear_regs_spsr_ss(task_pt_regs(task));
415 }
416 
417 /* Kernel API */
418 void kernel_enable_single_step(struct pt_regs *regs)
419 {
420 	WARN_ON(!irqs_disabled());
421 	set_regs_spsr_ss(regs);
422 	mdscr_write(mdscr_read() | DBG_MDSCR_SS);
423 	enable_debug_monitors(DBG_ACTIVE_EL1);
424 }
425 NOKPROBE_SYMBOL(kernel_enable_single_step);
426 
427 void kernel_disable_single_step(void)
428 {
429 	WARN_ON(!irqs_disabled());
430 	mdscr_write(mdscr_read() & ~DBG_MDSCR_SS);
431 	disable_debug_monitors(DBG_ACTIVE_EL1);
432 }
433 NOKPROBE_SYMBOL(kernel_disable_single_step);
434 
435 int kernel_active_single_step(void)
436 {
437 	WARN_ON(!irqs_disabled());
438 	return mdscr_read() & DBG_MDSCR_SS;
439 }
440 NOKPROBE_SYMBOL(kernel_active_single_step);
441 
442 /* ptrace API */
443 void user_enable_single_step(struct task_struct *task)
444 {
445 	struct thread_info *ti = task_thread_info(task);
446 
447 	if (!test_and_set_ti_thread_flag(ti, TIF_SINGLESTEP))
448 		set_regs_spsr_ss(task_pt_regs(task));
449 }
450 NOKPROBE_SYMBOL(user_enable_single_step);
451 
452 void user_disable_single_step(struct task_struct *task)
453 {
454 	clear_ti_thread_flag(task_thread_info(task), TIF_SINGLESTEP);
455 }
456 NOKPROBE_SYMBOL(user_disable_single_step);
457