blob: 28c23057d7bb11265a5ba2b8e93bb4a2bba08e82 [file] [log] [blame]
Stan Shebsc9061081999-04-16 01:35:26 +00001/* CPU support.
Tom Tromeyd01e8232025-04-02 13:30:10 -06002 Copyright (C) 1998-2025 Free Software Foundation, Inc.
Stan Shebsc9061081999-04-16 01:35:26 +00003 Contributed by Cygnus Solutions.
4
5This file is part of GDB, the GNU debugger.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
Joel Brobecker4744ac12007-08-24 14:30:15 +00009the Free Software Foundation; either version 3 of the License, or
10(at your option) any later version.
Stan Shebsc9061081999-04-16 01:35:26 +000011
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
Joel Brobecker4744ac12007-08-24 14:30:15 +000017You should have received a copy of the GNU General Public License
18along with this program. If not, see <http://www.gnu.org/licenses/>. */
Stan Shebsc9061081999-04-16 01:35:26 +000019
Mike Frysinger6df01ab2021-05-01 18:05:23 -040020/* This must come before any other includes. */
21#include "defs.h"
22
Tom de Vriesb3f89622020-08-10 17:26:09 +020023#include <stdlib.h>
24
Stan Shebsc9061081999-04-16 01:35:26 +000025#include "bfd.h"
26
Mike Frysinger20a8e072021-12-04 20:24:55 -050027#include "sim-main.h"
28
Stan Shebsc9061081999-04-16 01:35:26 +000029/* Allocate space for all cpus in the simulator.
Mike Frysingerd5a71b12016-08-12 22:12:41 +080030 Space for the cpu must currently exist prior to parsing ARGV. */
Stan Shebsc9061081999-04-16 01:35:26 +000031/* ??? wip. better solution must wait. */
32
33SIM_RC
Mike Frysingerffeb72b2016-08-12 23:43:27 +080034sim_cpu_alloc_all_extra (SIM_DESC sd, int ncpus, size_t extra_bytes)
Stan Shebsc9061081999-04-16 01:35:26 +000035{
36 int c;
37
Mike Frysinger883be192022-12-25 00:53:25 -050038 /* TODO: This should be a command line option for users to control. */
39 if (ncpus == 0)
40 ncpus = MAX_NR_PROCESSORS;
41
Stan Shebsc9061081999-04-16 01:35:26 +000042 for (c = 0; c < ncpus; ++c)
Mike Frysingerffeb72b2016-08-12 23:43:27 +080043 STATE_CPU (sd, c) = sim_cpu_alloc_extra (sd, extra_bytes);
Mike Frysinger883be192022-12-25 00:53:25 -050044
Stan Shebsc9061081999-04-16 01:35:26 +000045 return SIM_RC_OK;
46}
47
48/* Allocate space for a cpu object.
49 EXTRA_BYTES is additional space to allocate for the sim_cpu struct. */
50
51sim_cpu *
Mike Frysingerffeb72b2016-08-12 23:43:27 +080052sim_cpu_alloc_extra (SIM_DESC sd, size_t extra_bytes)
Stan Shebsc9061081999-04-16 01:35:26 +000053{
Mike Frysinger4a21ad12022-10-31 23:39:51 +054554 sim_cpu *cpu = zalloc (sizeof (*cpu));
Mike Frysingerd5a71b12016-08-12 22:12:41 +080055
Mike Frysingerffeb72b2016-08-12 23:43:27 +080056#ifndef CGEN_ARCH
57# define cgen_cpu_max_extra_bytes(sd) 0
58#endif
Mike Frysinger1c636da2021-06-28 21:42:56 -040059 extra_bytes += cgen_cpu_max_extra_bytes (sd);
Mike Frysingerffeb72b2016-08-12 23:43:27 +080060 if (extra_bytes)
61 CPU_ARCH_DATA (cpu) = zalloc (extra_bytes);
Mike Frysingerd5a71b12016-08-12 22:12:41 +080062
Mike Frysingerffeb72b2016-08-12 23:43:27 +080063 return cpu;
Stan Shebsc9061081999-04-16 01:35:26 +000064}
65
66/* Free all resources held by all cpus. */
67
68void
69sim_cpu_free_all (SIM_DESC sd)
70{
71 int c;
72
73 for (c = 0; c < MAX_NR_PROCESSORS; ++c)
74 if (STATE_CPU (sd, c))
75 sim_cpu_free (STATE_CPU (sd, c));
76}
77
78/* Free all resources used by CPU. */
79
80void
81sim_cpu_free (sim_cpu *cpu)
82{
Mike Frysingerffeb72b2016-08-12 23:43:27 +080083 free (CPU_ARCH_DATA (cpu));
Mike Frysingerd79fe0d2011-02-14 05:14:28 +000084 free (cpu);
Stan Shebsc9061081999-04-16 01:35:26 +000085}
86
87/* PC utilities. */
88
89sim_cia
90sim_pc_get (sim_cpu *cpu)
91{
92 return (* CPU_PC_FETCH (cpu)) (cpu);
93}
94
95void
96sim_pc_set (sim_cpu *cpu, sim_cia newval)
97{
98 (* CPU_PC_STORE (cpu)) (cpu, newval);
99}