Click here to learn
about this Sponsor:
Home  |  News  |  Articles  |  Polls  |  Forum  |  Directory

Keywords: Match:
Understanding Windows CE's VirtualSetAttributes function
by Kurt Kennett (Mar. 22, 2007)

Foreword: In this brief technical note, Windows CE developer Kurt Kennett looks at how virtual memory is used to map peripheral registers into a Windows CE process. He points out that peripheral registers often have "attributes" that exhibit behavior different from ordinary memory. The VirtualSetAttributes function takes care of this.



Understanding VirtualSetAttributes

by Kurt Kennett

Virtual Memory is fantastic. It allows you to create this personalized "view" of the memory space of a computer, and rearrange where things are physically to suit your desires. This is especially good for the organization of registers of memory-mapped peripherals. By allocating a range of virtual memory, and mapping that memory onto a physical register bank, you get a pointer to a C-like structure containing fields that correspond to each register.


This procedure is fairly straightforward, and allows easy access to the hardware registers of many types of CPUs and peripherals in a Windows CE system. Usually this is accomplished via one of two mechanisms:
  1. VirtualAlloc() + VirtualCopy() / VirtualFree()
    • A range of virtual address space is allocated that can map the physical space needed
    • The virtual to physical mapping is set up for those pages
    • When necessary, the range of virtual pages is released and the mapping destroyed

  2. MmMapIoSpace() / MmUnmapIoSpace()
    • This effectively does the first two steps above in one function call, with fixups on the address if it is not page-aligned.
The functions in (1) are exported to all drivers and applications. The functions in (2) are implemented as part of the CEDDK (CE Driver Development Kit), and are customizable for any particular platform.

With some new processor systems, it becomes optional and sometimes necessary to set "attributes" on virtual memory pages that map specific portions of physical memory. This is done to set qualities on the physical resources that are mapped. For example, TLB (Translation Lookaside Buffer) entries for a set of virtually mapped pages may need to be configured with specific attributes in order to allow a particular bus on the system to access the memory. If an operating system has segmented data and code areas, it can set the "no execute" bit to make sure the CPU doesn't fetch instructions from data regions. Another example is ARM's TrustZone technology, which uses a "secure" bit to establish privilege on certain memory regions. One of the most common examples is a display buffer, where you do not want to cache writes to a frame buffer, but want to use a hardware write buffer instead.

To allow this type of customization, the VirtualSetAttributes() function is provided. This allows us to manually call the function after the sequence in (1) above, or automatically in the function provided by an OEM in (2). Using the mechanism in (2) is more desirable, as it leads to more portable code (less specificity with regard to setting attributes for an end system in driver code). However, using (2) does not easily allow for multiple register banks to be allocated in one virtual page (leading to waste of virtual address space). Depending on your needs, you can use whichever method you desire.

By default the memory system is generic and does not know which buses are used to access a particular piece of physical memory. Because of this, if the default flags are used, a peripheral bus might not be accessed properly. You can set the attributes properly for your particular system by using the VirtualSetAttributes(). For example:

/* allocate dwBytes of virtual address space */
void *pVirtMem = VirtualAlloc(NULL,
dwBytes,
MEM_RESERVE,
0);

/* map the memory to physical address 0x10000000 (arbitrary) */
VirtualCopy(pVirtMem,
(void *)(0x10000000>>8),
dwBytes,
PAGE_PHYSICAL | PAGE_READWRITE | PAGE_NOCACHE);

/* set the attributes on the pages */
VirtualSetAttributes(pVirtMem,
dwBytes,
0x13,
0x13,
NULL);

The attributes on the set of pages mapping the physical address would now have the bits 0x13 set, to allow for possible use of a specific bus that holds the physical peripheral. The use of 0x13 is arbitrary here, because these flags are CPU-architecture specific. Consult the appropriate documentation for your CPU architecture to learn what each bit does in your page table entry. You might accidentally change the physical page number, which could cause some unexpected behavior! Caveats, and more information on the arguments to the virtual memory functions discussed here, can be found on MSDN help.


Copyright (c) 2007 Microsoft Corp. All rights reserved. Reproduced by WindowsForDevices.com with permission. This article was originally published on the Windows CE Base Team Blog, here.



About the author: Kurt Kennett is a development lead on the Windows CE base team responsible for drivers and forward-looking technology, including both OAL and bootloader design and implementation. His previous industry experience included stints at Electronic Arts, General Electric, Nortel Networks, and Intrinsyc Software. When not playing with computers, Kennett does science fiction/fantasy writing as well as some romance writing. Some of his work has over 25,000 readers on the Internet.



Related stories:

(Click here for further information)


Windows XP for Embedded Applications
This white paper describes the benefits of using Windows XP when developing embedded applications.

A Manager's Guide to Selecting a Mobile Device Operating System
This white paper offers a comparative review of Microsoft Windows CE and Windows Mobile.

Visual Basic 6.0 to .NET Migration
This paper focuses on the methodology and techniques which Infosys (Microsoft Technology Center) has developed for migrating VB 6.0 Applications to .NET. Our approach ensures a smooth, cost effective, and efficient migration.

Mobile Device Security: Securing the Handheld, Securing the Enterprise
This whitepaper identifies security threats to corporate data on mobile devices and details how mobile devices can become a "backdoor" to the enterprise.

