2 * Copyright (c) 2011 - 2019, Max Filippov, Open Source and Linux Lab.
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 "qemu/osdep.h"
30 #include "qemu/main-loop.h"
32 #include "exec/helper-proto.h"
33 #include "qemu/host-utils.h"
34 #include "exec/exec-all.h"
36 static void copy_window_from_phys(CPUXtensaState
*env
,
37 uint32_t window
, uint32_t phys
, uint32_t n
)
39 assert(phys
< env
->config
->nareg
);
40 if (phys
+ n
<= env
->config
->nareg
) {
41 memcpy(env
->regs
+ window
, env
->phys_regs
+ phys
,
42 n
* sizeof(uint32_t));
44 uint32_t n1
= env
->config
->nareg
- phys
;
45 memcpy(env
->regs
+ window
, env
->phys_regs
+ phys
,
46 n1
* sizeof(uint32_t));
47 memcpy(env
->regs
+ window
+ n1
, env
->phys_regs
,
48 (n
- n1
) * sizeof(uint32_t));
52 static void copy_phys_from_window(CPUXtensaState
*env
,
53 uint32_t phys
, uint32_t window
, uint32_t n
)
55 assert(phys
< env
->config
->nareg
);
56 if (phys
+ n
<= env
->config
->nareg
) {
57 memcpy(env
->phys_regs
+ phys
, env
->regs
+ window
,
58 n
* sizeof(uint32_t));
60 uint32_t n1
= env
->config
->nareg
- phys
;
61 memcpy(env
->phys_regs
+ phys
, env
->regs
+ window
,
62 n1
* sizeof(uint32_t));
63 memcpy(env
->phys_regs
, env
->regs
+ window
+ n1
,
64 (n
- n1
) * sizeof(uint32_t));
68 static inline unsigned windowbase_bound(unsigned a
, const CPUXtensaState
*env
)
70 return a
& (env
->config
->nareg
/ 4 - 1);
73 static inline unsigned windowstart_bit(unsigned a
, const CPUXtensaState
*env
)
75 return 1 << windowbase_bound(a
, env
);
78 void xtensa_sync_window_from_phys(CPUXtensaState
*env
)
80 copy_window_from_phys(env
, 0, env
->sregs
[WINDOW_BASE
] * 4, 16);
83 void xtensa_sync_phys_from_window(CPUXtensaState
*env
)
85 copy_phys_from_window(env
, env
->sregs
[WINDOW_BASE
] * 4, 0, 16);
88 static void xtensa_rotate_window_abs(CPUXtensaState
*env
, uint32_t position
)
90 xtensa_sync_phys_from_window(env
);
91 env
->sregs
[WINDOW_BASE
] = windowbase_bound(position
, env
);
92 xtensa_sync_window_from_phys(env
);
95 void xtensa_rotate_window(CPUXtensaState
*env
, uint32_t delta
)
97 xtensa_rotate_window_abs(env
, env
->sregs
[WINDOW_BASE
] + delta
);
100 void HELPER(sync_windowbase
)(CPUXtensaState
*env
)
102 xtensa_rotate_window_abs(env
, env
->windowbase_next
);
105 void HELPER(entry
)(CPUXtensaState
*env
, uint32_t pc
, uint32_t s
, uint32_t imm
)
107 int callinc
= (env
->sregs
[PS
] & PS_CALLINC
) >> PS_CALLINC_SHIFT
;
109 env
->regs
[(callinc
<< 2) | (s
& 3)] = env
->regs
[s
] - imm
;
110 env
->windowbase_next
= env
->sregs
[WINDOW_BASE
] + callinc
;
111 env
->sregs
[WINDOW_START
] |= windowstart_bit(env
->windowbase_next
, env
);
114 void HELPER(window_check
)(CPUXtensaState
*env
, uint32_t pc
, uint32_t w
)
116 uint32_t windowbase
= windowbase_bound(env
->sregs
[WINDOW_BASE
], env
);
117 uint32_t windowstart
= xtensa_replicate_windowstart(env
) >>
118 (env
->sregs
[WINDOW_BASE
] + 1);
119 uint32_t n
= ctz32(windowstart
) + 1;
123 xtensa_rotate_window(env
, n
);
124 env
->sregs
[PS
] = (env
->sregs
[PS
] & ~PS_OWB
) |
125 (windowbase
<< PS_OWB_SHIFT
) | PS_EXCM
;
126 env
->sregs
[EPC1
] = env
->pc
= pc
;
128 switch (ctz32(windowstart
>> n
)) {
130 HELPER(exception
)(env
, EXC_WINDOW_OVERFLOW4
);
133 HELPER(exception
)(env
, EXC_WINDOW_OVERFLOW8
);
136 HELPER(exception
)(env
, EXC_WINDOW_OVERFLOW12
);
141 void HELPER(test_ill_retw
)(CPUXtensaState
*env
, uint32_t pc
)
143 int n
= (env
->regs
[0] >> 30) & 0x3;
145 uint32_t windowbase
= windowbase_bound(env
->sregs
[WINDOW_BASE
], env
);
146 uint32_t windowstart
= env
->sregs
[WINDOW_START
];
148 if (windowstart
& windowstart_bit(windowbase
- 1, env
)) {
150 } else if (windowstart
& windowstart_bit(windowbase
- 2, env
)) {
152 } else if (windowstart
& windowstart_bit(windowbase
- 3, env
)) {
156 if (n
== 0 || (m
!= 0 && m
!= n
)) {
157 qemu_log_mask(LOG_GUEST_ERROR
, "Illegal retw instruction(pc = %08x), "
158 "PS = %08x, m = %d, n = %d\n",
159 pc
, env
->sregs
[PS
], m
, n
);
160 HELPER(exception_cause
)(env
, pc
, ILLEGAL_INSTRUCTION_CAUSE
);
164 void HELPER(test_underflow_retw
)(CPUXtensaState
*env
, uint32_t pc
)
166 int n
= (env
->regs
[0] >> 30) & 0x3;
168 if (!(env
->sregs
[WINDOW_START
] &
169 windowstart_bit(env
->sregs
[WINDOW_BASE
] - n
, env
))) {
170 uint32_t windowbase
= windowbase_bound(env
->sregs
[WINDOW_BASE
], env
);
172 xtensa_rotate_window(env
, -n
);
173 /* window underflow */
174 env
->sregs
[PS
] = (env
->sregs
[PS
] & ~PS_OWB
) |
175 (windowbase
<< PS_OWB_SHIFT
) | PS_EXCM
;
176 env
->sregs
[EPC1
] = env
->pc
= pc
;
179 HELPER(exception
)(env
, EXC_WINDOW_UNDERFLOW4
);
181 HELPER(exception
)(env
, EXC_WINDOW_UNDERFLOW8
);
183 HELPER(exception
)(env
, EXC_WINDOW_UNDERFLOW12
);
188 void HELPER(retw
)(CPUXtensaState
*env
, uint32_t a0
)
190 int n
= (a0
>> 30) & 0x3;
192 xtensa_rotate_window(env
, -n
);
195 void xtensa_restore_owb(CPUXtensaState
*env
)
197 xtensa_rotate_window_abs(env
, (env
->sregs
[PS
] & PS_OWB
) >> PS_OWB_SHIFT
);
200 void HELPER(restore_owb
)(CPUXtensaState
*env
)
202 xtensa_restore_owb(env
);
205 void HELPER(movsp
)(CPUXtensaState
*env
, uint32_t pc
)
207 if ((env
->sregs
[WINDOW_START
] &
208 (windowstart_bit(env
->sregs
[WINDOW_BASE
] - 3, env
) |
209 windowstart_bit(env
->sregs
[WINDOW_BASE
] - 2, env
) |
210 windowstart_bit(env
->sregs
[WINDOW_BASE
] - 1, env
))) == 0) {
211 HELPER(exception_cause
)(env
, pc
, ALLOCA_CAUSE
);