Release 961013
[wine.git] / memory / ldt.c
blob0d9200cb756b77e8be455ac482a85016e6ae7f47
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 linux
17 #include <syscall.h>
19 struct modify_ldt_s
21 unsigned int entry_number;
22 unsigned long base_addr;
23 unsigned int limit;
24 unsigned int seg_32bit : 1;
25 unsigned int contents : 2;
26 unsigned int read_exec_only : 1;
27 unsigned int limit_in_pages : 1;
28 unsigned int seg_not_present : 1;
31 static __inline__ _syscall3(int, modify_ldt, int, func, void *, ptr,
32 unsigned long, bytecount);
33 #endif /* linux */
35 #if defined(__svr4__) || defined(_SCO_DS)
36 #include <sys/sysi86.h>
37 #include <sys/seg.h>
38 #endif
40 #if defined(__NetBSD__) || defined(__FreeBSD__)
41 #include <machine/segments.h>
43 extern int i386_get_ldt(int, union descriptor *, int);
44 extern int i386_set_ldt(int, union descriptor *, int);
45 #endif /* __NetBSD__ || __FreeBSD__ */
48 ldt_copy_entry ldt_copy[LDT_SIZE];
49 unsigned char ldt_flags_copy[LDT_SIZE];
52 /***********************************************************************
53 * LDT_BytesToEntry
55 * Convert the raw bytes of the descriptor to an ldt_entry structure.
57 void LDT_BytesToEntry( const unsigned long *buffer, ldt_entry *content )
59 content->base = (*buffer >> 16) & 0x0000ffff;
60 content->limit = *buffer & 0x0000ffff;
61 buffer++;
62 content->base |= (*buffer & 0xff000000) | ((*buffer << 16) & 0x00ff0000);
63 content->limit |= (*buffer & 0x000f0000);
64 content->type = (*buffer >> 10) & 3;
65 content->seg_32bit = (*buffer & 0x00400000) != 0;
66 content->read_only = (*buffer & 0x00000200) == 0;
67 content->limit_in_pages = (*buffer & 0x00800000) != 0;
71 /***********************************************************************
72 * LDT_EntryToBytes
74 * Convert an ldt_entry structure to the raw bytes of the descriptor.
76 void LDT_EntryToBytes( unsigned long *buffer, const ldt_entry *content )
78 *buffer++ = ((content->base & 0x0000ffff) << 16) |
79 (content->limit & 0x0ffff);
80 *buffer = (content->base & 0xff000000) |
81 ((content->base & 0x00ff0000)>>16) |
82 (content->limit & 0xf0000) |
83 (content->type << 10) |
84 ((content->read_only == 0) << 9) |
85 ((content->seg_32bit != 0) << 22) |
86 ((content->limit_in_pages != 0) << 23) |
87 0xf000;
91 /***********************************************************************
92 * LDT_GetEntry
94 * Retrieve an LDT entry.
96 int LDT_GetEntry( int entry, ldt_entry *content )
98 int ret = 0;
100 content->base = ldt_copy[entry].base;
101 content->limit = ldt_copy[entry].limit;
102 content->type = (ldt_flags_copy[entry] & LDT_FLAGS_TYPE);
103 content->seg_32bit = (ldt_flags_copy[entry] & LDT_FLAGS_32BIT) != 0;
104 content->read_only = (ldt_flags_copy[entry] & LDT_FLAGS_READONLY) !=0;
105 content->limit_in_pages = (ldt_flags_copy[entry] & LDT_FLAGS_BIG) !=0;
106 if (content->limit_in_pages) content->limit >>= 12;
107 return ret;
111 /***********************************************************************
112 * LDT_SetEntry
114 * Set an LDT entry.
116 int LDT_SetEntry( int entry, const ldt_entry *content )
118 int ret = 0;
120 dprintf_ldt(stddeb,
121 "LDT_SetEntry: entry=%04x base=%08lx limit=%05lx %s %d-bit flags=%c%c%c\n",
122 entry, content->base, content->limit,
123 content->limit_in_pages ? "pages" : "bytes",
124 content->seg_32bit ? 32 : 16,
125 content->read_only && (content->type & SEGMENT_CODE) ? '-' : 'r',
126 content->read_only || (content->type & SEGMENT_CODE) ? '-' : 'w',
127 (content->type & SEGMENT_CODE) ? 'x' : '-' );
129 /* Entry 0 must not be modified; its base and limit are always 0 */
130 if (!entry) return 0;
132 #ifdef linux
133 if (!__winelib)
135 struct modify_ldt_s ldt_info;
137 ldt_info.entry_number = entry;
138 ldt_info.base_addr = content->base;
139 ldt_info.limit = content->limit;
140 ldt_info.seg_32bit = content->seg_32bit != 0;
141 ldt_info.contents = content->type;
142 ldt_info.read_exec_only = content->read_only != 0;
143 ldt_info.limit_in_pages = content->limit_in_pages != 0;
144 ldt_info.seg_not_present = 0;
145 /* Make sure the info will be accepted by the kernel */
146 /* This is ugly, but what can I do? */
147 if (content->type == SEGMENT_STACK)
149 /* FIXME */
151 else
153 if (ldt_info.base_addr >= 0xc0000000)
155 fprintf( stderr, "LDT_SetEntry: invalid base addr %08lx\n",
156 ldt_info.base_addr );
157 return -1;
159 if (content->limit_in_pages)
161 if ((ldt_info.limit << 12) + 0xfff >
162 0xc0000000 - ldt_info.base_addr)
163 ldt_info.limit = (0xc0000000 - 0xfff - ldt_info.base_addr) >> 12;
165 else
167 if (ldt_info.limit > 0xc0000000 - ldt_info.base_addr)
168 ldt_info.limit = 0xc0000000 - ldt_info.base_addr;
171 if ((ret = modify_ldt(1, &ldt_info, sizeof(ldt_info))) < 0)
172 perror( "modify_ldt" );
174 #endif /* linux */
176 #if defined(__NetBSD__) || defined(__FreeBSD__)
177 if (!__winelib)
179 long d[2];
181 LDT_EntryToBytes( d, content );
182 ret = i386_set_ldt(entry, (union descriptor *)d, 1);
183 if (ret < 0)
185 perror("i386_set_ldt");
186 fprintf(stderr,
187 "Did you reconfigure the kernel with \"options USER_LDT\"?\n");
188 exit(1);
191 #endif /* __NetBSD__ || __FreeBSD__ */
192 #if defined(__svr4__) || defined(_SCO_DS)
193 if (!__winelib)
195 struct ssd ldt_mod;
196 int i;
197 ldt_mod.sel = ENTRY_TO_SELECTOR(entry) | 4;
198 ldt_mod.bo = content->base;
199 ldt_mod.ls = content->limit;
200 i = ((content->limit & 0xf0000) |
201 (content->type << 10) |
202 (((content->read_only != 0) ^ 1) << 9) |
203 ((content->seg_32bit != 0) << 22) |
204 ((content->limit_in_pages != 0)<< 23) |
205 (1<<15) |
206 0x7000);
208 ldt_mod.acc1 = (i & 0xff00) >> 8;
209 ldt_mod.acc2 = (i & 0xf00000) >> 20;
211 if (content->base == 0)
213 ldt_mod.acc1 = 0;
214 ldt_mod.acc2 = 0;
216 if ((ret = sysi86(SI86DSCR, &ldt_mod)) == -1) perror("sysi86");
218 #endif
220 if (ret < 0) return ret;
221 ldt_copy[entry].base = content->base;
222 if (!content->limit_in_pages) ldt_copy[entry].limit = content->limit;
223 else ldt_copy[entry].limit = (content->limit << 12) | 0x0fff;
224 ldt_flags_copy[entry] = (content->type & LDT_FLAGS_TYPE) |
225 (content->read_only ? LDT_FLAGS_READONLY : 0) |
226 (content->seg_32bit ? LDT_FLAGS_32BIT : 0) |
227 (content->limit_in_pages ? LDT_FLAGS_BIG : 0) |
228 (ldt_flags_copy[entry] & LDT_FLAGS_ALLOCATED);
229 return ret;
233 /***********************************************************************
234 * LDT_Print
236 * Print the content of the LDT on stdout.
238 void LDT_Print( int start, int length )
240 int i;
241 char flags[3];
243 if (length == -1) length = LDT_SIZE - start;
244 for (i = start; i < start + length; i++)
246 if (!ldt_copy[i].base && !ldt_copy[i].limit) continue; /* Free entry */
247 if ((ldt_flags_copy[i] & LDT_FLAGS_TYPE) == SEGMENT_CODE)
249 flags[0] = (ldt_flags_copy[i] & LDT_FLAGS_EXECONLY) ? '-' : 'r';
250 flags[1] = '-';
251 flags[2] = 'x';
253 else
255 flags[0] = 'r';
256 flags[1] = (ldt_flags_copy[i] & LDT_FLAGS_READONLY) ? '-' : 'w';
257 flags[2] = '-';
259 printf("%04x: sel=%04x base=%08lx limit=%08lx %d-bit %c%c%c\n",
260 i, ENTRY_TO_SELECTOR(i),
261 ldt_copy[i].base, ldt_copy[i].limit,
262 ldt_flags_copy[i] & LDT_FLAGS_32BIT ? 32 : 16,
263 flags[0], flags[1], flags[2] );