Non-stop mode support.
[deliverable/binutils-gdb.git] / gdb / dcache.c
CommitLineData
69517000
AC
1/* Caching code for GDB, the GNU debugger.
2
9b254dd1 3 Copyright (C) 1992, 1993, 1995, 1996, 1998, 1999, 2000, 2001, 2003, 2007,
0fb0cc75 4 2008, 2009 Free Software Foundation, Inc.
c906108c
SS
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
a9762ec7 10 the Free Software Foundation; either version 3 of the License, or
c906108c
SS
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
a9762ec7 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
20
21#include "defs.h"
22#include "dcache.h"
23#include "gdbcmd.h"
24#include "gdb_string.h"
25#include "gdbcore.h"
4930751a 26#include "target.h"
c906108c 27
29e57380
C
28/* The data cache could lead to incorrect results because it doesn't
29 know about volatile variables, thus making it impossible to debug
30 functions which use memory mapped I/O devices. Set the nocache
31 memory region attribute in those cases.
c906108c
SS
32
33 In general the dcache speeds up performance, some speed improvement
34 comes from the actual caching mechanism, but the major gain is in
35 the reduction of the remote protocol overhead; instead of reading
36 or writing a large area of memory in 4 byte requests, the cache
37 bundles up the requests into 32 byte (actually LINE_SIZE) chunks.
38 Reducing the overhead to an eighth of what it was. This is very
39 obvious when displaying a large amount of data,
40
41 eg, x/200x 0
42
43 caching | no yes
44 ----------------------------
45 first time | 4 sec 2 sec improvement due to chunking
46 second time | 4 sec 0 sec improvement due to caching
47
48 The cache structure is unusual, we keep a number of cache blocks
49 (DCACHE_SIZE) and each one caches a LINE_SIZEed area of memory.
50 Within each line we remember the address of the line (always a
51 multiple of the LINE_SIZE) and a vector of bytes over the range.
52 There's another vector which contains the state of the bytes.
53
07128da0 54 ENTRY_INVALID means that the byte is just plain wrong, and has no
c906108c 55 correspondence with anything else (as it would when the cache is
07128da0 56 turned on, but nothing has been done to it).
c906108c
SS
57
58 ENTRY_DIRTY means that the byte has some data in it which should be
59 written out to the remote target one day, but contains correct
29e57380
C
60 data.
61
07128da0 62 ENTRY_VALID means that the data is the same in the cache as it is in
29e57380 63 remote memory.
c906108c
SS
64
65
66 The ENTRY_DIRTY state is necessary because GDB likes to write large
67 lumps of memory in small bits. If the caching mechanism didn't
68 maintain the DIRTY information, then something like a two byte
69 write would mean that the entire cache line would have to be read,
70 the two bytes modified and then written out again. The alternative
71 would be to not read in the cache line in the first place, and just
72 write the two bytes directly into target memory. The trouble with
73 that is that it really nails performance, because of the remote
74 protocol overhead. This way, all those little writes are bundled
75 up into an entire cache line write in one go, without having to
76 read the cache line in the first place.
29e57380 77 */
c906108c 78
29e57380 79/* NOTE: Interaction of dcache and memory region attributes
c906108c 80
29e57380
C
81 As there is no requirement that memory region attributes be aligned
82 to or be a multiple of the dcache page size, dcache_read_line() and
83 dcache_write_line() must break up the page by memory region. If a
84 chunk does not have the cache attribute set, an invalid memory type
85 is set, etc., then the chunk is skipped. Those chunks are handled
86 in target_xfer_memory() (or target_xfer_memory_partial()).
c906108c 87
29e57380
C
88 This doesn't occur very often. The most common occurance is when
89 the last bit of the .text segment and the first bit of the .data
90 segment fall within the same dcache page with a ro/cacheable memory
91 region defined for the .text segment and a rw/non-cacheable memory
92 region defined for the .data segment. */
c906108c
SS
93
94/* This value regulates the number of cache blocks stored.
95 Smaller values reduce the time spent searching for a cache
96 line, and reduce memory requirements, but increase the risk
97 of a line not being in memory */
98
c5aa993b 99#define DCACHE_SIZE 64
c906108c
SS
100
101/* This value regulates the size of a cache line. Smaller values
102 reduce the time taken to read a single byte, but reduce overall
103 throughput. */
104
c5aa993b 105#define LINE_SIZE_POWER (5)
c906108c
SS
106#define LINE_SIZE (1 << LINE_SIZE_POWER)
107
108/* Each cache block holds LINE_SIZE bytes of data
109 starting at a multiple-of-LINE_SIZE address. */
110
c5aa993b 111#define LINE_SIZE_MASK ((LINE_SIZE - 1))
c906108c
SS
112#define XFORM(x) ((x) & LINE_SIZE_MASK)
113#define MASK(x) ((x) & ~LINE_SIZE_MASK)
114
115
07128da0
DE
116#define ENTRY_INVALID 0 /* data at this byte is wrong */
117#define ENTRY_DIRTY 1 /* data at this byte needs to be written back */
118#define ENTRY_VALID 2 /* data at this byte is same as in memory */
c906108c 119
07128da0
DE
120/* For cache state display by "info dcache".
121 The letters I,D,V map to
122 I = ENTRY_INVALID
123 D = ENTRY_DIRTY
124 V = ENTRY_VALID */
125static const char state_chars[3] = { 'I', 'D', 'V' };
c906108c
SS
126
127struct dcache_block
c5aa993b
JM
128 {
129 struct dcache_block *p; /* next in list */
130 CORE_ADDR addr; /* Address for which data is recorded. */
6c932e54 131 gdb_byte data[LINE_SIZE]; /* bytes at given address */
c5aa993b 132 unsigned char state[LINE_SIZE]; /* what state the data is in */
c906108c 133
c5aa993b
JM
134 /* whether anything in state is dirty - used to speed up the
135 dirty scan. */
136 int anydirty;
c906108c 137
c5aa993b
JM
138 int refs;
139 };
c906108c
SS
140
141
29e57380
C
142/* FIXME: dcache_struct used to have a cache_has_stuff field that was
143 used to record whether the cache had been accessed. This was used
144 to invalidate the cache whenever caching was (re-)enabled (if the
145 cache was disabled and later re-enabled, it could contain stale
146 data). This was not needed because the cache is write through and
147 the code that enables, disables, and deletes memory region all
148 invalidate the cache.
149
150 This is overkill, since it also invalidates cache lines from
151 unrelated regions. One way this could be addressed by adding a
152 new function that takes an address and a length and invalidates
153 only those cache lines that match. */
154
c5aa993b
JM
155struct dcache_struct
156 {
c5aa993b
JM
157 /* free list */
158 struct dcache_block *free_head;
159 struct dcache_block *free_tail;
c906108c 160
c5aa993b
JM
161 /* in use list */
162 struct dcache_block *valid_head;
163 struct dcache_block *valid_tail;
c906108c 164
c5aa993b
JM
165 /* The cache itself. */
166 struct dcache_block *the_cache;
c5aa993b 167 };
c906108c 168
8edbea78 169static struct dcache_block *dcache_hit (DCACHE *dcache, CORE_ADDR addr);
c906108c 170
8edbea78 171static int dcache_write_line (DCACHE *dcache, struct dcache_block *db);
c906108c 172
8edbea78 173static int dcache_read_line (DCACHE *dcache, struct dcache_block *db);
c906108c 174
8edbea78
C
175static struct dcache_block *dcache_alloc (DCACHE *dcache, CORE_ADDR addr);
176
177static int dcache_writeback (DCACHE *dcache);
c906108c 178
a14ed312 179static void dcache_info (char *exp, int tty);
c906108c 180
a14ed312 181void _initialize_dcache (void);
c906108c 182
917317f4 183static int dcache_enabled_p = 0;
07128da0 184
920d2a44
AC
185static void
186show_dcache_enabled_p (struct ui_file *file, int from_tty,
187 struct cmd_list_element *c, const char *value)
188{
189 fprintf_filtered (file, _("Cache use for remote targets is %s.\n"), value);
190}
191
c906108c 192
c5aa993b 193DCACHE *last_cache; /* Used by info dcache */
c906108c
SS
194
195
196/* Free all the data cache blocks, thus discarding all cached data. */
197
198void
4930751a 199dcache_invalidate (DCACHE *dcache)
c906108c
SS
200{
201 int i;
202 dcache->valid_head = 0;
203 dcache->valid_tail = 0;
204
205 dcache->free_head = 0;
206 dcache->free_tail = 0;
207
208 for (i = 0; i < DCACHE_SIZE; i++)
209 {
210 struct dcache_block *db = dcache->the_cache + i;
211
212 if (!dcache->free_head)
213 dcache->free_head = db;
214 else
215 dcache->free_tail->p = db;
216 dcache->free_tail = db;
217 db->p = 0;
218 }
219
c906108c
SS
220 return;
221}
222
223/* If addr is present in the dcache, return the address of the block
224 containing it. */
225
226static struct dcache_block *
fba45db2 227dcache_hit (DCACHE *dcache, CORE_ADDR addr)
c906108c 228{
52f0bd74 229 struct dcache_block *db;
c906108c
SS
230
231 /* Search all cache blocks for one that is at this address. */
232 db = dcache->valid_head;
233
234 while (db)
235 {
c5aa993b 236 if (MASK (addr) == db->addr)
c906108c
SS
237 {
238 db->refs++;
239 return db;
240 }
241 db = db->p;
242 }
243
244 return NULL;
245}
246
247/* Make sure that anything in this line which needs to
248 be written is. */
249
250static int
aa1ee363 251dcache_write_line (DCACHE *dcache, struct dcache_block *db)
c906108c 252{
29e57380 253 CORE_ADDR memaddr;
6c932e54 254 gdb_byte *myaddr;
29e57380
C
255 int len;
256 int res;
257 int reg_len;
258 struct mem_region *region;
8edbea78 259
29e57380
C
260 if (!db->anydirty)
261 return 1;
262
263 len = LINE_SIZE;
264 memaddr = db->addr;
265 myaddr = db->data;
266
267 while (len > 0)
c906108c 268 {
29e57380
C
269 int s;
270 int e;
271 int dirty_len;
272
273 region = lookup_mem_region(memaddr);
274 if (memaddr + len < region->hi)
275 reg_len = len;
276 else
277 reg_len = region->hi - memaddr;
278
279 if (!region->attrib.cache || region->attrib.mode == MEM_RO)
280 {
281 memaddr += reg_len;
282 myaddr += reg_len;
283 len -= reg_len;
284 continue;
285 }
286
287 while (reg_len > 0)
c906108c 288 {
29e57380 289 s = XFORM(memaddr);
c839c4db 290 while (reg_len > 0) {
29e57380
C
291 if (db->state[s] == ENTRY_DIRTY)
292 break;
293 s++;
294 reg_len--;
c839c4db
C
295
296 memaddr++;
297 myaddr++;
298 len--;
299 }
29e57380
C
300
301 e = s;
c839c4db 302 while (reg_len > 0) {
29e57380
C
303 if (db->state[e] != ENTRY_DIRTY)
304 break;
305 e++;
306 reg_len--;
c839c4db 307 }
29e57380
C
308
309 dirty_len = e - s;
cf7a04e8
DJ
310 res = target_write (&current_target, TARGET_OBJECT_RAW_MEMORY,
311 NULL, myaddr, memaddr, dirty_len);
312 if (res < dirty_len)
313 return 0;
314
07128da0 315 memset (&db->state[XFORM(memaddr)], ENTRY_VALID, res);
cf7a04e8
DJ
316 memaddr += res;
317 myaddr += res;
318 len -= res;
c906108c 319 }
c906108c 320 }
29e57380
C
321
322 db->anydirty = 0;
c906108c
SS
323 return 1;
324}
325
8edbea78
C
326/* Read cache line */
327static int
328dcache_read_line (DCACHE *dcache, struct dcache_block *db)
329{
330 CORE_ADDR memaddr;
6c932e54 331 gdb_byte *myaddr;
8edbea78
C
332 int len;
333 int res;
29e57380
C
334 int reg_len;
335 struct mem_region *region;
8edbea78
C
336
337 /* If there are any dirty bytes in the line, it must be written
338 before a new line can be read */
339 if (db->anydirty)
340 {
341 if (!dcache_write_line (dcache, db))
342 return 0;
343 }
344
345 len = LINE_SIZE;
346 memaddr = db->addr;
347 myaddr = db->data;
348
349 while (len > 0)
350 {
29e57380
C
351 region = lookup_mem_region(memaddr);
352 if (memaddr + len < region->hi)
353 reg_len = len;
354 else
355 reg_len = region->hi - memaddr;
356
357 if (!region->attrib.cache || region->attrib.mode == MEM_WO)
358 {
359 memaddr += reg_len;
360 myaddr += reg_len;
361 len -= reg_len;
362 continue;
363 }
364
cf7a04e8
DJ
365 res = target_read (&current_target, TARGET_OBJECT_RAW_MEMORY,
366 NULL, myaddr, memaddr, reg_len);
367 if (res < reg_len)
368 return 0;
8edbea78 369
cf7a04e8
DJ
370 memaddr += res;
371 myaddr += res;
372 len -= res;
8edbea78
C
373 }
374
07128da0 375 memset (db->state, ENTRY_VALID, sizeof (db->data));
8edbea78
C
376 db->anydirty = 0;
377
378 return 1;
379}
380
c906108c 381/* Get a free cache block, put or keep it on the valid list,
f1d7622b 382 and return its address. */
c906108c
SS
383
384static struct dcache_block *
f1d7622b 385dcache_alloc (DCACHE *dcache, CORE_ADDR addr)
c906108c 386{
52f0bd74 387 struct dcache_block *db;
c906108c 388
c906108c
SS
389 /* Take something from the free list */
390 db = dcache->free_head;
391 if (db)
392 {
393 dcache->free_head = db->p;
394 }
395 else
396 {
397 /* Nothing left on free list, so grab one from the valid list */
398 db = dcache->valid_head;
c906108c 399
8edbea78
C
400 if (!dcache_write_line (dcache, db))
401 return NULL;
402
403 dcache->valid_head = db->p;
c906108c
SS
404 }
405
f1d7622b
C
406 db->addr = MASK(addr);
407 db->refs = 0;
408 db->anydirty = 0;
07128da0 409 memset (db->state, ENTRY_INVALID, sizeof (db->data));
f1d7622b 410
c906108c
SS
411 /* append this line to end of valid list */
412 if (!dcache->valid_head)
413 dcache->valid_head = db;
414 else
415 dcache->valid_tail->p = db;
416 dcache->valid_tail = db;
417 db->p = 0;
418
419 return db;
420}
421
29e57380 422/* Writeback any dirty lines. */
c906108c 423static int
fba45db2 424dcache_writeback (DCACHE *dcache)
c906108c
SS
425{
426 struct dcache_block *db;
427
428 db = dcache->valid_head;
429
430 while (db)
431 {
432 if (!dcache_write_line (dcache, db))
433 return 0;
434 db = db->p;
435 }
436 return 1;
437}
438
439
8edbea78
C
440/* Using the data cache DCACHE return the contents of the byte at
441 address ADDR in the remote machine.
442
443 Returns 0 on error. */
444
445static int
6c932e54 446dcache_peek_byte (DCACHE *dcache, CORE_ADDR addr, gdb_byte *ptr)
8edbea78 447{
52f0bd74 448 struct dcache_block *db = dcache_hit (dcache, addr);
8edbea78
C
449
450 if (!db)
451 {
452 db = dcache_alloc (dcache, addr);
453 if (!db)
454 return 0;
455 }
456
07128da0 457 if (db->state[XFORM (addr)] == ENTRY_INVALID)
8edbea78
C
458 {
459 if (!dcache_read_line(dcache, db))
460 return 0;
461 }
462
463 *ptr = db->data[XFORM (addr)];
464 return 1;
465}
466
467
c906108c
SS
468/* Write the byte at PTR into ADDR in the data cache.
469 Return zero on write error.
470 */
471
472static int
6c932e54 473dcache_poke_byte (DCACHE *dcache, CORE_ADDR addr, gdb_byte *ptr)
c906108c 474{
52f0bd74 475 struct dcache_block *db = dcache_hit (dcache, addr);
c906108c
SS
476
477 if (!db)
478 {
f1d7622b 479 db = dcache_alloc (dcache, addr);
8edbea78
C
480 if (!db)
481 return 0;
c906108c
SS
482 }
483
484 db->data[XFORM (addr)] = *ptr;
485 db->state[XFORM (addr)] = ENTRY_DIRTY;
486 db->anydirty = 1;
487 return 1;
488}
489
c906108c
SS
490/* Initialize the data cache. */
491DCACHE *
4930751a 492dcache_init (void)
c906108c
SS
493{
494 int csize = sizeof (struct dcache_block) * DCACHE_SIZE;
495 DCACHE *dcache;
496
497 dcache = (DCACHE *) xmalloc (sizeof (*dcache));
c906108c
SS
498
499 dcache->the_cache = (struct dcache_block *) xmalloc (csize);
500 memset (dcache->the_cache, 0, csize);
501
4930751a 502 dcache_invalidate (dcache);
c906108c
SS
503
504 last_cache = dcache;
505 return dcache;
506}
507
e99586d5
C
508/* Free a data cache */
509void
510dcache_free (DCACHE *dcache)
511{
512 if (last_cache == dcache)
513 last_cache = NULL;
514
b8c9b27d
KB
515 xfree (dcache->the_cache);
516 xfree (dcache);
e99586d5
C
517}
518
c906108c
SS
519/* Read or write LEN bytes from inferior memory at MEMADDR, transferring
520 to or from debugger address MYADDR. Write to inferior if SHOULD_WRITE is
521 nonzero.
522
523 Returns length of data written or read; 0 for error.
524
525 This routine is indended to be called by remote_xfer_ functions. */
526
527int
1b0ba102
AC
528dcache_xfer_memory (DCACHE *dcache, CORE_ADDR memaddr, gdb_byte *myaddr,
529 int len, int should_write)
c906108c
SS
530{
531 int i;
6c932e54 532 int (*xfunc) (DCACHE *dcache, CORE_ADDR addr, gdb_byte *ptr);
29e57380 533 xfunc = should_write ? dcache_poke_byte : dcache_peek_byte;
c906108c 534
29e57380 535 for (i = 0; i < len; i++)
c906108c 536 {
29e57380
C
537 if (!xfunc (dcache, memaddr + i, myaddr + i))
538 return 0;
c906108c 539 }
c906108c 540
29e57380
C
541 /* FIXME: There may be some benefit from moving the cache writeback
542 to a higher layer, as it could occur after a sequence of smaller
543 writes have been completed (as when a stack frame is constructed
544 for an inferior function call). Note that only moving it up one
545 level to target_xfer_memory() (also target_xfer_memory_partial())
546 is not sufficent, since we want to coalesce memory transfers that
547 are "logically" connected but not actually a single call to one
548 of the memory transfer functions. */
549
550 if (should_write)
551 dcache_writeback (dcache);
552
c906108c
SS
553 return len;
554}
555
c5aa993b 556static void
fba45db2 557dcache_info (char *exp, int tty)
c906108c
SS
558{
559 struct dcache_block *p;
560
a3f17187 561 printf_filtered (_("Dcache line width %d, depth %d\n"),
c906108c
SS
562 LINE_SIZE, DCACHE_SIZE);
563
5e2039ea 564 if (last_cache)
c906108c 565 {
a3f17187 566 printf_filtered (_("Cache state:\n"));
5e2039ea
C
567
568 for (p = last_cache->valid_head; p; p = p->p)
569 {
570 int j;
a3f17187 571 printf_filtered (_("Line at %s, referenced %d times\n"),
5e2039ea 572 paddr (p->addr), p->refs);
c906108c 573
5e2039ea
C
574 for (j = 0; j < LINE_SIZE; j++)
575 printf_filtered ("%02x", p->data[j] & 0xFF);
a3f17187 576 printf_filtered (("\n"));
c906108c 577
5e2039ea 578 for (j = 0; j < LINE_SIZE; j++)
07128da0 579 printf_filtered (" %c", state_chars[p->state[j]]);
5e2039ea
C
580 printf_filtered ("\n");
581 }
c906108c
SS
582 }
583}
584
585void
fba45db2 586_initialize_dcache (void)
c906108c 587{
5bf193a2
AC
588 add_setshow_boolean_cmd ("remotecache", class_support,
589 &dcache_enabled_p, _("\
590Set cache use for remote targets."), _("\
591Show cache use for remote targets."), _("\
c906108c
SS
592When on, use data caching for remote targets. For many remote targets\n\
593this option can offer better throughput for reading target memory.\n\
594Unfortunately, gdb does not currently know anything about volatile\n\
595registers and thus data caching will produce incorrect results with\n\
5bf193a2
AC
596volatile registers are in use. By default, this option is off."),
597 NULL,
920d2a44 598 show_dcache_enabled_p,
5bf193a2 599 &setlist, &showlist);
c906108c
SS
600
601 add_info ("dcache", dcache_info,
07128da0
DE
602 _("\
603Print information on the dcache performance.\n\
604The state of each cached byte is represented by a letter:\n\
605 I = invalid\n\
606 D = dirty\n\
607 V = valid"));
c906108c 608}
This page took 0.648103 seconds and 4 git commands to generate.