aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/tools/testing/selftests/x86/check_initial_reg_state.c
blob: 6aaed9b85bafb3f163bc259220cd80e7965f71d3 (plain) (blame)
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
/*
 * check_initial_reg_state.c - check that execve sets the correct state
 * Copyright (c) 2014-2016 Andrew Lutomirski
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 */

#define _GNU_SOURCE

#include <stdio.h>

unsigned long ax, bx, cx, dx, si, di, bp, sp, flags;
unsigned long r8, r9, r10, r11, r12, r13, r14, r15;

asm (
	".pushsection .text\n\t"
	".type real_start, @function\n\t"
	".global real_start\n\t"
	"real_start:\n\t"
#ifdef __x86_64__
	"mov %rax, ax\n\t"
	"mov %rbx, bx\n\t"
	"mov %rcx, cx\n\t"
	"mov %rdx, dx\n\t"
	"mov %rsi, si\n\t"
	"mov %rdi, di\n\t"
	"mov %rbp, bp\n\t"
	"mov %rsp, sp\n\t"
	"mov %r8, r8\n\t"
	"mov %r9, r9\n\t"
	"mov %r10, r10\n\t"
	"mov %r11, r11\n\t"
	"mov %r12, r12\n\t"
	"mov %r13, r13\n\t"
	"mov %r14, r14\n\t"
	"mov %r15, r15\n\t"
	"pushfq\n\t"
	"popq flags\n\t"
#else
	"mov %eax, ax\n\t"
	"mov %ebx, bx\n\t"
	"mov %ecx, cx\n\t"
	"mov %edx, dx\n\t"
	"mov %esi, si\n\t"
	"mov %edi, di\n\t"
	"mov %ebp, bp\n\t"
	"mov %esp, sp\n\t"
	"pushfl\n\t"
	"popl flags\n\t"
#endif
	"jmp _start\n\t"
	".size real_start, . - real_start\n\t"
	".popsection");

int main()
{
	int nerrs = 0;

	if (sp == 0) {
		printf("[FAIL]\tTest was built incorrectly\n");
		return 1;
	}

	if (ax || bx || cx || dx || si || di || bp
#ifdef __x86_64__
	    || r8 || r9 || r10 || r11 || r12 || r13 || r14 || r15
#endif
		) {
		printf("[FAIL]\tAll GPRs except SP should be 0\n");
#define SHOW(x) printf("\t" #x " = 0x%lx\n", x);
		SHOW(ax);
		SHOW(bx);
		SHOW(cx);
		SHOW(dx);
		SHOW(si);
		SHOW(di);
		SHOW(bp);
		SHOW(sp);
#ifdef __x86_64__
		SHOW(r8);
		SHOW(r9);
		SHOW(r10);
		SHOW(r11);
		SHOW(r12);
		SHOW(r13);
		SHOW(r14);
		SHOW(r15);
#endif
		nerrs++;
	} else {
		printf("[OK]\tAll GPRs except SP are 0\n");
	}

	if (flags != 0x202) {
		printf("[FAIL]\tFLAGS is 0x%lx, but it should be 0x202\n", flags);
		nerrs++;
	} else {
		printf("[OK]\tFLAGS is 0x202\n");
	}

	return nerrs ? 1 : 0;
}