Source-Changes-HG archive

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

[src/trunk]: src/tests/kernel/arch/amd64 Add ATF tests for hardware assisted ...



details:   https://anonhg.NetBSD.org/src/rev/74934564efc7
branches:  trunk
changeset: 819733:74934564efc7
user:      kamil <kamil%NetBSD.org@localhost>
date:      Thu Dec 15 12:15:20 2016 +0000

description:
Add ATF tests for hardware assisted watchpoints on amd64

Addedd tests:
 - watchpoint_count
 - watchpoint_read
 - watchpoint_write_unmodified
 - watchpoint_trap_code[0123]
 - watchpoint_trap_data_write[0123]
 - watchpoint_trap_data_rw[0123]

These code will be reused later for i386 and moved to a common place like
tests/kernel/arch/x86.

These tests are x86 specific only. The same API but different private
ptrace_watchpoint MD part has to be used on other ports.

All tests pass on amd64.

Sponsored by <The NetBSD Foundation>

diffstat:

 tests/kernel/arch/amd64/t_ptrace_wait.c |  1244 ++++++++++++++++++++++++++++++-
 1 files changed, 1242 insertions(+), 2 deletions(-)

diffs (truncated from 1278 to 300 lines):

diff -r 95f49193b218 -r 74934564efc7 tests/kernel/arch/amd64/t_ptrace_wait.c
--- a/tests/kernel/arch/amd64/t_ptrace_wait.c   Thu Dec 15 12:10:01 2016 +0000
+++ b/tests/kernel/arch/amd64/t_ptrace_wait.c   Thu Dec 15 12:15:20 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: t_ptrace_wait.c,v 1.7 2016/12/13 13:09:00 kamil Exp $  */
+/*     $NetBSD: t_ptrace_wait.c,v 1.8 2016/12/15 12:15:20 kamil Exp $  */
 
 /*-
  * Copyright (c) 2016 The NetBSD Foundation, Inc.
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: t_ptrace_wait.c,v 1.7 2016/12/13 13:09:00 kamil Exp $");
+__RCSID("$NetBSD: t_ptrace_wait.c,v 1.8 2016/12/15 12:15:20 kamil Exp $");
 
 #include <sys/param.h>
 #include <sys/types.h>
@@ -37,6 +37,7 @@
 #include <sys/sysctl.h>
 #include <sys/wait.h>
 #include <machine/reg.h>
+#include <x86/dbregs.h>
 #include <err.h>
 #include <errno.h>
 #include <sched.h>
@@ -137,6 +138,1229 @@
 }
 #endif
 
+#if defined(__HAVE_PTRACE_WATCHPOINTS)
+ATF_TC(watchpoint_count);
+ATF_TC_HEAD(watchpoint_count, tc)
+{
+       atf_tc_set_md_var(tc, "descr",
+           "Call PT_COUNT_WATCHPOINTS and assert four available watchpoints");
+}
+
+ATF_TC_BODY(watchpoint_count, tc)
+{
+       const int exitval = 5;
+       const int sigval = SIGSTOP;
+       pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+       int status;
+#endif
+       int N;
+
+       printf("Before forking process PID=%d\n", getpid());
+       ATF_REQUIRE((child = fork()) != -1);
+       if (child == 0) {
+               printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+               FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+               printf("Before raising %s from child\n", strsignal(sigval));
+               FORKEE_ASSERT(raise(sigval) == 0);
+
+               printf("Before exiting of the child process\n");
+               _exit(exitval);
+       }
+       printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+       printf("Before calling %s() for the child\n", TWAIT_FNAME);
+       TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+       validate_status_stopped(status, sigval);
+
+       printf("Call GETREGS for the child process\n");
+       ATF_REQUIRE((N = ptrace(PT_COUNT_WATCHPOINTS, child, NULL, 0)) != -1);
+       printf("Reported %d watchpoints\n", N);
+
+       ATF_REQUIRE_EQ_MSG(N, 4, "Expected 4 hw watchpoints - got %d", N);
+
+       printf("Before resuming the child process where it left off and "
+           "without signal to be sent\n");
+       ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+       printf("Before calling %s() for the child\n", TWAIT_FNAME);
+       TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+       validate_status_exited(status, exitval);
+
+       printf("Before calling %s() for the child\n", TWAIT_FNAME);
+       TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+#endif
+
+#if defined(__HAVE_PTRACE_WATCHPOINTS)
+ATF_TC(watchpoint_read);
+ATF_TC_HEAD(watchpoint_read, tc)
+{
+       atf_tc_set_md_var(tc, "descr",
+           "Call PT_COUNT_WATCHPOINTS and assert four available watchpoints");
+}
+
+ATF_TC_BODY(watchpoint_read, tc)
+{
+       const int exitval = 5;
+       const int sigval = SIGSTOP;
+       pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+       int status;
+#endif
+       int i, N;
+       struct ptrace_watchpoint pw;
+       int len = sizeof(pw);
+
+       printf("Before forking process PID=%d\n", getpid());
+       ATF_REQUIRE((child = fork()) != -1);
+       if (child == 0) {
+               printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+               FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+               printf("Before raising %s from child\n", strsignal(sigval));
+               FORKEE_ASSERT(raise(sigval) == 0);
+
+               printf("Before exiting of the child process\n");
+               _exit(exitval);
+       }
+       printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+       printf("Before calling %s() for the child\n", TWAIT_FNAME);
+       TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+       validate_status_stopped(status, sigval);
+
+       printf("Call GETREGS for the child process\n");
+       ATF_REQUIRE((N = ptrace(PT_COUNT_WATCHPOINTS, child, NULL, 0)) != -1);
+
+       ATF_REQUIRE_EQ_MSG(N, 4, "Expected 4 hw watchpoints - got %d", N);
+
+       for (i = 0; i < N; i++) {
+               printf("Before reading watchpoint %d\n", i);
+               pw.pw_index = i;
+               ATF_REQUIRE(ptrace(PT_READ_WATCHPOINT, child, &pw, len) != -1);
+
+               printf("struct ptrace {\n");
+               printf("\t.pw_index=%d\n", pw.pw_index);
+               printf("\t.pw_lwpid=%d\n", pw.pw_lwpid);
+               printf("\t.pw_md.md_address=%p\n", pw.pw_md.md_address);
+               printf("\t.pw_md.md_condition=%#x\n", pw.pw_md.md_condition);
+               printf("\t.pw_md.md_length=%#x\n", pw.pw_md.md_length);
+               printf("}\n");
+       }
+
+       printf("Before resuming the child process where it left off and "
+           "without signal to be sent\n");
+       ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+       printf("Before calling %s() for the child\n", TWAIT_FNAME);
+       TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+       validate_status_exited(status, exitval);
+
+       printf("Before calling %s() for the child\n", TWAIT_FNAME);
+       TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+#endif
+
+#if defined(__HAVE_PTRACE_WATCHPOINTS)
+ATF_TC(watchpoint_write_unmodified);
+ATF_TC_HEAD(watchpoint_write_unmodified, tc)
+{
+       atf_tc_set_md_var(tc, "descr",
+           "Call PT_COUNT_WATCHPOINTS and assert functional write of "
+           "unmodified data");
+}
+
+ATF_TC_BODY(watchpoint_write_unmodified, tc)
+{
+       const int exitval = 5;
+       const int sigval = SIGSTOP;
+       pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+       int status;
+#endif
+       int i, N;
+       struct ptrace_watchpoint pw;
+       int len = sizeof(pw);
+
+       printf("Before forking process PID=%d\n", getpid());
+       ATF_REQUIRE((child = fork()) != -1);
+       if (child == 0) {
+               printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+               FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+               printf("Before raising %s from child\n", strsignal(sigval));
+               FORKEE_ASSERT(raise(sigval) == 0);
+
+               printf("Before exiting of the child process\n");
+               _exit(exitval);
+       }
+       printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+       printf("Before calling %s() for the child\n", TWAIT_FNAME);
+       TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+       validate_status_stopped(status, sigval);
+
+       printf("Call GETREGS for the child process\n");
+       ATF_REQUIRE((N = ptrace(PT_COUNT_WATCHPOINTS, child, NULL, 0)) != -1);
+
+       ATF_REQUIRE_EQ_MSG(N, 4, "Expected 4 hw watchpoints - got %d", N);
+
+       for (i = 0; i < N; i++) {
+               printf("Before reading watchpoint %d\n", i);
+               pw.pw_index = i;
+               ATF_REQUIRE(ptrace(PT_READ_WATCHPOINT, child, &pw, len) != -1);
+
+               printf("struct ptrace {\n");
+               printf("\t.pw_index=%d\n", pw.pw_index);
+               printf("\t.pw_lwpid=%d\n", pw.pw_lwpid);
+               printf("\t.pw_md.md_address=%p\n", pw.pw_md.md_address);
+               printf("\t.pw_md.md_condition=%#x\n", pw.pw_md.md_condition);
+               printf("\t.pw_md.md_length=%#x\n", pw.pw_md.md_length);
+               printf("}\n");
+
+               printf("Before writing watchpoint %d (unmodified)\n", i);
+               ATF_REQUIRE(ptrace(PT_WRITE_WATCHPOINT, child, &pw, len)
+                   != -1);
+       }
+
+       printf("Before resuming the child process where it left off and "
+           "without signal to be sent\n");
+       ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+       printf("Before calling %s() for the child\n", TWAIT_FNAME);
+       TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+       validate_status_exited(status, exitval);
+
+       printf("Before calling %s() for the child\n", TWAIT_FNAME);
+       TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+#endif
+
+#if defined(__HAVE_PTRACE_WATCHPOINTS)
+ATF_TC(watchpoint_trap_code0);
+ATF_TC_HEAD(watchpoint_trap_code0, tc)
+{
+       atf_tc_set_md_var(tc, "descr",
+           "Call PT_COUNT_WATCHPOINTS and test code trap with watchpoint 0");
+}
+
+ATF_TC_BODY(watchpoint_trap_code0, tc)
+{
+       const int exitval = 5;
+       const int sigval = SIGSTOP;
+       pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+       int status;
+#endif
+       const int i = 0;
+       struct ptrace_watchpoint pw;
+       int len = sizeof(pw);
+       int watchme = 1234;
+
+       printf("Before forking process PID=%d\n", getpid());
+       ATF_REQUIRE((child = fork()) != -1);
+       if (child == 0) {
+               printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+               FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+               printf("Before raising %s from child\n", strsignal(sigval));
+               FORKEE_ASSERT(raise(sigval) == 0);
+
+               printf("check_happy(%d)=%d\n", watchme, check_happy(watchme));
+
+               printf("Before exiting of the child process\n");
+               _exit(exitval);
+       }
+       printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+       printf("Before calling %s() for the child\n", TWAIT_FNAME);
+       TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+       validate_status_stopped(status, sigval);
+
+       printf("Preparing code watchpoint trap %d\n", i);
+
+       pw.pw_index = i;
+       pw.pw_lwpid = 0;
+       pw.pw_md.md_address = (void *)check_happy;
+       pw.pw_md.md_condition = X86_HW_WATCHPOINT_DR7_CONDITION_EXECUTION;
+       pw.pw_md.md_length = X86_HW_WATCHPOINT_DR7_LENGTH_BYTE;
+
+       printf("struct ptrace {\n");
+       printf("\t.pw_index=%d\n", pw.pw_index);
+       printf("\t.pw_lwpid=%d\n", pw.pw_lwpid);
+       printf("\t.pw_md.md_address=%p\n", pw.pw_md.md_address);
+       printf("\t.pw_md.md_condition=%#x\n", pw.pw_md.md_condition);
+       printf("\t.pw_md.md_length=%#x\n", pw.pw_md.md_length);
+       printf("}\n");
+
+       printf("Before writing watchpoint %d\n", i);
+       ATF_REQUIRE(ptrace(PT_WRITE_WATCHPOINT, child, &pw, len) != -1);
+
+       printf("Before resuming the child process where it left off "
+           "and without signal to be sent\n");
+       ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);



Home | Main Index | Thread Index | Old Index