14e98e3e1Schristos /* The common simulator framework for GDB, the GNU Debugger. 24e98e3e1Schristos 3*88241920Schristos Copyright 2002-2024 Free Software Foundation, Inc. 44e98e3e1Schristos 54e98e3e1Schristos Contributed by Andrew Cagney and Red Hat. 64e98e3e1Schristos 74e98e3e1Schristos This file is part of GDB. 84e98e3e1Schristos 94e98e3e1Schristos This program is free software; you can redistribute it and/or modify 104e98e3e1Schristos it under the terms of the GNU General Public License as published by 114e98e3e1Schristos the Free Software Foundation; either version 3 of the License, or 124e98e3e1Schristos (at your option) any later version. 134e98e3e1Schristos 144e98e3e1Schristos This program is distributed in the hope that it will be useful, 154e98e3e1Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of 164e98e3e1Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 174e98e3e1Schristos GNU General Public License for more details. 184e98e3e1Schristos 194e98e3e1Schristos You should have received a copy of the GNU General Public License 204e98e3e1Schristos along with this program. If not, see <http://www.gnu.org/licenses/>. */ 214e98e3e1Schristos 224b169a6bSchristos /* This must come before any other includes. */ 234b169a6bSchristos #include "defs.h" 244b169a6bSchristos 254b169a6bSchristos #include <stdlib.h> 264e98e3e1Schristos 274e98e3e1Schristos #include "hw-main.h" 284e98e3e1Schristos #include "hw-base.h" 294e98e3e1Schristos 304e98e3e1Schristos struct hw_handle_mapping 314e98e3e1Schristos { 324e98e3e1Schristos cell_word external; 334e98e3e1Schristos struct hw *phandle; 344e98e3e1Schristos struct hw_instance *ihandle; 354e98e3e1Schristos struct hw_handle_mapping *next; 364e98e3e1Schristos }; 374e98e3e1Schristos 384e98e3e1Schristos 394e98e3e1Schristos struct hw_handle_data 404e98e3e1Schristos { 414e98e3e1Schristos int nr_mappings; 424e98e3e1Schristos struct hw_handle_mapping *mappings; 434e98e3e1Schristos }; 444e98e3e1Schristos 454e98e3e1Schristos void 464e98e3e1Schristos create_hw_handle_data (struct hw *hw) 474e98e3e1Schristos { 484e98e3e1Schristos if (hw_parent (hw) == NULL) 494e98e3e1Schristos { 504e98e3e1Schristos hw->handles_of_hw = HW_ZALLOC (hw, struct hw_handle_data); 514e98e3e1Schristos } 524e98e3e1Schristos else 534e98e3e1Schristos { 544e98e3e1Schristos hw->handles_of_hw = hw_root (hw)->handles_of_hw; 554e98e3e1Schristos } 564e98e3e1Schristos } 574e98e3e1Schristos 584e98e3e1Schristos void 594e98e3e1Schristos delete_hw_handle_data (struct hw *hw) 604e98e3e1Schristos { 614e98e3e1Schristos /* NULL */ 624e98e3e1Schristos } 634e98e3e1Schristos 644e98e3e1Schristos 654e98e3e1Schristos 664e98e3e1Schristos #if 0 674e98e3e1Schristos void 684e98e3e1Schristos hw_handle_init (struct hw *hw) 694e98e3e1Schristos { 704e98e3e1Schristos struct hw_handle_mapping *current_map = db->mappings; 714e98e3e1Schristos if (current_map != NULL) 724e98e3e1Schristos { 734e98e3e1Schristos db->nr_mappings = db->mappings->external; 744e98e3e1Schristos /* verify that the mappings that were not removed are in 754e98e3e1Schristos sequence down to nr 1 */ 764e98e3e1Schristos while (current_map->next != NULL) 774e98e3e1Schristos { 784e98e3e1Schristos if (current_map->external != current_map->next->external + 1) 794e98e3e1Schristos error ("hw_handle: hw_handle database possibly corrupt"); 804e98e3e1Schristos current_map = current_map->next; 814e98e3e1Schristos } 824e98e3e1Schristos ASSERT (current_map->next == NULL); 834e98e3e1Schristos if (current_map->external != 1) 844e98e3e1Schristos error ("hw_handle: hw_handle database possibly corrupt"); 854e98e3e1Schristos } 864e98e3e1Schristos else 874e98e3e1Schristos { 884e98e3e1Schristos db->nr_mappings = 0; 894e98e3e1Schristos } 904e98e3e1Schristos } 914e98e3e1Schristos #endif 924e98e3e1Schristos 934e98e3e1Schristos 944e98e3e1Schristos struct hw_instance * 954e98e3e1Schristos hw_handle_ihandle2 (struct hw *hw, 964e98e3e1Schristos cell_word external) 974e98e3e1Schristos { 984e98e3e1Schristos struct hw_handle_data *db = hw->handles_of_hw; 994e98e3e1Schristos struct hw_handle_mapping *current_map = db->mappings; 1004e98e3e1Schristos while (current_map != NULL) 1014e98e3e1Schristos { 1024e98e3e1Schristos if (current_map->external == external) 1034e98e3e1Schristos return current_map->ihandle; 1044e98e3e1Schristos current_map = current_map->next; 1054e98e3e1Schristos } 1064e98e3e1Schristos return (void*)0; 1074e98e3e1Schristos } 1084e98e3e1Schristos 1094e98e3e1Schristos 1104e98e3e1Schristos struct hw * 1114e98e3e1Schristos hw_handle_phandle2 (struct hw *hw, 1124e98e3e1Schristos cell_word external) 1134e98e3e1Schristos { 1144e98e3e1Schristos struct hw_handle_data *db = hw->handles_of_hw; 1154e98e3e1Schristos struct hw_handle_mapping *current_map = db->mappings; 1164e98e3e1Schristos while (current_map != NULL) 1174e98e3e1Schristos { 1184e98e3e1Schristos if (current_map->external == external) 1194e98e3e1Schristos return current_map->phandle; 1204e98e3e1Schristos current_map = current_map->next; 1214e98e3e1Schristos } 1224e98e3e1Schristos return (void*)0; 1234e98e3e1Schristos } 1244e98e3e1Schristos 1254e98e3e1Schristos 1264e98e3e1Schristos cell_word 1274e98e3e1Schristos hw_handle_2ihandle (struct hw *hw, 1284e98e3e1Schristos struct hw_instance *internal) 1294e98e3e1Schristos { 1304e98e3e1Schristos struct hw_handle_data *db = hw->handles_of_hw; 1314e98e3e1Schristos struct hw_handle_mapping *current_map = db->mappings; 1324e98e3e1Schristos while (current_map != NULL) 1334e98e3e1Schristos { 1344e98e3e1Schristos if (current_map->ihandle == internal) 1354e98e3e1Schristos return current_map->external; 1364e98e3e1Schristos current_map = current_map->next; 1374e98e3e1Schristos } 1384e98e3e1Schristos return 0; 1394e98e3e1Schristos } 1404e98e3e1Schristos 1414e98e3e1Schristos 1424e98e3e1Schristos cell_word 1434e98e3e1Schristos hw_handle_2phandle (struct hw *hw, 1444e98e3e1Schristos struct hw *internal) 1454e98e3e1Schristos { 1464e98e3e1Schristos struct hw_handle_data *db = hw->handles_of_hw; 1474e98e3e1Schristos struct hw_handle_mapping *current_map = db->mappings; 1484e98e3e1Schristos while (current_map != NULL) 1494e98e3e1Schristos { 1504e98e3e1Schristos if (current_map->phandle == internal) 1514e98e3e1Schristos return current_map->external; 1524e98e3e1Schristos current_map = current_map->next; 1534e98e3e1Schristos } 1544e98e3e1Schristos return 0; 1554e98e3e1Schristos } 1564e98e3e1Schristos 1574e98e3e1Schristos 1584e98e3e1Schristos void 1594e98e3e1Schristos hw_handle_add_ihandle (struct hw *hw, 1604e98e3e1Schristos struct hw_instance *internal) 1614e98e3e1Schristos { 1624e98e3e1Schristos struct hw_handle_data *db = hw->handles_of_hw; 1634e98e3e1Schristos if (hw_handle_2ihandle (hw, internal) != 0) 1644e98e3e1Schristos { 1654e98e3e1Schristos hw_abort (hw, "attempting to add an ihandle already in the data base"); 1664e98e3e1Schristos } 1674e98e3e1Schristos else 1684e98e3e1Schristos { 1694e98e3e1Schristos /* insert at the front making things in decending order */ 1704e98e3e1Schristos struct hw_handle_mapping *new_map = ZALLOC (struct hw_handle_mapping); 1714e98e3e1Schristos new_map->next = db->mappings; 1724e98e3e1Schristos new_map->ihandle = internal; 1734e98e3e1Schristos db->nr_mappings += 1; 1744e98e3e1Schristos new_map->external = db->nr_mappings; 1754e98e3e1Schristos db->mappings = new_map; 1764e98e3e1Schristos } 1774e98e3e1Schristos } 1784e98e3e1Schristos 1794e98e3e1Schristos 1804e98e3e1Schristos void 1814e98e3e1Schristos hw_handle_add_phandle (struct hw *hw, 1824e98e3e1Schristos struct hw *internal) 1834e98e3e1Schristos { 1844e98e3e1Schristos struct hw_handle_data *db = hw->handles_of_hw; 1854e98e3e1Schristos if (hw_handle_2phandle (hw, internal) != 0) 1864e98e3e1Schristos { 1874e98e3e1Schristos hw_abort (hw, "attempting to add a phandle already in the data base"); 1884e98e3e1Schristos } 1894e98e3e1Schristos else 1904e98e3e1Schristos { 1914e98e3e1Schristos /* insert at the front making things in decending order */ 1924e98e3e1Schristos struct hw_handle_mapping *new_map = ZALLOC (struct hw_handle_mapping); 1934e98e3e1Schristos new_map->next = db->mappings; 1944e98e3e1Schristos new_map->phandle = internal; 1954e98e3e1Schristos db->nr_mappings += 1; 1964e98e3e1Schristos new_map->external = db->nr_mappings; 1974e98e3e1Schristos db->mappings = new_map; 1984e98e3e1Schristos } 1994e98e3e1Schristos } 2004e98e3e1Schristos 2014e98e3e1Schristos 2024e98e3e1Schristos void 2034e98e3e1Schristos hw_handle_remove_ihandle (struct hw *hw, 2044e98e3e1Schristos struct hw_instance *internal) 2054e98e3e1Schristos { 2064e98e3e1Schristos struct hw_handle_data *db = hw->handles_of_hw; 2074e98e3e1Schristos struct hw_handle_mapping **current_map = &db->mappings; 2084e98e3e1Schristos while (*current_map != NULL) 2094e98e3e1Schristos { 2104e98e3e1Schristos if ((*current_map)->ihandle == internal) 2114e98e3e1Schristos { 2124e98e3e1Schristos struct hw_handle_mapping *delete = *current_map; 2134e98e3e1Schristos *current_map = delete->next; 2144e98e3e1Schristos free (delete); 2154e98e3e1Schristos return; 2164e98e3e1Schristos } 2174e98e3e1Schristos current_map = &(*current_map)->next; 2184e98e3e1Schristos } 2194e98e3e1Schristos hw_abort (hw, "attempt to remove nonexistant ihandle"); 2204e98e3e1Schristos } 2214e98e3e1Schristos 2224e98e3e1Schristos 2234e98e3e1Schristos void 2244e98e3e1Schristos hw_handle_remove_phandle (struct hw *hw, 2254e98e3e1Schristos struct hw *internal) 2264e98e3e1Schristos { 2274e98e3e1Schristos struct hw_handle_data *db = hw->handles_of_hw; 2284e98e3e1Schristos struct hw_handle_mapping **current_map = &db->mappings; 2294e98e3e1Schristos while (*current_map != NULL) 2304e98e3e1Schristos { 2314e98e3e1Schristos if ((*current_map)->phandle == internal) 2324e98e3e1Schristos { 2334e98e3e1Schristos struct hw_handle_mapping *delete = *current_map; 2344e98e3e1Schristos *current_map = delete->next; 2354e98e3e1Schristos free (delete); 2364e98e3e1Schristos return; 2374e98e3e1Schristos } 2384e98e3e1Schristos current_map = &(*current_map)->next; 2394e98e3e1Schristos } 2404e98e3e1Schristos hw_abort (hw, "attempt to remove nonexistant phandle"); 2414e98e3e1Schristos } 242