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