| 1 | /* Adapteva epiphany-core implementation of stdio support functions () | 
|---|
| 2 |  | 
|---|
| 3 |    Copyright (c) 2011, 2012 Adapteva, Inc. | 
|---|
| 4 |    All rights reserved. | 
|---|
| 5 |  | 
|---|
| 6 |    Redistribution and use in source and binary forms, with or without | 
|---|
| 7 |    modification, are permitted provided that the following conditions are met: | 
|---|
| 8 |     * Redistributions of source code must retain the above copyright notice, | 
|---|
| 9 |       this list of conditions and the following disclaimer. | 
|---|
| 10 |     * 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 |     * Neither the name of Adapteva nor the names of its contributors may be | 
|---|
| 14 |       used to endorse or promote products derived from this software without | 
|---|
| 15 |       specific prior written permission. | 
|---|
| 16 |  | 
|---|
| 17 |    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | 
|---|
| 18 |    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 
|---|
| 19 |    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 
|---|
| 20 |    ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | 
|---|
| 21 |    LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | 
|---|
| 22 |    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | 
|---|
| 23 |    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | 
|---|
| 24 |    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | 
|---|
| 25 |    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | 
|---|
| 26 |    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | 
|---|
| 27 |    POSSIBILITY OF SUCH DAMAGE.                                               */ | 
|---|
| 28 |  | 
|---|
| 29 | #include <errno.h> | 
|---|
| 30 |  | 
|---|
| 31 | /* simple include interface to EPIPHANY trap instruction. */ | 
|---|
| 32 |  | 
|---|
| 33 | /* | 
|---|
| 34 |  * The simulator uses a trap instruction to escape to the simulator to do file i/o | 
|---|
| 35 |  */ | 
|---|
| 36 |  | 
|---|
| 37 | /*      trapcode             r0            r1             r2  */ | 
|---|
| 38 | #define TRAP_write 0  /*    channel        addr           len */ | 
|---|
| 39 | #define TRAP_read 1   /*    channel        addr           len */ | 
|---|
| 40 | #define TRAP_open 2   /*    filepath       mode               */ | 
|---|
| 41 | #define TRAP_exit 3   /*    status                            */ | 
|---|
| 42 | #define TRAP_pass 4   /*     -                                */ | 
|---|
| 43 | #define TRAP_fail 5   /*     -                                */ | 
|---|
| 44 | #define TRAP_close 6  /*    channel                           */ | 
|---|
| 45 | #define TRAP_other 7  /*    subcode        r1              r2 */ | 
|---|
| 46 |  | 
|---|
| 47 |  | 
|---|
| 48 | #include "epiphany-syscalls.h" | 
|---|
| 49 |  | 
|---|
| 50 |  | 
|---|
| 51 | /* prototypical inline asm */ | 
|---|
| 52 | int __attribute__ ((section ("libgloss_epiphany")))  asm_write (int CHAN, void* ADDR, int LEN) | 
|---|
| 53 | { | 
|---|
| 54 |         register int chan asm("r0") = CHAN; | 
|---|
| 55 |         register void* addr asm("r1") = ADDR; | 
|---|
| 56 |         register int len asm("r2") = LEN; | 
|---|
| 57 |         register int result asm("r0"); | 
|---|
| 58 |         register int error asm("r3"); | 
|---|
| 59 |         asm ("trap 0" : "=r" (result), "=r" (error) : | 
|---|
| 60 |              "r" (chan), "r" (addr), "r" (len)); | 
|---|
| 61 |         if (error) | 
|---|
| 62 |           errno = error; | 
|---|
| 63 |         return result; | 
|---|
| 64 | } | 
|---|
| 65 |  | 
|---|
| 66 | int __attribute__ ((section ("libgloss_epiphany")))  asm_read(int CHAN, void *ADDR, int LEN) | 
|---|
| 67 | { | 
|---|
| 68 |         register int chan asm("r0") = CHAN; | 
|---|
| 69 |         register void* addr asm("r1") = ADDR; | 
|---|
| 70 |         register int len asm("r2") = LEN; | 
|---|
| 71 |         register int result asm("r0"); | 
|---|
| 72 |         register int error asm("r3"); | 
|---|
| 73 |         asm ("trap 1" : "=r" (result), "=r" (error) : | 
|---|
| 74 |              "r" (chan), "r" (addr), "r" (len)); | 
|---|
| 75 |         if (error) | 
|---|
| 76 |           errno = error; | 
|---|
| 77 |         return result; | 
|---|
| 78 | } | 
|---|
| 79 |  | 
|---|
| 80 |  | 
|---|
| 81 | int __attribute__ ((section ("libgloss_epiphany"))) | 
|---|
| 82 | asm_open(const char* FILE, int FLAGS, int MODE) | 
|---|
| 83 | { | 
|---|
| 84 |         register const char* file asm("r0") = FILE; | 
|---|
| 85 |         register int flags asm("r1") = FLAGS; | 
|---|
| 86 |         register int result asm("r0"); | 
|---|
| 87 |         register int error asm("r3"); | 
|---|
| 88 |         asm ("trap 2" : "=r" (result), "=r" (error) : "r" (file), "r" (flags)); | 
|---|
| 89 |         if (error) | 
|---|
| 90 |           errno = error; | 
|---|
| 91 |         return result; | 
|---|
| 92 | } | 
|---|
| 93 |  | 
|---|
| 94 | void __attribute__ ((section ("libgloss_epiphany")))  asm_exit(int STATUS) | 
|---|
| 95 | { | 
|---|
| 96 |         register int status asm("r0") = STATUS; | 
|---|
| 97 |         asm("trap 3" :: "r" (status)); | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 | int __attribute__ ((section ("libgloss_epiphany")))  asm_close(int CHAN) | 
|---|
| 101 | { | 
|---|
| 102 |         register int chan asm("r0") = CHAN; | 
|---|
| 103 |         register int result asm("r0"); | 
|---|
| 104 |         register int error asm("r3"); | 
|---|
| 105 |         asm ("trap 6" : "=r" (result), "=r" (error) : "r" (chan)); | 
|---|
| 106 |         if (error) | 
|---|
| 107 |           errno = error; | 
|---|
| 108 |         return result; | 
|---|
| 109 | } | 
|---|
| 110 |  | 
|---|
| 111 | int __attribute__ ((section ("libgloss_epiphany")))  asm_syscall(void *P1,void *P2, void *P3, int SUBFUN) | 
|---|
| 112 | { | 
|---|
| 113 |         register void* p1 asm("r0") = (void*)P1; | 
|---|
| 114 |         register void* p2 asm("r1") = (void*)P2; | 
|---|
| 115 |         register void* p3 asm("r2") = (void*)P3; | 
|---|
| 116 |         register int result asm("r0"); | 
|---|
| 117 |         register int subfun asm("r3") = SUBFUN; | 
|---|
| 118 |         register int error asm("r3"); | 
|---|
| 119 |         asm ("trap 7" : "=r" (result), "=r" (error) : | 
|---|
| 120 |              "r" (p1), "r" (p2), "r" (p3), "r" (subfun)); | 
|---|
| 121 |         if (error) | 
|---|
| 122 |           errno = error; | 
|---|
| 123 |         return result; | 
|---|
| 124 | } | 
|---|
| 125 |  | 
|---|
| 126 | /* | 
|---|
| 127 |  * Signal functions implementation | 
|---|
| 128 |  * | 
|---|
| 129 |  */ | 
|---|
| 130 |  | 
|---|
| 131 | #include "epiphany-config.h" | 
|---|
| 132 |  | 
|---|
| 133 |  | 
|---|
| 134 | #define HW_RESET 0 | 
|---|
| 135 | #define SW_EXCEPTION_IVT_N 1 | 
|---|
| 136 | #define PAGE_MISS_IVT_N 2 | 
|---|
| 137 | #define TIMER0_IVT_N 3 | 
|---|
| 138 | #define TIMER1_IVT_N 4 | 
|---|
| 139 | #define MESSAGE_IVT_N 5 | 
|---|
| 140 | #define DMA0_IVT_N 6 | 
|---|
| 141 | #define DMA1_IVT_N 7 | 
|---|
| 142 | #define WAND_IVT_N 8 | 
|---|
| 143 | #define USR_SOFT_IVT_N 9 | 
|---|
| 144 |  | 
|---|
| 145 |  | 
|---|
| 146 | typedef void (*sighandler_t)(int); | 
|---|
| 147 | extern sighandler_t ISR_VECTOR[]; | 
|---|
| 148 | extern void DEFAULT_ISR_CALLBACK(); | 
|---|
| 149 |  | 
|---|
| 150 | sighandler_t __attribute__ ((section ("libgloss_epiphany")))  signal(int signum, sighandler_t handler) { | 
|---|
| 151 |         switch( signum ) | 
|---|
| 152 |         { | 
|---|
| 153 |         case SIG_DFL /* see signal.h */: | 
|---|
| 154 |                 //the default is ignore | 
|---|
| 155 |                 break; | 
|---|
| 156 |         case SIG_IGN /* see signal.h */ : | 
|---|
| 157 |                 DEFAULT_ISR_CALLBACK(); | 
|---|
| 158 |                 break; | 
|---|
| 159 |         case SIG_ERR : | 
|---|
| 160 |                 asm("trap 5"); | 
|---|
| 161 |                 break; | 
|---|
| 162 |         case SIG_RESET: | 
|---|
| 163 |                 ISR_VECTOR[HW_RESET] = handler; | 
|---|
| 164 |                 break; | 
|---|
| 165 |         case SIG_SW_EXCEPTION: | 
|---|
| 166 |                 ISR_VECTOR[SW_EXCEPTION_IVT_N] = handler; | 
|---|
| 167 |                 break; | 
|---|
| 168 |         case SIG_PAGE_MISS: | 
|---|
| 169 |                 ISR_VECTOR[PAGE_MISS_IVT_N] = handler; | 
|---|
| 170 |                 break; | 
|---|
| 171 |         case SIG_TIMER0: | 
|---|
| 172 |                 ISR_VECTOR[TIMER0_IVT_N] = handler; | 
|---|
| 173 |                 break; | 
|---|
| 174 |         case SIG_TIMER1: | 
|---|
| 175 |                 ISR_VECTOR[TIMER1_IVT_N] = handler; | 
|---|
| 176 |                 break; | 
|---|
| 177 |         case SIG_MESSAGE: | 
|---|
| 178 |                 ISR_VECTOR[MESSAGE_IVT_N] = handler; | 
|---|
| 179 |                 break; | 
|---|
| 180 |         case SIG_DMA0: | 
|---|
| 181 |                 ISR_VECTOR[DMA0_IVT_N] = handler; | 
|---|
| 182 |                 break; | 
|---|
| 183 |         case SIG_DMA1: | 
|---|
| 184 |                 ISR_VECTOR[DMA1_IVT_N] = handler; | 
|---|
| 185 |                 break; | 
|---|
| 186 |         case SIG_WAND: | 
|---|
| 187 |                 ISR_VECTOR[WAND_IVT_N] = handler; | 
|---|
| 188 |                 break; | 
|---|
| 189 |  | 
|---|
| 190 |         case SIG_USR1: | 
|---|
| 191 |                 ISR_VECTOR[USR_SOFT_IVT_N] = handler; | 
|---|
| 192 |                 break; | 
|---|
| 193 |         default: | 
|---|
| 194 |                 //do nothing | 
|---|
| 195 |                 return 0; | 
|---|
| 196 |         } | 
|---|
| 197 |  | 
|---|
| 198 |         return 0; | 
|---|
| 199 | } | 
|---|
| 200 |  | 
|---|
| 201 | //int e_raise(int signum) __attribute__ ((optimize("O0"))); | 
|---|
| 202 |  | 
|---|
| 203 | int __attribute__ ((section ("libgloss_epiphany")))  e_raise(int signum) { | 
|---|
| 204 |  | 
|---|
| 205 |         //register int imask asm("r4") = 0 ; | 
|---|
| 206 |         volatile register int ilatst /*asm("r5") */= signum; | 
|---|
| 207 |  | 
|---|
| 208 |         switch( signum ) | 
|---|
| 209 |         { | 
|---|
| 210 |         case SIG_DFL /* see signal.h */: | 
|---|
| 211 |                 //the default is ignore | 
|---|
| 212 |                 return 0; | 
|---|
| 213 |         case SIG_IGN /* see signal.h */ : | 
|---|
| 214 |                 //do nothing | 
|---|
| 215 |                 return 0; | 
|---|
| 216 |         case SIG_ERR : | 
|---|
| 217 |  | 
|---|
| 218 |                 return 0; | 
|---|
| 219 |         case SIG_RESET: | 
|---|
| 220 |                 //imask = 1 << HW_RESET; | 
|---|
| 221 |                 ilatst = 1 << HW_RESET; | 
|---|
| 222 |                 break; | 
|---|
| 223 |  | 
|---|
| 224 |         case SIG_SW_EXCEPTION: | 
|---|
| 225 |                 //imask = 1 << SW_EXCEPTION_IVT_N; | 
|---|
| 226 |                 ilatst = 1 << SW_EXCEPTION_IVT_N; | 
|---|
| 227 |                 break; | 
|---|
| 228 |         case SIG_PAGE_MISS: | 
|---|
| 229 |                 //imask = 1 << PAGE_MISS_IVT_N; | 
|---|
| 230 |                 ilatst = 1 << PAGE_MISS_IVT_N; | 
|---|
| 231 |                 break; | 
|---|
| 232 |         case SIG_TIMER0: | 
|---|
| 233 |                 //imask = 1 << TIMER0_IVT_N; | 
|---|
| 234 |                 ilatst = 1 << TIMER0_IVT_N; | 
|---|
| 235 |                 break; | 
|---|
| 236 |         case SIG_TIMER1: | 
|---|
| 237 |                 //imask = 1 << TIMER1_IVT_N; | 
|---|
| 238 |                 ilatst = 1 << TIMER1_IVT_N; | 
|---|
| 239 |                 break; | 
|---|
| 240 |         case SIG_MESSAGE: | 
|---|
| 241 |                 //imask = 1 << MESSAGE_IVT_N; | 
|---|
| 242 |                 ilatst = 1 << MESSAGE_IVT_N; | 
|---|
| 243 |                 break; | 
|---|
| 244 |         case SIG_DMA0: | 
|---|
| 245 |                 //imask = 1 << DMA0_IVT_N; | 
|---|
| 246 |                 ilatst = 1 << DMA0_IVT_N; | 
|---|
| 247 |                 break; | 
|---|
| 248 |         case SIG_DMA1: | 
|---|
| 249 |                 //imask = 1 << DMA1_IVT_N; | 
|---|
| 250 |                 ilatst = 1 << DMA1_IVT_N; | 
|---|
| 251 |                 break; | 
|---|
| 252 |         case SIG_WAND: | 
|---|
| 253 |                 __asm__ __volatile__ ("wand"); | 
|---|
| 254 |                 //ilatst = 1 << WAND_IVT_N; | 
|---|
| 255 |                 //break; | 
|---|
| 256 |                 return; | 
|---|
| 257 |  | 
|---|
| 258 |         case SIG_USR1: | 
|---|
| 259 |                 ilatst = 1 << USR_SOFT_IVT_N; | 
|---|
| 260 |                 break; | 
|---|
| 261 |  | 
|---|
| 262 |         default: | 
|---|
| 263 |                 //do nothing | 
|---|
| 264 |                 return 0; | 
|---|
| 265 |         } | 
|---|
| 266 |         //asm("movts imask, r4;"); | 
|---|
| 267 |         //asm("movts ilatst, r5;"); | 
|---|
| 268 |         __asm__ __volatile__ ("movts ilatst, %0"  : "=r" (ilatst) : "r" (ilatst)); | 
|---|
| 269 |         return 0; | 
|---|
| 270 |  | 
|---|
| 271 | } | 
|---|