traps-linux.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360
  1. /* m32r exception, interrupt, and trap (EIT) support
  2. Copyright (C) 1998-2015 Free Software Foundation, Inc.
  3. Contributed by 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. #include "sim-main.h"
  16. #include "sim-syscall.h"
  17. #include "syscall.h"
  18. #include "targ-vals.h"
  19. #include <dirent.h>
  20. #include <errno.h>
  21. #include <fcntl.h>
  22. #include <time.h>
  23. #include <unistd.h>
  24. #include <utime.h>
  25. #include <sys/mman.h>
  26. #include <sys/poll.h>
  27. #include <sys/resource.h>
  28. #include <sys/sysinfo.h>
  29. #include <sys/stat.h>
  30. #include <sys/time.h>
  31. #include <sys/timeb.h>
  32. #include <sys/timex.h>
  33. #include <sys/types.h>
  34. #include <sys/uio.h>
  35. #include <sys/utsname.h>
  36. #include <sys/vfs.h>
  37. #include <linux/sysctl.h>
  38. #include <linux/types.h>
  39. #include <linux/unistd.h>
  40. #define TRAP_ELF_SYSCALL 0
  41. #define TRAP_LINUX_SYSCALL 2
  42. #define TRAP_FLUSH_CACHE 12
  43. /* The semantic code invokes this for invalid (unrecognized) instructions. */
  44. SEM_PC
  45. sim_engine_invalid_insn (SIM_CPU *current_cpu, IADDR cia, SEM_PC vpc)
  46. {
  47. SIM_DESC sd = CPU_STATE (current_cpu);
  48. #if 0
  49. if (STATE_ENVIRONMENT (sd) == OPERATING_ENVIRONMENT)
  50. {
  51. h_bsm_set (current_cpu, h_sm_get (current_cpu));
  52. h_bie_set (current_cpu, h_ie_get (current_cpu));
  53. h_bcond_set (current_cpu, h_cond_get (current_cpu));
  54. /* sm not changed */
  55. h_ie_set (current_cpu, 0);
  56. h_cond_set (current_cpu, 0);
  57. h_bpc_set (current_cpu, cia);
  58. sim_engine_restart (CPU_STATE (current_cpu), current_cpu, NULL,
  59. EIT_RSVD_INSN_ADDR);
  60. }
  61. else
  62. #endif
  63. sim_engine_halt (sd, current_cpu, NULL, cia, sim_stopped, SIM_SIGILL);
  64. return vpc;
  65. }
  66. /* Process an address exception. */
  67. void
  68. m32r_core_signal (SIM_DESC sd, SIM_CPU *current_cpu, sim_cia cia,
  69. unsigned int map, int nr_bytes, address_word addr,
  70. transfer_type transfer, sim_core_signals sig)
  71. {
  72. if (STATE_ENVIRONMENT (sd) == OPERATING_ENVIRONMENT)
  73. {
  74. m32rbf_h_cr_set (current_cpu, H_CR_BBPC,
  75. m32rbf_h_cr_get (current_cpu, H_CR_BPC));
  76. if (MACH_NUM (CPU_MACH (current_cpu)) == MACH_M32R)
  77. {
  78. m32rbf_h_bpsw_set (current_cpu, m32rbf_h_psw_get (current_cpu));
  79. /* sm not changed */
  80. m32rbf_h_psw_set (current_cpu, m32rbf_h_psw_get (current_cpu) & 0x80);
  81. }
  82. else if (MACH_NUM (CPU_MACH (current_cpu)) == MACH_M32RX)
  83. {
  84. m32rxf_h_bpsw_set (current_cpu, m32rxf_h_psw_get (current_cpu));
  85. /* sm not changed */
  86. m32rxf_h_psw_set (current_cpu, m32rxf_h_psw_get (current_cpu) & 0x80);
  87. }
  88. else
  89. {
  90. m32r2f_h_bpsw_set (current_cpu, m32r2f_h_psw_get (current_cpu));
  91. /* sm not changed */
  92. m32r2f_h_psw_set (current_cpu, m32r2f_h_psw_get (current_cpu) & 0x80);
  93. }
  94. m32rbf_h_cr_set (current_cpu, H_CR_BPC, cia);
  95. sim_engine_restart (CPU_STATE (current_cpu), current_cpu, NULL,
  96. EIT_ADDR_EXCP_ADDR);
  97. }
  98. else
  99. sim_core_signal (sd, current_cpu, cia, map, nr_bytes, addr,
  100. transfer, sig);
  101. }
  102. /* Translate target's address to host's address. */
  103. static void *
  104. t2h_addr (host_callback *cb, struct cb_syscall *sc,
  105. unsigned long taddr)
  106. {
  107. void *addr;
  108. SIM_DESC sd = (SIM_DESC) sc->p1;
  109. SIM_CPU *cpu = (SIM_CPU *) sc->p2;
  110. if (taddr == 0)
  111. return NULL;
  112. return sim_core_trans_addr (sd, cpu, read_map, taddr);
  113. }
  114. static unsigned int
  115. conv_endian (unsigned int tvalue)
  116. {
  117. unsigned int hvalue;
  118. unsigned int t1, t2, t3, t4;
  119. if (CURRENT_HOST_BYTE_ORDER == LITTLE_ENDIAN)
  120. {
  121. t1 = tvalue & 0xff000000;
  122. t2 = tvalue & 0x00ff0000;
  123. t3 = tvalue & 0x0000ff00;
  124. t4 = tvalue & 0x000000ff;
  125. hvalue = t1 >> 24;
  126. hvalue += t2 >> 8;
  127. hvalue += t3 << 8;
  128. hvalue += t4 << 24;
  129. }
  130. else
  131. hvalue = tvalue;
  132. return hvalue;
  133. }
  134. static unsigned short
  135. conv_endian16 (unsigned short tvalue)
  136. {
  137. unsigned short hvalue;
  138. unsigned short t1, t2;
  139. if (CURRENT_HOST_BYTE_ORDER == LITTLE_ENDIAN)
  140. {
  141. t1 = tvalue & 0xff00;
  142. t2 = tvalue & 0x00ff;
  143. hvalue = t1 >> 8;
  144. hvalue += t2 << 8;
  145. }
  146. else
  147. hvalue = tvalue;
  148. return hvalue;
  149. }
  150. static void
  151. translate_endian(void *addr, size_t size)
  152. {
  153. unsigned int *p = (unsigned int *) addr;
  154. int i;
  155. for (i = 0; i <= size - 4; i += 4,p++)
  156. *p = conv_endian(*p);
  157. if (i <= size - 2)
  158. *((unsigned short *) p) = conv_endian16(*((unsigned short *) p));
  159. }
  160. /* Trap support.
  161. The result is the pc address to continue at.
  162. Preprocessing like saving the various registers has already been done. */
  163. USI
  164. m32r_trap (SIM_CPU *current_cpu, PCADDR pc, int num)
  165. {
  166. SIM_DESC sd = CPU_STATE (current_cpu);
  167. host_callback *cb = STATE_CALLBACK (sd);
  168. #ifdef SIM_HAVE_BREAKPOINTS
  169. /* Check for breakpoints "owned" by the simulator first, regardless
  170. of --environment. */
  171. if (num == TRAP_BREAKPOINT)
  172. {
  173. /* First try sim-break.c. If it's a breakpoint the simulator "owns"
  174. it doesn't return. Otherwise it returns and let's us try. */
  175. sim_handle_breakpoint (sd, current_cpu, pc);
  176. /* Fall through. */
  177. }
  178. #endif
  179. switch (num)
  180. {
  181. case TRAP_ELF_SYSCALL :
  182. {
  183. long result, result2;
  184. int errcode;
  185. sim_syscall_multi (current_cpu,
  186. m32rbf_h_gr_get (current_cpu, 0),
  187. m32rbf_h_gr_get (current_cpu, 1),
  188. m32rbf_h_gr_get (current_cpu, 2),
  189. m32rbf_h_gr_get (current_cpu, 3),
  190. m32rbf_h_gr_get (current_cpu, 4),
  191. &result, &result2, &errcode);
  192. m32rbf_h_gr_set (current_cpu, 2, errcode);
  193. m32rbf_h_gr_set (current_cpu, 0, result);
  194. m32rbf_h_gr_set (current_cpu, 1, result2);
  195. break;
  196. }
  197. case TRAP_LINUX_SYSCALL :
  198. {
  199. CB_SYSCALL s;
  200. unsigned int func, arg1, arg2, arg3, arg4, arg5, arg6, arg7;
  201. int result, result2, errcode;
  202. if (STATE_ENVIRONMENT (sd) == OPERATING_ENVIRONMENT)
  203. {
  204. /* The new pc is the trap vector entry.
  205. We assume there's a branch there to some handler.
  206. Use cr5 as EVB (EIT Vector Base) register. */
  207. USI new_pc = m32rbf_h_cr_get (current_cpu, 5) + 0x40 + num * 4;
  208. return new_pc;
  209. }
  210. func = m32rbf_h_gr_get (current_cpu, 7);
  211. arg1 = m32rbf_h_gr_get (current_cpu, 0);
  212. arg2 = m32rbf_h_gr_get (current_cpu, 1);
  213. arg3 = m32rbf_h_gr_get (current_cpu, 2);
  214. arg4 = m32rbf_h_gr_get (current_cpu, 3);
  215. arg5 = m32rbf_h_gr_get (current_cpu, 4);
  216. arg6 = m32rbf_h_gr_get (current_cpu, 5);
  217. arg7 = m32rbf_h_gr_get (current_cpu, 6);
  218. CB_SYSCALL_INIT (&s);
  219. s.func = func;
  220. s.arg1 = arg1;
  221. s.arg2 = arg2;
  222. s.arg3 = arg3;
  223. s.p1 = (PTR) sd;
  224. s.p2 = (PTR) current_cpu;
  225. s.read_mem = sim_syscall_read_mem;
  226. s.write_mem = sim_syscall_write_mem;
  227. result = 0;
  228. result2 = 0;
  229. errcode = 0;
  230. switch (func)
  231. {
  232. case __NR_exit:
  233. sim_engine_halt (sd, current_cpu, NULL, pc, sim_exited, arg1);
  234. break;
  235. case __NR_read:
  236. result = read(arg1, t2h_addr(cb, &s, arg2), arg3);
  237. errcode = errno;
  238. break;
  239. case __NR_write:
  240. result = write(arg1, t2h_addr(cb, &s, arg2), arg3);
  241. errcode = errno;
  242. break;
  243. case __NR_open:
  244. result = open((char *) t2h_addr(cb, &s, arg1), arg2, arg3);
  245. errcode = errno;
  246. break;
  247. case __NR_close:
  248. result = close(arg1);
  249. errcode = errno;
  250. break;
  251. case __NR_creat:
  252. result = creat((char *) t2h_addr(cb, &s, arg1), arg2);
  253. errcode = errno;
  254. break;
  255. case __NR_link:
  256. result = link((char *) t2h_addr(cb, &s, arg1),
  257. (char *) t2h_addr(cb, &s, arg2));
  258. errcode = errno;
  259. break;
  260. case __NR_unlink:
  261. result = unlink((char *) t2h_addr(cb, &s, arg1));
  262. errcode = errno;
  263. break;
  264. case __NR_chdir:
  265. result = chdir((char *) t2h_addr(cb, &s, arg1));
  266. errcode = errno;
  267. break;
  268. case __NR_time:
  269. {
  270. time_t t;
  271. if (arg1 == 0)
  272. {
  273. result = (int) time(NULL);
  274. errcode = errno;
  275. }
  276. else
  277. {
  278. result = (int) time(&t);
  279. errcode = errno;
  280. if (result != 0)
  281. break;
  282. translate_endian((void *) &t, sizeof(t));
  283. if ((s.write_mem) (cb, &s, arg1, (char *) &t, sizeof(t)) != sizeof(t))
  284. {
  285. result = -1;
  286. errcode = EINVAL;
  287. }
  288. }
  289. }
  290. break;
  291. case __NR_mknod:
  292. result = mknod((char *) t2h_addr(cb, &s, arg1),
  293. (mode_t) arg2, (dev_t) arg3);
  294. errcode = errno;
  295. break;
  296. case __NR_chmod:
  297. result = chmod((char *) t2h_addr(cb, &s, arg1), (mode_t) arg2);
  298. errcode = errno;
  299. break;
  300. case __NR_lchown32:
  301. case __NR_lchown:
  302. result = lchown((char *) t2h_addr(cb, &s, arg1),
  303. (uid_t) arg2, (gid_t) arg3);
  304. errcode = errno;
  305. break;
  306. case __NR_lseek:
  307. result = (int) lseek(arg1, (off_t) arg2, arg3);
  308. errcode = errno;
  309. break;
  310. case __NR_getpid:
  311. result = getpid();
  312. errcode = errno;
  313. break;
  314. case __NR_getuid32:
  315. case __NR_getuid:
  316. result = getuid();
  317. errcode = errno;
  318. break;
  319. case __NR_utime:
  320. {
  321. struct utimbuf buf;
  322. if (arg2 == 0)
  323. {
  324. result = utime((char *) t2h_addr(cb, &s, arg1), NULL);
  325. errcode = errno;
  326. }
  327. else
  328. {
  329. buf = *((struct utimbuf *) t2h_addr(cb, &s, arg2));
  330. translate_endian((void *) &buf, sizeof(buf));
  331. result = utime((char *) t2h_addr(cb, &s, arg1), &buf);
  332. errcode = errno;
  333. }
  334. }
  335. break;
  336. case __NR_access:
  337. result = access((char *) t2h_addr(cb, &s, arg1), arg2);
  338. errcode = errno;
  339. break;
  340. case __NR_ftime:
  341. {
  342. struct timeb t;
  343. result = ftime(&t);
  344. errcode = errno;
  345. if (result != 0)
  346. break;
  347. t.time = conv_endian(t.time);
  348. t.millitm = conv_endian16(t.millitm);
  349. t.timezone = conv_endian16(t.timezone);
  350. t.dstflag = conv_endian16(t.dstflag);
  351. if ((s.write_mem) (cb, &s, arg1, (char *) &t, sizeof(t))
  352. != sizeof(t))
  353. {
  354. result = -1;
  355. errcode = EINVAL;
  356. }
  357. }
  358. case __NR_sync:
  359. sync();
  360. result = 0;
  361. break;
  362. case __NR_rename:
  363. result = rename((char *) t2h_addr(cb, &s, arg1),
  364. (char *) t2h_addr(cb, &s, arg2));
  365. errcode = errno;
  366. break;
  367. case __NR_mkdir:
  368. result = mkdir((char *) t2h_addr(cb, &s, arg1), arg2);
  369. errcode = errno;
  370. break;
  371. case __NR_rmdir:
  372. result = rmdir((char *) t2h_addr(cb, &s, arg1));
  373. errcode = errno;
  374. break;
  375. case __NR_dup:
  376. result = dup(arg1);
  377. errcode = errno;
  378. break;
  379. case __NR_brk:
  380. result = brk((void *) arg1);
  381. errcode = errno;
  382. //result = arg1;
  383. break;
  384. case __NR_getgid32:
  385. case __NR_getgid:
  386. result = getgid();
  387. errcode = errno;
  388. break;
  389. case __NR_geteuid32:
  390. case __NR_geteuid:
  391. result = geteuid();
  392. errcode = errno;
  393. break;
  394. case __NR_getegid32:
  395. case __NR_getegid:
  396. result = getegid();
  397. errcode = errno;
  398. break;
  399. case __NR_ioctl:
  400. result = ioctl(arg1, arg2, arg3);
  401. errcode = errno;
  402. break;
  403. case __NR_fcntl:
  404. result = fcntl(arg1, arg2, arg3);
  405. errcode = errno;
  406. break;
  407. case __NR_dup2:
  408. result = dup2(arg1, arg2);
  409. errcode = errno;
  410. break;
  411. case __NR_getppid:
  412. result = getppid();
  413. errcode = errno;
  414. break;
  415. case __NR_getpgrp:
  416. result = getpgrp();
  417. errcode = errno;
  418. break;
  419. case __NR_getrlimit:
  420. {
  421. struct rlimit rlim;
  422. result = getrlimit(arg1, &rlim);
  423. errcode = errno;
  424. if (result != 0)
  425. break;
  426. translate_endian((void *) &rlim, sizeof(rlim));
  427. if ((s.write_mem) (cb, &s, arg2, (char *) &rlim, sizeof(rlim))
  428. != sizeof(rlim))
  429. {
  430. result = -1;
  431. errcode = EINVAL;
  432. }
  433. }
  434. break;
  435. case __NR_getrusage:
  436. {
  437. struct rusage usage;
  438. result = getrusage(arg1, &usage);
  439. errcode = errno;
  440. if (result != 0)
  441. break;
  442. translate_endian((void *) &usage, sizeof(usage));
  443. if ((s.write_mem) (cb, &s, arg2, (char *) &usage, sizeof(usage))
  444. != sizeof(usage))
  445. {
  446. result = -1;
  447. errcode = EINVAL;
  448. }
  449. }
  450. break;
  451. case __NR_gettimeofday:
  452. {
  453. struct timeval tv;
  454. struct timezone tz;
  455. result = gettimeofday(&tv, &tz);
  456. errcode = errno;
  457. if (result != 0)
  458. break;
  459. translate_endian((void *) &tv, sizeof(tv));
  460. if ((s.write_mem) (cb, &s, arg1, (char *) &tv, sizeof(tv))
  461. != sizeof(tv))
  462. {
  463. result = -1;
  464. errcode = EINVAL;
  465. }
  466. translate_endian((void *) &tz, sizeof(tz));
  467. if ((s.write_mem) (cb, &s, arg2, (char *) &tz, sizeof(tz))
  468. != sizeof(tz))
  469. {
  470. result = -1;
  471. errcode = EINVAL;
  472. }
  473. }
  474. break;
  475. case __NR_getgroups32:
  476. case __NR_getgroups:
  477. {
  478. gid_t *list;
  479. if (arg1 > 0)
  480. list = (gid_t *) malloc(arg1 * sizeof(gid_t));
  481. result = getgroups(arg1, list);
  482. errcode = errno;
  483. if (result != 0)
  484. break;
  485. translate_endian((void *) list, arg1 * sizeof(gid_t));
  486. if (arg1 > 0)
  487. if ((s.write_mem) (cb, &s, arg2, (char *) list, arg1 * sizeof(gid_t))
  488. != arg1 * sizeof(gid_t))
  489. {
  490. result = -1;
  491. errcode = EINVAL;
  492. }
  493. }
  494. break;
  495. case __NR_select:
  496. {
  497. int n;
  498. fd_set readfds;
  499. fd_set *treadfdsp;
  500. fd_set *hreadfdsp;
  501. fd_set writefds;
  502. fd_set *twritefdsp;
  503. fd_set *hwritefdsp;
  504. fd_set exceptfds;
  505. fd_set *texceptfdsp;
  506. fd_set *hexceptfdsp;
  507. struct timeval *ttimeoutp;
  508. struct timeval timeout;
  509. n = arg1;
  510. treadfdsp = (fd_set *) arg2;
  511. if (treadfdsp != NULL)
  512. {
  513. readfds = *((fd_set *) t2h_addr(cb, &s, (unsigned int) treadfdsp));
  514. translate_endian((void *) &readfds, sizeof(readfds));
  515. hreadfdsp = &readfds;
  516. }
  517. else
  518. hreadfdsp = NULL;
  519. twritefdsp = (fd_set *) arg3;
  520. if (twritefdsp != NULL)
  521. {
  522. writefds = *((fd_set *) t2h_addr(cb, &s, (unsigned int) twritefdsp));
  523. translate_endian((void *) &writefds, sizeof(writefds));
  524. hwritefdsp = &writefds;
  525. }
  526. else
  527. hwritefdsp = NULL;
  528. texceptfdsp = (fd_set *) arg4;
  529. if (texceptfdsp != NULL)
  530. {
  531. exceptfds = *((fd_set *) t2h_addr(cb, &s, (unsigned int) texceptfdsp));
  532. translate_endian((void *) &exceptfds, sizeof(exceptfds));
  533. hexceptfdsp = &exceptfds;
  534. }
  535. else
  536. hexceptfdsp = NULL;
  537. ttimeoutp = (struct timeval *) arg5;
  538. timeout = *((struct timeval *) t2h_addr(cb, &s, (unsigned int) ttimeoutp));
  539. translate_endian((void *) &timeout, sizeof(timeout));
  540. result = select(n, hreadfdsp, hwritefdsp, hexceptfdsp, &timeout);
  541. errcode = errno;
  542. if (result != 0)
  543. break;
  544. if (treadfdsp != NULL)
  545. {
  546. translate_endian((void *) &readfds, sizeof(readfds));
  547. if ((s.write_mem) (cb, &s, (unsigned long) treadfdsp,
  548. (char *) &readfds, sizeof(readfds)) != sizeof(readfds))
  549. {
  550. result = -1;
  551. errcode = EINVAL;
  552. }
  553. }
  554. if (twritefdsp != NULL)
  555. {
  556. translate_endian((void *) &writefds, sizeof(writefds));
  557. if ((s.write_mem) (cb, &s, (unsigned long) twritefdsp,
  558. (char *) &writefds, sizeof(writefds)) != sizeof(writefds))
  559. {
  560. result = -1;
  561. errcode = EINVAL;
  562. }
  563. }
  564. if (texceptfdsp != NULL)
  565. {
  566. translate_endian((void *) &exceptfds, sizeof(exceptfds));
  567. if ((s.write_mem) (cb, &s, (unsigned long) texceptfdsp,
  568. (char *) &exceptfds, sizeof(exceptfds)) != sizeof(exceptfds))
  569. {
  570. result = -1;
  571. errcode = EINVAL;
  572. }
  573. }
  574. translate_endian((void *) &timeout, sizeof(timeout));
  575. if ((s.write_mem) (cb, &s, (unsigned long) ttimeoutp,
  576. (char *) &timeout, sizeof(timeout)) != sizeof(timeout))
  577. {
  578. result = -1;
  579. errcode = EINVAL;
  580. }
  581. }
  582. break;
  583. case __NR_symlink:
  584. result = symlink((char *) t2h_addr(cb, &s, arg1),
  585. (char *) t2h_addr(cb, &s, arg2));
  586. errcode = errno;
  587. break;
  588. case __NR_readlink:
  589. result = readlink((char *) t2h_addr(cb, &s, arg1),
  590. (char *) t2h_addr(cb, &s, arg2),
  591. arg3);
  592. errcode = errno;
  593. break;
  594. case __NR_readdir:
  595. result = (int) readdir((DIR *) t2h_addr(cb, &s, arg1));
  596. errcode = errno;
  597. break;
  598. #if 0
  599. case __NR_mmap:
  600. {
  601. result = (int) mmap((void *) t2h_addr(cb, &s, arg1),
  602. arg2, arg3, arg4, arg5, arg6);
  603. errcode = errno;
  604. if (errno == 0)
  605. {
  606. sim_core_attach (sd, NULL,
  607. 0, access_read_write_exec, 0,
  608. result, arg2, 0, NULL, NULL);
  609. }
  610. }
  611. break;
  612. #endif
  613. case __NR_mmap2:
  614. {
  615. void *addr;
  616. size_t len;
  617. int prot, flags, fildes;
  618. off_t off;
  619. addr = (void *) t2h_addr(cb, &s, arg1);
  620. len = arg2;
  621. prot = arg3;
  622. flags = arg4;
  623. fildes = arg5;
  624. off = arg6 << 12;
  625. result = (int) mmap(addr, len, prot, flags, fildes, off);
  626. errcode = errno;
  627. if (result != -1)
  628. {
  629. char c;
  630. if (sim_core_read_buffer (sd, NULL, read_map, &c, result, 1) == 0)
  631. sim_core_attach (sd, NULL,
  632. 0, access_read_write_exec, 0,
  633. result, len, 0, NULL, NULL);
  634. }
  635. }
  636. break;
  637. case __NR_mmap:
  638. {
  639. void *addr;
  640. size_t len;
  641. int prot, flags, fildes;
  642. off_t off;
  643. addr = *((void **) t2h_addr(cb, &s, arg1));
  644. len = *((size_t *) t2h_addr(cb, &s, arg1 + 4));
  645. prot = *((int *) t2h_addr(cb, &s, arg1 + 8));
  646. flags = *((int *) t2h_addr(cb, &s, arg1 + 12));
  647. fildes = *((int *) t2h_addr(cb, &s, arg1 + 16));
  648. off = *((off_t *) t2h_addr(cb, &s, arg1 + 20));
  649. addr = (void *) conv_endian((unsigned int) addr);
  650. len = conv_endian(len);
  651. prot = conv_endian(prot);
  652. flags = conv_endian(flags);
  653. fildes = conv_endian(fildes);
  654. off = conv_endian(off);
  655. //addr = (void *) t2h_addr(cb, &s, (unsigned int) addr);
  656. result = (int) mmap(addr, len, prot, flags, fildes, off);
  657. errcode = errno;
  658. //if (errno == 0)
  659. if (result != -1)
  660. {
  661. char c;
  662. if (sim_core_read_buffer (sd, NULL, read_map, &c, result, 1) == 0)
  663. sim_core_attach (sd, NULL,
  664. 0, access_read_write_exec, 0,
  665. result, len, 0, NULL, NULL);
  666. }
  667. }
  668. break;
  669. case __NR_munmap:
  670. {
  671. result = munmap((void *)arg1, arg2);
  672. errcode = errno;
  673. if (result != -1)
  674. {
  675. sim_core_detach (sd, NULL, 0, arg2, result);
  676. }
  677. }
  678. break;
  679. case __NR_truncate:
  680. result = truncate((char *) t2h_addr(cb, &s, arg1), arg2);
  681. errcode = errno;
  682. break;
  683. case __NR_ftruncate:
  684. result = ftruncate(arg1, arg2);
  685. errcode = errno;
  686. break;
  687. case __NR_fchmod:
  688. result = fchmod(arg1, arg2);
  689. errcode = errno;
  690. break;
  691. case __NR_fchown32:
  692. case __NR_fchown:
  693. result = fchown(arg1, arg2, arg3);
  694. errcode = errno;
  695. break;
  696. case __NR_statfs:
  697. {
  698. struct statfs statbuf;
  699. result = statfs((char *) t2h_addr(cb, &s, arg1), &statbuf);
  700. errcode = errno;
  701. if (result != 0)
  702. break;
  703. translate_endian((void *) &statbuf, sizeof(statbuf));
  704. if ((s.write_mem) (cb, &s, arg2, (char *) &statbuf, sizeof(statbuf))
  705. != sizeof(statbuf))
  706. {
  707. result = -1;
  708. errcode = EINVAL;
  709. }
  710. }
  711. break;
  712. case __NR_fstatfs:
  713. {
  714. struct statfs statbuf;
  715. result = fstatfs(arg1, &statbuf);
  716. errcode = errno;
  717. if (result != 0)
  718. break;
  719. translate_endian((void *) &statbuf, sizeof(statbuf));
  720. if ((s.write_mem) (cb, &s, arg2, (char *) &statbuf, sizeof(statbuf))
  721. != sizeof(statbuf))
  722. {
  723. result = -1;
  724. errcode = EINVAL;
  725. }
  726. }
  727. break;
  728. case __NR_syslog:
  729. result = syslog(arg1, (char *) t2h_addr(cb, &s, arg2));
  730. errcode = errno;
  731. break;
  732. case __NR_setitimer:
  733. {
  734. struct itimerval value, ovalue;
  735. value = *((struct itimerval *) t2h_addr(cb, &s, arg2));
  736. translate_endian((void *) &value, sizeof(value));
  737. if (arg2 == 0)
  738. {
  739. result = setitimer(arg1, &value, NULL);
  740. errcode = errno;
  741. }
  742. else
  743. {
  744. result = setitimer(arg1, &value, &ovalue);
  745. errcode = errno;
  746. if (result != 0)
  747. break;
  748. translate_endian((void *) &ovalue, sizeof(ovalue));
  749. if ((s.write_mem) (cb, &s, arg3, (char *) &ovalue, sizeof(ovalue))
  750. != sizeof(ovalue))
  751. {
  752. result = -1;
  753. errcode = EINVAL;
  754. }
  755. }
  756. }
  757. break;
  758. case __NR_getitimer:
  759. {
  760. struct itimerval value;
  761. result = getitimer(arg1, &value);
  762. errcode = errno;
  763. if (result != 0)
  764. break;
  765. translate_endian((void *) &value, sizeof(value));
  766. if ((s.write_mem) (cb, &s, arg2, (char *) &value, sizeof(value))
  767. != sizeof(value))
  768. {
  769. result = -1;
  770. errcode = EINVAL;
  771. }
  772. }
  773. break;
  774. case __NR_stat:
  775. {
  776. char *buf;
  777. int buflen;
  778. struct stat statbuf;
  779. result = stat((char *) t2h_addr(cb, &s, arg1), &statbuf);
  780. errcode = errno;
  781. if (result < 0)
  782. break;
  783. buflen = cb_host_to_target_stat (cb, NULL, NULL);
  784. buf = xmalloc (buflen);
  785. if (cb_host_to_target_stat (cb, &statbuf, buf) != buflen)
  786. {
  787. /* The translation failed. This is due to an internal
  788. host program error, not the target's fault. */
  789. free (buf);
  790. result = -1;
  791. errcode = ENOSYS;
  792. break;
  793. }
  794. if ((s.write_mem) (cb, &s, arg2, buf, buflen) != buflen)
  795. {
  796. free (buf);
  797. result = -1;
  798. errcode = EINVAL;
  799. break;
  800. }
  801. free (buf);
  802. }
  803. break;
  804. case __NR_lstat:
  805. {
  806. char *buf;
  807. int buflen;
  808. struct stat statbuf;
  809. result = lstat((char *) t2h_addr(cb, &s, arg1), &statbuf);
  810. errcode = errno;
  811. if (result < 0)
  812. break;
  813. buflen = cb_host_to_target_stat (cb, NULL, NULL);
  814. buf = xmalloc (buflen);
  815. if (cb_host_to_target_stat (cb, &statbuf, buf) != buflen)
  816. {
  817. /* The translation failed. This is due to an internal
  818. host program error, not the target's fault. */
  819. free (buf);
  820. result = -1;
  821. errcode = ENOSYS;
  822. break;
  823. }
  824. if ((s.write_mem) (cb, &s, arg2, buf, buflen) != buflen)
  825. {
  826. free (buf);
  827. result = -1;
  828. errcode = EINVAL;
  829. break;
  830. }
  831. free (buf);
  832. }
  833. break;
  834. case __NR_fstat:
  835. {
  836. char *buf;
  837. int buflen;
  838. struct stat statbuf;
  839. result = fstat(arg1, &statbuf);
  840. errcode = errno;
  841. if (result < 0)
  842. break;
  843. buflen = cb_host_to_target_stat (cb, NULL, NULL);
  844. buf = xmalloc (buflen);
  845. if (cb_host_to_target_stat (cb, &statbuf, buf) != buflen)
  846. {
  847. /* The translation failed. This is due to an internal
  848. host program error, not the target's fault. */
  849. free (buf);
  850. result = -1;
  851. errcode = ENOSYS;
  852. break;
  853. }
  854. if ((s.write_mem) (cb, &s, arg2, buf, buflen) != buflen)
  855. {
  856. free (buf);
  857. result = -1;
  858. errcode = EINVAL;
  859. break;
  860. }
  861. free (buf);
  862. }
  863. break;
  864. case __NR_sysinfo:
  865. {
  866. struct sysinfo info;
  867. result = sysinfo(&info);
  868. errcode = errno;
  869. if (result != 0)
  870. break;
  871. info.uptime = conv_endian(info.uptime);
  872. info.loads[0] = conv_endian(info.loads[0]);
  873. info.loads[1] = conv_endian(info.loads[1]);
  874. info.loads[2] = conv_endian(info.loads[2]);
  875. info.totalram = conv_endian(info.totalram);
  876. info.freeram = conv_endian(info.freeram);
  877. info.sharedram = conv_endian(info.sharedram);
  878. info.bufferram = conv_endian(info.bufferram);
  879. info.totalswap = conv_endian(info.totalswap);
  880. info.freeswap = conv_endian(info.freeswap);
  881. info.procs = conv_endian16(info.procs);
  882. #if LINUX_VERSION_CODE >= 0x20400
  883. info.totalhigh = conv_endian(info.totalhigh);
  884. info.freehigh = conv_endian(info.freehigh);
  885. info.mem_unit = conv_endian(info.mem_unit);
  886. #endif
  887. if ((s.write_mem) (cb, &s, arg1, (char *) &info, sizeof(info))
  888. != sizeof(info))
  889. {
  890. result = -1;
  891. errcode = EINVAL;
  892. }
  893. }
  894. break;
  895. #if 0
  896. case __NR_ipc:
  897. {
  898. result = ipc(arg1, arg2, arg3, arg4,
  899. (void *) t2h_addr(cb, &s, arg5), arg6);
  900. errcode = errno;
  901. }
  902. break;
  903. #endif
  904. case __NR_fsync:
  905. result = fsync(arg1);
  906. errcode = errno;
  907. break;
  908. case __NR_uname:
  909. /* utsname contains only arrays of char, so it is not necessary
  910. to translate endian. */
  911. result = uname((struct utsname *) t2h_addr(cb, &s, arg1));
  912. errcode = errno;
  913. break;
  914. case __NR_adjtimex:
  915. {
  916. struct timex buf;
  917. result = adjtimex(&buf);
  918. errcode = errno;
  919. if (result != 0)
  920. break;
  921. translate_endian((void *) &buf, sizeof(buf));
  922. if ((s.write_mem) (cb, &s, arg1, (char *) &buf, sizeof(buf))
  923. != sizeof(buf))
  924. {
  925. result = -1;
  926. errcode = EINVAL;
  927. }
  928. }
  929. break;
  930. case __NR_mprotect:
  931. result = mprotect((void *) arg1, arg2, arg3);
  932. errcode = errno;
  933. break;
  934. case __NR_fchdir:
  935. result = fchdir(arg1);
  936. errcode = errno;
  937. break;
  938. case __NR_setfsuid32:
  939. case __NR_setfsuid:
  940. result = setfsuid(arg1);
  941. errcode = errno;
  942. break;
  943. case __NR_setfsgid32:
  944. case __NR_setfsgid:
  945. result = setfsgid(arg1);
  946. errcode = errno;
  947. break;
  948. #if 0
  949. case __NR__llseek:
  950. {
  951. loff_t buf;
  952. result = _llseek(arg1, arg2, arg3, &buf, arg5);
  953. errcode = errno;
  954. if (result != 0)
  955. break;
  956. translate_endian((void *) &buf, sizeof(buf));
  957. if ((s.write_mem) (cb, &s, t2h_addr(cb, &s, arg4),
  958. (char *) &buf, sizeof(buf)) != sizeof(buf))
  959. {
  960. result = -1;
  961. errcode = EINVAL;
  962. }
  963. }
  964. break;
  965. case __NR_getdents:
  966. {
  967. struct dirent dir;
  968. result = getdents(arg1, &dir, arg3);
  969. errcode = errno;
  970. if (result != 0)
  971. break;
  972. dir.d_ino = conv_endian(dir.d_ino);
  973. dir.d_off = conv_endian(dir.d_off);
  974. dir.d_reclen = conv_endian16(dir.d_reclen);
  975. if ((s.write_mem) (cb, &s, arg2, (char *) &dir, sizeof(dir))
  976. != sizeof(dir))
  977. {
  978. result = -1;
  979. errcode = EINVAL;
  980. }
  981. }
  982. break;
  983. #endif
  984. case __NR_flock:
  985. result = flock(arg1, arg2);
  986. errcode = errno;
  987. break;
  988. case __NR_msync:
  989. result = msync((void *) arg1, arg2, arg3);
  990. errcode = errno;
  991. break;
  992. case __NR_readv:
  993. {
  994. struct iovec vector;
  995. vector = *((struct iovec *) t2h_addr(cb, &s, arg2));
  996. translate_endian((void *) &vector, sizeof(vector));
  997. result = readv(arg1, &vector, arg3);
  998. errcode = errno;
  999. }
  1000. break;
  1001. case __NR_writev:
  1002. {
  1003. struct iovec vector;
  1004. vector = *((struct iovec *) t2h_addr(cb, &s, arg2));
  1005. translate_endian((void *) &vector, sizeof(vector));
  1006. result = writev(arg1, &vector, arg3);
  1007. errcode = errno;
  1008. }
  1009. break;
  1010. case __NR_fdatasync:
  1011. result = fdatasync(arg1);
  1012. errcode = errno;
  1013. break;
  1014. case __NR_mlock:
  1015. result = mlock((void *) t2h_addr(cb, &s, arg1), arg2);
  1016. errcode = errno;
  1017. break;
  1018. case __NR_munlock:
  1019. result = munlock((void *) t2h_addr(cb, &s, arg1), arg2);
  1020. errcode = errno;
  1021. break;
  1022. case __NR_nanosleep:
  1023. {
  1024. struct timespec req, rem;
  1025. req = *((struct timespec *) t2h_addr(cb, &s, arg2));
  1026. translate_endian((void *) &req, sizeof(req));
  1027. result = nanosleep(&req, &rem);
  1028. errcode = errno;
  1029. if (result != 0)
  1030. break;
  1031. translate_endian((void *) &rem, sizeof(rem));
  1032. if ((s.write_mem) (cb, &s, arg2, (char *) &rem, sizeof(rem))
  1033. != sizeof(rem))
  1034. {
  1035. result = -1;
  1036. errcode = EINVAL;
  1037. }
  1038. }
  1039. break;
  1040. case __NR_mremap: /* FIXME */
  1041. result = (int) mremap((void *) t2h_addr(cb, &s, arg1), arg2, arg3, arg4);
  1042. errcode = errno;
  1043. break;
  1044. case __NR_getresuid32:
  1045. case __NR_getresuid:
  1046. {
  1047. uid_t ruid, euid, suid;
  1048. result = getresuid(&ruid, &euid, &suid);
  1049. errcode = errno;
  1050. if (result != 0)
  1051. break;
  1052. *((uid_t *) t2h_addr(cb, &s, arg1)) = conv_endian(ruid);
  1053. *((uid_t *) t2h_addr(cb, &s, arg2)) = conv_endian(euid);
  1054. *((uid_t *) t2h_addr(cb, &s, arg3)) = conv_endian(suid);
  1055. }
  1056. break;
  1057. case __NR_poll:
  1058. {
  1059. struct pollfd ufds;
  1060. ufds = *((struct pollfd *) t2h_addr(cb, &s, arg1));
  1061. ufds.fd = conv_endian(ufds.fd);
  1062. ufds.events = conv_endian16(ufds.events);
  1063. ufds.revents = conv_endian16(ufds.revents);
  1064. result = poll(&ufds, arg2, arg3);
  1065. errcode = errno;
  1066. }
  1067. break;
  1068. case __NR_getresgid32:
  1069. case __NR_getresgid:
  1070. {
  1071. uid_t rgid, egid, sgid;
  1072. result = getresgid(&rgid, &egid, &sgid);
  1073. errcode = errno;
  1074. if (result != 0)
  1075. break;
  1076. *((uid_t *) t2h_addr(cb, &s, arg1)) = conv_endian(rgid);
  1077. *((uid_t *) t2h_addr(cb, &s, arg2)) = conv_endian(egid);
  1078. *((uid_t *) t2h_addr(cb, &s, arg3)) = conv_endian(sgid);
  1079. }
  1080. break;
  1081. case __NR_pread:
  1082. result = pread(arg1, (void *) t2h_addr(cb, &s, arg2), arg3, arg4);
  1083. errcode = errno;
  1084. break;
  1085. case __NR_pwrite:
  1086. result = pwrite(arg1, (void *) t2h_addr(cb, &s, arg2), arg3, arg4);
  1087. errcode = errno;
  1088. break;
  1089. case __NR_chown32:
  1090. case __NR_chown:
  1091. result = chown((char *) t2h_addr(cb, &s, arg1), arg2, arg3);
  1092. errcode = errno;
  1093. break;
  1094. case __NR_getcwd:
  1095. result = (int) getcwd((char *) t2h_addr(cb, &s, arg1), arg2);
  1096. errcode = errno;
  1097. break;
  1098. case __NR_sendfile:
  1099. {
  1100. off_t offset;
  1101. offset = *((off_t *) t2h_addr(cb, &s, arg3));
  1102. offset = conv_endian(offset);
  1103. result = sendfile(arg1, arg2, &offset, arg3);
  1104. errcode = errno;
  1105. if (result != 0)
  1106. break;
  1107. *((off_t *) t2h_addr(cb, &s, arg3)) = conv_endian(offset);
  1108. }
  1109. break;
  1110. default:
  1111. result = -1;
  1112. errcode = ENOSYS;
  1113. break;
  1114. }
  1115. if (result == -1)
  1116. m32rbf_h_gr_set (current_cpu, 0, -errcode);
  1117. else
  1118. m32rbf_h_gr_set (current_cpu, 0, result);
  1119. break;
  1120. }
  1121. case TRAP_BREAKPOINT:
  1122. sim_engine_halt (sd, current_cpu, NULL, pc,
  1123. sim_stopped, SIM_SIGTRAP);
  1124. break;
  1125. case TRAP_FLUSH_CACHE:
  1126. /* Do nothing. */
  1127. break;
  1128. default :
  1129. {
  1130. /* Use cr5 as EVB (EIT Vector Base) register. */
  1131. USI new_pc = m32rbf_h_cr_get (current_cpu, 5) + 0x40 + num * 4;
  1132. return new_pc;
  1133. }
  1134. }
  1135. /* Fake an "rte" insn. */
  1136. /* FIXME: Should duplicate all of rte processing. */
  1137. return (pc & -4) + 4;
  1138. }