sim: constify sim_write source buffer (part 2)
[deliverable/binutils-gdb.git] / sim / ppc / emul_netbsd.c
index fae6f914121d33a12d49952d3d29df55ac539a37..f81c9cb250330033d77bd12b4f56b1411f655183 100644 (file)
@@ -1,6 +1,6 @@
 /*  This file is part of the program psim.
 
-    Copyright (C) 1994-1996, Andrew Cagney <cagney@highland.com.au>
+    Copyright (C) 1994-1998, Andrew Cagney <cagney@highland.com.au>
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU General Public License for more details.
+
     You should have received a copy of the GNU General Public License
     along with this program; if not, write to the Free Software
     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
     */
 
 
@@ -42,7 +42,7 @@
 #include <stdio.h>
 #include <signal.h>
 #include <fcntl.h>
-#include <sys/errno.h>
+#include <errno.h>
 #include <sys/param.h>
 #include <sys/time.h>
 
@@ -61,10 +61,6 @@ int getrusage();
 #include <sys/ioctl.h>
 #endif
 
-#if HAVE_SYS_MOUNT_H
-#include <sys/mount.h>
-#endif
-
 #if HAVE_DIRENT_H
 # include <dirent.h>
 # define NAMLEN(dirent) strlen((dirent)->d_name)
@@ -95,7 +91,16 @@ int getrusage();
 #if WITH_NetBSD_HOST /* here NetBSD as that is what we're emulating */
 #include <sys/syscall.h> /* FIXME - should not be including this one */
 #include <sys/sysctl.h>
+#include <sys/mount.h>
 extern int getdirentries(int fd, char *buf, int nbytes, long *basep);
+
+/* NetBSD post 2.0 has the statfs system call (if COMPAT_20), but does
+   not have struct statfs.  In this case don't implement fstatfs.
+   FIXME: Should implement fstatvfs.  */
+#ifndef HAVE_STRUCT_STATFS
+#undef HAVE_FSTATFS
+#endif
+
 #else
 
 /* If this is not netbsd, don't allow fstatfs or getdirentries at this time */
@@ -125,6 +130,15 @@ extern int errno;
 #endif
 
 
