| 1 | /* $Id: kLdrDyldOS.c 2883 2006-11-18 11:21:33Z bird $ */ | 
|---|
| 2 | /** @file | 
|---|
| 3 | * | 
|---|
| 4 | * kLdr - The Dynamic Loader, OS specific operations. | 
|---|
| 5 | * | 
|---|
| 6 | * Copyright (c) 2006 knut st. osmundsen <bird@anduin.net> | 
|---|
| 7 | * | 
|---|
| 8 | * | 
|---|
| 9 | * This file is part of kLdr. | 
|---|
| 10 | * | 
|---|
| 11 | * kLdr is free software; you can redistribute it and/or modify | 
|---|
| 12 | * it under the terms of the GNU General Public License as published by | 
|---|
| 13 | * the Free Software Foundation; either version 2 of the License, or | 
|---|
| 14 | * (at your option) any later version. | 
|---|
| 15 | * | 
|---|
| 16 | * kLdr is distributed in the hope that it will be useful, | 
|---|
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 19 | * GNU General Public License for more details. | 
|---|
| 20 | * | 
|---|
| 21 | * You should have received a copy of the GNU General Public License | 
|---|
| 22 | * along with kLdr; if not, write to the Free Software | 
|---|
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA | 
|---|
| 24 | * | 
|---|
| 25 | */ | 
|---|
| 26 |  | 
|---|
| 27 |  | 
|---|
| 28 | /******************************************************************************* | 
|---|
| 29 | *   Header Files                                                               * | 
|---|
| 30 | *******************************************************************************/ | 
|---|
| 31 | #ifdef __OS2__ | 
|---|
| 32 | # define INCL_BASE | 
|---|
| 33 | # define INCL_ERRORS | 
|---|
| 34 | # include <os2.h> | 
|---|
| 35 | #elif defined(__WIN__) | 
|---|
| 36 | # include <Windows.h> | 
|---|
| 37 | #else | 
|---|
| 38 | # error "port me" | 
|---|
| 39 | #endif | 
|---|
| 40 |  | 
|---|
| 41 | #include <kLdr.h> | 
|---|
| 42 | #include "kLdrHlp.h" | 
|---|
| 43 | #include "kLdrInternal.h" | 
|---|
| 44 |  | 
|---|
| 45 |  | 
|---|
| 46 | /** | 
|---|
| 47 | * Allocates a stack. | 
|---|
| 48 | * | 
|---|
| 49 | * @returns Pointer to the stack. NULL on allocation failure (assumes out of memory). | 
|---|
| 50 | * @param   cb      The size of the stack. This shall be page aligned. | 
|---|
| 51 | *                  If 0, a OS specific default stack size will be employed. | 
|---|
| 52 | */ | 
|---|
| 53 | void *kldrDyldOSAllocStack(size_t cb) | 
|---|
| 54 | { | 
|---|
| 55 | #ifdef __OS2__ | 
|---|
| 56 | APIRET rc; | 
|---|
| 57 | PVOID pv; | 
|---|
| 58 |  | 
|---|
| 59 | if (!cb) | 
|---|
| 60 | cb = 1 * 1024*1024; /* 1MB */ | 
|---|
| 61 |  | 
|---|
| 62 | rc = DosAllocMem(&pv, cb, OBJ_TILE | PAG_COMMIT | PAG_WRITE | PAG_READ); | 
|---|
| 63 | if (rc == NO_ERROR) | 
|---|
| 64 | return pv; | 
|---|
| 65 | return NULL; | 
|---|
| 66 |  | 
|---|
| 67 | #elif defined(__WIN__) | 
|---|
| 68 |  | 
|---|
| 69 | if (!cb) | 
|---|
| 70 | cb = 1 *1024*1024; /* 1MB */ | 
|---|
| 71 |  | 
|---|
| 72 | return VirtualAlloc(NULL, cb, MEM_COMMIT, PAGE_READWRITE); | 
|---|
| 73 |  | 
|---|
| 74 | #else | 
|---|
| 75 | # error "Port me" | 
|---|
| 76 | #endif | 
|---|
| 77 | } | 
|---|
| 78 |  | 
|---|
| 79 |  | 
|---|
| 80 | /** | 
|---|
| 81 | * Invokes the main executable entry point with whatever | 
|---|
| 82 | * parameters specific to the host OS and/or module format. | 
|---|
| 83 | * | 
|---|
| 84 | * @returns | 
|---|
| 85 | * @param   uMainEPAddress  The address of the main entry point. | 
|---|
| 86 | * @param   pvStack         Pointer to the stack object. | 
|---|
| 87 | * @param   cbStack         The size of the stack object. | 
|---|
| 88 | */ | 
|---|
| 89 | int kldrDyldOSStartExe(uintptr_t uMainEPAddress, void *pvStack, size_t cbStack) | 
|---|
| 90 | { | 
|---|
| 91 | #if defined(__OS2__) | 
|---|
| 92 |  | 
|---|
| 93 | #elif defined(__WIN__) | 
|---|
| 94 | /* | 
|---|
| 95 | * Invoke the entrypoint on the current stack for now. | 
|---|
| 96 | * Deal with other formats and stack switching another day. | 
|---|
| 97 | */ | 
|---|
| 98 | int rc; | 
|---|
| 99 | int (*pfnEP)(void); | 
|---|
| 100 | pfnEP = (int (*)(void))uMainEPAddress; | 
|---|
| 101 |  | 
|---|
| 102 | rc = pfnEP(); | 
|---|
| 103 |  | 
|---|
| 104 | TerminateProcess(GetCurrentProcess(), rc); | 
|---|
| 105 | kldrHlpAssert(!"TerminateProcess failed"); | 
|---|
| 106 | for (;;) | 
|---|
| 107 | TerminateProcess(GetCurrentProcess(), rc); | 
|---|
| 108 |  | 
|---|
| 109 | /*#elif defined(__NT__)*/ | 
|---|
| 110 | #else | 
|---|
| 111 | # error "Port me" | 
|---|
| 112 | #endif | 
|---|
| 113 |  | 
|---|
| 114 | return -1; | 
|---|
| 115 | } | 
|---|
| 116 |  | 
|---|
| 117 |  | 
|---|
| 118 | void kldrDyldDoLoadExeStackSwitch(PKLDRDYLDMOD pExe, void *pvStack, size_t cbStack) | 
|---|
| 119 | { | 
|---|
| 120 | /*kldrHlpAssert(!"not implemented");*/ | 
|---|
| 121 |  | 
|---|
| 122 | /** @todo implement this properly! */ | 
|---|
| 123 | kldrDyldDoLoadExe(pExe); | 
|---|
| 124 | } | 
|---|