xen/xen_platform: QOM casting sweep
[qemu.git] / target-moxie / helper.c
blobea0788fcea9da06d948eb25102c59dad151981a6
1 /*
2 * Moxie helper routines.
4 * Copyright (c) 2008, 2009, 2010, 2013 Anthony Green
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 General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <stdio.h>
21 #include <string.h>
22 #include <assert.h>
24 #include "config.h"
25 #include "cpu.h"
26 #include "mmu.h"
27 #include "exec/exec-all.h"
28 #include "qemu/host-utils.h"
29 #include "helper.h"
31 #define MMUSUFFIX _mmu
33 #define SHIFT 0
34 #include "exec/softmmu_template.h"
36 #define SHIFT 1
37 #include "exec/softmmu_template.h"
39 #define SHIFT 2
40 #include "exec/softmmu_template.h"
42 #define SHIFT 3
43 #include "exec/softmmu_template.h"
45 /* Try to fill the TLB and return an exception if error. If retaddr is
46 NULL, it means that the function was called in C code (i.e. not
47 from generated code or from helper.c) */
48 void tlb_fill(CPUMoxieState *env, target_ulong addr, int is_write, int mmu_idx,
49 uintptr_t retaddr)
51 int ret;
53 ret = cpu_moxie_handle_mmu_fault(env, addr, is_write, mmu_idx);
54 if (unlikely(ret)) {
55 if (retaddr) {
56 cpu_restore_state(env, retaddr);
59 cpu_loop_exit(env);
62 void helper_raise_exception(CPUMoxieState *env, int ex)
64 env->exception_index = ex;
65 /* Stash the exception type. */
66 env->sregs[2] = ex;
67 /* Stash the address where the exception occurred. */
68 cpu_restore_state(env, GETPC());
69 env->sregs[5] = env->pc;
70 /* Jump the the exception handline routine. */
71 env->pc = env->sregs[1];
72 cpu_loop_exit(env);
75 uint32_t helper_div(CPUMoxieState *env, uint32_t a, uint32_t b)
77 if (unlikely(b == 0)) {
78 helper_raise_exception(env, MOXIE_EX_DIV0);
79 return 0;
81 if (unlikely(a == INT_MIN && b == -1)) {
82 return INT_MIN;
85 return (int32_t)a / (int32_t)b;
88 uint32_t helper_udiv(CPUMoxieState *env, uint32_t a, uint32_t b)
90 if (unlikely(b == 0)) {
91 helper_raise_exception(env, MOXIE_EX_DIV0);
92 return 0;
94 return a / b;
97 void helper_debug(CPUMoxieState *env)
99 env->exception_index = EXCP_DEBUG;
100 cpu_loop_exit(env);
103 #if defined(CONFIG_USER_ONLY)
105 void moxie_cpu_do_interrupt(CPUState *env)
107 env->exception_index = -1;
110 int cpu_moxie_handle_mmu_fault(CPUMoxieState *env, target_ulong address,
111 int rw, int mmu_idx)
113 MoxieCPU *cpu = moxie_env_get_cpu(env);
115 env->exception_index = 0xaa;
116 env->debug1 = address;
117 cpu_dump_state(CPU(cpu), stderr, fprintf, 0);
118 return 1;
121 hwaddr cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
123 return addr;
126 #else /* !CONFIG_USER_ONLY */
128 int cpu_moxie_handle_mmu_fault(CPUMoxieState *env, target_ulong address,
129 int rw, int mmu_idx)
131 MoxieMMUResult res;
132 int prot, miss;
133 target_ulong phy;
134 int r = 1;
136 address &= TARGET_PAGE_MASK;
137 prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
138 miss = moxie_mmu_translate(&res, env, address, rw, mmu_idx);
139 if (miss) {
140 /* handle the miss. */
141 phy = 0;
142 env->exception_index = MOXIE_EX_MMU_MISS;
143 } else {
144 phy = res.phy;
145 r = 0;
147 tlb_set_page(env, address, phy, prot, mmu_idx, TARGET_PAGE_SIZE);
148 return r;
152 void moxie_cpu_do_interrupt(CPUState *cs)
154 MoxieCPU *cpu = MOXIE_CPU(cs);
155 CPUMoxieState *env = &cpu->env;
157 switch (env->exception_index) {
158 case MOXIE_EX_BREAK:
159 break;
160 default:
161 break;
165 hwaddr cpu_get_phys_page_debug(CPUMoxieState *env, target_ulong addr)
167 uint32_t phy = addr;
168 MoxieMMUResult res;
169 int miss;
170 miss = moxie_mmu_translate(&res, env, addr, 0, 0);
171 if (!miss) {
172 phy = res.phy;
174 return phy;
176 #endif