1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @ingroup ppc_exc |
---|
5 | * |
---|
6 | * @brief PowerPC Exceptions implementation. |
---|
7 | */ |
---|
8 | |
---|
9 | /* |
---|
10 | * Copyright (C) 2007 Till Straumann <strauman@slac.stanford.edu> |
---|
11 | * |
---|
12 | * Copyright (C) 2009 embedded brains GmbH. |
---|
13 | * |
---|
14 | * The license and distribution terms for this file may be |
---|
15 | * found in the file LICENSE in this distribution or at |
---|
16 | * http://www.rtems.com/license/LICENSE. |
---|
17 | * |
---|
18 | * $Id$ |
---|
19 | */ |
---|
20 | |
---|
21 | #include <string.h> |
---|
22 | |
---|
23 | #include <bsp/vectors.h> |
---|
24 | |
---|
25 | /* Offset into minimal prolog where vector number is hardcoded */ |
---|
26 | #define PPC_EXC_PROLOG_VEC_OFFSET 2 |
---|
27 | |
---|
28 | /* Symbols are defined by the linker */ |
---|
29 | extern const char ppc_exc_min_prolog_size []; |
---|
30 | extern const char ppc_exc_tgpr_clr_prolog_size []; |
---|
31 | |
---|
32 | /* Special prologue for handling register shadowing on 603-style CPUs */ |
---|
33 | extern const uint32_t ppc_exc_tgpr_clr_prolog []; |
---|
34 | |
---|
35 | /* |
---|
36 | * Classic prologue which determines the vector dynamically from the offset |
---|
37 | * address. This must only be used for classic, synchronous exceptions with a |
---|
38 | * vector offset aligned on a 256-byte boundary. |
---|
39 | */ |
---|
40 | extern const uint32_t ppc_exc_min_prolog_auto []; |
---|
41 | |
---|
42 | /* Minimal prologue templates */ |
---|
43 | extern const uint32_t ppc_exc_min_prolog_async_tmpl_std []; |
---|
44 | extern const uint32_t ppc_exc_min_prolog_sync_tmpl_std []; |
---|
45 | extern const uint32_t ppc_exc_min_prolog_async_tmpl_p405_crit []; |
---|
46 | extern const uint32_t ppc_exc_min_prolog_sync_tmpl_p405_crit []; |
---|
47 | extern const uint32_t ppc_exc_min_prolog_async_tmpl_bookE_crit []; |
---|
48 | extern const uint32_t ppc_exc_min_prolog_sync_tmpl_bookE_crit []; |
---|
49 | extern const uint32_t ppc_exc_min_prolog_sync_tmpl_e500_mchk []; |
---|
50 | extern const uint32_t ppc_exc_min_prolog_async_tmpl_e500_mchk []; |
---|
51 | extern const uint32_t ppc_exc_min_prolog_tmpl_naked []; |
---|
52 | |
---|
53 | static const uint32_t *const ppc_exc_prologue_templates [] = { |
---|
54 | [PPC_EXC_CLASSIC] = ppc_exc_min_prolog_sync_tmpl_std, |
---|
55 | [PPC_EXC_CLASSIC_ASYNC] = ppc_exc_min_prolog_async_tmpl_std, |
---|
56 | [PPC_EXC_405_CRITICAL] = ppc_exc_min_prolog_sync_tmpl_p405_crit, |
---|
57 | [PPC_EXC_405_CRITICAL_ASYNC] = ppc_exc_min_prolog_async_tmpl_p405_crit, |
---|
58 | [PPC_EXC_BOOKE_CRITICAL] = ppc_exc_min_prolog_sync_tmpl_bookE_crit, |
---|
59 | [PPC_EXC_BOOKE_CRITICAL_ASYNC] = ppc_exc_min_prolog_async_tmpl_bookE_crit, |
---|
60 | [PPC_EXC_E500_MACHCHK] = ppc_exc_min_prolog_sync_tmpl_e500_mchk, |
---|
61 | [PPC_EXC_E500_MACHCHK_ASYNC] = ppc_exc_min_prolog_async_tmpl_e500_mchk, |
---|
62 | [PPC_EXC_NAKED] = ppc_exc_min_prolog_tmpl_naked |
---|
63 | }; |
---|
64 | |
---|
65 | static bool ppc_exc_create_branch_op( |
---|
66 | unsigned vector, |
---|
67 | uint32_t *prologue, |
---|
68 | size_t prologue_size |
---|
69 | ) |
---|
70 | { |
---|
71 | static const uintptr_t BRANCH_OP_CODE = 18 << 26; |
---|
72 | static const uintptr_t BRANCH_OP_LINK = 0x1; |
---|
73 | static const uintptr_t BRANCH_OP_ABS = 0x2; |
---|
74 | static const uintptr_t BRANCH_OP_MSK = 0x3ffffff; |
---|
75 | size_t branch_op_index = prologue_size / 4 - 1; |
---|
76 | uintptr_t vector_address = (uintptr_t) ppc_exc_vector_address(vector); |
---|
77 | uintptr_t branch_op_address = vector_address + 4 * branch_op_index; |
---|
78 | |
---|
79 | /* This value may have BRANCH_OP_LINK set */ |
---|
80 | uintptr_t target_address = prologue [branch_op_index]; |
---|
81 | |
---|
82 | uintptr_t branch_target_address = target_address - branch_op_address; |
---|
83 | |
---|
84 | /* |
---|
85 | * We prefer to use a relative branch. This has the benefit that custom |
---|
86 | * minimal prologues in a read-only area are relocatable. |
---|
87 | */ |
---|
88 | if ((branch_target_address & ~BRANCH_OP_MSK) != 0) { |
---|
89 | /* Target to far for relative branch (PC ± 32M) */ |
---|
90 | if (target_address >= 0xfe000001 || target_address < 0x01fffffd) { |
---|
91 | /* Can use an absolute branch */ |
---|
92 | branch_target_address = (target_address | BRANCH_OP_ABS) & BRANCH_OP_MSK; |
---|
93 | } else { |
---|
94 | return false; |
---|
95 | } |
---|
96 | } |
---|
97 | |
---|
98 | prologue [branch_op_index] = BRANCH_OP_CODE | branch_target_address; |
---|
99 | |
---|
100 | return true; |
---|
101 | } |
---|
102 | |
---|
103 | rtems_status_code ppc_exc_make_prologue( |
---|
104 | unsigned vector, |
---|
105 | ppc_exc_category category, |
---|
106 | uint32_t *prologue, |
---|
107 | size_t *prologue_size |
---|
108 | ) |
---|
109 | { |
---|
110 | const uint32_t *prologue_template = NULL; |
---|
111 | size_t prologue_template_size = 0; |
---|
112 | bool fixup_vector = false; |
---|
113 | |
---|
114 | if (!ppc_exc_is_valid_category(category)) { |
---|
115 | return RTEMS_INVALID_NUMBER; |
---|
116 | } |
---|
117 | |
---|
118 | if ( |
---|
119 | ppc_cpu_has_shadowed_gprs() |
---|
120 | && (vector == ASM_60X_IMISS_VECTOR |
---|
121 | || vector == ASM_60X_DLMISS_VECTOR |
---|
122 | || vector == ASM_60X_DSMISS_VECTOR) |
---|
123 | ) { |
---|
124 | prologue_template = ppc_exc_tgpr_clr_prolog; |
---|
125 | prologue_template_size = (size_t) ppc_exc_tgpr_clr_prolog_size; |
---|
126 | } else if ( |
---|
127 | category == PPC_EXC_CLASSIC |
---|
128 | && ppc_cpu_is_bookE() != PPC_BOOKE_STD |
---|
129 | && ppc_cpu_is_bookE() != PPC_BOOKE_E500 |
---|
130 | ) { |
---|
131 | prologue_template = ppc_exc_min_prolog_auto; |
---|
132 | prologue_template_size = (size_t) ppc_exc_min_prolog_size; |
---|
133 | } else { |
---|
134 | prologue_template = ppc_exc_prologue_templates [category]; |
---|
135 | prologue_template_size = (size_t) ppc_exc_min_prolog_size; |
---|
136 | fixup_vector = true; |
---|
137 | } |
---|
138 | |
---|
139 | if (prologue_template_size <= *prologue_size) { |
---|
140 | *prologue_size = prologue_template_size; |
---|
141 | |
---|
142 | memcpy(prologue, prologue_template, prologue_template_size); |
---|
143 | |
---|
144 | if (!ppc_exc_create_branch_op(vector, prologue, prologue_template_size)) { |
---|
145 | return RTEMS_INVALID_ADDRESS; |
---|
146 | } |
---|
147 | |
---|
148 | if (fixup_vector) { |
---|
149 | if (vector <= 0x7fffU) { |
---|
150 | prologue [PPC_EXC_PROLOG_VEC_OFFSET] = |
---|
151 | (prologue [PPC_EXC_PROLOG_VEC_OFFSET] & 0xffff8000U) |
---|
152 | | (vector & 0x7fffU); |
---|
153 | } else { |
---|
154 | return RTEMS_INVALID_ID; |
---|
155 | } |
---|
156 | } |
---|
157 | } else { |
---|
158 | return RTEMS_INVALID_SIZE; |
---|
159 | } |
---|
160 | |
---|
161 | return RTEMS_SUCCESSFUL; |
---|
162 | } |
---|