Statistics
| Branch: | Revision:

root / hw / spapr_hcall.c @ 92f9a4f1

History | View | Annotate | Download (14.2 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 ed120055 David Gibson
#include "exec.h"
8 ed120055 David Gibson
#include "helper_regs.h"
9 9fdf0c29 David Gibson
#include "hw/spapr.h"
10 9fdf0c29 David Gibson
11 f43e3525 David Gibson
#define HPTES_PER_GROUP 8
12 f43e3525 David Gibson
13 f43e3525 David Gibson
#define HPTE_V_SSIZE_SHIFT      62
14 f43e3525 David Gibson
#define HPTE_V_AVPN_SHIFT       7
15 f43e3525 David Gibson
#define HPTE_V_AVPN             0x3fffffffffffff80ULL
16 f43e3525 David Gibson
#define HPTE_V_AVPN_VAL(x)      (((x) & HPTE_V_AVPN) >> HPTE_V_AVPN_SHIFT)
17 f43e3525 David Gibson
#define HPTE_V_COMPARE(x, y)    (!(((x) ^ (y)) & 0xffffffffffffff80UL))
18 f43e3525 David Gibson
#define HPTE_V_BOLTED           0x0000000000000010ULL
19 f43e3525 David Gibson
#define HPTE_V_LOCK             0x0000000000000008ULL
20 f43e3525 David Gibson
#define HPTE_V_LARGE            0x0000000000000004ULL
21 f43e3525 David Gibson
#define HPTE_V_SECONDARY        0x0000000000000002ULL
22 f43e3525 David Gibson
#define HPTE_V_VALID            0x0000000000000001ULL
23 f43e3525 David Gibson
24 f43e3525 David Gibson
#define HPTE_R_PP0              0x8000000000000000ULL
25 f43e3525 David Gibson
#define HPTE_R_TS               0x4000000000000000ULL
26 f43e3525 David Gibson
#define HPTE_R_KEY_HI           0x3000000000000000ULL
27 f43e3525 David Gibson
#define HPTE_R_RPN_SHIFT        12
28 f43e3525 David Gibson
#define HPTE_R_RPN              0x3ffffffffffff000ULL
29 f43e3525 David Gibson
#define HPTE_R_FLAGS            0x00000000000003ffULL
30 f43e3525 David Gibson
#define HPTE_R_PP               0x0000000000000003ULL
31 f43e3525 David Gibson
#define HPTE_R_N                0x0000000000000004ULL
32 f43e3525 David Gibson
#define HPTE_R_G                0x0000000000000008ULL
33 f43e3525 David Gibson
#define HPTE_R_M                0x0000000000000010ULL
34 f43e3525 David Gibson
#define HPTE_R_I                0x0000000000000020ULL
35 f43e3525 David Gibson
#define HPTE_R_W                0x0000000000000040ULL
36 f43e3525 David Gibson
#define HPTE_R_WIMG             0x0000000000000078ULL
37 f43e3525 David Gibson
#define HPTE_R_C                0x0000000000000080ULL
38 f43e3525 David Gibson
#define HPTE_R_R                0x0000000000000100ULL
39 f43e3525 David Gibson
#define HPTE_R_KEY_LO           0x0000000000000e00ULL
40 f43e3525 David Gibson
41 f43e3525 David Gibson
#define HPTE_V_1TB_SEG          0x4000000000000000ULL
42 f43e3525 David Gibson
#define HPTE_V_VRMA_MASK        0x4001ffffff000000ULL
43 f43e3525 David Gibson
44 f43e3525 David Gibson
#define HPTE_V_HVLOCK           0x40ULL
45 f43e3525 David Gibson
46 f43e3525 David Gibson
static inline int lock_hpte(void *hpte, target_ulong bits)
47 f43e3525 David Gibson
{
48 f43e3525 David Gibson
    uint64_t pteh;
49 f43e3525 David Gibson
50 f43e3525 David Gibson
    pteh = ldq_p(hpte);
51 f43e3525 David Gibson
52 f43e3525 David Gibson
    /* We're protected by qemu's global lock here */
53 f43e3525 David Gibson
    if (pteh & bits) {
54 f43e3525 David Gibson
        return 0;
55 f43e3525 David Gibson
    }
56 f43e3525 David Gibson
    stq_p(hpte, pteh | HPTE_V_HVLOCK);
57 f43e3525 David Gibson
    return 1;
58 f43e3525 David Gibson
}
59 f43e3525 David Gibson
60 f43e3525 David Gibson
static target_ulong compute_tlbie_rb(target_ulong v, target_ulong r,
61 f43e3525 David Gibson
                                     target_ulong pte_index)
62 f43e3525 David Gibson
{
63 f43e3525 David Gibson
    target_ulong rb, va_low;
64 f43e3525 David Gibson
65 f43e3525 David Gibson
    rb = (v & ~0x7fULL) << 16; /* AVA field */
66 f43e3525 David Gibson
    va_low = pte_index >> 3;
67 f43e3525 David Gibson
    if (v & HPTE_V_SECONDARY) {
68 f43e3525 David Gibson
        va_low = ~va_low;
69 f43e3525 David Gibson
    }
70 f43e3525 David Gibson
    /* xor vsid from AVA */
71 f43e3525 David Gibson
    if (!(v & HPTE_V_1TB_SEG)) {
72 f43e3525 David Gibson
        va_low ^= v >> 12;
73 f43e3525 David Gibson
    } else {
74 f43e3525 David Gibson
        va_low ^= v >> 24;
75 f43e3525 David Gibson
    }
76 f43e3525 David Gibson
    va_low &= 0x7ff;
77 f43e3525 David Gibson
    if (v & HPTE_V_LARGE) {
78 f43e3525 David Gibson
        rb |= 1;                         /* L field */
79 f43e3525 David Gibson
#if 0 /* Disable that P7 specific bit for now */
80 f43e3525 David Gibson
        if (r & 0xff000) {
81 f43e3525 David Gibson
            /* non-16MB large page, must be 64k */
82 f43e3525 David Gibson
            /* (masks depend on page size) */
83 f43e3525 David Gibson
            rb |= 0x1000;                /* page encoding in LP field */
84 f43e3525 David Gibson
            rb |= (va_low & 0x7f) << 16; /* 7b of VA in AVA/LP field */
85 f43e3525 David Gibson
            rb |= (va_low & 0xfe);       /* AVAL field */
86 f43e3525 David Gibson
        }
87 f43e3525 David Gibson
#endif
88 f43e3525 David Gibson
    } else {
89 f43e3525 David Gibson
        /* 4kB page */
90 f43e3525 David Gibson
        rb |= (va_low & 0x7ff) << 12;   /* remaining 11b of AVA */
91 f43e3525 David Gibson
    }
92 f43e3525 David Gibson
    rb |= (v >> 54) & 0x300;            /* B field */
93 f43e3525 David Gibson
    return rb;
94 f43e3525 David Gibson
}
95 f43e3525 David Gibson
96 f43e3525 David Gibson
static target_ulong h_enter(CPUState *env, sPAPREnvironment *spapr,
97 f43e3525 David Gibson
                            target_ulong opcode, target_ulong *args)
98 f43e3525 David Gibson
{
99 f43e3525 David Gibson
    target_ulong flags = args[0];
100 f43e3525 David Gibson
    target_ulong pte_index = args[1];
101 f43e3525 David Gibson
    target_ulong pteh = args[2];
102 f43e3525 David Gibson
    target_ulong ptel = args[3];
103 f43e3525 David Gibson
    target_ulong porder;
104 f43e3525 David Gibson
    target_ulong i, pa;
105 f43e3525 David Gibson
    uint8_t *hpte;
106 f43e3525 David Gibson
107 f43e3525 David Gibson
    /* only handle 4k and 16M pages for now */
108 f43e3525 David Gibson
    porder = 12;
109 f43e3525 David Gibson
    if (pteh & HPTE_V_LARGE) {
110 f43e3525 David Gibson
#if 0 /* We don't support 64k pages yet */
111 f43e3525 David Gibson
        if ((ptel & 0xf000) == 0x1000) {
112 f43e3525 David Gibson
            /* 64k page */
113 f43e3525 David Gibson
            porder = 16;
114 f43e3525 David Gibson
        } else
115 f43e3525 David Gibson
#endif
116 f43e3525 David Gibson
        if ((ptel & 0xff000) == 0) {
117 f43e3525 David Gibson
            /* 16M page */
118 f43e3525 David Gibson
            porder = 24;
119 f43e3525 David Gibson
            /* lowest AVA bit must be 0 for 16M pages */
120 f43e3525 David Gibson
            if (pteh & 0x80) {
121 f43e3525 David Gibson
                return H_PARAMETER;
122 f43e3525 David Gibson
            }
123 f43e3525 David Gibson
        } else {
124 f43e3525 David Gibson
            return H_PARAMETER;
125 f43e3525 David Gibson
        }
126 f43e3525 David Gibson
    }
127 f43e3525 David Gibson
128 f43e3525 David Gibson
    pa = ptel & HPTE_R_RPN;
129 f43e3525 David Gibson
    /* FIXME: bounds check the pa? */
130 f43e3525 David Gibson
131 f43e3525 David Gibson
    /* Check WIMG */
132 f43e3525 David Gibson
    if ((ptel & HPTE_R_WIMG) != HPTE_R_M) {
133 f43e3525 David Gibson
        return H_PARAMETER;
134 f43e3525 David Gibson
    }
135 f43e3525 David Gibson
    pteh &= ~0x60ULL;
136 f43e3525 David Gibson
137 f43e3525 David Gibson
    if ((pte_index * HASH_PTE_SIZE_64) & ~env->htab_mask) {
138 f43e3525 David Gibson
        return H_PARAMETER;
139 f43e3525 David Gibson
    }
140 f43e3525 David Gibson
    if (likely((flags & H_EXACT) == 0)) {
141 f43e3525 David Gibson
        pte_index &= ~7ULL;
142 f43e3525 David Gibson
        hpte = env->external_htab + (pte_index * HASH_PTE_SIZE_64);
143 f43e3525 David Gibson
        for (i = 0; ; ++i) {
144 f43e3525 David Gibson
            if (i == 8) {
145 f43e3525 David Gibson
                return H_PTEG_FULL;
146 f43e3525 David Gibson
            }
147 f43e3525 David Gibson
            if (((ldq_p(hpte) & HPTE_V_VALID) == 0) &&
148 f43e3525 David Gibson
                lock_hpte(hpte, HPTE_V_HVLOCK | HPTE_V_VALID)) {
149 f43e3525 David Gibson
                break;
150 f43e3525 David Gibson
            }
151 f43e3525 David Gibson
            hpte += HASH_PTE_SIZE_64;
152 f43e3525 David Gibson
        }
153 f43e3525 David Gibson
    } else {
154 f43e3525 David Gibson
        i = 0;
155 f43e3525 David Gibson
        hpte = env->external_htab + (pte_index * HASH_PTE_SIZE_64);
156 f43e3525 David Gibson
        if (!lock_hpte(hpte, HPTE_V_HVLOCK | HPTE_V_VALID)) {
157 f43e3525 David Gibson
            return H_PTEG_FULL;
158 f43e3525 David Gibson
        }
159 f43e3525 David Gibson
    }
160 f43e3525 David Gibson
    stq_p(hpte + (HASH_PTE_SIZE_64/2), ptel);
161 f43e3525 David Gibson
    /* eieio();  FIXME: need some sort of barrier for smp? */
162 f43e3525 David Gibson
    stq_p(hpte, pteh);
163 f43e3525 David Gibson
164 f43e3525 David Gibson
    assert(!(ldq_p(hpte) & HPTE_V_HVLOCK));
165 f43e3525 David Gibson
    args[0] = pte_index + i;
166 f43e3525 David Gibson
    return H_SUCCESS;
167 f43e3525 David Gibson
}
168 f43e3525 David Gibson
169 f43e3525 David Gibson
static target_ulong h_remove(CPUState *env, sPAPREnvironment *spapr,
170 f43e3525 David Gibson
                             target_ulong opcode, target_ulong *args)
171 f43e3525 David Gibson
{
172 f43e3525 David Gibson
    target_ulong flags = args[0];
173 f43e3525 David Gibson
    target_ulong pte_index = args[1];
174 f43e3525 David Gibson
    target_ulong avpn = args[2];
175 f43e3525 David Gibson
    uint8_t *hpte;
176 f43e3525 David Gibson
    target_ulong v, r, rb;
177 f43e3525 David Gibson
178 f43e3525 David Gibson
    if ((pte_index * HASH_PTE_SIZE_64) & ~env->htab_mask) {
179 f43e3525 David Gibson
        return H_PARAMETER;
180 f43e3525 David Gibson
    }
181 f43e3525 David Gibson
182 f43e3525 David Gibson
    hpte = env->external_htab + (pte_index * HASH_PTE_SIZE_64);
183 f43e3525 David Gibson
    while (!lock_hpte(hpte, HPTE_V_HVLOCK)) {
184 f43e3525 David Gibson
        /* We have no real concurrency in qemu soft-emulation, so we
185 f43e3525 David Gibson
         * will never actually have a contested lock */
186 f43e3525 David Gibson
        assert(0);
187 f43e3525 David Gibson
    }
188 f43e3525 David Gibson
189 f43e3525 David Gibson
    v = ldq_p(hpte);
190 f43e3525 David Gibson
    r = ldq_p(hpte + (HASH_PTE_SIZE_64/2));
191 f43e3525 David Gibson
192 f43e3525 David Gibson
    if ((v & HPTE_V_VALID) == 0 ||
193 f43e3525 David Gibson
        ((flags & H_AVPN) && (v & ~0x7fULL) != avpn) ||
194 f43e3525 David Gibson
        ((flags & H_ANDCOND) && (v & avpn) != 0)) {
195 f43e3525 David Gibson
        stq_p(hpte, v & ~HPTE_V_HVLOCK);
196 f43e3525 David Gibson
        assert(!(ldq_p(hpte) & HPTE_V_HVLOCK));
197 f43e3525 David Gibson
        return H_NOT_FOUND;
198 f43e3525 David Gibson
    }
199 f43e3525 David Gibson
    args[0] = v & ~HPTE_V_HVLOCK;
200 f43e3525 David Gibson
    args[1] = r;
201 f43e3525 David Gibson
    stq_p(hpte, 0);
202 f43e3525 David Gibson
    rb = compute_tlbie_rb(v, r, pte_index);
203 f43e3525 David Gibson
    ppc_tlb_invalidate_one(env, rb);
204 f43e3525 David Gibson
    assert(!(ldq_p(hpte) & HPTE_V_HVLOCK));
205 f43e3525 David Gibson
    return H_SUCCESS;
206 f43e3525 David Gibson
}
207 f43e3525 David Gibson
208 f43e3525 David Gibson
static target_ulong h_protect(CPUState *env, sPAPREnvironment *spapr,
209 f43e3525 David Gibson
                              target_ulong opcode, target_ulong *args)
210 f43e3525 David Gibson
{
211 f43e3525 David Gibson
    target_ulong flags = args[0];
212 f43e3525 David Gibson
    target_ulong pte_index = args[1];
213 f43e3525 David Gibson
    target_ulong avpn = args[2];
214 f43e3525 David Gibson
    uint8_t *hpte;
215 f43e3525 David Gibson
    target_ulong v, r, rb;
216 f43e3525 David Gibson
217 f43e3525 David Gibson
    if ((pte_index * HASH_PTE_SIZE_64) & ~env->htab_mask) {
218 f43e3525 David Gibson
        return H_PARAMETER;
219 f43e3525 David Gibson
    }
220 f43e3525 David Gibson
221 f43e3525 David Gibson
    hpte = env->external_htab + (pte_index * HASH_PTE_SIZE_64);
222 f43e3525 David Gibson
    while (!lock_hpte(hpte, HPTE_V_HVLOCK)) {
223 f43e3525 David Gibson
        /* We have no real concurrency in qemu soft-emulation, so we
224 f43e3525 David Gibson
         * will never actually have a contested lock */
225 f43e3525 David Gibson
        assert(0);
226 f43e3525 David Gibson
    }
227 f43e3525 David Gibson
228 f43e3525 David Gibson
    v = ldq_p(hpte);
229 f43e3525 David Gibson
    r = ldq_p(hpte + (HASH_PTE_SIZE_64/2));
230 f43e3525 David Gibson
231 f43e3525 David Gibson
    if ((v & HPTE_V_VALID) == 0 ||
232 f43e3525 David Gibson
        ((flags & H_AVPN) && (v & ~0x7fULL) != avpn)) {
233 f43e3525 David Gibson
        stq_p(hpte, v & ~HPTE_V_HVLOCK);
234 f43e3525 David Gibson
        assert(!(ldq_p(hpte) & HPTE_V_HVLOCK));
235 f43e3525 David Gibson
        return H_NOT_FOUND;
236 f43e3525 David Gibson
    }
237 f43e3525 David Gibson
238 f43e3525 David Gibson
    r &= ~(HPTE_R_PP0 | HPTE_R_PP | HPTE_R_N |
239 f43e3525 David Gibson
           HPTE_R_KEY_HI | HPTE_R_KEY_LO);
240 f43e3525 David Gibson
    r |= (flags << 55) & HPTE_R_PP0;
241 f43e3525 David Gibson
    r |= (flags << 48) & HPTE_R_KEY_HI;
242 f43e3525 David Gibson
    r |= flags & (HPTE_R_PP | HPTE_R_N | HPTE_R_KEY_LO);
243 f43e3525 David Gibson
    rb = compute_tlbie_rb(v, r, pte_index);
244 f43e3525 David Gibson
    stq_p(hpte, v & ~HPTE_V_VALID);
245 f43e3525 David Gibson
    ppc_tlb_invalidate_one(env, rb);
246 f43e3525 David Gibson
    stq_p(hpte + (HASH_PTE_SIZE_64/2), r);
247 f43e3525 David Gibson
    /* Don't need a memory barrier, due to qemu's global lock */
248 f43e3525 David Gibson
    stq_p(hpte, v & ~HPTE_V_HVLOCK);
249 f43e3525 David Gibson
    assert(!(ldq_p(hpte) & HPTE_V_HVLOCK));
250 f43e3525 David Gibson
    return H_SUCCESS;
251 f43e3525 David Gibson
}
252 f43e3525 David Gibson
253 821303f5 David Gibson
static target_ulong h_set_dabr(CPUState *env, sPAPREnvironment *spapr,
254 821303f5 David Gibson
                               target_ulong opcode, target_ulong *args)
255 821303f5 David Gibson
{
256 821303f5 David Gibson
    /* FIXME: actually implement this */
257 821303f5 David Gibson
    return H_HARDWARE;
258 821303f5 David Gibson
}
259 821303f5 David Gibson
260 ed120055 David Gibson
#define FLAGS_REGISTER_VPA         0x0000200000000000ULL
261 ed120055 David Gibson
#define FLAGS_REGISTER_DTL         0x0000400000000000ULL
262 ed120055 David Gibson
#define FLAGS_REGISTER_SLBSHADOW   0x0000600000000000ULL
263 ed120055 David Gibson
#define FLAGS_DEREGISTER_VPA       0x0000a00000000000ULL
264 ed120055 David Gibson
#define FLAGS_DEREGISTER_DTL       0x0000c00000000000ULL
265 ed120055 David Gibson
#define FLAGS_DEREGISTER_SLBSHADOW 0x0000e00000000000ULL
266 ed120055 David Gibson
267 ed120055 David Gibson
#define VPA_MIN_SIZE           640
268 ed120055 David Gibson
#define VPA_SIZE_OFFSET        0x4
269 ed120055 David Gibson
#define VPA_SHARED_PROC_OFFSET 0x9
270 ed120055 David Gibson
#define VPA_SHARED_PROC_VAL    0x2
271 ed120055 David Gibson
272 ed120055 David Gibson
static target_ulong register_vpa(CPUState *env, target_ulong vpa)
273 ed120055 David Gibson
{
274 ed120055 David Gibson
    uint16_t size;
275 ed120055 David Gibson
    uint8_t tmp;
276 ed120055 David Gibson
277 ed120055 David Gibson
    if (vpa == 0) {
278 ed120055 David Gibson
        hcall_dprintf("Can't cope with registering a VPA at logical 0\n");
279 ed120055 David Gibson
        return H_HARDWARE;
280 ed120055 David Gibson
    }
281 ed120055 David Gibson
282 ed120055 David Gibson
    if (vpa % env->dcache_line_size) {
283 ed120055 David Gibson
        return H_PARAMETER;
284 ed120055 David Gibson
    }
285 ed120055 David Gibson
    /* FIXME: bounds check the address */
286 ed120055 David Gibson
287 ed120055 David Gibson
    size = lduw_phys(vpa + 0x4);
288 ed120055 David Gibson
289 ed120055 David Gibson
    if (size < VPA_MIN_SIZE) {
290 ed120055 David Gibson
        return H_PARAMETER;
291 ed120055 David Gibson
    }
292 ed120055 David Gibson
293 ed120055 David Gibson
    /* VPA is not allowed to cross a page boundary */
294 ed120055 David Gibson
    if ((vpa / 4096) != ((vpa + size - 1) / 4096)) {
295 ed120055 David Gibson
        return H_PARAMETER;
296 ed120055 David Gibson
    }
297 ed120055 David Gibson
298 ed120055 David Gibson
    env->vpa = vpa;
299 ed120055 David Gibson
300 ed120055 David Gibson
    tmp = ldub_phys(env->vpa + VPA_SHARED_PROC_OFFSET);
301 ed120055 David Gibson
    tmp |= VPA_SHARED_PROC_VAL;
302 ed120055 David Gibson
    stb_phys(env->vpa + VPA_SHARED_PROC_OFFSET, tmp);
303 ed120055 David Gibson
304 ed120055 David Gibson
    return H_SUCCESS;
305 ed120055 David Gibson
}
306 ed120055 David Gibson
307 ed120055 David Gibson
static target_ulong deregister_vpa(CPUState *env, target_ulong vpa)
308 ed120055 David Gibson
{
309 ed120055 David Gibson
    if (env->slb_shadow) {
310 ed120055 David Gibson
        return H_RESOURCE;
311 ed120055 David Gibson
    }
312 ed120055 David Gibson
313 ed120055 David Gibson
    if (env->dispatch_trace_log) {
314 ed120055 David Gibson
        return H_RESOURCE;
315 ed120055 David Gibson
    }
316 ed120055 David Gibson
317 ed120055 David Gibson
    env->vpa = 0;
318 ed120055 David Gibson
    return H_SUCCESS;
319 ed120055 David Gibson
}
320 ed120055 David Gibson
321 ed120055 David Gibson
static target_ulong register_slb_shadow(CPUState *env, target_ulong addr)
322 ed120055 David Gibson
{
323 ed120055 David Gibson
    uint32_t size;
324 ed120055 David Gibson
325 ed120055 David Gibson
    if (addr == 0) {
326 ed120055 David Gibson
        hcall_dprintf("Can't cope with SLB shadow at logical 0\n");
327 ed120055 David Gibson
        return H_HARDWARE;
328 ed120055 David Gibson
    }
329 ed120055 David Gibson
330 ed120055 David Gibson
    size = ldl_phys(addr + 0x4);
331 ed120055 David Gibson
    if (size < 0x8) {
332 ed120055 David Gibson
        return H_PARAMETER;
333 ed120055 David Gibson
    }
334 ed120055 David Gibson
335 ed120055 David Gibson
    if ((addr / 4096) != ((addr + size - 1) / 4096)) {
336 ed120055 David Gibson
        return H_PARAMETER;
337 ed120055 David Gibson
    }
338 ed120055 David Gibson
339 ed120055 David Gibson
    if (!env->vpa) {
340 ed120055 David Gibson
        return H_RESOURCE;
341 ed120055 David Gibson
    }
342 ed120055 David Gibson
343 ed120055 David Gibson
    env->slb_shadow = addr;
344 ed120055 David Gibson
345 ed120055 David Gibson
    return H_SUCCESS;
346 ed120055 David Gibson
}
347 ed120055 David Gibson
348 ed120055 David Gibson
static target_ulong deregister_slb_shadow(CPUState *env, target_ulong addr)
349 ed120055 David Gibson
{
350 ed120055 David Gibson
    env->slb_shadow = 0;
351 ed120055 David Gibson
    return H_SUCCESS;
352 ed120055 David Gibson
}
353 ed120055 David Gibson
354 ed120055 David Gibson
static target_ulong register_dtl(CPUState *env, target_ulong addr)
355 ed120055 David Gibson
{
356 ed120055 David Gibson
    uint32_t size;
357 ed120055 David Gibson
358 ed120055 David Gibson
    if (addr == 0) {
359 ed120055 David Gibson
        hcall_dprintf("Can't cope with DTL at logical 0\n");
360 ed120055 David Gibson
        return H_HARDWARE;
361 ed120055 David Gibson
    }
362 ed120055 David Gibson
363 ed120055 David Gibson
    size = ldl_phys(addr + 0x4);
364 ed120055 David Gibson
365 ed120055 David Gibson
    if (size < 48) {
366 ed120055 David Gibson
        return H_PARAMETER;
367 ed120055 David Gibson
    }
368 ed120055 David Gibson
369 ed120055 David Gibson
    if (!env->vpa) {
370 ed120055 David Gibson
        return H_RESOURCE;
371 ed120055 David Gibson
    }
372 ed120055 David Gibson
373 ed120055 David Gibson
    env->dispatch_trace_log = addr;
374 ed120055 David Gibson
    env->dtl_size = size;
375 ed120055 David Gibson
376 ed120055 David Gibson
    return H_SUCCESS;
377 ed120055 David Gibson
}
378 ed120055 David Gibson
379 ed120055 David Gibson
static target_ulong deregister_dtl(CPUState *emv, target_ulong addr)
380 ed120055 David Gibson
{
381 ed120055 David Gibson
    env->dispatch_trace_log = 0;
382 ed120055 David Gibson
    env->dtl_size = 0;
383 ed120055 David Gibson
384 ed120055 David Gibson
    return H_SUCCESS;
385 ed120055 David Gibson
}
386 ed120055 David Gibson
387 ed120055 David Gibson
static target_ulong h_register_vpa(CPUState *env, sPAPREnvironment *spapr,
388 ed120055 David Gibson
                                   target_ulong opcode, target_ulong *args)
389 ed120055 David Gibson
{
390 ed120055 David Gibson
    target_ulong flags = args[0];
391 ed120055 David Gibson
    target_ulong procno = args[1];
392 ed120055 David Gibson
    target_ulong vpa = args[2];
393 ed120055 David Gibson
    target_ulong ret = H_PARAMETER;
394 ed120055 David Gibson
    CPUState *tenv;
395 ed120055 David Gibson
396 ed120055 David Gibson
    for (tenv = first_cpu; tenv; tenv = tenv->next_cpu) {
397 ed120055 David Gibson
        if (tenv->cpu_index == procno) {
398 ed120055 David Gibson
            break;
399 ed120055 David Gibson
        }
400 ed120055 David Gibson
    }
401 ed120055 David Gibson
402 ed120055 David Gibson
    if (!tenv) {
403 ed120055 David Gibson
        return H_PARAMETER;
404 ed120055 David Gibson
    }
405 ed120055 David Gibson
406 ed120055 David Gibson
    switch (flags) {
407 ed120055 David Gibson
    case FLAGS_REGISTER_VPA:
408 ed120055 David Gibson
        ret = register_vpa(tenv, vpa);
409 ed120055 David Gibson
        break;
410 ed120055 David Gibson
411 ed120055 David Gibson
    case FLAGS_DEREGISTER_VPA:
412 ed120055 David Gibson
        ret = deregister_vpa(tenv, vpa);
413 ed120055 David Gibson
        break;
414 ed120055 David Gibson
415 ed120055 David Gibson
    case FLAGS_REGISTER_SLBSHADOW:
416 ed120055 David Gibson
        ret = register_slb_shadow(tenv, vpa);
417 ed120055 David Gibson
        break;
418 ed120055 David Gibson
419 ed120055 David Gibson
    case FLAGS_DEREGISTER_SLBSHADOW:
420 ed120055 David Gibson
        ret = deregister_slb_shadow(tenv, vpa);
421 ed120055 David Gibson
        break;
422 ed120055 David Gibson
423 ed120055 David Gibson
    case FLAGS_REGISTER_DTL:
424 ed120055 David Gibson
        ret = register_dtl(tenv, vpa);
425 ed120055 David Gibson
        break;
426 ed120055 David Gibson
427 ed120055 David Gibson
    case FLAGS_DEREGISTER_DTL:
428 ed120055 David Gibson
        ret = deregister_dtl(tenv, vpa);
429 ed120055 David Gibson
        break;
430 ed120055 David Gibson
    }
431 ed120055 David Gibson
432 ed120055 David Gibson
    return ret;
433 ed120055 David Gibson
}
434 ed120055 David Gibson
435 ed120055 David Gibson
static target_ulong h_cede(CPUState *env, sPAPREnvironment *spapr,
436 ed120055 David Gibson
                           target_ulong opcode, target_ulong *args)
437 ed120055 David Gibson
{
438 ed120055 David Gibson
    env->msr |= (1ULL << MSR_EE);
439 ed120055 David Gibson
    hreg_compute_hflags(env);
440 ed120055 David Gibson
    if (!cpu_has_work(env)) {
441 ed120055 David Gibson
        env->halted = 1;
442 ed120055 David Gibson
    }
443 ed120055 David Gibson
    return H_SUCCESS;
444 ed120055 David Gibson
}
445 ed120055 David Gibson
446 39ac8455 David Gibson
static target_ulong h_rtas(CPUState *env, sPAPREnvironment *spapr,
447 39ac8455 David Gibson
                           target_ulong opcode, target_ulong *args)
448 39ac8455 David Gibson
{
449 39ac8455 David Gibson
    target_ulong rtas_r3 = args[0];
450 39ac8455 David Gibson
    uint32_t token = ldl_phys(rtas_r3);
451 39ac8455 David Gibson
    uint32_t nargs = ldl_phys(rtas_r3 + 4);
452 39ac8455 David Gibson
    uint32_t nret = ldl_phys(rtas_r3 + 8);
453 39ac8455 David Gibson
454 39ac8455 David Gibson
    return spapr_rtas_call(spapr, token, nargs, rtas_r3 + 12,
455 39ac8455 David Gibson
                           nret, rtas_r3 + 12 + 4*nargs);
456 39ac8455 David Gibson
}
457 39ac8455 David Gibson
458 39ac8455 David Gibson
spapr_hcall_fn papr_hypercall_table[(MAX_HCALL_OPCODE / 4) + 1];
459 39ac8455 David Gibson
spapr_hcall_fn kvmppc_hypercall_table[KVMPPC_HCALL_MAX - KVMPPC_HCALL_BASE];
460 9fdf0c29 David Gibson
461 9fdf0c29 David Gibson
void spapr_register_hypercall(target_ulong opcode, spapr_hcall_fn fn)
462 9fdf0c29 David Gibson
{
463 39ac8455 David Gibson
    spapr_hcall_fn *slot;
464 39ac8455 David Gibson
465 39ac8455 David Gibson
    if (opcode <= MAX_HCALL_OPCODE) {
466 39ac8455 David Gibson
        assert((opcode & 0x3) == 0);
467 9fdf0c29 David Gibson
468 39ac8455 David Gibson
        slot = &papr_hypercall_table[opcode / 4];
469 39ac8455 David Gibson
    } else {
470 39ac8455 David Gibson
        assert((opcode >= KVMPPC_HCALL_BASE) && (opcode <= KVMPPC_HCALL_MAX));
471 9fdf0c29 David Gibson
472 9fdf0c29 David Gibson
473 39ac8455 David Gibson
        slot = &kvmppc_hypercall_table[opcode - KVMPPC_HCALL_BASE];
474 39ac8455 David Gibson
    }
475 9fdf0c29 David Gibson
476 39ac8455 David Gibson
    assert(!(*slot) || (fn == *slot));
477 39ac8455 David Gibson
    *slot = fn;
478 9fdf0c29 David Gibson
}
479 9fdf0c29 David Gibson
480 9fdf0c29 David Gibson
target_ulong spapr_hypercall(CPUState *env, target_ulong opcode,
481 9fdf0c29 David Gibson
                             target_ulong *args)
482 9fdf0c29 David Gibson
{
483 9fdf0c29 David Gibson
    if (msr_pr) {
484 9fdf0c29 David Gibson
        hcall_dprintf("Hypercall made with MSR[PR]=1\n");
485 9fdf0c29 David Gibson
        return H_PRIVILEGE;
486 9fdf0c29 David Gibson
    }
487 9fdf0c29 David Gibson
488 9fdf0c29 David Gibson
    if ((opcode <= MAX_HCALL_OPCODE)
489 9fdf0c29 David Gibson
        && ((opcode & 0x3) == 0)) {
490 39ac8455 David Gibson
        spapr_hcall_fn fn = papr_hypercall_table[opcode / 4];
491 39ac8455 David Gibson
492 39ac8455 David Gibson
        if (fn) {
493 39ac8455 David Gibson
            return fn(env, spapr, opcode, args);
494 39ac8455 David Gibson
        }
495 39ac8455 David Gibson
    } else if ((opcode >= KVMPPC_HCALL_BASE) &&
496 39ac8455 David Gibson
               (opcode <= KVMPPC_HCALL_MAX)) {
497 39ac8455 David Gibson
        spapr_hcall_fn fn = kvmppc_hypercall_table[opcode - KVMPPC_HCALL_BASE];
498 9fdf0c29 David Gibson
499 9fdf0c29 David Gibson
        if (fn) {
500 9fdf0c29 David Gibson
            return fn(env, spapr, opcode, args);
501 9fdf0c29 David Gibson
        }
502 9fdf0c29 David Gibson
    }
503 9fdf0c29 David Gibson
504 9fdf0c29 David Gibson
    hcall_dprintf("Unimplemented hcall 0x" TARGET_FMT_lx "\n", opcode);
505 9fdf0c29 David Gibson
    return H_FUNCTION;
506 9fdf0c29 David Gibson
}
507 f43e3525 David Gibson
508 f43e3525 David Gibson
static void hypercall_init(void)
509 f43e3525 David Gibson
{
510 f43e3525 David Gibson
    /* hcall-pft */
511 f43e3525 David Gibson
    spapr_register_hypercall(H_ENTER, h_enter);
512 f43e3525 David Gibson
    spapr_register_hypercall(H_REMOVE, h_remove);
513 f43e3525 David Gibson
    spapr_register_hypercall(H_PROTECT, h_protect);
514 39ac8455 David Gibson
515 821303f5 David Gibson
    /* hcall-dabr */
516 821303f5 David Gibson
    spapr_register_hypercall(H_SET_DABR, h_set_dabr);
517 821303f5 David Gibson
518 ed120055 David Gibson
    /* hcall-splpar */
519 ed120055 David Gibson
    spapr_register_hypercall(H_REGISTER_VPA, h_register_vpa);
520 ed120055 David Gibson
    spapr_register_hypercall(H_CEDE, h_cede);
521 ed120055 David Gibson
522 39ac8455 David Gibson
    /* qemu/KVM-PPC specific hcalls */
523 39ac8455 David Gibson
    spapr_register_hypercall(KVMPPC_H_RTAS, h_rtas);
524 f43e3525 David Gibson
}
525 f43e3525 David Gibson
device_init(hypercall_init);