Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp4270012ybz; Tue, 28 Apr 2020 08:29:51 -0700 (PDT) X-Google-Smtp-Source: APiQypKtoHgTwussGXfvvdWHrdcMknc3PH+DTtIiliy4nc6W0yix5nqzQVlnziS3sSMboO8E8p7J X-Received: by 2002:a17:906:468c:: with SMTP id a12mr26006492ejr.149.1588087791809; Tue, 28 Apr 2020 08:29:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588087791; cv=none; d=google.com; s=arc-20160816; b=0iT/mmVcTUIU/rvqxEtklItkmVHr9w6ekBUlfIB+kHTurYzD+5n+d8vqaCOPx+6qpo ElAGzZU+nZPgu2WEXiNu0AKsLkxsaKgs7TM0cRJr6MtAkCKbt0MfhFNXi82p9xiwSf1A CIbpUfIED49UNEN+ZJUSrhAZfjys6L8FKq3SWL0xFdfWCkhGH2aLGljRvwi5zx6Kv7mb boAKMVVr6+OhIogifUlo5xrfrNAEwdN0UtwIEa+lh8JeEf/VdcjIxaKY0/E3maInH8P8 MvY5jcK6rWkG6HTfTcyJYF4R0roBK+0MQ/NoMT8z+r3TbKB5hPORUrnCQQvdqnUbixb7 QUwQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from; bh=CaY/dr1BmrcN7xq3+SrJnmHMobvpnS+zEaT/OPiQSnA=; b=jWjewGRyOk7wXp1DtpnsKU5NGdo3lbjmZyNjKj4TSZr8jXL/39rapwHdqPr3CnpKDC H2CoZxgTa0AXudLKOcpZ9/ipOtJb2jf1yu7csI+qkplAElqrN906EEp+O6uuNJ1r3SDr Rmm7qEnLpjjRQZ/PMEQhOPX2QT1KWEf88mj/qAbjqGa36YCC2rswJZD4JCCoe2WKrWRz SYFhoAPFB42NbaF3JzrCbokmr/KkWL28uW5Yf5gb4FYkYqvQwvVTku2xLlaN4zytefMB VfcqZNdEuxWiVwojHPh76lI+YNd28TuWiV5X1cLTMi3FLj8yPZ1GnFVy9hYW109Z5M+k dtIg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=8bytes.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id n1si1972689ejr.104.2020.04.28.08.28.36; Tue, 28 Apr 2020 08:29:51 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=8bytes.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728708AbgD1PYA (ORCPT + 99 others); Tue, 28 Apr 2020 11:24:00 -0400 Received: from 8bytes.org ([81.169.241.247]:37428 "EHLO theia.8bytes.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728211AbgD1PR6 (ORCPT ); Tue, 28 Apr 2020 11:17:58 -0400 Received: by theia.8bytes.org (Postfix, from userid 1000) id 12BEAE9A; Tue, 28 Apr 2020 17:17:45 +0200 (CEST) From: Joerg Roedel To: x86@kernel.org Cc: hpa@zytor.com, Andy Lutomirski , Dave Hansen , Peter Zijlstra , Thomas Hellstrom , Jiri Slaby , Dan Williams , Tom Lendacky , Juergen Gross , Kees Cook , David Rientjes , Cfir Cohen , Erdem Aktas , Masami Hiramatsu , Mike Stunes , Joerg Roedel , Joerg Roedel , linux-kernel@vger.kernel.org, kvm@vger.kernel.org, virtualization@lists.linux-foundation.org Subject: [PATCH v3 16/75] x86/boot/compressed/64: Always switch to own page-table Date: Tue, 28 Apr 2020 17:16:26 +0200 Message-Id: <20200428151725.31091-17-joro@8bytes.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200428151725.31091-1-joro@8bytes.org> References: <20200428151725.31091-1-joro@8bytes.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Joerg Roedel When booted through startup_64 the kernel keeps running on the EFI page-table until the KASLR code sets up its own page-table. Without KASLR the pre-decompression boot code never switches off the EFI page-table. Change that by unconditionally switching to a kernel controlled page-table after relocation. This makes sure we can make changes to the mapping when necessary, for example map pages unencrypted in SEV and SEV-ES guests. Also remove the debug_putstr() calls in initialize_identity_maps() because the function now runs before console_init() is called. Signed-off-by: Joerg Roedel --- arch/x86/boot/compressed/head_64.S | 3 +- arch/x86/boot/compressed/ident_map_64.c | 51 +++++++++++++++---------- arch/x86/boot/compressed/kaslr.c | 3 -- 3 files changed, 32 insertions(+), 25 deletions(-) diff --git a/arch/x86/boot/compressed/head_64.S b/arch/x86/boot/compressed/head_64.S index 089b9e676498..af571127c9ba 100644 --- a/arch/x86/boot/compressed/head_64.S +++ b/arch/x86/boot/compressed/head_64.S @@ -534,10 +534,11 @@ SYM_FUNC_START_LOCAL_NOALIGN(.Lrelocated) rep stosq /* - * Load stage2 IDT + * Load stage2 IDT and switch to our own page-table */ pushq %rsi call load_stage2_idt + call initialize_identity_maps popq %rsi /* diff --git a/arch/x86/boot/compressed/ident_map_64.c b/arch/x86/boot/compressed/ident_map_64.c index 33bdf923cbab..aa55e7b5cade 100644 --- a/arch/x86/boot/compressed/ident_map_64.c +++ b/arch/x86/boot/compressed/ident_map_64.c @@ -88,9 +88,31 @@ phys_addr_t physical_mask = (1ULL << __PHYSICAL_MASK_SHIFT) - 1; */ static struct x86_mapping_info mapping_info; +/* + * Adds the specified range to what will become the new identity mappings. + * Once all ranges have been added, the new mapping is activated by calling + * finalize_identity_maps() below. + */ +void add_identity_map(unsigned long start, unsigned long size) +{ + unsigned long end = start + size; + + /* Align boundary to 2M. */ + start = round_down(start, PMD_SIZE); + end = round_up(end, PMD_SIZE); + if (start >= end) + return; + + /* Build the mapping. */ + kernel_ident_mapping_init(&mapping_info, (pgd_t *)top_level_pgt, + start, end); +} + /* Locates and clears a region for a new top level page table. */ void initialize_identity_maps(void) { + unsigned long start, size; + /* If running as an SEV guest, the encryption mask is required. */ set_sev_encryption_mask(); @@ -123,37 +145,24 @@ void initialize_identity_maps(void) */ top_level_pgt = read_cr3_pa(); if (p4d_offset((pgd_t *)top_level_pgt, 0) == (p4d_t *)_pgtable) { - debug_putstr("booted via startup_32()\n"); pgt_data.pgt_buf = _pgtable + BOOT_INIT_PGT_SIZE; pgt_data.pgt_buf_size = BOOT_PGT_SIZE - BOOT_INIT_PGT_SIZE; memset(pgt_data.pgt_buf, 0, pgt_data.pgt_buf_size); } else { - debug_putstr("booted via startup_64()\n"); pgt_data.pgt_buf = _pgtable; pgt_data.pgt_buf_size = BOOT_PGT_SIZE; memset(pgt_data.pgt_buf, 0, pgt_data.pgt_buf_size); top_level_pgt = (unsigned long)alloc_pgt_page(&pgt_data); } -} -/* - * Adds the specified range to what will become the new identity mappings. - * Once all ranges have been added, the new mapping is activated by calling - * finalize_identity_maps() below. - */ -void add_identity_map(unsigned long start, unsigned long size) -{ - unsigned long end = start + size; - - /* Align boundary to 2M. */ - start = round_down(start, PMD_SIZE); - end = round_up(end, PMD_SIZE); - if (start >= end) - return; - - /* Build the mapping. */ - kernel_ident_mapping_init(&mapping_info, (pgd_t *)top_level_pgt, - start, end); + /* + * New page-table is set up - map the kernel image and load it + * into cr3. + */ + start = (unsigned long)_head; + size = _end - _head; + add_identity_map(start, size); + write_cr3(top_level_pgt); } /* diff --git a/arch/x86/boot/compressed/kaslr.c b/arch/x86/boot/compressed/kaslr.c index 7c61a8c5b9cf..856dc1c9bb0d 100644 --- a/arch/x86/boot/compressed/kaslr.c +++ b/arch/x86/boot/compressed/kaslr.c @@ -903,9 +903,6 @@ void choose_random_location(unsigned long input, boot_params->hdr.loadflags |= KASLR_FLAG; - /* Prepare to add new identity pagetables on demand. */ - initialize_identity_maps(); - /* Record the various known unsafe memory ranges. */ mem_avoid_init(input, input_size, *output); -- 2.17.1