Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp934124ybl; Wed, 29 Jan 2020 12:14:27 -0800 (PST) X-Google-Smtp-Source: APXvYqzOMHylHDvzFgw79p17h0pBg04dO30jsNgnXhVuszi9KJeUc3GZe3QDuRPsDTqr+W44pPr5 X-Received: by 2002:aca:4587:: with SMTP id s129mr486935oia.124.1580328866825; Wed, 29 Jan 2020 12:14:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1580328866; cv=none; d=google.com; s=arc-20160816; b=JfJqVDCP1QeLdPFoWmGpew/ZTowaC9zEbEto7b5GfBR/qFP9ip1GMKEB50QBSHAD0g d9dkpYvKu+y4oB9E00wFKFg4opUzEp9/djHJgOjT/O3awZJO6336eGAoR5okmD3RmH4D HK7FlcItCkbODTrSXu89zwUMaTboFOnorGrEAtGzuN/8oifR6iBmhducUK/2Y4wSFR2W I8I+Vu3G8S968oOecAPMsbwUBWo1eik60vW9M7edqJ1PJV6COEA82mHwTPTh5B5z1Avh 10wcmwpZ4tISt17xVxNh6+egDbj3hdSvtsRzmP6SiHbqopZ9QGILuAGs2EDZLESvd6A3 Qa+Q== 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 :message-id:references:in-reply-to:to:cc:subject:from:date :dkim-signature; bh=5dpkdxJKo0D3Co0IYX0+FJAqVgwjrCjxGckzFnAFk30=; b=XowA8VYaw9wPyhvNSCpVQelj6smQ4aAjNm5cNDcVUXgzvZQdj7oAOzRCKqeFxvc5/r kh7Ib5iobDsMXj/6QVW9Z9Ye1Ly2Gb/EIbtNO3Y9D5sjA/XlEe2sK/RP2PDDBYvJs+dP lYD1u9BIMleIQVlW9BzQ1W1g+m4IBZncIlkP+IPBet7U58A/Z3+FTMctbAX4bHNLg1oF aiFhTq2DV+PDub2Ic89VSFZvx9EUItAaNHIjLhr9xe+pmThMkDZWBljeUN6semXERR+8 3z0OZ93hPo9o1rrVB5Lt+v3ki9OcfBeIo+zB07M47UxdOVfI+OoSjmFgCHZtSK4J+cEk jQLA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b="EKGknQz/"; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w17si577863oiw.127.2020.01.29.12.14.13; Wed, 29 Jan 2020 12:14:26 -0800 (PST) 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=@google.com header.s=20161025 header.b="EKGknQz/"; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727020AbgA2TX3 (ORCPT + 99 others); Wed, 29 Jan 2020 14:23:29 -0500 Received: from mail-wr1-f65.google.com ([209.85.221.65]:41636 "EHLO mail-wr1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726171AbgA2TX3 (ORCPT ); Wed, 29 Jan 2020 14:23:29 -0500 Received: by mail-wr1-f65.google.com with SMTP id c9so829432wrw.8 for ; Wed, 29 Jan 2020 11:23:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:subject:cc:to:in-reply-to:references:message-id :mime-version:content-transfer-encoding; bh=5dpkdxJKo0D3Co0IYX0+FJAqVgwjrCjxGckzFnAFk30=; b=EKGknQz/CJF9qnizztY4apE1qf9rCi/BQDgUenjew4SIgrWKi7BReyO8h+UGAdhrYw +Om6yZO4ovjeY9v67bWESViU+IsvZ8HyaEaE8cVc+0BIMVUAo6pRQ55FStSyd/BYDqlU bUiC48/ZBmkbs0+KJiByUylNXnmiD6il31j5JkS0xOP0E68ZexmQ1DU1dQ3ge5fmfnTs FfqUMgb/Wl2QkE7kw+/tzmAk3en+eyjpfzbkAnhQVStl8SCKgcHwtN41oP/eL63zwKXW 3IW5aRzfYkSSU5o5k2PlTeY8fsX1S1uYyffVSKAzLcWGB+NEbRmVrKXsd+aG2W0Fd2GN ti3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:subject:cc:to:in-reply-to:references :message-id:mime-version:content-transfer-encoding; bh=5dpkdxJKo0D3Co0IYX0+FJAqVgwjrCjxGckzFnAFk30=; b=CL9uaJO/EcDk3B/GhwBFZ8xPiCjPO/9dWZNAzbAA2LNX1d2sU+jpD6Yh1TXpDLj1+z Whn13musbH8MUcsJl06ijqpetCX3BXF5YRiV9Hpbz+XqkAhyGBYNNKxGSHpjGxioUNrU 58EjOCn57CTB7pH4mYl2HZl8y+MbninHEk2XNUjAJ0GJu62SdbeZ6QojDyJ5P+kbXcpR 13VToIh+e52ysAXXVS5mbKJVm3swGLwzwlTUQF3GAfIWYSIWb+a7yHrL+E27KRXTRnLh IX3fBqys/I+ogKuEfy7RCqjv0mosIVgfzd/OdEIfQmxxZIAE6w8Mkp1GGGM8FuUz1k/0 UnRg== X-Gm-Message-State: APjAAAWYx2Vx2+YcDXTXubD5+obUieMFuO5z5nlrIGbWhDVSuSr8iUg5 w40hQSp59Ty+xflH4JYZ4Kt3ZQ== X-Received: by 2002:adf:e78a:: with SMTP id n10mr400841wrm.62.1580325806962; Wed, 29 Jan 2020 11:23:26 -0800 (PST) Received: from localhost ([2a00:79e0:d:11:1da2:3fd4:a302:4fff]) by smtp.gmail.com with ESMTPSA id r6sm970807wrp.95.2020.01.29.11.23.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Jan 2020 11:23:26 -0800 (PST) Date: Wed, 29 Jan 2020 11:23:26 -0800 (PST) X-Google-Original-Date: Wed, 29 Jan 2020 19:23:14 GMT (+0000) From: Palmer Dabbelt X-Google-Original-From: Palmer Dabbelt Subject: Re: [PATCH] riscv: set pmp configuration if kernel is running in M-mode CC: green.hu@gmail.com, greentime@kernel.org, Paul Walmsley , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, greentime.hu@sifive.com To: greentime.hu@sifive.com In-Reply-To: <20200109031740.29717-1-greentime.hu@sifive.com> References: <20200109031740.29717-1-greentime.hu@sifive.com> Message-ID: Mime-Version: 1.0 (MHng) Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, 09 Jan 2020 03:17:40 GMT (+0000), greentime.hu@sifive.com wrote: > When the kernel is running in S-mode, the expectation is that the > bootloader or SBI layer will configure the PMP to allow the kernel to > access physical memory. But, when the kernel is running in M-mode and is > started with the ELF "loader", there's probably no bootloader or SBI layer > involved to configure the PMP. Thus, we need to configure the PMP > ourselves to enable the kernel to access all regions. > > Signed-off-by: Greentime Hu > --- > arch/riscv/include/asm/csr.h | 12 ++++++++++++ > arch/riscv/kernel/head.S | 6 ++++++ > 2 files changed, 18 insertions(+) > > diff --git a/arch/riscv/include/asm/csr.h b/arch/riscv/include/asm/csr.h > index 0a62d2d68455..0f25e6c4e45c 100644 > --- a/arch/riscv/include/asm/csr.h > +++ b/arch/riscv/include/asm/csr.h > @@ -72,6 +72,16 @@ > #define EXC_LOAD_PAGE_FAULT 13 > #define EXC_STORE_PAGE_FAULT 15 > > +/* PMP configuration */ > +#define PMP_R 0x01 > +#define PMP_W 0x02 > +#define PMP_X 0x04 > +#define PMP_A 0x18 > +#define PMP_A_TOR 0x08 > +#define PMP_A_NA4 0x10 > +#define PMP_A_NAPOT 0x18 > +#define PMP_L 0x80 > + > /* symbolic CSR names: */ > #define CSR_CYCLE 0xc00 > #define CSR_TIME 0xc01 > @@ -100,6 +110,8 @@ > #define CSR_MCAUSE 0x342 > #define CSR_MTVAL 0x343 > #define CSR_MIP 0x344 > +#define CSR_PMPCFG0 0x3a0 > +#define CSR_PMPADDR0 0x3b0 > #define CSR_MHARTID 0xf14 > > #ifdef CONFIG_RISCV_M_MODE > diff --git a/arch/riscv/kernel/head.S b/arch/riscv/kernel/head.S > index 5c8b24bf4e4e..f8f996916c5b 100644 > --- a/arch/riscv/kernel/head.S > +++ b/arch/riscv/kernel/head.S > @@ -60,6 +60,12 @@ _start_kernel: > /* Reset all registers except ra, a0, a1 */ > call reset_regs > > + /* Setup a PMP to permit access to all of memory. */ > + li a0, -1 > + csrw CSR_PMPADDR0, a0 > + li a0, (PMP_A_NAPOT | PMP_R | PMP_W | PMP_X) > + csrw CSR_PMPCFG0, a0 These should be guarded by some sort of #ifdef CONFIG_M_MODE, as they're not part of S mode. > + > /* > * The hartid in a0 is expected later on, and we have no firmware > * to hand it to us.