+/* EMULATION
+
+   NetBSD - Emulation of user programs for NetBSD/PPC
+
+   DESCRIPTION
+
+   */
+
+
 /* NetBSD's idea of what is needed to implement emulations */
 
 struct _os_emul_data {
@@ -169,7 +183,7 @@ write_stat(unsigned_word addr,
   emul_write_buffer(&buf, addr, sizeof(buf), processor, cia);
 }
 
-  
+
 #ifdef HAVE_FSTATFS
 STATIC_INLINE_EMUL_NETBSD void
 write_statfs(unsigned_word addr,
@@ -197,7 +211,7 @@ write_statfs(unsigned_word addr,
 }
 #endif
 
-  
+
 STATIC_INLINE_EMUL_NETBSD void
 write_timeval(unsigned_word addr,
              struct timeval t,
@@ -209,7 +223,7 @@ write_timeval(unsigned_word addr,
   emul_write_buffer(&t, addr, sizeof(t), processor, cia);
 }
 
-  
+#ifdef HAVE_GETTIMEOFDAY
 STATIC_INLINE_EMUL_NETBSD void
 write_timezone(unsigned_word addr,
               struct timezone tz,
@@ -220,8 +234,8 @@ write_timezone(unsigned_word addr,
   H2T(tz.tz_dsttime);
   emul_write_buffer(&tz, addr, sizeof(tz), processor, cia);
 }
+#endif
 
-  
 #ifdef HAVE_GETDIRENTRIES
 STATIC_INLINE_EMUL_NETBSD void
 write_direntries(unsigned_word addr,
@@ -278,7 +292,7 @@ write_rusage(unsigned_word addr,
   emul_write_buffer(&rusage, addr, sizeof(rusage), processor, cia);
 }
 #endif
-  
+
 static void
 do_exit(os_emul_data *emul,
        unsigned call,
@@ -308,16 +322,16 @@ do_read(os_emul_data *emul,
   int nbytes = cpu_registers(processor)->gpr[arg0+2];
   int status;
   SYS(read);
-  
+
   if (WITH_TRACE && ppc_trace[trace_os_emul])
     printf_filtered ("%d, 0x%lx, %d", d, (long)buf, nbytes);
 
   /* get a tempoary bufer */
   scratch_buffer = zalloc(nbytes);
-  
+
   /* check if buffer exists by reading it */
   emul_read_buffer(scratch_buffer, buf, nbytes, processor, cia);
-  
+
   /* read */
 #if 0
   if (d == 0) {
@@ -327,11 +341,11 @@ do_read(os_emul_data *emul,
   }
 #endif
   status = read (d, scratch_buffer, nbytes);
-  
+
   emul_write_status(processor, status, errno);
   if (status > 0)
     emul_write_buffer(scratch_buffer, buf, status, processor, cia);
-  
+
   zfree(scratch_buffer);
 }
 
@@ -344,30 +358,22 @@ do_write(os_emul_data *emul,
         unsigned_word cia)
 {
   void *scratch_buffer = NULL;
-  int nr_moved;
   int d = (int)cpu_registers(processor)->gpr[arg0];
   unsigned_word buf = cpu_registers(processor)->gpr[arg0+1];
   int nbytes = cpu_registers(processor)->gpr[arg0+2];
   int status;
   SYS(write);
-  
+
   if (WITH_TRACE && ppc_trace[trace_os_emul])
     printf_filtered ("%d, 0x%lx, %d", d, (long)buf, nbytes);
 
   /* get a tempoary bufer */
   scratch_buffer = zalloc(nbytes); /* FIXME - nbytes == 0 */
-  
+
   /* copy in */
-  nr_moved = vm_data_map_read_buffer(cpu_data_map(processor),
-                                    scratch_buffer,
-                                    buf,
-                                    nbytes);
-  if (nr_moved != nbytes) {
-    /* FIXME - should handle better */
-    error("system_call()write copy failed (nr_moved=%d != nbytes=%d)\n",
-         nr_moved, nbytes);
-  }
-  
+  emul_read_buffer(scratch_buffer, buf, nbytes,
+                  processor, cia);
+
   /* write */
   status = write(d, scratch_buffer, nbytes);
   emul_write_status(processor, status, errno);
@@ -389,12 +395,34 @@ do_open(os_emul_data *emul,
   char *path = emul_read_string(path_buf, path_addr, PATH_MAX, processor, cia);
   int flags = (int)cpu_registers(processor)->gpr[arg0+1];
   int mode = (int)cpu_registers(processor)->gpr[arg0+2];
+  int hostflags;
+  int status;
 
   if (WITH_TRACE && ppc_trace[trace_os_emul])
     printf_filtered ("0x%lx [%s], 0x%x, 0x%x", (long)path_addr, path, flags, mode);
 
   SYS(open);
-  emul_write_status(processor, open(path, flags, mode), errno);
+
+  /* Do some translation on 'flags' to match it to the host's version.  */
+  /* These flag values were taken from the NetBSD 1.4 header files.  */
+  if ((flags & 3) == 0)
+    hostflags = O_RDONLY;
+  else if ((flags & 3) == 1)
+    hostflags = O_WRONLY;
+  else
+    hostflags = O_RDWR;
+  if (flags & 0x00000008)
+    hostflags |= O_APPEND;
+  if (flags & 0x00000200)
+    hostflags |= O_CREAT;
+  if (flags & 0x00000400)
+    hostflags |= O_TRUNC;
+  if (flags & 0x00000800)
+    hostflags |= O_EXCL;
+
+  /* Can't combine these statements, cuz open sets errno. */
+  status = open(path, hostflags, mode);
+  emul_write_status(processor, status, errno);
 }
 
 
@@ -406,12 +434,16 @@ do_close(os_emul_data *emul,
         unsigned_word cia)
 {
   int d = (int)cpu_registers(processor)->gpr[arg0];
+  int status;
 
   if (WITH_TRACE && ppc_trace[trace_os_emul])
     printf_filtered ("%d", d);
 
   SYS(close);
-  emul_write_status(processor, close(d), errno);
+
+  /* Can't combine these statements, cuz close sets errno. */
+  status = close(d);
+  emul_write_status(processor, status, errno);
 }
 
 
@@ -423,7 +455,6 @@ do_break(os_emul_data *emul,
         unsigned_word cia)
 {
   /* just pass this onto the `vm' device */
-  psim *system = cpu_system(processor);
   unsigned_word new_break = cpu_registers(processor)->gpr[arg0];
   int status;
 
@@ -432,9 +463,9 @@ do_break(os_emul_data *emul,
 
   SYS(break);
   status = device_ioctl(emul->vm,
-                       system,
                        processor,
                        cia,
+                       device_ioctl_break,
                        new_break); /*ioctl-data*/
   emul_write_status(processor, 0, status);
 }
@@ -573,7 +604,9 @@ do_sigprocmask(os_emul_data *emul,
   natural_word how = cpu_registers(processor)->gpr[arg0];
   unsigned_word set = cpu_registers(processor)->gpr[arg0+1];
   unsigned_word oset = cpu_registers(processor)->gpr[arg0+2];
+#ifdef SYS_sigprocmask
   SYS(sigprocmask);
+#endif
 
   if (WITH_TRACE && ppc_trace[trace_os_emul])
     printf_filtered ("%ld, 0x%ld, 0x%ld", (long)how, (long)set, (long)oset);
@@ -789,7 +822,9 @@ do_stat(os_emul_data *emul,
   char *path = emul_read_string(path_buf, path_addr, PATH_MAX, processor, cia);
   struct stat buf;
   int status;
+#ifdef SYS_stat
   SYS(stat);
+#endif
   status = stat(path, &buf);
   emul_write_status(processor, status, errno);
   if (status == 0)
@@ -810,8 +845,13 @@ do_fstat(os_emul_data *emul,
   int fd = cpu_registers(processor)->gpr[arg0];
   unsigned_word stat_buf_addr = cpu_registers(processor)->gpr[arg0+1];
   struct stat buf;
+  int status;
+#ifdef SYS_fstat
   SYS(fstat);
-  emul_write_status(processor, fstat(fd, &buf), errno);
+#endif
+  /* Can't combine these statements, cuz fstat sets errno. */
+  status = fstat(fd, &buf);
+  emul_write_status(processor, status, errno);
   write_stat(stat_buf_addr, buf, processor, cia);
 }
 #endif
@@ -831,8 +871,13 @@ do_lstat(os_emul_data *emul,
   char *path = emul_read_string(path_buf, path_addr, PATH_MAX, processor, cia);
   unsigned_word stat_buf_addr = cpu_registers(processor)->gpr[arg0+1];
   struct stat buf;
+  int status;
+#ifdef SYS_lstat
   SYS(lstat);
-  emul_write_status(processor, stat(path, &buf), errno);
+#endif
+  /* Can't combine these statements, cuz lstat sets errno. */
+  status = lstat(path, &buf);
+  emul_write_status(processor, status, errno);
   write_stat(stat_buf_addr, buf, processor, cia);
 }
 #endif
@@ -854,7 +899,9 @@ do_getdirentries(os_emul_data *emul,
   unsigned_word basep_addr = cpu_registers(processor)->gpr[arg0+3];
   long basep;
   int status;
+#ifdef SYS_getdirentries
   SYS(getdirentries);
+#endif
   if (buf_addr != 0 && nbytes >= 0)
     buf = zalloc(nbytes);
   else
@@ -940,7 +987,7 @@ do___sysctl(os_emul_data *emul,
                              processor,
                              cia);
   name += sizeof(mib);
-  
+
   /* see what to do with it ... */
   switch ((int)mib) {
   case 6/*CTL_HW*/:
@@ -987,7 +1034,7 @@ do___sysctl(os_emul_data *emul,
 static emul_syscall_descriptor netbsd_descriptors[] = {
   /* 0 */ { 0, "syscall" },
   /* 1 */ { do_exit, "exit" },
-  /* 2 */ { 0, "fork" },         
+  /* 2 */ { 0, "fork" },
   /* 3 */ { do_read, "read" },
   /* 4 */ { do_write, "write" },
   /* 5 */ { do_open, "open" },
@@ -1191,7 +1238,7 @@ static emul_syscall_descriptor netbsd_descriptors[] = {
   /* 203 */ { 0, "mlock" },
   /* 204 */ { 0, "munlock" },
 };
-    
+
 static char *(netbsd_error_names[]) = {
   /* 0 */ "ESUCCESS",
   /* 1 */ "EPERM",
@@ -1336,6 +1383,7 @@ emul_netbsd_create(device *root,
   int elf_binary;
   os_emul_data *bsd_data;
   device *vm;
+  char *filename;
 
   /* check that this emulation is really for us */
   if (name != NULL && strcmp(name, "netbsd") != 0)
@@ -1365,26 +1413,32 @@ emul_netbsd_create(device *root,
                        0 /*oea-interrupt-prefix*/);
 
   /* virtual memory - handles growth of stack/heap */
-  vm = device_tree_add_parsed(root, "/openprom/vm@0x%lx",
-                             (unsigned long)(top_of_stack - stack_size));
-  device_tree_add_parsed(vm, "./stack-base 0x%lx",
-                        (unsigned long)(top_of_stack - stack_size));
-  device_tree_add_parsed(vm, "./nr-bytes 0x%x", stack_size);
+  vm = tree_parse(root, "/openprom/vm");
+  tree_parse(vm, "./stack-base 0x%lx",
+            (unsigned long)(top_of_stack - stack_size));
+  tree_parse(vm, "./nr-bytes 0x%x", stack_size);
 
-  device_tree_add_parsed(root, "/openprom/vm/map-binary/file-name %s",
-                        bfd_get_filename(image));
+  filename = tree_quote_property (bfd_get_filename(image));
+  tree_parse(root, "/openprom/vm/map-binary/file-name %s",
+            filename);
+  free (filename);
 
   /* finish the init */
-  device_tree_add_parsed(root, "/openprom/init/register/pc 0x%lx",
-                        (unsigned long)bfd_get_start_address(image));
-  device_tree_add_parsed(root, "/openprom/init/register/sp 0x%lx",
-                        (unsigned long)top_of_stack);
-  device_tree_add_parsed(root, "/openprom/init/register/msr 0x%x",
-                        (device_find_boolean_property(root, "/options/little-endian?")
-                         ? msr_little_endian_mode
-                         : 0));
-  device_tree_add_parsed(root, "/openprom/init/stack/stack-type %s",
-                        (elf_binary ? "elf" : "xcoff"));
+  tree_parse(root, "/openprom/init/register/pc 0x%lx",
+            (unsigned long)bfd_get_start_address(image));
+  tree_parse(root, "/openprom/init/register/sp 0x%lx",
+            (unsigned long)top_of_stack);
+  tree_parse(root, "/openprom/init/register/msr 0x%x",
+            ((tree_find_boolean_property(root, "/options/little-endian?")
+              ? msr_little_endian_mode
+              : 0)
+             | (tree_find_boolean_property(root, "/openprom/options/floating-point?")
+                ? (msr_floating_point_available
+                   | msr_floating_point_exception_mode_0
+                   | msr_floating_point_exception_mode_1)
+                : 0)));
+  tree_parse(root, "/openprom/init/stack/stack-type %s",
+            (elf_binary ? "ppc-elf" : "ppc-xcoff"));
 
   /* finally our emulation data */
   bsd_data = ZALLOC(os_emul_data);
@@ -1422,4 +1476,4 @@ const os_emul emul_netbsd = {
   0 /*data*/
 };
 
-#endif /* _EMUL_NETBSD_C_ */
+#endif /* _EMUL_NETBSD_C_ */
This page took 0.034469 seconds and 4 git commands to generate.