res = vc_fetch_insn_kernel(ctxt, buffer); if (res) {
ctxt->fi.vector = X86_TRAP_PF;
ctxt->fi.error_code = X86_PF_INSTR;
ctxt->fi.cr2 = ctxt->regs->ip; return ES_EXCEPTION;
}
ret = insn_decode(&ctxt->insn, buffer, MAX_INSN_SIZE, INSN_MODE_64); if (ret < 0) return ES_DECODE_FAILED; else return ES_OK;
}
/* * User instruction decoding is also required for the EFI runtime. Even though * the EFI runtime is running in kernel mode, it uses special EFI virtual * address mappings that require the use of efi_mm to properly address and * decode.
*/ staticenum es_result vc_decode_insn(struct es_em_ctxt *ctxt)
{ if (user_mode(ctxt->regs) || mm_is_efi(current->active_mm)) return __vc_decode_user_insn(ctxt); else return __vc_decode_kern_insn(ctxt);
}
/* * This function uses __put_user() independent of whether kernel or user * memory is accessed. This works fine because __put_user() does no * sanity checks of the pointer being accessed. All that it does is * to report when the access failed. * * Also, this function runs in atomic context, so __put_user() is not * allowed to sleep. The page-fault handler detects that it is running * in atomic context and will not try to take mmap_sem and handle the * fault, so additional pagefault_enable()/disable() calls are not * needed. * * The access can't be done via copy_to_user() here because * vc_write_mem() must not use string instructions to access unsafe * memory. The reason is that MOVS is emulated by the #VC handler by * splitting the move up into a read and a write and taking a nested #VC * exception on whatever of them is the MMIO access. Using string * instructions here would cause infinite nesting.
*/ switch (size) { case 1: {
u8 d1;
u8 __user *target = (u8 __user *)dst;
/* * This function uses __get_user() independent of whether kernel or user * memory is accessed. This works fine because __get_user() does no * sanity checks of the pointer being accessed. All that it does is * to report when the access failed. * * Also, this function runs in atomic context, so __get_user() is not * allowed to sleep. The page-fault handler detects that it is running * in atomic context and will not try to take mmap_sem and handle the * fault, so additional pagefault_enable()/disable() calls are not * needed. * * The access can't be done via copy_from_user() here because * vc_read_mem() must not use string instructions to access unsafe * memory. The reason is that MOVS is emulated by the #VC handler by * splitting the move up into a read and a write and taking a nested #VC * exception on whatever of them is the MMIO access. Using string * instructions here would cause infinite nesting.
*/ switch (size) { case 1: {
u8 d1;
u8 __user *s = (u8 __user *)src;
/* * TSC related accesses should not exit to the hypervisor when a guest is * executing with Secure TSC enabled, so special handling is required for * accesses of MSR_IA32_TSC and MSR_AMD64_GUEST_TSC_FREQ.
*/ staticenum es_result __vc_handle_secure_tsc_msrs(struct es_em_ctxt *ctxt, bool write)
{ struct pt_regs *regs = ctxt->regs;
u64 tsc;
/* * Writing to MSR_IA32_TSC can cause subsequent reads of the TSC to * return undefined values, and GUEST_TSC_FREQ is read-only. Generate * a #GP on all writes.
*/ if (write) {
ctxt->fi.vector = X86_TRAP_GP;
ctxt->fi.error_code = 0; return ES_EXCEPTION;
}
/* * GUEST_TSC_FREQ read should not be intercepted when Secure TSC is * enabled. Terminate the guest if a read is attempted.
*/ if (regs->cx == MSR_AMD64_GUEST_TSC_FREQ) return ES_VMM_ERROR;
/* Reads of MSR_IA32_TSC should return the current TSC value. */
tsc = rdtsc_ordered();
regs->ax = lower_32_bits(tsc);
regs->dx = upper_32_bits(tsc);
/* * The MOVS instruction has two memory operands, which raises the * problem that it is not known whether the access to the source or the * destination caused the #VC exception (and hence whether an MMIO read * or write operation needs to be emulated). * * Instead of playing games with walking page-tables and trying to guess * whether the source or destination is an MMIO range, split the move * into two operations, a read and a write with only one memory operand. * This will cause a nested #VC exception on the MMIO address which can * then be handled. * * This implementation has the benefit that it also supports MOVS where * source _and_ destination are MMIO regions. * * It will slow MOVS on MMIO down a lot, but in SEV-ES guests it is a * rare operation. If it turns out to be a performance problem the split * operations can be moved to memcpy_fromio() and memcpy_toio().
*/ staticenum es_result vc_handle_mmio_movs(struct es_em_ctxt *ctxt, unsignedint bytes)
{ unsignedlong ds_base, es_base; unsignedchar *src, *dst; unsignedchar buffer[8]; enum es_result ret; bool rep; int off;
mmio = insn_decode_mmio(insn, &bytes); if (mmio == INSN_MMIO_DECODE_FAILED) return ES_DECODE_FAILED;
if (mmio != INSN_MMIO_WRITE_IMM && mmio != INSN_MMIO_MOVS) {
reg_data = insn_get_modrm_reg_ptr(insn, ctxt->regs); if (!reg_data) return ES_DECODE_FAILED;
}
if (user_mode(ctxt->regs)) return ES_UNSUPPORTED;
switch (mmio) { case INSN_MMIO_WRITE:
memcpy(ghcb->shared_buffer, reg_data, bytes);
ret = vc_do_mmio(ghcb, ctxt, bytes, false); break; case INSN_MMIO_WRITE_IMM:
memcpy(ghcb->shared_buffer, insn->immediate1.bytes, bytes);
ret = vc_do_mmio(ghcb, ctxt, bytes, false); break; case INSN_MMIO_READ:
ret = vc_do_mmio(ghcb, ctxt, bytes, true); if (ret) break;
/* Zero-extend for 32-bit operation */ if (bytes == 4)
*reg_data = 0;
memcpy(reg_data, ghcb->shared_buffer, bytes); break; case INSN_MMIO_READ_ZERO_EXTEND:
ret = vc_do_mmio(ghcb, ctxt, bytes, true); if (ret) break;
/* Zero extend based on operand size */
memset(reg_data, 0, insn->opnd_bytes);
memcpy(reg_data, ghcb->shared_buffer, bytes); break; case INSN_MMIO_READ_SIGN_EXTEND:
ret = vc_do_mmio(ghcb, ctxt, bytes, true); if (ret) break;
if (bytes == 1) {
u8 *val = (u8 *)ghcb->shared_buffer;
if (sev_status & MSR_AMD64_SNP_DEBUG_SWAP) return ES_VMM_ERROR;
if (!reg) return ES_DECODE_FAILED;
val = *reg;
/* Upper 32 bits must be written as zeroes */ if (val >> 32) {
ctxt->fi.vector = X86_TRAP_GP;
ctxt->fi.error_code = 0; return ES_EXCEPTION;
}
/* Clear out other reserved bits and set bit 10 */
val = (val & 0xffff23ffL) | BIT(10);
/* Early non-zero writes to DR7 are not supported */ if (!data && (val & ~DR7_RESET_VALUE)) return ES_UNSUPPORTED;
/* Using a value of 0 for ExitInfo1 means RAX holds the value */
ghcb_set_rax(ghcb, val);
ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_WRITE_DR7, 0, 0); if (ret != ES_OK) return ret;
staticenum es_result vc_handle_monitor(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
{ /* * Treat it as a NOP and do not leak a physical address to the * hypervisor.
*/ return ES_OK;
}
staticenum es_result vc_handle_mwait(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
{ /* Treat the same as MONITOR/MONITORX */ return ES_OK;
}
if (x86_platform.hyper.sev_es_hcall_prepare)
x86_platform.hyper.sev_es_hcall_prepare(ghcb, ctxt->regs);
ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_VMMCALL, 0, 0); if (ret != ES_OK) return ret;
if (!ghcb_rax_is_valid(ghcb)) return ES_VMM_ERROR;
ctxt->regs->ax = ghcb->save.rax;
/* * Call sev_es_hcall_finish() after regs->ax is already set. * This allows the hypervisor handler to overwrite it again if * necessary.
*/ if (x86_platform.hyper.sev_es_hcall_finish &&
!x86_platform.hyper.sev_es_hcall_finish(ghcb, ctxt->regs)) return ES_VMM_ERROR;
return ES_OK;
}
staticenum es_result vc_handle_trap_ac(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
{ /* * Calling ecx_alignment_check() directly does not work, because it * enables IRQs and the GHCB is active. Forward the exception and call * it later from vc_forward_exception().
*/
ctxt->fi.vector = X86_TRAP_AC;
ctxt->fi.error_code = 0; return ES_EXCEPTION;
}
switch (exit_code) { case SVM_EXIT_READ_DR7:
result = vc_handle_dr7_read(ghcb, ctxt); break; case SVM_EXIT_WRITE_DR7:
result = vc_handle_dr7_write(ghcb, ctxt); break; case SVM_EXIT_EXCP_BASE + X86_TRAP_AC:
result = vc_handle_trap_ac(ghcb, ctxt); break; case SVM_EXIT_RDTSC: case SVM_EXIT_RDTSCP:
result = vc_handle_rdtsc(ghcb, ctxt, exit_code); break; case SVM_EXIT_RDPMC:
result = vc_handle_rdpmc(ghcb, ctxt); break; case SVM_EXIT_INVD:
pr_err_ratelimited("#VC exception for INVD??? Seriously???\n");
result = ES_UNSUPPORTED; break; case SVM_EXIT_CPUID:
result = vc_handle_cpuid(ghcb, ctxt); break; case SVM_EXIT_IOIO:
result = vc_handle_ioio(ghcb, ctxt); break; case SVM_EXIT_MSR:
result = vc_handle_msr(ghcb, ctxt); break; case SVM_EXIT_VMMCALL:
result = vc_handle_vmmcall(ghcb, ctxt); break; case SVM_EXIT_WBINVD:
result = vc_handle_wbinvd(ghcb, ctxt); break; case SVM_EXIT_MONITOR:
result = vc_handle_monitor(ghcb, ctxt); break; case SVM_EXIT_MWAIT:
result = vc_handle_mwait(ghcb, ctxt); break; case SVM_EXIT_NPF:
result = vc_handle_mmio(ghcb, ctxt); break; default: /* * Unexpected #VC exception
*/
result = ES_UNSUPPORTED;
}
/* * If the code was already executing on the VC2 stack when the #VC * happened, let it proceed to the normal handling routine. This way the * code executing on the VC2 stack can cause #VC exceptions to get handled.
*/ return is_vc2_stack(sp) && !is_vc2_stack(prev_sp);
}
vc_ghcb_invalidate(ghcb);
result = vc_init_em_ctxt(&ctxt, regs, error_code);
if (result == ES_OK)
result = vc_handle_exitcode(&ctxt, ghcb, error_code);
__sev_put_ghcb(&state);
/* Done - now check the result */ switch (result) { case ES_OK:
vc_finish_insn(&ctxt); break; case ES_UNSUPPORTED:
pr_err_ratelimited("Unsupported exit-code 0x%02lx in #VC exception (IP: 0x%lx)\n",
error_code, regs->ip);
ret = false; break; case ES_VMM_ERROR:
pr_err_ratelimited("Failure in communication with VMM (exit-code 0x%02lx IP: 0x%lx)\n",
error_code, regs->ip);
ret = false; break; case ES_DECODE_FAILED:
pr_err_ratelimited("Failed to decode instruction (exit-code 0x%02lx IP: 0x%lx)\n",
error_code, regs->ip);
ret = false; break; case ES_EXCEPTION:
vc_forward_exception(&ctxt); break; case ES_RETRY: /* Nothing to do */ break; default:
pr_emerg("Unknown result in %s():%d\n", __func__, result); /* * Emulating the instruction which caused the #VC exception * failed - can't continue so print debug information
*/
BUG();
}
/* * Runtime #VC exception handler when raised from kernel mode. Runs in NMI mode * and will panic when an error happens.
*/
DEFINE_IDTENTRY_VC_KERNEL(exc_vmm_communication)
{
irqentry_state_t irq_state;
/* * With the current implementation it is always possible to switch to a * safe stack because #VC exceptions only happen at known places, like * intercepted instructions or accesses to MMIO areas/IO ports. They can * also happen with code instrumentation when the hypervisor intercepts * #DB, but the critical paths are forbidden to be instrumented, so #DB * exceptions currently also only happen in safe places. * * But keep this here in case the noinstr annotations are violated due * to bug elsewhere.
*/ if (unlikely(vc_from_invalid_context(regs))) {
instrumentation_begin();
panic("Can't handle #VC exception from unsupported context\n");
instrumentation_end();
}
/* * Handle #DB before calling into !noinstr code to avoid recursive #DB.
*/ if (vc_is_db(error_code)) {
exc_debug(regs); return;
}
irq_state = irqentry_nmi_enter(regs);
instrumentation_begin();
if (!vc_raw_handle_exception(regs, error_code)) { /* Show some debug info */
show_regs(regs);
/* Ask hypervisor to sev_es_terminate */
sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_GEN_REQ);
/* If that fails and we get here - just panic */
panic("Returned from Terminate-Request to Hypervisor\n");
}
/* * Runtime #VC exception handler when raised from user mode. Runs in IRQ mode * and will kill the current task with SIGBUS when an error happens.
*/
DEFINE_IDTENTRY_VC_USER(exc_vmm_communication)
{ /* * Handle #DB before calling into !noinstr code to avoid recursive #DB.
*/ if (vc_is_db(error_code)) {
noist_exc_debug(regs); return;
}
if (!vc_raw_handle_exception(regs, error_code)) { /* * Do not kill the machine if user-space triggered the * exception. Send SIGBUS instead and let user-space deal with * it.
*/
force_sig_fault(SIGBUS, BUS_OBJERR, (void __user *)0);
}
result = vc_init_em_ctxt(&ctxt, regs, exit_code); if (result == ES_OK)
result = vc_handle_exitcode(&ctxt, boot_ghcb, exit_code);
/* Done - now check the result */ switch (result) { case ES_OK:
vc_finish_insn(&ctxt); break; case ES_UNSUPPORTED:
early_printk("PANIC: Unsupported exit-code 0x%02lx in early #VC exception (IP: 0x%lx)\n",
exit_code, regs->ip); goto fail; case ES_VMM_ERROR:
early_printk("PANIC: Failure in communication with VMM (exit-code 0x%02lx IP: 0x%lx)\n",
exit_code, regs->ip); goto fail; case ES_DECODE_FAILED:
early_printk("PANIC: Failed to decode instruction (exit-code 0x%02lx IP: 0x%lx)\n",
exit_code, regs->ip); goto fail; case ES_EXCEPTION:
vc_early_forward_exception(&ctxt); break; case ES_RETRY: /* Nothing to do */ break; default:
BUG();
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.