| Stan Shebs | c906108 | 1999-04-16 01:35:26 +0000 | [diff] [blame] | 1 | /* CPU support. |
| Tom Tromey | d01e823 | 2025-04-02 13:30:10 -0600 | [diff] [blame] | 2 | Copyright (C) 1998-2025 Free Software Foundation, Inc. |
| Stan Shebs | c906108 | 1999-04-16 01:35:26 +0000 | [diff] [blame] | 3 | Contributed by Cygnus Solutions. |
| 4 | |
| 5 | This file is part of GDB, the GNU debugger. |
| 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| Joel Brobecker | 4744ac1 | 2007-08-24 14:30:15 +0000 | [diff] [blame] | 9 | the Free Software Foundation; either version 3 of the License, or |
| 10 | (at your option) any later version. |
| Stan Shebs | c906108 | 1999-04-16 01:35:26 +0000 | [diff] [blame] | 11 | |
| 12 | This program is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| Joel Brobecker | 4744ac1 | 2007-08-24 14:30:15 +0000 | [diff] [blame] | 17 | You should have received a copy of the GNU General Public License |
| 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| Stan Shebs | c906108 | 1999-04-16 01:35:26 +0000 | [diff] [blame] | 19 | |
| Mike Frysinger | 6df01ab | 2021-05-01 18:05:23 -0400 | [diff] [blame] | 20 | /* This must come before any other includes. */ |
| 21 | #include "defs.h" |
| 22 | |
| Tom de Vries | b3f8962 | 2020-08-10 17:26:09 +0200 | [diff] [blame] | 23 | #include <stdlib.h> |
| 24 | |
| Stan Shebs | c906108 | 1999-04-16 01:35:26 +0000 | [diff] [blame] | 25 | #include "bfd.h" |
| 26 | |
| Mike Frysinger | 20a8e07 | 2021-12-04 20:24:55 -0500 | [diff] [blame] | 27 | #include "sim-main.h" |
| 28 | |
| Stan Shebs | c906108 | 1999-04-16 01:35:26 +0000 | [diff] [blame] | 29 | /* Allocate space for all cpus in the simulator. |
| Mike Frysinger | d5a71b1 | 2016-08-12 22:12:41 +0800 | [diff] [blame] | 30 | Space for the cpu must currently exist prior to parsing ARGV. */ |
| Stan Shebs | c906108 | 1999-04-16 01:35:26 +0000 | [diff] [blame] | 31 | /* ??? wip. better solution must wait. */ |
| 32 | |
| 33 | SIM_RC |
| Mike Frysinger | ffeb72b | 2016-08-12 23:43:27 +0800 | [diff] [blame] | 34 | sim_cpu_alloc_all_extra (SIM_DESC sd, int ncpus, size_t extra_bytes) |
| Stan Shebs | c906108 | 1999-04-16 01:35:26 +0000 | [diff] [blame] | 35 | { |
| 36 | int c; |
| 37 | |
| Mike Frysinger | 883be19 | 2022-12-25 00:53:25 -0500 | [diff] [blame] | 38 | /* TODO: This should be a command line option for users to control. */ |
| 39 | if (ncpus == 0) |
| 40 | ncpus = MAX_NR_PROCESSORS; |
| 41 | |
| Stan Shebs | c906108 | 1999-04-16 01:35:26 +0000 | [diff] [blame] | 42 | for (c = 0; c < ncpus; ++c) |
| Mike Frysinger | ffeb72b | 2016-08-12 23:43:27 +0800 | [diff] [blame] | 43 | STATE_CPU (sd, c) = sim_cpu_alloc_extra (sd, extra_bytes); |
| Mike Frysinger | 883be19 | 2022-12-25 00:53:25 -0500 | [diff] [blame] | 44 | |
| Stan Shebs | c906108 | 1999-04-16 01:35:26 +0000 | [diff] [blame] | 45 | 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 | |
| 51 | sim_cpu * |
| Mike Frysinger | ffeb72b | 2016-08-12 23:43:27 +0800 | [diff] [blame] | 52 | sim_cpu_alloc_extra (SIM_DESC sd, size_t extra_bytes) |
| Stan Shebs | c906108 | 1999-04-16 01:35:26 +0000 | [diff] [blame] | 53 | { |
| Mike Frysinger | 4a21ad1 | 2022-10-31 23:39:51 +0545 | [diff] [blame] | 54 | sim_cpu *cpu = zalloc (sizeof (*cpu)); |
| Mike Frysinger | d5a71b1 | 2016-08-12 22:12:41 +0800 | [diff] [blame] | 55 | |
| Mike Frysinger | ffeb72b | 2016-08-12 23:43:27 +0800 | [diff] [blame] | 56 | #ifndef CGEN_ARCH |
| 57 | # define cgen_cpu_max_extra_bytes(sd) 0 |
| 58 | #endif |
| Mike Frysinger | 1c636da | 2021-06-28 21:42:56 -0400 | [diff] [blame] | 59 | extra_bytes += cgen_cpu_max_extra_bytes (sd); |
| Mike Frysinger | ffeb72b | 2016-08-12 23:43:27 +0800 | [diff] [blame] | 60 | if (extra_bytes) |
| 61 | CPU_ARCH_DATA (cpu) = zalloc (extra_bytes); |
| Mike Frysinger | d5a71b1 | 2016-08-12 22:12:41 +0800 | [diff] [blame] | 62 | |
| Mike Frysinger | ffeb72b | 2016-08-12 23:43:27 +0800 | [diff] [blame] | 63 | return cpu; |
| Stan Shebs | c906108 | 1999-04-16 01:35:26 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | /* Free all resources held by all cpus. */ |
| 67 | |
| 68 | void |
| 69 | sim_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 | |
| 80 | void |
| 81 | sim_cpu_free (sim_cpu *cpu) |
| 82 | { |
| Mike Frysinger | ffeb72b | 2016-08-12 23:43:27 +0800 | [diff] [blame] | 83 | free (CPU_ARCH_DATA (cpu)); |
| Mike Frysinger | d79fe0d | 2011-02-14 05:14:28 +0000 | [diff] [blame] | 84 | free (cpu); |
| Stan Shebs | c906108 | 1999-04-16 01:35:26 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /* PC utilities. */ |
| 88 | |
| 89 | sim_cia |
| 90 | sim_pc_get (sim_cpu *cpu) |
| 91 | { |
| 92 | return (* CPU_PC_FETCH (cpu)) (cpu); |
| 93 | } |
| 94 | |
| 95 | void |
| 96 | sim_pc_set (sim_cpu *cpu, sim_cia newval) |
| 97 | { |
| 98 | (* CPU_PC_STORE (cpu)) (cpu, newval); |
| 99 | } |