kernel32: Move the 16-bit thread and process functions to kernel16.c.
[wine] / dlls / kernel32 / kernel16.c
1 /*
2  * 16-bit kernel initialization code
3  *
4  * Copyright 2000 Alexandre Julliard
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <stdarg.h>
22 #include <stdio.h>
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winternl.h"
27 #include "wownt32.h"
28
29 #include "kernel_private.h"
30 #include "kernel16_private.h"
31 #include "wine/debug.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(module);
34
35 /**************************************************************************
36  *              DllEntryPoint   (KERNEL.669)
37  */
38 BOOL WINAPI KERNEL_DllEntryPoint( DWORD reasion, HINSTANCE16 inst, WORD ds,
39                                   WORD heap, DWORD reserved1, WORD reserved2 )
40 {
41     static int done;
42
43     /* the entry point can be called multiple times */
44     if (done) return TRUE;
45     done = 1;
46
47     /* Initialize 16-bit thunking entry points */
48     if (!WOWTHUNK_Init()) return FALSE;
49
50     /* Initialize DOS memory */
51     if (!DOSMEM_Init()) return FALSE;
52
53     /* Initialize special KERNEL entry points */
54
55     NE_SetEntryPoint( inst, 178, GetWinFlags16() );
56
57     NE_SetEntryPoint( inst, 454, wine_get_cs() );
58     NE_SetEntryPoint( inst, 455, wine_get_ds() );
59
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 */
64
65     /* Initialize KERNEL.THHOOK */
66     TASK_InstallTHHook(MapSL((SEGPTR)GetProcAddress16( inst, (LPCSTR)332 )));
67
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 ))
73
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
81
82     /* Force loading of some dlls */
83     LoadLibrary16( "system.drv" );
84
85     return TRUE;
86 }
87
88 /***********************************************************************
89  *              Reserved1 (KERNEL.77)
90  */
91 SEGPTR WINAPI KERNEL_AnsiNext16(SEGPTR current)
92 {
93     return (*(char *)MapSL(current)) ? current + 1 : current;
94 }
95
96 /***********************************************************************
97  *              Reserved2(KERNEL.78)
98  */
99 SEGPTR WINAPI KERNEL_AnsiPrev16( SEGPTR start, SEGPTR current )
100 {
101     return (current==start)?start:current-1;
102 }
103
104 /***********************************************************************
105  *              Reserved3 (KERNEL.79)
106  */
107 SEGPTR WINAPI KERNEL_AnsiUpper16( SEGPTR strOrChar )
108 {
109     /* uppercase only one char if strOrChar < 0x10000 */
110     if (HIWORD(strOrChar))
111     {
112         char *s = MapSL(strOrChar);
113         while (*s)
114         {
115             *s = toupper(*s);
116             s++;
117         }
118         return strOrChar;
119     }
120     else return toupper((char)strOrChar);
121 }
122
123 /***********************************************************************
124  *              Reserved4 (KERNEL.80)
125  */
126 SEGPTR WINAPI KERNEL_AnsiLower16( SEGPTR strOrChar )
127 {
128     /* lowercase only one char if strOrChar < 0x10000 */
129     if (HIWORD(strOrChar))
130     {
131         char *s = MapSL(strOrChar);
132         while (*s)
133         {
134             *s = tolower(*s);
135             s++;
136         }
137         return strOrChar;
138     }
139     else return tolower((char)strOrChar);
140 }
141
142 /***********************************************************************
143  *              Reserved5 (KERNEL.87)
144  */
145 INT16 WINAPI KERNEL_lstrcmp16( LPCSTR str1, LPCSTR str2 )
146 {
147     return (INT16)strcmp( str1, str2 );
148 }
149
150 /***********************************************************************
151  *           lstrcpy   (KERNEL.88)
152  */
153 SEGPTR WINAPI lstrcpy16( SEGPTR dst, LPCSTR src )
154 {
155     if (!lstrcpyA( MapSL(dst), src )) dst = 0;
156     return dst;
157 }
158
159 /***********************************************************************
160  *           lstrcat   (KERNEL.89)
161  */
162 SEGPTR WINAPI lstrcat16( SEGPTR dst, LPCSTR src )
163 {
164     /* Windows does not check for NULL pointers here, so we don't either */
165     strcat( MapSL(dst), src );
166     return dst;
167 }
168
169 /***********************************************************************
170  *           lstrlen   (KERNEL.90)
171  */
172 INT16 WINAPI lstrlen16( LPCSTR str )
173 {
174     return (INT16)lstrlenA( str );
175 }
176
177 /***********************************************************************
178  *           OutputDebugString   (KERNEL.115)
179  */
180 void WINAPI OutputDebugString16( LPCSTR str )
181 {
182     OutputDebugStringA( str );
183 }
184
185 /***********************************************************************
186  *           DebugBreak   (KERNEL.203)
187  */
188 void WINAPI DebugBreak16( CONTEXT *context )
189 {
190     EXCEPTION_RECORD rec;
191
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 );
198 }
199
200 /***********************************************************************
201  *           hmemcpy   (KERNEL.348)
202  */
203 void WINAPI hmemcpy16( LPVOID dst, LPCVOID src, LONG count )
204 {
205     memcpy( dst, src, count );
206 }
207
208 /***********************************************************************
209  *           lstrcpyn   (KERNEL.353)
210  */
211 SEGPTR WINAPI lstrcpyn16( SEGPTR dst, LPCSTR src, INT16 n )
212 {
213     lstrcpynA( MapSL(dst), src, n );
214     return dst;
215 }
216
217 /***********************************************************************
218  *           lstrcatn   (KERNEL.352)
219  */
220 SEGPTR WINAPI lstrcatn16( SEGPTR dst, LPCSTR src, INT16 n )
221 {
222     LPSTR p = MapSL(dst);
223     LPSTR start = p;
224
225     while (*p) p++;
226     if ((n -= (p - start)) <= 0) return dst;
227     lstrcpynA( p, src, n );
228     return dst;
229 }
230
231 /***********************************************************************
232  *           UnicodeToAnsi   (KERNEL.434)
233  */
234 INT16 WINAPI UnicodeToAnsi16( LPCWSTR src, LPSTR dst, INT16 codepage )
235 {
236     if ( codepage == -1 ) codepage = CP_ACP;
237     return WideCharToMultiByte( codepage, 0, src, -1, dst, 0x7fffffff, NULL, NULL );
238 }
239
240 /***********************************************************************
241  *       VWin32_EventCreate     (KERNEL.442)
242  */
243 HANDLE WINAPI VWin32_EventCreate(VOID)
244 {
245     HANDLE hEvent = CreateEventW( NULL, FALSE, 0, NULL );
246     return ConvertToGlobalHandle( hEvent );
247 }
248
249 /***********************************************************************
250  *       VWin32_EventDestroy    (KERNEL.443)
251  */
252 VOID WINAPI VWin32_EventDestroy(HANDLE event)
253 {
254     CloseHandle( event );
255 }
256
257 /***********************************************************************
258  *       VWin32_EventWait       (KERNEL.450)
259  */
260 VOID WINAPI VWin32_EventWait(HANDLE event)
261 {
262     DWORD mutex_count;
263
264     ReleaseThunkLock( &mutex_count );
265     WaitForSingleObject( event, INFINITE );
266     RestoreThunkLock( mutex_count );
267 }
268
269 /***********************************************************************
270  *       VWin32_EventSet        (KERNEL.451)
271  *       KERNEL_479             (KERNEL.479)
272  */
273 VOID WINAPI VWin32_EventSet(HANDLE event)
274 {
275     SetEvent( event );
276 }
277
278 /***********************************************************************
279  *           CreateW32Event    (KERNEL.457)
280  */
281 HANDLE WINAPI CreateW32Event( BOOL manual_reset, BOOL initial_state )
282 {
283     return CreateEventW( NULL, manual_reset, initial_state, NULL );
284 }
285
286 /***********************************************************************
287  *           SetW32Event (KERNEL.458)
288  */
289 BOOL WINAPI SetW32Event( HANDLE handle )
290 {
291     return SetEvent( handle );
292 }
293
294 /***********************************************************************
295  *           ResetW32Event (KERNEL.459)
296  */
297 BOOL WINAPI ResetW32Event( HANDLE handle )
298 {
299     return ResetEvent( handle );
300 }
301
302 /***********************************************************************
303  *           WaitForSingleObject   (KERNEL.460)
304  */
305 DWORD WINAPI WaitForSingleObject16( HANDLE handle, DWORD timeout )
306 {
307     DWORD retval, mutex_count;
308
309     ReleaseThunkLock( &mutex_count );
310     retval = WaitForSingleObject( handle, timeout );
311     RestoreThunkLock( mutex_count );
312     return retval;
313 }
314
315 /***********************************************************************
316  *           WaitForMultipleObjects   (KERNEL.461)
317  */
318 DWORD WINAPI WaitForMultipleObjects16( DWORD count, const HANDLE *handles,
319                                        BOOL wait_all, DWORD timeout )
320 {
321     DWORD retval, mutex_count;
322
323     ReleaseThunkLock( &mutex_count );
324     retval = WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE );
325     RestoreThunkLock( mutex_count );
326     return retval;
327 }
328
329 /***********************************************************************
330  *              GetCurrentThreadId (KERNEL.462)
331  */
332 DWORD WINAPI GetCurrentThreadId16(void)
333 {
334     return GetCurrentThreadId();
335 }
336
337 /***********************************************************************
338  *           ExitProcess   (KERNEL.466)
339  */
340 void WINAPI ExitProcess16( WORD status )
341 {
342     DWORD count;
343     ReleaseThunkLock( &count );
344     ExitProcess( status );
345 }
346
347 /***********************************************************************
348  *              GetCurrentProcessId (KERNEL.471)
349  */
350 DWORD WINAPI GetCurrentProcessId16(void)
351 {
352     return GetCurrentProcessId();
353 }
354
355 /*********************************************************************
356  *           MapProcessHandle   (KERNEL.483)
357  */
358 DWORD WINAPI MapProcessHandle( HANDLE hProcess )
359 {
360     return GetProcessId( hProcess );
361 }
362
363 /***********************************************************************
364  *              RegisterServiceProcess (KERNEL.491)
365  */
366 DWORD WINAPI RegisterServiceProcess16( DWORD dwProcessId, DWORD dwType )
367 {
368     return 1; /* success */
369 }
370
371 /***********************************************************************
372  *           WaitForMultipleObjectsEx   (KERNEL.495)
373  */
374 DWORD WINAPI WaitForMultipleObjectsEx16( DWORD count, const HANDLE *handles,
375                                          BOOL wait_all, DWORD timeout, BOOL alertable )
376 {
377     DWORD retval, mutex_count;
378
379     ReleaseThunkLock( &mutex_count );
380     retval = WaitForMultipleObjectsEx( count, handles, wait_all, timeout, alertable );
381     RestoreThunkLock( mutex_count );
382     return retval;
383 }
384
385 /**********************************************************************
386  * VWin32_BoostThreadGroup   (KERNEL.535)
387  */
388 VOID WINAPI VWin32_BoostThreadGroup( DWORD threadId, INT boost )
389 {
390     FIXME("(0x%08x,%d): stub\n", threadId, boost);
391 }
392
393
394 /**********************************************************************
395  * VWin32_BoostThreadStatic   (KERNEL.536)
396  */
397 VOID WINAPI VWin32_BoostThreadStatic( DWORD threadId, INT boost )
398 {
399     FIXME("(0x%08x,%d): stub\n", threadId, boost);
400 }
401
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)
411  *              K237 (KERNEL.237)
412  *              BUNNY_351 (KERNEL.351)
413  *              PIGLET_361 (KERNEL.361)
414  *
415  * Entry point for kernel functions that do nothing.
416  */
417 LONG WINAPI KERNEL_nop(void)
418 {
419     return 0;
420 }
421
422 /***********************************************************************
423  *           ToolHelpHook                             (KERNEL.341)
424  *      see "Undocumented Windows"
425  */
426 FARPROC16 WINAPI ToolHelpHook16(FARPROC16 func)
427 {
428     static FARPROC16 hook;
429
430     FIXME("(%p), stub.\n", func);
431     return InterlockedExchangePointer( (void **)&hook, func );
432 }
433
434 /* thunk for 16-bit CreateThread */
435 struct thread_args
436 {
437     FARPROC16 proc;
438     DWORD     param;
439 };
440
441 static DWORD CALLBACK start_thread16( LPVOID threadArgs )
442 {
443     struct thread_args args = *(struct thread_args *)threadArgs;
444     HeapFree( GetProcessHeap(), 0, threadArgs );
445     return K32WOWCallback16( (DWORD)args.proc, args.param );
446 }
447
448 /***********************************************************************
449  *           CreateThread16   (KERNEL.441)
450  */
451 HANDLE WINAPI CreateThread16( SECURITY_ATTRIBUTES *sa, DWORD stack,
452                               FARPROC16 start, SEGPTR param,
453                               DWORD flags, LPDWORD id )
454 {
455     struct thread_args *args = HeapAlloc( GetProcessHeap(), 0, sizeof(*args) );
456     if (!args) return INVALID_HANDLE_VALUE;
457     args->proc = start;
458     args->param = param;
459     return CreateThread( sa, stack, start_thread16, args, flags, id );
460 }
461
462 /***********************************************************************
463  *           _DebugOutput                    (KERNEL.328)
464  */
465 void WINAPIV _DebugOutput( WORD flags, LPCSTR spec, VA_LIST16 valist )
466 {
467     char caller[101];
468
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 );
472
473     /* FIXME: cannot use wvsnprintf16 from kernel */
474     /* wvsnprintf16( temp, sizeof(temp), spec, valist ); */
475
476     /* Output */
477     FIXME("%s %04x %s\n", caller, flags, debugstr_a(spec) );
478 }