This is the mail archive of the sid@sources.redhat.com mailing list for the SID project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

gloss patch for argc/argv retrieval


It is currently possible for a SID configuration to set the gloss
"command-line" attribute for process emulation.  However I noted that
the gloss component doesn't do much with it -- except store it.

The following patch adds a number of new syscalls to enable software
running on target CPUs to request the argc and argv variables passed
in from the outside world.  I'd be grateful of some feedback
(especially with respect to the choice of the enumerator values for
the syscall enum).  Comments?

Cheers, Ben


2002-01-25  Ben Elliston  <bje@redhat.com>

	* libgloss.h (libgloss::SYS_argc): New enumerator.
	(libgloss::SYS_argnlen, libgloss::SYS_argn): Likewise.
	(libgloss::SYS_unsupported): Raise its value.
	* gloss.cxx (gloss32::gloss32): Virtualise "command-line".
	(gloss32::get_command_line): New method.
	(gloss32::set_command_line): Likewise.
	(gloss32::set_string [string&]): Call char* version.
	(gloss32::set_string [char*]): Implement.
	(gloss32::syscall_trap): Handle SYS_argc, SYS_argn, SYS_argnlen.
	(gloss32::do_sys_argc): New method.
	(gloss32::do_sys_argn): Likewise.
	(gloss32::do_sys_argnlen): Likewise.
	* gloss.h (gloss32::set_string): New method which has a length
	parameter for binary data and null-terminated strings.
	(gloss32::do_sys_argc): Declare.
	(gloss32::do_sys_argn): Likewise.
	(gloss32::do_sys_argnlen): Likewise.
	(gloss32::command_line): Change type to vector<string>.
	(gloss32::get_command_line): New virtual attribute callback.
	(gloss32::set_command_line): Likewise.

Index: gloss.cxx
===================================================================
RCS file: /cvs/cvsfiles/devo/sid/component/gloss/gloss.cxx,v
retrieving revision 1.40
diff -u -r1.40 gloss.cxx
--- gloss.cxx	2001/12/01 00:57:01	1.40
+++ gloss.cxx	2002/01/25 03:01:45
@@ -1,6 +1,6 @@
 // gloss.cxx - Gloss routines.  -*- C++ -*-
 
-// Copyright (C) 1999, 2000 Red Hat.
+// Copyright (C) 1999, 2000, 2001, 2002 Red Hat.
 // This file is part of SID and is licensed under the GPL.
 // See the file COPYING.SID for conditions for redistribution.
 
@@ -42,11 +42,12 @@
   rx_pin(this, &gloss32::rx_handler),
   cpu (0),
   cpu_memory_bus (0),
-  command_line("<unknown>"),
   syscall_numbering_scheme("libgloss"),
   max_fds(32),
   verbose_p(false)
 {
+  command_line.push_back ("<unknown>");
+
   // ??? There's a naming disconnect between "cpu" and "target_memory".
   add_accessor("target-memory", &this->cpu_memory_bus);
 
@@ -63,8 +64,12 @@
   add_attribute_ro_value ("tk tty", string("hw-visual-tty"), "gui");
 
   add_uni_relation("cpu", &this->cpu);
+
+  add_attribute_virtual("command-line", this,
+			&gloss32::get_command_line,
+			&gloss32::set_command_line,
+			"setting");
 
-  add_attribute("command-line", &this->command_line, "setting");
   add_attribute("syscall-numbering-scheme", &this->syscall_numbering_scheme, "setting");
   add_attribute("verbose?", &this->verbose_p, "setting");
   
@@ -80,6 +85,45 @@
   delete [] fd_table;
 }
 
+string
+gloss32::get_command_line ()
+{
+  string cline;
+  for (std::vector<string>::const_iterator it = command_line.begin();
+       it != command_line.end ();
+       it++)
+    {
+      cline += *it;
+      if (it + 1 != command_line.end ())
+	cline += " ";
+    }
+  return cline;
+}
+
+component::status
+gloss32::set_command_line (const string& cline)
+{
+  vector<string> argv = sidutil::tokenize (cline, " ");
+  command_line.clear();
+
+  if (argv.empty())
+    {
+      command_line.push_back ("<unknown>");
+      return component::bad_value;
+    }
+
+  // Insert all non-empty strings into command_line.
+  for (std::vector<string>::iterator it = argv.begin();
+       it != argv.end ();
+       it++)
+    {
+      if (*it != "")
+	command_line.push_back (*it);
+    }
+
+  return command_line.empty() ? component::bad_value : component::ok;
+}
+
 void
 gloss32::reset_pin_handler(host_int_4 ignore)
 {
@@ -196,7 +240,7 @@
 }
 
 bool
