uv.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Ultravisor Interfaces
  4. *
  5. * Copyright IBM Corp. 2019, 2022
  6. *
  7. * Author(s):
  8. * Vasily Gorbik <[email protected]>
  9. * Janosch Frank <[email protected]>
  10. */
  11. #ifndef _ASM_S390_UV_H
  12. #define _ASM_S390_UV_H
  13. #include <linux/types.h>
  14. #include <linux/errno.h>
  15. #include <linux/bug.h>
  16. #include <linux/sched.h>
  17. #include <asm/page.h>
  18. #include <asm/gmap.h>
  19. #define UVC_CC_OK 0
  20. #define UVC_CC_ERROR 1
  21. #define UVC_CC_BUSY 2
  22. #define UVC_CC_PARTIAL 3
  23. #define UVC_RC_EXECUTED 0x0001
  24. #define UVC_RC_INV_CMD 0x0002
  25. #define UVC_RC_INV_STATE 0x0003
  26. #define UVC_RC_INV_LEN 0x0005
  27. #define UVC_RC_NO_RESUME 0x0007
  28. #define UVC_RC_NEED_DESTROY 0x8000
  29. #define UVC_CMD_QUI 0x0001
  30. #define UVC_CMD_INIT_UV 0x000f
  31. #define UVC_CMD_CREATE_SEC_CONF 0x0100
  32. #define UVC_CMD_DESTROY_SEC_CONF 0x0101
  33. #define UVC_CMD_CREATE_SEC_CPU 0x0120
  34. #define UVC_CMD_DESTROY_SEC_CPU 0x0121
  35. #define UVC_CMD_CONV_TO_SEC_STOR 0x0200
  36. #define UVC_CMD_CONV_FROM_SEC_STOR 0x0201
  37. #define UVC_CMD_DESTR_SEC_STOR 0x0202
  38. #define UVC_CMD_SET_SEC_CONF_PARAMS 0x0300
  39. #define UVC_CMD_UNPACK_IMG 0x0301
  40. #define UVC_CMD_VERIFY_IMG 0x0302
  41. #define UVC_CMD_CPU_RESET 0x0310
  42. #define UVC_CMD_CPU_RESET_INITIAL 0x0311
  43. #define UVC_CMD_PREPARE_RESET 0x0320
  44. #define UVC_CMD_CPU_RESET_CLEAR 0x0321
  45. #define UVC_CMD_CPU_SET_STATE 0x0330
  46. #define UVC_CMD_SET_UNSHARE_ALL 0x0340
  47. #define UVC_CMD_PIN_PAGE_SHARED 0x0341
  48. #define UVC_CMD_UNPIN_PAGE_SHARED 0x0342
  49. #define UVC_CMD_DUMP_INIT 0x0400
  50. #define UVC_CMD_DUMP_CONF_STOR_STATE 0x0401
  51. #define UVC_CMD_DUMP_CPU 0x0402
  52. #define UVC_CMD_DUMP_COMPLETE 0x0403
  53. #define UVC_CMD_SET_SHARED_ACCESS 0x1000
  54. #define UVC_CMD_REMOVE_SHARED_ACCESS 0x1001
  55. #define UVC_CMD_RETR_ATTEST 0x1020
  56. /* Bits in installed uv calls */
  57. enum uv_cmds_inst {
  58. BIT_UVC_CMD_QUI = 0,
  59. BIT_UVC_CMD_INIT_UV = 1,
  60. BIT_UVC_CMD_CREATE_SEC_CONF = 2,
  61. BIT_UVC_CMD_DESTROY_SEC_CONF = 3,
  62. BIT_UVC_CMD_CREATE_SEC_CPU = 4,
  63. BIT_UVC_CMD_DESTROY_SEC_CPU = 5,
  64. BIT_UVC_CMD_CONV_TO_SEC_STOR = 6,
  65. BIT_UVC_CMD_CONV_FROM_SEC_STOR = 7,
  66. BIT_UVC_CMD_SET_SHARED_ACCESS = 8,
  67. BIT_UVC_CMD_REMOVE_SHARED_ACCESS = 9,
  68. BIT_UVC_CMD_SET_SEC_PARMS = 11,
  69. BIT_UVC_CMD_UNPACK_IMG = 13,
  70. BIT_UVC_CMD_VERIFY_IMG = 14,
  71. BIT_UVC_CMD_CPU_RESET = 15,
  72. BIT_UVC_CMD_CPU_RESET_INITIAL = 16,
  73. BIT_UVC_CMD_CPU_SET_STATE = 17,
  74. BIT_UVC_CMD_PREPARE_RESET = 18,
  75. BIT_UVC_CMD_CPU_PERFORM_CLEAR_RESET = 19,
  76. BIT_UVC_CMD_UNSHARE_ALL = 20,
  77. BIT_UVC_CMD_PIN_PAGE_SHARED = 21,
  78. BIT_UVC_CMD_UNPIN_PAGE_SHARED = 22,
  79. BIT_UVC_CMD_DUMP_INIT = 24,
  80. BIT_UVC_CMD_DUMP_CONFIG_STOR_STATE = 25,
  81. BIT_UVC_CMD_DUMP_CPU = 26,
  82. BIT_UVC_CMD_DUMP_COMPLETE = 27,
  83. BIT_UVC_CMD_RETR_ATTEST = 28,
  84. };
  85. enum uv_feat_ind {
  86. BIT_UV_FEAT_MISC = 0,
  87. BIT_UV_FEAT_AIV = 1,
  88. };
  89. struct uv_cb_header {
  90. u16 len;
  91. u16 cmd; /* Command Code */
  92. u16 rc; /* Response Code */
  93. u16 rrc; /* Return Reason Code */
  94. } __packed __aligned(8);
  95. /* Query Ultravisor Information */
  96. struct uv_cb_qui {
  97. struct uv_cb_header header; /* 0x0000 */
  98. u64 reserved08; /* 0x0008 */
  99. u64 inst_calls_list[4]; /* 0x0010 */
  100. u64 reserved30[2]; /* 0x0030 */
  101. u64 uv_base_stor_len; /* 0x0040 */
  102. u64 reserved48; /* 0x0048 */
  103. u64 conf_base_phys_stor_len; /* 0x0050 */
  104. u64 conf_base_virt_stor_len; /* 0x0058 */
  105. u64 conf_virt_var_stor_len; /* 0x0060 */
  106. u64 cpu_stor_len; /* 0x0068 */
  107. u32 reserved70[3]; /* 0x0070 */
  108. u32 max_num_sec_conf; /* 0x007c */
  109. u64 max_guest_stor_addr; /* 0x0080 */
  110. u8 reserved88[158 - 136]; /* 0x0088 */
  111. u16 max_guest_cpu_id; /* 0x009e */
  112. u64 uv_feature_indications; /* 0x00a0 */
  113. u64 reserveda8; /* 0x00a8 */
  114. u64 supp_se_hdr_versions; /* 0x00b0 */
  115. u64 supp_se_hdr_pcf; /* 0x00b8 */
  116. u64 reservedc0; /* 0x00c0 */
  117. u64 conf_dump_storage_state_len; /* 0x00c8 */
  118. u64 conf_dump_finalize_len; /* 0x00d0 */
  119. u64 reservedd8; /* 0x00d8 */
  120. u64 supp_att_req_hdr_ver; /* 0x00e0 */
  121. u64 supp_att_pflags; /* 0x00e8 */
  122. u8 reservedf0[256 - 240]; /* 0x00f0 */
  123. } __packed __aligned(8);
  124. /* Initialize Ultravisor */
  125. struct uv_cb_init {
  126. struct uv_cb_header header;
  127. u64 reserved08[2];
  128. u64 stor_origin;
  129. u64 stor_len;
  130. u64 reserved28[4];
  131. } __packed __aligned(8);
  132. /* Create Guest Configuration */
  133. struct uv_cb_cgc {
  134. struct uv_cb_header header;
  135. u64 reserved08[2];
  136. u64 guest_handle;
  137. u64 conf_base_stor_origin;
  138. u64 conf_virt_stor_origin;
  139. u64 reserved30;
  140. u64 guest_stor_origin;
  141. u64 guest_stor_len;
  142. u64 guest_sca;
  143. u64 guest_asce;
  144. u64 reserved58[5];
  145. } __packed __aligned(8);
  146. /* Create Secure CPU */
  147. struct uv_cb_csc {
  148. struct uv_cb_header header;
  149. u64 reserved08[2];
  150. u64 cpu_handle;
  151. u64 guest_handle;
  152. u64 stor_origin;
  153. u8 reserved30[6];
  154. u16 num;
  155. u64 state_origin;
  156. u64 reserved40[4];
  157. } __packed __aligned(8);
  158. /* Convert to Secure */
  159. struct uv_cb_cts {
  160. struct uv_cb_header header;
  161. u64 reserved08[2];
  162. u64 guest_handle;
  163. u64 gaddr;
  164. } __packed __aligned(8);
  165. /* Convert from Secure / Pin Page Shared */
  166. struct uv_cb_cfs {
  167. struct uv_cb_header header;
  168. u64 reserved08[2];
  169. u64 paddr;
  170. } __packed __aligned(8);
  171. /* Set Secure Config Parameter */
  172. struct uv_cb_ssc {
  173. struct uv_cb_header header;
  174. u64 reserved08[2];
  175. u64 guest_handle;
  176. u64 sec_header_origin;
  177. u32 sec_header_len;
  178. u32 reserved2c;
  179. u64 reserved30[4];
  180. } __packed __aligned(8);
  181. /* Unpack */
  182. struct uv_cb_unp {
  183. struct uv_cb_header header;
  184. u64 reserved08[2];
  185. u64 guest_handle;
  186. u64 gaddr;
  187. u64 tweak[2];
  188. u64 reserved38[3];
  189. } __packed __aligned(8);
  190. #define PV_CPU_STATE_OPR 1
  191. #define PV_CPU_STATE_STP 2
  192. #define PV_CPU_STATE_CHKSTP 3
  193. #define PV_CPU_STATE_OPR_LOAD 5
  194. struct uv_cb_cpu_set_state {
  195. struct uv_cb_header header;
  196. u64 reserved08[2];
  197. u64 cpu_handle;
  198. u8 reserved20[7];
  199. u8 state;
  200. u64 reserved28[5];
  201. };
  202. /*
  203. * A common UV call struct for calls that take no payload
  204. * Examples:
  205. * Destroy cpu/config
  206. * Verify
  207. */
  208. struct uv_cb_nodata {
  209. struct uv_cb_header header;
  210. u64 reserved08[2];
  211. u64 handle;
  212. u64 reserved20[4];
  213. } __packed __aligned(8);
  214. /* Set Shared Access */
  215. struct uv_cb_share {
  216. struct uv_cb_header header;
  217. u64 reserved08[3];
  218. u64 paddr;
  219. u64 reserved28;
  220. } __packed __aligned(8);
  221. /* Retrieve Attestation Measurement */
  222. struct uv_cb_attest {
  223. struct uv_cb_header header; /* 0x0000 */
  224. u64 reserved08[2]; /* 0x0008 */
  225. u64 arcb_addr; /* 0x0018 */
  226. u64 cont_token; /* 0x0020 */
  227. u8 reserved28[6]; /* 0x0028 */
  228. u16 user_data_len; /* 0x002e */
  229. u8 user_data[256]; /* 0x0030 */
  230. u32 reserved130[3]; /* 0x0130 */
  231. u32 meas_len; /* 0x013c */
  232. u64 meas_addr; /* 0x0140 */
  233. u8 config_uid[16]; /* 0x0148 */
  234. u32 reserved158; /* 0x0158 */
  235. u32 add_data_len; /* 0x015c */
  236. u64 add_data_addr; /* 0x0160 */
  237. u64 reserved168[4]; /* 0x0168 */
  238. } __packed __aligned(8);
  239. struct uv_cb_dump_cpu {
  240. struct uv_cb_header header;
  241. u64 reserved08[2];
  242. u64 cpu_handle;
  243. u64 dump_area_origin;
  244. u64 reserved28[5];
  245. } __packed __aligned(8);
  246. struct uv_cb_dump_stor_state {
  247. struct uv_cb_header header;
  248. u64 reserved08[2];
  249. u64 config_handle;
  250. u64 dump_area_origin;
  251. u64 gaddr;
  252. u64 reserved28[4];
  253. } __packed __aligned(8);
  254. struct uv_cb_dump_complete {
  255. struct uv_cb_header header;
  256. u64 reserved08[2];
  257. u64 config_handle;
  258. u64 dump_area_origin;
  259. u64 reserved30[5];
  260. } __packed __aligned(8);
  261. static inline int __uv_call(unsigned long r1, unsigned long r2)
  262. {
  263. int cc;
  264. asm volatile(
  265. " .insn rrf,0xB9A40000,%[r1],%[r2],0,0\n"
  266. " ipm %[cc]\n"
  267. " srl %[cc],28\n"
  268. : [cc] "=d" (cc)
  269. : [r1] "a" (r1), [r2] "a" (r2)
  270. : "memory", "cc");
  271. return cc;
  272. }
  273. static inline int uv_call(unsigned long r1, unsigned long r2)
  274. {
  275. int cc;
  276. do {
  277. cc = __uv_call(r1, r2);
  278. } while (cc > 1);
  279. return cc;
  280. }
  281. /* Low level uv_call that avoids stalls for long running busy conditions */
  282. static inline int uv_call_sched(unsigned long r1, unsigned long r2)
  283. {
  284. int cc;
  285. do {
  286. cc = __uv_call(r1, r2);
  287. cond_resched();
  288. } while (cc > 1);
  289. return cc;
  290. }
  291. /*
  292. * special variant of uv_call that only transports the cpu or guest
  293. * handle and the command, like destroy or verify.
  294. */
  295. static inline int uv_cmd_nodata(u64 handle, u16 cmd, u16 *rc, u16 *rrc)
  296. {
  297. struct uv_cb_nodata uvcb = {
  298. .header.cmd = cmd,
  299. .header.len = sizeof(uvcb),
  300. .handle = handle,
  301. };
  302. int cc;
  303. WARN(!handle, "No handle provided to Ultravisor call cmd %x\n", cmd);
  304. cc = uv_call_sched(0, (u64)&uvcb);
  305. *rc = uvcb.header.rc;
  306. *rrc = uvcb.header.rrc;
  307. return cc ? -EINVAL : 0;
  308. }
  309. struct uv_info {
  310. unsigned long inst_calls_list[4];
  311. unsigned long uv_base_stor_len;
  312. unsigned long guest_base_stor_len;
  313. unsigned long guest_virt_base_stor_len;
  314. unsigned long guest_virt_var_stor_len;
  315. unsigned long guest_cpu_stor_len;
  316. unsigned long max_sec_stor_addr;
  317. unsigned int max_num_sec_conf;
  318. unsigned short max_guest_cpu_id;
  319. unsigned long uv_feature_indications;
  320. unsigned long supp_se_hdr_ver;
  321. unsigned long supp_se_hdr_pcf;
  322. unsigned long conf_dump_storage_state_len;
  323. unsigned long conf_dump_finalize_len;
  324. unsigned long supp_att_req_hdr_ver;
  325. unsigned long supp_att_pflags;
  326. };
  327. extern struct uv_info uv_info;
  328. #ifdef CONFIG_PROTECTED_VIRTUALIZATION_GUEST
  329. extern int prot_virt_guest;
  330. static inline int is_prot_virt_guest(void)
  331. {
  332. return prot_virt_guest;
  333. }
  334. static inline int share(unsigned long addr, u16 cmd)
  335. {
  336. struct uv_cb_share uvcb = {
  337. .header.cmd = cmd,
  338. .header.len = sizeof(uvcb),
  339. .paddr = addr
  340. };
  341. if (!is_prot_virt_guest())
  342. return -EOPNOTSUPP;
  343. /*
  344. * Sharing is page wise, if we encounter addresses that are
  345. * not page aligned, we assume something went wrong. If
  346. * malloced structs are passed to this function, we could leak
  347. * data to the hypervisor.
  348. */
  349. BUG_ON(addr & ~PAGE_MASK);
  350. if (!uv_call(0, (u64)&uvcb))
  351. return 0;
  352. return -EINVAL;
  353. }
  354. /*
  355. * Guest 2 request to the Ultravisor to make a page shared with the
  356. * hypervisor for IO.
  357. *
  358. * @addr: Real or absolute address of the page to be shared
  359. */
  360. static inline int uv_set_shared(unsigned long addr)
  361. {
  362. return share(addr, UVC_CMD_SET_SHARED_ACCESS);
  363. }
  364. /*
  365. * Guest 2 request to the Ultravisor to make a page unshared.
  366. *
  367. * @addr: Real or absolute address of the page to be unshared
  368. */
  369. static inline int uv_remove_shared(unsigned long addr)
  370. {
  371. return share(addr, UVC_CMD_REMOVE_SHARED_ACCESS);
  372. }
  373. #else
  374. #define is_prot_virt_guest() 0
  375. static inline int uv_set_shared(unsigned long addr) { return 0; }
  376. static inline int uv_remove_shared(unsigned long addr) { return 0; }
  377. #endif
  378. #if IS_ENABLED(CONFIG_KVM)
  379. extern int prot_virt_host;
  380. static inline int is_prot_virt_host(void)
  381. {
  382. return prot_virt_host;
  383. }
  384. int gmap_make_secure(struct gmap *gmap, unsigned long gaddr, void *uvcb);
  385. int gmap_destroy_page(struct gmap *gmap, unsigned long gaddr);
  386. int uv_destroy_owned_page(unsigned long paddr);
  387. int uv_convert_from_secure(unsigned long paddr);
  388. int uv_convert_owned_from_secure(unsigned long paddr);
  389. int gmap_convert_to_secure(struct gmap *gmap, unsigned long gaddr);
  390. void setup_uv(void);
  391. #else
  392. #define is_prot_virt_host() 0
  393. static inline void setup_uv(void) {}
  394. static inline int uv_destroy_owned_page(unsigned long paddr)
  395. {
  396. return 0;
  397. }
  398. static inline int uv_convert_from_secure(unsigned long paddr)
  399. {
  400. return 0;
  401. }
  402. static inline int uv_convert_owned_from_secure(unsigned long paddr)
  403. {
  404. return 0;
  405. }
  406. #endif
  407. #endif /* _ASM_S390_UV_H */