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

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