blob: 4f3661eeb7ec6f28873cccf4411ec649c2475815 [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
Peter Collingbournedceec3f2020-11-20 12:33:46 -0800237 arm64_force_sig_fault(SIGTRAP, si_code, instruction_pointer(regs),
238 "User debug trap");
Will Deacone04a28d2016-02-10 16:05:28 +0000239}
240
Will Deacon5a9132a2019-02-25 12:42:26 +0000241static int single_step_handler(unsigned long unused, unsigned int esr,
Will Deacon478fcb22012-03-05 11:49:33 +0000242 struct pt_regs *regs)
243{
Pratyush Anand3fb69642016-11-02 14:40:43 +0530244 bool handler_found = false;
245
Will Deacon478fcb22012-03-05 11:49:33 +0000246 /*
247 * If we are stepping a pending breakpoint, call the hw_breakpoint
248 * handler first.
249 */
250 if (!reinstall_suspended_bps(regs))
251 return 0;
252
Pratyush Anand3fb69642016-11-02 14:40:43 +0530253 if (!handler_found && call_step_hook(regs, esr) == DBG_HOOK_HANDLED)
254 handler_found = true;
255
256 if (!handler_found && user_mode(regs)) {
Will Deaconadeb68e2016-09-01 13:35:02 +0100257 send_user_sigtrap(TRAP_TRACE);
Will Deacon478fcb22012-03-05 11:49:33 +0000258
259 /*
260 * ptrace will disable single step unless explicitly
261 * asked to re-enable it. For other clients, it makes
262 * sense to leave it enabled (i.e. rewind the controls
263 * to the active-not-pending state).
264 */
265 user_rewind_single_step(current);
Pratyush Anand3fb69642016-11-02 14:40:43 +0530266 } else if (!handler_found) {
267 pr_warn("Unexpected kernel single-step exception at EL1\n");
Will Deacon478fcb22012-03-05 11:49:33 +0000268 /*
269 * Re-enable stepping since we know that we will be
270 * returning to regs.
271 */
272 set_regs_spsr_ss(regs);
273 }
274
275 return 0;
276}
Pratyush Anand44b53f62016-07-08 12:35:49 -0400277NOKPROBE_SYMBOL(single_step_handler);
Will Deacon478fcb22012-03-05 11:49:33 +0000278
Will Deacon26a04d82019-02-26 12:52:47 +0000279static LIST_HEAD(user_break_hook);
280static LIST_HEAD(kernel_break_hook);
Sandeepa Prabhuee6214c2013-12-04 05:50:20 +0000281
Will Deacon26a04d82019-02-26 12:52:47 +0000282void register_user_break_hook(struct break_hook *hook)
Sandeepa Prabhuee6214c2013-12-04 05:50:20 +0000283{
Will Deacon26a04d82019-02-26 12:52:47 +0000284 register_debug_hook(&hook->node, &user_break_hook);
Sandeepa Prabhuee6214c2013-12-04 05:50:20 +0000285}
286
Will Deacon26a04d82019-02-26 12:52:47 +0000287void unregister_user_break_hook(struct break_hook *hook)
Sandeepa Prabhuee6214c2013-12-04 05:50:20 +0000288{
Will Deacon26a04d82019-02-26 12:52:47 +0000289 unregister_debug_hook(&hook->node);
290}
291
292void register_kernel_break_hook(struct break_hook *hook)
293{
294 register_debug_hook(&hook->node, &kernel_break_hook);
295}
296
297void unregister_kernel_break_hook(struct break_hook *hook)
298{
299 unregister_debug_hook(&hook->node);
Sandeepa Prabhuee6214c2013-12-04 05:50:20 +0000300}
301
302static int call_break_hook(struct pt_regs *regs, unsigned int esr)
303{
304 struct break_hook *hook;
Will Deacon26a04d82019-02-26 12:52:47 +0000305 struct list_head *list;
Sandeepa Prabhuee6214c2013-12-04 05:50:20 +0000306 int (*fn)(struct pt_regs *regs, unsigned int esr) = NULL;
307
Will Deacon26a04d82019-02-26 12:52:47 +0000308 list = user_mode(regs) ? &user_break_hook : &kernel_break_hook;
309
Masami Hiramatsu760d8ed2019-07-25 17:16:25 +0900310 /*
311 * Since brk exception disables interrupt, this function is
312 * entirely not preemptible, and we can use rcu list safely here.
313 */
Will Deacon26a04d82019-02-26 12:52:47 +0000314 list_for_each_entry_rcu(hook, list, node) {
Will Deacon453b7742019-02-26 15:06:42 +0000315 unsigned int comment = esr & ESR_ELx_BRK64_ISS_COMMENT_MASK;
Will Deacon26a04d82019-02-26 12:52:47 +0000316
317 if ((comment & ~hook->mask) == hook->imm)
Sandeepa Prabhuee6214c2013-12-04 05:50:20 +0000318 fn = hook->fn;
Will Deacon26a04d82019-02-26 12:52:47 +0000319 }
Sandeepa Prabhuee6214c2013-12-04 05:50:20 +0000320
321 return fn ? fn(regs, esr) : DBG_HOOK_ERROR;
322}
Pratyush Anand44b53f62016-07-08 12:35:49 -0400323NOKPROBE_SYMBOL(call_break_hook);
Sandeepa Prabhuee6214c2013-12-04 05:50:20 +0000324
Will Deacon5a9132a2019-02-25 12:42:26 +0000325static int brk_handler(unsigned long unused, unsigned int esr,
Will Deacon1442b6e2013-03-16 08:48:13 +0000326 struct pt_regs *regs)
327{
Will Deaconab6211c2019-02-26 15:39:47 +0000328 if (call_break_hook(regs, esr) == DBG_HOOK_HANDLED)
329 return 0;
Pratyush Anand53d07e2182016-11-02 14:40:44 +0530330
Will Deaconab6211c2019-02-26 15:39:47 +0000331 if (user_mode(regs)) {
Pratyush Anand53d07e2182016-11-02 14:40:44 +0530332 send_user_sigtrap(TRAP_BRKPT);
Will Deaconab6211c2019-02-26 15:39:47 +0000333 } else {
Sandeepa Prabhu2dd0e8d2016-07-08 12:35:48 -0400334 pr_warn("Unexpected kernel BRK exception at EL1\n");
Will Deacon1442b6e2013-03-16 08:48:13 +0000335 return -EFAULT;
Will Deaconc878e0c2014-07-31 11:36:08 +0100336 }
Will Deacon1442b6e2013-03-16 08:48:13 +0000337
Will Deacon1442b6e2013-03-16 08:48:13 +0000338 return 0;
339}
Pratyush Anand44b53f62016-07-08 12:35:49 -0400340NOKPROBE_SYMBOL(brk_handler);
Will Deacon1442b6e2013-03-16 08:48:13 +0000341
342int aarch32_break_handler(struct pt_regs *regs)
343{
Matthew Leach2dacab72013-11-28 12:07:23 +0000344 u32 arm_instr;
345 u16 thumb_instr;
Will Deacon1442b6e2013-03-16 08:48:13 +0000346 bool bp = false;
347 void __user *pc = (void __user *)instruction_pointer(regs);
348
349 if (!compat_user_mode(regs))
350 return -EFAULT;
351
352 if (compat_thumb_mode(regs)) {
353 /* get 16-bit Thumb instruction */
Luc Van Oostenrycka5018b02017-06-28 16:55:52 +0200354 __le16 instr;
355 get_user(instr, (__le16 __user *)pc);
356 thumb_instr = le16_to_cpu(instr);
Matthew Leach2dacab72013-11-28 12:07:23 +0000357 if (thumb_instr == AARCH32_BREAK_THUMB2_LO) {
Will Deacon1442b6e2013-03-16 08:48:13 +0000358 /* get second half of 32-bit Thumb-2 instruction */
Luc Van Oostenrycka5018b02017-06-28 16:55:52 +0200359 get_user(instr, (__le16 __user *)(pc + 2));
360 thumb_instr = le16_to_cpu(instr);
Matthew Leach2dacab72013-11-28 12:07:23 +0000361 bp = thumb_instr == AARCH32_BREAK_THUMB2_HI;
Will Deacon1442b6e2013-03-16 08:48:13 +0000362 } else {
Matthew Leach2dacab72013-11-28 12:07:23 +0000363 bp = thumb_instr == AARCH32_BREAK_THUMB;
Will Deacon1442b6e2013-03-16 08:48:13 +0000364 }
365 } else {
366 /* 32-bit ARM instruction */
Luc Van Oostenrycka5018b02017-06-28 16:55:52 +0200367 __le32 instr;
368 get_user(instr, (__le32 __user *)pc);
369 arm_instr = le32_to_cpu(instr);
Matthew Leach2dacab72013-11-28 12:07:23 +0000370 bp = (arm_instr & ~0xf0000000) == AARCH32_BREAK_ARM;
Will Deacon1442b6e2013-03-16 08:48:13 +0000371 }
372
373 if (!bp)
374 return -EFAULT;
375
Will Deacone04a28d2016-02-10 16:05:28 +0000376 send_user_sigtrap(TRAP_BRKPT);
Will Deacon1442b6e2013-03-16 08:48:13 +0000377 return 0;
378}
Pratyush Anand44b53f62016-07-08 12:35:49 -0400379NOKPROBE_SYMBOL(aarch32_break_handler);
Will Deacon1442b6e2013-03-16 08:48:13 +0000380
Douglas Andersonb322c652020-05-13 16:06:37 -0700381void __init debug_traps_init(void)
Will Deacon478fcb22012-03-05 11:49:33 +0000382{
383 hook_debug_fault_code(DBG_ESR_EVT_HWSS, single_step_handler, SIGTRAP,
Will Deaconadeb68e2016-09-01 13:35:02 +0100384 TRAP_TRACE, "single-step handler");
Will Deacon1442b6e2013-03-16 08:48:13 +0000385 hook_debug_fault_code(DBG_ESR_EVT_BRK, brk_handler, SIGTRAP,
Will Deacon0fdb64c2020-09-15 15:48:09 +0100386 TRAP_BRKPT, "BRK handler");
Will Deacon478fcb22012-03-05 11:49:33 +0000387}
Will Deacon478fcb22012-03-05 11:49:33 +0000388
389/* Re-enable single step for syscall restarting. */
390void user_rewind_single_step(struct task_struct *task)
391{
392 /*
393 * If single step is active for this thread, then set SPSR.SS
394 * to 1 to avoid returning to the active-pending state.
395 */
Will Deacon5afc7852020-02-13 12:12:26 +0000396 if (test_tsk_thread_flag(task, TIF_SINGLESTEP))
Will Deacon478fcb22012-03-05 11:49:33 +0000397 set_regs_spsr_ss(task_pt_regs(task));
398}
Pratyush Anand44b53f62016-07-08 12:35:49 -0400399NOKPROBE_SYMBOL(user_rewind_single_step);
Will Deacon478fcb22012-03-05 11:49:33 +0000400
401void user_fastforward_single_step(struct task_struct *task)
402{
Will Deacon5afc7852020-02-13 12:12:26 +0000403 if (test_tsk_thread_flag(task, TIF_SINGLESTEP))
Will Deacon478fcb22012-03-05 11:49:33 +0000404 clear_regs_spsr_ss(task_pt_regs(task));
405}
406
Will Deacon3a5a4362020-02-13 12:06:26 +0000407void user_regs_reset_single_step(struct user_pt_regs *regs,
408 struct task_struct *task)
409{
410 if (test_tsk_thread_flag(task, TIF_SINGLESTEP))
411 set_user_regs_spsr_ss(regs);
412 else
413 clear_user_regs_spsr_ss(regs);
414}
415
Will Deacon478fcb22012-03-05 11:49:33 +0000416/* Kernel API */
417void kernel_enable_single_step(struct pt_regs *regs)
418{
419 WARN_ON(!irqs_disabled());
420 set_regs_spsr_ss(regs);
421 mdscr_write(mdscr_read() | DBG_MDSCR_SS);
422 enable_debug_monitors(DBG_ACTIVE_EL1);
423}
Pratyush Anand44b53f62016-07-08 12:35:49 -0400424NOKPROBE_SYMBOL(kernel_enable_single_step);
Will Deacon478fcb22012-03-05 11:49:33 +0000425
426void kernel_disable_single_step(void)
427{
428 WARN_ON(!irqs_disabled());
429 mdscr_write(mdscr_read() & ~DBG_MDSCR_SS);
430 disable_debug_monitors(DBG_ACTIVE_EL1);
431}
Pratyush Anand44b53f62016-07-08 12:35:49 -0400432NOKPROBE_SYMBOL(kernel_disable_single_step);
Will Deacon478fcb22012-03-05 11:49:33 +0000433
434int kernel_active_single_step(void)
435{
436 WARN_ON(!irqs_disabled());
437 return mdscr_read() & DBG_MDSCR_SS;
438}
Pratyush Anand44b53f62016-07-08 12:35:49 -0400439NOKPROBE_SYMBOL(kernel_active_single_step);
Will Deacon478fcb22012-03-05 11:49:33 +0000440
441/* ptrace API */
442void user_enable_single_step(struct task_struct *task)
443{
Will Deacon3a402a72016-08-26 11:36:39 +0100444 struct thread_info *ti = task_thread_info(task);
445
446 if (!test_and_set_ti_thread_flag(ti, TIF_SINGLESTEP))
447 set_regs_spsr_ss(task_pt_regs(task));
Will Deacon478fcb22012-03-05 11:49:33 +0000448}
Pratyush Anand44b53f62016-07-08 12:35:49 -0400449NOKPROBE_SYMBOL(user_enable_single_step);
Will Deacon478fcb22012-03-05 11:49:33 +0000450
451void user_disable_single_step(struct task_struct *task)
452{
453 clear_ti_thread_flag(task_thread_info(task), TIF_SINGLESTEP);
454}
Pratyush Anand44b53f62016-07-08 12:35:49 -0400455NOKPROBE_SYMBOL(user_disable_single_step);