early.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. psw.addr = (unsigned long) s390_base_ext_handler;
  139. S390_lowcore.external_new_psw = psw;
  140. psw.addr = (unsigned long) s390_base_pgm_handler;
  141. S390_lowcore.program_new_psw = psw;
  142. s390_base_pgm_handler_fn = early_pgm_check_handler;
  143. S390_lowcore.preempt_count = INIT_PREEMPT_COUNT;
  144. }
  145. static noinline __init void setup_facility_list(void)
  146. {
  147. stfle(S390_lowcore.stfle_fac_list,
  148. ARRAY_SIZE(S390_lowcore.stfle_fac_list));
  149. memcpy(S390_lowcore.alt_stfle_fac_list,
  150. S390_lowcore.stfle_fac_list,
  151. sizeof(S390_lowcore.alt_stfle_fac_list));
  152. if (!IS_ENABLED(CONFIG_KERNEL_NOBP))
  153. __clear_facility(82, S390_lowcore.alt_stfle_fac_list);
  154. }
  155. static __init void detect_diag9c(void)
  156. {
  157. unsigned int cpu_address;
  158. int rc;
  159. cpu_address = stap();
  160. diag_stat_inc(DIAG_STAT_X09C);
  161. asm volatile(
  162. " diag %2,0,0x9c\n"
  163. "0: la %0,0\n"
  164. "1:\n"
  165. EX_TABLE(0b,1b)
  166. : "=d" (rc) : "0" (-EOPNOTSUPP), "d" (cpu_address) : "cc");
  167. if (!rc)
  168. S390_lowcore.machine_flags |= MACHINE_FLAG_DIAG9C;
  169. }
  170. static __init void detect_diag44(void)
  171. {
  172. int rc;
  173. diag_stat_inc(DIAG_STAT_X044);
  174. asm volatile(
  175. " diag 0,0,0x44\n"
  176. "0: la %0,0\n"
  177. "1:\n"
  178. EX_TABLE(0b,1b)
  179. : "=d" (rc) : "0" (-EOPNOTSUPP) : "cc");
  180. if (!rc)
  181. S390_lowcore.machine_flags |= MACHINE_FLAG_DIAG44;
  182. }
  183. static __init void detect_machine_facilities(void)
  184. {
  185. if (test_facility(8)) {
  186. S390_lowcore.machine_flags |= MACHINE_FLAG_EDAT1;
  187. __ctl_set_bit(0, 23);
  188. }
  189. if (test_facility(78))
  190. S390_lowcore.machine_flags |= MACHINE_FLAG_EDAT2;
  191. if (test_facility(3))
  192. S390_lowcore.machine_flags |= MACHINE_FLAG_IDTE;
  193. if (test_facility(50) && test_facility(73)) {
  194. S390_lowcore.machine_flags |= MACHINE_FLAG_TE;
  195. __ctl_set_bit(0, 55);
  196. }
  197. if (test_facility(51))
  198. S390_lowcore.machine_flags |= MACHINE_FLAG_TLB_LC;
  199. if (test_facility(129)) {
  200. S390_lowcore.machine_flags |= MACHINE_FLAG_VX;
  201. __ctl_set_bit(0, 17);
  202. }
  203. if (test_facility(130)) {
  204. S390_lowcore.machine_flags |= MACHINE_FLAG_NX;
  205. __ctl_set_bit(0, 20);
  206. }
  207. if (test_facility(133))
  208. S390_lowcore.machine_flags |= MACHINE_FLAG_GS;
  209. if (test_facility(139) && (tod_clock_base[1] & 0x80)) {
  210. /* Enabled signed clock comparator comparisons */
  211. S390_lowcore.machine_flags |= MACHINE_FLAG_SCC;
  212. clock_comparator_max = -1ULL >> 1;
  213. __ctl_set_bit(0, 53);
  214. }
  215. }
  216. static inline void save_vector_registers(void)
  217. {
  218. #ifdef CONFIG_CRASH_DUMP
  219. if (test_facility(129))
  220. save_vx_regs(boot_cpu_vector_save_area);
  221. #endif
  222. }
  223. static int __init disable_vector_extension(char *str)
  224. {
  225. S390_lowcore.machine_flags &= ~MACHINE_FLAG_VX;
  226. __ctl_clear_bit(0, 17);
  227. return 0;
  228. }
  229. early_param("novx", disable_vector_extension);
  230. static int __init noexec_setup(char *str)
  231. {
  232. bool enabled;
  233. int rc;
  234. rc = kstrtobool(str, &enabled);
  235. if (!rc && !enabled) {
  236. /* Disable no-execute support */
  237. S390_lowcore.machine_flags &= ~MACHINE_FLAG_NX;
  238. __ctl_clear_bit(0, 20);
  239. }
  240. return rc;
  241. }
  242. early_param("noexec", noexec_setup);
  243. static int __init cad_setup(char *str)
  244. {
  245. bool enabled;
  246. int rc;
  247. rc = kstrtobool(str, &enabled);
  248. if (!rc && enabled && test_facility(128))
  249. /* Enable problem state CAD. */
  250. __ctl_set_bit(2, 3);
  251. return rc;
  252. }
  253. early_param("cad", cad_setup);
  254. /* Set up boot command line */
  255. static void __init append_to_cmdline(size_t (*ipl_data)(char *, size_t))
  256. {
  257. char *parm, *delim;
  258. size_t rc, len;
  259. len = strlen(boot_command_line);
  260. delim = boot_command_line + len; /* '\0' character position */
  261. parm = boot_command_line + len + 1; /* append right after '\0' */
  262. rc = ipl_data(parm, COMMAND_LINE_SIZE - len - 1);
  263. if (rc) {
  264. if (*parm == '=')
  265. memmove(boot_command_line, parm + 1, rc);
  266. else
  267. *delim = ' '; /* replace '\0' with space */
  268. }
  269. }
  270. static inline int has_ebcdic_char(const char *str)
  271. {
  272. int i;
  273. for (i = 0; str[i]; i++)
  274. if (str[i] & 0x80)
  275. return 1;
  276. return 0;
  277. }
  278. static void __init setup_boot_command_line(void)
  279. {
  280. COMMAND_LINE[ARCH_COMMAND_LINE_SIZE - 1] = 0;
  281. /* convert arch command line to ascii if necessary */
  282. if (has_ebcdic_char(COMMAND_LINE))
  283. EBCASC(COMMAND_LINE, ARCH_COMMAND_LINE_SIZE);
  284. /* copy arch command line */
  285. strlcpy(boot_command_line, strstrip(COMMAND_LINE),
  286. ARCH_COMMAND_LINE_SIZE);
  287. /* append IPL PARM data to the boot command line */
  288. if (MACHINE_IS_VM)
  289. append_to_cmdline(append_ipl_vmparm);
  290. append_to_cmdline(append_ipl_scpdata);
  291. }
  292. static void __init check_image_bootable(void)
  293. {
  294. if (!memcmp(EP_STRING, (void *)EP_OFFSET, strlen(EP_STRING)))
  295. return;
  296. sclp_early_printk("Linux kernel boot failure: An attempt to boot a vmlinux ELF image failed.\n");
  297. sclp_early_printk("This image does not contain all parts necessary for starting up. Use\n");
  298. sclp_early_printk("bzImage or arch/s390/boot/compressed/vmlinux instead.\n");
  299. disabled_wait(0xbadb007);
  300. }
  301. void __init startup_init(void)
  302. {
  303. check_image_bootable();
  304. time_early_init();
  305. init_kernel_storage_key();
  306. lockdep_off();
  307. setup_lowcore_early();
  308. setup_facility_list();
  309. detect_machine_type();
  310. setup_arch_string();
  311. ipl_store_parameters();
  312. setup_boot_command_line();
  313. detect_diag9c();
  314. detect_diag44();
  315. detect_machine_facilities();
  316. save_vector_registers();
  317. setup_topology();
  318. sclp_early_detect();
  319. lockdep_on();
  320. }