From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:56387) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bKhiC-0003mE-88 for qemu-devel@nongnu.org; Wed, 06 Jul 2016 03:57:38 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bKhi9-0007hR-2Z for qemu-devel@nongnu.org; Wed, 06 Jul 2016 03:57:36 -0400 Received: from mail-lf0-x242.google.com ([2a00:1450:4010:c07::242]:33034) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bKhi8-0007hC-Qi for qemu-devel@nongnu.org; Wed, 06 Jul 2016 03:57:33 -0400 Received: by mail-lf0-x242.google.com with SMTP id l188so21657846lfe.0 for ; Wed, 06 Jul 2016 00:57:32 -0700 (PDT) From: Stanislav Shmarov Date: Wed, 6 Jul 2016 10:54:28 +0300 Message-Id: <1467791668-2937838-1-git-send-email-snarpix@gmail.com> Subject: [Qemu-devel] [PATCH v2] 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: qemu-devel@nongnu.org Cc: Paolo Bonzini , Peter Crosthwaite , Richard Henderson , Sergey Fedorov , Stanislav Shmarov 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 retranslate TB before marking pages as PAGE_WRITE, and remove protection with mprotect on second SEGFAULT. Signed-off-by: Stanislav Shmarov --- v2: Moved setting PAGE_WRITE flag to separte loop, to cover cases, pointed by Sergey Fedorov. translate-all.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/translate-all.c b/translate-all.c index eaa95e4..fb3743f 100644 --- a/translate-all.c +++ b/translate-all.c @@ -2020,13 +2020,8 @@ int page_unprotect(target_ulong address, uintptr_t pc) host_start = address & qemu_host_page_mask; host_end = host_start + qemu_host_page_size; - prot = 0; for (addr = host_start ; addr < host_end ; addr += TARGET_PAGE_SIZE) { - p = page_find(addr >> TARGET_PAGE_BITS); - p->flags |= PAGE_WRITE; - prot |= p->flags; - - /* and since the content will be modified, we must invalidate + /* Since the content will be modified, we must invalidate the corresponding translated code. */ if (tb_invalidate_phys_page(addr, pc)) { mmap_unlock(); @@ -2036,6 +2031,16 @@ int page_unprotect(target_ulong address, uintptr_t pc) tb_invalidate_check(addr); #endif } + + /* If we got here, current TB have been retranslated (in case of + * self-modifying code), now it's safe to remove page protection. + */ + prot = 0; + for (addr = host_start ; addr < host_end ; addr += TARGET_PAGE_SIZE) { + p = page_find(addr >> TARGET_PAGE_BITS); + p->flags |= PAGE_WRITE; + prot |= p->flags; + } mprotect((void *)g2h(host_start), qemu_host_page_size, prot & PAGE_BITS); -- 1.9.3