Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp686208pxv; Thu, 22 Jul 2021 09:43:04 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzzz2fiUclQRZqfeF39Bc+IQAn0r2eSb8uv4U9qXKH2GB4KH++d6flePbXqQl4wm7B6gaRF X-Received: by 2002:a5d:88c6:: with SMTP id i6mr466073iol.75.1626972184502; Thu, 22 Jul 2021 09:43:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626972184; cv=none; d=google.com; s=arc-20160816; b=sqD/yzlTMLi5NM2SfnKm4x/Yja8RIh1FARAqLi67/5O6lyK8Ck64BAaHoH34FTEnBp YdVdm5gQ+zd2rygUY/DqHuI8H2OK9OOyicfOZtTLHryzgDisVSBATfYs+qKwbFykOxOX b4+CaEmmgoHqv0secT4iJZugWQOvO/skLM2s+s8A9HHpPAIbeaHL7JCVlRxvnQyyGAYN 5H2uRlJRG0VHbS+a24O7Hq7uoLKHpnQyr4FaqIs/dd2R0STG3goXiMP2rTT7dnZTT2Mg TVjpTdZy1KWMsdGvHKIos9XL/5zg4IN8ZW7dJ7mxEu0FW088gNEvCrK69oupFuDvoG2G Mlwg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=+/2FWmovtXvs0stNcxPrHIi+h5pUNfz2byTr7U5Iro8=; b=vsFvOfR0Z1djMBDR0cJh5+KrgehMaVj5ZPx+rEpZjZoCS5ACZvGZlG28I7Q4JiacsS aIgR4l1sSFrcYxkhSo7laSiewGd07i3nrroYKksZKKMw+Ia2zl87M9RwpF9TbTBpcTI1 X86TqccMb3a236w0PA9wNsEw2li9vwF6Q1L/1/DqK9FDjXIlbtKG1yn1WO9kHSQSOvST iYLYCeUf4kAoxTodlI4v1FUVMPoHKzd3TJnXDrBU4EKC4CD8WiMIkGgkpakPYCMKpR3m WiZ5zecla6KDn7wVUnI16fhKZC7TrVBp9i9zve0/VU6P8IfGwd32zUx/nLCvJTMbKKE6 B6Og== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=qowNbRtI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d187si33579495iog.56.2021.07.22.09.42.53; Thu, 22 Jul 2021 09:43:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=qowNbRtI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233099AbhGVQBj (ORCPT + 99 others); Thu, 22 Jul 2021 12:01:39 -0400 Received: from mail.kernel.org ([198.145.29.99]:37010 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233928AbhGVP7l (ORCPT ); Thu, 22 Jul 2021 11:59:41 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 8059761241; Thu, 22 Jul 2021 16:40:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1626972015; bh=ExshhKc8xGRdXQOIpIjltfrrPGb0liSS2/ZOZLUn2Xo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qowNbRtIY2OOTAug2emS8bpVImudT/6nx8XJOqKhVOKLzkpRq8MXeooTlKlhiAmWf Wc/khp4gSmuQi2Mar1z9lTf0JjcYyQvNrfu50Ysv/OfmdnKor0uy7ZfadreOo4blvb ux2vhmyP6tfTPR9X5fgkVJyp755mCJrONsQCqs+M= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Sven Schnelle , Heiko Carstens , Vasily Gorbik , Sasha Levin Subject: [PATCH 5.10 079/125] s390: introduce proper type handling call_on_stack() macro Date: Thu, 22 Jul 2021 18:31:10 +0200 Message-Id: <20210722155627.317475287@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210722155624.672583740@linuxfoundation.org> References: <20210722155624.672583740@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Heiko Carstens [ Upstream commit 41d71fe59cce41237f24f3b7bdc1b414069a34ed ] The existing CALL_ON_STACK() macro allows for subtle bugs: - There is no type checking of the function that is being called. That is: missing or too many arguments do not cause any compile error or warning. The same is true if the return type of the called function changes. This can lead to quite random bugs. - Sign and zero extension of arguments is missing. Given that the s390 C ABI requires that the caller of a function performs proper sign and zero extension this can also lead to subtle bugs. - If arguments to the CALL_ON_STACK() macros contain functions calls register corruption can happen due to register asm constructs being used. Therefore introduce a new call_on_stack() macro which is supposed to fix all these problems. Reviewed-by: Sven Schnelle Signed-off-by: Heiko Carstens Signed-off-by: Vasily Gorbik Signed-off-by: Sasha Levin --- arch/s390/include/asm/stacktrace.h | 97 ++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) diff --git a/arch/s390/include/asm/stacktrace.h b/arch/s390/include/asm/stacktrace.h index ee582896b6a3..90488b0c26f6 100644 --- a/arch/s390/include/asm/stacktrace.h +++ b/arch/s390/include/asm/stacktrace.h @@ -128,6 +128,103 @@ struct stack_frame { r2; \ }) +#define CALL_LARGS_0(...) \ + long dummy = 0 +#define CALL_LARGS_1(t1, a1) \ + long arg1 = (long)(t1)(a1) +#define CALL_LARGS_2(t1, a1, t2, a2) \ + CALL_LARGS_1(t1, a1); \ + long arg2 = (long)(t2)(a2) +#define CALL_LARGS_3(t1, a1, t2, a2, t3, a3) \ + CALL_LARGS_2(t1, a1, t2, a2); \ + long arg3 = (long)(t3)(a3) +#define CALL_LARGS_4(t1, a1, t2, a2, t3, a3, t4, a4) \ + CALL_LARGS_3(t1, a1, t2, a2, t3, a3); \ + long arg4 = (long)(t4)(a4) +#define CALL_LARGS_5(t1, a1, t2, a2, t3, a3, t4, a4, t5, a5) \ + CALL_LARGS_4(t1, a1, t2, a2, t3, a3, t4, a4); \ + long arg5 = (long)(t5)(a5) + +#define CALL_REGS_0 \ + register long r2 asm("2") = dummy +#define CALL_REGS_1 \ + register long r2 asm("2") = arg1 +#define CALL_REGS_2 \ + CALL_REGS_1; \ + register long r3 asm("3") = arg2 +#define CALL_REGS_3 \ + CALL_REGS_2; \ + register long r4 asm("4") = arg3 +#define CALL_REGS_4 \ + CALL_REGS_3; \ + register long r5 asm("5") = arg4 +#define CALL_REGS_5 \ + CALL_REGS_4; \ + register long r6 asm("6") = arg5 + +#define CALL_TYPECHECK_0(...) +#define CALL_TYPECHECK_1(t, a, ...) \ + typecheck(t, a) +#define CALL_TYPECHECK_2(t, a, ...) \ + CALL_TYPECHECK_1(__VA_ARGS__); \ + typecheck(t, a) +#define CALL_TYPECHECK_3(t, a, ...) \ + CALL_TYPECHECK_2(__VA_ARGS__); \ + typecheck(t, a) +#define CALL_TYPECHECK_4(t, a, ...) \ + CALL_TYPECHECK_3(__VA_ARGS__); \ + typecheck(t, a) +#define CALL_TYPECHECK_5(t, a, ...) \ + CALL_TYPECHECK_4(__VA_ARGS__); \ + typecheck(t, a) + +#define CALL_PARM_0(...) void +#define CALL_PARM_1(t, a, ...) t +#define CALL_PARM_2(t, a, ...) t, CALL_PARM_1(__VA_ARGS__) +#define CALL_PARM_3(t, a, ...) t, CALL_PARM_2(__VA_ARGS__) +#define CALL_PARM_4(t, a, ...) t, CALL_PARM_3(__VA_ARGS__) +#define CALL_PARM_5(t, a, ...) t, CALL_PARM_4(__VA_ARGS__) +#define CALL_PARM_6(t, a, ...) t, CALL_PARM_5(__VA_ARGS__) + +/* + * Use call_on_stack() to call a function switching to a specified + * stack. Proper sign and zero extension of function arguments is + * done. Usage: + * + * rc = call_on_stack(nr, stack, rettype, fn, t1, a1, t2, a2, ...) + * + * - nr specifies the number of function arguments of fn. + * - stack specifies the stack to be used. + * - fn is the function to be called. + * - rettype is the return type of fn. + * - t1, a1, ... are pairs, where t1 must match the type of the first + * argument of fn, t2 the second, etc. a1 is the corresponding + * first function argument (not name), etc. + */ +#define call_on_stack(nr, stack, rettype, fn, ...) \ +({ \ + rettype (*__fn)(CALL_PARM_##nr(__VA_ARGS__)) = fn; \ + unsigned long frame = current_frame_address(); \ + unsigned long __stack = stack; \ + unsigned long prev; \ + CALL_LARGS_##nr(__VA_ARGS__); \ + CALL_REGS_##nr; \ + \ + CALL_TYPECHECK_##nr(__VA_ARGS__); \ + asm volatile( \ + " lgr %[_prev],15\n" \ + " lg 15,%[_stack]\n" \ + " stg %[_frame],%[_bc](15)\n" \ + " brasl 14,%[_fn]\n" \ + " lgr 15,%[_prev]\n" \ + : [_prev] "=&d" (prev), CALL_FMT_##nr \ + : [_stack] "R" (__stack), \ + [_bc] "i" (offsetof(struct stack_frame, back_chain)), \ + [_frame] "d" (frame), \ + [_fn] "X" (__fn) : CALL_CLOBBER_##nr); \ + (rettype)r2; \ +}) + #define CALL_ON_STACK_NORETURN(fn, stack) \ ({ \ asm volatile( \ -- 2.30.2