1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
/*
* \brief Page allocation
* \author Christian Helmuth <ch12@tudos.org>
* Bjoern Doebel <doebel@tudos.org>
* \date 2007-01-22
*
* In Linux 2.6 this resides in mm/page_alloc.c.
*
* This implementation is far from complete as it does not cover "struct page"
* emulation. In Linux, there's an array of structures for all pages. In
* particular, iteration works for this array like:
*
* struct page *p = alloc_pages(3); // p refers to first page of allocation
* ++p; // p refers to second page
*
* There may be more things to cover and we should have a deep look into the
* kernel parts we want to reuse. Candidates for problems may be file systems,
* storage (USB, IDE), and video (bttv).
*/
/* Linux */
#include <linux/gfp.h>
#include <linux/string.h>
#include <linux/pagevec.h>
#include <linux/mm.h>
#include <asm/page.h>
#include "local.h"
unsigned long max_low_pfn;
unsigned long min_low_pfn;
unsigned long max_pfn;
/*******************
** Configuration **
*******************/
#define DEBUG_PAGE_ALLOC 0
/*
* DDE page cache
*
* We need to store all pages somewhere (which in the Linux kernel is
* performed by the huge VM infrastructure. Purpose for us is:
* - make virt_to_phys() work
* - enable external clients to hand in memory (e.g., a dm_phys
* dataspace and make it accessible as Linux pages to the DDE)
*/
#define DDE_PAGE_CACHE_SHIFT 10
#define DDE_PAGE_CACHE_SIZE (1 << DDE_PAGE_CACHE_SHIFT)
#define DDE_PAGE_CACHE_MASK (DDE_PAGE_CACHE_SIZE - 1)
typedef struct
{
struct hlist_node list;
struct page *page;
} page_cache_entry;
static struct hlist_head dde_page_cache[DDE_PAGE_CACHE_SIZE];
/** Hash function to map virtual addresses to page cache buckets. */
#define VIRT_TO_PAGEHASH(a) ((((unsigned long)a) >> PAGE_SHIFT) & DDE_PAGE_CACHE_MASK)
void dde_page_cache_add(struct page *p)
{
unsigned int hashval = VIRT_TO_PAGEHASH(p->virtual);
page_cache_entry *e = kmalloc(sizeof(page_cache_entry), GFP_KERNEL);
#if DEBUG_PAGE_ALLOC
DEBUG_MSG("virt %p, hash: %x", p->virtual, hashval);
#endif
e->page = p;
INIT_HLIST_NODE(&e->list);
hlist_add_head(&e->list, &dde_page_cache[hashval]);
}
void dde_page_cache_remove(struct page *p)
{
unsigned int hashval = VIRT_TO_PAGEHASH(p->virtual);
struct hlist_node *hn = NULL;
struct hlist_head *h = &dde_page_cache[hashval];
page_cache_entry *e = NULL;
struct hlist_node *v = NULL;
hlist_for_each_entry(e, hn, h, list) {
if ((unsigned long)e->page->virtual == ((unsigned long)p->virtual & PAGE_MASK))
v = hn;
break;
}
if (v) {
#if DEBUG_PAGE_ALLOC
DEBUG_MSG("deleting node %p which contained page %p", v, p);
#endif
hlist_del(v);
}
}
struct page* dde_page_lookup(unsigned long va)
{
unsigned int hashval = VIRT_TO_PAGEHASH(va);
struct hlist_node *hn = NULL;
struct hlist_head *h = &dde_page_cache[hashval];
page_cache_entry *e = NULL;
hlist_for_each_entry(e, hn, h, list) {
if ((unsigned long)e->page->virtual == (va & PAGE_MASK))
return e->page;
}
return NULL;
}
struct page * __alloc_pages_internal(gfp_t gfp_mask, unsigned int order,
struct zonelist *zonelist, nodemask_t *nm)
{
/* XXX: In fact, according to order, we should have one struct page
* for every page, not only for the first one.
*/
struct page *ret = kmalloc(sizeof(*ret), GFP_KERNEL);
ret->virtual = (void *)__get_free_pages(gfp_mask, order);
dde_page_cache_add(ret);
return ret;
}
unsigned long __get_free_pages(gfp_t gfp_mask, unsigned int order)
{
ddekit_log(DEBUG_PAGE_ALLOC, "gfp_mask=%x order=%d (%d bytes)",
gfp_mask, order, PAGE_SIZE << order);
Assert(gfp_mask != GFP_DMA);
void *p = ddekit_large_malloc(PAGE_SIZE << order);
return (unsigned long)p;
}
unsigned long get_zeroed_page(gfp_t gfp_mask)
{
unsigned long p = __get_free_pages(gfp_mask, 0);
if (p) memset((void *)p, 0, PAGE_SIZE);
return (unsigned long)p;
}
void free_hot_page(struct page *page)
{
WARN_UNIMPL;
}
/*
* XXX: If alloc_pages() gets fixed to allocate a page struct per page,
* this needs to be adapted, too.
*/
void __free_pages(struct page *page, unsigned int order)
{
free_pages((unsigned long)page->virtual, order);
dde_page_cache_remove(page);
}
void __pagevec_free(struct pagevec *pvec)
{
WARN_UNIMPL;
}
int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
unsigned long start, int len, int write, int force,
struct page **pages, struct vm_area_struct **vmas)
{
WARN_UNIMPL;
return 0;
}
/**
* ...
*
* XXX order may be larger than allocation at 'addr' - it may comprise several
* allocation via __get_free_pages()!
*/
void free_pages(unsigned long addr, unsigned int order)
{
ddekit_log(DEBUG_PAGE_ALLOC, "addr=%p order=%d", (void *)addr, order);
ddekit_large_free((void *)addr);
}
unsigned long __pa(volatile void *addr)
{
return ddekit_pgtab_get_physaddr((void*)addr);
}
void *__va(unsigned long addr)
{
return (void*)ddekit_pgtab_get_virtaddr((ddekit_addr_t) addr);
}
int set_page_dirty_lock(struct page *page)
{
WARN_UNIMPL;
return 0;
}
/*
* basically copied from linux/mm/page_alloc.c
*/
void *__init alloc_large_system_hash(const char *tablename,
unsigned long bucketsize,
unsigned long numentries,
int scale,
int flags,
unsigned int *_hash_shift,
unsigned int *_hash_mask,
unsigned long limit)
{
void * table = NULL;
unsigned long log2qty;
unsigned long size;
if (numentries == 0)
numentries = 1024;
log2qty = ilog2(numentries);
size = bucketsize << log2qty;
do {
unsigned long order;
for (order = 0; ((1UL << order) << PAGE_SHIFT) < size; order++);
table = (void*) __get_free_pages(GFP_ATOMIC, order);
} while (!table && size > PAGE_SIZE && --log2qty);
if (!table)
panic("Failed to allocate %s hash table\n", tablename);
printk("%s hash table entries: %d (order: %d, %lu bytes)\n",
tablename,
(1U << log2qty),
ilog2(size) - PAGE_SHIFT,
size);
if (_hash_shift)
*_hash_shift = log2qty;
if (_hash_mask)
*_hash_mask = (1 << log2qty) - 1;
return table;
}
static void __init dde_page_cache_init(void)
{
printk("Initializing DDE page cache\n");
int i=0;
for (i; i < DDE_PAGE_CACHE_SIZE; ++i)
INIT_HLIST_HEAD(&dde_page_cache[i]);
}
core_initcall(dde_page_cache_init);
|