Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp919066pxp; Wed, 16 Mar 2022 21:18:06 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyBqdOEGHA13d480wLB3ufy0UHOltEFqgOdoeENrZLekr9hx2eyzG/pXBVBviTxJYsDJocC X-Received: by 2002:a17:903:246:b0:153:84fe:a9b0 with SMTP id j6-20020a170903024600b0015384fea9b0mr3116360plh.163.1647490686365; Wed, 16 Mar 2022 21:18:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1647490686; cv=none; d=google.com; s=arc-20160816; b=hOYlg4qQ7J7a0bv6Z89XkWlkOO5NjXyJOMCttYwBM7MWMpzrn7mMijoSDmPM5IjdAX rDJRuCOuOWzS67LJmTKKg3h/uyxWZaFZTL4+TPDdp+9WP/2BQpAwsyPj3bi950R9GF6p btyZxKu+Cw/TzHbF0bMriZCCMa5CE0bHZF8kMs2sBGyTtNF9UU/kolVQUS2dgz7qZLfC P2FaX1NQt/NrTWGkzJ/03mRTRvf4Dv4pM2O0YtJQbI6raY8dvQhs+QOjVvzOPyJsGX2o oDz+XcGbKPftUUtZbcWX8qpBzlZdlPLKG9dwRmGSigYbkNPq05WdJg+iUajHWNokbaFf XJ3w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=OEy3N1FGpmYZATJvh9PIdaxPVMr+js7DQaTam69NCuo=; b=z0vdsbPxkc/osm2IEd25MAmCO+k5MJuRpziHxikGjNeAYpl2IptEEicLfXomba6Vrv qpCk/Z83y5iaMMTBzju/4S/fgUFDPDIuumleoNzny72qvq9LTxLivKJC7N8+frY8J143 kPcTaBsQ/c/Yp3SeA5gqPpYq1mT2//VueN7wnx4ak1FJ6EjNqmx+BriYQklO+sJfe/uq L2hLXNvVePwxHM/j1Vjwphx9najMY0HTKcq5wYH1C+ZKOJQhhOVafJ66SiuBCzRYLE6/ Sx0ptEZ1XtqGXXE6UKj4SYz3aQ4S+TDQDd6wIuFjsI3zPxpQKexjXK4O0S4OR/+ben0/ 6wFQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=llV3yJC+; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id d14-20020a631d0e000000b003816043ee6bsi970460pgd.96.2022.03.16.21.18.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Mar 2022 21:18:06 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=llV3yJC+; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 42203DA0; Wed, 16 Mar 2022 20:53:13 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1353144AbiCPCMi (ORCPT + 99 others); Tue, 15 Mar 2022 22:12:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47234 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1352829AbiCPCL0 (ORCPT ); Tue, 15 Mar 2022 22:11:26 -0400 Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B418E5E753 for ; Tue, 15 Mar 2022 19:10:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1647396611; x=1678932611; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=8Von0ynflwRj0iZvnoCEcfyY4cVSmuguWbqxumSlthE=; b=llV3yJC+10+5hRc/bBl4hx/yQnTIS6pWEJ+ATPSQ0cUsz1MOB7E6tF02 UWA45p5yiQh55sPE3MhayizaGVWK6F0ndkxF+Cqhaxn9TyoLO7i07EFlh 2KTrrTFQ0zSg7JYmDby2RAp717mbrcoLFhJNHaBBSLg7nOTr5xK2+TJvj NYf9Lwit+1qxaCkVyXkCFSfFPhVxN2GSzX2K8XhXncsWz/h/JOr/KbS+k VtAzhsZZf880q/mart9euKVRQO5y/pggXAGu6swDqZH+0sz1mZchwDwYq P/JQDxcdPi0wWjx3sXwid4W0plthUYXOTeLjQQ3NwdOVuW/FvtHs4t7W4 w==; X-IronPort-AV: E=McAfee;i="6200,9189,10286"; a="237075912" X-IronPort-AV: E=Sophos;i="5.90,185,1643702400"; d="scan'208";a="237075912" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Mar 2022 19:10:11 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.90,185,1643702400"; d="scan'208";a="690414232" Received: from black.fi.intel.com ([10.237.72.28]) by fmsmga001.fm.intel.com with ESMTP; 15 Mar 2022 19:10:04 -0700 Received: by black.fi.intel.com (Postfix, from userid 1000) id 477488D9; Wed, 16 Mar 2022 04:10:10 +0200 (EET) From: "Kirill A. Shutemov" To: tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@intel.com, luto@kernel.org, peterz@infradead.org Cc: sathyanarayanan.kuppuswamy@linux.intel.com, aarcange@redhat.com, ak@linux.intel.com, dan.j.williams@intel.com, david@redhat.com, hpa@zytor.com, jgross@suse.com, jmattson@google.com, joro@8bytes.org, jpoimboe@redhat.com, knsathya@kernel.org, pbonzini@redhat.com, sdeep@vmware.com, seanjc@google.com, tony.luck@intel.com, vkuznets@redhat.com, wanpengli@tencent.com, thomas.lendacky@amd.com, brijesh.singh@amd.com, x86@kernel.org, linux-kernel@vger.kernel.org, "Kirill A. Shutemov" Subject: [PATCHv6 15/30] x86/boot: Port I/O: allow to hook up alternative helpers Date: Wed, 16 Mar 2022 05:08:41 +0300 Message-Id: <20220316020856.24435-16-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220316020856.24435-1-kirill.shutemov@linux.intel.com> References: <20220316020856.24435-1-kirill.shutemov@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Port I/O instructions trigger #VE in the TDX environment. In response to the exception, kernel emulates these instructions using hypercalls. But during early boot, on the decompression stage, it is cumbersome to deal with #VE. It is cleaner to go to hypercalls directly, bypassing #VE handling. Add a way to hook up alternative port I/O helpers in the boot stub with a new pio_ops structure. For now, set the ops structure to just call the normal I/O operation functions. out*()/in*() macros redefined to use pio_ops callbacks. It eliminates need in changing call sites. io_delay() changed to use port I/O helper instead of inline assembly. Signed-off-by: Kirill A. Shutemov --- arch/x86/boot/boot.h | 4 +-- arch/x86/boot/compressed/misc.c | 4 +++ arch/x86/boot/compressed/misc.h | 2 +- arch/x86/boot/io.h | 50 +++++++++++++++++++++++++++++++++ arch/x86/boot/main.c | 4 +++ arch/x86/realmode/rm/wakemain.c | 4 +++ 6 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 arch/x86/boot/io.h diff --git a/arch/x86/boot/boot.h b/arch/x86/boot/boot.h index 22a474c5b3e8..b42b91606ca8 100644 --- a/arch/x86/boot/boot.h +++ b/arch/x86/boot/boot.h @@ -23,10 +23,10 @@ #include #include #include -#include #include "bitops.h" #include "ctype.h" #include "cpuflags.h" +#include "io.h" /* Useful macros */ #define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x))) @@ -39,7 +39,7 @@ extern struct boot_params boot_params; static inline void io_delay(void) { const u16 DELAY_PORT = 0x80; - asm volatile("outb %%al,%0" : : "dN" (DELAY_PORT)); + outb(0, DELAY_PORT); } /* These functions are used to reference data in other segments. */ diff --git a/arch/x86/boot/compressed/misc.c b/arch/x86/boot/compressed/misc.c index 2b1169869b96..9fdef6af20a1 100644 --- a/arch/x86/boot/compressed/misc.c +++ b/arch/x86/boot/compressed/misc.c @@ -47,6 +47,8 @@ void *memmove(void *dest, const void *src, size_t n); */ struct boot_params *boot_params; +struct port_io_ops pio_ops; + memptr free_mem_ptr; memptr free_mem_end_ptr; @@ -370,6 +372,8 @@ asmlinkage __visible void *extract_kernel(void *rmode, memptr heap, lines = boot_params->screen_info.orig_video_lines; cols = boot_params->screen_info.orig_video_cols; + init_default_io_ops(); + /* * Detect TDX guest environment. * diff --git a/arch/x86/boot/compressed/misc.h b/arch/x86/boot/compressed/misc.h index 8a253e85f990..ea71cf3d64e1 100644 --- a/arch/x86/boot/compressed/misc.h +++ b/arch/x86/boot/compressed/misc.h @@ -26,7 +26,6 @@ #include #include #include -#include #include "tdx.h" @@ -35,6 +34,7 @@ #define BOOT_BOOT_H #include "../ctype.h" +#include "../io.h" #ifdef CONFIG_X86_64 #define memptr long diff --git a/arch/x86/boot/io.h b/arch/x86/boot/io.h new file mode 100644 index 000000000000..5f0d99310f91 --- /dev/null +++ b/arch/x86/boot/io.h @@ -0,0 +1,50 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef BOOT_IO_H +#define BOOT_IO_H + +#include + +#undef inb +#undef inw +#undef inl +#undef outb +#undef outw +#undef outl + +struct port_io_ops { + u8 (*inb)(u16 port); + u16 (*inw)(u16 port); + u32 (*inl)(u16 port); + void (*outb)(u8 v, u16 port); + void (*outw)(u16 v, u16 port); + void (*outl)(u32 v, u16 port); +}; + +extern struct port_io_ops pio_ops; + +/* + * Use the normal I/O instructions by default. + * TDX guests override these to use hypercalls. + */ +static inline void init_default_io_ops(void) +{ + pio_ops.inb = __inb; + pio_ops.inw = __inw; + pio_ops.inl = __inl; + pio_ops.outb = __outb; + pio_ops.outw = __outw; + pio_ops.outl = __outl; +} + +/* + * Redirect port I/O operations via pio_ops callbacks. + * TDX guests override these callbacks with TDX-specific helpers. + */ +#define inb pio_ops.inb +#define inw pio_ops.inw +#define inl pio_ops.inl +#define outb pio_ops.outb +#define outw pio_ops.outw +#define outl pio_ops.outl + +#endif diff --git a/arch/x86/boot/main.c b/arch/x86/boot/main.c index e3add857c2c9..1202d4f8a390 100644 --- a/arch/x86/boot/main.c +++ b/arch/x86/boot/main.c @@ -17,6 +17,8 @@ struct boot_params boot_params __attribute__((aligned(16))); +struct port_io_ops pio_ops; + char *HEAP = _end; char *heap_end = _end; /* Default end of heap = no heap */ @@ -133,6 +135,8 @@ static void init_heap(void) void main(void) { + init_default_io_ops(); + /* First, copy the boot header into the "zeropage" */ copy_boot_params(); diff --git a/arch/x86/realmode/rm/wakemain.c b/arch/x86/realmode/rm/wakemain.c index 1d6437e6d2ba..a6f4d8388ad8 100644 --- a/arch/x86/realmode/rm/wakemain.c +++ b/arch/x86/realmode/rm/wakemain.c @@ -62,8 +62,12 @@ static void send_morse(const char *pattern) } } +struct port_io_ops pio_ops; + void main(void) { + init_default_io_ops(); + /* Kill machine if structures are wrong */ if (wakeup_header.real_magic != 0x12345678) while (1) -- 2.34.1