Statistics
| Branch: | Revision:

root / hw / spapr_hcall.c @ 39ac8455

History | View | Annotate | Download (9.7 kB)

1 9fdf0c29 David Gibson
#include "sysemu.h"
2 9fdf0c29 David Gibson
#include "cpu.h"
3 9fdf0c29 David Gibson
#include "qemu-char.h"
4 f43e3525 David Gibson
#include "sysemu.h"
5 f43e3525 David Gibson
#include "qemu-char.h"
6 f43e3525 David Gibson
#include "exec-all.h"
7 9fdf0c29 David Gibson
#include "hw/spapr.h"
8 9fdf0c29 David Gibson
9 f43e3525 David Gibson
#define HPTES_PER_GROUP 8
10 f43e3525 David Gibson
11 f43e3525 David Gibson
#define HPTE_V_SSIZE_SHIFT      62
12 f43e3525 David Gibson
#define HPTE_V_AVPN_SHIFT       7
13 f43e3525 David Gibson
#define HPTE_V_AVPN             0x3fffffffffffff80ULL
14 f43e3525 David Gibson
#define HPTE_V_AVPN_VAL(x)      (((x) & HPTE_V_AVPN) >> HPTE_V_AVPN_SHIFT)
15 f43e3525 David Gibson
#define HPTE_V_COMPARE(x, y)    (!(((x) ^ (y)) & 0xffffffffffffff80UL))
16 f43e3525 David Gibson
#define HPTE_V_BOLTED           0x0000000000000010ULL
17 f43e3525 David Gibson
#define HPTE_V_LOCK             0x0000000000000008ULL
18 f43e3525 David Gibson
#define HPTE_V_LARGE            0x0000000000000004ULL
19 f43e3525 David Gibson
#define HPTE_V_SECONDARY        0x0000000000000002ULL
20 f43e3525 David Gibson
#define HPTE_V_VALID            0x0000000000000001ULL
21 f43e3525 David Gibson
22 f43e3525 David Gibson
#define HPTE_R_PP0              0x8000000000000000ULL
23 f43e3525 David Gibson
#define HPTE_R_TS               0x4000000000000000ULL
24 f43e3525 David Gibson
#define HPTE_R_KEY_HI           0x3000000000000000ULL
25 f43e3525 David Gibson
#define HPTE_R_RPN_SHIFT        12
26 f43e3525 David Gibson
#define HPTE_R_RPN              0x3ffffffffffff000ULL
27 f43e3525 David Gibson
#define HPTE_R_FLAGS            0x00000000000003ffULL
28 f43e3525 David Gibson
#define HPTE_R_PP               0x0000000000000003ULL
29 f43e3525 David Gibson
#define HPTE_R_N                0x0000000000000004ULL
30 f43e3525 David Gibson
#define HPTE_R_G                0x0000000000000008ULL
31 f43e3525 David Gibson
#define HPTE_R_M                0x0000000000000010ULL
32 f43e3525 David Gibson
#define HPTE_R_I                0x0000000000000020ULL
33 f43e3525 David Gibson
#define HPTE_R_W                0x0000000000000040ULL
34 f43e3525 David Gibson
#define HPTE_R_WIMG             0x0000000000000078ULL
35 f43e3525 David Gibson
#define HPTE_R_C                0x0000000000000080ULL
36 f43e3525 David Gibson
#define HPTE_R_R                0x0000000000000100ULL
37 f43e3525 David Gibson
#define HPTE_R_KEY_LO           0x0000000000000e00ULL
38 f43e3525 David Gibson
39 f43e3525 David Gibson
#define HPTE_V_1TB_SEG          0x4000000000000000ULL
40 f43e3525 David Gibson
#define HPTE_V_VRMA_MASK        0x4001ffffff000000ULL
41 f43e3525 David Gibson
42 f43e3525 David Gibson
#define HPTE_V_HVLOCK           0x40ULL
43 f43e3525 David Gibson
44 f43e3525 David Gibson
static inline int lock_hpte(void *hpte, target_ulong bits)
45 f43e3525 David Gibson
{
46 f43e3525 David Gibson
    uint64_t pteh;
47 f43e3525 David Gibson
48 f43e3525 David Gibson
    pteh = ldq_p(hpte);
49 f43e3525 David Gibson
50 f43e3525 David Gibson
    /* We're protected by qemu's global lock here */
51 f43e3525 David Gibson
    if (pteh & bits) {
52 f43e3525 David Gibson
        return 0;
53 f43e3525 David Gibson
    }
54 f43e3525 David Gibson
    stq_p(hpte, pteh | HPTE_V_HVLOCK);
55 f43e3525 David Gibson
    return 1;
56 f43e3525 David Gibson
}
57 f43e3525 David Gibson
58 f43e3525 David Gibson
static target_ulong compute_tlbie_rb(target_ulong v, target_ulong r,
59 f43e3525 David Gibson
                                     target_ulong pte_index)
