1c9ed50e8SMax Filippov /*
2c9ed50e8SMax Filippov * Copyright (c) 2011 - 2019, Max Filippov, Open Source and Linux Lab.
3c9ed50e8SMax Filippov * All rights reserved.
4c9ed50e8SMax Filippov *
5c9ed50e8SMax Filippov * Redistribution and use in source and binary forms, with or without
6c9ed50e8SMax Filippov * modification, are permitted provided that the following conditions are met:
7c9ed50e8SMax Filippov * * Redistributions of source code must retain the above copyright
8c9ed50e8SMax Filippov * notice, this list of conditions and the following disclaimer.
9c9ed50e8SMax Filippov * * Redistributions in binary form must reproduce the above copyright
10c9ed50e8SMax Filippov * notice, this list of conditions and the following disclaimer in the
11c9ed50e8SMax Filippov * documentation and/or other materials provided with the distribution.
12c9ed50e8SMax Filippov * * Neither the name of the Open Source and Linux Lab nor the
13c9ed50e8SMax Filippov * names of its contributors may be used to endorse or promote products
14c9ed50e8SMax Filippov * derived from this software without specific prior written permission.
15c9ed50e8SMax Filippov *
16c9ed50e8SMax Filippov * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17c9ed50e8SMax Filippov * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18c9ed50e8SMax Filippov * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19c9ed50e8SMax Filippov * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20c9ed50e8SMax Filippov * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21c9ed50e8SMax Filippov * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22c9ed50e8SMax Filippov * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23c9ed50e8SMax Filippov * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24c9ed50e8SMax Filippov * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25c9ed50e8SMax Filippov * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26c9ed50e8SMax Filippov */
27c9ed50e8SMax Filippov
28c9ed50e8SMax Filippov #include "qemu/osdep.h"
29*cd617484SPhilippe Mathieu-Daudé #include "qemu/log.h"
30c9ed50e8SMax Filippov #include "cpu.h"
31c9ed50e8SMax Filippov #include "exec/helper-proto.h"
32c9ed50e8SMax Filippov #include "qemu/host-utils.h"
33c9ed50e8SMax Filippov #include "exec/exec-all.h"
34c9ed50e8SMax Filippov
copy_window_from_phys(CPUXtensaState * env,uint32_t window,uint32_t phys,uint32_t n)35c9ed50e8SMax Filippov static void copy_window_from_phys(CPUXtensaState *env,
36c9ed50e8SMax Filippov uint32_t window, uint32_t phys, uint32_t n)
37c9ed50e8SMax Filippov {
38c9ed50e8SMax Filippov assert(phys < env->config->nareg);
39c9ed50e8SMax Filippov if (phys + n <= env->config->nareg) {
40c9ed50e8SMax Filippov memcpy(env->regs + window, env->phys_regs + phys,
41c9ed50e8SMax Filippov n * sizeof(uint32_t));
42c9ed50e8SMax Filippov } else {
43c9ed50e8SMax Filippov uint32_t n1 = env->config->nareg - phys;
44c9ed50e8SMax Filippov memcpy(env->regs + window, env->phys_regs + phys,
45c9ed50e8SMax Filippov n1 * sizeof(uint32_t));
46c9ed50e8SMax Filippov memcpy(env->regs + window + n1, env->phys_regs,
47c9ed50e8SMax Filippov (n - n1) * sizeof(uint32_t));
48c9ed50e8SMax Filippov }
49c9ed50e8SMax Filippov }
50c9ed50e8SMax Filippov
copy_phys_from_window(CPUXtensaState * env,uint32_t phys,uint32_t window,uint32_t n)51c9ed50e8SMax Filippov static void copy_phys_from_window(CPUXtensaState *env,
52c9ed50e8SMax Filippov uint32_t phys, uint32_t window, uint32_t n)
53c9ed50e8SMax Filippov {
54c9ed50e8SMax Filippov assert(phys < env->config->nareg);
55c9ed50e8SMax Filippov if (phys + n <= env->config->nareg) {
56c9ed50e8SMax Filippov memcpy(env->phys_regs + phys, env->regs + window,
57c9ed50e8SMax Filippov n * sizeof(uint32_t));
58c9ed50e8SMax Filippov } else {
59c9ed50e8SMax Filippov uint32_t n1 = env->config->nareg - phys;
60c9ed50e8SMax Filippov memcpy(env->phys_regs + phys, env->regs + window,
61c9ed50e8SMax Filippov n1 * sizeof(uint32_t));
62c9ed50e8SMax Filippov memcpy(env->phys_regs, env->regs + window + n1,
63c9ed50e8SMax Filippov (n - n1) * sizeof(uint32_t));
64c9ed50e8SMax Filippov }
65c9ed50e8SMax Filippov }
66c9ed50e8SMax Filippov
windowbase_bound(unsigned a,const CPUXtensaState * env)67c9ed50e8SMax Filippov static inline unsigned windowbase_bound(unsigned a, const CPUXtensaState *env)
68c9ed50e8SMax Filippov {
69c9ed50e8SMax Filippov return a & (env->config->nareg / 4 - 1);
70c9ed50e8SMax Filippov }
71c9ed50e8SMax Filippov
windowstart_bit(unsigned a,const CPUXtensaState * env)72c9ed50e8SMax Filippov static inline unsigned windowstart_bit(unsigned a, const CPUXtensaState *env)
73c9ed50e8SMax Filippov {
74c9ed50e8SMax Filippov return 1 << windowbase_bound(a, env);
75c9ed50e8SMax Filippov }
76c9ed50e8SMax Filippov
xtensa_sync_window_from_phys(CPUXtensaState * env)77c9ed50e8SMax Filippov void xtensa_sync_window_from_phys(CPUXtensaState *env)
78c9ed50e8SMax Filippov {
79c9ed50e8SMax Filippov copy_window_from_phys(env, 0, env->sregs[WINDOW_BASE] * 4, 16);
80c9ed50e8SMax Filippov }
81c9ed50e8SMax Filippov
xtensa_sync_phys_from_window(CPUXtensaState * env)82c9ed50e8SMax Filippov void xtensa_sync_phys_from_window(CPUXtensaState *env)
83c9ed50e8SMax Filippov {
84c9ed50e8SMax Filippov copy_phys_from_window(env, env->sregs[WINDOW_BASE] * 4, 0, 16);
85c9ed50e8SMax Filippov }
86c9ed50e8SMax Filippov
xtensa_rotate_window_abs(CPUXtensaState * env,uint32_t position)87c9ed50e8SMax Filippov static void xtensa_rotate_window_abs(CPUXtensaState *env, uint32_t position)
88c9ed50e8SMax Filippov {
89c9ed50e8SMax Filippov xtensa_sync_phys_from_window(env);
90c9ed50e8SMax Filippov env->sregs[WINDOW_BASE] = windowbase_bound(position, env);
91c9ed50e8SMax Filippov xtensa_sync_window_from_phys(env);
92c9ed50e8SMax Filippov }
93c9ed50e8SMax Filippov
xtensa_rotate_window(CPUXtensaState * env,uint32_t delta)94c9ed50e8SMax Filippov void xtensa_rotate_window(CPUXtensaState *env, uint32_t delta)
95c9ed50e8SMax Filippov {
96c9ed50e8SMax Filippov xtensa_rotate_window_abs(env, env->sregs[WINDOW_BASE] + delta);
97c9ed50e8SMax Filippov }
98c9ed50e8SMax Filippov
HELPER(sync_windowbase)998df3fd35SMax Filippov void HELPER(sync_windowbase)(CPUXtensaState *env)
100c9ed50e8SMax Filippov {
1018df3fd35SMax Filippov xtensa_rotate_window_abs(env, env->windowbase_next);
102c9ed50e8SMax Filippov }
103c9ed50e8SMax Filippov
HELPER(entry)104c9ed50e8SMax Filippov void HELPER(entry)(CPUXtensaState *env, uint32_t pc, uint32_t s, uint32_t imm)
105c9ed50e8SMax Filippov {
106c9ed50e8SMax Filippov int callinc = (env->sregs[PS] & PS_CALLINC) >> PS_CALLINC_SHIFT;
107c9ed50e8SMax Filippov
108c9ed50e8SMax Filippov env->regs[(callinc << 2) | (s & 3)] = env->regs[s] - imm;
1098df3fd35SMax Filippov env->windowbase_next = env->sregs[WINDOW_BASE] + callinc;
1108df3fd35SMax Filippov env->sregs[WINDOW_START] |= windowstart_bit(env->windowbase_next, env);
111c9ed50e8SMax Filippov }
112c9ed50e8SMax Filippov
HELPER(window_check)113c9ed50e8SMax Filippov void HELPER(window_check)(CPUXtensaState *env, uint32_t pc, uint32_t w)
114c9ed50e8SMax Filippov {
115c9ed50e8SMax Filippov uint32_t windowbase = windowbase_bound(env->sregs[WINDOW_BASE], env);
116c9ed50e8SMax Filippov uint32_t windowstart = xtensa_replicate_windowstart(env) >>
117c9ed50e8SMax Filippov (env->sregs[WINDOW_BASE] + 1);
118c9ed50e8SMax Filippov uint32_t n = ctz32(windowstart) + 1;
119c9ed50e8SMax Filippov
120c9ed50e8SMax Filippov assert(n <= w);
121c9ed50e8SMax Filippov
122c9ed50e8SMax Filippov xtensa_rotate_window(env, n);
123c9ed50e8SMax Filippov env->sregs[PS] = (env->sregs[PS] & ~PS_OWB) |
124c9ed50e8SMax Filippov (windowbase << PS_OWB_SHIFT) | PS_EXCM;
125c9ed50e8SMax Filippov env->sregs[EPC1] = env->pc = pc;
126c9ed50e8SMax Filippov
127c9ed50e8SMax Filippov switch (ctz32(windowstart >> n)) {
128c9ed50e8SMax Filippov case 0:
129c9ed50e8SMax Filippov HELPER(exception)(env, EXC_WINDOW_OVERFLOW4);
130c9ed50e8SMax Filippov break;
131c9ed50e8SMax Filippov case 1:
132c9ed50e8SMax Filippov HELPER(exception)(env, EXC_WINDOW_OVERFLOW8);
133c9ed50e8SMax Filippov break;
134c9ed50e8SMax Filippov default:
135c9ed50e8SMax Filippov HELPER(exception)(env, EXC_WINDOW_OVERFLOW12);
136c9ed50e8SMax Filippov break;
137c9ed50e8SMax Filippov }
138c9ed50e8SMax Filippov }
139c9ed50e8SMax Filippov
HELPER(test_ill_retw)140c9ed50e8SMax Filippov void HELPER(test_ill_retw)(CPUXtensaState *env, uint32_t pc)
141c9ed50e8SMax Filippov {
142c9ed50e8SMax Filippov int n = (env->regs[0] >> 30) & 0x3;
143c9ed50e8SMax Filippov int m = 0;
144c9ed50e8SMax Filippov uint32_t windowbase = windowbase_bound(env->sregs[WINDOW_BASE], env);
145c9ed50e8SMax Filippov uint32_t windowstart = env->sregs[WINDOW_START];
146c9ed50e8SMax Filippov
147c9ed50e8SMax Filippov if (windowstart & windowstart_bit(windowbase - 1, env)) {
148c9ed50e8SMax Filippov m = 1;
149c9ed50e8SMax Filippov } else if (windowstart & windowstart_bit(windowbase - 2, env)) {
150c9ed50e8SMax Filippov m = 2;
151c9ed50e8SMax Filippov } else if (windowstart & windowstart_bit(windowbase - 3, env)) {
152c9ed50e8SMax Filippov m = 3;
153c9ed50e8SMax Filippov }
154c9ed50e8SMax Filippov
155c9ed50e8SMax Filippov if (n == 0 || (m != 0 && m != n)) {
156c9ed50e8SMax Filippov qemu_log_mask(LOG_GUEST_ERROR, "Illegal retw instruction(pc = %08x), "
157c9ed50e8SMax Filippov "PS = %08x, m = %d, n = %d\n",
158c9ed50e8SMax Filippov pc, env->sregs[PS], m, n);
159c9ed50e8SMax Filippov HELPER(exception_cause)(env, pc, ILLEGAL_INSTRUCTION_CAUSE);
160c9ed50e8SMax Filippov }
161c9ed50e8SMax Filippov }
162c9ed50e8SMax Filippov
HELPER(test_underflow_retw)163c9ed50e8SMax Filippov void HELPER(test_underflow_retw)(CPUXtensaState *env, uint32_t pc)
164c9ed50e8SMax Filippov {
165c9ed50e8SMax Filippov int n = (env->regs[0] >> 30) & 0x3;
166c9ed50e8SMax Filippov
167c9ed50e8SMax Filippov if (!(env->sregs[WINDOW_START] &
168c9ed50e8SMax Filippov windowstart_bit(env->sregs[WINDOW_BASE] - n, env))) {
169c9ed50e8SMax Filippov uint32_t windowbase = windowbase_bound(env->sregs[WINDOW_BASE], env);
170c9ed50e8SMax Filippov
171c9ed50e8SMax Filippov xtensa_rotate_window(env, -n);
172c9ed50e8SMax Filippov /* window underflow */
173c9ed50e8SMax Filippov env->sregs[PS] = (env->sregs[PS] & ~PS_OWB) |
174c9ed50e8SMax Filippov (windowbase << PS_OWB_SHIFT) | PS_EXCM;
175c9ed50e8SMax Filippov env->sregs[EPC1] = env->pc = pc;
176c9ed50e8SMax Filippov
177c9ed50e8SMax Filippov if (n == 1) {
178c9ed50e8SMax Filippov HELPER(exception)(env, EXC_WINDOW_UNDERFLOW4);
179c9ed50e8SMax Filippov } else if (n == 2) {
180c9ed50e8SMax Filippov HELPER(exception)(env, EXC_WINDOW_UNDERFLOW8);
181c9ed50e8SMax Filippov } else if (n == 3) {
182c9ed50e8SMax Filippov HELPER(exception)(env, EXC_WINDOW_UNDERFLOW12);
183c9ed50e8SMax Filippov }
184c9ed50e8SMax Filippov }
185c9ed50e8SMax Filippov }
186c9ed50e8SMax Filippov
HELPER(retw)187c949009bSMax Filippov void HELPER(retw)(CPUXtensaState *env, uint32_t a0)
188c9ed50e8SMax Filippov {
189c949009bSMax Filippov int n = (a0 >> 30) & 0x3;
190c9ed50e8SMax Filippov
191c9ed50e8SMax Filippov xtensa_rotate_window(env, -n);
192c9ed50e8SMax Filippov }
193c9ed50e8SMax Filippov
xtensa_restore_owb(CPUXtensaState * env)194c9ed50e8SMax Filippov void xtensa_restore_owb(CPUXtensaState *env)
195c9ed50e8SMax Filippov {
196c9ed50e8SMax Filippov xtensa_rotate_window_abs(env, (env->sregs[PS] & PS_OWB) >> PS_OWB_SHIFT);
197c9ed50e8SMax Filippov }
198c9ed50e8SMax Filippov
HELPER(restore_owb)199c9ed50e8SMax Filippov void HELPER(restore_owb)(CPUXtensaState *env)
200c9ed50e8SMax Filippov {
201c9ed50e8SMax Filippov xtensa_restore_owb(env);
202c9ed50e8SMax Filippov }
203c9ed50e8SMax Filippov
HELPER(movsp)204c9ed50e8SMax Filippov void HELPER(movsp)(CPUXtensaState *env, uint32_t pc)
205c9ed50e8SMax Filippov {
206c9ed50e8SMax Filippov if ((env->sregs[WINDOW_START] &
207c9ed50e8SMax Filippov (windowstart_bit(env->sregs[WINDOW_BASE] - 3, env) |
208c9ed50e8SMax Filippov windowstart_bit(env->sregs[WINDOW_BASE] - 2, env) |
209c9ed50e8SMax Filippov windowstart_bit(env->sregs[WINDOW_BASE] - 1, env))) == 0) {
210c9ed50e8SMax Filippov HELPER(exception_cause)(env, pc, ALLOCA_CAUSE);
211c9ed50e8SMax Filippov }
212c9ed50e8SMax Filippov }
213