setup.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * S390 version
  4. * Copyright IBM Corp. 1999, 2012
  5. * Author(s): Hartmut Penner ([email protected]),
  6. * Martin Schwidefsky ([email protected])
  7. *
  8. * Derived from "arch/i386/kernel/setup.c"
  9. * Copyright (C) 1995, Linus Torvalds
  10. */
  11. /*
  12. * This file handles the architecture-dependent parts of initialization
  13. */
  14. #define KMSG_COMPONENT "setup"
  15. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  16. #include <linux/errno.h>
  17. #include <linux/export.h>
  18. #include <linux/sched.h>
  19. #include <linux/sched/task.h>
  20. #include <linux/cpu.h>
  21. #include <linux/kernel.h>
  22. #include <linux/memblock.h>
  23. #include <linux/mm.h>
  24. #include <linux/stddef.h>
  25. #include <linux/unistd.h>
  26. #include <linux/ptrace.h>
  27. #include <linux/random.h>
  28. #include <linux/user.h>
  29. #include <linux/tty.h>
  30. #include <linux/ioport.h>
  31. #include <linux/delay.h>
  32. #include <linux/init.h>
  33. #include <linux/initrd.h>
  34. #include <linux/root_dev.h>
  35. #include <linux/console.h>
  36. #include <linux/kernel_stat.h>
  37. #include <linux/dma-map-ops.h>
  38. #include <linux/device.h>
  39. #include <linux/notifier.h>
  40. #include <linux/pfn.h>
  41. #include <linux/ctype.h>
  42. #include <linux/reboot.h>
  43. #include <linux/topology.h>
  44. #include <linux/kexec.h>
  45. #include <linux/crash_dump.h>
  46. #include <linux/memory.h>
  47. #include <linux/compat.h>
  48. #include <linux/start_kernel.h>
  49. #include <linux/hugetlb.h>
  50. #include <linux/kmemleak.h>
  51. #include <asm/boot_data.h>
  52. #include <asm/ipl.h>
  53. #include <asm/facility.h>
  54. #include <asm/smp.h>
  55. #include <asm/mmu_context.h>
  56. #include <asm/cpcmd.h>
  57. #include <asm/abs_lowcore.h>
  58. #include <asm/nmi.h>
  59. #include <asm/irq.h>
  60. #include <asm/page.h>
  61. #include <asm/ptrace.h>
  62. #include <asm/sections.h>
  63. #include <asm/ebcdic.h>
  64. #include <asm/diag.h>
  65. #include <asm/os_info.h>
  66. #include <asm/sclp.h>
  67. #include <asm/stacktrace.h>
  68. #include <asm/sysinfo.h>
  69. #include <asm/numa.h>
  70. #include <asm/alternative.h>
  71. #include <asm/nospec-branch.h>
  72. #include <asm/mem_detect.h>
  73. #include <asm/maccess.h>
  74. #include <asm/uv.h>
  75. #include <asm/asm-offsets.h>
  76. #include "entry.h"
  77. /*
  78. * Machine setup..
  79. */
  80. unsigned int console_mode = 0;
  81. EXPORT_SYMBOL(console_mode);
  82. unsigned int console_devno = -1;
  83. EXPORT_SYMBOL(console_devno);
  84. unsigned int console_irq = -1;
  85. EXPORT_SYMBOL(console_irq);
  86. /*
  87. * Some code and data needs to stay below 2 GB, even when the kernel would be
  88. * relocated above 2 GB, because it has to use 31 bit addresses.
  89. * Such code and data is part of the .amode31 section.
  90. */
  91. unsigned long __amode31_ref __samode31 = (unsigned long)&_samode31;
  92. unsigned long __amode31_ref __eamode31 = (unsigned long)&_eamode31;
  93. unsigned long __amode31_ref __stext_amode31 = (unsigned long)&_stext_amode31;
  94. unsigned long __amode31_ref __etext_amode31 = (unsigned long)&_etext_amode31;
  95. struct exception_table_entry __amode31_ref *__start_amode31_ex_table = _start_amode31_ex_table;
  96. struct exception_table_entry __amode31_ref *__stop_amode31_ex_table = _stop_amode31_ex_table;
  97. /*
  98. * Control registers CR2, CR5 and CR15 are initialized with addresses
  99. * of tables that must be placed below 2G which is handled by the AMODE31
  100. * sections.
  101. * Because the AMODE31 sections are relocated below 2G at startup,
  102. * the content of control registers CR2, CR5 and CR15 must be updated
  103. * with new addresses after the relocation. The initial initialization of
  104. * control registers occurs in head64.S and then gets updated again after AMODE31
  105. * relocation. We must access the relevant AMODE31 tables indirectly via
  106. * pointers placed in the .amode31.refs linker section. Those pointers get
  107. * updated automatically during AMODE31 relocation and always contain a valid
  108. * address within AMODE31 sections.
  109. */
  110. static __amode31_data u32 __ctl_duct_amode31[16] __aligned(64);
  111. static __amode31_data u64 __ctl_aste_amode31[8] __aligned(64) = {
  112. [1] = 0xffffffffffffffff
  113. };
  114. static __amode31_data u32 __ctl_duald_amode31[32] __aligned(128) = {
  115. 0x80000000, 0, 0, 0,
  116. 0x80000000, 0, 0, 0,
  117. 0x80000000, 0, 0, 0,
  118. 0x80000000, 0, 0, 0,
  119. 0x80000000, 0, 0, 0,
  120. 0x80000000, 0, 0, 0,
  121. 0x80000000, 0, 0, 0,
  122. 0x80000000, 0, 0, 0
  123. };
  124. static __amode31_data u32 __ctl_linkage_stack_amode31[8] __aligned(64) = {
  125. 0, 0, 0x89000000, 0,
  126. 0, 0, 0x8a000000, 0
  127. };
  128. static u64 __amode31_ref *__ctl_aste = __ctl_aste_amode31;
  129. static u32 __amode31_ref *__ctl_duald = __ctl_duald_amode31;
  130. static u32 __amode31_ref *__ctl_linkage_stack = __ctl_linkage_stack_amode31;
  131. static u32 __amode31_ref *__ctl_duct = __ctl_duct_amode31;
  132. int __bootdata(noexec_disabled);
  133. unsigned long __bootdata(ident_map_size);
  134. struct mem_detect_info __bootdata(mem_detect);
  135. struct initrd_data __bootdata(initrd_data);
  136. unsigned long __bootdata_preserved(__kaslr_offset);
  137. unsigned long __bootdata(__amode31_base);
  138. unsigned int __bootdata_preserved(zlib_dfltcc_support);
  139. EXPORT_SYMBOL(zlib_dfltcc_support);
  140. u64 __bootdata_preserved(stfle_fac_list[16]);
  141. EXPORT_SYMBOL(stfle_fac_list);
  142. u64 __bootdata_preserved(alt_stfle_fac_list[16]);
  143. struct oldmem_data __bootdata_preserved(oldmem_data);
  144. unsigned long VMALLOC_START;
  145. EXPORT_SYMBOL(VMALLOC_START);
  146. unsigned long VMALLOC_END;
  147. EXPORT_SYMBOL(VMALLOC_END);
  148. struct page *vmemmap;
  149. EXPORT_SYMBOL(vmemmap);
  150. unsigned long vmemmap_size;
  151. unsigned long MODULES_VADDR;
  152. unsigned long MODULES_END;
  153. /* An array with a pointer to the lowcore of every CPU. */
  154. struct lowcore *lowcore_ptr[NR_CPUS];
  155. EXPORT_SYMBOL(lowcore_ptr);
  156. DEFINE_STATIC_KEY_FALSE(cpu_has_bear);
  157. /*
  158. * The Write Back bit position in the physaddr is given by the SLPC PCI.
  159. * Leaving the mask zero always uses write through which is safe
  160. */
  161. unsigned long mio_wb_bit_mask __ro_after_init;
  162. /*
  163. * This is set up by the setup-routine at boot-time
  164. * for S390 need to find out, what we have to setup
  165. * using address 0x10400 ...
  166. */
  167. #include <asm/setup.h>
  168. /*
  169. * condev= and conmode= setup parameter.
  170. */
  171. static int __init condev_setup(char *str)
  172. {
  173. int vdev;
  174. vdev = simple_strtoul(str, &str, 0);
  175. if (vdev >= 0 && vdev < 65536) {
  176. console_devno = vdev;
  177. console_irq = -1;
  178. }
  179. return 1;
  180. }
  181. __setup("condev=", condev_setup);
  182. static void __init set_preferred_console(void)
  183. {
  184. if (CONSOLE_IS_3215 || CONSOLE_IS_SCLP)
  185. add_preferred_console("ttyS", 0, NULL);
  186. else if (CONSOLE_IS_3270)
  187. add_preferred_console("tty3270", 0, NULL);
  188. else if (CONSOLE_IS_VT220)
  189. add_preferred_console("ttysclp", 0, NULL);
  190. else if (CONSOLE_IS_HVC)
  191. add_preferred_console("hvc", 0, NULL);
  192. }
  193. static int __init conmode_setup(char *str)
  194. {
  195. #if defined(CONFIG_SCLP_CONSOLE) || defined(CONFIG_SCLP_VT220_CONSOLE)
  196. if (!strcmp(str, "hwc") || !strcmp(str, "sclp"))
  197. SET_CONSOLE_SCLP;
  198. #endif
  199. #if defined(CONFIG_TN3215_CONSOLE)
  200. if (!strcmp(str, "3215"))
  201. SET_CONSOLE_3215;
  202. #endif
  203. #if defined(CONFIG_TN3270_CONSOLE)
  204. if (!strcmp(str, "3270"))
  205. SET_CONSOLE_3270;
  206. #endif
  207. set_preferred_console();
  208. return 1;
  209. }
  210. __setup("conmode=", conmode_setup);
  211. static void __init conmode_default(void)
  212. {
  213. char query_buffer[1024];
  214. char *ptr;
  215. if (MACHINE_IS_VM) {
  216. cpcmd("QUERY CONSOLE", query_buffer, 1024, NULL);
  217. console_devno = simple_strtoul(query_buffer + 5, NULL, 16);
  218. ptr = strstr(query_buffer, "SUBCHANNEL =");
  219. console_irq = simple_strtoul(ptr + 13, NULL, 16);
  220. cpcmd("QUERY TERM", query_buffer, 1024, NULL);
  221. ptr = strstr(query_buffer, "CONMODE");
  222. /*
  223. * Set the conmode to 3215 so that the device recognition
  224. * will set the cu_type of the console to 3215. If the
  225. * conmode is 3270 and we don't set it back then both
  226. * 3215 and the 3270 driver will try to access the console
  227. * device (3215 as console and 3270 as normal tty).
  228. */
  229. cpcmd("TERM CONMODE 3215", NULL, 0, NULL);
  230. if (ptr == NULL) {
  231. #if defined(CONFIG_SCLP_CONSOLE) || defined(CONFIG_SCLP_VT220_CONSOLE)
  232. SET_CONSOLE_SCLP;
  233. #endif
  234. return;
  235. }
  236. if (str_has_prefix(ptr + 8, "3270")) {
  237. #if defined(CONFIG_TN3270_CONSOLE)
  238. SET_CONSOLE_3270;
  239. #elif defined(CONFIG_TN3215_CONSOLE)
  240. SET_CONSOLE_3215;
  241. #elif defined(CONFIG_SCLP_CONSOLE) || defined(CONFIG_SCLP_VT220_CONSOLE)
  242. SET_CONSOLE_SCLP;
  243. #endif
  244. } else if (str_has_prefix(ptr + 8, "3215")) {
  245. #if defined(CONFIG_TN3215_CONSOLE)
  246. SET_CONSOLE_3215;
  247. #elif defined(CONFIG_TN3270_CONSOLE)
  248. SET_CONSOLE_3270;
  249. #elif defined(CONFIG_SCLP_CONSOLE) || defined(CONFIG_SCLP_VT220_CONSOLE)
  250. SET_CONSOLE_SCLP;
  251. #endif
  252. }
  253. } else if (MACHINE_IS_KVM) {
  254. if (sclp.has_vt220 && IS_ENABLED(CONFIG_SCLP_VT220_CONSOLE))
  255. SET_CONSOLE_VT220;
  256. else if (sclp.has_linemode && IS_ENABLED(CONFIG_SCLP_CONSOLE))
  257. SET_CONSOLE_SCLP;
  258. else
  259. SET_CONSOLE_HVC;
  260. } else {
  261. #if defined(CONFIG_SCLP_CONSOLE) || defined(CONFIG_SCLP_VT220_CONSOLE)
  262. SET_CONSOLE_SCLP;
  263. #endif
  264. }
  265. }
  266. #ifdef CONFIG_CRASH_DUMP
  267. static void __init setup_zfcpdump(void)
  268. {
  269. if (!is_ipl_type_dump())
  270. return;
  271. if (oldmem_data.start)
  272. return;
  273. strcat(boot_command_line, " cio_ignore=all,!ipldev,!condev");
  274. console_loglevel = 2;
  275. }
  276. #else
  277. static inline void setup_zfcpdump(void) {}
  278. #endif /* CONFIG_CRASH_DUMP */
  279. /*
  280. * Reboot, halt and power_off stubs. They just call _machine_restart,
  281. * _machine_halt or _machine_power_off.
  282. */
  283. void machine_restart(char *command)
  284. {
  285. if ((!in_interrupt() && !in_atomic()) || oops_in_progress)
  286. /*
  287. * Only unblank the console if we are called in enabled
  288. * context or a bust_spinlocks cleared the way for us.
  289. */
  290. console_unblank();
  291. _machine_restart(command);
  292. }
  293. void machine_halt(void)
  294. {
  295. if (!in_interrupt() || oops_in_progress)
  296. /*
  297. * Only unblank the console if we are called in enabled
  298. * context or a bust_spinlocks cleared the way for us.
  299. */
  300. console_unblank();
  301. _machine_halt();
  302. }
  303. void machine_power_off(void)
  304. {
  305. if (!in_interrupt() || oops_in_progress)
  306. /*
  307. * Only unblank the console if we are called in enabled
  308. * context or a bust_spinlocks cleared the way for us.
  309. */
  310. console_unblank();
  311. _machine_power_off();
  312. }
  313. /*
  314. * Dummy power off function.
  315. */
  316. void (*pm_power_off)(void) = machine_power_off;
  317. EXPORT_SYMBOL_GPL(pm_power_off);
  318. void *restart_stack;
  319. unsigned long stack_alloc(void)
  320. {
  321. #ifdef CONFIG_VMAP_STACK
  322. void *ret;
  323. ret = __vmalloc_node(THREAD_SIZE, THREAD_SIZE, THREADINFO_GFP,
  324. NUMA_NO_NODE, __builtin_return_address(0));
  325. kmemleak_not_leak(ret);
  326. return (unsigned long)ret;
  327. #else
  328. return __get_free_pages(GFP_KERNEL, THREAD_SIZE_ORDER);
  329. #endif
  330. }
  331. void stack_free(unsigned long stack)
  332. {
  333. #ifdef CONFIG_VMAP_STACK
  334. vfree((void *) stack);
  335. #else
  336. free_pages(stack, THREAD_SIZE_ORDER);
  337. #endif
  338. }
  339. int __init arch_early_irq_init(void)
  340. {
  341. unsigned long stack;
  342. stack = __get_free_pages(GFP_KERNEL, THREAD_SIZE_ORDER);
  343. if (!stack)
  344. panic("Couldn't allocate async stack");
  345. S390_lowcore.async_stack = stack + STACK_INIT_OFFSET;
  346. return 0;
  347. }
  348. void __init arch_call_rest_init(void)
  349. {
  350. unsigned long stack;
  351. smp_reinit_ipl_cpu();
  352. stack = stack_alloc();
  353. if (!stack)
  354. panic("Couldn't allocate kernel stack");
  355. current->stack = (void *) stack;
  356. #ifdef CONFIG_VMAP_STACK
  357. current->stack_vm_area = (void *) stack;
  358. #endif
  359. set_task_stack_end_magic(current);
  360. stack += STACK_INIT_OFFSET;
  361. S390_lowcore.kernel_stack = stack;
  362. call_on_stack_noreturn(rest_init, stack);
  363. }
  364. static void __init setup_lowcore_dat_off(void)
  365. {
  366. unsigned long int_psw_mask = PSW_KERNEL_BITS;
  367. struct lowcore *abs_lc, *lc;
  368. unsigned long mcck_stack;
  369. unsigned long flags;
  370. if (IS_ENABLED(CONFIG_KASAN))
  371. int_psw_mask |= PSW_MASK_DAT;
  372. /*
  373. * Setup lowcore for boot cpu
  374. */
  375. BUILD_BUG_ON(sizeof(struct lowcore) != LC_PAGES * PAGE_SIZE);
  376. lc = memblock_alloc_low(sizeof(*lc), sizeof(*lc));
  377. if (!lc)
  378. panic("%s: Failed to allocate %zu bytes align=%zx\n",
  379. __func__, sizeof(*lc), sizeof(*lc));
  380. lc->restart_psw.mask = PSW_KERNEL_BITS;
  381. lc->restart_psw.addr = (unsigned long) restart_int_handler;
  382. lc->external_new_psw.mask = int_psw_mask | PSW_MASK_MCHECK;
  383. lc->external_new_psw.addr = (unsigned long) ext_int_handler;
  384. lc->svc_new_psw.mask = int_psw_mask | PSW_MASK_MCHECK;
  385. lc->svc_new_psw.addr = (unsigned long) system_call;
  386. lc->program_new_psw.mask = int_psw_mask | PSW_MASK_MCHECK;
  387. lc->program_new_psw.addr = (unsigned long) pgm_check_handler;
  388. lc->mcck_new_psw.mask = PSW_KERNEL_BITS;
  389. lc->mcck_new_psw.addr = (unsigned long) mcck_int_handler;
  390. lc->io_new_psw.mask = int_psw_mask | PSW_MASK_MCHECK;
  391. lc->io_new_psw.addr = (unsigned long) io_int_handler;
  392. lc->clock_comparator = clock_comparator_max;
  393. lc->nodat_stack = ((unsigned long) &init_thread_union)
  394. + THREAD_SIZE - STACK_FRAME_OVERHEAD - sizeof(struct pt_regs);
  395. lc->current_task = (unsigned long)&init_task;
  396. lc->lpp = LPP_MAGIC;
  397. lc->machine_flags = S390_lowcore.machine_flags;
  398. lc->preempt_count = S390_lowcore.preempt_count;
  399. nmi_alloc_mcesa_early(&lc->mcesad);
  400. lc->sys_enter_timer = S390_lowcore.sys_enter_timer;
  401. lc->exit_timer = S390_lowcore.exit_timer;
  402. lc->user_timer = S390_lowcore.user_timer;
  403. lc->system_timer = S390_lowcore.system_timer;
  404. lc->steal_timer = S390_lowcore.steal_timer;
  405. lc->last_update_timer = S390_lowcore.last_update_timer;
  406. lc->last_update_clock = S390_lowcore.last_update_clock;
  407. /*
  408. * Allocate the global restart stack which is the same for
  409. * all CPUs in cast *one* of them does a PSW restart.
  410. */
  411. restart_stack = memblock_alloc(THREAD_SIZE, THREAD_SIZE);
  412. if (!restart_stack)
  413. panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
  414. __func__, THREAD_SIZE, THREAD_SIZE);
  415. restart_stack += STACK_INIT_OFFSET;
  416. /*
  417. * Set up PSW restart to call ipl.c:do_restart(). Copy the relevant
  418. * restart data to the absolute zero lowcore. This is necessary if
  419. * PSW restart is done on an offline CPU that has lowcore zero.
  420. */
  421. lc->restart_stack = (unsigned long) restart_stack;
  422. lc->restart_fn = (unsigned long) do_restart;
  423. lc->restart_data = 0;
  424. lc->restart_source = -1U;
  425. abs_lc = get_abs_lowcore(&flags);
  426. abs_lc->restart_stack = lc->restart_stack;
  427. abs_lc->restart_fn = lc->restart_fn;
  428. abs_lc->restart_data = lc->restart_data;
  429. abs_lc->restart_source = lc->restart_source;
  430. abs_lc->restart_psw = lc->restart_psw;
  431. abs_lc->mcesad = lc->mcesad;
  432. put_abs_lowcore(abs_lc, flags);
  433. mcck_stack = (unsigned long)memblock_alloc(THREAD_SIZE, THREAD_SIZE);
  434. if (!mcck_stack)
  435. panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
  436. __func__, THREAD_SIZE, THREAD_SIZE);
  437. lc->mcck_stack = mcck_stack + STACK_INIT_OFFSET;
  438. lc->spinlock_lockval = arch_spin_lockval(0);
  439. lc->spinlock_index = 0;
  440. arch_spin_lock_setup(0);
  441. lc->return_lpswe = gen_lpswe(__LC_RETURN_PSW);
  442. lc->return_mcck_lpswe = gen_lpswe(__LC_RETURN_MCCK_PSW);
  443. lc->preempt_count = PREEMPT_DISABLED;
  444. set_prefix(__pa(lc));
  445. lowcore_ptr[0] = lc;
  446. }
  447. static void __init setup_lowcore_dat_on(void)
  448. {
  449. struct lowcore *abs_lc;
  450. unsigned long flags;
  451. int i;
  452. __ctl_clear_bit(0, 28);
  453. S390_lowcore.external_new_psw.mask |= PSW_MASK_DAT;
  454. S390_lowcore.svc_new_psw.mask |= PSW_MASK_DAT;
  455. S390_lowcore.program_new_psw.mask |= PSW_MASK_DAT;
  456. S390_lowcore.io_new_psw.mask |= PSW_MASK_DAT;
  457. __ctl_set_bit(0, 28);
  458. __ctl_store(S390_lowcore.cregs_save_area, 0, 15);
  459. if (abs_lowcore_map(0, lowcore_ptr[0], true))
  460. panic("Couldn't setup absolute lowcore");
  461. abs_lowcore_mapped = true;
  462. abs_lc = get_abs_lowcore(&flags);
  463. abs_lc->restart_flags = RESTART_FLAG_CTLREGS;
  464. abs_lc->program_new_psw = S390_lowcore.program_new_psw;
  465. for (i = 0; i < 16; i++)
  466. abs_lc->cregs_save_area[i] = S390_lowcore.cregs_save_area[i];
  467. put_abs_lowcore(abs_lc, flags);
  468. }
  469. static struct resource code_resource = {
  470. .name = "Kernel code",
  471. .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM,
  472. };
  473. static struct resource data_resource = {
  474. .name = "Kernel data",
  475. .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM,
  476. };
  477. static struct resource bss_resource = {
  478. .name = "Kernel bss",
  479. .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM,
  480. };
  481. static struct resource __initdata *standard_resources[] = {
  482. &code_resource,
  483. &data_resource,
  484. &bss_resource,
  485. };
  486. static void __init setup_resources(void)
  487. {
  488. struct resource *res, *std_res, *sub_res;
  489. phys_addr_t start, end;
  490. int j;
  491. u64 i;
  492. code_resource.start = (unsigned long) _text;
  493. code_resource.end = (unsigned long) _etext - 1;
  494. data_resource.start = (unsigned long) _etext;
  495. data_resource.end = (unsigned long) _edata - 1;
  496. bss_resource.start = (unsigned long) __bss_start;
  497. bss_resource.end = (unsigned long) __bss_stop - 1;
  498. for_each_mem_range(i, &start, &end) {
  499. res = memblock_alloc(sizeof(*res), 8);
  500. if (!res)
  501. panic("%s: Failed to allocate %zu bytes align=0x%x\n",
  502. __func__, sizeof(*res), 8);
  503. res->flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM;
  504. res->name = "System RAM";
  505. res->start = start;
  506. /*
  507. * In memblock, end points to the first byte after the
  508. * range while in resourses, end points to the last byte in
  509. * the range.
  510. */
  511. res->end = end - 1;
  512. request_resource(&iomem_resource, res);
  513. for (j = 0; j < ARRAY_SIZE(standard_resources); j++) {
  514. std_res = standard_resources[j];
  515. if (std_res->start < res->start ||
  516. std_res->start > res->end)
  517. continue;
  518. if (std_res->end > res->end) {
  519. sub_res = memblock_alloc(sizeof(*sub_res), 8);
  520. if (!sub_res)
  521. panic("%s: Failed to allocate %zu bytes align=0x%x\n",
  522. __func__, sizeof(*sub_res), 8);
  523. *sub_res = *std_res;
  524. sub_res->end = res->end;
  525. std_res->start = res->end + 1;
  526. request_resource(res, sub_res);
  527. } else {
  528. request_resource(res, std_res);
  529. }
  530. }
  531. }
  532. #ifdef CONFIG_CRASH_DUMP
  533. /*
  534. * Re-add removed crash kernel memory as reserved memory. This makes
  535. * sure it will be mapped with the identity mapping and struct pages
  536. * will be created, so it can be resized later on.
  537. * However add it later since the crash kernel resource should not be
  538. * part of the System RAM resource.
  539. */
  540. if (crashk_res.end) {
  541. memblock_add_node(crashk_res.start, resource_size(&crashk_res),
  542. 0, MEMBLOCK_NONE);
  543. memblock_reserve(crashk_res.start, resource_size(&crashk_res));
  544. insert_resource(&iomem_resource, &crashk_res);
  545. }
  546. #endif
  547. }
  548. static void __init setup_memory_end(void)
  549. {
  550. memblock_remove(ident_map_size, PHYS_ADDR_MAX - ident_map_size);
  551. max_pfn = max_low_pfn = PFN_DOWN(ident_map_size);
  552. pr_notice("The maximum memory size is %luMB\n", ident_map_size >> 20);
  553. }
  554. #ifdef CONFIG_CRASH_DUMP
  555. /*
  556. * When kdump is enabled, we have to ensure that no memory from the area
  557. * [0 - crashkernel memory size] is set offline - it will be exchanged with
  558. * the crashkernel memory region when kdump is triggered. The crashkernel
  559. * memory region can never get offlined (pages are unmovable).
  560. */
  561. static int kdump_mem_notifier(struct notifier_block *nb,
  562. unsigned long action, void *data)
  563. {
  564. struct memory_notify *arg = data;
  565. if (action != MEM_GOING_OFFLINE)
  566. return NOTIFY_OK;
  567. if (arg->start_pfn < PFN_DOWN(resource_size(&crashk_res)))
  568. return NOTIFY_BAD;
  569. return NOTIFY_OK;
  570. }
  571. static struct notifier_block kdump_mem_nb = {
  572. .notifier_call = kdump_mem_notifier,
  573. };
  574. #endif
  575. /*
  576. * Reserve memory for kdump kernel to be loaded with kexec
  577. */
  578. static void __init reserve_crashkernel(void)
  579. {
  580. #ifdef CONFIG_CRASH_DUMP
  581. unsigned long long crash_base, crash_size;
  582. phys_addr_t low, high;
  583. int rc;
  584. rc = parse_crashkernel(boot_command_line, ident_map_size, &crash_size,
  585. &crash_base);
  586. crash_base = ALIGN(crash_base, KEXEC_CRASH_MEM_ALIGN);
  587. crash_size = ALIGN(crash_size, KEXEC_CRASH_MEM_ALIGN);
  588. if (rc || crash_size == 0)
  589. return;
  590. if (memblock.memory.regions[0].size < crash_size) {
  591. pr_info("crashkernel reservation failed: %s\n",
  592. "first memory chunk must be at least crashkernel size");
  593. return;
  594. }
  595. low = crash_base ?: oldmem_data.start;
  596. high = low + crash_size;
  597. if (low >= oldmem_data.start && high <= oldmem_data.start + oldmem_data.size) {
  598. /* The crashkernel fits into OLDMEM, reuse OLDMEM */
  599. crash_base = low;
  600. } else {
  601. /* Find suitable area in free memory */
  602. low = max_t(unsigned long, crash_size, sclp.hsa_size);
  603. high = crash_base ? crash_base + crash_size : ULONG_MAX;
  604. if (crash_base && crash_base < low) {
  605. pr_info("crashkernel reservation failed: %s\n",
  606. "crash_base too low");
  607. return;
  608. }
  609. low = crash_base ?: low;
  610. crash_base = memblock_phys_alloc_range(crash_size,
  611. KEXEC_CRASH_MEM_ALIGN,
  612. low, high);
  613. }
  614. if (!crash_base) {
  615. pr_info("crashkernel reservation failed: %s\n",
  616. "no suitable area found");
  617. return;
  618. }
  619. if (register_memory_notifier(&kdump_mem_nb)) {
  620. memblock_phys_free(crash_base, crash_size);
  621. return;
  622. }
  623. if (!oldmem_data.start && MACHINE_IS_VM)
  624. diag10_range(PFN_DOWN(crash_base), PFN_DOWN(crash_size));
  625. crashk_res.start = crash_base;
  626. crashk_res.end = crash_base + crash_size - 1;
  627. memblock_remove(crash_base, crash_size);
  628. pr_info("Reserving %lluMB of memory at %lluMB "
  629. "for crashkernel (System RAM: %luMB)\n",
  630. crash_size >> 20, crash_base >> 20,
  631. (unsigned long)memblock.memory.total_size >> 20);
  632. os_info_crashkernel_add(crash_base, crash_size);
  633. #endif
  634. }
  635. /*
  636. * Reserve the initrd from being used by memblock
  637. */
  638. static void __init reserve_initrd(void)
  639. {
  640. #ifdef CONFIG_BLK_DEV_INITRD
  641. if (!initrd_data.start || !initrd_data.size)
  642. return;
  643. initrd_start = (unsigned long)__va(initrd_data.start);
  644. initrd_end = initrd_start + initrd_data.size;
  645. memblock_reserve(initrd_data.start, initrd_data.size);
  646. #endif
  647. }
  648. /*
  649. * Reserve the memory area used to pass the certificate lists
  650. */
  651. static void __init reserve_certificate_list(void)
  652. {
  653. if (ipl_cert_list_addr)
  654. memblock_reserve(ipl_cert_list_addr, ipl_cert_list_size);
  655. }
  656. static void __init reserve_mem_detect_info(void)
  657. {
  658. unsigned long start, size;
  659. get_mem_detect_reserved(&start, &size);
  660. if (size)
  661. memblock_reserve(start, size);
  662. }
  663. static void __init free_mem_detect_info(void)
  664. {
  665. unsigned long start, size;
  666. get_mem_detect_reserved(&start, &size);
  667. if (size)
  668. memblock_phys_free(start, size);
  669. }
  670. static const char * __init get_mem_info_source(void)
  671. {
  672. switch (mem_detect.info_source) {
  673. case MEM_DETECT_SCLP_STOR_INFO:
  674. return "sclp storage info";
  675. case MEM_DETECT_DIAG260:
  676. return "diag260";
  677. case MEM_DETECT_SCLP_READ_INFO:
  678. return "sclp read info";
  679. case MEM_DETECT_BIN_SEARCH:
  680. return "binary search";
  681. }
  682. return "none";
  683. }
  684. static void __init memblock_add_mem_detect_info(void)
  685. {
  686. unsigned long start, end;
  687. int i;
  688. pr_debug("physmem info source: %s (%hhd)\n",
  689. get_mem_info_source(), mem_detect.info_source);
  690. /* keep memblock lists close to the kernel */
  691. memblock_set_bottom_up(true);
  692. for_each_mem_detect_block(i, &start, &end) {
  693. memblock_add(start, end - start);
  694. memblock_physmem_add(start, end - start);
  695. }
  696. memblock_set_bottom_up(false);
  697. memblock_set_node(0, ULONG_MAX, &memblock.memory, 0);
  698. }
  699. /*
  700. * Check for initrd being in usable memory
  701. */
  702. static void __init check_initrd(void)
  703. {
  704. #ifdef CONFIG_BLK_DEV_INITRD
  705. if (initrd_data.start && initrd_data.size &&
  706. !memblock_is_region_memory(initrd_data.start, initrd_data.size)) {
  707. pr_err("The initial RAM disk does not fit into the memory\n");
  708. memblock_phys_free(initrd_data.start, initrd_data.size);
  709. initrd_start = initrd_end = 0;
  710. }
  711. #endif
  712. }
  713. /*
  714. * Reserve memory used for lowcore/command line/kernel image.
  715. */
  716. static void __init reserve_kernel(void)
  717. {
  718. memblock_reserve(0, STARTUP_NORMAL_OFFSET);
  719. memblock_reserve(OLDMEM_BASE, sizeof(unsigned long));
  720. memblock_reserve(OLDMEM_SIZE, sizeof(unsigned long));
  721. memblock_reserve(__amode31_base, __eamode31 - __samode31);
  722. memblock_reserve(__pa(sclp_early_sccb), EXT_SCCB_READ_SCP);
  723. memblock_reserve(__pa(_stext), _end - _stext);
  724. }
  725. static void __init setup_memory(void)
  726. {
  727. phys_addr_t start, end;
  728. u64 i;
  729. /*
  730. * Init storage key for present memory
  731. */
  732. for_each_mem_range(i, &start, &end)
  733. storage_key_init_range(start, end);
  734. psw_set_key(PAGE_DEFAULT_KEY);
  735. }
  736. static void __init relocate_amode31_section(void)
  737. {
  738. unsigned long amode31_size = __eamode31 - __samode31;
  739. long amode31_offset = __amode31_base - __samode31;
  740. long *ptr;
  741. pr_info("Relocating AMODE31 section of size 0x%08lx\n", amode31_size);
  742. /* Move original AMODE31 section to the new one */
  743. memmove((void *)__amode31_base, (void *)__samode31, amode31_size);
  744. /* Zero out the old AMODE31 section to catch invalid accesses within it */
  745. memset((void *)__samode31, 0, amode31_size);
  746. /* Update all AMODE31 region references */
  747. for (ptr = _start_amode31_refs; ptr != _end_amode31_refs; ptr++)
  748. *ptr += amode31_offset;
  749. }
  750. /* This must be called after AMODE31 relocation */
  751. static void __init setup_cr(void)
  752. {
  753. union ctlreg2 cr2;
  754. union ctlreg5 cr5;
  755. union ctlreg15 cr15;
  756. __ctl_duct[1] = (unsigned long)__ctl_aste;
  757. __ctl_duct[2] = (unsigned long)__ctl_aste;
  758. __ctl_duct[4] = (unsigned long)__ctl_duald;
  759. /* Update control registers CR2, CR5 and CR15 */
  760. __ctl_store(cr2.val, 2, 2);
  761. __ctl_store(cr5.val, 5, 5);
  762. __ctl_store(cr15.val, 15, 15);
  763. cr2.ducto = (unsigned long)__ctl_duct >> 6;
  764. cr5.pasteo = (unsigned long)__ctl_duct >> 6;
  765. cr15.lsea = (unsigned long)__ctl_linkage_stack >> 3;
  766. __ctl_load(cr2.val, 2, 2);
  767. __ctl_load(cr5.val, 5, 5);
  768. __ctl_load(cr15.val, 15, 15);
  769. }
  770. /*
  771. * Add system information as device randomness
  772. */
  773. static void __init setup_randomness(void)
  774. {
  775. struct sysinfo_3_2_2 *vmms;
  776. vmms = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
  777. if (!vmms)
  778. panic("Failed to allocate memory for sysinfo structure\n");
  779. if (stsi(vmms, 3, 2, 2) == 0 && vmms->count)
  780. add_device_randomness(&vmms->vm, sizeof(vmms->vm[0]) * vmms->count);
  781. memblock_free(vmms, PAGE_SIZE);
  782. if (cpacf_query_func(CPACF_PRNO, CPACF_PRNO_TRNG))
  783. static_branch_enable(&s390_arch_random_available);
  784. }
  785. /*
  786. * Find the correct size for the task_struct. This depends on
  787. * the size of the struct fpu at the end of the thread_struct
  788. * which is embedded in the task_struct.
  789. */
  790. static void __init setup_task_size(void)
  791. {
  792. int task_size = sizeof(struct task_struct);
  793. if (!MACHINE_HAS_VX) {
  794. task_size -= sizeof(__vector128) * __NUM_VXRS;
  795. task_size += sizeof(freg_t) * __NUM_FPRS;
  796. }
  797. arch_task_struct_size = task_size;
  798. }
  799. /*
  800. * Issue diagnose 318 to set the control program name and
  801. * version codes.
  802. */
  803. static void __init setup_control_program_code(void)
  804. {
  805. union diag318_info diag318_info = {
  806. .cpnc = CPNC_LINUX,
  807. .cpvc = 0,
  808. };
  809. if (!sclp.has_diag318)
  810. return;
  811. diag_stat_inc(DIAG_STAT_X318);
  812. asm volatile("diag %0,0,0x318\n" : : "d" (diag318_info.val));
  813. }
  814. /*
  815. * Print the component list from the IPL report
  816. */
  817. static void __init log_component_list(void)
  818. {
  819. struct ipl_rb_component_entry *ptr, *end;
  820. char *str;
  821. if (!early_ipl_comp_list_addr)
  822. return;
  823. if (ipl_block.hdr.flags & IPL_PL_FLAG_SIPL)
  824. pr_info("Linux is running with Secure-IPL enabled\n");
  825. else
  826. pr_info("Linux is running with Secure-IPL disabled\n");
  827. ptr = (void *) early_ipl_comp_list_addr;
  828. end = (void *) ptr + early_ipl_comp_list_size;
  829. pr_info("The IPL report contains the following components:\n");
  830. while (ptr < end) {
  831. if (ptr->flags & IPL_RB_COMPONENT_FLAG_SIGNED) {
  832. if (ptr->flags & IPL_RB_COMPONENT_FLAG_VERIFIED)
  833. str = "signed, verified";
  834. else
  835. str = "signed, verification failed";
  836. } else {
  837. str = "not signed";
  838. }
  839. pr_info("%016llx - %016llx (%s)\n",
  840. ptr->addr, ptr->addr + ptr->len, str);
  841. ptr++;
  842. }
  843. }
  844. /*
  845. * Setup function called from init/main.c just after the banner
  846. * was printed.
  847. */
  848. void __init setup_arch(char **cmdline_p)
  849. {
  850. /*
  851. * print what head.S has found out about the machine
  852. */
  853. if (MACHINE_IS_VM)
  854. pr_info("Linux is running as a z/VM "
  855. "guest operating system in 64-bit mode\n");
  856. else if (MACHINE_IS_KVM)
  857. pr_info("Linux is running under KVM in 64-bit mode\n");
  858. else if (MACHINE_IS_LPAR)
  859. pr_info("Linux is running natively in 64-bit mode\n");
  860. else
  861. pr_info("Linux is running as a guest in 64-bit mode\n");
  862. log_component_list();
  863. /* Have one command line that is parsed and saved in /proc/cmdline */
  864. /* boot_command_line has been already set up in early.c */
  865. *cmdline_p = boot_command_line;
  866. ROOT_DEV = Root_RAM0;
  867. setup_initial_init_mm(_text, _etext, _edata, _end);
  868. if (IS_ENABLED(CONFIG_EXPOLINE_AUTO))
  869. nospec_auto_detect();
  870. jump_label_init();
  871. parse_early_param();
  872. #ifdef CONFIG_CRASH_DUMP
  873. /* Deactivate elfcorehdr= kernel parameter */
  874. elfcorehdr_addr = ELFCORE_ADDR_MAX;
  875. #endif
  876. os_info_init();
  877. setup_ipl();
  878. setup_task_size();
  879. setup_control_program_code();
  880. /* Do some memory reservations *before* memory is added to memblock */
  881. reserve_kernel();
  882. reserve_initrd();
  883. reserve_certificate_list();
  884. reserve_mem_detect_info();
  885. memblock_set_current_limit(ident_map_size);
  886. memblock_allow_resize();
  887. /* Get information about *all* installed memory */
  888. memblock_add_mem_detect_info();
  889. free_mem_detect_info();
  890. setup_memory_end();
  891. memblock_dump_all();
  892. setup_memory();
  893. relocate_amode31_section();
  894. setup_cr();
  895. setup_uv();
  896. dma_contiguous_reserve(ident_map_size);
  897. vmcp_cma_reserve();
  898. if (MACHINE_HAS_EDAT2)
  899. hugetlb_cma_reserve(PUD_SHIFT - PAGE_SHIFT);
  900. check_initrd();
  901. reserve_crashkernel();
  902. #ifdef CONFIG_CRASH_DUMP
  903. /*
  904. * Be aware that smp_save_dump_secondary_cpus() triggers a system reset.
  905. * Therefore CPU and device initialization should be done afterwards.
  906. */
  907. smp_save_dump_secondary_cpus();
  908. #endif
  909. setup_resources();
  910. setup_lowcore_dat_off();
  911. smp_fill_possible_mask();
  912. cpu_detect_mhz_feature();
  913. cpu_init();
  914. numa_setup();
  915. smp_detect_cpus();
  916. topology_init_early();
  917. if (test_facility(193))
  918. static_branch_enable(&cpu_has_bear);
  919. /*
  920. * Create kernel page tables and switch to virtual addressing.
  921. */
  922. paging_init();
  923. memcpy_real_init();
  924. /*
  925. * After paging_init created the kernel page table, the new PSWs
  926. * in lowcore can now run with DAT enabled.
  927. */
  928. setup_lowcore_dat_on();
  929. #ifdef CONFIG_CRASH_DUMP
  930. smp_save_dump_ipl_cpu();
  931. #endif
  932. /* Setup default console */
  933. conmode_default();
  934. set_preferred_console();
  935. apply_alternative_instructions();
  936. if (IS_ENABLED(CONFIG_EXPOLINE))
  937. nospec_init_branches();
  938. /* Setup zfcp/nvme dump support */
  939. setup_zfcpdump();
  940. /* Add system specific data to the random pool */
  941. setup_randomness();
  942. }