11 #include "trinity.h" // page_size
19 static unsigned int num_mappings
= 0;
20 static struct map
*maps
= NULL
;
27 void * alloc_shared(unsigned int size
)
31 ret
= mmap(NULL
, size
, PROT_READ
| PROT_WRITE
, MAP_ANON
| MAP_SHARED
, -1, 0);
32 if (ret
== MAP_FAILED
)
38 static void dump_maps(void)
41 struct list_head
*node
;
43 output(2, "There are %d entries in the map table\n", num_mappings
);
45 list_for_each(node
, &maps
->list
) {
46 m
= (struct map
*) node
;
47 output(2, " start: %p name: %s\n", m
->ptr
, m
->name
);
51 static void alloc_zero_map(int prot
, const char *name
)
54 struct list_head
*list
;
55 unsigned long size
= 0;
57 /* Pick a random sized mmap. */
59 case 0: size
= page_size
;
61 case 1: size
= 1024 * 1024;
63 case 2: size
= 2 * (1024 * 1024);
65 case 3: size
= 4 * (1024 * 1024);
71 /* page_size * 2, so we have a guard page afterwards.
72 * This is necessary for when we want to test page boundaries.
73 * see end of _get_address() for details.
77 newnode
= zmalloc(sizeof(struct map
));
78 newnode
->name
= strdup(name
);
80 newnode
->ptr
= mmap(NULL
, size
, prot
, MAP_ANONYMOUS
| MAP_SHARED
, 0, 0);
81 if (newnode
->ptr
== MAP_FAILED
) {
82 outputerr("mmap failure\n");
86 newnode
->name
= malloc(80);
88 outputerr("malloc() failed in %s().", __func__
);
92 sprintf(newnode
->name
, "anon(%s)", name
);
97 list_add_tail(&newnode
->list
, list
);
99 output(2, "mapping[%d]: (zeropage %s) %p (%lu bytes)\n",
100 num_mappings
- 1, name
, newnode
->ptr
, size
);
103 void setup_maps(void)
105 maps
= zmalloc(sizeof(struct map
));
106 INIT_LIST_HEAD(&maps
->list
);
108 alloc_zero_map(PROT_READ
| PROT_WRITE
, "PROT_READ | PROT_WRITE");
109 alloc_zero_map(PROT_READ
, "PROT_READ");
110 alloc_zero_map(PROT_WRITE
, "PROT_WRITE");
115 /* Walk the list, get the j'th element */
119 struct list_head
*node
;
120 unsigned int i
, j
= 0;
122 i
= rand() % num_mappings
;
124 list_for_each(node
, &maps
->list
) {
125 m
= (struct map
*) node
;
134 void destroy_maps(void)
136 struct map
*m
= maps
;
138 while (!list_empty(&maps
->list
)) {
141 munmap(m
->ptr
, m
->size
);
144 maps
= (struct map
*) m
->list
.next
;
152 void init_buffers(void)
156 output(2, "shm is at %p\n", shm
);
158 page_zeros
= memalign(page_size
, page_size
* 2);
161 memset(page_zeros
, 0, page_size
);
162 output(2, "page_zeros @ %p\n", page_zeros
);
164 page_0xff
= memalign(page_size
, page_size
* 2);
167 memset(page_0xff
, 0xff, page_size
);
168 output(2, "page_0xff @ %p\n", page_0xff
);
170 page_rand
= memalign(page_size
, page_size
* 2);
173 memset(page_rand
, 0x55, page_size
); /* overwritten below */
174 output(2, "page_rand @ %p\n", page_rand
);
176 page_allocs
= memalign(page_size
, page_size
* 2);
179 memset(page_allocs
, 0xff, page_size
);
180 output(2, "page_allocs @ %p\n", page_allocs
);
182 for (i
= 0; i
< (page_size
/ sizeof(unsigned long *)); i
++)
183 page_allocs
[i
] = (unsigned long) malloc(page_size
);
187 // generate_random_page may end up using maps, so has to be last.
188 generate_random_page(page_rand
);