2 * 16-bit kernel initialization code
4 * Copyright 2000 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
29 #include "kernel_private.h"
30 #include "kernel16_private.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(module);
35 /**************************************************************************
36 * DllEntryPoint (KERNEL.669)
38 BOOL WINAPI KERNEL_DllEntryPoint( DWORD reasion, HINSTANCE16 inst, WORD ds,
39 WORD heap, DWORD reserved1, WORD reserved2 )
43 /* the entry point can be called multiple times */
44 if (done) return TRUE;
47 /* Initialize 16-bit thunking entry points */
48 if (!WOWTHUNK_Init()) return FALSE;
50 /* Initialize DOS memory */
51 if (!DOSMEM_Init()) return FALSE;
53 /* Initialize special KERNEL entry points */
55 NE_SetEntryPoint( inst, 178, GetWinFlags16() );
57 NE_SetEntryPoint( inst, 454, wine_get_cs() );
58 NE_SetEntryPoint( inst, 455, wine_get_ds() );
60 NE_SetEntryPoint( inst, 183, DOSMEM_0000H ); /* KERNEL.183: __0000H */
61 NE_SetEntryPoint( inst, 173, DOSMEM_BiosSysSeg ); /* KERNEL.173: __ROMBIOS */
62 NE_SetEntryPoint( inst, 193, DOSMEM_BiosDataSeg ); /* KERNEL.193: __0040H */
63 NE_SetEntryPoint( inst, 194, DOSMEM_BiosSysSeg ); /* KERNEL.194: __F000H */
65 /* Initialize KERNEL.THHOOK */
66 TASK_InstallTHHook(MapSL((SEGPTR)GetProcAddress16( inst, (LPCSTR)332 )));
68 /* Initialize the real-mode selector entry points */
69 #define SET_ENTRY_POINT( num, addr ) \
70 NE_SetEntryPoint( inst, (num), GLOBAL_CreateBlock( GMEM_FIXED, \
71 DOSMEM_MapDosToLinear(addr), 0x10000, inst, \
72 WINE_LDT_FLAGS_DATA ))
74 SET_ENTRY_POINT( 174, 0xa0000 ); /* KERNEL.174: __A000H */
75 SET_ENTRY_POINT( 181, 0xb0000 ); /* KERNEL.181: __B000H */
76 SET_ENTRY_POINT( 182, 0xb8000 ); /* KERNEL.182: __B800H */
77 SET_ENTRY_POINT( 195, 0xc0000 ); /* KERNEL.195: __C000H */
78 SET_ENTRY_POINT( 179, 0xd0000 ); /* KERNEL.179: __D000H */
79 SET_ENTRY_POINT( 190, 0xe0000 ); /* KERNEL.190: __E000H */
80 #undef SET_ENTRY_POINT
82 /* Force loading of some dlls */
83 LoadLibrary16( "system.drv" );
88 /***********************************************************************
89 * Reserved1 (KERNEL.77)
91 SEGPTR WINAPI KERNEL_AnsiNext16(SEGPTR current)
93 return (*(char *)MapSL(current)) ? current + 1 : current;
96 /***********************************************************************
97 * Reserved2(KERNEL.78)
99 SEGPTR WINAPI KERNEL_AnsiPrev16( SEGPTR start, SEGPTR current )
101 return (current==start)?start:current-1;
104 /***********************************************************************
105 * Reserved3 (KERNEL.79)
107 SEGPTR WINAPI KERNEL_AnsiUpper16( SEGPTR strOrChar )
109 /* uppercase only one char if strOrChar < 0x10000 */
110 if (HIWORD(strOrChar))
112 char *s = MapSL(strOrChar);
120 else return toupper((char)strOrChar);
123 /***********************************************************************
124 * Reserved4 (KERNEL.80)
126 SEGPTR WINAPI KERNEL_AnsiLower16( SEGPTR strOrChar )
128 /* lowercase only one char if strOrChar < 0x10000 */
129 if (HIWORD(strOrChar))
131 char *s = MapSL(strOrChar);
139 else return tolower((char)strOrChar);
142 /***********************************************************************
143 * Reserved5 (KERNEL.87)
145 INT16 WINAPI KERNEL_lstrcmp16( LPCSTR str1, LPCSTR str2 )
147 return (INT16)strcmp( str1, str2 );
150 /***********************************************************************
151 * lstrcpy (KERNEL.88)
153 SEGPTR WINAPI lstrcpy16( SEGPTR dst, LPCSTR src )
155 if (!lstrcpyA( MapSL(dst), src )) dst = 0;
159 /***********************************************************************
160 * lstrcat (KERNEL.89)
162 SEGPTR WINAPI lstrcat16( SEGPTR dst, LPCSTR src )
164 /* Windows does not check for NULL pointers here, so we don't either */
165 strcat( MapSL(dst), src );
169 /***********************************************************************
170 * lstrlen (KERNEL.90)
172 INT16 WINAPI lstrlen16( LPCSTR str )
174 return (INT16)lstrlenA( str );
177 /***********************************************************************
178 * OutputDebugString (KERNEL.115)
180 void WINAPI OutputDebugString16( LPCSTR str )
182 OutputDebugStringA( str );
185 /***********************************************************************
186 * DebugBreak (KERNEL.203)
188 void WINAPI DebugBreak16( CONTEXT *context )
190 EXCEPTION_RECORD rec;
192 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
193 rec.ExceptionFlags = 0;
194 rec.ExceptionRecord = NULL;
195 rec.ExceptionAddress = (LPVOID)context->Eip;
196 rec.NumberParameters = 0;
197 NtRaiseException( &rec, context, TRUE );
200 /***********************************************************************
201 * hmemcpy (KERNEL.348)
203 void WINAPI hmemcpy16( LPVOID dst, LPCVOID src, LONG count )
205 memcpy( dst, src, count );
208 /***********************************************************************
209 * lstrcpyn (KERNEL.353)
211 SEGPTR WINAPI lstrcpyn16( SEGPTR dst, LPCSTR src, INT16 n )
213 lstrcpynA( MapSL(dst), src, n );
217 /***********************************************************************
218 * lstrcatn (KERNEL.352)
220 SEGPTR WINAPI lstrcatn16( SEGPTR dst, LPCSTR src, INT16 n )
222 LPSTR p = MapSL(dst);
226 if ((n -= (p - start)) <= 0) return dst;
227 lstrcpynA( p, src, n );
231 /***********************************************************************
232 * UnicodeToAnsi (KERNEL.434)
234 INT16 WINAPI UnicodeToAnsi16( LPCWSTR src, LPSTR dst, INT16 codepage )
236 if ( codepage == -1 ) codepage = CP_ACP;
237 return WideCharToMultiByte( codepage, 0, src, -1, dst, 0x7fffffff, NULL, NULL );
240 /***********************************************************************
241 * VWin32_EventCreate (KERNEL.442)
243 HANDLE WINAPI VWin32_EventCreate(VOID)
245 HANDLE hEvent = CreateEventW( NULL, FALSE, 0, NULL );
246 return ConvertToGlobalHandle( hEvent );
249 /***********************************************************************
250 * VWin32_EventDestroy (KERNEL.443)
252 VOID WINAPI VWin32_EventDestroy(HANDLE event)
254 CloseHandle( event );
257 /***********************************************************************
258 * VWin32_EventWait (KERNEL.450)
260 VOID WINAPI VWin32_EventWait(HANDLE event)
264 ReleaseThunkLock( &mutex_count );
265 WaitForSingleObject( event, INFINITE );
266 RestoreThunkLock( mutex_count );
269 /***********************************************************************
270 * VWin32_EventSet (KERNEL.451)
271 * KERNEL_479 (KERNEL.479)
273 VOID WINAPI VWin32_EventSet(HANDLE event)
278 /***********************************************************************
279 * CreateW32Event (KERNEL.457)
281 HANDLE WINAPI CreateW32Event( BOOL manual_reset, BOOL initial_state )
283 return CreateEventW( NULL, manual_reset, initial_state, NULL );
286 /***********************************************************************
287 * SetW32Event (KERNEL.458)
289 BOOL WINAPI SetW32Event( HANDLE handle )
291 return SetEvent( handle );
294 /***********************************************************************
295 * ResetW32Event (KERNEL.459)
297 BOOL WINAPI ResetW32Event( HANDLE handle )
299 return ResetEvent( handle );
302 /***********************************************************************
303 * WaitForSingleObject (KERNEL.460)
305 DWORD WINAPI WaitForSingleObject16( HANDLE handle, DWORD timeout )
307 DWORD retval, mutex_count;
309 ReleaseThunkLock( &mutex_count );
310 retval = WaitForSingleObject( handle, timeout );
311 RestoreThunkLock( mutex_count );
315 /***********************************************************************
316 * WaitForMultipleObjects (KERNEL.461)
318 DWORD WINAPI WaitForMultipleObjects16( DWORD count, const HANDLE *handles,
319 BOOL wait_all, DWORD timeout )
321 DWORD retval, mutex_count;
323 ReleaseThunkLock( &mutex_count );
324 retval = WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE );
325 RestoreThunkLock( mutex_count );
329 /***********************************************************************
330 * GetCurrentThreadId (KERNEL.462)
332 DWORD WINAPI GetCurrentThreadId16(void)
334 return GetCurrentThreadId();
337 /***********************************************************************
338 * ExitProcess (KERNEL.466)
340 void WINAPI ExitProcess16( WORD status )
343 ReleaseThunkLock( &count );
344 ExitProcess( status );
347 /***********************************************************************
348 * GetCurrentProcessId (KERNEL.471)
350 DWORD WINAPI GetCurrentProcessId16(void)
352 return GetCurrentProcessId();
355 /*********************************************************************
356 * MapProcessHandle (KERNEL.483)
358 DWORD WINAPI MapProcessHandle( HANDLE hProcess )
360 return GetProcessId( hProcess );
363 /***********************************************************************
364 * RegisterServiceProcess (KERNEL.491)
366 DWORD WINAPI RegisterServiceProcess16( DWORD dwProcessId, DWORD dwType )
368 return 1; /* success */
371 /***********************************************************************
372 * WaitForMultipleObjectsEx (KERNEL.495)
374 DWORD WINAPI WaitForMultipleObjectsEx16( DWORD count, const HANDLE *handles,
375 BOOL wait_all, DWORD timeout, BOOL alertable )
377 DWORD retval, mutex_count;
379 ReleaseThunkLock( &mutex_count );
380 retval = WaitForMultipleObjectsEx( count, handles, wait_all, timeout, alertable );
381 RestoreThunkLock( mutex_count );
385 /**********************************************************************
386 * VWin32_BoostThreadGroup (KERNEL.535)
388 VOID WINAPI VWin32_BoostThreadGroup( DWORD threadId, INT boost )
390 FIXME("(0x%08x,%d): stub\n", threadId, boost);
394 /**********************************************************************
395 * VWin32_BoostThreadStatic (KERNEL.536)
397 VOID WINAPI VWin32_BoostThreadStatic( DWORD threadId, INT boost )
399 FIXME("(0x%08x,%d): stub\n", threadId, boost);
402 /***********************************************************************
403 * EnableDos (KERNEL.41)
404 * DisableDos (KERNEL.42)
405 * GetLastDiskChange (KERNEL.98)
406 * ValidateCodeSegments (KERNEL.100)
407 * KbdRst (KERNEL.123)
408 * EnableKernel (KERNEL.124)
409 * DisableKernel (KERNEL.125)
410 * ValidateFreeSpaces (KERNEL.200)
412 * BUNNY_351 (KERNEL.351)
413 * PIGLET_361 (KERNEL.361)
415 * Entry point for kernel functions that do nothing.
417 LONG WINAPI KERNEL_nop(void)
422 /***********************************************************************
423 * ToolHelpHook (KERNEL.341)
424 * see "Undocumented Windows"
426 FARPROC16 WINAPI ToolHelpHook16(FARPROC16 func)
428 static FARPROC16 hook;
430 FIXME("(%p), stub.\n", func);
431 return InterlockedExchangePointer( (void **)&hook, func );
434 /* thunk for 16-bit CreateThread */
441 static DWORD CALLBACK start_thread16( LPVOID threadArgs )
443 struct thread_args args = *(struct thread_args *)threadArgs;
444 HeapFree( GetProcessHeap(), 0, threadArgs );
445 return K32WOWCallback16( (DWORD)args.proc, args.param );
448 /***********************************************************************
449 * CreateThread16 (KERNEL.441)
451 HANDLE WINAPI CreateThread16( SECURITY_ATTRIBUTES *sa, DWORD stack,
452 FARPROC16 start, SEGPTR param,
453 DWORD flags, LPDWORD id )
455 struct thread_args *args = HeapAlloc( GetProcessHeap(), 0, sizeof(*args) );
456 if (!args) return INVALID_HANDLE_VALUE;
459 return CreateThread( sa, stack, start_thread16, args, flags, id );
462 /***********************************************************************
463 * _DebugOutput (KERNEL.328)
465 void WINAPIV _DebugOutput( WORD flags, LPCSTR spec, VA_LIST16 valist )
469 /* Decode caller address */
470 if (!GetModuleName16( GetExePtr(CURRENT_STACK16->cs), caller, sizeof(caller) ))
471 sprintf( caller, "%04X:%04X", CURRENT_STACK16->cs, CURRENT_STACK16->ip );
473 /* FIXME: cannot use wvsnprintf16 from kernel */
474 /* wvsnprintf16( temp, sizeof(temp), spec, valist ); */
477 FIXME("%s %04x %s\n", caller, flags, debugstr_a(spec) );