Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / pigweed / repo / pw_cpu_exception_cortex_m / py / pw_cpu_exception_cortex_m / cortex_m_constants.py
1 # Copyright 2020 The Pigweed Authors
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 # use this file except in compliance with the License. You may obtain a copy of
5 # the License at
6 #
7 #     https://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations under
13 # the License.
14 """Cortex-M architecture related constants."""
15
16 import collections
17
18 # Cortex-M (ARMv7-M + ARMv8-M) related constants.
19 # These values are from the ARMv7-M Architecture Reference Manual DDI 0403E.b
20 # and ARMv8-M Architecture Reference Manual DDI 0553A.e.
21 # https =//static.docs.arm.com/ddi0403/e/DDI0403E_B_armv7m_arm.pdf
22 # https =//static.docs.arm.com/ddi0553/a/DDI0553A_e_armv8m_arm.pdf
23
24 # Exception ISR number. (ARMv7-M Section B1.5.2)
25 # When the ISR number (accessible from ICSR and PSR) is zero, it indicates the
26 # core is in thread mode.
27 PW_CORTEX_M_THREAD_MODE_ISR_NUM = 0x0
28 PW_CORTEX_M_NMI_ISR_NUM = 0x2
29 PW_CORTEX_M_HARD_FAULT_ISR_NUM = 0x3
30 PW_CORTEX_M_MEM_FAULT_ISR_NUM = 0x4
31 PW_CORTEX_M_BUS_FAULT_ISR_NUM = 0x5
32 PW_CORTEX_M_USAGE_FAULT_ISR_NUM = 0x6
33
34 # Masks for Interrupt Control and State Register ICSR (ARMv7-M Section B3.2.4)
35 PW_CORTEX_M_ICSR_VECTACTIVE_MASK = (1 << 9) - 1
36
37 # Masks for individual bits of HFSR. (ARMv7-M Section B3.2.16)
38 PW_CORTEX_M_HFSR_FORCED_MASK = 0x1 << 30
39
40 # Masks for different sections of CFSR. (ARMv7-M Section B3.2.15)
41 PW_CORTEX_M_CFSR_MEM_FAULT_MASK = 0x000000ff
42 PW_CORTEX_M_CFSR_BUS_FAULT_MASK = 0x0000ff00
43 PW_CORTEX_M_CFSR_USAGE_FAULT_MASK = 0xffff0000
44
45 # Masks for individual bits of CFSR. (ARMv7-M Section B3.2.15)
46 # Memory faults (MemManage Status Register) =
47 PW_CORTEX_M_CFSR_MEM_FAULT_START = (0x1)
48 PW_CORTEX_M_CFSR_IACCVIOL_MASK = (PW_CORTEX_M_CFSR_MEM_FAULT_START << 0)
49 PW_CORTEX_M_CFSR_DACCVIOL_MASK = (PW_CORTEX_M_CFSR_MEM_FAULT_START << 1)
50 PW_CORTEX_M_CFSR_MUNSTKERR_MASK = (PW_CORTEX_M_CFSR_MEM_FAULT_START << 3)
51 PW_CORTEX_M_CFSR_MSTKERR_MASK = (PW_CORTEX_M_CFSR_MEM_FAULT_START << 4)
52 PW_CORTEX_M_CFSR_MLSPERR_MASK = (PW_CORTEX_M_CFSR_MEM_FAULT_START << 5)
53 PW_CORTEX_M_CFSR_MMARVALID_MASK = (PW_CORTEX_M_CFSR_MEM_FAULT_START << 7)
54 # Bus faults (BusFault Status Register) =
55 PW_CORTEX_M_CFSR_BUS_FAULT_START = (0x1 << 8)
56 PW_CORTEX_M_CFSR_IBUSERR_MASK = (PW_CORTEX_M_CFSR_BUS_FAULT_START << 0)
57 PW_CORTEX_M_CFSR_PRECISERR_MASK = (PW_CORTEX_M_CFSR_BUS_FAULT_START << 1)
58 PW_CORTEX_M_CFSR_IMPRECISERR_MASK = (PW_CORTEX_M_CFSR_BUS_FAULT_START << 2)
59 PW_CORTEX_M_CFSR_UNSTKERR_MASK = (PW_CORTEX_M_CFSR_BUS_FAULT_START << 3)
60 PW_CORTEX_M_CFSR_STKERR_MASK = (PW_CORTEX_M_CFSR_BUS_FAULT_START << 4)
61 PW_CORTEX_M_CFSR_LSPERR_MASK = (PW_CORTEX_M_CFSR_BUS_FAULT_START << 5)
62 PW_CORTEX_M_CFSR_BFARVALID_MASK = (PW_CORTEX_M_CFSR_BUS_FAULT_START << 7)
63 # Usage faults (UsageFault Status Register) =
64 PW_CORTEX_M_CFSR_USAGE_FAULT_START = (0x1 << 16)
65 PW_CORTEX_M_CFSR_UNDEFINSTR_MASK = (PW_CORTEX_M_CFSR_USAGE_FAULT_START << 0)
66 PW_CORTEX_M_CFSR_INVSTATE_MASK = (PW_CORTEX_M_CFSR_USAGE_FAULT_START << 1)
67 PW_CORTEX_M_CFSR_INVPC_MASK = (PW_CORTEX_M_CFSR_USAGE_FAULT_START << 2)
68 PW_CORTEX_M_CFSR_NOCP_MASK = (PW_CORTEX_M_CFSR_USAGE_FAULT_START << 3)
69 PW_CORTEX_M_CFSR_STKOF_MASK = (PW_CORTEX_M_CFSR_USAGE_FAULT_START << 4)
70 PW_CORTEX_M_CFSR_UNALIGNED_MASK = (PW_CORTEX_M_CFSR_USAGE_FAULT_START << 8)
71 PW_CORTEX_M_CFSR_DIVBYZERO_MASK = (PW_CORTEX_M_CFSR_USAGE_FAULT_START << 9)
72
73 # TODO(amontanez): We could probably make a whole module on bit field handling
74 # in python.
75 BitField = collections.namedtuple('BitField',
76                                   ['name', 'bit_mask', 'description'])
77
78 PW_CORTEX_M_CFSR_BIT_FIELDS = [
79     BitField('IACCVIOL', PW_CORTEX_M_CFSR_IACCVIOL_MASK,
80              'MPU violation on instruction fetch.'),
81     BitField('DACCVIOL', PW_CORTEX_M_CFSR_DACCVIOL_MASK,
82              'MPU violation on memory read/write.'),
83     BitField('MUNSTKERR', PW_CORTEX_M_CFSR_MUNSTKERR_MASK,
84              'MPU violation on exception return.'),
85     BitField('MSTKERR', PW_CORTEX_M_CFSR_MSTKERR_MASK,
86              'MPU violation on exception entry.'),
87     BitField('MLSPERR', PW_CORTEX_M_CFSR_MLSPERR_MASK,
88              'FPU lazy state preservation failed.'),
89     BitField('MMARVALID', PW_CORTEX_M_CFSR_MMARVALID_MASK,
90              'MMFAR register is valid.'),
91     BitField('IBUSERR', PW_CORTEX_M_CFSR_IBUSERR_MASK,
92              'Bus fault on instruction fetch.'),
93     BitField('PRECISERR', PW_CORTEX_M_CFSR_PRECISERR_MASK,
94              'Precise bus fault.'),
95     BitField('IMPRECISERR', PW_CORTEX_M_CFSR_IMPRECISERR_MASK,
96              'Imprecise bus fault.'),
97     BitField('UNSTKERR', PW_CORTEX_M_CFSR_UNSTKERR_MASK,
98              'Hardware failure on context restore.'),
99     BitField('STKERR', PW_CORTEX_M_CFSR_STKERR_MASK,
100              'Hardware failure on context save.'),
101     BitField('LSPERR', PW_CORTEX_M_CFSR_LSPERR_MASK,
102              'FPU lazy state preservation failed.'),
103     BitField('BFARVALID', PW_CORTEX_M_CFSR_BFARVALID_MASK, 'BFAR is valid.'),
104     BitField('UNDEFINSTR', PW_CORTEX_M_CFSR_UNDEFINSTR_MASK,
105              'Encountered invalid instruction.'),
106     BitField('INVSTATE', PW_CORTEX_M_CFSR_INVSTATE_MASK,
107              ('Attempted to execute an instruction with an invalid Execution '
108               'Program Status Register (EPSR) value.')),
109     BitField('INVPC', PW_CORTEX_M_CFSR_INVPC_MASK,
110              'Program Counter (PC) is not legal.'),
111     BitField('NOCP', PW_CORTEX_M_CFSR_NOCP_MASK,
112              'Coprocessor disabled or not present.'),
113     BitField('STKOF', PW_CORTEX_M_CFSR_STKOF_MASK, 'Stack overflowed.'),
114     BitField('UNALIGNED', PW_CORTEX_M_CFSR_UNALIGNED_MASK,
115              'Unaligned load or store. (This exception can be disabled)'),
116     BitField('DIVBYZERO', PW_CORTEX_M_CFSR_DIVBYZERO_MASK, 'Divide by zero.'),
117 ]