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
setup_arch_string(void)108 static noinline __init void setup_arch_string(void)
109 {
110 struct sysinfo_1_1_1 *mach = (struct sysinfo_1_1_1 *)&sysinfo_page;
111 struct sysinfo_3_2_2 *vm = (struct sysinfo_3_2_2 *)&sysinfo_page;
112 char mstr[80], hvstr[17];
113
114 if (stsi(mach, 1, 1, 1))
115 return;
116 EBCASC(mach->manufacturer, sizeof(mach->manufacturer));
117 EBCASC(mach->type, sizeof(mach->type));
118 EBCASC(mach->model, sizeof(mach->model));
119 EBCASC(mach->model_capacity, sizeof(mach->model_capacity));
120 sprintf(mstr, "%-16.16s %-4.4s %-16.16s %-16.16s",
121 mach->manufacturer, mach->type,
122 mach->model, mach->model_capacity);
123 strim_all(mstr);
124 if (stsi(vm, 3, 2, 2) == 0 && vm->count) {
125 EBCASC(vm->vm[0].cpi, sizeof(vm->vm[0].cpi));
126 sprintf(hvstr, "%-16.16s", vm->vm[0].cpi);
127 strim_all(hvstr);
128 } else {
129 sprintf(hvstr, "%s",
130 machine_is_lpar() ? "LPAR" :
131 machine_is_vm() ? "z/VM" :
132 machine_is_kvm() ? "KVM" : "unknown");
133 }
134 dump_stack_set_arch_desc("%s (%s)", mstr, hvstr);
135 }
136
setup_topology(void)137 static __init void setup_topology(void)
138 {
139 int max_mnest;
140
141 if (!cpu_has_topology())
142 return;
143 for (max_mnest = 6; max_mnest > 1; max_mnest--) {
144 if (stsi(&sysinfo_page, 15, 1, max_mnest) == 0)
145 break;
146 }
147 topology_max_mnest = max_mnest;
148 }
149
__do_early_pgm_check(struct pt_regs * regs)150 void __init __do_early_pgm_check(struct pt_regs *regs)
151 {
152 struct lowcore *lc = get_lowcore();
153 unsigned long ip;
154
155 regs->int_code = lc->pgm_int_code;
156 regs->int_parm_long = lc->trans_exc_code;
157 ip = __rewind_psw(regs->psw, regs->int_code >> 16);
158
159 /* Monitor Event? Might be a warning */
160 if ((regs->int_code & PGM_INT_CODE_MASK) == 0x40) {
161 if (report_bug(ip, regs) == BUG_TRAP_TYPE_WARN)
162 return;
163 }
164 if (fixup_exception(regs))
165 return;
166 /*
167 * Unhandled exception - system cannot continue but try to get some
168 * helpful messages to the console. Use early_printk() to print
169 * some basic information in case it is too early for printk().
170 */
171 register_early_console();
172 early_printk("PANIC: early exception %04x PSW: %016lx %016lx\n",
173 regs->int_code & 0xffff, regs->psw.mask, regs->psw.addr);
174 show_regs(regs);
175 disabled_wait();
176 }
177
setup_lowcore_early(void)178 static noinline __init void setup_lowcore_early(void)
179 {
180 struct lowcore *lc = get_lowcore();
181 psw_t psw;
182
183 psw.addr = (unsigned long)early_pgm_check_handler;
184 psw.mask = PSW_KERNEL_BITS;
185 lc->program_new_psw = psw;
186 lc->preempt_count = INIT_PREEMPT_COUNT;
187 lc->return_lpswe = gen_lpswe(__LC_RETURN_PSW);
188 lc->return_mcck_lpswe = gen_lpswe(__LC_RETURN_MCCK_PSW);
189 }
190
save_vector_registers(void)191 static inline void save_vector_registers(void)
192 {
193 #ifdef CONFIG_CRASH_DUMP
194 if (cpu_has_vx())
195 save_vx_regs(boot_cpu_vector_save_area);
196 #endif
197 }
198
setup_low_address_protection(void)199 static inline void setup_low_address_protection(void)
200 {
201 system_ctl_set_bit(0, CR0_LOW_ADDRESS_PROTECTION_BIT);
202 }
203
setup_access_registers(void)204 static inline void setup_access_registers(void)
205 {
206 unsigned int acrs[NUM_ACRS] = { 0 };
207
208 restore_access_regs(acrs);
209 }
210
211 char __bootdata(early_command_line)[COMMAND_LINE_SIZE];
setup_boot_command_line(void)212 static void __init setup_boot_command_line(void)
213 {
214 /* copy arch command line */
215 strscpy(boot_command_line, early_command_line, COMMAND_LINE_SIZE);
216 }
217
sort_amode31_extable(void)218 static void __init sort_amode31_extable(void)
219 {
220 sort_extable(__start_amode31_ex_table, __stop_amode31_ex_table);
221 }
222
startup_init(void)223 void __init startup_init(void)
224 {
225 kasan_early_init();
226 time_early_init();
227 init_kernel_storage_key();
228 lockdep_off();
229 sort_amode31_extable();
230 setup_lowcore_early();
231 setup_arch_string();
232 setup_boot_command_line();
233 save_vector_registers();
234 setup_topology();
235 sclp_early_detect();
236 setup_low_address_protection();
237 setup_access_registers();
238 lockdep_on();
239 }
240