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