15abf9495SPeter Crosthwaite /* 25abf9495SPeter Crosthwaite * emulator main execution loop 35abf9495SPeter Crosthwaite * 45abf9495SPeter Crosthwaite * Copyright (c) 2003-2005 Fabrice Bellard 55abf9495SPeter Crosthwaite * 65abf9495SPeter Crosthwaite * This library is free software; you can redistribute it and/or 75abf9495SPeter Crosthwaite * modify it under the terms of the GNU Lesser General Public 85abf9495SPeter Crosthwaite * License as published by the Free Software Foundation; either 95abf9495SPeter Crosthwaite * version 2 of the License, or (at your option) any later version. 105abf9495SPeter Crosthwaite * 115abf9495SPeter Crosthwaite * This library is distributed in the hope that it will be useful, 125abf9495SPeter Crosthwaite * but WITHOUT ANY WARRANTY; without even the implied warranty of 135abf9495SPeter Crosthwaite * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 145abf9495SPeter Crosthwaite * Lesser General Public License for more details. 155abf9495SPeter Crosthwaite * 165abf9495SPeter Crosthwaite * You should have received a copy of the GNU Lesser General Public 175abf9495SPeter Crosthwaite * License along with this library; if not, see <http://www.gnu.org/licenses/>. 185abf9495SPeter Crosthwaite */ 195abf9495SPeter Crosthwaite 205abf9495SPeter Crosthwaite #include "config.h" 215abf9495SPeter Crosthwaite #include "cpu.h" 225abf9495SPeter Crosthwaite #include "sysemu/cpus.h" 235abf9495SPeter Crosthwaite #include "exec/memory-internal.h" 245abf9495SPeter Crosthwaite 255abf9495SPeter Crosthwaite bool exit_request; 265abf9495SPeter Crosthwaite CPUState *tcg_current_cpu; 275abf9495SPeter Crosthwaite 285abf9495SPeter Crosthwaite /* exit the current TB from a signal handler. The host registers are 295abf9495SPeter Crosthwaite restored in a state compatible with the CPU emulator 305abf9495SPeter Crosthwaite */ 315abf9495SPeter Crosthwaite #if defined(CONFIG_SOFTMMU) 325abf9495SPeter Crosthwaite void cpu_resume_from_signal(CPUState *cpu, void *puc) 335abf9495SPeter Crosthwaite { 345abf9495SPeter Crosthwaite /* XXX: restore cpu registers saved in host registers */ 355abf9495SPeter Crosthwaite 365abf9495SPeter Crosthwaite cpu->exception_index = -1; 375abf9495SPeter Crosthwaite siglongjmp(cpu->jmp_env, 1); 385abf9495SPeter Crosthwaite } 395abf9495SPeter Crosthwaite 405abf9495SPeter Crosthwaite void cpu_reload_memory_map(CPUState *cpu) 415abf9495SPeter Crosthwaite { 425abf9495SPeter Crosthwaite AddressSpaceDispatch *d; 435abf9495SPeter Crosthwaite 445abf9495SPeter Crosthwaite if (qemu_in_vcpu_thread()) { 45*53f8a5e9SPeter Maydell /* The guest can in theory prolong the RCU critical section as long 46*53f8a5e9SPeter Maydell * as it feels like. The major problem with this is that because it 47*53f8a5e9SPeter Maydell * can do multiple reconfigurations of the memory map within the 48*53f8a5e9SPeter Maydell * critical section, we could potentially accumulate an unbounded 49*53f8a5e9SPeter Maydell * collection of memory data structures awaiting reclamation. 505abf9495SPeter Crosthwaite * 51*53f8a5e9SPeter Maydell * Because the only thing we're currently protecting with RCU is the 52*53f8a5e9SPeter Maydell * memory data structures, it's sufficient to break the critical section 53*53f8a5e9SPeter Maydell * in this callback, which we know will get called every time the 54*53f8a5e9SPeter Maydell * memory map is rearranged. 55*53f8a5e9SPeter Maydell * 56*53f8a5e9SPeter Maydell * (If we add anything else in the system that uses RCU to protect 57*53f8a5e9SPeter Maydell * its data structures, we will need to implement some other mechanism 58*53f8a5e9SPeter Maydell * to force TCG CPUs to exit the critical section, at which point this 59*53f8a5e9SPeter Maydell * part of this callback might become unnecessary.) 605abf9495SPeter Crosthwaite * 615abf9495SPeter Crosthwaite * This pair matches cpu_exec's rcu_read_lock()/rcu_read_unlock(), which 625abf9495SPeter Crosthwaite * only protects cpu->as->dispatch. Since we reload it below, we can 635abf9495SPeter Crosthwaite * split the critical section. 645abf9495SPeter Crosthwaite */ 655abf9495SPeter Crosthwaite rcu_read_unlock(); 665abf9495SPeter Crosthwaite rcu_read_lock(); 675abf9495SPeter Crosthwaite } 685abf9495SPeter Crosthwaite 695abf9495SPeter Crosthwaite /* The CPU and TLB are protected by the iothread lock. */ 705abf9495SPeter Crosthwaite d = atomic_rcu_read(&cpu->as->dispatch); 715abf9495SPeter Crosthwaite cpu->memory_dispatch = d; 725abf9495SPeter Crosthwaite tlb_flush(cpu, 1); 735abf9495SPeter Crosthwaite } 745abf9495SPeter Crosthwaite #endif 755abf9495SPeter Crosthwaite 765abf9495SPeter Crosthwaite void cpu_loop_exit(CPUState *cpu) 775abf9495SPeter Crosthwaite { 785abf9495SPeter Crosthwaite cpu->current_tb = NULL; 795abf9495SPeter Crosthwaite siglongjmp(cpu->jmp_env, 1); 805abf9495SPeter Crosthwaite } 815abf9495SPeter Crosthwaite 825abf9495SPeter Crosthwaite void cpu_loop_exit_restore(CPUState *cpu, uintptr_t pc) 835abf9495SPeter Crosthwaite { 845abf9495SPeter Crosthwaite if (pc) { 855abf9495SPeter Crosthwaite cpu_restore_state(cpu, pc); 865abf9495SPeter Crosthwaite } 875abf9495SPeter Crosthwaite cpu->current_tb = NULL; 885abf9495SPeter Crosthwaite siglongjmp(cpu->jmp_env, 1); 895abf9495SPeter Crosthwaite } 90