From nobody Fri Sep 25 13:19:17 2026 Received: from mail-qk2-f12.google.com (mail-qk2-f12.google.com [74.125.230.204]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 068C045D91E for ; Fri, 11 Sep 2026 21:33:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.230.204 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789162438; cv=none; b=pwZVGfPbLDQliUg8UTtRPtUWC5xKrU3mrtzbMCHSOe8+j6yY+aVr2wAsTIx52VidvjU67l4q5Miv2Eu27JtY0vX6evaIIM9Jw0myGaWThVGb7OfCzJi1ZxGgyX5iO+gjmx4rTCuGzpIpaDTHtiv/0/OvDp4qTzLg3U4e0tIr0YY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789162438; c=relaxed/simple; bh=IWB+cbvxBldSdWrEOxLvkQCXUWd+bHotGsjaP4R+oH8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=oLAEjWUOAYje0aD8Ai13Gtls2kEtMy1IKyu4sY6aihYb30UGiLggvyDweimLq8H/EathHKf5FeIQ0KMZs+azN6ahm78Ni4NEuJdVMPFNkQV6SS3fdI3zBiDnQpniNCDDs+OZo2etZZeAYy9abwOYzUDhmikMUIEmEURNRnr8d2U= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=s1afA1+N; arc=none smtp.client-ip=74.125.230.204 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="s1afA1+N" Received: by mail-qk2-f12.google.com with SMTP id af79cd13be357-939ca12ab70so2910485a.1 for ; Fri, 11 Sep 2026 14:33:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1789162435; x=1789767235; darn=vger.kernel.org; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to:content-type; bh=OV9D4q5YUye7Q1vIF9rdT+FmHjwXYwylKYDxv971ac0=; b=s1afA1+Nh/fV4+gGA6CqndLK2SISNZd9EwcvK1RkyCSdS9pF+Jlt9zAwX247rwgL75 0IzRb0/cByFW1HWbNWrgF92HCqKhF4fT3qikXg8bNYzIZTUdIyL6GZHv3AEECgNsIu5a soAM3/0y8r8alN+tylS4UXWeKVUodfum6IepdITxJfcK639iS3KCTI2Zwv0eFdjxCKkA JgiCePhLKolXUAwjvLy1ElboYv5dDql5ymi/UY96C0zqEjkFyd1U8b96nWNDLyES1l1z 7EHF9rEdNIqJ5FndsiZe/JKa/ZQdk1mNtbepCaoIzSYk4bBMhLvpmzFy8SwsDHs19Bsz RyJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1789162435; x=1789767235; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to: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=OV9D4q5YUye7Q1vIF9rdT+FmHjwXYwylKYDxv971ac0=; b=D1i7S0nc1n1OAo8PJYyCuNlMI6ncorAHwZVstWO+iLMwCoHE7AhyvXZknTV2yFQvT1 TBRe8aZIk/1qkqskZRaYqH/xdDRE8AyDmjtdtJlhYat6teD1TsgcJN/JSwJ+bKGwcuUv EjR4Bp7/kLL2ZfcpWUYvPehScf+s5Nv/JgU5doPwZ1NlfvVWqn5Bhk4Xt7dAJ/brevIg RcoX4bUVxPRv9tr1YGf+G1UhEijWp8GjLpEQkGiaGCrPDWJSNJZqJ67VK+wDZwaFdccn QrA3rvA6i4aXGn6rGEONiD8PzXR7lfQW4m9lOAkDTHw43yHM02DQGcW3jcj/P7sYphLt VEKA== X-Forwarded-Encrypted: i=1; AKwUvBwOapy5mwetM4Tr95TWiAIaQrtYo1oSCBhHNo03BQw2sb5UjWUpWLEWJD9bKmRXlO6WI2jZ5r5lJnnsqnQ=@vger.kernel.org X-Gm-Message-State: AFuF++ldV2h+IYgr3lrQPEOF7uE/Qg0NypIt1ljtwHL9fNx6Cr7wI/0Y NrdWcW2tXPg+EU4aTJSP74P5xwMTB5X28lBdVpSwBdsRkx7/NGInTe+z X-Gm-Gg: AYBFou3iK6mshYiz4hpr12+mFB/0+ngXYYiwOL9t/XyRMfAKJHuJWE2JuHEE12oUwAf XXbBqtdcBdCFlSGDBR7Rf/bnN8lmMChYu+gePqZ7avFTXToUuxBs74mdQNHguE6GYRzS+fePVlz pmThBMFslfci1QsuUxQ/pMDHFf3JsjwO+GP4QU/YgAZzT/2xlg91RElGwh3tQW5fE6STg4vIVb0 42Df+gE1KXxg4tMHEZgVef49NfTNfCjRl1Bv8oH1TOnSHlm0alLmcAD8a3hhs/NBmYBDWpAi44U nTHaHz14ll7mk/XH285qWfBiMqULOS5x7RP+XHELSuUHknO4oWY/QYAPyQaox26yXcSJBxZvLtE z6ktMM+FSlJ22aFeJ9cu2oMOn4SC9+eogMoLu4kTdGFxhNn2JfqOspMG6GQmVIsJZgEfPCXhvvQ FbKT836y8fM2m7RS5Ww8mcgwGaq+saty3WyP2QLeYZRSmEpPwmnkGcK1lMdmD2p1Lj X-Received: by 2002:a05:620a:444c:b0:939:d913:9a74 with SMTP id af79cd13be357-93a03939ffbmr20881785a.46.1789162434800; Fri, 11 Sep 2026 14:33:54 -0700 (PDT) Received: from Turin ([187.15.143.6]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-9120f496598sm30170196d6.35.2026.09.11.14.33.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Sep 2026 14:33:54 -0700 (PDT) From: Nicholas Dudar To: tsbogend@alpha.franken.de, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, eddyz87@gmail.com, memxor@gmail.com, johan.almbladh@anyfinetworks.com, paulburton@kernel.org Cc: martin.lau@linux.dev, song@kernel.org, yonghong.song@linux.dev, jolsa@kernel.org, emil@etsalapatis.com, ihor.solodrai@linux.dev, linux-mips@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, philmd@oss.qualcomm.com Subject: [PATCH bpf-next v4 1/3] bpf, mips: Factor out div/mod emission helpers Date: Fri, 11 Sep 2026 17:33:38 -0400 Message-ID: <20260911213340.3767930-2-main.kalliope@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260911213340.3767930-1-main.kalliope@gmail.com> References: <20260911213340.3767930-1-main.kalliope@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Factor MIPS32 and MIPS64 division and modulo emission out of emit_alu_r() and emit_alu_r64(). This prepares the JITs to select signed or unsigned opcodes without duplicating the R6 and pre-R6 handling. No functional change intended. Suggested-by: Philippe Mathieu-Daud=C3=A9 Reviewed-by: Philippe Mathieu-Daud=C3=A9 Assisted-by: Codex:gpt-5.6-sol Signed-off-by: Nicholas Dudar Acked-by: Johan Almbladh Tested-by: Johan Almbladh --- arch/mips/net/bpf_jit_comp.c | 36 ++++++++++++++++++++++------------ arch/mips/net/bpf_jit_comp64.c | 36 ++++++++++++++++++++++------------ 2 files changed, 48 insertions(+), 24 deletions(-) diff --git a/arch/mips/net/bpf_jit_comp.c b/arch/mips/net/bpf_jit_comp.c index 6ee4abe6a1f..320180330fb 100644 --- a/arch/mips/net/bpf_jit_comp.c +++ b/arch/mips/net/bpf_jit_comp.c @@ -338,6 +338,28 @@ void emit_alu_i(struct jit_context *ctx, u8 dst, s32 i= mm, u8 op) clobber_reg(ctx, dst); } =20 +/* ALU division operation (32-bit) */ +static void emit_div(struct jit_context *ctx, u8 dst, u8 src) +{ + if (cpu_has_mips32r6) { + emit(ctx, divu_r6, dst, dst, src); + } else { + emit(ctx, divu, dst, src); + emit(ctx, mflo, dst); + } +} + +/* ALU modulo operation (32-bit) */ +static void emit_mod(struct jit_context *ctx, u8 dst, u8 src) +{ + if (cpu_has_mips32r6) { + emit(ctx, modu, dst, dst, src); + } else { + emit(ctx, divu, dst, src); + emit(ctx, mfhi, dst); + } +} + /* ALU register operation (32-bit) */ void emit_alu_r(struct jit_context *ctx, u8 dst, u8 src, u8 op) { @@ -385,21 +407,11 @@ void emit_alu_r(struct jit_context *ctx, u8 dst, u8 s= rc, u8 op) break; /* dst =3D dst / src */ case BPF_DIV: - if (cpu_has_mips32r6) { - emit(ctx, divu_r6, dst, dst, src); - } else { - emit(ctx, divu, dst, src); - emit(ctx, mflo, dst); - } + emit_div(ctx, dst, src); break; /* dst =3D dst % src */ case BPF_MOD: - if (cpu_has_mips32r6) { - emit(ctx, modu, dst, dst, src); - } else { - emit(ctx, divu, dst, src); - emit(ctx, mfhi, dst); - } + emit_mod(ctx, dst, src); break; } clobber_reg(ctx, dst); diff --git a/arch/mips/net/bpf_jit_comp64.c b/arch/mips/net/bpf_jit_comp64.c index fa7e9aa37f4..2520e1db7ab 100644 --- a/arch/mips/net/bpf_jit_comp64.c +++ b/arch/mips/net/bpf_jit_comp64.c @@ -197,6 +197,28 @@ static void emit_alu_i64(struct jit_context *ctx, u8 d= st, s32 imm, u8 op) clobber_reg(ctx, dst); } =20 +/* ALU division operation (64-bit) */ +static void emit_div64(struct jit_context *ctx, u8 dst, u8 src) +{ + if (cpu_has_mips64r6) { + emit(ctx, ddivu_r6, dst, dst, src); + } else { + emit(ctx, ddivu, dst, src); + emit(ctx, mflo, dst); + } +} + +/* ALU modulo operation (64-bit) */ +static void emit_mod64(struct jit_context *ctx, u8 dst, u8 src) +{ + if (cpu_has_mips64r6) { + emit(ctx, dmodu, dst, dst, src); + } else { + emit(ctx, ddivu, dst, src); + emit(ctx, mfhi, dst); + } +} + /* ALU register operation (64-bit) */ static void emit_alu_r64(struct jit_context *ctx, u8 dst, u8 src, u8 op) { @@ -235,21 +257,11 @@ static void emit_alu_r64(struct jit_context *ctx, u8 = dst, u8 src, u8 op) break; /* dst =3D dst / src */ case BPF_DIV: - if (cpu_has_mips64r6) { - emit(ctx, ddivu_r6, dst, dst, src); - } else { - emit(ctx, ddivu, dst, src); - emit(ctx, mflo, dst); - } + emit_div64(ctx, dst, src); break; /* dst =3D dst % src */ case BPF_MOD: - if (cpu_has_mips64r6) { - emit(ctx, dmodu, dst, dst, src); - } else { - emit(ctx, ddivu, dst, src); - emit(ctx, mfhi, dst); - } + emit_mod64(ctx, dst, src); break; default: /* Width-generic operations */ From nobody Fri Sep 25 13:19:17 2026 Received: from mail-qv1-f49.google.com (mail-qv1-f49.google.com [209.85.219.49]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D81FE486429 for ; Fri, 11 Sep 2026 21:33:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789162440; cv=none; b=FrJOuuwSZJpz3sRlSGwbAlcGTfxdNdciuxVC/mXj317dtVrgVv3usyjDRFD6g+ph0Hk2FHbeD+mgNErjsS3yattqcpa6gTAfI0boQzW9FfGzZNo6KhW663pX/XtCagpFvHVVsCwiww5ZJgJ62Q+GwE8MclvBF4rK0G4ZxFgcTHk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789162440; c=relaxed/simple; bh=KXU0dEULBLMG2eQ2TXVrJNDUpRxLI4gG/Rawcj/EfCc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=SuqOZafASAsAJlcW4dLz2LrTDBO8Vvnbm14Erlehn1hlaftm6zqbCSh7Ox62PJVopFwRCty2jD4BEcPzF6CnL5qHIIRjnaefJ78GHAKATUiUgWEt//YJ3DC1NA7/GuHWG7TxOjnQxvxa4x8VBbVnV6625yaP1izatJO2T3w66C0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=StKJVxVD; arc=none smtp.client-ip=209.85.219.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="StKJVxVD" Received: by mail-qv1-f49.google.com with SMTP id 6a1803df08f44-9106ffe17e9so16316236d6.2 for ; Fri, 11 Sep 2026 14:33:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1789162438; x=1789767238; darn=vger.kernel.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:content-type; bh=rWWlWLFADBsCkSr4cxa/Iolx0HAZzLibqF5JxnIfQmw=; b=StKJVxVDhPBabD9yFkbQe2c1+VNNrZZlWnhGyUnCCNnBce5K1FfH3m7CdI7kq4sqaT ibgpSQXpdeCvi5l2CzQR5PTidGm7lF7dMuiWFAtqMkECoXCj4gvz3tGx/zCS6bspveV8 OcOyUJnkucuFfdj8rgt9IC0+4j6e1TNms9+m3fimNWXtAgpivk4ECDpN7RNS9MXoiy08 PJgbZ5qC2a136Z4c+zjizd16ofA0AkU7GW5yEF9KdYNKArOFTQGcwcoZm2pjP7d7DU1x 9NZz8fAmtIiVTptM8PFKF5sYGhf4ALacDCKHbwq9Oqw9l31+MMc/I2vInyX9WAgwXkKn MrLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1789162438; x=1789767238; h=content-transfer-encoding:mime-version:references:in-reply-to :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=rWWlWLFADBsCkSr4cxa/Iolx0HAZzLibqF5JxnIfQmw=; b=Dk17akZsBa1T8Qz7XmPtenycJppnYO/1caf7mq4uwyECUwEQ+1SyxNmCjqpJRnZthY swv514F8bUtP2fF6VPBhxNQd7G9AXJ78bWCjJSVAVltcJx4T1+wkDcieREnVLOOiIo1b 3nl2+Jwd38b5su6pY8fSFngC8jOrXbJ0h/i+Pue0JqVYnOQnMOSCMB2S48slMdB/Vf4o zFy0GHNi3KV5gE6w0TNT2bWqtuuEAJ/Oi12UZdcJ2BuusejS9tD+6bbr83torX8DsJsO 46vQZaQjPVjQjikudFHbCUx3PAdpY93Mrg99mOvTRm6BvHmoApaO425UTzh1qsW5gR8e AHZw== X-Forwarded-Encrypted: i=1; AKwUvBy71YQCuFvJDjJnY6N77Zq607snrBCmUMvIp4ceML07Z4cWIdAKHLw6461W/8iwI6Ol2nKugd/HIUQA4Oc=@vger.kernel.org X-Gm-Message-State: AFuF++kPXDR79Q5lwXegP8SjIq4QidwNod+4I/YpDvkDMBbl+q2D0QZy HLACaqS2jrqsAaW7/vugjuF2tCoXWE4Iy+haobi7uQq7CMDLuk/nlOEU X-Gm-Gg: AYBFou18aWINKpMPoyzIynAvfB+t+jNxfwk5RulbbdTbp/gLHp4FDJgH6i5cr4BNZfk vY6sGi5utA4Ltel7wyGmt8CI0uCSe8j8rTVpLdDWsLyZu7PZNtAzC7H/7Pvs4zWf43HUKDIVgBB eELybp6ccu3wIDT+hdyeBGrtF9pEp4zwsSEsJfp7qDvjBX3SSNXQUVBxr3ofH8tTACFcZuHqToL GXHcZgG+k1xOZjvM1QcNPRrJ68ftyDkg6N9YDaq+pu8x65G9D1y/9YRoy7KH/sTASB313vqeNyA efKIOo7w+M+HVgultVEcqs7+bxfh5/QQBS3KxnRfLRzFukC4UmfeyTZxsSpa8VYjTMZ5KxND3qH wX7JCSwtqpIoNMwUf2/yDFJvt0nn/L6jBWjvRrBUElEfYGWKz/ZjTV1ZUNORyTl+o2yObZ6XUlF sg29F7RaX2t3JepTIwxUMkvoOGcnHYWPvQQ8diGyLdGhxrCkH31wyMKe0LYBW2mLSH3Fi75S+mi wdx X-Received: by 2002:a05:622a:313:b0:519:efea:cf41 with SMTP id d75a77b69052e-530c869dabamr92604001cf.28.1789162437757; Fri, 11 Sep 2026 14:33:57 -0700 (PDT) Received: from Turin ([187.15.143.6]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-9120f496598sm30170196d6.35.2026.09.11.14.33.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Sep 2026 14:33:57 -0700 (PDT) From: Nicholas Dudar To: tsbogend@alpha.franken.de, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, eddyz87@gmail.com, memxor@gmail.com, johan.almbladh@anyfinetworks.com, paulburton@kernel.org Cc: martin.lau@linux.dev, song@kernel.org, yonghong.song@linux.dev, jolsa@kernel.org, emil@etsalapatis.com, ihor.solodrai@linux.dev, linux-mips@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, philmd@oss.qualcomm.com Subject: [PATCH bpf-next v4 2/3] MIPS: uasm: Add signed div/mod and sign-extension emitters Date: Fri, 11 Sep 2026 17:33:39 -0400 Message-ID: <20260911213340.3767930-3-main.kalliope@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260911213340.3767930-1-main.kalliope@gmail.com> References: <20260911213340.3767930-1-main.kalliope@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add uasm emitters for the signed DIV, MOD, DDIV, and DMOD instructions. Provide both pre-R6 and R6 division forms so users can select the instruction appropriate for the target ISA. Add SEB and SEH emitters for R2-or-newer CPUs. These will be used by the follow-on BPF_MOVSX series. Assisted-by: Codex:gpt-6 Signed-off-by: Nicholas Dudar Link: https://lore.kernel.org/bpf/CAM1=3D_QS79dDBfaaNQXnWd61AqHd1M4o9aDMMif= tnoJveNr=3DFZg@mail.gmail.com/ Link: https://lore.kernel.org/bpf/CAM1=3D_QTN3JJ6jtckf32F8wxBzLDbHSrzUttyMB= WNqZKmRe=3D2rg@mail.gmail.com/ Acked-by: Johan Almbladh Tested-by: Johan Almbladh --- arch/mips/include/asm/uasm.h | 8 ++++++++ arch/mips/mm/uasm-mips.c | 12 ++++++++++++ arch/mips/mm/uasm.c | 24 +++++++++++++++++------- 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/arch/mips/include/asm/uasm.h b/arch/mips/include/asm/uasm.h index b43bfd44525..adea35df0f8 100644 --- a/arch/mips/include/asm/uasm.h +++ b/arch/mips/include/asm/uasm.h @@ -85,15 +85,20 @@ Ip_u1u2(_ctc1); Ip_u2u1(_ctcmsa); Ip_u2u1s3(_daddiu); Ip_u3u1u2(_daddu); +Ip_u1u2(_ddiv); +Ip_u3u1u2(_ddiv_r6); Ip_u1u2(_ddivu); Ip_u3u1u2(_ddivu_r6); Ip_u1(_di); Ip_u2u1msbu3(_dins); Ip_u2u1msbu3(_dinsm); Ip_u2u1msbu3(_dinsu); +Ip_u1u2(_div); +Ip_u3u1u2(_div_r6); Ip_u1u2(_divu); Ip_u3u1u2(_divu_r6); Ip_u1u2u3(_dmfc0); +Ip_u3u1u2(_dmod); Ip_u3u1u2(_dmodu); Ip_u1u2u3(_dmtc0); Ip_u1u2(_dmultu); @@ -135,6 +140,7 @@ Ip_u1u2u3(_mfc0); Ip_u1u2u3(_mfhc0); Ip_u1(_mfhi); Ip_u1(_mflo); +Ip_u3u1u2(_mod); Ip_u3u1u2(_modu); Ip_u3u1u2(_movn); Ip_u3u1u2(_movz); @@ -156,6 +162,8 @@ Ip_u2s3u1(_sb); Ip_u2s3u1(_sc); Ip_u2s3u1(_scd); Ip_u2s3u1(_sd); +Ip_u2u1(_seb); +Ip_u2u1(_seh); Ip_u3u1u2(_seleqz); Ip_u3u1u2(_selnez); Ip_u2s3u1(_sh); diff --git a/arch/mips/mm/uasm-mips.c b/arch/mips/mm/uasm-mips.c index e15c6700cd0..2d1668b58dd 100644 --- a/arch/mips/mm/uasm-mips.c +++ b/arch/mips/mm/uasm-mips.c @@ -75,6 +75,9 @@ static const struct insn insn_table[insn_invalid] =3D { [insn_ctcmsa] =3D {M(msa_op, 0, msa_ctc_op, 0, 0, msa_elm_op), RD | RE}, [insn_daddiu] =3D {M(daddiu_op, 0, 0, 0, 0, 0), RS | RT | SIMM}, [insn_daddu] =3D {M(spec_op, 0, 0, 0, 0, daddu_op), RS | RT | RD}, + [insn_ddiv] =3D {M(spec_op, 0, 0, 0, 0, ddiv_op), RS | RT}, + [insn_ddiv_r6] =3D {M(spec_op, 0, 0, 0, ddiv_ddiv6_op, ddiv_op), + RS | RT | RD}, [insn_ddivu] =3D {M(spec_op, 0, 0, 0, 0, ddivu_op), RS | RT}, [insn_ddivu_r6] =3D {M(spec_op, 0, 0, 0, ddivu_ddivu6_op, ddivu_op), RS | RT | RD}, @@ -82,10 +85,15 @@ static const struct insn insn_table[insn_invalid] =3D { [insn_dins] =3D {M(spec3_op, 0, 0, 0, 0, dins_op), RS | RT | RD | RE}, [insn_dinsm] =3D {M(spec3_op, 0, 0, 0, 0, dinsm_op), RS | RT | RD | RE}, [insn_dinsu] =3D {M(spec3_op, 0, 0, 0, 0, dinsu_op), RS | RT | RD | RE}, + [insn_div] =3D {M(spec_op, 0, 0, 0, 0, div_op), RS | RT}, + [insn_div_r6] =3D {M(spec_op, 0, 0, 0, div_div6_op, div_op), + RS | RT | RD}, [insn_divu] =3D {M(spec_op, 0, 0, 0, 0, divu_op), RS | RT}, [insn_divu_r6] =3D {M(spec_op, 0, 0, 0, divu_divu6_op, divu_op), RS | RT | RD}, [insn_dmfc0] =3D {M(cop0_op, dmfc_op, 0, 0, 0, 0), RT | RD | SET}, + [insn_dmod] =3D {M(spec_op, 0, 0, 0, ddiv_dmod_op, ddiv_op), + RS | RT | RD}, [insn_dmodu] =3D {M(spec_op, 0, 0, 0, ddivu_dmodu_op, ddivu_op), RS | RT | RD}, [insn_dmtc0] =3D {M(cop0_op, dmtc_op, 0, 0, 0, 0), RT | RD | SET}, @@ -140,6 +148,8 @@ static const struct insn insn_table[insn_invalid] =3D { [insn_mfhc0] =3D {M(cop0_op, mfhc0_op, 0, 0, 0, 0), RT | RD | SET}, [insn_mfhi] =3D {M(spec_op, 0, 0, 0, 0, mfhi_op), RD}, [insn_mflo] =3D {M(spec_op, 0, 0, 0, 0, mflo_op), RD}, + [insn_mod] =3D {M(spec_op, 0, 0, 0, div_mod_op, div_op), + RS | RT | RD}, [insn_modu] =3D {M(spec_op, 0, 0, 0, divu_modu_op, divu_op), RS | RT | RD}, [insn_movn] =3D {M(spec_op, 0, 0, 0, 0, movn_op), RS | RT | RD}, @@ -177,6 +187,8 @@ static const struct insn insn_table[insn_invalid] =3D { [insn_scd] =3D {M6(spec3_op, 0, 0, 0, scd6_op), RS | RT | SIMM9}, #endif [insn_sd] =3D {M(sd_op, 0, 0, 0, 0, 0), RS | RT | SIMM}, + [insn_seb] =3D {M(spec3_op, 0, 0, 0, seb_op, bshfl_op), RT | RD}, + [insn_seh] =3D {M(spec3_op, 0, 0, 0, seh_op, bshfl_op), RT | RD}, [insn_seleqz] =3D {M(spec_op, 0, 0, 0, 0, seleqz_op), RS | RT | RD}, [insn_selnez] =3D {M(spec_op, 0, 0, 0, 0, selnez_op), RS | RT | RD}, [insn_sh] =3D {M(sh_op, 0, 0, 0, 0, 0), RS | RT | SIMM}, diff --git a/arch/mips/mm/uasm.c b/arch/mips/mm/uasm.c index 125140979d6..139afb0e14c 100644 --- a/arch/mips/mm/uasm.c +++ b/arch/mips/mm/uasm.c @@ -49,19 +49,21 @@ enum opcode { insn_addiu, insn_addu, insn_and, insn_andi, insn_bbit0, insn_bbit1, insn_beq, insn_beql, insn_bgez, insn_bgezl, insn_bgtz, insn_blez, insn_bltz, insn_bltzl, insn_bne, insn_break, insn_cache, insn_cfc1, - insn_cfcmsa, insn_ctc1, insn_ctcmsa, insn_daddiu, insn_daddu, insn_ddivu, - insn_ddivu_r6, insn_di, insn_dins, insn_dinsm, insn_dinsu, insn_divu, - insn_divu_r6, insn_dmfc0, insn_dmodu, insn_dmtc0, insn_dmultu, + insn_cfcmsa, insn_ctc1, insn_ctcmsa, insn_daddiu, insn_daddu, insn_ddiv, + insn_ddiv_r6, insn_ddivu, insn_ddivu_r6, insn_di, insn_dins, insn_dinsm, + insn_dinsu, insn_div, insn_div_r6, insn_divu, insn_divu_r6, insn_dmfc0, + insn_dmod, insn_dmodu, insn_dmtc0, insn_dmultu, insn_dmulu, insn_drotr, insn_drotr32, insn_dsbh, insn_dshd, insn_dsll, insn_dsll32, insn_dsllv, insn_dsra, insn_dsra32, insn_dsrav, insn_dsrl, insn_dsrl32, insn_dsrlv, insn_dsubu, insn_eret, insn_ext, insn_ins, insn_j, insn_jal, insn_jalr, insn_jr, insn_lb, insn_lbu, insn_ld, insn_lddir, insn_ldpte, insn_ldx, insn_lh, insn_lhu, insn_ll, insn_lld, insn_lui, insn_lw, insn_lwu, insn_lwx, insn_mfc0, insn_mfhc0, insn_mfhi, - insn_mflo, insn_modu, insn_movn, insn_movz, insn_mtc0, insn_mthc0, - insn_mthi, insn_mtlo, insn_mul, insn_multu, insn_mulu, insn_muhu, insn_no= r, - insn_or, insn_ori, insn_pref, insn_rfe, insn_rotr, insn_sb, insn_sc, - insn_scd, insn_seleqz, insn_selnez, insn_sd, insn_sh, insn_sll, + insn_mflo, insn_mod, insn_modu, insn_movn, insn_movz, insn_mtc0, + insn_mthc0, insn_mthi, insn_mtlo, insn_mul, insn_multu, insn_mulu, + insn_muhu, insn_nor, insn_or, insn_ori, insn_pref, insn_rfe, insn_rotr, + insn_sb, insn_sc, insn_scd, insn_seb, insn_seh, + insn_seleqz, insn_selnez, insn_sd, insn_sh, insn_sll, insn_sllv, insn_slt, insn_slti, insn_sltiu, insn_sltu, insn_sra, insn_srav, insn_srl, insn_srlv, insn_subu, insn_sw, insn_sync, insn_syscall, insn_tlbp, insn_tlbr, insn_tlbwi, insn_tlbwr, insn_wait, @@ -287,9 +289,12 @@ I_u1u2(_cfc1) I_u2u1(_cfcmsa) I_u1u2(_ctc1) I_u2u1(_ctcmsa) +I_u1u2(_ddiv) +I_u3u1u2(_ddiv_r6) I_u1u2(_ddivu) I_u3u1u2(_ddivu_r6) I_u1u2u3(_dmfc0) +I_u3u1u2(_dmod) I_u3u1u2(_dmodu) I_u1u2u3(_dmtc0) I_u1u2(_dmultu) @@ -297,6 +302,8 @@ I_u3u1u2(_dmulu) I_u2u1s3(_daddiu) I_u3u1u2(_daddu) I_u1(_di); +I_u1u2(_div) +I_u3u1u2(_div_r6) I_u1u2(_divu) I_u3u1u2(_divu_r6) I_u2u1(_dsbh); @@ -332,6 +339,7 @@ I_u2s3u1(_lw) I_u2s3u1(_lwu) I_u1u2u3(_mfc0) I_u1u2u3(_mfhc0) +I_u3u1u2(_mod) I_u3u1u2(_modu) I_u3u1u2(_movn) I_u3u1u2(_movz) @@ -353,6 +361,8 @@ I_u2s3u1(_sb) I_u2s3u1(_sc) I_u2s3u1(_scd) I_u2s3u1(_sd) +I_u2u1(_seb) +I_u2u1(_seh) I_u3u1u2(_seleqz) I_u3u1u2(_selnez) I_u2s3u1(_sh) From nobody Fri Sep 25 13:19:17 2026 Received: from mail-qv1-f49.google.com (mail-qv1-f49.google.com [209.85.219.49]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 718B0485928 for ; Fri, 11 Sep 2026 21:34:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789162444; cv=none; b=svdoY3C1DE9XZsZVIfWoqxoZh36zSj6D0QwVLbpidgSm8CrfLZKPvq4HcEyrW+YgE0Bupti/CMsqEdcnqtBk+8HS9E+/VpIoRM5Zp4paH3KycFhO/mG2uSzEXdGvmVJOGFg2UNsIDzNkcZ2QfRf8wOmkNkZEeMXXttnn8j4ZNb8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789162444; c=relaxed/simple; bh=uA0ykF/9XLZdNv217gEJhJFeitz39AccivE1H/bor60=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=obZEHlEnb1bWOM/RIKujT3p1QeoIE3r3L5nrTmRo2d4fQGDAEE5Uvbj9nXrSkreaXyx+fhCF8i1tsewI+x7u89MevQNeRxXgGwyHBFqoZXGP89c/DPw8YIJFCUqj6btS+jWwFved+HvaY3/dGGolQfzsRjlJvjECy/HnyCT+qMg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=q7LS1QUz; arc=none smtp.client-ip=209.85.219.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="q7LS1QUz" Received: by mail-qv1-f49.google.com with SMTP id 6a1803df08f44-9120eda2195so14992716d6.1 for ; Fri, 11 Sep 2026 14:34:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1789162441; x=1789767241; darn=vger.kernel.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:content-type; bh=eUgjt9sq7z3DvWbwF3U4aS6WwCN2ZGW8QdNmOZJwJVc=; b=q7LS1QUzxuQU9DAahKwpa3D6pNaef94voTKx+Y+Q0f0cnQA9j54ZQ0c5r4qOw4mAra a6kPtm3AcDaWHoQNHgLOhx4uf5WvKAoQMXp2nlRs5pYZQ42Iovb8SRiZPaczb+cnPIIG /VZEXDv5Mnq1ymgpshCLZZjDjjEaatakT1KLz7VJCUn3OvyrUZGi+KO8CYGRJUjaA0I1 3K0WddIr2ANG30yvx2PgqAfXNRKXrIDLQO2hubrhk/okxL96q4xMmQnWqD4XmJ68ORwT zdHdiwim6vmfhBcbxiPN8dql3WRjnaN1PaElihk3ZEMiUPphhoOnYtVBW/iqAox2aK0H dDdQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1789162441; x=1789767241; h=content-transfer-encoding:mime-version:references:in-reply-to :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=eUgjt9sq7z3DvWbwF3U4aS6WwCN2ZGW8QdNmOZJwJVc=; b=pHb+pMpRbzqtZr3aae+dEhbJW2H2Ktttd8Vwc6cK5D91xDYdLBI745NE7HUIpuag3i DOWKfFqL8u+lyqAgaXdR7IKt5TxgRKL3zAx925LFmhTXl91yiLhql1nhKTeQG1u9cofw YUipMCM8Qavs6fgMBwRzkm+g5KPsmeogp68gLsVYR1Ad5QBjxlH3GcYkeAj7KcMHDI3K XB8VUca4rW3bZ3ktDuV/1V1piYQ5LA/Z92J9GPgB6mbBTgUjR259Wg7uZ6+Xwj2O1KQl I5lVb2vCmVobqcEABfRvA4D13Wg9/fjZNUiKVVjN02nwrx8SIQUsfESkyB4XdAdZG/D0 igpQ== X-Forwarded-Encrypted: i=1; AKwUvBzF7RGKf6UDUW2RhMksOmAmgZWLSZlEK5X+CQdWX07Bb9UdP4gEJ3+bGFbhDaVfHzU5Pq6mQchqZA3Zkfs=@vger.kernel.org X-Gm-Message-State: AFuF++kw/jsuyi1WhcyuFvtv8ZbtIpNwGClq2yJzuvUWxHD1uwp5Vx3k P1rYWJE0AE7qulW6XjfRmwR1tBORuYtST8hsc58pApD2KpOJt3v4RYyI X-Gm-Gg: AYBFou16pJEBYju9iQ1ydHgMhXHvbNAnEknEgxnvggyz2QxzF0WnqYnzi4z2V8/WaJE ifbB9GIPrQPqHVXUuBFdmKvSGNU7EfgtMbO5OPCcrrZxdXsqJNS8c38JLZRq+xmz3qD6Vvo80c7 +AuPBWMHFKdfQnaYxWBEMUzp0rttz3zHo9AWfnmubkygFM2FHjp72tqh7p8lqKkrO7hGqNpYMlQ 4C3yfbR5AOJpHQX5JTq72hD9lgnUekMMmvHHc3JssZiuQOy/ENrZR/er81BrdXCqM3fUeqzbz8R UktH+kbfyyMb/2kbBPoXSf2BWXScPVeF+IbXYzRV1k8e54Xh5BaxjF7zFbiZqHBCkGJsx/aGUv8 e6qyO0SnqWxxAVI8a5sEHcGMGbx3foMbftGRthjULq6m93pNhItiTd7/kgHAHemJo5+fp7x0nb1 N4nigT0QGrAaN16GZ7KbJM6jy4tZb4piA0DqOz5nHNl3DL3iniQIDfDhSOxu6TbU5tiQ== X-Received: by 2002:a05:6214:460b:b0:910:3b67:b793 with SMTP id 6a1803df08f44-91202981ce7mr138281966d6.24.1789162441098; Fri, 11 Sep 2026 14:34:01 -0700 (PDT) Received: from Turin ([187.15.143.6]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-9120f496598sm30170196d6.35.2026.09.11.14.33.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Sep 2026 14:34:00 -0700 (PDT) From: Nicholas Dudar To: tsbogend@alpha.franken.de, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, eddyz87@gmail.com, memxor@gmail.com, johan.almbladh@anyfinetworks.com, paulburton@kernel.org Cc: martin.lau@linux.dev, song@kernel.org, yonghong.song@linux.dev, jolsa@kernel.org, emil@etsalapatis.com, ihor.solodrai@linux.dev, linux-mips@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, philmd@oss.qualcomm.com Subject: [PATCH bpf-next v4 3/3] bpf, mips: Add support for BPF_SDIV and BPF_SMOD Date: Fri, 11 Sep 2026 17:33:40 -0400 Message-ID: <20260911213340.3767930-4-main.kalliope@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260911213340.3767930-1-main.kalliope@gmail.com> References: <20260911213340.3767930-1-main.kalliope@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The MIPS JITs handle BPF_DIV and BPF_MOD without inspecting insn->off, which distinguishes BPF_SDIV and BPF_SMOD. Signed operations therefore use unsigned instructions, or unsigned helpers for ALU64 on 32-bit MIPS, and produce unsigned results for negative operands. Pass insn->off unchanged through immediate validation and register emission, and interpret it only in div/mod-specific paths. Use signed DIV/DDIV for pre-R6 and DIV/MOD/DDIV/DMOD for R6. On 32-bit MIPS, use div64_s64() and a signed remainder helper for ALU64. Keep the signed quotient signed while computing that remainder, and keep signed immediates out of the unsigned power-of-two shift/mask rewrite. check_alu_op() rejects immediate zero divisors. bpf_do_misc_fixups() rewrites signed immediate -1 and guards register divisors that are zero or, for signed operations, -1 before JIT compilation. The ALU32 paths continue to zero-extend their results. Found while auditing BPF JIT handling of signed div/mod. Assisted-by: Codex:gpt-5.6-sol Signed-off-by: Nicholas Dudar Link: https://lore.kernel.org/bpf/CAM1=3D_QS79dDBfaaNQXnWd61AqHd1M4o9aDMMif= tnoJveNr=3DFZg@mail.gmail.com/ Link: https://lore.kernel.org/bpf/CAM1=3D_QQ7XiJEozaetLt_+kVn91YAXWpJK_pyfT= 9=3DpaXseW=3DwwA@mail.gmail.com/ Acked-by: Johan Almbladh Tested-by: Johan Almbladh --- arch/mips/net/bpf_jit_comp.c | 35 ++++++++++++++++------- arch/mips/net/bpf_jit_comp.h | 4 +-- arch/mips/net/bpf_jit_comp32.c | 29 ++++++++++++------- arch/mips/net/bpf_jit_comp64.c | 51 +++++++++++++++++++++------------- 4 files changed, 78 insertions(+), 41 deletions(-) diff --git a/arch/mips/net/bpf_jit_comp.c b/arch/mips/net/bpf_jit_comp.c index 320180330fb..f14de6d7312 100644 --- a/arch/mips/net/bpf_jit_comp.c +++ b/arch/mips/net/bpf_jit_comp.c @@ -208,7 +208,7 @@ void emit_mov_r(struct jit_context *ctx, u8 dst, u8 src) } =20 /* Validate ALU immediate range */ -bool valid_alu_i(u8 op, s32 imm) +bool valid_alu_i(u8 op, s32 imm, s16 off) { switch (BPF_OP(op)) { case BPF_NEG: @@ -237,6 +237,9 @@ bool valid_alu_i(u8 op, s32 imm) return imm =3D=3D 0 || (imm > 0 && is_power_of_2(imm)); case BPF_DIV: case BPF_MOD: + /* Do not use unsigned shift/mask rewrites for signed div/mod. */ + if (off =3D=3D 1) + return false; /* imm must be an 17-bit power of two */ return (u32)imm <=3D 0x10000 && is_power_of_2((u32)imm); } @@ -339,29 +342,41 @@ void emit_alu_i(struct jit_context *ctx, u8 dst, s32 = imm, u8 op) } =20 /* ALU division operation (32-bit) */ -static void emit_div(struct jit_context *ctx, u8 dst, u8 src) +static void emit_div(struct jit_context *ctx, u8 dst, u8 src, s16 off) { if (cpu_has_mips32r6) { - emit(ctx, divu_r6, dst, dst, src); + if (off =3D=3D 1) + emit(ctx, div_r6, dst, dst, src); + else + emit(ctx, divu_r6, dst, dst, src); } else { - emit(ctx, divu, dst, src); + if (off =3D=3D 1) + emit(ctx, div, dst, src); + else + emit(ctx, divu, dst, src); emit(ctx, mflo, dst); } } =20 /* ALU modulo operation (32-bit) */ -static void emit_mod(struct jit_context *ctx, u8 dst, u8 src) +static void emit_mod(struct jit_context *ctx, u8 dst, u8 src, s16 off) { if (cpu_has_mips32r6) { - emit(ctx, modu, dst, dst, src); + if (off =3D=3D 1) + emit(ctx, mod, dst, dst, src); + else + emit(ctx, modu, dst, dst, src); } else { - emit(ctx, divu, dst, src); + if (off =3D=3D 1) + emit(ctx, div, dst, src); + else + emit(ctx, divu, dst, src); emit(ctx, mfhi, dst); } } =20 /* ALU register operation (32-bit) */ -void emit_alu_r(struct jit_context *ctx, u8 dst, u8 src, u8 op) +void emit_alu_r(struct jit_context *ctx, u8 dst, u8 src, u8 op, s16 off) { switch (BPF_OP(op)) { /* dst =3D dst & src */ @@ -407,11 +422,11 @@ void emit_alu_r(struct jit_context *ctx, u8 dst, u8 s= rc, u8 op) break; /* dst =3D dst / src */ case BPF_DIV: - emit_div(ctx, dst, src); + emit_div(ctx, dst, src, off); break; /* dst =3D dst % src */ case BPF_MOD: - emit_mod(ctx, dst, src); + emit_mod(ctx, dst, src, off); break; } clobber_reg(ctx, dst); diff --git a/arch/mips/net/bpf_jit_comp.h b/arch/mips/net/bpf_jit_comp.h index a37fe20818e..0ddedb733dd 100644 --- a/arch/mips/net/bpf_jit_comp.h +++ b/arch/mips/net/bpf_jit_comp.h @@ -163,7 +163,7 @@ void emit_mov_i(struct jit_context *ctx, u8 dst, s32 im= m); void emit_mov_r(struct jit_context *ctx, u8 dst, u8 src); =20 /* Validate ALU/ALU64 immediate range */ -bool valid_alu_i(u8 op, s32 imm); +bool valid_alu_i(u8 op, s32 imm, s16 off); =20 /* Rewrite ALU/ALU64 immediate operation */ bool rewrite_alu_i(u8 op, s32 imm, u8 *alu, s32 *val); @@ -172,7 +172,7 @@ bool rewrite_alu_i(u8 op, s32 imm, u8 *alu, s32 *val); void emit_alu_i(struct jit_context *ctx, u8 dst, s32 imm, u8 op); =20 /* ALU register operation (32-bit) */ -void emit_alu_r(struct jit_context *ctx, u8 dst, u8 src, u8 op); +void emit_alu_r(struct jit_context *ctx, u8 dst, u8 src, u8 op, s16 off); =20 /* Atomic read-modify-write (32-bit) */ void emit_atomic_r(struct jit_context *ctx, u8 dst, u8 src, s16 off, u8 co= de); diff --git a/arch/mips/net/bpf_jit_comp32.c b/arch/mips/net/bpf_jit_comp32.c index 40a878b672f..6619182b5ef 100644 --- a/arch/mips/net/bpf_jit_comp32.c +++ b/arch/mips/net/bpf_jit_comp32.c @@ -512,7 +512,7 @@ static void emit_mul_r64(struct jit_context *ctx, clobber_reg64(ctx, dst); } =20 -/* Helper function for 64-bit modulo */ +/* Helper function for unsigned 64-bit modulo */ static u64 jit_mod64(u64 a, u64 b) { u64 rem; @@ -521,13 +521,22 @@ static u64 jit_mod64(u64 a, u64 b) return rem; } =20 +/* Helper function for signed 64-bit modulo */ +static s64 jit_smod64(s64 a, s64 b) +{ + s64 quot =3D div64_s64(a, b); + + return a - quot * b; +} + /* ALU div/mod register (64-bit) */ -static void emit_divmod_r64(struct jit_context *ctx, - const u8 dst[], const u8 src[], u8 op) +static void emit_divmod_r64(struct jit_context *ctx, const u8 dst[], + const u8 src[], u8 op, s16 off) { const u8 *r0 =3D bpf2mips32[BPF_REG_0]; /* Mapped to v0-v1 */ const u8 *r1 =3D bpf2mips32[BPF_REG_1]; /* Mapped to a0-a1 */ const u8 *r2 =3D bpf2mips32[BPF_REG_2]; /* Mapped to a2-a3 */ + bool is_signed =3D off =3D=3D 1; int exclude, k; u32 addr =3D 0; =20 @@ -546,11 +555,11 @@ static void emit_divmod_r64(struct jit_context *ctx, switch (BPF_OP(op)) { /* dst =3D dst / src */ case BPF_DIV: - addr =3D (u32)&div64_u64; + addr =3D is_signed ? (u32)&div64_s64 : (u32)&div64_u64; break; /* dst =3D dst % src */ case BPF_MOD: - addr =3D (u32)&jit_mod64; + addr =3D is_signed ? (u32)&jit_smod64 : (u32)&jit_mod64; break; } emit_mov_i(ctx, MIPS_R_T9, addr); @@ -1516,9 +1525,9 @@ int build_insn(const struct bpf_insn *insn, struct ji= t_context *ctx) case BPF_ALU | BPF_MUL | BPF_K: case BPF_ALU | BPF_DIV | BPF_K: case BPF_ALU | BPF_MOD | BPF_K: - if (!valid_alu_i(BPF_OP(code), imm)) { + if (!valid_alu_i(BPF_OP(code), imm, off)) { emit_mov_i(ctx, MIPS_R_T6, imm); - emit_alu_r(ctx, lo(dst), MIPS_R_T6, BPF_OP(code)); + emit_alu_r(ctx, lo(dst), MIPS_R_T6, BPF_OP(code), off); } else if (rewrite_alu_i(BPF_OP(code), imm, &alu, &val)) { emit_alu_i(ctx, lo(dst), val, alu); } @@ -1546,7 +1555,7 @@ int build_insn(const struct bpf_insn *insn, struct ji= t_context *ctx) case BPF_ALU | BPF_MUL | BPF_X: case BPF_ALU | BPF_DIV | BPF_X: case BPF_ALU | BPF_MOD | BPF_X: - emit_alu_r(ctx, lo(dst), lo(src), BPF_OP(code)); + emit_alu_r(ctx, lo(dst), lo(src), BPF_OP(code), off); emit_zext_ver(ctx, dst); break; /* dst =3D imm (64-bit) */ @@ -1599,7 +1608,7 @@ int build_insn(const struct bpf_insn *insn, struct ji= t_context *ctx) * and then do the operation on this register. */ emit_mov_se_i64(ctx, tmp, imm); - emit_divmod_r64(ctx, dst, tmp, BPF_OP(code)); + emit_divmod_r64(ctx, dst, tmp, BPF_OP(code), off); break; /* dst =3D dst & src (64-bit) */ /* dst =3D dst | src (64-bit) */ @@ -1629,7 +1638,7 @@ int build_insn(const struct bpf_insn *insn, struct ji= t_context *ctx) /* dst =3D dst % src (64-bit) */ case BPF_ALU64 | BPF_DIV | BPF_X: case BPF_ALU64 | BPF_MOD | BPF_X: - emit_divmod_r64(ctx, dst, src, BPF_OP(code)); + emit_divmod_r64(ctx, dst, src, BPF_OP(code), off); break; /* dst =3D htole(dst) */ /* dst =3D htobe(dst) */ diff --git a/arch/mips/net/bpf_jit_comp64.c b/arch/mips/net/bpf_jit_comp64.c index 2520e1db7ab..db027c2d922 100644 --- a/arch/mips/net/bpf_jit_comp64.c +++ b/arch/mips/net/bpf_jit_comp64.c @@ -198,29 +198,42 @@ static void emit_alu_i64(struct jit_context *ctx, u8 = dst, s32 imm, u8 op) } =20 /* ALU division operation (64-bit) */ -static void emit_div64(struct jit_context *ctx, u8 dst, u8 src) +static void emit_div64(struct jit_context *ctx, u8 dst, u8 src, s16 off) { if (cpu_has_mips64r6) { - emit(ctx, ddivu_r6, dst, dst, src); + if (off =3D=3D 1) + emit(ctx, ddiv_r6, dst, dst, src); + else + emit(ctx, ddivu_r6, dst, dst, src); } else { - emit(ctx, ddivu, dst, src); + if (off =3D=3D 1) + emit(ctx, ddiv, dst, src); + else + emit(ctx, ddivu, dst, src); emit(ctx, mflo, dst); } } =20 /* ALU modulo operation (64-bit) */ -static void emit_mod64(struct jit_context *ctx, u8 dst, u8 src) +static void emit_mod64(struct jit_context *ctx, u8 dst, u8 src, s16 off) { if (cpu_has_mips64r6) { - emit(ctx, dmodu, dst, dst, src); + if (off =3D=3D 1) + emit(ctx, dmod, dst, dst, src); + else + emit(ctx, dmodu, dst, dst, src); } else { - emit(ctx, ddivu, dst, src); + if (off =3D=3D 1) + emit(ctx, ddiv, dst, src); + else + emit(ctx, ddivu, dst, src); emit(ctx, mfhi, dst); } } =20 /* ALU register operation (64-bit) */ -static void emit_alu_r64(struct jit_context *ctx, u8 dst, u8 src, u8 op) +static void emit_alu_r64(struct jit_context *ctx, u8 dst, u8 src, u8 op, + s16 off) { switch (BPF_OP(op)) { /* dst =3D dst << src */ @@ -257,15 +270,15 @@ static void emit_alu_r64(struct jit_context *ctx, u8 = dst, u8 src, u8 op) break; /* dst =3D dst / src */ case BPF_DIV: - emit_div64(ctx, dst, src); + emit_div64(ctx, dst, src, off); break; /* dst =3D dst % src */ case BPF_MOD: - emit_mod64(ctx, dst, src); + emit_mod64(ctx, dst, src, off); break; default: /* Width-generic operations */ - emit_alu_r(ctx, dst, src, op); + emit_alu_r(ctx, dst, src, op, off); } clobber_reg(ctx, dst); } @@ -686,9 +699,9 @@ int build_insn(const struct bpf_insn *insn, struct jit_= context *ctx) case BPF_ALU | BPF_AND | BPF_K: case BPF_ALU | BPF_XOR | BPF_K: case BPF_ALU | BPF_LSH | BPF_K: - if (!valid_alu_i(BPF_OP(code), imm)) { + if (!valid_alu_i(BPF_OP(code), imm, off)) { emit_mov_i(ctx, MIPS_R_T4, imm); - emit_alu_r(ctx, dst, MIPS_R_T4, BPF_OP(code)); + emit_alu_r(ctx, dst, MIPS_R_T4, BPF_OP(code), off); } else if (rewrite_alu_i(BPF_OP(code), imm, &alu, &val)) { emit_alu_i(ctx, dst, val, alu); } @@ -708,10 +721,10 @@ int build_insn(const struct bpf_insn *insn, struct ji= t_context *ctx) case BPF_ALU | BPF_MUL | BPF_K: case BPF_ALU | BPF_DIV | BPF_K: case BPF_ALU | BPF_MOD | BPF_K: - if (!valid_alu_i(BPF_OP(code), imm)) { + if (!valid_alu_i(BPF_OP(code), imm, off)) { emit_sext(ctx, dst, dst); emit_mov_i(ctx, MIPS_R_T4, imm); - emit_alu_r(ctx, dst, MIPS_R_T4, BPF_OP(code)); + emit_alu_r(ctx, dst, MIPS_R_T4, BPF_OP(code), off); } else if (rewrite_alu_i(BPF_OP(code), imm, &alu, &val)) { emit_sext(ctx, dst, dst); emit_alu_i(ctx, dst, val, alu); @@ -726,7 +739,7 @@ int build_insn(const struct bpf_insn *insn, struct jit_= context *ctx) case BPF_ALU | BPF_OR | BPF_X: case BPF_ALU | BPF_XOR | BPF_X: case BPF_ALU | BPF_LSH | BPF_X: - emit_alu_r(ctx, dst, src, BPF_OP(code)); + emit_alu_r(ctx, dst, src, BPF_OP(code), off); emit_zext_ver(ctx, dst); break; /* dst =3D dst >> src */ @@ -745,7 +758,7 @@ int build_insn(const struct bpf_insn *insn, struct jit_= context *ctx) case BPF_ALU | BPF_MOD | BPF_X: emit_sext(ctx, dst, dst); emit_sext(ctx, MIPS_R_T4, src); - emit_alu_r(ctx, dst, MIPS_R_T4, BPF_OP(code)); + emit_alu_r(ctx, dst, MIPS_R_T4, BPF_OP(code), off); emit_zext_ver(ctx, dst); break; /* dst =3D imm (64-bit) */ @@ -782,9 +795,9 @@ int build_insn(const struct bpf_insn *insn, struct jit_= context *ctx) case BPF_ALU64 | BPF_MUL | BPF_K: case BPF_ALU64 | BPF_DIV | BPF_K: case BPF_ALU64 | BPF_MOD | BPF_K: - if (!valid_alu_i(BPF_OP(code), imm)) { + if (!valid_alu_i(BPF_OP(code), imm, off)) { emit_mov_i(ctx, MIPS_R_T4, imm); - emit_alu_r64(ctx, dst, MIPS_R_T4, BPF_OP(code)); + emit_alu_r64(ctx, dst, MIPS_R_T4, BPF_OP(code), off); } else if (rewrite_alu_i(BPF_OP(code), imm, &alu, &val)) { emit_alu_i64(ctx, dst, val, alu); } @@ -811,7 +824,7 @@ int build_insn(const struct bpf_insn *insn, struct jit_= context *ctx) case BPF_ALU64 | BPF_MUL | BPF_X: case BPF_ALU64 | BPF_DIV | BPF_X: case BPF_ALU64 | BPF_MOD | BPF_X: - emit_alu_r64(ctx, dst, src, BPF_OP(code)); + emit_alu_r64(ctx, dst, src, BPF_OP(code), off); break; /* dst =3D htole(dst) */ /* dst =3D htobe(dst) */