helper.c 6.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 *  CRIS helper routines.
 *
 *  Copyright (c) 2007 AXIS Communications AB
 *  Written by Edgar E. Iglesias.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * 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 21 22
 */

#include "cpu.h"
#include "mmu.h"
T
ths 已提交
23
#include "host-utils.h"
24

25 26 27 28 29 30

//#define CRIS_HELPER_DEBUG


#ifdef CRIS_HELPER_DEBUG
#define D(x) x
31
#define D_LOG(...) qemu_log(__VA__ARGS__)
32
#else
33
#define D(x)
34 35
#define D_LOG(...) do { } while (0)
#endif
36

37 38
#if defined(CONFIG_USER_ONLY)

39
void do_interrupt (CPUCRISState *env)
40
{
41 42
	env->exception_index = -1;
	env->pregs[PR_ERP] = env->pc;
43 44
}

45
int cpu_cris_handle_mmu_fault(CPUCRISState * env, target_ulong address, int rw,
46
                              int mmu_idx)
47
{
48
	env->exception_index = 0xaa;
49
	env->pregs[PR_EDA] = address;
50 51
	cpu_dump_state(env, stderr, fprintf, 0);
	return 1;
52 53 54 55
}

#else /* !CONFIG_USER_ONLY */

56

57
static void cris_shift_ccs(CPUCRISState *env)
58 59 60 61
{
	uint32_t ccs;
	/* Apply the ccs shift.  */
	ccs = env->pregs[PR_CCS];
E
edgar_igl 已提交
62
	ccs = ((ccs & 0xc0000000) | ((ccs << 12) >> 2)) & ~0x3ff;
63 64 65
	env->pregs[PR_CCS] = ccs;
}

66
int cpu_cris_handle_mmu_fault (CPUCRISState *env, target_ulong address, int rw,
67
                               int mmu_idx)
68
{
69
	struct cris_mmu_result res;
70
	int prot, miss;
71
	int r = -1;
72 73
	target_ulong phy;

E
edgar_igl 已提交
74
	D(printf ("%s addr=%x pc=%x rw=%x\n", __func__, address, env->pc, rw));
75
	miss = cris_mmu_translate(&res, env, address & TARGET_PAGE_MASK,
76
				  rw, mmu_idx, 0);
77 78
	if (miss)
	{
E
edgar_igl 已提交
79
		if (env->exception_index == EXCP_BUSFAULT)
80
			cpu_abort(env,
E
edgar_igl 已提交
81
				  "CRIS: Illegal recursive bus fault."
82 83
				 "addr=%x rw=%d\n",
				 address, rw);
E
edgar_igl 已提交
84

85
		env->pregs[PR_EDA] = address;
E
edgar_igl 已提交
86
		env->exception_index = EXCP_BUSFAULT;
87 88
		env->fault_vector = res.bf_vec;
		r = 1;
89 90 91
	}
	else
	{
92 93 94 95 96
		/*
		 * Mask off the cache selection bit. The ETRAX busses do not
		 * see the top bit.
		 */
		phy = res.phy & ~0x80000000;
E
edgar_igl 已提交
97
		prot = res.prot;
P
Paul Brook 已提交
98
		tlb_set_page(env, address & TARGET_PAGE_MASK, phy,
99
                             prot, mmu_idx, TARGET_PAGE_SIZE);
P
Paul Brook 已提交
100
                r = 0;
101
	}
E
edgar_igl 已提交
102
	if (r > 0)
103 104 105
            D_LOG("%s returns %d irqreq=%x addr=%x phy=%x vec=%x pc=%x\n",
                  __func__, r, env->interrupt_request, address, res.phy,
                  res.bf_vec, env->pc);
106
	return r;
107 108
}

109
static void do_interruptv10(CPUCRISState *env)
110 111 112 113 114 115 116 117 118 119 120 121 122 123
{
	int ex_vec = -1;

	D_LOG( "exception index=%d interrupt_req=%d\n",
		   env->exception_index,
		   env->interrupt_request);

	assert(!(env->pregs[PR_CCS] & PFIX_FLAG));
	switch (env->exception_index)
	{
		case EXCP_BREAK:
			/* These exceptions are genereated by the core itself.
			   ERP should point to the insn following the brk.  */
			ex_vec = env->trap_vector;
124
			env->pregs[PRV10_BRP] = env->pc;
125 126 127 128 129
			break;

		case EXCP_NMI:
			/* NMI is hardwired to vector zero.  */
			ex_vec = 0;
130
			env->pregs[PR_CCS] &= ~M_FLAG_V10;
131
			env->pregs[PRV10_BRP] = env->pc;
132 133 134
			break;

		case EXCP_BUSFAULT:
135
                        cpu_abort(env, "Unhandled busfault");
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
			break;

		default:
			/* The interrupt controller gives us the vector.  */
			ex_vec = env->interrupt_vector;
			/* Normal interrupts are taken between
			   TB's.  env->pc is valid here.  */
			env->pregs[PR_ERP] = env->pc;
			break;
	}

	if (env->pregs[PR_CCS] & U_FLAG) {
		/* Swap stack pointers.  */
		env->pregs[PR_USP] = env->regs[R_SP];
		env->regs[R_SP] = env->ksp;
	}

	/* Now that we are in kernel mode, load the handlers address.  */
154
        env->pc = cpu_ldl_code(env, env->pregs[PR_EBP] + ex_vec * 4);
155
	env->locked_irq = 1;
156
	env->pregs[PR_CCS] |= F_FLAG_V10; /* set F.  */
157 158 159 160 161 162 163 164

	qemu_log_mask(CPU_LOG_INT, "%s isr=%x vec=%x ccs=%x pid=%d erp=%x\n", 
		      __func__, env->pc, ex_vec, 
		      env->pregs[PR_CCS],
		      env->pregs[PR_PID], 
		      env->pregs[PR_ERP]);
}

