2 * Altera Nios II helper routines.
4 * Copyright (c) 2012 Chris Wulff <crwulff@gmail.com>
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.1 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
18 * <http://www.gnu.org/licenses/lgpl-2.1.html>
21 #include "qemu/osdep.h"
24 #include "qemu/host-utils.h"
25 #include "exec/exec-all.h"
26 #include "exec/cpu_ldst.h"
28 #include "exec/helper-proto.h"
29 #include "semihosting/semihost.h"
32 void nios2_cpu_do_interrupt(CPUState
*cs
)
34 Nios2CPU
*cpu
= NIOS2_CPU(cs
);
35 CPUNios2State
*env
= &cpu
->env
;
37 switch (cs
->exception_index
) {
39 assert(env
->regs
[CR_STATUS
] & CR_STATUS_PIE
);
41 qemu_log_mask(CPU_LOG_INT
, "interrupt at pc=%x\n", env
->pc
);
43 env
->regs
[CR_ESTATUS
] = env
->regs
[CR_STATUS
];
44 env
->regs
[CR_STATUS
] |= CR_STATUS_IH
;
45 env
->regs
[CR_STATUS
] &= ~(CR_STATUS_PIE
| CR_STATUS_U
);
47 env
->regs
[CR_EXCEPTION
] &= ~(0x1F << 2);
48 env
->regs
[CR_EXCEPTION
] |= (cs
->exception_index
& 0x1F) << 2;
50 env
->regs
[R_EA
] = env
->pc
+ 4;
51 env
->pc
= cpu
->exception_addr
;
55 if ((env
->regs
[CR_STATUS
] & CR_STATUS_EH
) == 0) {
56 qemu_log_mask(CPU_LOG_INT
, "TLB MISS (fast) at pc=%x\n", env
->pc
);
59 /* Variation from the spec. Table 3-35 of the cpu reference shows
60 * estatus not being changed for TLB miss but this appears to
62 env
->regs
[CR_ESTATUS
] = env
->regs
[CR_STATUS
];
63 env
->regs
[CR_STATUS
] |= CR_STATUS_EH
;
64 env
->regs
[CR_STATUS
] &= ~(CR_STATUS_PIE
| CR_STATUS_U
);
66 env
->regs
[CR_EXCEPTION
] &= ~(0x1F << 2);
67 env
->regs
[CR_EXCEPTION
] |= (cs
->exception_index
& 0x1F) << 2;
69 env
->regs
[CR_TLBMISC
] &= ~CR_TLBMISC_DBL
;
70 env
->regs
[CR_TLBMISC
] |= CR_TLBMISC_WR
;
72 env
->regs
[R_EA
] = env
->pc
+ 4;
73 env
->pc
= cpu
->fast_tlb_miss_addr
;
75 qemu_log_mask(CPU_LOG_INT
, "TLB MISS (double) at pc=%x\n", env
->pc
);
78 env
->regs
[CR_STATUS
] |= CR_STATUS_EH
;
79 env
->regs
[CR_STATUS
] &= ~(CR_STATUS_PIE
| CR_STATUS_U
);
81 env
->regs
[CR_EXCEPTION
] &= ~(0x1F << 2);
82 env
->regs
[CR_EXCEPTION
] |= (cs
->exception_index
& 0x1F) << 2;
84 env
->regs
[CR_TLBMISC
] |= CR_TLBMISC_DBL
;
86 env
->pc
= cpu
->exception_addr
;
93 qemu_log_mask(CPU_LOG_INT
, "TLB PERM at pc=%x\n", env
->pc
);
95 env
->regs
[CR_ESTATUS
] = env
->regs
[CR_STATUS
];
96 env
->regs
[CR_STATUS
] |= CR_STATUS_EH
;
97 env
->regs
[CR_STATUS
] &= ~(CR_STATUS_PIE
| CR_STATUS_U
);
99 env
->regs
[CR_EXCEPTION
] &= ~(0x1F << 2);
100 env
->regs
[CR_EXCEPTION
] |= (cs
->exception_index
& 0x1F) << 2;
102 if ((env
->regs
[CR_STATUS
] & CR_STATUS_EH
) == 0) {
103 env
->regs
[CR_TLBMISC
] |= CR_TLBMISC_WR
;
106 env
->regs
[R_EA
] = env
->pc
+ 4;
107 env
->pc
= cpu
->exception_addr
;
113 qemu_log_mask(CPU_LOG_INT
, "SUPERVISOR exception at pc=%x\n", env
->pc
);
115 if ((env
->regs
[CR_STATUS
] & CR_STATUS_EH
) == 0) {
116 env
->regs
[CR_ESTATUS
] = env
->regs
[CR_STATUS
];
117 env
->regs
[R_EA
] = env
->pc
+ 4;
120 env
->regs
[CR_STATUS
] |= CR_STATUS_EH
;
121 env
->regs
[CR_STATUS
] &= ~(CR_STATUS_PIE
| CR_STATUS_U
);
123 env
->regs
[CR_EXCEPTION
] &= ~(0x1F << 2);
124 env
->regs
[CR_EXCEPTION
] |= (cs
->exception_index
& 0x1F) << 2;
126 env
->pc
= cpu
->exception_addr
;
131 qemu_log_mask(CPU_LOG_INT
, "TRAP exception at pc=%x\n", env
->pc
);
133 if ((env
->regs
[CR_STATUS
] & CR_STATUS_EH
) == 0) {
134 env
->regs
[CR_ESTATUS
] = env
->regs
[CR_STATUS
];
135 env
->regs
[R_EA
] = env
->pc
+ 4;
138 env
->regs
[CR_STATUS
] |= CR_STATUS_EH
;
139 env
->regs
[CR_STATUS
] &= ~(CR_STATUS_PIE
| CR_STATUS_U
);
141 env
->regs
[CR_EXCEPTION
] &= ~(0x1F << 2);
142 env
->regs
[CR_EXCEPTION
] |= (cs
->exception_index
& 0x1F) << 2;
144 env
->pc
= cpu
->exception_addr
;
148 qemu_log_mask(CPU_LOG_INT
, "BREAK exception at pc=%x\n", env
->pc
);
149 /* The semihosting instruction is "break 1". */
150 if (semihosting_enabled() &&
151 cpu_ldl_code(env
, env
->pc
) == 0x003da07a) {
152 qemu_log_mask(CPU_LOG_INT
, "Entering semihosting\n");
154 do_nios2_semihosting(env
);
158 if ((env
->regs
[CR_STATUS
] & CR_STATUS_EH
) == 0) {
159 env
->regs
[CR_BSTATUS
] = env
->regs
[CR_STATUS
];
160 env
->regs
[R_BA
] = env
->pc
+ 4;
163 env
->regs
[CR_STATUS
] |= CR_STATUS_EH
;
164 env
->regs
[CR_STATUS
] &= ~(CR_STATUS_PIE
| CR_STATUS_U
);
166 env
->regs
[CR_EXCEPTION
] &= ~(0x1F << 2);
167 env
->regs
[CR_EXCEPTION
] |= (cs
->exception_index
& 0x1F) << 2;
169 env
->pc
= cpu
->exception_addr
;
173 cpu_abort(cs
, "unhandled exception type=%d\n",
174 cs
->exception_index
);
179 hwaddr
nios2_cpu_get_phys_page_debug(CPUState
*cs
, vaddr addr
)
181 Nios2CPU
*cpu
= NIOS2_CPU(cs
);
182 CPUNios2State
*env
= &cpu
->env
;
183 target_ulong vaddr
, paddr
= 0;
187 if (cpu
->mmu_present
&& (addr
< 0xC0000000)) {
188 hit
= mmu_translate(env
, &lu
, addr
, 0, 0);
190 vaddr
= addr
& TARGET_PAGE_MASK
;
191 paddr
= lu
.paddr
+ vaddr
- lu
.vaddr
;
194 qemu_log("cpu_get_phys_page debug MISS: %#" PRIx64
"\n", addr
);
197 paddr
= addr
& TARGET_PAGE_MASK
;
203 void nios2_cpu_do_unaligned_access(CPUState
*cs
, vaddr addr
,
204 MMUAccessType access_type
,
205 int mmu_idx
, uintptr_t retaddr
)
207 Nios2CPU
*cpu
= NIOS2_CPU(cs
);
208 CPUNios2State
*env
= &cpu
->env
;
210 env
->regs
[CR_BADADDR
] = addr
;
211 env
->regs
[CR_EXCEPTION
] = EXCP_UNALIGN
<< 2;
212 helper_raise_exception(env
, EXCP_UNALIGN
);
215 bool nios2_cpu_tlb_fill(CPUState
*cs
, vaddr address
, int size
,
216 MMUAccessType access_type
, int mmu_idx
,
217 bool probe
, uintptr_t retaddr
)
219 Nios2CPU
*cpu
= NIOS2_CPU(cs
);
220 CPUNios2State
*env
= &cpu
->env
;
221 unsigned int excp
= EXCP_TLBD
;
222 target_ulong vaddr
, paddr
;
226 if (!cpu
->mmu_present
) {
228 address
&= TARGET_PAGE_MASK
;
229 tlb_set_page(cs
, address
, address
, PAGE_BITS
,
230 mmu_idx
, TARGET_PAGE_SIZE
);
234 if (MMU_SUPERVISOR_IDX
== mmu_idx
) {
235 if (address
>= 0xC0000000) {
236 /* Kernel physical page - TLB bypassed */
237 address
&= TARGET_PAGE_MASK
;
238 tlb_set_page(cs
, address
, address
, PAGE_BITS
,
239 mmu_idx
, TARGET_PAGE_SIZE
);
243 if (address
>= 0x80000000) {
244 /* Illegal access from user mode */
248 cs
->exception_index
= EXCP_SUPERA
;
249 env
->regs
[CR_BADADDR
] = address
;
250 cpu_loop_exit_restore(cs
, retaddr
);
255 hit
= mmu_translate(env
, &lu
, address
, access_type
, mmu_idx
);
257 vaddr
= address
& TARGET_PAGE_MASK
;
258 paddr
= lu
.paddr
+ vaddr
- lu
.vaddr
;
260 if (((access_type
== MMU_DATA_LOAD
) && (lu
.prot
& PAGE_READ
)) ||
261 ((access_type
== MMU_DATA_STORE
) && (lu
.prot
& PAGE_WRITE
)) ||
262 ((access_type
== MMU_INST_FETCH
) && (lu
.prot
& PAGE_EXEC
))) {
263 tlb_set_page(cs
, vaddr
, paddr
, lu
.prot
,
264 mmu_idx
, TARGET_PAGE_SIZE
);
268 /* Permission violation */
269 excp
= (access_type
== MMU_DATA_LOAD
? EXCP_TLBR
:
270 access_type
== MMU_DATA_STORE
? EXCP_TLBW
: EXCP_TLBX
);
277 if (access_type
== MMU_INST_FETCH
) {
278 env
->regs
[CR_TLBMISC
] &= ~CR_TLBMISC_D
;
280 env
->regs
[CR_TLBMISC
] |= CR_TLBMISC_D
;
282 env
->regs
[CR_PTEADDR
] &= CR_PTEADDR_PTBASE_MASK
;
283 env
->regs
[CR_PTEADDR
] |= (address
>> 10) & CR_PTEADDR_VPN_MASK
;
284 env
->mmu
.pteaddr_wr
= env
->regs
[CR_PTEADDR
];
286 cs
->exception_index
= excp
;
287 env
->regs
[CR_BADADDR
] = address
;
288 cpu_loop_exit_restore(cs
, retaddr
);