xref: /freebsd/sys/x86/linux/linux_vdso_getcpu_x86.inc (revision 031beb4e239bfce798af17f5fe8dba8bcaf13d99)
1/*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2021 The FreeBSD Foundation
5 * Copyright (c) 2022 Dmitry Chagin <dchagin@FreeBSD.org>
6 *
7 * Portions of this software were developed by Konstantin Belousov
8 * under sponsorship from the FreeBSD Foundation.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <x86/linux/linux_x86.h>
33
34static int
35__vdso_getcpu_rdpid(void)
36{
37	register_t res;
38
39	__asm("rdpid	%0" : "=r" (res));
40	return ((int)res);
41}
42
43static int
44__vdso_getcpu_rdtscp(void)
45{
46	int res;
47
48	__asm("rdtscp" : "=c" (res) : : "eax", "edx");
49	return (res);
50}
51
52static int
53__vdso_getcpu_try(void)
54{
55	int res;
56
57	switch (kern_cpu_selector) {
58	case LINUX_VDSO_CPU_RDTSCP:
59		res = __vdso_getcpu_rdtscp();
60		break;
61	case LINUX_VDSO_CPU_RDPID:
62		res = __vdso_getcpu_rdpid();
63		break;
64	default:
65		res = -1;
66	}
67	return (res);
68}
69