target-xtensa: implement windowed registers
[qemu.git] / target-xtensa / op_helper.c
blob7c3fb88a96047345093896e125dd75698b8febc1
1 /*
2 * Copyright (c) 2011, Max Filippov, Open Source and Linux Lab.
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the Open Source and Linux Lab nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "cpu.h"
29 #include "dyngen-exec.h"
30 #include "helpers.h"
31 #include "host-utils.h"
33 #define MMUSUFFIX _mmu
35 #define SHIFT 0
36 #include "softmmu_template.h"
38 #define SHIFT 1
39 #include "softmmu_template.h"
41 #define SHIFT 2
42 #include "softmmu_template.h"
44 #define SHIFT 3
45 #include "softmmu_template.h"
47 void tlb_fill(target_ulong addr, int is_write, int mmu_idx, void *retaddr)
49 tlb_set_page(cpu_single_env,
50 addr & ~(TARGET_PAGE_SIZE - 1),
51 addr & ~(TARGET_PAGE_SIZE - 1),
52 PAGE_READ | PAGE_WRITE | PAGE_EXEC,
53 mmu_idx, TARGET_PAGE_SIZE);
56 void HELPER(exception)(uint32_t excp)
58 env->exception_index = excp;
59 cpu_loop_exit(env);
62 void HELPER(exception_cause)(uint32_t pc, uint32_t cause)
64 uint32_t vector;
66 env->pc = pc;
67 if (env->sregs[PS] & PS_EXCM) {
68 if (env->config->ndepc) {
69 env->sregs[DEPC] = pc;
70 } else {
71 env->sregs[EPC1] = pc;
73 vector = EXC_DOUBLE;
74 } else {
75 env->sregs[EPC1] = pc;
76 vector = (env->sregs[PS] & PS_UM) ? EXC_USER : EXC_KERNEL;
79 env->sregs[EXCCAUSE] = cause;
80 env->sregs[PS] |= PS_EXCM;
82 HELPER(exception)(vector);
85 void HELPER(exception_cause_vaddr)(uint32_t pc, uint32_t cause, uint32_t vaddr)
87 env->sregs[EXCVADDR] = vaddr;
88 HELPER(exception_cause)(pc, cause);
91 uint32_t HELPER(nsa)(uint32_t v)
93 if (v & 0x80000000) {
94 v = ~v;
96 return v ? clz32(v) - 1 : 31;
99 uint32_t HELPER(nsau)(uint32_t v)
101 return v ? clz32(v) : 32;
104 static void copy_window_from_phys(CPUState *env,
105 uint32_t window, uint32_t phys, uint32_t n)
107 assert(phys < env->config->nareg);
108 if (phys + n <= env->config->nareg) {
109 memcpy(env->regs + window, env->phys_regs + phys,
110 n * sizeof(uint32_t));
111 } else {
112 uint32_t n1 = env->config->nareg - phys;
113 memcpy(env->regs + window, env->phys_regs + phys,
114 n1 * sizeof(uint32_t));
115 memcpy(env->regs + window + n1, env->phys_regs,
116 (n - n1) * sizeof(uint32_t));
120 static void copy_phys_from_window(CPUState *env,
121 uint32_t phys, uint32_t window, uint32_t n)
123 assert(phys < env->config->nareg);
124 if (phys + n <= env->config->nareg) {
125 memcpy(env->phys_regs + phys, env->regs + window,
126 n * sizeof(uint32_t));
127 } else {
128 uint32_t n1 = env->config->nareg - phys;
129 memcpy(env->phys_regs + phys, env->regs + window,
130 n1 * sizeof(uint32_t));
131 memcpy(env->phys_regs, env->regs + window + n1,
132 (n - n1) * sizeof(uint32_t));
137 static inline unsigned windowbase_bound(unsigned a, const CPUState *env)
139 return a & (env->config->nareg / 4 - 1);
142 static inline unsigned windowstart_bit(unsigned a, const CPUState *env)
144 return 1 << windowbase_bound(a, env);
147 void xtensa_sync_window_from_phys(CPUState *env)
149 copy_window_from_phys(env, 0, env->sregs[WINDOW_BASE] * 4, 16);
152 void xtensa_sync_phys_from_window(CPUState *env)
154 copy_phys_from_window(env, env->sregs[WINDOW_BASE] * 4, 0, 16);
157 static void rotate_window_abs(uint32_t position)
159 xtensa_sync_phys_from_window(env);
160 env->sregs[WINDOW_BASE] = windowbase_bound(position, env);
161 xtensa_sync_window_from_phys(env);
164 static void rotate_window(uint32_t delta)
166 rotate_window_abs(env->sregs[WINDOW_BASE] + delta);
169 void HELPER(wsr_windowbase)(uint32_t v)
171 rotate_window_abs(v);
174 void HELPER(entry)(uint32_t pc, uint32_t s, uint32_t imm)
176 int callinc = (env->sregs[PS] & PS_CALLINC) >> PS_CALLINC_SHIFT;
177 if (s > 3 || ((env->sregs[PS] & (PS_WOE | PS_EXCM)) ^ PS_WOE) != 0) {
178 qemu_log("Illegal entry instruction(pc = %08x), PS = %08x\n",
179 pc, env->sregs[PS]);
180 HELPER(exception_cause)(pc, ILLEGAL_INSTRUCTION_CAUSE);
181 } else {
182 env->regs[(callinc << 2) | (s & 3)] = env->regs[s] - (imm << 3);
183 rotate_window(callinc);
184 env->sregs[WINDOW_START] |=
185 windowstart_bit(env->sregs[WINDOW_BASE], env);
189 void HELPER(window_check)(uint32_t pc, uint32_t w)
191 uint32_t windowbase = windowbase_bound(env->sregs[WINDOW_BASE], env);
192 uint32_t windowstart = env->sregs[WINDOW_START];
193 uint32_t m, n;
195 if ((env->sregs[PS] & (PS_WOE | PS_EXCM)) ^ PS_WOE) {
196 return;
199 for (n = 1; ; ++n) {
200 if (n > w) {
201 return;
203 if (windowstart & windowstart_bit(windowbase + n, env)) {
204 break;
208 m = windowbase_bound(windowbase + n, env);
209 rotate_window(n);
210 env->sregs[PS] = (env->sregs[PS] & ~PS_OWB) |
211 (windowbase << PS_OWB_SHIFT) | PS_EXCM;
212 env->sregs[EPC1] = env->pc = pc;
214 if (windowstart & windowstart_bit(m + 1, env)) {
215 HELPER(exception)(EXC_WINDOW_OVERFLOW4);
216 } else if (windowstart & windowstart_bit(m + 2, env)) {
217 HELPER(exception)(EXC_WINDOW_OVERFLOW8);
218 } else {
219 HELPER(exception)(EXC_WINDOW_OVERFLOW12);
223 uint32_t HELPER(retw)(uint32_t pc)
225 int n = (env->regs[0] >> 30) & 0x3;
226 int m = 0;
227 uint32_t windowbase = windowbase_bound(env->sregs[WINDOW_BASE], env);
228 uint32_t windowstart = env->sregs[WINDOW_START];
229 uint32_t ret_pc = 0;
231 if (windowstart & windowstart_bit(windowbase - 1, env)) {
232 m = 1;
233 } else if (windowstart & windowstart_bit(windowbase - 2, env)) {
234 m = 2;
235 } else if (windowstart & windowstart_bit(windowbase - 3, env)) {
236 m = 3;
239 if (n == 0 || (m != 0 && m != n) ||
240 ((env->sregs[PS] & (PS_WOE | PS_EXCM)) ^ PS_WOE) != 0) {
241 qemu_log("Illegal retw instruction(pc = %08x), "
242 "PS = %08x, m = %d, n = %d\n",
243 pc, env->sregs[PS], m, n);
244 HELPER(exception_cause)(pc, ILLEGAL_INSTRUCTION_CAUSE);
245 } else {
246 int owb = windowbase;
248 ret_pc = (pc & 0xc0000000) | (env->regs[0] & 0x3fffffff);
250 rotate_window(-n);
251 if (windowstart & windowstart_bit(env->sregs[WINDOW_BASE], env)) {
252 env->sregs[WINDOW_START] &= ~windowstart_bit(owb, env);
253 } else {
254 /* window underflow */
255 env->sregs[PS] = (env->sregs[PS] & ~PS_OWB) |
256 (windowbase << PS_OWB_SHIFT) | PS_EXCM;
257 env->sregs[EPC1] = env->pc = pc;
259 if (n == 1) {
260 HELPER(exception)(EXC_WINDOW_UNDERFLOW4);
261 } else if (n == 2) {
262 HELPER(exception)(EXC_WINDOW_UNDERFLOW8);
263 } else if (n == 3) {
264 HELPER(exception)(EXC_WINDOW_UNDERFLOW12);
268 return ret_pc;
271 void HELPER(rotw)(uint32_t imm4)
273 rotate_window(imm4);
276 void HELPER(restore_owb)(void)
278 rotate_window_abs((env->sregs[PS] & PS_OWB) >> PS_OWB_SHIFT);
281 void HELPER(movsp)(uint32_t pc)
283 if ((env->sregs[WINDOW_START] &
284 (windowstart_bit(env->sregs[WINDOW_BASE] - 3, env) |
285 windowstart_bit(env->sregs[WINDOW_BASE] - 2, env) |
286 windowstart_bit(env->sregs[WINDOW_BASE] - 1, env))) == 0) {
287 HELPER(exception_cause)(pc, ALLOCA_CAUSE);
291 void HELPER(dump_state)(void)
293 cpu_dump_state(env, stderr, fprintf, 0);