xref: /linux/tools/testing/selftests/vDSO/vdso_test_getcpu.c (revision 0526b56cbc3c489642bd6a5fe4b718dea7ef0ee8)
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 
17 #if defined(__riscv)
18 const char *version = "LINUX_4.15";
19 #else
20 const char *version = "LINUX_2.6";
21 #endif
22 const char *name = "__vdso_getcpu";
23 
24 struct getcpu_cache;
25 typedef long (*getcpu_t)(unsigned int *, unsigned int *,
26 			 struct getcpu_cache *);
27 
28 int main(int argc, char **argv)
29 {
30 	unsigned long sysinfo_ehdr;
31 	unsigned int cpu, node;
32 	getcpu_t get_cpu;
33 	long ret;
34 
35 	sysinfo_ehdr = getauxval(AT_SYSINFO_EHDR);
36 	if (!sysinfo_ehdr) {
37 		printf("AT_SYSINFO_EHDR is not present!\n");
38 		return KSFT_SKIP;
39 	}
40 
41 	vdso_init_from_sysinfo_ehdr(getauxval(AT_SYSINFO_EHDR));
42 
43 	get_cpu = (getcpu_t)vdso_sym(version, name);
44 	if (!get_cpu) {
45 		printf("Could not find %s\n", name);
46 		return KSFT_SKIP;
47 	}
48 
49 	ret = get_cpu(&cpu, &node, 0);
50 	if (ret == 0) {
51 		printf("Running on CPU %u node %u\n", cpu, node);
52 	} else {
53 		printf("%s failed\n", name);
54 		return KSFT_FAIL;
55 	}
56 
57 	return 0;
58 }
59