From owner-freebsd-arch@FreeBSD.ORG Fri Aug 2 12:40:54 2013 Return-Path: Delivered-To: freebsd-arch@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTP id 21058AFF for ; Fri, 2 Aug 2013 12:40:54 +0000 (UTC) (envelope-from piyuskedia@gmail.com) Received: from mail-pb0-x232.google.com (mail-pb0-x232.google.com [IPv6:2607:f8b0:400e:c01::232]) (using TLSv1 with cipher ECDHE-RSA-RC4-SHA (128/128 bits)) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id EE526250C for ; Fri, 2 Aug 2013 12:40:53 +0000 (UTC) Received: by mail-pb0-f50.google.com with SMTP id uo5so636845pbc.23 for ; Fri, 02 Aug 2013 05:40:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=BFtiYAEVc9EE12SZ2X3VsAcqUvKR8DUG2Rtin83wiEw=; b=EV9y7rYfXCWssDpEBb4+JAREht2O3RmPYBRyXhG7n+5N6vpTsFZN/667AvbOXE9qH4 MBw0W1s6RLPW6zdXq8FFfBBs2pW62KvSdWFYr2dc66Xg5ozlZ8pgJT4UCNqyQ2ghReC2 IzDov73Ia38wf10fi3TMuCNwo1OiG1lKOYc4nRv+B8vmn32dP+DDbIMykUCQPvBRH5uA O7nI5bpjKEMq3Ol4XH4K4j1NqG5e8+o2089Fk0uF/i7TN+XLwVlaTmav2977nykWe21u UISK0W+TzI+w4hZuIkWiNTDRDQCwG7nkXn/Nrg6outFF+6TEA/iGxKl8d0MzviS41rjZ +VYg== MIME-Version: 1.0 X-Received: by 10.68.201.98 with SMTP id jz2mr7587460pbc.56.1375447253654; Fri, 02 Aug 2013 05:40:53 -0700 (PDT) Received: by 10.70.7.65 with HTTP; Fri, 2 Aug 2013 05:40:53 -0700 (PDT) In-Reply-To: <20130802120405.GY4972@kib.kiev.ua> References: <20130802120405.GY4972@kib.kiev.ua> Date: Fri, 2 Aug 2013 21:40:53 +0900 Message-ID: Subject: Re: Fwd: Use of the PC value in interrupt/exception handlers From: Piyus Kedia To: Konstantin Belousov Content-Type: text/plain; charset=ISO-8859-1 X-Content-Filtered-By: Mailman/MimeDel 2.1.14 Cc: freebsd-arch@freebsd.org X-BeenThere: freebsd-arch@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Discussion related to FreeBSD architecture List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 02 Aug 2013 12:40:54 -0000 Thanks, that helped a lot. On Fri, Aug 2, 2013 at 9:04 PM, Konstantin Belousov wrote: > On Fri, Aug 02, 2013 at 07:08:08PM +0900, Piyus Kedia wrote: > > Dear all, > > > > We are working on developing a dynamic binary translator for the kernel. > > Towards this, we wanted to confirm if the interrupted PC value pushed on > > stack by an interrupt/exception is used by the interrupt/exception > > handlers? For example, is the PC value compared against a fixed address > to > > determine the handler behaviour (like > > Linux's page fault handler compares the faulting PC against an exception > > table, to allow functions like copy_from_user to fault). > > > > Basically, we are wondering if it is safe to replace the pushed PC value > on > > stack by another value. This would be safe if the PC value is only used > for > > returning from interrupt, or for reading contents at that PC address > (e.g., > > to decode the instruction at current PC). It would be unsafe if the value > > of the address itself is meaningful to the handler. > > > > We found that in FreeBSD segment-not-present exception handler checks the > > trapped PC value against some fixed kernel PC by looking at the code, > > except that it is only used for debugging purposes. It would be nice if > > somebody could also confirm this. > > You did not specified which architecture you are talking about. There > are subtle differences between i386 and amd64 in this area, and I have > no knowledge of other architectures. The answer to your question if very > machine-specific. > > Yes, the most obvious place where the instruction pointer is replaced if > the trap(). There, one case is when saved %rip points to the predefined > list of the instructions which might fault because their operations are > inherently non-safe. This list is mostly populated with addresses from > the interrupt return sequence. > > Another mechanism, implemented in the trap() but actually used by other > code, in particular, the copyin/copyout, but also other functions, is > the pcb_onfault handler. If e.g. page fault happens during the copying, > the control stream is passed to the handler specified in pcb_onfault. > Look at the support.s for examples. I am not sure if this use is relevant > to you. > > The ddb looks for the special rip values to properly step over the trap > or interrupt frames, since these frames do not follow the 'normal' kernel > frame layout (not quite normal, because we deviate from the ABI-mandated > backtrace interface in the kernel). > > Are you only interested in the kernel side of things ? Usermode with the > signal delivery/sigreturn(2), ucontext(2) and ptrace(2) could be also > the interesting situation to consider. >