2 * Softmmu related functions
4 * Copyright (C) 2010-2012 Guan Xuetao
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation, or any later version.
9 * See the COPYING file in the top-level directory.
11 #ifdef CONFIG_USER_ONLY
12 #error This file only exist under softmmu circumstance
15 #include "qemu/osdep.h"
21 #define DPRINTF(fmt, ...) printf("%s: " fmt , __func__, ## __VA_ARGS__)
23 #define DPRINTF(fmt, ...) do {} while (0)
26 #define SUPERPAGE_SIZE (1 << 22)
27 #define UC32_PAGETABLE_READ (1 << 8)
28 #define UC32_PAGETABLE_WRITE (1 << 7)
29 #define UC32_PAGETABLE_EXEC (1 << 6)
30 #define UC32_PAGETABLE_EXIST (1 << 2)
31 #define PAGETABLE_TYPE(x) ((x) & 3)
34 /* Map CPU modes onto saved register banks. */
35 static inline int bank_number(CPUUniCore32State
*env
, int mode
)
37 UniCore32CPU
*cpu
= uc32_env_get_cpu(env
);
52 cpu_abort(CPU(cpu
), "Bad mode %x\n", mode
);
56 void switch_mode(CPUUniCore32State
*env
, int mode
)
61 old_mode
= env
->uncached_asr
& ASR_M
;
62 if (mode
== old_mode
) {
66 i
= bank_number(env
, old_mode
);
67 env
->banked_r29
[i
] = env
->regs
[29];
68 env
->banked_r30
[i
] = env
->regs
[30];
69 env
->banked_bsr
[i
] = env
->bsr
;
71 i
= bank_number(env
, mode
);
72 env
->regs
[29] = env
->banked_r29
[i
];
73 env
->regs
[30] = env
->banked_r30
[i
];
74 env
->bsr
= env
->banked_bsr
[i
];
77 /* Handle a CPU exception. */
78 void uc32_cpu_do_interrupt(CPUState
*cs
)
80 UniCore32CPU
*cpu
= UNICORE32_CPU(cs
);
81 CPUUniCore32State
*env
= &cpu
->env
;
85 switch (cs
->exception_index
) {
87 new_mode
= ASR_MODE_PRIV
;
91 DPRINTF("itrap happened at %x\n", env
->regs
[31]);
92 new_mode
= ASR_MODE_TRAP
;
96 DPRINTF("dtrap happened at %x\n", env
->regs
[31]);
97 new_mode
= ASR_MODE_TRAP
;
101 new_mode
= ASR_MODE_INTR
;
105 cpu_abort(cs
, "Unhandled exception 0x%x\n", cs
->exception_index
);
109 if (env
->cp0
.c1_sys
& (1 << 13)) {
113 switch_mode(env
, new_mode
);
114 env
->bsr
= cpu_asr_read(env
);
115 env
->uncached_asr
= (env
->uncached_asr
& ~ASR_M
) | new_mode
;
116 env
->uncached_asr
|= ASR_I
;
117 /* The PC already points to the proper instruction. */
118 env
->regs
[30] = env
->regs
[31];
119 env
->regs
[31] = addr
;
120 cs
->interrupt_request
|= CPU_INTERRUPT_EXITTB
;
123 static int get_phys_addr_ucv2(CPUUniCore32State
*env
, uint32_t address
,
124 int access_type
, int is_user
, uint32_t *phys_ptr
, int *prot
,
125 target_ulong
*page_size
)
127 UniCore32CPU
*cpu
= uc32_env_get_cpu(env
);
128 CPUState
*cs
= CPU(cpu
);
134 /* Pagetable walk. */
135 /* Lookup l1 descriptor. */
136 table
= env
->cp0
.c2_base
& 0xfffff000;
137 table
|= (address
>> 20) & 0xffc;
138 desc
= ldl_phys(cs
->as
, table
);
140 switch (PAGETABLE_TYPE(desc
)) {
143 if (!(desc
& UC32_PAGETABLE_EXIST
)) {
144 code
= 0x0b; /* superpage miss */
147 phys_addr
= (desc
& 0xffc00000) | (address
& 0x003fffff);
148 *page_size
= SUPERPAGE_SIZE
;
151 /* Lookup l2 entry. */
153 DPRINTF("PGD address %x, desc %x\n", table
, desc
);
155 if (!(desc
& UC32_PAGETABLE_EXIST
)) {
156 code
= 0x05; /* second pagetable miss */
159 table
= (desc
& 0xfffff000) | ((address
>> 10) & 0xffc);
160 desc
= ldl_phys(cs
->as
, table
);
163 DPRINTF("PTE address %x, desc %x\n", table
, desc
);
165 if (!(desc
& UC32_PAGETABLE_EXIST
)) {
166 code
= 0x08; /* page miss */
169 switch (PAGETABLE_TYPE(desc
)) {
171 phys_addr
= (desc
& 0xfffff000) | (address
& 0xfff);
172 *page_size
= TARGET_PAGE_SIZE
;
175 cpu_abort(CPU(cpu
), "wrong page type!");
179 cpu_abort(CPU(cpu
), "wrong page type!");
182 *phys_ptr
= phys_addr
;
184 /* Check access permissions. */
185 if (desc
& UC32_PAGETABLE_READ
) {
188 if (is_user
&& (access_type
== 0)) {
189 code
= 0x11; /* access unreadable area */
194 if (desc
& UC32_PAGETABLE_WRITE
) {
197 if (is_user
&& (access_type
== 1)) {
198 code
= 0x12; /* access unwritable area */
203 if (desc
& UC32_PAGETABLE_EXEC
) {
206 if (is_user
&& (access_type
== 2)) {
207 code
= 0x13; /* access unexecutable area */
216 int uc32_cpu_handle_mmu_fault(CPUState
*cs
, vaddr address
,
217 int access_type
, int mmu_idx
)
219 UniCore32CPU
*cpu
= UNICORE32_CPU(cs
);
220 CPUUniCore32State
*env
= &cpu
->env
;
222 target_ulong page_size
;
227 is_user
= mmu_idx
== MMU_USER_IDX
;
229 if ((env
->cp0
.c1_sys
& 1) == 0) {
232 prot
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
233 page_size
= TARGET_PAGE_SIZE
;
236 if ((address
& (1 << 31)) || (is_user
)) {
237 ret
= get_phys_addr_ucv2(env
, address
, access_type
, is_user
,
238 &phys_addr
, &prot
, &page_size
);
240 DPRINTF("user space access: ret %x, address %" VADDR_PRIx
", "
241 "access_type %x, phys_addr %x, prot %x\n",
242 ret
, address
, access_type
, phys_addr
, prot
);
246 phys_addr
= address
| (1 << 31);
247 prot
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
248 page_size
= TARGET_PAGE_SIZE
;
254 /* Map a single page. */
255 phys_addr
&= TARGET_PAGE_MASK
;
256 address
&= TARGET_PAGE_MASK
;
257 tlb_set_page(cs
, address
, phys_addr
, prot
, mmu_idx
, page_size
);
261 env
->cp0
.c3_faultstatus
= ret
;
262 env
->cp0
.c4_faultaddr
= address
;
263 if (access_type
== 2) {
264 cs
->exception_index
= UC32_EXCP_ITRAP
;
266 cs
->exception_index
= UC32_EXCP_DTRAP
;
271 hwaddr
uc32_cpu_get_phys_page_debug(CPUState
*cs
, vaddr addr
)
273 UniCore32CPU
*cpu
= UNICORE32_CPU(cs
);
275 cpu_abort(CPU(cpu
), "%s not supported yet\n", __func__
);