1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright IBM Corp. 2007, 2009
4 * Author(s): Hongjie Yang <hongjie@us.ibm.com>,
5 */
6
7 #define KMSG_COMPONENT "setup"
8 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
9
10 #include <linux/sched/debug.h>
11 #include <linux/cpufeature.h>
12 #include <linux/compiler.h>
13 #include <linux/init.h>
14 #include <linux/errno.h>
15 #include <linux/string.h>
16 #include <linux/ctype.h>
17 #include <linux/lockdep.h>
18 #include <linux/extable.h>
19 #include <linux/pfn.h>
20 #include <linux/uaccess.h>
21 #include <linux/kernel.h>
22 #include <asm/asm-extable.h>
23 #include <linux/memblock.h>
24 #include <asm/access-regs.h>
25 #include <asm/asm-offsets.h>
26 #include <asm/machine.h>
27 #include <asm/diag.h>
28 #include <asm/ebcdic.h>
29 #include <asm/fpu.h>
30 #include <asm/ipl.h>
31 #include <asm/lowcore.h>
32 #include <asm/processor.h>
33 #include <asm/sections.h>
34 #include <asm/setup.h>
35 #include <asm/sysinfo.h>
36 #include <asm/cpcmd.h>
37 #include <asm/sclp.h>
38 #include <asm/facility.h>
39 #include <asm/boot_data.h>
40 #include "entry.h"
41
42 #define __decompressor_handled_param(func, param) \
43 static int __init ignore_decompressor_param_##func(char *s) \
44 { \
45 return 0; \
46 } \
47 early_param(#param, ignore_decompressor_param_##func)
48
49 #define decompressor_handled_param(param) __decompressor_handled_param(param, param)
50
51 decompressor_handled_param(mem);
52 decompressor_handled_param(vmalloc);
53 decompressor_handled_param(dfltcc);
54 decompressor_handled_param(facilities);
55 decompressor_handled_param(nokaslr);
56 decompressor_handled_param(cmma);
57 decompressor_handled_param(relocate_lowcore);
58 decompressor_handled_param(bootdebug);
59 __decompressor_handled_param(debug_alternative, debug-alternative);
60 #if IS_ENABLED(CONFIG_KVM)
61 decompressor_handled_param(prot_virt);
62 #endif
63
kasan_early_init(void)64 static void __init kasan_early_init(void)
65 {
66 #ifdef CONFIG_KASAN
67 init_task.kasan_depth = 0;
68 pr_info("KernelAddressSanitizer initialized\n");
69 #endif
70 }
71
72 /*
73 * Initialize storage key for kernel pages
74 */
init_kernel_storage_key(void)75 static noinline __init void init_kernel_storage_key(void)
76 {
77 #if PAGE_DEFAULT_KEY
78 unsigned long end_pfn, init_pfn;
79
80 end_pfn = PFN_UP(__pa(_end));
81
82 for (init_pfn = 0 ; init_pfn < end_pfn; init_pfn++)
83 page_set_storage_key(init_pfn << PAGE_SHIFT,
84 PAGE_DEFAULT_KEY, 0);
85 #endif
86 }
87
88 static __initdata char sysinfo_page[PAGE_SIZE] __aligned(PAGE_SIZE);
89
90 /* Remove leading, trailing and double whitespace. */
strim_all(char * str)91 static inline void strim_all(char *str)
92 {
93 char *s;
94
95 s = strim(str);
96 if (s != str)
97 memmove(str, s, strlen(s));
98 while (*str) {
99 if (!isspace(*str++))
100 continue;
101 if (isspace(*str)) {
102 s = skip_spaces(str);
103 memmove(str, s, strlen(s) + 1);
104 }
105 }
106 }
107
108 char arch_hw_string[128];
109
setup_arch_string(void)110 static noinline __init void setup_arch_string(void)
111 {
112 struct sysinfo_1_1_1 *mach = (struct sysinfo_1_1_1 *)&sysinfo_page;
113 struct sysinfo_3_2_2 *vm = (struct sysinfo_3_2_2 *)&sysinfo_page;
114 char mstr[80], hvstr[17];
115
116 if (stsi(mach, 1, 1, 1))
117 return;
118 EBCASC(mach->manufacturer, sizeof(mach->manufacturer));
119 EBCASC(mach->type, sizeof(mach->type));
120 EBCASC(mach->model, sizeof(mach->model));
121 EBCASC(mach->model_capacity, sizeof(mach->model_capacity));
122 sprintf(mstr, "%-16.16s %-4.4s %-16.16s %-16.16s",
123 mach->manufacturer, mach->type,
124 mach->model, mach->model_capacity);
125 strim_all(mstr);
126 if (stsi(vm, 3, 2, 2) == 0 && vm->count) {
127 EBCASC(vm->vm[0].cpi, sizeof(vm->vm[0].cpi));
128 sprintf(hvstr, "%-16.16s", vm->vm[0].cpi);
129 strim_all(hvstr);
130 } else {
131 sprintf(hvstr, "%s",
132 machine_is_lpar() ? "LPAR" :
133 machine_is_vm() ? "z/VM" :
134 machine_is_kvm() ? "KVM" : "unknown");
135 }
136 sprintf(arch_hw_string, "HW: %s (%s)", mstr, hvstr);
137 dump_stack_set_arch_desc("%s (%s)", mstr, hvstr);
138 }
139
setup_topology(void)140 static __init void setup_topology(void)
141 {
142 int max_mnest;
143
144 if (!cpu_has_topology())
145 return;
146 for (max_mnest = 6; max_mnest > 1; max_mnest--) {
147 if (stsi(&sysinfo_page, 15, 1, max_mnest) == 0)
148 break;
149 }
150 topology_max_mnest = max_mnest;
151 }
152
__do_early_pgm_check(struct pt_regs * regs)153 void __init __do_early_pgm_check(struct pt_regs *regs)
154 {
155 struct lowcore *lc = get_lowcore();
156 unsigned long ip;
157
158 regs->int_code = lc->pgm_int_code;
159 regs->int_parm_long = lc->trans_exc_code;
160 regs->last_break = lc->pgm_last_break;
161 ip = __rewind_psw(regs->psw, regs->int_code >> 16);
162
163 /* Monitor Event? Might be a warning */
164 if ((regs->int_code & PGM_INT_CODE_MASK) == 0x40) {
165 if (report_bug(ip, regs) == BUG_TRAP_TYPE_WARN)
166 return;
167 }
168 if (fixup_exception(regs))
169 return;
170 /*
171 * Unhandled exception - system cannot continue but try to get some
172 * helpful messages to the console. Use early_printk() to print
173 * some basic information in case it is too early for printk().
174 */
175 register_early_console();
176 early_printk("PANIC: early exception %04x PSW: %016lx %016lx\n",
177 regs->int_code & 0xffff, regs->psw.mask, regs->psw.addr);
178 show_regs(regs);
179 disabled_wait();
180 }
181
setup_lowcore_early(void)182 static noinline __init void setup_lowcore_early(void)
183 {
184 struct lowcore *lc = get_lowcore();
185 psw_t psw;
186
187 psw.addr = (unsigned long)early_pgm_check_handler;
188 psw.mask = PSW_KERNEL_BITS;
189 lc->program_new_psw = psw;
190 lc->preempt_count = INIT_PREEMPT_COUNT;
191 lc->return_lpswe = gen_lpswe(__LC_RETURN_PSW);
192 lc->return_mcck_lpswe = gen_lpswe(__LC_RETURN_MCCK_PSW);
193 }
194
save_vector_registers(void)195 static inline void save_vector_registers(void)
196 {
197 #ifdef CONFIG_CRASH_DUMP
198 if (cpu_has_vx())
199 save_vx_regs(boot_cpu_vector_save_area);
200 #endif
201 }
202
setup_low_address_protection(void)203 static inline void setup_low_address_protection(void)
204 {
205 system_ctl_set_bit(0, CR0_LOW_ADDRESS_PROTECTION_BIT);
206 }
207
setup_access_registers(void)208 static inline void setup_access_registers(void)
209 {
210 unsigned int acrs[NUM_ACRS] = { 0 };
211
212 restore_access_regs(acrs);
213 }
214
215 char __bootdata(early_command_line)[COMMAND_LINE_SIZE];
setup_boot_command_line(void)216 static void __init setup_boot_command_line(void)
217 {
218 /* copy arch command line */
219 strscpy(boot_command_line, early_command_line, COMMAND_LINE_SIZE);
220 }
221
sort_amode31_extable(void)222 static void __init sort_amode31_extable(void)
223 {
224 sort_extable(__start_amode31_ex_table, __stop_amode31_ex_table);
225 }
226
startup_init(void)227 void __init startup_init(void)
228 {
229 kasan_early_init();
230 time_early_init();
231 init_kernel_storage_key();
232 lockdep_off();
233 sort_amode31_extable();
234 setup_lowcore_early();
235 setup_arch_string();
236 setup_boot_command_line();
237 save_vector_registers();
238 setup_topology();
239 sclp_early_detect();
240 setup_low_address_protection();
241 setup_access_registers();
242 lockdep_on();
243 }
244