123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720 |
- /*++
- Copyright (c) 2013 Minoca Corp.
- This file is licensed under the terms of the GNU General Public License
- version 3. Alternative licensing terms are available. Contact
- info@minocacorp.com for details. See the LICENSE file at the root of this
- project for complete licensing information.
- Module Name:
- ktestdrv.c
- Abstract:
- This module implements the kernel test driver.
- Author:
- Evan Green 5-Nov-2013
- Environment:
- Kernel
- --*/
- //
- // ------------------------------------------------------------------- Includes
- //
- #include <minoca/kernel/driver.h>
- #include "../ktestdrv.h"
- #include "testsup.h"
- //
- // ---------------------------------------------------------------- Definitions
- //
- //
- // ------------------------------------------------------ Data Type Definitions
- //
- //
- // ----------------------------------------------- Internal Function Prototypes
- //
- VOID
- KTestUnload (
- PVOID Driver
- );
- KSTATUS
- KTestAddDevice (
- PVOID Driver,
- PCSTR DeviceId,
- PCSTR ClassId,
- PCSTR CompatibleIds,
- PVOID DeviceToken
- );
- VOID
- KTestDispatchStateChange (
- PIRP Irp,
- PVOID DeviceContext,
- PVOID IrpContext
- );
- VOID
- KTestDispatchOpen (
- PIRP Irp,
- PVOID DeviceContext,
- PVOID IrpContext
- );
- VOID
- KTestDispatchClose (
- PIRP Irp,
- PVOID DeviceContext,
- PVOID IrpContext
- );
- VOID
- KTestDispatchIo (
- PIRP Irp,
- PVOID DeviceContext,
- PVOID IrpContext
- );
- VOID
- KTestDispatchSystemControl (
- PIRP Irp,
- PVOID DeviceContext,
- PVOID IrpContext
- );
- VOID
- KTestDispatchUserControl (
- PIRP Irp,
- PVOID DeviceContext,
- PVOID IrpContext
- );
- VOID
- KTestpHandleDeviceInformationRequest (
- PIRP Irp,
- PVOID DeviceContext
- );
- //
- // -------------------------------------------------------------------- Globals
- //
- PDRIVER KTestDriver = NULL;
- PDEVICE KTestDevice = NULL;
- BOOL KTestDeviceUnloaded = FALSE;
- UUID KTestTestDeviceInformationUuid = TEST_DEVICE_INFORMATION_UUID;
- //
- // ------------------------------------------------------------------ Functions
- //
- __USED
- KSTATUS
- DriverEntry (
- PDRIVER Driver
- )
- /*++
- Routine Description:
- This routine is the entry point for the kernel stress test driver. It
- registers its other dispatch functions, and performs driver-wide
- initialization.
- Arguments:
- Driver - Supplies a pointer to the driver object.
- Return Value:
- STATUS_SUCCESS on success.
- Failure code on error.
- --*/
- {
- DRIVER_FUNCTION_TABLE FunctionTable;
- KSTATUS Status;
- ASSERT((KTestDriver == NULL) && (KTestDevice == NULL));
- Status = KTestInitializeTestSupport();
- if (!KSUCCESS(Status)) {
- goto DriverEntryEnd;
- }
- KTestDriver = Driver;
- RtlZeroMemory(&FunctionTable, sizeof(DRIVER_FUNCTION_TABLE));
- FunctionTable.Version = DRIVER_FUNCTION_TABLE_VERSION;
- FunctionTable.Unload = KTestUnload;
- FunctionTable.AddDevice = KTestAddDevice;
- FunctionTable.DispatchStateChange = KTestDispatchStateChange;
- FunctionTable.DispatchOpen = KTestDispatchOpen;
- FunctionTable.DispatchClose = KTestDispatchClose;
- FunctionTable.DispatchIo = KTestDispatchIo;
- FunctionTable.DispatchSystemControl = KTestDispatchSystemControl;
- FunctionTable.DispatchUserControl = KTestDispatchUserControl;
- Status = IoRegisterDriverFunctions(Driver, &FunctionTable);
- if (!KSUCCESS(Status)) {
- goto DriverEntryEnd;
- }
- //
- // Increment the reference count on the driver so it doesn't disappear
- // in between the device being created and enumerated. This extra reference
- // is released in add device.
- //
- IoDriverAddReference(KTestDriver);
- Status = IoCreateDevice(NULL,
- NULL,
- NULL,
- KTEST_DEVICE_NAME,
- NULL,
- NULL,
- &KTestDevice);
- if (!KSUCCESS(Status)) {
- goto DriverEntryEnd;
- }
- DriverEntryEnd:
- if (KSUCCESS(Status)) {
- RtlDebugPrint("KTest driver loaded.\n");
- }
- return Status;
- }
- VOID
- KTestUnload (
- PVOID Driver
- )
- /*++
- Routine Description:
- This routine is called before a driver is about to be unloaded from memory.
- The driver should take this opportunity to free any resources it may have
- set up in the driver entry routine.
- Arguments:
- Driver - Supplies a pointer to the driver being torn down.
- Return Value:
- None.
- --*/
- {
- RtlDebugPrint("KTest driver unloaded.\n");
- return;
- }
- KSTATUS
- KTestAddDevice (
- PVOID Driver,
- PCSTR DeviceId,
- PCSTR ClassId,
- PCSTR CompatibleIds,
- PVOID DeviceToken
- )
- /*++
- Routine Description:
- This routine is called when a device is detected for which the null device
- acts as the function driver. The driver will attach itself to the stack.
- Arguments:
- Driver - Supplies a pointer to the driver being called.
- DeviceId - Supplies a pointer to a string with the device ID.
- ClassId - Supplies a pointer to a string containing the device's class ID.
- CompatibleIds - Supplies a pointer to a string containing device IDs
- that would be compatible with this device.
- DeviceToken - Supplies an opaque token that the driver can use to identify
- the device in the system. This token should be used when attaching to
- the stack.
- Return Value:
- STATUS_SUCCESS on success.
- Failure code if the driver was unsuccessful in attaching itself.
- --*/
- {
- KSTATUS Status;
- Status = IoAttachDriverToDevice(Driver, DeviceToken, NULL);
- if (KSUCCESS(Status)) {
- //
- // On success, release the reference that was taken up in driver entry.
- // The device itself has now taken a reference on the driver.
- //
- IoDriverReleaseReference(KTestDriver);
- }
- return Status;
- }
- VOID
- KTestDispatchStateChange (
- PIRP Irp,
- PVOID DeviceContext,
- PVOID IrpContext
- )
- /*++
- Routine Description:
- This routine handles State Change IRPs.
- Arguments:
- Irp - Supplies a pointer to the I/O request packet.
- DeviceContext - Supplies the context pointer supplied by the driver when it
- attached itself to the driver stack. Presumably this pointer contains
- driver-specific device context.
- IrpContext - Supplies the context pointer supplied by the driver when
- the IRP was created.
- Return Value:
- None.
- --*/
- {
- KSTATUS Status;
- ASSERT(Irp->MajorCode == IrpMajorStateChange);
- if (Irp->Direction == IrpDown) {
- switch (Irp->MinorCode) {
- case IrpMinorStartDevice:
- //
- // Publish the test information device type.
- //
- Status = IoRegisterDeviceInformation(
- Irp->Device,
- &KTestTestDeviceInformationUuid,
- TRUE);
- IoCompleteIrp(KTestDriver, Irp, Status);
- break;
- case IrpMinorRemoveDevice:
- Status = IoRegisterDeviceInformation(
- Irp->Device,
- &KTestTestDeviceInformationUuid,
- FALSE);
- IoCompleteIrp(KTestDriver, Irp, Status);
- break;
- case IrpMinorQueryResources:
- case IrpMinorQueryChildren:
- IoCompleteIrp(KTestDriver, Irp, STATUS_SUCCESS);
- break;
- default:
- break;
- }
- }
- return;
- }
- VOID
- KTestDispatchOpen (
- PIRP Irp,
- PVOID DeviceContext,
- PVOID IrpContext
- )
- /*++
- Routine Description:
- This routine handles Open IRPs.
- Arguments:
- Irp - Supplies a pointer to the I/O request packet.
- DeviceContext - Supplies the context pointer supplied by the driver when it
- attached itself to the driver stack. Presumably this pointer contains
- driver-specific device context.
- IrpContext - Supplies the context pointer supplied by the driver when
- the IRP was created.
- Return Value:
- None.
- --*/
- {
- IoCompleteIrp(KTestDriver, Irp, STATUS_SUCCESS);
- return;
- }
- VOID
- KTestDispatchClose (
- PIRP Irp,
- PVOID DeviceContext,
- PVOID IrpContext
- )
- /*++
- Routine Description:
- This routine handles Close IRPs.
- Arguments:
- Irp - Supplies a pointer to the I/O request packet.
- DeviceContext - Supplies the context pointer supplied by the driver when it
- attached itself to the driver stack. Presumably this pointer contains
- driver-specific device context.
- IrpContext - Supplies the context pointer supplied by the driver when
- the IRP was created.
- Return Value:
- None.
- --*/
- {
- IoCompleteIrp(KTestDriver, Irp, STATUS_SUCCESS);
- return;
- }
- VOID
- KTestDispatchIo (
- PIRP Irp,
- PVOID DeviceContext,
- PVOID IrpContext
- )
- /*++
- Routine Description:
- This routine handles I/O IRPs.
- Arguments:
- Irp - Supplies a pointer to the I/O request packet.
- DeviceContext - Supplies the context pointer supplied by the driver when it
- attached itself to the driver stack. Presumably this pointer contains
- driver-specific device context.
- IrpContext - Supplies the context pointer supplied by the driver when
- the IRP was created.
- Return Value:
- None.
- --*/
- {
- RtlDebugPrint("KTestDispatchIo\n");
- return;
- }
- VOID
- KTestDispatchSystemControl (
- PIRP Irp,
- PVOID DeviceContext,
- PVOID IrpContext
- )
- /*++
- Routine Description:
- This routine handles System Control IRPs.
- Arguments:
- Irp - Supplies a pointer to the I/O request packet.
- DeviceContext - Supplies the context pointer supplied by the driver when it
- attached itself to the driver stack. Presumably this pointer contains
- driver-specific device context.
- IrpContext - Supplies the context pointer supplied by the driver when
- the IRP was created.
- Return Value:
- None.
- --*/
- {
- PVOID Context;
- PSYSTEM_CONTROL_LOOKUP Lookup;
- PFILE_PROPERTIES Properties;
- KSTATUS Status;
- Context = Irp->U.SystemControl.SystemContext;
- switch (Irp->MinorCode) {
- case IrpMinorSystemControlLookup:
- Lookup = (PSYSTEM_CONTROL_LOOKUP)Context;
- Status = STATUS_PATH_NOT_FOUND;
- if (Lookup->Root != FALSE) {
- //
- // Enable opening of the root as a single file.
- //
- Properties = Lookup->Properties;
- Properties->FileId = 0;
- Properties->Type = IoObjectCharacterDevice;
- Properties->HardLinkCount = 1;
- Properties->BlockSize = 1;
- Properties->BlockCount = 1;
- Properties->Size = 0;
- Status = STATUS_SUCCESS;
- }
- IoCompleteIrp(KTestDriver, Irp, Status);
- break;
- case IrpMinorSystemControlWriteFileProperties:
- IoCompleteIrp(KTestDriver, Irp, STATUS_SUCCESS);
- break;
- case IrpMinorSystemControlTruncate:
- IoCompleteIrp(KTestDriver, Irp, STATUS_NOT_SUPPORTED);
- break;
- case IrpMinorSystemControlDeviceInformation:
- KTestpHandleDeviceInformationRequest(Irp, DeviceContext);
- break;
- //
- // Ignore everything unrecognized.
- //
- default:
- ASSERT(FALSE);
- break;
- }
- return;
- }
- VOID
- KTestDispatchUserControl (
- PIRP Irp,
- PVOID DeviceContext,
- PVOID IrpContext
- )
- /*++
- Routine Description:
- This routine handles User Control IRPs.
- Arguments:
- Irp - Supplies a pointer to the I/O request packet.
- DeviceContext - Supplies the context pointer supplied by the driver when it
- attached itself to the driver stack. Presumably this pointer contains
- driver-specific device context.
- IrpContext - Supplies the context pointer supplied by the driver when
- the IRP was created.
- Return Value:
- None.
- --*/
- {
- KSTATUS Status;
- PVOID UserBuffer;
- UINTN UserBufferSize;
- UserBuffer = Irp->U.UserControl.UserBuffer;
- UserBufferSize = Irp->U.UserControl.UserBufferSize;
- if (KTestDeviceUnloaded != FALSE) {
- ASSERT(FALSE);
- Status = STATUS_TOO_LATE;
- goto DispatchUserControlEnd;
- }
- switch ((KTEST_REQUEST)Irp->MinorCode) {
- case KTestRequestUnload:
- KTestDeviceUnloaded = TRUE;
- KTestFlushAllTests();
- Status = IoRemoveUnreportedDevice(Irp->Device);
- break;
- case KTestRequestStartTest:
- Status = KTestStartTest(UserBuffer, UserBufferSize);
- break;
- case KTestRequestCancelTest:
- Status = KTestRequestCancellation(UserBuffer, UserBufferSize);
- break;
- case KTestRequestPoll:
- Status = KTestPoll(UserBuffer, UserBufferSize);
- break;
- //
- // Ignore everything unrecognized.
- //
- default:
- ASSERT(FALSE);
- Status = STATUS_NOT_IMPLEMENTED;
- break;
- }
- DispatchUserControlEnd:
- IoCompleteIrp(KTestDriver, Irp, Status);
- return;
- }
- //
- // --------------------------------------------------------- Internal Functions
- //
- VOID
- KTestpHandleDeviceInformationRequest (
- PIRP Irp,
- PVOID DeviceContext
- )
- /*++
- Routine Description:
- This routine handles requests to get and set device information for the
- kernel test device.
- Arguments:
- Irp - Supplies a pointer to the IRP making the request.
- DeviceContext - Supplies a pointer to the device context create for this
- kernel test device.
- Return Value:
- None. Any completion status is set in the IRP.
- --*/
- {
- PTEST_DEVICE_INFORMATION Information;
- BOOL Match;
- PSYSTEM_CONTROL_DEVICE_INFORMATION Request;
- KSTATUS Status;
- Request = Irp->U.SystemControl.SystemContext;
- //
- // If this is not a request for the partition device information, ignore it.
- //
- Match = RtlAreUuidsEqual(&(Request->Uuid), &KTestTestDeviceInformationUuid);
- if (Match == FALSE) {
- return;
- }
- //
- // Setting test device information is not supported.
- //
- if (Request->Set != FALSE) {
- Status = STATUS_ACCESS_DENIED;
- goto HandleDeviceInformationRequestEnd;
- }
- //
- // Make sure the size is large enough.
- //
- if (Request->DataSize < sizeof(TEST_DEVICE_INFORMATION)) {
- Request->DataSize = sizeof(TEST_DEVICE_INFORMATION);
- Status = STATUS_BUFFER_TOO_SMALL;
- goto HandleDeviceInformationRequestEnd;
- }
- Request->DataSize = sizeof(TEST_DEVICE_INFORMATION);
- Information = Request->Data;
- RtlZeroMemory(Information, sizeof(TEST_DEVICE_INFORMATION));
- Information->Version = TEST_DEVICE_INFORMATION_VERSION;
- Information->DeviceType = TestDeviceKernel;
- Status = STATUS_SUCCESS;
- HandleDeviceInformationRequestEnd:
- IoCompleteIrp(KTestDriver, Irp, Status);
- return;
- }
|