0001
0002 #include <linux/kernel.h>
0003 #include <linux/init.h>
0004 #include <linux/ctype.h>
0005 #include <linux/pgtable.h>
0006 #include <asm/ebcdic.h>
0007 #include <asm/sclp.h>
0008 #include <asm/sections.h>
0009 #include <asm/boot_data.h>
0010 #include <asm/facility.h>
0011 #include <asm/setup.h>
0012 #include <asm/uv.h>
0013 #include "boot.h"
0014
0015 struct parmarea parmarea __section(".parmarea") = {
0016 .kernel_version = (unsigned long)kernel_version,
0017 .max_command_line_size = COMMAND_LINE_SIZE,
0018 .command_line = "root=/dev/ram0 ro",
0019 };
0020
0021 char __bootdata(early_command_line)[COMMAND_LINE_SIZE];
0022 int __bootdata(noexec_disabled);
0023
0024 unsigned int __bootdata_preserved(zlib_dfltcc_support) = ZLIB_DFLTCC_FULL;
0025 struct ipl_parameter_block __bootdata_preserved(ipl_block);
0026 int __bootdata_preserved(ipl_block_valid);
0027
0028 unsigned long vmalloc_size = VMALLOC_DEFAULT_SIZE;
0029 unsigned long memory_limit;
0030 int vmalloc_size_set;
0031 int kaslr_enabled;
0032
0033 static inline int __diag308(unsigned long subcode, void *addr)
0034 {
0035 unsigned long reg1, reg2;
0036 union register_pair r1;
0037 psw_t old;
0038
0039 r1.even = (unsigned long) addr;
0040 r1.odd = 0;
0041 asm volatile(
0042 " mvc 0(16,%[psw_old]),0(%[psw_pgm])\n"
0043 " epsw %[reg1],%[reg2]\n"
0044 " st %[reg1],0(%[psw_pgm])\n"
0045 " st %[reg2],4(%[psw_pgm])\n"
0046 " larl %[reg1],1f\n"
0047 " stg %[reg1],8(%[psw_pgm])\n"
0048 " diag %[r1],%[subcode],0x308\n"
0049 "1: mvc 0(16,%[psw_pgm]),0(%[psw_old])\n"
0050 : [r1] "+&d" (r1.pair),
0051 [reg1] "=&d" (reg1),
0052 [reg2] "=&a" (reg2),
0053 "+Q" (S390_lowcore.program_new_psw),
0054 "=Q" (old)
0055 : [subcode] "d" (subcode),
0056 [psw_old] "a" (&old),
0057 [psw_pgm] "a" (&S390_lowcore.program_new_psw)
0058 : "cc", "memory");
0059 return r1.odd;
0060 }
0061
0062 void store_ipl_parmblock(void)
0063 {
0064 int rc;
0065
0066 rc = __diag308(DIAG308_STORE, &ipl_block);
0067 if (rc == DIAG308_RC_OK &&
0068 ipl_block.hdr.version <= IPL_MAX_SUPPORTED_VERSION)
0069 ipl_block_valid = 1;
0070 }
0071
0072 bool is_ipl_block_dump(void)
0073 {
0074 if (ipl_block.pb0_hdr.pbt == IPL_PBT_FCP &&
0075 ipl_block.fcp.opt == IPL_PB0_FCP_OPT_DUMP)
0076 return true;
0077 if (ipl_block.pb0_hdr.pbt == IPL_PBT_NVME &&
0078 ipl_block.nvme.opt == IPL_PB0_NVME_OPT_DUMP)
0079 return true;
0080 return false;
0081 }
0082
0083 static size_t scpdata_length(const u8 *buf, size_t count)
0084 {
0085 while (count) {
0086 if (buf[count - 1] != '\0' && buf[count - 1] != ' ')
0087 break;
0088 count--;
0089 }
0090 return count;
0091 }
0092
0093 static size_t ipl_block_get_ascii_scpdata(char *dest, size_t size,
0094 const struct ipl_parameter_block *ipb)
0095 {
0096 const __u8 *scp_data;
0097 __u32 scp_data_len;
0098 int has_lowercase;
0099 size_t count = 0;
0100 size_t i;
0101
0102 switch (ipb->pb0_hdr.pbt) {
0103 case IPL_PBT_FCP:
0104 scp_data_len = ipb->fcp.scp_data_len;
0105 scp_data = ipb->fcp.scp_data;
0106 break;
0107 case IPL_PBT_NVME:
0108 scp_data_len = ipb->nvme.scp_data_len;
0109 scp_data = ipb->nvme.scp_data;
0110 break;
0111 default:
0112 goto out;
0113 }
0114
0115 count = min(size - 1, scpdata_length(scp_data, scp_data_len));
0116 if (!count)
0117 goto out;
0118
0119 has_lowercase = 0;
0120 for (i = 0; i < count; i++) {
0121 if (!isascii(scp_data[i])) {
0122 count = 0;
0123 goto out;
0124 }
0125 if (!has_lowercase && islower(scp_data[i]))
0126 has_lowercase = 1;
0127 }
0128
0129 if (has_lowercase)
0130 memcpy(dest, scp_data, count);
0131 else
0132 for (i = 0; i < count; i++)
0133 dest[i] = tolower(scp_data[i]);
0134 out:
0135 dest[count] = '\0';
0136 return count;
0137 }
0138
0139 static void append_ipl_block_parm(void)
0140 {
0141 char *parm, *delim;
0142 size_t len, rc = 0;
0143
0144 len = strlen(early_command_line);
0145
0146 delim = early_command_line + len;
0147 parm = early_command_line + len + 1;
0148
0149 switch (ipl_block.pb0_hdr.pbt) {
0150 case IPL_PBT_CCW:
0151 rc = ipl_block_get_ascii_vmparm(
0152 parm, COMMAND_LINE_SIZE - len - 1, &ipl_block);
0153 break;
0154 case IPL_PBT_FCP:
0155 case IPL_PBT_NVME:
0156 rc = ipl_block_get_ascii_scpdata(
0157 parm, COMMAND_LINE_SIZE - len - 1, &ipl_block);
0158 break;
0159 }
0160 if (rc) {
0161 if (*parm == '=')
0162 memmove(early_command_line, parm + 1, rc);
0163 else
0164 *delim = ' ';
0165 }
0166 }
0167
0168 static inline int has_ebcdic_char(const char *str)
0169 {
0170 int i;
0171
0172 for (i = 0; str[i]; i++)
0173 if (str[i] & 0x80)
0174 return 1;
0175 return 0;
0176 }
0177
0178 void setup_boot_command_line(void)
0179 {
0180 parmarea.command_line[COMMAND_LINE_SIZE - 1] = 0;
0181
0182 if (has_ebcdic_char(parmarea.command_line))
0183 EBCASC(parmarea.command_line, COMMAND_LINE_SIZE);
0184
0185 strcpy(early_command_line, strim(parmarea.command_line));
0186
0187
0188 if (!is_prot_virt_guest() && ipl_block_valid)
0189 append_ipl_block_parm();
0190 }
0191
0192 static void modify_facility(unsigned long nr, bool clear)
0193 {
0194 if (clear)
0195 __clear_facility(nr, stfle_fac_list);
0196 else
0197 __set_facility(nr, stfle_fac_list);
0198 }
0199
0200 static void check_cleared_facilities(void)
0201 {
0202 unsigned long als[] = { FACILITIES_ALS };
0203 int i;
0204
0205 for (i = 0; i < ARRAY_SIZE(als); i++) {
0206 if ((stfle_fac_list[i] & als[i]) != als[i]) {
0207 sclp_early_printk("Warning: The Linux kernel requires facilities cleared via command line option\n");
0208 print_missing_facilities();
0209 break;
0210 }
0211 }
0212 }
0213
0214 static void modify_fac_list(char *str)
0215 {
0216 unsigned long val, endval;
0217 char *endp;
0218 bool clear;
0219
0220 while (*str) {
0221 clear = false;
0222 if (*str == '!') {
0223 clear = true;
0224 str++;
0225 }
0226 val = simple_strtoull(str, &endp, 0);
0227 if (str == endp)
0228 break;
0229 str = endp;
0230 if (*str == '-') {
0231 str++;
0232 endval = simple_strtoull(str, &endp, 0);
0233 if (str == endp)
0234 break;
0235 str = endp;
0236 while (val <= endval) {
0237 modify_facility(val, clear);
0238 val++;
0239 }
0240 } else {
0241 modify_facility(val, clear);
0242 }
0243 if (*str != ',')
0244 break;
0245 str++;
0246 }
0247 check_cleared_facilities();
0248 }
0249
0250 static char command_line_buf[COMMAND_LINE_SIZE];
0251 void parse_boot_command_line(void)
0252 {
0253 char *param, *val;
0254 bool enabled;
0255 char *args;
0256 int rc;
0257
0258 kaslr_enabled = IS_ENABLED(CONFIG_RANDOMIZE_BASE);
0259 args = strcpy(command_line_buf, early_command_line);
0260 while (*args) {
0261 args = next_arg(args, ¶m, &val);
0262
0263 if (!strcmp(param, "mem") && val)
0264 memory_limit = round_down(memparse(val, NULL), PAGE_SIZE);
0265
0266 if (!strcmp(param, "vmalloc") && val) {
0267 vmalloc_size = round_up(memparse(val, NULL), PAGE_SIZE);
0268 vmalloc_size_set = 1;
0269 }
0270
0271 if (!strcmp(param, "dfltcc") && val) {
0272 if (!strcmp(val, "off"))
0273 zlib_dfltcc_support = ZLIB_DFLTCC_DISABLED;
0274 else if (!strcmp(val, "on"))
0275 zlib_dfltcc_support = ZLIB_DFLTCC_FULL;
0276 else if (!strcmp(val, "def_only"))
0277 zlib_dfltcc_support = ZLIB_DFLTCC_DEFLATE_ONLY;
0278 else if (!strcmp(val, "inf_only"))
0279 zlib_dfltcc_support = ZLIB_DFLTCC_INFLATE_ONLY;
0280 else if (!strcmp(val, "always"))
0281 zlib_dfltcc_support = ZLIB_DFLTCC_FULL_DEBUG;
0282 }
0283
0284 if (!strcmp(param, "noexec")) {
0285 rc = kstrtobool(val, &enabled);
0286 if (!rc && !enabled)
0287 noexec_disabled = 1;
0288 }
0289
0290 if (!strcmp(param, "facilities") && val)
0291 modify_fac_list(val);
0292
0293 if (!strcmp(param, "nokaslr"))
0294 kaslr_enabled = 0;
0295
0296 #if IS_ENABLED(CONFIG_KVM)
0297 if (!strcmp(param, "prot_virt")) {
0298 rc = kstrtobool(val, &enabled);
0299 if (!rc && enabled)
0300 prot_virt_host = 1;
0301 }
0302 #endif
0303 }
0304 }