1 /* Caching code. Typically used by remote back ends for
4 Copyright 1992, 1993 Free Software Foundation, Inc.
6 This file is part of GDB.
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 2 of the License, or
11 (at your option) any later version.
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.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
29 int remote_dcache = 0;
31 /* The data cache records all the data read from the remote machine
32 since the last time it stopped.
34 Each cache block holds LINE_SIZE bytes of data
35 starting at a multiple-of-LINE_SIZE address. */
37 #define LINE_SIZE_MASK ((LINE_SIZE - 1)) /* eg 7*2+1= 111*/
38 #define XFORM(x) (((x) & LINE_SIZE_MASK) >> 2)
40 /* Free all the data cache blocks, thus discarding all cached data. */
45 register struct dcache_block *db;
47 if (remote_dcache > 0)
48 while ((db = dcache->dcache_valid.next) != &dcache->dcache_valid)
51 insque (db, &dcache->dcache_free);
58 * If addr is present in the dcache, return the address of the block
63 dcache_hit (dcache, addr)
67 register struct dcache_block *db;
70 || remote_dcache == 0)
73 /* Search all cache blocks for one that is at this address. */
74 db = dcache->dcache_valid.next;
75 while (db != &dcache->dcache_valid)
77 if ((addr & ~LINE_SIZE_MASK) == db->addr)
85 /* Return the int data at address ADDR in dcache block DC. */
88 dcache_value (db, addr)
89 struct dcache_block *db;
93 || remote_dcache == 0)
95 return (db->data[XFORM (addr)]);
98 /* Get a free cache block, put or keep it on the valid list,
99 and return its address. The caller should store into the block
100 the address and data that it describes, then remque it from the
101 free list and insert it into the valid list. This procedure
102 prevents errors from creeping in if a memory retrieval is
103 interrupted (which used to put garbage blocks in the valid
106 struct dcache_block *
107 dcache_alloc (dcache)
110 register struct dcache_block *db;
112 if (remote_dcache == 0)
115 if ((db = dcache->dcache_free.next) == &dcache->dcache_free)
117 /* If we can't get one from the free list, take last valid and put
118 it on the free list. */
119 db = dcache->dcache_valid.last;
121 insque (db, &dcache->dcache_free);
125 insque (db, &dcache->dcache_valid);
129 /* Using the data cache DCACHE return the contents of the word at
130 address ADDR in the remote machine. */
132 dcache_fetch (dcache, addr)
136 register struct dcache_block *db;
138 if (remote_dcache == 0)
142 (*dcache->read_memory) (addr, (unsigned char *) &i, 4);
146 db = dcache_hit (dcache, addr);
149 db = dcache_alloc (dcache);
151 (*dcache->read_memory) (addr & ~LINE_SIZE_MASK, (unsigned char *) db->data, LINE_SIZE);
153 db->addr = addr & ~LINE_SIZE_MASK;
154 remque (db); /* Off the free list */
155 insque (db, &dcache->dcache_valid); /* On the valid list */
157 return (dcache_value (db, addr));
160 /* Write the word at ADDR both in the data cache and in the remote machine. */
162 dcache_poke (dcache, addr, data)
167 register struct dcache_block *db;
169 if (remote_dcache == 0)
171 (*dcache->write_memory) (addr, (unsigned char *) &data, 4);
175 /* First make sure the word is IN the cache. DB is its cache block. */
176 db = dcache_hit (dcache, addr);
179 db = dcache_alloc (dcache);
181 (*dcache->write_memory) (addr & ~LINE_SIZE_MASK, (unsigned char *) db->data, LINE_SIZE);
183 db->addr = addr & ~LINE_SIZE_MASK;
184 remque (db); /* Off the free list */
185 insque (db, &dcache->dcache_valid); /* On the valid list */
188 /* Modify the word in the cache. */
189 db->data[XFORM (addr)] = data;
191 /* Send the changed word. */
193 (*dcache->write_memory) (addr, (unsigned char *) &data, 4);
197 /* Initialize the data cache. */
199 dcache_init (reading, writing)
204 register struct dcache_block *db;
207 dcache = (DCACHE *) xmalloc (sizeof (*dcache));
208 dcache->read_memory = reading;
209 dcache->write_memory = writing;
210 dcache->the_cache = (struct dcache_block *)
211 xmalloc (sizeof (*dcache->the_cache) * DCACHE_SIZE);
213 dcache->dcache_free.next = dcache->dcache_free.last = &dcache->dcache_free;
214 dcache->dcache_valid.next = dcache->dcache_valid.last = &dcache->dcache_valid;
215 for (db = dcache->the_cache, i = 0; i < DCACHE_SIZE; i++, db++)
216 insque (db, &dcache->dcache_free);
222 _initialitize_dcache ()
225 (add_set_cmd ("remotecache", class_support, var_boolean,
226 (char *) &remote_dcache,
228 Set cache use for remote targets.\n\
229 When on, use data caching for remote targets. For many remote targets\n\
230 this option can offer better throughput for reading target memory.\n\
231 Unfortunately, gdb does not currently know anything about volatile\n\
232 registers and thus data caching will produce incorrect results with\n\
233 volatile registers are in use. By default, this option is off.",