Initial import from FreeBSD RELENG_4:
[games.git] / sys / platform / pc32 / include / globals.h
1 /*-
2  * Copyright (c) 1999 Luoqi Chen <luoqi@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/i386/include/globals.h,v 1.5.2.1 2000/05/16 06:58:10 dillon Exp $
27  */
28
29 #ifndef _MACHINE_GLOBALS_H_
30 #define _MACHINE_GLOBALS_H_
31
32 #ifdef _KERNEL
33
34 #define GLOBAL_LVALUE(name, type) \
35         (*(type *)_global_ptr_##name())
36         
37 #define GLOBAL_RVALUE(name, type) \
38         ((type)_global_##name())
39
40 /* non-volatile version */
41 #define GLOBAL_LVALUE_NV(name, type) \
42         (*(type *)_global_ptr_##name##_nv())
43         
44 #define GLOBAL_RVALUE_NV(name, type) \
45         ((type)_global_##name##_nv())
46
47 #define GLOBAL_FUNC(name) \
48         static __inline void *_global_ptr_##name(void) { \
49                 void *val; \
50                 __asm __volatile("movl $gd_" #name ",%0;" \
51                         "addl %%fs:globaldata,%0" : "=r" (val)); \
52                 return (val); \
53         } \
54         static __inline void *_global_ptr_##name##_nv(void) { \
55                 void *val; \
56                 __asm("movl $gd_" #name ",%0;" \
57                         "addl %%fs:globaldata,%0" : "=r" (val)); \
58                 return (val); \
59         } \
60         static __inline int _global_##name(void) { \
61                 int val; \
62                 __asm __volatile("movl %%fs:gd_" #name ",%0" : "=r" (val)); \
63                 return (val); \
64         } \
65         static __inline int _global_##name##_nv(void) { \
66                 int val; \
67                 __asm("movl %%fs:gd_" #name ",%0" : "=r" (val)); \
68                 return (val); \
69         } \
70         static __inline void _global_##name##_set(int val) { \
71                 __asm __volatile("movl %0,%%fs:gd_" #name : : "r" (val)); \
72         } \
73         static __inline void _global_##name##_set_nv(int val) { \
74                 __asm("movl %0,%%fs:gd_" #name : : "r" (val)); \
75         }
76
77 #if defined(SMP) || defined(KLD_MODULE) || defined(ACTUALLY_LKM_NOT_KERNEL)
78 /*
79  * The following set of macros works for UP kernel as well, but for maximum
80  * performance we allow the global variables to be accessed directly. On the
81  * other hand, kernel modules should always use these macros to maintain
82  * portability between UP and SMP kernels.
83  */
84 #define curproc         GLOBAL_RVALUE_NV(curproc, struct proc *)
85 #define curpcb          GLOBAL_RVALUE_NV(curpcb, struct pcb *)
86 #define npxproc         GLOBAL_LVALUE(npxproc, struct proc *)
87 #define common_tss      GLOBAL_LVALUE(common_tss, struct i386tss)
88 #define switchtime      GLOBAL_LVALUE(switchtime, struct timeval)
89 #define switchticks     GLOBAL_LVALUE(switchticks, int)
90
91 #define common_tssd     GLOBAL_LVALUE(common_tssd, struct segment_descriptor)
92 #define tss_gdt         GLOBAL_LVALUE(tss_gdt, struct segment_descriptor *)
93 #define astpending      GLOBAL_LVALUE(astpending, u_int)
94
95 #ifdef USER_LDT
96 #define currentldt      GLOBAL_LVALUE(currentldt, int)
97 #endif
98
99 #ifdef SMP
100 #define cpuid           GLOBAL_RVALUE(cpuid, u_int)
101 #define other_cpus      GLOBAL_LVALUE(other_cpus, u_int)
102 #define inside_intr     GLOBAL_LVALUE(inside_intr, int)
103 #define prv_CMAP1       GLOBAL_LVALUE(prv_CMAP1, pt_entry_t *)
104 #define prv_CMAP2       GLOBAL_LVALUE(prv_CMAP2, pt_entry_t *)
105 #define prv_CMAP3       GLOBAL_LVALUE(prv_CMAP3, pt_entry_t *)
106 #define prv_PMAP1       GLOBAL_LVALUE(prv_PMAP1, pt_entry_t *)
107 #define prv_CADDR1      GLOBAL_RVALUE(prv_CADDR1, caddr_t)
108 #define prv_CADDR2      GLOBAL_RVALUE(prv_CADDR2, caddr_t)
109 #define prv_CADDR3      GLOBAL_RVALUE(prv_CADDR3, caddr_t)
110 #define prv_PADDR1      GLOBAL_RVALUE(prv_PADDR1, unsigned *)
111 #endif
112 #endif  /*UP kernel*/
113
114 GLOBAL_FUNC(curproc)
115 GLOBAL_FUNC(astpending)
116 GLOBAL_FUNC(curpcb)
117 GLOBAL_FUNC(npxproc)
118 GLOBAL_FUNC(common_tss)
119 GLOBAL_FUNC(switchtime)
120 GLOBAL_FUNC(switchticks)
121
122 GLOBAL_FUNC(common_tssd)
123 GLOBAL_FUNC(tss_gdt)
124
125 #ifdef USER_LDT
126 GLOBAL_FUNC(currentldt)
127 #endif
128
129 #ifdef SMP
130 GLOBAL_FUNC(cpuid)
131 GLOBAL_FUNC(other_cpus)
132 GLOBAL_FUNC(inside_intr)
133 GLOBAL_FUNC(prv_CMAP1)
134 GLOBAL_FUNC(prv_CMAP2)
135 GLOBAL_FUNC(prv_CMAP3)
136 GLOBAL_FUNC(prv_PMAP1)
137 GLOBAL_FUNC(prv_CADDR1)
138 GLOBAL_FUNC(prv_CADDR2)
139 GLOBAL_FUNC(prv_CADDR3)
140 GLOBAL_FUNC(prv_PADDR1)
141 #endif
142
143 #define SET_CURPROC(x)  (_global_curproc_set_nv((int)x))
144
145 #endif  /* _KERNEL */
146
147 #endif  /* !_MACHINE_GLOBALS_H_ */