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