1 /* 2 * Copyright IBM Corp. 2007, 2009 3 * Author(s): Hongjie Yang <hongjie@us.ibm.com>, 4 * Heiko Carstens <heiko.carstens@de.ibm.com> 5 */ 6 7 #define KMSG_COMPONENT "setup" 8 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt 9 10 #include <linux/compiler.h> 11 #include <linux/init.h> 12 #include <linux/errno.h> 13 #include <linux/string.h> 14 #include <linux/ctype.h> 15 #include <linux/lockdep.h> 16 #include <linux/extable.h> 17 #include <linux/pfn.h> 18 #include <linux/uaccess.h> 19 #include <linux/kernel.h> 20 #include <asm/diag.h> 21 #include <asm/ebcdic.h> 22 #include <asm/ipl.h> 23 #include <asm/lowcore.h> 24 #include <asm/processor.h> 25 #include <asm/sections.h> 26 #include <asm/setup.h> 27 #include <asm/sysinfo.h> 28 #include <asm/cpcmd.h> 29 #include <asm/sclp.h> 30 #include <asm/facility.h> 31 #include "entry.h" 32 33 /* 34 * Create a Kernel NSS if the SAVESYS= parameter is defined 35 */ 36 #define DEFSYS_CMD_SIZE 128 37 #define SAVESYS_CMD_SIZE 32 38 39 char kernel_nss_name[NSS_NAME_SIZE + 1]; 40 41 static void __init setup_boot_command_line(void); 42 43 /* 44 * Get the TOD clock running. 45 */ 46 static void __init reset_tod_clock(void) 47 { 48 u64 time; 49 50 if (store_tod_clock(&time) == 0) 51 return; 52 /* TOD clock not running. Set the clock to Unix Epoch. */ 53 if (set_tod_clock(TOD_UNIX_EPOCH) != 0 || store_tod_clock(&time) != 0) 54 disabled_wait(0); 55 56 memset(tod_clock_base, 0, 16); 57 *(__u64 *) &tod_clock_base[1] = TOD_UNIX_EPOCH; 58 S390_lowcore.last_update_clock = TOD_UNIX_EPOCH; 59 } 60 61 #ifdef CONFIG_SHARED_KERNEL 62 int __init savesys_ipl_nss(char *cmd, const int cmdlen); 63 64 asm( 65 " .section .init.text,\"ax\",@progbits\n" 66 " .align 4\n" 67 " .type savesys_ipl_nss, @function\n" 68 "savesys_ipl_nss:\n" 69 " stmg 6,15,48(15)\n" 70 " lgr 14,3\n" 71 " sam31\n" 72 " diag 2,14,0x8\n" 73 " sam64\n" 74 " lgr 2,14\n" 75 " lmg 6,15,48(15)\n" 76 " br 14\n" 77 " .size savesys_ipl_nss, .-savesys_ipl_nss\n" 78 " .previous\n"); 79 80 static __initdata char upper_command_line[COMMAND_LINE_SIZE]; 81 82 static noinline __init void create_kernel_nss(void) 83 { 84 unsigned int i, stext_pfn, eshared_pfn, end_pfn, min_size; 85 #ifdef CONFIG_BLK_DEV_INITRD 86 unsigned int sinitrd_pfn, einitrd_pfn; 87 #endif 88 int response; 89 int hlen; 90 size_t len; 91 char *savesys_ptr; 92 char defsys_cmd[DEFSYS_CMD_SIZE]; 93 char savesys_cmd[SAVESYS_CMD_SIZE]; 94 95 /* Do nothing if we are not running under VM */ 96 if (!MACHINE_IS_VM) 97 return; 98 99 /* Convert COMMAND_LINE to upper case */ 100 for (i = 0; i < strlen(boot_command_line); i++) 101 upper_command_line[i] = toupper(boot_command_line[i]); 102 103 savesys_ptr = strstr(upper_command_line, "SAVESYS="); 104 105 if (!savesys_ptr) 106 return; 107 108 savesys_ptr += 8; /* Point to the beginning of the NSS name */ 109 for (i = 0; i < NSS_NAME_SIZE; i++) { 110 if (savesys_ptr[i] == ' ' || savesys_ptr[i] == '\0') 111 break; 112 kernel_nss_name[i] = savesys_ptr[i]; 113 } 114 115 stext_pfn = PFN_DOWN(__pa(&_stext)); 116 eshared_pfn = PFN_DOWN(__pa(&_eshared)); 117 end_pfn = PFN_UP(__pa(&_end)); 118 min_size = end_pfn << 2; 119 120 hlen = snprintf(defsys_cmd, DEFSYS_CMD_SIZE, 121 "DEFSYS %s 00000-%.5X EW %.5X-%.5X SR %.5X-%.5X", 122 kernel_nss_name, stext_pfn - 1, stext_pfn, 123 eshared_pfn - 1, eshared_pfn, end_pfn); 124 125 #ifdef CONFIG_BLK_DEV_INITRD 126 if (INITRD_START && INITRD_SIZE) { 127 sinitrd_pfn = PFN_DOWN(__pa(INITRD_START)); 128 einitrd_pfn = PFN_UP(__pa(INITRD_START + INITRD_SIZE)); 129 min_size = einitrd_pfn << 2; 130 hlen += snprintf(defsys_cmd + hlen, DEFSYS_CMD_SIZE - hlen, 131 " EW %.5X-%.5X", sinitrd_pfn, einitrd_pfn); 132 } 133 #endif 134 135 snprintf(defsys_cmd + hlen, DEFSYS_CMD_SIZE - hlen, 136 " EW MINSIZE=%.7iK PARMREGS=0-13", min_size); 137 defsys_cmd[DEFSYS_CMD_SIZE - 1] = '\0'; 138 snprintf(savesys_cmd, SAVESYS_CMD_SIZE, "SAVESYS %s \n IPL %s", 139 kernel_nss_name, kernel_nss_name); 140 savesys_cmd[SAVESYS_CMD_SIZE - 1] = '\0'; 141 142 __cpcmd(defsys_cmd, NULL, 0, &response); 143 144 if (response != 0) { 145 pr_err("Defining the Linux kernel NSS failed with rc=%d\n", 146 response); 147 kernel_nss_name[0] = '\0'; 148 return; 149 } 150 151 len = strlen(savesys_cmd); 152 ASCEBC(savesys_cmd, len); 153 response = savesys_ipl_nss(savesys_cmd, len); 154 155 /* On success: response is equal to the command size, 156 * max SAVESYS_CMD_SIZE 157 * On error: response contains the numeric portion of cp error message. 158 * for SAVESYS it will be >= 263 159 * for missing privilege class, it will be 1 160 */ 161 if (response > SAVESYS_CMD_SIZE || response == 1) { 162 pr_err("Saving the Linux kernel NSS failed with rc=%d\n", 163 response); 164 kernel_nss_name[0] = '\0'; 165 return; 166 } 167 168 /* re-initialize cputime accounting. */ 169 get_tod_clock_ext(tod_clock_base); 170 S390_lowcore.last_update_clock = *(__u64 *) &tod_clock_base[1]; 171 S390_lowcore.last_update_timer = 0x7fffffffffffffffULL; 172 S390_lowcore.user_timer = 0; 173 S390_lowcore.system_timer = 0; 174 asm volatile("SPT 0(%0)" : : "a" (&S390_lowcore.last_update_timer)); 175 176 /* re-setup boot command line with new ipl vm parms */ 177 ipl_update_parameters(); 178 setup_boot_command_line(); 179 180 ipl_flags = IPL_NSS_VALID; 181 } 182 183 #else /* CONFIG_SHARED_KERNEL */ 184 185 static inline void create_kernel_nss(void) { } 186 187 #endif /* CONFIG_SHARED_KERNEL */ 188 189 /* 190 * Clear bss memory 191 */ 192 static noinline __init void clear_bss_section(void) 193 { 194 memset(__bss_start, 0, __bss_stop - __bss_start); 195 } 196 197 /* 198 * Initialize storage key for kernel pages 199 */ 200 static noinline __init void init_kernel_storage_key(void) 201 { 202 #if PAGE_DEFAULT_KEY 203 unsigned long end_pfn, init_pfn; 204 205 end_pfn = PFN_UP(__pa(&_end)); 206 207 for (init_pfn = 0 ; init_pfn < end_pfn; init_pfn++) 208 page_set_storage_key(init_pfn << PAGE_SHIFT, 209 PAGE_DEFAULT_KEY, 0); 210 #endif 211 } 212 213 static __initdata char sysinfo_page[PAGE_SIZE] __aligned(PAGE_SIZE); 214 215 static noinline __init void detect_machine_type(void) 216 { 217 struct sysinfo_3_2_2 *vmms = (struct sysinfo_3_2_2 *)&sysinfo_page; 218 219 /* Check current-configuration-level */ 220 if (stsi(NULL, 0, 0, 0) <= 2) { 221 S390_lowcore.machine_flags |= MACHINE_FLAG_LPAR; 222 return; 223 } 224 /* Get virtual-machine cpu information. */ 225 if (stsi(vmms, 3, 2, 2) || !vmms->count) 226 return; 227 228 /* Running under KVM? If not we assume z/VM */ 229 if (!memcmp(vmms->vm[0].cpi, "\xd2\xe5\xd4", 3)) 230 S390_lowcore.machine_flags |= MACHINE_FLAG_KVM; 231 else 232 S390_lowcore.machine_flags |= MACHINE_FLAG_VM; 233 } 234 235 /* Remove leading, trailing and double whitespace. */ 236 static inline void strim_all(char *str) 237 { 238 char *s; 239 240 s = strim(str); 241 if (s != str) 242 memmove(str, s, strlen(s)); 243 while (*str) { 244 if (!isspace(*str++)) 245 continue; 246 if (isspace(*str)) { 247 s = skip_spaces(str); 248 memmove(str, s, strlen(s) + 1); 249 } 250 } 251 } 252 253 static noinline __init void setup_arch_string(void) 254 { 255 struct sysinfo_1_1_1 *mach = (struct sysinfo_1_1_1 *)&sysinfo_page; 256 struct sysinfo_3_2_2 *vm = (struct sysinfo_3_2_2 *)&sysinfo_page; 257 char mstr[80], hvstr[17]; 258 259 if (stsi(mach, 1, 1, 1)) 260 return; 261 EBCASC(mach->manufacturer, sizeof(mach->manufacturer)); 262 EBCASC(mach->type, sizeof(mach->type)); 263 EBCASC(mach->model, sizeof(mach->model)); 264 EBCASC(mach->model_capacity, sizeof(mach->model_capacity)); 265 sprintf(mstr, "%-16.16s %-4.4s %-16.16s %-16.16s", 266 mach->manufacturer, mach->type, 267 mach->model, mach->model_capacity); 268 strim_all(mstr); 269 if (stsi(vm, 3, 2, 2) == 0 && vm->count) { 270 EBCASC(vm->vm[0].cpi, sizeof(vm->vm[0].cpi)); 271 sprintf(hvstr, "%-16.16s", vm->vm[0].cpi); 272 strim_all(hvstr); 273 } else { 274 sprintf(hvstr, "%s", 275 MACHINE_IS_LPAR ? "LPAR" : 276 MACHINE_IS_VM ? "z/VM" : 277 MACHINE_IS_KVM ? "KVM" : "unknown"); 278 } 279 dump_stack_set_arch_desc("%s (%s)", mstr, hvstr); 280 } 281 282 static __init void setup_topology(void) 283 { 284 int max_mnest; 285 286 if (!test_facility(11)) 287 return; 288 S390_lowcore.machine_flags |= MACHINE_FLAG_TOPOLOGY; 289 for (max_mnest = 6; max_mnest > 1; max_mnest--) { 290 if (stsi(&sysinfo_page, 15, 1, max_mnest) == 0) 291 break; 292 } 293 topology_max_mnest = max_mnest; 294 } 295 296 static void early_pgm_check_handler(void) 297 { 298 const struct exception_table_entry *fixup; 299 unsigned long cr0, cr0_new; 300 unsigned long addr; 301 302 addr = S390_lowcore.program_old_psw.addr; 303 fixup = search_exception_tables(addr); 304 if (!fixup) 305 disabled_wait(0); 306 /* Disable low address protection before storing into lowcore. */ 307 __ctl_store(cr0, 0, 0); 308 cr0_new = cr0 & ~(1UL << 28); 309 __ctl_load(cr0_new, 0, 0); 310 S390_lowcore.program_old_psw.addr = extable_fixup(fixup); 311 __ctl_load(cr0, 0, 0); 312 } 313 314 static noinline __init void setup_lowcore_early(void) 315 { 316 psw_t psw; 317 318 psw.mask = PSW_MASK_BASE | PSW_DEFAULT_KEY | PSW_MASK_EA | PSW_MASK_BA; 319 psw.addr = (unsigned long) s390_base_ext_handler; 320 S390_lowcore.external_new_psw = psw; 321 psw.addr = (unsigned long) s390_base_pgm_handler; 322 S390_lowcore.program_new_psw = psw; 323 s390_base_pgm_handler_fn = early_pgm_check_handler; 324 S390_lowcore.preempt_count = INIT_PREEMPT_COUNT; 325 } 326 327 static noinline __init void setup_facility_list(void) 328 { 329 stfle(S390_lowcore.stfle_fac_list, 330 ARRAY_SIZE(S390_lowcore.stfle_fac_list)); 331 } 332 333 static __init void detect_diag9c(void) 334 { 335 unsigned int cpu_address; 336 int rc; 337 338 cpu_address = stap(); 339 diag_stat_inc(DIAG_STAT_X09C); 340 asm volatile( 341 " diag %2,0,0x9c\n" 342 "0: la %0,0\n" 343 "1:\n" 344 EX_TABLE(0b,1b) 345 : "=d" (rc) : "0" (-EOPNOTSUPP), "d" (cpu_address) : "cc"); 346 if (!rc) 347 S390_lowcore.machine_flags |= MACHINE_FLAG_DIAG9C; 348 } 349 350 static __init void detect_diag44(void) 351 { 352 int rc; 353 354 diag_stat_inc(DIAG_STAT_X044); 355 asm volatile( 356 " diag 0,0,0x44\n" 357 "0: la %0,0\n" 358 "1:\n" 359 EX_TABLE(0b,1b) 360 : "=d" (rc) : "0" (-EOPNOTSUPP) : "cc"); 361 if (!rc) 362 S390_lowcore.machine_flags |= MACHINE_FLAG_DIAG44; 363 } 364 365 static __init void detect_machine_facilities(void) 366 { 367 if (test_facility(8)) { 368 S390_lowcore.machine_flags |= MACHINE_FLAG_EDAT1; 369 __ctl_set_bit(0, 23); 370 } 371 if (test_facility(78)) 372 S390_lowcore.machine_flags |= MACHINE_FLAG_EDAT2; 373 if (test_facility(3)) 374 S390_lowcore.machine_flags |= MACHINE_FLAG_IDTE; 375 if (test_facility(40)) 376 S390_lowcore.machine_flags |= MACHINE_FLAG_LPP; 377 if (test_facility(50) && test_facility(73)) 378 S390_lowcore.machine_flags |= MACHINE_FLAG_TE; 379 if (test_facility(51)) 380 S390_lowcore.machine_flags |= MACHINE_FLAG_TLB_LC; 381 if (test_facility(129)) { 382 S390_lowcore.machine_flags |= MACHINE_FLAG_VX; 383 __ctl_set_bit(0, 17); 384 } 385 if (test_facility(130)) { 386 S390_lowcore.machine_flags |= MACHINE_FLAG_NX; 387 __ctl_set_bit(0, 20); 388 } 389 if (test_facility(133)) 390 S390_lowcore.machine_flags |= MACHINE_FLAG_GS; 391 if (test_facility(139) && (tod_clock_base[1] & 0x80)) { 392 /* Enabled signed clock comparator comparisons */ 393 S390_lowcore.machine_flags |= MACHINE_FLAG_SCC; 394 clock_comparator_max = -1ULL >> 1; 395 __ctl_set_bit(0, 53); 396 } 397 } 398 399 static inline void save_vector_registers(void) 400 { 401 #ifdef CONFIG_CRASH_DUMP 402 if (test_facility(129)) 403 save_vx_regs(boot_cpu_vector_save_area); 404 #endif 405 } 406 407 static int __init disable_vector_extension(char *str) 408 { 409 S390_lowcore.machine_flags &= ~MACHINE_FLAG_VX; 410 __ctl_clear_bit(0, 17); 411 return 0; 412 } 413 early_param("novx", disable_vector_extension); 414 415 static int __init noexec_setup(char *str) 416 { 417 bool enabled; 418 int rc; 419 420 rc = kstrtobool(str, &enabled); 421 if (!rc && !enabled) { 422 /* Disable no-execute support */ 423 S390_lowcore.machine_flags &= ~MACHINE_FLAG_NX; 424 __ctl_clear_bit(0, 20); 425 } 426 return rc; 427 } 428 early_param("noexec", noexec_setup); 429 430 static int __init cad_setup(char *str) 431 { 432 bool enabled; 433 int rc; 434 435 rc = kstrtobool(str, &enabled); 436 if (!rc && enabled && test_facility(128)) 437 /* Enable problem state CAD. */ 438 __ctl_set_bit(2, 3); 439 return rc; 440 } 441 early_param("cad", cad_setup); 442 443 static __init void memmove_early(void *dst, const void *src, size_t n) 444 { 445 unsigned long addr; 446 long incr; 447 psw_t old; 448 449 if (!n) 450 return; 451 incr = 1; 452 if (dst > src) { 453 incr = -incr; 454 dst += n - 1; 455 src += n - 1; 456 } 457 old = S390_lowcore.program_new_psw; 458 S390_lowcore.program_new_psw.mask = __extract_psw(); 459 asm volatile( 460 " larl %[addr],1f\n" 461 " stg %[addr],%[psw_pgm_addr]\n" 462 "0: mvc 0(1,%[dst]),0(%[src])\n" 463 " agr %[dst],%[incr]\n" 464 " agr %[src],%[incr]\n" 465 " brctg %[n],0b\n" 466 "1:\n" 467 : [addr] "=&d" (addr), 468 [psw_pgm_addr] "=Q" (S390_lowcore.program_new_psw.addr), 469 [dst] "+&a" (dst), [src] "+&a" (src), [n] "+d" (n) 470 : [incr] "d" (incr) 471 : "cc", "memory"); 472 S390_lowcore.program_new_psw = old; 473 } 474 475 static __init noinline void ipl_save_parameters(void) 476 { 477 void *src, *dst; 478 479 src = (void *)(unsigned long) S390_lowcore.ipl_parmblock_ptr; 480 dst = (void *) IPL_PARMBLOCK_ORIGIN; 481 memmove_early(dst, src, PAGE_SIZE); 482 S390_lowcore.ipl_parmblock_ptr = IPL_PARMBLOCK_ORIGIN; 483 } 484 485 static __init noinline void rescue_initrd(void) 486 { 487 #ifdef CONFIG_BLK_DEV_INITRD 488 unsigned long min_initrd_addr = (unsigned long) _end + (4UL << 20); 489 /* 490 * Just like in case of IPL from VM reader we make sure there is a 491 * gap of 4MB between end of kernel and start of initrd. 492 * That way we can also be sure that saving an NSS will succeed, 493 * which however only requires different segments. 494 */ 495 if (!INITRD_START || !INITRD_SIZE) 496 return; 497 if (INITRD_START >= min_initrd_addr) 498 return; 499 memmove_early((void *) min_initrd_addr, (void *) INITRD_START, INITRD_SIZE); 500 INITRD_START = min_initrd_addr; 501 #endif 502 } 503 504 /* Set up boot command line */ 505 static void __init append_to_cmdline(size_t (*ipl_data)(char *, size_t)) 506 { 507 char *parm, *delim; 508 size_t rc, len; 509 510 len = strlen(boot_command_line); 511 512 delim = boot_command_line + len; /* '\0' character position */ 513 parm = boot_command_line + len + 1; /* append right after '\0' */ 514 515 rc = ipl_data(parm, COMMAND_LINE_SIZE - len - 1); 516 if (rc) { 517 if (*parm == '=') 518 memmove(boot_command_line, parm + 1, rc); 519 else 520 *delim = ' '; /* replace '\0' with space */ 521 } 522 } 523 524 static inline int has_ebcdic_char(const char *str) 525 { 526 int i; 527 528 for (i = 0; str[i]; i++) 529 if (str[i] & 0x80) 530 return 1; 531 return 0; 532 } 533 534 static void __init setup_boot_command_line(void) 535 { 536 COMMAND_LINE[ARCH_COMMAND_LINE_SIZE - 1] = 0; 537 /* convert arch command line to ascii if necessary */ 538 if (has_ebcdic_char(COMMAND_LINE)) 539 EBCASC(COMMAND_LINE, ARCH_COMMAND_LINE_SIZE); 540 /* copy arch command line */ 541 strlcpy(boot_command_line, strstrip(COMMAND_LINE), 542 ARCH_COMMAND_LINE_SIZE); 543 544 /* append IPL PARM data to the boot command line */ 545 if (MACHINE_IS_VM) 546 append_to_cmdline(append_ipl_vmparm); 547 548 append_to_cmdline(append_ipl_scpdata); 549 } 550 551 /* 552 * Save ipl parameters, clear bss memory, initialize storage keys 553 * and create a kernel NSS at startup if the SAVESYS= parm is defined 554 */ 555 void __init startup_init(void) 556 { 557 reset_tod_clock(); 558 ipl_save_parameters(); 559 rescue_initrd(); 560 clear_bss_section(); 561 ipl_verify_parameters(); 562 time_early_init(); 563 init_kernel_storage_key(); 564 lockdep_off(); 565 setup_lowcore_early(); 566 setup_facility_list(); 567 detect_machine_type(); 568 setup_arch_string(); 569 ipl_update_parameters(); 570 setup_boot_command_line(); 571 create_kernel_nss(); 572 detect_diag9c(); 573 detect_diag44(); 574 detect_machine_facilities(); 575 save_vector_registers(); 576 setup_topology(); 577 sclp_early_detect(); 578 lockdep_on(); 579 } 580