Linux Audio

Check our new training course

Loading...
v4.17
 
  1/*
  2 * crash.c - kernel crash support code.
  3 * Copyright (C) 2002-2004 Eric Biederman  <ebiederm@xmission.com>
  4 *
  5 * This source code is licensed under the GNU General Public License,
  6 * Version 2.  See the file COPYING for more details.
  7 */
  8
 
  9#include <linux/crash_core.h>
 10#include <linux/utsname.h>
 11#include <linux/vmalloc.h>
 12
 13#include <asm/page.h>
 14#include <asm/sections.h>
 15
 
 
 16/* vmcoreinfo stuff */
 17static unsigned char *vmcoreinfo_data;
 18static size_t vmcoreinfo_size;
 19u32 *vmcoreinfo_note;
 20
 21/* trusted vmcoreinfo, e.g. we can make a copy in the crash memory */
 22static unsigned char *vmcoreinfo_data_safecopy;
 23
 24/*
 25 * parsing the "crashkernel" commandline
 26 *
 27 * this code is intended to be called from architecture specific code
 28 */
 29
 30
 31/*
 32 * This function parses command lines in the format
 33 *
 34 *   crashkernel=ramsize-range:size[,...][@offset]
 35 *
 36 * The function returns 0 on success and -EINVAL on failure.
 37 */
 38static int __init parse_crashkernel_mem(char *cmdline,
 39					unsigned long long system_ram,
 40					unsigned long long *crash_size,
 41					unsigned long long *crash_base)
 42{
 43	char *cur = cmdline, *tmp;
 44
 45	/* for each entry of the comma-separated list */
 46	do {
 47		unsigned long long start, end = ULLONG_MAX, size;
 48
 49		/* get the start of the range */
 50		start = memparse(cur, &tmp);
 51		if (cur == tmp) {
 52			pr_warn("crashkernel: Memory value expected\n");
 53			return -EINVAL;
 54		}
 55		cur = tmp;
 56		if (*cur != '-') {
 57			pr_warn("crashkernel: '-' expected\n");
 58			return -EINVAL;
 59		}
 60		cur++;
 61
 62		/* if no ':' is here, than we read the end */
 63		if (*cur != ':') {
 64			end = memparse(cur, &tmp);
 65			if (cur == tmp) {
 66				pr_warn("crashkernel: Memory value expected\n");
 67				return -EINVAL;
 68			}
 69			cur = tmp;
 70			if (end <= start) {
 71				pr_warn("crashkernel: end <= start\n");
 72				return -EINVAL;
 73			}
 74		}
 75
 76		if (*cur != ':') {
 77			pr_warn("crashkernel: ':' expected\n");
 78			return -EINVAL;
 79		}
 80		cur++;
 81
 82		size = memparse(cur, &tmp);
 83		if (cur == tmp) {
 84			pr_warn("Memory value expected\n");
 85			return -EINVAL;
 86		}
 87		cur = tmp;
 88		if (size >= system_ram) {
 89			pr_warn("crashkernel: invalid size\n");
 90			return -EINVAL;
 91		}
 92
 93		/* match ? */
 94		if (system_ram >= start && system_ram < end) {
 95			*crash_size = size;
 96			break;
 97		}
 98	} while (*cur++ == ',');
 99
100	if (*crash_size > 0) {
101		while (*cur && *cur != ' ' && *cur != '@')
102			cur++;
103		if (*cur == '@') {
104			cur++;
105			*crash_base = memparse(cur, &tmp);
106			if (cur == tmp) {
107				pr_warn("Memory value expected after '@'\n");
108				return -EINVAL;
109			}
110		}
111	} else
112		pr_info("crashkernel size resulted in zero bytes\n");
113
114	return 0;
115}
116
117/*
118 * That function parses "simple" (old) crashkernel command lines like
119 *
120 *	crashkernel=size[@offset]
121 *
122 * It returns 0 on success and -EINVAL on failure.
123 */
124static int __init parse_crashkernel_simple(char *cmdline,
125					   unsigned long long *crash_size,
126					   unsigned long long *crash_base)
127{
128	char *cur = cmdline;
129
130	*crash_size = memparse(cmdline, &cur);
131	if (cmdline == cur) {
132		pr_warn("crashkernel: memory value expected\n");
133		return -EINVAL;
134	}
135
136	if (*cur == '@')
137		*crash_base = memparse(cur+1, &cur);
138	else if (*cur != ' ' && *cur != '\0') {
139		pr_warn("crashkernel: unrecognized char: %c\n", *cur);
140		return -EINVAL;
141	}
142
143	return 0;
144}
145
146#define SUFFIX_HIGH 0
147#define SUFFIX_LOW  1
148#define SUFFIX_NULL 2
149static __initdata char *suffix_tbl[] = {
150	[SUFFIX_HIGH] = ",high",
151	[SUFFIX_LOW]  = ",low",
152	[SUFFIX_NULL] = NULL,
153};
154
155/*
156 * That function parses "suffix"  crashkernel command lines like
157 *
158 *	crashkernel=size,[high|low]
159 *
160 * It returns 0 on success and -EINVAL on failure.
161 */
162static int __init parse_crashkernel_suffix(char *cmdline,
163					   unsigned long long	*crash_size,
164					   const char *suffix)
165{
166	char *cur = cmdline;
167
168	*crash_size = memparse(cmdline, &cur);
169	if (cmdline == cur) {
170		pr_warn("crashkernel: memory value expected\n");
171		return -EINVAL;
172	}
173
174	/* check with suffix */
175	if (strncmp(cur, suffix, strlen(suffix))) {
176		pr_warn("crashkernel: unrecognized char: %c\n", *cur);
177		return -EINVAL;
178	}
179	cur += strlen(suffix);
180	if (*cur != ' ' && *cur != '\0') {
181		pr_warn("crashkernel: unrecognized char: %c\n", *cur);
182		return -EINVAL;
183	}
184
185	return 0;
186}
187
188static __init char *get_last_crashkernel(char *cmdline,
189			     const char *name,
190			     const char *suffix)
191{
192	char *p = cmdline, *ck_cmdline = NULL;
193
194	/* find crashkernel and use the last one if there are more */
195	p = strstr(p, name);
196	while (p) {
197		char *end_p = strchr(p, ' ');
198		char *q;
199
200		if (!end_p)
201			end_p = p + strlen(p);
202
203		if (!suffix) {
204			int i;
205
206			/* skip the one with any known suffix */
207			for (i = 0; suffix_tbl[i]; i++) {
208				q = end_p - strlen(suffix_tbl[i]);
209				if (!strncmp(q, suffix_tbl[i],
210					     strlen(suffix_tbl[i])))
211					goto next;
212			}
213			ck_cmdline = p;
214		} else {
215			q = end_p - strlen(suffix);
216			if (!strncmp(q, suffix, strlen(suffix)))
217				ck_cmdline = p;
218		}
219next:
220		p = strstr(p+1, name);
221	}
222
223	if (!ck_cmdline)
224		return NULL;
225
226	return ck_cmdline;
227}
228
229static int __init __parse_crashkernel(char *cmdline,
230			     unsigned long long system_ram,
231			     unsigned long long *crash_size,
232			     unsigned long long *crash_base,
233			     const char *name,
234			     const char *suffix)
235{
236	char	*first_colon, *first_space;
237	char	*ck_cmdline;
238
239	BUG_ON(!crash_size || !crash_base);
240	*crash_size = 0;
241	*crash_base = 0;
242
243	ck_cmdline = get_last_crashkernel(cmdline, name, suffix);
244
245	if (!ck_cmdline)
246		return -EINVAL;
247
248	ck_cmdline += strlen(name);
249
250	if (suffix)
251		return parse_crashkernel_suffix(ck_cmdline, crash_size,
252				suffix);
253	/*
254	 * if the commandline contains a ':', then that's the extended
255	 * syntax -- if not, it must be the classic syntax
256	 */
257	first_colon = strchr(ck_cmdline, ':');
258	first_space = strchr(ck_cmdline, ' ');
259	if (first_colon && (!first_space || first_colon < first_space))
260		return parse_crashkernel_mem(ck_cmdline, system_ram,
261				crash_size, crash_base);
262
263	return parse_crashkernel_simple(ck_cmdline, crash_size, crash_base);
264}
265
266/*
267 * That function is the entry point for command line parsing and should be
268 * called from the arch-specific code.
269 */
270int __init parse_crashkernel(char *cmdline,
271			     unsigned long long system_ram,
272			     unsigned long long *crash_size,
273			     unsigned long long *crash_base)
274{
275	return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
276					"crashkernel=", NULL);
277}
278
279int __init parse_crashkernel_high(char *cmdline,
280			     unsigned long long system_ram,
281			     unsigned long long *crash_size,
282			     unsigned long long *crash_base)
283{
284	return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
285				"crashkernel=", suffix_tbl[SUFFIX_HIGH]);
286}
287
288int __init parse_crashkernel_low(char *cmdline,
289			     unsigned long long system_ram,
290			     unsigned long long *crash_size,
291			     unsigned long long *crash_base)
292{
293	return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
294				"crashkernel=", suffix_tbl[SUFFIX_LOW]);
295}
296
297Elf_Word *append_elf_note(Elf_Word *buf, char *name, unsigned int type,
298			  void *data, size_t data_len)
299{
300	struct elf_note *note = (struct elf_note *)buf;
301
302	note->n_namesz = strlen(name) + 1;
303	note->n_descsz = data_len;
304	note->n_type   = type;
305	buf += DIV_ROUND_UP(sizeof(*note), sizeof(Elf_Word));
306	memcpy(buf, name, note->n_namesz);
307	buf += DIV_ROUND_UP(note->n_namesz, sizeof(Elf_Word));
308	memcpy(buf, data, data_len);
309	buf += DIV_ROUND_UP(data_len, sizeof(Elf_Word));
310
311	return buf;
312}
313
314void final_note(Elf_Word *buf)
315{
316	memset(buf, 0, sizeof(struct elf_note));
317}
318
319static void update_vmcoreinfo_note(void)
320{
321	u32 *buf = vmcoreinfo_note;
322
323	if (!vmcoreinfo_size)
324		return;
325	buf = append_elf_note(buf, VMCOREINFO_NOTE_NAME, 0, vmcoreinfo_data,
326			      vmcoreinfo_size);
327	final_note(buf);
328}
329
330void crash_update_vmcoreinfo_safecopy(void *ptr)
331{
332	if (ptr)
333		memcpy(ptr, vmcoreinfo_data, vmcoreinfo_size);
334
335	vmcoreinfo_data_safecopy = ptr;
336}
337
338void crash_save_vmcoreinfo(void)
339{
340	if (!vmcoreinfo_note)
341		return;
342
343	/* Use the safe copy to generate vmcoreinfo note if have */
344	if (vmcoreinfo_data_safecopy)
345		vmcoreinfo_data = vmcoreinfo_data_safecopy;
346
347	vmcoreinfo_append_str("CRASHTIME=%ld\n", get_seconds());
348	update_vmcoreinfo_note();
349}
350
351void vmcoreinfo_append_str(const char *fmt, ...)
352{
353	va_list args;
354	char buf[0x50];
355	size_t r;
356
357	va_start(args, fmt);
358	r = vscnprintf(buf, sizeof(buf), fmt, args);
359	va_end(args);
360
361	r = min(r, (size_t)VMCOREINFO_BYTES - vmcoreinfo_size);
362
363	memcpy(&vmcoreinfo_data[vmcoreinfo_size], buf, r);
364
365	vmcoreinfo_size += r;
366}
367
368/*
369 * provide an empty default implementation here -- architecture
370 * code may override this
371 */
372void __weak arch_crash_save_vmcoreinfo(void)
373{}
374
375phys_addr_t __weak paddr_vmcoreinfo_note(void)
376{
377	return __pa(vmcoreinfo_note);
378}
379EXPORT_SYMBOL(paddr_vmcoreinfo_note);
380
381static int __init crash_save_vmcoreinfo_init(void)
382{
383	vmcoreinfo_data = (unsigned char *)get_zeroed_page(GFP_KERNEL);
384	if (!vmcoreinfo_data) {
385		pr_warn("Memory allocation for vmcoreinfo_data failed\n");
386		return -ENOMEM;
387	}
388
389	vmcoreinfo_note = alloc_pages_exact(VMCOREINFO_NOTE_SIZE,
390						GFP_KERNEL | __GFP_ZERO);
391	if (!vmcoreinfo_note) {
392		free_page((unsigned long)vmcoreinfo_data);
393		vmcoreinfo_data = NULL;
394		pr_warn("Memory allocation for vmcoreinfo_note failed\n");
395		return -ENOMEM;
396	}
397
398	VMCOREINFO_OSRELEASE(init_uts_ns.name.release);
 
399	VMCOREINFO_PAGESIZE(PAGE_SIZE);
400
401	VMCOREINFO_SYMBOL(init_uts_ns);
 
402	VMCOREINFO_SYMBOL(node_online_map);
403#ifdef CONFIG_MMU
404	VMCOREINFO_SYMBOL(swapper_pg_dir);
405#endif
406	VMCOREINFO_SYMBOL(_stext);
407	VMCOREINFO_SYMBOL(vmap_area_list);
408
409#ifndef CONFIG_NEED_MULTIPLE_NODES
410	VMCOREINFO_SYMBOL(mem_map);
411	VMCOREINFO_SYMBOL(contig_page_data);
412#endif
413#ifdef CONFIG_SPARSEMEM
414	VMCOREINFO_SYMBOL_ARRAY(mem_section);
415	VMCOREINFO_LENGTH(mem_section, NR_SECTION_ROOTS);
416	VMCOREINFO_STRUCT_SIZE(mem_section);
417	VMCOREINFO_OFFSET(mem_section, section_mem_map);
 
 
418#endif
419	VMCOREINFO_STRUCT_SIZE(page);
420	VMCOREINFO_STRUCT_SIZE(pglist_data);
421	VMCOREINFO_STRUCT_SIZE(zone);
422	VMCOREINFO_STRUCT_SIZE(free_area);
423	VMCOREINFO_STRUCT_SIZE(list_head);
424	VMCOREINFO_SIZE(nodemask_t);
425	VMCOREINFO_OFFSET(page, flags);
426	VMCOREINFO_OFFSET(page, _refcount);
427	VMCOREINFO_OFFSET(page, mapping);
428	VMCOREINFO_OFFSET(page, lru);
429	VMCOREINFO_OFFSET(page, _mapcount);
430	VMCOREINFO_OFFSET(page, private);
431	VMCOREINFO_OFFSET(page, compound_dtor);
432	VMCOREINFO_OFFSET(page, compound_order);
433	VMCOREINFO_OFFSET(page, compound_head);
434	VMCOREINFO_OFFSET(pglist_data, node_zones);
435	VMCOREINFO_OFFSET(pglist_data, nr_zones);
436#ifdef CONFIG_FLAT_NODE_MEM_MAP
437	VMCOREINFO_OFFSET(pglist_data, node_mem_map);
438#endif
439	VMCOREINFO_OFFSET(pglist_data, node_start_pfn);
440	VMCOREINFO_OFFSET(pglist_data, node_spanned_pages);
441	VMCOREINFO_OFFSET(pglist_data, node_id);
442	VMCOREINFO_OFFSET(zone, free_area);
443	VMCOREINFO_OFFSET(zone, vm_stat);
444	VMCOREINFO_OFFSET(zone, spanned_pages);
445	VMCOREINFO_OFFSET(free_area, free_list);
446	VMCOREINFO_OFFSET(list_head, next);
447	VMCOREINFO_OFFSET(list_head, prev);
448	VMCOREINFO_OFFSET(vmap_area, va_start);
449	VMCOREINFO_OFFSET(vmap_area, list);
450	VMCOREINFO_LENGTH(zone.free_area, MAX_ORDER);
451	log_buf_vmcoreinfo_setup();
452	VMCOREINFO_LENGTH(free_area.free_list, MIGRATE_TYPES);
453	VMCOREINFO_NUMBER(NR_FREE_PAGES);
454	VMCOREINFO_NUMBER(PG_lru);
455	VMCOREINFO_NUMBER(PG_private);
456	VMCOREINFO_NUMBER(PG_swapcache);
457	VMCOREINFO_NUMBER(PG_swapbacked);
458	VMCOREINFO_NUMBER(PG_slab);
459#ifdef CONFIG_MEMORY_FAILURE
460	VMCOREINFO_NUMBER(PG_hwpoison);
461#endif
462	VMCOREINFO_NUMBER(PG_head_mask);
 
463	VMCOREINFO_NUMBER(PAGE_BUDDY_MAPCOUNT_VALUE);
464#ifdef CONFIG_HUGETLB_PAGE
465	VMCOREINFO_NUMBER(HUGETLB_PAGE_DTOR);
 
 
466#endif
467
468	arch_crash_save_vmcoreinfo();
469	update_vmcoreinfo_note();
470
471	return 0;
472}
473
474subsys_initcall(crash_save_vmcoreinfo_init);
v5.14.15
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * crash.c - kernel crash support code.
  4 * Copyright (C) 2002-2004 Eric Biederman  <ebiederm@xmission.com>
 
 
 
  5 */
  6
  7#include <linux/buildid.h>
  8#include <linux/crash_core.h>
  9#include <linux/utsname.h>
 10#include <linux/vmalloc.h>
 11
 12#include <asm/page.h>
 13#include <asm/sections.h>
 14
 15#include <crypto/sha1.h>
 16
 17/* vmcoreinfo stuff */
 18unsigned char *vmcoreinfo_data;
 19size_t vmcoreinfo_size;
 20u32 *vmcoreinfo_note;
 21
 22/* trusted vmcoreinfo, e.g. we can make a copy in the crash memory */
 23static unsigned char *vmcoreinfo_data_safecopy;
 24
 25/*
 26 * parsing the "crashkernel" commandline
 27 *
 28 * this code is intended to be called from architecture specific code
 29 */
 30
 31
 32/*
 33 * This function parses command lines in the format
 34 *
 35 *   crashkernel=ramsize-range:size[,...][@offset]
 36 *
 37 * The function returns 0 on success and -EINVAL on failure.
 38 */
 39static int __init parse_crashkernel_mem(char *cmdline,
 40					unsigned long long system_ram,
 41					unsigned long long *crash_size,
 42					unsigned long long *crash_base)
 43{
 44	char *cur = cmdline, *tmp;
 45
 46	/* for each entry of the comma-separated list */
 47	do {
 48		unsigned long long start, end = ULLONG_MAX, size;
 49
 50		/* get the start of the range */
 51		start = memparse(cur, &tmp);
 52		if (cur == tmp) {
 53			pr_warn("crashkernel: Memory value expected\n");
 54			return -EINVAL;
 55		}
 56		cur = tmp;
 57		if (*cur != '-') {
 58			pr_warn("crashkernel: '-' expected\n");
 59			return -EINVAL;
 60		}
 61		cur++;
 62
 63		/* if no ':' is here, than we read the end */
 64		if (*cur != ':') {
 65			end = memparse(cur, &tmp);
 66			if (cur == tmp) {
 67				pr_warn("crashkernel: Memory value expected\n");
 68				return -EINVAL;
 69			}
 70			cur = tmp;
 71			if (end <= start) {
 72				pr_warn("crashkernel: end <= start\n");
 73				return -EINVAL;
 74			}
 75		}
 76
 77		if (*cur != ':') {
 78			pr_warn("crashkernel: ':' expected\n");
 79			return -EINVAL;
 80		}
 81		cur++;
 82
 83		size = memparse(cur, &tmp);
 84		if (cur == tmp) {
 85			pr_warn("Memory value expected\n");
 86			return -EINVAL;
 87		}
 88		cur = tmp;
 89		if (size >= system_ram) {
 90			pr_warn("crashkernel: invalid size\n");
 91			return -EINVAL;
 92		}
 93
 94		/* match ? */
 95		if (system_ram >= start && system_ram < end) {
 96			*crash_size = size;
 97			break;
 98		}
 99	} while (*cur++ == ',');
100
101	if (*crash_size > 0) {
102		while (*cur && *cur != ' ' && *cur != '@')
103			cur++;
104		if (*cur == '@') {
105			cur++;
106			*crash_base = memparse(cur, &tmp);
107			if (cur == tmp) {
108				pr_warn("Memory value expected after '@'\n");
109				return -EINVAL;
110			}
111		}
112	} else
113		pr_info("crashkernel size resulted in zero bytes\n");
114
115	return 0;
116}
117
118/*
119 * That function parses "simple" (old) crashkernel command lines like
120 *
121 *	crashkernel=size[@offset]
122 *
123 * It returns 0 on success and -EINVAL on failure.
124 */
125static int __init parse_crashkernel_simple(char *cmdline,
126					   unsigned long long *crash_size,
127					   unsigned long long *crash_base)
128{
129	char *cur = cmdline;
130
131	*crash_size = memparse(cmdline, &cur);
132	if (cmdline == cur) {
133		pr_warn("crashkernel: memory value expected\n");
134		return -EINVAL;
135	}
136
137	if (*cur == '@')
138		*crash_base = memparse(cur+1, &cur);
139	else if (*cur != ' ' && *cur != '\0') {
140		pr_warn("crashkernel: unrecognized char: %c\n", *cur);
141		return -EINVAL;
142	}
143
144	return 0;
145}
146
147#define SUFFIX_HIGH 0
148#define SUFFIX_LOW  1
149#define SUFFIX_NULL 2
150static __initdata char *suffix_tbl[] = {
151	[SUFFIX_HIGH] = ",high",
152	[SUFFIX_LOW]  = ",low",
153	[SUFFIX_NULL] = NULL,
154};
155
156/*
157 * That function parses "suffix"  crashkernel command lines like
158 *
159 *	crashkernel=size,[high|low]
160 *
161 * It returns 0 on success and -EINVAL on failure.
162 */
163static int __init parse_crashkernel_suffix(char *cmdline,
164					   unsigned long long	*crash_size,
165					   const char *suffix)
166{
167	char *cur = cmdline;
168
169	*crash_size = memparse(cmdline, &cur);
170	if (cmdline == cur) {
171		pr_warn("crashkernel: memory value expected\n");
172		return -EINVAL;
173	}
174
175	/* check with suffix */
176	if (strncmp(cur, suffix, strlen(suffix))) {
177		pr_warn("crashkernel: unrecognized char: %c\n", *cur);
178		return -EINVAL;
179	}
180	cur += strlen(suffix);
181	if (*cur != ' ' && *cur != '\0') {
182		pr_warn("crashkernel: unrecognized char: %c\n", *cur);
183		return -EINVAL;
184	}
185
186	return 0;
187}
188
189static __init char *get_last_crashkernel(char *cmdline,
190			     const char *name,
191			     const char *suffix)
192{
193	char *p = cmdline, *ck_cmdline = NULL;
194
195	/* find crashkernel and use the last one if there are more */
196	p = strstr(p, name);
197	while (p) {
198		char *end_p = strchr(p, ' ');
199		char *q;
200
201		if (!end_p)
202			end_p = p + strlen(p);
203
204		if (!suffix) {
205			int i;
206
207			/* skip the one with any known suffix */
208			for (i = 0; suffix_tbl[i]; i++) {
209				q = end_p - strlen(suffix_tbl[i]);
210				if (!strncmp(q, suffix_tbl[i],
211					     strlen(suffix_tbl[i])))
212					goto next;
213			}
214			ck_cmdline = p;
215		} else {
216			q = end_p - strlen(suffix);
217			if (!strncmp(q, suffix, strlen(suffix)))
218				ck_cmdline = p;
219		}
220next:
221		p = strstr(p+1, name);
222	}
223
224	if (!ck_cmdline)
225		return NULL;
226
227	return ck_cmdline;
228}
229
230static int __init __parse_crashkernel(char *cmdline,
231			     unsigned long long system_ram,
232			     unsigned long long *crash_size,
233			     unsigned long long *crash_base,
234			     const char *name,
235			     const char *suffix)
236{
237	char	*first_colon, *first_space;
238	char	*ck_cmdline;
239
240	BUG_ON(!crash_size || !crash_base);
241	*crash_size = 0;
242	*crash_base = 0;
243
244	ck_cmdline = get_last_crashkernel(cmdline, name, suffix);
245
246	if (!ck_cmdline)
247		return -EINVAL;
248
249	ck_cmdline += strlen(name);
250
251	if (suffix)
252		return parse_crashkernel_suffix(ck_cmdline, crash_size,
253				suffix);
254	/*
255	 * if the commandline contains a ':', then that's the extended
256	 * syntax -- if not, it must be the classic syntax
257	 */
258	first_colon = strchr(ck_cmdline, ':');
259	first_space = strchr(ck_cmdline, ' ');
260	if (first_colon && (!first_space || first_colon < first_space))
261		return parse_crashkernel_mem(ck_cmdline, system_ram,
262				crash_size, crash_base);
263
264	return parse_crashkernel_simple(ck_cmdline, crash_size, crash_base);
265}
266
267/*
268 * That function is the entry point for command line parsing and should be
269 * called from the arch-specific code.
270 */
271int __init parse_crashkernel(char *cmdline,
272			     unsigned long long system_ram,
273			     unsigned long long *crash_size,
274			     unsigned long long *crash_base)
275{
276	return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
277					"crashkernel=", NULL);
278}
279
280int __init parse_crashkernel_high(char *cmdline,
281			     unsigned long long system_ram,
282			     unsigned long long *crash_size,
283			     unsigned long long *crash_base)
284{
285	return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
286				"crashkernel=", suffix_tbl[SUFFIX_HIGH]);
287}
288
289int __init parse_crashkernel_low(char *cmdline,
290			     unsigned long long system_ram,
291			     unsigned long long *crash_size,
292			     unsigned long long *crash_base)
293{
294	return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
295				"crashkernel=", suffix_tbl[SUFFIX_LOW]);
296}
297
298Elf_Word *append_elf_note(Elf_Word *buf, char *name, unsigned int type,
299			  void *data, size_t data_len)
300{
301	struct elf_note *note = (struct elf_note *)buf;
302
303	note->n_namesz = strlen(name) + 1;
304	note->n_descsz = data_len;
305	note->n_type   = type;
306	buf += DIV_ROUND_UP(sizeof(*note), sizeof(Elf_Word));
307	memcpy(buf, name, note->n_namesz);
308	buf += DIV_ROUND_UP(note->n_namesz, sizeof(Elf_Word));
309	memcpy(buf, data, data_len);
310	buf += DIV_ROUND_UP(data_len, sizeof(Elf_Word));
311
312	return buf;
313}
314
315void final_note(Elf_Word *buf)
316{
317	memset(buf, 0, sizeof(struct elf_note));
318}
319
320static void update_vmcoreinfo_note(void)
321{
322	u32 *buf = vmcoreinfo_note;
323
324	if (!vmcoreinfo_size)
325		return;
326	buf = append_elf_note(buf, VMCOREINFO_NOTE_NAME, 0, vmcoreinfo_data,
327			      vmcoreinfo_size);
328	final_note(buf);
329}
330
331void crash_update_vmcoreinfo_safecopy(void *ptr)
332{
333	if (ptr)
334		memcpy(ptr, vmcoreinfo_data, vmcoreinfo_size);
335
336	vmcoreinfo_data_safecopy = ptr;
337}
338
339void crash_save_vmcoreinfo(void)
340{
341	if (!vmcoreinfo_note)
342		return;
343
344	/* Use the safe copy to generate vmcoreinfo note if have */
345	if (vmcoreinfo_data_safecopy)
346		vmcoreinfo_data = vmcoreinfo_data_safecopy;
347
348	vmcoreinfo_append_str("CRASHTIME=%lld\n", ktime_get_real_seconds());
349	update_vmcoreinfo_note();
350}
351
352void vmcoreinfo_append_str(const char *fmt, ...)
353{
354	va_list args;
355	char buf[0x50];
356	size_t r;
357
358	va_start(args, fmt);
359	r = vscnprintf(buf, sizeof(buf), fmt, args);
360	va_end(args);
361
362	r = min(r, (size_t)VMCOREINFO_BYTES - vmcoreinfo_size);
363
364	memcpy(&vmcoreinfo_data[vmcoreinfo_size], buf, r);
365
366	vmcoreinfo_size += r;
367}
368
369/*
370 * provide an empty default implementation here -- architecture
371 * code may override this
372 */
373void __weak arch_crash_save_vmcoreinfo(void)
374{}
375
376phys_addr_t __weak paddr_vmcoreinfo_note(void)
377{
378	return __pa(vmcoreinfo_note);
379}
380EXPORT_SYMBOL(paddr_vmcoreinfo_note);
381
382static int __init crash_save_vmcoreinfo_init(void)
383{
384	vmcoreinfo_data = (unsigned char *)get_zeroed_page(GFP_KERNEL);
385	if (!vmcoreinfo_data) {
386		pr_warn("Memory allocation for vmcoreinfo_data failed\n");
387		return -ENOMEM;
388	}
389
390	vmcoreinfo_note = alloc_pages_exact(VMCOREINFO_NOTE_SIZE,
391						GFP_KERNEL | __GFP_ZERO);
392	if (!vmcoreinfo_note) {
393		free_page((unsigned long)vmcoreinfo_data);
394		vmcoreinfo_data = NULL;
395		pr_warn("Memory allocation for vmcoreinfo_note failed\n");
396		return -ENOMEM;
397	}
398
399	VMCOREINFO_OSRELEASE(init_uts_ns.name.release);
400	VMCOREINFO_BUILD_ID();
401	VMCOREINFO_PAGESIZE(PAGE_SIZE);
402
403	VMCOREINFO_SYMBOL(init_uts_ns);
404	VMCOREINFO_OFFSET(uts_namespace, name);
405	VMCOREINFO_SYMBOL(node_online_map);
406#ifdef CONFIG_MMU
407	VMCOREINFO_SYMBOL_ARRAY(swapper_pg_dir);
408#endif
409	VMCOREINFO_SYMBOL(_stext);
410	VMCOREINFO_SYMBOL(vmap_area_list);
411
412#ifndef CONFIG_NUMA
413	VMCOREINFO_SYMBOL(mem_map);
414	VMCOREINFO_SYMBOL(contig_page_data);
415#endif
416#ifdef CONFIG_SPARSEMEM
417	VMCOREINFO_SYMBOL_ARRAY(mem_section);
418	VMCOREINFO_LENGTH(mem_section, NR_SECTION_ROOTS);
419	VMCOREINFO_STRUCT_SIZE(mem_section);
420	VMCOREINFO_OFFSET(mem_section, section_mem_map);
421	VMCOREINFO_NUMBER(SECTION_SIZE_BITS);
422	VMCOREINFO_NUMBER(MAX_PHYSMEM_BITS);
423#endif
424	VMCOREINFO_STRUCT_SIZE(page);
425	VMCOREINFO_STRUCT_SIZE(pglist_data);
426	VMCOREINFO_STRUCT_SIZE(zone);
427	VMCOREINFO_STRUCT_SIZE(free_area);
428	VMCOREINFO_STRUCT_SIZE(list_head);
429	VMCOREINFO_SIZE(nodemask_t);
430	VMCOREINFO_OFFSET(page, flags);
431	VMCOREINFO_OFFSET(page, _refcount);
432	VMCOREINFO_OFFSET(page, mapping);
433	VMCOREINFO_OFFSET(page, lru);
434	VMCOREINFO_OFFSET(page, _mapcount);
435	VMCOREINFO_OFFSET(page, private);
436	VMCOREINFO_OFFSET(page, compound_dtor);
437	VMCOREINFO_OFFSET(page, compound_order);
438	VMCOREINFO_OFFSET(page, compound_head);
439	VMCOREINFO_OFFSET(pglist_data, node_zones);
440	VMCOREINFO_OFFSET(pglist_data, nr_zones);
441#ifdef CONFIG_FLATMEM
442	VMCOREINFO_OFFSET(pglist_data, node_mem_map);
443#endif
444	VMCOREINFO_OFFSET(pglist_data, node_start_pfn);
445	VMCOREINFO_OFFSET(pglist_data, node_spanned_pages);
446	VMCOREINFO_OFFSET(pglist_data, node_id);
447	VMCOREINFO_OFFSET(zone, free_area);
448	VMCOREINFO_OFFSET(zone, vm_stat);
449	VMCOREINFO_OFFSET(zone, spanned_pages);
450	VMCOREINFO_OFFSET(free_area, free_list);
451	VMCOREINFO_OFFSET(list_head, next);
452	VMCOREINFO_OFFSET(list_head, prev);
453	VMCOREINFO_OFFSET(vmap_area, va_start);
454	VMCOREINFO_OFFSET(vmap_area, list);
455	VMCOREINFO_LENGTH(zone.free_area, MAX_ORDER);
456	log_buf_vmcoreinfo_setup();
457	VMCOREINFO_LENGTH(free_area.free_list, MIGRATE_TYPES);
458	VMCOREINFO_NUMBER(NR_FREE_PAGES);
459	VMCOREINFO_NUMBER(PG_lru);
460	VMCOREINFO_NUMBER(PG_private);
461	VMCOREINFO_NUMBER(PG_swapcache);
462	VMCOREINFO_NUMBER(PG_swapbacked);
463	VMCOREINFO_NUMBER(PG_slab);
464#ifdef CONFIG_MEMORY_FAILURE
465	VMCOREINFO_NUMBER(PG_hwpoison);
466#endif
467	VMCOREINFO_NUMBER(PG_head_mask);
468#define PAGE_BUDDY_MAPCOUNT_VALUE	(~PG_buddy)
469	VMCOREINFO_NUMBER(PAGE_BUDDY_MAPCOUNT_VALUE);
470#ifdef CONFIG_HUGETLB_PAGE
471	VMCOREINFO_NUMBER(HUGETLB_PAGE_DTOR);
472#define PAGE_OFFLINE_MAPCOUNT_VALUE	(~PG_offline)
473	VMCOREINFO_NUMBER(PAGE_OFFLINE_MAPCOUNT_VALUE);
474#endif
475
476	arch_crash_save_vmcoreinfo();
477	update_vmcoreinfo_note();
478
479	return 0;
480}
481
482subsys_initcall(crash_save_vmcoreinfo_init);