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