2 * LDT manipulation functions
4 * Copyright 1993 Robert J. Amstadt
5 * Copyright 1995 Alexandre Julliard
21 unsigned int entry_number
;
22 unsigned long base_addr
;
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
);
35 #if defined(__svr4__) || defined(_SCO_DS)
36 #include <sys/sysi86.h>
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 /***********************************************************************
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;
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 /***********************************************************************
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) |
91 /***********************************************************************
94 * Retrieve an LDT entry.
96 int LDT_GetEntry( int entry
, ldt_entry
*content
)
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;
111 /***********************************************************************
116 int LDT_SetEntry( int entry
, const ldt_entry
*content
)
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;
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
)
153 if (ldt_info
.base_addr
>= 0xc0000000)
155 fprintf( stderr
, "LDT_SetEntry: invalid base addr %08lx\n",
156 ldt_info
.base_addr
);
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;
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" );
176 #if defined(__NetBSD__) || defined(__FreeBSD__)
181 LDT_EntryToBytes( d
, content
);
182 ret
= i386_set_ldt(entry
, (union descriptor
*)d
, 1);
185 perror("i386_set_ldt");
187 "Did you reconfigure the kernel with \"options USER_LDT\"?\n");
191 #endif /* __NetBSD__ || __FreeBSD__ */
192 #if defined(__svr4__) || defined(_SCO_DS)
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) |
208 ldt_mod
.acc1
= (i
& 0xff00) >> 8;
209 ldt_mod
.acc2
= (i
& 0xf00000) >> 20;
211 if (content
->base
== 0)
216 if ((ret
= sysi86(SI86DSCR
, &ldt_mod
)) == -1) perror("sysi86");
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
);
233 /***********************************************************************
236 * Print the content of the LDT on stdout.
238 void LDT_Print( int start
, int length
)
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';
256 flags
[1] = (ldt_flags_copy
[i
] & LDT_FLAGS_READONLY
) ? '-' : 'w';
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] );