unicorn/qemu/softmmu/cpus.c

215 lines
5.8 KiB
C
Raw Normal View History

2015-08-21 10:04:50 +03:00
/*
* QEMU System Emulator
*
* Copyright (c) 2003-2008 Fabrice Bellard
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
2021-10-03 17:14:44 +03:00
#include "sysemu/tcg.h"
2015-08-21 10:04:50 +03:00
#include "sysemu/cpus.h"
2021-10-03 17:14:44 +03:00
#include "qemu/bitmap.h"
#include "tcg/tcg.h"
#include "exec/tb-hash.h"
#include "accel/tcg/translate-all.h"
2015-08-21 10:04:50 +03:00
#include "uc_priv.h"
2021-10-03 17:14:44 +03:00
int64_t cpu_icount_to_ns(int64_t icount)
2015-08-21 10:04:50 +03:00
{
2021-10-03 17:14:44 +03:00
// return icount << atomic_read(&timers_state.icount_time_shift);
// from configure_icount(QemuOpts *opts, Error **errp)
/* 125MIPS seems a reasonable initial guess at the guest speed.
It will be corrected fairly quickly anyway. */
// timers_state.icount_time_shift = 3;
return icount << 3;
2015-08-21 10:04:50 +03:00
}
bool cpu_is_stopped(CPUState *cpu)
{
return cpu->stopped;
}
2021-10-03 17:14:44 +03:00
/* return the time elapsed in VM between vm_start and vm_stop. Unless
* icount is active, cpu_get_ticks() uses units of the host CPU cycle
* counter.
*/
int64_t cpu_get_ticks(void)
2015-08-21 10:04:50 +03:00
{
2021-10-03 17:14:44 +03:00
return cpu_get_host_ticks();
2015-08-21 10:04:50 +03:00
}
2021-10-03 17:14:44 +03:00
/* Return the monotonic time elapsed in VM, i.e.,
* the time between vm_start and vm_stop
*/
int64_t cpu_get_clock(void)
2015-08-21 10:04:50 +03:00
{
2021-10-03 17:14:44 +03:00
return get_clock();
2015-08-21 10:04:50 +03:00
}
2021-10-03 17:14:44 +03:00
static bool cpu_can_run(CPUState *cpu)
2015-08-21 10:04:50 +03:00
{
2021-10-03 17:14:44 +03:00
if (cpu->stop) {
return false;
2015-08-21 10:04:50 +03:00
}
2021-10-03 17:14:44 +03:00
if (cpu_is_stopped(cpu)) {
return false;
}
return true;
2015-08-21 10:04:50 +03:00
}
2021-10-03 17:14:44 +03:00
static void cpu_handle_guest_debug(CPUState *cpu)
2015-08-21 10:04:50 +03:00
{
2021-10-03 17:14:44 +03:00
cpu->stopped = true;
2015-08-21 10:04:50 +03:00
}
2021-10-03 17:14:44 +03:00
static int tcg_cpu_exec(struct uc_struct *uc)
2015-08-21 10:04:50 +03:00
{
int r;
bool finish = false;
2021-10-03 17:14:44 +03:00
2016-09-23 17:38:21 +03:00
while (!uc->exit_request) {
CPUState *cpu = uc->cpu;
2015-08-21 10:04:50 +03:00
//qemu_clock_enable(QEMU_CLOCK_VIRTUAL,
// (cpu->singlestep_enabled & SSTEP_NOTIMER) == 0);
if (cpu_can_run(cpu)) {
uc->quit_request = false;
2021-10-03 17:14:44 +03:00
r = cpu_exec(uc, cpu);
// quit current TB but continue emulating?
if (uc->quit_request) {
// reset stop_request
uc->stop_request = false;
} else if (uc->stop_request) {
2015-08-21 10:04:50 +03:00
//printf(">>> got STOP request!!!\n");
finish = true;
break;
}
// save invalid memory access error & quit
2021-10-03 17:14:44 +03:00
if (uc->invalid_error) {
2015-08-21 10:04:50 +03:00
// printf(">>> invalid memory accessed, STOP = %u!!!\n", env->invalid_error);
finish = true;
break;
}
// printf(">>> stop with r = %x, HLT=%x\n", r, EXCP_HLT);
if (r == EXCP_DEBUG) {
cpu_handle_guest_debug(cpu);
break;
}
if (r == EXCP_HLT) {
//printf(">>> got HLT!!!\n");
finish = true;
break;
}
} else if (cpu->stop || cpu->stopped) {
2019-07-31 10:43:06 +03:00
// printf(">>> got stopped!!!\n");
2015-08-21 10:04:50 +03:00
break;
}
}
uc->exit_request = 0;
return finish;
}
2021-10-03 17:14:44 +03:00
void cpu_resume(CPUState *cpu)
2015-08-21 10:04:50 +03:00
{
2021-10-03 17:14:44 +03:00
cpu->stop = false;
cpu->stopped = false;
2015-08-21 10:04:50 +03:00
}
2021-10-03 17:14:44 +03:00
static void qemu_tcg_init_vcpu(CPUState *cpu)
2015-08-21 10:04:50 +03:00
{
2021-10-03 17:14:44 +03:00
/*
* Initialize TCG regions--once. Now is a good time, because:
* (1) TCG's init context, prologue and target globals have been set up.
* (2) qemu_tcg_mttcg_enabled() works now (TCG init code runs before the
* -accel flag is processed, so the check doesn't work then).
*/
tcg_region_init(cpu->uc->tcg_ctx);
cpu->created = true;
2015-08-21 10:04:50 +03:00
}
2021-10-03 17:14:44 +03:00
void qemu_init_vcpu(CPUState *cpu)
2015-08-21 10:04:50 +03:00
{
2021-10-03 17:14:44 +03:00
cpu->nr_cores = 1;
cpu->nr_threads = 1;
cpu->stopped = true;
qemu_tcg_init_vcpu(cpu);
2015-08-21 10:04:50 +03:00
2021-10-03 17:14:44 +03:00
return;
}
2015-08-21 10:04:50 +03:00
2021-10-03 17:14:44 +03:00
void cpu_stop_current(struct uc_struct *uc)
{
if (uc->cpu) {
uc->cpu->stop = false;
uc->cpu->stopped = true;
cpu_exit(uc->cpu);
}
2015-08-21 10:04:50 +03:00
}
2021-10-03 17:14:44 +03:00
void resume_all_vcpus(struct uc_struct* uc)
2015-08-21 10:04:50 +03:00
{
2021-10-03 17:14:44 +03:00
CPUState *cpu = uc->cpu;
tb_page_addr_t start, end;
2021-10-03 17:14:44 +03:00
cpu->halted = 0;
cpu->exit_request = 0;
cpu->exception_index = -1;
cpu_resume(cpu);
/* static void qemu_tcg_cpu_loop(struct uc_struct *uc) */
cpu->created = true;
while (true) {
if (tcg_cpu_exec(uc)) {
break;
}
}
// clear the cache of the addr_end address, since the generated code
// at that address is to exit emulation, but not for the instruction there.
// if we dont do this, next time we cannot emulate at that address
if (uc->addr_end != 0) {
// GVA to GPA (GPA -> HVA via page_find, HVA->HPA via host mmu)
end = get_page_addr_code(uc->cpu->env_ptr, uc->addr_end);
// For 32bit target.
start = (end - 1) & (target_ulong)(-1);
end = end & (target_ulong)(-1);
// Unicorn: Why start - 1?
// 0: INC ecx
// 1: DEC edx <--- We put exit here, then the range of TB is [0, 1)
//
// While tb_invalidate_phys_range invalides [start, end)
tb_invalidate_phys_range(uc, start, end);
}
2021-10-03 17:14:44 +03:00
cpu->created = false;
2015-08-21 10:04:50 +03:00
}
2021-10-03 17:14:44 +03:00
void vm_start(struct uc_struct* uc)
{
resume_all_vcpus(uc);
}