11 #include "trinity.h" // page_size
17 static unsigned int num_mappings
= 0;
18 static struct map
*maps_list
;
25 void * alloc_shared(unsigned int size
)
29 ret
= mmap(NULL
, size
, PROT_READ
| PROT_WRITE
, MAP_ANON
| MAP_SHARED
, -1, 0);
30 if (ret
== MAP_FAILED
)
36 static struct map
* alloc_map(void)
40 newmap
= malloc(sizeof(struct map
));
42 outputerr("Couldn't allocate maps list!\n");
45 memset(newmap
, 0, sizeof(struct map
));
49 static void dump_maps(void)
51 struct map
*tmpmap
= maps_list
;
54 output(2, "There are %d entries in the map table\n", num_mappings
);
56 for (j
= 0; j
< num_mappings
; j
++) {
57 output(2, " start: %p name: %s\n", tmpmap
->ptr
, tmpmap
->name
);
58 tmpmap
= tmpmap
->next
;
62 static void * alloc_zero_map(struct map
*map
, int prot
, const char *name
)
64 struct map
*tmpmap
= map
;
66 unsigned long size
= 0;
71 fd
= open("/dev/zero", O_RDWR
);
73 outputerr("open /dev/zero failure. %s\n", strerror(errno
));
77 /* Pick a random sized mmap. */
79 case 0: size
= page_size
;
81 case 1: size
= 1024*1024;
83 case 2: size
= 2 * (1024*1024);
85 case 3: size
= 4 * (1024*1024);
91 /* page_size * 2, so we have a guard page afterwards.
92 * This is necessary for when we want to test page boundaries.
93 * see end of _get_address() for details.
97 tmpmap
->ptr
= mmap(NULL
, size
, prot
, MAP_ANONYMOUS
|MAP_SHARED
, fd
, 0);
99 if (tmpmap
->ptr
== MAP_FAILED
) {
100 outputerr("mmap /dev/zero failure\n");
106 tmpmap
->name
= malloc(80);
108 outputerr("malloc() failed in %s().", __func__
);
112 sprintf(tmpmap
->name
, "/dev/zero(%s)", name
);
115 output(2, "mapping[%d]: (zeropage %s) %p (%lu bytes)\n",
116 num_mappings
- 1, name
, tmpmap
->ptr
, size
);
123 void setup_maps(void)
127 tmpmap
= maps_list
= alloc_map();
129 /* Add a bunch of /dev/zero mappings */
130 tmpmap
->next
= alloc_zero_map(tmpmap
, PROT_READ
| PROT_WRITE
, "PROT_READ | PROT_WRITE");
131 tmpmap
= tmpmap
->next
;
133 tmpmap
->next
= alloc_zero_map(NULL
, PROT_READ
, "PROT_READ");
134 tmpmap
= tmpmap
->next
;
136 tmpmap
->next
= alloc_zero_map(NULL
, PROT_WRITE
, "PROT_WRITE");
138 output(2, "Added /dev/zero mappings.\n");
144 struct map
*tmpmap
= maps_list
;
147 i
= rand() % num_mappings
;
148 for (j
= 0; j
< i
; j
++)
149 tmpmap
= tmpmap
->next
;
154 void destroy_maps(void)
157 struct map
*thismap
= maps_list
, *next
;
159 for (i
= 0; i
< num_mappings
; i
++) {
160 next
= thismap
->next
;
161 munmap(thismap
->ptr
, thismap
->size
);
169 void init_buffers(void)
173 output(2, "shm is at %p\n", shm
);
175 page_zeros
= memalign(page_size
, page_size
* 2);
178 memset(page_zeros
, 0, page_size
);
179 output(2, "page_zeros @ %p\n", page_zeros
);
181 page_0xff
= memalign(page_size
, page_size
* 2);
184 memset(page_0xff
, 0xff, page_size
);
185 output(2, "page_0xff @ %p\n", page_0xff
);
187 page_rand
= memalign(page_size
, page_size
* 2);
190 memset(page_rand
, 0x55, page_size
); /* overwritten below */
191 output(2, "page_rand @ %p\n", page_rand
);
193 page_allocs
= memalign(page_size
, page_size
* 2);
196 memset(page_allocs
, 0xff, page_size
);
197 output(2, "page_allocs @ %p\n", page_allocs
);
199 for (i
= 0; i
< (page_size
/ sizeof(unsigned long *)); i
++)
200 page_allocs
[i
] = (unsigned long) malloc(page_size
);
204 // generate_random_page may end up using maps, so has to be last.
205 generate_random_page(page_rand
);