context.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /*
  2. * Copyright (C) 2001-2005 by egnite Software GmbH. All rights reserved.
  3. * Copyright (C) 2013 by Michael Fischer
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the copyright holders nor the names of
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  21. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  22. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  23. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  24. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  25. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  26. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  27. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  28. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29. * SUCH DAMAGE.
  30. *
  31. * For additional information see http://www.ethernut.de/
  32. *
  33. */
  34. /*
  35. * $Log$
  36. * Revision 1.00 2010/08/10 18:55:12 ulrichprinz
  37. * Inital version
  38. *
  39. */
  40. #include <cfg/os.h>
  41. #include <cfg/arch.h>
  42. #include <string.h>
  43. #include <sys/osdebug.h>
  44. #include <sys/nutdebug.h>
  45. #include <sys/atom.h>
  46. #include <sys/heap.h>
  47. #include <sys/thread.h>
  48. #define NUTDEBUG_CHECK_STACKMIN
  49. #define NUTDEBUG_CHECK_STACK
  50. /* For M0, push, pop, ldr etc only work on r0-r7
  51. * push{<list>} stores highest register at highest stack address
  52. * pop{<list>} pops highest register from highest stack address
  53. */
  54. #define M0_PushContext() \
  55. __asm__ volatile ( \
  56. "@ Save context\n\t" \
  57. "push {r4-r7,lr}\n\t" /* Save lr, r7, r6, r5, r4. */ \
  58. "mov r4, r8 \n\t" /* Mov high registers */ \
  59. "mov r5, r9 \n\t" \
  60. "mov r6, r10\n\t" \
  61. "mov r7, r11\n\t" \
  62. "push {r4-r7}\n\t" /* Save regs r11, r10, r9, r8*/ \
  63. "mrs r4, psr\n\t" /* Save status. */ \
  64. "push {r4}\n\t" \
  65. "mov r4, sp\n\t" \
  66. "str r4, %0\n\t" \
  67. ::"m" (runningThread->td_sp))
  68. #define M0_PopContext() \
  69. __asm__ __volatile__( \
  70. "@ Load context\n\t" \
  71. "ldr r4, %0\n\t" /* Get new stack address */ \
  72. "mov sp, r4\n\t" /* ... and store as sp */ \
  73. "pop {r4}\n\t" /* Get saved status... */ \
  74. "msr xpsr_nzcvq, r4\n\t" /* ... and restore psr. */ \
  75. "pop {r4-r7}\n\t" /* Pop r11, r10, r9, r8 */ \
  76. "mov r8 , r4\n\t" /* and move tohigh registers*/ \
  77. "mov r9 , r5\n\t" \
  78. "mov r10, r6\n\t" \
  79. "mov r11, r7\n\t" \
  80. "cpsie i\n\t" /* Enable interrupts */ \
  81. "pop {r4-r7, pc}\n\t" /* Pop PC, r7, r6, r5, r4 */ \
  82. ::"m"(runningThread->td_sp))
  83. /*!
  84. * \addtogroup xgNutArchCm3OsContext
  85. */
  86. /*@{*/
  87. /*!
  88. * \brief ARM7TDMI GCC context switch frame layout.
  89. *
  90. * This is the layout of the stack after a thread's context has been
  91. * switched-out. The stack pointer is stored in the thread info and
  92. * points to this structure.
  93. */
  94. typedef struct {
  95. #if defined (MCU_USE_CORTEX_FPU)
  96. uint32_t csf_fpscr;
  97. uint32_t csf_s16;
  98. uint32_t csf_s17;
  99. uint32_t csf_s18;
  100. uint32_t csf_s19;
  101. uint32_t csf_s20;
  102. uint32_t csf_s21;
  103. uint32_t csf_s22;
  104. uint32_t csf_s23;
  105. uint32_t csf_s24;
  106. uint32_t csf_s25;
  107. uint32_t csf_s26;
  108. uint32_t csf_s27;
  109. uint32_t csf_s28;
  110. uint32_t csf_s29;
  111. uint32_t csf_s30;
  112. uint32_t csf_s31;
  113. #endif
  114. #if (__CORTEX_M >= 0x03)
  115. uint32_t csf_cpsr;
  116. uint32_t csf_r4;
  117. uint32_t csf_r5;
  118. uint32_t csf_r6;
  119. uint32_t csf_r7;
  120. uint32_t csf_r8;
  121. uint32_t csf_r9;
  122. uint32_t csf_r10;
  123. uint32_t csf_r11; /* AKA fp */
  124. uint32_t csf_lr;
  125. #else
  126. uint32_t csf_cpsr;
  127. uint32_t csf_r8;
  128. uint32_t csf_r9;
  129. uint32_t csf_r10;
  130. uint32_t csf_r11;
  131. uint32_t csf_r4;
  132. uint32_t csf_r5;
  133. uint32_t csf_r6;
  134. uint32_t csf_r7;
  135. uint32_t csf_lr;
  136. #endif
  137. } SWITCHFRAME;
  138. /*!
  139. * \brief Thread entry frame layout.
  140. *
  141. * This is the stack layout being build to enter a new thread.
  142. */
  143. typedef struct {
  144. uint32_t cef_r0;
  145. uint32_t cef_pc;
  146. } ENTERFRAME;
  147. /*!
  148. * \brief Enter a new thread.
  149. */
  150. static void NutThreadEntry(void) __attribute__ ((naked));
  151. void NutThreadEntry(void)
  152. {
  153. /* Load argument in r0 and jump to thread entry. */
  154. asm volatile ("pop {r0, pc}\n\t":::"r0", "pc");
  155. }
  156. /*!
  157. * \brief Switch to another thread.
  158. *
  159. * Stop the current thread, saving its context. Then start the
  160. * one with the highest priority, which is ready to run.
  161. *
  162. * Application programs typically do not call this function.
  163. *
  164. * \note CPU interrupts must be disabled before calling this function.
  165. *
  166. */
  167. void NutThreadSwitch(void) __attribute__ ((naked));
  168. void NutThreadSwitch(void)
  169. {
  170. /* Save CPU context. */
  171. #if (__CORTEX_M >= 0x03)
  172. __asm__ volatile ( /* */
  173. "@ Save context\n\t" /* */
  174. "stmfd sp!, {r4-r11, lr}\n\t" /* Save registers. */
  175. "mrs r4, psr\n\t" /* Save status. */
  176. "stmfd sp!, {r4}\n\t" /* */
  177. #if defined (MCU_USE_CORTEX_FPU)
  178. "vpush {s16-s31}\n\t" /* Save registers. */
  179. "vmrs r4, fpscr\n\t" /* Save FPU status. */
  180. "stmfd sp!, {r4}\n\t" /* */
  181. #endif
  182. "str sp, %0" /* Save stack pointer. */
  183. ::"m" (runningThread->td_sp) /* */
  184. );
  185. #else
  186. M0_PushContext();
  187. #endif
  188. /* Select thread on top of the run queue. */
  189. runningThread = runQueue;
  190. runningThread->td_state = TDS_RUNNING;
  191. /* Restore context. */
  192. #if (__CORTEX_M >= 0x03)
  193. __asm__ __volatile__( /* */
  194. "@ Load context\n\t" /* */
  195. "ldr sp, %0\n\t" /* Restore stack pointer. */
  196. #if defined (MCU_USE_CORTEX_FPU)
  197. "ldmfd sp!, {r4}\n\t" /* Get saved FPU status... */
  198. "vmsr fpscr, r4\n\t" /* ...and save back. */
  199. "vpop {s16-s31}\n\t" /* Restore FPU registers. */
  200. #endif
  201. "ldmfd sp!, {r4}\n\t" /* Get saved status... */
  202. "msr xpsr_nzcvq, r4\n\t" /* ...and save execution and application status in psr. */
  203. "cpsie i\n\t" /* ...enable interrupts */
  204. "ldmfd sp!, {r4-r11, pc}\n\t" /* Restore registers. */
  205. ::"m"(runningThread->td_sp) /* */
  206. );
  207. #else
  208. M0_PopContext();
  209. #endif
  210. #if defined(NUT_CRITICAL_NESTING) && !defined(NUT_CRITICAL_NESTING_STACK)
  211. critical_nesting_level = 0;
  212. #endif
  213. }
  214. /*!
  215. * \brief Create a new thread.
  216. *
  217. * If the current thread's priority is lower or equal than the default
  218. * priority (64), then the current thread is stopped and the new one
  219. * is started.
  220. *
  221. * \param name String containing the symbolic name of the new thread,
  222. * up to 8 characters long.
  223. * \param fn The thread's entry point, typically created by the
  224. * THREAD macro.
  225. * \param arg Argument pointer passed to the new thread.
  226. * \param stackSize Number of bytes of the stack space allocated for
  227. * the new thread.
  228. *
  229. * \note The thread must run in ARM mode. Thumb mode is not supported.
  230. *
  231. * \return Pointer to the NUTTHREADINFO structure or 0 to indicate an
  232. * error.
  233. */
  234. HANDLE NutThreadCreate(char * name, void (*fn) (void *), void *arg, size_t stackSize)
  235. {
  236. uint8_t *threadMem;
  237. SWITCHFRAME *sf;
  238. ENTERFRAME *ef;
  239. NUTTHREADINFO *td;
  240. size_t alloc_size;
  241. /*
  242. * Allocate stack and thread info structure in one block.
  243. * We sill setup the following layout:
  244. *
  245. * Upper memory addresses.
  246. *
  247. * +--------------------+
  248. * I I
  249. * I NUTTHREADINFO I
  250. * I I
  251. * td -> +-----+--------------+ <- Stack top
  252. * I I I
  253. * I T I ENTERFRAME I
  254. * I H I I
  255. * ef -> I R +--------------+
  256. * I E I I ^
  257. * I A I SWITCHFRAME I I
  258. * I D I I I pop moves up
  259. * sf -> I +--------------+ <- Initial stack pointer
  260. * I S I I I push moves down
  261. * I T I Application I I
  262. * I A I Stack I V
  263. * I C I I
  264. * I K I I
  265. * threadMem -> +-----+--------------+ <- Stack bottom
  266. *
  267. * Lower memory addresses.
  268. */
  269. alloc_size = stackSize + sizeof(NUTTHREADINFO);
  270. alloc_size += 7;
  271. alloc_size &= ~7;
  272. if ((threadMem = NutHeapAlloc(alloc_size)) == 0) {
  273. return 0;
  274. }
  275. td = (NUTTHREADINFO *) (((uint32_t)(threadMem + stackSize)) & ~7);
  276. ef = (ENTERFRAME *) ((uintptr_t) td - sizeof(ENTERFRAME));
  277. sf = (SWITCHFRAME *) ((uintptr_t) ef - sizeof(SWITCHFRAME));
  278. /*
  279. * Set predefined values at the stack bottom. May be used to detect
  280. * stack overflows.
  281. */
  282. #if defined(NUTDEBUG_CHECK_STACKMIN) || defined(NUTDEBUG_CHECK_STACK)
  283. {
  284. uint32_t *fip = (uint32_t *)threadMem;
  285. while (fip < (uint32_t *)sf) {
  286. *fip++ = DEADBEEF;
  287. }
  288. }
  289. #else
  290. *((uint32_t *) threadMem) = DEADBEEF;
  291. *((uint32_t *) (threadMem + 4)) = DEADBEEF;
  292. *((uint32_t *) (threadMem + 8)) = DEADBEEF;
  293. *((uint32_t *) (threadMem + 12)) = DEADBEEF;
  294. #endif
  295. /*
  296. * Setup the entry frame to simulate C function entry.
  297. */
  298. ef->cef_pc = (uintptr_t) fn;
  299. ef->cef_r0 = (uintptr_t) arg;
  300. /*
  301. * Setup the switch frame.
  302. */
  303. memset(sf, 0x00, sizeof(SWITCHFRAME));
  304. sf->csf_lr = (uintptr_t) NutThreadEntry;
  305. sf->csf_cpsr = 0;
  306. #if defined (MCU_USE_CORTEX_FPU)
  307. sf->csf_fpscr = 0;
  308. #endif
  309. /*
  310. * Initialize the thread info structure and insert it into the
  311. * thread list and the run queue.
  312. */
  313. memcpy(td->td_name, name, sizeof(td->td_name) - 1);
  314. td->td_name[sizeof(td->td_name) - 1] = 0;
  315. td->td_state = TDS_READY;
  316. td->td_sp = (uintptr_t) sf;
  317. td->td_priority = 64;
  318. td->td_memory = threadMem;
  319. td->td_timer = 0;
  320. td->td_queue = 0;
  321. NutEnterCritical();
  322. td->td_next = nutThreadList;
  323. nutThreadList = td;
  324. NutThreadAddPriQueue(td, (NUTTHREADINFO **) & runQueue);
  325. /*
  326. * If no thread is running, then this is the first thread ever
  327. * created. In Nut/OS, the idle thread is created first.
  328. */
  329. if (runningThread == 0) {
  330. /* This will never return. */
  331. runningThread = runQueue;
  332. runningThread->td_state = TDS_RUNNING;
  333. /* Restore context. */
  334. #if (__CORTEX_M >= 0x03)
  335. __asm__ __volatile__( /* */
  336. "@ Load context\n\t" /* */
  337. "ldr sp, %0\n\t" /* Restore stack pointer. */
  338. #if defined (MCU_USE_CORTEX_FPU)
  339. "ldmfd sp!, {r4}\n\t" /* Get saved FPU status... */
  340. "vmsr fpscr, r4\n\t" /* ...and save back. */
  341. "vpop {s16-s31}\n\t" /* Restore FPU registers. */
  342. #endif
  343. "ldmfd sp!, {r4}\n\t" /* Get saved status... */
  344. "msr xpsr_nzcvq, r4\n\t" /* ...and save execution and application status in psr. */
  345. "cpsie i\n\t" /* ...enable interrupts */
  346. "ldmfd sp!, {r4-r11, pc}\n\t" /* Restore registers. */
  347. ::"m"(runningThread->td_sp) /* */
  348. );
  349. #else
  350. M0_PopContext();
  351. #endif
  352. }
  353. /*
  354. * If current context is not in front of the run queue (highest
  355. * priority), then switch to the thread in front.
  356. */
  357. if (runningThread != runQueue) {
  358. runningThread->td_state = TDS_READY;
  359. NutThreadSwitch();
  360. }
  361. NutExitCritical();
  362. return td;
  363. }
  364. /*@}*/