1 /* Main simulator entry points specific to Lattice Mico32. 2 Contributed by Jon Beniston <jon@beniston.com> 3 4 Copyright (C) 2009-2020 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 #include "sim-main.h" 22 #include "sim-options.h" 23 #include "libiberty.h" 24 #include "bfd.h" 25 26 #ifdef HAVE_STDLIB_H 27 #include <stdlib.h> 28 #endif 29 30 /* Cover function of sim_state_free to free the cpu buffers as well. */ 31 32 static void 33 free_state (SIM_DESC sd) 34 { 35 if (STATE_MODULES (sd) != NULL) 36 sim_module_uninstall (sd); 37 sim_cpu_free_all (sd); 38 sim_state_free (sd); 39 } 40 41 /* Find memory range used by program. */ 42 43 static unsigned long 44 find_base (bfd *prog_bfd) 45 { 46 int found; 47 unsigned long base = ~(0UL); 48 asection *s; 49 50 found = 0; 51 for (s = prog_bfd->sections; s; s = s->next) 52 { 53 if ((strcmp (bfd_section_name (s), ".boot") == 0) 54 || (strcmp (bfd_section_name (s), ".text") == 0) 55 || (strcmp (bfd_section_name (s), ".data") == 0) 56 || (strcmp (bfd_section_name (s), ".bss") == 0)) 57 { 58 if (!found) 59 { 60 base = bfd_section_vma (s); 61 found = 1; 62 } 63 else 64 base = bfd_section_vma (s) < base ? bfd_section_vma (s) : base; 65 } 66 } 67 return base & ~(0xffffUL); 68 } 69 70 static unsigned long 71 find_limit (SIM_DESC sd) 72 { 73 bfd_vma addr; 74 75 addr = trace_sym_value (sd, "_fstack"); 76 if (addr == -1) 77 return 0; 78 79 return (addr + 65536) & ~(0xffffUL); 80 } 81 82 /* Create an instance of the simulator. */ 83 84 SIM_DESC 85 sim_open (kind, callback, abfd, argv) 86 SIM_OPEN_KIND kind; 87 host_callback *callback; 88 struct bfd *abfd; 89 char * const *argv; 90 { 91 SIM_DESC sd = sim_state_alloc (kind, callback); 92 char c; 93 int i; 94 unsigned long base, limit; 95 96 /* The cpu data is kept in a separately allocated chunk of memory. */ 97 if (sim_cpu_alloc_all (sd, 1, cgen_cpu_max_extra_bytes ()) != SIM_RC_OK) 98 { 99 free_state (sd); 100 return 0; 101 } 102 103 if (sim_pre_argv_init (sd, argv[0]) != SIM_RC_OK) 104 { 105 free_state (sd); 106 return 0; 107 } 108 109 /* The parser will print an error message for us, so we silently return. */ 110 if (sim_parse_args (sd, argv) != SIM_RC_OK) 111 { 112 free_state (sd); 113 return 0; 114 } 115 116 #if 0 117 /* Allocate a handler for I/O devices 118 if no memory for that range has been allocated by the user. 119 All are allocated in one chunk to keep things from being 120 unnecessarily complicated. */ 121 if (sim_core_read_buffer (sd, NULL, read_map, &c, LM32_DEVICE_ADDR, 1) == 0) 122 sim_core_attach (sd, NULL, 0 /*level */ , 123 access_read_write, 0 /*space ??? */ , 124 LM32_DEVICE_ADDR, LM32_DEVICE_LEN /*nr_bytes */ , 125 0 /*modulo */ , 126 &lm32_devices, NULL /*buffer */ ); 127 #endif 128 129 /* check for/establish the reference program image. */ 130 if (sim_analyze_program (sd, 131 (STATE_PROG_ARGV (sd) != NULL 132 ? *STATE_PROG_ARGV (sd) 133 : NULL), abfd) != SIM_RC_OK) 134 { 135 free_state (sd); 136 return 0; 137 } 138 139 /* Check to see if memory exists at programs start address. */ 140 if (sim_core_read_buffer (sd, NULL, read_map, &c, STATE_START_ADDR (sd), 1) 141 == 0) 142 { 143 if (STATE_PROG_BFD (sd) != NULL) 144 { 145 /* It doesn't, so we should try to allocate enough memory to hold program. */ 146 base = find_base (STATE_PROG_BFD (sd)); 147 limit = find_limit (sd); 148 if (limit == 0) 149 { 150 sim_io_eprintf (sd, 151 "Failed to find symbol _fstack in program. You must specify memory regions with --memory-region.\n"); 152 free_state (sd); 153 return 0; 154 } 155 /*sim_io_printf (sd, "Allocating memory at 0x%x size 0x%x\n", base, limit); */ 156 sim_do_commandf (sd, "memory region 0x%x,0x%x", base, limit); 157 } 158 } 159 160 /* Establish any remaining configuration options. */ 161 if (sim_config (sd) != SIM_RC_OK) 162 { 163 free_state (sd); 164 return 0; 165 } 166 167 if (sim_post_argv_init (sd) != SIM_RC_OK) 168 { 169 free_state (sd); 170 return 0; 171 } 172 173 /* Open a copy of the cpu descriptor table. */ 174 { 175 CGEN_CPU_DESC cd = 176 lm32_cgen_cpu_open_1 (STATE_ARCHITECTURE (sd)->printable_name, 177 CGEN_ENDIAN_BIG); 178 for (i = 0; i < MAX_NR_PROCESSORS; ++i) 179 { 180 SIM_CPU *cpu = STATE_CPU (sd, i); 181 CPU_CPU_DESC (cpu) = cd; 182 CPU_DISASSEMBLER (cpu) = sim_cgen_disassemble_insn; 183 } 184 lm32_cgen_init_dis (cd); 185 } 186 187 /* Initialize various cgen things not done by common framework. 188 Must be done after lm32_cgen_cpu_open. */ 189 cgen_init (sd); 190 191 return sd; 192 } 193 194 SIM_RC 195 sim_create_inferior (sd, abfd, argv, envp) 196 SIM_DESC sd; 197 struct bfd *abfd; 198 char * const *argv; 199 char * const *envp; 200 { 201 SIM_CPU *current_cpu = STATE_CPU (sd, 0); 202 SIM_ADDR addr; 203 204 if (abfd != NULL) 205 addr = bfd_get_start_address (abfd); 206 else 207 addr = 0; 208 sim_pc_set (current_cpu, addr); 209 210 /* Standalone mode (i.e. `run`) will take care of the argv for us in 211 sim_open() -> sim_parse_args(). But in debug mode (i.e. 'target sim' 212 with `gdb`), we need to handle it because the user can change the 213 argv on the fly via gdb's 'run'. */ 214 if (STATE_PROG_ARGV (sd) != argv) 215 { 216 freeargv (STATE_PROG_ARGV (sd)); 217 STATE_PROG_ARGV (sd) = dupargv (argv); 218 } 219 220 return SIM_RC_OK; 221 } 222