virtualcopy函数

virtualcopy函数:

This function binds a specific physical memory range to a statically mapped virtual address.

BOOL VirtualCopy( 
  LPVOID lpvDest, 
  LPVOID lpvSrc, 
  DWORD cbSize, 
  DWORD fdwProtect 
);
Parameters
lpvDest
[out] Pointer to the destination memory, which must be reserved.
lpvSrc
[out] Pointer to committed memory.
cbSize
[in] Size, in bytes, of the region. The allocated pages include all pages containing one or more bytes in the range from lpAddress to lpAddress+ cbSize. This means that a 2-byte range straddling a page boundary causes both pages to be included in the allocated region.
fdwProtect
[in] Type of access protection. If the pages are being committed, any one of a number of flags can be specified, along with the PAGE_GUARD and PAGE_NOCACHE, protection modifier flags. The following table shows the flags that can be specified.
ValueDescription
PAGE_READONLYEnables read access to the committed region of pages. An attempt to write to the committed region results in an access violation. If the system differentiates between read-only access and execute access, an attempt to execute code in the committed region results in an access violation.
PAGE_READWRITEEnables both read and write access to the committed region of pages.
PAGE_EXECUTEEnables execution access to the committed region of pages. An attempt to read or write to the committed region results in an access violation.
PAGE_EXECUTE_READEnables execute and read access to the committed region of pages. An attempt to write to the committed region results in an access violation.
PAGE_EXECUTE_READWRITEEnables execute, read, and write access to the committed region of pages.
PAGE_GUARDPages in the region become guard pages. Any attempt to read from or write to a guard page causes the operating system to raise the STATUS_GUARD_PAGE exception and turn off the guard page status. Guard pages thus act as a one-shot access alarm.

The PAGE_GUARD flag is a page protection modifier. An application uses it with one of the other page protection flags, with one exception: it cannot be used with PAGE_NOACCESS.

When an access attempt leads the operating system to turn off guard page status, the underlying page protection takes over.

If a guard page exception occurs during a system service, the service typically returns a failure status indicator.

PAGE_NOACCESSDisables all access to the committed region of pages. An attempt to read from, write to, or execute in the committed region results in an access violation exception, called a general protection (GP) fault.
PAGE_NOCACHEAllows no caching of the committed regions of pages. The hardware attributes for the physical memory should be specified as no cache. It is useful for device drivers; when, for example, mapping a video frame buffer with no caching. This flag is a page protection modifier and is valid only when used with one of the page protections other than PAGE_NOACCESS.
PAGE_PHYSICALUsed to map a physical memory region. When using this flag, divide the physical address — that is, lpvSrc — by 256.
Return Values

TRUE indicates success; FALSE indicates failure. To get extended error information, call GetLastError.

Remarks

The following code example shows how VirtualCopy is called to map a 128-KB region at physical address 0x64000000.

VirtualCopy(
  pvDest, (void *)(0x64000000/256), 128*1024,   
  PAGE_READWRITE | PAGE_PHYSICAL | PAGE_NOCACHE);

Drivers or applications that run in user mode must use this function, because user-mode threads do not have permission to access physical memory directly.

This function ensures that the operating system maps a statically mapped virtual address to the specific physical address of the registers or frame buffer it needs to read or write. Before the thread calls VirtualCopy, it must first obtain a virtual address by calling VirtualAlloc. VirtualAlloc and VirtualCopy must be called at the beginning of every device driver thread or application that must access peripheral registers. This sequence of function calls is available in the MmMapIoSpace function.

The following code example shows a user-mode driver thread. For more examples, see the sample device driver code provided with Platform Builder, located in the directory %_WINCEROOT%/Platform/%BSP%/Drivers.

#include <windows.h>

#define PHYSADDR  ((PVOID)0x10000000)
// PHYSADDR is the physical address of the peripheral
// registers

#define SIZE  (4800*4)

LPVOID lpv;
BOOL bRet;

lpv = VirtualAlloc(0, SIZE, MEM_RESERVE, PAGE_NOACCESS);
// For a user mode driver, always leave the first
// parameter 0 and use only the flags MEM_RESERVE
// and PAGE_NOACCESS Check the return value: lpv == 0
// is an error

printf(TEXT("VirtualAlloc reservation @%8.8lx/r/n"), lpv);
bRet = VirtualCopy(lpv, PHYSADDR>>8, SIZE, PAGE_READWRITE | PAGE_NOCACHE | PAGE_PHYSICAL);
// The lpv parameter is the virtual address returned
// by VirtualAlloc().
// Always use PAGE_NOCACHE */

// Check the return value: bRet ==0 is an error */
printf(TEXT("VirtualCopy returned: %d/r/n"), bRet);

// At this point lpv is a virtual address which maps
// the I/O registers
// at PHYSADDR for SIZE bytes */

The destination address defined by the cbSize parameter must be an address range that has been reserved by a call to VirtualAlloc. VirtualAlloc, with the flags MEM_RESERVE and PAGE_NOACCESS and a size greater than or equal to 2 MB, is allocated outside the process virtual memory space. Otherwise, it is allocated inside the processes virtual memory space.

Requirements

OS Versions: Windows CE 2.10 and later.
Header: Pkfuncs.h.
Link Library: Coredll.lib.

 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值