xref: /linux/tools/testing/selftests/vDSO/vdso_test_getcpu.c (revision f858cc9eed5b05cbe38d7ffd2787c21e3718eb7d)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * vdso_test_getcpu.c: Sample code to test parse_vdso.c and vDSO getcpu()
4  *
5  * Copyright (c) 2020 Arm Ltd
6  */
7 
8 #include <stdint.h>
9 #include <elf.h>
10 #include <stdio.h>
11 #include <sys/auxv.h>
12 #include <sys/time.h>
13 
14 #include "../kselftest.h"
15 #include "parse_vdso.h"
16 #include "vdso_config.h"
17 #include "vdso_call.h"
18 
19 struct getcpu_cache;
20 typedef long (*getcpu_t)(unsigned int *, unsigned int *,
21 			 struct getcpu_cache *);
22 
23 int main(int argc, char **argv)
24 {
25 	const char *version = versions[VDSO_VERSION];
26 	const char **name = (const char **)&names[VDSO_NAMES];
27 	unsigned long sysinfo_ehdr;
28 	unsigned int cpu, node;
29 	getcpu_t get_cpu;
30 	long ret;
31 
32 	sysinfo_ehdr = getauxval(AT_SYSINFO_EHDR);
33 	if (!sysinfo_ehdr) {
34 		printf("AT_SYSINFO_EHDR is not present!\n");
35 		return KSFT_SKIP;
36 	}
37 
38 	vdso_init_from_sysinfo_ehdr(getauxval(AT_SYSINFO_EHDR));
39 
40 	get_cpu = (getcpu_t)vdso_sym(version, name[4]);
41 	if (!get_cpu) {
42 		printf("Could not find %s\n", name[4]);
43 		return KSFT_SKIP;
44 	}
45 
46 	ret = VDSO_CALL(get_cpu, 3, &cpu, &node, 0);
47 	if (ret == 0) {
48 		printf("Running on CPU %u node %u\n", cpu, node);
49 	} else {
50 		printf("%s failed\n", name[4]);
51 		return KSFT_FAIL;
52 	}
53 
54 	return 0;
55 }
56