Another question about unaligned access

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • CodeJingle
    New Member
    • Feb 2013
    • 4

    Another question about unaligned access

    There is some kernel code that is playing with uncached memory in an unaligned fashion and halting the system, and hardware-supported unaligned access only works for cached memory. I want changes to existing code to be minimal. Is there a way to tell the compiler to access the memory a byte at a time, equivalent to the __unaligned keyword in MS C++?
    Code:
    unsigned char* uncachedMemory = (unsigned char*)GetUncachedMemory(); // returns 8 bytes of uncached memory, with an address that is 4-byte aligned
    
    unsigned int* somePointer = (unsigned int*)(uncachedMemory+1);
    
    *somePointer = 0xFFFFFFFFu; // whoops! - halts system
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Basically non, __unaligned is a MS C++ compiler extension and you should not expect to find it in any other platform.

    That isn't to say you can't do it you will just have to code the byte accesses yourself.

    Comment

    • CodeJingle
      New Member
      • Feb 2013
      • 4

      #3
      Your solution is trivial and it would be my first choice, however is in not within the constraints I've been given.

      Expecting unaligned access to uncached memory to cause exception is specific to the cpu, not the OS or development platform. And so it should not be unexpected to find an equivalent.

      I either can make a tiny change to the existing code like adding another keyword to the type, or I have to change the behavior of the compiler to replace intrinsic memory copies from whatever code the compiler normally emits to code that does a strict align equivalent.

      You could imagine I am being forced to solve the problem the wrong way, and I just want to know how to do it?

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        No __unaligned is not in the standard therefore it is an extension in that specific compiler. You can not rely on extensions because even if on another platform the same problem exists you have go guarantee that the authors of the compiler will have provide a solution and even if they have it may not be the same keyword.

        The solution is to write portable code that access memory byte by byte. Not alter the behaviour of the compiler which is basically at this level impossible. You write functions to insert into and copy out of unaligned memory internally the functions access memory byte by byte and then you use those functions throughout your code.

        Once you have limited the issue to a few small functions you can then write versions of those functions that are platform specific if specific platforms provide neat solutions to the problem.

        Sorry I do realise that this does not involve minimal code changes but if the code was going to operate in this fashion it should have taken this consideration into account in the first place.

        BTW since you have not said what compiler you are using it may be possible that the compiler you are using does provide a neat solution to this problem, however we can't tell because we don't know what it is.

        Comment

        • CodeJingle
          New Member
          • Feb 2013
          • 4

          #5
          Yes I own copies of the C and C++ standard specifications, you and I both know very well that __unaligned is an MS extension. My point was that if any other ARM compilers have extensions covering functionality and behavior not covered by the standards that are specific to that processor then I would expect there to be something equivalent in those extensions.

          Even on the MS development platform, the __unaligned keyword only works when building for Itanium and x64. It is ignored for ARM. So on MS's own compiler I am looking for a way to replace the intrinsic memory copy emitted by the compiler with a strict align version.

          Comment

          • CodeJingle
            New Member
            • Feb 2013
            • 4

            #6
            Preliminary finding is that defining a local function with the same name and prototype as an intrinsic will cause the compiler to use the user-defined function over the standard intrinsic, without emitting a duplicate symbol linker error. This can work to my advantage for solving unaligned access issues until I can come up with something better.

            This solution is assuming that intrinsic memory copies and intrinsic memory sets are actually referencing intrinsic function calls, instead of performing the operation directly. So this idea may only work for a debug or non-optimized build, or it may not work at all.

            But that is the only realistic option I have so far. I welcome something better. Thanks for your help :)

            Comment

            Working...