qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: David Hildenbrand <david@redhat.com>
To: Janosch Frank <frankja@linux.ibm.com>, qemu-devel@nongnu.org
Cc: borntraeger@de.ibm.com, qemu-s390x@nongnu.org, cohuck@redhat.com
Subject: Re: [PATCH v6 03/18] s390x: protvirt: Support unpack facility
Date: Thu, 5 Mar 2020 14:51:15 +0100	[thread overview]
Message-ID: <2a93ff2e-4955-67c4-e7af-766a49fc8b32@redhat.com> (raw)
In-Reply-To: <20200304114231.23493-4-frankja@linux.ibm.com>

On 04.03.20 12:42, Janosch Frank wrote:
> When a guest has saved a ipib of type 5 and calls diagnose308 with

s/a/an/

> subcode 10, we have to setup the protected processing environment via
> Ultravisor calls. The calls are done by KVM and are exposed via an
> API.
> 
> The following steps are necessary:
> 1. Enable protected mode for the VM (register it and its cpus with the Ultravisor)
> 2. Forward the secure header to the Ultravisor (has all information on
> how to decrypt the image and VM information)
> 3. Protect image pages from the host and decrypt them
> 4. Verify the image integrity
> 
> Only after step 4 a protected VM is allowed to run.
> 
> Signed-off-by: Janosch Frank <frankja@linux.ibm.com>
> Signed-off-by: Christian Borntraeger <borntraeger@de.ibm.com> [Changes
> to machine]
> ---
>  hw/s390x/Makefile.objs              |   1 +
>  hw/s390x/ipl.c                      |  33 +++++++++
>  hw/s390x/ipl.h                      |   2 +
>  hw/s390x/pv.c                       | 106 ++++++++++++++++++++++++++++
>  hw/s390x/pv.h                       |  34 +++++++++
>  hw/s390x/s390-virtio-ccw.c          |  91 ++++++++++++++++++++++++
>  include/hw/s390x/s390-virtio-ccw.h  |   1 +
>  target/s390x/cpu.c                  |   4 ++
>  target/s390x/cpu.h                  |   1 +
>  target/s390x/cpu_features_def.inc.h |   1 +
>  10 files changed, 274 insertions(+)
>  create mode 100644 hw/s390x/pv.c
>  create mode 100644 hw/s390x/pv.h

[...]

