Received: from eggs.gnu.org ([2001:4830:134:3::10]:36312) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bLEXT-0001jf-U9 for qemu-devel@nongnu.org; Thu, 07 Jul 2016 15:00:44 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bLEXO-0007iO-Qe for qemu-devel@nongnu.org; Thu, 07 Jul 2016 15:00:42 -0400 Received: from mail-qk0-x241.google.com ([2607:f8b0:400d:c09::241]:36169) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bLEXO-0007iK-CE for qemu-devel@nongnu.org; Thu, 07 Jul 2016 15:00:38 -0400 Received: by mail-qk0-x241.google.com with SMTP id r68so3000354qka.3 for ; Thu, 07 Jul 2016 12:00:38 -0700 (PDT) Sender: Richard Henderson References: <1467880392-1043630-1-git-send-email-snarpix@gmail.com> From: Richard Henderson Message-ID: Date: Thu, 7 Jul 2016 12:00:34 -0700 MIME-Version: 1.0 In-Reply-To: <1467880392-1043630-1-git-send-email-snarpix@gmail.com> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [Qemu-devel] [PATCH v3] translate-all: Bugfix for user-mode self-modifying code in 2 page long TB List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Stanislav Shmarov , qemu-devel@nongnu.org Cc: Paolo Bonzini , Peter Crosthwaite , Sergey Fedorov On 07/07/2016 01:33 AM, Stanislav Shmarov wrote: > In user-mode emulation Translation Block can consist of 2 guest pages. > In that case QEMU also mprotects 2 host pages that are dedicated for > guest memory, containing instructions. QEMU detects self-modifying code > with SEGFAULT signal processing. > > In case if instruction in 1st page is modifying memory of 2nd > page (or vice versa) QEMU will mark 2nd page with PAGE_WRITE, > invalidate TB, generate new TB contatining 1 guest instruction and > exit to CPU loop. QEMU won't call mprotect, and new TB will cause > same SEGFAULT. Page will have both PAGE_WRITE_ORG and PAGE_WRITE > flags, so QEMU will handle the signal as guest binary problem, > and exit with guest SEGFAULT. > > Solution is to do following: In case if current TB was invalidated > continue to invalidate TBs from remaining guest pages and mark pages > as PAGE_WRITE. After that disable host page protection with mprotect. > If current tb was invalidated longjmp to main loop. That is more > efficient, since we won't get SEGFAULT when executing new TB. > > Signed-off-by: Stanislav Shmarov > --- > v3: Now mprotect is called on first SEGFAULT. (Significant changes) > v2: Moved setting PAGE_WRITE flag to separte loop, to cover cases, > pointed by Sergey Fedorov. > translate-all.c | 10 +++++----- > 1 file changed, 5 insertions(+), 5 deletions(-) Reviewed-by: Richard Henderson r~