tstPin.cpp revision a466f24ecf94b453750f9791ad1140556114711b
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync/** @file
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync *
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync * VBox host drivers - Ring-0 support drivers - Testcases:
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync * Test the memory locking interface
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync */
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync/*
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync * Copyright (C) 2006 InnoTek Systemberatung GmbH
452fd0f33b11dc60aad994e3001c74415179d401vboxsync *
452fd0f33b11dc60aad994e3001c74415179d401vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
452fd0f33b11dc60aad994e3001c74415179d401vboxsync * available from http://www.virtualbox.org. This file is free software;
452fd0f33b11dc60aad994e3001c74415179d401vboxsync * you can redistribute it and/or modify it under the terms of the GNU
452fd0f33b11dc60aad994e3001c74415179d401vboxsync * General Public License as published by the Free Software Foundation,
452fd0f33b11dc60aad994e3001c74415179d401vboxsync * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
452fd0f33b11dc60aad994e3001c74415179d401vboxsync * distribution. VirtualBox OSE is distributed in the hope that it will
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync * be useful, but WITHOUT ANY WARRANTY of any kind.
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync *
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync * If you received this file as part of a commercial VirtualBox
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync * distribution, then only the terms of your commercial VirtualBox
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync * license agreement apply instead of the previous paragraph.
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync */
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync/*******************************************************************************
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync* Header Files *
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync*******************************************************************************/
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync#include <VBox/sup.h>
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync#include <VBox/param.h>
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync#include <iprt/runtime.h>
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync#include <iprt/stream.h>
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync#include <stdlib.h>
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync#include <string.h>
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsyncint main(int argc, char **argv)
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync{
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync int rc;
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync int rcRet = 0;
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync RTHCPHYS HCPhys;
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync void *pv;
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync RTR3Init(true, ~0);
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync rc = SUPInit(NULL, ~0);
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync RTPrintf("SUPInit -> rc=%d\n", rc);
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync rcRet += rc != 0;
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync if (!rc)
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync {
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync static struct
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync {
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync void *pv;
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync void *pvAligned;
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync SUPPAGE aPages[16];
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync } aPinnings[500];
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync for (unsigned i = 0; i < sizeof(aPinnings) / sizeof(aPinnings[0]); i++)
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync {
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync#ifdef __OS2__
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync aPinnings[i].pv = NULL;
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync SUPPageAlloc(0x10000 >> PAGE_SHIFT, &aPinnings[i].pv);
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync#else
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync aPinnings[i].pv = malloc(0x10000);
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync#endif
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync aPinnings[i].pvAligned = ALIGNP(aPinnings[i].pv, PAGE_SIZE);
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync rc = SUPPageLock(aPinnings[i].pvAligned, 0xf000 >> PAGE_SHIFT, &aPinnings[i].aPages[0]);
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync if (!rc)
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync {
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync RTPrintf("i=%d: pvAligned=%p pv=%p:\n", i, aPinnings[i].pvAligned, aPinnings[i].pv);
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync memset(aPinnings[i].pv, 0xfa, 0x10000);
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync unsigned c4GPluss = 0;
214ccb21af5d26cadb50ca7a9ecf9a85dfb2cf57vboxsync for (unsigned j = 0; j < (0xf000 >> PAGE_SHIFT); j++)
6378fc9dbcca8bb2bdd10916afbc30702193f178vboxsync if (aPinnings[i].aPages[j].Phys >= _4G)
6378fc9dbcca8bb2bdd10916afbc30702193f178vboxsync {
6378fc9dbcca8bb2bdd10916afbc30702193f178vboxsync RTPrintf("%2d: vrt=%p phys=%VHp\n", j, (char *)aPinnings[i].pvAligned + (j << PAGE_SHIFT), aPinnings[i].aPages[j].Phys);
6378fc9dbcca8bb2bdd10916afbc30702193f178vboxsync c4GPluss++;
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync }
929372c3867cf188f069a440f4aed85837af022evboxsync RTPrintf("i=%d: c4GPluss=%d\n", i, c4GPluss);
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync }
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync else
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync {
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync RTPrintf("SUPPageLock -> rc=%d\n", rc);
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync rcRet++;
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync#ifdef __OS2__
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync SUPPageFree(aPinnings[i].pv, 0x10000 >> PAGE_SHIFT);
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync#else
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync free(aPinnings[i].pv);
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync#endif
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync aPinnings[i].pv = aPinnings[i].pvAligned = NULL;
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync break;
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync }
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync }
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync for (unsigned i = 0; i < sizeof(aPinnings) / sizeof(aPinnings[0]); i += 2)
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync {
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync if (aPinnings[i].pvAligned)
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync {
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync rc = SUPPageUnlock(aPinnings[i].pvAligned);
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync if (rc)
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync {
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync RTPrintf("SUPPageUnlock(%p) -> rc=%d\n", aPinnings[i].pvAligned, rc);
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync rcRet++;
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync }
6728a36898fd2be125a28e84d2115d19aa4923edvboxsync memset(aPinnings[i].pv, 0xaf, 0x10000);
6728a36898fd2be125a28e84d2115d19aa4923edvboxsync }
6728a36898fd2be125a28e84d2115d19aa4923edvboxsync }
6728a36898fd2be125a28e84d2115d19aa4923edvboxsync
6728a36898fd2be125a28e84d2115d19aa4923edvboxsync for (unsigned i = 0; i < sizeof(aPinnings) / sizeof(aPinnings[0]); i += 2)
6728a36898fd2be125a28e84d2115d19aa4923edvboxsync {
6728a36898fd2be125a28e84d2115d19aa4923edvboxsync if (aPinnings[i].pv)
6728a36898fd2be125a28e84d2115d19aa4923edvboxsync {
6728a36898fd2be125a28e84d2115d19aa4923edvboxsync memset(aPinnings[i].pv, 0xcc, 0x10000);
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync#ifdef __OS2__
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync SUPPageFree(aPinnings[i].pv, 0x10000 >> PAGE_SHIFT);
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync#else
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync free(aPinnings[i].pv);
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync#endif
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync aPinnings[i].pv = NULL;
214ccb21af5d26cadb50ca7a9ecf9a85dfb2cf57vboxsync }
214ccb21af5d26cadb50ca7a9ecf9a85dfb2cf57vboxsync }
214ccb21af5d26cadb50ca7a9ecf9a85dfb2cf57vboxsync
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync /*
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync * Allocate a bit of contiguous memory.
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync */
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync pv = SUPContAlloc(RT_ALIGN_Z(15003, PAGE_SIZE) >> PAGE_SHIFT, &HCPhys);
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync rcRet += pv == NULL || HCPhys == 0;
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync if (pv && HCPhys)
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync {
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync RTPrintf("SUPContAlloc(15003) -> HCPhys=%llx pv=%p\n", HCPhys, pv);
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync void *pv0 = pv;
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync memset(pv0, 0xaf, 15003);
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync pv = SUPContAlloc(RT_ALIGN_Z(12999, PAGE_SIZE) >> PAGE_SHIFT, &HCPhys);
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync rcRet += pv == NULL || HCPhys == 0;
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync if (pv && HCPhys)
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync {
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync RTPrintf("SUPContAlloc(12999) -> HCPhys=%llx pv=%p\n", HCPhys, pv);
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync memset(pv, 0xbf, 12999);
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync rc = SUPContFree(pv, RT_ALIGN_Z(12999, PAGE_SIZE) >> PAGE_SHIFT);
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync rcRet += rc != 0;
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync if (rc)
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync RTPrintf("SUPContFree failed! rc=%d\n", rc);
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync }
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync else
0b80b22020c2d2c55a20d2a73023f17214e5d6bavboxsync RTPrintf("SUPContAlloc (2nd) failed!\n");
0b80b22020c2d2c55a20d2a73023f17214e5d6bavboxsync memset(pv0, 0xaf, 15003);
0b80b22020c2d2c55a20d2a73023f17214e5d6bavboxsync /* pv0 is intentionally not freed! */
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync }
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync else
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync RTPrintf("SUPContAlloc failed!\n");
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync /*
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync * Allocate a big chunk of virtual memory and then lock it.
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync */
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync #define BIG_SIZE 72*1024*1024
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync #define BIG_SIZEPP (BIG_SIZE + PAGE_SIZE)
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync#ifdef __OS2__
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync pv = NULL;
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync SUPPageAlloc(BIG_SIZEPP >> PAGE_SHIFT, &pv);
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync#else
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync pv = malloc(BIG_SIZEPP);
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync#endif
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync if (pv)
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync {
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync static SUPPAGE aPages[BIG_SIZE >> PAGE_SHIFT];
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync void *pvAligned = RT_ALIGN_P(pv, PAGE_SIZE);
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync rc = SUPPageLock(pvAligned, BIG_SIZE >> PAGE_SHIFT, &aPages[0]);
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync if (!rc)
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync {
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync /* dump */
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync RTPrintf("SUPPageLock(%p,%d,) succeeded!\n", pvAligned, BIG_SIZE);
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync memset(pv, 0x42, BIG_SIZEPP);
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync #if 0
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync for (unsigned j = 0; j < (BIG_SIZE >> PAGE_SHIFT); j++)
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync RTPrintf("%2d: vrt=%p phys=%08x\n", j, (char *)pvAligned + (j << PAGE_SHIFT), (uintptr_t)aPages[j].pvPhys);
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync #endif
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync /* unlock */
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync rc = SUPPageUnlock(pvAligned);
0b80b22020c2d2c55a20d2a73023f17214e5d6bavboxsync if (rc)
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync {
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync RTPrintf("SUPPageUnlock(%p) -> rc=%d\n", pvAligned, rc);
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync rcRet++;
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync }
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync memset(pv, 0xcc, BIG_SIZEPP);
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync }
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync else
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync {
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync RTPrintf("SUPPageLock(%p) -> rc=%d\n", pvAligned, rc);
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync rcRet++;
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync }
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync#ifdef __OS2__
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync SUPPageFree(pv, BIG_SIZEPP >> PAGE_SHIFT);
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync#else
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync free(pv);
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync#endif
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync }
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync rc = SUPTerm();
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync RTPrintf("SUPTerm -> rc=%d\n", rc);
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync rcRet += rc != 0;
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync }
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync return rcRet;
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync}
febf3f1de573e25fb134b8453a22b0732b4c52e2vboxsync