xref: /openbmc/linux/arch/s390/kernel/early.c (revision b24413180f5600bcb3bb70fbed5cf186b60864bd)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *    Copyright IBM Corp. 2007, 2009
4  *    Author(s): Hongjie Yang <hongjie@us.ibm.com>,
5  *		 Heiko Carstens <heiko.carstens@de.ibm.com>
6  */
7 
8 #define KMSG_COMPONENT "setup"
9 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
10 
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>
21 #include <asm/diag.h>
22 #include <asm/ebcdic.h>
23 #include <asm/ipl.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>
30 #include <asm/sclp.h>
31 #include <asm/facility.h>
32 #include "entry.h"
33 
34 /*
35  * Create a Kernel NSS if the SAVESYS= parameter is defined
36  */
37 #define DEFSYS_CMD_SIZE		128
38 #define SAVESYS_CMD_SIZE	32
39 
40 char kernel_nss_name[NSS_NAME_SIZE + 1];
41 
42 static void __init setup_boot_command_line(void);
43 
44 /*
45  * Get the TOD clock running.
46  */
47 static void __init reset_tod_clock(void)
48 {
49 	u64 time;
50 
51 	if (store_tod_clock(&time) == 0)
52 		return;
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)
55 		disabled_wait(0);
56 
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;
60 }
61 
62 #ifdef CONFIG_SHARED_KERNEL
63 int __init savesys_ipl_nss(char *cmd, const int cmdlen);
64 
65 asm(
66 	"	.section .init.text,\"ax\",@progbits\n"
67 	"	.align	4\n"
68 	"	.type	savesys_ipl_nss, @function\n"
69 	"savesys_ipl_nss:\n"
70 	"	stmg	6,15,48(15)\n"
71 	"	lgr	14,3\n"
72 	"	sam31\n"
73 	"	diag	2,14,0x8\n"
74 	"	sam64\n"
75 	"	lgr	2,14\n"
76 	"	lmg	6,15,48(15)\n"
77 	"	br	14\n"
78 	"	.size	savesys_ipl_nss, .-savesys_ipl_nss\n"
79 	"	.previous\n");
80 
81 static __initdata char upper_command_line[COMMAND_LINE_SIZE];
82 
83 static noinline __init void create_kernel_nss(void)
84 {
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;
88 #endif
89 	int response;
90 	int hlen;
91 	size_t len;
92 	char *savesys_ptr;
93 	char defsys_cmd[DEFSYS_CMD_SIZE];
94 	char savesys_cmd[SAVESYS_CMD_SIZE];
95 
96 	/* Do nothing if we are not running under VM */
97 	if (!MACHINE_IS_VM)
98 		return;
99 
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]);
103 
104 	savesys_ptr = strstr(upper_command_line, "SAVESYS=");
105 
106 	if (!savesys_ptr)
107 		return;
108 
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')
112 			break;
113 		kernel_nss_name[i] = savesys_ptr[i];
114 	}
115 
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;
120 
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);
125 
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);
133 	}
134 #endif
135 
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';
142 
143 	__cpcmd(defsys_cmd, NULL, 0, &response);
144 
145 	if (response != 0) {
146 		pr_err("Defining the Linux kernel NSS failed with rc=%d\n",
147 			response);
148 		kernel_nss_name[0] = '\0';
149 		return;
150 	}
151 
152 	len = strlen(savesys_cmd);
153 	ASCEBC(savesys_cmd, len);
154 	response = savesys_ipl_nss(savesys_cmd, len);
155 
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
161 	 */
162 	if (response > SAVESYS_CMD_SIZE || response == 1) {
163 		pr_err("Saving the Linux kernel NSS failed with rc=%d\n",
164 			response);
165 		kernel_nss_name[0] = '\0';
166 		return;
167 	}
168 
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));
176 
177 	/* re-setup boot command line with new ipl vm parms */
178 	ipl_update_parameters();
179 	setup_boot_command_line();
180 
181 	ipl_flags = IPL_NSS_VALID;
182 }
183 
184 #else /* CONFIG_SHARED_KERNEL */
185 
186 static inline void create_kernel_nss(void) { }
187 
188 #endif /* CONFIG_SHARED_KERNEL */
189 
190 /*
191  * Clear bss memory
192  */
193 static noinline __init void clear_bss_section(void)
194 {
195 	memset(__bss_start, 0, __bss_stop - __bss_start);
196 }
197 
198 /*
199  * Initialize storage key for kernel pages
200  */
201 static noinline __init void init_kernel_storage_key(void)
202 {
203 #if PAGE_DEFAULT_KEY
204 	unsigned long end_pfn, init_pfn;
205 
206 	end_pfn = PFN_UP(__pa(&_end));
207 
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);
211 #endif
212 }
213 
214 static __initdata char sysinfo_page[PAGE_SIZE] __aligned(PAGE_SIZE);
215 
216 static noinline __init void detect_machine_type(void)
217 {
218 	struct sysinfo_3_2_2 *vmms = (struct sysinfo_3_2_2 *)&sysinfo_page;
219 
220 	/* Check current-configuration-level */
221 	if (stsi(NULL, 0, 0, 0) <= 2) {
222 		S390_lowcore.machine_flags |= MACHINE_FLAG_LPAR;
223 		return;
224 	}
225 	/* Get virtual-machine cpu information. */
226 	if (stsi(vmms, 3, 2, 2) || !vmms->count)
227 		return;
228 
229 	/* Running under KVM? If not we assume z/VM */
230 	if (!memcmp(vmms->vm[0].cpi, "\xd2\xe5\xd4", 3))
231 		S390_lowcore.machine_flags |= MACHINE_FLAG_KVM;
232 	else
233 		S390_lowcore.machine_flags |= MACHINE_FLAG_VM;
234 }
235 
236 /* Remove leading, trailing and double whitespace. */
237 static inline void strim_all(char *str)
238 {
239 	char *s;
240 
241 	s = strim(str);
242 	if (s != str)
243 		memmove(str, s, strlen(s));
244 	while (*str) {
245 		if (!isspace(*str++))
246 			continue;
247 		if (isspace(*str)) {
248 			s = skip_spaces(str);
249 			memmove(str, s, strlen(s) + 1);
250 		}
251 	}
252 }
253 
254 static noinline __init void setup_arch_string(void)
255 {
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];
259 
260 	if (stsi(mach, 1, 1, 1))
261 		return;
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);
269 	strim_all(mstr);
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);
273 		strim_all(hvstr);
274 	} else {
275 		sprintf(hvstr, "%s",
276 			MACHINE_IS_LPAR ? "LPAR" :
277 			MACHINE_IS_VM ? "z/VM" :
278 			MACHINE_IS_KVM ? "KVM" : "unknown");
279 	}
280 	dump_stack_set_arch_desc("%s (%s)", mstr, hvstr);
281 }
282 
283 static __init void setup_topology(void)
284 {
285 	int max_mnest;
286 
287 	if (!test_facility(11))
288 		return;
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)
292 			break;
293 	}
294 	topology_max_mnest = max_mnest;
295 }
296 
297 static void early_pgm_check_handler(void)
298 {
299 	const struct exception_table_entry *fixup;
300 	unsigned long cr0, cr0_new;
301 	unsigned long addr;
302 
303 	addr = S390_lowcore.program_old_psw.addr;
304 	fixup = search_exception_tables(addr);
305 	if (!fixup)
306 		disabled_wait(0);
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);
313 }
314 
315 static noinline __init void setup_lowcore_early(void)
316 {
317 	psw_t psw;
318 
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;
326 }
327 
328 static noinline __init void setup_facility_list(void)
329 {
330 	stfle(S390_lowcore.stfle_fac_list,
331 	      ARRAY_SIZE(S390_lowcore.stfle_fac_list));
332 }
333 
334 static __init void detect_diag9c(void)
335 {
336 	unsigned int cpu_address;
337 	int rc;
338 
339 	cpu_address = stap();
340 	diag_stat_inc(DIAG_STAT_X09C);
341 	asm volatile(
342 		"	diag	%2,0,0x9c\n"
343 		"0:	la	%0,0\n"
344 		"1:\n"
345 		EX_TABLE(0b,1b)
346 		: "=d" (rc) : "0" (-EOPNOTSUPP), "d" (cpu_address) : "cc");
347 	if (!rc)
348 		S390_lowcore.machine_flags |= MACHINE_FLAG_DIAG9C;
349 }
350 
351 static __init void detect_diag44(void)
352 {
353 	int rc;
354 
355 	diag_stat_inc(DIAG_STAT_X044);
356 	asm volatile(
357 		"	diag	0,0,0x44\n"
358 		"0:	la	%0,0\n"
359 		"1:\n"
360 		EX_TABLE(0b,1b)
361 		: "=d" (rc) : "0" (-EOPNOTSUPP) : "cc");
362 	if (!rc)
363 		S390_lowcore.machine_flags |= MACHINE_FLAG_DIAG44;
364 }
365 
366 static __init void detect_machine_facilities(void)
367 {
368 	if (test_facility(8)) {
369 		S390_lowcore.machine_flags |= MACHINE_FLAG_EDAT1;
370 		__ctl_set_bit(0, 23);
371 	}
372 	if (test_facility(78))
373 		S390_lowcore.machine_flags |= MACHINE_FLAG_EDAT2;
374 	if (test_facility(3))
375 		S390_lowcore.machine_flags |= MACHINE_FLAG_IDTE;
376 	if (test_facility(40))
377 		S390_lowcore.machine_flags |= MACHINE_FLAG_LPP;
378 	if (test_facility(50) && test_facility(73))
379 		S390_lowcore.machine_flags |= MACHINE_FLAG_TE;
380 	if (test_facility(51))
381 		S390_lowcore.machine_flags |= MACHINE_FLAG_TLB_LC;
382 	if (test_facility(129)) {
383 		S390_lowcore.machine_flags |= MACHINE_FLAG_VX;
384 		__ctl_set_bit(0, 17);
385 	}
386 	if (test_facility(130)) {
387 		S390_lowcore.machine_flags |= MACHINE_FLAG_NX;
388 		__ctl_set_bit(0, 20);
389 	}
390 	if (test_facility(133))
391 		S390_lowcore.machine_flags |= MACHINE_FLAG_GS;
392 	if (test_facility(139) && (tod_clock_base[1] & 0x80)) {
393 		/* Enabled signed clock comparator comparisons */
394 		S390_lowcore.machine_flags |= MACHINE_FLAG_SCC;
395 		clock_comparator_max = -1ULL >> 1;
396 		__ctl_set_bit(0, 53);
397 	}
398 }
399 
400 static inline void save_vector_registers(void)
401 {
402 #ifdef CONFIG_CRASH_DUMP
403 	if (test_facility(129))
404 		save_vx_regs(boot_cpu_vector_save_area);
405 #endif
406 }
407 
408 static int __init disable_vector_extension(char *str)
409 {
410 	S390_lowcore.machine_flags &= ~MACHINE_FLAG_VX;
411 	__ctl_clear_bit(0, 17);
412 	return 0;
413 }
414 early_param("novx", disable_vector_extension);
415 
416 static int __init noexec_setup(char *str)
417 {
418 	bool enabled;
419 	int rc;
420 
421 	rc = kstrtobool(str, &enabled);
422 	if (!rc && !enabled) {
423 		/* Disable no-execute support */
424 		S390_lowcore.machine_flags &= ~MACHINE_FLAG_NX;
425 		__ctl_clear_bit(0, 20);
426 	}
427 	return rc;
428 }
429 early_param("noexec", noexec_setup);
430 
431 static int __init cad_setup(char *str)
432 {
433 	bool enabled;
434 	int rc;
435 
436 	rc = kstrtobool(str, &enabled);
437 	if (!rc && enabled && test_facility(128))
438 		/* Enable problem state CAD. */
439 		__ctl_set_bit(2, 3);
440 	return rc;
441 }
442 early_param("cad", cad_setup);
443 
444 static __init void memmove_early(void *dst, const void *src, size_t n)
445 {
446 	unsigned long addr;
447 	long incr;
448 	psw_t old;
449 
450 	if (!n)
451 		return;
452 	incr = 1;
453 	if (dst > src) {
454 		incr = -incr;
455 		dst += n - 1;
456 		src += n - 1;
457 	}
458 	old = S390_lowcore.program_new_psw;
459 	S390_lowcore.program_new_psw.mask = __extract_psw();
460 	asm volatile(
461 		"	larl	%[addr],1f\n"
462 		"	stg	%[addr],%[psw_pgm_addr]\n"
463 		"0:     mvc	0(1,%[dst]),0(%[src])\n"
464 		"	agr	%[dst],%[incr]\n"
465 		"	agr	%[src],%[incr]\n"
466 		"	brctg	%[n],0b\n"
467 		"1:\n"
468 		: [addr] "=&d" (addr),
469 		  [psw_pgm_addr] "=Q" (S390_lowcore.program_new_psw.addr),
470 		  [dst] "+&a" (dst), [src] "+&a" (src),  [n] "+d" (n)
471 		: [incr] "d" (incr)
472 		: "cc", "memory");
473 	S390_lowcore.program_new_psw = old;
474 }
475 
476 static __init noinline void ipl_save_parameters(void)
477 {
478 	void *src, *dst;
479 
480 	src = (void *)(unsigned long) S390_lowcore.ipl_parmblock_ptr;
481 	dst = (void *) IPL_PARMBLOCK_ORIGIN;
482 	memmove_early(dst, src, PAGE_SIZE);
483 	S390_lowcore.ipl_parmblock_ptr = IPL_PARMBLOCK_ORIGIN;
484 }
485 
486 static __init noinline void rescue_initrd(void)
487 {
488 #ifdef CONFIG_BLK_DEV_INITRD
489 	unsigned long min_initrd_addr = (unsigned long) _end + (4UL << 20);
490 	/*
491 	 * Just like in case of IPL from VM reader we make sure there is a
492 	 * gap of 4MB between end of kernel and start of initrd.
493 	 * That way we can also be sure that saving an NSS will succeed,
494 	 * which however only requires different segments.
495 	 */
496 	if (!INITRD_START || !INITRD_SIZE)
497 		return;
498 	if (INITRD_START >= min_initrd_addr)
499 		return;
500 	memmove_early((void *) min_initrd_addr, (void *) INITRD_START, INITRD_SIZE);
501 	INITRD_START = min_initrd_addr;
502 #endif
503 }
504 
505 /* Set up boot command line */
506 static void __init append_to_cmdline(size_t (*ipl_data)(char *, size_t))
507 {
508 	char *parm, *delim;
509 	size_t rc, len;
510 
511 	len = strlen(boot_command_line);
512 
513 	delim = boot_command_line + len;	/* '\0' character position */
514 	parm  = boot_command_line + len + 1;	/* append right after '\0' */
515 
516 	rc = ipl_data(parm, COMMAND_LINE_SIZE - len - 1);
517 	if (rc) {
518 		if (*parm == '=')
519 			memmove(boot_command_line, parm + 1, rc);
520 		else
521 			*delim = ' ';		/* replace '\0' with space */
522 	}
523 }
524 
525 static inline int has_ebcdic_char(const char *str)
526 {
527 	int i;
528 
529 	for (i = 0; str[i]; i++)
530 		if (str[i] & 0x80)
531 			return 1;
532 	return 0;
533 }
534 
535 static void __init setup_boot_command_line(void)
536 {
537 	COMMAND_LINE[ARCH_COMMAND_LINE_SIZE - 1] = 0;
538 	/* convert arch command line to ascii if necessary */
539 	if (has_ebcdic_char(COMMAND_LINE))
540 		EBCASC(COMMAND_LINE, ARCH_COMMAND_LINE_SIZE);
541 	/* copy arch command line */
542 	strlcpy(boot_command_line, strstrip(COMMAND_LINE),
543 		ARCH_COMMAND_LINE_SIZE);
544 
545 	/* append IPL PARM data to the boot command line */
546 	if (MACHINE_IS_VM)
547 		append_to_cmdline(append_ipl_vmparm);
548 
549 	append_to_cmdline(append_ipl_scpdata);
550 }
551 
552 /*
553  * Save ipl parameters, clear bss memory, initialize storage keys
554  * and create a kernel NSS at startup if the SAVESYS= parm is defined
555  */
556 void __init startup_init(void)
557 {
558 	reset_tod_clock();
559 	ipl_save_parameters();
560 	rescue_initrd();
561 	clear_bss_section();
562 	ipl_verify_parameters();
563 	time_early_init();
564 	init_kernel_storage_key();
565 	lockdep_off();
566 	setup_lowcore_early();
567 	setup_facility_list();
568 	detect_machine_type();
569 	setup_arch_string();
570 	ipl_update_parameters();
571 	setup_boot_command_line();
572 	create_kernel_nss();
573 	detect_diag9c();
574 	detect_diag44();
575 	detect_machine_facilities();
576 	save_vector_registers();
577 	setup_topology();
578 	sclp_early_detect();
579 	lockdep_on();
580 }
581