From owner-svn-src-all@freebsd.org Wed Mar 21 00:29:57 2018 Return-Path: Delivered-To: svn-src-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 57EA3F59AA9 for ; Wed, 21 Mar 2018 00:29:57 +0000 (UTC) (envelope-from oliver.pinter@hardenedbsd.org) Received: from mail-yw0-x241.google.com (mail-yw0-x241.google.com [IPv6:2607:f8b0:4002:c05::241]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "Google Internet Authority G2" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id BCFED71E41 for ; Wed, 21 Mar 2018 00:29:56 +0000 (UTC) (envelope-from oliver.pinter@hardenedbsd.org) Received: by mail-yw0-x241.google.com with SMTP id y64so1160873ywa.3 for ; Tue, 20 Mar 2018 17:29:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=hardenedbsd-org.20150623.gappssmtp.com; s=20150623; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=naWPISMBntOCF1Gz9MHvS/z7ckjgwHEYm2ih96sWrbU=; b=k+HcYVWHjUG84CXKFifTMq9/uMoYBHaM/NOi05VPJAzhy+s7v4eT2zqate6PoaabbE oy+8lrvFYQeZmmg+0C5fmxc3S3SJVh2u20n14IQrhqaNHpspO2EuvroDTxG4ZZ1tNMP6 U9xuk12CR0hgRk2AOU1LstQh/IW++C4ih0XOtdewkngFKvYA1RfkjvnanoIYucmCbY2+ 5J7wTmdhwxh8e9gYdLuv/8SrStiFG9HWyW26ee0h9lmvKaqQtD4658H+tzJ3hXpo/yhc L2CPwuxRu390o+WRqIe/p+hNyCxn4DPu1cSKVSN9j5+qJJyBcl+ESZPEciPX8CDvDO9I 4GVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=naWPISMBntOCF1Gz9MHvS/z7ckjgwHEYm2ih96sWrbU=; b=FUlbLGDfdvNaZ+kSubRMNWBibT/54h3BbdPCcjARfeGcrUj/xZ4NRhA20S+yw3O/EE SECVPnDB+rY7zTebus9Nt9ePk4Bb3b9EfaemV78sYJAguGO9q7Fx/XlvX9aePqUCX5TI sBXm4tCnumnTnNQzy79T+vZfzo8qeskbbS/2DMvwBx7rVXX/7q6tydoJxoM9FALn1aWY euIu0N0CmuQ2rOMnnJxOeSa799Ia8ZH2ADz7KLBTdqmPm6UJ53s/iAB/fwi6tjSFe7TQ Ub/nQqn03VnuUA8i4sOROusrLvxNo37QgrmhBjivqWkg0QvX8TSbf7A1NMLuZ/nJ5r1h sdhA== X-Gm-Message-State: AElRT7GN+yDrhMUD6NDw76SAV/3HwMaTfIQsD4gf7OWIMLlsjkpGkGer RG65ayw8/5aphqCcooHJo0zzoy13N7dFYn/qTG1Fmg== X-Google-Smtp-Source: AG47ELvU8+6+ipOqV50KxQzHJ1wqzQGwCSl8aY7YDyYlsyFaK97q34wHgcqrS/TAeFwLWtu6BX+i5VU7zumvJQ8FYuE= X-Received: by 10.13.204.146 with SMTP id o140mr10950229ywd.265.1521592195937; Tue, 20 Mar 2018 17:29:55 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a25:61c5:0:0:0:0:0 with HTTP; Tue, 20 Mar 2018 17:29:55 -0700 (PDT) In-Reply-To: <201803201743.w2KHhoF9055658@repo.freebsd.org> References: <201803201743.w2KHhoF9055658@repo.freebsd.org> From: Oliver Pinter Date: Wed, 21 Mar 2018 01:29:55 +0100 Message-ID: Subject: Re: svn commit: r331252 - in head/sys/amd64: amd64 include To: Konstantin Belousov Cc: "src-committers@freebsd.org" , "svn-src-all@freebsd.org" , "svn-src-head@freebsd.org" Content-Type: text/plain; charset="UTF-8" X-Content-Filtered-By: Mailman/MimeDel 2.1.25 X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.25 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 21 Mar 2018 00:29:57 -0000 On Tuesday, March 20, 2018, Konstantin Belousov wrote: > Author: kib > Date: Tue Mar 20 17:43:50 2018 > New Revision: 331252 > URL: https://svnweb.freebsd.org/changeset/base/331252 > > Log: > Provide KPI for handling of rw/ro kernel text. > > This is a pure syntax patch to create an interface to enable and later > restore write access to the kernel text and other read-only mapped > regions. It is in line with e.g. vm_fault_disable_pagefaults() by > allowing the nesting. > > Discussed with: Peter Lei > Reviewed by: jtl > Sponsored by: The FreeBSD Foundation > MFC after: 1 week > Differential revision: https://reviews.freebsd.org/D14768 > > Modified: > head/sys/amd64/amd64/db_interface.c > head/sys/amd64/amd64/gdb_machdep.c > head/sys/amd64/amd64/machdep.c > head/sys/amd64/include/md_var.h > > Modified: head/sys/amd64/amd64/db_interface.c > ============================================================ > ================== > --- head/sys/amd64/amd64/db_interface.c Tue Mar 20 17:41:54 2018 > (r331251) > +++ head/sys/amd64/amd64/db_interface.c Tue Mar 20 17:43:50 2018 > (r331252) > @@ -37,6 +37,7 @@ __FBSDID("$FreeBSD$"); > #include > > #include > +#include > #include > > #include > @@ -75,19 +76,19 @@ db_write_bytes(vm_offset_t addr, size_t size, char *da > jmp_buf jb; > void *prev_jb; > char *dst; > - u_long cr0save; > + bool old_wp; > int ret; > > - cr0save = rcr0(); > + old_wp = false; This line should be old_wp = (rcr0() & CR0_WP) ? true : false; to preserve the old behavior in ret != 0 case. > prev_jb = kdb_jmpbuf(jb); > ret = setjmp(jb); > if (ret == 0) { > - load_cr0(cr0save & ~CR0_WP); > + old_wp = disable_wp(); > dst = (char *)addr; > while (size-- > 0) > *dst++ = *data++; > } > - load_cr0(cr0save); > + restore_wp(old_wp); > (void)kdb_jmpbuf(prev_jb); > return (ret); > } > > Modified: head/sys/amd64/amd64/gdb_machdep.c > ============================================================ > ================== > --- head/sys/amd64/amd64/gdb_machdep.c Tue Mar 20 17:41:54 2018 > (r331251) > +++ head/sys/amd64/amd64/gdb_machdep.c Tue Mar 20 17:43:50 2018 > (r331252) > @@ -39,6 +39,7 @@ __FBSDID("$FreeBSD$"); > #include > #include > #include > +#include > #include > #include > #include > @@ -127,17 +128,14 @@ gdb_cpu_signal(int type, int code) > void * > gdb_begin_write(void) > { > - u_long cr0save; > > - cr0save = rcr0(); > - load_cr0(cr0save & ~CR0_WP); > - return ((void *)cr0save); > + return (disable_wp() ? &gdb_begin_write : NULL); > } > > void > gdb_end_write(void *arg) > { > > - load_cr0((u_long)arg); > + restore_wp(arg != NULL); > } > > > Modified: head/sys/amd64/amd64/machdep.c > ============================================================ > ================== > --- head/sys/amd64/amd64/machdep.c Tue Mar 20 17:41:54 2018 > (r331251) > +++ head/sys/amd64/amd64/machdep.c Tue Mar 20 17:43:50 2018 > (r331252) > @@ -2597,6 +2597,31 @@ clear_pcb_flags(struct pcb *pcb, const u_int flags) > : "cc", "memory"); > } > > +/* > + * Enable and restore kernel text write permissions. > + * Callers must ensure that disable_wp()/restore_wp() are executed > + * without rescheduling on the same core. > + */ > +bool > +disable_wp(void) > +{ > + u_int cr0; > + > + cr0 = rcr0(); > + if ((cr0 & CR0_WP) == 0) > + return (false); > + load_cr0(cr0 & ~CR0_WP); > + return (true); > +} > + > +void > +restore_wp(bool old_wp) > +{ > + > + if (old_wp) > + load_cr0(rcr0() | CR0_WP); > +} > + > #ifdef KDB > > /* > > Modified: head/sys/amd64/include/md_var.h > ============================================================ > ================== > --- head/sys/amd64/include/md_var.h Tue Mar 20 17:41:54 2018 > (r331251) > +++ head/sys/amd64/include/md_var.h Tue Mar 20 17:43:50 2018 > (r331252) > @@ -53,6 +53,8 @@ void amd64_conf_fast_syscall(void); > void amd64_db_resume_dbreg(void); > void amd64_lower_shared_page(struct sysentvec *); > void amd64_syscall(struct thread *td, int traced); > +bool disable_wp(void); > +void restore_wp(bool old_wp); > void doreti_iret(void) __asm(__STRING(doreti_iret)); > void doreti_iret_fault(void) __asm(__STRING(doreti_iret_fault)); > void ld_ds(void) __asm(__STRING(ld_ds)); > _______________________________________________ > svn-src-head@freebsd.org mailing list > https://lists.freebsd.org/mailman/listinfo/svn-src-head > To unsubscribe, send any mail to "svn-src-head-unsubscribe@freebsd.org" >