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.10  2008/06/15 16:58:39  haraldkipp
00037  * Rolled back to version 1.8.
00038  *
00039  * Revision 1.8  2007/05/02 11:25:08  haraldkipp
00040  * Minor typo with big impact. Extended PC never set in context switch
00041  * frame.
00042  * Replaced __AVR_ATmega2561__ by __AVR_3_BYTE_PC__.
00043  * Removed unused local variable paddr.
00044  *
00045  * Revision 1.7  2007/04/12 09:19:00  haraldkipp
00046  * Added extended program counter byte for the ATmega2561. Code above 128k not
00047  * working, though.
00048  *
00049  * Revision 1.6  2006/09/29 12:27:31  haraldkipp
00050  * All code should use dedicated stack allocation routines. For targets
00051  * allocating stack from the normal heap the API calls are remapped by
00052  * preprocessor macros.
00053  *
00054  * Revision 1.5  2006/03/16 15:25:09  haraldkipp
00055  * Changed human readable strings from u_char to char to stop GCC 4 from
00056  * nagging about signedness.
00057  *
00058  * Revision 1.4  2005/10/04 05:17:15  hwmaier
00059  * Added support for separating stack and conventional heap as required by AT09CAN128 MCUs
00060  *
00061  * Revision 1.3  2005/08/18 15:36:36  christianwelzel
00062  * Fixed bug in handling of NUTDEBUG.
00063  *
00064  * Revision 1.2  2005/08/02 17:46:46  haraldkipp
00065  * Major API documentation update.
00066  *
00067  * Revision 1.1  2005/07/26 18:10:48  haraldkipp
00068  * Moved from os/thread.c
00069  *
00070  * Revision 1.2  2005/07/14 08:55:57  freckle
00071  * Rewrote CS in NutThreadCreate
00072  *
00073  * Revision 1.1  2005/05/27 17:17:31  drsung
00074  * Moved the file
00075  *
00076  * Revision 1.6  2005/04/30 16:42:42  chaac
00077  * Fixed bug in handling of NUTDEBUG. Added include for cfg/os.h. If NUTDEBUG
00078  * is defined in NutConf, it will make effect where it is used.
00079  *
00080  * Revision 1.5  2005/02/16 19:55:18  haraldkipp
00081  * Ready-to-run queue handling removed from interrupt context.
00082  * Avoid AVRGCC prologue and epilogue code. Thanks to Pete Allinson.
00083  *
00084  * Revision 1.4  2005/02/10 07:06:48  hwmaier
00085  * Changes to incorporate support for AT90CAN128 CPU
00086  *
00087  * Revision 1.3  2004/09/22 08:15:56  haraldkipp
00088  * Speparate IRQ stack configurable
00089  *
00090  * Revision 1.2  2004/04/25 17:06:17  drsung
00091  * Separate IRQ stack now compatible with nested interrupts.
00092  *
00093  * Revision 1.1  2004/03/16 16:48:46  haraldkipp
00094  * Added Jan Dubiec's H8/300 port.
00095  *
00096  * Revision 1.2  2004/02/18 16:32:48  drsung
00097  * Bugfix in NutThreadCreate. Thanks to Mike Cornelius.
00098  *
00099  * Revision 1.1  2004/02/01 18:49:48  haraldkipp
00100  * Added CPU family support
00101  *
00102  */
00103 
00104 #include <cfg/os.h>
00105 #include <cfg/memory.h>
00106 
00107 #include <string.h>
00108 
00109 #include <sys/atom.h>
00110 #include <sys/heap.h>
00111 #include <sys/thread.h>
00112 
00113 /* Support for separate irq stack only for avr-gcc */
00114 #include <dev/irqstack.h>
00115 
00116 #ifdef NUTDEBUG
00117 #include <sys/osdebug.h>
00118 #endif
00119 
00124 
00125 #ifdef IRQSTACK_SIZE
00126 
00133 u_short _irqstackdec = 128;
00134 #endif /* #ifdef IRQSTACK_SIZE */
00135 
00142 typedef struct {
00143     u_char csf_r29;
00144     u_char csf_r28;
00145     u_char csf_r17;
00146     u_char csf_r16;
00147     u_char csf_r15;
00148     u_char csf_r14;
00149     u_char csf_r13;
00150     u_char csf_r12;
00151     u_char csf_r11;
00152     u_char csf_r10;
00153     u_char csf_r9;
00154     u_char csf_r8;
00155     u_char csf_r7;
00156     u_char csf_r6;
00157     u_char csf_r5;
00158     u_char csf_r4;
00159     u_char csf_r3;
00160     u_char csf_r2;
00161 #ifdef __AVR_3_BYTE_PC__
00162     u_char csf_pcex;
00163 #endif
00164     u_char csf_pchi;
00165     u_char csf_pclo;
00166 } SWITCHFRAME;
00167 
00173 typedef struct {
00174     u_char cef_arghi;
00175     u_char cef_arglo;
00176     u_char cef_rampz;
00177     u_char cef_sreg;
00178     u_char cef_r1;
00179 #ifdef __AVR_3_BYTE_PC__
00180     u_char cef_pcex;
00181 #endif
00182     u_char cef_pchi;
00183     u_char cef_pclo;
00184 } ENTERFRAME;
00185 
00186 #define LONG_PTR_P(lp, mem_p) \
00187     __asm__ __volatile__("ldi %A0, lo8("#mem_p ")" "\n\t" \
00188                          "ldi %B0, hi8("#mem_p ")" "\n\t" \
00189                          "ldi %C0, hh8("#mem_p ")" "\n\t" \
00190                          "clr %D0" \
00191                          :"=d" (lp))
00192 
00193 /*
00194  * This code is executed when entering a thread.
00195  */
00196 static void NutThreadEntry(void) __attribute__ ((naked));
00197 static void NutThreadEntry(void)
00198 {
00199     __asm__ __volatile__("pop r25" "\n\t"       /* first parameter hi-byte */
00200                          "pop r24" "\n\t"       /* first parameter lo-byte */
00201                          "pop __tmp_reg__" "\n\t"       /* Get RAMPZ */
00202                          "out %0, __tmp_reg__" "\n\t"   /* Restore RAMPZ */
00203                          "pop __tmp_reg__" "\n\t"       /* Get SREG */
00204                          "out %1, __tmp_reg__" "\n\t"   /* Restore SREG */
00205                          "pop __zero_reg__" "\n\t"      /* Zero register */
00206                          "reti" "\n\t"  /* enables interrupts */
00207                          ::"I" _SFR_IO_ADDR(RAMPZ), "I" _SFR_IO_ADDR(SREG)
00208         );
00209 }
00210 
00211 
00223 void NutThreadSwitch(void) __attribute__ ((noinline)) __attribute__ ((naked));
00224 void NutThreadSwitch(void)
00225 {
00226     /*
00227      * Save all CPU registers.
00228      */
00229     asm volatile ("push r2" "\n\t"              /* */
00230               "push r3" "\n\t"              /* */
00231               "push r4" "\n\t"              /* */
00232               "push r5" "\n\t"              /* */
00233               "push r6" "\n\t"              /* */
00234               "push r7" "\n\t"              /* */
00235               "push r8" "\n\t"              /* */
00236               "push r9" "\n\t"              /* */
00237               "push r10" "\n\t"             /* */
00238               "push r11" "\n\t"             /* */
00239               "push r12" "\n\t"             /* */
00240               "push r13" "\n\t"             /* */
00241               "push r14" "\n\t"             /* */
00242               "push r15" "\n\t"             /* */
00243               "push r16" "\n\t"             /* */
00244               "push r17" "\n\t"             /* */
00245               "push r28" "\n\t"             /* */
00246               "push r29" "\n\t"             /* */
00247               "in %A0, %1" "\n\t"           /* */
00248               "in %B0, %2" "\n\t"           /* */
00249               :"=r" (runningThread->td_sp)  /* */
00250               :"I" _SFR_IO_ADDR(SPL),       /* */
00251                "I" _SFR_IO_ADDR(SPH)        /* */
00252     );
00253 
00254     /*
00255      * This defines a global label, which may be called
00256      * as an entry point into this function.
00257      */
00258     asm volatile (".global thread_start\n"  /* */
00259                   "thread_start:\n\t"::);
00260 
00261     /*
00262      * Reload CPU registers from the thread on top of the run queue.
00263      */
00264     runningThread = runQueue;
00265     runningThread->td_state = TDS_RUNNING;
00266     asm volatile ("out %1, %A0" "\n\t"          /* */
00267               "out %2, %B0" "\n\t"          /* */
00268               "pop r29" "\n\t"              /* */
00269               "pop r28" "\n\t"              /* */
00270               "pop r17" "\n\t"              /* */
00271               "pop r16" "\n\t"              /* */
00272               "pop r15" "\n\t"              /* */
00273               "pop r14" "\n\t"              /* */
00274               "pop r13" "\n\t"              /* */
00275               "pop r12" "\n\t"              /* */
00276               "pop r11" "\n\t"              /* */
00277               "pop r10" "\n\t"              /* */
00278               "pop r9" "\n\t"               /* */
00279               "pop r8" "\n\t"               /* */
00280               "pop r7" "\n\t"               /* */
00281               "pop r6" "\n\t"               /* */
00282               "pop r5" "\n\t"               /* */
00283               "pop r4" "\n\t"               /* */
00284               "pop r3" "\n\t"               /* */
00285               "pop r2" "\n\t"               /* */
00286               "ret" "\n\t"                  /* */
00287               ::"r" (runningThread->td_sp), /* */
00288                 "I" _SFR_IO_ADDR(SPL),      /* */
00289                 "I" _SFR_IO_ADDR(SPH)       /* */
00290     );
00291 }
00292 
00311 HANDLE NutThreadCreate(char * name, void (*fn) (void *), void *arg, size_t stackSize)
00312 {
00313     u_char *threadMem;
00314     SWITCHFRAME *sf;
00315     ENTERFRAME *ef;
00316     NUTTHREADINFO *td;
00317 #ifdef IRQSTACK_SIZE
00318     if (stackSize > _irqstackdec + 128) stackSize -= _irqstackdec;
00319 #endif
00320 
00321     /*
00322      * Allocate stack and thread info structure in one block.
00323      */
00324     if ((threadMem = NutStackAlloc(stackSize + sizeof(NUTTHREADINFO))) == 0) {
00325         return 0;
00326     }
00327 
00328     td = (NUTTHREADINFO *) (threadMem + stackSize);
00329     ef = (ENTERFRAME *) ((u_short) td - sizeof(ENTERFRAME));
00330     sf = (SWITCHFRAME *) ((u_short) ef - sizeof(SWITCHFRAME));
00331 
00332 
00333     memcpy(td->td_name, name, sizeof(td->td_name) - 1);
00334     td->td_name[sizeof(td->td_name) - 1] = 0;
00335     td->td_sp = (u_short) sf - 1;
00336     td->td_memory = threadMem;
00337     *((u_long *) threadMem) = DEADBEEF;
00338     *((u_long *) (threadMem + 4)) = DEADBEEF;
00339     *((u_long *) (threadMem + 8)) = DEADBEEF;
00340     *((u_long *) (threadMem + 12)) = DEADBEEF;
00341     td->td_priority = 64;
00342 
00343     /*
00344      * Setup entry frame to simulate C function entry.
00345      */
00346 #ifdef __AVR_3_BYTE_PC__
00347     ef->cef_pcex = 0;
00348 #endif
00349     ef->cef_pchi = (u_char) (((u_short) fn) >> 8);
00350     ef->cef_pclo = (u_char) (((u_short) fn) & 0xff);
00351     ef->cef_sreg = 0x80;
00352     ef->cef_rampz = 0;
00353     ef->cef_r1 = 0;
00354 
00355     ef->cef_arglo = (u_char) (((u_short) arg) & 0xff);
00356     ef->cef_arghi = (u_char) (((u_short) arg) >> 8);
00357 
00358 #ifdef __AVR_3_BYTE_PC__
00359     sf->csf_pcex = 0;
00360 #endif
00361     sf->csf_pchi = (u_char) (((u_short) NutThreadEntry) >> 8);
00362     sf->csf_pclo = (u_char) (((u_short) NutThreadEntry) & 0xff);
00363 
00364     /*
00365      * Insert into the thread list and the run queue.
00366      */
00367 
00368     td->td_next = nutThreadList;
00369     nutThreadList = td;
00370     td->td_state = TDS_READY;
00371     td->td_timer = 0;
00372     td->td_queue = 0;
00373 #ifdef NUTDEBUG
00374     if (__os_trf)
00375         fprintf(__os_trs, "Cre<%04x>", (uptr_t) td);
00376 #endif
00377 
00378     NutThreadAddPriQueue(td, (NUTTHREADINFO **) & runQueue);
00379 
00380 #ifdef NUTDEBUG
00381     if (__os_trf) {
00382         NutDumpThreadList(__os_trs);
00383         //NutDumpThreadQueue(__os_trs, runQueue);
00384     }
00385 #endif
00386 
00387     /*
00388      * If no thread is active, switch to new thread.
00389      */
00390     if (runningThread == 0) {
00391         NutEnterCritical();
00392         asm volatile ("rjmp thread_start\n\t"::);
00393         /* we will never come back here .. */
00394     }
00395 
00396     /*
00397      * If current context is not in front of
00398      * the run queue (highest priority), then
00399      * switch to the thread in front.
00400      */
00401     if (runningThread != runQueue) {
00402         runningThread->td_state = TDS_READY;
00403 #ifdef NUTDEBUG
00404         if (__os_trf)
00405             fprintf(__os_trs, "New<%04x %04x>", (uptr_t) runningThread, (uptr_t) runQueue);
00406 #endif
00407         NutEnterCritical();
00408         NutThreadSwitch();
00409         NutExitCritical();
00410     }
00411 
00412     return td;
00413 }
00414 

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