OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [newlib/] [newlib/] [libc/] [sys/] [arm/] [syscalls.c] - Blame information for rev 39

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 39 lampret
#include <_ansi.h>
2
#include <sys/types.h>
3
#include <sys/stat.h>
4
#include <sys/fcntl.h>
5
#include <stdio.h>
6
#include <time.h>
7
#include <sys/time.h>
8
#include <sys/times.h>
9
#include <errno.h>
10
#include "swi.h"
11
 
12
/* Adjust our internal handles to stay away from std* handles */
13
#define FILE_HANDLE_OFFSET (0x20)
14
 
15
static int monitor_stdin;
16
static int monitor_stdout;
17
static int monitor_stderr;
18
 
19
/* Struct used to keep track of the file position, just so we
20
   can implement fseek(fh,x,SEEK_CUR).  */
21
typedef struct
22
{
23
  int handle;
24
  int pos;
25
}
26
poslog;
27
 
28
#define MAX_OPEN_FILES 20
29
static poslog openfiles [MAX_OPEN_FILES];
30
 
31
static int
32
findslot (int fh)
33
{
34
  int i;
35
  for (i = 0; i < MAX_OPEN_FILES; i ++)
36
    if (openfiles[i].handle == fh)
37
      break;
38
  return i;
39
}
40
 
41
#ifdef ARM_RDI_MONITOR
42
 
43
static inline int
44
do_AngelSWI (int reason, void * arg)
45
{
46
  int value;
47
  asm volatile ("mov r0, %1; mov r1, %2; swi %a3; mov %0, r0"
48
       : "=r" (value) /* Outputs */
49
       : "r" (reason), "r" (arg), "i" (AngelSWI) /* Inputs */
50
       : "r0", "r1", "lr"
51
                /* Clobbers r0 and r1, and lr if in supervisor mode */);
52
  return value;
53
}
54
#endif /* ARM_RDI_MONITOR */
55
 
56
/* Function to convert std(in|out|err) handles to internal versions */
57
static int
58
remap_handle (int fh)
59
{
60
  if (fh == __sfileno (stdin))
61
    return monitor_stdin;
62
  if (fh == __sfileno (stdout))
63
    return monitor_stdout;
64
  if (fh == __sfileno (stderr))
65
    return monitor_stderr;
66
 
67
  return fh - FILE_HANDLE_OFFSET;
68
}
69
 
70
void
71
initialise_monitor_handles (void)
72
{
73
  int i;
74
 
75
#ifdef ARM_RDI_MONITOR
76
  int block[3];
77
 
78
  block[0] = (int) ":tt";
79
  block[2] = 3;     /* length of filename */
80
  block[1] = 0;     /* mode "r" */
81
  monitor_stdin = do_AngelSWI (AngelSWI_Reason_Open, block);
82
 
83
  block[0] = (int) ":tt";
84
  block[2] = 3;     /* length of filename */
85
  block[1] = 4;     /* mode "w" */
86
  monitor_stdout = monitor_stderr = do_AngelSWI (AngelSWI_Reason_Open, block);
87
#else
88
  int fh;
89
  const char * name;
90
 
91
  name = ":tt";
92
  asm ("mov r0,%2; mov r1, #0; swi %a1; mov %0, r0"
93
       : "=r"(fh)
94
       : "i" (SWI_Open),"r"(name)
95
       : "r0","r1");
96
  monitor_stdin = fh;
97
 
98
  name = ":tt";
99
  asm ("mov r0,%2; mov r1, #4; swi %a1; mov %0, r0"
100
       : "=r"(fh)
101
       : "i" (SWI_Open),"r"(name)
102
       : "r0","r1");
103
  monitor_stdout = monitor_stderr = fh;
104
#endif
105
 
106
  for (i = 0; i < MAX_OPEN_FILES; i ++)
107
    openfiles[i].handle = -1;
108
 
109
  openfiles[0].handle = monitor_stdin;
110
  openfiles[0].pos = 0;
111
  openfiles[1].handle = monitor_stdout;
112
  openfiles[1].pos = 0;
113
}
114
 
115
static int
116
get_errno ()
117
{
118
#ifdef ARM_RDI_MONITOR
119
  return do_AngelSWI (AngelSWI_Reason_Errno, NULL);
120
#else
121
  asm ("swi %a0" :: "i" (SWI_GetErrno));
122
#endif
123
}
124
 
