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"
27 #include "exec/helper-proto.h"
29 #if defined(CONFIG_USER_ONLY)
31 void nios2_cpu_do_interrupt(CPUState
*cs
)
33 Nios2CPU
*cpu
= NIOS2_CPU(cs
);
34 CPUNios2State
*env
= &cpu
->env
;
35 cs
->exception_index
= -1;
36 env
->regs
[R_EA
] = env
->regs
[R_PC
] + 4;
39 int nios2_cpu_handle_mmu_fault(CPUState
*cs
, vaddr address
, int size
,
42 cs
->exception_index
= 0xaa;
43 /* Page 0x1000 is kuser helper */
44 if (address
< 0x1000 || address
>= 0x2000) {
45 cpu_dump_state(cs
, stderr
, fprintf
, 0);
50 #else /* !CONFIG_USER_ONLY */
52 void nios2_cpu_do_interrupt(CPUState
*cs
)
54 Nios2CPU
*cpu
= NIOS2_CPU(cs
);
55 CPUNios2State
*env
= &cpu
->env
;
57 switch (cs
->exception_index
) {
59 assert(env
->regs
[CR_STATUS
] & CR_STATUS_PIE
);
61 qemu_log_mask(CPU_LOG_INT
, "interrupt at pc=%x\n", env
->regs
[R_PC
]);
63 env
->regs
[CR_ESTATUS
] = env
->regs
[CR_STATUS
];
64 env
->regs
[CR_STATUS
] |= CR_STATUS_IH
;
65 env
->regs
[CR_STATUS
] &= ~(CR_STATUS_PIE
| CR_STATUS_U
);
67 env
->regs
[CR_EXCEPTION
] &= ~(0x1F << 2);
68 env
->regs
[CR_EXCEPTION
] |= (cs
->exception_index
& 0x1F) << 2;
70 env
->regs
[R_EA
] = env
->regs
[R_PC
] + 4;
71 env
->regs
[R_PC
] = cpu
->exception_addr
;
75 if ((env
->regs
[CR_STATUS
] & CR_STATUS_EH
) == 0) {
76 qemu_log_mask(CPU_LOG_INT
, "TLB MISS (fast) at pc=%x\n",
80 /* Variation from the spec. Table 3-35 of the cpu reference shows
81 * estatus not being changed for TLB miss but this appears to
83 env
->regs
[CR_ESTATUS
] = env
->regs
[CR_STATUS
];
84 env
->regs
[CR_STATUS
] |= CR_STATUS_EH
;
85 env
->regs
[CR_STATUS
] &= ~(CR_STATUS_PIE
| CR_STATUS_U
);
87 env
->regs
[CR_EXCEPTION
] &= ~(0x1F << 2);
88 env
->regs
[CR_EXCEPTION
] |= (cs
->exception_index
& 0x1F) << 2;
90 env
->regs
[CR_TLBMISC
] &= ~CR_TLBMISC_DBL
;
91 env
->regs
[CR_TLBMISC
] |= CR_TLBMISC_WR
;
93 env
->regs
[R_EA
] = env
->regs
[R_PC
] + 4;
94 env
->regs
[R_PC
] = cpu
->fast_tlb_miss_addr
;
96 qemu_log_mask(CPU_LOG_INT
, "TLB MISS (double) at pc=%x\n",
100 env
->regs
[CR_STATUS
] |= CR_STATUS_EH
;
101 env
->regs
[CR_STATUS
] &= ~(CR_STATUS_PIE
| CR_STATUS_U
);
103 env
->regs
[CR_EXCEPTION
] &= ~(0x1F << 2);
104 env
->regs
[CR_EXCEPTION
] |= (cs
->exception_index
& 0x1F) << 2;
106 env
->regs
[CR_TLBMISC
] |= CR_TLBMISC_DBL
;
108 env
->regs
[R_PC
] = cpu
->exception_addr
;
115 qemu_log_mask(CPU_LOG_INT
, "TLB PERM at pc=%x\n", env
->regs
[R_PC
]);
117 env
->regs
[CR_ESTATUS
] = env
->regs
[CR_STATUS
];
118 env
->regs
[CR_STATUS
] |= CR_STATUS_EH
;
119 env
->regs
[CR_STATUS
] &= ~(CR_STATUS_PIE
| CR_STATUS_U
);
121 env
->regs
[CR_EXCEPTION
] &= ~(0x1F << 2);
122 env
->regs
[CR_EXCEPTION
] |= (cs
->exception_index
& 0x1F) << 2;
124 if ((env
->regs
[CR_STATUS
] & CR_STATUS_EH
) == 0) {
125 env
->regs
[CR_TLBMISC
] |= CR_TLBMISC_WR
;
128 env
->regs
[R_EA
] = env
->regs
[R_PC
] + 4;
129 env
->regs
[R_PC
] = cpu
->exception_addr
;
135 qemu_log_mask(CPU_LOG_INT
, "SUPERVISOR exception at pc=%x\n",
138 if ((env
->regs
[CR_STATUS
] & CR_STATUS_EH
) == 0) {
139 env
->regs
[CR_ESTATUS
] = env
->regs
[CR_STATUS
];
140 env
->regs
[R_EA
] = env
->regs
[R_PC
] + 4;
143 env
->regs
[CR_STATUS
] |= CR_STATUS_EH
;
144 env
->regs
[CR_STATUS
] &= ~(CR_STATUS_PIE
| CR_STATUS_U
);
146 env
->regs
[CR_EXCEPTION
] &= ~(0x1F << 2);
147 env
->regs
[CR_EXCEPTION
] |= (cs
->exception_index
& 0x1F) << 2;
149 env
->regs
[R_PC
] = cpu
->exception_addr
;
154 qemu_log_mask(CPU_LOG_INT
, "TRAP exception at pc=%x\n",
157 if ((env
->regs
[CR_STATUS
] & CR_STATUS_EH
) == 0) {
158 env
->regs
[CR_ESTATUS
] = env
->regs
[CR_STATUS
];
159 env
->regs
[R_EA
] = env
->regs
[R_PC
] + 4;
162 env
->regs
[CR_STATUS
] |= CR_STATUS_EH
;
163 env
->regs
[CR_STATUS
] &= ~(CR_STATUS_PIE
| CR_STATUS_U
);
165 env
->regs
[CR_EXCEPTION
] &= ~(0x1F << 2);
166 env
->regs
[CR_EXCEPTION
] |= (cs
->exception_index
& 0x1F) << 2;
168 env
->regs
[R_PC
] = cpu
->exception_addr
;
172 if ((env
->regs
[CR_STATUS
] & CR_STATUS_EH
) == 0) {
173 env
->regs
[CR_BSTATUS
] = env
->regs
[CR_STATUS
];
174 env
->regs
[R_BA
] = env
->regs
[R_PC
] + 4;
177 env
->regs
[CR_STATUS
] |= CR_STATUS_EH
;
178 env
->regs
[CR_STATUS
] &= ~(CR_STATUS_PIE
| CR_STATUS_U
);
180 env
->regs
[CR_EXCEPTION
] &= ~(0x1F << 2);
181 env
->regs
[CR_EXCEPTION
] |= (cs
->exception_index
& 0x1F) << 2;
183 env
->regs
[R_PC
] = cpu
->exception_addr
;
187 cpu_abort(cs
, "unhandled exception type=%d\n",
188 cs
->exception_index
);
193 static int cpu_nios2_handle_virtual_page(
194 CPUState
*cs
, target_ulong address
, int rw
, int mmu_idx
)
196 Nios2CPU
*cpu
= NIOS2_CPU(cs
);
197 CPUNios2State
*env
= &cpu
->env
;
198 target_ulong vaddr
, paddr
;
201 hit
= mmu_translate(env
, &lu
, address
, rw
, mmu_idx
);
203 vaddr
= address
& TARGET_PAGE_MASK
;
204 paddr
= lu
.paddr
+ vaddr
- lu
.vaddr
;
206 if (((rw
== 0) && (lu
.prot
& PAGE_READ
)) ||
207 ((rw
== 1) && (lu
.prot
& PAGE_WRITE
)) ||
208 ((rw
== 2) && (lu
.prot
& PAGE_EXEC
))) {
210 tlb_set_page(cs
, vaddr
, paddr
, lu
.prot
,
211 mmu_idx
, TARGET_PAGE_SIZE
);
214 /* Permission violation */
215 cs
->exception_index
= (rw
== 0) ? EXCP_TLBR
:
216 ((rw
== 1) ? EXCP_TLBW
:
220 cs
->exception_index
= EXCP_TLBD
;
224 env
->regs
[CR_TLBMISC
] &= ~CR_TLBMISC_D
;
226 env
->regs
[CR_TLBMISC
] |= CR_TLBMISC_D
;
228 env
->regs
[CR_PTEADDR
] &= CR_PTEADDR_PTBASE_MASK
;
229 env
->regs
[CR_PTEADDR
] |= (address
>> 10) & CR_PTEADDR_VPN_MASK
;
230 env
->mmu
.pteaddr_wr
= env
->regs
[CR_PTEADDR
];
231 env
->regs
[CR_BADADDR
] = address
;
235 int nios2_cpu_handle_mmu_fault(CPUState
*cs
, vaddr address
, int size
,
238 Nios2CPU
*cpu
= NIOS2_CPU(cs
);
239 CPUNios2State
*env
= &cpu
->env
;
241 if (cpu
->mmu_present
) {
242 if (MMU_SUPERVISOR_IDX
== mmu_idx
) {
243 if (address
>= 0xC0000000) {
244 /* Kernel physical page - TLB bypassed */
245 address
&= TARGET_PAGE_MASK
;
246 tlb_set_page(cs
, address
, address
, PAGE_BITS
,
247 mmu_idx
, TARGET_PAGE_SIZE
);
248 } else if (address
>= 0x80000000) {
249 /* Kernel virtual page */
250 return cpu_nios2_handle_virtual_page(cs
, address
, rw
, mmu_idx
);
252 /* User virtual page */
253 return cpu_nios2_handle_virtual_page(cs
, address
, rw
, mmu_idx
);
256 if (address
>= 0x80000000) {
257 /* Illegal access from user mode */
258 cs
->exception_index
= EXCP_SUPERA
;
259 env
->regs
[CR_BADADDR
] = address
;
262 /* User virtual page */
263 return cpu_nios2_handle_virtual_page(cs
, address
, rw
, mmu_idx
);
268 address
&= TARGET_PAGE_MASK
;
269 tlb_set_page(cs
, address
, address
, PAGE_BITS
,
270 mmu_idx
, TARGET_PAGE_SIZE
);
276 hwaddr
nios2_cpu_get_phys_page_debug(CPUState
*cs
, vaddr addr
)
278 Nios2CPU
*cpu
= NIOS2_CPU(cs
);
279 CPUNios2State
*env
= &cpu
->env
;
280 target_ulong vaddr
, paddr
= 0;
284 if (cpu
->mmu_present
&& (addr
< 0xC0000000)) {
285 hit
= mmu_translate(env
, &lu
, addr
, 0, 0);
287 vaddr
= addr
& TARGET_PAGE_MASK
;
288 paddr
= lu
.paddr
+ vaddr
- lu
.vaddr
;
291 qemu_log("cpu_get_phys_page debug MISS: %#" PRIx64
"\n", addr
);
294 paddr
= addr
& TARGET_PAGE_MASK
;
300 void nios2_cpu_do_unaligned_access(CPUState
*cs
, vaddr addr
,
301 MMUAccessType access_type
,
302 int mmu_idx
, uintptr_t retaddr
)
304 Nios2CPU
*cpu
= NIOS2_CPU(cs
);
305 CPUNios2State
*env
= &cpu
->env
;
307 env
->regs
[CR_BADADDR
] = addr
;
308 env
->regs
[CR_EXCEPTION
] = EXCP_UNALIGN
<< 2;
309 helper_raise_exception(env
, EXCP_UNALIGN
);
311 #endif /* !CONFIG_USER_ONLY */