OleMetaFilePictFromIconAndLabel16: slightly broken, use LPOLESTR16.
[wine] / memory / ldt.c
1 /*
2  * LDT manipulation functions
3  *
4  * Copyright 1993 Robert J. Amstadt
5  * Copyright 1995 Alexandre Julliard
6  */
7
8 #include <stdlib.h>
9 #include <string.h>
10 #include <errno.h>
11 #include "ldt.h"
12 #include "debugtools.h"
13
14 DEFAULT_DEBUG_CHANNEL(ldt)
15
16 #ifdef __i386__
17
18 #ifdef linux
19 #include <sys/syscall.h>
20
21 struct modify_ldt_s 
22 {
23     unsigned int  entry_number;
24     unsigned long base_addr;
25     unsigned int  limit;
26     unsigned int  seg_32bit : 1;
27     unsigned int  contents : 2;
28     unsigned int  read_exec_only : 1;
29     unsigned int  limit_in_pages : 1;
30     unsigned int  seg_not_present : 1;
31 };
32
33 static inline int modify_ldt( int func, struct modify_ldt_s *ptr,
34                                   unsigned long count )
35 {
36     int res;
37 #ifdef __PIC__
38     __asm__ __volatile__( "pushl %%ebx\n\t"
39                           "movl %2,%%ebx\n\t"
40                           "int $0x80\n\t"
41                           "popl %%ebx"
42                           : "=a" (res)
43                           : "0" (SYS_modify_ldt),
44                             "r" (func),
45                             "c" (ptr),
46                             "d" (count) );
47 #else
48     __asm__ __volatile__("int $0x80"
49                          : "=a" (res)
50                          : "0" (SYS_modify_ldt),
51                            "b" (func),
52                            "c" (ptr),
53                            "d" (count) );
54 #endif  /* __PIC__ */
55     if (res >= 0) return res;
56     errno = -res;
57     return -1;
58 }
59
60 #endif  /* linux */
61
62 #if defined(__svr4__) || defined(_SCO_DS)
63 #include <sys/sysi86.h>
64 extern int sysi86(int,void*);
65 #ifndef __sun__
66 #include <sys/seg.h>
67 #endif
68 #endif
69
70 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
71 #include <machine/segments.h>
72
73 extern int i386_get_ldt(int, union descriptor *, int);
74 extern int i386_set_ldt(int, union descriptor *, int);
75 #endif  /* __NetBSD__ || __FreeBSD__ || __OpenBSD__ */
76
77 #endif  /* __i386__ */
78
79
80 ldt_copy_entry ldt_copy[LDT_SIZE];
81 unsigned char ldt_flags_copy[LDT_SIZE];
82
83         
84 /***********************************************************************
85  *           LDT_BytesToEntry
86  *
87  * Convert the raw bytes of the descriptor to an ldt_entry structure.
88  */
89 void LDT_BytesToEntry( const unsigned long *buffer, ldt_entry *content )
90 {
91     content->base  = (*buffer >> 16) & 0x0000ffff;
92     content->limit = *buffer & 0x0000ffff;
93     buffer++;
94     content->base  |= (*buffer & 0xff000000) | ((*buffer << 16) & 0x00ff0000);
95     content->limit |= (*buffer & 0x000f0000);
96     content->type           = (*buffer >> 10) & 3;
97     content->seg_32bit      = (*buffer & 0x00400000) != 0;
98     content->read_only      = (*buffer & 0x00000200) == 0;
99     content->limit_in_pages = (*buffer & 0x00800000) != 0;
100 }
101
102
103 /***********************************************************************
104  *           LDT_EntryToBytes
105  *
106  * Convert an ldt_entry structure to the raw bytes of the descriptor.
107  */
108 void LDT_EntryToBytes( unsigned long *buffer, const ldt_entry *content )
109 {
110     *buffer++ = ((content->base & 0x0000ffff) << 16) |
111                  (content->limit & 0x0ffff);
112     *buffer = (content->base & 0xff000000) |
113               ((content->base & 0x00ff0000)>>16) |
114               (content->limit & 0xf0000) |
115               (content->type << 10) |
116               ((content->read_only == 0) << 9) |
117               ((content->seg_32bit != 0) << 22) |
118               ((content->limit_in_pages != 0) << 23) |
119               0xf000;
120 }
121
122
123 /***********************************************************************
124  *           LDT_GetEntry
125  *
126  * Retrieve an LDT entry.
127  */
128 int LDT_GetEntry( int entry, ldt_entry *content )
129 {
130     int ret = 0;
131
132     content->base           = ldt_copy[entry].base;
133     content->limit          = ldt_copy[entry].limit;
134     content->type           = (ldt_flags_copy[entry] & LDT_FLAGS_TYPE);
135     content->seg_32bit      = (ldt_flags_copy[entry] & LDT_FLAGS_32BIT) != 0;
136     content->read_only      = (ldt_flags_copy[entry] & LDT_FLAGS_READONLY) !=0;
137     content->limit_in_pages = (ldt_flags_copy[entry] & LDT_FLAGS_BIG) !=0;
138     if (content->limit_in_pages) content->limit >>= 12;
139     return ret;
140 }
141
142
143 /***********************************************************************
144  *           LDT_SetEntry
145  *
146  * Set an LDT entry.
147  */
148 int LDT_SetEntry( int entry, const ldt_entry *content )
149 {
150     int ret = 0;
151
152     TRACE("entry=%04x base=%08lx limit=%05lx %s %d-bit "
153                  "flags=%c%c%c\n", entry, content->base, content->limit,
154                  content->limit_in_pages ? "pages" : "bytes",
155                  content->seg_32bit ? 32 : 16,
156                  content->read_only && (content->type & SEGMENT_CODE) ? '-' : 'r',
157                  content->read_only || (content->type & SEGMENT_CODE) ? '-' : 'w',
158                  (content->type & SEGMENT_CODE) ? 'x' : '-' );
159     
160     /* Entry 0 must not be modified; its base and limit are always 0 */
161     if (!entry) return 0;
162
163 #ifdef __i386__
164
165 #ifdef linux
166     {
167         struct modify_ldt_s ldt_info;
168
169         ldt_info.entry_number    = entry;
170         ldt_info.base_addr       = content->base;
171         ldt_info.limit           = content->limit;
172         ldt_info.seg_32bit       = content->seg_32bit != 0;
173         ldt_info.contents        = content->type;
174         ldt_info.read_exec_only  = content->read_only != 0;
175         ldt_info.limit_in_pages  = content->limit_in_pages != 0;
176         ldt_info.seg_not_present = 0;
177         /* Make sure the info will be accepted by the kernel */
178         /* This is ugly, but what can I do? */
179         if (content->type == SEGMENT_STACK)
180         {
181             /* FIXME */
182         }
183         else
184         {
185             if (ldt_info.base_addr >= 0xc0000000)
186             {
187                 WARN("Invalid base addr %08lx\n",
188                          ldt_info.base_addr );
189                 return -1;
190             }
191             if (content->limit_in_pages)
192             {
193                 if ((ldt_info.limit << 12) + 0xfff >
194                                                0xc0000000 - ldt_info.base_addr)
195                     ldt_info.limit = (0xc0000000 - 0xfff - ldt_info.base_addr) >> 12;
196             }
197             else
198             {
199                 if (ldt_info.limit > 0xc0000000 - ldt_info.base_addr)
200                     ldt_info.limit = 0xc0000000 - ldt_info.base_addr;
201             }
202         }
203         if ((ret = modify_ldt(1, &ldt_info, sizeof(ldt_info))) < 0)
204             perror( "modify_ldt" );
205     }
206 #endif  /* linux */
207
208 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
209     {
210         long d[2];
211
212         LDT_EntryToBytes( d, content );
213         ret = i386_set_ldt(entry, (union descriptor *)d, 1);
214         if (ret < 0)
215         {
216             perror("i386_set_ldt");
217             MESSAGE("Did you reconfigure the kernel with \"options USER_LDT\"?\n");
218             exit(1);
219         }
220     }
221 #endif  /* __NetBSD__ || __FreeBSD__ || __OpenBSD__ */
222
223 #if defined(__svr4__) || defined(_SCO_DS)
224     {
225         struct ssd ldt_mod;
226         int i;
227         ldt_mod.sel = ENTRY_TO_SELECTOR(entry) | 4;
228         ldt_mod.bo = content->base;
229         ldt_mod.ls = content->limit;
230         i = ((content->limit & 0xf0000) |
231              (content->type << 10) |
232              (((content->read_only != 0) ^ 1) << 9) |
233              ((content->seg_32bit != 0) << 22) |
234              ((content->limit_in_pages != 0)<< 23) |
235              (1<<15) |
236              0x7000);
237
238         ldt_mod.acc1 = (i & 0xff00) >> 8;
239         ldt_mod.acc2 = (i & 0xf00000) >> 20;
240
241         if (content->base == 0)
242         {
243             ldt_mod.acc1 =  0;
244             ldt_mod.acc2 = 0;
245         }
246         if ((ret = sysi86(SI86DSCR, &ldt_mod)) == -1) perror("sysi86");
247     }    
248 #endif
249
250 #endif  /* __i386__ */
251
252     if (ret < 0) return ret;
253     ldt_copy[entry].base = content->base;
254     if (!content->limit_in_pages) ldt_copy[entry].limit = content->limit;
255     else ldt_copy[entry].limit = (content->limit << 12) | 0x0fff;
256     ldt_flags_copy[entry] = (content->type & LDT_FLAGS_TYPE) |
257                             (content->read_only ? LDT_FLAGS_READONLY : 0) |
258                             (content->seg_32bit ? LDT_FLAGS_32BIT : 0) |
259                             (content->limit_in_pages ? LDT_FLAGS_BIG : 0) |
260                             (ldt_flags_copy[entry] & LDT_FLAGS_ALLOCATED);
261     return ret;
262 }
263
264
265 /***********************************************************************
266  *           LDT_Print
267  *
268  * Print the content of the LDT on stdout.
269  */
270 void LDT_Print( int start, int length )
271 {
272     int i;
273     char flags[3];
274
275     if (length == -1) length = LDT_SIZE - start;
276     for (i = start; i < start + length; i++)
277     {
278         if (!ldt_copy[i].base && !ldt_copy[i].limit) continue; /* Free entry */
279         if ((ldt_flags_copy[i] & LDT_FLAGS_TYPE) == SEGMENT_CODE)
280         {
281             flags[0] = (ldt_flags_copy[i] & LDT_FLAGS_EXECONLY) ? '-' : 'r';
282             flags[1] = '-';
283             flags[2] = 'x';
284         }
285         else
286         {
287             flags[0] = 'r';
288             flags[1] = (ldt_flags_copy[i] & LDT_FLAGS_READONLY) ? '-' : 'w';
289             flags[2] = '-';
290         }
291         MESSAGE("%04x: sel=%04x base=%08lx limit=%08lx %d-bit %c%c%c\n",
292             i, ENTRY_TO_SELECTOR(i), ldt_copy[i].base, ldt_copy[i].limit,
293             ldt_flags_copy[i] & LDT_FLAGS_32BIT ? 32 : 16,
294             flags[0], flags[1], flags[2] );
295     }
296 }