xref: /openbmc/linux/tools/testing/selftests/vDSO/vdso_test_getcpu.c (revision 7ae9fb1b7ecbb5d85d07857943f677fd1a559b18)
12e9a9725SMark Brown // SPDX-License-Identifier: GPL-2.0-only
22e9a9725SMark Brown /*
32e9a9725SMark Brown  * vdso_test_getcpu.c: Sample code to test parse_vdso.c and vDSO getcpu()
42e9a9725SMark Brown  *
52e9a9725SMark Brown  * Copyright (c) 2020 Arm Ltd
62e9a9725SMark Brown  */
72e9a9725SMark Brown 
82e9a9725SMark Brown #include <stdint.h>
92e9a9725SMark Brown #include <elf.h>
102e9a9725SMark Brown #include <stdio.h>
112e9a9725SMark Brown #include <sys/auxv.h>
122e9a9725SMark Brown #include <sys/time.h>
132e9a9725SMark Brown 
142e9a9725SMark Brown #include "../kselftest.h"
152e9a9725SMark Brown #include "parse_vdso.h"
162e9a9725SMark Brown 
17*d942f231SGuo Ren #if defined(__riscv)
18*d942f231SGuo Ren const char *version = "LINUX_4.15";
19*d942f231SGuo Ren #else
202e9a9725SMark Brown const char *version = "LINUX_2.6";
21*d942f231SGuo Ren #endif
222e9a9725SMark Brown const char *name = "__vdso_getcpu";
232e9a9725SMark Brown 
242e9a9725SMark Brown struct getcpu_cache;
252e9a9725SMark Brown typedef long (*getcpu_t)(unsigned int *, unsigned int *,
262e9a9725SMark Brown 			 struct getcpu_cache *);
272e9a9725SMark Brown 
main(int argc,char ** argv)282e9a9725SMark Brown int main(int argc, char **argv)
292e9a9725SMark Brown {
302e9a9725SMark Brown 	unsigned long sysinfo_ehdr;
312e9a9725SMark Brown 	unsigned int cpu, node;
322e9a9725SMark Brown 	getcpu_t get_cpu;
332e9a9725SMark Brown 	long ret;
342e9a9725SMark Brown 
352e9a9725SMark Brown 	sysinfo_ehdr = getauxval(AT_SYSINFO_EHDR);
362e9a9725SMark Brown 	if (!sysinfo_ehdr) {
372e9a9725SMark Brown 		printf("AT_SYSINFO_EHDR is not present!\n");
382e9a9725SMark Brown 		return KSFT_SKIP;
392e9a9725SMark Brown 	}
402e9a9725SMark Brown 
412e9a9725SMark Brown 	vdso_init_from_sysinfo_ehdr(getauxval(AT_SYSINFO_EHDR));
422e9a9725SMark Brown 
432e9a9725SMark Brown 	get_cpu = (getcpu_t)vdso_sym(version, name);
442e9a9725SMark Brown 	if (!get_cpu) {
452e9a9725SMark Brown 		printf("Could not find %s\n", name);
462e9a9725SMark Brown 		return KSFT_SKIP;
472e9a9725SMark Brown 	}
482e9a9725SMark Brown 
492e9a9725SMark Brown 	ret = get_cpu(&cpu, &node, 0);
502e9a9725SMark Brown 	if (ret == 0) {
512e9a9725SMark Brown 		printf("Running on CPU %u node %u\n", cpu, node);
522e9a9725SMark Brown 	} else {
532e9a9725SMark Brown 		printf("%s failed\n", name);
542e9a9725SMark Brown 		return KSFT_FAIL;
552e9a9725SMark Brown 	}
562e9a9725SMark Brown 
572e9a9725SMark Brown 	return 0;
582e9a9725SMark Brown }
59