Linux Audio

Check our new training course

Yocto distribution development and maintenance

Need a Yocto distribution for your embedded project?
Loading...
v3.1
 
  1/*
  2 * APEI Error Record Serialization Table debug support
  3 *
  4 * ERST is a way provided by APEI to save and retrieve hardware error
  5 * information to and from a persistent store. This file provide the
  6 * debugging/testing support for ERST kernel support and firmware
  7 * implementation.
  8 *
  9 * Copyright 2010 Intel Corp.
 10 *   Author: Huang Ying <ying.huang@intel.com>
 11 *
 12 * This program is free software; you can redistribute it and/or
 13 * modify it under the terms of the GNU General Public License version
 14 * 2 as published by the Free Software Foundation.
 15 *
 16 * This program is distributed in the hope that it will be useful,
 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 19 * GNU General Public License for more details.
 20 *
 21 * You should have received a copy of the GNU General Public License
 22 * along with this program; if not, write to the Free Software
 23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 24 */
 25
 26#include <linux/kernel.h>
 27#include <linux/module.h>
 28#include <linux/uaccess.h>
 29#include <acpi/apei.h>
 30#include <linux/miscdevice.h>
 31
 32#include "apei-internal.h"
 33
 34#define ERST_DBG_PFX			"ERST DBG: "
 35
 36#define ERST_DBG_RECORD_LEN_MAX		0x4000
 37
 38static void *erst_dbg_buf;
 39static unsigned int erst_dbg_buf_len;
 40
 41/* Prevent erst_dbg_read/write from being invoked concurrently */
 42static DEFINE_MUTEX(erst_dbg_mutex);
 43
 44static int erst_dbg_open(struct inode *inode, struct file *file)
 45{
 46	int rc, *pos;
 47
 48	if (erst_disable)
 49		return -ENODEV;
 50
 51	pos = (int *)&file->private_data;
 52
 53	rc = erst_get_record_id_begin(pos);
 54	if (rc)
 55		return rc;
 56
 57	return nonseekable_open(inode, file);
 58}
 59
 60static int erst_dbg_release(struct inode *inode, struct file *file)
 61{
 62	erst_get_record_id_end();
 63
 64	return 0;
 65}
 66
 67static long erst_dbg_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
 68{
 69	int rc;
 70	u64 record_id;
 71	u32 record_count;
 72
 73	switch (cmd) {
 74	case APEI_ERST_CLEAR_RECORD:
 75		rc = copy_from_user(&record_id, (void __user *)arg,
 76				    sizeof(record_id));
 77		if (rc)
 78			return -EFAULT;
 79		return erst_clear(record_id);
 80	case APEI_ERST_GET_RECORD_COUNT:
 81		rc = erst_get_record_count();
 82		if (rc < 0)
 83			return rc;
 84		record_count = rc;
 85		rc = put_user(record_count, (u32 __user *)arg);
 86		if (rc)
 87			return rc;
 88		return 0;
 89	default:
 90		return -ENOTTY;
 91	}
 92}
 93
 94static ssize_t erst_dbg_read(struct file *filp, char __user *ubuf,
 95			     size_t usize, loff_t *off)
 96{
 97	int rc, *pos;
 98	ssize_t len = 0;
 99	u64 id;
100
101	if (*off)
102		return -EINVAL;
103
104	if (mutex_lock_interruptible(&erst_dbg_mutex) != 0)
105		return -EINTR;
106
107	pos = (int *)&filp->private_data;
108
109retry_next:
110	rc = erst_get_record_id_next(pos, &id);
111	if (rc)
112		goto out;
113	/* no more record */
114	if (id == APEI_ERST_INVALID_RECORD_ID)
 
 
 
 
 
 
 
 
115		goto out;
 
116retry:
117	rc = len = erst_read(id, erst_dbg_buf, erst_dbg_buf_len);
 
118	/* The record may be cleared by others, try read next record */
119	if (rc == -ENOENT)
120		goto retry_next;
121	if (rc < 0)
122		goto out;
123	if (len > ERST_DBG_RECORD_LEN_MAX) {
124		pr_warning(ERST_DBG_PFX
125			   "Record (ID: 0x%llx) length is too long: %zd\n",
126			   id, len);
127		rc = -EIO;
128		goto out;
129	}
130	if (len > erst_dbg_buf_len) {
131		void *p;
132		rc = -ENOMEM;
133		p = kmalloc(len, GFP_KERNEL);
134		if (!p)
135			goto out;
136		kfree(erst_dbg_buf);
137		erst_dbg_buf = p;
138		erst_dbg_buf_len = len;
139		goto retry;
140	}
141
142	rc = -EINVAL;
143	if (len > usize)
144		goto out;
145
146	rc = -EFAULT;
147	if (copy_to_user(ubuf, erst_dbg_buf, len))
148		goto out;
149	rc = 0;
150out:
151	mutex_unlock(&erst_dbg_mutex);
152	return rc ? rc : len;
153}
154
155static ssize_t erst_dbg_write(struct file *filp, const char __user *ubuf,
156			      size_t usize, loff_t *off)
157{
158	int rc;
159	struct cper_record_header *rcd;
160
161	if (!capable(CAP_SYS_ADMIN))
162		return -EPERM;
163
164	if (usize > ERST_DBG_RECORD_LEN_MAX) {
165		pr_err(ERST_DBG_PFX "Too long record to be written\n");
166		return -EINVAL;
167	}
168
169	if (mutex_lock_interruptible(&erst_dbg_mutex))
170		return -EINTR;
171	if (usize > erst_dbg_buf_len) {
172		void *p;
173		rc = -ENOMEM;
174		p = kmalloc(usize, GFP_KERNEL);
175		if (!p)
176			goto out;
177		kfree(erst_dbg_buf);
178		erst_dbg_buf = p;
179		erst_dbg_buf_len = usize;
180	}
181	rc = copy_from_user(erst_dbg_buf, ubuf, usize);
182	if (rc) {
183		rc = -EFAULT;
184		goto out;
185	}
186	rcd = erst_dbg_buf;
187	rc = -EINVAL;
188	if (rcd->record_length != usize)
189		goto out;
190
191	rc = erst_write(erst_dbg_buf);
192
193out:
194	mutex_unlock(&erst_dbg_mutex);
195	return rc < 0 ? rc : usize;
196}
197
198static const struct file_operations erst_dbg_ops = {
199	.owner		= THIS_MODULE,
200	.open		= erst_dbg_open,
201	.release	= erst_dbg_release,
202	.read		= erst_dbg_read,
203	.write		= erst_dbg_write,
204	.unlocked_ioctl	= erst_dbg_ioctl,
205	.llseek		= no_llseek,
206};
207
208static struct miscdevice erst_dbg_dev = {
209	.minor	= MISC_DYNAMIC_MINOR,
210	.name	= "erst_dbg",
211	.fops	= &erst_dbg_ops,
212};
213
214static __init int erst_dbg_init(void)
215{
216	if (erst_disable) {
217		pr_info(ERST_DBG_PFX "ERST support is disabled.\n");
218		return -ENODEV;
219	}
220	return misc_register(&erst_dbg_dev);
221}
222
223static __exit void erst_dbg_exit(void)
224{
225	misc_deregister(&erst_dbg_dev);
226	kfree(erst_dbg_buf);
227}
228
229module_init(erst_dbg_init);
230module_exit(erst_dbg_exit);
231
232MODULE_AUTHOR("Huang Ying");
233MODULE_DESCRIPTION("APEI Error Record Serialization Table debug support");
234MODULE_LICENSE("GPL");
v6.9.4
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * APEI Error Record Serialization Table debug support
  4 *
  5 * ERST is a way provided by APEI to save and retrieve hardware error
  6 * information to and from a persistent store. This file provide the
  7 * debugging/testing support for ERST kernel support and firmware
  8 * implementation.
  9 *
 10 * Copyright 2010 Intel Corp.
 11 *   Author: Huang Ying <ying.huang@intel.com>
 
 
 
 
 
 
 
 
 
 
 
 
 
 12 */
 13
 14#include <linux/kernel.h>
 15#include <linux/module.h>
 16#include <linux/uaccess.h>
 17#include <acpi/apei.h>
 18#include <linux/miscdevice.h>
 19
 20#include "apei-internal.h"
 21
 22#define ERST_DBG_PFX			"ERST DBG: "
 23
 24#define ERST_DBG_RECORD_LEN_MAX		0x4000
 25
 26static void *erst_dbg_buf;
 27static unsigned int erst_dbg_buf_len;
 28
 29/* Prevent erst_dbg_read/write from being invoked concurrently */
 30static DEFINE_MUTEX(erst_dbg_mutex);
 31
 32static int erst_dbg_open(struct inode *inode, struct file *file)
 33{
 34	int rc, *pos;
 35
 36	if (erst_disable)
 37		return -ENODEV;
 38
 39	pos = (int *)&file->private_data;
 40
 41	rc = erst_get_record_id_begin(pos);
 42	if (rc)
 43		return rc;
 44
 45	return nonseekable_open(inode, file);
 46}
 47
 48static int erst_dbg_release(struct inode *inode, struct file *file)
 49{
 50	erst_get_record_id_end();
 51
 52	return 0;
 53}
 54
 55static long erst_dbg_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
 56{
 57	int rc;
 58	u64 record_id;
 59	u32 record_count;
 60
 61	switch (cmd) {
 62	case APEI_ERST_CLEAR_RECORD:
 63		rc = copy_from_user(&record_id, (void __user *)arg,
 64				    sizeof(record_id));
 65		if (rc)
 66			return -EFAULT;
 67		return erst_clear(record_id);
 68	case APEI_ERST_GET_RECORD_COUNT:
 69		rc = erst_get_record_count();
 70		if (rc < 0)
 71			return rc;
 72		record_count = rc;
 73		rc = put_user(record_count, (u32 __user *)arg);
 74		if (rc)
 75			return rc;
 76		return 0;
 77	default:
 78		return -ENOTTY;
 79	}
 80}
 81
 82static ssize_t erst_dbg_read(struct file *filp, char __user *ubuf,
 83			     size_t usize, loff_t *off)
 84{
 85	int rc, *pos;
 86	ssize_t len = 0;
 87	u64 id;
 88
 89	if (*off)
 90		return -EINVAL;
 91
 92	if (mutex_lock_interruptible(&erst_dbg_mutex) != 0)
 93		return -EINTR;
 94
 95	pos = (int *)&filp->private_data;
 96
 97retry_next:
 98	rc = erst_get_record_id_next(pos, &id);
 99	if (rc)
100		goto out;
101	/* no more record */
102	if (id == APEI_ERST_INVALID_RECORD_ID) {
103		/*
104		 * If the persistent store is empty initially, the function
105		 * 'erst_read' below will return "-ENOENT" value. This causes
106		 * 'retry_next' label is entered again. The returned value
107		 * should be zero indicating the read operation is EOF.
108		 */
109		len = 0;
110
111		goto out;
112	}
113retry:
114	rc = len = erst_read_record(id, erst_dbg_buf, erst_dbg_buf_len,
115			erst_dbg_buf_len, NULL);
116	/* The record may be cleared by others, try read next record */
117	if (rc == -ENOENT)
118		goto retry_next;
119	if (rc < 0)
120		goto out;
121	if (len > ERST_DBG_RECORD_LEN_MAX) {
122		pr_warn(ERST_DBG_PFX
123			"Record (ID: 0x%llx) length is too long: %zd\n", id, len);
 
124		rc = -EIO;
125		goto out;
126	}
127	if (len > erst_dbg_buf_len) {
128		void *p;
129		rc = -ENOMEM;
130		p = kmalloc(len, GFP_KERNEL);
131		if (!p)
132			goto out;
133		kfree(erst_dbg_buf);
134		erst_dbg_buf = p;
135		erst_dbg_buf_len = len;
136		goto retry;
137	}
138
139	rc = -EINVAL;
140	if (len > usize)
141		goto out;
142
143	rc = -EFAULT;
144	if (copy_to_user(ubuf, erst_dbg_buf, len))
145		goto out;
146	rc = 0;
147out:
148	mutex_unlock(&erst_dbg_mutex);
149	return rc ? rc : len;
150}
151
152static ssize_t erst_dbg_write(struct file *filp, const char __user *ubuf,
153			      size_t usize, loff_t *off)
154{
155	int rc;
156	struct cper_record_header *rcd;
157
158	if (!capable(CAP_SYS_ADMIN))
159		return -EPERM;
160
161	if (usize > ERST_DBG_RECORD_LEN_MAX) {
162		pr_err(ERST_DBG_PFX "Too long record to be written\n");
163		return -EINVAL;
164	}
165
166	if (mutex_lock_interruptible(&erst_dbg_mutex))
167		return -EINTR;
168	if (usize > erst_dbg_buf_len) {
169		void *p;
170		rc = -ENOMEM;
171		p = kmalloc(usize, GFP_KERNEL);
172		if (!p)
173			goto out;
174		kfree(erst_dbg_buf);
175		erst_dbg_buf = p;
176		erst_dbg_buf_len = usize;
177	}
178	rc = copy_from_user(erst_dbg_buf, ubuf, usize);
179	if (rc) {
180		rc = -EFAULT;
181		goto out;
182	}
183	rcd = erst_dbg_buf;
184	rc = -EINVAL;
185	if (rcd->record_length != usize)
186		goto out;
187
188	rc = erst_write(erst_dbg_buf);
189
190out:
191	mutex_unlock(&erst_dbg_mutex);
192	return rc < 0 ? rc : usize;
193}
194
195static const struct file_operations erst_dbg_ops = {
196	.owner		= THIS_MODULE,
197	.open		= erst_dbg_open,
198	.release	= erst_dbg_release,
199	.read		= erst_dbg_read,
200	.write		= erst_dbg_write,
201	.unlocked_ioctl	= erst_dbg_ioctl,
202	.llseek		= no_llseek,
203};
204
205static struct miscdevice erst_dbg_dev = {
206	.minor	= MISC_DYNAMIC_MINOR,
207	.name	= "erst_dbg",
208	.fops	= &erst_dbg_ops,
209};
210
211static __init int erst_dbg_init(void)
212{
213	if (erst_disable) {
214		pr_info(ERST_DBG_PFX "ERST support is disabled.\n");
215		return -ENODEV;
216	}
217	return misc_register(&erst_dbg_dev);
218}
219
220static __exit void erst_dbg_exit(void)
221{
222	misc_deregister(&erst_dbg_dev);
223	kfree(erst_dbg_buf);
224}
225
226module_init(erst_dbg_init);
227module_exit(erst_dbg_exit);
228
229MODULE_AUTHOR("Huang Ying");
230MODULE_DESCRIPTION("APEI Error Record Serialization Table debug support");
231MODULE_LICENSE("GPL");