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