context_gcc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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/avr/os/context_gcc.c
  35. * \brief Context switching ported to AVR GCC.
  36. *
  37. * \verbatim File version $Id: context_gcc.c 4937 2013-01-22 11:38:42Z haraldkipp $ \endverbatim
  38. */
  39. #include <cfg/os.h>
  40. #include <cfg/memory.h>
  41. #include <string.h>
  42. #include <sys/atom.h>
  43. #include <sys/heap.h>
  44. #include <sys/thread.h>
  45. /* Support for separate irq stack only for avr-gcc */
  46. #include <dev/irqstack.h>
  47. #ifdef NUTDEBUG
  48. #include <sys/osdebug.h>
  49. #endif
  50. /*!
  51. * \addtogroup xgNutArchAvrOsContextGcc GCC Context Switching for AVR CPUs
  52. * \ingroup xgNutArchAvrOs
  53. * \brief Thread context switching for avr-gcc.
  54. */
  55. /*@{*/
  56. #ifdef IRQSTACK_SIZE
  57. /*! * \brief Decrement value for thread's stack size, if separate irq stack is used.
  58. *
  59. * If separate irq stack is enabled (avr-gcc only), the initial parameter
  60. * 'stacksize' in function 'NutThreadCreate' is decremented by this value, if the
  61. * remaining stack size is 128 bytes or greater.
  62. *
  63. */
  64. uint16_t _irqstackdec = 128;
  65. #endif /* #ifdef IRQSTACK_SIZE */
  66. /*!
  67. * \brief AVR GCC context switch frame layout.
  68. *
  69. * This is the layout of the stack after a thread's context has been
  70. * switched-out.
  71. */
  72. typedef struct {
  73. uint8_t csf_r29;
  74. uint8_t csf_r28;
  75. uint8_t csf_r17;
  76. uint8_t csf_r16;
  77. uint8_t csf_r15;
  78. uint8_t csf_r14;
  79. uint8_t csf_r13;
  80. uint8_t csf_r12;
  81. uint8_t csf_r11;
  82. uint8_t csf_r10;
  83. uint8_t csf_r9;
  84. uint8_t csf_r8;
  85. uint8_t csf_r7;
  86. uint8_t csf_r6;
  87. uint8_t csf_r5;
  88. uint8_t csf_r4;
  89. uint8_t csf_r3;
  90. uint8_t csf_r2;
  91. #ifdef __AVR_3_BYTE_PC__
  92. uint8_t csf_pcex;
  93. #endif
  94. uint8_t csf_pchi;
  95. uint8_t csf_pclo;
  96. } SWITCHFRAME;
  97. /*!
  98. * \brief Thread entry frame layout.
  99. *
  100. * This is the stack layout being build to enter a new thread.
  101. */
  102. typedef struct {
  103. uint8_t cef_arghi;
  104. uint8_t cef_arglo;
  105. uint8_t cef_rampz;
  106. uint8_t cef_sreg;
  107. uint8_t cef_r1;
  108. #ifdef __AVR_3_BYTE_PC__
  109. uint8_t cef_pcex;
  110. #endif
  111. uint8_t cef_pchi;
  112. uint8_t cef_pclo;
  113. } ENTERFRAME;
  114. #define LONG_PTR_P(lp, mem_p) \
  115. __asm__ __volatile__("ldi %A0, lo8("#mem_p ")" "\n\t" \
  116. "ldi %B0, hi8("#mem_p ")" "\n\t" \
  117. "ldi %C0, hh8("#mem_p ")" "\n\t" \
  118. "clr %D0" \
  119. :"=d" (lp))
  120. /*
  121. * This code is executed when entering a thread.
  122. */
  123. static void NutThreadEntry(void) NUT_NAKED_FUNC;
  124. static void NutThreadEntry(void)
  125. {
  126. __asm__ __volatile__("pop r25" "\n\t" /* first parameter hi-byte */
  127. "pop r24" "\n\t" /* first parameter lo-byte */
  128. "pop __tmp_reg__" "\n\t" /* Get RAMPZ */
  129. "out %0, __tmp_reg__" "\n\t" /* Restore RAMPZ */
  130. "pop __tmp_reg__" "\n\t" /* Get SREG */
  131. "out %1, __tmp_reg__" "\n\t" /* Restore SREG */
  132. "pop __zero_reg__" "\n\t" /* Zero register */
  133. "reti" "\n\t" /* enables interrupts */
  134. ::"I" _SFR_IO_ADDR(RAMPZ), "I" _SFR_IO_ADDR(SREG)
  135. );
  136. }
  137. void NutThreadSwitch(void) NUT_PREVENT_INLINE NUT_NAKED_FUNC;
  138. void NutThreadSwitch(void)
  139. {
  140. /*
  141. * Save all CPU registers.
  142. */
  143. asm volatile ("push r2" "\n\t" /* */
  144. "push r3" "\n\t" /* */
  145. "push r4" "\n\t" /* */
  146. "push r5" "\n\t" /* */
  147. "push r6" "\n\t" /* */
  148. "push r7" "\n\t" /* */
  149. "push r8" "\n\t" /* */
  150. "push r9" "\n\t" /* */
  151. "push r10" "\n\t" /* */
  152. "push r11" "\n\t" /* */
  153. "push r12" "\n\t" /* */
  154. "push r13" "\n\t" /* */
  155. "push r14" "\n\t" /* */
  156. "push r15" "\n\t" /* */
  157. "push r16" "\n\t" /* */
  158. "push r17" "\n\t" /* */
  159. "push r28" "\n\t" /* */
  160. "push r29" "\n\t" /* */
  161. "in %A0, %1" "\n\t" /* */
  162. "in %B0, %2" "\n\t" /* */
  163. :"=r" (runningThread->td_sp) /* */
  164. :"I" _SFR_IO_ADDR(SPL), /* */
  165. "I" _SFR_IO_ADDR(SPH) /* */
  166. );
  167. /*
  168. * This defines a global label, which may be called
  169. * as an entry point into this function.
  170. */
  171. asm volatile (".global thread_start\n" /* */
  172. "thread_start:\n\t"::);
  173. /*
  174. * Reload CPU registers from the thread on top of the run queue.
  175. */
  176. runningThread = runQueue;
  177. runningThread->td_state = TDS_RUNNING;
  178. asm volatile ("out %1, %A0" "\n\t" /* */
  179. "out %2, %B0" "\n\t" /* */
  180. "pop r29" "\n\t" /* */
  181. "pop r28" "\n\t" /* */
  182. "pop r17" "\n\t" /* */
  183. "pop r16" "\n\t" /* */
  184. "pop r15" "\n\t" /* */
  185. "pop r14" "\n\t" /* */
  186. "pop r13" "\n\t" /* */
  187. "pop r12" "\n\t" /* */
  188. "pop r11" "\n\t" /* */
  189. "pop r10" "\n\t" /* */
  190. "pop r9" "\n\t" /* */
  191. "pop r8" "\n\t" /* */
  192. "pop r7" "\n\t" /* */
  193. "pop r6" "\n\t" /* */
  194. "pop r5" "\n\t" /* */
  195. "pop r4" "\n\t" /* */
  196. "pop r3" "\n\t" /* */
  197. "pop r2" "\n\t" /* */
  198. "ret" "\n\t" /* */
  199. ::"r" (runningThread->td_sp), /* */
  200. "I" _SFR_IO_ADDR(SPL), /* */
  201. "I" _SFR_IO_ADDR(SPH) /* */
  202. );
  203. }
  204. HANDLE NutThreadCreate(char * name, void (*fn) (void *), void *arg, size_t stackSize)
  205. {
  206. uint8_t *threadMem;
  207. SWITCHFRAME *sf;
  208. ENTERFRAME *ef;
  209. NUTTHREADINFO *td;
  210. #ifdef IRQSTACK_SIZE
  211. if (stackSize > _irqstackdec + 128) stackSize -= _irqstackdec;
  212. #endif
  213. /*
  214. * Allocate stack and thread info structure in one block.
  215. */
  216. if ((threadMem = NutStackAlloc(stackSize + sizeof(NUTTHREADINFO))) == 0) {
  217. return 0;
  218. }
  219. td = (NUTTHREADINFO *) (threadMem + stackSize);
  220. ef = (ENTERFRAME *) ((uint16_t) td - sizeof(ENTERFRAME));
  221. sf = (SWITCHFRAME *) ((uint16_t) ef - sizeof(SWITCHFRAME));
  222. memcpy(td->td_name, name, sizeof(td->td_name) - 1);
  223. td->td_name[sizeof(td->td_name) - 1] = 0;
  224. td->td_sp = (uint16_t) sf - 1;
  225. td->td_memory = threadMem;
  226. /*
  227. * Set predefined values at the stack bottom. May be used to detect
  228. * stack overflows.
  229. */
  230. #if defined(NUTDEBUG_CHECK_STACKMIN) || defined(NUTDEBUG_CHECK_STACK)
  231. {
  232. uint32_t *fip = (uint32_t *)threadMem;
  233. while (fip < (uint32_t *)sf) {
  234. *fip++ = DEADBEEF;
  235. }
  236. }
  237. #else
  238. *((uint32_t *) threadMem) = DEADBEEF;
  239. *((uint32_t *) (threadMem + 4)) = DEADBEEF;
  240. *((uint32_t *) (threadMem + 8)) = DEADBEEF;
  241. *((uint32_t *) (threadMem + 12)) = DEADBEEF;
  242. #endif
  243. td->td_priority = 64;
  244. /*
  245. * Setup entry frame to simulate C function entry.
  246. */
  247. #ifdef __AVR_3_BYTE_PC__
  248. ef->cef_pcex = 0;
  249. #endif
  250. ef->cef_pchi = (uint8_t) (((uint16_t) fn) >> 8);
  251. ef->cef_pclo = (uint8_t) (((uint16_t) fn) & 0xff);
  252. ef->cef_sreg = 0x80;
  253. ef->cef_rampz = 0;
  254. ef->cef_r1 = 0;
  255. ef->cef_arglo = (uint8_t) (((uint16_t) arg) & 0xff);
  256. ef->cef_arghi = (uint8_t) (((uint16_t) arg) >> 8);
  257. #ifdef __AVR_3_BYTE_PC__
  258. sf->csf_pcex = 0;
  259. #endif
  260. sf->csf_pchi = (uint8_t) (((uint16_t) NutThreadEntry) >> 8);
  261. sf->csf_pclo = (uint8_t) (((uint16_t) NutThreadEntry) & 0xff);
  262. /*
  263. * Insert into the thread list and the run queue.
  264. */
  265. td->td_next = nutThreadList;
  266. nutThreadList = td;
  267. td->td_state = TDS_READY;
  268. td->td_timer = 0;
  269. td->td_queue = 0;
  270. #ifdef NUTDEBUG
  271. if (__os_trf)
  272. fprintf(__os_trs, "Cre<%04x>", (uintptr_t) td);
  273. #endif
  274. NutThreadAddPriQueue(td, (NUTTHREADINFO **) & runQueue);
  275. #ifdef NUTDEBUG
  276. if (__os_trf) {
  277. NutDumpThreadList(__os_trs);
  278. //NutDumpThreadQueue(__os_trs, runQueue);
  279. }
  280. #endif
  281. /*
  282. * If no thread is active, switch to new thread.
  283. */
  284. if (runningThread == 0) {
  285. NutEnterCritical();
  286. asm volatile ("rjmp thread_start\n\t"::);
  287. /* we will never come back here .. */
  288. }
  289. /*
  290. * If current context is not in front of
  291. * the run queue (highest priority), then
  292. * switch to the thread in front.
  293. */
  294. if (runningThread != runQueue) {
  295. runningThread->td_state = TDS_READY;
  296. #ifdef NUTDEBUG
  297. if (__os_trf)
  298. fprintf(__os_trs, "New<%04x %04x>", (uintptr_t) runningThread, (uintptr_t) runQueue);
  299. #endif
  300. NutEnterCritical();
  301. NutThreadSwitch();
  302. NutExitCritical();
  303. }
  304. return td;
  305. }
  306. /*@}*/