aboutsummaryrefslogtreecommitdiffstats
path: root/mca-recover.c
blob: e4276a1c3593c0758c0279f9a47ec8e916e776c3 (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
/*
 * Copyright (C) 2013 Intel Corporation
 * Authors: Tony Luck
 *
 * This software may be redistributed and/or modified under the terms of
 * the GNU General Public License ("GPL") version 2 only as published by the
 * Free Software Foundation.
 */

/*
 * Set up to get zapped by a machine check (injected elsewhere)
 * recovery function reports physical address of new page - so
 * we can inject to that and repeat over and over.
 */

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

static int pagesize;

/*
 * get information about address from /proc/{pid}/pagemap
 * Assumes target address is mapped as 4K (not hugepage)
 */
unsigned long long vtop(unsigned long long addr)
{
	unsigned long long pinfo;
	long offset = addr / pagesize * (sizeof pinfo);
	int fd;
	char	pagemapname[64];

	sprintf(pagemapname, "/proc/%d/pagemap", getpid());
	fd = open(pagemapname, O_RDONLY);
	if (fd == -1) {
		perror(pagemapname);
		exit(1);
	}
	if (pread(fd, &pinfo, sizeof pinfo, offset) != sizeof pinfo) {
		perror(pagemapname);
		exit(1);
	}
	close(fd);
	if ((pinfo & (1ull << 63)) == 0) {
		printf("page not present\n");
		exit(1);
	}
	return ((pinfo & 0x007fffffffffffffull) << 12) + (addr & (pagesize - 1));
}

int checksum(unsigned char *addr)
{
	int	i, sum;

	sum = 0;
	for (i = 0; i < pagesize; i++)
		sum += addr[i];
	return sum;
}

/*
 * Older glibc headers don't have the si_addr_lsb field in the siginfo_t
 * structure ... ugly hack to get it
 */
struct morebits {
	void	*addr;
	short	lsb;
};

char	*buf;
unsigned long long	phys;

/*
 * "Recover" from the error by allocating a new page and mapping
 * it at the same virtual address as the page we lost. Fill with
 * the same (trivial) contents.
 */
void recover(int sig, siginfo_t *si, void *v)
{
	struct morebits *m = (struct morebits *)&si->si_addr;
	char	*newbuf;

	printf("recover: sig=%d si=%p v=%p\n", sig, si, v);
	printf("Platform memory error at 0x%p\n", si->si_addr);
	printf("addr = %p lsb=%d\n", m->addr, m->lsb);

	newbuf = mmap(buf, pagesize, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
	if (newbuf == MAP_FAILED) {
		fprintf(stderr, "Can't get a single page of memory!\n");
		exit(1);
	}
	if (newbuf != buf) {
		fprintf(stderr, "Could not allocate at original virtual address\n");
		exit(1);
	}
	buf = newbuf;
	memset(buf, '*', pagesize);
	phys = vtop((unsigned long long)buf);

	printf("Recovery allocated new page at physical %llx\n", phys);
}

struct sigaction recover_act = {
	.sa_sigaction = recover,
	.sa_flags = SA_SIGINFO,
};

int main(int argc, char **argv)
{
	int	i, sum, rightsum;
	int	iflag = 0;
	int	tflag = 0;
	time_t	now;

	pagesize = getpagesize();

	buf = mmap(NULL, pagesize, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);

	if (buf == MAP_FAILED) {
		fprintf(stderr, "Can't get a single page of memory!\n");
		return 1;
	}
	memset(buf, '*', pagesize);
	rightsum = checksum(buf);
	phys = vtop((unsigned long long)buf);

	printf("vtop(%llx) = %llx\n", (unsigned long long)buf, phys);
	fflush(stdout);

	sigaction(SIGBUS, &recover_act, NULL);

	while (1) {
		sum = checksum(buf);
		if (sum != rightsum) {
			printf("Ooops.  Saw bad checksum %d\n", sum);
			break;
		}
	}

	return 1;
}