Drivers For 64-Bit Windows ® Nar Ganapathy Software Design Engineer Windows NT ® Base Systems Microsoft Corporation.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

A C++ Crash Course Part II UW Association for Computing Machinery Questions & Feedback.
Device Drivers. Linux Device Drivers Linux supports three types of hardware device: character, block and network –character devices: R/W without buffering.
Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode.
Things to Remember When Developing 64-bit Software OOO "Program Verification Systems"
Programming and Data Structure
Slide 2-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2 Using the Operating System 2.
SPIM and MIPS programming
The Linux Kernel: Memory Management
Memory management.
Chapter 10.
1 Homework Turn in HW2 at start of next class. Starting Chapter 2 K&R. Read ahead. HW3 is on line. –Due: class 9, but a lot to do! –You may want to get.
Structure of a C program
C Programming Basics Lecture 5 Engineering H192 Winter 2005 Lecture 05
6-1 I/O Methods I/O – Transfer of data between memory of the system and the I/O device Most devices operate asynchronously from the CPU Most methods involve.
Home: Phones OFF Please Unix Kernel Parminder Singh Kang Home:
OS Spring’03 Introduction Operating Systems Spring 2003.
OPERATING SYSTEMS Introduction
Portability CPSC 315 – Programming Studio Spring 2008 Material from The Practice of Programming, by Pike and Kernighan.
Pointers Applications
Data Types in the Kernel Sarah Diesburg COP 5641.
Windows 2000 Memory Management Computing Department, Lancaster University, UK.
CENG 311 Machine Representation/Numbers
Windows ® XP 64-Bit Edition Your Name Your Title Microsoft Corporation.
Moving Drivers To 64-Bit Nar Ganapathy Architect Windows Device Experience Group Microsoft Corporation.
C Tokens Identifiers Keywords Constants Operators Special symbols.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Operating Systems Lecture 7 OS Potpourri Adapted from Operating Systems Lecture Notes, Copyright 1997 Martin C. Rinard. Zhiqing Liu School of Software.
Module 6: Debugging a Windows CE Image.  Overview Debug Zones IDE Debug Setup IDE Debug Commands Platform Builder Integrated Kernel Debugger Other Debugging.
UNIX Files File organization and a few primitives.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Module R3 Process Scheduling. Module R3 involves the creation of a simple “Round Robin” dispatcher. The successful completion of this module will require.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics Lecture 5.
Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)
Best Practices Steve Maillet Chief Software Architect EmbeddedFusion ECE401 Best Practices For Driver Development.
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
Developing a 64-bit Strategy Craig McMurtry Developer Evangelist, Software Vendors Developer and Platform Evangelism Microsoft Corporation.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
I/O Software CS 537 – Introduction to Operating Systems.
1988 Morris Worm … estimated 10% penetration 2001 Code Red … 300,00 computers breached 2003 Slammer/Sapphire … 75,00 infections in 10 min Zotob …
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
1 Building a program in C: Preprocessor, Compilation and Linkage.
Windows Programming Lecture 03. Pointers and Arrays.
Secure Coding Rules for C++ Copyright © 2016 Curt Hill
Information and Computer Sciences University of Hawaii, Manoa
Dynamic Storage Allocation
Stack and Heap Memory Stack resident variables include:
Microprocessor Systems Design I
CPSC 315 – Programming Studio Spring 2012
Chapter7 Structure & C++
Computing with C# and the .NET Framework
Secure Coding Rules for C++ Copyright © Curt Hill
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
CS 240 – Lecture 18 Command-line Arguments, Typedef, Union, Bit Fields, Pointers to Functions.
Portability CPSC 315 – Programming Studio
Bits and Bytes Topics Representing information as bits
Bits and Bytes Topics Representing information as bits
Operating Systems Chapter 5: Input/Output Management
CS703 - Advanced Operating Systems
Bits and Bytes Topics Representing information as bits
Lecture Topics: 11/1 General Operating System Concepts Processes
Focus of the Course Object-Oriented Software Development
Comp Org & Assembly Lang
Data Types and Expressions
Operations and Arithmetic
Areas Of Focus Image format Firmware Memory management Problem areas
Presentation transcript:

Drivers For 64-Bit Windows ® Nar Ganapathy Software Design Engineer Windows NT ® Base Systems Microsoft Corporation

Agenda  What is 64-bit Windows  Programming model  Porting guidelines  Programming issues

64-Bit Windows 2000 Goals  Porting from Win32 ® to Win64  should be simple  A single source code base for Win32 and Win64  Enable existing applications to scale to enterprise capacities  Enable new designs that use huge address space and memory  Support existing 32-bit applications  32-bit device drivers not supported

What Is 64-Bit Version Of Windows 2000?  64-Bit Windows is an evolution of the Windows programming model and APIs  LLP64 model  Pointers are expanded to 64 bits  Memory allocation sizes are 64 bits  Longs and ints are 32 bits

Win64 Programming Model  Uses the same Win32 DDIs/APIs  Adds new explicitly sized types  Adds new integral types that match the precision of a pointer  Almost all Win32 32-bit data types remain 32-bits  E.g., size of ULONG is still 32 bits under Win64  Pointers are 64-bits

