2 * LDT manipulation functions
4 * Copyright 1993 Robert J. Amstadt
5 * Copyright 1995 Alexandre Julliard
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
)
26 #include <sys/sysi86.h>
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
] = { {0,0}, };
41 unsigned char ldt_flags_copy
[LDT_SIZE
] = { 0, };
44 /***********************************************************************
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;
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 /***********************************************************************
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) |
83 /***********************************************************************
86 * Retrieve an LDT entry.
88 int LDT_GetEntry( int entry
, ldt_entry
*content
)
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;
102 /***********************************************************************
107 int LDT_SetEntry( int entry
, const ldt_entry
*content
)
112 "LDT_SetEntry: entry=%04x base=%08lx limit=%05lx %s %d-bit flags=%c%c%c\n",
113 entry
, content
->base
, content
->limit
,
114 content
->limit_in_pages
? "pages" : "bytes",
115 content
->seg_32bit
? 32 : 16,
116 content
->read_only
&& (content
->type
& SEGMENT_CODE
) ? '-' : 'r',
117 content
->read_only
|| (content
->type
& SEGMENT_CODE
) ? '-' : 'w',
118 (content
->type
& SEGMENT_CODE
) ? 'x' : '-' );
120 /* Entry 0 must not be modified; its base and limit are always 0 */
121 if (!entry
) return 0;
126 struct modify_ldt_ldt_s ldt_info
;
128 memset( &ldt_info
, 0, sizeof(ldt_info
) );
129 ldt_info
.entry_number
= entry
;
130 ldt_info
.base_addr
= content
->base
;
131 ldt_info
.limit
= content
->limit
;
132 ldt_info
.seg_32bit
= content
->seg_32bit
!= 0;
133 ldt_info
.contents
= content
->type
;
134 ldt_info
.read_exec_only
= content
->read_only
!= 0;
135 ldt_info
.limit_in_pages
= content
->limit_in_pages
!= 0;
136 ret
= modify_ldt(1, &ldt_info
, sizeof(ldt_info
));
140 #if defined(__NetBSD__) || defined(__FreeBSD__)
144 LDT_EntryToBytes( d
, content
);
145 ret
= i386_set_ldt(entry
, (union descriptor
*)d
, 1);
148 perror("i386_set_ldt");
150 "Did you reconfigure the kernel with \"options USER_LDT\"?\n");
154 #endif /* __NetBSD__ || __FreeBSD__ */
159 ldt_mod
.sel
= ENTRY_TO_SELECTOR(entry
) | 4;
160 ldt_mod
.bo
= content
->base
;
161 ldt_mod
.ls
= content
->limit
;
162 i
= (content
->limit
& 0xf0000) |
163 (content
->type
<< 10) |
164 (((content
->read_only
!= 0) ^ 1) << 9) |
165 ((content
->seg_32bit
!= 0) << 22) |
166 ((content
->limit_in_pages
!= 0)<< 23) |
170 ldt_mod
.acc1
= (i
& 0xff00) >> 8;
171 ldt_mod
.acc2
= (i
& 0xf00000) >> 20;
174 if (content
->base
== 0)
179 if ((i
= sysi86(SI86DSCR
, &ldt_mod
)) == -1)
184 #endif /* ifndef WINELIB */
186 if (ret
< 0) return ret
;
187 ldt_copy
[entry
].base
= content
->base
;
188 if (!content
->limit_in_pages
) ldt_copy
[entry
].limit
= content
->limit
;
189 else ldt_copy
[entry
].limit
= (content
->limit
<< 12) | 0x0fff;
190 ldt_flags_copy
[entry
] = (content
->type
& LDT_FLAGS_TYPE
) |
191 (content
->read_only
? LDT_FLAGS_READONLY
: 0) |
192 (content
->seg_32bit
? LDT_FLAGS_32BIT
: 0) |
193 (content
->limit_in_pages
? LDT_FLAGS_BIG
: 0) |
194 (ldt_flags_copy
[entry
] & LDT_FLAGS_ALLOCATED
);
199 /***********************************************************************
202 * Print the content of the LDT on stdout.
204 void LDT_Print( int start
, int length
)
209 if (length
== -1) length
= LDT_SIZE
- start
;
210 for (i
= start
; i
< start
+ length
; i
++)
212 if (!ldt_copy
[i
].base
&& !ldt_copy
[i
].limit
) continue; /* Free entry */
213 if ((ldt_flags_copy
[i
] & LDT_FLAGS_TYPE
) == SEGMENT_CODE
)
215 flags
[0] = (ldt_flags_copy
[i
] & LDT_FLAGS_EXECONLY
) ? '-' : 'r';
222 flags
[1] = (ldt_flags_copy
[i
] & LDT_FLAGS_READONLY
) ? '-' : 'w';
225 printf("%04x: sel=%04x base=%08lx limit=%05lx %s %d-bit %c%c%c\n",
226 i
, ENTRY_TO_SELECTOR(i
),
227 ldt_copy
[i
].base
, ldt_copy
[i
].limit
,
228 ldt_flags_copy
[i
] & LDT_FLAGS_BIG
? "(pages)" : "(bytes)",
229 ldt_flags_copy
[i
] & LDT_FLAGS_32BIT
? 32 : 16,
230 flags
[0], flags
[1], flags
[2] );