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/compiler.h> 12 #include <linux/init.h> 13 #include <linux/errno.h> 14 #include <linux/string.h> 15 #include <linux/ctype.h> 16 #include <linux/lockdep.h> 17 #include <linux/extable.h> 18 #include <linux/pfn.h> 19 #include <linux/uaccess.h> 20 #include <linux/kernel.h> 21 #include <asm/asm-extable.h> 22 #include <linux/memblock.h> 23 #include <asm/access-regs.h> 24 #include <asm/diag.h> 25 #include <asm/ebcdic.h> 26 #include <asm/fpu.h> 27 #include <asm/ipl.h> 28 #include <asm/lowcore.h> 29 #include <asm/processor.h> 30 #include <asm/sections.h> 31 #include <asm/setup.h> 32 #include <asm/sysinfo.h> 33 #include <asm/cpcmd.h> 34 #include <asm/sclp.h> 35 #include <asm/facility.h> 36 #include <asm/boot_data.h> 37 #include "entry.h" 38 39 #define decompressor_handled_param(param) \ 40 static int __init ignore_decompressor_param_##param(char *s) \ 41 { \ 42 return 0; \ 43 } \ 44 early_param(#param, ignore_decompressor_param_##param) 45 46 decompressor_handled_param(mem); 47 decompressor_handled_param(vmalloc); 48 decompressor_handled_param(dfltcc); 49 decompressor_handled_param(facilities); 50 decompressor_handled_param(nokaslr); 51 decompressor_handled_param(cmma); 52 decompressor_handled_param(relocate_lowcore); 53 decompressor_handled_param(bootdebug); 54 #if IS_ENABLED(CONFIG_KVM) 55 decompressor_handled_param(prot_virt); 56 #endif 57 58 static void __init kasan_early_init(void) 59 { 60 #ifdef CONFIG_KASAN 61 init_task.kasan_depth = 0; 62 pr_info("KernelAddressSanitizer initialized\n"); 63 #endif 64 } 65 66 static void __init reset_tod_clock(void) 67 { 68 union tod_clock clk; 69 70 if (store_tod_clock_ext_cc(&clk) == 0) 71 return; 72 /* TOD clock not running. Set the clock to Unix Epoch. */ 73 if (set_tod_clock(TOD_UNIX_EPOCH) || store_tod_clock_ext_cc(&clk)) 74 disabled_wait(); 75 76 memset(&tod_clock_base, 0, sizeof(tod_clock_base)); 77 tod_clock_base.tod = TOD_UNIX_EPOCH; 78 get_lowcore()->last_update_clock = TOD_UNIX_EPOCH; 79 } 80 81 /* 82 * Initialize storage key for kernel pages 83 */ 84 static noinline __init void init_kernel_storage_key(void) 85 { 86 #if PAGE_DEFAULT_KEY 87 unsigned long end_pfn, init_pfn; 88 89 end_pfn = PFN_UP(__pa(_end)); 90 91 for (init_pfn = 0 ; init_pfn < end_pfn; init_pfn++) 92 page_set_storage_key(init_pfn << PAGE_SHIFT, 93 PAGE_DEFAULT_KEY, 0); 94 #endif 95 } 96 97 static __initdata char sysinfo_page[PAGE_SIZE] __aligned(PAGE_SIZE); 98 99 static noinline __init void detect_machine_type(void) 100 { 101 struct sysinfo_3_2_2 *vmms = (struct sysinfo_3_2_2 *)&sysinfo_page; 102 103 /* Check current-configuration-level */ 104 if (stsi(NULL, 0, 0, 0) <= 2) { 105 get_lowcore()->machine_flags |= MACHINE_FLAG_LPAR; 106 return; 107 } 108 /* Get virtual-machine cpu information. */ 109 if (stsi(vmms, 3, 2, 2) || !vmms->count) 110 return; 111 112 /* Detect known hypervisors */ 113 if (!memcmp(vmms->vm[0].cpi, "\xd2\xe5\xd4", 3)) 114 get_lowcore()->machine_flags |= MACHINE_FLAG_KVM; 115 else if (!memcmp(vmms->vm[0].cpi, "\xa9\x61\xe5\xd4", 4)) 116 get_lowcore()->machine_flags |= MACHINE_FLAG_VM; 117 } 118 119 /* Remove leading, trailing and double whitespace. */ 120 static inline void strim_all(char *str) 121 { 122 char *s; 123 124 s = strim(str); 125 if (s != str) 126 memmove(str, s, strlen(s)); 127 while (*str) { 128 if (!isspace(*str++)) 129 continue; 130 if (isspace(*str)) { 131 s = skip_spaces(str); 132 memmove(str, s, strlen(s) + 1); 133 } 134 } 135 } 136 137 static noinline __init void setup_arch_string(void) 138 { 139 struct sysinfo_1_1_1 *mach = (struct sysinfo_1_1_1 *)&sysinfo_page; 140 struct sysinfo_3_2_2 *vm = (struct sysinfo_3_2_2 *)&sysinfo_page; 141 char mstr[80], hvstr[17]; 142 143 if (stsi(mach, 1, 1, 1)) 144 return; 145 EBCASC(mach->manufacturer, sizeof(mach->manufacturer)); 146 EBCASC(mach->type, sizeof(mach->type)); 147 EBCASC(mach->model, sizeof(mach->model)); 148 EBCASC(mach->model_capacity, sizeof(mach->model_capacity)); 149 sprintf(mstr, "%-16.16s %-4.4s %-16.16s %-16.16s", 150 mach->manufacturer, mach->type, 151 mach->model, mach->model_capacity); 152 strim_all(mstr); 153 if (stsi(vm, 3, 2, 2) == 0 && vm->count) { 154 EBCASC(vm->vm[0].cpi, sizeof(vm->vm[0].cpi)); 155 sprintf(hvstr, "%-16.16s", vm->vm[0].cpi); 156 strim_all(hvstr); 157 } else { 158 sprintf(hvstr, "%s", 159 MACHINE_IS_LPAR ? "LPAR" : 160 MACHINE_IS_VM ? "z/VM" : 161 MACHINE_IS_KVM ? "KVM" : "unknown"); 162 } 163 dump_stack_set_arch_desc("%s (%s)", mstr, hvstr); 164 } 165 166 static __init void setup_topology(void) 167 { 168 int max_mnest; 169 170 if (!test_facility(11)) 171 return; 172 get_lowcore()->machine_flags |= MACHINE_FLAG_TOPOLOGY; 173 for (max_mnest = 6; max_mnest > 1; max_mnest--) { 174 if (stsi(&sysinfo_page, 15, 1, max_mnest) == 0) 175 break; 176 } 177 topology_max_mnest = max_mnest; 178 } 179 180 void __init __do_early_pgm_check(struct pt_regs *regs) 181 { 182 struct lowcore *lc = get_lowcore(); 183 unsigned long ip; 184 185 regs->int_code = lc->pgm_int_code; 186 regs->int_parm_long = lc->trans_exc_code; 187 ip = __rewind_psw(regs->psw, regs->int_code >> 16); 188 189 /* Monitor Event? Might be a warning */ 190 if ((regs->int_code & PGM_INT_CODE_MASK) == 0x40) { 191 if (report_bug(ip, regs) == BUG_TRAP_TYPE_WARN) 192 return; 193 } 194 if (fixup_exception(regs)) 195 return; 196 /* 197 * Unhandled exception - system cannot continue but try to get some 198 * helpful messages to the console. Use early_printk() to print 199 * some basic information in case it is too early for printk(). 200 */ 201 register_early_console(); 202 early_printk("PANIC: early exception %04x PSW: %016lx %016lx\n", 203 regs->int_code & 0xffff, regs->psw.mask, regs->psw.addr); 204 show_regs(regs); 205 disabled_wait(); 206 } 207 208 static noinline __init void setup_lowcore_early(void) 209 { 210 struct lowcore *lc = get_lowcore(); 211 psw_t psw; 212 213 psw.addr = (unsigned long)early_pgm_check_handler; 214 psw.mask = PSW_KERNEL_BITS; 215 lc->program_new_psw = psw; 216 lc->preempt_count = INIT_PREEMPT_COUNT; 217 lc->return_lpswe = gen_lpswe(__LC_RETURN_PSW); 218 lc->return_mcck_lpswe = gen_lpswe(__LC_RETURN_MCCK_PSW); 219 } 220 221 static __init void detect_diag9c(void) 222 { 223 unsigned int cpu_address; 224 int rc; 225 226 cpu_address = stap(); 227 diag_stat_inc(DIAG_STAT_X09C); 228 asm volatile( 229 " diag %2,0,0x9c\n" 230 "0: la %0,0\n" 231 "1:\n" 232 EX_TABLE(0b,1b) 233 : "=d" (rc) : "0" (-EOPNOTSUPP), "d" (cpu_address) : "cc"); 234 if (!rc) 235 get_lowcore()->machine_flags |= MACHINE_FLAG_DIAG9C; 236 } 237 238 static __init void detect_machine_facilities(void) 239 { 240 if (test_facility(8)) { 241 get_lowcore()->machine_flags |= MACHINE_FLAG_EDAT1; 242 system_ctl_set_bit(0, CR0_EDAT_BIT); 243 } 244 if (test_facility(78)) 245 get_lowcore()->machine_flags |= MACHINE_FLAG_EDAT2; 246 if (test_facility(3)) 247 get_lowcore()->machine_flags |= MACHINE_FLAG_IDTE; 248 if (test_facility(50) && test_facility(73)) { 249 get_lowcore()->machine_flags |= MACHINE_FLAG_TE; 250 system_ctl_set_bit(0, CR0_TRANSACTIONAL_EXECUTION_BIT); 251 } 252 if (test_facility(51)) 253 get_lowcore()->machine_flags |= MACHINE_FLAG_TLB_LC; 254 if (test_facility(129)) 255 system_ctl_set_bit(0, CR0_VECTOR_BIT); 256 if (test_facility(130)) 257 get_lowcore()->machine_flags |= MACHINE_FLAG_NX; 258 if (test_facility(133)) 259 get_lowcore()->machine_flags |= MACHINE_FLAG_GS; 260 if (test_facility(139) && (tod_clock_base.tod >> 63)) { 261 /* Enabled signed clock comparator comparisons */ 262 get_lowcore()->machine_flags |= MACHINE_FLAG_SCC; 263 clock_comparator_max = -1ULL >> 1; 264 system_ctl_set_bit(0, CR0_CLOCK_COMPARATOR_SIGN_BIT); 265 } 266 if (IS_ENABLED(CONFIG_PCI) && test_facility(153)) { 267 get_lowcore()->machine_flags |= MACHINE_FLAG_PCI_MIO; 268 /* the control bit is set during PCI initialization */ 269 } 270 if (test_facility(194)) 271 get_lowcore()->machine_flags |= MACHINE_FLAG_RDP; 272 if (test_facility(85)) 273 get_lowcore()->machine_flags |= MACHINE_FLAG_SEQ_INSN; 274 } 275 276 static inline void save_vector_registers(void) 277 { 278 #ifdef CONFIG_CRASH_DUMP 279 if (test_facility(129)) 280 save_vx_regs(boot_cpu_vector_save_area); 281 #endif 282 } 283 284 static inline void setup_low_address_protection(void) 285 { 286 system_ctl_set_bit(0, CR0_LOW_ADDRESS_PROTECTION_BIT); 287 } 288 289 static inline void setup_access_registers(void) 290 { 291 unsigned int acrs[NUM_ACRS] = { 0 }; 292 293 restore_access_regs(acrs); 294 } 295 296 char __bootdata(early_command_line)[COMMAND_LINE_SIZE]; 297 static void __init setup_boot_command_line(void) 298 { 299 /* copy arch command line */ 300 strscpy(boot_command_line, early_command_line, COMMAND_LINE_SIZE); 301 } 302 303 static void __init sort_amode31_extable(void) 304 { 305 sort_extable(__start_amode31_ex_table, __stop_amode31_ex_table); 306 } 307 308 void __init startup_init(void) 309 { 310 kasan_early_init(); 311 reset_tod_clock(); 312 time_early_init(); 313 init_kernel_storage_key(); 314 lockdep_off(); 315 sort_amode31_extable(); 316 setup_lowcore_early(); 317 detect_machine_type(); 318 setup_arch_string(); 319 setup_boot_command_line(); 320 detect_diag9c(); 321 detect_machine_facilities(); 322 save_vector_registers(); 323 setup_topology(); 324 sclp_early_detect(); 325 setup_low_address_protection(); 326 setup_access_registers(); 327 lockdep_on(); 328 } 329