1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* 3 * Copyright (c) 2017 Red Hat Inc 4 * 5 * Authors: 6 * David Hildenbrand <david@redhat.com> 7 */ 8 #ifndef _ASMS390X_ARCH_DEF_H_ 9 #define _ASMS390X_ARCH_DEF_H_ 10 11 struct stack_frame { 12 struct stack_frame *back_chain; 13 uint64_t reserved; 14 /* GRs 2 - 5 */ 15 uint64_t argument_area[4]; 16 /* GRs 6 - 15 */ 17 uint64_t grs[10]; 18 /* FPRs 0, 2, 4, 6 */ 19 int64_t fprs[4]; 20 }; 21 22 struct stack_frame_int { 23 struct stack_frame *back_chain; 24 uint64_t reserved; 25 /* 26 * The GRs are offset compatible with struct stack_frame so we 27 * can easily fetch GR14 for backtraces. 28 */ 29 /* GRs 2 - 15 */ 30 uint64_t grs0[14]; 31 /* GRs 0 and 1 */ 32 uint64_t grs1[2]; 33 uint32_t reserved1; 34 uint32_t fpc; 35 uint64_t fprs[16]; 36 uint64_t crs[16]; 37 }; 38 39 struct psw { 40 union { 41 uint64_t mask; 42 struct { 43 uint64_t reserved00:1; 44 uint64_t per:1; 45 uint64_t reserved02:3; 46 uint64_t dat:1; 47 uint64_t io:1; 48 uint64_t ext:1; 49 uint64_t key:4; 50 uint64_t reserved12:1; 51 uint64_t mchk:1; 52 uint64_t wait:1; 53 uint64_t pstate:1; 54 uint64_t as:2; 55 uint64_t cc:2; 56 uint64_t prg_mask:4; 57 uint64_t reserved24:7; 58 uint64_t ea:1; 59 uint64_t ba:1; 60 uint64_t reserved33:31; 61 }; 62 }; 63 uint64_t addr; 64 }; 65 _Static_assert(sizeof(struct psw) == 16, "PSW size"); 66 67 #define PSW(m, a) ((struct psw){ .mask = (m), .addr = (uint64_t)(a) }) 68 69 struct short_psw { 70 uint32_t mask; 71 uint32_t addr; 72 }; 73 74 struct cpu { 75 struct lowcore *lowcore; 76 uint64_t *stack; 77 void (*pgm_cleanup_func)(struct stack_frame_int *); 78 void (*ext_cleanup_func)(struct stack_frame_int *); 79 uint16_t addr; 80 uint16_t idx; 81 bool active; 82 bool pgm_int_expected; 83 bool ext_int_expected; 84 bool in_interrupt_handler; 85 }; 86 87 enum address_space { 88 AS_PRIM = 0, 89 AS_ACCR = 1, 90 AS_SECN = 2, 91 AS_HOME = 3 92 }; 93 94 #define PSW_MASK_DAT 0x0400000000000000UL 95 #define PSW_MASK_IO 0x0200000000000000UL 96 #define PSW_MASK_EXT 0x0100000000000000UL 97 #define PSW_MASK_KEY 0x00F0000000000000UL 98 #define PSW_MASK_WAIT 0x0002000000000000UL 99 #define PSW_MASK_PSTATE 0x0001000000000000UL 100 #define PSW_MASK_EA 0x0000000100000000UL 101 #define PSW_MASK_BA 0x0000000080000000UL 102 #define PSW_MASK_64 (PSW_MASK_BA | PSW_MASK_EA) 103 104 #define CTL0_TRANSACT_EX_CTL (63 - 8) 105 #define CTL0_LOW_ADDR_PROT (63 - 35) 106 #define CTL0_EDAT (63 - 40) 107 #define CTL0_FETCH_PROTECTION_OVERRIDE (63 - 38) 108 #define CTL0_STORAGE_PROTECTION_OVERRIDE (63 - 39) 109 #define CTL0_IEP (63 - 43) 110 #define CTL0_AFP (63 - 45) 111 #define CTL0_VECTOR (63 - 46) 112 #define CTL0_EMERGENCY_SIGNAL (63 - 49) 113 #define CTL0_EXTERNAL_CALL (63 - 50) 114 #define CTL0_CLOCK_COMPARATOR (63 - 52) 115 #define CTL0_CPU_TIMER (63 - 53) 116 #define CTL0_SERVICE_SIGNAL (63 - 54) 117 #define CR0_EXTM_MASK 0x0000000000006200UL /* Combined external masks */ 118 119 #define CTL2_GUARDED_STORAGE (63 - 59) 120 121 struct lowcore { 122 uint8_t pad_0x0000[0x0080 - 0x0000]; /* 0x0000 */ 123 uint32_t ext_int_param; /* 0x0080 */ 124 uint16_t cpu_addr; /* 0x0084 */ 125 uint16_t ext_int_code; /* 0x0086 */ 126 uint16_t svc_int_id; /* 0x0088 */ 127 uint16_t svc_int_code; /* 0x008a */ 128 uint16_t pgm_int_id; /* 0x008c */ 129 uint16_t pgm_int_code; /* 0x008e */ 130 uint32_t dxc_vxc; /* 0x0090 */ 131 uint16_t mon_class_nb; /* 0x0094 */ 132 uint8_t per_code; /* 0x0096 */ 133 uint8_t per_atmid; /* 0x0097 */ 134 uint64_t per_addr; /* 0x0098 */ 135 uint8_t exc_acc_id; /* 0x00a0 */ 136 uint8_t per_acc_id; /* 0x00a1 */ 137 uint8_t op_acc_id; /* 0x00a2 */ 138 uint8_t arch_mode_id; /* 0x00a3 */ 139 uint8_t pad_0x00a4[0x00a8 - 0x00a4]; /* 0x00a4 */ 140 uint64_t trans_exc_id; /* 0x00a8 */ 141 uint64_t mon_code; /* 0x00b0 */ 142 uint32_t subsys_id_word; /* 0x00b8 */ 143 uint32_t io_int_param; /* 0x00bc */ 144 uint32_t io_int_word; /* 0x00c0 */ 145 uint8_t pad_0x00c4[0x00c8 - 0x00c4]; /* 0x00c4 */ 146 uint32_t stfl; /* 0x00c8 */ 147 uint8_t pad_0x00cc[0x00e8 - 0x00cc]; /* 0x00cc */ 148 uint64_t mcck_int_code; /* 0x00e8 */ 149 uint8_t pad_0x00f0[0x00f4 - 0x00f0]; /* 0x00f0 */ 150 uint32_t ext_damage_code; /* 0x00f4 */ 151 uint64_t failing_storage_addr; /* 0x00f8 */ 152 uint64_t emon_ca_origin; /* 0x0100 */ 153 uint32_t emon_ca_size; /* 0x0108 */ 154 uint32_t emon_exc_count; /* 0x010c */ 155 uint64_t breaking_event_addr; /* 0x0110 */ 156 uint8_t pad_0x0118[0x0120 - 0x0118]; /* 0x0118 */ 157 struct psw restart_old_psw; /* 0x0120 */ 158 struct psw ext_old_psw; /* 0x0130 */ 159 struct psw svc_old_psw; /* 0x0140 */ 160 struct psw pgm_old_psw; /* 0x0150 */ 161 struct psw mcck_old_psw; /* 0x0160 */ 162 struct psw io_old_psw; /* 0x0170 */ 163 uint8_t pad_0x0180[0x01a0 - 0x0180]; /* 0x0180 */ 164 struct psw restart_new_psw; /* 0x01a0 */ 165 struct psw ext_new_psw; /* 0x01b0 */ 166 struct psw svc_new_psw; /* 0x01c0 */ 167 struct psw pgm_new_psw; /* 0x01d0 */ 168 struct psw mcck_new_psw; /* 0x01e0 */ 169 struct psw io_new_psw; /* 0x01f0 */ 170 /* sw definition: save area for registers in interrupt handlers */ 171 uint64_t sw_int_grs[16]; /* 0x0200 */ 172 uint8_t pad_0x0280[0x0308 - 0x0280]; /* 0x0280 */ 173 uint64_t sw_int_crs[16]; /* 0x0308 */ 174 struct psw sw_int_psw; /* 0x0388 */ 175 struct cpu *this_cpu; /* 0x0398 */ 176 uint8_t pad_0x03a0[0x11b0 - 0x03a0]; /* 0x03a0 */ 177 uint64_t mcck_ext_sa_addr; /* 0x11b0 */ 178 uint8_t pad_0x11b8[0x1200 - 0x11b8]; /* 0x11b8 */ 179 uint64_t fprs_sa[16]; /* 0x1200 */ 180 uint64_t grs_sa[16]; /* 0x1280 */ 181 struct psw psw_sa; /* 0x1300 */ 182 uint8_t pad_0x1310[0x1318 - 0x1310]; /* 0x1310 */ 183 uint32_t prefix_sa; /* 0x1318 */ 184 uint32_t fpc_sa; /* 0x131c */ 185 uint8_t pad_0x1320[0x1324 - 0x1320]; /* 0x1320 */ 186 uint32_t tod_pr_sa; /* 0x1324 */ 187 uint64_t cputm_sa; /* 0x1328 */ 188 uint64_t cc_sa; /* 0x1330 */ 189 uint8_t pad_0x1338[0x1340 - 0x1338]; /* 0x1338 */ 190 uint32_t ars_sa[16]; /* 0x1340 */ 191 uint64_t crs_sa[16]; /* 0x1380 */ 192 uint8_t pad_0x1400[0x1800 - 0x1400]; /* 0x1400 */ 193 uint8_t pgm_int_tdb[0x1900 - 0x1800]; /* 0x1800 */ 194 } __attribute__ ((__packed__)); 195 _Static_assert(sizeof(struct lowcore) == 0x1900, "Lowcore size"); 196 197 extern struct lowcore lowcore; 198 199 #define THIS_CPU (lowcore.this_cpu) 200 201 #define PGM_INT_CODE_OPERATION 0x01 202 #define PGM_INT_CODE_PRIVILEGED_OPERATION 0x02 203 #define PGM_INT_CODE_EXECUTE 0x03 204 #define PGM_INT_CODE_PROTECTION 0x04 205 #define PGM_INT_CODE_ADDRESSING 0x05 206 #define PGM_INT_CODE_SPECIFICATION 0x06 207 #define PGM_INT_CODE_DATA 0x07 208 #define PGM_INT_CODE_FIXED_POINT_OVERFLOW 0x08 209 #define PGM_INT_CODE_FIXED_POINT_DIVIDE 0x09 210 #define PGM_INT_CODE_DECIMAL_OVERFLOW 0x0a 211 #define PGM_INT_CODE_DECIMAL_DIVIDE 0x0b 212 #define PGM_INT_CODE_HFP_EXPONENT_OVERFLOW 0x0c 213 #define PGM_INT_CODE_HFP_EXPONENT_UNDERFLOW 0x0d 214 #define PGM_INT_CODE_HFP_SIGNIFICANCE 0x0e 215 #define PGM_INT_CODE_HFP_DIVIDE 0x0f 216 #define PGM_INT_CODE_SEGMENT_TRANSLATION 0x10 217 #define PGM_INT_CODE_PAGE_TRANSLATION 0x11 218 #define PGM_INT_CODE_TRANSLATION_SPEC 0x12 219 #define PGM_INT_CODE_SPECIAL_OPERATION 0x13 220 #define PGM_INT_CODE_OPERAND 0x15 221 #define PGM_INT_CODE_TRACE_TABLE 0x16 222 #define PGM_INT_CODE_VECTOR_PROCESSING 0x1b 223 #define PGM_INT_CODE_SPACE_SWITCH_EVENT 0x1c 224 #define PGM_INT_CODE_HFP_SQUARE_ROOT 0x1d 225 #define PGM_INT_CODE_PC_TRANSLATION_SPEC 0x1f 226 #define PGM_INT_CODE_AFX_TRANSLATION 0x20 227 #define PGM_INT_CODE_ASX_TRANSLATION 0x21 228 #define PGM_INT_CODE_LX_TRANSLATION 0x22 229 #define PGM_INT_CODE_EX_TRANSLATION 0x23 230 #define PGM_INT_CODE_PRIMARY_AUTHORITY 0x24 231 #define PGM_INT_CODE_SECONDARY_AUTHORITY 0x25 232 #define PGM_INT_CODE_LFX_TRANSLATION 0x26 233 #define PGM_INT_CODE_LSX_TRANSLATION 0x27 234 #define PGM_INT_CODE_ALET_SPECIFICATION 0x28 235 #define PGM_INT_CODE_ALEN_TRANSLATION 0x29 236 #define PGM_INT_CODE_ALE_SEQUENCE 0x2a 237 #define PGM_INT_CODE_ASTE_VALIDITY 0x2b 238 #define PGM_INT_CODE_ASTE_SEQUENCE 0x2c 239 #define PGM_INT_CODE_EXTENDED_AUTHORITY 0x2d 240 #define PGM_INT_CODE_LSTE_SEQUENCE 0x2e 241 #define PGM_INT_CODE_ASTE_INSTANCE 0x2f 242 #define PGM_INT_CODE_STACK_FULL 0x30 243 #define PGM_INT_CODE_STACK_EMPTY 0x31 244 #define PGM_INT_CODE_STACK_SPECIFICATION 0x32 245 #define PGM_INT_CODE_STACK_TYPE 0x33 246 #define PGM_INT_CODE_STACK_OPERATION 0x34 247 #define PGM_INT_CODE_ASCE_TYPE 0x38 248 #define PGM_INT_CODE_REGION_FIRST_TRANS 0x39 249 #define PGM_INT_CODE_REGION_SECOND_TRANS 0x3a 250 #define PGM_INT_CODE_REGION_THIRD_TRANS 0x3b 251 #define PGM_INT_CODE_SECURE_STOR_ACCESS 0x3d 252 #define PGM_INT_CODE_NON_SECURE_STOR_ACCESS 0x3e 253 #define PGM_INT_CODE_SECURE_STOR_VIOLATION 0x3f 254 #define PGM_INT_CODE_MONITOR_EVENT 0x40 255 #define PGM_INT_CODE_PER 0x80 256 #define PGM_INT_CODE_CRYPTO_OPERATION 0x119 257 #define PGM_INT_CODE_TX_ABORTED_EVENT 0x200 258 259 struct cpuid { 260 uint64_t version : 8; 261 uint64_t id : 24; 262 uint64_t type : 16; 263 uint64_t format : 1; 264 uint64_t reserved : 15; 265 }; 266 267 #define SVC_LEAVE_PSTATE 1 268 269 static inline unsigned short stap(void) 270 { 271 unsigned short cpu_address; 272 273 asm volatile("stap %0" : "=Q" (cpu_address)); 274 return cpu_address; 275 } 276 277 static inline uint64_t stidp(void) 278 { 279 uint64_t cpuid; 280 281 asm volatile("stidp %0" : "=Q" (cpuid)); 282 283 return cpuid; 284 } 285 286 enum tprot_permission { 287 TPROT_READ_WRITE = 0, 288 TPROT_READ = 1, 289 TPROT_RW_PROTECTED = 2, 290 TPROT_TRANSL_UNAVAIL = 3, 291 }; 292 293 static inline enum tprot_permission tprot(unsigned long addr, char access_key) 294 { 295 int cc; 296 297 asm volatile( 298 " tprot 0(%1),0(%2)\n" 299 " ipm %0\n" 300 " srl %0,28\n" 301 : "=d" (cc) : "a" (addr), "a" (access_key << 4) : "cc"); 302 return (enum tprot_permission)cc; 303 } 304 305 static inline void lctlg(int cr, uint64_t value) 306 { 307 asm volatile( 308 " lctlg %1,%1,%0\n" 309 : : "Q" (value), "i" (cr)); 310 } 311 312 static inline uint64_t stctg(int cr) 313 { 314 uint64_t value; 315 316 asm volatile( 317 " stctg %1,%1,%0\n" 318 : "=Q" (value) : "i" (cr) : "memory"); 319 return value; 320 } 321 322 static inline void ctl_set_bit(int cr, unsigned int bit) 323 { 324 uint64_t reg; 325 326 reg = stctg(cr); 327 reg |= 1UL << bit; 328 lctlg(cr, reg); 329 } 330 331 static inline void ctl_clear_bit(int cr, unsigned int bit) 332 { 333 uint64_t reg; 334 335 reg = stctg(cr); 336 reg &= ~(1UL << bit); 337 lctlg(cr, reg); 338 } 339 340 static inline uint64_t extract_psw_mask(void) 341 { 342 uint32_t mask_upper = 0, mask_lower = 0; 343 344 asm volatile( 345 " epsw %0,%1\n" 346 : "=r" (mask_upper), "=a" (mask_lower)); 347 348 return (uint64_t) mask_upper << 32 | mask_lower; 349 } 350 351 #define PSW_WITH_CUR_MASK(addr) PSW(extract_psw_mask(), (addr)) 352 353 static inline void load_psw_mask(uint64_t mask) 354 { 355 struct psw psw = { 356 .mask = mask, 357 .addr = 0, 358 }; 359 uint64_t tmp = 0; 360 361 asm volatile( 362 " larl %0,0f\n" 363 " stg %0,8(%1)\n" 364 " lpswe 0(%1)\n" 365 "0:\n" 366 : "+r" (tmp) : "a" (&psw) : "memory", "cc" ); 367 } 368 369 static inline void disabled_wait(uint64_t message) 370 { 371 struct psw psw = { 372 .mask = PSW_MASK_WAIT, /* Disabled wait */ 373 .addr = message, 374 }; 375 376 asm volatile(" lpswe 0(%0)\n" : : "a" (&psw) : "memory", "cc"); 377 } 378 379 /** 380 * psw_mask_clear_bits - clears bits from the current PSW mask 381 * @clear: bitmask of bits that will be cleared 382 */ 383 static inline void psw_mask_clear_bits(uint64_t clear) 384 { 385 load_psw_mask(extract_psw_mask() & ~clear); 386 } 387 388 /** 389 * psw_mask_set_bits - sets bits on the current PSW mask 390 * @set: bitmask of bits that will be set 391 */ 392 static inline void psw_mask_set_bits(uint64_t set) 393 { 394 load_psw_mask(extract_psw_mask() | set); 395 } 396 397 /** 398 * psw_mask_clear_and_set_bits - clears and sets bits on the current PSW mask 399 * @clear: bitmask of bits that will be cleared 400 * @set: bitmask of bits that will be set 401 * 402 * The bits in the @clear mask will be cleared, then the bits in the @set mask 403 * will be set. 404 */ 405 static inline void psw_mask_clear_and_set_bits(uint64_t clear, uint64_t set) 406 { 407 load_psw_mask((extract_psw_mask() & ~clear) | set); 408 } 409 410 /** 411 * enable_dat - enable the DAT bit in the current PSW 412 */ 413 static inline void enable_dat(void) 414 { 415 psw_mask_set_bits(PSW_MASK_DAT); 416 } 417 418 /** 419 * disable_dat - disable the DAT bit in the current PSW 420 */ 421 static inline void disable_dat(void) 422 { 423 psw_mask_clear_bits(PSW_MASK_DAT); 424 } 425 426 static inline void wait_for_interrupt(uint64_t irq_mask) 427 { 428 uint64_t psw_mask = extract_psw_mask(); 429 430 load_psw_mask(psw_mask | irq_mask | PSW_MASK_WAIT); 431 /* 432 * After being woken and having processed the interrupt, let's restore 433 * the PSW mask. 434 */ 435 load_psw_mask(psw_mask); 436 } 437 438 static inline void enter_pstate(void) 439 { 440 psw_mask_set_bits(PSW_MASK_PSTATE); 441 } 442 443 static inline void leave_pstate(void) 444 { 445 asm volatile(" svc %0\n" : : "i" (SVC_LEAVE_PSTATE)); 446 } 447 448 static inline int stsi(void *addr, int fc, int sel1, int sel2) 449 { 450 register int r0 asm("0") = (fc << 28) | sel1; 451 register int r1 asm("1") = sel2; 452 int cc; 453 454 asm volatile( 455 "stsi 0(%3)\n" 456 "ipm %[cc]\n" 457 "srl %[cc],28\n" 458 : "+d" (r0), [cc] "=d" (cc) 459 : "d" (r1), "a" (addr) 460 : "cc", "memory"); 461 return cc; 462 } 463 464 static inline unsigned long stsi_get_fc(void) 465 { 466 register unsigned long r0 asm("0") = 0; 467 register unsigned long r1 asm("1") = 0; 468 int cc; 469 470 asm volatile("stsi 0\n" 471 "ipm %[cc]\n" 472 "srl %[cc],28\n" 473 : "+d" (r0), [cc] "=d" (cc) 474 : "d" (r1) 475 : "cc", "memory"); 476 assert(!cc); 477 return r0 >> 28; 478 } 479 480 static inline int servc(uint32_t command, unsigned long sccb) 481 { 482 int cc; 483 484 asm volatile( 485 " .insn rre,0xb2200000,%1,%2\n" /* servc %1,%2 */ 486 " ipm %0\n" 487 " srl %0,28" 488 : "=&d" (cc) : "d" (command), "a" (sccb) 489 : "cc", "memory"); 490 return cc; 491 } 492 493 static inline void set_prefix(uint32_t new_prefix) 494 { 495 asm volatile(" spx %0" : : "Q" (new_prefix) : "memory"); 496 } 497 498 static inline uint32_t get_prefix(void) 499 { 500 uint32_t current_prefix; 501 502 asm volatile(" stpx %0" : "=Q" (current_prefix)); 503 return current_prefix; 504 } 505 506 #endif 507