context.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * Copyright (C) 2001-2005 by egnite Software GmbH. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the copyright holders nor the names of
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  24. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  25. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  27. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. *
  30. * For additional information see http://www.ethernut.de/
  31. *
  32. */
  33. /*!
  34. * \file arch/arm/os/context.c
  35. * \brief Context switching ported to ARM.
  36. *
  37. * \verbatim File version $Id: context.c 5472 2013-12-06 00:16:28Z olereinhardt $ \endverbatim
  38. */
  39. #include <cfg/os.h>
  40. #include <string.h>
  41. #include <sys/osdebug.h>
  42. #include <sys/nutdebug.h>
  43. #include <sys/atom.h>
  44. #include <sys/heap.h>
  45. #include <sys/thread.h>
  46. /*!
  47. * \addtogroup xgNutArchArmOsContext Context Switching for ARM CPUs
  48. * \ingroup xgNutArchArmOs
  49. * \brief Context Switching for ARM CPUs
  50. *
  51. * This code should work with other GCC distributions for the ARM CPU.
  52. */
  53. /*@{*/
  54. /*!
  55. * \brief ARM7TDMI GCC context switch frame layout.
  56. *
  57. * This is the layout of the stack after a thread's context has been
  58. * switched-out. The stack pointer is stored in the thread info and
  59. * points to this structure.
  60. */
  61. typedef struct {
  62. uint32_t csf_cpsr;
  63. uint32_t csf_r4;
  64. uint32_t csf_r5;
  65. uint32_t csf_r6;
  66. uint32_t csf_r7;
  67. uint32_t csf_r8;
  68. uint32_t csf_r9;
  69. uint32_t csf_r10;
  70. uint32_t csf_r11; /* AKA fp */
  71. uint32_t csf_lr;
  72. } SWITCHFRAME;
  73. /*!
  74. * \brief Thread entry frame layout.
  75. *
  76. * This is the stack layout being build to enter a new thread.
  77. */
  78. typedef struct {
  79. uint32_t cef_r0;
  80. uint32_t cef_pc;
  81. } ENTERFRAME;
  82. /*!
  83. * \brief Enter a new thread.
  84. */
  85. static void NutThreadEntry(void) NUT_NAKED_FUNC;
  86. void NutThreadEntry(void)
  87. {
  88. /* Load argument in r0 and jump to thread entry. */
  89. asm volatile ("ldmfd sp!, {r0, lr}\n\tbx lr":::"r0", "lr");
  90. }
  91. void NutThreadSwitch(void) NUT_NAKED_FUNC;
  92. void NutThreadSwitch(void)
  93. {
  94. __asm__ __volatile__
  95. /* Save CPU context. */
  96. ("@ Save context\n\t"
  97. /* Save registers. */
  98. "stmfd sp!, {r4-r11, lr}\n\t"
  99. /* Save status. */
  100. "mrs r0, cpsr\n\t"
  101. /* */
  102. "stmfd sp!, {r0}\n\t"
  103. /* Save stack pointer. */
  104. "str sp, %[td_sp]"
  105. /* Output. */
  106. :
  107. /* Input. */
  108. :[td_sp] "o"(runningThread->td_sp)
  109. /* Clobbers. */
  110. :"r0", "memory");
  111. /* Select thread on top of the run queue. */
  112. runningThread = runQueue;
  113. runningThread->td_state = TDS_RUNNING;
  114. #if defined(NUT_CRITICAL_NESTING) && !defined(NUT_CRITICAL_NESTING_STACK)
  115. critical_nesting_level = 0;
  116. #endif
  117. __asm__ __volatile__
  118. /* Restore context. */
  119. ("@ Reload context\n\t"
  120. /* Restore stack pointer. */
  121. "ldr sp, %[td_sp]\n\t"
  122. /* Get saved status... */
  123. "ldmfd sp!, {r0}\n\t"
  124. /* ...enable interrupts */
  125. "bic r0, r0, #0xC0\n\t"
  126. /* ...and save in spsr. */
  127. "msr spsr, r0\n\t"
  128. /* Restore registers. */
  129. "ldmfd sp!, {r4-r11, lr}\n\t"
  130. /* Restore status and return. */
  131. "movs pc, lr"
  132. /* Output. */
  133. :
  134. /* Input. */
  135. :[td_sp] "m"(runningThread->td_sp)
  136. /* Clobbers. */
  137. :"r0", "memory");
  138. }
  139. HANDLE NutThreadCreate(char * name, void (*fn) (void *), void *arg, size_t stackSize)
  140. {
  141. uint8_t *threadMem;
  142. SWITCHFRAME *sf;
  143. ENTERFRAME *ef;
  144. NUTTHREADINFO *td;
  145. size_t alloc_size;
  146. /*
  147. * Allocate stack and thread info structure in one block.
  148. * We sill setup the following layout:
  149. *
  150. * Upper memory addresses.
  151. *
  152. * +--------------------+
  153. * I I
  154. * I NUTTHREADINFO I
  155. * I I
  156. * td -> +-----+--------------+ <- Stack top
  157. * I I I
  158. * I T I ENTERFRAME I
  159. * I H I I
  160. * ef -> I R +--------------+
  161. * I E I I ^
  162. * I A I SWITCHFRAME I I
  163. * I D I I I pop moves up
  164. * sf -> I +--------------+ <- Initial stack pointer
  165. * I S I I I push moves down
  166. * I T I Application I I
  167. * I A I Stack I V
  168. * I C I I
  169. * I K I I
  170. * threadMem -> +-----+--------------+ <- Stack bottom
  171. *
  172. * Lower memory addresses.
  173. */
  174. alloc_size = stackSize + sizeof(NUTTHREADINFO);
  175. alloc_size += 7;
  176. alloc_size &= ~7;
  177. if ((threadMem = NutHeapAlloc(alloc_size)) == 0) {
  178. return 0;
  179. }
  180. td = (NUTTHREADINFO *) (threadMem + stackSize);
  181. ef = (ENTERFRAME *) ((uintptr_t) td - sizeof(ENTERFRAME));
  182. sf = (SWITCHFRAME *) ((uintptr_t) ef - sizeof(SWITCHFRAME));
  183. /*
  184. * Set predefined values at the stack bottom. May be used to detect
  185. * stack overflows.
  186. */
  187. #if defined(NUTDEBUG_CHECK_STACKMIN) || defined(NUTDEBUG_CHECK_STACK)
  188. {
  189. uint32_t *fip = (uint32_t *)threadMem;
  190. while (fip < (uint32_t *)sf) {
  191. *fip++ = DEADBEEF;
  192. }
  193. }
  194. #else
  195. *((uint32_t *) threadMem) = DEADBEEF;
  196. *((uint32_t *) (threadMem + 4)) = DEADBEEF;
  197. *((uint32_t *) (threadMem + 8)) = DEADBEEF;
  198. *((uint32_t *) (threadMem + 12)) = DEADBEEF;
  199. #endif
  200. /*
  201. * Setup the entry frame to simulate C function entry.
  202. */
  203. ef->cef_pc = (uintptr_t) fn;
  204. ef->cef_r0 = (uintptr_t) arg;
  205. /*
  206. * Setup the switch frame.
  207. */
  208. sf->csf_lr = (uintptr_t) NutThreadEntry;
  209. sf->csf_cpsr = ARM_CPSR_I_BIT | ARM_CPSR_F_BIT | ARM_MODE_SYS;
  210. /*
  211. * Initialize the thread info structure and insert it into the
  212. * thread list and the run queue.
  213. */
  214. memcpy(td->td_name, name, sizeof(td->td_name) - 1);
  215. td->td_name[sizeof(td->td_name) - 1] = 0;
  216. td->td_state = TDS_READY;
  217. td->td_sp = (uintptr_t) sf;
  218. td->td_priority = 64;
  219. td->td_memory = threadMem;
  220. td->td_timer = 0;
  221. td->td_queue = 0;
  222. NutEnterCritical();
  223. td->td_next = nutThreadList;
  224. nutThreadList = td;
  225. NutThreadAddPriQueue(td, (NUTTHREADINFO **) & runQueue);
  226. /*
  227. * If no thread is running, then this is the first thread ever
  228. * created. In Nut/OS, the idle thread is created first.
  229. */
  230. if (runningThread == 0) {
  231. /* This will never return. */
  232. runningThread = runQueue;
  233. runningThread->td_state = TDS_RUNNING;
  234. __asm__ __volatile__
  235. /* Load initial idle thread context. */
  236. ("@ Load context\n\t"
  237. /* Restore stack pointer. */
  238. "ldr sp, %[td_sp]\n\t"
  239. /* Get saved status... */
  240. "ldmfd sp!, {r0}\n\t"
  241. /* ...enable interrupts */
  242. "bic r0, r0, #0xC0\n\t"
  243. /* ...and save in spsr. */
  244. "msr spsr, r0\n\t"
  245. /* Restore registers. */
  246. "ldmfd sp!, {r4-r11, lr}\n\t"
  247. /* Restore status and return. */
  248. "movs pc, lr"
  249. /* Input. */
  250. :
  251. /* Output. */
  252. :[td_sp] "m" (runningThread->td_sp)
  253. /* Clobbers. */
  254. :"r0", "memory");
  255. }
  256. /*
  257. * If current context is not in front of the run queue (highest
  258. * priority), then switch to the thread in front.
  259. */
  260. if (runningThread != runQueue) {
  261. runningThread->td_state = TDS_READY;
  262. NutThreadSwitch();
  263. }
  264. NutExitCritical();
  265. return td;
  266. }
  267. /*@}*/