arch: arm: Change method of __swap processing
This patch reworks the current ARM __swap() function into a C function.
Due to some issues with using svc calls withing fault handlers, we
needed to change the way we initiate a swap by removing the dependence
on svc #0.
Before __swap() is called, the system has already done an irq_lock().
Upon return from __swap(), the equivalent of an irq_lock() is done due
to restoration of the key value from the irq_lock preceeding the call.
For ARM V6M (M0/M0+), the pendsv bit is toggled and the irqs are
enabled. There is no priority masking in v6m, so it's just a global
enable. For ARM V7M, the priority mask has to be set to 0x0 to allow
for the pendsv IRQ to be taken. This is done for both via a call to
irq_unlock(0).
After this unlock, a pendsv irq will be taken, either at the tail end
of the current irq handling if we are in handler mode, or immediately
due to the pendsv being asserted (no other outstanding irqs). The next
thread will be scheduled.
Upon return from the context switch to the original
thread, the priority mask will already be correct due to the pendsv
processing.
Signed-off-by: Andy Gross <andy.gross@linaro.org>
2018-02-22 14:22:45 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 Linaro, Limited
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <kernel.h>
|
|
|
|
#include <toolchain.h>
|
|
|
|
#include <kernel_structs.h>
|
|
|
|
|
|
|
|
#ifdef CONFIG_EXECUTION_BENCHMARKING
|
|
|
|
extern void read_timer_start_of_swap(void);
|
|
|
|
#endif
|
|
|
|
extern const int _k_neg_eagain;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @brief Initiate a cooperative context switch
|
|
|
|
*
|
|
|
|
* The __swap() routine is invoked by various kernel services to effect
|
|
|
|
* a cooperative context context switch. Prior to invoking __swap(), the caller
|
|
|
|
* disables interrupts via irq_lock() and the return 'key' is passed as a
|
|
|
|
* parameter to __swap(). The 'key' actually represents the BASEPRI register
|
|
|
|
* prior to disabling interrupts via the BASEPRI mechanism.
|
|
|
|
*
|
|
|
|
* __swap() itself does not do much.
|
|
|
|
*
|
|
|
|
* It simply stores the intlock key (the BASEPRI value) parameter into
|
|
|
|
* current->basepri, and then triggers a 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.
|
|
|
|
*
|
|
|
|
* Given that __swap() is called to effect a cooperative context switch,
|
|
|
|
* only the caller-saved integer registers need to be saved in the thread of the
|
|
|
|
* outgoing thread. This is all performed by the hardware, which stores it in
|
2019-02-11 18:07:58 +08:00
|
|
|
* its exception stack frame, created when handling the __pendsv exception.
|
arch: arm: Change method of __swap processing
This patch reworks the current ARM __swap() function into a C function.
Due to some issues with using svc calls withing fault handlers, we
needed to change the way we initiate a swap by removing the dependence
on svc #0.
Before __swap() is called, the system has already done an irq_lock().
Upon return from __swap(), the equivalent of an irq_lock() is done due
to restoration of the key value from the irq_lock preceeding the call.
For ARM V6M (M0/M0+), the pendsv bit is toggled and the irqs are
enabled. There is no priority masking in v6m, so it's just a global
enable. For ARM V7M, the priority mask has to be set to 0x0 to allow
for the pendsv IRQ to be taken. This is done for both via a call to
irq_unlock(0).
After this unlock, a pendsv irq will be taken, either at the tail end
of the current irq handling if we are in handler mode, or immediately
due to the pendsv being asserted (no other outstanding irqs). The next
thread will be scheduled.
Upon return from the context switch to the original
thread, the priority mask will already be correct due to the pendsv
processing.
Signed-off-by: Andy Gross <andy.gross@linaro.org>
2018-02-22 14:22:45 +08:00
|
|
|
*
|
|
|
|
* On ARMv6-M, the intlock key is represented by the PRIMASK register,
|
|
|
|
* as BASEPRI is not available.
|
|
|
|
*
|
2018-09-12 09:33:37 +08:00
|
|
|
* @return -EAGAIN, or a return value set by a call to
|
2019-03-09 05:19:05 +08:00
|
|
|
* z_set_thread_return_value()
|
arch: arm: Change method of __swap processing
This patch reworks the current ARM __swap() function into a C function.
Due to some issues with using svc calls withing fault handlers, we
needed to change the way we initiate a swap by removing the dependence
on svc #0.
Before __swap() is called, the system has already done an irq_lock().
Upon return from __swap(), the equivalent of an irq_lock() is done due
to restoration of the key value from the irq_lock preceeding the call.
For ARM V6M (M0/M0+), the pendsv bit is toggled and the irqs are
enabled. There is no priority masking in v6m, so it's just a global
enable. For ARM V7M, the priority mask has to be set to 0x0 to allow
for the pendsv IRQ to be taken. This is done for both via a call to
irq_unlock(0).
After this unlock, a pendsv irq will be taken, either at the tail end
of the current irq handling if we are in handler mode, or immediately
due to the pendsv being asserted (no other outstanding irqs). The next
thread will be scheduled.
Upon return from the context switch to the original
thread, the priority mask will already be correct due to the pendsv
processing.
Signed-off-by: Andy Gross <andy.gross@linaro.org>
2018-02-22 14:22:45 +08:00
|
|
|
*
|
|
|
|
*/
|
2018-09-12 09:33:37 +08:00
|
|
|
int __swap(int key)
|
arch: arm: Change method of __swap processing
This patch reworks the current ARM __swap() function into a C function.
Due to some issues with using svc calls withing fault handlers, we
needed to change the way we initiate a swap by removing the dependence
on svc #0.
Before __swap() is called, the system has already done an irq_lock().
Upon return from __swap(), the equivalent of an irq_lock() is done due
to restoration of the key value from the irq_lock preceeding the call.
For ARM V6M (M0/M0+), the pendsv bit is toggled and the irqs are
enabled. There is no priority masking in v6m, so it's just a global
enable. For ARM V7M, the priority mask has to be set to 0x0 to allow
for the pendsv IRQ to be taken. This is done for both via a call to
irq_unlock(0).
After this unlock, a pendsv irq will be taken, either at the tail end
of the current irq handling if we are in handler mode, or immediately
due to the pendsv being asserted (no other outstanding irqs). The next
thread will be scheduled.
Upon return from the context switch to the original
thread, the priority mask will already be correct due to the pendsv
processing.
Signed-off-by: Andy Gross <andy.gross@linaro.org>
2018-02-22 14:22:45 +08:00
|
|
|
{
|
|
|
|
#ifdef CONFIG_EXECUTION_BENCHMARKING
|
|
|
|
read_timer_start_of_swap();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* store off key and return value */
|
|
|
|
_current->arch.basepri = key;
|
|
|
|
_current->arch.swap_return_value = _k_neg_eagain;
|
|
|
|
|
2018-06-25 21:15:14 +08:00
|
|
|
#if defined(CONFIG_CPU_CORTEX_M)
|
arch: arm: Change method of __swap processing
This patch reworks the current ARM __swap() function into a C function.
Due to some issues with using svc calls withing fault handlers, we
needed to change the way we initiate a swap by removing the dependence
on svc #0.
Before __swap() is called, the system has already done an irq_lock().
Upon return from __swap(), the equivalent of an irq_lock() is done due
to restoration of the key value from the irq_lock preceeding the call.
For ARM V6M (M0/M0+), the pendsv bit is toggled and the irqs are
enabled. There is no priority masking in v6m, so it's just a global
enable. For ARM V7M, the priority mask has to be set to 0x0 to allow
for the pendsv IRQ to be taken. This is done for both via a call to
irq_unlock(0).
After this unlock, a pendsv irq will be taken, either at the tail end
of the current irq handling if we are in handler mode, or immediately
due to the pendsv being asserted (no other outstanding irqs). The next
thread will be scheduled.
Upon return from the context switch to the original
thread, the priority mask will already be correct due to the pendsv
processing.
Signed-off-by: Andy Gross <andy.gross@linaro.org>
2018-02-22 14:22:45 +08:00
|
|
|
/* set pending bit to make sure we will take a PendSV exception */
|
|
|
|
SCB->ICSR |= SCB_ICSR_PENDSVSET_Msk;
|
|
|
|
|
|
|
|
/* clear mask or enable all irqs to take a pendsv */
|
|
|
|
irq_unlock(0);
|
2018-06-25 21:15:14 +08:00
|
|
|
#elif defined(CONFIG_CPU_CORTEX_R)
|
|
|
|
cortex_r_svc();
|
|
|
|
irq_unlock(key);
|
|
|
|
#endif
|
arch: arm: Change method of __swap processing
This patch reworks the current ARM __swap() function into a C function.
Due to some issues with using svc calls withing fault handlers, we
needed to change the way we initiate a swap by removing the dependence
on svc #0.
Before __swap() is called, the system has already done an irq_lock().
Upon return from __swap(), the equivalent of an irq_lock() is done due
to restoration of the key value from the irq_lock preceeding the call.
For ARM V6M (M0/M0+), the pendsv bit is toggled and the irqs are
enabled. There is no priority masking in v6m, so it's just a global
enable. For ARM V7M, the priority mask has to be set to 0x0 to allow
for the pendsv IRQ to be taken. This is done for both via a call to
irq_unlock(0).
After this unlock, a pendsv irq will be taken, either at the tail end
of the current irq handling if we are in handler mode, or immediately
due to the pendsv being asserted (no other outstanding irqs). The next
thread will be scheduled.
Upon return from the context switch to the original
thread, the priority mask will already be correct due to the pendsv
processing.
Signed-off-by: Andy Gross <andy.gross@linaro.org>
2018-02-22 14:22:45 +08:00
|
|
|
|
2019-03-06 21:42:32 +08:00
|
|
|
/* Context switch is performed here. Returning implies the
|
|
|
|
* thread has been context-switched-in again.
|
|
|
|
*/
|
arch: arm: Change method of __swap processing
This patch reworks the current ARM __swap() function into a C function.
Due to some issues with using svc calls withing fault handlers, we
needed to change the way we initiate a swap by removing the dependence
on svc #0.
Before __swap() is called, the system has already done an irq_lock().
Upon return from __swap(), the equivalent of an irq_lock() is done due
to restoration of the key value from the irq_lock preceeding the call.
For ARM V6M (M0/M0+), the pendsv bit is toggled and the irqs are
enabled. There is no priority masking in v6m, so it's just a global
enable. For ARM V7M, the priority mask has to be set to 0x0 to allow
for the pendsv IRQ to be taken. This is done for both via a call to
irq_unlock(0).
After this unlock, a pendsv irq will be taken, either at the tail end
of the current irq handling if we are in handler mode, or immediately
due to the pendsv being asserted (no other outstanding irqs). The next
thread will be scheduled.
Upon return from the context switch to the original
thread, the priority mask will already be correct due to the pendsv
processing.
Signed-off-by: Andy Gross <andy.gross@linaro.org>
2018-02-22 14:22:45 +08:00
|
|
|
return _current->arch.swap_return_value;
|
|
|
|
}
|