Device Driver Best Practices in Windows Embedded Compact 7. Douglas Boling Boling Consulting Inc.

Similar documents
Creating a More Secure Device with Windows Embedded Compact 7. Douglas Boling Boling Consulting Inc.

CA Data Protection. Content Provider Development Guide. Release 15.0

2.6.1 Creating an Acronis account Subscription to Acronis Cloud Creating bootable rescue media... 12

Fastboot Techniques for x86 Architectures. Marcus Bortel Field Application Engineer QNX Software Systems

High Confidence Computing with the New Windows Embedded Compact 7

Special FEATURE. By Heinrich Munz

COS 318: Operating Systems. I/O Device and Drivers. Input and Output. Definitions and General Method. Revisit Hardware

Lecture 25 Symbian OS

CS 377: Operating Systems. Outline. A review of what you ve learned, and how it applies to a real operating system. Lecture 25 - Linux Case Study

Building Applications Using Micro Focus COBOL

UEFI Driver Development Guide for All Hardware Device Classes

Windows8 Internals, Sixth Edition, Part 1

WinDriver 5.22 User s Guide. Jungo Ltd

OPERATING SYSTEMS STRUCTURES

Linux Driver Devices. Why, When, Which, How?

Using a Patched Vulnerability to Bypass Windows 8 x64 Driver Signature Enforcement. MJ0011 th_decoder@126.com

REAL TIME OPERATING SYSTEM PROGRAMMING-II: II: Windows CE, OSEK and Real time Linux. Lesson-12: Real Time Linux

EMC RepliStor for Microsoft Windows ERROR MESSAGE AND CODE GUIDE P/N REV A02

I/O Device and Drivers

Why Threads Are A Bad Idea (for most purposes)

Introduction to Embedded Systems. Software Update Problem

Process Description and Control william stallings, maurizio pizzonia - sistemi operativi

Hi and welcome to the Microsoft Virtual Academy and

A+ Guide to Managing and Maintaining Your PC, 7e. Chapter 16 Fixing Windows Problems

Effects of Generic Com in App-V 5 SP2 Deployment Performance

Last Class: OS and Computer Architecture. Last Class: OS and Computer Architecture

Operating System Security

Unit 4 Objectives. System Software. Component 4: Introduction to Information and Computer Science. Unit 4: Application and System Software Lecture 2

System Structures. Services Interface Structure

Operating System Components and Services

Robustness Testing of the Microsoft Win32 API

The Bus (PCI and PCI-Express)

Mutual Exclusion using Monitors

Introduction to Operating Systems. Perspective of the Computer. System Software. Indiana University Chen Yu

AVR115: Data Logging with Atmel File System on ATmega32U4. Microcontrollers. Application Note. 1 Introduction. Atmel

2.8.1 Creating an Acronis account Subscription to Acronis Cloud Creating bootable rescue media... 16

Red Hat Linux Internals

Republic Polytechnic School of Information and Communications Technology C226 Operating System Concepts. Module Curriculum

Chapter 3: Operating-System Structures. Common System Components

KVM: A Hypervisor for All Seasons. Avi Kivity avi@qumranet.com

Melde- und Analysestelle Informationssicherung MELANI Torpig/Mebroot Reverse Code Engineering (RCE)

Integrating Gestures in Windows Embedded Compact 7. Douglas Boling Boling Consulting Inc.

MS Active Sync: Sync with External Memory Files

How to design and implement firmware for embedded systems

Windows Phone 8 Security deep dive

Example of Standard API

VMware ThinApp Agentless Application Virtualization Overview W HITE P APER

SYSTEM ecos Embedded Configurable Operating System

Optimizing Performance. Training Division New Delhi

Debugging A MotoHawk Application using the Application Monitor

Config software for D2 systems USER S MANUAL

The Microsoft Windows Hypervisor High Level Architecture

9 Headless Systems & Remote Management

EUCIP - IT Administrator. Module 2 Operating Systems. Version 2.0

CS161: Operating Systems

How to write a design document

UNIT 4 Software Development Flow

Page 1 of 5. IS 335: Information Technology in Business Lecture Outline Operating Systems

Navigating Endpoint Encryption Technologies

Programación de Sistemas Empotrados y Móviles (PSEM)

Plug and Play for Windows 2000

ExpressSAS Host Adapter 6Gb v2.15 Linux

Application Whitelisting - Extend your Security Arsenal? Mike Baldi Cyber Security Architect Honeywell Process Solutions

OPERATING SYSTEM SERVICES

AN10860_1. Contact information. NXP Semiconductors. LPC313x NAND flash data and bad block management

An Easier Way for Cross-Platform Data Acquisition Application Development

Waspmote IDE. User Guide

Benefits of Intel Matrix Storage Technology

Leak Check Version 2.1 for Linux TM

Managing Rack-Mount Servers

Virtual Machines.

