8 #include "helper_regs.h"
11 #define HPTES_PER_GROUP 8
13 #define HPTE_V_SSIZE_SHIFT 62
14 #define HPTE_V_AVPN_SHIFT 7
15 #define HPTE_V_AVPN 0x3fffffffffffff80ULL
16 #define HPTE_V_AVPN_VAL(x) (((x) & HPTE_V_AVPN) >> HPTE_V_AVPN_SHIFT)
17 #define HPTE_V_COMPARE(x, y) (!(((x) ^ (y)) & 0xffffffffffffff80UL))
18 #define HPTE_V_BOLTED 0x0000000000000010ULL
19 #define HPTE_V_LOCK 0x0000000000000008ULL
20 #define HPTE_V_LARGE 0x0000000000000004ULL
21 #define HPTE_V_SECONDARY 0x0000000000000002ULL
22 #define HPTE_V_VALID 0x0000000000000001ULL
24 #define HPTE_R_PP0 0x8000000000000000ULL
25 #define HPTE_R_TS 0x4000000000000000ULL
26 #define HPTE_R_KEY_HI 0x3000000000000000ULL
27 #define HPTE_R_RPN_SHIFT 12
28 #define HPTE_R_RPN 0x3ffffffffffff000ULL
29 #define HPTE_R_FLAGS 0x00000000000003ffULL
30 #define HPTE_R_PP 0x0000000000000003ULL
31 #define HPTE_R_N 0x0000000000000004ULL
32 #define HPTE_R_G 0x0000000000000008ULL
33 #define HPTE_R_M 0x0000000000000010ULL
34 #define HPTE_R_I 0x0000000000000020ULL
35 #define HPTE_R_W 0x0000000000000040ULL
36 #define HPTE_R_WIMG 0x0000000000000078ULL
37 #define HPTE_R_C 0x0000000000000080ULL
38 #define HPTE_R_R 0x0000000000000100ULL
39 #define HPTE_R_KEY_LO 0x0000000000000e00ULL
41 #define HPTE_V_1TB_SEG 0x4000000000000000ULL
42 #define HPTE_V_VRMA_MASK 0x4001ffffff000000ULL
44 #define HPTE_V_HVLOCK 0x40ULL
46 static inline int lock_hpte(void *hpte
, target_ulong bits
)
52 /* We're protected by qemu's global lock here */
56 stq_p(hpte
, pteh
| HPTE_V_HVLOCK
);
60 static target_ulong
compute_tlbie_rb(target_ulong v
, target_ulong r
,
61 target_ulong pte_index
)
63 target_ulong rb
, va_low
;
65 rb
= (v
& ~0x7fULL
) << 16; /* AVA field */
66 va_low
= pte_index
>> 3;
67 if (v
& HPTE_V_SECONDARY
) {
70 /* xor vsid from AVA */
71 if (!(v
& HPTE_V_1TB_SEG
)) {
77 if (v
& HPTE_V_LARGE
) {
78 rb
|= 1; /* L field */
79 #if 0 /* Disable that P7 specific bit for now */
81 /* non-16MB large page, must be 64k */
82 /* (masks depend on page size) */
83 rb
|= 0x1000; /* page encoding in LP field */
84 rb
|= (va_low
& 0x7f) << 16; /* 7b of VA in AVA/LP field */
85 rb
|= (va_low
& 0xfe); /* AVAL field */
90 rb
|= (va_low
& 0x7ff) << 12; /* remaining 11b of AVA */
92 rb
|= (v
>> 54) & 0x300; /* B field */
96 static target_ulong
h_enter(CPUState
*env
, sPAPREnvironment
*spapr
,
97 target_ulong opcode
, target_ulong
*args
)
99 target_ulong flags
= args
[0];
100 target_ulong pte_index
= args
[1];
101 target_ulong pteh
= args
[2];
102 target_ulong ptel
= args
[3];
107 /* only handle 4k and 16M pages for now */
109 if (pteh
& HPTE_V_LARGE
) {
110 #if 0 /* We don't support 64k pages yet */
111 if ((ptel
& 0xf000) == 0x1000) {
116 if ((ptel
& 0xff000) == 0) {
119 /* lowest AVA bit must be 0 for 16M pages */
128 pa
= ptel
& HPTE_R_RPN
;
129 /* FIXME: bounds check the pa? */
132 if ((ptel
& HPTE_R_WIMG
) != HPTE_R_M
) {
137 if ((pte_index
* HASH_PTE_SIZE_64
) & ~env
->htab_mask
) {
140 if (likely((flags
& H_EXACT
) == 0)) {
142 hpte
= env
->external_htab
+ (pte_index
* HASH_PTE_SIZE_64
);
147 if (((ldq_p(hpte
) & HPTE_V_VALID
) == 0) &&
148 lock_hpte(hpte
, HPTE_V_HVLOCK
| HPTE_V_VALID
)) {
151 hpte
+= HASH_PTE_SIZE_64
;
155 hpte
= env
->external_htab
+ (pte_index
* HASH_PTE_SIZE_64
);
156 if (!lock_hpte(hpte
, HPTE_V_HVLOCK
| HPTE_V_VALID
)) {
160 stq_p(hpte
+ (HASH_PTE_SIZE_64
/2), ptel
);
161 /* eieio(); FIXME: need some sort of barrier for smp? */
164 assert(!(ldq_p(hpte
) & HPTE_V_HVLOCK
));
165 args
[0] = pte_index
+ i
;
169 static target_ulong
h_remove(CPUState
*env
, sPAPREnvironment
*spapr
,
170 target_ulong opcode
, target_ulong
*args
)
172 target_ulong flags
= args
[0];
173 target_ulong pte_index
= args
[1];
174 target_ulong avpn
= args
[2];
176 target_ulong v
, r
, rb
;
178 if ((pte_index
* HASH_PTE_SIZE_64
) & ~env
->htab_mask
) {
182 hpte
= env
->external_htab
+ (pte_index
* HASH_PTE_SIZE_64
);
183 while (!lock_hpte(hpte
, HPTE_V_HVLOCK
)) {
184 /* We have no real concurrency in qemu soft-emulation, so we
185 * will never actually have a contested lock */
190 r
= ldq_p(hpte
+ (HASH_PTE_SIZE_64
/2));
192 if ((v
& HPTE_V_VALID
) == 0 ||
193 ((flags
& H_AVPN
) && (v
& ~0x7fULL
) != avpn
) ||
194 ((flags
& H_ANDCOND
) && (v
& avpn
) != 0)) {
195 stq_p(hpte
, v
& ~HPTE_V_HVLOCK
);
196 assert(!(ldq_p(hpte
) & HPTE_V_HVLOCK
));
199 args
[0] = v
& ~HPTE_V_HVLOCK
;
202 rb
= compute_tlbie_rb(v
, r
, pte_index
);
203 ppc_tlb_invalidate_one(env
, rb
);
204 assert(!(ldq_p(hpte
) & HPTE_V_HVLOCK
));
208 static target_ulong
h_protect(CPUState
*env
, sPAPREnvironment
*spapr
,
209 target_ulong opcode
, target_ulong
*args
)
211 target_ulong flags
= args
[0];
212 target_ulong pte_index
= args
[1];
213 target_ulong avpn
= args
[2];
215 target_ulong v
, r
, rb
;
217 if ((pte_index
* HASH_PTE_SIZE_64
) & ~env
->htab_mask
) {
221 hpte
= env
->external_htab
+ (pte_index
* HASH_PTE_SIZE_64
);
222 while (!lock_hpte(hpte
, HPTE_V_HVLOCK
)) {
223 /* We have no real concurrency in qemu soft-emulation, so we
224 * will never actually have a contested lock */
229 r
= ldq_p(hpte
+ (HASH_PTE_SIZE_64
/2));
231 if ((v
& HPTE_V_VALID
) == 0 ||
232 ((flags
& H_AVPN
) && (v
& ~0x7fULL
) != avpn
)) {
233 stq_p(hpte
, v
& ~HPTE_V_HVLOCK
);
234 assert(!(ldq_p(hpte
) & HPTE_V_HVLOCK
));
238 r
&= ~(HPTE_R_PP0
| HPTE_R_PP
| HPTE_R_N
|
239 HPTE_R_KEY_HI
| HPTE_R_KEY_LO
);
240 r
|= (flags
<< 55) & HPTE_R_PP0
;
241 r
|= (flags
<< 48) & HPTE_R_KEY_HI
;
242 r
|= flags
& (HPTE_R_PP
| HPTE_R_N
| HPTE_R_KEY_LO
);
243 rb
= compute_tlbie_rb(v
, r
, pte_index
);
244 stq_p(hpte
, v
& ~HPTE_V_VALID
);
245 ppc_tlb_invalidate_one(env
, rb
);
246 stq_p(hpte
+ (HASH_PTE_SIZE_64
/2), r
);
247 /* Don't need a memory barrier, due to qemu's global lock */
248 stq_p(hpte
, v
& ~HPTE_V_HVLOCK
);
249 assert(!(ldq_p(hpte
) & HPTE_V_HVLOCK
));
253 static target_ulong
h_set_dabr(CPUState
*env
, sPAPREnvironment
*spapr
,
254 target_ulong opcode
, target_ulong
*args
)
256 /* FIXME: actually implement this */
260 #define FLAGS_REGISTER_VPA 0x0000200000000000ULL
261 #define FLAGS_REGISTER_DTL 0x0000400000000000ULL
262 #define FLAGS_REGISTER_SLBSHADOW 0x0000600000000000ULL
263 #define FLAGS_DEREGISTER_VPA 0x0000a00000000000ULL
264 #define FLAGS_DEREGISTER_DTL 0x0000c00000000000ULL
265 #define FLAGS_DEREGISTER_SLBSHADOW 0x0000e00000000000ULL
267 #define VPA_MIN_SIZE 640
268 #define VPA_SIZE_OFFSET 0x4
269 #define VPA_SHARED_PROC_OFFSET 0x9
270 #define VPA_SHARED_PROC_VAL 0x2
272 static target_ulong
register_vpa(CPUState
*env
, target_ulong vpa
)
278 hcall_dprintf("Can't cope with registering a VPA at logical 0\n");
282 if (vpa
% env
->dcache_line_size
) {
285 /* FIXME: bounds check the address */
287 size
= lduw_phys(vpa
+ 0x4);
289 if (size
< VPA_MIN_SIZE
) {
293 /* VPA is not allowed to cross a page boundary */
294 if ((vpa
/ 4096) != ((vpa
+ size
- 1) / 4096)) {
300 tmp
= ldub_phys(env
->vpa
+ VPA_SHARED_PROC_OFFSET
);
301 tmp
|= VPA_SHARED_PROC_VAL
;
302 stb_phys(env
->vpa
+ VPA_SHARED_PROC_OFFSET
, tmp
);
307 static target_ulong
deregister_vpa(CPUState
*env
, target_ulong vpa
)
309 if (env
->slb_shadow
) {
313 if (env
->dispatch_trace_log
) {
321 static target_ulong
register_slb_shadow(CPUState
*env
, target_ulong addr
)
326 hcall_dprintf("Can't cope with SLB shadow at logical 0\n");
330 size
= ldl_phys(addr
+ 0x4);
335 if ((addr
/ 4096) != ((addr
+ size
- 1) / 4096)) {
343 env
->slb_shadow
= addr
;
348 static target_ulong
deregister_slb_shadow(CPUState
*env
, target_ulong addr
)
354 static target_ulong
register_dtl(CPUState
*env
, target_ulong addr
)
359 hcall_dprintf("Can't cope with DTL at logical 0\n");
363 size
= ldl_phys(addr
+ 0x4);
373 env
->dispatch_trace_log
= addr
;
374 env
->dtl_size
= size
;
379 static target_ulong
deregister_dtl(CPUState
*emv
, target_ulong addr
)
381 env
->dispatch_trace_log
= 0;
387 static target_ulong
h_register_vpa(CPUState
*env
, sPAPREnvironment
*spapr
,
388 target_ulong opcode
, target_ulong
*args
)
390 target_ulong flags
= args
[0];
391 target_ulong procno
= args
[1];
392 target_ulong vpa
= args
[2];
393 target_ulong ret
= H_PARAMETER
;
396 for (tenv
= first_cpu
; tenv
; tenv
= tenv
->next_cpu
) {
397 if (tenv
->cpu_index
== procno
) {
407 case FLAGS_REGISTER_VPA
:
408 ret
= register_vpa(tenv
, vpa
);
411 case FLAGS_DEREGISTER_VPA
:
412 ret
= deregister_vpa(tenv
, vpa
);
415 case FLAGS_REGISTER_SLBSHADOW
:
416 ret
= register_slb_shadow(tenv
, vpa
);
419 case FLAGS_DEREGISTER_SLBSHADOW
:
420 ret
= deregister_slb_shadow(tenv
, vpa
);
423 case FLAGS_REGISTER_DTL
:
424 ret
= register_dtl(tenv
, vpa
);
427 case FLAGS_DEREGISTER_DTL
:
428 ret
= deregister_dtl(tenv
, vpa
);
435 static target_ulong
h_cede(CPUState
*env
, sPAPREnvironment
*spapr
,
436 target_ulong opcode
, target_ulong
*args
)
438 env
->msr
|= (1ULL << MSR_EE
);
439 hreg_compute_hflags(env
);
440 if (!cpu_has_work(env
)) {
446 static target_ulong
h_rtas(CPUState
*env
, sPAPREnvironment
*spapr
,
447 target_ulong opcode
, target_ulong
*args
)
449 target_ulong rtas_r3
= args
[0];
450 uint32_t token
= ldl_phys(rtas_r3
);
451 uint32_t nargs
= ldl_phys(rtas_r3
+ 4);
452 uint32_t nret
= ldl_phys(rtas_r3
+ 8);
454 return spapr_rtas_call(spapr
, token
, nargs
, rtas_r3
+ 12,
455 nret
, rtas_r3
+ 12 + 4*nargs
);
458 spapr_hcall_fn papr_hypercall_table
[(MAX_HCALL_OPCODE
/ 4) + 1];
459 spapr_hcall_fn kvmppc_hypercall_table
[KVMPPC_HCALL_MAX
- KVMPPC_HCALL_BASE
];
461 void spapr_register_hypercall(target_ulong opcode
, spapr_hcall_fn fn
)
463 spapr_hcall_fn
*slot
;
465 if (opcode
<= MAX_HCALL_OPCODE
) {
466 assert((opcode
& 0x3) == 0);
468 slot
= &papr_hypercall_table
[opcode
/ 4];
470 assert((opcode
>= KVMPPC_HCALL_BASE
) && (opcode
<= KVMPPC_HCALL_MAX
));
473 slot
= &kvmppc_hypercall_table
[opcode
- KVMPPC_HCALL_BASE
];
476 assert(!(*slot
) || (fn
== *slot
));
480 target_ulong
spapr_hypercall(CPUState
*env
, target_ulong opcode
,
484 hcall_dprintf("Hypercall made with MSR[PR]=1\n");
488 if ((opcode
<= MAX_HCALL_OPCODE
)
489 && ((opcode
& 0x3) == 0)) {
490 spapr_hcall_fn fn
= papr_hypercall_table
[opcode
/ 4];
493 return fn(env
, spapr
, opcode
, args
);
495 } else if ((opcode
>= KVMPPC_HCALL_BASE
) &&
496 (opcode
<= KVMPPC_HCALL_MAX
)) {
497 spapr_hcall_fn fn
= kvmppc_hypercall_table
[opcode
- KVMPPC_HCALL_BASE
];
500 return fn(env
, spapr
, opcode
, args
);
504 hcall_dprintf("Unimplemented hcall 0x" TARGET_FMT_lx
"\n", opcode
);
508 static void hypercall_init(void)
511 spapr_register_hypercall(H_ENTER
, h_enter
);
512 spapr_register_hypercall(H_REMOVE
, h_remove
);
513 spapr_register_hypercall(H_PROTECT
, h_protect
);
516 spapr_register_hypercall(H_SET_DABR
, h_set_dabr
);
519 spapr_register_hypercall(H_REGISTER_VPA
, h_register_vpa
);
520 spapr_register_hypercall(H_CEDE
, h_cede
);
522 /* qemu/KVM-PPC specific hcalls */
523 spapr_register_hypercall(KVMPPC_H_RTAS
, h_rtas
);
525 device_init(hypercall_init
);