2
0

helper.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * LatticeMico32 helper routines.
  3. *
  4. * Copyright (c) 2010 Michael Walle <michael@walle.cc>
  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. #include "cpu.h"
  20. #include "qemu/host-utils.h"
  21. int lm32_cpu_handle_mmu_fault(CPUState *cs, vaddr address, int rw,
  22. int mmu_idx)
  23. {
  24. LM32CPU *cpu = LM32_CPU(cs);
  25. CPULM32State *env = &cpu->env;
  26. int prot;
  27. address &= TARGET_PAGE_MASK;
  28. prot = PAGE_BITS;
  29. if (env->flags & LM32_FLAG_IGNORE_MSB) {
  30. tlb_set_page(cs, address, address & 0x7fffffff, prot, mmu_idx,
  31. TARGET_PAGE_SIZE);
  32. } else {
  33. tlb_set_page(cs, address, address, prot, mmu_idx, TARGET_PAGE_SIZE);
  34. }
  35. return 0;
  36. }
  37. hwaddr lm32_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
  38. {
  39. LM32CPU *cpu = LM32_CPU(cs);
  40. addr &= TARGET_PAGE_MASK;
  41. if (cpu->env.flags & LM32_FLAG_IGNORE_MSB) {
  42. return addr & 0x7fffffff;
  43. } else {
  44. return addr;
  45. }
  46. }
  47. void lm32_breakpoint_insert(CPULM32State *env, int idx, target_ulong address)
  48. {
  49. LM32CPU *cpu = lm32_env_get_cpu(env);
  50. cpu_breakpoint_insert(CPU(cpu), address, BP_CPU,
  51. &env->cpu_breakpoint[idx]);
  52. }
  53. void lm32_breakpoint_remove(CPULM32State *env, int idx)
  54. {
  55. LM32CPU *cpu = lm32_env_get_cpu(env);
  56. if (!env->cpu_breakpoint[idx]) {
  57. return;
  58. }
  59. cpu_breakpoint_remove_by_ref(CPU(cpu), env->cpu_breakpoint[idx]);
  60. env->cpu_breakpoint[idx] = NULL;
  61. }
  62. void lm32_watchpoint_insert(CPULM32State *env, int idx, target_ulong address,
  63. lm32_wp_t wp_type)
  64. {
  65. LM32CPU *cpu = lm32_env_get_cpu(env);
  66. int flags = 0;
  67. switch (wp_type) {
  68. case LM32_WP_DISABLED:
  69. /* nothing to to */
  70. break;
  71. case LM32_WP_READ:
  72. flags = BP_CPU | BP_STOP_BEFORE_ACCESS | BP_MEM_READ;
  73. break;
  74. case LM32_WP_WRITE:
  75. flags = BP_CPU | BP_STOP_BEFORE_ACCESS | BP_MEM_WRITE;
  76. break;
  77. case LM32_WP_READ_WRITE:
  78. flags = BP_CPU | BP_STOP_BEFORE_ACCESS | BP_MEM_ACCESS;
  79. break;
  80. }
  81. if (flags != 0) {
  82. cpu_watchpoint_insert(CPU(cpu), address, 1, flags,
  83. &env->cpu_watchpoint[idx]);
  84. }
  85. }
  86. void lm32_watchpoint_remove(CPULM32State *env, int idx)
  87. {
  88. LM32CPU *cpu = lm32_env_get_cpu(env);
  89. if (!env->cpu_watchpoint[idx]) {
  90. return;
  91. }
  92. cpu_watchpoint_remove_by_ref(CPU(cpu), env->cpu_watchpoint[idx]);
  93. env->cpu_watchpoint[idx] = NULL;
  94. }
  95. static bool check_watchpoints(CPULM32State *env)
  96. {
  97. LM32CPU *cpu = lm32_env_get_cpu(env);
  98. int i;
  99. for (i = 0; i < cpu->num_watchpoints; i++) {
  100. if (env->cpu_watchpoint[i] &&
  101. env->cpu_watchpoint[i]->flags & BP_WATCHPOINT_HIT) {
  102. return true;
  103. }
  104. }
  105. return false;
  106. }
  107. void lm32_debug_excp_handler(CPULM32State *env)
  108. {
  109. CPUState *cs = CPU(lm32_env_get_cpu(env));
  110. CPUBreakpoint *bp;
  111. if (cs->watchpoint_hit) {
  112. if (cs->watchpoint_hit->flags & BP_CPU) {
  113. cs->watchpoint_hit = NULL;
  114. if (check_watchpoints(env)) {
  115. raise_exception(env, EXCP_WATCHPOINT);
  116. } else {
  117. cpu_resume_from_signal(cs, NULL);
  118. }
  119. }
  120. } else {
  121. QTAILQ_FOREACH(bp, &cs->breakpoints, entry) {
  122. if (bp->pc == env->pc) {
  123. if (bp->flags & BP_CPU) {
  124. raise_exception(env, EXCP_BREAKPOINT);
  125. }
  126. break;
  127. }
  128. }
  129. }
  130. }
  131. void lm32_cpu_do_interrupt(CPUState *cs)
  132. {
  133. LM32CPU *cpu = LM32_CPU(cs);
  134. CPULM32State *env = &cpu->env;
  135. qemu_log_mask(CPU_LOG_INT,
  136. "exception at pc=%x type=%x\n", env->pc, cs->exception_index);
  137. switch (cs->exception_index) {
  138. case EXCP_INSN_BUS_ERROR:
  139. case EXCP_DATA_BUS_ERROR:
  140. case EXCP_DIVIDE_BY_ZERO:
  141. case EXCP_IRQ:
  142. case EXCP_SYSTEMCALL:
  143. /* non-debug exceptions */
  144. env->regs[R_EA] = env->pc;
  145. env->ie |= (env->ie & IE_IE) ? IE_EIE : 0;
  146. env->ie &= ~IE_IE;
  147. if (env->dc & DC_RE) {
  148. env->pc = env->deba + (cs->exception_index * 32);
  149. } else {
  150. env->pc = env->eba + (cs->exception_index * 32);
  151. }
  152. log_cpu_state_mask(CPU_LOG_INT, cs, 0);
  153. break;
  154. case EXCP_BREAKPOINT:
  155. case EXCP_WATCHPOINT:
  156. /* debug exceptions */
  157. env->regs[R_BA] = env->pc;
  158. env->ie |= (env->ie & IE_IE) ? IE_BIE : 0;
  159. env->ie &= ~IE_IE;
  160. env->pc = env->deba + (cs->exception_index * 32);
  161. log_cpu_state_mask(CPU_LOG_INT, cs, 0);
  162. break;
  163. default:
  164. cpu_abort(cs, "unhandled exception type=%d\n",
  165. cs->exception_index);
  166. break;
  167. }
  168. }
  169. LM32CPU *cpu_lm32_init(const char *cpu_model)
  170. {
  171. return LM32_CPU(cpu_generic_init(TYPE_LM32_CPU, cpu_model));
  172. }
  173. /* Some soc ignores the MSB on the address bus. Thus creating a shadow memory
  174. * area. As a general rule, 0x00000000-0x7fffffff is cached, whereas
  175. * 0x80000000-0xffffffff is not cached and used to access IO devices. */
  176. void cpu_lm32_set_phys_msb_ignore(CPULM32State *env, int value)
  177. {
  178. if (value) {
  179. env->flags |= LM32_FLAG_IGNORE_MSB;
  180. } else {
  181. env->flags &= ~LM32_FLAG_IGNORE_MSB;
  182. }
  183. }