1/*
2 * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 */
7
8#include <linux/ptrace.h>
9#include <linux/module.h>
10#include <linux/mm.h>
11#include <linux/fs.h>
12#include <linux/kdev_t.h>
13#include <linux/fs_struct.h>
14#include <linux/proc_fs.h>
15#include <linux/file.h>
16#include <asm/arcregs.h>
17
18/*
19 * Common routine to print scratch regs (r0-r12) or callee regs (r13-r25)
20 *   -Prints 3 regs per line and a CR.
21 *   -To continue, callee regs right after scratch, special handling of CR
22 */
23static noinline void print_reg_file(long *reg_rev, int start_num)
24{
25	unsigned int i;
26	char buf[512];
27	int n = 0, len = sizeof(buf);
28
29	for (i = start_num; i < start_num + 13; i++) {
30		n += scnprintf(buf + n, len - n, "r%02u: 0x%08lx\t",
31			       i, (unsigned long)*reg_rev);
32
33		if (((i + 1) % 3) == 0)
34			n += scnprintf(buf + n, len - n, "\n");
35
36		/* because pt_regs has regs reversed: r12..r0, r25..r13 */
37		reg_rev--;
38	}
39
40	if (start_num != 0)
41		n += scnprintf(buf + n, len - n, "\n\n");
42
43	/* To continue printing callee regs on same line as scratch regs */
44	if (start_num == 0)
45		pr_info("%s", buf);
46	else
47		pr_cont("%s\n", buf);
48}
49
50static void show_callee_regs(struct callee_regs *cregs)
51{
52	print_reg_file(&(cregs->r13), 13);
53}
54
55static void print_task_path_n_nm(struct task_struct *tsk, char *buf)
56{
57	struct path path;
58	char *path_nm = NULL;
59	struct mm_struct *mm;
60	struct file *exe_file;
61
62	mm = get_task_mm(tsk);
63	if (!mm)
64		goto done;
65
66	exe_file = get_mm_exe_file(mm);
67	mmput(mm);
68
69	if (exe_file) {
70		path = exe_file->f_path;
71		path_get(&exe_file->f_path);
72		fput(exe_file);
73		path_nm = d_path(&path, buf, 255);
74		path_put(&path);
75	}
76
77done:
78	pr_info("Path: %s\n", path_nm);
79}
80
81static void show_faulting_vma(unsigned long address, char *buf)
82{
83	struct vm_area_struct *vma;
84	struct inode *inode;
85	unsigned long ino = 0;
86	dev_t dev = 0;
87	char *nm = buf;
88	struct mm_struct *active_mm = current->active_mm;
89
90	/* can't use print_vma_addr() yet as it doesn't check for
91	 * non-inclusive vma
92	 */
93	down_read(&active_mm->mmap_sem);
94	vma = find_vma(active_mm, address);
95
96	/* check against the find_vma( ) behaviour which returns the next VMA
97	 * if the container VMA is not found
98	 */
99	if (vma && (vma->vm_start <= address)) {
100		struct file *file = vma->vm_file;
101		if (file) {
102			struct path *path = &file->f_path;
103			nm = d_path(path, buf, PAGE_SIZE - 1);
104			inode = file_inode(vma->vm_file);
105			dev = inode->i_sb->s_dev;
106			ino = inode->i_ino;
107		}
108		pr_info("    @off 0x%lx in [%s]\n"
109			"    VMA: 0x%08lx to 0x%08lx\n",
110			vma->vm_start < TASK_UNMAPPED_BASE ?
111				address : address - vma->vm_start,
112			nm, vma->vm_start, vma->vm_end);
113	} else
114		pr_info("    @No matching VMA found\n");
115
116	up_read(&active_mm->mmap_sem);
117}
118
119static void show_ecr_verbose(struct pt_regs *regs)
120{
121	unsigned int vec, cause_code;
122	unsigned long address;
123
124	pr_info("\n[ECR   ]: 0x%08lx => ", regs->event);
125
126	/* For Data fault, this is data address not instruction addr */
127	address = current->thread.fault_address;
128
129	vec = regs->ecr_vec;
130	cause_code = regs->ecr_cause;
131
132	/* For DTLB Miss or ProtV, display the memory involved too */
133	if (vec == ECR_V_DTLB_MISS) {
134		pr_cont("Invalid %s @ 0x%08lx by insn @ 0x%08lx\n",
135		       (cause_code == 0x01) ? "Read" :
136		       ((cause_code == 0x02) ? "Write" : "EX"),
137		       address, regs->ret);
138	} else if (vec == ECR_V_ITLB_MISS) {
139		pr_cont("Insn could not be fetched\n");
140	} else if (vec == ECR_V_MACH_CHK) {
141		pr_cont("%s\n", (cause_code == 0x0) ?
142					"Double Fault" : "Other Fatal Err");
143
144	} else if (vec == ECR_V_PROTV) {
145		if (cause_code == ECR_C_PROTV_INST_FETCH)
146			pr_cont("Execute from Non-exec Page\n");
147		else if (cause_code == ECR_C_PROTV_MISALIG_DATA)
148			pr_cont("Misaligned r/w from 0x%08lx\n", address);
149		else
150			pr_cont("%s access not allowed on page\n",
151				(cause_code == 0x01) ? "Read" :
152				((cause_code == 0x02) ? "Write" : "EX"));
153	} else if (vec == ECR_V_INSN_ERR) {
154		pr_cont("Illegal Insn\n");
155	} else {
156		pr_cont("Check Programmer's Manual\n");
157	}
158}
159
160/************************************************************************
161 *  API called by rest of kernel
162 ***********************************************************************/
163
164void show_regs(struct pt_regs *regs)
165{
166	struct task_struct *tsk = current;
167	struct callee_regs *cregs;
168	char *buf;
169
170	buf = (char *)__get_free_page(GFP_TEMPORARY);
171	if (!buf)
172		return;
173
174	print_task_path_n_nm(tsk, buf);
175	show_regs_print_info(KERN_INFO);
176
177	show_ecr_verbose(regs);
178
179	pr_info("[EFA   ]: 0x%08lx\n[BLINK ]: %pS\n[ERET  ]: %pS\n",
180		current->thread.fault_address,
181		(void *)regs->blink, (void *)regs->ret);
182
183	if (user_mode(regs))
184		show_faulting_vma(regs->ret, buf); /* faulting code, not data */
185
186	pr_info("[STAT32]: 0x%08lx", regs->status32);
187
188#define STS_BIT(r, bit)	r->status32 & STATUS_##bit##_MASK ? #bit : ""
189	if (!user_mode(regs))
190		pr_cont(" : %2s %2s %2s %2s %2s\n",
191			STS_BIT(regs, AE), STS_BIT(regs, A2), STS_BIT(regs, A1),
192			STS_BIT(regs, E2), STS_BIT(regs, E1));
193
194	pr_info("BTA: 0x%08lx\t SP: 0x%08lx\t FP: 0x%08lx\n",
195		regs->bta, regs->sp, regs->fp);
196	pr_info("LPS: 0x%08lx\tLPE: 0x%08lx\tLPC: 0x%08lx\n",
197	       regs->lp_start, regs->lp_end, regs->lp_count);
198
199	/* print regs->r0 thru regs->r12
200	 * Sequential printing was generating horrible code
201	 */
202	print_reg_file(&(regs->r0), 0);
203
204	/* If Callee regs were saved, display them too */
205	cregs = (struct callee_regs *)current->thread.callee_reg;
206	if (cregs)
207		show_callee_regs(cregs);
208
209	free_page((unsigned long)buf);
210}
211
212void show_kernel_fault_diag(const char *str, struct pt_regs *regs,
213			    unsigned long address)
214{
215	current->thread.fault_address = address;
216
217	/* Caller and Callee regs */
218	show_regs(regs);
219
220	/* Show stack trace if this Fatality happened in kernel mode */
221	if (!user_mode(regs))
222		show_stacktrace(current, regs);
223}
224
225#ifdef CONFIG_DEBUG_FS
226
227#include <linux/module.h>
228#include <linux/fs.h>
229#include <linux/mount.h>
230#include <linux/pagemap.h>
231#include <linux/init.h>
232#include <linux/namei.h>
233#include <linux/debugfs.h>
234
235static struct dentry *test_dentry;
236static struct dentry *test_dir;
237static struct dentry *test_u32_dentry;
238
239static u32 clr_on_read = 1;
240
241#ifdef CONFIG_ARC_DBG_TLB_MISS_COUNT
242u32 numitlb, numdtlb, num_pte_not_present;
243
244static int fill_display_data(char *kbuf)
245{
246	size_t num = 0;
247	num += sprintf(kbuf + num, "I-TLB Miss %x\n", numitlb);
248	num += sprintf(kbuf + num, "D-TLB Miss %x\n", numdtlb);
249	num += sprintf(kbuf + num, "PTE not present %x\n", num_pte_not_present);
250
251	if (clr_on_read)
252		numitlb = numdtlb = num_pte_not_present = 0;
253
254	return num;
255}
256
257static int tlb_stats_open(struct inode *inode, struct file *file)
258{
259	file->private_data = (void *)__get_free_page(GFP_KERNEL);
260	return 0;
261}
262
263/* called on user read(): display the couters */
264static ssize_t tlb_stats_output(struct file *file,	/* file descriptor */
265				char __user *user_buf,	/* user buffer */
266				size_t len,		/* length of buffer */
267				loff_t *offset)		/* offset in the file */
268{
269	size_t num;
270	char *kbuf = (char *)file->private_data;
271
272	/* All of the data can he shoved in one iteration */
273	if (*offset != 0)
274		return 0;
275
276	num = fill_display_data(kbuf);
277
278	/* simple_read_from_buffer() is helper for copy to user space
279	   It copies up to @2 (num) bytes from kernel buffer @4 (kbuf) at offset
280	   @3 (offset) into the user space address starting at @1 (user_buf).
281	   @5 (len) is max size of user buffer
282	 */
283	return simple_read_from_buffer(user_buf, num, offset, kbuf, len);
284}
285
286/* called on user write : clears the counters */
287static ssize_t tlb_stats_clear(struct file *file, const char __user *user_buf,
288			       size_t length, loff_t *offset)
289{
290	numitlb = numdtlb = num_pte_not_present = 0;
291	return length;
292}
293
294static int tlb_stats_close(struct inode *inode, struct file *file)
295{
296	free_page((unsigned long)(file->private_data));
297	return 0;
298}
299
300static const struct file_operations tlb_stats_file_ops = {
301	.read = tlb_stats_output,
302	.write = tlb_stats_clear,
303	.open = tlb_stats_open,
304	.release = tlb_stats_close
305};
306#endif
307
308static int __init arc_debugfs_init(void)
309{
310	test_dir = debugfs_create_dir("arc", NULL);
311
312#ifdef CONFIG_ARC_DBG_TLB_MISS_COUNT
313	test_dentry = debugfs_create_file("tlb_stats", 0444, test_dir, NULL,
314					  &tlb_stats_file_ops);
315#endif
316
317	test_u32_dentry =
318	    debugfs_create_u32("clr_on_read", 0444, test_dir, &clr_on_read);
319
320	return 0;
321}
322
323module_init(arc_debugfs_init);
324
325static void __exit arc_debugfs_exit(void)
326{
327	debugfs_remove(test_u32_dentry);
328	debugfs_remove(test_dentry);
329	debugfs_remove(test_dir);
330}
331module_exit(arc_debugfs_exit);
332
333#endif
334