4 * This file is part of BeRTOS.
6 * Bertos is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * As a special exception, you may use this file as part of a free software
21 * library without restriction. Specifically, if other files instantiate
22 * templates or use macros or inline functions from this file, or you compile
23 * this file and link it with other files to produce an executable, this
24 * file does not by itself cause the resulting executable to be covered by
25 * the GNU General Public License. This exception does not however
26 * invalidate any other reasons why the executable file might be covered by
27 * the GNU General Public License.
29 * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
33 * \brief Cortex-M3 context switch
35 * \author Andrea Righi <arighi@develer.com>
38 #include <cfg/compiler.h>
39 #include <cfg/cfg_proc.h> /* CONFIG_KERN_PREEMPT */
40 #include <cpu/irq.h> /* IRQ_PRIO_DISABLED */
41 #include <cpu/types.h> /* cpu_stack_t */
42 #include <kern/proc_p.h> /* asm_switch_context() prototype */
43 #include <kern/proc.h> /* proc_preempt() */
44 #include "switch_ctx_cm3.h"
46 #if CONFIG_KERN_PREEMPT
48 * Kernel preemption: implementation details.
50 * The kernel preemption is implemented using the PendSV IRQ. Inside the
51 * SysTick handler when a process needs to be interrupted (expires its time
52 * quantum or a high-priority process is awakend) a pending PendSV call is
55 * The PendSV handler is called immediately after the SysTick handler, using
56 * the architecture's tail-chaining functionality (an ISR call without the
57 * overhead of state saving and restoration between different IRQs). Inside the
58 * PendSV handler we perform the stack-switching between the old and new
61 * Voluntary context switch is implemented as a soft-interrupt call (SVCall),
62 * so any process is always suspended and resumed from an interrupt context.
64 * NOTE: interrupts must be disabled or enabled when resuming a process context
65 * depending of the type of the previous suspension. If a process was suspended
66 * by a voluntary context switch IRQs must be disabled on resume (voluntary
67 * context switch always happen with IRQs disabled). Instead, if a process was
68 * suspended by the kernel preemption IRQs must be always re-enabled, because
69 * the PendSV handler resumes directly the process context. To keep track of
70 * this, we save the state of the IRQ priority in register r3 before performing
73 * If CONFIG_KERN_PREEMPT is not enabled the cooperative implementation
74 * fallbacks to the default stack-switching mechanism, performed directly in
75 * thread-mode and implemented as a normal function call.
79 * Voluntary context switch handler.
81 void NAKED svcall_handler(void)
87 "stmdb ip!, {r3-r11, lr}\n\t"
92 "ldmia ip!, {r3-r11, lr}\n\t"
95 "bx lr" : : : "memory");
99 * Preemptible context switch handler.
101 void NAKED pendsv_handler(void)
103 register cpu_stack_t *stack asm("ip");
106 "mrs r3, basepri\n\t"
108 "msr basepri, %0\n\t"
110 "stmdb %0!, {r3-r11, lr}\n\t"
112 : "r"(stack), "i"(IRQ_PRIO_DISABLED)
114 proc_current()->stack = stack;
116 stack = proc_current()->stack;
118 "ldmia %0!, {r3-r11, lr}\n\t"
120 "msr basepri, r3\n\t"
122 : "=r"(stack) : "r"(stack)
125 #else /* !CONFIG_KERN_PREEMPT */
126 void NAKED asm_switch_context(cpu_stack_t **new_sp, cpu_stack_t **old_sp)
128 register cpu_stack_t **_new_sp asm("r0") = new_sp;
129 register cpu_stack_t **_old_sp asm("r1") = old_sp;
134 "stmdb ip!, {r4-r11, lr}\n\t"
135 /* Save old stack pointer */
137 /* Load new stack pointer */
139 /* Load new registers */
140 "ldmia ip!, {r4-r11, lr}\n\t"
143 : : "r"(_new_sp), "r"(_old_sp) : "ip", "memory");
145 #endif /* CONFIG_KERN_PREEMPT */