xref: /dflybsd-src/sys/platform/pc64/include/thread.h (revision 3a6a85bc47bb0942419001ac35772cc2accc438c)
139923942SSimon Schubert /*
2*c8fe38aeSMatthew Dillon  * Copyright (c) 2003 Matt Dillon <dillon@backplane.com>
3*c8fe38aeSMatthew Dillon  * Copyright (c) 2008 The DragonFly Project.
4*c8fe38aeSMatthew Dillon  * All rights reserved.
539923942SSimon Schubert  *
639923942SSimon Schubert  * Redistribution and use in source and binary forms, with or without
739923942SSimon Schubert  * modification, are permitted provided that the following conditions
839923942SSimon Schubert  * are met:
939923942SSimon Schubert  * 1. Redistributions of source code must retain the above copyright
1039923942SSimon Schubert  *    notice, this list of conditions and the following disclaimer.
1139923942SSimon Schubert  * 2. Redistributions in binary form must reproduce the above copyright
1239923942SSimon Schubert  *    notice, this list of conditions and the following disclaimer in the
1339923942SSimon Schubert  *    documentation and/or other materials provided with the distribution.
1439923942SSimon Schubert  *
1539923942SSimon Schubert  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1639923942SSimon Schubert  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1739923942SSimon Schubert  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1839923942SSimon Schubert  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1939923942SSimon Schubert  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2039923942SSimon Schubert  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2139923942SSimon Schubert  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2239923942SSimon Schubert  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2339923942SSimon Schubert  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2439923942SSimon Schubert  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2539923942SSimon Schubert  * SUCH DAMAGE.
2639923942SSimon Schubert  *
2739923942SSimon Schubert  *	Machine independant code should not directly include this file.
2839923942SSimon Schubert  */
2939923942SSimon Schubert 
3039923942SSimon Schubert #ifndef	_MACHINE_THREAD_H_
3139923942SSimon Schubert #define	_MACHINE_THREAD_H_
3239923942SSimon Schubert 
33d7f50089SYonghong Yan #include <machine/segments.h>
34d7f50089SYonghong Yan 
3539923942SSimon Schubert struct md_thread {
3639923942SSimon Schubert     unsigned int	mtd_cpl;
37d7f50089SYonghong Yan     union savefpu	*mtd_savefpu;
38d7f50089SYonghong Yan     struct savetls	mtd_savetls;
3939923942SSimon Schubert };
4039923942SSimon Schubert 
4139923942SSimon Schubert #ifdef _KERNEL
4239923942SSimon Schubert 
4339923942SSimon Schubert #define td_cpl		td_mach.mtd_cpl
44d7f50089SYonghong Yan #define td_tls		td_mach.mtd_savetls
45d7f50089SYonghong Yan #define td_savefpu      td_mach.mtd_savefpu
4639923942SSimon Schubert 
4739923942SSimon Schubert /*
4839923942SSimon Schubert  * mycpu() retrieves the base of the current cpu's globaldata structure.
4939923942SSimon Schubert  * Note that it is *NOT* volatile, meaning that the value may be cached by
5039923942SSimon Schubert  * GCC.  We have to force a dummy memory reference so gcc does not cache
5139923942SSimon Schubert  * the gd pointer across a procedure call (which might block and cause us
5239923942SSimon Schubert  * to wakeup on a different cpu).
5339923942SSimon Schubert  *
5439923942SSimon Schubert  * Also note that in DragonFly a thread can be preempted, but it cannot
5539923942SSimon Schubert  * move to another cpu preemptively so the 'gd' pointer is good until you
5639923942SSimon Schubert  * block.
5739923942SSimon Schubert  */
5839923942SSimon Schubert 
5939923942SSimon Schubert struct globaldata;
6039923942SSimon Schubert 
6139923942SSimon Schubert extern int __mycpu__dummy;
6239923942SSimon Schubert 
6339923942SSimon Schubert static __inline
6439923942SSimon Schubert struct globaldata *
_get_mycpu(void)6539923942SSimon Schubert _get_mycpu(void)
6639923942SSimon Schubert {
6739923942SSimon Schubert     struct globaldata *gd;
6839923942SSimon Schubert 
69*c8fe38aeSMatthew Dillon     __asm ("movq %%gs:globaldata,%0" : "=r" (gd) : "m"(__mycpu__dummy));
7039923942SSimon Schubert     return(gd);
7139923942SSimon Schubert }
7239923942SSimon Schubert 
7339923942SSimon Schubert #define mycpu	_get_mycpu()
74d7f50089SYonghong Yan #define mycpuid (_get_mycpu()->gd_cpuid)
75d7f50089SYonghong Yan 
7639923942SSimon Schubert /*
7739923942SSimon Schubert  * note: curthread is never NULL, but curproc can be.  Also note that
7839923942SSimon Schubert  * in DragonFly, the current pcb is stored in the thread structure.
7939923942SSimon Schubert  */
8039923942SSimon Schubert #define curthread	mycpu->gd_curthread
8139923942SSimon Schubert #define	curproc		curthread->td_proc
8239923942SSimon Schubert 
8339923942SSimon Schubert #endif	/* _KERNEL */
8439923942SSimon Schubert 
8539923942SSimon Schubert #endif	/* !_MACHINE_THREAD_H_ */
86