Release 970914
[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 <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <errno.h>
12 #include "ldt.h"
13 #include "stddebug.h"
14 #include "debug.h"
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                             "g" (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 #include <sys/seg.h>
65 #endif
66
67 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
68 #include <machine/segments.h>
69
70 extern int i386_get_ldt(int, union descriptor *, int);
71 extern int i386_set_ldt(int, union descriptor *, int);
72 #endif  /* __NetBSD__ || __FreeBSD__ || __OpenBSD__ */
73
74 #endif  /* __i386__ */
75
76
77 ldt_copy_entry ldt_copy[LDT_SIZE];
78 unsigned char ldt_flags_copy[LDT_SIZE];
79
80         
81 /***********************************************************************
82  *           LDT_BytesToEntry
83  *
84  * Convert the raw bytes of the descriptor to an ldt_entry structure.
85  */
86 void LDT_BytesToEntry( const unsigned long *buffer, ldt_entry *content )
87 {
88     content->base  = (*buffer >> 16) & 0x0000ffff;
89     content->limit = *buffer & 0x0000ffff;
90     buffer++;
91     content->base  |= (*buffer & 0xff000000) | ((*buffer << 16) & 0x00ff0000);
92     content->limit |= (*buffer & 0x000f0000);
93     content->type           = (*buffer >> 10) & 3;
94     content->seg_32bit      = (*buffer & 0x00400000) != 0;
95     content->read_only      = (*buffer & 0x00000200) == 0;
96     content->limit_in_pages = (*buffer & 0x00800000) != 0;
97 }
98
99
100 /***********************************************************************
101  *           LDT_EntryToBytes
102  *
103  * Convert an ldt_entry structure to the raw bytes of the descriptor.
104  */
105 void LDT_EntryToBytes( unsigned long *buffer, const ldt_entry *content )
106 {
107     *buffer++ = ((content->base & 0x0000ffff) << 16) |
108                  (content->limit & 0x0ffff);
109     *buffer = (content->base & 0xff000000) |
110               ((content->base & 0x00ff0000)>>16) |
111               (content->limit & 0xf0000) |
112               (content->type << 10) |
113               ((content->read_only == 0) << 9) |
114               ((content->seg_32bit != 0) << 22) |
115               ((content->limit_in_pages != 0) << 23) |
116               0xf000;
117 }
118
119
120 /***********************************************************************
121  *           LDT_GetEntry
122  *
123  * Retrieve an LDT entry.
124  */
125 int LDT_GetEntry( int entry, ldt_entry *content )
126 {
127     int ret = 0;
128
129     content->base           = ldt_copy[entry].base;
130     content->limit          = ldt_copy[entry].limit;
131     content->type           = (ldt_flags_copy[entry] & LDT_FLAGS_TYPE);
132     content->seg_32bit      = (ldt_flags_copy[entry] & LDT_FLAGS_32BIT) != 0;
133     content->read_only      = (ldt_flags_copy[entry] & LDT_FLAGS_READONLY) !=0;
134     content->limit_in_pages = (ldt_flags_copy[entry] & LDT_FLAGS_BIG) !=0;
135     if (content->limit_in_pages) content->limit >>= 12;
136     return ret;
137 }
138
139
140 /***********************************************************************
141  *           LDT_SetEntry
142  *
143  * Set an LDT entry.
144  */
145 int LDT_SetEntry( int entry, const ldt_entry *content )
146 {
147     int ret = 0;
148
149     dprintf_ldt(stddeb,
150           "LDT_SetEntry: entry=%04x base=%08lx limit=%05lx %s %d-bit flags=%c%c%c\n",
151           entry, content->base, content->limit,
152           content->limit_in_pages ? "pages" : "bytes",
153           content->seg_32bit ? 32 : 16,
154           content->read_only && (content->type & SEGMENT_CODE) ? '-' : 'r',
155           content->read_only || (content->type & SEGMENT_CODE) ? '-' : 'w',
156           (content->type & SEGMENT_CODE) ? 'x' : '-' );
157
158     /* Entry 0 must not be modified; its base and limit are always 0 */
159     if (!entry) return 0;
160
161 #ifdef __i386__
162
163 #ifdef linux
164     if (!__winelib)
165     {
166         struct modify_ldt_s ldt_info;
167
168         ldt_info.entry_number    = entry;
169         ldt_info.base_addr       = content->base;
170         ldt_info.limit           = content->limit;
171         ldt_info.seg_32bit       = content->seg_32bit != 0;
172         ldt_info.contents        = content->type;
173         ldt_info.read_exec_only  = content->read_only != 0;
174         ldt_info.limit_in_pages  = content->limit_in_pages != 0;
175         ldt_info.seg_not_present = 0;
176         /* Make sure the info will be accepted by the kernel */
177         /* This is ugly, but what can I do? */
178         if (content->type == SEGMENT_STACK)
179         {
180             /* FIXME */
181         }
182         else
183         {
184             if (ldt_info.base_addr >= 0xc0000000)
185             {
186                 fprintf( stderr, "LDT_SetEntry: invalid base addr %08lx\n",
187                          ldt_info.base_addr );
188                 return -1;
189             }
190             if (content->limit_in_pages)
191             {
192                 if ((ldt_info.limit << 12) + 0xfff >
193                                                0xc0000000 - ldt_info.base_addr)
194                     ldt_info.limit = (0xc0000000 - 0xfff - ldt_info.base_addr) >> 12;
195             }
196             else
197             {
198                 if (ldt_info.limit > 0xc0000000 - ldt_info.base_addr)
199                     ldt_info.limit = 0xc0000000 - ldt_info.base_addr;
200             }
201         }
202         if ((ret = modify_ldt(1, &ldt_info, sizeof(ldt_info))) < 0)
203             perror( "modify_ldt" );
204     }
205 #endif  /* linux */
206
207 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
208     if (!__winelib)
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             fprintf(stderr,
218                 "Did you reconfigure the kernel with \"options USER_LDT\"?\n");
219             exit(1);
220         }
221     }
222 #endif  /* __NetBSD__ || __FreeBSD__ || __OpenBSD__ */
223
224 #if defined(__svr4__) || defined(_SCO_DS)
225     if (!__winelib)
226     {
227         struct ssd ldt_mod;
228         int i;
229         ldt_mod.sel = ENTRY_TO_SELECTOR(entry) | 4;
230         ldt_mod.bo = content->base;
231         ldt_mod.ls = content->limit;
232         i = ((content->limit & 0xf0000) |
233              (content->type << 10) |
234              (((content->read_only != 0) ^ 1) << 9) |
235              ((content->seg_32bit != 0) << 22) |
236              ((content->limit_in_pages != 0)<< 23) |
237              (1<<15) |
238              0x7000);
239
240         ldt_mod.acc1 = (i & 0xff00) >> 8;
241         ldt_mod.acc2 = (i & 0xf00000) >> 20;
242
243         if (content->base == 0)
244         {
245             ldt_mod.acc1 =  0;
246             ldt_mod.acc2 = 0;
247         }
248         if ((ret = sysi86(SI86DSCR, &ldt_mod)) == -1) perror("sysi86");
249     }    
250 #endif
251
252 #endif  /* __i386__ */
253
254     if (ret < 0) return ret;
255     ldt_copy[entry].base = content->base;
256     if (!content->limit_in_pages) ldt_copy[entry].limit = content->limit;
257     else ldt_copy[entry].limit = (content->limit << 12) | 0x0fff;
258     ldt_flags_copy[entry] = (content->type & LDT_FLAGS_TYPE) |
259                             (content->read_only ? LDT_FLAGS_READONLY : 0) |
260                             (content->seg_32bit ? LDT_FLAGS_32BIT : 0) |
261                             (content->limit_in_pages ? LDT_FLAGS_BIG : 0) |
262                             (ldt_flags_copy[entry] & LDT_FLAGS_ALLOCATED);
263     return ret;
264 }
265
266
267 /***********************************************************************
268  *           LDT_Print
269  *
270  * Print the content of the LDT on stdout.
271  */
272 void LDT_Print( int start, int length )
273 {
274     int i;
275     char flags[3];
276
277     if (length == -1) length = LDT_SIZE - start;
278     for (i = start; i < start + length; i++)
279     {
280         if (!ldt_copy[i].base && !ldt_copy[i].limit) continue; /* Free entry */
281         if ((ldt_flags_copy[i] & LDT_FLAGS_TYPE) == SEGMENT_CODE)
282         {
283             flags[0] = (ldt_flags_copy[i] & LDT_FLAGS_EXECONLY) ? '-' : 'r';
284             flags[1] = '-';
285             flags[2] = 'x';
286         }
287         else
288         {
289             flags[0] = 'r';
290             flags[1] = (ldt_flags_copy[i] & LDT_FLAGS_READONLY) ? '-' : 'w';
291             flags[2] = '-';
292         }
293         printf("%04x: sel=%04x base=%08lx limit=%08lx %d-bit %c%c%c\n",
294                 i, ENTRY_TO_SELECTOR(i),
295                 ldt_copy[i].base, ldt_copy[i].limit,
296                 ldt_flags_copy[i] & LDT_FLAGS_32BIT ? 32 : 16,
297                 flags[0], flags[1], flags[2] );
298     }
299 }