add more missing flushes
[trinity.git] / maps.c
blob1ff966664bc78e5b6905e2caaa09ae522992ebcf
1 #include <errno.h>
2 #include <fcntl.h>
3 #include <malloc.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <unistd.h>
8 #include <sys/mman.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include "trinity.h" // page_size
12 #include "arch.h"
13 #include "child.h"
14 #include "maps.h"
15 #include "list.h"
16 #include "log.h"
17 #include "random.h"
18 #include "shm.h"
19 #include "utils.h"
21 static unsigned int num_global_mappings = 0;
22 static struct map *global_mappings = NULL;
24 static void dump_global_mappings(void)
26 struct map *m;
27 struct list_head *node;
29 output(2, "There are %d entries in the map table\n", num_global_mappings);
31 list_for_each(node, &global_mappings->list) {
32 m = (struct map *) node;
33 output(2, " start: %p name: %s\n", m->ptr, m->name);
37 static void alloc_zero_map(unsigned long size, int prot, const char *name)
39 struct map *newnode;
40 struct list_head *list;
41 int fd;
43 fd = open("/dev/zero", O_RDWR);
45 newnode = zmalloc(sizeof(struct map));
46 newnode->name = strdup(name);
47 newnode->size = size;
48 newnode->prot = prot;
49 newnode->ptr = mmap(NULL, size, prot, MAP_ANONYMOUS | MAP_SHARED, fd, 0);
50 if (newnode->ptr == MAP_FAILED) {
51 outputerr("mmap failure\n");
52 exit(EXIT_FAILURE);
55 newnode->name = malloc(80);
56 if (!newnode->name) {
57 outputerr("malloc() failed in %s().", __func__);
58 exit(EXIT_FAILURE);
61 sprintf(newnode->name, "anon(%s)", name);
63 num_global_mappings++;
65 list = &global_mappings->list;
66 list_add_tail(&newnode->list, list);
68 output(2, "mapping[%d]: (zeropage %s) %p (%lu bytes)\n",
69 num_global_mappings - 1, name, newnode->ptr, size);
71 close(fd);
74 void setup_global_mappings(void)
76 unsigned int i;
77 const unsigned long sizes[] = {
78 1 * MB, 2 * MB, 4 * MB, 10 * MB,
79 // 1 * GB, // disabled for now, due to OOM.
82 global_mappings = zmalloc(sizeof(struct map));
83 INIT_LIST_HEAD(&global_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_global_mappings();
105 /* Walk the list, get the j'th element */
106 static struct map * __get_map(struct list_head *head, unsigned int max)
108 struct map *m;
109 struct list_head *node;
111 unsigned int i, j = 0;
113 i = rand() % max;
115 list_for_each(node, head) {
116 m = (struct map *) node;
118 if (i == j)
119 return m;
120 j++;
122 return 0;
125 struct map * get_map(void)
127 struct map *map;
128 bool local = FALSE;
130 /* If we're not running in child context, just do global. */
131 if (this_child == 0)
132 return __get_map(&global_mappings->list, num_global_mappings);
134 /* Only toss the dice if we actually have local mappings. */
135 if (shm->num_mappings[this_child] > 0)
136 local = rand_bool();
138 if (local == TRUE)
139 map = __get_map(&shm->mappings[this_child]->list, shm->num_mappings[this_child]);
140 else
141 map = __get_map(&global_mappings->list, num_global_mappings);
143 return map;
146 void destroy_global_mappings(void)
148 struct map *m = global_mappings;
150 while (!list_empty(&global_mappings->list)) {
151 m = global_mappings;
153 munmap(m->ptr, m->size);
154 free(m->name);
156 global_mappings = (struct map *) m->list.next;
158 list_del(&m->list);
159 free(m);
162 num_global_mappings = 0;
165 void delete_local_mapping(int childno, struct map *map)
167 list_del(&map->list);
168 shm->num_mappings[childno]--;
171 struct map * common_set_mmap_ptr_len(int childno)
173 struct map *map;
175 map = (struct map *) shm->a1[childno];
176 shm->scratch[childno] = (unsigned long) map; /* Save this for ->post */
177 if (map == NULL) {
178 shm->a1[childno] = 0;
179 shm->a2[childno] = 0;
180 return NULL;
183 shm->a1[childno] = (unsigned long) map->ptr;
184 shm->a2[childno] = map->size; //TODO: Munge this.
186 return map;