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