aboutsummaryrefslogtreecommitdiff
path: root/target-arm/kvm.c
blob: 02435be33e5e9cf1b786da9dd6690de7ab41dafa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
 * ARM implementation of KVM hooks
 *
 * Copyright Christoffer Dall 2009-2010
 *
 * This work is licensed under the terms of the GNU GPL, version 2 or later.
 * See the COPYING file in the top-level directory.
 *
 */

#include <stdio.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/mman.h>

#include <linux/kvm.h>

#include "qemu-common.h"
#include "qemu-timer.h"
#include "sysemu.h"
#include "kvm.h"
#include "cpu.h"
#include "device_tree.h"
#include "hw/arm-misc.h"

const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
    KVM_CAP_LAST_INFO
};

int kvm_arch_init(KVMState *s)
{
    /* For ARM interrupt delivery is always asynchronous,
     * whether we are using an in-kernel VGIC or not.
     */
    kvm_async_interrupts_allowed = true;
    return 0;
}

int kvm_arch_init_vcpu(CPUARMState *env)
{
    struct kvm_vcpu_init init;

    init.target = KVM_ARM_TARGET_CORTEX_A15;
    memset(init.features, 0, sizeof(init.features));
    return kvm_vcpu_ioctl(env, KVM_ARM_VCPU_INIT, &init);
}

