1 // SPDX-License-Identifier: GPL-2.0-only
3 * APEI Error Record Serialization Table debug support
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
10 * Copyright 2010 Intel Corp.
11 * Author: Huang Ying <ying.huang@intel.com>
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>
20 #include "apei-internal.h"
22 #define ERST_DBG_PFX "ERST DBG: "
24 #define ERST_DBG_RECORD_LEN_MAX 0x4000
26 static void *erst_dbg_buf
;
27 static unsigned int erst_dbg_buf_len
;
29 /* Prevent erst_dbg_read/write from being invoked concurrently */
30 static DEFINE_MUTEX(erst_dbg_mutex
);
32 static int erst_dbg_open(struct inode
*inode
, struct file
*file
)
39 pos
= (int *)&file
->private_data
;
41 rc
= erst_get_record_id_begin(pos
);
45 return nonseekable_open(inode
, file
);
48 static int erst_dbg_release(struct inode
*inode
, struct file
*file
)
50 erst_get_record_id_end();
55 static long erst_dbg_ioctl(struct file
*f
, unsigned int cmd
, unsigned long arg
)
62 case APEI_ERST_CLEAR_RECORD
:
63 rc
= copy_from_user(&record_id
, (void __user
*)arg
,
67 return erst_clear(record_id
);
68 case APEI_ERST_GET_RECORD_COUNT
:
69 rc
= erst_get_record_count();
73 rc
= put_user(record_count
, (u32 __user
*)arg
);
82 static ssize_t
erst_dbg_read(struct file
*filp
, char __user
*ubuf
,
83 size_t usize
, loff_t
*off
)
92 if (mutex_lock_interruptible(&erst_dbg_mutex
) != 0)
95 pos
= (int *)&filp
->private_data
;
98 rc
= erst_get_record_id_next(pos
, &id
);
102 if (id
== APEI_ERST_INVALID_RECORD_ID
) {
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.
114 rc
= len
= erst_read(id
, erst_dbg_buf
, erst_dbg_buf_len
);
115 /* The record may be cleared by others, try read next record */
120 if (len
> ERST_DBG_RECORD_LEN_MAX
) {
122 "Record (ID: 0x%llx) length is too long: %zd\n", id
, len
);
126 if (len
> erst_dbg_buf_len
) {
129 p
= kmalloc(len
, GFP_KERNEL
);
134 erst_dbg_buf_len
= len
;
143 if (copy_to_user(ubuf
, erst_dbg_buf
, len
))
147 mutex_unlock(&erst_dbg_mutex
);
148 return rc
? rc
: len
;
151 static ssize_t
erst_dbg_write(struct file
*filp
, const char __user
*ubuf
,
152 size_t usize
, loff_t
*off
)
155 struct cper_record_header
*rcd
;
157 if (!capable(CAP_SYS_ADMIN
))
160 if (usize
> ERST_DBG_RECORD_LEN_MAX
) {
161 pr_err(ERST_DBG_PFX
"Too long record to be written\n");
165 if (mutex_lock_interruptible(&erst_dbg_mutex
))
167 if (usize
> erst_dbg_buf_len
) {
170 p
= kmalloc(usize
, GFP_KERNEL
);
175 erst_dbg_buf_len
= usize
;
177 rc
= copy_from_user(erst_dbg_buf
, ubuf
, usize
);
184 if (rcd
->record_length
!= usize
)
187 rc
= erst_write(erst_dbg_buf
);
190 mutex_unlock(&erst_dbg_mutex
);
191 return rc
< 0 ? rc
: usize
;
194 static const struct file_operations erst_dbg_ops
= {
195 .owner
= THIS_MODULE
,
196 .open
= erst_dbg_open
,
197 .release
= erst_dbg_release
,
198 .read
= erst_dbg_read
,
199 .write
= erst_dbg_write
,
200 .unlocked_ioctl
= erst_dbg_ioctl
,
204 static struct miscdevice erst_dbg_dev
= {
205 .minor
= MISC_DYNAMIC_MINOR
,
207 .fops
= &erst_dbg_ops
,
210 static __init
int erst_dbg_init(void)
213 pr_info(ERST_DBG_PFX
"ERST support is disabled.\n");
216 return misc_register(&erst_dbg_dev
);
219 static __exit
void erst_dbg_exit(void)
221 misc_deregister(&erst_dbg_dev
);
225 module_init(erst_dbg_init
);
226 module_exit(erst_dbg_exit
);
228 MODULE_AUTHOR("Huang Ying");
229 MODULE_DESCRIPTION("APEI Error Record Serialization Table debug support");
230 MODULE_LICENSE("GPL");