summaryrefslogtreecommitdiff
path: root/Core/CPU/x64/x64CLib.c
blob: ad9641121dc968b8265e23a14a70d90f5dea4f1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
//*************************************************************************
//*************************************************************************
//**                                                                     **
//**        (C)Copyright 1985-2009, American Megatrends, Inc.            **
//**                                                                     **
//**                       All Rights Reserved.                          **
//**                                                                     **
//**      5555 Oakbrook Parkway, Suite 200, Norcross, GA 30093           **
//**                                                                     **
//**                       Phone: (770)-246-8600                         **
//**                                                                     **
//*************************************************************************
//*************************************************************************

//*************************************************************************
// $Header: /Alaska/SOURCE/Core/Modules/x64Core/x64CLib.c 4     7/10/09 9:30a Felixp $
//
// $Revision: 4 $
//
// $Date: 7/10/09 9:30a $
//*************************************************************************
// Revision History
// ----------------
// $Log: /Alaska/SOURCE/Core/Modules/x64Core/x64CLib.c $
// 
// 4     7/10/09 9:30a Felixp
// Function headers are added.
// 
// 3     10/09/06 10:11a Felixp
// MemCpy/MemSet replaced with assembler routines
// 
// 2     8/25/06 10:52a Felixp
// memcpy, memset functions updated with the volotile qualifier to
// restrain compiler optimization.
// 
// 1     8/24/06 12:57p Felixp
// 
//*************************************************************************
//<AMI_FHDR_START>
//
// Name: x64CLib.c
//
// Description:
//  Generic CPU library functions for the x64 architecture.
//
//<AMI_FHDR_END>
//*************************************************************************

#include <efi.h>

//*************************************************************************
// Math
//*************************************************************************

//*************************************************************************
//<AMI_GHDR_START>
//
// Name: Math_64_Bit_Functions
//
// Description:
//  Math functions involving 64-bit input parameters designed to be usable on
// both 32-bit and 64-bit platforms.
//
// Fields: Header Function Description
// ------------------------------------------------------------------
// AmiLib  Shr64  Shift 64-bit value right.
// AmiLib  Shl64  Shift 64-bit value left.
// AmiLib  Div64  Divide 64-bit value by a 31-bit value.
// AmiLib  Mul64  Multiply 64-bit value by a 32-bit value.
// AmiLib  GetPowerOfTwo64 Determine the highest set bit in a 64-bit value.
//
// Notes:
//  Header details which header file contains the function prototype for
// the above functions.  Append .h to the given name.
//  
//<AMI_GHDR_END>
//*************************************************************************

//*************************************************************************
//<AMI_GHDR_START>
//
// Name: CPU_Functions
//
// Description:
//  CPU related functions defined in the AMI library.
//
// Fields: Header Function Description
// ------------------------------------------------------------------
// None   CPULib_CpuID             Perform the cpuid assembly instruction.
// None   CPULib_DisableInterrupt  Disable interrupts.
// None   CPULib_EnableInterrupt   Enable interrupts.
// None   CPULib_GetInterruptState Get current interrupt state (enabled or disabled).
// None   CPULIB_GetPageTable      Retrieve address of the page table.
// None   CPULib_LoadGdt           Load GDT.
// None   CPULib_LoadIdt           Load IDT.
// None   CPULib_LockByteDec       Lock preceeding byte.
// None   CPULib_LockByteInc       Lock following byte.
// None   CPULib_Pause             Pause CPU.
// None   CPULib_SaveGdt           Save GDT at provided location.
// None   CPULib_SaveIdt           Save IDT at provided location.
// None   DisableCacheInCR0        Disable CPU cache using CR0.
// None   EnableCacheInCR0         Enable CPU cache using CR0.
// None   EnableMachineCheck       Enable machine check exception bit in CR4.
// AmiLib GetCpuTimer              Retrieve the CPU timer's value.
// None   GetCsSegment             Retrieve the code segment.
// None   HltCpu                   Halt the CPU.
// None   WaitForever              Perform infinite loop.
// None   WaitForSemaphore         Wait for semaphore to become available. then take control.
// None   WaitUntilZero32          Wait until a 32-bit memory region becomes zero.
// None   WaitUntilZero8           Wait until an 8-bit meomry region becomes zero.
// None   WriteMsr                 Write a value to a MSR.
// None   ReadMsr                  Read value from a MSR.
// AmiLib WriteCr3                 Write a value to CR3.
// AmiLib ReadCr3                  Read value from CR3.
// None   ReadRtdsc                Retrieve the time stamp counter. 
//
// Notes:
//  Header details which header file contains the function prototype for
// the above functions.  Append .h to the given name.
//  
//<AMI_GHDR_END>
//*************************************************************************


