emul_netbsd.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479
  1. /* This file is part of the program psim.
  2. Copyright (C) 1994-1998, Andrew Cagney <cagney@highland.com.au>
  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. */
  14. #ifndef _EMUL_NETBSD_C_
  15. #define _EMUL_NETBSD_C_
  16. /* Note: this module is called via a table. There is no benefit in
  17. making it inline */
  18. #include "emul_generic.h"
  19. #include "emul_netbsd.h"
  20. #ifdef HAVE_STRING_H
  21. #include <string.h>
  22. #else
  23. #ifdef HAVE_STRINGS_H
  24. #include <strings.h>
  25. #endif
  26. #endif
  27. #include <sys/types.h>
  28. #include <sys/stat.h>
  29. #include <stdio.h>
  30. #include <signal.h>
  31. #include <fcntl.h>
  32. #include <errno.h>
  33. #include <sys/param.h>
  34. #include <sys/time.h>
  35. #ifdef HAVE_GETRUSAGE
  36. #ifndef HAVE_SYS_RESOURCE_H
  37. #undef HAVE_GETRUSAGE
  38. #endif
  39. #endif
  40. #ifdef HAVE_GETRUSAGE
  41. #include <sys/resource.h>
  42. int getrusage();
  43. #endif
  44. #if HAVE_SYS_IOCTL_H
  45. #include <sys/ioctl.h>
  46. #endif
  47. #if HAVE_DIRENT_H
  48. # include <dirent.h>
  49. # define NAMLEN(dirent) strlen((dirent)->d_name)
  50. #else
  51. # define dirent direct
  52. # define NAMLEN(dirent) (dirent)->d_namlen
  53. # if HAVE_SYS_NDIR_H
  54. # include <sys/ndir.h>
  55. # endif
  56. # if HAVE_SYS_DIR_H
  57. # include <sys/dir.h>
  58. # endif
  59. # if HAVE_NDIR_H
  60. # include <ndir.h>
  61. # endif
  62. #endif
  63. #ifdef HAVE_UNISTD_H
  64. #undef MAXPATHLEN /* sys/param.h might define this also */
  65. #include <unistd.h>
  66. #endif
  67. #ifdef HAVE_STDLIB_H
  68. #include <stdlib.h>
  69. #endif
  70. #define WITH_NetBSD_HOST (NetBSD >= 199306)
  71. #if WITH_NetBSD_HOST /* here NetBSD as that is what we're emulating */
  72. #include <sys/syscall.h> /* FIXME - should not be including this one */
  73. #include <sys/sysctl.h>
  74. #include <sys/mount.h>
  75. extern int getdirentries(int fd, char *buf, int nbytes, long *basep);
  76. /* NetBSD post 2.0 has the statfs system call (if COMPAT_20), but does
  77. not have struct statfs. In this case don't implement fstatfs.
  78. FIXME: Should implement fstatvfs. */
  79. #ifndef HAVE_STRUCT_STATFS
  80. #undef HAVE_FSTATFS
  81. #endif
  82. #else
  83. /* If this is not netbsd, don't allow fstatfs or getdirentries at this time */
  84. #undef HAVE_FSTATFS
  85. #undef HAVE_GETDIRENTRIES
  86. #endif
  87. #if (BSD < 199306) /* here BSD as just a bug */
  88. extern int errno;
  89. #endif
  90. #ifndef STATIC_INLINE_EMUL_NETBSD
  91. #define STATIC_INLINE_EMUL_NETBSD STATIC_INLINE
  92. #endif
  93. #if WITH_NetBSD_HOST
  94. #define SYS(X) ASSERT(call == (SYS_##X))
  95. #else
  96. #define SYS(X)
  97. #endif
  98. #if WITH_NetBSD_HOST && (PATH_MAX != 1024)
  99. #error "PATH_MAX not 1024"
  100. #elif !defined(PATH_MAX)
  101. #define PATH_MAX 1024
  102. #endif
  103. /* EMULATION
  104. NetBSD - Emulation of user programs for NetBSD/PPC
  105. DESCRIPTION
  106. */
  107. /* NetBSD's idea of what is needed to implement emulations */
  108. struct _os_emul_data {
  109. device *vm;
  110. emul_syscall *syscalls;
  111. };
  112. STATIC_INLINE_EMUL_NETBSD void
  113. write_stat(unsigned_word addr,
  114. struct stat buf,
  115. cpu *processor,
  116. unsigned_word cia)
  117. {
  118. H2T(buf.st_dev);
  119. H2T(buf.st_ino);
  120. H2T(buf.st_mode);
  121. H2T(buf.st_nlink);
  122. H2T(buf.st_uid);
  123. H2T(buf.st_gid);
  124. H2T(buf.st_size);
  125. H2T(buf.st_atime);
  126. /* H2T(buf.st_spare1); */
  127. H2T(buf.st_mtime);
  128. /* H2T(buf.st_spare2); */
  129. H2T(buf.st_ctime);
  130. /* H2T(buf.st_spare3); */
  131. #ifdef AC_STRUCT_ST_RDEV
  132. H2T(buf.st_rdev);
  133. #endif
  134. #ifdef AC_STRUCT_ST_BLKSIZE
  135. H2T(buf.st_blksize);
  136. #endif
  137. #ifdef AC_STRUCT_ST_BLOCKS
  138. H2T(buf.st_blocks);
  139. #endif
  140. #if WITH_NetBSD_HOST
  141. H2T(buf.st_flags);
  142. H2T(buf.st_gen);
  143. #endif
  144. emul_write_buffer(&buf, addr, sizeof(buf), processor, cia);
  145. }
  146. #ifdef HAVE_FSTATFS
  147. STATIC_INLINE_EMUL_NETBSD void
  148. write_statfs(unsigned_word addr,
  149. struct statfs buf,
  150. cpu *processor,
  151. unsigned_word cia)
  152. {
  153. H2T(buf.f_type);
  154. H2T(buf.f_flags);
  155. H2T(buf.f_bsize);
  156. H2T(buf.f_iosize);
  157. H2T(buf.f_blocks);
  158. H2T(buf.f_bfree);
  159. H2T(buf.f_bavail);
  160. H2T(buf.f_files);
  161. H2T(buf.f_ffree);
  162. H2T(buf.f_fsid.val[0]);
  163. H2T(buf.f_fsid.val[1]);
  164. H2T(buf.f_owner);
  165. /* f_spare[4]; */
  166. /* f_fstypename[MFSNAMELEN]; */
  167. /* f_mntonname[MNAMELEN]; */
  168. /* f_mntfromname[MNAMELEN]; */
  169. emul_write_buffer(&buf, addr, sizeof(buf), processor, cia);
  170. }
  171. #endif
  172. STATIC_INLINE_EMUL_NETBSD void
  173. write_timeval(unsigned_word addr,
  174. struct timeval t,
  175. cpu *processor,
  176. unsigned_word cia)
  177. {
  178. H2T(t.tv_sec);
  179. H2T(t.tv_usec);
  180. emul_write_buffer(&t, addr, sizeof(t), processor, cia);
  181. }
  182. #ifdef HAVE_GETTIMEOFDAY
  183. STATIC_INLINE_EMUL_NETBSD void
  184. write_timezone(unsigned_word addr,
  185. struct timezone tz,
  186. cpu *processor,
  187. unsigned_word cia)
  188. {
  189. H2T(tz.tz_minuteswest);
  190. H2T(tz.tz_dsttime);
  191. emul_write_buffer(&tz, addr, sizeof(tz), processor, cia);
  192. }
  193. #endif
  194. #ifdef HAVE_GETDIRENTRIES
  195. STATIC_INLINE_EMUL_NETBSD void
  196. write_direntries(unsigned_word addr,
  197. char *buf,
  198. int nbytes,
  199. cpu *processor,
  200. unsigned_word cia)
  201. {
  202. while (nbytes > 0) {
  203. struct dirent *out;
  204. struct dirent *in = (struct dirent*)buf;
  205. ASSERT(in->d_reclen <= nbytes);
  206. out = (struct dirent*)zalloc(in->d_reclen);
  207. memcpy(out/*dest*/, in/*src*/, in->d_reclen);
  208. H2T(out->d_fileno);
  209. H2T(out->d_reclen);
  210. H2T(out->d_type);
  211. H2T(out->d_namlen);
  212. emul_write_buffer(out, addr, in->d_reclen, processor, cia);
  213. nbytes -= in->d_reclen;
  214. addr += in->d_reclen;
  215. buf += in->d_reclen;
  216. free(out);
  217. }
  218. }
  219. #endif
  220. #ifdef HAVE_GETRUSAGE
  221. STATIC_INLINE_EMUL_NETBSD void
  222. write_rusage(unsigned_word addr,
  223. struct rusage rusage,
  224. cpu *processor,
  225. unsigned_word cia)
  226. {
  227. H2T(rusage.ru_utime.tv_sec); /* user time used */
  228. H2T(rusage.ru_utime.tv_usec);
  229. H2T(rusage.ru_stime.tv_sec); /* system time used */
  230. H2T(rusage.ru_stime.tv_usec);
  231. H2T(rusage.ru_maxrss); /* integral max resident set size */
  232. H2T(rusage.ru_ixrss); /* integral shared text memory size */
  233. H2T(rusage.ru_idrss); /* integral unshared data size */
  234. H2T(rusage.ru_isrss); /* integral unshared stack size */
  235. H2T(rusage.ru_minflt); /* page reclaims */
  236. H2T(rusage.ru_majflt); /* page faults */
  237. H2T(rusage.ru_nswap); /* swaps */
  238. H2T(rusage.ru_inblock); /* block input operations */
  239. H2T(rusage.ru_oublock); /* block output operations */
  240. H2T(rusage.ru_msgsnd); /* messages sent */
  241. H2T(rusage.ru_msgrcv); /* messages received */
  242. H2T(rusage.ru_nsignals); /* signals received */
  243. H2T(rusage.ru_nvcsw); /* voluntary context switches */
  244. H2T(rusage.ru_nivcsw); /* involuntary context switches */
  245. emul_write_buffer(&rusage, addr, sizeof(rusage), processor, cia);
  246. }
  247. #endif
  248. static void
  249. do_exit(os_emul_data *emul,
  250. unsigned call,
  251. const int arg0,
  252. cpu *processor,
  253. unsigned_word cia)
  254. {
  255. int status = (int)cpu_registers(processor)->gpr[arg0];
  256. SYS(exit);
  257. if (WITH_TRACE && ppc_trace[trace_os_emul])
  258. printf_filtered ("%d)\n", status);
  259. cpu_halt(processor, cia, was_exited, status);
  260. }
  261. static void
  262. do_read(os_emul_data *emul,
  263. unsigned call,
  264. const int arg0,
  265. cpu *processor,
  266. unsigned_word cia)
  267. {
  268. void *scratch_buffer;
  269. int d = (int)cpu_registers(processor)->gpr[arg0];
  270. unsigned_word buf = cpu_registers(processor)->gpr[arg0+1];
  271. int nbytes = cpu_registers(processor)->gpr[arg0+2];
  272. int status;
  273. SYS(read);
  274. if (WITH_TRACE && ppc_trace[trace_os_emul])
  275. printf_filtered ("%d, 0x%lx, %d", d, (long)buf, nbytes);
  276. /* get a tempoary bufer */
  277. scratch_buffer = zalloc(nbytes);
  278. /* check if buffer exists by reading it */
  279. emul_read_buffer(scratch_buffer, buf, nbytes, processor, cia);
  280. /* read */
  281. #if 0
  282. if (d == 0) {
  283. status = fread (scratch_buffer, 1, nbytes, stdin);
  284. if (status == 0 && ferror (stdin))
  285. status = -1;
  286. }
  287. #endif
  288. status = read (d, scratch_buffer, nbytes);
  289. emul_write_status(processor, status, errno);
  290. if (status > 0)
  291. emul_write_buffer(scratch_buffer, buf, status, processor, cia);
  292. free(scratch_buffer);
  293. }
  294. static void
  295. do_write(os_emul_data *emul,
  296. unsigned call,
  297. const int arg0,
  298. cpu *processor,
  299. unsigned_word cia)
  300. {
  301. void *scratch_buffer = NULL;
  302. int d = (int)cpu_registers(processor)->gpr[arg0];
  303. unsigned_word buf = cpu_registers(processor)->gpr[arg0+1];
  304. int nbytes = cpu_registers(processor)->gpr[arg0+2];
  305. int status;
  306. SYS(write);
  307. if (WITH_TRACE && ppc_trace[trace_os_emul])
  308. printf_filtered ("%d, 0x%lx, %d", d, (long)buf, nbytes);
  309. /* get a tempoary bufer */
  310. scratch_buffer = zalloc(nbytes); /* FIXME - nbytes == 0 */
  311. /* copy in */
  312. emul_read_buffer(scratch_buffer, buf, nbytes,
  313. processor, cia);
  314. /* write */
  315. status = write(d, scratch_buffer, nbytes);
  316. emul_write_status(processor, status, errno);
  317. free(scratch_buffer);
  318. flush_stdoutput();
  319. }
  320. static void
  321. do_open(os_emul_data *emul,
  322. unsigned call,
  323. const int arg0,
  324. cpu *processor,
  325. unsigned_word cia)
  326. {
  327. unsigned_word path_addr = cpu_registers(processor)->gpr[arg0];
  328. char path_buf[PATH_MAX];
  329. char *path = emul_read_string(path_buf, path_addr, PATH_MAX, processor, cia);
  330. int flags = (int)cpu_registers(processor)->gpr[arg0+1];
  331. int mode = (int)cpu_registers(processor)->gpr[arg0+2];
  332. int hostflags;
  333. int status;
  334. if (WITH_TRACE && ppc_trace[trace_os_emul])
  335. printf_filtered ("0x%lx [%s], 0x%x, 0x%x", (long)path_addr, path, flags, mode);
  336. SYS(open);
  337. /* Do some translation on 'flags' to match it to the host's version. */
  338. /* These flag values were taken from the NetBSD 1.4 header files. */
  339. if ((flags & 3) == 0)
  340. hostflags = O_RDONLY;
  341. else if ((flags & 3) == 1)
  342. hostflags = O_WRONLY;
  343. else
  344. hostflags = O_RDWR;
  345. if (flags & 0x00000008)
  346. hostflags |= O_APPEND;
  347. if (flags & 0x00000200)
  348. hostflags |= O_CREAT;
  349. if (flags & 0x00000400)
  350. hostflags |= O_TRUNC;
  351. if (flags & 0x00000800)
  352. hostflags |= O_EXCL;
  353. /* Can't combine these statements, cuz open sets errno. */
  354. status = open(path, hostflags, mode);
  355. emul_write_status(processor, status, errno);
  356. }
  357. static void
  358. do_close(os_emul_data *emul,
  359. unsigned call,
  360. const int arg0,
  361. cpu *processor,
  362. unsigned_word cia)
  363. {
  364. int d = (int)cpu_registers(processor)->gpr[arg0];
  365. int status;
  366. if (WITH_TRACE && ppc_trace[trace_os_emul])
  367. printf_filtered ("%d", d);
  368. SYS(close);
  369. /* Can't combine these statements, cuz close sets errno. */
  370. status = close(d);
  371. emul_write_status(processor, status, errno);
  372. }
  373. static void
  374. do_break(os_emul_data *emul,
  375. unsigned call,
  376. const int arg0,
  377. cpu *processor,
  378. unsigned_word cia)
  379. {
  380. /* just pass this onto the `vm' device */
  381. unsigned_word new_break = cpu_registers(processor)->gpr[arg0];
  382. int status;
  383. if (WITH_TRACE && ppc_trace[trace_os_emul])
  384. printf_filtered ("0x%lx", (long)cpu_registers(processor)->gpr[arg0]);
  385. SYS(break);
  386. status = device_ioctl(emul->vm,
  387. processor,
  388. cia,
  389. device_ioctl_break,
  390. new_break); /*ioctl-data*/
  391. emul_write_status(processor, 0, status);
  392. }
  393. #ifndef HAVE_GETPID
  394. #define do_getpid 0
  395. #else
  396. static void
  397. do_getpid(os_emul_data *emul,
  398. unsigned call,
  399. const int arg0,
  400. cpu *processor,
  401. unsigned_word cia)
  402. {
  403. SYS(getpid);
  404. emul_write_status(processor, (int)getpid(), 0);
  405. }
  406. #endif
  407. #ifndef HAVE_GETUID
  408. #define do_getuid 0
  409. #else
  410. static void
  411. do_getuid(os_emul_data *emul,
  412. unsigned call,
  413. const int arg0,
  414. cpu *processor,
  415. unsigned_word cia)
  416. {
  417. SYS(getuid);
  418. emul_write_status(processor, (int)getuid(), 0);
  419. }
  420. #endif
  421. #ifndef HAVE_GETEUID
  422. #define do_geteuid 0
  423. #else
  424. static void
  425. do_geteuid(os_emul_data *emul,
  426. unsigned call,
  427. const int arg0,
  428. cpu *processor,
  429. unsigned_word cia)
  430. {
  431. SYS(geteuid);
  432. emul_write_status(processor, (int)geteuid(), 0);
  433. }
  434. #endif
  435. #ifndef HAVE_KILL
  436. #define do_kill 0
  437. #else
  438. static void
  439. do_kill(os_emul_data *emul,
  440. unsigned call,
  441. const int arg0,
  442. cpu *processor,
  443. unsigned_word cia)
  444. {
  445. pid_t pid = cpu_registers(processor)->gpr[arg0];
  446. int sig = cpu_registers(processor)->gpr[arg0+1];
  447. if (WITH_TRACE && ppc_trace[trace_os_emul])
  448. printf_filtered ("%d, %d", (int)pid, sig);
  449. SYS(kill);
  450. printf_filtered("SYS_kill at 0x%lx - more to this than just being killed\n",
  451. (long)cia);
  452. cpu_halt(processor, cia, was_signalled, sig);
  453. }
  454. #endif
  455. #ifndef HAVE_DUP
  456. #define do_dup 0
  457. #else
  458. static void
  459. do_dup(os_emul_data *emul,
  460. unsigned call,
  461. const int arg0,
  462. cpu *processor,
  463. unsigned_word cia)
  464. {
  465. int oldd = cpu_registers(processor)->gpr[arg0];
  466. int status = dup(oldd);
  467. int err = errno;
  468. if (WITH_TRACE && ppc_trace[trace_os_emul])
  469. printf_filtered ("%d", oldd);
  470. SYS(dup);
  471. emul_write_status(processor, status, err);
  472. }
  473. #endif
  474. #ifndef HAVE_GETEGID
  475. #define do_getegid 0
  476. #else
  477. static void
  478. do_getegid(os_emul_data *emul,
  479. unsigned call,
  480. const int arg0,
  481. cpu *processor,
  482. unsigned_word cia)
  483. {
  484. SYS(getegid);
  485. emul_write_status(processor, (int)getegid(), 0);
  486. }
  487. #endif
  488. #ifndef HAVE_GETGID
  489. #define do_getgid 0
  490. #else
  491. static void
  492. do_getgid(os_emul_data *emul,
  493. unsigned call,
  494. const int arg0,
  495. cpu *processor,
  496. unsigned_word cia)
  497. {
  498. SYS(getgid);
  499. emul_write_status(processor, (int)getgid(), 0);
  500. }
  501. #endif
  502. #ifndef HAVE_SIGPROCMASK
  503. #define do_sigprocmask 0
  504. #else
  505. static void
  506. do_sigprocmask(os_emul_data *emul,
  507. unsigned call,
  508. const int arg0,
  509. cpu *processor,
  510. unsigned_word cia)
  511. {
  512. natural_word how = cpu_registers(processor)->gpr[arg0];
  513. unsigned_word set = cpu_registers(processor)->gpr[arg0+1];
  514. unsigned_word oset = cpu_registers(processor)->gpr[arg0+2];
  515. #ifdef SYS_sigprocmask
  516. SYS(sigprocmask);
  517. #endif
  518. if (WITH_TRACE && ppc_trace[trace_os_emul])
  519. printf_filtered ("%ld, 0x%ld, 0x%ld", (long)how, (long)set, (long)oset);
  520. emul_write_status(processor, 0, 0);
  521. cpu_registers(processor)->gpr[4] = set;
  522. }
  523. #endif
  524. #ifndef HAVE_IOCTL
  525. #define do_ioctl 0
  526. #else
  527. static void
  528. do_ioctl(os_emul_data *emul,
  529. unsigned call,
  530. const int arg0,
  531. cpu *processor,
  532. unsigned_word cia)
  533. {
  534. int d = cpu_registers(processor)->gpr[arg0];
  535. unsigned request = cpu_registers(processor)->gpr[arg0+1];
  536. unsigned_word argp_addr = cpu_registers(processor)->gpr[arg0+2];
  537. #if !WITH_NetBSD_HOST
  538. cpu_registers(processor)->gpr[arg0] = 0; /* just succeed */
  539. #else
  540. unsigned dir = request & IOC_DIRMASK;
  541. int status;
  542. SYS(ioctl);
  543. /* what we haven't done */
  544. if (dir & IOC_IN /* write into the io device */
  545. || dir & IOC_OUT
  546. || !(dir & IOC_VOID))
  547. error("do_ioctl() read or write of parameter not implemented\n");
  548. status = ioctl(d, request, NULL);
  549. emul_write_status(processor, status, errno);
  550. #endif
  551. if (WITH_TRACE && ppc_trace[trace_os_emul])
  552. printf_filtered ("%d, 0x%x, 0x%lx", d, request, (long)argp_addr);
  553. }
  554. #endif
  555. #ifndef HAVE_UMASK
  556. #define do_umask 0
  557. #else
  558. static void
  559. do_umask(os_emul_data *emul,
  560. unsigned call,
  561. const int arg0,
  562. cpu *processor,
  563. unsigned_word cia)
  564. {
  565. int mask = cpu_registers(processor)->gpr[arg0];
  566. if (WITH_TRACE && ppc_trace[trace_os_emul])
  567. printf_filtered ("0%o", mask);
  568. SYS(umask);
  569. emul_write_status(processor, umask(mask), 0);
  570. }
  571. #endif
  572. #ifndef HAVE_DUP2
  573. #define do_dup2 0
  574. #else
  575. static void
  576. do_dup2(os_emul_data *emul,
  577. unsigned call,
  578. const int arg0,
  579. cpu *processor,
  580. unsigned_word cia)
  581. {
  582. int oldd = cpu_registers(processor)->gpr[arg0];
  583. int newd = cpu_registers(processor)->gpr[arg0+1];
  584. int status = dup2(oldd, newd);
  585. int err = errno;
  586. if (WITH_TRACE && ppc_trace[trace_os_emul])
  587. printf_filtered ("%d, %d", oldd, newd);
  588. SYS(dup2);
  589. emul_write_status(processor, status, err);
  590. }
  591. #endif
  592. #ifndef HAVE_FCNTL
  593. #define do_fcntl 0
  594. #else
  595. static void
  596. do_fcntl(os_emul_data *emul,
  597. unsigned call,
  598. const int arg0,
  599. cpu *processor,
  600. unsigned_word cia)
  601. {
  602. int fd = cpu_registers(processor)->gpr[arg0];
  603. int cmd = cpu_registers(processor)->gpr[arg0+1];
  604. int arg = cpu_registers(processor)->gpr[arg0+2];
  605. int status;
  606. if (WITH_TRACE && ppc_trace[trace_os_emul])
  607. printf_filtered ("%d, %d, %d", fd, cmd, arg);
  608. SYS(fcntl);
  609. status = fcntl(fd, cmd, arg);
  610. emul_write_status(processor, status, errno);
  611. }
  612. #endif
  613. #ifndef HAVE_GETTIMEOFDAY
  614. #define do_gettimeofday 0
  615. #else
  616. static void
  617. do_gettimeofday(os_emul_data *emul,
  618. unsigned call,
  619. const int arg0,
  620. cpu *processor,
  621. unsigned_word cia)
  622. {
  623. unsigned_word t_addr = cpu_registers(processor)->gpr[arg0];
  624. unsigned_word tz_addr = cpu_registers(processor)->gpr[arg0+1];
  625. struct timeval t;
  626. struct timezone tz;
  627. int status = gettimeofday((t_addr != 0 ? &t : NULL),
  628. (tz_addr != 0 ? &tz : NULL));
  629. int err = errno;
  630. if (WITH_TRACE && ppc_trace[trace_os_emul])
  631. printf_filtered ("0x%lx, 0x%lx", (long)t_addr, (long)tz_addr);
  632. SYS(gettimeofday);
  633. emul_write_status(processor, status, err);
  634. if (status == 0) {
  635. if (t_addr != 0)
  636. write_timeval(t_addr, t, processor, cia);
  637. if (tz_addr != 0)
  638. write_timezone(tz_addr, tz, processor, cia);
  639. }
  640. }
  641. #endif
  642. #ifndef HAVE_GETRUSAGE
  643. #define do_getrusage 0
  644. #else
  645. static void
  646. do_getrusage(os_emul_data *emul,
  647. unsigned call,
  648. const int arg0,
  649. cpu *processor,
  650. unsigned_word cia)
  651. {
  652. int who = cpu_registers(processor)->gpr[arg0];
  653. unsigned_word rusage_addr = cpu_registers(processor)->gpr[arg0+1];
  654. struct rusage rusage;
  655. int status = getrusage(who, (rusage_addr != 0 ? &rusage : NULL));
  656. int err = errno;
  657. if (WITH_TRACE && ppc_trace[trace_os_emul])
  658. printf_filtered ("%d, 0x%lx", who, (long)rusage_addr);
  659. SYS(getrusage);
  660. emul_write_status(processor, status, err);
  661. if (status == 0) {
  662. if (rusage_addr != 0)
  663. write_rusage(rusage_addr, rusage, processor, cia);
  664. }
  665. }
  666. #endif
  667. #ifndef HAVE_FSTATFS
  668. #define do_fstatfs 0
  669. #else
  670. static void
  671. do_fstatfs(os_emul_data *emul,
  672. unsigned call,
  673. const int arg0,
  674. cpu *processor,
  675. unsigned_word cia)
  676. {
  677. int fd = cpu_registers(processor)->gpr[arg0];
  678. unsigned_word buf_addr = cpu_registers(processor)->gpr[arg0+1];
  679. struct statfs buf;
  680. int status;
  681. if (WITH_TRACE && ppc_trace[trace_os_emul])
  682. printf_filtered ("%d, 0x%lx", fd, (long)buf_addr);
  683. SYS(fstatfs);
  684. status = fstatfs(fd, (buf_addr == 0 ? NULL : &buf));
  685. emul_write_status(processor, status, errno);
  686. if (status == 0) {
  687. if (buf_addr != 0)
  688. write_statfs(buf_addr, buf, processor, cia);
  689. }
  690. }
  691. #endif
  692. #ifndef HAVE_STAT
  693. #define do_stat 0
  694. #else
  695. static void
  696. do_stat(os_emul_data *emul,
  697. unsigned call,
  698. const int arg0,
  699. cpu *processor,
  700. unsigned_word cia)
  701. {
  702. char path_buf[PATH_MAX];
  703. unsigned_word path_addr = cpu_registers(processor)->gpr[arg0];
  704. unsigned_word stat_buf_addr = cpu_registers(processor)->gpr[arg0+1];
  705. char *path = emul_read_string(path_buf, path_addr, PATH_MAX, processor, cia);
  706. struct stat buf;
  707. int status;
  708. #ifdef SYS_stat
  709. SYS(stat);
  710. #endif
  711. status = stat(path, &buf);
  712. emul_write_status(processor, status, errno);
  713. if (status == 0)
  714. write_stat(stat_buf_addr, buf, processor, cia);
  715. }
  716. #endif
  717. #ifndef HAVE_FSTAT
  718. #define do_fstat 0
  719. #else
  720. static void
  721. do_fstat(os_emul_data *emul,
  722. unsigned call,
  723. const int arg0,
  724. cpu *processor,
  725. unsigned_word cia)
  726. {
  727. int fd = cpu_registers(processor)->gpr[arg0];
  728. unsigned_word stat_buf_addr = cpu_registers(processor)->gpr[arg0+1];
  729. struct stat buf;
  730. int status;
  731. #ifdef SYS_fstat
  732. SYS(fstat);
  733. #endif
  734. /* Can't combine these statements, cuz fstat sets errno. */
  735. status = fstat(fd, &buf);
  736. emul_write_status(processor, status, errno);
  737. write_stat(stat_buf_addr, buf, processor, cia);
  738. }
  739. #endif
  740. #ifndef HAVE_LSTAT
  741. #define do_lstat 0
  742. #else
  743. static void
  744. do_lstat(os_emul_data *emul,
  745. unsigned call,
  746. const int arg0,
  747. cpu *processor,
  748. unsigned_word cia)
  749. {
  750. char path_buf[PATH_MAX];
  751. unsigned_word path_addr = cpu_registers(processor)->gpr[arg0];
  752. char *path = emul_read_string(path_buf, path_addr, PATH_MAX, processor, cia);
  753. unsigned_word stat_buf_addr = cpu_registers(processor)->gpr[arg0+1];
  754. struct stat buf;
  755. int status;
  756. #ifdef SYS_lstat
  757. SYS(lstat);
  758. #endif
  759. /* Can't combine these statements, cuz lstat sets errno. */
  760. status = lstat(path, &buf);
  761. emul_write_status(processor, status, errno);
  762. write_stat(stat_buf_addr, buf, processor, cia);
  763. }
  764. #endif
  765. #ifndef HAVE_GETDIRENTRIES
  766. #define do_getdirentries 0
  767. #else
  768. static void
  769. do_getdirentries(os_emul_data *emul,
  770. unsigned call,
  771. const int arg0,
  772. cpu *processor,
  773. unsigned_word cia)
  774. {
  775. int fd = cpu_registers(processor)->gpr[arg0];
  776. unsigned_word buf_addr = cpu_registers(processor)->gpr[arg0+1];
  777. char *buf;
  778. int nbytes = cpu_registers(processor)->gpr[arg0+2];
  779. unsigned_word basep_addr = cpu_registers(processor)->gpr[arg0+3];
  780. long basep;
  781. int status;
  782. #ifdef SYS_getdirentries
  783. SYS(getdirentries);
  784. #endif
  785. if (buf_addr != 0 && nbytes >= 0)
  786. buf = zalloc(nbytes);
  787. else
  788. buf = NULL;
  789. status = getdirentries(fd,
  790. (buf_addr == 0 ? NULL : buf),
  791. nbytes,
  792. (basep_addr == 0 ? NULL : &basep));
  793. emul_write_status(processor, status, errno);
  794. if (basep_addr != 0)
  795. emul_write_word(basep_addr, basep, processor, cia);
  796. if (status > 0)
  797. write_direntries(buf_addr, buf, status, processor, cia);
  798. if (buf != NULL)
  799. free(buf);
  800. }
  801. #endif
  802. static void
  803. do___syscall(os_emul_data *emul,
  804. unsigned call,
  805. const int arg0,
  806. cpu *processor,
  807. unsigned_word cia)
  808. {
  809. SYS(__syscall);
  810. emul_do_system_call(emul,
  811. emul->syscalls,
  812. cpu_registers(processor)->gpr[arg0],
  813. arg0 + 1,
  814. processor,
  815. cia);
  816. }
  817. #ifndef HAVE_LSEEK
  818. #define do_lseek 0
  819. #else
  820. static void
  821. do_lseek(os_emul_data *emul,
  822. unsigned call,
  823. const int arg0,
  824. cpu *processor,
  825. unsigned_word cia)
  826. {
  827. int fildes = cpu_registers(processor)->gpr[arg0];
  828. off_t offset = emul_read_gpr64(processor, arg0+2);
  829. int whence = cpu_registers(processor)->gpr[arg0+4];
  830. off_t status;
  831. SYS(lseek);
  832. status = lseek(fildes, offset, whence);
  833. if (status == -1)
  834. emul_write_status(processor, -1, errno);
  835. else {
  836. emul_write_status(processor, 0, 0); /* success */
  837. emul_write_gpr64(processor, 3, status);
  838. }
  839. }
  840. #endif
  841. static void
  842. do___sysctl(os_emul_data *emul,
  843. unsigned call,
  844. const int arg0,
  845. cpu *processor,
  846. unsigned_word cia)
  847. {
  848. /* call the arguments by their real name */
  849. unsigned_word name = cpu_registers(processor)->gpr[arg0];
  850. natural_word namelen = cpu_registers(processor)->gpr[arg0+1];
  851. unsigned_word oldp = cpu_registers(processor)->gpr[arg0+2];
  852. unsigned_word oldlenp = cpu_registers(processor)->gpr[arg0+3];
  853. natural_word oldlen;
  854. natural_word mib;
  855. natural_word int_val;
  856. SYS(__sysctl);
  857. /* pluck out the management information base id */
  858. if (namelen < 1)
  859. error("system_call()SYS___sysctl bad name[0]\n");
  860. mib = vm_data_map_read_word(cpu_data_map(processor),
  861. name,
  862. processor,
  863. cia);
  864. name += sizeof(mib);
  865. /* see what to do with it ... */
  866. switch ((int)mib) {
  867. case 6/*CTL_HW*/:
  868. #if WITH_NetBSD_HOST && (CTL_HW != 6)
  869. # error "CTL_HW"
  870. #endif
  871. if (namelen < 2)
  872. error("system_call()SYS___sysctl - CTL_HW - bad name[1]\n");
  873. mib = vm_data_map_read_word(cpu_data_map(processor),
  874. name,
  875. processor,
  876. cia);
  877. name += sizeof(mib);
  878. switch ((int)mib) {
  879. case 7/*HW_PAGESIZE*/:
  880. #if WITH_NetBSD_HOST && (HW_PAGESIZE != 7)
  881. # error "HW_PAGESIZE"
  882. #endif
  883. oldlen = vm_data_map_read_word(cpu_data_map(processor),
  884. oldlenp,
  885. processor,
  886. cia);
  887. if (sizeof(natural_word) > oldlen)
  888. error("system_call()sysctl - CTL_HW.HW_PAGESIZE - to small\n");
  889. int_val = 8192;
  890. oldlen = sizeof(int_val);
  891. emul_write_word(oldp, int_val, processor, cia);
  892. emul_write_word(oldlenp, oldlen, processor, cia);
  893. break;
  894. default:
  895. error("sysctl() CTL_HW.%d unknown\n", mib);
  896. break;
  897. }
  898. break;
  899. default:
  900. error("sysctl() name[0]=%d unknown\n", (int)mib);
  901. break;
  902. }
  903. emul_write_status(processor, 0, 0); /* always succeed */
  904. }
  905. static emul_syscall_descriptor netbsd_descriptors[] = {
  906. /* 0 */ { 0, "syscall" },
  907. /* 1 */ { do_exit, "exit" },
  908. /* 2 */ { 0, "fork" },
  909. /* 3 */ { do_read, "read" },
  910. /* 4 */ { do_write, "write" },
  911. /* 5 */ { do_open, "open" },
  912. /* 6 */ { do_close, "close" },
  913. /* 7 */ { 0, "wait4" },
  914. { 0, }, /* 8 is old creat */
  915. /* 9 */ { 0, "link" },
  916. /* 10 */ { 0, "unlink" },
  917. { 0, }, /* 11 is obsolete execv */
  918. /* 12 */ { 0, "chdir" },
  919. /* 13 */ { 0, "fchdir" },
  920. /* 14 */ { 0, "mknod" },
  921. /* 15 */ { 0, "chmod" },
  922. /* 16 */ { 0, "chown" },
  923. /* 17 */ { do_break, "break" },
  924. /* 18 */ { 0, "getfsstat" },
  925. { 0, }, /* 19 is old lseek */
  926. /* 20 */ { do_getpid, "getpid" },
  927. /* 21 */ { 0, "mount" },
  928. /* 22 */ { 0, "unmount" },
  929. /* 23 */ { 0, "setuid" },
  930. /* 24 */ { do_getuid, "getuid" },
  931. /* 25 */ { do_geteuid, "geteuid" },
  932. /* 26 */ { 0, "ptrace" },
  933. /* 27 */ { 0, "recvmsg" },
  934. /* 28 */ { 0, "sendmsg" },
  935. /* 29 */ { 0, "recvfrom" },
  936. /* 30 */ { 0, "accept" },
  937. /* 31 */ { 0, "getpeername" },
  938. /* 32 */ { 0, "getsockname" },
  939. /* 33 */ { 0, "access" },
  940. /* 34 */ { 0, "chflags" },
  941. /* 35 */ { 0, "fchflags" },
  942. /* 36 */ { 0, "sync" },
  943. /* 37 */ { do_kill, "kill" },
  944. { 0, }, /* 38 is old stat */
  945. /* 39 */ { 0, "getppid" },
  946. { 0, }, /* 40 is old lstat */
  947. /* 41 */ { do_dup, "dup" },
  948. /* 42 */ { 0, "pipe" },
  949. /* 43 */ { do_getegid, "getegid" },
  950. /* 44 */ { 0, "profil" },
  951. /* 45 */ { 0, "ktrace" },
  952. /* 46 */ { 0, "sigaction" },
  953. /* 47 */ { do_getgid, "getgid" },
  954. /* 48 */ { do_sigprocmask, "sigprocmask" },
  955. /* 49 */ { 0, "getlogin" },
  956. /* 50 */ { 0, "setlogin" },
  957. /* 51 */ { 0, "acct" },
  958. /* 52 */ { 0, "sigpending" },
  959. /* 53 */ { 0, "sigaltstack" },
  960. /* 54 */ { do_ioctl, "ioctl" },
  961. /* 55 */ { 0, "reboot" },
  962. /* 56 */ { 0, "revoke" },
  963. /* 57 */ { 0, "symlink" },
  964. /* 58 */ { 0, "readlink" },
  965. /* 59 */ { 0, "execve" },
  966. /* 60 */ { do_umask, "umask" },
  967. /* 61 */ { 0, "chroot" },
  968. { 0, }, /* 62 is old fstat */
  969. { 0, }, /* 63 is old getkerninfo */
  970. { 0, }, /* 64 is old getpagesize */
  971. /* 65 */ { 0, "msync" },
  972. /* 66 */ { 0, "vfork" },
  973. { 0, }, /* 67 is obsolete vread */
  974. { 0, }, /* 68 is obsolete vwrite */
  975. /* 69 */ { 0, "sbrk" },
  976. /* 70 */ { 0, "sstk" },
  977. { 0, }, /* 71 is old mmap */
  978. /* 72 */ { 0, "vadvise" },
  979. /* 73 */ { 0, "munmap" },
  980. /* 74 */ { 0, "mprotect" },
  981. /* 75 */ { 0, "madvise" },
  982. { 0, }, /* 76 is obsolete vhangup */
  983. { 0, }, /* 77 is obsolete vlimit */
  984. /* 78 */ { 0, "mincore" },
  985. /* 79 */ { 0, "getgroups" },
  986. /* 80 */ { 0, "setgroups" },
  987. /* 81 */ { 0, "getpgrp" },
  988. /* 82 */ { 0, "setpgid" },
  989. /* 83 */ { 0, "setitimer" },
  990. { 0, }, /* 84 is old wait */
  991. /* 85 */ { 0, "swapon" },
  992. /* 86 */ { 0, "getitimer" },
  993. { 0, }, /* 87 is old gethostname */
  994. { 0, }, /* 88 is old sethostname */
  995. { 0, }, /* 89 is old getdtablesize */
  996. { do_dup2, "dup2" },
  997. { 0, }, /* 91 */
  998. /* 92 */ { do_fcntl, "fcntl" },
  999. /* 93 */ { 0, "select" },
  1000. { 0, }, /* 94 */
  1001. /* 95 */ { 0, "fsync" },
  1002. /* 96 */ { 0, "setpriority" },
  1003. /* 97 */ { 0, "socket" },
  1004. /* 98 */ { 0, "connect" },
  1005. { 0, }, /* 99 is old accept */
  1006. /* 100 */ { 0, "getpriority" },
  1007. { 0, }, /* 101 is old send */
  1008. { 0, }, /* 102 is old recv */
  1009. /* 103 */ { 0, "sigreturn" },
  1010. /* 104 */ { 0, "bind" },
  1011. /* 105 */ { 0, "setsockopt" },
  1012. /* 106 */ { 0, "listen" },
  1013. { 0, }, /* 107 is obsolete vtimes */
  1014. { 0, }, /* 108 is old sigvec */
  1015. { 0, }, /* 109 is old sigblock */
  1016. { 0, }, /* 110 is old sigsetmask */
  1017. /* 111 */ { 0, "sigsuspend" },
  1018. { 0, }, /* 112 is old sigstack */
  1019. { 0, }, /* 113 is old recvmsg */
  1020. { 0, }, /* 114 is old sendmsg */
  1021. /* - is obsolete vtrace */ { 0, "vtrace 115" },
  1022. /* 116 */ { do_gettimeofday, "gettimeofday" },
  1023. /* 117 */ { do_getrusage, "getrusage" },
  1024. /* 118 */ { 0, "getsockopt" },
  1025. /* 119 */ { 0, "resuba" },
  1026. /* 120 */ { 0, "readv" },
  1027. /* 121 */ { 0, "writev" },
  1028. /* 122 */ { 0, "settimeofday" },
  1029. /* 123 */ { 0, "fchown" },
  1030. /* 124 */ { 0, "fchmod" },
  1031. { 0, }, /* 125 is old recvfrom */
  1032. { 0, }, /* 126 is old setreuid */
  1033. { 0, }, /* 127 is old setregid */
  1034. /* 128 */ { 0, "rename" },
  1035. { 0, }, /* 129 is old truncate */
  1036. { 0, }, /* 130 is old ftruncate */
  1037. /* 131 */ { 0, "flock" },
  1038. /* 132 */ { 0, "mkfifo" },
  1039. /* 133 */ { 0, "sendto" },
  1040. /* 134 */ { 0, "shutdown" },
  1041. /* 135 */ { 0, "socketpair" },
  1042. /* 136 */ { 0, "mkdir" },
  1043. /* 137 */ { 0, "rmdir" },
  1044. /* 138 */ { 0, "utimes" },
  1045. { 0, }, /* 139 is obsolete 4.2 sigreturn */
  1046. /* 140 */ { 0, "adjtime" },
  1047. { 0, }, /* 141 is old getpeername */
  1048. { 0, }, /* 142 is old gethostid */
  1049. { 0, }, /* 143 is old sethostid */
  1050. { 0, }, /* 144 is old getrlimit */
  1051. { 0, }, /* 145 is old setrlimit */
  1052. { 0, }, /* 146 is old killpg */
  1053. /* 147 */ { 0, "setsid" },
  1054. /* 148 */ { 0, "quotactl" },
  1055. { 0, }, /* 149 is old quota */
  1056. { 0, }, /* 150 is old getsockname */
  1057. { 0, }, /* 151 */
  1058. { 0, }, /* 152 */
  1059. { 0, }, /* 153 */
  1060. { 0, }, /* 154 */
  1061. /* 155 */ { 0, "nfssvc" },
  1062. { 0, }, /* 156 is old getdirentries */
  1063. /* 157 */ { 0, "statfs" },
  1064. /* 158 */ { do_fstatfs, "fstatfs" },
  1065. { 0, }, /* 159 */
  1066. { 0, }, /* 160 */
  1067. /* 161 */ { 0, "getfh" },
  1068. { 0, }, /* 162 is old getdomainname */
  1069. { 0, }, /* 163 is old setdomainname */
  1070. { 0, }, /* 164 is old uname */
  1071. /* 165 */ { 0, "sysarch" },
  1072. { 0, }, /* 166 */
  1073. { 0, }, /* 167 */
  1074. { 0, }, /* 168 */
  1075. /* 169 */ { 0, "semsys" },
  1076. /* 170 */ { 0, "msgsys" },
  1077. /* 171 */ { 0, "shmsys" },
  1078. { 0, }, /* 172 */
  1079. { 0, }, /* 173 */
  1080. { 0, }, /* 174 */
  1081. { 0, }, /* 175 */
  1082. { 0, }, /* 176 */
  1083. { 0, }, /* 177 */
  1084. { 0, }, /* 178 */
  1085. { 0, }, /* 179 */
  1086. { 0, }, /* 180 */
  1087. /* 181 */ { 0, "setgid" },
  1088. /* 182 */ { 0, "setegid" },
  1089. /* 183 */ { 0, "seteuid" },
  1090. /* 184 */ { 0, "lfs_bmapv" },
  1091. /* 185 */ { 0, "lfs_markv" },
  1092. /* 186 */ { 0, "lfs_segclean" },
  1093. /* 187 */ { 0, "lfs_segwait" },
  1094. /* 188 */ { do_stat, "stat" },
  1095. /* 189 */ { do_fstat, "fstat" },
  1096. /* 190 */ { do_lstat, "lstat" },
  1097. /* 191 */ { 0, "pathconf" },
  1098. /* 192 */ { 0, "fpathconf" },
  1099. { 0, }, /* 193 */
  1100. /* 194 */ { 0, "getrlimit" },
  1101. /* 195 */ { 0, "setrlimit" },
  1102. /* 196 */ { do_getdirentries, "getdirentries" },
  1103. /* 197 */ { 0, "mmap" },
  1104. /* 198 */ { do___syscall, "__syscall" },
  1105. /* 199 */ { do_lseek, "lseek" },
  1106. /* 200 */ { 0, "truncate" },
  1107. /* 201 */ { 0, "ftruncate" },
  1108. /* 202 */ { do___sysctl, "__sysctl" },
  1109. /* 203 */ { 0, "mlock" },
  1110. /* 204 */ { 0, "munlock" },
  1111. };
  1112. static char *(netbsd_error_names[]) = {
  1113. /* 0 */ "ESUCCESS",
  1114. /* 1 */ "EPERM",
  1115. /* 2 */ "ENOENT",
  1116. /* 3 */ "ESRCH",
  1117. /* 4 */ "EINTR",
  1118. /* 5 */ "EIO",
  1119. /* 6 */ "ENXIO",
  1120. /* 7 */ "E2BIG",
  1121. /* 8 */ "ENOEXEC",
  1122. /* 9 */ "EBADF",
  1123. /* 10 */ "ECHILD",
  1124. /* 11 */ "EDEADLK",
  1125. /* 12 */ "ENOMEM",
  1126. /* 13 */ "EACCES",
  1127. /* 14 */ "EFAULT",
  1128. /* 15 */ "ENOTBLK",
  1129. /* 16 */ "EBUSY",
  1130. /* 17 */ "EEXIST",
  1131. /* 18 */ "EXDEV",
  1132. /* 19 */ "ENODEV",
  1133. /* 20 */ "ENOTDIR",
  1134. /* 21 */ "EISDIR",
  1135. /* 22 */ "EINVAL",
  1136. /* 23 */ "ENFILE",
  1137. /* 24 */ "EMFILE",
  1138. /* 25 */ "ENOTTY",
  1139. /* 26 */ "ETXTBSY",
  1140. /* 27 */ "EFBIG",
  1141. /* 28 */ "ENOSPC",
  1142. /* 29 */ "ESPIPE",
  1143. /* 30 */ "EROFS",
  1144. /* 31 */ "EMLINK",
  1145. /* 32 */ "EPIPE",
  1146. /* 33 */ "EDOM",
  1147. /* 34 */ "ERANGE",
  1148. /* 35 */ "EAGAIN",
  1149. /* 36 */ "EINPROGRESS",
  1150. /* 37 */ "EALREADY",
  1151. /* 38 */ "ENOTSOCK",
  1152. /* 39 */ "EDESTADDRREQ",
  1153. /* 40 */ "EMSGSIZE",
  1154. /* 41 */ "EPROTOTYPE",
  1155. /* 42 */ "ENOPROTOOPT",
  1156. /* 43 */ "EPROTONOSUPPORT",
  1157. /* 44 */ "ESOCKTNOSUPPORT",
  1158. /* 45 */ "EOPNOTSUPP",
  1159. /* 46 */ "EPFNOSUPPORT",
  1160. /* 47 */ "EAFNOSUPPORT",
  1161. /* 48 */ "EADDRINUSE",
  1162. /* 49 */ "EADDRNOTAVAIL",
  1163. /* 50 */ "ENETDOWN",
  1164. /* 51 */ "ENETUNREACH",
  1165. /* 52 */ "ENETRESET",
  1166. /* 53 */ "ECONNABORTED",
  1167. /* 54 */ "ECONNRESET",
  1168. /* 55 */ "ENOBUFS",
  1169. /* 56 */ "EISCONN",
  1170. /* 57 */ "ENOTCONN",
  1171. /* 58 */ "ESHUTDOWN",
  1172. /* 59 */ "ETOOMANYREFS",
  1173. /* 60 */ "ETIMEDOUT",
  1174. /* 61 */ "ECONNREFUSED",
  1175. /* 62 */ "ELOOP",
  1176. /* 63 */ "ENAMETOOLONG",
  1177. /* 64 */ "EHOSTDOWN",
  1178. /* 65 */ "EHOSTUNREACH",
  1179. /* 66 */ "ENOTEMPTY",
  1180. /* 67 */ "EPROCLIM",
  1181. /* 68 */ "EUSERS",
  1182. /* 69 */ "EDQUOT",
  1183. /* 70 */ "ESTALE",
  1184. /* 71 */ "EREMOTE",
  1185. /* 72 */ "EBADRPC",
  1186. /* 73 */ "ERPCMISMATCH",
  1187. /* 74 */ "EPROGUNAVAIL",
  1188. /* 75 */ "EPROGMISMATCH",
  1189. /* 76 */ "EPROCUNAVAIL",
  1190. /* 77 */ "ENOLCK",
  1191. /* 78 */ "ENOSYS",
  1192. /* 79 */ "EFTYPE",
  1193. /* 80 */ "EAUTH",
  1194. /* 81 */ "ENEEDAUTH",
  1195. /* 81 */ "ELAST",
  1196. };
  1197. static char *(netbsd_signal_names[]) = {
  1198. /* 0 */ 0,
  1199. /* 1 */ "SIGHUP",
  1200. /* 2 */ "SIGINT",
  1201. /* 3 */ "SIGQUIT",
  1202. /* 4 */ "SIGILL",
  1203. /* 5 */ "SIGTRAP",
  1204. /* 6 */ "SIGABRT",
  1205. /* 7 */ "SIGEMT",
  1206. /* 8 */ "SIGFPE",
  1207. /* 9 */ "SIGKILL",
  1208. /* 10 */ "SIGBUS",
  1209. /* 11 */ "SIGSEGV",
  1210. /* 12 */ "SIGSYS",
  1211. /* 13 */ "SIGPIPE",
  1212. /* 14 */ "SIGALRM",
  1213. /* 15 */ "SIGTERM",
  1214. /* 16 */ "SIGURG",
  1215. /* 17 */ "SIGSTOP",
  1216. /* 18 */ "SIGTSTP",
  1217. /* 19 */ "SIGCONT",
  1218. /* 20 */ "SIGCHLD",
  1219. /* 21 */ "SIGTTIN",
  1220. /* 22 */ "SIGTTOU",
  1221. /* 23 */ "SIGIO",
  1222. /* 24 */ "SIGXCPU",
  1223. /* 25 */ "SIGXFSZ",
  1224. /* 26 */ "SIGVTALRM",
  1225. /* 27 */ "SIGPROF",
  1226. /* 28 */ "SIGWINCH",
  1227. /* 29 */ "SIGINFO",
  1228. /* 30 */ "SIGUSR1",
  1229. /* 31 */ "SIGUSR2",
  1230. };
  1231. static emul_syscall emul_netbsd_syscalls = {
  1232. netbsd_descriptors,
  1233. sizeof(netbsd_descriptors) / sizeof(netbsd_descriptors[0]),
  1234. netbsd_error_names,
  1235. sizeof(netbsd_error_names) / sizeof(netbsd_error_names[0]),
  1236. netbsd_signal_names,
  1237. sizeof(netbsd_signal_names) / sizeof(netbsd_signal_names[0]),
  1238. };
  1239. /* NetBSD's os_emul interface, most are just passed on to the generic
  1240. syscall stuff */
  1241. static os_emul_data *
  1242. emul_netbsd_create(device *root,
  1243. bfd *image,
  1244. const char *name)
  1245. {
  1246. unsigned_word top_of_stack;
  1247. unsigned stack_size;
  1248. int elf_binary;
  1249. os_emul_data *bsd_data;
  1250. device *vm;
  1251. char *filename;
  1252. /* check that this emulation is really for us */
  1253. if (name != NULL && strcmp(name, "netbsd") != 0)
  1254. return NULL;
  1255. if (image == NULL)
  1256. return NULL;
  1257. /* merge any emulation specific entries into the device tree */
  1258. /* establish a few defaults */
  1259. if (image->xvec->flavour == bfd_target_elf_flavour) {
  1260. elf_binary = 1;
  1261. top_of_stack = 0xe0000000;
  1262. stack_size = 0x00100000;
  1263. }
  1264. else {
  1265. elf_binary = 0;
  1266. top_of_stack = 0x20000000;
  1267. stack_size = 0x00100000;
  1268. }
  1269. /* options */
  1270. emul_add_tree_options(root, image, "netbsd",
  1271. (WITH_ENVIRONMENT == USER_ENVIRONMENT
  1272. ? "user" : "virtual"),
  1273. 0 /*oea-interrupt-prefix*/);
  1274. /* virtual memory - handles growth of stack/heap */
  1275. vm = tree_parse(root, "/openprom/vm");
  1276. tree_parse(vm, "./stack-base 0x%lx",
  1277. (unsigned long)(top_of_stack - stack_size));
  1278. tree_parse(vm, "./nr-bytes 0x%x", stack_size);
  1279. filename = tree_quote_property (bfd_get_filename(image));
  1280. tree_parse(root, "/openprom/vm/map-binary/file-name %s",
  1281. filename);
  1282. free (filename);
  1283. /* finish the init */
  1284. tree_parse(root, "/openprom/init/register/pc 0x%lx",
  1285. (unsigned long)bfd_get_start_address(image));
  1286. tree_parse(root, "/openprom/init/register/sp 0x%lx",
  1287. (unsigned long)top_of_stack);
  1288. tree_parse(root, "/openprom/init/register/msr 0x%x",
  1289. ((tree_find_boolean_property(root, "/options/little-endian?")
  1290. ? msr_little_endian_mode
  1291. : 0)
  1292. | (tree_find_boolean_property(root, "/openprom/options/floating-point?")
  1293. ? (msr_floating_point_available
  1294. | msr_floating_point_exception_mode_0
  1295. | msr_floating_point_exception_mode_1)
  1296. : 0)));
  1297. tree_parse(root, "/openprom/init/stack/stack-type %s",
  1298. (elf_binary ? "ppc-elf" : "ppc-xcoff"));
  1299. /* finally our emulation data */
  1300. bsd_data = ZALLOC(os_emul_data);
  1301. bsd_data->vm = vm;
  1302. bsd_data->syscalls = &emul_netbsd_syscalls;
  1303. return bsd_data;
  1304. }
  1305. static void
  1306. emul_netbsd_init(os_emul_data *emul_data,
  1307. int nr_cpus)
  1308. {
  1309. /* nothing yet */
  1310. }
  1311. static void
  1312. emul_netbsd_system_call(cpu *processor,
  1313. unsigned_word cia,
  1314. os_emul_data *emul_data)
  1315. {
  1316. emul_do_system_call(emul_data,
  1317. emul_data->syscalls,
  1318. cpu_registers(processor)->gpr[0],
  1319. 3, /*r3 contains arg0*/
  1320. processor,
  1321. cia);
  1322. }
  1323. const os_emul emul_netbsd = {
  1324. "netbsd",
  1325. emul_netbsd_create,
  1326. emul_netbsd_init,
  1327. emul_netbsd_system_call,
  1328. 0, /*instruction_call*/
  1329. 0 /*data*/
  1330. };
  1331. #endif /* _EMUL_NETBSD_C_ */