From patchwork Tue Mar 30 21:35:56 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anthony Yznaga X-Patchwork-Id: 12173801 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 482BAC433DB for ; Tue, 30 Mar 2021 21:27:02 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id CB2C0619CA for ; Tue, 30 Mar 2021 21:27:01 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org CB2C0619CA Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=oracle.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 6BAF76B00A7; Tue, 30 Mar 2021 17:27:01 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 66C1C6B00A8; Tue, 30 Mar 2021 17:27:01 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 470A76B00A9; Tue, 30 Mar 2021 17:27:01 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0207.hostedemail.com [216.40.44.207]) by kanga.kvack.org (Postfix) with ESMTP id 267096B00A7 for ; Tue, 30 Mar 2021 17:27:01 -0400 (EDT) Received: from smtpin37.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id C9E7962CC for ; Tue, 30 Mar 2021 21:27:00 +0000 (UTC) X-FDA: 77977825800.37.645FDD6 Received: from userp2130.oracle.com (userp2130.oracle.com [156.151.31.86]) by imf10.hostedemail.com (Postfix) with ESMTP id 7B94540002C8 for ; Tue, 30 Mar 2021 21:26:59 +0000 (UTC) Received: from pps.filterd (userp2130.oracle.com [127.0.0.1]) by userp2130.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 12ULP4EA145279; Tue, 30 Mar 2021 21:26:34 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-type : mime-version; s=corp-2020-01-29; bh=5gs3kt58j9UHE+if8NBT4rT44pX3k6PV7VfaMeNhiWw=; b=cqvkyZE4IhinoTwc7pt0xiI1K0bYprXLSttf7cvCemG9SzXQlWtuAF+QWCvTICBZd4iX JG95kTeVZg7LRR+WBNYlaSAzs5NCGKmOrPHuiDmpF7pPS246cShX/00+MSypHWzL9bmL Iuq3nCiu0pmHuEWOD33YhmikiMHOcTFO3VeIyoMVjRGCiSGJoSsrlW2XBcxR/MkqsGCe u2IZ4RAVnrUGTahinKNRegXw58HDN0NmumclCWgKMDrWqKkv7YvNG7PKe/05mTXlX6th TA0Dx2avnI75DdCmVKi0KLKZCBiv53cYtDBsWslDL8I89s3NRwpB+RdUjGbMJifMkyPX Fg== Received: from aserp3020.oracle.com (aserp3020.oracle.com [141.146.126.70]) by userp2130.oracle.com with ESMTP id 37mad9r8g6-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 30 Mar 2021 21:26:34 +0000 Received: from pps.filterd (aserp3020.oracle.com [127.0.0.1]) by aserp3020.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 12ULOmHt183931; Tue, 30 Mar 2021 21:26:33 GMT Received: from nam10-mw2-obe.outbound.protection.outlook.com (mail-mw2nam10lp2103.outbound.protection.outlook.com [104.47.55.103]) by aserp3020.oracle.com with ESMTP id 37mac7u4nw-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 30 Mar 2021 21:26:33 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=FHuCN3PChu9hQWew/mcj3ugwv2uZ0BdGqa7NinqLrfJZn/6X7w2oH/l0MmAkn5AoRlOEzWyC/nZe/IG+sIc+kDnxrhwoZ7U9aQUcHvw4XrU1gv/THGBlw3mAU5K4aEVAMfzA01cdkkSV2UEjWfNfPhDTRPgqjtrpl9+U4QX5SWcEfZ2LVMenYguGcSPn6vLS9nqP4/aBfIYRPTiRbnfNXt5FXbvmewT/9b76/VZuUhVWSJaVWGbzigtP0Uj02Cuq0KcC0oBZRZJoKlI6UCHvEn8ND2VCesMygmv4GNE5alxh2gZetv1NdYXlBfjJ/3fZVhVLo8WPwLtBMYBUu+cvXA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=5gs3kt58j9UHE+if8NBT4rT44pX3k6PV7VfaMeNhiWw=; b=XKp2mCpw8nLcOfBsKpw97N3TcdgJL+Sho3DGKT7MgU6o96xlRU5WRDnIfhIc6etfsZ83p3IUYUqC+U+lGyrP08MyyDPfaioRL3UPOdOk4PeQhNT6T1fs0mJoQOOm1qPSkzmvBXPXwxQ+H0AvtyEObXCNdHyUt4JHOQJTimaoRKKfiadMUMryFZ7oX4IeQGIYjX4Q8XHYCx1tWMYGUqI7wrGD8cKciJumwNXbfyWgjooV7DHKLpB7P4mMYYF68X1juaSJh+AIp4ScAnRJcMexQyeJU3IuG+t6194FKkXiD15ylUx05xUGCwQohyx98x9P65H1NWPstBgWSwtHM0glNA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=5gs3kt58j9UHE+if8NBT4rT44pX3k6PV7VfaMeNhiWw=; b=NXEEMxPP4G5vT9nsmqj+S+m0SNYcQARtOhuaEaNGLAhraSDG64py2TGpMyBqD9lTOAKmXDZOtyN1Dhx++ONdvfMv9P9wDJTYtngV1md/bTsLUJ7wXETakhsqq1oiF8fjGeMveVN2Hhc52k6wSBBtwnNV+Zh+hI2xKQy11+K3/tQ= Authentication-Results: kvack.org; dkim=none (message not signed) header.d=none;kvack.org; dmarc=none action=none header.from=oracle.com; Received: from MN2PR10MB3533.namprd10.prod.outlook.com (2603:10b6:208:118::32) by MN2PR10MB3600.namprd10.prod.outlook.com (2603:10b6:208:114::29) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.24; Tue, 30 Mar 2021 21:26:30 +0000 Received: from MN2PR10MB3533.namprd10.prod.outlook.com ([fe80::cc79:c40e:430f:e254]) by MN2PR10MB3533.namprd10.prod.outlook.com ([fe80::cc79:c40e:430f:e254%4]) with mapi id 15.20.3977.033; Tue, 30 Mar 2021 21:26:30 +0000 From: Anthony Yznaga To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: willy@infradead.org, corbet@lwn.net, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, x86@kernel.org, hpa@zytor.com, dave.hansen@linux.intel.com, luto@kernel.org, peterz@infradead.org, rppt@kernel.org, akpm@linux-foundation.org, hughd@google.com, ebiederm@xmission.com, keescook@chromium.org, ardb@kernel.org, nivedita@alum.mit.edu, jroedel@suse.de, masahiroy@kernel.org, nathan@kernel.org, terrelln@fb.com, vincenzo.frascino@arm.com, martin.b.radev@gmail.com, andreyknvl@google.com, daniel.kiper@oracle.com, rafael.j.wysocki@intel.com, dan.j.williams@intel.com, Jonathan.Cameron@huawei.com, bhe@redhat.com, rminnich@gmail.com, ashish.kalra@amd.com, guro@fb.com, hannes@cmpxchg.org, mhocko@kernel.org, iamjoonsoo.kim@lge.com, vbabka@suse.cz, alex.shi@linux.alibaba.com, david@redhat.com, richard.weiyang@gmail.com, vdavydov.dev@gmail.com, graf@amazon.com, jason.zeng@intel.com, lei.l.li@intel.com, daniel.m.jordan@oracle.com, steven.sistare@oracle.com, linux-fsdevel@vger.kernel.org, linux-doc@vger.kernel.org, kexec@lists.infradead.org Subject: [RFC v2 21/43] x86/KASLR: PKRAM: support physical kaslr Date: Tue, 30 Mar 2021 14:35:56 -0700 Message-Id: <1617140178-8773-22-git-send-email-anthony.yznaga@oracle.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1617140178-8773-1-git-send-email-anthony.yznaga@oracle.com> References: <1617140178-8773-1-git-send-email-anthony.yznaga@oracle.com> X-Originating-IP: [148.87.23.8] X-ClientProxiedBy: BYAPR11CA0099.namprd11.prod.outlook.com (2603:10b6:a03:f4::40) To MN2PR10MB3533.namprd10.prod.outlook.com (2603:10b6:208:118::32) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from ca-qasparc-x86-2.us.oracle.com (148.87.23.8) by BYAPR11CA0099.namprd11.prod.outlook.com (2603:10b6:a03:f4::40) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.26 via Frontend Transport; Tue, 30 Mar 2021 21:26:26 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 4ca0fedb-bfa2-42e4-4f3b-08d8f3c27c31 X-MS-TrafficTypeDiagnostic: MN2PR10MB3600: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:2043; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 9HZHZeizw7fGjde36AqABFq5MkOakfXE1/Uvg6KbBOk3M8TEJNhdFucyJ2YB72ecLeJHD1em2xoRVztOhITSVN6mnLAmjCHSM553GWgP89v2Y+u56IpMvOU2sAmvCFB+7SGOqi7tZBSehMdyHHpVAKJ8GhwGSq55AHESs7BjZ+LetNl80bmbdRfa+CNc3N1u1KHfto6LxlaXUUDG7m3HBOQDg4UZIu45z8JE+vtDKSG1CZ/gYglmBsECc/0I3LuhdsDfrNA3/yVKjpBSN0MD2kE61nJIfW7L/1RlpFREd6VaWRALkkQYsnrqMBu0zoH9XJahbfyxr0TP3gd3aV27uyh7QV8JoN12n0Y1QwN/b/4yad7xRSunDy6kPw3SKl0huXzhKlM8P1bE8rhxyvzog9+l1qos8uBuKkrtAbe2sWw2LPCt7DQodh5ou5anRhNNpP0dG+2uKeN0hMCowoWjz+oEsW+wziGyb2OwhI53rjJ4a2XpsQTHZnVspQqunfq3xWKjiZ7WB9Aid/Vrb/2aKck1Tt3akMl71C70VSG42YTVnXoJl+fFFKkxLgLbwVAGVYzEUMBDH6K7cK9zoCJQ+Ym0upnUFLmHeZRk5UQf2SnlXFuwBjZdBq6JlaN1r46EMxPaL05K78W+8XCzkD7c/A== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:MN2PR10MB3533.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(376002)(136003)(346002)(366004)(39860400002)(396003)(6666004)(7696005)(316002)(36756003)(44832011)(8936002)(2616005)(956004)(5660300002)(16526019)(186003)(4326008)(6486002)(86362001)(38100700001)(83380400001)(26005)(66476007)(7406005)(7416002)(66556008)(8676002)(478600001)(52116002)(2906002)(66946007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: xkgSBQsdp+OWdaNLjlR0zWKuROZQzqHjAK4ETlGY72ojit/WtQS/8bruVXEm66BF+mPsE0u/gtmBSNvt8HTZ20NR4H5bPZmQ9L8w4UBykfTrtUhoQk73juxnZ9uWQl+Idi2SWjt+4upXHiPtB0ZQDKboyEIAeJRw+u0SBomKXcmV9HCs7+4usP8IM8nx1PnVm6y6Dlq/eKN/IRoNabP3bCMz1si8yGNZsUlRXiG3vlT3ugyXQGC70nyqYM7xh1sO+4+DRysePNSLo5wwgOUIe8wCh2A6kfdHL2uWuNo12+vwgdH2OENv9aqg1JE0SdWdGMw9bIr56n5vMBLju8oxRpK7iYbdoU43Q+udcnyeKAq5NkzBxF4CHsgAcwpQ7lWzpFRe5kOcbU5VJ3J6DINJjmaCaQdfgKNnhbi+NQzXJKZ2DxNORJi0SmnPo0fdnF7s/hNJEBs0QWNbUJaZiKidg1p+IxZK+9EjzOnFsy3MOerQIOvwZMQrBVn8J2pxxtE+MiD07og723/k124UDOaERq9RGgGPQmiJxG2tSy0NSl09awhuvinQS3OPZV5fxu5GgVeP/SIuY0nePURxHR/4cg7Iz/DrArsBVdGFDmBlSyWFL3uMiKEh7TyP/vIZ+LyW1b83umFgyaptwUxGKmY1KPyuemWpxmfbdRzKDijB+ArmnUfO9WHQSWOSFZUFjOdt5E+nL80Ea8Dqf4Dg+HWFU0kGshhwmn5mE1tfQeDnHLv9kzBTqyg5joL0zgAcLvXL6gomBh2INumGkDu7VOBxcXv8be9+okZr5jiZTHD2lCcob1QpjYue45hFKIErCXzT7zwDiyYbReZXabXDjv5dVPR6nzAlRHOPzxVN8L0qqcRZm49VHAWGP9n37KO8GEEMs/i6K8IjYagVQNCEcLBCnnKWL3SsCWF7IGT45DX2mVhucfBbVNGVI2zDoY7pTY5MBn3DixRzLFeU9dI09PCYarAkWqIz4rVc/1PbWQfW82YDc9eib/auEff/Ry0KIDKX9vKoGUy5PC+cdZyoNizDkohK6Lk+gLnfGzpOMFyYyHFY1g5WO6+akPwl8b6Qy7KjRJtsrdMqyS/tOGLgcVBMmSrvyqcI94vId4C6qosFtcbu749t3fIZDRARfP8w/+LFuXI6u4RNl7Vvdp3ayHCgOTOUMWoPTaiWesU8xi9xHZG01fmC2qOgRBlodBaW5kNBhs3tMVf5WRwoWXbpf8J1/csDtvlIR+4cSPoS6wsiAKFE6wycYT0f3fHWY415wtrl53X3Di0k4ES78Osida/pqfm+6jH+1pBBxjWD/lJclzhdtOJqzT8S9fwENoEqylS5 X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 4ca0fedb-bfa2-42e4-4f3b-08d8f3c27c31 X-MS-Exchange-CrossTenant-AuthSource: MN2PR10MB3533.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Mar 2021 21:26:30.7415 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: cDashneOTBdN/vrdiqbxGCCf25XO7nrV7Yt0WLm3Hdj3f8fM2NS2cXB59kRiDkJx9cMAyxJP1le68Fy2ko5/PbkmsFbHQ63vHTDxzsqXDFA= X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR10MB3600 X-Proofpoint-Virus-Version: vendor=nai engine=6200 definitions=9939 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=999 mlxscore=0 adultscore=0 spamscore=0 malwarescore=0 phishscore=0 suspectscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2103300000 definitions=main-2103300156 X-Proofpoint-ORIG-GUID: B8bb_ymWzzcEVCO5qZuiU-DZA_fkOWZ_ X-Proofpoint-GUID: B8bb_ymWzzcEVCO5qZuiU-DZA_fkOWZ_ X-Proofpoint-Virus-Version: vendor=nai engine=6200 definitions=9939 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 priorityscore=1501 malwarescore=0 bulkscore=0 clxscore=1015 lowpriorityscore=0 suspectscore=0 adultscore=0 spamscore=0 mlxscore=0 mlxlogscore=999 phishscore=0 impostorscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2103300000 definitions=main-2103300156 X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 7B94540002C8 X-Stat-Signature: 5ehpzx6yzpr4u6xhiqxjxwm697t1t1r5 Received-SPF: none (oracle.com>: No applicable sender policy available) receiver=imf10; identity=mailfrom; envelope-from=""; helo=userp2130.oracle.com; client-ip=156.151.31.86 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1617139619-401147 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Avoid regions of memory that contain preserved pages when computing slots used to select where to put the decompressed kernel. Signed-off-by: Anthony Yznaga --- arch/x86/boot/compressed/Makefile | 3 ++ arch/x86/boot/compressed/kaslr.c | 10 +++- arch/x86/boot/compressed/misc.h | 10 ++++ arch/x86/boot/compressed/pkram.c | 109 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 130 insertions(+), 2 deletions(-) create mode 100644 arch/x86/boot/compressed/pkram.c diff --git a/arch/x86/boot/compressed/Makefile b/arch/x86/boot/compressed/Makefile index e0bc3988c3fa..ef27d411b641 100644 --- a/arch/x86/boot/compressed/Makefile +++ b/arch/x86/boot/compressed/Makefile @@ -93,6 +93,9 @@ ifdef CONFIG_X86_64 vmlinux-objs-y += $(obj)/mem_encrypt.o vmlinux-objs-y += $(obj)/pgtable_64.o vmlinux-objs-$(CONFIG_AMD_MEM_ENCRYPT) += $(obj)/sev-es.o +ifdef CONFIG_RANDOMIZE_BASE + vmlinux-objs-$(CONFIG_PKRAM) += $(obj)/pkram.o +endif endif vmlinux-objs-$(CONFIG_ACPI) += $(obj)/acpi.o diff --git a/arch/x86/boot/compressed/kaslr.c b/arch/x86/boot/compressed/kaslr.c index b92fffbe761f..a007363a7698 100644 --- a/arch/x86/boot/compressed/kaslr.c +++ b/arch/x86/boot/compressed/kaslr.c @@ -440,6 +440,7 @@ static bool mem_avoid_overlap(struct mem_vector *img, struct setup_data *ptr; u64 earliest = img->start + img->size; bool is_overlapping = false; + struct mem_vector avoid; for (i = 0; i < MEM_AVOID_MAX; i++) { if (mem_overlaps(img, &mem_avoid[i]) && @@ -453,8 +454,6 @@ static bool mem_avoid_overlap(struct mem_vector *img, /* Avoid all entries in the setup_data linked list. */ ptr = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data; while (ptr) { - struct mem_vector avoid; - avoid.start = (unsigned long)ptr; avoid.size = sizeof(*ptr) + ptr->len; @@ -479,6 +478,12 @@ static bool mem_avoid_overlap(struct mem_vector *img, ptr = (struct setup_data *)(unsigned long)ptr->next; } + if (pkram_has_overlap(img, &avoid) && (avoid.start < earliest)) { + *overlap = avoid; + earliest = overlap->start; + is_overlapping = true; + } + return is_overlapping; } @@ -840,6 +845,7 @@ void choose_random_location(unsigned long input, return; } + pkram_init(); boot_params->hdr.loadflags |= KASLR_FLAG; if (IS_ENABLED(CONFIG_X86_32)) diff --git a/arch/x86/boot/compressed/misc.h b/arch/x86/boot/compressed/misc.h index 901ea5ebec22..f8232ffd8141 100644 --- a/arch/x86/boot/compressed/misc.h +++ b/arch/x86/boot/compressed/misc.h @@ -116,6 +116,16 @@ static inline void console_init(void) { } #endif +#ifdef CONFIG_PKRAM +void pkram_init(void); +int pkram_has_overlap(struct mem_vector *entry, struct mem_vector *overlap); +#else +static inline void pkram_init(void) { } +static inline int pkram_has_overlap(struct mem_vector *entry, + struct mem_vector *overlap); +{ return 0; } +#endif + void set_sev_encryption_mask(void); #ifdef CONFIG_AMD_MEM_ENCRYPT diff --git a/arch/x86/boot/compressed/pkram.c b/arch/x86/boot/compressed/pkram.c new file mode 100644 index 000000000000..60380f074c3f --- /dev/null +++ b/arch/x86/boot/compressed/pkram.c @@ -0,0 +1,109 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include "misc.h" + +#define PKRAM_MAGIC 0x706B726D + +struct pkram_super_block { + __u32 magic; + + __u64 node_pfn; + __u64 region_list_pfn; + __u64 nr_regions; +}; + +struct pkram_region { + phys_addr_t base; + phys_addr_t size; +}; + +struct pkram_region_list { + __u64 prev_pfn; + __u64 next_pfn; + + struct pkram_region regions[0]; +}; + +#define PKRAM_REGIONS_LIST_MAX \ + ((PAGE_SIZE-sizeof(struct pkram_region_list))/sizeof(struct pkram_region)) + +static u64 pkram_sb_pfn; +static struct pkram_super_block *pkram_sb; + +void pkram_init(void) +{ + struct pkram_super_block *sb; + char arg[32]; + + if (cmdline_find_option("pkram", arg, sizeof(arg)) > 0) { + if (kstrtoull(arg, 16, &pkram_sb_pfn) != 0) + return; + } else + return; + + sb = (struct pkram_super_block *)(pkram_sb_pfn << PAGE_SHIFT); + if (sb->magic != PKRAM_MAGIC) { + debug_putstr("PKRAM: invalid super block\n"); + return; + } + + pkram_sb = sb; +} + +static struct pkram_region *pkram_first_region(struct pkram_super_block *sb, struct pkram_region_list **rlp, int *idx) +{ + if (!sb || !sb->region_list_pfn) + return NULL; + + *rlp = (struct pkram_region_list *)(sb->region_list_pfn << PAGE_SHIFT); + *idx = 0; + + return &(*rlp)->regions[0]; +} + +static struct pkram_region *pkram_next_region(struct pkram_region_list **rlp, int *idx) +{ + struct pkram_region_list *rl = *rlp; + int i = *idx; + + i++; + if (i >= PKRAM_REGIONS_LIST_MAX) { + if (!rl->next_pfn) { + debug_putstr("PKRAM: no more pkram_region_list pages\n"); + return NULL; + } + rl = (struct pkram_region_list *)(rl->next_pfn << PAGE_SHIFT); + *rlp = rl; + i = 0; + } + *idx = i; + + if (rl->regions[i].size == 0) + return NULL; + + return &rl->regions[i]; +} + +int pkram_has_overlap(struct mem_vector *entry, struct mem_vector *overlap) +{ + struct pkram_region_list *rl; + struct pkram_region *r; + int idx; + + r = pkram_first_region(pkram_sb, &rl, &idx); + + while (r) { + if (r->base + r->size <= entry->start) { + r = pkram_next_region(&rl, &idx); + continue; + } + if (r->base >= entry->start + entry->size) + return 0; + + overlap->start = r->base; + overlap->size = r->size; + return 1; + } + + return 0; +}