Received: by 2002:a25:d7c1:0:0:0:0:0 with SMTP id o184csp1280103ybg; Fri, 18 Oct 2019 15:08:11 -0700 (PDT) X-Google-Smtp-Source: APXvYqz+eV3kYvZBRjFRWbJoLipOWMAQ8G/z8N3IlMl+054ohsE64dcbOubtsPDBxiqxus5hra0Z X-Received: by 2002:a50:f296:: with SMTP id f22mr12255472edm.69.1571436491719; Fri, 18 Oct 2019 15:08:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1571436491; cv=none; d=google.com; s=arc-20160816; b=0gOvrAU8r5UTYAvbiSZr/E7egIhVZtN+m6M42gNFjQMCsMww65Gzc+Fel0K2zFLYeM K+BdG42usQs2DaHZVW+Z+DX0bR/HeBXrM+2GG0K8QO+fsJMoIBaBTt+KlrCGjGUeEPvF T/wrBQrOCT4qsXP4SEpenEQIpollT3BEYI3z6DadgEzDopxDxzRXgQncCsiFaS4+HjXz rIPFHB17TdT+cf0UhGdSLFG7W3v8DBSZWFVEivWnS1fCWz1DD0NPxYI7VDcnm4XFT8C7 Gf7Sb097rPG7SrkmoYIWtNLioCg/6wZ5XpRhRorC407Y1vOll6l7MiVbhtsQF1Ds7byt ot3Q== 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=beV2TrydHrYLZOLTznLZKJeyXBjPaXx2Er11cQ1xqkA=; b=J6qw5Xi+AJQFWs5w+Zlz4zuxbx3LkYNkwR+wppZziIMed23UmXtykxtG9r5uVhKuyi id6U4g1sXItShY3WiqIdEaEQI1VgOcjFt/VaOX9YwDPUv4ub2o6HLkjVe5RBOuyyzrZe HhgxHXUHo/x3AgH/AzdUbmdK0LWg1keDXzzZouzIsjQddyGBO+O2YVHrHbyosmmXdoND 4e4D1xXdTwGnF3td+hWqFSZHkBbqgygVa3YRz5IEE/CqoQZ6rlCAWE7guxhKHQSJEKfI 0QOfii1efCdO8XNlwUDYlgZn9Pl+jPRqQIsU6Vv5cQWCtBGG6a0rONDxGlodJYlHiTlF YyYg== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=J7Oc2tPA; 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 m33si4751030edc.94.2019.10.18.15.07.49; Fri, 18 Oct 2019 15:08:11 -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=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=J7Oc2tPA; 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 S2503127AbfJQRiX (ORCPT + 99 others); Thu, 17 Oct 2019 13:38:23 -0400 Received: from bombadil.infradead.org ([198.137.202.133]:37842 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2503117AbfJQRiT (ORCPT ); Thu, 17 Oct 2019 13:38:19 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender :Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From :Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help: List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=beV2TrydHrYLZOLTznLZKJeyXBjPaXx2Er11cQ1xqkA=; b=J7Oc2tPAZn7jV+kT/yfMdDRbmc tnkCInRWuy5HpFKdYrhvLMIIYvQXpX8nqkutcL3vnGIk3GZlGZ/mTD6J9t872R0LAlya+bMULb3te nPIix4Jz2WZva7tgqfCxVhbZ6pc21tIgcuIxM5UOBpkBUDyr6d5muuDHLnEO+Pd3Om0jh7HBZWcD8 0J7mT4E5skGJD1y6Nk0SHtjs/nBI7ohOv0rpg/ai3eNp1/SrcwMAAts2TmQCg6y0jcxFyEpGDSAYC iHsYwp//40OWx6wlD3n2q2i6QUxgmZNcdCqJfbcAkEdlXFL1zOVGVnYG0VHNsUKRZFhB5slTEPn0s 2zxo7HVw==; Received: from [2001:4bb8:18c:d7b:c70:4a89:bc61:3] (helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.92.3 #3 (Red Hat Linux)) id 1iL9j5-00081a-Qb; Thu, 17 Oct 2019 17:38:16 +0000 From: Christoph Hellwig To: Palmer Dabbelt , Paul Walmsley Cc: Damien Le Moal , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH 12/15] riscv: clear the instruction cache and all registers when booting Date: Thu, 17 Oct 2019 19:37:40 +0200 Message-Id: <20191017173743.5430-13-hch@lst.de> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191017173743.5430-1-hch@lst.de> References: <20191017173743.5430-1-hch@lst.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When we get booted we want a clear slate without any leaks from previous supervisors or the firmware. Flush the instruction cache and then clear all registers to known good values. This is really important for the upcoming nommu support that runs on M-mode, but can't really harm when running in S-mode either. Vaguely based on the concepts from opensbi. Signed-off-by: Christoph Hellwig --- arch/riscv/include/asm/csr.h | 1 + arch/riscv/kernel/head.S | 88 +++++++++++++++++++++++++++++++++++- 2 files changed, 88 insertions(+), 1 deletion(-) diff --git a/arch/riscv/include/asm/csr.h b/arch/riscv/include/asm/csr.h index d0b5113e1a54..ee0101278608 100644 --- a/arch/riscv/include/asm/csr.h +++ b/arch/riscv/include/asm/csr.h @@ -83,6 +83,7 @@ /* symbolic CSR names: */ #define CSR_MHARTID 0xf14 #define CSR_MSTATUS 0x300 +#define CSR_MISA 0x301 #define CSR_MIE 0x304 #define CSR_MTVEC 0x305 #define CSR_MSCRATCH 0x340 diff --git a/arch/riscv/kernel/head.S b/arch/riscv/kernel/head.S index 583784cb3a32..25867b99cc95 100644 --- a/arch/riscv/kernel/head.S +++ b/arch/riscv/kernel/head.S @@ -11,6 +11,7 @@ #include #include #include +#include #include __INIT @@ -51,12 +52,18 @@ _start_kernel: csrw CSR_XIP, zero #ifdef CONFIG_RISCV_M_MODE + /* flush the instruction cache */ + fence.i + + /* Reset all registers except ra, a0, a1 */ + call reset_regs + /* * The hartid in a0 is expected later on, and we have no firmware * to hand it to us. */ csrr a0, CSR_MHARTID -#endif +#endif /* CONFIG_RISCV_M_MODE */ /* Load the global pointer */ .option push @@ -203,6 +210,85 @@ relocate: j .Lsecondary_park END(_start) +#ifdef CONFIG_RISCV_M_MODE +ENTRY(reset_regs) + li sp, 0 + li gp, 0 + li tp, 0 + li t0, 0 + li t1, 0 + li t2, 0 + li s0, 0 + li s1, 0 + li a2, 0 + li a3, 0 + li a4, 0 + li a5, 0 + li a6, 0 + li a7, 0 + li s2, 0 + li s3, 0 + li s4, 0 + li s5, 0 + li s6, 0 + li s7, 0 + li s8, 0 + li s9, 0 + li s10, 0 + li s11, 0 + li t3, 0 + li t4, 0 + li t5, 0 + li t6, 0 + csrw sscratch, 0 + +#ifdef CONFIG_FPU + csrr t0, CSR_MISA + andi t0, t0, (COMPAT_HWCAP_ISA_F | COMPAT_HWCAP_ISA_D) + bnez t0, .Lreset_regs_done + + li t1, SR_FS + csrs CSR_XSTATUS, t1 + fmv.s.x f0, zero + fmv.s.x f1, zero + fmv.s.x f2, zero + fmv.s.x f3, zero + fmv.s.x f4, zero + fmv.s.x f5, zero + fmv.s.x f6, zero + fmv.s.x f7, zero + fmv.s.x f8, zero + fmv.s.x f9, zero + fmv.s.x f10, zero + fmv.s.x f11, zero + fmv.s.x f12, zero + fmv.s.x f13, zero + fmv.s.x f14, zero + fmv.s.x f15, zero + fmv.s.x f16, zero + fmv.s.x f17, zero + fmv.s.x f18, zero + fmv.s.x f19, zero + fmv.s.x f20, zero + fmv.s.x f21, zero + fmv.s.x f22, zero + fmv.s.x f23, zero + fmv.s.x f24, zero + fmv.s.x f25, zero + fmv.s.x f26, zero + fmv.s.x f27, zero + fmv.s.x f28, zero + fmv.s.x f29, zero + fmv.s.x f30, zero + fmv.s.x f31, zero + csrw fcsr, 0 + /* note that the caller must clear SR_FS */ +#endif /* CONFIG_FPU */ +.Lreset_regs_done: + ret +END(reset_regs) +#endif /* CONFIG_RISCV_M_MODE */ + __PAGE_ALIGNED_BSS /* Empty zero page */ .balign PAGE_SIZE -- 2.20.1