summaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/membarrier/membarrier_test.c
blob: 3c9f2179acd83fcc63e4a08977f31349b8801062 (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
#define _GNU_SOURCE
#define __EXPORTED_HEADERS__

#include <linux/membarrier.h>
#include <asm-generic/unistd.h>
#include <sys/syscall.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>

#include "../kselftest.h"

static int sys_membarrier(int cmd, int flags)
{
	return syscall(__NR_membarrier, cmd, flags);
}

static void test_membarrier_fail(void)
{
	int cmd = -1, flags = 0;

	if (sys_membarrier(cmd, flags) != -1) {
		printf("membarrier: Should fail but passed\n");
		ksft_exit_fail();
	}
}

static void test_membarrier_success(void)
{
	int flags = 0;

	if (sys_membarrier(MEMBARRIER_CMD_SHARED, flags) != 0) {
		printf("membarrier: Executing MEMBARRIER failed, %s\n",
				strerror(errno));
		ksft_exit_fail();
	}

	printf("membarrier: MEMBARRIER_CMD_SHARED success\n");
}

static void test_membarrier(void)
{
	test_membarrier_fail();
	test_membarrier_success();
}

static int test_membarrier_exists(void)
{
	int flags = 0;

	if (sys_membarrier(MEMBARRIER_CMD_QUERY, flags))
		return 0;

	return 1;
}

int main(int argc, char **argv)
{
	printf("membarrier: MEMBARRIER_CMD_QUERY ");
	if (test_membarrier_exists()) {
		printf("syscall implemented\n");
		test_membarrier();
	} else {
		printf("syscall not implemented!\n");
		return ksft_exit_fail();
	}

	printf("membarrier: tests done!\n");

	return ksft_exit_pass();
}