1999-04-16 01:35:26 +00:00
|
|
|
/* armos.c -- ARMulator OS interface: ARM6 Instruction Emulator.
|
|
|
|
Copyright (C) 1994 Advanced RISC Machines Ltd.
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
|
|
|
|
|
|
|
|
/* This file contains a model of Demon, ARM Ltd's Debug Monitor,
|
2000-12-03 23:28:46 +00:00
|
|
|
including all the SWI's required to support the C library. The code in
|
|
|
|
it is not really for the faint-hearted (especially the abort handling
|
|
|
|
code), but it is a complete example. Defining NOOS will disable all the
|
|
|
|
fun, and definign VAILDATE will define SWI 1 to enter SVC mode, and SWI
|
|
|
|
0x11 to halt the emulator. */
|
1999-04-16 01:35:26 +00:00
|
|
|
|
|
|
|
#include "config.h"
|
2000-02-08 20:54:27 +00:00
|
|
|
#include "ansidecl.h"
|
1999-04-16 01:35:26 +00:00
|
|
|
|
|
|
|
#include <time.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#ifndef O_RDONLY
|
|
|
|
#define O_RDONLY 0
|
|
|
|
#endif
|
|
|
|
#ifndef O_WRONLY
|
|
|
|
#define O_WRONLY 1
|
|
|
|
#endif
|
|
|
|
#ifndef O_RDWR
|
|
|
|
#define O_RDWR 2
|
|
|
|
#endif
|
|
|
|
#ifndef O_BINARY
|
|
|
|
#define O_BINARY 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef __STDC__
|
|
|
|
#define unlink(s) remove(s)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_UNISTD_H
|
2000-02-05 07:30:26 +00:00
|
|
|
#include <unistd.h> /* For SEEK_SET etc */
|
1999-04-16 01:35:26 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef __riscos
|
2000-02-05 07:30:26 +00:00
|
|
|
extern int _fisatty (FILE *);
|
1999-04-16 01:35:26 +00:00
|
|
|
#define isatty_(f) _fisatty(f)
|
|
|
|
#else
|
|
|
|
#ifdef __ZTC__
|
|
|
|
#include <io.h>
|
|
|
|
#define isatty_(f) isatty((f)->_file)
|
|
|
|
#else
|
|
|
|
#ifdef macintosh
|
|
|
|
#include <ioctl.h>
|
|
|
|
#define isatty_(f) (~ioctl ((f)->_file, FIOINTERACTIVE, NULL))
|
|
|
|
#else
|
|
|
|
#define isatty_(f) isatty (fileno (f))
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "armdefs.h"
|
|
|
|
#include "armos.h"
|
2000-11-30 01:55:12 +00:00
|
|
|
#include "armemu.h"
|
|
|
|
|
1999-04-16 01:35:26 +00:00
|
|
|
#ifndef NOOS
|
|
|
|
#ifndef VALIDATE
|
|
|
|
/* #ifndef ASIM */
|
|
|
|
#include "armfpe.h"
|
|
|
|
/* #endif */
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* For RDIError_BreakpointReached. */
|
|
|
|
#include "dbg_rdi.h"
|
|
|
|
|
2000-02-05 07:30:26 +00:00
|
|
|
extern unsigned ARMul_OSInit (ARMul_State * state);
|
|
|
|
extern void ARMul_OSExit (ARMul_State * state);
|
|
|
|
extern unsigned ARMul_OSHandleSWI (ARMul_State * state, ARMword number);
|
|
|
|
extern unsigned ARMul_OSException (ARMul_State * state, ARMword vector,
|
|
|
|
ARMword pc);
|
|
|
|
extern ARMword ARMul_OSLastErrorP (ARMul_State * state);
|
|
|
|
extern ARMword ARMul_Debug (ARMul_State * state, ARMword pc, ARMword instr);
|
1999-04-16 01:35:26 +00:00
|
|
|
|
|
|
|
#define BUFFERSIZE 4096
|
|
|
|
#ifndef FOPEN_MAX
|
|
|
|
#define FOPEN_MAX 64
|
|
|
|
#endif
|
|
|
|
#define UNIQUETEMPS 256
|
|
|
|
|
2000-12-03 23:28:46 +00:00
|
|
|
/* OS private Information. */
|
1999-04-16 01:35:26 +00:00
|
|
|
|
2000-02-05 07:30:26 +00:00
|
|
|
struct OSblock
|
|
|
|
{
|
|
|
|
ARMword Time0;
|
|
|
|
ARMword ErrorP;
|
|
|
|
ARMword ErrorNo;
|
|
|
|
FILE *FileTable[FOPEN_MAX];
|
|
|
|
char FileFlags[FOPEN_MAX];
|
|
|
|
char *tempnames[UNIQUETEMPS];
|
|
|
|
};
|
1999-04-16 01:35:26 +00:00
|
|
|
|
|
|
|
#define NOOP 0
|
|
|
|
#define BINARY 1
|
|
|
|
#define READOP 2
|
|
|
|
#define WRITEOP 4
|
|
|
|
|
|
|
|
#ifdef macintosh
|
|
|
|
#define FIXCRLF(t,c) ((t & BINARY) ? \
|
|
|
|
c : \
|
|
|
|
((c == '\n' || c == '\r' ) ? (c ^ 7) : c) \
|
|
|
|
)
|
2000-02-05 07:30:26 +00:00
|
|
|
#else
|
|
|
|
#define FIXCRLF(t,c) c
|
1999-04-16 01:35:26 +00:00
|
|
|
#endif
|
|
|
|
|
2000-02-08 20:54:27 +00:00
|
|
|
static ARMword softvectorcode[] =
|
2000-12-03 23:28:46 +00:00
|
|
|
{
|
|
|
|
/* Basic: swi tidyexception + event; mov pc, lr;
|
|
|
|
ldmia r11,{r11,pc}; swi generateexception + event. */
|
|
|
|
0xef000090, 0xe1a0e00f, 0xe89b8800, 0xef000080, /* Reset */
|
|
|
|
0xef000091, 0xe1a0e00f, 0xe89b8800, 0xef000081, /* Undef */
|
|
|
|
0xef000092, 0xe1a0e00f, 0xe89b8800, 0xef000082, /* SWI */
|
|
|
|
0xef000093, 0xe1a0e00f, 0xe89b8800, 0xef000083, /* Prefetch abort */
|
|
|
|
0xef000094, 0xe1a0e00f, 0xe89b8800, 0xef000084, /* Data abort */
|
|
|
|
0xef000095, 0xe1a0e00f, 0xe89b8800, 0xef000085, /* Address exception */
|
|
|
|
0xef000096, 0xe1a0e00f, 0xe89b8800, 0xef000086, /* IRQ */
|
|
|
|
0xef000097, 0xe1a0e00f, 0xe89b8800, 0xef000087, /* FIQ */
|
|
|
|
0xef000098, 0xe1a0e00f, 0xe89b8800, 0xef000088, /* Error */
|
|
|
|
0xe1a0f00e /* Default handler */
|
1999-04-16 01:35:26 +00:00
|
|
|
};
|
|
|
|
|
2001-02-14 03:50:46 +00:00
|
|
|
/* Set to prevent aborts when emulating SWI routines. */
|
|
|
|
static int in_SWI_handler = 0;
|
|
|
|
|
2000-12-03 23:28:46 +00:00
|
|
|
/* Time for the Operating System to initialise itself. */
|
1999-04-16 01:35:26 +00:00
|
|
|
|
2000-02-05 07:30:26 +00:00
|
|
|
unsigned
|
|
|
|
ARMul_OSInit (ARMul_State * state)
|
1999-04-16 01:35:26 +00:00
|
|
|
{
|
|
|
|
#ifndef NOOS
|
|
|
|
#ifndef VALIDATE
|
2000-02-05 07:30:26 +00:00
|
|
|
ARMword instr, i, j;
|
|
|
|
struct OSblock *OSptr = (struct OSblock *) state->OSptr;
|
|
|
|
|
|
|
|
if (state->OSptr == NULL)
|
|
|
|
{
|
|
|
|
state->OSptr = (unsigned char *) malloc (sizeof (struct OSblock));
|
|
|
|
if (state->OSptr == NULL)
|
|
|
|
{
|
|
|
|
perror ("OS Memory");
|
|
|
|
exit (15);
|
|
|
|
}
|
1999-04-16 01:35:26 +00:00
|
|
|
}
|
2000-12-03 23:28:46 +00:00
|
|
|
|
2000-02-05 07:30:26 +00:00
|
|
|
OSptr = (struct OSblock *) state->OSptr;
|
|
|
|
OSptr->ErrorP = 0;
|
|
|
|
state->Reg[13] = ADDRSUPERSTACK; /* set up a stack for the current mode */
|
|
|
|
ARMul_SetReg (state, SVC32MODE, 13, ADDRSUPERSTACK); /* and for supervisor mode */
|
|
|
|
ARMul_SetReg (state, ABORT32MODE, 13, ADDRSUPERSTACK); /* and for abort 32 mode */
|
|
|
|
ARMul_SetReg (state, UNDEF32MODE, 13, ADDRSUPERSTACK); /* and for undef 32 mode */
|
2000-05-30 17:13:37 +00:00
|
|
|
ARMul_SetReg (state, SYSTEMMODE, 13, ADDRSUPERSTACK); /* and for system mode */
|
2000-02-05 07:30:26 +00:00
|
|
|
instr = 0xe59ff000 | (ADDRSOFTVECTORS - 8); /* load pc from soft vector */
|
2000-12-03 23:28:46 +00:00
|
|
|
|
2000-02-05 07:30:26 +00:00
|
|
|
for (i = ARMul_ResetV; i <= ARMFIQV; i += 4)
|
|
|
|
ARMul_WriteWord (state, i, instr); /* write hardware vectors */
|
2000-12-03 23:28:46 +00:00
|
|
|
|
2001-02-01 00:14:40 +00:00
|
|
|
SWI_vector_installed = 0;
|
|
|
|
|
2000-02-05 07:30:26 +00:00
|
|
|
for (i = ARMul_ResetV; i <= ARMFIQV + 4; i += 4)
|
|
|
|
{
|
|
|
|
ARMul_WriteWord (state, ADDRSOFTVECTORS + i, SOFTVECTORCODE + i * 4);
|
|
|
|
ARMul_WriteWord (state, ADDRSOFHANDLERS + 2 * i + 4L,
|
|
|
|
SOFTVECTORCODE + sizeof (softvectorcode) - 4L);
|
1999-04-16 01:35:26 +00:00
|
|
|
}
|
2000-12-03 23:28:46 +00:00
|
|
|
|
2000-02-05 07:30:26 +00:00
|
|
|
for (i = 0; i < sizeof (softvectorcode); i += 4)
|
|
|
|
ARMul_WriteWord (state, SOFTVECTORCODE + i, softvectorcode[i / 4]);
|
2000-12-03 23:28:46 +00:00
|
|
|
|
2000-02-05 07:30:26 +00:00
|
|
|
for (i = 0; i < FOPEN_MAX; i++)
|
|
|
|
OSptr->FileTable[i] = NULL;
|
2000-12-03 23:28:46 +00:00
|
|
|
|
2000-02-05 07:30:26 +00:00
|
|
|
for (i = 0; i < UNIQUETEMPS; i++)
|
|
|
|
OSptr->tempnames[i] = NULL;
|
2000-12-03 23:28:46 +00:00
|
|
|
|
2000-02-05 07:30:26 +00:00
|
|
|
ARMul_ConsolePrint (state, ", Demon 1.01");
|
1999-04-16 01:35:26 +00:00
|
|
|
|
|
|
|
/* #ifndef ASIM */
|
|
|
|
|
2000-02-05 07:30:26 +00:00
|
|
|
/* install fpe */
|
|
|
|
for (i = 0; i < fpesize; i += 4) /* copy the code */
|
|
|
|
ARMul_WriteWord (state, FPESTART + i, fpecode[i >> 2]);
|
2000-12-03 23:28:46 +00:00
|
|
|
|
2000-02-05 07:30:26 +00:00
|
|
|
for (i = FPESTART + fpesize;; i -= 4)
|
|
|
|
{ /* reverse the error strings */
|
|
|
|
if ((j = ARMul_ReadWord (state, i)) == 0xffffffff)
|
|
|
|
break;
|
|
|
|
if (state->bigendSig && j < 0x80000000)
|
|
|
|
{ /* it's part of the string so swap it */
|
|
|
|
j = ((j >> 0x18) & 0x000000ff) |
|
|
|
|
((j >> 0x08) & 0x0000ff00) |
|
|
|
|
((j << 0x08) & 0x00ff0000) | ((j << 0x18) & 0xff000000);
|
|
|
|
ARMul_WriteWord (state, i, j);
|
|
|
|
}
|
1999-04-16 01:35:26 +00:00
|
|
|
}
|
2000-12-03 23:28:46 +00:00
|
|
|
|
2000-02-05 07:30:26 +00:00
|
|
|
ARMul_WriteWord (state, FPEOLDVECT, ARMul_ReadWord (state, 4)); /* copy old illegal instr vector */
|
|
|
|
ARMul_WriteWord (state, 4, FPENEWVECT (ARMul_ReadWord (state, i - 4))); /* install new vector */
|
|
|
|
ARMul_ConsolePrint (state, ", FPE");
|
1999-04-16 01:35:26 +00:00
|
|
|
|
2000-02-08 20:54:27 +00:00
|
|
|
/* #endif ASIM */
|
1999-04-16 01:35:26 +00:00
|
|
|
#endif /* VALIDATE */
|
|
|
|
#endif /* NOOS */
|
|
|
|
|
2000-12-03 23:28:46 +00:00
|
|
|
return TRUE;
|
1999-04-16 01:35:26 +00:00
|
|
|
}
|
|
|
|
|
2000-02-05 07:30:26 +00:00
|
|
|
void
|
|
|
|
ARMul_OSExit (ARMul_State * state)
|
1999-04-16 01:35:26 +00:00
|
|
|
{
|
2000-02-05 07:30:26 +00:00
|
|
|
free ((char *) state->OSptr);
|
1999-04-16 01:35:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-12-03 23:28:46 +00:00
|
|
|
/* Return the last Operating System Error. */
|
1999-04-16 01:35:26 +00:00
|
|
|
|
2000-02-05 07:30:26 +00:00
|
|
|
ARMword ARMul_OSLastErrorP (ARMul_State * state)
|
1999-04-16 01:35:26 +00:00
|
|
|
{
|
2000-02-05 07:30:26 +00:00
|
|
|
return ((struct OSblock *) state->OSptr)->ErrorP;
|
1999-04-16 01:35:26 +00:00
|
|
|
}
|
|
|
|
|
2000-12-03 23:28:46 +00:00
|
|
|
static int translate_open_mode[] =
|
|
|
|
{
|
2000-02-05 07:30:26 +00:00
|
|
|
O_RDONLY, /* "r" */
|
|
|
|
O_RDONLY + O_BINARY, /* "rb" */
|
|
|
|
O_RDWR, /* "r+" */
|
|
|
|
O_RDWR + O_BINARY, /* "r+b" */
|
|
|
|
O_WRONLY + O_CREAT + O_TRUNC, /* "w" */
|
|
|
|
O_WRONLY + O_BINARY + O_CREAT + O_TRUNC, /* "wb" */
|
|
|
|
O_RDWR + O_CREAT + O_TRUNC, /* "w+" */
|
|
|
|
O_RDWR + O_BINARY + O_CREAT + O_TRUNC, /* "w+b" */
|
|
|
|
O_WRONLY + O_APPEND + O_CREAT, /* "a" */
|
|
|
|
O_WRONLY + O_BINARY + O_APPEND + O_CREAT, /* "ab" */
|
|
|
|
O_RDWR + O_APPEND + O_CREAT, /* "a+" */
|
|
|
|
O_RDWR + O_BINARY + O_APPEND + O_CREAT /* "a+b" */
|
1999-04-16 01:35:26 +00:00
|
|
|
};
|
|
|
|
|
2000-02-05 07:30:26 +00:00
|
|
|
static void
|
|
|
|
SWIWrite0 (ARMul_State * state, ARMword addr)
|
1999-04-16 01:35:26 +00:00
|
|
|
{
|
|
|
|
ARMword temp;
|
2000-02-05 07:30:26 +00:00
|
|
|
struct OSblock *OSptr = (struct OSblock *) state->OSptr;
|
1999-04-16 01:35:26 +00:00
|
|
|
|
|
|
|
while ((temp = ARMul_ReadByte (state, addr++)) != 0)
|
1999-10-05 23:13:56 +00:00
|
|
|
(void) fputc ((char) temp, stdout);
|
1999-04-16 01:35:26 +00:00
|
|
|
|
|
|
|
OSptr->ErrorNo = errno;
|
|
|
|
}
|
|
|
|
|
2000-02-05 07:30:26 +00:00
|
|
|
static void
|
|
|
|
WriteCommandLineTo (ARMul_State * state, ARMword addr)
|
1999-04-16 01:35:26 +00:00
|
|
|
{
|
|
|
|
ARMword temp;
|
|
|
|
char *cptr = state->CommandLine;
|
2000-12-03 23:28:46 +00:00
|
|
|
|
1999-04-16 01:35:26 +00:00
|
|
|
if (cptr == NULL)
|
|
|
|
cptr = "\0";
|
2000-02-05 07:30:26 +00:00
|
|
|
do
|
|
|
|
{
|
|
|
|
temp = (ARMword) * cptr++;
|
|
|
|
ARMul_WriteByte (state, addr++, temp);
|
|
|
|
}
|
|
|
|
while (temp != 0);
|
1999-04-16 01:35:26 +00:00
|
|
|
}
|
|
|
|
|
2000-02-05 07:30:26 +00:00
|
|
|
static void
|
|
|
|
SWIopen (ARMul_State * state, ARMword name, ARMword SWIflags)
|
1999-04-16 01:35:26 +00:00
|
|
|
{
|
2000-02-05 07:30:26 +00:00
|
|
|
struct OSblock *OSptr = (struct OSblock *) state->OSptr;
|
1999-04-16 01:35:26 +00:00
|
|
|
char dummy[2000];
|
|
|
|
int flags;
|
|
|
|
int i;
|
|
|
|
|
2000-02-08 20:54:27 +00:00
|
|
|
for (i = 0; (dummy[i] = ARMul_ReadByte (state, name + i)); i++)
|
1999-04-16 01:35:26 +00:00
|
|
|
;
|
|
|
|
|
2000-12-03 23:28:46 +00:00
|
|
|
/* Now we need to decode the Demon open mode. */
|
1999-04-16 01:35:26 +00:00
|
|
|
flags = translate_open_mode[SWIflags];
|
|
|
|
|
2000-12-03 23:28:46 +00:00
|
|
|
/* Filename ":tt" is special: it denotes stdin/out. */
|
1999-04-16 01:35:26 +00:00
|
|
|
if (strcmp (dummy, ":tt") == 0)
|
|
|
|
{
|
2000-02-05 07:30:26 +00:00
|
|
|
if (flags == O_RDONLY) /* opening tty "r" */
|
|
|
|
state->Reg[0] = 0; /* stdin */
|
|
|
|
else
|
|
|
|
state->Reg[0] = 1; /* stdout */
|
1999-04-16 01:35:26 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1999-10-12 04:37:53 +00:00
|
|
|
state->Reg[0] = (int) open (dummy, flags, 0666);
|
1999-04-16 01:35:26 +00:00
|
|
|
OSptr->ErrorNo = errno;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-02-05 07:30:26 +00:00
|
|
|
static void
|
|
|
|
SWIread (ARMul_State * state, ARMword f, ARMword ptr, ARMword len)
|
1999-04-16 01:35:26 +00:00
|
|
|
{
|
2000-02-05 07:30:26 +00:00
|
|
|
struct OSblock *OSptr = (struct OSblock *) state->OSptr;
|
1999-04-16 01:35:26 +00:00
|
|
|
int res;
|
|
|
|
int i;
|
|
|
|
char *local = malloc (len);
|
|
|
|
|
1999-12-07 03:56:43 +00:00
|
|
|
if (local == NULL)
|
|
|
|
{
|
2000-02-08 20:54:27 +00:00
|
|
|
fprintf (stderr, "sim: Unable to read 0x%ulx bytes - out of memory\n",
|
2000-02-05 07:30:26 +00:00
|
|
|
len);
|
1999-12-07 03:56:43 +00:00
|
|
|
return;
|
|
|
|
}
|
2000-02-05 07:30:26 +00:00
|
|
|
|
1999-04-16 01:35:26 +00:00
|
|
|
res = read (f, local, len);
|
|
|
|
if (res > 0)
|
2000-02-05 07:30:26 +00:00
|
|
|
for (i = 0; i < res; i++)
|
1999-04-16 01:35:26 +00:00
|
|
|
ARMul_WriteByte (state, ptr + i, local[i]);
|
2000-12-03 23:28:46 +00:00
|
|
|
|
1999-04-16 01:35:26 +00:00
|
|
|
free (local);
|
|
|
|
state->Reg[0] = res == -1 ? -1 : len - res;
|
|
|
|
OSptr->ErrorNo = errno;
|
|
|
|
}
|
|
|
|
|
2000-02-05 07:30:26 +00:00
|
|
|
static void
|
|
|
|
SWIwrite (ARMul_State * state, ARMword f, ARMword ptr, ARMword len)
|
1999-04-16 01:35:26 +00:00
|
|
|
{
|
2000-02-05 07:30:26 +00:00
|
|
|
struct OSblock *OSptr = (struct OSblock *) state->OSptr;
|
1999-04-16 01:35:26 +00:00
|
|
|
int res;
|
2000-02-08 20:54:27 +00:00
|
|
|
ARMword i;
|
1999-04-16 01:35:26 +00:00
|
|
|
char *local = malloc (len);
|
|
|
|
|
1999-12-07 03:56:43 +00:00
|
|
|
if (local == NULL)
|
1999-04-16 01:35:26 +00:00
|
|
|
{
|
2000-02-08 20:54:27 +00:00
|
|
|
fprintf (stderr, "sim: Unable to write 0x%lx bytes - out of memory\n",
|
|
|
|
(long) len);
|
1999-12-07 03:56:43 +00:00
|
|
|
return;
|
1999-04-16 01:35:26 +00:00
|
|
|
}
|
2000-02-05 07:30:26 +00:00
|
|
|
|
|
|
|
for (i = 0; i < len; i++)
|
1999-12-07 03:56:43 +00:00
|
|
|
local[i] = ARMul_ReadByte (state, ptr + i);
|
|
|
|
|
1999-04-16 01:35:26 +00:00
|
|
|
res = write (f, local, len);
|
|
|
|
state->Reg[0] = res == -1 ? -1 : len - res;
|
|
|
|
free (local);
|
2000-12-03 23:28:46 +00:00
|
|
|
|
1999-04-16 01:35:26 +00:00
|
|
|
OSptr->ErrorNo = errno;
|
|
|
|
}
|
|
|
|
|
2000-02-05 07:30:26 +00:00
|
|
|
static void
|
|
|
|
SWIflen (ARMul_State * state, ARMword fh)
|
1999-04-16 01:35:26 +00:00
|
|
|
{
|
2000-02-05 07:30:26 +00:00
|
|
|
struct OSblock *OSptr = (struct OSblock *) state->OSptr;
|
1999-04-16 01:35:26 +00:00
|
|
|
ARMword addr;
|
|
|
|
|
|
|
|
if (fh == 0 || fh > FOPEN_MAX)
|
|
|
|
{
|
|
|
|
OSptr->ErrorNo = EBADF;
|
|
|
|
state->Reg[0] = -1L;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
addr = lseek (fh, 0, SEEK_CUR);
|
2000-02-08 20:54:27 +00:00
|
|
|
|
|
|
|
state->Reg[0] = lseek (fh, 0L, SEEK_END);
|
|
|
|
(void) lseek (fh, addr, SEEK_SET);
|
1999-04-16 01:35:26 +00:00
|
|
|
|
|
|
|
OSptr->ErrorNo = errno;
|
|
|
|
}
|
|
|
|
|
2000-12-03 23:28:46 +00:00
|
|
|
/* The emulator calls this routine when a SWI instruction is encuntered.
|
|
|
|
The parameter passed is the SWI number (lower 24 bits of the instruction). */
|
1999-04-16 01:35:26 +00:00
|
|
|
|
2000-02-05 07:30:26 +00:00
|
|
|
unsigned
|
|
|
|
ARMul_OSHandleSWI (ARMul_State * state, ARMword number)
|
1999-04-16 01:35:26 +00:00
|
|
|
{
|
2001-02-14 03:50:46 +00:00
|
|
|
ARMword addr;
|
|
|
|
ARMword temp;
|
|
|
|
ARMword saved_number = 0;
|
|
|
|
struct OSblock * OSptr = (struct OSblock *) state->OSptr;
|
2000-12-03 23:28:46 +00:00
|
|
|
|
2001-02-14 03:50:46 +00:00
|
|
|
in_SWI_handler = 1;
|
|
|
|
|
2000-12-03 23:28:46 +00:00
|
|
|
/* Intel do not want DEMON SWI support. */
|
|
|
|
if (state->is_XScale)
|
|
|
|
switch (number)
|
|
|
|
{
|
|
|
|
case SWI_Read:
|
|
|
|
case SWI_Write:
|
|
|
|
case SWI_Open:
|
|
|
|
case SWI_Clock:
|
|
|
|
case SWI_Time:
|
|
|
|
case SWI_Close:
|
|
|
|
case SWI_Flen:
|
|
|
|
case SWI_Exit:
|
|
|
|
case SWI_Seek:
|
|
|
|
case SWI_WriteC:
|
|
|
|
case SWI_Write0:
|
|
|
|
case SWI_GetErrno:
|
|
|
|
case SWI_GetEnv:
|
|
|
|
saved_number = number;
|
|
|
|
number = -1;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
1999-04-16 01:35:26 +00:00
|
|
|
switch (number)
|
|
|
|
{
|
|
|
|
case SWI_Read:
|
2000-02-05 07:30:26 +00:00
|
|
|
SWIread (state, state->Reg[0], state->Reg[1], state->Reg[2]);
|
2001-02-14 03:50:46 +00:00
|
|
|
break;
|
1999-04-16 01:35:26 +00:00
|
|
|
|
|
|
|
case SWI_Write:
|
2000-02-05 07:30:26 +00:00
|
|
|
SWIwrite (state, state->Reg[0], state->Reg[1], state->Reg[2]);
|
2001-02-14 03:50:46 +00:00
|
|
|
break;
|
1999-04-16 01:35:26 +00:00
|
|
|
|
|
|
|
case SWI_Open:
|
2000-02-05 07:30:26 +00:00
|
|
|
SWIopen (state, state->Reg[0], state->Reg[1]);
|
2001-02-14 03:50:46 +00:00
|
|
|
break;
|
1999-04-16 01:35:26 +00:00
|
|
|
|
2000-02-05 07:30:26 +00:00
|
|
|
case SWI_Clock:
|
2000-12-03 23:28:46 +00:00
|
|
|
/* Return number of centi-seconds. */
|
2000-02-05 07:30:26 +00:00
|
|
|
state->Reg[0] =
|
1999-04-16 01:35:26 +00:00
|
|
|
#ifdef CLOCKS_PER_SEC
|
2000-02-05 07:30:26 +00:00
|
|
|
(CLOCKS_PER_SEC >= 100)
|
|
|
|
? (ARMword) (clock () / (CLOCKS_PER_SEC / 100))
|
|
|
|
: (ARMword) ((clock () * 100) / CLOCKS_PER_SEC);
|
1999-04-16 01:35:26 +00:00
|
|
|
#else
|
2000-12-03 23:28:46 +00:00
|
|
|
/* Presume unix... clock() returns microseconds. */
|
2000-02-05 07:30:26 +00:00
|
|
|
(ARMword) (clock () / 10000);
|
1999-04-16 01:35:26 +00:00
|
|
|
#endif
|
2000-02-05 07:30:26 +00:00
|
|
|
OSptr->ErrorNo = errno;
|
2001-02-14 03:50:46 +00:00
|
|
|
break;
|
2000-02-05 07:30:26 +00:00
|
|
|
|
|
|
|
case SWI_Time:
|
|
|
|
state->Reg[0] = (ARMword) time (NULL);
|
|
|
|
OSptr->ErrorNo = errno;
|
2001-02-14 03:50:46 +00:00
|
|
|
break;
|
2000-02-05 07:30:26 +00:00
|
|
|
|
1999-04-16 01:35:26 +00:00
|
|
|
case SWI_Close:
|
|
|
|
state->Reg[0] = close (state->Reg[0]);
|
|
|
|
OSptr->ErrorNo = errno;
|
2001-02-14 03:50:46 +00:00
|
|
|
break;
|
1999-04-16 01:35:26 +00:00
|
|
|
|
2000-02-05 07:30:26 +00:00
|
|
|
case SWI_Flen:
|
1999-04-16 01:35:26 +00:00
|
|
|
SWIflen (state, state->Reg[0]);
|
2001-02-14 03:50:46 +00:00
|
|
|
break;
|
1999-04-16 01:35:26 +00:00
|
|
|
|
|
|
|
case SWI_Exit:
|
2000-02-05 07:30:26 +00:00
|
|
|
state->Emulate = FALSE;
|
2001-02-14 03:50:46 +00:00
|
|
|
break;
|
1999-04-16 01:35:26 +00:00
|
|
|
|
|
|
|
case SWI_Seek:
|
2001-02-14 03:50:46 +00:00
|
|
|
/* We must return non-zero for failure. */
|
|
|
|
state->Reg[0] = -1 >= lseek (state->Reg[0], state->Reg[1], SEEK_SET);
|
|
|
|
OSptr->ErrorNo = errno;
|
|
|
|
break;
|
1999-04-16 01:35:26 +00:00
|
|
|
|
2000-02-05 07:30:26 +00:00
|
|
|
case SWI_WriteC:
|
|
|
|
(void) fputc ((int) state->Reg[0], stdout);
|
|
|
|
OSptr->ErrorNo = errno;
|
2001-02-14 03:50:46 +00:00
|
|
|
break;
|
1999-04-16 01:35:26 +00:00
|
|
|
|
2000-02-05 07:30:26 +00:00
|
|
|
case SWI_Write0:
|
1999-04-16 01:35:26 +00:00
|
|
|
SWIWrite0 (state, state->Reg[0]);
|
2001-02-14 03:50:46 +00:00
|
|
|
break;
|
1999-04-16 01:35:26 +00:00
|
|
|
|
2000-02-05 07:30:26 +00:00
|
|
|
case SWI_GetErrno:
|
|
|
|
state->Reg[0] = OSptr->ErrorNo;
|
2001-02-14 03:50:46 +00:00
|
|
|
break;
|
1999-04-16 01:35:26 +00:00
|
|
|
|
2000-02-05 07:30:26 +00:00
|
|
|
case SWI_GetEnv:
|
|
|
|
state->Reg[0] = ADDRCMDLINE;
|
|
|
|
if (state->MemSize)
|
|
|
|
state->Reg[1] = state->MemSize;
|
|
|
|
else
|
|
|
|
state->Reg[1] = ADDRUSERSTACK;
|
1999-04-16 01:35:26 +00:00
|
|
|
|
2000-02-05 07:30:26 +00:00
|
|
|
WriteCommandLineTo (state, state->Reg[0]);
|
2001-02-14 03:50:46 +00:00
|
|
|
break;
|
2000-12-03 23:28:46 +00:00
|
|
|
|
2000-11-30 01:55:12 +00:00
|
|
|
case SWI_Breakpoint:
|
|
|
|
state->EndCondition = RDIError_BreakpointReached;
|
|
|
|
state->Emulate = FALSE;
|
2001-02-14 03:50:46 +00:00
|
|
|
break;
|
1999-04-16 01:35:26 +00:00
|
|
|
|
2000-12-03 23:28:46 +00:00
|
|
|
/* Handle Angel SWIs as well as Demon ones. */
|
1999-04-16 01:35:26 +00:00
|
|
|
case AngelSWI_ARM:
|
|
|
|
case AngelSWI_Thumb:
|
2000-12-03 23:28:46 +00:00
|
|
|
/* R1 is almost always a parameter block. */
|
1999-04-16 01:35:26 +00:00
|
|
|
addr = state->Reg[1];
|
2000-12-03 23:28:46 +00:00
|
|
|
/* R0 is a reason code. */
|
1999-04-16 01:35:26 +00:00
|
|
|
switch (state->Reg[0])
|
|
|
|
{
|
2000-12-03 23:28:46 +00:00
|
|
|
/* Unimplemented reason codes. */
|
1999-04-16 01:35:26 +00:00
|
|
|
case AngelSWI_Reason_ReadC:
|
|
|
|
case AngelSWI_Reason_IsTTY:
|
|
|
|
case AngelSWI_Reason_TmpNam:
|
|
|
|
case AngelSWI_Reason_Remove:
|
|
|
|
case AngelSWI_Reason_Rename:
|
|
|
|
case AngelSWI_Reason_System:
|
|
|
|
case AngelSWI_Reason_EnterSVC:
|
|
|
|
default:
|
|
|
|
state->Emulate = FALSE;
|
2001-02-14 03:50:46 +00:00
|
|
|
in_SWI_handler = 0;
|
2000-12-03 23:28:46 +00:00
|
|
|
return FALSE;
|
1999-04-16 01:35:26 +00:00
|
|
|
|
|
|
|
case AngelSWI_Reason_Clock:
|
2000-12-03 23:28:46 +00:00
|
|
|
/* Return number of centi-seconds. */
|
1999-04-16 01:35:26 +00:00
|
|
|
state->Reg[0] =
|
|
|
|
#ifdef CLOCKS_PER_SEC
|
|
|
|
(CLOCKS_PER_SEC >= 100)
|
2000-02-05 07:30:26 +00:00
|
|
|
? (ARMword) (clock () / (CLOCKS_PER_SEC / 100))
|
|
|
|
: (ARMword) ((clock () * 100) / CLOCKS_PER_SEC);
|
1999-04-16 01:35:26 +00:00
|
|
|
#else
|
2000-12-03 23:28:46 +00:00
|
|
|
/* Presume unix... clock() returns microseconds. */
|
2000-02-05 07:30:26 +00:00
|
|
|
(ARMword) (clock () / 10000);
|
1999-04-16 01:35:26 +00:00
|
|
|
#endif
|
|
|
|
OSptr->ErrorNo = errno;
|
2001-02-14 03:50:46 +00:00
|
|
|
break;
|
1999-04-16 01:35:26 +00:00
|
|
|
|
|
|
|
case AngelSWI_Reason_Time:
|
|
|
|
state->Reg[0] = (ARMword) time (NULL);
|
|
|
|
OSptr->ErrorNo = errno;
|
2001-02-14 03:50:46 +00:00
|
|
|
break;
|
1999-04-16 01:35:26 +00:00
|
|
|
|
|
|
|
case AngelSWI_Reason_WriteC:
|
2000-02-05 07:30:26 +00:00
|
|
|
(void) fputc ((int) ARMul_ReadByte (state, addr), stdout);
|
1999-04-16 01:35:26 +00:00
|
|
|
OSptr->ErrorNo = errno;
|
2001-02-14 03:55:57 +00:00
|
|
|
/* Fall thgrough. */
|
1999-04-16 01:35:26 +00:00
|
|
|
|
|
|
|
case AngelSWI_Reason_Write0:
|
|
|
|
SWIWrite0 (state, addr);
|
2001-02-14 03:50:46 +00:00
|
|
|
break;
|
1999-04-16 01:35:26 +00:00
|
|
|
|
|
|
|
case AngelSWI_Reason_Close:
|
|
|
|
state->Reg[0] = close (ARMul_ReadWord (state, addr));
|
|
|
|
OSptr->ErrorNo = errno;
|
2001-02-14 03:50:46 +00:00
|
|
|
break;
|
1999-04-16 01:35:26 +00:00
|
|
|
|
|
|
|
case AngelSWI_Reason_Seek:
|
2000-02-05 07:30:26 +00:00
|
|
|
state->Reg[0] = -1 >= lseek (ARMul_ReadWord (state, addr),
|
|
|
|
ARMul_ReadWord (state, addr + 4),
|
1999-04-16 01:35:26 +00:00
|
|
|
SEEK_SET);
|
|
|
|
OSptr->ErrorNo = errno;
|
2001-02-14 03:50:46 +00:00
|
|
|
break;
|
1999-04-16 01:35:26 +00:00
|
|
|
|
|
|
|
case AngelSWI_Reason_FLen:
|
|
|
|
SWIflen (state, ARMul_ReadWord (state, addr));
|
2001-02-14 03:50:46 +00:00
|
|
|
break;
|
1999-04-16 01:35:26 +00:00
|
|
|
|
2000-02-05 07:30:26 +00:00
|
|
|
case AngelSWI_Reason_GetCmdLine:
|
1999-04-16 01:35:26 +00:00
|
|
|
WriteCommandLineTo (state, ARMul_ReadWord (state, addr));
|
2001-02-14 03:50:46 +00:00
|
|
|
break;
|
1999-04-16 01:35:26 +00:00
|
|
|
|
|
|
|
case AngelSWI_Reason_HeapInfo:
|
2000-12-03 23:28:46 +00:00
|
|
|
/* R1 is a pointer to a pointer. */
|
1999-04-16 01:35:26 +00:00
|
|
|
addr = ARMul_ReadWord (state, addr);
|
|
|
|
|
2000-12-03 23:28:46 +00:00
|
|
|
/* Pick up the right memory limit. */
|
1999-04-16 01:35:26 +00:00
|
|
|
if (state->MemSize)
|
|
|
|
temp = state->MemSize;
|
|
|
|
else
|
|
|
|
temp = ADDRUSERSTACK;
|
|
|
|
|
2000-02-05 07:30:26 +00:00
|
|
|
ARMul_WriteWord (state, addr, 0); /* Heap base */
|
|
|
|
ARMul_WriteWord (state, addr + 4, temp); /* Heap limit */
|
|
|
|
ARMul_WriteWord (state, addr + 8, temp); /* Stack base */
|
|
|
|
ARMul_WriteWord (state, addr + 12, temp); /* Stack limit */
|
2001-02-14 03:50:46 +00:00
|
|
|
break;
|
1999-04-16 01:35:26 +00:00
|
|
|
|
|
|
|
case AngelSWI_Reason_ReportException:
|
|
|
|
if (state->Reg[1] == ADP_Stopped_ApplicationExit)
|
|
|
|
state->Reg[0] = 0;
|
|
|
|
else
|
|
|
|
state->Reg[0] = -1;
|
2000-02-05 07:30:26 +00:00
|
|
|
state->Emulate = FALSE;
|
2001-02-14 03:50:46 +00:00
|
|
|
break;
|
1999-04-16 01:35:26 +00:00
|
|
|
|
|
|
|
case ADP_Stopped_ApplicationExit:
|
|
|
|
state->Reg[0] = 0;
|
2000-02-05 07:30:26 +00:00
|
|
|
state->Emulate = FALSE;
|
2001-02-14 03:50:46 +00:00
|
|
|
break;
|
2000-02-05 07:30:26 +00:00
|
|
|
|
1999-04-16 01:35:26 +00:00
|
|
|
case ADP_Stopped_RunTimeError:
|
|
|
|
state->Reg[0] = -1;
|
2000-02-05 07:30:26 +00:00
|
|
|
state->Emulate = FALSE;
|
2001-02-14 03:50:46 +00:00
|
|
|
break;
|
1999-04-16 01:35:26 +00:00
|
|
|
|
|
|
|
case AngelSWI_Reason_Errno:
|
|
|
|
state->Reg[0] = OSptr->ErrorNo;
|
2001-02-14 03:50:46 +00:00
|
|
|
break;
|
1999-04-16 01:35:26 +00:00
|
|
|
|
|
|
|
case AngelSWI_Reason_Open:
|
2000-02-05 07:30:26 +00:00
|
|
|
SWIopen (state,
|
|
|
|
ARMul_ReadWord (state, addr),
|
|
|
|
ARMul_ReadWord (state, addr + 4));
|
2001-02-14 03:50:46 +00:00
|
|
|
break;
|
1999-04-16 01:35:26 +00:00
|
|
|
|
|
|
|
case AngelSWI_Reason_Read:
|
2000-02-05 07:30:26 +00:00
|
|
|
SWIread (state,
|
|
|
|
ARMul_ReadWord (state, addr),
|
|
|
|
ARMul_ReadWord (state, addr + 4),
|
|
|
|
ARMul_ReadWord (state, addr + 8));
|
2001-02-14 03:50:46 +00:00
|
|
|
break;
|
1999-04-16 01:35:26 +00:00
|
|
|
|
|
|
|
case AngelSWI_Reason_Write:
|
2000-02-05 07:30:26 +00:00
|
|
|
SWIwrite (state,
|
|
|
|
ARMul_ReadWord (state, addr),
|
|
|
|
ARMul_ReadWord (state, addr + 4),
|
|
|
|
ARMul_ReadWord (state, addr + 8));
|
2001-02-14 03:50:46 +00:00
|
|
|
break;
|
1999-04-16 01:35:26 +00:00
|
|
|
}
|
|
|
|
|
2000-11-30 01:55:12 +00:00
|
|
|
case 0x90:
|
2000-12-11 03:08:17 +00:00
|
|
|
case 0x91:
|
2000-11-30 01:55:12 +00:00
|
|
|
case 0x92:
|
|
|
|
/* These are used by the FPE code. */
|
2001-02-14 03:50:46 +00:00
|
|
|
break;
|
2000-11-30 01:55:12 +00:00
|
|
|
|
2000-02-05 07:30:26 +00:00
|
|
|
default:
|
2001-02-14 03:50:46 +00:00
|
|
|
in_SWI_handler = 0;
|
2001-02-14 03:55:57 +00:00
|
|
|
|
2001-02-14 03:50:46 +00:00
|
|
|
/* If there is a SWI vector installed use it. */
|
|
|
|
if (state->is_XScale && saved_number != -1)
|
|
|
|
number = saved_number;
|
2000-12-03 23:28:46 +00:00
|
|
|
|
2001-02-14 03:50:46 +00:00
|
|
|
if (SWI_vector_installed && number != SWI_Breakpoint)
|
|
|
|
{
|
|
|
|
ARMword cpsr;
|
|
|
|
ARMword i_size;
|
2001-02-14 03:55:57 +00:00
|
|
|
|
2001-02-14 03:50:46 +00:00
|
|
|
cpsr = ARMul_GetCPSR (state);
|
|
|
|
i_size = INSN_SIZE;
|
2001-02-14 03:55:57 +00:00
|
|
|
|
2001-02-14 03:50:46 +00:00
|
|
|
ARMul_SetSPSR (state, SVC32MODE, cpsr);
|
2001-02-14 03:55:57 +00:00
|
|
|
|
2001-02-14 03:50:46 +00:00
|
|
|
cpsr &= ~0xbf;
|
|
|
|
cpsr |= SVC32MODE | 0x80;
|
|
|
|
ARMul_SetCPSR (state, cpsr);
|
2001-02-14 03:55:57 +00:00
|
|
|
|
2001-02-14 03:50:46 +00:00
|
|
|
state->RegBank[SVCBANK][14] = state->Reg[14] = state->Reg[15] - i_size;
|
|
|
|
state->NextInstr = RESUME;
|
|
|
|
state->Reg[15] = state->pc = ARMSWIV;
|
|
|
|
FLUSHPIPE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-11-30 01:55:12 +00:00
|
|
|
fprintf (stderr, "unknown SWI encountered - %x - ignoring\n", number);
|
2001-02-14 03:50:46 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
1999-04-16 01:35:26 +00:00
|
|
|
}
|
2001-02-14 03:50:46 +00:00
|
|
|
|
|
|
|
in_SWI_handler = 0;
|
|
|
|
return TRUE;
|
1999-04-16 01:35:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef NOOS
|
|
|
|
#ifndef ASIM
|
|
|
|
|
2000-11-30 01:55:12 +00:00
|
|
|
/* The emulator calls this routine when an Exception occurs. The second
|
|
|
|
parameter is the address of the relevant exception vector. Returning
|
|
|
|
FALSE from this routine causes the trap to be taken, TRUE causes it to
|
|
|
|
be ignored (so set state->Emulate to FALSE!). */
|
1999-04-16 01:35:26 +00:00
|
|
|
|
2000-02-05 07:30:26 +00:00
|
|
|
unsigned
|
2000-12-03 23:28:46 +00:00
|
|
|
ARMul_OSException (ARMul_State * state ATTRIBUTE_UNUSED,
|
2000-11-30 01:55:12 +00:00
|
|
|
ARMword vector ATTRIBUTE_UNUSED,
|
|
|
|
ARMword pc ATTRIBUTE_UNUSED)
|
|
|
|
{
|
2001-02-14 03:50:46 +00:00
|
|
|
/* If we are inside a SWI handler routine, then ignore any exceptions.
|
|
|
|
They could be caused by data exceptions for misaligned reads, for
|
|
|
|
example, but for the purposes of emulating a SWI, we do not care. */
|
|
|
|
|
|
|
|
return in_SWI_handler;
|
1999-04-16 01:35:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
#endif /* NOOS */
|