Statistics
| Branch: | Revision:

root / target-lm32 / helper.c @ 54976b75

History | View | Annotate | Download (6.2 kB)

1 17c0fa3d Michael Walle
/*
2 17c0fa3d Michael Walle
 *  LatticeMico32 helper routines.
3 17c0fa3d Michael Walle
 *
4 17c0fa3d Michael Walle
 *  Copyright (c) 2010 Michael Walle <michael@walle.cc>
5 17c0fa3d Michael Walle
 *
6 17c0fa3d Michael Walle
 * This library is free software; you can redistribute it and/or
7 17c0fa3d Michael Walle
 * modify it under the terms of the GNU Lesser General Public
8 17c0fa3d Michael Walle
 * License as published by the Free Software Foundation; either
9 17c0fa3d Michael Walle
 * version 2 of the License, or (at your option) any later version.
10 17c0fa3d Michael Walle
 *
11 17c0fa3d Michael Walle
 * This library is distributed in the hope that it will be useful,
12 17c0fa3d Michael Walle
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 17c0fa3d Michael Walle
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 17c0fa3d Michael Walle
 * Lesser General Public License for more details.
15 17c0fa3d Michael Walle
 *
16 17c0fa3d Michael Walle
 * You should have received a copy of the GNU Lesser General Public
17 17c0fa3d Michael Walle
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 17c0fa3d Michael Walle
 */
19 17c0fa3d Michael Walle
20 17c0fa3d Michael Walle
#include "cpu.h"
21 1de7afc9 Paolo Bonzini
#include "qemu/host-utils.h"
22 17c0fa3d Michael Walle
23 6393c08d Andreas Färber
int cpu_lm32_handle_mmu_fault(CPULM32State *env, target_ulong address, int rw,
24 97b348e7 Blue Swirl
                              int mmu_idx)
