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. */
26 int remote_dcache = 0;
28 /* In case the system header files define a prototype for insque and
29 remque that uses a pointer to a struct qelem, silence the warnings */
30 #define Insque(a,b) insque((PTR)(a), (PTR)(b))
31 #define Remque(a) remque((PTR)(a))
33 /* The data cache records all the data read from the remote machine
34 since the last time it stopped.
36 Each cache block holds LINE_SIZE bytes of data
37 starting at a multiple-of-LINE_SIZE address. */
39 #define LINE_SIZE_MASK ((LINE_SIZE - 1)) /* eg 7*2+1= 111*/
40 #define XFORM(x) (((x) & LINE_SIZE_MASK) >> 2)
42 /* Free all the data cache blocks, thus discarding all cached data. */
47 register struct dcache_block *db;
49 if (remote_dcache > 0)
50 while ((db = dcache->dcache_valid.next) != &dcache->dcache_valid)
53 Insque (db, &dcache->dcache_free);
60 * If addr is present in the dcache, return the address of the block
65 dcache_hit (dcache, addr)
69 register struct dcache_block *db;
72 || remote_dcache == 0)
75 /* Search all cache blocks for one that is at this address. */
76 db = dcache->dcache_valid.next;
77 while (db != &dcache->dcache_valid)
79 if ((addr & ~LINE_SIZE_MASK) == db->addr)
87 /* Return the int data at address ADDR in dcache block DC. */
90 dcache_value (db, addr)
91 struct dcache_block *db;
95 || remote_dcache == 0)
97 return (db->data[XFORM (addr)]);
100 /* Get a free cache block, put or keep it on the valid list,
101 and return its address. The caller should store into the block
102 the address and data that it describes, then remque it from the
103 free list and insert it into the valid list. This procedure
104 prevents errors from creeping in if a memory retrieval is
105 interrupted (which used to put garbage blocks in the valid
108 struct dcache_block *
109 dcache_alloc (dcache)
112 register struct dcache_block *db;
114 if (remote_dcache == 0)
117 if ((db = dcache->dcache_free.next) == &dcache->dcache_free)
119 /* If we can't get one from the free list, take last valid and put
120 it on the free list. */
121 db = dcache->dcache_valid.last;
123 Insque (db, &dcache->dcache_free);
127 Insque (db, &dcache->dcache_valid);
131 /* Using the data cache DCACHE return the contents of the word at
132 address ADDR in the remote machine. */
134 dcache_fetch (dcache, addr)
138 register struct dcache_block *db;
140 if (remote_dcache == 0)
144 (*dcache->read_memory) (addr, (unsigned char *) &i, 4);
148 db = dcache_hit (dcache, addr);
151 db = dcache_alloc (dcache);
153 (*dcache->read_memory) (addr & ~LINE_SIZE_MASK, (unsigned char *) db->data, LINE_SIZE);
155 db->addr = addr & ~LINE_SIZE_MASK;
156 Remque (db); /* Off the free list */
157 Insque (db, &dcache->dcache_valid); /* On the valid list */
159 return (dcache_value (db, addr));
162 /* Write the word at ADDR both in the data cache and in the remote machine. */
164 dcache_poke (dcache, addr, data)
169 register struct dcache_block *db;
171 if (remote_dcache == 0)
173 (*dcache->write_memory) (addr, (unsigned char *) &data, 4);
177 /* First make sure the word is IN the cache. DB is its cache block. */
178 db = dcache_hit (dcache, addr);
181 db = dcache_alloc (dcache);
183 (*dcache->write_memory) (addr & ~LINE_SIZE_MASK, (unsigned char *) db->data, LINE_SIZE);
185 db->addr = addr & ~LINE_SIZE_MASK;
186 Remque (db); /* Off the free list */
187 Insque (db, &dcache->dcache_valid); /* On the valid list */
190 /* Modify the word in the cache. */
191 db->data[XFORM (addr)] = data;
193 /* Send the changed word. */
195 (*dcache->write_memory) (addr, (unsigned char *) &data, 4);
199 /* Initialize the data cache. */
201 dcache_init (reading, writing)
206 register struct dcache_block *db;
209 dcache = (DCACHE *) xmalloc (sizeof (*dcache));
210 dcache->read_memory = reading;
211 dcache->write_memory = writing;
212 dcache->the_cache = (struct dcache_block *)
213 xmalloc (sizeof (*dcache->the_cache) * DCACHE_SIZE);
215 dcache->dcache_free.next = dcache->dcache_free.last = &dcache->dcache_free;
216 dcache->dcache_valid.next = dcache->dcache_valid.last = &dcache->dcache_valid;
217 for (db = dcache->the_cache, i = 0; i < DCACHE_SIZE; i++, db++)
218 Insque (db, &dcache->dcache_free);
224 _initialitize_dcache ()
227 (add_set_cmd ("remotecache", class_support, var_boolean,
228 (char *) &remote_dcache,
230 Set cache use for remote targets.\n\
231 When on, use data caching for remote targets. For many remote targets\n\
232 this option can offer better throughput for reading target memory.\n\
233 Unfortunately, gdb does not currently know anything about volatile\n\
234 registers and thus data caching will produce incorrect results with\n\
235 volatile registers are in use. By default, this option is off.",