xref: /qemu/target/xtensa/win_helper.c (revision 7cef6d686309e2792186504ae17cf4f3eb57ef68)
1 /*
2  * Copyright (c) 2011 - 2019, Max Filippov, Open Source and Linux Lab.
3  * All rights reserved.
4  *
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.
15  *
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.
26  */
27 
28 #include "qemu/osdep.h"
29 #include "qemu/log.h"
30 #include "cpu.h"
31 #include "exec/helper-proto.h"
32 #include "qemu/host-utils.h"
33 
copy_window_from_phys(CPUXtensaState * env,uint32_t window,uint32_t phys,uint32_t n)34 static void copy_window_from_phys(CPUXtensaState *env,
35                                   uint32_t window, uint32_t phys, uint32_t n)
36 {
37     assert(phys < env->config->nareg);
38     if (phys + n <= env->config->nareg) {
39         memcpy(env->regs + window, env->phys_regs + phys,
40                n * sizeof(uint32_t));
41     } else {
42         uint32_t n1 = env->config->nareg - phys;
43         memcpy(env->regs + window, env->phys_regs + phys,
44                n1 * sizeof(uint32_t));
45         memcpy(env->regs + window + n1, env->phys_regs,
46                (n - n1) * sizeof(uint32_t));
47     }
48 }
49 
copy_phys_from_window(CPUXtensaState * env,uint32_t phys,uint32_t window,uint32_t n)50 static void copy_phys_from_window(CPUXtensaState *env,
51                                   uint32_t phys, uint32_t window, uint32_t n)
52 {
53     assert(phys < env->config->nareg);
54     if (phys + n <= env->config->nareg) {
55         memcpy(env->phys_regs + phys, env->regs + window,
56                n * sizeof(uint32_t));
57     } else {
58         uint32_t n1 = env->config->nareg - phys;
59         memcpy(env->phys_regs + phys, env->regs + window,
60                n1 * sizeof(uint32_t));
61         memcpy(env->phys_regs, env->regs + window + n1,
62                (n - n1) * sizeof(uint32_t));
63     }
64 }
65 
windowbase_bound(unsigned a,const CPUXtensaState * env)66 static inline unsigned windowbase_bound(unsigned a, const CPUXtensaState *env)
67 {
68     return a & (env->config->nareg / 4 - 1);
69 }
70 
windowstart_bit(unsigned a,const CPUXtensaState * env)71 static inline unsigned windowstart_bit(unsigned a, const CPUXtensaState *env)
72 {
73     return 1 << windowbase_bound(a, env);
74 }
75 
xtensa_sync_window_from_phys(CPUXtensaState * env)76 void xtensa_sync_window_from_phys(CPUXtensaState *env)
77 {
78     copy_window_from_phys(env, 0, env->sregs[WINDOW_BASE] * 4, 16);
79 }
80 
xtensa_sync_phys_from_window(CPUXtensaState * env)81 void xtensa_sync_phys_from_window(CPUXtensaState *env)
82 {
83     copy_phys_from_window(env, env->sregs[WINDOW_BASE] * 4, 0, 16);
84 }
85 
xtensa_rotate_window_abs(CPUXtensaState * env,uint32_t position)86 static void xtensa_rotate_window_abs(CPUXtensaState *env, uint32_t position)
87 {
88     xtensa_sync_phys_from_window(env);
89     env->sregs[WINDOW_BASE] = windowbase_bound(position, env);
90     xtensa_sync_window_from_phys(env);
91 }
92 
xtensa_rotate_window(CPUXtensaState * env,uint32_t delta)93 void xtensa_rotate_window(CPUXtensaState *env, uint32_t delta)
94 {
95     xtensa_rotate_window_abs(env, env->sregs[WINDOW_BASE] + delta);
96 }
97 
HELPER(sync_windowbase)98 void HELPER(sync_windowbase)(CPUXtensaState *env)
99 {
100     xtensa_rotate_window_abs(env, env->windowbase_next);
101 }
102 
HELPER(entry)103 void HELPER(entry)(CPUXtensaState *env, uint32_t pc, uint32_t s, uint32_t imm)
104 {
105     int callinc = (env->sregs[PS] & PS_CALLINC) >> PS_CALLINC_SHIFT;
106 
107     env->regs[(callinc << 2) | (s & 3)] = env->regs[s] - imm;
108     env->windowbase_next = env->sregs[WINDOW_BASE] + callinc;
109     env->sregs[WINDOW_START] |= windowstart_bit(env->windowbase_next, env);
110 }
111 
HELPER(window_check)112 void HELPER(window_check)(CPUXtensaState *env, uint32_t pc, uint32_t w)
113 {
114     uint32_t windowbase = windowbase_bound(env->sregs[WINDOW_BASE], env);
115     uint32_t windowstart = xtensa_replicate_windowstart(env) >>
116         (env->sregs[WINDOW_BASE] + 1);
117     uint32_t n = ctz32(windowstart) + 1;
118 
119     assert(n <= w);
120 
121     xtensa_rotate_window(env, n);
122     env->sregs[PS] = (env->sregs[PS] & ~PS_OWB) |
123         (windowbase << PS_OWB_SHIFT) | PS_EXCM;
124     env->sregs[EPC1] = env->pc = pc;
125 
126     switch (ctz32(windowstart >> n)) {
127     case 0:
128         HELPER(exception)(env, EXC_WINDOW_OVERFLOW4);
129         break;
130     case 1:
131         HELPER(exception)(env, EXC_WINDOW_OVERFLOW8);
132         break;
133     default:
134         HELPER(exception)(env, EXC_WINDOW_OVERFLOW12);
135         break;
136     }
137 }
138 
HELPER(test_ill_retw)139 void HELPER(test_ill_retw)(CPUXtensaState *env, uint32_t pc)
140 {
141     int n = (env->regs[0] >> 30) & 0x3;
142     int m = 0;
143     uint32_t windowbase = windowbase_bound(env->sregs[WINDOW_BASE], env);
144     uint32_t windowstart = env->sregs[WINDOW_START];
145 
146     if (windowstart & windowstart_bit(windowbase - 1, env)) {
147         m = 1;
148     } else if (windowstart & windowstart_bit(windowbase - 2, env)) {
149         m = 2;
150     } else if (windowstart & windowstart_bit(windowbase - 3, env)) {
151         m = 3;
152     }
153 
154     if (n == 0 || (m != 0 && m != n)) {
155         qemu_log_mask(LOG_GUEST_ERROR, "Illegal retw instruction(pc = %08x), "
156                       "PS = %08x, m = %d, n = %d\n",
157                       pc, env->sregs[PS], m, n);
158         HELPER(exception_cause)(env, pc, ILLEGAL_INSTRUCTION_CAUSE);
159     }
160 }
161 
HELPER(test_underflow_retw)162 void HELPER(test_underflow_retw)(CPUXtensaState *env, uint32_t pc)
163 {
164     int n = (env->regs[0] >> 30) & 0x3;
165 
166     if (!(env->sregs[WINDOW_START] &
167           windowstart_bit(env->sregs[WINDOW_BASE] - n, env))) {
168         uint32_t windowbase = windowbase_bound(env->sregs[WINDOW_BASE], env);
169 
170         xtensa_rotate_window(env, -n);
171         /* window underflow */
172         env->sregs[PS] = (env->sregs[PS] & ~PS_OWB) |
173             (windowbase << PS_OWB_SHIFT) | PS_EXCM;
174         env->sregs[EPC1] = env->pc = pc;
175 
176         if (n == 1) {
177             HELPER(exception)(env, EXC_WINDOW_UNDERFLOW4);
178         } else if (n == 2) {
179             HELPER(exception)(env, EXC_WINDOW_UNDERFLOW8);
180         } else if (n == 3) {
181             HELPER(exception)(env, EXC_WINDOW_UNDERFLOW12);
182         }
183     }
184 }
185 
HELPER(retw)186 void HELPER(retw)(CPUXtensaState *env, uint32_t a0)
187 {
188     int n = (a0 >> 30) & 0x3;
189 
190     xtensa_rotate_window(env, -n);
191 }
192 
xtensa_restore_owb(CPUXtensaState * env)193 void xtensa_restore_owb(CPUXtensaState *env)
194 {
195     xtensa_rotate_window_abs(env, (env->sregs[PS] & PS_OWB) >> PS_OWB_SHIFT);
196 }
197 
HELPER(restore_owb)198 void HELPER(restore_owb)(CPUXtensaState *env)
199 {
200     xtensa_restore_owb(env);
201 }
202 
HELPER(movsp)203 void HELPER(movsp)(CPUXtensaState *env, uint32_t pc)
204 {
205     if ((env->sregs[WINDOW_START] &
206          (windowstart_bit(env->sregs[WINDOW_BASE] - 3, env) |
207           windowstart_bit(env->sregs[WINDOW_BASE] - 2, env) |
208           windowstart_bit(env->sregs[WINDOW_BASE] - 1, env))) == 0) {
209         HELPER(exception_cause)(env, pc, ALLOCA_CAUSE);
210     }
211 }
212