60 f43e3525 David Gibson
{
61 f43e3525 David Gibson
    target_ulong rb, va_low;
62 f43e3525 David Gibson
63 f43e3525 David Gibson
    rb = (v & ~0x7fULL) << 16; /* AVA field */
64 f43e3525 David Gibson
    va_low = pte_index >> 3;
65 f43e3525 David Gibson
    if (v & HPTE_V_SECONDARY) {
66 f43e3525 David Gibson
        va_low = ~va_low;
67 f43e3525 David Gibson
    }
68 f43e3525 David Gibson
    /* xor vsid from AVA */
69 f43e3525 David Gibson
    if (!(v & HPTE_V_1TB_SEG)) {
70 f43e3525 David Gibson
        va_low ^= v >> 12;
71 f43e3525 David Gibson
    } else {
72 f43e3525 David Gibson
        va_low ^= v >> 24;
73 f43e3525 David Gibson
    }
74 f43e3525 David Gibson
    va_low &= 0x7ff;
75 f43e3525 David Gibson
    if (v & HPTE_V_LARGE) {
76 f43e3525 David Gibson
        rb |= 1;                         /* L field */
77 f43e3525 David Gibson
#if 0 /* Disable that P7 specific bit for now */
78 f43e3525 David Gibson
        if (r & 0xff000) {
79 f43e3525 David Gibson
            /* non-16MB large page, must be 64k */
80 f43e3525 David Gibson
            /* (masks depend on page size) */
81 f43e3525 David Gibson
            rb |= 0x1000;                /* page encoding in LP field */
82 f43e3525 David Gibson
            rb |= (va_low & 0x7f) << 16; /* 7b of VA in AVA/LP field */
83 f43e3525 David Gibson
            rb |= (va_low & 0xfe);       /* AVAL field */
84 f43e3525 David Gibson
        }
85 f43e3525 David Gibson
#endif
86 f43e3525 David Gibson
    } else {
87 f43e3525 David Gibson
        /* 4kB page */
88 f43e3525 David Gibson
        rb |= (va_low & 0x7ff) << 12;   /* remaining 11b of AVA */
89 f43e3525 David Gibson
    }
90 f43e3525 David Gibson
    rb |= (v >> 54) & 0x300;            /* B field */
91 f43e3525 David Gibson
    return rb;
92 f43e3525 David Gibson
}
93 f43e3525 David Gibson
94 f43e3525 David Gibson
static target_ulong h_enter(CPUState *env, sPAPREnvironment *spapr,
95 f43e3525 David Gibson
                            target_ulong opcode, target_ulong *args)
