split out the page dirtying to its own file.
[trinity.git] / maps-fault.c
blob12d7df8908b04b6dc0d2606c8518368339b2d596
1 #include <stdlib.h>
2 #include <string.h>
3 #include <sys/mman.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include "arch.h"
7 #include "maps.h"
8 #include "random.h"
11 * Routine to perform various kinds of write operations to a mapping
12 * that we created.
14 void dirty_mapping(struct map *map)
16 char *p = map->ptr;
17 unsigned int i;
18 unsigned int num_pages = map->size / page_size;
20 /* Check mapping is writable, or we'll segv.
21 * TODO: Perhaps we should do that, and trap it, mark it writable,
22 * then reprotect after we dirtied it ? */
23 if (!(map->prot & PROT_WRITE))
24 return;
26 switch (rand() % 6) {
27 case 0:
28 /* Just fault in one page. */
29 p[rand() % map->size] = rand();
30 break;
32 case 1:
33 /* fault in the whole mapping. */
34 for (i = 0; i < map->size; i += page_size)
35 p[i] = rand();
36 break;
38 case 2:
39 /* every other page. */
40 for (i = 0; i < map->size; i += (page_size * 2))
41 p[i] = rand();
42 break;
44 case 3:
45 /* whole mapping in reverse */
46 for (i = (map->size - page_size); i > 0; i -= page_size)
47 p[i] = rand();
48 break;
50 case 4:
51 /* fault in a random set of map->size pages. (some may be faulted >once) */
52 for (i = 0; i < num_pages; i++)
53 p[(rand() % (num_pages + 1)) * page_size] = rand();
54 break;
56 case 5:
57 /* fault in the last page in a mapping
58 * Fill it with ascii, in the hope we do something like
59 * a strlen and go off the end. */
60 memset((void *) p + (map->size - page_size), 'A', page_size);
61 break;