cpu.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*
  2. * OpenRISC virtual CPU header.
  3. *
  4. * Copyright (c) 2011-2012 Jia Liu <proljc@gmail.com>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef CPU_OPENRISC_H
  20. #define CPU_OPENRISC_H
  21. #define TARGET_LONG_BITS 32
  22. #define CPUArchState struct CPUOpenRISCState
  23. /* cpu_openrisc_map_address_* in CPUOpenRISCTLBContext need this decl. */
  24. struct OpenRISCCPU;
  25. #include "config.h"
  26. #include "qemu-common.h"
  27. #include "exec/cpu-defs.h"
  28. #include "fpu/softfloat.h"
  29. #include "qom/cpu.h"
  30. #define TYPE_OPENRISC_CPU "or32-cpu"
  31. #define OPENRISC_CPU_CLASS(klass) \
  32. OBJECT_CLASS_CHECK(OpenRISCCPUClass, (klass), TYPE_OPENRISC_CPU)
  33. #define OPENRISC_CPU(obj) \
  34. OBJECT_CHECK(OpenRISCCPU, (obj), TYPE_OPENRISC_CPU)
  35. #define OPENRISC_CPU_GET_CLASS(obj) \
  36. OBJECT_GET_CLASS(OpenRISCCPUClass, (obj), TYPE_OPENRISC_CPU)
  37. /**
  38. * OpenRISCCPUClass:
  39. * @parent_realize: The parent class' realize handler.
  40. * @parent_reset: The parent class' reset handler.
  41. *
  42. * A OpenRISC CPU model.
  43. */
  44. typedef struct OpenRISCCPUClass {
  45. /*< private >*/
  46. CPUClass parent_class;
  47. /*< public >*/
  48. DeviceRealize parent_realize;
  49. void (*parent_reset)(CPUState *cpu);
  50. } OpenRISCCPUClass;
  51. #define NB_MMU_MODES 3
  52. enum {
  53. MMU_NOMMU_IDX = 0,
  54. MMU_SUPERVISOR_IDX = 1,
  55. MMU_USER_IDX = 2,
  56. };
  57. #define TARGET_PAGE_BITS 13
  58. #define TARGET_PHYS_ADDR_SPACE_BITS 32
  59. #define TARGET_VIRT_ADDR_SPACE_BITS 32
  60. #define SET_FP_CAUSE(reg, v) do {\
  61. (reg) = ((reg) & ~(0x3f << 12)) | \
  62. ((v & 0x3f) << 12);\
  63. } while (0)
  64. #define GET_FP_ENABLE(reg) (((reg) >> 7) & 0x1f)
  65. #define UPDATE_FP_FLAGS(reg, v) do {\
  66. (reg) |= ((v & 0x1f) << 2);\
  67. } while (0)
  68. /* Version Register */
  69. #define SPR_VR 0xFFFF003F
  70. /* Internal flags, delay slot flag */
  71. #define D_FLAG 1
  72. /* Interrupt */
  73. #define NR_IRQS 32
  74. /* Unit presece register */
  75. enum {
  76. UPR_UP = (1 << 0),
  77. UPR_DCP = (1 << 1),
  78. UPR_ICP = (1 << 2),
  79. UPR_DMP = (1 << 3),
  80. UPR_IMP = (1 << 4),
  81. UPR_MP = (1 << 5),
  82. UPR_DUP = (1 << 6),
  83. UPR_PCUR = (1 << 7),
  84. UPR_PMP = (1 << 8),
  85. UPR_PICP = (1 << 9),
  86. UPR_TTP = (1 << 10),
  87. UPR_CUP = (255 << 24),
  88. };
  89. /* CPU configure register */
  90. enum {
  91. CPUCFGR_NSGF = (15 << 0),
  92. CPUCFGR_CGF = (1 << 4),
  93. CPUCFGR_OB32S = (1 << 5),
  94. CPUCFGR_OB64S = (1 << 6),
  95. CPUCFGR_OF32S = (1 << 7),
  96. CPUCFGR_OF64S = (1 << 8),
  97. CPUCFGR_OV64S = (1 << 9),
  98. };
  99. /* DMMU configure register */
  100. enum {
  101. DMMUCFGR_NTW = (3 << 0),
  102. DMMUCFGR_NTS = (7 << 2),
  103. DMMUCFGR_NAE = (7 << 5),
  104. DMMUCFGR_CRI = (1 << 8),
  105. DMMUCFGR_PRI = (1 << 9),
  106. DMMUCFGR_TEIRI = (1 << 10),
  107. DMMUCFGR_HTR = (1 << 11),
  108. };
  109. /* IMMU configure register */
  110. enum {
  111. IMMUCFGR_NTW = (3 << 0),
  112. IMMUCFGR_NTS = (7 << 2),
  113. IMMUCFGR_NAE = (7 << 5),
  114. IMMUCFGR_CRI = (1 << 8),
  115. IMMUCFGR_PRI = (1 << 9),
  116. IMMUCFGR_TEIRI = (1 << 10),
  117. IMMUCFGR_HTR = (1 << 11),
  118. };
  119. /* Float point control status register */
  120. enum {
  121. FPCSR_FPEE = 1,
  122. FPCSR_RM = (3 << 1),
  123. FPCSR_OVF = (1 << 3),
  124. FPCSR_UNF = (1 << 4),
  125. FPCSR_SNF = (1 << 5),
  126. FPCSR_QNF = (1 << 6),
  127. FPCSR_ZF = (1 << 7),
  128. FPCSR_IXF = (1 << 8),
  129. FPCSR_IVF = (1 << 9),
  130. FPCSR_INF = (1 << 10),
  131. FPCSR_DZF = (1 << 11),
  132. };
  133. /* Exceptions indices */
  134. enum {
  135. EXCP_RESET = 0x1,
  136. EXCP_BUSERR = 0x2,
  137. EXCP_DPF = 0x3,
  138. EXCP_IPF = 0x4,
  139. EXCP_TICK = 0x5,
  140. EXCP_ALIGN = 0x6,
  141. EXCP_ILLEGAL = 0x7,
  142. EXCP_INT = 0x8,
  143. EXCP_DTLBMISS = 0x9,
  144. EXCP_ITLBMISS = 0xa,
  145. EXCP_RANGE = 0xb,
  146. EXCP_SYSCALL = 0xc,
  147. EXCP_FPE = 0xd,
  148. EXCP_TRAP = 0xe,
  149. EXCP_NR,
  150. };
  151. /* Supervisor register */
  152. enum {
  153. SR_SM = (1 << 0),
  154. SR_TEE = (1 << 1),
  155. SR_IEE = (1 << 2),
  156. SR_DCE = (1 << 3),
  157. SR_ICE = (1 << 4),
  158. SR_DME = (1 << 5),
  159. SR_IME = (1 << 6),
  160. SR_LEE = (1 << 7),
  161. SR_CE = (1 << 8),
  162. SR_F = (1 << 9),
  163. SR_CY = (1 << 10),
  164. SR_OV = (1 << 11),
  165. SR_OVE = (1 << 12),
  166. SR_DSX = (1 << 13),
  167. SR_EPH = (1 << 14),
  168. SR_FO = (1 << 15),
  169. SR_SUMRA = (1 << 16),
  170. SR_SCE = (1 << 17),
  171. };
  172. /* OpenRISC Hardware Capabilities */
  173. enum {
  174. OPENRISC_FEATURE_NSGF = (15 << 0),
  175. OPENRISC_FEATURE_CGF = (1 << 4),
  176. OPENRISC_FEATURE_OB32S = (1 << 5),
  177. OPENRISC_FEATURE_OB64S = (1 << 6),
  178. OPENRISC_FEATURE_OF32S = (1 << 7),
  179. OPENRISC_FEATURE_OF64S = (1 << 8),
  180. OPENRISC_FEATURE_OV64S = (1 << 9),
  181. };
  182. /* Tick Timer Mode Register */
  183. enum {
  184. TTMR_TP = (0xfffffff),
  185. TTMR_IP = (1 << 28),
  186. TTMR_IE = (1 << 29),
  187. TTMR_M = (3 << 30),
  188. };
  189. /* Timer Mode */
  190. enum {
  191. TIMER_NONE = (0 << 30),
  192. TIMER_INTR = (1 << 30),
  193. TIMER_SHOT = (2 << 30),
  194. TIMER_CONT = (3 << 30),
  195. };
  196. /* TLB size */
  197. enum {
  198. DTLB_WAYS = 1,
  199. DTLB_SIZE = 64,
  200. DTLB_MASK = (DTLB_SIZE-1),
  201. ITLB_WAYS = 1,
  202. ITLB_SIZE = 64,
  203. ITLB_MASK = (ITLB_SIZE-1),
  204. };
  205. /* TLB prot */
  206. enum {
  207. URE = (1 << 6),
  208. UWE = (1 << 7),
  209. SRE = (1 << 8),
  210. SWE = (1 << 9),
  211. SXE = (1 << 6),
  212. UXE = (1 << 7),
  213. };
  214. /* check if tlb available */
  215. enum {
  216. TLBRET_INVALID = -3,
  217. TLBRET_NOMATCH = -2,
  218. TLBRET_BADADDR = -1,
  219. TLBRET_MATCH = 0
  220. };
  221. typedef struct OpenRISCTLBEntry {
  222. uint32_t mr;
  223. uint32_t tr;
  224. } OpenRISCTLBEntry;
  225. #ifndef CONFIG_USER_ONLY
  226. typedef struct CPUOpenRISCTLBContext {
  227. OpenRISCTLBEntry itlb[ITLB_WAYS][ITLB_SIZE];
  228. OpenRISCTLBEntry dtlb[DTLB_WAYS][DTLB_SIZE];
  229. int (*cpu_openrisc_map_address_code)(struct OpenRISCCPU *cpu,
  230. hwaddr *physical,
  231. int *prot,
  232. target_ulong address, int rw);
  233. int (*cpu_openrisc_map_address_data)(struct OpenRISCCPU *cpu,
  234. hwaddr *physical,
  235. int *prot,
  236. target_ulong address, int rw);
  237. } CPUOpenRISCTLBContext;
  238. #endif
  239. typedef struct CPUOpenRISCState {
  240. target_ulong gpr[32]; /* General registers */
  241. target_ulong pc; /* Program counter */
  242. target_ulong npc; /* Next PC */
  243. target_ulong ppc; /* Prev PC */
  244. target_ulong jmp_pc; /* Jump PC */
  245. target_ulong machi; /* Multiply register MACHI */
  246. target_ulong maclo; /* Multiply register MACLO */
  247. target_ulong fpmaddhi; /* Multiply and add float register FPMADDHI */
  248. target_ulong fpmaddlo; /* Multiply and add float register FPMADDLO */
  249. target_ulong epcr; /* Exception PC register */
  250. target_ulong eear; /* Exception EA register */
  251. uint32_t sr; /* Supervisor register */
  252. uint32_t vr; /* Version register */
  253. uint32_t upr; /* Unit presence register */
  254. uint32_t cpucfgr; /* CPU configure register */
  255. uint32_t dmmucfgr; /* DMMU configure register */
  256. uint32_t immucfgr; /* IMMU configure register */
  257. uint32_t esr; /* Exception supervisor register */
  258. uint32_t fpcsr; /* Float register */
  259. float_status fp_status;
  260. uint32_t flags; /* cpu_flags, we only use it for exception
  261. in solt so far. */
  262. uint32_t btaken; /* the SR_F bit */
  263. CPU_COMMON
  264. /* Fields from here on are preserved across CPU reset. */
  265. #ifndef CONFIG_USER_ONLY
  266. CPUOpenRISCTLBContext * tlb;
  267. QEMUTimer *timer;
  268. uint32_t ttmr; /* Timer tick mode register */
  269. uint32_t ttcr; /* Timer tick count register */
  270. uint32_t picmr; /* Interrupt mask register */
  271. uint32_t picsr; /* Interrupt contrl register*/
  272. #endif
  273. void *irq[32]; /* Interrupt irq input */
  274. } CPUOpenRISCState;
  275. /**
  276. * OpenRISCCPU:
  277. * @env: #CPUOpenRISCState
  278. *
  279. * A OpenRISC CPU.
  280. */
  281. typedef struct OpenRISCCPU {
  282. /*< private >*/
  283. CPUState parent_obj;
  284. /*< public >*/
  285. CPUOpenRISCState env;
  286. uint32_t feature; /* CPU Capabilities */
  287. } OpenRISCCPU;
  288. static inline OpenRISCCPU *openrisc_env_get_cpu(CPUOpenRISCState *env)
  289. {
  290. return container_of(env, OpenRISCCPU, env);
  291. }
  292. #define ENV_GET_CPU(e) CPU(openrisc_env_get_cpu(e))
  293. #define ENV_OFFSET offsetof(OpenRISCCPU, env)
  294. OpenRISCCPU *cpu_openrisc_init(const char *cpu_model);
  295. void cpu_openrisc_list(FILE *f, fprintf_function cpu_fprintf);
  296. int cpu_openrisc_exec(CPUState *cpu);
  297. void openrisc_cpu_do_interrupt(CPUState *cpu);
  298. bool openrisc_cpu_exec_interrupt(CPUState *cpu, int int_req);
  299. void openrisc_cpu_dump_state(CPUState *cpu, FILE *f,
  300. fprintf_function cpu_fprintf, int flags);
  301. hwaddr openrisc_cpu_get_phys_page_debug(CPUState *cpu, vaddr addr);
  302. int openrisc_cpu_gdb_read_register(CPUState *cpu, uint8_t *buf, int reg);
  303. int openrisc_cpu_gdb_write_register(CPUState *cpu, uint8_t *buf, int reg);
  304. void openrisc_translate_init(void);
  305. int openrisc_cpu_handle_mmu_fault(CPUState *cpu, vaddr address,
  306. int rw, int mmu_idx);
  307. int cpu_openrisc_signal_handler(int host_signum, void *pinfo, void *puc);
  308. #define cpu_list cpu_openrisc_list
  309. #define cpu_exec cpu_openrisc_exec
  310. #define cpu_signal_handler cpu_openrisc_signal_handler
  311. #ifndef CONFIG_USER_ONLY
  312. extern const struct VMStateDescription vmstate_openrisc_cpu;
  313. /* hw/openrisc_pic.c */
  314. void cpu_openrisc_pic_init(OpenRISCCPU *cpu);
  315. /* hw/openrisc_timer.c */
  316. void cpu_openrisc_clock_init(OpenRISCCPU *cpu);
  317. void cpu_openrisc_count_update(OpenRISCCPU *cpu);
  318. void cpu_openrisc_timer_update(OpenRISCCPU *cpu);
  319. void cpu_openrisc_count_start(OpenRISCCPU *cpu);
  320. void cpu_openrisc_count_stop(OpenRISCCPU *cpu);
  321. void cpu_openrisc_mmu_init(OpenRISCCPU *cpu);
  322. int cpu_openrisc_get_phys_nommu(OpenRISCCPU *cpu,
  323. hwaddr *physical,
  324. int *prot, target_ulong address, int rw);
  325. int cpu_openrisc_get_phys_code(OpenRISCCPU *cpu,
  326. hwaddr *physical,
  327. int *prot, target_ulong address, int rw);
  328. int cpu_openrisc_get_phys_data(OpenRISCCPU *cpu,
  329. hwaddr *physical,
  330. int *prot, target_ulong address, int rw);
  331. #endif
  332. #define cpu_init(cpu_model) CPU(cpu_openrisc_init(cpu_model))
  333. #include "exec/cpu-all.h"
  334. static inline void cpu_get_tb_cpu_state(CPUOpenRISCState *env,
  335. target_ulong *pc,
  336. target_ulong *cs_base, int *flags)
  337. {
  338. *pc = env->pc;
  339. *cs_base = 0;
  340. /* D_FLAG -- branch instruction exception */
  341. *flags = (env->flags & D_FLAG);
  342. }
  343. static inline int cpu_mmu_index(CPUOpenRISCState *env, bool ifetch)
  344. {
  345. if (!(env->sr & SR_IME)) {
  346. return MMU_NOMMU_IDX;
  347. }
  348. return (env->sr & SR_SM) == 0 ? MMU_USER_IDX : MMU_SUPERVISOR_IDX;
  349. }
  350. #define CPU_INTERRUPT_TIMER CPU_INTERRUPT_TGT_INT_0
  351. #include "exec/exec-all.h"
  352. #endif /* CPU_OPENRISC_H */