96 f43e3525 David Gibson
{
97 f43e3525 David Gibson
    target_ulong flags = args[0];
98 f43e3525 David Gibson
    target_ulong pte_index = args[1];
99 f43e3525 David Gibson
    target_ulong pteh = args[2];
100 f43e3525 David Gibson
    target_ulong ptel = args[3];
101 f43e3525 David Gibson
    target_ulong porder;
102 f43e3525 David Gibson
    target_ulong i, pa;
103 f43e3525 David Gibson
    uint8_t *hpte;
104 f43e3525 David Gibson
105 f43e3525 David Gibson
    /* only handle 4k and 16M pages for now */
106 f43e3525 David Gibson
    porder = 12;
107 f43e3525 David Gibson
    if (pteh & HPTE_V_LARGE) {
108 f43e3525 David Gibson
#if 0 /* We don't support 64k pages yet */
109 f43e3525 David Gibson
        if ((ptel & 0xf000) == 0x1000) {
110 f43e3525 David Gibson
            /* 64k page */
111 f43e3525 David Gibson
            porder = 16;
112 f43e3525 David Gibson
        } else
113 f43e3525 David Gibson
#endif
114 f43e3525 David Gibson
        if ((ptel & 0xff000) == 0) {
115 f43e3525 David Gibson
            /* 16M page */
116 f43e3525 David Gibson
            porder = 24;
117 f43e3525 David Gibson
            /* lowest AVA bit must be 0 for 16M pages */
118 f43e3525 David Gibson
            if (pteh & 0x80) {
119 f43e3525 David Gibson
                return H_PARAMETER;
120 f43e3525 David Gibson
            }
121 f43e3525 David Gibson
        } else {
122 f43e3525 David Gibson
            return H_PARAMETER;
123 f43e3525 David Gibson
        }
124 f43e3525 David Gibson
    }
125 f43e3525 David Gibson
126 f43e3525 David Gibson
    pa = ptel & HPTE_R_RPN;
127 f43e3525 David Gibson
    /* FIXME: bounds check the pa? */
128 f43e3525 David Gibson
129 f43e3525 David Gibson
    /* Check WIMG */
130 f43e3525 David Gibson
    if ((ptel & HPTE_R_WIMG) != HPTE_R_M) {
131 f43e3525 David Gibson
        return H_PARAMETER;
132 f43e3525 David Gibson
    }
133 f43e3525 David Gibson
    pteh &= ~0x60ULL;
134 f43e3525 David Gibson
135 f43e3525 David Gibson
    if ((pte_index * HASH_PTE_SIZE_64) & ~env->htab_mask) {
136 f43e3525 David Gibson
        return H_PARAMETER;
137 f43e3525 David Gibson
    }
138 f43e3525 David Gibson
    if (likely((flags & H_EXACT) == 0)) {
139 f43e3525 David Gibson
        pte_index &= ~7ULL;
140 f43e3525 David Gibson
        hpte = env->external_htab + (pte_index * HASH_PTE_SIZE_64);
141 f43e3525 David Gibson
        for (i = 0; ; ++i) {
142 f43e3525 David Gibson
            if (i == 8) {
143 f43e3525 David Gibson
                return H_PTEG_FULL;
144 f43e3525 David Gibson
            }
145 f43e3525 David Gibson
            if (((ldq_p(hpte) & HPTE_V_VALID) == 0) &&
146 f43e3525 David Gibson
                lock_hpte(hpte, HPTE_V_HVLOCK | HPTE_V_VALID)) {
147 f43e3525 David Gibson
                break;
148 f43e3525 David Gibson
            }
149 f43e3525 David Gibson
            hpte += HASH_PTE_SIZE_64;
150 f43e3525 David Gibson
        }
151 f43e3525 David Gibson
    } else {
152 f43e3525 David Gibson
        i = 0;
153 f43e3525 David Gibson
        hpte = env->external_htab + (pte_index * HASH_PTE_SIZE_64);
154 f43e3525 David Gibson
        if (!lock_hpte(hpte, HPTE_V_HVLOCK | HPTE_V_VALID)) {
155 f43e3525 David Gibson
            return H_PTEG_FULL;
156 f43e3525 David Gibson
        }
157 f43e3525 David Gibson
    }
158 f43e3525 David Gibson
    stq_p(hpte + (HASH_PTE_SIZE_64/2), ptel);
159 f43e3525 David Gibson
    /* eieio();  FIXME: need some sort of barrier for smp? */
160 f43e3525 David Gibson
    stq_p(hpte, pteh);
161 f43e3525 David Gibson
162 f43e3525 David Gibson
    assert(!(ldq_p(hpte) & HPTE_V_HVLOCK));
163 f43e3525 David Gibson
    args[0] = pte_index + i;
164 f43e3525 David Gibson
    return H_SUCCESS;
165 f43e3525 David Gibson
}
166 f43e3525 David Gibson
167 f43e3525 David Gibson
static target_ulong h_remove(CPUState *env, sPAPREnvironment *spapr,
168 f43e3525 David Gibson
                             target_ulong opcode, target_ulong *args)