Mobile Device Security: The Eight Areas of Risk
It's common knowledge that adding mobile devices to your network increases security risks. There are multiple facets to mobile security, all of which should be paid close attention to. This E-Guide presents a more in depth look into the eight key areas of securing wireless devices.

Quality Assurance and .NET
This paper discusses best practices for functional, regression and load testing of .NET applications.

SCADA Security in Integrated Networks
As businesses leverage their SCADA systems by integrating them into the business networks, they must also assure the security of the SCADA system.

The Advantages of Small Form Factor HMI
HMIs have mutated and changed with new requirements, and they have become more flexible and capable. And while they've been doing that, they've become smaller and more useful.

9 Critical Requirements for Web Application Security
Learn why your Web applications expose dangerous security breaches and what’s required to effectively protect your Web applications and the sensitive information behind them.

 


Got a HOT tip?   please tell us!
Free weekly newsletter
Enter your email...
Click here for a profile of each sponsor:
PLATINUM SPONSORS
(Become a sponsor)

ADVERTISEMENT
(Advertise here)


Updated! The latest Windows-powered...

mobile phones!

other cool
gadgets

HOT TOPICS
Microsoft targets PNDs with new embedded OS
Microsoft tips .NET MF 3.0 highlights
Microsoft previews Windows Embedded Standard
Microsoft offers free Windows CE 6.0 textbook
Microsoft renames embedded operating systems
Microsoft unveils Windows Mobile 6.1
New Atom models target low-cost PCs
REFERENCE GUIDES
Windows Device Showcase
Intro to Windows Embedded
Intro to Shared Source
Real-time Windows Embedded
Windows Embedded books
Join our Windows Embedded discussion forums:
Windows XP Embedded
Windows CE
Windows Mobile


Windows Embedded developer newsgroups
Windows CE
XP Embedded
PocketPC
Smartphone

Microsoft's Windows Embedded resources
Embedded dev center
Mobile dev center
Windows CE tutorials
XP Embedded tutorials
Windows Embedded seminars
Windows Embedded application categories
3rd-party partners


BREAKING NEWS

• Windows CE PND sports heads-up display
• ARM9 SoC gets Windows CE BSP
• First Atom-based nano-ITX board?
• HTC spins WiMAX phone?
• Windows Mobile app turns phone into "air mouse"
• Budget smartphone adds GPS, WiFi
• Vest-pocket Vista PC unveiled
• Phones rev'd to Windows Mobile 6.1
• Twin POS systems run WEPOS
• Windows Mobile Firefox "just weeks away"
• Mobile WiMAX service launches in U.S.
• PMP sports head-mounted display
• Windows Mobile app fakes phone calls
• 802.11n modules target mobile devices
• HTC Touch jilts U.S.


MOST POPULAR (last 90 days)
• "Netbook" uses Intel's Atom N270
• Windows CE takes on Linux in low-end netbooks
• Windows Mobile 6.1 phone has GPS
• T-Mobile's Touch Diamond clone does HSUPA
• iPhone-like Windows Mobile device has 16GB of storage
• HTC phone has slide-out keyboard and TV output
• Windows Mobile trouncing the iPhone?
• HTC releases Touch Diamond ROM upgrade
• Sprint upgrades HTC Touch, Mogul
• Intel's Atom powers mini-ITX board
MOST POPULAR (Classics from the vault)
Windows XP Embedded USB boot
Troubleshooting Windows XPe's blue screen "Stop 0x0000007B" error
Asus reveals $190 mini notebook
Windows Mobile 6 SDKs available for download
Windows Mobile VPN client plays with Cisco
HTC adds GPS to Windows Mobile Touch line
Microsoft unveils Windows Mobile 6.1
Guide to HTC's Windows Mobile smartphone platforms
Customizing Windows XP Embedded thin clients
Visual Studio 2008 adds mobile application features

Also visit our sister sites:


Sign up for WindowsForDevices.com's...

news feed

Home  |  News  |  Articles  |  Polls  |  Forum  |  Directory  |  About  |  Contact
 

Ziff Davis Enterprise Home | Contact Us | Advertise | Link to Us | Reprints | Magazine Subscriptions | Newsletters
Tech RSS Feeds | White Papers | ROI Calculators | Tech Podcasts | Tech Video | VARs | Channel News

Baseline | Careers | Channel Insider | CIO Insight | DesktopLinux | DeviceForge | DevSource | eSeminars |
eWEEK | Enterprise Network Security | LinuxDevices | Linux Watch | Microsoft Watch | Mid-market | Networking | PDF Zone |
Publish | Security IT Hub | Strategic Partner | Web Buyer's Guide | Windows for Devices

Developer Shed | Dev Shed | ASP Free | Dev Articles | Dev Hardware | SEO Chat | Tutorialized | Scripts |
Code Walkers | Web Hosters | Dev Mechanic | Dev Archives | igrep

Use of this site is governed by our Terms of Service and Privacy Policy. Except where otherwise specified, the contents of this site are copyright © 1999-2008 Ziff Davis Enterprise Holdings Inc. All Rights Reserved. Reproduction in whole or in part in any form or medium without express written permission of Ziff Davis Enterprise is prohibited. Windows is a trademark or registered trademark of Microsoft Corporation in the United States and/or other countries and is used by WindowsForDevices under license from owner. All other marks are the property of their respective owners. WindowsForDevices is an independent publication not affiliated with Microsoft Corporation.