ipl_parm.c 7.2 KB

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