1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* 3 * Guest Ultravisor Call tests 4 * 5 * Copyright (c) 2020 IBM Corp 6 * 7 * Authors: 8 * Janosch Frank <frankja@linux.ibm.com> 9 */ 10 11 #include <libcflat.h> 12 #include <alloc_page.h> 13 #include <asm/page.h> 14 #include <asm/asm-offsets.h> 15 #include <asm/interrupt.h> 16 #include <asm/facility.h> 17 #include <asm/uv.h> 18 #include <sclp.h> 19 #include <uv.h> 20 21 static unsigned long page; 22 23 static void test_priv(void) 24 { 25 struct uv_cb_header uvcb = {}; 26 27 report_prefix_push("privileged"); 28 29 report_prefix_push("query"); 30 uvcb.cmd = UVC_CMD_QUI; 31 uvcb.len = sizeof(struct uv_cb_qui); 32 expect_pgm_int(); 33 enter_pstate(); 34 uv_call_once(0, (u64)&uvcb); 35 check_pgm_int_code(PGM_INT_CODE_PRIVILEGED_OPERATION); 36 report_prefix_pop(); 37 38 report_prefix_push("share"); 39 uvcb.cmd = UVC_CMD_SET_SHARED_ACCESS; 40 uvcb.len = sizeof(struct uv_cb_share); 41 expect_pgm_int(); 42 enter_pstate(); 43 uv_call_once(0, (u64)&uvcb); 44 check_pgm_int_code(PGM_INT_CODE_PRIVILEGED_OPERATION); 45 report_prefix_pop(); 46 47 report_prefix_push("unshare"); 48 uvcb.cmd = UVC_CMD_REMOVE_SHARED_ACCESS; 49 uvcb.len = sizeof(struct uv_cb_share); 50 expect_pgm_int(); 51 enter_pstate(); 52 uv_call_once(0, (u64)&uvcb); 53 check_pgm_int_code(PGM_INT_CODE_PRIVILEGED_OPERATION); 54 report_prefix_pop(); 55 56 report_prefix_pop(); 57 } 58 59 static void test_query(void) 60 { 61 struct uv_cb_qui uvcb = { 62 .header.cmd = UVC_CMD_QUI, 63 /* A dword below the minimum length */ 64 .header.len = 0xa0, 65 }; 66 int cc; 67 68 report_prefix_push("query"); 69 cc = uv_call(0, (u64)&uvcb); 70 report(cc == 1 && uvcb.header.rc == UVC_RC_INV_LEN, "length"); 71 72 uvcb.header.len = sizeof(uvcb); 73 cc = uv_call(0, (u64)&uvcb); 74 report((!cc && uvcb.header.rc == UVC_RC_EXECUTED) || 75 (cc == 1 && uvcb.header.rc == 0x100), 76 "successful query"); 77 78 /* 79 * These bits have been introduced with the very first 80 * Ultravisor version and are expected to always be available 81 * because they are basic building blocks. 82 */ 83 report(test_bit_inv(BIT_UVC_CMD_QUI, &uvcb.inst_calls_list[0]), 84 "query indicated"); 85 report(test_bit_inv(BIT_UVC_CMD_SET_SHARED_ACCESS, &uvcb.inst_calls_list[0]), 86 "share indicated"); 87 report(test_bit_inv(BIT_UVC_CMD_REMOVE_SHARED_ACCESS, &uvcb.inst_calls_list[0]), 88 "unshare indicated"); 89 report_prefix_pop(); 90 } 91 92 static void test_sharing(void) 93 { 94 struct uv_cb_share uvcb = { 95 .header.cmd = UVC_CMD_SET_SHARED_ACCESS, 96 .header.len = sizeof(uvcb) - 8, 97 .paddr = page, 98 }; 99 int cc; 100 101 report_prefix_push("share"); 102 cc = uv_call(0, (u64)&uvcb); 103 report(cc == 1 && uvcb.header.rc == UVC_RC_INV_LEN, "length"); 104 uvcb.header.len = sizeof(uvcb); 105 cc = uv_call(0, (u64)&uvcb); 106 report(cc == 0 && uvcb.header.rc == UVC_RC_EXECUTED, "share"); 107 uvcb.paddr = get_ram_size() + PAGE_SIZE; 108 cc = uv_call(0, (u64)&uvcb); 109 report(cc == 1 && uvcb.header.rc == 0x101, "invalid memory"); 110 uvcb.paddr = page; 111 report_prefix_pop(); 112 113 report_prefix_push("unshare"); 114 uvcb.header.cmd = UVC_CMD_REMOVE_SHARED_ACCESS; 115 uvcb.header.len -= 8; 116 cc = uv_call(0, (u64)&uvcb); 117 report(cc == 1 && uvcb.header.rc == UVC_RC_INV_LEN, "length"); 118 uvcb.header.len = sizeof(uvcb); 119 cc = uv_call(0, (u64)&uvcb); 120 report(cc == 0 && uvcb.header.rc == UVC_RC_EXECUTED, "unshare"); 121 report_prefix_pop(); 122 123 report_prefix_pop(); 124 } 125 126 static struct { 127 const char *name; 128 uint16_t cmd; 129 uint16_t len; 130 int call_bit; 131 } invalid_cmds[] = { 132 { "bogus", 0x4242, sizeof(struct uv_cb_header), -1 }, 133 { "init", UVC_CMD_INIT_UV, sizeof(struct uv_cb_init), BIT_UVC_CMD_INIT_UV }, 134 { "create conf", UVC_CMD_CREATE_SEC_CONF, sizeof(struct uv_cb_cgc), BIT_UVC_CMD_CREATE_SEC_CONF }, 135 { "destroy conf", UVC_CMD_DESTROY_SEC_CONF, sizeof(struct uv_cb_nodata), BIT_UVC_CMD_DESTROY_SEC_CONF }, 136 { "create cpu", UVC_CMD_CREATE_SEC_CPU, sizeof(struct uv_cb_csc), BIT_UVC_CMD_CREATE_SEC_CPU }, 137 { "destroy cpu", UVC_CMD_DESTROY_SEC_CPU, sizeof(struct uv_cb_nodata), BIT_UVC_CMD_DESTROY_SEC_CPU }, 138 { "conv to", UVC_CMD_CONV_TO_SEC_STOR, sizeof(struct uv_cb_cts), BIT_UVC_CMD_CONV_TO_SEC_STOR }, 139 { "conv from", UVC_CMD_CONV_FROM_SEC_STOR, sizeof(struct uv_cb_cfs), BIT_UVC_CMD_CONV_FROM_SEC_STOR }, 140 { "set sec conf", UVC_CMD_SET_SEC_CONF_PARAMS, sizeof(struct uv_cb_ssc), BIT_UVC_CMD_SET_SEC_PARMS }, 141 { "unpack", UVC_CMD_UNPACK_IMG, sizeof(struct uv_cb_unp), BIT_UVC_CMD_UNPACK_IMG }, 142 { "verify", UVC_CMD_VERIFY_IMG, sizeof(struct uv_cb_nodata), BIT_UVC_CMD_VERIFY_IMG }, 143 { "cpu reset", UVC_CMD_CPU_RESET, sizeof(struct uv_cb_nodata), BIT_UVC_CMD_CPU_RESET }, 144 { "cpu initial reset", UVC_CMD_CPU_RESET_INITIAL, sizeof(struct uv_cb_nodata), BIT_UVC_CMD_CPU_RESET_INITIAL }, 145 { "conf clear reset", UVC_CMD_PERF_CONF_CLEAR_RESET, sizeof(struct uv_cb_nodata), BIT_UVC_CMD_PREPARE_CLEAR_RESET }, 146 { "cpu clear reset", UVC_CMD_CPU_RESET_CLEAR, sizeof(struct uv_cb_nodata), BIT_UVC_CMD_CPU_PERFORM_CLEAR_RESET }, 147 { "cpu set state", UVC_CMD_CPU_SET_STATE, sizeof(struct uv_cb_cpu_set_state), BIT_UVC_CMD_CPU_SET_STATE }, 148 { "pin shared", UVC_CMD_PIN_PAGE_SHARED, sizeof(struct uv_cb_cfs), BIT_UVC_CMD_PIN_PAGE_SHARED }, 149 { "unpin shared", UVC_CMD_UNPIN_PAGE_SHARED, sizeof(struct uv_cb_cts), BIT_UVC_CMD_UNPIN_PAGE_SHARED }, 150 { NULL, 0, 0 }, 151 }; 152 153 static void test_invalid(void) 154 { 155 struct uv_cb_header *hdr = (void *)page; 156 int cc, i; 157 158 report_prefix_push("invalid"); 159 for (i = 0; invalid_cmds[i].name; i++) { 160 hdr->cmd = invalid_cmds[i].cmd; 161 hdr->len = invalid_cmds[i].len; 162 cc = uv_call(0, (u64)hdr); 163 report(cc == 1 && hdr->rc == UVC_RC_INV_CMD && 164 (invalid_cmds[i].call_bit == -1 || !uv_query_test_call(invalid_cmds[i].call_bit)), 165 "%s", invalid_cmds[i].name); 166 } 167 report_prefix_pop(); 168 } 169 170 int main(void) 171 { 172 bool has_uvc = test_facility(158); 173 174 report_prefix_push("uvc"); 175 if (!has_uvc) { 176 report_skip("Ultravisor call facility is not available"); 177 goto done; 178 } 179 180 if (!uv_os_is_guest()) { 181 report_skip("Not a protected guest"); 182 goto done; 183 } 184 185 page = (unsigned long)alloc_page(); 186 test_priv(); 187 test_invalid(); 188 test_query(); 189 test_sharing(); 190 free_page((void *)page); 191 done: 192 report_prefix_pop(); 193 return report_summary(); 194 } 195