1 /* Caching code for GDB, the GNU debugger.
3 Copyright (C) 1992, 1993, 1995, 1996, 1998, 1999, 2000, 2001, 2003, 2007,
4 2008, 2009 Free Software Foundation, Inc.
6 This file is part of GDB.
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 3 of the License, or
11 (at your option) any later version.
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.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "gdb_string.h"
27 #include "splay-tree.h"
29 /* The data cache could lead to incorrect results because it doesn't
30 know about volatile variables, thus making it impossible to debug
31 functions which use memory mapped I/O devices. Set the nocache
32 memory region attribute in those cases.
34 In general the dcache speeds up performance. Some speed improvement
35 comes from the actual caching mechanism, but the major gain is in
36 the reduction of the remote protocol overhead; instead of reading
37 or writing a large area of memory in 4 byte requests, the cache
38 bundles up the requests into LINE_SIZE chunks, reducing overhead
39 significantly. This is most useful when accessing a large amount
40 of data, such as when performing a backtrace.
42 The cache is a splay tree along with a linked list for replacement.
43 Each block caches a LINE_SIZE area of memory. Wtihin each line we remember
44 the address of the line (which must be a multiple of LINE_SIZE) and the
47 Lines are only allocated as needed, so DCACHE_SIZE really specifies the
48 *maximum* number of lines in the cache.
50 At present, the cache is write-through rather than writeback: as soon
51 as data is written to the cache, it is also immediately written to
52 the target. Therefore, cache lines are never "dirty". Whether a given
53 line is valid or not depends on where it is stored in the dcache_struct;
54 there is no per-block valid flag. */
56 /* NOTE: Interaction of dcache and memory region attributes
58 As there is no requirement that memory region attributes be aligned
59 to or be a multiple of the dcache page size, dcache_read_line() and
60 dcache_write_line() must break up the page by memory region. If a
61 chunk does not have the cache attribute set, an invalid memory type
62 is set, etc., then the chunk is skipped. Those chunks are handled
63 in target_xfer_memory() (or target_xfer_memory_partial()).
65 This doesn't occur very often. The most common occurance is when
66 the last bit of the .text segment and the first bit of the .data
67 segment fall within the same dcache page with a ro/cacheable memory
68 region defined for the .text segment and a rw/non-cacheable memory
69 region defined for the .data segment. */
71 /* The maximum number of lines stored. The total size of the cache is
72 equal to DCACHE_SIZE times LINE_SIZE. */
73 #define DCACHE_SIZE 4096
75 /* The size of a cache line. Smaller values reduce the time taken to
76 read a single byte and make the cache more granular, but increase
77 overhead and reduce the effectiveness of the cache as a prefetcher. */
78 #define LINE_SIZE_POWER 6
79 #define LINE_SIZE (1 << LINE_SIZE_POWER)
81 /* Each cache block holds LINE_SIZE bytes of data
82 starting at a multiple-of-LINE_SIZE address. */
84 #define LINE_SIZE_MASK ((LINE_SIZE - 1))
85 #define XFORM(x) ((x) & LINE_SIZE_MASK)
86 #define MASK(x) ((x) & ~LINE_SIZE_MASK)
90 struct dcache_block
*newer
; /* for LRU and free list */
91 CORE_ADDR addr
; /* address of data */
92 gdb_byte data
[LINE_SIZE
]; /* bytes at given address */
93 int refs
; /* # hits */
99 struct dcache_block
*oldest
;
100 struct dcache_block
*newest
;
102 struct dcache_block
*freelist
;
104 /* The number of in-use lines in the cache. */
108 static struct dcache_block
*dcache_hit (DCACHE
*dcache
, CORE_ADDR addr
);
110 static int dcache_write_line (DCACHE
*dcache
, struct dcache_block
*db
);
112 static int dcache_read_line (DCACHE
*dcache
, struct dcache_block
*db
);
114 static struct dcache_block
*dcache_alloc (DCACHE
*dcache
, CORE_ADDR addr
);
116 static void dcache_info (char *exp
, int tty
);
118 void _initialize_dcache (void);
120 static int dcache_enabled_p
= 0;
123 show_dcache_enabled_p (struct ui_file
*file
, int from_tty
,
124 struct cmd_list_element
*c
, const char *value
)
126 fprintf_filtered (file
, _("Cache use for remote targets is %s.\n"), value
);
130 static DCACHE
*last_cache
; /* Used by info dcache */
132 /* Free all the data cache blocks, thus discarding all cached data. */
135 dcache_invalidate (DCACHE
*dcache
)
137 struct dcache_block
*block
, *next
;
139 block
= dcache
->oldest
;
143 splay_tree_remove (dcache
->tree
, (splay_tree_key
) block
->addr
);
146 block
->newer
= dcache
->freelist
;
147 dcache
->freelist
= block
;
152 dcache
->oldest
= NULL
;
153 dcache
->newest
= NULL
;
157 /* If addr is present in the dcache, return the address of the block
160 static struct dcache_block
*
161 dcache_hit (DCACHE
*dcache
, CORE_ADDR addr
)
163 struct dcache_block
*db
;
165 splay_tree_node node
= splay_tree_lookup (dcache
->tree
,
166 (splay_tree_key
) MASK (addr
));
171 db
= (struct dcache_block
*) node
->value
;
176 /* Fill a cache line from target memory. */
179 dcache_read_line (DCACHE
*dcache
, struct dcache_block
*db
)
186 struct mem_region
*region
;
194 /* Don't overrun if this block is right at the end of the region. */
195 region
= lookup_mem_region (memaddr
);
196 if (region
->hi
== 0 || memaddr
+ len
< region
->hi
)
199 reg_len
= region
->hi
- memaddr
;
201 /* Skip non-cacheable/non-readable regions. */
202 if (!region
->attrib
.cache
|| region
->attrib
.mode
== MEM_WO
)
210 res
= target_read (¤t_target
, TARGET_OBJECT_RAW_MEMORY
,
211 NULL
, myaddr
, memaddr
, reg_len
);
223 /* Get a free cache block, put or keep it on the valid list,
224 and return its address. */
226 static struct dcache_block
*
227 dcache_alloc (DCACHE
*dcache
, CORE_ADDR addr
)
229 struct dcache_block
*db
;
231 if (dcache
->size
>= DCACHE_SIZE
)
233 /* Evict the least recently used line. */
235 dcache
->oldest
= db
->newer
;
237 splay_tree_remove (dcache
->tree
, (splay_tree_key
) db
->addr
);
241 db
= dcache
->freelist
;
243 dcache
->freelist
= db
->newer
;
245 db
= xmalloc (sizeof (struct dcache_block
));
250 db
->addr
= MASK (addr
);
255 dcache
->newest
->newer
= db
;
262 splay_tree_insert (dcache
->tree
, (splay_tree_key
) db
->addr
,
263 (splay_tree_value
) db
);
268 /* Using the data cache DCACHE return the contents of the byte at
269 address ADDR in the remote machine.
271 Returns 1 for success, 0 for error. */
274 dcache_peek_byte (DCACHE
*dcache
, CORE_ADDR addr
, gdb_byte
*ptr
)
276 struct dcache_block
*db
= dcache_hit (dcache
, addr
);
280 db
= dcache_alloc (dcache
, addr
);
282 if (!dcache_read_line (dcache
, db
))
286 *ptr
= db
->data
[XFORM (addr
)];
290 /* Write the byte at PTR into ADDR in the data cache.
292 The caller is responsible for also promptly writing the data
293 through to target memory.
295 If addr is not in cache, this function does nothing; writing to
296 an area of memory which wasn't present in the cache doesn't cause
299 Always return 1 to simplify dcache_xfer_memory. */
302 dcache_poke_byte (DCACHE
*dcache
, CORE_ADDR addr
, gdb_byte
*ptr
)
304 struct dcache_block
*db
= dcache_hit (dcache
, addr
);
307 db
->data
[XFORM (addr
)] = *ptr
;
313 dcache_splay_tree_compare (splay_tree_key a
, splay_tree_key b
)
323 /* Initialize the data cache. */
331 dcache
= (DCACHE
*) xmalloc (sizeof (*dcache
));
333 dcache
->tree
= splay_tree_new (dcache_splay_tree_compare
,
337 dcache
->oldest
= NULL
;
338 dcache
->newest
= NULL
;
339 dcache
->freelist
= NULL
;
346 /* Free a data cache. */
349 dcache_free (DCACHE
*dcache
)
351 struct dcache_block
*db
, *next
;
353 if (last_cache
== dcache
)
356 splay_tree_delete (dcache
->tree
);
357 for (db
= dcache
->freelist
; db
!= NULL
; db
= next
)
365 /* Read or write LEN bytes from inferior memory at MEMADDR, transferring
366 to or from debugger address MYADDR. Write to inferior if SHOULD_WRITE is
369 Returns length of data written or read; 0 for error. */
372 dcache_xfer_memory (struct target_ops
*ops
, DCACHE
*dcache
,
373 CORE_ADDR memaddr
, gdb_byte
*myaddr
,
374 int len
, int should_write
)
378 int (*xfunc
) (DCACHE
*dcache
, CORE_ADDR addr
, gdb_byte
*ptr
);
379 xfunc
= should_write
? dcache_poke_byte
: dcache_peek_byte
;
381 /* Do write-through first, so that if it fails, we don't write to
386 res
= target_write (ops
, TARGET_OBJECT_RAW_MEMORY
,
387 NULL
, myaddr
, memaddr
, len
);
392 for (i
= 0; i
< len
; i
++)
394 if (!xfunc (dcache
, memaddr
+ i
, myaddr
+ i
))
401 /* FIXME: There would be some benefit to making the cache write-back and
402 moving the writeback operation to a higher layer, as it could occur
403 after a sequence of smaller writes have been completed (as when a stack
404 frame is constructed for an inferior function call). Note that only
405 moving it up one level to target_xfer_memory[_partial]() is not
406 sufficient since we want to coalesce memory transfers that are
407 "logically" connected but not actually a single call to one of the
408 memory transfer functions. */
411 dcache_print_line (int index
)
414 struct dcache_block
*db
;
419 printf_filtered (_("No data cache available.\n"));
423 n
= splay_tree_min (last_cache
->tree
);
425 for (i
= index
; i
> 0; --i
)
429 n
= splay_tree_successor (last_cache
->tree
, n
->key
);
434 printf_filtered (_("No such cache line exists.\n"));
438 db
= (struct dcache_block
*) n
->value
;
440 printf_filtered (_("Line %d: address %s [%d hits]\n"),
441 index
, paddress (target_gdbarch
, db
->addr
), db
->refs
);
443 for (j
= 0; j
< LINE_SIZE
; j
++)
445 printf_filtered ("%02x ", db
->data
[j
]);
447 /* Print a newline every 16 bytes (48 characters) */
448 if ((j
% 16 == 15) && (j
!= LINE_SIZE
- 1))
449 printf_filtered ("\n");
451 printf_filtered ("\n");
455 dcache_info (char *exp
, int tty
)
458 int i
, refcount
, lineno
;
463 i
= strtol (exp
, &linestart
, 10);
464 if (linestart
== exp
|| i
< 0)
466 printf_filtered (_("Usage: info dcache [linenumber]\n"));
470 dcache_print_line (i
);
474 printf_filtered (_("Dcache line width %d, maximum size %d\n"),
475 LINE_SIZE
, DCACHE_SIZE
);
479 printf_filtered (_("No data cache available.\n"));
485 n
= splay_tree_min (last_cache
->tree
);
490 struct dcache_block
*db
= (struct dcache_block
*) n
->value
;
492 printf_filtered (_("Line %d: address %s [%d hits]\n"),
493 i
, paddress (target_gdbarch
, db
->addr
), db
->refs
);
495 refcount
+= db
->refs
;
497 n
= splay_tree_successor (last_cache
->tree
, n
->key
);
500 printf_filtered (_("Cache state: %d active lines, %d hits\n"), i
, refcount
);
504 _initialize_dcache (void)
506 add_setshow_boolean_cmd ("remotecache", class_support
,
507 &dcache_enabled_p
, _("\
508 Set cache use for remote targets."), _("\
509 Show cache use for remote targets."), _("\
510 When on, use data caching for remote targets. For many remote targets\n\
511 this option can offer better throughput for reading target memory.\n\
512 Unfortunately, gdb does not currently know anything about volatile\n\
513 registers and thus data caching will produce incorrect results with\n\
514 volatile registers are in use. By default, this option is off."),
516 show_dcache_enabled_p
,
517 &setlist
, &showlist
);
519 add_info ("dcache", dcache_info
,
521 Print information on the dcache performance.\n\
522 With no arguments, this command prints the cache configuration and a\n\
523 summary of each line in the cache. Use \"info dcache <lineno> to dump\"\n\
524 the contents of a given line."));