uv.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Common Ultravisor functions and initialization
  4. *
  5. * Copyright IBM Corp. 2019, 2020
  6. */
  7. #define KMSG_COMPONENT "prot_virt"
  8. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  9. #include <linux/kernel.h>
  10. #include <linux/types.h>
  11. #include <linux/sizes.h>
  12. #include <linux/bitmap.h>
  13. #include <linux/memblock.h>
  14. #include <linux/pagemap.h>
  15. #include <linux/swap.h>
  16. #include <linux/pagewalk.h>
  17. #include <asm/facility.h>
  18. #include <asm/sections.h>
  19. #include <asm/uv.h>
  20. #if !IS_ENABLED(CONFIG_KVM)
  21. unsigned long __gmap_translate(struct gmap *gmap, unsigned long gaddr)
  22. {
  23. return 0;
  24. }
  25. int gmap_fault(struct gmap *gmap, unsigned long gaddr,
  26. unsigned int fault_flags)
  27. {
  28. return 0;
  29. }
  30. #endif
  31. /* the bootdata_preserved fields come from ones in arch/s390/boot/uv.c */
  32. int __bootdata_preserved(prot_virt_guest);
  33. EXPORT_SYMBOL(prot_virt_guest);
  34. /*
  35. * uv_info contains both host and guest information but it's currently only
  36. * expected to be used within modules if it's the KVM module or for
  37. * any PV guest module.
  38. *
  39. * The kernel itself will write these values once in uv_query_info()
  40. * and then make some of them readable via a sysfs interface.
  41. */
  42. struct uv_info __bootdata_preserved(uv_info);
  43. EXPORT_SYMBOL(uv_info);
  44. int __bootdata_preserved(prot_virt_host);
  45. EXPORT_SYMBOL(prot_virt_host);
  46. static int __init uv_init(phys_addr_t stor_base, unsigned long stor_len)
  47. {
  48. struct uv_cb_init uvcb = {
  49. .header.cmd = UVC_CMD_INIT_UV,
  50. .header.len = sizeof(uvcb),
  51. .stor_origin = stor_base,
  52. .stor_len = stor_len,
  53. };
  54. if (uv_call(0, (uint64_t)&uvcb)) {
  55. pr_err("Ultravisor init failed with rc: 0x%x rrc: 0%x\n",
  56. uvcb.header.rc, uvcb.header.rrc);
  57. return -1;
  58. }
  59. return 0;
  60. }
  61. void __init setup_uv(void)
  62. {
  63. void *uv_stor_base;
  64. if (!is_prot_virt_host())
  65. return;
  66. uv_stor_base = memblock_alloc_try_nid(
  67. uv_info.uv_base_stor_len, SZ_1M, SZ_2G,
  68. MEMBLOCK_ALLOC_ACCESSIBLE, NUMA_NO_NODE);
  69. if (!uv_stor_base) {
  70. pr_warn("Failed to reserve %lu bytes for ultravisor base storage\n",
  71. uv_info.uv_base_stor_len);
  72. goto fail;
  73. }
  74. if (uv_init(__pa(uv_stor_base), uv_info.uv_base_stor_len)) {
  75. memblock_free(uv_stor_base, uv_info.uv_base_stor_len);
  76. goto fail;
  77. }
  78. pr_info("Reserving %luMB as ultravisor base storage\n",
  79. uv_info.uv_base_stor_len >> 20);
  80. return;
  81. fail:
  82. pr_info("Disabling support for protected virtualization");
  83. prot_virt_host = 0;
  84. }
  85. /*
  86. * Requests the Ultravisor to pin the page in the shared state. This will
  87. * cause an intercept when the guest attempts to unshare the pinned page.
  88. */
  89. int uv_pin_shared(unsigned long paddr)
  90. {
  91. struct uv_cb_cfs uvcb = {
  92. .header.cmd = UVC_CMD_PIN_PAGE_SHARED,
  93. .header.len = sizeof(uvcb),
  94. .paddr = paddr,
  95. };
  96. if (uv_call(0, (u64)&uvcb))
  97. return -EINVAL;
  98. return 0;
  99. }
  100. EXPORT_SYMBOL_GPL(uv_pin_shared);
  101. /*
  102. * Requests the Ultravisor to destroy a guest page and make it
  103. * accessible to the host. The destroy clears the page instead of
  104. * exporting.
  105. *
  106. * @paddr: Absolute host address of page to be destroyed
  107. */
  108. static int uv_destroy(unsigned long paddr)
  109. {
  110. struct uv_cb_cfs uvcb = {
  111. .header.cmd = UVC_CMD_DESTR_SEC_STOR,
  112. .header.len = sizeof(uvcb),
  113. .paddr = paddr
  114. };
  115. if (uv_call(0, (u64)&uvcb)) {
  116. /*
  117. * Older firmware uses 107/d as an indication of a non secure
  118. * page. Let us emulate the newer variant (no-op).
  119. */
  120. if (uvcb.header.rc == 0x107 && uvcb.header.rrc == 0xd)
  121. return 0;
  122. return -EINVAL;
  123. }
  124. return 0;
  125. }
  126. /*
  127. * The caller must already hold a reference to the folio
  128. */
  129. int uv_destroy_folio(struct folio *folio)
  130. {
  131. int rc;
  132. /* See gmap_make_secure(): large folios cannot be secure */
  133. if (unlikely(folio_test_large(folio)))
  134. return 0;
  135. folio_get(folio);
  136. rc = uv_destroy(folio_to_phys(folio));
  137. if (!rc)
  138. clear_bit(PG_arch_1, &folio->flags);
  139. folio_put(folio);
  140. return rc;
  141. }
  142. /*
  143. * The present PTE still indirectly holds a folio reference through the mapping.
  144. */
  145. int uv_destroy_pte(pte_t pte)
  146. {
  147. VM_WARN_ON(!pte_present(pte));
  148. return uv_destroy_folio(pfn_folio(pte_pfn(pte)));
  149. }
  150. /*
  151. * Requests the Ultravisor to encrypt a guest page and make it
  152. * accessible to the host for paging (export).
  153. *
  154. * @paddr: Absolute host address of page to be exported
  155. */
  156. static int uv_convert_from_secure(unsigned long paddr)
  157. {
  158. struct uv_cb_cfs uvcb = {
  159. .header.cmd = UVC_CMD_CONV_FROM_SEC_STOR,
  160. .header.len = sizeof(uvcb),
  161. .paddr = paddr
  162. };
  163. if (uv_call(0, (u64)&uvcb))
  164. return -EINVAL;
  165. return 0;
  166. }
  167. /*
  168. * The caller must already hold a reference to the folio.
  169. */
  170. static int uv_convert_from_secure_folio(struct folio *folio)
  171. {
  172. int rc;
  173. /* See gmap_make_secure(): large folios cannot be secure */
  174. if (unlikely(folio_test_large(folio)))
  175. return 0;
  176. folio_get(folio);
  177. rc = uv_convert_from_secure(folio_to_phys(folio));
  178. if (!rc)
  179. clear_bit(PG_arch_1, &folio->flags);
  180. folio_put(folio);
  181. return rc;
  182. }
  183. /*
  184. * The present PTE still indirectly holds a folio reference through the mapping.
  185. */
  186. int uv_convert_from_secure_pte(pte_t pte)
  187. {
  188. VM_WARN_ON(!pte_present(pte));
  189. return uv_convert_from_secure_folio(pfn_folio(pte_pfn(pte)));
  190. }
  191. /*
  192. * Calculate the expected ref_count for a folio that would otherwise have no
  193. * further pins. This was cribbed from similar functions in other places in
  194. * the kernel, but with some slight modifications. We know that a secure
  195. * folio can not be a large folio, for example.
  196. */
  197. static int expected_folio_refs(struct folio *folio)
  198. {
  199. int res;
  200. res = folio_mapcount(folio);
  201. if (folio_test_swapcache(folio)) {
  202. res++;
  203. } else if (folio_mapping(folio)) {
  204. res++;
  205. if (folio->private)
  206. res++;
  207. }
  208. return res;
  209. }
  210. static int make_folio_secure(struct folio *folio, struct uv_cb_header *uvcb)
  211. {
  212. int expected, cc = 0;
  213. if (folio_test_writeback(folio))
  214. return -EAGAIN;
  215. expected = expected_folio_refs(folio);
  216. if (!folio_ref_freeze(folio, expected))
  217. return -EBUSY;
  218. set_bit(PG_arch_1, &folio->flags);
  219. /*
  220. * If the UVC does not succeed or fail immediately, we don't want to
  221. * loop for long, or we might get stall notifications.
  222. * On the other hand, this is a complex scenario and we are holding a lot of
  223. * locks, so we can't easily sleep and reschedule. We try only once,
  224. * and if the UVC returned busy or partial completion, we return
  225. * -EAGAIN and we let the callers deal with it.
  226. */
  227. cc = __uv_call(0, (u64)uvcb);
  228. folio_ref_unfreeze(folio, expected);
  229. /*
  230. * Return -ENXIO if the folio was not mapped, -EINVAL for other errors.
  231. * If busy or partially completed, return -EAGAIN.
  232. */
  233. if (cc == UVC_CC_OK)
  234. return 0;
  235. else if (cc == UVC_CC_BUSY || cc == UVC_CC_PARTIAL)
  236. return -EAGAIN;
  237. return uvcb->rc == 0x10a ? -ENXIO : -EINVAL;
  238. }
  239. /**
  240. * should_export_before_import - Determine whether an export is needed
  241. * before an import-like operation
  242. * @uvcb: the Ultravisor control block of the UVC to be performed
  243. * @mm: the mm of the process
  244. *
  245. * Returns whether an export is needed before every import-like operation.
  246. * This is needed for shared pages, which don't trigger a secure storage
  247. * exception when accessed from a different guest.
  248. *
  249. * Although considered as one, the Unpin Page UVC is not an actual import,
  250. * so it is not affected.
  251. *
  252. * No export is needed also when there is only one protected VM, because the
  253. * page cannot belong to the wrong VM in that case (there is no "other VM"
  254. * it can belong to).
  255. *
  256. * Return: true if an export is needed before every import, otherwise false.
  257. */
  258. static bool should_export_before_import(struct uv_cb_header *uvcb, struct mm_struct *mm)
  259. {
  260. /*
  261. * The misc feature indicates, among other things, that importing a
  262. * shared page from a different protected VM will automatically also
  263. * transfer its ownership.
  264. */
  265. if (uv_has_feature(BIT_UV_FEAT_MISC))
  266. return false;
  267. if (uvcb->cmd == UVC_CMD_UNPIN_PAGE_SHARED)
  268. return false;
  269. return atomic_read(&mm->context.protected_count) > 1;
  270. }
  271. /*
  272. * Drain LRU caches: the local one on first invocation and the ones of all
  273. * CPUs on successive invocations. Returns "true" on the first invocation.
  274. */
  275. static bool drain_lru(bool *drain_lru_called)
  276. {
  277. /*
  278. * If we have tried a local drain and the folio refcount
  279. * still does not match our expected safe value, try with a
  280. * system wide drain. This is needed if the pagevecs holding
  281. * the page are on a different CPU.
  282. */
  283. if (*drain_lru_called) {
  284. lru_add_drain_all();
  285. /* We give up here, don't retry immediately. */
  286. return false;
  287. }
  288. /*
  289. * We are here if the folio refcount does not match the
  290. * expected safe value. The main culprits are usually
  291. * pagevecs. With lru_add_drain() we drain the pagevecs
  292. * on the local CPU so that hopefully the refcount will
  293. * reach the expected safe value.
  294. */
  295. lru_add_drain();
  296. *drain_lru_called = true;
  297. /* The caller should try again immediately */
  298. return true;
  299. }
  300. /*
  301. * Requests the Ultravisor to make a page accessible to a guest.
  302. * If it's brought in the first time, it will be cleared. If
  303. * it has been exported before, it will be decrypted and integrity
  304. * checked.
  305. */
  306. int gmap_make_secure(struct gmap *gmap, unsigned long gaddr, void *uvcb)
  307. {
  308. struct vm_area_struct *vma;
  309. bool drain_lru_called = false;
  310. spinlock_t *ptelock;
  311. unsigned long uaddr;
  312. struct folio *folio;
  313. pte_t *ptep;
  314. int rc;
  315. again:
  316. rc = -EFAULT;
  317. mmap_read_lock(gmap->mm);
  318. uaddr = __gmap_translate(gmap, gaddr);
  319. if (IS_ERR_VALUE(uaddr))
  320. goto out;
  321. vma = vma_lookup(gmap->mm, uaddr);
  322. if (!vma)
  323. goto out;
  324. /*
  325. * Secure pages cannot be huge and userspace should not combine both.
  326. * In case userspace does it anyway this will result in an -EFAULT for
  327. * the unpack. The guest is thus never reaching secure mode. If
  328. * userspace is playing dirty tricky with mapping huge pages later
  329. * on this will result in a segmentation fault.
  330. */
  331. if (is_vm_hugetlb_page(vma))
  332. goto out;
  333. rc = -ENXIO;
  334. ptep = get_locked_pte(gmap->mm, uaddr, &ptelock);
  335. if (!ptep)
  336. goto out;
  337. if (pte_present(*ptep) && !(pte_val(*ptep) & _PAGE_INVALID) && pte_write(*ptep)) {
  338. folio = page_folio(pte_page(*ptep));
  339. rc = -EAGAIN;
  340. if (folio_test_large(folio)) {
  341. rc = -E2BIG;
  342. } else if (folio_trylock(folio)) {
  343. if (should_export_before_import(uvcb, gmap->mm))
  344. uv_convert_from_secure(PFN_PHYS(folio_pfn(folio)));
  345. rc = make_folio_secure(folio, uvcb);
  346. folio_unlock(folio);
  347. }
  348. /*
  349. * Once we drop the PTL, the folio may get unmapped and
  350. * freed immediately. We need a temporary reference.
  351. */
  352. if (rc == -EAGAIN || rc == -E2BIG)
  353. folio_get(folio);
  354. }
  355. pte_unmap_unlock(ptep, ptelock);
  356. out:
  357. mmap_read_unlock(gmap->mm);
  358. switch (rc) {
  359. case -E2BIG:
  360. folio_lock(folio);
  361. rc = split_folio(folio);
  362. folio_unlock(folio);
  363. folio_put(folio);
  364. switch (rc) {
  365. case 0:
  366. /* Splitting succeeded, try again immediately. */
  367. goto again;
  368. case -EAGAIN:
  369. /* Additional folio references. */
  370. if (drain_lru(&drain_lru_called))
  371. goto again;
  372. return -EAGAIN;
  373. case -EBUSY:
  374. /* Unexpected race. */
  375. return -EAGAIN;
  376. }
  377. WARN_ON_ONCE(1);
  378. return -ENXIO;
  379. case -EAGAIN:
  380. /*
  381. * If we are here because the UVC returned busy or partial
  382. * completion, this is just a useless check, but it is safe.
  383. */
  384. folio_wait_writeback(folio);
  385. folio_put(folio);
  386. return -EAGAIN;
  387. case -EBUSY:
  388. /* Additional folio references. */
  389. if (drain_lru(&drain_lru_called))
  390. goto again;
  391. return -EAGAIN;
  392. case -ENXIO:
  393. if (gmap_fault(gmap, gaddr, FAULT_FLAG_WRITE))
  394. return -EFAULT;
  395. return -EAGAIN;
  396. }
  397. return rc;
  398. }
  399. EXPORT_SYMBOL_GPL(gmap_make_secure);
  400. int gmap_convert_to_secure(struct gmap *gmap, unsigned long gaddr)
  401. {
  402. struct uv_cb_cts uvcb = {
  403. .header.cmd = UVC_CMD_CONV_TO_SEC_STOR,
  404. .header.len = sizeof(uvcb),
  405. .guest_handle = gmap->guest_handle,
  406. .gaddr = gaddr,
  407. };
  408. return gmap_make_secure(gmap, gaddr, &uvcb);
  409. }
  410. EXPORT_SYMBOL_GPL(gmap_convert_to_secure);
  411. /**
  412. * gmap_destroy_page - Destroy a guest page.
  413. * @gmap: the gmap of the guest
  414. * @gaddr: the guest address to destroy
  415. *
  416. * An attempt will be made to destroy the given guest page. If the attempt
  417. * fails, an attempt is made to export the page. If both attempts fail, an
  418. * appropriate error is returned.
  419. */
  420. int gmap_destroy_page(struct gmap *gmap, unsigned long gaddr)
  421. {
  422. struct vm_area_struct *vma;
  423. struct folio_walk fw;
  424. unsigned long uaddr;
  425. struct folio *folio;
  426. int rc;
  427. rc = -EFAULT;
  428. mmap_read_lock(gmap->mm);
  429. uaddr = __gmap_translate(gmap, gaddr);
  430. if (IS_ERR_VALUE(uaddr))
  431. goto out;
  432. vma = vma_lookup(gmap->mm, uaddr);
  433. if (!vma)
  434. goto out;
  435. /*
  436. * Huge pages should not be able to become secure
  437. */
  438. if (is_vm_hugetlb_page(vma))
  439. goto out;
  440. rc = 0;
  441. folio = folio_walk_start(&fw, vma, uaddr, 0);
  442. if (!folio)
  443. goto out;
  444. /*
  445. * See gmap_make_secure(): large folios cannot be secure. Small
  446. * folio implies FW_LEVEL_PTE.
  447. */
  448. if (folio_test_large(folio) || !pte_write(fw.pte))
  449. goto out_walk_end;
  450. rc = uv_destroy_folio(folio);
  451. /*
  452. * Fault handlers can race; it is possible that two CPUs will fault
  453. * on the same secure page. One CPU can destroy the page, reboot,
  454. * re-enter secure mode and import it, while the second CPU was
  455. * stuck at the beginning of the handler. At some point the second
  456. * CPU will be able to progress, and it will not be able to destroy
  457. * the page. In that case we do not want to terminate the process,
  458. * we instead try to export the page.
  459. */
  460. if (rc)
  461. rc = uv_convert_from_secure_folio(folio);
  462. out_walk_end:
  463. folio_walk_end(&fw, vma);
  464. out:
  465. mmap_read_unlock(gmap->mm);
  466. return rc;
  467. }
  468. EXPORT_SYMBOL_GPL(gmap_destroy_page);
  469. /*
  470. * To be called with the folio locked or with an extra reference! This will
  471. * prevent gmap_make_secure from touching the folio concurrently. Having 2
  472. * parallel arch_make_folio_accessible is fine, as the UV calls will become a
  473. * no-op if the folio is already exported.
  474. */
  475. int arch_make_folio_accessible(struct folio *folio)
  476. {
  477. int rc = 0;
  478. /* See gmap_make_secure(): large folios cannot be secure */
  479. if (unlikely(folio_test_large(folio)))
  480. return 0;
  481. /*
  482. * PG_arch_1 is used in 2 places:
  483. * 1. for storage keys of hugetlb folios and KVM
  484. * 2. As an indication that this small folio might be secure. This can
  485. * overindicate, e.g. we set the bit before calling
  486. * convert_to_secure.
  487. * As secure pages are never large folios, both variants can co-exists.
  488. */
  489. if (!test_bit(PG_arch_1, &folio->flags))
  490. return 0;
  491. rc = uv_pin_shared(folio_to_phys(folio));
  492. if (!rc) {
  493. clear_bit(PG_arch_1, &folio->flags);
  494. return 0;
  495. }
  496. rc = uv_convert_from_secure(folio_to_phys(folio));
  497. if (!rc) {
  498. clear_bit(PG_arch_1, &folio->flags);
  499. return 0;
  500. }
  501. return rc;
  502. }
  503. EXPORT_SYMBOL_GPL(arch_make_folio_accessible);
  504. static ssize_t uv_query_facilities(struct kobject *kobj,
  505. struct kobj_attribute *attr, char *buf)
  506. {
  507. return sysfs_emit(buf, "%lx\n%lx\n%lx\n%lx\n",
  508. uv_info.inst_calls_list[0],
  509. uv_info.inst_calls_list[1],
  510. uv_info.inst_calls_list[2],
  511. uv_info.inst_calls_list[3]);
  512. }
  513. static struct kobj_attribute uv_query_facilities_attr =
  514. __ATTR(facilities, 0444, uv_query_facilities, NULL);
  515. static ssize_t uv_query_supp_se_hdr_ver(struct kobject *kobj,
  516. struct kobj_attribute *attr, char *buf)
  517. {
  518. return sysfs_emit(buf, "%lx\n", uv_info.supp_se_hdr_ver);
  519. }
  520. static struct kobj_attribute uv_query_supp_se_hdr_ver_attr =
  521. __ATTR(supp_se_hdr_ver, 0444, uv_query_supp_se_hdr_ver, NULL);
  522. static ssize_t uv_query_supp_se_hdr_pcf(struct kobject *kobj,
  523. struct kobj_attribute *attr, char *buf)
  524. {
  525. return sysfs_emit(buf, "%lx\n", uv_info.supp_se_hdr_pcf);
  526. }
  527. static struct kobj_attribute uv_query_supp_se_hdr_pcf_attr =
  528. __ATTR(supp_se_hdr_pcf, 0444, uv_query_supp_se_hdr_pcf, NULL);
  529. static ssize_t uv_query_dump_cpu_len(struct kobject *kobj,
  530. struct kobj_attribute *attr, char *buf)
  531. {
  532. return sysfs_emit(buf, "%lx\n", uv_info.guest_cpu_stor_len);
  533. }
  534. static struct kobj_attribute uv_query_dump_cpu_len_attr =
  535. __ATTR(uv_query_dump_cpu_len, 0444, uv_query_dump_cpu_len, NULL);
  536. static ssize_t uv_query_dump_storage_state_len(struct kobject *kobj,
  537. struct kobj_attribute *attr, char *buf)
  538. {
  539. return sysfs_emit(buf, "%lx\n", uv_info.conf_dump_storage_state_len);
  540. }
  541. static struct kobj_attribute uv_query_dump_storage_state_len_attr =
  542. __ATTR(dump_storage_state_len, 0444, uv_query_dump_storage_state_len, NULL);
  543. static ssize_t uv_query_dump_finalize_len(struct kobject *kobj,
  544. struct kobj_attribute *attr, char *buf)
  545. {
  546. return sysfs_emit(buf, "%lx\n", uv_info.conf_dump_finalize_len);
  547. }
  548. static struct kobj_attribute uv_query_dump_finalize_len_attr =
  549. __ATTR(dump_finalize_len, 0444, uv_query_dump_finalize_len, NULL);
  550. static ssize_t uv_query_feature_indications(struct kobject *kobj,
  551. struct kobj_attribute *attr, char *buf)
  552. {
  553. return sysfs_emit(buf, "%lx\n", uv_info.uv_feature_indications);
  554. }
  555. static struct kobj_attribute uv_query_feature_indications_attr =
  556. __ATTR(feature_indications, 0444, uv_query_feature_indications, NULL);
  557. static ssize_t uv_query_max_guest_cpus(struct kobject *kobj,
  558. struct kobj_attribute *attr, char *buf)
  559. {
  560. return sysfs_emit(buf, "%d\n", uv_info.max_guest_cpu_id + 1);
  561. }
  562. static struct kobj_attribute uv_query_max_guest_cpus_attr =
  563. __ATTR(max_cpus, 0444, uv_query_max_guest_cpus, NULL);
  564. static ssize_t uv_query_max_guest_vms(struct kobject *kobj,
  565. struct kobj_attribute *attr, char *buf)
  566. {
  567. return sysfs_emit(buf, "%d\n", uv_info.max_num_sec_conf);
  568. }
  569. static struct kobj_attribute uv_query_max_guest_vms_attr =
  570. __ATTR(max_guests, 0444, uv_query_max_guest_vms, NULL);
  571. static ssize_t uv_query_max_guest_addr(struct kobject *kobj,
  572. struct kobj_attribute *attr, char *buf)
  573. {
  574. return sysfs_emit(buf, "%lx\n", uv_info.max_sec_stor_addr);
  575. }
  576. static struct kobj_attribute uv_query_max_guest_addr_attr =
  577. __ATTR(max_address, 0444, uv_query_max_guest_addr, NULL);
  578. static ssize_t uv_query_supp_att_req_hdr_ver(struct kobject *kobj,
  579. struct kobj_attribute *attr, char *buf)
  580. {
  581. return sysfs_emit(buf, "%lx\n", uv_info.supp_att_req_hdr_ver);
  582. }
  583. static struct kobj_attribute uv_query_supp_att_req_hdr_ver_attr =
  584. __ATTR(supp_att_req_hdr_ver, 0444, uv_query_supp_att_req_hdr_ver, NULL);
  585. static ssize_t uv_query_supp_att_pflags(struct kobject *kobj,
  586. struct kobj_attribute *attr, char *buf)
  587. {
  588. return sysfs_emit(buf, "%lx\n", uv_info.supp_att_pflags);
  589. }
  590. static struct kobj_attribute uv_query_supp_att_pflags_attr =
  591. __ATTR(supp_att_pflags, 0444, uv_query_supp_att_pflags, NULL);
  592. static ssize_t uv_query_supp_add_secret_req_ver(struct kobject *kobj,
  593. struct kobj_attribute *attr, char *buf)
  594. {
  595. return sysfs_emit(buf, "%lx\n", uv_info.supp_add_secret_req_ver);
  596. }
  597. static struct kobj_attribute uv_query_supp_add_secret_req_ver_attr =
  598. __ATTR(supp_add_secret_req_ver, 0444, uv_query_supp_add_secret_req_ver, NULL);
  599. static ssize_t uv_query_supp_add_secret_pcf(struct kobject *kobj,
  600. struct kobj_attribute *attr, char *buf)
  601. {
  602. return sysfs_emit(buf, "%lx\n", uv_info.supp_add_secret_pcf);
  603. }
  604. static struct kobj_attribute uv_query_supp_add_secret_pcf_attr =
  605. __ATTR(supp_add_secret_pcf, 0444, uv_query_supp_add_secret_pcf, NULL);
  606. static ssize_t uv_query_supp_secret_types(struct kobject *kobj,
  607. struct kobj_attribute *attr, char *buf)
  608. {
  609. return sysfs_emit(buf, "%lx\n", uv_info.supp_secret_types);
  610. }
  611. static struct kobj_attribute uv_query_supp_secret_types_attr =
  612. __ATTR(supp_secret_types, 0444, uv_query_supp_secret_types, NULL);
  613. static ssize_t uv_query_max_secrets(struct kobject *kobj,
  614. struct kobj_attribute *attr, char *buf)
  615. {
  616. return sysfs_emit(buf, "%d\n", uv_info.max_secrets);
  617. }
  618. static struct kobj_attribute uv_query_max_secrets_attr =
  619. __ATTR(max_secrets, 0444, uv_query_max_secrets, NULL);
  620. static struct attribute *uv_query_attrs[] = {
  621. &uv_query_facilities_attr.attr,
  622. &uv_query_feature_indications_attr.attr,
  623. &uv_query_max_guest_cpus_attr.attr,
  624. &uv_query_max_guest_vms_attr.attr,
  625. &uv_query_max_guest_addr_attr.attr,
  626. &uv_query_supp_se_hdr_ver_attr.attr,
  627. &uv_query_supp_se_hdr_pcf_attr.attr,
  628. &uv_query_dump_storage_state_len_attr.attr,
  629. &uv_query_dump_finalize_len_attr.attr,
  630. &uv_query_dump_cpu_len_attr.attr,
  631. &uv_query_supp_att_req_hdr_ver_attr.attr,
  632. &uv_query_supp_att_pflags_attr.attr,
  633. &uv_query_supp_add_secret_req_ver_attr.attr,
  634. &uv_query_supp_add_secret_pcf_attr.attr,
  635. &uv_query_supp_secret_types_attr.attr,
  636. &uv_query_max_secrets_attr.attr,
  637. NULL,
  638. };
  639. static struct attribute_group uv_query_attr_group = {
  640. .attrs = uv_query_attrs,
  641. };
  642. static ssize_t uv_is_prot_virt_guest(struct kobject *kobj,
  643. struct kobj_attribute *attr, char *buf)
  644. {
  645. return sysfs_emit(buf, "%d\n", prot_virt_guest);
  646. }
  647. static ssize_t uv_is_prot_virt_host(struct kobject *kobj,
  648. struct kobj_attribute *attr, char *buf)
  649. {
  650. return sysfs_emit(buf, "%d\n", prot_virt_host);
  651. }
  652. static struct kobj_attribute uv_prot_virt_guest =
  653. __ATTR(prot_virt_guest, 0444, uv_is_prot_virt_guest, NULL);
  654. static struct kobj_attribute uv_prot_virt_host =
  655. __ATTR(prot_virt_host, 0444, uv_is_prot_virt_host, NULL);
  656. static const struct attribute *uv_prot_virt_attrs[] = {
  657. &uv_prot_virt_guest.attr,
  658. &uv_prot_virt_host.attr,
  659. NULL,
  660. };
  661. static struct kset *uv_query_kset;
  662. static struct kobject *uv_kobj;
  663. static int __init uv_info_init(void)
  664. {
  665. int rc = -ENOMEM;
  666. if (!test_facility(158))
  667. return 0;
  668. uv_kobj = kobject_create_and_add("uv", firmware_kobj);
  669. if (!uv_kobj)
  670. return -ENOMEM;
  671. rc = sysfs_create_files(uv_kobj, uv_prot_virt_attrs);
  672. if (rc)
  673. goto out_kobj;
  674. uv_query_kset = kset_create_and_add("query", NULL, uv_kobj);
  675. if (!uv_query_kset) {
  676. rc = -ENOMEM;
  677. goto out_ind_files;
  678. }
  679. rc = sysfs_create_group(&uv_query_kset->kobj, &uv_query_attr_group);
  680. if (!rc)
  681. return 0;
  682. kset_unregister(uv_query_kset);
  683. out_ind_files:
  684. sysfs_remove_files(uv_kobj, uv_prot_virt_attrs);
  685. out_kobj:
  686. kobject_del(uv_kobj);
  687. kobject_put(uv_kobj);
  688. return rc;
  689. }
  690. device_initcall(uv_info_init);