2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Kernel
4 * FILE: ntoskrnl/ex/profile.c
5 * PURPOSE: Support for Executive Profile Objects
6 * PROGRAMMERS: Alex Ionescu (alex@relsoft.net)
10 /* INCLUDES *****************************************************************/
16 #if defined (ALLOC_PRAGMA)
17 #pragma alloc_text(INIT, ExpInitializeProfileImplementation)
20 #define TAG_PROFILE 'forP'
22 /* GLOBALS *******************************************************************/
24 POBJECT_TYPE ExProfileObjectType
= NULL
;
25 KMUTEX ExpProfileMutex
;
27 GENERIC_MAPPING ExpProfileMapping
=
29 STANDARD_RIGHTS_READ
| PROFILE_CONTROL
,
30 STANDARD_RIGHTS_WRITE
| PROFILE_CONTROL
,
31 STANDARD_RIGHTS_EXECUTE
| PROFILE_CONTROL
,
35 /* FUNCTIONS *****************************************************************/
39 ExpDeleteProfile(PVOID ObjectBody
)
44 /* Typecast the Object */
45 Profile
= (PEPROFILE
)ObjectBody
;
47 /* Check if there if the Profile was started */
48 if (Profile
->LockedBufferAddress
)
50 /* Stop the Profile */
51 State
= KeStopProfile(Profile
->ProfileObject
);
52 ASSERT(State
!= FALSE
);
54 /* Unmap the Locked Buffer */
55 MmUnmapLockedPages(Profile
->LockedBufferAddress
, Profile
->Mdl
);
56 MmUnlockPages(Profile
->Mdl
);
57 ExFreePool(Profile
->Mdl
);
60 /* Check if a Process is associated and reference it */
61 if (Profile
->Process
) ObDereferenceObject(Profile
->Process
);
67 ExpInitializeProfileImplementation(VOID
)
69 OBJECT_TYPE_INITIALIZER ObjectTypeInitializer
;
71 DPRINT("Creating Profile Object Type\n");
73 /* Initialize the Mutex to lock the States */
74 KeInitializeMutex(&ExpProfileMutex
, 64);
76 /* Create the Event Pair Object Type */
77 RtlZeroMemory(&ObjectTypeInitializer
, sizeof(ObjectTypeInitializer
));
78 RtlInitUnicodeString(&Name
, L
"Profile");
79 ObjectTypeInitializer
.Length
= sizeof(ObjectTypeInitializer
);
80 ObjectTypeInitializer
.DefaultNonPagedPoolCharge
= sizeof(KPROFILE
);
81 ObjectTypeInitializer
.GenericMapping
= ExpProfileMapping
;
82 ObjectTypeInitializer
.PoolType
= NonPagedPool
;
83 ObjectTypeInitializer
.DeleteProcedure
= ExpDeleteProfile
;
84 ObjectTypeInitializer
.ValidAccessMask
= PROFILE_ALL_ACCESS
;
85 ObCreateObjectType(&Name
, &ObjectTypeInitializer
, NULL
, &ExProfileObjectType
);
90 NtCreateProfile(OUT PHANDLE ProfileHandle
,
91 IN HANDLE Process OPTIONAL
,
97 IN KPROFILE_SOURCE ProfileSource
,
98 IN KAFFINITY Affinity
)
103 KPROCESSOR_MODE PreviousMode
= ExGetPreviousMode();
104 OBJECT_ATTRIBUTES ObjectAttributes
;
107 ULONG_PTR Segment
= 0;
111 if(!BufferSize
) return STATUS_INVALID_PARAMETER_7
;
113 /* Check if this is a low-memory profile */
114 if ((!BucketSize
) && (RangeBase
< (PVOID
)(0x10000)))
117 if (BufferSize
< sizeof(ULONG
)) return STATUS_INVALID_PARAMETER_7
;
119 /* This will become a segmented profile object */
120 Segment
= (ULONG_PTR
)RangeBase
;
123 /* Recalculate the bucket size */
124 BucketSize
= RangeSize
/ (BufferSize
/ sizeof(ULONG
));
126 /* Convert it to log2 */
128 while (BucketSize
>>= 1) Log2
++;
129 BucketSize
+= Log2
+ 1;
132 /* Validate bucket size */
133 if ((BucketSize
> 31) || (BucketSize
< 2))
135 DPRINT1("Bucket size invalid\n");
136 return STATUS_INVALID_PARAMETER
;
139 /* Make sure that the buckets can map the range */
140 if ((RangeSize
>> (BucketSize
- 2)) > BufferSize
)
142 DPRINT1("Bucket size too small\n");
143 return STATUS_BUFFER_TOO_SMALL
;
146 /* Make sure that the range isn't too gigantic */
147 if (((ULONG_PTR
)RangeBase
+ RangeSize
) < RangeSize
)
149 DPRINT1("Range too big\n");
150 return STATUS_BUFFER_OVERFLOW
;
153 /* Check if we were called from user-mode */
154 if(PreviousMode
!= KernelMode
)
159 /* Make sure that the handle pointer is valid */
160 ProbeForWriteHandle(ProfileHandle
);
162 /* Check if the buffer is valid */
163 ProbeForWrite(Buffer
,
167 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
169 /* Return the exception code */
170 _SEH2_YIELD(return _SEH2_GetExceptionCode());
175 /* Check if a process was specified */
179 Status
= ObReferenceObjectByHandle(Process
,
180 PROCESS_QUERY_INFORMATION
,
185 if (!NT_SUCCESS(Status
)) return(Status
);
189 /* Segmented profile objects cannot be used system-wide */
190 if (Segment
) return STATUS_INVALID_PARAMETER
;
192 /* No process was specified, which means a System-Wide Profile */
195 /* For this, we need to check the Privilege */
196 if(!SeSinglePrivilegeCheck(SeSystemProfilePrivilege
, PreviousMode
))
198 DPRINT1("NtCreateProfile: Caller requires the SeSystemProfilePrivilege privilege!\n");
199 return STATUS_PRIVILEGE_NOT_HELD
;
203 /* Create the object */
204 InitializeObjectAttributes(&ObjectAttributes
,
209 Status
= ObCreateObject(KernelMode
,
216 sizeof(EPROFILE
) + sizeof(KPROFILE
),
218 if (!NT_SUCCESS(Status
))
220 /* Dereference the process object if it was specified */
221 if (pProcess
) ObDereferenceObject(pProcess
);
228 Profile
->RangeBase
= RangeBase
;
229 Profile
->RangeSize
= RangeSize
;
230 Profile
->Buffer
= Buffer
;
231 Profile
->BufferSize
= BufferSize
;
232 Profile
->BucketSize
= BucketSize
;
233 Profile
->LockedBufferAddress
= NULL
;
234 Profile
->Segment
= Segment
;
235 Profile
->ProfileSource
= ProfileSource
;
236 Profile
->Affinity
= Affinity
;
237 Profile
->Process
= pProcess
;
239 /* Insert into the Object Tree */
240 Status
= ObInsertObject ((PVOID
)Profile
,
246 ObDereferenceObject(Profile
);
248 /* Check for Success */
249 if (!NT_SUCCESS(Status
))
251 /* Dereference Process on failure */
252 if (pProcess
) ObDereferenceObject(pProcess
);
259 /* Copy the created handle back to the caller*/
260 *ProfileHandle
= hProfile
;
262 _SEH2_EXCEPT(ExSystemExceptionFilter())
264 Status
= _SEH2_GetExceptionCode();
274 NtQueryPerformanceCounter(OUT PLARGE_INTEGER PerformanceCounter
,
275 OUT PLARGE_INTEGER PerformanceFrequency OPTIONAL
)
277 KPROCESSOR_MODE PreviousMode
= ExGetPreviousMode();
278 LARGE_INTEGER PerfFrequency
;
279 NTSTATUS Status
= STATUS_SUCCESS
;
281 /* Check if we were called from user-mode */
282 if (PreviousMode
!= KernelMode
)
284 /* Entry SEH Block */
287 /* Make sure the counter and frequency are valid */
288 ProbeForWriteLargeInteger(PerformanceCounter
);
289 if (PerformanceFrequency
)
291 ProbeForWriteLargeInteger(PerformanceFrequency
);
294 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
296 /* Return the exception code */
297 _SEH2_YIELD(return _SEH2_GetExceptionCode());
302 /* Enter a new SEH Block */
305 /* Query the Kernel */
306 *PerformanceCounter
= KeQueryPerformanceCounter(&PerfFrequency
);
308 /* Return Frequency if requested */
309 if (PerformanceFrequency
) *PerformanceFrequency
= PerfFrequency
;
311 _SEH2_EXCEPT(ExSystemExceptionFilter())
313 /* Get the exception code */
314 Status
= _SEH2_GetExceptionCode();
318 /* Return status to caller */
324 NtStartProfile(IN HANDLE ProfileHandle
)
327 PKPROFILE ProfileObject
;
328 KPROCESSOR_MODE PreviousMode
= ExGetPreviousMode();
329 PVOID TempLockedBufferAddress
;
334 Status
= ObReferenceObjectByHandle(ProfileHandle
,
340 if (!NT_SUCCESS(Status
)) return(Status
);
342 /* To avoid a Race, wait on the Mutex */
343 KeWaitForSingleObject(&ExpProfileMutex
,
349 /* The Profile can still be enabled though, so handle that */
350 if (Profile
->LockedBufferAddress
)
352 /* Release our lock, dereference and return */
353 KeReleaseMutex(&ExpProfileMutex
, FALSE
);
354 ObDereferenceObject(Profile
);
355 return STATUS_PROFILING_NOT_STOPPED
;
358 /* Allocate a Kernel Profile Object. */
359 ProfileObject
= ExAllocatePoolWithTag(NonPagedPool
,
364 /* Out of memory, fail */
365 KeReleaseMutex(&ExpProfileMutex
, FALSE
);
366 ObDereferenceObject(Profile
);
367 return STATUS_INSUFFICIENT_RESOURCES
;
370 /* Allocate the Mdl Structure */
371 Profile
->Mdl
= MmCreateMdl(NULL
, Profile
->Buffer
, Profile
->BufferSize
);
373 /* Protect this in SEH as we might raise an exception */
376 /* Probe and Lock for Write Access */
377 MmProbeAndLockPages(Profile
->Mdl
, PreviousMode
, IoWriteAccess
);
379 _SEH2_EXCEPT(ExSystemExceptionFilter())
381 /* Release our lock, free the buffer, dereference and return */
382 KeReleaseMutex(&ExpProfileMutex
, FALSE
);
383 ObDereferenceObject(Profile
);
384 ExFreePool(ProfileObject
);
385 _SEH2_YIELD(return _SEH2_GetExceptionCode());
390 TempLockedBufferAddress
= MmMapLockedPages(Profile
->Mdl
, KernelMode
);
392 /* Initialize the Kernel Profile Object */
393 Profile
->ProfileObject
= ProfileObject
;
394 KeInitializeProfile(ProfileObject
,
395 (PKPROCESS
)Profile
->Process
,
399 Profile
->ProfileSource
,
402 /* Start the Profiling */
403 KeStartProfile(ProfileObject
, TempLockedBufferAddress
);
405 /* Now it's safe to save this */
406 Profile
->LockedBufferAddress
= TempLockedBufferAddress
;
408 /* Release mutex, dereference and return */
409 KeReleaseMutex(&ExpProfileMutex
, FALSE
);
410 ObDereferenceObject(Profile
);
411 return STATUS_SUCCESS
;
416 NtStopProfile(IN HANDLE ProfileHandle
)
419 KPROCESSOR_MODE PreviousMode
= ExGetPreviousMode();
424 Status
= ObReferenceObjectByHandle(ProfileHandle
,
430 if (!NT_SUCCESS(Status
)) return(Status
);
433 KeWaitForSingleObject(&ExpProfileMutex
,
439 /* Make sure the Profile Object is really Started */
440 if (!Profile
->LockedBufferAddress
)
442 Status
= STATUS_PROFILING_NOT_STARTED
;
446 /* Stop the Profile */
447 KeStopProfile(Profile
->ProfileObject
);
449 /* Unlock the Buffer */
450 MmUnmapLockedPages(Profile
->LockedBufferAddress
, Profile
->Mdl
);
451 MmUnlockPages(Profile
->Mdl
);
452 ExFreePool(Profile
->ProfileObject
);
454 /* Clear the Locked Buffer pointer, meaning the Object is Stopped */
455 Profile
->LockedBufferAddress
= NULL
;
458 /* Release Mutex, Dereference and Return */
459 KeReleaseMutex(&ExpProfileMutex
, FALSE
);
460 ObDereferenceObject(Profile
);
466 NtQueryIntervalProfile(IN KPROFILE_SOURCE ProfileSource
,
469 KPROCESSOR_MODE PreviousMode
= ExGetPreviousMode();
470 ULONG ReturnInterval
;
471 NTSTATUS Status
= STATUS_SUCCESS
;
474 /* Check if we were called from user-mode */
475 if (PreviousMode
!= KernelMode
)
477 /* Enter SEH Block */
480 /* Validate interval */
481 ProbeForWriteUlong(Interval
);
483 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
485 /* Return the exception code */
486 _SEH2_YIELD(return _SEH2_GetExceptionCode());
491 /* Query the Interval */
492 ReturnInterval
= KeQueryIntervalProfile(ProfileSource
);
494 /* Enter SEH block for return */
497 /* Return the data */
498 *Interval
= ReturnInterval
;
500 _SEH2_EXCEPT(ExSystemExceptionFilter())
502 /* Get the exception code */
503 Status
= _SEH2_GetExceptionCode();
513 NtSetIntervalProfile(IN ULONG Interval
,
514 IN KPROFILE_SOURCE Source
)
516 /* Let the Kernel do the job */
517 KeSetIntervalProfile(Interval
, Source
);
519 /* Nothing can go wrong */
520 return STATUS_SUCCESS
;