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"
31 #if defined(CONFIG_USER_ONLY)
33 void nios2_cpu_do_interrupt(CPUState
*cs
)
35 Nios2CPU
*cpu
= NIOS2_CPU(cs
);
36 CPUNios2State
*env
= &cpu
->env
;
37 cs
->exception_index
= -1;
38 env
->regs
[R_EA
] = env
->regs
[R_PC
] + 4;
41 void nios2_cpu_record_sigsegv(CPUState
*cs
, vaddr addr
,
42 MMUAccessType access_type
,
43 bool maperr
, uintptr_t retaddr
)
45 /* FIXME: Disentangle kuser page from linux-user sigsegv handling. */
46 cs
->exception_index
= 0xaa;
47 cpu_loop_exit_restore(cs
, retaddr
);
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 qemu_log_mask(CPU_LOG_INT
, "BREAK exception at pc=%x\n",
174 /* The semihosting instruction is "break 1". */
175 if (semihosting_enabled() &&
176 cpu_ldl_code(env
, env
->regs
[R_PC
]) == 0x003da07a) {
177 qemu_log_mask(CPU_LOG_INT
, "Entering semihosting\n");
178 env
->regs
[R_PC
] += 4;
179 do_nios2_semihosting(env
);
183 if ((env
->regs
[CR_STATUS
] & CR_STATUS_EH
) == 0) {
184 env
->regs
[CR_BSTATUS
] = env
->regs
[CR_STATUS
];
185 env
->regs
[R_BA
] = env
->regs
[R_PC
] + 4;
188 env
->regs
[CR_STATUS
] |= CR_STATUS_EH
;
189 env
->regs
[CR_STATUS
] &= ~(CR_STATUS_PIE
| CR_STATUS_U
);
191 env
->regs
[CR_EXCEPTION
] &= ~(0x1F << 2);
192 env
->regs
[CR_EXCEPTION
] |= (cs
->exception_index
& 0x1F) << 2;
194 env
->regs
[R_PC
] = cpu
->exception_addr
;
198 cpu_abort(cs
, "unhandled exception type=%d\n",
199 cs
->exception_index
);
204 hwaddr
nios2_cpu_get_phys_page_debug(CPUState
*cs
, vaddr addr
)
206 Nios2CPU
*cpu
= NIOS2_CPU(cs
);
207 CPUNios2State
*env
= &cpu
->env
;
208 target_ulong vaddr
, paddr
= 0;
212 if (cpu
->mmu_present
&& (addr
< 0xC0000000)) {
213 hit
= mmu_translate(env
, &lu
, addr
, 0, 0);
215 vaddr
= addr
& TARGET_PAGE_MASK
;
216 paddr
= lu
.paddr
+ vaddr
- lu
.vaddr
;
219 qemu_log("cpu_get_phys_page debug MISS: %#" PRIx64
"\n", addr
);
222 paddr
= addr
& TARGET_PAGE_MASK
;
228 void nios2_cpu_do_unaligned_access(CPUState
*cs
, vaddr addr
,
229 MMUAccessType access_type
,
230 int mmu_idx
, uintptr_t retaddr
)
232 Nios2CPU
*cpu
= NIOS2_CPU(cs
);
233 CPUNios2State
*env
= &cpu
->env
;
235 env
->regs
[CR_BADADDR
] = addr
;
236 env
->regs
[CR_EXCEPTION
] = EXCP_UNALIGN
<< 2;
237 helper_raise_exception(env
, EXCP_UNALIGN
);
240 bool nios2_cpu_tlb_fill(CPUState
*cs
, vaddr address
, int size
,
241 MMUAccessType access_type
, int mmu_idx
,
242 bool probe
, uintptr_t retaddr
)
244 Nios2CPU
*cpu
= NIOS2_CPU(cs
);
245 CPUNios2State
*env
= &cpu
->env
;
246 unsigned int excp
= EXCP_TLBD
;
247 target_ulong vaddr
, paddr
;
251 if (!cpu
->mmu_present
) {
253 address
&= TARGET_PAGE_MASK
;
254 tlb_set_page(cs
, address
, address
, PAGE_BITS
,
255 mmu_idx
, TARGET_PAGE_SIZE
);
259 if (MMU_SUPERVISOR_IDX
== mmu_idx
) {
260 if (address
>= 0xC0000000) {
261 /* Kernel physical page - TLB bypassed */
262 address
&= TARGET_PAGE_MASK
;
263 tlb_set_page(cs
, address
, address
, PAGE_BITS
,
264 mmu_idx
, TARGET_PAGE_SIZE
);
268 if (address
>= 0x80000000) {
269 /* Illegal access from user mode */
273 cs
->exception_index
= EXCP_SUPERA
;
274 env
->regs
[CR_BADADDR
] = address
;
275 cpu_loop_exit_restore(cs
, retaddr
);
280 hit
= mmu_translate(env
, &lu
, address
, access_type
, mmu_idx
);
282 vaddr
= address
& TARGET_PAGE_MASK
;
283 paddr
= lu
.paddr
+ vaddr
- lu
.vaddr
;
285 if (((access_type
== MMU_DATA_LOAD
) && (lu
.prot
& PAGE_READ
)) ||
286 ((access_type
== MMU_DATA_STORE
) && (lu
.prot
& PAGE_WRITE
)) ||
287 ((access_type
== MMU_INST_FETCH
) && (lu
.prot
& PAGE_EXEC
))) {
288 tlb_set_page(cs
, vaddr
, paddr
, lu
.prot
,
289 mmu_idx
, TARGET_PAGE_SIZE
);
293 /* Permission violation */
294 excp
= (access_type
== MMU_DATA_LOAD
? EXCP_TLBR
:
295 access_type
== MMU_DATA_STORE
? EXCP_TLBW
: EXCP_TLBX
);
302 if (access_type
== MMU_INST_FETCH
) {
303 env
->regs
[CR_TLBMISC
] &= ~CR_TLBMISC_D
;
305 env
->regs
[CR_TLBMISC
] |= CR_TLBMISC_D
;
307 env
->regs
[CR_PTEADDR
] &= CR_PTEADDR_PTBASE_MASK
;
308 env
->regs
[CR_PTEADDR
] |= (address
>> 10) & CR_PTEADDR_VPN_MASK
;
309 env
->mmu
.pteaddr_wr
= env
->regs
[CR_PTEADDR
];
311 cs
->exception_index
= excp
;
312 env
->regs
[CR_BADADDR
] = address
;
313 cpu_loop_exit_restore(cs
, retaddr
);
315 #endif /* !CONFIG_USER_ONLY */