|
1 | 1 | // Copyright (c) Microsoft Corporation. |
2 | 2 | // License: MIT OR Apache-2.0 |
3 | 3 |
|
4 | | -use core::sync::atomic::Ordering; |
| 4 | +use core::{sync::atomic::Ordering, time::Duration}; |
5 | 5 |
|
6 | 6 | use wdk::{nt_success, paged_code, println, wdf}; |
7 | 7 | use wdk_sys::{ |
@@ -44,12 +44,6 @@ use crate::{ |
44 | 44 | WDF_TIMER_CONFIG_SIZE, |
45 | 45 | }; |
46 | 46 |
|
47 | | -/// Set max write length for testing |
48 | | -const MAX_WRITE_LENGTH: usize = 1024 * 40; |
49 | | - |
50 | | -/// Set timer period in ms |
51 | | -const TIMER_PERIOD: u32 = 1000 * 10; |
52 | | - |
53 | 47 | /// This routine will interlock increment a value only if the current value |
54 | 48 | /// is greater then the floor value. |
55 | 49 | /// |
@@ -130,6 +124,19 @@ fn echo_interlocked_increment_gtzero(target: &AtomicI32) -> i32 { |
130 | 124 | /// * `NTSTATUS` |
131 | 125 | #[link_section = "PAGE"] |
132 | 126 | pub unsafe fn echo_queue_initialize(device: WDFDEVICE) -> NTSTATUS { |
| 127 | + #[allow( |
| 128 | + clippy::cast_possible_truncation, |
| 129 | + reason = "10 seconds in millisecond units is known to fit in u32" |
| 130 | + )] |
| 131 | + const TIMER_PERIOD_MS: u32 = { |
| 132 | + const MILLIS: u128 = Duration::from_secs(10).as_millis(); |
| 133 | + const { |
| 134 | + assert!(MILLIS <= u32::MAX as u128, "10,000 should fit in u32"); |
| 135 | + }; |
| 136 | + |
| 137 | + MILLIS as u32 |
| 138 | + }; |
| 139 | + |
133 | 140 | paged_code!(); |
134 | 141 |
|
135 | 142 | let mut queue = WDF_NO_HANDLE as WDFQUEUE; |
@@ -206,7 +213,7 @@ pub unsafe fn echo_queue_initialize(device: WDFDEVICE) -> NTSTATUS { |
206 | 213 | let mut timer_config = WDF_TIMER_CONFIG { |
207 | 214 | Size: WDF_TIMER_CONFIG_SIZE, |
208 | 215 | EvtTimerFunc: Some(echo_evt_timer_func), |
209 | | - Period: TIMER_PERIOD, |
| 216 | + Period: TIMER_PERIOD_MS, |
210 | 217 | AutomaticSerialization: u8::from(true), |
211 | 218 | TolerableDelay: 0, |
212 | 219 | ..WDF_TIMER_CONFIG::default() |
@@ -518,6 +525,15 @@ extern "C" fn echo_evt_io_read(queue: WDFQUEUE, request: WDFREQUEST, mut length: |
518 | 525 | /// |
519 | 526 | /// * `VOID` |
520 | 527 | extern "C" fn echo_evt_io_write(queue: WDFQUEUE, request: WDFREQUEST, length: usize) { |
| 528 | + /// Number of bytes in one kilobyte. |
| 529 | + const BYTES_PER_KB: usize = 1024; |
| 530 | + /// Max write length, in bytes, for testing |
| 531 | + const MAX_WRITE_LENGTH: usize = 40 * BYTES_PER_KB; |
| 532 | + |
| 533 | + /// Non-zero char literal (of one to four chars) for pool tag used in |
| 534 | + /// `ExAllocatePool2` |
| 535 | + const MEMORY_TAG: u32 = u32::from_be_bytes(*b"sam1"); |
| 536 | + |
521 | 537 | let mut memory = WDF_NO_HANDLE as WDFMEMORY; |
522 | 538 | let mut status: NTSTATUS; |
523 | 539 | let queue_context = unsafe { queue_get_context(queue as WDFOBJECT) }; |
@@ -564,9 +580,8 @@ extern "C" fn echo_evt_io_write(queue: WDFQUEUE, request: WDFREQUEST, length: us |
564 | 580 | (*queue_context).length = 0; |
565 | 581 | } |
566 | 582 |
|
567 | | - // FIXME: Memory Tag |
568 | 583 | (*queue_context).buffer = |
569 | | - ExAllocatePool2(POOL_FLAG_NON_PAGED, length as SIZE_T, 's' as u32); |
| 584 | + ExAllocatePool2(POOL_FLAG_NON_PAGED, length as SIZE_T, MEMORY_TAG); |
570 | 585 | if (*queue_context).buffer.is_null() { |
571 | 586 | println!( |
572 | 587 | "echo_evt_io_write Could not allocate {:?} byte buffer", |
@@ -697,9 +712,8 @@ unsafe extern "C" fn echo_evt_timer_func(timer: WDFTIMER) { |
697 | 712 | // currently racing with it), there is no need to use an interlocked |
698 | 713 | // decrement to lower the cancel ownership count. |
699 | 714 |
|
700 | | - // 2 is the initial count we set when we initialized |
701 | | - // CancelCompletionOwnershipCount plus the call to |
702 | | - // EchoIncrementRequestCancelOwnershipCount() |
| 715 | + // 2 = the initial ownership count (1) plus the one increment |
| 716 | + // acquired via echo_increment_request_cancel_ownership_count. |
703 | 717 | (*request_context) |
704 | 718 | .cancel_completion_ownership_count |
705 | 719 | .fetch_sub(2, Ordering::SeqCst); |
|
0 commit comments