125
static int
126
error (int result)
127
{
128
  errno = get_errno ();
129
  return result;
130
}
131
 
132
static int
133
wrap (int result)
134
{
135
  if (result == -1)
136
    return error (-1);
137
  return result;
138
}
139
 
140
/* Returns # chars not! written */
141
 
142
int
143
_swiread (int file,
144
          char * ptr,
145
          int len)
146
{
147
  int fh = remap_handle (file);
148
#ifdef ARM_RDI_MONITOR
149
  int block[3];
150
 
151
  block[0] = fh;
152
  block[1] = (int) ptr;
153
  block[2] = len;
154
 
155
  return do_AngelSWI (AngelSWI_Reason_Read, block);
156
#else
157
  asm ("mov r0, %1; mov r1, %2;mov r2, %3; swi %a0"
158
       : /* No outputs */
159
       : "i"(SWI_Read), "r"(fh), "r"(ptr), "r"(len)
160
       : "r0","r1","r2");
161
#endif
162
}
163
 
164
int
165
_read (int file,
166
       char * ptr,
167
       int len)
168
{
169
  int slot = findslot (remap_handle (file));
170
  int x = _swiread (file, ptr, len);
171
 
172
  if (x < 0)
173
    return error (-1);
174
 
175
  if (slot != MAX_OPEN_FILES)
176
    openfiles [slot].pos += len - x;
177
 
178
  /* x == len is not an error, at least if we want feof() to work */
179
  return len - x;
180
}
181
 
182
int
183
_swilseek (int file,
184
        int ptr,
185
        int dir)
186
{
187
  int res;
188
  int fh = remap_handle (file);
189
  int slot = findslot (fh);
190
#ifdef ARM_RDI_MONITOR
191
  int block[2];
192
#endif
193
 
194
  if (dir == SEEK_CUR)
195
  {
196
    if (slot == MAX_OPEN_FILES)
197
      return -1;
198
    ptr = openfiles[slot].pos + ptr;
199
    dir = SEEK_SET;
200
  }
201
 
202
#ifdef ARM_RDI_MONITOR
203
  if (dir == SEEK_END)
204
  {
205
    block[0] = fh;
206
    ptr += do_AngelSWI (AngelSWI_Reason_FLen, block);
207
  }
208
 
209
  /* This code only does absolute seeks */
210
  block[0] = remap_handle (file);
211
  block[1] = ptr;
212
  res = do_AngelSWI (AngelSWI_Reason_Seek, block);
213
#else
214
  if (dir == SEEK_END)
215
  {
216
    asm ("mov r0, %2; swi %a1; mov %0, r0"
217
         : "=r" (res)
218
         : "i" (SWI_Flen), "r" (fh)
219
         : "r0");
220
    ptr += res;
221
  }
222
 
223
  /* This code only does absolute seeks */
224
  asm ("mov r0, %2; mov r1, %3; swi %a1; mov %0, r0"
225
       : "=r" (res)
226
       : "i" (SWI_Seek), "r" (fh), "r" (ptr)
227
       : "r0", "r1");
228
#endif
229
 
230
  if (slot != MAX_OPEN_FILES && res == 0)
231
    openfiles[slot].pos = ptr;
232
 
233
  /* This is expected to return the position in the file */
234
  return res == 0 ? ptr : -1;
235
}
236
 
237
int
238
_lseek (int file,
239
        int ptr,
240
        int dir)
241
{
242
  return wrap (_swilseek (file, ptr, dir));
243
}
244
 
245
/* Returns #chars not! written */
246
int
247
_swiwrite (
248
         int file,
249
         char * ptr,
250
         int len)
251
{
252
  int fh = remap_handle (file);
253
#ifdef ARM_RDI_MONITOR
254
  int block[3];
255
 
256
  block[0] = fh;
257
  block[1] = (int) ptr;
258
  block[2] = len;
259
 
260
  return do_AngelSWI (AngelSWI_Reason_Write, block);
261
#else
262
  asm ("mov r0, %1; mov r1, %2;mov r2, %3; swi %a0"
263
       : /* No outputs */
264
       : "i"(SWI_Write), "r"(fh), "r"(ptr), "r"(len)
265
       : "r0","r1","r2");
266
#endif
267
}
268
 
