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