2 * LatticeMico32 helper routines.
4 * Copyright (c) 2010-2014 Michael Walle <michael@walle.cc>
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.
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.
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/>.
20 #include "qemu/osdep.h"
22 #include "qemu/host-utils.h"
23 #include "sysemu/sysemu.h"
24 #include "exec/semihost.h"
27 int lm32_cpu_handle_mmu_fault(CPUState
*cs
, vaddr address
, int rw
,
30 LM32CPU
*cpu
= LM32_CPU(cs
);
31 CPULM32State
*env
= &cpu
->env
;
34 address
&= TARGET_PAGE_MASK
;
36 if (env
->flags
& LM32_FLAG_IGNORE_MSB
) {
37 tlb_set_page(cs
, address
, address
& 0x7fffffff, prot
, mmu_idx
,
40 tlb_set_page(cs
, address
, address
, prot
, mmu_idx
, TARGET_PAGE_SIZE
);
46 hwaddr
lm32_cpu_get_phys_page_debug(CPUState
*cs
, vaddr addr
)
48 LM32CPU
*cpu
= LM32_CPU(cs
);
50 addr
&= TARGET_PAGE_MASK
;
51 if (cpu
->env
.flags
& LM32_FLAG_IGNORE_MSB
) {
52 return addr
& 0x7fffffff;
58 void lm32_breakpoint_insert(CPULM32State
*env
, int idx
, target_ulong address
)
60 LM32CPU
*cpu
= lm32_env_get_cpu(env
);
62 cpu_breakpoint_insert(CPU(cpu
), address
, BP_CPU
,
63 &env
->cpu_breakpoint
[idx
]);
66 void lm32_breakpoint_remove(CPULM32State
*env
, int idx
)
68 LM32CPU
*cpu
= lm32_env_get_cpu(env
);
70 if (!env
->cpu_breakpoint
[idx
]) {
74 cpu_breakpoint_remove_by_ref(CPU(cpu
), env
->cpu_breakpoint
[idx
]);
75 env
->cpu_breakpoint
[idx
] = NULL
;
78 void lm32_watchpoint_insert(CPULM32State
*env
, int idx
, target_ulong address
,
81 LM32CPU
*cpu
= lm32_env_get_cpu(env
);
85 case LM32_WP_DISABLED
:
89 flags
= BP_CPU
| BP_STOP_BEFORE_ACCESS
| BP_MEM_READ
;
92 flags
= BP_CPU
| BP_STOP_BEFORE_ACCESS
| BP_MEM_WRITE
;
94 case LM32_WP_READ_WRITE
:
95 flags
= BP_CPU
| BP_STOP_BEFORE_ACCESS
| BP_MEM_ACCESS
;
100 cpu_watchpoint_insert(CPU(cpu
), address
, 1, flags
,
101 &env
->cpu_watchpoint
[idx
]);
105 void lm32_watchpoint_remove(CPULM32State
*env
, int idx
)
107 LM32CPU
*cpu
= lm32_env_get_cpu(env
);
109 if (!env
->cpu_watchpoint
[idx
]) {
113 cpu_watchpoint_remove_by_ref(CPU(cpu
), env
->cpu_watchpoint
[idx
]);
114 env
->cpu_watchpoint
[idx
] = NULL
;
117 static bool check_watchpoints(CPULM32State
*env
)
119 LM32CPU
*cpu
= lm32_env_get_cpu(env
);
122 for (i
= 0; i
< cpu
->num_watchpoints
; i
++) {
123 if (env
->cpu_watchpoint
[i
] &&
124 env
->cpu_watchpoint
[i
]->flags
& BP_WATCHPOINT_HIT
) {
131 void lm32_debug_excp_handler(CPUState
*cs
)
133 LM32CPU
*cpu
= LM32_CPU(cs
);
134 CPULM32State
*env
= &cpu
->env
;
137 if (cs
->watchpoint_hit
) {
138 if (cs
->watchpoint_hit
->flags
& BP_CPU
) {
139 cs
->watchpoint_hit
= NULL
;
140 if (check_watchpoints(env
)) {
141 raise_exception(env
, EXCP_WATCHPOINT
);
143 cpu_resume_from_signal(cs
, NULL
);
147 QTAILQ_FOREACH(bp
, &cs
->breakpoints
, entry
) {
148 if (bp
->pc
== env
->pc
) {
149 if (bp
->flags
& BP_CPU
) {
150 raise_exception(env
, EXCP_BREAKPOINT
);
158 void lm32_cpu_do_interrupt(CPUState
*cs
)
160 LM32CPU
*cpu
= LM32_CPU(cs
);
161 CPULM32State
*env
= &cpu
->env
;
163 qemu_log_mask(CPU_LOG_INT
,
164 "exception at pc=%x type=%x\n", env
->pc
, cs
->exception_index
);
166 switch (cs
->exception_index
) {
167 case EXCP_SYSTEMCALL
:
168 if (unlikely(semihosting_enabled())) {
169 /* do_semicall() returns true if call was handled. Otherwise
170 * do the normal exception handling. */
171 if (lm32_cpu_do_semihosting(cs
)) {
177 case EXCP_INSN_BUS_ERROR
:
178 case EXCP_DATA_BUS_ERROR
:
179 case EXCP_DIVIDE_BY_ZERO
:
181 /* non-debug exceptions */
182 env
->regs
[R_EA
] = env
->pc
;
183 env
->ie
|= (env
->ie
& IE_IE
) ? IE_EIE
: 0;
185 if (env
->dc
& DC_RE
) {
186 env
->pc
= env
->deba
+ (cs
->exception_index
* 32);
188 env
->pc
= env
->eba
+ (cs
->exception_index
* 32);
190 log_cpu_state_mask(CPU_LOG_INT
, cs
, 0);
192 case EXCP_BREAKPOINT
:
193 case EXCP_WATCHPOINT
:
194 /* debug exceptions */
195 env
->regs
[R_BA
] = env
->pc
;
196 env
->ie
|= (env
->ie
& IE_IE
) ? IE_BIE
: 0;
198 env
->pc
= env
->deba
+ (cs
->exception_index
* 32);
199 log_cpu_state_mask(CPU_LOG_INT
, cs
, 0);
202 cpu_abort(cs
, "unhandled exception type=%d\n",
203 cs
->exception_index
);
208 bool lm32_cpu_exec_interrupt(CPUState
*cs
, int interrupt_request
)
210 LM32CPU
*cpu
= LM32_CPU(cs
);
211 CPULM32State
*env
= &cpu
->env
;
213 if ((interrupt_request
& CPU_INTERRUPT_HARD
) && (env
->ie
& IE_IE
)) {
214 cs
->exception_index
= EXCP_IRQ
;
215 lm32_cpu_do_interrupt(cs
);
221 LM32CPU
*cpu_lm32_init(const char *cpu_model
)
223 return LM32_CPU(cpu_generic_init(TYPE_LM32_CPU
, cpu_model
));
226 /* Some soc ignores the MSB on the address bus. Thus creating a shadow memory
227 * area. As a general rule, 0x00000000-0x7fffffff is cached, whereas
228 * 0x80000000-0xffffffff is not cached and used to access IO devices. */
229 void cpu_lm32_set_phys_msb_ignore(CPULM32State
*env
, int value
)
232 env
->flags
|= LM32_FLAG_IGNORE_MSB
;
234 env
->flags
&= ~LM32_FLAG_IGNORE_MSB
;