Merge tag 'usb-ci-v5.9-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/peter...
[linux-block.git] / arch / x86 / mm / mmio-mod.c
CommitLineData
1a59d1b8 1// SPDX-License-Identifier: GPL-2.0-or-later
8b7d89d0 2/*
8b7d89d0
PP
3 *
4 * Copyright (C) IBM Corporation, 2005
5 * Jeff Muizelaar, 2006, 2007
6 * Pekka Paalanen, 2008 <pq@iki.fi>
7 *
8 * Derived from the read-mod example from relay-examples by Tom Zanussi.
9 */
3a0340be 10
eba11d6d 11#define pr_fmt(fmt) "mmiotrace: " fmt
3a0340be 12
d61fc448
PP
13#define DEBUG 1
14
4b599fed 15#include <linux/moduleparam.h>
8b7d89d0 16#include <linux/debugfs.h>
5a0e3ad6 17#include <linux/slab.h>
f984b51e 18#include <linux/uaccess.h>
970e6fa0 19#include <linux/io.h>
8b7d89d0 20#include <linux/mmiotrace.h>
65fddcfc 21#include <linux/pgtable.h>
66441bd3 22#include <asm/e820/api.h> /* for ISA_START_ADDRESS */
60063497 23#include <linux/atomic.h>
f5136380 24#include <linux/percpu.h>
7423d111 25#include <linux/cpu.h>
8b7d89d0 26
8b7d89d0
PP
27#include "pf_in.h"
28
8b7d89d0
PP
29struct trap_reason {
30 unsigned long addr;
31 unsigned long ip;
32 enum reason_type type;
33 int active_traces;
34};
35
d61fc448
PP
36struct remap_trace {
37 struct list_head list;
38 struct kmmio_probe probe;
dee310d0 39 resource_size_t phys;
d61fc448
PP
40 unsigned long id;
41};
42
fe1ffafa 43/* Accessed per-cpu. */
f5136380 44static DEFINE_PER_CPU(struct trap_reason, pf_reason);
bd8ac686 45static DEFINE_PER_CPU(struct mmiotrace_rw, cpu_trace);
8b7d89d0 46
d61fc448
PP
47static DEFINE_MUTEX(mmiotrace_mutex);
48static DEFINE_SPINLOCK(trace_lock);
49static atomic_t mmiotrace_enabled;
50static LIST_HEAD(trace_list); /* struct remap_trace */
d61fc448
PP
51
52/*
53 * Locking in this file:
54 * - mmiotrace_mutex enforces enable/disable_mmiotrace() critical sections.
55 * - mmiotrace_enabled may be modified only when holding mmiotrace_mutex
56 * and trace_lock.
57 * - Routines depending on is_enabled() must take trace_lock.
58 * - trace_list users must hold trace_lock.
9e57fb35 59 * - is_enabled() guarantees that mmio_trace_{rw,mapping} are allowed.
d61fc448
PP
60 * - pre/post callbacks assume the effect of is_enabled() being true.
61 */
8b7d89d0
PP
62
63/* module parameters */
d61fc448 64static unsigned long filter_offset;
476bc001
RR
65static bool nommiotrace;
66static bool trace_pc;
8b7d89d0 67
8b7d89d0
PP
68module_param(filter_offset, ulong, 0);
69module_param(nommiotrace, bool, 0);
8b7d89d0
PP
70module_param(trace_pc, bool, 0);
71
8b7d89d0
PP
72MODULE_PARM_DESC(filter_offset, "Start address of traced mappings.");
73MODULE_PARM_DESC(nommiotrace, "Disable actual MMIO tracing.");
8b7d89d0 74MODULE_PARM_DESC(trace_pc, "Record address of faulting instructions.");
d61fc448
PP
75
76static bool is_enabled(void)
77{
78 return atomic_read(&mmiotrace_enabled);
79}
8b7d89d0 80
8b7d89d0
PP
81static void print_pte(unsigned long address)
82{
790e2a29 83 unsigned int level;
75bb8835
PP
84 pte_t *pte = lookup_address(address, &level);
85
86 if (!pte) {
3a0340be
JP
87 pr_err("Error in %s: no pte for page 0x%08lx\n",
88 __func__, address);
75bb8835
PP
89 return;
90 }
91
92 if (level == PG_LEVEL_2M) {
3a0340be
JP
93 pr_emerg("4MB pages are not currently supported: 0x%08lx\n",
94 address);
8b7d89d0
PP
95 BUG();
96 }
3a0340be
JP
97 pr_info("pte for 0x%lx: 0x%llx 0x%llx\n",
98 address,
0663bb6c
RD
99 (unsigned long long)pte_val(*pte),
100 (unsigned long long)pte_val(*pte) & _PAGE_PRESENT);
8b7d89d0
PP
101}
102
103/*
104 * For some reason the pre/post pairs have been called in an
105 * unmatched order. Report and die.
106 */
107static void die_kmmio_nesting_error(struct pt_regs *regs, unsigned long addr)
108{
f5136380 109 const struct trap_reason *my_reason = &get_cpu_var(pf_reason);
3a0340be
JP
110 pr_emerg("unexpected fault for address: 0x%08lx, last fault for address: 0x%08lx\n",
111 addr, my_reason->addr);
8b7d89d0 112 print_pte(addr);
7b606162
SS
113 pr_emerg("faulting IP is at %pS\n", (void *)regs->ip);
114 pr_emerg("last faulting IP was at %pS\n", (void *)my_reason->ip);
8b7d89d0 115#ifdef __i386__
0fd0e3da 116 pr_emerg("eax: %08lx ebx: %08lx ecx: %08lx edx: %08lx\n",
3a0340be 117 regs->ax, regs->bx, regs->cx, regs->dx);
0fd0e3da 118 pr_emerg("esi: %08lx edi: %08lx ebp: %08lx esp: %08lx\n",
3a0340be 119 regs->si, regs->di, regs->bp, regs->sp);
8b7d89d0 120#else
0fd0e3da 121 pr_emerg("rax: %016lx rcx: %016lx rdx: %016lx\n",
3a0340be 122 regs->ax, regs->cx, regs->dx);
0fd0e3da 123 pr_emerg("rsi: %016lx rdi: %016lx rbp: %016lx rsp: %016lx\n",
3a0340be 124 regs->si, regs->di, regs->bp, regs->sp);
8b7d89d0 125#endif
f5136380 126 put_cpu_var(pf_reason);
8b7d89d0
PP
127 BUG();
128}
129
130static void pre(struct kmmio_probe *p, struct pt_regs *regs,
131 unsigned long addr)
132{
f5136380 133 struct trap_reason *my_reason = &get_cpu_var(pf_reason);
bd8ac686 134 struct mmiotrace_rw *my_trace = &get_cpu_var(cpu_trace);
8b7d89d0
PP
135 const unsigned long instptr = instruction_pointer(regs);
136 const enum reason_type type = get_ins_type(instptr);
a50445d7 137 struct remap_trace *trace = p->private;
8b7d89d0
PP
138
139 /* it doesn't make sense to have more than one active trace per cpu */
f5136380 140 if (my_reason->active_traces)
8b7d89d0
PP
141 die_kmmio_nesting_error(regs, addr);
142 else
f5136380 143 my_reason->active_traces++;
8b7d89d0 144
f5136380
PP
145 my_reason->type = type;
146 my_reason->addr = addr;
147 my_reason->ip = instptr;
8b7d89d0 148
bd8ac686
PP
149 my_trace->phys = addr - trace->probe.addr + trace->phys;
150 my_trace->map_id = trace->id;
8b7d89d0
PP
151
152 /*
153 * Only record the program counter when requested.
154 * It may taint clean-room reverse engineering.
155 */
156 if (trace_pc)
bd8ac686 157 my_trace->pc = instptr;
8b7d89d0 158 else
bd8ac686 159 my_trace->pc = 0;
8b7d89d0 160
f984b51e
PP
161 /*
162 * XXX: the timestamp recorded will be *after* the tracing has been
163 * done, not at the time we hit the instruction. SMP implications
164 * on event ordering?
165 */
8b7d89d0
PP
166
167 switch (type) {
168 case REG_READ:
bd8ac686
PP
169 my_trace->opcode = MMIO_READ;
170 my_trace->width = get_ins_mem_width(instptr);
8b7d89d0
PP
171 break;
172 case REG_WRITE:
bd8ac686
PP
173 my_trace->opcode = MMIO_WRITE;
174 my_trace->width = get_ins_mem_width(instptr);
175 my_trace->value = get_ins_reg_val(instptr, regs);
8b7d89d0
PP
176 break;
177 case IMM_WRITE:
bd8ac686
PP
178 my_trace->opcode = MMIO_WRITE;
179 my_trace->width = get_ins_mem_width(instptr);
180 my_trace->value = get_ins_imm_val(instptr);
8b7d89d0
PP
181 break;
182 default:
183 {
184 unsigned char *ip = (unsigned char *)instptr;
bd8ac686
PP
185 my_trace->opcode = MMIO_UNKNOWN_OP;
186 my_trace->width = 0;
187 my_trace->value = (*ip) << 16 | *(ip + 1) << 8 |
f5136380 188 *(ip + 2);
8b7d89d0
PP
189 }
190 }
f5136380
PP
191 put_cpu_var(cpu_trace);
192 put_cpu_var(pf_reason);
8b7d89d0
PP
193}
194
195static void post(struct kmmio_probe *p, unsigned long condition,
196 struct pt_regs *regs)
197{
f5136380 198 struct trap_reason *my_reason = &get_cpu_var(pf_reason);
bd8ac686 199 struct mmiotrace_rw *my_trace = &get_cpu_var(cpu_trace);
8b7d89d0
PP
200
201 /* this should always return the active_trace count to 0 */
f5136380
PP
202 my_reason->active_traces--;
203 if (my_reason->active_traces) {
3a0340be 204 pr_emerg("unexpected post handler");
8b7d89d0
PP
205 BUG();
206 }
207
f5136380 208 switch (my_reason->type) {
8b7d89d0 209 case REG_READ:
bd8ac686 210 my_trace->value = get_ins_reg_val(my_reason->ip, regs);
8b7d89d0
PP
211 break;
212 default:
213 break;
214 }
f984b51e 215
bd8ac686 216 mmio_trace_rw(my_trace);
f5136380
PP
217 put_cpu_var(cpu_trace);
218 put_cpu_var(pf_reason);
8b7d89d0
PP
219}
220
dee310d0 221static void ioremap_trace_core(resource_size_t offset, unsigned long size,
8b7d89d0
PP
222 void __iomem *addr)
223{
d61fc448 224 static atomic_t next_id;
8b7d89d0 225 struct remap_trace *trace = kmalloc(sizeof(*trace), GFP_KERNEL);
87e547fe 226 /* These are page-unaligned. */
bd8ac686
PP
227 struct mmiotrace_map map = {
228 .phys = offset,
229 .virt = (unsigned long)addr,
230 .len = size,
231 .opcode = MMIO_PROBE
8b7d89d0 232 };
8b7d89d0 233
d61fc448 234 if (!trace) {
3a0340be 235 pr_err("kmalloc failed in ioremap\n");
d61fc448
PP
236 return;
237 }
238
8b7d89d0
PP
239 *trace = (struct remap_trace) {
240 .probe = {
241 .addr = (unsigned long)addr,
242 .len = size,
243 .pre_handler = pre,
244 .post_handler = post,
a50445d7 245 .private = trace
d61fc448
PP
246 },
247 .phys = offset,
248 .id = atomic_inc_return(&next_id)
8b7d89d0 249 };
bd8ac686 250 map.map_id = trace->id;
8b7d89d0 251
d61fc448 252 spin_lock_irq(&trace_lock);
bbe5c783
PP
253 if (!is_enabled()) {
254 kfree(trace);
d61fc448 255 goto not_enabled;
bbe5c783 256 }
d61fc448 257
bd8ac686 258 mmio_trace_mapping(&map);
8b7d89d0 259 list_add_tail(&trace->list, &trace_list);
8b7d89d0
PP
260 if (!nommiotrace)
261 register_kmmio_probe(&trace->probe);
d61fc448
PP
262
263not_enabled:
264 spin_unlock_irq(&trace_lock);
8b7d89d0
PP
265}
266
dee310d0
PP
267void mmiotrace_ioremap(resource_size_t offset, unsigned long size,
268 void __iomem *addr)
8b7d89d0 269{
d61fc448 270 if (!is_enabled()) /* recheck and proper locking in *_core() */
8b7d89d0
PP
271 return;
272
3a0340be
JP
273 pr_debug("ioremap_*(0x%llx, 0x%lx) = %p\n",
274 (unsigned long long)offset, size, addr);
d61fc448 275 if ((filter_offset) && (offset != filter_offset))
8b7d89d0 276 return;
d61fc448 277 ioremap_trace_core(offset, size, addr);
8b7d89d0 278}
8b7d89d0 279
d61fc448 280static void iounmap_trace_core(volatile void __iomem *addr)
8b7d89d0 281{
bd8ac686
PP
282 struct mmiotrace_map map = {
283 .phys = 0,
284 .virt = (unsigned long)addr,
285 .len = 0,
286 .opcode = MMIO_UNPROBE
8b7d89d0
PP
287 };
288 struct remap_trace *trace;
289 struct remap_trace *tmp;
d61fc448
PP
290 struct remap_trace *found_trace = NULL;
291
3a0340be 292 pr_debug("Unmapping %p.\n", addr);
8b7d89d0 293
d61fc448
PP
294 spin_lock_irq(&trace_lock);
295 if (!is_enabled())
296 goto not_enabled;
297
8b7d89d0
PP
298 list_for_each_entry_safe(trace, tmp, &trace_list, list) {
299 if ((unsigned long)addr == trace->probe.addr) {
300 if (!nommiotrace)
301 unregister_kmmio_probe(&trace->probe);
302 list_del(&trace->list);
d61fc448 303 found_trace = trace;
8b7d89d0
PP
304 break;
305 }
306 }
bd8ac686
PP
307 map.map_id = (found_trace) ? found_trace->id : -1;
308 mmio_trace_mapping(&map);
d61fc448
PP
309
310not_enabled:
311 spin_unlock_irq(&trace_lock);
312 if (found_trace) {
313 synchronize_rcu(); /* unregister_kmmio_probe() requirement */
314 kfree(found_trace);
315 }
316}
317
318void mmiotrace_iounmap(volatile void __iomem *addr)
319{
320 might_sleep();
321 if (is_enabled()) /* recheck and proper locking in *_core() */
322 iounmap_trace_core(addr);
8b7d89d0 323}
8b7d89d0 324
9e57fb35
PP
325int mmiotrace_printk(const char *fmt, ...)
326{
327 int ret = 0;
328 va_list args;
329 unsigned long flags;
330 va_start(args, fmt);
331
332 spin_lock_irqsave(&trace_lock, flags);
333 if (is_enabled())
334 ret = mmio_trace_printk(fmt, args);
335 spin_unlock_irqrestore(&trace_lock, flags);
336
337 va_end(args);
338 return ret;
339}
340EXPORT_SYMBOL(mmiotrace_printk);
341
8b7d89d0
PP
342static void clear_trace_list(void)
343{
344 struct remap_trace *trace;
345 struct remap_trace *tmp;
346
d61fc448
PP
347 /*
348 * No locking required, because the caller ensures we are in a
349 * critical section via mutex, and is_enabled() is false,
350 * i.e. nothing can traverse or modify this list.
351 * Caller also ensures is_enabled() cannot change.
352 */
353 list_for_each_entry(trace, &trace_list, list) {
3a0340be
JP
354 pr_notice("purging non-iounmapped trace @0x%08lx, size 0x%lx.\n",
355 trace->probe.addr, trace->probe.len);
8b7d89d0
PP
356 if (!nommiotrace)
357 unregister_kmmio_probe(&trace->probe);
d61fc448
PP
358 }
359 synchronize_rcu(); /* unregister_kmmio_probe() requirement */
360
361 list_for_each_entry_safe(trace, tmp, &trace_list, list) {
8b7d89d0
PP
362 list_del(&trace->list);
363 kfree(trace);
d61fc448
PP
364 }
365}
366
7423d111 367#ifdef CONFIG_HOTPLUG_CPU
c38da569 368static cpumask_var_t downed_cpus;
7423d111
PP
369
370static void enter_uniprocessor(void)
371{
372 int cpu;
373 int err;
374
d7110a26 375 if (!cpumask_available(downed_cpus) &&
c38da569 376 !alloc_cpumask_var(&downed_cpus, GFP_KERNEL)) {
3a0340be 377 pr_notice("Failed to allocate mask\n");
c38da569
RR
378 goto out;
379 }
380
7423d111 381 get_online_cpus();
c38da569
RR
382 cpumask_copy(downed_cpus, cpu_online_mask);
383 cpumask_clear_cpu(cpumask_first(cpu_online_mask), downed_cpus);
7423d111 384 if (num_online_cpus() > 1)
3a0340be 385 pr_notice("Disabling non-boot CPUs...\n");
7423d111
PP
386 put_online_cpus();
387
c38da569 388 for_each_cpu(cpu, downed_cpus) {
af7aa046 389 err = remove_cpu(cpu);
970e6fa0 390 if (!err)
3a0340be 391 pr_info("CPU%d is down.\n", cpu);
970e6fa0 392 else
3a0340be 393 pr_err("Error taking CPU%d down: %d\n", cpu, err);
7423d111 394 }
c38da569 395out:
7423d111 396 if (num_online_cpus() > 1)
8d3bcc44 397 pr_warn("multiple CPUs still online, may miss events.\n");
7423d111
PP
398}
399
148f9bb8 400static void leave_uniprocessor(void)
7423d111
PP
401{
402 int cpu;
403 int err;
404
d7110a26 405 if (!cpumask_available(downed_cpus) || cpumask_weight(downed_cpus) == 0)
7423d111 406 return;
3a0340be 407 pr_notice("Re-enabling CPUs...\n");
c38da569 408 for_each_cpu(cpu, downed_cpus) {
af7aa046 409 err = add_cpu(cpu);
7423d111 410 if (!err)
3a0340be 411 pr_info("enabled CPU%d.\n", cpu);
7423d111 412 else
3a0340be 413 pr_err("cannot re-enable CPU%d: %d\n", cpu, err);
7423d111
PP
414 }
415}
416
417#else /* !CONFIG_HOTPLUG_CPU */
418static void enter_uniprocessor(void)
419{
420 if (num_online_cpus() > 1)
8d3bcc44
KW
421 pr_warn("multiple CPUs are online, may miss events. "
422 "Suggest booting with maxcpus=1 kernel argument.\n");
7423d111
PP
423}
424
425static void leave_uniprocessor(void)
426{
427}
428#endif
429
f984b51e 430void enable_mmiotrace(void)
d61fc448
PP
431{
432 mutex_lock(&mmiotrace_mutex);
433 if (is_enabled())
434 goto out;
435
d61fc448 436 if (nommiotrace)
3a0340be 437 pr_info("MMIO tracing disabled.\n");
0f9a623d 438 kmmio_init();
7423d111 439 enter_uniprocessor();
d61fc448
PP
440 spin_lock_irq(&trace_lock);
441 atomic_inc(&mmiotrace_enabled);
442 spin_unlock_irq(&trace_lock);
3a0340be 443 pr_info("enabled.\n");
d61fc448
PP
444out:
445 mutex_unlock(&mmiotrace_mutex);
446}
447
f984b51e 448void disable_mmiotrace(void)
d61fc448
PP
449{
450 mutex_lock(&mmiotrace_mutex);
451 if (!is_enabled())
452 goto out;
453
454 spin_lock_irq(&trace_lock);
455 atomic_dec(&mmiotrace_enabled);
456 BUG_ON(is_enabled());
457 spin_unlock_irq(&trace_lock);
458
459 clear_trace_list(); /* guarantees: no more kmmio callbacks */
7423d111 460 leave_uniprocessor();
0f9a623d 461 kmmio_cleanup();
3a0340be 462 pr_info("disabled.\n");
d61fc448
PP
463out:
464 mutex_unlock(&mmiotrace_mutex);
8b7d89d0 465}