startup.c 22 KB

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