Skip to content

Commit 267e3d8

Browse files
committed
[VIDEOPRT] Improve IntAttachToCSRSS/IntDetachFromCSRSS() prototypes (reactos#8449)
- Make `IntAttachToCSRSS()` return a BOOLEAN to distinguish calls made prior to `CsrProcess` being initialized. - Adjust the callers of `IntAttachToCSRSS()` and make them returning a proper error value if attaching failed (if `CsrProcess == NULL`). - Make `IntDetachFromCSRSS()` just take a `PKPROCESS` parameter instead a pointer to `PKPROCESS` -- the function won't need to modify its value. Adjust its callers to reflect the change.
1 parent a912f89 commit 267e3d8

File tree

4 files changed

+107
-66
lines changed

4 files changed

+107
-66
lines changed

win32ss/drivers/videoprt/dispatch.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ VideoPortWin32kCallout(
4343
if (!Win32kCallout)
4444
return;
4545

46-
/* Perform the call in the context of CSRSS */
46+
/* Perform the call in the CSRSS context */
4747
if (!CsrProcess)
4848
return;
4949

@@ -378,10 +378,8 @@ IntVideoPortDispatchOpen(
378378

379379
if (!CsrProcess)
380380
{
381-
/*
382-
* We know the first open call will be from the CSRSS process
383-
* to let us know its handle.
384-
*/
381+
/* We know the first open call is from the CSRSS process.
382+
* Get a reference to it for Int10 support. */
385383
INFO_(VIDEOPRT, "Referencing CSRSS\n");
386384
CsrProcess = (PKPROCESS)PsGetCurrentProcess();
387385
ObReferenceObject(CsrProcess);

win32ss/drivers/videoprt/int10.c

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
#define IsLowV86Mem(_Seg, _Off) ((((_Seg) << 4) + (_Off)) < (0xa0000))
3434

3535
/* Those two functions below are there so that CSRSS can't access low mem.
36-
* Expecially, MAKE IT CRASH ON NULL ACCESS */
36+
* Especially, MAKE IT CRASH ON NULL ACCESS */
3737
static
3838
VOID
3939
ProtectLowV86Mem(VOID)
@@ -44,7 +44,7 @@ ProtectLowV86Mem(VOID)
4444
NTSTATUS Status;
4545
SIZE_T ViewSize = 0xa0000 - PAGE_SIZE;
4646

47-
/* We should only do that for CSRSS. */
47+
/* We should only do that for CSRSS */
4848
ASSERT(PsGetCurrentProcess() == (PEPROCESS)CsrProcess);
4949

5050
/* Commit (again) the pages, but with PAGE_NOACCESS protection */
@@ -96,6 +96,9 @@ IntInitializeVideoAddressSpace(VOID)
9696
CHAR IVTAndBda[1024 + 256];
9797
#endif // _M_IX86
9898

99+
/* We should only do that for CSRSS */
100+
ASSERT(PsGetCurrentProcess() == (PEPROCESS)CsrProcess);
101+
99102
/* Free the 1MB pre-reserved region. In reality, ReactOS should simply support us mapping the view into the reserved area, but it doesn't. */
100103
BaseAddress = 0;
101104
ViewSize = 1024 * 1024;
@@ -216,13 +219,15 @@ IntInt10AllocateBuffer(
216219
NTSTATUS Status;
217220
#ifdef _M_IX86
218221
PVOID MemoryAddress;
219-
PKPROCESS CallingProcess = (PKPROCESS)PsGetCurrentProcess();
222+
PKPROCESS CallingProcess;
220223
KAPC_STATE ApcState;
221224
SIZE_T Size;
222225

223226
TRACE_(VIDEOPRT, "IntInt10AllocateBuffer\n");
224227

225-
IntAttachToCSRSS(&CallingProcess, &ApcState);
228+
/* Perform the call in the CSRSS context */
229+
if (!IntAttachToCSRSS(&CallingProcess, &ApcState))
230+
return ERROR_INVALID_PARAMETER;
226231

227232
Size = *Length;
228233
MemoryAddress = (PVOID)0x20000;
@@ -236,7 +241,7 @@ IntInt10AllocateBuffer(
236241
if (!NT_SUCCESS(Status))
237242
{
238243
WARN_(VIDEOPRT, "- ZwAllocateVirtualMemory failed\n");
239-
IntDetachFromCSRSS(&CallingProcess, &ApcState);
244+
IntDetachFromCSRSS(CallingProcess, &ApcState);
240245
return ERROR_NOT_ENOUGH_MEMORY;
241246
}
242247

@@ -247,20 +252,20 @@ IntInt10AllocateBuffer(
247252
&Size,
248253
MEM_RELEASE);
249254
WARN_(VIDEOPRT, "- Unacceptable memory allocated\n");
250-
IntDetachFromCSRSS(&CallingProcess, &ApcState);
255+
IntDetachFromCSRSS(CallingProcess, &ApcState);
251256
return ERROR_NOT_ENOUGH_MEMORY;
252257
}
253258

259+
IntDetachFromCSRSS(CallingProcess, &ApcState);
260+
254261
*Length = (ULONG)Size;
255262
*Seg = (USHORT)((ULONG_PTR)MemoryAddress >> 4);
256263
*Off = (USHORT)((ULONG_PTR)MemoryAddress & 0xF);
257264

258-
INFO_(VIDEOPRT, "- Segment: %x\n", (ULONG_PTR)MemoryAddress >> 4);
259-
INFO_(VIDEOPRT, "- Offset: %x\n", (ULONG_PTR)MemoryAddress & 0xF);
265+
INFO_(VIDEOPRT, "- Segment: %x\n", *Seg);
266+
INFO_(VIDEOPRT, "- Offset: %x\n", *Off);
260267
INFO_(VIDEOPRT, "- Length: %x\n", *Length);
261268

262-
IntDetachFromCSRSS(&CallingProcess, &ApcState);
263-
264269
return NO_ERROR;
265270
#else
266271
Status = x86BiosAllocateBuffer(Length, Seg, Off);
@@ -278,21 +283,24 @@ IntInt10FreeBuffer(
278283
NTSTATUS Status;
279284
#ifdef _M_IX86
280285
PVOID MemoryAddress = (PVOID)((ULONG_PTR)(Seg << 4) | Off);
281-
PKPROCESS CallingProcess = (PKPROCESS)PsGetCurrentProcess();
286+
PKPROCESS CallingProcess;
282287
KAPC_STATE ApcState;
283288
SIZE_T Size = 0;
284289

285290
TRACE_(VIDEOPRT, "IntInt10FreeBuffer\n");
286291
INFO_(VIDEOPRT, "- Segment: %x\n", Seg);
287292
INFO_(VIDEOPRT, "- Offset: %x\n", Off);
288293

289-
IntAttachToCSRSS(&CallingProcess, &ApcState);
294+
/* Perform the call in the CSRSS context */
295+
if (!IntAttachToCSRSS(&CallingProcess, &ApcState))
296+
return ERROR_INVALID_PARAMETER;
297+
290298
Status = ZwFreeVirtualMemory(NtCurrentProcess(),
291299
&MemoryAddress,
292300
&Size,
293301
MEM_RELEASE);
294302

295-
IntDetachFromCSRSS(&CallingProcess, &ApcState);
303+
IntDetachFromCSRSS(CallingProcess, &ApcState);
296304

297305
return Status;
298306
#else
@@ -311,7 +319,7 @@ IntInt10ReadMemory(
311319
IN ULONG Length)
312320
{
313321
#ifdef _M_IX86
314-
PKPROCESS CallingProcess = (PKPROCESS)PsGetCurrentProcess();
322+
PKPROCESS CallingProcess;
315323
KAPC_STATE ApcState;
316324

317325
TRACE_(VIDEOPRT, "IntInt10ReadMemory\n");
@@ -320,15 +328,17 @@ IntInt10ReadMemory(
320328
INFO_(VIDEOPRT, "- Buffer: %x\n", Buffer);
321329
INFO_(VIDEOPRT, "- Length: %x\n", Length);
322330

323-
IntAttachToCSRSS(&CallingProcess, &ApcState);
331+
/* Perform the call in the CSRSS context */
332+
if (!IntAttachToCSRSS(&CallingProcess, &ApcState))
333+
return ERROR_INVALID_PARAMETER;
324334

325335
if (IsLowV86Mem(Seg, Off))
326336
UnprotectLowV86Mem();
327337
RtlCopyMemory(Buffer, (PVOID)((ULONG_PTR)(Seg << 4) | Off), Length);
328338
if (IsLowV86Mem(Seg, Off))
329339
ProtectLowV86Mem();
330340

331-
IntDetachFromCSRSS(&CallingProcess, &ApcState);
341+
IntDetachFromCSRSS(CallingProcess, &ApcState);
332342

333343
return NO_ERROR;
334344
#else
@@ -349,7 +359,7 @@ IntInt10WriteMemory(
349359
IN ULONG Length)
350360
{
351361
#ifdef _M_IX86
352-
PKPROCESS CallingProcess = (PKPROCESS)PsGetCurrentProcess();
362+
PKPROCESS CallingProcess;
353363
KAPC_STATE ApcState;
354364

355365
TRACE_(VIDEOPRT, "IntInt10WriteMemory\n");
@@ -358,13 +368,17 @@ IntInt10WriteMemory(
358368
INFO_(VIDEOPRT, "- Buffer: %x\n", Buffer);
359369
INFO_(VIDEOPRT, "- Length: %x\n", Length);
360370

361-
IntAttachToCSRSS(&CallingProcess, &ApcState);
371+
/* Perform the call in the CSRSS context */
372+
if (!IntAttachToCSRSS(&CallingProcess, &ApcState))
373+
return ERROR_INVALID_PARAMETER;
374+
362375
if (IsLowV86Mem(Seg, Off))
363376
UnprotectLowV86Mem();
364377
RtlCopyMemory((PVOID)((ULONG_PTR)(Seg << 4) | Off), Buffer, Length);
365378
if (IsLowV86Mem(Seg, Off))
366379
ProtectLowV86Mem();
367-
IntDetachFromCSRSS(&CallingProcess, &ApcState);
380+
381+
IntDetachFromCSRSS(CallingProcess, &ApcState);
368382

369383
return NO_ERROR;
370384
#else
@@ -387,16 +401,11 @@ IntInt10CallBios(
387401
CONTEXT BiosContext;
388402
#endif
389403
NTSTATUS Status;
390-
PKPROCESS CallingProcess = (PKPROCESS)PsGetCurrentProcess();
404+
PKPROCESS CallingProcess;
391405
KAPC_STATE ApcState;
392406

393-
/* Attach to CSRSS */
394-
IntAttachToCSRSS(&CallingProcess, &ApcState);
395-
396-
/* Clear the context */
407+
/* Clear the context and fill out the BIOS arguments */
397408
RtlZeroMemory(&BiosContext, sizeof(BiosContext));
398-
399-
/* Fill out the bios arguments */
400409
BiosContext.Eax = BiosArguments->Eax;
401410
BiosContext.Ebx = BiosArguments->Ebx;
402411
BiosContext.Ecx = BiosArguments->Ecx;
@@ -407,6 +416,10 @@ IntInt10CallBios(
407416
BiosContext.SegDs = BiosArguments->SegDs;
408417
BiosContext.SegEs = BiosArguments->SegEs;
409418

419+
/* Perform the call in the CSRSS context */
420+
if (!IntAttachToCSRSS(&CallingProcess, &ApcState))
421+
return ERROR_INVALID_PARAMETER;
422+
410423
/* Do the ROM BIOS call */
411424
(void)KeWaitForMutexObject(&VideoPortInt10Mutex,
412425
Executive,
@@ -425,6 +438,8 @@ IntInt10CallBios(
425438

426439
KeReleaseMutex(&VideoPortInt10Mutex, FALSE);
427440

441+
IntDetachFromCSRSS(CallingProcess, &ApcState);
442+
428443
/* Return the arguments */
429444
BiosArguments->Eax = BiosContext.Eax;
430445
BiosArguments->Ebx = BiosContext.Ebx;
@@ -436,15 +451,7 @@ IntInt10CallBios(
436451
BiosArguments->SegDs = (USHORT)BiosContext.SegDs;
437452
BiosArguments->SegEs = (USHORT)BiosContext.SegEs;
438453

439-
/* Detach and return status */
440-
IntDetachFromCSRSS(&CallingProcess, &ApcState);
441-
442-
if (NT_SUCCESS(Status))
443-
{
444-
return NO_ERROR;
445-
}
446-
447-
return ERROR_INVALID_PARAMETER;
454+
return NT_SUCCESS(Status) ? NO_ERROR : ERROR_INVALID_PARAMETER;
448455
}
449456

450457
/* PUBLIC FUNCTIONS ***********************************************************/

win32ss/drivers/videoprt/videoprt.c

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -570,29 +570,56 @@ IntVideoPortFindAdapter(
570570
return Status;
571571
}
572572

573-
VOID
573+
/**
574+
* @brief
575+
* Attach the current thread to the CSRSS process. The caller must detach from
576+
* the process by invoking IntDetachFromCSRSS() after operating in its context.
577+
*
578+
* @param[out] CallingProcess
579+
* Pointer to a PKPROCESS variable that receives the current process.
580+
*
581+
* @param[out] ApcState
582+
* Pointer to a caller-provided KAPC_STATE structure that will be initialized.
583+
*
584+
* @return
585+
* TRUE if attachment succeeded (the CSRSS process exists); FALSE if not.
586+
**/
587+
BOOLEAN
574588
FASTCALL
575589
IntAttachToCSRSS(
576-
PKPROCESS *CallingProcess,
577-
PKAPC_STATE ApcState)
590+
_Outptr_ PKPROCESS* CallingProcess,
591+
_Out_ PKAPC_STATE ApcState)
578592
{
593+
if (!CsrProcess)
594+
return FALSE;
595+
579596
*CallingProcess = (PKPROCESS)PsGetCurrentProcess();
580597
if (*CallingProcess != CsrProcess)
581-
{
582598
KeStackAttachProcess(CsrProcess, ApcState);
583-
}
599+
return TRUE;
584600
}
585601

602+
/**
603+
* @brief
604+
* Detach the current thread from the CSRSS process. This routine is
605+
* to be invoked after a previous successful IntAttachToCSRSS() call.
606+
*
607+
* @param[in] CallingProcess
608+
* The calling process that previously invoked IntAttachToCSRSS().
609+
*
610+
* @param[in] ApcState
611+
* Pointer to the KAPC_STATE structure that was initialized by a
612+
* previous IntAttachToCSRSS() call.
613+
**/
586614
VOID
587615
FASTCALL
588616
IntDetachFromCSRSS(
589-
PKPROCESS *CallingProcess,
590-
PKAPC_STATE ApcState)
617+
_In_ PKPROCESS CallingProcess,
618+
_In_ PKAPC_STATE ApcState)
591619
{
592-
if (*CallingProcess != CsrProcess)
593-
{
620+
ASSERT(CsrProcess);
621+
if (CallingProcess != CsrProcess)
594622
KeUnstackDetachProcess(ApcState);
595-
}
596623
}
597624

598625
VOID
@@ -1154,7 +1181,7 @@ VideoPortGetRomImage(
11541181
TRACE_(VIDEOPRT, "VideoPortGetRomImage(HwDeviceExtension 0x%X Length 0x%X)\n",
11551182
HwDeviceExtension, Length);
11561183

1157-
/* If the length is zero then free the existing buffer. */
1184+
/* If the length is zero then free the existing buffer */
11581185
if (Length == 0)
11591186
{
11601187
if (RomImageBuffer != NULL)
@@ -1168,28 +1195,31 @@ VideoPortGetRomImage(
11681195
{
11691196
/*
11701197
* The DDK says we shouldn't use the legacy C0000 method but get the
1171-
* rom base address from the corresponding pci or acpi register but
1198+
* ROM base address from the corresponding PCI or ACPI register but
11721199
* lets ignore that and use C0000 anyway. We have already mapped the
1173-
* bios area into memory so we'll copy from there.
1200+
* BIOS area into memory so we'll copy from there.
11741201
*/
11751202

1176-
/* Copy the bios. */
1203+
/* Copy the BIOS */
11771204
Length = min(Length, 0x10000);
11781205
if (RomImageBuffer != NULL)
1179-
{
11801206
ExFreePool(RomImageBuffer);
1181-
}
11821207

11831208
RomImageBuffer = ExAllocatePool(PagedPool, Length);
11841209
if (RomImageBuffer == NULL)
1185-
{
11861210
return NULL;
1187-
}
1188-
1189-
IntAttachToCSRSS(&CallingProcess, &ApcState);
1190-
RtlCopyMemory(RomImageBuffer, (PUCHAR)0xC0000, Length);
1191-
IntDetachFromCSRSS(&CallingProcess, &ApcState);
11921211

1212+
/* Perform the copy in the CSRSS context */
1213+
if (IntAttachToCSRSS(&CallingProcess, &ApcState))
1214+
{
1215+
RtlCopyMemory(RomImageBuffer, (PUCHAR)0xC0000, Length);
1216+
IntDetachFromCSRSS(CallingProcess, &ApcState);
1217+
}
1218+
else
1219+
{
1220+
ExFreePool(RomImageBuffer);
1221+
RomImageBuffer = NULL;
1222+
}
11931223
return RomImageBuffer;
11941224
}
11951225
}

win32ss/drivers/videoprt/videoprt.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,11 +258,17 @@ extern KMUTEX VideoPortInt10Mutex;
258258
extern KSPIN_LOCK HwResetAdaptersLock;
259259
extern LIST_ENTRY HwResetAdaptersList;
260260

261-
VOID FASTCALL
262-
IntAttachToCSRSS(PKPROCESS *CallingProcess, PKAPC_STATE ApcState);
261+
BOOLEAN
262+
FASTCALL
263+
IntAttachToCSRSS(
264+
_Outptr_ PKPROCESS* CallingProcess,
265+
_Out_ PKAPC_STATE ApcState);
263266

264-
VOID FASTCALL
265-
IntDetachFromCSRSS(PKPROCESS *CallingProcess, PKAPC_STATE ApcState);
267+
VOID
268+
FASTCALL
269+
IntDetachFromCSRSS(
270+
_In_ PKPROCESS CallingProcess,
271+
_In_ PKAPC_STATE ApcState);
266272

267273
NTSTATUS NTAPI
268274
IntVideoPortCreateAdapterDeviceObject(

0 commit comments

Comments
 (0)