tstX86-1.cpp revision 78f04b74aefa851f2b51815a7260b47d5be8c776
65c72795ab90d3daefa759b716fbb5c6352c7a56vboxsync * X86 instruction set exploration/testcase #1.
65c72795ab90d3daefa759b716fbb5c6352c7a56vboxsync * Copyright (C) 2011 Oracle Corporation
65c72795ab90d3daefa759b716fbb5c6352c7a56vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
65c72795ab90d3daefa759b716fbb5c6352c7a56vboxsync * available from http://www.virtualbox.org. This file is free software;
65c72795ab90d3daefa759b716fbb5c6352c7a56vboxsync * you can redistribute it and/or modify it under the terms of the GNU
65c72795ab90d3daefa759b716fbb5c6352c7a56vboxsync * General Public License (GPL) as published by the Free Software
65c72795ab90d3daefa759b716fbb5c6352c7a56vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
65c72795ab90d3daefa759b716fbb5c6352c7a56vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
65c72795ab90d3daefa759b716fbb5c6352c7a56vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
65c72795ab90d3daefa759b716fbb5c6352c7a56vboxsync/*******************************************************************************
65c72795ab90d3daefa759b716fbb5c6352c7a56vboxsync* Header Files *
65c72795ab90d3daefa759b716fbb5c6352c7a56vboxsync*******************************************************************************/
65c72795ab90d3daefa759b716fbb5c6352c7a56vboxsync/*******************************************************************************
65c72795ab90d3daefa759b716fbb5c6352c7a56vboxsync* Structures and Typedefs *
65c72795ab90d3daefa759b716fbb5c6352c7a56vboxsync*******************************************************************************/
65c72795ab90d3daefa759b716fbb5c6352c7a56vboxsynctypedef struct TRAPINFO
65c72795ab90d3daefa759b716fbb5c6352c7a56vboxsync/*******************************************************************************
65c72795ab90d3daefa759b716fbb5c6352c7a56vboxsync* Global Variables *
65c72795ab90d3daefa759b716fbb5c6352c7a56vboxsync*******************************************************************************/
65c72795ab90d3daefa759b716fbb5c6352c7a56vboxsync/*******************************************************************************
65c72795ab90d3daefa759b716fbb5c6352c7a56vboxsync* Internal Functions *
65c72795ab90d3daefa759b716fbb5c6352c7a56vboxsync*******************************************************************************/
78f04b74aefa851f2b51815a7260b47d5be8c776vboxsyncstatic PCTRAPINFO findTrapInfo(uintptr_t uTrapPC, uintptr_t uTrapSP)
78f04b74aefa851f2b51815a7260b47d5be8c776vboxsync /* Search by trap program counter. */
78f04b74aefa851f2b51815a7260b47d5be8c776vboxsync /* Search by return address. */
78f04b74aefa851f2b51815a7260b47d5be8c776vboxsync if (g_aTrapInfo[i].uTrapPC + g_aTrapInfo[i].cbInstr == uReturn)
65c72795ab90d3daefa759b716fbb5c6352c7a56vboxsyncstatic void sigHandler(int iSig, siginfo_t *pSigInfo, void *pvSigCtx)
65c72795ab90d3daefa759b716fbb5c6352c7a56vboxsync# if defined(RT_ARCH_AMD64) && defined(RT_OS_DARWIN)
78f04b74aefa851f2b51815a7260b47d5be8c776vboxsync uintptr_t *puPC = (uintptr_t *)&pCtx->uc_mcontext->__ss.__rip;
e39e9ca944f684a8b310c320b54aa43bbaa072davboxsync# elif defined(RT_ARCH_AMD64) && defined(RT_OS_FREEBSD)
78f04b74aefa851f2b51815a7260b47d5be8c776vboxsync uintptr_t *puPC = (uintptr_t *)&pCtx->uc_mcontext.mc_rip;
78f04b74aefa851f2b51815a7260b47d5be8c776vboxsync uintptr_t *puPC = (uintptr_t *)&pCtx->uc_mcontext.gregs[REG_RIP];
78f04b74aefa851f2b51815a7260b47d5be8c776vboxsync uintptr_t uTrapNo = pCtx->uc_mcontext.gregs[REG_TRAPNO];
65c72795ab90d3daefa759b716fbb5c6352c7a56vboxsync# elif defined(RT_ARCH_X86) && defined(RT_OS_DARWIN)
78f04b74aefa851f2b51815a7260b47d5be8c776vboxsync uintptr_t *puPC = (uintptr_t *)&pCtx->uc_mcontext->__ss.__eip;
e39e9ca944f684a8b310c320b54aa43bbaa072davboxsync# elif defined(RT_ARCH_X86) && defined(RT_OS_FREEBSD)
78f04b74aefa851f2b51815a7260b47d5be8c776vboxsync uintptr_t *puPC = (uintptr_t *)&pCtx->uc_mcontext.mc_eip;
78f04b74aefa851f2b51815a7260b47d5be8c776vboxsync uintptr_t *puPC = (uintptr_t *)&pCtx->uc_mcontext.gregs[REG_EIP];
78f04b74aefa851f2b51815a7260b47d5be8c776vboxsync uintptr_t uTrapNo = pCtx->uc_mcontext.gregs[REG_TRAPNO];
78f04b74aefa851f2b51815a7260b47d5be8c776vboxsync RTAssertMsg2("tstX86-1: Trap #%#04x err=%#06x at %p\n", uTrapNo, uErr, *puPC);
65c72795ab90d3daefa759b716fbb5c6352c7a56vboxsync /** @todo verify the kind of trap */
65c72795ab90d3daefa759b716fbb5c6352c7a56vboxsync * Set up the test environment.
65c72795ab90d3daefa759b716fbb5c6352c7a56vboxsync RTEXITCODE rcExit = RTTestInitAndCreate("tstX86-1", &hTest);
65c72795ab90d3daefa759b716fbb5c6352c7a56vboxsync g_pbEfPage = (uint8_t *)RTTestGuardedAllocTail(hTest, PAGE_SIZE);
78f04b74aefa851f2b51815a7260b47d5be8c776vboxsync g_pbEfExecPage = (uint8_t *)RTMemExecAlloc(PAGE_SIZE*2);
78f04b74aefa851f2b51815a7260b47d5be8c776vboxsync RTTESTI_CHECK(!((uintptr_t)g_pbEfExecPage & PAGE_OFFSET_MASK));
78f04b74aefa851f2b51815a7260b47d5be8c776vboxsync RTTESTI_CHECK_RC(RTMemProtect(g_pbEfExecPage + PAGE_SIZE, PAGE_SIZE, RTMEM_PROT_NONE), VINF_SUCCESS);
78f04b74aefa851f2b51815a7260b47d5be8c776vboxsync static int const s_aiSigs[] = { SIGBUS, SIGSEGV, SIGFPE, SIGILL };
65c72795ab90d3daefa759b716fbb5c6352c7a56vboxsync for (unsigned i = 0; i < RT_ELEMENTS(s_aiSigs); i++)
65c72795ab90d3daefa759b716fbb5c6352c7a56vboxsync RTTESTI_CHECK_BREAK(sigaction(s_aiSigs[i], NULL, &SigAct) == 0);
65c72795ab90d3daefa759b716fbb5c6352c7a56vboxsync RTTESTI_CHECK(sigaction(s_aiSigs[i], &SigAct, NULL) == 0);
65c72795ab90d3daefa759b716fbb5c6352c7a56vboxsync /** @todo implement me. */
65c72795ab90d3daefa759b716fbb5c6352c7a56vboxsync * Do the testing.