xref: /netbsd-src/external/gpl3/gdb/dist/sim/common/sim-cpu.c (revision 88241920d21b339bf319c0e979ffda80c49a2936)
14e98e3e1Schristos /* CPU support.
2*88241920Schristos    Copyright (C) 1998-2024 Free Software Foundation, Inc.
34e98e3e1Schristos    Contributed by Cygnus Solutions.
44e98e3e1Schristos 
54e98e3e1Schristos This file is part of GDB, the GNU debugger.
64e98e3e1Schristos 
74e98e3e1Schristos This program is free software; you can redistribute it and/or modify
84e98e3e1Schristos it under the terms of the GNU General Public License as published by
94e98e3e1Schristos the Free Software Foundation; either version 3 of the License, or
104e98e3e1Schristos (at your option) any later version.
114e98e3e1Schristos 
124e98e3e1Schristos This program is distributed in the hope that it will be useful,
134e98e3e1Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of
144e98e3e1Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
154e98e3e1Schristos GNU General Public License for more details.
164e98e3e1Schristos 
174e98e3e1Schristos You should have received a copy of the GNU General Public License
184e98e3e1Schristos along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
194e98e3e1Schristos 
204b169a6bSchristos /* This must come before any other includes.  */
214b169a6bSchristos #include "defs.h"
224b169a6bSchristos 
238dffb485Schristos #include <stdlib.h>
248dffb485Schristos 
254e98e3e1Schristos #include "bfd.h"
264e98e3e1Schristos 
274b169a6bSchristos #include "sim-main.h"
284b169a6bSchristos 
294e98e3e1Schristos /* Allocate space for all cpus in the simulator.
304b169a6bSchristos    Space for the cpu must currently exist prior to parsing ARGV.  */
314e98e3e1Schristos /* ??? wip.  better solution must wait.  */
324e98e3e1Schristos 
334e98e3e1Schristos SIM_RC
34*88241920Schristos sim_cpu_alloc_all_extra (SIM_DESC sd, int ncpus, size_t extra_bytes)
354e98e3e1Schristos {
364e98e3e1Schristos   int c;
374e98e3e1Schristos 
38*88241920Schristos   /* TODO: This should be a command line option for users to control.  */
39*88241920Schristos   if (ncpus == 0)
40*88241920Schristos     ncpus = MAX_NR_PROCESSORS;
41*88241920Schristos 
424e98e3e1Schristos   for (c = 0; c < ncpus; ++c)
43*88241920Schristos     STATE_CPU (sd, c) = sim_cpu_alloc_extra (sd, extra_bytes);
44*88241920Schristos 
454e98e3e1Schristos   return SIM_RC_OK;
464e98e3e1Schristos }
474e98e3e1Schristos 
484e98e3e1Schristos /* Allocate space for a cpu object.
494e98e3e1Schristos    EXTRA_BYTES is additional space to allocate for the sim_cpu struct.  */
504e98e3e1Schristos 
514e98e3e1Schristos sim_cpu *
52*88241920Schristos sim_cpu_alloc_extra (SIM_DESC sd, size_t extra_bytes)
534e98e3e1Schristos {
54*88241920Schristos   sim_cpu *cpu = zalloc (sizeof (*cpu));
554b169a6bSchristos 
56*88241920Schristos #ifndef CGEN_ARCH
57*88241920Schristos # define cgen_cpu_max_extra_bytes(sd) 0
584b169a6bSchristos #endif
59*88241920Schristos   extra_bytes += cgen_cpu_max_extra_bytes (sd);
60*88241920Schristos   if (extra_bytes)
61*88241920Schristos     CPU_ARCH_DATA (cpu) = zalloc (extra_bytes);
624b169a6bSchristos 
63*88241920Schristos   return cpu;
644e98e3e1Schristos }
654e98e3e1Schristos 
664e98e3e1Schristos /* Free all resources held by all cpus.  */
674e98e3e1Schristos 
684e98e3e1Schristos void
694e98e3e1Schristos sim_cpu_free_all (SIM_DESC sd)
704e98e3e1Schristos {
714e98e3e1Schristos   int c;
724e98e3e1Schristos 
734e98e3e1Schristos   for (c = 0; c < MAX_NR_PROCESSORS; ++c)
744e98e3e1Schristos     if (STATE_CPU (sd, c))
754e98e3e1Schristos       sim_cpu_free (STATE_CPU (sd, c));
764e98e3e1Schristos }
774e98e3e1Schristos 
784e98e3e1Schristos /* Free all resources used by CPU.  */
794e98e3e1Schristos 
804e98e3e1Schristos void
814e98e3e1Schristos sim_cpu_free (sim_cpu *cpu)
824e98e3e1Schristos {
83*88241920Schristos   free (CPU_ARCH_DATA (cpu));
844e98e3e1Schristos   free (cpu);
854e98e3e1Schristos }
864e98e3e1Schristos 
874e98e3e1Schristos /* PC utilities.  */
884e98e3e1Schristos 
894e98e3e1Schristos sim_cia
904e98e3e1Schristos sim_pc_get (sim_cpu *cpu)
914e98e3e1Schristos {
924e98e3e1Schristos   return (* CPU_PC_FETCH (cpu)) (cpu);
934e98e3e1Schristos }
944e98e3e1Schristos 
954e98e3e1Schristos void
964e98e3e1Schristos sim_pc_set (sim_cpu *cpu, sim_cia newval)
974e98e3e1Schristos {
984e98e3e1Schristos   (* CPU_PC_STORE (cpu)) (cpu, newval);
994e98e3e1Schristos }
100