1 /* 2 * OpenRISC Machine 3 * 4 * Copyright (c) 2011-2012 Jia Liu <proljc@gmail.com> 5 * 6 * This library is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU Lesser General Public 8 * License as published by the Free Software Foundation; either 9 * version 2 of the License, or (at your option) any later version. 10 * 11 * This library is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public 17 * License along with this library; if not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 #include "qemu/osdep.h" 21 #include "qemu-common.h" 22 #include "cpu.h" 23 #include "hw/hw.h" 24 #include "hw/boards.h" 25 #include "migration/cpu.h" 26 27 static int env_post_load(void *opaque, int version_id) 28 { 29 CPUOpenRISCState *env = opaque; 30 31 /* Restore MMU handlers */ 32 if (env->sr & SR_DME) { 33 env->tlb.cpu_openrisc_map_address_data = 34 &cpu_openrisc_get_phys_data; 35 } else { 36 env->tlb.cpu_openrisc_map_address_data = 37 &cpu_openrisc_get_phys_nommu; 38 } 39 40 if (env->sr & SR_IME) { 41 env->tlb.cpu_openrisc_map_address_code = 42 &cpu_openrisc_get_phys_code; 43 } else { 44 env->tlb.cpu_openrisc_map_address_code = 45 &cpu_openrisc_get_phys_nommu; 46 } 47 48 49 return 0; 50 } 51 52 static const VMStateDescription vmstate_tlb_entry = { 53 .name = "tlb_entry", 54 .version_id = 1, 55 .minimum_version_id = 1, 56 .minimum_version_id_old = 1, 57 .fields = (VMStateField[]) { 58 VMSTATE_UINTTL(mr, OpenRISCTLBEntry), 59 VMSTATE_UINTTL(tr, OpenRISCTLBEntry), 60 VMSTATE_END_OF_LIST() 61 } 62 }; 63 64 static const VMStateDescription vmstate_cpu_tlb = { 65 .name = "cpu_tlb", 66 .version_id = 1, 67 .minimum_version_id = 1, 68 .minimum_version_id_old = 1, 69 .fields = (VMStateField[]) { 70 VMSTATE_STRUCT_2DARRAY(itlb, CPUOpenRISCTLBContext, 71 ITLB_WAYS, ITLB_SIZE, 0, 72 vmstate_tlb_entry, OpenRISCTLBEntry), 73 VMSTATE_STRUCT_2DARRAY(dtlb, CPUOpenRISCTLBContext, 74 DTLB_WAYS, DTLB_SIZE, 0, 75 vmstate_tlb_entry, OpenRISCTLBEntry), 76 VMSTATE_END_OF_LIST() 77 } 78 }; 79 80 static int get_sr(QEMUFile *f, void *opaque, size_t size, VMStateField *field) 81 { 82 CPUOpenRISCState *env = opaque; 83 cpu_set_sr(env, qemu_get_be32(f)); 84 return 0; 85 } 86 87 static int put_sr(QEMUFile *f, void *opaque, size_t size, 88 VMStateField *field, QJSON *vmdesc) 89 { 90 CPUOpenRISCState *env = opaque; 91 qemu_put_be32(f, cpu_get_sr(env)); 92 return 0; 93 } 94 95 static const VMStateInfo vmstate_sr = { 96 .name = "sr", 97 .get = get_sr, 98 .put = put_sr, 99 }; 100 101 static const VMStateDescription vmstate_env = { 102 .name = "env", 103 .version_id = 6, 104 .minimum_version_id = 6, 105 .post_load = env_post_load, 106 .fields = (VMStateField[]) { 107 VMSTATE_UINTTL_2DARRAY(shadow_gpr, CPUOpenRISCState, 16, 32), 108 VMSTATE_UINTTL(pc, CPUOpenRISCState), 109 VMSTATE_UINTTL(ppc, CPUOpenRISCState), 110 VMSTATE_UINTTL(jmp_pc, CPUOpenRISCState), 111 VMSTATE_UINTTL(lock_addr, CPUOpenRISCState), 112 VMSTATE_UINTTL(lock_value, CPUOpenRISCState), 113 VMSTATE_UINTTL(epcr, CPUOpenRISCState), 114 VMSTATE_UINTTL(eear, CPUOpenRISCState), 115 116 /* Save the architecture value of the SR, not the internally 117 expanded version. Since this architecture value does not 118 exist in memory to be stored, this requires a but of hoop 119 jumping. We want OFFSET=0 so that we effectively pass ENV 120 to the helper functions, and we need to fill in the name by 121 hand since there's no field of that name. */ 122 { 123 .name = "sr", 124 .version_id = 0, 125 .size = sizeof(uint32_t), 126 .info = &vmstate_sr, 127 .flags = VMS_SINGLE, 128 .offset = 0 129 }, 130 131 VMSTATE_UINT32(vr, CPUOpenRISCState), 132 VMSTATE_UINT32(upr, CPUOpenRISCState), 133 VMSTATE_UINT32(cpucfgr, CPUOpenRISCState), 134 VMSTATE_UINT32(dmmucfgr, CPUOpenRISCState), 135 VMSTATE_UINT32(immucfgr, CPUOpenRISCState), 136 VMSTATE_UINT32(evbar, CPUOpenRISCState), 137 VMSTATE_UINT32(pmr, CPUOpenRISCState), 138 VMSTATE_UINT32(esr, CPUOpenRISCState), 139 VMSTATE_UINT32(fpcsr, CPUOpenRISCState), 140 VMSTATE_UINT64(mac, CPUOpenRISCState), 141 142 VMSTATE_STRUCT(tlb, CPUOpenRISCState, 1, 143 vmstate_cpu_tlb, CPUOpenRISCTLBContext), 144 145 VMSTATE_TIMER_PTR(timer, CPUOpenRISCState), 146 VMSTATE_UINT32(ttmr, CPUOpenRISCState), 147 148 VMSTATE_UINT32(picmr, CPUOpenRISCState), 149 VMSTATE_UINT32(picsr, CPUOpenRISCState), 150 151 VMSTATE_END_OF_LIST() 152 } 153 }; 154 155 const VMStateDescription vmstate_openrisc_cpu = { 156 .name = "cpu", 157 .version_id = 1, 158 .minimum_version_id = 1, 159 .fields = (VMStateField[]) { 160 VMSTATE_CPU(), 161 VMSTATE_STRUCT(env, OpenRISCCPU, 1, vmstate_env, CPUOpenRISCState), 162 VMSTATE_END_OF_LIST() 163 } 164 }; 165