xref: /freebsd/sys/i386/include/pcpu_aux.h (revision ab92c99aa56fdec4a06f9af5f30f87ef08fbeb0e)
1a2a0f906SKonstantin Belousov /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3a2a0f906SKonstantin Belousov  *
4a2a0f906SKonstantin Belousov  * Copyright (c) 2019 The FreeBSD Foundation
5a2a0f906SKonstantin Belousov  *
6a2a0f906SKonstantin Belousov  * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
7a2a0f906SKonstantin Belousov  * under sponsorship from the FreeBSD Foundation.
8a2a0f906SKonstantin Belousov  *
9a2a0f906SKonstantin Belousov  * Redistribution and use in source and binary forms, with or without
10a2a0f906SKonstantin Belousov  * modification, are permitted provided that the following conditions
11a2a0f906SKonstantin Belousov  * are met:
12a2a0f906SKonstantin Belousov  * 1. Redistributions of source code must retain the above copyright
13a2a0f906SKonstantin Belousov  *    notice, this list of conditions and the following disclaimer.
14a2a0f906SKonstantin Belousov  * 2. Redistributions in binary form must reproduce the above copyright
15a2a0f906SKonstantin Belousov  *    notice, this list of conditions and the following disclaimer in the
16a2a0f906SKonstantin Belousov  *    documentation and/or other materials provided with the distribution.
17a2a0f906SKonstantin Belousov  *
18a2a0f906SKonstantin Belousov  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19a2a0f906SKonstantin Belousov  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20a2a0f906SKonstantin Belousov  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21a2a0f906SKonstantin Belousov  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22a2a0f906SKonstantin Belousov  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23a2a0f906SKonstantin Belousov  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24a2a0f906SKonstantin Belousov  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25a2a0f906SKonstantin Belousov  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26a2a0f906SKonstantin Belousov  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27a2a0f906SKonstantin Belousov  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28a2a0f906SKonstantin Belousov  * SUCH DAMAGE.
29a2a0f906SKonstantin Belousov  */
30a2a0f906SKonstantin Belousov 
31a2a0f906SKonstantin Belousov #ifndef _MACHINE_PCPU_AUX_H_
32a2a0f906SKonstantin Belousov #define	_MACHINE_PCPU_AUX_H_
33a2a0f906SKonstantin Belousov 
34a2a0f906SKonstantin Belousov #ifndef _KERNEL
35a2a0f906SKonstantin Belousov #error "Not for userspace"
36a2a0f906SKonstantin Belousov #endif
37a2a0f906SKonstantin Belousov 
38a2a0f906SKonstantin Belousov #ifndef _SYS_PCPU_H_
39a2a0f906SKonstantin Belousov #error "Do not include machine/pcpu_aux.h directly"
40a2a0f906SKonstantin Belousov #endif
41a2a0f906SKonstantin Belousov 
42a2a0f906SKonstantin Belousov /* Required for counters(9) to work on x86. */
43a2a0f906SKonstantin Belousov _Static_assert(sizeof(struct pcpu) == UMA_PCPU_ALLOC_SIZE, "fix pcpu size");
44a2a0f906SKonstantin Belousov 
45a2a0f906SKonstantin Belousov extern struct pcpu __pcpu[];
46a2a0f906SKonstantin Belousov 
47a2a0f906SKonstantin Belousov static __inline __pure2 struct thread *
48a2a0f906SKonstantin Belousov __curthread(void)
49a2a0f906SKonstantin Belousov {
50a2a0f906SKonstantin Belousov 	struct thread *td;
51a2a0f906SKonstantin Belousov 
52*ab92c99aSRyan Libby 	__asm("movl %%fs:%c1,%0" : "=r" (td)
53*ab92c99aSRyan Libby 	    : "i" (offsetof(struct pcpu, pc_curthread)));
54a2a0f906SKonstantin Belousov 	return (td);
55a2a0f906SKonstantin Belousov }
56a2a0f906SKonstantin Belousov #define	curthread		(__curthread())
57a2a0f906SKonstantin Belousov 
58a2a0f906SKonstantin Belousov static __inline __pure2 struct pcb *
59a2a0f906SKonstantin Belousov __curpcb(void)
60a2a0f906SKonstantin Belousov {
61a2a0f906SKonstantin Belousov 	struct pcb *pcb;
62a2a0f906SKonstantin Belousov 
63*ab92c99aSRyan Libby 	__asm("movl %%fs:%c1,%0" : "=r" (pcb)
64*ab92c99aSRyan Libby 	    : "i" (offsetof(struct pcpu, pc_curpcb)));
65a2a0f906SKonstantin Belousov 	return (pcb);
66a2a0f906SKonstantin Belousov }
67a2a0f906SKonstantin Belousov #define	curpcb		(__curpcb())
68a2a0f906SKonstantin Belousov 
69a2a0f906SKonstantin Belousov #endif	/* _MACHINE_PCPU_AUX_H_ */
70