//*************************************************************************
//<AMI_PHDR_START>
//
// Name: Shr64
//
// Description:
//  UINT64 Shr64(IN UINT64 Value, IN UINT8 Shift) shifts the 64-bit Value
// right the provided number of bits, Shift.
//
// Input:
//  IN UINT64 Value
// The value to be shifted.
//
//  IN UINT8 Shift
// The number of bits to shift right.
//
// Output:
//  UINT64 Value shifted right Shift number of bits.
//
// Modified:
//
// Referrals:
// 
// Notes:	
// 
//<AMI_PHDR_END>
//*************************************************************************
UINT64 Shr64(
	IN UINT64 Value,
	IN UINT8 Shift
	)
{
	return Value>>Shift;
}

//*************************************************************************
//<AMI_PHDR_START>
//
// Name: Shl64
//
// Description:
//  UINT64 Shl64(IN UINT64 Value, IN UINT8 Shift) shifts the 64-bit Value
// left the provided number of bits, Shift.
//
// Input:
//  IN UINT64 Value
// The value to be shifted left.
//
//  IN UINT8 Shift
// The number of bits to shift.
//
// Output:
//  UINT64 Value shifted left Shift number of bits.
//
// Modified:
//
// Referrals:
// 
// Notes:	
// 
//<AMI_PHDR_END>
//*************************************************************************
UINT64 Shl64(
	IN UINT64 Value,
	IN UINT8 Shift
	)
{
	return Value<<Shift;
}

//*************************************************************************
//<AMI_PHDR_START>
//
// Name: Div64
//
// Description:
//  UINT64 Div64(IN UINT64 Dividend, IN UINTN Divisor, 
// OUT UINTN *Remainder OPTIONAL) divides a 64-bit number, Dividend, by the
// Divisor, which can be up to 31-bits.
//
// Input:
//  IN UINT64 Dividend
// The 64-bit number to be divided.
//
//  IN UINT Divisor
// The number to divide Dividend by; may not exceed 31-bits in size.
//
//  OUT UINTN *Remainder OPTIONAL
// The remainder of the division.  Provide NULL if undesired; otherwise user
// is responsible for handling the necessary memory resources.
//
// Output:
//  UINT64 result of dividing Dividend by Divisor.
//
// Modified:
//
// Referrals:
// 
// Notes:	
// 
//<AMI_PHDR_END>
//*************************************************************************
UINT64 Div64 (
	IN UINT64	Dividend,
	IN UINTN	Divisor,	//Can only be 31 bits.
  	OUT UINTN	*Remainder OPTIONAL
  	)
{
	UINT64 	Result = Dividend/Divisor;
	if (Remainder) *Remainder=Dividend%Divisor;
  	return Result;
}

//*************************************************************************
//<AMI_PHDR_START>
//
// Name: Mul64
//
// Description:
//  UINT64 Mul64(IN UINT64 Value64, IN UINTN Value32) multiplies a 64-bit
// number by a 32-bit number and returns the 64-bit result.
//
// Input:
//  IN UINTN64 Value64
// The 64-bit number to multiply by.
//
//  IN UINTN Value32
// The 32-bit number to multiply by.
//
// Output:
//  UINT64 result of multiplying Value64 by Value32.
//
// Modified:
//
// Referrals:
// 
// Notes:	
// 
//<AMI_PHDR_END>
//*************************************************************************
UINT64 Mul64(
    IN UINT64   Value64,
    IN UINTN	Value32
    )
{
	return Value64*Value32;
}

//*************************************************************************
// Memory Operations
//*************************************************************************

VOID MemSet(VOID* pBuffer, UINTN Count, UINT8 Value);

//*************************************************************************
//<AMI_PHDR_START>
//
// Name: memset
//
// Description:
//  VOID memset(IN VOID *pBuffer, IN UINT8 Value, IN UINTN Count) is a
// wrapper for MemSet which fills Count bytes of memory in pBuffer with
// Value.
//
// Input:
//  IN VOID *pBuffer
// The starting location in memory where to begin filling.
//
//  IN UINT8 Value
// The value to fill memory with starting at pBuffer.
//
//  IN UINTN Count
// The number of bytes to fill with Value.
//
// Output:
//  VOID.
//
// Modified:
//
// Referrals:
//  MemSet
// 
// Notes:	
// 
//<AMI_PHDR_END>
//*************************************************************************
VOID memset(VOID* pBuffer, UINTN Value, UINTN Count)
{
    MemSet(pBuffer,Count,(UINT8)Value);
}

//*************************************************************************
//*************************************************************************
//**                                                                     **
//**        (C)Copyright 1985-2009, American Megatrends, Inc.            **
//**                                                                     **
//**                       All Rights Reserved.                          **
//**                                                                     **
//**      5555 Oakbrook Parkway, Suite 200, Norcross, GA 30093           **
//**                                                                     **
//**                       Phone: (770)-246-8600                         **
//**                                                                     **
//*************************************************************************
//*************************************************************************