context_gcc.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2001-2005 by egnite Software GmbH. All rights reserved.
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions
00006  * are met:
00007  *
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the copyright holders nor the names of
00014  *    contributors may be used to endorse or promote products derived
00015  *    from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY EGNITE SOFTWARE GMBH AND CONTRIBUTORS
00018  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00019  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00020  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL EGNITE
00021  * SOFTWARE GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00022  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00023  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00024  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00025  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00026  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00027  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00028  * SUCH DAMAGE.
00029  *
00030  * For additional information see http://www.ethernut.de/
00031  *
00032  */
00033 
00034 /*
00035  * $Log: context_gcc.c,v $
00036  * Revision 1.11  2008/08/11 06:59:39  haraldkipp
00037  * BSD types replaced by stdint types (feature request #1282721).
00038  *
00039  * Revision 1.10  2008/06/15 16:58:39  haraldkipp
00040  * Rolled back to version 1.8.
00041  *
00042  * Revision 1.8  2007/05/02 11:25:08  haraldkipp
00043  * Minor typo with big impact. Extended PC never set in context switch
00044  * frame.
00045  * Replaced __AVR_ATmega2561__ by __AVR_3_BYTE_PC__.
00046  * Removed unused local variable paddr.
00047  *
00048  * Revision 1.7  2007/04/12 09:19:00  haraldkipp
00049  * Added extended program counter byte for the ATmega2561. Code above 128k not
00050  * working, though.
00051  *
00052  * Revision 1.6  2006/09/29 12:27:31  haraldkipp
00053  * All code should use dedicated stack allocation routines. For targets
00054  * allocating stack from the normal heap the API calls are remapped by
00055  * preprocessor macros.
00056  *
00057  * Revision 1.5  2006/03/16 15:25:09  haraldkipp
00058  * Changed human readable strings from u_char to char to stop GCC 4 from
00059  * nagging about signedness.
00060  *
00061  * Revision 1.4  2005/10/04 05:17:15  hwmaier
00062  * Added support for separating stack and conventional heap as required by AT09CAN128 MCUs
00063  *
00064  * Revision 1.3  2005/08/18 15:36:36  christianwelzel
00065  * Fixed bug in handling of NUTDEBUG.
00066  *
00067  * Revision 1.2  2005/08/02 17:46:46  haraldkipp
00068  * Major API documentation update.
00069  *
00070  * Revision 1.1  2005/07/26 18:10:48  haraldkipp
00071  * Moved from os/thread.c
00072  *
00073  * Revision 1.2  2005/07/14 08:55:57  freckle
00074  * Rewrote CS in NutThreadCreate
00075  *
00076  * Revision 1.1  2005/05/27 17:17:31  drsung
00077  * Moved the file
00078  *
00079  * Revision 1.6  2005/04/30 16:42:42  chaac
00080  * Fixed bug in handling of NUTDEBUG. Added include for cfg/os.h. If NUTDEBUG
00081  * is defined in NutConf, it will make effect where it is used.
00082  *
00083  * Revision 1.5  2005/02/16 19:55:18  haraldkipp
00084  * Ready-to-run queue handling removed from interrupt context.
00085  * Avoid AVRGCC prologue and epilogue code. Thanks to Pete Allinson.
00086  *
00087  * Revision 1.4  2005/02/10 07:06:48  hwmaier
00088  * Changes to incorporate support for AT90CAN128 CPU
00089  *
00090  * Revision 1.3  2004/09/22 08:15:56  haraldkipp
00091  * Speparate IRQ stack configurable
00092  *
00093  * Revision 1.2  2004/04/25 17:06:17  drsung
00094  * Separate IRQ stack now compatible with nested interrupts.
00095  *
00096  * Revision 1.1  2004/03/16 16:48:46  haraldkipp
00097  * Added Jan Dubiec's H8/300 port.
00098  *
00099  * Revision 1.2  2004/02/18 16:32:48  drsung
00100  * Bugfix in NutThreadCreate. Thanks to Mike Cornelius.
00101  *
00102  * Revision 1.1  2004/02/01 18:49:48  haraldkipp
00103  * Added CPU family support
00104  *
00105  */
00106 
00107 #include <cfg/os.h>
00108 #include <cfg/memory.h>
00109 
00110 #include <string.h>
00111 
00112 #include <sys/atom.h>
00113 #include <sys/heap.h>
00114 #include <sys/thread.h>
00115 
00116 /* Support for separate irq stack only for avr-gcc */
00117 #include <dev/irqstack.h>
00118 
00119 #ifdef NUTDEBUG
00120 #include <sys/osdebug.h>
00121 #endif
00122 
00127 
00128 #ifdef IRQSTACK_SIZE
00129 
00136 uint16_t _irqstackdec = 128;
00137 #endif /* #ifdef IRQSTACK_SIZE */
00138 
00145 typedef struct {
00146     uint8_t csf_r29;
00147     uint8_t csf_r28;
00148     uint8_t csf_r17;
00149     uint8_t csf_r16;
00150     uint8_t csf_r15;
00151     uint8_t csf_r14;
00152     uint8_t csf_r13;
00153     uint8_t csf_r12;
00154     uint8_t csf_r11;
00155     uint8_t csf_r10;
00156     uint8_t csf_r9;
00157     uint8_t csf_r8;
00158     uint8_t csf_r7;
00159     uint8_t csf_r6;
00160     uint8_t csf_r5;
00161     uint8_t csf_r4;
00162     uint8_t csf_r3;
00163     uint8_t csf_r2;
00164 #ifdef __AVR_3_BYTE_PC__
00165     uint8_t csf_pcex;
00166 #endif
00167     uint8_t csf_pchi;
00168     uint8_t csf_pclo;
00169 } SWITCHFRAME;
00170 
00176 typedef struct {
00177     uint8_t cef_arghi;
00178     uint8_t cef_arglo;
00179     uint8_t cef_rampz;
00180     uint8_t cef_sreg;
00181     uint8_t cef_r1;
00182 #ifdef __AVR_3_BYTE_PC__
00183     uint8_t cef_pcex;
00184 #endif
00185     uint8_t cef_pchi;
00186     uint8_t cef_pclo;
00187 } ENTERFRAME;
00188 
00189 #define LONG_PTR_P(lp, mem_p) \
00190     __asm__ __volatile__("ldi %A0, lo8("#mem_p ")" "\n\t" \
00191                          "ldi %B0, hi8("#mem_p ")" "\n\t" \
00192                          "ldi %C0, hh8("#mem_p ")" "\n\t" \
00193                          "clr %D0" \
00194                          :"=d" (lp))
00195 
00196 /*
00197  * This code is executed when entering a thread.
00198  */
00199 static void NutThreadEntry(void) __attribute__ ((naked));
00200 static void NutThreadEntry(void)
00201 {
00202     __asm__ __volatile__("pop r25" "\n\t"       /* first parameter hi-byte */
00203                          "pop r24" "\n\t"       /* first parameter lo-byte */
00204                          "pop __tmp_reg__" "\n\t"       /* Get RAMPZ */
00205                          "out %0, __tmp_reg__" "\n\t"   /* Restore RAMPZ */
00206                          "pop __tmp_reg__" "\n\t"       /* Get SREG */
00207                          "out %1, __tmp_reg__" "\n\t"   /* Restore SREG */
00208                          "pop __zero_reg__" "\n\t"      /* Zero register */
00209                          "reti" "\n\t"  /* enables interrupts */
00210                          ::"I" _SFR_IO_ADDR(RAMPZ), "I" _SFR_IO_ADDR(SREG)
00211         );
00212 }
00213 
00214 
00226 void NutThreadSwitch(void) __attribute__ ((noinline)) __attribute__ ((naked));
00227 void NutThreadSwitch(void)
00228 {
00229     /*
00230      * Save all CPU registers.
00231      */
00232     asm volatile ("push r2" "\n\t"              /* */
00233               "push r3" "\n\t"              /* */
00234               "push r4" "\n\t"              /* */
00235               "push r5" "\n\t"              /* */
00236               "push r6" "\n\t"              /* */
00237               "push r7" "\n\t"              /* */
00238               "push r8" "\n\t"              /* */
00239               "push r9" "\n\t"              /* */
00240               "push r10" "\n\t"             /* */
00241               "push r11" "\n\t"             /* */
00242               "push r12" "\n\t"             /* */
00243               "push r13" "\n\t"             /* */
00244               "push r14" "\n\t"             /* */
00245               "push r15" "\n\t"             /* */
00246               "push r16" "\n\t"             /* */
00247               "push r17" "\n\t"             /* */
00248               "push r28" "\n\t"             /* */
00249               "push r29" "\n\t"             /* */
00250               "in %A0, %1" "\n\t"           /* */
00251               "in %B0, %2" "\n\t"           /* */
00252               :"=r" (runningThread->td_sp)  /* */
00253               :"I" _SFR_IO_ADDR(SPL),       /* */
00254                "I" _SFR_IO_ADDR(SPH)        /* */
00255     );
00256 
00257     /*
00258      * This defines a global label, which may be called
00259      * as an entry point into this function.
00260      */
00261     asm volatile (".global thread_start\n"  /* */
00262                   "thread_start:\n\t"::);
00263 
00264     /*
00265      * Reload CPU registers from the thread on top of the run queue.
00266      */
00267     runningThread = runQueue;
00268     runningThread->td_state = TDS_RUNNING;
00269     asm volatile ("out %1, %A0" "\n\t"          /* */
00270               "out %2, %B0" "\n\t"          /* */
00271               "pop r29" "\n\t"              /* */
00272               "pop r28" "\n\t"              /* */
00273               "pop r17" "\n\t"              /* */
00274               "pop r16" "\n\t"              /* */
00275               "pop r15" "\n\t"              /* */
00276               "pop r14" "\n\t"              /* */
00277               "pop r13" "\n\t"              /* */
00278               "pop r12" "\n\t"              /* */
00279               "pop r11" "\n\t"              /* */
00280               "pop r10" "\n\t"              /* */
00281               "pop r9" "\n\t"               /* */
00282               "pop r8" "\n\t"               /* */
00283               "pop r7" "\n\t"               /* */
00284               "pop r6" "\n\t"               /* */
00285               "pop r5" "\n\t"               /* */
00286               "pop r4" "\n\t"               /* */
00287               "pop r3" "\n\t"               /* */
00288               "pop r2" "\n\t"               /* */
00289               "ret" "\n\t"                  /* */
00290               ::"r" (runningThread->td_sp), /* */
00291                 "I" _SFR_IO_ADDR(SPL),      /* */
00292                 "I" _SFR_IO_ADDR(SPH)       /* */
00293     );
00294 }
00295 
00314 HANDLE NutThreadCreate(char * name, void (*fn) (void *), void *arg, size_t stackSize)
00315 {
00316     uint8_t *threadMem;
00317     SWITCHFRAME *sf;
00318     ENTERFRAME *ef;
00319     NUTTHREADINFO *td;
00320 #ifdef IRQSTACK_SIZE
00321     if (stackSize > _irqstackdec + 128) stackSize -= _irqstackdec;
00322 #endif
00323 
00324     /*
00325      * Allocate stack and thread info structure in one block.
00326      */
00327     if ((threadMem = NutStackAlloc(stackSize + sizeof(NUTTHREADINFO))) == 0) {
00328         return 0;
00329     }
00330 
00331     td = (NUTTHREADINFO *) (threadMem + stackSize);
00332     ef = (ENTERFRAME *) ((uint16_t) td - sizeof(ENTERFRAME));
00333     sf = (SWITCHFRAME *) ((uint16_t) ef - sizeof(SWITCHFRAME));
00334 
00335 
00336     memcpy(td->td_name, name, sizeof(td->td_name) - 1);
00337     td->td_name[sizeof(td->td_name) - 1] = 0;
00338     td->td_sp = (uint16_t) sf - 1;
00339     td->td_memory = threadMem;
00340     *((uint32_t *) threadMem) = DEADBEEF;
00341     *((uint32_t *) (threadMem + 4)) = DEADBEEF;
00342     *((uint32_t *) (threadMem + 8)) = DEADBEEF;
00343     *((uint32_t *) (threadMem + 12)) = DEADBEEF;
00344     td->td_priority = 64;
00345 
00346     /*
00347      * Setup entry frame to simulate C function entry.
00348      */
00349 #ifdef __AVR_3_BYTE_PC__
00350     ef->cef_pcex = 0;
00351 #endif
00352     ef->cef_pchi = (uint8_t) (((uint16_t) fn) >> 8);
00353     ef->cef_pclo = (uint8_t) (((uint16_t) fn) & 0xff);
00354     ef->cef_sreg = 0x80;
00355     ef->cef_rampz = 0;
00356     ef->cef_r1 = 0;
00357 
00358     ef->cef_arglo = (uint8_t) (((uint16_t) arg) & 0xff);
00359     ef->cef_arghi = (uint8_t) (((uint16_t) arg) >> 8);
00360 
00361 #ifdef __AVR_3_BYTE_PC__
00362     sf->csf_pcex = 0;
00363 #endif
00364     sf->csf_pchi = (uint8_t) (((uint16_t) NutThreadEntry) >> 8);
00365     sf->csf_pclo = (uint8_t) (((uint16_t) NutThreadEntry) & 0xff);
00366 
00367     /*
00368      * Insert into the thread list and the run queue.
00369      */
00370 
00371     td->td_next = nutThreadList;
00372     nutThreadList = td;
00373     td->td_state = TDS_READY;
00374     td->td_timer = 0;
00375     td->td_queue = 0;
00376 #ifdef NUTDEBUG
00377     if (__os_trf)
00378         fprintf(__os_trs, "Cre<%04x>", (uptr_t) td);
00379 #endif
00380 
00381     NutThreadAddPriQueue(td, (NUTTHREADINFO **) & runQueue);
00382 
00383 #ifdef NUTDEBUG
00384     if (__os_trf) {
00385         NutDumpThreadList(__os_trs);
00386         //NutDumpThreadQueue(__os_trs, runQueue);
00387     }
00388 #endif
00389 
00390     /*
00391      * If no thread is active, switch to new thread.
00392      */
00393     if (runningThread == 0) {
00394         NutEnterCritical();
00395         asm volatile ("rjmp thread_start\n\t"::);
00396         /* we will never come back here .. */
00397     }
00398 
00399     /*
00400      * If current context is not in front of
00401      * the run queue (highest priority), then
00402      * switch to the thread in front.
00403      */
00404     if (runningThread != runQueue) {
00405         runningThread->td_state = TDS_READY;
00406 #ifdef NUTDEBUG
00407         if (__os_trf)
00408             fprintf(__os_trs, "New<%04x %04x>", (uptr_t) runningThread, (uptr_t) runQueue);
00409 #endif
00410         NutEnterCritical();
00411         NutThreadSwitch();
00412         NutExitCritical();
00413     }
00414 
00415     return td;
00416 }
00417 

© 2000-2007 by egnite Software GmbH - visit http://www.ethernut.de/