Cygwin: cygwin_GetCommandLineW/A: don't rely on __argv[0] content
[newlib-cygwin.git] / libgloss / arm / redboot-syscalls.c
blobc6b743e67edf97c7a178c16740e45980ce20f516
1 /*
2 * redboot-syscalls.c -- provide system call support for RedBoot
4 * Copyright (c) 1997, 2001, 2002 Red Hat, Inc.
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.
18 #include <stdlib.h>
19 #include <sys/stat.h>
20 #include <sys/times.h>
21 #include <errno.h>
22 #include "syscall.h"
24 // Use "naked" attribute to suppress C prologue/epilogue
25 static int __attribute__ ((naked)) __syscall(int func_no, ...)
27 asm ("mov r12, lr\n");
28 #ifdef __thumb__
29 asm ("swi 0x18\n");
30 #else
31 asm ("swi 0x180001\n");
32 #endif
33 asm ("mov pc, r12\n");
36 int
37 _close(int fd)
39 int err;
40 err = __syscall(SYS_close, fd);
41 if (err<0)
43 errno = -err;
44 return -1;
46 return err;
50 void
51 _exit(int stat)
53 while (1)
54 __syscall(SYS_exit, stat);
58 int
59 _stat (const char *filename, struct stat *st)
61 int err;
62 err = __syscall(SYS_stat, filename, st);
63 if (err<0)
65 errno = -err;
66 return -1;
68 return err;
71 int
72 _fstat (int file, struct stat *st)
74 int err;
75 err = __syscall(SYS_fstat, file, st);
76 if (err<0)
78 errno = -err;
79 return -1;
81 return err;
84 int
85 _getpid(void)
87 return 1;
91 int
92 _gettimeofday (void * tp, void * tzp)
94 int err;
95 err = __syscall(SYS_gettimeofday, tp, tzp);
96 if (err<0)
98 errno = -err;
99 return -1;
101 return err;
106 _isatty(int fd)
108 int err;
109 err = __syscall(SYS_isatty, fd);
110 if (err<0)
112 errno = -err;
113 return -1;
115 return err;
120 _kill(int pid, int sig)
122 if(pid == 1)
123 _exit(sig);
124 return 0;
128 off_t
129 _lseek(int fd, off_t offset, int whence)
131 int err;
132 err = __syscall(SYS_lseek, fd, offset, whence);
133 if (err<0)
135 errno = -err;
136 return (off_t)-1;
138 return err;
143 _open(const char *buf, int flags, int mode)
145 int err ;
146 err = __syscall(SYS_open, buf, flags, mode);
147 if (err<0)
149 errno = -err;
150 return -1;
152 return err;
157 _write(int fd, const char *buf, int nbytes)
159 int err;
161 err = __syscall(SYS_write, fd, buf, nbytes);
162 if (err<0)
164 errno = -err;
165 return -1;
167 return err;
171 void
172 print(char *ptr)
174 char *p = ptr;
176 while (*p != '\0')
177 p++;
179 _write (1, ptr, p-ptr);
182 void
183 _raise (void)
185 return;
190 _read(int fd, char *buf, int nbytes)
192 int err;
193 err = __syscall(SYS_read, fd, buf, nbytes);
194 if (err<0)
196 errno = -err;
197 return -1;
199 return err;
203 extern char end[]; /* end is set in the linker command file */
205 char *heap_ptr;
207 char *
208 _sbrk (int nbytes)
210 char *base;
212 if (!heap_ptr)
213 heap_ptr = (char *)&end;
214 base = heap_ptr;
215 heap_ptr += nbytes;
217 return base;
221 clock_t
222 _times(struct tms * tp)
224 clock_t utime;
225 int err;
226 err = __syscall(SYS_times, &utime);
227 if (err)
228 utime = 0;
230 if (tp) {
231 tp->tms_utime = utime;
232 tp->tms_stime = 0;
233 tp->tms_cutime = 0;
234 tp->tms_cstime = 0;
237 return utime;
241 _rename (const char *oldpath, const char *newpath)
243 int err ;
244 err = __syscall(SYS_rename, oldpath, newpath);
245 if (err<0)
247 errno = -err;
248 return -1;
250 return err;
254 _unlink (const char *pathname)
256 int err ;
257 err = __syscall(SYS_unlink, pathname);
258 if (err<0)
260 errno = -err;
261 return -1;
263 return err;
267 _system (const char *command)
269 int err ;
270 err = __syscall(SYS_system, command);
271 return err;
274 #define SYS_meminfo 1001
276 void *
277 __get_memtop(void)
279 unsigned long totmem = 0, topmem = 0;
280 int numbanks;
282 __syscall(SYS_meminfo, (unsigned long)&totmem, (unsigned long)&topmem, 0);
283 return (void*)topmem;