Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Win32 'syslevel' routines |
| 3 | * |
| 4 | * Copyright 1998 Ulrich Weigand |
| 5 | */ |
| 6 | |
| 7 | #include <unistd.h> |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 8 | #include <sys/types.h> |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 9 | #include "syslevel.h" |
| 10 | #include "heap.h" |
| 11 | #include "stackframe.h" |
Alexandre Julliard | 1565709 | 1999-05-23 10:25:25 +0000 | [diff] [blame] | 12 | #include "debugtools.h" |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 13 | |
Patrik Stridvall | b4b9fae | 1999-04-19 14:56:29 +0000 | [diff] [blame] | 14 | DEFAULT_DEBUG_CHANNEL(win32) |
| 15 | |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 16 | static SYSLEVEL Win16Mutex; |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 17 | static SEGPTR segpWin16Mutex; |
| 18 | |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 19 | /* Global variable to save current TEB while in 16-bit code */ |
| 20 | WORD SYSLEVEL_Win16CurrentTeb = 0; |
| 21 | |
Ulrich Weigand | 2e30f6f | 1998-10-11 18:59:11 +0000 | [diff] [blame] | 22 | /* TEB of initial process for emergency use */ |
| 23 | WORD SYSLEVEL_EmergencyTeb = 0; |
| 24 | |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 25 | |
| 26 | /************************************************************************ |
| 27 | * SYSLEVEL_Init |
| 28 | */ |
| 29 | void SYSLEVEL_Init(void) |
| 30 | { |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 31 | SYSLEVEL **w16Mutex = SEGPTR_ALLOC(sizeof(SYSLEVEL *)); |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 32 | |
| 33 | *w16Mutex = &Win16Mutex; |
| 34 | segpWin16Mutex = SEGPTR_GET(w16Mutex); |
| 35 | |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 36 | _CreateSysLevel( &Win16Mutex, 1 ); |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | /************************************************************************ |
| 40 | * GetpWin16Lock32 (KERNEL32.93) |
| 41 | */ |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 42 | VOID WINAPI GetpWin16Lock(SYSLEVEL **lock) |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 43 | { |
| 44 | *lock = &Win16Mutex; |
| 45 | } |
| 46 | |
| 47 | /************************************************************************ |
| 48 | * GetpWin16Lock16 (KERNEL.449) |
| 49 | */ |
| 50 | SEGPTR WINAPI GetpWin16Lock16(void) |
| 51 | { |
| 52 | return segpWin16Mutex; |
| 53 | } |
| 54 | |
| 55 | /************************************************************************ |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 56 | * _CreateSysLevel (KERNEL.438) |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 57 | */ |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 58 | VOID WINAPI _CreateSysLevel(SYSLEVEL *lock, INT level) |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 59 | { |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 60 | InitializeCriticalSection( &lock->crst ); |
| 61 | MakeCriticalSectionGlobal( &lock->crst ); |
| 62 | lock->level = level; |
| 63 | |
Alexandre Julliard | 1565709 | 1999-05-23 10:25:25 +0000 | [diff] [blame] | 64 | TRACE("(%p, %d): handle is %d\n", |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 65 | lock, level, lock->crst.LockSemaphore ); |
| 66 | } |
| 67 | |
| 68 | /************************************************************************ |
| 69 | * _EnterSysLevel (KERNEL32.97) (KERNEL.439) |
| 70 | */ |
| 71 | VOID WINAPI _EnterSysLevel(SYSLEVEL *lock) |
| 72 | { |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 73 | TEB *teb = NtCurrentTeb(); |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 74 | int i; |
| 75 | |
Patrik Stridvall | 1ed4ecf | 1999-06-26 14:58:24 +0000 | [diff] [blame] | 76 | TRACE("(%p, level %d): thread %p (fs %04x, pid %ld) count before %ld\n", |
| 77 | lock, lock->level, teb->tid, teb->teb_sel, (long) getpid(), |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 78 | teb->sys_count[lock->level] ); |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 79 | |
| 80 | for ( i = 3; i > lock->level; i-- ) |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 81 | if ( teb->sys_count[i] > 0 ) |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 82 | { |
Alexandre Julliard | 1565709 | 1999-05-23 10:25:25 +0000 | [diff] [blame] | 83 | ERR("(%p, level %d): Holding %p, level %d. Expect deadlock!\n", |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 84 | lock, lock->level, teb->sys_mutex[i], i ); |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | EnterCriticalSection( &lock->crst ); |
| 88 | |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 89 | teb->sys_count[lock->level]++; |
| 90 | teb->sys_mutex[lock->level] = lock; |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 91 | |
Patrik Stridvall | 1ed4ecf | 1999-06-26 14:58:24 +0000 | [diff] [blame] | 92 | TRACE("(%p, level %d): thread %p (fs %04x, pid %ld) count after %ld\n", |
| 93 | lock, lock->level, teb->tid, teb->teb_sel, (long) getpid(), |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 94 | teb->sys_count[lock->level] ); |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 95 | |
| 96 | if (lock == &Win16Mutex) |
| 97 | GET_FS( SYSLEVEL_Win16CurrentTeb ); |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /************************************************************************ |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 101 | * _LeaveSysLevel (KERNEL32.98) (KERNEL.440) |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 102 | */ |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 103 | VOID WINAPI _LeaveSysLevel(SYSLEVEL *lock) |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 104 | { |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 105 | TEB *teb = NtCurrentTeb(); |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 106 | |
Patrik Stridvall | 1ed4ecf | 1999-06-26 14:58:24 +0000 | [diff] [blame] | 107 | TRACE("(%p, level %d): thread %p (fs %04x, pid %ld) count before %ld\n", |
| 108 | lock, lock->level, teb->tid, teb->teb_sel, (long) getpid(), |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 109 | teb->sys_count[lock->level] ); |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 110 | |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 111 | if ( teb->sys_count[lock->level] <= 0 || teb->sys_mutex[lock->level] != lock ) |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 112 | { |
Alexandre Julliard | 1565709 | 1999-05-23 10:25:25 +0000 | [diff] [blame] | 113 | ERR("(%p, level %d): Invalid state: count %ld mutex %p.\n", |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 114 | lock, lock->level, teb->sys_count[lock->level], |
| 115 | teb->sys_mutex[lock->level] ); |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 116 | } |
| 117 | else |
| 118 | { |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 119 | if ( --teb->sys_count[lock->level] == 0 ) |
| 120 | teb->sys_mutex[lock->level] = NULL; |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | LeaveCriticalSection( &lock->crst ); |
| 124 | |
Patrik Stridvall | 1ed4ecf | 1999-06-26 14:58:24 +0000 | [diff] [blame] | 125 | TRACE("(%p, level %d): thread %p (fs %04x, pid %ld) count after %ld\n", |
| 126 | lock, lock->level, teb->tid, teb->teb_sel, (long) getpid(), |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 127 | teb->sys_count[lock->level] ); |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | /************************************************************************ |
| 131 | * (KERNEL32.86) |
| 132 | */ |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 133 | VOID WINAPI _KERNEL32_86(SYSLEVEL *lock) |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 134 | { |
| 135 | _LeaveSysLevel(lock); |
| 136 | } |
| 137 | |
| 138 | /************************************************************************ |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 139 | * _ConfirmSysLevel (KERNEL32.95) (KERNEL.436) |
| 140 | */ |
| 141 | DWORD WINAPI _ConfirmSysLevel(SYSLEVEL *lock) |
| 142 | { |
| 143 | if ( lock && lock->crst.OwningThread == GetCurrentThreadId() ) |
| 144 | return lock->crst.RecursionCount; |
| 145 | else |
| 146 | return 0L; |
| 147 | } |
| 148 | |
| 149 | /************************************************************************ |
| 150 | * _CheckNotSysLevel (KERNEL32.94) (KERNEL.437) |
| 151 | */ |
| 152 | VOID WINAPI _CheckNotSysLevel(SYSLEVEL *lock) |
| 153 | { |
Alexandre Julliard | 1565709 | 1999-05-23 10:25:25 +0000 | [diff] [blame] | 154 | FIXME("(%p)\n", lock); |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | |
| 158 | /************************************************************************ |
Marcus Meissner | a763ed9 | 1998-10-22 17:02:25 +0000 | [diff] [blame] | 159 | * SYSLEVEL_EnterWin16Lock [KERNEL.480] |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 160 | */ |
Andreas Mohr | c21650e | 1998-10-11 17:37:53 +0000 | [diff] [blame] | 161 | VOID WINAPI SYSLEVEL_EnterWin16Lock(VOID) |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 162 | { |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 163 | _EnterSysLevel(&Win16Mutex); |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | /************************************************************************ |
Marcus Meissner | a763ed9 | 1998-10-22 17:02:25 +0000 | [diff] [blame] | 167 | * SYSLEVEL_LeaveWin16Lock [KERNEL.481] |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 168 | */ |
Andreas Mohr | c21650e | 1998-10-11 17:37:53 +0000 | [diff] [blame] | 169 | VOID WINAPI SYSLEVEL_LeaveWin16Lock(VOID) |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 170 | { |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 171 | _LeaveSysLevel(&Win16Mutex); |
| 172 | } |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 173 | /************************************************************************ |
| 174 | * _ConfirmWin16Lock (KERNEL32.96) |
| 175 | */ |
| 176 | DWORD WINAPI _ConfirmWin16Lock(void) |
| 177 | { |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 178 | return _ConfirmSysLevel(&Win16Mutex); |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | /************************************************************************ |
| 182 | * ReleaseThunkLock (KERNEL32.48) |
| 183 | */ |
| 184 | VOID WINAPI ReleaseThunkLock(DWORD *mutex_count) |
| 185 | { |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 186 | DWORD count = _ConfirmSysLevel(&Win16Mutex); |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 187 | *mutex_count = count; |
| 188 | |
| 189 | while (count-- > 0) |
| 190 | _LeaveSysLevel(&Win16Mutex); |
| 191 | } |
| 192 | |
| 193 | /************************************************************************ |
| 194 | * RestoreThunkLock (KERNEL32.49) |
| 195 | */ |
| 196 | VOID WINAPI RestoreThunkLock(DWORD mutex_count) |
| 197 | { |
| 198 | while (mutex_count-- > 0) |
| 199 | _EnterSysLevel(&Win16Mutex); |
| 200 | } |
| 201 | |
| 202 | /************************************************************************ |
| 203 | * SYSLEVEL_ReleaseWin16Lock |
| 204 | */ |
| 205 | VOID SYSLEVEL_ReleaseWin16Lock(VOID) |
| 206 | { |
Ulrich Weigand | 3dff7bb | 1999-07-11 13:58:31 +0000 | [diff] [blame] | 207 | /* entry_point is never used again once the entry point has |
| 208 | been called. Thus we re-use it to hold the Win16Lock count */ |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 209 | |
Ulrich Weigand | 3dff7bb | 1999-07-11 13:58:31 +0000 | [diff] [blame] | 210 | ReleaseThunkLock(&CURRENT_STACK16->entry_point); |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | /************************************************************************ |
| 214 | * SYSLEVEL_RestoreWin16Lock |
| 215 | */ |
| 216 | VOID SYSLEVEL_RestoreWin16Lock(VOID) |
| 217 | { |
Ulrich Weigand | 3dff7bb | 1999-07-11 13:58:31 +0000 | [diff] [blame] | 218 | RestoreThunkLock(CURRENT_STACK16->entry_point); |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 219 | } |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 220 | |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 221 | /************************************************************************ |
| 222 | * SYSLEVEL_CheckNotLevel |
| 223 | */ |
| 224 | VOID SYSLEVEL_CheckNotLevel( INT level ) |
| 225 | { |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 226 | INT i; |
| 227 | |
| 228 | for ( i = 3; i >= level; i-- ) |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 229 | if ( NtCurrentTeb()->sys_count[i] > 0 ) |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 230 | { |
Alexandre Julliard | 1565709 | 1999-05-23 10:25:25 +0000 | [diff] [blame] | 231 | ERR("(%d): Holding lock of level %d!\n", |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 232 | level, i ); |
Alexandre Julliard | 55e1af9 | 1999-06-18 18:24:47 +0000 | [diff] [blame] | 233 | DebugBreak(); |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 234 | break; |
| 235 | } |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 236 | } |
| 237 | |