summaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/riscv/mm/mmap_test.h
blob: 2e0db9c5be6c334f9ed7d0187fae6ed6de950745 (plain)
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
/* SPDX-License-Identifier: GPL-2.0-only */
#ifndef _TESTCASES_MMAP_TEST_H
#define _TESTCASES_MMAP_TEST_H
#include <sys/mman.h>
#include <sys/resource.h>
#include <stddef.h>

#define TOP_DOWN 0
#define BOTTOM_UP 1

struct addresses {
	int *no_hint;
	int *on_37_addr;
	int *on_38_addr;
	int *on_46_addr;
	int *on_47_addr;
	int *on_55_addr;
	int *on_56_addr;
};

// Only works on 64 bit
#if __riscv_xlen == 64
static inline void do_mmaps(struct addresses *mmap_addresses)
{
	/*
	 * Place all of the hint addresses on the boundaries of mmap
	 * sv39, sv48, sv57
	 * User addresses end at 1<<38, 1<<47, 1<<56 respectively
	 */
	void *on_37_bits = (void *)(1UL << 37);
	void *on_38_bits = (void *)(1UL << 38);
	void *on_46_bits = (void *)(1UL << 46);
	void *on_47_bits = (void *)(1UL << 47);
	void *on_55_bits = (void *)(1UL << 55);
	void *on_56_bits = (void *)(1UL << 56);

	int prot = PROT_READ | PROT_WRITE;
	int flags = MAP_PRIVATE | MAP_ANONYMOUS;

	mmap_addresses->no_hint =
		mmap(NULL, 5 * sizeof(int), prot, flags, 0, 0);
	mmap_addresses->on_37_addr =
		mmap(on_37_bits, 5 * sizeof(int), prot, flags, 0, 0);
	mmap_addresses->on_38_addr =
		mmap(on_38_bits, 5 * sizeof(int), prot, flags, 0, 0);
	mmap_addresses->on_46_addr =
		mmap(on_46_bits, 5 * sizeof(int), prot, flags, 0, 0);
	mmap_addresses->on_47_addr =
		mmap(on_47_bits, 5 * sizeof(int), prot, flags, 0, 0);
	mmap_addresses->on_55_addr =
		mmap(on_55_bits, 5 * sizeof(int), prot, flags, 0, 0);
	mmap_addresses->on_56_addr =
		mmap(on_56_bits, 5 * sizeof(int), prot, flags, 0, 0);
}
#endif /* __riscv_xlen == 64 */

static inline int memory_layout(void)
{
	int prot = PROT_READ | PROT_WRITE;
	int flags = MAP_PRIVATE | MAP_ANONYMOUS;

	void *value1 = mmap(NULL, sizeof(int), prot, flags, 0, 0);
	void *value2 = mmap(NULL, sizeof(int), prot, flags, 0, 0);

	return value2 > value1;
}
#endif /* _TESTCASES_MMAP_TEST_H */