Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  *  S390 version
0004  *    Copyright IBM Corp. 1999, 2012
0005  *    Author(s): Hartmut Penner (hp@de.ibm.com),
0006  *               Martin Schwidefsky (schwidefsky@de.ibm.com)
0007  *
0008  *  Derived from "arch/i386/kernel/setup.c"
0009  *    Copyright (C) 1995, Linus Torvalds
0010  */
0011 
0012 /*
0013  * This file handles the architecture-dependent parts of initialization
0014  */
0015 
0016 #define KMSG_COMPONENT "setup"
0017 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
0018 
0019 #include <linux/errno.h>
0020 #include <linux/export.h>
0021 #include <linux/sched.h>
0022 #include <linux/sched/task.h>
0023 #include <linux/cpu.h>
0024 #include <linux/kernel.h>
0025 #include <linux/memblock.h>
0026 #include <linux/mm.h>
0027 #include <linux/stddef.h>
0028 #include <linux/unistd.h>
0029 #include <linux/ptrace.h>
0030 #include <linux/random.h>
0031 #include <linux/user.h>
0032 #include <linux/tty.h>
0033 #include <linux/ioport.h>
0034 #include <linux/delay.h>
0035 #include <linux/init.h>
0036 #include <linux/initrd.h>
0037 #include <linux/root_dev.h>
0038 #include <linux/console.h>
0039 #include <linux/kernel_stat.h>
0040 #include <linux/dma-map-ops.h>
0041 #include <linux/device.h>
0042 #include <linux/notifier.h>
0043 #include <linux/pfn.h>
0044 #include <linux/ctype.h>
0045 #include <linux/reboot.h>
0046 #include <linux/topology.h>
0047 #include <linux/kexec.h>
0048 #include <linux/crash_dump.h>
0049 #include <linux/memory.h>
0050 #include <linux/compat.h>
0051 #include <linux/start_kernel.h>
0052 #include <linux/hugetlb.h>
0053 #include <linux/kmemleak.h>
0054 
0055 #include <asm/boot_data.h>
0056 #include <asm/ipl.h>
0057 #include <asm/facility.h>
0058 #include <asm/smp.h>
0059 #include <asm/mmu_context.h>
0060 #include <asm/cpcmd.h>
0061 #include <asm/lowcore.h>
0062 #include <asm/nmi.h>
0063 #include <asm/irq.h>
0064 #include <asm/page.h>
0065 #include <asm/ptrace.h>
0066 #include <asm/sections.h>
0067 #include <asm/ebcdic.h>
0068 #include <asm/diag.h>
0069 #include <asm/os_info.h>
0070 #include <asm/sclp.h>
0071 #include <asm/stacktrace.h>
0072 #include <asm/sysinfo.h>
0073 #include <asm/numa.h>
0074 #include <asm/alternative.h>
0075 #include <asm/nospec-branch.h>
0076 #include <asm/mem_detect.h>
0077 #include <asm/uv.h>
0078 #include <asm/asm-offsets.h>
0079 #include "entry.h"
0080 
0081 /*
0082  * Machine setup..
0083  */
0084 unsigned int console_mode = 0;
0085 EXPORT_SYMBOL(console_mode);
0086 
0087 unsigned int console_devno = -1;
0088 EXPORT_SYMBOL(console_devno);
0089 
0090 unsigned int console_irq = -1;
0091 EXPORT_SYMBOL(console_irq);
0092 
0093 /*
0094  * Some code and data needs to stay below 2 GB, even when the kernel would be
0095  * relocated above 2 GB, because it has to use 31 bit addresses.
0096  * Such code and data is part of the .amode31 section.
0097  */
0098 unsigned long __amode31_ref __samode31 = (unsigned long)&_samode31;
0099 unsigned long __amode31_ref __eamode31 = (unsigned long)&_eamode31;
0100 unsigned long __amode31_ref __stext_amode31 = (unsigned long)&_stext_amode31;
0101 unsigned long __amode31_ref __etext_amode31 = (unsigned long)&_etext_amode31;
0102 struct exception_table_entry __amode31_ref *__start_amode31_ex_table = _start_amode31_ex_table;
0103 struct exception_table_entry __amode31_ref *__stop_amode31_ex_table = _stop_amode31_ex_table;
0104 
0105 /*
0106  * Control registers CR2, CR5 and CR15 are initialized with addresses
0107  * of tables that must be placed below 2G which is handled by the AMODE31
0108  * sections.
0109  * Because the AMODE31 sections are relocated below 2G at startup,
0110  * the content of control registers CR2, CR5 and CR15 must be updated
0111  * with new addresses after the relocation. The initial initialization of
0112  * control registers occurs in head64.S and then gets updated again after AMODE31
0113  * relocation. We must access the relevant AMODE31 tables indirectly via
0114  * pointers placed in the .amode31.refs linker section. Those pointers get
0115  * updated automatically during AMODE31 relocation and always contain a valid
0116  * address within AMODE31 sections.
0117  */
0118 
0119 static __amode31_data u32 __ctl_duct_amode31[16] __aligned(64);
0120 
0121 static __amode31_data u64 __ctl_aste_amode31[8] __aligned(64) = {
0122     [1] = 0xffffffffffffffff
0123 };
0124 
0125 static __amode31_data u32 __ctl_duald_amode31[32] __aligned(128) = {
0126     0x80000000, 0, 0, 0,
0127     0x80000000, 0, 0, 0,
0128     0x80000000, 0, 0, 0,
0129     0x80000000, 0, 0, 0,
0130     0x80000000, 0, 0, 0,
0131     0x80000000, 0, 0, 0,
0132     0x80000000, 0, 0, 0,
0133     0x80000000, 0, 0, 0
0134 };
0135 
0136 static __amode31_data u32 __ctl_linkage_stack_amode31[8] __aligned(64) = {
0137     0, 0, 0x89000000, 0,
0138     0, 0, 0x8a000000, 0
0139 };
0140 
0141 static u64 __amode31_ref *__ctl_aste = __ctl_aste_amode31;
0142 static u32 __amode31_ref *__ctl_duald = __ctl_duald_amode31;
0143 static u32 __amode31_ref *__ctl_linkage_stack = __ctl_linkage_stack_amode31;
0144 static u32 __amode31_ref *__ctl_duct = __ctl_duct_amode31;
0145 
0146 int __bootdata(noexec_disabled);
0147 unsigned long __bootdata(ident_map_size);
0148 struct mem_detect_info __bootdata(mem_detect);
0149 struct initrd_data __bootdata(initrd_data);
0150 
0151 unsigned long __bootdata_preserved(__kaslr_offset);
0152 unsigned long __bootdata(__amode31_base);
0153 unsigned int __bootdata_preserved(zlib_dfltcc_support);
0154 EXPORT_SYMBOL(zlib_dfltcc_support);
0155 u64 __bootdata_preserved(stfle_fac_list[16]);
0156 EXPORT_SYMBOL(stfle_fac_list);
0157 u64 __bootdata_preserved(alt_stfle_fac_list[16]);
0158 struct oldmem_data __bootdata_preserved(oldmem_data);
0159 
0160 unsigned long VMALLOC_START;
0161 EXPORT_SYMBOL(VMALLOC_START);
0162 
0163 unsigned long VMALLOC_END;
0164 EXPORT_SYMBOL(VMALLOC_END);
0165 
0166 struct page *vmemmap;
0167 EXPORT_SYMBOL(vmemmap);
0168 unsigned long vmemmap_size;
0169 
0170 unsigned long MODULES_VADDR;
0171 unsigned long MODULES_END;
0172 
0173 /* An array with a pointer to the lowcore of every CPU. */
0174 struct lowcore *lowcore_ptr[NR_CPUS];
0175 EXPORT_SYMBOL(lowcore_ptr);
0176 
0177 DEFINE_STATIC_KEY_FALSE(cpu_has_bear);
0178 
0179 /*
0180  * The Write Back bit position in the physaddr is given by the SLPC PCI.
0181  * Leaving the mask zero always uses write through which is safe
0182  */
0183 unsigned long mio_wb_bit_mask __ro_after_init;
0184 
0185 /*
0186  * This is set up by the setup-routine at boot-time
0187  * for S390 need to find out, what we have to setup
0188  * using address 0x10400 ...
0189  */
0190 
0191 #include <asm/setup.h>
0192 
0193 /*
0194  * condev= and conmode= setup parameter.
0195  */
0196 
0197 static int __init condev_setup(char *str)
0198 {
0199     int vdev;
0200 
0201     vdev = simple_strtoul(str, &str, 0);
0202     if (vdev >= 0 && vdev < 65536) {
0203         console_devno = vdev;
0204         console_irq = -1;
0205     }
0206     return 1;
0207 }
0208 
0209 __setup("condev=", condev_setup);
0210 
0211 static void __init set_preferred_console(void)
0212 {
0213     if (CONSOLE_IS_3215 || CONSOLE_IS_SCLP)
0214         add_preferred_console("ttyS", 0, NULL);
0215     else if (CONSOLE_IS_3270)
0216         add_preferred_console("tty3270", 0, NULL);
0217     else if (CONSOLE_IS_VT220)
0218         add_preferred_console("ttysclp", 0, NULL);
0219     else if (CONSOLE_IS_HVC)
0220         add_preferred_console("hvc", 0, NULL);
0221 }
0222 
0223 static int __init conmode_setup(char *str)
0224 {
0225 #if defined(CONFIG_SCLP_CONSOLE) || defined(CONFIG_SCLP_VT220_CONSOLE)
0226     if (!strcmp(str, "hwc") || !strcmp(str, "sclp"))
0227                 SET_CONSOLE_SCLP;
0228 #endif
0229 #if defined(CONFIG_TN3215_CONSOLE)
0230     if (!strcmp(str, "3215"))
0231         SET_CONSOLE_3215;
0232 #endif
0233 #if defined(CONFIG_TN3270_CONSOLE)
0234     if (!strcmp(str, "3270"))
0235         SET_CONSOLE_3270;
0236 #endif
0237     set_preferred_console();
0238         return 1;
0239 }
0240 
0241 __setup("conmode=", conmode_setup);
0242 
0243 static void __init conmode_default(void)
0244 {
0245     char query_buffer[1024];
0246     char *ptr;
0247 
0248         if (MACHINE_IS_VM) {
0249         cpcmd("QUERY CONSOLE", query_buffer, 1024, NULL);
0250         console_devno = simple_strtoul(query_buffer + 5, NULL, 16);
0251         ptr = strstr(query_buffer, "SUBCHANNEL =");
0252         console_irq = simple_strtoul(ptr + 13, NULL, 16);
0253         cpcmd("QUERY TERM", query_buffer, 1024, NULL);
0254         ptr = strstr(query_buffer, "CONMODE");
0255         /*
0256          * Set the conmode to 3215 so that the device recognition 
0257          * will set the cu_type of the console to 3215. If the
0258          * conmode is 3270 and we don't set it back then both
0259          * 3215 and the 3270 driver will try to access the console
0260          * device (3215 as console and 3270 as normal tty).
0261          */
0262         cpcmd("TERM CONMODE 3215", NULL, 0, NULL);
0263         if (ptr == NULL) {
0264 #if defined(CONFIG_SCLP_CONSOLE) || defined(CONFIG_SCLP_VT220_CONSOLE)
0265             SET_CONSOLE_SCLP;
0266 #endif
0267             return;
0268         }
0269         if (str_has_prefix(ptr + 8, "3270")) {
0270 #if defined(CONFIG_TN3270_CONSOLE)
0271             SET_CONSOLE_3270;
0272 #elif defined(CONFIG_TN3215_CONSOLE)
0273             SET_CONSOLE_3215;
0274 #elif defined(CONFIG_SCLP_CONSOLE) || defined(CONFIG_SCLP_VT220_CONSOLE)
0275             SET_CONSOLE_SCLP;
0276 #endif
0277         } else if (str_has_prefix(ptr + 8, "3215")) {
0278 #if defined(CONFIG_TN3215_CONSOLE)
0279             SET_CONSOLE_3215;
0280 #elif defined(CONFIG_TN3270_CONSOLE)
0281             SET_CONSOLE_3270;
0282 #elif defined(CONFIG_SCLP_CONSOLE) || defined(CONFIG_SCLP_VT220_CONSOLE)
0283             SET_CONSOLE_SCLP;
0284 #endif
0285         }
0286     } else if (MACHINE_IS_KVM) {
0287         if (sclp.has_vt220 && IS_ENABLED(CONFIG_SCLP_VT220_CONSOLE))
0288             SET_CONSOLE_VT220;
0289         else if (sclp.has_linemode && IS_ENABLED(CONFIG_SCLP_CONSOLE))
0290             SET_CONSOLE_SCLP;
0291         else
0292             SET_CONSOLE_HVC;
0293     } else {
0294 #if defined(CONFIG_SCLP_CONSOLE) || defined(CONFIG_SCLP_VT220_CONSOLE)
0295         SET_CONSOLE_SCLP;
0296 #endif
0297     }
0298 }
0299 
0300 #ifdef CONFIG_CRASH_DUMP
0301 static void __init setup_zfcpdump(void)
0302 {
0303     if (!is_ipl_type_dump())
0304         return;
0305     if (oldmem_data.start)
0306         return;
0307     strcat(boot_command_line, " cio_ignore=all,!ipldev,!condev");
0308     console_loglevel = 2;
0309 }
0310 #else
0311 static inline void setup_zfcpdump(void) {}
0312 #endif /* CONFIG_CRASH_DUMP */
0313 
0314  /*
0315  * Reboot, halt and power_off stubs. They just call _machine_restart,
0316  * _machine_halt or _machine_power_off. 
0317  */
0318 
0319 void machine_restart(char *command)
0320 {
0321     if ((!in_interrupt() && !in_atomic()) || oops_in_progress)
0322         /*
0323          * Only unblank the console if we are called in enabled
0324          * context or a bust_spinlocks cleared the way for us.
0325          */
0326         console_unblank();
0327     _machine_restart(command);
0328 }
0329 
0330 void machine_halt(void)
0331 {
0332     if (!in_interrupt() || oops_in_progress)
0333         /*
0334          * Only unblank the console if we are called in enabled
0335          * context or a bust_spinlocks cleared the way for us.
0336          */
0337         console_unblank();
0338     _machine_halt();
0339 }
0340 
0341 void machine_power_off(void)
0342 {
0343     if (!in_interrupt() || oops_in_progress)
0344         /*
0345          * Only unblank the console if we are called in enabled
0346          * context or a bust_spinlocks cleared the way for us.
0347          */
0348         console_unblank();
0349     _machine_power_off();
0350 }
0351 
0352 /*
0353  * Dummy power off function.
0354  */
0355 void (*pm_power_off)(void) = machine_power_off;
0356 EXPORT_SYMBOL_GPL(pm_power_off);
0357 
0358 void *restart_stack;
0359 
0360 unsigned long stack_alloc(void)
0361 {
0362 #ifdef CONFIG_VMAP_STACK
0363     void *ret;
0364 
0365     ret = __vmalloc_node(THREAD_SIZE, THREAD_SIZE, THREADINFO_GFP,
0366                  NUMA_NO_NODE, __builtin_return_address(0));
0367     kmemleak_not_leak(ret);
0368     return (unsigned long)ret;
0369 #else
0370     return __get_free_pages(GFP_KERNEL, THREAD_SIZE_ORDER);
0371 #endif
0372 }
0373 
0374 void stack_free(unsigned long stack)
0375 {
0376 #ifdef CONFIG_VMAP_STACK
0377     vfree((void *) stack);
0378 #else
0379     free_pages(stack, THREAD_SIZE_ORDER);
0380 #endif
0381 }
0382 
0383 int __init arch_early_irq_init(void)
0384 {
0385     unsigned long stack;
0386 
0387     stack = __get_free_pages(GFP_KERNEL, THREAD_SIZE_ORDER);
0388     if (!stack)
0389         panic("Couldn't allocate async stack");
0390     S390_lowcore.async_stack = stack + STACK_INIT_OFFSET;
0391     return 0;
0392 }
0393 
0394 void __init arch_call_rest_init(void)
0395 {
0396     unsigned long stack;
0397 
0398     stack = stack_alloc();
0399     if (!stack)
0400         panic("Couldn't allocate kernel stack");
0401     current->stack = (void *) stack;
0402 #ifdef CONFIG_VMAP_STACK
0403     current->stack_vm_area = (void *) stack;
0404 #endif
0405     set_task_stack_end_magic(current);
0406     stack += STACK_INIT_OFFSET;
0407     S390_lowcore.kernel_stack = stack;
0408     call_on_stack_noreturn(rest_init, stack);
0409 }
0410 
0411 static void __init setup_lowcore_dat_off(void)
0412 {
0413     unsigned long int_psw_mask = PSW_KERNEL_BITS;
0414     unsigned long mcck_stack;
0415     struct lowcore *lc;
0416 
0417     if (IS_ENABLED(CONFIG_KASAN))
0418         int_psw_mask |= PSW_MASK_DAT;
0419 
0420     /*
0421      * Setup lowcore for boot cpu
0422      */
0423     BUILD_BUG_ON(sizeof(struct lowcore) != LC_PAGES * PAGE_SIZE);
0424     lc = memblock_alloc_low(sizeof(*lc), sizeof(*lc));
0425     if (!lc)
0426         panic("%s: Failed to allocate %zu bytes align=%zx\n",
0427               __func__, sizeof(*lc), sizeof(*lc));
0428 
0429     lc->restart_psw.mask = PSW_KERNEL_BITS;
0430     lc->restart_psw.addr = (unsigned long) restart_int_handler;
0431     lc->external_new_psw.mask = int_psw_mask | PSW_MASK_MCHECK;
0432     lc->external_new_psw.addr = (unsigned long) ext_int_handler;
0433     lc->svc_new_psw.mask = int_psw_mask | PSW_MASK_MCHECK;
0434     lc->svc_new_psw.addr = (unsigned long) system_call;
0435     lc->program_new_psw.mask = int_psw_mask | PSW_MASK_MCHECK;
0436     lc->program_new_psw.addr = (unsigned long) pgm_check_handler;
0437     lc->mcck_new_psw.mask = PSW_KERNEL_BITS;
0438     lc->mcck_new_psw.addr = (unsigned long) mcck_int_handler;
0439     lc->io_new_psw.mask = int_psw_mask | PSW_MASK_MCHECK;
0440     lc->io_new_psw.addr = (unsigned long) io_int_handler;
0441     lc->clock_comparator = clock_comparator_max;
0442     lc->nodat_stack = ((unsigned long) &init_thread_union)
0443         + THREAD_SIZE - STACK_FRAME_OVERHEAD - sizeof(struct pt_regs);
0444     lc->current_task = (unsigned long)&init_task;
0445     lc->lpp = LPP_MAGIC;
0446     lc->machine_flags = S390_lowcore.machine_flags;
0447     lc->preempt_count = S390_lowcore.preempt_count;
0448     nmi_alloc_mcesa_early(&lc->mcesad);
0449     lc->sys_enter_timer = S390_lowcore.sys_enter_timer;
0450     lc->exit_timer = S390_lowcore.exit_timer;
0451     lc->user_timer = S390_lowcore.user_timer;
0452     lc->system_timer = S390_lowcore.system_timer;
0453     lc->steal_timer = S390_lowcore.steal_timer;
0454     lc->last_update_timer = S390_lowcore.last_update_timer;
0455     lc->last_update_clock = S390_lowcore.last_update_clock;
0456 
0457     /*
0458      * Allocate the global restart stack which is the same for
0459      * all CPUs in cast *one* of them does a PSW restart.
0460      */
0461     restart_stack = memblock_alloc(THREAD_SIZE, THREAD_SIZE);
0462     if (!restart_stack)
0463         panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
0464               __func__, THREAD_SIZE, THREAD_SIZE);
0465     restart_stack += STACK_INIT_OFFSET;
0466 
0467     /*
0468      * Set up PSW restart to call ipl.c:do_restart(). Copy the relevant
0469      * restart data to the absolute zero lowcore. This is necessary if
0470      * PSW restart is done on an offline CPU that has lowcore zero.
0471      */
0472     lc->restart_stack = (unsigned long) restart_stack;
0473     lc->restart_fn = (unsigned long) do_restart;
0474     lc->restart_data = 0;
0475     lc->restart_source = -1U;
0476 
0477     put_abs_lowcore(restart_stack, lc->restart_stack);
0478     put_abs_lowcore(restart_fn, lc->restart_fn);
0479     put_abs_lowcore(restart_data, lc->restart_data);
0480     put_abs_lowcore(restart_source, lc->restart_source);
0481     put_abs_lowcore(restart_psw, lc->restart_psw);
0482     put_abs_lowcore(mcesad, lc->mcesad);
0483 
0484     mcck_stack = (unsigned long)memblock_alloc(THREAD_SIZE, THREAD_SIZE);
0485     if (!mcck_stack)
0486         panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
0487               __func__, THREAD_SIZE, THREAD_SIZE);
0488     lc->mcck_stack = mcck_stack + STACK_INIT_OFFSET;
0489 
0490     lc->spinlock_lockval = arch_spin_lockval(0);
0491     lc->spinlock_index = 0;
0492     arch_spin_lock_setup(0);
0493     lc->return_lpswe = gen_lpswe(__LC_RETURN_PSW);
0494     lc->return_mcck_lpswe = gen_lpswe(__LC_RETURN_MCCK_PSW);
0495     lc->preempt_count = PREEMPT_DISABLED;
0496 
0497     set_prefix(__pa(lc));
0498     lowcore_ptr[0] = lc;
0499 }
0500 
0501 static void __init setup_lowcore_dat_on(void)
0502 {
0503     struct lowcore *lc = lowcore_ptr[0];
0504     int cr;
0505 
0506     __ctl_clear_bit(0, 28);
0507     S390_lowcore.external_new_psw.mask |= PSW_MASK_DAT;
0508     S390_lowcore.svc_new_psw.mask |= PSW_MASK_DAT;
0509     S390_lowcore.program_new_psw.mask |= PSW_MASK_DAT;
0510     S390_lowcore.io_new_psw.mask |= PSW_MASK_DAT;
0511     __ctl_set_bit(0, 28);
0512     __ctl_store(S390_lowcore.cregs_save_area, 0, 15);
0513     put_abs_lowcore(restart_flags, RESTART_FLAG_CTLREGS);
0514     put_abs_lowcore(program_new_psw, lc->program_new_psw);
0515     for (cr = 0; cr < ARRAY_SIZE(lc->cregs_save_area); cr++)
0516         put_abs_lowcore(cregs_save_area[cr], lc->cregs_save_area[cr]);
0517 }
0518 
0519 static struct resource code_resource = {
0520     .name  = "Kernel code",
0521     .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM,
0522 };
0523 
0524 static struct resource data_resource = {
0525     .name = "Kernel data",
0526     .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM,
0527 };
0528 
0529 static struct resource bss_resource = {
0530     .name = "Kernel bss",
0531     .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM,
0532 };
0533 
0534 static struct resource __initdata *standard_resources[] = {
0535     &code_resource,
0536     &data_resource,
0537     &bss_resource,
0538 };
0539 
0540 static void __init setup_resources(void)
0541 {
0542     struct resource *res, *std_res, *sub_res;
0543     phys_addr_t start, end;
0544     int j;
0545     u64 i;
0546 
0547     code_resource.start = (unsigned long) _text;
0548     code_resource.end = (unsigned long) _etext - 1;
0549     data_resource.start = (unsigned long) _etext;
0550     data_resource.end = (unsigned long) _edata - 1;
0551     bss_resource.start = (unsigned long) __bss_start;
0552     bss_resource.end = (unsigned long) __bss_stop - 1;
0553 
0554     for_each_mem_range(i, &start, &end) {
0555         res = memblock_alloc(sizeof(*res), 8);
0556         if (!res)
0557             panic("%s: Failed to allocate %zu bytes align=0x%x\n",
0558                   __func__, sizeof(*res), 8);
0559         res->flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM;
0560 
0561         res->name = "System RAM";
0562         res->start = start;
0563         /*
0564          * In memblock, end points to the first byte after the
0565          * range while in resourses, end points to the last byte in
0566          * the range.
0567          */
0568         res->end = end - 1;
0569         request_resource(&iomem_resource, res);
0570 
0571         for (j = 0; j < ARRAY_SIZE(standard_resources); j++) {
0572             std_res = standard_resources[j];
0573             if (std_res->start < res->start ||
0574                 std_res->start > res->end)
0575                 continue;
0576             if (std_res->end > res->end) {
0577                 sub_res = memblock_alloc(sizeof(*sub_res), 8);
0578                 if (!sub_res)
0579                     panic("%s: Failed to allocate %zu bytes align=0x%x\n",
0580                           __func__, sizeof(*sub_res), 8);
0581                 *sub_res = *std_res;
0582                 sub_res->end = res->end;
0583                 std_res->start = res->end + 1;
0584                 request_resource(res, sub_res);
0585             } else {
0586                 request_resource(res, std_res);
0587             }
0588         }
0589     }
0590 #ifdef CONFIG_CRASH_DUMP
0591     /*
0592      * Re-add removed crash kernel memory as reserved memory. This makes
0593      * sure it will be mapped with the identity mapping and struct pages
0594      * will be created, so it can be resized later on.
0595      * However add it later since the crash kernel resource should not be
0596      * part of the System RAM resource.
0597      */
0598     if (crashk_res.end) {
0599         memblock_add_node(crashk_res.start, resource_size(&crashk_res),
0600                   0, MEMBLOCK_NONE);
0601         memblock_reserve(crashk_res.start, resource_size(&crashk_res));
0602         insert_resource(&iomem_resource, &crashk_res);
0603     }
0604 #endif
0605 }
0606 
0607 static void __init setup_memory_end(void)
0608 {
0609     memblock_remove(ident_map_size, PHYS_ADDR_MAX - ident_map_size);
0610     max_pfn = max_low_pfn = PFN_DOWN(ident_map_size);
0611     pr_notice("The maximum memory size is %luMB\n", ident_map_size >> 20);
0612 }
0613 
0614 #ifdef CONFIG_CRASH_DUMP
0615 
0616 /*
0617  * When kdump is enabled, we have to ensure that no memory from the area
0618  * [0 - crashkernel memory size] is set offline - it will be exchanged with
0619  * the crashkernel memory region when kdump is triggered. The crashkernel
0620  * memory region can never get offlined (pages are unmovable).
0621  */
0622 static int kdump_mem_notifier(struct notifier_block *nb,
0623                   unsigned long action, void *data)
0624 {
0625     struct memory_notify *arg = data;
0626 
0627     if (action != MEM_GOING_OFFLINE)
0628         return NOTIFY_OK;
0629     if (arg->start_pfn < PFN_DOWN(resource_size(&crashk_res)))
0630         return NOTIFY_BAD;
0631     return NOTIFY_OK;
0632 }
0633 
0634 static struct notifier_block kdump_mem_nb = {
0635     .notifier_call = kdump_mem_notifier,
0636 };
0637 
0638 #endif
0639 
0640 /*
0641  * Reserve memory for kdump kernel to be loaded with kexec
0642  */
0643 static void __init reserve_crashkernel(void)
0644 {
0645 #ifdef CONFIG_CRASH_DUMP
0646     unsigned long long crash_base, crash_size;
0647     phys_addr_t low, high;
0648     int rc;
0649 
0650     rc = parse_crashkernel(boot_command_line, ident_map_size, &crash_size,
0651                    &crash_base);
0652 
0653     crash_base = ALIGN(crash_base, KEXEC_CRASH_MEM_ALIGN);
0654     crash_size = ALIGN(crash_size, KEXEC_CRASH_MEM_ALIGN);
0655     if (rc || crash_size == 0)
0656         return;
0657 
0658     if (memblock.memory.regions[0].size < crash_size) {
0659         pr_info("crashkernel reservation failed: %s\n",
0660             "first memory chunk must be at least crashkernel size");
0661         return;
0662     }
0663 
0664     low = crash_base ?: oldmem_data.start;
0665     high = low + crash_size;
0666     if (low >= oldmem_data.start && high <= oldmem_data.start + oldmem_data.size) {
0667         /* The crashkernel fits into OLDMEM, reuse OLDMEM */
0668         crash_base = low;
0669     } else {
0670         /* Find suitable area in free memory */
0671         low = max_t(unsigned long, crash_size, sclp.hsa_size);
0672         high = crash_base ? crash_base + crash_size : ULONG_MAX;
0673 
0674         if (crash_base && crash_base < low) {
0675             pr_info("crashkernel reservation failed: %s\n",
0676                 "crash_base too low");
0677             return;
0678         }
0679         low = crash_base ?: low;
0680         crash_base = memblock_phys_alloc_range(crash_size,
0681                                KEXEC_CRASH_MEM_ALIGN,
0682                                low, high);
0683     }
0684 
0685     if (!crash_base) {
0686         pr_info("crashkernel reservation failed: %s\n",
0687             "no suitable area found");
0688         return;
0689     }
0690 
0691     if (register_memory_notifier(&kdump_mem_nb)) {
0692         memblock_phys_free(crash_base, crash_size);
0693         return;
0694     }
0695 
0696     if (!oldmem_data.start && MACHINE_IS_VM)
0697         diag10_range(PFN_DOWN(crash_base), PFN_DOWN(crash_size));
0698     crashk_res.start = crash_base;
0699     crashk_res.end = crash_base + crash_size - 1;
0700     memblock_remove(crash_base, crash_size);
0701     pr_info("Reserving %lluMB of memory at %lluMB "
0702         "for crashkernel (System RAM: %luMB)\n",
0703         crash_size >> 20, crash_base >> 20,
0704         (unsigned long)memblock.memory.total_size >> 20);
0705     os_info_crashkernel_add(crash_base, crash_size);
0706 #endif
0707 }
0708 
0709 /*
0710  * Reserve the initrd from being used by memblock
0711  */
0712 static void __init reserve_initrd(void)
0713 {
0714 #ifdef CONFIG_BLK_DEV_INITRD
0715     if (!initrd_data.start || !initrd_data.size)
0716         return;
0717     initrd_start = (unsigned long)__va(initrd_data.start);
0718     initrd_end = initrd_start + initrd_data.size;
0719     memblock_reserve(initrd_data.start, initrd_data.size);
0720 #endif
0721 }
0722 
0723 /*
0724  * Reserve the memory area used to pass the certificate lists
0725  */
0726 static void __init reserve_certificate_list(void)
0727 {
0728     if (ipl_cert_list_addr)
0729         memblock_reserve(ipl_cert_list_addr, ipl_cert_list_size);
0730 }
0731 
0732 static void __init reserve_mem_detect_info(void)
0733 {
0734     unsigned long start, size;
0735 
0736     get_mem_detect_reserved(&start, &size);
0737     if (size)
0738         memblock_reserve(start, size);
0739 }
0740 
0741 static void __init free_mem_detect_info(void)
0742 {
0743     unsigned long start, size;
0744 
0745     get_mem_detect_reserved(&start, &size);
0746     if (size)
0747         memblock_phys_free(start, size);
0748 }
0749 
0750 static const char * __init get_mem_info_source(void)
0751 {
0752     switch (mem_detect.info_source) {
0753     case MEM_DETECT_SCLP_STOR_INFO:
0754         return "sclp storage info";
0755     case MEM_DETECT_DIAG260:
0756         return "diag260";
0757     case MEM_DETECT_SCLP_READ_INFO:
0758         return "sclp read info";
0759     case MEM_DETECT_BIN_SEARCH:
0760         return "binary search";
0761     }
0762     return "none";
0763 }
0764 
0765 static void __init memblock_add_mem_detect_info(void)
0766 {
0767     unsigned long start, end;
0768     int i;
0769 
0770     pr_debug("physmem info source: %s (%hhd)\n",
0771          get_mem_info_source(), mem_detect.info_source);
0772     /* keep memblock lists close to the kernel */
0773     memblock_set_bottom_up(true);
0774     for_each_mem_detect_block(i, &start, &end) {
0775         memblock_add(start, end - start);
0776         memblock_physmem_add(start, end - start);
0777     }
0778     memblock_set_bottom_up(false);
0779     memblock_set_node(0, ULONG_MAX, &memblock.memory, 0);
0780 }
0781 
0782 /*
0783  * Check for initrd being in usable memory
0784  */
0785 static void __init check_initrd(void)
0786 {
0787 #ifdef CONFIG_BLK_DEV_INITRD
0788     if (initrd_data.start && initrd_data.size &&
0789         !memblock_is_region_memory(initrd_data.start, initrd_data.size)) {
0790         pr_err("The initial RAM disk does not fit into the memory\n");
0791         memblock_phys_free(initrd_data.start, initrd_data.size);
0792         initrd_start = initrd_end = 0;
0793     }
0794 #endif
0795 }
0796 
0797 /*
0798  * Reserve memory used for lowcore/command line/kernel image.
0799  */
0800 static void __init reserve_kernel(void)
0801 {
0802     memblock_reserve(0, STARTUP_NORMAL_OFFSET);
0803     memblock_reserve(OLDMEM_BASE, sizeof(unsigned long));
0804     memblock_reserve(OLDMEM_SIZE, sizeof(unsigned long));
0805     memblock_reserve(__amode31_base, __eamode31 - __samode31);
0806     memblock_reserve(__pa(sclp_early_sccb), EXT_SCCB_READ_SCP);
0807     memblock_reserve(__pa(_stext), _end - _stext);
0808 }
0809 
0810 static void __init setup_memory(void)
0811 {
0812     phys_addr_t start, end;
0813     u64 i;
0814 
0815     /*
0816      * Init storage key for present memory
0817      */
0818     for_each_mem_range(i, &start, &end)
0819         storage_key_init_range(start, end);
0820 
0821     psw_set_key(PAGE_DEFAULT_KEY);
0822 }
0823 
0824 static void __init relocate_amode31_section(void)
0825 {
0826     unsigned long amode31_size = __eamode31 - __samode31;
0827     long amode31_offset = __amode31_base - __samode31;
0828     long *ptr;
0829 
0830     pr_info("Relocating AMODE31 section of size 0x%08lx\n", amode31_size);
0831 
0832     /* Move original AMODE31 section to the new one */
0833     memmove((void *)__amode31_base, (void *)__samode31, amode31_size);
0834     /* Zero out the old AMODE31 section to catch invalid accesses within it */
0835     memset((void *)__samode31, 0, amode31_size);
0836 
0837     /* Update all AMODE31 region references */
0838     for (ptr = _start_amode31_refs; ptr != _end_amode31_refs; ptr++)
0839         *ptr += amode31_offset;
0840 }
0841 
0842 /* This must be called after AMODE31 relocation */
0843 static void __init setup_cr(void)
0844 {
0845     union ctlreg2 cr2;
0846     union ctlreg5 cr5;
0847     union ctlreg15 cr15;
0848 
0849     __ctl_duct[1] = (unsigned long)__ctl_aste;
0850     __ctl_duct[2] = (unsigned long)__ctl_aste;
0851     __ctl_duct[4] = (unsigned long)__ctl_duald;
0852 
0853     /* Update control registers CR2, CR5 and CR15 */
0854     __ctl_store(cr2.val, 2, 2);
0855     __ctl_store(cr5.val, 5, 5);
0856     __ctl_store(cr15.val, 15, 15);
0857     cr2.ducto = (unsigned long)__ctl_duct >> 6;
0858     cr5.pasteo = (unsigned long)__ctl_duct >> 6;
0859     cr15.lsea = (unsigned long)__ctl_linkage_stack >> 3;
0860     __ctl_load(cr2.val, 2, 2);
0861     __ctl_load(cr5.val, 5, 5);
0862     __ctl_load(cr15.val, 15, 15);
0863 }
0864 
0865 /*
0866  * Add system information as device randomness
0867  */
0868 static void __init setup_randomness(void)
0869 {
0870     struct sysinfo_3_2_2 *vmms;
0871 
0872     vmms = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
0873     if (!vmms)
0874         panic("Failed to allocate memory for sysinfo structure\n");
0875     if (stsi(vmms, 3, 2, 2) == 0 && vmms->count)
0876         add_device_randomness(&vmms->vm, sizeof(vmms->vm[0]) * vmms->count);
0877     memblock_free(vmms, PAGE_SIZE);
0878 
0879     if (cpacf_query_func(CPACF_PRNO, CPACF_PRNO_TRNG))
0880         static_branch_enable(&s390_arch_random_available);
0881 }
0882 
0883 /*
0884  * Find the correct size for the task_struct. This depends on
0885  * the size of the struct fpu at the end of the thread_struct
0886  * which is embedded in the task_struct.
0887  */
0888 static void __init setup_task_size(void)
0889 {
0890     int task_size = sizeof(struct task_struct);
0891 
0892     if (!MACHINE_HAS_VX) {
0893         task_size -= sizeof(__vector128) * __NUM_VXRS;
0894         task_size += sizeof(freg_t) * __NUM_FPRS;
0895     }
0896     arch_task_struct_size = task_size;
0897 }
0898 
0899 /*
0900  * Issue diagnose 318 to set the control program name and
0901  * version codes.
0902  */
0903 static void __init setup_control_program_code(void)
0904 {
0905     union diag318_info diag318_info = {
0906         .cpnc = CPNC_LINUX,
0907         .cpvc = 0,
0908     };
0909 
0910     if (!sclp.has_diag318)
0911         return;
0912 
0913     diag_stat_inc(DIAG_STAT_X318);
0914     asm volatile("diag %0,0,0x318\n" : : "d" (diag318_info.val));
0915 }
0916 
0917 /*
0918  * Print the component list from the IPL report
0919  */
0920 static void __init log_component_list(void)
0921 {
0922     struct ipl_rb_component_entry *ptr, *end;
0923     char *str;
0924 
0925     if (!early_ipl_comp_list_addr)
0926         return;
0927     if (ipl_block.hdr.flags & IPL_PL_FLAG_SIPL)
0928         pr_info("Linux is running with Secure-IPL enabled\n");
0929     else
0930         pr_info("Linux is running with Secure-IPL disabled\n");
0931     ptr = (void *) early_ipl_comp_list_addr;
0932     end = (void *) ptr + early_ipl_comp_list_size;
0933     pr_info("The IPL report contains the following components:\n");
0934     while (ptr < end) {
0935         if (ptr->flags & IPL_RB_COMPONENT_FLAG_SIGNED) {
0936             if (ptr->flags & IPL_RB_COMPONENT_FLAG_VERIFIED)
0937                 str = "signed, verified";
0938             else
0939                 str = "signed, verification failed";
0940         } else {
0941             str = "not signed";
0942         }
0943         pr_info("%016llx - %016llx (%s)\n",
0944             ptr->addr, ptr->addr + ptr->len, str);
0945         ptr++;
0946     }
0947 }
0948 
0949 /*
0950  * Setup function called from init/main.c just after the banner
0951  * was printed.
0952  */
0953 
0954 void __init setup_arch(char **cmdline_p)
0955 {
0956         /*
0957          * print what head.S has found out about the machine
0958          */
0959     if (MACHINE_IS_VM)
0960         pr_info("Linux is running as a z/VM "
0961             "guest operating system in 64-bit mode\n");
0962     else if (MACHINE_IS_KVM)
0963         pr_info("Linux is running under KVM in 64-bit mode\n");
0964     else if (MACHINE_IS_LPAR)
0965         pr_info("Linux is running natively in 64-bit mode\n");
0966     else
0967         pr_info("Linux is running as a guest in 64-bit mode\n");
0968 
0969     log_component_list();
0970 
0971     /* Have one command line that is parsed and saved in /proc/cmdline */
0972     /* boot_command_line has been already set up in early.c */
0973     *cmdline_p = boot_command_line;
0974 
0975         ROOT_DEV = Root_RAM0;
0976 
0977     setup_initial_init_mm(_text, _etext, _edata, _end);
0978 
0979     if (IS_ENABLED(CONFIG_EXPOLINE_AUTO))
0980         nospec_auto_detect();
0981 
0982     jump_label_init();
0983     parse_early_param();
0984 #ifdef CONFIG_CRASH_DUMP
0985     /* Deactivate elfcorehdr= kernel parameter */
0986     elfcorehdr_addr = ELFCORE_ADDR_MAX;
0987 #endif
0988 
0989     os_info_init();
0990     setup_ipl();
0991     setup_task_size();
0992     setup_control_program_code();
0993 
0994     /* Do some memory reservations *before* memory is added to memblock */
0995     reserve_kernel();
0996     reserve_initrd();
0997     reserve_certificate_list();
0998     reserve_mem_detect_info();
0999     memblock_set_current_limit(ident_map_size);
1000     memblock_allow_resize();
1001 
1002     /* Get information about *all* installed memory */
1003     memblock_add_mem_detect_info();
1004 
1005     free_mem_detect_info();
1006     setup_memory_end();
1007     memblock_dump_all();
1008     setup_memory();
1009 
1010     relocate_amode31_section();
1011     setup_cr();
1012     setup_uv();
1013     dma_contiguous_reserve(ident_map_size);
1014     vmcp_cma_reserve();
1015     if (MACHINE_HAS_EDAT2)
1016         hugetlb_cma_reserve(PUD_SHIFT - PAGE_SHIFT);
1017 
1018     check_initrd();
1019     reserve_crashkernel();
1020 #ifdef CONFIG_CRASH_DUMP
1021     /*
1022      * Be aware that smp_save_dump_cpus() triggers a system reset.
1023      * Therefore CPU and device initialization should be done afterwards.
1024      */
1025     smp_save_dump_cpus();
1026 #endif
1027 
1028     setup_resources();
1029     setup_lowcore_dat_off();
1030     smp_fill_possible_mask();
1031     cpu_detect_mhz_feature();
1032         cpu_init();
1033     numa_setup();
1034     smp_detect_cpus();
1035     topology_init_early();
1036 
1037     if (test_facility(193))
1038         static_branch_enable(&cpu_has_bear);
1039 
1040     /*
1041      * Create kernel page tables and switch to virtual addressing.
1042      */
1043         paging_init();
1044 
1045     /*
1046      * After paging_init created the kernel page table, the new PSWs
1047      * in lowcore can now run with DAT enabled.
1048      */
1049     setup_lowcore_dat_on();
1050 
1051         /* Setup default console */
1052     conmode_default();
1053     set_preferred_console();
1054 
1055     apply_alternative_instructions();
1056     if (IS_ENABLED(CONFIG_EXPOLINE))
1057         nospec_init_branches();
1058 
1059     /* Setup zfcp/nvme dump support */
1060     setup_zfcpdump();
1061 
1062     /* Add system specific data to the random pool */
1063     setup_randomness();
1064 }