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