xref: /freebsd/sys/riscv/include/pcpu.h (revision 2ff63af9b88c7413b7d71715b5532625752a248e)
18d7e7a98SRuslan Bukin /*-
28d7e7a98SRuslan Bukin  * Copyright (c) 1999 Luoqi Chen <luoqi@freebsd.org>
317696c12SRuslan Bukin  * Copyright (c) 2015-2016 Ruslan Bukin <br@bsdpad.com>
48d7e7a98SRuslan Bukin  * All rights reserved.
58d7e7a98SRuslan Bukin  *
617696c12SRuslan Bukin  * Portions of this software were developed by SRI International and the
717696c12SRuslan Bukin  * University of Cambridge Computer Laboratory under DARPA/AFRL contract
817696c12SRuslan Bukin  * FA8750-10-C-0237 ("CTSRD"), as part of the DARPA CRASH research programme.
917696c12SRuslan Bukin  *
1017696c12SRuslan Bukin  * Portions of this software were developed by the University of Cambridge
1117696c12SRuslan Bukin  * Computer Laboratory as part of the CTSRD Project, with support from the
1217696c12SRuslan Bukin  * UK Higher Education Innovation Fund (HEIF).
1317696c12SRuslan Bukin  *
148d7e7a98SRuslan Bukin  * Redistribution and use in source and binary forms, with or without
158d7e7a98SRuslan Bukin  * modification, are permitted provided that the following conditions
168d7e7a98SRuslan Bukin  * are met:
178d7e7a98SRuslan Bukin  * 1. Redistributions of source code must retain the above copyright
188d7e7a98SRuslan Bukin  *    notice, this list of conditions and the following disclaimer.
198d7e7a98SRuslan Bukin  * 2. Redistributions in binary form must reproduce the above copyright
208d7e7a98SRuslan Bukin  *    notice, this list of conditions and the following disclaimer in the
218d7e7a98SRuslan Bukin  *    documentation and/or other materials provided with the distribution.
228d7e7a98SRuslan Bukin  *
238d7e7a98SRuslan Bukin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
248d7e7a98SRuslan Bukin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
258d7e7a98SRuslan Bukin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
268d7e7a98SRuslan Bukin  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
278d7e7a98SRuslan Bukin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
288d7e7a98SRuslan Bukin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
298d7e7a98SRuslan Bukin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
308d7e7a98SRuslan Bukin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
318d7e7a98SRuslan Bukin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
328d7e7a98SRuslan Bukin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
338d7e7a98SRuslan Bukin  * SUCH DAMAGE.
348d7e7a98SRuslan Bukin  *
358d7e7a98SRuslan Bukin  *	from: FreeBSD: src/sys/i386/include/globaldata.h,v 1.27 2001/04/27
368d7e7a98SRuslan Bukin  */
378d7e7a98SRuslan Bukin 
388d7e7a98SRuslan Bukin #ifndef	_MACHINE_PCPU_H_
398d7e7a98SRuslan Bukin #define	_MACHINE_PCPU_H_
408d7e7a98SRuslan Bukin 
418d7e7a98SRuslan Bukin #include <machine/cpu.h>
428d7e7a98SRuslan Bukin #include <machine/cpufunc.h>
438d7e7a98SRuslan Bukin 
44*4fffc56cSMitchell Horne /* Keep in sync with db_show_mdpcpu() */
458d7e7a98SRuslan Bukin #define	PCPU_MD_FIELDS							\
4635c91b0cSMark Johnston 	struct pmap *pc_curpmap;	/* Currently active pmap */	\
4717696c12SRuslan Bukin 	uint32_t pc_pending_ipis;	/* IPIs pending to this CPU */	\
48b803d0b7SRuslan Bukin 	uint32_t pc_hart;		/* Hart ID */			\
4990699f2aSJohn Baldwin 	char __pad[56]			/* Pad to factor of PAGE_SIZE */
508d7e7a98SRuslan Bukin 
518d7e7a98SRuslan Bukin #ifdef _KERNEL
528d7e7a98SRuslan Bukin 
538d7e7a98SRuslan Bukin struct pcb;
548d7e7a98SRuslan Bukin struct pcpu;
558d7e7a98SRuslan Bukin 
568d7e7a98SRuslan Bukin static inline struct pcpu *
get_pcpu(void)578d7e7a98SRuslan Bukin get_pcpu(void)
588d7e7a98SRuslan Bukin {
594d50647dSRuslan Bukin 	struct pcpu *pcpu;
608d7e7a98SRuslan Bukin 
616ae48dd8SMitchell Horne 	__asm __volatile("mv %0, tp" : "=&r"(pcpu));
624d50647dSRuslan Bukin 
634d50647dSRuslan Bukin 	return (pcpu);
648d7e7a98SRuslan Bukin }
658d7e7a98SRuslan Bukin 
668d7e7a98SRuslan Bukin static inline struct thread *
get_curthread(void)678d7e7a98SRuslan Bukin get_curthread(void)
688d7e7a98SRuslan Bukin {
698d7e7a98SRuslan Bukin 	struct thread *td;
708d7e7a98SRuslan Bukin 
716ae48dd8SMitchell Horne 	__asm __volatile("ld %0, 0(tp)" : "=&r"(td));
728d7e7a98SRuslan Bukin 
738d7e7a98SRuslan Bukin 	return (td);
748d7e7a98SRuslan Bukin }
758d7e7a98SRuslan Bukin 
768d7e7a98SRuslan Bukin #define	curthread get_curthread()
778d7e7a98SRuslan Bukin 
788d7e7a98SRuslan Bukin #define	PCPU_GET(member)	(get_pcpu()->pc_ ## member)
798d7e7a98SRuslan Bukin #define	PCPU_ADD(member, value)	(get_pcpu()->pc_ ## member += (value))
808d7e7a98SRuslan Bukin #define	PCPU_PTR(member)	(&get_pcpu()->pc_ ## member)
818d7e7a98SRuslan Bukin #define	PCPU_SET(member,value)	(get_pcpu()->pc_ ## member = (value))
828d7e7a98SRuslan Bukin 
838d7e7a98SRuslan Bukin #endif	/* _KERNEL */
848d7e7a98SRuslan Bukin 
858d7e7a98SRuslan Bukin #endif	/* !_MACHINE_PCPU_H_ */
86