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