Mercurial > mplayer.hg
annotate loader/setup_FS.c @ 1733:220f86ac8c15
examples and started cleanup of rules by andre.dahlqvist@telia.com
author | eyck |
---|---|
date | Tue, 28 Aug 2001 19:34:32 +0000 |
parents | 73c8f54305b1 |
children |
rev | line source |
---|---|
236 | 1 #include <string.h> |
2 #include <stdlib.h> | |
3 #include <errno.h> | |
4 #include <fcntl.h> | |
5 #include <sys/mman.h> | |
6 #include <sys/types.h> | |
7 #include <stdio.h> | |
8 #include <unistd.h> | |
9 | |
10 #ifdef __linux__ | |
11 #include <asm/unistd.h> | |
12 #include <asm/ldt.h> | |
1307
d8c1b0b38edc
Add prototypes to wine/loader stuff, so that we can catch __stdcall function
jkeil
parents:
1020
diff
changeset
|
13 #else /* !__linux__ */ |
236 | 14 |
15 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) | |
16 #include <machine/sysarch.h> | |
17 #endif | |
18 | |
19 #ifdef __svr4__ | |
20 #include <sys/segment.h> | |
21 #include <sys/sysi86.h> | |
22 /* solaris x86: add missing prototype for sysi86() */ | |
23 extern int sysi86(int, void*); | |
1679
73c8f54305b1
Add a few ifdefs, so that the code compiles on old solaris releases (2.6 and 7)
jkeil
parents:
1307
diff
changeset
|
24 #ifndef NUMSYSLDTS /* SunOS 2.5.1 does not define NUMSYSLDTS */ |
73c8f54305b1
Add a few ifdefs, so that the code compiles on old solaris releases (2.6 and 7)
jkeil
parents:
1307
diff
changeset
|
25 #define NUMSYSLDTS 6 /* Let's hope the SunOS 5.8 value is OK */ |
73c8f54305b1
Add a few ifdefs, so that the code compiles on old solaris releases (2.6 and 7)
jkeil
parents:
1307
diff
changeset
|
26 #endif |
236 | 27 #define TEB_SEL_IDX NUMSYSLDTS |
1307
d8c1b0b38edc
Add prototypes to wine/loader stuff, so that we can catch __stdcall function
jkeil
parents:
1020
diff
changeset
|
28 #endif /* __svr4__ */ |
d8c1b0b38edc
Add prototypes to wine/loader stuff, so that we can catch __stdcall function
jkeil
parents:
1020
diff
changeset
|
29 |
d8c1b0b38edc
Add prototypes to wine/loader stuff, so that we can catch __stdcall function
jkeil
parents:
1020
diff
changeset
|
30 |
236 | 31 |
32 #define LDT_ENTRIES 8192 | |
33 #define LDT_ENTRY_SIZE 8 | |
34 #pragma pack(4) | |
35 struct modify_ldt_ldt_s { | |
36 unsigned int entry_number; | |
37 unsigned long base_addr; | |
38 unsigned int limit; | |
39 unsigned int seg_32bit:1; | |
40 unsigned int contents:2; | |
41 unsigned int read_exec_only:1; | |
42 unsigned int limit_in_pages:1; | |
43 unsigned int seg_not_present:1; | |
44 unsigned int useable:1; | |
45 }; | |
46 | |
47 #define MODIFY_LDT_CONTENTS_DATA 0 | |
48 #define MODIFY_LDT_CONTENTS_STACK 1 | |
49 #define MODIFY_LDT_CONTENTS_CODE 2 | |
1307
d8c1b0b38edc
Add prototypes to wine/loader stuff, so that we can catch __stdcall function
jkeil
parents:
1020
diff
changeset
|
50 #endif /* !__linux__ */ |
d8c1b0b38edc
Add prototypes to wine/loader stuff, so that we can catch __stdcall function
jkeil
parents:
1020
diff
changeset
|
51 |
d8c1b0b38edc
Add prototypes to wine/loader stuff, so that we can catch __stdcall function
jkeil
parents:
1020
diff
changeset
|
52 #include "setup_FS.h" |
d8c1b0b38edc
Add prototypes to wine/loader stuff, so that we can catch __stdcall function
jkeil
parents:
1020
diff
changeset
|
53 |
236 | 54 |
55 | |
56 /* user level (privilege level: 3) ldt (1<<2) segment selector */ | |
57 #define LDT_SEL(idx) ((idx) << 3 | 1 << 2 | 3) | |
58 | |
59 #ifndef TEB_SEL_IDX | |
60 #define TEB_SEL_IDX 1 | |
61 #endif | |
62 | |
63 #define TEB_SEL LDT_SEL(TEB_SEL_IDX) | |
64 | |
65 void setup_FS_Segment() | |
66 { | |
340 | 67 #ifdef DETAILED_OUT |
236 | 68 printf("Setup FS...");fflush(stdout); |
340 | 69 #endif |
236 | 70 __asm__ __volatile__( |
71 "movl %0,%%eax; movw %%ax, %%fs" : : "i" (TEB_SEL) | |
72 ); | |
340 | 73 #ifdef DETAILED_OUT |
236 | 74 printf("OK!\n"); |
340 | 75 #endif |
236 | 76 } |
77 | |
78 | |
79 /** | |
80 * | |
81 * This should be performed before we create first thread. See remarks | |
82 * for write_ldt(), linux/kernel/ldt.c. | |
83 * | |
84 */ | |
85 | |
86 static void* fs_seg=NULL; | |
87 | |
88 #ifdef __linux__ | |
89 /* XXX: why is this routine from libc redefined here? */ | |
90 /* NOTE: the redefined version ignores the count param, count is hardcoded as 16 */ | |
91 static int modify_ldt( int func, struct modify_ldt_ldt_s *ptr, | |
92 unsigned long count ) | |
93 { | |
94 int res; | |
95 #ifdef __PIC__ | |
96 __asm__ __volatile__( "pushl %%ebx\n\t" | |
97 "movl %2,%%ebx\n\t" | |
98 "int $0x80\n\t" | |
99 "popl %%ebx" | |
100 : "=a" (res) | |
101 : "0" (__NR_modify_ldt), | |
102 "r" (func), | |
103 "c" (ptr), | |
104 "d"(16)//sizeof(*ptr) from kernel point of view | |
105 :"esi" ); | |
106 #else | |
107 __asm__ __volatile__("int $0x80" | |
108 : "=a" (res) | |
109 : "0" (__NR_modify_ldt), | |
110 "b" (func), | |
111 "c" (ptr), | |
112 "d"(16) | |
113 :"esi"); | |
114 #endif /* __PIC__ */ | |
115 if (res >= 0) return res; | |
116 errno = -res; | |
117 return -1; | |
118 } | |
119 #endif | |
120 | |
121 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) | |
122 static void LDT_EntryToBytes( unsigned long *buffer, const struct modify_ldt_ldt_s *content ) | |
123 { | |
124 *buffer++ = ((content->base_addr & 0x0000ffff) << 16) | | |
125 (content->limit & 0x0ffff); | |
126 *buffer = (content->base_addr & 0xff000000) | | |
127 ((content->base_addr & 0x00ff0000)>>16) | | |
128 (content->limit & 0xf0000) | | |
129 (content->contents << 10) | | |
130 ((content->read_exec_only == 0) << 9) | | |
131 ((content->seg_32bit != 0) << 22) | | |
132 ((content->limit_in_pages != 0) << 23) | | |
133 0xf000; | |
134 } | |
135 #endif | |
136 | |
137 void Setup_LDT_Keeper(){ | |
138 struct modify_ldt_ldt_s array; | |
139 int fd; | |
140 int ret; | |
141 void* prev_struct; | |
142 | |
143 if(fs_seg) return; // already set! | |
144 | |
145 fd=open("/dev/zero", O_RDWR); | |
146 fs_seg=mmap(NULL, getpagesize(), PROT_READ | PROT_WRITE, MAP_PRIVATE, | |
147 fd, 0); | |
148 if(fs_seg==(void*)-1) | |
149 { | |
150 perror("ERROR: Couldn't allocate memory for fs segment"); | |
151 return; | |
152 } | |
153 array.base_addr=(int)fs_seg; | |
154 array.entry_number=TEB_SEL_IDX; | |
155 array.limit=array.base_addr+getpagesize()-1; | |
156 array.seg_32bit=1; | |
157 array.read_exec_only=0; | |
158 array.seg_not_present=0; | |
159 array.contents=MODIFY_LDT_CONTENTS_DATA; | |
160 array.limit_in_pages=0; | |
161 #ifdef __linux__ | |
162 ret=modify_ldt(0x1, &array, sizeof(struct modify_ldt_ldt_s)); | |
163 if(ret<0) | |
164 { | |
165 perror("install_fs"); | |
166 printf("Couldn't install fs segment, expect segfault\n"); | |
167 } | |
168 #endif /*linux*/ | |
169 | |
170 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) | |
171 { | |
172 unsigned long d[2]; | |
173 | |
174 LDT_EntryToBytes( d, &array ); | |
175 ret = i386_set_ldt(array.entry_number, (union descriptor *)d, 1); | |
176 if (ret < 0) | |
177 { | |
178 perror("install_fs"); | |
179 printf("Couldn't install fs segment, expect segfault\n"); | |
180 printf("Did you reconfigure the kernel with \"options USER_LDT\"?\n"); | |
181 } | |
182 printf("Set_LDT\n"); | |
183 } | |
184 #endif /* __NetBSD__ || __FreeBSD__ || __OpenBSD__ */ | |
185 | |
186 #if defined(__svr4__) | |
1020
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
340
diff
changeset
|
187 { |
236 | 188 struct ssd ssd; |
189 ssd.sel = TEB_SEL; | |
190 ssd.bo = array.base_addr; | |
191 ssd.ls = array.limit - array.base_addr; | |
192 ssd.acc1 = ((array.read_exec_only == 0) << 1) | | |
193 (array.contents << 2) | | |
194 0xf0; /* P(resent) | DPL3 | S */ | |
195 ssd.acc2 = 0x4; /* byte limit, 32-bit segment */ | |
196 if (sysi86(SI86DSCR, &ssd) < 0) { | |
197 perror("sysi86(SI86DSCR)"); | |
198 printf("Couldn't install fs segment, expect segfault\n"); | |
199 } | |
1020
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
340
diff
changeset
|
200 } |
236 | 201 #endif |
202 | |
203 setup_FS_Segment(); | |
204 | |
205 prev_struct=malloc(8); | |
206 *(void**)array.base_addr=prev_struct; | |
207 close(fd); | |
208 } | |
209 | |
210 void Restore_LDT_Keeper() | |
211 { | |
212 if(fs_seg==0) return; | |
213 munmap((char*)fs_seg, getpagesize()); | |
214 } | |
215 |