From 5473cb012319b78e876a6c96b013db4d346b18db Mon Sep 17 00:00:00 2001 From: arpi Date: Thu, 4 Oct 2001 00:07:15 +0000 Subject: setup_FS.c -> ldt_keeper.c (avifile sync) git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@2068 b3059339-0415-0410-9bf9-f77b7e298cf2 --- loader/dshow/DS_AudioDec.c | 9 +- loader/dshow/DS_VideoDec.c | 5 +- loader/dshow/DS_VideoDecoder.c | 5 +- loader/ldt_keeper.c | 229 +++++++++++++++++++++++++++++++++++++++++ loader/ldt_keeper.h | 15 +++ 5 files changed, 253 insertions(+), 10 deletions(-) create mode 100644 loader/ldt_keeper.c create mode 100644 loader/ldt_keeper.h (limited to 'loader') diff --git a/loader/dshow/DS_AudioDec.c b/loader/dshow/DS_AudioDec.c index 9b462f631f..d96d513072 100644 --- a/loader/dshow/DS_AudioDec.c +++ b/loader/dshow/DS_AudioDec.c @@ -18,6 +18,8 @@ #include "DS_AudioDec.h" +#include "ldt_keeper.h" + // DS_Decoder(const CodecInfo& info, const WAVEFORMATEX*); // virtual ~DS_Decoder(); // virtual int Convert(const void*, size_t, void*, size_t, size_t*, size_t*); @@ -25,13 +27,10 @@ static void* _handle; -extern "C" void Setup_LDT_Keeper(); -extern "C" void setup_FS_Segment(); - extern "C" int DS_AudioDecoder_Open(char* dllname, GUID* guid, WAVEFORMATEX* wf){ Setup_LDT_Keeper(); - setup_FS_Segment(); + Setup_FS_Segment(); CodecInfo ci; ci.dll=dllname; @@ -55,7 +54,7 @@ extern "C" int DS_AudioDecoder_Convert(unsigned char* in_data, unsigned in_size, unsigned char* out_data, unsigned out_size, unsigned* size_read, unsigned* size_written){ DS_AudioDecoder* dec=(DS_AudioDecoder*)_handle; - setup_FS_Segment(); + Setup_FS_Segment(); return dec->Convert( (void*)in_data,(size_t)in_size, (void*)out_data,(size_t)out_size, (size_t*)size_read, (size_t*)size_written ); diff --git a/loader/dshow/DS_VideoDec.c b/loader/dshow/DS_VideoDec.c index 60eb1d8d82..541fc34fa3 100644 --- a/loader/dshow/DS_VideoDec.c +++ b/loader/dshow/DS_VideoDec.c @@ -20,12 +20,11 @@ #include "DS_VideoDec.h" +#include "ldt_keeper.h" + static void* _handle; // will be parameter later... static char** _d_ptr; // will be parameter later... -extern "C" void Setup_LDT_Keeper(); -extern "C" void setup_FS_Segment(); - extern "C" int DS_VideoDecoder_Open(char* dllname, GUID* guid, BITMAPINFOHEADER* format, int flip,char** d_ptr){ Setup_LDT_Keeper(); diff --git a/loader/dshow/DS_VideoDecoder.c b/loader/dshow/DS_VideoDecoder.c index 60156166a3..c91eb70f77 100644 --- a/loader/dshow/DS_VideoDecoder.c +++ b/loader/dshow/DS_VideoDecoder.c @@ -24,8 +24,9 @@ #define __MODULE__ "DirectShow_VideoDecoder" +#include "ldt_keeper.h" + extern "C" char* def_path; -extern "C" void setup_FS_Segment(); using namespace std; @@ -232,7 +233,7 @@ int DS_VideoDecoder::DecodeInternal(void* src, size_t size, int is_keyframe, CIm // crashes inside ...->Receive() fixed now? // // nope - but this is surely helpfull - I'll try some more experiments - setup_FS_Segment(); + Setup_FS_Segment(); #if 0 if (!m_pDS_Filter || !m_pDS_Filter->m_pImp || !m_pDS_Filter->m_pImp->vt diff --git a/loader/ldt_keeper.c b/loader/ldt_keeper.c new file mode 100644 index 0000000000..2ddbcb77b7 --- /dev/null +++ b/loader/ldt_keeper.c @@ -0,0 +1,229 @@ +/** + * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + * This file MUST be in main library because LDT must + * be modified before program creates first thread + * - avifile includes this file from C++ code + * and initializes it at the start of player! + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef __linux__ +#include +#include +#else +#if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) +#include +#endif + +#ifdef __svr4__ +#include +#include + +/* solaris x86: add missing prototype for sysi86() */ +#ifdef __cplusplus +extern "C" { +#endif +extern int sysi86(int, void*); +#ifdef __cplusplus +} +#endif + +#ifndef NUMSYSLDTS /* SunOS 2.5.1 does not define NUMSYSLDTS */ +#define NUMSYSLDTS 6 /* Let's hope the SunOS 5.8 value is OK */ +#endif + +#define TEB_SEL_IDX NUMSYSLDTS +#endif + +#define LDT_ENTRIES 8192 +#define LDT_ENTRY_SIZE 8 +#pragma pack(4) +struct modify_ldt_ldt_s { + unsigned int entry_number; + unsigned long base_addr; + unsigned int limit; + unsigned int seg_32bit:1; + unsigned int contents:2; + unsigned int read_exec_only:1; + unsigned int limit_in_pages:1; + unsigned int seg_not_present:1; + unsigned int useable:1; +}; + +#define MODIFY_LDT_CONTENTS_DATA 0 +#define MODIFY_LDT_CONTENTS_STACK 1 +#define MODIFY_LDT_CONTENTS_CODE 2 +#endif + + +/* user level (privilege level: 3) ldt (1<<2) segment selector */ +#define LDT_SEL(idx) ((idx) << 3 | 1 << 2 | 3) + +#ifndef TEB_SEL_IDX +#define TEB_SEL_IDX 1 +#endif +#define TEB_SEL LDT_SEL(TEB_SEL_IDX) + +/** + * + * This should be performed before we create first thread. See remarks + * for write_ldt(), linux/kernel/ldt.c. + * + */ + +static void* fs_seg=NULL; +static char* prev_struct=NULL; +/** + * here is a small logical problem with Restore for multithreaded programs - + * in C++ we use static class for this... + */ + +#ifdef __cplusplus +extern "C" +#endif +void Setup_FS_Segment(void) +{ + __asm__ __volatile__( + "movl %0,%%eax; movw %%ax, %%fs" : : "i" (TEB_SEL) + ); +} + +#ifdef __linux__ +/* XXX: why is this routine from libc redefined here? */ +/* NOTE: the redefined version ignores the count param, count is hardcoded as 16 */ +static int LDT_Modify( int func, struct modify_ldt_ldt_s *ptr, + unsigned long count ) +{ + int res; +#ifdef __PIC__ + __asm__ __volatile__( "pushl %%ebx\n\t" + "movl %2,%%ebx\n\t" + "int $0x80\n\t" + "popl %%ebx" + : "=a" (res) + : "0" (__NR_modify_ldt), + "r" (func), + "c" (ptr), + "d"(16)//sizeof(*ptr) from kernel point of view + :"esi" ); +#else + __asm__ __volatile__("int $0x80" + : "=a" (res) + : "0" (__NR_modify_ldt), + "b" (func), + "c" (ptr), + "d"(16) + :"esi"); +#endif /* __PIC__ */ + if (res >= 0) return res; + errno = -res; + return -1; +} +#endif + +#if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) +static void LDT_EntryToBytes( unsigned long *buffer, const struct modify_ldt_ldt_s *content ) +{ + *buffer++ = ((content->base_addr & 0x0000ffff) << 16) | + (content->limit & 0x0ffff); + *buffer = (content->base_addr & 0xff000000) | + ((content->base_addr & 0x00ff0000)>>16) | + (content->limit & 0xf0000) | + (content->contents << 10) | + ((content->read_exec_only == 0) << 9) | + ((content->seg_32bit != 0) << 22) | + ((content->limit_in_pages != 0) << 23) | + 0xf000; +} +#endif + +void Setup_LDT_Keeper(void) +{ + struct modify_ldt_ldt_s array; + int fd; + int ret; + + if (fs_seg) + return; + + prev_struct = 0; + fd = open("/dev/zero", O_RDWR); + fs_seg = mmap(NULL, getpagesize(), PROT_READ | PROT_WRITE, MAP_PRIVATE, + fd, 0); + if(fs_seg==(void*)-1) + { + perror("ERROR: Couldn't allocate memory for fs segment"); + return; + } + array.base_addr=(int)fs_seg; + array.entry_number=TEB_SEL_IDX; + array.limit=array.base_addr+getpagesize()-1; + array.seg_32bit=1; + array.read_exec_only=0; + array.seg_not_present=0; + array.contents=MODIFY_LDT_CONTENTS_DATA; + array.limit_in_pages=0; +#ifdef __linux__ + ret=LDT_Modify(0x1, &array, sizeof(struct modify_ldt_ldt_s)); + if(ret<0) + { + perror("install_fs"); + printf("Couldn't install fs segment, expect segfault\n"); + } +#endif /*linux*/ + +#if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) + { + unsigned long d[2]; + + LDT_EntryToBytes( d, &array ); + ret = i386_set_ldt(array.entry_number, (union descriptor *)d, 1); + if (ret < 0) + { + perror("install_fs"); + printf("Couldn't install fs segment, expect segfault\n"); + printf("Did you reconfigure the kernel with \"options USER_LDT\"?\n"); + } + printf("Set_LDT\n"); + } +#endif /* __NetBSD__ || __FreeBSD__ || __OpenBSD__ */ + +#if defined(__svr4__) + struct ssd ssd; + ssd.sel = TEB_SEL; + ssd.bo = array.base_addr; + ssd.ls = array.limit - array.base_addr; + ssd.acc1 = ((array.read_exec_only == 0) << 1) | + (array.contents << 2) | + 0xf0; /* P(resent) | DPL3 | S */ + ssd.acc2 = 0x4; /* byte limit, 32-bit segment */ + if (sysi86(SI86DSCR, &ssd) < 0) { + perror("sysi86(SI86DSCR)"); + printf("Couldn't install fs segment, expect segfault\n"); + } +#endif + + Setup_FS_Segment(); + + prev_struct = (char*)malloc(sizeof(char) * 8); + *(void**)array.base_addr = prev_struct; + close(fd); +} + +void Restore_LDT_Keeper(void) +{ + if (fs_seg == 0) + return; + if (prev_struct) + free(prev_struct); + munmap((char*)fs_seg, getpagesize()); + fs_seg = 0; +} diff --git a/loader/ldt_keeper.h b/loader/ldt_keeper.h new file mode 100644 index 0000000000..eec7da298a --- /dev/null +++ b/loader/ldt_keeper.h @@ -0,0 +1,15 @@ +#ifndef LDT_KEEPER_H +#define LDT_KEEPER_H + +#ifdef __cplusplus +extern "C" +{ +#endif +void Setup_FS_Segment(void); +void Setup_LDT_Keeper(void); +void Restore_LDT_Keeper(void); +#ifdef __cplusplus +} +#endif + +#endif /* LDT_KEEPER_H */ -- cgit v1.2.3