1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright IBM Corp. 2007, 2009
4 * Author(s): Hongjie Yang <hongjie@us.ibm.com>,
5 * Heiko Carstens <heiko.carstens@de.ibm.com>
8 #define KMSG_COMPONENT "setup"
9 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
11 #include <linux/compiler.h>
12 #include <linux/init.h>
13 #include <linux/errno.h>
14 #include <linux/string.h>
15 #include <linux/ctype.h>
16 #include <linux/lockdep.h>
17 #include <linux/extable.h>
18 #include <linux/pfn.h>
19 #include <linux/uaccess.h>
20 #include <linux/kernel.h>
22 #include <asm/ebcdic.h>
24 #include <asm/lowcore.h>
25 #include <asm/processor.h>
26 #include <asm/sections.h>
27 #include <asm/setup.h>
28 #include <asm/sysinfo.h>
29 #include <asm/cpcmd.h>
31 #include <asm/facility.h>
35 * Create a Kernel NSS if the SAVESYS= parameter is defined
37 #define DEFSYS_CMD_SIZE 128
38 #define SAVESYS_CMD_SIZE 32
40 char kernel_nss_name
[NSS_NAME_SIZE
+ 1];
42 static void __init
setup_boot_command_line(void);
45 * Get the TOD clock running.
47 static void __init
reset_tod_clock(void)
51 if (store_tod_clock(&time
) == 0)
53 /* TOD clock not running. Set the clock to Unix Epoch. */
54 if (set_tod_clock(TOD_UNIX_EPOCH
) != 0 || store_tod_clock(&time
) != 0)
57 memset(tod_clock_base
, 0, 16);
58 *(__u64
*) &tod_clock_base
[1] = TOD_UNIX_EPOCH
;
59 S390_lowcore
.last_update_clock
= TOD_UNIX_EPOCH
;
62 #ifdef CONFIG_SHARED_KERNEL
63 int __init
savesys_ipl_nss(char *cmd
, const int cmdlen
);
66 " .section .init.text,\"ax\",@progbits\n"
68 " .type savesys_ipl_nss, @function\n"
78 " .size savesys_ipl_nss, .-savesys_ipl_nss\n"
81 static __initdata
char upper_command_line
[COMMAND_LINE_SIZE
];
83 static noinline __init
void create_kernel_nss(void)
85 unsigned int i
, stext_pfn
, eshared_pfn
, end_pfn
, min_size
;
86 #ifdef CONFIG_BLK_DEV_INITRD
87 unsigned int sinitrd_pfn
, einitrd_pfn
;
93 char defsys_cmd
[DEFSYS_CMD_SIZE
];
94 char savesys_cmd
[SAVESYS_CMD_SIZE
];
96 /* Do nothing if we are not running under VM */
100 /* Convert COMMAND_LINE to upper case */
101 for (i
= 0; i
< strlen(boot_command_line
); i
++)
102 upper_command_line
[i
] = toupper(boot_command_line
[i
]);
104 savesys_ptr
= strstr(upper_command_line
, "SAVESYS=");
109 savesys_ptr
+= 8; /* Point to the beginning of the NSS name */
110 for (i
= 0; i
< NSS_NAME_SIZE
; i
++) {
111 if (savesys_ptr
[i
] == ' ' || savesys_ptr
[i
] == '\0')
113 kernel_nss_name
[i
] = savesys_ptr
[i
];
116 stext_pfn
= PFN_DOWN(__pa(&_stext
));
117 eshared_pfn
= PFN_DOWN(__pa(&_eshared
));
118 end_pfn
= PFN_UP(__pa(&_end
));
119 min_size
= end_pfn
<< 2;
121 hlen
= snprintf(defsys_cmd
, DEFSYS_CMD_SIZE
,
122 "DEFSYS %s 00000-%.5X EW %.5X-%.5X SR %.5X-%.5X",
123 kernel_nss_name
, stext_pfn
- 1, stext_pfn
,
124 eshared_pfn
- 1, eshared_pfn
, end_pfn
);
126 #ifdef CONFIG_BLK_DEV_INITRD
127 if (INITRD_START
&& INITRD_SIZE
) {
128 sinitrd_pfn
= PFN_DOWN(__pa(INITRD_START
));
129 einitrd_pfn
= PFN_UP(__pa(INITRD_START
+ INITRD_SIZE
));
130 min_size
= einitrd_pfn
<< 2;
131 hlen
+= snprintf(defsys_cmd
+ hlen
, DEFSYS_CMD_SIZE
- hlen
,
132 " EW %.5X-%.5X", sinitrd_pfn
, einitrd_pfn
);
136 snprintf(defsys_cmd
+ hlen
, DEFSYS_CMD_SIZE
- hlen
,
137 " EW MINSIZE=%.7iK PARMREGS=0-13", min_size
);
138 defsys_cmd
[DEFSYS_CMD_SIZE
- 1] = '\0';
139 snprintf(savesys_cmd
, SAVESYS_CMD_SIZE
, "SAVESYS %s \n IPL %s",
140 kernel_nss_name
, kernel_nss_name
);
141 savesys_cmd
[SAVESYS_CMD_SIZE
- 1] = '\0';
143 __cpcmd(defsys_cmd
, NULL
, 0, &response
);
146 pr_err("Defining the Linux kernel NSS failed with rc=%d\n",
148 kernel_nss_name
[0] = '\0';
152 len
= strlen(savesys_cmd
);
153 ASCEBC(savesys_cmd
, len
);
154 response
= savesys_ipl_nss(savesys_cmd
, len
);
156 /* On success: response is equal to the command size,
157 * max SAVESYS_CMD_SIZE
158 * On error: response contains the numeric portion of cp error message.
159 * for SAVESYS it will be >= 263
160 * for missing privilege class, it will be 1
162 if (response
> SAVESYS_CMD_SIZE
|| response
== 1) {
163 pr_err("Saving the Linux kernel NSS failed with rc=%d\n",
165 kernel_nss_name
[0] = '\0';
169 /* re-initialize cputime accounting. */
170 get_tod_clock_ext(tod_clock_base
);
171 S390_lowcore
.last_update_clock
= *(__u64
*) &tod_clock_base
[1];
172 S390_lowcore
.last_update_timer
= 0x7fffffffffffffffULL
;
173 S390_lowcore
.user_timer
= 0;
174 S390_lowcore
.system_timer
= 0;
175 asm volatile("SPT 0(%0)" : : "a" (&S390_lowcore
.last_update_timer
));
177 /* re-setup boot command line with new ipl vm parms */
178 ipl_update_parameters();
179 setup_boot_command_line();
181 ipl_flags
= IPL_NSS_VALID
;
184 #else /* CONFIG_SHARED_KERNEL */
186 static inline void create_kernel_nss(void) { }
188 #endif /* CONFIG_SHARED_KERNEL */
193 static noinline __init
void clear_bss_section(void)
195 memset(__bss_start
, 0, __bss_stop
- __bss_start
);
199 * Initialize storage key for kernel pages
201 static noinline __init
void init_kernel_storage_key(void)
204 unsigned long end_pfn
, init_pfn
;
206 end_pfn
= PFN_UP(__pa(&_end
));
208 for (init_pfn
= 0 ; init_pfn
< end_pfn
; init_pfn
++)
209 page_set_storage_key(init_pfn
<< PAGE_SHIFT
,
210 PAGE_DEFAULT_KEY
, 0);
214 static __initdata
char sysinfo_page
[PAGE_SIZE
] __aligned(PAGE_SIZE
);
216 static noinline __init
void detect_machine_type(void)
218 struct sysinfo_3_2_2
*vmms
= (struct sysinfo_3_2_2
*)&sysinfo_page
;
220 /* Check current-configuration-level */
221 if (stsi(NULL
, 0, 0, 0) <= 2) {
222 S390_lowcore
.machine_flags
|= MACHINE_FLAG_LPAR
;
225 /* Get virtual-machine cpu information. */
226 if (stsi(vmms
, 3, 2, 2) || !vmms
->count
)
229 /* Detect known hypervisors */
230 if (!memcmp(vmms
->vm
[0].cpi
, "\xd2\xe5\xd4", 3))
231 S390_lowcore
.machine_flags
|= MACHINE_FLAG_KVM
;
232 else if (!memcmp(vmms
->vm
[0].cpi
, "\xa9\x61\xe5\xd4", 4))
233 S390_lowcore
.machine_flags
|= MACHINE_FLAG_VM
;
236 /* Remove leading, trailing and double whitespace. */
237 static inline void strim_all(char *str
)
243 memmove(str
, s
, strlen(s
));
245 if (!isspace(*str
++))
248 s
= skip_spaces(str
);
249 memmove(str
, s
, strlen(s
) + 1);
254 static noinline __init
void setup_arch_string(void)
256 struct sysinfo_1_1_1
*mach
= (struct sysinfo_1_1_1
*)&sysinfo_page
;
257 struct sysinfo_3_2_2
*vm
= (struct sysinfo_3_2_2
*)&sysinfo_page
;
258 char mstr
[80], hvstr
[17];
260 if (stsi(mach
, 1, 1, 1))
262 EBCASC(mach
->manufacturer
, sizeof(mach
->manufacturer
));
263 EBCASC(mach
->type
, sizeof(mach
->type
));
264 EBCASC(mach
->model
, sizeof(mach
->model
));
265 EBCASC(mach
->model_capacity
, sizeof(mach
->model_capacity
));
266 sprintf(mstr
, "%-16.16s %-4.4s %-16.16s %-16.16s",
267 mach
->manufacturer
, mach
->type
,
268 mach
->model
, mach
->model_capacity
);
270 if (stsi(vm
, 3, 2, 2) == 0 && vm
->count
) {
271 EBCASC(vm
->vm
[0].cpi
, sizeof(vm
->vm
[0].cpi
));
272 sprintf(hvstr
, "%-16.16s", vm
->vm
[0].cpi
);
276 MACHINE_IS_LPAR
? "LPAR" :
277 MACHINE_IS_VM
? "z/VM" :
278 MACHINE_IS_KVM
? "KVM" : "unknown");
280 dump_stack_set_arch_desc("%s (%s)", mstr
, hvstr
);
283 static __init
void setup_topology(void)
287 if (!test_facility(11))
289 S390_lowcore
.machine_flags
|= MACHINE_FLAG_TOPOLOGY
;
290 for (max_mnest
= 6; max_mnest
> 1; max_mnest
--) {
291 if (stsi(&sysinfo_page
, 15, 1, max_mnest
) == 0)
294 topology_max_mnest
= max_mnest
;
297 static void early_pgm_check_handler(void)
299 const struct exception_table_entry
*fixup
;
300 unsigned long cr0
, cr0_new
;
303 addr
= S390_lowcore
.program_old_psw
.addr
;
304 fixup
= search_exception_tables(addr
);
307 /* Disable low address protection before storing into lowcore. */
308 __ctl_store(cr0
, 0, 0);
309 cr0_new
= cr0
& ~(1UL << 28);
310 __ctl_load(cr0_new
, 0, 0);
311 S390_lowcore
.program_old_psw
.addr
= extable_fixup(fixup
);
312 __ctl_load(cr0
, 0, 0);
315 static noinline __init
void setup_lowcore_early(void)
319 psw
.mask
= PSW_MASK_BASE
| PSW_DEFAULT_KEY
| PSW_MASK_EA
| PSW_MASK_BA
;
320 psw
.addr
= (unsigned long) s390_base_ext_handler
;
321 S390_lowcore
.external_new_psw
= psw
;
322 psw
.addr
= (unsigned long) s390_base_pgm_handler
;
323 S390_lowcore
.program_new_psw
= psw
;
324 s390_base_pgm_handler_fn
= early_pgm_check_handler
;
325 S390_lowcore
.preempt_count
= INIT_PREEMPT_COUNT
;
328 static noinline __init
void setup_facility_list(void)
330 stfle(S390_lowcore
.stfle_fac_list
,
331 ARRAY_SIZE(S390_lowcore
.stfle_fac_list
));
332 memcpy(S390_lowcore
.alt_stfle_fac_list
,
333 S390_lowcore
.stfle_fac_list
,
334 sizeof(S390_lowcore
.alt_stfle_fac_list
));
335 if (!IS_ENABLED(CONFIG_KERNEL_NOBP
))
336 __clear_facility(82, S390_lowcore
.alt_stfle_fac_list
);
339 static __init
void detect_diag9c(void)
341 unsigned int cpu_address
;
344 cpu_address
= stap();
345 diag_stat_inc(DIAG_STAT_X09C
);
351 : "=d" (rc
) : "0" (-EOPNOTSUPP
), "d" (cpu_address
) : "cc");
353 S390_lowcore
.machine_flags
|= MACHINE_FLAG_DIAG9C
;
356 static __init
void detect_diag44(void)
360 diag_stat_inc(DIAG_STAT_X044
);
366 : "=d" (rc
) : "0" (-EOPNOTSUPP
) : "cc");
368 S390_lowcore
.machine_flags
|= MACHINE_FLAG_DIAG44
;
371 static __init
void detect_machine_facilities(void)
373 if (test_facility(8)) {
374 S390_lowcore
.machine_flags
|= MACHINE_FLAG_EDAT1
;
375 __ctl_set_bit(0, 23);
377 if (test_facility(78))
378 S390_lowcore
.machine_flags
|= MACHINE_FLAG_EDAT2
;
379 if (test_facility(3))
380 S390_lowcore
.machine_flags
|= MACHINE_FLAG_IDTE
;
381 if (test_facility(40))
382 S390_lowcore
.machine_flags
|= MACHINE_FLAG_LPP
;
383 if (test_facility(50) && test_facility(73)) {
384 S390_lowcore
.machine_flags
|= MACHINE_FLAG_TE
;
385 __ctl_set_bit(0, 55);
387 if (test_facility(51))
388 S390_lowcore
.machine_flags
|= MACHINE_FLAG_TLB_LC
;
389 if (test_facility(129)) {
390 S390_lowcore
.machine_flags
|= MACHINE_FLAG_VX
;
391 __ctl_set_bit(0, 17);
393 if (test_facility(130)) {
394 S390_lowcore
.machine_flags
|= MACHINE_FLAG_NX
;
395 __ctl_set_bit(0, 20);
397 if (test_facility(133))
398 S390_lowcore
.machine_flags
|= MACHINE_FLAG_GS
;
399 if (test_facility(139) && (tod_clock_base
[1] & 0x80)) {
400 /* Enabled signed clock comparator comparisons */
401 S390_lowcore
.machine_flags
|= MACHINE_FLAG_SCC
;
402 clock_comparator_max
= -1ULL >> 1;
403 __ctl_set_bit(0, 53);
407 static inline void save_vector_registers(void)
409 #ifdef CONFIG_CRASH_DUMP
410 if (test_facility(129))
411 save_vx_regs(boot_cpu_vector_save_area
);
415 static int __init
disable_vector_extension(char *str
)
417 S390_lowcore
.machine_flags
&= ~MACHINE_FLAG_VX
;
418 __ctl_clear_bit(0, 17);
421 early_param("novx", disable_vector_extension
);
423 static int __init
noexec_setup(char *str
)
428 rc
= kstrtobool(str
, &enabled
);
429 if (!rc
&& !enabled
) {
430 /* Disable no-execute support */
431 S390_lowcore
.machine_flags
&= ~MACHINE_FLAG_NX
;
432 __ctl_clear_bit(0, 20);
436 early_param("noexec", noexec_setup
);
438 static int __init
cad_setup(char *str
)
443 rc
= kstrtobool(str
, &enabled
);
444 if (!rc
&& enabled
&& test_facility(128))
445 /* Enable problem state CAD. */
449 early_param("cad", cad_setup
);
451 static __init
void memmove_early(void *dst
, const void *src
, size_t n
)
465 old
= S390_lowcore
.program_new_psw
;
466 S390_lowcore
.program_new_psw
.mask
= __extract_psw();
469 " stg %[addr],%[psw_pgm_addr]\n"
470 "0: mvc 0(1,%[dst]),0(%[src])\n"
471 " agr %[dst],%[incr]\n"
472 " agr %[src],%[incr]\n"
475 : [addr
] "=&d" (addr
),
476 [psw_pgm_addr
] "=Q" (S390_lowcore
.program_new_psw
.addr
),
477 [dst
] "+&a" (dst
), [src
] "+&a" (src
), [n
] "+d" (n
)
480 S390_lowcore
.program_new_psw
= old
;
483 static __init noinline
void ipl_save_parameters(void)
487 src
= (void *)(unsigned long) S390_lowcore
.ipl_parmblock_ptr
;
488 dst
= (void *) IPL_PARMBLOCK_ORIGIN
;
489 memmove_early(dst
, src
, PAGE_SIZE
);
490 S390_lowcore
.ipl_parmblock_ptr
= IPL_PARMBLOCK_ORIGIN
;
493 static __init noinline
void rescue_initrd(void)
495 #ifdef CONFIG_BLK_DEV_INITRD
496 unsigned long min_initrd_addr
= (unsigned long) _end
+ (4UL << 20);
498 * Just like in case of IPL from VM reader we make sure there is a
499 * gap of 4MB between end of kernel and start of initrd.
500 * That way we can also be sure that saving an NSS will succeed,
501 * which however only requires different segments.
503 if (!INITRD_START
|| !INITRD_SIZE
)
505 if (INITRD_START
>= min_initrd_addr
)
507 memmove_early((void *) min_initrd_addr
, (void *) INITRD_START
, INITRD_SIZE
);
508 INITRD_START
= min_initrd_addr
;
512 /* Set up boot command line */
513 static void __init
append_to_cmdline(size_t (*ipl_data
)(char *, size_t))
518 len
= strlen(boot_command_line
);
520 delim
= boot_command_line
+ len
; /* '\0' character position */
521 parm
= boot_command_line
+ len
+ 1; /* append right after '\0' */
523 rc
= ipl_data(parm
, COMMAND_LINE_SIZE
- len
- 1);
526 memmove(boot_command_line
, parm
+ 1, rc
);
528 *delim
= ' '; /* replace '\0' with space */
532 static inline int has_ebcdic_char(const char *str
)
536 for (i
= 0; str
[i
]; i
++)
542 static void __init
setup_boot_command_line(void)
544 COMMAND_LINE
[ARCH_COMMAND_LINE_SIZE
- 1] = 0;
545 /* convert arch command line to ascii if necessary */
546 if (has_ebcdic_char(COMMAND_LINE
))
547 EBCASC(COMMAND_LINE
, ARCH_COMMAND_LINE_SIZE
);
548 /* copy arch command line */
549 strlcpy(boot_command_line
, strstrip(COMMAND_LINE
),
550 ARCH_COMMAND_LINE_SIZE
);
552 /* append IPL PARM data to the boot command line */
554 append_to_cmdline(append_ipl_vmparm
);
556 append_to_cmdline(append_ipl_scpdata
);
560 * Save ipl parameters, clear bss memory, initialize storage keys
561 * and create a kernel NSS at startup if the SAVESYS= parm is defined
563 void __init
startup_init(void)
566 ipl_save_parameters();
569 ipl_verify_parameters();
571 init_kernel_storage_key();
573 setup_lowcore_early();
574 setup_facility_list();
575 detect_machine_type();
577 ipl_update_parameters();
578 setup_boot_command_line();
582 detect_machine_facilities();
583 save_vector_registers();