1 /* 2 * Tests guarded storage support. 3 * 4 * Copyright 2018 IBM Corp. 5 * 6 * Authors: 7 * Martin Schwidefsky <schwidefsky@de.ibm.com> 8 * Janosch Frank <frankja@de.ibm.com> 9 * 10 * This code is free software; you can redistribute it and/or modify it 11 * under the terms of the GNU Library General Public License version 2. 12 */ 13 #include <libcflat.h> 14 #include <asm/page.h> 15 #include <asm/facility.h> 16 #include <asm/interrupt.h> 17 #include <asm-generic/barrier.h> 18 19 struct gs_cb { 20 uint64_t reserved; 21 uint64_t gsd; 22 uint64_t gssm; 23 uint64_t gs_epl_a; 24 }; 25 26 struct gs_epl { 27 uint8_t pad1; 28 union { 29 uint8_t gs_eam; 30 struct { 31 uint8_t : 6; 32 uint8_t e : 1; 33 uint8_t b : 1; 34 }; 35 }; 36 union { 37 uint8_t gs_eci; 38 struct { 39 uint8_t tx : 1; 40 uint8_t cx : 1; 41 uint8_t : 5; 42 uint8_t in : 1; 43 }; 44 }; 45 union { 46 uint8_t gs_eai; 47 struct { 48 uint8_t : 1; 49 uint8_t t : 1; 50 uint8_t as : 2; 51 uint8_t ar : 4; 52 }; 53 }; 54 uint32_t pad2; 55 uint64_t gs_eha; 56 uint64_t gs_eia; 57 uint64_t gs_eoa; 58 uint64_t gs_eir; 59 uint64_t gs_era; 60 }; 61 62 static volatile int guarded = 0; 63 static struct gs_cb gs_cb; 64 static struct gs_epl gs_epl; 65 static unsigned long gs_area = 0x2000000; 66 67 static inline void load_gs_cb(struct gs_cb *gs_cb) 68 { 69 asm volatile(".insn rxy,0xe3000000004d,0,%0" : : "Q" (*gs_cb)); 70 } 71 72 static inline void store_gs_cb(struct gs_cb *gs_cb) 73 { 74 asm volatile(".insn rxy,0xe30000000049,0,%0" : : "Q" (*gs_cb)); 75 } 76 77 static inline unsigned long load_guarded(unsigned long *p) 78 { 79 unsigned long v; 80 81 asm(".insn rxy,0xe3000000004c, %0,%1" 82 : "=d" (v) 83 : "m" (*p) 84 : "r14", "memory"); 85 return v; 86 } 87 88 /* guarded-storage event handler and finally it calls gs_handler */ 89 extern void gs_handler_asm(void); 90 asm(".globl gs_handler_asm\n" 91 "gs_handler_asm:\n" 92 " lgr %r14,%r15\n" /* Save current stack address in r14 */ 93 " aghi %r15,-320\n" /* Allocate stack frame */ 94 " stmg %r0,%r13,192(%r15)\n" /* Store regs to save area */ 95 " stg %r14,312(%r15)\n" 96 " la %r2,160(%r15)\n" /* Store gscb address in this_cb */ 97 " .insn rxy,0xe30000000049,0,160(%r15)\n" /* stgsc */ 98 " lg %r14,24(%r2)\n" /* Get GSEPLA from GSCB*/ 99 " lg %r14,40(%r14)\n" /* Get GSERA from GSEPL*/ 100 " stg %r14,304(%r15)\n" /* Store GSERA in r14 of reg save area */ 101 " brasl %r14,gs_handler\n" /* Jump to gs_handler */ 102 " lmg %r0,%r15,192(%r15)\n" /* Restore regs */ 103 " aghi %r14, 6\n" /* Add lgg instr len to GSERA */ 104 " br %r14\n" /* Jump to next instruction after lgg */ 105 " .size gs_handler_asm,.-gs_handler_asm\n"); 106 107 void gs_handler(struct gs_cb *this_cb) 108 { 109 guarded = 1; 110 struct gs_epl *gs_epl = (struct gs_epl *) this_cb->gs_epl_a; 111 printf("gs_handler called for %016lx at %016lx\n", 112 gs_epl->gs_eir, gs_epl->gs_eia); 113 } 114 115 /* Test if load guarded gets intercepted. */ 116 static void test_load(void) 117 { 118 unsigned long v; 119 120 guarded = 0; 121 v = load_guarded(&gs_area); 122 report("load guarded %ld", guarded, v); 123 guarded = 0; 124 } 125 126 /* Test gs instructions without enablement resulting in an exception */ 127 static void test_special(void) 128 { 129 expect_pgm_int(); 130 load_gs_cb(&gs_cb); 131 check_pgm_int_code(PGM_INT_CODE_SPECIAL_OPERATION); 132 expect_pgm_int(); 133 store_gs_cb(&gs_cb); 134 check_pgm_int_code(PGM_INT_CODE_SPECIAL_OPERATION); 135 } 136 137 static void init(void) 138 { 139 /* Enable control bit for gs */ 140 ctl_set_bit(2, 4); 141 142 /* Setup gs registers to guard the gs_area */ 143 gs_cb.gsd = gs_area | 25; 144 145 /* Check all 512kb slots for events */ 146 gs_cb.gssm = 0xffffffffffffffffULL; 147 gs_cb.gs_epl_a = (unsigned long) &gs_epl; 148 149 /* Register handler */ 150 gs_epl.gs_eha = (unsigned long) gs_handler_asm; 151 load_gs_cb(&gs_cb); 152 } 153 154 int main(void) 155 { 156 bool has_gs = test_facility(133); 157 158 report_prefix_push("gs"); 159 report_xfail("Guarded storage available", !has_gs, has_gs); 160 if (!has_gs) 161 goto done; 162 163 test_special(); 164 init(); 165 test_load(); 166 167 done: 168 report_prefix_pop(); 169 return report_summary(); 170 } 171