s390-skeys.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /*
  2. * s390 storage key device
  3. *
  4. * Copyright 2015 IBM Corp.
  5. * Author(s): Jason J. Herne <jjherne@linux.vnet.ibm.com>
  6. *
  7. * This work is licensed under the terms of the GNU GPL, version 2 or (at
  8. * your option) any later version. See the COPYING file in the top-level
  9. * directory.
  10. */
  11. #include "qemu/osdep.h"
  12. #include "qemu/units.h"
  13. #include "hw/s390x/s390-virtio-ccw.h"
  14. #include "hw/qdev-properties.h"
  15. #include "hw/s390x/storage-keys.h"
  16. #include "qapi/error.h"
  17. #include "qapi/qapi-commands-misc-target.h"
  18. #include "qobject/qdict.h"
  19. #include "qemu/error-report.h"
  20. #include "system/memory_mapping.h"
  21. #include "exec/address-spaces.h"
  22. #include "system/kvm.h"
  23. #include "migration/qemu-file-types.h"
  24. #include "migration/register.h"
  25. #include "trace.h"
  26. #define S390_SKEYS_BUFFER_SIZE (128 * KiB) /* Room for 128k storage keys */
  27. #define S390_SKEYS_SAVE_FLAG_EOS 0x01
  28. #define S390_SKEYS_SAVE_FLAG_SKEYS 0x02
  29. #define S390_SKEYS_SAVE_FLAG_ERROR 0x04
  30. S390SKeysState *s390_get_skeys_device(void)
  31. {
  32. S390SKeysState *ss;
  33. ss = S390_SKEYS(object_resolve_path_type("", TYPE_S390_SKEYS, NULL));
  34. assert(ss);
  35. return ss;
  36. }
  37. void s390_skeys_init(void)
  38. {
  39. Object *obj;
  40. if (kvm_enabled()) {
  41. obj = object_new(TYPE_KVM_S390_SKEYS);
  42. } else {
  43. obj = object_new(TYPE_QEMU_S390_SKEYS);
  44. }
  45. object_property_add_child(qdev_get_machine(), TYPE_S390_SKEYS,
  46. obj);
  47. object_unref(obj);
  48. qdev_realize(DEVICE(obj), NULL, &error_fatal);
  49. }
  50. int s390_skeys_get(S390SKeysState *ks, uint64_t start_gfn,
  51. uint64_t count, uint8_t *keys)
  52. {
  53. S390SKeysClass *kc = S390_SKEYS_GET_CLASS(ks);
  54. int rc;
  55. rc = kc->get_skeys(ks, start_gfn, count, keys);
  56. if (rc) {
  57. trace_s390_skeys_get_nonzero(rc);
  58. }
  59. return rc;
  60. }
  61. int s390_skeys_set(S390SKeysState *ks, uint64_t start_gfn,
  62. uint64_t count, uint8_t *keys)
  63. {
  64. S390SKeysClass *kc = S390_SKEYS_GET_CLASS(ks);
  65. int rc;
  66. rc = kc->set_skeys(ks, start_gfn, count, keys);
  67. if (rc) {
  68. trace_s390_skeys_set_nonzero(rc);
  69. }
  70. return rc;
  71. }
  72. static void write_keys(FILE *f, uint8_t *keys, uint64_t startgfn,
  73. uint64_t count, Error **errp)
  74. {
  75. uint64_t curpage = startgfn;
  76. uint64_t maxpage = curpage + count - 1;
  77. for (; curpage <= maxpage; curpage++) {
  78. uint8_t acc = (*keys & 0xF0) >> 4;
  79. int fp = (*keys & 0x08);
  80. int ref = (*keys & 0x04);
  81. int ch = (*keys & 0x02);
  82. int res = (*keys & 0x01);
  83. fprintf(f, "page=%03" PRIx64 ": key(%d) => ACC=%X, FP=%d, REF=%d,"
  84. " ch=%d, reserved=%d\n",
  85. curpage, *keys, acc, fp, ref, ch, res);
  86. keys++;
  87. }
  88. }
  89. void hmp_info_skeys(Monitor *mon, const QDict *qdict)
  90. {
  91. S390SKeysState *ss = s390_get_skeys_device();
  92. S390SKeysClass *skeyclass = S390_SKEYS_GET_CLASS(ss);
  93. uint64_t addr = qdict_get_int(qdict, "addr");
  94. uint8_t key;
  95. int r;
  96. /* Quick check to see if guest is using storage keys*/
  97. if (!skeyclass->skeys_are_enabled(ss)) {
  98. monitor_printf(mon, "Error: This guest is not using storage keys\n");
  99. return;
  100. }
  101. if (!address_space_access_valid(&address_space_memory,
  102. addr & TARGET_PAGE_MASK, TARGET_PAGE_SIZE,
  103. false, MEMTXATTRS_UNSPECIFIED)) {
  104. monitor_printf(mon, "Error: The given address is not valid\n");
  105. return;
  106. }
  107. r = skeyclass->get_skeys(ss, addr / TARGET_PAGE_SIZE, 1, &key);
  108. if (r < 0) {
  109. monitor_printf(mon, "Error: %s\n", strerror(-r));
  110. return;
  111. }
  112. monitor_printf(mon, " key: 0x%X\n", key);
  113. }
  114. void hmp_dump_skeys(Monitor *mon, const QDict *qdict)
  115. {
  116. const char *filename = qdict_get_str(qdict, "filename");
  117. Error *err = NULL;
  118. qmp_dump_skeys(filename, &err);
  119. if (err) {
  120. error_report_err(err);
  121. }
  122. }
  123. void qmp_dump_skeys(const char *filename, Error **errp)
  124. {
  125. S390SKeysState *ss = s390_get_skeys_device();
  126. S390SKeysClass *skeyclass = S390_SKEYS_GET_CLASS(ss);
  127. GuestPhysBlockList guest_phys_blocks;
  128. GuestPhysBlock *block;
  129. uint64_t pages, gfn;
  130. Error *lerr = NULL;
  131. uint8_t *buf;
  132. int ret;
  133. int fd;
  134. FILE *f;
  135. /* Quick check to see if guest is using storage keys*/
  136. if (!skeyclass->skeys_are_enabled(ss)) {
  137. error_setg(errp, "This guest is not using storage keys - "
  138. "nothing to dump");
  139. return;
  140. }
  141. fd = qemu_open_old(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
  142. if (fd < 0) {
  143. error_setg_file_open(errp, errno, filename);
  144. return;
  145. }
  146. f = fdopen(fd, "wb");
  147. if (!f) {
  148. close(fd);
  149. error_setg_file_open(errp, errno, filename);
  150. return;
  151. }
  152. buf = g_try_malloc(S390_SKEYS_BUFFER_SIZE);
  153. if (!buf) {
  154. error_setg(errp, "Could not allocate memory");
  155. goto out;
  156. }
  157. assert(bql_locked());
  158. guest_phys_blocks_init(&guest_phys_blocks);
  159. guest_phys_blocks_append(&guest_phys_blocks);
  160. QTAILQ_FOREACH(block, &guest_phys_blocks.head, next) {
  161. assert(QEMU_IS_ALIGNED(block->target_start, TARGET_PAGE_SIZE));
  162. assert(QEMU_IS_ALIGNED(block->target_end, TARGET_PAGE_SIZE));
  163. gfn = block->target_start / TARGET_PAGE_SIZE;
  164. pages = (block->target_end - block->target_start) / TARGET_PAGE_SIZE;
  165. while (pages) {
  166. const uint64_t cur_pages = MIN(pages, S390_SKEYS_BUFFER_SIZE);
  167. ret = skeyclass->get_skeys(ss, gfn, cur_pages, buf);
  168. if (ret < 0) {
  169. error_setg_errno(errp, -ret, "get_keys error");
  170. goto out_free;
  171. }
  172. /* write keys to stream */
  173. write_keys(f, buf, gfn, cur_pages, &lerr);
  174. if (lerr) {
  175. goto out_free;
  176. }
  177. gfn += cur_pages;
  178. pages -= cur_pages;
  179. }
  180. }
  181. out_free:
  182. guest_phys_blocks_free(&guest_phys_blocks);
  183. error_propagate(errp, lerr);
  184. g_free(buf);
  185. out:
  186. fclose(f);
  187. }
  188. static bool qemu_s390_skeys_are_enabled(S390SKeysState *ss)
  189. {
  190. QEMUS390SKeysState *skeys = QEMU_S390_SKEYS(ss);
  191. /* Lockless check is sufficient. */
  192. return !!skeys->keydata;
  193. }
  194. static bool qemu_s390_enable_skeys(S390SKeysState *ss)
  195. {
  196. QEMUS390SKeysState *skeys = QEMU_S390_SKEYS(ss);
  197. static gsize initialized;
  198. if (likely(skeys->keydata)) {
  199. return true;
  200. }
  201. /*
  202. * TODO: Modern Linux doesn't use storage keys unless running KVM guests
  203. * that use storage keys. Therefore, we keep it simple for now.
  204. *
  205. * 1) We should initialize to "referenced+changed" for an initial
  206. * over-indication. Let's avoid touching megabytes of data for now and
  207. * assume that any sane user will issue a storage key instruction before
  208. * actually relying on this data.
  209. * 2) Relying on ram_size and allocating a big array is ugly. We should
  210. * allocate and manage storage key data per RAMBlock or optimally using
  211. * some sparse data structure.
  212. * 3) We only ever have a single S390SKeysState, so relying on
  213. * g_once_init_enter() is good enough.
  214. */
  215. if (g_once_init_enter(&initialized)) {
  216. S390CcwMachineState *s390ms = S390_CCW_MACHINE(qdev_get_machine());
  217. skeys->key_count = s390_get_memory_limit(s390ms) / TARGET_PAGE_SIZE;
  218. skeys->keydata = g_malloc0(skeys->key_count);
  219. g_once_init_leave(&initialized, 1);
  220. }
  221. return false;
  222. }
  223. static int qemu_s390_skeys_set(S390SKeysState *ss, uint64_t start_gfn,
  224. uint64_t count, uint8_t *keys)
  225. {
  226. QEMUS390SKeysState *skeydev = QEMU_S390_SKEYS(ss);
  227. int i;
  228. /* Check for uint64 overflow and access beyond end of key data */
  229. if (unlikely(!skeydev->keydata || start_gfn + count > skeydev->key_count ||
  230. start_gfn + count < count)) {
  231. error_report("Error: Setting storage keys for pages with unallocated "
  232. "storage key memory: gfn=%" PRIx64 " count=%" PRId64,
  233. start_gfn, count);
  234. return -EINVAL;
  235. }
  236. for (i = 0; i < count; i++) {
  237. skeydev->keydata[start_gfn + i] = keys[i];
  238. }
  239. return 0;
  240. }
  241. static int qemu_s390_skeys_get(S390SKeysState *ss, uint64_t start_gfn,
  242. uint64_t count, uint8_t *keys)
  243. {
  244. QEMUS390SKeysState *skeydev = QEMU_S390_SKEYS(ss);
  245. int i;
  246. /* Check for uint64 overflow and access beyond end of key data */
  247. if (unlikely(!skeydev->keydata || start_gfn + count > skeydev->key_count ||
  248. start_gfn + count < count)) {
  249. error_report("Error: Getting storage keys for pages with unallocated "
  250. "storage key memory: gfn=%" PRIx64 " count=%" PRId64,
  251. start_gfn, count);
  252. return -EINVAL;
  253. }
  254. for (i = 0; i < count; i++) {
  255. keys[i] = skeydev->keydata[start_gfn + i];
  256. }
  257. return 0;
  258. }
  259. static void qemu_s390_skeys_class_init(ObjectClass *oc, void *data)
  260. {
  261. S390SKeysClass *skeyclass = S390_SKEYS_CLASS(oc);
  262. DeviceClass *dc = DEVICE_CLASS(oc);
  263. skeyclass->skeys_are_enabled = qemu_s390_skeys_are_enabled;
  264. skeyclass->enable_skeys = qemu_s390_enable_skeys;
  265. skeyclass->get_skeys = qemu_s390_skeys_get;
  266. skeyclass->set_skeys = qemu_s390_skeys_set;
  267. /* Reason: Internal device (only one skeys device for the whole memory) */
  268. dc->user_creatable = false;
  269. }
  270. static const TypeInfo qemu_s390_skeys_info = {
  271. .name = TYPE_QEMU_S390_SKEYS,
  272. .parent = TYPE_S390_SKEYS,
  273. .instance_size = sizeof(QEMUS390SKeysState),
  274. .class_init = qemu_s390_skeys_class_init,
  275. .class_size = sizeof(S390SKeysClass),
  276. };
  277. static void s390_storage_keys_save(QEMUFile *f, void *opaque)
  278. {
  279. S390SKeysState *ss = S390_SKEYS(opaque);
  280. S390SKeysClass *skeyclass = S390_SKEYS_GET_CLASS(ss);
  281. GuestPhysBlockList guest_phys_blocks;
  282. GuestPhysBlock *block;
  283. uint64_t pages, gfn;
  284. int error = 0;
  285. uint8_t *buf;
  286. if (!skeyclass->skeys_are_enabled(ss)) {
  287. goto end_stream;
  288. }
  289. buf = g_try_malloc(S390_SKEYS_BUFFER_SIZE);
  290. if (!buf) {
  291. error_report("storage key save could not allocate memory");
  292. goto end_stream;
  293. }
  294. guest_phys_blocks_init(&guest_phys_blocks);
  295. guest_phys_blocks_append(&guest_phys_blocks);
  296. /* Send each contiguous physical memory range separately. */
  297. QTAILQ_FOREACH(block, &guest_phys_blocks.head, next) {
  298. assert(QEMU_IS_ALIGNED(block->target_start, TARGET_PAGE_SIZE));
  299. assert(QEMU_IS_ALIGNED(block->target_end, TARGET_PAGE_SIZE));
  300. gfn = block->target_start / TARGET_PAGE_SIZE;
  301. pages = (block->target_end - block->target_start) / TARGET_PAGE_SIZE;
  302. qemu_put_be64(f, block->target_start | S390_SKEYS_SAVE_FLAG_SKEYS);
  303. qemu_put_be64(f, pages);
  304. while (pages) {
  305. const uint64_t cur_pages = MIN(pages, S390_SKEYS_BUFFER_SIZE);
  306. if (!error) {
  307. error = skeyclass->get_skeys(ss, gfn, cur_pages, buf);
  308. if (error) {
  309. /*
  310. * Create a valid stream with all 0x00 and indicate
  311. * S390_SKEYS_SAVE_FLAG_ERROR to the destination.
  312. */
  313. error_report("S390_GET_KEYS error %d", error);
  314. memset(buf, 0, S390_SKEYS_BUFFER_SIZE);
  315. }
  316. }
  317. qemu_put_buffer(f, buf, cur_pages);
  318. gfn += cur_pages;
  319. pages -= cur_pages;
  320. }
  321. if (error) {
  322. break;
  323. }
  324. }
  325. guest_phys_blocks_free(&guest_phys_blocks);
  326. g_free(buf);
  327. end_stream:
  328. if (error) {
  329. qemu_put_be64(f, S390_SKEYS_SAVE_FLAG_ERROR);
  330. } else {
  331. qemu_put_be64(f, S390_SKEYS_SAVE_FLAG_EOS);
  332. }
  333. }
  334. static int s390_storage_keys_load(QEMUFile *f, void *opaque, int version_id)
  335. {
  336. S390SKeysState *ss = S390_SKEYS(opaque);
  337. S390SKeysClass *skeyclass = S390_SKEYS_GET_CLASS(ss);
  338. int ret = 0;
  339. /*
  340. * Make sure to lazy-enable if required to be done explicitly. No need to
  341. * flush any TLB as the VM is not running yet.
  342. */
  343. if (skeyclass->enable_skeys) {
  344. skeyclass->enable_skeys(ss);
  345. }
  346. while (!ret) {
  347. ram_addr_t addr;
  348. int flags;
  349. addr = qemu_get_be64(f);
  350. flags = addr & ~TARGET_PAGE_MASK;
  351. addr &= TARGET_PAGE_MASK;
  352. switch (flags) {
  353. case S390_SKEYS_SAVE_FLAG_SKEYS: {
  354. const uint64_t total_count = qemu_get_be64(f);
  355. uint64_t handled_count = 0, cur_count;
  356. uint64_t cur_gfn = addr / TARGET_PAGE_SIZE;
  357. uint8_t *buf = g_try_malloc(S390_SKEYS_BUFFER_SIZE);
  358. if (!buf) {
  359. error_report("storage key load could not allocate memory");
  360. ret = -ENOMEM;
  361. break;
  362. }
  363. while (handled_count < total_count) {
  364. cur_count = MIN(total_count - handled_count,
  365. S390_SKEYS_BUFFER_SIZE);
  366. qemu_get_buffer(f, buf, cur_count);
  367. ret = skeyclass->set_skeys(ss, cur_gfn, cur_count, buf);
  368. if (ret < 0) {
  369. error_report("S390_SET_KEYS error %d", ret);
  370. break;
  371. }
  372. handled_count += cur_count;
  373. cur_gfn += cur_count;
  374. }
  375. g_free(buf);
  376. break;
  377. }
  378. case S390_SKEYS_SAVE_FLAG_ERROR: {
  379. error_report("Storage key data is incomplete");
  380. ret = -EINVAL;
  381. break;
  382. }
  383. case S390_SKEYS_SAVE_FLAG_EOS:
  384. /* normal exit */
  385. return 0;
  386. default:
  387. error_report("Unexpected storage key flag data: %#x", flags);
  388. ret = -EINVAL;
  389. }
  390. }
  391. return ret;
  392. }
  393. static SaveVMHandlers savevm_s390_storage_keys = {
  394. .save_state = s390_storage_keys_save,
  395. .load_state = s390_storage_keys_load,
  396. };
  397. static void s390_skeys_realize(DeviceState *dev, Error **errp)
  398. {
  399. S390SKeysState *ss = S390_SKEYS(dev);
  400. register_savevm_live(TYPE_S390_SKEYS, 0, 1, &savevm_s390_storage_keys, ss);
  401. }
  402. static void s390_skeys_class_init(ObjectClass *oc, void *data)
  403. {
  404. DeviceClass *dc = DEVICE_CLASS(oc);
  405. dc->hotpluggable = false;
  406. dc->realize = s390_skeys_realize;
  407. set_bit(DEVICE_CATEGORY_MISC, dc->categories);
  408. }
  409. static const TypeInfo s390_skeys_info = {
  410. .name = TYPE_S390_SKEYS,
  411. .parent = TYPE_DEVICE,
  412. .instance_size = sizeof(S390SKeysState),
  413. .class_init = s390_skeys_class_init,
  414. .class_size = sizeof(S390SKeysClass),
  415. .abstract = true,
  416. };
  417. static void qemu_s390_skeys_register_types(void)
  418. {
  419. type_register_static(&s390_skeys_info);
  420. type_register_static(&qemu_s390_skeys_info);
  421. }
  422. type_init(qemu_s390_skeys_register_types)