armos.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  1. /* armos.c -- ARMulator OS interface: ARM6 Instruction Emulator.
  2. Copyright (C) 1994 Advanced RISC Machines Ltd.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, see <http://www.gnu.org/licenses/>. */
  13. /* This file contains a model of Demon, ARM Ltd's Debug Monitor,
  14. including all the SWI's required to support the C library. The code in
  15. it is not really for the faint-hearted (especially the abort handling
  16. code), but it is a complete example. Defining NOOS will disable all the
  17. fun, and definign VAILDATE will define SWI 1 to enter SVC mode, and SWI
  18. 0x11 to halt the emulator. */
  19. /* This must come before any other includes. */
  20. #include "defs.h"
  21. #include "ansidecl.h"
  22. #include "libiberty.h"
  23. #include <time.h>
  24. #include <errno.h>
  25. #include <limits.h>
  26. #include <string.h>
  27. #ifdef HAVE_UNISTD_H
  28. #include <unistd.h> /* For SEEK_SET etc. */
  29. #endif
  30. #include "armdefs.h"
  31. #include "armos.h"
  32. #include "armemu.h"
  33. #ifndef NOOS
  34. #ifndef VALIDATE
  35. /* #ifndef ASIM */
  36. #include "armfpe.h"
  37. /* #endif */
  38. #endif
  39. #endif
  40. /* For RDIError_BreakpointReached. */
  41. #include "dbg_rdi.h"
  42. #include "sim/callback.h"
  43. extern host_callback *sim_callback;
  44. extern unsigned ARMul_OSInit (ARMul_State *);
  45. extern unsigned ARMul_OSHandleSWI (ARMul_State *, ARMword);
  46. #ifndef FOPEN_MAX
  47. #define FOPEN_MAX 64
  48. #endif
  49. #ifndef PATH_MAX
  50. #define PATH_MAX 1024
  51. #endif
  52. /* OS private Information. */
  53. struct OSblock
  54. {
  55. ARMword ErrorNo;
  56. };
  57. /* Bit mask of enabled SWI implementations. */
  58. unsigned int swi_mask = -1;
  59. static ARMword softvectorcode[] =
  60. {
  61. /* Installed instructions:
  62. swi tidyexception + event;
  63. mov lr, pc;
  64. ldmia fp, {fp, pc};
  65. swi generateexception + event. */
  66. 0xef000090, 0xe1a0e00f, 0xe89b8800, 0xef000080, /* Reset */
  67. 0xef000091, 0xe1a0e00f, 0xe89b8800, 0xef000081, /* Undef */
  68. 0xef000092, 0xe1a0e00f, 0xe89b8800, 0xef000082, /* SWI */
  69. 0xef000093, 0xe1a0e00f, 0xe89b8800, 0xef000083, /* Prefetch abort */
  70. 0xef000094, 0xe1a0e00f, 0xe89b8800, 0xef000084, /* Data abort */
  71. 0xef000095, 0xe1a0e00f, 0xe89b8800, 0xef000085, /* Address exception */
  72. 0xef000096, 0xe1a0e00f, 0xe89b8800, 0xef000086, /* IRQ */
  73. 0xef000097, 0xe1a0e00f, 0xe89b8800, 0xef000087, /* FIQ */
  74. 0xef000098, 0xe1a0e00f, 0xe89b8800, 0xef000088, /* Error */
  75. 0xe1a0f00e /* Default handler */
  76. };
  77. /* Time for the Operating System to initialise itself. */
  78. unsigned
  79. ARMul_OSInit (ARMul_State * state)
  80. {
  81. #ifndef NOOS
  82. #ifndef VALIDATE
  83. ARMword instr, i, j;
  84. struct OSblock *OSptr = (struct OSblock *) state->OSptr;
  85. if (state->OSptr == NULL)
  86. {
  87. state->OSptr = (unsigned char *) malloc (sizeof (struct OSblock));
  88. if (state->OSptr == NULL)
  89. {
  90. perror ("OS Memory");
  91. exit (15);
  92. }
  93. }
  94. OSptr = (struct OSblock *) state->OSptr;
  95. state->Reg[13] = ADDRSUPERSTACK; /* Set up a stack for the current mode... */
  96. ARMul_SetReg (state, SVC32MODE, 13, ADDRSUPERSTACK);/* ...and for supervisor mode... */
  97. ARMul_SetReg (state, ABORT32MODE, 13, ADDRSUPERSTACK);/* ...and for abort 32 mode... */
  98. ARMul_SetReg (state, UNDEF32MODE, 13, ADDRSUPERSTACK);/* ...and for undef 32 mode... */
  99. ARMul_SetReg (state, SYSTEMMODE, 13, ADDRSUPERSTACK);/* ...and for system mode. */
  100. instr = 0xe59ff000 | (ADDRSOFTVECTORS - 8); /* Load pc from soft vector */
  101. for (i = ARMul_ResetV; i <= ARMFIQV; i += 4)
  102. /* Write hardware vectors. */
  103. ARMul_WriteWord (state, i, instr);
  104. SWI_vector_installed = 0;
  105. for (i = ARMul_ResetV; i <= ARMFIQV + 4; i += 4)
  106. {
  107. ARMul_WriteWord (state, ADDRSOFTVECTORS + i, SOFTVECTORCODE + i * 4);
  108. ARMul_WriteWord (state, ADDRSOFHANDLERS + 2 * i + 4L,
  109. SOFTVECTORCODE + sizeof (softvectorcode) - 4L);
  110. }
  111. for (i = 0; i < sizeof (softvectorcode); i += 4)
  112. ARMul_WriteWord (state, SOFTVECTORCODE + i, softvectorcode[i / 4]);
  113. ARMul_ConsolePrint (state, ", Demon 1.01");
  114. /* #ifndef ASIM */
  115. /* Install FPE. */
  116. for (i = 0; i < fpesize; i += 4)
  117. /* Copy the code. */
  118. ARMul_WriteWord (state, FPESTART + i, fpecode[i >> 2]);
  119. /* Scan backwards from the end of the code. */
  120. for (i = FPESTART + fpesize;; i -= 4)
  121. {
  122. /* When we reach the marker value, break out of
  123. the loop, leaving i pointing at the maker. */
  124. if ((j = ARMul_ReadWord (state, i)) == 0xffffffff)
  125. break;
  126. /* If necessary, reverse the error strings. */
  127. if (state->bigendSig && j < 0x80000000)
  128. {
  129. /* It's part of the string so swap it. */
  130. j = ((j >> 0x18) & 0x000000ff) |
  131. ((j >> 0x08) & 0x0000ff00) |
  132. ((j << 0x08) & 0x00ff0000) | ((j << 0x18) & 0xff000000);
  133. ARMul_WriteWord (state, i, j);
  134. }
  135. }
  136. /* Copy old illegal instr vector. */
  137. ARMul_WriteWord (state, FPEOLDVECT, ARMul_ReadWord (state, ARMUndefinedInstrV));
  138. /* Install new vector. */
  139. ARMul_WriteWord (state, ARMUndefinedInstrV, FPENEWVECT (ARMul_ReadWord (state, i - 4)));
  140. ARMul_ConsolePrint (state, ", FPE");
  141. /* #endif ASIM */
  142. #endif /* VALIDATE */
  143. #endif /* NOOS */
  144. /* Intel do not want DEMON SWI support. */
  145. if (state->is_XScale)
  146. swi_mask = SWI_MASK_ANGEL;
  147. return TRUE;
  148. }
  149. /* These are libgloss defines, but seem to be common across all supported ARM
  150. targets at the moment. These should get moved to the callback open_map. */
  151. #define TARGET_O_BINARY 0
  152. #define TARGET_O_APPEND 0x8
  153. #define TARGET_O_CREAT 0x200
  154. #define TARGET_O_RDONLY 0x0
  155. #define TARGET_O_RDWR 0x2
  156. #define TARGET_O_TRUNC 0x400
  157. #define TARGET_O_WRONLY 0x1
  158. static const int translate_open_mode[] =
  159. {
  160. TARGET_O_RDONLY, /* "r" */
  161. TARGET_O_RDONLY + TARGET_O_BINARY, /* "rb" */
  162. TARGET_O_RDWR, /* "r+" */
  163. TARGET_O_RDWR + TARGET_O_BINARY, /* "r+b" */
  164. TARGET_O_WRONLY + TARGET_O_CREAT + TARGET_O_TRUNC, /* "w" */
  165. TARGET_O_WRONLY + TARGET_O_BINARY + TARGET_O_CREAT + TARGET_O_TRUNC, /* "wb" */
  166. TARGET_O_RDWR + TARGET_O_CREAT + TARGET_O_TRUNC, /* "w+" */
  167. TARGET_O_RDWR + TARGET_O_BINARY + TARGET_O_CREAT + TARGET_O_TRUNC, /* "w+b" */
  168. TARGET_O_WRONLY + TARGET_O_APPEND + TARGET_O_CREAT, /* "a" */
  169. TARGET_O_WRONLY + TARGET_O_BINARY + TARGET_O_APPEND + TARGET_O_CREAT, /* "ab" */
  170. TARGET_O_RDWR + TARGET_O_APPEND + TARGET_O_CREAT, /* "a+" */
  171. TARGET_O_RDWR + TARGET_O_BINARY + TARGET_O_APPEND + TARGET_O_CREAT /* "a+b" */
  172. };
  173. static void
  174. SWIWrite0 (ARMul_State * state, ARMword addr)
  175. {
  176. ARMword temp;
  177. struct OSblock *OSptr = (struct OSblock *) state->OSptr;
  178. while ((temp = ARMul_SafeReadByte (state, addr++)) != 0)
  179. {
  180. char buffer = temp;
  181. /* Note - we cannot just cast 'temp' to a (char *) here,
  182. since on a big-endian host the byte value will end
  183. up in the wrong place and a nul character will be printed. */
  184. (void) sim_callback->write_stdout (sim_callback, & buffer, 1);
  185. }
  186. OSptr->ErrorNo = sim_callback->get_errno (sim_callback);
  187. }
  188. static void
  189. WriteCommandLineTo (ARMul_State * state, ARMword addr)
  190. {
  191. ARMword temp;
  192. char *cptr = state->CommandLine;
  193. if (cptr == NULL)
  194. cptr = "\0";
  195. do
  196. {
  197. temp = (ARMword) * cptr++;
  198. ARMul_SafeWriteByte (state, addr++, temp);
  199. }
  200. while (temp != 0);
  201. }
  202. static int
  203. ReadFileName (ARMul_State * state, char *buf, ARMword src, size_t n)
  204. {
  205. struct OSblock *OSptr = (struct OSblock *) state->OSptr;
  206. char *p = buf;
  207. while (n--)
  208. if ((*p++ = ARMul_SafeReadByte (state, src++)) == '\0')
  209. return 0;
  210. OSptr->ErrorNo = cb_host_to_target_errno (sim_callback, ENAMETOOLONG);
  211. state->Reg[0] = -1;
  212. return -1;
  213. }
  214. static void
  215. SWIopen (ARMul_State * state, ARMword name, ARMword SWIflags)
  216. {
  217. struct OSblock *OSptr = (struct OSblock *) state->OSptr;
  218. char buf[PATH_MAX];
  219. int flags;
  220. if (ReadFileName (state, buf, name, sizeof buf) == -1)
  221. return;
  222. /* Now we need to decode the Demon open mode. */
  223. if (SWIflags >= ARRAY_SIZE (translate_open_mode))
  224. flags = 0;
  225. else
  226. flags = translate_open_mode[SWIflags];
  227. /* Filename ":tt" is special: it denotes stdin/out. */
  228. if (strcmp (buf, ":tt") == 0)
  229. {
  230. if (flags == TARGET_O_RDONLY) /* opening tty "r" */
  231. state->Reg[0] = 0; /* stdin */
  232. else
  233. state->Reg[0] = 1; /* stdout */
  234. }
  235. else
  236. {
  237. state->Reg[0] = sim_callback->open (sim_callback, buf, flags);
  238. OSptr->ErrorNo = sim_callback->get_errno (sim_callback);
  239. }
  240. }
  241. static void
  242. SWIread (ARMul_State * state, ARMword f, ARMword ptr, ARMword len)
  243. {
  244. struct OSblock *OSptr = (struct OSblock *) state->OSptr;
  245. int res;
  246. int i;
  247. char *local = malloc (len);
  248. if (local == NULL)
  249. {
  250. sim_callback->printf_filtered
  251. (sim_callback,
  252. "sim: Unable to read 0x%lx bytes - out of memory\n",
  253. (long)len);
  254. return;
  255. }
  256. res = sim_callback->read (sim_callback, f, local, len);
  257. if (res > 0)
  258. for (i = 0; i < res; i++)
  259. ARMul_SafeWriteByte (state, ptr + i, local[i]);
  260. free (local);
  261. state->Reg[0] = res == -1 ? -1 : len - res;
  262. OSptr->ErrorNo = sim_callback->get_errno (sim_callback);
  263. }
  264. static void
  265. SWIwrite (ARMul_State * state, ARMword f, ARMword ptr, ARMword len)
  266. {
  267. struct OSblock *OSptr = (struct OSblock *) state->OSptr;
  268. int res;
  269. ARMword i;
  270. char *local = malloc (len);
  271. if (local == NULL)
  272. {
  273. sim_callback->printf_filtered
  274. (sim_callback,
  275. "sim: Unable to write 0x%lx bytes - out of memory\n",
  276. (long) len);
  277. return;
  278. }
  279. for (i = 0; i < len; i++)
  280. local[i] = ARMul_SafeReadByte (state, ptr + i);
  281. res = sim_callback->write (sim_callback, f, local, len);
  282. state->Reg[0] = res == -1 ? -1 : len - res;
  283. free (local);
  284. OSptr->ErrorNo = sim_callback->get_errno (sim_callback);
  285. }
  286. static void
  287. SWIflen (ARMul_State * state, ARMword fh)
  288. {
  289. struct OSblock *OSptr = (struct OSblock *) state->OSptr;
  290. ARMword addr;
  291. if (fh > FOPEN_MAX)
  292. {
  293. OSptr->ErrorNo = EBADF;
  294. state->Reg[0] = -1L;
  295. return;
  296. }
  297. addr = sim_callback->lseek (sim_callback, fh, 0, SEEK_CUR);
  298. state->Reg[0] = sim_callback->lseek (sim_callback, fh, 0L, SEEK_END);
  299. (void) sim_callback->lseek (sim_callback, fh, addr, SEEK_SET);
  300. OSptr->ErrorNo = sim_callback->get_errno (sim_callback);
  301. }
  302. static void
  303. SWIremove (ARMul_State * state, ARMword path)
  304. {
  305. char buf[PATH_MAX];
  306. if (ReadFileName (state, buf, path, sizeof buf) != -1)
  307. {
  308. struct OSblock *OSptr = (struct OSblock *) state->OSptr;
  309. state->Reg[0] = sim_callback->unlink (sim_callback, buf);
  310. OSptr->ErrorNo = sim_callback->get_errno (sim_callback);
  311. }
  312. }
  313. static void
  314. SWIrename (ARMul_State * state, ARMword old, ARMword new)
  315. {
  316. char oldbuf[PATH_MAX], newbuf[PATH_MAX];
  317. if (ReadFileName (state, oldbuf, old, sizeof oldbuf) != -1
  318. && ReadFileName (state, newbuf, new, sizeof newbuf) != -1)
  319. {
  320. struct OSblock *OSptr = (struct OSblock *) state->OSptr;
  321. state->Reg[0] = sim_callback->rename (sim_callback, oldbuf, newbuf);
  322. OSptr->ErrorNo = sim_callback->get_errno (sim_callback);
  323. }
  324. }
  325. /* The emulator calls this routine when a SWI instruction is encuntered.
  326. The parameter passed is the SWI number (lower 24 bits of the instruction). */
  327. unsigned
  328. ARMul_OSHandleSWI (ARMul_State * state, ARMword number)
  329. {
  330. struct OSblock * OSptr = (struct OSblock *) state->OSptr;
  331. int unhandled = FALSE;
  332. switch (number)
  333. {
  334. case SWI_Read:
  335. if (swi_mask & SWI_MASK_DEMON)
  336. SWIread (state, state->Reg[0], state->Reg[1], state->Reg[2]);
  337. else
  338. unhandled = TRUE;
  339. break;
  340. case SWI_Write:
  341. if (swi_mask & SWI_MASK_DEMON)
  342. SWIwrite (state, state->Reg[0], state->Reg[1], state->Reg[2]);
  343. else
  344. unhandled = TRUE;
  345. break;
  346. case SWI_Open:
  347. if (swi_mask & SWI_MASK_DEMON)
  348. SWIopen (state, state->Reg[0], state->Reg[1]);
  349. else
  350. unhandled = TRUE;
  351. break;
  352. case SWI_Clock:
  353. if (swi_mask & SWI_MASK_DEMON)
  354. {
  355. /* Return number of centi-seconds. */
  356. state->Reg[0] =
  357. #ifdef CLOCKS_PER_SEC
  358. (CLOCKS_PER_SEC >= 100)
  359. ? (ARMword) (clock () / (CLOCKS_PER_SEC / 100))
  360. : (ARMword) ((clock () * 100) / CLOCKS_PER_SEC);
  361. #else
  362. /* Presume unix... clock() returns microseconds. */
  363. (ARMword) (clock () / 10000);
  364. #endif
  365. OSptr->ErrorNo = errno;
  366. }
  367. else
  368. unhandled = TRUE;
  369. break;
  370. case SWI_Time:
  371. if (swi_mask & SWI_MASK_DEMON)
  372. {
  373. state->Reg[0] = (ARMword) sim_callback->time (sim_callback);
  374. OSptr->ErrorNo = sim_callback->get_errno (sim_callback);
  375. }
  376. else
  377. unhandled = TRUE;
  378. break;
  379. case SWI_Close:
  380. if (swi_mask & SWI_MASK_DEMON)
  381. {
  382. state->Reg[0] = sim_callback->close (sim_callback, state->Reg[0]);
  383. OSptr->ErrorNo = sim_callback->get_errno (sim_callback);
  384. }
  385. else
  386. unhandled = TRUE;
  387. break;
  388. case SWI_Flen:
  389. if (swi_mask & SWI_MASK_DEMON)
  390. SWIflen (state, state->Reg[0]);
  391. else
  392. unhandled = TRUE;
  393. break;
  394. case SWI_Exit:
  395. if (swi_mask & SWI_MASK_DEMON)
  396. state->Emulate = FALSE;
  397. else
  398. unhandled = TRUE;
  399. break;
  400. case SWI_Seek:
  401. if (swi_mask & SWI_MASK_DEMON)
  402. {
  403. /* We must return non-zero for failure. */
  404. state->Reg[0] = -1 >= sim_callback->lseek (sim_callback, state->Reg[0], state->Reg[1], SEEK_SET);
  405. OSptr->ErrorNo = sim_callback->get_errno (sim_callback);
  406. }
  407. else
  408. unhandled = TRUE;
  409. break;
  410. case SWI_WriteC:
  411. if (swi_mask & SWI_MASK_DEMON)
  412. {
  413. char tmp = state->Reg[0];
  414. (void) sim_callback->write_stdout (sim_callback, &tmp, 1);
  415. OSptr->ErrorNo = sim_callback->get_errno (sim_callback);
  416. }
  417. else
  418. unhandled = TRUE;
  419. break;
  420. case SWI_Write0:
  421. if (swi_mask & SWI_MASK_DEMON)
  422. SWIWrite0 (state, state->Reg[0]);
  423. else
  424. unhandled = TRUE;
  425. break;
  426. case SWI_GetErrno:
  427. if (swi_mask & SWI_MASK_DEMON)
  428. state->Reg[0] = OSptr->ErrorNo;
  429. else
  430. unhandled = TRUE;
  431. break;
  432. case SWI_GetEnv:
  433. if (swi_mask & SWI_MASK_DEMON)
  434. {
  435. state->Reg[0] = ADDRCMDLINE;
  436. if (state->MemSize)
  437. state->Reg[1] = state->MemSize;
  438. else
  439. state->Reg[1] = ADDRUSERSTACK;
  440. WriteCommandLineTo (state, state->Reg[0]);
  441. }
  442. else
  443. unhandled = TRUE;
  444. break;
  445. case SWI_Breakpoint:
  446. state->EndCondition = RDIError_BreakpointReached;
  447. state->Emulate = FALSE;
  448. break;
  449. case SWI_Remove:
  450. if (swi_mask & SWI_MASK_DEMON)
  451. SWIremove (state, state->Reg[0]);
  452. else
  453. unhandled = TRUE;
  454. break;
  455. case SWI_Rename:
  456. if (swi_mask & SWI_MASK_DEMON)
  457. SWIrename (state, state->Reg[0], state->Reg[1]);
  458. else
  459. unhandled = TRUE;
  460. break;
  461. case SWI_IsTTY:
  462. if (swi_mask & SWI_MASK_DEMON)
  463. {
  464. state->Reg[0] = sim_callback->isatty (sim_callback, state->Reg[0]);
  465. OSptr->ErrorNo = sim_callback->get_errno (sim_callback);
  466. }
  467. else
  468. unhandled = TRUE;
  469. break;
  470. /* Handle Angel SWIs as well as Demon ones. */
  471. case AngelSWI_ARM:
  472. case AngelSWI_Thumb:
  473. if (swi_mask & SWI_MASK_ANGEL)
  474. {
  475. ARMword addr;
  476. ARMword temp;
  477. /* R1 is almost always a parameter block. */
  478. addr = state->Reg[1];
  479. /* R0 is a reason code. */
  480. switch (state->Reg[0])
  481. {
  482. case -1:
  483. /* This can happen when a SWI is interrupted (eg receiving a
  484. ctrl-C whilst processing SWIRead()). The SWI will complete
  485. returning -1 in r0 to the caller. If GDB is then used to
  486. resume the system call the reason code will now be -1. */
  487. return TRUE;
  488. /* Unimplemented reason codes. */
  489. case AngelSWI_Reason_ReadC:
  490. case AngelSWI_Reason_TmpNam:
  491. case AngelSWI_Reason_System:
  492. case AngelSWI_Reason_EnterSVC:
  493. default:
  494. state->Emulate = FALSE;
  495. return FALSE;
  496. case AngelSWI_Reason_Clock:
  497. /* Return number of centi-seconds. */
  498. state->Reg[0] =
  499. #ifdef CLOCKS_PER_SEC
  500. (CLOCKS_PER_SEC >= 100)
  501. ? (ARMword) (clock () / (CLOCKS_PER_SEC / 100))
  502. : (ARMword) ((clock () * 100) / CLOCKS_PER_SEC);
  503. #else
  504. /* Presume unix... clock() returns microseconds. */
  505. (ARMword) (clock () / 10000);
  506. #endif
  507. OSptr->ErrorNo = errno;
  508. break;
  509. case AngelSWI_Reason_Time:
  510. state->Reg[0] = (ARMword) sim_callback->time (sim_callback);
  511. OSptr->ErrorNo = sim_callback->get_errno (sim_callback);
  512. break;
  513. case AngelSWI_Reason_WriteC:
  514. {
  515. char tmp = ARMul_SafeReadByte (state, addr);
  516. (void) sim_callback->write_stdout (sim_callback, &tmp, 1);
  517. OSptr->ErrorNo = sim_callback->get_errno (sim_callback);
  518. break;
  519. }
  520. case AngelSWI_Reason_Write0:
  521. SWIWrite0 (state, addr);
  522. break;
  523. case AngelSWI_Reason_Close:
  524. state->Reg[0] = sim_callback->close (sim_callback, ARMul_ReadWord (state, addr));
  525. OSptr->ErrorNo = sim_callback->get_errno (sim_callback);
  526. break;
  527. case AngelSWI_Reason_Seek:
  528. state->Reg[0] = -1 >= sim_callback->lseek (sim_callback, ARMul_ReadWord (state, addr),
  529. ARMul_ReadWord (state, addr + 4),
  530. SEEK_SET);
  531. OSptr->ErrorNo = sim_callback->get_errno (sim_callback);
  532. break;
  533. case AngelSWI_Reason_FLen:
  534. SWIflen (state, ARMul_ReadWord (state, addr));
  535. break;
  536. case AngelSWI_Reason_GetCmdLine:
  537. WriteCommandLineTo (state, ARMul_ReadWord (state, addr));
  538. break;
  539. case AngelSWI_Reason_HeapInfo:
  540. /* R1 is a pointer to a pointer. */
  541. addr = ARMul_ReadWord (state, addr);
  542. /* Pick up the right memory limit. */
  543. if (state->MemSize)
  544. temp = state->MemSize;
  545. else
  546. temp = ADDRUSERSTACK;
  547. ARMul_WriteWord (state, addr, 0); /* Heap base. */
  548. ARMul_WriteWord (state, addr + 4, temp); /* Heap limit. */
  549. ARMul_WriteWord (state, addr + 8, temp); /* Stack base. */
  550. ARMul_WriteWord (state, addr + 12, temp); /* Stack limit. */
  551. break;
  552. case AngelSWI_Reason_ReportException:
  553. if (state->Reg[1] == ADP_Stopped_ApplicationExit)
  554. state->Reg[0] = 0;
  555. else
  556. state->Reg[0] = -1;
  557. state->Emulate = FALSE;
  558. break;
  559. case ADP_Stopped_ApplicationExit:
  560. state->Reg[0] = 0;
  561. state->Emulate = FALSE;
  562. break;
  563. case ADP_Stopped_RunTimeError:
  564. state->Reg[0] = -1;
  565. state->Emulate = FALSE;
  566. break;
  567. case AngelSWI_Reason_Errno:
  568. state->Reg[0] = OSptr->ErrorNo;
  569. break;
  570. case AngelSWI_Reason_Open:
  571. SWIopen (state,
  572. ARMul_ReadWord (state, addr),
  573. ARMul_ReadWord (state, addr + 4));
  574. break;
  575. case AngelSWI_Reason_Read:
  576. SWIread (state,
  577. ARMul_ReadWord (state, addr),
  578. ARMul_ReadWord (state, addr + 4),
  579. ARMul_ReadWord (state, addr + 8));
  580. break;
  581. case AngelSWI_Reason_Write:
  582. SWIwrite (state,
  583. ARMul_ReadWord (state, addr),
  584. ARMul_ReadWord (state, addr + 4),
  585. ARMul_ReadWord (state, addr + 8));
  586. break;
  587. case AngelSWI_Reason_IsTTY:
  588. state->Reg[0] = sim_callback->isatty (sim_callback,
  589. ARMul_ReadWord (state, addr));
  590. OSptr->ErrorNo = sim_callback->get_errno (sim_callback);
  591. break;
  592. case AngelSWI_Reason_Remove:
  593. SWIremove (state,
  594. ARMul_ReadWord (state, addr));
  595. case AngelSWI_Reason_Rename:
  596. SWIrename (state,
  597. ARMul_ReadWord (state, addr),
  598. ARMul_ReadWord (state, addr + 4));
  599. }
  600. }
  601. else
  602. unhandled = TRUE;
  603. break;
  604. /* The following SWIs are generated by the softvectorcode[]
  605. installed by default by the simulator. */
  606. case 0x91: /* Undefined Instruction. */
  607. {
  608. ARMword addr = state->RegBank[UNDEFBANK][14] - 4;
  609. sim_callback->printf_filtered
  610. (sim_callback, "sim: exception: Unhandled Instruction '0x%08x' at 0x%08x. Stopping.\n",
  611. ARMul_ReadWord (state, addr), addr);
  612. state->EndCondition = RDIError_SoftwareInterrupt;
  613. state->Emulate = FALSE;
  614. return FALSE;
  615. }
  616. case 0x90: /* Reset. */
  617. case 0x92: /* SWI. */
  618. /* These two can be safely ignored. */
  619. break;
  620. case 0x93: /* Prefetch Abort. */
  621. case 0x94: /* Data Abort. */
  622. case 0x95: /* Address Exception. */
  623. case 0x96: /* IRQ. */
  624. case 0x97: /* FIQ. */
  625. case 0x98: /* Error. */
  626. unhandled = TRUE;
  627. break;
  628. case -1:
  629. /* This can happen when a SWI is interrupted (eg receiving a
  630. ctrl-C whilst processing SWIRead()). The SWI will complete
  631. returning -1 in r0 to the caller. If GDB is then used to
  632. resume the system call the reason code will now be -1. */
  633. return TRUE;
  634. case 0x180001: /* RedBoot's Syscall SWI in ARM mode. */
  635. if (swi_mask & SWI_MASK_REDBOOT)
  636. {
  637. switch (state->Reg[0])
  638. {
  639. /* These numbers are defined in libgloss/syscall.h
  640. but the simulator should not be dependend upon
  641. libgloss being installed. */
  642. case 1: /* Exit. */
  643. state->Emulate = FALSE;
  644. /* Copy exit code into r0. */
  645. state->Reg[0] = state->Reg[1];
  646. break;
  647. case 2: /* Open. */
  648. SWIopen (state, state->Reg[1], state->Reg[2]);
  649. break;
  650. case 3: /* Close. */
  651. state->Reg[0] = sim_callback->close (sim_callback, state->Reg[1]);
  652. OSptr->ErrorNo = sim_callback->get_errno (sim_callback);
  653. break;
  654. case 4: /* Read. */
  655. SWIread (state, state->Reg[1], state->Reg[2], state->Reg[3]);
  656. break;
  657. case 5: /* Write. */
  658. SWIwrite (state, state->Reg[1], state->Reg[2], state->Reg[3]);
  659. break;
  660. case 6: /* Lseek. */
  661. state->Reg[0] = sim_callback->lseek (sim_callback,
  662. state->Reg[1],
  663. state->Reg[2],
  664. state->Reg[3]);
  665. OSptr->ErrorNo = sim_callback->get_errno (sim_callback);
  666. break;
  667. case 17: /* Utime. */
  668. state->Reg[0] = state->Reg[1] = (ARMword) sim_callback->time (sim_callback);
  669. OSptr->ErrorNo = sim_callback->get_errno (sim_callback);
  670. break;
  671. case 7: /* Unlink. */
  672. case 8: /* Getpid. */
  673. case 9: /* Kill. */
  674. case 10: /* Fstat. */
  675. case 11: /* Sbrk. */
  676. case 12: /* Argvlen. */
  677. case 13: /* Argv. */
  678. case 14: /* ChDir. */
  679. case 15: /* Stat. */
  680. case 16: /* Chmod. */
  681. case 18: /* Time. */
  682. sim_callback->printf_filtered
  683. (sim_callback,
  684. "sim: unhandled RedBoot syscall `%d' encountered - "
  685. "returning ENOSYS\n",
  686. state->Reg[0]);
  687. state->Reg[0] = -1;
  688. OSptr->ErrorNo = cb_host_to_target_errno
  689. (sim_callback, ENOSYS);
  690. break;
  691. case 1001: /* Meminfo. */
  692. {
  693. ARMword totmem = state->Reg[1],
  694. topmem = state->Reg[2];
  695. ARMword stack = state->MemSize > 0
  696. ? state->MemSize : ADDRUSERSTACK;
  697. if (totmem != 0)
  698. ARMul_WriteWord (state, totmem, stack);
  699. if (topmem != 0)
  700. ARMul_WriteWord (state, topmem, stack);
  701. state->Reg[0] = 0;
  702. break;
  703. }
  704. default:
  705. sim_callback->printf_filtered
  706. (sim_callback,
  707. "sim: unknown RedBoot syscall '%d' encountered - ignoring\n",
  708. state->Reg[0]);
  709. return FALSE;
  710. }
  711. break;
  712. }
  713. default:
  714. unhandled = TRUE;
  715. }
  716. if (unhandled)
  717. {
  718. if (SWI_vector_installed)
  719. {
  720. ARMword cpsr;
  721. ARMword i_size;
  722. cpsr = ARMul_GetCPSR (state);
  723. i_size = INSN_SIZE;
  724. ARMul_SetSPSR (state, SVC32MODE, cpsr);
  725. cpsr &= ~0xbf;
  726. cpsr |= SVC32MODE | 0x80;
  727. ARMul_SetCPSR (state, cpsr);
  728. state->RegBank[SVCBANK][14] = state->Reg[14] = state->Reg[15] - i_size;
  729. state->NextInstr = RESUME;
  730. state->Reg[15] = state->pc = ARMSWIV;
  731. FLUSHPIPE;
  732. }
  733. else
  734. {
  735. sim_callback->printf_filtered
  736. (sim_callback,
  737. "sim: unknown SWI encountered - %x - ignoring\n",
  738. number);
  739. return FALSE;
  740. }
  741. }
  742. return TRUE;
  743. }