Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/tests/modules/x86_pte_tester Add tests on the x86 PTEs. We s...



details:   https://anonhg.NetBSD.org/src/rev/11e36285ae45
branches:  trunk
changeset: 931310:11e36285ae45
user:      maxv <maxv%NetBSD.org@localhost>
date:      Sun Apr 26 09:08:40 2020 +0000

description:
Add tests on the x86 PTEs. We scan the MMU page tables directly and verify
certain properties.

diffstat:

 distrib/sets/lists/debug/md.amd64             |    3 +-
 distrib/sets/lists/tests/md.amd64             |    5 +-
 tests/modules/Makefile                        |    9 +-
 tests/modules/t_x86_pte.c                     |  132 +++++++
 tests/modules/x86_pte_tester/Makefile         |   14 +
 tests/modules/x86_pte_tester/x86_pte_tester.c |  464 ++++++++++++++++++++++++++
 6 files changed, 624 insertions(+), 3 deletions(-)

diffs (truncated from 681 to 300 lines):

diff -r ad74cef4e13d -r 11e36285ae45 distrib/sets/lists/debug/md.amd64
--- a/distrib/sets/lists/debug/md.amd64 Sun Apr 26 07:01:52 2020 +0000
+++ b/distrib/sets/lists/debug/md.amd64 Sun Apr 26 09:08:40 2020 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: md.amd64,v 1.107 2020/04/19 13:22:58 maxv Exp $
+# $NetBSD: md.amd64,v 1.108 2020/04/26 09:08:40 maxv Exp $
 ./usr/lib/i386/12.202++_g.a                    comp-c-debuglib         debuglib,compat,12.202xx
 ./usr/lib/i386/libi386_g.a                     comp-c-debuglib         debuglib,compat
 ./usr/lib/i386/libiberty_g.a                   comp-obsolete           obsolete
@@ -28,3 +28,4 @@
 ./usr/libdata/debug/usr/tests/lib/libi386/t_user_ldt.debug     tests-lib-debug debug,atf
 ./usr/libdata/debug/usr/tests/lib/libnvmm/h_io_assist.debug    tests-lib-debug debug,atf
 ./usr/libdata/debug/usr/tests/lib/libnvmm/h_mem_assist.debug   tests-lib-debug debug,atf
+./usr/libdata/debug/usr/tests/modules/t_x86_pte.debug  tests-sys-debug         debug,atf
diff -r ad74cef4e13d -r 11e36285ae45 distrib/sets/lists/tests/md.amd64
--- a/distrib/sets/lists/tests/md.amd64 Sun Apr 26 07:01:52 2020 +0000
+++ b/distrib/sets/lists/tests/md.amd64 Sun Apr 26 09:08:40 2020 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: md.amd64,v 1.8 2020/04/19 13:22:58 maxv Exp $
+# $NetBSD: md.amd64,v 1.9 2020/04/26 09:08:40 maxv Exp $
 ./usr/tests/kernel/arch/x86/Atffile            tests-obsolete  obsolete
 ./usr/tests/kernel/arch/x86/Kyuafile           tests-obsolete  obsolete
 ./usr/tests/kernel/arch/x86/t_ptrace_wait      tests-obsolete  obsolete
@@ -12,3 +12,6 @@
 ./usr/tests/lib/libnvmm/t_io_assist            tests-lib-tests compattestfile,atf
 ./usr/tests/lib/libnvmm/h_mem_assist           tests-lib-tests compattestfile,atf
 ./usr/tests/lib/libnvmm/t_mem_assist           tests-lib-tests compattestfile,atf
+./usr/tests/modules/t_x86_pte                  tests-sys-tests atf
+./usr/tests/modules/x86_pte_tester             tests-sys-tests atf
+./usr/tests/modules/x86_pte_tester/x86_pte_tester.kmod tests-sys-tests atf
diff -r ad74cef4e13d -r 11e36285ae45 tests/modules/Makefile
--- a/tests/modules/Makefile    Sun Apr 26 07:01:52 2020 +0000
+++ b/tests/modules/Makefile    Sun Apr 26 09:08:40 2020 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.18 2020/03/01 18:08:15 christos Exp $
+# $NetBSD: Makefile,v 1.19 2020/04/26 09:08:40 maxv Exp $
 
 .include <bsd.own.mk>
 
