]> git.ipfire.org Git - thirdparty/kernel/stable.git/blob - arch/x86/kernel/kdebugfs.c
treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 428
[thirdparty/kernel/stable.git] / arch / x86 / kernel / kdebugfs.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Architecture specific debugfs files
4 *
5 * Copyright (C) 2007, Intel Corp.
6 * Huang Ying <ying.huang@intel.com>
7 */
8 #include <linux/debugfs.h>
9 #include <linux/uaccess.h>
10 #include <linux/export.h>
11 #include <linux/slab.h>
12 #include <linux/init.h>
13 #include <linux/stat.h>
14 #include <linux/io.h>
15 #include <linux/mm.h>
16
17 #include <asm/setup.h>
18
19 struct dentry *arch_debugfs_dir;
20 EXPORT_SYMBOL(arch_debugfs_dir);
21
22 #ifdef CONFIG_DEBUG_BOOT_PARAMS
23 struct setup_data_node {
24 u64 paddr;
25 u32 type;
26 u32 len;
27 };
28
29 static ssize_t setup_data_read(struct file *file, char __user *user_buf,
30 size_t count, loff_t *ppos)
31 {
32 struct setup_data_node *node = file->private_data;
33 unsigned long remain;
34 loff_t pos = *ppos;
35 void *p;
36 u64 pa;
37
38 if (pos < 0)
39 return -EINVAL;
40
41 if (pos >= node->len)
42 return 0;
43
44 if (count > node->len - pos)
45 count = node->len - pos;
46
47 pa = node->paddr + sizeof(struct setup_data) + pos;
48 p = memremap(pa, count, MEMREMAP_WB);
49 if (!p)
50 return -ENOMEM;
51
52 remain = copy_to_user(user_buf, p, count);
53
54 memunmap(p);
55
56 if (remain)
57 return -EFAULT;
58
59 *ppos = pos + count;
60
61 return count;
62 }
63
64 static const struct file_operations fops_setup_data = {
65 .read = setup_data_read,
66 .open = simple_open,
67 .llseek = default_llseek,
68 };
69
70 static int __init
71 create_setup_data_node(struct dentry *parent, int no,
72 struct setup_data_node *node)
73 {
74 struct dentry *d, *type, *data;
75 char buf[16];
76
77 sprintf(buf, "%d", no);
78 d = debugfs_create_dir(buf, parent);
79 if (!d)
80 return -ENOMEM;
81
82 type = debugfs_create_x32("type", S_IRUGO, d, &node->type);
83 if (!type)
84 goto err_dir;
85
86 data = debugfs_create_file("data", S_IRUGO, d, node, &fops_setup_data);
87 if (!data)
88 goto err_type;
89
90 return 0;
91
92 err_type:
93 debugfs_remove(type);
94 err_dir:
95 debugfs_remove(d);
96 return -ENOMEM;
97 }
98
99 static int __init create_setup_data_nodes(struct dentry *parent)
100 {
101 struct setup_data_node *node;
102 struct setup_data *data;
103 int error;
104 struct dentry *d;
105 u64 pa_data;
106 int no = 0;
107
108 d = debugfs_create_dir("setup_data", parent);
109 if (!d)
110 return -ENOMEM;
111
112 pa_data = boot_params.hdr.setup_data;
113
114 while (pa_data) {
115 node = kmalloc(sizeof(*node), GFP_KERNEL);
116 if (!node) {
117 error = -ENOMEM;
118 goto err_dir;
119 }
120
121 data = memremap(pa_data, sizeof(*data), MEMREMAP_WB);
122 if (!data) {
123 kfree(node);
124 error = -ENOMEM;
125 goto err_dir;
126 }
127
128 node->paddr = pa_data;
129 node->type = data->type;
130 node->len = data->len;
131 error = create_setup_data_node(d, no, node);
132 pa_data = data->next;
133
134 memunmap(data);
135 if (error)
136 goto err_dir;
137 no++;
138 }
139
140 return 0;
141
142 err_dir:
143 debugfs_remove(d);
144 return error;
145 }
146
147 static struct debugfs_blob_wrapper boot_params_blob = {
148 .data = &boot_params,
149 .size = sizeof(boot_params),
150 };
151
152 static int __init boot_params_kdebugfs_init(void)
153 {
154 struct dentry *dbp, *version, *data;
155 int error = -ENOMEM;
156
157 dbp = debugfs_create_dir("boot_params", arch_debugfs_dir);
158 if (!dbp)
159 return -ENOMEM;
160
161 version = debugfs_create_x16("version", S_IRUGO, dbp,
162 &boot_params.hdr.version);
163 if (!version)
164 goto err_dir;
165
166 data = debugfs_create_blob("data", S_IRUGO, dbp,
167 &boot_params_blob);
168 if (!data)
169 goto err_version;
170
171 error = create_setup_data_nodes(dbp);
172 if (error)
173 goto err_data;
174
175 return 0;
176
177 err_data:
178 debugfs_remove(data);
179 err_version:
180 debugfs_remove(version);
181 err_dir:
182 debugfs_remove(dbp);
183 return error;
184 }
185 #endif /* CONFIG_DEBUG_BOOT_PARAMS */
186
187 static int __init arch_kdebugfs_init(void)
188 {
189 int error = 0;
190
191 arch_debugfs_dir = debugfs_create_dir("x86", NULL);
192 if (!arch_debugfs_dir)
193 return -ENOMEM;
194
195 #ifdef CONFIG_DEBUG_BOOT_PARAMS
196 error = boot_params_kdebugfs_init();
197 #endif
198
199 return error;
200 }
201 arch_initcall(arch_kdebugfs_init);