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