18 #include "trinity.h" // page_size
21 static unsigned int num_global_mappings
= 0;
22 static struct map
*global_mappings
= NULL
;
24 static void dump_global_mappings(void)
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
)
40 struct list_head
*list
;
43 fd
= open("/dev/zero", O_RDWR
);
45 outputerr("couldn't open /dev/zero\n");
49 newnode
= zmalloc(sizeof(struct map
));
50 newnode
->name
= strdup(name
);
53 newnode
->ptr
= mmap(NULL
, size
, prot
, MAP_ANONYMOUS
| MAP_SHARED
, fd
, 0);
54 if (newnode
->ptr
== MAP_FAILED
) {
55 outputerr("mmap failure\n");
59 newnode
->name
= malloc(80);
61 outputerr("malloc() failed in %s().", __func__
);
65 sprintf(newnode
->name
, "anon(%s)", name
);
67 num_global_mappings
++;
69 list
= &global_mappings
->list
;
70 list_add_tail(&newnode
->list
, list
);
72 output(2, "mapping[%d]: (zeropage %s) %p (%lu bytes)\n",
73 num_global_mappings
- 1, name
, newnode
->ptr
, size
);
78 void setup_global_mappings(void)
81 const unsigned long sizes
[] = {
82 1 * MB
, 2 * MB
, 4 * MB
, 10 * MB
,
83 // 1 * GB, // disabled for now, due to OOM.
86 global_mappings
= zmalloc(sizeof(struct map
));
87 INIT_LIST_HEAD(&global_mappings
->list
);
89 /* page_size * 2, so we have a guard page afterwards.
90 * This is necessary for when we want to test page boundaries.
91 * see end of _get_address() for details.
93 alloc_zero_map(page_size
* 2, PROT_READ
| PROT_WRITE
, "PROT_READ | PROT_WRITE");
94 alloc_zero_map(page_size
* 2, PROT_READ
, "PROT_READ");
95 alloc_zero_map(page_size
* 2, PROT_WRITE
, "PROT_WRITE");
98 * multi megabyte page mappings.
100 for (i
= 0; i
< ARRAY_SIZE(sizes
); i
++) {
101 alloc_zero_map(sizes
[i
], PROT_READ
| PROT_WRITE
, "PROT_READ | PROT_WRITE");
102 alloc_zero_map(sizes
[i
], PROT_READ
, "PROT_READ");
103 alloc_zero_map(sizes
[i
], PROT_WRITE
, "PROT_WRITE");
106 dump_global_mappings();
109 /* Walk the list, get the j'th element */
110 static struct map
* __get_map(struct list_head
*head
, unsigned int max
)
113 struct list_head
*node
;
115 unsigned int i
, j
= 0;
119 list_for_each(node
, head
) {
120 m
= (struct map
*) node
;
129 struct map
* get_map(void)
134 /* If we're not running in child context, just do global. */
136 return __get_map(&global_mappings
->list
, num_global_mappings
);
138 /* Only toss the dice if we actually have local mappings. */
139 if (shm
->num_mappings
[this_child
] > 0)
143 map
= __get_map(&shm
->mappings
[this_child
]->list
, shm
->num_mappings
[this_child
]);
145 map
= __get_map(&global_mappings
->list
, num_global_mappings
);
150 void destroy_global_mappings(void)
154 while (!list_empty(&global_mappings
->list
)) {
157 munmap(m
->ptr
, m
->size
);
160 global_mappings
= (struct map
*) m
->list
.next
;
166 num_global_mappings
= 0;
169 void delete_local_mapping(int childno
, struct map
*map
)
171 list_del(&map
->list
);
172 shm
->num_mappings
[childno
]--;
175 struct map
* common_set_mmap_ptr_len(int childno
)
179 map
= (struct map
*) shm
->a1
[childno
];
180 shm
->scratch
[childno
] = (unsigned long) map
; /* Save this for ->post */
182 shm
->a1
[childno
] = 0;
183 shm
->a2
[childno
] = 0;
187 shm
->a1
[childno
] = (unsigned long) map
->ptr
;
188 shm
->a2
[childno
] = map
->size
; //TODO: Munge this.
193 void dirty_mapping(struct map
*map
)
198 /* Check mapping is writable. */
199 if (!(map
->prot
& PROT_WRITE
))
203 /* Just fault in one page. */
204 p
[rand() % page_size
] = 1;
206 /* fault in the whole mapping */
207 for (i
= 0; i
< map
->size
; i
+= page_size
)
210 //TODO: More access patterns.