Release 970804
[wine/multimedia.git] / memory / ldt.c
blobde1cad0a5df252f3437c6f1c8f6d539c6de2d5a1
1 /*
2 * LDT manipulation functions
4 * Copyright 1993 Robert J. Amstadt
5 * Copyright 1995 Alexandre Julliard
6 */
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"
16 #ifdef __i386__
18 #ifdef linux
19 #include <sys/syscall.h>
21 struct modify_ldt_s
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;
33 static __inline__ int modify_ldt( int func, struct modify_ldt_s *ptr,
34 unsigned long count )
36 int res;
37 __asm__ __volatile__("int $0x80"
38 : "=a" (res)
39 : "0" (SYS_modify_ldt),
40 "b" (func),
41 "c" (ptr),
42 "d" (count) );
43 if (res >= 0) return res;
44 errno = -res;
45 return -1;
48 #endif /* linux */
50 #if defined(__svr4__) || defined(_SCO_DS)
51 #include <sys/sysi86.h>
52 #include <sys/seg.h>
53 #endif
55 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
56 #include <machine/segments.h>
58 extern int i386_get_ldt(int, union descriptor *, int);
59 extern int i386_set_ldt(int, union descriptor *, int);
60 #endif /* __NetBSD__ || __FreeBSD__ || __OpenBSD__ */
62 #endif /* __i386__ */
65 ldt_copy_entry ldt_copy[LDT_SIZE];
66 unsigned char ldt_flags_copy[LDT_SIZE];
69 /***********************************************************************
70 * LDT_BytesToEntry
72 * Convert the raw bytes of the descriptor to an ldt_entry structure.
74 void LDT_BytesToEntry( const unsigned long *buffer, ldt_entry *content )
76 content->base = (*buffer >> 16) & 0x0000ffff;
77 content->limit = *buffer & 0x0000ffff;
78 buffer++;
79 content->base |= (*buffer & 0xff000000) | ((*buffer << 16) & 0x00ff0000);
80 content->limit |= (*buffer & 0x000f0000);
81 content->type = (*buffer >> 10) & 3;
82 content->seg_32bit = (*buffer & 0x00400000) != 0;
83 content->read_only = (*buffer & 0x00000200) == 0;
84 content->limit_in_pages = (*buffer & 0x00800000) != 0;
88 /***********************************************************************
89 * LDT_EntryToBytes
91 * Convert an ldt_entry structure to the raw bytes of the descriptor.
93 void LDT_EntryToBytes( unsigned long *buffer, const ldt_entry *content )
95 *buffer++ = ((content->base & 0x0000ffff) << 16) |
96 (content->limit & 0x0ffff);
97 *buffer = (content->base & 0xff000000) |
98 ((content->base & 0x00ff0000)>>16) |
99 (content->limit & 0xf0000) |
100 (content->type << 10) |
101 ((content->read_only == 0) << 9) |
102 ((content->seg_32bit != 0) << 22) |
103 ((content->limit_in_pages != 0) << 23) |
104 0xf000;
108 /***********************************************************************
109 * LDT_GetEntry
111 * Retrieve an LDT entry.
113 int LDT_GetEntry( int entry, ldt_entry *content )
115 int ret = 0;
117 content->base = ldt_copy[entry].base;
118 content->limit = ldt_copy[entry].limit;
119 content->type = (ldt_flags_copy[entry] & LDT_FLAGS_TYPE);
120 content->seg_32bit = (ldt_flags_copy[entry] & LDT_FLAGS_32BIT) != 0;
121 content->read_only = (ldt_flags_copy[entry] & LDT_FLAGS_READONLY) !=0;
122 content->limit_in_pages = (ldt_flags_copy[entry] & LDT_FLAGS_BIG) !=0;
123 if (content->limit_in_pages) content->limit >>= 12;
124 return ret;
128 /***********************************************************************
129 * LDT_SetEntry
131 * Set an LDT entry.
133 int LDT_SetEntry( int entry, const ldt_entry *content )
135 int ret = 0;
137 dprintf_ldt(stddeb,
138 "LDT_SetEntry: entry=%04x base=%08lx limit=%05lx %s %d-bit flags=%c%c%c\n",
139 entry, content->base, content->limit,
140 content->limit_in_pages ? "pages" : "bytes",
141 content->seg_32bit ? 32 : 16,
142 content->read_only && (content->type & SEGMENT_CODE) ? '-' : 'r',
143 content->read_only || (content->type & SEGMENT_CODE) ? '-' : 'w',
144 (content->type & SEGMENT_CODE) ? 'x' : '-' );
146 /* Entry 0 must not be modified; its base and limit are always 0 */
147 if (!entry) return 0;
149 #ifdef __i386__
151 #ifdef linux
152 if (!__winelib)
154 struct modify_ldt_s ldt_info;
156 ldt_info.entry_number = entry;
157 ldt_info.base_addr = content->base;
158 ldt_info.limit = content->limit;
159 ldt_info.seg_32bit = content->seg_32bit != 0;
160 ldt_info.contents = content->type;
161 ldt_info.read_exec_only = content->read_only != 0;
162 ldt_info.limit_in_pages = content->limit_in_pages != 0;
163 ldt_info.seg_not_present = 0;
164 /* Make sure the info will be accepted by the kernel */
165 /* This is ugly, but what can I do? */
166 if (content->type == SEGMENT_STACK)
168 /* FIXME */
170 else
172 if (ldt_info.base_addr >= 0xc0000000)
174 fprintf( stderr, "LDT_SetEntry: invalid base addr %08lx\n",
175 ldt_info.base_addr );
176 return -1;
178 if (content->limit_in_pages)
180 if ((ldt_info.limit << 12) + 0xfff >
181 0xc0000000 - ldt_info.base_addr)
182 ldt_info.limit = (0xc0000000 - 0xfff - ldt_info.base_addr) >> 12;
184 else
186 if (ldt_info.limit > 0xc0000000 - ldt_info.base_addr)
187 ldt_info.limit = 0xc0000000 - ldt_info.base_addr;
190 if ((ret = modify_ldt(1, &ldt_info, sizeof(ldt_info))) < 0)
191 perror( "modify_ldt" );
193 #endif /* linux */
195 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
196 if (!__winelib)
198 long d[2];
200 LDT_EntryToBytes( d, content );
201 ret = i386_set_ldt(entry, (union descriptor *)d, 1);
202 if (ret < 0)
204 perror("i386_set_ldt");
205 fprintf(stderr,
206 "Did you reconfigure the kernel with \"options USER_LDT\"?\n");
207 exit(1);
210 #endif /* __NetBSD__ || __FreeBSD__ || __OpenBSD__ */
212 #if defined(__svr4__) || defined(_SCO_DS)
213 if (!__winelib)
215 struct ssd ldt_mod;
216 int i;
217 ldt_mod.sel = ENTRY_TO_SELECTOR(entry) | 4;
218 ldt_mod.bo = content->base;
219 ldt_mod.ls = content->limit;
220 i = ((content->limit & 0xf0000) |
221 (content->type << 10) |
222 (((content->read_only != 0) ^ 1) << 9) |
223 ((content->seg_32bit != 0) << 22) |
224 ((content->limit_in_pages != 0)<< 23) |
225 (1<<15) |
226 0x7000);
228 ldt_mod.acc1 = (i & 0xff00) >> 8;
229 ldt_mod.acc2 = (i & 0xf00000) >> 20;
231 if (content->base == 0)
233 ldt_mod.acc1 = 0;
234 ldt_mod.acc2 = 0;
236 if ((ret = sysi86(SI86DSCR, &ldt_mod)) == -1) perror("sysi86");
238 #endif
240 #endif /* __i386__ */
242 if (ret < 0) return ret;
243 ldt_copy[entry].base = content->base;
244 if (!content->limit_in_pages) ldt_copy[entry].limit = content->limit;
245 else ldt_copy[entry].limit = (content->limit << 12) | 0x0fff;
246 ldt_flags_copy[entry] = (content->type & LDT_FLAGS_TYPE) |
247 (content->read_only ? LDT_FLAGS_READONLY : 0) |
248 (content->seg_32bit ? LDT_FLAGS_32BIT : 0) |
249 (content->limit_in_pages ? LDT_FLAGS_BIG : 0) |
250 (ldt_flags_copy[entry] & LDT_FLAGS_ALLOCATED);
251 return ret;
255 /***********************************************************************
256 * LDT_Print
258 * Print the content of the LDT on stdout.
260 void LDT_Print( int start, int length )
262 int i;
263 char flags[3];
265 if (length == -1) length = LDT_SIZE - start;
266 for (i = start; i < start + length; i++)
268 if (!ldt_copy[i].base && !ldt_copy[i].limit) continue; /* Free entry */
269 if ((ldt_flags_copy[i] & LDT_FLAGS_TYPE) == SEGMENT_CODE)
271 flags[0] = (ldt_flags_copy[i] & LDT_FLAGS_EXECONLY) ? '-' : 'r';
272 flags[1] = '-';
273 flags[2] = 'x';
275 else
277 flags[0] = 'r';
278 flags[1] = (ldt_flags_copy[i] & LDT_FLAGS_READONLY) ? '-' : 'w';
279 flags[2] = '-';
281 printf("%04x: sel=%04x base=%08lx limit=%08lx %d-bit %c%c%c\n",
282 i, ENTRY_TO_SELECTOR(i),
283 ldt_copy[i].base, ldt_copy[i].limit,
284 ldt_flags_copy[i] & LDT_FLAGS_32BIT ? 32 : 16,
285 flags[0], flags[1], flags[2] );