Added PF_NX_ENABLED definition.
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 /* configuration */
31
32 extern const char *wine_get_config_dir(void);
33 extern const char *wine_get_server_dir(void);
34 extern const char *wine_get_user_name(void);
35 extern void wine_init_argv0_path( const char *argv0 );
36 extern void wine_exec_wine_binary( const char *name, char **argv, char **envp, int use_preloader );
37
38 /* dll loading */
39
40 typedef void (*load_dll_callback_t)( void *, const char * );
41
42 extern void *wine_dlopen( const char *filename, int flag, char *error, size_t errorsize );
43 extern void *wine_dlsym( void *handle, const char *symbol, char *error, size_t errorsize );
44 extern int wine_dlclose( void *handle, char *error, size_t errorsize );
45 extern void wine_dll_set_callback( load_dll_callback_t load );
46 extern void *wine_dll_load( const char *filename, char *error, int errorsize, int *file_exists );
47 extern void *wine_dll_load_main_exe( const char *name, char *error, int errorsize,
48                                      int test_only, int *file_exists );
49 extern void wine_dll_unload( void *handle );
50 extern int wine_dll_get_owner( const char *name, char *buffer, int size, int *file_exists );
51
52 extern int __wine_main_argc;
53 extern char **__wine_main_argv;
54 extern WCHAR **__wine_main_wargv;
55 extern char **__wine_main_environ;
56 extern void wine_init( int argc, char *argv[], char *error, int error_size );
57
58 /* debugging */
59
60 extern const char * (*__wine_dbgstr_an)( const char * s, int n );
61 extern const char * (*__wine_dbgstr_wn)( const WCHAR *s, int n );
62 extern const char * (*__wine_dbg_vsprintf)( const char *format, va_list args );
63 extern int (*__wine_dbg_vprintf)( const char *format, va_list args );
64 extern int (*__wine_dbg_vlog)( unsigned int cls, const char *channel,
65                                const char *function, const char *format, va_list args );
66
67 extern void wine_dbg_add_option( const char *name, unsigned char set, unsigned char clear );
68 extern int wine_dbg_parse_options( const char *str );
69
70 /* portability */
71
72 extern void DECLSPEC_NORETURN wine_switch_to_stack( void (*func)(void *), void *arg, void *stack );
73 extern void wine_set_pe_load_area( void *base, size_t size );
74 extern void wine_free_pe_load_area(void);
75
76 /* memory mappings */
77
78 extern void *wine_anon_mmap( void *start, size_t size, int prot, int flags );
79 extern void wine_mmap_add_reserved_area( void *addr, size_t size );
80 extern void wine_mmap_remove_reserved_area( void *addr, size_t size, int unmap );
81 extern int wine_mmap_is_in_reserved_area( void *addr, size_t size );
82
83 /* LDT management */
84
85 extern void wine_ldt_init_locking( void (*lock_func)(void), void (*unlock_func)(void) );
86 extern void wine_ldt_get_entry( unsigned short sel, LDT_ENTRY *entry );
87 extern int wine_ldt_set_entry( unsigned short sel, const LDT_ENTRY *entry );
88 extern int wine_ldt_is_system( unsigned short sel );
89 extern void *wine_ldt_get_ptr( unsigned short sel, unsigned int offset );
90 extern unsigned short wine_ldt_alloc_entries( int count );
91 extern unsigned short wine_ldt_realloc_entries( unsigned short sel, int oldcount, int newcount );
92 extern void wine_ldt_free_entries( unsigned short sel, int count );
93 #ifdef __i386__
94 extern unsigned short wine_ldt_alloc_fs(void);
95 extern void wine_ldt_init_fs( unsigned short sel, const LDT_ENTRY *entry );
96 extern void wine_ldt_free_fs( unsigned short sel );
97 #else  /* __i386__ */
98 static inline unsigned short wine_ldt_alloc_fs(void) { return 0x0b; /* pseudo GDT selector */ }
99 static inline void wine_ldt_init_fs( unsigned short sel, const LDT_ENTRY *entry ) { }
100 static inline void wine_ldt_free_fs( unsigned short sel ) { }
101 #endif  /* __i386__ */
102
103
104 /* the local copy of the LDT */
105 #ifdef __CYGWIN__
106 # ifdef WINE_EXPORT_LDT_COPY
107 #  define WINE_LDT_EXTERN __declspec(dllexport)
108 # else
109 #  define WINE_LDT_EXTERN __declspec(dllimport)
110 # endif
111 #else
112 # define WINE_LDT_EXTERN extern
113 #endif
114
115 WINE_LDT_EXTERN struct __wine_ldt_copy
116 {
117     void         *base[8192];  /* base address or 0 if entry is free   */
118     unsigned long limit[8192]; /* limit in bytes or 0 if entry is free */
119     unsigned char flags[8192]; /* flags (defined below) */
120 } wine_ldt_copy;
121
122 #define WINE_LDT_FLAGS_DATA      0x13  /* Data segment */
123 #define WINE_LDT_FLAGS_STACK     0x17  /* Stack segment */
124 #define WINE_LDT_FLAGS_CODE      0x1b  /* Code segment */
125 #define WINE_LDT_FLAGS_TYPE_MASK 0x1f  /* Mask for segment type */
126 #define WINE_LDT_FLAGS_32BIT     0x40  /* Segment is 32-bit (code or stack) */
127 #define WINE_LDT_FLAGS_ALLOCATED 0x80  /* Segment is allocated (no longer free) */
128
129 /* helper functions to manipulate the LDT_ENTRY structure */
130 inline static void wine_ldt_set_base( LDT_ENTRY *ent, const void *base )
131 {
132     ent->BaseLow               = (WORD)(unsigned long)base;
133     ent->HighWord.Bits.BaseMid = (BYTE)((unsigned long)base >> 16);
134     ent->HighWord.Bits.BaseHi  = (BYTE)((unsigned long)base >> 24);
135 }
136 inline static void wine_ldt_set_limit( LDT_ENTRY *ent, unsigned int limit )
137 {
138     if ((ent->HighWord.Bits.Granularity = (limit >= 0x100000))) limit >>= 12;
139     ent->LimitLow = (WORD)limit;
140     ent->HighWord.Bits.LimitHi = (limit >> 16);
141 }
142 inline static void *wine_ldt_get_base( const LDT_ENTRY *ent )
143 {
144     return (void *)(ent->BaseLow |
145                     (unsigned long)ent->HighWord.Bits.BaseMid << 16 |
146                     (unsigned long)ent->HighWord.Bits.BaseHi << 24);
147 }
148 inline static unsigned int wine_ldt_get_limit( const LDT_ENTRY *ent )
149 {
150     unsigned int limit = ent->LimitLow | (ent->HighWord.Bits.LimitHi << 16);
151     if (ent->HighWord.Bits.Granularity) limit = (limit << 12) | 0xfff;
152     return limit;
153 }
154 inline static void wine_ldt_set_flags( LDT_ENTRY *ent, unsigned char flags )
155 {
156     ent->HighWord.Bits.Dpl         = 3;
157     ent->HighWord.Bits.Pres        = 1;
158     ent->HighWord.Bits.Type        = flags;
159     ent->HighWord.Bits.Sys         = 0;
160     ent->HighWord.Bits.Reserved_0  = 0;
161     ent->HighWord.Bits.Default_Big = (flags & WINE_LDT_FLAGS_32BIT) != 0;
162 }
163 inline static unsigned char wine_ldt_get_flags( const LDT_ENTRY *ent )
164 {
165     unsigned char ret = ent->HighWord.Bits.Type;
166     if (ent->HighWord.Bits.Default_Big) ret |= WINE_LDT_FLAGS_32BIT;
167     return ret;
168 }
169 inline static int wine_ldt_is_empty( const LDT_ENTRY *ent )
170 {
171     const DWORD *dw = (const DWORD *)ent;
172     return (dw[0] | dw[1]) == 0;
173 }
174
175 /* segment register access */
176
177 #ifdef __i386__
178 # ifdef __GNUC__
179 #  define __DEFINE_GET_SEG(seg) \
180     extern inline unsigned short wine_get_##seg(void); \
181     extern inline unsigned short wine_get_##seg(void) \
182     { unsigned short res; __asm__("movw %%" #seg ",%w0" : "=r"(res)); return res; }
183 #  define __DEFINE_SET_SEG(seg) \
184     extern inline void wine_set_##seg(int val); \
185     extern inline void wine_set_##seg(int val) { __asm__("movw %w0,%%" #seg : : "r" (val)); }
186 # elif defined(_MSC_VER)
187 #  define __DEFINE_GET_SEG(seg) \
188     extern inline unsigned short wine_get_##seg(void); \
189     extern inline unsigned short wine_get_##seg(void) \
190     { unsigned short res; __asm { mov res, seg } return res; }
191 #  define __DEFINE_SET_SEG(seg) \
192     extern inline void wine_set_##seg(unsigned short val); \
193     extern inline void wine_set_##seg(unsigned short val) { __asm { mov seg, val } }
194 # else  /* __GNUC__ || _MSC_VER */
195 #  define __DEFINE_GET_SEG(seg) extern unsigned short wine_get_##seg(void);
196 #  define __DEFINE_SET_SEG(seg) extern void wine_set_##seg(unsigned int);
197 # endif /* __GNUC__ || _MSC_VER */
198 #else  /* __i386__ */
199 # define __DEFINE_GET_SEG(seg) inline static unsigned short wine_get_##seg(void) { return 0; }
200 # define __DEFINE_SET_SEG(seg) inline static void wine_set_##seg(int val) { /* nothing */ }
201 #endif  /* __i386__ */
202
203 __DEFINE_GET_SEG(cs)
204 __DEFINE_GET_SEG(ds)
205 __DEFINE_GET_SEG(es)
206 __DEFINE_GET_SEG(fs)
207 __DEFINE_GET_SEG(gs)
208 __DEFINE_GET_SEG(ss)
209 __DEFINE_SET_SEG(fs)
210 __DEFINE_SET_SEG(gs)
211 #undef __DEFINE_GET_SEG
212 #undef __DEFINE_SET_SEG
213
214 #endif  /* __WINE_WINE_LIBRARY_H */