From patchwork Wed Feb 8 08:09:15 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kyle Huey X-Patchwork-Id: 9561853 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 8CADC60434 for ; Wed, 8 Feb 2017 08:09:59 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7F26125D9E for ; Wed, 8 Feb 2017 08:09:59 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 72FBD28415; Wed, 8 Feb 2017 08:09:59 +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=-6.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,RCVD_IN_DNSWL_HI,RCVD_IN_SORBS_SPAM autolearn=unavailable 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 D1B5625D9E for ; Wed, 8 Feb 2017 08:09:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932750AbdBHIJx (ORCPT ); Wed, 8 Feb 2017 03:09:53 -0500 Received: from mail-pg0-f65.google.com ([74.125.83.65]:33815 "EHLO mail-pg0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932145AbdBHIJt (ORCPT ); Wed, 8 Feb 2017 03:09:49 -0500 Received: by mail-pg0-f65.google.com with SMTP id v184so14512078pgv.1 for ; Wed, 08 Feb 2017 00:09:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kylehuey.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=Uhh0qQ5594/zmN8INN1mXBy/LCfjoQH+QCeZLD4Isqw=; b=ON2/BvOpE110+4a+oQ/uUcunbINZHF2iP58qPc+bglsLLl5+t/jyJb+IlpLAnmwPdi AjRnJ8+H81skuSIu9V3hKc3M4QzIToChZi7xIw1JILds+yZ3Eao2CSNrP93kcNIz7Mll QHIoW8H+l3wbmujC3BKSfPVas5cPT1YL9Vv3gp8AWKtFooSfFX951Tlbp9+r1LG3hp6j QAyA6Hyxg0/rNR2a1nuIVIIPQRwRhAJwFot1E4YgpQRKkGb8FyxzEdboRVwOIzjE/c26 dgzSUziijfXz2BlyE3vMc8N3A+IpGlILlkhusK2FrVME0anEhCn/yytpLHuTEEeQbJx8 Khxw== 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=Uhh0qQ5594/zmN8INN1mXBy/LCfjoQH+QCeZLD4Isqw=; b=coCM1ySWEOX7FrlqbGI726zQHSeHfejStqIZICc5vrZ6Gnz8uFm7mOuaj1e9/6qpaW JrFobg24QTl2563mqFdbNvEIRbDHt5xbD/4gr3NxbeBXqqUogsy9Hxd/KVFrM0K3Bwrd 8IktAWkHCVY5xLzn0KpGSeHMRbgpKoUVNXvxRVF5Fsg0RrsA3SDGRA4rqLmlHDafV1UR eNICwzNbSgf6yq0kRyND+Bmjsk2WXKfOM0NAcPrx4v733cNcJIV0zEodR1gRLvwLcbhG FaFGsopf5CVSHAcwxEN+5JcmSWgAf9AVdaS5ykkiXZM+BatxgkUYGMcyWKU3XsmXi9ZN fAeg== X-Gm-Message-State: AIkVDXItJg+g+LQJNAjbBF7TM5i+NESPXobUqD0vESESMOa6EzlpnAGW3dVip2nFgMdIwQ== X-Received: by 10.98.223.195 with SMTP id d64mr24741089pfl.80.1486541384118; Wed, 08 Feb 2017 00:09:44 -0800 (PST) Received: from minbar.home.kylehuey.com (c-50-150-100-179.hsd1.ca.comcast.net. [50.150.100.179]) by smtp.gmail.com with ESMTPSA id j28sm17585078pfj.2.2017.02.08.00.09.41 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 08 Feb 2017 00:09:43 -0800 (PST) From: Kyle Huey X-Google-Original-From: Kyle Huey To: Robert O'Callahan , Thomas Gleixner , Andy Lutomirski , Ingo Molnar , "H. Peter Anvin" , x86@kernel.org, Paolo Bonzini , =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= , Jeff Dike , Richard Weinberger , Alexander Viro , Shuah Khan , Dave Hansen , Borislav Petkov , Peter Zijlstra , Boris Ostrovsky , Len Brown , "Rafael J. Wysocki" , Dmitry Safonov , David Matlack , Nadav Amit , Andi Kleen Cc: linux-kernel@vger.kernel.org, user-mode-linux-devel@lists.sourceforge.net, user-mode-linux-user@lists.sourceforge.net, linux-fsdevel@vger.kernel.org, linux-kselftest@vger.kernel.org, kvm@vger.kernel.org Subject: [PATCH v14 7/9] x86/arch_prctl: Selftest for ARCH_[GET|SET]_CPUID Date: Wed, 8 Feb 2017 00:09:15 -0800 Message-Id: <20170208080917.24320-8-khuey@kylehuey.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170208080917.24320-1-khuey@kylehuey.com> References: <20170208080917.24320-1-khuey@kylehuey.com> Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Test disabling and reenabling the cpuid instruction via the new arch_prctl ARCH_SET_CPUID, retrieving the current state via ARCH_GET_CPUID, and the expected behaviors across fork() and exec(). Signed-off-by: Kyle Huey --- tools/testing/selftests/x86/Makefile | 4 +- tools/testing/selftests/x86/cpuid_fault.c | 251 ++++++++++++++++++++++++++++++ 2 files changed, 253 insertions(+), 2 deletions(-) create mode 100644 tools/testing/selftests/x86/cpuid_fault.c diff --git a/tools/testing/selftests/x86/Makefile b/tools/testing/selftests/x86/Makefile index 8c1cb423cfe6..131c5f266d84 100644 --- a/tools/testing/selftests/x86/Makefile +++ b/tools/testing/selftests/x86/Makefile @@ -1,17 +1,17 @@ -all: +ll: include ../lib.mk .PHONY: all all_32 all_64 warn_32bit_failure clean TARGETS_C_BOTHBITS := single_step_syscall sysret_ss_attrs syscall_nt ptrace_syscall test_mremap_vdso \ check_initial_reg_state sigreturn ldt_gdt iopl \ - protection_keys test_vdso + protection_keys test_vdso cpuid_fault TARGETS_C_32BIT_ONLY := entry_from_vm86 syscall_arg_fault test_syscall_vdso unwind_vdso \ test_FCMOV test_FCOMI test_FISTTP \ vdso_restorer TARGETS_C_64BIT_ONLY := fsgsbase TARGETS_C_32BIT_ALL := $(TARGETS_C_BOTHBITS) $(TARGETS_C_32BIT_ONLY) TARGETS_C_64BIT_ALL := $(TARGETS_C_BOTHBITS) $(TARGETS_C_64BIT_ONLY) BINARIES_32 := $(TARGETS_C_32BIT_ALL:%=%_32) diff --git a/tools/testing/selftests/x86/cpuid_fault.c b/tools/testing/selftests/x86/cpuid_fault.c new file mode 100644 index 000000000000..d061abde85e8 --- /dev/null +++ b/tools/testing/selftests/x86/cpuid_fault.c @@ -0,0 +1,251 @@ + +/* + * Tests for arch_prctl(ARCH_GET_CPUID, ...) / arch_prctl(ARCH_SET_CPUID, ...) + * + * Basic test to test behaviour of ARCH_GET_CPUID and ARCH_SET_CPUID + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +/* +#define ARCH_GET_CPUID 0x1005 +#define ARCH_SET_CPUID 0x1006 +#ifdef __x86_64__ +#define SYS_arch_prctl 158 +#else +#define SYS_arch_prctl 385 +#endif +*/ + +const char *cpuid_names[] = { + [0] = "[cpuid disabled]", + [1] = "[cpuid enabled]", +}; + +int arch_prctl(int code, unsigned long arg2) +{ + return syscall(SYS_arch_prctl, code, arg2); +} + +int cpuid(unsigned int *eax, unsigned int *ebx, unsigned int *ecx, + unsigned int *edx) +{ + return __get_cpuid(0, eax, ebx, ecx, edx); +} + +int do_child_exec_test(int eax, int ebx, int ecx, int edx) +{ + int cpuid_val = 0, child = 0, status = 0; + + printf("arch_prctl(ARCH_GET_CPUID); "); + + cpuid_val = arch_prctl(ARCH_GET_CPUID, 0); + if (cpuid_val < 0) + errx(1, "ARCH_GET_CPUID fails now, but not before?"); + + printf("cpuid_val == %s\n", cpuid_names[cpuid_val]); + if (cpuid_val != 0) + errx(1, "How did cpuid get re-enabled on fork?"); + + child = fork(); + if (child == 0) { + cpuid_val = arch_prctl(ARCH_GET_CPUID, 0); + if (cpuid_val < 0) + errx(1, "ARCH_GET_CPUID fails now, but not before?"); + + printf("cpuid_val == %s\n", cpuid_names[cpuid_val]); + if (cpuid_val != 0) + errx(1, "How did cpuid get re-enabled on fork?"); + + printf("exec\n"); + execl("/proc/self/exe", "cpuid-fault", "-early-return", NULL); + } + + if (child != waitpid(child, &status, 0)) + errx(1, "waitpid failed!?"); + + if (WEXITSTATUS(status) != 0) + errx(1, "Execed child exited abnormally"); + + return 0; +} + +int child_received_signal; + +void child_sigsegv_cb(int sig) +{ + int cpuid_val = 0; + + child_received_signal = 1; + printf("[ SIG_SEGV ]\n"); + printf("arch_prctl(ARCH_GET_CPUID); "); + + cpuid_val = arch_prctl(ARCH_GET_CPUID, 0); + if (cpuid_val < 0) + errx(1, "ARCH_GET_CPUID fails now, but not before?"); + + printf("cpuid_val == %s\n", cpuid_names[cpuid_val]); + printf("arch_prctl(ARCH_SET_CPUID, 1)\n"); + if (arch_prctl(ARCH_SET_CPUID, 1) != 0) + exit(errno); + + printf("cpuid() == "); +} + +int do_child_test(void) +{ + unsigned int eax = 0, ebx = 0, ecx = 0, edx = 0; + + signal(SIGSEGV, child_sigsegv_cb); + + /* the child starts out with cpuid disabled, the signal handler + * attempts to enable and retry + */ + printf("cpuid() == "); + cpuid(&eax, &ebx, &ecx, &edx); + printf("{%x, %x, %x, %x}\n", eax, ebx, ecx, edx); + return child_received_signal ? 0 : 42; +} + +int signal_count; + +void sigsegv_cb(int sig) +{ + int cpuid_val = 0; + + signal_count++; + printf("[ SIG_SEGV ]\n"); + printf("arch_prctl(ARCH_GET_CPUID); "); + + cpuid_val = arch_prctl(ARCH_GET_CPUID, 0); + if (cpuid_val < 0) + errx(1, "ARCH_GET_CPUID fails now, but not before?"); + + printf("cpuid_val == %s\n", cpuid_names[cpuid_val]); + printf("arch_prctl(ARC_SET_CPUID, 1)\n"); + if (arch_prctl(ARCH_SET_CPUID, 1) != 0) + errx(1, "ARCH_SET_CPUID failed!"); + + printf("cpuid() == "); +} + +int main(int argc, char **argv) +{ + int cpuid_val = 0, child = 0, status = 0; + unsigned int eax = 0, ebx = 0, ecx = 0, edx = 0; + + signal(SIGSEGV, sigsegv_cb); + setvbuf(stdout, NULL, _IONBF, 0); + + cpuid(&eax, &ebx, &ecx, &edx); + printf("cpuid() == {%x, %x, %x, %x}\n", eax, ebx, ecx, edx); + printf("arch_prctl(ARCH_GET_CPUID); "); + + cpuid_val = arch_prctl(ARCH_GET_CPUID, 0); + if (cpuid_val < 0) { + if (errno == EINVAL) { + printf("ARCH_GET_CPUID is unsupported on this kernel.\n"); + fflush(stdout); + exit(0); /* no ARCH_GET_CPUID on this system */ + } else if (errno == ENODEV) { + printf("ARCH_GET_CPUID is unsupported on this hardware.\n"); + fflush(stdout); + exit(0); /* no ARCH_GET_CPUID on this system */ + } else { + errx(errno, "ARCH_GET_CPUID failed unexpectedly!"); + } + } + + printf("cpuid_val == %s\n", cpuid_names[cpuid_val]); + cpuid(&eax, &ebx, &ecx, &edx); + printf("cpuid() == {%x, %x, %x, %x}\n", eax, ebx, ecx, edx); + printf("arch_prctl(ARCH_SET_CPUID, 1)\n"); + + if (arch_prctl(ARCH_SET_CPUID, 1) != 0) { + if (errno == EINVAL) { + printf("ARCH_SET_CPUID is unsupported on this kernel."); + exit(0); /* no ARCH_SET_CPUID on this system */ + } else if (errno == ENODEV) { + printf("ARCH_SET_CPUID is unsupported on this hardware."); + exit(0); /* no ARCH_SET_CPUID on this system */ + } else { + errx(errno, "ARCH_SET_CPUID failed unexpectedly!"); + } + } + + + cpuid(&eax, &ebx, &ecx, &edx); + printf("cpuid() == {%x, %x, %x, %x}\n", eax, ebx, ecx, edx); + printf("arch_prctl(ARCH_SET_CPUID, 0)\n"); + fflush(stdout); + + if (arch_prctl(ARCH_SET_CPUID, 0) == -1) + errx(1, "ARCH_SET_CPUID failed!"); + + printf("cpuid() == "); + eax = ebx = ecx = edx = 0; + cpuid(&eax, &ebx, &ecx, &edx); + printf("{%x, %x, %x, %x}\n", eax, ebx, ecx, edx); + printf("arch_prctl(ARCH_SET_CPUID, 0)\n"); + + if (signal_count != 1) + errx(1, "cpuid didn't fault!"); + + if (arch_prctl(ARCH_SET_CPUID, 0) == -1) + errx(1, "ARCH_SET_CPUID failed!"); + + if (argc > 1) + exit(0); /* Don't run the whole test again if we were execed */ + + printf("do_child_test\n"); + child = fork(); + if (child == 0) + return do_child_test(); + + if (child != waitpid(child, &status, 0)) + errx(1, "waitpid failed!?"); + + if (WEXITSTATUS(status) != 0) + errx(1, "Child exited abnormally!"); + + /* The child enabling cpuid should not have affected us */ + printf("cpuid() == "); + eax = ebx = ecx = edx = 0; + cpuid(&eax, &ebx, &ecx, &edx); + printf("{%x, %x, %x, %x}\n", eax, ebx, ecx, edx); + printf("arch_prctl(ARCH_SET_CPUID, 0)\n"); + + if (signal_count != 2) + errx(1, "cpuid didn't fault!"); + + if (arch_prctl(ARCH_SET_CPUID, 0) == -1) + errx(1, "ARCH_SET_CPUID failed!"); + + /* Our ARCH_CPUID_SIGSEGV should not propagate through exec */ + printf("do_child_exec_test\n"); + fflush(stdout); + + child = fork(); + if (child == 0) + return do_child_exec_test(eax, ebx, ecx, edx); + + if (child != waitpid(child, &status, 0)) + errx(1, "waitpid failed!?"); + + if (WEXITSTATUS(status) != 0) + errx(1, "Child exited abnormally!"); + + printf("All tests passed!\n"); + exit(EXIT_SUCCESS); +}