-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap_unmap_map.c
More file actions
71 lines (57 loc) · 2.51 KB
/
map_unmap_map.c
File metadata and controls
71 lines (57 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <stdio.h>
#include <stdint.h> // For uint64_t
#include <fcntl.h> // For O_RDONLY in get_physical_addr fn
#include <unistd.h> // For pread in get_physical_addr fn
#include <sys/mman.h> // For mmap
#define PAGE_COUNT 10
#define PAGE_SIZE 4096
// get_physical_addr function has been taken from
// https://github.com/IAIK/flipfloyd/blob/master/waylaying/check.c
static uint64_t get_physical_addr(uint64_t virtual_addr) {
static int g_pagemap_fd = -1;
uint64_t value;
// open the pagemap
if(g_pagemap_fd == -1) {
g_pagemap_fd = open("/proc/self/pagemap", O_RDONLY);
}
if(g_pagemap_fd == -1) return 0;
// read physical address
off_t offset = (virtual_addr / 4096) * sizeof(value);
int got = pread(g_pagemap_fd, &value, sizeof(value), offset);
if(got != 8) return 0;
// Check the "page present" flag.
if(!(value & (1ULL << 63))) return 0;
// return physical address
uint64_t frame_num = value & ((1ULL << 55) - 1);
return (frame_num * 4096) | (virtual_addr & (4095));
}
void main() {
uint8_t * buffer;
printf("____________________________________________________\n");
printf("\n MAP\n");
printf("____________________________________________________\n");
buffer = mmap(NULL, PAGE_COUNT * PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_POPULATE | MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); // Mapping all pages at once.
for (int i = 0; i < PAGE_COUNT; i++) {
printf("Virtual : %p \t", &buffer[i*PAGE_SIZE]);
printf("Physical : %lx \n", get_physical_addr((uint64_t)&buffer[i*PAGE_SIZE]));
}
printf("____________________________________________________\n");
printf("\n MUNMAP\n");
printf("____________________________________________________\n");
//munmap(&buffer[0], PAGE_SIZE*PAGE_COUNT); // Unmap all pages at once.
for (int i = 0; i < PAGE_COUNT; i++) {
munmap(&buffer[i*PAGE_SIZE], PAGE_SIZE); // Unmapping one page at a time.
}
for (int i = 0; i < PAGE_COUNT; i++) {
printf("Virtual : %p \t", &buffer[i*PAGE_SIZE]);
printf("Physical : %lx \n", get_physical_addr((uint64_t)&buffer[i*PAGE_SIZE]));
}
printf("____________________________________________________\n");
printf("\n MAP\n");
printf("____________________________________________________\n");
buffer = mmap(NULL, PAGE_COUNT * PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_POPULATE | MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); // Mapping all pages at once.
for (int i = 0; i < PAGE_COUNT; i++) {
printf("Virtual : %p \t", &buffer[i*PAGE_SIZE]);
printf("Physical : %lx \n", get_physical_addr((uint64_t)&buffer[i*PAGE_SIZE]));
}
}