From patchwork Sun Dec 24 10:07:58 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arbel Moshe X-Patchwork-Id: 10131917 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 9F6346019C for ; Sun, 24 Dec 2017 10:09:07 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 87F1A28C58 for ; Sun, 24 Dec 2017 10:09:07 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7BAB428D6F; Sun, 24 Dec 2017 10:09:07 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, RCVD_IN_DNSWL_HI, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9757328C58 for ; Sun, 24 Dec 2017 10:09:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752062AbdLXKJD (ORCPT ); Sun, 24 Dec 2017 05:09:03 -0500 Received: from userp2130.oracle.com ([156.151.31.86]:34607 "EHLO userp2130.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751941AbdLXKJB (ORCPT ); Sun, 24 Dec 2017 05:09:01 -0500 Received: from pps.filterd (userp2130.oracle.com [127.0.0.1]) by userp2130.oracle.com (8.16.0.21/8.16.0.21) with SMTP id vBOA8sMc090561; Sun, 24 Dec 2017 10:08:58 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references; s=corp-2017-10-26; bh=aPxekYsrPSzP/6OBchHJmnk8Bps+EZIcfmb6Oj1Graw=; b=sZ+iqouCpjq22NHn/lfclA32sGsnHLWNt+8+tu+/2kph51SqQ6nrGfMMC7tMVtz6aKHb Blf4E7vzU+ruE//K9WP9Bl+SyfArIgrRcCT3WPh9FbauauXx+vG3G2KEfSIuSGgLBpbp 3/rqL8L8nQbGI37LhmrdmRNjNCjL2h4skFSqBSeqpL0fqvLeaUCI3F0gTR3fgwxk+o9f cY4BEeCVPk7G98U06OqCjN8pwNBPhzEzdGZyRkwaBvN2UYD/9Ckl/krLZHtIECFcqBFK hAngsAXXzwph65UMoHukamidEWo9JwWtgEWMitw+nibYNFsRNXDBFA+ZROzS4ATybVqB jg== Received: from userv0022.oracle.com (userv0022.oracle.com [156.151.31.74]) by userp2130.oracle.com with ESMTP id 2f2a68g04b-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sun, 24 Dec 2017 10:08:58 +0000 Received: from userv0122.oracle.com (userv0122.oracle.com [156.151.31.75]) by userv0022.oracle.com (8.14.4/8.14.4) with ESMTP id vBOA8vrT032037 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Sun, 24 Dec 2017 10:08:57 GMT Received: from abhmp0016.oracle.com (abhmp0016.oracle.com [141.146.116.22]) by userv0122.oracle.com (8.14.4/8.14.4) with ESMTP id vBOA8vSL023326; Sun, 24 Dec 2017 10:08:57 GMT Received: from spark.ravello.local (/213.57.127.2) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Sun, 24 Dec 2017 02:08:57 -0800 From: Arbel Moshe To: pbonzini@redhat.com, rkrcmar@redhat.com, kvm@vger.kernel.org Cc: idan.brown@oracle.com, liran.alon@oracle.com, Arbel Moshe , Konrad Rzeszutek Wilk Subject: [PATCH 2/5] x86: Add Test Utility to run in User Mode and catch exceptions Date: Sun, 24 Dec 2017 12:07:58 +0200 Message-Id: <20171224100801.145806-3-arbel.moshe@oracle.com> X-Mailer: git-send-email 2.14.1 In-Reply-To: <20171224100801.145806-1-arbel.moshe@oracle.com> References: <20171224100801.145806-1-arbel.moshe@oracle.com> X-Proofpoint-Virus-Version: vendor=nai engine=5900 definitions=8754 signatures=668650 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 mlxscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1711220000 definitions=main-1712240137 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Add a test utility that enables: 1. Running test function in User Mode or Kernel Mode. 2. Catching an exception. 3. Running a callback function to test return val. Signed-off-by: Arbel Moshe Reviewed-by: Liran Alon Signed-off-by: Konrad Rzeszutek Wilk --- lib/x86/fault_test.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/x86/fault_test.h | 42 ++++++++++++++++++++++++++++++++++++++++++ x86/Makefile.common | 1 + 3 files changed, 95 insertions(+) create mode 100644 lib/x86/fault_test.c create mode 100644 lib/x86/fault_test.h diff --git a/lib/x86/fault_test.c b/lib/x86/fault_test.c new file mode 100644 index 0000000..71ac982 --- /dev/null +++ b/lib/x86/fault_test.c @@ -0,0 +1,52 @@ +#include "fault_test.h" + +jmp_buf jmpbuf; + +static void restore_exec_to_jmpbuf(void) +{ + longjmp(jmpbuf, 1); +} + +static void fault_test_fault(struct ex_regs *regs) +{ + regs->rip = (unsigned long)&restore_exec_to_jmpbuf; +} + +static bool fault_test(struct fault_test_arg *arg) +{ + uint64_t val; + bool raised_vector = false; + test_fault_func func = (test_fault_func) arg->func; + /* Init as success in case there isn't callback */ + bool callback_success = true; + + if (arg->usermode) { + val = run_in_user((usermode_func) func, arg->fault_vector, + arg->arg[0], arg->arg[1], arg->arg[2], + arg->arg[3], &raised_vector); + } else { + handle_exception(arg->fault_vector, fault_test_fault); + if (setjmp(jmpbuf) == 0) + val = func(arg->arg[0], arg->arg[1], arg->arg[2], + arg->arg[3]); + else + raised_vector = true; + } + + if (!raised_vector) { + arg->retval = val; + if (arg->callback != NULL) + callback_success = arg->callback(arg); + } + + return arg->should_fault ? + raised_vector : (!raised_vector && callback_success); +} + +void test_run(struct fault_test *test) +{ + bool passed = fault_test(&(test->arg)); + + report("%s", passed, test->name); +} + diff --git a/lib/x86/fault_test.h b/lib/x86/fault_test.h new file mode 100644 index 0000000..dfa715b --- /dev/null +++ b/lib/x86/fault_test.h @@ -0,0 +1,42 @@ +#ifndef __FAULT_TEST__ +#define __FAULT_TEST__ + +#include "x86/msr.h" +#include "x86/processor.h" +#include "x86/apic-defs.h" +#include "x86/apic.h" +#include "x86/desc.h" +#include "x86/isr.h" +#include "alloc.h" +#include "setjmp.h" +#include "usermode.h" + +#include "libcflat.h" +#include + +#define FAULT_TEST(nm, a) { .name = nm, .arg = a} + +struct fault_test_arg; + +typedef uint64_t (*test_fault_func)(uint64_t arg1, uint64_t arg2, + uint64_t arg3, uint64_t arg4); +typedef bool (*test_fault_callback)(struct fault_test_arg *arg); + +struct fault_test_arg { + bool usermode; + unsigned int fault_vector; + bool should_fault; + uint64_t arg[4]; + uint64_t retval; + test_fault_func func; + test_fault_callback callback; +}; + +struct fault_test { + const char *name; + struct fault_test_arg arg; +}; + +void test_run(struct fault_test *test); + +#endif diff --git a/x86/Makefile.common b/x86/Makefile.common index 8a9d245..cbd5847 100644 --- a/x86/Makefile.common +++ b/x86/Makefile.common @@ -20,6 +20,7 @@ cflatobjs += lib/x86/isr.o cflatobjs += lib/x86/acpi.o cflatobjs += lib/x86/stack.o cflatobjs += lib/x86/usermode.o +cflatobjs += lib/x86/fault_test.o OBJDIRS += lib/x86