summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/powerpc/primitives/load_unaligned_zeropad.c
blob: 1439c8c7ff380c5e37f9870069a04fb44d2044ee (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Userspace test harness for load_unaligned_zeropad. Creates two
 * pages and uses mprotect to prevent access to the second page and
 * a SEGV handler that walks the exception tables and runs the fixup
 * routine.
 *
 * The results are compared against a normal load that is that is
 * performed while access to the second page is enabled via mprotect.
 *
 * Copyright (C) 2014 Anton Blanchard <anton@au.ibm.com>, IBM
 */

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
#include <signal.h>
#include <unistd.h>
#include <sys/mman.h>

#define FIXUP_SECTION ".ex_fixup"

static inline unsigned long __fls(unsigned long x);

#include "word-at-a-time.h"

#include "utils.h"

static inline unsigned long __fls(unsigned long x)
{
	int lz;

	asm (PPC_CNTLZL "%0,%1" : "=r" (lz) : "r" (x));
	return sizeof(unsigned long) - 1 - lz;
}

static int page_size;
static char *mem_region;

static int protect_region(void)
{
	if (mprotect(mem_region + page_size, page_size, PROT_NONE)) {
		perror("mprotect");
		return 1;
	}

	return 0;
}

static int unprotect_region(void)
{
	if (mprotect(mem_region + page_size, page_size, PROT_READ|PROT_WRITE)) {
		perror("mprotect");
		return 1;
	}

	return 0;
}

extern char __start___ex_table[];
extern char __stop___ex_table[];

struct extbl_entry {
	int insn;
	int fixup;
};

static void segv_handler(int signr, siginfo_t *info, void *ptr)
{
	ucontext_t *uc = (ucontext_t *)ptr;
	unsigned long addr = (unsigned long)info->si_addr;
	unsigned long *ip = &UCONTEXT_NIA(uc);
	struct extbl_entry *entry = (struct extbl_entry *)__start___ex_table;

	while (entry < (struct extbl_entry *)__stop___ex_table) {
		unsigned long insn, fixup;

		insn  = (unsigned long)&entry->insn + entry->insn;
		fixup = (unsigned long)&entry->fixup + entry->fixup;

		if (insn == *ip) {
			*ip = fixup;
			return;
		}
	}

	printf("No exception table match for NIA %lx ADDR %lx\n", *ip, addr);
	abort();
}

static void setup_segv_handler(void)
{
	struct sigaction action;

	memset(&action, 0, sizeof(action));
	action.sa_sigaction = segv_handler;
	action.sa_flags = SA_SIGINFO;
	sigaction(SIGSEGV, &action, NULL);
}

static int do_one_test(char *p, int page_offset)
{
	unsigned long should;
	unsigned long got;

	FAIL_IF(unprotect_region());
	should = *(unsigned long *)p;
	FAIL_IF(protect_region());

	got = load_unaligned_zeropad(p);

	if (should != got) {
		printf("offset %u load_unaligned_zeropad returned 0x%lx, should be 0x%lx\n", page_offset, got, should);
		return 1;
	}

	return 0;
}

static int test_body(void)
{
	unsigned long i;

	page_size = getpagesize();
	mem_region = mmap(NULL, page_size * 2, PROT_READ|PROT_WRITE,
		MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);

	FAIL_IF(mem_region == MAP_FAILED);

	for (i = 0; i < page_size; i++)
		mem_region[i] = i;

	memset(mem_region+page_size, 0, page_size);

	setup_segv_handler();

	for (i = 0; i < page_size; i++)
		FAIL_IF(do_one_test(mem_region+i, i));

	return 0;
}

int main(void)
{
	return test_harness(test_body, "load_unaligned_zeropad");
}