From patchwork Thu Sep 1 07:48:44 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rui Teng X-Patchwork-Id: 664814 Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [103.22.144.68]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3sPvYx3HrSz9s5w for ; Thu, 1 Sep 2016 17:50:29 +1000 (AEST) Received: from ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 3sPvYx2F3XzDrps for ; Thu, 1 Sep 2016 17:50:29 +1000 (AEST) X-Original-To: linuxppc-dev@lists.ozlabs.org Delivered-To: linuxppc-dev@lists.ozlabs.org Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 3sPvXn1Mg5zDrgX for ; Thu, 1 Sep 2016 17:49:28 +1000 (AEST) Received: from pps.filterd (m0098399.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.17/8.16.0.17) with SMTP id u817hKBN125059 for ; Thu, 1 Sep 2016 03:49:26 -0400 Received: from e36.co.us.ibm.com (e36.co.us.ibm.com [32.97.110.154]) by mx0a-001b2d01.pphosted.com with ESMTP id 256cqbsfnu-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Thu, 01 Sep 2016 03:49:26 -0400 Received: from localhost by e36.co.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Thu, 1 Sep 2016 01:49:24 -0600 Received: from d03dlp03.boulder.ibm.com (9.17.202.179) by e36.co.us.ibm.com (192.168.1.136) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Thu, 1 Sep 2016 01:49:21 -0600 X-IBM-Helo: d03dlp03.boulder.ibm.com X-IBM-MailFrom: rui.teng@linux.vnet.ibm.com Received: from b01cxnp23033.gho.pok.ibm.com (b01cxnp23033.gho.pok.ibm.com [9.57.198.28]) by d03dlp03.boulder.ibm.com (Postfix) with ESMTP id 761C319D8041; Thu, 1 Sep 2016 01:48:52 -0600 (MDT) Received: from b01ledav005.gho.pok.ibm.com (b01ledav005.gho.pok.ibm.com [9.57.199.110]) by b01cxnp23033.gho.pok.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id u817nM5O14877044; Thu, 1 Sep 2016 07:49:22 GMT Received: from b01ledav005.gho.pok.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 9847FAE058; Thu, 1 Sep 2016 03:49:20 -0400 (EDT) Received: from localhost (unknown [9.123.228.123]) by b01ledav005.gho.pok.ibm.com (Postfix) with ESMTP id 4BFCCAE04B; Thu, 1 Sep 2016 03:49:20 -0400 (EDT) From: Rui Teng To: linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org Subject: [PATCH] [V3] powerpc/mm: Add validation for platform reserved memory ranges Date: Thu, 1 Sep 2016 15:48:44 +0800 X-Mailer: git-send-email 2.7.4 X-TM-AS-GCONF: 00 X-Content-Scanned: Fidelis XPS MAILER x-cbid: 16090107-0020-0000-0000-000009AF1FD4 X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00005691; HX=3.00000240; KW=3.00000007; PH=3.00000004; SC=3.00000184; SDB=6.00752384; UDB=6.00355728; IPR=6.00525228; BA=6.00004683; NDR=6.00000001; ZLA=6.00000005; ZF=6.00000009; ZB=6.00000000; ZP=6.00000000; ZH=6.00000000; ZU=6.00000002; MB=3.00012543; XFM=3.00000011; UTC=2016-09-01 07:49:23 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 16090107-0021-0000-0000-0000551685F1 Message-Id: <1472716124-3344-1-git-send-email-rui.teng@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2016-09-01_02:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 suspectscore=0 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1604210000 definitions=main-1609010086 X-BeenThere: linuxppc-dev@lists.ozlabs.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: Linux on PowerPC Developers Mail List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Rui Teng , Anshuman Khandual , Paul Mackerras Errors-To: linuxppc-dev-bounces+patchwork-incoming=ozlabs.org@lists.ozlabs.org Sender: "Linuxppc-dev" From: Anshuman Khandual For partition running on PHYP, there can be a adjunct partition which shares the virtual address range with the operating system. Virtual address ranges which can be used by the adjunct partition are communicated with virtual device node of the device tree with a property known as "ibm,reserved-virtual-addresses". This patch introduces a new function named 'validate_reserved_va_range' which is called during initialization to validate that these reserved virtual address ranges do not overlap with the address ranges used by the kernel for all supported memory contexts. This helps prevent the possibility of getting return codes similar to H_RESOURCE for H_PROTECT hcalls for conflicting HPTE entries. Signed-off-by: Anshuman Khandual Signed-off-by: Rui Teng --- - Tested on both POWER8 LE and BE platforms Changes in V3: - Use u32 and u64 to store the virtual address and use CPU endian mask. Changes in V2: - Added braces to the definition of LINUX_VA_BITS - Adjusted tabs as spaces for the definition of PARTIAL_LINUX_VA_MASK --- arch/powerpc/mm/hash_utils_64.c | 68 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/arch/powerpc/mm/hash_utils_64.c b/arch/powerpc/mm/hash_utils_64.c index 0821556..85c5123 100644 --- a/arch/powerpc/mm/hash_utils_64.c +++ b/arch/powerpc/mm/hash_utils_64.c @@ -1733,3 +1733,71 @@ void hash__setup_initial_memory_limit(phys_addr_t first_memblock_base, /* Finally limit subsequent allocations */ memblock_set_current_limit(ppc64_rma_size); } + +/* + * PAPR says that each reserved virtual address range record + * contains three be32 elements which is of toal 12 bytes. + * First two be32 elements contain the abbreviated virtual + * address (high order 32 bits and low order 32 bits that + * generate the abbreviated virtual address of 64 bits which + * need to be concatenated with 24 bits of 0 at the end) and + * the third be32 element contains the size of the reserved + * virtual address range as number of consecutive 4K pages. + */ +struct reserved_va_record { + u32 high_addr; + u32 low_addr; + u32 nr_pages_4K; +}; + +/* + * Linux uses 65 bits (CONTEXT_BITS + ESID_BITS + SID_SHIFT) + * of virtual address. As reserved virtual address comes in + * as an abbreviated form (64 bits) from the device tree, we + * will use a partial address bit mask (65 >> 24) to match it + * for simplicity. + */ +#define RVA_LESS_BITS 24 +#define LINUX_VA_BITS (CONTEXT_BITS + ESID_BITS + SID_SHIFT) +#define PARTIAL_LINUX_VA_MASK ((1ULL << (LINUX_VA_BITS - RVA_LESS_BITS)) - 1) + +static int __init validate_reserved_va_range(void) +{ + struct reserved_va_record rva; + struct device_node *np; + int records, i; + u64 vaddr; + + np = of_find_node_by_name(NULL, "vdevice"); + if (!np) + return -ENODEV; + + records = of_property_count_elems_of_size(np, + "ibm,reserved-virtual-addresses", + sizeof(struct reserved_va_record)); + if (records < 0) + return records; + + for (i = 0; i < records; i++) { + of_property_read_u32_index(np, + "ibm,reserved-virtual-addresses", + 3 * i, &rva.high_addr); + of_property_read_u32_index(np, + "ibm,reserved-virtual-addresses", + 3 * i + 1, &rva.low_addr); + of_property_read_u32_index(np, + "ibm,reserved-virtual-addresses", + 3 * i + 2, &rva.nr_pages_4K); + + vaddr = rva.high_addr; + vaddr = (vaddr << 32) | rva.low_addr; + if (unlikely(!(vaddr & ~PARTIAL_LINUX_VA_MASK))) { + pr_err("RVA [0x%llx000000 (0x%x in bytes)] overlapped\n", + vaddr, rva.nr_pages_4K * 4096); + BUG(); + } + } + of_node_put(np); + return 0; +} +device_initcall(validate_reserved_va_range);