[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[win-pv-devel] [PATCH] Re-synchronize util.h with XENBUS and use __toupper()



A recent patch introduced __tolower() and __toupper() into util.h as
replacements for tolower() and toupper() respectively as the latter do
a hidden conversion to Unicode which make then unsafe at any IRQL other
than PASSIVE_LEVEL.
This patch imports util.h from XENBUS and fixes other code to be compatible.

Signed-off-by: Paul Durrant <paul.durrant@xxxxxxxxxx>
---
 include/util.h          | 317 ------------------------------------------------
 src/xeniface/fdo.c      |   4 +-
 src/xeniface/registry.c |   4 +-
 src/xeniface/thread.c   |   4 +-
 src/xeniface/util.h     | 312 +++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 318 insertions(+), 323 deletions(-)
 delete mode 100644 include/util.h
 create mode 100644 src/xeniface/util.h

diff --git a/include/util.h b/include/util.h
deleted file mode 100644
index 10e5414..0000000
--- a/include/util.h
+++ /dev/null
@@ -1,317 +0,0 @@
-/* Copyright (c) Citrix Systems Inc.
- * All rights reserved.
- * 
- * Redistribution and use in source and binary forms, 
- * with or without modification, are permitted provided 
- * that the following conditions are met:
- * 
- * *   Redistributions of source code must retain the above 
- *     copyright notice, this list of conditions and the 
- *     following disclaimer.
- * *   Redistributions in binary form must reproduce the above 
- *     copyright notice, this list of conditions and the 
- *     following disclaimer in the documentation and/or other 
- *     materials provided with the distribution.
- * 
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 
- * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 
- * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
- * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 
- * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
- * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
- * SUCH DAMAGE.
- */
-
-#ifndef _UTIL_H
-#define _UTIL_H
-
-#include <ntddk.h>
-
-#include "assert.h"
-
-#define        P2ROUNDUP(_x, _a)   \
-        (-(-(_x) & -(_a)))
-
-static FORCEINLINE LONG
-__ffs(
-    IN  unsigned long long  mask
-    )
-{
-    unsigned char           *array = (unsigned char *)&mask;
-    unsigned int            byte;
-    unsigned int            bit;
-    unsigned char           val;
-
-    val = 0;
-
-    byte = 0;
-    while (byte < 8) {
-        val = array[byte];
-
-        if (val != 0)
-            break;
-
-        byte++;
-    }
-    if (byte == 8)
-        return -1;
-
-    bit = 0;
-    while (bit < 8) {
-        if (val & 0x01)
-            break;
-
-        val >>= 1;
-        bit++;
-    }
-
-    return (byte * 8) + bit;
-}
-
-#define __ffu(_mask)  \
-        __ffs(~(_mask))
-
-static FORCEINLINE LONG
-__InterlockedAdd(
-    IN  LONG    *Value,
-    IN  LONG    Delta
-    )
-{
-    LONG        New;
-    LONG        Old;
-
-    do {
-        Old = *Value;
-        New = Old + Delta;
-    } while (InterlockedCompareExchange(Value, New, Old) != Old);
-
-    return New;
-}
-
-static FORCEINLINE LONG
-__InterlockedSubtract(
-    IN  LONG    *Value,
-    IN  LONG    Delta
-    )
-{
-    LONG        New;
-    LONG        Old;
-
-    do {
-        Old = *Value;
-        New = Old - Delta;
-    } while (InterlockedCompareExchange(Value, New, Old) != Old);
-
-    return New;
-}
-
-typedef struct _NON_PAGED_BUFFER_HEADER {
-    SIZE_T  Length;
-    ULONG   Tag;
-} NON_PAGED_BUFFER_HEADER, *PNON_PAGED_BUFFER_HEADER;
-
-typedef struct _NON_PAGED_BUFFER_TRAILER {
-    ULONG   Tag;
-} NON_PAGED_BUFFER_TRAILER, *PNON_PAGED_BUFFER_TRAILER;
-
-static FORCEINLINE PVOID
-__AllocateNonPagedPoolWithTag(
-    IN  SIZE_T                  Length,
-    IN  ULONG                   Tag
-    )
-{
-    PUCHAR                      Buffer;
-    PNON_PAGED_BUFFER_HEADER    Header;
-    PNON_PAGED_BUFFER_TRAILER   Trailer;
-
-    ASSERT(Length != 0);
-
-    Buffer = (PUCHAR)ExAllocatePoolWithTag(NonPagedPool,
-                                   sizeof (NON_PAGED_BUFFER_HEADER) +
-                                   Length +
-                                   sizeof (NON_PAGED_BUFFER_TRAILER),
-                                   Tag);
-    if (Buffer == NULL)
-        goto done;
-
-    RtlZeroMemory(Buffer, 
-                  sizeof (NON_PAGED_BUFFER_HEADER) +
-                  Length +
-                  sizeof (NON_PAGED_BUFFER_TRAILER));
-
-    Header = (PNON_PAGED_BUFFER_HEADER)Buffer;
-    Header->Length = Length;
-    Header->Tag = Tag;
-
-    Buffer += sizeof (NON_PAGED_BUFFER_HEADER);
-
-    Trailer = (PNON_PAGED_BUFFER_TRAILER)(Buffer + Length);
-    Trailer->Tag = Tag;
-
-done:
-    return Buffer;
-}
-
-static FORCEINLINE VOID
-__FreePoolWithTag(
-    IN  PVOID                   _Buffer,
-    IN  ULONG                   Tag
-    )
-{
-    PUCHAR                      Buffer = (PUCHAR)_Buffer;
-    SIZE_T                      Length;
-    PNON_PAGED_BUFFER_HEADER    Header;
-    PNON_PAGED_BUFFER_TRAILER   Trailer;
-
-    ASSERT(Buffer != NULL);
-
-    Buffer -= sizeof (NON_PAGED_BUFFER_HEADER);
-
-    Header = (PNON_PAGED_BUFFER_HEADER)Buffer;
-    ASSERT3U(Tag, ==, Header->Tag);
-    Length = Header->Length;
-
-    Buffer += sizeof (NON_PAGED_BUFFER_HEADER);
-
-    Trailer = (PNON_PAGED_BUFFER_TRAILER)(Buffer + Length);
-    ASSERT3U(Tag, ==, Trailer->Tag);
-
-    Buffer -= sizeof (NON_PAGED_BUFFER_HEADER);
-
-    RtlFillMemory(Buffer, 
-                  sizeof (NON_PAGED_BUFFER_HEADER) +
-                  Length +
-                  sizeof (NON_PAGED_BUFFER_TRAILER),
-                  0xAA);
-
-    ExFreePoolWithTag(Buffer, Tag);
-}
-
-static FORCEINLINE PMDL
-__AllocatePage(
-    VOID
-    )
-{
-    PHYSICAL_ADDRESS    LowAddress;
-    PHYSICAL_ADDRESS    HighAddress;
-    LARGE_INTEGER       SkipBytes;
-    SIZE_T              TotalBytes;
-    PMDL                Mdl;
-    PUCHAR              MdlMappedSystemVa;
-    NTSTATUS            status;
-
-    LowAddress.QuadPart = 0ull;
-    HighAddress.QuadPart = ~0ull;
-    SkipBytes.QuadPart = 0ull;
-    TotalBytes = (SIZE_T)PAGE_SIZE;
-
-    Mdl = MmAllocatePagesForMdlEx(LowAddress,
-                                  HighAddress,
-                                  SkipBytes,
-                                  TotalBytes,
-                                  MmCached,
-                                  0);
-
-    status = STATUS_NO_MEMORY;
-    if (Mdl == NULL)
-        goto fail1;
-
-    ASSERT((Mdl->MdlFlags & (MDL_MAPPED_TO_SYSTEM_VA |
-                             MDL_PARTIAL_HAS_BEEN_MAPPED |
-                             MDL_PARTIAL |
-                             MDL_PARENT_MAPPED_SYSTEM_VA |
-                             MDL_SOURCE_IS_NONPAGED_POOL |
-                             MDL_IO_SPACE)) == 0);
-
-    MdlMappedSystemVa = MmMapLockedPagesSpecifyCache(Mdl,
-                                                     KernelMode,
-                                                     MmCached,
-                                                     NULL,
-                                                     FALSE,
-                                                     NormalPagePriority);
-
-    status = STATUS_UNSUCCESSFUL;
-    if (MdlMappedSystemVa == NULL)
-        goto fail2;
-
-    ASSERT3P(MdlMappedSystemVa, ==, Mdl->MappedSystemVa);
-
-    RtlZeroMemory(MdlMappedSystemVa, PAGE_SIZE);
-
-    return Mdl;
-
-fail2:
-    Error("fail2\n");
-
-    MmFreePagesFromMdl(Mdl);
-    ExFreePool(Mdl);
-
-fail1:
-    Error("fail1 (%08x)\n", status);
-
-    return NULL;
-}
-
-static FORCEINLINE VOID
-__FreePage(
-    IN PMDL    Mdl
-    )
-{
-    PUCHAR     MdlMappedSystemVa;
-
-    ASSERT(Mdl->MdlFlags & MDL_MAPPED_TO_SYSTEM_VA);
-    MdlMappedSystemVa = Mdl->MappedSystemVa;
-
-    RtlFillMemory(MdlMappedSystemVa, PAGE_SIZE, 0xAA);
-    
-    MmUnmapLockedPages(MdlMappedSystemVa, Mdl);
-
-    MmFreePagesFromMdl(Mdl);
-}
-
-static FORCEINLINE PCHAR
-__strtok_r(
-    IN      PCHAR   Buffer,
-    IN      PCHAR   Delimiter,
-    IN OUT  PCHAR   *Context
-    )
-{
-    PCHAR           Token;
-    PCHAR           End;
-
-    if (Buffer != NULL)
-        *Context = Buffer;
-
-    Token = *Context;
-
-    if (Token == NULL)
-        return NULL;
-
-    while (*Token != L'\0' &&
-           strchr(Delimiter, *Token) != NULL)
-        Token++;
-
-    if (*Token == L'\0')
-        return NULL;
-
-    End = Token + 1;
-    while (*End != L'\0' &&
-           strchr(Delimiter, *End) == NULL)
-        End++;
-
-    if (*End != L'\0')
-        *End++ = L'\0';
-
-    *Context = End;
-
-    return Token;
-}
-
-#endif  // _UTIL_H
diff --git a/src/xeniface/fdo.c b/src/xeniface/fdo.c
index 6eea3ab..b23832d 100644
--- a/src/xeniface/fdo.c
+++ b/src/xeniface/fdo.c
@@ -187,7 +187,7 @@ __FdoAllocate(
     IN  ULONG   Length
     )
 {
-    return __AllocateNonPagedPoolWithTag(Length, FDO_POOL);
+    return __AllocatePoolWithTag(NonPagedPool, Length, FDO_POOL);
 }
 
 static FORCEINLINE VOID
@@ -591,7 +591,7 @@ __FdoMultiSzToUpcaseAnsi(
             if (Buffer[Index] == '\0')
                 break;
         } else {
-            Buffer[Index] = (CHAR)toupper(Buffer[Index]);
+            Buffer[Index] = __toupper(Buffer[Index]);
             Index++;
         }
     }
