-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.c
69 lines (57 loc) · 2.23 KB
/
map.c
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
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define SEG_ADDR 0x43c00000
#define SEG_SIZE 0x10000
int main(int argc, char **argv)
{
// use the dev/mem dev driver that's build into linux
int fd = open("/dev/mem", O_RDWR);
if(fd < 0){
perror("Could not open /dev/mem: \n");
exit(0);
}
/*
void *mmap(void *addr, size_t length, int prot, int flags,
int fd, off_t offset);
usage:
mmap creates a new mapping in the virtual addr space of the calling proc
the contents of the file mapping are initialized using length bytes starting
at offset in the file referred to by fd.
params:
void *addr - starting addr for the new mapping
if addr == NULL:
kernel chooses page-aligned addr at which to create the mapping
the most portable method of creating a new mapping
if addr != NULL:
kernel takes addr as a hint about where to place the mapping;
kernel will pick a nearby page boundary (always above or equal
to the value specified by /proc/sys/vm/mmap_min_addr) and attempt
to create a mapping there
size_t length - the length of the mapping (must be > 0)
int prot - the desired memory protection of the mapping
must NOT conflict with the open mode of the file
PROT_READ: pages may be read
PROT_WRITE: pages may be written
int flags - determines whether "updates to the mapping" are visible to other
processes mapping the same region, and whether updates are
carried through to the underlying file.
MAP_SHARED: updates to the mapping are visible to other proceses
mapping the same region.
fd -
offset -
offset must be a multiple
of the page size as returned by sysconf(_SC_PAGE_SIZE)
return:
the address of the new mapping
*/
uint32_t *base = mmap(0, getpagesize(), PROT_READ | PROT_WRITE,
MAP_SHARED, fd, SEG_ADDR);
if(base == MAP_FAILED){
}
}