cpu.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  1. /*
  2. * S/390 virtual CPU header
  3. *
  4. * For details on the s390x architecture and used definitions (e.g.,
  5. * PSW, PER and DAT (Dynamic Address Translation)), please refer to
  6. * the "z/Architecture Principles of Operations" - a.k.a. PoP.
  7. *
  8. * Copyright (c) 2009 Ulrich Hecht
  9. * Copyright IBM Corp. 2012, 2018
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  23. */
  24. #ifndef S390X_CPU_H
  25. #define S390X_CPU_H
  26. #include "cpu-qom.h"
  27. #include "cpu_models.h"
  28. #include "exec/cpu-defs.h"
  29. #include "qemu/cpu-float.h"
  30. #include "qapi/qapi-types-machine-common.h"
  31. #define ELF_MACHINE_UNAME "S390X"
  32. #define TARGET_HAS_PRECISE_SMC
  33. #define TARGET_INSN_START_EXTRA_WORDS 2
  34. #define MMU_USER_IDX 0
  35. #define S390_MAX_CPUS 248
  36. #ifndef CONFIG_KVM
  37. #define S390_ADAPTER_SUPPRESSIBLE 0x01
  38. #else
  39. #define S390_ADAPTER_SUPPRESSIBLE KVM_S390_ADAPTER_SUPPRESSIBLE
  40. #endif
  41. typedef struct PSW {
  42. uint64_t mask;
  43. uint64_t addr;
  44. } PSW;
  45. typedef struct CPUArchState {
  46. uint64_t regs[16]; /* GP registers */
  47. /*
  48. * The floating point registers are part of the vector registers.
  49. * vregs[0][0] -> vregs[15][0] are 16 floating point registers
  50. */
  51. uint64_t vregs[32][2] QEMU_ALIGNED(16); /* vector registers */
  52. uint32_t aregs[16]; /* access registers */
  53. uint64_t gscb[4]; /* guarded storage control */
  54. uint64_t etoken; /* etoken */
  55. uint64_t etoken_extension; /* etoken extension */
  56. uint64_t diag318_info;
  57. /* Fields up to this point are not cleared by initial CPU reset */
  58. struct {} start_initial_reset_fields;
  59. uint32_t fpc; /* floating-point control register */
  60. uint32_t cc_op;
  61. bool bpbc; /* branch prediction blocking */
  62. float_status fpu_status; /* passed to softfloat lib */
  63. PSW psw;
  64. S390CrashReason crash_reason;
  65. uint64_t cc_src;
  66. uint64_t cc_dst;
  67. uint64_t cc_vr;
  68. uint64_t ex_value;
  69. uint64_t ex_target;
  70. uint64_t __excp_addr;
  71. uint64_t psa;
  72. uint32_t int_pgm_code;
  73. uint32_t int_pgm_ilen;
  74. uint32_t int_svc_code;
  75. uint32_t int_svc_ilen;
  76. uint64_t per_address;
  77. uint16_t per_perc_atmid;
  78. uint64_t cregs[16]; /* control registers */
  79. uint64_t ckc;
  80. uint64_t cputm;
  81. uint32_t todpr;
  82. uint64_t pfault_token;
  83. uint64_t pfault_compare;
  84. uint64_t pfault_select;
  85. uint64_t gbea;
  86. uint64_t pp;
  87. /* Fields up to this point are not cleared by normal CPU reset */
  88. struct {} start_normal_reset_fields;
  89. uint8_t riccb[64]; /* runtime instrumentation control */
  90. int pending_int;
  91. uint16_t external_call_addr;
  92. DECLARE_BITMAP(emergency_signals, S390_MAX_CPUS);
  93. #if !defined(CONFIG_USER_ONLY)
  94. uint64_t tlb_fill_tec; /* translation exception code during tlb_fill */
  95. int tlb_fill_exc; /* exception number seen during tlb_fill */
  96. #endif
  97. /* Fields up to this point are cleared by a CPU reset */
  98. struct {} end_reset_fields;
  99. #if !defined(CONFIG_USER_ONLY)
  100. uint32_t core_id; /* PoP "CPU address", same as cpu_index */
  101. int32_t socket_id;
  102. int32_t book_id;
  103. int32_t drawer_id;
  104. bool dedicated;
  105. S390CpuEntitlement entitlement; /* Used only for vertical polarization */
  106. uint64_t cpuid;
  107. #endif
  108. QEMUTimer *tod_timer;
  109. QEMUTimer *cpu_timer;
  110. /*
  111. * The cpu state represents the logical state of a cpu. In contrast to other
  112. * architectures, there is a difference between a halt and a stop on s390.
  113. * If all cpus are either stopped (including check stop) or in the disabled
  114. * wait state, the vm can be shut down.
  115. * The acceptable cpu_state values are defined in the CpuInfoS390State
  116. * enum.
  117. */
  118. uint8_t cpu_state;
  119. /* currently processed sigp order */
  120. uint8_t sigp_order;
  121. } CPUS390XState;
  122. static inline uint64_t *get_freg(CPUS390XState *cs, int nr)
  123. {
  124. return &cs->vregs[nr][0];
  125. }
  126. /**
  127. * S390CPU:
  128. * @env: #CPUS390XState.
  129. *
  130. * An S/390 CPU.
  131. */
  132. struct ArchCPU {
  133. CPUState parent_obj;
  134. CPUS390XState env;
  135. S390CPUModel *model;
  136. /* needed for live migration */
  137. void *irqstate;
  138. uint32_t irqstate_saved_size;
  139. };
  140. typedef enum cpu_reset_type {
  141. S390_CPU_RESET_NORMAL,
  142. S390_CPU_RESET_INITIAL,
  143. S390_CPU_RESET_CLEAR,
  144. } cpu_reset_type;
  145. /**
  146. * S390CPUClass:
  147. * @parent_realize: The parent class' realize handler.
  148. * @parent_reset: The parent class' reset handler.
  149. * @load_normal: Performs a load normal.
  150. * @cpu_reset: Performs a CPU reset.
  151. * @initial_cpu_reset: Performs an initial CPU reset.
  152. *
  153. * An S/390 CPU model.
  154. */
  155. struct S390CPUClass {
  156. CPUClass parent_class;
  157. const S390CPUDef *cpu_def;
  158. bool kvm_required;
  159. bool is_static;
  160. bool is_migration_safe;
  161. const char *desc;
  162. DeviceRealize parent_realize;
  163. DeviceReset parent_reset;
  164. void (*load_normal)(CPUState *cpu);
  165. void (*reset)(CPUState *cpu, cpu_reset_type type);
  166. };
  167. #ifndef CONFIG_USER_ONLY
  168. extern const VMStateDescription vmstate_s390_cpu;
  169. #endif
  170. /* distinguish between 24 bit and 31 bit addressing */
  171. #define HIGH_ORDER_BIT 0x80000000
  172. /* Interrupt Codes */
  173. /* Program Interrupts */
  174. #define PGM_OPERATION 0x0001
  175. #define PGM_PRIVILEGED 0x0002
  176. #define PGM_EXECUTE 0x0003
  177. #define PGM_PROTECTION 0x0004
  178. #define PGM_ADDRESSING 0x0005
  179. #define PGM_SPECIFICATION 0x0006
  180. #define PGM_DATA 0x0007
  181. #define PGM_FIXPT_OVERFLOW 0x0008
  182. #define PGM_FIXPT_DIVIDE 0x0009
  183. #define PGM_DEC_OVERFLOW 0x000a
  184. #define PGM_DEC_DIVIDE 0x000b
  185. #define PGM_HFP_EXP_OVERFLOW 0x000c
  186. #define PGM_HFP_EXP_UNDERFLOW 0x000d
  187. #define PGM_HFP_SIGNIFICANCE 0x000e
  188. #define PGM_HFP_DIVIDE 0x000f
  189. #define PGM_SEGMENT_TRANS 0x0010
  190. #define PGM_PAGE_TRANS 0x0011
  191. #define PGM_TRANS_SPEC 0x0012
  192. #define PGM_SPECIAL_OP 0x0013
  193. #define PGM_OPERAND 0x0015
  194. #define PGM_TRACE_TABLE 0x0016
  195. #define PGM_VECTOR_PROCESSING 0x001b
  196. #define PGM_SPACE_SWITCH 0x001c
  197. #define PGM_HFP_SQRT 0x001d
  198. #define PGM_PC_TRANS_SPEC 0x001f
  199. #define PGM_AFX_TRANS 0x0020
  200. #define PGM_ASX_TRANS 0x0021
  201. #define PGM_LX_TRANS 0x0022
  202. #define PGM_EX_TRANS 0x0023
  203. #define PGM_PRIM_AUTH 0x0024
  204. #define PGM_SEC_AUTH 0x0025
  205. #define PGM_ALET_SPEC 0x0028
  206. #define PGM_ALEN_SPEC 0x0029
  207. #define PGM_ALE_SEQ 0x002a
  208. #define PGM_ASTE_VALID 0x002b
  209. #define PGM_ASTE_SEQ 0x002c
  210. #define PGM_EXT_AUTH 0x002d
  211. #define PGM_STACK_FULL 0x0030
  212. #define PGM_STACK_EMPTY 0x0031
  213. #define PGM_STACK_SPEC 0x0032
  214. #define PGM_STACK_TYPE 0x0033
  215. #define PGM_STACK_OP 0x0034
  216. #define PGM_ASCE_TYPE 0x0038
  217. #define PGM_REG_FIRST_TRANS 0x0039
  218. #define PGM_REG_SEC_TRANS 0x003a
  219. #define PGM_REG_THIRD_TRANS 0x003b
  220. #define PGM_MONITOR 0x0040
  221. #define PGM_PER 0x0080
  222. #define PGM_CRYPTO 0x0119
  223. /* External Interrupts */
  224. #define EXT_INTERRUPT_KEY 0x0040
  225. #define EXT_CLOCK_COMP 0x1004
  226. #define EXT_CPU_TIMER 0x1005
  227. #define EXT_MALFUNCTION 0x1200
  228. #define EXT_EMERGENCY 0x1201
  229. #define EXT_EXTERNAL_CALL 0x1202
  230. #define EXT_ETR 0x1406
  231. #define EXT_SERVICE 0x2401
  232. #define EXT_VIRTIO 0x2603
  233. /* PSW defines */
  234. #undef PSW_MASK_PER
  235. #undef PSW_MASK_UNUSED_2
  236. #undef PSW_MASK_UNUSED_3
  237. #undef PSW_MASK_DAT
  238. #undef PSW_MASK_IO
  239. #undef PSW_MASK_EXT
  240. #undef PSW_MASK_KEY
  241. #undef PSW_SHIFT_KEY
  242. #undef PSW_MASK_MCHECK
  243. #undef PSW_MASK_WAIT
  244. #undef PSW_MASK_PSTATE
  245. #undef PSW_MASK_ASC
  246. #undef PSW_SHIFT_ASC
  247. #undef PSW_MASK_CC
  248. #undef PSW_MASK_PM
  249. #undef PSW_MASK_RI
  250. #undef PSW_SHIFT_MASK_PM
  251. #undef PSW_MASK_64
  252. #undef PSW_MASK_32
  253. #undef PSW_MASK_ESA_ADDR
  254. #define PSW_MASK_PER 0x4000000000000000ULL
  255. #define PSW_MASK_UNUSED_2 0x2000000000000000ULL
  256. #define PSW_MASK_UNUSED_3 0x1000000000000000ULL
  257. #define PSW_MASK_DAT 0x0400000000000000ULL
  258. #define PSW_MASK_IO 0x0200000000000000ULL
  259. #define PSW_MASK_EXT 0x0100000000000000ULL
  260. #define PSW_MASK_KEY 0x00F0000000000000ULL
  261. #define PSW_SHIFT_KEY 52
  262. #define PSW_MASK_SHORTPSW 0x0008000000000000ULL
  263. #define PSW_MASK_MCHECK 0x0004000000000000ULL
  264. #define PSW_MASK_WAIT 0x0002000000000000ULL
  265. #define PSW_MASK_PSTATE 0x0001000000000000ULL
  266. #define PSW_MASK_ASC 0x0000C00000000000ULL
  267. #define PSW_SHIFT_ASC 46
  268. #define PSW_MASK_CC 0x0000300000000000ULL
  269. #define PSW_MASK_PM 0x00000F0000000000ULL
  270. #define PSW_SHIFT_MASK_PM 40
  271. #define PSW_MASK_RI 0x0000008000000000ULL
  272. #define PSW_MASK_64 0x0000000100000000ULL
  273. #define PSW_MASK_32 0x0000000080000000ULL
  274. #define PSW_MASK_SHORT_ADDR 0x000000007fffffffULL
  275. #define PSW_MASK_SHORT_CTRL 0xffffffff80000000ULL
  276. #define PSW_MASK_RESERVED 0xb80800fe7fffffffULL
  277. #undef PSW_ASC_PRIMARY
  278. #undef PSW_ASC_ACCREG
  279. #undef PSW_ASC_SECONDARY
  280. #undef PSW_ASC_HOME
  281. #define PSW_ASC_PRIMARY 0x0000000000000000ULL
  282. #define PSW_ASC_ACCREG 0x0000400000000000ULL
  283. #define PSW_ASC_SECONDARY 0x0000800000000000ULL
  284. #define PSW_ASC_HOME 0x0000C00000000000ULL
  285. /* the address space values shifted */
  286. #define AS_PRIMARY 0
  287. #define AS_ACCREG 1
  288. #define AS_SECONDARY 2
  289. #define AS_HOME 3
  290. /* tb flags */
  291. #define FLAG_MASK_PSW_SHIFT 31
  292. #define FLAG_MASK_32 0x00000001u
  293. #define FLAG_MASK_64 0x00000002u
  294. #define FLAG_MASK_AFP 0x00000004u
  295. #define FLAG_MASK_VECTOR 0x00000008u
  296. #define FLAG_MASK_ASC 0x00018000u
  297. #define FLAG_MASK_PSTATE 0x00020000u
  298. #define FLAG_MASK_PER_IFETCH_NULLIFY 0x01000000u
  299. #define FLAG_MASK_DAT 0x08000000u
  300. #define FLAG_MASK_PER_STORE_REAL 0x20000000u
  301. #define FLAG_MASK_PER_IFETCH 0x40000000u
  302. #define FLAG_MASK_PER_BRANCH 0x80000000u
  303. QEMU_BUILD_BUG_ON(FLAG_MASK_32 != PSW_MASK_32 >> FLAG_MASK_PSW_SHIFT);
  304. QEMU_BUILD_BUG_ON(FLAG_MASK_64 != PSW_MASK_64 >> FLAG_MASK_PSW_SHIFT);
  305. QEMU_BUILD_BUG_ON(FLAG_MASK_ASC != PSW_MASK_ASC >> FLAG_MASK_PSW_SHIFT);
  306. QEMU_BUILD_BUG_ON(FLAG_MASK_PSTATE != PSW_MASK_PSTATE >> FLAG_MASK_PSW_SHIFT);
  307. QEMU_BUILD_BUG_ON(FLAG_MASK_DAT != PSW_MASK_DAT >> FLAG_MASK_PSW_SHIFT);
  308. #define FLAG_MASK_PSW (FLAG_MASK_DAT | FLAG_MASK_PSTATE | \
  309. FLAG_MASK_ASC | FLAG_MASK_64 | FLAG_MASK_32)
  310. #define FLAG_MASK_CR9 (FLAG_MASK_PER_BRANCH | FLAG_MASK_PER_IFETCH)
  311. #define FLAG_MASK_PER (FLAG_MASK_PER_BRANCH | \
  312. FLAG_MASK_PER_IFETCH | \
  313. FLAG_MASK_PER_IFETCH_NULLIFY | \
  314. FLAG_MASK_PER_STORE_REAL)
  315. /* Control register 0 bits */
  316. #define CR0_LOWPROT 0x0000000010000000ULL
  317. #define CR0_SECONDARY 0x0000000004000000ULL
  318. #define CR0_EDAT 0x0000000000800000ULL
  319. #define CR0_AFP 0x0000000000040000ULL
  320. #define CR0_VECTOR 0x0000000000020000ULL
  321. #define CR0_IEP 0x0000000000100000ULL
  322. #define CR0_EMERGENCY_SIGNAL_SC 0x0000000000004000ULL
  323. #define CR0_EXTERNAL_CALL_SC 0x0000000000002000ULL
  324. #define CR0_CKC_SC 0x0000000000000800ULL
  325. #define CR0_CPU_TIMER_SC 0x0000000000000400ULL
  326. #define CR0_SERVICE_SC 0x0000000000000200ULL
  327. /* Control register 14 bits */
  328. #define CR14_CHANNEL_REPORT_SC 0x0000000010000000ULL
  329. /* MMU */
  330. #define MMU_PRIMARY_IDX 0
  331. #define MMU_SECONDARY_IDX 1
  332. #define MMU_HOME_IDX 2
  333. #define MMU_REAL_IDX 3
  334. static inline int s390x_env_mmu_index(CPUS390XState *env, bool ifetch)
  335. {
  336. #ifdef CONFIG_USER_ONLY
  337. return MMU_USER_IDX;
  338. #else
  339. if (!(env->psw.mask & PSW_MASK_DAT)) {
  340. return MMU_REAL_IDX;
  341. }
  342. if (ifetch) {
  343. if ((env->psw.mask & PSW_MASK_ASC) == PSW_ASC_HOME) {
  344. return MMU_HOME_IDX;
  345. }
  346. return MMU_PRIMARY_IDX;
  347. }
  348. switch (env->psw.mask & PSW_MASK_ASC) {
  349. case PSW_ASC_PRIMARY:
  350. return MMU_PRIMARY_IDX;
  351. case PSW_ASC_SECONDARY:
  352. return MMU_SECONDARY_IDX;
  353. case PSW_ASC_HOME:
  354. return MMU_HOME_IDX;
  355. case PSW_ASC_ACCREG:
  356. /* Fallthrough: access register mode is not yet supported */
  357. default:
  358. abort();
  359. }
  360. #endif
  361. }
  362. #ifdef CONFIG_TCG
  363. #include "tcg/tcg_s390x.h"
  364. void cpu_get_tb_cpu_state(CPUS390XState *env, vaddr *pc,
  365. uint64_t *cs_base, uint32_t *flags);
  366. #endif /* CONFIG_TCG */
  367. /* PER bits from control register 9 */
  368. #define PER_CR9_EVENT_BRANCH 0x80000000
  369. #define PER_CR9_EVENT_IFETCH 0x40000000
  370. #define PER_CR9_EVENT_STORE 0x20000000
  371. #define PER_CR9_EVENT_STORAGE_KEY_ALTERATION 0x10000000
  372. #define PER_CR9_EVENT_STORE_REAL 0x08000000
  373. #define PER_CR9_EVENT_ZERO_ADDRESS_DETECTION 0x04000000
  374. #define PER_CR9_EVENT_TRANSACTION_END 0x02000000
  375. #define PER_CR9_EVENT_IFETCH_NULLIFICATION 0x01000000
  376. #define PER_CR9_CONTROL_BRANCH_ADDRESS 0x00800000
  377. #define PER_CR9_CONTROL_TRANSACTION_SUPRESS 0x00400000
  378. #define PER_CR9_CONTROL_STORAGE_ALTERATION 0x00200000
  379. QEMU_BUILD_BUG_ON(FLAG_MASK_PER_BRANCH != PER_CR9_EVENT_BRANCH);
  380. QEMU_BUILD_BUG_ON(FLAG_MASK_PER_IFETCH != PER_CR9_EVENT_IFETCH);
  381. QEMU_BUILD_BUG_ON(FLAG_MASK_PER_IFETCH_NULLIFY !=
  382. PER_CR9_EVENT_IFETCH_NULLIFICATION);
  383. /* PER bits from the PER CODE/ATMID/AI in lowcore */
  384. #define PER_CODE_EVENT_BRANCH 0x8000
  385. #define PER_CODE_EVENT_IFETCH 0x4000
  386. #define PER_CODE_EVENT_STORE 0x2000
  387. #define PER_CODE_EVENT_STORE_REAL 0x0800
  388. #define PER_CODE_EVENT_NULLIFICATION 0x0100
  389. #define EXCP_EXT 1 /* external interrupt */
  390. #define EXCP_SVC 2 /* supervisor call (syscall) */
  391. #define EXCP_PGM 3 /* program interruption */
  392. #define EXCP_RESTART 4 /* restart interrupt */
  393. #define EXCP_STOP 5 /* stop interrupt */
  394. #define EXCP_IO 7 /* I/O interrupt */
  395. #define EXCP_MCHK 8 /* machine check */
  396. #define INTERRUPT_EXT_CPU_TIMER (1 << 3)
  397. #define INTERRUPT_EXT_CLOCK_COMPARATOR (1 << 4)
  398. #define INTERRUPT_EXTERNAL_CALL (1 << 5)
  399. #define INTERRUPT_EMERGENCY_SIGNAL (1 << 6)
  400. #define INTERRUPT_RESTART (1 << 7)
  401. #define INTERRUPT_STOP (1 << 8)
  402. /* Program Status Word. */
  403. #define S390_PSWM_REGNUM 0
  404. #define S390_PSWA_REGNUM 1
  405. /* General Purpose Registers. */
  406. #define S390_R0_REGNUM 2
  407. #define S390_R1_REGNUM 3
  408. #define S390_R2_REGNUM 4
  409. #define S390_R3_REGNUM 5
  410. #define S390_R4_REGNUM 6
  411. #define S390_R5_REGNUM 7
  412. #define S390_R6_REGNUM 8
  413. #define S390_R7_REGNUM 9
  414. #define S390_R8_REGNUM 10
  415. #define S390_R9_REGNUM 11
  416. #define S390_R10_REGNUM 12
  417. #define S390_R11_REGNUM 13
  418. #define S390_R12_REGNUM 14
  419. #define S390_R13_REGNUM 15
  420. #define S390_R14_REGNUM 16
  421. #define S390_R15_REGNUM 17
  422. static inline void setcc(S390CPU *cpu, uint64_t cc)
  423. {
  424. CPUS390XState *env = &cpu->env;
  425. env->psw.mask &= ~(3ull << 44);
  426. env->psw.mask |= (cc & 3) << 44;
  427. env->cc_op = cc;
  428. }
  429. /* STSI */
  430. #define STSI_R0_FC_MASK 0x00000000f0000000ULL
  431. #define STSI_R0_FC_CURRENT 0x0000000000000000ULL
  432. #define STSI_R0_FC_LEVEL_1 0x0000000010000000ULL
  433. #define STSI_R0_FC_LEVEL_2 0x0000000020000000ULL
  434. #define STSI_R0_FC_LEVEL_3 0x0000000030000000ULL
  435. #define STSI_R0_RESERVED_MASK 0x000000000fffff00ULL
  436. #define STSI_R0_SEL1_MASK 0x00000000000000ffULL
  437. #define STSI_R1_RESERVED_MASK 0x00000000ffff0000ULL
  438. #define STSI_R1_SEL2_MASK 0x000000000000ffffULL
  439. /* Basic Machine Configuration */
  440. typedef struct SysIB_111 {
  441. uint8_t res1[32];
  442. uint8_t manuf[16];
  443. uint8_t type[4];
  444. uint8_t res2[12];
  445. uint8_t model[16];
  446. uint8_t sequence[16];
  447. uint8_t plant[4];
  448. uint8_t res3[3996];
  449. } SysIB_111;
  450. QEMU_BUILD_BUG_ON(sizeof(SysIB_111) != 4096);
  451. /* Basic Machine CPU */
  452. typedef struct SysIB_121 {
  453. uint8_t res1[80];
  454. uint8_t sequence[16];
  455. uint8_t plant[4];
  456. uint8_t res2[2];
  457. uint16_t cpu_addr;
  458. uint8_t res3[3992];
  459. } SysIB_121;
  460. QEMU_BUILD_BUG_ON(sizeof(SysIB_121) != 4096);
  461. /* Basic Machine CPUs */
  462. typedef struct SysIB_122 {
  463. uint8_t res1[32];
  464. uint32_t capability;
  465. uint16_t total_cpus;
  466. uint16_t conf_cpus;
  467. uint16_t standby_cpus;
  468. uint16_t reserved_cpus;
  469. uint16_t adjustments[2026];
  470. } SysIB_122;
  471. QEMU_BUILD_BUG_ON(sizeof(SysIB_122) != 4096);
  472. /* LPAR CPU */
  473. typedef struct SysIB_221 {
  474. uint8_t res1[80];
  475. uint8_t sequence[16];
  476. uint8_t plant[4];
  477. uint16_t cpu_id;
  478. uint16_t cpu_addr;
  479. uint8_t res3[3992];
  480. } SysIB_221;
  481. QEMU_BUILD_BUG_ON(sizeof(SysIB_221) != 4096);
  482. /* LPAR CPUs */
  483. typedef struct SysIB_222 {
  484. uint8_t res1[32];
  485. uint16_t lpar_num;
  486. uint8_t res2;
  487. uint8_t lcpuc;
  488. uint16_t total_cpus;
  489. uint16_t conf_cpus;
  490. uint16_t standby_cpus;
  491. uint16_t reserved_cpus;
  492. uint8_t name[8];
  493. uint32_t caf;
  494. uint8_t res3[16];
  495. uint16_t dedicated_cpus;
  496. uint16_t shared_cpus;
  497. uint8_t res4[4020];
  498. } SysIB_222;
  499. QEMU_BUILD_BUG_ON(sizeof(SysIB_222) != 4096);
  500. /* VM CPUs */
  501. typedef struct SysIB_322 {
  502. uint8_t res1[31];
  503. uint8_t count;
  504. struct {
  505. uint8_t res2[4];
  506. uint16_t total_cpus;
  507. uint16_t conf_cpus;
  508. uint16_t standby_cpus;
  509. uint16_t reserved_cpus;
  510. uint8_t name[8];
  511. uint32_t caf;
  512. uint8_t cpi[16];
  513. uint8_t res5[3];
  514. uint8_t ext_name_encoding;
  515. uint32_t res3;
  516. uint8_t uuid[16];
  517. } vm[8];
  518. uint8_t res4[1504];
  519. uint8_t ext_names[8][256];
  520. } SysIB_322;
  521. QEMU_BUILD_BUG_ON(sizeof(SysIB_322) != 4096);
  522. /*
  523. * Topology Magnitude fields (MAG) indicates the maximum number of
  524. * topology list entries (TLE) at the corresponding nesting level.
  525. */
  526. #define S390_TOPOLOGY_MAG 6
  527. #define S390_TOPOLOGY_MAG6 0
  528. #define S390_TOPOLOGY_MAG5 1
  529. #define S390_TOPOLOGY_MAG4 2
  530. #define S390_TOPOLOGY_MAG3 3
  531. #define S390_TOPOLOGY_MAG2 4
  532. #define S390_TOPOLOGY_MAG1 5
  533. /* Configuration topology */
  534. typedef struct SysIB_151x {
  535. uint8_t reserved0[2];
  536. uint16_t length;
  537. uint8_t mag[S390_TOPOLOGY_MAG];
  538. uint8_t reserved1;
  539. uint8_t mnest;
  540. uint32_t reserved2;
  541. char tle[];
  542. } SysIB_151x;
  543. QEMU_BUILD_BUG_ON(sizeof(SysIB_151x) != 16);
  544. typedef union SysIB {
  545. SysIB_111 sysib_111;
  546. SysIB_121 sysib_121;
  547. SysIB_122 sysib_122;
  548. SysIB_221 sysib_221;
  549. SysIB_222 sysib_222;
  550. SysIB_322 sysib_322;
  551. SysIB_151x sysib_151x;
  552. } SysIB;
  553. QEMU_BUILD_BUG_ON(sizeof(SysIB) != 4096);
  554. /*
  555. * CPU Topology List provided by STSI with fc=15 provides a list
  556. * of two different Topology List Entries (TLE) types to specify
  557. * the topology hierarchy.
  558. *
  559. * - Container Topology List Entry
  560. * Defines a container to contain other Topology List Entries
  561. * of any type, nested containers or CPU.
  562. * - CPU Topology List Entry
  563. * Specifies the CPUs position, type, entitlement and polarization
  564. * of the CPUs contained in the last container TLE.
  565. *
  566. * There can be theoretically up to five levels of containers, QEMU
  567. * uses only three levels, the drawer's, book's and socket's level.
  568. *
  569. * A container with a nesting level (NL) greater than 1 can only
  570. * contain another container of nesting level NL-1.
  571. *
  572. * A container of nesting level 1 (socket), contains as many CPU TLE
  573. * as needed to describe the position and qualities of all CPUs inside
  574. * the container.
  575. * The qualities of a CPU are polarization, entitlement and type.
  576. *
  577. * The CPU TLE defines the position of the CPUs of identical qualities
  578. * using a 64bits mask which first bit has its offset defined by
  579. * the CPU address origin field of the CPU TLE like in:
  580. * CPU address = origin * 64 + bit position within the mask
  581. */
  582. /* Container type Topology List Entry */
  583. typedef struct SYSIBContainerListEntry {
  584. uint8_t nl;
  585. uint8_t reserved[6];
  586. uint8_t id;
  587. } SYSIBContainerListEntry;
  588. QEMU_BUILD_BUG_ON(sizeof(SYSIBContainerListEntry) != 8);
  589. /* CPU type Topology List Entry */
  590. typedef struct SysIBCPUListEntry {
  591. uint8_t nl;
  592. uint8_t reserved0[3];
  593. #define SYSIB_TLE_POLARITY_MASK 0x03
  594. #define SYSIB_TLE_DEDICATED 0x04
  595. uint8_t flags;
  596. uint8_t type;
  597. uint16_t origin;
  598. uint64_t mask;
  599. } SysIBCPUListEntry;
  600. QEMU_BUILD_BUG_ON(sizeof(SysIBCPUListEntry) != 16);
  601. void insert_stsi_15_1_x(S390CPU *cpu, int sel2, uint64_t addr, uint8_t ar, uintptr_t ra);
  602. void s390_cpu_topology_set_changed(bool changed);
  603. /* MMU defines */
  604. #define ASCE_ORIGIN (~0xfffULL) /* segment table origin */
  605. #define ASCE_SUBSPACE 0x200 /* subspace group control */
  606. #define ASCE_PRIVATE_SPACE 0x100 /* private space control */
  607. #define ASCE_ALT_EVENT 0x80 /* storage alteration event control */
  608. #define ASCE_SPACE_SWITCH 0x40 /* space switch event */
  609. #define ASCE_REAL_SPACE 0x20 /* real space control */
  610. #define ASCE_TYPE_MASK 0x0c /* asce table type mask */
  611. #define ASCE_TYPE_REGION1 0x0c /* region first table type */
  612. #define ASCE_TYPE_REGION2 0x08 /* region second table type */
  613. #define ASCE_TYPE_REGION3 0x04 /* region third table type */
  614. #define ASCE_TYPE_SEGMENT 0x00 /* segment table type */
  615. #define ASCE_TABLE_LENGTH 0x03 /* region table length */
  616. #define REGION_ENTRY_ORIGIN 0xfffffffffffff000ULL
  617. #define REGION_ENTRY_P 0x0000000000000200ULL
  618. #define REGION_ENTRY_TF 0x00000000000000c0ULL
  619. #define REGION_ENTRY_I 0x0000000000000020ULL
  620. #define REGION_ENTRY_TT 0x000000000000000cULL
  621. #define REGION_ENTRY_TL 0x0000000000000003ULL
  622. #define REGION_ENTRY_TT_REGION1 0x000000000000000cULL
  623. #define REGION_ENTRY_TT_REGION2 0x0000000000000008ULL
  624. #define REGION_ENTRY_TT_REGION3 0x0000000000000004ULL
  625. #define REGION3_ENTRY_RFAA 0xffffffff80000000ULL
  626. #define REGION3_ENTRY_AV 0x0000000000010000ULL
  627. #define REGION3_ENTRY_ACC 0x000000000000f000ULL
  628. #define REGION3_ENTRY_F 0x0000000000000800ULL
  629. #define REGION3_ENTRY_FC 0x0000000000000400ULL
  630. #define REGION3_ENTRY_IEP 0x0000000000000100ULL
  631. #define REGION3_ENTRY_CR 0x0000000000000010ULL
  632. #define SEGMENT_ENTRY_ORIGIN 0xfffffffffffff800ULL
  633. #define SEGMENT_ENTRY_SFAA 0xfffffffffff00000ULL
  634. #define SEGMENT_ENTRY_AV 0x0000000000010000ULL
  635. #define SEGMENT_ENTRY_ACC 0x000000000000f000ULL
  636. #define SEGMENT_ENTRY_F 0x0000000000000800ULL
  637. #define SEGMENT_ENTRY_FC 0x0000000000000400ULL
  638. #define SEGMENT_ENTRY_P 0x0000000000000200ULL
  639. #define SEGMENT_ENTRY_IEP 0x0000000000000100ULL
  640. #define SEGMENT_ENTRY_I 0x0000000000000020ULL
  641. #define SEGMENT_ENTRY_CS 0x0000000000000010ULL
  642. #define SEGMENT_ENTRY_TT 0x000000000000000cULL
  643. #define SEGMENT_ENTRY_TT_SEGMENT 0x0000000000000000ULL
  644. #define PAGE_ENTRY_0 0x0000000000000800ULL
  645. #define PAGE_ENTRY_I 0x0000000000000400ULL
  646. #define PAGE_ENTRY_P 0x0000000000000200ULL
  647. #define PAGE_ENTRY_IEP 0x0000000000000100ULL
  648. #define VADDR_REGION1_TX_MASK 0xffe0000000000000ULL
  649. #define VADDR_REGION2_TX_MASK 0x001ffc0000000000ULL
  650. #define VADDR_REGION3_TX_MASK 0x000003ff80000000ULL
  651. #define VADDR_SEGMENT_TX_MASK 0x000000007ff00000ULL
  652. #define VADDR_PAGE_TX_MASK 0x00000000000ff000ULL
  653. #define VADDR_REGION1_TX(vaddr) (((vaddr) & VADDR_REGION1_TX_MASK) >> 53)
  654. #define VADDR_REGION2_TX(vaddr) (((vaddr) & VADDR_REGION2_TX_MASK) >> 42)
  655. #define VADDR_REGION3_TX(vaddr) (((vaddr) & VADDR_REGION3_TX_MASK) >> 31)
  656. #define VADDR_SEGMENT_TX(vaddr) (((vaddr) & VADDR_SEGMENT_TX_MASK) >> 20)
  657. #define VADDR_PAGE_TX(vaddr) (((vaddr) & VADDR_PAGE_TX_MASK) >> 12)
  658. #define VADDR_REGION1_TL(vaddr) (((vaddr) & 0xc000000000000000ULL) >> 62)
  659. #define VADDR_REGION2_TL(vaddr) (((vaddr) & 0x0018000000000000ULL) >> 51)
  660. #define VADDR_REGION3_TL(vaddr) (((vaddr) & 0x0000030000000000ULL) >> 40)
  661. #define VADDR_SEGMENT_TL(vaddr) (((vaddr) & 0x0000000060000000ULL) >> 29)
  662. #define SK_C (0x1 << 1)
  663. #define SK_R (0x1 << 2)
  664. #define SK_F (0x1 << 3)
  665. #define SK_ACC_MASK (0xf << 4)
  666. /* SIGP order codes */
  667. #define SIGP_SENSE 0x01
  668. #define SIGP_EXTERNAL_CALL 0x02
  669. #define SIGP_EMERGENCY 0x03
  670. #define SIGP_START 0x04
  671. #define SIGP_STOP 0x05
  672. #define SIGP_RESTART 0x06
  673. #define SIGP_STOP_STORE_STATUS 0x09
  674. #define SIGP_INITIAL_CPU_RESET 0x0b
  675. #define SIGP_CPU_RESET 0x0c
  676. #define SIGP_SET_PREFIX 0x0d
  677. #define SIGP_STORE_STATUS_ADDR 0x0e
  678. #define SIGP_SET_ARCH 0x12
  679. #define SIGP_COND_EMERGENCY 0x13
  680. #define SIGP_SENSE_RUNNING 0x15
  681. #define SIGP_STORE_ADTL_STATUS 0x17
  682. /* SIGP condition codes */
  683. #define SIGP_CC_ORDER_CODE_ACCEPTED 0
  684. #define SIGP_CC_STATUS_STORED 1
  685. #define SIGP_CC_BUSY 2
  686. #define SIGP_CC_NOT_OPERATIONAL 3
  687. /* SIGP status bits */
  688. #define SIGP_STAT_EQUIPMENT_CHECK 0x80000000UL
  689. #define SIGP_STAT_NOT_RUNNING 0x00000400UL
  690. #define SIGP_STAT_INCORRECT_STATE 0x00000200UL
  691. #define SIGP_STAT_INVALID_PARAMETER 0x00000100UL
  692. #define SIGP_STAT_EXT_CALL_PENDING 0x00000080UL
  693. #define SIGP_STAT_STOPPED 0x00000040UL
  694. #define SIGP_STAT_OPERATOR_INTERV 0x00000020UL
  695. #define SIGP_STAT_CHECK_STOP 0x00000010UL
  696. #define SIGP_STAT_INOPERATIVE 0x00000004UL
  697. #define SIGP_STAT_INVALID_ORDER 0x00000002UL
  698. #define SIGP_STAT_RECEIVER_CHECK 0x00000001UL
  699. /* SIGP order code mask corresponding to bit positions 56-63 */
  700. #define SIGP_ORDER_MASK 0x000000ff
  701. /* machine check interruption code */
  702. /* subclasses */
  703. #define MCIC_SC_SD 0x8000000000000000ULL
  704. #define MCIC_SC_PD 0x4000000000000000ULL
  705. #define MCIC_SC_SR 0x2000000000000000ULL
  706. #define MCIC_SC_CD 0x0800000000000000ULL
  707. #define MCIC_SC_ED 0x0400000000000000ULL
  708. #define MCIC_SC_DG 0x0100000000000000ULL
  709. #define MCIC_SC_W 0x0080000000000000ULL
  710. #define MCIC_SC_CP 0x0040000000000000ULL
  711. #define MCIC_SC_SP 0x0020000000000000ULL
  712. #define MCIC_SC_CK 0x0010000000000000ULL
  713. /* subclass modifiers */
  714. #define MCIC_SCM_B 0x0002000000000000ULL
  715. #define MCIC_SCM_DA 0x0000000020000000ULL
  716. #define MCIC_SCM_AP 0x0000000000080000ULL
  717. /* storage errors */
  718. #define MCIC_SE_SE 0x0000800000000000ULL
  719. #define MCIC_SE_SC 0x0000400000000000ULL
  720. #define MCIC_SE_KE 0x0000200000000000ULL
  721. #define MCIC_SE_DS 0x0000100000000000ULL
  722. #define MCIC_SE_IE 0x0000000080000000ULL
  723. /* validity bits */
  724. #define MCIC_VB_WP 0x0000080000000000ULL
  725. #define MCIC_VB_MS 0x0000040000000000ULL
  726. #define MCIC_VB_PM 0x0000020000000000ULL
  727. #define MCIC_VB_IA 0x0000010000000000ULL
  728. #define MCIC_VB_FA 0x0000008000000000ULL
  729. #define MCIC_VB_VR 0x0000004000000000ULL
  730. #define MCIC_VB_EC 0x0000002000000000ULL
  731. #define MCIC_VB_FP 0x0000001000000000ULL
  732. #define MCIC_VB_GR 0x0000000800000000ULL
  733. #define MCIC_VB_CR 0x0000000400000000ULL
  734. #define MCIC_VB_ST 0x0000000100000000ULL
  735. #define MCIC_VB_AR 0x0000000040000000ULL
  736. #define MCIC_VB_GS 0x0000000008000000ULL
  737. #define MCIC_VB_PR 0x0000000000200000ULL
  738. #define MCIC_VB_FC 0x0000000000100000ULL
  739. #define MCIC_VB_CT 0x0000000000020000ULL
  740. #define MCIC_VB_CC 0x0000000000010000ULL
  741. static inline uint64_t s390_build_validity_mcic(void)
  742. {
  743. uint64_t mcic;
  744. /*
  745. * Indicate all validity bits (no damage) only. Other bits have to be
  746. * added by the caller. (storage errors, subclasses and subclass modifiers)
  747. */
  748. mcic = MCIC_VB_WP | MCIC_VB_MS | MCIC_VB_PM | MCIC_VB_IA | MCIC_VB_FP |
  749. MCIC_VB_GR | MCIC_VB_CR | MCIC_VB_ST | MCIC_VB_AR | MCIC_VB_PR |
  750. MCIC_VB_FC | MCIC_VB_CT | MCIC_VB_CC;
  751. if (s390_has_feat(S390_FEAT_VECTOR)) {
  752. mcic |= MCIC_VB_VR;
  753. }
  754. if (s390_has_feat(S390_FEAT_GUARDED_STORAGE)) {
  755. mcic |= MCIC_VB_GS;
  756. }
  757. return mcic;
  758. }
  759. static inline void s390_do_cpu_full_reset(CPUState *cs, run_on_cpu_data arg)
  760. {
  761. cpu_reset(cs);
  762. }
  763. static inline void s390_do_cpu_reset(CPUState *cs, run_on_cpu_data arg)
  764. {
  765. S390CPUClass *scc = S390_CPU_GET_CLASS(cs);
  766. scc->reset(cs, S390_CPU_RESET_NORMAL);
  767. }
  768. static inline void s390_do_cpu_initial_reset(CPUState *cs, run_on_cpu_data arg)
  769. {
  770. S390CPUClass *scc = S390_CPU_GET_CLASS(cs);
  771. scc->reset(cs, S390_CPU_RESET_INITIAL);
  772. }
  773. static inline void s390_do_cpu_load_normal(CPUState *cs, run_on_cpu_data arg)
  774. {
  775. S390CPUClass *scc = S390_CPU_GET_CLASS(cs);
  776. scc->load_normal(cs);
  777. }
  778. /* cpu.c */
  779. void s390_crypto_reset(void);
  780. int s390_set_memory_limit(uint64_t new_limit, uint64_t *hw_limit);
  781. void s390_set_max_pagesize(uint64_t pagesize, Error **errp);
  782. void s390_cmma_reset(void);
  783. void s390_enable_css_support(S390CPU *cpu);
  784. void s390_do_cpu_set_diag318(CPUState *cs, run_on_cpu_data arg);
  785. int s390_assign_subch_ioeventfd(EventNotifier *notifier, uint32_t sch_id,
  786. int vq, bool assign);
  787. #ifndef CONFIG_USER_ONLY
  788. unsigned int s390_cpu_set_state(uint8_t cpu_state, S390CPU *cpu);
  789. #else
  790. static inline unsigned int s390_cpu_set_state(uint8_t cpu_state, S390CPU *cpu)
  791. {
  792. return 0;
  793. }
  794. #endif /* CONFIG_USER_ONLY */
  795. static inline uint8_t s390_cpu_get_state(S390CPU *cpu)
  796. {
  797. return cpu->env.cpu_state;
  798. }
  799. /* cpu_models.c */
  800. void s390_cpu_list(void);
  801. #define cpu_list s390_cpu_list
  802. void s390_set_qemu_cpu_model(uint16_t type, uint8_t gen, uint8_t ec_ga,
  803. const S390FeatInit feat_init);
  804. /* helper.c */
  805. #define CPU_RESOLVING_TYPE TYPE_S390_CPU
  806. /* interrupt.c */
  807. #define RA_IGNORED 0
  808. void s390_program_interrupt(CPUS390XState *env, uint32_t code, uintptr_t ra);
  809. /* service interrupts are floating therefore we must not pass an cpustate */
  810. void s390_sclp_extint(uint32_t parm);
  811. /* mmu_helper.c */
  812. int s390_cpu_virt_mem_rw(S390CPU *cpu, vaddr laddr, uint8_t ar, void *hostbuf,
  813. int len, bool is_write);
  814. #define s390_cpu_virt_mem_read(cpu, laddr, ar, dest, len) \
  815. s390_cpu_virt_mem_rw(cpu, laddr, ar, dest, len, false)
  816. #define s390_cpu_virt_mem_write(cpu, laddr, ar, dest, len) \
  817. s390_cpu_virt_mem_rw(cpu, laddr, ar, dest, len, true)
  818. #define s390_cpu_virt_mem_check_read(cpu, laddr, ar, len) \
  819. s390_cpu_virt_mem_rw(cpu, laddr, ar, NULL, len, false)
  820. #define s390_cpu_virt_mem_check_write(cpu, laddr, ar, len) \
  821. s390_cpu_virt_mem_rw(cpu, laddr, ar, NULL, len, true)
  822. void s390_cpu_virt_mem_handle_exc(S390CPU *cpu, uintptr_t ra);
  823. int s390_cpu_pv_mem_rw(S390CPU *cpu, unsigned int offset, void *hostbuf,
  824. int len, bool is_write);
  825. #define s390_cpu_pv_mem_read(cpu, offset, dest, len) \
  826. s390_cpu_pv_mem_rw(cpu, offset, dest, len, false)
  827. #define s390_cpu_pv_mem_write(cpu, offset, dest, len) \
  828. s390_cpu_pv_mem_rw(cpu, offset, dest, len, true)
  829. /* sigp.c */
  830. int s390_cpu_restart(S390CPU *cpu);
  831. void s390_init_sigp(void);
  832. /* helper.c */
  833. void s390_cpu_set_psw(CPUS390XState *env, uint64_t mask, uint64_t addr);
  834. uint64_t s390_cpu_get_psw_mask(CPUS390XState *env);
  835. /* outside of target/s390x/ */
  836. S390CPU *s390_cpu_addr2state(uint16_t cpu_addr);
  837. #include "exec/cpu-all.h"
  838. #endif