keep Windows GDB, believe it or not
[deliverable/binutils-gdb.git] / gdb / dcache.c
CommitLineData
755892d6
RP
1/* Caching code. Typically used by remote back ends for
2 caching remote memory.
3
4 Copyright 1992, 1993 Free Software Foundation, Inc.
5
6This file is part of GDB.
7
8This program is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 2 of the License, or
11(at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
20Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
21
22#include "defs.h"
23#include "dcache.h"
d538b510 24#include "gdbcmd.h"
755892d6 25
fed9a8d4 26#if defined(__STDC__) && defined(__linux__)
677653a0
MM
27/* In case the system header files define a prototype for insque and
28 remque that uses a pointer to a struct qelem, silence the warnings */
29struct qelem;
30#define insque(a,b) (insque)((struct qelem *)(a), (struct qelem *)(b))
31#define remque(a) (remque)((struct qelem *)(a))
a978b966
MM
32#else
33
34extern int insque();
35extern int remque();
677653a0
MM
36#endif
37
d538b510
RP
38int remote_dcache = 0;
39
755892d6
RP
40/* The data cache records all the data read from the remote machine
41 since the last time it stopped.
42
9e58280a
RP
43 Each cache block holds LINE_SIZE bytes of data
44 starting at a multiple-of-LINE_SIZE address. */
755892d6
RP
45
46#define LINE_SIZE_MASK ((LINE_SIZE - 1)) /* eg 7*2+1= 111*/
47#define XFORM(x) (((x) & LINE_SIZE_MASK) >> 2)
48
49/* Free all the data cache blocks, thus discarding all cached data. */
50void
51dcache_flush (dcache)
52 DCACHE *dcache;
53{
54 register struct dcache_block *db;
55
d538b510
RP
56 if (remote_dcache > 0)
57 while ((db = dcache->dcache_valid.next) != &dcache->dcache_valid)
58 {
59 remque (db);
60 insque (db, &dcache->dcache_free);
61 }
62
63 return;
755892d6
RP
64}
65
66/*
67 * If addr is present in the dcache, return the address of the block
68 * containing it.
69 */
d538b510 70static
755892d6
RP
71struct dcache_block *
72dcache_hit (dcache, addr)
73 DCACHE *dcache;
74 unsigned int addr;
75{
76 register struct dcache_block *db;
77
d538b510
RP
78 if (addr & 3
79 || remote_dcache == 0)
755892d6
RP
80 abort ();
81
82 /* Search all cache blocks for one that is at this address. */
83 db = dcache->dcache_valid.next;
84 while (db != &dcache->dcache_valid)
85 {
86 if ((addr & ~LINE_SIZE_MASK) == db->addr)
87 return db;
88 db = db->next;
89 }
d538b510 90
755892d6
RP
91 return NULL;
92}
93
94/* Return the int data at address ADDR in dcache block DC. */
d538b510 95static
755892d6
RP
96int
97dcache_value (db, addr)
98 struct dcache_block *db;
99 unsigned int addr;
100{
d538b510
RP
101 if (addr & 3
102 || remote_dcache == 0)
755892d6
RP
103 abort ();
104 return (db->data[XFORM (addr)]);
105}
106
107/* Get a free cache block, put or keep it on the valid list,
108 and return its address. The caller should store into the block
109 the address and data that it describes, then remque it from the
110 free list and insert it into the valid list. This procedure
9e58280a
RP
111 prevents errors from creeping in if a memory retrieval is
112 interrupted (which used to put garbage blocks in the valid
113 list...). */
d538b510 114static
755892d6
RP
115struct dcache_block *
116dcache_alloc (dcache)
117 DCACHE *dcache;
118{
119 register struct dcache_block *db;
120
d538b510
RP
121 if (remote_dcache == 0)
122 abort();
123
755892d6
RP
124 if ((db = dcache->dcache_free.next) == &dcache->dcache_free)
125 {
126 /* If we can't get one from the free list, take last valid and put
127 it on the free list. */
128 db = dcache->dcache_valid.last;
129 remque (db);
130 insque (db, &dcache->dcache_free);
131 }
132
133 remque (db);
134 insque (db, &dcache->dcache_valid);
135 return (db);
136}
137
d538b510
RP
138/* Using the data cache DCACHE return the contents of the word at
139 address ADDR in the remote machine. */
755892d6
RP
140int
141dcache_fetch (dcache, addr)
142 DCACHE *dcache;
143 CORE_ADDR addr;
144{
145 register struct dcache_block *db;
146
d538b510
RP
147 if (remote_dcache == 0)
148 {
149 int i;
150
151 (*dcache->read_memory) (addr, (unsigned char *) &i, 4);
152 return(i);
153 }
154
755892d6
RP
155 db = dcache_hit (dcache, addr);
156 if (db == 0)
157 {
158 db = dcache_alloc (dcache);
159 immediate_quit++;
160 (*dcache->read_memory) (addr & ~LINE_SIZE_MASK, (unsigned char *) db->data, LINE_SIZE);
161 immediate_quit--;
162 db->addr = addr & ~LINE_SIZE_MASK;
163 remque (db); /* Off the free list */
164 insque (db, &dcache->dcache_valid); /* On the valid list */
165 }
166 return (dcache_value (db, addr));
167}
168
169/* Write the word at ADDR both in the data cache and in the remote machine. */
170void
171dcache_poke (dcache, addr, data)
172 DCACHE *dcache;
173 CORE_ADDR addr;
174 int data;
175{
176 register struct dcache_block *db;
177
d538b510
RP
178 if (remote_dcache == 0)
179 {
180 (*dcache->write_memory) (addr, (unsigned char *) &data, 4);
181 return;
182 }
183
755892d6
RP
184 /* First make sure the word is IN the cache. DB is its cache block. */
185 db = dcache_hit (dcache, addr);
186 if (db == 0)
187 {
188 db = dcache_alloc (dcache);
189 immediate_quit++;
190 (*dcache->write_memory) (addr & ~LINE_SIZE_MASK, (unsigned char *) db->data, LINE_SIZE);
191 immediate_quit--;
192 db->addr = addr & ~LINE_SIZE_MASK;
d538b510
RP
193 remque (db); /* Off the free list */
194 insque (db, &dcache->dcache_valid); /* On the valid list */
755892d6
RP
195 }
196
197 /* Modify the word in the cache. */
198 db->data[XFORM (addr)] = data;
199
200 /* Send the changed word. */
201 immediate_quit++;
202 (*dcache->write_memory) (addr, (unsigned char *) &data, 4);
203 immediate_quit--;
204}
205
206/* Initialize the data cache. */
207DCACHE *
208dcache_init (reading, writing)
209 memxferfunc reading;
210 memxferfunc writing;
211{
212 register i;
213 register struct dcache_block *db;
214 DCACHE *dcache;
215
ac7a377f 216 dcache = (DCACHE *) xmalloc (sizeof (*dcache));
755892d6
RP
217 dcache->read_memory = reading;
218 dcache->write_memory = writing;
ac7a377f
JK
219 dcache->the_cache = (struct dcache_block *)
220 xmalloc (sizeof (*dcache->the_cache) * DCACHE_SIZE);
755892d6
RP
221
222 dcache->dcache_free.next = dcache->dcache_free.last = &dcache->dcache_free;
223 dcache->dcache_valid.next = dcache->dcache_valid.last = &dcache->dcache_valid;
224 for (db = dcache->the_cache, i = 0; i < DCACHE_SIZE; i++, db++)
225 insque (db, &dcache->dcache_free);
226
227 return(dcache);
228}
229
d538b510
RP
230void
231_initialitize_dcache ()
232{
233 add_show_from_set
234 (add_set_cmd ("remotecache", class_support, var_boolean,
235 (char *) &remote_dcache,
236 "\
237Set cache use for remote targets.\n\
238When on, use data caching for remote targets. For many remote targets\n\
239this option can offer better throughput for reading target memory.\n\
240Unfortunately, gdb does not currently know anything about volatile\n\
241registers and thus data caching will produce incorrect results with\n\
242volatile registers are in use. By default, this option is off.",
243 &setlist),
244 &showlist);
245}
This page took 0.104797 seconds and 4 git commands to generate.