2015-04-10 16:44:37 -07:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013-2014 Wind River Systems, Inc.
|
|
|
|
*
|
2017-01-18 17:01:01 -08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2015-04-10 16:44:37 -07:00
|
|
|
*/
|
|
|
|
|
2015-12-04 10:09:39 -05:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @brief Thread context switching for ARM Cortex-M
|
|
|
|
*
|
|
|
|
* This module implements the routines necessary for thread context switching
|
2017-03-11 19:33:29 +03:00
|
|
|
* on ARM Cortex-M CPUs.
|
2015-07-01 17:22:39 -04:00
|
|
|
*/
|
2015-04-10 16:44:37 -07:00
|
|
|
|
2016-11-08 10:36:50 -05:00
|
|
|
#include <kernel_structs.h>
|
|
|
|
#include <offsets_short.h>
|
2015-04-10 16:44:37 -07:00
|
|
|
#include <toolchain.h>
|
2015-05-28 10:56:47 -07:00
|
|
|
#include <arch/cpu.h>
|
2018-08-10 15:43:31 +02:00
|
|
|
#include <syscall.h>
|
2015-04-10 16:44:37 -07:00
|
|
|
|
|
|
|
_ASM_FILE_PROLOGUE
|
|
|
|
|
2019-09-30 12:31:07 -07:00
|
|
|
GTEXT(z_arm_svc)
|
|
|
|
GTEXT(z_arm_pendsv)
|
2019-03-14 09:20:46 -06:00
|
|
|
GTEXT(z_do_kernel_oops)
|
|
|
|
GTEXT(z_arm_do_syscall)
|
2016-09-02 16:20:19 -04:00
|
|
|
GDATA(_k_neg_eagain)
|
2015-04-10 16:44:37 -07:00
|
|
|
|
2016-11-08 10:36:50 -05:00
|
|
|
GDATA(_kernel)
|
2015-04-10 16:44:37 -07:00
|
|
|
|
2015-07-01 17:22:39 -04:00
|
|
|
/**
|
|
|
|
*
|
2015-07-01 17:51:40 -04:00
|
|
|
* @brief PendSV exception handler, handling context switches
|
2015-07-01 17:22:39 -04:00
|
|
|
*
|
2015-08-20 11:04:01 -04:00
|
|
|
* The PendSV exception is the only execution context in the system that can
|
|
|
|
* perform context switching. When an execution context finds out it has to
|
|
|
|
* switch contexts, it pends the PendSV exception.
|
2015-07-01 17:22:39 -04:00
|
|
|
*
|
|
|
|
* When PendSV is pended, the decision that a context switch must happen has
|
2019-09-30 12:31:07 -07:00
|
|
|
* already been taken. In other words, when z_arm_pendsv() runs, we *know* we
|
|
|
|
* have to swap *something*.
|
2019-09-27 09:44:35 +09:00
|
|
|
*
|
|
|
|
* For Cortex-R, PendSV exception is not supported by the architecture and this
|
|
|
|
* function is directly called either by _IntExit in case of preemption, or
|
|
|
|
* z_arm_svc in case of cooperative switching.
|
2015-07-01 17:22:39 -04:00
|
|
|
*/
|
2015-04-10 16:44:37 -07:00
|
|
|
|
2019-09-30 12:31:07 -07:00
|
|
|
SECTION_FUNC(TEXT, z_arm_pendsv)
|
2015-04-10 16:44:37 -07:00
|
|
|
|
2018-07-04 08:03:03 -05:00
|
|
|
#ifdef CONFIG_TRACING
|
2017-05-11 13:29:15 -07:00
|
|
|
/* Register the context switch */
|
2019-01-31 16:31:01 -06:00
|
|
|
push {r0, lr}
|
2019-09-19 09:25:19 +02:00
|
|
|
bl sys_trace_thread_switched_out
|
2018-02-06 23:47:58 +01:00
|
|
|
#if defined(CONFIG_ARMV6_M_ARMV8_M_BASELINE)
|
2019-01-31 16:31:01 -06:00
|
|
|
pop {r0, r1}
|
|
|
|
mov lr, r1
|
2017-05-11 13:29:15 -07:00
|
|
|
#else
|
2019-01-31 16:31:01 -06:00
|
|
|
pop {r0, lr}
|
2019-02-06 15:26:27 +01:00
|
|
|
#endif /* CONFIG_ARMV6_M_ARMV8_M_BASELINE */
|
2018-04-06 07:48:53 -04:00
|
|
|
#endif /* CONFIG_TRACING */
|
2015-08-11 13:33:26 -05:00
|
|
|
|
2016-11-08 10:36:50 -05:00
|
|
|
/* load _kernel into r1 and current k_thread into r2 */
|
|
|
|
ldr r1, =_kernel
|
|
|
|
ldr r2, [r1, #_kernel_offset_to_current]
|
2015-04-10 16:44:37 -07:00
|
|
|
|
2017-03-27 15:35:09 +01:00
|
|
|
/* addr of callee-saved regs in thread in r0 */
|
2016-11-08 10:36:50 -05:00
|
|
|
ldr r0, =_thread_offset_to_callee_saved
|
2016-10-05 19:43:36 -03:00
|
|
|
add r0, r2
|
2015-04-10 16:44:37 -07:00
|
|
|
|
2017-03-27 15:35:09 +01:00
|
|
|
/* save callee-saved + psp in thread */
|
2018-06-25 09:15:14 -04:00
|
|
|
#if defined(CONFIG_CPU_CORTEX_M)
|
2015-04-10 16:44:37 -07:00
|
|
|
mrs ip, PSP
|
2018-06-25 09:15:14 -04:00
|
|
|
#endif
|
2015-04-10 16:44:37 -07:00
|
|
|
|
2018-02-06 23:47:58 +01:00
|
|
|
#if defined(CONFIG_ARMV6_M_ARMV8_M_BASELINE)
|
2016-10-05 19:43:36 -03:00
|
|
|
/* Store current r4-r7 */
|
|
|
|
stmea r0!, {r4-r7}
|
|
|
|
/* copy r8-r12 into r3-r7 */
|
|
|
|
mov r3, r8
|
|
|
|
mov r4, r9
|
|
|
|
mov r5, r10
|
|
|
|
mov r6, r11
|
|
|
|
mov r7, ip
|
|
|
|
/* store r8-12 */
|
|
|
|
stmea r0!, {r3-r7}
|
2018-02-06 23:47:58 +01:00
|
|
|
#elif defined(CONFIG_ARMV7_M_ARMV8_M_MAINLINE)
|
2016-10-05 19:43:36 -03:00
|
|
|
stmia r0, {v1-v8, ip}
|
2016-05-13 14:22:46 -04:00
|
|
|
#ifdef CONFIG_FP_SHARING
|
2019-04-26 21:14:02 +02:00
|
|
|
/* Assess whether switched-out thread had been using the FP registers. */
|
|
|
|
ldr r0, =0x10 /* EXC_RETURN.F_Type Mask */
|
|
|
|
tst lr, r0 /* EXC_RETURN & EXC_RETURN.F_Type_Msk */
|
|
|
|
beq out_fp_active
|
|
|
|
/* FP context inactive: clear FP state */
|
|
|
|
ldr r0, [r2, #_thread_offset_to_mode]
|
|
|
|
bic r0, #0x4 /* _current->arch.mode &= ~(CONTROL_FPCA_Msk) */
|
|
|
|
b out_fp_endif
|
|
|
|
|
|
|
|
out_fp_active:
|
|
|
|
/* FP context active: set FP state and store callee-saved registers */
|
2016-11-08 10:36:50 -05:00
|
|
|
add r0, r2, #_thread_offset_to_preempt_float
|
2016-05-13 14:22:46 -04:00
|
|
|
vstmia r0, {s16-s31}
|
2019-04-26 21:14:02 +02:00
|
|
|
ldr r0, [r2, #_thread_offset_to_mode]
|
|
|
|
orrs r0, r0, #0x4 /* _current->arch.mode |= CONTROL_FPCA_Msk */
|
|
|
|
|
|
|
|
out_fp_endif:
|
|
|
|
str r0, [r2, #_thread_offset_to_mode]
|
2016-10-05 19:43:36 -03:00
|
|
|
#endif /* CONFIG_FP_SHARING */
|
2018-06-25 09:15:14 -04:00
|
|
|
#elif defined(CONFIG_ARMV7_R)
|
|
|
|
/* Store rest of process context */
|
|
|
|
mrs r12, SPSR
|
|
|
|
stm r0, {r4-r12,sp,lr}^
|
2016-12-31 13:18:25 +00:00
|
|
|
#else
|
|
|
|
#error Unknown ARM architecture
|
2019-04-27 05:26:17 +02:00
|
|
|
#endif /* CONFIG_ARMV6_M_ARMV8_M_BASELINE */
|
|
|
|
|
|
|
|
/* Protect the kernel state while we play with the thread lists */
|
|
|
|
#if defined(CONFIG_ARMV6_M_ARMV8_M_BASELINE)
|
|
|
|
cpsid i
|
|
|
|
#elif defined(CONFIG_ARMV7_M_ARMV8_M_MAINLINE)
|
|
|
|
movs.n r0, #_EXC_IRQ_DEFAULT_PRIO
|
|
|
|
msr BASEPRI, r0
|
|
|
|
isb /* Make the effect of disabling interrupts be realized immediately */
|
2018-06-25 09:15:14 -04:00
|
|
|
#elif defined(CONFIG_ARMV7_R)
|
|
|
|
/*
|
2019-09-21 18:05:17 -07:00
|
|
|
* Interrupts are still disabled from z_arch_swap so empty clause
|
2018-06-25 09:15:14 -04:00
|
|
|
* here to avoid the preprocessor error below
|
|
|
|
*/
|
2019-04-27 05:26:17 +02:00
|
|
|
#else
|
|
|
|
#error Unknown ARM architecture
|
2018-02-06 23:47:58 +01:00
|
|
|
#endif /* CONFIG_ARMV6_M_ARMV8_M_BASELINE */
|
2016-05-13 14:22:46 -04:00
|
|
|
|
2015-04-10 16:44:37 -07:00
|
|
|
/*
|
|
|
|
* Prepare to clear PendSV with interrupts unlocked, but
|
|
|
|
* don't clear it yet. PendSV must not be cleared until
|
|
|
|
* the new thread is context-switched in since all decisions
|
|
|
|
* to pend PendSV have been taken with the current kernel
|
|
|
|
* state and this is what we're handling currently.
|
|
|
|
*/
|
2018-06-25 09:15:14 -04:00
|
|
|
#if defined(CONFIG_CPU_CORTEX_M)
|
2016-10-01 18:49:36 -04:00
|
|
|
ldr v4, =_SCS_ICSR
|
|
|
|
ldr v3, =_SCS_ICSR_UNPENDSV
|
2018-06-25 09:15:14 -04:00
|
|
|
#endif
|
2015-04-10 16:44:37 -07:00
|
|
|
|
kernel/arch: enhance the "ready thread" cache
The way the ready thread cache was implemented caused it to not always
be "hot", i.e. there could be some misses, which happened when the
cached thread was taken out of the ready queue. When that happened, it
was not replaced immediately, since doing so could mean that the
replacement might not run because the flow could be interrupted and
another thread could take its place. This was the more conservative
approach that insured that moving a thread to the cache would never be
wasted.
However, this caused two problems:
1. The cache could not be refilled until another thread context-switched
in, since there was no thread in the cache to compare priorities
against.
2. Interrupt exit code would always have to call into C to find what
thread to run when the current thread was not coop and did not have the
scheduler locked. Furthermore, it was possible for this code path to
encounter a cold cache and then it had to find out what thread to run
the long way.
To fix this, filling the cache is now more aggressive, i.e. the next
thread to put in the cache is found even in the case the current cached
thread is context-switched out. This ensures the interrupt exit code is
much faster on the slow path. In addition, since finding the next thread
to run is now always "get it from the cache", which is a simple fetch
from memory (_kernel.ready_q.cache), there is no need to call the more
complex C code.
On the ARM FRDM K64F board, this improvement is seen:
Before:
1- Measure time to switch from ISR back to interrupted task
switching time is 215 tcs = 1791 nsec
2- Measure time from ISR to executing a different task (rescheduled)
switch time is 315 tcs = 2625 nsec
After:
1- Measure time to switch from ISR back to interrupted task
switching time is 130 tcs = 1083 nsec
2- Measure time from ISR to executing a different task (rescheduled)
switch time is 225 tcs = 1875 nsec
These are the most dramatic improvements, but most of the numbers
generated by the latency_measure test are improved.
Fixes ZEP-1401.
Change-Id: I2eaac147048b1ec71a93bd0a285e743a39533973
Signed-off-by: Benjamin Walsh <benjamin.walsh@windriver.com>
2016-12-02 10:37:27 -05:00
|
|
|
/* _kernel is still in r1 */
|
2015-04-10 16:44:37 -07:00
|
|
|
|
kernel/arch: enhance the "ready thread" cache
The way the ready thread cache was implemented caused it to not always
be "hot", i.e. there could be some misses, which happened when the
cached thread was taken out of the ready queue. When that happened, it
was not replaced immediately, since doing so could mean that the
replacement might not run because the flow could be interrupted and
another thread could take its place. This was the more conservative
approach that insured that moving a thread to the cache would never be
wasted.
However, this caused two problems:
1. The cache could not be refilled until another thread context-switched
in, since there was no thread in the cache to compare priorities
against.
2. Interrupt exit code would always have to call into C to find what
thread to run when the current thread was not coop and did not have the
scheduler locked. Furthermore, it was possible for this code path to
encounter a cold cache and then it had to find out what thread to run
the long way.
To fix this, filling the cache is now more aggressive, i.e. the next
thread to put in the cache is found even in the case the current cached
thread is context-switched out. This ensures the interrupt exit code is
much faster on the slow path. In addition, since finding the next thread
to run is now always "get it from the cache", which is a simple fetch
from memory (_kernel.ready_q.cache), there is no need to call the more
complex C code.
On the ARM FRDM K64F board, this improvement is seen:
Before:
1- Measure time to switch from ISR back to interrupted task
switching time is 215 tcs = 1791 nsec
2- Measure time from ISR to executing a different task (rescheduled)
switch time is 315 tcs = 2625 nsec
After:
1- Measure time to switch from ISR back to interrupted task
switching time is 130 tcs = 1083 nsec
2- Measure time from ISR to executing a different task (rescheduled)
switch time is 225 tcs = 1875 nsec
These are the most dramatic improvements, but most of the numbers
generated by the latency_measure test are improved.
Fixes ZEP-1401.
Change-Id: I2eaac147048b1ec71a93bd0a285e743a39533973
Signed-off-by: Benjamin Walsh <benjamin.walsh@windriver.com>
2016-12-02 10:37:27 -05:00
|
|
|
/* fetch the thread to run from the ready queue cache */
|
2019-03-13 11:26:28 -05:00
|
|
|
ldr r2, [r1, #_kernel_offset_to_ready_q_cache]
|
2015-04-10 16:44:37 -07:00
|
|
|
|
2016-11-08 10:36:50 -05:00
|
|
|
str r2, [r1, #_kernel_offset_to_current]
|
2015-04-10 16:44:37 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Clear PendSV so that if another interrupt comes in and
|
2019-02-28 15:42:50 +01:00
|
|
|
* decides, with the new kernel state based on the new thread
|
|
|
|
* being context-switched in, that it needs to reschedule, it
|
2015-04-10 16:44:37 -07:00
|
|
|
* will take, but that previously pended PendSVs do not take,
|
|
|
|
* since they were based on the previous kernel state and this
|
|
|
|
* has been handled.
|
|
|
|
*/
|
|
|
|
|
2016-10-01 18:49:36 -04:00
|
|
|
/* _SCS_ICSR is still in v4 and _SCS_ICSR_UNPENDSV in v3 */
|
2018-06-25 09:15:14 -04:00
|
|
|
#if defined(CONFIG_CPU_CORTEX_M)
|
2016-10-01 18:49:36 -04:00
|
|
|
str v3, [v4, #0]
|
2018-06-25 09:15:14 -04:00
|
|
|
#endif
|
2015-04-10 16:44:37 -07:00
|
|
|
|
2016-10-05 19:43:36 -03:00
|
|
|
/* Restore previous interrupt disable state (irq_lock key) */
|
2018-07-25 11:47:21 -07:00
|
|
|
#if (defined(CONFIG_CPU_CORTEX_M0PLUS) || defined(CONFIG_CPU_CORTEX_M0)) && \
|
|
|
|
_thread_offset_to_basepri > 124
|
|
|
|
/* Doing it this way since the offset to thread->arch.basepri can in
|
|
|
|
* some configurations be larger than the maximum of 124 for ldr/str
|
|
|
|
* immediate offsets.
|
|
|
|
*/
|
|
|
|
ldr r4, =_thread_offset_to_basepri
|
|
|
|
adds r4, r2, r4
|
|
|
|
|
|
|
|
ldr r0, [r4]
|
|
|
|
movs.n r3, #0
|
|
|
|
str r3, [r4]
|
|
|
|
#else
|
2016-11-08 10:36:50 -05:00
|
|
|
ldr r0, [r2, #_thread_offset_to_basepri]
|
2018-06-25 09:15:14 -04:00
|
|
|
movs r3, #0
|
2016-11-08 10:36:50 -05:00
|
|
|
str r3, [r2, #_thread_offset_to_basepri]
|
2018-07-25 11:47:21 -07:00
|
|
|
#endif
|
2016-10-05 19:43:36 -03:00
|
|
|
|
2018-02-06 23:47:58 +01:00
|
|
|
#if defined(CONFIG_ARMV6_M_ARMV8_M_BASELINE)
|
2016-10-05 19:43:36 -03:00
|
|
|
/* BASEPRI not available, previous interrupt disable state
|
|
|
|
* maps to PRIMASK.
|
|
|
|
*
|
|
|
|
* Only enable interrupts if value is 0, meaning interrupts
|
|
|
|
* were enabled before irq_lock was called.
|
|
|
|
*/
|
|
|
|
cmp r0, #0
|
|
|
|
bne _thread_irq_disabled
|
|
|
|
cpsie i
|
|
|
|
_thread_irq_disabled:
|
|
|
|
|
2016-11-08 10:36:50 -05:00
|
|
|
ldr r4, =_thread_offset_to_callee_saved
|
2016-10-05 19:43:36 -03:00
|
|
|
adds r0, r2, r4
|
|
|
|
|
|
|
|
/* restore r4-r12 for new thread */
|
|
|
|
/* first restore r8-r12 located after r4-r7 (4*4bytes) */
|
|
|
|
adds r0, #16
|
|
|
|
ldmia r0!, {r3-r7}
|
|
|
|
/* move to correct registers */
|
|
|
|
mov r8, r3
|
|
|
|
mov r9, r4
|
|
|
|
mov r10, r5
|
|
|
|
mov r11, r6
|
|
|
|
mov ip, r7
|
|
|
|
/* restore r4-r7, go back 9*4 bytes to the start of the stored block */
|
|
|
|
subs r0, #36
|
|
|
|
ldmia r0!, {r4-r7}
|
2018-02-06 23:47:58 +01:00
|
|
|
#elif defined(CONFIG_ARMV7_M_ARMV8_M_MAINLINE)
|
2016-10-05 19:43:36 -03:00
|
|
|
/* restore BASEPRI for the incoming thread */
|
2015-04-10 16:44:37 -07:00
|
|
|
msr BASEPRI, r0
|
|
|
|
|
2016-05-13 14:22:46 -04:00
|
|
|
#ifdef CONFIG_FP_SHARING
|
2019-04-26 21:14:02 +02:00
|
|
|
/* Assess whether switched-in thread had been using the FP registers. */
|
|
|
|
ldr r0, [r2, #_thread_offset_to_mode]
|
|
|
|
tst r0, #0x04 /* thread.arch.mode & CONTROL.FPCA Msk */
|
|
|
|
bne in_fp_active
|
|
|
|
/* FP context inactive for swapped-in thread:
|
|
|
|
* - reset FPSCR to 0
|
|
|
|
* - set EXC_RETURN.F_Type (prevents FP frame un-stacking when returning
|
|
|
|
* from pendSV)
|
|
|
|
*/
|
|
|
|
movs.n r3, #0
|
|
|
|
vmsr fpscr, r3
|
|
|
|
orrs lr, lr, #0x10 /* EXC_RETURN & EXC_RETURN.F_Type_Msk */
|
|
|
|
b in_fp_endif
|
|
|
|
|
|
|
|
in_fp_active:
|
|
|
|
/* FP context active:
|
|
|
|
* - clear EXC_RETURN.F_Type
|
|
|
|
* - FPSCR and caller-saved registers will be restored automatically
|
|
|
|
* - restore callee-saved FP registers
|
|
|
|
*/
|
|
|
|
bic lr, #0x10 /* EXC_RETURN | (~EXC_RETURN.F_Type_Msk) */
|
2016-11-08 10:36:50 -05:00
|
|
|
add r0, r2, #_thread_offset_to_preempt_float
|
2016-05-13 14:22:46 -04:00
|
|
|
vldmia r0, {s16-s31}
|
2019-04-26 21:14:02 +02:00
|
|
|
in_fp_endif:
|
|
|
|
/* Clear CONTROL.FPCA that may have been set by FP instructions */
|
|
|
|
mrs r3, CONTROL
|
|
|
|
bic r3, #0x4 /* CONTROL.FPCA Msk */
|
|
|
|
msr CONTROL, r3
|
|
|
|
isb
|
2016-05-13 14:22:46 -04:00
|
|
|
#endif
|
|
|
|
|
2018-12-04 16:44:21 +01:00
|
|
|
#if defined (CONFIG_ARM_MPU)
|
|
|
|
/* Re-program dynamic memory map */
|
|
|
|
push {r2,lr}
|
|
|
|
ldr r0, =_kernel
|
|
|
|
ldr r0, [r0, #_kernel_offset_to_current]
|
2019-09-30 12:31:07 -07:00
|
|
|
bl z_arm_configure_dynamic_mpu_regions
|
2018-12-04 16:44:21 +01:00
|
|
|
pop {r2,lr}
|
|
|
|
#endif
|
2017-03-29 11:31:45 +01:00
|
|
|
|
2017-07-07 20:29:30 +08:00
|
|
|
#ifdef CONFIG_USERSPACE
|
2017-12-08 12:22:49 -06:00
|
|
|
/* restore mode */
|
|
|
|
ldr r0, [r2, #_thread_offset_to_mode]
|
|
|
|
mrs r3, CONTROL
|
|
|
|
bic r3, #1
|
|
|
|
orr r3, r0
|
|
|
|
msr CONTROL, r3
|
|
|
|
|
2018-04-06 16:03:47 -07:00
|
|
|
/* ISB is not strictly necessary here (stack pointer is not being
|
|
|
|
* touched), but it's recommended to avoid executing pre-fetched
|
|
|
|
* instructions with the previous privilege.
|
|
|
|
*/
|
|
|
|
isb
|
|
|
|
|
2017-12-08 12:22:49 -06:00
|
|
|
#endif
|
2017-07-07 20:29:30 +08:00
|
|
|
|
2017-03-27 15:35:09 +01:00
|
|
|
/* load callee-saved + psp from thread */
|
2016-11-08 10:36:50 -05:00
|
|
|
add r0, r2, #_thread_offset_to_callee_saved
|
2015-04-10 16:44:37 -07:00
|
|
|
ldmia r0, {v1-v8, ip}
|
2018-06-25 09:15:14 -04:00
|
|
|
#elif defined(CONFIG_ARMV7_R)
|
|
|
|
_thread_irq_disabled:
|
|
|
|
/* load _kernel into r1 and current k_thread into r2 */
|
|
|
|
ldr r1, =_kernel
|
|
|
|
ldr r2, [r1, #_kernel_offset_to_current]
|
|
|
|
|
|
|
|
/* addr of callee-saved regs in thread in r0 */
|
|
|
|
ldr r0, =_thread_offset_to_callee_saved
|
|
|
|
add r0, r2
|
|
|
|
|
|
|
|
/* restore r4-r12 for incoming thread, plus system sp and lr */
|
|
|
|
ldm r0, {r4-r12,sp,lr}^
|
|
|
|
msr SPSR_fsxc, r12
|
2016-12-31 13:18:25 +00:00
|
|
|
#else
|
|
|
|
#error Unknown ARM architecture
|
2018-02-06 23:47:58 +01:00
|
|
|
#endif /* CONFIG_ARMV6_M_ARMV8_M_BASELINE */
|
2016-10-05 19:43:36 -03:00
|
|
|
|
2018-10-08 15:48:49 +02:00
|
|
|
#ifdef CONFIG_BUILTIN_STACK_GUARD
|
|
|
|
/* clear stack pointer limit before setting the PSP */
|
2019-02-11 08:53:13 +01:00
|
|
|
mov r0, #0
|
|
|
|
msr PSPLIM, r0
|
|
|
|
#endif /* CONFIG_BUILTIN_STACK_GUARD */
|
2018-10-08 15:48:49 +02:00
|
|
|
|
2018-06-25 09:15:14 -04:00
|
|
|
#if defined(CONFIG_CPU_CORTEX_M)
|
2015-04-10 16:44:37 -07:00
|
|
|
msr PSP, ip
|
2018-06-25 09:15:14 -04:00
|
|
|
#endif
|
2015-04-10 16:44:37 -07:00
|
|
|
|
2018-10-08 15:48:49 +02:00
|
|
|
#ifdef CONFIG_BUILTIN_STACK_GUARD
|
|
|
|
/* r2 contains k_thread */
|
|
|
|
add r0, r2, #0
|
|
|
|
push {r2, lr}
|
2019-03-13 11:26:28 -05:00
|
|
|
bl configure_builtin_stack_guard
|
2018-10-08 15:48:49 +02:00
|
|
|
pop {r2, lr}
|
|
|
|
#endif /* CONFIG_BUILTIN_STACK_GUARD */
|
|
|
|
|
2017-05-03 13:11:51 +05:30
|
|
|
#ifdef CONFIG_EXECUTION_BENCHMARKING
|
2019-01-31 16:31:01 -06:00
|
|
|
push {r0, lr}
|
2017-08-31 20:05:05 +05:30
|
|
|
bl read_timer_end_of_swap
|
2018-02-06 23:47:58 +01:00
|
|
|
#if defined(CONFIG_ARMV6_M_ARMV8_M_BASELINE)
|
2019-01-31 16:31:01 -06:00
|
|
|
pop {r0, r1}
|
|
|
|
mov lr,r1
|
2017-05-03 13:11:51 +05:30
|
|
|
#else
|
2019-01-31 16:31:01 -06:00
|
|
|
pop {r0, lr}
|
2018-02-06 23:47:58 +01:00
|
|
|
#endif /* CONFIG_ARMV6_M_ARMV8_M_BASELINE */
|
2019-01-31 16:31:01 -06:00
|
|
|
|
2017-08-23 16:02:00 +05:30
|
|
|
#endif /* CONFIG_EXECUTION_BENCHMARKING */
|
2017-05-03 13:11:51 +05:30
|
|
|
|
2018-10-26 16:54:16 +02:00
|
|
|
#ifdef CONFIG_TRACING
|
|
|
|
/* Register the context switch */
|
2019-01-31 16:31:01 -06:00
|
|
|
push {r0, lr}
|
2019-09-19 09:25:19 +02:00
|
|
|
bl sys_trace_thread_switched_in
|
2018-10-26 16:54:16 +02:00
|
|
|
#if defined(CONFIG_ARMV6_M_ARMV8_M_BASELINE)
|
2019-01-31 16:31:01 -06:00
|
|
|
pop {r0, r1}
|
|
|
|
mov lr, r1
|
2018-10-26 16:54:16 +02:00
|
|
|
#else
|
2019-01-31 16:31:01 -06:00
|
|
|
pop {r0, lr}
|
|
|
|
#endif
|
2018-10-26 16:54:16 +02:00
|
|
|
#endif /* CONFIG_TRACING */
|
|
|
|
|
2019-09-27 09:44:35 +09:00
|
|
|
/*
|
|
|
|
* Cortex-M: return from PendSV exception
|
|
|
|
* Cortex-R: return to the caller (_IntExit or z_arm_svc)
|
|
|
|
*/
|
2015-04-10 16:44:37 -07:00
|
|
|
bx lr
|
|
|
|
|
2018-02-06 23:47:58 +01:00
|
|
|
#if defined(CONFIG_ARMV6_M_ARMV8_M_BASELINE)
|
2019-02-11 11:07:58 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @brief Service call handler
|
|
|
|
*
|
|
|
|
* The service call (svc) is used in the following occasions:
|
|
|
|
* - IRQ offloading
|
|
|
|
* - Kernel run-time exceptions
|
|
|
|
*
|
|
|
|
* @return N/A
|
|
|
|
*/
|
2019-09-30 12:31:07 -07:00
|
|
|
SECTION_FUNC(TEXT, z_arm_svc)
|
2017-06-01 13:36:44 -07:00
|
|
|
/* Use EXC_RETURN state to find out if stack frame is on the
|
|
|
|
* MSP or PSP
|
|
|
|
*/
|
|
|
|
ldr r0, =0x4
|
|
|
|
mov r1, lr
|
|
|
|
tst r1, r0
|
|
|
|
beq _stack_frame_msp
|
|
|
|
mrs r0, PSP
|
|
|
|
bne _stack_frame_endif
|
|
|
|
_stack_frame_msp:
|
|
|
|
mrs r0, MSP
|
|
|
|
_stack_frame_endif:
|
|
|
|
|
|
|
|
/* Figure out what SVC call number was invoked */
|
|
|
|
ldr r1, [r0, #24] /* grab address of PC from stack frame */
|
|
|
|
/* SVC is a two-byte instruction, point to it and read encoding */
|
|
|
|
subs r1, r1, #2
|
|
|
|
ldrb r1, [r1, #0]
|
|
|
|
|
|
|
|
/*
|
|
|
|
* grab service call number:
|
|
|
|
* 1: irq_offload (if configured)
|
|
|
|
* 2: kernel panic or oops (software generated fatal exception)
|
|
|
|
* Planned implementation of system calls for memory protection will
|
|
|
|
* expand this case.
|
|
|
|
*/
|
|
|
|
|
|
|
|
cmp r1, #2
|
|
|
|
beq _oops
|
|
|
|
|
2019-05-17 15:00:32 -04:00
|
|
|
#if defined(CONFIG_IRQ_OFFLOAD)
|
2019-01-31 16:31:01 -06:00
|
|
|
push {r0, lr}
|
2019-03-14 09:20:46 -06:00
|
|
|
bl z_irq_do_offload /* call C routine which executes the offload */
|
2019-01-31 16:31:01 -06:00
|
|
|
pop {r0, r1}
|
|
|
|
mov lr, r1
|
|
|
|
#endif /* CONFIG_IRQ_OFFLOAD */
|
2017-06-01 13:36:44 -07:00
|
|
|
|
2019-09-30 12:31:07 -07:00
|
|
|
/* exception return is done in z_arm_int_exit() */
|
|
|
|
b z_arm_int_exit
|
2017-06-01 13:36:44 -07:00
|
|
|
|
|
|
|
_oops:
|
2019-01-31 16:31:01 -06:00
|
|
|
push {r0, lr}
|
2019-03-14 09:20:46 -06:00
|
|
|
bl z_do_kernel_oops
|
2019-01-31 16:31:01 -06:00
|
|
|
pop {r0, pc}
|
2017-06-01 13:36:44 -07:00
|
|
|
|
2018-02-06 23:47:58 +01:00
|
|
|
#elif defined(CONFIG_ARMV7_M_ARMV8_M_MAINLINE)
|
2019-02-11 11:07:58 +01:00
|
|
|
|
2015-07-01 17:22:39 -04:00
|
|
|
/**
|
|
|
|
*
|
2015-07-01 17:51:40 -04:00
|
|
|
* @brief Service call handler
|
2015-07-01 17:22:39 -04:00
|
|
|
*
|
2019-02-11 11:07:58 +01:00
|
|
|
* The service call (svc) is used in the following occasions:
|
|
|
|
* - IRQ offloading
|
|
|
|
* - Kernel run-time exceptions
|
|
|
|
* - System Calls (User mode)
|
2015-07-01 17:22:39 -04:00
|
|
|
*
|
2015-07-01 17:29:04 -04:00
|
|
|
* @return N/A
|
2015-07-01 17:22:39 -04:00
|
|
|
*/
|
2019-09-30 12:31:07 -07:00
|
|
|
SECTION_FUNC(TEXT, z_arm_svc)
|
2015-11-17 14:08:45 -08:00
|
|
|
tst lr, #0x4 /* did we come from thread mode ? */
|
|
|
|
ite eq /* if zero (equal), came from handler mode */
|
|
|
|
mrseq r0, MSP /* handler mode, stack frame is on MSP */
|
|
|
|
mrsne r0, PSP /* thread mode, stack frame is on PSP */
|
|
|
|
|
2017-04-19 12:53:48 -07:00
|
|
|
ldr r1, [r0, #24] /* grab address of PC from stack frame */
|
2019-02-28 15:42:50 +01:00
|
|
|
/* SVC is a two-byte instruction, point to it and read encoding */
|
2017-04-19 12:53:48 -07:00
|
|
|
ldrh r1, [r1, #-2]
|
2015-11-17 14:08:45 -08:00
|
|
|
|
|
|
|
/*
|
2017-04-19 12:53:48 -07:00
|
|
|
* grab service call number:
|
2018-05-23 13:33:18 +02:00
|
|
|
* 0: Unused
|
2017-04-19 12:53:48 -07:00
|
|
|
* 1: irq_offload (if configured)
|
|
|
|
* 2: kernel panic or oops (software generated fatal exception)
|
2019-02-28 15:42:50 +01:00
|
|
|
* 3: System call (if user mode supported)
|
2017-04-19 12:53:48 -07:00
|
|
|
* Planned implementation of system calls for memory protection will
|
|
|
|
* expand this case.
|
2015-11-17 14:08:45 -08:00
|
|
|
*/
|
2017-04-19 12:53:48 -07:00
|
|
|
ands r1, #0xff
|
2019-05-17 10:21:37 -04:00
|
|
|
#if defined(CONFIG_USERSPACE)
|
2017-12-08 12:22:49 -06:00
|
|
|
mrs r2, CONTROL
|
|
|
|
|
|
|
|
cmp r1, #3
|
|
|
|
beq _do_syscall
|
|
|
|
|
|
|
|
/*
|
|
|
|
* check that we are privileged before invoking other SVCs
|
|
|
|
* oops if we are unprivileged
|
|
|
|
*/
|
|
|
|
tst r2, #0x1
|
|
|
|
bne _oops
|
|
|
|
|
|
|
|
#endif
|
2015-11-17 14:08:45 -08:00
|
|
|
|
2017-04-19 12:53:48 -07:00
|
|
|
cmp r1, #2
|
|
|
|
beq _oops
|
|
|
|
|
2019-05-17 15:00:32 -04:00
|
|
|
#if defined(CONFIG_IRQ_OFFLOAD)
|
2019-01-31 16:31:01 -06:00
|
|
|
push {r0, lr}
|
2019-03-14 09:20:46 -06:00
|
|
|
bl z_irq_do_offload /* call C routine which executes the offload */
|
2019-01-31 16:31:01 -06:00
|
|
|
pop {r0, lr}
|
2015-11-17 14:08:45 -08:00
|
|
|
|
2019-09-30 12:31:07 -07:00
|
|
|
/* exception return is done in z_arm_int_exit() */
|
|
|
|
b z_arm_int_exit
|
2017-04-19 12:53:48 -07:00
|
|
|
#endif
|
2015-11-17 14:08:45 -08:00
|
|
|
|
2017-04-19 12:53:48 -07:00
|
|
|
_oops:
|
2019-01-31 16:31:01 -06:00
|
|
|
push {r0, lr}
|
2019-03-14 09:20:46 -06:00
|
|
|
bl z_do_kernel_oops
|
2019-01-31 16:31:01 -06:00
|
|
|
pop {r0, pc}
|
2017-04-19 12:53:48 -07:00
|
|
|
|
2019-05-17 10:21:37 -04:00
|
|
|
#if defined(CONFIG_USERSPACE)
|
2017-12-08 12:22:49 -06:00
|
|
|
/*
|
2019-06-28 11:49:19 +02:00
|
|
|
* System call will setup a jump to the z_arm_do_syscall() function
|
2017-12-08 12:22:49 -06:00
|
|
|
* when the SVC returns via the bx lr.
|
|
|
|
*
|
|
|
|
* There is some trickery involved here because we have to preserve
|
2019-02-28 15:42:50 +01:00
|
|
|
* the original PC value so that we can return back to the caller of
|
2017-12-08 12:22:49 -06:00
|
|
|
* the SVC.
|
|
|
|
*
|
|
|
|
* On SVC exeption, the stack looks like the following:
|
|
|
|
* r0 - r1 - r2 - r3 - r12 - LR - PC - PSR
|
|
|
|
*
|
2018-04-13 16:03:24 -05:00
|
|
|
* Registers look like:
|
|
|
|
* r0 - arg1
|
|
|
|
* r1 - arg2
|
|
|
|
* r2 - arg3
|
|
|
|
* r3 - arg4
|
|
|
|
* r4 - arg5
|
|
|
|
* r5 - arg6
|
|
|
|
* r6 - call_id
|
2018-06-01 08:49:14 +02:00
|
|
|
* r8 - saved link register
|
2017-12-08 12:22:49 -06:00
|
|
|
*/
|
|
|
|
_do_syscall:
|
2018-06-01 08:49:14 +02:00
|
|
|
ldr r8, [r0, #24] /* grab address of PC from stack frame */
|
2019-03-14 09:20:46 -06:00
|
|
|
ldr r1, =z_arm_do_syscall
|
|
|
|
str r1, [r0, #24] /* overwrite the PC to point to z_arm_do_syscall */
|
2017-12-08 12:22:49 -06:00
|
|
|
|
2019-06-27 13:51:06 +02:00
|
|
|
/* validate syscall limit */
|
2018-08-10 15:43:31 +02:00
|
|
|
ldr ip, =K_SYSCALL_LIMIT
|
2018-04-13 16:03:24 -05:00
|
|
|
cmp r6, ip
|
2017-12-08 12:22:49 -06:00
|
|
|
blt valid_syscall_id
|
|
|
|
|
2019-06-28 11:49:19 +02:00
|
|
|
/* bad syscall id. Set arg1 to bad id and set call_id to SYSCALL_BAD */
|
2018-04-13 16:03:24 -05:00
|
|
|
str r6, [r0, #0]
|
2018-08-10 15:43:31 +02:00
|
|
|
ldr r6, =K_SYSCALL_BAD
|
2017-12-08 12:22:49 -06:00
|
|
|
|
2019-06-27 13:51:06 +02:00
|
|
|
/* Bad syscalls treated as valid syscalls with ID K_SYSCALL_BAD. */
|
|
|
|
|
2017-12-08 12:22:49 -06:00
|
|
|
valid_syscall_id:
|
2019-04-01 12:40:23 +02:00
|
|
|
push {r0, r1}
|
|
|
|
ldr r0, =_kernel
|
|
|
|
ldr r0, [r0, #_kernel_offset_to_current]
|
|
|
|
ldr r1, [r0, #_thread_offset_to_mode]
|
|
|
|
bic r1, #1
|
|
|
|
/* Store (privileged) mode in thread's mode state variable */
|
|
|
|
str r1, [r0, #_thread_offset_to_mode]
|
|
|
|
dsb
|
2017-12-08 12:22:49 -06:00
|
|
|
/* set mode to privileged, r2 still contains value from CONTROL */
|
|
|
|
bic r2, #1
|
|
|
|
msr CONTROL, r2
|
|
|
|
|
2018-04-06 16:03:47 -07:00
|
|
|
/* ISB is not strictly necessary here (stack pointer is not being
|
|
|
|
* touched), but it's recommended to avoid executing pre-fetched
|
|
|
|
* instructions with the previous privilege.
|
|
|
|
*/
|
|
|
|
isb
|
2019-04-01 12:40:23 +02:00
|
|
|
pop {r0, r1}
|
2018-04-06 16:03:47 -07:00
|
|
|
|
2019-03-14 09:20:46 -06:00
|
|
|
/* return from SVC to the modified LR - z_arm_do_syscall */
|
2017-12-08 12:22:49 -06:00
|
|
|
bx lr
|
2019-09-27 09:44:35 +09:00
|
|
|
#endif /* CONFIG_USERSPACE */
|
2017-12-08 12:22:49 -06:00
|
|
|
|
2018-06-25 09:15:14 -04:00
|
|
|
#elif defined(CONFIG_ARMV7_R)
|
2019-09-27 09:44:35 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @brief Service call handler
|
|
|
|
*
|
|
|
|
* The service call (svc) is used in the following occasions:
|
|
|
|
* - Cooperative context switching
|
|
|
|
* - IRQ offloading
|
|
|
|
* - Kernel run-time exceptions
|
|
|
|
*
|
|
|
|
* @return N/A
|
|
|
|
*/
|
2019-09-30 12:31:07 -07:00
|
|
|
SECTION_FUNC(TEXT, z_arm_svc)
|
2018-06-25 09:15:14 -04:00
|
|
|
/*
|
|
|
|
* Switch to system mode to store r0-r3 to the process stack pointer.
|
2019-09-27 09:44:35 +09:00
|
|
|
* Save r12 and the lr as we could be swapping in another process and
|
2018-06-25 09:15:14 -04:00
|
|
|
* returning to a different location.
|
|
|
|
*/
|
|
|
|
push {r4, r5}
|
|
|
|
mov r4, r12
|
|
|
|
mov r5, lr
|
|
|
|
|
|
|
|
cps #MODE_SYS
|
|
|
|
stmdb sp!, {r0-r5}
|
|
|
|
cps #MODE_SVC
|
|
|
|
|
|
|
|
pop {r4, r5}
|
|
|
|
|
|
|
|
/* Get SVC number */
|
|
|
|
mrs r0, spsr
|
|
|
|
tst r0, #0x20
|
|
|
|
|
|
|
|
ldreq r1, [lr, #-4]
|
|
|
|
biceq r1, #0xff000000
|
|
|
|
beq demux
|
|
|
|
|
|
|
|
ldr r1, [lr, #-2]
|
|
|
|
bic r1, #0xff00
|
|
|
|
|
|
|
|
/*
|
|
|
|
* grab service call number:
|
|
|
|
* 0: context switch
|
|
|
|
* 1: irq_offload (if configured)
|
|
|
|
* 2: kernel panic or oops (software generated fatal exception)
|
|
|
|
* Planned implementation of system calls for memory protection will
|
|
|
|
* expand this case.
|
|
|
|
*/
|
|
|
|
demux:
|
|
|
|
cmp r1, #_SVC_CALL_CONTEXT_SWITCH
|
|
|
|
beq _context_switch
|
|
|
|
|
|
|
|
cmp r1, #_SVC_CALL_RUNTIME_EXCEPT
|
|
|
|
beq _oops
|
|
|
|
|
|
|
|
#if CONFIG_IRQ_OFFLOAD
|
|
|
|
blx z_irq_do_offload /* call C routine which executes the offload */
|
|
|
|
|
2019-09-30 12:31:07 -07:00
|
|
|
/* exception return is done in z_arm_int_exit() */
|
2018-06-25 09:15:14 -04:00
|
|
|
mov r0, #RET_FROM_SVC
|
2019-09-30 12:31:07 -07:00
|
|
|
b z_arm_int_exit
|
2018-06-25 09:15:14 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
_context_switch:
|
|
|
|
/* handler mode exit, to PendSV */
|
2019-09-30 12:31:07 -07:00
|
|
|
bl z_arm_pendsv
|
2018-06-25 09:15:14 -04:00
|
|
|
|
|
|
|
mov r0, #RET_FROM_SVC
|
2019-09-30 12:31:07 -07:00
|
|
|
b z_arm_int_exit
|
2018-06-25 09:15:14 -04:00
|
|
|
|
|
|
|
_oops:
|
|
|
|
push {r0, lr}
|
|
|
|
blx z_do_kernel_oops
|
|
|
|
pop {r0, lr}
|
|
|
|
cpsie i
|
|
|
|
movs pc, lr
|
|
|
|
|
2019-09-30 12:31:07 -07:00
|
|
|
GTEXT(z_arm_cortex_r_svc)
|
|
|
|
SECTION_FUNC(TEXT, z_arm_cortex_r_svc)
|
2018-06-25 09:15:14 -04:00
|
|
|
svc #0
|
|
|
|
bx lr
|
|
|
|
|
2016-12-31 13:18:25 +00:00
|
|
|
#else
|
|
|
|
#error Unknown ARM architecture
|
2018-02-06 23:47:58 +01:00
|
|
|
#endif /* CONFIG_ARMV6_M_ARMV8_M_BASELINE */
|