add to, and prioritize the TODO a little.
[trinity.git] / maps-shared.c
blob271b388d558c2571f8c66746cbb00d1595150274
1 #include <fcntl.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <unistd.h>
6 #include <sys/mman.h>
7 #include "arch.h"
8 #include "list.h"
9 #include "log.h"
10 #include "maps.h"
11 #include "utils.h"
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) {
23 struct map *m;
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)
32 struct map *newnode;
33 struct list_head *list;
34 int fd;
35 char buf[10];
37 fd = open("/dev/zero", O_RDWR);
38 if (fd == -1) {
39 outputerr("couldn't open /dev/zero\n");
40 exit(EXIT_FAILURE);
43 newnode = zmalloc(sizeof(struct map));
44 newnode->name = strdup(name);
45 newnode->size = size;
46 newnode->prot = prot;
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");
51 exit(EXIT_FAILURE);
54 newnode->name = malloc(80);
55 if (!newnode->name) {
56 outputerr("malloc() failed in %s().", __func__);
57 exit(EXIT_FAILURE);
60 sprintf(newnode->name, "anon(%s)", name);
62 num_shared_mappings++;
64 list = &shared_mappings->list;
65 list_add_tail(&newnode->list, list);
67 sizeunit(size, buf);
68 output(2, "mapping[%d]: (zeropage %s) %p (%s)\n",
69 num_shared_mappings - 1, name, newnode->ptr, buf);
71 close(fd);
74 void setup_shared_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 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)
107 struct map *m;
109 while (!list_empty(&shared_mappings->list)) {
110 m = shared_mappings;
112 munmap(m->ptr, m->size);
113 free(m->name);
115 shared_mappings = (struct map *) m->list.next;
117 list_del(&m->list);
118 free(m);
121 num_shared_mappings = 0;