summarylogtreecommitdiffstats
path: root/0003-LoL-abi.vsyscall32-alternative_patch_by_using_a_fake_cs_segment.patch
blob: 55917702ac29700436683406819f606d04f7c66b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
diff --git a/dlls/ntdll/unix/signal_i386.c b/dlls/ntdll/unix/signal_i386.c
index e2a6148..574e74e 100644
--- a/dlls/ntdll/unix/signal_i386.c
+++ b/dlls/ntdll/unix/signal_i386.c
@@ -386,6 +386,8 @@ static inline int set_thread_area( struct modify_ldt_s *ptr )
 
 static ULONG first_ldt_entry = 32;
 
+static int wine_cs;
+
 enum i386_trap_code
 {
 #if defined(__FreeBSD__) || defined (__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
@@ -502,6 +504,11 @@ static inline WORD get_cs(void) { WORD res; __asm__( "movw %%cs,%0" : "=r" (res)
 static inline WORD get_ds(void) { WORD res; __asm__( "movw %%ds,%0" : "=r" (res) ); return res; }
 static inline WORD get_fs(void) { WORD res; __asm__( "movw %%fs,%0" : "=r" (res) ); return res; }
 static inline WORD get_gs(void) { WORD res; __asm__( "movw %%gs,%0" : "=r" (res) ); return res; }
+static CDECL void __attribute((naked)) set_cs( DWORD val ) {
+                                          asm  ( "movl 4(%esp),%eax\n\t"
+                                                 "xchg 0(%esp),%eax\n\t"
+                                                 "push %eax\n\t"
+                                                 "lret"); }
 static inline void set_fs( WORD val ) { __asm__( "mov %0,%%fs" :: "r" (val)); }
 static inline void set_gs( WORD val ) { __asm__( "mov %0,%%gs" :: "r" (val)); }
 
@@ -679,7 +686,8 @@ static inline void *init_handler( const ucontext_t *sigcontext )
     }
 #endif
 
-    if (!ldt_is_system(CS_sig(sigcontext)) || !ldt_is_system(SS_sig(sigcontext)))  /* 16-bit mode */
+    if ((CS_sig(sigcontext) != wine_cs && !ldt_is_system(CS_sig(sigcontext))) ||
+        !ldt_is_system(SS_sig(sigcontext)))  /* 16-bit mode */
     {
         /*
          * Win16 or DOS protected mode. Note that during switch
@@ -1170,7 +1178,7 @@ static inline DWORD is_privileged_instr( CONTEXT *context )
     BYTE instr[16];
     unsigned int i, len, prefix_count = 0;
 
-    if (!ldt_is_system( context->SegCs )) return 0;
+    if (context->SegCs != wine_cs && !ldt_is_system( context->SegCs )) return 0;
     len = virtual_uninterrupted_read_memory( (BYTE *)context->Eip, instr, sizeof(instr) );
 
     for (i = 0; i < len; i++) switch (instr[i])
@@ -1237,7 +1245,7 @@ static inline BOOL check_invalid_gs( ucontext_t *sigcontext, CONTEXT *context )
     WORD system_gs = x86_thread_data()->gs;
 
     if (context->SegGs == system_gs) return FALSE;
-    if (!ldt_is_system( context->SegCs )) return FALSE;
+    if (context->SegCs != wine_cs && !ldt_is_system( context->SegCs )) return FALSE;
     /* only handle faults in system libraries */
     if (virtual_is_valid_code_address( instr, 1 )) return FALSE;
 
@@ -1478,7 +1486,7 @@ C_ASSERT( (offsetof(struct stack_layout, xstate) == sizeof(struct stack_layout))
     EIP_sig(sigcontext) = (DWORD)pKiUserExceptionDispatcher;
     /* clear single-step, direction, and align check flag */
     EFL_sig(sigcontext) &= ~(0x100|0x400|0x40000);
-    CS_sig(sigcontext)  = get_cs();
+    CS_sig(sigcontext)  = wine_cs;
     DS_sig(sigcontext)  = get_ds();
     ES_sig(sigcontext)  = get_ds();
     FS_sig(sigcontext)  = get_fs();
@@ -2143,6 +2151,36 @@ static void ldt_set_entry( WORD sel, LDT_ENTRY entry )
                                     LDT_FLAGS_ALLOCATED);
 }
 
+static WORD internal_ldt_alloc_entry(void)
+{
+    for (int idx = first_ldt_entry; idx < LDT_SIZE; idx++)
+    {
+        if (__wine_ldt_copy.flags[idx] & LDT_FLAGS_ALLOCATED) continue;
+
+        /* mark selector as allocated */
+        __wine_ldt_copy.flags[idx] |= LDT_FLAGS_ALLOCATED;
+        return (idx << 3) | 7;
+    }
+    return 0;
+}
+
+static inline void cs_init( int first_thread )
+{
+    LDT_ENTRY entry;
+    sigset_t sigset;
+
+    /* no locking for first thread */
+    if (!first_thread) server_enter_uninterrupted_section( &ldt_mutex, &sigset );
+    if (!wine_cs)
+        wine_cs = internal_ldt_alloc_entry();
+
+    entry = ldt_make_entry( 0, (UINT_PTR)-1, LDT_FLAGS_CODE|LDT_FLAGS_32BIT );
+    ldt_set_entry( wine_cs, entry );
+
+    if (!first_thread) server_leave_uninterrupted_section( &ldt_mutex, &sigset );
+    set_cs( wine_cs );
+}
+
 static void ldt_set_fs( WORD sel, TEB *teb )
 {
     if (sel == gdt_fs_sel)
@@ -2260,38 +2298,35 @@ void signal_init_threading(void)
 NTSTATUS signal_alloc_thread( TEB *teb )
 {
     struct x86_thread_data *thread_data = (struct x86_thread_data *)&teb->GdiTebBatch;
+    static int first_thread = 1;
 
     if (!gdt_fs_sel)
     {
-        static int first_thread = 1;
         sigset_t sigset;
-        int idx;
+        WORD sel;
         LDT_ENTRY entry = ldt_make_entry( teb, page_size - 1, LDT_FLAGS_DATA | LDT_FLAGS_32BIT );
 
         if (first_thread)  /* no locking for first thread */
         {
             /* leave some space if libc is using the LDT for %gs */
             if (!is_gdt_sel( get_gs() )) first_ldt_entry = 512;
-            idx = first_ldt_entry;
-            ldt_set_entry( (idx << 3) | 7, entry );
-            first_thread = 0;
+            sel = (first_ldt_entry << 3) | 7;
+            ldt_set_entry( sel, entry );
         }
         else
         {
             server_enter_uninterrupted_section( &ldt_mutex, &sigset );
-            for (idx = first_ldt_entry; idx < LDT_SIZE; idx++)
-            {
-                if (__wine_ldt_copy.flags[idx]) continue;
-                ldt_set_entry( (idx << 3) | 7, entry );
-                break;
-            }
+            sel = internal_ldt_alloc_entry();
+            if (sel) ldt_set_entry( sel, entry );
             server_leave_uninterrupted_section( &ldt_mutex, &sigset );
-            if (idx == LDT_SIZE) return STATUS_TOO_MANY_THREADS;
+            if (!sel) return STATUS_TOO_MANY_THREADS;
         }
-        thread_data->fs = (idx << 3) | 7;
+        thread_data->fs = sel;
     }
     else thread_data->fs = gdt_fs_sel;
 
+    cs_init( first_thread );
+    first_thread = 0;
     teb->WOW32Reserved = __wine_syscall_dispatcher;
     return STATUS_SUCCESS;
 }