OSDN Git Service

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