13 unsigned int num_shared_mappings
= 0;
14 struct map
*shared_mappings
= NULL
;
16 static void dump_shared_mappings(void)
18 struct list_head
*node
;
20 output(2, "There are %d entries in the map table\n", num_shared_mappings
);
22 list_for_each(node
, &shared_mappings
->list
) {
25 m
= (struct map
*) node
;
26 output(2, " start: %p name: %s\n", m
->ptr
, m
->name
);
30 static void alloc_zero_map(unsigned long size
, int prot
, const char *name
)
33 struct list_head
*list
;
37 fd
= open("/dev/zero", O_RDWR
);
39 outputerr("couldn't open /dev/zero\n");
43 newnode
= zmalloc(sizeof(struct map
));
44 newnode
->name
= strdup(name
);
47 newnode
->type
= MAP_GLOBAL
;
48 newnode
->ptr
= mmap(NULL
, size
, prot
, MAP_ANONYMOUS
| MAP_SHARED
, fd
, 0);
49 if (newnode
->ptr
== MAP_FAILED
) {
50 outputerr("mmap failure\n");
54 newnode
->name
= malloc(80);
56 outputerr("malloc() failed in %s().", __func__
);
60 sprintf(newnode
->name
, "anon(%s)", name
);
62 num_shared_mappings
++;
64 list
= &shared_mappings
->list
;
65 list_add_tail(&newnode
->list
, list
);
68 output(2, "mapping[%d]: (zeropage %s) %p (%s)\n",
69 num_shared_mappings
- 1, name
, newnode
->ptr
, buf
);
74 void setup_shared_mappings(void)
77 const unsigned long sizes
[] = {
78 1 * MB
, 2 * MB
, 4 * MB
, 10 * MB
,
79 // 1 * GB, // disabled for now, due to OOM.
82 shared_mappings
= zmalloc(sizeof(struct map
));
83 INIT_LIST_HEAD(&shared_mappings
->list
);
85 /* page_size * 2, so we have a guard page afterwards.
86 * This is necessary for when we want to test page boundaries.
87 * see end of _get_address() for details.
89 alloc_zero_map(page_size
* 2, PROT_READ
| PROT_WRITE
, "PROT_READ | PROT_WRITE");
90 alloc_zero_map(page_size
* 2, PROT_READ
, "PROT_READ");
91 alloc_zero_map(page_size
* 2, PROT_WRITE
, "PROT_WRITE");
94 * multi megabyte page mappings.
96 for (i
= 0; i
< ARRAY_SIZE(sizes
); i
++) {
97 alloc_zero_map(sizes
[i
], PROT_READ
| PROT_WRITE
, "PROT_READ | PROT_WRITE");
98 alloc_zero_map(sizes
[i
], PROT_READ
, "PROT_READ");
99 alloc_zero_map(sizes
[i
], PROT_WRITE
, "PROT_WRITE");
102 dump_shared_mappings();
105 void destroy_shared_mappings(void)
109 while (!list_empty(&shared_mappings
->list
)) {
112 munmap(m
->ptr
, m
->size
);
115 shared_mappings
= (struct map
*) m
->list
.next
;
121 num_shared_mappings
= 0;