Giter VIP home page Giter VIP logo

Comments (5)

dkfellows avatar dkfellows commented on June 11, 2024

Apparently, gcc 7.2 has the fix (and it was a general bug too). It would be nice to not have the function call overhead in these very critical bits of code!

from spinnaker_tools.

lplana avatar lplana commented on June 11, 2024

Please be very careful. There is no straight-forward way to 'prove' that any changes are correct. Usually we can only state that no issues manifest in the tests we run.

I believe that this issue may be different from the one that we experimented. We only looked at it using 'armcc'.

Our issue is not a bug. In our case, the compiler reorders instructions that it is entitled to reorder because we do not establish a sequence point when we 'inline' the functions that enable/disable interrupts. The function call is the sequence point.

The issue manifests in the following code.

void event_schedule (event_t *e, uint t) {
uint cpsr = int_off ();
... code that inserts event in event queue ...
mode_set (cpsr);
}

In the compiled code, an instruction that reads a variable into a register, which should have been located between the 'int_off()' and 'mode_set()', was placed by the compiler before the 'int_off()' call WHEN the functions were inlined. The compiler is entitled to do it, unless the variable is declared as volatile or a sequence point is established by means other than the function call. If an interrupt occurs at the wrong time, the variable gets the wrong value. This is very difficult to diagnose.

We tried to set the sequence point as follows:

void event_schedule (event_t *e, uint t) {
uint cpsr = int_off ();
__schedule_barrier();
... code that inserts event in event queue ...
__schedule_barrier();
mode_set (cpsr);
}

ARM indicated that this is not the correct way to set a sequence point even thought the 'armcc' documentation states:


__schedule_barrier intrinsic

This intrinsic creates a sequence point where operations before and operations after the sequence point are not merged by the compiler.

They said that the correct way to do it would be:

void event_schedule (event_t *e, uint t) {
uint cpsr = int_off ();
__memory_changed();
... code that inserts event in event queue ...
__force_stores();
mode_set (cpsr);
}

This seems to work, but I think that these intrinsics only apply to 'armcc'.

from spinnaker_tools.

dkfellows avatar dkfellows commented on June 11, 2024

I think that means that we should do this (syntactically compressing it a bit from the real code to improve the readability of this page!):

static __inline __attribute__((always_inline)) void spin1_store_barrier(void) {
    asm volatile("" : : : "cc", "memory");
}

static __inline __attribute__((always_inline)) void spin1_memory_barrier(void) {
    asm volatile("" : : : "cc", "memory");
}

__inline __attribute__((always_inline)) uint spin1_irq_disable(void) {
    uint old_val, new_val;
    asm volatile (
    "mrs	%[old_val], cpsr \n\
     orr	%[new_val], %[old_val], #0x80 \n\
     msr	cpsr_c, %[new_val] \n"
     : [old_val] "=r" (old_val), [new_val] "=r" (new_val) : : );
    spin1_memory_barrier();
    return old_val;
}

__inline __attribute__((always_inline)) void spin1_mode_restore(uint cpsr) {
    spin1_store_barrier();
    asm volatile (
    "msr	cpsr_c, %[cpsr]"
    : : [cpsr] "r" (cpsr) : );
}

Now, spin1_memory_barrier() and spin1_store_barrier() correspond to the __memory_changed() and __force_stores() intrinsics, and I'm calling them in here so that we don't have to modify everywhere that uses the other calls. They're the same in GCC (so far as I can tell, after a very confusing morning of searching online!) but different in ArmCC (where they actually just call the real intrinsics).

from spinnaker_tools.

dkfellows avatar dkfellows commented on June 11, 2024

References:

from spinnaker_tools.

dkfellows avatar dkfellows commented on June 11, 2024

I believe that #107 will put this all to bed. And somehow we've ended up with the code already being inline (but outside of SCAMP; SCAMP is built in THUMB mode…)

from spinnaker_tools.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.