25 17c0fa3d Michael Walle
{
26 17c0fa3d Michael Walle
    int prot;
27 17c0fa3d Michael Walle
28 17c0fa3d Michael Walle
    address &= TARGET_PAGE_MASK;
29 17c0fa3d Michael Walle
    prot = PAGE_BITS;
30 17c0fa3d Michael Walle
    if (env->flags & LM32_FLAG_IGNORE_MSB) {
31 17c0fa3d Michael Walle
        tlb_set_page(env, address, address & 0x7fffffff, prot, mmu_idx,
32 17c0fa3d Michael Walle
                TARGET_PAGE_SIZE);
33 17c0fa3d Michael Walle
    } else {
34 17c0fa3d Michael Walle
        tlb_set_page(env, address, address, prot, mmu_idx, TARGET_PAGE_SIZE);
35 17c0fa3d Michael Walle
    }
36 17c0fa3d Michael Walle
37 17c0fa3d Michael Walle
    return 0;
38 17c0fa3d Michael Walle
}
39 17c0fa3d Michael Walle
40 a8170e5e Avi Kivity
hwaddr cpu_get_phys_page_debug(CPULM32State *env, target_ulong addr)
41 17c0fa3d Michael Walle
{
42 b92e062a Michael Walle
    addr &= TARGET_PAGE_MASK;
43 b92e062a Michael Walle
    if (env->flags & LM32_FLAG_IGNORE_MSB) {
44 b92e062a Michael Walle
        return addr & 0x7fffffff;
45 b92e062a Michael Walle
    } else {
46 b92e062a Michael Walle
        return addr;
47 b92e062a Michael Walle
    }
48 17c0fa3d Michael Walle
}
49 17c0fa3d Michael Walle
50 97a8ea5a Andreas Färber
void lm32_cpu_do_interrupt(CPUState *cs)
51 17c0fa3d Michael Walle
{
52 97a8ea5a Andreas Färber
    LM32CPU *cpu = LM32_CPU(cs);
53 97a8ea5a Andreas Färber
    CPULM32State *env = &cpu->env;
54 97a8ea5a Andreas Färber
55 17c0fa3d Michael Walle
    qemu_log_mask(CPU_LOG_INT,
56 17c0fa3d Michael Walle
            "exception at pc=%x type=%x\n", env->pc, env->exception_index);
57 17c0fa3d Michael Walle
58 17c0fa3d Michael Walle
    switch (env->exception_index) {
59 17c0fa3d Michael Walle
    case EXCP_INSN_BUS_ERROR:
60 17c0fa3d Michael Walle
    case EXCP_DATA_BUS_ERROR:
61 17c0fa3d Michael Walle
    case EXCP_DIVIDE_BY_ZERO:
62 17c0fa3d Michael Walle
    case EXCP_IRQ:
63 17c0fa3d Michael Walle
    case EXCP_SYSTEMCALL:
64 17c0fa3d Michael Walle
        /* non-debug exceptions */
65 17c0fa3d Michael Walle
        env->regs[R_EA] = env->pc;
66 17c0fa3d Michael Walle
        env->ie |= (env->ie & IE_IE) ? IE_EIE : 0;
67 17c0fa3d Michael Walle
        env->ie &= ~IE_IE;
68 17c0fa3d Michael Walle
        if (env->dc & DC_RE) {
69 17c0fa3d Michael Walle
            env->pc = env->deba + (env->exception_index * 32);
70 17c0fa3d Michael Walle
        } else {
71 17c0fa3d Michael Walle
            env->pc = env->eba + (env->exception_index * 32);
72 17c0fa3d Michael Walle
        }
73 17c0fa3d Michael Walle
        log_cpu_state_mask(CPU_LOG_INT, env, 0);
74 17c0fa3d Michael Walle
        break;
75 17c0fa3d Michael Walle
    case EXCP_BREAKPOINT:
76 17c0fa3d Michael Walle
    case EXCP_WATCHPOINT:
77 17c0fa3d Michael Walle
        /* debug exceptions */
78 17c0fa3d Michael Walle
        env->regs[R_BA] = env->pc;
79 17c0fa3d Michael Walle
        env->ie |= (env->ie & IE_IE) ? IE_BIE : 0;
80 17c0fa3d Michael Walle
        env->ie &= ~IE_IE;
81 ecbe1de8 Michael Walle
        env->pc = env->deba + (env->exception_index * 32);
82 17c0fa3d Michael Walle
        log_cpu_state_mask(CPU_LOG_INT, env, 0);
83 17c0fa3d Michael Walle
        break;
84 17c0fa3d Michael Walle
    default:
85 17c0fa3d Michael Walle
        cpu_abort(env, "unhandled exception type=%d\n",
86 17c0fa3d Michael Walle
                  env->exception_index);
87 17c0fa3d Michael Walle
        break;
88 17c0fa3d Michael Walle
    }
89 17c0fa3d Michael Walle
}
90 17c0fa3d Michael Walle
91 17c0fa3d Michael Walle
typedef struct {
92 17c0fa3d Michael Walle
    const char *name;
93 17c0fa3d Michael Walle
    uint32_t revision;
94 17c0fa3d Michael Walle
    uint8_t num_interrupts;
95 17c0fa3d Michael Walle
    uint8_t num_breakpoints;
96 17c0fa3d Michael Walle
    uint8_t num_watchpoints;
97 17c0fa3d Michael Walle
    uint32_t features;
98 17c0fa3d Michael Walle
} LM32Def;
99 17c0fa3d Michael Walle
100 17c0fa3d Michael Walle
static const LM32Def lm32_defs[] = {
101 17c0fa3d Michael Walle
    {
102 17c0fa3d Michael Walle
        .name = "lm32-basic",
103 17c0fa3d Michael Walle
        .revision = 3,
104 17c0fa3d Michael Walle
        .num_interrupts = 32,
105 17c0fa3d Michael Walle
        .num_breakpoints = 4,
106 17c0fa3d Michael Walle
        .num_watchpoints = 4,
107 17c0fa3d Michael Walle
        .features = (LM32_FEATURE_SHIFT
108 17c0fa3d Michael Walle
                     | LM32_FEATURE_SIGN_EXTEND
109 17c0fa3d Michael Walle
                     | LM32_FEATURE_CYCLE_COUNT),
110 17c0fa3d Michael Walle
    },
111 17c0fa3d Michael Walle
    {
112 17c0fa3d Michael Walle
        .name = "lm32-standard",
113 17c0fa3d Michael Walle
        .revision = 3,
114 17c0fa3d Michael Walle
        .num_interrupts = 32,
115 17c0fa3d Michael Walle
        .num_breakpoints = 4,
116 17c0fa3d Michael Walle
        .num_watchpoints = 4,
117 17c0fa3d Michael Walle
        .features = (LM32_FEATURE_MULTIPLY
118 17c0fa3d Michael Walle
                     | LM32_FEATURE_DIVIDE
119 17c0fa3d Michael Walle
                     | LM32_FEATURE_SHIFT
120 17c0fa3d Michael Walle
                     | LM32_FEATURE_SIGN_EXTEND
121 17c0fa3d Michael Walle
                     | LM32_FEATURE_I_CACHE
122 17c0fa3d Michael Walle
                     | LM32_FEATURE_CYCLE_COUNT),
123 17c0fa3d Michael Walle
    },
124 17c0fa3d Michael Walle
    {
125 17c0fa3d Michael Walle
        .name = "lm32-full",
126 17c0fa3d Michael Walle
        .revision = 3,
127 17c0fa3d Michael Walle
        .num_interrupts = 32,
128 17c0fa3d Michael Walle
        .num_breakpoints = 4,
129 17c0fa3d Michael Walle
        .num_watchpoints = 4,
130 17c0fa3d Michael Walle
        .features = (LM32_FEATURE_MULTIPLY
131 17c0fa3d Michael Walle
                     | LM32_FEATURE_DIVIDE
132 17c0fa3d Michael Walle
                     | LM32_FEATURE_SHIFT
133 17c0fa3d Michael Walle
                     | LM32_FEATURE_SIGN_EXTEND
134 17c0fa3d Michael Walle
                     | LM32_FEATURE_I_CACHE
135 17c0fa3d Michael Walle
                     | LM32_FEATURE_D_CACHE
136 17c0fa3d Michael Walle
                     | LM32_FEATURE_CYCLE_COUNT),
137 17c0fa3d Michael Walle
    }
138 17c0fa3d Michael Walle
};
139 17c0fa3d Michael Walle
140 17c0fa3d Michael Walle
void cpu_lm32_list(FILE *f, fprintf_function cpu_fprintf)
141 17c0fa3d Michael Walle
{
142 17c0fa3d Michael Walle
    int i;
143 17c0fa3d Michael Walle
144 17c0fa3d Michael Walle
    cpu_fprintf(f, "Available CPUs:\n");
145 17c0fa3d Michael Walle
    for (i = 0; i < ARRAY_SIZE(lm32_defs); i++) {
146 17c0fa3d Michael Walle
        cpu_fprintf(f, "  %s\n", lm32_defs[i].name);
147 17c0fa3d Michael Walle
    }
148 17c0fa3d Michael Walle
}
149 17c0fa3d Michael Walle
150 17c0fa3d Michael Walle
static const LM32Def *cpu_lm32_find_by_name(const char *name)
151 17c0fa3d Michael Walle
{
152 17c0fa3d Michael Walle
    int i;
153 17c0fa3d Michael Walle
154 17c0fa3d Michael Walle
    for (i = 0; i < ARRAY_SIZE(lm32_defs); i++) {
155 17c0fa3d Michael Walle
        if (strcasecmp(name, lm32_defs[i].name) == 0) {
156 17c0fa3d Michael Walle
            return &lm32_defs[i];
157 17c0fa3d Michael Walle
        }
158 17c0fa3d Michael Walle
    }
159 17c0fa3d Michael Walle
160 17c0fa3d Michael Walle
    return NULL;
161 17c0fa3d Michael Walle
}
162 17c0fa3d Michael Walle
163 17c0fa3d Michael Walle
static uint32_t cfg_by_def(const LM32Def *def)
164 17c0fa3d Michael Walle
{
165 17c0fa3d Michael Walle
    uint32_t cfg = 0;
166 17c0fa3d Michael Walle
167 17c0fa3d Michael Walle
    if (def->features & LM32_FEATURE_MULTIPLY) {
168 17c0fa3d Michael Walle
        cfg |= CFG_M;
169 17c0fa3d Michael Walle
    }
170 17c0fa3d Michael Walle
171 17c0fa3d Michael Walle
    if (def->features & LM32_FEATURE_DIVIDE) {
172 17c0fa3d Michael Walle
        cfg |= CFG_D;
173 17c0fa3d Michael Walle
    }
174 17c0fa3d Michael Walle
175 17c0fa3d Michael Walle
    if (def->features & LM32_FEATURE_SHIFT) {
176 17c0fa3d Michael Walle
        cfg |= CFG_S;
177 17c0fa3d Michael Walle
    }
178 17c0fa3d Michael Walle
179 17c0fa3d Michael Walle
    if (def->features & LM32_FEATURE_SIGN_EXTEND) {
180 17c0fa3d Michael Walle
        cfg |= CFG_X;
181 17c0fa3d Michael Walle
    }
182 17c0fa3d Michael Walle
183 17c0fa3d Michael Walle
    if (def->features & LM32_FEATURE_I_CACHE) {
184 17c0fa3d Michael Walle
        cfg |= CFG_IC;
185 17c0fa3d Michael Walle
    }
186 17c0fa3d Michael Walle
187 17c0fa3d Michael Walle
    if (def->features & LM32_FEATURE_D_CACHE) {
188 17c0fa3d Michael Walle
        cfg |= CFG_DC;
189 17c0fa3d Michael Walle
    }
190 17c0fa3d Michael Walle
191 17c0fa3d Michael Walle
    if (def->features & LM32_FEATURE_CYCLE_COUNT) {
192 17c0fa3d Michael Walle
        cfg |= CFG_CC;
193 17c0fa3d Michael Walle
    }
194 17c0fa3d Michael Walle
195 17c0fa3d Michael Walle
    cfg |= (def->num_interrupts << CFG_INT_SHIFT);
196 17c0fa3d Michael Walle
    cfg |= (def->num_breakpoints << CFG_BP_SHIFT);
197 17c0fa3d Michael Walle
    cfg |= (def->num_watchpoints << CFG_WP_SHIFT);
198 17c0fa3d Michael Walle
    cfg |= (def->revision << CFG_REV_SHIFT);
199 17c0fa3d Michael Walle
200 17c0fa3d Michael Walle
    return cfg;
201 17c0fa3d Michael Walle
}
202 17c0fa3d Michael Walle
203 0347d689 Andreas Färber
LM32CPU *cpu_lm32_init(const char *cpu_model)
204 17c0fa3d Michael Walle
{
205 fc0ced2f Andreas Färber
    LM32CPU *cpu;
206 6393c08d Andreas Färber
    CPULM32State *env;
207 17c0fa3d Michael Walle
    const LM32Def *def;
208 17c0fa3d Michael Walle
209 17c0fa3d Michael Walle
    def = cpu_lm32_find_by_name(cpu_model);
210 17c0fa3d Michael Walle
    if (!def) {
211 17c0fa3d Michael Walle
        return NULL;
212 17c0fa3d Michael Walle
    }
213 17c0fa3d Michael Walle
214 fc0ced2f Andreas Färber
    cpu = LM32_CPU(object_new(TYPE_LM32_CPU));
215 fc0ced2f Andreas Färber
    env = &cpu->env;
216 17c0fa3d Michael Walle
217 17c0fa3d Michael Walle
    env->features = def->features;
218 17c0fa3d Michael Walle
    env->num_bps = def->num_breakpoints;
219 17c0fa3d Michael Walle
    env->num_wps = def->num_watchpoints;
220 17c0fa3d Michael Walle
    env->cfg = cfg_by_def(def);
221 17c0fa3d Michael Walle
222 9c23169e Andreas Färber
    object_property_set_bool(OBJECT(cpu), true, "realized", NULL);
223 9c23169e Andreas Färber
224 0347d689 Andreas Färber
    return cpu;
225 17c0fa3d Michael Walle
}
226 17c0fa3d Michael Walle
227 17c0fa3d Michael Walle
/* Some soc ignores the MSB on the address bus. Thus creating a shadow memory
228 17c0fa3d Michael Walle
 * area. As a general rule, 0x00000000-0x7fffffff is cached, whereas
229 17c0fa3d Michael Walle
 * 0x80000000-0xffffffff is not cached and used to access IO devices. */
230 6393c08d Andreas Färber
void cpu_lm32_set_phys_msb_ignore(CPULM32State *env, int value)
231 17c0fa3d Michael Walle
{
232 17c0fa3d Michael Walle
    if (value) {
233 17c0fa3d Michael Walle
        env->flags |= LM32_FLAG_IGNORE_MSB;
234 17c0fa3d Michael Walle
    } else {
235 17c0fa3d Michael Walle
        env->flags &= ~LM32_FLAG_IGNORE_MSB;
236 17c0fa3d Michael Walle
    }
237 17c0fa3d Michael Walle
}