traps.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337
  1. /* m32r exception, interrupt, and trap (EIT) support
  2. Copyright (C) 1998-2022 Free Software Foundation, Inc.
  3. Contributed by Cygnus Solutions & Renesas.
  4. This file is part of GDB, the GNU debugger.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /* This must come before any other includes. */
  16. #include "defs.h"
  17. #include "portability.h"
  18. #include "sim-main.h"
  19. #include "sim-signal.h"
  20. #include "sim-syscall.h"
  21. #include "sim/callback.h"
  22. #include "syscall.h"
  23. #include <dirent.h>
  24. #include <errno.h>
  25. #include <fcntl.h>
  26. #include <stdlib.h>
  27. #include <time.h>
  28. #include <unistd.h>
  29. #include <utime.h>
  30. /* TODO: The Linux syscall emulation needs work to support non-Linux hosts.
  31. Use an OS hack for now so the CPU emulation is available everywhere.
  32. NB: The emulation is also missing argument conversion (endian & bitsize)
  33. even on Linux hosts. */
  34. #ifdef __linux__
  35. #include <sys/mman.h>
  36. #include <sys/poll.h>
  37. #include <sys/resource.h>
  38. #include <sys/sysinfo.h>
  39. #include <sys/stat.h>
  40. #include <sys/time.h>
  41. #include <sys/timeb.h>
  42. #include <sys/timex.h>
  43. #include <sys/types.h>
  44. #include <sys/uio.h>
  45. #include <sys/utsname.h>
  46. #include <sys/vfs.h>
  47. #include <linux/sysctl.h>
  48. #include <linux/types.h>
  49. #include <linux/unistd.h>
  50. #endif
  51. #define TRAP_LINUX_SYSCALL 2
  52. #define TRAP_FLUSH_CACHE 12
  53. /* The semantic code invokes this for invalid (unrecognized) instructions. */
  54. SEM_PC
  55. sim_engine_invalid_insn (SIM_CPU *current_cpu, IADDR cia, SEM_PC pc)
  56. {
  57. SIM_DESC sd = CPU_STATE (current_cpu);
  58. #if 0
  59. if (STATE_ENVIRONMENT (sd) == OPERATING_ENVIRONMENT)
  60. {
  61. h_bsm_set (current_cpu, h_sm_get (current_cpu));
  62. h_bie_set (current_cpu, h_ie_get (current_cpu));
  63. h_bcond_set (current_cpu, h_cond_get (current_cpu));
  64. /* sm not changed */
  65. h_ie_set (current_cpu, 0);
  66. h_cond_set (current_cpu, 0);
  67. h_bpc_set (current_cpu, cia);
  68. sim_engine_restart (CPU_STATE (current_cpu), current_cpu, NULL,
  69. EIT_RSVD_INSN_ADDR);
  70. }
  71. else
  72. #endif
  73. sim_engine_halt (sd, current_cpu, NULL, cia, sim_stopped, SIM_SIGILL);
  74. return pc;
  75. }
  76. /* Process an address exception. */
  77. void
  78. m32r_core_signal (SIM_DESC sd, SIM_CPU *current_cpu, sim_cia cia,
  79. unsigned int map, int nr_bytes, address_word addr,
  80. transfer_type transfer, sim_core_signals sig)
  81. {
  82. if (STATE_ENVIRONMENT (sd) == OPERATING_ENVIRONMENT)
  83. {
  84. m32rbf_h_cr_set (current_cpu, H_CR_BBPC,
  85. m32rbf_h_cr_get (current_cpu, H_CR_BPC));
  86. switch (MACH_NUM (CPU_MACH (current_cpu)))
  87. {
  88. case MACH_M32R:
  89. m32rbf_h_bpsw_set (current_cpu, m32rbf_h_psw_get (current_cpu));
  90. /* sm not changed. */
  91. m32rbf_h_psw_set (current_cpu, m32rbf_h_psw_get (current_cpu) & 0x80);
  92. break;
  93. case MACH_M32RX:
  94. m32rxf_h_bpsw_set (current_cpu, m32rxf_h_psw_get (current_cpu));
  95. /* sm not changed. */
  96. m32rxf_h_psw_set (current_cpu, m32rxf_h_psw_get (current_cpu) & 0x80);
  97. break;
  98. case MACH_M32R2:
  99. m32r2f_h_bpsw_set (current_cpu, m32r2f_h_psw_get (current_cpu));
  100. /* sm not changed. */
  101. m32r2f_h_psw_set (current_cpu, m32r2f_h_psw_get (current_cpu) & 0x80);
  102. break;
  103. default:
  104. abort ();
  105. }
  106. m32rbf_h_cr_set (current_cpu, H_CR_BPC, cia);
  107. sim_engine_restart (CPU_STATE (current_cpu), current_cpu, NULL,
  108. EIT_ADDR_EXCP_ADDR);
  109. }
  110. else
  111. sim_core_signal (sd, current_cpu, cia, map, nr_bytes, addr,
  112. transfer, sig);
  113. }
  114. /* Translate target's address to host's address. */
  115. static void *
  116. t2h_addr (host_callback *cb, struct cb_syscall *sc,
  117. unsigned long taddr)
  118. {
  119. void *addr;
  120. SIM_DESC sd = (SIM_DESC) sc->p1;
  121. SIM_CPU *cpu = (SIM_CPU *) sc->p2;
  122. if (taddr == 0)
  123. return NULL;
  124. return sim_core_trans_addr (sd, cpu, read_map, taddr);
  125. }
  126. /* TODO: These functions are a big hack and assume that the host runtime has
  127. type sizes and struct layouts that match the target. So the Linux emulation
  128. probaly only really works in 32-bit runtimes. */
  129. static void
  130. translate_endian_h2t (void *addr, size_t size)
  131. {
  132. unsigned int *p = (unsigned int *) addr;
  133. int i;
  134. for (i = 0; i <= size - 4; i += 4,p++)
  135. *p = H2T_4 (*p);
  136. if (i <= size - 2)
  137. *((unsigned short *) p) = H2T_2 (*((unsigned short *) p));
  138. }
  139. static void
  140. translate_endian_t2h (void *addr, size_t size)
  141. {
  142. unsigned int *p = (unsigned int *) addr;
  143. int i;
  144. for (i = 0; i <= size - 4; i += 4,p++)
  145. *p = T2H_4 (*p);
  146. if (i <= size - 2)
  147. *((unsigned short *) p) = T2H_2 (*((unsigned short *) p));
  148. }
  149. /* Trap support.
  150. The result is the pc address to continue at.
  151. Preprocessing like saving the various registers has already been done. */
  152. USI
  153. m32r_trap (SIM_CPU *current_cpu, PCADDR pc, int num)
  154. {
  155. SIM_DESC sd = CPU_STATE (current_cpu);
  156. host_callback *cb = STATE_CALLBACK (sd);
  157. if (STATE_ENVIRONMENT (sd) == OPERATING_ENVIRONMENT)
  158. goto case_default;
  159. switch (num)
  160. {
  161. case TRAP_SYSCALL:
  162. {
  163. long result, result2;
  164. int errcode;
  165. sim_syscall_multi (current_cpu,
  166. m32rbf_h_gr_get (current_cpu, 0),
  167. m32rbf_h_gr_get (current_cpu, 1),
  168. m32rbf_h_gr_get (current_cpu, 2),
  169. m32rbf_h_gr_get (current_cpu, 3),
  170. m32rbf_h_gr_get (current_cpu, 4),
  171. &result, &result2, &errcode);
  172. m32rbf_h_gr_set (current_cpu, 2, errcode);
  173. m32rbf_h_gr_set (current_cpu, 0, result);
  174. m32rbf_h_gr_set (current_cpu, 1, result2);
  175. break;
  176. }
  177. #ifdef __linux__
  178. case TRAP_LINUX_SYSCALL:
  179. {
  180. CB_SYSCALL s;
  181. unsigned int func, arg1, arg2, arg3, arg4, arg5, arg6, arg7;
  182. int result, result2, errcode;
  183. if (STATE_ENVIRONMENT (sd) != USER_ENVIRONMENT)
  184. goto case_default;
  185. func = m32rbf_h_gr_get (current_cpu, 7);
  186. arg1 = m32rbf_h_gr_get (current_cpu, 0);
  187. arg2 = m32rbf_h_gr_get (current_cpu, 1);
  188. arg3 = m32rbf_h_gr_get (current_cpu, 2);
  189. arg4 = m32rbf_h_gr_get (current_cpu, 3);
  190. arg5 = m32rbf_h_gr_get (current_cpu, 4);
  191. arg6 = m32rbf_h_gr_get (current_cpu, 5);
  192. arg7 = m32rbf_h_gr_get (current_cpu, 6);
  193. CB_SYSCALL_INIT (&s);
  194. s.func = func;
  195. s.arg1 = arg1;
  196. s.arg2 = arg2;
  197. s.arg3 = arg3;
  198. s.arg4 = arg4;
  199. s.arg5 = arg5;
  200. s.arg6 = arg6;
  201. s.arg7 = arg7;
  202. s.p1 = (PTR) sd;
  203. s.p2 = (PTR) current_cpu;
  204. s.read_mem = sim_syscall_read_mem;
  205. s.write_mem = sim_syscall_write_mem;
  206. result = 0;
  207. result2 = 0;
  208. errcode = 0;
  209. switch (func)
  210. {
  211. case TARGET_LINUX_SYS_exit:
  212. sim_engine_halt (sd, current_cpu, NULL, pc, sim_exited, arg1);
  213. break;
  214. case TARGET_LINUX_SYS_read:
  215. result = read (arg1, t2h_addr (cb, &s, arg2), arg3);
  216. errcode = errno;
  217. break;
  218. case TARGET_LINUX_SYS_write:
  219. result = write (arg1, t2h_addr (cb, &s, arg2), arg3);
  220. errcode = errno;
  221. break;
  222. case TARGET_LINUX_SYS_open:
  223. result = open ((char *) t2h_addr (cb, &s, arg1), arg2, arg3);
  224. errcode = errno;
  225. break;
  226. case TARGET_LINUX_SYS_close:
  227. result = close (arg1);
  228. errcode = errno;
  229. break;
  230. case TARGET_LINUX_SYS_creat:
  231. result = creat ((char *) t2h_addr (cb, &s, arg1), arg2);
  232. errcode = errno;
  233. break;
  234. case TARGET_LINUX_SYS_link:
  235. result = link ((char *) t2h_addr (cb, &s, arg1),
  236. (char *) t2h_addr (cb, &s, arg2));
  237. errcode = errno;
  238. break;
  239. case TARGET_LINUX_SYS_unlink:
  240. result = unlink ((char *) t2h_addr (cb, &s, arg1));
  241. errcode = errno;
  242. break;
  243. case TARGET_LINUX_SYS_chdir:
  244. result = chdir ((char *) t2h_addr (cb, &s, arg1));
  245. errcode = errno;
  246. break;
  247. case TARGET_LINUX_SYS_time:
  248. {
  249. time_t t;
  250. if (arg1 == 0)
  251. {
  252. result = (int) time (NULL);
  253. errcode = errno;
  254. }
  255. else
  256. {
  257. result = (int) time (&t);
  258. errcode = errno;
  259. if (result != 0)
  260. break;
  261. t = H2T_4 (t);
  262. if ((s.write_mem) (cb, &s, arg1, (char *) &t, sizeof(t)) != sizeof(t))
  263. {
  264. result = -1;
  265. errcode = EINVAL;
  266. }
  267. }
  268. }
  269. break;
  270. case TARGET_LINUX_SYS_mknod:
  271. result = mknod ((char *) t2h_addr (cb, &s, arg1),
  272. (mode_t) arg2, (dev_t) arg3);
  273. errcode = errno;
  274. break;
  275. case TARGET_LINUX_SYS_chmod:
  276. result = chmod ((char *) t2h_addr (cb, &s, arg1), (mode_t) arg2);
  277. errcode = errno;
  278. break;
  279. case TARGET_LINUX_SYS_lchown32:
  280. case TARGET_LINUX_SYS_lchown:
  281. result = lchown ((char *) t2h_addr (cb, &s, arg1),
  282. (uid_t) arg2, (gid_t) arg3);
  283. errcode = errno;
  284. break;
  285. case TARGET_LINUX_SYS_lseek:
  286. result = (int) lseek (arg1, (off_t) arg2, arg3);
  287. errcode = errno;
  288. break;
  289. case TARGET_LINUX_SYS_getpid:
  290. result = getpid ();
  291. errcode = errno;
  292. break;
  293. case TARGET_LINUX_SYS_getuid32:
  294. case TARGET_LINUX_SYS_getuid:
  295. result = getuid ();
  296. errcode = errno;
  297. break;
  298. case TARGET_LINUX_SYS_utime:
  299. {
  300. struct utimbuf buf;
  301. if (arg2 == 0)
  302. {
  303. result = utime ((char *) t2h_addr (cb, &s, arg1), NULL);
  304. errcode = errno;
  305. }
  306. else
  307. {
  308. buf = *((struct utimbuf *) t2h_addr (cb, &s, arg2));
  309. translate_endian_t2h (&buf, sizeof(buf));
  310. result = utime ((char *) t2h_addr (cb, &s, arg1), &buf);
  311. errcode = errno;
  312. }
  313. }
  314. break;
  315. case TARGET_LINUX_SYS_access:
  316. result = access ((char *) t2h_addr (cb, &s, arg1), arg2);
  317. errcode = errno;
  318. break;
  319. case TARGET_LINUX_SYS_ftime:
  320. {
  321. struct timeb t;
  322. result = ftime (&t);
  323. errcode = errno;
  324. if (result != 0)
  325. break;
  326. t.time = H2T_4 (t.time);
  327. t.millitm = H2T_2 (t.millitm);
  328. t.timezone = H2T_2 (t.timezone);
  329. t.dstflag = H2T_2 (t.dstflag);
  330. if ((s.write_mem) (cb, &s, arg1, (char *) &t, sizeof(t))
  331. != sizeof(t))
  332. {
  333. result = -1;
  334. errcode = EINVAL;
  335. }
  336. }
  337. case TARGET_LINUX_SYS_sync:
  338. sync ();
  339. result = 0;
  340. break;
  341. case TARGET_LINUX_SYS_rename:
  342. result = rename ((char *) t2h_addr (cb, &s, arg1),
  343. (char *) t2h_addr (cb, &s, arg2));
  344. errcode = errno;
  345. break;
  346. case TARGET_LINUX_SYS_mkdir:
  347. result = mkdir ((char *) t2h_addr (cb, &s, arg1), arg2);
  348. errcode = errno;
  349. break;
  350. case TARGET_LINUX_SYS_rmdir:
  351. result = rmdir ((char *) t2h_addr (cb, &s, arg1));
  352. errcode = errno;
  353. break;
  354. case TARGET_LINUX_SYS_dup:
  355. result = dup (arg1);
  356. errcode = errno;
  357. break;
  358. case TARGET_LINUX_SYS_brk:
  359. result = brk ((void *) arg1);
  360. errcode = errno;
  361. //result = arg1;
  362. break;
  363. case TARGET_LINUX_SYS_getgid32:
  364. case TARGET_LINUX_SYS_getgid:
  365. result = getgid ();
  366. errcode = errno;
  367. break;
  368. case TARGET_LINUX_SYS_geteuid32:
  369. case TARGET_LINUX_SYS_geteuid:
  370. result = geteuid ();
  371. errcode = errno;
  372. break;
  373. case TARGET_LINUX_SYS_getegid32:
  374. case TARGET_LINUX_SYS_getegid:
  375. result = getegid ();
  376. errcode = errno;
  377. break;
  378. case TARGET_LINUX_SYS_ioctl:
  379. result = ioctl (arg1, arg2, arg3);
  380. errcode = errno;
  381. break;
  382. case TARGET_LINUX_SYS_fcntl:
  383. result = fcntl (arg1, arg2, arg3);
  384. errcode = errno;
  385. break;
  386. case TARGET_LINUX_SYS_dup2:
  387. result = dup2 (arg1, arg2);
  388. errcode = errno;
  389. break;
  390. case TARGET_LINUX_SYS_getppid:
  391. result = getppid ();
  392. errcode = errno;
  393. break;
  394. case TARGET_LINUX_SYS_getpgrp:
  395. result = getpgrp ();
  396. errcode = errno;
  397. break;
  398. case TARGET_LINUX_SYS_getrlimit:
  399. {
  400. struct rlimit rlim;
  401. result = getrlimit (arg1, &rlim);
  402. errcode = errno;
  403. if (result != 0)
  404. break;
  405. translate_endian_h2t (&rlim, sizeof(rlim));
  406. if ((s.write_mem) (cb, &s, arg2, (char *) &rlim, sizeof(rlim))
  407. != sizeof(rlim))
  408. {
  409. result = -1;
  410. errcode = EINVAL;
  411. }
  412. }
  413. break;
  414. case TARGET_LINUX_SYS_getrusage:
  415. {
  416. struct rusage usage;
  417. result = getrusage (arg1, &usage);
  418. errcode = errno;
  419. if (result != 0)
  420. break;
  421. translate_endian_h2t (&usage, sizeof(usage));
  422. if ((s.write_mem) (cb, &s, arg2, (char *) &usage, sizeof(usage))
  423. != sizeof(usage))
  424. {
  425. result = -1;
  426. errcode = EINVAL;
  427. }
  428. }
  429. break;
  430. case TARGET_LINUX_SYS_gettimeofday:
  431. {
  432. struct timeval tv;
  433. struct timezone tz;
  434. result = gettimeofday (&tv, &tz);
  435. errcode = errno;
  436. if (result != 0)
  437. break;
  438. translate_endian_h2t (&tv, sizeof(tv));
  439. if ((s.write_mem) (cb, &s, arg1, (char *) &tv, sizeof(tv))
  440. != sizeof(tv))
  441. {
  442. result = -1;
  443. errcode = EINVAL;
  444. }
  445. translate_endian_h2t (&tz, sizeof(tz));
  446. if ((s.write_mem) (cb, &s, arg2, (char *) &tz, sizeof(tz))
  447. != sizeof(tz))
  448. {
  449. result = -1;
  450. errcode = EINVAL;
  451. }
  452. }
  453. break;
  454. case TARGET_LINUX_SYS_getgroups32:
  455. case TARGET_LINUX_SYS_getgroups:
  456. {
  457. gid_t *list;
  458. if (arg1 > 0)
  459. list = (gid_t *) malloc (arg1 * sizeof(gid_t));
  460. result = getgroups (arg1, list);
  461. errcode = errno;
  462. if (result != 0)
  463. break;
  464. translate_endian_h2t (list, arg1 * sizeof(gid_t));
  465. if (arg1 > 0)
  466. if ((s.write_mem) (cb, &s, arg2, (char *) list, arg1 * sizeof(gid_t))
  467. != arg1 * sizeof(gid_t))
  468. {
  469. result = -1;
  470. errcode = EINVAL;
  471. }
  472. }
  473. break;
  474. case TARGET_LINUX_SYS_select:
  475. {
  476. int n;
  477. fd_set readfds;
  478. fd_set *treadfdsp;
  479. fd_set *hreadfdsp;
  480. fd_set writefds;
  481. fd_set *twritefdsp;
  482. fd_set *hwritefdsp;
  483. fd_set exceptfds;
  484. fd_set *texceptfdsp;
  485. fd_set *hexceptfdsp;
  486. struct timeval *ttimeoutp;
  487. struct timeval timeout;
  488. n = arg1;
  489. treadfdsp = (fd_set *) arg2;
  490. if (treadfdsp != NULL)
  491. {
  492. readfds = *((fd_set *) t2h_addr (cb, &s, (unsigned int) treadfdsp));
  493. translate_endian_t2h (&readfds, sizeof(readfds));
  494. hreadfdsp = &readfds;
  495. }
  496. else
  497. hreadfdsp = NULL;
  498. twritefdsp = (fd_set *) arg3;
  499. if (twritefdsp != NULL)
  500. {
  501. writefds = *((fd_set *) t2h_addr (cb, &s, (unsigned int) twritefdsp));
  502. translate_endian_t2h (&writefds, sizeof(writefds));
  503. hwritefdsp = &writefds;
  504. }
  505. else
  506. hwritefdsp = NULL;
  507. texceptfdsp = (fd_set *) arg4;
  508. if (texceptfdsp != NULL)
  509. {
  510. exceptfds = *((fd_set *) t2h_addr (cb, &s, (unsigned int) texceptfdsp));
  511. translate_endian_t2h (&exceptfds, sizeof(exceptfds));
  512. hexceptfdsp = &exceptfds;
  513. }
  514. else
  515. hexceptfdsp = NULL;
  516. ttimeoutp = (struct timeval *) arg5;
  517. timeout = *((struct timeval *) t2h_addr (cb, &s, (unsigned int) ttimeoutp));
  518. translate_endian_t2h (&timeout, sizeof(timeout));
  519. result = select (n, hreadfdsp, hwritefdsp, hexceptfdsp, &timeout);
  520. errcode = errno;
  521. if (result != 0)
  522. break;
  523. if (treadfdsp != NULL)
  524. {
  525. translate_endian_h2t (&readfds, sizeof(readfds));
  526. if ((s.write_mem) (cb, &s, (unsigned long) treadfdsp,
  527. (char *) &readfds, sizeof(readfds)) != sizeof(readfds))
  528. {
  529. result = -1;
  530. errcode = EINVAL;
  531. }
  532. }
  533. if (twritefdsp != NULL)
  534. {
  535. translate_endian_h2t (&writefds, sizeof(writefds));
  536. if ((s.write_mem) (cb, &s, (unsigned long) twritefdsp,
  537. (char *) &writefds, sizeof(writefds)) != sizeof(writefds))
  538. {
  539. result = -1;
  540. errcode = EINVAL;
  541. }
  542. }
  543. if (texceptfdsp != NULL)
  544. {
  545. translate_endian_h2t (&exceptfds, sizeof(exceptfds));
  546. if ((s.write_mem) (cb, &s, (unsigned long) texceptfdsp,
  547. (char *) &exceptfds, sizeof(exceptfds)) != sizeof(exceptfds))
  548. {
  549. result = -1;
  550. errcode = EINVAL;
  551. }
  552. }
  553. translate_endian_h2t (&timeout, sizeof(timeout));
  554. if ((s.write_mem) (cb, &s, (unsigned long) ttimeoutp,
  555. (char *) &timeout, sizeof(timeout)) != sizeof(timeout))
  556. {
  557. result = -1;
  558. errcode = EINVAL;
  559. }
  560. }
  561. break;
  562. case TARGET_LINUX_SYS_symlink:
  563. result = symlink ((char *) t2h_addr (cb, &s, arg1),
  564. (char *) t2h_addr (cb, &s, arg2));
  565. errcode = errno;
  566. break;
  567. case TARGET_LINUX_SYS_readlink:
  568. result = readlink ((char *) t2h_addr (cb, &s, arg1),
  569. (char *) t2h_addr (cb, &s, arg2),
  570. arg3);
  571. errcode = errno;
  572. break;
  573. case TARGET_LINUX_SYS_readdir:
  574. result = (int) readdir ((DIR *) t2h_addr (cb, &s, arg1));
  575. errcode = errno;
  576. break;
  577. #if 0
  578. case TARGET_LINUX_SYS_mmap:
  579. {
  580. result = (int) mmap ((void *) t2h_addr (cb, &s, arg1),
  581. arg2, arg3, arg4, arg5, arg6);
  582. errcode = errno;
  583. if (errno == 0)
  584. {
  585. sim_core_attach (sd, NULL,
  586. 0, access_read_write_exec, 0,
  587. result, arg2, 0, NULL, NULL);
  588. }
  589. }
  590. break;
  591. #endif
  592. case TARGET_LINUX_SYS_mmap2:
  593. {
  594. void *addr;
  595. size_t len;
  596. int prot, flags, fildes;
  597. off_t off;
  598. addr = (void *) t2h_addr (cb, &s, arg1);
  599. len = arg2;
  600. prot = arg3;
  601. flags = arg4;
  602. fildes = arg5;
  603. off = arg6 << 12;
  604. result = (int) mmap (addr, len, prot, flags, fildes, off);
  605. errcode = errno;
  606. if (result != -1)
  607. {
  608. char c;
  609. if (sim_core_read_buffer (sd, NULL, read_map, &c, result, 1) == 0)
  610. sim_core_attach (sd, NULL,
  611. 0, access_read_write_exec, 0,
  612. result, len, 0, NULL, NULL);
  613. }
  614. }
  615. break;
  616. case TARGET_LINUX_SYS_mmap:
  617. {
  618. void *addr;
  619. size_t len;
  620. int prot, flags, fildes;
  621. off_t off;
  622. addr = *((void **) t2h_addr (cb, &s, arg1));
  623. len = *((size_t *) t2h_addr (cb, &s, arg1 + 4));
  624. prot = *((int *) t2h_addr (cb, &s, arg1 + 8));
  625. flags = *((int *) t2h_addr (cb, &s, arg1 + 12));
  626. fildes = *((int *) t2h_addr (cb, &s, arg1 + 16));
  627. off = *((off_t *) t2h_addr (cb, &s, arg1 + 20));
  628. addr = (void *) T2H_4 ((unsigned int) addr);
  629. len = T2H_4 (len);
  630. prot = T2H_4 (prot);
  631. flags = T2H_4 (flags);
  632. fildes = T2H_4 (fildes);
  633. off = T2H_4 (off);
  634. //addr = (void *) t2h_addr (cb, &s, (unsigned int) addr);
  635. result = (int) mmap (addr, len, prot, flags, fildes, off);
  636. errcode = errno;
  637. //if (errno == 0)
  638. if (result != -1)
  639. {
  640. char c;
  641. if (sim_core_read_buffer (sd, NULL, read_map, &c, result, 1) == 0)
  642. sim_core_attach (sd, NULL,
  643. 0, access_read_write_exec, 0,
  644. result, len, 0, NULL, NULL);
  645. }
  646. }
  647. break;
  648. case TARGET_LINUX_SYS_munmap:
  649. result = munmap ((void *)arg1, arg2);
  650. errcode = errno;
  651. if (result != -1)
  652. sim_core_detach (sd, NULL, 0, arg2, result);
  653. break;
  654. case TARGET_LINUX_SYS_truncate:
  655. result = truncate ((char *) t2h_addr (cb, &s, arg1), arg2);
  656. errcode = errno;
  657. break;
  658. case TARGET_LINUX_SYS_ftruncate:
  659. result = ftruncate (arg1, arg2);
  660. errcode = errno;
  661. break;
  662. case TARGET_LINUX_SYS_fchmod:
  663. result = fchmod (arg1, arg2);
  664. errcode = errno;
  665. break;
  666. case TARGET_LINUX_SYS_fchown32:
  667. case TARGET_LINUX_SYS_fchown:
  668. result = fchown (arg1, arg2, arg3);
  669. errcode = errno;
  670. break;
  671. case TARGET_LINUX_SYS_statfs:
  672. {
  673. struct statfs statbuf;
  674. result = statfs ((char *) t2h_addr (cb, &s, arg1), &statbuf);
  675. errcode = errno;
  676. if (result != 0)
  677. break;
  678. translate_endian_h2t (&statbuf, sizeof(statbuf));
  679. if ((s.write_mem) (cb, &s, arg2, (char *) &statbuf, sizeof(statbuf))
  680. != sizeof(statbuf))
  681. {
  682. result = -1;
  683. errcode = EINVAL;
  684. }
  685. }
  686. break;
  687. case TARGET_LINUX_SYS_fstatfs:
  688. {
  689. struct statfs statbuf;
  690. result = fstatfs (arg1, &statbuf);
  691. errcode = errno;
  692. if (result != 0)
  693. break;
  694. translate_endian_h2t (&statbuf, sizeof(statbuf));
  695. if ((s.write_mem) (cb, &s, arg2, (char *) &statbuf, sizeof(statbuf))
  696. != sizeof(statbuf))
  697. {
  698. result = -1;
  699. errcode = EINVAL;
  700. }
  701. }
  702. break;
  703. case TARGET_LINUX_SYS_syslog:
  704. result = syslog (arg1, (char *) t2h_addr (cb, &s, arg2));
  705. errcode = errno;
  706. break;
  707. case TARGET_LINUX_SYS_setitimer:
  708. {
  709. struct itimerval value, ovalue;
  710. value = *((struct itimerval *) t2h_addr (cb, &s, arg2));
  711. translate_endian_t2h (&value, sizeof(value));
  712. if (arg2 == 0)
  713. {
  714. result = setitimer (arg1, &value, NULL);
  715. errcode = errno;
  716. }
  717. else
  718. {
  719. result = setitimer (arg1, &value, &ovalue);
  720. errcode = errno;
  721. if (result != 0)
  722. break;
  723. translate_endian_h2t (&ovalue, sizeof(ovalue));
  724. if ((s.write_mem) (cb, &s, arg3, (char *) &ovalue, sizeof(ovalue))
  725. != sizeof(ovalue))
  726. {
  727. result = -1;
  728. errcode = EINVAL;
  729. }
  730. }
  731. }
  732. break;
  733. case TARGET_LINUX_SYS_getitimer:
  734. {
  735. struct itimerval value;
  736. result = getitimer (arg1, &value);
  737. errcode = errno;
  738. if (result != 0)
  739. break;
  740. translate_endian_h2t (&value, sizeof(value));
  741. if ((s.write_mem) (cb, &s, arg2, (char *) &value, sizeof(value))
  742. != sizeof(value))
  743. {
  744. result = -1;
  745. errcode = EINVAL;
  746. }
  747. }
  748. break;
  749. case TARGET_LINUX_SYS_stat:
  750. {
  751. char *buf;
  752. int buflen;
  753. struct stat statbuf;
  754. result = stat ((char *) t2h_addr (cb, &s, arg1), &statbuf);
  755. errcode = errno;
  756. if (result < 0)
  757. break;
  758. buflen = cb_host_to_target_stat (cb, NULL, NULL);
  759. buf = xmalloc (buflen);
  760. if (cb_host_to_target_stat (cb, &statbuf, buf) != buflen)
  761. {
  762. /* The translation failed. This is due to an internal
  763. host program error, not the target's fault. */
  764. free (buf);
  765. result = -1;
  766. errcode = ENOSYS;
  767. break;
  768. }
  769. if ((s.write_mem) (cb, &s, arg2, buf, buflen) != buflen)
  770. {
  771. free (buf);
  772. result = -1;
  773. errcode = EINVAL;
  774. break;
  775. }
  776. free (buf);
  777. }
  778. break;
  779. case TARGET_LINUX_SYS_lstat:
  780. {
  781. char *buf;
  782. int buflen;
  783. struct stat statbuf;
  784. result = lstat ((char *) t2h_addr (cb, &s, arg1), &statbuf);
  785. errcode = errno;
  786. if (result < 0)
  787. break;
  788. buflen = cb_host_to_target_stat (cb, NULL, NULL);
  789. buf = xmalloc (buflen);
  790. if (cb_host_to_target_stat (cb, &statbuf, buf) != buflen)
  791. {
  792. /* The translation failed. This is due to an internal
  793. host program error, not the target's fault. */
  794. free (buf);
  795. result = -1;
  796. errcode = ENOSYS;
  797. break;
  798. }
  799. if ((s.write_mem) (cb, &s, arg2, buf, buflen) != buflen)
  800. {
  801. free (buf);
  802. result = -1;
  803. errcode = EINVAL;
  804. break;
  805. }
  806. free (buf);
  807. }
  808. break;
  809. case TARGET_LINUX_SYS_fstat:
  810. {
  811. char *buf;
  812. int buflen;
  813. struct stat statbuf;
  814. result = fstat (arg1, &statbuf);
  815. errcode = errno;
  816. if (result < 0)
  817. break;
  818. buflen = cb_host_to_target_stat (cb, NULL, NULL);
  819. buf = xmalloc (buflen);
  820. if (cb_host_to_target_stat (cb, &statbuf, buf) != buflen)
  821. {
  822. /* The translation failed. This is due to an internal
  823. host program error, not the target's fault. */
  824. free (buf);
  825. result = -1;
  826. errcode = ENOSYS;
  827. break;
  828. }
  829. if ((s.write_mem) (cb, &s, arg2, buf, buflen) != buflen)
  830. {
  831. free (buf);
  832. result = -1;
  833. errcode = EINVAL;
  834. break;
  835. }
  836. free (buf);
  837. }
  838. break;
  839. case TARGET_LINUX_SYS_sysinfo:
  840. {
  841. struct sysinfo info;
  842. result = sysinfo (&info);
  843. errcode = errno;
  844. if (result != 0)
  845. break;
  846. info.uptime = H2T_4 (info.uptime);
  847. info.loads[0] = H2T_4 (info.loads[0]);
  848. info.loads[1] = H2T_4 (info.loads[1]);
  849. info.loads[2] = H2T_4 (info.loads[2]);
  850. info.totalram = H2T_4 (info.totalram);
  851. info.freeram = H2T_4 (info.freeram);
  852. info.sharedram = H2T_4 (info.sharedram);
  853. info.bufferram = H2T_4 (info.bufferram);
  854. info.totalswap = H2T_4 (info.totalswap);
  855. info.freeswap = H2T_4 (info.freeswap);
  856. info.procs = H2T_2 (info.procs);
  857. #if LINUX_VERSION_CODE >= 0x20400
  858. info.totalhigh = H2T_4 (info.totalhigh);
  859. info.freehigh = H2T_4 (info.freehigh);
  860. info.mem_unit = H2T_4 (info.mem_unit);
  861. #endif
  862. if ((s.write_mem) (cb, &s, arg1, (char *) &info, sizeof(info))
  863. != sizeof(info))
  864. {
  865. result = -1;
  866. errcode = EINVAL;
  867. }
  868. }
  869. break;
  870. #if 0
  871. case TARGET_LINUX_SYS_ipc:
  872. {
  873. result = ipc (arg1, arg2, arg3, arg4,
  874. (void *) t2h_addr (cb, &s, arg5), arg6);
  875. errcode = errno;
  876. }
  877. break;
  878. #endif
  879. case TARGET_LINUX_SYS_fsync:
  880. result = fsync (arg1);
  881. errcode = errno;
  882. break;
  883. case TARGET_LINUX_SYS_uname:
  884. /* utsname contains only arrays of char, so it is not necessary
  885. to translate endian. */
  886. result = uname ((struct utsname *) t2h_addr (cb, &s, arg1));
  887. errcode = errno;
  888. break;
  889. case TARGET_LINUX_SYS_adjtimex:
  890. {
  891. struct timex buf;
  892. result = adjtimex (&buf);
  893. errcode = errno;
  894. if (result != 0)
  895. break;
  896. translate_endian_h2t (&buf, sizeof(buf));
  897. if ((s.write_mem) (cb, &s, arg1, (char *) &buf, sizeof(buf))
  898. != sizeof(buf))
  899. {
  900. result = -1;
  901. errcode = EINVAL;
  902. }
  903. }
  904. break;
  905. case TARGET_LINUX_SYS_mprotect:
  906. result = mprotect ((void *) arg1, arg2, arg3);
  907. errcode = errno;
  908. break;
  909. case TARGET_LINUX_SYS_fchdir:
  910. result = fchdir (arg1);
  911. errcode = errno;
  912. break;
  913. case TARGET_LINUX_SYS_setfsuid32:
  914. case TARGET_LINUX_SYS_setfsuid:
  915. result = setfsuid (arg1);
  916. errcode = errno;
  917. break;
  918. case TARGET_LINUX_SYS_setfsgid32:
  919. case TARGET_LINUX_SYS_setfsgid:
  920. result = setfsgid (arg1);
  921. errcode = errno;
  922. break;
  923. #if 0
  924. case TARGET_LINUX_SYS__llseek:
  925. {
  926. loff_t buf;
  927. result = _llseek (arg1, arg2, arg3, &buf, arg5);
  928. errcode = errno;
  929. if (result != 0)
  930. break;
  931. translate_endian_h2t (&buf, sizeof(buf));
  932. if ((s.write_mem) (cb, &s, t2h_addr (cb, &s, arg4),
  933. (char *) &buf, sizeof(buf)) != sizeof(buf))
  934. {
  935. result = -1;
  936. errcode = EINVAL;
  937. }
  938. }
  939. break;
  940. case TARGET_LINUX_SYS_getdents:
  941. {
  942. struct dirent dir;
  943. result = getdents (arg1, &dir, arg3);
  944. errcode = errno;
  945. if (result != 0)
  946. break;
  947. dir.d_ino = H2T_4 (dir.d_ino);
  948. dir.d_off = H2T_4 (dir.d_off);
  949. dir.d_reclen = H2T_2 (dir.d_reclen);
  950. if ((s.write_mem) (cb, &s, arg2, (char *) &dir, sizeof(dir))
  951. != sizeof(dir))
  952. {
  953. result = -1;
  954. errcode = EINVAL;
  955. }
  956. }
  957. break;
  958. #endif
  959. case TARGET_LINUX_SYS_flock:
  960. result = flock (arg1, arg2);
  961. errcode = errno;
  962. break;
  963. case TARGET_LINUX_SYS_msync:
  964. result = msync ((void *) arg1, arg2, arg3);
  965. errcode = errno;
  966. break;
  967. case TARGET_LINUX_SYS_readv:
  968. {
  969. struct iovec vector;
  970. vector = *((struct iovec *) t2h_addr (cb, &s, arg2));
  971. translate_endian_t2h (&vector, sizeof(vector));
  972. result = readv (arg1, &vector, arg3);
  973. errcode = errno;
  974. }
  975. break;
  976. case TARGET_LINUX_SYS_writev:
  977. {
  978. struct iovec vector;
  979. vector = *((struct iovec *) t2h_addr (cb, &s, arg2));
  980. translate_endian_t2h (&vector, sizeof(vector));
  981. result = writev (arg1, &vector, arg3);
  982. errcode = errno;
  983. }
  984. break;
  985. case TARGET_LINUX_SYS_fdatasync:
  986. result = fdatasync (arg1);
  987. errcode = errno;
  988. break;
  989. case TARGET_LINUX_SYS_mlock:
  990. result = mlock ((void *) t2h_addr (cb, &s, arg1), arg2);
  991. errcode = errno;
  992. break;
  993. case TARGET_LINUX_SYS_munlock:
  994. result = munlock ((void *) t2h_addr (cb, &s, arg1), arg2);
  995. errcode = errno;
  996. break;
  997. case TARGET_LINUX_SYS_nanosleep:
  998. {
  999. struct timespec req, rem;
  1000. req = *((struct timespec *) t2h_addr (cb, &s, arg2));
  1001. translate_endian_t2h (&req, sizeof(req));
  1002. result = nanosleep (&req, &rem);
  1003. errcode = errno;
  1004. if (result != 0)
  1005. break;
  1006. translate_endian_h2t (&rem, sizeof(rem));
  1007. if ((s.write_mem) (cb, &s, arg2, (char *) &rem, sizeof(rem))
  1008. != sizeof(rem))
  1009. {
  1010. result = -1;
  1011. errcode = EINVAL;
  1012. }
  1013. }
  1014. break;
  1015. case TARGET_LINUX_SYS_mremap: /* FIXME */
  1016. result = (int) mremap ((void *) t2h_addr (cb, &s, arg1), arg2, arg3, arg4);
  1017. errcode = errno;
  1018. break;
  1019. case TARGET_LINUX_SYS_getresuid32:
  1020. case TARGET_LINUX_SYS_getresuid:
  1021. {
  1022. uid_t ruid, euid, suid;
  1023. result = getresuid (&ruid, &euid, &suid);
  1024. errcode = errno;
  1025. if (result != 0)
  1026. break;
  1027. *((uid_t *) t2h_addr (cb, &s, arg1)) = H2T_4 (ruid);
  1028. *((uid_t *) t2h_addr (cb, &s, arg2)) = H2T_4 (euid);
  1029. *((uid_t *) t2h_addr (cb, &s, arg3)) = H2T_4 (suid);
  1030. }
  1031. break;
  1032. case TARGET_LINUX_SYS_poll:
  1033. {
  1034. struct pollfd ufds;
  1035. ufds = *((struct pollfd *) t2h_addr (cb, &s, arg1));
  1036. ufds.fd = T2H_4 (ufds.fd);
  1037. ufds.events = T2H_2 (ufds.events);
  1038. ufds.revents = T2H_2 (ufds.revents);
  1039. result = poll (&ufds, arg2, arg3);
  1040. errcode = errno;
  1041. }
  1042. break;
  1043. case TARGET_LINUX_SYS_getresgid32:
  1044. case TARGET_LINUX_SYS_getresgid:
  1045. {
  1046. uid_t rgid, egid, sgid;
  1047. result = getresgid (&rgid, &egid, &sgid);
  1048. errcode = errno;
  1049. if (result != 0)
  1050. break;
  1051. *((uid_t *) t2h_addr (cb, &s, arg1)) = H2T_4 (rgid);
  1052. *((uid_t *) t2h_addr (cb, &s, arg2)) = H2T_4 (egid);
  1053. *((uid_t *) t2h_addr (cb, &s, arg3)) = H2T_4 (sgid);
  1054. }
  1055. break;
  1056. case TARGET_LINUX_SYS_pread:
  1057. result = pread (arg1, (void *) t2h_addr (cb, &s, arg2), arg3, arg4);
  1058. errcode = errno;
  1059. break;
  1060. case TARGET_LINUX_SYS_pwrite:
  1061. result = pwrite (arg1, (void *) t2h_addr (cb, &s, arg2), arg3, arg4);
  1062. errcode = errno;
  1063. break;
  1064. case TARGET_LINUX_SYS_chown32:
  1065. case TARGET_LINUX_SYS_chown:
  1066. result = chown ((char *) t2h_addr (cb, &s, arg1), arg2, arg3);
  1067. errcode = errno;
  1068. break;
  1069. case TARGET_LINUX_SYS_getcwd:
  1070. result = (int) getcwd ((char *) t2h_addr (cb, &s, arg1), arg2);
  1071. errcode = errno;
  1072. break;
  1073. case TARGET_LINUX_SYS_sendfile:
  1074. {
  1075. off_t offset;
  1076. offset = *((off_t *) t2h_addr (cb, &s, arg3));
  1077. offset = T2H_4 (offset);
  1078. result = sendfile (arg1, arg2, &offset, arg3);
  1079. errcode = errno;
  1080. if (result != 0)
  1081. break;
  1082. *((off_t *) t2h_addr (cb, &s, arg3)) = H2T_4 (offset);
  1083. }
  1084. break;
  1085. default:
  1086. result = -1;
  1087. errcode = ENOSYS;
  1088. break;
  1089. }
  1090. if (result == -1)
  1091. m32rbf_h_gr_set (current_cpu, 0, -errcode);
  1092. else
  1093. m32rbf_h_gr_set (current_cpu, 0, result);
  1094. break;
  1095. }
  1096. #endif
  1097. case TRAP_BREAKPOINT:
  1098. sim_engine_halt (sd, current_cpu, NULL, pc,
  1099. sim_stopped, SIM_SIGTRAP);
  1100. break;
  1101. case TRAP_FLUSH_CACHE:
  1102. /* Do nothing. */
  1103. break;
  1104. case_default:
  1105. default:
  1106. {
  1107. /* The new pc is the trap vector entry.
  1108. We assume there's a branch there to some handler.
  1109. Use cr5 as EVB (EIT Vector Base) register. */
  1110. /* USI new_pc = EIT_TRAP_BASE_ADDR + num * 4; */
  1111. USI new_pc = m32rbf_h_cr_get (current_cpu, 5) + 0x40 + num * 4;
  1112. return new_pc;
  1113. }
  1114. }
  1115. /* Fake an "rte" insn. */
  1116. /* FIXME: Should duplicate all of rte processing. */
  1117. return (pc & -4) + 4;
  1118. }