* mips.h (M_ULD, M_ULD_A, M_USD, M_USD_A): Define.
[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
d538b510
RP
26int remote_dcache = 0;
27
755892d6
RP
28/* The data cache records all the data read from the remote machine
29 since the last time it stopped.
30
9e58280a
RP
31 Each cache block holds LINE_SIZE bytes of data
32 starting at a multiple-of-LINE_SIZE address. */
755892d6
RP
33
34#define LINE_SIZE_MASK ((LINE_SIZE - 1)) /* eg 7*2+1= 111*/
35#define XFORM(x) (((x) & LINE_SIZE_MASK) >> 2)
36
37/* Free all the data cache blocks, thus discarding all cached data. */
38void
39dcache_flush (dcache)
40 DCACHE *dcache;
41{
42 register struct dcache_block *db;
43
d538b510
RP
44 if (remote_dcache > 0)
45 while ((db = dcache->dcache_valid.next) != &dcache->dcache_valid)
46 {
47 remque (db);
48 insque (db, &dcache->dcache_free);
49 }
50
51 return;
755892d6
RP
52}
53
54/*
55 * If addr is present in the dcache, return the address of the block
56 * containing it.
57 */
d538b510 58static
755892d6
RP
59struct dcache_block *
60dcache_hit (dcache, addr)
61 DCACHE *dcache;
62 unsigned int addr;
63{
64 register struct dcache_block *db;
65
d538b510
RP
66 if (addr & 3
67 || remote_dcache == 0)
755892d6
RP
68 abort ();
69
70 /* Search all cache blocks for one that is at this address. */
71 db = dcache->dcache_valid.next;
72 while (db != &dcache->dcache_valid)
73 {
74 if ((addr & ~LINE_SIZE_MASK) == db->addr)
75 return db;
76 db = db->next;
77 }
d538b510 78
755892d6
RP
79 return NULL;
80}
81
82/* Return the int data at address ADDR in dcache block DC. */
d538b510 83static
755892d6
RP
84int
85dcache_value (db, addr)
86 struct dcache_block *db;
87 unsigned int addr;
88{
d538b510
RP
89 if (addr & 3
90 || remote_dcache == 0)
755892d6
RP
91 abort ();
92 return (db->data[XFORM (addr)]);
93}
94
95/* Get a free cache block, put or keep it on the valid list,
96 and return its address. The caller should store into the block
97 the address and data that it describes, then remque it from the
98 free list and insert it into the valid list. This procedure
9e58280a
RP
99 prevents errors from creeping in if a memory retrieval is
100 interrupted (which used to put garbage blocks in the valid
101 list...). */
d538b510 102static
755892d6
RP
103struct dcache_block *
104dcache_alloc (dcache)
105 DCACHE *dcache;
106{
107 register struct dcache_block *db;
108
d538b510
RP
109 if (remote_dcache == 0)
110 abort();
111
755892d6
RP
112 if ((db = dcache->dcache_free.next) == &dcache->dcache_free)
113 {
114 /* If we can't get one from the free list, take last valid and put
115 it on the free list. */
116 db = dcache->dcache_valid.last;
117 remque (db);
118 insque (db, &dcache->dcache_free);
119 }
120
121 remque (db);
122 insque (db, &dcache->dcache_valid);
123 return (db);
124}
125
d538b510
RP
126/* Using the data cache DCACHE return the contents of the word at
127 address ADDR in the remote machine. */
755892d6
RP
128int
129dcache_fetch (dcache, addr)
130 DCACHE *dcache;
131 CORE_ADDR addr;
132{
133 register struct dcache_block *db;
134
d538b510
RP
135 if (remote_dcache == 0)
136 {
137 int i;
138
139 (*dcache->read_memory) (addr, (unsigned char *) &i, 4);
140 return(i);
141 }
142
755892d6
RP
143 db = dcache_hit (dcache, addr);
144 if (db == 0)
145 {
146 db = dcache_alloc (dcache);
147 immediate_quit++;
148 (*dcache->read_memory) (addr & ~LINE_SIZE_MASK, (unsigned char *) db->data, LINE_SIZE);
149 immediate_quit--;
150 db->addr = addr & ~LINE_SIZE_MASK;
151 remque (db); /* Off the free list */
152 insque (db, &dcache->dcache_valid); /* On the valid list */
153 }
154 return (dcache_value (db, addr));
155}
156
157/* Write the word at ADDR both in the data cache and in the remote machine. */
158void
159dcache_poke (dcache, addr, data)
160 DCACHE *dcache;
161 CORE_ADDR addr;
162 int data;
163{
164 register struct dcache_block *db;
165
d538b510
RP
166 if (remote_dcache == 0)
167 {
168 (*dcache->write_memory) (addr, (unsigned char *) &data, 4);
169 return;
170 }
171
755892d6
RP
172 /* First make sure the word is IN the cache. DB is its cache block. */
173 db = dcache_hit (dcache, addr);
174 if (db == 0)
175 {
176 db = dcache_alloc (dcache);
177 immediate_quit++;
178 (*dcache->write_memory) (addr & ~LINE_SIZE_MASK, (unsigned char *) db->data, LINE_SIZE);
179 immediate_quit--;
180 db->addr = addr & ~LINE_SIZE_MASK;
d538b510
RP
181 remque (db); /* Off the free list */
182 insque (db, &dcache->dcache_valid); /* On the valid list */
755892d6
RP
183 }
184
185 /* Modify the word in the cache. */
186 db->data[XFORM (addr)] = data;
187
188 /* Send the changed word. */
189 immediate_quit++;
190 (*dcache->write_memory) (addr, (unsigned char *) &data, 4);
191 immediate_quit--;
192}
193
194/* Initialize the data cache. */
195DCACHE *
196dcache_init (reading, writing)
197 memxferfunc reading;
198 memxferfunc writing;
199{
200 register i;
201 register struct dcache_block *db;
202 DCACHE *dcache;
203
ac7a377f 204 dcache = (DCACHE *) xmalloc (sizeof (*dcache));
755892d6
RP
205 dcache->read_memory = reading;
206 dcache->write_memory = writing;
ac7a377f
JK
207 dcache->the_cache = (struct dcache_block *)
208 xmalloc (sizeof (*dcache->the_cache) * DCACHE_SIZE);
755892d6
RP
209
210 dcache->dcache_free.next = dcache->dcache_free.last = &dcache->dcache_free;
211 dcache->dcache_valid.next = dcache->dcache_valid.last = &dcache->dcache_valid;
212 for (db = dcache->the_cache, i = 0; i < DCACHE_SIZE; i++, db++)
213 insque (db, &dcache->dcache_free);
214
215 return(dcache);
216}
217
d538b510
RP
218void
219_initialitize_dcache ()
220{
221 add_show_from_set
222 (add_set_cmd ("remotecache", class_support, var_boolean,
223 (char *) &remote_dcache,
224 "\
225Set cache use for remote targets.\n\
226When on, use data caching for remote targets. For many remote targets\n\
227this option can offer better throughput for reading target memory.\n\
228Unfortunately, gdb does not currently know anything about volatile\n\
229registers and thus data caching will produce incorrect results with\n\
230volatile registers are in use. By default, this option is off.",
231 &setlist),
232 &showlist);
233}
This page took 0.091485 seconds and 4 git commands to generate.