blob: f46e5df76b583adf83d123cd39945a5c933e7d2f [file] [log] [blame]
Tony Luckca01d6d2010-12-28 14:25:21 -08001/*
2 * Persistent Storage - pstore.h
3 *
4 * Copyright (C) 2010 Intel Corporation <tony.luck@intel.com>
5 *
6 * This code is the generic layer to export data records from platform
7 * level persistent storage via a file system.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22#ifndef _LINUX_PSTORE_H
23#define _LINUX_PSTORE_H
24
Mark Salyzyn5bf6d1b2016-09-01 08:13:46 -070025#include <linux/compiler.h>
26#include <linux/errno.h>
Kees Cook3d6d8d22011-11-17 13:13:29 -080027#include <linux/kmsg_dump.h>
Anton Vorontsov67a101f2012-07-17 11:37:07 -070028#include <linux/mutex.h>
Anton Vorontsov67a101f2012-07-17 11:37:07 -070029#include <linux/spinlock.h>
Mark Salyzyn5bf6d1b2016-09-01 08:13:46 -070030#include <linux/time.h>
31#include <linux/types.h>
Kees Cook3d6d8d22011-11-17 13:13:29 -080032
Kees Cook9abdccc2017-03-03 16:59:29 -080033struct module;
34
Kees Cook0edae0b2017-03-03 12:16:16 -080035/* pstore record types (see fs/pstore/inode.c for filename templates) */
Tony Luckca01d6d2010-12-28 14:25:21 -080036enum pstore_type_id {
37 PSTORE_TYPE_DMESG = 0,
38 PSTORE_TYPE_MCE = 1,
Anton Vorontsovf29e5952012-05-26 06:20:19 -070039 PSTORE_TYPE_CONSOLE = 2,
Anton Vorontsov060287b2012-07-09 17:10:41 -070040 PSTORE_TYPE_FTRACE = 3,
Aruna Balakrishnaiah69020ee2013-06-06 00:21:44 +053041 /* PPC64 partition types */
42 PSTORE_TYPE_PPC_RTAS = 4,
Aruna Balakrishnaiahf33f7482013-06-06 00:22:10 +053043 PSTORE_TYPE_PPC_OF = 5,
Aruna Balakrishnaiaha5e47972013-06-06 00:22:20 +053044 PSTORE_TYPE_PPC_COMMON = 6,
Mark Salyzyn9d5438f2015-01-16 16:01:10 -080045 PSTORE_TYPE_PMSG = 7,
Hari Bathiniae011d22015-02-06 01:06:28 +053046 PSTORE_TYPE_PPC_OPAL = 8,
Tony Luckca01d6d2010-12-28 14:25:21 -080047 PSTORE_TYPE_UNKNOWN = 255
48};
49
Kees Cook9abdccc2017-03-03 16:59:29 -080050struct pstore_info;
51/**
52 * struct pstore_record - details of a pstore record entry
53 * @psi: pstore backend driver information
54 * @type: pstore record type
55 * @id: per-type unique identifier for record
56 * @time: timestamp of the record
Kees Cook9abdccc2017-03-03 16:59:29 -080057 * @buf: pointer to record contents
58 * @size: size of @buf
59 * @ecc_notice_size:
60 * ECC information for @buf
Kees Cook76cc9582017-03-03 23:28:53 -080061 *
62 * Valid for PSTORE_TYPE_DMESG @type:
63 *
64 * @count: Oops count since boot
65 * @reason: kdump reason for notification
66 * @part: position in a multipart record
67 * @compressed: whether the buffer is compressed
68 *
Kees Cook9abdccc2017-03-03 16:59:29 -080069 */
70struct pstore_record {
71 struct pstore_info *psi;
72 enum pstore_type_id type;
73 u64 id;
Kees Cook7aaa8222018-05-14 15:50:52 -070074 struct timespec64 time;
Kees Cook9abdccc2017-03-03 16:59:29 -080075 char *buf;
76 ssize_t size;
77 ssize_t ecc_notice_size;
Kees Cook76cc9582017-03-03 23:28:53 -080078
79 int count;
80 enum kmsg_dump_reason reason;
81 unsigned int part;
82 bool compressed;
Kees Cook9abdccc2017-03-03 16:59:29 -080083};
Anton Vorontsov67a101f2012-07-17 11:37:07 -070084
Kees Cook0edae0b2017-03-03 12:16:16 -080085/**
86 * struct pstore_info - backend pstore driver structure
87 *
Kees Cook0eed84f2018-11-01 14:03:07 -070088 * @owner: module which is responsible for this backend driver
Kees Cook0edae0b2017-03-03 12:16:16 -080089 * @name: name of the backend driver
90 *
91 * @buf_lock: spinlock to serialize access to @buf
92 * @buf: preallocated crash dump buffer
Kees Cook89d328f2018-11-01 16:17:22 -070093 * @bufsize: size of @buf available for crash dump bytes (must match
94 * smallest number of bytes available for writing to a
95 * backend entry, since compressed bytes don't take kindly
96 * to being truncated)
Kees Cook0edae0b2017-03-03 12:16:16 -080097 *
98 * @read_mutex: serializes @open, @read, @close, and @erase callbacks
99 * @flags: bitfield of frontends the backend can accept writes for
100 * @data: backend-private pointer passed back during callbacks
101 *
102 * Callbacks:
103 *
104 * @open:
105 * Notify backend that pstore is starting a full read of backend
106 * records. Followed by one or more @read calls, and a final @close.
107 *
108 * @psi: in: pointer to the struct pstore_info for the backend
109 *
110 * Returns 0 on success, and non-zero on error.
111 *
112 * @close:
113 * Notify backend that pstore has finished a full read of backend
114 * records. Always preceded by an @open call and one or more @read
115 * calls.
116 *
117 * @psi: in: pointer to the struct pstore_info for the backend
118 *
119 * Returns 0 on success, and non-zero on error. (Though pstore will
120 * ignore the error.)
121 *
122 * @read:
123 * Read next available backend record. Called after a successful
124 * @open.
125 *
Kees Cook125cc422017-03-03 22:09:18 -0800126 * @record:
127 * pointer to record to populate. @buf should be allocated
128 * by the backend and filled. At least @type and @id should
129 * be populated, since these are used when creating pstorefs
130 * file names.
Kees Cook0edae0b2017-03-03 12:16:16 -0800131 *
132 * Returns record size on success, zero when no more records are
133 * available, or negative on error.
134 *
135 * @write:
Kees Cook4c9ec212017-03-05 22:41:10 -0800136 * A newly generated record needs to be written to backend storage.
Kees Cook0edae0b2017-03-03 12:16:16 -0800137 *
Kees Cook76cc9582017-03-03 23:28:53 -0800138 * @record:
Kees Cook4c9ec212017-03-05 22:41:10 -0800139 * pointer to record metadata. When @type is PSTORE_TYPE_DMESG,
140 * @buf will be pointing to the preallocated @psi.buf, since
141 * memory allocation may be broken during an Oops. Regardless,
142 * @buf must be proccesed or copied before returning. The
143 * backend is also expected to write @id with something that
Kees Cookc7f3c5952017-05-19 15:29:10 -0700144 * can help identify this record to a future @erase callback.
145 * The @time field will be prepopulated with the current time,
146 * when available. The @size field will have the size of data
147 * in @buf.
Kees Cook0edae0b2017-03-03 12:16:16 -0800148 *
149 * Returns 0 on success, and non-zero on error.
150 *
Kees Cook4c9ec212017-03-05 22:41:10 -0800151 * @write_user:
Kees Cook0edae0b2017-03-03 12:16:16 -0800152 * Perform a frontend write to a backend record, using a specified
Kees Cookfdd03112017-03-05 00:56:38 -0800153 * buffer that is coming directly from userspace, instead of the
154 * @record @buf.
Kees Cook0edae0b2017-03-03 12:16:16 -0800155 *
Kees Cookfdd03112017-03-05 00:56:38 -0800156 * @record: pointer to record metadata.
157 * @buf: pointer to userspace contents to write to backend
Kees Cook0edae0b2017-03-03 12:16:16 -0800158 *
159 * Returns 0 on success, and non-zero on error.
160 *
161 * @erase:
162 * Delete a record from backend storage. Different backends
Kees Cooka61072a2017-03-04 23:31:19 -0800163 * identify records differently, so entire original record is
164 * passed back to assist in identification of what the backend
165 * should remove from storage.
Kees Cook0edae0b2017-03-03 12:16:16 -0800166 *
Kees Cooka61072a2017-03-04 23:31:19 -0800167 * @record: pointer to record metadata.
Kees Cook0edae0b2017-03-03 12:16:16 -0800168 *
169 * Returns 0 on success, and non-zero on error.
170 *
171 */
Tony Luckca01d6d2010-12-28 14:25:21 -0800172struct pstore_info {
173 struct module *owner;
174 char *name;
Kees Cook0edae0b2017-03-03 12:16:16 -0800175
176 spinlock_t buf_lock;
Tony Luckca01d6d2010-12-28 14:25:21 -0800177 char *buf;
178 size_t bufsize;
Kees Cook0edae0b2017-03-03 12:16:16 -0800179
180 struct mutex read_mutex;
181
Luck, Tonydf36ac12013-12-18 15:17:10 -0800182 int flags;
Kees Cook0edae0b2017-03-03 12:16:16 -0800183 void *data;
184
Chen Gong06cf91b2011-05-16 11:00:27 -0700185 int (*open)(struct pstore_info *psi);
186 int (*close)(struct pstore_info *psi);
Kees Cook125cc422017-03-03 22:09:18 -0800187 ssize_t (*read)(struct pstore_record *record);
Kees Cook76cc9582017-03-03 23:28:53 -0800188 int (*write)(struct pstore_record *record);
Kees Cook4c9ec212017-03-05 22:41:10 -0800189 int (*write_user)(struct pstore_record *record,
190 const char __user *buf);
Kees Cooka61072a2017-03-04 23:31:19 -0800191 int (*erase)(struct pstore_record *record);
Tony Luckca01d6d2010-12-28 14:25:21 -0800192};
193
Kees Cook0edae0b2017-03-03 12:16:16 -0800194/* Supported frontends */
Kees Cook4af62a62018-11-01 15:30:05 -0700195#define PSTORE_FLAGS_DMESG BIT(0)
196#define PSTORE_FLAGS_CONSOLE BIT(1)
197#define PSTORE_FLAGS_FTRACE BIT(2)
198#define PSTORE_FLAGS_PMSG BIT(3)
Namhyung Kimc950fd62016-07-28 00:08:25 +0900199
Tony Luckca01d6d2010-12-28 14:25:21 -0800200extern int pstore_register(struct pstore_info *);
Geliang Tangee1d2672015-10-20 00:39:03 -0700201extern void pstore_unregister(struct pstore_info *);
Seiji Aguchi9f244e92013-01-11 18:09:41 +0000202extern bool pstore_cannot_block_path(enum kmsg_dump_reason reason);
Tony Luckca01d6d2010-12-28 14:25:21 -0800203
Joel Fernandesfbccdeb2016-10-20 00:34:05 -0700204struct pstore_ftrace_record {
205 unsigned long ip;
206 unsigned long parent_ip;
207 u64 ts;
208};
209
210/*
211 * ftrace related stuff: Both backends and frontends need these so expose
212 * them here.
213 */
214
215#if NR_CPUS <= 2 && defined(CONFIG_ARM_THUMB)
216#define PSTORE_CPU_IN_IP 0x1
217#elif NR_CPUS <= 4 && defined(CONFIG_ARM)
218#define PSTORE_CPU_IN_IP 0x3
219#endif
220
221#define TS_CPU_SHIFT 8
222#define TS_CPU_MASK (BIT(TS_CPU_SHIFT) - 1)
223
224/*
225 * If CPU number can be stored in IP, store it there, otherwise store it in
226 * the time stamp. This means more timestamp resolution is available when
227 * the CPU can be stored in the IP.
228 */
229#ifdef PSTORE_CPU_IN_IP
230static inline void
231pstore_ftrace_encode_cpu(struct pstore_ftrace_record *rec, unsigned int cpu)
232{
233 rec->ip |= cpu;
234}
235
236static inline unsigned int
237pstore_ftrace_decode_cpu(struct pstore_ftrace_record *rec)
238{
239 return rec->ip & PSTORE_CPU_IN_IP;
240}
241
242static inline u64
243pstore_ftrace_read_timestamp(struct pstore_ftrace_record *rec)
244{
245 return rec->ts;
246}
247
248static inline void
249pstore_ftrace_write_timestamp(struct pstore_ftrace_record *rec, u64 val)
250{
251 rec->ts = val;
252}
253#else
254static inline void
255pstore_ftrace_encode_cpu(struct pstore_ftrace_record *rec, unsigned int cpu)
256{
257 rec->ts &= ~(TS_CPU_MASK);
258 rec->ts |= cpu;
259}
260
261static inline unsigned int
262pstore_ftrace_decode_cpu(struct pstore_ftrace_record *rec)
263{
264 return rec->ts & TS_CPU_MASK;
265}
266
267static inline u64
268pstore_ftrace_read_timestamp(struct pstore_ftrace_record *rec)
269{
270 return rec->ts >> TS_CPU_SHIFT;
271}
272
273static inline void
274pstore_ftrace_write_timestamp(struct pstore_ftrace_record *rec, u64 val)
275{
276 rec->ts = (rec->ts & TS_CPU_MASK) | (val << TS_CPU_SHIFT);
277}
278#endif
279
Tony Luckca01d6d2010-12-28 14:25:21 -0800280#endif /*_LINUX_PSTORE_H*/