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 assert(vm->sblk->icptcode != ICPT_VALIDITY); 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 vm.sblk = alloc_page(); 114 115 vm.sblk->cpuflags = CPUSTAT_ZARCH | CPUSTAT_RUNNING; 116 vm.sblk->prefix = 0; 117 /* 118 * Pageable guest with the same ASCE as the test programm, but 119 * the guest memory 0x0 is offset to start at the allocated 120 * guest pages and end after 1MB. 121 * 122 * It's not pretty but faster and easier than managing guest ASCEs. 123 */ 124 vm.sblk->mso = (u64)guest; 125 vm.sblk->msl = (u64)guest; 126 vm.sblk->ihcpu = 0xffff; 127 128 vm.sblk->crycbd = (uint64_t)alloc_page(); 129 130 vm.sblk->gpsw.addr = PAGE_SIZE * 4; 131 vm.sblk->gpsw.mask = 0x0000000180000000ULL; 132 vm.sblk->ictl = ICTL_OPEREXC | ICTL_PINT; 133 /* Enable MVPG interpretation as we want to test KVM and not ourselves */ 134 vm.sblk->eca = ECA_MVPGI; 135 136 src = guest + PAGE_SIZE * 6; 137 dst = guest + PAGE_SIZE * 5; 138 cmp = alloc_page(); 139 memset(cmp, 0, PAGE_SIZE); 140 } 141 142 int main(void) 143 { 144 report_prefix_push("mvpg-sie"); 145 if (!sclp_facilities.has_sief2) { 146 report_skip("SIEF2 facility unavailable"); 147 goto done; 148 } 149 150 setup_guest(); 151 test_mvpg(); 152 test_mvpg_pei(); 153 154 done: 155 report_prefix_pop(); 156 return report_summary(); 157 158 } 159