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