1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* 3 * Tests mvpg SIE partial execution intercepts. 4 * 5 * Copyright 2021 IBM Corp. 6 * 7 * Authors: 8 * Janosch Frank <frankja@linux.ibm.com> 9 */ 10 #include <libcflat.h> 11 #include <asm/asm-offsets.h> 12 #include <asm-generic/barrier.h> 13 #include <asm/pgtable.h> 14 #include <mmu.h> 15 #include <asm/page.h> 16 #include <asm/facility.h> 17 #include <asm/mem.h> 18 #include <alloc_page.h> 19 #include <vm.h> 20 #include <sclp.h> 21 #include <sie.h> 22 23 static u8 *guest; 24 static u8 *guest_instr; 25 static struct vm vm; 26 27 static uint8_t *src; 28 static uint8_t *dst; 29 static uint8_t *cmp; 30 31 extern const char _binary_s390x_snippets_c_mvpg_snippet_gbin_start[]; 32 extern const char _binary_s390x_snippets_c_mvpg_snippet_gbin_end[]; 33 int binary_size; 34 35 static void sie(struct vm *vm) 36 { 37 /* Reset icptcode so we don't trip over it below */ 38 vm->sblk->icptcode = 0; 39 40 while (vm->sblk->icptcode == 0) { 41 sie64a(vm->sblk, &vm->save_area); 42 sie_handle_validity(vm); 43 } 44 vm->save_area.guest.grs[14] = vm->sblk->gg14; 45 vm->save_area.guest.grs[15] = vm->sblk->gg15; 46 } 47 48 static void test_mvpg_pei(void) 49 { 50 uint64_t **pei_dst = (uint64_t **)((uintptr_t) vm.sblk + 0xc0); 51 uint64_t **pei_src = (uint64_t **)((uintptr_t) vm.sblk + 0xc8); 52 53 report_prefix_push("pei"); 54 55 report_prefix_push("src"); 56 memset(dst, 0, PAGE_SIZE); 57 protect_page(src, PAGE_ENTRY_I); 58 sie(&vm); 59 report(vm.sblk->icptcode == ICPT_PARTEXEC, "Partial execution"); 60 report((uintptr_t)**pei_src == (uintptr_t)src + PAGE_ENTRY_I, "PEI_SRC correct"); 61 report((uintptr_t)**pei_dst == (uintptr_t)dst, "PEI_DST correct"); 62 unprotect_page(src, PAGE_ENTRY_I); 63 report(!memcmp(cmp, dst, PAGE_SIZE), "Destination intact"); 64 /* 65 * We need to execute the diag44 which is used as a blocker 66 * behind the mvpg. It makes sure we fail the tests above if 67 * the mvpg wouldn't have intercepted. 68 */ 69 sie(&vm); 70 /* Make sure we intercepted for the diag44 and nothing else */ 71 assert(vm.sblk->icptcode == ICPT_INST && 72 vm.sblk->ipa == 0x8300 && vm.sblk->ipb == 0x440000); 73 report_prefix_pop(); 74 75 /* Clear PEI data for next check */ 76 report_prefix_push("dst"); 77 memset((uint64_t *)((uintptr_t) vm.sblk + 0xc0), 0, 16); 78 memset(dst, 0, PAGE_SIZE); 79 protect_page(dst, PAGE_ENTRY_I); 80 sie(&vm); 81 report(vm.sblk->icptcode == ICPT_PARTEXEC, "Partial execution"); 82 report((uintptr_t)**pei_src == (uintptr_t)src, "PEI_SRC correct"); 83 report((uintptr_t)**pei_dst == (uintptr_t)dst + PAGE_ENTRY_I, "PEI_DST correct"); 84 /* Needed for the memcmp and general cleanup */ 85 unprotect_page(dst, PAGE_ENTRY_I); 86 report(!memcmp(cmp, dst, PAGE_SIZE), "Destination intact"); 87 report_prefix_pop(); 88 89 report_prefix_pop(); 90 } 91 92 static void test_mvpg(void) 93 { 94 int binary_size = ((uintptr_t)_binary_s390x_snippets_c_mvpg_snippet_gbin_end - 95 (uintptr_t)_binary_s390x_snippets_c_mvpg_snippet_gbin_start); 96 97 memcpy(guest, _binary_s390x_snippets_c_mvpg_snippet_gbin_start, binary_size); 98 memset(src, 0x42, PAGE_SIZE); 99 memset(dst, 0x43, PAGE_SIZE); 100 sie(&vm); 101 report(!memcmp(src, dst, PAGE_SIZE) && *dst == 0x42, "Page moved"); 102 } 103 104 static void setup_guest(void) 105 { 106 setup_vm(); 107 108 /* Allocate 1MB as guest memory */ 109 guest = alloc_pages(8); 110 /* The first two pages are the lowcore */ 111 guest_instr = guest + PAGE_SIZE * 2; 112 113 sie_guest_create(&vm, (uint64_t)guest, HPAGE_SIZE); 114 115 vm.sblk->gpsw.addr = PAGE_SIZE * 4; 116 vm.sblk->gpsw.mask = 0x0000000180000000ULL; 117 vm.sblk->ictl = ICTL_OPEREXC | ICTL_PINT; 118 /* Enable MVPG interpretation as we want to test KVM and not ourselves */ 119 vm.sblk->eca = ECA_MVPGI; 120 121 src = guest + PAGE_SIZE * 6; 122 dst = guest + PAGE_SIZE * 5; 123 cmp = alloc_page(); 124 memset(cmp, 0, PAGE_SIZE); 125 } 126 127 int main(void) 128 { 129 report_prefix_push("mvpg-sie"); 130 if (!sclp_facilities.has_sief2) { 131 report_skip("SIEF2 facility unavailable"); 132 goto done; 133 } 134 135 setup_guest(); 136 test_mvpg(); 137 test_mvpg_pei(); 138 sie_guest_destroy(&vm); 139 140 done: 141 report_prefix_pop(); 142 return report_summary(); 143 144 } 145