early.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. * Get the TOD clock running.
  34. */
  35. static void __init reset_tod_clock(void)
  36. {
  37. u64 time;
  38. if (store_tod_clock(&time) == 0)
  39. return;
  40. /* TOD clock not running. Set the clock to Unix Epoch. */
  41. if (set_tod_clock(TOD_UNIX_EPOCH) != 0 || store_tod_clock(&time) != 0)
  42. disabled_wait(0);
  43. memset(tod_clock_base, 0, 16);
  44. *(__u64 *) &tod_clock_base[1] = TOD_UNIX_EPOCH;
  45. S390_lowcore.last_update_clock = TOD_UNIX_EPOCH;
  46. }
  47. /*
  48. * Clear bss memory
  49. */
  50. static noinline __init void clear_bss_section(void)
  51. {
  52. memset(__bss_start, 0, __bss_stop - __bss_start);
  53. }
  54. /*
  55. * Initialize storage key for kernel pages
  56. */
  57. static noinline __init void init_kernel_storage_key(void)
  58. {
  59. #if PAGE_DEFAULT_KEY
  60. unsigned long end_pfn, init_pfn;
  61. end_pfn = PFN_UP(__pa(&_end));
  62. for (init_pfn = 0 ; init_pfn < end_pfn; init_pfn++)
  63. page_set_storage_key(init_pfn << PAGE_SHIFT,
  64. PAGE_DEFAULT_KEY, 0);
  65. #endif
  66. }
  67. static __initdata char sysinfo_page[PAGE_SIZE] __aligned(PAGE_SIZE);
  68. static noinline __init void detect_machine_type(void)
  69. {
  70. struct sysinfo_3_2_2 *vmms = (struct sysinfo_3_2_2 *)&sysinfo_page;
  71. /* Check current-configuration-level */
  72. if (stsi(NULL, 0, 0, 0) <= 2) {
  73. S390_lowcore.machine_flags |= MACHINE_FLAG_LPAR;
  74. return;
  75. }
  76. /* Get virtual-machine cpu information. */
  77. if (stsi(vmms, 3, 2, 2) || !vmms->count)
  78. return;
  79. /* Running under KVM? If not we assume z/VM */
  80. if (!memcmp(vmms->vm[0].cpi, "\xd2\xe5\xd4", 3))
  81. S390_lowcore.machine_flags |= MACHINE_FLAG_KVM;
  82. else
  83. S390_lowcore.machine_flags |= MACHINE_FLAG_VM;
  84. }
  85. /* Remove leading, trailing and double whitespace. */
  86. static inline void strim_all(char *str)
  87. {
  88. char *s;
  89. s = strim(str);
  90. if (s != str)
  91. memmove(str, s, strlen(s));
  92. while (*str) {
  93. if (!isspace(*str++))
  94. continue;
  95. if (isspace(*str)) {
  96. s = skip_spaces(str);
  97. memmove(str, s, strlen(s) + 1);
  98. }
  99. }
  100. }
  101. static noinline __init void setup_arch_string(void)
  102. {
  103. struct sysinfo_1_1_1 *mach = (struct sysinfo_1_1_1 *)&sysinfo_page;
  104. struct sysinfo_3_2_2 *vm = (struct sysinfo_3_2_2 *)&sysinfo_page;
  105. char mstr[80], hvstr[17];
  106. if (stsi(mach, 1, 1, 1))
  107. return;
  108. EBCASC(mach->manufacturer, sizeof(mach->manufacturer));
  109. EBCASC(mach->type, sizeof(mach->type));
  110. EBCASC(mach->model, sizeof(mach->model));
  111. EBCASC(mach->model_capacity, sizeof(mach->model_capacity));
  112. sprintf(mstr, "%-16.16s %-4.4s %-16.16s %-16.16s",
  113. mach->manufacturer, mach->type,
  114. mach->model, mach->model_capacity);
  115. strim_all(mstr);
  116. if (stsi(vm, 3, 2, 2) == 0 && vm->count) {
  117. EBCASC(vm->vm[0].cpi, sizeof(vm->vm[0].cpi));
  118. sprintf(hvstr, "%-16.16s", vm->vm[0].cpi);
  119. strim_all(hvstr);
  120. } else {
  121. sprintf(hvstr, "%s",
  122. MACHINE_IS_LPAR ? "LPAR" :
  123. MACHINE_IS_VM ? "z/VM" :
  124. MACHINE_IS_KVM ? "KVM" : "unknown");
  125. }
  126. dump_stack_set_arch_desc("%s (%s)", mstr, hvstr);
  127. }
  128. static __init void setup_topology(void)
  129. {
  130. int max_mnest;
  131. if (!test_facility(11))
  132. return;
  133. S390_lowcore.machine_flags |= MACHINE_FLAG_TOPOLOGY;
  134. for (max_mnest = 6; max_mnest > 1; max_mnest--) {
  135. if (stsi(&sysinfo_page, 15, 1, max_mnest) == 0)
  136. break;
  137. }
  138. topology_max_mnest = max_mnest;
  139. }
  140. static void early_pgm_check_handler(void)
  141. {
  142. const struct exception_table_entry *fixup;
  143. unsigned long cr0, cr0_new;
  144. unsigned long addr;
  145. addr = S390_lowcore.program_old_psw.addr;
  146. fixup = search_exception_tables(addr);
  147. if (!fixup)
  148. disabled_wait(0);
  149. /* Disable low address protection before storing into lowcore. */
  150. __ctl_store(cr0, 0, 0);
  151. cr0_new = cr0 & ~(1UL << 28);
  152. __ctl_load(cr0_new, 0, 0);
  153. S390_lowcore.program_old_psw.addr = extable_fixup(fixup);
  154. __ctl_load(cr0, 0, 0);
  155. }
  156. static noinline __init void setup_lowcore_early(void)
  157. {
  158. psw_t psw;
  159. psw.mask = PSW_MASK_BASE | PSW_DEFAULT_KEY | PSW_MASK_EA | PSW_MASK_BA;
  160. psw.addr = (unsigned long) s390_base_ext_handler;
  161. S390_lowcore.external_new_psw = psw;
  162. psw.addr = (unsigned long) s390_base_pgm_handler;
  163. S390_lowcore.program_new_psw = psw;
  164. s390_base_pgm_handler_fn = early_pgm_check_handler;
  165. S390_lowcore.preempt_count = INIT_PREEMPT_COUNT;
  166. }
  167. static noinline __init void setup_facility_list(void)
  168. {
  169. stfle(S390_lowcore.stfle_fac_list,
  170. ARRAY_SIZE(S390_lowcore.stfle_fac_list));
  171. memcpy(S390_lowcore.alt_stfle_fac_list,
  172. S390_lowcore.stfle_fac_list,
  173. sizeof(S390_lowcore.alt_stfle_fac_list));
  174. if (!IS_ENABLED(CONFIG_KERNEL_NOBP))
  175. __clear_facility(82, S390_lowcore.alt_stfle_fac_list);
  176. }
  177. static __init void detect_diag9c(void)
  178. {
  179. unsigned int cpu_address;
  180. int rc;
  181. cpu_address = stap();
  182. diag_stat_inc(DIAG_STAT_X09C);
  183. asm volatile(
  184. " diag %2,0,0x9c\n"
  185. "0: la %0,0\n"
  186. "1:\n"
  187. EX_TABLE(0b,1b)
  188. : "=d" (rc) : "0" (-EOPNOTSUPP), "d" (cpu_address) : "cc");
  189. if (!rc)
  190. S390_lowcore.machine_flags |= MACHINE_FLAG_DIAG9C;
  191. }
  192. static __init void detect_diag44(void)
  193. {
  194. int rc;
  195. diag_stat_inc(DIAG_STAT_X044);
  196. asm volatile(
  197. " diag 0,0,0x44\n"
  198. "0: la %0,0\n"
  199. "1:\n"
  200. EX_TABLE(0b,1b)
  201. : "=d" (rc) : "0" (-EOPNOTSUPP) : "cc");
  202. if (!rc)
  203. S390_lowcore.machine_flags |= MACHINE_FLAG_DIAG44;
  204. }
  205. static __init void detect_machine_facilities(void)
  206. {
  207. if (test_facility(8)) {
  208. S390_lowcore.machine_flags |= MACHINE_FLAG_EDAT1;
  209. __ctl_set_bit(0, 23);
  210. }
  211. if (test_facility(78))
  212. S390_lowcore.machine_flags |= MACHINE_FLAG_EDAT2;
  213. if (test_facility(3))
  214. S390_lowcore.machine_flags |= MACHINE_FLAG_IDTE;
  215. if (test_facility(40))
  216. S390_lowcore.machine_flags |= MACHINE_FLAG_LPP;
  217. if (test_facility(50) && test_facility(73)) {
  218. S390_lowcore.machine_flags |= MACHINE_FLAG_TE;
  219. __ctl_set_bit(0, 55);
  220. }
  221. if (test_facility(51))
  222. S390_lowcore.machine_flags |= MACHINE_FLAG_TLB_LC;
  223. if (test_facility(129)) {
  224. S390_lowcore.machine_flags |= MACHINE_FLAG_VX;
  225. __ctl_set_bit(0, 17);
  226. }
  227. if (test_facility(130)) {
  228. S390_lowcore.machine_flags |= MACHINE_FLAG_NX;
  229. __ctl_set_bit(0, 20);
  230. }
  231. if (test_facility(133))
  232. S390_lowcore.machine_flags |= MACHINE_FLAG_GS;
  233. if (test_facility(139) && (tod_clock_base[1] & 0x80)) {
  234. /* Enabled signed clock comparator comparisons */
  235. S390_lowcore.machine_flags |= MACHINE_FLAG_SCC;
  236. clock_comparator_max = -1ULL >> 1;
  237. __ctl_set_bit(0, 53);
  238. }
  239. }
  240. static inline void save_vector_registers(void)
  241. {
  242. #ifdef CONFIG_CRASH_DUMP
  243. if (test_facility(129))
  244. save_vx_regs(boot_cpu_vector_save_area);
  245. #endif
  246. }
  247. static int __init disable_vector_extension(char *str)
  248. {
  249. S390_lowcore.machine_flags &= ~MACHINE_FLAG_VX;
  250. __ctl_clear_bit(0, 17);
  251. return 0;
  252. }
  253. early_param("novx", disable_vector_extension);
  254. static int __init noexec_setup(char *str)
  255. {
  256. bool enabled;
  257. int rc;
  258. rc = kstrtobool(str, &enabled);
  259. if (!rc && !enabled) {
  260. /* Disable no-execute support */
  261. S390_lowcore.machine_flags &= ~MACHINE_FLAG_NX;
  262. __ctl_clear_bit(0, 20);
  263. }
  264. return rc;
  265. }
  266. early_param("noexec", noexec_setup);
  267. static int __init cad_setup(char *str)
  268. {
  269. bool enabled;
  270. int rc;
  271. rc = kstrtobool(str, &enabled);
  272. if (!rc && enabled && test_facility(128))
  273. /* Enable problem state CAD. */
  274. __ctl_set_bit(2, 3);
  275. return rc;
  276. }
  277. early_param("cad", cad_setup);
  278. static __init void memmove_early(void *dst, const void *src, size_t n)
  279. {
  280. unsigned long addr;
  281. long incr;
  282. psw_t old;
  283. if (!n)
  284. return;
  285. incr = 1;
  286. if (dst > src) {
  287. incr = -incr;
  288. dst += n - 1;
  289. src += n - 1;
  290. }
  291. old = S390_lowcore.program_new_psw;
  292. S390_lowcore.program_new_psw.mask = __extract_psw();
  293. asm volatile(
  294. " larl %[addr],1f\n"
  295. " stg %[addr],%[psw_pgm_addr]\n"
  296. "0: mvc 0(1,%[dst]),0(%[src])\n"
  297. " agr %[dst],%[incr]\n"
  298. " agr %[src],%[incr]\n"
  299. " brctg %[n],0b\n"
  300. "1:\n"
  301. : [addr] "=&d" (addr),
  302. [psw_pgm_addr] "=Q" (S390_lowcore.program_new_psw.addr),
  303. [dst] "+&a" (dst), [src] "+&a" (src), [n] "+d" (n)
  304. : [incr] "d" (incr)
  305. : "cc", "memory");
  306. S390_lowcore.program_new_psw = old;
  307. }
  308. static __init noinline void ipl_save_parameters(void)
  309. {
  310. void *src, *dst;
  311. src = (void *)(unsigned long) S390_lowcore.ipl_parmblock_ptr;
  312. dst = (void *) IPL_PARMBLOCK_ORIGIN;
  313. memmove_early(dst, src, PAGE_SIZE);
  314. S390_lowcore.ipl_parmblock_ptr = IPL_PARMBLOCK_ORIGIN;
  315. }
  316. static __init noinline void rescue_initrd(void)
  317. {
  318. #ifdef CONFIG_BLK_DEV_INITRD
  319. unsigned long min_initrd_addr = (unsigned long) _end + (4UL << 20);
  320. /*
  321. * Just like in case of IPL from VM reader we make sure there is a
  322. * gap of 4MB between end of kernel and start of initrd.
  323. * That way we can also be sure that saving an NSS will succeed,
  324. * which however only requires different segments.
  325. */
  326. if (!INITRD_START || !INITRD_SIZE)
  327. return;
  328. if (INITRD_START >= min_initrd_addr)
  329. return;
  330. memmove_early((void *) min_initrd_addr, (void *) INITRD_START, INITRD_SIZE);
  331. INITRD_START = min_initrd_addr;
  332. #endif
  333. }
  334. /* Set up boot command line */
  335. static void __init append_to_cmdline(size_t (*ipl_data)(char *, size_t))
  336. {
  337. char *parm, *delim;
  338. size_t rc, len;
  339. len = strlen(boot_command_line);
  340. delim = boot_command_line + len; /* '\0' character position */
  341. parm = boot_command_line + len + 1; /* append right after '\0' */
  342. rc = ipl_data(parm, COMMAND_LINE_SIZE - len - 1);
  343. if (rc) {
  344. if (*parm == '=')
  345. memmove(boot_command_line, parm + 1, rc);
  346. else
  347. *delim = ' '; /* replace '\0' with space */
  348. }
  349. }
  350. static inline int has_ebcdic_char(const char *str)
  351. {
  352. int i;
  353. for (i = 0; str[i]; i++)
  354. if (str[i] & 0x80)
  355. return 1;
  356. return 0;
  357. }
  358. static void __init setup_boot_command_line(void)
  359. {
  360. COMMAND_LINE[ARCH_COMMAND_LINE_SIZE - 1] = 0;
  361. /* convert arch command line to ascii if necessary */
  362. if (has_ebcdic_char(COMMAND_LINE))
  363. EBCASC(COMMAND_LINE, ARCH_COMMAND_LINE_SIZE);
  364. /* copy arch command line */
  365. strlcpy(boot_command_line, strstrip(COMMAND_LINE),
  366. ARCH_COMMAND_LINE_SIZE);
  367. /* append IPL PARM data to the boot command line */
  368. if (MACHINE_IS_VM)
  369. append_to_cmdline(append_ipl_vmparm);
  370. append_to_cmdline(append_ipl_scpdata);
  371. }
  372. void __init startup_init(void)
  373. {
  374. reset_tod_clock();
  375. ipl_save_parameters();
  376. rescue_initrd();
  377. clear_bss_section();
  378. ipl_verify_parameters();
  379. time_early_init();
  380. init_kernel_storage_key();
  381. lockdep_off();
  382. setup_lowcore_early();
  383. setup_facility_list();
  384. detect_machine_type();
  385. setup_arch_string();
  386. ipl_update_parameters();
  387. setup_boot_command_line();
  388. detect_diag9c();
  389. detect_diag44();
  390. detect_machine_facilities();
  391. save_vector_registers();
  392. setup_topology();
  393. sclp_early_detect();
  394. lockdep_on();
  395. }