169 f43e3525 David Gibson
{
170 f43e3525 David Gibson
    target_ulong flags = args[0];
171 f43e3525 David Gibson
    target_ulong pte_index = args[1];
172 f43e3525 David Gibson
    target_ulong avpn = args[2];
173 f43e3525 David Gibson
    uint8_t *hpte;
174 f43e3525 David Gibson
    target_ulong v, r, rb;
175 f43e3525 David Gibson
176 f43e3525 David Gibson
    if ((pte_index * HASH_PTE_SIZE_64) & ~env->htab_mask) {
177 f43e3525 David Gibson
        return H_PARAMETER;
178 f43e3525 David Gibson
    }
179 f43e3525 David Gibson
180 f43e3525 David Gibson
    hpte = env->external_htab + (pte_index * HASH_PTE_SIZE_64);
181 f43e3525 David Gibson
    while (!lock_hpte(hpte, HPTE_V_HVLOCK)) {
182 f43e3525 David Gibson
        /* We have no real concurrency in qemu soft-emulation, so we
183 f43e3525 David Gibson
         * will never actually have a contested lock */
184 f43e3525 David Gibson
        assert(0);
185 f43e3525 David Gibson
    }
186 f43e3525 David Gibson
187 f43e3525 David Gibson
    v = ldq_p(hpte);
188 f43e3525 David Gibson
    r = ldq_p(hpte + (HASH_PTE_SIZE_64/2));
189 f43e3525 David Gibson
190 f43e3525 David Gibson
    if ((v & HPTE_V_VALID) == 0 ||
191 f43e3525 David Gibson
        ((flags & H_AVPN) && (v & ~0x7fULL) != avpn) ||
192 f43e3525 David Gibson
        ((flags & H_ANDCOND) && (v & avpn) != 0)) {
193 f43e3525 David Gibson
        stq_p(hpte, v & ~HPTE_V_HVLOCK);
194 f43e3525 David Gibson
        assert(!(ldq_p(hpte) & HPTE_V_HVLOCK));
195 f43e3525 David Gibson
        return H_NOT_FOUND;
196 f43e3525 David Gibson
    }
197 f43e3525 David Gibson
    args[0] = v & ~HPTE_V_HVLOCK;
198 f43e3525 David Gibson
    args[1] = r;
199 f43e3525 David Gibson
    stq_p(hpte, 0);
200 f43e3525 David Gibson
    rb = compute_tlbie_rb(v, r, pte_index);
201 f43e3525 David Gibson
    ppc_tlb_invalidate_one(env, rb);
202 f43e3525 David Gibson
    assert(!(ldq_p(hpte) & HPTE_V_HVLOCK));
203 f43e3525 David Gibson
    return H_SUCCESS;
204 f43e3525 David Gibson
}
205 f43e3525 David Gibson
206 f43e3525 David Gibson
static target_ulong h_protect(CPUState *env, sPAPREnvironment *spapr,
207 f43e3525 David Gibson
                              target_ulong opcode, target_ulong *args)
