xref: /netbsd-src/external/gpl3/gdb/dist/sim/lm32/sim-if.c (revision 88241920d21b339bf319c0e979ffda80c49a2936)
14e98e3e1Schristos /* Main simulator entry points specific to Lattice Mico32.
24e98e3e1Schristos    Contributed by Jon Beniston <jon@beniston.com>
34e98e3e1Schristos 
4*88241920Schristos    Copyright (C) 2009-2024 Free Software Foundation, Inc.
54e98e3e1Schristos 
64e98e3e1Schristos    This file is part of GDB.
74e98e3e1Schristos 
84e98e3e1Schristos    This program is free software; you can redistribute it and/or modify
94e98e3e1Schristos    it under the terms of the GNU General Public License as published by
104e98e3e1Schristos    the Free Software Foundation; either version 3 of the License, or
114e98e3e1Schristos    (at your option) any later version.
124e98e3e1Schristos 
134e98e3e1Schristos    This program is distributed in the hope that it will be useful,
144e98e3e1Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
154e98e3e1Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
164e98e3e1Schristos    GNU General Public License for more details.
174e98e3e1Schristos 
184e98e3e1Schristos    You should have received a copy of the GNU General Public License
194e98e3e1Schristos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
204e98e3e1Schristos 
214b169a6bSchristos /* This must come before any other includes.  */
224b169a6bSchristos #include "defs.h"
234b169a6bSchristos 
244b169a6bSchristos #include <stdlib.h>
254b169a6bSchristos 
264b169a6bSchristos #include "sim/callback.h"
274e98e3e1Schristos #include "sim-main.h"
284e98e3e1Schristos #include "sim-options.h"
294e98e3e1Schristos #include "libiberty.h"
304e98e3e1Schristos #include "bfd.h"
314e98e3e1Schristos 
324e98e3e1Schristos /* Cover function of sim_state_free to free the cpu buffers as well.  */
334e98e3e1Schristos 
344e98e3e1Schristos static void
354e98e3e1Schristos free_state (SIM_DESC sd)
364e98e3e1Schristos {
374e98e3e1Schristos   if (STATE_MODULES (sd) != NULL)
384e98e3e1Schristos     sim_module_uninstall (sd);
394e98e3e1Schristos   sim_cpu_free_all (sd);
404e98e3e1Schristos   sim_state_free (sd);
414e98e3e1Schristos }
424e98e3e1Schristos 
434e98e3e1Schristos /* Find memory range used by program.  */
444e98e3e1Schristos 
454e98e3e1Schristos static unsigned long
464e98e3e1Schristos find_base (bfd *prog_bfd)
474e98e3e1Schristos {
484e98e3e1Schristos   int found;
494e98e3e1Schristos   unsigned long base = ~(0UL);
504e98e3e1Schristos   asection *s;
514e98e3e1Schristos 
524e98e3e1Schristos   found = 0;
534e98e3e1Schristos   for (s = prog_bfd->sections; s; s = s->next)
544e98e3e1Schristos     {
558dffb485Schristos       if ((strcmp (bfd_section_name (s), ".boot") == 0)
568dffb485Schristos 	  || (strcmp (bfd_section_name (s), ".text") == 0)
578dffb485Schristos 	  || (strcmp (bfd_section_name (s), ".data") == 0)
588dffb485Schristos 	  || (strcmp (bfd_section_name (s), ".bss") == 0))
594e98e3e1Schristos 	{
604e98e3e1Schristos 	  if (!found)
614e98e3e1Schristos 	    {
628dffb485Schristos 	      base = bfd_section_vma (s);
634e98e3e1Schristos 	      found = 1;
644e98e3e1Schristos 	    }
654e98e3e1Schristos 	  else
668dffb485Schristos 	    base = bfd_section_vma (s) < base ? bfd_section_vma (s) : base;
674e98e3e1Schristos 	}
684e98e3e1Schristos     }
694e98e3e1Schristos   return base & ~(0xffffUL);
704e98e3e1Schristos }
714e98e3e1Schristos 
724e98e3e1Schristos static unsigned long
73796c32c9Schristos find_limit (SIM_DESC sd)
744e98e3e1Schristos {
75796c32c9Schristos   bfd_vma addr;
764e98e3e1Schristos 
77796c32c9Schristos   addr = trace_sym_value (sd, "_fstack");
78796c32c9Schristos   if (addr == -1)
794e98e3e1Schristos     return 0;
804e98e3e1Schristos 
81796c32c9Schristos   return (addr + 65536) & ~(0xffffUL);
824e98e3e1Schristos }
834e98e3e1Schristos 
844b169a6bSchristos extern const SIM_MACH * const lm32_sim_machs[];
854b169a6bSchristos 
864e98e3e1Schristos /* Create an instance of the simulator.  */
874e98e3e1Schristos 
884e98e3e1Schristos SIM_DESC
894b169a6bSchristos sim_open (SIM_OPEN_KIND kind, host_callback *callback, struct bfd *abfd,
904b169a6bSchristos 	  char * const *argv)
914e98e3e1Schristos {
924e98e3e1Schristos   SIM_DESC sd = sim_state_alloc (kind, callback);
934e98e3e1Schristos   char c;
944e98e3e1Schristos   int i;
954e98e3e1Schristos   unsigned long base, limit;
964e98e3e1Schristos 
974b169a6bSchristos   /* Set default options before parsing user options.  */
984b169a6bSchristos   STATE_MACHS (sd) = lm32_sim_machs;
994b169a6bSchristos   STATE_MODEL_NAME (sd) = "lm32";
1004b169a6bSchristos   current_alignment = STRICT_ALIGNMENT;
1014b169a6bSchristos   current_target_byte_order = BFD_ENDIAN_BIG;
1024b169a6bSchristos 
1034e98e3e1Schristos   /* The cpu data is kept in a separately allocated chunk of memory.  */
104*88241920Schristos   if (sim_cpu_alloc_all_extra (sd, 0, sizeof (struct lm32_sim_cpu))
105*88241920Schristos       != SIM_RC_OK)
1064e98e3e1Schristos     {
1074e98e3e1Schristos       free_state (sd);
1084e98e3e1Schristos       return 0;
1094e98e3e1Schristos     }
1104e98e3e1Schristos 
1114e98e3e1Schristos   if (sim_pre_argv_init (sd, argv[0]) != SIM_RC_OK)
1124e98e3e1Schristos     {
1134e98e3e1Schristos       free_state (sd);
1144e98e3e1Schristos       return 0;
1154e98e3e1Schristos     }
1164e98e3e1Schristos 
117ba340e45Schristos   /* The parser will print an error message for us, so we silently return.  */
1184e98e3e1Schristos   if (sim_parse_args (sd, argv) != SIM_RC_OK)
1194e98e3e1Schristos     {
1204e98e3e1Schristos       free_state (sd);
1214e98e3e1Schristos       return 0;
1224e98e3e1Schristos     }
1234e98e3e1Schristos 
1244e98e3e1Schristos #if 0
1254e98e3e1Schristos   /* Allocate a handler for I/O devices
1264e98e3e1Schristos      if no memory for that range has been allocated by the user.
1274e98e3e1Schristos      All are allocated in one chunk to keep things from being
1284e98e3e1Schristos      unnecessarily complicated.  */
1294e98e3e1Schristos   if (sim_core_read_buffer (sd, NULL, read_map, &c, LM32_DEVICE_ADDR, 1) == 0)
1304e98e3e1Schristos     sim_core_attach (sd, NULL, 0 /*level */ ,
1314e98e3e1Schristos 		     access_read_write, 0 /*space ??? */ ,
1324e98e3e1Schristos 		     LM32_DEVICE_ADDR, LM32_DEVICE_LEN /*nr_bytes */ ,
1334e98e3e1Schristos 		     0 /*modulo */ ,
1344e98e3e1Schristos 		     &lm32_devices, NULL /*buffer */ );
1354e98e3e1Schristos #endif
1364e98e3e1Schristos 
1374e98e3e1Schristos   /* check for/establish the reference program image.  */
1384b169a6bSchristos   if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
1394e98e3e1Schristos     {
1404e98e3e1Schristos       free_state (sd);
1414e98e3e1Schristos       return 0;
1424e98e3e1Schristos     }
1434e98e3e1Schristos 
1444e98e3e1Schristos   /* Check to see if memory exists at programs start address.  */
1454e98e3e1Schristos   if (sim_core_read_buffer (sd, NULL, read_map, &c, STATE_START_ADDR (sd), 1)
1464e98e3e1Schristos       == 0)
1474e98e3e1Schristos     {
1484e98e3e1Schristos       if (STATE_PROG_BFD (sd) != NULL)
1494e98e3e1Schristos 	{
1504e98e3e1Schristos 	  /* It doesn't, so we should try to allocate enough memory to hold program.  */
1514e98e3e1Schristos 	  base = find_base (STATE_PROG_BFD (sd));
152796c32c9Schristos 	  limit = find_limit (sd);
1534e98e3e1Schristos 	  if (limit == 0)
1544e98e3e1Schristos 	    {
1554e98e3e1Schristos 	      sim_io_eprintf (sd,
1564e98e3e1Schristos 			      "Failed to find symbol _fstack in program. You must specify memory regions with --memory-region.\n");
1574e98e3e1Schristos 	      free_state (sd);
1584e98e3e1Schristos 	      return 0;
1594e98e3e1Schristos 	    }
1604b169a6bSchristos 	  /*sim_io_printf (sd, "Allocating memory at 0x%lx size 0x%lx\n", base, limit); */
1614b169a6bSchristos 	  sim_do_commandf (sd, "memory region 0x%lx,0x%lx", base, limit);
1624e98e3e1Schristos 	}
1634e98e3e1Schristos     }
1644e98e3e1Schristos 
1654e98e3e1Schristos   /* Establish any remaining configuration options.  */
1664e98e3e1Schristos   if (sim_config (sd) != SIM_RC_OK)
1674e98e3e1Schristos     {
1684e98e3e1Schristos       free_state (sd);
1694e98e3e1Schristos       return 0;
1704e98e3e1Schristos     }
1714e98e3e1Schristos 
1724e98e3e1Schristos   if (sim_post_argv_init (sd) != SIM_RC_OK)
1734e98e3e1Schristos     {
1744e98e3e1Schristos       free_state (sd);
1754e98e3e1Schristos       return 0;
1764e98e3e1Schristos     }
1774e98e3e1Schristos 
1784e98e3e1Schristos   /* Open a copy of the cpu descriptor table.  */
1794e98e3e1Schristos   {
1804e98e3e1Schristos     CGEN_CPU_DESC cd =
1814e98e3e1Schristos       lm32_cgen_cpu_open_1 (STATE_ARCHITECTURE (sd)->printable_name,
1824e98e3e1Schristos 			    CGEN_ENDIAN_BIG);
1834e98e3e1Schristos     for (i = 0; i < MAX_NR_PROCESSORS; ++i)
1844e98e3e1Schristos       {
1854e98e3e1Schristos 	SIM_CPU *cpu = STATE_CPU (sd, i);
1864e98e3e1Schristos 	CPU_CPU_DESC (cpu) = cd;
1874e98e3e1Schristos 	CPU_DISASSEMBLER (cpu) = sim_cgen_disassemble_insn;
1884e98e3e1Schristos       }
1894e98e3e1Schristos     lm32_cgen_init_dis (cd);
1904e98e3e1Schristos   }
1914e98e3e1Schristos 
1924e98e3e1Schristos   return sd;
1934e98e3e1Schristos }
1944e98e3e1Schristos 
1954e98e3e1Schristos SIM_RC
1964b169a6bSchristos sim_create_inferior (SIM_DESC sd, struct bfd *abfd, char * const *argv,
1974b169a6bSchristos 		     char * const *env)
1984e98e3e1Schristos {
1994e98e3e1Schristos   SIM_CPU *current_cpu = STATE_CPU (sd, 0);
2004b169a6bSchristos   host_callback *cb = STATE_CALLBACK (sd);
201*88241920Schristos   bfd_vma addr;
2024e98e3e1Schristos 
2034e98e3e1Schristos   if (abfd != NULL)
2044e98e3e1Schristos     addr = bfd_get_start_address (abfd);
2054e98e3e1Schristos   else
2064e98e3e1Schristos     addr = 0;
2074e98e3e1Schristos   sim_pc_set (current_cpu, addr);
2084e98e3e1Schristos 
209ba340e45Schristos   /* Standalone mode (i.e. `run`) will take care of the argv for us in
210ba340e45Schristos      sim_open() -> sim_parse_args().  But in debug mode (i.e. 'target sim'
211ba340e45Schristos      with `gdb`), we need to handle it because the user can change the
212ba340e45Schristos      argv on the fly via gdb's 'run'.  */
213ba340e45Schristos   if (STATE_PROG_ARGV (sd) != argv)
214ba340e45Schristos     {
215ba340e45Schristos       freeargv (STATE_PROG_ARGV (sd));
216ba340e45Schristos       STATE_PROG_ARGV (sd) = dupargv (argv);
217ba340e45Schristos     }
2184e98e3e1Schristos 
2194b169a6bSchristos   if (STATE_PROG_ENVP (sd) != env)
2204b169a6bSchristos     {
2214b169a6bSchristos       freeargv (STATE_PROG_ENVP (sd));
2224b169a6bSchristos       STATE_PROG_ENVP (sd) = dupargv (env);
2234b169a6bSchristos     }
2244b169a6bSchristos 
2254b169a6bSchristos   cb->argv = STATE_PROG_ARGV (sd);
2264b169a6bSchristos   cb->envp = STATE_PROG_ENVP (sd);
2274b169a6bSchristos 
2284e98e3e1Schristos   return SIM_RC_OK;
2294e98e3e1Schristos }
230