blob: b49bd97d33af8be82406f9a61ddfeb068131d360 [file] [log] [blame]
Vasily Gorbik49698742018-05-15 13:28:53 +02001// SPDX-License-Identifier: GPL-2.0
Vasily Gorbik6d85dac2019-02-27 17:36:35 +01002#include <linux/kernel.h>
Vasily Gorbik49698742018-05-15 13:28:53 +02003#include <linux/init.h>
4#include <linux/ctype.h>
5#include <asm/ebcdic.h>
6#include <asm/sclp.h>
7#include <asm/sections.h>
8#include <asm/boot_data.h>
Vasily Gorbikb5e80452019-02-27 16:52:42 +01009#include <asm/facility.h>
Vasily Gorbikdb9492c2019-04-01 19:11:04 +020010#include <asm/uv.h>
Vasily Gorbik49698742018-05-15 13:28:53 +020011#include "boot.h"
12
13char __bootdata(early_command_line)[COMMAND_LINE_SIZE];
Vasily Gorbik1e941d32019-04-01 19:10:51 +020014struct ipl_parameter_block __bootdata_preserved(ipl_block);
15int __bootdata_preserved(ipl_block_valid);
Vasily Gorbik49698742018-05-15 13:28:53 +020016
17unsigned long __bootdata(memory_end);
18int __bootdata(memory_end_set);
Vasily Gorbikd58106c2017-11-17 18:44:28 +010019int __bootdata(noexec_disabled);
Vasily Gorbik49698742018-05-15 13:28:53 +020020
Gerald Schaeferb2d24b92019-02-03 21:37:20 +010021int kaslr_enabled __section(.data);
22
Vasily Gorbik49698742018-05-15 13:28:53 +020023static inline int __diag308(unsigned long subcode, void *addr)
24{
25 register unsigned long _addr asm("0") = (unsigned long)addr;
26 register unsigned long _rc asm("1") = 0;
27 unsigned long reg1, reg2;
28 psw_t old = S390_lowcore.program_new_psw;
29
30 asm volatile(
31 " epsw %0,%1\n"
32 " st %0,%[psw_pgm]\n"
33 " st %1,%[psw_pgm]+4\n"
34 " larl %0,1f\n"
35 " stg %0,%[psw_pgm]+8\n"
36 " diag %[addr],%[subcode],0x308\n"
37 "1: nopr %%r7\n"
38 : "=&d" (reg1), "=&a" (reg2),
39 [psw_pgm] "=Q" (S390_lowcore.program_new_psw),
40 [addr] "+d" (_addr), "+d" (_rc)
41 : [subcode] "d" (subcode)
42 : "cc", "memory");
43 S390_lowcore.program_new_psw = old;
44 return _rc;
45}
46
47void store_ipl_parmblock(void)
48{
49 int rc;
50
Vasily Gorbikdb9492c2019-04-01 19:11:04 +020051 uv_set_shared(__pa(&ipl_block));
Vasily Gorbik1e941d32019-04-01 19:10:51 +020052 rc = __diag308(DIAG308_STORE, &ipl_block);
Vasily Gorbikdb9492c2019-04-01 19:11:04 +020053 uv_remove_shared(__pa(&ipl_block));
Vasily Gorbik49698742018-05-15 13:28:53 +020054 if (rc == DIAG308_RC_OK &&
Vasily Gorbik1e941d32019-04-01 19:10:51 +020055 ipl_block.hdr.version <= IPL_MAX_SUPPORTED_VERSION)
56 ipl_block_valid = 1;
Vasily Gorbik49698742018-05-15 13:28:53 +020057}
58
59static size_t scpdata_length(const char *buf, size_t count)
60{
61 while (count) {
62 if (buf[count - 1] != '\0' && buf[count - 1] != ' ')
63 break;
64 count--;
65 }
66 return count;
67}
68
69static size_t ipl_block_get_ascii_scpdata(char *dest, size_t size,
70 const struct ipl_parameter_block *ipb)
71{
72 size_t count;
73 size_t i;
74 int has_lowercase;
75
Martin Schwidefsky86c74d82019-02-20 14:08:26 +010076 count = min(size - 1, scpdata_length(ipb->fcp.scp_data,
77 ipb->fcp.scp_data_len));
Vasily Gorbik49698742018-05-15 13:28:53 +020078 if (!count)
79 goto out;
80
81 has_lowercase = 0;
82 for (i = 0; i < count; i++) {
Martin Schwidefsky86c74d82019-02-20 14:08:26 +010083 if (!isascii(ipb->fcp.scp_data[i])) {
Vasily Gorbik49698742018-05-15 13:28:53 +020084 count = 0;
85 goto out;
86 }
Martin Schwidefsky86c74d82019-02-20 14:08:26 +010087 if (!has_lowercase && islower(ipb->fcp.scp_data[i]))
Vasily Gorbik49698742018-05-15 13:28:53 +020088 has_lowercase = 1;
89 }
90
91 if (has_lowercase)
Martin Schwidefsky86c74d82019-02-20 14:08:26 +010092 memcpy(dest, ipb->fcp.scp_data, count);
Vasily Gorbik49698742018-05-15 13:28:53 +020093 else
94 for (i = 0; i < count; i++)
Martin Schwidefsky86c74d82019-02-20 14:08:26 +010095 dest[i] = tolower(ipb->fcp.scp_data[i]);
Vasily Gorbik49698742018-05-15 13:28:53 +020096out:
97 dest[count] = '\0';
98 return count;
99}
100
101static void append_ipl_block_parm(void)
102{
103 char *parm, *delim;
104 size_t len, rc = 0;
105
106 len = strlen(early_command_line);
107
108 delim = early_command_line + len; /* '\0' character position */
109 parm = early_command_line + len + 1; /* append right after '\0' */
110
Martin Schwidefsky5f1207f2019-02-20 14:26:51 +0100111 switch (ipl_block.pb0_hdr.pbt) {
112 case IPL_PBT_CCW:
Vasily Gorbik49698742018-05-15 13:28:53 +0200113 rc = ipl_block_get_ascii_vmparm(
Vasily Gorbik1e941d32019-04-01 19:10:51 +0200114 parm, COMMAND_LINE_SIZE - len - 1, &ipl_block);
Vasily Gorbik49698742018-05-15 13:28:53 +0200115 break;
Martin Schwidefsky5f1207f2019-02-20 14:26:51 +0100116 case IPL_PBT_FCP:
Vasily Gorbik49698742018-05-15 13:28:53 +0200117 rc = ipl_block_get_ascii_scpdata(
Vasily Gorbik1e941d32019-04-01 19:10:51 +0200118 parm, COMMAND_LINE_SIZE - len - 1, &ipl_block);
Vasily Gorbik49698742018-05-15 13:28:53 +0200119 break;
120 }
121 if (rc) {
122 if (*parm == '=')
123 memmove(early_command_line, parm + 1, rc);
124 else
125 *delim = ' '; /* replace '\0' with space */
126 }
127}
128
129static inline int has_ebcdic_char(const char *str)
130{
131 int i;
132
133 for (i = 0; str[i]; i++)
134 if (str[i] & 0x80)
135 return 1;
136 return 0;
137}
138
139void setup_boot_command_line(void)
140{
141 COMMAND_LINE[ARCH_COMMAND_LINE_SIZE - 1] = 0;
142 /* convert arch command line to ascii if necessary */
143 if (has_ebcdic_char(COMMAND_LINE))
144 EBCASC(COMMAND_LINE, ARCH_COMMAND_LINE_SIZE);
145 /* copy arch command line */
146 strcpy(early_command_line, strim(COMMAND_LINE));
147
148 /* append IPL PARM data to the boot command line */
Vasily Gorbik093ddcc2019-04-01 19:11:08 +0200149 if (!is_prot_virt_guest() && ipl_block_valid)
Vasily Gorbik49698742018-05-15 13:28:53 +0200150 append_ipl_block_parm();
151}
152
Vasily Gorbikb5e80452019-02-27 16:52:42 +0100153static void modify_facility(unsigned long nr, bool clear)
154{
155 if (clear)
156 __clear_facility(nr, S390_lowcore.stfle_fac_list);
157 else
158 __set_facility(nr, S390_lowcore.stfle_fac_list);
159}
160
Vasily Gorbik6d85dac2019-02-27 17:36:35 +0100161static void check_cleared_facilities(void)
162{
163 unsigned long als[] = { FACILITIES_ALS };
164 int i;
165
166 for (i = 0; i < ARRAY_SIZE(als); i++) {
167 if ((S390_lowcore.stfle_fac_list[i] & als[i]) != als[i]) {
168 sclp_early_printk("Warning: The Linux kernel requires facilities cleared via command line option\n");
169 print_missing_facilities();
170 break;
171 }
172 }
173}
174
Vasily Gorbikb5e80452019-02-27 16:52:42 +0100175static void modify_fac_list(char *str)
176{
177 unsigned long val, endval;
178 char *endp;
179 bool clear;
180
181 while (*str) {
182 clear = false;
183 if (*str == '!') {
184 clear = true;
185 str++;
186 }
187 val = simple_strtoull(str, &endp, 0);
188 if (str == endp)
189 break;
190 str = endp;
191 if (*str == '-') {
192 str++;
193 endval = simple_strtoull(str, &endp, 0);
194 if (str == endp)
195 break;
196 str = endp;
197 while (val <= endval) {
198 modify_facility(val, clear);
199 val++;
200 }
201 } else {
202 modify_facility(val, clear);
203 }
204 if (*str != ',')
205 break;
206 str++;
207 }
Vasily Gorbik6d85dac2019-02-27 17:36:35 +0100208 check_cleared_facilities();
Vasily Gorbikb5e80452019-02-27 16:52:42 +0100209}
210
Vasily Gorbik49698742018-05-15 13:28:53 +0200211static char command_line_buf[COMMAND_LINE_SIZE] __section(.data);
Vasily Gorbikb5e80452019-02-27 16:52:42 +0100212void parse_boot_command_line(void)
Vasily Gorbik49698742018-05-15 13:28:53 +0200213{
Vasily Gorbik49698742018-05-15 13:28:53 +0200214 char *param, *val;
Vasily Gorbikd58106c2017-11-17 18:44:28 +0100215 bool enabled;
216 char *args;
217 int rc;
Vasily Gorbik49698742018-05-15 13:28:53 +0200218
Gerald Schaeferb2d24b92019-02-03 21:37:20 +0100219 kaslr_enabled = IS_ENABLED(CONFIG_RANDOMIZE_BASE);
Vasily Gorbik49698742018-05-15 13:28:53 +0200220 args = strcpy(command_line_buf, early_command_line);
221 while (*args) {
222 args = next_arg(args, &param, &val);
223
224 if (!strcmp(param, "mem")) {
225 memory_end = memparse(val, NULL);
226 memory_end_set = 1;
227 }
Vasily Gorbikd58106c2017-11-17 18:44:28 +0100228
229 if (!strcmp(param, "noexec")) {
230 rc = kstrtobool(val, &enabled);
231 if (!rc && !enabled)
232 noexec_disabled = 1;
233 }
Vasily Gorbikb5e80452019-02-27 16:52:42 +0100234
235 if (!strcmp(param, "facilities"))
236 modify_fac_list(val);
Gerald Schaeferb2d24b92019-02-03 21:37:20 +0100237
238 if (!strcmp(param, "nokaslr"))
239 kaslr_enabled = 0;
Vasily Gorbik49698742018-05-15 13:28:53 +0200240 }
241}
242
243void setup_memory_end(void)
244{
Vasily Gorbik49698742018-05-15 13:28:53 +0200245#ifdef CONFIG_CRASH_DUMP
Gerald Schaeferb2d24b92019-02-03 21:37:20 +0100246 if (OLDMEM_BASE) {
247 kaslr_enabled = 0;
248 } else if (ipl_block_valid &&
249 ipl_block.pb0_hdr.pbt == IPL_PBT_FCP &&
250 ipl_block.fcp.opt == IPL_PB0_FCP_OPT_DUMP) {
251 kaslr_enabled = 0;
Vasily Gorbik49698742018-05-15 13:28:53 +0200252 if (!sclp_early_get_hsa_size(&memory_end) && memory_end)
253 memory_end_set = 1;
254 }
255#endif
256}