int kvm_arch_put_registers(CPUARMState *env, int level)
{
    struct kvm_regs regs;
    int mode, bn;
    int ret;

    ret = kvm_vcpu_ioctl(env, KVM_GET_REGS, &regs);
    if (ret < 0) {
        return ret;
    }

    /* We make sure the banked regs are properly set */
    mode = env->uncached_cpsr & CPSR_M;
    bn = bank_number(env, mode);
    if (mode == ARM_CPU_MODE_FIQ) {
        memcpy(env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
    } else {
        memcpy(env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
    }
    env->banked_r13[bn] = env->regs[13];
    env->banked_r14[bn] = env->regs[14];
    env->banked_spsr[bn] = env->spsr;

    /* Now we can safely copy stuff down to the kernel */
    memcpy(regs.regs0_7, env->regs, sizeof(uint32_t) * 8);
    memcpy(regs.usr_regs8_12, env->usr_regs, sizeof(uint32_t) * 5);
    memcpy(regs.fiq_regs8_12, env->fiq_regs, sizeof(uint32_t) * 5);
    regs.reg13[MODE_FIQ] = env->banked_r13[5];
    regs.reg13[MODE_IRQ] = env->banked_r13[4];
    regs.reg13[MODE_SVC] = env->banked_r13[1];
    regs.reg13[MODE_ABT] = env->banked_r13[2];
    regs.reg13[MODE_UND] = env->banked_r13[3];
    regs.reg13[MODE_USR] = env->banked_r13[0];
    regs.reg14[MODE_FIQ] = env->banked_r14[5];
    regs.reg14[MODE_IRQ] = env->banked_r14[4];
    regs.reg14[MODE_SVC] = env->banked_r14[1];
    regs.reg14[MODE_ABT] = env->banked_r14[2];
    regs.reg14[MODE_UND] = env->banked_r14[3];
    regs.reg14[MODE_USR] = env->banked_r14[0];
    regs.reg15 = env->regs[15];
    regs.cpsr = cpsr_read(env);
    regs.spsr[MODE_FIQ] = env->banked_spsr[5];
    regs.spsr[MODE_IRQ] = env->banked_spsr[4];
    regs.spsr[MODE_SVC] = env->banked_spsr[1];
    regs.spsr[MODE_ABT] = env->banked_spsr[2];
    regs.spsr[MODE_UND] = env->banked_spsr[3];

    regs.cp15.c0_midr = env->cp15.c0_cpuid;
    regs.cp15.c1_sys = env->cp15.c1_sys;

    ret = kvm_vcpu_ioctl(env, KVM_SET_REGS, &regs);

    return ret;
}

int kvm_arch_get_registers(CPUARMState *env)
{
    struct kvm_regs regs;
    int mode, bn;
    int32_t ret;

    ret = kvm_vcpu_ioctl(env, KVM_GET_REGS, &regs);
    if (ret < 0) {
        return ret;
    }

    /* First, let's transfer the banked state */
    cpsr_write(env, regs.cpsr, 0xFFFFFFFF);
    memcpy(env->regs, regs.regs0_7, sizeof(uint32_t) * 8);
    memcpy(env->usr_regs, regs.usr_regs8_12, sizeof(uint32_t) * 5);
    memcpy(env->fiq_regs, regs.fiq_regs8_12, sizeof(uint32_t) * 5);

    env->banked_r13[5] = regs.reg13[MODE_FIQ];
    env->banked_r13[4] = regs.reg13[MODE_IRQ];
    env->banked_r13[1] = regs.reg13[MODE_SVC];
    env->banked_r13[2] = regs.reg13[MODE_ABT];
    env->banked_r13[3] = regs.reg13[MODE_UND];
    env->banked_r13[0] = regs.reg13[MODE_USR];
    env->banked_r14[5] = regs.reg14[MODE_FIQ];
    env->banked_r14[4] = regs.reg14[MODE_IRQ];
    env->banked_r14[1] = regs.reg14[MODE_SVC];
    env->banked_r14[2] = regs.reg14[MODE_ABT];
    env->banked_r14[3] = regs.reg14[MODE_UND];
    env->banked_r14[0] = regs.reg14[MODE_USR];
    env->regs[15] = regs.reg15;
    env->banked_spsr[5] = regs.spsr[MODE_FIQ];
    env->banked_spsr[4] = regs.spsr[MODE_IRQ];
    env->banked_spsr[1] = regs.spsr[MODE_SVC];
    env->banked_spsr[2] = regs.spsr[MODE_ABT];
    env->banked_spsr[3] = regs.spsr[MODE_UND];

    /* We make sure the current mode regs are properly set */
    mode = env->uncached_cpsr & CPSR_M;
    bn = bank_number(env, mode);
    if (mode == ARM_CPU_MODE_FIQ) {
        memcpy(env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
    } else {
        memcpy(env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
    }
    env->regs[13] = env->banked_r13[bn];
    env->regs[14] = env->banked_r14[bn];
    env->spsr = env->banked_spsr[bn];

    env->cp15.c1_sys = regs.cp15.c1_sys;
    env->cp15.c2_base0 = regs.cp15.c2_base0;
    env->cp15.c2_base1 = regs.cp15.c2_base1;

    /* This is ugly, but necessary for GDB compatibility
     * TODO: do this via an access function.
     */
    env->cp15.c2_control = regs.cp15.c2_control;
    env->cp15.c2_mask = ~(((uint32_t)0xffffffffu) >> regs.cp15.c2_control);
    env->cp15.c2_base_mask = ~((uint32_t)0x3fffu >> regs.cp15.c2_control);

    env->cp15.c3 = regs.cp15.c3_dacr;

    return 0;
}

void kvm_arch_pre_run(CPUARMState *env, struct kvm_run *run)
{
}

void kvm_arch_post_run(CPUARMState *env, struct kvm_run *run)
{
}

int kvm_arch_handle_exit(CPUARMState *env, struct kvm_run *run)
{
    int ret = 0;

    return ret;
}

void kvm_arch_reset_vcpu(CPUARMState *env)
{
}

bool kvm_arch_stop_on_emulation_error(CPUARMState *env)
{
    return true;
}

int kvm_arch_process_async_events(CPUARMState *env)
{
    return 0;
}

int kvm_arch_on_sigbus_vcpu(CPUARMState *env, int code, void *addr)
{
    return 1;
}

int kvm_arch_on_sigbus(int code, void *addr)
{
    return 1;
}

void kvm_arch_update_guest_debug(CPUARMState *env, struct kvm_guest_debug *dbg)
{
    fprintf(stderr, "%s: not implemented\n", __func__);
}

int kvm_arch_insert_sw_breakpoint(CPUARMState *env,
                                  struct kvm_sw_breakpoint *bp)
{
    fprintf(stderr, "%s: not implemented\n", __func__);
    return -EINVAL;
}

int kvm_arch_insert_hw_breakpoint(target_ulong addr,
                                  target_ulong len, int type)
{
    fprintf(stderr, "%s: not implemented\n", __func__);
    return -EINVAL;
}

int kvm_arch_remove_hw_breakpoint(target_ulong addr,
                                  target_ulong len, int type)
{
    fprintf(stderr, "%s: not implemented\n", __func__);
    return -EINVAL;
}

int kvm_arch_remove_sw_breakpoint(CPUARMState *env,
                                  struct kvm_sw_breakpoint *bp)
{
    fprintf(stderr, "%s: not implemented\n", __func__);
    return -EINVAL;
}

void kvm_arch_remove_all_hw_breakpoints(void)
{
    fprintf(stderr, "%s: not implemented\n", __func__);
}