From patchwork Thu Jun 29 18:46:44 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jim Mattson X-Patchwork-Id: 9817733 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 7A15F6035F for ; Thu, 29 Jun 2017 18:49:15 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6B56B28705 for ; Thu, 29 Jun 2017 18:49:15 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5F7AD28776; Thu, 29 Jun 2017 18:49:15 +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 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 081C728705 for ; Thu, 29 Jun 2017 18:49:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752787AbdF2StN (ORCPT ); Thu, 29 Jun 2017 14:49:13 -0400 Received: from mail-pf0-f181.google.com ([209.85.192.181]:33709 "EHLO mail-pf0-f181.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752320AbdF2StM (ORCPT ); Thu, 29 Jun 2017 14:49:12 -0400 Received: by mail-pf0-f181.google.com with SMTP id e7so54777454pfk.0 for ; Thu, 29 Jun 2017 11:49:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=KZLgMZrSgOaCp9qzKEi9ZkNnpWM7dfiIDXqI6S68p+8=; b=E4WIUAj4YwpK9TG5y5VWtFkh8WLCf2Dv2k1D/mIUH0Yts/VeShP2WtZGOLeD14K8bE Ge+seOAkCbVqjtqZnhBTD3TARNVEFg21CVgnnLhd9/U1mReKyPodeg6locrbNk0mOnI4 ZpqPlHE30GY72e9XszuIfbsyRwhsBaLjdGmsVOrMgO1MiTGt14GtJEnyzYJI87yvw9Dk /mCrntt4aTIGfHa8gnKbtaojVVkXOYCiiiT9K8BD6fVA4M+uLrSJumgnw4mNjG4rXf0W AjjQXSbbv3AYs4Lc+CJ4yJy3K4pDRfPjZGPPZqcRk4UXOEu2tr9IfNUPaTTuNSgYyc1W i+KA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=KZLgMZrSgOaCp9qzKEi9ZkNnpWM7dfiIDXqI6S68p+8=; b=YRudVf1yok71pUsob8NZsY18u2CQrqrAHfCt+hlK/SQ+HOGwgcijNt4yEZLbjVExzS ZwIE1wvV3H40hJZja/18he238UDvdL9JHZmxhffTJacYs16O2PWHXOiTVLzHNYSW5xI9 vRp/GbYH/0pg68Zew4t8FcxSrU5QYTFrRMMxkC9M7ZjCC7RzP02Dwzg4mNdZH030wteN NmN1PwVJA5TykwyK4/15dFGG9zXGgUel9Naz0nCEG7/DP9uEiYu4gjcC5zQM/h0MNCCV 6EXh37Xrif4w4ChdVKKLPKGOBiZkozfzoaEmACOnv7z8Jpl9BoB2tv9fnQ1EW9wSWeOP 5BDw== X-Gm-Message-State: AKS2vOwBlTn/rY5iUaXNtYGlXnO7EMr4OASJJTPClEcInjL/M5UuUUsu DJtyKJD6+5XBnyIR X-Received: by 10.84.151.99 with SMTP id i90mr19815252pli.81.1498762151228; Thu, 29 Jun 2017 11:49:11 -0700 (PDT) Received: from turtle.sea.corp.google.com ([172.31.88.24]) by smtp.gmail.com with ESMTPSA id r63sm10912312pgr.65.2017.06.29.11.49.09 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 29 Jun 2017 11:49:09 -0700 (PDT) From: Jim Mattson To: Paolo Bonzini , David Hildenbrand , kvm list , David Matlack Cc: Jim Mattson Subject: [kvm-unit-tests PATCH v2 1/4] Save/restore handler in test_for_exception Date: Thu, 29 Jun 2017 11:46:44 -0700 Message-Id: <20170629184647.76674-1-jmattson@google.com> X-Mailer: git-send-email 2.13.2.725.g09c95d1e9-goog In-Reply-To: <20170629175211.66170-1-jmattson@google.com> References: <20170629175211.66170-1-jmattson@google.com> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The default handler for #DE, #UD, and #GP is check_exception_table. Test_for_exception should restore the original handler before returning, rather than blindly clobbering it with NULL. Signed-off-by: Jim Mattson --- lib/x86/desc.c | 16 ++++++++++------ lib/x86/desc.h | 4 +++- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/lib/x86/desc.c b/lib/x86/desc.c index 402204ddcac4..830c5d127dbc 100644 --- a/lib/x86/desc.c +++ b/lib/x86/desc.c @@ -117,13 +117,16 @@ static void check_exception_table(struct ex_regs *regs) unhandled_exception(regs, false); } -static void (*exception_handlers[32])(struct ex_regs *regs); +static handler exception_handlers[32]; - -void handle_exception(u8 v, void (*func)(struct ex_regs *regs)) +handler handle_exception(u8 v, handler fn) { + handler old; + + old = exception_handlers[v]; if (v < 32) - exception_handlers[v] = func; + exception_handlers[v] = fn; + return old; } #ifndef __x86_64__ @@ -390,14 +393,15 @@ static void exception_handler(struct ex_regs *regs) bool test_for_exception(unsigned int ex, void (*trigger_func)(void *data), void *data) { + handler old; jmp_buf jmpbuf; int ret; - handle_exception(ex, exception_handler); + old = handle_exception(ex, exception_handler); ret = set_exception_jmpbuf(jmpbuf); if (ret == 0) trigger_func(data); - handle_exception(ex, NULL); + handle_exception(ex, old); return ret; } diff --git a/lib/x86/desc.h b/lib/x86/desc.h index be52fd4e1d92..a2500e0f6006 100644 --- a/lib/x86/desc.h +++ b/lib/x86/desc.h @@ -20,6 +20,8 @@ struct ex_regs { unsigned long rflags; }; +typedef void (*handler)(struct ex_regs *regs); + typedef struct { u16 prev; u16 res1; @@ -153,7 +155,7 @@ void set_idt_dpl(int vec, u16 dpl); void set_gdt_entry(int sel, u32 base, u32 limit, u8 access, u8 gran); void set_intr_alt_stack(int e, void *fn); void print_current_tss_info(void); -void handle_exception(u8 v, void (*func)(struct ex_regs *regs)); +handler handle_exception(u8 v, handler fn); bool test_for_exception(unsigned int ex, void (*trigger_func)(void *data), void *data);