Home | History | Annotate | Line # | Download | only in lm32
      1 /* Main simulator entry points specific to Lattice Mico32.
      2    Contributed by Jon Beniston <jon (at) beniston.com>
      3 
      4    Copyright (C) 2009-2024 Free Software Foundation, Inc.
      5 
      6    This file is part of GDB.
      7 
      8    This program is free software; you can redistribute it and/or modify
      9    it under the terms of the GNU General Public License as published by
     10    the Free Software Foundation; either version 3 of the License, or
     11    (at your option) any later version.
     12 
     13    This program is distributed in the hope that it will be useful,
     14    but WITHOUT ANY WARRANTY; without even the implied warranty of
     15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     16    GNU General Public License for more details.
     17 
     18    You should have received a copy of the GNU General Public License
     19    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     20 
     21 /* This must come before any other includes.  */
     22 #include "defs.h"
     23 
     24 #include <stdlib.h>
     25 
     26 #include "sim/callback.h"
     27 #include "sim-main.h"
     28 #include "sim-options.h"
     29 #include "libiberty.h"
     30 #include "bfd.h"
     31 
     32 /* Cover function of sim_state_free to free the cpu buffers as well.  */
     34 
     35 static void
     36 free_state (SIM_DESC sd)
     37 {
     38   if (STATE_MODULES (sd) != NULL)
     39     sim_module_uninstall (sd);
     40   sim_cpu_free_all (sd);
     41   sim_state_free (sd);
     42 }
     43 
     44 /* Find memory range used by program.  */
     45 
     46 static unsigned long
     47 find_base (bfd *prog_bfd)
     48 {
     49   int found;
     50   unsigned long base = ~(0UL);
     51   asection *s;
     52 
     53   found = 0;
     54   for (s = prog_bfd->sections; s; s = s->next)
     55     {
     56       if ((strcmp (bfd_section_name (s), ".boot") == 0)
     57 	  || (strcmp (bfd_section_name (s), ".text") == 0)
     58 	  || (strcmp (bfd_section_name (s), ".data") == 0)
     59 	  || (strcmp (bfd_section_name (s), ".bss") == 0))
     60 	{
     61 	  if (!found)
     62 	    {
     63 	      base = bfd_section_vma (s);
     64 	      found = 1;
     65 	    }
     66 	  else
     67 	    base = bfd_section_vma (s) < base ? bfd_section_vma (s) : base;
     68 	}
     69     }
     70   return base & ~(0xffffUL);
     71 }
     72 
     73 static unsigned long
     74 find_limit (SIM_DESC sd)
     75 {
     76   bfd_vma addr;
     77 
     78   addr = trace_sym_value (sd, "_fstack");
     79   if (addr == -1)
     80     return 0;
     81 
     82   return (addr + 65536) & ~(0xffffUL);
     83 }
     84 
     85 extern const SIM_MACH * const lm32_sim_machs[];
     86 
     87 /* Create an instance of the simulator.  */
     88 
     89 SIM_DESC
     90 sim_open (SIM_OPEN_KIND kind, host_callback *callback, struct bfd *abfd,
     91 	  char * const *argv)
     92 {
     93   SIM_DESC sd = sim_state_alloc (kind, callback);
     94   char c;
     95   int i;
     96   unsigned long base, limit;
     97 
     98   /* Set default options before parsing user options.  */
     99   STATE_MACHS (sd) = lm32_sim_machs;
    100   STATE_MODEL_NAME (sd) = "lm32";
    101   current_alignment = STRICT_ALIGNMENT;
    102   current_target_byte_order = BFD_ENDIAN_BIG;
    103 
    104   /* The cpu data is kept in a separately allocated chunk of memory.  */
    105   if (sim_cpu_alloc_all_extra (sd, 0, sizeof (struct lm32_sim_cpu))
    106       != SIM_RC_OK)
    107     {
    108       free_state (sd);
    109       return 0;
    110     }
    111 
    112   if (sim_pre_argv_init (sd, argv[0]) != SIM_RC_OK)
    113     {
    114       free_state (sd);
    115       return 0;
    116     }
    117 
    118   /* The parser will print an error message for us, so we silently return.  */
    119   if (sim_parse_args (sd, argv) != SIM_RC_OK)
    120     {
    121       free_state (sd);
    122       return 0;
    123     }
    124 
    125 #if 0
    126   /* Allocate a handler for I/O devices
    127      if no memory for that range has been allocated by the user.
    128      All are allocated in one chunk to keep things from being
    129      unnecessarily complicated.  */
    130   if (sim_core_read_buffer (sd, NULL, read_map, &c, LM32_DEVICE_ADDR, 1) == 0)
    131     sim_core_attach (sd, NULL, 0 /*level */ ,
    132 		     access_read_write, 0 /*space ??? */ ,
    133 		     LM32_DEVICE_ADDR, LM32_DEVICE_LEN /*nr_bytes */ ,
    134 		     0 /*modulo */ ,
    135 		     &lm32_devices, NULL /*buffer */ );
    136 #endif
    137 
    138   /* check for/establish the reference program image.  */
    139   if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
    140     {
    141       free_state (sd);
    142       return 0;
    143     }
    144 
    145   /* Check to see if memory exists at programs start address.  */
    146   if (sim_core_read_buffer (sd, NULL, read_map, &c, STATE_START_ADDR (sd), 1)
    147       == 0)
    148     {
    149       if (STATE_PROG_BFD (sd) != NULL)
    150 	{
    151 	  /* It doesn't, so we should try to allocate enough memory to hold program.  */
    152 	  base = find_base (STATE_PROG_BFD (sd));
    153 	  limit = find_limit (sd);
    154 	  if (limit == 0)
    155 	    {
    156 	      sim_io_eprintf (sd,
    157 			      "Failed to find symbol _fstack in program. You must specify memory regions with --memory-region.\n");
    158 	      free_state (sd);
    159 	      return 0;
    160 	    }
    161 	  /*sim_io_printf (sd, "Allocating memory at 0x%lx size 0x%lx\n", base, limit); */
    162 	  sim_do_commandf (sd, "memory region 0x%lx,0x%lx", base, limit);
    163 	}
    164     }
    165 
    166   /* Establish any remaining configuration options.  */
    167   if (sim_config (sd) != SIM_RC_OK)
    168     {
    169       free_state (sd);
    170       return 0;
    171     }
    172 
    173   if (sim_post_argv_init (sd) != SIM_RC_OK)
    174     {
    175       free_state (sd);
    176       return 0;
    177     }
    178 
    179   /* Open a copy of the cpu descriptor table.  */
    180   {
    181     CGEN_CPU_DESC cd =
    182       lm32_cgen_cpu_open_1 (STATE_ARCHITECTURE (sd)->printable_name,
    183 			    CGEN_ENDIAN_BIG);
    184     for (i = 0; i < MAX_NR_PROCESSORS; ++i)
    185       {
    186 	SIM_CPU *cpu = STATE_CPU (sd, i);
    187 	CPU_CPU_DESC (cpu) = cd;
    188 	CPU_DISASSEMBLER (cpu) = sim_cgen_disassemble_insn;
    189       }
    190     lm32_cgen_init_dis (cd);
    191   }
    192 
    193   return sd;
    194 }
    195 
    196 SIM_RC
    198 sim_create_inferior (SIM_DESC sd, struct bfd *abfd, char * const *argv,
    199 		     char * const *env)
    200 {
    201   SIM_CPU *current_cpu = STATE_CPU (sd, 0);
    202   host_callback *cb = STATE_CALLBACK (sd);
    203   bfd_vma addr;
    204 
    205   if (abfd != NULL)
    206     addr = bfd_get_start_address (abfd);
    207   else
    208     addr = 0;
    209   sim_pc_set (current_cpu, addr);
    210 
    211   /* Standalone mode (i.e. `run`) will take care of the argv for us in
    212      sim_open() -> sim_parse_args().  But in debug mode (i.e. 'target sim'
    213      with `gdb`), we need to handle it because the user can change the
    214      argv on the fly via gdb's 'run'.  */
    215   if (STATE_PROG_ARGV (sd) != argv)
    216     {
    217       freeargv (STATE_PROG_ARGV (sd));
    218       STATE_PROG_ARGV (sd) = dupargv (argv);
    219     }
    220 
    221   if (STATE_PROG_ENVP (sd) != env)
    222     {
    223       freeargv (STATE_PROG_ENVP (sd));
    224       STATE_PROG_ENVP (sd) = dupargv (env);
    225     }
    226 
    227   cb->argv = STATE_PROG_ARGV (sd);
    228   cb->envp = STATE_PROG_ENVP (sd);
    229 
    230   return SIM_RC_OK;
    231 }
    232