269
int
270
_write (int file,
271
        char *ptr,
272
        int len)
273
{
274
  int slot = findslot (remap_handle (file));
275
  int x = _swiwrite (file, ptr,len);
276
 
277
  if (x == -1 || x == len)
278
    return error (-1);
279
 
280
  if (slot != MAX_OPEN_FILES)
281
    openfiles[slot].pos += len - x;
282
 
283
  return len - x;
284
}
285
 
286
int
287
_swiopen (const char * path,
288
       int flags)
289
{
290
  int aflags = 0, fh;
291
#ifdef ARM_RDI_MONITOR
292
  int block[3];
293
#endif
294
 
295
  int i = findslot (-1);
296
 
297
  if (i == MAX_OPEN_FILES)
298
    return -1;
299
 
300
  /* The flags are Unix-style, so we need to convert them */
301
#ifdef O_BINARY
302
  if (flags & O_BINARY)
303
    aflags |= 1;
304
#endif
305
 
306
  if (flags & O_RDWR)
307
    aflags |= 2;
308
 
309
  if (flags & O_CREAT)
310
    aflags |= 4;
311
 
312
  if (flags & O_TRUNC)
313
    aflags |= 4;
314
 
315
  if (flags & O_APPEND)
316
    {
317
      aflags &= ~4;     /* Can't ask for w AND a; means just 'a' */
318
      aflags |= 8;
319
    }
320
 
321
#ifdef ARM_RDI_MONITOR
322
  block[0] = (int) path;
323
  block[2] = strlen (path);
324
  block[1] = aflags;
325
 
326
  fh = do_AngelSWI (AngelSWI_Reason_Open, block);
327
 
328
#else
329
  asm ("mov r0,%2; mov r1, %3; swi %a1; mov %0, r0"
330
       : "=r"(fh)
331
       : "i" (SWI_Open),"r"(path),"r"(aflags)
332
       : "r0","r1");
333
#endif
334
 
335
  if (fh >= 0)
336
    {
337
      openfiles[i].handle = fh;
338
      openfiles[i].pos = 0;
339
    }
340
 
341
  return fh >= 0 ? fh + FILE_HANDLE_OFFSET : error (fh);
342
}
343
 
344
int
345
_open (const char *path,
346
       int flags,...)
347
{
348
  return wrap (_swiopen (path, flags));
349
}
350
 
351
int
352
_swiclose (int file)
353
{
354
  int myhan = remap_handle (file);
355
  int slot = findslot (myhan);
356
 
357
  if (slot != MAX_OPEN_FILES)
358
    openfiles[slot].handle = -1;
359
 
360
#ifdef ARM_RDI_MONITOR
361
  return do_AngelSWI (AngelSWI_Reason_Close, & myhan);
362
#else
363
  asm ("mov r0, %1; swi %a0" :: "i" (SWI_Close),"r"(myhan):"r0");
364
#endif
365
}
366
 
367
int
368
_close (int file)
369
{
370
  return wrap (_swiclose (file));
371
}
372
 
373
void
374
_exit (n)
375
{
376
#ifdef ARM_RDI_MONITOR
377
  do_AngelSWI (AngelSWI_Reason_ReportException,
378
              (void *) ADP_Stopped_ApplicationExit);
379
#else
380
  asm ("swi %a0" :: "i" (SWI_Exit));
381
#endif
382
}
383
 
384
void
385
abort ()
386
{
387
#ifdef ARM_RDI_MONITOR
388
  do_AngelSWI (AngelSWI_Reason_ReportException,
389
              (void *) ADP_Stopped_RunTimeError);
390
#else
391
 asm ("mov r0,#17\nswi %a0" :: "i" (SWI_Exit));
392
#endif
393
}
394
 
395
int
396
_kill (n, m)
397
{
398
#ifdef ARM_RDI_MONITOR
399
  do_AngelSWI (AngelSWI_Reason_ReportException,
400
              (void *) ADP_Stopped_ApplicationExit);
401
#else
402
  asm ("swi %a0" :: "i" (SWI_Exit));
403
#endif
404
}
405
 
406
int
407
_getpid (n)
408
{
409
  return 1;
410
}
411
 
