Release 960728
[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 linux
17 #include <linux/unistd.h>
18 #include <linux/head.h>
19 #include <linux/ldt.h>
20
21 _syscall3(int, modify_ldt, int, func, void *, ptr, unsigned long, bytecount)
22 #endif  /* linux */
23
24 #if defined(__svr4__) || defined(_SCO_DS)
25 #include <sys/sysi86.h>
26 #include <sys/seg.h>
27 #endif
28
29 #if defined(__NetBSD__) || defined(__FreeBSD__)
30 #include <machine/segments.h>
31
32 extern int i386_get_ldt(int, union descriptor *, int);
33 extern int i386_set_ldt(int, union descriptor *, int);
34 #endif  /* __NetBSD__ || __FreeBSD__ */
35
36
37 ldt_copy_entry ldt_copy[LDT_SIZE];
38 unsigned char ldt_flags_copy[LDT_SIZE];
39
40         
41 /***********************************************************************
42  *           LDT_BytesToEntry
43  *
44  * Convert the raw bytes of the descriptor to an ldt_entry structure.
45  */
46 void LDT_BytesToEntry( const unsigned long *buffer, ldt_entry *content )
47 {
48     content->base  = (*buffer >> 16) & 0x0000ffff;
49     content->limit = *buffer & 0x0000ffff;
50     buffer++;
51     content->base  |= (*buffer & 0xff000000) | ((*buffer << 16) & 0x00ff0000);
52     content->limit |= (*buffer & 0x000f0000);
53     content->type           = (*buffer >> 10) & 3;
54     content->seg_32bit      = (*buffer & 0x00400000) != 0;
55     content->read_only      = (*buffer & 0x00000200) == 0;
56     content->limit_in_pages = (*buffer & 0x00800000) != 0;
57 }
58
59
60 /***********************************************************************
61  *           LDT_EntryToBytes
62  *
63  * Convert an ldt_entry structure to the raw bytes of the descriptor.
64  */
65 void LDT_EntryToBytes( unsigned long *buffer, const ldt_entry *content )
66 {
67     *buffer++ = ((content->base & 0x0000ffff) << 16) |
68                  (content->limit & 0x0ffff);
69     *buffer = (content->base & 0xff000000) |
70               ((content->base & 0x00ff0000)>>16) |
71               (content->limit & 0xf0000) |
72               (content->type << 10) |
73               ((content->read_only == 0) << 9) |
74               ((content->seg_32bit != 0) << 22) |
75               ((content->limit_in_pages != 0) << 23) |
76               0xf000;
77 }
78
79
80 /***********************************************************************
81  *           LDT_GetEntry
82  *
83  * Retrieve an LDT entry.
84  */
85 int LDT_GetEntry( int entry, ldt_entry *content )
86 {
87     int ret = 0;
88
89     content->base           = ldt_copy[entry].base;
90     content->limit          = ldt_copy[entry].limit;
91     content->type           = (ldt_flags_copy[entry] & LDT_FLAGS_TYPE);
92     content->seg_32bit      = (ldt_flags_copy[entry] & LDT_FLAGS_32BIT) != 0;
93     content->read_only      = (ldt_flags_copy[entry] & LDT_FLAGS_READONLY) !=0;
94     content->limit_in_pages = (ldt_flags_copy[entry] & LDT_FLAGS_BIG) !=0;
95     if (content->limit_in_pages) content->limit >>= 12;
96     return ret;
97 }
98
99
100 /***********************************************************************
101  *           LDT_SetEntry
102  *
103  * Set an LDT entry.
104  */
105 int LDT_SetEntry( int entry, const ldt_entry *content )
106 {
107     int ret = 0;
108
109     dprintf_ldt(stddeb,
110           "LDT_SetEntry: entry=%04x base=%08lx limit=%05lx %s %d-bit flags=%c%c%c\n",
111           entry, content->base, content->limit,
112           content->limit_in_pages ? "pages" : "bytes",
113           content->seg_32bit ? 32 : 16,
114           content->read_only && (content->type & SEGMENT_CODE) ? '-' : 'r',
115           content->read_only || (content->type & SEGMENT_CODE) ? '-' : 'w',
116           (content->type & SEGMENT_CODE) ? 'x' : '-' );
117
118     /* Entry 0 must not be modified; its base and limit are always 0 */
119     if (!entry) return 0;
120
121 #ifdef linux
122     if (!__winelib)
123     {
124         struct modify_ldt_ldt_s ldt_info;
125
126         /* Clear all unused bits (like seg_not_present) */
127         memset( &ldt_info, 0, sizeof(ldt_info) );
128         ldt_info.entry_number   = entry;
129         ldt_info.base_addr      = content->base;
130         ldt_info.limit          = content->limit;
131         ldt_info.seg_32bit      = content->seg_32bit != 0;
132         ldt_info.contents       = content->type;
133         ldt_info.read_exec_only = content->read_only != 0;
134         ldt_info.limit_in_pages = content->limit_in_pages != 0;
135         /* Make sure the info will be accepted by the kernel */
136         /* This is ugly, but what can I do? */
137         if (content->type == SEGMENT_STACK)
138         {
139             /* FIXME */
140         }
141         else
142         {
143             if (ldt_info.base_addr >= 0xc0000000)
144             {
145                 fprintf( stderr, "LDT_SetEntry: invalid base addr %08lx\n",
146                          ldt_info.base_addr );
147                 return -1;
148             }
149             if (content->limit_in_pages)
150             {
151                 if ((ldt_info.limit << 12) + 0xfff >
152                                                0xc0000000 - ldt_info.base_addr)
153                     ldt_info.limit = (0xc0000000 - 0xfff - ldt_info.base_addr) >> 12;
154             }
155             else
156             {
157                 if (ldt_info.limit > 0xc0000000 - ldt_info.base_addr)
158                     ldt_info.limit = 0xc0000000 - ldt_info.base_addr;
159             }
160         }
161         if ((ret = modify_ldt(1, &ldt_info, sizeof(ldt_info))) < 0)
162             perror( "modify_ldt" );
163     }
164 #endif  /* linux */
165
166 #if defined(__NetBSD__) || defined(__FreeBSD__)
167     if (!__winelib)
168     {
169         long d[2];
170
171         LDT_EntryToBytes( d, content );
172         ret = i386_set_ldt(entry, (union descriptor *)d, 1);
173         if (ret < 0)
174         {
175             perror("i386_set_ldt");
176             fprintf(stderr,
177                 "Did you reconfigure the kernel with \"options USER_LDT\"?\n");
178             exit(1);
179         }
180     }
181 #endif  /* __NetBSD__ || __FreeBSD__ */
182 #if defined(__svr4__) || defined(_SCO_DS)
183     if (!__winelib)
184     {
185         struct ssd ldt_mod;
186         int i;
187         ldt_mod.sel = ENTRY_TO_SELECTOR(entry) | 4;
188         ldt_mod.bo = content->base;
189         ldt_mod.ls = content->limit;
190         i = ((content->limit & 0xf0000) |
191              (content->type << 10) |
192              (((content->read_only != 0) ^ 1) << 9) |
193              ((content->seg_32bit != 0) << 22) |
194              ((content->limit_in_pages != 0)<< 23) |
195              (1<<15) |
196              0x7000);
197
198         ldt_mod.acc1 = (i & 0xff00) >> 8;
199         ldt_mod.acc2 = (i & 0xf00000) >> 20;
200
201         if (content->base == 0)
202         {
203             ldt_mod.acc1 =  0;
204             ldt_mod.acc2 = 0;
205         }
206         if ((ret = sysi86(SI86DSCR, &ldt_mod)) == -1) perror("sysi86");
207     }    
208 #endif
209
210     if (ret < 0) return ret;
211     ldt_copy[entry].base = content->base;
212     if (!content->limit_in_pages) ldt_copy[entry].limit = content->limit;
213     else ldt_copy[entry].limit = (content->limit << 12) | 0x0fff;
214     ldt_flags_copy[entry] = (content->type & LDT_FLAGS_TYPE) |
215                             (content->read_only ? LDT_FLAGS_READONLY : 0) |
216                             (content->seg_32bit ? LDT_FLAGS_32BIT : 0) |
217                             (content->limit_in_pages ? LDT_FLAGS_BIG : 0) |
218                             (ldt_flags_copy[entry] & LDT_FLAGS_ALLOCATED);
219     return ret;
220 }
221
222
223 /***********************************************************************
224  *           LDT_Print
225  *
226  * Print the content of the LDT on stdout.
227  */
228 void LDT_Print( int start, int length )
229 {
230     int i;
231     char flags[3];
232
233     if (length == -1) length = LDT_SIZE - start;
234     for (i = start; i < start + length; i++)
235     {
236         if (!ldt_copy[i].base && !ldt_copy[i].limit) continue; /* Free entry */
237         if ((ldt_flags_copy[i] & LDT_FLAGS_TYPE) == SEGMENT_CODE)
238         {
239             flags[0] = (ldt_flags_copy[i] & LDT_FLAGS_EXECONLY) ? '-' : 'r';
240             flags[1] = '-';
241             flags[2] = 'x';
242         }
243         else
244         {
245             flags[0] = 'r';
246             flags[1] = (ldt_flags_copy[i] & LDT_FLAGS_READONLY) ? '-' : 'w';
247             flags[2] = '-';
248         }
249         printf("%04x: sel=%04x base=%08lx limit=%08lx %d-bit %c%c%c\n",
250                 i, ENTRY_TO_SELECTOR(i),
251                 ldt_copy[i].base, ldt_copy[i].limit,
252                 ldt_flags_copy[i] & LDT_FLAGS_32BIT ? 32 : 16,
253                 flags[0], flags[1], flags[2] );
254     }
255 }