2067
|
1 /**
|
|
2 * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
3 * This file MUST be in main library because LDT must
|
|
4 * be modified before program creates first thread
|
|
5 * - avifile includes this file from C++ code
|
|
6 * and initializes it at the start of player!
|
|
7 */
|
|
8
|
|
9 #include <string.h>
|
|
10 #include <stdlib.h>
|
|
11 #include <errno.h>
|
|
12 #include <fcntl.h>
|
|
13 #include <sys/mman.h>
|
|
14 #include <sys/types.h>
|
|
15 #include <stdio.h>
|
|
16 #include <unistd.h>
|
|
17
|
|
18 #ifdef __linux__
|
|
19 #include <asm/unistd.h>
|
|
20 #include <asm/ldt.h>
|
|
21 #else
|
|
22 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
|
|
23 #include <machine/sysarch.h>
|
|
24 #endif
|
|
25
|
|
26 #ifdef __svr4__
|
|
27 #include <sys/segment.h>
|
|
28 #include <sys/sysi86.h>
|
|
29
|
|
30 /* solaris x86: add missing prototype for sysi86() */
|
|
31 #ifdef __cplusplus
|
|
32 extern "C" {
|
|
33 #endif
|
|
34 extern int sysi86(int, void*);
|
|
35 #ifdef __cplusplus
|
|
36 }
|
|
37 #endif
|
|
38
|
|
39 #ifndef NUMSYSLDTS /* SunOS 2.5.1 does not define NUMSYSLDTS */
|
|
40 #define NUMSYSLDTS 6 /* Let's hope the SunOS 5.8 value is OK */
|
|
41 #endif
|
|
42
|
|
43 #define TEB_SEL_IDX NUMSYSLDTS
|
|
44 #endif
|
|
45
|
|
46 #define LDT_ENTRIES 8192
|
|
47 #define LDT_ENTRY_SIZE 8
|
|
48 #pragma pack(4)
|
|
49 struct modify_ldt_ldt_s {
|
|
50 unsigned int entry_number;
|
|
51 unsigned long base_addr;
|
|
52 unsigned int limit;
|
|
53 unsigned int seg_32bit:1;
|
|
54 unsigned int contents:2;
|
|
55 unsigned int read_exec_only:1;
|
|
56 unsigned int limit_in_pages:1;
|
|
57 unsigned int seg_not_present:1;
|
|
58 unsigned int useable:1;
|
|
59 };
|
|
60
|
|
61 #define MODIFY_LDT_CONTENTS_DATA 0
|
|
62 #define MODIFY_LDT_CONTENTS_STACK 1
|
|
63 #define MODIFY_LDT_CONTENTS_CODE 2
|
|
64 #endif
|
|
65
|
|
66
|
|
67 /* user level (privilege level: 3) ldt (1<<2) segment selector */
|
|
68 #define LDT_SEL(idx) ((idx) << 3 | 1 << 2 | 3)
|
|
69
|
|
70 #ifndef TEB_SEL_IDX
|
|
71 #define TEB_SEL_IDX 1
|
|
72 #endif
|
|
73 #define TEB_SEL LDT_SEL(TEB_SEL_IDX)
|
|
74
|
|
75 /**
|
|
76 *
|
|
77 * This should be performed before we create first thread. See remarks
|
|
78 * for write_ldt(), linux/kernel/ldt.c.
|
|
79 *
|
|
80 */
|
|
81
|
|
82 static void* fs_seg=NULL;
|
|
83 static char* prev_struct=NULL;
|
|
84 /**
|
|
85 * here is a small logical problem with Restore for multithreaded programs -
|
|
86 * in C++ we use static class for this...
|
|
87 */
|
|
88
|
|
89 #ifdef __cplusplus
|
|
90 extern "C"
|
|
91 #endif
|
|
92 void Setup_FS_Segment(void)
|
|
93 {
|
|
94 __asm__ __volatile__(
|
|
95 "movl %0,%%eax; movw %%ax, %%fs" : : "i" (TEB_SEL)
|
|
96 );
|
|
97 }
|
|
98
|
|
99 #ifdef __linux__
|
|
100 /* XXX: why is this routine from libc redefined here? */
|
|
101 /* NOTE: the redefined version ignores the count param, count is hardcoded as 16 */
|
|
102 static int LDT_Modify( int func, struct modify_ldt_ldt_s *ptr,
|
|
103 unsigned long count )
|
|
104 {
|
|
105 int res;
|
|
106 #ifdef __PIC__
|
|
107 __asm__ __volatile__( "pushl %%ebx\n\t"
|
|
108 "movl %2,%%ebx\n\t"
|
|
109 "int $0x80\n\t"
|
|
110 "popl %%ebx"
|
|
111 : "=a" (res)
|
|
112 : "0" (__NR_modify_ldt),
|
|
113 "r" (func),
|
|
114 "c" (ptr),
|
|
115 "d"(16)//sizeof(*ptr) from kernel point of view
|
|
116 :"esi" );
|
|
117 #else
|
|
118 __asm__ __volatile__("int $0x80"
|
|
119 : "=a" (res)
|
|
120 : "0" (__NR_modify_ldt),
|
|
121 "b" (func),
|
|
122 "c" (ptr),
|
|
123 "d"(16)
|
|
124 :"esi");
|
|
125 #endif /* __PIC__ */
|
|
126 if (res >= 0) return res;
|
|
127 errno = -res;
|
|
128 return -1;
|
|
129 }
|
|
130 #endif
|
|
131
|
|
132 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
|
|
133 static void LDT_EntryToBytes( unsigned long *buffer, const struct modify_ldt_ldt_s *content )
|
|
134 {
|
|
135 *buffer++ = ((content->base_addr & 0x0000ffff) << 16) |
|
|
136 (content->limit & 0x0ffff);
|
|
137 *buffer = (content->base_addr & 0xff000000) |
|
|
138 ((content->base_addr & 0x00ff0000)>>16) |
|
|
139 (content->limit & 0xf0000) |
|
|
140 (content->contents << 10) |
|
|
141 ((content->read_exec_only == 0) << 9) |
|
|
142 ((content->seg_32bit != 0) << 22) |
|
|
143 ((content->limit_in_pages != 0) << 23) |
|
|
144 0xf000;
|
|
145 }
|
|
146 #endif
|
|
147
|
|
148 void Setup_LDT_Keeper(void)
|
|
149 {
|
|
150 struct modify_ldt_ldt_s array;
|
|
151 int fd;
|
|
152 int ret;
|
|
153
|
|
154 if (fs_seg)
|
|
155 return;
|
|
156
|
|
157 prev_struct = 0;
|
|
158 fd = open("/dev/zero", O_RDWR);
|
|
159 fs_seg = mmap(NULL, getpagesize(), PROT_READ | PROT_WRITE, MAP_PRIVATE,
|
|
160 fd, 0);
|
|
161 if(fs_seg==(void*)-1)
|
|
162 {
|
|
163 perror("ERROR: Couldn't allocate memory for fs segment");
|
|
164 return;
|
|
165 }
|
|
166 array.base_addr=(int)fs_seg;
|
|
167 array.entry_number=TEB_SEL_IDX;
|
|
168 array.limit=array.base_addr+getpagesize()-1;
|
|
169 array.seg_32bit=1;
|
|
170 array.read_exec_only=0;
|
|
171 array.seg_not_present=0;
|
|
172 array.contents=MODIFY_LDT_CONTENTS_DATA;
|
|
173 array.limit_in_pages=0;
|
|
174 #ifdef __linux__
|
|
175 ret=LDT_Modify(0x1, &array, sizeof(struct modify_ldt_ldt_s));
|
|
176 if(ret<0)
|
|
177 {
|
|
178 perror("install_fs");
|
|
179 printf("Couldn't install fs segment, expect segfault\n");
|
|
180 }
|
|
181 #endif /*linux*/
|
|
182
|
|
183 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
|
|
184 {
|
|
185 unsigned long d[2];
|
|
186
|
|
187 LDT_EntryToBytes( d, &array );
|
|
188 ret = i386_set_ldt(array.entry_number, (union descriptor *)d, 1);
|
|
189 if (ret < 0)
|
|
190 {
|
|
191 perror("install_fs");
|
|
192 printf("Couldn't install fs segment, expect segfault\n");
|
|
193 printf("Did you reconfigure the kernel with \"options USER_LDT\"?\n");
|
|
194 }
|
|
195 printf("Set_LDT\n");
|
|
196 }
|
|
197 #endif /* __NetBSD__ || __FreeBSD__ || __OpenBSD__ */
|
|
198
|
|
199 #if defined(__svr4__)
|
|
200 struct ssd ssd;
|
|
201 ssd.sel = TEB_SEL;
|
|
202 ssd.bo = array.base_addr;
|
|
203 ssd.ls = array.limit - array.base_addr;
|
|
204 ssd.acc1 = ((array.read_exec_only == 0) << 1) |
|
|
205 (array.contents << 2) |
|
|
206 0xf0; /* P(resent) | DPL3 | S */
|
|
207 ssd.acc2 = 0x4; /* byte limit, 32-bit segment */
|
|
208 if (sysi86(SI86DSCR, &ssd) < 0) {
|
|
209 perror("sysi86(SI86DSCR)");
|
|
210 printf("Couldn't install fs segment, expect segfault\n");
|
|
211 }
|
|
212 #endif
|
|
213
|
|
214 Setup_FS_Segment();
|
|
215
|
|
216 prev_struct = (char*)malloc(sizeof(char) * 8);
|
|
217 *(void**)array.base_addr = prev_struct;
|
|
218 close(fd);
|
|
219 }
|
|
220
|
|
221 void Restore_LDT_Keeper(void)
|
|
222 {
|
|
223 if (fs_seg == 0)
|
|
224 return;
|
|
225 if (prev_struct)
|
|
226 free(prev_struct);
|
|
227 munmap((char*)fs_seg, getpagesize());
|
|
228 fs_seg = 0;
|
|
229 }
|