Implement out of context stack unwinder (#13302)
[platform/upstream/coreclr.git] / src / pal / inc / pal.h
1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4
5 /*++
6
7 Module Name:
8
9     pal.h
10
11 Abstract:
12
13     Rotor Platform Adaptation Layer (PAL) header file.  This file
14     defines all types and API calls required by the Rotor port of
15     the Microsoft Common Language Runtime.
16
17     Defines which control the behavior of this include file:
18       UNICODE - define it to set the Ansi/Unicode neutral names to
19                 be the ...W names.  Otherwise the neutral names default
20                 to be the ...A names.
21       PAL_IMPLEMENTATION - define it when implementing the PAL.  Otherwise
22                 leave it undefined when consuming the PAL.
23
24     Note:  some fields in structs have been renamed from the original
25     SDK documentation names, with _PAL_Undefined appended.  This leaves
26     the structure layout identical to its Win32 version, but prevents
27     PAL consumers from inadvertently referencing undefined fields.
28
29     If you want to add a PAL_ wrapper function to a native function in
30     here, you also need to edit palinternal.h and win32pal.h.
31     
32
33
34 --*/
35
36 #ifndef __PAL_H__
37 #define __PAL_H__
38
39 #ifdef PAL_STDCPP_COMPAT
40 #include <stddef.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <stdarg.h>
44 #include <string.h>
45 #include <errno.h>
46 #include <ctype.h>
47 #endif
48
49 #ifdef  __cplusplus
50 extern "C" {
51 #endif
52
53 // This macro is used to standardize the wide character string literals between UNIX and Windows.
54 // Unix L"" is UTF32, and on windows it's UTF16.  Because of built-in assumptions on the size
55 // of string literals, it's important to match behaviour between Unix and Windows.  Unix will be defined
56 // as u"" (char16_t)
57 #define W(str)  u##str
58
59 // Undefine the QUOTE_MACRO_L helper and redefine it in terms of u.
60 // The reason that we do this is that quote macro is defined in ndp\common\inc,
61 // not inside of coreclr sources.
62
63 #define QUOTE_MACRO_L(x) QUOTE_MACRO_u(x)
64 #define QUOTE_MACRO_u_HELPER(x)     u###x
65 #define QUOTE_MACRO_u(x)            QUOTE_MACRO_u_HELPER(x)
66
67 #include <pal_char16.h>
68 #include <pal_error.h>
69 #include <pal_mstypes.h>
70
71 /******************* Processor-specific glue  *****************************/
72
73 #ifndef _MSC_VER
74
75 #if defined(__i686__) && !defined(_M_IX86)
76 #define _M_IX86 600
77 #elif defined(__i586__) && !defined(_M_IX86)
78 #define _M_IX86 500
79 #elif defined(__i486__) && !defined(_M_IX86)
80 #define _M_IX86 400
81 #elif defined(__i386__) && !defined(_M_IX86)
82 #define _M_IX86 300
83 #elif defined(__x86_64__) && !defined(_M_AMD64)
84 #define _M_AMD64 100
85 #elif defined(__arm__) && !defined(_M_ARM)
86 #define _M_ARM 7
87 #elif defined(__aarch64__) && !defined(_M_ARM64)
88 #define _M_ARM64 1
89 #endif
90
91 #if defined(_M_IX86) && !defined(_X86_)
92 #define _X86_
93 #elif defined(_M_AMD64) && !defined(_AMD64_)
94 #define _AMD64_
95 #elif defined(_M_ARM) && !defined(_ARM_)
96 #define _ARM_
97 #elif defined(_M_ARM64) && !defined(_ARM64_)
98 #define _ARM64_
99 #endif
100
101 #endif // !_MSC_VER
102
103 /******************* ABI-specific glue *******************************/
104
105 #ifdef __APPLE__
106 // Both PowerPC, i386 and x86_64 on Mac OS X use 16-byte alignment.
107 #define STACK_ALIGN_BITS             4
108 #define STACK_ALIGN_REQ             (1 << STACK_ALIGN_BITS)
109 #endif
110
111 #define MAX_PATH 260
112 #define _MAX_PATH 260
113 #define _MAX_DRIVE  3   /* max. length of drive component */
114 #define _MAX_DIR    256 /* max. length of path component */
115 #define _MAX_FNAME  256 /* max. length of file name component */
116 #define _MAX_EXT    256 /* max. length of extension component */
117
118 // In some Win32 APIs MAX_PATH is used for file names (even though 256 is the normal file system limit)
119 // use _MAX_PATH_FNAME to indicate these cases
120 #define MAX_PATH_FNAME MAX_PATH
121 #define MAX_LONGPATH   1024  /* max. length of full pathname */
122
123 #define MAXSHORT      0x7fff
124 #define MAXLONG       0x7fffffff
125 #define MAXCHAR       0x7f
126 #define MAXDWORD      0xffffffff
127
128 //  Sorting IDs.
129 //
130 //  Note that the named locale APIs (eg CompareStringExEx) are recommended.
131 //
132
133 #define LANG_CHINESE                     0x04
134 #define LANG_ENGLISH                     0x09
135 #define LANG_JAPANESE                    0x11
136 #define LANG_KOREAN                      0x12
137 #define LANG_THAI                        0x1e
138
139 /******************* Compiler-specific glue *******************************/
140
141 #ifndef _MSC_VER
142 #if defined(CORECLR)
143 // Define this if the underlying platform supports true 2-pass EH.
144 // At the same time, this enables running several PAL instances
145 // side-by-side.
146 #define FEATURE_PAL_SXS 1
147 #endif // CORECLR
148 #endif // !_MSC_VER
149
150 #if defined(_MSC_VER) || defined(__llvm__)
151 #define DECLSPEC_ALIGN(x)   __declspec(align(x))
152 #else
153 #define DECLSPEC_ALIGN(x) 
154 #endif
155
156 #define DECLSPEC_NORETURN   PAL_NORETURN
157
158 #if !defined(_MSC_VER) || defined(SOURCE_FORMATTING)
159 #define __assume(x) (void)0
160 #define __annotation(x)
161 #endif //!MSC_VER
162
163 #define UNALIGNED
164
165 #ifndef FORCEINLINE
166 #if _MSC_VER < 1200
167 #define FORCEINLINE inline
168 #else
169 #define FORCEINLINE __forceinline
170 #endif
171 #endif
172
173 #ifndef PAL_STDCPP_COMPAT
174
175 #if __GNUC__
176
177 typedef __builtin_va_list va_list;
178
179 /* We should consider if the va_arg definition here is actually necessary.
180    Could we use the standard va_arg definition? */
181
182 #define va_start    __builtin_va_start
183 #define va_arg      __builtin_va_arg
184
185 #define va_copy     __builtin_va_copy
186 #define va_end      __builtin_va_end
187
188 #define VOID void
189
190 #define PUB __attribute__((visibility("default")))
191
192 #else // __GNUC__
193
194 typedef char * va_list;
195
196 #define _INTSIZEOF(n)   ( (sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) - 1) )
197
198 #if _MSC_VER >= 1400
199
200 #ifdef  __cplusplus
201 #define _ADDRESSOF(v)   ( &reinterpret_cast<const char &>(v) )
202 #else
203 #define _ADDRESSOF(v)   ( &(v) )
204 #endif
205
206 #define _crt_va_start(ap,v)  ( ap = (va_list)_ADDRESSOF(v) + _INTSIZEOF(v) )
207 #define _crt_va_arg(ap,t)    ( *(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)) )
208 #define _crt_va_end(ap)      ( ap = (va_list)0 )
209
210 #define va_start _crt_va_start
211 #define va_arg _crt_va_arg
212 #define va_end _crt_va_end
213
214 #else  // _MSC_VER
215
216 #define va_start(ap,v)    (ap = (va_list) (&(v)) + _INTSIZEOF(v))
217 #define va_arg(ap,t)    ( *(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)) )
218 #define va_end(ap)
219
220 #endif // _MSC_VER
221
222 #define va_copy(dest,src) (dest = src)
223
224 #endif // __GNUC__
225
226 #endif // !PAL_STDCPP_COMPAT
227
228 /******************* PAL-Specific Entrypoints *****************************/
229
230 #define IsDebuggerPresent PAL_IsDebuggerPresent
231
232 PALIMPORT
233 BOOL
234 PALAPI
235 PAL_IsDebuggerPresent(VOID);
236  
237 #define MAXIMUM_SUSPEND_COUNT  MAXCHAR
238
239 #define CHAR_BIT      8
240
241 #define SCHAR_MIN   (-128)
242 #define SCHAR_MAX     127
243 #define UCHAR_MAX     0xff
244
245 #define SHRT_MIN    (-32768)
246 #define SHRT_MAX      32767
247 #define USHRT_MAX     0xffff
248
249 #define INT_MIN     (-2147483647 - 1)
250 #define INT_MAX       2147483647
251 #define UINT_MAX      0xffffffff
252
253 #define LONG_MIN    (-2147483647L - 1)
254 #define LONG_MAX      2147483647L
255 #define ULONG_MAX     0xffffffffUL
256
257 #define FLT_MAX 3.402823466e+38F
258 #define DBL_MAX 1.7976931348623157e+308
259
260 /* minimum signed 64 bit value */
261 #define _I64_MIN    (I64(-9223372036854775807) - 1)
262 /* maximum signed 64 bit value */
263 #define _I64_MAX      I64(9223372036854775807)
264 /* maximum unsigned 64 bit value */
265 #define _UI64_MAX     UI64(0xffffffffffffffff)
266
267 #define _I8_MAX   SCHAR_MAX
268 #define _I8_MIN   SCHAR_MIN
269 #define _I16_MAX  SHRT_MAX
270 #define _I16_MIN  SHRT_MIN
271 #define _I32_MAX  INT_MAX
272 #define _I32_MIN  INT_MIN
273 #define _UI8_MAX  UCHAR_MAX
274 #define _UI8_MIN  UCHAR_MIN
275 #define _UI16_MAX USHRT_MAX
276 #define _UI16_MIN USHRT_MIN
277 #define _UI32_MAX UINT_MAX
278 #define _UI32_MIN UINT_MIN
279
280 #ifdef PAL_STDCPP_COMPAT
281 #undef NULL
282 #endif
283
284 #ifndef NULL
285 #if defined(__cplusplus)
286 #define NULL    0
287 #else
288 #define NULL    ((void *)0)
289 #endif
290 #endif
291
292 #if defined(PAL_STDCPP_COMPAT) && !defined(__cplusplus)
293 #define nullptr NULL
294 #endif // defined(PAL_STDCPP_COMPAT) && !defined(__cplusplus)
295
296 #ifndef PAL_STDCPP_COMPAT
297
298 #if _WIN64 || _MSC_VER >= 1400
299 typedef __int64 time_t;
300 #else
301 typedef long time_t;
302 #endif
303 #define _TIME_T_DEFINED
304 #endif // !PAL_STDCPP_COMPAT
305
306 #if ENABLE_DOWNLEVEL_FOR_NLS
307 #define MAKELCID(lgid, srtid)  ((DWORD)((((DWORD)((WORD  )(srtid))) << 16) |  \
308                                          ((DWORD)((WORD  )(lgid)))))
309 #define LANGIDFROMLCID(lcid)   ((WORD)(lcid))
310 #define SORTIDFROMLCID(lcid)   ((WORD)((((DWORD)(lcid)) >> 16) & 0xf))
311
312 #define LANG_NEUTRAL                     0x00
313 #define LANG_INVARIANT                   0x7f
314 #define SUBLANG_NEUTRAL                  0x00    // language neutral
315 #define SUBLANG_DEFAULT                  0x01    // user default
316 #define SORT_DEFAULT                     0x0     // sorting default
317 #define SUBLANG_SYS_DEFAULT              0x02    // system default
318
319 #define MAKELANGID(p, s)       ((((WORD  )(s)) << 10) | (WORD  )(p))
320 #define PRIMARYLANGID(lgid)    ((WORD  )(lgid) & 0x3ff)
321 #define SUBLANGID(lgid)        ((WORD  )(lgid) >> 10)
322
323 #define LANG_SYSTEM_DEFAULT    (MAKELANGID(LANG_NEUTRAL, SUBLANG_SYS_DEFAULT))
324 #define LANG_USER_DEFAULT      (MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT))
325 #define LOCALE_SYSTEM_DEFAULT  (MAKELCID(LANG_SYSTEM_DEFAULT, SORT_DEFAULT))
326 #define LOCALE_USER_DEFAULT    (MAKELCID(LANG_USER_DEFAULT, SORT_DEFAULT))
327 #define LOCALE_NEUTRAL         (MAKELCID(MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), SORT_DEFAULT))
328 #define LOCALE_US_ENGLISH      (MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT))
329 #define LOCALE_INVARIANT       (MAKELCID(MAKELANGID(LANG_INVARIANT, SUBLANG_NEUTRAL), SORT_DEFAULT))
330
331 #define SUBLANG_ENGLISH_US               0x01
332 #define SUBLANG_CHINESE_TRADITIONAL      0x01    /* Chinese (Traditional) */
333
334 #endif // ENABLE_DOWNLEVEL_FOR_NLS
335
336
337 #define CT_CTYPE1                 0x00000001  /* ctype 1 information */
338 #define CT_CTYPE2                 0x00000002  /* ctype 2 information */
339 #define CT_CTYPE3                 0x00000004  /* ctype 3 information */
340 #define C1_UPPER                  0x0001      /* upper case */
341 #define C1_LOWER                  0x0002      /* lower case */
342 #define C1_DIGIT                  0x0004      /* decimal digits */
343 #define C1_SPACE                  0x0008      /* spacing characters */
344 #define C1_PUNCT                  0x0010      /* punctuation characters */
345 #define C1_CNTRL                  0x0020      /* control characters */
346 #define C1_BLANK                  0x0040      /* blank characters */
347 #define C1_XDIGIT                 0x0080      /* other digits */
348 #define C1_ALPHA                  0x0100      /* any linguistic character */
349 #define C2_LEFTTORIGHT            0x0001      /* left to right */
350 #define C2_RIGHTTOLEFT            0x0002      /* right to left */
351 #define C2_EUROPENUMBER           0x0003      /* European number, digit */
352 #define C2_EUROPESEPARATOR        0x0004      /* European numeric separator */
353 #define C2_EUROPETERMINATOR       0x0005      /* European numeric terminator */
354 #define C2_ARABICNUMBER           0x0006      /* Arabic number */
355 #define C2_COMMONSEPARATOR        0x0007      /* common numeric separator */
356 #define C2_BLOCKSEPARATOR         0x0008      /* block separator */
357 #define C2_SEGMENTSEPARATOR       0x0009      /* segment separator */
358 #define C2_WHITESPACE             0x000A      /* white space */
359 #define C2_OTHERNEUTRAL           0x000B      /* other neutrals */
360 #define C2_NOTAPPLICABLE          0x0000      /* no implicit directionality */
361 #define C3_NONSPACING             0x0001      /* nonspacing character */
362 #define C3_DIACRITIC              0x0002      /* diacritic mark */
363 #define C3_VOWELMARK              0x0004      /* vowel mark */
364 #define C3_SYMBOL                 0x0008      /* symbols */
365 #define C3_KATAKANA               0x0010      /* katakana character */
366 #define C3_HIRAGANA               0x0020      /* hiragana character */
367 #define C3_HALFWIDTH              0x0040      /* half width character */
368 #define C3_FULLWIDTH              0x0080      /* full width character */
369 #define C3_IDEOGRAPH              0x0100      /* ideographic character */
370 #define C3_KASHIDA                0x0200      /* Arabic kashida character */
371 #define C3_LEXICAL                0x0400      /* lexical character */
372 #define C3_ALPHA                  0x8000      /* any ling. char (C1_ALPHA) */
373 #define C3_NOTAPPLICABLE          0x0000      /* ctype 3 is not applicable */
374
375 #define DLL_PROCESS_ATTACH 1
376 #define DLL_THREAD_ATTACH  2
377 #define DLL_THREAD_DETACH  3
378 #define DLL_PROCESS_DETACH 0
379
380 #define PAL_INITIALIZE_NONE                         0x00
381 #define PAL_INITIALIZE_SYNC_THREAD                  0x01
382 #define PAL_INITIALIZE_EXEC_ALLOCATOR               0x02
383 #define PAL_INITIALIZE_STD_HANDLES                  0x04
384 #define PAL_INITIALIZE_REGISTER_SIGTERM_HANDLER     0x08
385 #define PAL_INITIALIZE_DEBUGGER_EXCEPTIONS          0x10
386
387 // PAL_Initialize() flags
388 #define PAL_INITIALIZE                 (PAL_INITIALIZE_SYNC_THREAD | PAL_INITIALIZE_STD_HANDLES)
389
390 // PAL_InitializeDLL() flags - don't start any of the helper threads
391 #define PAL_INITIALIZE_DLL             PAL_INITIALIZE_NONE       
392
393 // PAL_InitializeCoreCLR() flags
394 #define PAL_INITIALIZE_CORECLR         (PAL_INITIALIZE | PAL_INITIALIZE_EXEC_ALLOCATOR | PAL_INITIALIZE_REGISTER_SIGTERM_HANDLER | PAL_INITIALIZE_DEBUGGER_EXCEPTIONS)
395
396 typedef DWORD (PALAPI *PTHREAD_START_ROUTINE)(LPVOID lpThreadParameter);
397 typedef PTHREAD_START_ROUTINE LPTHREAD_START_ROUTINE;
398
399 /******************* PAL-Specific Entrypoints *****************************/
400
401 PALIMPORT
402 int
403 PALAPI
404 PAL_Initialize(
405     int argc,
406     const char * const argv[]);
407
408 PALIMPORT
409 int
410 PALAPI
411 PAL_InitializeDLL(VOID);
412
413 PALIMPORT
414 DWORD
415 PALAPI
416 PAL_InitializeCoreCLR(
417     const char *szExePath);
418
419 PALIMPORT
420 DWORD_PTR
421 PALAPI
422 PAL_EntryPoint(
423     IN LPTHREAD_START_ROUTINE lpStartAddress,
424     IN LPVOID lpParameter);
425
426 /// <summary>
427 /// This function shuts down PAL WITHOUT exiting the current process.
428 /// </summary>
429 PALIMPORT
430 void
431 PALAPI
432 PAL_Shutdown(
433     void);
434
435 /// <summary>
436 /// This function shuts down PAL and exits the current process.
437 /// </summary>
438 PALIMPORT
439 void
440 PALAPI
441 PAL_Terminate(
442     void);
443
444 /// <summary>
445 /// This function shuts down PAL and exits the current process with
446 /// the specified exit code.
447 /// </summary>
448 PALIMPORT
449 void
450 PALAPI
451 PAL_TerminateEx(
452     int exitCode);
453
454 typedef VOID (*PSHUTDOWN_CALLBACK)(void);
455
456 PALIMPORT
457 VOID
458 PALAPI
459 PAL_SetShutdownCallback(
460     IN PSHUTDOWN_CALLBACK callback);
461
462 typedef VOID (*PPAL_STARTUP_CALLBACK)(
463     char *modulePath,
464     HMODULE hModule,
465     PVOID parameter);
466
467 PALIMPORT
468 DWORD
469 PALAPI
470 PAL_RegisterForRuntimeStartup(
471     IN DWORD dwProcessId,
472     IN PPAL_STARTUP_CALLBACK pfnCallback,
473     IN PVOID parameter,
474     OUT PVOID *ppUnregisterToken);
475
476 PALIMPORT
477 DWORD
478 PALAPI
479 PAL_UnregisterForRuntimeStartup(
480     IN PVOID pUnregisterToken);
481
482 PALIMPORT
483 BOOL
484 PALAPI
485 PAL_NotifyRuntimeStarted(VOID);
486
487 static const int MAX_DEBUGGER_TRANSPORT_PIPE_NAME_LENGTH = 64;
488
489 PALIMPORT
490 VOID
491 PALAPI
492 PAL_GetTransportPipeName(
493     OUT char *name,
494     IN DWORD id,
495     IN const char *suffix);
496
497 PALIMPORT
498 void
499 PALAPI
500 PAL_InitializeDebug(
501     void);
502
503 PALIMPORT
504 void
505 PALAPI
506 PAL_IgnoreProfileSignal(int signalNum);
507
508 PALIMPORT
509 HINSTANCE
510 PALAPI
511 PAL_RegisterModule(
512     IN LPCSTR lpLibFileName);
513
514 PALIMPORT
515 VOID 
516 PALAPI
517 PAL_UnregisterModule(
518     IN HINSTANCE hInstance);
519
520 PALIMPORT
521 BOOL
522 PALAPI
523 PAL_GetPALDirectoryW(
524     OUT LPWSTR lpDirectoryName,
525     IN OUT UINT* cchDirectoryName);
526 #ifdef UNICODE
527 #define PAL_GetPALDirectory PAL_GetPALDirectoryW
528 #else
529 #define PAL_GetPALDirectory PAL_GetPALDirectoryA
530 #endif
531
532 PALIMPORT
533 BOOL
534 PALAPI
535 PAL_Random(
536     IN BOOL bStrong,
537     IN OUT LPVOID lpBuffer,
538     IN DWORD dwLength);
539
540 PALIMPORT
541 BOOL
542 PALAPI
543 PAL_ProbeMemory(
544     PVOID pBuffer,
545     DWORD cbBuffer,
546     BOOL fWriteAccess);
547
548 /******************* winuser.h Entrypoints *******************************/
549 PALIMPORT
550 LPSTR
551 PALAPI
552 CharNextA(
553             IN LPCSTR lpsz);
554
555 PALIMPORT
556 LPSTR
557 PALAPI
558 CharNextExA(
559         IN WORD CodePage,
560         IN LPCSTR lpCurrentChar,
561         IN DWORD dwFlags);
562
563 #ifndef UNICODE
564 #define CharNext CharNextA
565 #define CharNextEx CharNextExA
566 #endif
567
568
569 #define MB_OK                   0x00000000L
570 #define MB_OKCANCEL             0x00000001L
571 #define MB_ABORTRETRYIGNORE     0x00000002L
572 #define MB_YESNO                0x00000004L
573 #define MB_RETRYCANCEL          0x00000005L
574
575 #define MB_ICONHAND             0x00000010L
576 #define MB_ICONQUESTION         0x00000020L
577 #define MB_ICONEXCLAMATION      0x00000030L
578 #define MB_ICONASTERISK         0x00000040L
579
580 #define MB_ICONINFORMATION      MB_ICONASTERISK
581 #define MB_ICONSTOP             MB_ICONHAND
582 #define MB_ICONERROR            MB_ICONHAND
583
584 #define MB_DEFBUTTON1           0x00000000L
585 #define MB_DEFBUTTON2           0x00000100L
586 #define MB_DEFBUTTON3           0x00000200L
587
588 #define MB_SYSTEMMODAL          0x00001000L
589 #define MB_TASKMODAL            0x00002000L
590 #define MB_SETFOREGROUND        0x00010000L
591 #define MB_TOPMOST              0x00040000L
592
593 #define MB_NOFOCUS                  0x00008000L
594 #define MB_SETFOREGROUND            0x00010000L
595 #define MB_DEFAULT_DESKTOP_ONLY     0x00020000L
596
597 // Note: this is the NT 4.0 and greater value.
598 #define MB_SERVICE_NOTIFICATION 0x00200000L
599
600 #define MB_TYPEMASK             0x0000000FL
601 #define MB_ICONMASK             0x000000F0L
602 #define MB_DEFMASK              0x00000F00L
603
604 #define IDOK                    1
605 #define IDCANCEL                2
606 #define IDABORT                 3
607 #define IDRETRY                 4
608 #define IDIGNORE                5
609 #define IDYES                   6
610 #define IDNO                    7
611
612
613 PALIMPORT
614 int
615 PALAPI
616 MessageBoxW(
617         IN LPVOID hWnd,  // NOTE: diff from winuser.h
618         IN LPCWSTR lpText,
619         IN LPCWSTR lpCaption,
620         IN UINT uType);
621
622
623 #ifdef UNICODE
624 #define MessageBox MessageBoxW
625 #else
626 #define MessageBox MessageBoxA
627 #endif
628
629 /***************** wincon.h Entrypoints **********************************/
630
631 #define CTRL_C_EVENT        0
632 #define CTRL_BREAK_EVENT    1
633 #define CTRL_CLOSE_EVENT    2
634 // 3 is reserved!
635 // 4 is reserved!
636 #define CTRL_LOGOFF_EVENT   5
637 #define CTRL_SHUTDOWN_EVENT 6
638
639 typedef
640 BOOL
641 (PALAPI *PHANDLER_ROUTINE)(
642     DWORD CtrlType
643     );
644
645 #ifndef CORECLR
646 PALIMPORT
647 BOOL
648 PALAPI
649 GenerateConsoleCtrlEvent(
650     IN DWORD dwCtrlEvent,
651     IN DWORD dwProcessGroupId
652     );
653 #endif // !CORECLR
654
655 //end wincon.h Entrypoints
656
657 // From win32.h
658 #ifndef _CRTIMP
659 #ifdef __llvm__
660 #define _CRTIMP
661 #else // __llvm__
662 #define _CRTIMP __declspec(dllimport)
663 #endif // __llvm__
664 #endif // _CRTIMP
665
666 /******************* winbase.h Entrypoints and defines ************************/
667 PALIMPORT
668 BOOL
669 PALAPI
670 AreFileApisANSI(
671         VOID);
672
673 typedef struct _SECURITY_ATTRIBUTES {
674             DWORD nLength;
675             LPVOID lpSecurityDescriptor;
676             BOOL bInheritHandle;
677 } SECURITY_ATTRIBUTES, *PSECURITY_ATTRIBUTES, *LPSECURITY_ATTRIBUTES;
678
679 #define _SH_DENYWR      0x20    /* deny write mode */
680
681 #define FILE_READ_DATA            ( 0x0001 )    // file & pipe
682 #define FILE_APPEND_DATA          ( 0x0004 )    // file
683
684 #define GENERIC_READ               (0x80000000L)
685 #define GENERIC_WRITE              (0x40000000L)
686
687 #define FILE_SHARE_READ            0x00000001
688 #define FILE_SHARE_WRITE           0x00000002
689 #define FILE_SHARE_DELETE          0x00000004
690
691 #define CREATE_NEW                 1
692 #define CREATE_ALWAYS              2
693 #define OPEN_EXISTING              3
694 #define OPEN_ALWAYS                4
695 #define TRUNCATE_EXISTING          5
696
697 #define FILE_ATTRIBUTE_READONLY                 0x00000001
698 #define FILE_ATTRIBUTE_HIDDEN                   0x00000002
699 #define FILE_ATTRIBUTE_SYSTEM                   0x00000004
700 #define FILE_ATTRIBUTE_DIRECTORY                0x00000010
701 #define FILE_ATTRIBUTE_ARCHIVE                  0x00000020
702 #define FILE_ATTRIBUTE_DEVICE                   0x00000040
703 #define FILE_ATTRIBUTE_NORMAL                   0x00000080
704
705 #define FILE_FLAG_WRITE_THROUGH    0x80000000
706 #define FILE_FLAG_NO_BUFFERING     0x20000000
707 #define FILE_FLAG_RANDOM_ACCESS    0x10000000
708 #define FILE_FLAG_SEQUENTIAL_SCAN  0x08000000
709 #define FILE_FLAG_BACKUP_SEMANTICS 0x02000000
710
711 #define FILE_BEGIN                 0
712 #define FILE_CURRENT               1
713 #define FILE_END                   2
714
715 #define STILL_ACTIVE (0x00000103L)
716
717 #define INVALID_SET_FILE_POINTER   ((DWORD)-1)
718
719
720 PALIMPORT
721 HANDLE
722 PALAPI
723 CreateFileW(
724         IN LPCWSTR lpFileName,
725         IN DWORD dwDesiredAccess,
726         IN DWORD dwShareMode,
727         IN LPSECURITY_ATTRIBUTES lpSecurityAttributes,
728         IN DWORD dwCreationDisposition,
729         IN DWORD dwFlagsAndAttributes,
730         IN HANDLE hTemplateFile);
731
732 #ifdef UNICODE
733 #define CreateFile CreateFileW
734 #else
735 #define CreateFile CreateFileA
736 #endif
737
738 PALIMPORT
739 BOOL
740 PALAPI
741 LockFile(
742     IN HANDLE hFile,
743     IN DWORD dwFileOffsetLow,
744     IN DWORD dwFileOffsetHigh,
745     IN DWORD nNumberOfBytesToLockLow,
746     IN DWORD nNumberOfBytesToLockHigh
747     );
748
749 PALIMPORT
750 BOOL
751 PALAPI
752 UnlockFile(
753     IN HANDLE hFile,
754     IN DWORD dwFileOffsetLow,
755     IN DWORD dwFileOffsetHigh,
756     IN DWORD nNumberOfBytesToUnlockLow,
757     IN DWORD nNumberOfBytesToUnlockHigh
758     );
759
760
761 PALIMPORT
762 DWORD
763 PALAPI
764 SearchPathW(
765     IN LPCWSTR lpPath,
766     IN LPCWSTR lpFileName,
767     IN LPCWSTR lpExtension,
768     IN DWORD nBufferLength,
769     OUT LPWSTR lpBuffer,
770     OUT LPWSTR *lpFilePart
771     );
772 #ifdef UNICODE
773 #define SearchPath  SearchPathW
774 #else
775 #define SearchPath  SearchPathA
776 #endif // !UNICODE
777
778
779
780 PALIMPORT
781 BOOL
782 PALAPI
783 CopyFileW(
784       IN LPCWSTR lpExistingFileName,
785       IN LPCWSTR lpNewFileName,
786       IN BOOL bFailIfExists);
787
788 #ifdef UNICODE
789 #define CopyFile CopyFileW
790 #else
791 #define CopyFile CopyFileA
792 #endif
793
794
795 PALIMPORT
796 BOOL
797 PALAPI
798 DeleteFileW(
799         IN LPCWSTR lpFileName);
800
801 #ifdef UNICODE
802 #define DeleteFile DeleteFileW
803 #else
804 #define DeleteFile DeleteFileA
805 #endif
806
807
808
809 PALIMPORT
810 BOOL
811 PALAPI
812 MoveFileW(
813      IN LPCWSTR lpExistingFileName,
814      IN LPCWSTR lpNewFileName);
815
816 #ifdef UNICODE
817 #define MoveFile MoveFileW
818 #else
819 #define MoveFile MoveFileA
820 #endif
821
822 #define MOVEFILE_REPLACE_EXISTING      0x00000001
823 #define MOVEFILE_COPY_ALLOWED          0x00000002
824
825
826 PALIMPORT
827 BOOL
828 PALAPI
829 MoveFileExW(
830         IN LPCWSTR lpExistingFileName,
831         IN LPCWSTR lpNewFileName,
832         IN DWORD dwFlags);
833
834 #ifdef UNICODE
835 #define MoveFileEx MoveFileExW
836 #else
837 #define MoveFileEx MoveFileExA
838 #endif
839
840 PALIMPORT
841 BOOL
842 PALAPI
843 CreateDirectoryW(
844          IN LPCWSTR lpPathName,
845          IN LPSECURITY_ATTRIBUTES lpSecurityAttributes);
846
847 #ifdef UNICODE
848 #define CreateDirectory CreateDirectoryW
849 #else
850 #define CreateDirectory CreateDirectoryA
851 #endif
852
853 PALIMPORT
854 BOOL
855 PALAPI
856 RemoveDirectoryW(
857          IN LPCWSTR lpPathName);
858
859 #ifdef UNICODE
860 #define RemoveDirectory RemoveDirectoryW
861 #else
862 #define RemoveDirectory RemoveDirectoryA
863 #endif
864
865 typedef struct _BY_HANDLE_FILE_INFORMATION {  
866     DWORD dwFileAttributes;
867     FILETIME ftCreationTime;
868     FILETIME ftLastAccessTime;
869     FILETIME ftLastWriteTime;
870     DWORD dwVolumeSerialNumber;
871     DWORD nFileSizeHigh;
872     DWORD nFileSizeLow;
873     DWORD nNumberOfLinks;
874     DWORD nFileIndexHigh;
875     DWORD nFileIndexLow;
876 } BY_HANDLE_FILE_INFORMATION, *PBY_HANDLE_FILE_INFORMATION, *LPBY_HANDLE_FILE_INFORMATION;
877
878 typedef struct _WIN32_FIND_DATAA {
879     DWORD dwFileAttributes;
880     FILETIME ftCreationTime;
881     FILETIME ftLastAccessTime;
882     FILETIME ftLastWriteTime;
883     DWORD nFileSizeHigh;
884     DWORD nFileSizeLow;
885     DWORD dwReserved0;
886     DWORD dwReserved1;
887     CHAR cFileName[ MAX_PATH_FNAME ];
888     CHAR cAlternateFileName[ 14 ];
889 } WIN32_FIND_DATAA, *PWIN32_FIND_DATAA, *LPWIN32_FIND_DATAA;
890
891 typedef struct _WIN32_FIND_DATAW {
892     DWORD dwFileAttributes;
893     FILETIME ftCreationTime;
894     FILETIME ftLastAccessTime;
895     FILETIME ftLastWriteTime;
896     DWORD nFileSizeHigh;
897     DWORD nFileSizeLow;
898     DWORD dwReserved0;
899     DWORD dwReserved1;
900     WCHAR cFileName[ MAX_PATH_FNAME ];
901     WCHAR cAlternateFileName[ 14 ];
902 } WIN32_FIND_DATAW, *PWIN32_FIND_DATAW, *LPWIN32_FIND_DATAW;
903
904 #ifdef UNICODE
905 typedef WIN32_FIND_DATAW WIN32_FIND_DATA;
906 typedef PWIN32_FIND_DATAW PWIN32_FIND_DATA;
907 typedef LPWIN32_FIND_DATAW LPWIN32_FIND_DATA;
908 #else
909 typedef WIN32_FIND_DATAA WIN32_FIND_DATA;
910 typedef PWIN32_FIND_DATAA PWIN32_FIND_DATA;
911 typedef LPWIN32_FIND_DATAA LPWIN32_FIND_DATA;
912 #endif
913
914 PALIMPORT
915 HANDLE
916 PALAPI
917 FindFirstFileW(
918            IN LPCWSTR lpFileName,
919            OUT LPWIN32_FIND_DATAW lpFindFileData);
920
921 #ifdef UNICODE
922 #define FindFirstFile FindFirstFileW
923 #else
924 #define FindFirstFile FindFirstFileA
925 #endif
926
927 PALIMPORT
928 BOOL
929 PALAPI
930 FindNextFileW(
931           IN HANDLE hFindFile,
932           OUT LPWIN32_FIND_DATAW lpFindFileData);
933
934 #ifdef UNICODE
935 #define FindNextFile FindNextFileW
936 #else
937 #define FindNextFile FindNextFileA
938 #endif
939
940 PALIMPORT
941 BOOL
942 PALAPI
943 FindClose(
944       IN OUT HANDLE hFindFile);
945
946 PALIMPORT
947 DWORD
948 PALAPI
949 GetFileAttributesW(
950            IN LPCWSTR lpFileName);
951
952 #ifdef UNICODE
953 #define GetFileAttributes GetFileAttributesW
954 #else
955 #define GetFileAttributes GetFileAttributesA
956 #endif
957
958 typedef enum _GET_FILEEX_INFO_LEVELS {
959   GetFileExInfoStandard
960 } GET_FILEEX_INFO_LEVELS;
961
962 typedef enum _FINDEX_INFO_LEVELS {
963     FindExInfoStandard,
964     FindExInfoBasic,
965     FindExInfoMaxInfoLevel
966 } FINDEX_INFO_LEVELS;
967
968 typedef enum _FINDEX_SEARCH_OPS {
969     FindExSearchNameMatch,
970     FindExSearchLimitToDirectories,
971     FindExSearchLimitToDevices,
972     FindExSearchMaxSearchOp
973 } FINDEX_SEARCH_OPS;
974
975 typedef struct _WIN32_FILE_ATTRIBUTE_DATA {
976     DWORD      dwFileAttributes;
977     FILETIME   ftCreationTime;
978     FILETIME   ftLastAccessTime;
979     FILETIME   ftLastWriteTime;
980     DWORD      nFileSizeHigh;
981     DWORD      nFileSizeLow;
982 } WIN32_FILE_ATTRIBUTE_DATA, *LPWIN32_FILE_ATTRIBUTE_DATA;
983
984 PALIMPORT
985 BOOL
986 PALAPI
987 GetFileAttributesExW(
988              IN LPCWSTR lpFileName,
989              IN GET_FILEEX_INFO_LEVELS fInfoLevelId,
990              OUT LPVOID lpFileInformation);
991
992 #ifdef UNICODE
993 #define GetFileAttributesEx GetFileAttributesExW
994 #endif
995
996 PALIMPORT
997 BOOL
998 PALAPI
999 SetFileAttributesW(
1000            IN LPCWSTR lpFileName,
1001            IN DWORD dwFileAttributes);
1002
1003 #ifdef UNICODE
1004 #define SetFileAttributes SetFileAttributesW
1005 #else
1006 #define SetFileAttributes SetFileAttributesA
1007 #endif
1008
1009 typedef LPVOID LPOVERLAPPED;  // diff from winbase.h
1010
1011 PALIMPORT
1012 BOOL
1013 PALAPI
1014 WriteFile(
1015       IN HANDLE hFile,
1016       IN LPCVOID lpBuffer,
1017       IN DWORD nNumberOfBytesToWrite,
1018       OUT LPDWORD lpNumberOfBytesWritten,
1019       IN LPOVERLAPPED lpOverlapped);
1020
1021 PALIMPORT
1022 BOOL
1023 PALAPI
1024 ReadFile(
1025      IN HANDLE hFile,
1026      OUT LPVOID lpBuffer,
1027      IN DWORD nNumberOfBytesToRead,
1028      OUT LPDWORD lpNumberOfBytesRead,
1029      IN LPOVERLAPPED lpOverlapped);
1030
1031 #define STD_INPUT_HANDLE         ((DWORD)-10)
1032 #define STD_OUTPUT_HANDLE        ((DWORD)-11)
1033 #define STD_ERROR_HANDLE         ((DWORD)-12)
1034
1035 PALIMPORT
1036 HANDLE
1037 PALAPI
1038 GetStdHandle(
1039          IN DWORD nStdHandle);
1040
1041 PALIMPORT
1042 BOOL
1043 PALAPI
1044 SetEndOfFile(
1045          IN HANDLE hFile);
1046
1047 PALIMPORT
1048 DWORD
1049 PALAPI
1050 SetFilePointer(
1051            IN HANDLE hFile,
1052            IN LONG lDistanceToMove,
1053            IN PLONG lpDistanceToMoveHigh,
1054            IN DWORD dwMoveMethod);
1055
1056 PALIMPORT
1057 BOOL
1058 PALAPI
1059 SetFilePointerEx(
1060            IN HANDLE hFile,
1061            IN LARGE_INTEGER liDistanceToMove,
1062            OUT PLARGE_INTEGER lpNewFilePointer,
1063            IN DWORD dwMoveMethod);
1064
1065 PALIMPORT
1066 DWORD
1067 PALAPI
1068 GetFileSize(
1069         IN HANDLE hFile,
1070         OUT LPDWORD lpFileSizeHigh);
1071
1072 PALIMPORT
1073 BOOL
1074 PALAPI GetFileSizeEx(
1075         IN   HANDLE hFile,
1076         OUT  PLARGE_INTEGER lpFileSize);
1077
1078 PALIMPORT
1079 BOOL
1080 PALAPI
1081 GetFileInformationByHandle(
1082         IN HANDLE hFile,
1083         OUT BY_HANDLE_FILE_INFORMATION* lpFileInformation);
1084
1085 PALIMPORT
1086 LONG
1087 PALAPI
1088 CompareFileTime(
1089         IN CONST FILETIME *lpFileTime1,
1090         IN CONST FILETIME *lpFileTime2);
1091
1092 PALIMPORT
1093 BOOL
1094 PALAPI
1095 SetFileTime(
1096         IN HANDLE hFile,
1097         IN CONST FILETIME *lpCreationTime,
1098         IN CONST FILETIME *lpLastAccessTime,
1099         IN CONST FILETIME *lpLastWriteTime);
1100
1101 PALIMPORT
1102 BOOL
1103 PALAPI
1104 GetFileTime(
1105         IN HANDLE hFile,
1106         OUT LPFILETIME lpCreationTime,
1107         OUT LPFILETIME lpLastAccessTime,
1108         OUT LPFILETIME lpLastWriteTime);
1109
1110 PALIMPORT
1111 VOID
1112 PALAPI
1113 GetSystemTimeAsFileTime(
1114             OUT LPFILETIME lpSystemTimeAsFileTime);
1115
1116 typedef struct _SYSTEMTIME {
1117     WORD wYear;
1118     WORD wMonth;
1119     WORD wDayOfWeek;
1120     WORD wDay;
1121     WORD wHour;
1122     WORD wMinute;
1123     WORD wSecond;
1124     WORD wMilliseconds;
1125 } SYSTEMTIME, *PSYSTEMTIME, *LPSYSTEMTIME;
1126
1127 PALIMPORT
1128 VOID
1129 PALAPI
1130 GetSystemTime(
1131           OUT LPSYSTEMTIME lpSystemTime);
1132
1133 PALIMPORT
1134 BOOL
1135 PALAPI
1136 FileTimeToSystemTime(
1137             IN CONST FILETIME *lpFileTime,
1138             OUT LPSYSTEMTIME lpSystemTime);
1139
1140 PALIMPORT
1141 BOOL
1142 PALAPI
1143 FileTimeToDosDateTime(
1144     IN CONST FILETIME *lpFileTime,
1145     OUT LPWORD lpFatDate,
1146     OUT LPWORD lpFatTime
1147     );
1148
1149
1150
1151 PALIMPORT
1152 BOOL
1153 PALAPI
1154 FlushFileBuffers(
1155          IN HANDLE hFile);
1156
1157 #define FILE_TYPE_UNKNOWN         0x0000
1158 #define FILE_TYPE_DISK            0x0001
1159 #define FILE_TYPE_CHAR            0x0002
1160 #define FILE_TYPE_PIPE            0x0003
1161 #define FILE_TYPE_REMOTE          0x8000
1162
1163 PALIMPORT
1164 DWORD
1165 PALAPI
1166 GetFileType(
1167         IN HANDLE hFile);
1168
1169 PALIMPORT
1170 UINT
1171 PALAPI
1172 GetConsoleCP(
1173          VOID);
1174
1175 PALIMPORT
1176 UINT
1177 PALAPI
1178 GetConsoleOutputCP(
1179            VOID);
1180
1181 PALIMPORT
1182 DWORD
1183 PALAPI
1184 GetFullPathNameW(
1185          IN LPCWSTR lpFileName,
1186          IN DWORD nBufferLength,
1187          OUT LPWSTR lpBuffer,
1188          OUT LPWSTR *lpFilePart);
1189
1190 #ifdef UNICODE
1191 #define GetFullPathName GetFullPathNameW
1192 #else
1193 #define GetFullPathName GetFullPathNameA
1194 #endif
1195
1196 PALIMPORT
1197 DWORD
1198 PALAPI
1199 GetLongPathNameW(
1200          IN LPCWSTR lpszShortPath,
1201                  OUT LPWSTR lpszLongPath,
1202          IN DWORD cchBuffer);
1203
1204 #ifdef UNICODE
1205 #define GetLongPathName GetLongPathNameW
1206 #endif
1207
1208 PALIMPORT
1209 DWORD
1210 PALAPI
1211 GetShortPathNameW(
1212          IN LPCWSTR lpszLongPath,
1213                  OUT LPWSTR lpszShortPath,
1214          IN DWORD cchBuffer);
1215
1216 #ifdef UNICODE
1217 #define GetShortPathName GetShortPathNameW
1218 #endif
1219
1220
1221 PALIMPORT
1222 UINT
1223 PALAPI
1224 GetTempFileNameW(
1225          IN LPCWSTR lpPathName,
1226          IN LPCWSTR lpPrefixString,
1227          IN UINT uUnique,
1228          OUT LPWSTR lpTempFileName);
1229
1230 #ifdef UNICODE
1231 #define GetTempFileName GetTempFileNameW
1232 #else
1233 #define GetTempFileName GetTempFileNameA
1234 #endif
1235
1236 PALIMPORT
1237 DWORD
1238 PALAPI
1239 GetTempPathW(
1240          IN DWORD nBufferLength,
1241          OUT LPWSTR lpBuffer);
1242
1243 #ifdef UNICODE
1244 #define GetTempPath GetTempPathW
1245 #else
1246 #define GetTempPath GetTempPathA
1247 #endif
1248
1249 PALIMPORT
1250 DWORD
1251 PALAPI
1252 GetCurrentDirectoryW(
1253              IN DWORD nBufferLength,
1254              OUT LPWSTR lpBuffer);
1255
1256 #ifdef UNICODE
1257 #define GetCurrentDirectory GetCurrentDirectoryW
1258 #else
1259 #define GetCurrentDirectory GetCurrentDirectoryA
1260 #endif
1261
1262 PALIMPORT
1263 BOOL
1264 PALAPI
1265 SetCurrentDirectoryW(
1266             IN LPCWSTR lpPathName);
1267
1268
1269 #ifdef UNICODE
1270 #define SetCurrentDirectory SetCurrentDirectoryW
1271 #else
1272 #define SetCurrentDirectory SetCurrentDirectoryA
1273 #endif
1274
1275 // maximum length of the NETBIOS name (not including NULL)
1276 #define MAX_COMPUTERNAME_LENGTH 15
1277
1278 // maximum length of the username (not including NULL)
1279 #define UNLEN   256
1280
1281 PALIMPORT
1282 BOOL
1283 PALAPI
1284 GetUserNameW(
1285     OUT LPWSTR lpBuffer,      // address of name buffer
1286     IN OUT LPDWORD nSize );   // address of size of name buffer
1287
1288 PALIMPORT
1289 BOOL
1290 PALAPI
1291 GetComputerNameW(
1292     OUT LPWSTR lpBuffer,     // address of name buffer
1293     IN OUT LPDWORD nSize);   // address of size of name buffer
1294
1295 #ifdef UNICODE
1296 #define GetUserName GetUserNameW
1297 #define GetComputerName GetComputerNameW
1298 #endif // UNICODE
1299
1300 PALIMPORT
1301 HANDLE
1302 PALAPI
1303 CreateSemaphoreW(
1304          IN LPSECURITY_ATTRIBUTES lpSemaphoreAttributes,
1305          IN LONG lInitialCount,
1306          IN LONG lMaximumCount,
1307          IN LPCWSTR lpName);
1308
1309 PALIMPORT
1310 HANDLE
1311 PALAPI
1312 CreateSemaphoreExW(
1313         IN LPSECURITY_ATTRIBUTES lpSemaphoreAttributes,
1314         IN LONG lInitialCount,
1315         IN LONG lMaximumCount,
1316         IN LPCWSTR lpName,
1317         IN /*_Reserved_*/  DWORD dwFlags,
1318         IN DWORD dwDesiredAccess);
1319
1320 PALIMPORT
1321 HANDLE
1322 PALAPI
1323 OpenSemaphoreW(
1324     IN DWORD dwDesiredAccess,
1325     IN BOOL bInheritHandle,
1326     IN LPCWSTR lpName);
1327
1328 #ifdef UNICODE
1329 #define CreateSemaphore CreateSemaphoreW
1330 #define CreateSemaphoreEx CreateSemaphoreExW
1331 #else
1332 #define CreateSemaphore CreateSemaphoreA
1333 #define CreateSemaphoreEx CreateSemaphoreExA
1334 #endif
1335
1336 PALIMPORT
1337 BOOL
1338 PALAPI
1339 ReleaseSemaphore(
1340          IN HANDLE hSemaphore,
1341          IN LONG lReleaseCount,
1342          OUT LPLONG lpPreviousCount);
1343
1344 PALIMPORT
1345 HANDLE
1346 PALAPI
1347 CreateEventW(
1348          IN LPSECURITY_ATTRIBUTES lpEventAttributes,
1349          IN BOOL bManualReset,
1350          IN BOOL bInitialState,
1351          IN LPCWSTR lpName);
1352
1353 PALIMPORT
1354 HANDLE
1355 PALAPI
1356 CreateEventExW(
1357          IN LPSECURITY_ATTRIBUTES lpEventAttributes,
1358          IN LPCWSTR lpName,
1359          IN DWORD dwFlags,
1360          IN DWORD dwDesiredAccess);
1361
1362 // CreateEventExW: dwFlags
1363 #define CREATE_EVENT_MANUAL_RESET ((DWORD)0x1)
1364 #define CREATE_EVENT_INITIAL_SET ((DWORD)0x2)
1365
1366 #ifdef UNICODE
1367 #define CreateEvent CreateEventW
1368 #else
1369 #define CreateEvent CreateEventA
1370 #endif
1371
1372 PALIMPORT
1373 BOOL
1374 PALAPI
1375 SetEvent(
1376      IN HANDLE hEvent);
1377
1378 PALIMPORT
1379 BOOL
1380 PALAPI
1381 ResetEvent(
1382        IN HANDLE hEvent);
1383
1384 PALIMPORT
1385 HANDLE
1386 PALAPI
1387 OpenEventW(
1388        IN DWORD dwDesiredAccess,
1389        IN BOOL bInheritHandle,
1390        IN LPCWSTR lpName);
1391
1392 #ifdef UNICODE
1393 #define OpenEvent OpenEventW
1394 #endif
1395
1396 PALIMPORT
1397 HANDLE
1398 PALAPI
1399 CreateMutexW(
1400     IN LPSECURITY_ATTRIBUTES lpMutexAttributes,
1401     IN BOOL bInitialOwner,
1402     IN LPCWSTR lpName);
1403
1404 PALIMPORT
1405 HANDLE
1406 PALAPI
1407 CreateMutexExW(
1408     IN LPSECURITY_ATTRIBUTES lpMutexAttributes,
1409     IN LPCWSTR lpName,
1410     IN DWORD dwFlags,
1411     IN DWORD dwDesiredAccess);
1412
1413 // CreateMutexExW: dwFlags
1414 #define CREATE_MUTEX_INITIAL_OWNER ((DWORD)0x1)
1415
1416 #ifdef UNICODE
1417 #define CreateMutex CreateMutexW
1418 #else
1419 #define CreateMutex CreateMutexA
1420 #endif
1421
1422 PALIMPORT
1423 HANDLE
1424 PALAPI
1425 OpenMutexW(
1426        IN DWORD dwDesiredAccess,
1427        IN BOOL bInheritHandle,
1428        IN LPCWSTR lpName);
1429
1430
1431 #ifdef UNICODE
1432 #define OpenMutex  OpenMutexW
1433 #else
1434 #define OpenMutex  OpenMutexA
1435 #endif // UNICODE
1436
1437 PALIMPORT
1438 BOOL
1439 PALAPI
1440 ReleaseMutex(
1441     IN HANDLE hMutex);
1442
1443 PALIMPORT
1444 DWORD
1445 PALAPI
1446 GetCurrentProcessId(
1447             VOID);
1448
1449 PALIMPORT
1450 DWORD
1451 PALAPI
1452 GetCurrentSessionId(
1453             VOID);
1454
1455 PALIMPORT
1456 HANDLE
1457 PALAPI
1458 GetCurrentProcess(
1459           VOID);
1460
1461 PALIMPORT
1462 DWORD
1463 PALAPI
1464 GetCurrentThreadId(
1465            VOID);
1466
1467 // To work around multiply-defined symbols in the Carbon framework.
1468 #define GetCurrentThread PAL_GetCurrentThread
1469 PALIMPORT
1470 HANDLE
1471 PALAPI
1472 GetCurrentThread(
1473          VOID);
1474
1475
1476 #define STARTF_USESTDHANDLES       0x00000100
1477
1478 typedef struct _STARTUPINFOW {
1479     DWORD cb;
1480     LPWSTR lpReserved_PAL_Undefined;
1481     LPWSTR lpDesktop_PAL_Undefined;
1482     LPWSTR lpTitle_PAL_Undefined;
1483     DWORD dwX_PAL_Undefined;
1484     DWORD dwY_PAL_Undefined;
1485     DWORD dwXSize_PAL_Undefined;
1486     DWORD dwYSize_PAL_Undefined;
1487     DWORD dwXCountChars_PAL_Undefined;
1488     DWORD dwYCountChars_PAL_Undefined;
1489     DWORD dwFillAttribute_PAL_Undefined;
1490     DWORD dwFlags;
1491     WORD wShowWindow_PAL_Undefined;
1492     WORD cbReserved2_PAL_Undefined;
1493     LPBYTE lpReserved2_PAL_Undefined;
1494     HANDLE hStdInput;
1495     HANDLE hStdOutput;
1496     HANDLE hStdError;
1497 } STARTUPINFOW, *LPSTARTUPINFOW;
1498
1499 typedef struct _STARTUPINFOA {
1500     DWORD cb;
1501     LPSTR lpReserved_PAL_Undefined;
1502     LPSTR lpDesktop_PAL_Undefined;
1503     LPSTR lpTitle_PAL_Undefined;
1504     DWORD dwX_PAL_Undefined;
1505     DWORD dwY_PAL_Undefined;
1506     DWORD dwXSize_PAL_Undefined;
1507     DWORD dwYSize_PAL_Undefined;
1508     DWORD dwXCountChars_PAL_Undefined;
1509     DWORD dwYCountChars_PAL_Undefined;
1510     DWORD dwFillAttribute_PAL_Undefined;
1511     DWORD dwFlags;
1512     WORD wShowWindow_PAL_Undefined;
1513     WORD cbReserved2_PAL_Undefined;
1514     LPBYTE lpReserved2_PAL_Undefined;
1515     HANDLE hStdInput;
1516     HANDLE hStdOutput;
1517     HANDLE hStdError;
1518 } STARTUPINFOA, *LPSTARTUPINFOA;
1519
1520 #ifdef UNICODE
1521 typedef STARTUPINFOW STARTUPINFO;
1522 typedef LPSTARTUPINFOW LPSTARTUPINFO;
1523 #else
1524 typedef STARTUPINFOA STARTUPINFO;
1525 typedef LPSTARTUPINFOW LPSTARTUPINFO;
1526 #endif
1527
1528 #define CREATE_NEW_CONSOLE          0x00000010
1529
1530 #define NORMAL_PRIORITY_CLASS             0x00000020
1531
1532 typedef struct _PROCESS_INFORMATION {
1533     HANDLE hProcess;
1534     HANDLE hThread;
1535     DWORD dwProcessId;
1536     DWORD dwThreadId_PAL_Undefined;
1537 } PROCESS_INFORMATION, *PPROCESS_INFORMATION, *LPPROCESS_INFORMATION;
1538
1539 PALIMPORT
1540 BOOL
1541 PALAPI
1542 CreateProcessW(
1543            IN LPCWSTR lpApplicationName,
1544            IN LPWSTR lpCommandLine,
1545            IN LPSECURITY_ATTRIBUTES lpProcessAttributes,
1546            IN LPSECURITY_ATTRIBUTES lpThreadAttributes,
1547            IN BOOL bInheritHandles,
1548            IN DWORD dwCreationFlags,
1549            IN LPVOID lpEnvironment,
1550            IN LPCWSTR lpCurrentDirectory,
1551            IN LPSTARTUPINFOW lpStartupInfo,
1552            OUT LPPROCESS_INFORMATION lpProcessInformation);
1553
1554 #ifdef UNICODE
1555 #define CreateProcess CreateProcessW
1556 #else
1557 #define CreateProcess CreateProcessA
1558 #endif
1559
1560 PALIMPORT
1561 PAL_NORETURN
1562 VOID
1563 PALAPI
1564 ExitProcess(
1565         IN UINT uExitCode);
1566
1567 PALIMPORT
1568 BOOL
1569 PALAPI
1570 TerminateProcess(
1571          IN HANDLE hProcess,
1572          IN UINT uExitCode);
1573
1574 PALIMPORT
1575 BOOL
1576 PALAPI
1577 GetExitCodeProcess(
1578            IN HANDLE hProcess,
1579            IN LPDWORD lpExitCode);
1580
1581 PALIMPORT
1582 BOOL
1583 PALAPI
1584 GetProcessTimes(
1585         IN HANDLE hProcess,
1586         OUT LPFILETIME lpCreationTime,
1587         OUT LPFILETIME lpExitTime,
1588         OUT LPFILETIME lpKernelTime,
1589         OUT LPFILETIME lpUserTime);
1590
1591 #define MAXIMUM_WAIT_OBJECTS  64
1592 #define WAIT_OBJECT_0 0
1593 #define WAIT_ABANDONED   0x00000080
1594 #define WAIT_ABANDONED_0 0x00000080
1595 #define WAIT_TIMEOUT 258
1596 #define WAIT_FAILED ((DWORD)0xFFFFFFFF)
1597
1598 #define INFINITE 0xFFFFFFFF // Infinite timeout
1599
1600 PALIMPORT
1601 DWORD
1602 PALAPI
1603 WaitForSingleObject(
1604             IN HANDLE hHandle,
1605             IN DWORD dwMilliseconds);
1606
1607 PALIMPORT
1608 DWORD
1609 PALAPI
1610 WaitForSingleObjectEx(
1611             IN HANDLE hHandle,
1612             IN DWORD dwMilliseconds,
1613             IN BOOL bAlertable);
1614
1615 PALIMPORT
1616 DWORD
1617 PALAPI
1618 WaitForMultipleObjects(
1619                IN DWORD nCount,
1620                IN CONST HANDLE *lpHandles,
1621                IN BOOL bWaitAll,
1622                IN DWORD dwMilliseconds);
1623
1624 PALIMPORT
1625 DWORD
1626 PALAPI
1627 WaitForMultipleObjectsEx(
1628              IN DWORD nCount,
1629              IN CONST HANDLE *lpHandles,
1630              IN BOOL bWaitAll,
1631              IN DWORD dwMilliseconds,
1632              IN BOOL bAlertable);
1633
1634 #define DUPLICATE_CLOSE_SOURCE      0x00000001
1635 #define DUPLICATE_SAME_ACCESS       0x00000002
1636
1637 PALIMPORT
1638 BOOL
1639 PALAPI
1640 DuplicateHandle(
1641         IN HANDLE hSourceProcessHandle,
1642         IN HANDLE hSourceHandle,
1643         IN HANDLE hTargetProcessHandle,
1644         OUT LPHANDLE lpTargetHandle,
1645         IN DWORD dwDesiredAccess,
1646         IN BOOL bInheritHandle,
1647         IN DWORD dwOptions);
1648
1649 PALIMPORT
1650 VOID
1651 PALAPI
1652 Sleep(
1653       IN DWORD dwMilliseconds);
1654
1655 PALIMPORT
1656 DWORD
1657 PALAPI
1658 SleepEx(
1659     IN DWORD dwMilliseconds,
1660     IN BOOL bAlertable);
1661
1662 PALIMPORT
1663 BOOL
1664 PALAPI
1665 SwitchToThread(
1666     VOID);
1667
1668 #define DEBUG_PROCESS                     0x00000001
1669 #define DEBUG_ONLY_THIS_PROCESS           0x00000002
1670 #define CREATE_SUSPENDED                  0x00000004
1671 #define STACK_SIZE_PARAM_IS_A_RESERVATION 0x00010000
1672
1673 PALIMPORT
1674 HANDLE
1675 PALAPI
1676 CreateThread(
1677          IN LPSECURITY_ATTRIBUTES lpThreadAttributes,
1678          IN DWORD dwStackSize,
1679          IN LPTHREAD_START_ROUTINE lpStartAddress,
1680          IN LPVOID lpParameter,
1681          IN DWORD dwCreationFlags,
1682          OUT LPDWORD lpThreadId);
1683
1684 PALIMPORT
1685 PAL_NORETURN
1686 VOID
1687 PALAPI
1688 ExitThread(
1689        IN DWORD dwExitCode);
1690
1691 PALIMPORT
1692 BOOL
1693 PALAPI
1694 GetExitCodeThread(
1695            IN HANDLE hThread,
1696            IN LPDWORD lpExitCode);
1697
1698 PALIMPORT
1699 DWORD
1700 PALAPI
1701 ResumeThread(
1702          IN HANDLE hThread);
1703
1704 typedef VOID (PALAPI *PAPCFUNC)(ULONG_PTR dwParam);
1705
1706 PALIMPORT
1707 DWORD
1708 PALAPI
1709 QueueUserAPC(
1710          IN PAPCFUNC pfnAPC,
1711          IN HANDLE hThread,
1712          IN ULONG_PTR dwData);
1713
1714 #ifdef _X86_
1715
1716 //
1717 // ***********************************************************************************
1718 //
1719 // NOTE: These context definitions are replicated in ndp/clr/src/debug/inc/DbgTargetContext.h (for the
1720 // purposes manipulating contexts from different platforms during remote debugging). Be sure to keep those
1721 // definitions in sync if you make any changes here.
1722 //
1723 // ***********************************************************************************
1724 //
1725
1726 #define SIZE_OF_80387_REGISTERS      80
1727
1728 #define CONTEXT_i386            0x00010000
1729 #define CONTEXT_CONTROL         (CONTEXT_i386 | 0x00000001L) // SS:SP, CS:IP, FLAGS, BP
1730 #define CONTEXT_INTEGER         (CONTEXT_i386 | 0x00000002L) // AX, BX, CX, DX, SI, DI
1731 #define CONTEXT_SEGMENTS        (CONTEXT_i386 | 0x00000004L)
1732 #define CONTEXT_FLOATING_POINT  (CONTEXT_i386 | 0x00000008L) // 387 state
1733 #define CONTEXT_DEBUG_REGISTERS (CONTEXT_i386 | 0x00000010L)
1734
1735 #define CONTEXT_FULL (CONTEXT_CONTROL | CONTEXT_INTEGER | CONTEXT_SEGMENTS)
1736 #define CONTEXT_EXTENDED_REGISTERS  (CONTEXT_i386 | 0x00000020L)
1737 #define CONTEXT_ALL (CONTEXT_CONTROL | CONTEXT_INTEGER | CONTEXT_SEGMENTS | CONTEXT_FLOATING_POINT | CONTEXT_DEBUG_REGISTERS | CONTEXT_EXTENDED_REGISTERS)
1738
1739 #define MAXIMUM_SUPPORTED_EXTENSION     512
1740
1741 #define CONTEXT_XSTATE (CONTEXT_i386 | 0x40L)
1742
1743 #define CONTEXT_EXCEPTION_ACTIVE 0x8000000L
1744 #define CONTEXT_SERVICE_ACTIVE 0x10000000L
1745 #define CONTEXT_EXCEPTION_REQUEST 0x40000000L
1746 #define CONTEXT_EXCEPTION_REPORTING 0x80000000L
1747
1748 //
1749 // This flag is set by the unwinder if it has unwound to a call
1750 // site, and cleared whenever it unwinds through a trap frame.
1751 // It is used by language-specific exception handlers to help
1752 // differentiate exception scopes during dispatching.
1753 //
1754
1755 #define CONTEXT_UNWOUND_TO_CALL 0x20000000
1756
1757 typedef struct _FLOATING_SAVE_AREA {
1758     DWORD   ControlWord;
1759     DWORD   StatusWord;
1760     DWORD   TagWord;
1761     DWORD   ErrorOffset;
1762     DWORD   ErrorSelector;
1763     DWORD   DataOffset;
1764     DWORD   DataSelector;
1765     BYTE    RegisterArea[SIZE_OF_80387_REGISTERS];
1766     DWORD   Cr0NpxState;
1767 } FLOATING_SAVE_AREA;
1768
1769 typedef FLOATING_SAVE_AREA *PFLOATING_SAVE_AREA;
1770
1771 typedef struct _CONTEXT {
1772     ULONG ContextFlags;
1773
1774     ULONG   Dr0_PAL_Undefined;
1775     ULONG   Dr1_PAL_Undefined;
1776     ULONG   Dr2_PAL_Undefined;
1777     ULONG   Dr3_PAL_Undefined;
1778     ULONG   Dr6_PAL_Undefined;
1779     ULONG   Dr7_PAL_Undefined;
1780
1781     FLOATING_SAVE_AREA FloatSave;
1782
1783     ULONG   SegGs_PAL_Undefined;
1784     ULONG   SegFs_PAL_Undefined;
1785     ULONG   SegEs_PAL_Undefined;
1786     ULONG   SegDs_PAL_Undefined;
1787
1788     ULONG   Edi;
1789     ULONG   Esi;
1790     ULONG   Ebx;
1791     ULONG   Edx;
1792     ULONG   Ecx;
1793     ULONG   Eax;
1794
1795     ULONG   Ebp;
1796     ULONG   Eip;
1797     ULONG   SegCs;
1798     ULONG   EFlags;
1799     ULONG   Esp;
1800     ULONG   SegSs;
1801
1802     UCHAR   ExtendedRegisters[MAXIMUM_SUPPORTED_EXTENSION];
1803 } CONTEXT, *PCONTEXT, *LPCONTEXT;
1804
1805 // To support saving and loading xmm register context we need to know the offset in the ExtendedRegisters
1806 // section at which they are stored. This has been determined experimentally since I have found no
1807 // documentation thus far but it corresponds to the offset we'd expect if a fxsave instruction was used to
1808 // store the regular FP state along with the XMM registers at the start of the extended registers section.
1809 // Technically the offset doesn't really matter if no code in the PAL or runtime knows what the offset should
1810 // be either (as long as we're consistent across GetThreadContext() and SetThreadContext() and we don't
1811 // support any other values in the ExtendedRegisters) but we might as well be as accurate as we can.
1812 #define CONTEXT_EXREG_XMM_OFFSET 160
1813
1814 typedef struct _KNONVOLATILE_CONTEXT {
1815
1816     DWORD Edi;
1817     DWORD Esi;
1818     DWORD Ebx;
1819     DWORD Ebp;
1820
1821 } KNONVOLATILE_CONTEXT, *PKNONVOLATILE_CONTEXT;
1822
1823 typedef struct _KNONVOLATILE_CONTEXT_POINTERS {
1824
1825     // The ordering of these fields should be aligned with that
1826     // of corresponding fields in CONTEXT
1827     //
1828     // (See FillRegDisplay in inc/regdisp.h for details)
1829     PDWORD Edi;
1830     PDWORD Esi;
1831     PDWORD Ebx;
1832     PDWORD Edx;
1833     PDWORD Ecx;
1834     PDWORD Eax;
1835
1836     PDWORD Ebp;
1837
1838 } KNONVOLATILE_CONTEXT_POINTERS, *PKNONVOLATILE_CONTEXT_POINTERS;
1839
1840 #elif defined(_AMD64_)
1841 // copied from winnt.h
1842
1843 #define CONTEXT_AMD64   0x100000
1844
1845 #define CONTEXT_CONTROL (CONTEXT_AMD64 | 0x1L)
1846 #define CONTEXT_INTEGER (CONTEXT_AMD64 | 0x2L)
1847 #define CONTEXT_SEGMENTS (CONTEXT_AMD64 | 0x4L)
1848 #define CONTEXT_FLOATING_POINT  (CONTEXT_AMD64 | 0x8L)
1849 #define CONTEXT_DEBUG_REGISTERS (CONTEXT_AMD64 | 0x10L)
1850
1851 #define CONTEXT_FULL (CONTEXT_CONTROL | CONTEXT_INTEGER | CONTEXT_FLOATING_POINT)
1852
1853 #define CONTEXT_ALL (CONTEXT_CONTROL | CONTEXT_INTEGER | CONTEXT_SEGMENTS | CONTEXT_FLOATING_POINT | CONTEXT_DEBUG_REGISTERS)
1854
1855 #define CONTEXT_XSTATE (CONTEXT_AMD64 | 0x40L)
1856
1857 #define CONTEXT_EXCEPTION_ACTIVE 0x8000000
1858 #define CONTEXT_SERVICE_ACTIVE 0x10000000
1859 #define CONTEXT_EXCEPTION_REQUEST 0x40000000
1860 #define CONTEXT_EXCEPTION_REPORTING 0x80000000
1861
1862 typedef struct DECLSPEC_ALIGN(16) _M128A {
1863     ULONGLONG Low;
1864     LONGLONG High;
1865 } M128A, *PM128A;
1866
1867 typedef struct _XMM_SAVE_AREA32 {
1868     WORD   ControlWord;
1869     WORD   StatusWord;
1870     BYTE  TagWord;
1871     BYTE  Reserved1;
1872     WORD   ErrorOpcode;
1873     DWORD ErrorOffset;
1874     WORD   ErrorSelector;
1875     WORD   Reserved2;
1876     DWORD DataOffset;
1877     WORD   DataSelector;
1878     WORD   Reserved3;
1879     DWORD MxCsr;
1880     DWORD MxCsr_Mask;
1881     M128A FloatRegisters[8];
1882     M128A XmmRegisters[16];
1883     BYTE  Reserved4[96];
1884 } XMM_SAVE_AREA32, *PXMM_SAVE_AREA32;
1885
1886 #define LEGACY_SAVE_AREA_LENGTH sizeof(XMM_SAVE_AREA32)
1887
1888 //
1889 // Context Frame
1890 //
1891 //  This frame has a several purposes: 1) it is used as an argument to
1892 //  NtContinue, 2) is is used to constuct a call frame for APC delivery,
1893 //  and 3) it is used in the user level thread creation routines.
1894 //
1895 //
1896 // The flags field within this record controls the contents of a CONTEXT
1897 // record.
1898 //
1899 // If the context record is used as an input parameter, then for each
1900 // portion of the context record controlled by a flag whose value is
1901 // set, it is assumed that that portion of the context record contains
1902 // valid context. If the context record is being used to modify a threads
1903 // context, then only that portion of the threads context is modified.
1904 //
1905 // If the context record is used as an output parameter to capture the
1906 // context of a thread, then only those portions of the thread's context
1907 // corresponding to set flags will be returned.
1908 //
1909 // CONTEXT_CONTROL specifies SegSs, Rsp, SegCs, Rip, and EFlags.
1910 //
1911 // CONTEXT_INTEGER specifies Rax, Rcx, Rdx, Rbx, Rbp, Rsi, Rdi, and R8-R15.
1912 //
1913 // CONTEXT_SEGMENTS specifies SegDs, SegEs, SegFs, and SegGs.
1914 //
1915 // CONTEXT_DEBUG_REGISTERS specifies Dr0-Dr3 and Dr6-Dr7.
1916 //
1917 // CONTEXT_MMX_REGISTERS specifies the floating point and extended registers
1918 //     Mm0/St0-Mm7/St7 and Xmm0-Xmm15).
1919 //
1920
1921 typedef struct DECLSPEC_ALIGN(16) _CONTEXT {
1922
1923     //
1924     // Register parameter home addresses.
1925     //
1926     // N.B. These fields are for convience - they could be used to extend the
1927     //      context record in the future.
1928     //
1929
1930     DWORD64 P1Home;
1931     DWORD64 P2Home;
1932     DWORD64 P3Home;
1933     DWORD64 P4Home;
1934     DWORD64 P5Home;
1935     DWORD64 P6Home;
1936
1937     //
1938     // Control flags.
1939     //
1940
1941     DWORD ContextFlags;
1942     DWORD MxCsr;
1943
1944     //
1945     // Segment Registers and processor flags.
1946     //
1947
1948     WORD   SegCs;
1949     WORD   SegDs;
1950     WORD   SegEs;
1951     WORD   SegFs;
1952     WORD   SegGs;
1953     WORD   SegSs;
1954     DWORD EFlags;
1955
1956     //
1957     // Debug registers
1958     //
1959
1960     DWORD64 Dr0;
1961     DWORD64 Dr1;
1962     DWORD64 Dr2;
1963     DWORD64 Dr3;
1964     DWORD64 Dr6;
1965     DWORD64 Dr7;
1966
1967     //
1968     // Integer registers.
1969     //
1970
1971     DWORD64 Rax;
1972     DWORD64 Rcx;
1973     DWORD64 Rdx;
1974     DWORD64 Rbx;
1975     DWORD64 Rsp;
1976     DWORD64 Rbp;
1977     DWORD64 Rsi;
1978     DWORD64 Rdi;
1979     DWORD64 R8;
1980     DWORD64 R9;
1981     DWORD64 R10;
1982     DWORD64 R11;
1983     DWORD64 R12;
1984     DWORD64 R13;
1985     DWORD64 R14;
1986     DWORD64 R15;
1987
1988     //
1989     // Program counter.
1990     //
1991
1992     DWORD64 Rip;
1993
1994     //
1995     // Floating point state.
1996     //
1997
1998     union {
1999         XMM_SAVE_AREA32 FltSave;
2000         struct {
2001             M128A Header[2];
2002             M128A Legacy[8];
2003             M128A Xmm0;
2004             M128A Xmm1;
2005             M128A Xmm2;
2006             M128A Xmm3;
2007             M128A Xmm4;
2008             M128A Xmm5;
2009             M128A Xmm6;
2010             M128A Xmm7;
2011             M128A Xmm8;
2012             M128A Xmm9;
2013             M128A Xmm10;
2014             M128A Xmm11;
2015             M128A Xmm12;
2016             M128A Xmm13;
2017             M128A Xmm14;
2018             M128A Xmm15;
2019         };
2020     };
2021
2022     //
2023     // Vector registers.
2024     //
2025
2026     M128A VectorRegister[26];
2027     DWORD64 VectorControl;
2028
2029     //
2030     // Special debug control registers.
2031     //
2032
2033     DWORD64 DebugControl;
2034     DWORD64 LastBranchToRip;
2035     DWORD64 LastBranchFromRip;
2036     DWORD64 LastExceptionToRip;
2037     DWORD64 LastExceptionFromRip;
2038 } CONTEXT, *PCONTEXT, *LPCONTEXT;
2039
2040 //
2041 // Nonvolatile context pointer record.
2042 //
2043
2044 typedef struct _KNONVOLATILE_CONTEXT_POINTERS {
2045     union {
2046         PM128A FloatingContext[16];
2047         struct {
2048             PM128A Xmm0;
2049             PM128A Xmm1;
2050             PM128A Xmm2;
2051             PM128A Xmm3;
2052             PM128A Xmm4;
2053             PM128A Xmm5;
2054             PM128A Xmm6;
2055             PM128A Xmm7;
2056             PM128A Xmm8;
2057             PM128A Xmm9;
2058             PM128A Xmm10;
2059             PM128A Xmm11;
2060             PM128A Xmm12;
2061             PM128A Xmm13;
2062             PM128A Xmm14;
2063             PM128A Xmm15;
2064         } ;
2065     } ;
2066
2067     union {
2068         PDWORD64 IntegerContext[16];
2069         struct {
2070             PDWORD64 Rax;
2071             PDWORD64 Rcx;
2072             PDWORD64 Rdx;
2073             PDWORD64 Rbx;
2074             PDWORD64 Rsp;
2075             PDWORD64 Rbp;
2076             PDWORD64 Rsi;
2077             PDWORD64 Rdi;
2078             PDWORD64 R8;
2079             PDWORD64 R9;
2080             PDWORD64 R10;
2081             PDWORD64 R11;
2082             PDWORD64 R12;
2083             PDWORD64 R13;
2084             PDWORD64 R14;
2085             PDWORD64 R15;
2086         } ;
2087     } ;
2088
2089 } KNONVOLATILE_CONTEXT_POINTERS, *PKNONVOLATILE_CONTEXT_POINTERS;
2090
2091 #elif defined(_ARM_)
2092
2093 #define CONTEXT_ARM   0x00200000L
2094
2095 // end_wx86
2096
2097 #define CONTEXT_CONTROL (CONTEXT_ARM | 0x1L)
2098 #define CONTEXT_INTEGER (CONTEXT_ARM | 0x2L)
2099 #define CONTEXT_FLOATING_POINT  (CONTEXT_ARM | 0x4L)
2100 #define CONTEXT_DEBUG_REGISTERS (CONTEXT_ARM | 0x8L)
2101
2102 #define CONTEXT_FULL (CONTEXT_CONTROL | CONTEXT_INTEGER | CONTEXT_FLOATING_POINT)
2103
2104 #define CONTEXT_ALL (CONTEXT_CONTROL | CONTEXT_INTEGER | CONTEXT_FLOATING_POINT | CONTEXT_DEBUG_REGISTERS)
2105
2106 #define CONTEXT_EXCEPTION_ACTIVE 0x8000000L
2107 #define CONTEXT_SERVICE_ACTIVE 0x10000000L
2108 #define CONTEXT_EXCEPTION_REQUEST 0x40000000L
2109 #define CONTEXT_EXCEPTION_REPORTING 0x80000000L
2110
2111 //
2112 // This flag is set by the unwinder if it has unwound to a call
2113 // site, and cleared whenever it unwinds through a trap frame.
2114 // It is used by language-specific exception handlers to help
2115 // differentiate exception scopes during dispatching.
2116 //
2117
2118 #define CONTEXT_UNWOUND_TO_CALL 0x20000000
2119
2120 //
2121 // Specify the number of breakpoints and watchpoints that the OS
2122 // will track. Architecturally, ARM supports up to 16. In practice,
2123 // however, almost no one implements more than 4 of each.
2124 //
2125
2126 #define ARM_MAX_BREAKPOINTS     8
2127 #define ARM_MAX_WATCHPOINTS     1
2128
2129 typedef struct _NEON128 {
2130     ULONGLONG Low;
2131     LONGLONG High;
2132 } NEON128, *PNEON128;
2133
2134 //
2135 // Context Frame
2136 //
2137 //  This frame has a several purposes: 1) it is used as an argument to
2138 //  NtContinue, 2) it is used to constuct a call frame for APC delivery,
2139 //  and 3) it is used in the user level thread creation routines.
2140 //
2141 //
2142 // The flags field within this record controls the contents of a CONTEXT
2143 // record.
2144 //
2145 // If the context record is used as an input parameter, then for each
2146 // portion of the context record controlled by a flag whose value is
2147 // set, it is assumed that that portion of the context record contains
2148 // valid context. If the context record is being used to modify a threads
2149 // context, then only that portion of the threads context is modified.
2150 //
2151 // If the context record is used as an output parameter to capture the
2152 // context of a thread, then only those portions of the thread's context
2153 // corresponding to set flags will be returned.
2154 //
2155 // CONTEXT_CONTROL specifies Sp, Lr, Pc, and Cpsr
2156 //
2157 // CONTEXT_INTEGER specifies R0-R12
2158 //
2159 // CONTEXT_FLOATING_POINT specifies Q0-Q15 / D0-D31 / S0-S31
2160 //
2161 // CONTEXT_DEBUG_REGISTERS specifies up to 16 of DBGBVR, DBGBCR, DBGWVR,
2162 //      DBGWCR.
2163 //
2164
2165 typedef struct DECLSPEC_ALIGN(8) _CONTEXT {
2166
2167     //
2168     // Control flags.
2169     //
2170
2171     DWORD ContextFlags;
2172
2173     //
2174     // Integer registers
2175     //
2176
2177     DWORD R0;
2178     DWORD R1;
2179     DWORD R2;
2180     DWORD R3;
2181     DWORD R4;
2182     DWORD R5;
2183     DWORD R6;
2184     DWORD R7;
2185     DWORD R8;
2186     DWORD R9;
2187     DWORD R10;
2188     DWORD R11;
2189     DWORD R12;
2190
2191     //
2192     // Control Registers
2193     //
2194
2195     DWORD Sp;
2196     DWORD Lr;
2197     DWORD Pc;
2198     DWORD Cpsr;
2199
2200     //
2201     // Floating Point/NEON Registers
2202     //
2203
2204     DWORD Fpscr;
2205     DWORD Padding;
2206     union {
2207         NEON128 Q[16];
2208         ULONGLONG D[32];
2209         DWORD S[32];
2210     };
2211
2212     //
2213     // Debug registers
2214     //
2215
2216     DWORD Bvr[ARM_MAX_BREAKPOINTS];
2217     DWORD Bcr[ARM_MAX_BREAKPOINTS];
2218     DWORD Wvr[ARM_MAX_WATCHPOINTS];
2219     DWORD Wcr[ARM_MAX_WATCHPOINTS];
2220
2221     DWORD Padding2[2];
2222
2223 } CONTEXT, *PCONTEXT, *LPCONTEXT;
2224
2225 //
2226 // Nonvolatile context pointer record.
2227 //
2228
2229 typedef struct _KNONVOLATILE_CONTEXT_POINTERS {
2230
2231     PDWORD R4;
2232     PDWORD R5;
2233     PDWORD R6;
2234     PDWORD R7;
2235     PDWORD R8;
2236     PDWORD R9;
2237     PDWORD R10;
2238     PDWORD R11;
2239     PDWORD Lr;
2240
2241     PULONGLONG D8;
2242     PULONGLONG D9;
2243     PULONGLONG D10;
2244     PULONGLONG D11;
2245     PULONGLONG D12;
2246     PULONGLONG D13;
2247     PULONGLONG D14;
2248     PULONGLONG D15;
2249
2250 } KNONVOLATILE_CONTEXT_POINTERS, *PKNONVOLATILE_CONTEXT_POINTERS;
2251
2252 typedef struct _IMAGE_ARM_RUNTIME_FUNCTION_ENTRY {
2253     DWORD BeginAddress;
2254     DWORD EndAddress;
2255     union {
2256         DWORD UnwindData;
2257         struct {
2258             DWORD Flag : 2;
2259             DWORD FunctionLength : 11;
2260             DWORD Ret : 2;
2261             DWORD H : 1;
2262             DWORD Reg : 3;
2263             DWORD R : 1;
2264             DWORD L : 1;
2265             DWORD C : 1;
2266             DWORD StackAdjust : 10;
2267         };
2268     };
2269 } IMAGE_ARM_RUNTIME_FUNCTION_ENTRY, * PIMAGE_ARM_RUNTIME_FUNCTION_ENTRY;
2270
2271 #elif defined(_ARM64_)
2272
2273 #define CONTEXT_ARM64   0x00400000L
2274
2275 #define CONTEXT_CONTROL (CONTEXT_ARM64 | 0x1L)
2276 #define CONTEXT_INTEGER (CONTEXT_ARM64 | 0x2L)
2277 #define CONTEXT_FLOATING_POINT  (CONTEXT_ARM64 | 0x4L)
2278 #define CONTEXT_DEBUG_REGISTERS (CONTEXT_ARM64 | 0x8L)
2279
2280 #define CONTEXT_FULL (CONTEXT_CONTROL | CONTEXT_INTEGER | CONTEXT_FLOATING_POINT)
2281
2282 #define CONTEXT_ALL (CONTEXT_CONTROL | CONTEXT_INTEGER | CONTEXT_FLOATING_POINT | CONTEXT_DEBUG_REGISTERS)
2283
2284 #define CONTEXT_EXCEPTION_ACTIVE 0x8000000L
2285 #define CONTEXT_SERVICE_ACTIVE 0x10000000L
2286 #define CONTEXT_EXCEPTION_REQUEST 0x40000000L
2287 #define CONTEXT_EXCEPTION_REPORTING 0x80000000L
2288
2289 //
2290 // This flag is set by the unwinder if it has unwound to a call
2291 // site, and cleared whenever it unwinds through a trap frame.
2292 // It is used by language-specific exception handlers to help
2293 // differentiate exception scopes during dispatching.
2294 //
2295
2296 #define CONTEXT_UNWOUND_TO_CALL 0x20000000
2297
2298 //
2299 // Define initial Cpsr/Fpscr value
2300 //
2301
2302 #define INITIAL_CPSR 0x10
2303 #define INITIAL_FPSCR 0
2304
2305 // begin_ntoshvp
2306
2307 //
2308 // Specify the number of breakpoints and watchpoints that the OS
2309 // will track. Architecturally, ARM64 supports up to 16. In practice,
2310 // however, almost no one implements more than 4 of each.
2311 //
2312
2313 #define ARM64_MAX_BREAKPOINTS     8
2314 #define ARM64_MAX_WATCHPOINTS     2
2315
2316 //
2317 // Context Frame
2318 //
2319 //  This frame has a several purposes: 1) it is used as an argument to
2320 //  NtContinue, 2) it is used to constuct a call frame for APC delivery,
2321 //  and 3) it is used in the user level thread creation routines.
2322 //
2323 //
2324 // The flags field within this record controls the contents of a CONTEXT
2325 // record.
2326 //
2327 // If the context record is used as an input parameter, then for each
2328 // portion of the context record controlled by a flag whose value is
2329 // set, it is assumed that that portion of the context record contains
2330 // valid context. If the context record is being used to modify a threads
2331 // context, then only that portion of the threads context is modified.
2332 //
2333 // If the context record is used as an output parameter to capture the
2334 // context of a thread, then only those portions of the thread's context
2335 // corresponding to set flags will be returned.
2336 //
2337 // CONTEXT_CONTROL specifies Sp, Lr, Pc, and Cpsr
2338 //
2339 // CONTEXT_INTEGER specifies R0-R12
2340 //
2341 // CONTEXT_FLOATING_POINT specifies Q0-Q15 / D0-D31 / S0-S31
2342 //
2343 // CONTEXT_DEBUG_REGISTERS specifies up to 16 of DBGBVR, DBGBCR, DBGWVR,
2344 //      DBGWCR.
2345 //
2346
2347 typedef struct _NEON128 {
2348     ULONGLONG Low;
2349     LONGLONG High;
2350 } NEON128, *PNEON128;
2351
2352 typedef struct DECLSPEC_ALIGN(16) _CONTEXT {
2353
2354     //
2355     // Control flags.
2356     //
2357
2358     /* +0x000 */ DWORD ContextFlags;
2359
2360     //
2361     // Integer registers
2362     //
2363
2364     /* +0x004 */ DWORD Cpsr;       // NZVF + DAIF + CurrentEL + SPSel
2365     /* +0x008 */ union {
2366                     struct {
2367                         DWORD64 X0;
2368                         DWORD64 X1;
2369                         DWORD64 X2;
2370                         DWORD64 X3;
2371                         DWORD64 X4;
2372                         DWORD64 X5;
2373                         DWORD64 X6;
2374                         DWORD64 X7;
2375                         DWORD64 X8;
2376                         DWORD64 X9;
2377                         DWORD64 X10;
2378                         DWORD64 X11;
2379                         DWORD64 X12;
2380                         DWORD64 X13;
2381                         DWORD64 X14;
2382                         DWORD64 X15;
2383                         DWORD64 X16;
2384                         DWORD64 X17;
2385                         DWORD64 X18;
2386                         DWORD64 X19;
2387                         DWORD64 X20;
2388                         DWORD64 X21;
2389                         DWORD64 X22;
2390                         DWORD64 X23;
2391                         DWORD64 X24;
2392                         DWORD64 X25;
2393                         DWORD64 X26;
2394                         DWORD64 X27;
2395                         DWORD64 X28;
2396                     };
2397                     DWORD64 X[29];
2398                 };
2399     /* +0x0f0 */ DWORD64 Fp;
2400     /* +0x0f8 */ DWORD64 Lr;
2401     /* +0x100 */ DWORD64 Sp;
2402     /* +0x108 */ DWORD64 Pc;
2403
2404     //
2405     // Floating Point/NEON Registers
2406     //
2407
2408     /* +0x110 */ NEON128 V[32];
2409     /* +0x310 */ DWORD Fpcr;
2410     /* +0x314 */ DWORD Fpsr;
2411
2412     //
2413     // Debug registers
2414     //
2415
2416     /* +0x318 */ DWORD Bcr[ARM64_MAX_BREAKPOINTS];
2417     /* +0x338 */ DWORD64 Bvr[ARM64_MAX_BREAKPOINTS];
2418     /* +0x378 */ DWORD Wcr[ARM64_MAX_WATCHPOINTS];
2419     /* +0x380 */ DWORD64 Wvr[ARM64_MAX_WATCHPOINTS];
2420     /* +0x390 */
2421
2422 } CONTEXT, *PCONTEXT, *LPCONTEXT;
2423
2424 //
2425 // Nonvolatile context pointer record.
2426 //
2427
2428 typedef struct _KNONVOLATILE_CONTEXT_POINTERS {
2429
2430     PDWORD64 X19;
2431     PDWORD64 X20;
2432     PDWORD64 X21;
2433     PDWORD64 X22;
2434     PDWORD64 X23;
2435     PDWORD64 X24;
2436     PDWORD64 X25;
2437     PDWORD64 X26;
2438     PDWORD64 X27;
2439     PDWORD64 X28;
2440     PDWORD64 Fp;
2441     PDWORD64 Lr;
2442
2443     PDWORD64 D8;
2444     PDWORD64 D9;
2445     PDWORD64 D10;
2446     PDWORD64 D11;
2447     PDWORD64 D12;
2448     PDWORD64 D13;
2449     PDWORD64 D14;
2450     PDWORD64 D15;
2451
2452 } KNONVOLATILE_CONTEXT_POINTERS, *PKNONVOLATILE_CONTEXT_POINTERS;
2453
2454 #else
2455 #error Unknown architecture for defining CONTEXT.
2456 #endif
2457
2458
2459 PALIMPORT
2460 BOOL
2461 PALAPI
2462 GetThreadContext(
2463          IN HANDLE hThread,
2464          IN OUT LPCONTEXT lpContext);
2465
2466 PALIMPORT
2467 BOOL
2468 PALAPI
2469 SetThreadContext(
2470          IN HANDLE hThread,
2471          IN CONST CONTEXT *lpContext);
2472
2473 #define THREAD_BASE_PRIORITY_LOWRT    15
2474 #define THREAD_BASE_PRIORITY_MAX      2
2475 #define THREAD_BASE_PRIORITY_MIN      (-2)
2476 #define THREAD_BASE_PRIORITY_IDLE     (-15)
2477
2478 #define THREAD_PRIORITY_LOWEST        THREAD_BASE_PRIORITY_MIN
2479 #define THREAD_PRIORITY_BELOW_NORMAL  (THREAD_PRIORITY_LOWEST+1)
2480 #define THREAD_PRIORITY_NORMAL        0
2481 #define THREAD_PRIORITY_HIGHEST       THREAD_BASE_PRIORITY_MAX
2482 #define THREAD_PRIORITY_ABOVE_NORMAL  (THREAD_PRIORITY_HIGHEST-1)
2483 #define THREAD_PRIORITY_ERROR_RETURN  (MAXLONG)
2484
2485 #define THREAD_PRIORITY_TIME_CRITICAL THREAD_BASE_PRIORITY_LOWRT
2486 #define THREAD_PRIORITY_IDLE          THREAD_BASE_PRIORITY_IDLE
2487
2488 PALIMPORT
2489 int
2490 PALAPI
2491 GetThreadPriority(
2492           IN HANDLE hThread);
2493
2494 PALIMPORT
2495 BOOL
2496 PALAPI
2497 SetThreadPriority(
2498           IN HANDLE hThread,
2499           IN int nPriority);
2500
2501 PALIMPORT
2502 BOOL
2503 PALAPI
2504 GetThreadTimes(
2505         IN HANDLE hThread,
2506         OUT LPFILETIME lpCreationTime,
2507         OUT LPFILETIME lpExitTime,
2508         OUT LPFILETIME lpKernelTime,
2509         OUT LPFILETIME lpUserTime);
2510     
2511 #define TLS_OUT_OF_INDEXES ((DWORD)0xFFFFFFFF)
2512
2513 PALIMPORT
2514 DWORD
2515 PALAPI
2516 TlsAlloc(
2517      VOID);
2518
2519 PALIMPORT
2520 LPVOID
2521 PALAPI
2522 TlsGetValue(
2523         IN DWORD dwTlsIndex);
2524
2525 PALIMPORT
2526 BOOL
2527 PALAPI
2528 TlsSetValue(
2529         IN DWORD dwTlsIndex,
2530         IN LPVOID lpTlsValue);
2531
2532 PALIMPORT
2533 BOOL
2534 PALAPI
2535 TlsFree(
2536     IN DWORD dwTlsIndex);
2537
2538 PALIMPORT
2539 void *
2540 PALAPI
2541 PAL_GetStackBase(VOID);
2542
2543 PALIMPORT
2544 void *
2545 PALAPI
2546 PAL_GetStackLimit(VOID);
2547
2548 PALIMPORT
2549 DWORD
2550 PALAPI
2551 PAL_GetLogicalCpuCountFromOS(VOID);
2552
2553 PALIMPORT
2554 size_t
2555 PALAPI
2556 PAL_GetRestrictedPhysicalMemoryLimit(VOID);
2557
2558 PALIMPORT
2559 BOOL
2560 PALAPI
2561 PAL_GetWorkingSetSize(size_t* val);
2562
2563 PALIMPORT
2564 BOOL
2565 PALAPI
2566 PAL_GetCpuLimit(UINT* val);
2567
2568 PALIMPORT
2569 size_t
2570 PALAPI
2571 PAL_GetLogicalProcessorCacheSizeFromOS(VOID);
2572
2573 typedef BOOL(*UnwindReadMemoryCallback)(PVOID address, PVOID buffer, SIZE_T size);
2574
2575 PALIMPORT BOOL PALAPI PAL_VirtualUnwind(CONTEXT *context, KNONVOLATILE_CONTEXT_POINTERS *contextPointers);
2576
2577 PALIMPORT BOOL PALAPI PAL_VirtualUnwindOutOfProc(CONTEXT *context, KNONVOLATILE_CONTEXT_POINTERS *contextPointers, SIZE_T baseAddress, UnwindReadMemoryCallback readMemoryCallback);
2578
2579 #define GetLogicalProcessorCacheSizeFromOS PAL_GetLogicalProcessorCacheSizeFromOS
2580
2581 /* PAL_CS_NATIVE_DATA_SIZE is defined as sizeof(PAL_CRITICAL_SECTION_NATIVE_DATA) */
2582
2583 #if defined(__APPLE__) && defined(__i386__)
2584 #define PAL_CS_NATIVE_DATA_SIZE 76
2585 #elif defined(__APPLE__) && defined(__x86_64__)
2586 #define PAL_CS_NATIVE_DATA_SIZE 120
2587 #elif defined(__FreeBSD__) && defined(_X86_)
2588 #define PAL_CS_NATIVE_DATA_SIZE 12
2589 #elif defined(__FreeBSD__) && defined(__x86_64__)
2590 #define PAL_CS_NATIVE_DATA_SIZE 24
2591 #elif defined(__linux__) && defined(_ARM_)
2592 #define PAL_CS_NATIVE_DATA_SIZE 80
2593 #elif defined(__linux__) && defined(_ARM64_)
2594 #define PAL_CS_NATIVE_DATA_SIZE 116
2595 #elif defined(__linux__) && defined(__i386__)
2596 #define PAL_CS_NATIVE_DATA_SIZE 76
2597 #elif defined(__linux__) && defined(__x86_64__)
2598 #define PAL_CS_NATIVE_DATA_SIZE 96
2599 #elif defined(__NetBSD__) && defined(__amd64__)
2600 #define PAL_CS_NATIVE_DATA_SIZE 96
2601 #elif defined(__NetBSD__) && defined(__earm__)
2602 #define PAL_CS_NATIVE_DATA_SIZE 56
2603 #elif defined(__NetBSD__) && defined(__i386__)
2604 #define PAL_CS_NATIVE_DATA_SIZE 56
2605 #else 
2606 #warning 
2607 #error  PAL_CS_NATIVE_DATA_SIZE is not defined for this architecture
2608 #endif
2609     
2610 // 
2611 typedef struct _CRITICAL_SECTION {
2612     PVOID DebugInfo;
2613     LONG LockCount;
2614     LONG RecursionCount;
2615     HANDLE OwningThread;
2616     HANDLE LockSemaphore;
2617     ULONG_PTR SpinCount;
2618
2619     BOOL bInternal;
2620     volatile DWORD dwInitState;
2621     union CSNativeDataStorage
2622     {
2623         BYTE rgNativeDataStorage[PAL_CS_NATIVE_DATA_SIZE]; 
2624         VOID * pvAlign; // make sure the storage is machine-pointer-size aligned
2625     } csnds;    
2626 } CRITICAL_SECTION, *PCRITICAL_SECTION, *LPCRITICAL_SECTION;
2627
2628 PALIMPORT VOID PALAPI EnterCriticalSection(IN OUT LPCRITICAL_SECTION lpCriticalSection);
2629 PALIMPORT VOID PALAPI LeaveCriticalSection(IN OUT LPCRITICAL_SECTION lpCriticalSection);
2630 PALIMPORT VOID PALAPI InitializeCriticalSection(OUT LPCRITICAL_SECTION lpCriticalSection);
2631 PALIMPORT BOOL PALAPI InitializeCriticalSectionEx(LPCRITICAL_SECTION lpCriticalSection, DWORD dwSpinCount, DWORD Flags);
2632 PALIMPORT VOID PALAPI DeleteCriticalSection(IN OUT LPCRITICAL_SECTION lpCriticalSection);
2633 PALIMPORT BOOL PALAPI TryEnterCriticalSection(IN OUT LPCRITICAL_SECTION lpCriticalSection);
2634
2635 #define SEM_FAILCRITICALERRORS          0x0001
2636 #define SEM_NOOPENFILEERRORBOX          0x8000
2637
2638 PALIMPORT
2639 UINT
2640 PALAPI
2641 SetErrorMode(
2642          IN UINT uMode);
2643
2644 #define PAGE_NOACCESS                   0x01
2645 #define PAGE_READONLY                   0x02
2646 #define PAGE_READWRITE                  0x04
2647 #define PAGE_WRITECOPY                  0x08
2648 #define PAGE_EXECUTE                    0x10
2649 #define PAGE_EXECUTE_READ               0x20
2650 #define PAGE_EXECUTE_READWRITE          0x40
2651 #define PAGE_EXECUTE_WRITECOPY          0x80
2652 #define MEM_COMMIT                      0x1000
2653 #define MEM_RESERVE                     0x2000
2654 #define MEM_DECOMMIT                    0x4000
2655 #define MEM_RELEASE                     0x8000
2656 #define MEM_RESET                       0x80000
2657 #define MEM_FREE                        0x10000
2658 #define MEM_PRIVATE                     0x20000
2659 #define MEM_MAPPED                      0x40000
2660 #define MEM_TOP_DOWN                    0x100000
2661 #define MEM_WRITE_WATCH                 0x200000
2662 #define MEM_RESERVE_EXECUTABLE          0x40000000 // reserve memory using executable memory allocator
2663
2664 PALIMPORT
2665 HANDLE
2666 PALAPI
2667 CreateFileMappingW(
2668            IN HANDLE hFile,
2669            IN LPSECURITY_ATTRIBUTES lpFileMappingAttributes,
2670            IN DWORD flProtect,
2671            IN DWORD dwMaxmimumSizeHigh,
2672            IN DWORD dwMaximumSizeLow,
2673            IN LPCWSTR lpName);
2674
2675 #ifdef UNICODE
2676 #define CreateFileMapping CreateFileMappingW
2677 #else
2678 #define CreateFileMapping CreateFileMappingA
2679 #endif
2680
2681 #define SECTION_QUERY       0x0001
2682 #define SECTION_MAP_WRITE   0x0002
2683 #define SECTION_MAP_READ    0x0004
2684 #define SECTION_ALL_ACCESS  (SECTION_MAP_READ | SECTION_MAP_WRITE) // diff from winnt.h
2685
2686 #define FILE_MAP_WRITE      SECTION_MAP_WRITE
2687 #define FILE_MAP_READ       SECTION_MAP_READ
2688 #define FILE_MAP_ALL_ACCESS SECTION_ALL_ACCESS
2689 #define FILE_MAP_COPY       SECTION_QUERY
2690
2691 PALIMPORT
2692 HANDLE
2693 PALAPI
2694 OpenFileMappingW(
2695          IN DWORD dwDesiredAccess,
2696          IN BOOL bInheritHandle,
2697          IN LPCWSTR lpName);
2698
2699 #ifdef UNICODE
2700 #define OpenFileMapping OpenFileMappingW
2701 #else
2702 #define OpenFileMapping OpenFileMappingA
2703 #endif
2704
2705 PALIMPORT
2706 LPVOID
2707 PALAPI
2708 MapViewOfFile(
2709           IN HANDLE hFileMappingObject,
2710           IN DWORD dwDesiredAccess,
2711           IN DWORD dwFileOffsetHigh,
2712           IN DWORD dwFileOffsetLow,
2713           IN SIZE_T dwNumberOfBytesToMap);
2714
2715 PALIMPORT
2716 LPVOID
2717 PALAPI
2718 MapViewOfFileEx(
2719           IN HANDLE hFileMappingObject,
2720           IN DWORD dwDesiredAccess,
2721           IN DWORD dwFileOffsetHigh,
2722           IN DWORD dwFileOffsetLow,
2723           IN SIZE_T dwNumberOfBytesToMap,
2724           IN LPVOID lpBaseAddress);
2725           
2726 PALIMPORT
2727 BOOL
2728 PALAPI
2729 FlushViewOfFile(
2730         IN LPVOID lpBaseAddress,
2731         IN SIZE_T dwNumberOfBytesToFlush);
2732
2733 PALIMPORT
2734 BOOL
2735 PALAPI
2736 UnmapViewOfFile(
2737         IN LPCVOID lpBaseAddress);
2738
2739
2740 PALIMPORT
2741 HMODULE
2742 PALAPI
2743 LoadLibraryW(
2744         IN LPCWSTR lpLibFileName);
2745
2746 PALIMPORT
2747 HMODULE
2748 PALAPI
2749 LoadLibraryExW(
2750         IN LPCWSTR lpLibFileName,
2751         IN /*Reserved*/ HANDLE hFile,
2752         IN DWORD dwFlags);
2753
2754 PALIMPORT
2755 void *
2756 PALAPI
2757 PAL_LoadLibraryDirect(
2758         IN LPCWSTR lpLibFileName);
2759
2760 PALIMPORT
2761 HMODULE
2762 PALAPI
2763 PAL_RegisterLibraryDirect(
2764         IN void *dl_handle,
2765         IN LPCWSTR lpLibFileName);
2766
2767 /*++
2768 Function:
2769   PAL_LOADLoadPEFile
2770
2771 Abstract
2772   Loads a PE file into memory.  Properly maps all of the sections in the PE file.  Returns a pointer to the
2773   loaded base.
2774
2775 Parameters:
2776     IN hFile    - The file to load
2777
2778 Return value:
2779     A valid base address if successful.
2780     0 if failure
2781 --*/
2782 void *
2783 PALAPI
2784 PAL_LOADLoadPEFile(HANDLE hFile);
2785
2786 /*++
2787     PAL_LOADUnloadPEFile
2788
2789     Unload a PE file that was loaded by PAL_LOADLoadPEFile().
2790
2791 Parameters:
2792     IN ptr - the file pointer returned by PAL_LOADLoadPEFile()
2793
2794 Return value:
2795     TRUE - success
2796     FALSE - failure (incorrect ptr, etc.)
2797 --*/
2798 BOOL 
2799 PALAPI
2800 PAL_LOADUnloadPEFile(void * ptr);
2801
2802 #ifdef UNICODE
2803 #define LoadLibrary LoadLibraryW
2804 #define LoadLibraryEx LoadLibraryExW
2805 #else
2806 #define LoadLibrary LoadLibraryA
2807 #define LoadLibraryEx LoadLibraryExA
2808 #endif
2809
2810 typedef INT_PTR (PALAPI *FARPROC)();
2811
2812 PALIMPORT
2813 FARPROC
2814 PALAPI
2815 GetProcAddress(
2816     IN HMODULE hModule,
2817     IN LPCSTR lpProcName);
2818
2819 PALIMPORT
2820 BOOL
2821 PALAPI
2822 FreeLibrary(
2823     IN OUT HMODULE hLibModule);
2824
2825 PALIMPORT
2826 PAL_NORETURN
2827 VOID
2828 PALAPI
2829 FreeLibraryAndExitThread(
2830     IN HMODULE hLibModule,
2831     IN DWORD dwExitCode);
2832
2833 PALIMPORT
2834 BOOL
2835 PALAPI
2836 DisableThreadLibraryCalls(
2837     IN HMODULE hLibModule);
2838
2839 PALIMPORT
2840 DWORD
2841 PALAPI
2842 GetModuleFileNameW(
2843     IN HMODULE hModule,
2844     OUT LPWSTR lpFileName,
2845     IN DWORD nSize);
2846
2847 #ifdef UNICODE
2848 #define GetModuleFileName GetModuleFileNameW
2849 #else
2850 #define GetModuleFileName GetModuleFileNameA
2851 #endif
2852
2853 PALIMPORT
2854 DWORD
2855 PALAPI
2856 GetModuleFileNameExW(
2857     IN HANDLE hProcess,
2858     IN HMODULE hModule,
2859     OUT LPWSTR lpFilename,
2860     IN DWORD nSize
2861     );
2862
2863 #ifdef UNICODE
2864 #define GetModuleFileNameEx GetModuleFileNameExW
2865 #endif
2866
2867 // Get base address of the module containing a given symbol 
2868 PALAPI
2869 LPCVOID
2870 PAL_GetSymbolModuleBase(void *symbol);
2871
2872 PALIMPORT
2873 LPVOID
2874 PALAPI
2875 PAL_VirtualReserveFromExecutableMemoryAllocatorWithinRange(
2876     IN LPCVOID lpBeginAddress,
2877     IN LPCVOID lpEndAddress,
2878     IN SIZE_T dwSize);
2879
2880 PALIMPORT
2881 LPVOID
2882 PALAPI
2883 VirtualAlloc(
2884          IN LPVOID lpAddress,
2885          IN SIZE_T dwSize,
2886          IN DWORD flAllocationType,
2887          IN DWORD flProtect);
2888
2889 PALIMPORT
2890 BOOL
2891 PALAPI
2892 VirtualFree(
2893         IN LPVOID lpAddress,
2894         IN SIZE_T dwSize,
2895         IN DWORD dwFreeType);
2896
2897 PALIMPORT
2898 BOOL
2899 PALAPI
2900 VirtualProtect(
2901            IN LPVOID lpAddress,
2902            IN SIZE_T dwSize,
2903            IN DWORD flNewProtect,
2904            OUT PDWORD lpflOldProtect);
2905
2906 typedef struct _MEMORYSTATUSEX {
2907   DWORD     dwLength;
2908   DWORD     dwMemoryLoad;
2909   DWORDLONG ullTotalPhys;
2910   DWORDLONG ullAvailPhys;
2911   DWORDLONG ullTotalPageFile;
2912   DWORDLONG ullAvailPageFile;
2913   DWORDLONG ullTotalVirtual;
2914   DWORDLONG ullAvailVirtual;
2915   DWORDLONG ullAvailExtendedVirtual;
2916 } MEMORYSTATUSEX, *LPMEMORYSTATUSEX;
2917
2918 PALIMPORT 
2919 BOOL
2920 PALAPI
2921 GlobalMemoryStatusEx(
2922             IN OUT LPMEMORYSTATUSEX lpBuffer);
2923
2924 typedef struct _MEMORY_BASIC_INFORMATION {
2925     PVOID BaseAddress;
2926     PVOID AllocationBase_PAL_Undefined;
2927     DWORD AllocationProtect;
2928     SIZE_T RegionSize;
2929     DWORD State;
2930     DWORD Protect;
2931     DWORD Type;
2932 } MEMORY_BASIC_INFORMATION, *PMEMORY_BASIC_INFORMATION;
2933
2934 PALIMPORT
2935 SIZE_T
2936 PALAPI
2937 VirtualQuery(
2938          IN LPCVOID lpAddress,
2939          OUT PMEMORY_BASIC_INFORMATION lpBuffer,
2940          IN SIZE_T dwLength);
2941
2942 PALIMPORT
2943 VOID
2944 PALAPI
2945 RtlMoveMemory(
2946           IN PVOID Destination,
2947           IN CONST VOID *Source,
2948           IN SIZE_T Length);
2949
2950 PALIMPORT
2951 VOID
2952 PALAPI
2953 RtlZeroMemory(
2954     IN PVOID Destination,
2955     IN SIZE_T Length);
2956
2957 #define MoveMemory memmove
2958 #define CopyMemory memcpy
2959 #define FillMemory(Destination,Length,Fill) memset((Destination),(Fill),(Length))
2960 #define ZeroMemory(Destination,Length) memset((Destination),0,(Length))
2961
2962
2963 PALIMPORT
2964 HANDLE
2965 PALAPI
2966 GetProcessHeap(
2967            VOID);
2968
2969 #define HEAP_ZERO_MEMORY 0x00000008
2970
2971 PALIMPORT
2972 HANDLE
2973 PALAPI
2974 HeapCreate(
2975          IN DWORD flOptions,
2976          IN SIZE_T dwInitialSize,
2977          IN SIZE_T dwMaximumSize);
2978
2979 PALIMPORT
2980 LPVOID
2981 PALAPI
2982 HeapAlloc(
2983       IN HANDLE hHeap,
2984       IN DWORD dwFlags,
2985       IN SIZE_T dwBytes);
2986
2987 PALIMPORT
2988 LPVOID
2989 PALAPI
2990 HeapReAlloc(
2991     IN HANDLE hHeap,
2992     IN DWORD dwFlags,
2993     IN LPVOID lpMem,
2994     IN SIZE_T dwBytes
2995     );
2996
2997 PALIMPORT
2998 BOOL
2999 PALAPI
3000 HeapFree(
3001      IN HANDLE hHeap,
3002      IN DWORD dwFlags,
3003      IN LPVOID lpMem);
3004
3005 typedef enum _HEAP_INFORMATION_CLASS {
3006     HeapCompatibilityInformation,
3007     HeapEnableTerminationOnCorruption
3008 } HEAP_INFORMATION_CLASS;
3009
3010 PALIMPORT
3011 BOOL
3012 PALAPI
3013 HeapSetInformation(
3014         IN OPTIONAL HANDLE HeapHandle,
3015         IN HEAP_INFORMATION_CLASS HeapInformationClass,
3016         IN PVOID HeapInformation,
3017         IN SIZE_T HeapInformationLength);
3018
3019 #define LMEM_FIXED          0x0000
3020 #define LMEM_MOVEABLE       0x0002
3021 #define LMEM_ZEROINIT       0x0040
3022 #define LPTR                (LMEM_FIXED | LMEM_ZEROINIT)
3023
3024 PALIMPORT
3025 HLOCAL
3026 PALAPI
3027 LocalAlloc(
3028        IN UINT uFlags,
3029        IN SIZE_T uBytes);
3030
3031 PALIMPORT
3032 HLOCAL
3033 PALAPI
3034 LocalReAlloc(
3035        IN HLOCAL hMem,
3036        IN SIZE_T uBytes,
3037        IN UINT   uFlags);
3038
3039 PALIMPORT
3040 HLOCAL
3041 PALAPI
3042 LocalFree(
3043       IN HLOCAL hMem);
3044
3045 PALIMPORT
3046 BOOL
3047 PALAPI
3048 FlushInstructionCache(
3049               IN HANDLE hProcess,
3050               IN LPCVOID lpBaseAddress,
3051               IN SIZE_T dwSize);
3052
3053 #if ENABLE_DOWNLEVEL_FOR_NLS
3054
3055 PALIMPORT
3056 BOOL
3057 PALAPI
3058 GetStringTypeExW(
3059          IN LCID Locale,
3060          IN DWORD dwInfoType,
3061          IN LPCWSTR lpSrcStr,
3062          IN int cchSrc,
3063          OUT LPWORD lpCharType);
3064
3065 #ifdef UNICODE
3066 #define GetStringTypeEx GetStringTypeExW
3067 #endif
3068
3069 #endif // ENABLE_DOWNLEVEL_FOR_NLS
3070
3071
3072 #define NORM_IGNORECASE           0x00000001  // ignore case
3073 #define NORM_IGNOREWIDTH          0x00020000  // ignore width
3074
3075 #define NORM_LINGUISTIC_CASING    0x08000000  // use linguistic rules for casing
3076
3077 #ifdef __APPLE__
3078 #define NORM_IGNORENONSPACE       0x00000002  // ignore nonspacing chars
3079 #define NORM_IGNORESYMBOLS        0x00000004  // ignore symbols
3080 #define NORM_IGNOREKANATYPE       0x00010000  // ignore kanatype
3081 #define SORT_STRINGSORT           0x00001000  // use string sort method
3082 #endif // __APPLE__
3083
3084
3085 typedef struct nlsversioninfo { 
3086   DWORD     dwNLSVersionInfoSize; 
3087   DWORD     dwNLSVersion; 
3088   DWORD     dwDefinedVersion; 
3089 } NLSVERSIONINFO, *LPNLSVERSIONINFO; 
3090
3091 #define CSTR_LESS_THAN     1
3092 #define CSTR_EQUAL         2
3093 #define CSTR_GREATER_THAN  3
3094
3095 #if ENABLE_DOWNLEVEL_FOR_NLS
3096
3097
3098 PALIMPORT
3099 int
3100 PALAPI
3101 CompareStringW(
3102     IN LCID     Locale,
3103     IN DWORD    dwCmpFlags,
3104     IN LPCWSTR  lpString1,
3105     IN int      cchCount1,
3106     IN LPCWSTR  lpString2,
3107     IN int      cchCount2);
3108
3109 #endif // ENABLE_DOWNLEVEL_FOR_NLS
3110
3111
3112 PALIMPORT
3113 int
3114 PALAPI
3115 CompareStringEx(
3116     IN LPCWSTR lpLocaleName,
3117     IN DWORD    dwCmpFlags,
3118     IN LPCWSTR  lpString1,
3119     IN int      cchCount1,
3120     IN LPCWSTR  lpString2,
3121     IN int      cchCount2,
3122     IN LPNLSVERSIONINFO lpVersionInformation,
3123     IN LPVOID lpReserved,
3124     IN LPARAM lParam);
3125
3126
3127 #ifdef UNICODE
3128 #define CompareString  CompareStringW
3129 #endif
3130
3131 #define MAX_LEADBYTES         12
3132 #define MAX_DEFAULTCHAR       2
3133
3134 PALIMPORT
3135 UINT
3136 PALAPI
3137 GetACP(void);
3138
3139 typedef struct _cpinfo {
3140     UINT MaxCharSize;
3141     BYTE DefaultChar[MAX_DEFAULTCHAR];
3142     BYTE LeadByte[MAX_LEADBYTES];
3143 } CPINFO, *LPCPINFO;
3144
3145 PALIMPORT
3146 BOOL
3147 PALAPI
3148 GetCPInfo(
3149       IN UINT CodePage,
3150       OUT LPCPINFO lpCPInfo);
3151
3152 PALIMPORT
3153 BOOL
3154 PALAPI
3155 IsDBCSLeadByteEx(
3156          IN UINT CodePage,
3157          IN BYTE TestChar);
3158
3159 PALIMPORT
3160 BOOL
3161 PALAPI
3162 IsDBCSLeadByte(
3163         IN BYTE TestChar);
3164
3165 PALIMPORT
3166 BOOL
3167 PALAPI
3168 IsValidCodePage(
3169         IN UINT CodePage);
3170         
3171
3172 #define MB_PRECOMPOSED            0x00000001
3173 #define MB_ERR_INVALID_CHARS      0x00000008
3174
3175 PALIMPORT
3176 int
3177 PALAPI
3178 MultiByteToWideChar(
3179             IN UINT CodePage,
3180             IN DWORD dwFlags,
3181             IN LPCSTR lpMultiByteStr,
3182             IN int cbMultiByte,
3183             OUT LPWSTR lpWideCharStr,
3184             IN int cchWideChar);
3185
3186 #define WC_NO_BEST_FIT_CHARS      0x00000400
3187
3188 PALIMPORT
3189 int
3190 PALAPI
3191 WideCharToMultiByte(
3192             IN UINT CodePage,
3193             IN DWORD dwFlags,
3194             IN LPCWSTR lpWideCharStr,
3195             IN int cchWideChar,
3196             OUT LPSTR lpMultiByteStr,
3197             IN int cbMultyByte,
3198             IN LPCSTR lpDefaultChar,
3199             OUT LPBOOL lpUsedDefaultChar);
3200
3201 #if ENABLE_DOWNLEVEL_FOR_NLS
3202
3203 PALIMPORT
3204 LANGID
3205 PALAPI
3206 GetSystemDefaultLangID(
3207                void);
3208
3209 PALIMPORT
3210 LANGID
3211 PALAPI
3212 GetUserDefaultLangID(
3213              void);
3214
3215 PALIMPORT
3216 BOOL
3217 PALAPI
3218 SetThreadLocale(
3219         IN LCID Locale);
3220
3221 PALIMPORT
3222 LCID
3223 PALAPI
3224 GetThreadLocale(
3225         void);
3226
3227 #endif //ENABLE_DOWNLEVEL_FOR_NLS
3228
3229 //
3230 //  Locale Types.
3231 //
3232 //  These types are used for the GetLocaleInfo NLS API routine.
3233 //
3234
3235 #ifdef __APPLE__
3236
3237 //
3238 //  The following LCTypes may be used in combination with any other LCTypes.
3239 //
3240 //    LOCALE_NOUSEROVERRIDE is also used in GetTimeFormat and
3241 //    GetDateFormat.
3242 //
3243 //    LOCALE_RETURN_NUMBER will return the result from GetLocaleInfo as a
3244 //    number instead of a string.  This flag is only valid for the LCTypes
3245 //    beginning with LOCALE_I.
3246 //
3247 #define LOCALE_NOUSEROVERRIDE         0x80000000    /* do not use user overrides */
3248 #define LOCALE_RETURN_NUMBER          0x20000000    /* return number instead of string */
3249 #define LOCALE_RETURN_GENITIVE_NAMES  0x10000000   //Flag to return the Genitive forms of month names
3250
3251 #define LOCALE_SLOCALIZEDDISPLAYNAME  0x00000002   // localized name of locale, eg "German (Germany)" in UI language
3252 #define LOCALE_SENGLISHDISPLAYNAME    0x00000072   // Display name (language + country usually) in English, eg "German (Germany)"
3253 #define LOCALE_SNATIVEDISPLAYNAME     0x00000073   // Display name in native locale language, eg "Deutsch (Deutschland)
3254
3255 #define LOCALE_SLOCALIZEDLANGUAGENAME 0x0000006f   // Language Display Name for a language, eg "German" in UI language
3256 #define LOCALE_SENGLISHLANGUAGENAME   0x00001001   // English name of language, eg "German"
3257 #define LOCALE_SNATIVELANGUAGENAME    0x00000004   // native name of language, eg "Deutsch"
3258
3259 #define LOCALE_SLOCALIZEDCOUNTRYNAME  0x00000006   // localized name of country, eg "Germany" in UI language
3260 #define LOCALE_SENGLISHCOUNTRYNAME    0x00001002   // English name of country, eg "Germany"
3261 #define LOCALE_SNATIVECOUNTRYNAME     0x00000008   // native name of country, eg "Deutschland"
3262
3263 //
3264 //  The following LCTypes are mutually exclusive in that they may NOT
3265 //  be used in combination with each other.
3266 //
3267 #define LOCALE_ILANGUAGE              0x00000001    /* language id */
3268 #define LOCALE_SLANGUAGE              0x00000002    /* localized name of language */
3269 #define LOCALE_SENGLANGUAGE           0x00001001    /* English name of language */
3270 #define LOCALE_SABBREVLANGNAME        0x00000003    /* abbreviated language name */
3271 #define LOCALE_SNATIVELANGNAME        0x00000004    /* native name of language */
3272 #define LOCALE_ICOUNTRY               0x00000005    /* country code */
3273 #define LOCALE_SCOUNTRY               0x00000006    /* localized name of country */
3274
3275 #define LOCALE_SENGCOUNTRY            0x00001002    /* English name of country */
3276 #define LOCALE_SABBREVCTRYNAME        0x00000007    /* abbreviated country name */
3277 #define LOCALE_SNATIVECTRYNAME        0x00000008    /* native name of country */
3278
3279 #define LOCALE_SLIST                  0x0000000C    /* list item separator */
3280 #define LOCALE_IMEASURE               0x0000000D    /* 0 = metric, 1 = US */
3281
3282 #define LOCALE_SDECIMAL               0x0000000E    /* decimal separator */
3283 #define LOCALE_STHOUSAND              0x0000000F    /* thousand separator */
3284 #define LOCALE_SGROUPING              0x00000010    /* digit grouping */
3285 #define LOCALE_IDIGITS                0x00000011    /* number of fractional digits */
3286 #define LOCALE_ILZERO                 0x00000012    /* leading zeros for decimal */
3287 #define LOCALE_INEGNUMBER             0x00001010    /* negative number mode */
3288 #define LOCALE_SNATIVEDIGITS          0x00000013    /* native ascii 0-9 */
3289
3290 #define LOCALE_SCURRENCY              0x00000014    /* local monetary symbol */
3291 #define LOCALE_SINTLSYMBOL            0x00000015    /* intl monetary symbol */
3292 #define LOCALE_SMONDECIMALSEP         0x00000016    /* monetary decimal separator */
3293 #define LOCALE_SMONTHOUSANDSEP        0x00000017    /* monetary thousand separator */
3294 #define LOCALE_SMONGROUPING           0x00000018    /* monetary grouping */
3295 #define LOCALE_ICURRDIGITS            0x00000019    /* # local monetary digits */
3296 #define LOCALE_IINTLCURRDIGITS        0x0000001A    /* # intl monetary digits */
3297 #define LOCALE_ICURRENCY              0x0000001B    /* positive currency mode */
3298 #define LOCALE_INEGCURR               0x0000001C    /* negative currency mode */
3299
3300 #define LOCALE_SSHORTDATE             0x0000001F    /* short date format string */
3301 #define LOCALE_SLONGDATE              0x00000020    /* long date format string */
3302 #define LOCALE_STIMEFORMAT            0x00001003    /* time format string */
3303 #define LOCALE_S1159                  0x00000028    /* AM designator */
3304 #define LOCALE_S2359                  0x00000029    /* PM designator */
3305
3306 #define LOCALE_ICALENDARTYPE          0x00001009    /* type of calendar specifier */
3307 #define LOCALE_IFIRSTDAYOFWEEK        0x0000100C    /* first day of week specifier */
3308 #define LOCALE_IFIRSTWEEKOFYEAR       0x0000100D    /* first week of year specifier */
3309
3310 #define LOCALE_SDAYNAME1              0x0000002A    /* long name for Monday */
3311 #define LOCALE_SDAYNAME2              0x0000002B    /* long name for Tuesday */
3312 #define LOCALE_SDAYNAME3              0x0000002C    /* long name for Wednesday */
3313 #define LOCALE_SDAYNAME4              0x0000002D    /* long name for Thursday */
3314 #define LOCALE_SDAYNAME5              0x0000002E    /* long name for Friday */
3315 #define LOCALE_SDAYNAME6              0x0000002F    /* long name for Saturday */
3316 #define LOCALE_SDAYNAME7              0x00000030    /* long name for Sunday */
3317 #define LOCALE_SABBREVDAYNAME1        0x00000031    /* abbreviated name for Monday */
3318 #define LOCALE_SABBREVDAYNAME2        0x00000032    /* abbreviated name for Tuesday */
3319 #define LOCALE_SABBREVDAYNAME3        0x00000033    /* abbreviated name for Wednesday */
3320 #define LOCALE_SABBREVDAYNAME4        0x00000034    /* abbreviated name for Thursday */
3321 #define LOCALE_SABBREVDAYNAME5        0x00000035    /* abbreviated name for Friday */
3322 #define LOCALE_SABBREVDAYNAME6        0x00000036    /* abbreviated name for Saturday */
3323 #define LOCALE_SABBREVDAYNAME7        0x00000037    /* abbreviated name for Sunday */
3324 #define LOCALE_SMONTHNAME1            0x00000038    /* long name for January */
3325 #define LOCALE_SMONTHNAME2            0x00000039    /* long name for February */
3326 #define LOCALE_SMONTHNAME3            0x0000003A    /* long name for March */
3327 #define LOCALE_SMONTHNAME4            0x0000003B    /* long name for April */
3328 #define LOCALE_SMONTHNAME5            0x0000003C    /* long name for May */
3329 #define LOCALE_SMONTHNAME6            0x0000003D    /* long name for June */
3330 #define LOCALE_SMONTHNAME7            0x0000003E    /* long name for July */
3331 #define LOCALE_SMONTHNAME8            0x0000003F    /* long name for August */
3332 #define LOCALE_SMONTHNAME9            0x00000040    /* long name for September */
3333 #define LOCALE_SMONTHNAME10           0x00000041    /* long name for October */
3334 #define LOCALE_SMONTHNAME11           0x00000042    /* long name for November */
3335 #define LOCALE_SMONTHNAME12           0x00000043    /* long name for December */
3336 #define LOCALE_SMONTHNAME13           0x0000100E    /* long name for 13th month (if exists) */
3337 #define LOCALE_SABBREVMONTHNAME1      0x00000044    /* abbreviated name for January */
3338 #define LOCALE_SABBREVMONTHNAME2      0x00000045    /* abbreviated name for February */
3339 #define LOCALE_SABBREVMONTHNAME3      0x00000046    /* abbreviated name for March */
3340 #define LOCALE_SABBREVMONTHNAME4      0x00000047    /* abbreviated name for April */
3341 #define LOCALE_SABBREVMONTHNAME5      0x00000048    /* abbreviated name for May */
3342 #define LOCALE_SABBREVMONTHNAME6      0x00000049    /* abbreviated name for June */
3343 #define LOCALE_SABBREVMONTHNAME7      0x0000004A    /* abbreviated name for July */
3344 #define LOCALE_SABBREVMONTHNAME8      0x0000004B    /* abbreviated name for August */
3345 #define LOCALE_SABBREVMONTHNAME9      0x0000004C    /* abbreviated name for September */
3346 #define LOCALE_SABBREVMONTHNAME10     0x0000004D    /* abbreviated name for October */
3347 #define LOCALE_SABBREVMONTHNAME11     0x0000004E    /* abbreviated name for November */
3348 #define LOCALE_SABBREVMONTHNAME12     0x0000004F    /* abbreviated name for December */
3349 #define LOCALE_SABBREVMONTHNAME13     0x0000100F    /* abbreviated name for 13th month (if exists) */
3350
3351 #define LOCALE_SPOSITIVESIGN          0x00000050    /* positive sign */
3352 #define LOCALE_SNEGATIVESIGN          0x00000051    /* negative sign */
3353
3354 #define LOCALE_FONTSIGNATURE          0x00000058    /* font signature */
3355 #define LOCALE_SISO639LANGNAME        0x00000059    /* ISO abbreviated language name */
3356 #define LOCALE_SISO3166CTRYNAME       0x0000005A    /* ISO abbreviated country name */
3357
3358 #define LOCALE_SENGCURRNAME           0x00001007    /* english name of currency */
3359 #define LOCALE_SNATIVECURRNAME        0x00001008    /* native name of currency */
3360 #define LOCALE_SYEARMONTH             0x00001006    /* year month format string */
3361 #define LOCALE_IDIGITSUBSTITUTION     0x00001014    /* 0 = context, 1 = none, 2 = national */
3362
3363 #define LOCALE_SNAME                  0x0000005C    /* locale name <language>[-<Script>][-<REGION>[_<sort order>]] */
3364 #define LOCALE_SDURATION              0x0000005d    /* time duration format */
3365 #define LOCALE_SKEYBOARDSTOINSTALL    0x0000005e    /* (windows only) keyboards to install */
3366 #define LOCALE_SSHORTESTDAYNAME1      0x00000060    /* Shortest day name for Monday */
3367 #define LOCALE_SSHORTESTDAYNAME2      0x00000061    /* Shortest day name for Tuesday */
3368 #define LOCALE_SSHORTESTDAYNAME3      0x00000062    /* Shortest day name for Wednesday */
3369 #define LOCALE_SSHORTESTDAYNAME4      0x00000063    /* Shortest day name for Thursday */
3370 #define LOCALE_SSHORTESTDAYNAME5      0x00000064    /* Shortest day name for Friday */
3371 #define LOCALE_SSHORTESTDAYNAME6      0x00000065    /* Shortest day name for Saturday */
3372 #define LOCALE_SSHORTESTDAYNAME7      0x00000066    /* Shortest day name for Sunday */
3373 #define LOCALE_SISO639LANGNAME2       0x00000067    /* 3 character ISO abbreviated language name */
3374 #define LOCALE_SISO3166CTRYNAME2      0x00000068    /* 3 character ISO country name */
3375 #define LOCALE_SNAN                   0x00000069    /* Not a Number */
3376 #define LOCALE_SPOSINFINITY           0x0000006a    /* + Infinity */
3377 #define LOCALE_SNEGINFINITY           0x0000006b    /* - Infinity */
3378 #define LOCALE_SSCRIPTS               0x0000006c    /* Typical scripts in the locale */
3379 #define LOCALE_SPARENT                0x0000006d    /* Fallback name for resources */
3380 #define LOCALE_SCONSOLEFALLBACKNAME   0x0000006e    /* Fallback name for within the console */
3381 #define LOCALE_SLANGDISPLAYNAME       0x0000006f    /* Language Display Name for a language */ 
3382 #define LOCALE_IREADINGLAYOUT         0x00000070   // Returns one of the following 4 reading layout values:
3383                                                    // 0 - Left to right (eg en-US)
3384                                                    // 1 - Right to left (eg arabic locales)
3385                                                    // 2 - Vertical top to bottom with columns to the left and also left to right (ja-JP locales)
3386                                                    // 3 - Vertical top to bottom with columns proceeding to the right
3387 #define LOCALE_INEUTRAL               0x00000071   // Returns 0 for specific cultures, 1 for neutral cultures.
3388 #define LOCALE_INEGATIVEPERCENT       0x00000074   // Returns 0-11 for the negative percent format
3389 #define LOCALE_IPOSITIVEPERCENT       0x00000075   // Returns 0-3 for the positive percent formatIPOSITIVEPERCENT
3390 #define LOCALE_SPERCENT               0x00000076   // Returns the percent symbol
3391 #define LOCALE_SPERMILLE              0x00000077   // Returns the permille (U+2030) symbol
3392 #define LOCALE_SMONTHDAY              0x00000078   // Returns the preferred month/day format
3393 #define LOCALE_SSHORTTIME             0x00000079   // Returns the preferred short time format (ie: no seconds, just h:mm)
3394 #define LOCALE_SOPENTYPELANGUAGETAG   0x0000007a   // Open type language tag, eg: "latn" or "dflt"
3395 #define LOCALE_SSORTLOCALE            0x0000007b   // Name of locale to use for sorting/collation/casing behavior.
3396
3397 #define LCMAP_LINGUISTIC_CASING       0x01000000    /* Use linguistic casing */
3398
3399 #define CAL_RETURN_GENITIVE_NAMES       LOCALE_RETURN_GENITIVE_NAMES  // return genitive forms of month names
3400
3401 #define CAL_SSHORTESTDAYNAME1         0x00000031
3402 #define CAL_SSHORTESTDAYNAME2         0x00000032
3403 #define CAL_SSHORTESTDAYNAME3         0x00000033
3404 #define CAL_SSHORTESTDAYNAME4         0x00000034
3405 #define CAL_SSHORTESTDAYNAME5         0x00000035
3406 #define CAL_SSHORTESTDAYNAME6         0x00000036
3407 #define CAL_SSHORTESTDAYNAME7         0x00000037
3408
3409 #define CAL_SMONTHDAY                   0x00000038  // Month/day pattern (reserve for potential inclusion in a future version)
3410 #define CAL_SERASTRING                  0x00000004  // era name for IYearOffsetRanges, eg A.D.
3411 #define CAL_SABBREVERASTRING            0x00000039  // Abbreviated era string (eg: AD)
3412
3413 #define CAL_SSHORTDATE            0x00000005  /* short date format string */
3414 #define CAL_SLONGDATE             0x00000006  /* long date format string */
3415 #define CAL_SDAYNAME1             0x00000007  /* native name for Monday */
3416 #define CAL_SDAYNAME2             0x00000008  /* native name for Tuesday */
3417 #define CAL_SDAYNAME3             0x00000009  /* native name for Wednesday */
3418 #define CAL_SDAYNAME4             0x0000000a  /* native name for Thursday */
3419 #define CAL_SDAYNAME5             0x0000000b  /* native name for Friday */
3420 #define CAL_SDAYNAME6             0x0000000c  /* native name for Saturday */
3421 #define CAL_SDAYNAME7             0x0000000d  /* native name for Sunday */
3422 #define CAL_SABBREVDAYNAME1       0x0000000e  /* abbreviated name for Monday */
3423 #define CAL_SABBREVDAYNAME2       0x0000000f  /* abbreviated name for Tuesday */
3424 #define CAL_SABBREVDAYNAME3       0x00000010  /* abbreviated name for Wednesday */
3425 #define CAL_SABBREVDAYNAME4       0x00000011  /* abbreviated name for Thursday */
3426 #define CAL_SABBREVDAYNAME5       0x00000012  /* abbreviated name for Friday */
3427 #define CAL_SABBREVDAYNAME6       0x00000013  /* abbreviated name for Saturday */
3428 #define CAL_SABBREVDAYNAME7       0x00000014  /* abbreviated name for Sunday */
3429 #define CAL_SMONTHNAME1           0x00000015  /* native name for January */
3430 #define CAL_SMONTHNAME2           0x00000016  /* native name for February */
3431 #define CAL_SMONTHNAME3           0x00000017  /* native name for March */
3432 #define CAL_SMONTHNAME4           0x00000018  /* native name for April */
3433 #define CAL_SMONTHNAME5           0x00000019  /* native name for May */
3434 #define CAL_SMONTHNAME6           0x0000001a  /* native name for June */
3435 #define CAL_SMONTHNAME7           0x0000001b  /* native name for July */
3436 #define CAL_SMONTHNAME8           0x0000001c  /* native name for August */
3437 #define CAL_SMONTHNAME9           0x0000001d  /* native name for September */
3438 #define CAL_SMONTHNAME10          0x0000001e  /* native name for October */
3439 #define CAL_SMONTHNAME11          0x0000001f  /* native name for November */
3440 #define CAL_SMONTHNAME12          0x00000020  /* native name for December */
3441 #define CAL_SMONTHNAME13          0x00000021  /* native name for 13th month (if any) */
3442 #define CAL_SABBREVMONTHNAME1     0x00000022  /* abbreviated name for January */
3443 #define CAL_SABBREVMONTHNAME2     0x00000023  /* abbreviated name for February */
3444 #define CAL_SABBREVMONTHNAME3     0x00000024  /* abbreviated name for March */
3445 #define CAL_SABBREVMONTHNAME4     0x00000025  /* abbreviated name for April */
3446 #define CAL_SABBREVMONTHNAME5     0x00000026  /* abbreviated name for May */
3447 #define CAL_SABBREVMONTHNAME6     0x00000027  /* abbreviated name for June */
3448 #define CAL_SABBREVMONTHNAME7     0x00000028  /* abbreviated name for July */
3449 #define CAL_SABBREVMONTHNAME8     0x00000029  /* abbreviated name for August */
3450 #define CAL_SABBREVMONTHNAME9     0x0000002a  /* abbreviated name for September */
3451 #define CAL_SABBREVMONTHNAME10    0x0000002b  /* abbreviated name for October */
3452 #define CAL_SABBREVMONTHNAME11    0x0000002c  /* abbreviated name for November */
3453 #define CAL_SABBREVMONTHNAME12    0x0000002d  /* abbreviated name for December */
3454 #define CAL_SABBREVMONTHNAME13    0x0000002e  /* abbreviated name for 13th month (if any) */
3455 #define CAL_SYEARMONTH            0x0000002f  /* year month format string */
3456
3457
3458 #else // __APPLE__
3459
3460 #define LOCALE_SDECIMAL               0x0000000E    /* decimal separator */
3461 #define LOCALE_STHOUSAND              0x0000000F    /* thousand separator */
3462 #define LOCALE_ILZERO                 0x00000012    /* leading zeros for decimal */
3463 #define LOCALE_SCURRENCY              0x00000014    /* local monetary symbol */
3464 #define LOCALE_SMONDECIMALSEP         0x00000016    /* monetary decimal separator */
3465 #define LOCALE_SMONTHOUSANDSEP        0x00000017    /* monetary thousand separator */
3466
3467 #endif // __APPLE__
3468
3469
3470 #if ENABLE_DOWNLEVEL_FOR_NLS
3471
3472 PALIMPORT
3473 int
3474 PALAPI
3475 GetLocaleInfoW(
3476     IN LCID     Locale,
3477     IN LCTYPE   LCType,
3478     OUT LPWSTR  lpLCData,
3479     IN int      cchData);
3480
3481 #endif // ENABLE_DOWNLEVEL_FOR_NLS
3482
3483 PALIMPORT
3484 int
3485 PALAPI
3486 GetLocaleInfoEx(
3487     IN LPCWSTR  lpLocaleName,
3488     IN LCTYPE   LCType,
3489     OUT LPWSTR  lpLCData,
3490     IN int      cchData);
3491
3492
3493 PALIMPORT
3494 int 
3495 PALAPI
3496 CompareStringOrdinal(
3497     IN LPCWSTR lpString1, 
3498   IN int cchCount1, 
3499   IN LPCWSTR lpString2, 
3500   IN int cchCount2, 
3501   IN BOOL bIgnoreCase);
3502
3503 typedef struct _nlsversioninfoex { 
3504   DWORD  dwNLSVersionInfoSize; 
3505   DWORD  dwNLSVersion; 
3506   DWORD  dwDefinedVersion; 
3507   DWORD  dwEffectiveId;   
3508   GUID  guidCustomVersion; 
3509   } NLSVERSIONINFOEX, *LPNLSVERSIONINFOEX; 
3510
3511 PALIMPORT
3512 int 
3513 PALAPI
3514 FindNLSStringEx(
3515     IN LPCWSTR lpLocaleName, 
3516   IN DWORD dwFindNLSStringFlags, 
3517   IN LPCWSTR lpStringSource, 
3518   IN int cchSource, 
3519     IN LPCWSTR lpStringValue, 
3520   IN int cchValue, 
3521   OUT LPINT pcchFound, 
3522   IN LPNLSVERSIONINFOEX lpVersionInformation, 
3523   IN LPVOID lpReserved, 
3524   IN LPARAM lParam );
3525
3526 typedef enum {
3527     COMPARE_STRING = 0x0001,
3528 } NLS_FUNCTION;
3529
3530 PALIMPORT
3531 BOOL 
3532 PALAPI
3533 IsNLSDefinedString(
3534     IN NLS_FUNCTION Function, 
3535   IN DWORD dwFlags, 
3536   IN LPNLSVERSIONINFOEX lpVersionInfo, 
3537   IN LPCWSTR lpString, 
3538   IN int cchStr );
3539
3540
3541 PALIMPORT
3542 int
3543 PALAPI
3544 ResolveLocaleName(
3545     IN LPCWSTR lpNameToResolve,
3546         OUT LPWSTR lpLocaleName,
3547         IN int cchLocaleName );
3548
3549 PALIMPORT
3550 BOOL 
3551 PALAPI
3552 GetThreadPreferredUILanguages(
3553     IN DWORD  dwFlags,
3554     OUT PULONG  pulNumLanguages,
3555     OUT PWSTR  pwszLanguagesBuffer,
3556     IN OUT PULONG  pcchLanguagesBuffer);
3557
3558
3559 PALIMPORT
3560 int 
3561 PALAPI
3562 GetSystemDefaultLocaleName(
3563     OUT LPWSTR lpLocaleName, 
3564   IN int cchLocaleName);
3565
3566 #ifdef UNICODE
3567 #define GetLocaleInfo GetLocaleInfoW
3568 #endif
3569
3570 #if ENABLE_DOWNLEVEL_FOR_NLS
3571 PALIMPORT
3572 LCID
3573 PALAPI
3574 GetUserDefaultLCID(
3575            void);
3576 #endif
3577
3578
3579 PALIMPORT
3580 int
3581 PALAPI
3582 GetUserDefaultLocaleName(
3583            OUT LPWSTR lpLocaleName,
3584            IN int cchLocaleName);
3585
3586
3587 #define LCID_INSTALLED            0x00000001  // installed locale ids
3588 #define LCID_SUPPORTED            0x00000002  // supported locale ids
3589 #ifdef __APPLE__
3590 #define LCID_ALTERNATE_SORTS      0x00000004  // alternate sort locale ids
3591 #endif // __APPLE__
3592
3593 #if ENABLE_DOWNLEVEL_FOR_NLS
3594 PALIMPORT
3595 BOOL
3596 PALAPI
3597 IsValidLocale(
3598           IN LCID Locale,
3599           IN DWORD dwFlags);
3600 #endif // ENABLE_DOWNLEVEL_FOR_NLS
3601
3602
3603 typedef DWORD CALID;
3604 typedef DWORD CALTYPE;
3605
3606 #define CAL_ITWODIGITYEARMAX 0x00000030 // two digit year max
3607 #define CAL_RETURN_NUMBER    0x20000000 // return number instead of string
3608
3609 #define CAL_GREGORIAN                 1 // Gregorian (localized) calendar
3610 #define CAL_GREGORIAN_US              2 // Gregorian (U.S.) calendar
3611 #define CAL_JAPAN                     3 // Japanese Emperor Era calendar
3612 #define CAL_TAIWAN                    4 // Taiwan Era calendar
3613 #define CAL_KOREA                     5 // Korean Tangun Era calendar
3614 #define CAL_HIJRI                     6 // Hijri (Arabic Lunar) calendar
3615 #define CAL_THAI                      7 // Thai calendar
3616 #define CAL_HEBREW                    8 // Hebrew (Lunar) calendar
3617 #define CAL_GREGORIAN_ME_FRENCH       9 // Gregorian Middle East French calendar
3618 #define CAL_GREGORIAN_ARABIC         10 // Gregorian Arabic calendar
3619 #define CAL_GREGORIAN_XLIT_ENGLISH   11 // Gregorian Transliterated English calendar
3620 #define CAL_GREGORIAN_XLIT_FRENCH    12 // Gregorian Transliterated French calendar
3621 #define CAL_JULIAN                   13
3622
3623 #if ENABLE_DOWNLEVEL_FOR_NLS
3624 PALIMPORT
3625 int
3626 PALAPI
3627 GetCalendarInfoW(
3628          IN LCID Locale,
3629          IN CALID Calendar,
3630          IN CALTYPE CalType,
3631          OUT LPWSTR lpCalData,
3632          IN int cchData,
3633          OUT LPDWORD lpValue);
3634
3635 #ifdef UNICODE
3636 #define GetCalendarInfo GetCalendarInfoW
3637 #endif
3638
3639 #endif // ENABLE_DOWNLEVEL_FOR_NLS
3640
3641
3642 PALIMPORT
3643 int
3644 PALAPI
3645 GetCalendarInfoEx(
3646          IN LPCWSTR lpLocaleName,
3647          IN CALID Calendar,
3648          IN LPCWSTR lpReserved,
3649          IN CALTYPE CalType,
3650          OUT LPWSTR lpCalData,
3651          IN int cchData,
3652          OUT LPDWORD lpValue);
3653
3654 #if ENABLE_DOWNLEVEL_FOR_NLS
3655 typedef BOOL (CALLBACK* LOCALE_ENUMPROCW)(LPWSTR);
3656
3657 PALIMPORT
3658 BOOL
3659 PALAPI
3660 EnumSystemLocalesW(
3661     IN LOCALE_ENUMPROCW lpLocaleEnumProc,
3662     IN DWORD            dwFlags);
3663 #endif //  ENABLE_DOWNLEVEL_FOR_NLS
3664
3665 #define DATE_SHORTDATE            0x00000001  // use short date picture
3666 #define DATE_LONGDATE             0x00000002  // use long date picture
3667 #define DATE_YEARMONTH            0x00000008  // use year month picture
3668
3669 typedef BOOL (CALLBACK* DATEFMT_ENUMPROCEXW)(LPWSTR, CALID);
3670
3671 #if ENABLE_DOWNLEVEL_FOR_NLS
3672
3673 PALIMPORT
3674 BOOL
3675 PALAPI
3676 EnumDateFormatsExW(
3677     IN DATEFMT_ENUMPROCEXW lpDateFmtEnumProcEx,
3678     IN LCID                Locale,
3679     IN DWORD               dwFlags);
3680
3681 #else // ENABLE_DOWNLEVEL_FOR_NLS
3682
3683 typedef BOOL (CALLBACK* DATEFMT_ENUMPROCEXEXW)(LPWSTR, CALID, LPARAM);
3684
3685 PALIMPORT
3686 BOOL
3687 PALAPI
3688 EnumDateFormatsExEx(
3689     IN DATEFMT_ENUMPROCEXEXW lpDateFmtEnumProcEx,
3690     IN LPCWSTR          lpLocaleName,
3691     IN DWORD               dwFlags,
3692     IN LPARAM      lParam);
3693
3694 #endif // ENABLE_DOWNLEVEL_FOR_NLS
3695
3696 typedef BOOL (CALLBACK* TIMEFMT_ENUMPROCW)(LPWSTR);
3697
3698 #if ENABLE_DOWNLEVEL_FOR_NLS
3699
3700 PALIMPORT
3701 BOOL
3702 PALAPI
3703 EnumTimeFormatsW(
3704     IN TIMEFMT_ENUMPROCW lpTimeFmtEnumProc,
3705     IN LCID              Locale,
3706     IN DWORD             dwFlags);
3707
3708 #else // ENABLE_DOWNLEVEL_FOR_NLS
3709
3710 typedef BOOL (CALLBACK* TIMEFMT_ENUMPROCEXW)(LPWSTR, LPARAM);
3711
3712 PALIMPORT
3713 BOOL
3714 PALAPI
3715 EnumTimeFormatsEx(
3716     IN TIMEFMT_ENUMPROCEXW lpTimeFmtEnumProc,
3717     IN LPCWSTR          lpLocaleName,
3718     IN DWORD             dwFlags,
3719     IN LPARAM    lParam);
3720
3721 #endif // ENABLE_DOWNLEVEL_FOR_NLS
3722
3723 #define ENUM_ALL_CALENDARS        0xffffffff  // enumerate all calendars
3724 #define CAL_ICALINTVALUE          0x00000001  // calendar type
3725 #define CAL_NOUSEROVERRIDE        LOCALE_NOUSEROVERRIDE  // do not use user overrides
3726 #define CAL_SCALNAME              0x00000002  // native name of calendar
3727
3728 typedef BOOL (CALLBACK* CALINFO_ENUMPROCEXW)(LPWSTR,CALID);
3729
3730 #if ENABLE_DOWNLEVEL_FOR_NLS
3731
3732 PALIMPORT
3733 BOOL
3734 PALAPI
3735 EnumCalendarInfoExW(
3736     IN CALINFO_ENUMPROCEXW lpCalInfoEnumProc,
3737     IN LCID              Locale,
3738     IN CALID             Calendar,
3739     IN CALTYPE           CalType);
3740
3741 #else // ENABLE_DOWNLEVEL_FOR_NLS
3742
3743 typedef BOOL (CALLBACK* CALINFO_ENUMPROCEXEXW)(LPWSTR, CALID, LPWSTR, LPARAM);
3744
3745 PALIMPORT
3746 BOOL
3747 PALAPI
3748 EnumCalendarInfoExEx(
3749     IN CALINFO_ENUMPROCEXEXW lpCalInfoEnumProc,
3750     IN LPCWSTR          lpLocaleName,
3751     IN CALID             Calendar,
3752     IN LPCWSTR           lpReserved,
3753     IN CALTYPE           CalType,
3754     IN LPARAM        lParam);
3755
3756 #endif // ENABLE_DOWNLEVEL_FOR_NLS
3757
3758 #define LCMAP_LOWERCASE  0x00000100
3759 #define LCMAP_UPPERCASE  0x00000200
3760
3761 #if ENABLE_DOWNLEVEL_FOR_NLS
3762
3763 PALIMPORT
3764 int
3765 PALAPI
3766 LCMapStringW(
3767     IN LCID    Locale,
3768     IN DWORD   dwMapFlags,
3769     IN LPCWSTR lpSrcStr,
3770     IN int     cchSrc,
3771     OUT LPWSTR lpDestStr,
3772     IN int     cchDest);
3773
3774 #ifdef UNICODE
3775 #define LCMapString LCMapStringW
3776 #endif
3777
3778
3779 #endif // ENABLE_DOWNLEVEL_FOR_NLS
3780
3781
3782 PALIMPORT
3783 int
3784 PALAPI
3785 LCMapStringEx(
3786     IN LPCWSTR    lpLocaleName,
3787     IN DWORD   dwMapFlags,
3788     IN LPCWSTR lpSrcStr,
3789     IN int     cchSrc,
3790     OUT LPWSTR lpDestStr,
3791     IN int     cchDest,
3792     IN LPNLSVERSIONINFO lpVersionInformation, 
3793     IN LPVOID lpReserved, 
3794     IN LPARAM lParam );
3795
3796 PALIMPORT
3797 int
3798 PALAPI
3799 PAL_LCMapCharW(
3800     IN LPCWSTR    lpLocaleName,
3801     IN DWORD   dwMapFlags,
3802     IN WCHAR   srcChar,
3803     OUT WCHAR  *destChar,
3804     LPNLSVERSIONINFO lpVersionInformation,
3805     LPVOID lpReserved,
3806     LPARAM lParam );
3807
3808 PALIMPORT
3809 int
3810 PALAPI
3811 PAL_NormalizeStringExW(
3812     IN LPCWSTR    lpLocaleName,
3813     IN DWORD   dwMapFlags,
3814     IN LPCWSTR lpSrcStr,
3815     IN int     cchSrc,
3816     OUT LPWSTR lpDestStr,
3817     IN int     cchDest);
3818
3819 PALIMPORT
3820 int
3821 PALAPI
3822 PAL_ParseDateW(
3823     IN LPCWSTR   lpLocaleName,
3824     IN LPCWSTR   lpFormat,
3825     IN LPCWSTR   lpString,
3826     OUT LPSYSTEMTIME lpTime);
3827
3828 PALIMPORT
3829 int
3830 PALAPI
3831 PAL_GetCalendar(
3832     IN LPCWSTR   lpLocaleName,
3833     OUT CALID*   pCalendar);
3834
3835 #define GEOID_NOT_AVAILABLE -1
3836
3837 #define DATE_USE_ALT_CALENDAR 0x00000004
3838
3839 #if ENABLE_DOWNLEVEL_FOR_NLS
3840
3841 PALIMPORT
3842 int
3843 PALAPI
3844 GetDateFormatW(
3845            IN LCID Locale,
3846            IN DWORD dwFlags,
3847            IN CONST SYSTEMTIME *lpDate,
3848            IN LPCWSTR lpFormat,
3849            OUT LPWSTR lpDateStr,
3850            IN int cchDate);
3851
3852 #else
3853
3854 PALIMPORT
3855 int
3856 PALAPI
3857 GetDateFormatEx(
3858            IN LPCWSTR Locale,
3859            IN DWORD dwFlags,
3860            IN CONST SYSTEMTIME *lpDate,
3861            IN LPCWSTR lpFormat,
3862            OUT LPWSTR lpDateStr,
3863            IN int cchDate,
3864            IN LPCWSTR lpCalendar);
3865
3866
3867 #endif // ENABLE_DOWNLEVEL_FOR_NLS
3868
3869 PALIMPORT
3870 int
3871 PALAPI
3872 GetDateFormatEx(
3873            IN LPCWSTR lpLocaleName,
3874            IN DWORD dwFlags,
3875            IN CONST SYSTEMTIME *lpDate,
3876            IN LPCWSTR lpFormat,
3877            OUT LPWSTR lpDateStr,
3878            IN int cchDate,
3879            LPCWSTR lpCalendar);
3880
3881
3882 #ifdef UNICODE
3883 #define GetDateFormat GetDateFormatW
3884 #endif
3885
3886
3887 PALIMPORT
3888 int
3889 PALAPI
3890 PAL_GetResourceString(
3891         IN LPCSTR lpDomain,
3892         IN LPCSTR lpResourceStr,
3893         OUT LPWSTR lpWideCharStr,
3894         IN int cchWideChar);
3895
3896 PALIMPORT
3897 BOOL
3898 PALAPI
3899 PAL_BindResources(IN LPCSTR lpDomain);
3900
3901 #define EXCEPTION_NONCONTINUABLE 0x1
3902 #define EXCEPTION_UNWINDING 0x2
3903
3904 #ifdef FEATURE_PAL_SXS
3905
3906 #define EXCEPTION_EXIT_UNWIND 0x4       // Exit unwind is in progress (not used by PAL SEH)
3907 #define EXCEPTION_NESTED_CALL 0x10      // Nested exception handler call
3908 #define EXCEPTION_TARGET_UNWIND 0x20    // Target unwind in progress
3909 #define EXCEPTION_COLLIDED_UNWIND 0x40  // Collided exception handler call
3910 #define EXCEPTION_SKIP_VEH 0x200
3911
3912 #define EXCEPTION_UNWIND (EXCEPTION_UNWINDING | EXCEPTION_EXIT_UNWIND | \
3913                           EXCEPTION_TARGET_UNWIND | EXCEPTION_COLLIDED_UNWIND)
3914
3915 #define IS_DISPATCHING(Flag) ((Flag & EXCEPTION_UNWIND) == 0)
3916 #define IS_UNWINDING(Flag) ((Flag & EXCEPTION_UNWIND) != 0)
3917 #define IS_TARGET_UNWIND(Flag) (Flag & EXCEPTION_TARGET_UNWIND)
3918
3919 #endif // FEATURE_PAL_SXS
3920
3921 #define EXCEPTION_IS_SIGNAL 0x100
3922
3923 #define EXCEPTION_MAXIMUM_PARAMETERS 15
3924
3925 // Index in the ExceptionInformation array where we will keep the reference
3926 // to the native exception that needs to be deleted when dispatching
3927 // exception in managed code.
3928 #define NATIVE_EXCEPTION_ASYNC_SLOT (EXCEPTION_MAXIMUM_PARAMETERS-1)
3929
3930 typedef struct _EXCEPTION_RECORD {
3931     DWORD ExceptionCode;
3932     DWORD ExceptionFlags;
3933     struct _EXCEPTION_RECORD *ExceptionRecord;
3934     PVOID ExceptionAddress;
3935     DWORD NumberParameters;
3936     ULONG_PTR ExceptionInformation[EXCEPTION_MAXIMUM_PARAMETERS];
3937 } EXCEPTION_RECORD, *PEXCEPTION_RECORD;
3938
3939 typedef struct _EXCEPTION_POINTERS {
3940     PEXCEPTION_RECORD ExceptionRecord;
3941     PCONTEXT ContextRecord;
3942 } EXCEPTION_POINTERS, *PEXCEPTION_POINTERS, *LPEXCEPTION_POINTERS;
3943
3944 #ifdef FEATURE_PAL_SXS
3945
3946 typedef LONG EXCEPTION_DISPOSITION;
3947
3948 enum {
3949     ExceptionContinueExecution,
3950     ExceptionContinueSearch,
3951     ExceptionNestedException,
3952     ExceptionCollidedUnwind,
3953 };
3954
3955 #endif // FEATURE_PAL_SXS
3956
3957 //
3958 // A function table entry is generated for each frame function.
3959 //
3960 typedef struct _RUNTIME_FUNCTION {
3961     DWORD BeginAddress;
3962 #ifdef _TARGET_AMD64_
3963     DWORD EndAddress;
3964 #endif
3965     DWORD UnwindData;
3966 } RUNTIME_FUNCTION, *PRUNTIME_FUNCTION;
3967
3968 #define STANDARD_RIGHTS_REQUIRED  (0x000F0000L)
3969 #define SYNCHRONIZE               (0x00100000L)
3970 #define READ_CONTROL              (0x00020000L)
3971 #define MAXIMUM_ALLOWED           (0x02000000L)
3972
3973 #define EVENT_MODIFY_STATE        (0x0002)
3974 #define EVENT_ALL_ACCESS          (STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | 0x3) 
3975
3976 #define MUTANT_QUERY_STATE        (0x0001)
3977 #define MUTANT_ALL_ACCESS         (STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | MUTANT_QUERY_STATE)
3978 #define MUTEX_ALL_ACCESS          MUTANT_ALL_ACCESS
3979
3980 #define SEMAPHORE_MODIFY_STATE    (0x0002)
3981 #define SEMAPHORE_ALL_ACCESS      (STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | 0x3)
3982
3983 #define PROCESS_TERMINATE         (0x0001)  
3984 #define PROCESS_CREATE_THREAD     (0x0002)  
3985 #define PROCESS_SET_SESSIONID     (0x0004)  
3986 #define PROCESS_VM_OPERATION      (0x0008)  
3987 #define PROCESS_VM_READ           (0x0010)  
3988 #define PROCESS_VM_WRITE          (0x0020)  
3989 #define PROCESS_DUP_HANDLE        (0x0040)  
3990 #define PROCESS_CREATE_PROCESS    (0x0080)  
3991 #define PROCESS_SET_QUOTA         (0x0100)  
3992 #define PROCESS_SET_INFORMATION   (0x0200)  
3993 #define PROCESS_QUERY_INFORMATION (0x0400)  
3994 #define PROCESS_SUSPEND_RESUME    (0x0800)  
3995 #define PROCESS_ALL_ACCESS        (STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | \
3996                                    0xFFF)
3997
3998 PALIMPORT
3999 HANDLE
4000 PALAPI
4001 OpenProcess(
4002     IN DWORD dwDesiredAccess, /* PROCESS_DUP_HANDLE or PROCESS_ALL_ACCESS */
4003     IN BOOL bInheritHandle,
4004     IN DWORD dwProcessId
4005     );
4006
4007 PALIMPORT
4008 BOOL
4009 PALAPI
4010 EnumProcessModules(
4011     IN HANDLE hProcess,
4012     OUT HMODULE *lphModule,
4013     IN DWORD cb,
4014     OUT LPDWORD lpcbNeeded
4015     );
4016
4017 PALIMPORT
4018 VOID
4019 PALAPI
4020 OutputDebugStringA(
4021     IN LPCSTR lpOutputString);
4022
4023 PALIMPORT
4024 VOID
4025 PALAPI
4026 OutputDebugStringW(
4027     IN LPCWSTR lpOutputStrig);
4028
4029 #ifdef UNICODE
4030 #define OutputDebugString OutputDebugStringW
4031 #else
4032 #define OutputDebugString OutputDebugStringA
4033 #endif
4034
4035 PALIMPORT
4036 VOID
4037 PALAPI
4038 DebugBreak(
4039        VOID);
4040
4041 PALIMPORT
4042 int
4043 PALAPI
4044 lstrlenA(
4045      IN LPCSTR lpString);
4046
4047 PALIMPORT
4048 int
4049 PALAPI
4050 lstrlenW(
4051      IN LPCWSTR lpString);
4052
4053 #ifdef UNICODE
4054 #define lstrlen lstrlenW
4055 #else
4056 #define lstrlen lstrlenA
4057 #endif
4058
4059 PALIMPORT
4060 DWORD
4061 PALAPI
4062 GetEnvironmentVariableW(
4063             IN LPCWSTR lpName,
4064             OUT LPWSTR lpBuffer,
4065             IN DWORD nSize);
4066
4067 #ifdef UNICODE
4068 #define GetEnvironmentVariable GetEnvironmentVariableW
4069 #else
4070 #define GetEnvironmentVariable GetEnvironmentVariableA
4071 #endif
4072
4073 PALIMPORT
4074 BOOL
4075 PALAPI
4076 SetEnvironmentVariableW(
4077             IN LPCWSTR lpName,
4078             IN LPCWSTR lpValue);
4079
4080 #ifdef UNICODE
4081 #define SetEnvironmentVariable SetEnvironmentVariableW
4082 #else
4083 #define SetEnvironmentVariable SetEnvironmentVariableA
4084 #endif
4085
4086 PALIMPORT
4087 LPWSTR
4088 PALAPI
4089 GetEnvironmentStringsW(
4090                VOID);
4091
4092 #ifdef UNICODE
4093 #define GetEnvironmentStrings GetEnvironmentStringsW
4094 #else
4095 #define GetEnvironmentStrings GetEnvironmentStringsA
4096 #endif
4097
4098 PALIMPORT
4099 BOOL
4100 PALAPI
4101 FreeEnvironmentStringsW(
4102             IN LPWSTR);
4103
4104 #ifdef UNICODE
4105 #define FreeEnvironmentStrings FreeEnvironmentStringsW
4106 #else
4107 #define FreeEnvironmentStrings FreeEnvironmentStringsA
4108 #endif
4109
4110 PALIMPORT
4111 BOOL
4112 PALAPI
4113 CloseHandle(
4114         IN OUT HANDLE hObject);
4115
4116 PALIMPORT
4117 VOID
4118 PALAPI
4119 RaiseException(
4120            IN DWORD dwExceptionCode,
4121            IN DWORD dwExceptionFlags,
4122            IN DWORD nNumberOfArguments,
4123            IN CONST ULONG_PTR *lpArguments);
4124
4125 #ifdef FEATURE_PAL_SXS
4126 PALIMPORT
4127 PAL_NORETURN
4128 VOID
4129 PALAPI
4130 PAL_RaiseException(
4131            IN PEXCEPTION_POINTERS ExceptionPointers);
4132 #endif // FEATURE_PAL_SXS
4133
4134 PALIMPORT
4135 DWORD
4136 PALAPI
4137 GetTickCount(
4138          VOID);
4139
4140 PALIMPORT
4141 ULONGLONG
4142 PALAPI
4143 GetTickCount64(VOID);
4144
4145 PALIMPORT
4146 BOOL
4147 PALAPI
4148 QueryPerformanceCounter(
4149     OUT LARGE_INTEGER *lpPerformanceCount
4150     );
4151
4152 PALIMPORT
4153 BOOL
4154 PALAPI
4155 QueryPerformanceFrequency(
4156     OUT LARGE_INTEGER *lpFrequency
4157     );
4158
4159 PALIMPORT
4160 BOOL
4161 PALAPI
4162 QueryThreadCycleTime(
4163     IN HANDLE ThreadHandle,
4164     OUT PULONG64 CycleTime);
4165
4166 PALIMPORT
4167 INT
4168 PALAPI
4169 PAL_nanosleep(
4170     IN long timeInNs);
4171
4172 #ifndef FEATURE_PAL_SXS
4173
4174 typedef LONG (PALAPI *PTOP_LEVEL_EXCEPTION_FILTER)(
4175                            struct _EXCEPTION_POINTERS *ExceptionInfo);
4176 typedef PTOP_LEVEL_EXCEPTION_FILTER LPTOP_LEVEL_EXCEPTION_FILTER;
4177
4178 PALIMPORT
4179 LPTOP_LEVEL_EXCEPTION_FILTER
4180 PALAPI
4181 SetUnhandledExceptionFilter(
4182                 IN LPTOP_LEVEL_EXCEPTION_FILTER lpTopLevelExceptionFilter);
4183
4184 #else // FEATURE_PAL_SXS
4185
4186 typedef EXCEPTION_DISPOSITION (PALAPI *PVECTORED_EXCEPTION_HANDLER)(
4187                            struct _EXCEPTION_POINTERS *ExceptionPointers);
4188
4189 #endif // FEATURE_PAL_SXS
4190
4191 // Define BitScanForward64 and BitScanForward
4192 // Per MSDN, BitScanForward64 will search the mask data from LSB to MSB for a set bit.
4193 // If one is found, its bit position is returned in the out PDWORD argument and 1 is returned.
4194 // Otherwise, 0 is returned.
4195 //
4196 // On GCC, the equivalent function is __builtin_ffsl. It returns 1+index of the least
4197 // significant set bit, or 0 if if mask is zero.
4198 //
4199 // The same is true for BitScanForward, except that the GCC function is __builtin_ffs.
4200 EXTERN_C
4201 PALIMPORT
4202 inline
4203 unsigned char
4204 PALAPI
4205 BitScanForward(
4206     IN OUT PDWORD Index,
4207     IN UINT qwMask)
4208 {
4209     unsigned char bRet = FALSE;
4210     int iIndex = __builtin_ffsl(qwMask);
4211     if (iIndex != 0)
4212     {
4213         // Set the Index after deducting unity
4214         *Index = (DWORD)(iIndex - 1);
4215         bRet = TRUE;
4216     }
4217
4218     return bRet;
4219 }
4220
4221 EXTERN_C
4222 PALIMPORT
4223 inline
4224 unsigned char
4225 PALAPI
4226 BitScanForward64(
4227     IN OUT PDWORD Index,
4228     IN UINT64 qwMask)
4229 {
4230     unsigned char bRet = FALSE;
4231     int iIndex = __builtin_ffsl(qwMask);
4232     if (iIndex != 0)
4233     {
4234         // Set the Index after deducting unity
4235         *Index = (DWORD)(iIndex - 1);
4236         bRet = TRUE;
4237     }
4238
4239     return bRet;
4240 }
4241
4242 /*++
4243 Function:
4244 InterlockedIncrement
4245
4246 The InterlockedIncrement function increments (increases by one) the
4247 value of the specified variable and checks the resulting value. The
4248 function prevents more than one thread from using the same variable
4249 simultaneously.
4250
4251 Parameters
4252
4253 lpAddend 
4254 [in/out] Pointer to the variable to increment. 
4255
4256 Return Values
4257
4258 The return value is the resulting incremented value. 
4259
4260 --*/
4261 EXTERN_C
4262 PALIMPORT
4263 inline
4264 LONG
4265 PALAPI
4266 InterlockedIncrement(
4267     IN OUT LONG volatile *lpAddend)
4268 {
4269     return __sync_add_and_fetch(lpAddend, (LONG)1);
4270 }
4271
4272 EXTERN_C
4273 PALIMPORT
4274 inline
4275 LONGLONG
4276 PALAPI
4277 InterlockedIncrement64(
4278     IN OUT LONGLONG volatile *lpAddend)
4279 {
4280     return __sync_add_and_fetch(lpAddend, (LONGLONG)1);
4281 }
4282
4283 /*++
4284 Function:
4285 InterlockedDecrement
4286
4287 The InterlockedDecrement function decrements (decreases by one) the
4288 value of the specified variable and checks the resulting value. The
4289 function prevents more than one thread from using the same variable
4290 simultaneously.
4291
4292 Parameters
4293
4294 lpAddend 
4295 [in/out] Pointer to the variable to decrement. 
4296
4297 Return Values
4298
4299 The return value is the resulting decremented value.
4300
4301 --*/
4302 EXTERN_C
4303 PALIMPORT
4304 inline
4305 LONG
4306 PALAPI
4307 InterlockedDecrement(
4308     IN OUT LONG volatile *lpAddend)
4309 {
4310     return __sync_sub_and_fetch(lpAddend, (LONG)1);
4311 }
4312
4313 EXTERN_C
4314 PALIMPORT
4315 inline
4316 LONGLONG
4317 PALAPI
4318 InterlockedDecrement64(
4319     IN OUT LONGLONG volatile *lpAddend)
4320 {
4321     return __sync_sub_and_fetch(lpAddend, (LONGLONG)1);
4322 }
4323
4324 /*++
4325 Function:
4326 InterlockedExchange
4327
4328 The InterlockedExchange function atomically exchanges a pair of
4329 values. The function prevents more than one thread from using the same
4330 variable simultaneously.
4331
4332 Parameters
4333
4334 Target 
4335 [in/out] Pointer to the value to exchange. The function sets
4336 this variable to Value, and returns its prior value.
4337 Value 
4338 [in] Specifies a new value for the variable pointed to by Target. 
4339
4340 Return Values
4341
4342 The function returns the initial value pointed to by Target. 
4343
4344 --*/
4345 EXTERN_C
4346 PALIMPORT
4347 inline
4348 LONG
4349 PALAPI
4350 InterlockedExchange(
4351     IN OUT LONG volatile *Target,
4352     IN LONG Value)
4353 {
4354     return __sync_swap(Target, Value);
4355 }
4356
4357 EXTERN_C
4358 PALIMPORT
4359 inline
4360 LONGLONG
4361 PALAPI
4362 InterlockedExchange64(
4363     IN OUT LONGLONG volatile *Target,
4364     IN LONGLONG Value)
4365 {
4366     return __sync_swap(Target, Value);
4367 }
4368
4369 /*++
4370 Function:
4371 InterlockedCompareExchange
4372
4373 The InterlockedCompareExchange function performs an atomic comparison
4374 of the specified values and exchanges the values, based on the outcome
4375 of the comparison. The function prevents more than one thread from
4376 using the same variable simultaneously.
4377
4378 If you are exchanging pointer values, this function has been
4379 superseded by the InterlockedCompareExchangePointer function.
4380
4381 Parameters
4382
4383 Destination     [in/out] Specifies the address of the destination value. The sign is ignored.
4384 Exchange        [in]     Specifies the exchange value. The sign is ignored.
4385 Comperand       [in]     Specifies the value to compare to Destination. The sign is ignored.
4386
4387 Return Values
4388
4389 The return value is the initial value of the destination.
4390
4391 --*/
4392 EXTERN_C
4393 PALIMPORT
4394 inline
4395 LONG
4396 PALAPI
4397 InterlockedCompareExchange(
4398     IN OUT LONG volatile *Destination,
4399     IN LONG Exchange,
4400     IN LONG Comperand)
4401 {
4402     return __sync_val_compare_and_swap(
4403         Destination, /* The pointer to a variable whose value is to be compared with. */
4404         Comperand, /* The value to be compared */
4405         Exchange /* The value to be stored */);
4406 }
4407
4408 EXTERN_C
4409 PALIMPORT
4410 inline
4411 LONG
4412 PALAPI
4413 InterlockedCompareExchangeAcquire(
4414     IN OUT LONG volatile *Destination,
4415     IN LONG Exchange,
4416     IN LONG Comperand)
4417 {
4418     // TODO: implement the version with only the acquire semantics
4419     return __sync_val_compare_and_swap(
4420         Destination, /* The pointer to a variable whose value is to be compared with. */
4421         Comperand, /* The value to be compared */
4422         Exchange /* The value to be stored */);
4423 }
4424
4425 EXTERN_C
4426 PALIMPORT
4427 inline
4428 LONG
4429 PALAPI
4430 InterlockedCompareExchangeRelease(
4431     IN OUT LONG volatile *Destination,
4432     IN LONG Exchange,
4433     IN LONG Comperand)
4434 {
4435     // TODO: implement the version with only the release semantics
4436     return __sync_val_compare_and_swap(
4437         Destination, /* The pointer to a variable whose value is to be compared with. */
4438         Comperand, /* The value to be compared */
4439         Exchange /* The value to be stored */);
4440 }
4441
4442 // See the 32-bit variant in interlock2.s
4443 EXTERN_C
4444 PALIMPORT
4445 inline
4446 LONGLONG
4447 PALAPI
4448 InterlockedCompareExchange64(
4449     IN OUT LONGLONG volatile *Destination,
4450     IN LONGLONG Exchange,
4451     IN LONGLONG Comperand)
4452 {
4453     return __sync_val_compare_and_swap(
4454         Destination, /* The pointer to a variable whose value is to be compared with. */
4455         Comperand, /* The value to be compared */
4456         Exchange /* The value to be stored */);
4457 }
4458
4459 /*++
4460 Function:
4461 InterlockedExchangeAdd
4462
4463 The InterlockedExchangeAdd function atomically adds the value of 'Value'
4464 to the variable that 'Addend' points to.
4465
4466 Parameters
4467
4468 lpAddend
4469 [in/out] Pointer to the variable to to added.
4470
4471 Return Values
4472
4473 The return value is the original value that 'Addend' pointed to.
4474
4475 --*/
4476 EXTERN_C
4477 PALIMPORT
4478 inline
4479 LONG
4480 PALAPI
4481 InterlockedExchangeAdd(
4482     IN OUT LONG volatile *Addend,
4483     IN LONG Value)
4484 {
4485     return __sync_fetch_and_add(Addend, Value);
4486 }
4487
4488 EXTERN_C
4489 PALIMPORT
4490 inline
4491 LONGLONG
4492 PALAPI
4493 InterlockedExchangeAdd64(
4494     IN OUT LONGLONG volatile *Addend,
4495     IN LONGLONG Value)
4496 {
4497     return __sync_fetch_and_add(Addend, Value);
4498 }
4499
4500 EXTERN_C
4501 PALIMPORT
4502 inline
4503 LONG
4504 PALAPI
4505 InterlockedAnd(
4506     IN OUT LONG volatile *Destination,
4507     IN LONG Value)
4508 {
4509     return __sync_fetch_and_and(Destination, Value);
4510 }
4511
4512 EXTERN_C
4513 PALIMPORT
4514 inline
4515 LONG
4516 PALAPI
4517 InterlockedOr(
4518     IN OUT LONG volatile *Destination,
4519     IN LONG Value)
4520 {
4521     return __sync_fetch_and_or(Destination, Value);
4522 }
4523
4524 EXTERN_C
4525 PALIMPORT
4526 inline
4527 UCHAR
4528 PALAPI
4529 InterlockedBitTestAndReset(
4530     IN OUT LONG volatile *Base,
4531     IN LONG Bit)
4532 {
4533     return (InterlockedAnd(Base, ~(1 << Bit)) & (1 << Bit)) != 0;
4534 }
4535
4536 EXTERN_C
4537 PALIMPORT
4538 inline
4539 UCHAR
4540 PALAPI
4541 InterlockedBitTestAndSet(
4542     IN OUT LONG volatile *Base,
4543     IN LONG Bit)
4544 {
4545     return (InterlockedOr(Base, (1 << Bit)) & (1 << Bit)) != 0;
4546 }
4547
4548 #if defined(BIT64)
4549 #define InterlockedExchangePointer(Target, Value) \
4550     ((PVOID)InterlockedExchange64((PLONG64)(Target), (LONGLONG)(Value)))
4551
4552 #define InterlockedCompareExchangePointer(Destination, ExChange, Comperand) \
4553     ((PVOID)InterlockedCompareExchange64((PLONG64)(Destination), (LONGLONG)(ExChange), (LONGLONG)(Comperand)))
4554 #else
4555 #define InterlockedExchangePointer(Target, Value) \
4556     ((PVOID)(UINT_PTR)InterlockedExchange((PLONG)(UINT_PTR)(Target), (LONG)(UINT_PTR)(Value)))
4557
4558 #define InterlockedCompareExchangePointer(Destination, ExChange, Comperand) \
4559     ((PVOID)(UINT_PTR)InterlockedCompareExchange((PLONG)(UINT_PTR)(Destination), (LONG)(UINT_PTR)(ExChange), (LONG)(UINT_PTR)(Comperand)))
4560 #endif
4561
4562 /*++
4563 Function:
4564 MemoryBarrier
4565
4566 The MemoryBarrier function creates a full memory barrier.
4567
4568 --*/
4569 EXTERN_C
4570 PALIMPORT
4571 inline
4572 VOID
4573 PALAPI
4574 MemoryBarrier(
4575     VOID)
4576 {
4577     __sync_synchronize();
4578 }
4579
4580 EXTERN_C
4581 PALIMPORT
4582 inline
4583 VOID
4584 PALAPI
4585 YieldProcessor(
4586     VOID)
4587 {
4588 #if defined(_X86_) || defined(_AMD64_)
4589     __asm__ __volatile__(
4590         "rep\n"
4591         "nop");
4592 #elif defined(_ARM64_)
4593     __asm__ __volatile__( "yield");
4594 #else
4595     return;
4596 #endif
4597 }
4598
4599 PALIMPORT
4600 DWORD
4601 PALAPI
4602 GetCurrentProcessorNumber(VOID);
4603
4604 /*++
4605 Function:
4606 PAL_HasGetCurrentProcessorNumber
4607
4608 Checks if GetCurrentProcessorNumber is available in the current environment
4609
4610 --*/
4611 PALIMPORT
4612 BOOL
4613 PALAPI
4614 PAL_HasGetCurrentProcessorNumber(VOID);
4615     
4616 #define FORMAT_MESSAGE_ALLOCATE_BUFFER 0x00000100
4617 #define FORMAT_MESSAGE_IGNORE_INSERTS  0x00000200
4618 #define FORMAT_MESSAGE_FROM_STRING     0x00000400
4619 #define FORMAT_MESSAGE_FROM_SYSTEM     0x00001000
4620 #define FORMAT_MESSAGE_ARGUMENT_ARRAY  0x00002000
4621 #define FORMAT_MESSAGE_MAX_WIDTH_MASK  0x000000FF
4622
4623 PALIMPORT
4624 DWORD
4625 PALAPI
4626 FormatMessageW(
4627            IN DWORD dwFlags,
4628            IN LPCVOID lpSource,
4629            IN DWORD dwMessageId,
4630            IN DWORD dwLanguageId,
4631            OUT LPWSTR lpBffer,
4632            IN DWORD nSize,
4633            IN va_list *Arguments);
4634
4635 #ifdef UNICODE
4636 #define FormatMessage FormatMessageW
4637 #endif
4638
4639
4640 PALIMPORT
4641 DWORD
4642 PALAPI
4643 GetLastError(
4644          VOID);
4645
4646 PALIMPORT
4647 VOID
4648 PALAPI
4649 SetLastError(
4650          IN DWORD dwErrCode);
4651
4652 PALIMPORT
4653 LPWSTR
4654 PALAPI
4655 GetCommandLineW(
4656         VOID);
4657
4658 #ifdef UNICODE
4659 #define GetCommandLine GetCommandLineW
4660 #endif
4661
4662 PALIMPORT
4663 VOID 
4664 PALAPI 
4665 RtlRestoreContext(
4666   IN PCONTEXT ContextRecord,
4667   IN PEXCEPTION_RECORD ExceptionRecord
4668 );
4669
4670 PALIMPORT
4671 VOID 
4672 PALAPI 
4673 RtlCaptureContext(
4674   OUT PCONTEXT ContextRecord
4675 );
4676
4677 PALIMPORT
4678 UINT 
4679 PALAPI 
4680 GetWriteWatch(
4681   IN DWORD dwFlags,
4682   IN PVOID lpBaseAddress,
4683   IN SIZE_T dwRegionSize,
4684   OUT PVOID *lpAddresses,
4685   IN OUT PULONG_PTR lpdwCount,
4686   OUT PULONG lpdwGranularity
4687 );
4688
4689 PALIMPORT
4690 UINT 
4691 PALAPI 
4692 ResetWriteWatch(
4693   IN LPVOID lpBaseAddress,
4694   IN SIZE_T dwRegionSize
4695 );
4696
4697 PALIMPORT
4698 VOID 
4699 PALAPI 
4700 FlushProcessWriteBuffers(VOID);
4701
4702 typedef void (*PAL_ActivationFunction)(CONTEXT *context);
4703 typedef BOOL (*PAL_SafeActivationCheckFunction)(SIZE_T ip, BOOL checkingCurrentThread);
4704
4705 PALIMPORT
4706 VOID
4707 PALAPI
4708 PAL_SetActivationFunction(
4709     IN PAL_ActivationFunction pActivationFunction,
4710     IN PAL_SafeActivationCheckFunction pSafeActivationCheckFunction);
4711
4712 PALIMPORT
4713 BOOL
4714 PALAPI
4715 PAL_InjectActivation(
4716     IN HANDLE hThread
4717 );
4718
4719 #define VER_PLATFORM_WIN32_WINDOWS        1
4720 #define VER_PLATFORM_WIN32_NT        2
4721 #define VER_PLATFORM_UNIX            10
4722 #define VER_PLATFORM_MACOSX          11
4723
4724 typedef struct _OSVERSIONINFOA {
4725     DWORD dwOSVersionInfoSize;
4726     DWORD dwMajorVersion;
4727     DWORD dwMinorVersion;
4728     DWORD dwBuildNumber;
4729     DWORD dwPlatformId;
4730     CHAR szCSDVersion[ 128 ];
4731 } OSVERSIONINFOA, *POSVERSIONINFOA, *LPOSVERSIONINFOA;
4732
4733 typedef struct _OSVERSIONINFOW {
4734     DWORD dwOSVersionInfoSize;
4735     DWORD dwMajorVersion;
4736     DWORD dwMinorVersion;
4737     DWORD dwBuildNumber;
4738     DWORD dwPlatformId;
4739     WCHAR szCSDVersion[ 128 ];
4740 } OSVERSIONINFOW, *POSVERSIONINFOW, *LPOSVERSIONINFOW;
4741
4742 #ifdef UNICODE
4743 typedef OSVERSIONINFOW OSVERSIONINFO;
4744 typedef POSVERSIONINFOW POSVERSIONINFO;
4745 typedef LPOSVERSIONINFOW LPOSVERSIONINFO;
4746 #else
4747 typedef OSVERSIONINFOA OSVERSIONINFO;
4748 typedef POSVERSIONINFOA POSVERSIONINFO;
4749 typedef LPOSVERSIONINFOA LPOSVERSIONINFO;
4750 #endif
4751
4752 typedef struct _OSVERSIONINFOEXA {
4753     DWORD dwOSVersionInfoSize;
4754     DWORD dwMajorVersion;
4755     DWORD dwMinorVersion;
4756     DWORD dwBuildNumber;
4757     DWORD dwPlatformId;
4758     CHAR szCSDVersion[ 128 ];
4759     WORD  wServicePackMajor;
4760     WORD  wServicePackMinor;
4761     WORD  wSuiteMask;
4762     BYTE  wProductType;
4763     BYTE  wReserved;
4764 } OSVERSIONINFOEXA, *POSVERSIONINFOEXA, *LPOSVERSIONINFOEXA;
4765
4766 typedef struct _OSVERSIONINFOEXW {
4767     DWORD dwOSVersionInfoSize;
4768     DWORD dwMajorVersion;
4769     DWORD dwMinorVersion;
4770     DWORD dwBuildNumber;
4771     DWORD dwPlatformId;
4772     WCHAR szCSDVersion[ 128 ];
4773     WORD  wServicePackMajor;
4774     WORD  wServicePackMinor;
4775     WORD  wSuiteMask;
4776     BYTE  wProductType;
4777     BYTE  wReserved;
4778 } OSVERSIONINFOEXW, *POSVERSIONINFOEXW, *LPOSVERSIONINFOEXW;
4779
4780 #ifdef UNICODE
4781 typedef OSVERSIONINFOEXW OSVERSIONINFOEX;
4782 typedef POSVERSIONINFOEXW POSVERSIONINFOEX;
4783 typedef LPOSVERSIONINFOEXW LPOSVERSIONINFOEX;
4784 #else
4785 typedef OSVERSIONINFOEXA OSVERSIONINFOEX;
4786 typedef POSVERSIONINFOEXA POSVERSIONINFOEX;
4787 typedef LPOSVERSIONINFOEXA LPOSVERSIONINFOEX;
4788 #endif
4789
4790 #define IMAGE_FILE_MACHINE_I386              0x014c
4791 #define IMAGE_FILE_MACHINE_ARM64             0xAA64  // ARM64 Little-Endian
4792
4793 typedef struct _SYSTEM_INFO {
4794     WORD wProcessorArchitecture_PAL_Undefined;
4795     WORD wReserved_PAL_Undefined; // NOTE: diff from winbase.h - no obsolete dwOemId union
4796     DWORD dwPageSize;
4797     LPVOID lpMinimumApplicationAddress;
4798     LPVOID lpMaximumApplicationAddress;
4799     DWORD_PTR dwActiveProcessorMask_PAL_Undefined;
4800     DWORD dwNumberOfProcessors;
4801     DWORD dwProcessorType_PAL_Undefined;
4802     DWORD dwAllocationGranularity;
4803     WORD wProcessorLevel_PAL_Undefined;
4804     WORD wProcessorRevision_PAL_Undefined;
4805 } SYSTEM_INFO, *LPSYSTEM_INFO;
4806
4807 PALIMPORT
4808 VOID
4809 PALAPI
4810 GetSystemInfo(
4811           OUT LPSYSTEM_INFO lpSystemInfo);
4812
4813 PALIMPORT
4814 BOOL
4815 PALAPI
4816 GetDiskFreeSpaceW(
4817           LPCWSTR lpDirectoryName,
4818           LPDWORD lpSectorsPerCluster,
4819           LPDWORD lpBytesPerSector,
4820           LPDWORD lpNumberOfFreeClusters,
4821           LPDWORD lpTotalNumberOfClusters);
4822
4823 #ifdef UNICODE
4824 #define GetDiskFreeSpace GetDiskFreeSpaceW
4825 #endif
4826
4827 PALIMPORT
4828 BOOL
4829 PALAPI
4830 CreatePipe(
4831     OUT PHANDLE hReadPipe,
4832     OUT PHANDLE hWritePipe,
4833     IN LPSECURITY_ATTRIBUTES lpPipeAttributes,
4834     IN DWORD nSize
4835     );
4836
4837 PALIMPORT
4838 BOOL
4839 PALAPI
4840 DeregisterEventSource (
4841     IN HANDLE hEventLog
4842     );
4843
4844 PALIMPORT
4845 HANDLE
4846 PALAPI
4847 RegisterEventSourceA (
4848     IN OPTIONAL LPCSTR lpUNCServerName,
4849     IN     LPCSTR lpSourceName
4850     );
4851 PALIMPORT
4852 HANDLE
4853 PALAPI
4854 RegisterEventSourceW (
4855     IN OPTIONAL LPCWSTR lpUNCServerName,
4856     IN     LPCWSTR lpSourceName
4857     );
4858 #ifdef UNICODE
4859 #define RegisterEventSource  RegisterEventSourceW
4860 #else
4861 #define RegisterEventSource  RegisterEventSourceA
4862 #endif // !UNICODE
4863
4864 //
4865 // NUMA related APIs
4866 //
4867
4868 typedef enum _PROCESSOR_CACHE_TYPE { 
4869   CacheUnified,
4870   CacheInstruction,
4871   CacheData,
4872   CacheTrace
4873 } PROCESSOR_CACHE_TYPE;
4874
4875 typedef struct _PROCESSOR_NUMBER {
4876   WORD Group;
4877   BYTE Number;
4878   BYTE Reserved;
4879 } PROCESSOR_NUMBER, *PPROCESSOR_NUMBER;
4880
4881 typedef enum _LOGICAL_PROCESSOR_RELATIONSHIP { 
4882   RelationProcessorCore,
4883   RelationNumaNode,
4884   RelationCache,
4885   RelationProcessorPackage,
4886   RelationGroup,
4887   RelationAll               = 0xffff
4888 } LOGICAL_PROCESSOR_RELATIONSHIP;
4889
4890 typedef ULONG_PTR KAFFINITY;
4891
4892 #define ANYSIZE_ARRAY 1
4893
4894 typedef struct _GROUP_AFFINITY {
4895   KAFFINITY Mask;
4896   WORD      Group;
4897   WORD      Reserved[3];
4898 } GROUP_AFFINITY, *PGROUP_AFFINITY;
4899
4900 typedef struct _PROCESSOR_GROUP_INFO {
4901   BYTE      MaximumProcessorCount;
4902   BYTE      ActiveProcessorCount;
4903   BYTE      Reserved[38];
4904   KAFFINITY ActiveProcessorMask;
4905 } PROCESSOR_GROUP_INFO, *PPROCESSOR_GROUP_INFO;
4906
4907 typedef struct _PROCESSOR_RELATIONSHIP {
4908   BYTE           Flags;
4909   BYTE           EfficiencyClass;
4910   BYTE           Reserved[21];
4911   WORD           GroupCount;
4912   GROUP_AFFINITY GroupMask[ANYSIZE_ARRAY];
4913 } PROCESSOR_RELATIONSHIP, *PPROCESSOR_RELATIONSHIP;
4914
4915 typedef struct _GROUP_RELATIONSHIP {
4916   WORD                 MaximumGroupCount;
4917   WORD                 ActiveGroupCount;
4918   BYTE                 Reserved[20];
4919   PROCESSOR_GROUP_INFO GroupInfo[ANYSIZE_ARRAY];
4920 } GROUP_RELATIONSHIP, *PGROUP_RELATIONSHIP;
4921
4922 typedef struct _NUMA_NODE_RELATIONSHIP {
4923   DWORD          NodeNumber;
4924   BYTE           Reserved[20];
4925   GROUP_AFFINITY GroupMask;
4926 } NUMA_NODE_RELATIONSHIP, *PNUMA_NODE_RELATIONSHIP;
4927
4928 typedef struct _CACHE_RELATIONSHIP {
4929   BYTE                 Level;
4930   BYTE                 Associativity;
4931   WORD                 LineSize;
4932   DWORD                CacheSize;
4933   PROCESSOR_CACHE_TYPE Type;
4934   BYTE                 Reserved[20];
4935   GROUP_AFFINITY       GroupMask;
4936 } CACHE_RELATIONSHIP, *PCACHE_RELATIONSHIP;
4937
4938 typedef struct _SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX {
4939   LOGICAL_PROCESSOR_RELATIONSHIP Relationship;
4940   DWORD                          Size;
4941   union {
4942     PROCESSOR_RELATIONSHIP Processor;
4943     NUMA_NODE_RELATIONSHIP NumaNode;
4944     CACHE_RELATIONSHIP     Cache;
4945     GROUP_RELATIONSHIP     Group;
4946   };
4947 } SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX, *PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX;
4948
4949
4950 PALIMPORT
4951 BOOL
4952 PALAPI
4953 GetNumaHighestNodeNumber(
4954   OUT PULONG HighestNodeNumber
4955 );
4956
4957 PALIMPORT
4958 BOOL
4959 PALAPI
4960 GetNumaProcessorNodeEx(
4961   IN  PPROCESSOR_NUMBER Processor,
4962   OUT PUSHORT NodeNumber
4963 );
4964
4965 PALIMPORT
4966 LPVOID
4967 PALAPI
4968 VirtualAllocExNuma(
4969   IN HANDLE hProcess,
4970   IN OPTIONAL LPVOID lpAddress,
4971   IN SIZE_T dwSize,
4972   IN DWORD flAllocationType,
4973   IN DWORD flProtect,
4974   IN DWORD nndPreferred
4975 );
4976
4977 PALIMPORT
4978 BOOL
4979 PALAPI
4980 GetLogicalProcessorInformationEx(
4981   IN LOGICAL_PROCESSOR_RELATIONSHIP RelationshipType,
4982   OUT OPTIONAL PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX Buffer,
4983   IN OUT PDWORD ReturnedLength
4984 );
4985
4986 PALIMPORT
4987 DWORD_PTR
4988 PALAPI
4989 SetThreadAffinityMask(
4990   IN HANDLE hThread,
4991   IN DWORD_PTR dwThreadAffinityMask
4992 );
4993
4994 PALIMPORT
4995 BOOL
4996 PALAPI
4997 SetThreadGroupAffinity(
4998   IN HANDLE hThread,
4999   IN const GROUP_AFFINITY *GroupAffinity,
5000   OUT OPTIONAL PGROUP_AFFINITY PreviousGroupAffinity
5001 );
5002
5003 PALIMPORT
5004 BOOL
5005 PALAPI
5006 GetThreadGroupAffinity(
5007   IN HANDLE hThread,
5008   OUT PGROUP_AFFINITY GroupAffinity
5009 );
5010
5011 PALIMPORT
5012 VOID
5013 PALAPI
5014 GetCurrentProcessorNumberEx(
5015   OUT PPROCESSOR_NUMBER ProcNumber
5016 );
5017
5018 PALIMPORT
5019 BOOL
5020 PALAPI
5021 GetProcessAffinityMask(
5022   IN HANDLE hProcess,
5023   OUT PDWORD_PTR lpProcessAffinityMask,
5024   OUT PDWORD_PTR lpSystemAffinityMask
5025 );
5026
5027 //
5028 // The types of events that can be logged.
5029 //
5030 #define EVENTLOG_SUCCESS                0x0000
5031 #define EVENTLOG_ERROR_TYPE             0x0001
5032 #define EVENTLOG_WARNING_TYPE           0x0002
5033 #define EVENTLOG_INFORMATION_TYPE       0x0004
5034 #define EVENTLOG_AUDIT_SUCCESS          0x0008
5035 #define EVENTLOG_AUDIT_FAILURE          0x0010
5036
5037 PALIMPORT
5038 BOOL
5039 PALAPI
5040 ReportEventA (
5041     IN     HANDLE     hEventLog,
5042     IN     WORD       wType,
5043     IN     WORD       wCategory,
5044     IN     DWORD      dwEventID,
5045     IN OPTIONAL PSID       lpUserSid,
5046     IN     WORD       wNumStrings,
5047     IN     DWORD      dwDataSize,
5048     IN OPTIONAL LPCSTR *lpStrings,
5049     IN OPTIONAL LPVOID lpRawData
5050     );
5051 PALIMPORT
5052 BOOL
5053 PALAPI
5054 ReportEventW (
5055     IN     HANDLE     hEventLog,
5056     IN     WORD       wType,
5057     IN     WORD       wCategory,
5058     IN     DWORD      dwEventID,
5059     IN OPTIONAL PSID       lpUserSid,
5060     IN     WORD       wNumStrings,
5061     IN     DWORD      dwDataSize,
5062     IN OPTIONAL LPCWSTR *lpStrings,
5063     IN OPTIONAL LPVOID lpRawData
5064     );
5065 #ifdef UNICODE
5066 #define ReportEvent  ReportEventW
5067 #else
5068 #define ReportEvent  ReportEventA
5069 #endif // !UNICODE
5070
5071 PALIMPORT
5072 HRESULT
5073 PALAPI
5074 CoCreateGuid(OUT GUID * pguid);
5075
5076 #if defined FEATURE_PAL_ANSI
5077 #include "palprivate.h"
5078 #endif //FEATURE_PAL_ANSI
5079 /******************* C Runtime Entrypoints *******************************/
5080
5081 /* Some C runtime functions needs to be reimplemented by the PAL.
5082    To avoid name collisions, those functions have been renamed using
5083    defines */
5084 #ifndef PAL_STDCPP_COMPAT
5085 #define exit          PAL_exit
5086 #define atexit        PAL_atexit
5087 #define printf        PAL_printf
5088 #define vprintf       PAL_vprintf
5089 #define wprintf       PAL_wprintf
5090 #define wcsspn        PAL_wcsspn
5091 #define wcstod        PAL_wcstod
5092 #define wcstol        PAL_wcstol
5093 #define wcstoul       PAL_wcstoul
5094 #define wcscat        PAL_wcscat
5095 #define wcscpy        PAL_wcscpy
5096 #define wcslen        PAL_wcslen
5097 #define wcsncmp       PAL_wcsncmp
5098 #define wcschr        PAL_wcschr
5099 #define wcsrchr       PAL_wcsrchr
5100 #define wcsstr        PAL_wcsstr
5101 #define swscanf       PAL_swscanf
5102 #define wcspbrk       PAL_wcspbrk
5103 #define wcscmp        PAL_wcscmp
5104 #define wcsncat       PAL_wcsncat
5105 #define wcsncpy       PAL_wcsncpy
5106 #define wcstok        PAL_wcstok
5107 #define wcscspn       PAL_wcscspn
5108 #define iswprint      PAL_iswprint
5109 #define iswalpha      PAL_iswalpha
5110 #define iswdigit      PAL_iswdigit
5111 #define iswspace      PAL_iswspace
5112 #define iswupper      PAL_iswupper
5113 #define iswxdigit     PAL_iswxdigit
5114 #define towlower      PAL_towlower
5115 #define towupper      PAL_towupper
5116 #define realloc       PAL_realloc
5117 #define fopen         PAL_fopen
5118 #define strtok        PAL_strtok
5119 #define strtoul       PAL_strtoul
5120 #define fprintf       PAL_fprintf
5121 #define fwprintf      PAL_fwprintf
5122 #define vfprintf      PAL_vfprintf
5123 #define vfwprintf     PAL_vfwprintf
5124 #define ctime         PAL_ctime
5125 #define localtime     PAL_localtime
5126 #define mktime        PAL_mktime
5127 #define rand          PAL_rand
5128 #define time          PAL_time
5129 #define getenv        PAL_getenv
5130 #define fgets         PAL_fgets
5131 #define fgetws        PAL_fgetws
5132 #define fputc         PAL_fputc
5133 #define putchar       PAL_putchar
5134 #define qsort         PAL_qsort
5135 #define bsearch       PAL_bsearch
5136 #define ferror        PAL_ferror
5137 #define fread         PAL_fread
5138 #define fwrite        PAL_fwrite
5139 #define feof          PAL_feof
5140 #define ftell         PAL_ftell
5141 #define fclose        PAL_fclose
5142 #define setbuf        PAL_setbuf
5143 #define fflush        PAL_fflush
5144 #define fputs         PAL_fputs
5145 #define fseek         PAL_fseek
5146 #define fgetpos       PAL_fgetpos
5147 #define fsetpos       PAL_fsetpos
5148 #define getc          PAL_getc
5149 #define fgetc         PAL_getc // not a typo
5150 #define ungetc        PAL_ungetc
5151 #define setvbuf       PAL_setvbuf
5152 #define atol          PAL_atol
5153 #define labs          PAL_labs
5154 #define acos          PAL_acos
5155 #define asin          PAL_asin
5156 #define atan2         PAL_atan2
5157 #define exp           PAL_exp
5158 #define log           PAL_log
5159 #define log10         PAL_log10
5160 #define pow           PAL_pow
5161 #define acosf         PAL_acosf
5162 #define asinf         PAL_asinf
5163 #define atan2f        PAL_atan2f
5164 #define expf          PAL_expf
5165 #define logf          PAL_logf
5166 #define log10f        PAL_log10f
5167 #define powf          PAL_powf
5168 #define malloc        PAL_malloc
5169 #define free          PAL_free
5170 #define mkstemp       PAL_mkstemp
5171 #define rename        PAL_rename
5172 #define _strdup       PAL__strdup
5173 #define _getcwd       PAL__getcwd
5174 #define _open         PAL__open
5175 #define _close        PAL__close
5176 #define _wcstoui64    PAL__wcstoui64
5177 #define _flushall     PAL__flushall
5178 #define strnlen       PAL_strnlen
5179 #define wcsnlen       PAL_wcsnlen
5180
5181 #ifdef _AMD64_ 
5182 #define _mm_getcsr    PAL__mm_getcsr
5183 #define _mm_setcsr    PAL__mm_setcsr
5184 #endif // _AMD64_
5185
5186 #endif // !PAL_STDCPP_COMPAT
5187
5188 #ifndef _CONST_RETURN
5189 #ifdef  __cplusplus
5190 #define _CONST_RETURN  const
5191 #define _CRT_CONST_CORRECT_OVERLOADS
5192 #else
5193 #define _CONST_RETURN
5194 #endif
5195 #endif
5196
5197 /* For backwards compatibility */
5198 #define _WConst_return _CONST_RETURN
5199
5200 #define EOF     (-1)
5201
5202 typedef int errno_t;
5203
5204 #ifndef PAL_STDCPP_COMPAT
5205
5206 typedef struct {
5207     int quot;
5208     int rem;
5209 } div_t;
5210
5211 PALIMPORT div_t div(int numer, int denom);
5212
5213 #if defined(_DEBUG)
5214
5215 /*++
5216 Function:
5217 PAL_memcpy
5218
5219 Overlapping buffer-safe version of memcpy.
5220 See MSDN doc for memcpy
5221 --*/
5222 EXTERN_C
5223 PALIMPORT
5224 void *PAL_memcpy (void *dest, const void *src, size_t count);
5225
5226 PALIMPORT void * __cdecl memcpy(void *, const void *, size_t);
5227
5228 #define memcpy PAL_memcpy
5229 #define IS_PAL_memcpy 1
5230 #define TEST_PAL_DEFERRED(def) IS_##def
5231 #define IS_REDEFINED_IN_PAL(def) TEST_PAL_DEFERRED(def)
5232 #else //defined(_DEBUG)
5233 PALIMPORT void * __cdecl memcpy(void *, const void *, size_t);
5234 #endif //defined(_DEBUG)
5235 PALIMPORT int    __cdecl memcmp(const void *, const void *, size_t);
5236 PALIMPORT void * __cdecl memset(void *, int, size_t);
5237 PALIMPORT void * __cdecl memmove(void *, const void *, size_t);
5238 PALIMPORT void * __cdecl memchr(const void *, int, size_t);
5239 PALIMPORT long long int __cdecl atoll(const char *);
5240 PALIMPORT size_t __cdecl strlen(const char *);
5241 PALIMPORT int __cdecl strcmp(const char*, const char *);
5242 PALIMPORT int __cdecl strncmp(const char*, const char *, size_t);
5243 PALIMPORT int __cdecl _strnicmp(const char *, const char *, size_t);
5244 PALIMPORT char * __cdecl strcat(char *, const char *);
5245 PALIMPORT char * __cdecl strncat(char *, const char *, size_t);
5246 PALIMPORT char * __cdecl strcpy(char *, const char *);
5247 PALIMPORT char * __cdecl strncpy(char *, const char *, size_t);
5248 PALIMPORT char * __cdecl strchr(const char *, int);
5249 PALIMPORT char * __cdecl strrchr(const char *, int);
5250 PALIMPORT char * __cdecl strpbrk(const char *, const char *);
5251 PALIMPORT char * __cdecl strstr(const char *, const char *);
5252 PALIMPORT char * __cdecl strtok(char *, const char *);
5253 PALIMPORT size_t __cdecl strspn(const char *, const char *);
5254 PALIMPORT size_t  __cdecl strcspn(const char *, const char *);
5255 PALIMPORT int __cdecl atoi(const char *);
5256 PALIMPORT LONG __cdecl atol(const char *);
5257 PALIMPORT ULONG __cdecl strtoul(const char *, char **, int);
5258 PALIMPORT double __cdecl atof(const char *);
5259 PALIMPORT double __cdecl strtod(const char *, char **);
5260 PALIMPORT int __cdecl isprint(int);
5261 PALIMPORT int __cdecl isspace(int);
5262 PALIMPORT int __cdecl isalpha(int);
5263 PALIMPORT int __cdecl isalnum(int);
5264 PALIMPORT int __cdecl isdigit(int);
5265 PALIMPORT int __cdecl isxdigit(int);
5266 PALIMPORT int __cdecl isupper(int);
5267 PALIMPORT int __cdecl islower(int);
5268 PALIMPORT int __cdecl tolower(int);
5269 PALIMPORT int __cdecl toupper(int);
5270
5271 #endif // PAL_STDCPP_COMPAT
5272
5273 /* _TRUNCATE */
5274 #if !defined(_TRUNCATE)
5275 #define _TRUNCATE ((size_t)-1)
5276 #endif
5277
5278 PALIMPORT errno_t __cdecl memcpy_s(void *, size_t, const void *, size_t);
5279 PALIMPORT errno_t __cdecl memmove_s(void *, size_t, const void *, size_t);
5280 PALIMPORT char * __cdecl _strlwr(char *);
5281 PALIMPORT int __cdecl _stricmp(const char *, const char *);
5282 PALIMPORT int __cdecl vsprintf_s(char *, size_t, const char *, va_list);
5283 PALIMPORT char * __cdecl _gcvt_s(char *, int, double, int);
5284 PALIMPORT char * __cdecl _ecvt(double, int, int *, int *);
5285 PALIMPORT int __cdecl __iscsym(int);
5286 PALIMPORT unsigned char * __cdecl _mbsinc(const unsigned char *);
5287 PALIMPORT unsigned char * __cdecl _mbsninc(const unsigned char *, size_t);
5288 PALIMPORT unsigned char * __cdecl _mbsdec(const unsigned char *, const unsigned char *);
5289 PALIMPORT int __cdecl _wcsicmp(const WCHAR *, const WCHAR*);
5290 PALIMPORT int __cdecl _wcsnicmp(const WCHAR *, const WCHAR *, size_t);
5291 PALIMPORT int __cdecl _vsnprintf(char *, size_t, const char *, va_list);
5292 PALIMPORT int __cdecl _vsnprintf_s(char *, size_t, size_t, const char *, va_list);
5293 PALIMPORT int __cdecl _vsnwprintf_s(WCHAR *, size_t, size_t, const WCHAR *, va_list);
5294 PALIMPORT int __cdecl _snwprintf_s(WCHAR *, size_t, size_t, const WCHAR *, ...);
5295 PALIMPORT int __cdecl _snprintf_s(char *, size_t, size_t, const char *, ...);
5296 PALIMPORT int __cdecl sprintf_s(char *, size_t, const char *, ... );
5297 PALIMPORT int __cdecl swprintf_s(WCHAR *, size_t, const WCHAR *, ... );
5298 PALIMPORT int __cdecl _snwprintf_s(WCHAR *, size_t, size_t, const WCHAR *, ...);
5299 PALIMPORT int __cdecl vswprintf_s( WCHAR *, size_t, const WCHAR *, va_list);
5300 PALIMPORT int __cdecl sscanf_s(const char *, const char *, ...);
5301 PALIMPORT errno_t __cdecl _itow_s(int, WCHAR *, size_t, int);
5302
5303 PALIMPORT size_t __cdecl PAL_wcslen(const WCHAR *);
5304 PALIMPORT int __cdecl PAL_wcscmp(const WCHAR*, const WCHAR*);
5305 PALIMPORT int __cdecl PAL_wcsncmp(const WCHAR *, const WCHAR *, size_t);
5306 PALIMPORT WCHAR * __cdecl PAL_wcscat(WCHAR *, const WCHAR *);
5307 PALIMPORT WCHAR * __cdecl PAL_wcsncat(WCHAR *, const WCHAR *, size_t);
5308 PALIMPORT WCHAR * __cdecl PAL_wcscpy(WCHAR *, const WCHAR *);
5309 PALIMPORT WCHAR * __cdecl PAL_wcsncpy(WCHAR *, const WCHAR *, size_t);
5310 PALIMPORT const WCHAR * __cdecl PAL_wcschr(const WCHAR *, WCHAR);
5311 PALIMPORT const WCHAR * __cdecl PAL_wcsrchr(const WCHAR *, WCHAR);
5312 PALIMPORT WCHAR _WConst_return * __cdecl PAL_wcspbrk(const WCHAR *, const WCHAR *);
5313 PALIMPORT WCHAR _WConst_return * __cdecl PAL_wcsstr(const WCHAR *, const WCHAR *);
5314 PALIMPORT WCHAR * __cdecl PAL_wcstok(WCHAR *, const WCHAR *);
5315 PALIMPORT size_t __cdecl PAL_wcscspn(const WCHAR *, const WCHAR *);
5316 PALIMPORT int __cdecl PAL_swprintf(WCHAR *, const WCHAR *, ...);
5317 PALIMPORT int __cdecl PAL_vswprintf(WCHAR *, const WCHAR *, va_list);
5318 PALIMPORT int __cdecl PAL_swscanf(const WCHAR *, const WCHAR *, ...);
5319 PALIMPORT LONG __cdecl PAL_wcstol(const WCHAR *, WCHAR **, int);
5320 PALIMPORT ULONG __cdecl PAL_wcstoul(const WCHAR *, WCHAR **, int);
5321 PALIMPORT size_t __cdecl PAL_wcsspn (const WCHAR *, const WCHAR *);
5322 PALIMPORT double __cdecl PAL_wcstod(const WCHAR *, WCHAR **);
5323 PALIMPORT int __cdecl PAL_iswalpha(WCHAR);
5324 PALIMPORT int __cdecl PAL_iswprint(WCHAR);
5325 PALIMPORT int __cdecl PAL_iswupper(WCHAR);
5326 PALIMPORT int __cdecl PAL_iswspace(WCHAR);
5327 PALIMPORT int __cdecl PAL_iswdigit(WCHAR);
5328 PALIMPORT int __cdecl PAL_iswxdigit(WCHAR);
5329 PALIMPORT WCHAR __cdecl PAL_towlower(WCHAR);
5330 PALIMPORT WCHAR __cdecl PAL_towupper(WCHAR);
5331
5332 PALIMPORT WCHAR * __cdecl _wcslwr(WCHAR *);
5333 PALIMPORT ULONGLONG _wcstoui64(const WCHAR *, WCHAR **, int);
5334 PALIMPORT errno_t __cdecl _i64tow_s(long long, WCHAR *, size_t, int);
5335 PALIMPORT int __cdecl _wtoi(const WCHAR *);
5336
5337 #ifdef __cplusplus
5338 extern "C++" {
5339 inline WCHAR *PAL_wcschr(WCHAR *_S, WCHAR _C)
5340         {return ((WCHAR *)PAL_wcschr((const WCHAR *)_S, _C)); }
5341 inline WCHAR *PAL_wcsrchr(WCHAR *_S, WCHAR _C)
5342         {return ((WCHAR *)PAL_wcsrchr((const WCHAR *)_S, _C)); }
5343 inline WCHAR *PAL_wcspbrk(WCHAR *_S, const WCHAR *_P)
5344         {return ((WCHAR *)PAL_wcspbrk((const WCHAR *)_S, _P)); }
5345 inline WCHAR *PAL_wcsstr(WCHAR *_S, const WCHAR *_P)
5346         {return ((WCHAR *)PAL_wcsstr((const WCHAR *)_S, _P)); }
5347 }
5348 #endif
5349
5350 #if !__has_builtin(_rotl)
5351 /*++
5352 Function:
5353 _rotl
5354
5355 See MSDN doc.
5356 --*/
5357 EXTERN_C
5358 PALIMPORT
5359 inline
5360 unsigned int __cdecl _rotl(unsigned int value, int shift)
5361 {
5362     unsigned int retval = 0;
5363
5364     shift &= 0x1f;
5365     retval = (value << shift) | (value >> (sizeof(int) * CHAR_BIT - shift));
5366     return retval;
5367 }
5368 #endif // !__has_builtin(_rotl)
5369
5370 // On 64 bit unix, make the long an int.
5371 #ifdef BIT64
5372 #define _lrotl _rotl
5373 #endif // BIT64
5374
5375 #if !__has_builtin(_rotr)
5376
5377 /*++
5378 Function:
5379 _rotr
5380
5381 See MSDN doc.
5382 --*/
5383 EXTERN_C
5384 PALIMPORT
5385 inline
5386 unsigned int __cdecl _rotr(unsigned int value, int shift)
5387 {
5388     unsigned int retval;
5389
5390     shift &= 0x1f;
5391     retval = (value >> shift) | (value << (sizeof(int) * CHAR_BIT - shift));
5392     return retval;
5393 }
5394
5395 #endif // !__has_builtin(_rotr)
5396
5397 PALIMPORT int __cdecl abs(int);
5398 #ifndef PAL_STDCPP_COMPAT
5399 PALIMPORT LONG __cdecl labs(LONG);
5400 #endif // !PAL_STDCPP_COMPAT
5401 // clang complains if this is declared with __int64
5402 PALIMPORT long long __cdecl llabs(long long);
5403
5404 PALIMPORT int __cdecl _finite(double);
5405 PALIMPORT int __cdecl _isnan(double);
5406 PALIMPORT double __cdecl _copysign(double, double);
5407 PALIMPORT double __cdecl acos(double);
5408 PALIMPORT double __cdecl asin(double);
5409 PALIMPORT double __cdecl atan(double);
5410 PALIMPORT double __cdecl atan2(double, double);
5411 PALIMPORT double __cdecl ceil(double);
5412 PALIMPORT double __cdecl cos(double);
5413 PALIMPORT double __cdecl cosh(double);
5414 PALIMPORT double __cdecl exp(double);
5415 PALIMPORT double __cdecl fabs(double);
5416 PALIMPORT double __cdecl floor(double);
5417 PALIMPORT double __cdecl fmod(double, double); 
5418 PALIMPORT double __cdecl log(double);
5419 PALIMPORT double __cdecl log10(double);
5420 PALIMPORT double __cdecl modf(double, double*);
5421 PALIMPORT double __cdecl pow(double, double);
5422 PALIMPORT double __cdecl sin(double);
5423 PALIMPORT double __cdecl sinh(double);
5424 PALIMPORT double __cdecl sqrt(double);
5425 PALIMPORT double __cdecl tan(double);
5426 PALIMPORT double __cdecl tanh(double);
5427
5428 PALIMPORT int __cdecl _finitef(float);
5429 PALIMPORT int __cdecl _isnanf(float);
5430 PALIMPORT float __cdecl _copysignf(float, float);
5431 PALIMPORT float __cdecl acosf(float);
5432 PALIMPORT float __cdecl asinf(float);
5433 PALIMPORT float __cdecl atanf(float);
5434 PALIMPORT float __cdecl atan2f(float, float);
5435 PALIMPORT float __cdecl ceilf(float);
5436 PALIMPORT float __cdecl cosf(float);
5437 PALIMPORT float __cdecl coshf(float);
5438 PALIMPORT float __cdecl expf(float);
5439 PALIMPORT float __cdecl fabsf(float);
5440 PALIMPORT float __cdecl floorf(float);
5441 PALIMPORT float __cdecl fmodf(float, float); 
5442 PALIMPORT float __cdecl logf(float);
5443 PALIMPORT float __cdecl log10f(float);
5444 PALIMPORT float __cdecl modff(float, float*);
5445 PALIMPORT float __cdecl powf(float, float);
5446 PALIMPORT float __cdecl sinf(float);
5447 PALIMPORT float __cdecl sinhf(float);
5448 PALIMPORT float __cdecl sqrtf(float);
5449 PALIMPORT float __cdecl tanf(float);
5450 PALIMPORT float __cdecl tanhf(float);
5451
5452 #ifndef PAL_STDCPP_COMPAT
5453
5454 #ifdef __cplusplus
5455 extern "C++" {
5456
5457 inline __int64 abs(__int64 _X) {
5458     return llabs(_X);
5459 }
5460
5461 }
5462 #endif
5463
5464 PALIMPORT void * __cdecl malloc(size_t);
5465 PALIMPORT void   __cdecl free(void *);
5466 PALIMPORT void * __cdecl realloc(void *, size_t);
5467 PALIMPORT char * __cdecl _strdup(const char *);
5468
5469 #if defined(_MSC_VER)
5470 #define alloca _alloca
5471 #else
5472 #define _alloca alloca
5473 #endif //_MSC_VER
5474
5475 #define alloca  __builtin_alloca
5476
5477 #define max(a, b) (((a) > (b)) ? (a) : (b))
5478 #define min(a, b) (((a) < (b)) ? (a) : (b))
5479
5480 #endif // !PAL_STDCPP_COMPAT
5481
5482 PALIMPORT PAL_NORETURN void __cdecl exit(int);
5483 int __cdecl atexit(void (__cdecl *function)(void));
5484
5485 PALIMPORT void __cdecl qsort(void *, size_t, size_t, int (__cdecl *)(const void *, const void *));
5486 PALIMPORT void * __cdecl bsearch(const void *, const void *, size_t, size_t,
5487 int (__cdecl *)(const void *, const void *));
5488
5489 PALIMPORT char * __cdecl _fullpath(char *, const char *, size_t);
5490
5491 #ifndef PAL_STDCPP_COMPAT
5492 PALIMPORT time_t __cdecl time(time_t *);
5493
5494 struct tm {
5495         int tm_sec;     /* seconds after the minute - [0,59] */
5496         int tm_min;     /* minutes after the hour - [0,59] */
5497         int tm_hour;    /* hours since midnight - [0,23] */
5498         int tm_mday;    /* day of the month - [1,31] */
5499         int tm_mon;     /* months since January - [0,11] */
5500         int tm_year;    /* years since 1900 */
5501         int tm_wday;    /* days since Sunday - [0,6] */
5502         int tm_yday;    /* days since January 1 - [0,365] */
5503         int tm_isdst;   /* daylight savings time flag */
5504         };
5505
5506 PALIMPORT struct tm * __cdecl localtime(const time_t *);
5507 PALIMPORT time_t __cdecl mktime(struct tm *);
5508 PALIMPORT char * __cdecl ctime(const time_t *);
5509 #endif // !PAL_STDCPP_COMPAT
5510
5511 PALIMPORT int __cdecl _open_osfhandle(INT_PTR, int);
5512 PALIMPORT int __cdecl _close(int);
5513 PALIMPORT int __cdecl _flushall();
5514
5515 #ifdef PAL_STDCPP_COMPAT
5516
5517 struct _PAL_FILE;
5518 typedef struct _PAL_FILE PAL_FILE;
5519
5520 #else // PAL_STDCPP_COMPAT
5521
5522 struct _FILE;
5523 typedef struct _FILE FILE;
5524 typedef struct _FILE PAL_FILE;
5525
5526 #define SEEK_SET    0
5527 #define SEEK_CUR    1
5528 #define SEEK_END    2
5529
5530 /* Locale categories */
5531 #define LC_ALL          0
5532 #define LC_COLLATE      1
5533 #define LC_CTYPE        2
5534 #define LC_MONETARY     3
5535 #define LC_NUMERIC      4
5536 #define LC_TIME         5
5537
5538 #define _IOFBF  0       /* setvbuf should set fully buffered */
5539 #define _IOLBF  1       /* setvbuf should set line buffered */
5540 #define _IONBF  2       /* setvbuf should set unbuffered */
5541
5542 #endif // PAL_STDCPP_COMPAT
5543
5544 PALIMPORT int __cdecl PAL_fclose(PAL_FILE *);
5545 PALIMPORT void __cdecl PAL_setbuf(PAL_FILE *, char*);
5546 PALIMPORT int __cdecl PAL_fflush(PAL_FILE *);
5547 PALIMPORT size_t __cdecl PAL_fwrite(const void *, size_t, size_t, PAL_FILE *);
5548 PALIMPORT size_t __cdecl PAL_fread(void *, size_t, size_t, PAL_FILE *);
5549 PALIMPORT char * __cdecl PAL_fgets(char *, int, PAL_FILE *);
5550 PALIMPORT int __cdecl PAL_fputs(const char *, PAL_FILE *);
5551 PALIMPORT int __cdecl PAL_fputc(int c, PAL_FILE *stream);
5552 PALIMPORT int __cdecl PAL_putchar(int c);
5553 PALIMPORT int __cdecl PAL_fprintf(PAL_FILE *, const char *, ...);
5554 PALIMPORT int __cdecl PAL_vfprintf(PAL_FILE *, const char *, va_list);
5555 PALIMPORT int __cdecl PAL_fseek(PAL_FILE *, LONG, int);
5556 PALIMPORT LONG __cdecl PAL_ftell(PAL_FILE *);
5557 PALIMPORT int __cdecl PAL_feof(PAL_FILE *);
5558 PALIMPORT int __cdecl PAL_ferror(PAL_FILE *);
5559 PALIMPORT PAL_FILE * __cdecl PAL_fopen(const char *, const char *);
5560 PALIMPORT int __cdecl PAL_getc(PAL_FILE *stream);
5561 PALIMPORT int __cdecl PAL_fgetc(PAL_FILE *stream);
5562 PALIMPORT int __cdecl PAL_ungetc(int c, PAL_FILE *stream);
5563 PALIMPORT int __cdecl PAL_setvbuf(PAL_FILE *stream, char *, int, size_t);
5564 PALIMPORT WCHAR * __cdecl PAL_fgetws(WCHAR *, int, PAL_FILE *);
5565 PALIMPORT int __cdecl PAL_fwprintf(PAL_FILE *, const WCHAR *, ...);
5566 PALIMPORT int __cdecl PAL_vfwprintf(PAL_FILE *, const WCHAR *, va_list);
5567 PALIMPORT int __cdecl PAL_wprintf(const WCHAR*, ...);
5568
5569 PALIMPORT int __cdecl _getw(PAL_FILE *);
5570 PALIMPORT int __cdecl _putw(int, PAL_FILE *);
5571 PALIMPORT PAL_FILE * __cdecl _fdopen(int, const char *);
5572 PALIMPORT PAL_FILE * __cdecl _wfopen(const WCHAR *, const WCHAR *);
5573 PALIMPORT PAL_FILE * __cdecl _wfsopen(const WCHAR *, const WCHAR *, int);
5574
5575 /* Maximum value that can be returned by the rand function. */
5576
5577 #ifndef PAL_STDCPP_COMPAT
5578 #define RAND_MAX 0x7fff
5579 #endif // !PAL_STDCPP_COMPAT
5580
5581 PALIMPORT int __cdecl rand(void);
5582 PALIMPORT void __cdecl srand(unsigned int);
5583
5584 PALIMPORT int __cdecl printf(const char *, ...);
5585 PALIMPORT int __cdecl vprintf(const char *, va_list);
5586
5587 #ifdef _MSC_VER
5588 #define PAL_get_caller _MSC_VER
5589 #else
5590 #define PAL_get_caller 0
5591 #endif
5592
5593 PALIMPORT PAL_FILE * __cdecl PAL_get_stdout(int caller);
5594 PALIMPORT PAL_FILE * __cdecl PAL_get_stdin(int caller);
5595 PALIMPORT PAL_FILE * __cdecl PAL_get_stderr(int caller);
5596 PALIMPORT int * __cdecl PAL_errno(int caller);
5597
5598 #ifdef PAL_STDCPP_COMPAT
5599 #define PAL_stdout (PAL_get_stdout(PAL_get_caller))
5600 #define PAL_stdin  (PAL_get_stdin(PAL_get_caller))
5601 #define PAL_stderr (PAL_get_stderr(PAL_get_caller))
5602 #define PAL_errno   (*PAL_errno(PAL_get_caller))
5603 #else // PAL_STDCPP_COMPAT
5604 #define stdout (PAL_get_stdout(PAL_get_caller))
5605 #define stdin  (PAL_get_stdin(PAL_get_caller))
5606 #define stderr (PAL_get_stderr(PAL_get_caller))
5607 #define errno  (*PAL_errno(PAL_get_caller))
5608 #endif // PAL_STDCPP_COMPAT
5609
5610 PALIMPORT char * __cdecl getenv(const char *);
5611 PALIMPORT int __cdecl _putenv(const char *);
5612
5613 #define ERANGE          34
5614
5615 /******************* PAL-specific I/O completion port *****************/
5616
5617 typedef struct _PAL_IOCP_CPU_INFORMATION {
5618     union {
5619         FILETIME ftLastRecordedIdleTime;
5620         FILETIME ftLastRecordedCurrentTime;
5621     } LastRecordedTime;
5622     FILETIME ftLastRecordedKernelTime;
5623     FILETIME ftLastRecordedUserTime;
5624 } PAL_IOCP_CPU_INFORMATION;
5625
5626 PALIMPORT
5627 INT
5628 PALAPI
5629 PAL_GetCPUBusyTime(
5630     IN OUT PAL_IOCP_CPU_INFORMATION *lpPrevCPUInfo);
5631
5632 /****************PAL Perf functions for PInvoke*********************/
5633 #if PAL_PERF
5634 PALIMPORT
5635 VOID
5636 PALAPI
5637 PAL_EnableProcessProfile(VOID);
5638
5639 PALIMPORT
5640 VOID
5641 PALAPI
5642 PAL_DisableProcessProfile(VOID);
5643
5644 PALIMPORT
5645 BOOL
5646 PALAPI
5647 PAL_IsProcessProfileEnabled(VOID);
5648
5649 PALIMPORT
5650 INT64
5651 PALAPI
5652 PAL_GetCpuTickCount(VOID);
5653 #endif // PAL_PERF
5654
5655 /******************* PAL functions for SIMD extensions *****************/
5656
5657 PALIMPORT
5658 unsigned int _mm_getcsr(void);
5659
5660 PALIMPORT
5661 void _mm_setcsr(unsigned int i);
5662
5663 /******************* PAL side-by-side support  ************************/
5664
5665 #ifdef FEATURE_PAL_SXS
5666 //
5667 // Some versions of the PAL support several PALs side-by-side
5668 // in the process.  To avoid those PALs interfering with one
5669 // another, they need to be told by clients when they are active
5670 // and when they are not.
5671 //
5672
5673 // To avoid performance problems incurred by swapping thread
5674 // exception ports every time we leave the PAL, there's also
5675 // the concept of entering/leaving the PAL at its top boundary
5676 // (entering down/leaving up) or at the bottom boundary
5677 // (leaving down/entering up).
5678
5679 typedef enum _PAL_Boundary {
5680     PAL_BoundaryTop,            // closer to main()
5681     PAL_BoundaryBottom,         // closer to execution
5682     PAL_BoundaryEH,             // out-of-band during EH
5683     
5684     PAL_BoundaryMax = PAL_BoundaryEH
5685 } PAL_Boundary;
5686
5687 // This function needs to be called on a thread when it enters
5688 // a region of code that depends on this instance of the PAL
5689 // in the process, and the current thread may or may not be
5690 // known to the PAL.  This function can fail (for something else
5691 // than an internal error) if this is the first time that the
5692 // current thread entered this PAL.  Note that PAL_Initialize
5693 // implies a call to this function.  Does not modify LastError.
5694 PALIMPORT
5695 DWORD
5696 PALAPI
5697 PAL_Enter(PAL_Boundary boundary);
5698
5699 // Returns TRUE if we this thread has already entered the PAL,
5700 // returns FALSE if we have not entered the PAL.
5701 PALIMPORT
5702 BOOL
5703 PALAPI
5704 PAL_HasEntered(VOID);
5705
5706 // Equivalent to PAL_Enter(PAL_BoundaryTop) and is for stub
5707 // code generation use.
5708 PALIMPORT
5709 DWORD
5710 PALAPI
5711 PAL_EnterTop(VOID);
5712
5713 // This function needs to be called on a thread when it enters
5714 // a region of code that depends on this instance of the PAL
5715 // in the process, and the current thread is already known to
5716 // the PAL.  Does not modify LastError.
5717 PALIMPORT
5718 VOID
5719 PALAPI
5720 PAL_Reenter(PAL_Boundary boundary);
5721
5722 // This function needs to be called on a thread when it enters
5723 // a region of code that depends on this instance of the PAL
5724 // in the process, and it is unknown whether the current thread
5725 // is already running in the PAL.  Returns TRUE if and only if
5726 // the thread was not running in the PAL previously.  Does not
5727 // modify LastError.
5728 PALIMPORT
5729 BOOL
5730 PALAPI
5731 PAL_ReenterForEH(VOID);
5732
5733 // This function needs to be called on a thread when it leaves
5734 // a region of code that depends on this instance of the PAL
5735 // in the process.  Does not modify LastError.
5736 PALIMPORT
5737 VOID
5738 PALAPI
5739 PAL_Leave(PAL_Boundary boundary);
5740
5741 // This function is equivalent to PAL_Leave(PAL_BoundaryBottom)
5742 // and is available to limit the creation of stub code.
5743 PALIMPORT
5744 VOID
5745 PALAPI
5746 PAL_LeaveBottom(VOID);
5747
5748 // This function is equivalent to PAL_Leave(PAL_BoundaryTop)
5749 // and is available to limit the creation of stub code.
5750 PALIMPORT
5751 VOID
5752 PALAPI
5753 PAL_LeaveTop(VOID);
5754
5755 #ifdef  __cplusplus
5756 //
5757 // A holder to enter the PAL for a specific region of code.
5758 // Previously, we must have been executing outside the PAL
5759 // (unless fEnter is set to FALSE).
5760 //
5761 class PAL_EnterHolder
5762 {
5763 private:
5764     BOOL m_fEntered;
5765     DWORD m_palError;
5766 public:
5767     PAL_EnterHolder(BOOL fEnter = TRUE) : m_palError(ERROR_SUCCESS)
5768     {
5769         if (fEnter)
5770         {
5771             m_palError = PAL_Enter(PAL_BoundaryTop);
5772             m_fEntered = m_palError == ERROR_SUCCESS;
5773         }
5774         else
5775         {
5776             m_fEntered = FALSE;
5777         }
5778     }
5779
5780     ~PAL_EnterHolder()
5781     {
5782         if (m_fEntered)
5783         {
5784             PAL_Leave(PAL_BoundaryTop);
5785         }
5786     }
5787
5788     DWORD GetError()
5789     {
5790         return m_palError;
5791     }
5792     
5793     void SuppressRelease()
5794     {
5795         // Used to avoid calling PAL_Leave() when 
5796         // another code path will explicitly do so.
5797         m_fEntered = FALSE;
5798     }
5799 };
5800
5801 class PAL_LeaveHolder
5802 {
5803 public:
5804     PAL_LeaveHolder()
5805     {
5806         PAL_Leave(PAL_BoundaryBottom);
5807     }
5808
5809     ~PAL_LeaveHolder()
5810     {
5811         PAL_Reenter(PAL_BoundaryBottom);
5812     }
5813 };
5814 #endif // __cplusplus
5815
5816 #else // FEATURE_PAL_SXS
5817
5818 #define PAL_Enter(boundary) ERROR_SUCCESS
5819 #define PAL_Reenter(boundary)
5820 #define PAL_Leave(boundary)
5821
5822 #ifdef __cplusplus
5823 class PAL_EnterHolder {
5824 public:
5825     // using constructor to suppress the "unused variable" warnings
5826     PAL_EnterHolder() {}
5827 };
5828 class PAL_LeaveHolder {
5829 public:
5830     // using constructor to suppress the "unused variable" warnings
5831     PAL_LeaveHolder() {}
5832 };
5833 #endif // __cplusplus
5834
5835 #endif // FEATURE_PAL_SXS
5836
5837 #ifdef __cplusplus
5838
5839 #include "pal_unwind.h"
5840
5841 PALIMPORT
5842 VOID
5843 PALAPI
5844 PAL_FreeExceptionRecords(
5845   IN EXCEPTION_RECORD *exceptionRecord, 
5846   IN CONTEXT *contextRecord);
5847
5848 #define EXCEPTION_CONTINUE_SEARCH   0
5849 #define EXCEPTION_EXECUTE_HANDLER   1
5850 #define EXCEPTION_CONTINUE_EXECUTION -1
5851
5852 struct PAL_SEHException
5853 {
5854 private:
5855     static const SIZE_T NoTargetFrameSp = SIZE_MAX;
5856
5857     void Move(PAL_SEHException& ex)
5858     {
5859         ExceptionPointers.ExceptionRecord = ex.ExceptionPointers.ExceptionRecord;
5860         ExceptionPointers.ContextRecord = ex.ExceptionPointers.ContextRecord;
5861         TargetFrameSp = ex.TargetFrameSp;
5862
5863         ex.Clear();
5864     }
5865
5866     void FreeRecords()
5867     {
5868         if (ExceptionPointers.ExceptionRecord != NULL)
5869         {
5870             PAL_FreeExceptionRecords(ExceptionPointers.ExceptionRecord, ExceptionPointers.ContextRecord);
5871             ExceptionPointers.ExceptionRecord = NULL;
5872             ExceptionPointers.ContextRecord = NULL;
5873         }
5874     }
5875
5876 public:
5877     EXCEPTION_POINTERS ExceptionPointers;
5878     // Target frame stack pointer set before the 2nd pass.
5879     SIZE_T TargetFrameSp;
5880
5881     PAL_SEHException(EXCEPTION_RECORD *pExceptionRecord, CONTEXT *pContextRecord)
5882     {
5883         ExceptionPointers.ExceptionRecord = pExceptionRecord;
5884         ExceptionPointers.ContextRecord = pContextRecord;
5885         TargetFrameSp = NoTargetFrameSp;
5886     }
5887
5888     PAL_SEHException()
5889     {
5890         Clear();
5891     }    
5892
5893     // The copy constructor and copy assignment operators are deleted so that the PAL_SEHException
5894     // can never be copied, only moved. This enables simple lifetime management of the exception and
5895     // context records, since there is always just one PAL_SEHException instance referring to the same records.
5896     PAL_SEHException(const PAL_SEHException& ex) = delete;
5897     PAL_SEHException& operator=(const PAL_SEHException& ex) = delete;
5898
5899     PAL_SEHException(PAL_SEHException&& ex)
5900     {
5901         Move(ex);
5902     }    
5903
5904     PAL_SEHException& operator=(PAL_SEHException&& ex)
5905     {
5906         FreeRecords();
5907         Move(ex);
5908         return *this;
5909     }
5910
5911     ~PAL_SEHException()
5912     {
5913         FreeRecords();
5914     }
5915
5916     void Clear()
5917     {
5918         ExceptionPointers.ExceptionRecord = NULL;
5919         ExceptionPointers.ContextRecord = NULL;
5920         TargetFrameSp = NoTargetFrameSp;
5921     }
5922
5923     CONTEXT* GetContextRecord()
5924     {
5925         return ExceptionPointers.ContextRecord;
5926     }
5927
5928     EXCEPTION_RECORD* GetExceptionRecord()
5929     {
5930         return ExceptionPointers.ExceptionRecord;
5931     }
5932
5933     bool IsFirstPass()
5934     {
5935         return (TargetFrameSp == NoTargetFrameSp);
5936     }
5937
5938     void SecondPassDone()
5939     {
5940         TargetFrameSp = NoTargetFrameSp;
5941     }
5942 };
5943
5944 typedef BOOL (*PHARDWARE_EXCEPTION_HANDLER)(PAL_SEHException* ex);
5945 typedef BOOL (*PHARDWARE_EXCEPTION_SAFETY_CHECK_FUNCTION)(PCONTEXT contextRecord, PEXCEPTION_RECORD exceptionRecord);
5946 typedef VOID (*PTERMINATION_REQUEST_HANDLER)();
5947 typedef DWORD (*PGET_GCMARKER_EXCEPTION_CODE)(LPVOID ip);
5948
5949 PALIMPORT
5950 VOID
5951 PALAPI
5952 PAL_SetHardwareExceptionHandler(
5953     IN PHARDWARE_EXCEPTION_HANDLER exceptionHandler,
5954     IN PHARDWARE_EXCEPTION_SAFETY_CHECK_FUNCTION exceptionCheckFunction);
5955
5956 PALIMPORT
5957 VOID
5958 PALAPI
5959 PAL_SetGetGcMarkerExceptionCode(
5960     IN PGET_GCMARKER_EXCEPTION_CODE getGcMarkerExceptionCode);
5961
5962 PALIMPORT
5963 VOID
5964 PALAPI
5965 PAL_ThrowExceptionFromContext(
5966     IN CONTEXT* context,
5967     IN PAL_SEHException* ex);
5968
5969 PALIMPORT
5970 VOID
5971 PALAPI
5972 PAL_SetTerminationRequestHandler(
5973     IN PTERMINATION_REQUEST_HANDLER terminationRequestHandler);
5974
5975 //
5976 // This holder is used to indicate that a hardware
5977 // exception should be raised as a C++ exception
5978 // to better emulate SEH on the xplat platforms.
5979 //
5980 class CatchHardwareExceptionHolder
5981 {
5982 public:
5983     CatchHardwareExceptionHolder();
5984
5985     ~CatchHardwareExceptionHolder();
5986
5987     static bool IsEnabled();
5988 };
5989
5990 //
5991 // NOTE: Catching hardware exceptions are only enabled in the DAC and SOS 
5992 // builds. A hardware exception in coreclr code will fail fast/terminate
5993 // the process.
5994 //
5995 #ifdef FEATURE_ENABLE_HARDWARE_EXCEPTIONS
5996 #define HardwareExceptionHolder CatchHardwareExceptionHolder __catchHardwareException;
5997 #else
5998 #define HardwareExceptionHolder
5999 #endif // FEATURE_ENABLE_HARDWARE_EXCEPTIONS
6000
6001 #ifdef FEATURE_PAL_SXS
6002
6003 extern "C++" {
6004
6005 //
6006 // This is the base class of native exception holder used to provide 
6007 // the filter function to the exception dispatcher. This allows the
6008 // filter to be called during the first pass to better emulate SEH
6009 // the xplat platforms that only have C++ exception support.
6010 //
6011 class NativeExceptionHolderBase
6012 {
6013     // Save the address of the holder head so the destructor 
6014     // doesn't have access the slow (on Linux) TLS value again.
6015     NativeExceptionHolderBase **m_head;
6016
6017     // The next holder on the stack
6018     NativeExceptionHolderBase *m_next;
6019
6020 protected:
6021     NativeExceptionHolderBase();
6022
6023     ~NativeExceptionHolderBase();
6024
6025 public:
6026     // Calls the holder's filter handler.
6027     virtual EXCEPTION_DISPOSITION InvokeFilter(PAL_SEHException& ex) = 0;
6028
6029     // Adds the holder to the "stack" of holders. This is done explicitly instead
6030     // of in the constructor was to avoid the mess of move constructors combined
6031     // with return value optimization (in CreateHolder).
6032     void Push();
6033
6034     // Given the currentHolder and locals stack range find the next holder starting with this one
6035     // To find the first holder, pass nullptr as the currentHolder.
6036     static NativeExceptionHolderBase *FindNextHolder(NativeExceptionHolderBase *currentHolder, void *frameLowAddress, void *frameHighAddress);
6037 };
6038
6039 //
6040 // This is the second part of the native exception filter holder. It is
6041 // templated because the lambda used to wrap the exception filter is a
6042 // unknown type. 
6043 //
6044 template<class FilterType>
6045 class NativeExceptionHolder : public NativeExceptionHolderBase
6046 {
6047     FilterType* m_exceptionFilter;
6048
6049 public:
6050     NativeExceptionHolder(FilterType* exceptionFilter)
6051         : NativeExceptionHolderBase()
6052     {
6053         m_exceptionFilter = exceptionFilter;
6054     }
6055
6056     virtual EXCEPTION_DISPOSITION InvokeFilter(PAL_SEHException& ex)
6057     {
6058         return (*m_exceptionFilter)(ex);
6059     }
6060 };
6061
6062 //
6063 // This is a native exception holder that is used when the catch catches
6064 // all exceptions.
6065 //
6066 class NativeExceptionHolderCatchAll : public NativeExceptionHolderBase
6067 {
6068
6069 public:
6070     NativeExceptionHolderCatchAll()
6071         : NativeExceptionHolderBase()
6072     {
6073     }
6074
6075     virtual EXCEPTION_DISPOSITION InvokeFilter(PAL_SEHException& ex)
6076     {
6077         return EXCEPTION_EXECUTE_HANDLER;
6078     }
6079 };
6080
6081 // This is a native exception holder that doesn't catch any exceptions.
6082 class NativeExceptionHolderNoCatch : public NativeExceptionHolderBase
6083 {
6084
6085 public:
6086     NativeExceptionHolderNoCatch()
6087         : NativeExceptionHolderBase()
6088     {
6089     }
6090
6091     virtual EXCEPTION_DISPOSITION InvokeFilter(PAL_SEHException& ex)
6092     {
6093         return EXCEPTION_CONTINUE_SEARCH;
6094     }
6095 };
6096
6097 //
6098 // This factory class for the native exception holder is necessary because
6099 // templated functions don't need the explicit type parameter and can infer
6100 // the template type from the parameter.
6101 //
6102 class NativeExceptionHolderFactory
6103 {
6104 public:
6105     template<class FilterType>
6106     static NativeExceptionHolder<FilterType> CreateHolder(FilterType* exceptionFilter)
6107     {
6108         return NativeExceptionHolder<FilterType>(exceptionFilter);
6109     }
6110 };
6111
6112 // Start of a try block for exceptions raised by RaiseException
6113 #define PAL_TRY(__ParamType, __paramDef, __paramRef)                            \
6114 {                                                                               \
6115     __ParamType __param = __paramRef;                                           \
6116     auto tryBlock = [](__ParamType __paramDef)                                  \
6117     {
6118
6119 // Start of an exception handler. If an exception raised by the RaiseException 
6120 // occurs in the try block and the disposition is EXCEPTION_EXECUTE_HANDLER, 
6121 // the handler code is executed. If the disposition is EXCEPTION_CONTINUE_SEARCH,
6122 // the exception is rethrown. The EXCEPTION_CONTINUE_EXECUTION disposition is
6123 // not supported.
6124 #define PAL_EXCEPT(dispositionExpression)                                       \
6125     };                                                                          \
6126     const bool isFinally = false;                                               \
6127     auto finallyBlock = []() {};                                                \
6128     EXCEPTION_DISPOSITION disposition = EXCEPTION_CONTINUE_EXECUTION;           \
6129     auto exceptionFilter = [&disposition, &__param](PAL_SEHException& ex)       \
6130     {                                                                           \
6131         disposition = dispositionExpression;                                    \
6132         _ASSERTE(disposition != EXCEPTION_CONTINUE_EXECUTION);                  \
6133         return disposition;                                                     \
6134     };                                                                          \
6135     try                                                                         \
6136     {                                                                           \
6137         HardwareExceptionHolder                                                 \
6138         auto __exceptionHolder = NativeExceptionHolderFactory::CreateHolder(&exceptionFilter); \
6139         __exceptionHolder.Push();                                               \
6140         tryBlock(__param);                                                      \
6141     }                                                                           \
6142     catch (PAL_SEHException& ex)                                                \
6143     {                                                                           \
6144         if (disposition == EXCEPTION_CONTINUE_EXECUTION)                        \
6145         {                                                                       \
6146             exceptionFilter(ex);                                                \
6147         }                                                                       \
6148         if (disposition == EXCEPTION_CONTINUE_SEARCH)                           \
6149         {                                                                       \
6150             throw;                                                              \
6151         }                                                                       \
6152         ex.SecondPassDone();
6153
6154 // Start of an exception handler. It works the same way as the PAL_EXCEPT except
6155 // that the disposition is obtained by calling the specified filter.
6156 #define PAL_EXCEPT_FILTER(filter) PAL_EXCEPT(filter(&ex.ExceptionPointers, __param))
6157
6158 // Start of a finally block. The finally block is executed both when the try block
6159 // finishes or when an exception is raised using the RaiseException in it.
6160 #define PAL_FINALLY                     \
6161     };                                  \
6162     const bool isFinally = true;        \
6163     auto finallyBlock = [&]()           \
6164     {                       
6165
6166 // End of an except or a finally block.
6167 #define PAL_ENDTRY                      \
6168     };                                  \
6169     if (isFinally)                      \
6170     {                                   \
6171         try                             \
6172         {                               \
6173             tryBlock(__param);          \
6174         }                               \
6175         catch (...)                     \
6176         {                               \
6177             finallyBlock();             \
6178             throw;                      \
6179         }                               \
6180         finallyBlock();                 \
6181     }                                   \
6182 }
6183
6184 } // extern "C++"
6185
6186 #endif // FEATURE_PAL_SXS
6187
6188 #define PAL_CPP_THROW(type, obj) { throw obj; }
6189 #define PAL_CPP_RETHROW { throw; }
6190 #define PAL_CPP_TRY                     try { HardwareExceptionHolder
6191 #define PAL_CPP_CATCH_EXCEPTION(ident)  } catch (Exception *ident) { PAL_Reenter(PAL_BoundaryBottom);
6192 #define PAL_CPP_CATCH_EXCEPTION_NOARG   } catch (Exception *) { PAL_Reenter(PAL_BoundaryBottom);
6193 #define PAL_CPP_CATCH_DERIVED(type, ident) } catch (type *ident) { PAL_Reenter(PAL_BoundaryBottom);
6194 #define PAL_CPP_CATCH_ALL               } catch (...) {                                           \
6195                                             PAL_Reenter(PAL_BoundaryBottom);                      \
6196                                             try { throw; }                                        \
6197                                             catch (PAL_SEHException& ex) { ex.SecondPassDone(); } \
6198                                             catch (...) {}
6199
6200 #define PAL_CPP_ENDTRY                  }
6201
6202 #ifdef _MSC_VER
6203 #pragma warning(disable:4611) // interaction between '_setjmp' and C++ object destruction is non-portable
6204 #endif
6205
6206 #ifdef FEATURE_PAL_SXS
6207
6208 #define PAL_TRY_FOR_DLLMAIN(ParamType, paramDef, paramRef, _reason) PAL_TRY(ParamType, paramDef, paramRef)
6209
6210 #else // FEATURE_PAL_SXS
6211
6212 #define PAL_TRY(ParamType, paramDef, paramRef)                          \
6213     {                                                                   \
6214         ParamType __param = paramRef;                                   \
6215         ParamType paramDef; paramDef = __param;                         \
6216         try {                                                           \
6217             HardwareExceptionHolder
6218
6219 #define PAL_TRY_FOR_DLLMAIN(ParamType, paramDef, paramRef, _reason)     \
6220     {                                                                   \
6221         ParamType __param = paramRef;                                   \
6222         ParamType paramDef; paramDef = __param;                         \
6223         try {                                                           \
6224             HardwareExceptionHolder
6225
6226 #define PAL_ENDTRY                                                      \
6227         }                                                               \
6228     }
6229
6230 #endif // FEATURE_PAL_SXS
6231
6232 #endif // __cplusplus
6233
6234 // Platform-specific library naming
6235 // 
6236 #ifdef __APPLE__
6237 #define MAKEDLLNAME_W(name) u"lib" name u".dylib"
6238 #define MAKEDLLNAME_A(name)  "lib" name  ".dylib"
6239 #else
6240 #define MAKEDLLNAME_W(name) u"lib" name u".so"
6241 #define MAKEDLLNAME_A(name)  "lib" name  ".so"
6242 #endif
6243
6244 #ifdef UNICODE
6245 #define MAKEDLLNAME(x) MAKEDLLNAME_W(x)
6246 #else
6247 #define MAKEDLLNAME(x) MAKEDLLNAME_A(x)
6248 #endif
6249
6250 #define PAL_SHLIB_PREFIX "lib"
6251
6252 #if __APPLE__
6253 #define PAL_SHLIB_SUFFIX ".dylib"
6254 #else
6255 #define PAL_SHLIB_SUFFIX ".so"
6256 #endif
6257
6258 #define DBG_EXCEPTION_HANDLED            ((DWORD   )0x00010001L)    
6259 #define DBG_CONTINUE                     ((DWORD   )0x00010002L)    
6260 #define DBG_EXCEPTION_NOT_HANDLED        ((DWORD   )0x80010001L)    
6261
6262 #define DBG_TERMINATE_THREAD             ((DWORD   )0x40010003L)    
6263 #define DBG_TERMINATE_PROCESS            ((DWORD   )0x40010004L)    
6264 #define DBG_CONTROL_C                    ((DWORD   )0x40010005L)    
6265 #define DBG_RIPEXCEPTION                 ((DWORD   )0x40010007L)    
6266 #define DBG_CONTROL_BREAK                ((DWORD   )0x40010008L)    
6267 #define DBG_COMMAND_EXCEPTION            ((DWORD   )0x40010009L)    
6268
6269 #define STATUS_USER_APC                  ((DWORD   )0x000000C0L)
6270 #define STATUS_GUARD_PAGE_VIOLATION      ((DWORD   )0x80000001L)
6271 #define STATUS_DATATYPE_MISALIGNMENT     ((DWORD   )0x80000002L)
6272 #define STATUS_BREAKPOINT                ((DWORD   )0x80000003L)
6273 #define STATUS_SINGLE_STEP               ((DWORD   )0x80000004L)
6274 #define STATUS_LONGJUMP                  ((DWORD   )0x80000026L)
6275 #define STATUS_UNWIND_CONSOLIDATE        ((DWORD   )0x80000029L)
6276 #define STATUS_ACCESS_VIOLATION          ((DWORD   )0xC0000005L)
6277 #define STATUS_IN_PAGE_ERROR             ((DWORD   )0xC0000006L)
6278 #define STATUS_INVALID_HANDLE            ((DWORD   )0xC0000008L)
6279 #define STATUS_NO_MEMORY                 ((DWORD   )0xC0000017L)
6280 #define STATUS_ILLEGAL_INSTRUCTION       ((DWORD   )0xC000001DL)
6281 #define STATUS_NONCONTINUABLE_EXCEPTION  ((DWORD   )0xC0000025L)
6282 #define STATUS_INVALID_DISPOSITION       ((DWORD   )0xC0000026L)
6283 #define STATUS_ARRAY_BOUNDS_EXCEEDED     ((DWORD   )0xC000008CL)
6284 #define STATUS_FLOAT_DENORMAL_OPERAND    ((DWORD   )0xC000008DL)
6285 #define STATUS_FLOAT_DIVIDE_BY_ZERO      ((DWORD   )0xC000008EL)
6286 #define STATUS_FLOAT_INEXACT_RESULT      ((DWORD   )0xC000008FL)
6287 #define STATUS_FLOAT_INVALID_OPERATION   ((DWORD   )0xC0000090L)
6288 #define STATUS_FLOAT_OVERFLOW            ((DWORD   )0xC0000091L)
6289 #define STATUS_FLOAT_STACK_CHECK         ((DWORD   )0xC0000092L)
6290 #define STATUS_FLOAT_UNDERFLOW           ((DWORD   )0xC0000093L)
6291 #define STATUS_INTEGER_DIVIDE_BY_ZERO    ((DWORD   )0xC0000094L)
6292 #define STATUS_INTEGER_OVERFLOW          ((DWORD   )0xC0000095L)
6293 #define STATUS_PRIVILEGED_INSTRUCTION    ((DWORD   )0xC0000096L)
6294 #define STATUS_STACK_OVERFLOW            ((DWORD   )0xC00000FDL)
6295 #define STATUS_CONTROL_C_EXIT            ((DWORD   )0xC000013AL)
6296
6297 #define WAIT_IO_COMPLETION                  STATUS_USER_APC
6298
6299 #define EXCEPTION_ACCESS_VIOLATION          STATUS_ACCESS_VIOLATION
6300 #define EXCEPTION_DATATYPE_MISALIGNMENT     STATUS_DATATYPE_MISALIGNMENT
6301 #define EXCEPTION_BREAKPOINT                STATUS_BREAKPOINT
6302 #define EXCEPTION_SINGLE_STEP               STATUS_SINGLE_STEP
6303 #define EXCEPTION_ARRAY_BOUNDS_EXCEEDED     STATUS_ARRAY_BOUNDS_EXCEEDED
6304 #define EXCEPTION_FLT_DENORMAL_OPERAND      STATUS_FLOAT_DENORMAL_OPERAND
6305 #define EXCEPTION_FLT_DIVIDE_BY_ZERO        STATUS_FLOAT_DIVIDE_BY_ZERO
6306 #define EXCEPTION_FLT_INEXACT_RESULT        STATUS_FLOAT_INEXACT_RESULT
6307 #define EXCEPTION_FLT_INVALID_OPERATION     STATUS_FLOAT_INVALID_OPERATION
6308 #define EXCEPTION_FLT_OVERFLOW              STATUS_FLOAT_OVERFLOW
6309 #define EXCEPTION_FLT_STACK_CHECK           STATUS_FLOAT_STACK_CHECK
6310 #define EXCEPTION_FLT_UNDERFLOW             STATUS_FLOAT_UNDERFLOW
6311 #define EXCEPTION_INT_DIVIDE_BY_ZERO        STATUS_INTEGER_DIVIDE_BY_ZERO
6312 #define EXCEPTION_INT_OVERFLOW              STATUS_INTEGER_OVERFLOW
6313 #define EXCEPTION_PRIV_INSTRUCTION          STATUS_PRIVILEGED_INSTRUCTION
6314 #define EXCEPTION_IN_PAGE_ERROR             STATUS_IN_PAGE_ERROR
6315 #define EXCEPTION_ILLEGAL_INSTRUCTION       STATUS_ILLEGAL_INSTRUCTION
6316 #define EXCEPTION_NONCONTINUABLE_EXCEPTION  STATUS_NONCONTINUABLE_EXCEPTION
6317 #define EXCEPTION_STACK_OVERFLOW            STATUS_STACK_OVERFLOW
6318 #define EXCEPTION_INVALID_DISPOSITION       STATUS_INVALID_DISPOSITION
6319 #define EXCEPTION_GUARD_PAGE                STATUS_GUARD_PAGE_VIOLATION
6320 #define EXCEPTION_INVALID_HANDLE            STATUS_INVALID_HANDLE
6321
6322 #define CONTROL_C_EXIT                      STATUS_CONTROL_C_EXIT
6323
6324 /*  These are from the <FCNTL.H> file in windows.
6325     They are needed for _open_osfhandle.*/
6326 #define _O_RDONLY   0x0000
6327 #define _O_APPEND   0x0008
6328 #define _O_TEXT     0x4000
6329 #define _O_BINARY   0x8000
6330
6331 #ifdef  __cplusplus
6332 }
6333 #endif
6334
6335 #endif // __PAL_H__