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>
|
2015-04-10 16:44:37 -07:00
|
|
|
|
|
|
|
_ASM_FILE_PROLOGUE
|
|
|
|
|
2017-04-06 15:30:27 -07:00
|
|
|
GTEXT(__swap)
|
2015-04-10 16:44:37 -07:00
|
|
|
GTEXT(__svc)
|
|
|
|
GTEXT(__pendsv)
|
2017-04-19 12:53:48 -07:00
|
|
|
GTEXT(_do_kernel_oops)
|
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
|
|
|
|
* already been taken. In other words, when __pendsv() runs, we *know* we have
|
|
|
|
* to swap *something*.
|
|
|
|
*/
|
2015-04-10 16:44:37 -07:00
|
|
|
|
|
|
|
SECTION_FUNC(TEXT, __pendsv)
|
|
|
|
|
2017-06-07 09:33:16 -07:00
|
|
|
#ifdef CONFIG_KERNEL_EVENT_LOGGER_CONTEXT_SWITCH
|
2017-05-11 13:29:15 -07:00
|
|
|
/* Register the context switch */
|
|
|
|
push {lr}
|
|
|
|
bl _sys_k_event_logger_context_switch
|
|
|
|
#if defined(CONFIG_ARMV6_M)
|
|
|
|
pop {r0}
|
|
|
|
mov lr, r0
|
|
|
|
#else
|
|
|
|
pop {lr}
|
|
|
|
#endif /* CONFIG_ARMV6_M */
|
2017-06-07 09:33:16 -07:00
|
|
|
#endif /* CONFIG_KERNEL_EVENT_LOGGER_CONTEXT_SWITCH */
|
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 */
|
2015-04-10 16:44:37 -07:00
|
|
|
mrs ip, PSP
|
|
|
|
|
2016-12-31 14:09:41 +00:00
|
|
|
#if defined(CONFIG_ARMV6_M)
|
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}
|
2016-12-31 14:41:19 +00:00
|
|
|
#elif defined(CONFIG_ARMV7_M)
|
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
|
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}
|
2016-10-05 19:43:36 -03:00
|
|
|
#endif /* CONFIG_FP_SHARING */
|
2016-12-31 13:18:25 +00:00
|
|
|
#else
|
|
|
|
#error Unknown ARM architecture
|
2016-12-31 14:09:41 +00:00
|
|
|
#endif /* CONFIG_ARMV6_M */
|
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.
|
|
|
|
*/
|
2016-10-01 18:49:36 -04:00
|
|
|
ldr v4, =_SCS_ICSR
|
|
|
|
ldr v3, =_SCS_ICSR_UNPENDSV
|
2015-04-10 16:44:37 -07:00
|
|
|
|
|
|
|
/* protect the kernel state while we play with the thread lists */
|
2016-12-31 14:09:41 +00:00
|
|
|
#if defined(CONFIG_ARMV6_M)
|
2016-10-05 19:43:36 -03:00
|
|
|
cpsid i
|
2016-12-31 14:41:19 +00:00
|
|
|
#elif defined(CONFIG_ARMV7_M)
|
2015-04-10 16:44:37 -07:00
|
|
|
movs.n r0, #_EXC_IRQ_DEFAULT_PRIO
|
|
|
|
msr BASEPRI, r0
|
2016-12-31 13:18:25 +00:00
|
|
|
#else
|
|
|
|
#error Unknown ARM architecture
|
2016-12-31 14:09:41 +00:00
|
|
|
#endif /* CONFIG_ARMV6_M */
|
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 */
|
|
|
|
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
|
|
|
|
* decides, with the new kernel state baseed on the new thread
|
|
|
|
* being context-switched in, that it needs to reschedules, it
|
|
|
|
* 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 */
|
|
|
|
str v3, [v4, #0]
|
2015-04-10 16:44:37 -07:00
|
|
|
|
2016-10-05 19:43:36 -03:00
|
|
|
/* Restore previous interrupt disable state (irq_lock key) */
|
2016-11-08 10:36:50 -05:00
|
|
|
ldr r0, [r2, #_thread_offset_to_basepri]
|
2016-10-05 19:43:36 -03:00
|
|
|
movs.n r3, #0
|
2016-11-08 10:36:50 -05:00
|
|
|
str r3, [r2, #_thread_offset_to_basepri]
|
2016-10-05 19:43:36 -03:00
|
|
|
|
2016-12-31 14:09:41 +00:00
|
|
|
#if defined(CONFIG_ARMV6_M)
|
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}
|
2016-12-31 14:41:19 +00:00
|
|
|
#elif defined(CONFIG_ARMV7_M)
|
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
|
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}
|
|
|
|
#endif
|
|
|
|
|
2017-03-29 11:31:45 +01:00
|
|
|
#ifdef CONFIG_MPU_STACK_GUARD
|
|
|
|
/* r2 contains k_thread */
|
|
|
|
add r0, r2, #0
|
|
|
|
push {r2, lr}
|
|
|
|
blx configure_mpu_stack_guard
|
|
|
|
pop {r2, lr}
|
|
|
|
#endif /* CONFIG_MPU_STACK_GUARD */
|
|
|
|
|
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}
|
2016-12-31 13:18:25 +00:00
|
|
|
#else
|
|
|
|
#error Unknown ARM architecture
|
2016-12-31 14:09:41 +00:00
|
|
|
#endif /* CONFIG_ARMV6_M */
|
2016-10-05 19:43:36 -03:00
|
|
|
|
2015-04-10 16:44:37 -07:00
|
|
|
msr PSP, ip
|
|
|
|
|
2017-05-03 13:11:51 +05:30
|
|
|
#ifdef CONFIG_EXECUTION_BENCHMARKING
|
2017-08-23 16:02:00 +05:30
|
|
|
stm sp!,{r0-r3} /* Save regs r0 to r4 on stack */
|
2017-05-03 13:11:51 +05:30
|
|
|
push {lr}
|
2017-08-31 20:05:05 +05:30
|
|
|
bl read_timer_end_of_swap
|
2017-05-03 13:11:51 +05:30
|
|
|
|
|
|
|
#if defined(CONFIG_ARMV6_M)
|
|
|
|
pop {r3}
|
|
|
|
mov lr,r3
|
|
|
|
#else
|
|
|
|
pop {lr}
|
2017-08-23 16:02:00 +05:30
|
|
|
#endif /* CONFIG_ARMV6_M */
|
|
|
|
ldm sp!,{r0-r3} /* Load back regs ro to r4 */
|
|
|
|
#endif /* CONFIG_EXECUTION_BENCHMARKING */
|
2017-05-03 13:11:51 +05:30
|
|
|
|
2015-04-10 16:44:37 -07:00
|
|
|
/* exc return */
|
|
|
|
bx lr
|
|
|
|
|
2016-12-31 14:09:41 +00:00
|
|
|
#if defined(CONFIG_ARMV6_M)
|
2017-06-01 13:36:44 -07:00
|
|
|
SECTION_FUNC(TEXT, __svc)
|
|
|
|
/* 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
|
|
|
|
|
|
|
|
#if CONFIG_IRQ_OFFLOAD
|
|
|
|
push {lr}
|
|
|
|
blx _irq_do_offload /* call C routine which executes the offload */
|
|
|
|
pop {r3}
|
|
|
|
mov lr, r3
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* exception return is done in _IntExit() */
|
|
|
|
b _IntExit
|
|
|
|
|
|
|
|
_oops:
|
|
|
|
push {lr}
|
|
|
|
blx _do_kernel_oops
|
|
|
|
pop {pc}
|
|
|
|
|
2016-12-31 14:41:19 +00:00
|
|
|
#elif defined(CONFIG_ARMV7_M)
|
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
|
|
|
*
|
2017-04-06 15:30:27 -07:00
|
|
|
* The service call (svc) is only used in __swap() to enter handler mode so we
|
2015-07-01 17:22:39 -04:00
|
|
|
* can go through the PendSV exception to perform a context switch.
|
|
|
|
*
|
2015-07-01 17:29:04 -04:00
|
|
|
* @return N/A
|
2015-07-01 17:22:39 -04:00
|
|
|
*/
|
2015-04-10 16:44:37 -07:00
|
|
|
|
|
|
|
SECTION_FUNC(TEXT, __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 */
|
2015-11-17 14:08:45 -08: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:
|
|
|
|
* 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.
|
2015-11-17 14:08:45 -08:00
|
|
|
*/
|
2017-04-19 12:53:48 -07:00
|
|
|
ands r1, #0xff
|
2015-11-17 14:08:45 -08:00
|
|
|
beq _context_switch
|
|
|
|
|
2017-04-19 12:53:48 -07:00
|
|
|
cmp r1, #2
|
|
|
|
beq _oops
|
|
|
|
|
|
|
|
#if CONFIG_IRQ_OFFLOAD
|
2015-11-17 14:08:45 -08:00
|
|
|
push {lr}
|
|
|
|
blx _irq_do_offload /* call C routine which executes the offload */
|
|
|
|
pop {lr}
|
|
|
|
|
2016-12-19 09:07:04 -05:00
|
|
|
/* exception return is done in _IntExit() */
|
2015-11-17 14:08:45 -08:00
|
|
|
b _IntExit
|
2017-04-19 12:53:48 -07:00
|
|
|
#endif
|
2015-11-17 14:08:45 -08:00
|
|
|
|
2016-08-18 09:25:00 -07:00
|
|
|
_context_switch:
|
2015-11-17 14:08:45 -08:00
|
|
|
|
2015-04-10 16:44:37 -07:00
|
|
|
/*
|
|
|
|
* Unlock interrupts:
|
|
|
|
* - in a SVC call, so protected against context switches
|
|
|
|
* - allow PendSV, since it's running at prio 0xff
|
|
|
|
*/
|
|
|
|
eors.n r0, r0
|
|
|
|
msr BASEPRI, r0
|
|
|
|
|
|
|
|
/* set PENDSV bit, pending the PendSV exception */
|
|
|
|
ldr r1, =_SCS_ICSR
|
|
|
|
ldr r2, =_SCS_ICSR_PENDSV
|
|
|
|
str r2, [r1, #0]
|
|
|
|
|
|
|
|
/* handler mode exit, to PendSV */
|
|
|
|
bx lr
|
2017-04-19 12:53:48 -07:00
|
|
|
|
|
|
|
_oops:
|
|
|
|
push {lr}
|
|
|
|
blx _do_kernel_oops
|
|
|
|
pop {pc}
|
|
|
|
|
2016-12-31 13:18:25 +00:00
|
|
|
#else
|
|
|
|
#error Unknown ARM architecture
|
2016-12-31 14:09:41 +00:00
|
|
|
#endif /* CONFIG_ARMV6_M */
|
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 Initiate a cooperative context switch
|
2015-07-01 17:22:39 -04:00
|
|
|
*
|
2017-04-06 15:30:27 -07:00
|
|
|
* The __swap() routine is invoked by various kernel services to effect
|
|
|
|
* a cooperative context context switch. Prior to invoking __swap(), the caller
|
2015-07-01 17:22:39 -04:00
|
|
|
* disables interrupts via irq_lock() and the return 'key' is passed as a
|
2017-04-06 15:30:27 -07:00
|
|
|
* parameter to __swap(). The 'key' actually represents the BASEPRI register
|
2015-07-01 17:22:39 -04:00
|
|
|
* prior to disabling interrupts via the BASEPRI mechanism.
|
|
|
|
*
|
2017-04-06 15:30:27 -07:00
|
|
|
* __swap() itself does not do much.
|
2015-07-01 17:22:39 -04:00
|
|
|
*
|
|
|
|
* It simply stores the intlock key (the BASEPRI value) parameter into
|
|
|
|
* current->basepri, and then triggers a service call exception (svc) to setup
|
|
|
|
* the PendSV exception, which does the heavy lifting of context switching.
|
|
|
|
|
|
|
|
* This is the only place we have to save BASEPRI since the other paths to
|
|
|
|
* __pendsv all come from handling an interrupt, which means we know the
|
|
|
|
* interrupts were not locked: in that case the BASEPRI value is 0.
|
|
|
|
*
|
2017-04-06 15:30:27 -07:00
|
|
|
* Given that __swap() is called to effect a cooperative context switch,
|
2017-03-27 15:35:09 +01:00
|
|
|
* only the caller-saved integer registers need to be saved in the thread of the
|
2015-08-20 11:04:01 -04:00
|
|
|
* outgoing thread. This is all performed by the hardware, which stores it in
|
2015-07-01 17:22:39 -04:00
|
|
|
* its exception stack frame, created when handling the svc exception.
|
|
|
|
*
|
2016-10-05 19:43:36 -03:00
|
|
|
* On Cortex-M0/M0+ the intlock key is represented by the PRIMASK register,
|
|
|
|
* as BASEPRI is not available.
|
|
|
|
*
|
2016-12-21 11:16:01 -05:00
|
|
|
* @return may contain a return value setup by a call to
|
|
|
|
* _set_thread_return_value()
|
2015-07-01 17:22:39 -04:00
|
|
|
*
|
|
|
|
* C function prototype:
|
|
|
|
*
|
2017-04-06 15:30:27 -07:00
|
|
|
* unsigned int __swap (unsigned int basepri);
|
2015-07-01 17:22:39 -04:00
|
|
|
*
|
|
|
|
*/
|
2015-04-10 16:44:37 -07:00
|
|
|
|
2017-04-06 15:30:27 -07:00
|
|
|
SECTION_FUNC(TEXT, __swap)
|
2015-04-10 16:44:37 -07:00
|
|
|
|
2017-05-03 13:11:51 +05:30
|
|
|
#ifdef CONFIG_EXECUTION_BENCHMARKING
|
|
|
|
push {lr}
|
2017-08-31 20:05:05 +05:30
|
|
|
bl read_timer_start_of_swap
|
2017-05-03 13:11:51 +05:30
|
|
|
#if defined(CONFIG_ARMV6_M)
|
|
|
|
pop {r3}
|
|
|
|
mov lr,r3
|
|
|
|
#else
|
|
|
|
pop {lr}
|
2017-08-23 16:02:00 +05:30
|
|
|
#endif /* CONFIG_ARMV6_M */
|
|
|
|
#endif /* CONFIG_EXECUTION_BENCHMARKING */
|
2016-11-08 10:36:50 -05:00
|
|
|
ldr r1, =_kernel
|
|
|
|
ldr r2, [r1, #_kernel_offset_to_current]
|
|
|
|
str r0, [r2, #_thread_offset_to_basepri]
|
2015-04-10 16:44:37 -07:00
|
|
|
|
kernel/arm: fix race condition when setting _Swap() return value
There was a possible race condition when setting the return value of a
thread that is pending, from an ISR.
A kernel function causes a thread to pend, with the following series of
steps:
- disable interrupts
- move current thread to wait_q
- call _Swap
Depending if running on M3/4 or M0+, _Swap will either issue a svc #0,
or pend PendSV directly. The same problem exists in both cases.
M3/4:
__svc will:
- enable interrupts
- trigger __pendsv
M0+:
_Swap() will enable interrupts.
__pendsv will:
- save register context including PSP into the thread struct
If an interrupt occurs between interrupts being enabled them and
__pendsv saving PSP, and the ISR sets the pending thread's return value,
this will happen:
- sees the thread in a wait_q
- removes it
- makes it ready
- calls _set_thread_return_value
- _set_thread_return_value looks at the thread's saved PSP to poke
the value
In this scenario, PSP hasn't yet been updated by __pendsv so it's a
stale value from the previous context switch, resulting in unpredictable
word on the stack getting set to the return value.
There is no way to fix this issue and still have the return value being
delivered directly in the pending thread's exception stack frame, in the
M0+ case. There will always be a window between the unlocking of
interrupts and PendSV being handled. On M3/4, it could be possible with
the mix of SVC and PendSV, since the exception stack frame is created in
the __svc handler. However, because we want to keep the two
implementations as close as possible, and there were talks of moving
M3/4 to using PendSV only, to save an exception, the approach taken
solves both cases.
The approach taken is similar to the ARC and Nios2 ports, where
there is a field in the thread structure that holds the return value.
_Swap() then loads r0/a1 with that value just before returning.
Fixes ZEP-1289.
Change-Id: Iee7e06fe3f8ded84aff918fd43408c7f589344d9
Signed-off-by: Benjamin Walsh <benjamin.walsh@windriver.com>
2016-11-15 18:45:43 -05:00
|
|
|
/*
|
2017-04-06 15:30:27 -07:00
|
|
|
* Set __swap()'s default return code to -EAGAIN. This eliminates the need
|
kernel/arm: fix race condition when setting _Swap() return value
There was a possible race condition when setting the return value of a
thread that is pending, from an ISR.
A kernel function causes a thread to pend, with the following series of
steps:
- disable interrupts
- move current thread to wait_q
- call _Swap
Depending if running on M3/4 or M0+, _Swap will either issue a svc #0,
or pend PendSV directly. The same problem exists in both cases.
M3/4:
__svc will:
- enable interrupts
- trigger __pendsv
M0+:
_Swap() will enable interrupts.
__pendsv will:
- save register context including PSP into the thread struct
If an interrupt occurs between interrupts being enabled them and
__pendsv saving PSP, and the ISR sets the pending thread's return value,
this will happen:
- sees the thread in a wait_q
- removes it
- makes it ready
- calls _set_thread_return_value
- _set_thread_return_value looks at the thread's saved PSP to poke
the value
In this scenario, PSP hasn't yet been updated by __pendsv so it's a
stale value from the previous context switch, resulting in unpredictable
word on the stack getting set to the return value.
There is no way to fix this issue and still have the return value being
delivered directly in the pending thread's exception stack frame, in the
M0+ case. There will always be a window between the unlocking of
interrupts and PendSV being handled. On M3/4, it could be possible with
the mix of SVC and PendSV, since the exception stack frame is created in
the __svc handler. However, because we want to keep the two
implementations as close as possible, and there were talks of moving
M3/4 to using PendSV only, to save an exception, the approach taken
solves both cases.
The approach taken is similar to the ARC and Nios2 ports, where
there is a field in the thread structure that holds the return value.
_Swap() then loads r0/a1 with that value just before returning.
Fixes ZEP-1289.
Change-Id: Iee7e06fe3f8ded84aff918fd43408c7f589344d9
Signed-off-by: Benjamin Walsh <benjamin.walsh@windriver.com>
2016-11-15 18:45:43 -05:00
|
|
|
* for the timeout code to set it itself.
|
|
|
|
*/
|
|
|
|
ldr r1, =_k_neg_eagain
|
|
|
|
ldr r1, [r1]
|
|
|
|
str r1, [r2, #_thread_offset_to_swap_return_value]
|
|
|
|
|
2016-12-31 14:09:41 +00:00
|
|
|
#if defined(CONFIG_ARMV6_M)
|
2016-10-05 19:43:36 -03:00
|
|
|
/* No priority-based interrupt masking on M0/M0+,
|
|
|
|
* pending PendSV is used instead of svc
|
|
|
|
*/
|
|
|
|
ldr r1, =_SCS_ICSR
|
kernel/arm: fix race condition when setting _Swap() return value
There was a possible race condition when setting the return value of a
thread that is pending, from an ISR.
A kernel function causes a thread to pend, with the following series of
steps:
- disable interrupts
- move current thread to wait_q
- call _Swap
Depending if running on M3/4 or M0+, _Swap will either issue a svc #0,
or pend PendSV directly. The same problem exists in both cases.
M3/4:
__svc will:
- enable interrupts
- trigger __pendsv
M0+:
_Swap() will enable interrupts.
__pendsv will:
- save register context including PSP into the thread struct
If an interrupt occurs between interrupts being enabled them and
__pendsv saving PSP, and the ISR sets the pending thread's return value,
this will happen:
- sees the thread in a wait_q
- removes it
- makes it ready
- calls _set_thread_return_value
- _set_thread_return_value looks at the thread's saved PSP to poke
the value
In this scenario, PSP hasn't yet been updated by __pendsv so it's a
stale value from the previous context switch, resulting in unpredictable
word on the stack getting set to the return value.
There is no way to fix this issue and still have the return value being
delivered directly in the pending thread's exception stack frame, in the
M0+ case. There will always be a window between the unlocking of
interrupts and PendSV being handled. On M3/4, it could be possible with
the mix of SVC and PendSV, since the exception stack frame is created in
the __svc handler. However, because we want to keep the two
implementations as close as possible, and there were talks of moving
M3/4 to using PendSV only, to save an exception, the approach taken
solves both cases.
The approach taken is similar to the ARC and Nios2 ports, where
there is a field in the thread structure that holds the return value.
_Swap() then loads r0/a1 with that value just before returning.
Fixes ZEP-1289.
Change-Id: Iee7e06fe3f8ded84aff918fd43408c7f589344d9
Signed-off-by: Benjamin Walsh <benjamin.walsh@windriver.com>
2016-11-15 18:45:43 -05:00
|
|
|
ldr r3, =_SCS_ICSR_PENDSV
|
|
|
|
str r3, [r1, #0]
|
2016-11-04 12:02:50 -04:00
|
|
|
|
2016-10-05 19:43:36 -03:00
|
|
|
/* Unlock interrupts to allow PendSV, since it's running at prio 0xff
|
|
|
|
*
|
|
|
|
* PendSV handler will be called if there are no other interrupts
|
|
|
|
* of a higher priority pending.
|
|
|
|
*/
|
|
|
|
cpsie i
|
2016-12-31 14:41:19 +00:00
|
|
|
#elif defined(CONFIG_ARMV7_M)
|
2015-04-10 16:44:37 -07:00
|
|
|
svc #0
|
2016-12-31 13:18:25 +00:00
|
|
|
#else
|
|
|
|
#error Unknown ARM architecture
|
2016-12-31 14:09:41 +00:00
|
|
|
#endif /* CONFIG_ARMV6_M */
|
2015-04-10 16:44:37 -07:00
|
|
|
|
kernel/arm: fix race condition when setting _Swap() return value
There was a possible race condition when setting the return value of a
thread that is pending, from an ISR.
A kernel function causes a thread to pend, with the following series of
steps:
- disable interrupts
- move current thread to wait_q
- call _Swap
Depending if running on M3/4 or M0+, _Swap will either issue a svc #0,
or pend PendSV directly. The same problem exists in both cases.
M3/4:
__svc will:
- enable interrupts
- trigger __pendsv
M0+:
_Swap() will enable interrupts.
__pendsv will:
- save register context including PSP into the thread struct
If an interrupt occurs between interrupts being enabled them and
__pendsv saving PSP, and the ISR sets the pending thread's return value,
this will happen:
- sees the thread in a wait_q
- removes it
- makes it ready
- calls _set_thread_return_value
- _set_thread_return_value looks at the thread's saved PSP to poke
the value
In this scenario, PSP hasn't yet been updated by __pendsv so it's a
stale value from the previous context switch, resulting in unpredictable
word on the stack getting set to the return value.
There is no way to fix this issue and still have the return value being
delivered directly in the pending thread's exception stack frame, in the
M0+ case. There will always be a window between the unlocking of
interrupts and PendSV being handled. On M3/4, it could be possible with
the mix of SVC and PendSV, since the exception stack frame is created in
the __svc handler. However, because we want to keep the two
implementations as close as possible, and there were talks of moving
M3/4 to using PendSV only, to save an exception, the approach taken
solves both cases.
The approach taken is similar to the ARC and Nios2 ports, where
there is a field in the thread structure that holds the return value.
_Swap() then loads r0/a1 with that value just before returning.
Fixes ZEP-1289.
Change-Id: Iee7e06fe3f8ded84aff918fd43408c7f589344d9
Signed-off-by: Benjamin Walsh <benjamin.walsh@windriver.com>
2016-11-15 18:45:43 -05:00
|
|
|
/* coming back from exception, r2 still holds the pointer to _current */
|
|
|
|
ldr r0, [r2, #_thread_offset_to_swap_return_value]
|
2015-04-10 16:44:37 -07:00
|
|
|
bx lr
|