Merge tag 'for-linus-20160523' of git://git.infradead.org/linux-mtd
[linux-2.6-block.git] / include / linux / kexec.h
CommitLineData
dc009d92
EB
1#ifndef LINUX_KEXEC_H
2#define LINUX_KEXEC_H
3
cf2df639
GL
4#define IND_DESTINATION_BIT 0
5#define IND_INDIRECTION_BIT 1
6#define IND_DONE_BIT 2
7#define IND_SOURCE_BIT 3
8
9#define IND_DESTINATION (1 << IND_DESTINATION_BIT)
10#define IND_INDIRECTION (1 << IND_INDIRECTION_BIT)
11#define IND_DONE (1 << IND_DONE_BIT)
12#define IND_SOURCE (1 << IND_SOURCE_BIT)
b28c2ee8 13#define IND_FLAGS (IND_DESTINATION | IND_INDIRECTION | IND_DONE | IND_SOURCE)
cf2df639
GL
14
15#if !defined(__ASSEMBLY__)
16
607ca46e 17#include <uapi/linux/kexec.h>
29a5c67e 18
2965faa5 19#ifdef CONFIG_KEXEC_CORE
dc009d92
EB
20#include <linux/list.h>
21#include <linux/linkage.h>
22#include <linux/compat.h>
9c15e852 23#include <linux/ioport.h>
6672f76a
SH
24#include <linux/elfcore.h>
25#include <linux/elf.h>
12db5562 26#include <linux/module.h>
dc009d92
EB
27#include <asm/kexec.h>
28
29/* Verify architecture specific macros are defined */
30
31#ifndef KEXEC_SOURCE_MEMORY_LIMIT
32#error KEXEC_SOURCE_MEMORY_LIMIT not defined
33#endif
34
35#ifndef KEXEC_DESTINATION_MEMORY_LIMIT
36#error KEXEC_DESTINATION_MEMORY_LIMIT not defined
37#endif
38
39#ifndef KEXEC_CONTROL_MEMORY_LIMIT
40#error KEXEC_CONTROL_MEMORY_LIMIT not defined
41#endif
42
7e01b5ac
MS
43#ifndef KEXEC_CONTROL_MEMORY_GFP
44#define KEXEC_CONTROL_MEMORY_GFP GFP_KERNEL
45#endif
46
163f6876
HY
47#ifndef KEXEC_CONTROL_PAGE_SIZE
48#error KEXEC_CONTROL_PAGE_SIZE not defined
dc009d92
EB
49#endif
50
51#ifndef KEXEC_ARCH
52#error KEXEC_ARCH not defined
53#endif
54
3d214fae
MH
55#ifndef KEXEC_CRASH_CONTROL_MEMORY_LIMIT
56#define KEXEC_CRASH_CONTROL_MEMORY_LIMIT KEXEC_CONTROL_MEMORY_LIMIT
57#endif
58
558df720
MH
59#ifndef KEXEC_CRASH_MEM_ALIGN
60#define KEXEC_CRASH_MEM_ALIGN PAGE_SIZE
61#endif
62
6672f76a
SH
63#define KEXEC_NOTE_HEAD_BYTES ALIGN(sizeof(struct elf_note), 4)
64#define KEXEC_CORE_NOTE_NAME "CORE"
65#define KEXEC_CORE_NOTE_NAME_BYTES ALIGN(sizeof(KEXEC_CORE_NOTE_NAME), 4)
66#define KEXEC_CORE_NOTE_DESC_BYTES ALIGN(sizeof(struct elf_prstatus), 4)
67/*
68 * The per-cpu notes area is a list of notes terminated by a "NULL"
69 * note header. For kdump, the code in vmcore.c runs in the context
70 * of the second kernel to combine them into one note.
71 */
cb78edfd 72#ifndef KEXEC_NOTE_BYTES
6672f76a
SH
73#define KEXEC_NOTE_BYTES ( (KEXEC_NOTE_HEAD_BYTES * 2) + \
74 KEXEC_CORE_NOTE_NAME_BYTES + \
75 KEXEC_CORE_NOTE_DESC_BYTES )
cb78edfd 76#endif
6672f76a 77
dc009d92
EB
78/*
79 * This structure is used to hold the arguments that are used when loading
80 * kernel binaries.
81 */
82
83typedef unsigned long kimage_entry_t;
dc009d92 84
dc009d92 85struct kexec_segment {
815d5704
VG
86 /*
87 * This pointer can point to user memory if kexec_load() system
88 * call is used or will point to kernel memory if
89 * kexec_file_load() system call is used.
90 *
91 * Use ->buf when expecting to deal with user memory and use ->kbuf
92 * when expecting to deal with kernel memory.
93 */
94 union {
95 void __user *buf;
96 void *kbuf;
97 };
dc009d92 98 size_t bufsz;
29a5c67e 99 unsigned long mem;
dc009d92
EB
100 size_t memsz;
101};
102
103#ifdef CONFIG_COMPAT
104struct compat_kexec_segment {
105 compat_uptr_t buf;
106 compat_size_t bufsz;
107 compat_ulong_t mem; /* User space sees this as a (void *) ... */
108 compat_size_t memsz;
109};
110#endif
111
978e30c9 112#ifdef CONFIG_KEXEC_FILE
12db5562
VG
113struct purgatory_info {
114 /* Pointer to elf header of read only purgatory */
115 Elf_Ehdr *ehdr;
116
117 /* Pointer to purgatory sechdrs which are modifiable */
118 Elf_Shdr *sechdrs;
119 /*
120 * Temporary buffer location where purgatory is loaded and relocated
121 * This memory can be freed post image load
122 */
123 void *purgatory_buf;
124
125 /* Address where purgatory is finally loaded and is executed from */
126 unsigned long purgatory_load_addr;
127};
128
978e30c9
XP
129typedef int (kexec_probe_t)(const char *kernel_buf, unsigned long kernel_size);
130typedef void *(kexec_load_t)(struct kimage *image, char *kernel_buf,
131 unsigned long kernel_len, char *initrd,
132 unsigned long initrd_len, char *cmdline,
133 unsigned long cmdline_len);
134typedef int (kexec_cleanup_t)(void *loader_data);
135
136#ifdef CONFIG_KEXEC_VERIFY_SIG
137typedef int (kexec_verify_sig_t)(const char *kernel_buf,
138 unsigned long kernel_len);
139#endif
140
141struct kexec_file_ops {
142 kexec_probe_t *probe;
143 kexec_load_t *load;
144 kexec_cleanup_t *cleanup;
145#ifdef CONFIG_KEXEC_VERIFY_SIG
146 kexec_verify_sig_t *verify_sig;
147#endif
148};
149#endif
150
dc009d92
EB
151struct kimage {
152 kimage_entry_t head;
153 kimage_entry_t *entry;
154 kimage_entry_t *last_entry;
155
dc009d92
EB
156 unsigned long start;
157 struct page *control_code_page;
3ab83521 158 struct page *swap_page;
dc009d92
EB
159
160 unsigned long nr_segments;
161 struct kexec_segment segment[KEXEC_SEGMENT_MAX];
162
163 struct list_head control_pages;
164 struct list_head dest_pages;
7d3e2bca 165 struct list_head unusable_pages;
dc009d92
EB
166
167 /* Address of next control page to allocate for crash kernels. */
168 unsigned long control_page;
169
170 /* Flags to indicate special processing */
171 unsigned int type : 1;
172#define KEXEC_TYPE_DEFAULT 0
173#define KEXEC_TYPE_CRASH 1
3ab83521 174 unsigned int preserve_context : 1;
cb105258
VG
175 /* If set, we are using file mode kexec syscall */
176 unsigned int file_mode:1;
92be3d6b
HY
177
178#ifdef ARCH_HAS_KIMAGE_ARCH
179 struct kimage_arch arch;
180#endif
cb105258 181
978e30c9 182#ifdef CONFIG_KEXEC_FILE
cb105258
VG
183 /* Additional fields for file based kexec syscall */
184 void *kernel_buf;
185 unsigned long kernel_buf_len;
186
187 void *initrd_buf;
188 unsigned long initrd_buf_len;
189
190 char *cmdline_buf;
191 unsigned long cmdline_buf_len;
192
193 /* File operations provided by image loader */
194 struct kexec_file_ops *fops;
195
196 /* Image loader handling the kernel can store a pointer here */
197 void *image_loader_data;
12db5562
VG
198
199 /* Information for loading purgatory */
200 struct purgatory_info purgatory_info;
978e30c9 201#endif
cb105258 202};
dc009d92
EB
203
204/* kexec interface functions */
3ab83521 205extern void machine_kexec(struct kimage *image);
dc009d92
EB
206extern int machine_kexec_prepare(struct kimage *image);
207extern void machine_kexec_cleanup(struct kimage *image);
208extern asmlinkage long sys_kexec_load(unsigned long entry,
72414d3f
MS
209 unsigned long nr_segments,
210 struct kexec_segment __user *segments,
211 unsigned long flags);
3ab83521 212extern int kernel_kexec(void);
cb105258
VG
213extern int kexec_add_buffer(struct kimage *image, char *buffer,
214 unsigned long bufsz, unsigned long memsz,
215 unsigned long buf_align, unsigned long buf_min,
216 unsigned long buf_max, bool top_down,
217 unsigned long *load_addr);
72414d3f
MS
218extern struct page *kimage_alloc_control_pages(struct kimage *image,
219 unsigned int order);
12db5562
VG
220extern int kexec_load_purgatory(struct kimage *image, unsigned long min,
221 unsigned long max, int top_down,
222 unsigned long *load_addr);
223extern int kexec_purgatory_get_set_symbol(struct kimage *image,
224 const char *name, void *buf,
225 unsigned int size, bool get_value);
226extern void *kexec_purgatory_get_symbol_addr(struct kimage *image,
227 const char *name);
7bbee5ca 228extern void __crash_kexec(struct pt_regs *);
6e274d14
AN
229extern void crash_kexec(struct pt_regs *);
230int kexec_should_crash(struct task_struct *);
85916f81 231void crash_save_cpu(struct pt_regs *regs, int cpu);
fd59d231
KO
232void crash_save_vmcoreinfo(void);
233void arch_crash_save_vmcoreinfo(void);
b9075fa9
JP
234__printf(1, 2)
235void vmcoreinfo_append_str(const char *fmt, ...);
fd59d231
KO
236unsigned long paddr_vmcoreinfo_note(void);
237
c6e2bee2
BW
238#define VMCOREINFO_OSRELEASE(value) \
239 vmcoreinfo_append_str("OSRELEASE=%s\n", value)
bba1f603
KO
240#define VMCOREINFO_PAGESIZE(value) \
241 vmcoreinfo_append_str("PAGESIZE=%ld\n", value)
bcbba6c1 242#define VMCOREINFO_SYMBOL(name) \
fd59d231 243 vmcoreinfo_append_str("SYMBOL(%s)=%lx\n", #name, (unsigned long)&name)
bcbba6c1 244#define VMCOREINFO_SIZE(name) \
6cfa062f
KO
245 vmcoreinfo_append_str("SIZE(%s)=%lu\n", #name, \
246 (unsigned long)sizeof(name))
c76f860c
KO
247#define VMCOREINFO_STRUCT_SIZE(name) \
248 vmcoreinfo_append_str("SIZE(%s)=%lu\n", #name, \
249 (unsigned long)sizeof(struct name))
bcbba6c1 250#define VMCOREINFO_OFFSET(name, field) \
d768281e 251 vmcoreinfo_append_str("OFFSET(%s.%s)=%lu\n", #name, #field, \
1e4f2955 252 (unsigned long)offsetof(struct name, field))
bcbba6c1 253#define VMCOREINFO_LENGTH(name, value) \
d768281e 254 vmcoreinfo_append_str("LENGTH(%s)=%lu\n", #name, (unsigned long)value)
bcbba6c1 255#define VMCOREINFO_NUMBER(name) \
6cfa062f 256 vmcoreinfo_append_str("NUMBER(%s)=%ld\n", #name, (long)name)
bcbba6c1 257#define VMCOREINFO_CONFIG(name) \
fd59d231
KO
258 vmcoreinfo_append_str("CONFIG_%s=y\n", #name)
259
dc009d92 260extern struct kimage *kexec_image;
c330dda9 261extern struct kimage *kexec_crash_image;
7984754b 262extern int kexec_load_disabled;
dc009d92 263
a7956113
ZN
264#ifndef kexec_flush_icache_page
265#define kexec_flush_icache_page(page)
266#endif
267
3ab83521
HY
268/* List of defined/legal kexec flags */
269#ifndef CONFIG_KEXEC_JUMP
270#define KEXEC_FLAGS KEXEC_ON_CRASH
271#else
272#define KEXEC_FLAGS (KEXEC_ON_CRASH | KEXEC_PRESERVE_CONTEXT)
273#endif
dc009d92 274
cb105258
VG
275/* List of defined/legal kexec file flags */
276#define KEXEC_FILE_FLAGS (KEXEC_FILE_UNLOAD | KEXEC_FILE_ON_CRASH | \
277 KEXEC_FILE_NO_INITRAMFS)
278
fd59d231
KO
279#define VMCOREINFO_BYTES (4096)
280#define VMCOREINFO_NOTE_NAME "VMCOREINFO"
281#define VMCOREINFO_NOTE_NAME_BYTES ALIGN(sizeof(VMCOREINFO_NOTE_NAME), 4)
282#define VMCOREINFO_NOTE_SIZE (KEXEC_NOTE_HEAD_BYTES*2 + VMCOREINFO_BYTES \
283 + VMCOREINFO_NOTE_NAME_BYTES)
284
dc009d92
EB
285/* Location of a reserved region to hold the crash kernel.
286 */
287extern struct resource crashk_res;
0212f915 288extern struct resource crashk_low_res;
6672f76a 289typedef u32 note_buf_t[KEXEC_NOTE_BYTES/4];
43cf38eb 290extern note_buf_t __percpu *crash_notes;
fd59d231 291extern u32 vmcoreinfo_note[VMCOREINFO_NOTE_SIZE/4];
d768281e
KO
292extern size_t vmcoreinfo_size;
293extern size_t vmcoreinfo_max_size;
dc009d92 294
4fc9bbf9
KA
295/* flag to track if kexec reboot is in progress */
296extern bool kexec_in_progress;
297
cba63c30
BW
298int __init parse_crashkernel(char *cmdline, unsigned long long system_ram,
299 unsigned long long *crash_size, unsigned long long *crash_base);
55a20ee7
YL
300int parse_crashkernel_high(char *cmdline, unsigned long long system_ram,
301 unsigned long long *crash_size, unsigned long long *crash_base);
0212f915
YL
302int parse_crashkernel_low(char *cmdline, unsigned long long system_ram,
303 unsigned long long *crash_size, unsigned long long *crash_base);
06a7f711
AW
304int crash_shrink_memory(unsigned long new_size);
305size_t crash_get_memory_size(void);
c0bb9e45 306void crash_free_reserved_phys_range(unsigned long begin, unsigned long end);
a7956113 307
a43cac0d
DY
308int __weak arch_kexec_kernel_image_probe(struct kimage *image, void *buf,
309 unsigned long buf_len);
310void * __weak arch_kexec_kernel_image_load(struct kimage *image);
311int __weak arch_kimage_file_post_load_cleanup(struct kimage *image);
312int __weak arch_kexec_kernel_verify_sig(struct kimage *image, void *buf,
313 unsigned long buf_len);
314int __weak arch_kexec_apply_relocations_add(const Elf_Ehdr *ehdr,
315 Elf_Shdr *sechdrs, unsigned int relsec);
316int __weak arch_kexec_apply_relocations(const Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
317 unsigned int relsec);
9b492cf5
XP
318void arch_kexec_protect_crashkres(void);
319void arch_kexec_unprotect_crashkres(void);
a43cac0d 320
2965faa5 321#else /* !CONFIG_KEXEC_CORE */
6e274d14
AN
322struct pt_regs;
323struct task_struct;
7bbee5ca 324static inline void __crash_kexec(struct pt_regs *regs) { }
6e274d14
AN
325static inline void crash_kexec(struct pt_regs *regs) { }
326static inline int kexec_should_crash(struct task_struct *p) { return 0; }
2b94ed24 327#define kexec_in_progress false
2965faa5 328#endif /* CONFIG_KEXEC_CORE */
cf2df639
GL
329
330#endif /* !defined(__ASSEBMLY__) */
331
dc009d92 332#endif /* LINUX_KEXEC_H */