2 * Copyright (c) 1999 Luoqi Chen <luoqi@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * $FreeBSD: src/sys/i386/include/globals.h,v 1.5.2.1 2000/05/16 06:58:10 dillon Exp $
27 * $DragonFly: src/sys/platform/pc32/include/Attic/globals.h,v 1.5 2003/06/18 18:29:58 dillon Exp $
30 #ifndef _MACHINE_GLOBALS_H_
31 #define _MACHINE_GLOBALS_H_
35 #define GLOBAL_LVALUE(name, type) \
36 (*(type *)_global_ptr_##name())
38 #define GLOBAL_RVALUE(name, type) \
39 ((type)_global_##name())
41 /* non-volatile version */
42 #define GLOBAL_LVALUE_NV(name, type) \
43 (*(type *)_global_ptr_##name##_nv())
45 #define GLOBAL_RVALUE_NV(name, type) \
46 ((type)_global_##name##_nv())
48 #define GLOBAL_FUNC(name) \
49 static __inline void *_global_ptr_##name(void) { \
51 __asm __volatile("movl $gd_" #name ",%0;" \
52 "addl %%fs:globaldata,%0" : "=r" (val)); \
55 static __inline void *_global_ptr_##name##_nv(void) { \
57 __asm("movl $gd_" #name ",%0;" \
58 "addl %%fs:globaldata,%0" : "=r" (val)); \
61 static __inline int _global_##name(void) { \
63 __asm __volatile("movl %%fs:gd_" #name ",%0" : "=r" (val)); \
66 static __inline int _global_##name##_nv(void) { \
68 __asm("movl %%fs:gd_" #name ",%0" : "=r" (val)); \
71 static __inline void _global_##name##_set(int val) { \
72 __asm __volatile("movl %0,%%fs:gd_" #name : : "r" (val)); \
74 static __inline void _global_##name##_set_nv(int val) { \
75 __asm("movl %0,%%fs:gd_" #name : : "r" (val)); \
78 #if defined(SMP) || defined(KLD_MODULE) || defined(ACTUALLY_LKM_NOT_KERNEL)
80 * The following set of macros works for UP kernel as well, but for maximum
81 * performance we allow the global variables to be accessed directly. On the
82 * other hand, kernel modules should always use these macros to maintain
83 * portability between UP and SMP kernels.
85 #define curthread GLOBAL_RVALUE_NV(curthread, struct thread *)
86 #define idlethread GLOBAL_RVALUE_NV(idlethread, struct thread)
87 #define npxthread GLOBAL_LVALUE(npxthread, struct thread *)
88 #define common_tss GLOBAL_LVALUE(common_tss, struct i386tss)
89 #define switchtime GLOBAL_LVALUE(switchtime, struct timeval)
90 #define switchticks GLOBAL_LVALUE(switchticks, int)
92 #define common_tssd GLOBAL_LVALUE(common_tssd, struct segment_descriptor)
93 #define tss_gdt GLOBAL_LVALUE(tss_gdt, struct segment_descriptor *)
94 #define astpending GLOBAL_LVALUE(astpending, u_int)
97 #define currentldt GLOBAL_LVALUE(currentldt, int)
101 #define cpuid GLOBAL_RVALUE(cpuid, u_int)
102 #define other_cpus GLOBAL_LVALUE(other_cpus, u_int)
103 #define inside_intr GLOBAL_LVALUE(inside_intr, int)
104 #define prv_CMAP1 GLOBAL_LVALUE(prv_CMAP1, pt_entry_t *)
105 #define prv_CMAP2 GLOBAL_LVALUE(prv_CMAP2, pt_entry_t *)
106 #define prv_CMAP3 GLOBAL_LVALUE(prv_CMAP3, pt_entry_t *)
107 #define prv_PMAP1 GLOBAL_LVALUE(prv_PMAP1, pt_entry_t *)
108 #define prv_CADDR1 GLOBAL_RVALUE(prv_CADDR1, caddr_t)
109 #define prv_CADDR2 GLOBAL_RVALUE(prv_CADDR2, caddr_t)
110 #define prv_CADDR3 GLOBAL_RVALUE(prv_CADDR3, caddr_t)
111 #define prv_PADDR1 GLOBAL_RVALUE(prv_PADDR1, unsigned *)
116 * Otherwise we optimize for direct access in UP
118 extern struct thread *curthread; /* Current running proc. */
119 extern struct thread idlethread; /* Idle thread global */
120 extern u_int astpending; /* software interrupt pending */
121 extern int switchticks; /* `ticks' at last context switch. */
122 extern struct timeval switchtime; /* Uptime at last context switch */
127 * note: curthread is never NULL, but curproc can be. Also note that
128 * in Turtle, the current pcb is stored in the thread structure.
130 #define curproc (curthread->td_proc)
132 GLOBAL_FUNC(curthread)
133 GLOBAL_FUNC(idlethread)
134 GLOBAL_FUNC(astpending)
135 GLOBAL_FUNC(npxthread)
136 GLOBAL_FUNC(common_tss)
137 GLOBAL_FUNC(switchtime)
138 GLOBAL_FUNC(switchticks)
140 GLOBAL_FUNC(common_tssd)
144 GLOBAL_FUNC(currentldt)
149 GLOBAL_FUNC(other_cpus)
150 GLOBAL_FUNC(inside_intr)
151 GLOBAL_FUNC(prv_CMAP1)
152 GLOBAL_FUNC(prv_CMAP2)
153 GLOBAL_FUNC(prv_CMAP3)
154 GLOBAL_FUNC(prv_PMAP1)
155 GLOBAL_FUNC(prv_CADDR1)
156 GLOBAL_FUNC(prv_CADDR2)
157 GLOBAL_FUNC(prv_CADDR3)
158 GLOBAL_FUNC(prv_PADDR1)
162 * This is only used in kern/init_main.c and kern/kern_exit.c and should
163 * be converted to procedures. YYY
165 #define SET_CURTHREAD(x) (_global_curthread_set_nv((int)x))
166 #define CLR_CURPROC() curthread->td_proc = NULL
170 #endif /* !_MACHINE_GLOBALS_H_ */