412
register char *stack_ptr asm ("sp");
413
 
414
caddr_t
415
_sbrk (int incr)
416
{
417
  extern char end asm ("__end__");      /* Defined by the linker */
418
  static char *heap_end;
419
  char *prev_heap_end;
420
 
421
  if (heap_end == NULL)
422
    {
423
      heap_end = & end;
424
    }
425
 
426
  prev_heap_end = heap_end;
427
 
428
  if (heap_end + incr > stack_ptr)
429
    {
430
      _write (1, "_sbrk: Heap and stack collision\n", 32);
431
      abort ();
432
    }
433
 
434
  heap_end += incr;
435
 
436
  return (caddr_t) prev_heap_end;
437
}
438
 
439
int
440
_fstat (int file,
441
        struct stat *st)
442
{
443
  st->st_mode = S_IFCHR;
444
  return 0;
445
}
446
 
447
int
448
_unlink ()
449
{
450
  return -1;
451
}
452
 
453
isatty (fd)
454
     int fd;
455
{
456
  return 1;
457
}
458
 
459
_raise ()
460
{
461
}
462
 
463
int
464
_gettimeofday (struct timeval *tp, struct timezone *tzp)
465
{
466
 
467
  if (tp)
468
    {
469
    /* Ask the host for the seconds since the Unix epoch */
470
#ifdef ARM_RDI_MONITOR
471
      tp->tv_sec = do_AngelSWI(AngelSWI_Reason_Time,NULL);
472
#else
473
      {
474
        int value;
475
        asm ("swi %a1; mov %0, r0" : "=r" (value): "i" (SWI_Time) : "r0");
476
        tp->tv_sec = value;
477
      }
478
#endif
479
      tp->tv_usec = 0;
480
    }
481
 
482
  /* Return fixed data for the timezone */
483
  if (tzp)
484
    {
485
      tzp->tz_minuteswest = 0;
486
      tzp->tz_dsttime = 0;
487
    }
488
 
489
  return 0;
490
}
491
 
492
/* Return a clock that ticks at 100Hz. */
493
clock_t
494
_times(struct tms *tp)
495
{
496
  clock_t timeval;
497
 
498
#ifdef ARM_RDI_MONITOR
499
  timeval = do_AngelSWI(AngelSWI_Reason_Clock,NULL);
500
#else
501
  asm ("swi %a1; mov %0, r0" : "=r" (timeval): "i" (SWI_Clock) : "r0");
502
#endif
503
 
504
  if (tp)
505
    {
506
      tp->tms_utime=timeval;    /* user time */
507
      tp->tms_stime=0;           /* system time */
508
      tp->tms_cutime=0;          /* user time, children */
509
      tp->tms_cstime=0;          /* system time, children */
510
    }
511
  return timeval;
512
};
513
 
514
#if 0
515
int
516
_stat (const char *path, struct stat *st)
517
{
518
  asm ("swi %a0" :: "i" (SWI_Stat));
519
}
520
 
521
int
522
_chmod (const char *path, short mode)
523
{
524
  asm ("swi %a0" :: "i" (SWI_Chmod));
525
}
526
 
527
int
528
_chown (const char *path, short owner, short group)
529
{
530
  asm ("swi %a0" :: "i" (SWI_Chown));
531
}
532
 
533
int
534
_utime (path, times)
535
     const char *path;
536
     char *times;
537
{
538
  asm ("swi %a0" :: "i" (SWI_Utime));
539
}
540
 
541
int
542
_fork ()
543
{
544
  asm ("swi %a0" :: "i" (SWI_Fork));
545
}
546
 
547
int
548
_wait (statusp)
549
     int *statusp;
550
{
551
  asm ("swi %a0" :: "i" (SWI_Wait));
552
}
553
 
554
int
555
_execve (const char *path, char *const argv[], char *const envp[])
556
{
557
  return _trap3 (SYS_execve, path, argv, envp);
558
}
559
 
560
int
561
_execv (const char *path, char *const argv[])
562
{
563
  return _trap3 (SYS_execv, path, argv);
564
}
565
 
566
int
567
_pipe (int *fd)
568
{
569
  return _trap3 (SYS_pipe, fd);
570
}
571
#endif
572
 
573
alarm()
574
{
575
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.