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