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