thread.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * Copyright (C) 2004 by Jan Dubiec. 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 JAN DUBIEC 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 JAN DUBIEC
  21. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  23. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. /*
  30. * $Log$
  31. * Revision 1.2 2006/02/28 16:16:04 freckle
  32. * terminating \0 in thread name
  33. *
  34. * Revision 1.1 2005/05/27 17:18:41 drsung
  35. * Moved the file.
  36. *
  37. * Revision 1.2 2005/04/30 16:42:42 chaac
  38. * Fixed bug in handling of NUTDEBUG. Added include for cfg/os.h. If NUTDEBUG
  39. * is defined in NutConf, it will make effect where it is used.
  40. *
  41. * Revision 1.1 2004/03/16 16:48:46 haraldkipp
  42. * Added Jan Dubiec's H8/300 port.
  43. *
  44. */
  45. #include <cfg/os.h>
  46. /*!
  47. * \brief H8/300 GCC context switch frame layout.
  48. *
  49. * This is the layout of the stack after a thread's context has been
  50. * switched-out.
  51. */
  52. typedef struct __attribute__ ((packed)) {
  53. u_long csf_er6;
  54. u_long csf_er5;
  55. u_long csf_er4;
  56. u_long csf_er3;
  57. u_long csf_er2;
  58. u_long csf_er1;
  59. u_long csf_er0;
  60. u_short csf_ccr;
  61. u_long csf_pc;
  62. } SWITCHFRAME;
  63. /*!
  64. * \brief H8/300 GCC thread entry frame layout.
  65. *
  66. * This is the stack layout being build to enter a new thread.
  67. */
  68. typedef struct {
  69. u_long cef_er0; /* Thread argument (void* arg) */
  70. u_long cef_ccr_pc; /* ccr<-@sp+, pc<-@sp+ */
  71. } ENTERFRAME;
  72. __attribute__ ((interrupt_handler))
  73. static void NutThreadEntry(void)
  74. {
  75. asm volatile ("pop.l er0" "\n\t"); /* thread argument */
  76. /* RTE instruction is generatet automatically by H8 gcc */
  77. }
  78. /*!
  79. * \brief Switch to another thread.
  80. *
  81. * Stop the current thread, saving its context. Then start the
  82. * one with the highest priority, which is ready to run.
  83. *
  84. * Application programs typically do not call this function.
  85. *
  86. * \note CPU interrupts must be disabled before calling this function.
  87. *
  88. */
  89. __attribute__ ((noinline))
  90. void NutThreadSwitch(void)
  91. {
  92. asm volatile ("stc.w ccr, @-er7" "\n\t"
  93. "push.l er0" "\n\t"
  94. "push.l er1" "\n\t"
  95. "push.l er2" "\n\t"
  96. "push.l er3" "\n\t"
  97. "push.l er4" "\n\t" "push.l er5" "\n\t" "push.l er6" "\n\t" "mov.l er7,%0" "\n\t":"=r" (runningThread->td_sp):);
  98. /*
  99. * This defines a global label, which may be called
  100. * as an entry point into this function.
  101. */
  102. asm volatile (".globl thread_start" "\n\t" "thread_start:" "\n\t"::);
  103. /*
  104. * Reload CPU registers from the thread in front
  105. * of the run queue.
  106. */
  107. runningThread = runQueue;
  108. runningThread->td_state = TDS_RUNNING;
  109. asm volatile ("mov.l %0,er7" "\n\t"
  110. "pop.l er6" "\n\t"
  111. "pop.l er5" "\n\t"
  112. "pop.l er4" "\n\t"
  113. "pop.l er3" "\n\t"
  114. "pop.l er2" "\n\t" "pop.l er1" "\n\t" "pop.l er0" "\n\t" "ldc.w @er7+, ccr" "\n\t"::"p" (runningThread->td_sp));
  115. }
  116. /*!
  117. * \brief Create a new thread.
  118. *
  119. * If the current thread's priority is lower or equal than the default
  120. * priority (64), then the current thread is stopped and the new one
  121. * is started.
  122. *
  123. * \param name String containing the symbolic name of the new thread,
  124. * up to 8 characters long.
  125. * \param fn The thread's entry point, typically created by the
  126. * THREAD macro.
  127. * \param arg Argument pointer passed to the new thread.
  128. * \param stackSize Number of bytes of the stack space allocated for
  129. * the new thread.
  130. *
  131. * \return Pointer to the NUTTHREADINFO structure or 0 to indicate an
  132. * error.
  133. */
  134. HANDLE NutThreadCreate(u_char * name, void (*fn) (void *), void *arg, size_t stackSize)
  135. {
  136. u_char *threadMem;
  137. SWITCHFRAME *sf;
  138. ENTERFRAME *ef;
  139. NUTTHREADINFO *td;
  140. size_t alloc_size;
  141. NutEnterCritical();
  142. /* Align to the 4 byte boundary */
  143. alloc_size = stackSize + sizeof(NUTTHREADINFO);
  144. alloc_size += 3;
  145. alloc_size &= ~3;
  146. /*
  147. * Allocate stack and thread info structure in one block.
  148. */
  149. if ((threadMem = NutHeapAlloc(alloc_size)) == 0) {
  150. NutJumpOutCritical();
  151. return 0;
  152. }
  153. /* Align to 4 byte boundary */
  154. td = (NUTTHREADINFO *) ((threadMem + stackSize) & ~0x03);
  155. ef = (ENTERFRAME *) ((uptr_t) td - sizeof(ENTERFRAME));
  156. sf = (SWITCHFRAME *) ((uptr_t) ef - sizeof(SWITCHFRAME));
  157. memcpy(td->td_name, name, sizeof(td->td_name) - 1);
  158. td->td_name[sizeof(td->td_name) - 1] = 0;
  159. td->td_sp = (uptr_t) sf;
  160. td->td_memory = threadMem;
  161. *((u_long *) threadMem) = DEADBEEF;
  162. *((u_long *) (threadMem + 4)) = DEADBEEF;
  163. *((u_long *) (threadMem + 8)) = DEADBEEF;
  164. *((u_long *) (threadMem + 12)) = DEADBEEF;
  165. td->td_priority = 64;
  166. /*
  167. * Setup entry frame to simulate C function entry.
  168. */
  169. ef->cef_er0 = (uptr_t) arg;
  170. ef->cef_ccr_pc = (uptr_t) fn;
  171. sf->csf_pc = (uptr_t) NutThreadEntry;
  172. /*
  173. * Insert into the thread list and the run queue.
  174. */
  175. // NutEnterCritical();
  176. td->td_next = nutThreadList;
  177. nutThreadList = td;
  178. td->td_state = TDS_READY;
  179. td->td_timer = 0;
  180. td->td_queue = 0;
  181. #ifdef NUTDEBUG
  182. if (__os_trf)
  183. fprintf(__os_trs, "Cre<%08lx>", (uptr_t) td);
  184. #endif
  185. NutThreadAddPriQueue(td, (NUTTHREADINFO **) & runQueue);
  186. #ifdef NUTDEBUG
  187. if (__os_trf)
  188. NutDumpThreadList(__os_trs);
  189. //NutDumpThreadQueue(__os_trs, runQueue);
  190. #endif
  191. /*
  192. * If no thread is active, switch to new thread.
  193. */
  194. if (runningThread == 0)
  195. asm volatile ("jmp thread_start");
  196. /*
  197. * If current context is not in front of
  198. * the run queue (highest priority), then
  199. * switch to the thread in front.
  200. */
  201. if (runningThread != runQueue) {
  202. runningThread->td_state = TDS_READY;
  203. #ifdef NUTDEBUG
  204. if (__os_trf)
  205. fprintf(__os_trs, "New<%08lx %08lx>", (uptr_t) runningThread, (uptr_t) runQueue);
  206. #endif
  207. NutThreadSwitch();
  208. }
  209. NutExitCritical();
  210. return td;
  211. }