2 * Alpha emulation - PALcode emulation for qemu.
4 * Copyright (c) 2007 Jocelyn Mayer
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
28 static void pal_reset (CPUState
*env
);
29 /* Console handlers */
30 static void pal_console_call (CPUState
*env
, uint32_t palcode
);
31 /* OpenVMS handlers */
32 static void pal_openvms_call (CPUState
*env
, uint32_t palcode
);
33 /* UNIX / Linux handlers */
34 static void pal_unix_call (CPUState
*env
, uint32_t palcode
);
36 pal_handler_t pal_handlers
[] = {
40 .call_pal
= &pal_console_call
,
45 .call_pal
= &pal_openvms_call
,
47 /* UNIX / Linux handler */
50 .call_pal
= &pal_unix_call
,
55 /* One must explicitly check that the TB is valid and the FOE bit is reset */
56 static void update_itb (void)
58 /* This writes into a temp register, not the actual one */
61 /* This commits the TB update */
65 static void update_dtb (void);
68 /* This write into a temp register, not the actual one */
70 /* This commits the TB update */
75 static void pal_reset (CPUState
*env
)
79 static void do_swappal (CPUState
*env
, uint64_t palid
)
81 pal_handler_t
*pal_handler
;
85 pal_handler
= &pal_handlers
[palid
];
86 env
->pal_handler
= pal_handler
;
87 env
->ipr
[IPR_PAL_BASE
] = -1ULL;
88 (*pal_handler
->reset
)(env
);
91 /* Unknown identifier */
95 /* We were given the entry point address */
96 env
->pal_handler
= NULL
;
97 env
->ipr
[IPR_PAL_BASE
] = palid
;
98 env
->pc
= env
->ipr
[IPR_PAL_BASE
];
103 static void pal_console_call (CPUState
*env
, uint32_t palcode
)
107 if (palcode
< 0x00000080) {
108 /* Privileged palcodes */
109 if (!(env
->ps
>> 3)) {
110 /* TODO: generate privilege exception */
124 /* Implemented as no-op */
134 do_swappal(env
, palid
);
147 /* Implemented as no-op */
166 static void pal_openvms_call (CPUState
*env
, uint32_t palcode
)
168 uint64_t palid
, val
, oldval
;
170 if (palcode
< 0x00000080) {
171 /* Privileged palcodes */
172 if (!(env
->ps
>> 3)) {
173 /* TODO: generate privilege exception */
187 /* Implemented as no-op */
200 if (cpu_alpha_mfpr(env
, IPR_ASN
, &val
) == 0)
206 if (cpu_alpha_mtpr(env
, IPR_ASTEN
, val
, &oldval
) == 1)
212 if (cpu_alpha_mtpr(env
, IPR_ASTSR
, val
, &oldval
) == 1)
223 do_swappal(env
, palid
);
227 if (cpu_alpha_mfpr(env
, IPR_FEN
, &val
) == 0)
233 if (cpu_alpha_mtpr(env
, IPR_FEN
, val
, &oldval
) == 1)
239 if (cpu_alpha_mtpr(env
, IPR_IPIR
, val
, &oldval
) == 1)
244 if (cpu_alpha_mfpr(env
, IPR_IPL
, &val
) == 0)
250 if (cpu_alpha_mtpr(env
, IPR_IPL
, val
, &oldval
) == 1)
255 if (cpu_alpha_mfpr(env
, IPR_MCES
, &val
) == 0)
261 if (cpu_alpha_mtpr(env
, IPR_MCES
, val
, &oldval
) == 1)
266 if (cpu_alpha_mfpr(env
, IPR_PCBB
, &val
) == 0)
271 if (cpu_alpha_mfpr(env
, IPR_PRBR
, &val
) == 0)
277 if (cpu_alpha_mtpr(env
, IPR_PRBR
, val
, &oldval
) == 1)
282 if (cpu_alpha_mfpr(env
, IPR_PTBR
, &val
) == 0)
287 if (cpu_alpha_mfpr(env
, IPR_SCBB
, &val
) == 0)
293 if (cpu_alpha_mtpr(env
, IPR_SCBB
, val
, &oldval
) == 1)
299 if (cpu_alpha_mtpr(env
, IPR_SIRR
, val
, &oldval
) == 1)
304 if (cpu_alpha_mfpr(env
, IPR_SISR
, &val
) == 0)
309 if (cpu_alpha_mfpr(env
, IPR_TBCHK
, &val
) == 0)
315 if (cpu_alpha_mtpr(env
, IPR_TBIA
, val
, &oldval
) == 1)
321 if (cpu_alpha_mtpr(env
, IPR_TBIAP
, val
, &oldval
) == 1)
327 if (cpu_alpha_mtpr(env
, IPR_TBIS
, val
, &oldval
) == 1)
332 if (cpu_alpha_mfpr(env
, IPR_ESP
, &val
) == 0)
338 if (cpu_alpha_mtpr(env
, IPR_ESP
, val
, &oldval
) == 1)
343 if (cpu_alpha_mfpr(env
, IPR_SSP
, &val
) == 0)
349 if (cpu_alpha_mtpr(env
, IPR_SSP
, val
, &oldval
) == 1)
354 if (cpu_alpha_mfpr(env
, IPR_USP
, &val
) == 0)
360 if (cpu_alpha_mtpr(env
, IPR_USP
, val
, &oldval
) == 1)
366 if (cpu_alpha_mtpr(env
, IPR_TBISD
, val
, &oldval
) == 1)
372 if (cpu_alpha_mtpr(env
, IPR_TBISI
, val
, &oldval
) == 1)
377 if (cpu_alpha_mfpr(env
, IPR_ASTEN
, &val
) == 0)
382 if (cpu_alpha_mfpr(env
, IPR_ASTSR
, &val
) == 0)
387 if (cpu_alpha_mfpr(env
, IPR_VPTB
, &val
) == 0)
393 if (cpu_alpha_mtpr(env
, IPR_VPTB
, val
, &oldval
) == 1)
399 if (cpu_alpha_mtpr(env
, IPR_PERFMON
, val
, &oldval
) == 1)
405 if (cpu_alpha_mtpr(env
, IPR_DATFX
, val
, &oldval
) == 1)
413 if (cpu_alpha_mfpr(env
, IPR_WHAMI
, &val
) == 0)
439 /* Implemented as no-op */
560 static void pal_unix_call (CPUState
*env
, uint32_t palcode
)
562 uint64_t palid
, val
, oldval
;
564 if (palcode
< 0x00000080) {
565 /* Privileged palcodes */
566 if (!(env
->ps
>> 3)) {
567 /* TODO: generate privilege exception */
581 /* Implemented as no-op */
591 do_swappal(env
, palid
);
596 if (cpu_alpha_mtpr(env
, IPR_IPIR
, val
, &oldval
) == 1)
601 if (cpu_alpha_mfpr(env
, IPR_MCES
, &val
) == 0)
607 if (cpu_alpha_mtpr(env
, IPR_MCES
, val
, &oldval
) == 1)
613 if (cpu_alpha_mtpr(env
, IPR_PERFMON
, val
, &oldval
) == 1)
631 if (cpu_alpha_mtpr(env
, IPR_TBIS
, val
, &oldval
) == 1)
649 if (cpu_alpha_mtpr(env
, IPR_USP
, val
, &oldval
) == 1)
655 if (cpu_alpha_mtpr(env
, IPR_PERFMON
, val
, &oldval
) == 1)
660 if (cpu_alpha_mfpr(env
, IPR_USP
, &val
) == 0)
665 if (cpu_alpha_mfpr(env
, IPR_WHAMI
, &val
) == 0)
676 if (cpu_alpha_mfpr(env
, IPR_WHAMI
, &val
) == 0)
693 /* Implemented as no-op */
718 void call_pal (CPUState
*env
)
720 pal_handler_t
*pal_handler
= env
->pal_handler
;
722 switch (env
->exception_index
) {
724 (*pal_handler
->reset
)(env
);
727 (*pal_handler
->machine_check
)(env
);
730 (*pal_handler
->arithmetic
)(env
);
733 (*pal_handler
->interrupt
)(env
);
736 (*pal_handler
->dfault
)(env
);
738 case EXCP_DTB_MISS_PAL
:
739 (*pal_handler
->dtb_miss_pal
)(env
);
741 case EXCP_DTB_MISS_NATIVE
:
742 (*pal_handler
->dtb_miss_native
)(env
);
745 (*pal_handler
->unalign
)(env
);
748 (*pal_handler
->itb_miss
)(env
);
751 (*pal_handler
->itb_acv
)(env
);
754 (*pal_handler
->opcdec
)(env
);
757 (*pal_handler
->fen
)(env
);
760 if (env
->exception_index
>= EXCP_CALL_PAL
&&
761 env
->exception_index
< EXCP_CALL_PALP
) {
762 /* Unprivileged PAL call */
763 (*pal_handler
->call_pal
)
764 (env
, (env
->exception_index
- EXCP_CALL_PAL
) >> 6);
765 } else if (env
->exception_index
>= EXCP_CALL_PALP
&&
766 env
->exception_index
< EXCP_CALL_PALE
) {
767 /* Privileged PAL call */
768 (*pal_handler
->call_pal
)
769 (env
, ((env
->exception_index
- EXCP_CALL_PALP
) >> 6) + 0x80);
771 /* Should never happen */
775 env
->ipr
[IPR_EXC_ADDR
] &= ~1;
778 void pal_init (CPUState
*env
)
784 static uint64_t get_ptebase (CPUState
*env
, uint64_t vaddr
)
786 uint64_t virbnd
, ptbr
;
788 if ((env
->features
& FEATURE_VIRBND
)) {
789 cpu_alpha_mfpr(env
, IPR_VIRBND
, &virbnd
);
791 cpu_alpha_mfpr(env
, IPR_SYSPTBR
, &ptbr
);
793 cpu_alpha_mfpr(env
, IPR_PTBR
, &ptbr
);
795 cpu_alpha_mfpr(env
, IPR_PTBR
, &ptbr
);
801 static int get_page_bits (CPUState
*env
)
807 static int get_pte (uint64_t *pfnp
, int *zbitsp
, int *protp
,
808 uint64_t ptebase
, int page_bits
, uint64_t level
,
811 uint64_t pteaddr
, pte
, pfn
;
813 int ure
, uwe
, kre
, kwe
, foE
, foR
, foW
, v
, ret
, ar
, is_user
;
816 is_user
= mmu_idx
== MMU_USER_IDX
;
817 pteaddr
= (ptebase
<< page_bits
) + (8 * level
);
818 pte
= ldq_raw(pteaddr
);
819 /* Decode all interresting PTE fields */
821 uwe
= (pte
>> 13) & 1;
822 kwe
= (pte
>> 12) & 1;
823 ure
= (pte
>> 9) & 1;
824 kre
= (pte
>> 8) & 1;
826 foE
= (pte
>> 3) & 1;
827 foW
= (pte
>> 2) & 1;
828 foR
= (pte
>> 1) & 1;
833 /* Check access rights */
862 *zbitsp
= page_bits
+ (3 * gh
);
869 static int paddr_from_pte (uint64_t *paddr
, int *zbitsp
, int *prot
,
870 uint64_t ptebase
, int page_bits
,
871 uint64_t vaddr
, int mmu_idx
, int rw
)
873 uint64_t pfn
, page_mask
, lvl_mask
, level1
, level2
, level3
;
876 page_mask
= (1ULL << page_bits
) - 1ULL;
877 lvl_bits
= page_bits
- 3;
878 lvl_mask
= (1ULL << lvl_bits
) - 1ULL;
879 level3
= (vaddr
>> page_bits
) & lvl_mask
;
880 level2
= (vaddr
>> (page_bits
+ lvl_bits
)) & lvl_mask
;
881 level1
= (vaddr
>> (page_bits
+ (2 * lvl_bits
))) & lvl_mask
;
883 ret
= get_pte(&pfn
, NULL
, NULL
, ptebase
, page_bits
, level1
, 0, 0);
886 /* Access violation */
889 /* translation not valid */
896 ret
= get_pte(&pfn
, NULL
, NULL
, pfn
, page_bits
, level2
, 0, 0);
899 /* Access violation */
902 /* translation not valid */
909 ret
= get_pte(&pfn
, zbitsp
, prot
, pfn
, page_bits
, level3
, mmu_idx
, rw
);
911 /* Translation not valid */
913 } else if (ret
& 2) {
914 /* Access violation */
927 /* Fault on execute */
936 *paddr
= (pfn
<< page_bits
) | (vaddr
& page_mask
);
941 static int virtual_to_physical (CPUState
*env
, uint64_t *physp
,
942 int *zbitsp
, int *protp
,
943 uint64_t virtual, int mmu_idx
, int rw
)
945 uint64_t sva
, ptebase
;
946 int seg
, page_bits
, ret
;
948 sva
= ((int64_t)(virtual << (64 - VA_BITS
))) >> (64 - VA_BITS
);
952 seg
= sva
>> (VA_BITS
- 2);
953 virtual &= ~(0xFFFFFC0000000000ULL
<< (VA_BITS
- 43));
954 ptebase
= get_ptebase(env
, virtual);
955 page_bits
= get_page_bits(env
);
959 /* seg1: 3 levels of PTE */
960 ret
= paddr_from_pte(physp
, zbitsp
, protp
, ptebase
, page_bits
,
961 virtual, mmu_idx
, rw
);
964 /* seg1: 2 levels of PTE */
965 ret
= paddr_from_pte(physp
, zbitsp
, protp
, ptebase
, page_bits
,
966 virtual, mmu_idx
, rw
);
977 /* seg1: TB mapped */
978 ret
= paddr_from_pte(physp
, zbitsp
, protp
, ptebase
, page_bits
,
979 virtual, mmu_idx
, rw
);
989 /* XXX: code provision */
990 int cpu_ppc_handle_mmu_fault (CPUState
*env
, uint32_t address
, int rw
,
991 int mmu_idx
, int is_softmmu
)
993 uint64_t physical
, page_size
, end
;
994 int prot
, zbits
, ret
;
996 ret
= virtual_to_physical(env
, &physical
, &zbits
, &prot
,
997 address
, mmu_idx
, rw
);
1002 page_size
= 1ULL << zbits
;
1003 address
&= ~(page_size
- 1);
1004 /* FIXME: page_size should probably be passed to tlb_set_page,
1005 and this loop removed. */
1006 for (end
= physical
+ page_size
; physical
< end
; physical
+= 0x1000) {
1007 tlb_set_page(env
, address
, physical
, prot
, mmu_idx
,
1015 env
->exception_index
= EXCP_DFAULT
;
1016 env
->ipr
[IPR_EXC_ADDR
] = address
;
1020 env
->exception_index
= EXCP_ACCESS_VIOLATION
;
1021 env
->ipr
[IPR_EXC_ADDR
] = address
;
1025 env
->exception_index
= EXCP_FAULT_ON_READ
;
1026 env
->ipr
[IPR_EXC_ADDR
] = address
;
1030 env
->exception_index
= EXCP_FAULT_ON_EXECUTE
;
1031 env
->ipr
[IPR_EXC_ADDR
] = address
;
1034 env
->exception_index
= EXCP_FAULT_ON_WRITE
;
1035 env
->ipr
[IPR_EXC_ADDR
] = address
;
1039 /* Should never happen */
1040 env
->exception_index
= EXCP_MCHK
;
1041 env
->ipr
[IPR_EXC_ADDR
] = address
;