-gloss32::set_string(address32 address, const string& value) 
+gloss32::set_string (address32 address, const char* value, unsigned length)
 {
   if (! this->cpu_memory_bus)
     {
@@ -206,12 +250,12 @@
   
   if (verbose_p)
     {
-      cerr << "Writing " << value.size() << " byte(s) to target memory at "
+      cerr << "Writing " << length << " byte(s) to target memory at "
 	   << make_numeric_attribute (address, ios::hex | ios::showbase)
            << ": ";
     }
   
-  for (unsigned i = 0; i < value.size(); i++)
+  for (unsigned i = 0; i < length; i++)
     {
       char c = value[i];
       little_int_1 byte = c;
@@ -241,6 +285,12 @@
 }
 
 bool
+gloss32::set_string(address32 address, const string& value)
+{
+  return set_string (address, value.c_str(), value.length () + 1);
+}
+
+bool
 gloss32::get_halfword (address32 addr, sid::host_int_2& value)
 {
   if (! cpu_memory_bus)
@@ -728,6 +778,15 @@
     case libgloss::SYS_unlink:
       do_sys_unlink();
       break;
+    case libgloss::SYS_argc:
+      do_sys_argc();
+      break;
+    case libgloss::SYS_argnlen:
+      do_sys_argnlen();
+      break;
+    case libgloss::SYS_argn:
+      do_sys_argn();
+      break;
     default:
       do_nonstandard_target_syscalls (syscall);
       break;
@@ -741,6 +800,60 @@
     cerr << "Unimplemented syscall " << target_syscall << endl;
   set_int_result(-1);
   set_error_result(newlib::eNoSys);
+}
+
+void
+gloss32::do_sys_argc ()
+{
+  set_int_result (command_line.size ());
+  set_error_result (0);
+}
+
+void
+gloss32::do_sys_argnlen ()
+{
+  int32 n;
+  get_int_argument(1, n);
+
+  if (n < command_line.size ())
+    {
+      set_int_result (command_line[n].length ());
+      set_error_result (0);
+    }
+  else
+    {
+      set_int_result (-1);
+      set_error_result (newlib::eInval);
+    }
+}
+
+void
+gloss32::do_sys_argn ()
+{
+  int32 n, str_ptr;
+  get_int_argument (1, n);
+  get_int_argument(2, str_ptr);
+
+  if (n < command_line.size ())
+    {
+      // Include the NULL byte.
+      int i = command_line[n].length () + 1;
+      if (set_string (str_ptr, command_line[n]))
+	{
+	  set_int_result (i);
+	  set_error_result (0);
+	}
+      else
+	{
+	  set_int_result (-1);
+	  set_error_result (newlib::eFault);
+	}
+    }
+  else
+    {
+      set_int_result (-1);
+      set_error_result (newlib::eInval);
+    }
 }
 
 void
Index: gloss.h
===================================================================
RCS file: /cvs/cvsfiles/devo/sid/component/gloss/gloss.h,v
retrieving revision 1.23
diff -u -r1.23 gloss.h
--- gloss.h	2002/01/03 01:41:54	1.23
+++ gloss.h	2002/01/25 03:01:45
@@ -1,7 +1,7 @@
 // gloss.h - Basic process emulation plus ROM monitor support.
 // -*- C++ -*-
 
-// Copyright (C) 1999, 2000, 2001 Red Hat.
+// Copyright (C) 1999, 2000, 2001, 2002 Red Hat.
 // This file is part of SID and is licensed under the GPL.
 // See the file COPYING.SID for conditions for redistribution.
 
@@ -121,6 +121,7 @@
   // Calling get_string with length = 0 indicates that there is no
   // imposed length limit; read from memory until a NUL is encountered.
   bool set_string(address32 address, const string& value);
+  bool set_string(address32 address, const char* value, unsigned length);
   bool get_string(address32 address, string& value, unsigned length = 0);
 
   // Get the value of the cpu's program counter.
@@ -149,6 +150,9 @@
   void do_sys_gettimeofday();
   void do_sys_times();
   void do_sys_unlink();
+  void do_sys_argc();
+  void do_sys_argn();
+  void do_sys_argnlen();
   virtual void do_nonstandard_target_syscalls(int32 syscall);
   virtual bool target_to_host_open_flags (int open_flags, int& flags);
   virtual int32 target_to_host_syscall (int32 syscall);
@@ -157,7 +161,9 @@
   virtual void fault_trap(host_int_4 trap_type, host_int_4 trap_code);
 
   // For Unix process emulation.
-  string command_line;
+  vector<string> command_line;
+  string get_command_line ();
+  component::status set_command_line (const string& cmd_line);
 
   // System calls.
 
Index: libgloss.h
===================================================================
RCS file: /cvs/cvsfiles/devo/sid/component/gloss/libgloss.h,v
retrieving revision 1.7
diff -u -r1.7 libgloss.h
--- libgloss.h	2002/01/03 01:41:54	1.7
+++ libgloss.h	2002/01/25 03:01:45
@@ -1,6 +1,6 @@
 // libgloss.h - Interface details for libgloss.  -*- C++ -*-
 
-// Copyright (C) 1999, 2000, 2001 Red Hat.
+// Copyright (C) 1999, 2000, 2001, 2002 Red Hat.
 // This file is part of SID and is licensed under the GPL.
 // See the file COPYING.SID for conditions for redistribution.
 
@@ -38,7 +38,10 @@
     SYS_time = 18,
     SYS_gettimeofday = 19,
     SYS_times = 20,
-    SYS_unsupported = 99 // arbitrary syscall number, unsupported by default gloss component
+    SYS_argc = 172,
+    SYS_argnlen = 173,
+    SYS_argn = 174,
+    SYS_unsupported = 255 // arbitrary syscall number, unsupported by default gloss component
   };
 };
 


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]