Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

986 lines
28 KiB

  1. /*++
  2. Copyright (c) 1989-1993 Microsoft Corporation
  3. Module Name:
  4. Timer.c
  5. Abstract:
  6. This file contains the code to implement timer functions.
  7. Author:
  8. Jim Stewart (Jimst) 10-2-92
  9. Revision History:
  10. --*/
  11. #include "precomp.h"
  12. #include "timer.h"
  13. #include "ntddndis.h"
  14. // the timer Q
  15. tTIMERQ TimerQ;
  16. //******************* Pageable Routine Declarations ****************
  17. #ifdef ALLOC_PRAGMA
  18. #pragma CTEMakePageable(PAGE, InitTimerQ)
  19. #pragma CTEMakePageable(PAGE, DestroyTimerQ)
  20. #pragma CTEMakePageable(PAGE, DelayedNbtStopWakeupTimer)
  21. #endif
  22. // #pragma CTEMakePageable(PAGE, WakeupTimerExpiry)
  23. //******************* Pageable Routine Declarations ****************
  24. //----------------------------------------------------------------------------
  25. NTSTATUS
  26. InterlockedCallCompletion(
  27. IN tTIMERQENTRY *pTimer,
  28. IN NTSTATUS status
  29. )
  30. /*++
  31. Routine Description:
  32. This routine calls the completion routine if it hasn't been called
  33. yet, by first getting the JointLock spin lock and then getting the
  34. Completion routine ptr. If the ptr is null then the completion routine
  35. has already been called. Holding the Spin lock interlocks this
  36. with the timer expiry routine to prevent more than one call to the
  37. completion routine.
  38. Arguments:
  39. Return Value:
  40. there is no return value
  41. --*/
  42. {
  43. CTELockHandle OldIrq;
  44. COMPLETIONCLIENT pClientCompletion;
  45. // to synch. with the the Timer completion routines, Null the client completion
  46. // routine so it gets called just once, either from here or from the
  47. // timer completion routine setup when the timer was started.(in namesrv.c)
  48. //
  49. CTESpinLock(&NbtConfig.JointLock,OldIrq);
  50. pClientCompletion = pTimer->ClientCompletion;
  51. pTimer->ClientCompletion = NULL;
  52. if (pClientCompletion)
  53. {
  54. // remove the link from the name table to this timer block
  55. CHECK_PTR(((tNAMEADDR *)pTimer->pCacheEntry));
  56. ((tNAMEADDR *)pTimer->pCacheEntry)->pTimer = NULL;
  57. CTESpinFree(&NbtConfig.JointLock,OldIrq);
  58. (*pClientCompletion) (pTimer->ClientContext, status);
  59. return(STATUS_SUCCESS);
  60. }
  61. else
  62. {
  63. CTESpinFree(&NbtConfig.JointLock,OldIrq);
  64. }
  65. return(STATUS_UNSUCCESSFUL);
  66. }
  67. //----------------------------------------------------------------------------
  68. NTSTATUS
  69. InitTimerQ(
  70. VOID
  71. )
  72. /*++
  73. Routine Description:
  74. This routine sets up the timer Q.
  75. Arguments:
  76. Return Value:
  77. The function value is the status of the operation.
  78. --*/
  79. {
  80. tTIMERQENTRY *pTimerEntry;
  81. CTEPagedCode();
  82. InitializeListHead(&TimerQ.ActiveHead);
  83. ExInitializeNPagedLookasideList(
  84. &TimerQ.LookasideList,
  85. NULL,
  86. NULL,
  87. 0,
  88. sizeof(tTIMERQENTRY),
  89. NBT_TAG2('16'),
  90. 0
  91. );
  92. return(STATUS_SUCCESS);
  93. }
  94. //----------------------------------------------------------------------------
  95. NTSTATUS
  96. DestroyTimerQ(
  97. )
  98. /*++
  99. Routine Description:
  100. This routine clears up the TimerQEntry structures allocated
  101. Arguments:
  102. Return Value:
  103. The function value is the status of the operation.
  104. --*/
  105. {
  106. tTIMERQENTRY *pTimer;
  107. PLIST_ENTRY pEntry;
  108. CTEPagedCode();
  109. ExDeleteNPagedLookasideList(&TimerQ.LookasideList);
  110. return(STATUS_SUCCESS);
  111. }
  112. //----------------------------------------------------------------------------
  113. NTSTATUS
  114. GetTimerEntry(
  115. OUT tTIMERQENTRY **ppTimerEntry
  116. )
  117. /*++
  118. Routine Description:
  119. This routine gets a free block&TimerQ.
  120. NOTE: this function is called with the JointLock held.
  121. Arguments:
  122. Return Value:
  123. The function value is the status of the operation.
  124. --*/
  125. {
  126. PLIST_ENTRY pEntry;
  127. tTIMERQENTRY *pTimerEntry;
  128. pTimerEntry = (tTIMERQENTRY *)ExAllocateFromNPagedLookasideList(&TimerQ.LookasideList);
  129. if (NULL == pTimerEntry) {
  130. KdPrint(("Unable to allocate memory!! - for the timer Q\n"));
  131. return (STATUS_INSUFFICIENT_RESOURCES);
  132. }
  133. *ppTimerEntry = pTimerEntry;
  134. pTimerEntry->Verify = NBT_VERIFY_TIMER_ACTIVE;
  135. NbtConfig.lNumTimersRunning++;
  136. return(STATUS_SUCCESS);
  137. }
  138. //----------------------------------------------------------------------------
  139. VOID
  140. ReturnTimerToFreeQ(
  141. tTIMERQENTRY *pTimerEntry,
  142. BOOLEAN fLocked
  143. )
  144. {
  145. CTELockHandle OldIrq;
  146. if (!fLocked)
  147. {
  148. CTESpinLock(&NbtConfig.JointLock,OldIrq);
  149. }
  150. // return the timer block
  151. ASSERT (pTimerEntry->Verify == NBT_VERIFY_TIMER_ACTIVE);
  152. pTimerEntry->Verify = NBT_VERIFY_TIMER_DOWN;
  153. ExFreeToNPagedLookasideList(&TimerQ.LookasideList, pTimerEntry);
  154. if (!--NbtConfig.lNumTimersRunning)
  155. {
  156. KeSetEvent(&NbtConfig.TimerQLastEvent, 0, FALSE);
  157. }
  158. if (!fLocked)
  159. {
  160. CTESpinFree(&NbtConfig.JointLock,OldIrq);
  161. }
  162. }
  163. //----------------------------------------------------------------------------
  164. NTSTATUS
  165. CleanupCTETimer(
  166. IN tTIMERQENTRY *pTimerEntry
  167. )
  168. /*++
  169. Routine Description:
  170. This routine cleans up the timer. Called with the JointLock held.
  171. Arguments:
  172. Return Value:
  173. returns the reference count after the decrement
  174. --*/
  175. {
  176. COMPLETIONROUTINE TimeoutRoutine;
  177. PVOID Context;
  178. PVOID Context2;
  179. pTimerEntry->RefCount = 0;
  180. // the expiry routine is not currently running so we can call the
  181. // completion routine and remove the timer from the active timer Q
  182. TimeoutRoutine = (COMPLETIONROUTINE)pTimerEntry->TimeoutRoutine;
  183. pTimerEntry->TimeoutRoutine = NULL;
  184. Context = pTimerEntry->Context;
  185. Context2 = pTimerEntry->Context2;
  186. if (pTimerEntry->pDeviceContext)
  187. {
  188. NBT_DEREFERENCE_DEVICE ((tDEVICECONTEXT *) pTimerEntry->pDeviceContext, REF_DEV_TIMER, TRUE);
  189. pTimerEntry->pDeviceContext = NULL;
  190. }
  191. // release any tracker block hooked to the timer entry.. This could
  192. // be modified to not call the completion routine if there was
  193. // no context value... ie. for those timers that do not have anything
  194. // to cleanup ...however, for now we require all completion routines
  195. // to have a if (pTimerQEntry) if around the code so when it gets hit
  196. // from this call it does not access any part of pTimerQEntry.
  197. //
  198. if (TimeoutRoutine)
  199. {
  200. // call the completion routine so it can clean up its own buffers
  201. // the routine that called this one will call the client's completion
  202. // routine. A NULL timerEntry value indicates to the routine that
  203. // cleanup should be done.
  204. (VOID)(*TimeoutRoutine) (Context, Context2, NULL);
  205. }
  206. // move to the free list
  207. RemoveEntryList(&pTimerEntry->Linkage);
  208. ReturnTimerToFreeQ (pTimerEntry, TRUE);
  209. return(STATUS_SUCCESS);
  210. }
  211. //----------------------------------------------------------------------------
  212. NTSTATUS
  213. StartTimer(
  214. IN PVOID TimeoutRoutine,
  215. IN ULONG DeltaTime,
  216. IN PVOID Context,
  217. IN PVOID Context2,
  218. IN PVOID ContextClient,
  219. IN PVOID CompletionClient,
  220. IN tDEVICECONTEXT *pDeviceContext,
  221. OUT tTIMERQENTRY **ppTimerEntry,
  222. IN USHORT Retries,
  223. BOOLEAN fLocked)
  224. /*++
  225. Routine Description:
  226. This routine starts a timer.
  227. It has to be called with the JointLock held!
  228. Arguments:
  229. The value passed in is in milliseconds - must be converted to 100ns
  230. so multiply to 10,000
  231. Return Value:
  232. The function value is the status of the operation.
  233. --*/
  234. {
  235. tTIMERQENTRY *pTimerEntry;
  236. NTSTATUS status;
  237. CTELockHandle OldIrq;
  238. //
  239. // Do not allow any timers to be started if we are currently
  240. // Unloading!
  241. //
  242. if (NbtConfig.Unloading)
  243. {
  244. return STATUS_UNSUCCESSFUL;
  245. }
  246. if (!fLocked)
  247. {
  248. CTESpinLock(&NbtConfig.JointLock,OldIrq);
  249. }
  250. if ((!pDeviceContext) ||
  251. (NBT_REFERENCE_DEVICE (pDeviceContext, REF_DEV_TIMER, TRUE)))
  252. {
  253. // get a free timer block
  254. status = GetTimerEntry (&pTimerEntry);
  255. if (NT_SUCCESS(status))
  256. {
  257. pTimerEntry->DeltaTime = DeltaTime;
  258. pTimerEntry->RefCount = 1;
  259. //
  260. // this is the context value and routine called when the timer expires,
  261. // called by TimerExpiry below.
  262. //
  263. pTimerEntry->Context = Context;
  264. pTimerEntry->Context2 = Context2;
  265. pTimerEntry->TimeoutRoutine = TimeoutRoutine;
  266. pTimerEntry->Flags = 0; // no flags
  267. // now fill in the client's completion routines that ultimately get called
  268. // after one or more timeouts...
  269. pTimerEntry->ClientContext = (PVOID)ContextClient;
  270. pTimerEntry->ClientCompletion = (COMPLETIONCLIENT)CompletionClient;
  271. pTimerEntry->Retries = Retries;
  272. pTimerEntry->pDeviceContext = (PVOID) pDeviceContext;
  273. CTEInitTimer(&pTimerEntry->VxdTimer);
  274. CTEStartTimer(&pTimerEntry->VxdTimer,
  275. pTimerEntry->DeltaTime,
  276. (CTEEventRtn)TimerExpiry,
  277. (PVOID)pTimerEntry);
  278. // check if there is a ptr to return
  279. if (ppTimerEntry)
  280. {
  281. *ppTimerEntry = pTimerEntry;
  282. }
  283. // put on list
  284. InsertHeadList(&TimerQ.ActiveHead, &pTimerEntry->Linkage);
  285. }
  286. else if (pDeviceContext)
  287. {
  288. NBT_DEREFERENCE_DEVICE (pDeviceContext, REF_DEV_TIMER, TRUE);
  289. }
  290. }
  291. else
  292. {
  293. status = STATUS_INVALID_DEVICE_STATE;
  294. }
  295. if (!fLocked)
  296. {
  297. CTESpinFree(&NbtConfig.JointLock,OldIrq);
  298. }
  299. return(status);
  300. }
  301. //----------------------------------------------------------------------------
  302. NTSTATUS
  303. StopTimer(
  304. IN tTIMERQENTRY *pTimerEntry,
  305. OUT COMPLETIONCLIENT *ppClientCompletion,
  306. OUT PVOID *ppContext)
  307. /*++
  308. Routine Description:
  309. This routine stops a timer. Must be called with the Joint lock held.
  310. Arguments:
  311. Return Value:
  312. The function value is the status of the operation.
  313. --*/
  314. {
  315. NTSTATUS status;
  316. COMPLETIONROUTINE TimeoutRoutine;
  317. // null the client completion routine and Context so that it can't be called again
  318. // accidently
  319. if (ppClientCompletion)
  320. {
  321. *ppClientCompletion = NULL;
  322. }
  323. if (ppContext)
  324. {
  325. *ppContext = NULL;
  326. }
  327. // it is possible that the timer expiry routine has just run and the timer
  328. // has not been restarted, so check the refcount, it will be zero if the
  329. // timer was not restarted and 2 if the timer expiry is currently running.
  330. if (pTimerEntry->RefCount == 1)
  331. {
  332. // this allows the caller to call the client's completion routine with
  333. // the context value.
  334. if (ppClientCompletion)
  335. {
  336. *ppClientCompletion = pTimerEntry->ClientCompletion;
  337. }
  338. if (ppContext)
  339. {
  340. *ppContext = pTimerEntry->ClientContext;
  341. }
  342. pTimerEntry->ClientCompletion = NULL;
  343. if (!(pTimerEntry->Flags & TIMER_NOT_STARTED))
  344. {
  345. if (!CTEStopTimer((CTETimer *)&pTimerEntry->VxdTimer ))
  346. {
  347. //
  348. // It means the TimerExpiry routine is waiting to run,
  349. // so let it return this timer block to the free Q
  350. // Bug # 229535
  351. //
  352. // Before returning from here, we should do the cleanup since
  353. // the CompletionRoutine (if any) can result in some data
  354. // that is required for cleanup to be cleaned up (Bug # 398730)
  355. //
  356. if (TimeoutRoutine = (COMPLETIONROUTINE)pTimerEntry->TimeoutRoutine)
  357. {
  358. // call the completion routine so it can clean up its own buffers
  359. // the routine that called this one will call the client's completion
  360. // routine. A NULL timerEntry value indicates to the routine that
  361. // cleanup should be done.
  362. pTimerEntry->TimeoutRoutine = NULL;
  363. (VOID)(*TimeoutRoutine) (pTimerEntry->Context, pTimerEntry->Context2, NULL);
  364. }
  365. pTimerEntry->RefCount = 2;
  366. return (STATUS_SUCCESS);
  367. }
  368. }
  369. status = STATUS_SUCCESS;
  370. status = CleanupCTETimer(pTimerEntry);
  371. }
  372. else if (pTimerEntry->RefCount == 2)
  373. {
  374. // the timer expiry completion routines must set this routine to
  375. // null with the spin lock held to synchronize with this stop timer
  376. // routine. Likewise that routine checks this value too, to synchronize
  377. // with this routine.
  378. //
  379. if (pTimerEntry->ClientCompletion)
  380. {
  381. // this allows the caller to call the client's completion routine with
  382. // the context value.
  383. if (ppClientCompletion)
  384. {
  385. *ppClientCompletion = pTimerEntry->ClientCompletion;
  386. }
  387. if (ppContext)
  388. {
  389. *ppContext = pTimerEntry->ClientContext;
  390. }
  391. // so that the timer completion routine cannot also call the client
  392. // completion routine.
  393. pTimerEntry->ClientCompletion = NULL;
  394. }
  395. // signal the TimerExpiry routine that the timer has been cancelled
  396. //
  397. pTimerEntry->RefCount++;
  398. status = STATUS_UNSUCCESSFUL;
  399. }
  400. else
  401. {
  402. status = STATUS_UNSUCCESSFUL;
  403. }
  404. return(status);
  405. }
  406. //----------------------------------------------------------------------------
  407. VOID
  408. TimerExpiry(
  409. #ifndef VXD
  410. IN PKDPC Dpc,
  411. IN PVOID DeferredContext,
  412. IN PVOID SystemArg1,
  413. IN PVOID SystemArg2
  414. #else
  415. IN CTEEvent * pCTEEvent,
  416. IN PVOID DeferredContext
  417. #endif
  418. )
  419. /*++
  420. Routine Description:
  421. This routine is the timer expiry completion routine. It is called by the
  422. kernel when the timer expires.
  423. Arguments:
  424. Return Value:
  425. The function value is the status of the operation.
  426. --*/
  427. {
  428. tTIMERQENTRY *pTimerEntry;
  429. CTELockHandle OldIrq1;
  430. CTESpinLock(&NbtConfig.JointLock,OldIrq1);
  431. // get the timer Q list entry from the context passed in
  432. pTimerEntry = (tTIMERQENTRY *)DeferredContext;
  433. if (pTimerEntry->RefCount == 0)
  434. {
  435. // the timer has been cancelled already!
  436. CTESpinFree(&NbtConfig.JointLock,OldIrq1);
  437. return;
  438. }
  439. else if (pTimerEntry->RefCount >= 2) // Bug #: 229535
  440. {
  441. // the timer has been cancelled already!
  442. // Bug # 324655
  443. // If the Timer has been cancelled, we still need to do cleanup,
  444. // so do not NULL the TimeoutRoutine!
  445. //
  446. // pTimerEntry->TimeoutRoutine = NULL;
  447. ASSERT ((pTimerEntry->RefCount == 2) || (pTimerEntry->TimeoutRoutine == NULL));
  448. CleanupCTETimer (pTimerEntry);
  449. CTESpinFree(&NbtConfig.JointLock,OldIrq1);
  450. return;
  451. }
  452. // increment the reference count because we are handling a timer completion
  453. // now
  454. pTimerEntry->RefCount++;
  455. CTESpinFree(&NbtConfig.JointLock,OldIrq1);
  456. // call the completion routine passing the context value
  457. pTimerEntry->Flags &= ~TIMER_RESTART; // incase the clients wants to restart the timer
  458. (*(COMPLETIONROUTINE)pTimerEntry->TimeoutRoutine)(
  459. pTimerEntry->Context,
  460. pTimerEntry->Context2,
  461. pTimerEntry);
  462. CTESpinLock(&NbtConfig.JointLock,OldIrq1);
  463. pTimerEntry->RefCount--;
  464. if (pTimerEntry->Flags & TIMER_RESTART)
  465. {
  466. if (pTimerEntry->RefCount == 2)
  467. {
  468. // the timer was stopped during the expiry processing, so call the
  469. // deference routine
  470. //
  471. CleanupCTETimer(pTimerEntry);
  472. CTESpinFree(&NbtConfig.JointLock,OldIrq1);
  473. return;
  474. }
  475. else
  476. {
  477. CTEStartTimer (&pTimerEntry->VxdTimer,
  478. pTimerEntry->DeltaTime,
  479. (CTEEventRtn)TimerExpiry,
  480. (PVOID)pTimerEntry);
  481. }
  482. CTESpinFree(&NbtConfig.JointLock,OldIrq1);
  483. return;
  484. }
  485. else
  486. {
  487. // move to the free list after setting the reference count to zero
  488. // since this timer block is no longer active.
  489. //
  490. pTimerEntry->TimeoutRoutine = NULL;
  491. CleanupCTETimer (pTimerEntry);
  492. CTESpinFree(&NbtConfig.JointLock,OldIrq1);
  493. }
  494. }
  495. //----------------------------------------------------------------------------
  496. VOID
  497. ExpireTimer(
  498. IN tTIMERQENTRY *pTimerEntry,
  499. IN CTELockHandle *OldIrq1
  500. )
  501. /*++
  502. Routine Description:
  503. This routine causes the timer to be stopped (if it hasn't already)
  504. and if successful, calls the Completion routine.
  505. Arguments:
  506. Return Value:
  507. The function value is the status of the operation.
  508. --*/
  509. {
  510. //
  511. // Reset the Number of retries
  512. //
  513. pTimerEntry->Retries = 1;
  514. //
  515. // RefCount == 0 => Timer was stopped, but not restarted
  516. // RefCount == 1 => Timer is still running *
  517. // RefCount == 2 => TimerExpiry is currently running
  518. //
  519. if ((pTimerEntry->RefCount == 1) &&
  520. (!(pTimerEntry->Flags & TIMER_NOT_STARTED)) &&
  521. (CTEStopTimer( (CTETimer *)&pTimerEntry->VxdTimer)))
  522. {
  523. // increment the reference count because we are handling a timer completion
  524. // now
  525. pTimerEntry->RefCount++;
  526. CTESpinFree(&NbtConfig.JointLock, *OldIrq1);
  527. // call the completion routine passing the context value
  528. pTimerEntry->Flags &= ~TIMER_RESTART; // incase the clients wants to restart the timer
  529. (*(COMPLETIONROUTINE)pTimerEntry->TimeoutRoutine) (pTimerEntry->Context,
  530. pTimerEntry->Context2,
  531. pTimerEntry);
  532. CTESpinLock(&NbtConfig.JointLock, *OldIrq1);
  533. pTimerEntry->RefCount--;
  534. if (pTimerEntry->Flags & TIMER_RESTART)
  535. {
  536. if (pTimerEntry->RefCount == 2)
  537. {
  538. // the timer was stopped during the expiry processing, so call the
  539. // deference routine
  540. //
  541. CleanupCTETimer(pTimerEntry);
  542. }
  543. else
  544. {
  545. CTEStartTimer(&pTimerEntry->VxdTimer,
  546. pTimerEntry->DeltaTime,
  547. (CTEEventRtn)TimerExpiry,
  548. (PVOID)pTimerEntry);
  549. }
  550. }
  551. else
  552. {
  553. // move to the free list after setting the reference count to zero
  554. // since this tierm block is no longer active.
  555. //
  556. pTimerEntry->TimeoutRoutine = NULL;
  557. CleanupCTETimer (pTimerEntry);
  558. }
  559. }
  560. CTESpinFree(&NbtConfig.JointLock, *OldIrq1);
  561. }
  562. //----------------------------------------------------------------------------
  563. //
  564. // Wakeup Timer routines
  565. //
  566. //----------------------------------------------------------------------------
  567. VOID
  568. WakeupTimerExpiry(
  569. PVOID DeferredContext,
  570. ULONG LowTime,
  571. LONG HighTime
  572. )
  573. {
  574. BOOLEAN fAttached = FALSE;
  575. CTELockHandle OldIrq;
  576. tTIMERQENTRY *pTimerEntry = (tTIMERQENTRY *)DeferredContext;
  577. //
  578. // Call the TimerExpiry function while holding the NbtConfig.Resource
  579. //
  580. CTEExAcquireResourceExclusive (&NbtConfig.Resource,TRUE);
  581. if (pTimerEntry->RefCount > 1)
  582. {
  583. //
  584. // The timer is waiting to be cleaned up on a Worker thread,
  585. // so let it cleanup!
  586. //
  587. CTEExReleaseResource (&NbtConfig.Resource);
  588. return;
  589. }
  590. //
  591. // The Timeout routine has to ensure that it cleans up
  592. // properly since this pTimerEntry + handle will not be valid
  593. // at the end of this routine!
  594. //
  595. (*(COMPLETIONROUTINE) pTimerEntry->TimeoutRoutine) (pTimerEntry->Context,
  596. pTimerEntry->Context2,
  597. pTimerEntry);
  598. //
  599. // Close the timer handle
  600. //
  601. CTEAttachFsp(&fAttached, REF_FSP_WAKEUP_TIMER_EXPIRY);
  602. //
  603. // The Expiry routine should always be called in the context
  604. // of the system process
  605. //
  606. ASSERT (fAttached == FALSE);
  607. ZwClose (pTimerEntry->WakeupTimerHandle);
  608. CTEDetachFsp(fAttached, REF_FSP_WAKEUP_TIMER_EXPIRY);
  609. CTEExReleaseResource (&NbtConfig.Resource);
  610. ReturnTimerToFreeQ (pTimerEntry, FALSE);
  611. }
  612. //----------------------------------------------------------------------------
  613. VOID
  614. DelayedNbtStopWakeupTimer(
  615. IN tDGRAM_SEND_TRACKING *pUnused1,
  616. IN PVOID pClientContext,
  617. IN PVOID Unused2,
  618. IN tDEVICECONTEXT *Unused3
  619. )
  620. /*++
  621. Routine Description:
  622. This Routine stops a timer.
  623. This function has to be called after ensuring that the TimerExpiry has not
  624. yet cleaned up (while holding the NbtConfig.Resource)
  625. The NbtConfig.Resource has to be held while this routine is called
  626. Arguments:
  627. Timer - Timer structure
  628. Return Value:
  629. PVOID - a pointer to the memory or NULL if a failure
  630. --*/
  631. {
  632. NTSTATUS Status;
  633. BOOLEAN CurrentState = FALSE;
  634. BOOLEAN fAttached = FALSE;
  635. tTIMERQENTRY *pTimerEntry = (tTIMERQENTRY *) pClientContext;
  636. CTEPagedCode();
  637. ASSERT (pTimerEntry->fIsWakeupTimer);
  638. CTEAttachFsp(&fAttached, REF_FSP_STOP_WAKEUP_TIMER);
  639. Status = ZwCancelTimer (pTimerEntry->WakeupTimerHandle, &CurrentState);
  640. ZwClose (pTimerEntry->WakeupTimerHandle);
  641. CTEDetachFsp(fAttached, REF_FSP_STOP_WAKEUP_TIMER);
  642. ReturnTimerToFreeQ (pTimerEntry, FALSE);
  643. return;
  644. }
  645. //----------------------------------------------------------------------------
  646. VOID
  647. DelayedNbtStartWakeupTimer(
  648. IN tDGRAM_SEND_TRACKING *pUnused1,
  649. IN PVOID Unused2,
  650. IN PVOID Unused3,
  651. IN tDEVICECONTEXT *Unused4
  652. )
  653. /*++
  654. Routine Description:
  655. This routine starts a Wakeup timer.
  656. NbtConfig.Resource may be held on entry into this routine!
  657. Arguments:
  658. The value passed in is in milliseconds - must be converted to 100ns
  659. so multiply to 10,000
  660. Return Value:
  661. The function value is the status of the operation.
  662. --*/
  663. {
  664. NTSTATUS Status = STATUS_UNSUCCESSFUL;
  665. OBJECT_ATTRIBUTES ObjectAttributes;
  666. BOOLEAN fAttached = FALSE;
  667. LARGE_INTEGER Time;
  668. tTIMERQENTRY *pTimerEntry;
  669. CTELockHandle OldIrq;
  670. ULONG TimerInterval = 0;
  671. ULONG MilliSecsLeftInTtl = 0;
  672. LIST_ENTRY *pEntry;
  673. LIST_ENTRY *pHead;
  674. tDEVICECONTEXT *pDeviceContext;
  675. BOOLEAN fValidDevice = FALSE;
  676. CTEAttachFsp(&fAttached, REF_FSP_START_WAKEUP_TIMER);
  677. CTEExAcquireResourceExclusive(&NbtConfig.Resource,TRUE);
  678. CTESpinLock(&NbtConfig.JointLock,OldIrq);
  679. ASSERT (!NbtConfig.pWakeupRefreshTimer);
  680. //
  681. // Verify that at least 1 WOL-enabled device has an Ip + Wins address!
  682. //
  683. pHead = pEntry = &NbtConfig.DeviceContexts;
  684. while ((pEntry = pEntry->Flink) != pHead)
  685. {
  686. pDeviceContext = CONTAINING_RECORD (pEntry,tDEVICECONTEXT,Linkage);
  687. if ((pDeviceContext->WOLProperties & NDIS_DEVICE_WAKE_UP_ENABLE) &&
  688. (pDeviceContext->IpAddress) &&
  689. (pDeviceContext->lNameServerAddress != LOOP_BACK))
  690. {
  691. fValidDevice = TRUE;
  692. break;
  693. }
  694. }
  695. if ((NbtConfig.Unloading) || // Problem!!!
  696. (!fValidDevice) || // No valid device ?
  697. !(NbtConfig.GlobalRefreshState & NBT_G_REFRESH_SLEEPING)) // check if request was cancelled!
  698. {
  699. CTESpinFree(&NbtConfig.JointLock,OldIrq);
  700. KdPrint (("Nbt.NbtStartWakeupTimer: FAIL: Either: Unloading=<%x>, fValidDevice=<%x>, RefreshState=<%x>\n",
  701. NbtConfig.Unloading, fValidDevice, NbtConfig.GlobalRefreshState));
  702. }
  703. else if (!NT_SUCCESS (Status = GetTimerEntry (&pTimerEntry))) // get a free timer block
  704. {
  705. CTESpinFree(&NbtConfig.JointLock,OldIrq);
  706. KdPrint (("Nbt.NbtStartWakeupTimer: ERROR: GetTimerEntry returned <%x>\n", Status));
  707. }
  708. else
  709. {
  710. pTimerEntry->RefCount = 1;
  711. pTimerEntry->TimeoutRoutine = WakeupRefreshTimeout;
  712. pTimerEntry->Context = NULL;
  713. pTimerEntry->Context2 = NULL;
  714. CTESpinFree(&NbtConfig.JointLock,OldIrq);
  715. #ifdef HDL_FIX
  716. InitializeObjectAttributes (&ObjectAttributes, NULL, OBJ_KERNEL_HANDLE, NULL, NULL);
  717. #else
  718. InitializeObjectAttributes (&ObjectAttributes, NULL, 0, NULL, NULL);
  719. #endif // HDL_FIX
  720. Status = ZwCreateTimer (&pTimerEntry->WakeupTimerHandle,
  721. TIMER_ALL_ACCESS,
  722. &ObjectAttributes,
  723. NotificationTimer);
  724. if (NT_SUCCESS (Status))
  725. {
  726. //
  727. // Set the machine to Wakeup at 1/2 the time between now and Ttl
  728. // This should not be less than the configured MinimumRefreshSleepTimeout
  729. // (default = 6 hrs)
  730. //
  731. MilliSecsLeftInTtl = NbtConfig.MinimumTtl
  732. - (ULONG) (((ULONGLONG) NbtConfig.sTimeoutCount * NbtConfig.MinimumTtl)
  733. / NbtConfig.RefreshDivisor);
  734. if ((MilliSecsLeftInTtl/2) < NbtConfig.MinimumRefreshSleepTimeout)
  735. {
  736. TimerInterval = NbtConfig.MinimumRefreshSleepTimeout;
  737. }
  738. else
  739. {
  740. TimerInterval = MilliSecsLeftInTtl/2;
  741. }
  742. pTimerEntry->DeltaTime = TimerInterval;
  743. IF_DBG(NBT_DEBUG_PNP_POWER)
  744. KdPrint(("Nbt.DelayedNbtStartWakeupTimer: TimerInterval=<%d:%d> (h:m), Currently: <%d/%d>\n",
  745. TimerInterval/(3600000), ((TimerInterval/60000)%60),
  746. NbtConfig.sTimeoutCount, NbtConfig.RefreshDivisor));
  747. //
  748. // convert to 100 ns units by multiplying by 10,000
  749. //
  750. Time.QuadPart = UInt32x32To64(pTimerEntry->DeltaTime,(LONG)MILLISEC_TO_100NS);
  751. Time.QuadPart = -(Time.QuadPart); // to make a delta time, negate the time
  752. pTimerEntry->fIsWakeupTimer = TRUE;
  753. ASSERT(Time.QuadPart < 0);
  754. Status = ZwSetTimer(pTimerEntry->WakeupTimerHandle,
  755. &Time,
  756. (PTIMER_APC_ROUTINE) WakeupTimerExpiry,
  757. pTimerEntry,
  758. TRUE,
  759. 0,
  760. NULL);
  761. if (!NT_SUCCESS (Status))
  762. {
  763. KdPrint (("Nbt.NbtStartWakeupTimer: ERROR: ZwSetTimer returned <%x>, TimerHandle=<%x>\n",
  764. Status, pTimerEntry->WakeupTimerHandle));
  765. ZwClose (pTimerEntry->WakeupTimerHandle);
  766. }
  767. }
  768. else
  769. {
  770. KdPrint (("Nbt.NbtStartWakeupTimer: ERROR: ZwCreateTimer returned <%x>\n", Status));
  771. }
  772. if (NT_SUCCESS (Status))
  773. {
  774. NbtConfig.pWakeupRefreshTimer = pTimerEntry;
  775. }
  776. else
  777. {
  778. ReturnTimerToFreeQ (pTimerEntry, FALSE);
  779. }
  780. }
  781. CTEExReleaseResource(&NbtConfig.Resource);
  782. CTEDetachFsp(fAttached, REF_FSP_START_WAKEUP_TIMER);
  783. KeSetEvent (&NbtConfig.WakeupTimerStartedEvent, 0, FALSE);
  784. return;
  785. }