208 f43e3525 David Gibson
{
209 f43e3525 David Gibson
    target_ulong flags = args[0];
210 f43e3525 David Gibson
    target_ulong pte_index = args[1];
211 f43e3525 David Gibson
    target_ulong avpn = args[2];
212 f43e3525 David Gibson
    uint8_t *hpte;
213 f43e3525 David Gibson
    target_ulong v, r, rb;
214 f43e3525 David Gibson
215 f43e3525 David Gibson
    if ((pte_index * HASH_PTE_SIZE_64) & ~env->htab_mask) {
216 f43e3525 David Gibson
        return H_PARAMETER;
217 f43e3525 David Gibson
    }
218 f43e3525 David Gibson
219 f43e3525 David Gibson
    hpte = env->external_htab + (pte_index * HASH_PTE_SIZE_64);
220 f43e3525 David Gibson
    while (!lock_hpte(hpte, HPTE_V_HVLOCK)) {
221 f43e3525 David Gibson
        /* We have no real concurrency in qemu soft-emulation, so we
222 f43e3525 David Gibson
         * will never actually have a contested lock */
223 f43e3525 David Gibson
        assert(0);
224 f43e3525 David Gibson
    }
225 f43e3525 David Gibson
226 f43e3525 David Gibson
    v = ldq_p(hpte);
227 f43e3525 David Gibson
    r = ldq_p(hpte + (HASH_PTE_SIZE_64/2));
228 f43e3525 David Gibson
229 f43e3525 David Gibson
    if ((v & HPTE_V_VALID) == 0 ||
230 f43e3525 David Gibson
        ((flags & H_AVPN) && (v & ~0x7fULL) != avpn)) {
231 f43e3525 David Gibson
        stq_p(hpte, v & ~HPTE_V_HVLOCK);
232 f43e3525 David Gibson
        assert(!(ldq_p(hpte) & HPTE_V_HVLOCK));
233 f43e3525 David Gibson
        return H_NOT_FOUND;
234 f43e3525 David Gibson
    }
235 f43e3525 David Gibson
236 f43e3525 David Gibson
    r &= ~(HPTE_R_PP0 | HPTE_R_PP | HPTE_R_N |
237 f43e3525 David Gibson
           HPTE_R_KEY_HI | HPTE_R_KEY_LO);
238 f43e3525 David Gibson
    r |= (flags << 55) & HPTE_R_PP0;
239 f43e3525 David Gibson
    r |= (flags << 48) & HPTE_R_KEY_HI;
240 f43e3525 David Gibson
    r |= flags & (HPTE_R_PP | HPTE_R_N | HPTE_R_KEY_LO);
241 f43e3525 David Gibson
    rb = compute_tlbie_rb(v, r, pte_index);
242 f43e3525 David Gibson
    stq_p(hpte, v & ~HPTE_V_VALID);
243 f43e3525 David Gibson
    ppc_tlb_invalidate_one(env, rb);
244 f43e3525 David Gibson
    stq_p(hpte + (HASH_PTE_SIZE_64/2), r);
245 f43e3525 David Gibson
    /* Don't need a memory barrier, due to qemu's global lock */
246 f43e3525 David Gibson
    stq_p(hpte, v & ~HPTE_V_HVLOCK);
247 f43e3525 David Gibson
    assert(!(ldq_p(hpte) & HPTE_V_HVLOCK));
248 f43e3525 David Gibson
    return H_SUCCESS;
249 f43e3525 David Gibson
}
250 f43e3525 David Gibson
251 39ac8455 David Gibson
static target_ulong h_rtas(CPUState *env, sPAPREnvironment *spapr,
252 39ac8455 David Gibson
                           target_ulong opcode, target_ulong *args)
