riscv: Dump out kernel offset information on panic
[platform/kernel/linux-rpi.git] / arch / riscv / kernel / setup.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2009 Sunplus Core Technology Co., Ltd.
4  *  Chen Liqin <liqin.chen@sunplusct.com>
5  *  Lennox Wu <lennox.wu@sunplusct.com>
6  * Copyright (C) 2012 Regents of the University of California
7  * Copyright (C) 2020 FORTH-ICS/CARV
8  *  Nick Kossifidis <mick@ics.forth.gr>
9  */
10
11 #include <linux/acpi.h>
12 #include <linux/cpu.h>
13 #include <linux/init.h>
14 #include <linux/mm.h>
15 #include <linux/memblock.h>
16 #include <linux/sched.h>
17 #include <linux/console.h>
18 #include <linux/screen_info.h>
19 #include <linux/of_fdt.h>
20 #include <linux/sched/task.h>
21 #include <linux/smp.h>
22 #include <linux/efi.h>
23 #include <linux/crash_dump.h>
24 #include <linux/panic_notifier.h>
25
26 #include <asm/acpi.h>
27 #include <asm/alternative.h>
28 #include <asm/cacheflush.h>
29 #include <asm/cpu_ops.h>
30 #include <asm/early_ioremap.h>
31 #include <asm/pgtable.h>
32 #include <asm/setup.h>
33 #include <asm/set_memory.h>
34 #include <asm/sections.h>
35 #include <asm/sbi.h>
36 #include <asm/tlbflush.h>
37 #include <asm/thread_info.h>
38 #include <asm/kasan.h>
39 #include <asm/efi.h>
40
41 #include "head.h"
42
43 #if defined(CONFIG_DUMMY_CONSOLE) || defined(CONFIG_EFI)
44 struct screen_info screen_info __section(".data") = {
45         .orig_video_lines       = 30,
46         .orig_video_cols        = 80,
47         .orig_video_mode        = 0,
48         .orig_video_ega_bx      = 0,
49         .orig_video_isVGA       = 1,
50         .orig_video_points      = 8
51 };
52 #endif
53
54 /*
55  * The lucky hart to first increment this variable will boot the other cores.
56  * This is used before the kernel initializes the BSS so it can't be in the
57  * BSS.
58  */
59 atomic_t hart_lottery __section(".sdata")
60 #ifdef CONFIG_XIP_KERNEL
61 = ATOMIC_INIT(0xC001BEEF)
62 #endif
63 ;
64 unsigned long boot_cpu_hartid;
65 static DEFINE_PER_CPU(struct cpu, cpu_devices);
66
67 /*
68  * Place kernel memory regions on the resource tree so that
69  * kexec-tools can retrieve them from /proc/iomem. While there
70  * also add "System RAM" regions for compatibility with other
71  * archs, and the rest of the known regions for completeness.
72  */
73 static struct resource kimage_res = { .name = "Kernel image", };
74 static struct resource code_res = { .name = "Kernel code", };
75 static struct resource data_res = { .name = "Kernel data", };
76 static struct resource rodata_res = { .name = "Kernel rodata", };
77 static struct resource bss_res = { .name = "Kernel bss", };
78 #ifdef CONFIG_CRASH_DUMP
79 static struct resource elfcorehdr_res = { .name = "ELF Core hdr", };
80 #endif
81
82 static int __init add_resource(struct resource *parent,
83                                 struct resource *res)
84 {
85         int ret = 0;
86
87         ret = insert_resource(parent, res);
88         if (ret < 0) {
89                 pr_err("Failed to add a %s resource at %llx\n",
90                         res->name, (unsigned long long) res->start);
91                 return ret;
92         }
93
94         return 1;
95 }
96
97 static int __init add_kernel_resources(void)
98 {
99         int ret = 0;
100
101         /*
102          * The memory region of the kernel image is continuous and
103          * was reserved on setup_bootmem, register it here as a
104          * resource, with the various segments of the image as
105          * child nodes.
106          */
107
108         code_res.start = __pa_symbol(_text);
109         code_res.end = __pa_symbol(_etext) - 1;
110         code_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
111
112         rodata_res.start = __pa_symbol(__start_rodata);
113         rodata_res.end = __pa_symbol(__end_rodata) - 1;
114         rodata_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
115
116         data_res.start = __pa_symbol(_data);
117         data_res.end = __pa_symbol(_edata) - 1;
118         data_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
119
120         bss_res.start = __pa_symbol(__bss_start);
121         bss_res.end = __pa_symbol(__bss_stop) - 1;
122         bss_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
123
124         kimage_res.start = code_res.start;
125         kimage_res.end = bss_res.end;
126         kimage_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
127
128         ret = add_resource(&iomem_resource, &kimage_res);
129         if (ret < 0)
130                 return ret;
131
132         ret = add_resource(&kimage_res, &code_res);
133         if (ret < 0)
134                 return ret;
135
136         ret = add_resource(&kimage_res, &rodata_res);
137         if (ret < 0)
138                 return ret;
139
140         ret = add_resource(&kimage_res, &data_res);
141         if (ret < 0)
142                 return ret;
143
144         ret = add_resource(&kimage_res, &bss_res);
145
146         return ret;
147 }
148
149 static void __init init_resources(void)
150 {
151         struct memblock_region *region = NULL;
152         struct resource *res = NULL;
153         struct resource *mem_res = NULL;
154         size_t mem_res_sz = 0;
155         int num_resources = 0, res_idx = 0;
156         int ret = 0;
157
158         /* + 1 as memblock_alloc() might increase memblock.reserved.cnt */
159         num_resources = memblock.memory.cnt + memblock.reserved.cnt + 1;
160         res_idx = num_resources - 1;
161
162         mem_res_sz = num_resources * sizeof(*mem_res);
163         mem_res = memblock_alloc(mem_res_sz, SMP_CACHE_BYTES);
164         if (!mem_res)
165                 panic("%s: Failed to allocate %zu bytes\n", __func__, mem_res_sz);
166
167         /*
168          * Start by adding the reserved regions, if they overlap
169          * with /memory regions, insert_resource later on will take
170          * care of it.
171          */
172         ret = add_kernel_resources();
173         if (ret < 0)
174                 goto error;
175
176 #ifdef CONFIG_KEXEC_CORE
177         if (crashk_res.start != crashk_res.end) {
178                 ret = add_resource(&iomem_resource, &crashk_res);
179                 if (ret < 0)
180                         goto error;
181         }
182 #endif
183
184 #ifdef CONFIG_CRASH_DUMP
185         if (elfcorehdr_size > 0) {
186                 elfcorehdr_res.start = elfcorehdr_addr;
187                 elfcorehdr_res.end = elfcorehdr_addr + elfcorehdr_size - 1;
188                 elfcorehdr_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
189                 add_resource(&iomem_resource, &elfcorehdr_res);
190         }
191 #endif
192
193         for_each_reserved_mem_region(region) {
194                 res = &mem_res[res_idx--];
195
196                 res->name = "Reserved";
197                 res->flags = IORESOURCE_MEM | IORESOURCE_EXCLUSIVE;
198                 res->start = __pfn_to_phys(memblock_region_reserved_base_pfn(region));
199                 res->end = __pfn_to_phys(memblock_region_reserved_end_pfn(region)) - 1;
200
201                 /*
202                  * Ignore any other reserved regions within
203                  * system memory.
204                  */
205                 if (memblock_is_memory(res->start)) {
206                         /* Re-use this pre-allocated resource */
207                         res_idx++;
208                         continue;
209                 }
210
211                 ret = add_resource(&iomem_resource, res);
212                 if (ret < 0)
213                         goto error;
214         }
215
216         /* Add /memory regions to the resource tree */
217         for_each_mem_region(region) {
218                 res = &mem_res[res_idx--];
219
220                 if (unlikely(memblock_is_nomap(region))) {
221                         res->name = "Reserved";
222                         res->flags = IORESOURCE_MEM | IORESOURCE_EXCLUSIVE;
223                 } else {
224                         res->name = "System RAM";
225                         res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
226                 }
227
228                 res->start = __pfn_to_phys(memblock_region_memory_base_pfn(region));
229                 res->end = __pfn_to_phys(memblock_region_memory_end_pfn(region)) - 1;
230
231                 ret = add_resource(&iomem_resource, res);
232                 if (ret < 0)
233                         goto error;
234         }
235
236         /* Clean-up any unused pre-allocated resources */
237         if (res_idx >= 0)
238                 memblock_free(mem_res, (res_idx + 1) * sizeof(*mem_res));
239         return;
240
241  error:
242         /* Better an empty resource tree than an inconsistent one */
243         release_child_resources(&iomem_resource);
244         memblock_free(mem_res, mem_res_sz);
245 }
246
247
248 static void __init parse_dtb(void)
249 {
250         /* Early scan of device tree from init memory */
251         if (early_init_dt_scan(dtb_early_va)) {
252                 const char *name = of_flat_dt_get_machine_name();
253
254                 if (name) {
255                         pr_info("Machine model: %s\n", name);
256                         dump_stack_set_arch_desc("%s (DT)", name);
257                 }
258         } else {
259                 pr_err("No DTB passed to the kernel\n");
260         }
261
262 #ifdef CONFIG_CMDLINE_FORCE
263         strscpy(boot_command_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
264         pr_info("Forcing kernel command line to: %s\n", boot_command_line);
265 #endif
266 }
267
268 extern void __init init_rt_signal_env(void);
269
270 void __init setup_arch(char **cmdline_p)
271 {
272         parse_dtb();
273         setup_initial_init_mm(_stext, _etext, _edata, _end);
274
275         *cmdline_p = boot_command_line;
276
277         early_ioremap_setup();
278         sbi_init();
279         jump_label_init();
280         parse_early_param();
281
282         efi_init();
283         paging_init();
284
285         /* Parse the ACPI tables for possible boot-time configuration */
286         acpi_boot_table_init();
287
288 #if IS_ENABLED(CONFIG_BUILTIN_DTB)
289         unflatten_and_copy_device_tree();
290 #else
291         unflatten_device_tree();
292 #endif
293         misc_mem_init();
294
295         init_resources();
296
297 #ifdef CONFIG_KASAN
298         kasan_init();
299 #endif
300
301 #ifdef CONFIG_SMP
302         setup_smp();
303 #endif
304
305         if (!acpi_disabled)
306                 acpi_init_rintc_map();
307
308         riscv_init_cbo_blocksizes();
309         riscv_fill_hwcap();
310         init_rt_signal_env();
311         apply_boot_alternatives();
312         if (IS_ENABLED(CONFIG_RISCV_ISA_ZICBOM) &&
313             riscv_isa_extension_available(NULL, ZICBOM))
314                 riscv_noncoherent_supported();
315 }
316
317 static int __init topology_init(void)
318 {
319         int i, ret;
320
321         for_each_possible_cpu(i) {
322                 struct cpu *cpu = &per_cpu(cpu_devices, i);
323
324                 cpu->hotpluggable = cpu_has_hotplug(i);
325                 ret = register_cpu(cpu, i);
326                 if (unlikely(ret))
327                         pr_warn("Warning: %s: register_cpu %d failed (%d)\n",
328                                __func__, i, ret);
329         }
330
331         return 0;
332 }
333 subsys_initcall(topology_init);
334
335 void free_initmem(void)
336 {
337         if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX)) {
338                 set_kernel_memory(lm_alias(__init_begin), lm_alias(__init_end), set_memory_rw_nx);
339                 if (IS_ENABLED(CONFIG_64BIT))
340                         set_kernel_memory(__init_begin, __init_end, set_memory_nx);
341         }
342
343         free_initmem_default(POISON_FREE_INITMEM);
344 }
345
346 static int dump_kernel_offset(struct notifier_block *self,
347                               unsigned long v, void *p)
348 {
349         pr_emerg("Kernel Offset: 0x%lx from 0x%lx\n",
350                  kernel_map.virt_offset,
351                  KERNEL_LINK_ADDR);
352
353         return 0;
354 }
355
356 static struct notifier_block kernel_offset_notifier = {
357         .notifier_call = dump_kernel_offset
358 };
359
360 static int __init register_kernel_offset_dumper(void)
361 {
362         if (IS_ENABLED(CONFIG_RANDOMIZE_BASE))
363                 atomic_notifier_chain_register(&panic_notifier_list,
364                                                &kernel_offset_notifier);
365
366         return 0;
367 }
368 device_initcall(register_kernel_offset_dumper);