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
|
2069
|
75 #include "ldt_keeper.h"
|
|
76
|
2067
|
77 /**
|
|
78 *
|
|
79 * This should be performed before we create first thread. See remarks
|
|
80 * for write_ldt(), linux/kernel/ldt.c.
|
|
81 *
|
|
82 */
|
|
83
|
|
84 static void* fs_seg=NULL;
|
|
85 static char* prev_struct=NULL;
|
|
86 /**
|
|
87 * here is a small logical problem with Restore for multithreaded programs -
|
|
88 * in C++ we use static class for this...
|
|
89 */
|
|
90
|
|
91 #ifdef __cplusplus
|
|
92 extern "C"
|
|
93 #endif
|
|
94 void Setup_FS_Segment(void)
|
|
95 {
|
|
96 __asm__ __volatile__(
|
|
97 "movl %0,%%eax; movw %%ax, %%fs" : : "i" (TEB_SEL)
|
|
98 );
|
|
99 }
|
|
100
|
|
101 #ifdef __linux__
|
|
102 /* XXX: why is this routine from libc redefined here? */
|
|
103 /* NOTE: the redefined version ignores the count param, count is hardcoded as 16 */
|
|
104 static int LDT_Modify( int func, struct modify_ldt_ldt_s *ptr,
|
|
105 unsigned long count )
|
|
106 {
|
|
107 int res;
|
|
108 #ifdef __PIC__
|
|
109 __asm__ __volatile__( "pushl %%ebx\n\t"
|
|
110 "movl %2,%%ebx\n\t"
|
|
111 "int $0x80\n\t"
|
|
112 "popl %%ebx"
|
|
113 : "=a" (res)
|
|
114 : "0" (__NR_modify_ldt),
|
|
115 "r" (func),
|
|
116 "c" (ptr),
|
|
117 "d"(16)//sizeof(*ptr) from kernel point of view
|
|
118 :"esi" );
|
|
119 #else
|
|
120 __asm__ __volatile__("int $0x80"
|
|
121 : "=a" (res)
|
|
122 : "0" (__NR_modify_ldt),
|
|
123 "b" (func),
|
|
124 "c" (ptr),
|
|
125 "d"(16)
|
|
126 :"esi");
|
|
127 #endif /* __PIC__ */
|
|
128 if (res >= 0) return res;
|
|
129 errno = -res;
|
|
130 return -1;
|
|
131 }
|
|
132 #endif
|
|
133
|
|
134 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
|
|
135 static void LDT_EntryToBytes( unsigned long *buffer, const struct modify_ldt_ldt_s *content )
|
|
136 {
|
|
137 *buffer++ = ((content->base_addr & 0x0000ffff) << 16) |
|
|
138 (content->limit & 0x0ffff);
|
|
139 *buffer = (content->base_addr & 0xff000000) |
|
|
140 ((content->base_addr & 0x00ff0000)>>16) |
|
|
141 (content->limit & 0xf0000) |
|
|
142 (content->contents << 10) |
|
|
143 ((content->read_exec_only == 0) << 9) |
|
|
144 ((content->seg_32bit != 0) << 22) |
|
|
145 ((content->limit_in_pages != 0) << 23) |
|
|
146 0xf000;
|
|
147 }
|
|
148 #endif
|
|
149
|
|
150 void Setup_LDT_Keeper(void)
|
|
151 {
|
|
152 struct modify_ldt_ldt_s array;
|
|
153 int fd;
|
|
154 int ret;
|
|
155
|
|
156 if (fs_seg)
|
|
157 return;
|
|
158
|
|
159 prev_struct = 0;
|
|
160 fd = open("/dev/zero", O_RDWR);
|
|
161 fs_seg = mmap(NULL, getpagesize(), PROT_READ | PROT_WRITE, MAP_PRIVATE,
|
|
162 fd, 0);
|
|
163 if(fs_seg==(void*)-1)
|
|
164 {
|
|
165 perror("ERROR: Couldn't allocate memory for fs segment");
|
|
166 return;
|
|
167 }
|
|
168 array.base_addr=(int)fs_seg;
|
|
169 array.entry_number=TEB_SEL_IDX;
|
|
170 array.limit=array.base_addr+getpagesize()-1;
|
|
171 array.seg_32bit=1;
|
|
172 array.read_exec_only=0;
|
|
173 array.seg_not_present=0;
|
|
174 array.contents=MODIFY_LDT_CONTENTS_DATA;
|
|
175 array.limit_in_pages=0;
|
|
176 #ifdef __linux__
|
|
177 ret=LDT_Modify(0x1, &array, sizeof(struct modify_ldt_ldt_s));
|
|
178 if(ret<0)
|
|
179 {
|
|
180 perror("install_fs");
|
|
181 printf("Couldn't install fs segment, expect segfault\n");
|
|
182 }
|
|
183 #endif /*linux*/
|
|
184
|
|
185 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
|
|
186 {
|
|
187 unsigned long d[2];
|
|
188
|
|
189 LDT_EntryToBytes( d, &array );
|
|
190 ret = i386_set_ldt(array.entry_number, (union descriptor *)d, 1);
|
|
191 if (ret < 0)
|
|
192 {
|
|
193 perror("install_fs");
|
|
194 printf("Couldn't install fs segment, expect segfault\n");
|
|
195 printf("Did you reconfigure the kernel with \"options USER_LDT\"?\n");
|
|
196 }
|
|
197 printf("Set_LDT\n");
|
|
198 }
|
|
199 #endif /* __NetBSD__ || __FreeBSD__ || __OpenBSD__ */
|
|
200
|
|
201 #if defined(__svr4__)
|
|
202 struct ssd ssd;
|
|
203 ssd.sel = TEB_SEL;
|
|
204 ssd.bo = array.base_addr;
|
|
205 ssd.ls = array.limit - array.base_addr;
|
|
206 ssd.acc1 = ((array.read_exec_only == 0) << 1) |
|
|
207 (array.contents << 2) |
|
|
208 0xf0; /* P(resent) | DPL3 | S */
|
|
209 ssd.acc2 = 0x4; /* byte limit, 32-bit segment */
|
|
210 if (sysi86(SI86DSCR, &ssd) < 0) {
|
|
211 perror("sysi86(SI86DSCR)");
|
|
212 printf("Couldn't install fs segment, expect segfault\n");
|
|
213 }
|
|
214 #endif
|
|
215
|
|
216 Setup_FS_Segment();
|
|
217
|
|
218 prev_struct = (char*)malloc(sizeof(char) * 8);
|
|
219 *(void**)array.base_addr = prev_struct;
|
|
220 close(fd);
|
|
221 }
|
|
222
|
|
223 void Restore_LDT_Keeper(void)
|
|
224 {
|
|
225 if (fs_seg == 0)
|
|
226 return;
|
|
227 if (prev_struct)
|
|
228 free(prev_struct);
|
|
229 munmap((char*)fs_seg, getpagesize());
|
|
230 fs_seg = 0;
|
|
231 }
|