diff --git a/src/xeniface/registry.c b/src/xeniface/registry.c
index 519d3f6..136502c 100644
--- a/src/xeniface/registry.c
+++ b/src/xeniface/registry.c
@@ -30,9 +30,9 @@
  */
 
 #include <ntddk.h>
-#include <util.h>
 
 #include "registry.h"
+#include "util.h"
 #include "assert.h"
 
 #define REGISTRY_POOL 'GERX'
@@ -44,7 +44,7 @@ __RegistryAllocate(
     IN  ULONG   Length
     )
 {
-    return __AllocateNonPagedPoolWithTag(Length, REGISTRY_POOL);
+    return __AllocatePoolWithTag(NonPagedPool, Length, REGISTRY_POOL);
 }
 
 static FORCEINLINE VOID
diff --git a/src/xeniface/thread.c b/src/xeniface/thread.c
index f008834..386bba7 100644
--- a/src/xeniface/thread.c
+++ b/src/xeniface/thread.c
@@ -30,9 +30,9 @@
  */
 
 #include <ntddk.h>
-#include <util.h>
 
 #include "thread.h"
+#include "util.h"
 #include "log.h"
 #include "assert.h"
 
@@ -52,7 +52,7 @@ __ThreadAllocate(
     IN  ULONG   Length
     )
 {
-    return __AllocateNonPagedPoolWithTag(Length, THREAD_POOL);
+    return __AllocatePoolWithTag(NonPagedPool, Length, THREAD_POOL);
 }
 
 static FORCEINLINE VOID
