server: Make the mapping base address a client_ptr_t instead of a void pointer.
[wine] / include / wine / library.h
1 /*
2  * Definitions for the Wine library
3  *
4  * Copyright 2000 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #ifndef __WINE_WINE_LIBRARY_H
22 #define __WINE_WINE_LIBRARY_H
23
24 #include <stdarg.h>
25 #include <sys/types.h>
26
27 #include <windef.h>
28 #include <winbase.h>
29
30 #ifdef __WINE_WINE_TEST_H
31 #error This file should not be used in Wine tests
32 #endif
33
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37
38 /* configuration */
39
40 extern const char *wine_get_build_dir(void);
41 extern const char *wine_get_config_dir(void);
42 extern const char *wine_get_data_dir(void);
43 extern const char *wine_get_server_dir(void);
44 extern const char *wine_get_user_name(void);
45 extern const char *wine_get_version(void);
46 extern const char *wine_get_build_id(void);
47 extern void wine_init_argv0_path( const char *argv0 );
48 extern void wine_exec_wine_binary( const char *name, char **argv, const char *env_var );
49
50 /* dll loading */
51
52 typedef void (*load_dll_callback_t)( void *, const char * );
53
54 extern void *wine_dlopen( const char *filename, int flag, char *error, size_t errorsize );
55 extern void *wine_dlsym( void *handle, const char *symbol, char *error, size_t errorsize );
56 extern int wine_dlclose( void *handle, char *error, size_t errorsize );
57 extern void wine_dll_set_callback( load_dll_callback_t load );
58 extern void *wine_dll_load( const char *filename, char *error, int errorsize, int *file_exists );
59 extern void *wine_dll_load_main_exe( const char *name, char *error, int errorsize,
60                                      int test_only, int *file_exists );
61 extern void wine_dll_unload( void *handle );
62 extern const char *wine_dll_enum_load_path( unsigned int index );
63 extern int wine_dll_get_owner( const char *name, char *buffer, int size, int *file_exists );
64
65 extern int __wine_main_argc;
66 extern char **__wine_main_argv;
67 extern WCHAR **__wine_main_wargv;
68 extern char **__wine_main_environ;
69 extern void __wine_dll_register( const IMAGE_NT_HEADERS *header, const char *filename );
70 extern void wine_init( int argc, char *argv[], char *error, int error_size );
71
72 /* portability */
73
74 extern void DECLSPEC_NORETURN wine_switch_to_stack( void (*func)(void *), void *arg, void *stack );
75 extern int wine_call_on_stack( int (*func)(void *), void *arg, void *stack );
76
77 /* memory mappings */
78
79 extern void *wine_anon_mmap( void *start, size_t size, int prot, int flags );
80 extern void wine_mmap_add_reserved_area( void *addr, size_t size );
81 extern void wine_mmap_remove_reserved_area( void *addr, size_t size, int unmap );
82 extern int wine_mmap_is_in_reserved_area( void *addr, size_t size );
83 extern int wine_mmap_enum_reserved_areas( int (*enum_func)(void *base, size_t size, void *arg),
84                                           void *arg, int top_down );
85
86 /* LDT management */
87
88 extern void wine_ldt_init_locking( void (*lock_func)(void), void (*unlock_func)(void) );
89 extern void wine_ldt_get_entry( unsigned short sel, LDT_ENTRY *entry );
90 extern int wine_ldt_set_entry( unsigned short sel, const LDT_ENTRY *entry );
91 extern int wine_ldt_is_system( unsigned short sel );
92 extern void *wine_ldt_get_ptr( unsigned short sel, unsigned long offset );
93 extern unsigned short wine_ldt_alloc_entries( int count );
94 extern unsigned short wine_ldt_realloc_entries( unsigned short sel, int oldcount, int newcount );
95 extern void wine_ldt_free_entries( unsigned short sel, int count );
96 #if defined(__i386__) && !defined(__MINGW32__) && !defined(_MSC_VER)
97 extern unsigned short wine_ldt_alloc_fs(void);
98 extern void wine_ldt_init_fs( unsigned short sel, const LDT_ENTRY *entry );
99 extern void wine_ldt_free_fs( unsigned short sel );
100 #else  /* __i386__ */
101 static inline unsigned short wine_ldt_alloc_fs(void) { return 0x0b; /* pseudo GDT selector */ }
102 static inline void wine_ldt_init_fs( unsigned short sel, const LDT_ENTRY *entry ) { }
103 static inline void wine_ldt_free_fs( unsigned short sel ) { }
104 #endif  /* __i386__ */
105
106
107 /* the local copy of the LDT */
108 #ifdef __CYGWIN__
109 # ifdef WINE_EXPORT_LDT_COPY
110 #  define WINE_LDT_EXTERN __declspec(dllexport)
111 # else
112 #  define WINE_LDT_EXTERN __declspec(dllimport)
113 # endif
114 #else
115 # define WINE_LDT_EXTERN extern
116 #endif
117
118 WINE_LDT_EXTERN struct __wine_ldt_copy
119 {
120     void         *base[8192];  /* base address or 0 if entry is free   */
121     unsigned long limit[8192]; /* limit in bytes or 0 if entry is free */
122     unsigned char flags[8192]; /* flags (defined below) */
123 } wine_ldt_copy;
124
125 #define WINE_LDT_FLAGS_DATA      0x13  /* Data segment */
126 #define WINE_LDT_FLAGS_STACK     0x17  /* Stack segment */
127 #define WINE_LDT_FLAGS_CODE      0x1b  /* Code segment */
128 #define WINE_LDT_FLAGS_TYPE_MASK 0x1f  /* Mask for segment type */
129 #define WINE_LDT_FLAGS_32BIT     0x40  /* Segment is 32-bit (code or stack) */
130 #define WINE_LDT_FLAGS_ALLOCATED 0x80  /* Segment is allocated (no longer free) */
131
132 /* helper functions to manipulate the LDT_ENTRY structure */
133 static inline void wine_ldt_set_base( LDT_ENTRY *ent, const void *base )
134 {
135     ent->BaseLow               = (WORD)(ULONG_PTR)base;
136     ent->HighWord.Bits.BaseMid = (BYTE)((ULONG_PTR)base >> 16);
137     ent->HighWord.Bits.BaseHi  = (BYTE)((ULONG_PTR)base >> 24);
138 #ifdef _WIN64
139     ent->BaseHigh              = (ULONG_PTR)base >> 32;
140 #endif
141 }
142 static inline void wine_ldt_set_limit( LDT_ENTRY *ent, unsigned int limit )
143 {
144     if ((ent->HighWord.Bits.Granularity = (limit >= 0x100000))) limit >>= 12;
145     ent->LimitLow = (WORD)limit;
146     ent->HighWord.Bits.LimitHi = (limit >> 16);
147 }
148 static inline void *wine_ldt_get_base( const LDT_ENTRY *ent )
149 {
150     return (void *)(ent->BaseLow |
151 #ifdef _WIN64
152                     (ULONG_PTR)ent->BaseHigh << 32 |
153 #endif
154                     (ULONG_PTR)ent->HighWord.Bits.BaseMid << 16 |
155                     (ULONG_PTR)ent->HighWord.Bits.BaseHi << 24);
156 }
157 static inline unsigned int wine_ldt_get_limit( const LDT_ENTRY *ent )
158 {
159     unsigned int limit = ent->LimitLow | (ent->HighWord.Bits.LimitHi << 16);
160     if (ent->HighWord.Bits.Granularity) limit = (limit << 12) | 0xfff;
161     return limit;
162 }
163 static inline void wine_ldt_set_flags( LDT_ENTRY *ent, unsigned char flags )
164 {
165     ent->HighWord.Bits.Dpl         = 3;
166     ent->HighWord.Bits.Pres        = 1;
167     ent->HighWord.Bits.Type        = flags;
168     ent->HighWord.Bits.Sys         = 0;
169     ent->HighWord.Bits.Reserved_0  = 0;
170     ent->HighWord.Bits.Default_Big = (flags & WINE_LDT_FLAGS_32BIT) != 0;
171 }
172 static inline unsigned char wine_ldt_get_flags( const LDT_ENTRY *ent )
173 {
174     unsigned char ret = ent->HighWord.Bits.Type;
175     if (ent->HighWord.Bits.Default_Big) ret |= WINE_LDT_FLAGS_32BIT;
176     return ret;
177 }
178 static inline int wine_ldt_is_empty( const LDT_ENTRY *ent )
179 {
180     const DWORD *dw = (const DWORD *)ent;
181     return (dw[0] | dw[1]) == 0;
182 }
183
184 /* segment register access */
185
186 #ifdef __i386__
187 # ifdef __MINGW32__
188 #  define __DEFINE_GET_SEG(seg) \
189     static inline unsigned short wine_get_##seg(void); \
190     static inline unsigned short wine_get_##seg(void) \
191     { unsigned short res; __asm__ __volatile__("movw %%" #seg ",%w0" : "=r"(res)); return res; }
192 #  define __DEFINE_SET_SEG(seg) \
193     static inline void wine_set_##seg(int val); \
194     static inline void wine_set_##seg(int val) { __asm__("movw %w0,%%" #seg : : "r" (val)); }
195 # elif defined(__GNUC__)
196 #  define __DEFINE_GET_SEG(seg) \
197     extern inline unsigned short wine_get_##seg(void); \
198     extern inline unsigned short wine_get_##seg(void) \
199     { unsigned short res; __asm__ __volatile__("movw %%" #seg ",%w0" : "=r"(res)); return res; }
200 #  define __DEFINE_SET_SEG(seg) \
201     extern inline void wine_set_##seg(int val); \
202     extern inline void wine_set_##seg(int val) { __asm__("movw %w0,%%" #seg : : "r" (val)); }
203 # elif defined(_MSC_VER)
204 #  define __DEFINE_GET_SEG(seg) \
205     extern inline unsigned short wine_get_##seg(void); \
206     extern inline unsigned short wine_get_##seg(void) \
207     { unsigned short res; __asm { mov res, seg } return res; }
208 #  define __DEFINE_SET_SEG(seg) \
209     extern inline void wine_set_##seg(unsigned short val); \
210     extern inline void wine_set_##seg(unsigned short val) { __asm { mov seg, val } }
211 # else  /* __GNUC__ || _MSC_VER */
212 #  define __DEFINE_GET_SEG(seg) extern unsigned short wine_get_##seg(void);
213 #  define __DEFINE_SET_SEG(seg) extern void wine_set_##seg(unsigned int);
214 # endif /* __GNUC__ || _MSC_VER */
215 #else  /* __i386__ */
216 # define __DEFINE_GET_SEG(seg) static inline unsigned short wine_get_##seg(void) { return 0; }
217 # define __DEFINE_SET_SEG(seg) static inline void wine_set_##seg(int val) { /* nothing */ }
218 #endif  /* __i386__ */
219
220 __DEFINE_GET_SEG(cs)
221 __DEFINE_GET_SEG(ds)
222 __DEFINE_GET_SEG(es)
223 __DEFINE_GET_SEG(fs)
224 __DEFINE_GET_SEG(gs)
225 __DEFINE_GET_SEG(ss)
226 __DEFINE_SET_SEG(fs)
227 __DEFINE_SET_SEG(gs)
228 #undef __DEFINE_GET_SEG
229 #undef __DEFINE_SET_SEG
230
231 #ifdef __cplusplus
232 }
233 #endif
234
235 #endif  /* __WINE_WINE_LIBRARY_H */