Win64 Programming Model  Items expanded to 64 bits  Length for most memory allocations  CPU mask  Unchanged  Length for I/O operations  Unicode string lengths

Win64 Data Types TYPE NAME What It is LONG32, INT32 32 bit signed LONG64, INT64 64 bit signed ULONG32,UINT32,DWORD32 32 bit unsigned ULONG64,UINT64,DWORD64 64 bit unsigned

Win64 Data Types Continued TYPE NAME What it is INT_PTR,LONG_PTR Signed Int, Pointer precision UINT_PTR,ULONG_PTR,DWORD_PTR Unsigned Int, Pointer precision SIZE_T Unsigned count, Pointer precision SSIZE_T Signed count, Pointer precision

Win64 Porting Guidelines  Use Win64 safe data types  Examine all pointer usage, especially pointer arithmetic  Two major porting topics  Driver specific issues (e.g., IOCTLs, DMA)  Programming issues

Memory Management (IA64)  User addresses range from 0x10000 – 0x000006FBFFFEFFFF  One terabyte system cache  Sixteen gigabyte HyperSpace  128 gigabyte paged pool  128 gigabyte System address space for kernel threads, etc.  128 gigabyte non-paged pool  Physical memory addresses are 64 bits

WOW64  Runs Windows NT x86 binaries  No support for mixing of x86 and IA-64 code within a process  User/kernel transitions are thunked to account for structure differences and transition between instruction sets  Only a few dlls are thunked: ntdll.dll user.dll, gdi.dll plus a set of LPC calls  The rest of the dlls are stock x86 binaries copied from the release shares

Driver Specific Issues  Drivers need to support Plug and Play and Power Management  I/O request length is limited to 32 bits  32-bit devices will be double buffered for DMA transfers  Some drivers will need to support 32- and 64-bit versions of IOCTL command

Support 32-Bit IOCTLS  IOCTL structures that contain pointer dependent types have different sizes in 64-bit and 32-bit applications  Driver has to distinguish between Ioctls issued from 32-bit or 64-bit threads  Driver validates the input or output buffer lengths based on the issuer’s bit width

Supporting 32-Bit IOCTL  Three possible solutions  Avoid having pointer types in IOCTL structures  Use the API IoIs32bitProcess()  Use a new bit in the Function code field of the IOCTL code for 64-bit callers

32 And 64-Bit IOCTL Example  IOCTL structure in header file typedef struct _IOCTL_PARAMETERS { PVOID Addr; PVOID Addr; SIZE_T Length; SIZE_T Length; HANDLE Handle; HANDLE Handle; } IOCTL_PARAMETERS, *PIOCTL_PARAMETERS;

32 And 64-Bit IOCTL Example  32-bit IOCTL structure // // This structure is defined inside the driver source code // typedef struct _IOCTL_PARAMETERS_32 { VOID*POINTER_32 Addr; VOID*POINTER_32 Addr; INT32 Length; INT32 Length; VOID*POINTER_32 Handle; VOID*POINTER_32 Handle; } IOCTL_PARAMETERS_32, *PIOCTL_PARAMETERS_32;

32 And 64-Bit IOCTL Example #ifdef _WIN64 case IOCTL_REGISTER: if (IoIs32bitProcess(Irp)) { /* If this is a 32 bit process */ params32 = (PIOCTL_PARAMETERS_32) (Irp->AssociatedIrp.SystemBuffer); if(irpSp->Parameters.DeviceIoControl.InputBufferLength Addr; LocalParam.Handle = params32->Handle; LocalParam.Length = params32->Length; if(irpSp->Parameters.DeviceIoControl.InputBufferLength Addr; LocalParam.Handle = params32->Handle; LocalParam.Length = params32->Length; /* Handle the ioctl here */ /* Handle the ioctl here */ status = STATUS_SUCCESS; Irp->IoStatus.Information = sizeof(IOCTL_PARAMETERS); }

32 And 64-Bit IOCTL Example } else { /* 64bit process IOCTL */ params = (PIOCTL_PARAMETERS) (Irp->AssociatedIrp.SystemBuffer); params = (PIOCTL_PARAMETERS) (Irp->AssociatedIrp.SystemBuffer); if (irpSp->Parameters.DeviceIoControl.InputBufferLength Parameters.DeviceIoControl.InputBufferLength < sizeof(IOCTL_PARAMETERS)) { status = STATUS_INVALID_PARAMETER; } else { RtlCopyMemory(&LocalParam, params, sizeof(IOCTL_PARAMETERS)); /* Handle the ioctl here */ /* Handle the ioctl here */ status = STATUS_SUCCESS; } Irp->IoStatus.Information = sizeof(IOCTL_PARAMETERS); status = STATUS_SUCCESS; } Irp->IoStatus.Information = sizeof(IOCTL_PARAMETERS); } break;

Use New IOCTL Code  Use bit in function field to indicate 64-bit IOCTL  Current IOCTL codes have four fields  New IOCTL code has one extra bit for 64 bit Device Type (16) Access (2) Function (12) Method (2) Device Type (16) Access (2) 64 bit (1) Function (11) Method (2)