How To Write A Windows Operating System (Windows) (For Linux) (Windows 2) (Programming) (Operating System) (Permanent) (Powerbook) (Unix) (Amd64) (Win2) (X

Real-Time Systems Prof. Dr. Rajib Mall Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Table of Contents. Safety Warnings..3. Introduction.. 4. Host-side Remote Desktop Connection.. 5. Setting Date and Time... 7

SATA+Ultra ATA RAID CONTROLLER RC212. User Manual

Going Linux on Massive Multicore

Developing, Deploying, and Debugging Applications on Windows Embedded Standard 7

Unity web- player issues in browsers & in client system

Dialogic Global Call API

PARALLELS SERVER 4 BARE METAL README

An Implementation Of Multiprocessor Linux

Computers: Tools for an Information Age

Imation LOCK User Manual

Embedded PC The modular Industrial PC for mid-range control. Embedded PC 1

Review from last time. CS 537 Lecture 3 OS Structure. OS structure. What you should learn from this lecture

Best Practices for Deploying, Replicating, and Managing Real-Time and FPGA Applications. ni.com

Chapter 3 Operating-System Structures

A White Paper By: Dr. Gaurav Banga SVP, Engineering & CTO, Phoenix Technologies. Bridging BIOS to UEFI

IBM Tivoli Monitoring V6.2.3, how to debug issues with Windows performance objects issues - overview and tools.

ReactOS is (not) Windows. Windows internals and why ReactOS couldn t just use a Linux kernel

MF Digital 5000 USB SD Platform Series Operating Manual

RELIABLE OPERATING SYSTEMS

PARALLELS SERVER BARE METAL 5.0 README

FRONT FLYLEAF PAGE. This page has been intentionally left blank

Detecting Malware With Memory Forensics. Hal Pomeranz SANS Institute

LabVIEW Advanced Programming Techniques

Operating Systems 4 th Class

OPERATING SYSTEMS Software in the Background. Chapter 2

Chapter 6, The Operating System Machine Level

Transcription:

Device Driver Best Practices in Windows Embedded Compact 7 Douglas Boling Boling Consulting Inc.

About Douglas Boling Independent consultant specializing in Windows Mobile and Windows Embedded Compact (Windows CE) On-Site Instruction Consulting and Development Author Programming Embedded Windows CE Fourth Edition

Agenda Basic drivers General Tips Threading and Drivers Stable Drivers

What is a Windows Embedded Compact Driver? It s a DLL Loaded by the Device Manager Kernel mode or User mode drivers Kernel mode drivers Faster User mode drivers More secure Most drivers expose a Stream Interface How the OS interfaces with the driver

Major Stream Interface Functions xxx_init xxx_predeinit xxx_deinit Called when initializing/deinitializing the device driver xxx_open xxx_preclose xxx_close Called when an application opens and closes a device xxx_read xxx_write xxx_seek Called to read and write data to and from the device xxx_iocontrol Catchall API for functions not anticipated by standard interface

Plug and Play Loading? Drivers are loaded (and unloaded) as needed Boot time Device Discovery USB Device plug in / remove SD Card insert / remove PC Card insert / remove Application request OS finds drivers using the registry

Driver Tips

Use Debug Zones Debug Zones are runtime configurable diagnostic messages Configurable at Compile time Load time Interactively Zones in all the Microsoft code Use to learn what is going on Zones allow your driver to be debugged Take care with performance using too many messages

Use the Registry Interrupt thread priority System Interrupt / hardware interrupt mapping Base Address of hardware Size of hardware window(s)

Using the Registry The xxx_init function is passed the name of the Active key Open this key and read the Key value This is the name of the registry key that caused the driver to load This can be \drivers\builtin\<drivername> \drivers\usb\clientdrivers\<drivername> Others. All under HKEY_LOCAL_MACHINE Store configuration information under this key

xxx_init Code Must Be Quick The boot sequence serializes load of drivers One thread calls each _Init function in sequence Don t block Major parts of the OS API isn t available at driver load during boot Kernel, Filesys, some driver support available GWES, Shell, everything else not available Don t spend time waiting on hardware Spin off separate thread to wait on hardware Fail open of device until secondary thread completes wait

Never Use Global Data in a Driver Drivers are DLLs loaded by the device manager Multiple instances of the driver can be loaded But only one copy of the code is loaded Global data will be shared across all instances Probability not what you want Use handle returned by xxx_init to store ptr to instance data Handle is passed back in other calls (Open, DeInit, PreDeinit) Open returns handle that can is returned in other calls

Quick Note on Performance Drivers are DLLs System notifies DLLs when threads are created and destroyed Typically, drivers don t need this information In LibMain, in Process Attach notification call BOOL DisableThreadLibraryCalls (HMODULE hlibmodule);

Threading and Drivers Drivers are implicitly multithreaded Multiple applications can call a driver at the same time Drivers must protect internal data structures from corruption Use Critical Sections to protect shared data structures Drivers must have a method of unblocking blocked threads When the driver closes When the application unexpectedly terminates

Use PreClose and PreDeinit Allows driver to free threads blocked within driver code PreClose is called before Close Driver should unblock any blocked threads within driver Driver should cancel any pending IO PreDeinit is called before Deinit Allows driver to unblock any threads Allows driver to inform others it is unloading Driver already unhooked from driver list when Deinit called

Unexpected Application Termination Applications that die unexpectedly can cause problems Application threads blocked with drivers can prevent process from being removed from memory When an application is terminated Drivers will receive IOCTL_PSL_NOTIFY IoControl call Once for each time driver opened by application Driver will then receive one PreClose call

Know the Thread Priority APIs

Priority Bug 1 What is wrong with this code? // save current priority int oldpri = GetThreadPriority (hthread); // Elevate our thread priority CeSetThreadPriority (hthread, 100); // Do work... // Restore original priority... (really?) CeSetThreadPriority (hthread, oldpri);

Priority Bug 1 Answer // save current priority (typically normal == 3) int oldpri = GetThreadPriority (hthread); // Elevate our thread priority CeSetThreadPriority (hthread, 100); // Do work... // Using saved priority (3) with new API. BADBAD! CeSetThreadPriority (hthread, oldpri); Code mixing new and old APIs, thread ends up at priority 0-7

Bug 1, Proper Code // save current priority int oldpri = CeGetThreadPriority (hthread); // Elevate our thread priority CeSetThreadPriority (hthread, 100); // Do work... // Properly restoring original priority CeSetThreadPriority (hthread, oldpri); Moral: Don t mix the priority APIs!

Priority Bug 2 What is wrong with this code? CeSetThreadPriority (h, THREAD_PRIORITY_NORMAL);

Priority Bug 2 Answer Using constants defined for SetThreadPriority CeSetThreadPriority (h, THREAD_PRIORITY_NORMAL); Defines are used by SetThreadPriority, not CeSetThreadPriority #define THREAD_PRIORITY_TIME_CRITICAL 0 #define THREAD_PRIORITY_HIGHEST 1 #define THREAD_PRIORITY_ABOVE_NORMAL 2 #define THREAD_PRIORITY_NORMAL 3 #define THREAD_PRIORITY_BELOW_NORMAL 4 #define THREAD_PRIORITY_LOWEST 5 #define THREAD_PRIORITY_ABOVE_IDLE 6 #define THREAD_PRIORITY_IDLE 7

Basic Steps to Stable Drivers You must check return codes You must check memory allocations Never trust a pointer Most data is passed to the driver via pointers There is sure way to test the validity of a pointer The driver must be able to tolerate a bad pointer The OS catches all thread exceptions at the call to the driver The driver should catch them first

The try except Block try { // guarded code } except ( expression ) { // exception handler code } Expression values EXCEPTION_CONTINUE_EXECUTION EXCEPTION_CONTINUE_SEARCH EXCEPTION_EXECUTE_HANDLER

Use try except Wrap every entry point in your code Callbacks DLL entry points Driver entry points Around all accesses to buffers passed to the driver Don t use C++ exception handling in drivers / OAL code

What is Wrong Here? CRITICAL_SECTION cs; try { EnterCriticalSection (&cs); // Some code here LeaveCriticalSection (&cs); } except (EXCEPTION_EXECUTE_HANDLER) { // Process exception }

The try finally Block CRITICAL_SECTION cs; try { EnterCriticalSection (&cs); // Some code here } finally { LeaveCriticalSection (&cs); }

The try finally Block CRITICAL_SECTION cs; try { EnterCriticalSection (&cs); // Some code here leave; // Code here not executed } finally { LeaveCriticalSection (&cs); }

Use try finally finally clause always executes If you fall out the end of the try block If you return in the middle of the try block If an exception occurs Don t return out of a try, finally block It generates lots of code When it works at all Use the leave keyword

Much Better Code CRITICAL_SECTION cs; try { EnterCriticalSection (&cs); // Some code here } except (EXCEPTION_EXECUTE_HANDLER) { // Exception handling code here } finally { LeaveCriticalSection (&cs); }

Good Drivers Are Written in C not C++ I m not a language bigot I just speak from experience One of the goals of C++ is to obfuscate interior function If the hardware is new (or failing) its this interior function that is failing In other words, the only way to do good, efficient, and system-level and portable C++ ends up to limit yourself to all the things that are basically available in C. Linus Torvalds

Summary Basic stream interface makes drivers simple Use the registry to query configuration information Check xxx_init performance Take care to protect data structures from thread conflicts NEVER TRUST A POINTER!

Questions Doug Boling Boling Consulting Inc. www.bolingconsulting.com dboling @ bolingconsulting.com

2011 Microsoft Corporation. All All rights reserved. This This presentation is for informational is informational purposes purposes only. Microsoft only. makes Microsoft no warranties, makes no express warranties, or implied, express in this or summary. implied, in this summary.