blob: fa76151de6ff148372963f91521af354931fc115 [file] [log] [blame]
Thomas Gleixnercaab2772019-06-03 07:44:50 +02001// SPDX-License-Identifier: GPL-2.0-only
Will Deacon478fcb22012-03-05 11:49:33 +00002/*
3 * ARMv8 single-step debug support and mdscr context switching.
4 *
5 * Copyright (C) 2012 ARM Limited
6 *
Will Deacon478fcb22012-03-05 11:49:33 +00007 * Author: Will Deacon <will.deacon@arm.com>
8 */
9
10#include <linux/cpu.h>
11#include <linux/debugfs.h>
12#include <linux/hardirq.h>
13#include <linux/init.h>
14#include <linux/ptrace.h>
Sandeepa Prabhu2dd0e8d2016-07-08 12:35:48 -040015#include <linux/kprobes.h>
Will Deacon478fcb22012-03-05 11:49:33 +000016#include <linux/stat.h>
Will Deacon1442b6e2013-03-16 08:48:13 +000017#include <linux/uaccess.h>
Ingo Molnar68db0cf2017-02-08 18:51:37 +010018#include <linux/sched/task_stack.h>
Will Deacon478fcb22012-03-05 11:49:33 +000019
Suzuki K. Poulose3085bb02015-10-19 14:24:54 +010020#include <asm/cpufeature.h>
Will Deacon478fcb22012-03-05 11:49:33 +000021#include <asm/cputype.h>
James Morse65be7a12017-11-02 12:12:35 +000022#include <asm/daifflags.h>
Suzuki K. Poulose3085bb02015-10-19 14:24:54 +010023#include <asm/debug-monitors.h>
Will Deacon478fcb22012-03-05 11:49:33 +000024#include <asm/system_misc.h>
Will Deacon4e829b62018-02-20 15:18:13 +000025#include <asm/traps.h>
Will Deacon478fcb22012-03-05 11:49:33 +000026
Will Deacon478fcb22012-03-05 11:49:33 +000027/* Determine debug architecture. */
28u8 debug_monitors_arch(void)
29{
Dave Martin46823dd2017-03-23 15:14:39 +000030 return cpuid_feature_extract_unsigned_field(read_sanitised_ftr_reg(SYS_ID_AA64DFR0_EL1),
Suzuki K. Poulose3085bb02015-10-19 14:24:54 +010031 ID_AA64DFR0_DEBUGVER_SHIFT);
Will Deacon478fcb22012-03-05 11:49:33 +000032}
33
34/*
35 * MDSCR access routines.
36 */
37static void mdscr_write(u32 mdscr)
38{
39 unsigned long flags;
James Morse65be7a12017-11-02 12:12:35 +000040 flags = local_daif_save();
Mark Rutlandadf75892016-09-08 13:55:38 +010041 write_sysreg(mdscr, mdscr_el1);
James Morse65be7a12017-11-02 12:12:35 +000042 local_daif_restore(flags);
Will Deacon478fcb22012-03-05 11:49:33 +000043}
Pratyush Anand44b53f62016-07-08 12:35:49 -040044NOKPROBE_SYMBOL(mdscr_write);
Will Deacon478fcb22012-03-05 11:49:33 +000045
46static u32 mdscr_read(void)
47{
Mark Rutlandadf75892016-09-08 13:55:38 +010048 return read_sysreg(mdscr_el1);
Will Deacon478fcb22012-03-05 11:49:33 +000049}
Pratyush Anand44b53f62016-07-08 12:35:49 -040050NOKPROBE_SYMBOL(mdscr_read);
Will Deacon478fcb22012-03-05 11:49:33 +000051
52/*
53 * Allow root to disable self-hosted debug from userspace.
54 * This is useful if you want to connect an external JTAG debugger.
55 */
Viresh Kumar621a5f72015-09-26 15:04:07 -070056static bool debug_enabled = true;
Will Deacon478fcb22012-03-05 11:49:33 +000057
58static int create_debug_debugfs_entry(void)
59{
60 debugfs_create_bool("debug_enabled", 0644, NULL, &debug_enabled);
61 return 0;
62}
63fs_initcall(create_debug_debugfs_entry);
64
65static int __init early_debug_disable(char *buf)
66{
Viresh Kumar621a5f72015-09-26 15:04:07 -070067 debug_enabled = false;
Will Deacon478fcb22012-03-05 11:49:33 +000068 return 0;
69}
70
71early_param("nodebugmon", early_debug_disable);
72
73/*
74 * Keep track of debug users on each core.
75 * The ref counts are per-cpu so we use a local_t type.
76 */
Christoph Lameter1436c1a2013-10-21 13:17:08 +010077static DEFINE_PER_CPU(int, mde_ref_count);
78static DEFINE_PER_CPU(int, kde_ref_count);
Will Deacon478fcb22012-03-05 11:49:33 +000079
Will Deacon6f883d12015-07-27 18:36:54 +010080void enable_debug_monitors(enum dbg_active_el el)
Will Deacon478fcb22012-03-05 11:49:33 +000081{
82 u32 mdscr, enable = 0;
83
84 WARN_ON(preemptible());
85
Christoph Lameter1436c1a2013-10-21 13:17:08 +010086 if (this_cpu_inc_return(mde_ref_count) == 1)
Will Deacon478fcb22012-03-05 11:49:33 +000087 enable = DBG_MDSCR_MDE;
88
89 if (el == DBG_ACTIVE_EL1 &&
Christoph Lameter1436c1a2013-10-21 13:17:08 +010090 this_cpu_inc_return(kde_ref_count) == 1)
Will Deacon478fcb22012-03-05 11:49:33 +000091 enable |= DBG_MDSCR_KDE;
92
93 if (enable && debug_enabled) {
94 mdscr = mdscr_read();
95 mdscr |= enable;
96 mdscr_write(mdscr);
97 }
98}
Pratyush Anand44b53f62016-07-08 12:35:49 -040099NOKPROBE_SYMBOL(enable_debug_monitors);
Will Deacon478fcb22012-03-05 11:49:33 +0000100
Will Deacon6f883d12015-07-27 18:36:54 +0100101void disable_debug_monitors(enum dbg_active_el el)
Will Deacon478fcb22012-03-05 11:49:33 +0000102{
103 u32 mdscr, disable = 0;
104
105 WARN_ON(preemptible());
106
Christoph Lameter1436c1a2013-10-21 13:17:08 +0100107 if (this_cpu_dec_return(mde_ref_count) == 0)
Will Deacon478fcb22012-03-05 11:49:33 +0000108 disable = ~DBG_MDSCR_MDE;
109
110 if (el == DBG_ACTIVE_EL1 &&
Christoph Lameter1436c1a2013-10-21 13:17:08 +0100111 this_cpu_dec_return(kde_ref_count) == 0)
Will Deacon478fcb22012-03-05 11:49:33 +0000112 disable &= ~DBG_MDSCR_KDE;
113
114 if (disable) {
115 mdscr = mdscr_read();
116 mdscr &= disable;
117 mdscr_write(mdscr);
118 }
119}
Pratyush Anand44b53f62016-07-08 12:35:49 -0400120NOKPROBE_SYMBOL(disable_debug_monitors);
Will Deacon478fcb22012-03-05 11:49:33 +0000121
122/*
123 * OS lock clearing.
124 */
Will Deacone937dd52016-08-16 11:29:17 +0100125static int clear_os_lock(unsigned int cpu)
Will Deacon478fcb22012-03-05 11:49:33 +0000126{
Jean-Philippe Brucker6fda41b2019-04-08 18:17:18 +0100127 write_sysreg(0, osdlr_el1);
Mark Rutlandadf75892016-09-08 13:55:38 +0100128 write_sysreg(0, oslar_el1);
Will Deacone937dd52016-08-16 11:29:17 +0100129 isb();
130 return 0;
Will Deacon478fcb22012-03-05 11:49:33 +0000131}
132
Christophe JAILLET5311ebf2020-05-31 13:00:15 +0200133static int __init debug_monitors_init(void)
Will Deacon478fcb22012-03-05 11:49:33 +0000134{
Will Deacone937dd52016-08-16 11:29:17 +0100135 return cpuhp_setup_state(CPUHP_AP_ARM64_DEBUG_MONITORS_STARTING,
Thomas Gleixner73c1b412016-12-21 20:19:54 +0100136 "arm64/debug_monitors:starting",
Will Deacone937dd52016-08-16 11:29:17 +0100137 clear_os_lock, NULL);
Will Deacon478fcb22012-03-05 11:49:33 +0000138}
139postcore_initcall(debug_monitors_init);
140
141/*
142 * Single step API and exception handling.
143 */
Will Deacon3a5a4362020-02-13 12:06:26 +0000144static void set_user_regs_spsr_ss(struct user_pt_regs *regs)
Will Deacon478fcb22012-03-05 11:49:33 +0000145{
Will Deacon6b68e142016-07-19 15:07:38 +0100146 regs->pstate |= DBG_SPSR_SS;
Will Deacon478fcb22012-03-05 11:49:33 +0000147}
Will Deacon3a5a4362020-02-13 12:06:26 +0000148NOKPROBE_SYMBOL(set_user_regs_spsr_ss);
Will Deacon478fcb22012-03-05 11:49:33 +0000149
Will Deacon3a5a4362020-02-13 12:06:26 +0000150static void clear_user_regs_spsr_ss(struct user_pt_regs *regs)
Will Deacon478fcb22012-03-05 11:49:33 +0000151{
Will Deacon6b68e142016-07-19 15:07:38 +0100152 regs->pstate &= ~DBG_SPSR_SS;
Will Deacon478fcb22012-03-05 11:49:33 +0000153}
Will Deacon3a5a4362020-02-13 12:06:26 +0000154NOKPROBE_SYMBOL(clear_user_regs_spsr_ss);
155
156#define set_regs_spsr_ss(r) set_user_regs_spsr_ss(&(r)->user_regs)
157#define clear_regs_spsr_ss(r) clear_user_regs_spsr_ss(&(r)->user_regs)
Will Deacon478fcb22012-03-05 11:49:33 +0000158
Will Deacon26a04d82019-02-26 12:52:47 +0000159static DEFINE_SPINLOCK(debug_hook_lock);
160static LIST_HEAD(user_step_hook);
161static LIST_HEAD(kernel_step_hook);
Sandeepa Prabhuee6214c2013-12-04 05:50:20 +0000162
Will Deacon26a04d82019-02-26 12:52:47 +0000163static void register_debug_hook(struct list_head *node, struct list_head *list)
Sandeepa Prabhuee6214c2013-12-04 05:50:20 +0000164{
Will Deacon26a04d82019-02-26 12:52:47 +0000165 spin_lock(&debug_hook_lock);
166 list_add_rcu(node, list);
167 spin_unlock(&debug_hook_lock);
168
Sandeepa Prabhuee6214c2013-12-04 05:50:20 +0000169}
170
Will Deacon26a04d82019-02-26 12:52:47 +0000171static void unregister_debug_hook(struct list_head *node)
Sandeepa Prabhuee6214c2013-12-04 05:50:20 +0000172{
Will Deacon26a04d82019-02-26 12:52:47 +0000173 spin_lock(&debug_hook_lock);
174 list_del_rcu(node);
175 spin_unlock(&debug_hook_lock);
Yang Shicf0a2542016-02-08 14:49:24 -0800176 synchronize_rcu();
Sandeepa Prabhuee6214c2013-12-04 05:50:20 +0000177}
178
Will Deacon26a04d82019-02-26 12:52:47 +0000179void register_user_step_hook(struct step_hook *hook)
180{
181 register_debug_hook(&hook->node, &user_step_hook);
182}
183
184void unregister_user_step_hook(struct step_hook *hook)
185{
186 unregister_debug_hook(&hook->node);
187}
188
189void register_kernel_step_hook(struct step_hook *hook)
190{
191 register_debug_hook(&hook->node, &kernel_step_hook);
192}
193
194void unregister_kernel_step_hook(struct step_hook *hook)
195{
196 unregister_debug_hook(&hook->node);
197}
198
Sandeepa Prabhuee6214c2013-12-04 05:50:20 +0000199/*
Yang Shi95485fd2015-09-18 22:09:00 +0100200 * Call registered single step handlers
Sandeepa Prabhuee6214c2013-12-04 05:50:20 +0000201 * There is no Syndrome info to check for determining the handler.
202 * So we call all the registered handlers, until the right handler is
203 * found which returns zero.
204 */
205static int call_step_hook(struct pt_regs *regs, unsigned int esr)
206{
207 struct step_hook *hook;
Will Deacon26a04d82019-02-26 12:52:47 +0000208 struct list_head *list;
Sandeepa Prabhuee6214c2013-12-04 05:50:20 +0000209 int retval = DBG_HOOK_ERROR;
210
Will Deacon26a04d82019-02-26 12:52:47 +0000211 list = user_mode(regs) ? &user_step_hook : &kernel_step_hook;
212
Masami Hiramatsu760d8ed2019-07-25 17:16:25 +0900213 /*
214 * Since single-step exception disables interrupt, this function is
215 * entirely not preemptible, and we can use rcu list safely here.
216 */
Will Deacon26a04d82019-02-26 12:52:47 +0000217 list_for_each_entry_rcu(hook, list, node) {
Sandeepa Prabhuee6214c2013-12-04 05:50:20 +0000218 retval = hook->fn(regs, esr);
219 if (retval == DBG_HOOK_HANDLED)
220 break;
221 }
222
Sandeepa Prabhuee6214c2013-12-04 05:50:20 +0000223 return retval;
224}
Pratyush Anand44b53f62016-07-08 12:35:49 -0400225NOKPROBE_SYMBOL(call_step_hook);
Sandeepa Prabhuee6214c2013-12-04 05:50:20 +0000226
Will Deacone04a28d2016-02-10 16:05:28 +0000227static void send_user_sigtrap(int si_code)
228{
229 struct pt_regs *regs = current_pt_regs();
Will Deacone04a28d2016-02-10 16:05:28 +0000230
231 if (WARN_ON(!user_mode(regs)))
232 return;
233
234 if (interrupts_enabled(regs))
235 local_irq_enable();
236
Eric W. Biedermanfeca3552018-09-22 10:26:57 +0200237 arm64_force_sig_fault(SIGTRAP, si_code,
238 (void __user *)instruction_pointer(regs),
239 "User debug trap");
Will Deacone04a28d2016-02-10 16:05:28 +0000240}
241
Will Deacon5a9132a2019-02-25 12:42:26 +0000242static int single_step_handler(unsigned long unused, unsigned int esr,
Will Deacon478fcb22012-03-05 11:49:33 +0000243 struct pt_regs *regs)
244{
Pratyush Anand3fb69642016-11-02 14:40:43 +0530245 bool handler_found = false;
246
Will Deacon478fcb22012-03-05 11:49:33 +0000247 /*
248 * If we are stepping a pending breakpoint, call the hw_breakpoint
249 * handler first.
250 */
251 if (!reinstall_suspended_bps(regs))
252 return 0;
253
Pratyush Anand3fb69642016-11-02 14:40:43 +0530254 if (!handler_found && call_step_hook(regs, esr) == DBG_HOOK_HANDLED)
255 handler_found = true;
256
257 if (!handler_found && user_mode(regs)) {
Will Deaconadeb68e2016-09-01 13:35:02 +0100258 send_user_sigtrap(TRAP_TRACE);
Will Deacon478fcb22012-03-05 11:49:33 +0000259
260 /*
261 * ptrace will disable single step unless explicitly
262 * asked to re-enable it. For other clients, it makes
263 * sense to leave it enabled (i.e. rewind the controls
264 * to the active-not-pending state).
265 */
266 user_rewind_single_step(current);
Pratyush Anand3fb69642016-11-02 14:40:43 +0530267 } else if (!handler_found) {
268 pr_warn("Unexpected kernel single-step exception at EL1\n");
Will Deacon478fcb22012-03-05 11:49:33 +0000269 /*
270 * Re-enable stepping since we know that we will be
271 * returning to regs.
272 */
273 set_regs_spsr_ss(regs);
274 }
275
276 return 0;
277}
Pratyush Anand44b53f62016-07-08 12:35:49 -0400278NOKPROBE_SYMBOL(single_step_handler);
Will Deacon478fcb22012-03-05 11:49:33 +0000279
Will Deacon26a04d82019-02-26 12:52:47 +0000280static LIST_HEAD(user_break_hook);
281static LIST_HEAD(kernel_break_hook);
Sandeepa Prabhuee6214c2013-12-04 05:50:20 +0000282
Will Deacon26a04d82019-02-26 12:52:47 +0000283void register_user_break_hook(struct break_hook *hook)
Sandeepa Prabhuee6214c2013-12-04 05:50:20 +0000284{
Will Deacon26a04d82019-02-26 12:52:47 +0000285 register_debug_hook(&hook->node, &user_break_hook);
Sandeepa Prabhuee6214c2013-12-04 05:50:20 +0000286}
287
Will Deacon26a04d82019-02-26 12:52:47 +0000288void unregister_user_break_hook(struct break_hook *hook)
Sandeepa Prabhuee6214c2013-12-04 05:50:20 +0000289{
Will Deacon26a04d82019-02-26 12:52:47 +0000290 unregister_debug_hook(&hook->node);
291}
292
293void register_kernel_break_hook(struct break_hook *hook)
294{
295 register_debug_hook(&hook->node, &kernel_break_hook);
296}
297
298void unregister_kernel_break_hook(struct break_hook *hook)
299{
300 unregister_debug_hook(&hook->node);
Sandeepa Prabhuee6214c2013-12-04 05:50:20 +0000301}
302
303static int call_break_hook(struct pt_regs *regs, unsigned int esr)
304{
305 struct break_hook *hook;
Will Deacon26a04d82019-02-26 12:52:47 +0000306 struct list_head *list;
Sandeepa Prabhuee6214c2013-12-04 05:50:20 +0000307 int (*fn)(struct pt_regs *regs, unsigned int esr) = NULL;
308
Will Deacon26a04d82019-02-26 12:52:47 +0000309 list = user_mode(regs) ? &user_break_hook : &kernel_break_hook;
310
Masami Hiramatsu760d8ed2019-07-25 17:16:25 +0900311 /*
312 * Since brk exception disables interrupt, this function is
313 * entirely not preemptible, and we can use rcu list safely here.
314 */
Will Deacon26a04d82019-02-26 12:52:47 +0000315 list_for_each_entry_rcu(hook, list, node) {
Will Deacon453b7742019-02-26 15:06:42 +0000316 unsigned int comment = esr & ESR_ELx_BRK64_ISS_COMMENT_MASK;
Will Deacon26a04d82019-02-26 12:52:47 +0000317
318 if ((comment & ~hook->mask) == hook->imm)
Sandeepa Prabhuee6214c2013-12-04 05:50:20 +0000319 fn = hook->fn;
Will Deacon26a04d82019-02-26 12:52:47 +0000320 }
Sandeepa Prabhuee6214c2013-12-04 05:50:20 +0000321
322 return fn ? fn(regs, esr) : DBG_HOOK_ERROR;
323}
Pratyush Anand44b53f62016-07-08 12:35:49 -0400324NOKPROBE_SYMBOL(call_break_hook);
Sandeepa Prabhuee6214c2013-12-04 05:50:20 +0000325
Will Deacon5a9132a2019-02-25 12:42:26 +0000326static int brk_handler(unsigned long unused, unsigned int esr,
Will Deacon1442b6e2013-03-16 08:48:13 +0000327 struct pt_regs *regs)
328{
Will Deaconab6211c2019-02-26 15:39:47 +0000329 if (call_break_hook(regs, esr) == DBG_HOOK_HANDLED)
330 return 0;
Pratyush Anand53d07e2182016-11-02 14:40:44 +0530331
Will Deaconab6211c2019-02-26 15:39:47 +0000332 if (user_mode(regs)) {
Pratyush Anand53d07e2182016-11-02 14:40:44 +0530333 send_user_sigtrap(TRAP_BRKPT);
Will Deaconab6211c2019-02-26 15:39:47 +0000334 } else {
Sandeepa Prabhu2dd0e8d2016-07-08 12:35:48 -0400335 pr_warn("Unexpected kernel BRK exception at EL1\n");
Will Deacon1442b6e2013-03-16 08:48:13 +0000336 return -EFAULT;
Will Deaconc878e0c2014-07-31 11:36:08 +0100337 }
Will Deacon1442b6e2013-03-16 08:48:13 +0000338
Will Deacon1442b6e2013-03-16 08:48:13 +0000339 return 0;
340}
Pratyush Anand44b53f62016-07-08 12:35:49 -0400341NOKPROBE_SYMBOL(brk_handler);
Will Deacon1442b6e2013-03-16 08:48:13 +0000342
343int aarch32_break_handler(struct pt_regs *regs)
344{
Matthew Leach2dacab72013-11-28 12:07:23 +0000345 u32 arm_instr;
346 u16 thumb_instr;
Will Deacon1442b6e2013-03-16 08:48:13 +0000347 bool bp = false;
348 void __user *pc = (void __user *)instruction_pointer(regs);
349
350 if (!compat_user_mode(regs))
351 return -EFAULT;
352
353 if (compat_thumb_mode(regs)) {
354 /* get 16-bit Thumb instruction */
Luc Van Oostenrycka5018b02017-06-28 16:55:52 +0200355 __le16 instr;
356 get_user(instr, (__le16 __user *)pc);
357 thumb_instr = le16_to_cpu(instr);
Matthew Leach2dacab72013-11-28 12:07:23 +0000358 if (thumb_instr == AARCH32_BREAK_THUMB2_LO) {
Will Deacon1442b6e2013-03-16 08:48:13 +0000359 /* get second half of 32-bit Thumb-2 instruction */
Luc Van Oostenrycka5018b02017-06-28 16:55:52 +0200360 get_user(instr, (__le16 __user *)(pc + 2));
361 thumb_instr = le16_to_cpu(instr);
Matthew Leach2dacab72013-11-28 12:07:23 +0000362 bp = thumb_instr == AARCH32_BREAK_THUMB2_HI;
Will Deacon1442b6e2013-03-16 08:48:13 +0000363 } else {
Matthew Leach2dacab72013-11-28 12:07:23 +0000364 bp = thumb_instr == AARCH32_BREAK_THUMB;
Will Deacon1442b6e2013-03-16 08:48:13 +0000365 }
366 } else {
367 /* 32-bit ARM instruction */
Luc Van Oostenrycka5018b02017-06-28 16:55:52 +0200368 __le32 instr;
369 get_user(instr, (__le32 __user *)pc);
370 arm_instr = le32_to_cpu(instr);
Matthew Leach2dacab72013-11-28 12:07:23 +0000371 bp = (arm_instr & ~0xf0000000) == AARCH32_BREAK_ARM;
Will Deacon1442b6e2013-03-16 08:48:13 +0000372 }
373
374 if (!bp)
375 return -EFAULT;
376
Will Deacone04a28d2016-02-10 16:05:28 +0000377 send_user_sigtrap(TRAP_BRKPT);
Will Deacon1442b6e2013-03-16 08:48:13 +0000378 return 0;
379}
Pratyush Anand44b53f62016-07-08 12:35:49 -0400380NOKPROBE_SYMBOL(aarch32_break_handler);
Will Deacon1442b6e2013-03-16 08:48:13 +0000381
Douglas Andersonb322c652020-05-13 16:06:37 -0700382void __init debug_traps_init(void)
Will Deacon478fcb22012-03-05 11:49:33 +0000383{
384 hook_debug_fault_code(DBG_ESR_EVT_HWSS, single_step_handler, SIGTRAP,
Will Deaconadeb68e2016-09-01 13:35:02 +0100385 TRAP_TRACE, "single-step handler");
Will Deacon1442b6e2013-03-16 08:48:13 +0000386 hook_debug_fault_code(DBG_ESR_EVT_BRK, brk_handler, SIGTRAP,
Will Deacon0fdb64c2020-09-15 15:48:09 +0100387 TRAP_BRKPT, "BRK handler");
Will Deacon478fcb22012-03-05 11:49:33 +0000388}
Will Deacon478fcb22012-03-05 11:49:33 +0000389
390/* Re-enable single step for syscall restarting. */
391void user_rewind_single_step(struct task_struct *task)
392{
393 /*
394 * If single step is active for this thread, then set SPSR.SS
395 * to 1 to avoid returning to the active-pending state.
396 */
Will Deacon5afc7852020-02-13 12:12:26 +0000397 if (test_tsk_thread_flag(task, TIF_SINGLESTEP))
Will Deacon478fcb22012-03-05 11:49:33 +0000398 set_regs_spsr_ss(task_pt_regs(task));
399}
Pratyush Anand44b53f62016-07-08 12:35:49 -0400400NOKPROBE_SYMBOL(user_rewind_single_step);
Will Deacon478fcb22012-03-05 11:49:33 +0000401
402void user_fastforward_single_step(struct task_struct *task)
403{
Will Deacon5afc7852020-02-13 12:12:26 +0000404 if (test_tsk_thread_flag(task, TIF_SINGLESTEP))
Will Deacon478fcb22012-03-05 11:49:33 +0000405 clear_regs_spsr_ss(task_pt_regs(task));
406}
407
Will Deacon3a5a4362020-02-13 12:06:26 +0000408void user_regs_reset_single_step(struct user_pt_regs *regs,
409 struct task_struct *task)
410{
411 if (test_tsk_thread_flag(task, TIF_SINGLESTEP))
412 set_user_regs_spsr_ss(regs);
413 else
414 clear_user_regs_spsr_ss(regs);
415}
416
Will Deacon478fcb22012-03-05 11:49:33 +0000417/* Kernel API */
418void 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}
Pratyush Anand44b53f62016-07-08 12:35:49 -0400425NOKPROBE_SYMBOL(kernel_enable_single_step);
Will Deacon478fcb22012-03-05 11:49:33 +0000426
427void 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}
Pratyush Anand44b53f62016-07-08 12:35:49 -0400433NOKPROBE_SYMBOL(kernel_disable_single_step);
Will Deacon478fcb22012-03-05 11:49:33 +0000434
435int kernel_active_single_step(void)
436{
437 WARN_ON(!irqs_disabled());
438 return mdscr_read() & DBG_MDSCR_SS;
439}
Pratyush Anand44b53f62016-07-08 12:35:49 -0400440NOKPROBE_SYMBOL(kernel_active_single_step);
Will Deacon478fcb22012-03-05 11:49:33 +0000441
442/* ptrace API */
443void user_enable_single_step(struct task_struct *task)
444{
Will Deacon3a402a72016-08-26 11:36:39 +0100445 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));
Will Deacon478fcb22012-03-05 11:49:33 +0000449}
Pratyush Anand44b53f62016-07-08 12:35:49 -0400450NOKPROBE_SYMBOL(user_enable_single_step);
Will Deacon478fcb22012-03-05 11:49:33 +0000451
452void user_disable_single_step(struct task_struct *task)
453{
454 clear_ti_thread_flag(task_thread_info(task), TIF_SINGLESTEP);
455}
Pratyush Anand44b53f62016-07-08 12:35:49 -0400456NOKPROBE_SYMBOL(user_disable_single_step);