1 // SPDX-License-Identifier: GPL-2.0 2 #include <linux/string.h> 3 #include <linux/elf.h> 4 #include <asm/page-states.h> 5 #include <asm/boot_data.h> 6 #include <asm/extmem.h> 7 #include <asm/sections.h> 8 #include <asm/maccess.h> 9 #include <asm/cpu_mf.h> 10 #include <asm/setup.h> 11 #include <asm/kasan.h> 12 #include <asm/kexec.h> 13 #include <asm/sclp.h> 14 #include <asm/diag.h> 15 #include <asm/uv.h> 16 #include <asm/abs_lowcore.h> 17 #include <asm/physmem_info.h> 18 #include "decompressor.h" 19 #include "boot.h" 20 #include "uv.h" 21 22 struct vm_layout __bootdata_preserved(vm_layout); 23 unsigned long __bootdata_preserved(__abs_lowcore); 24 unsigned long __bootdata_preserved(__memcpy_real_area); 25 pte_t *__bootdata_preserved(memcpy_real_ptep); 26 unsigned long __bootdata_preserved(VMALLOC_START); 27 unsigned long __bootdata_preserved(VMALLOC_END); 28 struct page *__bootdata_preserved(vmemmap); 29 unsigned long __bootdata_preserved(vmemmap_size); 30 unsigned long __bootdata_preserved(MODULES_VADDR); 31 unsigned long __bootdata_preserved(MODULES_END); 32 unsigned long __bootdata_preserved(max_mappable); 33 34 u64 __bootdata_preserved(stfle_fac_list[16]); 35 struct oldmem_data __bootdata_preserved(oldmem_data); 36 37 struct machine_info machine; 38 39 void error(char *x) 40 { 41 sclp_early_printk("\n\n"); 42 sclp_early_printk(x); 43 sclp_early_printk("\n\n -- System halted"); 44 45 disabled_wait(); 46 } 47 48 static void detect_facilities(void) 49 { 50 if (test_facility(8)) { 51 machine.has_edat1 = 1; 52 local_ctl_set_bit(0, CR0_EDAT_BIT); 53 } 54 if (test_facility(78)) 55 machine.has_edat2 = 1; 56 if (test_facility(130)) 57 machine.has_nx = 1; 58 } 59 60 static int cmma_test_essa(void) 61 { 62 unsigned long reg1, reg2, tmp = 0; 63 int rc = 1; 64 psw_t old; 65 66 /* Test ESSA_GET_STATE */ 67 asm volatile( 68 " mvc 0(16,%[psw_old]),0(%[psw_pgm])\n" 69 " epsw %[reg1],%[reg2]\n" 70 " st %[reg1],0(%[psw_pgm])\n" 71 " st %[reg2],4(%[psw_pgm])\n" 72 " larl %[reg1],1f\n" 73 " stg %[reg1],8(%[psw_pgm])\n" 74 " .insn rrf,0xb9ab0000,%[tmp],%[tmp],%[cmd],0\n" 75 " la %[rc],0\n" 76 "1: mvc 0(16,%[psw_pgm]),0(%[psw_old])\n" 77 : [reg1] "=&d" (reg1), 78 [reg2] "=&a" (reg2), 79 [rc] "+&d" (rc), 80 [tmp] "=&d" (tmp), 81 "+Q" (get_lowcore()->program_new_psw), 82 "=Q" (old) 83 : [psw_old] "a" (&old), 84 [psw_pgm] "a" (&get_lowcore()->program_new_psw), 85 [cmd] "i" (ESSA_GET_STATE) 86 : "cc", "memory"); 87 return rc; 88 } 89 90 static void cmma_init(void) 91 { 92 if (!cmma_flag) 93 return; 94 if (cmma_test_essa()) { 95 cmma_flag = 0; 96 return; 97 } 98 if (test_facility(147)) 99 cmma_flag = 2; 100 } 101 102 static void setup_lpp(void) 103 { 104 get_lowcore()->current_pid = 0; 105 get_lowcore()->lpp = LPP_MAGIC; 106 if (test_facility(40)) 107 lpp(&get_lowcore()->lpp); 108 } 109 110 #ifdef CONFIG_KERNEL_UNCOMPRESSED 111 static unsigned long mem_safe_offset(void) 112 { 113 return (unsigned long)_compressed_start; 114 } 115 116 static void deploy_kernel(void *output) 117 { 118 void *uncompressed_start = (void *)_compressed_start; 119 120 if (output == uncompressed_start) 121 return; 122 memmove(output, uncompressed_start, vmlinux.image_size); 123 memset(uncompressed_start, 0, vmlinux.image_size); 124 } 125 #endif 126 127 static void rescue_initrd(unsigned long min, unsigned long max) 128 { 129 unsigned long old_addr, addr, size; 130 131 if (!IS_ENABLED(CONFIG_BLK_DEV_INITRD)) 132 return; 133 if (!get_physmem_reserved(RR_INITRD, &addr, &size)) 134 return; 135 if (addr >= min && addr + size <= max) 136 return; 137 old_addr = addr; 138 physmem_free(RR_INITRD); 139 addr = physmem_alloc_top_down(RR_INITRD, size, 0); 140 memmove((void *)addr, (void *)old_addr, size); 141 } 142 143 static void copy_bootdata(void) 144 { 145 if (__boot_data_end - __boot_data_start != vmlinux.bootdata_size) 146 error(".boot.data section size mismatch"); 147 memcpy((void *)vmlinux.bootdata_off, __boot_data_start, vmlinux.bootdata_size); 148 if (__boot_data_preserved_end - __boot_data_preserved_start != vmlinux.bootdata_preserved_size) 149 error(".boot.preserved.data section size mismatch"); 150 memcpy((void *)vmlinux.bootdata_preserved_off, __boot_data_preserved_start, vmlinux.bootdata_preserved_size); 151 } 152 153 static void kaslr_adjust_relocs(unsigned long min_addr, unsigned long max_addr, 154 unsigned long offset, unsigned long phys_offset) 155 { 156 int *reloc; 157 long loc; 158 159 /* Adjust R_390_64 relocations */ 160 for (reloc = (int *)__vmlinux_relocs_64_start; reloc < (int *)__vmlinux_relocs_64_end; reloc++) { 161 loc = (long)*reloc + phys_offset; 162 if (loc < min_addr || loc > max_addr) 163 error("64-bit relocation outside of kernel!\n"); 164 *(u64 *)loc += offset - __START_KERNEL; 165 } 166 } 167 168 static void kaslr_adjust_got(unsigned long offset) 169 { 170 u64 *entry; 171 172 /* 173 * Adjust GOT entries, except for ones for undefined weak symbols 174 * that resolved to zero. This also skips the first three reserved 175 * entries on s390x that are zero. 176 */ 177 for (entry = (u64 *)vmlinux.got_start; entry < (u64 *)vmlinux.got_end; entry++) { 178 if (*entry) 179 *entry += offset - __START_KERNEL; 180 } 181 } 182 183 /* 184 * Merge information from several sources into a single ident_map_size value. 185 * "ident_map_size" represents the upper limit of physical memory we may ever 186 * reach. It might not be all online memory, but also include standby (offline) 187 * memory. "ident_map_size" could be lower then actual standby or even online 188 * memory present, due to limiting factors. We should never go above this limit. 189 * It is the size of our identity mapping. 190 * 191 * Consider the following factors: 192 * 1. max_physmem_end - end of physical memory online or standby. 193 * Always >= end of the last online memory range (get_physmem_online_end()). 194 * 2. CONFIG_MAX_PHYSMEM_BITS - the maximum size of physical memory the 195 * kernel is able to support. 196 * 3. "mem=" kernel command line option which limits physical memory usage. 197 * 4. OLDMEM_BASE which is a kdump memory limit when the kernel is executed as 198 * crash kernel. 199 * 5. "hsa" size which is a memory limit when the kernel is executed during 200 * zfcp/nvme dump. 201 */ 202 static void setup_ident_map_size(unsigned long max_physmem_end) 203 { 204 unsigned long hsa_size; 205 206 ident_map_size = max_physmem_end; 207 if (memory_limit) 208 ident_map_size = min(ident_map_size, memory_limit); 209 ident_map_size = min(ident_map_size, 1UL << MAX_PHYSMEM_BITS); 210 211 #ifdef CONFIG_CRASH_DUMP 212 if (oldmem_data.start) { 213 __kaslr_enabled = 0; 214 ident_map_size = min(ident_map_size, oldmem_data.size); 215 } else if (ipl_block_valid && is_ipl_block_dump()) { 216 __kaslr_enabled = 0; 217 if (!sclp_early_get_hsa_size(&hsa_size) && hsa_size) 218 ident_map_size = min(ident_map_size, hsa_size); 219 } 220 #endif 221 } 222 223 #define FIXMAP_SIZE round_up(MEMCPY_REAL_SIZE + ABS_LOWCORE_MAP_SIZE, sizeof(struct lowcore)) 224 225 static unsigned long get_vmem_size(unsigned long identity_size, 226 unsigned long vmemmap_size, 227 unsigned long vmalloc_size, 228 unsigned long rte_size) 229 { 230 unsigned long max_mappable, vsize; 231 232 max_mappable = max(identity_size, MAX_DCSS_ADDR); 233 vsize = round_up(SZ_2G + max_mappable, rte_size) + 234 round_up(vmemmap_size, rte_size) + 235 FIXMAP_SIZE + MODULES_LEN + KASLR_LEN; 236 return size_add(vsize, vmalloc_size); 237 } 238 239 static unsigned long setup_kernel_memory_layout(unsigned long kernel_size) 240 { 241 unsigned long vmemmap_start; 242 unsigned long kernel_start; 243 unsigned long asce_limit; 244 unsigned long rte_size; 245 unsigned long pages; 246 unsigned long vsize; 247 unsigned long vmax; 248 249 pages = ident_map_size / PAGE_SIZE; 250 /* vmemmap contains a multiple of PAGES_PER_SECTION struct pages */ 251 vmemmap_size = SECTION_ALIGN_UP(pages) * sizeof(struct page); 252 253 /* choose kernel address space layout: 4 or 3 levels. */ 254 BUILD_BUG_ON(!IS_ALIGNED(__START_KERNEL, THREAD_SIZE)); 255 BUILD_BUG_ON(!IS_ALIGNED(__NO_KASLR_START_KERNEL, THREAD_SIZE)); 256 BUILD_BUG_ON(__NO_KASLR_END_KERNEL > _REGION1_SIZE); 257 vsize = get_vmem_size(ident_map_size, vmemmap_size, vmalloc_size, _REGION3_SIZE); 258 if (IS_ENABLED(CONFIG_KASAN) || __NO_KASLR_END_KERNEL > _REGION2_SIZE || 259 (vsize > _REGION2_SIZE && kaslr_enabled())) { 260 asce_limit = _REGION1_SIZE; 261 if (__NO_KASLR_END_KERNEL > _REGION2_SIZE) { 262 rte_size = _REGION2_SIZE; 263 vsize = get_vmem_size(ident_map_size, vmemmap_size, vmalloc_size, _REGION2_SIZE); 264 } else { 265 rte_size = _REGION3_SIZE; 266 } 267 } else { 268 asce_limit = _REGION2_SIZE; 269 rte_size = _REGION3_SIZE; 270 } 271 272 /* 273 * Forcing modules and vmalloc area under the ultravisor 274 * secure storage limit, so that any vmalloc allocation 275 * we do could be used to back secure guest storage. 276 * 277 * Assume the secure storage limit always exceeds _REGION2_SIZE, 278 * otherwise asce_limit and rte_size would have been adjusted. 279 */ 280 vmax = adjust_to_uv_max(asce_limit); 281 #ifdef CONFIG_KASAN 282 BUILD_BUG_ON(__NO_KASLR_END_KERNEL > KASAN_SHADOW_START); 283 /* force vmalloc and modules below kasan shadow */ 284 vmax = min(vmax, KASAN_SHADOW_START); 285 #endif 286 vsize = min(vsize, vmax); 287 if (kaslr_enabled()) { 288 unsigned long kernel_end, kaslr_len, slots, pos; 289 290 kaslr_len = max(KASLR_LEN, vmax - vsize); 291 slots = DIV_ROUND_UP(kaslr_len - kernel_size, THREAD_SIZE); 292 if (get_random(slots, &pos)) 293 pos = 0; 294 kernel_end = vmax - pos * THREAD_SIZE; 295 kernel_start = round_down(kernel_end - kernel_size, THREAD_SIZE); 296 } else if (vmax < __NO_KASLR_END_KERNEL || vsize > __NO_KASLR_END_KERNEL) { 297 kernel_start = round_down(vmax - kernel_size, THREAD_SIZE); 298 decompressor_printk("The kernel base address is forced to %lx\n", kernel_start); 299 } else { 300 kernel_start = __NO_KASLR_START_KERNEL; 301 } 302 __kaslr_offset = kernel_start; 303 304 MODULES_END = round_down(kernel_start, _SEGMENT_SIZE); 305 MODULES_VADDR = MODULES_END - MODULES_LEN; 306 VMALLOC_END = MODULES_VADDR; 307 if (IS_ENABLED(CONFIG_KMSAN)) 308 VMALLOC_END -= MODULES_LEN * 2; 309 310 /* allow vmalloc area to occupy up to about 1/2 of the rest virtual space left */ 311 vsize = (VMALLOC_END - FIXMAP_SIZE) / 2; 312 vsize = round_down(vsize, _SEGMENT_SIZE); 313 vmalloc_size = min(vmalloc_size, vsize); 314 if (IS_ENABLED(CONFIG_KMSAN)) { 315 /* take 2/3 of vmalloc area for KMSAN shadow and origins */ 316 vmalloc_size = round_down(vmalloc_size / 3, _SEGMENT_SIZE); 317 VMALLOC_END -= vmalloc_size * 2; 318 } 319 VMALLOC_START = VMALLOC_END - vmalloc_size; 320 321 __memcpy_real_area = round_down(VMALLOC_START - MEMCPY_REAL_SIZE, PAGE_SIZE); 322 __abs_lowcore = round_down(__memcpy_real_area - ABS_LOWCORE_MAP_SIZE, 323 sizeof(struct lowcore)); 324 325 /* split remaining virtual space between 1:1 mapping & vmemmap array */ 326 pages = __abs_lowcore / (PAGE_SIZE + sizeof(struct page)); 327 pages = SECTION_ALIGN_UP(pages); 328 /* keep vmemmap_start aligned to a top level region table entry */ 329 vmemmap_start = round_down(__abs_lowcore - pages * sizeof(struct page), rte_size); 330 /* make sure identity map doesn't overlay with vmemmap */ 331 ident_map_size = min(ident_map_size, vmemmap_start); 332 vmemmap_size = SECTION_ALIGN_UP(ident_map_size / PAGE_SIZE) * sizeof(struct page); 333 /* make sure vmemmap doesn't overlay with absolute lowcore area */ 334 if (vmemmap_start + vmemmap_size > __abs_lowcore) { 335 vmemmap_size = SECTION_ALIGN_DOWN(ident_map_size / PAGE_SIZE) * sizeof(struct page); 336 ident_map_size = vmemmap_size / sizeof(struct page) * PAGE_SIZE; 337 } 338 vmemmap = (struct page *)vmemmap_start; 339 /* maximum address for which linear mapping could be created (DCSS, memory) */ 340 BUILD_BUG_ON(MAX_DCSS_ADDR > (1UL << MAX_PHYSMEM_BITS)); 341 max_mappable = max(ident_map_size, MAX_DCSS_ADDR); 342 max_mappable = min(max_mappable, vmemmap_start); 343 __identity_base = round_down(vmemmap_start - max_mappable, rte_size); 344 345 return asce_limit; 346 } 347 348 /* 349 * This function clears the BSS section of the decompressed Linux kernel and NOT the decompressor's. 350 */ 351 static void clear_bss_section(unsigned long kernel_start) 352 { 353 memset((void *)kernel_start + vmlinux.image_size, 0, vmlinux.bss_size); 354 } 355 356 /* 357 * Set vmalloc area size to an 8th of (potential) physical memory 358 * size, unless size has been set by kernel command line parameter. 359 */ 360 static void setup_vmalloc_size(void) 361 { 362 unsigned long size; 363 364 if (vmalloc_size_set) 365 return; 366 size = round_up(ident_map_size / 8, _SEGMENT_SIZE); 367 vmalloc_size = max(size, vmalloc_size); 368 } 369 370 static void kaslr_adjust_vmlinux_info(long offset) 371 { 372 vmlinux.bootdata_off += offset; 373 vmlinux.bootdata_preserved_off += offset; 374 vmlinux.got_start += offset; 375 vmlinux.got_end += offset; 376 vmlinux.init_mm_off += offset; 377 vmlinux.swapper_pg_dir_off += offset; 378 vmlinux.invalid_pg_dir_off += offset; 379 #ifdef CONFIG_KASAN 380 vmlinux.kasan_early_shadow_page_off += offset; 381 vmlinux.kasan_early_shadow_pte_off += offset; 382 vmlinux.kasan_early_shadow_pmd_off += offset; 383 vmlinux.kasan_early_shadow_pud_off += offset; 384 vmlinux.kasan_early_shadow_p4d_off += offset; 385 #endif 386 } 387 388 static void fixup_vmlinux_info(void) 389 { 390 vmlinux.entry -= __START_KERNEL; 391 kaslr_adjust_vmlinux_info(-__START_KERNEL); 392 } 393 394 void startup_kernel(void) 395 { 396 unsigned long kernel_size = vmlinux.image_size + vmlinux.bss_size; 397 unsigned long nokaslr_offset_phys, kaslr_large_page_offset; 398 unsigned long amode31_lma = 0; 399 unsigned long max_physmem_end; 400 unsigned long asce_limit; 401 unsigned long safe_addr; 402 psw_t psw; 403 404 fixup_vmlinux_info(); 405 setup_lpp(); 406 407 /* 408 * Non-randomized kernel physical start address must be _SEGMENT_SIZE 409 * aligned (see blow). 410 */ 411 nokaslr_offset_phys = ALIGN(mem_safe_offset(), _SEGMENT_SIZE); 412 safe_addr = PAGE_ALIGN(nokaslr_offset_phys + kernel_size); 413 414 /* 415 * Reserve decompressor memory together with decompression heap, 416 * buffer and memory which might be occupied by uncompressed kernel 417 * (if KASLR is off or failed). 418 */ 419 physmem_reserve(RR_DECOMPRESSOR, 0, safe_addr); 420 if (IS_ENABLED(CONFIG_BLK_DEV_INITRD) && parmarea.initrd_size) 421 physmem_reserve(RR_INITRD, parmarea.initrd_start, parmarea.initrd_size); 422 oldmem_data.start = parmarea.oldmem_base; 423 oldmem_data.size = parmarea.oldmem_size; 424 425 store_ipl_parmblock(); 426 read_ipl_report(); 427 uv_query_info(); 428 sclp_early_read_info(); 429 setup_boot_command_line(); 430 parse_boot_command_line(); 431 detect_facilities(); 432 cmma_init(); 433 sanitize_prot_virt_host(); 434 max_physmem_end = detect_max_physmem_end(); 435 setup_ident_map_size(max_physmem_end); 436 setup_vmalloc_size(); 437 asce_limit = setup_kernel_memory_layout(kernel_size); 438 /* got final ident_map_size, physmem allocations could be performed now */ 439 physmem_set_usable_limit(ident_map_size); 440 detect_physmem_online_ranges(max_physmem_end); 441 save_ipl_cert_comp_list(); 442 rescue_initrd(safe_addr, ident_map_size); 443 444 /* 445 * __kaslr_offset_phys must be _SEGMENT_SIZE aligned, so the lower 446 * 20 bits (the offset within a large page) are zero. Copy the last 447 * 20 bits of __kaslr_offset, which is THREAD_SIZE aligned, to 448 * __kaslr_offset_phys. 449 * 450 * With this the last 20 bits of __kaslr_offset_phys and __kaslr_offset 451 * are identical, which is required to allow for large mappings of the 452 * kernel image. 453 */ 454 kaslr_large_page_offset = __kaslr_offset & ~_SEGMENT_MASK; 455 if (kaslr_enabled()) { 456 unsigned long end = ident_map_size - kaslr_large_page_offset; 457 458 __kaslr_offset_phys = randomize_within_range(kernel_size, _SEGMENT_SIZE, 0, end); 459 } 460 if (!__kaslr_offset_phys) 461 __kaslr_offset_phys = nokaslr_offset_phys; 462 __kaslr_offset_phys |= kaslr_large_page_offset; 463 kaslr_adjust_vmlinux_info(__kaslr_offset_phys); 464 physmem_reserve(RR_VMLINUX, __kaslr_offset_phys, kernel_size); 465 deploy_kernel((void *)__kaslr_offset_phys); 466 467 /* vmlinux decompression is done, shrink reserved low memory */ 468 physmem_reserve(RR_DECOMPRESSOR, 0, (unsigned long)_decompressor_end); 469 470 /* 471 * In case KASLR is enabled the randomized location of .amode31 472 * section might overlap with .vmlinux.relocs section. To avoid that 473 * the below randomize_within_range() could have been called with 474 * __vmlinux_relocs_64_end as the lower range address. However, 475 * .amode31 section is written to by the decompressed kernel - at 476 * that time the contents of .vmlinux.relocs is not needed anymore. 477 * Conversly, .vmlinux.relocs is read only by the decompressor, even 478 * before the kernel started. Therefore, in case the two sections 479 * overlap there is no risk of corrupting any data. 480 */ 481 if (kaslr_enabled()) 482 amode31_lma = randomize_within_range(vmlinux.amode31_size, PAGE_SIZE, 0, SZ_2G); 483 if (!amode31_lma) 484 amode31_lma = __kaslr_offset_phys - vmlinux.amode31_size; 485 physmem_reserve(RR_AMODE31, amode31_lma, vmlinux.amode31_size); 486 487 /* 488 * The order of the following operations is important: 489 * 490 * - kaslr_adjust_relocs() must follow clear_bss_section() to establish 491 * static memory references to data in .bss to be used by setup_vmem() 492 * (i.e init_mm.pgd) 493 * 494 * - setup_vmem() must follow kaslr_adjust_relocs() to be able using 495 * static memory references to data in .bss (i.e init_mm.pgd) 496 * 497 * - copy_bootdata() must follow setup_vmem() to propagate changes 498 * to bootdata made by setup_vmem() 499 */ 500 clear_bss_section(__kaslr_offset_phys); 501 kaslr_adjust_relocs(__kaslr_offset_phys, __kaslr_offset_phys + vmlinux.image_size, 502 __kaslr_offset, __kaslr_offset_phys); 503 kaslr_adjust_got(__kaslr_offset); 504 setup_vmem(__kaslr_offset, __kaslr_offset + kernel_size, asce_limit); 505 copy_bootdata(); 506 507 /* 508 * Save KASLR offset for early dumps, before vmcore_info is set. 509 * Mark as uneven to distinguish from real vmcore_info pointer. 510 */ 511 get_lowcore()->vmcore_info = __kaslr_offset_phys ? __kaslr_offset_phys | 0x1UL : 0; 512 513 /* 514 * Jump to the decompressed kernel entry point and switch DAT mode on. 515 */ 516 psw.addr = __kaslr_offset + vmlinux.entry; 517 psw.mask = PSW_KERNEL_BITS; 518 __load_psw(psw); 519 } 520