]> Git Repo - qemu.git/blame - hw/ppc/spapr_hcall.c
target/ppc: SDR1 is a hypervisor resource
[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 /*
53 * hash value/pteg group index is normalized by htab_mask
54 */
c6404ade 55 if (((ptex & ~7ULL) / HPTES_PER_GROUP) & ~cpu->env.htab_mask) {
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;
7c43bca0 87 uint64_t token;
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)) {
c6404ade
DG
126 token = ppc_hash64_start_access(cpu, ptex);
127 for (slot = 0; slot < 8; slot++) {
128 if (!(ppc_hash64_load_hpte0(cpu, token, slot) & HPTE64_V_VALID)) {
f43e3525
DG
129 break;
130 }
7aaf4957 131 }
c18ad9a5 132 ppc_hash64_stop_access(cpu, token);
c6404ade 133 if (slot == 8) {
7aaf4957
AK
134 return H_PTEG_FULL;
135 }
f43e3525 136 } else {
c6404ade 137 token = ppc_hash64_start_access(cpu, ptex);
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
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{
7c43bca0 163 uint64_t token;
61a36c9b 164 target_ulong v, r;
f43e3525 165
c6404ade 166 if (!valid_ptex(cpu, ptex)) {
a3d0abae 167 return REMOVE_PARM;
f43e3525
DG
168 }
169
7ef23068
DG
170 token = ppc_hash64_start_access(cpu, ptex);
171 v = ppc_hash64_load_hpte0(cpu, token, 0);
172 r = ppc_hash64_load_hpte1(cpu, token, 0);
c18ad9a5 173 ppc_hash64_stop_access(cpu, token);
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];
7c43bca0 294 uint64_t token;
61a36c9b 295 target_ulong v, r;
f43e3525 296
c6404ade 297 if (!valid_ptex(cpu, ptex)) {
f43e3525
DG
298 return H_PARAMETER;
299 }
300
c6404ade 301 token = ppc_hash64_start_access(cpu, ptex);
7ef23068
DG
302 v = ppc_hash64_load_hpte0(cpu, token, 0);
303 r = ppc_hash64_load_hpte1(cpu, token, 0);
c18ad9a5 304 ppc_hash64_stop_access(cpu, token);
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{
329 CPUPPCState *env = &cpu->env;
330 target_ulong flags = args[0];
c6404ade 331 target_ulong ptex = args[1];
6bbd5dde
EC
332 uint8_t *hpte;
333 int i, ridx, n_entries = 1;
334
c6404ade 335 if (!valid_ptex(cpu, ptex)) {
6bbd5dde
EC
336 return H_PARAMETER;
337 }
338
339 if (flags & H_READ_4) {
340 /* Clear the two low order bits */
c6404ade 341 ptex &= ~(3ULL);
6bbd5dde
EC
342 n_entries = 4;
343 }
344
c6404ade 345 hpte = env->external_htab + (ptex * HASH_PTE_SIZE_64);
6bbd5dde
EC
346
347 for (i = 0, ridx = 0; i < n_entries; i++) {
348 args[ridx++] = ldq_p(hpte);
349 args[ridx++] = ldq_p(hpte + (HASH_PTE_SIZE_64/2));
350 hpte += HASH_PTE_SIZE_64;
351 }
352
353 return H_SUCCESS;
354}
355
423576f7
TH
356static target_ulong h_set_sprg0(PowerPCCPU *cpu, sPAPRMachineState *spapr,
357 target_ulong opcode, target_ulong *args)
358{
359 cpu_synchronize_state(CPU(cpu));
360 cpu->env.spr[SPR_SPRG0] = args[0];
361
362 return H_SUCCESS;
363}
364
28e02042 365static target_ulong h_set_dabr(PowerPCCPU *cpu, sPAPRMachineState *spapr,
821303f5
DG
366 target_ulong opcode, target_ulong *args)
367{
af08a58f
TH
368 if (!has_spr(cpu, SPR_DABR)) {
369 return H_HARDWARE; /* DABR register not available */
370 }
371 cpu_synchronize_state(CPU(cpu));
372
373 if (has_spr(cpu, SPR_DABRX)) {
374 cpu->env.spr[SPR_DABRX] = 0x3; /* Use Problem and Privileged state */
375 } else if (!(args[0] & 0x4)) { /* Breakpoint Translation set? */
376 return H_RESERVED_DABR;
377 }
378
379 cpu->env.spr[SPR_DABR] = args[0];
380 return H_SUCCESS;
821303f5
DG
381}
382
e49ff266
TH
383static target_ulong h_set_xdabr(PowerPCCPU *cpu, sPAPRMachineState *spapr,
384 target_ulong opcode, target_ulong *args)
385{
386 target_ulong dabrx = args[1];
387
388 if (!has_spr(cpu, SPR_DABR) || !has_spr(cpu, SPR_DABRX)) {
389 return H_HARDWARE;
390 }
391
392 if ((dabrx & ~0xfULL) != 0 || (dabrx & H_DABRX_HYPERVISOR) != 0
393 || (dabrx & (H_DABRX_KERNEL | H_DABRX_USER)) == 0) {
394 return H_PARAMETER;
395 }
396
397 cpu_synchronize_state(CPU(cpu));
398 cpu->env.spr[SPR_DABRX] = dabrx;
399 cpu->env.spr[SPR_DABR] = args[0];
400
401 return H_SUCCESS;
402}
403
3240dd9a
TH
404static target_ulong h_page_init(PowerPCCPU *cpu, sPAPRMachineState *spapr,
405 target_ulong opcode, target_ulong *args)
406{
407 target_ulong flags = args[0];
408 hwaddr dst = args[1];
409 hwaddr src = args[2];
410 hwaddr len = TARGET_PAGE_SIZE;
411 uint8_t *pdst, *psrc;
412 target_long ret = H_SUCCESS;
413
414 if (flags & ~(H_ICACHE_SYNCHRONIZE | H_ICACHE_INVALIDATE
415 | H_COPY_PAGE | H_ZERO_PAGE)) {
416 qemu_log_mask(LOG_UNIMP, "h_page_init: Bad flags (" TARGET_FMT_lx "\n",
417 flags);
418 return H_PARAMETER;
419 }
420
421 /* Map-in destination */
422 if (!is_ram_address(spapr, dst) || (dst & ~TARGET_PAGE_MASK) != 0) {
423 return H_PARAMETER;
424 }
425 pdst = cpu_physical_memory_map(dst, &len, 1);
426 if (!pdst || len != TARGET_PAGE_SIZE) {
427 return H_PARAMETER;
428 }
429
430 if (flags & H_COPY_PAGE) {
431 /* Map-in source, copy to destination, and unmap source again */
432 if (!is_ram_address(spapr, src) || (src & ~TARGET_PAGE_MASK) != 0) {
433 ret = H_PARAMETER;
434 goto unmap_out;
435 }
436 psrc = cpu_physical_memory_map(src, &len, 0);
437 if (!psrc || len != TARGET_PAGE_SIZE) {
438 ret = H_PARAMETER;
439 goto unmap_out;
440 }
441 memcpy(pdst, psrc, len);
442 cpu_physical_memory_unmap(psrc, len, 0, len);
443 } else if (flags & H_ZERO_PAGE) {
444 memset(pdst, 0, len); /* Just clear the destination page */
445 }
446
447 if (kvm_enabled() && (flags & H_ICACHE_SYNCHRONIZE) != 0) {
448 kvmppc_dcbst_range(cpu, pdst, len);
449 }
450 if (flags & (H_ICACHE_SYNCHRONIZE | H_ICACHE_INVALIDATE)) {
451 if (kvm_enabled()) {
452 kvmppc_icbi_range(cpu, pdst, len);
453 } else {
454 tb_flush(CPU(cpu));
455 }
456 }
457
458unmap_out:
459 cpu_physical_memory_unmap(pdst, TARGET_PAGE_SIZE, 1, len);
460 return ret;
461}
462
ed120055
DG
463#define FLAGS_REGISTER_VPA 0x0000200000000000ULL
464#define FLAGS_REGISTER_DTL 0x0000400000000000ULL
465#define FLAGS_REGISTER_SLBSHADOW 0x0000600000000000ULL
466#define FLAGS_DEREGISTER_VPA 0x0000a00000000000ULL
467#define FLAGS_DEREGISTER_DTL 0x0000c00000000000ULL
468#define FLAGS_DEREGISTER_SLBSHADOW 0x0000e00000000000ULL
469
470#define VPA_MIN_SIZE 640
471#define VPA_SIZE_OFFSET 0x4
472#define VPA_SHARED_PROC_OFFSET 0x9
473#define VPA_SHARED_PROC_VAL 0x2
474
e2684c0b 475static target_ulong register_vpa(CPUPPCState *env, target_ulong vpa)
ed120055 476{
33276f1b 477 CPUState *cs = CPU(ppc_env_get_cpu(env));
ed120055
DG
478 uint16_t size;
479 uint8_t tmp;
480
481 if (vpa == 0) {
482 hcall_dprintf("Can't cope with registering a VPA at logical 0\n");
483 return H_HARDWARE;
484 }
485
486 if (vpa % env->dcache_line_size) {
487 return H_PARAMETER;
488 }
489 /* FIXME: bounds check the address */
490
41701aa4 491 size = lduw_be_phys(cs->as, vpa + 0x4);
ed120055
DG
492
493 if (size < VPA_MIN_SIZE) {
494 return H_PARAMETER;
495 }
496
497 /* VPA is not allowed to cross a page boundary */
498 if ((vpa / 4096) != ((vpa + size - 1) / 4096)) {
499 return H_PARAMETER;
500 }
501
1bfb37d1 502 env->vpa_addr = vpa;
ed120055 503
2c17449b 504 tmp = ldub_phys(cs->as, env->vpa_addr + VPA_SHARED_PROC_OFFSET);
ed120055 505 tmp |= VPA_SHARED_PROC_VAL;
db3be60d 506 stb_phys(cs->as, env->vpa_addr + VPA_SHARED_PROC_OFFSET, tmp);
ed120055
DG
507
508 return H_SUCCESS;
509}
510
e2684c0b 511static target_ulong deregister_vpa(CPUPPCState *env, target_ulong vpa)
ed120055 512{
1bfb37d1 513 if (env->slb_shadow_addr) {
ed120055
DG
514 return H_RESOURCE;
515 }
516
1bfb37d1 517 if (env->dtl_addr) {
ed120055
DG
518 return H_RESOURCE;
519 }
520
1bfb37d1 521 env->vpa_addr = 0;
ed120055
DG
522 return H_SUCCESS;
523}
524
e2684c0b 525static target_ulong register_slb_shadow(CPUPPCState *env, target_ulong addr)
ed120055 526{
33276f1b 527 CPUState *cs = CPU(ppc_env_get_cpu(env));
ed120055
DG
528 uint32_t size;
529
530 if (addr == 0) {
531 hcall_dprintf("Can't cope with SLB shadow at logical 0\n");
532 return H_HARDWARE;
533 }
534
fdfba1a2 535 size = ldl_be_phys(cs->as, addr + 0x4);
ed120055
DG
536 if (size < 0x8) {
537 return H_PARAMETER;
538 }
539
540 if ((addr / 4096) != ((addr + size - 1) / 4096)) {
541 return H_PARAMETER;
542 }
543
1bfb37d1 544 if (!env->vpa_addr) {
ed120055
DG
545 return H_RESOURCE;
546 }
547
1bfb37d1
DG
548 env->slb_shadow_addr = addr;
549 env->slb_shadow_size = size;
ed120055
DG
550
551 return H_SUCCESS;
552}
553
e2684c0b 554static target_ulong deregister_slb_shadow(CPUPPCState *env, target_ulong addr)
ed120055 555{
1bfb37d1
DG
556 env->slb_shadow_addr = 0;
557 env->slb_shadow_size = 0;
ed120055
DG
558 return H_SUCCESS;
559}
560
e2684c0b 561static target_ulong register_dtl(CPUPPCState *env, target_ulong addr)
ed120055 562{
33276f1b 563 CPUState *cs = CPU(ppc_env_get_cpu(env));
ed120055
DG
564 uint32_t size;
565
566 if (addr == 0) {
567 hcall_dprintf("Can't cope with DTL at logical 0\n");
568 return H_HARDWARE;
569 }
570
fdfba1a2 571 size = ldl_be_phys(cs->as, addr + 0x4);
ed120055
DG
572
573 if (size < 48) {
574 return H_PARAMETER;
575 }
576
1bfb37d1 577 if (!env->vpa_addr) {
ed120055
DG
578 return H_RESOURCE;
579 }
580
1bfb37d1 581 env->dtl_addr = addr;
ed120055
DG
582 env->dtl_size = size;
583
584 return H_SUCCESS;
585}
586
73f7821b 587static target_ulong deregister_dtl(CPUPPCState *env, target_ulong addr)
ed120055 588{
1bfb37d1 589 env->dtl_addr = 0;
ed120055
DG
590 env->dtl_size = 0;
591
592 return H_SUCCESS;
593}
594
28e02042 595static target_ulong h_register_vpa(PowerPCCPU *cpu, sPAPRMachineState *spapr,
ed120055
DG
596 target_ulong opcode, target_ulong *args)
597{
598 target_ulong flags = args[0];
599 target_ulong procno = args[1];
600 target_ulong vpa = args[2];
601 target_ulong ret = H_PARAMETER;
e2684c0b 602 CPUPPCState *tenv;
0f20ba62 603 PowerPCCPU *tcpu;
ed120055 604
0f20ba62 605 tcpu = ppc_get_vcpu_by_dt_id(procno);
5353d03d 606 if (!tcpu) {
ed120055
DG
607 return H_PARAMETER;
608 }
0f20ba62 609 tenv = &tcpu->env;
ed120055
DG
610
611 switch (flags) {
612 case FLAGS_REGISTER_VPA:
613 ret = register_vpa(tenv, vpa);
614 break;
615
616 case FLAGS_DEREGISTER_VPA:
617 ret = deregister_vpa(tenv, vpa);
618 break;
619
620 case FLAGS_REGISTER_SLBSHADOW:
621 ret = register_slb_shadow(tenv, vpa);
622 break;
623
624 case FLAGS_DEREGISTER_SLBSHADOW:
625 ret = deregister_slb_shadow(tenv, vpa);
626 break;
627
628 case FLAGS_REGISTER_DTL:
629 ret = register_dtl(tenv, vpa);
630 break;
631
632 case FLAGS_DEREGISTER_DTL:
633 ret = deregister_dtl(tenv, vpa);
634 break;
635 }
636
637 return ret;
638}
639
28e02042 640static target_ulong h_cede(PowerPCCPU *cpu, sPAPRMachineState *spapr,
ed120055
DG
641 target_ulong opcode, target_ulong *args)
642{
b13ce26d 643 CPUPPCState *env = &cpu->env;
fcd7d003 644 CPUState *cs = CPU(cpu);
b13ce26d 645
ed120055
DG
646 env->msr |= (1ULL << MSR_EE);
647 hreg_compute_hflags(env);
fcd7d003 648 if (!cpu_has_work(cs)) {
259186a7 649 cs->halted = 1;
27103424 650 cs->exception_index = EXCP_HLT;
fcd7d003 651 cs->exit_request = 1;
ed120055
DG
652 }
653 return H_SUCCESS;
654}
655
28e02042 656static target_ulong h_rtas(PowerPCCPU *cpu, sPAPRMachineState *spapr,
39ac8455
DG
657 target_ulong opcode, target_ulong *args)
658{
659 target_ulong rtas_r3 = args[0];
4fe822e0
AK
660 uint32_t token = rtas_ld(rtas_r3, 0);
661 uint32_t nargs = rtas_ld(rtas_r3, 1);
662 uint32_t nret = rtas_ld(rtas_r3, 2);
39ac8455 663
210b580b 664 return spapr_rtas_call(cpu, spapr, token, nargs, rtas_r3 + 12,
39ac8455
DG
665 nret, rtas_r3 + 12 + 4*nargs);
666}
667
28e02042 668static target_ulong h_logical_load(PowerPCCPU *cpu, sPAPRMachineState *spapr,
827200a2
DG
669 target_ulong opcode, target_ulong *args)
670{
fdfba1a2 671 CPUState *cs = CPU(cpu);
827200a2
DG
672 target_ulong size = args[0];
673 target_ulong addr = args[1];
674
675 switch (size) {
676 case 1:
2c17449b 677 args[0] = ldub_phys(cs->as, addr);
827200a2
DG
678 return H_SUCCESS;
679 case 2:
41701aa4 680 args[0] = lduw_phys(cs->as, addr);
827200a2
DG
681 return H_SUCCESS;
682 case 4:
fdfba1a2 683 args[0] = ldl_phys(cs->as, addr);
827200a2
DG
684 return H_SUCCESS;
685 case 8:
2c17449b 686 args[0] = ldq_phys(cs->as, addr);
827200a2
DG
687 return H_SUCCESS;
688 }
689 return H_PARAMETER;
690}
691
28e02042 692static target_ulong h_logical_store(PowerPCCPU *cpu, sPAPRMachineState *spapr,
827200a2
DG
693 target_ulong opcode, target_ulong *args)
694{
f606604f
EI
695 CPUState *cs = CPU(cpu);
696
827200a2
DG
697 target_ulong size = args[0];
698 target_ulong addr = args[1];
699 target_ulong val = args[2];
700
701 switch (size) {
702 case 1:
db3be60d 703 stb_phys(cs->as, addr, val);
827200a2
DG
704 return H_SUCCESS;
705 case 2:
5ce5944d 706 stw_phys(cs->as, addr, val);
827200a2
DG
707 return H_SUCCESS;
708 case 4:
ab1da857 709 stl_phys(cs->as, addr, val);
827200a2
DG
710 return H_SUCCESS;
711 case 8:
f606604f 712 stq_phys(cs->as, addr, val);
827200a2
DG
713 return H_SUCCESS;
714 }
715 return H_PARAMETER;
716}
717
28e02042 718static target_ulong h_logical_memop(PowerPCCPU *cpu, sPAPRMachineState *spapr,
c73e3771
BH
719 target_ulong opcode, target_ulong *args)
720{
fdfba1a2
EI
721 CPUState *cs = CPU(cpu);
722
c73e3771
BH
723 target_ulong dst = args[0]; /* Destination address */
724 target_ulong src = args[1]; /* Source address */
725 target_ulong esize = args[2]; /* Element size (0=1,1=2,2=4,3=8) */
726 target_ulong count = args[3]; /* Element count */
727 target_ulong op = args[4]; /* 0 = copy, 1 = invert */
728 uint64_t tmp;
729 unsigned int mask = (1 << esize) - 1;
730 int step = 1 << esize;
731
732 if (count > 0x80000000) {
733 return H_PARAMETER;
734 }
735
736 if ((dst & mask) || (src & mask) || (op > 1)) {
737 return H_PARAMETER;
738 }
739
740 if (dst >= src && dst < (src + (count << esize))) {
741 dst = dst + ((count - 1) << esize);
742 src = src + ((count - 1) << esize);
743 step = -step;
744 }
745
746 while (count--) {
747 switch (esize) {
748 case 0:
2c17449b 749 tmp = ldub_phys(cs->as, src);
c73e3771
BH
750 break;
751 case 1:
41701aa4 752 tmp = lduw_phys(cs->as, src);
c73e3771
BH
753 break;
754 case 2:
fdfba1a2 755 tmp = ldl_phys(cs->as, src);
c73e3771
BH
756 break;
757 case 3:
2c17449b 758 tmp = ldq_phys(cs->as, src);
c73e3771
BH
759 break;
760 default:
761 return H_PARAMETER;
762 }
763 if (op == 1) {
764 tmp = ~tmp;
765 }
766 switch (esize) {
767 case 0:
db3be60d 768 stb_phys(cs->as, dst, tmp);
c73e3771
BH
769 break;
770 case 1:
5ce5944d 771 stw_phys(cs->as, dst, tmp);
c73e3771
BH
772 break;
773 case 2:
ab1da857 774 stl_phys(cs->as, dst, tmp);
c73e3771
BH
775 break;
776 case 3:
f606604f 777 stq_phys(cs->as, dst, tmp);
c73e3771
BH
778 break;
779 }
780 dst = dst + step;
781 src = src + step;
782 }
783
784 return H_SUCCESS;
785}
786
28e02042 787static target_ulong h_logical_icbi(PowerPCCPU *cpu, sPAPRMachineState *spapr,
827200a2
DG
788 target_ulong opcode, target_ulong *args)
789{
790 /* Nothing to do on emulation, KVM will trap this in the kernel */
791 return H_SUCCESS;
792}
793
28e02042 794static target_ulong h_logical_dcbf(PowerPCCPU *cpu, sPAPRMachineState *spapr,
827200a2
DG
795 target_ulong opcode, target_ulong *args)
796{
797 /* Nothing to do on emulation, KVM will trap this in the kernel */
798 return H_SUCCESS;
799}
800
7d0cd464
PM
801static target_ulong h_set_mode_resource_le(PowerPCCPU *cpu,
802 target_ulong mflags,
803 target_ulong value1,
804 target_ulong value2)
42561bf2
AB
805{
806 CPUState *cs;
42561bf2 807
c4015bbd
AK
808 if (value1) {
809 return H_P3;
810 }
811 if (value2) {
812 return H_P4;
813 }
814
815 switch (mflags) {
816 case H_SET_MODE_ENDIAN_BIG:
817 CPU_FOREACH(cs) {
818 set_spr(cs, SPR_LPCR, 0, LPCR_ILE);
42561bf2 819 }
eefaccc0 820 spapr_pci_switch_vga(true);
c4015bbd
AK
821 return H_SUCCESS;
822
823 case H_SET_MODE_ENDIAN_LITTLE:
824 CPU_FOREACH(cs) {
825 set_spr(cs, SPR_LPCR, LPCR_ILE, LPCR_ILE);
42561bf2 826 }
eefaccc0 827 spapr_pci_switch_vga(false);
c4015bbd
AK
828 return H_SUCCESS;
829 }
42561bf2 830
c4015bbd
AK
831 return H_UNSUPPORTED_FLAG;
832}
42561bf2 833
7d0cd464
PM
834static target_ulong h_set_mode_resource_addr_trans_mode(PowerPCCPU *cpu,
835 target_ulong mflags,
836 target_ulong value1,
837 target_ulong value2)
d5ac4f54
AK
838{
839 CPUState *cs;
840 PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
d5ac4f54
AK
841
842 if (!(pcc->insns_flags2 & PPC2_ISA207S)) {
843 return H_P2;
844 }
845 if (value1) {
846 return H_P3;
847 }
848 if (value2) {
849 return H_P4;
850 }
851
5c94b2a5 852 if (mflags == AIL_RESERVED) {
d5ac4f54
AK
853 return H_UNSUPPORTED_FLAG;
854 }
855
856 CPU_FOREACH(cs) {
d5ac4f54 857 set_spr(cs, SPR_LPCR, mflags << LPCR_AIL_SHIFT, LPCR_AIL);
d5ac4f54
AK
858 }
859
860 return H_SUCCESS;
861}
862
28e02042 863static target_ulong h_set_mode(PowerPCCPU *cpu, sPAPRMachineState *spapr,
c4015bbd
AK
864 target_ulong opcode, target_ulong *args)
865{
866 target_ulong resource = args[1];
867 target_ulong ret = H_P2;
868
869 switch (resource) {
870 case H_SET_MODE_RESOURCE_LE:
7d0cd464 871 ret = h_set_mode_resource_le(cpu, args[0], args[2], args[3]);
c4015bbd 872 break;
d5ac4f54 873 case H_SET_MODE_RESOURCE_ADDR_TRANS_MODE:
7d0cd464
PM
874 ret = h_set_mode_resource_addr_trans_mode(cpu, args[0],
875 args[2], args[3]);
d5ac4f54 876 break;
42561bf2
AB
877 }
878
42561bf2
AB
879 return ret;
880}
881
1c7ad77e
NP
882#define H_SIGNAL_SYS_RESET_ALL -1
883#define H_SIGNAL_SYS_RESET_ALLBUTSELF -2
884
885static target_ulong h_signal_sys_reset(PowerPCCPU *cpu,
886 sPAPRMachineState *spapr,
887 target_ulong opcode, target_ulong *args)
888{
889 target_long target = args[0];
890 CPUState *cs;
891
892 if (target < 0) {
893 /* Broadcast */
894 if (target < H_SIGNAL_SYS_RESET_ALLBUTSELF) {
895 return H_PARAMETER;
896 }
897
898 CPU_FOREACH(cs) {
899 PowerPCCPU *c = POWERPC_CPU(cs);
900
901 if (target == H_SIGNAL_SYS_RESET_ALLBUTSELF) {
902 if (c == cpu) {
903 continue;
904 }
905 }
906 run_on_cpu(cs, spapr_do_system_reset_on_cpu, RUN_ON_CPU_NULL);
907 }
908 return H_SUCCESS;
909
910 } else {
911 /* Unicast */
912 CPU_FOREACH(cs) {
913 if (cpu->cpu_dt_id == target) {
914 run_on_cpu(cs, spapr_do_system_reset_on_cpu, RUN_ON_CPU_NULL);
915 return H_SUCCESS;
916 }
917 }
918 return H_PARAMETER;
919 }
920}
921
152ef803 922static target_ulong h_client_architecture_support(PowerPCCPU *cpu,
28e02042 923 sPAPRMachineState *spapr,
2a6593cb
AK
924 target_ulong opcode,
925 target_ulong *args)
926{
27ac3e06 927 target_ulong list = ppc64_phys_to_real(args[0]);
facdb8b6 928 target_ulong ov_table;
152ef803
DG
929 bool explicit_match = false; /* Matched the CPU's real PVR */
930 uint32_t max_compat = cpu->max_compat;
931 uint32_t best_compat = 0;
932 int i;
6787d27b 933 sPAPROptionVector *ov5_guest, *ov5_cas_old, *ov5_updates;
3794d548 934
152ef803
DG
935 /*
936 * We scan the supplied table of PVRs looking for two things
937 * 1. Is our real CPU PVR in the list?
938 * 2. What's the "best" listed logical PVR
939 */
940 for (i = 0; i < 512; ++i) {
3794d548
AK
941 uint32_t pvr, pvr_mask;
942
27ac3e06 943 pvr_mask = ldl_be_phys(&address_space_memory, list);
152ef803
DG
944 pvr = ldl_be_phys(&address_space_memory, list + 4);
945 list += 8;
946
3794d548 947 if (~pvr_mask & pvr) {
152ef803 948 break; /* Terminator record */
3794d548 949 }
152ef803
DG
950
951 if ((cpu->env.spr[SPR_PVR] & pvr_mask) == (pvr & pvr_mask)) {
952 explicit_match = true;
953 } else {
954 if (ppc_check_compat(cpu, pvr, best_compat, max_compat)) {
955 best_compat = pvr;
956 }
957 }
958 }
959
960 if ((best_compat == 0) && (!explicit_match || max_compat)) {
961 /* We couldn't find a suitable compatibility mode, and either
962 * the guest doesn't support "raw" mode for this CPU, or raw
963 * mode is disabled because a maximum compat mode is set */
964 return H_HARDWARE;
3794d548
AK
965 }
966
3794d548 967 /* Parsing finished */
152ef803 968 trace_spapr_cas_pvr(cpu->compat_pvr, explicit_match, best_compat);
3794d548
AK
969
970 /* Update CPUs */
152ef803 971 if (cpu->compat_pvr != best_compat) {
f6f242c7 972 Error *local_err = NULL;
3794d548 973
f6f242c7
DG
974 ppc_set_compat_all(best_compat, &local_err);
975 if (local_err) {
976 error_report_err(local_err);
977 return H_HARDWARE;
3794d548
AK
978 }
979 }
980
03d196b7
BR
981 /* For the future use: here @ov_table points to the first option vector */
982 ov_table = list;
983
facdb8b6 984 ov5_guest = spapr_ovec_parse_vector(ov_table, 5);
2a6593cb 985
facdb8b6
MR
986 /* NOTE: there are actually a number of ov5 bits where input from the
987 * guest is always zero, and the platform/QEMU enables them independently
988 * of guest input. To model these properly we'd want some sort of mask,
989 * but since they only currently apply to memory migration as defined
990 * by LoPAPR 1.1, 14.5.4.8, which QEMU doesn't implement, we don't need
6787d27b 991 * to worry about this for now.
facdb8b6 992 */
6787d27b
MR
993 ov5_cas_old = spapr_ovec_clone(spapr->ov5_cas);
994 /* full range of negotiated ov5 capabilities */
facdb8b6
MR
995 spapr_ovec_intersect(spapr->ov5_cas, spapr->ov5, ov5_guest);
996 spapr_ovec_cleanup(ov5_guest);
6787d27b
MR
997 /* capabilities that have been added since CAS-generated guest reset.
998 * if capabilities have since been removed, generate another reset
999 */
1000 ov5_updates = spapr_ovec_new();
1001 spapr->cas_reboot = spapr_ovec_diff(ov5_updates,
1002 ov5_cas_old, spapr->ov5_cas);
1003
1004 if (!spapr->cas_reboot) {
1005 spapr->cas_reboot =
5b120785 1006 (spapr_h_cas_compose_response(spapr, args[1], args[2],
6787d27b
MR
1007 ov5_updates) != 0);
1008 }
1009 spapr_ovec_cleanup(ov5_updates);
03d196b7 1010
6787d27b 1011 if (spapr->cas_reboot) {
2a6593cb
AK
1012 qemu_system_reset_request();
1013 }
1014
1015 return H_SUCCESS;
1016}
1017
7d7ba3fe
DG
1018static spapr_hcall_fn papr_hypercall_table[(MAX_HCALL_OPCODE / 4) + 1];
1019static spapr_hcall_fn kvmppc_hypercall_table[KVMPPC_HCALL_MAX - KVMPPC_HCALL_BASE + 1];
9fdf0c29
DG
1020
1021void spapr_register_hypercall(target_ulong opcode, spapr_hcall_fn fn)
1022{
39ac8455
DG
1023 spapr_hcall_fn *slot;
1024
1025 if (opcode <= MAX_HCALL_OPCODE) {
1026 assert((opcode & 0x3) == 0);
9fdf0c29 1027
39ac8455
DG
1028 slot = &papr_hypercall_table[opcode / 4];
1029 } else {
1030 assert((opcode >= KVMPPC_HCALL_BASE) && (opcode <= KVMPPC_HCALL_MAX));
9fdf0c29 1031
39ac8455
DG
1032 slot = &kvmppc_hypercall_table[opcode - KVMPPC_HCALL_BASE];
1033 }
9fdf0c29 1034
c89d5299 1035 assert(!(*slot));
39ac8455 1036 *slot = fn;
9fdf0c29
DG
1037}
1038
aa100fa4 1039target_ulong spapr_hypercall(PowerPCCPU *cpu, target_ulong opcode,
9fdf0c29
DG
1040 target_ulong *args)
1041{
28e02042
DG
1042 sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
1043
9fdf0c29
DG
1044 if ((opcode <= MAX_HCALL_OPCODE)
1045 && ((opcode & 0x3) == 0)) {
39ac8455
DG
1046 spapr_hcall_fn fn = papr_hypercall_table[opcode / 4];
1047
1048 if (fn) {
b13ce26d 1049 return fn(cpu, spapr, opcode, args);
39ac8455
DG
1050 }
1051 } else if ((opcode >= KVMPPC_HCALL_BASE) &&
1052 (opcode <= KVMPPC_HCALL_MAX)) {
1053 spapr_hcall_fn fn = kvmppc_hypercall_table[opcode - KVMPPC_HCALL_BASE];
9fdf0c29
DG
1054
1055 if (fn) {
b13ce26d 1056 return fn(cpu, spapr, opcode, args);
9fdf0c29
DG
1057 }
1058 }
1059
aaf87c66
TH
1060 qemu_log_mask(LOG_UNIMP, "Unimplemented SPAPR hcall 0x" TARGET_FMT_lx "\n",
1061 opcode);
9fdf0c29
DG
1062 return H_FUNCTION;
1063}
f43e3525 1064
83f7d43a 1065static void hypercall_register_types(void)
f43e3525
DG
1066{
1067 /* hcall-pft */
1068 spapr_register_hypercall(H_ENTER, h_enter);
1069 spapr_register_hypercall(H_REMOVE, h_remove);
1070 spapr_register_hypercall(H_PROTECT, h_protect);
6bbd5dde 1071 spapr_register_hypercall(H_READ, h_read);
39ac8455 1072
a3d0abae
DG
1073 /* hcall-bulk */
1074 spapr_register_hypercall(H_BULK_REMOVE, h_bulk_remove);
1075
ed120055
DG
1076 /* hcall-splpar */
1077 spapr_register_hypercall(H_REGISTER_VPA, h_register_vpa);
1078 spapr_register_hypercall(H_CEDE, h_cede);
1c7ad77e 1079 spapr_register_hypercall(H_SIGNAL_SYS_RESET, h_signal_sys_reset);
ed120055 1080
423576f7
TH
1081 /* processor register resource access h-calls */
1082 spapr_register_hypercall(H_SET_SPRG0, h_set_sprg0);
af08a58f 1083 spapr_register_hypercall(H_SET_DABR, h_set_dabr);
e49ff266 1084 spapr_register_hypercall(H_SET_XDABR, h_set_xdabr);
3240dd9a 1085 spapr_register_hypercall(H_PAGE_INIT, h_page_init);
423576f7
TH
1086 spapr_register_hypercall(H_SET_MODE, h_set_mode);
1087
827200a2
DG
1088 /* "debugger" hcalls (also used by SLOF). Note: We do -not- differenciate
1089 * here between the "CI" and the "CACHE" variants, they will use whatever
1090 * mapping attributes qemu is using. When using KVM, the kernel will
1091 * enforce the attributes more strongly
1092 */
1093 spapr_register_hypercall(H_LOGICAL_CI_LOAD, h_logical_load);
1094 spapr_register_hypercall(H_LOGICAL_CI_STORE, h_logical_store);
1095 spapr_register_hypercall(H_LOGICAL_CACHE_LOAD, h_logical_load);
1096 spapr_register_hypercall(H_LOGICAL_CACHE_STORE, h_logical_store);
1097 spapr_register_hypercall(H_LOGICAL_ICBI, h_logical_icbi);
1098 spapr_register_hypercall(H_LOGICAL_DCBF, h_logical_dcbf);
c73e3771 1099 spapr_register_hypercall(KVMPPC_H_LOGICAL_MEMOP, h_logical_memop);
827200a2 1100
39ac8455
DG
1101 /* qemu/KVM-PPC specific hcalls */
1102 spapr_register_hypercall(KVMPPC_H_RTAS, h_rtas);
42561bf2 1103
2a6593cb
AK
1104 /* ibm,client-architecture-support support */
1105 spapr_register_hypercall(KVMPPC_H_CAS, h_client_architecture_support);
f43e3525 1106}
83f7d43a
AF
1107
1108type_init(hypercall_register_types)
This page took 0.706083 seconds and 4 git commands to generate.