- Added missing configuration #if:s and #includes:s.
[wine.git] / memory / ldt.c
blob8130feabdbda2822dec36fd25e1e807a90c36013
1 /*
2 * LDT manipulation functions
4 * Copyright 1993 Robert J. Amstadt
5 * Copyright 1995 Alexandre Julliard
6 */
8 #include "config.h"
10 #include <stdlib.h>
11 #include <string.h>
12 #include <errno.h>
13 #include "ldt.h"
14 #include "debugtools.h"
16 DEFAULT_DEBUG_CHANNEL(ldt)
18 #ifdef __i386__
20 #ifdef linux
22 #ifdef HAVE_SYS_SYSCALL_H
23 # include <sys/syscall.h>
24 #endif
26 struct modify_ldt_s
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;
38 static inline int modify_ldt( int func, struct modify_ldt_s *ptr,
39 unsigned long count )
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;
65 #endif /* linux */
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
75 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
76 #include <machine/segments.h>
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__ */
82 #endif /* __i386__ */
85 ldt_copy_entry ldt_copy[LDT_SIZE];
86 unsigned char ldt_flags_copy[LDT_SIZE];
89 /***********************************************************************
90 * LDT_BytesToEntry
92 * Convert the raw bytes of the descriptor to an ldt_entry structure.
94 void LDT_BytesToEntry( const unsigned long *buffer, ldt_entry *content )
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;
108 /***********************************************************************
109 * LDT_EntryToBytes
111 * Convert an ldt_entry structure to the raw bytes of the descriptor.
113 void LDT_EntryToBytes( unsigned long *buffer, const ldt_entry *content )
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;
128 /***********************************************************************
129 * LDT_GetEntry
131 * Retrieve an LDT entry.
133 int LDT_GetEntry( int entry, ldt_entry *content )
135 int ret = 0;
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;
148 /***********************************************************************
149 * LDT_SetEntry
151 * Set an LDT entry.
153 int LDT_SetEntry( int entry, const ldt_entry *content )
155 int ret = 0;
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' : '-' );
165 /* Entry 0 must not be modified; its base and limit are always 0 */
166 if (!entry) return 0;
168 #ifdef __i386__
170 #ifdef linux
172 struct modify_ldt_s ldt_info;
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)
186 /* FIXME */
188 else
190 if (ldt_info.base_addr >= 0xc0000000)
192 WARN("Invalid base addr %08lx\n",
193 ldt_info.base_addr );
194 return -1;
196 if (content->limit_in_pages)
198 if ((ldt_info.limit << 12) + 0xfff >
199 0xc0000000 - ldt_info.base_addr)
200 ldt_info.limit = (0xc0000000 - 0xfff - ldt_info.base_addr) >> 12;
202 else
204 if (ldt_info.limit > 0xc0000000 - ldt_info.base_addr)
205 ldt_info.limit = 0xc0000000 - ldt_info.base_addr;
208 if ((ret = modify_ldt(1, &ldt_info, sizeof(ldt_info))) < 0)
209 perror( "modify_ldt" );
211 #endif /* linux */
213 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
215 long d[2];
217 LDT_EntryToBytes( d, content );
218 ret = i386_set_ldt(entry, (union descriptor *)d, 1);
219 if (ret < 0)
221 perror("i386_set_ldt");
222 MESSAGE("Did you reconfigure the kernel with \"options USER_LDT\"?\n");
223 exit(1);
226 #endif /* __NetBSD__ || __FreeBSD__ || __OpenBSD__ */
228 #if defined(__svr4__) || defined(_SCO_DS)
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);
243 ldt_mod.acc1 = (i & 0xff00) >> 8;
244 ldt_mod.acc2 = (i & 0xf00000) >> 20;
246 if (content->base == 0)
248 ldt_mod.acc1 = 0;
249 ldt_mod.acc2 = 0;
251 if ((ret = sysi86(SI86DSCR, &ldt_mod)) == -1) perror("sysi86");
253 #endif
255 #endif /* __i386__ */
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;
270 /***********************************************************************
271 * LDT_Print
273 * Print the content of the LDT on stdout.
275 void LDT_Print( int start, int length )
277 int i;
278 char flags[3];
280 if (length == -1) length = LDT_SIZE - start;
281 for (i = start; i < start + length; i++)
283 if (!ldt_copy[i].base && !ldt_copy[i].limit) continue; /* Free entry */
284 if ((ldt_flags_copy[i] & LDT_FLAGS_TYPE) == SEGMENT_CODE)
286 flags[0] = (ldt_flags_copy[i] & LDT_FLAGS_EXECONLY) ? '-' : 'r';
287 flags[1] = '-';
288 flags[2] = 'x';
290 else
292 flags[0] = 'r';
293 flags[1] = (ldt_flags_copy[i] & LDT_FLAGS_READONLY) ? '-' : 'w';
294 flags[2] = '-';
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] );