@@ -20,6 +20,10 @@
 LDADD.${i}+=   -lrumpfs_kernfs ${LIBRUMPBASE}
 .endfor
 
+.if ${MACHINE} == "amd64"
+TESTS_C+=      t_x86_pte
+.endif
+
 TESTS_SH=      t_abi_uvm
 TESTS_SH+=     t_modload
 TESTS_SH+=     t_klua_pr_52864
@@ -31,5 +35,8 @@
 SUBDIR+=       k_uvm
 SUBDIR+=       threadpool_tester
 SUBDIR+=       ufetchstore
+.if ${MACHINE} == "amd64"
+SUBDIR+=       x86_pte_tester
+.endif
 
 .include <bsd.test.mk>
diff -r ad74cef4e13d -r 11e36285ae45 tests/modules/t_x86_pte.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/modules/t_x86_pte.c Sun Apr 26 09:08:40 2020 +0000
@@ -0,0 +1,132 @@
+/*     $NetBSD: t_x86_pte.c,v 1.1 2020/04/26 09:08:40 maxv Exp $       */
+
+/*
+ * Copyright (c) 2020 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Maxime Villard.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__COPYRIGHT("@(#) Copyright (c) 2020\
+ The NetBSD Foundation, inc. All rights reserved.");
+__RCSID("$NetBSD: t_x86_pte.c,v 1.1 2020/04/26 09:08:40 maxv Exp $");
+
+#include <sys/types.h>
+#include <sys/module.h>
+#include <sys/sysctl.h>
+#include <err.h>
+#include <errno.h>
+#include <atf-c.h>
+
+#define mib_name       "kern.x86_pte_test.test"
+#define MODULE_PATH    "/usr/tests/modules/x86_pte_tester/x86_pte_tester.kmod"
+#define MODULE_NAME    "x86_pte_tester"
+
+static bool module_loaded;
+
+static void
+load_module(void)
+{
+#ifndef SKIP_MODULE
+       if (module_loaded)
+               return;
+
+       modctl_load_t params = {
+               .ml_filename = MODULE_PATH,
+               .ml_flags = MODCTL_NO_PROP,
+       };
+
+       if (modctl(MODCTL_LOAD, &params) != 0) {
+               warn("failed to load module '%s'", MODULE_PATH);
+       } else {
+               module_loaded = true;
+       }
+#else
+       module_loaded = true;
+#endif /* ! SKIP_MODULE */
+}
+
+static void
+unload_module(void)
+{
+#ifndef SKIP_MODULE
+       char module_name[] = MODULE_NAME;
+
+       if (modctl(MODCTL_UNLOAD, module_name) != 0) {
+               warn("failed to unload module '%s'", MODULE_NAME);
+       } else {
+               module_loaded = false;
+       }
+#endif /* ! SKIP_MODULE */
+}
+
+ATF_TC_WITH_CLEANUP(x86_pte);
+ATF_TC_HEAD(x86_pte, tc)
+{
+       atf_tc_set_md_var(tc, "descr",
+           "testing the PTEs for correctness");
+}
+ATF_TC_BODY(x86_pte, tc)
+{
+       struct {
+               size_t n_rwx;
+               bool kernel_map_with_low_ptes;
+               bool pte_is_user_accessible;
+               size_t n_user_space_is_kernel;
+               size_t n_kernel_space_is_user;
+               size_t n_svs_g_bit_set;
+       } results;
+       size_t len = sizeof(results);
+       int rv;
+
+       load_module();
+       if (!module_loaded) {
+               atf_tc_skip("loading '%s' module failed.", MODULE_NAME);
+       }
+
+       rv = sysctlbyname(mib_name, &results, &len, 0, 0);
+       ATF_REQUIRE_EQ(rv, 0);
+
+       ATF_REQUIRE_EQ(results.n_rwx, 0);
+       ATF_REQUIRE_EQ(results.kernel_map_with_low_ptes, false);
+       ATF_REQUIRE_EQ(results.pte_is_user_accessible, false);
+       ATF_REQUIRE_EQ(results.n_user_space_is_kernel, 0);
+       ATF_REQUIRE_EQ(results.n_kernel_space_is_user, 0);
+       if (results.n_svs_g_bit_set != (size_t)-1) {
+               ATF_REQUIRE_EQ(results.n_svs_g_bit_set, 0);
+       }
+}
+ATF_TC_CLEANUP(x86_pte, tc)
+{
+       unload_module();
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+       ATF_TP_ADD_TC(tp, x86_pte);
+
+       return atf_no_error();
+}
diff -r ad74cef4e13d -r 11e36285ae45 tests/modules/x86_pte_tester/Makefile
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/modules/x86_pte_tester/Makefile     Sun Apr 26 09:08:40 2020 +0000
@@ -0,0 +1,14 @@
+#      $NetBSD: Makefile,v 1.1 2020/04/26 09:08:41 maxv Exp $
+
+.include <bsd.own.mk>
+
+KMOD=          x86_pte_tester
+KMODULEDIR=    ${DESTDIR}/${TESTSBASE}/modules/${KMOD}
+
+SRCS=          x86_pte_tester.c
+
+ATFFILE=       no
+NOMAN=         # defined
+
+.include <bsd.test.mk>
+.include <bsd.kmodule.mk>
diff -r ad74cef4e13d -r 11e36285ae45 tests/modules/x86_pte_tester/x86_pte_tester.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/modules/x86_pte_tester/x86_pte_tester.c     Sun Apr 26 09:08:40 2020 +0000
@@ -0,0 +1,464 @@
+/*     $NetBSD: x86_pte_tester.c,v 1.1 2020/04/26 09:08:41 maxv Exp $  */
+
+/*
+ * Copyright (c) 2016 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#define __HAVE_DIRECT_MAP
+#define __HAVE_PCPU_AREA
+#define SVS
+
+#include <sys/cdefs.h>
+#include <sys/param.h>
+#include <sys/module.h>
+#include <sys/proc.h>
+#include <sys/sysctl.h>
+#include <uvm/uvm.h>
+#include <x86/pmap.h>
+
+#if defined(__x86_64__)
+# include <amd64/pmap.h>
+# define NLEVEL 4
+#else
+# error "Unsupported configuration"
+#endif
+
+static struct {
+       struct sysctllog *ctx_sysctllog;
+       vaddr_t levels[NLEVEL];
+       struct {
+               size_t l4;
+               size_t l3;
+               size_t l2;
+               size_t l1;
+       } coord;
+       struct {
+               size_t n_rwx;
+               bool kernel_map_with_low_ptes;
+               bool pte_is_user_accessible;
+               size_t n_user_space_is_kernel;
+               size_t n_kernel_space_is_user;
+               size_t n_svs_g_bit_set;
+       } results;
+} tester_ctx;
+
+typedef enum {
+       WALK_NEXT, /* go to the next level */
+       WALK_SKIP, /* skip the next level, but keep iterating on the current one */
+       WALK_STOP  /* stop the iteration on the current level */
+} walk_type;
+
+/* -------------------------------------------------------------------------- */
+
+#define is_flag(__ent, __flag) (((__ent) & __flag) != 0)
+#define is_valid(__ent)                is_flag(__ent, PTE_P)
+#define get_pa(__pde)          (__pde & PTE_FRAME)
+
+#define L4_MAX_NENTRIES (PAGE_SIZE / sizeof(pd_entry_t))
+#define L3_MAX_NENTRIES (PAGE_SIZE / sizeof(pd_entry_t))
+#define L2_MAX_NENTRIES (PAGE_SIZE / sizeof(pd_entry_t))
+#define L1_MAX_NENTRIES (PAGE_SIZE / sizeof(pd_entry_t))
+



Home | Main Index | Thread Index | Old Index