ipl.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. /*
  2. * bootloader support
  3. *
  4. * Copyright IBM, Corp. 2012, 2020
  5. *
  6. * Authors:
  7. * Christian Borntraeger <borntraeger@de.ibm.com>
  8. * Janosch Frank <frankja@linux.ibm.com>
  9. *
  10. * This work is licensed under the terms of the GNU GPL, version 2 or (at your
  11. * option) any later version. See the COPYING file in the top-level directory.
  12. *
  13. */
  14. #include "qemu/osdep.h"
  15. #include "qemu/datadir.h"
  16. #include "qapi/error.h"
  17. #include "sysemu/reset.h"
  18. #include "sysemu/runstate.h"
  19. #include "sysemu/tcg.h"
  20. #include "elf.h"
  21. #include "hw/loader.h"
  22. #include "hw/qdev-properties.h"
  23. #include "hw/boards.h"
  24. #include "hw/s390x/virtio-ccw.h"
  25. #include "hw/s390x/vfio-ccw.h"
  26. #include "hw/s390x/css.h"
  27. #include "hw/s390x/ebcdic.h"
  28. #include "hw/s390x/pv.h"
  29. #include "ipl.h"
  30. #include "qemu/error-report.h"
  31. #include "qemu/config-file.h"
  32. #include "qemu/cutils.h"
  33. #include "qemu/option.h"
  34. #include "exec/exec-all.h"
  35. #define KERN_IMAGE_START 0x010000UL
  36. #define LINUX_MAGIC_ADDR 0x010008UL
  37. #define KERN_PARM_AREA_SIZE_ADDR 0x010430UL
  38. #define KERN_PARM_AREA 0x010480UL
  39. #define LEGACY_KERN_PARM_AREA_SIZE 0x000380UL
  40. #define INITRD_START 0x800000UL
  41. #define INITRD_PARM_START 0x010408UL
  42. #define PARMFILE_START 0x001000UL
  43. #define ZIPL_IMAGE_START 0x009000UL
  44. #define IPL_PSW_MASK (PSW_MASK_32 | PSW_MASK_64)
  45. static bool iplb_extended_needed(void *opaque)
  46. {
  47. S390IPLState *ipl = S390_IPL(object_resolve_path(TYPE_S390_IPL, NULL));
  48. return ipl->iplbext_migration;
  49. }
  50. static const VMStateDescription vmstate_iplb_extended = {
  51. .name = "ipl/iplb_extended",
  52. .version_id = 0,
  53. .minimum_version_id = 0,
  54. .needed = iplb_extended_needed,
  55. .fields = (VMStateField[]) {
  56. VMSTATE_UINT8_ARRAY(reserved_ext, IplParameterBlock, 4096 - 200),
  57. VMSTATE_END_OF_LIST()
  58. }
  59. };
  60. static const VMStateDescription vmstate_iplb = {
  61. .name = "ipl/iplb",
  62. .version_id = 0,
  63. .minimum_version_id = 0,
  64. .fields = (VMStateField[]) {
  65. VMSTATE_UINT8_ARRAY(reserved1, IplParameterBlock, 110),
  66. VMSTATE_UINT16(devno, IplParameterBlock),
  67. VMSTATE_UINT8_ARRAY(reserved2, IplParameterBlock, 88),
  68. VMSTATE_END_OF_LIST()
  69. },
  70. .subsections = (const VMStateDescription*[]) {
  71. &vmstate_iplb_extended,
  72. NULL
  73. }
  74. };
  75. static const VMStateDescription vmstate_ipl = {
  76. .name = "ipl",
  77. .version_id = 0,
  78. .minimum_version_id = 0,
  79. .fields = (VMStateField[]) {
  80. VMSTATE_UINT64(compat_start_addr, S390IPLState),
  81. VMSTATE_UINT64(compat_bios_start_addr, S390IPLState),
  82. VMSTATE_STRUCT(iplb, S390IPLState, 0, vmstate_iplb, IplParameterBlock),
  83. VMSTATE_BOOL(iplb_valid, S390IPLState),
  84. VMSTATE_UINT8(cssid, S390IPLState),
  85. VMSTATE_UINT8(ssid, S390IPLState),
  86. VMSTATE_UINT16(devno, S390IPLState),
  87. VMSTATE_END_OF_LIST()
  88. }
  89. };
  90. static S390IPLState *get_ipl_device(void)
  91. {
  92. return S390_IPL(object_resolve_path_type("", TYPE_S390_IPL, NULL));
  93. }
  94. static uint64_t bios_translate_addr(void *opaque, uint64_t srcaddr)
  95. {
  96. uint64_t dstaddr = *(uint64_t *) opaque;
  97. /*
  98. * Assuming that our s390-ccw.img was linked for starting at address 0,
  99. * we can simply add the destination address for the final location
  100. */
  101. return srcaddr + dstaddr;
  102. }
  103. static uint64_t get_max_kernel_cmdline_size(void)
  104. {
  105. uint64_t *size_ptr = rom_ptr(KERN_PARM_AREA_SIZE_ADDR, sizeof(*size_ptr));
  106. if (size_ptr) {
  107. uint64_t size;
  108. size = be64_to_cpu(*size_ptr);
  109. if (size) {
  110. return size;
  111. }
  112. }
  113. return LEGACY_KERN_PARM_AREA_SIZE;
  114. }
  115. static void s390_ipl_realize(DeviceState *dev, Error **errp)
  116. {
  117. MachineState *ms = MACHINE(qdev_get_machine());
  118. S390IPLState *ipl = S390_IPL(dev);
  119. uint32_t *ipl_psw;
  120. uint64_t pentry;
  121. char *magic;
  122. int kernel_size;
  123. int bios_size;
  124. char *bios_filename;
  125. /*
  126. * Always load the bios if it was enforced,
  127. * even if an external kernel has been defined.
  128. */
  129. if (!ipl->kernel || ipl->enforce_bios) {
  130. uint64_t fwbase = (MIN(ms->ram_size, 0x80000000U) - 0x200000) & ~0xffffUL;
  131. bios_filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, ipl->firmware);
  132. if (bios_filename == NULL) {
  133. error_setg(errp, "could not find stage1 bootloader");
  134. return;
  135. }
  136. bios_size = load_elf(bios_filename, NULL,
  137. bios_translate_addr, &fwbase,
  138. &ipl->bios_start_addr, NULL, NULL, NULL, 1,
  139. EM_S390, 0, 0);
  140. if (bios_size > 0) {
  141. /* Adjust ELF start address to final location */
  142. ipl->bios_start_addr += fwbase;
  143. } else {
  144. /* Try to load non-ELF file */
  145. bios_size = load_image_targphys(bios_filename, ZIPL_IMAGE_START,
  146. 4096);
  147. ipl->bios_start_addr = ZIPL_IMAGE_START;
  148. }
  149. g_free(bios_filename);
  150. if (bios_size == -1) {
  151. error_setg(errp, "could not load bootloader '%s'", ipl->firmware);
  152. return;
  153. }
  154. /* default boot target is the bios */
  155. ipl->start_addr = ipl->bios_start_addr;
  156. }
  157. if (ipl->kernel) {
  158. kernel_size = load_elf(ipl->kernel, NULL, NULL, NULL,
  159. &pentry, NULL,
  160. NULL, NULL, 1, EM_S390, 0, 0);
  161. if (kernel_size < 0) {
  162. kernel_size = load_image_targphys(ipl->kernel, 0, ms->ram_size);
  163. if (kernel_size < 0) {
  164. error_setg(errp, "could not load kernel '%s'", ipl->kernel);
  165. return;
  166. }
  167. /* if this is Linux use KERN_IMAGE_START */
  168. magic = rom_ptr(LINUX_MAGIC_ADDR, 6);
  169. if (magic && !memcmp(magic, "S390EP", 6)) {
  170. pentry = KERN_IMAGE_START;
  171. } else {
  172. /* if not Linux load the address of the (short) IPL PSW */
  173. ipl_psw = rom_ptr(4, 4);
  174. if (ipl_psw) {
  175. pentry = be32_to_cpu(*ipl_psw) & PSW_MASK_SHORT_ADDR;
  176. } else {
  177. error_setg(errp, "Could not get IPL PSW");
  178. return;
  179. }
  180. }
  181. }
  182. /*
  183. * Is it a Linux kernel (starting at 0x10000)? If yes, we fill in the
  184. * kernel parameters here as well. Note: For old kernels (up to 3.2)
  185. * we can not rely on the ELF entry point - it was 0x800 (the SALIPL
  186. * loader) and it won't work. For this case we force it to 0x10000, too.
  187. */
  188. if (pentry == KERN_IMAGE_START || pentry == 0x800) {
  189. size_t cmdline_size = strlen(ipl->cmdline) + 1;
  190. char *parm_area = rom_ptr(KERN_PARM_AREA, cmdline_size);
  191. ipl->start_addr = KERN_IMAGE_START;
  192. /* Overwrite parameters in the kernel image, which are "rom" */
  193. if (parm_area) {
  194. uint64_t max_cmdline_size = get_max_kernel_cmdline_size();
  195. if (cmdline_size > max_cmdline_size) {
  196. error_setg(errp,
  197. "kernel command line exceeds maximum size:"
  198. " %zu > %" PRIu64,
  199. cmdline_size, max_cmdline_size);
  200. return;
  201. }
  202. strcpy(parm_area, ipl->cmdline);
  203. }
  204. } else {
  205. ipl->start_addr = pentry;
  206. }
  207. if (ipl->initrd) {
  208. ram_addr_t initrd_offset;
  209. int initrd_size;
  210. uint64_t *romptr;
  211. initrd_offset = INITRD_START;
  212. while (kernel_size + 0x100000 > initrd_offset) {
  213. initrd_offset += 0x100000;
  214. }
  215. initrd_size = load_image_targphys(ipl->initrd, initrd_offset,
  216. ms->ram_size - initrd_offset);
  217. if (initrd_size == -1) {
  218. error_setg(errp, "could not load initrd '%s'", ipl->initrd);
  219. return;
  220. }
  221. /*
  222. * we have to overwrite values in the kernel image,
  223. * which are "rom"
  224. */
  225. romptr = rom_ptr(INITRD_PARM_START, 16);
  226. if (romptr) {
  227. stq_p(romptr, initrd_offset);
  228. stq_p(romptr + 1, initrd_size);
  229. }
  230. }
  231. }
  232. /*
  233. * Don't ever use the migrated values, they could come from a different
  234. * BIOS and therefore don't work. But still migrate the values, so
  235. * QEMUs relying on it don't break.
  236. */
  237. ipl->compat_start_addr = ipl->start_addr;
  238. ipl->compat_bios_start_addr = ipl->bios_start_addr;
  239. /*
  240. * Because this Device is not on any bus in the qbus tree (it is
  241. * not a sysbus device and it's not on some other bus like a PCI
  242. * bus) it will not be automatically reset by the 'reset the
  243. * sysbus' hook registered by vl.c like most devices. So we must
  244. * manually register a reset hook for it.
  245. * TODO: there should be a better way to do this.
  246. */
  247. qemu_register_reset(resettable_cold_reset_fn, dev);
  248. }
  249. static Property s390_ipl_properties[] = {
  250. DEFINE_PROP_STRING("kernel", S390IPLState, kernel),
  251. DEFINE_PROP_STRING("initrd", S390IPLState, initrd),
  252. DEFINE_PROP_STRING("cmdline", S390IPLState, cmdline),
  253. DEFINE_PROP_STRING("firmware", S390IPLState, firmware),
  254. DEFINE_PROP_STRING("netboot_fw", S390IPLState, netboot_fw),
  255. DEFINE_PROP_BOOL("enforce_bios", S390IPLState, enforce_bios, false),
  256. DEFINE_PROP_BOOL("iplbext_migration", S390IPLState, iplbext_migration,
  257. true),
  258. DEFINE_PROP_END_OF_LIST(),
  259. };
  260. static void s390_ipl_set_boot_menu(S390IPLState *ipl)
  261. {
  262. QemuOptsList *plist = qemu_find_opts("boot-opts");
  263. QemuOpts *opts = QTAILQ_FIRST(&plist->head);
  264. const char *tmp;
  265. unsigned long splash_time = 0;
  266. if (!get_boot_device(0)) {
  267. if (boot_menu) {
  268. error_report("boot menu requires a bootindex to be specified for "
  269. "the IPL device");
  270. }
  271. return;
  272. }
  273. switch (ipl->iplb.pbt) {
  274. case S390_IPL_TYPE_CCW:
  275. /* In the absence of -boot menu, use zipl parameters */
  276. if (!qemu_opt_get(opts, "menu")) {
  277. ipl->qipl.qipl_flags |= QIPL_FLAG_BM_OPTS_ZIPL;
  278. return;
  279. }
  280. break;
  281. case S390_IPL_TYPE_QEMU_SCSI:
  282. break;
  283. default:
  284. if (boot_menu) {
  285. error_report("boot menu is not supported for this device type");
  286. }
  287. return;
  288. }
  289. if (!boot_menu) {
  290. return;
  291. }
  292. ipl->qipl.qipl_flags |= QIPL_FLAG_BM_OPTS_CMD;
  293. tmp = qemu_opt_get(opts, "splash-time");
  294. if (tmp && qemu_strtoul(tmp, NULL, 10, &splash_time)) {
  295. error_report("splash-time is invalid, forcing it to 0");
  296. ipl->qipl.boot_menu_timeout = 0;
  297. return;
  298. }
  299. if (splash_time > 0xffffffff) {
  300. error_report("splash-time is too large, forcing it to max value");
  301. ipl->qipl.boot_menu_timeout = 0xffffffff;
  302. return;
  303. }
  304. ipl->qipl.boot_menu_timeout = cpu_to_be32(splash_time);
  305. }
  306. #define CCW_DEVTYPE_NONE 0x00
  307. #define CCW_DEVTYPE_VIRTIO 0x01
  308. #define CCW_DEVTYPE_VIRTIO_NET 0x02
  309. #define CCW_DEVTYPE_SCSI 0x03
  310. #define CCW_DEVTYPE_VFIO 0x04
  311. static CcwDevice *s390_get_ccw_device(DeviceState *dev_st, int *devtype)
  312. {
  313. CcwDevice *ccw_dev = NULL;
  314. int tmp_dt = CCW_DEVTYPE_NONE;
  315. if (dev_st) {
  316. VirtIONet *virtio_net_dev = (VirtIONet *)
  317. object_dynamic_cast(OBJECT(dev_st), TYPE_VIRTIO_NET);
  318. VirtioCcwDevice *virtio_ccw_dev = (VirtioCcwDevice *)
  319. object_dynamic_cast(OBJECT(qdev_get_parent_bus(dev_st)->parent),
  320. TYPE_VIRTIO_CCW_DEVICE);
  321. VFIOCCWDevice *vfio_ccw_dev = (VFIOCCWDevice *)
  322. object_dynamic_cast(OBJECT(dev_st), TYPE_VFIO_CCW);
  323. if (virtio_ccw_dev) {
  324. ccw_dev = CCW_DEVICE(virtio_ccw_dev);
  325. if (virtio_net_dev) {
  326. tmp_dt = CCW_DEVTYPE_VIRTIO_NET;
  327. } else {
  328. tmp_dt = CCW_DEVTYPE_VIRTIO;
  329. }
  330. } else if (vfio_ccw_dev) {
  331. ccw_dev = CCW_DEVICE(vfio_ccw_dev);
  332. tmp_dt = CCW_DEVTYPE_VFIO;
  333. } else {
  334. SCSIDevice *sd = (SCSIDevice *)
  335. object_dynamic_cast(OBJECT(dev_st),
  336. TYPE_SCSI_DEVICE);
  337. if (sd) {
  338. SCSIBus *bus = scsi_bus_from_device(sd);
  339. VirtIOSCSI *vdev = container_of(bus, VirtIOSCSI, bus);
  340. VirtIOSCSICcw *scsi_ccw = container_of(vdev, VirtIOSCSICcw,
  341. vdev);
  342. ccw_dev = (CcwDevice *)object_dynamic_cast(OBJECT(scsi_ccw),
  343. TYPE_CCW_DEVICE);
  344. tmp_dt = CCW_DEVTYPE_SCSI;
  345. }
  346. }
  347. }
  348. if (devtype) {
  349. *devtype = tmp_dt;
  350. }
  351. return ccw_dev;
  352. }
  353. static bool s390_gen_initial_iplb(S390IPLState *ipl)
  354. {
  355. DeviceState *dev_st;
  356. CcwDevice *ccw_dev = NULL;
  357. SCSIDevice *sd;
  358. int devtype;
  359. dev_st = get_boot_device(0);
  360. if (dev_st) {
  361. ccw_dev = s390_get_ccw_device(dev_st, &devtype);
  362. }
  363. /*
  364. * Currently allow IPL only from CCW devices.
  365. */
  366. if (ccw_dev) {
  367. switch (devtype) {
  368. case CCW_DEVTYPE_SCSI:
  369. sd = SCSI_DEVICE(dev_st);
  370. ipl->iplb.len = cpu_to_be32(S390_IPLB_MIN_QEMU_SCSI_LEN);
  371. ipl->iplb.blk0_len =
  372. cpu_to_be32(S390_IPLB_MIN_QEMU_SCSI_LEN - S390_IPLB_HEADER_LEN);
  373. ipl->iplb.pbt = S390_IPL_TYPE_QEMU_SCSI;
  374. ipl->iplb.scsi.lun = cpu_to_be32(sd->lun);
  375. ipl->iplb.scsi.target = cpu_to_be16(sd->id);
  376. ipl->iplb.scsi.channel = cpu_to_be16(sd->channel);
  377. ipl->iplb.scsi.devno = cpu_to_be16(ccw_dev->sch->devno);
  378. ipl->iplb.scsi.ssid = ccw_dev->sch->ssid & 3;
  379. break;
  380. case CCW_DEVTYPE_VFIO:
  381. ipl->iplb.len = cpu_to_be32(S390_IPLB_MIN_CCW_LEN);
  382. ipl->iplb.pbt = S390_IPL_TYPE_CCW;
  383. ipl->iplb.ccw.devno = cpu_to_be16(ccw_dev->sch->devno);
  384. ipl->iplb.ccw.ssid = ccw_dev->sch->ssid & 3;
  385. break;
  386. case CCW_DEVTYPE_VIRTIO_NET:
  387. ipl->netboot = true;
  388. /* Fall through to CCW_DEVTYPE_VIRTIO case */
  389. case CCW_DEVTYPE_VIRTIO:
  390. ipl->iplb.len = cpu_to_be32(S390_IPLB_MIN_CCW_LEN);
  391. ipl->iplb.blk0_len =
  392. cpu_to_be32(S390_IPLB_MIN_CCW_LEN - S390_IPLB_HEADER_LEN);
  393. ipl->iplb.pbt = S390_IPL_TYPE_CCW;
  394. ipl->iplb.ccw.devno = cpu_to_be16(ccw_dev->sch->devno);
  395. ipl->iplb.ccw.ssid = ccw_dev->sch->ssid & 3;
  396. break;
  397. }
  398. if (!s390_ipl_set_loadparm(ipl->iplb.loadparm)) {
  399. ipl->iplb.flags |= DIAG308_FLAGS_LP_VALID;
  400. }
  401. return true;
  402. }
  403. return false;
  404. }
  405. int s390_ipl_set_loadparm(uint8_t *loadparm)
  406. {
  407. MachineState *machine = MACHINE(qdev_get_machine());
  408. char *lp = object_property_get_str(OBJECT(machine), "loadparm", NULL);
  409. if (lp) {
  410. int i;
  411. /* lp is an uppercase string without leading/embedded spaces */
  412. for (i = 0; i < 8 && lp[i]; i++) {
  413. loadparm[i] = ascii2ebcdic[(uint8_t) lp[i]];
  414. }
  415. if (i < 8) {
  416. memset(loadparm + i, 0x40, 8 - i); /* fill with EBCDIC spaces */
  417. }
  418. g_free(lp);
  419. return 0;
  420. }
  421. return -1;
  422. }
  423. static int load_netboot_image(Error **errp)
  424. {
  425. MachineState *ms = MACHINE(qdev_get_machine());
  426. S390IPLState *ipl = get_ipl_device();
  427. char *netboot_filename;
  428. MemoryRegion *sysmem = get_system_memory();
  429. MemoryRegion *mr = NULL;
  430. void *ram_ptr = NULL;
  431. int img_size = -1;
  432. mr = memory_region_find(sysmem, 0, 1).mr;
  433. if (!mr) {
  434. error_setg(errp, "Failed to find memory region at address 0");
  435. return -1;
  436. }
  437. ram_ptr = memory_region_get_ram_ptr(mr);
  438. if (!ram_ptr) {
  439. error_setg(errp, "No RAM found");
  440. goto unref_mr;
  441. }
  442. netboot_filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, ipl->netboot_fw);
  443. if (netboot_filename == NULL) {
  444. error_setg(errp, "Could not find network bootloader '%s'",
  445. ipl->netboot_fw);
  446. goto unref_mr;
  447. }
  448. img_size = load_elf_ram(netboot_filename, NULL, NULL, NULL,
  449. &ipl->start_addr,
  450. NULL, NULL, NULL, 1, EM_S390, 0, 0, NULL,
  451. false);
  452. if (img_size < 0) {
  453. img_size = load_image_size(netboot_filename, ram_ptr, ms->ram_size);
  454. ipl->start_addr = KERN_IMAGE_START;
  455. }
  456. if (img_size < 0) {
  457. error_setg(errp, "Failed to load network bootloader");
  458. }
  459. g_free(netboot_filename);
  460. unref_mr:
  461. memory_region_unref(mr);
  462. return img_size;
  463. }
  464. static bool is_virtio_ccw_device_of_type(IplParameterBlock *iplb,
  465. int virtio_id)
  466. {
  467. uint8_t cssid;
  468. uint8_t ssid;
  469. uint16_t devno;
  470. uint16_t schid;
  471. SubchDev *sch = NULL;
  472. if (iplb->pbt != S390_IPL_TYPE_CCW) {
  473. return false;
  474. }
  475. devno = be16_to_cpu(iplb->ccw.devno);
  476. ssid = iplb->ccw.ssid & 3;
  477. for (schid = 0; schid < MAX_SCHID; schid++) {
  478. for (cssid = 0; cssid < MAX_CSSID; cssid++) {
  479. sch = css_find_subch(1, cssid, ssid, schid);
  480. if (sch && sch->devno == devno) {
  481. return sch->id.cu_model == virtio_id;
  482. }
  483. }
  484. }
  485. return false;
  486. }
  487. static bool is_virtio_net_device(IplParameterBlock *iplb)
  488. {
  489. return is_virtio_ccw_device_of_type(iplb, VIRTIO_ID_NET);
  490. }
  491. static bool is_virtio_scsi_device(IplParameterBlock *iplb)
  492. {
  493. return is_virtio_ccw_device_of_type(iplb, VIRTIO_ID_SCSI);
  494. }
  495. static void update_machine_ipl_properties(IplParameterBlock *iplb)
  496. {
  497. Object *machine = qdev_get_machine();
  498. Error *err = NULL;
  499. /* Sync loadparm */
  500. if (iplb->flags & DIAG308_FLAGS_LP_VALID) {
  501. uint8_t *ebcdic_loadparm = iplb->loadparm;
  502. char ascii_loadparm[9];
  503. int i;
  504. for (i = 0; i < 8 && ebcdic_loadparm[i]; i++) {
  505. ascii_loadparm[i] = ebcdic2ascii[(uint8_t) ebcdic_loadparm[i]];
  506. }
  507. ascii_loadparm[i] = 0;
  508. object_property_set_str(machine, "loadparm", ascii_loadparm, &err);
  509. } else {
  510. object_property_set_str(machine, "loadparm", "", &err);
  511. }
  512. if (err) {
  513. warn_report_err(err);
  514. }
  515. }
  516. void s390_ipl_update_diag308(IplParameterBlock *iplb)
  517. {
  518. S390IPLState *ipl = get_ipl_device();
  519. /*
  520. * The IPLB set and retrieved by subcodes 8/9 is completely
  521. * separate from the one managed via subcodes 5/6.
  522. */
  523. if (iplb->pbt == S390_IPL_TYPE_PV) {
  524. ipl->iplb_pv = *iplb;
  525. ipl->iplb_valid_pv = true;
  526. } else {
  527. ipl->iplb = *iplb;
  528. ipl->iplb_valid = true;
  529. }
  530. ipl->netboot = is_virtio_net_device(iplb);
  531. update_machine_ipl_properties(iplb);
  532. }
  533. IplParameterBlock *s390_ipl_get_iplb_pv(void)
  534. {
  535. S390IPLState *ipl = get_ipl_device();
  536. if (!ipl->iplb_valid_pv) {
  537. return NULL;
  538. }
  539. return &ipl->iplb_pv;
  540. }
  541. IplParameterBlock *s390_ipl_get_iplb(void)
  542. {
  543. S390IPLState *ipl = get_ipl_device();
  544. if (!ipl->iplb_valid) {
  545. return NULL;
  546. }
  547. return &ipl->iplb;
  548. }
  549. void s390_ipl_reset_request(CPUState *cs, enum s390_reset reset_type)
  550. {
  551. S390IPLState *ipl = get_ipl_device();
  552. if (reset_type == S390_RESET_EXTERNAL || reset_type == S390_RESET_REIPL) {
  553. /* use CPU 0 for full resets */
  554. ipl->reset_cpu_index = 0;
  555. } else {
  556. ipl->reset_cpu_index = cs->cpu_index;
  557. }
  558. ipl->reset_type = reset_type;
  559. if (reset_type == S390_RESET_REIPL &&
  560. ipl->iplb_valid &&
  561. !ipl->netboot &&
  562. ipl->iplb.pbt == S390_IPL_TYPE_CCW &&
  563. is_virtio_scsi_device(&ipl->iplb)) {
  564. CcwDevice *ccw_dev = s390_get_ccw_device(get_boot_device(0), NULL);
  565. if (ccw_dev &&
  566. cpu_to_be16(ccw_dev->sch->devno) == ipl->iplb.ccw.devno &&
  567. (ccw_dev->sch->ssid & 3) == ipl->iplb.ccw.ssid) {
  568. /*
  569. * this is the original boot device's SCSI
  570. * so restore IPL parameter info from it
  571. */
  572. ipl->iplb_valid = s390_gen_initial_iplb(ipl);
  573. }
  574. }
  575. if (reset_type == S390_RESET_MODIFIED_CLEAR ||
  576. reset_type == S390_RESET_LOAD_NORMAL ||
  577. reset_type == S390_RESET_PV) {
  578. /* ignore -no-reboot, send no event */
  579. qemu_system_reset_request(SHUTDOWN_CAUSE_SUBSYSTEM_RESET);
  580. } else {
  581. qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
  582. }
  583. /* as this is triggered by a CPU, make sure to exit the loop */
  584. if (tcg_enabled()) {
  585. cpu_loop_exit(cs);
  586. }
  587. }
  588. void s390_ipl_get_reset_request(CPUState **cs, enum s390_reset *reset_type)
  589. {
  590. S390IPLState *ipl = get_ipl_device();
  591. *cs = qemu_get_cpu(ipl->reset_cpu_index);
  592. if (!*cs) {
  593. /* use any CPU */
  594. *cs = first_cpu;
  595. }
  596. *reset_type = ipl->reset_type;
  597. }
  598. void s390_ipl_clear_reset_request(void)
  599. {
  600. S390IPLState *ipl = get_ipl_device();
  601. ipl->reset_type = S390_RESET_EXTERNAL;
  602. /* use CPU 0 for full resets */
  603. ipl->reset_cpu_index = 0;
  604. }
  605. static void s390_ipl_prepare_qipl(S390CPU *cpu)
  606. {
  607. S390IPLState *ipl = get_ipl_device();
  608. uint8_t *addr;
  609. uint64_t len = 4096;
  610. addr = cpu_physical_memory_map(cpu->env.psa, &len, true);
  611. if (!addr || len < QIPL_ADDRESS + sizeof(QemuIplParameters)) {
  612. error_report("Cannot set QEMU IPL parameters");
  613. return;
  614. }
  615. memcpy(addr + QIPL_ADDRESS, &ipl->qipl, sizeof(QemuIplParameters));
  616. cpu_physical_memory_unmap(addr, len, 1, len);
  617. }
  618. int s390_ipl_prepare_pv_header(void)
  619. {
  620. IplParameterBlock *ipib = s390_ipl_get_iplb_pv();
  621. IPLBlockPV *ipib_pv = &ipib->pv;
  622. void *hdr = g_malloc(ipib_pv->pv_header_len);
  623. int rc;
  624. cpu_physical_memory_read(ipib_pv->pv_header_addr, hdr,
  625. ipib_pv->pv_header_len);
  626. rc = s390_pv_set_sec_parms((uintptr_t)hdr,
  627. ipib_pv->pv_header_len);
  628. g_free(hdr);
  629. return rc;
  630. }
  631. int s390_ipl_pv_unpack(void)
  632. {
  633. IplParameterBlock *ipib = s390_ipl_get_iplb_pv();
  634. IPLBlockPV *ipib_pv = &ipib->pv;
  635. int i, rc = 0;
  636. for (i = 0; i < ipib_pv->num_comp; i++) {
  637. rc = s390_pv_unpack(ipib_pv->components[i].addr,
  638. TARGET_PAGE_ALIGN(ipib_pv->components[i].size),
  639. ipib_pv->components[i].tweak_pref);
  640. if (rc) {
  641. break;
  642. }
  643. }
  644. return rc;
  645. }
  646. void s390_ipl_prepare_cpu(S390CPU *cpu)
  647. {
  648. S390IPLState *ipl = get_ipl_device();
  649. cpu->env.psw.addr = ipl->start_addr;
  650. cpu->env.psw.mask = IPL_PSW_MASK;
  651. if (!ipl->kernel || ipl->iplb_valid) {
  652. cpu->env.psw.addr = ipl->bios_start_addr;
  653. if (!ipl->iplb_valid) {
  654. ipl->iplb_valid = s390_gen_initial_iplb(ipl);
  655. }
  656. }
  657. if (ipl->netboot) {
  658. load_netboot_image(&error_fatal);
  659. ipl->qipl.netboot_start_addr = cpu_to_be64(ipl->start_addr);
  660. }
  661. s390_ipl_set_boot_menu(ipl);
  662. s390_ipl_prepare_qipl(cpu);
  663. }
  664. static void s390_ipl_reset(DeviceState *dev)
  665. {
  666. S390IPLState *ipl = S390_IPL(dev);
  667. if (ipl->reset_type != S390_RESET_REIPL) {
  668. ipl->iplb_valid = false;
  669. memset(&ipl->iplb, 0, sizeof(IplParameterBlock));
  670. }
  671. }
  672. static void s390_ipl_class_init(ObjectClass *klass, void *data)
  673. {
  674. DeviceClass *dc = DEVICE_CLASS(klass);
  675. dc->realize = s390_ipl_realize;
  676. device_class_set_props(dc, s390_ipl_properties);
  677. dc->reset = s390_ipl_reset;
  678. dc->vmsd = &vmstate_ipl;
  679. set_bit(DEVICE_CATEGORY_MISC, dc->categories);
  680. /* Reason: Loads the ROMs and thus can only be used one time - internally */
  681. dc->user_creatable = false;
  682. }
  683. static const TypeInfo s390_ipl_info = {
  684. .class_init = s390_ipl_class_init,
  685. .parent = TYPE_DEVICE,
  686. .name = TYPE_S390_IPL,
  687. .instance_size = sizeof(S390IPLState),
  688. };
  689. static void s390_ipl_register_types(void)
  690. {
  691. type_register_static(&s390_ipl_info);
  692. }
  693. type_init(s390_ipl_register_types)