165
void do_interrupt(CPUCRISState *env)
166
{
167
	int ex_vec = -1;
168

169 170 171
	if (env->pregs[PR_VR] < 32)
		return do_interruptv10(env);

172
	D_LOG( "exception index=%d interrupt_req=%d\n",
E
edgar_igl 已提交
173
		   env->exception_index,
174
		   env->interrupt_request);
175 176 177 178

	switch (env->exception_index)
	{
		case EXCP_BREAK:
179 180 181
			/* These exceptions are genereated by the core itself.
			   ERP should point to the insn following the brk.  */
			ex_vec = env->trap_vector;
182
			env->pregs[PR_ERP] = env->pc;
183
			break;
184

E
edgar_igl 已提交
185 186 187
		case EXCP_NMI:
			/* NMI is hardwired to vector zero.  */
			ex_vec = 0;
188
			env->pregs[PR_CCS] &= ~M_FLAG_V32;
E
edgar_igl 已提交
189 190 191 192
			env->pregs[PR_NRP] = env->pc;
			break;

		case EXCP_BUSFAULT:
193
			ex_vec = env->fault_vector;
E
edgar_igl 已提交
194
			env->pregs[PR_ERP] = env->pc;
195 196 197
			break;

		default:
E
edgar_igl 已提交
198
			/* The interrupt controller gives us the vector.  */
E
edgar_igl 已提交
199 200 201 202 203 204 205
			ex_vec = env->interrupt_vector;
			/* Normal interrupts are taken between
			   TB's.  env->pc is valid here.  */
			env->pregs[PR_ERP] = env->pc;
			break;
	}

206 207 208
	/* Fill in the IDX field.  */
	env->pregs[PR_EXS] = (ex_vec & 0xff) << 8;

209
	if (env->dslot) {
210
		D_LOG("excp isr=%x PC=%x ds=%d SP=%x"
211 212
			  " ERP=%x pid=%x ccs=%x cc=%d %x\n",
			  ex_vec, env->pc, env->dslot,
E
edgar_igl 已提交
213
			  env->regs[R_SP],
E
edgar_igl 已提交
214 215
			  env->pregs[PR_ERP], env->pregs[PR_PID],
			  env->pregs[PR_CCS],
216
			  env->cc_op, env->cc_mask);
217 218 219 220 221
		/* We loose the btarget, btaken state here so rexec the
		   branch.  */
		env->pregs[PR_ERP] -= env->dslot;
		/* Exception starts with dslot cleared.  */
		env->dslot = 0;
222
	}
E
edgar_igl 已提交
223 224 225 226 227 228 229 230
	
	if (env->pregs[PR_CCS] & U_FLAG) {
		/* Swap stack pointers.  */
		env->pregs[PR_USP] = env->regs[R_SP];
		env->regs[R_SP] = env->ksp;
	}

	/* Apply the CRIS CCS shift. Clears U if set.  */
231
	cris_shift_ccs(env);
232

233 234 235
	/* Now that we are in kernel mode, load the handlers address.
	   This load may not fault, real hw leaves that behaviour as
	   undefined.  */
236
        env->pc = cpu_ldl_code(env, env->pregs[PR_EBP] + ex_vec * 4);
237

238 239 240 241
	/* Clear the excption_index to avoid spurios hw_aborts for recursive
	   bus faults.  */
	env->exception_index = -1;

242 243
	D_LOG("%s isr=%x vec=%x ccs=%x pid=%d erp=%x\n",
		   __func__, env->pc, ex_vec,
E
edgar_igl 已提交
244 245
		   env->pregs[PR_CCS],
		   env->pregs[PR_PID], 
246
		   env->pregs[PR_ERP]);
247 248
}

249
target_phys_addr_t cpu_get_phys_page_debug(CPUCRISState * env, target_ulong addr)
250 251
{
	uint32_t phy = addr;
252
	struct cris_mmu_result res;
253
	int miss;
254

255
	miss = cris_mmu_translate(&res, env, addr, 0, 0, 1);
256 257
	/* If D TLB misses, try I TLB.  */
	if (miss) {
258
		miss = cris_mmu_translate(&res, env, addr, 2, 0, 1);
259 260
	}

261 262
	if (!miss)
		phy = res.phy;
263
	D(fprintf(stderr, "%s %x -> %x\n", __func__, addr, phy));
264 265 266
	return phy;
}
#endif