1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* 3 * Library for managing various aspects of guests 4 * 5 * Copyright (c) 2021 IBM Corp 6 * 7 * Authors: 8 * Janosch Frank <frankja@linux.ibm.com> 9 */ 10 11 #include <asm/barrier.h> 12 #include <bitops.h> 13 #include <libcflat.h> 14 #include <sie.h> 15 #include <asm/page.h> 16 #include <libcflat.h> 17 #include <alloc_page.h> 18 19 void sie_expect_validity(struct vm *vm) 20 { 21 vm->validity_expected = true; 22 } 23 24 uint16_t sie_get_validity(struct vm *vm) 25 { 26 assert(vm->sblk->icptcode == ICPT_VALIDITY); 27 return vm->sblk->ipb >> 16; 28 } 29 30 void sie_check_validity(struct vm *vm, uint16_t vir_exp) 31 { 32 uint16_t vir = sie_get_validity(vm); 33 34 report(vir_exp == vir, "VALIDITY: %x", vir); 35 } 36 37 void sie_handle_validity(struct vm *vm) 38 { 39 if (vm->sblk->icptcode != ICPT_VALIDITY) 40 return; 41 42 if (!vm->validity_expected) 43 report_abort("VALIDITY: %x", sie_get_validity(vm)); 44 vm->validity_expected = false; 45 } 46 47 void sie(struct vm *vm) 48 { 49 if (vm->sblk->sdf == 2) 50 memcpy(vm->sblk->pv_grregs, vm->save_area.guest.grs, 51 sizeof(vm->save_area.guest.grs)); 52 53 /* Reset icptcode so we don't trip over it below */ 54 vm->sblk->icptcode = 0; 55 56 while (vm->sblk->icptcode == 0) { 57 sie64a(vm->sblk, &vm->save_area); 58 sie_handle_validity(vm); 59 } 60 vm->save_area.guest.grs[14] = vm->sblk->gg14; 61 vm->save_area.guest.grs[15] = vm->sblk->gg15; 62 63 if (vm->sblk->sdf == 2) 64 memcpy(vm->save_area.guest.grs, vm->sblk->pv_grregs, 65 sizeof(vm->save_area.guest.grs)); 66 } 67 68 void sie_guest_sca_create(struct vm *vm) 69 { 70 vm->sca = (struct esca_block *)alloc_page(); 71 72 /* Let's start out with one page of ESCA for now */ 73 vm->sblk->scaoh = ((uint64_t)vm->sca >> 32); 74 vm->sblk->scaol = (uint64_t)vm->sca & ~0x3fU; 75 vm->sblk->ecb2 |= ECB2_ESCA; 76 77 /* Enable SIGP sense running interpretation */ 78 vm->sblk->ecb |= ECB_SRSI; 79 80 /* We assume that cpu 0 is always part of the vm */ 81 vm->sca->mcn[0] = BIT(63); 82 vm->sca->cpu[0].sda = (uint64_t)vm->sblk; 83 } 84 85 /* Initializes the struct vm members like the SIE control block. */ 86 void sie_guest_create(struct vm *vm, uint64_t guest_mem, uint64_t guest_mem_len) 87 { 88 vm->sblk = alloc_page(); 89 memset(vm->sblk, 0, PAGE_SIZE); 90 vm->sblk->cpuflags = CPUSTAT_ZARCH | CPUSTAT_RUNNING; 91 vm->sblk->ihcpu = 0xffff; 92 vm->sblk->prefix = 0; 93 94 /* Guest memory chunks are always 1MB */ 95 assert(!(guest_mem_len & ~HPAGE_MASK)); 96 /* For non-PV guests we re-use the host's ASCE for ease of use */ 97 vm->save_area.guest.asce = stctg(1); 98 /* Currently MSO/MSL is the easiest option */ 99 vm->sblk->mso = (uint64_t)guest_mem; 100 vm->sblk->msl = (uint64_t)guest_mem + ((guest_mem_len - 1) & HPAGE_MASK); 101 102 /* CRYCB needs to be in the first 2GB */ 103 vm->crycb = alloc_pages_flags(0, AREA_DMA31); 104 vm->sblk->crycbd = (uint32_t)(uintptr_t)vm->crycb; 105 } 106 107 /* Frees the memory that was gathered on initialization */ 108 void sie_guest_destroy(struct vm *vm) 109 { 110 free_page(vm->crycb); 111 free_page(vm->sblk); 112 if (vm->sblk->ecb2 & ECB2_ESCA) 113 free_page(vm->sca); 114 } 115