Thomas Gleixner | 4505153 | 2019-05-29 16:57:47 -0700 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 2 | /* |
| 3 | * Persistent Storage - pstore.h |
| 4 | * |
| 5 | * Copyright (C) 2010 Intel Corporation <tony.luck@intel.com> |
| 6 | * |
| 7 | * This code is the generic layer to export data records from platform |
| 8 | * level persistent storage via a file system. |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 9 | */ |
| 10 | #ifndef _LINUX_PSTORE_H |
| 11 | #define _LINUX_PSTORE_H |
| 12 | |
Mark Salyzyn | 5bf6d1b | 2016-09-01 08:13:46 -0700 | [diff] [blame] | 13 | #include <linux/compiler.h> |
| 14 | #include <linux/errno.h> |
Kees Cook | 3d6d8d2 | 2011-11-17 13:13:29 -0800 | [diff] [blame] | 15 | #include <linux/kmsg_dump.h> |
Anton Vorontsov | 67a101f | 2012-07-17 11:37:07 -0700 | [diff] [blame] | 16 | #include <linux/mutex.h> |
Kees Cook | ea84b58 | 2018-11-30 14:36:58 -0800 | [diff] [blame] | 17 | #include <linux/semaphore.h> |
Mark Salyzyn | 5bf6d1b | 2016-09-01 08:13:46 -0700 | [diff] [blame] | 18 | #include <linux/time.h> |
| 19 | #include <linux/types.h> |
Kees Cook | 3d6d8d2 | 2011-11-17 13:13:29 -0800 | [diff] [blame] | 20 | |
Kees Cook | 9abdccc | 2017-03-03 16:59:29 -0800 | [diff] [blame] | 21 | struct module; |
| 22 | |
Joel Fernandes (Google) | f0f23e5 | 2018-11-03 16:38:16 -0700 | [diff] [blame] | 23 | /* |
| 24 | * pstore record types (see fs/pstore/platform.c for pstore_type_names[]) |
| 25 | * These values may be written to storage (see EFI vars backend), so |
| 26 | * they are kind of an ABI. Be careful changing the mappings. |
| 27 | */ |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 28 | enum pstore_type_id { |
Joel Fernandes (Google) | f0f23e5 | 2018-11-03 16:38:16 -0700 | [diff] [blame] | 29 | /* Frontend storage types */ |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 30 | PSTORE_TYPE_DMESG = 0, |
| 31 | PSTORE_TYPE_MCE = 1, |
Anton Vorontsov | f29e595 | 2012-05-26 06:20:19 -0700 | [diff] [blame] | 32 | PSTORE_TYPE_CONSOLE = 2, |
Anton Vorontsov | 060287b | 2012-07-09 17:10:41 -0700 | [diff] [blame] | 33 | PSTORE_TYPE_FTRACE = 3, |
Joel Fernandes (Google) | f0f23e5 | 2018-11-03 16:38:16 -0700 | [diff] [blame] | 34 | |
| 35 | /* PPC64-specific partition types */ |
Aruna Balakrishnaiah | 69020ee | 2013-06-06 00:21:44 +0530 | [diff] [blame] | 36 | PSTORE_TYPE_PPC_RTAS = 4, |
Aruna Balakrishnaiah | f33f748 | 2013-06-06 00:22:10 +0530 | [diff] [blame] | 37 | PSTORE_TYPE_PPC_OF = 5, |
Aruna Balakrishnaiah | a5e4797 | 2013-06-06 00:22:20 +0530 | [diff] [blame] | 38 | PSTORE_TYPE_PPC_COMMON = 6, |
Mark Salyzyn | 9d5438f | 2015-01-16 16:01:10 -0800 | [diff] [blame] | 39 | PSTORE_TYPE_PMSG = 7, |
Hari Bathini | ae011d2 | 2015-02-06 01:06:28 +0530 | [diff] [blame] | 40 | PSTORE_TYPE_PPC_OPAL = 8, |
Joel Fernandes (Google) | f0f23e5 | 2018-11-03 16:38:16 -0700 | [diff] [blame] | 41 | |
| 42 | /* End of the list */ |
| 43 | PSTORE_TYPE_MAX |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 44 | }; |
| 45 | |
Joel Fernandes (Google) | f0f23e5 | 2018-11-03 16:38:16 -0700 | [diff] [blame] | 46 | const char *pstore_type_to_name(enum pstore_type_id type); |
| 47 | enum pstore_type_id pstore_name_to_type(const char *name); |
| 48 | |
Kees Cook | 9abdccc | 2017-03-03 16:59:29 -0800 | [diff] [blame] | 49 | struct pstore_info; |
| 50 | /** |
| 51 | * struct pstore_record - details of a pstore record entry |
| 52 | * @psi: pstore backend driver information |
| 53 | * @type: pstore record type |
| 54 | * @id: per-type unique identifier for record |
| 55 | * @time: timestamp of the record |
Kees Cook | 9abdccc | 2017-03-03 16:59:29 -0800 | [diff] [blame] | 56 | * @buf: pointer to record contents |
| 57 | * @size: size of @buf |
| 58 | * @ecc_notice_size: |
| 59 | * ECC information for @buf |
Kees Cook | 76cc958 | 2017-03-03 23:28:53 -0800 | [diff] [blame] | 60 | * |
| 61 | * Valid for PSTORE_TYPE_DMESG @type: |
| 62 | * |
| 63 | * @count: Oops count since boot |
| 64 | * @reason: kdump reason for notification |
| 65 | * @part: position in a multipart record |
| 66 | * @compressed: whether the buffer is compressed |
| 67 | * |
Kees Cook | 9abdccc | 2017-03-03 16:59:29 -0800 | [diff] [blame] | 68 | */ |
| 69 | struct pstore_record { |
| 70 | struct pstore_info *psi; |
| 71 | enum pstore_type_id type; |
| 72 | u64 id; |
Kees Cook | 7aaa822 | 2018-05-14 15:50:52 -0700 | [diff] [blame] | 73 | struct timespec64 time; |
Kees Cook | 9abdccc | 2017-03-03 16:59:29 -0800 | [diff] [blame] | 74 | char *buf; |
| 75 | ssize_t size; |
| 76 | ssize_t ecc_notice_size; |
Kees Cook | 76cc958 | 2017-03-03 23:28:53 -0800 | [diff] [blame] | 77 | |
| 78 | int count; |
| 79 | enum kmsg_dump_reason reason; |
| 80 | unsigned int part; |
| 81 | bool compressed; |
Kees Cook | 9abdccc | 2017-03-03 16:59:29 -0800 | [diff] [blame] | 82 | }; |
Anton Vorontsov | 67a101f | 2012-07-17 11:37:07 -0700 | [diff] [blame] | 83 | |
Kees Cook | 0edae0b | 2017-03-03 12:16:16 -0800 | [diff] [blame] | 84 | /** |
| 85 | * struct pstore_info - backend pstore driver structure |
| 86 | * |
Kees Cook | 0eed84f | 2018-11-01 14:03:07 -0700 | [diff] [blame] | 87 | * @owner: module which is responsible for this backend driver |
Kees Cook | 0edae0b | 2017-03-03 12:16:16 -0800 | [diff] [blame] | 88 | * @name: name of the backend driver |
| 89 | * |
Kees Cook | ea84b58 | 2018-11-30 14:36:58 -0800 | [diff] [blame] | 90 | * @buf_lock: semaphore to serialize access to @buf |
Kees Cook | 0edae0b | 2017-03-03 12:16:16 -0800 | [diff] [blame] | 91 | * @buf: preallocated crash dump buffer |
Kees Cook | 89d328f | 2018-11-01 16:17:22 -0700 | [diff] [blame] | 92 | * @bufsize: size of @buf available for crash dump bytes (must match |
| 93 | * smallest number of bytes available for writing to a |
| 94 | * backend entry, since compressed bytes don't take kindly |
| 95 | * to being truncated) |
Kees Cook | 0edae0b | 2017-03-03 12:16:16 -0800 | [diff] [blame] | 96 | * |
| 97 | * @read_mutex: serializes @open, @read, @close, and @erase callbacks |
| 98 | * @flags: bitfield of frontends the backend can accept writes for |
| 99 | * @data: backend-private pointer passed back during callbacks |
| 100 | * |
| 101 | * Callbacks: |
| 102 | * |
| 103 | * @open: |
| 104 | * Notify backend that pstore is starting a full read of backend |
| 105 | * records. Followed by one or more @read calls, and a final @close. |
| 106 | * |
| 107 | * @psi: in: pointer to the struct pstore_info for the backend |
| 108 | * |
| 109 | * Returns 0 on success, and non-zero on error. |
| 110 | * |
| 111 | * @close: |
| 112 | * Notify backend that pstore has finished a full read of backend |
| 113 | * records. Always preceded by an @open call and one or more @read |
| 114 | * calls. |
| 115 | * |
| 116 | * @psi: in: pointer to the struct pstore_info for the backend |
| 117 | * |
| 118 | * Returns 0 on success, and non-zero on error. (Though pstore will |
| 119 | * ignore the error.) |
| 120 | * |
| 121 | * @read: |
| 122 | * Read next available backend record. Called after a successful |
| 123 | * @open. |
| 124 | * |
Kees Cook | 125cc42 | 2017-03-03 22:09:18 -0800 | [diff] [blame] | 125 | * @record: |
| 126 | * pointer to record to populate. @buf should be allocated |
| 127 | * by the backend and filled. At least @type and @id should |
| 128 | * be populated, since these are used when creating pstorefs |
| 129 | * file names. |
Kees Cook | 0edae0b | 2017-03-03 12:16:16 -0800 | [diff] [blame] | 130 | * |
| 131 | * Returns record size on success, zero when no more records are |
| 132 | * available, or negative on error. |
| 133 | * |
| 134 | * @write: |
Kees Cook | 4c9ec21 | 2017-03-05 22:41:10 -0800 | [diff] [blame] | 135 | * A newly generated record needs to be written to backend storage. |
Kees Cook | 0edae0b | 2017-03-03 12:16:16 -0800 | [diff] [blame] | 136 | * |
Kees Cook | 76cc958 | 2017-03-03 23:28:53 -0800 | [diff] [blame] | 137 | * @record: |
Kees Cook | 4c9ec21 | 2017-03-05 22:41:10 -0800 | [diff] [blame] | 138 | * pointer to record metadata. When @type is PSTORE_TYPE_DMESG, |
| 139 | * @buf will be pointing to the preallocated @psi.buf, since |
| 140 | * memory allocation may be broken during an Oops. Regardless, |
| 141 | * @buf must be proccesed or copied before returning. The |
| 142 | * backend is also expected to write @id with something that |
Kees Cook | c7f3c595 | 2017-05-19 15:29:10 -0700 | [diff] [blame] | 143 | * can help identify this record to a future @erase callback. |
| 144 | * The @time field will be prepopulated with the current time, |
| 145 | * when available. The @size field will have the size of data |
| 146 | * in @buf. |
Kees Cook | 0edae0b | 2017-03-03 12:16:16 -0800 | [diff] [blame] | 147 | * |
| 148 | * Returns 0 on success, and non-zero on error. |
| 149 | * |
Kees Cook | 4c9ec21 | 2017-03-05 22:41:10 -0800 | [diff] [blame] | 150 | * @write_user: |
Kees Cook | 0edae0b | 2017-03-03 12:16:16 -0800 | [diff] [blame] | 151 | * Perform a frontend write to a backend record, using a specified |
Kees Cook | fdd0311 | 2017-03-05 00:56:38 -0800 | [diff] [blame] | 152 | * buffer that is coming directly from userspace, instead of the |
| 153 | * @record @buf. |
Kees Cook | 0edae0b | 2017-03-03 12:16:16 -0800 | [diff] [blame] | 154 | * |
Kees Cook | fdd0311 | 2017-03-05 00:56:38 -0800 | [diff] [blame] | 155 | * @record: pointer to record metadata. |
| 156 | * @buf: pointer to userspace contents to write to backend |
Kees Cook | 0edae0b | 2017-03-03 12:16:16 -0800 | [diff] [blame] | 157 | * |
| 158 | * Returns 0 on success, and non-zero on error. |
| 159 | * |
| 160 | * @erase: |
| 161 | * Delete a record from backend storage. Different backends |
Kees Cook | a61072a | 2017-03-04 23:31:19 -0800 | [diff] [blame] | 162 | * identify records differently, so entire original record is |
| 163 | * passed back to assist in identification of what the backend |
| 164 | * should remove from storage. |
Kees Cook | 0edae0b | 2017-03-03 12:16:16 -0800 | [diff] [blame] | 165 | * |
Kees Cook | a61072a | 2017-03-04 23:31:19 -0800 | [diff] [blame] | 166 | * @record: pointer to record metadata. |
Kees Cook | 0edae0b | 2017-03-03 12:16:16 -0800 | [diff] [blame] | 167 | * |
| 168 | * Returns 0 on success, and non-zero on error. |
| 169 | * |
| 170 | */ |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 171 | struct pstore_info { |
| 172 | struct module *owner; |
| 173 | char *name; |
Kees Cook | 0edae0b | 2017-03-03 12:16:16 -0800 | [diff] [blame] | 174 | |
Kees Cook | ea84b58 | 2018-11-30 14:36:58 -0800 | [diff] [blame] | 175 | struct semaphore buf_lock; |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 176 | char *buf; |
| 177 | size_t bufsize; |
Kees Cook | 0edae0b | 2017-03-03 12:16:16 -0800 | [diff] [blame] | 178 | |
| 179 | struct mutex read_mutex; |
| 180 | |
Luck, Tony | df36ac1 | 2013-12-18 15:17:10 -0800 | [diff] [blame] | 181 | int flags; |
Kees Cook | 0edae0b | 2017-03-03 12:16:16 -0800 | [diff] [blame] | 182 | void *data; |
| 183 | |
Chen Gong | 06cf91b | 2011-05-16 11:00:27 -0700 | [diff] [blame] | 184 | int (*open)(struct pstore_info *psi); |
| 185 | int (*close)(struct pstore_info *psi); |
Kees Cook | 125cc42 | 2017-03-03 22:09:18 -0800 | [diff] [blame] | 186 | ssize_t (*read)(struct pstore_record *record); |
Kees Cook | 76cc958 | 2017-03-03 23:28:53 -0800 | [diff] [blame] | 187 | int (*write)(struct pstore_record *record); |
Kees Cook | 4c9ec21 | 2017-03-05 22:41:10 -0800 | [diff] [blame] | 188 | int (*write_user)(struct pstore_record *record, |
| 189 | const char __user *buf); |
Kees Cook | a61072a | 2017-03-04 23:31:19 -0800 | [diff] [blame] | 190 | int (*erase)(struct pstore_record *record); |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 191 | }; |
| 192 | |
Kees Cook | 0edae0b | 2017-03-03 12:16:16 -0800 | [diff] [blame] | 193 | /* Supported frontends */ |
Kees Cook | 4af62a6 | 2018-11-01 15:30:05 -0700 | [diff] [blame] | 194 | #define PSTORE_FLAGS_DMESG BIT(0) |
| 195 | #define PSTORE_FLAGS_CONSOLE BIT(1) |
| 196 | #define PSTORE_FLAGS_FTRACE BIT(2) |
| 197 | #define PSTORE_FLAGS_PMSG BIT(3) |
Namhyung Kim | c950fd6 | 2016-07-28 00:08:25 +0900 | [diff] [blame] | 198 | |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 199 | extern int pstore_register(struct pstore_info *); |
Geliang Tang | ee1d267 | 2015-10-20 00:39:03 -0700 | [diff] [blame] | 200 | extern void pstore_unregister(struct pstore_info *); |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 201 | |
Joel Fernandes | fbccdeb | 2016-10-20 00:34:05 -0700 | [diff] [blame] | 202 | struct pstore_ftrace_record { |
| 203 | unsigned long ip; |
| 204 | unsigned long parent_ip; |
| 205 | u64 ts; |
| 206 | }; |
| 207 | |
| 208 | /* |
| 209 | * ftrace related stuff: Both backends and frontends need these so expose |
| 210 | * them here. |
| 211 | */ |
| 212 | |
| 213 | #if NR_CPUS <= 2 && defined(CONFIG_ARM_THUMB) |
| 214 | #define PSTORE_CPU_IN_IP 0x1 |
| 215 | #elif NR_CPUS <= 4 && defined(CONFIG_ARM) |
| 216 | #define PSTORE_CPU_IN_IP 0x3 |
| 217 | #endif |
| 218 | |
| 219 | #define TS_CPU_SHIFT 8 |
| 220 | #define TS_CPU_MASK (BIT(TS_CPU_SHIFT) - 1) |
| 221 | |
| 222 | /* |
| 223 | * If CPU number can be stored in IP, store it there, otherwise store it in |
| 224 | * the time stamp. This means more timestamp resolution is available when |
| 225 | * the CPU can be stored in the IP. |
| 226 | */ |
| 227 | #ifdef PSTORE_CPU_IN_IP |
| 228 | static inline void |
| 229 | pstore_ftrace_encode_cpu(struct pstore_ftrace_record *rec, unsigned int cpu) |
| 230 | { |
| 231 | rec->ip |= cpu; |
| 232 | } |
| 233 | |
| 234 | static inline unsigned int |
| 235 | pstore_ftrace_decode_cpu(struct pstore_ftrace_record *rec) |
| 236 | { |
| 237 | return rec->ip & PSTORE_CPU_IN_IP; |
| 238 | } |
| 239 | |
| 240 | static inline u64 |
| 241 | pstore_ftrace_read_timestamp(struct pstore_ftrace_record *rec) |
| 242 | { |
| 243 | return rec->ts; |
| 244 | } |
| 245 | |
| 246 | static inline void |
| 247 | pstore_ftrace_write_timestamp(struct pstore_ftrace_record *rec, u64 val) |
| 248 | { |
| 249 | rec->ts = val; |
| 250 | } |
| 251 | #else |
| 252 | static inline void |
| 253 | pstore_ftrace_encode_cpu(struct pstore_ftrace_record *rec, unsigned int cpu) |
| 254 | { |
| 255 | rec->ts &= ~(TS_CPU_MASK); |
| 256 | rec->ts |= cpu; |
| 257 | } |
| 258 | |
| 259 | static inline unsigned int |
| 260 | pstore_ftrace_decode_cpu(struct pstore_ftrace_record *rec) |
| 261 | { |
| 262 | return rec->ts & TS_CPU_MASK; |
| 263 | } |
| 264 | |
| 265 | static inline u64 |
| 266 | pstore_ftrace_read_timestamp(struct pstore_ftrace_record *rec) |
| 267 | { |
| 268 | return rec->ts >> TS_CPU_SHIFT; |
| 269 | } |
| 270 | |
| 271 | static inline void |
| 272 | pstore_ftrace_write_timestamp(struct pstore_ftrace_record *rec, u64 val) |
| 273 | { |
| 274 | rec->ts = (rec->ts & TS_CPU_MASK) | (val << TS_CPU_SHIFT); |
| 275 | } |
| 276 | #endif |
| 277 | |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 278 | #endif /*_LINUX_PSTORE_H*/ |