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 TAG('P', 'r', 'o', 'f')
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
;
105 NTSTATUS Status
= STATUS_SUCCESS
;
106 ULONG Segment
= 0, Log2
= 0;
110 if(!BufferSize
) return STATUS_INVALID_PARAMETER_7
;
112 /* Check if this is a low-memory profile */
113 if ((!BucketSize
) && (RangeBase
< (PVOID
)(0x10000)))
116 if (BufferSize
< sizeof(ULONG
)) return STATUS_INVALID_PARAMETER_7
;
118 /* This will become a segmented profile object */
119 Segment
= (ULONG
)RangeBase
;
122 /* Recalculate the bucket size */
123 BucketSize
= RangeSize
/ (BufferSize
/ sizeof(ULONG
));
125 /* Convert it to log2 */
127 while (BucketSize
>>= 1) Log2
++;
128 BucketSize
+= Log2
+ 1;
131 /* Validate bucket size */
132 if ((BucketSize
> 31) || (BucketSize
< 2))
134 DPRINT1("Bucket size invalid\n");
135 return STATUS_INVALID_PARAMETER
;
138 /* Make sure that the buckets can map the range */
139 if ((RangeSize
>> (BucketSize
- 2)) > BufferSize
)
141 DPRINT1("Bucket size too small\n");
142 return STATUS_BUFFER_TOO_SMALL
;
145 /* Make sure that the range isn't too gigantic */
146 if (((ULONG_PTR
)RangeBase
+ RangeSize
) < RangeSize
)
148 DPRINT1("Range too big\n");
149 return STATUS_BUFFER_OVERFLOW
;
152 /* Check if we were called from user-mode */
153 if(PreviousMode
!= KernelMode
)
158 /* Make sure that the handle pointer is valid */
159 ProbeForWriteHandle(ProfileHandle
);
161 /* Check if the buffer is valid */
162 ProbeForWrite(Buffer
,
166 _SEH_EXCEPT(_SEH_ExSystemExceptionFilter
)
168 Status
= _SEH_GetExceptionCode();
172 /* Bail out if we failed */
173 if(!NT_SUCCESS(Status
)) return Status
;
176 /* Check if a process was specified */
180 Status
= ObReferenceObjectByHandle(Process
,
181 PROCESS_QUERY_INFORMATION
,
186 if (!NT_SUCCESS(Status
)) return(Status
);
190 /* Segmented profile objects cannot be used system-wide */
191 if (Segment
) return STATUS_INVALID_PARAMETER
;
193 /* No process was specified, which means a System-Wide Profile */
196 /* For this, we need to check the Privilege */
197 if(!SeSinglePrivilegeCheck(SeSystemProfilePrivilege
, PreviousMode
))
199 DPRINT1("NtCreateProfile: Caller requires the SeSystemProfilePrivilege privilege!\n");
200 return STATUS_PRIVILEGE_NOT_HELD
;
204 /* Create the object */
205 InitializeObjectAttributes(&ObjectAttributes
,
210 Status
= ObCreateObject(KernelMode
,
217 sizeof(EPROFILE
) + sizeof(KPROFILE
),
219 if (!NT_SUCCESS(Status
))
221 /* Dereference the process object if it was specified */
222 if (Process
) ObDereferenceObject(Process
);
229 Profile
->RangeBase
= RangeBase
;
230 Profile
->RangeSize
= RangeSize
;
231 Profile
->Buffer
= Buffer
;
232 Profile
->BufferSize
= BufferSize
;
233 Profile
->BucketSize
= BucketSize
;
234 Profile
->LockedBufferAddress
= NULL
;
235 Profile
->Segment
= Segment
;
236 Profile
->ProfileSource
= ProfileSource
;
237 Profile
->Affinity
= Affinity
;
238 Profile
->Process
= pProcess
;
240 /* Insert into the Object Tree */
241 Status
= ObInsertObject ((PVOID
)Profile
,
247 ObDereferenceObject(Profile
);
249 /* Check for Success */
250 if (!NT_SUCCESS(Status
))
252 /* Dereference Process on failure */
253 if (pProcess
) ObDereferenceObject(pProcess
);
260 /* Copy the created handle back to the caller*/
261 *ProfileHandle
= hProfile
;
263 _SEH_EXCEPT(_SEH_ExSystemExceptionFilter
)
265 Status
= _SEH_GetExceptionCode();
275 NtQueryPerformanceCounter(OUT PLARGE_INTEGER PerformanceCounter
,
276 OUT PLARGE_INTEGER PerformanceFrequency OPTIONAL
)
278 KPROCESSOR_MODE PreviousMode
= ExGetPreviousMode();
279 LARGE_INTEGER PerfFrequency
;
280 NTSTATUS Status
= STATUS_SUCCESS
;
282 /* Check if we were called from user-mode */
283 if(PreviousMode
!= KernelMode
)
285 /* Entry SEH Block */
288 /* Make sure the counter and frequency are valid */
289 ProbeForWriteLargeInteger(PerformanceCounter
);
290 if (PerformanceFrequency
)
292 ProbeForWriteLargeInteger(PerformanceFrequency
);
295 _SEH_EXCEPT(_SEH_ExSystemExceptionFilter
)
297 Status
= _SEH_GetExceptionCode();
301 /* If the pointers are invalid, bail out */
302 if(!NT_SUCCESS(Status
)) return Status
;
305 /* Enter a new SEH Block */
308 /* Query the Kernel */
309 *PerformanceCounter
= KeQueryPerformanceCounter(&PerfFrequency
);
311 /* Return Frequency if requested */
312 if(PerformanceFrequency
) *PerformanceFrequency
= PerfFrequency
;
314 _SEH_EXCEPT(_SEH_ExSystemExceptionFilter
)
316 Status
= _SEH_GetExceptionCode();
320 /* Return status to caller */
326 NtStartProfile(IN HANDLE ProfileHandle
)
329 PKPROFILE ProfileObject
;
330 KPROCESSOR_MODE PreviousMode
= ExGetPreviousMode();
331 PVOID TempLockedBufferAddress
;
336 Status
= ObReferenceObjectByHandle(ProfileHandle
,
342 if (!NT_SUCCESS(Status
)) return(Status
);
344 /* To avoid a Race, wait on the Mutex */
345 KeWaitForSingleObject(&ExpProfileMutex
,
351 /* The Profile can still be enabled though, so handle that */
352 if (Profile
->LockedBufferAddress
)
354 /* Release our lock, dereference and return */
355 KeReleaseMutex(&ExpProfileMutex
, FALSE
);
356 ObDereferenceObject(Profile
);
357 return STATUS_PROFILING_NOT_STOPPED
;
360 /* Allocate a Kernel Profile Object. */
361 ProfileObject
= ExAllocatePoolWithTag(NonPagedPool
,
366 /* Out of memory, fail */
367 KeReleaseMutex(&ExpProfileMutex
, FALSE
);
368 ObDereferenceObject(Profile
);
369 return STATUS_INSUFFICIENT_RESOURCES
;
372 /* Allocate the Mdl Structure */
373 Profile
->Mdl
= MmCreateMdl(NULL
, Profile
->Buffer
, Profile
->BufferSize
);
375 /* Protect this in SEH as we might raise an exception */
378 /* Probe and Lock for Write Access */
379 MmProbeAndLockPages(Profile
->Mdl
, PreviousMode
, IoWriteAccess
);
381 _SEH_EXCEPT(_SEH_ExSystemExceptionFilter
)
383 /* Get the exception code */
384 Status
= _SEH_GetExceptionCode();
388 /* Fail if we raised an exception */
389 if (!NT_SUCCESS(Status
))
391 /* Release our lock, free the buffer, dereference and return */
392 KeReleaseMutex(&ExpProfileMutex
, FALSE
);
393 ObDereferenceObject(Profile
);
394 ExFreePool(ProfileObject
);
399 TempLockedBufferAddress
= MmMapLockedPages(Profile
->Mdl
, KernelMode
);
401 /* Initialize the Kernel Profile Object */
402 Profile
->ProfileObject
= ProfileObject
;
403 KeInitializeProfile(ProfileObject
,
404 (PKPROCESS
)Profile
->Process
,
408 Profile
->ProfileSource
,
411 /* Start the Profiling */
412 KeStartProfile(ProfileObject
, TempLockedBufferAddress
);
414 /* Now it's safe to save this */
415 Profile
->LockedBufferAddress
= TempLockedBufferAddress
;
417 /* Release mutex, dereference and return */
418 KeReleaseMutex(&ExpProfileMutex
, FALSE
);
419 ObDereferenceObject(Profile
);
420 return STATUS_SUCCESS
;
425 NtStopProfile(IN HANDLE ProfileHandle
)
428 KPROCESSOR_MODE PreviousMode
= ExGetPreviousMode();
433 Status
= ObReferenceObjectByHandle(ProfileHandle
,
439 if (!NT_SUCCESS(Status
)) return(Status
);
442 KeWaitForSingleObject(&ExpProfileMutex
,
448 /* Make sure the Profile Object is really Started */
449 if (!Profile
->LockedBufferAddress
)
451 Status
= STATUS_PROFILING_NOT_STARTED
;
455 /* Stop the Profile */
456 KeStopProfile(Profile
->ProfileObject
);
458 /* Unlock the Buffer */
459 MmUnmapLockedPages(Profile
->LockedBufferAddress
, Profile
->Mdl
);
460 MmUnlockPages(Profile
->Mdl
);
461 ExFreePool(Profile
->ProfileObject
);
463 /* Clear the Locked Buffer pointer, meaning the Object is Stopped */
464 Profile
->LockedBufferAddress
= NULL
;
467 /* Release Mutex, Dereference and Return */
468 KeReleaseMutex(&ExpProfileMutex
, FALSE
);
469 ObDereferenceObject(Profile
);
475 NtQueryIntervalProfile(IN KPROFILE_SOURCE ProfileSource
,
478 KPROCESSOR_MODE PreviousMode
= ExGetPreviousMode();
479 ULONG ReturnInterval
;
480 NTSTATUS Status
= STATUS_SUCCESS
;
483 /* Check if we were called from user-mode */
484 if(PreviousMode
!= KernelMode
)
486 /* Enter SEH Block */
489 /* Validate interval */
490 ProbeForWriteUlong(Interval
);
492 _SEH_EXCEPT(_SEH_ExSystemExceptionFilter
)
494 Status
= _SEH_GetExceptionCode();
498 /* If pointer was invalid, bail out */
499 if(!NT_SUCCESS(Status
)) return Status
;
502 /* Query the Interval */
503 ReturnInterval
= KeQueryIntervalProfile(ProfileSource
);
505 /* Enter SEH block for return */
508 /* Return the data */
509 *Interval
= ReturnInterval
;
511 _SEH_EXCEPT(_SEH_ExSystemExceptionFilter
)
513 Status
= _SEH_GetExceptionCode();
518 return STATUS_SUCCESS
;
523 NtSetIntervalProfile(IN ULONG Interval
,
524 IN KPROFILE_SOURCE Source
)
526 /* Let the Kernel do the job */
527 KeSetIntervalProfile(Interval
, Source
);
529 /* Nothing can go wrong */
530 return STATUS_SUCCESS
;