diff --git a/src/xeniface/util.h b/src/xeniface/util.h
new file mode 100644
index 0000000..13a6734
--- /dev/null
+++ b/src/xeniface/util.h
@@ -0,0 +1,312 @@
+/* Copyright (c) Citrix Systems Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms,
+ * with or without modification, are permitted provided
+ * that the following conditions are met:
+ *
+ * *   Redistributions of source code must retain the above
+ *     copyright notice, this list of conditions and the
+ *     following disclaimer.
+ * *   Redistributions in binary form must reproduce the above
+ *     copyright notice, this list of conditions and the
+ *     following disclaimer in the documentation and/or other
+ *     materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef _XENIFACE_UTIL_H
+#define _XENIFACE_UTIL_H
+
+#include <ntddk.h>
+
+#include "assert.h"
+
+#define        P2ROUNDUP(_x, _a)   \
+        (-(-(_x) & -(_a)))
+
+static FORCEINLINE LONG
+__ffs(
+    IN  unsigned long long  mask
+    )
+{
+    unsigned char           *array = (unsigned char *)&mask;
+    unsigned int            byte;
+    unsigned int            bit;
+    unsigned char           val;
+
+    val = 0;
+
+    byte = 0;
+    while (byte < 8) {
+        val = array[byte];
+
+        if (val != 0)
+            break;
+
+        byte++;
+    }
+    if (byte == 8)
+        return -1;
+
+    bit = 0;
+    while (bit < 8) {
+        if (val & 0x01)
+            break;
+
+        val >>= 1;
+        bit++;
+    }
+
+    return (byte * 8) + bit;
+}
+
+#define __ffu(_mask)  \
+        __ffs(~(_mask))
+
+static FORCEINLINE VOID
+__CpuId(
+    IN  ULONG   Leaf,
+    OUT PULONG  EAX OPTIONAL,
+    OUT PULONG  EBX OPTIONAL,
+    OUT PULONG  ECX OPTIONAL,
+    OUT PULONG  EDX OPTIONAL
+    )
+{
+    ULONG       Value[4] = {0};
+
+    __cpuid(Value, Leaf);
+
+    if (EAX)
+        *EAX = Value[0];
+
+    if (EBX)
+        *EBX = Value[1];
+
+    if (ECX)
+        *ECX = Value[2];
+
+    if (EDX)
+        *EDX = Value[3];
+}
+
+static FORCEINLINE LONG
+__InterlockedAdd(
+    IN  LONG    *Value,
+    IN  LONG    Delta
+    )
+{
+    LONG        New;
+    LONG        Old;
+
+    do {
+        Old = *Value;
+        New = Old + Delta;
+    } while (InterlockedCompareExchange(Value, New, Old) != Old);
+
+    return New;
+}
+
+static FORCEINLINE LONG
+__InterlockedSubtract(
+    IN  LONG    *Value,
+    IN  LONG    Delta
+    )
+{
+    LONG        New;
+    LONG        Old;
+
+    do {
+        Old = *Value;
+        New = Old - Delta;
+    } while (InterlockedCompareExchange(Value, New, Old) != Old);
+
+    return New;
+}
+
+static FORCEINLINE PVOID
+__AllocatePoolWithTag(
+    IN  POOL_TYPE   PoolType,
+    IN  SIZE_T      NumberOfBytes,
+    IN  ULONG       Tag
+    )
+{
+    PUCHAR          Buffer;
+
+    __analysis_assume(PoolType == NonPagedPool ||
+                      PoolType == PagedPool);
+
+    Buffer = ExAllocatePoolWithTag(PoolType, NumberOfBytes, Tag);
+    if (Buffer == NULL)
+        return NULL;
+
+    RtlZeroMemory(Buffer, NumberOfBytes);
+    return Buffer;
+}
+
+static FORCEINLINE VOID
+__FreePoolWithTag(
+    IN  PVOID   Buffer,
+    IN  ULONG   Tag
+    )
+{
+    ExFreePoolWithTag(Buffer, Tag);
+}
+
+static FORCEINLINE PMDL
+__AllocatePage(
+    VOID
+    )
+{
+    PHYSICAL_ADDRESS    LowAddress;
+    PHYSICAL_ADDRESS    HighAddress;
+    LARGE_INTEGER       SkipBytes;
+    SIZE_T              TotalBytes;
+    PMDL                Mdl;
+    PUCHAR              MdlMappedSystemVa;
+    NTSTATUS            status;
+
+    LowAddress.QuadPart = 0ull;
+    HighAddress.QuadPart = ~0ull;
+    SkipBytes.QuadPart = 0ull;
+    TotalBytes = (SIZE_T)PAGE_SIZE;
+
+    Mdl = MmAllocatePagesForMdlEx(LowAddress,
+                                  HighAddress,
+                                  SkipBytes,
+                                  TotalBytes,
+                                  MmCached,
+                                  0);
+
+    status = STATUS_NO_MEMORY;
+    if (Mdl == NULL)
+        goto fail1;
+
+    ASSERT((Mdl->MdlFlags & (MDL_MAPPED_TO_SYSTEM_VA |
+                             MDL_PARTIAL_HAS_BEEN_MAPPED |
+                             MDL_PARTIAL |
+                             MDL_PARENT_MAPPED_SYSTEM_VA |
+                             MDL_SOURCE_IS_NONPAGED_POOL |
+                             MDL_IO_SPACE)) == 0);
+
+    MdlMappedSystemVa = MmMapLockedPagesSpecifyCache(Mdl,
+                                                     KernelMode,
+                                                                            
MmCached,
+                                                                            
NULL,
+                                                                            
FALSE,
+                                                                            
NormalPagePriority);
+
+    status = STATUS_UNSUCCESSFUL;
+    if (MdlMappedSystemVa == NULL)
+        goto fail2;
+
+    ASSERT3P(MdlMappedSystemVa, ==, Mdl->MappedSystemVa);
+
+    RtlZeroMemory(MdlMappedSystemVa, PAGE_SIZE);
+
+    return Mdl;
+
+fail2:
+    Error("fail2\n");
+
+    MmFreePagesFromMdl(Mdl);
+    ExFreePool(Mdl);
+
+fail1:
+    Error("fail1 (%08x)\n", status);
+
+    return NULL;
+}
+
+static FORCEINLINE VOID
+__FreePage(
+    IN PMDL    Mdl
+    )
+{
+    PUCHAR     MdlMappedSystemVa;
+
+    ASSERT(Mdl->MdlFlags & MDL_MAPPED_TO_SYSTEM_VA);
+    MdlMappedSystemVa = Mdl->MappedSystemVa;
+
+    RtlFillMemory(MdlMappedSystemVa, PAGE_SIZE, 0xAA);
+
+    MmUnmapLockedPages(MdlMappedSystemVa, Mdl);
+
+    MmFreePagesFromMdl(Mdl);
+}
+
+static FORCEINLINE PCHAR
+__strtok_r(
+    IN      PCHAR   Buffer,
+    IN      PCHAR   Delimiter,
+    IN OUT  PCHAR   *Context
+    )
+{
+    PCHAR           Token;
+    PCHAR           End;
+
+    if (Buffer != NULL)
+        *Context = Buffer;
+
+    Token = *Context;
+
+    if (Token == NULL)
+        return NULL;
+
+    while (*Token != L'\0' &&
+           strchr(Delimiter, *Token) != NULL)
+        Token++;
+
+    if (*Token == L'\0')
+        return NULL;
+
+    End = Token + 1;
+    while (*End != L'\0' &&
+           strchr(Delimiter, *End) == NULL)
+        End++;
+
+    if (*End != L'\0')
+        *End++ = L'\0';
+
+    *Context = End;
+
+    return Token;
+}
+
+static FORCEINLINE CHAR
+__toupper(
+    IN  CHAR    Character
+    )
+{
+    if (Character < 'a' || Character > 'z')
+        return Character;
+
+    return 'A' + Character - 'a';
+}
+
+static FORCEINLINE CHAR
+__tolower(
+    IN  CHAR    Character
+    )
+{
+    if (Character < 'A' || Character > 'Z')
+        return Character;
+
+    return 'a' + Character - 'A';
+}
+
+#endif  // _XENIFACE_UTIL_H
-- 
2.1.1


_______________________________________________
win-pv-devel mailing list
win-pv-devel@xxxxxxxxxxxxxxxxxxxx
http://lists.xenproject.org/cgi-bin/mailman/listinfo/win-pv-devel


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.