1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/kernel.h>
3 #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>
9 #include <asm/facility.h>
10 #include <asm/pgtable.h>
11 #include <asm/uv.h>
12 #include "boot.h"
13
14 char __bootdata(early_command_line)[COMMAND_LINE_SIZE];
15 struct ipl_parameter_block __bootdata_preserved(ipl_block);
16 int __bootdata_preserved(ipl_block_valid);
17
18 unsigned long __bootdata(vmalloc_size) = VMALLOC_DEFAULT_SIZE;
19 unsigned long __bootdata(memory_end);
20 int __bootdata(memory_end_set);
21 int __bootdata(noexec_disabled);
22
23 int kaslr_enabled __section(.data);
24
__diag308(unsigned long subcode,void * addr)25 static inline int __diag308(unsigned long subcode, void *addr)
26 {
27 register unsigned long _addr asm("0") = (unsigned long)addr;
28 register unsigned long _rc asm("1") = 0;
29 unsigned long reg1, reg2;
30 psw_t old;
31
32 asm volatile(
33 " mvc 0(16,%[psw_old]),0(%[psw_pgm])\n"
34 " epsw %0,%1\n"
35 " st %0,0(%[psw_pgm])\n"
36 " st %1,4(%[psw_pgm])\n"
37 " larl %0,1f\n"
38 " stg %0,8(%[psw_pgm])\n"
39 " diag %[addr],%[subcode],0x308\n"
40 "1: mvc 0(16,%[psw_pgm]),0(%[psw_old])\n"
41 : "=&d" (reg1), "=&a" (reg2),
42 "+Q" (S390_lowcore.program_new_psw),
43 "=Q" (old),
44 [addr] "+d" (_addr), "+d" (_rc)
45 : [subcode] "d" (subcode),
46 [psw_old] "a" (&old),
47 [psw_pgm] "a" (&S390_lowcore.program_new_psw)
48 : "cc", "memory");
49 return _rc;
50 }
51
store_ipl_parmblock(void)52 void store_ipl_parmblock(void)
53 {
54 int rc;
55
56 rc = __diag308(DIAG308_STORE, &ipl_block);
57 if (rc == DIAG308_RC_OK &&
58 ipl_block.hdr.version <= IPL_MAX_SUPPORTED_VERSION)
59 ipl_block_valid = 1;
60 }
61
scpdata_length(const u8 * buf,size_t count)62 static size_t scpdata_length(const u8 *buf, size_t count)
63 {
64 while (count) {
65 if (buf[count - 1] != '\0' && buf[count - 1] != ' ')
66 break;
67 count--;
68 }
69 return count;
70 }
71
ipl_block_get_ascii_scpdata(char * dest,size_t size,const struct ipl_parameter_block * ipb)72 static size_t ipl_block_get_ascii_scpdata(char *dest, size_t size,
73 const struct ipl_parameter_block *ipb)
74 {
75 size_t count;
76 size_t i;
77 int has_lowercase;
78
79 count = min(size - 1, scpdata_length(ipb->fcp.scp_data,
80 ipb->fcp.scp_data_len));
81 if (!count)
82 goto out;
83
84 has_lowercase = 0;
85 for (i = 0; i < count; i++) {
86 if (!isascii(ipb->fcp.scp_data[i])) {
87 count = 0;
88 goto out;
89 }
90 if (!has_lowercase && islower(ipb->fcp.scp_data[i]))
91 has_lowercase = 1;
92 }
93
94 if (has_lowercase)
95 memcpy(dest, ipb->fcp.scp_data, count);
96 else
97 for (i = 0; i < count; i++)
98 dest[i] = tolower(ipb->fcp.scp_data[i]);
99 out:
100 dest[count] = '\0';
101 return count;
102 }
103
append_ipl_block_parm(void)104 static void append_ipl_block_parm(void)
105 {
106 char *parm, *delim;
107 size_t len, rc = 0;
108
109 len = strlen(early_command_line);
110
111 delim = early_command_line + len; /* '\0' character position */
112 parm = early_command_line + len + 1; /* append right after '\0' */
113
114 switch (ipl_block.pb0_hdr.pbt) {
115 case IPL_PBT_CCW:
116 rc = ipl_block_get_ascii_vmparm(
117 parm, COMMAND_LINE_SIZE - len - 1, &ipl_block);
118 break;
119 case IPL_PBT_FCP:
120 rc = ipl_block_get_ascii_scpdata(
121 parm, COMMAND_LINE_SIZE - len - 1, &ipl_block);
122 break;
123 }
124 if (rc) {
125 if (*parm == '=')
126 memmove(early_command_line, parm + 1, rc);
127 else
128 *delim = ' '; /* replace '\0' with space */
129 }
130 }
131
has_ebcdic_char(const char * str)132 static inline int has_ebcdic_char(const char *str)
133 {
134 int i;
135
136 for (i = 0; str[i]; i++)
137 if (str[i] & 0x80)
138 return 1;
139 return 0;
140 }
141
setup_boot_command_line(void)142 void setup_boot_command_line(void)
143 {
144 COMMAND_LINE[ARCH_COMMAND_LINE_SIZE - 1] = 0;
145 /* convert arch command line to ascii if necessary */
146 if (has_ebcdic_char(COMMAND_LINE))
147 EBCASC(COMMAND_LINE, ARCH_COMMAND_LINE_SIZE);
148 /* copy arch command line */
149 strcpy(early_command_line, strim(COMMAND_LINE));
150
151 /* append IPL PARM data to the boot command line */
152 if (!is_prot_virt_guest() && ipl_block_valid)
153 append_ipl_block_parm();
154 }
155
modify_facility(unsigned long nr,bool clear)156 static void modify_facility(unsigned long nr, bool clear)
157 {
158 if (clear)
159 __clear_facility(nr, S390_lowcore.stfle_fac_list);
160 else
161 __set_facility(nr, S390_lowcore.stfle_fac_list);
162 }
163
check_cleared_facilities(void)164 static void check_cleared_facilities(void)
165 {
166 unsigned long als[] = { FACILITIES_ALS };
167 int i;
168
169 for (i = 0; i < ARRAY_SIZE(als); i++) {
170 if ((S390_lowcore.stfle_fac_list[i] & als[i]) != als[i]) {
171 sclp_early_printk("Warning: The Linux kernel requires facilities cleared via command line option\n");
172 print_missing_facilities();
173 break;
174 }
175 }
176 }
177
modify_fac_list(char * str)178 static void modify_fac_list(char *str)
179 {
180 unsigned long val, endval;
181 char *endp;
182 bool clear;
183
184 while (*str) {
185 clear = false;
186 if (*str == '!') {
187 clear = true;
188 str++;
189 }
190 val = simple_strtoull(str, &endp, 0);
191 if (str == endp)
192 break;
193 str = endp;
194 if (*str == '-') {
195 str++;
196 endval = simple_strtoull(str, &endp, 0);
197 if (str == endp)
198 break;
199 str = endp;
200 while (val <= endval) {
201 modify_facility(val, clear);
202 val++;
203 }
204 } else {
205 modify_facility(val, clear);
206 }
207 if (*str != ',')
208 break;
209 str++;
210 }
211 check_cleared_facilities();
212 }
213
214 static char command_line_buf[COMMAND_LINE_SIZE] __section(.data);
parse_boot_command_line(void)215 void parse_boot_command_line(void)
216 {
217 char *param, *val;
218 bool enabled;
219 char *args;
220 int rc;
221
222 kaslr_enabled = IS_ENABLED(CONFIG_RANDOMIZE_BASE);
223 args = strcpy(command_line_buf, early_command_line);
224 while (*args) {
225 args = next_arg(args, ¶m, &val);
226
227 if (!strcmp(param, "mem") && val) {
228 memory_end = round_down(memparse(val, NULL), PAGE_SIZE);
229 memory_end_set = 1;
230 }
231
232 if (!strcmp(param, "vmalloc") && val)
233 vmalloc_size = round_up(memparse(val, NULL), PAGE_SIZE);
234
235 if (!strcmp(param, "noexec")) {
236 rc = kstrtobool(val, &enabled);
237 if (!rc && !enabled)
238 noexec_disabled = 1;
239 }
240
241 if (!strcmp(param, "facilities") && val)
242 modify_fac_list(val);
243
244 if (!strcmp(param, "nokaslr"))
245 kaslr_enabled = 0;
246 }
247 }
248
setup_memory_end(void)249 void setup_memory_end(void)
250 {
251 #ifdef CONFIG_CRASH_DUMP
252 if (OLDMEM_BASE) {
253 kaslr_enabled = 0;
254 } else if (ipl_block_valid &&
255 ipl_block.pb0_hdr.pbt == IPL_PBT_FCP &&
256 ipl_block.fcp.opt == IPL_PB0_FCP_OPT_DUMP) {
257 kaslr_enabled = 0;
258 if (!sclp_early_get_hsa_size(&memory_end) && memory_end)
259 memory_end_set = 1;
260 }
261 #endif
262 }
263