1 // SPDX-License-Identifier: GPL-2.0-only
3 * crash.c - kernel crash support code.
4 * Copyright (C) 2002-2004 Eric Biederman <ebiederm@xmission.com>
7 #include <linux/buildid.h>
8 #include <linux/init.h>
9 #include <linux/utsname.h>
10 #include <linux/vmalloc.h>
11 #include <linux/sizes.h>
12 #include <linux/kexec.h>
13 #include <linux/memory.h>
14 #include <linux/cpuhotplug.h>
15 #include <linux/memblock.h>
16 #include <linux/kmemleak.h>
19 #include <asm/sections.h>
21 #include <crypto/sha1.h>
23 #include "kallsyms_internal.h"
24 #include "kexec_internal.h"
26 /* vmcoreinfo stuff */
27 unsigned char *vmcoreinfo_data
;
28 size_t vmcoreinfo_size
;
31 /* trusted vmcoreinfo, e.g. we can make a copy in the crash memory */
32 static unsigned char *vmcoreinfo_data_safecopy
;
34 Elf_Word
*append_elf_note(Elf_Word
*buf
, char *name
, unsigned int type
,
35 void *data
, size_t data_len
)
37 struct elf_note
*note
= (struct elf_note
*)buf
;
39 note
->n_namesz
= strlen(name
) + 1;
40 note
->n_descsz
= data_len
;
42 buf
+= DIV_ROUND_UP(sizeof(*note
), sizeof(Elf_Word
));
43 memcpy(buf
, name
, note
->n_namesz
);
44 buf
+= DIV_ROUND_UP(note
->n_namesz
, sizeof(Elf_Word
));
45 memcpy(buf
, data
, data_len
);
46 buf
+= DIV_ROUND_UP(data_len
, sizeof(Elf_Word
));
51 void final_note(Elf_Word
*buf
)
53 memset(buf
, 0, sizeof(struct elf_note
));
56 static void update_vmcoreinfo_note(void)
58 u32
*buf
= vmcoreinfo_note
;
62 buf
= append_elf_note(buf
, VMCOREINFO_NOTE_NAME
, 0, vmcoreinfo_data
,
67 void crash_update_vmcoreinfo_safecopy(void *ptr
)
70 memcpy(ptr
, vmcoreinfo_data
, vmcoreinfo_size
);
72 vmcoreinfo_data_safecopy
= ptr
;
75 void crash_save_vmcoreinfo(void)
80 /* Use the safe copy to generate vmcoreinfo note if have */
81 if (vmcoreinfo_data_safecopy
)
82 vmcoreinfo_data
= vmcoreinfo_data_safecopy
;
84 vmcoreinfo_append_str("CRASHTIME=%lld\n", ktime_get_real_seconds());
85 update_vmcoreinfo_note();
88 void vmcoreinfo_append_str(const char *fmt
, ...)
95 r
= vscnprintf(buf
, sizeof(buf
), fmt
, args
);
98 r
= min(r
, (size_t)VMCOREINFO_BYTES
- vmcoreinfo_size
);
100 memcpy(&vmcoreinfo_data
[vmcoreinfo_size
], buf
, r
);
102 vmcoreinfo_size
+= r
;
104 WARN_ONCE(vmcoreinfo_size
== VMCOREINFO_BYTES
,
105 "vmcoreinfo data exceeds allocated size, truncating");
109 * provide an empty default implementation here -- architecture
110 * code may override this
112 void __weak
arch_crash_save_vmcoreinfo(void)
115 phys_addr_t __weak
paddr_vmcoreinfo_note(void)
117 return __pa(vmcoreinfo_note
);
119 EXPORT_SYMBOL(paddr_vmcoreinfo_note
);
121 static int __init
crash_save_vmcoreinfo_init(void)
123 vmcoreinfo_data
= (unsigned char *)get_zeroed_page(GFP_KERNEL
);
124 if (!vmcoreinfo_data
) {
125 pr_warn("Memory allocation for vmcoreinfo_data failed\n");
129 vmcoreinfo_note
= alloc_pages_exact(VMCOREINFO_NOTE_SIZE
,
130 GFP_KERNEL
| __GFP_ZERO
);
131 if (!vmcoreinfo_note
) {
132 free_page((unsigned long)vmcoreinfo_data
);
133 vmcoreinfo_data
= NULL
;
134 pr_warn("Memory allocation for vmcoreinfo_note failed\n");
138 VMCOREINFO_OSRELEASE(init_uts_ns
.name
.release
);
139 VMCOREINFO_BUILD_ID();
140 VMCOREINFO_PAGESIZE(PAGE_SIZE
);
142 VMCOREINFO_SYMBOL(init_uts_ns
);
143 VMCOREINFO_OFFSET(uts_namespace
, name
);
144 VMCOREINFO_SYMBOL(node_online_map
);
146 VMCOREINFO_SYMBOL_ARRAY(swapper_pg_dir
);
148 VMCOREINFO_SYMBOL(_stext
);
149 vmcoreinfo_append_str("NUMBER(VMALLOC_START)=0x%lx\n", (unsigned long) VMALLOC_START
);
152 VMCOREINFO_SYMBOL(mem_map
);
153 VMCOREINFO_SYMBOL(contig_page_data
);
155 #ifdef CONFIG_SPARSEMEM_VMEMMAP
156 VMCOREINFO_SYMBOL_ARRAY(vmemmap
);
158 #ifdef CONFIG_SPARSEMEM
159 VMCOREINFO_SYMBOL_ARRAY(mem_section
);
160 VMCOREINFO_LENGTH(mem_section
, NR_SECTION_ROOTS
);
161 VMCOREINFO_STRUCT_SIZE(mem_section
);
162 VMCOREINFO_OFFSET(mem_section
, section_mem_map
);
163 VMCOREINFO_NUMBER(SECTION_SIZE_BITS
);
164 VMCOREINFO_NUMBER(MAX_PHYSMEM_BITS
);
166 VMCOREINFO_STRUCT_SIZE(page
);
167 VMCOREINFO_STRUCT_SIZE(pglist_data
);
168 VMCOREINFO_STRUCT_SIZE(zone
);
169 VMCOREINFO_STRUCT_SIZE(free_area
);
170 VMCOREINFO_STRUCT_SIZE(list_head
);
171 VMCOREINFO_SIZE(nodemask_t
);
172 VMCOREINFO_OFFSET(page
, flags
);
173 VMCOREINFO_OFFSET(page
, _refcount
);
174 VMCOREINFO_OFFSET(page
, mapping
);
175 VMCOREINFO_OFFSET(page
, lru
);
176 VMCOREINFO_OFFSET(page
, _mapcount
);
177 VMCOREINFO_OFFSET(page
, private);
178 VMCOREINFO_OFFSET(page
, compound_head
);
179 VMCOREINFO_OFFSET(pglist_data
, node_zones
);
180 VMCOREINFO_OFFSET(pglist_data
, nr_zones
);
181 #ifdef CONFIG_FLATMEM
182 VMCOREINFO_OFFSET(pglist_data
, node_mem_map
);
184 VMCOREINFO_OFFSET(pglist_data
, node_start_pfn
);
185 VMCOREINFO_OFFSET(pglist_data
, node_spanned_pages
);
186 VMCOREINFO_OFFSET(pglist_data
, node_id
);
187 VMCOREINFO_OFFSET(zone
, free_area
);
188 VMCOREINFO_OFFSET(zone
, vm_stat
);
189 VMCOREINFO_OFFSET(zone
, spanned_pages
);
190 VMCOREINFO_OFFSET(free_area
, free_list
);
191 VMCOREINFO_OFFSET(list_head
, next
);
192 VMCOREINFO_OFFSET(list_head
, prev
);
193 VMCOREINFO_LENGTH(zone
.free_area
, NR_PAGE_ORDERS
);
194 log_buf_vmcoreinfo_setup();
195 VMCOREINFO_LENGTH(free_area
.free_list
, MIGRATE_TYPES
);
196 VMCOREINFO_NUMBER(NR_FREE_PAGES
);
197 VMCOREINFO_NUMBER(PG_lru
);
198 VMCOREINFO_NUMBER(PG_private
);
199 VMCOREINFO_NUMBER(PG_swapcache
);
200 VMCOREINFO_NUMBER(PG_swapbacked
);
201 #define PAGE_SLAB_MAPCOUNT_VALUE (PGTY_slab << 24)
202 VMCOREINFO_NUMBER(PAGE_SLAB_MAPCOUNT_VALUE
);
203 #ifdef CONFIG_MEMORY_FAILURE
204 VMCOREINFO_NUMBER(PG_hwpoison
);
206 VMCOREINFO_NUMBER(PG_head_mask
);
207 #define PAGE_BUDDY_MAPCOUNT_VALUE (PGTY_buddy << 24)
208 VMCOREINFO_NUMBER(PAGE_BUDDY_MAPCOUNT_VALUE
);
209 #define PAGE_HUGETLB_MAPCOUNT_VALUE (PGTY_hugetlb << 24)
210 VMCOREINFO_NUMBER(PAGE_HUGETLB_MAPCOUNT_VALUE
);
211 #define PAGE_OFFLINE_MAPCOUNT_VALUE (PGTY_offline << 24)
212 VMCOREINFO_NUMBER(PAGE_OFFLINE_MAPCOUNT_VALUE
);
214 #ifdef CONFIG_KALLSYMS
215 VMCOREINFO_SYMBOL(kallsyms_names
);
216 VMCOREINFO_SYMBOL(kallsyms_num_syms
);
217 VMCOREINFO_SYMBOL(kallsyms_token_table
);
218 VMCOREINFO_SYMBOL(kallsyms_token_index
);
219 VMCOREINFO_SYMBOL(kallsyms_offsets
);
220 VMCOREINFO_SYMBOL(kallsyms_relative_base
);
221 #endif /* CONFIG_KALLSYMS */
223 arch_crash_save_vmcoreinfo();
224 update_vmcoreinfo_note();
229 subsys_initcall(crash_save_vmcoreinfo_init
);