]>
Commit | Line | Data |
---|---|---|
755892d6 RP |
1 | /* Caching code. Typically used by remote back ends for |
2 | caching remote memory. | |
3 | ||
45993f61 | 4 | Copyright 1992, 1993, 1995 Free Software Foundation, Inc. |
755892d6 | 5 | |
45993f61 | 6 | This file is part of GDB. |
755892d6 | 7 | |
45993f61 SC |
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. | |
755892d6 | 12 | |
45993f61 SC |
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. | |
755892d6 | 17 | |
45993f61 SC |
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 | |
6c9638b4 | 20 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
755892d6 RP |
21 | |
22 | #include "defs.h" | |
23 | #include "dcache.h" | |
d538b510 | 24 | #include "gdbcmd.h" |
2b576293 | 25 | #include "gdb_string.h" |
755892d6 | 26 | |
92c6bf4d | 27 | |
45993f61 SC |
28 | /* |
29 | The data cache could lead to incorrect results because it doesn't know | |
30 | about volatile variables, thus making it impossible to debug | |
31 | functions which use memory mapped I/O devices. | |
677653a0 | 32 | |
45993f61 | 33 | set remotecache 0 |
755892d6 | 34 | |
45993f61 SC |
35 | In those cases. |
36 | ||
37 | In general the dcache speeds up performance, some speed improvement | |
38 | comes from the actual caching mechanism, but the major gain is in | |
39 | the reduction of the remote protocol overhead; instead of reading | |
40 | or writing a large area of memory in 4 byte requests, the cache | |
41 | bundles up the requests into 32 byte (actually LINE_SIZE) chunks. | |
42 | Reducing the overhead to an eighth of what it was. This is very | |
43 | obvious when displaying a large amount of data, | |
44 | ||
45 | eg, x/200x 0 | |
46 | ||
47 | caching | no yes | |
48 | ---------------------------- | |
49 | first time | 4 sec 2 sec improvement due to chunking | |
50 | second time | 4 sec 0 sec improvement due to caching | |
51 | ||
52 | The cache structure is unusual, we keep a number of cache blocks | |
53 | (DCACHE_SIZE) and each one caches a LINE_SIZEed area of memory. | |
54 | Within each line we remember the address of the line (always a | |
55 | multiple of the LINE_SIZE) and a vector of bytes over the range. | |
56 | There's another vector which contains the state of the bytes. | |
57 | ||
58 | ENTRY_BAD means that the byte is just plain wrong, and has no | |
59 | correspondence with anything else (as it would when the cache is | |
60 | turned on, but nothing has been done to it. | |
61 | ||
62 | ENTRY_DIRTY means that the byte has some data in it which should be | |
63 | written out to the remote target one day, but contains correct | |
64 | data. ENTRY_OK means that the data is the same in the cache as it | |
65 | is in remote memory. | |
66 | ||
67 | ||
68 | The ENTRY_DIRTY state is necessary because GDB likes to write large | |
69 | lumps of memory in small bits. If the caching mechanism didn't | |
70 | maintain the DIRTY information, then something like a two byte | |
71 | write would mean that the entire cache line would have to be read, | |
72 | the two bytes modified and then written out again. The alternative | |
73 | would be to not read in the cache line in the first place, and just | |
74 | write the two bytes directly into target memory. The trouble with | |
75 | that is that it really nails performance, because of the remote | |
76 | protocol overhead. This way, all those little writes are bundled | |
77 | up into an entire cache line write in one go, without having to | |
78 | read the cache line in the first place. | |
79 | ||
80 | ||
81 | */ | |
82 | ||
83 | ||
84 | /* This value regulates the number of cache blocks stored. | |
85 | Smaller values reduce the time spent searching for a cache | |
86 | line, and reduce memory requirements, but increase the risk | |
87 | of a line not being in memory */ | |
88 | ||
89 | #define DCACHE_SIZE 64 | |
90 | ||
91 | /* This value regulates the size of a cache line. Smaller values | |
92 | reduce the time taken to read a single byte, but reduce overall | |
93 | throughput. */ | |
94 | ||
95 | #define LINE_SIZE_POWER (5) | |
96 | #define LINE_SIZE (1 << LINE_SIZE_POWER) | |
97 | ||
98 | /* Each cache block holds LINE_SIZE bytes of data | |
9e58280a | 99 | starting at a multiple-of-LINE_SIZE address. */ |
755892d6 | 100 | |
45993f61 SC |
101 | #define LINE_SIZE_MASK ((LINE_SIZE - 1)) |
102 | #define XFORM(x) ((x) & LINE_SIZE_MASK) | |
103 | #define MASK(x) ((x) & ~LINE_SIZE_MASK) | |
104 | ||
105 | ||
106 | #define ENTRY_BAD 0 /* data at this byte is wrong */ | |
107 | #define ENTRY_DIRTY 1 /* data at this byte needs to be written back */ | |
108 | #define ENTRY_OK 2 /* data at this byte is same as in memory */ | |
109 | ||
110 | ||
111 | struct dcache_block | |
112 | { | |
113 | struct dcache_block *p; /* next in list */ | |
114 | unsigned int addr; /* Address for which data is recorded. */ | |
6b14af2b | 115 | char data[LINE_SIZE]; /* bytes at given address */ |
45993f61 SC |
116 | unsigned char state[LINE_SIZE]; /* what state the data is in */ |
117 | ||
118 | /* whether anything in state is dirty - used to speed up the | |
119 | dirty scan. */ | |
120 | int anydirty; | |
121 | ||
122 | int refs; | |
123 | }; | |
124 | ||
125 | ||
126 | struct dcache_struct | |
127 | { | |
128 | /* Function to actually read the target memory. */ | |
129 | memxferfunc read_memory; | |
130 | ||
131 | /* Function to actually write the target memory */ | |
132 | memxferfunc write_memory; | |
133 | ||
134 | /* free list */ | |
135 | struct dcache_block *free_head; | |
136 | struct dcache_block *free_tail; | |
137 | ||
138 | /* in use list */ | |
139 | struct dcache_block *valid_head; | |
140 | struct dcache_block *valid_tail; | |
141 | ||
142 | /* The cache itself. */ | |
143 | struct dcache_block *the_cache; | |
144 | ||
145 | /* potentially, if the cache was enabled, and then turned off, and | |
146 | then turned on again, the stuff in it could be stale, so this is | |
147 | used to mark it */ | |
148 | int cache_has_stuff; | |
149 | } ; | |
150 | ||
4930f0a7 | 151 | int remote_dcache = 0; |
45993f61 SC |
152 | |
153 | DCACHE *last_cache; /* Used by info dcache */ | |
154 | ||
155 | ||
755892d6 RP |
156 | |
157 | /* Free all the data cache blocks, thus discarding all cached data. */ | |
45993f61 | 158 | |
755892d6 RP |
159 | void |
160 | dcache_flush (dcache) | |
161 | DCACHE *dcache; | |
162 | { | |
45993f61 SC |
163 | int i; |
164 | dcache->valid_head = 0; | |
165 | dcache->valid_tail = 0; | |
166 | ||
167 | dcache->free_head = 0; | |
168 | dcache->free_tail = 0; | |
169 | ||
170 | for (i = 0; i < DCACHE_SIZE; i++) | |
171 | { | |
172 | struct dcache_block *db = dcache->the_cache + i; | |
173 | ||
174 | if (!dcache->free_head) | |
175 | dcache->free_head = db; | |
176 | else | |
177 | dcache->free_tail->p = db; | |
178 | dcache->free_tail = db; | |
179 | db->p = 0; | |
180 | } | |
755892d6 | 181 | |
45993f61 | 182 | dcache->cache_has_stuff = 0; |
d538b510 RP |
183 | |
184 | return; | |
755892d6 RP |
185 | } |
186 | ||
45993f61 SC |
187 | /* If addr is present in the dcache, return the address of the block |
188 | containing it. */ | |
d538b510 | 189 | static |
755892d6 RP |
190 | struct dcache_block * |
191 | dcache_hit (dcache, addr) | |
192 | DCACHE *dcache; | |
193 | unsigned int addr; | |
194 | { | |
195 | register struct dcache_block *db; | |
196 | ||
755892d6 | 197 | /* Search all cache blocks for one that is at this address. */ |
45993f61 SC |
198 | db = dcache->valid_head; |
199 | ||
200 | while (db) | |
755892d6 | 201 | { |
45993f61 SC |
202 | if (MASK(addr) == db->addr) |
203 | { | |
204 | db->refs++; | |
205 | return db; | |
206 | } | |
207 | db = db->p; | |
755892d6 | 208 | } |
d538b510 | 209 | |
755892d6 RP |
210 | return NULL; |
211 | } | |
212 | ||
45993f61 SC |
213 | /* Make sure that anything in this line which needs to |
214 | be written is. */ | |
215 | ||
216 | static int | |
217 | dcache_write_line (dcache, db) | |
218 | DCACHE *dcache; | |
219 | register struct dcache_block *db; | |
755892d6 | 220 | { |
45993f61 SC |
221 | int s; |
222 | int e; | |
223 | s = 0; | |
224 | if (db->anydirty) | |
225 | { | |
226 | for (s = 0; s < LINE_SIZE; s++) | |
227 | { | |
228 | if (db->state[s] == ENTRY_DIRTY) | |
229 | { | |
230 | int len = 0; | |
231 | for (e = s ; e < LINE_SIZE; e++, len++) | |
232 | if (db->state[e] != ENTRY_DIRTY) | |
69c626a9 SC |
233 | break; |
234 | { | |
235 | /* all bytes from s..s+len-1 need to | |
236 | be written out */ | |
237 | int done = 0; | |
238 | while (done < len) { | |
239 | int t = dcache->write_memory (db->addr + s + done, | |
240 | db->data + s + done, | |
241 | len - done); | |
242 | if (t == 0) | |
243 | return 0; | |
244 | done += t; | |
245 | } | |
246 | memset (db->state + s, ENTRY_OK, len); | |
247 | s = e; | |
248 | } | |
45993f61 SC |
249 | } |
250 | } | |
251 | db->anydirty = 0; | |
252 | } | |
253 | return 1; | |
755892d6 RP |
254 | } |
255 | ||
45993f61 | 256 | |
755892d6 RP |
257 | /* Get a free cache block, put or keep it on the valid list, |
258 | and return its address. The caller should store into the block | |
259 | the address and data that it describes, then remque it from the | |
260 | free list and insert it into the valid list. This procedure | |
9e58280a RP |
261 | prevents errors from creeping in if a memory retrieval is |
262 | interrupted (which used to put garbage blocks in the valid | |
263 | list...). */ | |
d538b510 | 264 | static |
755892d6 RP |
265 | struct dcache_block * |
266 | dcache_alloc (dcache) | |
267 | DCACHE *dcache; | |
268 | { | |
269 | register struct dcache_block *db; | |
270 | ||
d538b510 | 271 | if (remote_dcache == 0) |
45993f61 | 272 | abort (); |
d538b510 | 273 | |
45993f61 | 274 | /* Take something from the free list */ |
6b14af2b FF |
275 | db = dcache->free_head; |
276 | if (db) | |
755892d6 | 277 | { |
45993f61 SC |
278 | dcache->free_head = db->p; |
279 | } | |
6b14af2b | 280 | else |
45993f61 | 281 | { |
6b14af2b | 282 | /* Nothing left on free list, so grab one from the valid list */ |
45993f61 SC |
283 | db = dcache->valid_head; |
284 | dcache->valid_head = db->p; | |
285 | ||
286 | dcache_write_line (dcache, db); | |
755892d6 RP |
287 | } |
288 | ||
45993f61 SC |
289 | /* append this line to end of valid list */ |
290 | if (!dcache->valid_head) | |
291 | dcache->valid_head = db; | |
292 | else | |
293 | dcache->valid_tail->p = db; | |
294 | dcache->valid_tail = db; | |
295 | db->p = 0; | |
296 | ||
297 | return db; | |
755892d6 RP |
298 | } |
299 | ||
45993f61 SC |
300 | /* Using the data cache DCACHE return the contents of the byte at |
301 | address ADDR in the remote machine. | |
302 | ||
303 | Returns 0 on error. */ | |
304 | ||
755892d6 | 305 | int |
45993f61 | 306 | dcache_peek_byte (dcache, addr, ptr) |
755892d6 RP |
307 | DCACHE *dcache; |
308 | CORE_ADDR addr; | |
6b14af2b | 309 | char *ptr; |
755892d6 | 310 | { |
45993f61 SC |
311 | register struct dcache_block *db = dcache_hit (dcache, addr); |
312 | int ok=1; | |
313 | int done = 0; | |
314 | if (db == 0 | |
315 | || db->state[XFORM (addr)] == ENTRY_BAD) | |
755892d6 | 316 | { |
45993f61 SC |
317 | if (db) |
318 | { | |
319 | dcache_write_line (dcache, db); | |
320 | } | |
321 | else | |
755892d6 RP |
322 | db = dcache_alloc (dcache); |
323 | immediate_quit++; | |
45993f61 SC |
324 | db->addr = MASK (addr); |
325 | while (done < LINE_SIZE) | |
326 | { | |
327 | int try = | |
328 | (*dcache->read_memory) | |
329 | (db->addr + done, | |
6b14af2b | 330 | db->data + done, |
45993f61 SC |
331 | LINE_SIZE - done); |
332 | if (try == 0) | |
333 | return 0; | |
334 | done += try; | |
335 | } | |
755892d6 | 336 | immediate_quit--; |
45993f61 SC |
337 | |
338 | memset (db->state, ENTRY_OK, sizeof (db->data)); | |
339 | db->anydirty = 0; | |
755892d6 | 340 | } |
45993f61 SC |
341 | *ptr = db->data[XFORM (addr)]; |
342 | return ok; | |
755892d6 RP |
343 | } |
344 | ||
45993f61 SC |
345 | /* Using the data cache DCACHE return the contents of the word at |
346 | address ADDR in the remote machine. | |
347 | ||
348 | Returns 0 on error. */ | |
349 | ||
350 | int | |
351 | dcache_peek (dcache, addr, data) | |
755892d6 RP |
352 | DCACHE *dcache; |
353 | CORE_ADDR addr; | |
45993f61 | 354 | int *data; |
755892d6 | 355 | { |
6b14af2b | 356 | char *dp = (char *) data; |
45993f61 SC |
357 | int i; |
358 | for (i = 0; i < sizeof (int); i++) | |
359 | { | |
360 | if (!dcache_peek_byte (dcache, addr, dp + i)) | |
361 | return 0; | |
362 | } | |
363 | return 1; | |
364 | } | |
755892d6 | 365 | |
45993f61 SC |
366 | |
367 | /* Writeback any dirty lines to the remote. */ | |
368 | static int | |
369 | dcache_writeback (dcache) | |
370 | DCACHE *dcache; | |
371 | { | |
372 | struct dcache_block *db; | |
373 | ||
374 | db = dcache->valid_head; | |
375 | ||
376 | while (db) | |
d538b510 | 377 | { |
45993f61 SC |
378 | if (!dcache_write_line (dcache, db)) |
379 | return 0; | |
380 | db = db->p; | |
d538b510 | 381 | } |
45993f61 SC |
382 | return 1; |
383 | } | |
d538b510 | 384 | |
45993f61 SC |
385 | |
386 | /* Using the data cache DCACHE return the contents of the word at | |
387 | address ADDR in the remote machine. */ | |
388 | int | |
389 | dcache_fetch (dcache, addr) | |
390 | DCACHE *dcache; | |
391 | CORE_ADDR addr; | |
392 | { | |
393 | int res; | |
394 | dcache_peek (dcache, addr, &res); | |
395 | return res; | |
396 | } | |
397 | ||
398 | ||
399 | /* Write the byte at PTR into ADDR in the data cache. | |
400 | Return zero on write error. | |
401 | */ | |
402 | ||
403 | int | |
404 | dcache_poke_byte (dcache, addr, ptr) | |
405 | DCACHE *dcache; | |
406 | CORE_ADDR addr; | |
407 | char *ptr; | |
408 | { | |
409 | register struct dcache_block *db = dcache_hit (dcache, addr); | |
410 | ||
411 | if (!db) | |
755892d6 RP |
412 | { |
413 | db = dcache_alloc (dcache); | |
45993f61 SC |
414 | db->addr = MASK (addr); |
415 | memset (db->state, ENTRY_BAD, sizeof (db->data)); | |
755892d6 RP |
416 | } |
417 | ||
45993f61 SC |
418 | db->data[XFORM (addr)] = *ptr; |
419 | db->state[XFORM (addr)] = ENTRY_DIRTY; | |
420 | db->anydirty = 1; | |
421 | return 1; | |
422 | } | |
755892d6 | 423 | |
45993f61 SC |
424 | /* Write the word at ADDR both in the data cache and in the remote machine. |
425 | Return zero on write error. | |
426 | */ | |
427 | ||
428 | int | |
429 | dcache_poke (dcache, addr, data) | |
430 | DCACHE *dcache; | |
431 | CORE_ADDR addr; | |
432 | int data; | |
433 | { | |
6b14af2b | 434 | char *dp = (char *) (&data); |
45993f61 SC |
435 | int i; |
436 | for (i = 0; i < sizeof (int); i++) | |
437 | { | |
438 | if (!dcache_poke_byte (dcache, addr, dp + i)) | |
439 | return 0; | |
440 | } | |
441 | dcache_writeback (dcache); | |
442 | return 1; | |
755892d6 RP |
443 | } |
444 | ||
45993f61 | 445 | |
755892d6 RP |
446 | /* Initialize the data cache. */ |
447 | DCACHE * | |
448 | dcache_init (reading, writing) | |
449 | memxferfunc reading; | |
450 | memxferfunc writing; | |
451 | { | |
45993f61 | 452 | int csize = sizeof (struct dcache_block) * DCACHE_SIZE; |
755892d6 RP |
453 | DCACHE *dcache; |
454 | ||
ac7a377f | 455 | dcache = (DCACHE *) xmalloc (sizeof (*dcache)); |
755892d6 RP |
456 | dcache->read_memory = reading; |
457 | dcache->write_memory = writing; | |
755892d6 | 458 | |
45993f61 SC |
459 | dcache->the_cache = (struct dcache_block *) xmalloc (csize); |
460 | memset (dcache->the_cache, 0, csize); | |
461 | ||
462 | dcache_flush (dcache); | |
463 | ||
464 | last_cache = dcache; | |
465 | return dcache; | |
466 | } | |
467 | ||
468 | /* Read or write LEN bytes from inferior memory at MEMADDR, transferring | |
469 | to or from debugger address MYADDR. Write to inferior if SHOULD_WRITE is | |
470 | nonzero. | |
471 | ||
472 | Returns length of data written or read; 0 for error. | |
473 | ||
474 | This routine is indended to be called by remote_xfer_ functions. */ | |
475 | ||
476 | int | |
477 | dcache_xfer_memory (dcache, memaddr, myaddr, len, should_write) | |
478 | DCACHE *dcache; | |
479 | CORE_ADDR memaddr; | |
480 | char *myaddr; | |
481 | int len; | |
482 | int should_write; | |
483 | { | |
484 | int i; | |
485 | ||
486 | if (remote_dcache) | |
487 | { | |
488 | int (*xfunc) () | |
489 | = should_write ? dcache_poke_byte : dcache_peek_byte; | |
490 | ||
491 | for (i = 0; i < len; i++) | |
492 | { | |
493 | if (!xfunc (dcache, memaddr + i, myaddr + i)) | |
494 | return 0; | |
495 | } | |
496 | dcache->cache_has_stuff = 1; | |
497 | dcache_writeback (dcache); | |
498 | } | |
499 | else | |
500 | { | |
501 | int (*xfunc) () | |
502 | = should_write ? dcache->write_memory : dcache->read_memory; | |
503 | ||
504 | if (dcache->cache_has_stuff) | |
505 | dcache_flush (dcache); | |
755892d6 | 506 | |
45993f61 SC |
507 | len = xfunc (memaddr, myaddr, len); |
508 | } | |
509 | return len; | |
510 | } | |
511 | ||
512 | static void | |
513 | dcache_info (exp, tty) | |
514 | char *exp; | |
515 | int tty; | |
516 | { | |
517 | struct dcache_block *p; | |
518 | ||
519 | if (!remote_dcache) | |
520 | { | |
521 | printf_filtered ("Dcache not enabled\n"); | |
522 | return; | |
523 | } | |
524 | printf_filtered ("Dcache enabled, line width %d, depth %d\n", | |
525 | LINE_SIZE, DCACHE_SIZE); | |
526 | ||
527 | printf_filtered ("Cache state:\n"); | |
528 | ||
529 | for (p = last_cache->valid_head; p; p = p->p) | |
530 | { | |
531 | int j; | |
532 | printf_filtered ("Line at %08xd, referenced %d times\n", | |
533 | p->addr, p->refs); | |
534 | ||
535 | for (j = 0; j < LINE_SIZE; j++) | |
6b14af2b | 536 | printf_filtered ("%02x", p->data[j] & 0xFF); |
45993f61 SC |
537 | printf_filtered ("\n"); |
538 | ||
539 | for (j = 0; j < LINE_SIZE; j++) | |
6b14af2b | 540 | printf_filtered (" %2x", p->state[j]); |
45993f61 SC |
541 | printf_filtered ("\n"); |
542 | } | |
755892d6 RP |
543 | } |
544 | ||
d538b510 | 545 | void |
45993f61 | 546 | _initialize_dcache () |
d538b510 RP |
547 | { |
548 | add_show_from_set | |
549 | (add_set_cmd ("remotecache", class_support, var_boolean, | |
550 | (char *) &remote_dcache, | |
551 | "\ | |
552 | Set cache use for remote targets.\n\ | |
553 | When on, use data caching for remote targets. For many remote targets\n\ | |
554 | this option can offer better throughput for reading target memory.\n\ | |
555 | Unfortunately, gdb does not currently know anything about volatile\n\ | |
556 | registers and thus data caching will produce incorrect results with\n\ | |
45993f61 | 557 | volatile registers are in use. By default, this option is on.", |
d538b510 RP |
558 | &setlist), |
559 | &showlist); | |
45993f61 SC |
560 | |
561 | add_info ("dcache", dcache_info, | |
562 | "Print information on the dcache performance."); | |
563 | ||
d538b510 | 564 | } |