xref: /linux/tools/testing/selftests/vDSO/vdso_test_getcpu.c (revision 69050f8d6d075dc01af7a5f2f550a8067510366f)
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 typedef long (*getcpu_t)(unsigned int *, unsigned int *, void *);
20 
21 int main(int argc, char **argv)
22 {
23 	const char *version = versions[VDSO_VERSION];
24 	const char **name = (const char **)&names[VDSO_NAMES];
25 	unsigned long sysinfo_ehdr;
26 	unsigned int cpu, node;
27 	getcpu_t get_cpu;
28 	long ret;
29 
30 	sysinfo_ehdr = getauxval(AT_SYSINFO_EHDR);
31 	if (!sysinfo_ehdr) {
32 		printf("AT_SYSINFO_EHDR is not present!\n");
33 		return KSFT_SKIP;
34 	}
35 
36 	vdso_init_from_sysinfo_ehdr(getauxval(AT_SYSINFO_EHDR));
37 
38 	get_cpu = (getcpu_t)vdso_sym(version, name[4]);
39 	if (!get_cpu) {
40 		printf("Could not find %s\n", name[4]);
41 		return KSFT_SKIP;
42 	}
43 
44 	ret = VDSO_CALL(get_cpu, 3, &cpu, &node, 0);
45 	if (ret == 0) {
46 		printf("Running on CPU %u node %u\n", cpu, node);
47 	} else {
48 		printf("%s failed\n", name[4]);
49 		return KSFT_FAIL;
50 	}
51 
52 	return 0;
53 }
54