>  
>  #define KERN_IMAGE_START                0x010000UL
>  #define LINUX_MAGIC_ADDR                0x010008UL
> @@ -676,6 +677,38 @@ static void s390_ipl_prepare_qipl(S390CPU *cpu)
>      cpu_physical_memory_unmap(addr, len, 1, len);
>  }
>  
> +int s390_ipl_prepare_pv_header(void)
> +{
> +    S390IPLState *ipl = get_ipl_device();
> +    IPLBlockPV *ipib_pv = &ipl->iplb_pv.pv;
> +    void *hdr = g_malloc(ipib_pv->pv_header_len);
> +    int rc;
> +
> +    cpu_physical_memory_read(ipib_pv->pv_header_addr, hdr,
> +                             ipib_pv->pv_header_len);
> +    rc = s390_pv_set_sec_parms((uint64_t)hdr,
> +                          ipib_pv->pv_header_len);
> +    g_free(hdr);
> +    return rc;
> +}
> +
> +int s390_ipl_pv_unpack(void)
> +{
> +    int i, rc = 0;
> +    S390IPLState *ipl = get_ipl_device();
> +    IPLBlockPV *ipib_pv = &ipl->iplb_pv.pv;
> +
> +    for (i = 0; i < ipib_pv->num_comp; i++) {
> +        rc = s390_pv_unpack(ipib_pv->components[i].addr,
> +                            TARGET_PAGE_ALIGN(ipib_pv->components[i].size),
> +                            ipib_pv->components[i].tweak_pref);
> +        if (rc) {
> +            break;
> +        }

You can check for " && !rc" in the loop condition

> +    }
> +    return rc;
> +}
> +
>  void s390_ipl_prepare_cpu(S390CPU *cpu)
>  {
>      S390IPLState *ipl = get_ipl_device();
> diff --git a/hw/s390x/ipl.h b/hw/s390x/ipl.h
> index 04be63cee1..ad8090a02c 100644
> --- a/hw/s390x/ipl.h
> +++ b/hw/s390x/ipl.h
> @@ -105,6 +105,8 @@ typedef union IplParameterBlock IplParameterBlock;
>  int s390_ipl_set_loadparm(uint8_t *loadparm);
>  int s390_ipl_pv_check_components(IplParameterBlock *iplb);
>  void s390_ipl_update_diag308(IplParameterBlock *iplb);
> +int s390_ipl_prepare_pv_header(void);
> +int s390_ipl_pv_unpack(void);
>  void s390_ipl_prepare_cpu(S390CPU *cpu);
>  IplParameterBlock *s390_ipl_get_iplb(void);
>  IplParameterBlock *s390_ipl_get_iplb_secure(void);
> diff --git a/hw/s390x/pv.c b/hw/s390x/pv.c
> new file mode 100644
> index 0000000000..50b68b6c34
> --- /dev/null
> +++ b/hw/s390x/pv.c
> @@ -0,0 +1,106 @@
> +/*
> + * Secure execution functions
> + *
> + * Copyright IBM Corp. 2020
> + * Author(s):
> + *  Janosch Frank <frankja@linux.ibm.com>
> + *
> + * This work is licensed under the terms of the GNU GPL, version 2 or (at
> + * your option) any later version. See the COPYING file in the top-level
> + * directory.
> + */
> +#include "qemu/osdep.h"
> +#include <sys/ioctl.h>
> +
> +#include <linux/kvm.h>
> +
> +#include "qemu/error-report.h"
> +#include "sysemu/kvm.h"
> +#include "pv.h"
> +
> +const char *cmd_names[] = {
> +    "VM_ENABLE",
> +    "VM_DISABLE",
> +    "VM_SET_SEC_PARAMS",
> +    "VM_UNPACK",
> +    "VM_VERIFY",
> +    "VM_PREP_RESET",
> +    "VM_UNSHARE_ALL",
> +    NULL

Is the NULL really needed? This will be somewhat error prone when we add
new PV commands. Not sure if guarding this by an access function (chack
against ARRAY_SIZE() makes sense).

> diff --git a/hw/s390x/s390-virtio-ccw.c b/hw/s390x/s390-virtio-ccw.c
> index a89cf4c129..dd39890f89 100644
> --- a/hw/s390x/s390-virtio-ccw.c
> +++ b/hw/s390x/s390-virtio-ccw.c
> @@ -41,6 +41,8 @@
>  #include "hw/qdev-properties.h"
>  #include "hw/s390x/tod.h"
>  #include "sysemu/sysemu.h"
> +#include "hw/s390x/pv.h"
> +#include <linux/kvm.h>
>  
>  S390CPU *s390_cpu_addr2state(uint16_t cpu_addr)
>  {
> @@ -238,9 +240,11 @@ static void s390_create_sclpconsole(const char *type, Chardev *chardev)
>  static void ccw_init(MachineState *machine)
>  {
>      int ret;
> +    S390CcwMachineState *ms = S390_CCW_MACHINE(machine);
>      VirtualCssBus *css_bus;
>      DeviceState *dev;
>  
> +    ms->pv = false;

Should not be necessary, default is false.

>      s390_sclp_init();
>      /* init memory + setup max page size. Required for the CPU model */
>      s390_memory_init(machine->ram);
> @@ -316,10 +320,75 @@ static inline void s390_do_cpu_ipl(CPUState *cs, run_on_cpu_data arg)
>      s390_cpu_set_state(S390_CPU_STATE_OPERATING, cpu);
>  }
>  
> +static void s390_machine_unprotect(S390CcwMachineState *ms)
> +{
> +    CPUState *t;
> +
> +    if (!ms->pv)
> +        return;

How can this ever happen? g_assert(ms->pv) ?

Also, i don't see this function getting called from anywhere else except
when s390_machine_protect() fails. That looks wrong. This has to be
called when going out of PV mode.


> +    s390_pv_vm_disable();
> +    CPU_FOREACH(t) {
> +        S390_CPU(t)->env.pv = false;
> +    }
> +    ms->pv = false;
> +}
> +
> +static int s390_machine_protect(S390CcwMachineState *ms)
> +{
> +    CPUState *t;
> +    int rc;
> +

g_assert(!ms->pv) ?

> +    /* Create SE VM */
> +    rc = s390_pv_vm_enable();
> +    if (rc) {
> +        return rc;
> +    }
> +
> +    CPU_FOREACH(t) {
> +        S390_CPU(t)->env.pv = true;
> +    }
> +    ms->pv = true;
> +
> +    /* Set SE header and unpack */
> +    rc = s390_ipl_prepare_pv_header();
> +    if (rc) {
> +        goto out_err;
> +    }
> +
> +    /* Decrypt image */
> +    rc = s390_ipl_pv_unpack();
> +    if (rc) {
> +        goto out_err;
> +    }
> +
> +    /* Verify integrity */
> +    rc = s390_pv_verify();
> +    if (rc) {
> +        goto out_err;
> +    }
> +    return rc;
> +
> +out_err:
> +    s390_machine_unprotect(ms);
> +    return rc;
> +}
> +
> +#define DIAG_308_RC_INVAL_FOR_PV    0x0a02
> +static void s390_machine_inject_pv_error(CPUState *cs)
> +{
> +    int r1 = (cs->kvm_run->s390_sieic.ipa & 0x00f0) >> 4;
> +    CPUS390XState *env = &S390_CPU(cs)->env;
> +
> +    /* Report that we are unable to enter protected mode */
> +    env->regs[r1 + 1] = DIAG_308_RC_INVAL_FOR_PV;
> +}
> +

[...]
>      switch (reset_type) {
>      case S390_RESET_EXTERNAL:
>      case S390_RESET_REIPL:
> @@ -353,6 +424,26 @@ static void s390_machine_reset(MachineState *machine)
>          }
>          subsystem_reset();
>          run_on_cpu(cs, s390_do_cpu_initial_reset, RUN_ON_CPU_NULL);
> +        run_on_cpu(cs, s390_do_cpu_load_normal, RUN_ON_CPU_NULL);

This does look unrelated and wrong?

> +        break;
> +    case S390_RESET_PV: /* Subcode 10 */
> +        subsystem_reset();
> +        s390_crypto_reset();
> +
> +        CPU_FOREACH(t) {
> +            if (t == cs) {
> +                continue;
> +            }
> +            run_on_cpu(t, s390_do_cpu_full_reset, RUN_ON_CPU_NULL);
> +        }
> +        run_on_cpu(cs, s390_do_cpu_reset, RUN_ON_CPU_NULL);
> +
> +        if (s390_machine_protect(ms)) {
> +            s390_machine_inject_pv_error(cs);

Ah, so it's not an actual exception. BUT: All other guest cpus were
reset, can the guest deal with that?

(run_on_cpu(cs, s390_do_cpu_reset, RUN_ON_CPU_NULL) should go after the
s390_machine_protect() I assume - the change you had in the other patch)

> +            s390_cpu_set_state(S390_CPU_STATE_OPERATING, cpu);
> +            return;
> +        }
> +
>          run_on_cpu(cs, s390_do_cpu_load_normal, RUN_ON_CPU_NULL);
>          break;
>      default:


-- 
Thanks,

David / dhildenb



  reply	other threads:[~2020-03-05 13:52 UTC|newest]

Thread overview: 72+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-04 11:42 [PATCH v6 00/18] s390x: Protected Virtualization support Janosch Frank
2020-03-04 11:42 ` [PATCH v6 01/18] Sync pv Janosch Frank
2020-03-04 11:42 ` [PATCH v6 02/18] s390x: protvirt: Add diag308 subcodes 8 - 10 Janosch Frank
2020-03-04 17:04   ` David Hildenbrand
2020-03-05 12:04     ` Janosch Frank
2020-03-05 12:24       ` Janosch Frank
2020-03-05 12:30         ` David Hildenbrand
2020-03-04 17:04   ` David Hildenbrand
2020-03-04 17:06   ` David Hildenbrand
2020-03-06  9:59     ` Janosch Frank
2020-03-04 18:59   ` Christian Borntraeger
2020-03-05 14:39     ` Janosch Frank
2020-03-04 11:42 ` [PATCH v6 03/18] s390x: protvirt: Support unpack facility Janosch Frank
2020-03-05 13:51   ` David Hildenbrand [this message]
2020-03-05 14:10     ` Janosch Frank
2020-03-05 14:15       ` David Hildenbrand
2020-03-05 14:20         ` Janosch Frank
2020-03-05 14:23           ` David Hildenbrand
2020-03-05 14:24             ` Janosch Frank
2020-03-05 13:52   ` David Hildenbrand
2020-03-05 14:15     ` Janosch Frank
2020-03-06 11:48   ` Christian Borntraeger
2020-03-06 13:36     ` Janosch Frank
2020-03-04 11:42 ` [PATCH v6 04/18] s390x: protvirt: Add migration blocker Janosch Frank
2020-03-04 17:13   ` David Hildenbrand
2020-03-05  9:16     ` Janosch Frank
2020-03-05  9:30       ` David Hildenbrand
2020-03-04 11:42 ` [PATCH v6 05/18] s390x: protvirt: Handle diag 308 subcodes 0,1,3,4 Janosch Frank
2020-03-04 11:42 ` [PATCH v6 06/18] s390x: protvirt: Inhibit balloon when switching to protected mode Janosch Frank
2020-03-05 12:00   ` Christian Borntraeger
2020-03-04 11:42 ` [PATCH v6 07/18] s390x: protvirt: KVM intercept changes Janosch Frank
2020-03-04 11:42 ` [PATCH v6 08/18] s390x: Add SIDA memory ops Janosch Frank
2020-03-04 17:39   ` David Hildenbrand
2020-03-05  9:23     ` Janosch Frank
2020-03-04 11:42 ` [PATCH v6 09/18] s390x: protvirt: Move STSI data over SIDAD Janosch Frank
2020-03-04 17:43   ` David Hildenbrand
2020-03-05  9:27     ` Janosch Frank
2020-03-04 11:42 ` [PATCH v6 10/18] s390x: protvirt: SCLP interpretation Janosch Frank
2020-03-04 17:48   ` David Hildenbrand
2020-03-05  9:34     ` Janosch Frank
2020-03-05 10:09       ` David Hildenbrand
2020-03-04 11:42 ` [PATCH v6 11/18] s390x: protvirt: Set guest IPL PSW Janosch Frank
2020-03-04 17:51   ` David Hildenbrand
2020-03-04 11:42 ` [PATCH v6 12/18] s390x: protvirt: Move diag 308 data over SIDAD Janosch Frank
2020-03-04 17:54   ` David Hildenbrand
2020-03-04 11:42 ` [PATCH v6 13/18] s390x: protvirt: Disable address checks for PV guest IO emulation Janosch Frank
2020-03-04 17:55   ` David Hildenbrand
2020-03-05  9:42     ` Janosch Frank
2020-03-05 10:00       ` David Hildenbrand
2020-03-05 11:26         ` Janosch Frank
2020-03-05 11:37           ` David Hildenbrand
2020-03-04 11:42 ` [PATCH v6 14/18] s390x: protvirt: Move IO control structures over SIDA Janosch Frank
2020-03-04 18:56   ` David Hildenbrand
2020-03-05  9:55     ` Janosch Frank
2020-03-05 10:01       ` David Hildenbrand
2020-03-04 11:42 ` [PATCH v6 15/18] s390x: protvirt: Handle SIGP store status correctly Janosch Frank
2020-03-04 18:41   ` David Hildenbrand
2020-03-05  9:59     ` Janosch Frank
2020-03-04 11:42 ` [PATCH v6 16/18] s390x: Add unpack facility feature to GA1 Janosch Frank
2020-03-04 18:42   ` David Hildenbrand
2020-03-06 10:14   ` Janosch Frank
2020-03-06 10:22     ` David Hildenbrand
2020-03-04 11:42 ` [PATCH v6 17/18] docs: Add protvirt docs Janosch Frank
2020-03-04 19:09   ` David Hildenbrand
2020-03-09  9:51     ` Janosch Frank
2020-03-04 11:42 ` [PATCH v6 18/18] pc-bios: s390x: Save iplb location in lowcore Janosch Frank
2020-03-04 12:40   ` David Hildenbrand
2020-03-04 13:25   ` Christian Borntraeger
2020-03-04 13:37     ` David Hildenbrand
2020-03-05 17:04   ` Christian Borntraeger
2020-03-04 17:15 ` [PATCH v6 00/18] s390x: Protected Virtualization support David Hildenbrand
2020-03-04 17:45   ` Christian Borntraeger

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2a93ff2e-4955-67c4-e7af-766a49fc8b32@redhat.com \
    --to=david@redhat.com \
    --cc=borntraeger@de.ibm.com \
    --cc=cohuck@redhat.com \
    --cc=frankja@linux.ibm.com \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-s390x@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).