1 | /* |
---|
2 | * C library support files for the Blackfin processor |
---|
3 | * |
---|
4 | * Copyright (C) 2006 Analog Devices, Inc. |
---|
5 | * |
---|
6 | * The authors hereby grant permission to use, copy, modify, distribute, |
---|
7 | * and license this software and its documentation for any purpose, provided |
---|
8 | * that existing copyright notices are retained in all copies and that this |
---|
9 | * notice is included verbatim in any distributions. No written agreement, |
---|
10 | * license, or royalty fee is required for any of the authorized uses. |
---|
11 | * Modifications to this software may be copyrighted by their authors |
---|
12 | * and need not follow the licensing terms described here, provided that |
---|
13 | * the new terms are clearly indicated on the first page of each file where |
---|
14 | * they apply. |
---|
15 | */ |
---|
16 | |
---|
17 | #include <_ansi.h> |
---|
18 | #include <sys/types.h> |
---|
19 | #include <sys/stat.h> |
---|
20 | #include <sys/fcntl.h> |
---|
21 | #include <stdio.h> |
---|
22 | #include <time.h> |
---|
23 | #include <sys/time.h> |
---|
24 | #include <sys/times.h> |
---|
25 | #include "syscall.h" |
---|
26 | #include <errno.h> |
---|
27 | #include <reent.h> |
---|
28 | #include <unistd.h> |
---|
29 | |
---|
30 | register char *stack_ptr asm ("SP"); |
---|
31 | |
---|
32 | static inline int |
---|
33 | do_syscall (int reason, void *arg) |
---|
34 | { |
---|
35 | int result, result2, errcode; |
---|
36 | asm volatile ("excpt 0;" |
---|
37 | : "=q0" (result), |
---|
38 | "=q1" (result2), |
---|
39 | "=q2" (errcode) |
---|
40 | : "qA" (reason), |
---|
41 | "q0" (arg) |
---|
42 | : "memory", "CC"); |
---|
43 | errno = errcode; |
---|
44 | return result; |
---|
45 | } |
---|
46 | |
---|
47 | int |
---|
48 | _read (int file, char *ptr, int len) |
---|
49 | { |
---|
50 | int block[3]; |
---|
51 | |
---|
52 | block[0] = file; |
---|
53 | block[1] = (int) ptr; |
---|
54 | block[2] = len; |
---|
55 | |
---|
56 | return do_syscall (SYS_read, block); |
---|
57 | } |
---|
58 | |
---|
59 | int |
---|
60 | _lseek (int file, int ptr, int whence) |
---|
61 | { |
---|
62 | int block[3]; |
---|
63 | |
---|
64 | block[0] = file; |
---|
65 | block[1] = ptr; |
---|
66 | block[2] = whence; |
---|
67 | |
---|
68 | return do_syscall (SYS_lseek, block); |
---|
69 | } |
---|
70 | |
---|
71 | int |
---|
72 | _write (int file, char *ptr, int len) |
---|
73 | { |
---|
74 | int block[3]; |
---|
75 | |
---|
76 | block[0] = file; |
---|
77 | block[1] = (int) ptr; |
---|
78 | block[2] = len; |
---|
79 | |
---|
80 | return do_syscall (SYS_write, block); |
---|
81 | } |
---|
82 | |
---|
83 | int |
---|
84 | _open (const char *path, int flags) |
---|
85 | { |
---|
86 | int block[2]; |
---|
87 | |
---|
88 | block[0] = (int) path; |
---|
89 | block[1] = flags; |
---|
90 | |
---|
91 | return do_syscall (SYS_open, block); |
---|
92 | } |
---|
93 | |
---|
94 | int |
---|
95 | _close (int file) |
---|
96 | { |
---|
97 | return do_syscall (SYS_close, &file); |
---|
98 | } |
---|
99 | |
---|
100 | void |
---|
101 | _exit (int n) |
---|
102 | { |
---|
103 | do_syscall (SYS_exit, &n); |
---|
104 | } |
---|
105 | |
---|
106 | int |
---|
107 | _kill (int n, int m) |
---|
108 | { |
---|
109 | int block[2]; |
---|
110 | |
---|
111 | block[0] = n; |
---|
112 | block[1] = m; |
---|
113 | |
---|
114 | return do_syscall (SYS_kill, block); |
---|
115 | } |
---|
116 | |
---|
117 | int |
---|
118 | _getpid (int n) |
---|
119 | { |
---|
120 | return do_syscall (SYS_getpid, &n); |
---|
121 | } |
---|
122 | |
---|
123 | caddr_t |
---|
124 | _sbrk (int incr) |
---|
125 | { |
---|
126 | extern char end; /* Defined by the linker. */ |
---|
127 | static char *heap_end; |
---|
128 | char *prev_heap_end; |
---|
129 | |
---|
130 | if (heap_end == NULL) |
---|
131 | heap_end = &end; |
---|
132 | |
---|
133 | prev_heap_end = heap_end; |
---|
134 | |
---|
135 | if (heap_end + incr > stack_ptr) |
---|
136 | { |
---|
137 | /* Some of the libstdc++-v3 tests rely upon detecting |
---|
138 | out of memory errors, so do not abort here. */ |
---|
139 | #if 0 |
---|
140 | extern void abort (void); |
---|
141 | |
---|
142 | _write (1, "_sbrk: Heap and stack collision\n", 32); |
---|
143 | |
---|
144 | abort (); |
---|
145 | #else |
---|
146 | errno = ENOMEM; |
---|
147 | return (caddr_t) -1; |
---|
148 | #endif |
---|
149 | } |
---|
150 | |
---|
151 | heap_end += incr; |
---|
152 | |
---|
153 | return (caddr_t) prev_heap_end; |
---|
154 | } |
---|
155 | |
---|
156 | extern void memset (struct stat *, int, unsigned int); |
---|
157 | |
---|
158 | int |
---|
159 | _fstat (int file, struct stat *st) |
---|
160 | { |
---|
161 | int block[2]; |
---|
162 | |
---|
163 | block[0] = file; |
---|
164 | block[1] = (int) st; |
---|
165 | |
---|
166 | return do_syscall (SYS_fstat, block); |
---|
167 | } |
---|
168 | |
---|
169 | int _stat (const char *fname, struct stat *st) |
---|
170 | { |
---|
171 | int block[2]; |
---|
172 | |
---|
173 | block[0] = (int) fname; |
---|
174 | block[1] = (int) st; |
---|
175 | |
---|
176 | return do_syscall (SYS_stat, block); |
---|
177 | } |
---|
178 | |
---|
179 | int |
---|
180 | _link (const char *existing, const char *new) |
---|
181 | { |
---|
182 | int block[2]; |
---|
183 | |
---|
184 | block[0] = (int) existing; |
---|
185 | block[1] = (int) new; |
---|
186 | |
---|
187 | return do_syscall (SYS_link, block); |
---|
188 | } |
---|
189 | |
---|
190 | int |
---|
191 | _unlink (const char *path) |
---|
192 | { |
---|
193 | return do_syscall (SYS_unlink, path); |
---|
194 | } |
---|
195 | |
---|
196 | void |
---|
197 | _raise (void) |
---|
198 | { |
---|
199 | return; |
---|
200 | } |
---|
201 | |
---|
202 | int |
---|
203 | _gettimeofday (struct timeval *tv, void *tz) |
---|
204 | { |
---|
205 | tv->tv_usec = 0; |
---|
206 | tv->tv_sec = do_syscall (SYS_time, 0); |
---|
207 | return 0; |
---|
208 | } |
---|
209 | |
---|
210 | /* Return a clock that ticks at 100Hz. */ |
---|
211 | clock_t |
---|
212 | _times (struct tms * tp) |
---|
213 | { |
---|
214 | return -1; |
---|
215 | } |
---|
216 | |
---|
217 | int |
---|
218 | _isatty (int fd) |
---|
219 | { |
---|
220 | return 1; |
---|
221 | } |
---|
222 | |
---|
223 | int |
---|
224 | _system (const char *s) |
---|
225 | { |
---|
226 | if (s == NULL) |
---|
227 | return 0; |
---|
228 | errno = ENOSYS; |
---|
229 | return -1; |
---|
230 | } |
---|
231 | |
---|
232 | int |
---|
233 | _rename (const char * oldpath, const char * newpath) |
---|
234 | { |
---|
235 | errno = ENOSYS; |
---|
236 | return -1; |
---|
237 | } |
---|
238 | |
---|
239 | static inline int |
---|
240 | __setup_argv_for_main (int argc) |
---|
241 | { |
---|
242 | int block[2]; |
---|
243 | char **argv; |
---|
244 | int i = argc; |
---|
245 | |
---|
246 | argv = __builtin_alloca ((1 + argc) * sizeof (*argv)); |
---|
247 | |
---|
248 | argv[i] = NULL; |
---|
249 | while (i--) { |
---|
250 | block[0] = i; |
---|
251 | argv[i] = __builtin_alloca (1 + do_syscall (SYS_argnlen, (void *)block)); |
---|
252 | block[1] = (int) argv[i]; |
---|
253 | do_syscall (SYS_argn, (void *)block); |
---|
254 | } |
---|
255 | |
---|
256 | return main (argc, argv); |
---|
257 | } |
---|
258 | |
---|
259 | int |
---|
260 | __setup_argv_and_call_main () |
---|
261 | { |
---|
262 | int argc = do_syscall (SYS_argc, 0); |
---|
263 | |
---|
264 | if (argc <= 0) |
---|
265 | return main (argc, NULL); |
---|
266 | else |
---|
267 | return __setup_argv_for_main (argc); |
---|
268 | } |
---|