English 中文(简体)
Memory access exception handling with MinGW on XP
原标题:

I am trying to use the MinGW GCC toolchain on XP with some vendor code from an embedded project that accesses high memory (>0xFFFF0000) which is, I believe, beyond the virtual mem address space allowed in civilian processes in XP.

I want to handle the memory access exceptions myself in some way that will permit execution to continue at the instruction following the exception, ie ignore it. Is there some way to do it with MinGW? Or with MS toolchain?

The vastly simplified picture is thus:

/////////////
// MyFile.c
MyFunc(){
    VendorFunc_A();
}

/////////////////
// VendorFile.c
VendorFunc_A(){
    VendorFunc_DoSomeDesirableSideEffect();
    VendorFunc_B();
    VendorFunc_DoSomeMoreGoodStuff();
}

VendorFunc_B(){
    int *pHW_Reg = 0xFFFF0000;
    *pHW_Reg = 1;  // Mem Access EXCEPTION HERE
    return(0);     // I want to continue here
}

More detail: I am developing an embedded project on an Atmel AVR32 platform with freeRTOS using the AVR32-gcc toolchain. It is desirable to develop/debug high level application code independent of the hardware (and the slow avr32 simulator). Various gcc, makefile and macro tricks permit me to build my Avr32/freeRTOS project in the MinGW/Win32 freeRTOS port enviroment and I can debug in eclipse/gdb. But the high-mem HW access in the (vendor supplied) Avr32 code crashes the MinGW exe (due to the mem access exception).

I am contemplating some combination of these approaches:

1) Manage the access exceptions in SW. Ideally I d be creating a kind of HW simulator but that d be difficult and involve some gnarly assembly code, I think. Alot of the exceptions can likely just be ignored.

2) Creating a modified copy of the Avr32 header files so as to relocate the HW register #defines into user process address space (and create some structs and linker sections that commmit those areas of virtual memory space)

3) Conditional compilation of function calls that result in highMem/HW access, or alernatively more macro tricks, so as to minimize code cruft in the real HW target code. (There are other developers on this project.)

Any suggestions or helpful links would be appreciated.

This page is on the right track, but seems overly complicated, and is C++ which I d like to avoid. But I may try it yet, absent other suggestions. http://www.programmingunlimited.net/siteexec/content.cgi?page=mingw-seh

问题回答

You need to figure out why the vendor code wants to write 1 to address 0xFFFF0000 in the first place, and then write a custom VendorFunc_B() function that emulates this behavior. It is likely that 0xFFFF0000 is a hardware register that will do something special when written to (eg. change baud rate on a serial port or power up the laser or ...). When you know what will happen when you write to this register on the target hardware, you can rewrite the vendor code to do something appropriate in the windows code (eg. write the string "Starting laser" to a log file). It is safe to assume that writing 1 to address 0xFFFF0000 on Windows XP will not be the right thing to do, and the Windows XP memory protection system detects this and terminates your program.

I had a similar issue recently, and this is the solution i settled on:

Trap memory accesses inside a standard executable built with MinGW

First of all, you need to find a way to remap those address ranges (maybe some undef/define combos) to some usable memory. If you can t do this, maybe you can hook through a seg-fault and handle the write yourself.

I also use this to "simulate" some specific HW behavior inside a single executable, for some already written code. However, in my case, i found a way to redefine early all the register access macros.





相关问题
handling exceptions IN Action Filters

Is there a better way to handle exceptions that occur inside an Action Filter itself in ASP .NET MVC? There re 2 ways I can think of at the moment. Using a try catch and setting the HTTP Status ...

既可捕获,又可举出例外。

我有一种办法,可以进入亚洲开发银行,因此,我国的亚行在多瑙河航道中的所有 st子都位于一个试捕区。 它正在追捕Kexception

Cross compiler exception handling - Can it be done safely?

I am doing some maintenance on a C++ windows dll library that is required to work with different VC++ compilers (as I don’t want to address different mangling schemes). I have already eliminated any ...

File Handling Issue

I am developing a tool in c#, at one instance I start writing into a xml file continuously using my tool,when i suddenly restart my machine the particular xml file gets corrupted, what is the reason ...

Watch a memory location/install data breakpoint from code?

We have a memory overwrite problem. At some point, during the course of our program, a memory location is being overwritten and causing our program to crash. the problem happens only in release mode. ...

Unit Test for Exceptions Message

Is there a simple (Attribute-driven) way to have the following test fail on the message of the exception. [TestMethod()] [ExpectedException(typeof(ArgumentException))] public void ExceptionTestTest() ...

热门标签