-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy patharm_exception.S
More file actions
339 lines (287 loc) · 11.7 KB
/
Copy patharm_exception.S
File metadata and controls
339 lines (287 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/****************************************************************************
* arch/arm/src/armv7-m/arm_exception.S
*
* SPDX-License-Identifier: BSD-3-Clause
* SPDX-FileCopyrightText: 2009-2013, 2015-2016, 2018 Gregory Nutt.
* SPDX-FileCopyrightText: 2012 Michael Smith. All rights reserved.
* SPDX-FileContributor: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <arch/irq.h>
#include <arch/armv7-m/nvicpri.h>
#include "chip.h"
#include "exc_return.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* Configuration ************************************************************/
#ifdef CONFIG_ARCH_HIPRI_INTERRUPT
/* In protected mode without an interrupt stack, this interrupt handler will set the
* MSP to the stack pointer of the interrupted thread. If the interrupted thread
* was a privileged thread, that will be the MSP otherwise it will be the PSP. If
* the PSP is used, then the value of the MSP will be invalid when the interrupt
* handler returns because it will be a pointer to an old position in the
* unprivileged stack. Then when the high priority interrupt occurs and uses this
* stale MSP, there will most likely be a system failure.
*
* If the interrupt stack is selected, on the other hand, then the interrupt
* handler will always set the MSP to the interrupt stack. So when the high
* priority interrupt occurs, it will either use the MSP of the last privileged
* thread to run or, in the case of the nested interrupt, the interrupt stack if
* no privileged task has run.
*/
# if defined(CONFIG_BUILD_PROTECTED) && CONFIG_ARCH_INTERRUPTSTACK < 8
# error Interrupt stack must be used with high priority interrupts in protected mode
# endif
#endif
/****************************************************************************
* Public Symbols
****************************************************************************/
.globl exception_common
#ifdef __ghs__
.thumb2
#else
.syntax unified
.thumb
#endif
.file "arm_exception.S"
/****************************************************************************
* .text
****************************************************************************/
/* Common exception handling logic. On entry here, the return stack is on either
* the PSP or the MSP and looks like the following:
*
* REG_XPSR
* REG_R15
* REG_R14
* REG_R12
* REG_R3
* REG_R2
* REG_R1
* MSP->REG_R0
*
* And
* IPSR contains the IRQ number
* R14 Contains the EXC_RETURN value
* We are in handler mode and the current SP is the MSP
*
* If CONFIG_ARCH_FPU is defined, the volatile FP registers and FPSCR are on the
* return stack immediately above REG_XPSR.
*/
.text
.section .text.exception_common
#ifdef __ghs__
.type exception_common, $function
#else
.thumb_func
.type exception_common, function
#endif
exception_common:
.cfi_sections .debug_frame
.cfi_startproc
mrs r0, ipsr /* R0=exception number */
mrs r12, control /* R12=control */
/* Complete the context save */
/* The EXC_RETURN value tells us whether the context is on the MSP or PSP */
tst r14, #EXC_RETURN_PROCESS_STACK /* nonzero if context on process stack */
beq 1f /* Branch if context already on the MSP */
mrs r1, psp /* R1=The process stack pointer (PSP) */
b 2f
1:
mrs r1, msp /* R1=The main stack pointer (MSP) */
sub r2, r1, #SW_XCPT_SIZE /* Reserved stack space */
msr msp, r2
isb sy
2:
mov r2, r1 /* R2=Copy of the main/process stack pointer */
add r2, #HW_XCPT_SIZE /* R2=MSP/PSP before the interrupt was taken */
/* (ignoring the xPSR[9] alignment bit) */
mrs r3, basepri /* R3=Current BASEPRI setting */
#ifdef CONFIG_ARCH_FPU
/* Save the non-volatile FP registers here.
*
* This routine is the only point where we can save these registers; either before
* or after calling arm_doirq. The compiler is free to use them at any time as long
* as they are restored before returning, so we can't assume that we can get at the
* true values of these registers in any routine called from here.
*
* REVISIT: we could do all this saving lazily on the context switch side if we knew
* where to put the registers.
*/
/* Switched-out task including volatile FP registers ? */
tst r14, #EXC_RETURN_STD_CONTEXT
ite eq
vstmdbeq r1!, {s16-s31} /* Save the non-volatile FP context */
subne r1, #(4*SW_FPU_REGS)
#endif
stmdb r1!, {r2-r12,r14} /* Save the remaining registers plus the SP/PRIMASK values */
/* There are two arguments to arm_doirq:
*
* R0 = The IRQ number
* R1 = The top of the stack points to the saved state
*/
#if CONFIG_ARCH_INTERRUPTSTACK < 7
/* If CONFIG_ARCH_INTERRUPTSTACK is not defined, we will reuse the
* interrupted thread's stack. That may mean using either MSP or PSP
* stack for interrupt level processing (in kernel mode).
*/
/* If the interrupt stack is disabled, reserve xcpcontext to ensure
* that signal processing can have a separate xcpcontext to handle
* signal context (reference: arm_schedulesigaction.c):
* ----------------------
* | IRQ XCP context |
* -------------------
* | Signal XCP context |
* ---------------------- <- SP
* also the sp should be restore after arm_doirq()
*/
tst r14, #EXC_RETURN_THREAD_MODE /* Nonzero if context on thread mode */
beq 3f /* Branch if context already on the handle mode */
sub r2, r1, #XCPTCONTEXT_SIZE /* Reserve signal context */
bic r2, r2, #7 /* Get the stack pointer with 8-byte alignment */
mov sp, r2 /* Instantiate the aligned stack */
3:
#endif
mov fp, r1
.cfi_def_cfa r4, 0 /* Register in fp, so we just set fp as frame */
.cfi_offset pc, REG_PC * 4
.cfi_offset sp, REG_SP * 4
.cfi_offset lr, REG_LR * 4
bl arm_doirq /* R0=IRQ, R1=register save (msp) */
/* On return from arm_doirq, R0 will hold a pointer to register context
* array to use for the interrupt return.
*/
ldmia r0!, {r2-r12,r14} /* Recover R4-R12, r14 + 2 temp values */
#ifdef CONFIG_ARCH_FPU
/* Switched-in task including volatile FP registers ? */
tst r14, #EXC_RETURN_STD_CONTEXT
ite eq
vldmiaeq r0!, {s16-s31} /* Recover S16-S31 */
addne r0, #(4*SW_FPU_REGS)
#endif
#ifdef CONFIG_ARMV7M_SP_CONTEXT_RESTORE
/* Check if the desired SP (r2) differs from the implied SP.
* The implied SP after exception return = r0 + hw_frame_size,
* where hw_frame_size depends on whether the frame includes FPU
* state (determined by EXC_RETURN bit 4 in r14).
* If they differ, we must relocate the HW frame so that the final
* SP after hardware pop equals the desired value (r2).
*/
#ifdef CONFIG_ARCH_FPU
/* Determine actual HW frame size from EXC_RETURN:
* bit 4 set (STD_CONTEXT): 8 words = 32 bytes (no FPU)
* bit 4 clear: 26 words = 104 bytes (with FPU)
*/
tst r14, #EXC_RETURN_STD_CONTEXT
ite ne
movne r1, #(4*HW_INT_REGS) /* Standard frame: 32 bytes */
moveq r1, #HW_XCPT_SIZE /* Extended frame: 104 bytes */
#else
mov r1, #HW_XCPT_SIZE /* Always 32 bytes without FPU */
#endif
/* r1 = actual HW frame size in bytes */
add r1, r0, r1 /* r1 = implied SP = r0 + frame_size */
cmp r2, r1 /* desired SP == implied SP? */
beq .Lno_sp_relocate /* Yes, skip relocation */
/* Relocate HW exception frame from r0 to (r2 - frame_size).
* frame_size = r1 - r0. We have r0 (source), r2 (desired SP).
* r4-r11 are already restored. Available scratch: r1, r2, r3.
* r3 holds basepri which we need to preserve — push to MSP.
*
* Copy direction matters for overlapping regions:
* dest < source: forward copy (low to high)
* dest > source: backward copy (high to low)
*/
push {r3} /* Save basepri on MSP */
sub r3, r1, r0 /* r3 = frame_size = implied_SP - src */
sub r2, r2, r3 /* r2 = dest = desired_SP - frame_size */
cmp r2, r0 /* dest vs source? */
bhs .Lsp_copy_backward /* dest >= source: copy backward */
/* Forward copy (dest < source) */
push {r2} /* Save dest start for later */
.Lsp_copy_fwd:
ldr r1, [r0], #4
str r1, [r2], #4
subs r3, r3, #4
bne .Lsp_copy_fwd
pop {r0} /* r0 = dest start = relocated frame */
pop {r3} /* Restore basepri */
b .Lno_sp_relocate
.Lsp_copy_backward:
/* Backward copy (dest >= source) */
push {r2} /* Save dest start for later */
add r0, r0, r3 /* r0 = source end (one past) */
add r2, r2, r3 /* r2 = dest end (one past) */
.Lsp_copy_bwd:
ldr r1, [r0, #-4]! /* Pre-decrement, load */
str r1, [r2, #-4]! /* Pre-decrement, store */
subs r3, r3, #4
bne .Lsp_copy_bwd
pop {r0} /* r0 = dest start = relocated frame */
pop {r3} /* Restore basepri */
.Lno_sp_relocate:
#endif /* CONFIG_ARMV7M_SP_CONTEXT_RESTORE */
/* The EXC_RETURN value tells us whether we are returning on the MSP or PSP
*/
tst r14, #EXC_RETURN_PROCESS_STACK /* Nonzero if context on process stack */
ite eq /* Next two instructions conditional */
msreq msp, r0 /* R0=The main stack pointer */
msrne psp, r0 /* R0=The process stack pointer */
/* Restore the interrupt state */
msr basepri, r3 /* Restore interrupts priority masking */
msr control, r12 /* Restore control */
/* Always return with R14 containing the special value that will: (1)
* return to thread mode, and (2) select the correct stack.
*/
bx r14 /* And return */
.cfi_endproc
.size exception_common, .-exception_common
/****************************************************************************
* Name: g_intstackalloc/g_intstacktop
*
* Description:
* Shouldn't happen
*
****************************************************************************/
#if !defined(CONFIG_SMP) && CONFIG_ARCH_INTERRUPTSTACK > 7
.bss
.global g_intstackalloc
.global g_intstacktop
.balign 8
g_intstackalloc:
.skip ((CONFIG_ARCH_INTERRUPTSTACK + 4) & ~7)
g_intstacktop:
.size g_intstackalloc, .-g_intstackalloc
#endif
.end