Stackdb
Stackdb is a stackable, multi-target and -level source debugger and memory forensics library.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
memcache.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 The University of Utah
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of
7  * the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
19 #ifndef __MEMCACHE_H__
20 #define __MEMCACHE_H__
21 
22 #include "config.h"
23 #include "glib.h"
24 #include "common.h"
25 #include "clfit.h"
26 
27 /*
28  * Conceptually, we might need/want to cache any of physical or virtual
29  * memory page, or page ranges, depending on what kind of memory the
30  * target supports. We might want to tag it based on some kind of
31  * thread/addrspace/region identifier, so we can quickly blow away parts
32  * of the cache if a thread/addrspace/region goes away.
33  *
34  * Then, we also want to cache address translations; for now, start with
35  * v2p. Tag these with the same identifier as the mmap cache for now.
36  *
37  * I think we should do this via whatever ADDR-sized identifier the
38  * target wants to use. For an OS target, it would probably be the pgd.
39  */
40 
41 /*
42  * NB: TAG_ANY is a wildcard tag for some functions; therefore, never
43  * use it as a legitimate tag!
44  */
45 #define MEMCACHE_TAG_ANY ADDRMAX
46 
47 typedef void (*memcache_tag_priv_dtor)(ADDR tag,void *tag_priv);
48 
49 typedef enum {
50  MEMCACHE_PHYS = 1 << 0,
51  MEMCACHE_VIRT = 1 << 1,
53 
54 struct memcache {
55  unsigned long int max_v2p;
56  unsigned long int max_mmap_size;
57  unsigned long int current_mmap_size;
58  GHashTable *cache;
60 };
61 
63  void *priv;
64  GHashTable *v2p_cache;
65  /*
66  * For now, offer the option to cache either phys or virt mmaps.
67  * The reason to do this is that sometimes a bulk mmap might be made
68  * for a virt address, but of course, the mapped phys pages
69  * underneath are 1) only good in that mapping, for that v2p
70  * translation for that pgd; and 2)
71  * because it's a single mmap, we can't benefit from selectively
72  * uncaching only the virtual parts we're done with, and saving the
73  * phys pages that are still valid. So there seems to be little
74  * point to only caching phys pages -- *IF YOU WANT* the benefits of
75  * contiguously-mapped virtual address space!
76  *
77  * If you do not want contiguously-mapped vaddr space, then,
78  * clearly, caching individual physical pages is *always* the way to
79  * go... it offers the least need to remap.
80  *
81  * So let's at least offer users the option! Which mmap cache they
82  * get is controlled by the flags argument to the get|set_mmap
83  * functions.
84  */
87  /*
88  * Every time we increment ticks, we calculate the oldest entry.
89  * Then it's ready for our LRU eviction. We use ADDRMAX as the
90  * magic value saying there is no "max" entry (i.e., there *is* no
91  * entry :)). This can happen if memcache_lru_evict_mmap has to
92  * evict more than one (oldest) mapping, for instance. It's a win
93  * to keep these calculated only when we inc_ticks, though. If we
94  * need them and they're not available, we can calculate them
95  * quickly.
96  */
100  unsigned int oldest_mmap_p_ticks;
101  unsigned int oldest_mmap_v_ticks;
102 };
103 
105  //int used;
106  unsigned int unused_ticks;
108 };
109 
111  //int used;
112  unsigned int unused_ticks;
113  unsigned long int mem_len;
114  void *mem;
115  /*
116  * For now, don't track dependent values -- because we can always
117  * just use memcache_get to check and see if the tagged v2p exists,
118  * and if pa is mmap'd or not.
119  */
120 };
121 
122 struct memcache *memcache_create(unsigned long int max_v2p,
123  unsigned long int max_mmap_size,
124  memcache_tag_priv_dtor pdtor);
125 void memcache_destroy(struct memcache *memcache);
126 
130 
131 void memcache_inc_ticks(struct memcache *memcache,unsigned int new_ticks);
132 
133 int memcache_get_v2p(struct memcache *memcache,ADDR tag,ADDR va,
134  ADDR *pa,void **tag_priv);
135 int memcache_get_mmap(struct memcache *memcache,ADDR tag,ADDR pa,
136  unsigned long int pa_len,memcache_flags_t flags,
137  ADDR *pa_start,OFFSET *pa_offset,
138  void **mem,unsigned long int *mem_len,void **tag_priv);
139 
140 int memcache_set_tag_priv(struct memcache *memcache,ADDR tag,void *tag_priv);
141 int memcache_set_v2p(struct memcache *memcache,ADDR tag,ADDR va,ADDR pa);
142 int memcache_set_mmap(struct memcache *memcache,ADDR tag,ADDR pa,
143  memcache_flags_t flags,
144  void *mem,unsigned long int mem_len);
145 
146 /*
147  * A dumb little LRU evictor. Just makes sure to evict (at least)
148  * mem_len. It doesn't try to evict smartly -- i.e., even if the oldest
149  * is a big huge mmap and there's a slightly less-used smaller map right
150  * there, we'll evict that anyway. Also, for us, it might make more
151  * sense to evict virt ranges rather than physical pages... but no :).
152  * Return number of bytes evicted; might be less than requested if
153  * we couldn't find enough!
154  */
155 unsigned long int memcache_lru_evict_mmap(struct memcache *memcache,ADDR tag,
156  memcache_flags_t flags,
157  unsigned long int mem_len);
158 
159 #endif /* __MEMCACHE_H__ */
Definition: memcache.h:62
unsigned int oldest_mmap_v_ticks
Definition: memcache.h:101
ADDR pa
Definition: memcache.h:107
unsigned long int max_mmap_size
Definition: memcache.h:56
int memcache_set_v2p(struct memcache *memcache, ADDR tag, ADDR va, ADDR pa)
Definition: memcache.c:369
int32_t OFFSET
Definition: common.h:65
int memcache_get_mmap(struct memcache *memcache, ADDR tag, ADDR pa, unsigned long int pa_len, memcache_flags_t flags, ADDR *pa_start, OFFSET *pa_offset, void **mem, unsigned long int *mem_len, void **tag_priv)
Definition: memcache.c:265
int memcache_invalidate_all_v2p(struct memcache *memcache, ADDR tag)
Definition: memcache.c:78
int memcache_invalidate_all(struct memcache *memcache)
Definition: memcache.c:138
unsigned long int memcache_lru_evict_mmap(struct memcache *memcache, ADDR tag, memcache_flags_t flags, unsigned long int mem_len)
Definition: memcache.c:486
clrangesimple_t mmap_cache_v
Definition: memcache.h:86
int memcache_get_v2p(struct memcache *memcache, ADDR tag, ADDR va, ADDR *pa, void **tag_priv)
Definition: memcache.c:232
void memcache_destroy(struct memcache *memcache)
Definition: memcache.c:49
GHashTable * cache
Definition: memcache.h:58
memcache_flags_t
Definition: memcache.h:49
GHashTable * v2p_cache
Definition: memcache.h:64
clrangesimple_t mmap_cache_p
Definition: memcache.h:85
int memcache_invalidate_all_mmap(struct memcache *memcache, ADDR tag)
Definition: memcache.c:117
memcache_tag_priv_dtor tag_priv_dtor
Definition: memcache.h:59
void memcache_inc_ticks(struct memcache *memcache, unsigned int new_ticks)
Definition: memcache.c:220
ADDR oldest_v2p
Definition: memcache.h:97
int memcache_set_mmap(struct memcache *memcache, ADDR tag, ADDR pa, memcache_flags_t flags, void *mem, unsigned long int mem_len)
Definition: memcache.c:402
struct memcache * memcache_create(unsigned long int max_v2p, unsigned long int max_mmap_size, memcache_tag_priv_dtor pdtor)
Definition: memcache.c:33
Pvoid_t clrangesimple_t
Definition: clfit.h:95
unsigned int oldest_mmap_p_ticks
Definition: memcache.h:100
ADDR oldest_mmap_p
Definition: memcache.h:98
unsigned long int current_mmap_size
Definition: memcache.h:57
void * priv
Definition: memcache.h:63
unsigned long int mem_len
Definition: memcache.h:113
uint32_t ADDR
Definition: common.h:64
Definition: memcache.h:110
int memcache_set_tag_priv(struct memcache *memcache, ADDR tag, void *tag_priv)
Definition: memcache.c:354
ADDR oldest_mmap_v
Definition: memcache.h:99
Definition: memcache.h:104
unsigned int unused_ticks
Definition: memcache.h:106
unsigned int unused_ticks
Definition: memcache.h:112
unsigned long int max_v2p
Definition: memcache.h:55
void * mem
Definition: memcache.h:114
void(* memcache_tag_priv_dtor)(ADDR tag, void *tag_priv)
Definition: memcache.h:47