DMA APIs  Drivers should use PHYSICAL_ADDRESS typedef to access physical addresses (64 bits long)  Driver must treat all 64 bits as a Valid Physical Address, do not ignore top 32 bits  Drivers must use the Windows DMA APIs  IoAllocateAdapter, IoAllocateAdapterChannel,IoMapTransfer,etc.  Use new scatter/gather DMA APIs  Devices with 64-bit addressing capability will significantly improve performance

Programming Issues  Polymorphic Data usage  Miscellaneous cleanup  Alignment issues

Polymorphic Data Usage  Don’t cast pointer to int, long, ULONG, DWORD  Use UINT_PTR and INT_PTR ImageBase = (PVOID) ((ULONG)ImageBase | 1); ImageBase = (PVOID) ((ULONG_PTR)ImageBase | 1); Should be re-written as

…Polymorphic Data Usage…  Use PtrToUlong and PtrToLong to truncate pointers  Never cast a int or ULONG that stores a truncated pointer back to a pointer  Be careful when computing buffer sizes; Sizes may exceed capacity of ULONG

…Polymorhpic Data Usage  Be careful when calling functions that have pointer OUT parameters void GetBufferAddress(OUT PULONG *ptr); {*ptr=0x ;} void foo() { ULONG bufAddress; ULONG bufAddress; // // // this call causes memory corruption // this call causes memory corruption // // GetBufferAddress((PULONG *)&bufAddress); GetBufferAddress((PULONG *)&bufAddress);}

Miscellaneous Cleanup  -1 != 0xFFFFFFFF  0xFFFFFFFF != invalid handle  DWORD is always 32 bits  Look for DWORD variables used to store pointers or handles  Cast pointers to PCHAR for Pointer arithmetic  ptr = (PVOID) ((PCHAR) ptr + pageSize);

Miscellaneous Cleanup  Use %I to print pointers in debug statements  Addresses >= 0x are not necessarily kernel addresses

Alignment  Misaligned memory references will raise an exception on Win64 #pragma pack (1) /* also set by /Zp switch */ struct AlignSample { ULONG size; void *ptr; }; void foo(void *p) { struct AlignSample s; s.ptr = p;// will cause alignment fault...}

Alignment  Use UNALIGNED macro to fix  Better solution is to put 64-bit values and pointers at the beginning of the structure void foo(void *p) { struct AlignSample s; *(UNALIGNED void *)&s.ptr = p; *(UNALIGNED void *)&s.ptr = p;}

Alignment  Be cautious of any structure packing directives  Be especially cautious of different PACK levels used in the same header  RtlCopyMemory and memcpy will not fault

More Programming Issues  Carefully examine  Explicit and implicit unions with pointers  Data structures stored on disk or exchanged with 32-bit processes  Security descriptors  Code which deals with memory region sizes len = ptr2 – ptr1 len = ptr2 – ptr1 len could be greater than 2^32 len could be greater than 2^32

Miscellaneous Programming Issues  Executive handles can be truncated to 32 bit  Piecemeal size allocations: struct foo { DWORD NumberOfPointers; PVOID Pointers[1]; } xx; Wrong:malloc(sizeof(DWORD)+100*sizeof(PVOID));Correct: malloc(FIELD_OFFSET(struct foo,Pointers) +100*sizeof(PVOID));

Miscellaneous Programming Issues  Be careful when using hexadecimal constants and unsigned values  Following assertion not true on 64 bit systems ~((UINT64)(PAGE_SIZE-1)) == (UINT64)~(PAGE_SIZE-1) PAGE_SIZE = 0x1000UL // Unsigned Long - 32 bits PAGE_SIZE - 1 = 0x00000fff  LHS expression: // Unsigned expansion (UINT64)(PAGE_SIZE -1 ) = 0x fff ~((UINT64)(PAGE_SIZE -1 ))= 0xfffffffffffff000  RHS expression: ~(PAGE_SIZE-1) = 0xfffff000 (UINT64)(~(PAGE_SIZE - 1)) = 0x fffff000  Hence ~((UINT64)(PAGE_SIZE-1))!= (UINT64)(~(PAGE_SIZE-1))

Miscellaneous Programming Issues  Be careful when using unsigned numbers as subscripts: DWORD index = 0; CHAR *p; If (p[index – 1] == ‘0’) causes access violation on Win64!  On 32-bit machines: p[index-1] == p[0xffffffff] == p[-1]  On 64-bit machines: p[index-1] == p[0x ffffffff] != p[-1]

Call To Action  Develop Windows 2000 ready drivers by supporting Plug and Play and Power Management  Start coding using Win64 safe programming practices now!  Use the 64-bit compiler to find problem areas  Make your devices address 64 bits of physical memory

Additional Information  Read “Getting Ready for 64-bit Windows” porting guide on Windows 2000 SDK  Other information available at /guide/platform/strategic/64bit.asp /guide/platform/strategic/64bit.asp /guide/platform/strategic/64bit.asp