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
|
2017-03-11 19:33:29 +03:00
|
|
|
* @brief ARM Cortex-M exception/interrupt exit API
|
2015-12-04 10:09:39 -05:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* Provides functions for performing kernel handling when exiting exceptions or
|
|
|
|
* interrupts that are installed directly in the vector table (i.e. that are not
|
|
|
|
* wrapped around by _isr_wrapper()).
|
2015-07-01 17:22:39 -04:00
|
|
|
*/
|
2015-04-10 16:44:37 -07:00
|
|
|
|
|
|
|
#include <toolchain.h>
|
2019-10-09 09:38:54 +02:00
|
|
|
#include <linker/sections.h>
|
|
|
|
#include <offsets_short.h>
|
2015-05-28 10:56:47 -07:00
|
|
|
#include <arch/cpu.h>
|
2015-04-10 16:44:37 -07:00
|
|
|
|
|
|
|
_ASM_FILE_PROLOGUE
|
|
|
|
|
2019-09-30 12:31:07 -07:00
|
|
|
GTEXT(z_arm_exc_exit)
|
|
|
|
GTEXT(z_arm_int_exit)
|
2016-11-08 10:36:50 -05:00
|
|
|
GDATA(_kernel)
|
2018-06-25 09:15:14 -04:00
|
|
|
#if defined(CONFIG_CPU_CORTEX_R)
|
2019-09-30 12:31:07 -07:00
|
|
|
GTEXT(z_arm_pendsv)
|
2018-06-25 09:15:14 -04:00
|
|
|
#endif
|
2016-09-02 16:20:19 -04:00
|
|
|
|
2015-07-01 17:22:39 -04:00
|
|
|
/**
|
|
|
|
*
|
2015-07-01 17:51:40 -04:00
|
|
|
* @brief Kernel housekeeping when exiting interrupt handler installed
|
2015-07-01 17:22:39 -04:00
|
|
|
* directly in vector table
|
|
|
|
*
|
|
|
|
* Kernel allows installing interrupt handlers (ISRs) directly into the vector
|
2019-09-30 12:31:07 -07:00
|
|
|
* table to get the lowest interrupt latency possible. This allows the ISR to
|
|
|
|
* be invoked directly without going through a software interrupt table.
|
|
|
|
* However, upon exiting the ISR, some kernel work must still be performed,
|
|
|
|
* namely possible context switching. While ISRs connected in the software
|
|
|
|
* interrupt table do this automatically via a wrapper, ISRs connected directly
|
|
|
|
* in the vector table must invoke z_arm_int_exit() as the *very last* action
|
|
|
|
* before returning.
|
2015-07-01 17:22:39 -04:00
|
|
|
*
|
|
|
|
* e.g.
|
|
|
|
*
|
|
|
|
* void myISR(void)
|
|
|
|
* {
|
|
|
|
* printk("in %s\n", __FUNCTION__);
|
|
|
|
* doStuff();
|
2019-09-30 12:31:07 -07:00
|
|
|
* z_arm_int_exit();
|
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
|
|
|
*/
|
2015-04-10 16:44:37 -07:00
|
|
|
|
2019-09-30 12:31:07 -07:00
|
|
|
SECTION_SUBSEC_FUNC(TEXT, _HandlerModeExit, z_arm_int_exit)
|
2015-04-10 16:44:37 -07:00
|
|
|
|
2019-09-30 12:31:07 -07:00
|
|
|
/* z_arm_int_exit falls through to z_arm_exc_exit (they are aliases of each
|
|
|
|
* other)
|
|
|
|
*/
|
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 Kernel housekeeping when exiting exception handler installed
|
2015-07-01 17:22:39 -04:00
|
|
|
* directly in vector table
|
|
|
|
*
|
2019-09-30 12:31:07 -07:00
|
|
|
* See z_arm_int_exit().
|
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
|
|
|
*/
|
2015-04-10 16:44:37 -07:00
|
|
|
|
2019-09-30 12:31:07 -07:00
|
|
|
SECTION_SUBSEC_FUNC(TEXT, _HandlerModeExit, z_arm_exc_exit)
|
2018-06-25 09:15:14 -04:00
|
|
|
#if defined(CONFIG_CPU_CORTEX_R)
|
2019-09-27 09:44:35 +09:00
|
|
|
/* r0 contains the caller mode */
|
2018-06-25 09:15:14 -04:00
|
|
|
push {r0, lr}
|
|
|
|
#endif
|
2015-04-10 16:44:37 -07:00
|
|
|
|
2016-12-14 14:34:29 -05:00
|
|
|
#ifdef CONFIG_PREEMPT_ENABLED
|
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
|
|
|
ldr r0, =_kernel
|
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
|
|
|
ldr r1, [r0, #_kernel_offset_to_current]
|
2016-12-21 16:00:35 -05:00
|
|
|
|
2019-03-13 11:26:28 -05:00
|
|
|
ldr r0, [r0, #_kernel_offset_to_ready_q_cache]
|
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
|
|
|
cmp r0, r1
|
|
|
|
beq _EXIT_EXC
|
2016-09-02 16:20:19 -04:00
|
|
|
|
2018-06-25 09:15:14 -04:00
|
|
|
#if defined(CONFIG_CPU_CORTEX_M)
|
2015-04-10 16:44:37 -07:00
|
|
|
/* context switch required, pend the PendSV exception */
|
|
|
|
ldr r1, =_SCS_ICSR
|
|
|
|
ldr r2, =_SCS_ICSR_PENDSV
|
|
|
|
str r2, [r1]
|
2018-06-25 09:15:14 -04:00
|
|
|
#elif defined(CONFIG_CPU_CORTEX_R)
|
2019-09-30 12:31:07 -07:00
|
|
|
bl z_arm_pendsv
|
2018-06-25 09:15:14 -04:00
|
|
|
#endif
|
2015-04-10 16:44:37 -07:00
|
|
|
|
|
|
|
_ExcExitWithGdbStub:
|
|
|
|
|
2016-10-05 19:43:36 -03:00
|
|
|
_EXIT_EXC:
|
2016-12-14 14:34:29 -05:00
|
|
|
#endif /* CONFIG_PREEMPT_ENABLED */
|
2016-10-05 19:43:36 -03:00
|
|
|
|
2017-05-11 13:29:15 -07:00
|
|
|
#ifdef CONFIG_STACK_SENTINEL
|
2019-09-26 15:03:34 +09:00
|
|
|
#if defined(CONFIG_CPU_CORTEX_M)
|
2019-01-31 16:31:01 -06:00
|
|
|
push {r0, lr}
|
2019-03-08 14:19:05 -07:00
|
|
|
bl z_check_stack_sentinel
|
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}
|
2018-02-06 23:47:58 +01:00
|
|
|
#endif /* CONFIG_ARMV6_M_ARMV8_M_BASELINE */
|
2019-09-26 15:03:34 +09:00
|
|
|
#else
|
|
|
|
bl z_check_stack_sentinel
|
|
|
|
#endif /* CONFIG_CPU_CORTEX_M */
|
2017-05-11 13:29:15 -07:00
|
|
|
#endif /* CONFIG_STACK_SENTINEL */
|
2018-06-25 09:15:14 -04:00
|
|
|
|
|
|
|
#if defined(CONFIG_CPU_CORTEX_M)
|
2015-04-10 16:44:37 -07:00
|
|
|
bx lr
|
2018-06-25 09:15:14 -04:00
|
|
|
#elif defined(CONFIG_CPU_CORTEX_R)
|
2019-09-27 09:44:35 +09:00
|
|
|
/* Restore the caller mode to r0 */
|
|
|
|
pop {r0, lr}
|
|
|
|
|
2018-06-25 09:15:14 -04:00
|
|
|
/*
|
2019-09-27 09:44:35 +09:00
|
|
|
* Restore r0-r3, r12 and lr stored into the process stack by the mode
|
|
|
|
* entry function. These registers are saved by _isr_wrapper for IRQ mode
|
|
|
|
* and z_arm_svc for SVC mode.
|
|
|
|
*
|
2018-06-25 09:15:14 -04:00
|
|
|
* r0-r3 are either the values from the thread before it was switched out
|
2019-09-27 09:44:35 +09:00
|
|
|
* or they are the args to _new_thread for a new thread.
|
2018-06-25 09:15:14 -04:00
|
|
|
*/
|
|
|
|
push {r4, r5}
|
|
|
|
|
|
|
|
cmp r0, #RET_FROM_SVC
|
|
|
|
cps #MODE_SYS
|
|
|
|
ldmia sp!, {r0-r5}
|
|
|
|
beq _svc_exit
|
|
|
|
|
|
|
|
cps #MODE_IRQ
|
|
|
|
b _exc_exit
|
|
|
|
|
|
|
|
_svc_exit:
|
|
|
|
cps #MODE_SVC
|
|
|
|
|
|
|
|
_exc_exit:
|
|
|
|
mov r12, r4
|
|
|
|
mov lr, r5
|
|
|
|
pop {r4, r5}
|
|
|
|
movs pc, lr
|
|
|
|
#endif
|