sys_helper.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. * OpenRISC system instructions helper routines
  3. *
  4. * Copyright (c) 2011-2012 Jia Liu <proljc@gmail.com>
  5. * Zhizhou Zhang <etouzh@gmail.com>
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include "qemu/osdep.h"
  21. #include "cpu.h"
  22. #include "exec/exec-all.h"
  23. #include "exec/cputlb.h"
  24. #include "exec/helper-proto.h"
  25. #include "exception.h"
  26. #ifndef CONFIG_USER_ONLY
  27. #include "hw/boards.h"
  28. #endif
  29. #include "tcg/insn-start-words.h"
  30. #define TO_SPR(group, number) (((group) << 11) + (number))
  31. static inline bool is_user(CPUOpenRISCState *env)
  32. {
  33. #ifdef CONFIG_USER_ONLY
  34. return true;
  35. #else
  36. return (env->sr & SR_SM) == 0;
  37. #endif
  38. }
  39. void HELPER(mtspr)(CPUOpenRISCState *env, target_ulong spr, target_ulong rb)
  40. {
  41. OpenRISCCPU *cpu = env_archcpu(env);
  42. #ifndef CONFIG_USER_ONLY
  43. CPUState *cs = env_cpu(env);
  44. target_ulong mr;
  45. int idx;
  46. #endif
  47. /* Handle user accessible SPRs first. */
  48. switch (spr) {
  49. case TO_SPR(0, 20): /* FPCSR */
  50. cpu_set_fpcsr(env, rb);
  51. return;
  52. }
  53. if (is_user(env)) {
  54. raise_exception(cpu, EXCP_ILLEGAL);
  55. }
  56. #ifndef CONFIG_USER_ONLY
  57. switch (spr) {
  58. case TO_SPR(0, 11): /* EVBAR */
  59. env->evbar = rb;
  60. break;
  61. case TO_SPR(0, 16): /* NPC */
  62. cpu_restore_state(cs, GETPC());
  63. /* ??? Mirror or1ksim in not trashing delayed branch state
  64. when "jumping" to the current instruction. */
  65. if (env->pc != rb) {
  66. env->pc = rb;
  67. env->dflag = 0;
  68. }
  69. cpu_loop_exit(cs);
  70. break;
  71. case TO_SPR(0, 17): /* SR */
  72. cpu_set_sr(env, rb);
  73. break;
  74. case TO_SPR(0, 32): /* EPCR */
  75. env->epcr = rb;
  76. break;
  77. case TO_SPR(0, 48): /* EEAR */
  78. env->eear = rb;
  79. break;
  80. case TO_SPR(0, 64): /* ESR */
  81. env->esr = rb;
  82. break;
  83. case TO_SPR(0, 1024) ... TO_SPR(0, 1024 + (16 * 32)): /* Shadow GPRs */
  84. idx = (spr - 1024);
  85. env->shadow_gpr[idx / 32][idx % 32] = rb;
  86. break;
  87. case TO_SPR(1, 512) ... TO_SPR(1, 512 + TLB_SIZE - 1): /* DTLBW0MR 0-127 */
  88. idx = spr - TO_SPR(1, 512);
  89. mr = env->tlb.dtlb[idx].mr;
  90. if (mr & 1) {
  91. tlb_flush_page(cs, mr & TARGET_PAGE_MASK);
  92. }
  93. if (rb & 1) {
  94. tlb_flush_page(cs, rb & TARGET_PAGE_MASK);
  95. }
  96. env->tlb.dtlb[idx].mr = rb;
  97. break;
  98. case TO_SPR(1, 640) ... TO_SPR(1, 640 + TLB_SIZE - 1): /* DTLBW0TR 0-127 */
  99. idx = spr - TO_SPR(1, 640);
  100. env->tlb.dtlb[idx].tr = rb;
  101. break;
  102. case TO_SPR(1, 768) ... TO_SPR(1, 895): /* DTLBW1MR 0-127 */
  103. case TO_SPR(1, 896) ... TO_SPR(1, 1023): /* DTLBW1TR 0-127 */
  104. case TO_SPR(1, 1024) ... TO_SPR(1, 1151): /* DTLBW2MR 0-127 */
  105. case TO_SPR(1, 1152) ... TO_SPR(1, 1279): /* DTLBW2TR 0-127 */
  106. case TO_SPR(1, 1280) ... TO_SPR(1, 1407): /* DTLBW3MR 0-127 */
  107. case TO_SPR(1, 1408) ... TO_SPR(1, 1535): /* DTLBW3TR 0-127 */
  108. break;
  109. case TO_SPR(2, 512) ... TO_SPR(2, 512 + TLB_SIZE - 1): /* ITLBW0MR 0-127 */
  110. idx = spr - TO_SPR(2, 512);
  111. mr = env->tlb.itlb[idx].mr;
  112. if (mr & 1) {
  113. tlb_flush_page(cs, mr & TARGET_PAGE_MASK);
  114. }
  115. if (rb & 1) {
  116. tlb_flush_page(cs, rb & TARGET_PAGE_MASK);
  117. }
  118. env->tlb.itlb[idx].mr = rb;
  119. break;
  120. case TO_SPR(2, 640) ... TO_SPR(2, 640 + TLB_SIZE - 1): /* ITLBW0TR 0-127 */
  121. idx = spr - TO_SPR(2, 640);
  122. env->tlb.itlb[idx].tr = rb;
  123. break;
  124. case TO_SPR(2, 768) ... TO_SPR(2, 895): /* ITLBW1MR 0-127 */
  125. case TO_SPR(2, 896) ... TO_SPR(2, 1023): /* ITLBW1TR 0-127 */
  126. case TO_SPR(2, 1024) ... TO_SPR(2, 1151): /* ITLBW2MR 0-127 */
  127. case TO_SPR(2, 1152) ... TO_SPR(2, 1279): /* ITLBW2TR 0-127 */
  128. case TO_SPR(2, 1280) ... TO_SPR(2, 1407): /* ITLBW3MR 0-127 */
  129. case TO_SPR(2, 1408) ... TO_SPR(2, 1535): /* ITLBW3TR 0-127 */
  130. break;
  131. case TO_SPR(5, 1): /* MACLO */
  132. env->mac = deposit64(env->mac, 0, 32, rb);
  133. break;
  134. case TO_SPR(5, 2): /* MACHI */
  135. env->mac = deposit64(env->mac, 32, 32, rb);
  136. break;
  137. case TO_SPR(8, 0): /* PMR */
  138. env->pmr = rb;
  139. if (env->pmr & PMR_DME || env->pmr & PMR_SME) {
  140. cpu_restore_state(cs, GETPC());
  141. env->pc += 4;
  142. cs->halted = 1;
  143. raise_exception(cpu, EXCP_HALTED);
  144. }
  145. break;
  146. case TO_SPR(9, 0): /* PICMR */
  147. env->picmr = rb;
  148. bql_lock();
  149. if (env->picsr & env->picmr) {
  150. cpu_interrupt(cs, CPU_INTERRUPT_HARD);
  151. } else {
  152. cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
  153. }
  154. bql_unlock();
  155. break;
  156. case TO_SPR(9, 2): /* PICSR */
  157. env->picsr &= ~rb;
  158. break;
  159. case TO_SPR(10, 0): /* TTMR */
  160. {
  161. bql_lock();
  162. if ((env->ttmr & TTMR_M) ^ (rb & TTMR_M)) {
  163. switch (rb & TTMR_M) {
  164. case TIMER_NONE:
  165. cpu_openrisc_count_stop(cpu);
  166. break;
  167. case TIMER_INTR:
  168. case TIMER_SHOT:
  169. case TIMER_CONT:
  170. cpu_openrisc_count_start(cpu);
  171. break;
  172. default:
  173. break;
  174. }
  175. }
  176. int ip = env->ttmr & TTMR_IP;
  177. if (rb & TTMR_IP) { /* Keep IP bit. */
  178. env->ttmr = (rb & ~TTMR_IP) | ip;
  179. } else { /* Clear IP bit. */
  180. env->ttmr = rb & ~TTMR_IP;
  181. cs->interrupt_request &= ~CPU_INTERRUPT_TIMER;
  182. }
  183. cpu_openrisc_timer_update(cpu);
  184. bql_unlock();
  185. }
  186. break;
  187. case TO_SPR(10, 1): /* TTCR */
  188. bql_lock();
  189. cpu_openrisc_count_set(cpu, rb);
  190. cpu_openrisc_timer_update(cpu);
  191. bql_unlock();
  192. break;
  193. }
  194. #endif
  195. }
  196. target_ulong HELPER(mfspr)(CPUOpenRISCState *env, target_ulong rd,
  197. target_ulong spr)
  198. {
  199. OpenRISCCPU *cpu = env_archcpu(env);
  200. #ifndef CONFIG_USER_ONLY
  201. uint64_t data[TARGET_INSN_START_WORDS];
  202. MachineState *ms = MACHINE(qdev_get_machine());
  203. CPUState *cs = env_cpu(env);
  204. int idx;
  205. #endif
  206. /* Handle user accessible SPRs first. */
  207. switch (spr) {
  208. case TO_SPR(0, 20): /* FPCSR */
  209. return env->fpcsr;
  210. }
  211. if (is_user(env)) {
  212. raise_exception(cpu, EXCP_ILLEGAL);
  213. }
  214. #ifndef CONFIG_USER_ONLY
  215. switch (spr) {
  216. case TO_SPR(0, 0): /* VR */
  217. return env->vr;
  218. case TO_SPR(0, 1): /* UPR */
  219. return env->upr;
  220. case TO_SPR(0, 2): /* CPUCFGR */
  221. return env->cpucfgr;
  222. case TO_SPR(0, 3): /* DMMUCFGR */
  223. return env->dmmucfgr;
  224. case TO_SPR(0, 4): /* IMMUCFGR */
  225. return env->immucfgr;
  226. case TO_SPR(0, 9): /* VR2 */
  227. return env->vr2;
  228. case TO_SPR(0, 10): /* AVR */
  229. return env->avr;
  230. case TO_SPR(0, 11): /* EVBAR */
  231. return env->evbar;
  232. case TO_SPR(0, 16): /* NPC (equals PC) */
  233. if (cpu_unwind_state_data(cs, GETPC(), data)) {
  234. return data[0];
  235. }
  236. return env->pc;
  237. case TO_SPR(0, 17): /* SR */
  238. return cpu_get_sr(env);
  239. case TO_SPR(0, 18): /* PPC */
  240. if (cpu_unwind_state_data(cs, GETPC(), data)) {
  241. if (data[1] & 2) {
  242. return data[0] - 4;
  243. }
  244. }
  245. return env->ppc;
  246. case TO_SPR(0, 32): /* EPCR */
  247. return env->epcr;
  248. case TO_SPR(0, 48): /* EEAR */
  249. return env->eear;
  250. case TO_SPR(0, 64): /* ESR */
  251. return env->esr;
  252. case TO_SPR(0, 128): /* COREID */
  253. return cpu->parent_obj.cpu_index;
  254. case TO_SPR(0, 129): /* NUMCORES */
  255. return ms->smp.max_cpus;
  256. case TO_SPR(0, 1024) ... TO_SPR(0, 1024 + (16 * 32)): /* Shadow GPRs */
  257. idx = (spr - 1024);
  258. return env->shadow_gpr[idx / 32][idx % 32];
  259. case TO_SPR(1, 512) ... TO_SPR(1, 512 + TLB_SIZE - 1): /* DTLBW0MR 0-127 */
  260. idx = spr - TO_SPR(1, 512);
  261. return env->tlb.dtlb[idx].mr;
  262. case TO_SPR(1, 640) ... TO_SPR(1, 640 + TLB_SIZE - 1): /* DTLBW0TR 0-127 */
  263. idx = spr - TO_SPR(1, 640);
  264. return env->tlb.dtlb[idx].tr;
  265. case TO_SPR(1, 768) ... TO_SPR(1, 895): /* DTLBW1MR 0-127 */
  266. case TO_SPR(1, 896) ... TO_SPR(1, 1023): /* DTLBW1TR 0-127 */
  267. case TO_SPR(1, 1024) ... TO_SPR(1, 1151): /* DTLBW2MR 0-127 */
  268. case TO_SPR(1, 1152) ... TO_SPR(1, 1279): /* DTLBW2TR 0-127 */
  269. case TO_SPR(1, 1280) ... TO_SPR(1, 1407): /* DTLBW3MR 0-127 */
  270. case TO_SPR(1, 1408) ... TO_SPR(1, 1535): /* DTLBW3TR 0-127 */
  271. break;
  272. case TO_SPR(2, 512) ... TO_SPR(2, 512 + TLB_SIZE - 1): /* ITLBW0MR 0-127 */
  273. idx = spr - TO_SPR(2, 512);
  274. return env->tlb.itlb[idx].mr;
  275. case TO_SPR(2, 640) ... TO_SPR(2, 640 + TLB_SIZE - 1): /* ITLBW0TR 0-127 */
  276. idx = spr - TO_SPR(2, 640);
  277. return env->tlb.itlb[idx].tr;
  278. case TO_SPR(2, 768) ... TO_SPR(2, 895): /* ITLBW1MR 0-127 */
  279. case TO_SPR(2, 896) ... TO_SPR(2, 1023): /* ITLBW1TR 0-127 */
  280. case TO_SPR(2, 1024) ... TO_SPR(2, 1151): /* ITLBW2MR 0-127 */
  281. case TO_SPR(2, 1152) ... TO_SPR(2, 1279): /* ITLBW2TR 0-127 */
  282. case TO_SPR(2, 1280) ... TO_SPR(2, 1407): /* ITLBW3MR 0-127 */
  283. case TO_SPR(2, 1408) ... TO_SPR(2, 1535): /* ITLBW3TR 0-127 */
  284. break;
  285. case TO_SPR(5, 1): /* MACLO */
  286. return (uint32_t)env->mac;
  287. break;
  288. case TO_SPR(5, 2): /* MACHI */
  289. return env->mac >> 32;
  290. break;
  291. case TO_SPR(8, 0): /* PMR */
  292. return env->pmr;
  293. case TO_SPR(9, 0): /* PICMR */
  294. return env->picmr;
  295. case TO_SPR(9, 2): /* PICSR */
  296. return env->picsr;
  297. case TO_SPR(10, 0): /* TTMR */
  298. return env->ttmr;
  299. case TO_SPR(10, 1): /* TTCR */
  300. bql_lock();
  301. cpu_openrisc_count_update(cpu);
  302. bql_unlock();
  303. return cpu_openrisc_count_get(cpu);
  304. }
  305. #endif
  306. /* for rd is passed in, if rd unchanged, just keep it back. */
  307. return rd;
  308. }