5ba38e16235896db1ed9e7716588cdc785d156df
[mirror_edk2.git] / EmbeddedPkg / TemplateCpuDxe / IA32 / Exception.c
1 /** @file
2
3 Copyright (c) 2008-2009, Apple Inc. All rights reserved.
4
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include <CpuDxe.h>
16
17
18 EFI_EXCEPTION_CALLBACK gExceptionHandlers[0x100];
19
20
21 /**
22 This function registers and enables the handler specified by InterruptHandler for a processor
23 interrupt or exception type specified by InterruptType. If InterruptHandler is NULL, then the
24 handler for the processor interrupt or exception type specified by InterruptType is uninstalled.
25 The installed handler is called once for each processor interrupt or exception.
26
27 @param InterruptType A pointer to the processor's current interrupt state. Set to TRUE if interrupts
28 are enabled and FALSE if interrupts are disabled.
29 @param InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called
30 when a processor interrupt occurs. If this parameter is NULL, then the handler
31 will be uninstalled.
32
33 @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.
34 @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was
35 previously installed.
36 @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not
37 previously installed.
38 @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported.
39
40 **/
41 EFI_STATUS
42 RegisterInterruptHandler (
43 IN EFI_EXCEPTION_TYPE InterruptType,
44 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler
45 )
46 {
47 if (InterruptType > 0xFF) {
48 return EFI_UNSUPPORTED;
49 }
50
51 if ((InterruptHandler == NULL) && (gExceptionHandlers[InterruptType] == NULL)) {
52 return EFI_INVALID_PARAMETER;
53 }
54
55 if ((InterruptHandler != NULL) && (gExceptionHandlers[InterruptType] != NULL)) {
56 return EFI_ALREADY_STARTED;
57 }
58
59 gExceptionHandlers[InterruptType] = InterruptHandler;
60
61 return EFI_SUCCESS;
62 }
63
64
65
66
67 VOID
68 EFIAPI
69 DefaultExceptionHandler (
70 IN EFI_EXCEPTION_TYPE ExceptionType,
71 IN OUT EFI_SYSTEM_CONTEXT SystemContext
72 )
73 {
74 DEBUG ((EFI_D_ERROR, "Exception %d from %x\n", ExceptionType, SystemContext.SystemContextIa32->Eip));
75 ASSERT (FALSE);
76
77 return;
78 }
79
80
81
82 EFI_STATUS
83 InitializeExceptions (
84 IN EFI_CPU_ARCH_PROTOCOL *Cpu
85 )
86 {
87 // You need to initialize gExceptionHandlers[] to point to DefaultExceptionHandler()
88 // and write all the assembly to handle the interrupts.
89 ASSERT (FALSE);
90 return EFI_UNSUPPORTED;
91 }
92
93
94
95 /**
96 This function reads the processor timer specified by TimerIndex and returns it in TimerValue.
97
98 @param TimerIndex Specifies which processor timer is to be returned in TimerValue. This parameter
99 must be between 0 and NumberOfTimers-1.
100 @param TimerValue Pointer to the returned timer value.
101 @param TimerPeriod A pointer to the amount of time that passes in femtoseconds for each increment
102 of TimerValue.
103
104 @retval EFI_SUCCESS The processor timer value specified by TimerIndex was returned in TimerValue.
105 @retval EFI_DEVICE_ERROR An error occurred attempting to read one of the processor's timers.
106 @retval EFI_INVALID_PARAMETER TimerValue is NULL or TimerIndex is not valid.
107 @retval EFI_UNSUPPORTED The processor does not have any readable timers.
108
109 **/
110 EFI_STATUS
111 EFIAPI
112 GetTimerValue (
113 IN UINT32 TimerIndex,
114 OUT UINT64 *TimerValue,
115 OUT UINT64 *TimerPeriod OPTIONAL
116 )
117 {
118 if (TimerValue == NULL) {
119 return EFI_INVALID_PARAMETER;
120 }
121
122 if (TimerIndex == 0) {
123 *TimerValue = AsmReadTsc ();
124 if (TimerPeriod != NULL) {
125 //
126 // BugBug: Hard coded. Don't know how to do this generically
127 //
128 *TimerPeriod = 1000000000;
129 }
130 return EFI_SUCCESS;
131 }
132 return EFI_INVALID_PARAMETER;
133 }
134
135
136 /**
137 This function flushes the range of addresses from Start to Start+Length
138 from the processor's data cache. If Start is not aligned to a cache line
139 boundary, then the bytes before Start to the preceding cache line boundary
140 are also flushed. If Start+Length is not aligned to a cache line boundary,
141 then the bytes past Start+Length to the end of the next cache line boundary
142 are also flushed. The FlushType of EfiCpuFlushTypeWriteBackInvalidate must be
143 supported. If the data cache is fully coherent with all DMA operations, then
144 this function can just return EFI_SUCCESS. If the processor does not support
145 flushing a range of the data cache, then the entire data cache can be flushed.
146
147 @param Start The beginning physical address to flush from the processor's data
148 cache.
149 @param Length The number of bytes to flush from the processor's data cache. This
150 function may flush more bytes than Length specifies depending upon
151 the granularity of the flush operation that the processor supports.
152 @param FlushType Specifies the type of flush operation to perform.
153
154 @retval EFI_SUCCESS The address range from Start to Start+Length was flushed from
155 the processor's data cache.
156 @retval EFI_UNSUPPORTED The processor does not support the cache flush type specified
157 by FlushType.
158 @retval EFI_DEVICE_ERROR The address range from Start to Start+Length could not be flushed
159 from the processor's data cache.
160
161 **/
162 EFI_STATUS
163 EFIAPI
164 FlushCpuDataCache (
165 IN EFI_PHYSICAL_ADDRESS Start,
166 IN UINT64 Length,
167 IN EFI_CPU_FLUSH_TYPE FlushType
168 )
169 {
170 if (FlushType == EfiCpuFlushTypeWriteBackInvalidate) {
171 AsmWbinvd ();
172 return EFI_SUCCESS;
173 } else if (FlushType == EfiCpuFlushTypeInvalidate) {
174 AsmInvd ();
175 return EFI_SUCCESS;
176 } else {
177 return EFI_UNSUPPORTED;
178 }
179 }
180
181
182
183