ipl_parm.c 6.9 KB

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