From nobody Tue Aug 09 23:02:46 2022 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4M2T9y4Htvz4YcH6; Tue, 9 Aug 2022 23:02:46 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4M2T9y1xhvz3bZq; Tue, 9 Aug 2022 23:02:46 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1660086166; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=tDJ29anJUWzMZ74zZvBLL+JgjU9OmSISpKYKZu9xPjk=; b=AN4T16dFRSKJ+M5Cd+IE0WY46qZ18Cc0gxSq1DDFhuPd9R+a2I0xxmZD6D0wig2k1BJX2Q d2ziZX8MTdwOr+p6rbVJc1YPZiHQ212dERUHB0y4gCnfBy7zORn8nqWiSlPT454AWeFuBp FMDR9LAxpPsx0s7t4HTh5yEJxUQEbfHtNyDVRyQROPE3lItDP4vBLQLIXuoJTtYUAnEcf4 OF2Xm7e+/DhUr3KliV65rV5u75i/vsDKYK4CTx6K9nKq8zWrBY+m8nH8hbjsU/AOZPERbJ o6VkBhMvojAjQylDgtyDfg6t19eAfwCjQhEeA9FGBc74K82PP8grw6N2MVCi8g== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4M2T9y0rqYzpsB; Tue, 9 Aug 2022 23:02:46 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 279N2kWR082054; Tue, 9 Aug 2022 23:02:46 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 279N2kgS082053; Tue, 9 Aug 2022 23:02:46 GMT (envelope-from git) Date: Tue, 9 Aug 2022 23:02:46 GMT Message-Id: <202208092302.279N2kgS082053@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Mark Johnston Subject: git: 3ba8e9dc4a0e - main - dtrace/amd64: Implement emulation of call instructions List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: markj X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 3ba8e9dc4a0e0e9c35cfadfe25379871ce581697 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1660086166; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=tDJ29anJUWzMZ74zZvBLL+JgjU9OmSISpKYKZu9xPjk=; b=iRky3T4ijTlottTf2ImT1YbUQVggOV8OsXaynNZERVIpc71dVyPWN5mZs9S3sF4QjBFF0n 1qyCDn2GSd3T6G5sks8g5eDs33gnKAWG6apzmMEYKNF4frlhCKkvoZSE5fCgofnSQX+eEb D73ExPb/+UJwwcb4f2+hqYvfLme2ThX3AyARGx/ptP7i7crFDTVsRWPf7FQ3+0j7fS5iVO jo2AseI1bOipkqZO0/F+/hmKhOu+5VHsnLDjoe7SNoPW86XhF3AwHd5eWZ8WKFqGyP/hin cEsV+NB6IXKYwAB2ZP8QDIs5ouERKDkWuFW6s8+XkDPCpBzukzQDomBwwP5F/Q== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1660086166; a=rsa-sha256; cv=none; b=VumJ7nhRDcZ01PIJIP3hzdO5HlpBxGS19zaELnZNspk4VxqB6wdn5BdRz01ocxVGLnNPbh q9Y24tBfrnGwQtkmxo20ioOiNTIJmNVn5HzknKq7sGdhhsi77uvysBGlQ8pEKHVfLbZors is1xjffs/Nxh+OThyMX6xkY8bqsJydeSH2doOiImroN1fK3awVIkksjIEHGyyPCOBmjENL ZSBAhHHLVykhhm+iy8sC9yzqmk+UE08MWbj3/booAsT60Th6fKZnYDwjMaJnPSHqzC7vy7 OX+vPTxOO12kCFyCWOtbJ9e/fJpZ2dnjQ+W/Q+aEALLqvzhlgDZStyMsDZiPdg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=3ba8e9dc4a0e0e9c35cfadfe25379871ce581697 commit 3ba8e9dc4a0e0e9c35cfadfe25379871ce581697 Author: Mark Johnston AuthorDate: 2022-08-09 20:08:13 +0000 Commit: Mark Johnston CommitDate: 2022-08-09 22:34:01 +0000 dtrace/amd64: Implement emulation of call instructions Here, the provider is responsible for updating the trapframe to redirect control flow and for computing the return address. Once software-saved registers are restored, the emulation shifts the remaining context down on the stack to make space for the return address, then copies the address provided by the invop handler. dtrace_invop() is modified to allocate temporary storage space on the stack for use by the provider to return the return address. This is to support a new provider for amd64 which can instrument arbitrary instructions, not just function entry and exit instructions as FBT does. In collaboration with: christos Sponsored by: Google, Inc. (GSoC 2022) Sponsored by: The FreeBSD Foundation MFC after: 2 weeks --- .../contrib/opensolaris/uts/common/sys/dtrace.h | 4 ++ sys/cddl/dev/dtrace/amd64/dtrace_asm.S | 51 +++++++++++++++++++++- sys/cddl/dev/dtrace/amd64/dtrace_subr.c | 11 ++--- 3 files changed, 59 insertions(+), 7 deletions(-) diff --git a/sys/cddl/contrib/opensolaris/uts/common/sys/dtrace.h b/sys/cddl/contrib/opensolaris/uts/common/sys/dtrace.h index c15cc39189b1..5cad5a3b18c9 100644 --- a/sys/cddl/contrib/opensolaris/uts/common/sys/dtrace.h +++ b/sys/cddl/contrib/opensolaris/uts/common/sys/dtrace.h @@ -2440,6 +2440,10 @@ extern void dtrace_helpers_destroy(proc_t *); #define DTRACE_INVOP_NOP 4 #define DTRACE_INVOP_RET 5 +#if defined(__amd64) +#define DTRACE_INVOP_CALL 6 +#endif + #elif defined(__powerpc__) #define DTRACE_INVOP_BCTR 1 diff --git a/sys/cddl/dev/dtrace/amd64/dtrace_asm.S b/sys/cddl/dev/dtrace/amd64/dtrace_asm.S index 9a83c87dda02..ffeaa787bd5e 100644 --- a/sys/cddl/dev/dtrace/amd64/dtrace_asm.S +++ b/sys/cddl/dev/dtrace/amd64/dtrace_asm.S @@ -58,6 +58,8 @@ swapgs; \ 1: addq $TF_RIP,%rsp; +.globl dtrace_invop_callsite +.type dtrace_invop_callsite,@function ENTRY(dtrace_invop_start) @@ -69,11 +71,22 @@ movq TF_RIP(%rsp), %rdi decq %rdi movq %rsp, %rsi - movq TF_RAX(%rsp), %rdx + + /* + * Allocate some scratch space to let the invop handler return a value. + * This is needed when emulating "call" instructions. + */ + subq $16, %rsp + movq %rsp, %rdx + call dtrace_invop - ENTRY(dtrace_invop_callsite) +dtrace_invop_callsite: + addq $16, %rsp + cmpl $DTRACE_INVOP_PUSHL_EBP, %eax je bp_push + cmpl $DTRACE_INVOP_CALL, %eax + je bp_call cmpl $DTRACE_INVOP_LEAVE, %eax je bp_leave cmpl $DTRACE_INVOP_NOP, %eax @@ -109,6 +122,40 @@ bp_push: iretq /* return from interrupt */ /*NOTREACHED*/ +bp_call: + /* + * Emulate a "call" instruction. The invop handler must have already + * updated the saved copy of %rip in the register set. It's our job to + * pull the hardware-saved registers down to make space for the return + * address, which is provided by the invop handler in our scratch + * space. + */ + INTR_POP + subq $16, %rsp /* make room for %rbp */ + pushq %rax /* push temp */ + pushq %rbx /* push temp */ + + movq 32(%rsp), %rax /* load calling RIP */ + movq %rax, 16(%rsp) /* store calling RIP */ + movq 40(%rsp), %rax /* load calling CS */ + movq %rax, 24(%rsp) /* store calling CS */ + movq 48(%rsp), %rax /* load calling RFLAGS */ + movq %rax, 32(%rsp) /* store calling RFLAGS */ + movq 56(%rsp), %rax /* load calling RSP */ + subq $8, %rax /* make room for return address */ + movq %rax, 40(%rsp) /* store calling RSP */ + movq 64(%rsp), %rax /* load calling SS */ + movq %rax, 48(%rsp) /* store calling SS */ + + movq -(TF_RIP - 16)(%rsp), %rax /* load return address */ + movq 40(%rsp), %rbx /* reload calling RSP */ + movq %rax, (%rbx) /* store return address */ + + popq %rbx /* pop temp */ + popq %rax /* pop temp */ + iretq /* return from interrupt */ + /*NOTREACHED*/ + bp_leave: /* * We must emulate a "leave", which is the same as a "movq %rbp, %rsp" diff --git a/sys/cddl/dev/dtrace/amd64/dtrace_subr.c b/sys/cddl/dev/dtrace/amd64/dtrace_subr.c index 4f9d9995cbab..f4fb70f80a6b 100644 --- a/sys/cddl/dev/dtrace/amd64/dtrace_subr.c +++ b/sys/cddl/dev/dtrace/amd64/dtrace_subr.c @@ -50,7 +50,7 @@ extern void dtrace_getnanotime(struct timespec *tsp); extern int (*dtrace_invop_jump_addr)(struct trapframe *); -int dtrace_invop(uintptr_t, struct trapframe *, uintptr_t); +int dtrace_invop(uintptr_t, struct trapframe *, void **); int dtrace_invop_start(struct trapframe *frame); void dtrace_invop_init(void); void dtrace_invop_uninit(void); @@ -63,15 +63,16 @@ typedef struct dtrace_invop_hdlr { dtrace_invop_hdlr_t *dtrace_invop_hdlr; int -dtrace_invop(uintptr_t addr, struct trapframe *frame, uintptr_t eax) +dtrace_invop(uintptr_t addr, struct trapframe *frame, void **scratch) { dtrace_invop_hdlr_t *hdlr; int rval; - for (hdlr = dtrace_invop_hdlr; hdlr != NULL; hdlr = hdlr->dtih_next) - if ((rval = hdlr->dtih_func(addr, frame, eax)) != 0) + for (hdlr = dtrace_invop_hdlr; hdlr != NULL; hdlr = hdlr->dtih_next) { + rval = hdlr->dtih_func(addr, frame, (uintptr_t)scratch); + if (rval != 0) return (rval); - + } return (0); }