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