Release 980503
[wine.git] / memory / ldt.c
blob4ebd231ba36d22bb970587f566224c5234491e9d
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 "debug.h"
15 #ifdef __i386__
17 #ifdef linux
18 #include <sys/syscall.h>
20 struct modify_ldt_s
22 unsigned int entry_number;
23 unsigned long base_addr;
24 unsigned int limit;
25 unsigned int seg_32bit : 1;
26 unsigned int contents : 2;
27 unsigned int read_exec_only : 1;
28 unsigned int limit_in_pages : 1;
29 unsigned int seg_not_present : 1;
32 static __inline__ int modify_ldt( int func, struct modify_ldt_s *ptr,
33 unsigned long count )
35 int res;
36 #ifdef __PIC__
37 __asm__ __volatile__( "pushl %%ebx\n\t"
38 "movl %2,%%ebx\n\t"
39 "int $0x80\n\t"
40 "popl %%ebx"
41 : "=a" (res)
42 : "0" (SYS_modify_ldt),
43 "g" (func),
44 "c" (ptr),
45 "d" (count) );
46 #else
47 __asm__ __volatile__("int $0x80"
48 : "=a" (res)
49 : "0" (SYS_modify_ldt),
50 "b" (func),
51 "c" (ptr),
52 "d" (count) );
53 #endif /* __PIC__ */
54 if (res >= 0) return res;
55 errno = -res;
56 return -1;
59 #endif /* linux */
61 #if defined(__svr4__) || defined(_SCO_DS)
62 #include <sys/sysi86.h>
63 #ifndef __sun__
64 #include <sys/seg.h>
65 #endif
66 #endif
68 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
69 #include <machine/segments.h>
71 extern int i386_get_ldt(int, union descriptor *, int);
72 extern int i386_set_ldt(int, union descriptor *, int);
73 #endif /* __NetBSD__ || __FreeBSD__ || __OpenBSD__ */
75 #endif /* __i386__ */
78 ldt_copy_entry ldt_copy[LDT_SIZE];
79 unsigned char ldt_flags_copy[LDT_SIZE];
82 /***********************************************************************
83 * LDT_BytesToEntry
85 * Convert the raw bytes of the descriptor to an ldt_entry structure.
87 void LDT_BytesToEntry( const unsigned long *buffer, ldt_entry *content )
89 content->base = (*buffer >> 16) & 0x0000ffff;
90 content->limit = *buffer & 0x0000ffff;
91 buffer++;
92 content->base |= (*buffer & 0xff000000) | ((*buffer << 16) & 0x00ff0000);
93 content->limit |= (*buffer & 0x000f0000);
94 content->type = (*buffer >> 10) & 3;
95 content->seg_32bit = (*buffer & 0x00400000) != 0;
96 content->read_only = (*buffer & 0x00000200) == 0;
97 content->limit_in_pages = (*buffer & 0x00800000) != 0;
101 /***********************************************************************
102 * LDT_EntryToBytes
104 * Convert an ldt_entry structure to the raw bytes of the descriptor.
106 void LDT_EntryToBytes( unsigned long *buffer, const ldt_entry *content )
108 *buffer++ = ((content->base & 0x0000ffff) << 16) |
109 (content->limit & 0x0ffff);
110 *buffer = (content->base & 0xff000000) |
111 ((content->base & 0x00ff0000)>>16) |
112 (content->limit & 0xf0000) |
113 (content->type << 10) |
114 ((content->read_only == 0) << 9) |
115 ((content->seg_32bit != 0) << 22) |
116 ((content->limit_in_pages != 0) << 23) |
117 0xf000;
121 /***********************************************************************
122 * LDT_GetEntry
124 * Retrieve an LDT entry.
126 int LDT_GetEntry( int entry, ldt_entry *content )
128 int ret = 0;
130 content->base = ldt_copy[entry].base;
131 content->limit = ldt_copy[entry].limit;
132 content->type = (ldt_flags_copy[entry] & LDT_FLAGS_TYPE);
133 content->seg_32bit = (ldt_flags_copy[entry] & LDT_FLAGS_32BIT) != 0;
134 content->read_only = (ldt_flags_copy[entry] & LDT_FLAGS_READONLY) !=0;
135 content->limit_in_pages = (ldt_flags_copy[entry] & LDT_FLAGS_BIG) !=0;
136 if (content->limit_in_pages) content->limit >>= 12;
137 return ret;
141 /***********************************************************************
142 * LDT_SetEntry
144 * Set an LDT entry.
146 int LDT_SetEntry( int entry, const ldt_entry *content )
148 int ret = 0;
150 TRACE(ldt, "entry=%04x base=%08lx limit=%05lx %s %d-bit "
151 "flags=%c%c%c\n", entry, content->base, content->limit,
152 content->limit_in_pages ? "pages" : "bytes",
153 content->seg_32bit ? 32 : 16,
154 content->read_only && (content->type & SEGMENT_CODE) ? '-' : 'r',
155 content->read_only || (content->type & SEGMENT_CODE) ? '-' : 'w',
156 (content->type & SEGMENT_CODE) ? 'x' : '-' );
158 /* Entry 0 must not be modified; its base and limit are always 0 */
159 if (!entry) return 0;
161 #ifdef __i386__
163 #ifdef linux
165 struct modify_ldt_s ldt_info;
167 ldt_info.entry_number = entry;
168 ldt_info.base_addr = content->base;
169 ldt_info.limit = content->limit;
170 ldt_info.seg_32bit = content->seg_32bit != 0;
171 ldt_info.contents = content->type;
172 ldt_info.read_exec_only = content->read_only != 0;
173 ldt_info.limit_in_pages = content->limit_in_pages != 0;
174 ldt_info.seg_not_present = 0;
175 /* Make sure the info will be accepted by the kernel */
176 /* This is ugly, but what can I do? */
177 if (content->type == SEGMENT_STACK)
179 /* FIXME */
181 else
183 if (ldt_info.base_addr >= 0xc0000000)
185 WARN(ldt, "Invalid base addr %08lx\n",
186 ldt_info.base_addr );
187 return -1;
189 if (content->limit_in_pages)
191 if ((ldt_info.limit << 12) + 0xfff >
192 0xc0000000 - ldt_info.base_addr)
193 ldt_info.limit = (0xc0000000 - 0xfff - ldt_info.base_addr) >> 12;
195 else
197 if (ldt_info.limit > 0xc0000000 - ldt_info.base_addr)
198 ldt_info.limit = 0xc0000000 - ldt_info.base_addr;
201 if ((ret = modify_ldt(1, &ldt_info, sizeof(ldt_info))) < 0)
202 perror( "modify_ldt" );
204 #endif /* linux */
206 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
208 long d[2];
210 LDT_EntryToBytes( d, content );
211 ret = i386_set_ldt(entry, (union descriptor *)d, 1);
212 if (ret < 0)
214 perror("i386_set_ldt");
215 MSG("Did you reconfigure the kernel with \"options USER_LDT\"?\n");
216 exit(1);
219 #endif /* __NetBSD__ || __FreeBSD__ || __OpenBSD__ */
221 #if defined(__svr4__) || defined(_SCO_DS)
223 struct ssd ldt_mod;
224 int i;
225 ldt_mod.sel = ENTRY_TO_SELECTOR(entry) | 4;
226 ldt_mod.bo = content->base;
227 ldt_mod.ls = content->limit;
228 i = ((content->limit & 0xf0000) |
229 (content->type << 10) |
230 (((content->read_only != 0) ^ 1) << 9) |
231 ((content->seg_32bit != 0) << 22) |
232 ((content->limit_in_pages != 0)<< 23) |
233 (1<<15) |
234 0x7000);
236 ldt_mod.acc1 = (i & 0xff00) >> 8;
237 ldt_mod.acc2 = (i & 0xf00000) >> 20;
239 if (content->base == 0)
241 ldt_mod.acc1 = 0;
242 ldt_mod.acc2 = 0;
244 if ((ret = sysi86(SI86DSCR, &ldt_mod)) == -1) perror("sysi86");
246 #endif
248 #endif /* __i386__ */
250 if (ret < 0) return ret;
251 ldt_copy[entry].base = content->base;
252 if (!content->limit_in_pages) ldt_copy[entry].limit = content->limit;
253 else ldt_copy[entry].limit = (content->limit << 12) | 0x0fff;
254 ldt_flags_copy[entry] = (content->type & LDT_FLAGS_TYPE) |
255 (content->read_only ? LDT_FLAGS_READONLY : 0) |
256 (content->seg_32bit ? LDT_FLAGS_32BIT : 0) |
257 (content->limit_in_pages ? LDT_FLAGS_BIG : 0) |
258 (ldt_flags_copy[entry] & LDT_FLAGS_ALLOCATED);
259 return ret;
263 /***********************************************************************
264 * LDT_Print
266 * Print the content of the LDT on stdout.
268 void LDT_Print( int start, int length )
270 int i;
271 char flags[3];
273 if (length == -1) length = LDT_SIZE - start;
274 for (i = start; i < start + length; i++)
276 if (!ldt_copy[i].base && !ldt_copy[i].limit) continue; /* Free entry */
277 if ((ldt_flags_copy[i] & LDT_FLAGS_TYPE) == SEGMENT_CODE)
279 flags[0] = (ldt_flags_copy[i] & LDT_FLAGS_EXECONLY) ? '-' : 'r';
280 flags[1] = '-';
281 flags[2] = 'x';
283 else
285 flags[0] = 'r';
286 flags[1] = (ldt_flags_copy[i] & LDT_FLAGS_READONLY) ? '-' : 'w';
287 flags[2] = '-';
289 TRACE(ldt,"%04x: sel=%04x base=%08lx limit=%08lx %d-bit %c%c%c\n",
290 i, ENTRY_TO_SELECTOR(i),
291 ldt_copy[i].base, ldt_copy[i].limit,
292 ldt_flags_copy[i] & LDT_FLAGS_32BIT ? 32 : 16,
293 flags[0], flags[1], flags[2] );