From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mx0b-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2355D3B18A for ; Wed, 1 Jul 2026 16:47:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=148.163.158.5 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782924457; cv=none; b=OJUqGRmMTzYuQfdt3vv8P7k6soxO9yLirso06XnQd1acJfo28RamOmPFDvXM8s4XXkn594QlJaVn2litmw/E/d/I5iF9vGBBB1lNTqu7sSJHB3wY8LTQsi89Bl0afwnTAByiHl0mUO/xoR0Ap7q3oToMBDpweGpq4zsnfurEIrw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782924457; c=relaxed/simple; bh=HjQZHuVnmD1cd89Wsw2umwlpFo1C8I77LUBaAji+yXg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=VKiqyL7sdjtvQ0SRTjwnfCnIBrUgM2JFB8gcgDmEOhyfmkag2OIf6sKzQxVmaD/zd5aa8YhwvmN0kH1sSjacIeIRnWTP7r7nVEolXoPgUSfASM9PXtLXpDH6GGeHYSWG79lTnnuNegRMZzgfW3/T4b53dRX7inmfD4CgUypba3A= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com; spf=pass smtp.mailfrom=linux.ibm.com; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b=T4W+H8W2; arc=none smtp.client-ip=148.163.158.5 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b="T4W+H8W2" Received: from pps.filterd (m0353725.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 661AIJvJ494359; Wed, 1 Jul 2026 16:47:01 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=cc :content-transfer-encoding:date:from:in-reply-to:message-id :mime-version:references:subject:to; s=pp1; bh=SMcfvUuy1FF4Yj2Z9 8N8Bq+9kGrY0iMwV3cE0YsF+Wg=; b=T4W+H8W2n+pN4o0i+fYI1acnT+io3eBc/ TlCOkPhii25O6C+TqQDhhozBnX5G3O4e7OznotJuGfuDb3GWylKJdkG8bPwWOTaL mOLap8axsicKEKwf79Yw/4rHaBqks8mya4sVhgWpZ2ksJUA5rcw1gw/6IksuSjrH uzj7fYH+vfrcUV3ziMr+qbIH8hdfzyHH0YRNvMemt0XdZAOsko8ZBzW4JNXcB9kT fJfEa67l4U2yWl97gnImfd4RCZeGuCs3FRVamTsNJ+P0afKs2Yf/QHBbxRgnHmB4 xT0AczlJTWxitjh5FJ1HAh3a/Kidd7frFknmE1agtmn/TBE6MiFsw== Received: from ppma23.wdc07v.mail.ibm.com (5d.69.3da9.ip4.static.sl-reverse.com [169.61.105.93]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 4f26rf56fc-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 01 Jul 2026 16:47:00 +0000 (GMT) Received: from pps.filterd (ppma23.wdc07v.mail.ibm.com [127.0.0.1]) by ppma23.wdc07v.mail.ibm.com (8.18.1.7/8.18.1.7) with ESMTP id 661GYbMs023115; Wed, 1 Jul 2026 16:47:00 GMT Received: from smtprelay02.fra02v.mail.ibm.com ([9.218.2.226]) by ppma23.wdc07v.mail.ibm.com (PPS) with ESMTPS id 4f2tbhg3wt-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 01 Jul 2026 16:47:00 +0000 (GMT) Received: from smtpav07.fra02v.mail.ibm.com (smtpav07.fra02v.mail.ibm.com [10.20.54.106]) by smtprelay02.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 661GkuDI50528658 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 1 Jul 2026 16:46:56 GMT Received: from smtpav07.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 170C920043; Wed, 1 Jul 2026 16:46:56 +0000 (GMT) Received: from smtpav07.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id CBD8D2004D; Wed, 1 Jul 2026 16:46:55 +0000 (GMT) Received: from funtu2.ibm.com (unknown [9.111.187.249]) by smtpav07.fra02v.mail.ibm.com (Postfix) with ESMTP; Wed, 1 Jul 2026 16:46:55 +0000 (GMT) From: Harald Freudenberger To: richard.henderson@linaro.org, iii@linux.ibm.com, david@kernel.org, thuth@redhat.com, berrange@redhat.com Cc: qemu-s390x@nongnu.org, qemu-devel@nongnu.org, linux390-list@tuxmaker.boeblingen.de.ibm.com, linux-s390@vger.kernel.org, dengler@linux.ibm.com, borntraeger@linux.ibm.com, fcallies@linux.ibm.com, cohuck@redhat.com Subject: [PATCH v9 20/21] target/s390x: Improve fetch and store mem from and to guest Date: Wed, 1 Jul 2026 18:46:43 +0200 Message-ID: <20260701164650.95760-21-freude@linux.ibm.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260701164650.95760-1-freude@linux.ibm.com> References: <20260701164650.95760-1-freude@linux.ibm.com> Precedence: bulk X-Mailing-List: linux-s390@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-TM-AS-GCONF: 00 X-Authority-Analysis: v=2.4 cv=a4kAM0SF c=1 sm=1 tr=0 ts=6a454485 cx=c_pps a=3Bg1Hr4SwmMryq2xdFQyZA==:117 a=3Bg1Hr4SwmMryq2xdFQyZA==:17 a=RAioF0-LDSMA:10 a=VkNPw1HP01LnGYTKEx00:22 a=RnoormkPH1_aCDwRdu11:22 a=V8glGbnc2Ofi9Qvn3v5h:22 a=VnNF1IyMAAAA:8 a=SH-LS3B6E9p2pXL9WtEA:9 a=ZDSMja1iq-x7GFnX:21 X-Proofpoint-ORIG-GUID: cvQIM9D9BBTF16by8ahsE00gy3vFhsLi X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwNzAxMDE3MyBTYWx0ZWRfX9q62Ju1Er/aY 77u+vX0PdXT9Fa96h6z/w3HoRJgABywyR6EA2Mr7TikJS3hifdAWLTLzymiPqpxEzjAHtZ6uXJn bECrjUVJTWwiRv6YbW5vzwHa6Jl5hCrA8Yif4RJABN/lzcBtkg616Dnhw+4Qoc/2EcJzALJ2Sc6 QMO1vp77R798nZBW+f1qpV350ZsOx2TV0a8nHejGjzQtYVFn4lwkLDLTtgI2Y7jwXJnydlbld5G l8ahTvkrH6vIXXH/ucpubi0fNUNM4B3YCkcdFSwp/sj4GrhQ7F5JPfumgNUTRoE/NqfQ9RFav4F DLTL0b+8YOYEBdEiadV3XHl0CmkEDQGA0Qa5JwYLtCC4jMae5qf75I2qGGHwFYGOj11jXzk6fCA TdHpssFoFexckqGVKFC30nplQBnm4FtWnmh0Yju4f7zawJZvKljWNFeeWbiVJ+d0/+brusCfmU1 Sv0T8uxi9dRBGSscnUg== X-Proofpoint-GUID: cvQIM9D9BBTF16by8ahsE00gy3vFhsLi X-Proofpoint-Spam-Info: AW1haW4tMjYwNzAxMDE3MyBTYWx0ZWRfX/5IiZeJ6ynnw IzvPikIbeYC4vmHEqFWunO6PXCAPpaRkZF4oQlgnOJkKEh5dD5NkQdr7PbzXv3Q6CiQxeO6XvH7 p7Obdu/JPatI7oRw/QjOaWJFpNWRlCI= X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1143,Hydra:6.1.125,FMLib:17.12.100.49 definitions=2026-07-01_03,2026-06-26_01,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 clxscore=1015 bulkscore=0 suspectscore=0 lowpriorityscore=0 impostorscore=0 spamscore=0 priorityscore=1501 adultscore=0 malwarescore=0 phishscore=0 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2606150000 definitions=main-2607010173 Introduce helper functions: * copy_from_guest_wrap() * copy_to_guest_wrap() for copy some memory from guest into a local buffer and the reverse direction. Rework the other functions to use these helpers. By doing so some local variables could be removed also and the code is better readable now. Suggested-by: Ilya Leoshkevich Signed-off-by: Harald Freudenberger --- target/s390x/tcg/cpacf_aes.c | 305 ++++++++++++++--------------------- 1 file changed, 124 insertions(+), 181 deletions(-) diff --git a/target/s390x/tcg/cpacf_aes.c b/target/s390x/tcg/cpacf_aes.c index 4298a0eac6..15124f3274 100644 --- a/target/s390x/tcg/cpacf_aes.c +++ b/target/s390x/tcg/cpacf_aes.c @@ -16,37 +16,64 @@ #include "crypto/aes.h" #include "target/s390x/tcg/cpacf.h" -static void aes_read_block(CPUS390XState *env, const int mmu_idx, - uint64_t addr, uint8_t *a, uintptr_t ra) +/* + * helper function to copy some memory from guest to a local buffer + */ +static inline void copy_from_guest_wrap(CPUS390XState *env, const int mmu_idx, + const uintptr_t ra, uint64_t guest_addr, + uint8_t *dest, size_t len) { const MemOpIdx oi = make_memop_idx(MO_8, mmu_idx); - for (int i = 0; i < AES_BLOCK_SIZE; i++, addr += 1) { - uint64_t _addr = wrap_address(env, addr); - a[i] = cpu_ldb_mmu(env, _addr, oi, ra); + for (size_t i = 0; i < len; i++, guest_addr++) { + uint64_t waddr = wrap_address(env, guest_addr); + dest[i] = cpu_ldb_mmu(env, waddr, oi, ra); } } -static void aes_write_block(CPUS390XState *env, const int mmu_idx, - uint64_t addr, uint8_t *a, uintptr_t ra) +/* + * helper function to copy from a local buffer to guest memory + */ +static inline void copy_to_guest_wrap(CPUS390XState *env, const int mmu_idx, + const uintptr_t ra, uint64_t guest_addr, + const uint8_t *src, size_t len) { const MemOpIdx oi = make_memop_idx(MO_8, mmu_idx); - for (int i = 0; i < AES_BLOCK_SIZE; i++, addr += 1) { - uint64_t _addr = wrap_address(env, addr); - cpu_stb_mmu(env, _addr, a[i], oi, ra); + for (size_t i = 0; i < len; i++, guest_addr++) { + uint64_t waddr = wrap_address(env, guest_addr); + cpu_stb_mmu(env, waddr, src[i], oi, ra); } } +/* + * read exactly one AES block from guest memory into a local buffer + */ +static inline void aes_read_block(CPUS390XState *env, const int mmu_idx, + const uintptr_t ra, uint64_t guest_addr, + uint8_t *buf) +{ + copy_from_guest_wrap(env, mmu_idx, ra, guest_addr, buf, AES_BLOCK_SIZE); +} + +/* + * write exactly one AES block from local buffer to guest memory + */ +static void aes_write_block(CPUS390XState *env, const int mmu_idx, + const uintptr_t ra, uint64_t guest_addr, + uint8_t *buf) +{ + copy_to_guest_wrap(env, mmu_idx, ra, guest_addr, buf, AES_BLOCK_SIZE); +} + int cpacf_aes_ecb(CPUS390XState *env, const int mmu_idx, uintptr_t ra, uint64_t param_addr, uint64_t *dst_ptr_reg, uint64_t *src_ptr_reg, uint64_t *src_len_reg, uint32_t type, uint8_t fc, uint8_t mod) { enum { MAX_BLOCKS_PER_RUN = 8192 / AES_BLOCK_SIZE }; - const MemOpIdx oi = make_memop_idx(MO_8, mmu_idx); uint8_t in[AES_BLOCK_SIZE], out[AES_BLOCK_SIZE]; - uint64_t addr, len = *src_len_reg, done = 0; + uint64_t len = *src_len_reg, done = 0; int i, keysize, addr_reg_size = 64; uint8_t key[32]; AES_KEY exkey; @@ -77,10 +104,7 @@ int cpacf_aes_ecb(CPUS390XState *env, const int mmu_idx, uintptr_t ra, } /* fetch key from param block */ - for (i = 0; i < keysize; i++) { - addr = wrap_address(env, param_addr + i); - key[i] = cpu_ldb_mmu(env, addr, oi, ra); - } + copy_from_guest_wrap(env, mmu_idx, ra, param_addr, key, keysize); /* expand key */ if (mod) { @@ -91,13 +115,13 @@ int cpacf_aes_ecb(CPUS390XState *env, const int mmu_idx, uintptr_t ra, /* process up to MAX_BLOCKS_PER_RUN aes blocks */ for (i = 0; i < MAX_BLOCKS_PER_RUN && len >= AES_BLOCK_SIZE; i++) { - aes_read_block(env, mmu_idx, *src_ptr_reg + done, in, ra); + aes_read_block(env, mmu_idx, ra, *src_ptr_reg + done, in); if (mod) { AES_decrypt(in, out, &exkey); } else { AES_encrypt(in, out, &exkey); } - aes_write_block(env, mmu_idx, *dst_ptr_reg + done, out, ra); + aes_write_block(env, mmu_idx, ra, *dst_ptr_reg + done, out); len -= AES_BLOCK_SIZE; done += AES_BLOCK_SIZE; } @@ -118,8 +142,7 @@ int cpacf_aes_cbc(CPUS390XState *env, const int mmu_idx, uintptr_t ra, { enum { MAX_BLOCKS_PER_RUN = 8192 / AES_BLOCK_SIZE }; uint8_t in[AES_BLOCK_SIZE], out[AES_BLOCK_SIZE]; - const MemOpIdx oi = make_memop_idx(MO_8, mmu_idx); - uint64_t addr, len = *src_len_reg, done = 0; + uint64_t len = *src_len_reg, done = 0; int i, keysize, addr_reg_size = 64; uint8_t key[32], iv[AES_BLOCK_SIZE]; AES_KEY exkey; @@ -151,16 +174,11 @@ int cpacf_aes_cbc(CPUS390XState *env, const int mmu_idx, uintptr_t ra, } /* fetch iv from param block */ - for (i = 0; i < AES_BLOCK_SIZE; i++) { - addr = wrap_address(env, param_addr + i); - iv[i] = cpu_ldb_mmu(env, addr, oi, ra); - } + copy_from_guest_wrap(env, mmu_idx, ra, param_addr, iv, AES_BLOCK_SIZE); /* fetch key from param block */ - for (i = 0; i < keysize; i++) { - addr = wrap_address(env, param_addr + AES_BLOCK_SIZE + i); - key[i] = cpu_ldb_mmu(env, addr, oi, ra); - } + copy_from_guest_wrap(env, mmu_idx, ra, + param_addr + AES_BLOCK_SIZE, key, keysize); /* expand key */ if (mod) { @@ -171,7 +189,7 @@ int cpacf_aes_cbc(CPUS390XState *env, const int mmu_idx, uintptr_t ra, /* process up to MAX_BLOCKS_PER_RUN aes blocks */ for (i = 0; i < MAX_BLOCKS_PER_RUN && len >= AES_BLOCK_SIZE; i++) { - aes_read_block(env, mmu_idx, *src_ptr_reg + done, in, ra); + aes_read_block(env, mmu_idx, ra, *src_ptr_reg + done, in); if (mod) { /* decrypt in => out */ AES_cbc_decrypt(in, out, iv, &exkey); @@ -179,16 +197,13 @@ int cpacf_aes_cbc(CPUS390XState *env, const int mmu_idx, uintptr_t ra, /* encrypt in => out */ AES_cbc_encrypt(in, out, iv, &exkey); } - aes_write_block(env, mmu_idx, *dst_ptr_reg + done, out, ra); + aes_write_block(env, mmu_idx, ra, *dst_ptr_reg + done, out); len -= AES_BLOCK_SIZE; done += AES_BLOCK_SIZE; } /* update iv in param block */ - for (i = 0; i < AES_BLOCK_SIZE; i++) { - addr = wrap_address(env, param_addr + i); - cpu_stb_mmu(env, addr, iv[i], oi, ra); - } + copy_to_guest_wrap(env, mmu_idx, ra, param_addr, iv, AES_BLOCK_SIZE); *src_ptr_reg = deposit64(*src_ptr_reg, 0, addr_reg_size, *src_ptr_reg + done); @@ -206,9 +221,8 @@ int cpacf_aes_ctr(CPUS390XState *env, const int mmu_idx, uintptr_t ra, uint8_t fc, uint8_t mod) { enum { MAX_BLOCKS_PER_RUN = 8192 / AES_BLOCK_SIZE }; - const MemOpIdx oi = make_memop_idx(MO_8, mmu_idx); uint8_t in[AES_BLOCK_SIZE], out[AES_BLOCK_SIZE]; - uint64_t addr, len = *src_len_reg, done = 0; + uint64_t len = *src_len_reg, done = 0; uint8_t ctr[AES_BLOCK_SIZE], key[32]; int i, keysize, addr_reg_size = 64; AES_KEY exkey; @@ -240,10 +254,7 @@ int cpacf_aes_ctr(CPUS390XState *env, const int mmu_idx, uintptr_t ra, } /* fetch key from param block */ - for (i = 0; i < keysize; i++) { - addr = wrap_address(env, param_addr + i); - key[i] = cpu_ldb_mmu(env, addr, oi, ra); - } + copy_from_guest_wrap(env, mmu_idx, ra, param_addr, key, keysize); /* expand key */ AES_set_encrypt_key(key, keysize * 8, &exkey); @@ -251,13 +262,13 @@ int cpacf_aes_ctr(CPUS390XState *env, const int mmu_idx, uintptr_t ra, /* process up to MAX_BLOCKS_PER_RUN aes blocks */ for (i = 0; i < MAX_BLOCKS_PER_RUN && len >= AES_BLOCK_SIZE; i++) { /* read in nonce/ctr => ctr */ - aes_read_block(env, mmu_idx, *ctr_ptr_reg + done, ctr, ra); + aes_read_block(env, mmu_idx, ra, *ctr_ptr_reg + done, ctr); /* read in one block of input data => in */ - aes_read_block(env, mmu_idx, *src_ptr_reg + done, in, ra); + aes_read_block(env, mmu_idx, ra, *src_ptr_reg + done, in); /* encrypt ctr and xor with in => out */ AES_ctr_encrypt(in, out, ctr, &exkey); /* write out the processed block */ - aes_write_block(env, mmu_idx, *dst_ptr_reg + done, out, ra); + aes_write_block(env, mmu_idx, ra, *dst_ptr_reg + done, out); len -= AES_BLOCK_SIZE; done += AES_BLOCK_SIZE; } @@ -277,9 +288,7 @@ int cpacf_aes_pcc(CPUS390XState *env, const int mmu_idx, uintptr_t ra, uint64_t param_addr, uint8_t fc) { uint8_t key[32], tweak[AES_BLOCK_SIZE], buf[AES_BLOCK_SIZE]; - const MemOpIdx oi = make_memop_idx(MO_8, mmu_idx); int keysize, i; - uint64_t addr; AES_KEY exkey; switch (fc) { @@ -294,10 +303,9 @@ int cpacf_aes_pcc(CPUS390XState *env, const int mmu_idx, uintptr_t ra, } /* fetch block sequence nr from param block into buf */ - for (i = 0; i < AES_BLOCK_SIZE; i++) { - addr = wrap_address(env, param_addr + keysize + AES_BLOCK_SIZE + i); - buf[i] = cpu_ldb_mmu(env, addr, oi, ra); - } + copy_from_guest_wrap(env, mmu_idx, ra, + param_addr + keysize + AES_BLOCK_SIZE, + buf, AES_BLOCK_SIZE); /* is the block sequence nr 0 ? */ for (i = 0; i < AES_BLOCK_SIZE && !buf[i]; i++) { @@ -310,16 +318,11 @@ int cpacf_aes_pcc(CPUS390XState *env, const int mmu_idx, uintptr_t ra, } /* fetch key from param block */ - for (i = 0; i < keysize; i++) { - addr = wrap_address(env, param_addr + i); - key[i] = cpu_ldb_mmu(env, addr, oi, ra); - } + copy_from_guest_wrap(env, mmu_idx, ra, param_addr, key, keysize); /* fetch tweak from param block into tweak */ - for (i = 0; i < AES_BLOCK_SIZE; i++) { - addr = wrap_address(env, param_addr + keysize + i); - tweak[i] = cpu_ldb_mmu(env, addr, oi, ra); - } + copy_from_guest_wrap(env, mmu_idx, ra, + param_addr + keysize, tweak, AES_BLOCK_SIZE); /* expand key */ AES_set_encrypt_key(key, keysize * 8, &exkey); @@ -328,10 +331,9 @@ int cpacf_aes_pcc(CPUS390XState *env, const int mmu_idx, uintptr_t ra, AES_encrypt(tweak, buf, &exkey); /* store encrypted tweak into xts parameter field of the param block */ - for (i = 0; i < AES_BLOCK_SIZE; i++) { - addr = wrap_address(env, param_addr + keysize + 3 * AES_BLOCK_SIZE + i); - cpu_stb_mmu(env, addr, buf[i], oi, ra); - } + copy_to_guest_wrap(env, mmu_idx, ra, + param_addr + keysize + 3 * AES_BLOCK_SIZE, + buf, AES_BLOCK_SIZE); return 0; } @@ -343,8 +345,7 @@ int cpacf_aes_xts(CPUS390XState *env, const int mmu_idx, uintptr_t ra, { enum { MAX_BLOCKS_PER_RUN = 8192 / AES_BLOCK_SIZE }; uint8_t in[AES_BLOCK_SIZE], out[AES_BLOCK_SIZE]; - const MemOpIdx oi = make_memop_idx(MO_8, mmu_idx); - uint64_t addr, len = *src_len_reg, done = 0; + uint64_t len = *src_len_reg, done = 0; uint8_t key[32], tweak[AES_BLOCK_SIZE]; int i, keysize, addr_reg_size = 64; AES_KEY exkey; @@ -373,10 +374,7 @@ int cpacf_aes_xts(CPUS390XState *env, const int mmu_idx, uintptr_t ra, } /* fetch key from param block */ - for (i = 0; i < keysize; i++) { - addr = wrap_address(env, param_addr + i); - key[i] = cpu_ldb_mmu(env, addr, oi, ra); - } + copy_from_guest_wrap(env, mmu_idx, ra, param_addr, key, keysize); /* expand key */ if (mod) { @@ -386,15 +384,13 @@ int cpacf_aes_xts(CPUS390XState *env, const int mmu_idx, uintptr_t ra, } /* fetch tweak from param block */ - for (i = 0; i < AES_BLOCK_SIZE; i++) { - addr = wrap_address(env, param_addr + keysize + i); - tweak[i] = cpu_ldb_mmu(env, addr, oi, ra); - } + copy_from_guest_wrap(env, mmu_idx, ra, + param_addr + keysize, tweak, AES_BLOCK_SIZE); /* process up to MAX_BLOCKS_PER_RUN aes blocks */ for (i = 0; i < MAX_BLOCKS_PER_RUN && len >= AES_BLOCK_SIZE; i++) { /* fetch one AES block into in */ - aes_read_block(env, mmu_idx, *src_ptr_reg + done, in, ra); + aes_read_block(env, mmu_idx, ra, *src_ptr_reg + done, in); if (mod) { /* decrypt in => out */ AES_xts_decrypt(in, out, tweak, &exkey); @@ -405,16 +401,14 @@ int cpacf_aes_xts(CPUS390XState *env, const int mmu_idx, uintptr_t ra, /* prep tweak for next round */ AES_xts_prep_next_tweak(tweak); /* write out this processed block from out */ - aes_write_block(env, mmu_idx, *dst_ptr_reg + done, out, ra); + aes_write_block(env, mmu_idx, ra, *dst_ptr_reg + done, out); len -= AES_BLOCK_SIZE; done += AES_BLOCK_SIZE; } /* update tweak in param block */ - for (i = 0; i < AES_BLOCK_SIZE; i++) { - addr = wrap_address(env, param_addr + keysize + i); - cpu_stb_mmu(env, addr, tweak[i], oi, ra); - } + copy_to_guest_wrap(env, mmu_idx, ra, + param_addr + keysize, tweak, AES_BLOCK_SIZE); *src_ptr_reg = deposit64(*src_ptr_reg, 0, addr_reg_size, *src_ptr_reg + done); @@ -467,10 +461,8 @@ static void decrypt_protkey(uint8_t *key, int keysize) int cpacf_aes_pckmo(CPUS390XState *env, const int mmu_idx, uintptr_t ra, uint64_t param_addr, uint8_t fc) { - const MemOpIdx oi = make_memop_idx(MO_8, mmu_idx); uint8_t key[32]; - int keysize, i; - uint64_t addr; + int keysize; switch (fc) { case CPACF_PCKMO_ENC_AES_128_KEY: @@ -487,24 +479,17 @@ int cpacf_aes_pckmo(CPUS390XState *env, const int mmu_idx, uintptr_t ra, } /* fetch key from param block */ - for (i = 0; i < keysize; i++) { - addr = wrap_address(env, param_addr + i); - key[i] = cpu_ldb_mmu(env, addr, oi, ra); - } + copy_from_guest_wrap(env, mmu_idx, ra, param_addr, key, keysize); /* 'derive' the protected key from the clear key */ encrypt_clrkey(key, keysize); /* store the protected key into param block */ - for (i = 0; i < keysize; i++) { - addr = wrap_address(env, param_addr + i); - cpu_stb_mmu(env, addr, key[i], oi, ra); - } + copy_to_guest_wrap(env, mmu_idx, ra, param_addr, key, keysize); /* followed by the fake wkvp */ - for (i = 0; i < sizeof(protkey_wkvp); i++) { - addr = wrap_address(env, param_addr + keysize + i); - cpu_stb_mmu(env, addr, protkey_wkvp[i], oi, ra); - } + copy_to_guest_wrap(env, mmu_idx, ra, + param_addr + keysize, + protkey_wkvp, sizeof(protkey_wkvp)); return 0; } @@ -515,9 +500,8 @@ int cpacf_paes_ecb(CPUS390XState *env, const int mmu_idx, uintptr_t ra, uint32_t type, uint8_t fc, uint8_t mod) { enum { MAX_BLOCKS_PER_RUN = 8192 / AES_BLOCK_SIZE }; - const MemOpIdx oi = make_memop_idx(MO_8, mmu_idx); uint8_t in[AES_BLOCK_SIZE], out[AES_BLOCK_SIZE]; - uint64_t addr, len = *src_len_reg, done = 0; + uint64_t len = *src_len_reg, done = 0; int i, keysize, addr_reg_size = 64; uint8_t key[32], wkvp[32]; AES_KEY exkey; @@ -549,20 +533,15 @@ int cpacf_paes_ecb(CPUS390XState *env, const int mmu_idx, uintptr_t ra, } /* fetch and check wkvp from param block */ - for (i = 0; i < sizeof(wkvp); i++) { - addr = wrap_address(env, param_addr + keysize + i); - wkvp[i] = cpu_ldb_mmu(env, addr, oi, ra); - } + copy_from_guest_wrap(env, mmu_idx, ra, + param_addr + keysize, wkvp, sizeof(wkvp)); if (memcmp(wkvp, protkey_wkvp, sizeof(wkvp))) { /* wkvp mismatch -> return with cc 1 */ return 1; } /* fetch protected key from param block */ - for (i = 0; i < keysize; i++) { - addr = wrap_address(env, param_addr + i); - key[i] = cpu_ldb_mmu(env, addr, oi, ra); - } + copy_from_guest_wrap(env, mmu_idx, ra, param_addr, key, keysize); /* decrypt the protected key */ decrypt_protkey(key, keysize); @@ -575,13 +554,13 @@ int cpacf_paes_ecb(CPUS390XState *env, const int mmu_idx, uintptr_t ra, /* process up to MAX_BLOCKS_PER_RUN aes blocks */ for (i = 0; i < MAX_BLOCKS_PER_RUN && len >= AES_BLOCK_SIZE; i++) { - aes_read_block(env, mmu_idx, *src_ptr_reg + done, in, ra); + aes_read_block(env, mmu_idx, ra, *src_ptr_reg + done, in); if (mod) { AES_decrypt(in, out, &exkey); } else { AES_encrypt(in, out, &exkey); } - aes_write_block(env, mmu_idx, *dst_ptr_reg + done, out, ra); + aes_write_block(env, mmu_idx, ra, *dst_ptr_reg + done, out); len -= AES_BLOCK_SIZE; done += AES_BLOCK_SIZE; } @@ -602,9 +581,8 @@ int cpacf_paes_cbc(CPUS390XState *env, const int mmu_idx, uintptr_t ra, { enum { MAX_BLOCKS_PER_RUN = 8192 / AES_BLOCK_SIZE }; uint8_t in[AES_BLOCK_SIZE], out[AES_BLOCK_SIZE]; - const MemOpIdx oi = make_memop_idx(MO_8, mmu_idx); uint8_t key[32], wkvp[32], iv[AES_BLOCK_SIZE]; - uint64_t addr, len = *src_len_reg, done = 0; + uint64_t len = *src_len_reg, done = 0; int i, keysize, addr_reg_size = 64; AES_KEY exkey; @@ -635,26 +613,20 @@ int cpacf_paes_cbc(CPUS390XState *env, const int mmu_idx, uintptr_t ra, } /* fetch and check wkvp from param block */ - for (i = 0; i < sizeof(wkvp); i++) { - addr = wrap_address(env, param_addr + AES_BLOCK_SIZE + keysize + i); - wkvp[i] = cpu_ldb_mmu(env, addr, oi, ra); - } + copy_from_guest_wrap(env, mmu_idx, ra, + param_addr + AES_BLOCK_SIZE + keysize, + wkvp, sizeof(wkvp)); if (memcmp(wkvp, protkey_wkvp, sizeof(wkvp))) { /* wkvp mismatch -> return with cc 1 */ return 1; } /* fetch iv from param block */ - for (i = 0; i < AES_BLOCK_SIZE; i++) { - addr = wrap_address(env, param_addr + i); - iv[i] = cpu_ldb_mmu(env, addr, oi, ra); - } + copy_from_guest_wrap(env, mmu_idx, ra, param_addr, iv, AES_BLOCK_SIZE); /* fetch protected key from param block */ - for (i = 0; i < keysize; i++) { - addr = wrap_address(env, param_addr + AES_BLOCK_SIZE + i); - key[i] = cpu_ldb_mmu(env, addr, oi, ra); - } + copy_from_guest_wrap(env, mmu_idx, ra, + param_addr + AES_BLOCK_SIZE, key, keysize); /* decrypt the protected key */ decrypt_protkey(key, keysize); @@ -667,7 +639,7 @@ int cpacf_paes_cbc(CPUS390XState *env, const int mmu_idx, uintptr_t ra, /* process up to MAX_BLOCKS_PER_RUN aes blocks */ for (i = 0; i < MAX_BLOCKS_PER_RUN && len >= AES_BLOCK_SIZE; i++) { - aes_read_block(env, mmu_idx, *src_ptr_reg + done, in, ra); + aes_read_block(env, mmu_idx, ra, *src_ptr_reg + done, in); if (mod) { /* decrypt in => out */ AES_cbc_decrypt(in, out, iv, &exkey); @@ -675,16 +647,13 @@ int cpacf_paes_cbc(CPUS390XState *env, const int mmu_idx, uintptr_t ra, /* encrypt in => out */ AES_cbc_encrypt(in, out, iv, &exkey); } - aes_write_block(env, mmu_idx, *dst_ptr_reg + done, out, ra); + aes_write_block(env, mmu_idx, ra, *dst_ptr_reg + done, out); len -= AES_BLOCK_SIZE; done += AES_BLOCK_SIZE; } /* update iv in param block */ - for (i = 0; i < AES_BLOCK_SIZE; i++) { - addr = wrap_address(env, param_addr + i); - cpu_stb_mmu(env, addr, iv[i], oi, ra); - } + copy_to_guest_wrap(env, mmu_idx, ra, param_addr, iv, AES_BLOCK_SIZE); *src_ptr_reg = deposit64(*src_ptr_reg, 0, addr_reg_size, *src_ptr_reg + done); @@ -702,10 +671,9 @@ int cpacf_paes_ctr(CPUS390XState *env, const int mmu_idx, uintptr_t ra, uint8_t fc, uint8_t mod) { enum { MAX_BLOCKS_PER_RUN = 8192 / AES_BLOCK_SIZE }; - const MemOpIdx oi = make_memop_idx(MO_8, mmu_idx); uint8_t in[AES_BLOCK_SIZE], out[AES_BLOCK_SIZE]; uint8_t ctr[AES_BLOCK_SIZE], key[32], wkvp[32]; - uint64_t addr, len = *src_len_reg, done = 0; + uint64_t len = *src_len_reg, done = 0; int i, keysize, addr_reg_size = 64; AES_KEY exkey; @@ -736,20 +704,15 @@ int cpacf_paes_ctr(CPUS390XState *env, const int mmu_idx, uintptr_t ra, } /* fetch and check wkvp from param block */ - for (i = 0; i < sizeof(wkvp); i++) { - addr = wrap_address(env, param_addr + keysize + i); - wkvp[i] = cpu_ldb_mmu(env, addr, oi, ra); - } + copy_from_guest_wrap(env, mmu_idx, ra, + param_addr + keysize, wkvp, sizeof(wkvp)); if (memcmp(wkvp, protkey_wkvp, sizeof(wkvp))) { /* wkvp mismatch -> return with cc 1 */ return 1; } /* fetch protected key from param block */ - for (i = 0; i < keysize; i++) { - addr = wrap_address(env, param_addr + i); - key[i] = cpu_ldb_mmu(env, addr, oi, ra); - } + copy_from_guest_wrap(env, mmu_idx, ra, param_addr, key, keysize); /* decrypt the protected key */ decrypt_protkey(key, keysize); @@ -759,13 +722,13 @@ int cpacf_paes_ctr(CPUS390XState *env, const int mmu_idx, uintptr_t ra, /* process up to MAX_BLOCKS_PER_RUN aes blocks */ for (i = 0; i < MAX_BLOCKS_PER_RUN && len >= AES_BLOCK_SIZE; i++) { /* read in nonce/ctr => ctr */ - aes_read_block(env, mmu_idx, *ctr_ptr_reg + done, ctr, ra); + aes_read_block(env, mmu_idx, ra, *ctr_ptr_reg + done, ctr); /* read in one block of input data => in */ - aes_read_block(env, mmu_idx, *src_ptr_reg + done, in, ra); + aes_read_block(env, mmu_idx, ra, *src_ptr_reg + done, in); /* encrypt ctr and xor with in => out */ AES_ctr_encrypt(in, out, ctr, &exkey); /* write out the processed block */ - aes_write_block(env, mmu_idx, *dst_ptr_reg + done, out, ra); + aes_write_block(env, mmu_idx, ra, *dst_ptr_reg + done, out); len -= AES_BLOCK_SIZE; done += AES_BLOCK_SIZE; } @@ -785,9 +748,7 @@ int cpacf_paes_pcc(CPUS390XState *env, const int mmu_idx, uintptr_t ra, uint64_t param_addr, uint8_t fc) { uint8_t key[32], wkvp[32], tweak[AES_BLOCK_SIZE], buf[AES_BLOCK_SIZE]; - const MemOpIdx oi = make_memop_idx(MO_8, mmu_idx); int keysize, i; - uint64_t addr; AES_KEY exkey; switch (fc) { @@ -802,21 +763,17 @@ int cpacf_paes_pcc(CPUS390XState *env, const int mmu_idx, uintptr_t ra, } /* fetch and check wkvp from param block */ - for (i = 0; i < sizeof(wkvp); i++) { - addr = wrap_address(env, param_addr + keysize + i); - wkvp[i] = cpu_ldb_mmu(env, addr, oi, ra); - } + copy_from_guest_wrap(env, mmu_idx, ra, + param_addr + keysize, wkvp, sizeof(wkvp)); if (memcmp(wkvp, protkey_wkvp, sizeof(wkvp))) { /* wkvp mismatch -> return with cc 1 */ return 1; } /* fetch block sequence nr from param block into buf */ - for (i = 0; i < AES_BLOCK_SIZE; i++) { - addr = wrap_address(env, param_addr + keysize + - sizeof(wkvp) + AES_BLOCK_SIZE + i); - buf[i] = cpu_ldb_mmu(env, addr, oi, ra); - } + copy_from_guest_wrap(env, mmu_idx, ra, + param_addr + keysize + sizeof(wkvp) + AES_BLOCK_SIZE, + buf, AES_BLOCK_SIZE); /* is the block sequence nr 0 ? */ for (i = 0; i < AES_BLOCK_SIZE && !buf[i]; i++) { @@ -829,18 +786,14 @@ int cpacf_paes_pcc(CPUS390XState *env, const int mmu_idx, uintptr_t ra, } /* fetch protected key from param block */ - for (i = 0; i < keysize; i++) { - addr = wrap_address(env, param_addr + i); - key[i] = cpu_ldb_mmu(env, addr, oi, ra); - } + copy_from_guest_wrap(env, mmu_idx, ra, param_addr, key, keysize); /* decrypt the protected key */ decrypt_protkey(key, keysize); /* fetch tweak from param block into tweak */ - for (i = 0; i < AES_BLOCK_SIZE; i++) { - addr = wrap_address(env, param_addr + keysize + sizeof(wkvp) + i); - tweak[i] = cpu_ldb_mmu(env, addr, oi, ra); - } + copy_from_guest_wrap(env, mmu_idx, ra, + param_addr + keysize + sizeof(wkvp), + tweak, AES_BLOCK_SIZE); /* expand key */ AES_set_encrypt_key(key, keysize * 8, &exkey); @@ -849,11 +802,9 @@ int cpacf_paes_pcc(CPUS390XState *env, const int mmu_idx, uintptr_t ra, AES_encrypt(tweak, buf, &exkey); /* store encrypted tweak into xts parameter field of the param block */ - for (i = 0; i < AES_BLOCK_SIZE; i++) { - addr = wrap_address(env, param_addr + keysize + - sizeof(wkvp) + 3 * AES_BLOCK_SIZE + i); - cpu_stb_mmu(env, addr, buf[i], oi, ra); - } + copy_to_guest_wrap(env, mmu_idx, ra, + param_addr + keysize + sizeof(wkvp) + 3 * AES_BLOCK_SIZE, + buf, AES_BLOCK_SIZE); return 0; } @@ -865,9 +816,8 @@ int cpacf_paes_xts(CPUS390XState *env, const int mmu_idx, uintptr_t ra, { enum { MAX_BLOCKS_PER_RUN = 8192 / AES_BLOCK_SIZE }; uint8_t in[AES_BLOCK_SIZE], out[AES_BLOCK_SIZE]; - const MemOpIdx oi = make_memop_idx(MO_8, mmu_idx); uint8_t key[32], wkvp[32], tweak[AES_BLOCK_SIZE]; - uint64_t addr, len = *src_len_reg, done = 0; + uint64_t len = *src_len_reg, done = 0; int i, keysize, addr_reg_size = 64; AES_KEY exkey; @@ -895,20 +845,15 @@ int cpacf_paes_xts(CPUS390XState *env, const int mmu_idx, uintptr_t ra, } /* fetch and check wkvp from param block */ - for (i = 0; i < sizeof(wkvp); i++) { - addr = wrap_address(env, param_addr + keysize + i); - wkvp[i] = cpu_ldb_mmu(env, addr, oi, ra); - } + copy_from_guest_wrap(env, mmu_idx, ra, + param_addr + keysize, wkvp, sizeof(wkvp)); if (memcmp(wkvp, protkey_wkvp, sizeof(wkvp))) { /* wkvp mismatch -> return with cc 1 */ return 1; } /* fetch protected key from param block */ - for (i = 0; i < keysize; i++) { - addr = wrap_address(env, param_addr + i); - key[i] = cpu_ldb_mmu(env, addr, oi, ra); - } + copy_from_guest_wrap(env, mmu_idx, ra, param_addr, key, keysize); /* decrypt the protected key */ decrypt_protkey(key, keysize); @@ -920,15 +865,14 @@ int cpacf_paes_xts(CPUS390XState *env, const int mmu_idx, uintptr_t ra, } /* fetch tweak from param block */ - for (i = 0; i < AES_BLOCK_SIZE; i++) { - addr = wrap_address(env, param_addr + keysize + sizeof(wkvp) + i); - tweak[i] = cpu_ldb_mmu(env, addr, oi, ra); - } + copy_from_guest_wrap(env, mmu_idx, ra, + param_addr + keysize + sizeof(wkvp), + tweak, AES_BLOCK_SIZE); /* process up to MAX_BLOCKS_PER_RUN aes blocks */ for (i = 0; i < MAX_BLOCKS_PER_RUN && len >= AES_BLOCK_SIZE; i++) { /* fetch one AES block into in */ - aes_read_block(env, mmu_idx, *src_ptr_reg + done, in, ra); + aes_read_block(env, mmu_idx, ra, *src_ptr_reg + done, in); if (mod) { /* decrypt in => out */ AES_xts_decrypt(in, out, tweak, &exkey); @@ -939,16 +883,15 @@ int cpacf_paes_xts(CPUS390XState *env, const int mmu_idx, uintptr_t ra, /* prep tweak for next round */ AES_xts_prep_next_tweak(tweak); /* write out this processed block from out */ - aes_write_block(env, mmu_idx, *dst_ptr_reg + done, out, ra); + aes_write_block(env, mmu_idx, ra, *dst_ptr_reg + done, out); len -= AES_BLOCK_SIZE; done += AES_BLOCK_SIZE; } /* update tweak in param block */ - for (i = 0; i < AES_BLOCK_SIZE; i++) { - addr = wrap_address(env, param_addr + keysize + sizeof(wkvp) + i); - cpu_stb_mmu(env, addr, tweak[i], oi, ra); - } + copy_to_guest_wrap(env, mmu_idx, ra, + param_addr + keysize + sizeof(wkvp), + tweak, AES_BLOCK_SIZE); *src_ptr_reg = deposit64(*src_ptr_reg, 0, addr_reg_size, *src_ptr_reg + done); -- 2.43.0