Release 960717
[wine/multimedia.git] / memory / ldt.c
blob35116372e63d3236b6fde0a732e5d412de726248
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 #ifndef WINELIB
18 #ifdef linux
19 #include <linux/unistd.h>
20 #include <linux/head.h>
21 #include <linux/ldt.h>
23 _syscall3(int, modify_ldt, int, func, void *, ptr, unsigned long, bytecount)
24 #endif /* linux */
25 #if defined(__svr4__) || defined(_SCO_DS)
26 #include <sys/sysi86.h>
27 #include <sys/seg.h>
28 #endif
30 #if defined(__NetBSD__) || defined(__FreeBSD__)
31 #include <machine/segments.h>
33 extern int i386_get_ldt(int, union descriptor *, int);
34 extern int i386_set_ldt(int, union descriptor *, int);
35 #endif /* __NetBSD__ || __FreeBSD__ */
37 #endif /* ifndef WINELIB */
40 ldt_copy_entry ldt_copy[LDT_SIZE];
41 unsigned char ldt_flags_copy[LDT_SIZE];
44 /***********************************************************************
45 * LDT_BytesToEntry
47 * Convert the raw bytes of the descriptor to an ldt_entry structure.
49 void LDT_BytesToEntry( const unsigned long *buffer, ldt_entry *content )
51 content->base = (*buffer >> 16) & 0x0000ffff;
52 content->limit = *buffer & 0x0000ffff;
53 buffer++;
54 content->base |= (*buffer & 0xff000000) | ((*buffer << 16) & 0x00ff0000);
55 content->limit |= (*buffer & 0x000f0000);
56 content->type = (*buffer >> 10) & 3;
57 content->seg_32bit = (*buffer & 0x00400000) != 0;
58 content->read_only = (*buffer & 0x00000200) == 0;
59 content->limit_in_pages = (*buffer & 0x00800000) != 0;
63 /***********************************************************************
64 * LDT_EntryToBytes
66 * Convert an ldt_entry structure to the raw bytes of the descriptor.
68 void LDT_EntryToBytes( unsigned long *buffer, const ldt_entry *content )
70 *buffer++ = ((content->base & 0x0000ffff) << 16) |
71 (content->limit & 0x0ffff);
72 *buffer = (content->base & 0xff000000) |
73 ((content->base & 0x00ff0000)>>16) |
74 (content->limit & 0xf0000) |
75 (content->type << 10) |
76 ((content->read_only == 0) << 9) |
77 ((content->seg_32bit != 0) << 22) |
78 ((content->limit_in_pages != 0) << 23) |
79 0xf000;
83 /***********************************************************************
84 * LDT_GetEntry
86 * Retrieve an LDT entry.
88 int LDT_GetEntry( int entry, ldt_entry *content )
90 int ret = 0;
92 content->base = ldt_copy[entry].base;
93 content->limit = ldt_copy[entry].limit;
94 content->type = (ldt_flags_copy[entry] & LDT_FLAGS_TYPE);
95 content->seg_32bit = (ldt_flags_copy[entry] & LDT_FLAGS_32BIT) != 0;
96 content->read_only = (ldt_flags_copy[entry] & LDT_FLAGS_READONLY) !=0;
97 content->limit_in_pages = (ldt_flags_copy[entry] & LDT_FLAGS_BIG) !=0;
98 if (content->limit_in_pages) content->limit >>= 12;
99 return ret;
103 /***********************************************************************
104 * LDT_SetEntry
106 * Set an LDT entry.
108 int LDT_SetEntry( int entry, const ldt_entry *content )
110 int ret = 0;
112 dprintf_ldt(stddeb,
113 "LDT_SetEntry: entry=%04x base=%08lx limit=%05lx %s %d-bit flags=%c%c%c\n",
114 entry, content->base, content->limit,
115 content->limit_in_pages ? "pages" : "bytes",
116 content->seg_32bit ? 32 : 16,
117 content->read_only && (content->type & SEGMENT_CODE) ? '-' : 'r',
118 content->read_only || (content->type & SEGMENT_CODE) ? '-' : 'w',
119 (content->type & SEGMENT_CODE) ? 'x' : '-' );
121 /* Entry 0 must not be modified; its base and limit are always 0 */
122 if (!entry) return 0;
124 #ifndef WINELIB
125 #ifdef linux
127 struct modify_ldt_ldt_s ldt_info;
129 /* Clear all unused bits (like seg_not_present) */
130 memset( &ldt_info, 0, sizeof(ldt_info) );
131 ldt_info.entry_number = entry;
132 ldt_info.base_addr = content->base;
133 ldt_info.limit = content->limit;
134 ldt_info.seg_32bit = content->seg_32bit != 0;
135 ldt_info.contents = content->type;
136 ldt_info.read_exec_only = content->read_only != 0;
137 ldt_info.limit_in_pages = content->limit_in_pages != 0;
138 /* Make sure the info will be accepted by the kernel */
139 /* This is ugly, but what can I do? */
140 if (content->type == SEGMENT_STACK)
142 /* FIXME */
144 else
146 if (ldt_info.base_addr >= 0xc0000000)
148 fprintf( stderr, "LDT_SetEntry: invalid base addr %08lx\n",
149 ldt_info.base_addr );
150 return -1;
152 if (content->limit_in_pages)
154 if ((ldt_info.limit << 12) + 0xfff >
155 0xc0000000 - ldt_info.base_addr)
156 ldt_info.limit = (0xc0000000 - 0xfff - ldt_info.base_addr) >> 12;
158 else
160 if (ldt_info.limit > 0xc0000000 - ldt_info.base_addr)
161 ldt_info.limit = 0xc0000000 - ldt_info.base_addr;
164 if ((ret = modify_ldt(1, &ldt_info, sizeof(ldt_info))) < 0)
165 perror( "modify_ldt" );
167 #endif /* linux */
169 #if defined(__NetBSD__) || defined(__FreeBSD__)
171 long d[2];
173 LDT_EntryToBytes( d, content );
174 ret = i386_set_ldt(entry, (union descriptor *)d, 1);
175 if (ret < 0)
177 perror("i386_set_ldt");
178 fprintf(stderr,
179 "Did you reconfigure the kernel with \"options USER_LDT\"?\n");
180 exit(1);
183 #endif /* __NetBSD__ || __FreeBSD__ */
184 #if defined(__svr4__) || defined(_SCO_DS)
186 struct ssd ldt_mod;
187 int i;
188 ldt_mod.sel = ENTRY_TO_SELECTOR(entry) | 4;
189 ldt_mod.bo = content->base;
190 ldt_mod.ls = content->limit;
191 i = (content->limit & 0xf0000) |
192 (content->type << 10) |
193 (((content->read_only != 0) ^ 1) << 9) |
194 ((content->seg_32bit != 0) << 22) |
195 ((content->limit_in_pages != 0)<< 23) |
196 (1<<15) |
197 0x7000;
199 ldt_mod.acc1 = (i & 0xff00) >> 8;
200 ldt_mod.acc2 = (i & 0xf00000) >> 20;
203 if (content->base == 0)
205 ldt_mod.acc1 = 0;
206 ldt_mod.acc2 = 0;
208 if ((i = sysi86(SI86DSCR, &ldt_mod)) == -1)
209 perror("sysi86");
212 #endif
213 #endif /* ifndef WINELIB */
215 if (ret < 0) return ret;
216 ldt_copy[entry].base = content->base;
217 if (!content->limit_in_pages) ldt_copy[entry].limit = content->limit;
218 else ldt_copy[entry].limit = (content->limit << 12) | 0x0fff;
219 ldt_flags_copy[entry] = (content->type & LDT_FLAGS_TYPE) |
220 (content->read_only ? LDT_FLAGS_READONLY : 0) |
221 (content->seg_32bit ? LDT_FLAGS_32BIT : 0) |
222 (content->limit_in_pages ? LDT_FLAGS_BIG : 0) |
223 (ldt_flags_copy[entry] & LDT_FLAGS_ALLOCATED);
224 return ret;
228 /***********************************************************************
229 * LDT_Print
231 * Print the content of the LDT on stdout.
233 void LDT_Print( int start, int length )
235 int i;
236 char flags[3];
238 if (length == -1) length = LDT_SIZE - start;
239 for (i = start; i < start + length; i++)
241 if (!ldt_copy[i].base && !ldt_copy[i].limit) continue; /* Free entry */
242 if ((ldt_flags_copy[i] & LDT_FLAGS_TYPE) == SEGMENT_CODE)
244 flags[0] = (ldt_flags_copy[i] & LDT_FLAGS_EXECONLY) ? '-' : 'r';
245 flags[1] = '-';
246 flags[2] = 'x';
248 else
250 flags[0] = 'r';
251 flags[1] = (ldt_flags_copy[i] & LDT_FLAGS_READONLY) ? '-' : 'w';
252 flags[2] = '-';
254 printf("%04x: sel=%04x base=%08lx limit=%08lx %d-bit %c%c%c\n",
255 i, ENTRY_TO_SELECTOR(i),
256 ldt_copy[i].base, ldt_copy[i].limit,
257 ldt_flags_copy[i] & LDT_FLAGS_32BIT ? 32 : 16,
258 flags[0], flags[1], flags[2] );