253 39ac8455 David Gibson
{
254 39ac8455 David Gibson
    target_ulong rtas_r3 = args[0];
255 39ac8455 David Gibson
    uint32_t token = ldl_phys(rtas_r3);
256 39ac8455 David Gibson
    uint32_t nargs = ldl_phys(rtas_r3 + 4);
257 39ac8455 David Gibson
    uint32_t nret = ldl_phys(rtas_r3 + 8);
258 39ac8455 David Gibson
259 39ac8455 David Gibson
    return spapr_rtas_call(spapr, token, nargs, rtas_r3 + 12,
260 39ac8455 David Gibson
                           nret, rtas_r3 + 12 + 4*nargs);
261 39ac8455 David Gibson
}
262 39ac8455 David Gibson
263 39ac8455 David Gibson
spapr_hcall_fn papr_hypercall_table[(MAX_HCALL_OPCODE / 4) + 1];
264 39ac8455 David Gibson
spapr_hcall_fn kvmppc_hypercall_table[KVMPPC_HCALL_MAX - KVMPPC_HCALL_BASE];
265 9fdf0c29 David Gibson
266 9fdf0c29 David Gibson
void spapr_register_hypercall(target_ulong opcode, spapr_hcall_fn fn)
267 9fdf0c29 David Gibson
{
268 39ac8455 David Gibson
    spapr_hcall_fn *slot;
269 39ac8455 David Gibson
270 39ac8455 David Gibson
    if (opcode <= MAX_HCALL_OPCODE) {
271 39ac8455 David Gibson
        assert((opcode & 0x3) == 0);
272 9fdf0c29 David Gibson
273 39ac8455 David Gibson
        slot = &papr_hypercall_table[opcode / 4];
274 39ac8455 David Gibson
    } else {
275 39ac8455 David Gibson
        assert((opcode >= KVMPPC_HCALL_BASE) && (opcode <= KVMPPC_HCALL_MAX));
276 9fdf0c29 David Gibson
277 9fdf0c29 David Gibson
278 39ac8455 David Gibson
        slot = &kvmppc_hypercall_table[opcode - KVMPPC_HCALL_BASE];
279 39ac8455 David Gibson
    }
280 9fdf0c29 David Gibson
281 39ac8455 David Gibson
    assert(!(*slot) || (fn == *slot));
282 39ac8455 David Gibson
    *slot = fn;
283 9fdf0c29 David Gibson
}
284 9fdf0c29 David Gibson
285 9fdf0c29 David Gibson
target_ulong spapr_hypercall(CPUState *env, target_ulong opcode,
286 9fdf0c29 David Gibson
                             target_ulong *args)
287 9fdf0c29 David Gibson
{
288 9fdf0c29 David Gibson
    if (msr_pr) {
289 9fdf0c29 David Gibson
        hcall_dprintf("Hypercall made with MSR[PR]=1\n");
290 9fdf0c29 David Gibson
        return H_PRIVILEGE;
291 9fdf0c29 David Gibson
    }
292 9fdf0c29 David Gibson
293 9fdf0c29 David Gibson
    if ((opcode <= MAX_HCALL_OPCODE)
294 9fdf0c29 David Gibson
        && ((opcode & 0x3) == 0)) {
295 39ac8455 David Gibson
        spapr_hcall_fn fn = papr_hypercall_table[opcode / 4];
296 39ac8455 David Gibson
297 39ac8455 David Gibson
        if (fn) {
298 39ac8455 David Gibson
            return fn(env, spapr, opcode, args);
299 39ac8455 David Gibson
        }
300 39ac8455 David Gibson
    } else if ((opcode >= KVMPPC_HCALL_BASE) &&
301 39ac8455 David Gibson
               (opcode <= KVMPPC_HCALL_MAX)) {
302 39ac8455 David Gibson
        spapr_hcall_fn fn = kvmppc_hypercall_table[opcode - KVMPPC_HCALL_BASE];
303 9fdf0c29 David Gibson
304 9fdf0c29 David Gibson
        if (fn) {
305 9fdf0c29 David Gibson
            return fn(env, spapr, opcode, args);
306 9fdf0c29 David Gibson
        }
307 9fdf0c29 David Gibson
    }
308 9fdf0c29 David Gibson
309 9fdf0c29 David Gibson
    hcall_dprintf("Unimplemented hcall 0x" TARGET_FMT_lx "\n", opcode);
310 9fdf0c29 David Gibson
    return H_FUNCTION;
311 9fdf0c29 David Gibson
}
312 f43e3525 David Gibson
313 f43e3525 David Gibson
static void hypercall_init(void)
314 f43e3525 David Gibson
{
315 f43e3525 David Gibson
    /* hcall-pft */
316 f43e3525 David Gibson
    spapr_register_hypercall(H_ENTER, h_enter);
317 f43e3525 David Gibson
    spapr_register_hypercall(H_REMOVE, h_remove);
318 f43e3525 David Gibson
    spapr_register_hypercall(H_PROTECT, h_protect);
319 39ac8455 David Gibson
320 39ac8455 David Gibson
    /* qemu/KVM-PPC specific hcalls */
321 39ac8455 David Gibson
    spapr_register_hypercall(KVMPPC_H_RTAS, h_rtas);
322 f43e3525 David Gibson
}
323 f43e3525 David Gibson
device_init(hypercall_init);