This patch introduces an interface to access the guest visible
storage keys. It supports three operations that model the behavior
that SSKE/ISKE/RRBE instructions would have if they were issued by
the guest. These instructions are all documented in the z architecture
principles of operation book.

Signed-off-by: Carsten Otte <[email protected]>
---
Index: linux-2.5-cecsim/arch/s390/include/asm/kvm_host.h
===================================================================
--- linux-2.5-cecsim.orig/arch/s390/include/asm/kvm_host.h
+++ linux-2.5-cecsim/arch/s390/include/asm/kvm_host.h
@@ -25,6 +25,9 @@
 #define KVM_PRIVATE_MEM_SLOTS 4
 
 #define KVM_SIE_PAGE_OFFSET 1
+#define KVM_S390_KEYOP_SSKE 0x01
+#define KVM_S390_KEYOP_ISKE 0x02
+#define KVM_S390_KEYOP_RRBE 0x03
 
 struct sca_entry {
        atomic_t scn;
Index: linux-2.5-cecsim/arch/s390/kvm/kvm-s390.c
===================================================================
--- linux-2.5-cecsim.orig/arch/s390/kvm/kvm-s390.c
+++ linux-2.5-cecsim/arch/s390/kvm/kvm-s390.c
@@ -112,13 +112,144 @@ void kvm_arch_exit(void)
 {
 }
 
+pte_t *ptep_for_addr(unsigned long addr)
+{
+       struct vm_area_struct *vma;
+       pgd_t *pgd;
+       pud_t *pud;
+       pmd_t *pmd;
+       pte_t *rc;
+
+       down_read(&current->mm->mmap_sem);
+
+       rc = NULL;
+       vma = find_vma(current->mm, addr);
+       if (!vma)
+               goto up_out;
+
+       pgd = pgd_offset(current->mm, addr);
+       pud = pud_alloc(current->mm, pgd, addr);
+       if (!pud)
+               goto up_out;
+
+       pmd = pmd_alloc(current->mm, pud, addr);
+       if (!pmd)
+               goto up_out;
+
+       if (!pmd_present(*pmd) &&
+           __pte_alloc(current->mm, vma, pmd, addr))
+               goto up_out;
+
+       rc = pte_offset(pmd, addr);
+up_out:
+       up_read(&current->mm->mmap_sem);
+       return rc;
+}
+
+static long kvm_s390_keyop(struct kvm_s390_keyop *kop)
+{
+       unsigned long addr = kop->user_addr;
+       pte_t *ptep;
+       pgste_t pgste;
+       int r;
+       unsigned long skey;
+       unsigned long bits;
+
+       /* make sure this process is a hypervisor */
+       r = -EINVAL;
+       if (!mm_has_pgste(current->mm))
+               goto out;
+
+       r = -ENXIO;
+       if (addr >= PGDIR_SIZE)
+               goto out;
+
+       spin_lock(&current->mm->page_table_lock);
+       ptep = ptep_for_addr(addr);
+       if (!ptep)
+               goto out_unlock;
+
+       pgste = pgste_get_lock(ptep);
+
+       switch (kop->operation) {
+       case KVM_S390_KEYOP_SSKE:
+               pgste = pgste_update_all(ptep, pgste);
+               /* set the real key back w/o rc bits */
+               skey = kop->key & (_PAGE_ACC_BITS | _PAGE_FP_BIT);
+               if (pte_present(*ptep))
+                       page_set_storage_key(pte_val(*ptep), skey, 1);
+               /* put acc+f plus guest refereced and changed into the pgste */
+               pgste_val(pgste) &= ~(RCP_ACC_BITS | RCP_FP_BIT | RCP_GR_BIT
+                                    | RCP_GC_BIT);
+               bits = (kop->key & (_PAGE_ACC_BITS | _PAGE_FP_BIT));
+               pgste_val(pgste) |= bits << 56;
+               bits = (kop->key & (_PAGE_CHANGED | _PAGE_REFERENCED));
+               pgste_val(pgste) |= bits << 48;
+               r = 0;
+               break;
+       case KVM_S390_KEYOP_ISKE:
+               if (pte_present(*ptep)) {
+                       skey = page_get_storage_key(pte_val(*ptep));
+                       kop->key = skey & (_PAGE_ACC_BITS | _PAGE_FP_BIT);
+               } else {
+                       skey = 0;
+                       kop->key = (pgste_val(pgste) >> 56) &
+                                  (_PAGE_ACC_BITS | _PAGE_FP_BIT);
+               }
+               kop->key |= skey & (_PAGE_CHANGED | _PAGE_REFERENCED);
+               kop->key |= (pgste_val(pgste) >> 48) &
+                           (_PAGE_CHANGED | _PAGE_REFERENCED);
+               r = 0;
+               break;
+       case KVM_S390_KEYOP_RRBE:
+               pgste = pgste_update_all(ptep, pgste);
+               kop->key = 0;
+               if (pgste_val(pgste) & RCP_GR_BIT)
+                       kop->key |= _PAGE_REFERENCED;
+               pgste_val(pgste) &= ~RCP_GR_BIT;
+               r = 0;
+               break;
+       default:
+               r = -EINVAL;
+       }
+       pgste_set_unlock(ptep, pgste);
+
+out_unlock:
+       spin_unlock(&current->mm->page_table_lock);
+out:
+       return r;
+}
+
 /* Section: device related */
 long kvm_arch_dev_ioctl(struct file *filp,
                        unsigned int ioctl, unsigned long arg)
 {
-       if (ioctl == KVM_S390_ENABLE_SIE)
-               return s390_enable_sie();
-       return -EINVAL;
+       void __user *argp = (void __user *)arg;
+       int r;
+
+       switch (ioctl) {
+       case KVM_S390_ENABLE_SIE:
+               r = s390_enable_sie();
+               break;
+       case KVM_S390_KEYOP: {
+               struct kvm_s390_keyop kop;
+               r = -EFAULT;
+               if (copy_from_user(&kop, argp, sizeof(struct kvm_s390_keyop)))
+                       break;
+               r = kvm_s390_keyop(&kop);
+               if (r)
+                       break;
+               r = -EFAULT;
+               if (copy_to_user(argp, &kop, sizeof(struct kvm_s390_keyop)))
+                       break;
+               r = 0;
+               break;
+       }
+       default:
+               r = -ENOTTY;
+       }
+
+       return r;
 }
 
 int kvm_dev_ioctl_check_extension(long ext)
Index: linux-2.5-cecsim/include/linux/kvm.h
===================================================================
--- linux-2.5-cecsim.orig/include/linux/kvm.h
+++ linux-2.5-cecsim/include/linux/kvm.h
@@ -445,6 +445,13 @@ struct kvm_ppc_pvinfo {
 #define KVM_GET_MSR_INDEX_LIST    _IOWR(KVMIO, 0x02, struct kvm_msr_list)
 
 #define KVM_S390_ENABLE_SIE       _IO(KVMIO,   0x06)
+
+struct kvm_s390_keyop {
+       __u64 user_addr;
+       __u8  key;
+       __u8  operation;
+};
+#define KVM_S390_KEYOP            _IOWR(KVMIO,   0x09, struct kvm_s390_keyop)
 /*
  * Check if a kvm extension is available.  Argument is extension number,
  * return is 1 (yes) or 0 (no, sorry).

--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to