ipl_parm.c 6.8 KB

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