From patchwork Tue Nov 28 10:03:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Roger_Pau_Monn=C3=A9?= X-Patchwork-Id: 13470768 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 39165C07E98 for ; Tue, 28 Nov 2023 10:04:15 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.642875.1002652 (Exim 4.92) (envelope-from ) id 1r7uwp-0002SE-GY; Tue, 28 Nov 2023 10:04:07 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 642875.1002652; Tue, 28 Nov 2023 10:04:07 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1r7uwp-0002RO-9y; Tue, 28 Nov 2023 10:04:07 +0000 Received: by outflank-mailman (input) for mailman id 642875; Tue, 28 Nov 2023 10:04:06 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1r7uwo-0002NP-3E for xen-devel@lists.xenproject.org; Tue, 28 Nov 2023 10:04:06 +0000 Received: from mail-wr1-x42d.google.com (mail-wr1-x42d.google.com [2a00:1450:4864:20::42d]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 7680d66b-8dd5-11ee-98e3-6d05b1d4d9a1; Tue, 28 Nov 2023 11:04:05 +0100 (CET) Received: by mail-wr1-x42d.google.com with SMTP id ffacd0b85a97d-332f90a375eso1759750f8f.3 for ; Tue, 28 Nov 2023 02:04:05 -0800 (PST) Received: from localhost ([213.195.113.99]) by smtp.gmail.com with ESMTPSA id b7-20020adfe307000000b00332fbf7ab21sm6937611wrj.60.2023.11.28.02.04.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Nov 2023 02:04:03 -0800 (PST) X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 7680d66b-8dd5-11ee-98e3-6d05b1d4d9a1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=citrix.com; s=google; t=1701165844; x=1701770644; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=nCTCryaDRhdDFZ2oalNsQcOivJksIALZ3Jna08fl7Bk=; b=BFbmsOEqRnfiSSmRdJbi4fgJqRYvYw9vGVdy2ZCsQqc6qG2RFQAg2gFkf++R2i+hS+ CZawD+bWdvidvvRuB0GGZSgQeyfHrxkcthPPYYggXqCDFFqN+YdlXiPM730VfyesN/gy zEVFstefYMM8j2KmsPnAaOkg7yDbPCv3FcsJ8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701165844; x=1701770644; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=nCTCryaDRhdDFZ2oalNsQcOivJksIALZ3Jna08fl7Bk=; b=wkyi6Y1V/SrYSxiPIU343B43pChi0iphG8urLMFq6uuS0LuUkYV5DbAo4v8O5ehsEc 2EvC1rMPw4sFSDbK06uK1VHhXWhWtFM8UYQbZvo2ilL10iJkPhTPJ4SeQ25cGDtQLN92 9PXqOQGBrDMQEoNpHyEzXKtzcuFXy036eW1lkydeya/fJ/v1m6jDg07EGp15+3XaAWCu Ab1sKdr7/o3TV5f+D6fVO7YopG0uvMLdxgcO1SyX311UNCTt4fap8M6XZC963mkuot5/ wydrlvBBVUlPjHSx08e0e2BLsEo2FRu3s3dvZ65pyzB+MG1wDv7OeJIWIb7oQhiYlTDj 2ZNA== X-Gm-Message-State: AOJu0YyF8GouLfcjCwDXdYQRqlGBgdw7WdJ9u1A0IVO9pKTg0zSIk9u0 f/+i2y+hwtWSpUTamX7R6ij5yF9R517sCRF/MCU= X-Google-Smtp-Source: AGHT+IGQuUtgRkOf070NhCs+mSJ4aFuaA23C1fs1racT0rVWl/ek/z+YCDVIT4AF3eMZAlEaAGqmoA== X-Received: by 2002:adf:ef4e:0:b0:331:4e5e:d9e7 with SMTP id c14-20020adfef4e000000b003314e5ed9e7mr10106417wrp.34.1701165843982; Tue, 28 Nov 2023 02:04:03 -0800 (PST) From: Roger Pau Monne To: xen-devel@lists.xenproject.org Cc: Roger Pau Monne , Jan Beulich , Andrew Cooper , Wei Liu , Konrad Rzeszutek Wilk , Ross Lagerwall Subject: [PATCH v2 1/5] x86/livepatch: set function alignment to ensure minimal function size Date: Tue, 28 Nov 2023 11:03:48 +0100 Message-ID: <20231128100352.35430-2-roger.pau@citrix.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20231128100352.35430-1-roger.pau@citrix.com> References: <20231128100352.35430-1-roger.pau@citrix.com> MIME-Version: 1.0 The minimal function size requirements for livepatch are either 5 bytes (for jmp) or 9 bytes (for endbr + jmp). Ensure that functions are always at least that size by requesting the compiled to align the functions to 8 or 16 bytes, depending on whether Xen is build with IBT support. Note that it's possible for the compiler to end up using a higher function alignment regardless of the passed value, so this change just make sure that the minimum required for livepatch to work is present. Since the option (-falign-functions) is supported by both minimal required compiler versions of clang and gcc there's no need to add a test to check for its presence. The alignment is currently only implemented for livepatch on x86, I'm unsure whether ARM has a mandatory function alignment high enough to cover for the space required by the replacement instruction(s). Signed-off-by: Roger Pau Monné --- Changes since v1: - New in this version. --- xen/arch/x86/Kconfig | 6 ++++++ xen/arch/x86/Makefile | 2 ++ xen/arch/x86/livepatch.c | 4 ++++ 3 files changed, 12 insertions(+) diff --git a/xen/arch/x86/Kconfig b/xen/arch/x86/Kconfig index 1acdffc51c22..612a4acf079b 100644 --- a/xen/arch/x86/Kconfig +++ b/xen/arch/x86/Kconfig @@ -47,6 +47,12 @@ config HAS_CC_CET_IBT # Retpoline check to work around https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93654 def_bool $(cc-option,-fcf-protection=branch -mmanual-endbr -mindirect-branch=thunk-extern) && $(as-instr,endbr64) +# Set function alignment to ensure enough padding available +config CC_FUNCTION_ALIGNMENT + int + default 16 if LIVEPATCH && XEN_IBT + default 8 if LIVEPATCH + menu "Architecture Features" source "arch/Kconfig" diff --git a/xen/arch/x86/Makefile b/xen/arch/x86/Makefile index f3abdf9cd111..f629157086d0 100644 --- a/xen/arch/x86/Makefile +++ b/xen/arch/x86/Makefile @@ -82,6 +82,8 @@ obj-$(CONFIG_COMPAT) += x86_64/platform_hypercall.o obj-y += sysctl.o endif +CFLAGS-$(CONFIG_LIVEPATCH) += -falign-functions=$(CONFIG_CC_FUNCTION_ALIGNMENT) + extra-y += asm-macros.i extra-y += xen.lds diff --git a/xen/arch/x86/livepatch.c b/xen/arch/x86/livepatch.c index ee539f001b73..4a6ba09e0ec5 100644 --- a/xen/arch/x86/livepatch.c +++ b/xen/arch/x86/livepatch.c @@ -109,6 +109,10 @@ int arch_livepatch_verify_func(const struct livepatch_func *func) */ uint8_t needed = ARCH_PATCH_INSN_SIZE; + BUILD_BUG_ON(ARCH_PATCH_INSN_SIZE + + (IS_ENABLED(CONIFG_XEN_IBT) ? ENDBR64_LEN : 0) > + CONFIG_CC_FUNCTION_ALIGNMENT); + if ( is_endbr64(func->old_addr) || is_endbr64_poison(func->old_addr) ) needed += ENDBR64_LEN; From patchwork Tue Nov 28 10:03:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Roger_Pau_Monn=C3=A9?= X-Patchwork-Id: 13470771 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 37C74C46CA0 for ; Tue, 28 Nov 2023 10:04:24 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.642876.1002665 (Exim 4.92) (envelope-from ) id 1r7uwq-0002tn-N0; Tue, 28 Nov 2023 10:04:08 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 642876.1002665; Tue, 28 Nov 2023 10:04:08 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1r7uwq-0002tc-Jt; Tue, 28 Nov 2023 10:04:08 +0000 Received: by outflank-mailman (input) for mailman id 642876; Tue, 28 Nov 2023 10:04:06 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1r7uwo-0002NP-TZ for xen-devel@lists.xenproject.org; Tue, 28 Nov 2023 10:04:06 +0000 Received: from mail-lj1-x236.google.com (mail-lj1-x236.google.com [2a00:1450:4864:20::236]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 7750044f-8dd5-11ee-98e3-6d05b1d4d9a1; Tue, 28 Nov 2023 11:04:06 +0100 (CET) Received: by mail-lj1-x236.google.com with SMTP id 38308e7fff4ca-2c997467747so36351941fa.1 for ; Tue, 28 Nov 2023 02:04:06 -0800 (PST) Received: from localhost ([213.195.113.99]) by smtp.gmail.com with ESMTPSA id a10-20020a056000050a00b0032ddf2804ccsm14288986wrf.83.2023.11.28.02.04.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Nov 2023 02:04:05 -0800 (PST) X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 7750044f-8dd5-11ee-98e3-6d05b1d4d9a1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=citrix.com; s=google; t=1701165845; x=1701770645; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=WKzhNQNtxrNJla1Dvx/CNVFVt9mfF6UZ3a4ebqZogfY=; b=uSy2i5dY2qzwh0od/byWASiBZg0pemBzLLZkFcxeyHCRNY9Zo3eSxZITng4aehZdw9 KYlY+ZpwcMTR4KvG6zXC3K/n92lq0q0qJkSOqc5aUe5eM9hyKZsDdZwcx2fyVyH7WYxP A4xRCjqYUoANEzVTQRcccWR+pgTBJmyP34STc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701165845; x=1701770645; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=WKzhNQNtxrNJla1Dvx/CNVFVt9mfF6UZ3a4ebqZogfY=; b=P7qUlz7OMi0wru3DeEvj8ewUwZSx5ctBHt+SvEAQfaayC+htAsEci7ej3J8ci6vA5n aSB68Q0MEr5I27jICxgtW9TNos89wBGdRxrJh/83HxAZdcL5OjD+0WBHxHlgzyZmbuk6 22VzoXBL4ymMhdw0XkoimClEygECgYH22beOoc7lqMWzFQHkzAzprj2Q8g31O2erNEIF RHX0RkOPK/nyeu+iGZAK0V7bqAnQnxUqnJYrMHxmS9dpj8Gph0o8MopSfRPpIxmYZoxr W/gJn6Tu8LFwzmg2HSgazTPdwRBBjtjDKYUaCTYE801avn9KfKQgGx2+woGi0JxtT3Jp nbaA== X-Gm-Message-State: AOJu0Yxpw+I0VqmIcmEFaCSJNrZ7Xt7E95wuAhL79zu5IWMmhGYId1KD Ke/Y8iOrUMgSyFZ7i/h4ikvLvLumS/gwYnxEs9Q= X-Google-Smtp-Source: AGHT+IEAPFRNttEIDc2cga3vjZhyJzcR+IN/kUQKqgwhWxsJGmAecXFEOYUlzZSYsfk2FSth4FaCfA== X-Received: by 2002:a2e:1551:0:b0:2c9:9a39:28ed with SMTP id 17-20020a2e1551000000b002c99a3928edmr5366718ljv.31.1701165845276; Tue, 28 Nov 2023 02:04:05 -0800 (PST) From: Roger Pau Monne To: xen-devel@lists.xenproject.org Cc: Roger Pau Monne , Doug Goldstein , Stefano Stabellini Subject: [PATCH v2 2/5] automation/alpine: add elfutils-dev Date: Tue, 28 Nov 2023 11:03:49 +0100 Message-ID: <20231128100352.35430-3-roger.pau@citrix.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20231128100352.35430-1-roger.pau@citrix.com> References: <20231128100352.35430-1-roger.pau@citrix.com> MIME-Version: 1.0 In preparation for adding some livepatch-build-tools test update the Alpine container to also install elfutils-dev. Signed-off-by: Roger Pau Monné Acked-by: Stefano Stabellini --- automation/build/alpine/3.18.dockerfile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/automation/build/alpine/3.18.dockerfile b/automation/build/alpine/3.18.dockerfile index 4ae9cb5e9e30..aac2d8cc82d9 100644 --- a/automation/build/alpine/3.18.dockerfile +++ b/automation/build/alpine/3.18.dockerfile @@ -47,3 +47,5 @@ RUN apk --no-cache add \ libcap-ng-dev \ ninja \ pixman-dev \ + # livepatch-tools deps + elfutils-dev From patchwork Tue Nov 28 10:03:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Roger_Pau_Monn=C3=A9?= X-Patchwork-Id: 13470770 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id D6A80C07E98 for ; Tue, 28 Nov 2023 10:04:22 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.642877.1002675 (Exim 4.92) (envelope-from ) id 1r7uws-0003AT-1W; Tue, 28 Nov 2023 10:04:10 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 642877.1002675; Tue, 28 Nov 2023 10:04:10 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1r7uwr-0003A7-Tb; Tue, 28 Nov 2023 10:04:09 +0000 Received: by outflank-mailman (input) for mailman id 642877; Tue, 28 Nov 2023 10:04:08 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1r7uwq-0002NP-9A for xen-devel@lists.xenproject.org; Tue, 28 Nov 2023 10:04:08 +0000 Received: from mail-lj1-x231.google.com (mail-lj1-x231.google.com [2a00:1450:4864:20::231]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 77f89934-8dd5-11ee-98e3-6d05b1d4d9a1; Tue, 28 Nov 2023 11:04:07 +0100 (CET) Received: by mail-lj1-x231.google.com with SMTP id 38308e7fff4ca-2c9b5b72983so4151171fa.2 for ; Tue, 28 Nov 2023 02:04:07 -0800 (PST) Received: from localhost ([213.195.113.99]) by smtp.gmail.com with ESMTPSA id o7-20020a05600c510700b0040b36ad5413sm16784552wms.46.2023.11.28.02.04.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Nov 2023 02:04:06 -0800 (PST) X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 77f89934-8dd5-11ee-98e3-6d05b1d4d9a1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=citrix.com; s=google; t=1701165846; x=1701770646; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=M3yKV/RFZSYIusyATo8RxWzm7Bfr63u910kNQKsr5wU=; b=OppS1gb/idvKi6zG2UoTdtxquNDadKMPiBJHV5UO2LzZBfjyv+pIIqClBWykfpPmst Gx5HbEUzPOmLGNVmPkxYIAXgeWZaFLfIhBzELVrmzbojiU1kqxAWY4iQc14AJuUdW0rW O2ZDHwbgLKtYjC3NcuADjQjTbqR19soL1dquM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701165846; x=1701770646; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=M3yKV/RFZSYIusyATo8RxWzm7Bfr63u910kNQKsr5wU=; b=Vceb7xnvAYUSp9zXRYziV6aYF7PschBWcRaPkzDFIcmw/wtPc+trX8NA/pcyr8Ti2x IHXC3/lilTGcU0Wp+U58JuJD/I/wGL9cvZiSyoXYuUZS6R7Rc4iPzyw+xJ6l5NRK8P+w WgLqHcok4KtRdm1+0MRsVtIh/BYaWIlD/al7sfQRicR5STf4/IU1ofQsNgyOQiV0IZ9Q bIe0xlKijtUuVie76PuIros/sY67E0CpWTVUeFkrWEKQYUvkITt3q7lL/rej4nNWibAB VgtNW5OahX1vjcyB33wCnY55smvs1h4Suin30UPMXQjOXwiiE7FMSI3OFkH+vbbOPXSM Ro9Q== X-Gm-Message-State: AOJu0YyH2UzLOUaLiWWCzv9K6FKx48ykYzgdM39YKnqUoIFCE4QIORuS r1hHXgs01RrVhbu6NoufiNPlrdz2L+CVAvPJ0IE= X-Google-Smtp-Source: AGHT+IGUr4ORQZi+oIxzqruDPiO803YUuddIpmkAqngLT3LpdjlpHf0BiBh0Sx7bAd60OcacEiNMAw== X-Received: by 2002:ac2:504f:0:b0:50b:bf06:ea0b with SMTP id a15-20020ac2504f000000b0050bbf06ea0bmr405848lfm.53.1701165846522; Tue, 28 Nov 2023 02:04:06 -0800 (PST) From: Roger Pau Monne To: xen-devel@lists.xenproject.org Cc: Roger Pau Monne , Wei Liu , Anthony PERARD , Juergen Gross , Andrew Cooper , George Dunlap , Jan Beulich , Julien Grall , Stefano Stabellini Subject: [PATCH v2 3/5] xen/x86: introduce self modifying code test Date: Tue, 28 Nov 2023 11:03:50 +0100 Message-ID: <20231128100352.35430-4-roger.pau@citrix.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20231128100352.35430-1-roger.pau@citrix.com> References: <20231128100352.35430-1-roger.pau@citrix.com> MIME-Version: 1.0 Introduce a helper to perform checks related to self modifying code, and start by creating a simple test to check that alternatives have been applied. Such test is hooked into the boot process and called just after alternatives have been applied. In case of failure a message is printed, and the hypervisor is tainted as not having passed the tests, this does require introducing a new taint bit (printed as 'A'). A new sysctl is also introduced to run the tests on demand. While there are no current users introduced here, further changes will introduce those, and it's helpful to have the interface defined in the sysctl header from the start. Signed-off-by: Roger Pau Monné --- Changes since v1: - Rework test and interface. --- tools/include/xenctrl.h | 2 + tools/libs/ctrl/xc_misc.c | 14 ++++++ xen/arch/x86/Makefile | 1 + xen/arch/x86/include/asm/test-smc.h | 18 ++++++++ xen/arch/x86/setup.c | 3 ++ xen/arch/x86/sysctl.c | 7 +++ xen/arch/x86/test-smc.c | 68 +++++++++++++++++++++++++++++ xen/common/kernel.c | 5 ++- xen/include/public/sysctl.h | 9 ++++ xen/include/xen/lib.h | 1 + 10 files changed, 126 insertions(+), 2 deletions(-) create mode 100644 xen/arch/x86/include/asm/test-smc.h create mode 100644 xen/arch/x86/test-smc.c diff --git a/tools/include/xenctrl.h b/tools/include/xenctrl.h index 2ef8b4e05422..0f87ffa4affd 100644 --- a/tools/include/xenctrl.h +++ b/tools/include/xenctrl.h @@ -2658,6 +2658,8 @@ int xc_dt_overlay(xc_interface *xch, void *overlay_fdt, uint32_t overlay_fdt_size, uint8_t overlay_op); #endif +int xc_test_smc(xc_interface *xch, uint32_t tests, uint32_t *result); + /* Compat shims */ #include "xenctrl_compat.h" diff --git a/tools/libs/ctrl/xc_misc.c b/tools/libs/ctrl/xc_misc.c index 5ecdfa2c7934..7f7ece589cc2 100644 --- a/tools/libs/ctrl/xc_misc.c +++ b/tools/libs/ctrl/xc_misc.c @@ -1021,6 +1021,20 @@ int xc_livepatch_replace(xc_interface *xch, char *name, uint32_t timeout, uint32 return _xc_livepatch_action(xch, name, LIVEPATCH_ACTION_REPLACE, timeout, flags); } +int xc_test_smc(xc_interface *xch, uint32_t tests, uint32_t *result) +{ + struct xen_sysctl sysctl = { + .cmd = XEN_SYSCTL_test_smc, + .u.smc.tests = tests, + }; + int rc = do_sysctl(xch, &sysctl); + + if ( !rc ) + *result = sysctl.u.smc.results; + + return rc; +} + /* * Local variables: * mode: C diff --git a/xen/arch/x86/Makefile b/xen/arch/x86/Makefile index f629157086d0..bdd2183a2fd7 100644 --- a/xen/arch/x86/Makefile +++ b/xen/arch/x86/Makefile @@ -65,6 +65,7 @@ obj-y += smpboot.o obj-y += spec_ctrl.o obj-y += srat.o obj-y += string.o +obj-y += test-smc.o obj-y += time.o obj-y += traps.o obj-y += tsx.o diff --git a/xen/arch/x86/include/asm/test-smc.h b/xen/arch/x86/include/asm/test-smc.h new file mode 100644 index 000000000000..18b23dbdbf2d --- /dev/null +++ b/xen/arch/x86/include/asm/test-smc.h @@ -0,0 +1,18 @@ +#ifndef _ASM_X86_TEST_SMC_H_ +#define _ASM_X86_TEST_SMC_H_ + +#include + +int test_smc(uint32_t selection, uint32_t *results); + +#endif /* _ASM_X86_TEST_SMC_H_ */ + +/* + * Local variables: + * mode: C + * c-file-style: "BSD" + * c-basic-offset: 4 + * tab-width: 4 + * indent-tabs-mode: nil + * End: + */ diff --git a/xen/arch/x86/setup.c b/xen/arch/x86/setup.c index f6b8a3efd752..1f90d30204fe 100644 --- a/xen/arch/x86/setup.c +++ b/xen/arch/x86/setup.c @@ -58,6 +58,7 @@ #include #include #include +#include /* opt_nosmp: If true, secondary processors are ignored. */ static bool __initdata opt_nosmp; @@ -1952,6 +1953,8 @@ void __init noreturn __start_xen(unsigned long mbi_p) alternative_branches(); + test_smc(XEN_SYSCTL_TEST_SMC_ALL, NULL); + /* * NB: when running as a PV shim VCPUOP_up/down is wired to the shim * physical cpu_add/remove functions, so launch the guest with only diff --git a/xen/arch/x86/sysctl.c b/xen/arch/x86/sysctl.c index 1d40d82c5ad2..77d091f4bd59 100644 --- a/xen/arch/x86/sysctl.c +++ b/xen/arch/x86/sysctl.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -423,6 +424,12 @@ long arch_do_sysctl( break; } + case XEN_SYSCTL_test_smc: + ret = test_smc(sysctl->u.smc.tests, &sysctl->u.smc.results); + if ( !ret && __copy_field_to_guest(u_sysctl, sysctl, u.smc.results) ) + ret = -EFAULT; + break; + default: ret = -ENOSYS; break; diff --git a/xen/arch/x86/test-smc.c b/xen/arch/x86/test-smc.c new file mode 100644 index 000000000000..8916c185d60a --- /dev/null +++ b/xen/arch/x86/test-smc.c @@ -0,0 +1,68 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#include + +#include +#include +#include + +static bool cf_check test_insn_replacement(void) +{ +#define EXPECTED_VALUE 2 + unsigned int r = ~EXPECTED_VALUE; + + alternative_io("", "mov $" STR(EXPECTED_VALUE) ", %0", + X86_FEATURE_ALWAYS, "=r"(r)); + + return r == EXPECTED_VALUE; +#undef EXPECTED_VALUE +} + +int test_smc(uint32_t selection, uint32_t *results) +{ + struct { + unsigned int mask; + bool (*test)(void); + const char *name; + } static const tests[] = { + { XEN_SYSCTL_TEST_SMC_INSN_REPL, &test_insn_replacement, + "alternative instruction replacement" }, + }; + unsigned int i; + + if ( selection & ~XEN_SYSCTL_TEST_SMC_ALL ) + return -EINVAL; + + if ( results ) + *results = 0; + + printk(XENLOG_INFO "Checking Self Modify Code\n"); + + for ( i = 0; i < ARRAY_SIZE(tests); i++ ) + { + if ( !(selection & tests[i].mask) ) + continue; + + if ( tests[i].test() ) + { + if ( results ) + *results |= tests[i].mask; + continue; + } + + add_taint(TAINT_ERROR_SMC); + printk(XENLOG_ERR "%s test failed\n", tests[i].name); + } + + return 0; +} + +/* + * Local variables: + * mode: C + * c-file-style: "BSD" + * c-basic-offset: 4 + * tab-width: 4 + * indent-tabs-mode: nil + * End: + */ diff --git a/xen/common/kernel.c b/xen/common/kernel.c index 08dbaa2a054c..fed7ed0d587f 100644 --- a/xen/common/kernel.c +++ b/xen/common/kernel.c @@ -386,13 +386,14 @@ char *print_tainted(char *str) { if ( tainted ) { - snprintf(str, TAINT_STRING_MAX_LEN, "Tainted: %c%c%c%c%c%c", + snprintf(str, TAINT_STRING_MAX_LEN, "Tainted: %c%c%c%c%c%c%c", tainted & TAINT_MACHINE_INSECURE ? 'I' : ' ', tainted & TAINT_MACHINE_CHECK ? 'M' : ' ', tainted & TAINT_SYNC_CONSOLE ? 'C' : ' ', tainted & TAINT_ERROR_INJECT ? 'E' : ' ', tainted & TAINT_HVM_FEP ? 'H' : ' ', - tainted & TAINT_CPU_OUT_OF_SPEC ? 'S' : ' '); + tainted & TAINT_CPU_OUT_OF_SPEC ? 'S' : ' ', + tainted & TAINT_ERROR_SMC ? 'A' : ' '); } else { diff --git a/xen/include/public/sysctl.h b/xen/include/public/sysctl.h index 9b19679caeb1..94287009387c 100644 --- a/xen/include/public/sysctl.h +++ b/xen/include/public/sysctl.h @@ -1201,6 +1201,13 @@ struct xen_sysctl_dt_overlay { }; #endif +struct xen_sysctl_test_smc { + uint32_t tests; /* IN: bitmap with selected tests to execute. */ +#define XEN_SYSCTL_TEST_SMC_INSN_REPL (1U << 0) +#define XEN_SYSCTL_TEST_SMC_ALL (XEN_SYSCTL_TEST_SMC_INSN_REPL) + uint32_t results; /* OUT: test result: 1 -> success, 0 -> failure. */ +}; + struct xen_sysctl { uint32_t cmd; #define XEN_SYSCTL_readconsole 1 @@ -1232,6 +1239,7 @@ struct xen_sysctl { /* #define XEN_SYSCTL_set_parameter 28 */ #define XEN_SYSCTL_get_cpu_policy 29 #define XEN_SYSCTL_dt_overlay 30 +#define XEN_SYSCTL_test_smc 31 uint32_t interface_version; /* XEN_SYSCTL_INTERFACE_VERSION */ union { struct xen_sysctl_readconsole readconsole; @@ -1261,6 +1269,7 @@ struct xen_sysctl { struct xen_sysctl_livepatch_op livepatch; #if defined(__i386__) || defined(__x86_64__) struct xen_sysctl_cpu_policy cpu_policy; + struct xen_sysctl_test_smc smc; #endif #if defined(__arm__) || defined (__aarch64__) diff --git a/xen/include/xen/lib.h b/xen/include/xen/lib.h index 1793be5b6b89..1bec6a01b18a 100644 --- a/xen/include/xen/lib.h +++ b/xen/include/xen/lib.h @@ -167,6 +167,7 @@ uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c); #define TAINT_HVM_FEP (1u << 3) #define TAINT_MACHINE_INSECURE (1u << 4) #define TAINT_CPU_OUT_OF_SPEC (1u << 5) +#define TAINT_ERROR_SMC (1U << 6) extern unsigned int tainted; #define TAINT_STRING_MAX_LEN 20 extern char *print_tainted(char *str); From patchwork Tue Nov 28 10:03:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Roger_Pau_Monn=C3=A9?= X-Patchwork-Id: 13470773 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id CB4CAC4167B for ; Tue, 28 Nov 2023 10:04:28 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.642878.1002681 (Exim 4.92) (envelope-from ) id 1r7uws-0003Fr-FH; Tue, 28 Nov 2023 10:04:10 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 642878.1002681; Tue, 28 Nov 2023 10:04:10 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1r7uws-0003ED-BN; Tue, 28 Nov 2023 10:04:10 +0000 Received: by outflank-mailman (input) for mailman id 642878; Tue, 28 Nov 2023 10:04:09 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1r7uwr-0002NP-9o for xen-devel@lists.xenproject.org; Tue, 28 Nov 2023 10:04:09 +0000 Received: from mail-lj1-x233.google.com (mail-lj1-x233.google.com [2a00:1450:4864:20::233]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 78a63ff1-8dd5-11ee-98e3-6d05b1d4d9a1; Tue, 28 Nov 2023 11:04:08 +0100 (CET) Received: by mail-lj1-x233.google.com with SMTP id 38308e7fff4ca-2c59a4dd14cso59502321fa.2 for ; Tue, 28 Nov 2023 02:04:08 -0800 (PST) Received: from localhost ([213.195.113.99]) by smtp.gmail.com with ESMTPSA id s7-20020a05600c45c700b0040b37f1079dsm15402156wmo.29.2023.11.28.02.04.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Nov 2023 02:04:07 -0800 (PST) X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 78a63ff1-8dd5-11ee-98e3-6d05b1d4d9a1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=citrix.com; s=google; t=1701165847; x=1701770647; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=+6FykrpCamJbjFK3ebQ5tpnfxbk6kWdBuuLGd0nmB/Q=; b=cU86KHKAC3655vnPm5kvBbv4D1E/8iROqxn1tgEgTx/UhIxMx1fzqmRVt/DKkFHHd8 mg/3HpXWEnVodFWXFzadPXkl3iKIP9SVLbxoly05Y1ACjO1shat1It0e+OdHkf85ljaZ iDKYG810qarnbQSK5iwf4IWnfQ0S8N8YYSAgo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701165847; x=1701770647; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=+6FykrpCamJbjFK3ebQ5tpnfxbk6kWdBuuLGd0nmB/Q=; b=dDFsHpzXLD3kroKdOhFUVceXx5HuyhXz164hNLzu0dfP0mESGDesEQ4UWBhuCpDxaw UWAzVbFouqeafFl77BqXWY4P6JpNFDu4D+jhDpyoKXHDPGI4k0dNTAwIGIkpVbaudH7L yHiymFk2ABcLH/3YZo9gnCBUDMm/d2to0rP5GrtWuAni5JXu9FBE9lwf8NJ8bfWIdB48 OQuvXY9TWRI9+Apir2W628Pd3nr0JB8GnRI86iilXHKiVkv5yL97SX/2Px7KBo4etAOy AAE11beXxi6F3+C37x4y8t6Eo9WE51JeiMUD+Pt8MfARnj3p0fuvCUusQJjkSJkXFrUm /XZA== X-Gm-Message-State: AOJu0YxHSSUlHJmOueMvXjGkvXHvjM45m/dmb3wJBUPx8Zfr+hDZ/R/P ET2R5h8MlCtsOu+TTkeUi51Jx50hKjvCOBNNmUs= X-Google-Smtp-Source: AGHT+IHF2djFkoNstPWyfXNmbJAwb5aYM7dmCbAtf3cyAvrTxaziGV47Fc+srammLi7ku6r/+i/UCA== X-Received: by 2002:a2e:2245:0:b0:2c9:9fbf:52b1 with SMTP id i66-20020a2e2245000000b002c99fbf52b1mr4865431lji.13.1701165847693; Tue, 28 Nov 2023 02:04:07 -0800 (PST) From: Roger Pau Monne To: xen-devel@lists.xenproject.org Cc: Roger Pau Monne , Konrad Rzeszutek Wilk , Ross Lagerwall , Wei Liu , Anthony PERARD , Andrew Cooper , George Dunlap , Jan Beulich , Julien Grall , Stefano Stabellini Subject: [PATCH v2 4/5] x86/livepatch: introduce a basic live patch test to gitlab CI Date: Tue, 28 Nov 2023 11:03:51 +0100 Message-ID: <20231128100352.35430-5-roger.pau@citrix.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20231128100352.35430-1-roger.pau@citrix.com> References: <20231128100352.35430-1-roger.pau@citrix.com> MIME-Version: 1.0 Introduce a basic livepatch test using the interface to run self modifying tests. The introduced test relies on changing a function from returning false to returning true. To simplify the burden of keeping a patch that can be provided to livepatch-build-tools, introduce two new files: one containing the unpatched test functions, and another one that contains the patched forms of such functions. Note that only the former is linked into the Xen image, the latter is built but the object file is not consumed afterwards. Do this to assert that the file containing the patched functions continues to build. Since livepatch testing will ensure that the functions are not patched previous the applying the livepatch, allow the livepatch related tests to fail without tainting the hypervisor. Note the livepatch tests are not run as part of the self modifying checks executed during boot, as they would obviously fail. Signed-off-by: Roger Pau Monné --- Changes since v1: - New interface & test. --- tools/misc/xen-livepatch.c | 29 +++++++++++++++++++++++++++++ xen/arch/x86/Makefile | 2 ++ xen/arch/x86/include/asm/test-smc.h | 2 ++ xen/arch/x86/setup.c | 2 +- xen/arch/x86/test-smc-lp-alt.c | 23 +++++++++++++++++++++++ xen/arch/x86/test-smc-lp.c | 23 +++++++++++++++++++++++ xen/arch/x86/test-smc.c | 11 ++++++++++- xen/include/public/sysctl.h | 6 +++++- 8 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 xen/arch/x86/test-smc-lp-alt.c create mode 100644 xen/arch/x86/test-smc-lp.c diff --git a/tools/misc/xen-livepatch.c b/tools/misc/xen-livepatch.c index 5bf9d9a32b65..fb396f46aaac 100644 --- a/tools/misc/xen-livepatch.c +++ b/tools/misc/xen-livepatch.c @@ -37,6 +37,7 @@ void show_help(void) " replace apply patch and revert all others.\n" " unload unload name patch.\n" " load [flags] upload and apply with name as the name\n" + " test execute self modifying code livepatch hypervisor tests\n" " Supported flags:\n" " --nodeps Disable inter-module buildid dependency check.\n" " Check only against hypervisor buildid.\n", @@ -542,6 +543,33 @@ error: return rc; } +static int test_func(int argc, char *argv[]) +{ + uint32_t results = 0; + int rc; + + if ( argc != 0 ) + { + show_help(); + return -1; + } + + rc = xc_test_smc(xch, XEN_SYSCTL_TEST_SMC_LP, &results); + if ( rc ) + { + fprintf(stderr, "test operation failed: %s\n", strerror(errno)); + return -1; + } + if ( (results & XEN_SYSCTL_TEST_SMC_LP) != XEN_SYSCTL_TEST_SMC_LP ) + { + fprintf(stderr, "some tests failed: %#x (expected %#x)\n", + results, XEN_SYSCTL_TEST_SMC_LP); + return -1; + } + + return 0; +} + /* * These are also functions in action_options that are called in case * none of the ones in main_options match. @@ -554,6 +582,7 @@ struct { { "list", list_func }, { "upload", upload_func }, { "load", load_func }, + { "test", test_func }, }; int main(int argc, char *argv[]) diff --git a/xen/arch/x86/Makefile b/xen/arch/x86/Makefile index bdd2183a2fd7..71cb22e080b8 100644 --- a/xen/arch/x86/Makefile +++ b/xen/arch/x86/Makefile @@ -66,6 +66,8 @@ obj-y += spec_ctrl.o obj-y += srat.o obj-y += string.o obj-y += test-smc.o +obj-$(CONFIG_LIVEPATCH) += test-smc-lp.o # for livepatch testing +extra-$(CONFIG_LIVEPATCH) += test-smc-lp-alt.o obj-y += time.o obj-y += traps.o obj-y += tsx.o diff --git a/xen/arch/x86/include/asm/test-smc.h b/xen/arch/x86/include/asm/test-smc.h index 18b23dbdbf2d..6013e4daf7f8 100644 --- a/xen/arch/x86/include/asm/test-smc.h +++ b/xen/arch/x86/include/asm/test-smc.h @@ -5,6 +5,8 @@ int test_smc(uint32_t selection, uint32_t *results); +bool cf_check test_lp_insn_replacement(void); + #endif /* _ASM_X86_TEST_SMC_H_ */ /* diff --git a/xen/arch/x86/setup.c b/xen/arch/x86/setup.c index 1f90d30204fe..8bfb394909b4 100644 --- a/xen/arch/x86/setup.c +++ b/xen/arch/x86/setup.c @@ -1953,7 +1953,7 @@ void __init noreturn __start_xen(unsigned long mbi_p) alternative_branches(); - test_smc(XEN_SYSCTL_TEST_SMC_ALL, NULL); + test_smc(XEN_SYSCTL_TEST_SMC_ALL & ~XEN_SYSCTL_TEST_SMC_LP, NULL); /* * NB: when running as a PV shim VCPUOP_up/down is wired to the shim diff --git a/xen/arch/x86/test-smc-lp-alt.c b/xen/arch/x86/test-smc-lp-alt.c new file mode 100644 index 000000000000..7bde547a950d --- /dev/null +++ b/xen/arch/x86/test-smc-lp-alt.c @@ -0,0 +1,23 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#include + +/* + * Interesting case because `return false` can be encoded as an xor + * instruction, which is shorter than `return true` which is a mov instruction, + * and also shorter than a jmp instruction. + */ +bool cf_check test_lp_insn_replacement(void) +{ + return true; +} + +/* + * Local variables: + * mode: C + * c-file-style: "BSD" + * c-basic-offset: 4 + * tab-width: 4 + * indent-tabs-mode: nil + * End: + */ diff --git a/xen/arch/x86/test-smc-lp.c b/xen/arch/x86/test-smc-lp.c new file mode 100644 index 000000000000..0ae776053a42 --- /dev/null +++ b/xen/arch/x86/test-smc-lp.c @@ -0,0 +1,23 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#include + +/* + * Interesting case because `return false` can be encoded as an xor + * instruction, which is shorter than `return true` which is a mov instruction, + * and also shorter than a jmp instruction. + */ +bool cf_check test_lp_insn_replacement(void) +{ + return false; +} + +/* + * Local variables: + * mode: C + * c-file-style: "BSD" + * c-basic-offset: 4 + * tab-width: 4 + * indent-tabs-mode: nil + * End: + */ diff --git a/xen/arch/x86/test-smc.c b/xen/arch/x86/test-smc.c index 8916c185d60a..1967016a229f 100644 --- a/xen/arch/x86/test-smc.c +++ b/xen/arch/x86/test-smc.c @@ -27,6 +27,10 @@ int test_smc(uint32_t selection, uint32_t *results) } static const tests[] = { { XEN_SYSCTL_TEST_SMC_INSN_REPL, &test_insn_replacement, "alternative instruction replacement" }, +#ifdef CONFIG_LIVEPATCH + { XEN_SYSCTL_TEST_SMC_LP_INSN, &test_lp_insn_replacement, + "livepatch instruction replacement" }, +#endif }; unsigned int i; @@ -50,7 +54,12 @@ int test_smc(uint32_t selection, uint32_t *results) continue; } - add_taint(TAINT_ERROR_SMC); + /* + * livepatch related tests don't taint the hypervisor because we also + * want to check the failing case. + */ + if ( !(tests[i].mask & XEN_SYSCTL_TEST_SMC_LP) ) + add_taint(TAINT_ERROR_SMC); printk(XENLOG_ERR "%s test failed\n", tests[i].name); } diff --git a/xen/include/public/sysctl.h b/xen/include/public/sysctl.h index 94287009387c..c87878e72a42 100644 --- a/xen/include/public/sysctl.h +++ b/xen/include/public/sysctl.h @@ -1204,7 +1204,11 @@ struct xen_sysctl_dt_overlay { struct xen_sysctl_test_smc { uint32_t tests; /* IN: bitmap with selected tests to execute. */ #define XEN_SYSCTL_TEST_SMC_INSN_REPL (1U << 0) -#define XEN_SYSCTL_TEST_SMC_ALL (XEN_SYSCTL_TEST_SMC_INSN_REPL) +#define XEN_SYSCTL_TEST_SMC_LP_INSN (1U << 1) +#define XEN_SYSCTL_TEST_SMC_ALL (XEN_SYSCTL_TEST_SMC_INSN_REPL | \ + XEN_SYSCTL_TEST_SMC_LP_INSN) +#define XEN_SYSCTL_TEST_SMC_LP (XEN_SYSCTL_TEST_SMC_LP_INSN) + uint32_t results; /* OUT: test result: 1 -> success, 0 -> failure. */ }; From patchwork Tue Nov 28 10:03:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Roger_Pau_Monn=C3=A9?= X-Patchwork-Id: 13470772 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 19F80C07CA9 for ; Tue, 28 Nov 2023 10:04:24 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.642879.1002695 (Exim 4.92) (envelope-from ) id 1r7uwu-0003kN-Ur; Tue, 28 Nov 2023 10:04:12 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 642879.1002695; Tue, 28 Nov 2023 10:04:12 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1r7uwu-0003k6-PC; Tue, 28 Nov 2023 10:04:12 +0000 Received: by outflank-mailman (input) for mailman id 642879; Tue, 28 Nov 2023 10:04:11 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1r7uwt-0001sW-70 for xen-devel@lists.xenproject.org; Tue, 28 Nov 2023 10:04:11 +0000 Received: from mail-wr1-x42b.google.com (mail-wr1-x42b.google.com [2a00:1450:4864:20::42b]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 789cfc28-8dd5-11ee-9b0e-b553b5be7939; Tue, 28 Nov 2023 11:04:08 +0100 (CET) Received: by mail-wr1-x42b.google.com with SMTP id ffacd0b85a97d-32f8441dfb5so3556602f8f.0 for ; Tue, 28 Nov 2023 02:04:09 -0800 (PST) Received: from localhost ([213.195.113.99]) by smtp.gmail.com with ESMTPSA id q4-20020adfea04000000b003296b488961sm14424366wrm.31.2023.11.28.02.04.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Nov 2023 02:04:08 -0800 (PST) X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 789cfc28-8dd5-11ee-9b0e-b553b5be7939 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=citrix.com; s=google; t=1701165849; x=1701770649; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=nnuz0Skbms73/IaPgLyxoHfzJBbgmoe2G5CAGghY1zE=; b=EvyUOAyWFfQeUKqPPAvz7cGbCtL7Kd8tNXqur5RZOegb6soTcpJAQR86EIltYITbig udhtyWbMjKz5Z4vJfeE2G/OF2H/K9bYP3B3B35W+Hinh56BJHyIkEErScWA5r0gSwzpQ sAtgXrA5TttkKYcb3SKfCnaXbxqfUyUWf1vxc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701165849; x=1701770649; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=nnuz0Skbms73/IaPgLyxoHfzJBbgmoe2G5CAGghY1zE=; b=iRfMOFVFKsZ3FGDJIRBul5ZgsVvsNbOrmOF+789uerC4V37ut9wppImas6svZcY1zc sRsS+NHg6OWkjPXLp2IaQDPA7Gp305Dd6rgFKa+jLc1j1rmBOB8qKaMKpkwGLLPam+/V YIWVJ3+tZjtl4qxSzg+kz1emaLi3SxzbFq5AZFRSqqtcXdM1YI/ctpyD9o5oEVD1rAyi 2Lwynr/yPIKCTaVS1ndSQhX/WEZhbF+8AjeDMzLzU8rfU5/j0guWuHOzvyJzK/QFlkbJ QWsnT6LRgJaM/5DKiiKzxrMnnjk6uuHIVTlr1rKv7XB01FQzc4JX6uWYURNr0TQuX6So 0UYw== X-Gm-Message-State: AOJu0YxbkNV5hfCX7uoWmjU1MUltjGk0ETC0AjTtq8X5Nlzg3Bf1Ktni yL0Xa/QT2ChbotasjVGuy5JY1MkuMJj+Yr60Bbc= X-Google-Smtp-Source: AGHT+IHueuC4uSA1qK/jZqwfC/DCqs0QYWMuzHDFCxW3pLiIfFHCUM1C9WKXGeqKkljXCWPSFTQk1w== X-Received: by 2002:a5d:6a42:0:b0:332:c585:400a with SMTP id t2-20020a5d6a42000000b00332c585400amr10615660wrw.44.1701165848859; Tue, 28 Nov 2023 02:04:08 -0800 (PST) From: Roger Pau Monne To: xen-devel@lists.xenproject.org Cc: Roger Pau Monne , Doug Goldstein , Stefano Stabellini Subject: [PATCH v2 5/5] automation: add x86-64 livepatching test Date: Tue, 28 Nov 2023 11:03:52 +0100 Message-ID: <20231128100352.35430-6-roger.pau@citrix.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20231128100352.35430-1-roger.pau@citrix.com> References: <20231128100352.35430-1-roger.pau@citrix.com> MIME-Version: 1.0 Introduce a new gitlab tests for livepatching, using livepatch-build-tools, which better reflects how downstreams build live patches rather than the in-tree tests. The tests applies the dummy in-tree patch example, checks that the patch is applied correctly and then reverts and unloads it. Signed-off-by: Roger Pau Monné --- automation/gitlab-ci/build.yaml | 8 +++ automation/gitlab-ci/test.yaml | 8 +++ automation/scripts/build | 21 ++++++ .../scripts/qemu-alpine-x86_64-livepatch.sh | 68 +++++++++++++++++++ 4 files changed, 105 insertions(+) create mode 100755 automation/scripts/qemu-alpine-x86_64-livepatch.sh diff --git a/automation/gitlab-ci/build.yaml b/automation/gitlab-ci/build.yaml index 32af30ccedc9..22026df51b87 100644 --- a/automation/gitlab-ci/build.yaml +++ b/automation/gitlab-ci/build.yaml @@ -358,6 +358,14 @@ alpine-3.18-gcc-debug: variables: CONTAINER: alpine:3.18 +alpine-3.18-gcc-livepatch: + extends: .gcc-x86-64-build + variables: + CONTAINER: alpine:3.18 + LIVEPATCH: y + EXTRA_XEN_CONFIG: | + CONFIG_LIVEPATCH=y + debian-stretch-gcc-debug: extends: .gcc-x86-64-build-debug variables: diff --git a/automation/gitlab-ci/test.yaml b/automation/gitlab-ci/test.yaml index 6aabdb9d156f..58a90be5ed0e 100644 --- a/automation/gitlab-ci/test.yaml +++ b/automation/gitlab-ci/test.yaml @@ -459,3 +459,11 @@ qemu-smoke-ppc64le-powernv9-gcc: needs: - qemu-system-ppc64-8.1.0-ppc64-export - debian-bullseye-gcc-ppc64le-debug + +qemu-alpine-x86_64-gcc-livepatch: + extends: .qemu-x86-64 + script: + - ./automation/scripts/qemu-alpine-x86_64-livepatch.sh 2>&1 | tee ${LOGFILE} + needs: + - *x86-64-test-needs + - alpine-3.18-gcc-livepatch diff --git a/automation/scripts/build b/automation/scripts/build index b3c71fb6fb60..0a0a6dceb08c 100755 --- a/automation/scripts/build +++ b/automation/scripts/build @@ -103,3 +103,24 @@ else cp -r dist binaries/ if [[ -f xen/xen ]] ; then cp xen/xen binaries/xen; fi fi + +if [[ "$LIVEPATCH" == "y" ]]; then + # Build a test livepatch using livepatch-build-tools. + + if [[ "$XEN_TARGET_ARCH" != "x86_64" ]]; then + exit 1 + fi + + # git diff --no-index returns 0 if no differences, otherwise 1. + git diff --no-index --output=test.patch xen/arch/x86/test-smc-lp.c \ + xen/arch/x86/test-smc-lp-alt.c && exit 1 + + BUILDID=$(readelf -Wn xen/xen-syms | sed -n -e 's/^.*Build ID: //p') + + git clone https://xenbits.xen.org/git-http/livepatch-build-tools.git + cd livepatch-build-tools + make + ./livepatch-build -s ../ -p ../test.patch -o out -c ../xen/.config \ + --depends $BUILDID --xen-depends $BUILDID + cp out/test.livepatch ../binaries/test.livepatch +fi diff --git a/automation/scripts/qemu-alpine-x86_64-livepatch.sh b/automation/scripts/qemu-alpine-x86_64-livepatch.sh new file mode 100755 index 000000000000..da478cac4376 --- /dev/null +++ b/automation/scripts/qemu-alpine-x86_64-livepatch.sh @@ -0,0 +1,68 @@ +#!/bin/bash + +set -ex + +cd binaries +# initrd.tar.gz is Dom0 rootfs +mkdir -p rootfs +cd rootfs +tar xvzf ../initrd.tar.gz +mkdir proc +mkdir run +mkdir srv +mkdir sys +rm var/run +cp -ar ../dist/install/* . +cp ../test.livepatch ./root/ +cat << "EOF" >> etc/local.d/xen-lp.start +#!/bin/bash + +set -ex + +trap poweroff EXIT + +export LD_LIBRARY_PATH=/usr/local/lib + +xen-livepatch test && exit 1 || true + +xen-livepatch load /root/test.livepatch + +# Cannot fail now +xen-livepatch test + +xen-livepatch revert test +xen-livepatch unload test + +xen-livepatch test && exit 1 || true + +echo "SUCCESS" +EOF +chmod +x etc/local.d/xen-lp.start +echo "rc_verbose=yes" >> etc/rc.conf +# rebuild Dom0 rootfs +find . |cpio -H newc -o|gzip > ../xen-rootfs.cpio.gz +cd ../.. + +cat >> binaries/pxelinux.0 << EOF +#!ipxe + +kernel xen console=com1 console_timestamps=boot +module bzImage console=hvc0 +module xen-rootfs.cpio.gz +boot +EOF + +# Run the test +rm -f smoke.serial +timeout -k 1 360 \ +qemu-system-x86_64 \ + -cpu qemu64,+svm \ + -m 2G -smp 2 \ + -monitor none -serial stdio \ + -nographic \ + -device virtio-net-pci,netdev=n0 \ + -netdev user,id=n0,tftp=binaries,bootfile=/pxelinux.0 |& \ + tee smoke.serial | sed 's/\r//' + +grep -q "SUCCESS" smoke.serial +exit 0