[2944] | 1 | /* $Id: kHlpPage.c 3608 2007-10-29 00:49:05Z bird $ */
|
---|
| 2 | /** @file
|
---|
[3573] | 3 | * kHlp - Generic Page Memory Functions.
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[3608] | 7 | * Copyright (c) 2006-2007 knut st. osmundsen <bird-kStuff-spam@anduin.net>
|
---|
[2944] | 8 | *
|
---|
[3573] | 9 | * This file is part of kStuff.
|
---|
[2944] | 10 | *
|
---|
[3573] | 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.
|
---|
[2944] | 15 | *
|
---|
[3608] | 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 | *
|
---|
[3573] | 22 | * kStuff is distributed in the hope that it will be useful,
|
---|
[2944] | 23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
[3573] | 24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 25 | * Lesser General Public License for more details.
|
---|
[2944] | 26 | *
|
---|
[3573] | 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
|
---|
[3608] | 29 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
---|
| 30 | * 02110-1301, USA
|
---|
[2944] | 31 | */
|
---|
| 32 |
|
---|
| 33 | /*******************************************************************************
|
---|
| 34 | * Header Files *
|
---|
| 35 | *******************************************************************************/
|
---|
[3573] | 36 | #include <k/kHlpAlloc.h>
|
---|
[3575] | 37 | #include <k/kHlpAssert.h>
|
---|
[3573] | 38 |
|
---|
[3599] | 39 | #if K_OS == K_OS_DARWIN \
|
---|
| 40 | || K_OS == K_OS_FREEBSD \
|
---|
| 41 | || K_OS == K_OS_LINUX \
|
---|
| 42 | || K_OS == K_OS_NETBSD \
|
---|
| 43 | || K_OS == K_OS_OPENBSD \
|
---|
| 44 | || K_OS == K_OS_SOLARIS
|
---|
| 45 | # include <k/kHlpSys.h>
|
---|
[3594] | 46 | # include <sys/mman.h>
|
---|
| 47 |
|
---|
| 48 | #elif K_OS == K_OS_OS2
|
---|
[2944] | 49 | # define INCL_BASE
|
---|
| 50 | # define INCL_ERRORS
|
---|
| 51 | # include <os2.h>
|
---|
[3594] | 52 |
|
---|
[3573] | 53 | #elif K_OS == K_OS_WINDOWS
|
---|
[2944] | 54 | # include <Windows.h>
|
---|
[3594] | 55 |
|
---|
[2944] | 56 | #else
|
---|
| 57 | # error "port me"
|
---|
| 58 | #endif
|
---|
| 59 |
|
---|
| 60 |
|
---|
| 61 | /*******************************************************************************
|
---|
| 62 | * Global Variables *
|
---|
| 63 | *******************************************************************************/
|
---|
[3594] | 64 | #if K_OS == K_OS_DARWIN \
|
---|
| 65 | || K_OS == K_OS_FREEBSD \
|
---|
| 66 | || K_OS == K_OS_LINUX \
|
---|
| 67 | || K_OS == K_OS_NETBSD \
|
---|
| 68 | || K_OS == K_OS_OPENBSD \
|
---|
| 69 | || K_OS == K_OS_SOLARIS
|
---|
| 70 | /* nothing */
|
---|
| 71 | #elif K_OS == K_OS_OS2
|
---|
[3575] | 72 | /** The base of the loader stub object. <kLdr Hack>
|
---|
[2944] | 73 | * The OS/2 exe stub consists of a single data object. When allocating memory
|
---|
[3575] | 74 | * for an executable, we'll have to reuse this. */
|
---|
[2944] | 75 | static void *g_pvStub = NULL;
|
---|
[3575] | 76 | /** The size of the stub object - 0 if no stub. <kLdr Hack> */
|
---|
[3567] | 77 | static KSIZE g_cbStub = 0;
|
---|
[2944] | 78 |
|
---|
[3573] | 79 | #elif K_OS == K_OS_WINDOWS
|
---|
[2944] | 80 | /** The system info. */
|
---|
| 81 | static SYSTEM_INFO g_SystemInfo;
|
---|
| 82 | #else
|
---|
| 83 | # error "port me"
|
---|
| 84 | #endif
|
---|
| 85 |
|
---|
| 86 |
|
---|
| 87 |
|
---|
[3594] | 88 | #if K_OS == K_OS_DARWIN \
|
---|
| 89 | || K_OS == K_OS_FREEBSD \
|
---|
| 90 | || K_OS == K_OS_LINUX \
|
---|
| 91 | || K_OS == K_OS_NETBSD \
|
---|
| 92 | || K_OS == K_OS_OPENBSD \
|
---|
| 93 | || K_OS == K_OS_SOLARIS
|
---|
| 94 | static int kHlpPageProtToNative(KPROT enmProt)
|
---|
| 95 | {
|
---|
| 96 | switch (enmProt)
|
---|
| 97 | {
|
---|
| 98 | case KPROT_NOACCESS: return PROT_NONE;
|
---|
| 99 | case KPROT_READONLY: return PROT_READ;
|
---|
| 100 | case KPROT_READWRITE: return PROT_READ | PROT_WRITE;
|
---|
| 101 | case KPROT_EXECUTE: return PROT_EXEC;
|
---|
| 102 | case KPROT_EXECUTE_READ: return PROT_EXEC | PROT_READ;
|
---|
| 103 | case KPROT_EXECUTE_READWRITE: return PROT_EXEC | PROT_READ | PROT_WRITE;
|
---|
| 104 | default:
|
---|
| 105 | kHlpAssert(0);
|
---|
| 106 | return ~0U;
|
---|
| 107 | }
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | #elif K_OS == K_OS_OS2
|
---|
[3573] | 111 | static ULONG kHlpPageProtToNative(KPROT enmProt)
|
---|
[2944] | 112 | {
|
---|
| 113 | switch (enmProt)
|
---|
| 114 | {
|
---|
[3571] | 115 | case KPROT_NOACCESS: return PAG_EXECUTE | PAG_READ | PAG_WRITE;
|
---|
| 116 | case KPROT_READONLY: return PAG_COMMIT | PAG_READ;
|
---|
| 117 | case KPROT_READWRITE: return PAG_COMMIT | PAG_READ | PAG_WRITE;
|
---|
| 118 | case KPROT_EXECUTE: return PAG_COMMIT | PAG_EXECUTE;
|
---|
| 119 | case KPROT_EXECUTE_READ: return PAG_COMMIT | PAG_EXECUTE | PAG_READ;
|
---|
| 120 | case KPROT_EXECUTE_READWRITE: return PAG_COMMIT | PAG_EXECUTE | PAG_READ | PAG_WRITE;
|
---|
[2944] | 121 | default:
|
---|
[3573] | 122 | kHlpAssert(0);
|
---|
[2944] | 123 | return ~0U;
|
---|
| 124 | }
|
---|
| 125 | }
|
---|
[3573] | 126 | #elif K_OS == K_OS_WINDOWS
|
---|
| 127 | static DWORD kHlpPageProtToNative(KPROT enmProt)
|
---|
[2944] | 128 | {
|
---|
| 129 | switch (enmProt)
|
---|
| 130 | {
|
---|
[3571] | 131 | case KPROT_NOACCESS: return PAGE_NOACCESS;
|
---|
| 132 | case KPROT_READONLY: return PAGE_READONLY;
|
---|
| 133 | case KPROT_READWRITE: return PAGE_READWRITE;
|
---|
| 134 | case KPROT_EXECUTE: return PAGE_EXECUTE;
|
---|
| 135 | case KPROT_EXECUTE_READ: return PAGE_EXECUTE_READ;
|
---|
| 136 | case KPROT_EXECUTE_READWRITE: return PAGE_EXECUTE_READWRITE;
|
---|
[2944] | 137 | default:
|
---|
[3573] | 138 | kHlpAssert(0);
|
---|
[2944] | 139 | return ~0U;
|
---|
| 140 | }
|
---|
| 141 | }
|
---|
| 142 | #endif
|
---|
| 143 |
|
---|
| 144 |
|
---|
[3594] | 145 |
|
---|
[2944] | 146 | /**
|
---|
| 147 | * Allocate a chunk of memory with page granularity.
|
---|
| 148 | *
|
---|
| 149 | * @returns 0 on success, non-zero OS status code on failure.
|
---|
| 150 | * @param ppv Where to store the address of the allocated memory.
|
---|
| 151 | * If fFixed is set, *ppv will on entry contain the desired address (page aligned).
|
---|
| 152 | * @param cb Number of bytes. Page aligned.
|
---|
| 153 | * @param enmProt The new protection. Copy-on-write is invalid.
|
---|
| 154 | */
|
---|
[3573] | 155 | KHLP_DECL(int) kHlpPageAlloc(void **ppv, KSIZE cb, KPROT enmProt, KBOOL fFixed)
|
---|
[2944] | 156 | {
|
---|
[3599] | 157 | #if K_OS == K_OS_DARWIN \
|
---|
| 158 | || K_OS == K_OS_FREEBSD \
|
---|
| 159 | || K_OS == K_OS_LINUX \
|
---|
| 160 | || K_OS == K_OS_NETBSD \
|
---|
| 161 | || K_OS == K_OS_OPENBSD \
|
---|
| 162 | || K_OS == K_OS_SOLARIS
|
---|
[3594] | 163 | void *pv;
|
---|
| 164 |
|
---|
| 165 | pv = kHlpSys_mmap(fFixed ? *ppv : NULL, cb, kHlpPageProtToNative(enmProt),
|
---|
| 166 | fFixed ? MAP_FIXED | MAP_ANON: MAP_ANON, -1, 0);
|
---|
| 167 | if ((KIPTR)pv < 256)
|
---|
| 168 | {
|
---|
| 169 | kHlpAssert(0);
|
---|
| 170 | return (int)(KIPTR)pv; /** @todo convert errno to kErrors */
|
---|
| 171 | }
|
---|
| 172 | *ppv = pv;
|
---|
| 173 | return 0;
|
---|
| 174 |
|
---|
| 175 | #elif K_OS == K_OS_OS2
|
---|
[2944] | 176 | APIRET rc;
|
---|
[3594] | 177 | ULONG fFlags = kHlpPageProtToNative(enmProt);
|
---|
[2944] | 178 |
|
---|
| 179 | if (!fFixed)
|
---|
| 180 | {
|
---|
| 181 | /* simple */
|
---|
| 182 | rc = DosAllocMem(ppv, cb, fFlags | OBJ_ANY);
|
---|
| 183 | if (rc == ERROR_INVALID_PARAMETER)
|
---|
| 184 | rc = DosAllocMem(ppv, cb, fFlags);
|
---|
| 185 | }
|
---|
| 186 | else
|
---|
| 187 | {
|
---|
| 188 | /* not so simple. */
|
---|
| 189 | /** @todo I've got code for this in libc somewhere. */
|
---|
| 190 | rc = -1;
|
---|
| 191 | }
|
---|
| 192 | if (!rc)
|
---|
| 193 | return 0;
|
---|
[3573] | 194 | kHlpAssert(0);
|
---|
[2944] | 195 | return rc;
|
---|
| 196 |
|
---|
[3573] | 197 | #elif K_OS == K_OS_WINDOWS
|
---|
[2944] | 198 | /* (We don't have to care about the stub here, because the stub will be unmapped before we get here.) */
|
---|
| 199 | int rc;
|
---|
[3573] | 200 | DWORD fProt = kHlpPageProtToNative(enmProt);
|
---|
[2944] | 201 |
|
---|
| 202 | if (!g_SystemInfo.dwPageSize)
|
---|
| 203 | GetSystemInfo(&g_SystemInfo);
|
---|
| 204 |
|
---|
| 205 | *ppv = VirtualAlloc(fFixed ? *ppv : NULL, cb, MEM_COMMIT, fProt);
|
---|
| 206 | if (*ppv != NULL)
|
---|
| 207 | return 0;
|
---|
| 208 | rc = GetLastError();
|
---|
[3573] | 209 | kHlpAssert(0);
|
---|
[2944] | 210 | return rc;
|
---|
| 211 |
|
---|
| 212 | #else
|
---|
| 213 | # error "port me"
|
---|
| 214 | #endif
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 |
|
---|
| 218 | /**
|
---|
| 219 | * Change the protection of one or more pages in an allocation.
|
---|
| 220 | *
|
---|
[3573] | 221 | * (This will of course only work correctly on memory allocated by kHlpPageAlloc().)
|
---|
[2944] | 222 | *
|
---|
| 223 | * @returns 0 on success, non-zero OS status code on failure.
|
---|
| 224 | * @param pv First page. Page aligned.
|
---|
| 225 | * @param cb Number of bytes. Page aligned.
|
---|
| 226 | * @param enmProt The new protection. Copy-on-write is invalid.
|
---|
| 227 | */
|
---|
[3573] | 228 | KHLP_DECL(int) kHlpPageProtect(void *pv, KSIZE cb, KPROT enmProt)
|
---|
[2944] | 229 | {
|
---|
[3599] | 230 | #if K_OS == K_OS_DARWIN \
|
---|
| 231 | || K_OS == K_OS_FREEBSD \
|
---|
| 232 | || K_OS == K_OS_LINUX \
|
---|
| 233 | || K_OS == K_OS_NETBSD \
|
---|
| 234 | || K_OS == K_OS_OPENBSD \
|
---|
| 235 | || K_OS == K_OS_SOLARIS
|
---|
[3594] | 236 | int rc;
|
---|
| 237 |
|
---|
| 238 | rc = kHlpSys_mprotect(pv, cb, kHlpPageProtToNative(enmProt));
|
---|
| 239 | if (!rc)
|
---|
| 240 | return 0;
|
---|
| 241 | /** @todo convert errno -> kErrors */
|
---|
| 242 | kHlpAssert(0);
|
---|
| 243 | return rc;
|
---|
| 244 |
|
---|
| 245 | #elif K_OS == K_OS_OS2
|
---|
[2944] | 246 | APIRET rc;
|
---|
[3594] | 247 | ULONG fFlags = kHlpPageProtToNative(enmProt);
|
---|
[2944] | 248 |
|
---|
| 249 | /*
|
---|
| 250 | * The non-stub pages.
|
---|
| 251 | */
|
---|
| 252 | rc = DosSetMem(pv, cb, fFlags);
|
---|
| 253 | if (rc && fFlags != PAG_DECOMMIT)
|
---|
| 254 | rc = DosSetMem(pv, cb, fFlags | PAG_COMMIT);
|
---|
| 255 | if (rc)
|
---|
| 256 | {
|
---|
| 257 | /* Try page by page. */
|
---|
| 258 | while (cb > 0)
|
---|
| 259 | {
|
---|
| 260 | rc = DosSetMem(pv, 0x1000, fFlags);
|
---|
| 261 | if (rc && fFlags != PAG_DECOMMIT)
|
---|
| 262 | rc = DosSetMem(pv, 0x1000, fFlags | PAG_COMMIT);
|
---|
| 263 | if (rc)
|
---|
| 264 | return rc;
|
---|
[3567] | 265 | pv = (void *)((KUPTR)pv + 0x1000);
|
---|
[2944] | 266 | cb -= 0x1000;
|
---|
| 267 | }
|
---|
| 268 | }
|
---|
[3573] | 269 | kHlpAssert(!rc);
|
---|
[2944] | 270 | return rc;
|
---|
| 271 |
|
---|
[3573] | 272 | #elif K_OS == K_OS_WINDOWS
|
---|
[2944] | 273 | DWORD fOldProt = 0;
|
---|
[3573] | 274 | DWORD fProt = kHlpPageProtToNative(enmProt);
|
---|
[2944] | 275 | int rc = 0;
|
---|
| 276 |
|
---|
| 277 | if (!VirtualProtect(pv, cb, fProt, &fOldProt))
|
---|
| 278 | {
|
---|
| 279 | rc = GetLastError();
|
---|
[3573] | 280 | kHlpAssert(0);
|
---|
[2944] | 281 | }
|
---|
| 282 | return rc;
|
---|
| 283 | #else
|
---|
| 284 | # error "port me"
|
---|
| 285 | #endif
|
---|
| 286 | }
|
---|
| 287 |
|
---|
| 288 |
|
---|
| 289 | /**
|
---|
[3573] | 290 | * Free memory allocated by kHlpPageAlloc().
|
---|
[2944] | 291 | *
|
---|
| 292 | * @returns 0 on success, non-zero OS status code on failure.
|
---|
[3573] | 293 | * @param pv The address returned by kHlpPageAlloc().
|
---|
| 294 | * @param cb The byte count requested from kHlpPageAlloc().
|
---|
[2944] | 295 | */
|
---|
[3575] | 296 | KHLP_DECL(int) kHlpPageFree(void *pv, KSIZE cb)
|
---|
[2944] | 297 | {
|
---|
[3599] | 298 | #if K_OS == K_OS_DARWIN \
|
---|
| 299 | || K_OS == K_OS_FREEBSD \
|
---|
| 300 | || K_OS == K_OS_LINUX \
|
---|
| 301 | || K_OS == K_OS_NETBSD \
|
---|
| 302 | || K_OS == K_OS_OPENBSD \
|
---|
| 303 | || K_OS == K_OS_SOLARIS
|
---|
[3594] | 304 | int rc;
|
---|
| 305 |
|
---|
| 306 | rc = kHlpSys_munmap(pv, cb);
|
---|
| 307 | if (!rc)
|
---|
| 308 | return 0;
|
---|
| 309 | /** @todo convert errno -> kErrors */
|
---|
| 310 | return rc;
|
---|
| 311 |
|
---|
| 312 | #elif K_OS == K_OS_OS2
|
---|
[2944] | 313 | APIRET rc;
|
---|
| 314 |
|
---|
| 315 | /*
|
---|
| 316 | * Deal with any portion overlapping with the stub.
|
---|
| 317 | */
|
---|
[3567] | 318 | KUPTR offStub = (KUPTR)pv - (KUPTR)g_pvStub;
|
---|
[2944] | 319 | if (offStub < g_cbStub)
|
---|
| 320 | {
|
---|
| 321 | /* decommit the pages in the stub. */
|
---|
[3569] | 322 | KSIZE cbStub = K_MIN(g_cbStub - offStub, cb);
|
---|
[2944] | 323 | rc = DosSetMem(pv, cbStub, PAG_DECOMMIT);
|
---|
| 324 | if (rc)
|
---|
| 325 | {
|
---|
| 326 | /* Page by page, ignoring errors after the first success. */
|
---|
| 327 | while (cbStub > 0)
|
---|
| 328 | {
|
---|
| 329 | if (!DosSetMem(pv, 0x1000, PAG_DECOMMIT))
|
---|
| 330 | rc = 0;
|
---|
[3567] | 331 | pv = (void *)((KUPTR)pv + 0x1000);
|
---|
[2944] | 332 | cbStub -= 0x1000;
|
---|
| 333 | cb -= 0x1000;
|
---|
| 334 | }
|
---|
| 335 | if (rc)
|
---|
| 336 | {
|
---|
[3573] | 337 | kHlpAssert(!rc);
|
---|
[2944] | 338 | return rc;
|
---|
| 339 | }
|
---|
| 340 | }
|
---|
| 341 | else
|
---|
| 342 | {
|
---|
| 343 | cb -= cbStub;
|
---|
| 344 | if (!cb)
|
---|
| 345 | return 0;
|
---|
[3567] | 346 | pv = (void *)((KUPTR)pv + cbStub);
|
---|
[2944] | 347 | }
|
---|
| 348 | }
|
---|
| 349 |
|
---|
| 350 | /*
|
---|
| 351 | * Free the object.
|
---|
| 352 | */
|
---|
| 353 | rc = DosFreeMem(pv);
|
---|
[3573] | 354 | kHlpAssert(!rc);
|
---|
[2944] | 355 | return rc;
|
---|
| 356 |
|
---|
[3573] | 357 | #elif K_OS == K_OS_WINDOWS
|
---|
[2944] | 358 | /*
|
---|
| 359 | * Free the object.
|
---|
| 360 | */
|
---|
| 361 | int rc = 0;
|
---|
| 362 | if (!VirtualFree(pv, 0 /*cb*/, MEM_RELEASE))
|
---|
| 363 | {
|
---|
| 364 | rc = GetLastError();
|
---|
[3573] | 365 | kHlpAssert(0);
|
---|
[2944] | 366 | }
|
---|
| 367 | return rc;
|
---|
| 368 |
|
---|
| 369 | #else
|
---|
| 370 | # error "port me"
|
---|
| 371 | #endif
|
---|
| 372 | }
|
---|
| 373 |
|
---|