]> Git Repo - qemu.git/blame - hw/ppc/spapr_hcall.c
spapr: cpu core: separate child threads destruction from machine state operations
[qemu.git] / hw / ppc / spapr_hcall.c
CommitLineData
0d75590d 1#include "qemu/osdep.h"
da34e65c 2#include "qapi/error.h"
b3946626 3#include "sysemu/hw_accel.h"
9c17d615 4#include "sysemu/sysemu.h"
03dd024f 5#include "qemu/log.h"
9fdf0c29 6#include "cpu.h"
63c91552 7#include "exec/exec-all.h"
ed120055 8#include "helper_regs.h"
0d09e41a 9#include "hw/ppc/spapr.h"
d5aea6f3 10#include "mmu-hash64.h"
3794d548
AK
11#include "cpu-models.h"
12#include "trace.h"
13#include "kvm_ppc.h"
facdb8b6 14#include "hw/ppc/spapr_ovec.h"
f43e3525 15
a46622fd 16struct SPRSyncState {
a46622fd
AK
17 int spr;
18 target_ulong value;
19 target_ulong mask;
20};
21
14e6fe12 22static void do_spr_sync(CPUState *cs, run_on_cpu_data arg)
a46622fd 23{
14e6fe12 24 struct SPRSyncState *s = arg.host_ptr;
e0eeb4a2 25 PowerPCCPU *cpu = POWERPC_CPU(cs);
a46622fd
AK
26 CPUPPCState *env = &cpu->env;
27
e0eeb4a2 28 cpu_synchronize_state(cs);
a46622fd
AK
29 env->spr[s->spr] &= ~s->mask;
30 env->spr[s->spr] |= s->value;
31}
32
33static void set_spr(CPUState *cs, int spr, target_ulong value,
34 target_ulong mask)
35{
36 struct SPRSyncState s = {
a46622fd
AK
37 .spr = spr,
38 .value = value,
39 .mask = mask
40 };
14e6fe12 41 run_on_cpu(cs, do_spr_sync, RUN_ON_CPU_HOST_PTR(&s));
a46622fd
AK
42}
43
af08a58f
TH
44static bool has_spr(PowerPCCPU *cpu, int spr)
45{
46 /* We can test whether the SPR is defined by checking for a valid name */
47 return cpu->env.spr_cb[spr].name != NULL;
48}
49
f3c75d42
AK
50static inline bool valid_pte_index(CPUPPCState *env, target_ulong pte_index)
51{
52 /*
53 * hash value/pteg group index is normalized by htab_mask
54 */
55 if (((pte_index & ~7ULL) / HPTES_PER_GROUP) & ~env->htab_mask) {
56 return false;
57 }
58 return true;
59}
60
ecbc25fa
DG
61static bool is_ram_address(sPAPRMachineState *spapr, hwaddr addr)
62{
63 MachineState *machine = MACHINE(spapr);
64 MemoryHotplugState *hpms = &spapr->hotplug_memory;
65
66 if (addr < machine->ram_size) {
67 return true;
68 }
69 if ((addr >= hpms->base)
70 && ((addr - hpms->base) < memory_region_size(&hpms->mr))) {
71 return true;
72 }
73
74 return false;
75}
76
28e02042 77static target_ulong h_enter(PowerPCCPU *cpu, sPAPRMachineState *spapr,
f43e3525
DG
78 target_ulong opcode, target_ulong *args)
79{
b13ce26d 80 CPUPPCState *env = &cpu->env;
f43e3525
DG
81 target_ulong flags = args[0];
82 target_ulong pte_index = args[1];
83 target_ulong pteh = args[2];
84 target_ulong ptel = args[3];
1f0252e6 85 unsigned apshift;
f73a2575 86 target_ulong raddr;
7c43bca0 87 target_ulong index;
7c43bca0 88 uint64_t token;
f43e3525 89
1f0252e6 90 apshift = ppc_hash64_hpte_page_shift_noslb(cpu, pteh, ptel);
1114e712
DG
91 if (!apshift) {
92 /* Bad page size encoding */
93 return H_PARAMETER;
f43e3525
DG
94 }
95
1114e712 96 raddr = (ptel & HPTE64_R_RPN) & ~((1ULL << apshift) - 1);
f43e3525 97
ecbc25fa 98 if (is_ram_address(spapr, raddr)) {
f73a2575 99 /* Regular RAM - should have WIMG=0010 */
d5aea6f3 100 if ((ptel & HPTE64_R_WIMG) != HPTE64_R_M) {
f73a2575
DG
101 return H_PARAMETER;
102 }
103 } else {
c1175907 104 target_ulong wimg_flags;
f73a2575
DG
105 /* Looks like an IO address */
106 /* FIXME: What WIMG combinations could be sensible for IO?
107 * For now we allow WIMG=010x, but are there others? */
108 /* FIXME: Should we check against registered IO addresses? */
c1175907
AK
109 wimg_flags = (ptel & (HPTE64_R_W | HPTE64_R_I | HPTE64_R_M));
110
111 if (wimg_flags != HPTE64_R_I &&
112 wimg_flags != (HPTE64_R_I | HPTE64_R_M)) {
f73a2575
DG
113 return H_PARAMETER;
114 }
f43e3525 115 }
f73a2575 116
f43e3525
DG
117 pteh &= ~0x60ULL;
118
f3c75d42 119 if (!valid_pte_index(env, pte_index)) {
f43e3525
DG
120 return H_PARAMETER;
121 }
7c43bca0
AK
122
123 index = 0;
f43e3525
DG
124 if (likely((flags & H_EXACT) == 0)) {
125 pte_index &= ~7ULL;
7c43bca0 126 token = ppc_hash64_start_access(cpu, pte_index);
7aaf4957 127 for (; index < 8; index++) {
7ef23068 128 if (!(ppc_hash64_load_hpte0(cpu, token, index) & HPTE64_V_VALID)) {
f43e3525
DG
129 break;
130 }
7aaf4957 131 }
c18ad9a5 132 ppc_hash64_stop_access(cpu, token);
7aaf4957
AK
133 if (index == 8) {
134 return H_PTEG_FULL;
135 }
f43e3525 136 } else {
7c43bca0 137 token = ppc_hash64_start_access(cpu, pte_index);
7ef23068 138 if (ppc_hash64_load_hpte0(cpu, token, 0) & HPTE64_V_VALID) {
c18ad9a5 139 ppc_hash64_stop_access(cpu, token);
f43e3525
DG
140 return H_PTEG_FULL;
141 }
c18ad9a5 142 ppc_hash64_stop_access(cpu, token);
f43e3525 143 }
7c43bca0 144
7ef23068 145 ppc_hash64_store_hpte(cpu, pte_index + index,
3f94170b 146 pteh | HPTE64_V_HPTE_DIRTY, ptel);
f43e3525 147
7c43bca0 148 args[0] = pte_index + index;
f43e3525
DG
149 return H_SUCCESS;
150}
151
a3801402 152typedef enum {
a3d0abae
DG
153 REMOVE_SUCCESS = 0,
154 REMOVE_NOT_FOUND = 1,
155 REMOVE_PARM = 2,
156 REMOVE_HW = 3,
a3801402 157} RemoveResult;
a3d0abae 158
7ef23068 159static RemoveResult remove_hpte(PowerPCCPU *cpu, target_ulong ptex,
a3d0abae
DG
160 target_ulong avpn,
161 target_ulong flags,
162 target_ulong *vp, target_ulong *rp)
f43e3525 163{
7ef23068 164 CPUPPCState *env = &cpu->env;
7c43bca0 165 uint64_t token;
61a36c9b 166 target_ulong v, r;
f43e3525 167
f3c75d42 168 if (!valid_pte_index(env, ptex)) {
a3d0abae 169 return REMOVE_PARM;
f43e3525
DG
170 }
171
7ef23068
DG
172 token = ppc_hash64_start_access(cpu, ptex);
173 v = ppc_hash64_load_hpte0(cpu, token, 0);
174 r = ppc_hash64_load_hpte1(cpu, token, 0);
c18ad9a5 175 ppc_hash64_stop_access(cpu, token);
f43e3525 176
d5aea6f3 177 if ((v & HPTE64_V_VALID) == 0 ||
f43e3525
DG
178 ((flags & H_AVPN) && (v & ~0x7fULL) != avpn) ||
179 ((flags & H_ANDCOND) && (v & avpn) != 0)) {
a3d0abae 180 return REMOVE_NOT_FOUND;
f43e3525 181 }
35f9304d 182 *vp = v;
a3d0abae 183 *rp = r;
7ef23068 184 ppc_hash64_store_hpte(cpu, ptex, HPTE64_V_HPTE_DIRTY, 0);
61a36c9b 185 ppc_hash64_tlb_flush_hpte(cpu, ptex, v, r);
a3d0abae
DG
186 return REMOVE_SUCCESS;
187}
188
28e02042 189static target_ulong h_remove(PowerPCCPU *cpu, sPAPRMachineState *spapr,
a3d0abae
DG
190 target_ulong opcode, target_ulong *args)
191{
cd0c6f47 192 CPUPPCState *env = &cpu->env;
a3d0abae
DG
193 target_ulong flags = args[0];
194 target_ulong pte_index = args[1];
195 target_ulong avpn = args[2];
a3801402 196 RemoveResult ret;
a3d0abae 197
7ef23068 198 ret = remove_hpte(cpu, pte_index, avpn, flags,
a3d0abae
DG
199 &args[0], &args[1]);
200
201 switch (ret) {
202 case REMOVE_SUCCESS:
e3cffe6f 203 check_tlb_flush(env, true);
a3d0abae
DG
204 return H_SUCCESS;
205
206 case REMOVE_NOT_FOUND:
207 return H_NOT_FOUND;
208
209 case REMOVE_PARM:
210 return H_PARAMETER;
211
212 case REMOVE_HW:
213 return H_HARDWARE;
214 }
215
9a39970d 216 g_assert_not_reached();
a3d0abae
DG
217}
218
219#define H_BULK_REMOVE_TYPE 0xc000000000000000ULL
220#define H_BULK_REMOVE_REQUEST 0x4000000000000000ULL
221#define H_BULK_REMOVE_RESPONSE 0x8000000000000000ULL
222#define H_BULK_REMOVE_END 0xc000000000000000ULL
223#define H_BULK_REMOVE_CODE 0x3000000000000000ULL
224#define H_BULK_REMOVE_SUCCESS 0x0000000000000000ULL
225#define H_BULK_REMOVE_NOT_FOUND 0x1000000000000000ULL
226#define H_BULK_REMOVE_PARM 0x2000000000000000ULL
227#define H_BULK_REMOVE_HW 0x3000000000000000ULL
228#define H_BULK_REMOVE_RC 0x0c00000000000000ULL
229#define H_BULK_REMOVE_FLAGS 0x0300000000000000ULL
230#define H_BULK_REMOVE_ABSOLUTE 0x0000000000000000ULL
231#define H_BULK_REMOVE_ANDCOND 0x0100000000000000ULL
232#define H_BULK_REMOVE_AVPN 0x0200000000000000ULL
233#define H_BULK_REMOVE_PTEX 0x00ffffffffffffffULL
234
235#define H_BULK_REMOVE_MAX_BATCH 4
236
28e02042 237static target_ulong h_bulk_remove(PowerPCCPU *cpu, sPAPRMachineState *spapr,
a3d0abae
DG
238 target_ulong opcode, target_ulong *args)
239{
cd0c6f47 240 CPUPPCState *env = &cpu->env;
a3d0abae 241 int i;
cd0c6f47 242 target_ulong rc = H_SUCCESS;
a3d0abae
DG
243
244 for (i = 0; i < H_BULK_REMOVE_MAX_BATCH; i++) {
245 target_ulong *tsh = &args[i*2];
246 target_ulong tsl = args[i*2 + 1];
247 target_ulong v, r, ret;
248
249 if ((*tsh & H_BULK_REMOVE_TYPE) == H_BULK_REMOVE_END) {
250 break;
251 } else if ((*tsh & H_BULK_REMOVE_TYPE) != H_BULK_REMOVE_REQUEST) {
252 return H_PARAMETER;
253 }
254
255 *tsh &= H_BULK_REMOVE_PTEX | H_BULK_REMOVE_FLAGS;
256 *tsh |= H_BULK_REMOVE_RESPONSE;
257
258 if ((*tsh & H_BULK_REMOVE_ANDCOND) && (*tsh & H_BULK_REMOVE_AVPN)) {
259 *tsh |= H_BULK_REMOVE_PARM;
260 return H_PARAMETER;
261 }
262
7ef23068 263 ret = remove_hpte(cpu, *tsh & H_BULK_REMOVE_PTEX, tsl,
a3d0abae
DG
264 (*tsh & H_BULK_REMOVE_FLAGS) >> 26,
265 &v, &r);
266
267 *tsh |= ret << 60;
268
269 switch (ret) {
270 case REMOVE_SUCCESS:
d5aea6f3 271 *tsh |= (r & (HPTE64_R_C | HPTE64_R_R)) << 43;
a3d0abae
DG
272 break;
273
274 case REMOVE_PARM:
cd0c6f47
BH
275 rc = H_PARAMETER;
276 goto exit;
a3d0abae
DG
277
278 case REMOVE_HW:
cd0c6f47
BH
279 rc = H_HARDWARE;
280 goto exit;
a3d0abae
DG
281 }
282 }
cd0c6f47 283 exit:
e3cffe6f 284 check_tlb_flush(env, true);
a3d0abae 285
cd0c6f47 286 return rc;
f43e3525
DG
287}
288
28e02042 289static target_ulong h_protect(PowerPCCPU *cpu, sPAPRMachineState *spapr,
f43e3525
DG
290 target_ulong opcode, target_ulong *args)
291{
b13ce26d 292 CPUPPCState *env = &cpu->env;
f43e3525
DG
293 target_ulong flags = args[0];
294 target_ulong pte_index = args[1];
295 target_ulong avpn = args[2];
7c43bca0 296 uint64_t token;
61a36c9b 297 target_ulong v, r;
f43e3525 298
f3c75d42 299 if (!valid_pte_index(env, pte_index)) {
f43e3525
DG
300 return H_PARAMETER;
301 }
302
7c43bca0 303 token = ppc_hash64_start_access(cpu, pte_index);
7ef23068
DG
304 v = ppc_hash64_load_hpte0(cpu, token, 0);
305 r = ppc_hash64_load_hpte1(cpu, token, 0);
c18ad9a5 306 ppc_hash64_stop_access(cpu, token);
f43e3525 307
d5aea6f3 308 if ((v & HPTE64_V_VALID) == 0 ||
f43e3525 309 ((flags & H_AVPN) && (v & ~0x7fULL) != avpn)) {
f43e3525
DG
310 return H_NOT_FOUND;
311 }
312
d5aea6f3
DG
313 r &= ~(HPTE64_R_PP0 | HPTE64_R_PP | HPTE64_R_N |
314 HPTE64_R_KEY_HI | HPTE64_R_KEY_LO);
315 r |= (flags << 55) & HPTE64_R_PP0;
316 r |= (flags << 48) & HPTE64_R_KEY_HI;
317 r |= flags & (HPTE64_R_PP | HPTE64_R_N | HPTE64_R_KEY_LO);
7ef23068 318 ppc_hash64_store_hpte(cpu, pte_index,
3f94170b 319 (v & ~HPTE64_V_VALID) | HPTE64_V_HPTE_DIRTY, 0);
61a36c9b 320 ppc_hash64_tlb_flush_hpte(cpu, pte_index, v, r);
d76ab5e1
ND
321 /* Flush the tlb */
322 check_tlb_flush(env, true);
f43e3525 323 /* Don't need a memory barrier, due to qemu's global lock */
7ef23068 324 ppc_hash64_store_hpte(cpu, pte_index, v | HPTE64_V_HPTE_DIRTY, r);
f43e3525
DG
325 return H_SUCCESS;
326}
327
28e02042 328static target_ulong h_read(PowerPCCPU *cpu, sPAPRMachineState *spapr,
6bbd5dde
EC
329 target_ulong opcode, target_ulong *args)
330{
331 CPUPPCState *env = &cpu->env;
332 target_ulong flags = args[0];
333 target_ulong pte_index = args[1];
334 uint8_t *hpte;
335 int i, ridx, n_entries = 1;
336
f3c75d42 337 if (!valid_pte_index(env, pte_index)) {
6bbd5dde
EC
338 return H_PARAMETER;
339 }
340
341 if (flags & H_READ_4) {
342 /* Clear the two low order bits */
343 pte_index &= ~(3ULL);
344 n_entries = 4;
345 }
346
347 hpte = env->external_htab + (pte_index * HASH_PTE_SIZE_64);
348
349 for (i = 0, ridx = 0; i < n_entries; i++) {
350 args[ridx++] = ldq_p(hpte);
351 args[ridx++] = ldq_p(hpte + (HASH_PTE_SIZE_64/2));
352 hpte += HASH_PTE_SIZE_64;
353 }
354
355 return H_SUCCESS;
356}
357
423576f7
TH
358static target_ulong h_set_sprg0(PowerPCCPU *cpu, sPAPRMachineState *spapr,
359 target_ulong opcode, target_ulong *args)
360{
361 cpu_synchronize_state(CPU(cpu));
362 cpu->env.spr[SPR_SPRG0] = args[0];
363
364 return H_SUCCESS;
365}
366
28e02042 367static target_ulong h_set_dabr(PowerPCCPU *cpu, sPAPRMachineState *spapr,
821303f5
DG
368 target_ulong opcode, target_ulong *args)
369{
af08a58f
TH
370 if (!has_spr(cpu, SPR_DABR)) {
371 return H_HARDWARE; /* DABR register not available */
372 }
373 cpu_synchronize_state(CPU(cpu));
374
375 if (has_spr(cpu, SPR_DABRX)) {
376 cpu->env.spr[SPR_DABRX] = 0x3; /* Use Problem and Privileged state */
377 } else if (!(args[0] & 0x4)) { /* Breakpoint Translation set? */
378 return H_RESERVED_DABR;
379 }
380
381 cpu->env.spr[SPR_DABR] = args[0];
382 return H_SUCCESS;
821303f5
DG
383}
384
e49ff266
TH
385static target_ulong h_set_xdabr(PowerPCCPU *cpu, sPAPRMachineState *spapr,
386 target_ulong opcode, target_ulong *args)
387{
388 target_ulong dabrx = args[1];
389
390 if (!has_spr(cpu, SPR_DABR) || !has_spr(cpu, SPR_DABRX)) {
391 return H_HARDWARE;
392 }
393
394 if ((dabrx & ~0xfULL) != 0 || (dabrx & H_DABRX_HYPERVISOR) != 0
395 || (dabrx & (H_DABRX_KERNEL | H_DABRX_USER)) == 0) {
396 return H_PARAMETER;
397 }
398
399 cpu_synchronize_state(CPU(cpu));
400 cpu->env.spr[SPR_DABRX] = dabrx;
401 cpu->env.spr[SPR_DABR] = args[0];
402
403 return H_SUCCESS;
404}
405
3240dd9a
TH
406static target_ulong h_page_init(PowerPCCPU *cpu, sPAPRMachineState *spapr,
407 target_ulong opcode, target_ulong *args)
408{
409 target_ulong flags = args[0];
410 hwaddr dst = args[1];
411 hwaddr src = args[2];
412 hwaddr len = TARGET_PAGE_SIZE;
413 uint8_t *pdst, *psrc;
414 target_long ret = H_SUCCESS;
415
416 if (flags & ~(H_ICACHE_SYNCHRONIZE | H_ICACHE_INVALIDATE
417 | H_COPY_PAGE | H_ZERO_PAGE)) {
418 qemu_log_mask(LOG_UNIMP, "h_page_init: Bad flags (" TARGET_FMT_lx "\n",
419 flags);
420 return H_PARAMETER;
421 }
422
423 /* Map-in destination */
424 if (!is_ram_address(spapr, dst) || (dst & ~TARGET_PAGE_MASK) != 0) {
425 return H_PARAMETER;
426 }
427 pdst = cpu_physical_memory_map(dst, &len, 1);
428 if (!pdst || len != TARGET_PAGE_SIZE) {
429 return H_PARAMETER;
430 }
431
432 if (flags & H_COPY_PAGE) {
433 /* Map-in source, copy to destination, and unmap source again */
434 if (!is_ram_address(spapr, src) || (src & ~TARGET_PAGE_MASK) != 0) {
435 ret = H_PARAMETER;
436 goto unmap_out;
437 }
438 psrc = cpu_physical_memory_map(src, &len, 0);
439 if (!psrc || len != TARGET_PAGE_SIZE) {
440 ret = H_PARAMETER;
441 goto unmap_out;
442 }
443 memcpy(pdst, psrc, len);
444 cpu_physical_memory_unmap(psrc, len, 0, len);
445 } else if (flags & H_ZERO_PAGE) {
446 memset(pdst, 0, len); /* Just clear the destination page */
447 }
448
449 if (kvm_enabled() && (flags & H_ICACHE_SYNCHRONIZE) != 0) {
450 kvmppc_dcbst_range(cpu, pdst, len);
451 }
452 if (flags & (H_ICACHE_SYNCHRONIZE | H_ICACHE_INVALIDATE)) {
453 if (kvm_enabled()) {
454 kvmppc_icbi_range(cpu, pdst, len);
455 } else {
456 tb_flush(CPU(cpu));
457 }
458 }
459
460unmap_out:
461 cpu_physical_memory_unmap(pdst, TARGET_PAGE_SIZE, 1, len);
462 return ret;
463}
464
ed120055
DG
465#define FLAGS_REGISTER_VPA 0x0000200000000000ULL
466#define FLAGS_REGISTER_DTL 0x0000400000000000ULL
467#define FLAGS_REGISTER_SLBSHADOW 0x0000600000000000ULL
468#define FLAGS_DEREGISTER_VPA 0x0000a00000000000ULL
469#define FLAGS_DEREGISTER_DTL 0x0000c00000000000ULL
470#define FLAGS_DEREGISTER_SLBSHADOW 0x0000e00000000000ULL
471
472#define VPA_MIN_SIZE 640
473#define VPA_SIZE_OFFSET 0x4
474#define VPA_SHARED_PROC_OFFSET 0x9
475#define VPA_SHARED_PROC_VAL 0x2
476
e2684c0b 477static target_ulong register_vpa(CPUPPCState *env, target_ulong vpa)
ed120055 478{
33276f1b 479 CPUState *cs = CPU(ppc_env_get_cpu(env));
ed120055
DG
480 uint16_t size;
481 uint8_t tmp;
482
483 if (vpa == 0) {
484 hcall_dprintf("Can't cope with registering a VPA at logical 0\n");
485 return H_HARDWARE;
486 }
487
488 if (vpa % env->dcache_line_size) {
489 return H_PARAMETER;
490 }
491 /* FIXME: bounds check the address */
492
41701aa4 493 size = lduw_be_phys(cs->as, vpa + 0x4);
ed120055
DG
494
495 if (size < VPA_MIN_SIZE) {
496 return H_PARAMETER;
497 }
498
499 /* VPA is not allowed to cross a page boundary */
500 if ((vpa / 4096) != ((vpa + size - 1) / 4096)) {
501 return H_PARAMETER;
502 }
503
1bfb37d1 504 env->vpa_addr = vpa;
ed120055 505
2c17449b 506 tmp = ldub_phys(cs->as, env->vpa_addr + VPA_SHARED_PROC_OFFSET);
ed120055 507 tmp |= VPA_SHARED_PROC_VAL;
db3be60d 508 stb_phys(cs->as, env->vpa_addr + VPA_SHARED_PROC_OFFSET, tmp);
ed120055
DG
509
510 return H_SUCCESS;
511}
512
e2684c0b 513static target_ulong deregister_vpa(CPUPPCState *env, target_ulong vpa)
ed120055 514{
1bfb37d1 515 if (env->slb_shadow_addr) {
ed120055
DG
516 return H_RESOURCE;
517 }
518
1bfb37d1 519 if (env->dtl_addr) {
ed120055
DG
520 return H_RESOURCE;
521 }
522
1bfb37d1 523 env->vpa_addr = 0;
ed120055
DG
524 return H_SUCCESS;
525}
526
e2684c0b 527static target_ulong register_slb_shadow(CPUPPCState *env, target_ulong addr)
ed120055 528{
33276f1b 529 CPUState *cs = CPU(ppc_env_get_cpu(env));
ed120055
DG
530 uint32_t size;
531
532 if (addr == 0) {
533 hcall_dprintf("Can't cope with SLB shadow at logical 0\n");
534 return H_HARDWARE;
535 }
536
fdfba1a2 537 size = ldl_be_phys(cs->as, addr + 0x4);
ed120055
DG
538 if (size < 0x8) {
539 return H_PARAMETER;
540 }
541
542 if ((addr / 4096) != ((addr + size - 1) / 4096)) {
543 return H_PARAMETER;
544 }
545
1bfb37d1 546 if (!env->vpa_addr) {
ed120055
DG
547 return H_RESOURCE;
548 }
549
1bfb37d1
DG
550 env->slb_shadow_addr = addr;
551 env->slb_shadow_size = size;
ed120055
DG
552
553 return H_SUCCESS;
554}
555
e2684c0b 556static target_ulong deregister_slb_shadow(CPUPPCState *env, target_ulong addr)
ed120055 557{
1bfb37d1
DG
558 env->slb_shadow_addr = 0;
559 env->slb_shadow_size = 0;
ed120055
DG
560 return H_SUCCESS;
561}
562
e2684c0b 563static target_ulong register_dtl(CPUPPCState *env, target_ulong addr)
ed120055 564{
33276f1b 565 CPUState *cs = CPU(ppc_env_get_cpu(env));
ed120055
DG
566 uint32_t size;
567
568 if (addr == 0) {
569 hcall_dprintf("Can't cope with DTL at logical 0\n");
570 return H_HARDWARE;
571 }
572
fdfba1a2 573 size = ldl_be_phys(cs->as, addr + 0x4);
ed120055
DG
574
575 if (size < 48) {
576 return H_PARAMETER;
577 }
578
1bfb37d1 579 if (!env->vpa_addr) {
ed120055
DG
580 return H_RESOURCE;
581 }
582
1bfb37d1 583 env->dtl_addr = addr;
ed120055
DG
584 env->dtl_size = size;
585
586 return H_SUCCESS;
587}
588
73f7821b 589static target_ulong deregister_dtl(CPUPPCState *env, target_ulong addr)
ed120055 590{
1bfb37d1 591 env->dtl_addr = 0;
ed120055
DG
592 env->dtl_size = 0;
593
594 return H_SUCCESS;
595}
596
28e02042 597static target_ulong h_register_vpa(PowerPCCPU *cpu, sPAPRMachineState *spapr,
ed120055
DG
598 target_ulong opcode, target_ulong *args)
599{
600 target_ulong flags = args[0];
601 target_ulong procno = args[1];
602 target_ulong vpa = args[2];
603 target_ulong ret = H_PARAMETER;
e2684c0b 604 CPUPPCState *tenv;
0f20ba62 605 PowerPCCPU *tcpu;
ed120055 606
0f20ba62 607 tcpu = ppc_get_vcpu_by_dt_id(procno);
5353d03d 608 if (!tcpu) {
ed120055
DG
609 return H_PARAMETER;
610 }
0f20ba62 611 tenv = &tcpu->env;
ed120055
DG
612
613 switch (flags) {
614 case FLAGS_REGISTER_VPA:
615 ret = register_vpa(tenv, vpa);
616 break;
617
618 case FLAGS_DEREGISTER_VPA:
619 ret = deregister_vpa(tenv, vpa);
620 break;
621
622 case FLAGS_REGISTER_SLBSHADOW:
623 ret = register_slb_shadow(tenv, vpa);
624 break;
625
626 case FLAGS_DEREGISTER_SLBSHADOW:
627 ret = deregister_slb_shadow(tenv, vpa);
628 break;
629
630 case FLAGS_REGISTER_DTL:
631 ret = register_dtl(tenv, vpa);
632 break;
633
634 case FLAGS_DEREGISTER_DTL:
635 ret = deregister_dtl(tenv, vpa);
636 break;
637 }
638
639 return ret;
640}
641
28e02042 642static target_ulong h_cede(PowerPCCPU *cpu, sPAPRMachineState *spapr,
ed120055
DG
643 target_ulong opcode, target_ulong *args)
644{
b13ce26d 645 CPUPPCState *env = &cpu->env;
fcd7d003 646 CPUState *cs = CPU(cpu);
b13ce26d 647
ed120055
DG
648 env->msr |= (1ULL << MSR_EE);
649 hreg_compute_hflags(env);
fcd7d003 650 if (!cpu_has_work(cs)) {
259186a7 651 cs->halted = 1;
27103424 652 cs->exception_index = EXCP_HLT;
fcd7d003 653 cs->exit_request = 1;
ed120055
DG
654 }
655 return H_SUCCESS;
656}
657
28e02042 658static target_ulong h_rtas(PowerPCCPU *cpu, sPAPRMachineState *spapr,
39ac8455
DG
659 target_ulong opcode, target_ulong *args)
660{
661 target_ulong rtas_r3 = args[0];
4fe822e0
AK
662 uint32_t token = rtas_ld(rtas_r3, 0);
663 uint32_t nargs = rtas_ld(rtas_r3, 1);
664 uint32_t nret = rtas_ld(rtas_r3, 2);
39ac8455 665
210b580b 666 return spapr_rtas_call(cpu, spapr, token, nargs, rtas_r3 + 12,
39ac8455
DG
667 nret, rtas_r3 + 12 + 4*nargs);
668}
669
28e02042 670static target_ulong h_logical_load(PowerPCCPU *cpu, sPAPRMachineState *spapr,
827200a2
DG
671 target_ulong opcode, target_ulong *args)
672{
fdfba1a2 673 CPUState *cs = CPU(cpu);
827200a2
DG
674 target_ulong size = args[0];
675 target_ulong addr = args[1];
676
677 switch (size) {
678 case 1:
2c17449b 679 args[0] = ldub_phys(cs->as, addr);
827200a2
DG
680 return H_SUCCESS;
681 case 2:
41701aa4 682 args[0] = lduw_phys(cs->as, addr);
827200a2
DG
683 return H_SUCCESS;
684 case 4:
fdfba1a2 685 args[0] = ldl_phys(cs->as, addr);
827200a2
DG
686 return H_SUCCESS;
687 case 8:
2c17449b 688 args[0] = ldq_phys(cs->as, addr);
827200a2
DG
689 return H_SUCCESS;
690 }
691 return H_PARAMETER;
692}
693
28e02042 694static target_ulong h_logical_store(PowerPCCPU *cpu, sPAPRMachineState *spapr,
827200a2
DG
695 target_ulong opcode, target_ulong *args)
696{
f606604f
EI
697 CPUState *cs = CPU(cpu);
698
827200a2
DG
699 target_ulong size = args[0];
700 target_ulong addr = args[1];
701 target_ulong val = args[2];
702
703 switch (size) {
704 case 1:
db3be60d 705 stb_phys(cs->as, addr, val);
827200a2
DG
706 return H_SUCCESS;
707 case 2:
5ce5944d 708 stw_phys(cs->as, addr, val);
827200a2
DG
709 return H_SUCCESS;
710 case 4:
ab1da857 711 stl_phys(cs->as, addr, val);
827200a2
DG
712 return H_SUCCESS;
713 case 8:
f606604f 714 stq_phys(cs->as, addr, val);
827200a2
DG
715 return H_SUCCESS;
716 }
717 return H_PARAMETER;
718}
719
28e02042 720static target_ulong h_logical_memop(PowerPCCPU *cpu, sPAPRMachineState *spapr,
c73e3771
BH
721 target_ulong opcode, target_ulong *args)
722{
fdfba1a2
EI
723 CPUState *cs = CPU(cpu);
724
c73e3771
BH
725 target_ulong dst = args[0]; /* Destination address */
726 target_ulong src = args[1]; /* Source address */
727 target_ulong esize = args[2]; /* Element size (0=1,1=2,2=4,3=8) */
728 target_ulong count = args[3]; /* Element count */
729 target_ulong op = args[4]; /* 0 = copy, 1 = invert */
730 uint64_t tmp;
731 unsigned int mask = (1 << esize) - 1;
732 int step = 1 << esize;
733
734 if (count > 0x80000000) {
735 return H_PARAMETER;
736 }
737
738 if ((dst & mask) || (src & mask) || (op > 1)) {
739 return H_PARAMETER;
740 }
741
742 if (dst >= src && dst < (src + (count << esize))) {
743 dst = dst + ((count - 1) << esize);
744 src = src + ((count - 1) << esize);
745 step = -step;
746 }
747
748 while (count--) {
749 switch (esize) {
750 case 0:
2c17449b 751 tmp = ldub_phys(cs->as, src);
c73e3771
BH
752 break;
753 case 1:
41701aa4 754 tmp = lduw_phys(cs->as, src);
c73e3771
BH
755 break;
756 case 2:
fdfba1a2 757 tmp = ldl_phys(cs->as, src);
c73e3771
BH
758 break;
759 case 3:
2c17449b 760 tmp = ldq_phys(cs->as, src);
c73e3771
BH
761 break;
762 default:
763 return H_PARAMETER;
764 }
765 if (op == 1) {
766 tmp = ~tmp;
767 }
768 switch (esize) {
769 case 0:
db3be60d 770 stb_phys(cs->as, dst, tmp);
c73e3771
BH
771 break;
772 case 1:
5ce5944d 773 stw_phys(cs->as, dst, tmp);
c73e3771
BH
774 break;
775 case 2:
ab1da857 776 stl_phys(cs->as, dst, tmp);
c73e3771
BH
777 break;
778 case 3:
f606604f 779 stq_phys(cs->as, dst, tmp);
c73e3771
BH
780 break;
781 }
782 dst = dst + step;
783 src = src + step;
784 }
785
786 return H_SUCCESS;
787}
788
28e02042 789static target_ulong h_logical_icbi(PowerPCCPU *cpu, sPAPRMachineState *spapr,
827200a2
DG
790 target_ulong opcode, target_ulong *args)
791{
792 /* Nothing to do on emulation, KVM will trap this in the kernel */
793 return H_SUCCESS;
794}
795
28e02042 796static target_ulong h_logical_dcbf(PowerPCCPU *cpu, sPAPRMachineState *spapr,
827200a2
DG
797 target_ulong opcode, target_ulong *args)
798{
799 /* Nothing to do on emulation, KVM will trap this in the kernel */
800 return H_SUCCESS;
801}
802
7d0cd464
PM
803static target_ulong h_set_mode_resource_le(PowerPCCPU *cpu,
804 target_ulong mflags,
805 target_ulong value1,
806 target_ulong value2)
42561bf2
AB
807{
808 CPUState *cs;
42561bf2 809
c4015bbd
AK
810 if (value1) {
811 return H_P3;
812 }
813 if (value2) {
814 return H_P4;
815 }
816
817 switch (mflags) {
818 case H_SET_MODE_ENDIAN_BIG:
819 CPU_FOREACH(cs) {
820 set_spr(cs, SPR_LPCR, 0, LPCR_ILE);
42561bf2 821 }
eefaccc0 822 spapr_pci_switch_vga(true);
c4015bbd
AK
823 return H_SUCCESS;
824
825 case H_SET_MODE_ENDIAN_LITTLE:
826 CPU_FOREACH(cs) {
827 set_spr(cs, SPR_LPCR, LPCR_ILE, LPCR_ILE);
42561bf2 828 }
eefaccc0 829 spapr_pci_switch_vga(false);
c4015bbd
AK
830 return H_SUCCESS;
831 }
42561bf2 832
c4015bbd
AK
833 return H_UNSUPPORTED_FLAG;
834}
42561bf2 835
7d0cd464
PM
836static target_ulong h_set_mode_resource_addr_trans_mode(PowerPCCPU *cpu,
837 target_ulong mflags,
838 target_ulong value1,
839 target_ulong value2)
d5ac4f54
AK
840{
841 CPUState *cs;
842 PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
d5ac4f54
AK
843
844 if (!(pcc->insns_flags2 & PPC2_ISA207S)) {
845 return H_P2;
846 }
847 if (value1) {
848 return H_P3;
849 }
850 if (value2) {
851 return H_P4;
852 }
853
5c94b2a5 854 if (mflags == AIL_RESERVED) {
d5ac4f54
AK
855 return H_UNSUPPORTED_FLAG;
856 }
857
858 CPU_FOREACH(cs) {
d5ac4f54 859 set_spr(cs, SPR_LPCR, mflags << LPCR_AIL_SHIFT, LPCR_AIL);
d5ac4f54
AK
860 }
861
862 return H_SUCCESS;
863}
864
28e02042 865static target_ulong h_set_mode(PowerPCCPU *cpu, sPAPRMachineState *spapr,
c4015bbd
AK
866 target_ulong opcode, target_ulong *args)
867{
868 target_ulong resource = args[1];
869 target_ulong ret = H_P2;
870
871 switch (resource) {
872 case H_SET_MODE_RESOURCE_LE:
7d0cd464 873 ret = h_set_mode_resource_le(cpu, args[0], args[2], args[3]);
c4015bbd 874 break;
d5ac4f54 875 case H_SET_MODE_RESOURCE_ADDR_TRANS_MODE:
7d0cd464
PM
876 ret = h_set_mode_resource_addr_trans_mode(cpu, args[0],
877 args[2], args[3]);
d5ac4f54 878 break;
42561bf2
AB
879 }
880
42561bf2
AB
881 return ret;
882}
883
1c7ad77e
NP
884#define H_SIGNAL_SYS_RESET_ALL -1
885#define H_SIGNAL_SYS_RESET_ALLBUTSELF -2
886
887static target_ulong h_signal_sys_reset(PowerPCCPU *cpu,
888 sPAPRMachineState *spapr,
889 target_ulong opcode, target_ulong *args)
890{
891 target_long target = args[0];
892 CPUState *cs;
893
894 if (target < 0) {
895 /* Broadcast */
896 if (target < H_SIGNAL_SYS_RESET_ALLBUTSELF) {
897 return H_PARAMETER;
898 }
899
900 CPU_FOREACH(cs) {
901 PowerPCCPU *c = POWERPC_CPU(cs);
902
903 if (target == H_SIGNAL_SYS_RESET_ALLBUTSELF) {
904 if (c == cpu) {
905 continue;
906 }
907 }
908 run_on_cpu(cs, spapr_do_system_reset_on_cpu, RUN_ON_CPU_NULL);
909 }
910 return H_SUCCESS;
911
912 } else {
913 /* Unicast */
914 CPU_FOREACH(cs) {
915 if (cpu->cpu_dt_id == target) {
916 run_on_cpu(cs, spapr_do_system_reset_on_cpu, RUN_ON_CPU_NULL);
917 return H_SUCCESS;
918 }
919 }
920 return H_PARAMETER;
921 }
922}
923
152ef803 924static target_ulong h_client_architecture_support(PowerPCCPU *cpu,
28e02042 925 sPAPRMachineState *spapr,
2a6593cb
AK
926 target_ulong opcode,
927 target_ulong *args)
928{
27ac3e06 929 target_ulong list = ppc64_phys_to_real(args[0]);
facdb8b6 930 target_ulong ov_table;
152ef803
DG
931 bool explicit_match = false; /* Matched the CPU's real PVR */
932 uint32_t max_compat = cpu->max_compat;
933 uint32_t best_compat = 0;
934 int i;
6787d27b 935 sPAPROptionVector *ov5_guest, *ov5_cas_old, *ov5_updates;
3794d548 936
152ef803
DG
937 /*
938 * We scan the supplied table of PVRs looking for two things
939 * 1. Is our real CPU PVR in the list?
940 * 2. What's the "best" listed logical PVR
941 */
942 for (i = 0; i < 512; ++i) {
3794d548
AK
943 uint32_t pvr, pvr_mask;
944
27ac3e06 945 pvr_mask = ldl_be_phys(&address_space_memory, list);
152ef803
DG
946 pvr = ldl_be_phys(&address_space_memory, list + 4);
947 list += 8;
948
3794d548 949 if (~pvr_mask & pvr) {
152ef803 950 break; /* Terminator record */
3794d548 951 }
152ef803
DG
952
953 if ((cpu->env.spr[SPR_PVR] & pvr_mask) == (pvr & pvr_mask)) {
954 explicit_match = true;
955 } else {
956 if (ppc_check_compat(cpu, pvr, best_compat, max_compat)) {
957 best_compat = pvr;
958 }
959 }
960 }
961
962 if ((best_compat == 0) && (!explicit_match || max_compat)) {
963 /* We couldn't find a suitable compatibility mode, and either
964 * the guest doesn't support "raw" mode for this CPU, or raw
965 * mode is disabled because a maximum compat mode is set */
966 return H_HARDWARE;
3794d548
AK
967 }
968
3794d548 969 /* Parsing finished */
152ef803 970 trace_spapr_cas_pvr(cpu->compat_pvr, explicit_match, best_compat);
3794d548
AK
971
972 /* Update CPUs */
152ef803 973 if (cpu->compat_pvr != best_compat) {
f6f242c7 974 Error *local_err = NULL;
3794d548 975
f6f242c7
DG
976 ppc_set_compat_all(best_compat, &local_err);
977 if (local_err) {
978 error_report_err(local_err);
979 return H_HARDWARE;
3794d548
AK
980 }
981 }
982
03d196b7
BR
983 /* For the future use: here @ov_table points to the first option vector */
984 ov_table = list;
985
facdb8b6 986 ov5_guest = spapr_ovec_parse_vector(ov_table, 5);
2a6593cb 987
facdb8b6
MR
988 /* NOTE: there are actually a number of ov5 bits where input from the
989 * guest is always zero, and the platform/QEMU enables them independently
990 * of guest input. To model these properly we'd want some sort of mask,
991 * but since they only currently apply to memory migration as defined
992 * by LoPAPR 1.1, 14.5.4.8, which QEMU doesn't implement, we don't need
6787d27b 993 * to worry about this for now.
facdb8b6 994 */
6787d27b
MR
995 ov5_cas_old = spapr_ovec_clone(spapr->ov5_cas);
996 /* full range of negotiated ov5 capabilities */
facdb8b6
MR
997 spapr_ovec_intersect(spapr->ov5_cas, spapr->ov5, ov5_guest);
998 spapr_ovec_cleanup(ov5_guest);
6787d27b
MR
999 /* capabilities that have been added since CAS-generated guest reset.
1000 * if capabilities have since been removed, generate another reset
1001 */
1002 ov5_updates = spapr_ovec_new();
1003 spapr->cas_reboot = spapr_ovec_diff(ov5_updates,
1004 ov5_cas_old, spapr->ov5_cas);
1005
1006 if (!spapr->cas_reboot) {
1007 spapr->cas_reboot =
5b120785 1008 (spapr_h_cas_compose_response(spapr, args[1], args[2],
6787d27b
MR
1009 ov5_updates) != 0);
1010 }
1011 spapr_ovec_cleanup(ov5_updates);
03d196b7 1012
6787d27b 1013 if (spapr->cas_reboot) {
2a6593cb
AK
1014 qemu_system_reset_request();
1015 }
1016
1017 return H_SUCCESS;
1018}
1019
7d7ba3fe
DG
1020static spapr_hcall_fn papr_hypercall_table[(MAX_HCALL_OPCODE / 4) + 1];
1021static spapr_hcall_fn kvmppc_hypercall_table[KVMPPC_HCALL_MAX - KVMPPC_HCALL_BASE + 1];
9fdf0c29
DG
1022
1023void spapr_register_hypercall(target_ulong opcode, spapr_hcall_fn fn)
1024{
39ac8455
DG
1025 spapr_hcall_fn *slot;
1026
1027 if (opcode <= MAX_HCALL_OPCODE) {
1028 assert((opcode & 0x3) == 0);
9fdf0c29 1029
39ac8455
DG
1030 slot = &papr_hypercall_table[opcode / 4];
1031 } else {
1032 assert((opcode >= KVMPPC_HCALL_BASE) && (opcode <= KVMPPC_HCALL_MAX));
9fdf0c29 1033
39ac8455
DG
1034 slot = &kvmppc_hypercall_table[opcode - KVMPPC_HCALL_BASE];
1035 }
9fdf0c29 1036
c89d5299 1037 assert(!(*slot));
39ac8455 1038 *slot = fn;
9fdf0c29
DG
1039}
1040
aa100fa4 1041target_ulong spapr_hypercall(PowerPCCPU *cpu, target_ulong opcode,
9fdf0c29
DG
1042 target_ulong *args)
1043{
28e02042
DG
1044 sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
1045
9fdf0c29
DG
1046 if ((opcode <= MAX_HCALL_OPCODE)
1047 && ((opcode & 0x3) == 0)) {
39ac8455
DG
1048 spapr_hcall_fn fn = papr_hypercall_table[opcode / 4];
1049
1050 if (fn) {
b13ce26d 1051 return fn(cpu, spapr, opcode, args);
39ac8455
DG
1052 }
1053 } else if ((opcode >= KVMPPC_HCALL_BASE) &&
1054 (opcode <= KVMPPC_HCALL_MAX)) {
1055 spapr_hcall_fn fn = kvmppc_hypercall_table[opcode - KVMPPC_HCALL_BASE];
9fdf0c29
DG
1056
1057 if (fn) {
b13ce26d 1058 return fn(cpu, spapr, opcode, args);
9fdf0c29
DG
1059 }
1060 }
1061
aaf87c66
TH
1062 qemu_log_mask(LOG_UNIMP, "Unimplemented SPAPR hcall 0x" TARGET_FMT_lx "\n",
1063 opcode);
9fdf0c29
DG
1064 return H_FUNCTION;
1065}
f43e3525 1066
83f7d43a 1067static void hypercall_register_types(void)
f43e3525
DG
1068{
1069 /* hcall-pft */
1070 spapr_register_hypercall(H_ENTER, h_enter);
1071 spapr_register_hypercall(H_REMOVE, h_remove);
1072 spapr_register_hypercall(H_PROTECT, h_protect);
6bbd5dde 1073 spapr_register_hypercall(H_READ, h_read);
39ac8455 1074
a3d0abae
DG
1075 /* hcall-bulk */
1076 spapr_register_hypercall(H_BULK_REMOVE, h_bulk_remove);
1077
ed120055
DG
1078 /* hcall-splpar */
1079 spapr_register_hypercall(H_REGISTER_VPA, h_register_vpa);
1080 spapr_register_hypercall(H_CEDE, h_cede);
1c7ad77e 1081 spapr_register_hypercall(H_SIGNAL_SYS_RESET, h_signal_sys_reset);
ed120055 1082
423576f7
TH
1083 /* processor register resource access h-calls */
1084 spapr_register_hypercall(H_SET_SPRG0, h_set_sprg0);
af08a58f 1085 spapr_register_hypercall(H_SET_DABR, h_set_dabr);
e49ff266 1086 spapr_register_hypercall(H_SET_XDABR, h_set_xdabr);
3240dd9a 1087 spapr_register_hypercall(H_PAGE_INIT, h_page_init);
423576f7
TH
1088 spapr_register_hypercall(H_SET_MODE, h_set_mode);
1089
827200a2
DG
1090 /* "debugger" hcalls (also used by SLOF). Note: We do -not- differenciate
1091 * here between the "CI" and the "CACHE" variants, they will use whatever
1092 * mapping attributes qemu is using. When using KVM, the kernel will
1093 * enforce the attributes more strongly
1094 */
1095 spapr_register_hypercall(H_LOGICAL_CI_LOAD, h_logical_load);
1096 spapr_register_hypercall(H_LOGICAL_CI_STORE, h_logical_store);
1097 spapr_register_hypercall(H_LOGICAL_CACHE_LOAD, h_logical_load);
1098 spapr_register_hypercall(H_LOGICAL_CACHE_STORE, h_logical_store);
1099 spapr_register_hypercall(H_LOGICAL_ICBI, h_logical_icbi);
1100 spapr_register_hypercall(H_LOGICAL_DCBF, h_logical_dcbf);
c73e3771 1101 spapr_register_hypercall(KVMPPC_H_LOGICAL_MEMOP, h_logical_memop);
827200a2 1102
39ac8455
DG
1103 /* qemu/KVM-PPC specific hcalls */
1104 spapr_register_hypercall(KVMPPC_H_RTAS, h_rtas);
42561bf2 1105
2a6593cb
AK
1106 /* ibm,client-architecture-support support */
1107 spapr_register_hypercall(KVMPPC_H_CAS, h_client_architecture_support);
f43e3525 1108}
83f7d43a
AF
1109
1110type_init(hypercall_register_types)
This page took 0.74789 seconds and 4 git commands to generate.