Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (prime256v1) server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 45789606A32C for ; Thu, 3 Dec 2026 16:55:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=hCSrV0eK Received: from list by lists.xenproject.org with outflank-mailman.1512775.1704718 (Exim 4.92) (envelope-from ) id 1xZA5e-00044h-DV; Thu, 03 Dec 2026 16:55:26 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 1512775.1704718; Thu, 03 Dec 2026 16:55:26 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xZA5e-00044a-Ay; Thu, 03 Dec 2026 16:55:26 +0000 Received: by outflank-mailman (input) for mailman id 1512775; Thu, 03 Dec 2026 16:55:25 +0000 Received: from mx.expurgate.net ([195.190.135.10]) by lists.xenproject.org with esmtp (Exim 4.92) id 1xZA5d-00044M-KN for xen-devel@lists.xenproject.org; Thu, 03 Dec 2026 16:55:25 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1xZA5c-00DBVv-1F for xen-devel@lists.xenproject.org; Thu, 03 Dec 2026 17:55:24 +0100 Received: from [10.42.69.1] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 6b119eef-bab6-0a2a0a5309dd-0a2a4501bc9a-36 for ; Thu, 03 Dec 2026 17:55:24 +0100 Received: from [209.85.218.49] (helo=mail-ej1-f49.google.com) by tlsNG-d62444.mxtls.expurgate.net with ESMTPS (eXpurgate 4.57.1) (envelope-from ) id 6b119efb-5bcc-0a2a45010019-d155da31e432-3 for ; Thu, 03 Dec 2026 17:55:23 +0100 Received: by mail-ej1-f49.google.com with SMTP id a640c23a62f3a-c51d158acc6so17569266b.0 for ; Thu, 03 Dec 2026 08:55:23 -0800 (PST) Received: from fedora (user-109-243-65-71.play-internet.pl. [109.243.65.71]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-c525b400311sm3271366b.53.2026.12.03.08.55.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Dec 2026 08:55:22 -0800 (PST) X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Post: List-Help: List-Subscribe: , Precedence: list Sender: "Xen-devel" Authentication-Results: eu.smtp.expurgate.cloud; dkim=pass header.s=20260707 header.d=gmail.com header.i="@gmail.com" header.h="Content-Transfer-Encoding:MIME-Version:Message-ID:Date:Subject:Cc:To:From" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796316923; x=1796921723; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=Hp+xQUNwUCqdyshIWQjDR8SnyQI7eaey7hLXyCRm2fs=; b=hCSrV0eKz8n2MfkltJ3JzaYELZ5r63os4Ll09tc+HFsDpy+jnwDoOCZUjyz0AdknlT QtxEhLYRAvbh4s5e7OysJ0r1WNQJi9QG4sMTXX4R68EPEjezC4k8GnaFctAh4+0+1Qdd MDRYEvb3efsP8B4jsZB4sajeqhsn4Wy+6SsQpnz6Tl9uaI5XiMTnfqT/DwBHrSOzuXJw B5/bk8g5xIPZ1OaL8+crYZqsNXvMHEinxoAgRCRdVcHf+XyaiUc8po/LXJV8Xo8apVRR cM/EKZ573Fbrg9nsU+87NmlmkxgIOccA8B7IXydp7/ySOPArjRsx4+BmVSJjDRbkLgCp M6zA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796316923; x=1796921723; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=Hp+xQUNwUCqdyshIWQjDR8SnyQI7eaey7hLXyCRm2fs=; b=CGC0Hvmhn03vUyOZcY1UKnvLuC1hPbvINIPUy7uyXOCNM00fE9IZ3pBqCqsHuv0aDF uVqmWKD+yMhn4AdGAdcdNCuw1OxHsn3ZK6S4QzKjlZ39st0gA4gPeWblWza744DgKYc4 ynO/C9RfHLhYcTnqnzcWQHaKVG5yBTYTECPAqZaa+O++hrAOpCiR/5T0HdNTvZpruqpB uKFZiG8AcRywnME3eF0CbPjEjAURCFGRu3KsFb+SamFFe1zT2RNCZqMvpp6ZseJSwSFD OiWDvbHHa7SxEC7FYlxy7iiPZYhJ+tlxo6nNoPe24JNmcjCFqAj1T+mEgC+EmowNyoHG OhDg== X-Gm-Message-State: APuU4Z8hOWOi28Wg7duO2hTn3Xz/AvQUwEh88EM9o13Mco81V0vgjKWP +QDyIzUUHWUe7JJqdhOicLfPs5XGI/tNrE7W8vH87SZWXvBHm/okT2daqpcyQA== X-Gm-Gg: AZFdTZcY8XHLCMZAr6HyvTLk57MoyAsmQD6IwsnQMHJeRyC8v1c9OXynpY8/NN5TmGa cf/lmisxdAInhzfzFQxgvr4muNlyUkChzT0Aqv/D4eH1mT9U6yzFZjiq9iNubhbazs0ifiM2Q0m eunveXCw6mGIGWRuWIeRDzFS91/2734CZCpr4tN7t90tWcyZyNpHQp03xYJtwmGw/upWgFq3t8k Uq/Ytv9wmJAEdeRS9QvMop5v5GF8JId4ZpcGinSN6X6GE/nC0fwgALPXeAK5a3BuFVbRCM3fqST /d9gVncT3a6Xu5lyKnyMp2xEbj2lr7IRxyCXEyYBn5M+kVQ3bkFu3sKcH9Fb9aAjV8+9HkkIyDv CnXje8jfsNZ+6osX7/5IiSuWXB8Uyobw/cQsbPtfSfoO6ZNswUlNYLnZZ0un2gVkw47//heHMCr f86ocEupT0FTKHMxhyrmYTpo0BGWIgEOJdasWJYAKEZ6jMQGjyNBr2sOIgQlrWf8oO4S3k4GhPG kWWMh7WEB3+GW+s7rDNJDZj5oGbrRbFFEQjbgnTJr/xnRKfSfyY14LTRF6r5XHi/hzEpfeIus42 Mplr0ff82Mz855zp1P7GTb3ARmsDiJo2f71IQe6kvycnmzl8MJAJ9drTqJXEh9uspPg/ X-Received: by 2002:a17:906:9555:b0:c50:52b5:e17 with SMTP id a640c23a62f3a-c513a5e1d53mr3096288766b.13.1796316923038; Thu, 03 Dec 2026 08:55:23 -0800 (PST) From: Oleksii Kurochko To: xen-devel@lists.xenproject.org Cc: Bruno Verachten , Oleksii Kurochko , Alistair Francis , Baptiste Le Duc , Connor Davis , Andrew Cooper , Anthony PERARD , Michal Orzel , Jan Beulich , Julien Grall , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Stefano Stabellini Subject: [PATCH] xen/riscv: look up exception table entries by faulting address Date: Thu, 3 Dec 2026 17:55:20 +0100 Message-ID: <20261203165520.163545-1-oleksii.kurochko@gmail.com> X-Mailer: git-send-email 2.56.1 X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-purgate-ID: tlsNG-d62444/1796316923-4FB4E5FD-7CF8FABD/10/73395122804 X-purgate-type: spam X-purgate-size: 3092 X-Spam-Status: No, score=-20.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,MENTIONS_GIT_HOSTING,SPF_HELO_PASS,SPF_PASS, UNPARSEABLE_RELAY,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 From: Bruno Verachten fixup_exception() searched the exception table with a key built on its own stack: key.insn = pc - &key.insn, the distance from the key.insn field to pc. key.insn is an int32_t, so this only works while the stack is within 2 GiB of Xen's text. Today that holds, as Xen runs on cpu0_boot_stack, which is part of the Xen image, and the existing users (csr_read_safe() and the A/D update probe) run at boot. It stops holding once Xen runs on vCPU stacks: these are allocated with vzalloc() in arch_vcpu_create() and sit in the VMAP area, far more than 2 GiB away from Xen's text. The distance is then truncated, the address the key decodes to lands next to the stack rather than in Xen's text, and bsearch() misses the entry, so a fault that is meant to be recovered from reaches the BUG_ON() or do_unexpected_trap() in do_trap() and stops Xen. Pass the faulting address itself as the key, with a comparator that takes it as such. The entries keep their relative encoding. Fixes: a651dabf873d ("xen/riscv: add exception table support") Assisted-by: Claude:claude-opus-5-5 Signed-off-by: Bruno Verachten Signed-off-by: Oleksii Kurochko --- I took the patch from [1] with some minimal changes which could be found in discussion from [1]. [1] https://gitlab.com/xen-project/people/olkur/xen/-/merge_requests/39 --- --- xen/arch/riscv/extable.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/xen/arch/riscv/extable.c b/xen/arch/riscv/extable.c index 5b89c4278c65..c7898cac741f 100644 --- a/xen/arch/riscv/extable.c +++ b/xen/arch/riscv/extable.c @@ -42,6 +42,20 @@ static int cf_check cmp_ex(const void *a, const void *b) return (insn_a > insn_b) - (insn_a < insn_b); } +/* + * bsearch() comparator looking up an exception table entry by address. + * + * pc_ptr - pointer to the faulting address + * ex - exception table entry to compare it with + */ +static int cf_check cmp_ex_addr(const void *pc_ptr, const void *ex) +{ + const unsigned long pc = *(const unsigned long *)pc_ptr; + const unsigned long insn = ex_insn(ex); + + return (pc > insn) - (pc < insn); +} + void init_or_livepatch sort_exception_table(struct exception_table_entry *start, const struct exception_table_entry *stop) { @@ -64,15 +78,12 @@ bool fixup_exception(struct cpu_user_regs *regs) unsigned long pc = regs->sepc; const struct virtual_region *region = find_text_region(pc); const struct exception_table_entry *ex; - struct exception_table_entry key; if ( !region || !region->ex ) return false; - key.insn = pc - (unsigned long)&key.insn; - - ex = bsearch(&key, region->ex, region->ex_end - region->ex, sizeof(key), - cmp_ex); + ex = bsearch(&pc, region->ex, region->ex_end - region->ex, sizeof(*ex), + cmp_ex_addr); if ( !ex ) return false; -- 2.56.1