early.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright IBM Corp. 2007, 2009
  4. * Author(s): Hongjie Yang <hongjie@us.ibm.com>,
  5. * Heiko Carstens <heiko.carstens@de.ibm.com>
  6. */
  7. #define KMSG_COMPONENT "setup"
  8. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  9. #include <linux/compiler.h>
  10. #include <linux/init.h>
  11. #include <linux/errno.h>
  12. #include <linux/string.h>
  13. #include <linux/ctype.h>
  14. #include <linux/lockdep.h>
  15. #include <linux/extable.h>
  16. #include <linux/pfn.h>
  17. #include <linux/uaccess.h>
  18. #include <linux/kernel.h>
  19. #include <asm/diag.h>
  20. #include <asm/ebcdic.h>
  21. #include <asm/ipl.h>
  22. #include <asm/lowcore.h>
  23. #include <asm/processor.h>
  24. #include <asm/sections.h>
  25. #include <asm/setup.h>
  26. #include <asm/sysinfo.h>
  27. #include <asm/cpcmd.h>
  28. #include <asm/sclp.h>
  29. #include <asm/facility.h>
  30. #include "entry.h"
  31. static void __init setup_boot_command_line(void);
  32. /*
  33. * Initialize storage key for kernel pages
  34. */
  35. static noinline __init void init_kernel_storage_key(void)
  36. {
  37. #if PAGE_DEFAULT_KEY
  38. unsigned long end_pfn, init_pfn;
  39. end_pfn = PFN_UP(__pa(_end));
  40. for (init_pfn = 0 ; init_pfn < end_pfn; init_pfn++)
  41. page_set_storage_key(init_pfn << PAGE_SHIFT,
  42. PAGE_DEFAULT_KEY, 0);
  43. #endif
  44. }
  45. static __initdata char sysinfo_page[PAGE_SIZE] __aligned(PAGE_SIZE);
  46. static noinline __init void detect_machine_type(void)
  47. {
  48. struct sysinfo_3_2_2 *vmms = (struct sysinfo_3_2_2 *)&sysinfo_page;
  49. /* Check current-configuration-level */
  50. if (stsi(NULL, 0, 0, 0) <= 2) {
  51. S390_lowcore.machine_flags |= MACHINE_FLAG_LPAR;
  52. return;
  53. }
  54. /* Get virtual-machine cpu information. */
  55. if (stsi(vmms, 3, 2, 2) || !vmms->count)
  56. return;
  57. /* Detect known hypervisors */
  58. if (!memcmp(vmms->vm[0].cpi, "\xd2\xe5\xd4", 3))
  59. S390_lowcore.machine_flags |= MACHINE_FLAG_KVM;
  60. else if (!memcmp(vmms->vm[0].cpi, "\xa9\x61\xe5\xd4", 4))
  61. S390_lowcore.machine_flags |= MACHINE_FLAG_VM;
  62. }
  63. /* Remove leading, trailing and double whitespace. */
  64. static inline void strim_all(char *str)
  65. {
  66. char *s;
  67. s = strim(str);
  68. if (s != str)
  69. memmove(str, s, strlen(s));
  70. while (*str) {
  71. if (!isspace(*str++))
  72. continue;
  73. if (isspace(*str)) {
  74. s = skip_spaces(str);
  75. memmove(str, s, strlen(s) + 1);
  76. }
  77. }
  78. }
  79. static noinline __init void setup_arch_string(void)
  80. {
  81. struct sysinfo_1_1_1 *mach = (struct sysinfo_1_1_1 *)&sysinfo_page;
  82. struct sysinfo_3_2_2 *vm = (struct sysinfo_3_2_2 *)&sysinfo_page;
  83. char mstr[80], hvstr[17];
  84. if (stsi(mach, 1, 1, 1))
  85. return;
  86. EBCASC(mach->manufacturer, sizeof(mach->manufacturer));
  87. EBCASC(mach->type, sizeof(mach->type));
  88. EBCASC(mach->model, sizeof(mach->model));
  89. EBCASC(mach->model_capacity, sizeof(mach->model_capacity));
  90. sprintf(mstr, "%-16.16s %-4.4s %-16.16s %-16.16s",
  91. mach->manufacturer, mach->type,
  92. mach->model, mach->model_capacity);
  93. strim_all(mstr);
  94. if (stsi(vm, 3, 2, 2) == 0 && vm->count) {
  95. EBCASC(vm->vm[0].cpi, sizeof(vm->vm[0].cpi));
  96. sprintf(hvstr, "%-16.16s", vm->vm[0].cpi);
  97. strim_all(hvstr);
  98. } else {
  99. sprintf(hvstr, "%s",
  100. MACHINE_IS_LPAR ? "LPAR" :
  101. MACHINE_IS_VM ? "z/VM" :
  102. MACHINE_IS_KVM ? "KVM" : "unknown");
  103. }
  104. dump_stack_set_arch_desc("%s (%s)", mstr, hvstr);
  105. }
  106. static __init void setup_topology(void)
  107. {
  108. int max_mnest;
  109. if (!test_facility(11))
  110. return;
  111. S390_lowcore.machine_flags |= MACHINE_FLAG_TOPOLOGY;
  112. for (max_mnest = 6; max_mnest > 1; max_mnest--) {
  113. if (stsi(&sysinfo_page, 15, 1, max_mnest) == 0)
  114. break;
  115. }
  116. topology_max_mnest = max_mnest;
  117. }
  118. static void early_pgm_check_handler(void)
  119. {
  120. const struct exception_table_entry *fixup;
  121. unsigned long cr0, cr0_new;
  122. unsigned long addr;
  123. addr = S390_lowcore.program_old_psw.addr;
  124. fixup = search_exception_tables(addr);
  125. if (!fixup)
  126. disabled_wait(0);
  127. /* Disable low address protection before storing into lowcore. */
  128. __ctl_store(cr0, 0, 0);
  129. cr0_new = cr0 & ~(1UL << 28);
  130. __ctl_load(cr0_new, 0, 0);
  131. S390_lowcore.program_old_psw.addr = extable_fixup(fixup);
  132. __ctl_load(cr0, 0, 0);
  133. }
  134. static noinline __init void setup_lowcore_early(void)
  135. {
  136. psw_t psw;
  137. psw.mask = PSW_MASK_BASE | PSW_DEFAULT_KEY | PSW_MASK_EA | PSW_MASK_BA;
  138. if (IS_ENABLED(CONFIG_KASAN))
  139. psw.mask |= PSW_MASK_DAT;
  140. psw.addr = (unsigned long) s390_base_ext_handler;
  141. S390_lowcore.external_new_psw = psw;
  142. psw.addr = (unsigned long) s390_base_pgm_handler;
  143. S390_lowcore.program_new_psw = psw;
  144. s390_base_pgm_handler_fn = early_pgm_check_handler;
  145. S390_lowcore.preempt_count = INIT_PREEMPT_COUNT;
  146. }
  147. static noinline __init void setup_facility_list(void)
  148. {
  149. stfle(S390_lowcore.stfle_fac_list,
  150. ARRAY_SIZE(S390_lowcore.stfle_fac_list));
  151. memcpy(S390_lowcore.alt_stfle_fac_list,
  152. S390_lowcore.stfle_fac_list,
  153. sizeof(S390_lowcore.alt_stfle_fac_list));
  154. if (!IS_ENABLED(CONFIG_KERNEL_NOBP))
  155. __clear_facility(82, S390_lowcore.alt_stfle_fac_list);
  156. }
  157. static __init void detect_diag9c(void)
  158. {
  159. unsigned int cpu_address;
  160. int rc;
  161. cpu_address = stap();
  162. diag_stat_inc(DIAG_STAT_X09C);
  163. asm volatile(
  164. " diag %2,0,0x9c\n"
  165. "0: la %0,0\n"
  166. "1:\n"
  167. EX_TABLE(0b,1b)
  168. : "=d" (rc) : "0" (-EOPNOTSUPP), "d" (cpu_address) : "cc");
  169. if (!rc)
  170. S390_lowcore.machine_flags |= MACHINE_FLAG_DIAG9C;
  171. }
  172. static __init void detect_diag44(void)
  173. {
  174. int rc;
  175. diag_stat_inc(DIAG_STAT_X044);
  176. asm volatile(
  177. " diag 0,0,0x44\n"
  178. "0: la %0,0\n"
  179. "1:\n"
  180. EX_TABLE(0b,1b)
  181. : "=d" (rc) : "0" (-EOPNOTSUPP) : "cc");
  182. if (!rc)
  183. S390_lowcore.machine_flags |= MACHINE_FLAG_DIAG44;
  184. }
  185. static __init void detect_machine_facilities(void)
  186. {
  187. if (test_facility(8)) {
  188. S390_lowcore.machine_flags |= MACHINE_FLAG_EDAT1;
  189. __ctl_set_bit(0, 23);
  190. }
  191. if (test_facility(78))
  192. S390_lowcore.machine_flags |= MACHINE_FLAG_EDAT2;
  193. if (test_facility(3))
  194. S390_lowcore.machine_flags |= MACHINE_FLAG_IDTE;
  195. if (test_facility(50) && test_facility(73)) {
  196. S390_lowcore.machine_flags |= MACHINE_FLAG_TE;
  197. __ctl_set_bit(0, 55);
  198. }
  199. if (test_facility(51))
  200. S390_lowcore.machine_flags |= MACHINE_FLAG_TLB_LC;
  201. if (test_facility(129)) {
  202. S390_lowcore.machine_flags |= MACHINE_FLAG_VX;
  203. __ctl_set_bit(0, 17);
  204. }
  205. if (test_facility(130)) {
  206. S390_lowcore.machine_flags |= MACHINE_FLAG_NX;
  207. __ctl_set_bit(0, 20);
  208. }
  209. if (test_facility(133))
  210. S390_lowcore.machine_flags |= MACHINE_FLAG_GS;
  211. if (test_facility(139) && (tod_clock_base[1] & 0x80)) {
  212. /* Enabled signed clock comparator comparisons */
  213. S390_lowcore.machine_flags |= MACHINE_FLAG_SCC;
  214. clock_comparator_max = -1ULL >> 1;
  215. __ctl_set_bit(0, 53);
  216. }
  217. }
  218. static inline void save_vector_registers(void)
  219. {
  220. #ifdef CONFIG_CRASH_DUMP
  221. if (test_facility(129))
  222. save_vx_regs(boot_cpu_vector_save_area);
  223. #endif
  224. }
  225. static int __init disable_vector_extension(char *str)
  226. {
  227. S390_lowcore.machine_flags &= ~MACHINE_FLAG_VX;
  228. __ctl_clear_bit(0, 17);
  229. return 0;
  230. }
  231. early_param("novx", disable_vector_extension);
  232. static int __init noexec_setup(char *str)
  233. {
  234. bool enabled;
  235. int rc;
  236. rc = kstrtobool(str, &enabled);
  237. if (!rc && !enabled) {
  238. /* Disable no-execute support */
  239. S390_lowcore.machine_flags &= ~MACHINE_FLAG_NX;
  240. __ctl_clear_bit(0, 20);
  241. }
  242. return rc;
  243. }
  244. early_param("noexec", noexec_setup);
  245. static int __init cad_setup(char *str)
  246. {
  247. bool enabled;
  248. int rc;
  249. rc = kstrtobool(str, &enabled);
  250. if (!rc && enabled && test_facility(128))
  251. /* Enable problem state CAD. */
  252. __ctl_set_bit(2, 3);
  253. return rc;
  254. }
  255. early_param("cad", cad_setup);
  256. /* Set up boot command line */
  257. static void __init append_to_cmdline(size_t (*ipl_data)(char *, size_t))
  258. {
  259. char *parm, *delim;
  260. size_t rc, len;
  261. len = strlen(boot_command_line);
  262. delim = boot_command_line + len; /* '\0' character position */
  263. parm = boot_command_line + len + 1; /* append right after '\0' */
  264. rc = ipl_data(parm, COMMAND_LINE_SIZE - len - 1);
  265. if (rc) {
  266. if (*parm == '=')
  267. memmove(boot_command_line, parm + 1, rc);
  268. else
  269. *delim = ' '; /* replace '\0' with space */
  270. }
  271. }
  272. static inline int has_ebcdic_char(const char *str)
  273. {
  274. int i;
  275. for (i = 0; str[i]; i++)
  276. if (str[i] & 0x80)
  277. return 1;
  278. return 0;
  279. }
  280. static void __init setup_boot_command_line(void)
  281. {
  282. COMMAND_LINE[ARCH_COMMAND_LINE_SIZE - 1] = 0;
  283. /* convert arch command line to ascii if necessary */
  284. if (has_ebcdic_char(COMMAND_LINE))
  285. EBCASC(COMMAND_LINE, ARCH_COMMAND_LINE_SIZE);
  286. /* copy arch command line */
  287. strlcpy(boot_command_line, strstrip(COMMAND_LINE),
  288. ARCH_COMMAND_LINE_SIZE);
  289. /* append IPL PARM data to the boot command line */
  290. if (MACHINE_IS_VM)
  291. append_to_cmdline(append_ipl_vmparm);
  292. append_to_cmdline(append_ipl_scpdata);
  293. }
  294. static void __init check_image_bootable(void)
  295. {
  296. if (!memcmp(EP_STRING, (void *)EP_OFFSET, strlen(EP_STRING)))
  297. return;
  298. sclp_early_printk("Linux kernel boot failure: An attempt to boot a vmlinux ELF image failed.\n");
  299. sclp_early_printk("This image does not contain all parts necessary for starting up. Use\n");
  300. sclp_early_printk("bzImage or arch/s390/boot/compressed/vmlinux instead.\n");
  301. disabled_wait(0xbadb007);
  302. }
  303. void __init startup_init(void)
  304. {
  305. check_image_bootable();
  306. time_early_init();
  307. init_kernel_storage_key();
  308. lockdep_off();
  309. setup_lowcore_early();
  310. setup_facility_list();
  311. detect_machine_type();
  312. setup_arch_string();
  313. ipl_store_parameters();
  314. setup_boot_command_line();
  315. detect_diag9c();
  316. detect_diag44();
  317. detect_machine_facilities();
  318. save_vector_registers();
  319. setup_topology();
  320. sclp_early_detect();
  321. lockdep_on();
  322. }