Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp4551848ybb; Tue, 24 Mar 2020 00:31:43 -0700 (PDT) X-Google-Smtp-Source: ADFU+vt2SJcCWSAB5qcsVKN7iTx5GU199U/abeheSlc4JXLRidG8XfxTwE99jbmX/bsrOpar8Hhr X-Received: by 2002:a05:6830:118d:: with SMTP id u13mr19642204otq.41.1585035103347; Tue, 24 Mar 2020 00:31:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1585035103; cv=none; d=google.com; s=arc-20160816; b=ZMy68W2KJYI3ZVXmMEA42LSAM3xwZmJLnRaNcT8/GEix+4dq11hQsDIw3+uC98H5vD h+dD4urvD4ZrFBVTz2PMDUyqFyhQXDdGWm+K5cUFJOmPfgKO9BWGvCHJokrUuirwLX9n Hm6scUkUFAdttSgQHXcO5Bta0JwAzqJHWBjnL2EJiZphu9z98LI7kANPhYiTCb8L8Q8d r9CQbsS/TaPCsG8Qmm+0wZllnNmbe9DSwWhNuvkMqAvDt8wcPFysrehycIsYkJBeD5pu 3aRWkTInHZMBzWXnyGz64iOPtnONw2kYQ0AcGdHRshv5BQUbP6U34MNC4WBvvz6qbGXz +MUA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=klBEcTaZTM89My6NFvl8X/nEuMTFhvIx/ugZCt3MXO4=; b=h/XuZJAOs5y1OGoObM0XCONtO74WaB9Weg0OJwWMR3JTv7X/ToSEU3iY69vhSbJJA2 SIkyRXrHk0FamYuZC7612ry1927UdZ0eoLQxjbZRGev5LP7v6+iRRogCDyugckAVYtEz pW+7GZ5r67Ea/Qn7yKV0lWxRq/dveZmhZxtyol0UPUCEaStDk1q0Idu05/ETXbqgCHCq 2/JVOYOntElTzwb1ldev3qZsrxIdTK3tVR8uGGfahW+7dbG35rkbPUJA3L9zBHKQazFr ugyyJK0YZIN0US3I4bP6k3q6MaMZxtColuDq8p6/D8VBqjjrbg3GNWktC7IM3wCkkA+5 UUYA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b="AG+9uC/8"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l20si9528568otn.37.2020.03.24.00.31.31; Tue, 24 Mar 2020 00:31:43 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b="AG+9uC/8"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727504AbgCXHbG (ORCPT + 99 others); Tue, 24 Mar 2020 03:31:06 -0400 Received: from mail-pj1-f67.google.com ([209.85.216.67]:40541 "EHLO mail-pj1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727443AbgCXHbB (ORCPT ); Tue, 24 Mar 2020 03:31:01 -0400 Received: by mail-pj1-f67.google.com with SMTP id kx8so1045949pjb.5 for ; Tue, 24 Mar 2020 00:31:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=klBEcTaZTM89My6NFvl8X/nEuMTFhvIx/ugZCt3MXO4=; b=AG+9uC/8nRSOJB+P5ypaf3gqmhSCEHjRpdinGoG0ihkvqs00uicArdKU/RauncEVNS /jr4FM3waJ7m3kgA0/YlE9Y/s2AXXDgjbcVvws87Q+asnKzmm0lO8BVXx4qHBuS+Zdac utC7Is/6yjR+1eC5kX1thCIKZAwGmuq+3OJT6bOgObDpguY3PMR3hGE+qaVesFwDMmx6 3g/jgTiZ2VJSx0ct7YhuzgCBmxpGWsUXxl+v5d4+UMOczw0qWBkSS6RxXqATJlLs63bp TnETxvwb0viDn668uejcqivlG5/kLXFfhOtcIHeJvDtfYmKios0w0AS4V3YtK6DnRdye zAcA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=klBEcTaZTM89My6NFvl8X/nEuMTFhvIx/ugZCt3MXO4=; b=jhQ794TeEUkpqkZbLo+PNMWJLox6G/2eJPtOOPfRgUozqjKSF8BwIH+eB9m9Mt5SAA LothuVm3Hpv+0cfGvQlwYpI02YJEBHU7jVND9RIPOiRtz7Ahew+z0R49oNjfAlQeI21E K9fUxUPHTSBTNg7fhxNAr4uTC3zPLOxOnpw4KWhTUtl0CrxPKGl/sC5ejwTrPTiCdaJL p7XzNIzyzviIEqU6K167oIzcNg4gmZpE89nqgInoRUBZbNUH1Fr792wn62jUIygskJDc GCePnrtNtj6r+d2/hGtF5DA/Xx53APlBDiJ0cyaDp+O91NqwLCgdqmmDPuYwfYSMgVpz kAEQ== X-Gm-Message-State: ANhLgQ0H7sph6a/xN9Q+cm1Jph0egQRrpsiXJdjR5DrG2xbbzXMo3KnO XLre0T5/uDHmXPp8W1QeUbvwEw== X-Received: by 2002:a17:90a:628a:: with SMTP id d10mr3826207pjj.25.1585035060570; Tue, 24 Mar 2020 00:31:00 -0700 (PDT) Received: from hsinchu02.internal.sifive.com (220-132-236-182.HINET-IP.hinet.net. [220.132.236.182]) by smtp.gmail.com with ESMTPSA id i187sm15124648pfg.33.2020.03.24.00.30.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Mar 2020 00:31:00 -0700 (PDT) From: Zong Li To: palmer@dabbelt.com, paul.walmsley@sifive.com, alex@ghiti.fr, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Cc: Zong Li Subject: [PATCH RFC 2/8] riscv/kaslr: introduce functions to clear page table Date: Tue, 24 Mar 2020 15:30:47 +0800 Message-Id: <286a940ef8fbc4480c63679271eea440d167a258.1584352425.git.zong.li@sifive.com> X-Mailer: git-send-email 2.25.2 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In KASLR, we need to re-create page table after getting a random destination. Introduce clear function to clear old content. Also, the page table entries allow writing value when it's empty, so we have to clear the early page table. This patch is a preparation to support KASLR. Signed-off-by: Zong Li --- arch/riscv/mm/init.c | 54 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c index ace5d74fd939..51e263c04fa2 100644 --- a/arch/riscv/mm/init.c +++ b/arch/riscv/mm/init.c @@ -315,6 +315,7 @@ static void __init create_pmd_mapping(pmd_t *pmdp, #define get_pgd_next_virt(__pa) get_pmd_virt(__pa) #define create_pgd_next_mapping(__nextp, __va, __pa, __sz, __prot) \ create_pmd_mapping(__nextp, __va, __pa, __sz, __prot) +#define clear_pgd_next_mapping(__nextp) clear_pmd(__nextp) #define fixmap_pgd_next fixmap_pmd #else #define pgd_next_t pte_t @@ -322,6 +323,7 @@ static void __init create_pmd_mapping(pmd_t *pmdp, #define get_pgd_next_virt(__pa) get_pte_virt(__pa) #define create_pgd_next_mapping(__nextp, __va, __pa, __sz, __prot) \ create_pte_mapping(__nextp, __va, __pa, __sz, __prot) +#define clear_pgd_next_mapping(__nextp) clear_pte(__nextp) #define fixmap_pgd_next fixmap_pte #endif @@ -361,6 +363,58 @@ static uintptr_t __init best_map_size(phys_addr_t base, phys_addr_t size) return PMD_SIZE; } +#ifdef CONFIG_RANDOMIZE_BASE +static void __init clear_pte(pte_t *ptep) +{ + unsigned int i; + + for (i = 0; i < PTRS_PER_PTE; i++) + if (!pte_none(ptep[i])) + ptep[i] = __pte(0); +} + +static void __init clear_pmd(pmd_t *pmdp) +{ + unsigned int i; + pte_t *ptep; + phys_addr_t pte_phys; + uintptr_t kaslr_offset = get_kaslr_offset(); + + for (i = 0; i < PTRS_PER_PMD; i++) + if (!pmd_none(pmdp[i])) { + if (pmd_leaf(pmdp[i])) { + pmd_clear(&pmdp[i]); + } else { + pte_phys = PFN_PHYS(_pmd_pfn(pmdp[i])); + ptep = get_pte_virt(pte_phys + kaslr_offset); + clear_pte(ptep); + pmd_clear(&pmdp[i]); + } + } +} + +static void __init clear_pgd(pgd_t *pgdp) +{ + unsigned int i; + pgd_next_t *nextp; + phys_addr_t next_phys; + uintptr_t kaslr_offset = get_kaslr_offset(); + + for (i = 0; i < PTRS_PER_PGD; i++) + if (pgd_val(pgdp[i]) != 0) { + if (pgd_leaf(pgd_val(pgdp[i]))) { + set_pgd(&pgdp[i], __pgd(0)); + } else { + next_phys = PFN_PHYS(_pgd_pfn(pgdp[i])); + nextp = get_pgd_next_virt(next_phys + + kaslr_offset); + clear_pgd_next_mapping(nextp); + set_pgd(&pgdp[i], __pgd(0)); + } + } +} +#endif + /* * setup_vm() is called from head.S with MMU-off. * -- 2.25.1