Protection Attributes

[Previous] [Next]

Individual pages of physical storage allocated can be assigned different protection attributes. The protection attributes are shown in the following table.

Protection Attribute Description
PAGE_NOACCESS Attempts to read from, write to, or execute code in this page raise an access violation.
PAGE_READONLY Attempts to write to or execute code in this page raise an access violation.
PAGE_READWRITE Attempts to execute code in this page raise an access violation.
PAGE_EXECUTE Attempts to read or write memory in this page raise an access violation.
PAGE_EXECUTE_READ Attempts to write to memory in this page raise an access violation.
PAGE_EXECUTE_READWRITE There is nothing you can do to this page to raise an access violation.
PAGE_WRITECOPY Attempts to execute code in this page raise an access violation. Attempts to write to memory in this page cause the system to give the process its own private copy of the page (backed by the paging file).
PAGE_EXECUTE_WRITECOPY There is nothing you can do to this region to raise an access violation. Attempts to write to memory in this page cause the system to give the process its own private copy of the page (backed by the paging file).

The x86 and Alpha CPUs do not support the execute protection attribute, although the operating system software does support this attribute. These CPUs treat read access as execute access. This means that if you assign PAGE_ EXECUTE protection to memory, that memory will also have read privileges. Of course, you should not rely on this behavior because Windows implementations on other CPUs might very well treat execute protection as execute-only protection.

Windows 98
Windows 98 supports only the PAGE_NOACCESS, PAGE_READONLY, and PAGE_READWRITE protection attributes.

Copy-On-Write Access

The protection attributes listed in the preceding table should all be fairly self-explanatory except the last two: PAGE_WRITECOPY and PAGE_EXECUTE_ WRITECOPY. These attributes exist to conserve RAM usage and paging file space. Windows supports a mechanism that allows two or more processes to share a single block of storage. So, if 10 instances of Notepad are running, all instances share the application's code and data pages. Having all instances share the same storage pages greatly improves the performance of the system—but this does require that all instances consider the storage to be read-only or execute-only. If a thread in one instance wrote to the storage modifying it, the storage as seen by the other instances would also be modified, causing total chaos.

To prevent this chaos, copy-on-write protection is assigned to the shared block of storage by the operating system. When an .exe or .dll module is mapped into an address space, the system calculates how many pages are writable. (Usually, the pages containing code are marked as PAGE_EXECUTE_READ while the pages containing data are marked PAGE_READWRITE.) Then, the system allocates storage from the paging file to accommodate these writable pages. This paging file storage is not used unless the module's writable pages are actually written to.

When a thread in one process attempts to write to a shared block, the system intervenes and performs the following steps:

  1. The system finds a free page of memory in RAM. Note that this free page will be backed by one of the pages allocated in the paging file when the module was first mapped into the process's address space. Since the system allocated all the potentially required paging file space when the module was first mapped, this step cannot possibly fail.
  2. The system copies the contents of the page attempting to be modified to the free page found in step 1. This free page will be assigned either PAGE_READWRITE or PAGE_EXECUTE_READWRITE protection. The original page's protection and data does not change at all.
  3. The system then updates the process's page tables so that the accessed virtual address now translates to the new page of RAM.

After the system has performed these steps, the process can access its very own private instance of this page of storage. In Chapter 17, sharing storage and copy-on-write protection are covered in much more detail.

In addition, you should not pass either PAGE_WRITECOPY or PAGE_ EXECUTE_WRITECOPY when you are reserving address space or committing physical storage using the VirtualAlloc function. Doing so will cause the call to VirtualAlloc to fail; calling GetLastError returns ERROR_ INVALID_PARAMETER. These two attributes are used by the operating system when it maps .exe and DLL file images.

Windows 98
Windows 98 does not support copy-on-write protection. When Windows 98 sees that copy-on-write protection has been requested, it immediately makes copies of the data instead of waiting for an attempted memory write.

Special Access Protection Attribute Flags

In addition to the protection attributes already discussed, there are three protection attribute flags: PAGE_NOCACHE, PAGE_WRITECOMBINE, and PAGE_GUARD. You use these three flags by bitwise ORing them with any of the protection attributes except PAGE_NOACCESS.

The first of these protection attribute flags, PAGE_NOCACHE, disables caching of the committed pages. This flag is not recommended for general use—it exists mostly for hardware device driver developers who need to manipulate memory buffers.

The second protection attribute flag, PAGE_WRITECOMBINE, is also for device driver developers. It allows multiple writes to a single device to be combined together in order to improve performance.

The last protection attribute flag, PAGE_GUARD, allows an application to receive a notification (via an exception) when a byte on a page has been written to. There are some clever uses for this flag. Windows 2000 uses this flag when it creates a thread's stack. See the section "A Thread's Stack" in Chapter 16 for more information about this flag.

Windows 98
Windows 98 ignores the PAGE_NOCACHE, PAGE_WRITECOMBINE, and PAGE_GUARD protection attribute flags.



Programming Applications for Microsoft Windows
Programming Applications for Microsoft Windows (Microsoft Programming Series)
ISBN: 1572319968
EAN: 2147483647
Year: 1999
Pages: 193

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net