• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
reset_tod_clock(void)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 
create_kernel_nss(void)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 
create_kernel_nss(void)186 static inline void create_kernel_nss(void) { }
187 
188 #endif /* CONFIG_SHARED_KERNEL */
189 
190 /*
191  * Clear bss memory
192  */
clear_bss_section(void)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  */
init_kernel_storage_key(void)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 
detect_machine_type(void)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 	/* 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;
234 }
235 
236 /* Remove leading, trailing and double whitespace. */
strim_all(char * str)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 
setup_arch_string(void)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 
setup_topology(void)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 
early_pgm_check_handler(void)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 
setup_lowcore_early(void)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 
setup_facility_list(void)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 	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);
337 }
338 
detect_diag9c(void)339 static __init void detect_diag9c(void)
340 {
341 	unsigned int cpu_address;
342 	int rc;
343 
344 	cpu_address = stap();
345 	diag_stat_inc(DIAG_STAT_X09C);
346 	asm volatile(
347 		"	diag	%2,0,0x9c\n"
348 		"0:	la	%0,0\n"
349 		"1:\n"
350 		EX_TABLE(0b,1b)
351 		: "=d" (rc) : "0" (-EOPNOTSUPP), "d" (cpu_address) : "cc");
352 	if (!rc)
353 		S390_lowcore.machine_flags |= MACHINE_FLAG_DIAG9C;
354 }
355 
detect_diag44(void)356 static __init void detect_diag44(void)
357 {
358 	int rc;
359 
360 	diag_stat_inc(DIAG_STAT_X044);
361 	asm volatile(
362 		"	diag	0,0,0x44\n"
363 		"0:	la	%0,0\n"
364 		"1:\n"
365 		EX_TABLE(0b,1b)
366 		: "=d" (rc) : "0" (-EOPNOTSUPP) : "cc");
367 	if (!rc)
368 		S390_lowcore.machine_flags |= MACHINE_FLAG_DIAG44;
369 }
370 
detect_machine_facilities(void)371 static __init void detect_machine_facilities(void)
372 {
373 	if (test_facility(8)) {
374 		S390_lowcore.machine_flags |= MACHINE_FLAG_EDAT1;
375 		__ctl_set_bit(0, 23);
376 	}
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);
386 	}
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);
392 	}
393 	if (test_facility(130)) {
394 		S390_lowcore.machine_flags |= MACHINE_FLAG_NX;
395 		__ctl_set_bit(0, 20);
396 	}
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);
404 	}
405 }
406 
save_vector_registers(void)407 static inline void save_vector_registers(void)
408 {
409 #ifdef CONFIG_CRASH_DUMP
410 	if (test_facility(129))
411 		save_vx_regs(boot_cpu_vector_save_area);
412 #endif
413 }
414 
disable_vector_extension(char * str)415 static int __init disable_vector_extension(char *str)
416 {
417 	S390_lowcore.machine_flags &= ~MACHINE_FLAG_VX;
418 	__ctl_clear_bit(0, 17);
419 	return 0;
420 }
421 early_param("novx", disable_vector_extension);
422 
noexec_setup(char * str)423 static int __init noexec_setup(char *str)
424 {
425 	bool enabled;
426 	int rc;
427 
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);
433 	}
434 	return rc;
435 }
436 early_param("noexec", noexec_setup);
437 
cad_setup(char * str)438 static int __init cad_setup(char *str)
439 {
440 	bool enabled;
441 	int rc;
442 
443 	rc = kstrtobool(str, &enabled);
444 	if (!rc && enabled && test_facility(128))
445 		/* Enable problem state CAD. */
446 		__ctl_set_bit(2, 3);
447 	return rc;
448 }
449 early_param("cad", cad_setup);
450 
memmove_early(void * dst,const void * src,size_t n)451 static __init void memmove_early(void *dst, const void *src, size_t n)
452 {
453 	unsigned long addr;
454 	long incr;
455 	psw_t old;
456 
457 	if (!n)
458 		return;
459 	incr = 1;
460 	if (dst > src) {
461 		incr = -incr;
462 		dst += n - 1;
463 		src += n - 1;
464 	}
465 	old = S390_lowcore.program_new_psw;
466 	S390_lowcore.program_new_psw.mask = __extract_psw();
467 	asm volatile(
468 		"	larl	%[addr],1f\n"
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"
473 		"	brctg	%[n],0b\n"
474 		"1:\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)
478 		: [incr] "d" (incr)
479 		: "cc", "memory");
480 	S390_lowcore.program_new_psw = old;
481 }
482 
ipl_save_parameters(void)483 static __init noinline void ipl_save_parameters(void)
484 {
485 	void *src, *dst;
486 
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;
491 }
492 
rescue_initrd(void)493 static __init noinline void rescue_initrd(void)
494 {
495 #ifdef CONFIG_BLK_DEV_INITRD
496 	unsigned long min_initrd_addr = (unsigned long) _end + (4UL << 20);
497 	/*
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.
502 	 */
503 	if (!INITRD_START || !INITRD_SIZE)
504 		return;
505 	if (INITRD_START >= min_initrd_addr)
506 		return;
507 	memmove_early((void *) min_initrd_addr, (void *) INITRD_START, INITRD_SIZE);
508 	INITRD_START = min_initrd_addr;
509 #endif
510 }
511 
512 /* Set up boot command line */
append_to_cmdline(size_t (* ipl_data)(char *,size_t))513 static void __init append_to_cmdline(size_t (*ipl_data)(char *, size_t))
514 {
515 	char *parm, *delim;
516 	size_t rc, len;
517 
518 	len = strlen(boot_command_line);
519 
520 	delim = boot_command_line + len;	/* '\0' character position */
521 	parm  = boot_command_line + len + 1;	/* append right after '\0' */
522 
523 	rc = ipl_data(parm, COMMAND_LINE_SIZE - len - 1);
524 	if (rc) {
525 		if (*parm == '=')
526 			memmove(boot_command_line, parm + 1, rc);
527 		else
528 			*delim = ' ';		/* replace '\0' with space */
529 	}
530 }
531 
has_ebcdic_char(const char * str)532 static inline int has_ebcdic_char(const char *str)
533 {
534 	int i;
535 
536 	for (i = 0; str[i]; i++)
537 		if (str[i] & 0x80)
538 			return 1;
539 	return 0;
540 }
541 
setup_boot_command_line(void)542 static void __init setup_boot_command_line(void)
543 {
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);
551 
552 	/* append IPL PARM data to the boot command line */
553 	if (MACHINE_IS_VM)
554 		append_to_cmdline(append_ipl_vmparm);
555 
556 	append_to_cmdline(append_ipl_scpdata);
557 }
558 
559 /*
560  * Save ipl parameters, clear bss memory, initialize storage keys
561  * and create a kernel NSS at startup if the SAVESYS= parm is defined
562  */
startup_init(void)563 void __init startup_init(void)
564 {
565 	reset_tod_clock();
566 	ipl_save_parameters();
567 	rescue_initrd();
568 	clear_bss_section();
569 	ipl_verify_parameters();
570 	time_early_init();
571 	init_kernel_storage_key();
572 	lockdep_off();
573 	setup_lowcore_early();
574 	setup_facility_list();
575 	detect_machine_type();
576 	setup_arch_string();
577 	ipl_update_parameters();
578 	setup_boot_command_line();
579 	create_kernel_nss();
580 	detect_diag9c();
581 	detect_diag44();
582 	detect_machine_facilities();
583 	save_vector_registers();
584 	setup_topology();
585 	sclp_early_detect();
586 	lockdep_on();
587 }
588