From patchwork Wed May 16 09:10:11 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Janosch Frank X-Patchwork-Id: 10403121 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 38884601D2 for ; Wed, 16 May 2018 09:10:39 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 26BDC28814 for ; Wed, 16 May 2018 09:10:39 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 247DA2889A; Wed, 16 May 2018 09:10:39 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00, MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id CAD0728846 for ; Wed, 16 May 2018 09:10:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752146AbeEPJK3 (ORCPT ); Wed, 16 May 2018 05:10:29 -0400 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:38888 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752007AbeEPJK0 (ORCPT ); Wed, 16 May 2018 05:10:26 -0400 Received: from pps.filterd (m0098417.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.22/8.16.0.22) with SMTP id w4G966sd108490 for ; Wed, 16 May 2018 05:10:25 -0400 Received: from e06smtp13.uk.ibm.com (e06smtp13.uk.ibm.com [195.75.94.109]) by mx0a-001b2d01.pphosted.com with ESMTP id 2j0fusdpfx-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Wed, 16 May 2018 05:10:25 -0400 Received: from localhost by e06smtp13.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Wed, 16 May 2018 10:10:23 +0100 Received: from b06cxnps3075.portsmouth.uk.ibm.com (9.149.109.195) by e06smtp13.uk.ibm.com (192.168.101.143) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Wed, 16 May 2018 10:10:21 +0100 Received: from d06av24.portsmouth.uk.ibm.com (mk.ibm.com [9.149.105.60]) by b06cxnps3075.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id w4G9AKVE16974250; Wed, 16 May 2018 09:10:20 GMT Received: from d06av24.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 68F6442047; Wed, 16 May 2018 10:01:12 +0100 (BST) Received: from d06av24.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 326D342041; Wed, 16 May 2018 10:01:12 +0100 (BST) Received: from s38lp20.boeblingen.de.ibm.com (unknown [9.152.224.155]) by d06av24.portsmouth.uk.ibm.com (Postfix) with ESMTP; Wed, 16 May 2018 10:01:12 +0100 (BST) From: Janosch Frank To: kvm@vger.kernel.org Cc: thuth@redhat.com, david@redhat.com Subject: [kvm-unit-tests PATCH] s390x: IEP tests Date: Wed, 16 May 2018 11:10:11 +0200 X-Mailer: git-send-email 2.7.4 X-TM-AS-GCONF: 00 x-cbid: 18051609-0012-0000-0000-000005D716D8 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 18051609-0013-0000-0000-000019543A05 Message-Id: <1526461811-97881-1-git-send-email-frankja@linux.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:, , definitions=2018-05-16_04:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=1 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 impostorscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1709140000 definitions=main-1805160093 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Janosch Frank Tests no-execute (Instruction Execution Protection) DAT protection. Signed-off-by: Janosch Frank --- This was originally part of my first patchset, but was pushed back after David introduced virtual memory allocation. It's more a RFC, as I'm not completely happy with the memory management manipulation I do. --- lib/s390x/asm/interrupt.h | 1 + lib/s390x/interrupt.c | 9 +++++ s390x/Makefile | 1 + s390x/iep.c | 89 +++++++++++++++++++++++++++++++++++++++++++++++ s390x/unittests.cfg | 4 +++ 5 files changed, 104 insertions(+) create mode 100644 s390x/iep.c diff --git a/lib/s390x/asm/interrupt.h b/lib/s390x/asm/interrupt.h index 3ccc8e3..39ae534 100644 --- a/lib/s390x/asm/interrupt.h +++ b/lib/s390x/asm/interrupt.h @@ -15,6 +15,7 @@ void handle_pgm_int(void); void expect_pgm_int(void); uint16_t clear_pgm_int(void); void check_pgm_int_code(uint16_t code); +void register_pgm_handler(void (*handler)(struct lowcore *)); /* Activate low-address protection */ static inline void low_prot_enable(void) diff --git a/lib/s390x/interrupt.c b/lib/s390x/interrupt.c index 56c7603..fe6cdba 100644 --- a/lib/s390x/interrupt.c +++ b/lib/s390x/interrupt.c @@ -15,6 +15,7 @@ static bool pgm_int_expected; static struct lowcore *lc; +static void (*custom_pgm_handler)(struct lowcore *); void expect_pgm_int(void) { @@ -41,8 +42,16 @@ void check_pgm_int_code(uint16_t code) code == lc->pgm_int_code, code, lc->pgm_int_code); } +void register_pgm_handler(void (*handler)(struct lowcore *)) +{ + custom_pgm_handler = handler; +} + static void fixup_pgm_int(void) { + if (custom_pgm_handler) + return custom_pgm_handler(lc); + switch (lc->pgm_int_code) { case PGM_INT_CODE_PRIVILEGED_OPERATION: /* Normal operation is in supervisor state, so this exception diff --git a/s390x/Makefile b/s390x/Makefile index abc3242..d4275a1 100644 --- a/s390x/Makefile +++ b/s390x/Makefile @@ -9,6 +9,7 @@ tests += $(TEST_DIR)/pfmf.elf tests += $(TEST_DIR)/cmm.elf tests += $(TEST_DIR)/vector.elf tests += $(TEST_DIR)/gs.elf +tests += $(TEST_DIR)/iep.elf all: directories test_cases diff --git a/s390x/iep.c b/s390x/iep.c new file mode 100644 index 0000000..87f3007 --- /dev/null +++ b/s390x/iep.c @@ -0,0 +1,89 @@ +/* + * Instruction Execution Prevention (IEP) DAT test. + * + * Copyright (c) 2018 IBM Corp + * + * Authors: + * Janosch Frank + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU Library General Public License version 2. + */ +#include +#include +#include +#include +#include +#include +#include + +static pteval_t *get_pte(pgd_t *pgtable, uintptr_t vaddr) +{ + pgd_t *pgd = pgd_offset(pgtable, vaddr); + p4d_t *p4d = p4d_offset(pgd, vaddr); + pud_t *pud = pud_offset(p4d, vaddr); + pmd_t *pmd = pmd_offset(pud, vaddr); + pte_t *pte = pte_offset(pmd, vaddr); + + return &pte_val(*pte); +} + +void iep_handler(struct lowcore *lc) +{ + if (lc->pgm_int_code == PGM_INT_CODE_PROTECTION) + lc->pgm_old_psw.addr = lc->sw_int_grs[14]; +} + +static void test_iep(void) +{ + uint16_t *code; + uint8_t *iepbuf = 0; + void (*fn)(void); + pteval_t *pte; + pgd_t *pgtable = (pgd_t *)(stctg(1) & ~(ASCE_DT_REGION1 | REGION_TABLE_LENGTH)); + + + /* Enable IEP */ + ctl_set_bit(0, 20); + + /* Get and protect a page with the IEP bit */ + iepbuf = alloc_page(); + pte = get_pte(pgtable, (uintptr_t)iepbuf); + ipte((uintptr_t)iepbuf, pte); + *pte |= PAGE_ENTRY_IEP; + *pte &= ~PAGE_ENTRY_I; + mb(); + + register_pgm_handler(&iep_handler); + + /* Code branches into r14 which contains the return address. */ + code = (uint16_t *)iepbuf; + *code = 0x07fe; + asm volatile("" : : "m" (code)); + fn = (void *)code; + mb(); + + expect_pgm_int(); + /* Jump into protected page */ + fn(); + check_pgm_int_code(PGM_INT_CODE_PROTECTION); + ctl_clear_bit(0, 20); +} + +int main(void) +{ + bool has_iep = test_facility(130); + + report_prefix_push("iep"); + report_xfail("DAT IEP available", !has_iep, has_iep); + if (!has_iep) + goto done; + + /* Setup DAT 1:1 mapping and memory management */ + setup_vm(); + test_iep(); + +done: + report_prefix_pop(); + return report_summary(); +} diff --git a/s390x/unittests.cfg b/s390x/unittests.cfg index ff7eea1..760402e 100644 --- a/s390x/unittests.cfg +++ b/s390x/unittests.cfg @@ -55,3 +55,7 @@ file = vector.elf [gs] file = gs.elf + +[iep] +file = iep.elf +